blob: 407e923fb78e68084d30cf2de9e2f6a5e1e2fadb [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
Damjan Marion9787f5f2018-10-24 12:56:32 +020049
50uword
51clib_mem_get_page_size (void)
52{
53 return getpagesize ();
54}
55
56uword
57clib_mem_get_default_hugepage_size (void)
58{
59 unformat_input_t input;
60 static u32 size = 0;
61 int fd;
62
63 if (size)
64 goto done;
65
Dave Barach036343b2019-01-01 09:45:08 -050066 /*
67 * If the kernel doesn't support hugepages, /proc/meminfo won't
68 * say anything about it. Use the regular page size as a default.
69 */
70 size = clib_mem_get_page_size () / 1024;
71
Damjan Marion9787f5f2018-10-24 12:56:32 +020072 if ((fd = open ("/proc/meminfo", 0)) == -1)
73 return 0;
74
75 unformat_init_clib_file (&input, fd);
76
77 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
78 {
79 if (unformat (&input, "Hugepagesize:%_%u kB", &size))
80 ;
81 else
82 unformat_skip_line (&input);
83 }
84 unformat_free (&input);
85 close (fd);
86done:
87 return 1024ULL * size;
88}
89
Dave Barach9466c452018-08-24 17:21:14 -040090u64
Damjan Marion567e61d2018-10-24 17:08:26 +020091clib_mem_get_fd_page_size (int fd)
Damjan Marion01914ce2017-09-14 19:04:50 +020092{
93 struct stat st = { 0 };
Chris Lukeb2bcad62017-09-18 08:51:22 -040094 if (fstat (fd, &st) == -1)
Damjan Marion01914ce2017-09-14 19:04:50 +020095 return 0;
Florin Corasd3e83a92018-01-16 02:40:18 -080096 return st.st_blksize;
97}
98
99int
Damjan Marion567e61d2018-10-24 17:08:26 +0200100clib_mem_get_fd_log2_page_size (int fd)
Florin Corasd3e83a92018-01-16 02:40:18 -0800101{
Damjan Marion567e61d2018-10-24 17:08:26 +0200102 return min_log2 (clib_mem_get_fd_page_size (fd));
Florin Corasd3e83a92018-01-16 02:40:18 -0800103}
104
Florin Corasb384b542018-01-15 01:08:33 -0800105void
Florin Corasd3e83a92018-01-16 02:40:18 -0800106clib_mem_vm_randomize_va (uword * requested_va, u32 log2_page_size)
107{
108 u8 bit_mask = 15;
109
110 if (log2_page_size <= 12)
111 bit_mask = 15;
112 else if (log2_page_size > 12 && log2_page_size <= 16)
113 bit_mask = 3;
114 else
115 bit_mask = 0;
116
Haiyang Tana5ab5032018-10-15 06:17:55 -0700117 *requested_va +=
118 (clib_cpu_time_now () & bit_mask) * (1ull << log2_page_size);
Damjan Marion01914ce2017-09-14 19:04:50 +0200119}
120
Damjan Marion1636b162018-10-19 12:54:42 +0200121#ifndef MFD_HUGETLB
122#define MFD_HUGETLB 0x0004U
123#endif
124
125clib_error_t *
Damjan Marion567e61d2018-10-24 17:08:26 +0200126clib_mem_create_fd (char *name, int *fdp)
127{
128 int fd;
129
130 ASSERT (name);
131
132 if ((fd = memfd_create (name, MFD_ALLOW_SEALING)) == -1)
133 return clib_error_return_unix (0, "memfd_create");
134
135 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
136 {
137 close (fd);
138 return clib_error_return_unix (0, "fcntl (F_ADD_SEALS)");
139 }
140
141 *fdp = fd;
142 return 0;
143}
144
145clib_error_t *
Damjan Marion1636b162018-10-19 12:54:42 +0200146clib_mem_create_hugetlb_fd (char *name, int *fdp)
147{
148 clib_error_t *err = 0;
149 int fd = -1;
150 static int memfd_hugetlb_supported = 1;
151 char *mount_dir;
152 char template[] = "/tmp/hugepage_mount.XXXXXX";
153 u8 *filename;
154
Juraj Sloboda430634c2018-10-22 10:59:45 +0200155 ASSERT (name);
156
Damjan Marion1636b162018-10-19 12:54:42 +0200157 if (memfd_hugetlb_supported)
158 {
159 if ((fd = memfd_create (name, MFD_HUGETLB)) != -1)
160 goto done;
161
162 /* avoid further tries if memfd MFD_HUGETLB is not supported */
163 if (errno == EINVAL && strnlen (name, 256) <= 249)
164 memfd_hugetlb_supported = 0;
165 }
166
167 mount_dir = mkdtemp (template);
168 if (mount_dir == 0)
169 return clib_error_return_unix (0, "mkdtemp \'%s\'", template);
170
171 if (mount ("none", (char *) mount_dir, "hugetlbfs", 0, NULL))
172 {
173 rmdir ((char *) mount_dir);
174 err = clib_error_return_unix (0, "mount hugetlb directory '%s'",
175 mount_dir);
176 }
177
178 filename = format (0, "%s/%s%c", mount_dir, name, 0);
179 fd = open ((char *) filename, O_CREAT | O_RDWR, 0755);
180 umount2 ((char *) mount_dir, MNT_DETACH);
181 rmdir ((char *) mount_dir);
182
183 if (fd == -1)
184 err = clib_error_return_unix (0, "open");
185
186done:
187 if (fd != -1)
188 fdp[0] = fd;
189 return err;
190}
191
Damjan Marion01914ce2017-09-14 19:04:50 +0200192clib_error_t *
193clib_mem_vm_ext_alloc (clib_mem_vm_alloc_t * a)
194{
195 int fd = -1;
196 clib_error_t *err = 0;
197 void *addr = 0;
198 u8 *filename = 0;
Damjan Marion7b185362018-03-04 16:41:35 +0100199 int mmap_flags = 0;
Damjan Marion01914ce2017-09-14 19:04:50 +0200200 int log2_page_size;
201 int n_pages;
202 int old_mpol = -1;
Dave Barach9466c452018-08-24 17:21:14 -0400203 long unsigned int old_mask[16] = { 0 };
Damjan Marion01914ce2017-09-14 19:04:50 +0200204
205 /* save old numa mem policy if needed */
206 if (a->flags & (CLIB_MEM_VM_F_NUMA_PREFER | CLIB_MEM_VM_F_NUMA_FORCE))
207 {
208 int rv;
Damjan Marion2e172ea2018-01-03 15:48:34 +0000209 rv = get_mempolicy (&old_mpol, old_mask, sizeof (old_mask) * 8 + 1,
210 0, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200211
212 if (rv == -1)
213 {
Damjan Marion2e172ea2018-01-03 15:48:34 +0000214 if (a->numa_node != 0 && (a->flags & CLIB_MEM_VM_F_NUMA_FORCE) != 0)
Damjan Marion01914ce2017-09-14 19:04:50 +0200215 {
216 err = clib_error_return_unix (0, "get_mempolicy");
217 goto error;
218 }
219 else
220 old_mpol = -1;
221 }
222 }
223
Damjan Marion7b185362018-03-04 16:41:35 +0100224 if (a->flags & CLIB_MEM_VM_F_LOCKED)
225 mmap_flags |= MAP_LOCKED;
226
Damjan Marion01914ce2017-09-14 19:04:50 +0200227 /* if we are creating shared segment, we need file descriptor */
228 if (a->flags & CLIB_MEM_VM_F_SHARED)
229 {
Damjan Marion7b185362018-03-04 16:41:35 +0100230 mmap_flags |= MAP_SHARED;
Damjan Marion01914ce2017-09-14 19:04:50 +0200231 /* if hugepages are needed we need to create mount point */
232 if (a->flags & CLIB_MEM_VM_F_HUGETLB)
233 {
Damjan Marion1636b162018-10-19 12:54:42 +0200234 if ((err = clib_mem_create_hugetlb_fd (a->name, &fd)))
235 goto error;
Damjan Marion01914ce2017-09-14 19:04:50 +0200236
Damjan Marion01914ce2017-09-14 19:04:50 +0200237 mmap_flags |= MAP_LOCKED;
238 }
239 else
240 {
Damjan Marion567e61d2018-10-24 17:08:26 +0200241 if ((err = clib_mem_create_fd (a->name, &fd)))
242 goto error;
Damjan Marion01914ce2017-09-14 19:04:50 +0200243 }
Chris Luke879ace32017-09-26 13:15:16 -0400244
Damjan Marion567e61d2018-10-24 17:08:26 +0200245 log2_page_size = clib_mem_get_fd_log2_page_size (fd);
Chris Luke879ace32017-09-26 13:15:16 -0400246 if (log2_page_size == 0)
247 {
248 err = clib_error_return_unix (0, "cannot determine page size");
249 goto error;
250 }
Florin Corasd3e83a92018-01-16 02:40:18 -0800251
252 if (a->requested_va)
253 {
254 clib_mem_vm_randomize_va (&a->requested_va, log2_page_size);
255 mmap_flags |= MAP_FIXED;
256 }
Damjan Marion01914ce2017-09-14 19:04:50 +0200257 }
258 else /* not CLIB_MEM_VM_F_SHARED */
259 {
Damjan Marion7b185362018-03-04 16:41:35 +0100260 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
Damjan Marion01914ce2017-09-14 19:04:50 +0200261 if (a->flags & CLIB_MEM_VM_F_HUGETLB)
262 {
Damjan Marion7b185362018-03-04 16:41:35 +0100263 mmap_flags |= MAP_HUGETLB;
Damjan Marion01914ce2017-09-14 19:04:50 +0200264 log2_page_size = 21;
265 }
266 else
267 {
Damjan Marion01914ce2017-09-14 19:04:50 +0200268 log2_page_size = min_log2 (sysconf (_SC_PAGESIZE));
269 }
270 }
271
272 n_pages = ((a->size - 1) >> log2_page_size) + 1;
273
Damjan Marion01914ce2017-09-14 19:04:50 +0200274 if (a->flags & CLIB_MEM_VM_F_HUGETLB_PREALLOC)
275 {
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200276 err = clib_sysfs_prealloc_hugepages (a->numa_node, log2_page_size,
Damjan Marion01914ce2017-09-14 19:04:50 +0200277 n_pages);
278 if (err)
279 goto error;
280
281 }
282
283 if (fd != -1)
Lee Roberts45a09462018-03-07 19:47:00 -0700284 if ((ftruncate (fd, (u64) n_pages * (1 << log2_page_size))) == -1)
Damjan Marion01914ce2017-09-14 19:04:50 +0200285 {
286 err = clib_error_return_unix (0, "ftruncate");
287 goto error;
288 }
289
290 if (old_mpol != -1)
291 {
292 int rv;
Dave Barach9466c452018-08-24 17:21:14 -0400293 long unsigned int mask[16] = { 0 };
Damjan Marion01914ce2017-09-14 19:04:50 +0200294 mask[0] = 1 << a->numa_node;
295 rv = set_mempolicy (MPOL_BIND, mask, sizeof (mask) * 8 + 1);
Damjan Marion915e3f12018-04-18 09:21:24 +0200296 if (rv == -1 && a->numa_node != 0 &&
297 (a->flags & CLIB_MEM_VM_F_NUMA_FORCE) != 0)
Damjan Marion01914ce2017-09-14 19:04:50 +0200298 {
299 err = clib_error_return_unix (0, "set_mempolicy");
300 goto error;
301 }
302 }
303
Florin Corasd3e83a92018-01-16 02:40:18 -0800304 addr = mmap (uword_to_pointer (a->requested_va, void *), a->size,
305 (PROT_READ | PROT_WRITE), mmap_flags, fd, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200306 if (addr == MAP_FAILED)
307 {
308 err = clib_error_return_unix (0, "mmap");
309 goto error;
310 }
311
Damjan Marion2e172ea2018-01-03 15:48:34 +0000312 /* re-apply old numa memory policy */
Damjan Marion01914ce2017-09-14 19:04:50 +0200313 if (old_mpol != -1 &&
314 set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1) == -1)
315 {
316 err = clib_error_return_unix (0, "set_mempolicy");
317 goto error;
318 }
319
320 a->log2_page_size = log2_page_size;
321 a->n_pages = n_pages;
322 a->addr = addr;
323 a->fd = fd;
324 goto done;
325
326error:
327 if (fd != -1)
328 close (fd);
329
330done:
331 vec_free (filename);
332 return err;
333}
334
Haiyang Tan642829d2018-10-09 19:09:45 -0700335void
336clib_mem_vm_ext_free (clib_mem_vm_alloc_t * a)
337{
338 if (a != 0)
339 {
Haiyang Tana5ab5032018-10-15 06:17:55 -0700340 clib_mem_vm_free (a->addr, 1ull << a->log2_page_size);
Haiyang Tan642829d2018-10-09 19:09:45 -0700341 if (a->fd != -1)
342 close (a->fd);
343 }
344}
345
Damjan Marion01914ce2017-09-14 19:04:50 +0200346u64 *
347clib_mem_vm_get_paddr (void *mem, int log2_page_size, int n_pages)
348{
349 int pagesize = sysconf (_SC_PAGESIZE);
350 int fd;
351 int i;
352 u64 *r = 0;
353
354 if ((fd = open ((char *) "/proc/self/pagemap", O_RDONLY)) == -1)
355 return 0;
356
357 for (i = 0; i < n_pages; i++)
358 {
359 u64 seek, pagemap = 0;
360 uword vaddr = pointer_to_uword (mem) + (((u64) i) << log2_page_size);
361 seek = ((u64) vaddr / pagesize) * sizeof (u64);
362 if (lseek (fd, seek, SEEK_SET) != seek)
363 goto done;
364
365 if (read (fd, &pagemap, sizeof (pagemap)) != (sizeof (pagemap)))
366 goto done;
367
368 if ((pagemap & (1ULL << 63)) == 0)
369 goto done;
370
371 pagemap &= pow2_mask (55);
372 vec_add1 (r, pagemap * pagesize);
373 }
374
375done:
376 close (fd);
377 if (vec_len (r) != n_pages)
378 {
379 vec_free (r);
380 return 0;
381 }
382 return r;
383}
384
Florin Corasd3e83a92018-01-16 02:40:18 -0800385clib_error_t *
386clib_mem_vm_ext_map (clib_mem_vm_map_t * a)
387{
388 int mmap_flags = MAP_SHARED;
389 void *addr;
Damjan Marion01914ce2017-09-14 19:04:50 +0200390
Florin Corasd3e83a92018-01-16 02:40:18 -0800391 if (a->requested_va)
392 mmap_flags |= MAP_FIXED;
393
394 addr = (void *) mmap (uword_to_pointer (a->requested_va, void *), a->size,
395 PROT_READ | PROT_WRITE, mmap_flags, a->fd, 0);
396
397 if (addr == MAP_FAILED)
398 return clib_error_return_unix (0, "mmap");
399
400 a->addr = addr;
401 return 0;
402}
Damjan Marion01914ce2017-09-14 19:04:50 +0200403
404/*
405 * fd.io coding-style-patch-verification: ON
406 *
407 * Local Variables:
408 * eval: (c-set-style "gnu")
409 * End:
410 */