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