Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 1 | /* |
| 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 Marion | 70ae0a9 | 2020-10-26 10:39:30 +0100 | [diff] [blame] | 29 | #include <vppinfra/lock.h> |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 30 | #include <vppinfra/time.h> |
Damjan Marion | fecb252 | 2022-10-17 17:27:28 +0200 | [diff] [blame] | 31 | #include <vppinfra/bitmap.h> |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 32 | #include <vppinfra/format.h> |
| 33 | #include <vppinfra/clib_error.h> |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 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 Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 49 | #ifndef MFD_HUGETLB |
| 50 | #define MFD_HUGETLB 0x0004U |
| 51 | #endif |
Damjan Marion | 9787f5f | 2018-10-24 12:56:32 +0200 | [diff] [blame] | 52 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 53 | #ifndef MAP_HUGE_SHIFT |
| 54 | #define MAP_HUGE_SHIFT 26 |
| 55 | #endif |
| 56 | |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 57 | #ifndef MFD_HUGE_SHIFT |
| 58 | #define MFD_HUGE_SHIFT 26 |
| 59 | #endif |
| 60 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 61 | #ifndef MAP_FIXED_NOREPLACE |
| 62 | #define MAP_FIXED_NOREPLACE 0x100000 |
| 63 | #endif |
Damjan Marion | 9787f5f | 2018-10-24 12:56:32 +0200 | [diff] [blame] | 64 | |
Damjan Marion | 70ae0a9 | 2020-10-26 10:39:30 +0100 | [diff] [blame] | 65 | static void |
| 66 | map_lock () |
| 67 | { |
| 68 | while (clib_atomic_test_and_set (&clib_mem_main.map_lock)) |
| 69 | CLIB_PAUSE (); |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | map_unlock () |
| 74 | { |
| 75 | clib_atomic_release (&clib_mem_main.map_lock); |
| 76 | } |
| 77 | |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 78 | static clib_mem_page_sz_t |
| 79 | legacy_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 | |
| 101 | void |
Dave Barach | 6c89a35 | 2022-12-26 14:01:36 -0500 | [diff] [blame] | 102 | clib_mem_main_init (void) |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 103 | { |
Florin Coras | 1f870c9 | 2024-07-22 12:09:53 -0700 | [diff] [blame] | 104 | unsigned long nodemask = 0, maxnode = CLIB_MAX_NUMAS; |
| 105 | unsigned long flags = MPOL_F_MEMS_ALLOWED; |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 106 | clib_mem_main_t *mm = &clib_mem_main; |
Andrew Yourtchenko | ea0b890 | 2022-09-15 11:56:50 +0000 | [diff] [blame] | 107 | long sysconf_page_size; |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 108 | uword page_size; |
Florin Coras | 1f870c9 | 2024-07-22 12:09:53 -0700 | [diff] [blame] | 109 | void *va = 0; |
| 110 | int fd, mode; |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 111 | |
| 112 | if (mm->log2_page_sz != CLIB_MEM_PAGE_SZ_UNKNOWN) |
| 113 | return; |
| 114 | |
| 115 | /* system page size */ |
Andrew Yourtchenko | ea0b890 | 2022-09-15 11:56:50 +0000 | [diff] [blame] | 116 | sysconf_page_size = sysconf (_SC_PAGESIZE); |
| 117 | if (sysconf_page_size < 0) |
| 118 | { |
| 119 | clib_panic ("Could not determine the page size"); |
| 120 | } |
| 121 | page_size = sysconf_page_size; |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 122 | mm->log2_page_sz = min_log2 (page_size); |
| 123 | |
| 124 | /* default system hugeppage size */ |
Damjan Marion | f8cb701 | 2020-10-09 17:16:55 +0200 | [diff] [blame] | 125 | if ((fd = syscall (__NR_memfd_create, "test", MFD_HUGETLB)) != -1) |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 126 | { |
| 127 | mm->log2_default_hugepage_sz = clib_mem_get_fd_log2_page_size (fd); |
| 128 | close (fd); |
| 129 | } |
| 130 | else /* likely kernel older than 4.14 */ |
| 131 | mm->log2_default_hugepage_sz = legacy_get_log2_default_hugepage_size (); |
| 132 | |
Damjan Marion | ff011b2 | 2021-09-21 11:38:04 +0200 | [diff] [blame] | 133 | mm->log2_sys_default_hugepage_sz = mm->log2_default_hugepage_sz; |
| 134 | |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 135 | /* numa nodes */ |
Florin Coras | 1f870c9 | 2024-07-22 12:09:53 -0700 | [diff] [blame] | 136 | if (syscall (__NR_get_mempolicy, &mode, &nodemask, maxnode, va, flags) == 0) |
| 137 | mm->numa_node_bitmap = nodemask; |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 140 | __clib_export u64 |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 141 | clib_mem_get_fd_page_size (int fd) |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 142 | { |
| 143 | struct stat st = { 0 }; |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 144 | if (fstat (fd, &st) == -1) |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 145 | return 0; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 146 | return st.st_blksize; |
| 147 | } |
| 148 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 149 | __clib_export clib_mem_page_sz_t |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 150 | clib_mem_get_fd_log2_page_size (int fd) |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 151 | { |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 152 | uword page_size = clib_mem_get_fd_page_size (fd); |
| 153 | return page_size ? min_log2 (page_size) : CLIB_MEM_PAGE_SZ_UNKNOWN; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 156 | __clib_export void |
Damjan Marion | b509504 | 2020-09-11 22:13:46 +0200 | [diff] [blame] | 157 | clib_mem_vm_randomize_va (uword * requested_va, |
| 158 | clib_mem_page_sz_t log2_page_size) |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 159 | { |
| 160 | u8 bit_mask = 15; |
| 161 | |
| 162 | if (log2_page_size <= 12) |
| 163 | bit_mask = 15; |
| 164 | else if (log2_page_size > 12 && log2_page_size <= 16) |
| 165 | bit_mask = 3; |
| 166 | else |
| 167 | bit_mask = 0; |
| 168 | |
Haiyang Tan | a5ab503 | 2018-10-15 06:17:55 -0700 | [diff] [blame] | 169 | *requested_va += |
| 170 | (clib_cpu_time_now () & bit_mask) * (1ull << log2_page_size); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 173 | static int |
| 174 | legacy_memfd_create (u8 * name) |
| 175 | { |
| 176 | clib_mem_main_t *mm = &clib_mem_main; |
| 177 | int fd = -1; |
| 178 | char *mount_dir; |
Benoît Ganne | 2b92c70 | 2020-09-28 17:34:17 +0200 | [diff] [blame] | 179 | u8 *temp; |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 180 | u8 *filename; |
| 181 | |
Benoît Ganne | 2b92c70 | 2020-09-28 17:34:17 +0200 | [diff] [blame] | 182 | /* |
| 183 | * Since mkdtemp will modify template string "/tmp/hugepage_mount.XXXXXX", |
| 184 | * it must not be a string constant, but should be declared as |
| 185 | * a character array. |
| 186 | */ |
| 187 | temp = format (0, "/tmp/hugepage_mount.XXXXXX%c", 0); |
| 188 | |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 189 | /* create mount directory */ |
Benoît Ganne | 2b92c70 | 2020-09-28 17:34:17 +0200 | [diff] [blame] | 190 | if ((mount_dir = mkdtemp ((char *) temp)) == 0) |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 191 | { |
Benoît Ganne | 2b92c70 | 2020-09-28 17:34:17 +0200 | [diff] [blame] | 192 | vec_free (temp); |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 193 | vec_reset_length (mm->error); |
| 194 | mm->error = clib_error_return_unix (mm->error, "mkdtemp"); |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 195 | return CLIB_MEM_ERROR; |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (mount ("none", mount_dir, "hugetlbfs", 0, NULL)) |
| 199 | { |
Benoît Ganne | 2b92c70 | 2020-09-28 17:34:17 +0200 | [diff] [blame] | 200 | vec_free (temp); |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 201 | rmdir ((char *) mount_dir); |
| 202 | vec_reset_length (mm->error); |
| 203 | mm->error = clib_error_return_unix (mm->error, "mount"); |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 204 | return CLIB_MEM_ERROR; |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | filename = format (0, "%s/%s%c", mount_dir, name, 0); |
| 208 | |
| 209 | if ((fd = open ((char *) filename, O_CREAT | O_RDWR, 0755)) == -1) |
| 210 | { |
| 211 | vec_reset_length (mm->error); |
| 212 | mm->error = clib_error_return_unix (mm->error, "mkdtemp"); |
| 213 | } |
| 214 | |
| 215 | umount2 ((char *) mount_dir, MNT_DETACH); |
| 216 | rmdir ((char *) mount_dir); |
| 217 | vec_free (filename); |
Benoît Ganne | 2b92c70 | 2020-09-28 17:34:17 +0200 | [diff] [blame] | 218 | vec_free (temp); |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 219 | |
| 220 | return fd; |
| 221 | } |
| 222 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 223 | __clib_export int |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 224 | clib_mem_vm_create_fd (clib_mem_page_sz_t log2_page_size, char *fmt, ...) |
| 225 | { |
| 226 | clib_mem_main_t *mm = &clib_mem_main; |
| 227 | int fd; |
| 228 | unsigned int memfd_flags; |
| 229 | va_list va; |
| 230 | u8 *s = 0; |
| 231 | |
| 232 | if (log2_page_size == mm->log2_page_sz) |
| 233 | log2_page_size = CLIB_MEM_PAGE_SZ_DEFAULT; |
Damjan Marion | ff011b2 | 2021-09-21 11:38:04 +0200 | [diff] [blame] | 234 | else if (log2_page_size == mm->log2_sys_default_hugepage_sz) |
Benoît Ganne | 2b92c70 | 2020-09-28 17:34:17 +0200 | [diff] [blame] | 235 | log2_page_size = CLIB_MEM_PAGE_SZ_DEFAULT_HUGE; |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 236 | |
| 237 | switch (log2_page_size) |
| 238 | { |
| 239 | case CLIB_MEM_PAGE_SZ_UNKNOWN: |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 240 | return CLIB_MEM_ERROR; |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 241 | case CLIB_MEM_PAGE_SZ_DEFAULT: |
| 242 | memfd_flags = MFD_ALLOW_SEALING; |
| 243 | break; |
| 244 | case CLIB_MEM_PAGE_SZ_DEFAULT_HUGE: |
| 245 | memfd_flags = MFD_HUGETLB; |
| 246 | break; |
| 247 | default: |
| 248 | memfd_flags = MFD_HUGETLB | log2_page_size << MFD_HUGE_SHIFT; |
| 249 | } |
| 250 | |
| 251 | va_start (va, fmt); |
| 252 | s = va_format (0, fmt, &va); |
| 253 | va_end (va); |
| 254 | |
| 255 | /* memfd_create maximum string size is 249 chars without trailing zero */ |
| 256 | if (vec_len (s) > 249) |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 257 | vec_set_len (s, 249); |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 258 | vec_add1 (s, 0); |
| 259 | |
| 260 | /* memfd_create introduced in kernel 3.17, we don't support older kernels */ |
Damjan Marion | f8cb701 | 2020-10-09 17:16:55 +0200 | [diff] [blame] | 261 | fd = syscall (__NR_memfd_create, (char *) s, memfd_flags); |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 262 | |
| 263 | /* kernel versions < 4.14 does not support memfd_create for huge pages */ |
| 264 | if (fd == -1 && errno == EINVAL && |
| 265 | log2_page_size == CLIB_MEM_PAGE_SZ_DEFAULT_HUGE) |
| 266 | { |
| 267 | fd = legacy_memfd_create (s); |
| 268 | } |
| 269 | else if (fd == -1) |
| 270 | { |
| 271 | vec_reset_length (mm->error); |
| 272 | mm->error = clib_error_return_unix (mm->error, "memfd_create"); |
| 273 | vec_free (s); |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 274 | return CLIB_MEM_ERROR; |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | vec_free (s); |
| 278 | |
| 279 | if ((memfd_flags & MFD_ALLOW_SEALING) && |
| 280 | ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)) |
| 281 | { |
| 282 | vec_reset_length (mm->error); |
| 283 | mm->error = clib_error_return_unix (mm->error, "fcntl (F_ADD_SEALS)"); |
| 284 | close (fd); |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 285 | return CLIB_MEM_ERROR; |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | return fd; |
| 289 | } |
| 290 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 291 | uword |
Damjan Marion | b509504 | 2020-09-11 22:13:46 +0200 | [diff] [blame] | 292 | clib_mem_vm_reserve (uword start, uword size, clib_mem_page_sz_t log2_page_sz) |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 293 | { |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 294 | clib_mem_main_t *mm = &clib_mem_main; |
| 295 | uword pagesize = 1ULL << log2_page_sz; |
| 296 | uword sys_page_sz = 1ULL << mm->log2_page_sz; |
| 297 | uword n_bytes; |
| 298 | void *base = 0, *p; |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 299 | |
| 300 | size = round_pow2 (size, pagesize); |
| 301 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 302 | /* in adition of requested reservation, we also rserve one system page |
| 303 | * (typically 4K) adjacent to the start off reservation */ |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 304 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 305 | if (start) |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 306 | { |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 307 | /* start address is provided, so we just need to make sure we are not |
| 308 | * replacing existing map */ |
| 309 | if (start & pow2_mask (log2_page_sz)) |
| 310 | return ~0; |
| 311 | |
| 312 | base = (void *) start - sys_page_sz; |
| 313 | base = mmap (base, size + sys_page_sz, PROT_NONE, |
| 314 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0); |
| 315 | return (base == MAP_FAILED) ? ~0 : start; |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 316 | } |
| 317 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 318 | /* to make sure that we get reservation aligned to page_size we need to |
| 319 | * request one additional page as mmap will return us address which is |
| 320 | * aligned only to system page size */ |
| 321 | base = mmap (0, size + pagesize, PROT_NONE, |
| 322 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 323 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 324 | if (base == MAP_FAILED) |
| 325 | return ~0; |
| 326 | |
| 327 | /* return additional space at the end of allocation */ |
| 328 | p = base + size + pagesize; |
| 329 | n_bytes = (uword) p & pow2_mask (log2_page_sz); |
| 330 | if (n_bytes) |
| 331 | { |
| 332 | p -= n_bytes; |
| 333 | munmap (p, n_bytes); |
| 334 | } |
| 335 | |
| 336 | /* return additional space at the start of allocation */ |
| 337 | n_bytes = pagesize - sys_page_sz - n_bytes; |
| 338 | if (n_bytes) |
| 339 | { |
| 340 | munmap (base, n_bytes); |
| 341 | base += n_bytes; |
| 342 | } |
| 343 | |
| 344 | return (uword) base + sys_page_sz; |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 345 | } |
| 346 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 347 | __clib_export clib_mem_vm_map_hdr_t * |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 348 | clib_mem_vm_get_next_map_hdr (clib_mem_vm_map_hdr_t * hdr) |
| 349 | { |
| 350 | clib_mem_main_t *mm = &clib_mem_main; |
Dave Barach | 27c35e3 | 2020-10-07 09:37:36 -0400 | [diff] [blame] | 351 | uword sys_page_sz = 1ULL << mm->log2_page_sz; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 352 | clib_mem_vm_map_hdr_t *next; |
| 353 | if (hdr == 0) |
| 354 | { |
| 355 | hdr = mm->first_map; |
| 356 | if (hdr) |
| 357 | mprotect (hdr, sys_page_sz, PROT_READ); |
| 358 | return hdr; |
| 359 | } |
| 360 | next = hdr->next; |
| 361 | mprotect (hdr, sys_page_sz, PROT_NONE); |
| 362 | if (next) |
| 363 | mprotect (next, sys_page_sz, PROT_READ); |
| 364 | return next; |
| 365 | } |
| 366 | |
| 367 | void * |
| 368 | clib_mem_vm_map_internal (void *base, clib_mem_page_sz_t log2_page_sz, |
| 369 | uword size, int fd, uword offset, char *name) |
| 370 | { |
| 371 | clib_mem_main_t *mm = &clib_mem_main; |
| 372 | clib_mem_vm_map_hdr_t *hdr; |
Dave Barach | 27c35e3 | 2020-10-07 09:37:36 -0400 | [diff] [blame] | 373 | uword sys_page_sz = 1ULL << mm->log2_page_sz; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 374 | int mmap_flags = MAP_FIXED, is_huge = 0; |
| 375 | |
| 376 | if (fd != -1) |
| 377 | { |
| 378 | mmap_flags |= MAP_SHARED; |
| 379 | log2_page_sz = clib_mem_get_fd_log2_page_size (fd); |
| 380 | if (log2_page_sz > mm->log2_page_sz) |
| 381 | is_huge = 1; |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS; |
| 386 | |
| 387 | if (log2_page_sz == mm->log2_page_sz) |
| 388 | log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT; |
| 389 | |
| 390 | switch (log2_page_sz) |
| 391 | { |
| 392 | case CLIB_MEM_PAGE_SZ_UNKNOWN: |
| 393 | /* will fail later */ |
| 394 | break; |
| 395 | case CLIB_MEM_PAGE_SZ_DEFAULT: |
| 396 | log2_page_sz = mm->log2_page_sz; |
| 397 | break; |
| 398 | case CLIB_MEM_PAGE_SZ_DEFAULT_HUGE: |
| 399 | mmap_flags |= MAP_HUGETLB; |
| 400 | log2_page_sz = mm->log2_default_hugepage_sz; |
| 401 | is_huge = 1; |
| 402 | break; |
| 403 | default: |
| 404 | mmap_flags |= MAP_HUGETLB; |
| 405 | mmap_flags |= log2_page_sz << MAP_HUGE_SHIFT; |
| 406 | is_huge = 1; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | if (log2_page_sz == CLIB_MEM_PAGE_SZ_UNKNOWN) |
| 411 | return CLIB_MEM_VM_MAP_FAILED; |
| 412 | |
Dave Barach | 27c35e3 | 2020-10-07 09:37:36 -0400 | [diff] [blame] | 413 | size = round_pow2 (size, 1ULL << log2_page_sz); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 414 | |
| 415 | base = (void *) clib_mem_vm_reserve ((uword) base, size, log2_page_sz); |
| 416 | |
| 417 | if (base == (void *) ~0) |
| 418 | return CLIB_MEM_VM_MAP_FAILED; |
| 419 | |
| 420 | base = mmap (base, size, PROT_READ | PROT_WRITE, mmap_flags, fd, offset); |
| 421 | |
| 422 | if (base == MAP_FAILED) |
| 423 | return CLIB_MEM_VM_MAP_FAILED; |
| 424 | |
| 425 | if (is_huge && (mlock (base, size) != 0)) |
| 426 | { |
| 427 | munmap (base, size); |
| 428 | return CLIB_MEM_VM_MAP_FAILED; |
| 429 | } |
| 430 | |
| 431 | hdr = mmap (base - sys_page_sz, sys_page_sz, PROT_READ | PROT_WRITE, |
| 432 | MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); |
| 433 | |
| 434 | if (hdr != base - sys_page_sz) |
| 435 | { |
| 436 | munmap (base, size); |
| 437 | return CLIB_MEM_VM_MAP_FAILED; |
| 438 | } |
| 439 | |
Damjan Marion | 70ae0a9 | 2020-10-26 10:39:30 +0100 | [diff] [blame] | 440 | map_lock (); |
| 441 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 442 | if (mm->last_map) |
| 443 | { |
| 444 | mprotect (mm->last_map, sys_page_sz, PROT_READ | PROT_WRITE); |
| 445 | mm->last_map->next = hdr; |
| 446 | mprotect (mm->last_map, sys_page_sz, PROT_NONE); |
| 447 | } |
| 448 | else |
| 449 | mm->first_map = hdr; |
| 450 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 451 | clib_mem_unpoison (hdr, sys_page_sz); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 452 | hdr->next = 0; |
| 453 | hdr->prev = mm->last_map; |
Florin Coras | b4d9c5d | 2021-02-02 15:40:35 -0800 | [diff] [blame] | 454 | snprintf (hdr->name, CLIB_VM_MAP_HDR_NAME_MAX_LEN - 1, "%s", (char *) name); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 455 | mm->last_map = hdr; |
| 456 | |
| 457 | hdr->base_addr = (uword) base; |
| 458 | hdr->log2_page_sz = log2_page_sz; |
| 459 | hdr->num_pages = size >> log2_page_sz; |
Damjan Marion | 5ef2516 | 2020-09-17 13:29:33 +0200 | [diff] [blame] | 460 | hdr->fd = fd; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 461 | hdr->name[CLIB_VM_MAP_HDR_NAME_MAX_LEN - 1] = 0; |
| 462 | mprotect (hdr, sys_page_sz, PROT_NONE); |
| 463 | |
liuyacan | 9b8dc824 | 2021-12-22 16:41:21 +0800 | [diff] [blame] | 464 | map_unlock (); |
| 465 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 466 | clib_mem_unpoison (base, size); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 467 | return base; |
| 468 | } |
| 469 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 470 | __clib_export int |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 471 | clib_mem_vm_unmap (void *base) |
| 472 | { |
| 473 | clib_mem_main_t *mm = &clib_mem_main; |
Dave Barach | 27c35e3 | 2020-10-07 09:37:36 -0400 | [diff] [blame] | 474 | uword size, sys_page_sz = 1ULL << mm->log2_page_sz; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 475 | clib_mem_vm_map_hdr_t *hdr = base - sys_page_sz;; |
| 476 | |
chenqijun | f299dc4 | 2021-07-12 10:51:05 +0800 | [diff] [blame] | 477 | map_lock (); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 478 | if (mprotect (hdr, sys_page_sz, PROT_READ | PROT_WRITE) != 0) |
chenqijun | f299dc4 | 2021-07-12 10:51:05 +0800 | [diff] [blame] | 479 | goto out; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 480 | |
| 481 | size = hdr->num_pages << hdr->log2_page_sz; |
| 482 | if (munmap ((void *) hdr->base_addr, size) != 0) |
chenqijun | f299dc4 | 2021-07-12 10:51:05 +0800 | [diff] [blame] | 483 | goto out; |
Damjan Marion | 70ae0a9 | 2020-10-26 10:39:30 +0100 | [diff] [blame] | 484 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 485 | if (hdr->next) |
| 486 | { |
| 487 | mprotect (hdr->next, sys_page_sz, PROT_READ | PROT_WRITE); |
| 488 | hdr->next->prev = hdr->prev; |
| 489 | mprotect (hdr->next, sys_page_sz, PROT_NONE); |
| 490 | } |
| 491 | else |
| 492 | mm->last_map = hdr->prev; |
| 493 | |
| 494 | if (hdr->prev) |
| 495 | { |
| 496 | mprotect (hdr->prev, sys_page_sz, PROT_READ | PROT_WRITE); |
| 497 | hdr->prev->next = hdr->next; |
| 498 | mprotect (hdr->prev, sys_page_sz, PROT_NONE); |
| 499 | } |
| 500 | else |
| 501 | mm->first_map = hdr->next; |
| 502 | |
Damjan Marion | 70ae0a9 | 2020-10-26 10:39:30 +0100 | [diff] [blame] | 503 | map_unlock (); |
| 504 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 505 | if (munmap (hdr, sys_page_sz) != 0) |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 506 | return CLIB_MEM_ERROR; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 507 | |
| 508 | return 0; |
chenqijun | f299dc4 | 2021-07-12 10:51:05 +0800 | [diff] [blame] | 509 | out: |
| 510 | map_unlock (); |
| 511 | return CLIB_MEM_ERROR; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 514 | __clib_export void |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 515 | clib_mem_get_page_stats (void *start, clib_mem_page_sz_t log2_page_size, |
| 516 | uword n_pages, clib_mem_page_stats_t * stats) |
| 517 | { |
| 518 | int i, *status = 0; |
| 519 | void **ptr = 0; |
Jay Wang | c44fa93 | 2024-04-19 12:16:41 +0000 | [diff] [blame] | 520 | unsigned char incore; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 521 | |
| 522 | log2_page_size = clib_mem_log2_page_size_validate (log2_page_size); |
| 523 | |
| 524 | vec_validate (status, n_pages - 1); |
| 525 | vec_validate (ptr, n_pages - 1); |
| 526 | |
| 527 | for (i = 0; i < n_pages; i++) |
| 528 | ptr[i] = start + (i << log2_page_size); |
| 529 | |
| 530 | clib_memset (stats, 0, sizeof (clib_mem_page_stats_t)); |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 531 | stats->total = n_pages; |
| 532 | stats->log2_page_sz = log2_page_size; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 533 | |
Damjan Marion | f8cb701 | 2020-10-09 17:16:55 +0200 | [diff] [blame] | 534 | if (syscall (__NR_move_pages, 0, n_pages, ptr, 0, status, 0) != 0) |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 535 | { |
| 536 | stats->unknown = n_pages; |
Florin Coras | 7c7ed17 | 2021-02-26 13:24:47 -0800 | [diff] [blame] | 537 | goto done; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | for (i = 0; i < n_pages; i++) |
| 541 | { |
Jay Wang | c44fa93 | 2024-04-19 12:16:41 +0000 | [diff] [blame] | 542 | /* move_pages() returns -ENONET in status for huge pages on 5.19+ kernel. |
| 543 | * Retry with get_mempolicy() to obtain NUMA node info only if the pages |
| 544 | * are allocated and in memory, which is checked by mincore(). */ |
| 545 | if (status[i] == -ENOENT && |
| 546 | syscall (__NR_mincore, ptr[i], 1, &incore) == 0 && (incore & 1) != 0) |
| 547 | { |
| 548 | if (syscall (__NR_get_mempolicy, &status[i], 0, 0, ptr[i], |
| 549 | MPOL_F_NODE | MPOL_F_ADDR) != 0) |
| 550 | { |
| 551 | /* if get_mempolicy fails, keep the original value in status */ |
| 552 | status[i] = -ENONET; |
| 553 | } |
| 554 | } |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 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 Coras | 7c7ed17 | 2021-02-26 13:24:47 -0800 | [diff] [blame] | 565 | |
| 566 | done: |
| 567 | vec_free (status); |
| 568 | vec_free (ptr); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 572 | __clib_export u64 * |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 573 | clib_mem_vm_get_paddr (void *mem, clib_mem_page_sz_t log2_page_size, |
| 574 | int n_pages) |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 575 | { |
| 576 | int pagesize = sysconf (_SC_PAGESIZE); |
| 577 | int fd; |
| 578 | int i; |
| 579 | u64 *r = 0; |
| 580 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 581 | log2_page_size = clib_mem_log2_page_size_validate (log2_page_size); |
| 582 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 583 | 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 | |
| 604 | done: |
| 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 Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 614 | __clib_export int |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 615 | clib_mem_set_numa_affinity (u8 numa_node, int force) |
| 616 | { |
| 617 | clib_mem_main_t *mm = &clib_mem_main; |
Damjan Marion | fecb252 | 2022-10-17 17:27:28 +0200 | [diff] [blame] | 618 | clib_bitmap_t *bmp = 0; |
| 619 | int rv; |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 620 | |
| 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 Marion | fecb252 | 2022-10-17 17:27:28 +0200 | [diff] [blame] | 635 | bmp = clib_bitmap_set (bmp, numa_node, 1); |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 636 | |
Damjan Marion | fecb252 | 2022-10-17 17:27:28 +0200 | [diff] [blame] | 637 | rv = syscall (__NR_set_mempolicy, force ? MPOL_BIND : MPOL_PREFERRED, bmp, |
Andrew Yourtchenko | 0d36720 | 2022-10-21 13:23:51 +0000 | [diff] [blame] | 638 | vec_len (bmp) * sizeof (bmp[0]) * 8 + 1); |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 639 | |
Damjan Marion | fecb252 | 2022-10-17 17:27:28 +0200 | [diff] [blame] | 640 | clib_bitmap_free (bmp); |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 641 | vec_reset_length (mm->error); |
Damjan Marion | fecb252 | 2022-10-17 17:27:28 +0200 | [diff] [blame] | 642 | |
| 643 | if (rv) |
| 644 | { |
| 645 | mm->error = clib_error_return_unix (mm->error, (char *) __func__); |
| 646 | return CLIB_MEM_ERROR; |
| 647 | } |
| 648 | |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 649 | return 0; |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 652 | __clib_export int |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 653 | clib_mem_set_default_numa_affinity () |
| 654 | { |
| 655 | clib_mem_main_t *mm = &clib_mem_main; |
| 656 | |
Damjan Marion | f8cb701 | 2020-10-09 17:16:55 +0200 | [diff] [blame] | 657 | if (syscall (__NR_set_mempolicy, MPOL_DEFAULT, 0, 0)) |
Damjan Marion | 561ae5d | 2020-09-24 13:53:46 +0200 | [diff] [blame] | 658 | { |
| 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 Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 666 | /* |
| 667 | * fd.io coding-style-patch-verification: ON |
| 668 | * |
| 669 | * Local Variables: |
| 670 | * eval: (c-set-style "gnu") |
| 671 | * End: |
| 672 | */ |