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> |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 29 | #include <vppinfra/time.h> |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 30 | #include <vppinfra/format.h> |
| 31 | #include <vppinfra/clib_error.h> |
| 32 | #include <vppinfra/linux/syscall.h> |
| 33 | #include <vppinfra/linux/sysfs.h> |
| 34 | |
| 35 | #ifndef F_LINUX_SPECIFIC_BASE |
| 36 | #define F_LINUX_SPECIFIC_BASE 1024 |
| 37 | #endif |
| 38 | |
| 39 | #ifndef F_ADD_SEALS |
| 40 | #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) |
| 41 | #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10) |
| 42 | |
| 43 | #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */ |
| 44 | #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */ |
| 45 | #define F_SEAL_GROW 0x0004 /* prevent file from growing */ |
| 46 | #define F_SEAL_WRITE 0x0008 /* prevent writes */ |
| 47 | #endif |
| 48 | |
Damjan Marion | 9787f5f | 2018-10-24 12:56:32 +0200 | [diff] [blame] | 49 | |
| 50 | uword |
| 51 | clib_mem_get_page_size (void) |
| 52 | { |
| 53 | return getpagesize (); |
| 54 | } |
| 55 | |
| 56 | uword |
| 57 | clib_mem_get_default_hugepage_size (void) |
| 58 | { |
| 59 | unformat_input_t input; |
| 60 | static u32 size = 0; |
| 61 | int fd; |
| 62 | |
| 63 | if (size) |
| 64 | goto done; |
| 65 | |
Dave Barach | 036343b | 2019-01-01 09:45:08 -0500 | [diff] [blame] | 66 | /* |
| 67 | * If the kernel doesn't support hugepages, /proc/meminfo won't |
| 68 | * say anything about it. Use the regular page size as a default. |
| 69 | */ |
| 70 | size = clib_mem_get_page_size () / 1024; |
| 71 | |
Damjan Marion | 9787f5f | 2018-10-24 12:56:32 +0200 | [diff] [blame] | 72 | if ((fd = open ("/proc/meminfo", 0)) == -1) |
| 73 | return 0; |
| 74 | |
| 75 | unformat_init_clib_file (&input, fd); |
| 76 | |
| 77 | while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) |
| 78 | { |
| 79 | if (unformat (&input, "Hugepagesize:%_%u kB", &size)) |
| 80 | ; |
| 81 | else |
| 82 | unformat_skip_line (&input); |
| 83 | } |
| 84 | unformat_free (&input); |
| 85 | close (fd); |
| 86 | done: |
| 87 | return 1024ULL * size; |
| 88 | } |
| 89 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 90 | u64 |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 91 | clib_mem_get_fd_page_size (int fd) |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 92 | { |
| 93 | struct stat st = { 0 }; |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 94 | if (fstat (fd, &st) == -1) |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 95 | return 0; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 96 | return st.st_blksize; |
| 97 | } |
| 98 | |
| 99 | int |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 100 | clib_mem_get_fd_log2_page_size (int fd) |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 101 | { |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 102 | return min_log2 (clib_mem_get_fd_page_size (fd)); |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 105 | void |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 106 | clib_mem_vm_randomize_va (uword * requested_va, u32 log2_page_size) |
| 107 | { |
| 108 | u8 bit_mask = 15; |
| 109 | |
| 110 | if (log2_page_size <= 12) |
| 111 | bit_mask = 15; |
| 112 | else if (log2_page_size > 12 && log2_page_size <= 16) |
| 113 | bit_mask = 3; |
| 114 | else |
| 115 | bit_mask = 0; |
| 116 | |
Haiyang Tan | a5ab503 | 2018-10-15 06:17:55 -0700 | [diff] [blame] | 117 | *requested_va += |
| 118 | (clib_cpu_time_now () & bit_mask) * (1ull << log2_page_size); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Damjan Marion | 1636b16 | 2018-10-19 12:54:42 +0200 | [diff] [blame] | 121 | #ifndef MFD_HUGETLB |
| 122 | #define MFD_HUGETLB 0x0004U |
| 123 | #endif |
| 124 | |
| 125 | clib_error_t * |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 126 | clib_mem_create_fd (char *name, int *fdp) |
| 127 | { |
| 128 | int fd; |
| 129 | |
| 130 | ASSERT (name); |
| 131 | |
| 132 | if ((fd = memfd_create (name, MFD_ALLOW_SEALING)) == -1) |
| 133 | return clib_error_return_unix (0, "memfd_create"); |
| 134 | |
| 135 | if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) |
| 136 | { |
| 137 | close (fd); |
| 138 | return clib_error_return_unix (0, "fcntl (F_ADD_SEALS)"); |
| 139 | } |
| 140 | |
| 141 | *fdp = fd; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | clib_error_t * |
Damjan Marion | 1636b16 | 2018-10-19 12:54:42 +0200 | [diff] [blame] | 146 | clib_mem_create_hugetlb_fd (char *name, int *fdp) |
| 147 | { |
| 148 | clib_error_t *err = 0; |
| 149 | int fd = -1; |
| 150 | static int memfd_hugetlb_supported = 1; |
| 151 | char *mount_dir; |
| 152 | char template[] = "/tmp/hugepage_mount.XXXXXX"; |
| 153 | u8 *filename; |
| 154 | |
Juraj Sloboda | 430634c | 2018-10-22 10:59:45 +0200 | [diff] [blame] | 155 | ASSERT (name); |
| 156 | |
Damjan Marion | 1636b16 | 2018-10-19 12:54:42 +0200 | [diff] [blame] | 157 | if (memfd_hugetlb_supported) |
| 158 | { |
| 159 | if ((fd = memfd_create (name, MFD_HUGETLB)) != -1) |
| 160 | goto done; |
| 161 | |
| 162 | /* avoid further tries if memfd MFD_HUGETLB is not supported */ |
| 163 | if (errno == EINVAL && strnlen (name, 256) <= 249) |
| 164 | memfd_hugetlb_supported = 0; |
| 165 | } |
| 166 | |
| 167 | mount_dir = mkdtemp (template); |
| 168 | if (mount_dir == 0) |
| 169 | return clib_error_return_unix (0, "mkdtemp \'%s\'", template); |
| 170 | |
| 171 | if (mount ("none", (char *) mount_dir, "hugetlbfs", 0, NULL)) |
| 172 | { |
| 173 | rmdir ((char *) mount_dir); |
| 174 | err = clib_error_return_unix (0, "mount hugetlb directory '%s'", |
| 175 | mount_dir); |
| 176 | } |
| 177 | |
| 178 | filename = format (0, "%s/%s%c", mount_dir, name, 0); |
| 179 | fd = open ((char *) filename, O_CREAT | O_RDWR, 0755); |
| 180 | umount2 ((char *) mount_dir, MNT_DETACH); |
| 181 | rmdir ((char *) mount_dir); |
| 182 | |
| 183 | if (fd == -1) |
| 184 | err = clib_error_return_unix (0, "open"); |
| 185 | |
| 186 | done: |
| 187 | if (fd != -1) |
| 188 | fdp[0] = fd; |
| 189 | return err; |
| 190 | } |
| 191 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 192 | clib_error_t * |
| 193 | clib_mem_vm_ext_alloc (clib_mem_vm_alloc_t * a) |
| 194 | { |
| 195 | int fd = -1; |
| 196 | clib_error_t *err = 0; |
| 197 | void *addr = 0; |
| 198 | u8 *filename = 0; |
Damjan Marion | 7b18536 | 2018-03-04 16:41:35 +0100 | [diff] [blame] | 199 | int mmap_flags = 0; |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 200 | int log2_page_size; |
| 201 | int n_pages; |
| 202 | int old_mpol = -1; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 203 | long unsigned int old_mask[16] = { 0 }; |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 204 | |
| 205 | /* save old numa mem policy if needed */ |
| 206 | if (a->flags & (CLIB_MEM_VM_F_NUMA_PREFER | CLIB_MEM_VM_F_NUMA_FORCE)) |
| 207 | { |
| 208 | int rv; |
Damjan Marion | 2e172ea | 2018-01-03 15:48:34 +0000 | [diff] [blame] | 209 | rv = get_mempolicy (&old_mpol, old_mask, sizeof (old_mask) * 8 + 1, |
| 210 | 0, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 211 | |
| 212 | if (rv == -1) |
| 213 | { |
Damjan Marion | 2e172ea | 2018-01-03 15:48:34 +0000 | [diff] [blame] | 214 | if (a->numa_node != 0 && (a->flags & CLIB_MEM_VM_F_NUMA_FORCE) != 0) |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 215 | { |
| 216 | err = clib_error_return_unix (0, "get_mempolicy"); |
| 217 | goto error; |
| 218 | } |
| 219 | else |
| 220 | old_mpol = -1; |
| 221 | } |
| 222 | } |
| 223 | |
Damjan Marion | 7b18536 | 2018-03-04 16:41:35 +0100 | [diff] [blame] | 224 | if (a->flags & CLIB_MEM_VM_F_LOCKED) |
| 225 | mmap_flags |= MAP_LOCKED; |
| 226 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 227 | /* if we are creating shared segment, we need file descriptor */ |
| 228 | if (a->flags & CLIB_MEM_VM_F_SHARED) |
| 229 | { |
Damjan Marion | 7b18536 | 2018-03-04 16:41:35 +0100 | [diff] [blame] | 230 | mmap_flags |= MAP_SHARED; |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 231 | /* if hugepages are needed we need to create mount point */ |
| 232 | if (a->flags & CLIB_MEM_VM_F_HUGETLB) |
| 233 | { |
Damjan Marion | 1636b16 | 2018-10-19 12:54:42 +0200 | [diff] [blame] | 234 | if ((err = clib_mem_create_hugetlb_fd (a->name, &fd))) |
| 235 | goto error; |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 236 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 237 | mmap_flags |= MAP_LOCKED; |
| 238 | } |
| 239 | else |
| 240 | { |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 241 | if ((err = clib_mem_create_fd (a->name, &fd))) |
| 242 | goto error; |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 243 | } |
Chris Luke | 879ace3 | 2017-09-26 13:15:16 -0400 | [diff] [blame] | 244 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 245 | log2_page_size = clib_mem_get_fd_log2_page_size (fd); |
Chris Luke | 879ace3 | 2017-09-26 13:15:16 -0400 | [diff] [blame] | 246 | if (log2_page_size == 0) |
| 247 | { |
| 248 | err = clib_error_return_unix (0, "cannot determine page size"); |
| 249 | goto error; |
| 250 | } |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 251 | |
| 252 | if (a->requested_va) |
| 253 | { |
| 254 | clib_mem_vm_randomize_va (&a->requested_va, log2_page_size); |
| 255 | mmap_flags |= MAP_FIXED; |
| 256 | } |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 257 | } |
| 258 | else /* not CLIB_MEM_VM_F_SHARED */ |
| 259 | { |
Damjan Marion | 7b18536 | 2018-03-04 16:41:35 +0100 | [diff] [blame] | 260 | mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS; |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 261 | if (a->flags & CLIB_MEM_VM_F_HUGETLB) |
| 262 | { |
Damjan Marion | 7b18536 | 2018-03-04 16:41:35 +0100 | [diff] [blame] | 263 | mmap_flags |= MAP_HUGETLB; |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 264 | log2_page_size = 21; |
| 265 | } |
| 266 | else |
| 267 | { |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 268 | log2_page_size = min_log2 (sysconf (_SC_PAGESIZE)); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | n_pages = ((a->size - 1) >> log2_page_size) + 1; |
| 273 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 274 | if (a->flags & CLIB_MEM_VM_F_HUGETLB_PREALLOC) |
| 275 | { |
Damjan Marion | 6f3f1cb | 2018-10-22 13:01:46 +0200 | [diff] [blame] | 276 | err = clib_sysfs_prealloc_hugepages (a->numa_node, log2_page_size, |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 277 | n_pages); |
| 278 | if (err) |
| 279 | goto error; |
| 280 | |
| 281 | } |
| 282 | |
| 283 | if (fd != -1) |
Lee Roberts | 45a0946 | 2018-03-07 19:47:00 -0700 | [diff] [blame] | 284 | if ((ftruncate (fd, (u64) n_pages * (1 << log2_page_size))) == -1) |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 285 | { |
| 286 | err = clib_error_return_unix (0, "ftruncate"); |
| 287 | goto error; |
| 288 | } |
| 289 | |
| 290 | if (old_mpol != -1) |
| 291 | { |
| 292 | int rv; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 293 | long unsigned int mask[16] = { 0 }; |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 294 | mask[0] = 1 << a->numa_node; |
| 295 | rv = set_mempolicy (MPOL_BIND, mask, sizeof (mask) * 8 + 1); |
Damjan Marion | 915e3f1 | 2018-04-18 09:21:24 +0200 | [diff] [blame] | 296 | if (rv == -1 && a->numa_node != 0 && |
| 297 | (a->flags & CLIB_MEM_VM_F_NUMA_FORCE) != 0) |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 298 | { |
| 299 | err = clib_error_return_unix (0, "set_mempolicy"); |
| 300 | goto error; |
| 301 | } |
| 302 | } |
| 303 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 304 | addr = mmap (uword_to_pointer (a->requested_va, void *), a->size, |
| 305 | (PROT_READ | PROT_WRITE), mmap_flags, fd, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 306 | if (addr == MAP_FAILED) |
| 307 | { |
| 308 | err = clib_error_return_unix (0, "mmap"); |
| 309 | goto error; |
| 310 | } |
| 311 | |
Damjan Marion | 2e172ea | 2018-01-03 15:48:34 +0000 | [diff] [blame] | 312 | /* re-apply old numa memory policy */ |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 313 | if (old_mpol != -1 && |
| 314 | set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1) == -1) |
| 315 | { |
| 316 | err = clib_error_return_unix (0, "set_mempolicy"); |
| 317 | goto error; |
| 318 | } |
| 319 | |
| 320 | a->log2_page_size = log2_page_size; |
| 321 | a->n_pages = n_pages; |
| 322 | a->addr = addr; |
| 323 | a->fd = fd; |
Benoît Ganne | 1557d9a | 2020-02-07 11:58:16 +0100 | [diff] [blame] | 324 | CLIB_MEM_UNPOISON (addr, a->size); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 325 | goto done; |
| 326 | |
| 327 | error: |
| 328 | if (fd != -1) |
| 329 | close (fd); |
| 330 | |
| 331 | done: |
| 332 | vec_free (filename); |
| 333 | return err; |
| 334 | } |
| 335 | |
Haiyang Tan | 642829d | 2018-10-09 19:09:45 -0700 | [diff] [blame] | 336 | void |
| 337 | clib_mem_vm_ext_free (clib_mem_vm_alloc_t * a) |
| 338 | { |
| 339 | if (a != 0) |
| 340 | { |
Haiyang Tan | a5ab503 | 2018-10-15 06:17:55 -0700 | [diff] [blame] | 341 | clib_mem_vm_free (a->addr, 1ull << a->log2_page_size); |
Haiyang Tan | 642829d | 2018-10-09 19:09:45 -0700 | [diff] [blame] | 342 | if (a->fd != -1) |
| 343 | close (a->fd); |
| 344 | } |
| 345 | } |
| 346 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 347 | u64 * |
| 348 | clib_mem_vm_get_paddr (void *mem, int log2_page_size, int n_pages) |
| 349 | { |
| 350 | int pagesize = sysconf (_SC_PAGESIZE); |
| 351 | int fd; |
| 352 | int i; |
| 353 | u64 *r = 0; |
| 354 | |
| 355 | if ((fd = open ((char *) "/proc/self/pagemap", O_RDONLY)) == -1) |
| 356 | return 0; |
| 357 | |
| 358 | for (i = 0; i < n_pages; i++) |
| 359 | { |
| 360 | u64 seek, pagemap = 0; |
| 361 | uword vaddr = pointer_to_uword (mem) + (((u64) i) << log2_page_size); |
| 362 | seek = ((u64) vaddr / pagesize) * sizeof (u64); |
| 363 | if (lseek (fd, seek, SEEK_SET) != seek) |
| 364 | goto done; |
| 365 | |
| 366 | if (read (fd, &pagemap, sizeof (pagemap)) != (sizeof (pagemap))) |
| 367 | goto done; |
| 368 | |
| 369 | if ((pagemap & (1ULL << 63)) == 0) |
| 370 | goto done; |
| 371 | |
| 372 | pagemap &= pow2_mask (55); |
| 373 | vec_add1 (r, pagemap * pagesize); |
| 374 | } |
| 375 | |
| 376 | done: |
| 377 | close (fd); |
| 378 | if (vec_len (r) != n_pages) |
| 379 | { |
| 380 | vec_free (r); |
| 381 | return 0; |
| 382 | } |
| 383 | return r; |
| 384 | } |
| 385 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 386 | clib_error_t * |
| 387 | clib_mem_vm_ext_map (clib_mem_vm_map_t * a) |
| 388 | { |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 389 | long unsigned int old_mask[16] = { 0 }; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 390 | int mmap_flags = MAP_SHARED; |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 391 | clib_error_t *err = 0; |
| 392 | int old_mpol = -1; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 393 | void *addr; |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 394 | int rv; |
| 395 | |
| 396 | if (a->numa_node) |
| 397 | { |
| 398 | rv = get_mempolicy (&old_mpol, old_mask, sizeof (old_mask) * 8 + 1, 0, |
| 399 | 0); |
| 400 | |
| 401 | if (rv == -1) |
| 402 | { |
| 403 | err = clib_error_return_unix (0, "get_mempolicy"); |
| 404 | goto done; |
| 405 | } |
| 406 | } |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 407 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 408 | if (a->requested_va) |
| 409 | mmap_flags |= MAP_FIXED; |
| 410 | |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 411 | if (old_mpol != -1) |
| 412 | { |
| 413 | long unsigned int mask[16] = { 0 }; |
| 414 | mask[0] = 1 << a->numa_node; |
| 415 | rv = set_mempolicy (MPOL_BIND, mask, sizeof (mask) * 8 + 1); |
| 416 | if (rv == -1) |
| 417 | { |
| 418 | err = clib_error_return_unix (0, "set_mempolicy"); |
| 419 | goto done; |
| 420 | } |
| 421 | } |
| 422 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 423 | addr = (void *) mmap (uword_to_pointer (a->requested_va, void *), a->size, |
| 424 | PROT_READ | PROT_WRITE, mmap_flags, a->fd, 0); |
| 425 | |
| 426 | if (addr == MAP_FAILED) |
| 427 | return clib_error_return_unix (0, "mmap"); |
| 428 | |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 429 | /* re-apply old numa memory policy */ |
| 430 | if (old_mpol != -1 && |
| 431 | set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1) == -1) |
| 432 | { |
| 433 | err = clib_error_return_unix (0, "set_mempolicy"); |
| 434 | goto done; |
| 435 | } |
| 436 | |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 437 | a->addr = addr; |
Benoît Ganne | 1557d9a | 2020-02-07 11:58:16 +0100 | [diff] [blame] | 438 | CLIB_MEM_UNPOISON (addr, a->size); |
Florin Coras | 6fe8998 | 2020-02-07 23:28:41 +0000 | [diff] [blame] | 439 | |
| 440 | done: |
| 441 | return err; |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 442 | } |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 443 | |
| 444 | /* |
| 445 | * fd.io coding-style-patch-verification: ON |
| 446 | * |
| 447 | * Local Variables: |
| 448 | * eval: (c-set-style "gnu") |
| 449 | * End: |
| 450 | */ |