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