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 | { |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 66 | uword base, pagesize; |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 67 | u64 *pt = 0; |
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 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 86 | base = clib_mem_vm_reserve (base_addr, size, pm->def_log2_page_sz); |
Damjan Marion | 5a6c809 | 2019-02-21 14:44:59 +0100 | [diff] [blame] | 87 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 88 | if (base == ~0) |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 89 | { |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 90 | pm->error = clib_error_return (0, "failed to reserve %u pages", |
| 91 | pm->max_pages); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 92 | return -1; |
| 93 | } |
| 94 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 95 | pm->base = uword_to_pointer (base, void *); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static inline void * |
| 100 | alloc_chunk_from_page (clib_pmalloc_main_t * pm, clib_pmalloc_page_t * pp, |
| 101 | u32 n_blocks, u32 block_align, u32 numa_node) |
| 102 | { |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 103 | clib_pmalloc_chunk_t *c = 0; |
| 104 | clib_pmalloc_arena_t *a; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 105 | void *va; |
| 106 | u32 off; |
| 107 | u32 alloc_chunk_index; |
| 108 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 109 | a = pool_elt_at_index (pm->arenas, pp->arena_index); |
| 110 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 111 | if (pp->chunks == 0) |
| 112 | { |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 113 | u32 i, start = 0, prev = ~0; |
| 114 | |
| 115 | for (i = 0; i < a->subpages_per_page; i++) |
| 116 | { |
| 117 | pool_get (pp->chunks, c); |
| 118 | c->start = start; |
| 119 | c->prev = prev; |
| 120 | c->size = pp->n_free_blocks / a->subpages_per_page; |
| 121 | start += c->size; |
| 122 | if (prev == ~0) |
| 123 | pp->first_chunk_index = c - pp->chunks; |
| 124 | else |
| 125 | pp->chunks[prev].next = c - pp->chunks; |
| 126 | prev = c - pp->chunks; |
| 127 | } |
| 128 | c->next = ~0; |
| 129 | pp->n_free_chunks = a->subpages_per_page; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Damjan Marion | 78c0ff7 | 2019-01-23 12:50:24 +0100 | [diff] [blame] | 132 | if (pp->n_free_blocks < n_blocks) |
| 133 | return 0; |
| 134 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 135 | alloc_chunk_index = pp->first_chunk_index; |
| 136 | |
| 137 | next_chunk: |
| 138 | c = pool_elt_at_index (pp->chunks, alloc_chunk_index); |
| 139 | off = (block_align - (c->start & (block_align - 1))) & (block_align - 1); |
| 140 | |
| 141 | if (c->used || n_blocks + off > c->size) |
| 142 | { |
| 143 | if (c->next == ~0) |
| 144 | return 0; |
| 145 | alloc_chunk_index = c->next; |
| 146 | goto next_chunk; |
| 147 | } |
| 148 | |
| 149 | /* if alignment is needed create new empty chunk */ |
| 150 | if (off) |
| 151 | { |
| 152 | u32 offset_chunk_index; |
| 153 | clib_pmalloc_chunk_t *co; |
| 154 | pool_get (pp->chunks, c); |
| 155 | pp->n_free_chunks++; |
| 156 | offset_chunk_index = alloc_chunk_index; |
| 157 | alloc_chunk_index = c - pp->chunks; |
| 158 | |
| 159 | co = pool_elt_at_index (pp->chunks, offset_chunk_index); |
| 160 | c->size = co->size - off; |
| 161 | c->next = co->next; |
| 162 | c->start = co->start + off; |
| 163 | c->prev = offset_chunk_index; |
| 164 | co->size = off; |
| 165 | co->next = alloc_chunk_index; |
| 166 | } |
| 167 | |
| 168 | c->used = 1; |
| 169 | if (c->size > n_blocks) |
| 170 | { |
| 171 | u32 tail_chunk_index; |
| 172 | clib_pmalloc_chunk_t *ct; |
| 173 | pool_get (pp->chunks, ct); |
| 174 | pp->n_free_chunks++; |
| 175 | tail_chunk_index = ct - pp->chunks; |
| 176 | c = pool_elt_at_index (pp->chunks, alloc_chunk_index); |
| 177 | ct->size = c->size - n_blocks; |
| 178 | ct->next = c->next; |
| 179 | ct->prev = alloc_chunk_index; |
| 180 | ct->start = c->start + n_blocks; |
| 181 | |
| 182 | c->size = n_blocks; |
| 183 | c->next = tail_chunk_index; |
| 184 | if (ct->next != ~0) |
| 185 | pool_elt_at_index (pp->chunks, ct->next)->prev = tail_chunk_index; |
| 186 | } |
| 187 | else if (c->next != ~0) |
| 188 | pool_elt_at_index (pp->chunks, c->next)->prev = alloc_chunk_index; |
| 189 | |
| 190 | c = get_chunk (pp, alloc_chunk_index); |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 191 | va = pm->base + ((pp - pm->pages) << pm->def_log2_page_sz) + |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 192 | (c->start << PMALLOC_LOG2_BLOCK_SZ); |
| 193 | hash_set (pm->chunk_index_by_va, pointer_to_uword (va), alloc_chunk_index); |
| 194 | pp->n_free_blocks -= n_blocks; |
| 195 | pp->n_free_chunks--; |
| 196 | return va; |
| 197 | } |
| 198 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 199 | static void |
| 200 | pmalloc_update_lookup_table (clib_pmalloc_main_t * pm, u32 first, u32 count) |
| 201 | { |
| 202 | uword seek, va, pa, p; |
| 203 | int fd; |
| 204 | u32 elts_per_page = 1U << (pm->def_log2_page_sz - pm->lookup_log2_page_sz); |
| 205 | |
| 206 | vec_validate_aligned (pm->lookup_table, vec_len (pm->pages) * |
| 207 | elts_per_page - 1, CLIB_CACHE_LINE_BYTES); |
| 208 | |
Dave Barach | 96e2d44 | 2018-11-14 11:42:03 -0500 | [diff] [blame] | 209 | p = (uword) first *elts_per_page; |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 210 | if (pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP) |
| 211 | { |
Damjan Marion | 878b65a | 2018-10-26 10:29:35 +0200 | [diff] [blame] | 212 | while (p < (uword) elts_per_page * count) |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 213 | { |
| 214 | pm->lookup_table[p] = pointer_to_uword (pm->base) + |
| 215 | (p << pm->lookup_log2_page_sz); |
| 216 | p++; |
| 217 | } |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | fd = open ((char *) "/proc/self/pagemap", O_RDONLY); |
Damjan Marion | 878b65a | 2018-10-26 10:29:35 +0200 | [diff] [blame] | 222 | while (p < (uword) elts_per_page * count) |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 223 | { |
| 224 | va = pointer_to_uword (pm->base) + (p << pm->lookup_log2_page_sz); |
Damjan Marion | 878b65a | 2018-10-26 10:29:35 +0200 | [diff] [blame] | 225 | pa = 0; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 226 | seek = (va >> pm->sys_log2_page_sz) * sizeof (pa); |
| 227 | if (fd != -1 && lseek (fd, seek, SEEK_SET) == seek && |
| 228 | read (fd, &pa, sizeof (pa)) == (sizeof (pa)) && |
| 229 | pa & (1ULL << 63) /* page present bit */ ) |
| 230 | { |
| 231 | pa = (pa & pow2_mask (55)) << pm->sys_log2_page_sz; |
| 232 | } |
| 233 | pm->lookup_table[p] = va - pa; |
| 234 | p++; |
| 235 | } |
| 236 | |
| 237 | if (fd != -1) |
| 238 | close (fd); |
| 239 | } |
| 240 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 241 | static inline clib_pmalloc_page_t * |
| 242 | pmalloc_map_pages (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a, |
| 243 | u32 numa_node, u32 n_pages) |
| 244 | { |
| 245 | clib_pmalloc_page_t *pp = 0; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 246 | int status, rv, i, mmap_flags; |
Damjan Marion | 801c701 | 2019-10-30 18:07:35 +0100 | [diff] [blame] | 247 | void *va = MAP_FAILED; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 248 | int old_mpol = -1; |
| 249 | long unsigned int mask[16] = { 0 }; |
| 250 | long unsigned int old_mask[16] = { 0 }; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 251 | uword size = (uword) n_pages << pm->def_log2_page_sz; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 252 | |
| 253 | clib_error_free (pm->error); |
| 254 | |
| 255 | if (pm->max_pages <= vec_len (pm->pages)) |
| 256 | { |
| 257 | pm->error = clib_error_return (0, "maximum number of pages reached"); |
| 258 | return 0; |
| 259 | } |
| 260 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 261 | if (a->log2_subpage_sz != pm->sys_log2_page_sz) |
| 262 | { |
| 263 | pm->error = clib_sysfs_prealloc_hugepages (numa_node, |
| 264 | a->log2_subpage_sz, n_pages); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 265 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 266 | if (pm->error) |
| 267 | return 0; |
| 268 | } |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 269 | |
| 270 | rv = get_mempolicy (&old_mpol, old_mask, sizeof (old_mask) * 8 + 1, 0, 0); |
| 271 | /* failure to get mempolicy means we can only proceed with numa 0 maps */ |
| 272 | if (rv == -1 && numa_node != 0) |
| 273 | { |
| 274 | pm->error = clib_error_return_unix (0, "failed to get mempolicy"); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | mask[0] = 1 << numa_node; |
| 279 | rv = set_mempolicy (MPOL_BIND, mask, sizeof (mask) * 8 + 1); |
| 280 | if (rv == -1 && numa_node != 0) |
| 281 | { |
| 282 | pm->error = clib_error_return_unix (0, "failed to set mempolicy for " |
| 283 | "numa node %u", numa_node); |
| 284 | return 0; |
| 285 | } |
| 286 | |
Damjan Marion | 54e8e39 | 2018-11-07 17:55:26 +0100 | [diff] [blame] | 287 | mmap_flags = MAP_FIXED; |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 288 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 289 | if (a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM) |
| 290 | { |
| 291 | mmap_flags |= MAP_SHARED; |
Damjan Marion | 8ebd792 | 2018-11-28 10:46:03 +0100 | [diff] [blame] | 292 | if (a->log2_subpage_sz != pm->sys_log2_page_sz) |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 293 | pm->error = clib_mem_create_hugetlb_fd ((char *) a->name, &a->fd); |
| 294 | else |
| 295 | pm->error = clib_mem_create_fd ((char *) a->name, &a->fd); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 296 | if (a->fd == -1) |
| 297 | goto error; |
Damjan Marion | 54e8e39 | 2018-11-07 17:55:26 +0100 | [diff] [blame] | 298 | if ((ftruncate (a->fd, size)) == -1) |
| 299 | goto error; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 300 | } |
| 301 | else |
| 302 | { |
Damjan Marion | 8ebd792 | 2018-11-28 10:46:03 +0100 | [diff] [blame] | 303 | if (a->log2_subpage_sz != pm->sys_log2_page_sz) |
| 304 | mmap_flags |= MAP_HUGETLB; |
| 305 | |
Damjan Marion | 54e8e39 | 2018-11-07 17:55:26 +0100 | [diff] [blame] | 306 | mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 307 | a->fd = -1; |
| 308 | } |
| 309 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 310 | va = pm->base + (((uword) vec_len (pm->pages)) << pm->def_log2_page_sz); |
| 311 | if (mmap (va, size, PROT_READ | PROT_WRITE, mmap_flags, a->fd, 0) == |
| 312 | MAP_FAILED) |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 313 | { |
| 314 | pm->error = clib_error_return_unix (0, "failed to mmap %u pages at %p " |
| 315 | "fd %d numa %d flags 0x%x", n_pages, |
| 316 | va, a->fd, numa_node, mmap_flags); |
Andrew Yourtchenko | 9ce3523 | 2019-11-18 10:23:54 +0000 | [diff] [blame] | 317 | va = MAP_FAILED; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 318 | goto error; |
| 319 | } |
| 320 | |
Damjan Marion | 801c701 | 2019-10-30 18:07:35 +0100 | [diff] [blame] | 321 | 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] | 322 | { |
Damjan Marion | 801c701 | 2019-10-30 18:07:35 +0100 | [diff] [blame] | 323 | pm->error = clib_error_return_unix (0, "Unable to lock pages"); |
| 324 | goto error; |
Artem Belov | f6defa1 | 2019-02-26 01:47:34 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 327 | clib_memset (va, 0, size); |
| 328 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 329 | rv = set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1); |
| 330 | if (rv == -1 && numa_node != 0) |
| 331 | { |
| 332 | pm->error = clib_error_return_unix (0, "failed to restore mempolicy"); |
| 333 | goto error; |
| 334 | } |
| 335 | |
| 336 | /* we tolerate move_pages failure only if request os for numa node 0 |
| 337 | to support non-numa kernels */ |
| 338 | rv = move_pages (0, 1, &va, 0, &status, 0); |
| 339 | if ((rv == 0 && status != numa_node) || (rv != 0 && numa_node != 0)) |
| 340 | { |
| 341 | pm->error = rv == -1 ? |
| 342 | clib_error_return_unix (0, "page allocated on wrong node, numa node " |
| 343 | "%u status %d", numa_node, status) : |
| 344 | clib_error_return (0, "page allocated on wrong node, numa node " |
| 345 | "%u status %d", numa_node, status); |
| 346 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 347 | goto error; |
| 348 | } |
| 349 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 350 | for (i = 0; i < n_pages; i++) |
| 351 | { |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 352 | vec_add2 (pm->pages, pp, 1); |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 353 | 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] | 354 | pp->index = pp - pm->pages; |
| 355 | pp->arena_index = a->index; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 356 | vec_add1 (a->page_indices, pp->index); |
| 357 | a->n_pages++; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 360 | |
| 361 | /* if new arena is using smaller page size, we need to rebuild whole |
| 362 | lookup table */ |
| 363 | if (a->log2_subpage_sz < pm->lookup_log2_page_sz) |
| 364 | { |
| 365 | pm->lookup_log2_page_sz = a->log2_subpage_sz; |
| 366 | pmalloc_update_lookup_table (pm, vec_len (pm->pages) - n_pages, |
| 367 | n_pages); |
| 368 | } |
| 369 | else |
| 370 | pmalloc_update_lookup_table (pm, 0, vec_len (pm->pages)); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 371 | |
| 372 | /* return pointer to 1st page */ |
| 373 | return pp - (n_pages - 1); |
| 374 | |
| 375 | error: |
Damjan Marion | 801c701 | 2019-10-30 18:07:35 +0100 | [diff] [blame] | 376 | if (va != MAP_FAILED) |
| 377 | { |
| 378 | /* unmap & reserve */ |
| 379 | munmap (va, size); |
| 380 | mmap (va, size, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, |
| 381 | -1, 0); |
| 382 | } |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 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 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 611 | if (pp->chunks == 0) |
| 612 | return s; |
| 613 | |
| 614 | s = format (s, "free %u chunks %u free-chunks %d ", |
| 615 | (pp->n_free_blocks) << PMALLOC_LOG2_BLOCK_SZ, |
| 616 | pool_elts (pp->chunks), pp->n_free_chunks); |
| 617 | |
| 618 | if (verbose >= 2) |
| 619 | { |
| 620 | clib_pmalloc_chunk_t *c; |
| 621 | c = pool_elt_at_index (pp->chunks, pp->first_chunk_index); |
| 622 | s = format (s, "\n%U%12s%12s%8s%8s%8s%8s", |
| 623 | format_white_space, indent + 2, |
| 624 | "chunk offset", "size", "used", "index", "prev", "next"); |
| 625 | while (1) |
| 626 | { |
| 627 | s = format (s, "\n%U%12u%12u%8s%8d%8d%8d", |
| 628 | format_white_space, indent + 2, |
| 629 | c->start << PMALLOC_LOG2_BLOCK_SZ, |
| 630 | c->size << PMALLOC_LOG2_BLOCK_SZ, |
| 631 | c->used ? "yes" : "no", |
| 632 | c - pp->chunks, c->prev, c->next); |
| 633 | if (c->next == ~0) |
| 634 | break; |
| 635 | c = pool_elt_at_index (pp->chunks, c->next); |
| 636 | } |
| 637 | } |
| 638 | return s; |
| 639 | } |
| 640 | |
| 641 | u8 * |
| 642 | format_pmalloc (u8 * s, va_list * va) |
| 643 | { |
| 644 | clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *); |
| 645 | int verbose = va_arg (*va, int); |
| 646 | u32 indent = format_get_indent (s); |
| 647 | |
| 648 | clib_pmalloc_page_t *pp; |
| 649 | clib_pmalloc_arena_t *a; |
| 650 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 651 | 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] | 652 | "lookup-page-size %U%s", vec_len (pm->pages), pm->max_pages, |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 653 | format_log2_page_size, pm->def_log2_page_sz, |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 654 | format_log2_page_size, pm->lookup_log2_page_sz, |
| 655 | pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP ? " no-pagemap" : ""); |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 656 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 657 | |
| 658 | if (verbose >= 2) |
| 659 | s = format (s, " va-start %p", pm->base); |
| 660 | |
| 661 | if (pm->error) |
| 662 | s = format (s, "\n%Ulast-error: %U", format_white_space, indent + 2, |
| 663 | format_clib_error, pm->error); |
| 664 | |
| 665 | |
| 666 | /* *INDENT-OFF* */ |
| 667 | pool_foreach (a, pm->arenas, |
| 668 | { |
| 669 | u32 *page_index; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 670 | s = format (s, "\n%Uarena '%s' pages %u subpage-size %U numa-node %u", |
| 671 | format_white_space, indent + 2, a->name, |
| 672 | vec_len (a->page_indices), format_log2_page_size, |
| 673 | a->log2_subpage_sz, a->numa_node); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 674 | if (a->fd != -1) |
| 675 | s = format (s, " shared fd %d", a->fd); |
| 676 | if (verbose >= 1) |
| 677 | vec_foreach (page_index, a->page_indices) |
| 678 | { |
| 679 | pp = vec_elt_at_index (pm->pages, *page_index); |
| 680 | s = format (s, "\n%U%U", format_white_space, indent + 4, |
| 681 | format_pmalloc_page, pp, verbose); |
| 682 | } |
| 683 | }); |
| 684 | /* *INDENT-ON* */ |
| 685 | |
| 686 | return s; |
| 687 | } |
| 688 | |
Mohsin Kazmi | 6ec99c3 | 2018-11-07 16:55:18 +0100 | [diff] [blame] | 689 | u8 * |
| 690 | format_pmalloc_map (u8 * s, va_list * va) |
| 691 | { |
| 692 | clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *); |
| 693 | |
| 694 | u32 index; |
| 695 | s = format (s, "%16s %13s %8s", "virtual-addr", "physical-addr", "size"); |
| 696 | vec_foreach_index (index, pm->lookup_table) |
| 697 | { |
| 698 | uword *lookup_val, pa, va; |
| 699 | lookup_val = vec_elt_at_index (pm->lookup_table, index); |
Kingwel Xie | dbc34b8 | 2018-11-11 22:55:58 -0500 | [diff] [blame] | 700 | va = |
| 701 | pointer_to_uword (pm->base) + |
| 702 | ((uword) index << pm->lookup_log2_page_sz); |
Mohsin Kazmi | 6ec99c3 | 2018-11-07 16:55:18 +0100 | [diff] [blame] | 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 | */ |