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