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