blob: 227e55b6d56dcfbd35b1cb5d32207a6af4465e9e [file] [log] [blame]
Damjan Marion01914ce2017-09-14 19:04:50 +02001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#define _GNU_SOURCE
17#include <stdlib.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <unistd.h>
21#include <sys/mount.h>
22#include <sys/mman.h>
23#include <fcntl.h>
24#include <linux/mempolicy.h>
25#include <linux/memfd.h>
26
27#include <vppinfra/clib.h>
28#include <vppinfra/mem.h>
Florin Corasd3e83a92018-01-16 02:40:18 -080029#include <vppinfra/time.h>
Damjan Marion01914ce2017-09-14 19:04:50 +020030#include <vppinfra/format.h>
31#include <vppinfra/clib_error.h>
32#include <vppinfra/linux/syscall.h>
33#include <vppinfra/linux/sysfs.h>
34
35#ifndef F_LINUX_SPECIFIC_BASE
36#define F_LINUX_SPECIFIC_BASE 1024
37#endif
38
39#ifndef F_ADD_SEALS
40#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
41#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
42
43#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
44#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
45#define F_SEAL_GROW 0x0004 /* prevent file from growing */
46#define F_SEAL_WRITE 0x0008 /* prevent writes */
47#endif
48
Dave Barach9466c452018-08-24 17:21:14 -040049u64
Florin Corasd3e83a92018-01-16 02:40:18 -080050clib_mem_vm_get_page_size (int fd)
Damjan Marion01914ce2017-09-14 19:04:50 +020051{
52 struct stat st = { 0 };
Chris Lukeb2bcad62017-09-18 08:51:22 -040053 if (fstat (fd, &st) == -1)
Damjan Marion01914ce2017-09-14 19:04:50 +020054 return 0;
Florin Corasd3e83a92018-01-16 02:40:18 -080055 return st.st_blksize;
56}
57
58int
59clib_mem_vm_get_log2_page_size (int fd)
60{
61 return min_log2 (clib_mem_vm_get_page_size (fd));
62}
63
Florin Corasb384b542018-01-15 01:08:33 -080064void
Florin Corasd3e83a92018-01-16 02:40:18 -080065clib_mem_vm_randomize_va (uword * requested_va, u32 log2_page_size)
66{
67 u8 bit_mask = 15;
68
69 if (log2_page_size <= 12)
70 bit_mask = 15;
71 else if (log2_page_size > 12 && log2_page_size <= 16)
72 bit_mask = 3;
73 else
74 bit_mask = 0;
75
Haiyang Tana5ab5032018-10-15 06:17:55 -070076 *requested_va +=
77 (clib_cpu_time_now () & bit_mask) * (1ull << log2_page_size);
Damjan Marion01914ce2017-09-14 19:04:50 +020078}
79
80clib_error_t *
81clib_mem_vm_ext_alloc (clib_mem_vm_alloc_t * a)
82{
83 int fd = -1;
84 clib_error_t *err = 0;
85 void *addr = 0;
86 u8 *filename = 0;
Damjan Marion7b185362018-03-04 16:41:35 +010087 int mmap_flags = 0;
Damjan Marion01914ce2017-09-14 19:04:50 +020088 int log2_page_size;
89 int n_pages;
90 int old_mpol = -1;
Dave Barach9466c452018-08-24 17:21:14 -040091 long unsigned int old_mask[16] = { 0 };
Damjan Marion01914ce2017-09-14 19:04:50 +020092
93 /* save old numa mem policy if needed */
94 if (a->flags & (CLIB_MEM_VM_F_NUMA_PREFER | CLIB_MEM_VM_F_NUMA_FORCE))
95 {
96 int rv;
Damjan Marion2e172ea2018-01-03 15:48:34 +000097 rv = get_mempolicy (&old_mpol, old_mask, sizeof (old_mask) * 8 + 1,
98 0, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +020099
100 if (rv == -1)
101 {
Damjan Marion2e172ea2018-01-03 15:48:34 +0000102 if (a->numa_node != 0 && (a->flags & CLIB_MEM_VM_F_NUMA_FORCE) != 0)
Damjan Marion01914ce2017-09-14 19:04:50 +0200103 {
104 err = clib_error_return_unix (0, "get_mempolicy");
105 goto error;
106 }
107 else
108 old_mpol = -1;
109 }
110 }
111
Damjan Marion7b185362018-03-04 16:41:35 +0100112 if (a->flags & CLIB_MEM_VM_F_LOCKED)
113 mmap_flags |= MAP_LOCKED;
114
Damjan Marion01914ce2017-09-14 19:04:50 +0200115 /* if we are creating shared segment, we need file descriptor */
116 if (a->flags & CLIB_MEM_VM_F_SHARED)
117 {
Damjan Marion7b185362018-03-04 16:41:35 +0100118 mmap_flags |= MAP_SHARED;
Damjan Marion01914ce2017-09-14 19:04:50 +0200119 /* if hugepages are needed we need to create mount point */
120 if (a->flags & CLIB_MEM_VM_F_HUGETLB)
121 {
122 char *mount_dir;
123 char template[] = "/tmp/hugepage_mount.XXXXXX";
124
125 mount_dir = mkdtemp (template);
126 if (mount_dir == 0)
127 return clib_error_return_unix (0, "mkdtemp \'%s\'", template);
128
129 if (mount ("none", (char *) mount_dir, "hugetlbfs", 0, NULL))
130 {
Dave Barach9840cca2018-04-10 11:00:50 -0400131 rmdir ((char *) mount_dir);
Damjan Marion01914ce2017-09-14 19:04:50 +0200132 err = clib_error_return_unix (0, "mount hugetlb directory '%s'",
133 mount_dir);
134 goto error;
135 }
136
137 filename = format (0, "%s/%s%c", mount_dir, a->name, 0);
138
139 if ((fd = open ((char *) filename, O_CREAT | O_RDWR, 0755)) == -1)
140 {
Dave Barach9840cca2018-04-10 11:00:50 -0400141 umount2 ((char *) mount_dir, MNT_DETACH);
142 rmdir ((char *) mount_dir);
Damjan Marion01914ce2017-09-14 19:04:50 +0200143 err = clib_error_return_unix (0, "open");
144 goto error;
145 }
146 umount2 ((char *) mount_dir, MNT_DETACH);
147 rmdir ((char *) mount_dir);
148 mmap_flags |= MAP_LOCKED;
149 }
150 else
151 {
152 if ((fd = memfd_create (a->name, MFD_ALLOW_SEALING)) == -1)
153 {
154 err = clib_error_return_unix (0, "memfd_create");
155 goto error;
156 }
157
158 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
159 {
160 err = clib_error_return_unix (0, "fcntl (F_ADD_SEALS)");
161 goto error;
162 }
163 }
Chris Luke879ace32017-09-26 13:15:16 -0400164
Florin Corasd3e83a92018-01-16 02:40:18 -0800165 log2_page_size = clib_mem_vm_get_log2_page_size (fd);
Chris Luke879ace32017-09-26 13:15:16 -0400166 if (log2_page_size == 0)
167 {
168 err = clib_error_return_unix (0, "cannot determine page size");
169 goto error;
170 }
Florin Corasd3e83a92018-01-16 02:40:18 -0800171
172 if (a->requested_va)
173 {
174 clib_mem_vm_randomize_va (&a->requested_va, log2_page_size);
175 mmap_flags |= MAP_FIXED;
176 }
Damjan Marion01914ce2017-09-14 19:04:50 +0200177 }
178 else /* not CLIB_MEM_VM_F_SHARED */
179 {
Damjan Marion7b185362018-03-04 16:41:35 +0100180 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
Damjan Marion01914ce2017-09-14 19:04:50 +0200181 if (a->flags & CLIB_MEM_VM_F_HUGETLB)
182 {
Damjan Marion7b185362018-03-04 16:41:35 +0100183 mmap_flags |= MAP_HUGETLB;
Damjan Marion01914ce2017-09-14 19:04:50 +0200184 log2_page_size = 21;
185 }
186 else
187 {
Damjan Marion01914ce2017-09-14 19:04:50 +0200188 log2_page_size = min_log2 (sysconf (_SC_PAGESIZE));
189 }
190 }
191
192 n_pages = ((a->size - 1) >> log2_page_size) + 1;
193
Damjan Marion01914ce2017-09-14 19:04:50 +0200194 if (a->flags & CLIB_MEM_VM_F_HUGETLB_PREALLOC)
195 {
196 err = clib_sysfs_prealloc_hugepages (a->numa_node,
197 1 << (log2_page_size - 10),
198 n_pages);
199 if (err)
200 goto error;
201
202 }
203
204 if (fd != -1)
Lee Roberts45a09462018-03-07 19:47:00 -0700205 if ((ftruncate (fd, (u64) n_pages * (1 << log2_page_size))) == -1)
Damjan Marion01914ce2017-09-14 19:04:50 +0200206 {
207 err = clib_error_return_unix (0, "ftruncate");
208 goto error;
209 }
210
211 if (old_mpol != -1)
212 {
213 int rv;
Dave Barach9466c452018-08-24 17:21:14 -0400214 long unsigned int mask[16] = { 0 };
Damjan Marion01914ce2017-09-14 19:04:50 +0200215 mask[0] = 1 << a->numa_node;
216 rv = set_mempolicy (MPOL_BIND, mask, sizeof (mask) * 8 + 1);
Damjan Marion915e3f12018-04-18 09:21:24 +0200217 if (rv == -1 && a->numa_node != 0 &&
218 (a->flags & CLIB_MEM_VM_F_NUMA_FORCE) != 0)
Damjan Marion01914ce2017-09-14 19:04:50 +0200219 {
220 err = clib_error_return_unix (0, "set_mempolicy");
221 goto error;
222 }
223 }
224
Florin Corasd3e83a92018-01-16 02:40:18 -0800225 addr = mmap (uword_to_pointer (a->requested_va, void *), a->size,
226 (PROT_READ | PROT_WRITE), mmap_flags, fd, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200227 if (addr == MAP_FAILED)
228 {
229 err = clib_error_return_unix (0, "mmap");
230 goto error;
231 }
232
Damjan Marion2e172ea2018-01-03 15:48:34 +0000233 /* re-apply old numa memory policy */
Damjan Marion01914ce2017-09-14 19:04:50 +0200234 if (old_mpol != -1 &&
235 set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1) == -1)
236 {
237 err = clib_error_return_unix (0, "set_mempolicy");
238 goto error;
239 }
240
241 a->log2_page_size = log2_page_size;
242 a->n_pages = n_pages;
243 a->addr = addr;
244 a->fd = fd;
245 goto done;
246
247error:
248 if (fd != -1)
249 close (fd);
250
251done:
252 vec_free (filename);
253 return err;
254}
255
Haiyang Tan642829d2018-10-09 19:09:45 -0700256void
257clib_mem_vm_ext_free (clib_mem_vm_alloc_t * a)
258{
259 if (a != 0)
260 {
Haiyang Tana5ab5032018-10-15 06:17:55 -0700261 clib_mem_vm_free (a->addr, 1ull << a->log2_page_size);
Haiyang Tan642829d2018-10-09 19:09:45 -0700262 if (a->fd != -1)
263 close (a->fd);
264 }
265}
266
Damjan Marion01914ce2017-09-14 19:04:50 +0200267u64 *
268clib_mem_vm_get_paddr (void *mem, int log2_page_size, int n_pages)
269{
270 int pagesize = sysconf (_SC_PAGESIZE);
271 int fd;
272 int i;
273 u64 *r = 0;
274
275 if ((fd = open ((char *) "/proc/self/pagemap", O_RDONLY)) == -1)
276 return 0;
277
278 for (i = 0; i < n_pages; i++)
279 {
280 u64 seek, pagemap = 0;
281 uword vaddr = pointer_to_uword (mem) + (((u64) i) << log2_page_size);
282 seek = ((u64) vaddr / pagesize) * sizeof (u64);
283 if (lseek (fd, seek, SEEK_SET) != seek)
284 goto done;
285
286 if (read (fd, &pagemap, sizeof (pagemap)) != (sizeof (pagemap)))
287 goto done;
288
289 if ((pagemap & (1ULL << 63)) == 0)
290 goto done;
291
292 pagemap &= pow2_mask (55);
293 vec_add1 (r, pagemap * pagesize);
294 }
295
296done:
297 close (fd);
298 if (vec_len (r) != n_pages)
299 {
300 vec_free (r);
301 return 0;
302 }
303 return r;
304}
305
Florin Corasd3e83a92018-01-16 02:40:18 -0800306clib_error_t *
307clib_mem_vm_ext_map (clib_mem_vm_map_t * a)
308{
309 int mmap_flags = MAP_SHARED;
310 void *addr;
Damjan Marion01914ce2017-09-14 19:04:50 +0200311
Florin Corasd3e83a92018-01-16 02:40:18 -0800312 if (a->requested_va)
313 mmap_flags |= MAP_FIXED;
314
315 addr = (void *) mmap (uword_to_pointer (a->requested_va, void *), a->size,
316 PROT_READ | PROT_WRITE, mmap_flags, a->fd, 0);
317
318 if (addr == MAP_FAILED)
319 return clib_error_return_unix (0, "mmap");
320
321 a->addr = addr;
322 return 0;
323}
Damjan Marion01914ce2017-09-14 19:04:50 +0200324
325/*
326 * fd.io coding-style-patch-verification: ON
327 *
328 * Local Variables:
329 * eval: (c-set-style "gnu")
330 * End:
331 */