blob: 546a4fe336bcd49735520f4509ad87ef7ef72096 [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 Marion68b4da62018-09-30 18:26:20 +020022#include <linux/mempolicy.h>
23#include <linux/memfd.h>
Damjan Marion1ee346a2019-03-18 17:06:51 +010024#include <sched.h>
Damjan Marion68b4da62018-09-30 18:26:20 +020025
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
39static inline clib_pmalloc_chunk_t *
40get_chunk (clib_pmalloc_page_t * pp, u32 index)
41{
42 return pool_elt_at_index (pp->chunks, index);
43}
44
Damjan Marion567e61d2018-10-24 17:08:26 +020045static inline uword
46pmalloc_size2pages (uword size, u32 log2_page_sz)
47{
48 return round_pow2 (size, 1ULL << log2_page_sz) >> log2_page_sz;
49}
50
Damjan Marion68b4da62018-09-30 18:26:20 +020051static inline int
52pmalloc_validate_numa_node (u32 * numa_node)
53{
54 if (*numa_node == CLIB_PMALLOC_NUMA_LOCAL)
55 {
56 u32 cpu;
Damjan Marion1ee346a2019-03-18 17:06:51 +010057 if (getcpu (&cpu, numa_node) != 0)
Damjan Marion68b4da62018-09-30 18:26:20 +020058 return 1;
59 }
60 return 0;
61}
62
Damjan Mariondae1c7e2020-10-17 13:32:25 +020063__clib_export int
Damjan Marion5a6c8092019-02-21 14:44:59 +010064clib_pmalloc_init (clib_pmalloc_main_t * pm, uword base_addr, uword size)
Damjan Marion68b4da62018-09-30 18:26:20 +020065{
Dave Barach16e4a4a2020-04-16 12:00:14 -040066 uword base, pagesize;
Damjan Marionc04e2b02018-10-25 15:56:04 +020067 u64 *pt = 0;
Damjan Marion68b4da62018-09-30 18:26:20 +020068
69 ASSERT (pm->error == 0);
70
Damjan Marion9787f5f2018-10-24 12:56:32 +020071 pagesize = clib_mem_get_default_hugepage_size ();
Damjan Marion567e61d2018-10-24 17:08:26 +020072 pm->def_log2_page_sz = min_log2 (pagesize);
Damjan Marion567e61d2018-10-24 17:08:26 +020073 pm->lookup_log2_page_sz = pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +020074
Damjan Marionc04e2b02018-10-25 15:56:04 +020075 /* check if pagemap is accessible */
Damjan Marion6bfd0762020-09-11 22:16:53 +020076 pt = clib_mem_vm_get_paddr (&pt, CLIB_MEM_PAGE_SZ_DEFAULT, 1);
Damjan Marionc04e2b02018-10-25 15:56:04 +020077 if (pt == 0 || pt[0] == 0)
78 pm->flags |= CLIB_PMALLOC_F_NO_PAGEMAP;
79
Damjan Marion68b4da62018-09-30 18:26:20 +020080 size = size ? size : ((u64) DEFAULT_RESERVED_MB) << 20;
81 size = round_pow2 (size, pagesize);
82
Damjan Marion567e61d2018-10-24 17:08:26 +020083 pm->max_pages = size >> pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +020084
Dave Barach16e4a4a2020-04-16 12:00:14 -040085 base = clib_mem_vm_reserve (base_addr, size, pm->def_log2_page_sz);
Damjan Marion5a6c8092019-02-21 14:44:59 +010086
Dave Barach16e4a4a2020-04-16 12:00:14 -040087 if (base == ~0)
Damjan Marion68b4da62018-09-30 18:26:20 +020088 {
Dave Barach16e4a4a2020-04-16 12:00:14 -040089 pm->error = clib_error_return (0, "failed to reserve %u pages",
90 pm->max_pages);
Damjan Marion68b4da62018-09-30 18:26:20 +020091 return -1;
92 }
93
Dave Barach16e4a4a2020-04-16 12:00:14 -040094 pm->base = uword_to_pointer (base, void *);
Damjan Marion68b4da62018-09-30 18:26:20 +020095 return 0;
96}
97
98static inline void *
99alloc_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 Marion567e61d2018-10-24 17:08:26 +0200102 clib_pmalloc_chunk_t *c = 0;
103 clib_pmalloc_arena_t *a;
Damjan Marion68b4da62018-09-30 18:26:20 +0200104 void *va;
105 u32 off;
106 u32 alloc_chunk_index;
107
Damjan Marion567e61d2018-10-24 17:08:26 +0200108 a = pool_elt_at_index (pm->arenas, pp->arena_index);
109
Damjan Marion68b4da62018-09-30 18:26:20 +0200110 if (pp->chunks == 0)
111 {
Damjan Marion567e61d2018-10-24 17:08:26 +0200112 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 Marion68b4da62018-09-30 18:26:20 +0200129 }
130
Damjan Marion78c0ff72019-01-23 12:50:24 +0100131 if (pp->n_free_blocks < n_blocks)
132 return 0;
133
Damjan Marion68b4da62018-09-30 18:26:20 +0200134 alloc_chunk_index = pp->first_chunk_index;
135
136next_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 Marion567e61d2018-10-24 17:08:26 +0200190 va = pm->base + ((pp - pm->pages) << pm->def_log2_page_sz) +
Damjan Marion68b4da62018-09-30 18:26:20 +0200191 (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 Marion567e61d2018-10-24 17:08:26 +0200198static void
199pmalloc_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 Barach96e2d442018-11-14 11:42:03 -0500208 p = (uword) first *elts_per_page;
Damjan Marionc04e2b02018-10-25 15:56:04 +0200209 if (pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP)
210 {
Damjan Marion878b65a2018-10-26 10:29:35 +0200211 while (p < (uword) elts_per_page * count)
Damjan Marionc04e2b02018-10-25 15:56:04 +0200212 {
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 Marion878b65a2018-10-26 10:29:35 +0200221 while (p < (uword) elts_per_page * count)
Damjan Marion567e61d2018-10-24 17:08:26 +0200222 {
223 va = pointer_to_uword (pm->base) + (p << pm->lookup_log2_page_sz);
Damjan Marion878b65a2018-10-26 10:29:35 +0200224 pa = 0;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200225 seek = (va >> clib_mem_get_log2_page_size ()) * sizeof (pa);
Damjan Marion567e61d2018-10-24 17:08:26 +0200226 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 Marion6bfd0762020-09-11 22:16:53 +0200230 pa = (pa & pow2_mask (55)) << clib_mem_get_log2_page_size ();
Damjan Marion567e61d2018-10-24 17:08:26 +0200231 }
232 pm->lookup_table[p] = va - pa;
233 p++;
234 }
235
236 if (fd != -1)
237 close (fd);
238}
239
Damjan Marion68b4da62018-09-30 18:26:20 +0200240static inline clib_pmalloc_page_t *
241pmalloc_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 Marion567e61d2018-10-24 17:08:26 +0200245 int status, rv, i, mmap_flags;
Damjan Marion801c7012019-10-30 18:07:35 +0100246 void *va = MAP_FAILED;
Damjan Marion68b4da62018-09-30 18:26:20 +0200247 int old_mpol = -1;
248 long unsigned int mask[16] = { 0 };
249 long unsigned int old_mask[16] = { 0 };
Damjan Marion567e61d2018-10-24 17:08:26 +0200250 uword size = (uword) n_pages << pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +0200251
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 Marion6bfd0762020-09-11 22:16:53 +0200260 if (a->log2_subpage_sz != clib_mem_get_log2_page_size ())
Damjan Marion567e61d2018-10-24 17:08:26 +0200261 {
262 pm->error = clib_sysfs_prealloc_hugepages (numa_node,
263 a->log2_subpage_sz, n_pages);
Damjan Marion68b4da62018-09-30 18:26:20 +0200264
Damjan Marion567e61d2018-10-24 17:08:26 +0200265 if (pm->error)
266 return 0;
267 }
Damjan Marion68b4da62018-09-30 18:26:20 +0200268
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 Marion54e8e392018-11-07 17:55:26 +0100286 mmap_flags = MAP_FIXED;
Damjan Marionc04e2b02018-10-25 15:56:04 +0200287
Damjan Marion68b4da62018-09-30 18:26:20 +0200288 if (a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM)
289 {
290 mmap_flags |= MAP_SHARED;
Damjan Marionbdbb0c52020-09-17 10:40:44 +0200291 a->fd = clib_mem_vm_create_fd (a->log2_subpage_sz, "%s", a->name);
Damjan Marion68b4da62018-09-30 18:26:20 +0200292 if (a->fd == -1)
293 goto error;
Damjan Marion54e8e392018-11-07 17:55:26 +0100294 if ((ftruncate (a->fd, size)) == -1)
295 goto error;
Damjan Marion68b4da62018-09-30 18:26:20 +0200296 }
297 else
298 {
Damjan Marion6bfd0762020-09-11 22:16:53 +0200299 if (a->log2_subpage_sz != clib_mem_get_log2_page_size ())
Damjan Marion8ebd7922018-11-28 10:46:03 +0100300 mmap_flags |= MAP_HUGETLB;
301
Damjan Marion54e8e392018-11-07 17:55:26 +0100302 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
Damjan Marion68b4da62018-09-30 18:26:20 +0200303 a->fd = -1;
304 }
305
Damjan Marion567e61d2018-10-24 17:08:26 +0200306 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 Marion68b4da62018-09-30 18:26:20 +0200309 {
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 Yourtchenko9ce35232019-11-18 10:23:54 +0000313 va = MAP_FAILED;
Damjan Marion68b4da62018-09-30 18:26:20 +0200314 goto error;
315 }
316
Damjan Marion6bfd0762020-09-11 22:16:53 +0200317 if (a->log2_subpage_sz != clib_mem_get_log2_page_size () &&
318 mlock (va, size) != 0)
Artem Belovf6defa12019-02-26 01:47:34 +0000319 {
Damjan Marion801c7012019-10-30 18:07:35 +0100320 pm->error = clib_error_return_unix (0, "Unable to lock pages");
321 goto error;
Artem Belovf6defa12019-02-26 01:47:34 +0000322 }
323
Damjan Marion567e61d2018-10-24 17:08:26 +0200324 clib_memset (va, 0, size);
325
Damjan Marion68b4da62018-09-30 18:26:20 +0200326 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 Marion68b4da62018-09-30 18:26:20 +0200344 goto error;
345 }
346
Damjan Marion68b4da62018-09-30 18:26:20 +0200347 for (i = 0; i < n_pages; i++)
348 {
Damjan Marion68b4da62018-09-30 18:26:20 +0200349 vec_add2 (pm->pages, pp, 1);
Damjan Marion567e61d2018-10-24 17:08:26 +0200350 pp->n_free_blocks = 1 << (pm->def_log2_page_sz - PMALLOC_LOG2_BLOCK_SZ);
Damjan Marion68b4da62018-09-30 18:26:20 +0200351 pp->index = pp - pm->pages;
352 pp->arena_index = a->index;
Damjan Marion68b4da62018-09-30 18:26:20 +0200353 vec_add1 (a->page_indices, pp->index);
354 a->n_pages++;
Damjan Marion68b4da62018-09-30 18:26:20 +0200355 }
356
Damjan Marion567e61d2018-10-24 17:08:26 +0200357
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 Marion68b4da62018-09-30 18:26:20 +0200368
369 /* return pointer to 1st page */
370 return pp - (n_pages - 1);
371
372error:
Damjan Marion801c7012019-10-30 18:07:35 +0100373 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 Marion68b4da62018-09-30 18:26:20 +0200380 if (a->fd != -1)
381 close (a->fd);
382 return 0;
383}
384
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200385__clib_export void *
Damjan Marion68b4da62018-09-30 18:26:20 +0200386clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name,
Damjan Marion567e61d2018-10-24 17:08:26 +0200387 uword size, u32 log2_page_sz, u32 numa_node)
Damjan Marion68b4da62018-09-30 18:26:20 +0200388{
389 clib_pmalloc_arena_t *a;
390 clib_pmalloc_page_t *pp;
Damjan Marion567e61d2018-10-24 17:08:26 +0200391 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 Marion6bfd0762020-09-11 22:16:53 +0200398 log2_page_sz != clib_mem_get_log2_page_size ())
Damjan Marion567e61d2018-10-24 17:08:26 +0200399 {
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 Marion68b4da62018-09-30 18:26:20 +0200406
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 Marion567e61d2018-10-24 17:08:26 +0200418 a->log2_subpage_sz = log2_page_sz;
419 a->subpages_per_page = 1U << (pm->def_log2_page_sz - log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200420
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 Xie5efaeee2018-11-10 02:56:00 -0500429 return pm->base + ((uword) pp->index << pm->def_log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200430}
431
432static inline void *
433clib_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 Marion567e61d2018-10-24 17:08:26 +0200446 if (size > 1ULL << pm->def_log2_page_sz)
447 return 0;
448
Damjan Marion68b4da62018-09-30 18:26:20 +0200449 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 Marion567e61d2018-10-24 17:08:26 +0200457 a->log2_subpage_sz = pm->def_log2_page_sz;
458 a->subpages_per_page = 1;
Damjan Marion68b4da62018-09-30 18:26:20 +0200459 }
460 else
461 a = pool_elt_at_index (pm->arenas,
462 pm->default_arena_for_numa_node[numa_node]);
463 }
Damjan Marion567e61d2018-10-24 17:08:26 +0200464 else if (size > 1ULL << a->log2_subpage_sz)
465 return 0;
Damjan Marion68b4da62018-09-30 18:26:20 +0200466
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 Mariondae1c7e2020-10-17 13:32:25 +0200487__clib_export void *
Damjan Marion68b4da62018-09-30 18:26:20 +0200488clib_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
494void *
495clib_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
501void *
502clib_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 Marion567e61d2018-10-24 17:08:26 +0200509static inline int
510pmalloc_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 Mariondae1c7e2020-10-17 13:32:25 +0200531__clib_export void
Damjan Marion68b4da62018-09-30 18:26:20 +0200532clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va)
533{
534 clib_pmalloc_page_t *pp;
535 clib_pmalloc_chunk_t *c;
Damjan Marion567e61d2018-10-24 17:08:26 +0200536 clib_pmalloc_arena_t *a;
Damjan Marion68b4da62018-09-30 18:26:20 +0200537 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 Marion567e61d2018-10-24 17:08:26 +0200551 a = pool_elt_at_index (pm->arenas, pp->arena_index);
Damjan Marion68b4da62018-09-30 18:26:20 +0200552 c->used = 0;
553 pp->n_free_blocks += c->size;
554 pp->n_free_chunks++;
555
556 /* merge with next if free */
Damjan Marion567e61d2018-10-24 17:08:26 +0200557 if (pmalloc_chunks_mergeable (a, pp, chunk_index, c->next))
Damjan Marion68b4da62018-09-30 18:26:20 +0200558 {
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 Marion567e61d2018-10-24 17:08:26 +0200570 if (pmalloc_chunks_mergeable (a, pp, c->prev, chunk_index))
Damjan Marion68b4da62018-09-30 18:26:20 +0200571 {
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
583static u8 *
584format_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 Marion68b4da62018-09-30 18:26:20 +0200590 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 Mariondae1c7e2020-10-17 13:32:25 +0200620__clib_export u8 *
Damjan Marion68b4da62018-09-30 18:26:20 +0200621format_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 Marion567e61d2018-10-24 17:08:26 +0200630 s = format (s, "used-pages %u reserved-pages %u default-page-size %U "
Damjan Marionc04e2b02018-10-25 15:56:04 +0200631 "lookup-page-size %U%s", vec_len (pm->pages), pm->max_pages,
Damjan Marion567e61d2018-10-24 17:08:26 +0200632 format_log2_page_size, pm->def_log2_page_sz,
Damjan Marionc04e2b02018-10-25 15:56:04 +0200633 format_log2_page_size, pm->lookup_log2_page_sz,
634 pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP ? " no-pagemap" : "");
Damjan Marion567e61d2018-10-24 17:08:26 +0200635
Damjan Marion68b4da62018-09-30 18:26:20 +0200636
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 Marionb2c31b62020-12-13 21:47:40 +0100646 pool_foreach (a, pm->arenas)
Damjan Marion68b4da62018-09-30 18:26:20 +0200647 {
648 u32 *page_index;
Damjan Marion567e61d2018-10-24 17:08:26 +0200649 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 Marion68b4da62018-09-30 18:26:20 +0200653 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 Marionb2c31b62020-12-13 21:47:40 +0100662 }
Damjan Marion68b4da62018-09-30 18:26:20 +0200663 /* *INDENT-ON* */
664
665 return s;
666}
667
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200668__clib_export u8 *
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100669format_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 Xiedbc34b82018-11-11 22:55:58 -0500679 va =
680 pointer_to_uword (pm->base) +
681 ((uword) index << pm->lookup_log2_page_sz);
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100682 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 Marion68b4da62018-09-30 18:26:20 +0200691/*
692 * fd.io coding-style-patch-verification: ON
693 *
694 * Local Variables:
695 * eval: (c-set-style "gnu")
696 * End:
697 */