blob: 734f5c4788c1cef2f63cdbd9664faf6ad9115f36 [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>
Damjan Marion70ae0a92020-10-26 10:39:30 +010029#include <vppinfra/lock.h>
Florin Corasd3e83a92018-01-16 02:40:18 -080030#include <vppinfra/time.h>
Damjan Marionfecb2522022-10-17 17:27:28 +020031#include <vppinfra/bitmap.h>
Damjan Marion01914ce2017-09-14 19:04:50 +020032#include <vppinfra/format.h>
33#include <vppinfra/clib_error.h>
Damjan Marion01914ce2017-09-14 19:04:50 +020034
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 Marionc63e2a42020-09-16 21:36:00 +020049#ifndef MFD_HUGETLB
50#define MFD_HUGETLB 0x0004U
51#endif
Damjan Marion9787f5f2018-10-24 12:56:32 +020052
Damjan Marion6bfd0762020-09-11 22:16:53 +020053#ifndef MAP_HUGE_SHIFT
54#define MAP_HUGE_SHIFT 26
55#endif
56
Damjan Marionbdbb0c52020-09-17 10:40:44 +020057#ifndef MFD_HUGE_SHIFT
58#define MFD_HUGE_SHIFT 26
59#endif
60
Damjan Marion6bfd0762020-09-11 22:16:53 +020061#ifndef MAP_FIXED_NOREPLACE
62#define MAP_FIXED_NOREPLACE 0x100000
63#endif
Damjan Marion9787f5f2018-10-24 12:56:32 +020064
Damjan Marion70ae0a92020-10-26 10:39:30 +010065static void
66map_lock ()
67{
68 while (clib_atomic_test_and_set (&clib_mem_main.map_lock))
69 CLIB_PAUSE ();
70}
71
72static void
73map_unlock ()
74{
75 clib_atomic_release (&clib_mem_main.map_lock);
76}
77
Damjan Marionc63e2a42020-09-16 21:36:00 +020078static clib_mem_page_sz_t
79legacy_get_log2_default_hugepage_size (void)
80{
81 clib_mem_page_sz_t log2_page_size = CLIB_MEM_PAGE_SZ_UNKNOWN;
82 FILE *fp;
83 char tmp[33] = { };
84
85 if ((fp = fopen ("/proc/meminfo", "r")) == NULL)
86 return CLIB_MEM_PAGE_SZ_UNKNOWN;
87
88 while (fscanf (fp, "%32s", tmp) > 0)
89 if (strncmp ("Hugepagesize:", tmp, 13) == 0)
90 {
91 u32 size;
92 if (fscanf (fp, "%u", &size) > 0)
93 log2_page_size = 10 + min_log2 (size);
94 break;
95 }
96
97 fclose (fp);
98 return log2_page_size;
99}
100
101void
Dave Barach6c89a352022-12-26 14:01:36 -0500102clib_mem_main_init (void)
Damjan Marionc63e2a42020-09-16 21:36:00 +0200103{
104 clib_mem_main_t *mm = &clib_mem_main;
Andrew Yourtchenkoea0b8902022-09-15 11:56:50 +0000105 long sysconf_page_size;
Damjan Marionc63e2a42020-09-16 21:36:00 +0200106 uword page_size;
107 void *va;
108 int fd;
109
110 if (mm->log2_page_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
111 return;
112
113 /* system page size */
Andrew Yourtchenkoea0b8902022-09-15 11:56:50 +0000114 sysconf_page_size = sysconf (_SC_PAGESIZE);
115 if (sysconf_page_size < 0)
116 {
117 clib_panic ("Could not determine the page size");
118 }
119 page_size = sysconf_page_size;
Damjan Marionc63e2a42020-09-16 21:36:00 +0200120 mm->log2_page_sz = min_log2 (page_size);
121
122 /* default system hugeppage size */
Damjan Marionf8cb7012020-10-09 17:16:55 +0200123 if ((fd = syscall (__NR_memfd_create, "test", MFD_HUGETLB)) != -1)
Damjan Marionc63e2a42020-09-16 21:36:00 +0200124 {
125 mm->log2_default_hugepage_sz = clib_mem_get_fd_log2_page_size (fd);
126 close (fd);
127 }
128 else /* likely kernel older than 4.14 */
129 mm->log2_default_hugepage_sz = legacy_get_log2_default_hugepage_size ();
130
Damjan Marionff011b22021-09-21 11:38:04 +0200131 mm->log2_sys_default_hugepage_sz = mm->log2_default_hugepage_sz;
132
Damjan Marionc63e2a42020-09-16 21:36:00 +0200133 /* numa nodes */
134 va = mmap (0, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE |
135 MAP_ANONYMOUS, -1, 0);
136 if (va == MAP_FAILED)
137 return;
138
139 if (mlock (va, page_size))
140 goto done;
141
142 for (int i = 0; i < CLIB_MAX_NUMAS; i++)
143 {
144 int status;
Damjan Marionf8cb7012020-10-09 17:16:55 +0200145 if (syscall (__NR_move_pages, 0, 1, &va, &i, &status, 0) == 0)
Damjan Marionc63e2a42020-09-16 21:36:00 +0200146 mm->numa_node_bitmap |= 1ULL << i;
147 }
148
149done:
150 munmap (va, page_size);
151}
152
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200153__clib_export u64
Damjan Marion567e61d2018-10-24 17:08:26 +0200154clib_mem_get_fd_page_size (int fd)
Damjan Marion01914ce2017-09-14 19:04:50 +0200155{
156 struct stat st = { 0 };
Chris Lukeb2bcad62017-09-18 08:51:22 -0400157 if (fstat (fd, &st) == -1)
Damjan Marion01914ce2017-09-14 19:04:50 +0200158 return 0;
Florin Corasd3e83a92018-01-16 02:40:18 -0800159 return st.st_blksize;
160}
161
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200162__clib_export clib_mem_page_sz_t
Damjan Marion567e61d2018-10-24 17:08:26 +0200163clib_mem_get_fd_log2_page_size (int fd)
Florin Corasd3e83a92018-01-16 02:40:18 -0800164{
Damjan Marion6bfd0762020-09-11 22:16:53 +0200165 uword page_size = clib_mem_get_fd_page_size (fd);
166 return page_size ? min_log2 (page_size) : CLIB_MEM_PAGE_SZ_UNKNOWN;
Florin Corasd3e83a92018-01-16 02:40:18 -0800167}
168
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200169__clib_export void
Damjan Marionb5095042020-09-11 22:13:46 +0200170clib_mem_vm_randomize_va (uword * requested_va,
171 clib_mem_page_sz_t log2_page_size)
Florin Corasd3e83a92018-01-16 02:40:18 -0800172{
173 u8 bit_mask = 15;
174
175 if (log2_page_size <= 12)
176 bit_mask = 15;
177 else if (log2_page_size > 12 && log2_page_size <= 16)
178 bit_mask = 3;
179 else
180 bit_mask = 0;
181
Haiyang Tana5ab5032018-10-15 06:17:55 -0700182 *requested_va +=
183 (clib_cpu_time_now () & bit_mask) * (1ull << log2_page_size);
Damjan Marion01914ce2017-09-14 19:04:50 +0200184}
185
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200186static int
187legacy_memfd_create (u8 * name)
188{
189 clib_mem_main_t *mm = &clib_mem_main;
190 int fd = -1;
191 char *mount_dir;
Benoît Ganne2b92c702020-09-28 17:34:17 +0200192 u8 *temp;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200193 u8 *filename;
194
Benoît Ganne2b92c702020-09-28 17:34:17 +0200195 /*
196 * Since mkdtemp will modify template string "/tmp/hugepage_mount.XXXXXX",
197 * it must not be a string constant, but should be declared as
198 * a character array.
199 */
200 temp = format (0, "/tmp/hugepage_mount.XXXXXX%c", 0);
201
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200202 /* create mount directory */
Benoît Ganne2b92c702020-09-28 17:34:17 +0200203 if ((mount_dir = mkdtemp ((char *) temp)) == 0)
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200204 {
Benoît Ganne2b92c702020-09-28 17:34:17 +0200205 vec_free (temp);
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200206 vec_reset_length (mm->error);
207 mm->error = clib_error_return_unix (mm->error, "mkdtemp");
Damjan Marion561ae5d2020-09-24 13:53:46 +0200208 return CLIB_MEM_ERROR;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200209 }
210
211 if (mount ("none", mount_dir, "hugetlbfs", 0, NULL))
212 {
Benoît Ganne2b92c702020-09-28 17:34:17 +0200213 vec_free (temp);
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200214 rmdir ((char *) mount_dir);
215 vec_reset_length (mm->error);
216 mm->error = clib_error_return_unix (mm->error, "mount");
Damjan Marion561ae5d2020-09-24 13:53:46 +0200217 return CLIB_MEM_ERROR;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200218 }
219
220 filename = format (0, "%s/%s%c", mount_dir, name, 0);
221
222 if ((fd = open ((char *) filename, O_CREAT | O_RDWR, 0755)) == -1)
223 {
224 vec_reset_length (mm->error);
225 mm->error = clib_error_return_unix (mm->error, "mkdtemp");
226 }
227
228 umount2 ((char *) mount_dir, MNT_DETACH);
229 rmdir ((char *) mount_dir);
230 vec_free (filename);
Benoît Ganne2b92c702020-09-28 17:34:17 +0200231 vec_free (temp);
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200232
233 return fd;
234}
235
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200236__clib_export int
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200237clib_mem_vm_create_fd (clib_mem_page_sz_t log2_page_size, char *fmt, ...)
238{
239 clib_mem_main_t *mm = &clib_mem_main;
240 int fd;
241 unsigned int memfd_flags;
242 va_list va;
243 u8 *s = 0;
244
245 if (log2_page_size == mm->log2_page_sz)
246 log2_page_size = CLIB_MEM_PAGE_SZ_DEFAULT;
Damjan Marionff011b22021-09-21 11:38:04 +0200247 else if (log2_page_size == mm->log2_sys_default_hugepage_sz)
Benoît Ganne2b92c702020-09-28 17:34:17 +0200248 log2_page_size = CLIB_MEM_PAGE_SZ_DEFAULT_HUGE;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200249
250 switch (log2_page_size)
251 {
252 case CLIB_MEM_PAGE_SZ_UNKNOWN:
Damjan Marion561ae5d2020-09-24 13:53:46 +0200253 return CLIB_MEM_ERROR;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200254 case CLIB_MEM_PAGE_SZ_DEFAULT:
255 memfd_flags = MFD_ALLOW_SEALING;
256 break;
257 case CLIB_MEM_PAGE_SZ_DEFAULT_HUGE:
258 memfd_flags = MFD_HUGETLB;
259 break;
260 default:
261 memfd_flags = MFD_HUGETLB | log2_page_size << MFD_HUGE_SHIFT;
262 }
263
264 va_start (va, fmt);
265 s = va_format (0, fmt, &va);
266 va_end (va);
267
268 /* memfd_create maximum string size is 249 chars without trailing zero */
269 if (vec_len (s) > 249)
Damjan Marion8bea5892022-04-04 22:40:45 +0200270 vec_set_len (s, 249);
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200271 vec_add1 (s, 0);
272
273 /* memfd_create introduced in kernel 3.17, we don't support older kernels */
Damjan Marionf8cb7012020-10-09 17:16:55 +0200274 fd = syscall (__NR_memfd_create, (char *) s, memfd_flags);
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200275
276 /* kernel versions < 4.14 does not support memfd_create for huge pages */
277 if (fd == -1 && errno == EINVAL &&
278 log2_page_size == CLIB_MEM_PAGE_SZ_DEFAULT_HUGE)
279 {
280 fd = legacy_memfd_create (s);
281 }
282 else if (fd == -1)
283 {
284 vec_reset_length (mm->error);
285 mm->error = clib_error_return_unix (mm->error, "memfd_create");
286 vec_free (s);
Damjan Marion561ae5d2020-09-24 13:53:46 +0200287 return CLIB_MEM_ERROR;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200288 }
289
290 vec_free (s);
291
292 if ((memfd_flags & MFD_ALLOW_SEALING) &&
293 ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1))
294 {
295 vec_reset_length (mm->error);
296 mm->error = clib_error_return_unix (mm->error, "fcntl (F_ADD_SEALS)");
297 close (fd);
Damjan Marion561ae5d2020-09-24 13:53:46 +0200298 return CLIB_MEM_ERROR;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200299 }
300
301 return fd;
302}
303
Dave Barach16e4a4a2020-04-16 12:00:14 -0400304uword
Damjan Marionb5095042020-09-11 22:13:46 +0200305clib_mem_vm_reserve (uword start, uword size, clib_mem_page_sz_t log2_page_sz)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400306{
Damjan Marion6bfd0762020-09-11 22:16:53 +0200307 clib_mem_main_t *mm = &clib_mem_main;
308 uword pagesize = 1ULL << log2_page_sz;
309 uword sys_page_sz = 1ULL << mm->log2_page_sz;
310 uword n_bytes;
311 void *base = 0, *p;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400312
313 size = round_pow2 (size, pagesize);
314
Damjan Marion6bfd0762020-09-11 22:16:53 +0200315 /* in adition of requested reservation, we also rserve one system page
316 * (typically 4K) adjacent to the start off reservation */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400317
Damjan Marion6bfd0762020-09-11 22:16:53 +0200318 if (start)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400319 {
Damjan Marion6bfd0762020-09-11 22:16:53 +0200320 /* start address is provided, so we just need to make sure we are not
321 * replacing existing map */
322 if (start & pow2_mask (log2_page_sz))
323 return ~0;
324
325 base = (void *) start - sys_page_sz;
326 base = mmap (base, size + sys_page_sz, PROT_NONE,
327 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0);
328 return (base == MAP_FAILED) ? ~0 : start;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400329 }
330
Damjan Marion6bfd0762020-09-11 22:16:53 +0200331 /* to make sure that we get reservation aligned to page_size we need to
332 * request one additional page as mmap will return us address which is
333 * aligned only to system page size */
334 base = mmap (0, size + pagesize, PROT_NONE,
335 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dave Barach16e4a4a2020-04-16 12:00:14 -0400336
Damjan Marion6bfd0762020-09-11 22:16:53 +0200337 if (base == MAP_FAILED)
338 return ~0;
339
340 /* return additional space at the end of allocation */
341 p = base + size + pagesize;
342 n_bytes = (uword) p & pow2_mask (log2_page_sz);
343 if (n_bytes)
344 {
345 p -= n_bytes;
346 munmap (p, n_bytes);
347 }
348
349 /* return additional space at the start of allocation */
350 n_bytes = pagesize - sys_page_sz - n_bytes;
351 if (n_bytes)
352 {
353 munmap (base, n_bytes);
354 base += n_bytes;
355 }
356
357 return (uword) base + sys_page_sz;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400358}
359
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200360__clib_export clib_mem_vm_map_hdr_t *
Damjan Marion6bfd0762020-09-11 22:16:53 +0200361clib_mem_vm_get_next_map_hdr (clib_mem_vm_map_hdr_t * hdr)
362{
363 clib_mem_main_t *mm = &clib_mem_main;
Dave Barach27c35e32020-10-07 09:37:36 -0400364 uword sys_page_sz = 1ULL << mm->log2_page_sz;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200365 clib_mem_vm_map_hdr_t *next;
366 if (hdr == 0)
367 {
368 hdr = mm->first_map;
369 if (hdr)
370 mprotect (hdr, sys_page_sz, PROT_READ);
371 return hdr;
372 }
373 next = hdr->next;
374 mprotect (hdr, sys_page_sz, PROT_NONE);
375 if (next)
376 mprotect (next, sys_page_sz, PROT_READ);
377 return next;
378}
379
380void *
381clib_mem_vm_map_internal (void *base, clib_mem_page_sz_t log2_page_sz,
382 uword size, int fd, uword offset, char *name)
383{
384 clib_mem_main_t *mm = &clib_mem_main;
385 clib_mem_vm_map_hdr_t *hdr;
Dave Barach27c35e32020-10-07 09:37:36 -0400386 uword sys_page_sz = 1ULL << mm->log2_page_sz;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200387 int mmap_flags = MAP_FIXED, is_huge = 0;
388
389 if (fd != -1)
390 {
391 mmap_flags |= MAP_SHARED;
392 log2_page_sz = clib_mem_get_fd_log2_page_size (fd);
393 if (log2_page_sz > mm->log2_page_sz)
394 is_huge = 1;
395 }
396 else
397 {
398 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
399
400 if (log2_page_sz == mm->log2_page_sz)
401 log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT;
402
403 switch (log2_page_sz)
404 {
405 case CLIB_MEM_PAGE_SZ_UNKNOWN:
406 /* will fail later */
407 break;
408 case CLIB_MEM_PAGE_SZ_DEFAULT:
409 log2_page_sz = mm->log2_page_sz;
410 break;
411 case CLIB_MEM_PAGE_SZ_DEFAULT_HUGE:
412 mmap_flags |= MAP_HUGETLB;
413 log2_page_sz = mm->log2_default_hugepage_sz;
414 is_huge = 1;
415 break;
416 default:
417 mmap_flags |= MAP_HUGETLB;
418 mmap_flags |= log2_page_sz << MAP_HUGE_SHIFT;
419 is_huge = 1;
420 }
421 }
422
423 if (log2_page_sz == CLIB_MEM_PAGE_SZ_UNKNOWN)
424 return CLIB_MEM_VM_MAP_FAILED;
425
Dave Barach27c35e32020-10-07 09:37:36 -0400426 size = round_pow2 (size, 1ULL << log2_page_sz);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200427
428 base = (void *) clib_mem_vm_reserve ((uword) base, size, log2_page_sz);
429
430 if (base == (void *) ~0)
431 return CLIB_MEM_VM_MAP_FAILED;
432
433 base = mmap (base, size, PROT_READ | PROT_WRITE, mmap_flags, fd, offset);
434
435 if (base == MAP_FAILED)
436 return CLIB_MEM_VM_MAP_FAILED;
437
438 if (is_huge && (mlock (base, size) != 0))
439 {
440 munmap (base, size);
441 return CLIB_MEM_VM_MAP_FAILED;
442 }
443
444 hdr = mmap (base - sys_page_sz, sys_page_sz, PROT_READ | PROT_WRITE,
445 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
446
447 if (hdr != base - sys_page_sz)
448 {
449 munmap (base, size);
450 return CLIB_MEM_VM_MAP_FAILED;
451 }
452
Damjan Marion70ae0a92020-10-26 10:39:30 +0100453 map_lock ();
454
Damjan Marion6bfd0762020-09-11 22:16:53 +0200455 if (mm->last_map)
456 {
457 mprotect (mm->last_map, sys_page_sz, PROT_READ | PROT_WRITE);
458 mm->last_map->next = hdr;
459 mprotect (mm->last_map, sys_page_sz, PROT_NONE);
460 }
461 else
462 mm->first_map = hdr;
463
Damjan Marion79934e82022-04-05 12:40:31 +0200464 clib_mem_unpoison (hdr, sys_page_sz);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200465 hdr->next = 0;
466 hdr->prev = mm->last_map;
Florin Corasb4d9c5d2021-02-02 15:40:35 -0800467 snprintf (hdr->name, CLIB_VM_MAP_HDR_NAME_MAX_LEN - 1, "%s", (char *) name);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200468 mm->last_map = hdr;
469
470 hdr->base_addr = (uword) base;
471 hdr->log2_page_sz = log2_page_sz;
472 hdr->num_pages = size >> log2_page_sz;
Damjan Marion5ef25162020-09-17 13:29:33 +0200473 hdr->fd = fd;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200474 hdr->name[CLIB_VM_MAP_HDR_NAME_MAX_LEN - 1] = 0;
475 mprotect (hdr, sys_page_sz, PROT_NONE);
476
liuyacan9b8dc8242021-12-22 16:41:21 +0800477 map_unlock ();
478
Damjan Marion79934e82022-04-05 12:40:31 +0200479 clib_mem_unpoison (base, size);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200480 return base;
481}
482
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200483__clib_export int
Damjan Marion6bfd0762020-09-11 22:16:53 +0200484clib_mem_vm_unmap (void *base)
485{
486 clib_mem_main_t *mm = &clib_mem_main;
Dave Barach27c35e32020-10-07 09:37:36 -0400487 uword size, sys_page_sz = 1ULL << mm->log2_page_sz;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200488 clib_mem_vm_map_hdr_t *hdr = base - sys_page_sz;;
489
chenqijunf299dc42021-07-12 10:51:05 +0800490 map_lock ();
Damjan Marion6bfd0762020-09-11 22:16:53 +0200491 if (mprotect (hdr, sys_page_sz, PROT_READ | PROT_WRITE) != 0)
chenqijunf299dc42021-07-12 10:51:05 +0800492 goto out;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200493
494 size = hdr->num_pages << hdr->log2_page_sz;
495 if (munmap ((void *) hdr->base_addr, size) != 0)
chenqijunf299dc42021-07-12 10:51:05 +0800496 goto out;
Damjan Marion70ae0a92020-10-26 10:39:30 +0100497
Damjan Marion6bfd0762020-09-11 22:16:53 +0200498 if (hdr->next)
499 {
500 mprotect (hdr->next, sys_page_sz, PROT_READ | PROT_WRITE);
501 hdr->next->prev = hdr->prev;
502 mprotect (hdr->next, sys_page_sz, PROT_NONE);
503 }
504 else
505 mm->last_map = hdr->prev;
506
507 if (hdr->prev)
508 {
509 mprotect (hdr->prev, sys_page_sz, PROT_READ | PROT_WRITE);
510 hdr->prev->next = hdr->next;
511 mprotect (hdr->prev, sys_page_sz, PROT_NONE);
512 }
513 else
514 mm->first_map = hdr->next;
515
Damjan Marion70ae0a92020-10-26 10:39:30 +0100516 map_unlock ();
517
Damjan Marion6bfd0762020-09-11 22:16:53 +0200518 if (munmap (hdr, sys_page_sz) != 0)
Damjan Marion561ae5d2020-09-24 13:53:46 +0200519 return CLIB_MEM_ERROR;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200520
521 return 0;
chenqijunf299dc42021-07-12 10:51:05 +0800522out:
523 map_unlock ();
524 return CLIB_MEM_ERROR;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200525}
526
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200527__clib_export void
Damjan Marion6bfd0762020-09-11 22:16:53 +0200528clib_mem_get_page_stats (void *start, clib_mem_page_sz_t log2_page_size,
529 uword n_pages, clib_mem_page_stats_t * stats)
530{
531 int i, *status = 0;
532 void **ptr = 0;
533
534 log2_page_size = clib_mem_log2_page_size_validate (log2_page_size);
535
536 vec_validate (status, n_pages - 1);
537 vec_validate (ptr, n_pages - 1);
538
539 for (i = 0; i < n_pages; i++)
540 ptr[i] = start + (i << log2_page_size);
541
542 clib_memset (stats, 0, sizeof (clib_mem_page_stats_t));
Damjan Marionbfa75d62020-10-06 17:46:06 +0200543 stats->total = n_pages;
544 stats->log2_page_sz = log2_page_size;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200545
Damjan Marionf8cb7012020-10-09 17:16:55 +0200546 if (syscall (__NR_move_pages, 0, n_pages, ptr, 0, status, 0) != 0)
Damjan Marion6bfd0762020-09-11 22:16:53 +0200547 {
548 stats->unknown = n_pages;
Florin Coras7c7ed172021-02-26 13:24:47 -0800549 goto done;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200550 }
551
552 for (i = 0; i < n_pages; i++)
553 {
554 if (status[i] >= 0 && status[i] < CLIB_MAX_NUMAS)
555 {
556 stats->mapped++;
557 stats->per_numa[status[i]]++;
558 }
559 else if (status[i] == -EFAULT)
560 stats->not_mapped++;
561 else
562 stats->unknown++;
563 }
Florin Coras7c7ed172021-02-26 13:24:47 -0800564
565done:
566 vec_free (status);
567 vec_free (ptr);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200568}
569
570
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200571__clib_export u64 *
Damjan Marion6bfd0762020-09-11 22:16:53 +0200572clib_mem_vm_get_paddr (void *mem, clib_mem_page_sz_t log2_page_size,
573 int n_pages)
Damjan Marion01914ce2017-09-14 19:04:50 +0200574{
575 int pagesize = sysconf (_SC_PAGESIZE);
576 int fd;
577 int i;
578 u64 *r = 0;
579
Damjan Marion6bfd0762020-09-11 22:16:53 +0200580 log2_page_size = clib_mem_log2_page_size_validate (log2_page_size);
581
Damjan Marion01914ce2017-09-14 19:04:50 +0200582 if ((fd = open ((char *) "/proc/self/pagemap", O_RDONLY)) == -1)
583 return 0;
584
585 for (i = 0; i < n_pages; i++)
586 {
587 u64 seek, pagemap = 0;
588 uword vaddr = pointer_to_uword (mem) + (((u64) i) << log2_page_size);
589 seek = ((u64) vaddr / pagesize) * sizeof (u64);
590 if (lseek (fd, seek, SEEK_SET) != seek)
591 goto done;
592
593 if (read (fd, &pagemap, sizeof (pagemap)) != (sizeof (pagemap)))
594 goto done;
595
596 if ((pagemap & (1ULL << 63)) == 0)
597 goto done;
598
599 pagemap &= pow2_mask (55);
600 vec_add1 (r, pagemap * pagesize);
601 }
602
603done:
604 close (fd);
605 if (vec_len (r) != n_pages)
606 {
607 vec_free (r);
608 return 0;
609 }
610 return r;
611}
612
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200613__clib_export int
Damjan Marion561ae5d2020-09-24 13:53:46 +0200614clib_mem_set_numa_affinity (u8 numa_node, int force)
615{
616 clib_mem_main_t *mm = &clib_mem_main;
Damjan Marionfecb2522022-10-17 17:27:28 +0200617 clib_bitmap_t *bmp = 0;
618 int rv;
Damjan Marion561ae5d2020-09-24 13:53:46 +0200619
620 /* no numa support */
621 if (mm->numa_node_bitmap == 0)
622 {
623 if (numa_node)
624 {
625 vec_reset_length (mm->error);
626 mm->error = clib_error_return (mm->error, "%s: numa not supported",
627 (char *) __func__);
628 return CLIB_MEM_ERROR;
629 }
630 else
631 return 0;
632 }
633
Damjan Marionfecb2522022-10-17 17:27:28 +0200634 bmp = clib_bitmap_set (bmp, numa_node, 1);
Damjan Marion561ae5d2020-09-24 13:53:46 +0200635
Damjan Marionfecb2522022-10-17 17:27:28 +0200636 rv = syscall (__NR_set_mempolicy, force ? MPOL_BIND : MPOL_PREFERRED, bmp,
Andrew Yourtchenko0d367202022-10-21 13:23:51 +0000637 vec_len (bmp) * sizeof (bmp[0]) * 8 + 1);
Damjan Marion561ae5d2020-09-24 13:53:46 +0200638
Damjan Marionfecb2522022-10-17 17:27:28 +0200639 clib_bitmap_free (bmp);
Damjan Marion561ae5d2020-09-24 13:53:46 +0200640 vec_reset_length (mm->error);
Damjan Marionfecb2522022-10-17 17:27:28 +0200641
642 if (rv)
643 {
644 mm->error = clib_error_return_unix (mm->error, (char *) __func__);
645 return CLIB_MEM_ERROR;
646 }
647
Damjan Marion561ae5d2020-09-24 13:53:46 +0200648 return 0;
Damjan Marion561ae5d2020-09-24 13:53:46 +0200649}
650
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200651__clib_export int
Damjan Marion561ae5d2020-09-24 13:53:46 +0200652clib_mem_set_default_numa_affinity ()
653{
654 clib_mem_main_t *mm = &clib_mem_main;
655
Damjan Marionf8cb7012020-10-09 17:16:55 +0200656 if (syscall (__NR_set_mempolicy, MPOL_DEFAULT, 0, 0))
Damjan Marion561ae5d2020-09-24 13:53:46 +0200657 {
658 vec_reset_length (mm->error);
659 mm->error = clib_error_return_unix (mm->error, (char *) __func__);
660 return CLIB_MEM_ERROR;
661 }
662 return 0;
663}
664
Damjan Marion01914ce2017-09-14 19:04:50 +0200665/*
666 * fd.io coding-style-patch-verification: ON
667 *
668 * Local Variables:
669 * eval: (c-set-style "gnu")
670 * End:
671 */