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