blob: 8f696a1036a54b6749aef62fc693bdadbefb6b99 [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#include <vppinfra/linux/sysfs.h>
35
36#ifndef F_LINUX_SPECIFIC_BASE
37#define F_LINUX_SPECIFIC_BASE 1024
38#endif
39
40#ifndef F_ADD_SEALS
41#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
42#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
43
44#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
45#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
46#define F_SEAL_GROW 0x0004 /* prevent file from growing */
47#define F_SEAL_WRITE 0x0008 /* prevent writes */
48#endif
49
Damjan Marionc63e2a42020-09-16 21:36:00 +020050#ifndef MFD_HUGETLB
51#define MFD_HUGETLB 0x0004U
52#endif
Damjan Marion9787f5f2018-10-24 12:56:32 +020053
Damjan Marion6bfd0762020-09-11 22:16:53 +020054#ifndef MAP_HUGE_SHIFT
55#define MAP_HUGE_SHIFT 26
56#endif
57
Damjan Marionbdbb0c52020-09-17 10:40:44 +020058#ifndef MFD_HUGE_SHIFT
59#define MFD_HUGE_SHIFT 26
60#endif
61
Damjan Marion6bfd0762020-09-11 22:16:53 +020062#ifndef MAP_FIXED_NOREPLACE
63#define MAP_FIXED_NOREPLACE 0x100000
64#endif
Damjan Marion9787f5f2018-10-24 12:56:32 +020065
Damjan Marion70ae0a92020-10-26 10:39:30 +010066static void
67map_lock ()
68{
69 while (clib_atomic_test_and_set (&clib_mem_main.map_lock))
70 CLIB_PAUSE ();
71}
72
73static void
74map_unlock ()
75{
76 clib_atomic_release (&clib_mem_main.map_lock);
77}
78
Damjan Marionc63e2a42020-09-16 21:36:00 +020079static clib_mem_page_sz_t
80legacy_get_log2_default_hugepage_size (void)
81{
82 clib_mem_page_sz_t log2_page_size = CLIB_MEM_PAGE_SZ_UNKNOWN;
83 FILE *fp;
84 char tmp[33] = { };
85
86 if ((fp = fopen ("/proc/meminfo", "r")) == NULL)
87 return CLIB_MEM_PAGE_SZ_UNKNOWN;
88
89 while (fscanf (fp, "%32s", tmp) > 0)
90 if (strncmp ("Hugepagesize:", tmp, 13) == 0)
91 {
92 u32 size;
93 if (fscanf (fp, "%u", &size) > 0)
94 log2_page_size = 10 + min_log2 (size);
95 break;
96 }
97
98 fclose (fp);
99 return log2_page_size;
100}
101
102void
Dave Barach6c89a352022-12-26 14:01:36 -0500103clib_mem_main_init (void)
Damjan Marionc63e2a42020-09-16 21:36:00 +0200104{
105 clib_mem_main_t *mm = &clib_mem_main;
Andrew Yourtchenkoea0b8902022-09-15 11:56:50 +0000106 long sysconf_page_size;
Damjan Marionc63e2a42020-09-16 21:36:00 +0200107 uword page_size;
108 void *va;
109 int fd;
110
111 if (mm->log2_page_sz != CLIB_MEM_PAGE_SZ_UNKNOWN)
112 return;
113
114 /* system page size */
Andrew Yourtchenkoea0b8902022-09-15 11:56:50 +0000115 sysconf_page_size = sysconf (_SC_PAGESIZE);
116 if (sysconf_page_size < 0)
117 {
118 clib_panic ("Could not determine the page size");
119 }
120 page_size = sysconf_page_size;
Damjan Marionc63e2a42020-09-16 21:36:00 +0200121 mm->log2_page_sz = min_log2 (page_size);
122
123 /* default system hugeppage size */
Damjan Marionf8cb7012020-10-09 17:16:55 +0200124 if ((fd = syscall (__NR_memfd_create, "test", MFD_HUGETLB)) != -1)
Damjan Marionc63e2a42020-09-16 21:36:00 +0200125 {
126 mm->log2_default_hugepage_sz = clib_mem_get_fd_log2_page_size (fd);
127 close (fd);
128 }
129 else /* likely kernel older than 4.14 */
130 mm->log2_default_hugepage_sz = legacy_get_log2_default_hugepage_size ();
131
Damjan Marionff011b22021-09-21 11:38:04 +0200132 mm->log2_sys_default_hugepage_sz = mm->log2_default_hugepage_sz;
133
Damjan Marionc63e2a42020-09-16 21:36:00 +0200134 /* numa nodes */
135 va = mmap (0, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE |
136 MAP_ANONYMOUS, -1, 0);
137 if (va == MAP_FAILED)
138 return;
139
140 if (mlock (va, page_size))
141 goto done;
142
143 for (int i = 0; i < CLIB_MAX_NUMAS; i++)
144 {
145 int status;
Damjan Marionf8cb7012020-10-09 17:16:55 +0200146 if (syscall (__NR_move_pages, 0, 1, &va, &i, &status, 0) == 0)
Damjan Marionc63e2a42020-09-16 21:36:00 +0200147 mm->numa_node_bitmap |= 1ULL << i;
148 }
149
150done:
151 munmap (va, page_size);
152}
153
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200154__clib_export u64
Damjan Marion567e61d2018-10-24 17:08:26 +0200155clib_mem_get_fd_page_size (int fd)
Damjan Marion01914ce2017-09-14 19:04:50 +0200156{
157 struct stat st = { 0 };
Chris Lukeb2bcad62017-09-18 08:51:22 -0400158 if (fstat (fd, &st) == -1)
Damjan Marion01914ce2017-09-14 19:04:50 +0200159 return 0;
Florin Corasd3e83a92018-01-16 02:40:18 -0800160 return st.st_blksize;
161}
162
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200163__clib_export clib_mem_page_sz_t
Damjan Marion567e61d2018-10-24 17:08:26 +0200164clib_mem_get_fd_log2_page_size (int fd)
Florin Corasd3e83a92018-01-16 02:40:18 -0800165{
Damjan Marion6bfd0762020-09-11 22:16:53 +0200166 uword page_size = clib_mem_get_fd_page_size (fd);
167 return page_size ? min_log2 (page_size) : CLIB_MEM_PAGE_SZ_UNKNOWN;
Florin Corasd3e83a92018-01-16 02:40:18 -0800168}
169
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200170__clib_export void
Damjan Marionb5095042020-09-11 22:13:46 +0200171clib_mem_vm_randomize_va (uword * requested_va,
172 clib_mem_page_sz_t log2_page_size)
Florin Corasd3e83a92018-01-16 02:40:18 -0800173{
174 u8 bit_mask = 15;
175
176 if (log2_page_size <= 12)
177 bit_mask = 15;
178 else if (log2_page_size > 12 && log2_page_size <= 16)
179 bit_mask = 3;
180 else
181 bit_mask = 0;
182
Haiyang Tana5ab5032018-10-15 06:17:55 -0700183 *requested_va +=
184 (clib_cpu_time_now () & bit_mask) * (1ull << log2_page_size);
Damjan Marion01914ce2017-09-14 19:04:50 +0200185}
186
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200187static int
188legacy_memfd_create (u8 * name)
189{
190 clib_mem_main_t *mm = &clib_mem_main;
191 int fd = -1;
192 char *mount_dir;
Benoît Ganne2b92c702020-09-28 17:34:17 +0200193 u8 *temp;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200194 u8 *filename;
195
Benoît Ganne2b92c702020-09-28 17:34:17 +0200196 /*
197 * Since mkdtemp will modify template string "/tmp/hugepage_mount.XXXXXX",
198 * it must not be a string constant, but should be declared as
199 * a character array.
200 */
201 temp = format (0, "/tmp/hugepage_mount.XXXXXX%c", 0);
202
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200203 /* create mount directory */
Benoît Ganne2b92c702020-09-28 17:34:17 +0200204 if ((mount_dir = mkdtemp ((char *) temp)) == 0)
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200205 {
Benoît Ganne2b92c702020-09-28 17:34:17 +0200206 vec_free (temp);
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200207 vec_reset_length (mm->error);
208 mm->error = clib_error_return_unix (mm->error, "mkdtemp");
Damjan Marion561ae5d2020-09-24 13:53:46 +0200209 return CLIB_MEM_ERROR;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200210 }
211
212 if (mount ("none", mount_dir, "hugetlbfs", 0, NULL))
213 {
Benoît Ganne2b92c702020-09-28 17:34:17 +0200214 vec_free (temp);
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200215 rmdir ((char *) mount_dir);
216 vec_reset_length (mm->error);
217 mm->error = clib_error_return_unix (mm->error, "mount");
Damjan Marion561ae5d2020-09-24 13:53:46 +0200218 return CLIB_MEM_ERROR;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200219 }
220
221 filename = format (0, "%s/%s%c", mount_dir, name, 0);
222
223 if ((fd = open ((char *) filename, O_CREAT | O_RDWR, 0755)) == -1)
224 {
225 vec_reset_length (mm->error);
226 mm->error = clib_error_return_unix (mm->error, "mkdtemp");
227 }
228
229 umount2 ((char *) mount_dir, MNT_DETACH);
230 rmdir ((char *) mount_dir);
231 vec_free (filename);
Benoît Ganne2b92c702020-09-28 17:34:17 +0200232 vec_free (temp);
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200233
234 return fd;
235}
236
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200237__clib_export int
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200238clib_mem_vm_create_fd (clib_mem_page_sz_t log2_page_size, char *fmt, ...)
239{
240 clib_mem_main_t *mm = &clib_mem_main;
241 int fd;
242 unsigned int memfd_flags;
243 va_list va;
244 u8 *s = 0;
245
246 if (log2_page_size == mm->log2_page_sz)
247 log2_page_size = CLIB_MEM_PAGE_SZ_DEFAULT;
Damjan Marionff011b22021-09-21 11:38:04 +0200248 else if (log2_page_size == mm->log2_sys_default_hugepage_sz)
Benoît Ganne2b92c702020-09-28 17:34:17 +0200249 log2_page_size = CLIB_MEM_PAGE_SZ_DEFAULT_HUGE;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200250
251 switch (log2_page_size)
252 {
253 case CLIB_MEM_PAGE_SZ_UNKNOWN:
Damjan Marion561ae5d2020-09-24 13:53:46 +0200254 return CLIB_MEM_ERROR;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200255 case CLIB_MEM_PAGE_SZ_DEFAULT:
256 memfd_flags = MFD_ALLOW_SEALING;
257 break;
258 case CLIB_MEM_PAGE_SZ_DEFAULT_HUGE:
259 memfd_flags = MFD_HUGETLB;
260 break;
261 default:
262 memfd_flags = MFD_HUGETLB | log2_page_size << MFD_HUGE_SHIFT;
263 }
264
265 va_start (va, fmt);
266 s = va_format (0, fmt, &va);
267 va_end (va);
268
269 /* memfd_create maximum string size is 249 chars without trailing zero */
270 if (vec_len (s) > 249)
Damjan Marion8bea5892022-04-04 22:40:45 +0200271 vec_set_len (s, 249);
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200272 vec_add1 (s, 0);
273
274 /* memfd_create introduced in kernel 3.17, we don't support older kernels */
Damjan Marionf8cb7012020-10-09 17:16:55 +0200275 fd = syscall (__NR_memfd_create, (char *) s, memfd_flags);
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200276
277 /* kernel versions < 4.14 does not support memfd_create for huge pages */
278 if (fd == -1 && errno == EINVAL &&
279 log2_page_size == CLIB_MEM_PAGE_SZ_DEFAULT_HUGE)
280 {
281 fd = legacy_memfd_create (s);
282 }
283 else if (fd == -1)
284 {
285 vec_reset_length (mm->error);
286 mm->error = clib_error_return_unix (mm->error, "memfd_create");
287 vec_free (s);
Damjan Marion561ae5d2020-09-24 13:53:46 +0200288 return CLIB_MEM_ERROR;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200289 }
290
291 vec_free (s);
292
293 if ((memfd_flags & MFD_ALLOW_SEALING) &&
294 ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1))
295 {
296 vec_reset_length (mm->error);
297 mm->error = clib_error_return_unix (mm->error, "fcntl (F_ADD_SEALS)");
298 close (fd);
Damjan Marion561ae5d2020-09-24 13:53:46 +0200299 return CLIB_MEM_ERROR;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200300 }
301
302 return fd;
303}
304
Dave Barach16e4a4a2020-04-16 12:00:14 -0400305uword
Damjan Marionb5095042020-09-11 22:13:46 +0200306clib_mem_vm_reserve (uword start, uword size, clib_mem_page_sz_t log2_page_sz)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400307{
Damjan Marion6bfd0762020-09-11 22:16:53 +0200308 clib_mem_main_t *mm = &clib_mem_main;
309 uword pagesize = 1ULL << log2_page_sz;
310 uword sys_page_sz = 1ULL << mm->log2_page_sz;
311 uword n_bytes;
312 void *base = 0, *p;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400313
314 size = round_pow2 (size, pagesize);
315
Damjan Marion6bfd0762020-09-11 22:16:53 +0200316 /* in adition of requested reservation, we also rserve one system page
317 * (typically 4K) adjacent to the start off reservation */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400318
Damjan Marion6bfd0762020-09-11 22:16:53 +0200319 if (start)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400320 {
Damjan Marion6bfd0762020-09-11 22:16:53 +0200321 /* start address is provided, so we just need to make sure we are not
322 * replacing existing map */
323 if (start & pow2_mask (log2_page_sz))
324 return ~0;
325
326 base = (void *) start - sys_page_sz;
327 base = mmap (base, size + sys_page_sz, PROT_NONE,
328 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0);
329 return (base == MAP_FAILED) ? ~0 : start;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400330 }
331
Damjan Marion6bfd0762020-09-11 22:16:53 +0200332 /* to make sure that we get reservation aligned to page_size we need to
333 * request one additional page as mmap will return us address which is
334 * aligned only to system page size */
335 base = mmap (0, size + pagesize, PROT_NONE,
336 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dave Barach16e4a4a2020-04-16 12:00:14 -0400337
Damjan Marion6bfd0762020-09-11 22:16:53 +0200338 if (base == MAP_FAILED)
339 return ~0;
340
341 /* return additional space at the end of allocation */
342 p = base + size + pagesize;
343 n_bytes = (uword) p & pow2_mask (log2_page_sz);
344 if (n_bytes)
345 {
346 p -= n_bytes;
347 munmap (p, n_bytes);
348 }
349
350 /* return additional space at the start of allocation */
351 n_bytes = pagesize - sys_page_sz - n_bytes;
352 if (n_bytes)
353 {
354 munmap (base, n_bytes);
355 base += n_bytes;
356 }
357
358 return (uword) base + sys_page_sz;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400359}
360
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200361__clib_export clib_mem_vm_map_hdr_t *
Damjan Marion6bfd0762020-09-11 22:16:53 +0200362clib_mem_vm_get_next_map_hdr (clib_mem_vm_map_hdr_t * hdr)
363{
364 clib_mem_main_t *mm = &clib_mem_main;
Dave Barach27c35e32020-10-07 09:37:36 -0400365 uword sys_page_sz = 1ULL << mm->log2_page_sz;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200366 clib_mem_vm_map_hdr_t *next;
367 if (hdr == 0)
368 {
369 hdr = mm->first_map;
370 if (hdr)
371 mprotect (hdr, sys_page_sz, PROT_READ);
372 return hdr;
373 }
374 next = hdr->next;
375 mprotect (hdr, sys_page_sz, PROT_NONE);
376 if (next)
377 mprotect (next, sys_page_sz, PROT_READ);
378 return next;
379}
380
381void *
382clib_mem_vm_map_internal (void *base, clib_mem_page_sz_t log2_page_sz,
383 uword size, int fd, uword offset, char *name)
384{
385 clib_mem_main_t *mm = &clib_mem_main;
386 clib_mem_vm_map_hdr_t *hdr;
Dave Barach27c35e32020-10-07 09:37:36 -0400387 uword sys_page_sz = 1ULL << mm->log2_page_sz;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200388 int mmap_flags = MAP_FIXED, is_huge = 0;
389
390 if (fd != -1)
391 {
392 mmap_flags |= MAP_SHARED;
393 log2_page_sz = clib_mem_get_fd_log2_page_size (fd);
394 if (log2_page_sz > mm->log2_page_sz)
395 is_huge = 1;
396 }
397 else
398 {
399 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
400
401 if (log2_page_sz == mm->log2_page_sz)
402 log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT;
403
404 switch (log2_page_sz)
405 {
406 case CLIB_MEM_PAGE_SZ_UNKNOWN:
407 /* will fail later */
408 break;
409 case CLIB_MEM_PAGE_SZ_DEFAULT:
410 log2_page_sz = mm->log2_page_sz;
411 break;
412 case CLIB_MEM_PAGE_SZ_DEFAULT_HUGE:
413 mmap_flags |= MAP_HUGETLB;
414 log2_page_sz = mm->log2_default_hugepage_sz;
415 is_huge = 1;
416 break;
417 default:
418 mmap_flags |= MAP_HUGETLB;
419 mmap_flags |= log2_page_sz << MAP_HUGE_SHIFT;
420 is_huge = 1;
421 }
422 }
423
424 if (log2_page_sz == CLIB_MEM_PAGE_SZ_UNKNOWN)
425 return CLIB_MEM_VM_MAP_FAILED;
426
Dave Barach27c35e32020-10-07 09:37:36 -0400427 size = round_pow2 (size, 1ULL << log2_page_sz);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200428
429 base = (void *) clib_mem_vm_reserve ((uword) base, size, log2_page_sz);
430
431 if (base == (void *) ~0)
432 return CLIB_MEM_VM_MAP_FAILED;
433
434 base = mmap (base, size, PROT_READ | PROT_WRITE, mmap_flags, fd, offset);
435
436 if (base == MAP_FAILED)
437 return CLIB_MEM_VM_MAP_FAILED;
438
439 if (is_huge && (mlock (base, size) != 0))
440 {
441 munmap (base, size);
442 return CLIB_MEM_VM_MAP_FAILED;
443 }
444
445 hdr = mmap (base - sys_page_sz, sys_page_sz, PROT_READ | PROT_WRITE,
446 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
447
448 if (hdr != base - sys_page_sz)
449 {
450 munmap (base, size);
451 return CLIB_MEM_VM_MAP_FAILED;
452 }
453
Damjan Marion70ae0a92020-10-26 10:39:30 +0100454 map_lock ();
455
Damjan Marion6bfd0762020-09-11 22:16:53 +0200456 if (mm->last_map)
457 {
458 mprotect (mm->last_map, sys_page_sz, PROT_READ | PROT_WRITE);
459 mm->last_map->next = hdr;
460 mprotect (mm->last_map, sys_page_sz, PROT_NONE);
461 }
462 else
463 mm->first_map = hdr;
464
Damjan Marion79934e82022-04-05 12:40:31 +0200465 clib_mem_unpoison (hdr, sys_page_sz);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200466 hdr->next = 0;
467 hdr->prev = mm->last_map;
Florin Corasb4d9c5d2021-02-02 15:40:35 -0800468 snprintf (hdr->name, CLIB_VM_MAP_HDR_NAME_MAX_LEN - 1, "%s", (char *) name);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200469 mm->last_map = hdr;
470
471 hdr->base_addr = (uword) base;
472 hdr->log2_page_sz = log2_page_sz;
473 hdr->num_pages = size >> log2_page_sz;
Damjan Marion5ef25162020-09-17 13:29:33 +0200474 hdr->fd = fd;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200475 hdr->name[CLIB_VM_MAP_HDR_NAME_MAX_LEN - 1] = 0;
476 mprotect (hdr, sys_page_sz, PROT_NONE);
477
liuyacan9b8dc8242021-12-22 16:41:21 +0800478 map_unlock ();
479
Damjan Marion79934e82022-04-05 12:40:31 +0200480 clib_mem_unpoison (base, size);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200481 return base;
482}
483
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200484__clib_export int
Damjan Marion6bfd0762020-09-11 22:16:53 +0200485clib_mem_vm_unmap (void *base)
486{
487 clib_mem_main_t *mm = &clib_mem_main;
Dave Barach27c35e32020-10-07 09:37:36 -0400488 uword size, sys_page_sz = 1ULL << mm->log2_page_sz;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200489 clib_mem_vm_map_hdr_t *hdr = base - sys_page_sz;;
490
chenqijunf299dc42021-07-12 10:51:05 +0800491 map_lock ();
Damjan Marion6bfd0762020-09-11 22:16:53 +0200492 if (mprotect (hdr, sys_page_sz, PROT_READ | PROT_WRITE) != 0)
chenqijunf299dc42021-07-12 10:51:05 +0800493 goto out;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200494
495 size = hdr->num_pages << hdr->log2_page_sz;
496 if (munmap ((void *) hdr->base_addr, size) != 0)
chenqijunf299dc42021-07-12 10:51:05 +0800497 goto out;
Damjan Marion70ae0a92020-10-26 10:39:30 +0100498
Damjan Marion6bfd0762020-09-11 22:16:53 +0200499 if (hdr->next)
500 {
501 mprotect (hdr->next, sys_page_sz, PROT_READ | PROT_WRITE);
502 hdr->next->prev = hdr->prev;
503 mprotect (hdr->next, sys_page_sz, PROT_NONE);
504 }
505 else
506 mm->last_map = hdr->prev;
507
508 if (hdr->prev)
509 {
510 mprotect (hdr->prev, sys_page_sz, PROT_READ | PROT_WRITE);
511 hdr->prev->next = hdr->next;
512 mprotect (hdr->prev, sys_page_sz, PROT_NONE);
513 }
514 else
515 mm->first_map = hdr->next;
516
Damjan Marion70ae0a92020-10-26 10:39:30 +0100517 map_unlock ();
518
Damjan Marion6bfd0762020-09-11 22:16:53 +0200519 if (munmap (hdr, sys_page_sz) != 0)
Damjan Marion561ae5d2020-09-24 13:53:46 +0200520 return CLIB_MEM_ERROR;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200521
522 return 0;
chenqijunf299dc42021-07-12 10:51:05 +0800523out:
524 map_unlock ();
525 return CLIB_MEM_ERROR;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200526}
527
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200528__clib_export void
Damjan Marion6bfd0762020-09-11 22:16:53 +0200529clib_mem_get_page_stats (void *start, clib_mem_page_sz_t log2_page_size,
530 uword n_pages, clib_mem_page_stats_t * stats)
531{
532 int i, *status = 0;
533 void **ptr = 0;
534
535 log2_page_size = clib_mem_log2_page_size_validate (log2_page_size);
536
537 vec_validate (status, n_pages - 1);
538 vec_validate (ptr, n_pages - 1);
539
540 for (i = 0; i < n_pages; i++)
541 ptr[i] = start + (i << log2_page_size);
542
543 clib_memset (stats, 0, sizeof (clib_mem_page_stats_t));
Damjan Marionbfa75d62020-10-06 17:46:06 +0200544 stats->total = n_pages;
545 stats->log2_page_sz = log2_page_size;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200546
Damjan Marionf8cb7012020-10-09 17:16:55 +0200547 if (syscall (__NR_move_pages, 0, n_pages, ptr, 0, status, 0) != 0)
Damjan Marion6bfd0762020-09-11 22:16:53 +0200548 {
549 stats->unknown = n_pages;
Florin Coras7c7ed172021-02-26 13:24:47 -0800550 goto done;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200551 }
552
553 for (i = 0; i < n_pages; i++)
554 {
555 if (status[i] >= 0 && status[i] < CLIB_MAX_NUMAS)
556 {
557 stats->mapped++;
558 stats->per_numa[status[i]]++;
559 }
560 else if (status[i] == -EFAULT)
561 stats->not_mapped++;
562 else
563 stats->unknown++;
564 }
Florin Coras7c7ed172021-02-26 13:24:47 -0800565
566done:
567 vec_free (status);
568 vec_free (ptr);
Damjan Marion6bfd0762020-09-11 22:16:53 +0200569}
570
571
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200572__clib_export u64 *
Damjan Marion6bfd0762020-09-11 22:16:53 +0200573clib_mem_vm_get_paddr (void *mem, clib_mem_page_sz_t log2_page_size,
574 int n_pages)
Damjan Marion01914ce2017-09-14 19:04:50 +0200575{
576 int pagesize = sysconf (_SC_PAGESIZE);
577 int fd;
578 int i;
579 u64 *r = 0;
580
Damjan Marion6bfd0762020-09-11 22:16:53 +0200581 log2_page_size = clib_mem_log2_page_size_validate (log2_page_size);
582
Damjan Marion01914ce2017-09-14 19:04:50 +0200583 if ((fd = open ((char *) "/proc/self/pagemap", O_RDONLY)) == -1)
584 return 0;
585
586 for (i = 0; i < n_pages; i++)
587 {
588 u64 seek, pagemap = 0;
589 uword vaddr = pointer_to_uword (mem) + (((u64) i) << log2_page_size);
590 seek = ((u64) vaddr / pagesize) * sizeof (u64);
591 if (lseek (fd, seek, SEEK_SET) != seek)
592 goto done;
593
594 if (read (fd, &pagemap, sizeof (pagemap)) != (sizeof (pagemap)))
595 goto done;
596
597 if ((pagemap & (1ULL << 63)) == 0)
598 goto done;
599
600 pagemap &= pow2_mask (55);
601 vec_add1 (r, pagemap * pagesize);
602 }
603
604done:
605 close (fd);
606 if (vec_len (r) != n_pages)
607 {
608 vec_free (r);
609 return 0;
610 }
611 return r;
612}
613
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200614__clib_export int
Damjan Marion561ae5d2020-09-24 13:53:46 +0200615clib_mem_set_numa_affinity (u8 numa_node, int force)
616{
617 clib_mem_main_t *mm = &clib_mem_main;
Damjan Marionfecb2522022-10-17 17:27:28 +0200618 clib_bitmap_t *bmp = 0;
619 int rv;
Damjan Marion561ae5d2020-09-24 13:53:46 +0200620
621 /* no numa support */
622 if (mm->numa_node_bitmap == 0)
623 {
624 if (numa_node)
625 {
626 vec_reset_length (mm->error);
627 mm->error = clib_error_return (mm->error, "%s: numa not supported",
628 (char *) __func__);
629 return CLIB_MEM_ERROR;
630 }
631 else
632 return 0;
633 }
634
Damjan Marionfecb2522022-10-17 17:27:28 +0200635 bmp = clib_bitmap_set (bmp, numa_node, 1);
Damjan Marion561ae5d2020-09-24 13:53:46 +0200636
Damjan Marionfecb2522022-10-17 17:27:28 +0200637 rv = syscall (__NR_set_mempolicy, force ? MPOL_BIND : MPOL_PREFERRED, bmp,
Andrew Yourtchenko0d367202022-10-21 13:23:51 +0000638 vec_len (bmp) * sizeof (bmp[0]) * 8 + 1);
Damjan Marion561ae5d2020-09-24 13:53:46 +0200639
Damjan Marionfecb2522022-10-17 17:27:28 +0200640 clib_bitmap_free (bmp);
Damjan Marion561ae5d2020-09-24 13:53:46 +0200641 vec_reset_length (mm->error);
Damjan Marionfecb2522022-10-17 17:27:28 +0200642
643 if (rv)
644 {
645 mm->error = clib_error_return_unix (mm->error, (char *) __func__);
646 return CLIB_MEM_ERROR;
647 }
648
Damjan Marion561ae5d2020-09-24 13:53:46 +0200649 return 0;
Damjan Marion561ae5d2020-09-24 13:53:46 +0200650}
651
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200652__clib_export int
Damjan Marion561ae5d2020-09-24 13:53:46 +0200653clib_mem_set_default_numa_affinity ()
654{
655 clib_mem_main_t *mm = &clib_mem_main;
656
Damjan Marionf8cb7012020-10-09 17:16:55 +0200657 if (syscall (__NR_set_mempolicy, MPOL_DEFAULT, 0, 0))
Damjan Marion561ae5d2020-09-24 13:53:46 +0200658 {
659 vec_reset_length (mm->error);
660 mm->error = clib_error_return_unix (mm->error, (char *) __func__);
661 return CLIB_MEM_ERROR;
662 }
663 return 0;
664}
665
Damjan Marion01914ce2017-09-14 19:04:50 +0200666/*
667 * fd.io coding-style-patch-verification: ON
668 *
669 * Local Variables:
670 * eval: (c-set-style "gnu")
671 * End:
672 */