blob: 9fb8ac4dcfef48d6caa3b66c1bdc1f2e6521d783 [file] [log] [blame]
Damjan Marion68b4da62018-09-30 18:26:20 +02001/*
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 Belovf6defa12019-02-26 01:47:34 +000021#include <unistd.h>
Damjan Marion1ee346a2019-03-18 17:06:51 +010022#include <sched.h>
Damjan Marion68b4da62018-09-30 18:26:20 +020023
24#include <vppinfra/format.h>
Damjan Marion68b4da62018-09-30 18:26:20 +020025#include <vppinfra/linux/sysfs.h>
26#include <vppinfra/mem.h>
27#include <vppinfra/hash.h>
28#include <vppinfra/pmalloc.h>
Damjan Marionf8cb7012020-10-09 17:16:55 +020029#include <vppinfra/cpu.h>
Damjan Marion68b4da62018-09-30 18:26:20 +020030
31#if __SIZEOF_POINTER__ >= 8
32#define DEFAULT_RESERVED_MB 16384
33#else
34#define DEFAULT_RESERVED_MB 256
35#endif
36
37static inline clib_pmalloc_chunk_t *
38get_chunk (clib_pmalloc_page_t * pp, u32 index)
39{
40 return pool_elt_at_index (pp->chunks, index);
41}
42
Damjan Marion567e61d2018-10-24 17:08:26 +020043static inline uword
44pmalloc_size2pages (uword size, u32 log2_page_sz)
45{
46 return round_pow2 (size, 1ULL << log2_page_sz) >> log2_page_sz;
47}
48
Damjan Mariondae1c7e2020-10-17 13:32:25 +020049__clib_export int
Damjan Marion5a6c8092019-02-21 14:44:59 +010050clib_pmalloc_init (clib_pmalloc_main_t * pm, uword base_addr, uword size)
Damjan Marion68b4da62018-09-30 18:26:20 +020051{
Dave Barach16e4a4a2020-04-16 12:00:14 -040052 uword base, pagesize;
Damjan Marionc04e2b02018-10-25 15:56:04 +020053 u64 *pt = 0;
Damjan Marion68b4da62018-09-30 18:26:20 +020054
55 ASSERT (pm->error == 0);
56
Damjan Marion9787f5f2018-10-24 12:56:32 +020057 pagesize = clib_mem_get_default_hugepage_size ();
Damjan Marion567e61d2018-10-24 17:08:26 +020058 pm->def_log2_page_sz = min_log2 (pagesize);
Damjan Marion567e61d2018-10-24 17:08:26 +020059 pm->lookup_log2_page_sz = pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +020060
Damjan Marionc04e2b02018-10-25 15:56:04 +020061 /* check if pagemap is accessible */
Damjan Marion6bfd0762020-09-11 22:16:53 +020062 pt = clib_mem_vm_get_paddr (&pt, CLIB_MEM_PAGE_SZ_DEFAULT, 1);
Damjan Marionc04e2b02018-10-25 15:56:04 +020063 if (pt == 0 || pt[0] == 0)
64 pm->flags |= CLIB_PMALLOC_F_NO_PAGEMAP;
65
Damjan Marion68b4da62018-09-30 18:26:20 +020066 size = size ? size : ((u64) DEFAULT_RESERVED_MB) << 20;
67 size = round_pow2 (size, pagesize);
68
Damjan Marion567e61d2018-10-24 17:08:26 +020069 pm->max_pages = size >> pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +020070
Dave Barach16e4a4a2020-04-16 12:00:14 -040071 base = clib_mem_vm_reserve (base_addr, size, pm->def_log2_page_sz);
Damjan Marion5a6c8092019-02-21 14:44:59 +010072
Dave Barach16e4a4a2020-04-16 12:00:14 -040073 if (base == ~0)
Damjan Marion68b4da62018-09-30 18:26:20 +020074 {
Dave Barach16e4a4a2020-04-16 12:00:14 -040075 pm->error = clib_error_return (0, "failed to reserve %u pages",
76 pm->max_pages);
Damjan Marion68b4da62018-09-30 18:26:20 +020077 return -1;
78 }
79
Dave Barach16e4a4a2020-04-16 12:00:14 -040080 pm->base = uword_to_pointer (base, void *);
Damjan Marion68b4da62018-09-30 18:26:20 +020081 return 0;
82}
83
84static inline void *
85alloc_chunk_from_page (clib_pmalloc_main_t * pm, clib_pmalloc_page_t * pp,
86 u32 n_blocks, u32 block_align, u32 numa_node)
87{
Damjan Marion567e61d2018-10-24 17:08:26 +020088 clib_pmalloc_chunk_t *c = 0;
89 clib_pmalloc_arena_t *a;
Damjan Marion68b4da62018-09-30 18:26:20 +020090 void *va;
91 u32 off;
92 u32 alloc_chunk_index;
93
Damjan Marion567e61d2018-10-24 17:08:26 +020094 a = pool_elt_at_index (pm->arenas, pp->arena_index);
95
Damjan Marion68b4da62018-09-30 18:26:20 +020096 if (pp->chunks == 0)
97 {
Damjan Marion567e61d2018-10-24 17:08:26 +020098 u32 i, start = 0, prev = ~0;
99
100 for (i = 0; i < a->subpages_per_page; i++)
101 {
102 pool_get (pp->chunks, c);
103 c->start = start;
104 c->prev = prev;
105 c->size = pp->n_free_blocks / a->subpages_per_page;
106 start += c->size;
107 if (prev == ~0)
108 pp->first_chunk_index = c - pp->chunks;
109 else
110 pp->chunks[prev].next = c - pp->chunks;
111 prev = c - pp->chunks;
112 }
113 c->next = ~0;
114 pp->n_free_chunks = a->subpages_per_page;
Damjan Marion68b4da62018-09-30 18:26:20 +0200115 }
116
Damjan Marion78c0ff72019-01-23 12:50:24 +0100117 if (pp->n_free_blocks < n_blocks)
118 return 0;
119
Damjan Marion68b4da62018-09-30 18:26:20 +0200120 alloc_chunk_index = pp->first_chunk_index;
121
122next_chunk:
123 c = pool_elt_at_index (pp->chunks, alloc_chunk_index);
124 off = (block_align - (c->start & (block_align - 1))) & (block_align - 1);
125
126 if (c->used || n_blocks + off > c->size)
127 {
128 if (c->next == ~0)
129 return 0;
130 alloc_chunk_index = c->next;
131 goto next_chunk;
132 }
133
134 /* if alignment is needed create new empty chunk */
135 if (off)
136 {
137 u32 offset_chunk_index;
138 clib_pmalloc_chunk_t *co;
139 pool_get (pp->chunks, c);
140 pp->n_free_chunks++;
141 offset_chunk_index = alloc_chunk_index;
142 alloc_chunk_index = c - pp->chunks;
143
144 co = pool_elt_at_index (pp->chunks, offset_chunk_index);
145 c->size = co->size - off;
146 c->next = co->next;
147 c->start = co->start + off;
148 c->prev = offset_chunk_index;
149 co->size = off;
150 co->next = alloc_chunk_index;
151 }
152
153 c->used = 1;
154 if (c->size > n_blocks)
155 {
156 u32 tail_chunk_index;
157 clib_pmalloc_chunk_t *ct;
158 pool_get (pp->chunks, ct);
159 pp->n_free_chunks++;
160 tail_chunk_index = ct - pp->chunks;
161 c = pool_elt_at_index (pp->chunks, alloc_chunk_index);
162 ct->size = c->size - n_blocks;
163 ct->next = c->next;
164 ct->prev = alloc_chunk_index;
165 ct->start = c->start + n_blocks;
166
167 c->size = n_blocks;
168 c->next = tail_chunk_index;
169 if (ct->next != ~0)
170 pool_elt_at_index (pp->chunks, ct->next)->prev = tail_chunk_index;
171 }
172 else if (c->next != ~0)
173 pool_elt_at_index (pp->chunks, c->next)->prev = alloc_chunk_index;
174
175 c = get_chunk (pp, alloc_chunk_index);
Damjan Marion567e61d2018-10-24 17:08:26 +0200176 va = pm->base + ((pp - pm->pages) << pm->def_log2_page_sz) +
Damjan Marion68b4da62018-09-30 18:26:20 +0200177 (c->start << PMALLOC_LOG2_BLOCK_SZ);
178 hash_set (pm->chunk_index_by_va, pointer_to_uword (va), alloc_chunk_index);
179 pp->n_free_blocks -= n_blocks;
180 pp->n_free_chunks--;
181 return va;
182}
183
Damjan Marion567e61d2018-10-24 17:08:26 +0200184static void
185pmalloc_update_lookup_table (clib_pmalloc_main_t * pm, u32 first, u32 count)
186{
187 uword seek, va, pa, p;
188 int fd;
189 u32 elts_per_page = 1U << (pm->def_log2_page_sz - pm->lookup_log2_page_sz);
190
191 vec_validate_aligned (pm->lookup_table, vec_len (pm->pages) *
192 elts_per_page - 1, CLIB_CACHE_LINE_BYTES);
193
Dave Barach96e2d442018-11-14 11:42:03 -0500194 p = (uword) first *elts_per_page;
Damjan Marionc04e2b02018-10-25 15:56:04 +0200195 if (pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP)
196 {
Damjan Marion878b65a2018-10-26 10:29:35 +0200197 while (p < (uword) elts_per_page * count)
Damjan Marionc04e2b02018-10-25 15:56:04 +0200198 {
199 pm->lookup_table[p] = pointer_to_uword (pm->base) +
200 (p << pm->lookup_log2_page_sz);
201 p++;
202 }
203 return;
204 }
205
206 fd = open ((char *) "/proc/self/pagemap", O_RDONLY);
Damjan Marion878b65a2018-10-26 10:29:35 +0200207 while (p < (uword) elts_per_page * count)
Damjan Marion567e61d2018-10-24 17:08:26 +0200208 {
209 va = pointer_to_uword (pm->base) + (p << pm->lookup_log2_page_sz);
Damjan Marion878b65a2018-10-26 10:29:35 +0200210 pa = 0;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200211 seek = (va >> clib_mem_get_log2_page_size ()) * sizeof (pa);
Damjan Marion567e61d2018-10-24 17:08:26 +0200212 if (fd != -1 && lseek (fd, seek, SEEK_SET) == seek &&
213 read (fd, &pa, sizeof (pa)) == (sizeof (pa)) &&
214 pa & (1ULL << 63) /* page present bit */ )
215 {
Damjan Marion6bfd0762020-09-11 22:16:53 +0200216 pa = (pa & pow2_mask (55)) << clib_mem_get_log2_page_size ();
Damjan Marion567e61d2018-10-24 17:08:26 +0200217 }
218 pm->lookup_table[p] = va - pa;
219 p++;
220 }
221
222 if (fd != -1)
223 close (fd);
224}
225
Damjan Marion68b4da62018-09-30 18:26:20 +0200226static inline clib_pmalloc_page_t *
227pmalloc_map_pages (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
228 u32 numa_node, u32 n_pages)
229{
Damjan Marionf8cb7012020-10-09 17:16:55 +0200230 clib_mem_page_stats_t stats = {};
Damjan Marion68b4da62018-09-30 18:26:20 +0200231 clib_pmalloc_page_t *pp = 0;
Damjan Marionf8cb7012020-10-09 17:16:55 +0200232 int rv, i, mmap_flags;
Damjan Marion801c7012019-10-30 18:07:35 +0100233 void *va = MAP_FAILED;
Damjan Marion567e61d2018-10-24 17:08:26 +0200234 uword size = (uword) n_pages << pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +0200235
236 clib_error_free (pm->error);
237
238 if (pm->max_pages <= vec_len (pm->pages))
239 {
240 pm->error = clib_error_return (0, "maximum number of pages reached");
241 return 0;
242 }
243
Tom Jonescb3372d2024-01-26 17:34:51 +0000244#ifdef __linux__
Damjan Marion6bfd0762020-09-11 22:16:53 +0200245 if (a->log2_subpage_sz != clib_mem_get_log2_page_size ())
Damjan Marion567e61d2018-10-24 17:08:26 +0200246 {
247 pm->error = clib_sysfs_prealloc_hugepages (numa_node,
248 a->log2_subpage_sz, n_pages);
Damjan Marion68b4da62018-09-30 18:26:20 +0200249
Damjan Marion567e61d2018-10-24 17:08:26 +0200250 if (pm->error)
251 return 0;
252 }
Tom Jonescb3372d2024-01-26 17:34:51 +0000253#endif /* __linux__ */
Damjan Marion68b4da62018-09-30 18:26:20 +0200254
Damjan Marionf8cb7012020-10-09 17:16:55 +0200255 rv = clib_mem_set_numa_affinity (numa_node, /* force */ 1);
256 if (rv == CLIB_MEM_ERROR && numa_node != 0)
Damjan Marion68b4da62018-09-30 18:26:20 +0200257 {
258 pm->error = clib_error_return_unix (0, "failed to set mempolicy for "
259 "numa node %u", numa_node);
260 return 0;
261 }
262
Damjan Marion54e8e392018-11-07 17:55:26 +0100263 mmap_flags = MAP_FIXED;
Damjan Marionc04e2b02018-10-25 15:56:04 +0200264
Damjan Marion68b4da62018-09-30 18:26:20 +0200265 if (a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM)
266 {
267 mmap_flags |= MAP_SHARED;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200268 a->fd = clib_mem_vm_create_fd (a->log2_subpage_sz, "%s", a->name);
Damjan Marion68b4da62018-09-30 18:26:20 +0200269 if (a->fd == -1)
270 goto error;
Damjan Marion54e8e392018-11-07 17:55:26 +0100271 if ((ftruncate (a->fd, size)) == -1)
272 goto error;
Damjan Marion68b4da62018-09-30 18:26:20 +0200273 }
274 else
275 {
Tom Jones77ce67f2024-01-26 14:15:54 +0000276#ifdef __linux__
Damjan Marion6bfd0762020-09-11 22:16:53 +0200277 if (a->log2_subpage_sz != clib_mem_get_log2_page_size ())
Damjan Marion8ebd7922018-11-28 10:46:03 +0100278 mmap_flags |= MAP_HUGETLB;
Tom Jones77ce67f2024-01-26 14:15:54 +0000279#endif /* __linux__ */
Damjan Marion8ebd7922018-11-28 10:46:03 +0100280
Damjan Marion54e8e392018-11-07 17:55:26 +0100281 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
Damjan Marion68b4da62018-09-30 18:26:20 +0200282 a->fd = -1;
283 }
284
Damjan Marion567e61d2018-10-24 17:08:26 +0200285 va = pm->base + (((uword) vec_len (pm->pages)) << pm->def_log2_page_sz);
286 if (mmap (va, size, PROT_READ | PROT_WRITE, mmap_flags, a->fd, 0) ==
287 MAP_FAILED)
Damjan Marion68b4da62018-09-30 18:26:20 +0200288 {
289 pm->error = clib_error_return_unix (0, "failed to mmap %u pages at %p "
290 "fd %d numa %d flags 0x%x", n_pages,
291 va, a->fd, numa_node, mmap_flags);
Andrew Yourtchenko9ce35232019-11-18 10:23:54 +0000292 va = MAP_FAILED;
Damjan Marion68b4da62018-09-30 18:26:20 +0200293 goto error;
294 }
295
Damjan Marion6bfd0762020-09-11 22:16:53 +0200296 if (a->log2_subpage_sz != clib_mem_get_log2_page_size () &&
297 mlock (va, size) != 0)
Artem Belovf6defa12019-02-26 01:47:34 +0000298 {
Damjan Marion801c7012019-10-30 18:07:35 +0100299 pm->error = clib_error_return_unix (0, "Unable to lock pages");
300 goto error;
Artem Belovf6defa12019-02-26 01:47:34 +0000301 }
302
Damjan Marion567e61d2018-10-24 17:08:26 +0200303 clib_memset (va, 0, size);
304
Damjan Marionf8cb7012020-10-09 17:16:55 +0200305 rv = clib_mem_set_default_numa_affinity ();
306 if (rv == CLIB_MEM_ERROR && numa_node != 0)
Damjan Marion68b4da62018-09-30 18:26:20 +0200307 {
308 pm->error = clib_error_return_unix (0, "failed to restore mempolicy");
309 goto error;
310 }
311
312 /* we tolerate move_pages failure only if request os for numa node 0
313 to support non-numa kernels */
Damjan Marionf8cb7012020-10-09 17:16:55 +0200314 clib_mem_get_page_stats (va, CLIB_MEM_PAGE_SZ_DEFAULT, 1, &stats);
315
Klement Sekeraec62af52021-04-20 18:08:45 +0200316 if (stats.per_numa[numa_node] != 1 &&
317 !(numa_node == 0 && stats.unknown == 1))
Damjan Marion68b4da62018-09-30 18:26:20 +0200318 {
Damjan Marionf8cb7012020-10-09 17:16:55 +0200319 u16 allocated_at = ~0;
320 if (stats.unknown)
321 clib_error_return (0,
322 "unable to get information about numa allocation");
323
324 for (u16 i = 0; i < CLIB_MAX_NUMAS; i++)
325 if (stats.per_numa[i] == 1)
326 allocated_at = i;
327
328 clib_error_return (0,
329 "page allocated on the wrong numa node (%u), "
330 "expected %u",
331 allocated_at, numa_node);
Damjan Marion68b4da62018-09-30 18:26:20 +0200332
Damjan Marion68b4da62018-09-30 18:26:20 +0200333 goto error;
334 }
335
Damjan Marion68b4da62018-09-30 18:26:20 +0200336 for (i = 0; i < n_pages; i++)
337 {
Damjan Marion68b4da62018-09-30 18:26:20 +0200338 vec_add2 (pm->pages, pp, 1);
Damjan Marion567e61d2018-10-24 17:08:26 +0200339 pp->n_free_blocks = 1 << (pm->def_log2_page_sz - PMALLOC_LOG2_BLOCK_SZ);
Damjan Marion68b4da62018-09-30 18:26:20 +0200340 pp->index = pp - pm->pages;
341 pp->arena_index = a->index;
Damjan Marion68b4da62018-09-30 18:26:20 +0200342 vec_add1 (a->page_indices, pp->index);
343 a->n_pages++;
Damjan Marion68b4da62018-09-30 18:26:20 +0200344 }
345
Damjan Marion567e61d2018-10-24 17:08:26 +0200346
347 /* if new arena is using smaller page size, we need to rebuild whole
348 lookup table */
349 if (a->log2_subpage_sz < pm->lookup_log2_page_sz)
350 {
351 pm->lookup_log2_page_sz = a->log2_subpage_sz;
352 pmalloc_update_lookup_table (pm, vec_len (pm->pages) - n_pages,
353 n_pages);
354 }
355 else
356 pmalloc_update_lookup_table (pm, 0, vec_len (pm->pages));
Damjan Marion68b4da62018-09-30 18:26:20 +0200357
358 /* return pointer to 1st page */
359 return pp - (n_pages - 1);
360
361error:
Damjan Marion801c7012019-10-30 18:07:35 +0100362 if (va != MAP_FAILED)
363 {
364 /* unmap & reserve */
365 munmap (va, size);
366 mmap (va, size, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
367 -1, 0);
368 }
Damjan Marion68b4da62018-09-30 18:26:20 +0200369 if (a->fd != -1)
370 close (a->fd);
371 return 0;
372}
373
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200374__clib_export void *
Damjan Marion68b4da62018-09-30 18:26:20 +0200375clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name,
Damjan Marion567e61d2018-10-24 17:08:26 +0200376 uword size, u32 log2_page_sz, u32 numa_node)
Damjan Marion68b4da62018-09-30 18:26:20 +0200377{
378 clib_pmalloc_arena_t *a;
379 clib_pmalloc_page_t *pp;
Damjan Marion567e61d2018-10-24 17:08:26 +0200380 u32 n_pages;
381
382 clib_error_free (pm->error);
383
384 if (log2_page_sz == 0)
385 log2_page_sz = pm->def_log2_page_sz;
386 else if (log2_page_sz != pm->def_log2_page_sz &&
Damjan Marion6bfd0762020-09-11 22:16:53 +0200387 log2_page_sz != clib_mem_get_log2_page_size ())
Damjan Marion567e61d2018-10-24 17:08:26 +0200388 {
389 pm->error = clib_error_create ("unsupported page size (%uKB)",
390 1 << (log2_page_sz - 10));
391 return 0;
392 }
393
394 n_pages = pmalloc_size2pages (size, pm->def_log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200395
396 if (n_pages + vec_len (pm->pages) > pm->max_pages)
397 return 0;
398
Damjan Marionf8cb7012020-10-09 17:16:55 +0200399 if (numa_node == CLIB_PMALLOC_NUMA_LOCAL)
400 numa_node = clib_get_current_numa_node ();
Damjan Marion68b4da62018-09-30 18:26:20 +0200401
402 pool_get (pm->arenas, a);
403 a->index = a - pm->arenas;
404 a->name = format (0, "%s%c", name, 0);
405 a->numa_node = numa_node;
406 a->flags = CLIB_PMALLOC_ARENA_F_SHARED_MEM;
Damjan Marion567e61d2018-10-24 17:08:26 +0200407 a->log2_subpage_sz = log2_page_sz;
408 a->subpages_per_page = 1U << (pm->def_log2_page_sz - log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200409
410 if ((pp = pmalloc_map_pages (pm, a, numa_node, n_pages)) == 0)
411 {
412 vec_free (a->name);
413 memset (a, 0, sizeof (*a));
414 pool_put (pm->arenas, a);
415 return 0;
416 }
417
Kingwel Xie5efaeee2018-11-10 02:56:00 -0500418 return pm->base + ((uword) pp->index << pm->def_log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200419}
420
421static inline void *
422clib_pmalloc_alloc_inline (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
423 uword size, uword align, u32 numa_node)
424{
425 clib_pmalloc_page_t *pp;
426 u32 n_blocks, block_align, *page_index;
427
428 ASSERT (is_pow2 (align));
429
Damjan Marionf8cb7012020-10-09 17:16:55 +0200430 if (numa_node == CLIB_PMALLOC_NUMA_LOCAL)
431 numa_node = clib_get_current_numa_node ();
Damjan Marion68b4da62018-09-30 18:26:20 +0200432
433 if (a == 0)
434 {
Damjan Marion567e61d2018-10-24 17:08:26 +0200435 if (size > 1ULL << pm->def_log2_page_sz)
436 return 0;
437
Damjan Marion68b4da62018-09-30 18:26:20 +0200438 vec_validate_init_empty (pm->default_arena_for_numa_node,
439 numa_node, ~0);
440 if (pm->default_arena_for_numa_node[numa_node] == ~0)
441 {
442 pool_get (pm->arenas, a);
443 pm->default_arena_for_numa_node[numa_node] = a - pm->arenas;
444 a->name = format (0, "default-numa-%u%c", numa_node, 0);
445 a->numa_node = numa_node;
Damjan Marion567e61d2018-10-24 17:08:26 +0200446 a->log2_subpage_sz = pm->def_log2_page_sz;
447 a->subpages_per_page = 1;
Damjan Marion68b4da62018-09-30 18:26:20 +0200448 }
449 else
450 a = pool_elt_at_index (pm->arenas,
451 pm->default_arena_for_numa_node[numa_node]);
452 }
Damjan Marion567e61d2018-10-24 17:08:26 +0200453 else if (size > 1ULL << a->log2_subpage_sz)
454 return 0;
Damjan Marion68b4da62018-09-30 18:26:20 +0200455
456 n_blocks = round_pow2 (size, PMALLOC_BLOCK_SZ) / PMALLOC_BLOCK_SZ;
457 block_align = align >> PMALLOC_LOG2_BLOCK_SZ;
458
459 vec_foreach (page_index, a->page_indices)
460 {
461 pp = vec_elt_at_index (pm->pages, *page_index);
462 void *rv = alloc_chunk_from_page (pm, pp, n_blocks, block_align,
463 numa_node);
464
465 if (rv)
466 return rv;
467 }
468
469 if ((a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM) == 0 &&
470 (pp = pmalloc_map_pages (pm, a, numa_node, 1)))
471 return alloc_chunk_from_page (pm, pp, n_blocks, block_align, numa_node);
472
473 return 0;
474}
475
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200476__clib_export void *
Damjan Marion68b4da62018-09-30 18:26:20 +0200477clib_pmalloc_alloc_aligned_on_numa (clib_pmalloc_main_t * pm, uword size,
478 uword align, u32 numa_node)
479{
480 return clib_pmalloc_alloc_inline (pm, 0, size, align, numa_node);
481}
482
Damjan Marionf4cfa2a2022-06-01 16:18:23 +0200483__clib_export void *
484clib_pmalloc_alloc_aligned (clib_pmalloc_main_t *pm, uword size, uword align)
Damjan Marion68b4da62018-09-30 18:26:20 +0200485{
486 return clib_pmalloc_alloc_inline (pm, 0, size, align,
487 CLIB_PMALLOC_NUMA_LOCAL);
488}
489
Damjan Marion4a251d02021-05-06 17:28:12 +0200490__clib_export void *
491clib_pmalloc_alloc_from_arena (clib_pmalloc_main_t *pm, void *arena_va,
Damjan Marion68b4da62018-09-30 18:26:20 +0200492 uword size, uword align)
493{
494 clib_pmalloc_arena_t *a = clib_pmalloc_get_arena (pm, arena_va);
495 return clib_pmalloc_alloc_inline (pm, a, size, align, 0);
496}
497
Damjan Marion567e61d2018-10-24 17:08:26 +0200498static inline int
499pmalloc_chunks_mergeable (clib_pmalloc_arena_t * a, clib_pmalloc_page_t * pp,
500 u32 ci1, u32 ci2)
501{
502 clib_pmalloc_chunk_t *c1, *c2;
503
504 if (ci1 == ~0 || ci2 == ~0)
505 return 0;
506
507 c1 = get_chunk (pp, ci1);
508 c2 = get_chunk (pp, ci2);
509
510 if (c1->used || c2->used)
511 return 0;
512
513 if (c1->start >> (a->log2_subpage_sz - PMALLOC_LOG2_BLOCK_SZ) !=
514 c2->start >> (a->log2_subpage_sz - PMALLOC_LOG2_BLOCK_SZ))
515 return 0;
516
517 return 1;
518}
519
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200520__clib_export void
Damjan Marion68b4da62018-09-30 18:26:20 +0200521clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va)
522{
523 clib_pmalloc_page_t *pp;
524 clib_pmalloc_chunk_t *c;
Damjan Marion567e61d2018-10-24 17:08:26 +0200525 clib_pmalloc_arena_t *a;
Damjan Marion68b4da62018-09-30 18:26:20 +0200526 uword *p;
527 u32 chunk_index, page_index;
528
529 p = hash_get (pm->chunk_index_by_va, pointer_to_uword (va));
530
531 if (p == 0)
532 os_panic ();
533
534 chunk_index = p[0];
535 page_index = clib_pmalloc_get_page_index (pm, va);
536 hash_unset (pm->chunk_index_by_va, pointer_to_uword (va));
537
538 pp = vec_elt_at_index (pm->pages, page_index);
539 c = pool_elt_at_index (pp->chunks, chunk_index);
Damjan Marion567e61d2018-10-24 17:08:26 +0200540 a = pool_elt_at_index (pm->arenas, pp->arena_index);
Damjan Marion68b4da62018-09-30 18:26:20 +0200541 c->used = 0;
542 pp->n_free_blocks += c->size;
543 pp->n_free_chunks++;
544
545 /* merge with next if free */
Damjan Marion567e61d2018-10-24 17:08:26 +0200546 if (pmalloc_chunks_mergeable (a, pp, chunk_index, c->next))
Damjan Marion68b4da62018-09-30 18:26:20 +0200547 {
548 clib_pmalloc_chunk_t *next = get_chunk (pp, c->next);
549 c->size += next->size;
550 c->next = next->next;
551 if (next->next != ~0)
552 get_chunk (pp, next->next)->prev = chunk_index;
553 memset (next, 0, sizeof (*next));
554 pool_put (pp->chunks, next);
555 pp->n_free_chunks--;
556 }
557
558 /* merge with prev if free */
Damjan Marion567e61d2018-10-24 17:08:26 +0200559 if (pmalloc_chunks_mergeable (a, pp, c->prev, chunk_index))
Damjan Marion68b4da62018-09-30 18:26:20 +0200560 {
561 clib_pmalloc_chunk_t *prev = get_chunk (pp, c->prev);
562 prev->size += c->size;
563 prev->next = c->next;
564 if (c->next != ~0)
565 get_chunk (pp, c->next)->prev = c->prev;
566 memset (c, 0, sizeof (*c));
567 pool_put (pp->chunks, c);
568 pp->n_free_chunks--;
569 }
570}
571
572static u8 *
573format_pmalloc_page (u8 * s, va_list * va)
574{
575 clib_pmalloc_page_t *pp = va_arg (*va, clib_pmalloc_page_t *);
576 int verbose = va_arg (*va, int);
577 u32 indent = format_get_indent (s);
578
Damjan Marion68b4da62018-09-30 18:26:20 +0200579 if (pp->chunks == 0)
580 return s;
581
582 s = format (s, "free %u chunks %u free-chunks %d ",
583 (pp->n_free_blocks) << PMALLOC_LOG2_BLOCK_SZ,
584 pool_elts (pp->chunks), pp->n_free_chunks);
585
586 if (verbose >= 2)
587 {
588 clib_pmalloc_chunk_t *c;
589 c = pool_elt_at_index (pp->chunks, pp->first_chunk_index);
590 s = format (s, "\n%U%12s%12s%8s%8s%8s%8s",
591 format_white_space, indent + 2,
592 "chunk offset", "size", "used", "index", "prev", "next");
593 while (1)
594 {
595 s = format (s, "\n%U%12u%12u%8s%8d%8d%8d",
596 format_white_space, indent + 2,
597 c->start << PMALLOC_LOG2_BLOCK_SZ,
598 c->size << PMALLOC_LOG2_BLOCK_SZ,
599 c->used ? "yes" : "no",
600 c - pp->chunks, c->prev, c->next);
601 if (c->next == ~0)
602 break;
603 c = pool_elt_at_index (pp->chunks, c->next);
604 }
605 }
606 return s;
607}
608
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200609__clib_export u8 *
Damjan Marion68b4da62018-09-30 18:26:20 +0200610format_pmalloc (u8 * s, va_list * va)
611{
612 clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *);
613 int verbose = va_arg (*va, int);
614 u32 indent = format_get_indent (s);
615
616 clib_pmalloc_page_t *pp;
617 clib_pmalloc_arena_t *a;
618
Damjan Marion567e61d2018-10-24 17:08:26 +0200619 s = format (s, "used-pages %u reserved-pages %u default-page-size %U "
Damjan Marionc04e2b02018-10-25 15:56:04 +0200620 "lookup-page-size %U%s", vec_len (pm->pages), pm->max_pages,
Damjan Marion567e61d2018-10-24 17:08:26 +0200621 format_log2_page_size, pm->def_log2_page_sz,
Damjan Marionc04e2b02018-10-25 15:56:04 +0200622 format_log2_page_size, pm->lookup_log2_page_sz,
623 pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP ? " no-pagemap" : "");
Damjan Marion567e61d2018-10-24 17:08:26 +0200624
Damjan Marion68b4da62018-09-30 18:26:20 +0200625
626 if (verbose >= 2)
627 s = format (s, " va-start %p", pm->base);
628
629 if (pm->error)
630 s = format (s, "\n%Ulast-error: %U", format_white_space, indent + 2,
631 format_clib_error, pm->error);
632
633
634 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100635 pool_foreach (a, pm->arenas)
Damjan Marion68b4da62018-09-30 18:26:20 +0200636 {
637 u32 *page_index;
Damjan Marion567e61d2018-10-24 17:08:26 +0200638 s = format (s, "\n%Uarena '%s' pages %u subpage-size %U numa-node %u",
639 format_white_space, indent + 2, a->name,
640 vec_len (a->page_indices), format_log2_page_size,
641 a->log2_subpage_sz, a->numa_node);
Damjan Marion68b4da62018-09-30 18:26:20 +0200642 if (a->fd != -1)
643 s = format (s, " shared fd %d", a->fd);
644 if (verbose >= 1)
645 vec_foreach (page_index, a->page_indices)
646 {
647 pp = vec_elt_at_index (pm->pages, *page_index);
648 s = format (s, "\n%U%U", format_white_space, indent + 4,
649 format_pmalloc_page, pp, verbose);
650 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100651 }
Damjan Marion68b4da62018-09-30 18:26:20 +0200652 /* *INDENT-ON* */
653
654 return s;
655}
656
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200657__clib_export u8 *
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100658format_pmalloc_map (u8 * s, va_list * va)
659{
660 clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *);
661
662 u32 index;
663 s = format (s, "%16s %13s %8s", "virtual-addr", "physical-addr", "size");
664 vec_foreach_index (index, pm->lookup_table)
665 {
666 uword *lookup_val, pa, va;
667 lookup_val = vec_elt_at_index (pm->lookup_table, index);
Kingwel Xiedbc34b82018-11-11 22:55:58 -0500668 va =
669 pointer_to_uword (pm->base) +
670 ((uword) index << pm->lookup_log2_page_sz);
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100671 pa = va - *lookup_val;
672 s =
673 format (s, "\n %16p %13p %8U", uword_to_pointer (va, u64),
674 uword_to_pointer (pa, u64), format_log2_page_size,
675 pm->lookup_log2_page_sz);
676 }
677 return s;
678}
679
Damjan Marion68b4da62018-09-30 18:26:20 +0200680/*
681 * fd.io coding-style-patch-verification: ON
682 *
683 * Local Variables:
684 * eval: (c-set-style "gnu")
685 * End:
686 */