Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 <fcntl.h> |
Artem Belov | f6defa1 | 2019-02-26 01:47:34 +0000 | [diff] [blame] | 21 | #include <unistd.h> |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 22 | #include <linux/mempolicy.h> |
| 23 | #include <linux/memfd.h> |
| 24 | |
| 25 | #include <vppinfra/format.h> |
| 26 | #include <vppinfra/linux/syscall.h> |
| 27 | #include <vppinfra/linux/sysfs.h> |
| 28 | #include <vppinfra/mem.h> |
| 29 | #include <vppinfra/hash.h> |
| 30 | #include <vppinfra/pmalloc.h> |
| 31 | |
| 32 | #if __SIZEOF_POINTER__ >= 8 |
| 33 | #define DEFAULT_RESERVED_MB 16384 |
| 34 | #else |
| 35 | #define DEFAULT_RESERVED_MB 256 |
| 36 | #endif |
| 37 | |
| 38 | static inline clib_pmalloc_chunk_t * |
| 39 | get_chunk (clib_pmalloc_page_t * pp, u32 index) |
| 40 | { |
| 41 | return pool_elt_at_index (pp->chunks, index); |
| 42 | } |
| 43 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 44 | static inline uword |
| 45 | pmalloc_size2pages (uword size, u32 log2_page_sz) |
| 46 | { |
| 47 | return round_pow2 (size, 1ULL << log2_page_sz) >> log2_page_sz; |
| 48 | } |
| 49 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 50 | static inline int |
| 51 | pmalloc_validate_numa_node (u32 * numa_node) |
| 52 | { |
| 53 | if (*numa_node == CLIB_PMALLOC_NUMA_LOCAL) |
| 54 | { |
| 55 | u32 cpu; |
| 56 | if (getcpu (&cpu, numa_node, 0) != 0) |
| 57 | return 1; |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | int |
Damjan Marion | 5a6c809 | 2019-02-21 14:44:59 +0100 | [diff] [blame] | 63 | clib_pmalloc_init (clib_pmalloc_main_t * pm, uword base_addr, uword size) |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 64 | { |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 65 | uword off, pagesize; |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 66 | u64 *pt = 0; |
Damjan Marion | 5a6c809 | 2019-02-21 14:44:59 +0100 | [diff] [blame] | 67 | int mmap_flags; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 68 | |
| 69 | ASSERT (pm->error == 0); |
| 70 | |
Damjan Marion | 9787f5f | 2018-10-24 12:56:32 +0200 | [diff] [blame] | 71 | pagesize = clib_mem_get_default_hugepage_size (); |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 72 | pm->def_log2_page_sz = min_log2 (pagesize); |
| 73 | pm->sys_log2_page_sz = min_log2 (sysconf (_SC_PAGESIZE)); |
| 74 | pm->lookup_log2_page_sz = pm->def_log2_page_sz; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 75 | |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 76 | /* check if pagemap is accessible */ |
| 77 | pt = clib_mem_vm_get_paddr (&pt, pm->sys_log2_page_sz, 1); |
| 78 | if (pt == 0 || pt[0] == 0) |
| 79 | pm->flags |= CLIB_PMALLOC_F_NO_PAGEMAP; |
| 80 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 81 | size = size ? size : ((u64) DEFAULT_RESERVED_MB) << 20; |
| 82 | size = round_pow2 (size, pagesize); |
| 83 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 84 | pm->max_pages = size >> pm->def_log2_page_sz; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 85 | |
| 86 | /* reserve VA space for future growth */ |
Damjan Marion | 5a6c809 | 2019-02-21 14:44:59 +0100 | [diff] [blame] | 87 | mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 88 | |
| 89 | if (base_addr) |
| 90 | mmap_flags |= MAP_FIXED; |
| 91 | |
| 92 | pm->base = mmap (uword_to_pointer (base_addr, void *), size + pagesize, |
| 93 | PROT_NONE, mmap_flags, -1, 0); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 94 | |
| 95 | if (pm->base == MAP_FAILED) |
| 96 | { |
| 97 | pm->error = clib_error_return_unix (0, "failed to reserve %u pages"); |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | off = round_pow2 (pointer_to_uword (pm->base), pagesize) - |
| 102 | pointer_to_uword (pm->base); |
| 103 | |
| 104 | /* trim start and end of reservation to be page aligned */ |
| 105 | if (off) |
| 106 | { |
| 107 | munmap (pm->base, off); |
| 108 | pm->base += off; |
| 109 | } |
| 110 | |
Damjan Marion | 878b65a | 2018-10-26 10:29:35 +0200 | [diff] [blame] | 111 | munmap (pm->base + ((uword) pm->max_pages * pagesize), pagesize - off); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static inline void * |
| 116 | alloc_chunk_from_page (clib_pmalloc_main_t * pm, clib_pmalloc_page_t * pp, |
| 117 | u32 n_blocks, u32 block_align, u32 numa_node) |
| 118 | { |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 119 | clib_pmalloc_chunk_t *c = 0; |
| 120 | clib_pmalloc_arena_t *a; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 121 | void *va; |
| 122 | u32 off; |
| 123 | u32 alloc_chunk_index; |
| 124 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 125 | a = pool_elt_at_index (pm->arenas, pp->arena_index); |
| 126 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 127 | if (pp->chunks == 0) |
| 128 | { |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 129 | u32 i, start = 0, prev = ~0; |
| 130 | |
| 131 | for (i = 0; i < a->subpages_per_page; i++) |
| 132 | { |
| 133 | pool_get (pp->chunks, c); |
| 134 | c->start = start; |
| 135 | c->prev = prev; |
| 136 | c->size = pp->n_free_blocks / a->subpages_per_page; |
| 137 | start += c->size; |
| 138 | if (prev == ~0) |
| 139 | pp->first_chunk_index = c - pp->chunks; |
| 140 | else |
| 141 | pp->chunks[prev].next = c - pp->chunks; |
| 142 | prev = c - pp->chunks; |
| 143 | } |
| 144 | c->next = ~0; |
| 145 | pp->n_free_chunks = a->subpages_per_page; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Damjan Marion | 78c0ff7 | 2019-01-23 12:50:24 +0100 | [diff] [blame] | 148 | if (pp->n_free_blocks < n_blocks) |
| 149 | return 0; |
| 150 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 151 | alloc_chunk_index = pp->first_chunk_index; |
| 152 | |
| 153 | next_chunk: |
| 154 | c = pool_elt_at_index (pp->chunks, alloc_chunk_index); |
| 155 | off = (block_align - (c->start & (block_align - 1))) & (block_align - 1); |
| 156 | |
| 157 | if (c->used || n_blocks + off > c->size) |
| 158 | { |
| 159 | if (c->next == ~0) |
| 160 | return 0; |
| 161 | alloc_chunk_index = c->next; |
| 162 | goto next_chunk; |
| 163 | } |
| 164 | |
| 165 | /* if alignment is needed create new empty chunk */ |
| 166 | if (off) |
| 167 | { |
| 168 | u32 offset_chunk_index; |
| 169 | clib_pmalloc_chunk_t *co; |
| 170 | pool_get (pp->chunks, c); |
| 171 | pp->n_free_chunks++; |
| 172 | offset_chunk_index = alloc_chunk_index; |
| 173 | alloc_chunk_index = c - pp->chunks; |
| 174 | |
| 175 | co = pool_elt_at_index (pp->chunks, offset_chunk_index); |
| 176 | c->size = co->size - off; |
| 177 | c->next = co->next; |
| 178 | c->start = co->start + off; |
| 179 | c->prev = offset_chunk_index; |
| 180 | co->size = off; |
| 181 | co->next = alloc_chunk_index; |
| 182 | } |
| 183 | |
| 184 | c->used = 1; |
| 185 | if (c->size > n_blocks) |
| 186 | { |
| 187 | u32 tail_chunk_index; |
| 188 | clib_pmalloc_chunk_t *ct; |
| 189 | pool_get (pp->chunks, ct); |
| 190 | pp->n_free_chunks++; |
| 191 | tail_chunk_index = ct - pp->chunks; |
| 192 | c = pool_elt_at_index (pp->chunks, alloc_chunk_index); |
| 193 | ct->size = c->size - n_blocks; |
| 194 | ct->next = c->next; |
| 195 | ct->prev = alloc_chunk_index; |
| 196 | ct->start = c->start + n_blocks; |
| 197 | |
| 198 | c->size = n_blocks; |
| 199 | c->next = tail_chunk_index; |
| 200 | if (ct->next != ~0) |
| 201 | pool_elt_at_index (pp->chunks, ct->next)->prev = tail_chunk_index; |
| 202 | } |
| 203 | else if (c->next != ~0) |
| 204 | pool_elt_at_index (pp->chunks, c->next)->prev = alloc_chunk_index; |
| 205 | |
| 206 | c = get_chunk (pp, alloc_chunk_index); |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 207 | va = pm->base + ((pp - pm->pages) << pm->def_log2_page_sz) + |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 208 | (c->start << PMALLOC_LOG2_BLOCK_SZ); |
| 209 | hash_set (pm->chunk_index_by_va, pointer_to_uword (va), alloc_chunk_index); |
| 210 | pp->n_free_blocks -= n_blocks; |
| 211 | pp->n_free_chunks--; |
| 212 | return va; |
| 213 | } |
| 214 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 215 | static void |
| 216 | pmalloc_update_lookup_table (clib_pmalloc_main_t * pm, u32 first, u32 count) |
| 217 | { |
| 218 | uword seek, va, pa, p; |
| 219 | int fd; |
| 220 | u32 elts_per_page = 1U << (pm->def_log2_page_sz - pm->lookup_log2_page_sz); |
| 221 | |
| 222 | vec_validate_aligned (pm->lookup_table, vec_len (pm->pages) * |
| 223 | elts_per_page - 1, CLIB_CACHE_LINE_BYTES); |
| 224 | |
Dave Barach | 96e2d44 | 2018-11-14 11:42:03 -0500 | [diff] [blame] | 225 | p = (uword) first *elts_per_page; |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 226 | if (pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP) |
| 227 | { |
Damjan Marion | 878b65a | 2018-10-26 10:29:35 +0200 | [diff] [blame] | 228 | while (p < (uword) elts_per_page * count) |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 229 | { |
| 230 | pm->lookup_table[p] = pointer_to_uword (pm->base) + |
| 231 | (p << pm->lookup_log2_page_sz); |
| 232 | p++; |
| 233 | } |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | fd = open ((char *) "/proc/self/pagemap", O_RDONLY); |
Damjan Marion | 878b65a | 2018-10-26 10:29:35 +0200 | [diff] [blame] | 238 | while (p < (uword) elts_per_page * count) |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 239 | { |
| 240 | va = pointer_to_uword (pm->base) + (p << pm->lookup_log2_page_sz); |
Damjan Marion | 878b65a | 2018-10-26 10:29:35 +0200 | [diff] [blame] | 241 | pa = 0; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 242 | seek = (va >> pm->sys_log2_page_sz) * sizeof (pa); |
| 243 | if (fd != -1 && lseek (fd, seek, SEEK_SET) == seek && |
| 244 | read (fd, &pa, sizeof (pa)) == (sizeof (pa)) && |
| 245 | pa & (1ULL << 63) /* page present bit */ ) |
| 246 | { |
| 247 | pa = (pa & pow2_mask (55)) << pm->sys_log2_page_sz; |
| 248 | } |
| 249 | pm->lookup_table[p] = va - pa; |
| 250 | p++; |
| 251 | } |
| 252 | |
| 253 | if (fd != -1) |
| 254 | close (fd); |
| 255 | } |
| 256 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 257 | static inline clib_pmalloc_page_t * |
| 258 | pmalloc_map_pages (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a, |
| 259 | u32 numa_node, u32 n_pages) |
| 260 | { |
| 261 | clib_pmalloc_page_t *pp = 0; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 262 | int status, rv, i, mmap_flags; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 263 | void *va; |
| 264 | int old_mpol = -1; |
| 265 | long unsigned int mask[16] = { 0 }; |
| 266 | long unsigned int old_mask[16] = { 0 }; |
Artem Belov | f6defa1 | 2019-02-26 01:47:34 +0000 | [diff] [blame] | 267 | uword page_size = 1 << a->log2_subpage_sz; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 268 | uword size = (uword) n_pages << pm->def_log2_page_sz; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 269 | |
| 270 | clib_error_free (pm->error); |
| 271 | |
| 272 | if (pm->max_pages <= vec_len (pm->pages)) |
| 273 | { |
| 274 | pm->error = clib_error_return (0, "maximum number of pages reached"); |
| 275 | return 0; |
| 276 | } |
| 277 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 278 | if (a->log2_subpage_sz != pm->sys_log2_page_sz) |
| 279 | { |
| 280 | pm->error = clib_sysfs_prealloc_hugepages (numa_node, |
| 281 | a->log2_subpage_sz, n_pages); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 282 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 283 | if (pm->error) |
| 284 | return 0; |
| 285 | } |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 286 | |
| 287 | rv = get_mempolicy (&old_mpol, old_mask, sizeof (old_mask) * 8 + 1, 0, 0); |
| 288 | /* failure to get mempolicy means we can only proceed with numa 0 maps */ |
| 289 | if (rv == -1 && numa_node != 0) |
| 290 | { |
| 291 | pm->error = clib_error_return_unix (0, "failed to get mempolicy"); |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | mask[0] = 1 << numa_node; |
| 296 | rv = set_mempolicy (MPOL_BIND, mask, sizeof (mask) * 8 + 1); |
| 297 | if (rv == -1 && numa_node != 0) |
| 298 | { |
| 299 | pm->error = clib_error_return_unix (0, "failed to set mempolicy for " |
| 300 | "numa node %u", numa_node); |
| 301 | return 0; |
| 302 | } |
| 303 | |
Damjan Marion | 54e8e39 | 2018-11-07 17:55:26 +0100 | [diff] [blame] | 304 | mmap_flags = MAP_FIXED; |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 305 | |
| 306 | if ((pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP) == 0) |
| 307 | mmap_flags |= MAP_LOCKED; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 308 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 309 | if (a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM) |
| 310 | { |
| 311 | mmap_flags |= MAP_SHARED; |
Damjan Marion | 8ebd792 | 2018-11-28 10:46:03 +0100 | [diff] [blame] | 312 | if (a->log2_subpage_sz != pm->sys_log2_page_sz) |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 313 | pm->error = clib_mem_create_hugetlb_fd ((char *) a->name, &a->fd); |
| 314 | else |
| 315 | pm->error = clib_mem_create_fd ((char *) a->name, &a->fd); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 316 | if (a->fd == -1) |
| 317 | goto error; |
Damjan Marion | 54e8e39 | 2018-11-07 17:55:26 +0100 | [diff] [blame] | 318 | if ((ftruncate (a->fd, size)) == -1) |
| 319 | goto error; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 320 | } |
| 321 | else |
| 322 | { |
Damjan Marion | 8ebd792 | 2018-11-28 10:46:03 +0100 | [diff] [blame] | 323 | if (a->log2_subpage_sz != pm->sys_log2_page_sz) |
| 324 | mmap_flags |= MAP_HUGETLB; |
| 325 | |
Damjan Marion | 54e8e39 | 2018-11-07 17:55:26 +0100 | [diff] [blame] | 326 | mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 327 | a->fd = -1; |
| 328 | } |
| 329 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 330 | va = pm->base + (((uword) vec_len (pm->pages)) << pm->def_log2_page_sz); |
| 331 | if (mmap (va, size, PROT_READ | PROT_WRITE, mmap_flags, a->fd, 0) == |
| 332 | MAP_FAILED) |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 333 | { |
| 334 | pm->error = clib_error_return_unix (0, "failed to mmap %u pages at %p " |
| 335 | "fd %d numa %d flags 0x%x", n_pages, |
| 336 | va, a->fd, numa_node, mmap_flags); |
| 337 | goto error; |
| 338 | } |
| 339 | |
Artem Belov | f6defa1 | 2019-02-26 01:47:34 +0000 | [diff] [blame] | 340 | /* Check if huge page is not allocated, |
| 341 | wrong allocation will generate the SIGBUS */ |
| 342 | if (a->log2_subpage_sz != pm->sys_log2_page_sz) |
| 343 | { |
| 344 | for (int i = 0; i < n_pages; i++) |
| 345 | { |
| 346 | unsigned char flag; |
| 347 | mincore (va + i * page_size, 1, &flag); |
| 348 | // flag is 1 if the page was successfully allocated and in memory |
| 349 | if (!flag) |
| 350 | { |
| 351 | pm->error = |
| 352 | clib_error_return_unix (0, |
| 353 | "Unable to fulfill huge page allocation request"); |
| 354 | goto error; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 359 | clib_memset (va, 0, size); |
| 360 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 361 | rv = set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1); |
| 362 | if (rv == -1 && numa_node != 0) |
| 363 | { |
| 364 | pm->error = clib_error_return_unix (0, "failed to restore mempolicy"); |
| 365 | goto error; |
| 366 | } |
| 367 | |
| 368 | /* we tolerate move_pages failure only if request os for numa node 0 |
| 369 | to support non-numa kernels */ |
| 370 | rv = move_pages (0, 1, &va, 0, &status, 0); |
| 371 | if ((rv == 0 && status != numa_node) || (rv != 0 && numa_node != 0)) |
| 372 | { |
| 373 | pm->error = rv == -1 ? |
| 374 | clib_error_return_unix (0, "page allocated on wrong node, numa node " |
| 375 | "%u status %d", numa_node, status) : |
| 376 | clib_error_return (0, "page allocated on wrong node, numa node " |
| 377 | "%u status %d", numa_node, status); |
| 378 | |
| 379 | /* unmap & reesrve */ |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 380 | munmap (va, size); |
| 381 | mmap (va, size, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, |
| 382 | -1, 0); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 383 | goto error; |
| 384 | } |
| 385 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 386 | for (i = 0; i < n_pages; i++) |
| 387 | { |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 388 | vec_add2 (pm->pages, pp, 1); |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 389 | pp->n_free_blocks = 1 << (pm->def_log2_page_sz - PMALLOC_LOG2_BLOCK_SZ); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 390 | pp->index = pp - pm->pages; |
| 391 | pp->arena_index = a->index; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 392 | vec_add1 (a->page_indices, pp->index); |
| 393 | a->n_pages++; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 396 | |
| 397 | /* if new arena is using smaller page size, we need to rebuild whole |
| 398 | lookup table */ |
| 399 | if (a->log2_subpage_sz < pm->lookup_log2_page_sz) |
| 400 | { |
| 401 | pm->lookup_log2_page_sz = a->log2_subpage_sz; |
| 402 | pmalloc_update_lookup_table (pm, vec_len (pm->pages) - n_pages, |
| 403 | n_pages); |
| 404 | } |
| 405 | else |
| 406 | pmalloc_update_lookup_table (pm, 0, vec_len (pm->pages)); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 407 | |
| 408 | /* return pointer to 1st page */ |
| 409 | return pp - (n_pages - 1); |
| 410 | |
| 411 | error: |
| 412 | if (a->fd != -1) |
| 413 | close (a->fd); |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | void * |
| 418 | clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name, |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 419 | uword size, u32 log2_page_sz, u32 numa_node) |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 420 | { |
| 421 | clib_pmalloc_arena_t *a; |
| 422 | clib_pmalloc_page_t *pp; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 423 | u32 n_pages; |
| 424 | |
| 425 | clib_error_free (pm->error); |
| 426 | |
| 427 | if (log2_page_sz == 0) |
| 428 | log2_page_sz = pm->def_log2_page_sz; |
| 429 | else if (log2_page_sz != pm->def_log2_page_sz && |
| 430 | log2_page_sz != pm->sys_log2_page_sz) |
| 431 | { |
| 432 | pm->error = clib_error_create ("unsupported page size (%uKB)", |
| 433 | 1 << (log2_page_sz - 10)); |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | n_pages = pmalloc_size2pages (size, pm->def_log2_page_sz); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 438 | |
| 439 | if (n_pages + vec_len (pm->pages) > pm->max_pages) |
| 440 | return 0; |
| 441 | |
| 442 | if (pmalloc_validate_numa_node (&numa_node)) |
| 443 | return 0; |
| 444 | |
| 445 | pool_get (pm->arenas, a); |
| 446 | a->index = a - pm->arenas; |
| 447 | a->name = format (0, "%s%c", name, 0); |
| 448 | a->numa_node = numa_node; |
| 449 | a->flags = CLIB_PMALLOC_ARENA_F_SHARED_MEM; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 450 | a->log2_subpage_sz = log2_page_sz; |
| 451 | a->subpages_per_page = 1U << (pm->def_log2_page_sz - log2_page_sz); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 452 | |
| 453 | if ((pp = pmalloc_map_pages (pm, a, numa_node, n_pages)) == 0) |
| 454 | { |
| 455 | vec_free (a->name); |
| 456 | memset (a, 0, sizeof (*a)); |
| 457 | pool_put (pm->arenas, a); |
| 458 | return 0; |
| 459 | } |
| 460 | |
Kingwel Xie | 5efaeee | 2018-11-10 02:56:00 -0500 | [diff] [blame] | 461 | return pm->base + ((uword) pp->index << pm->def_log2_page_sz); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static inline void * |
| 465 | clib_pmalloc_alloc_inline (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a, |
| 466 | uword size, uword align, u32 numa_node) |
| 467 | { |
| 468 | clib_pmalloc_page_t *pp; |
| 469 | u32 n_blocks, block_align, *page_index; |
| 470 | |
| 471 | ASSERT (is_pow2 (align)); |
| 472 | |
| 473 | if (pmalloc_validate_numa_node (&numa_node)) |
| 474 | return 0; |
| 475 | |
| 476 | if (a == 0) |
| 477 | { |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 478 | if (size > 1ULL << pm->def_log2_page_sz) |
| 479 | return 0; |
| 480 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 481 | vec_validate_init_empty (pm->default_arena_for_numa_node, |
| 482 | numa_node, ~0); |
| 483 | if (pm->default_arena_for_numa_node[numa_node] == ~0) |
| 484 | { |
| 485 | pool_get (pm->arenas, a); |
| 486 | pm->default_arena_for_numa_node[numa_node] = a - pm->arenas; |
| 487 | a->name = format (0, "default-numa-%u%c", numa_node, 0); |
| 488 | a->numa_node = numa_node; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 489 | a->log2_subpage_sz = pm->def_log2_page_sz; |
| 490 | a->subpages_per_page = 1; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 491 | } |
| 492 | else |
| 493 | a = pool_elt_at_index (pm->arenas, |
| 494 | pm->default_arena_for_numa_node[numa_node]); |
| 495 | } |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 496 | else if (size > 1ULL << a->log2_subpage_sz) |
| 497 | return 0; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 498 | |
| 499 | n_blocks = round_pow2 (size, PMALLOC_BLOCK_SZ) / PMALLOC_BLOCK_SZ; |
| 500 | block_align = align >> PMALLOC_LOG2_BLOCK_SZ; |
| 501 | |
| 502 | vec_foreach (page_index, a->page_indices) |
| 503 | { |
| 504 | pp = vec_elt_at_index (pm->pages, *page_index); |
| 505 | void *rv = alloc_chunk_from_page (pm, pp, n_blocks, block_align, |
| 506 | numa_node); |
| 507 | |
| 508 | if (rv) |
| 509 | return rv; |
| 510 | } |
| 511 | |
| 512 | if ((a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM) == 0 && |
| 513 | (pp = pmalloc_map_pages (pm, a, numa_node, 1))) |
| 514 | return alloc_chunk_from_page (pm, pp, n_blocks, block_align, numa_node); |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | void * |
| 520 | clib_pmalloc_alloc_aligned_on_numa (clib_pmalloc_main_t * pm, uword size, |
| 521 | uword align, u32 numa_node) |
| 522 | { |
| 523 | return clib_pmalloc_alloc_inline (pm, 0, size, align, numa_node); |
| 524 | } |
| 525 | |
| 526 | void * |
| 527 | clib_pmalloc_alloc_aligned (clib_pmalloc_main_t * pm, uword size, uword align) |
| 528 | { |
| 529 | return clib_pmalloc_alloc_inline (pm, 0, size, align, |
| 530 | CLIB_PMALLOC_NUMA_LOCAL); |
| 531 | } |
| 532 | |
| 533 | void * |
| 534 | clib_pmalloc_alloc_from_arena (clib_pmalloc_main_t * pm, void *arena_va, |
| 535 | uword size, uword align) |
| 536 | { |
| 537 | clib_pmalloc_arena_t *a = clib_pmalloc_get_arena (pm, arena_va); |
| 538 | return clib_pmalloc_alloc_inline (pm, a, size, align, 0); |
| 539 | } |
| 540 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 541 | static inline int |
| 542 | pmalloc_chunks_mergeable (clib_pmalloc_arena_t * a, clib_pmalloc_page_t * pp, |
| 543 | u32 ci1, u32 ci2) |
| 544 | { |
| 545 | clib_pmalloc_chunk_t *c1, *c2; |
| 546 | |
| 547 | if (ci1 == ~0 || ci2 == ~0) |
| 548 | return 0; |
| 549 | |
| 550 | c1 = get_chunk (pp, ci1); |
| 551 | c2 = get_chunk (pp, ci2); |
| 552 | |
| 553 | if (c1->used || c2->used) |
| 554 | return 0; |
| 555 | |
| 556 | if (c1->start >> (a->log2_subpage_sz - PMALLOC_LOG2_BLOCK_SZ) != |
| 557 | c2->start >> (a->log2_subpage_sz - PMALLOC_LOG2_BLOCK_SZ)) |
| 558 | return 0; |
| 559 | |
| 560 | return 1; |
| 561 | } |
| 562 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 563 | void |
| 564 | clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va) |
| 565 | { |
| 566 | clib_pmalloc_page_t *pp; |
| 567 | clib_pmalloc_chunk_t *c; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 568 | clib_pmalloc_arena_t *a; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 569 | uword *p; |
| 570 | u32 chunk_index, page_index; |
| 571 | |
| 572 | p = hash_get (pm->chunk_index_by_va, pointer_to_uword (va)); |
| 573 | |
| 574 | if (p == 0) |
| 575 | os_panic (); |
| 576 | |
| 577 | chunk_index = p[0]; |
| 578 | page_index = clib_pmalloc_get_page_index (pm, va); |
| 579 | hash_unset (pm->chunk_index_by_va, pointer_to_uword (va)); |
| 580 | |
| 581 | pp = vec_elt_at_index (pm->pages, page_index); |
| 582 | c = pool_elt_at_index (pp->chunks, chunk_index); |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 583 | a = pool_elt_at_index (pm->arenas, pp->arena_index); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 584 | c->used = 0; |
| 585 | pp->n_free_blocks += c->size; |
| 586 | pp->n_free_chunks++; |
| 587 | |
| 588 | /* merge with next if free */ |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 589 | if (pmalloc_chunks_mergeable (a, pp, chunk_index, c->next)) |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 590 | { |
| 591 | clib_pmalloc_chunk_t *next = get_chunk (pp, c->next); |
| 592 | c->size += next->size; |
| 593 | c->next = next->next; |
| 594 | if (next->next != ~0) |
| 595 | get_chunk (pp, next->next)->prev = chunk_index; |
| 596 | memset (next, 0, sizeof (*next)); |
| 597 | pool_put (pp->chunks, next); |
| 598 | pp->n_free_chunks--; |
| 599 | } |
| 600 | |
| 601 | /* merge with prev if free */ |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 602 | if (pmalloc_chunks_mergeable (a, pp, c->prev, chunk_index)) |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 603 | { |
| 604 | clib_pmalloc_chunk_t *prev = get_chunk (pp, c->prev); |
| 605 | prev->size += c->size; |
| 606 | prev->next = c->next; |
| 607 | if (c->next != ~0) |
| 608 | get_chunk (pp, c->next)->prev = c->prev; |
| 609 | memset (c, 0, sizeof (*c)); |
| 610 | pool_put (pp->chunks, c); |
| 611 | pp->n_free_chunks--; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | static u8 * |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 616 | format_log2_page_size (u8 * s, va_list * va) |
| 617 | { |
| 618 | u32 log2_page_sz = va_arg (*va, u32); |
| 619 | |
| 620 | if (log2_page_sz >= 30) |
| 621 | return format (s, "%uGB", 1 << (log2_page_sz - 30)); |
| 622 | |
| 623 | if (log2_page_sz >= 20) |
| 624 | return format (s, "%uMB", 1 << (log2_page_sz - 20)); |
| 625 | |
| 626 | if (log2_page_sz >= 10) |
| 627 | return format (s, "%uKB", 1 << (log2_page_sz - 10)); |
| 628 | |
| 629 | return format (s, "%uB", 1 << log2_page_sz); |
| 630 | } |
| 631 | |
| 632 | |
| 633 | static u8 * |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 634 | format_pmalloc_page (u8 * s, va_list * va) |
| 635 | { |
| 636 | clib_pmalloc_page_t *pp = va_arg (*va, clib_pmalloc_page_t *); |
| 637 | int verbose = va_arg (*va, int); |
| 638 | u32 indent = format_get_indent (s); |
| 639 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 640 | if (pp->chunks == 0) |
| 641 | return s; |
| 642 | |
| 643 | s = format (s, "free %u chunks %u free-chunks %d ", |
| 644 | (pp->n_free_blocks) << PMALLOC_LOG2_BLOCK_SZ, |
| 645 | pool_elts (pp->chunks), pp->n_free_chunks); |
| 646 | |
| 647 | if (verbose >= 2) |
| 648 | { |
| 649 | clib_pmalloc_chunk_t *c; |
| 650 | c = pool_elt_at_index (pp->chunks, pp->first_chunk_index); |
| 651 | s = format (s, "\n%U%12s%12s%8s%8s%8s%8s", |
| 652 | format_white_space, indent + 2, |
| 653 | "chunk offset", "size", "used", "index", "prev", "next"); |
| 654 | while (1) |
| 655 | { |
| 656 | s = format (s, "\n%U%12u%12u%8s%8d%8d%8d", |
| 657 | format_white_space, indent + 2, |
| 658 | c->start << PMALLOC_LOG2_BLOCK_SZ, |
| 659 | c->size << PMALLOC_LOG2_BLOCK_SZ, |
| 660 | c->used ? "yes" : "no", |
| 661 | c - pp->chunks, c->prev, c->next); |
| 662 | if (c->next == ~0) |
| 663 | break; |
| 664 | c = pool_elt_at_index (pp->chunks, c->next); |
| 665 | } |
| 666 | } |
| 667 | return s; |
| 668 | } |
| 669 | |
| 670 | u8 * |
| 671 | format_pmalloc (u8 * s, va_list * va) |
| 672 | { |
| 673 | clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *); |
| 674 | int verbose = va_arg (*va, int); |
| 675 | u32 indent = format_get_indent (s); |
| 676 | |
| 677 | clib_pmalloc_page_t *pp; |
| 678 | clib_pmalloc_arena_t *a; |
| 679 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 680 | s = format (s, "used-pages %u reserved-pages %u default-page-size %U " |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 681 | "lookup-page-size %U%s", vec_len (pm->pages), pm->max_pages, |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 682 | format_log2_page_size, pm->def_log2_page_sz, |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 683 | format_log2_page_size, pm->lookup_log2_page_sz, |
| 684 | pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP ? " no-pagemap" : ""); |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 685 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 686 | |
| 687 | if (verbose >= 2) |
| 688 | s = format (s, " va-start %p", pm->base); |
| 689 | |
| 690 | if (pm->error) |
| 691 | s = format (s, "\n%Ulast-error: %U", format_white_space, indent + 2, |
| 692 | format_clib_error, pm->error); |
| 693 | |
| 694 | |
| 695 | /* *INDENT-OFF* */ |
| 696 | pool_foreach (a, pm->arenas, |
| 697 | { |
| 698 | u32 *page_index; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 699 | s = format (s, "\n%Uarena '%s' pages %u subpage-size %U numa-node %u", |
| 700 | format_white_space, indent + 2, a->name, |
| 701 | vec_len (a->page_indices), format_log2_page_size, |
| 702 | a->log2_subpage_sz, a->numa_node); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 703 | if (a->fd != -1) |
| 704 | s = format (s, " shared fd %d", a->fd); |
| 705 | if (verbose >= 1) |
| 706 | vec_foreach (page_index, a->page_indices) |
| 707 | { |
| 708 | pp = vec_elt_at_index (pm->pages, *page_index); |
| 709 | s = format (s, "\n%U%U", format_white_space, indent + 4, |
| 710 | format_pmalloc_page, pp, verbose); |
| 711 | } |
| 712 | }); |
| 713 | /* *INDENT-ON* */ |
| 714 | |
| 715 | return s; |
| 716 | } |
| 717 | |
Mohsin Kazmi | 6ec99c3 | 2018-11-07 16:55:18 +0100 | [diff] [blame] | 718 | u8 * |
| 719 | format_pmalloc_map (u8 * s, va_list * va) |
| 720 | { |
| 721 | clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *); |
| 722 | |
| 723 | u32 index; |
| 724 | s = format (s, "%16s %13s %8s", "virtual-addr", "physical-addr", "size"); |
| 725 | vec_foreach_index (index, pm->lookup_table) |
| 726 | { |
| 727 | uword *lookup_val, pa, va; |
| 728 | lookup_val = vec_elt_at_index (pm->lookup_table, index); |
Kingwel Xie | dbc34b8 | 2018-11-11 22:55:58 -0500 | [diff] [blame] | 729 | va = |
| 730 | pointer_to_uword (pm->base) + |
| 731 | ((uword) index << pm->lookup_log2_page_sz); |
Mohsin Kazmi | 6ec99c3 | 2018-11-07 16:55:18 +0100 | [diff] [blame] | 732 | pa = va - *lookup_val; |
| 733 | s = |
| 734 | format (s, "\n %16p %13p %8U", uword_to_pointer (va, u64), |
| 735 | uword_to_pointer (pa, u64), format_log2_page_size, |
| 736 | pm->lookup_log2_page_sz); |
| 737 | } |
| 738 | return s; |
| 739 | } |
| 740 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 741 | /* |
| 742 | * fd.io coding-style-patch-verification: ON |
| 743 | * |
| 744 | * Local Variables: |
| 745 | * eval: (c-set-style "gnu") |
| 746 | * End: |
| 747 | */ |