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