blob: 9f908f40cc9ddc3fb78156c1119439e51cd652ce [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>
24
25#include <vppinfra/format.h>
26#include <vppinfra/linux/syscall.h>
27#include <vppinfra/linux/sysfs.h>
28#include <vppinfra/mem.h>
29#include <vppinfra/hash.h>
30#include <vppinfra/pmalloc.h>
31
32#if __SIZEOF_POINTER__ >= 8
33#define DEFAULT_RESERVED_MB 16384
34#else
35#define DEFAULT_RESERVED_MB 256
36#endif
37
38static inline clib_pmalloc_chunk_t *
39get_chunk (clib_pmalloc_page_t * pp, u32 index)
40{
41 return pool_elt_at_index (pp->chunks, index);
42}
43
Damjan Marion567e61d2018-10-24 17:08:26 +020044static inline uword
45pmalloc_size2pages (uword size, u32 log2_page_sz)
46{
47 return round_pow2 (size, 1ULL << log2_page_sz) >> log2_page_sz;
48}
49
Damjan Marion68b4da62018-09-30 18:26:20 +020050static inline int
51pmalloc_validate_numa_node (u32 * numa_node)
52{
53 if (*numa_node == CLIB_PMALLOC_NUMA_LOCAL)
54 {
55 u32 cpu;
56 if (getcpu (&cpu, numa_node, 0) != 0)
57 return 1;
58 }
59 return 0;
60}
61
62int
Damjan Marion5a6c8092019-02-21 14:44:59 +010063clib_pmalloc_init (clib_pmalloc_main_t * pm, uword base_addr, uword size)
Damjan Marion68b4da62018-09-30 18:26:20 +020064{
Damjan Marion68b4da62018-09-30 18:26:20 +020065 uword off, pagesize;
Damjan Marionc04e2b02018-10-25 15:56:04 +020066 u64 *pt = 0;
Damjan Marion5a6c8092019-02-21 14:44:59 +010067 int mmap_flags;
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);
73 pm->sys_log2_page_sz = min_log2 (sysconf (_SC_PAGESIZE));
74 pm->lookup_log2_page_sz = pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +020075
Damjan Marionc04e2b02018-10-25 15:56:04 +020076 /* check if pagemap is accessible */
77 pt = clib_mem_vm_get_paddr (&pt, pm->sys_log2_page_sz, 1);
78 if (pt == 0 || pt[0] == 0)
79 pm->flags |= CLIB_PMALLOC_F_NO_PAGEMAP;
80
Damjan Marion68b4da62018-09-30 18:26:20 +020081 size = size ? size : ((u64) DEFAULT_RESERVED_MB) << 20;
82 size = round_pow2 (size, pagesize);
83
Damjan Marion567e61d2018-10-24 17:08:26 +020084 pm->max_pages = size >> pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +020085
86 /* reserve VA space for future growth */
Damjan Marion5a6c8092019-02-21 14:44:59 +010087 mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS;
88
89 if (base_addr)
90 mmap_flags |= MAP_FIXED;
91
92 pm->base = mmap (uword_to_pointer (base_addr, void *), size + pagesize,
93 PROT_NONE, mmap_flags, -1, 0);
Damjan Marion68b4da62018-09-30 18:26:20 +020094
95 if (pm->base == MAP_FAILED)
96 {
97 pm->error = clib_error_return_unix (0, "failed to reserve %u pages");
98 return -1;
99 }
100
101 off = round_pow2 (pointer_to_uword (pm->base), pagesize) -
102 pointer_to_uword (pm->base);
103
104 /* trim start and end of reservation to be page aligned */
105 if (off)
106 {
107 munmap (pm->base, off);
108 pm->base += off;
109 }
110
Damjan Marion878b65a2018-10-26 10:29:35 +0200111 munmap (pm->base + ((uword) pm->max_pages * pagesize), pagesize - off);
Damjan Marion68b4da62018-09-30 18:26:20 +0200112 return 0;
113}
114
115static inline void *
116alloc_chunk_from_page (clib_pmalloc_main_t * pm, clib_pmalloc_page_t * pp,
117 u32 n_blocks, u32 block_align, u32 numa_node)
118{
Damjan Marion567e61d2018-10-24 17:08:26 +0200119 clib_pmalloc_chunk_t *c = 0;
120 clib_pmalloc_arena_t *a;
Damjan Marion68b4da62018-09-30 18:26:20 +0200121 void *va;
122 u32 off;
123 u32 alloc_chunk_index;
124
Damjan Marion567e61d2018-10-24 17:08:26 +0200125 a = pool_elt_at_index (pm->arenas, pp->arena_index);
126
Damjan Marion68b4da62018-09-30 18:26:20 +0200127 if (pp->chunks == 0)
128 {
Damjan Marion567e61d2018-10-24 17:08:26 +0200129 u32 i, start = 0, prev = ~0;
130
131 for (i = 0; i < a->subpages_per_page; i++)
132 {
133 pool_get (pp->chunks, c);
134 c->start = start;
135 c->prev = prev;
136 c->size = pp->n_free_blocks / a->subpages_per_page;
137 start += c->size;
138 if (prev == ~0)
139 pp->first_chunk_index = c - pp->chunks;
140 else
141 pp->chunks[prev].next = c - pp->chunks;
142 prev = c - pp->chunks;
143 }
144 c->next = ~0;
145 pp->n_free_chunks = a->subpages_per_page;
Damjan Marion68b4da62018-09-30 18:26:20 +0200146 }
147
Damjan Marion78c0ff72019-01-23 12:50:24 +0100148 if (pp->n_free_blocks < n_blocks)
149 return 0;
150
Damjan Marion68b4da62018-09-30 18:26:20 +0200151 alloc_chunk_index = pp->first_chunk_index;
152
153next_chunk:
154 c = pool_elt_at_index (pp->chunks, alloc_chunk_index);
155 off = (block_align - (c->start & (block_align - 1))) & (block_align - 1);
156
157 if (c->used || n_blocks + off > c->size)
158 {
159 if (c->next == ~0)
160 return 0;
161 alloc_chunk_index = c->next;
162 goto next_chunk;
163 }
164
165 /* if alignment is needed create new empty chunk */
166 if (off)
167 {
168 u32 offset_chunk_index;
169 clib_pmalloc_chunk_t *co;
170 pool_get (pp->chunks, c);
171 pp->n_free_chunks++;
172 offset_chunk_index = alloc_chunk_index;
173 alloc_chunk_index = c - pp->chunks;
174
175 co = pool_elt_at_index (pp->chunks, offset_chunk_index);
176 c->size = co->size - off;
177 c->next = co->next;
178 c->start = co->start + off;
179 c->prev = offset_chunk_index;
180 co->size = off;
181 co->next = alloc_chunk_index;
182 }
183
184 c->used = 1;
185 if (c->size > n_blocks)
186 {
187 u32 tail_chunk_index;
188 clib_pmalloc_chunk_t *ct;
189 pool_get (pp->chunks, ct);
190 pp->n_free_chunks++;
191 tail_chunk_index = ct - pp->chunks;
192 c = pool_elt_at_index (pp->chunks, alloc_chunk_index);
193 ct->size = c->size - n_blocks;
194 ct->next = c->next;
195 ct->prev = alloc_chunk_index;
196 ct->start = c->start + n_blocks;
197
198 c->size = n_blocks;
199 c->next = tail_chunk_index;
200 if (ct->next != ~0)
201 pool_elt_at_index (pp->chunks, ct->next)->prev = tail_chunk_index;
202 }
203 else if (c->next != ~0)
204 pool_elt_at_index (pp->chunks, c->next)->prev = alloc_chunk_index;
205
206 c = get_chunk (pp, alloc_chunk_index);
Damjan Marion567e61d2018-10-24 17:08:26 +0200207 va = pm->base + ((pp - pm->pages) << pm->def_log2_page_sz) +
Damjan Marion68b4da62018-09-30 18:26:20 +0200208 (c->start << PMALLOC_LOG2_BLOCK_SZ);
209 hash_set (pm->chunk_index_by_va, pointer_to_uword (va), alloc_chunk_index);
210 pp->n_free_blocks -= n_blocks;
211 pp->n_free_chunks--;
212 return va;
213}
214
Damjan Marion567e61d2018-10-24 17:08:26 +0200215static void
216pmalloc_update_lookup_table (clib_pmalloc_main_t * pm, u32 first, u32 count)
217{
218 uword seek, va, pa, p;
219 int fd;
220 u32 elts_per_page = 1U << (pm->def_log2_page_sz - pm->lookup_log2_page_sz);
221
222 vec_validate_aligned (pm->lookup_table, vec_len (pm->pages) *
223 elts_per_page - 1, CLIB_CACHE_LINE_BYTES);
224
Dave Barach96e2d442018-11-14 11:42:03 -0500225 p = (uword) first *elts_per_page;
Damjan Marionc04e2b02018-10-25 15:56:04 +0200226 if (pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP)
227 {
Damjan Marion878b65a2018-10-26 10:29:35 +0200228 while (p < (uword) elts_per_page * count)
Damjan Marionc04e2b02018-10-25 15:56:04 +0200229 {
230 pm->lookup_table[p] = pointer_to_uword (pm->base) +
231 (p << pm->lookup_log2_page_sz);
232 p++;
233 }
234 return;
235 }
236
237 fd = open ((char *) "/proc/self/pagemap", O_RDONLY);
Damjan Marion878b65a2018-10-26 10:29:35 +0200238 while (p < (uword) elts_per_page * count)
Damjan Marion567e61d2018-10-24 17:08:26 +0200239 {
240 va = pointer_to_uword (pm->base) + (p << pm->lookup_log2_page_sz);
Damjan Marion878b65a2018-10-26 10:29:35 +0200241 pa = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +0200242 seek = (va >> pm->sys_log2_page_sz) * sizeof (pa);
243 if (fd != -1 && lseek (fd, seek, SEEK_SET) == seek &&
244 read (fd, &pa, sizeof (pa)) == (sizeof (pa)) &&
245 pa & (1ULL << 63) /* page present bit */ )
246 {
247 pa = (pa & pow2_mask (55)) << pm->sys_log2_page_sz;
248 }
249 pm->lookup_table[p] = va - pa;
250 p++;
251 }
252
253 if (fd != -1)
254 close (fd);
255}
256
Damjan Marion68b4da62018-09-30 18:26:20 +0200257static inline clib_pmalloc_page_t *
258pmalloc_map_pages (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
259 u32 numa_node, u32 n_pages)
260{
261 clib_pmalloc_page_t *pp = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +0200262 int status, rv, i, mmap_flags;
Damjan Marion68b4da62018-09-30 18:26:20 +0200263 void *va;
264 int old_mpol = -1;
265 long unsigned int mask[16] = { 0 };
266 long unsigned int old_mask[16] = { 0 };
Dave Baracha2aefef2019-02-27 12:36:28 -0500267 uword page_size = 1ULL << a->log2_subpage_sz;
Damjan Marion567e61d2018-10-24 17:08:26 +0200268 uword size = (uword) n_pages << pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +0200269
270 clib_error_free (pm->error);
271
272 if (pm->max_pages <= vec_len (pm->pages))
273 {
274 pm->error = clib_error_return (0, "maximum number of pages reached");
275 return 0;
276 }
277
Damjan Marion567e61d2018-10-24 17:08:26 +0200278 if (a->log2_subpage_sz != pm->sys_log2_page_sz)
279 {
280 pm->error = clib_sysfs_prealloc_hugepages (numa_node,
281 a->log2_subpage_sz, n_pages);
Damjan Marion68b4da62018-09-30 18:26:20 +0200282
Damjan Marion567e61d2018-10-24 17:08:26 +0200283 if (pm->error)
284 return 0;
285 }
Damjan Marion68b4da62018-09-30 18:26:20 +0200286
287 rv = get_mempolicy (&old_mpol, old_mask, sizeof (old_mask) * 8 + 1, 0, 0);
288 /* failure to get mempolicy means we can only proceed with numa 0 maps */
289 if (rv == -1 && numa_node != 0)
290 {
291 pm->error = clib_error_return_unix (0, "failed to get mempolicy");
292 return 0;
293 }
294
295 mask[0] = 1 << numa_node;
296 rv = set_mempolicy (MPOL_BIND, mask, sizeof (mask) * 8 + 1);
297 if (rv == -1 && numa_node != 0)
298 {
299 pm->error = clib_error_return_unix (0, "failed to set mempolicy for "
300 "numa node %u", numa_node);
301 return 0;
302 }
303
Damjan Marion54e8e392018-11-07 17:55:26 +0100304 mmap_flags = MAP_FIXED;
Damjan Marionc04e2b02018-10-25 15:56:04 +0200305
306 if ((pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP) == 0)
307 mmap_flags |= MAP_LOCKED;
Damjan Marion567e61d2018-10-24 17:08:26 +0200308
Damjan Marion68b4da62018-09-30 18:26:20 +0200309 if (a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM)
310 {
311 mmap_flags |= MAP_SHARED;
Damjan Marion8ebd7922018-11-28 10:46:03 +0100312 if (a->log2_subpage_sz != pm->sys_log2_page_sz)
Damjan Marion567e61d2018-10-24 17:08:26 +0200313 pm->error = clib_mem_create_hugetlb_fd ((char *) a->name, &a->fd);
314 else
315 pm->error = clib_mem_create_fd ((char *) a->name, &a->fd);
Damjan Marion68b4da62018-09-30 18:26:20 +0200316 if (a->fd == -1)
317 goto error;
Damjan Marion54e8e392018-11-07 17:55:26 +0100318 if ((ftruncate (a->fd, size)) == -1)
319 goto error;
Damjan Marion68b4da62018-09-30 18:26:20 +0200320 }
321 else
322 {
Damjan Marion8ebd7922018-11-28 10:46:03 +0100323 if (a->log2_subpage_sz != pm->sys_log2_page_sz)
324 mmap_flags |= MAP_HUGETLB;
325
Damjan Marion54e8e392018-11-07 17:55:26 +0100326 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
Damjan Marion68b4da62018-09-30 18:26:20 +0200327 a->fd = -1;
328 }
329
Damjan Marion567e61d2018-10-24 17:08:26 +0200330 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 Marion68b4da62018-09-30 18:26:20 +0200333 {
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);
337 goto error;
338 }
339
Artem Belovf6defa12019-02-26 01:47:34 +0000340 /* Check if huge page is not allocated,
341 wrong allocation will generate the SIGBUS */
342 if (a->log2_subpage_sz != pm->sys_log2_page_sz)
343 {
344 for (int i = 0; i < n_pages; i++)
345 {
346 unsigned char flag;
347 mincore (va + i * page_size, 1, &flag);
348 // flag is 1 if the page was successfully allocated and in memory
349 if (!flag)
350 {
351 pm->error =
352 clib_error_return_unix (0,
353 "Unable to fulfill huge page allocation request");
354 goto error;
355 }
356 }
357 }
358
Damjan Marion567e61d2018-10-24 17:08:26 +0200359 clib_memset (va, 0, size);
360
Damjan Marion68b4da62018-09-30 18:26:20 +0200361 rv = set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1);
362 if (rv == -1 && numa_node != 0)
363 {
364 pm->error = clib_error_return_unix (0, "failed to restore mempolicy");
365 goto error;
366 }
367
368 /* we tolerate move_pages failure only if request os for numa node 0
369 to support non-numa kernels */
370 rv = move_pages (0, 1, &va, 0, &status, 0);
371 if ((rv == 0 && status != numa_node) || (rv != 0 && numa_node != 0))
372 {
373 pm->error = rv == -1 ?
374 clib_error_return_unix (0, "page allocated on wrong node, numa node "
375 "%u status %d", numa_node, status) :
376 clib_error_return (0, "page allocated on wrong node, numa node "
377 "%u status %d", numa_node, status);
378
379 /* unmap & reesrve */
Damjan Marion567e61d2018-10-24 17:08:26 +0200380 munmap (va, size);
381 mmap (va, size, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
382 -1, 0);
Damjan Marion68b4da62018-09-30 18:26:20 +0200383 goto error;
384 }
385
Damjan Marion68b4da62018-09-30 18:26:20 +0200386 for (i = 0; i < n_pages; i++)
387 {
Damjan Marion68b4da62018-09-30 18:26:20 +0200388 vec_add2 (pm->pages, pp, 1);
Damjan Marion567e61d2018-10-24 17:08:26 +0200389 pp->n_free_blocks = 1 << (pm->def_log2_page_sz - PMALLOC_LOG2_BLOCK_SZ);
Damjan Marion68b4da62018-09-30 18:26:20 +0200390 pp->index = pp - pm->pages;
391 pp->arena_index = a->index;
Damjan Marion68b4da62018-09-30 18:26:20 +0200392 vec_add1 (a->page_indices, pp->index);
393 a->n_pages++;
Damjan Marion68b4da62018-09-30 18:26:20 +0200394 }
395
Damjan Marion567e61d2018-10-24 17:08:26 +0200396
397 /* if new arena is using smaller page size, we need to rebuild whole
398 lookup table */
399 if (a->log2_subpage_sz < pm->lookup_log2_page_sz)
400 {
401 pm->lookup_log2_page_sz = a->log2_subpage_sz;
402 pmalloc_update_lookup_table (pm, vec_len (pm->pages) - n_pages,
403 n_pages);
404 }
405 else
406 pmalloc_update_lookup_table (pm, 0, vec_len (pm->pages));
Damjan Marion68b4da62018-09-30 18:26:20 +0200407
408 /* return pointer to 1st page */
409 return pp - (n_pages - 1);
410
411error:
412 if (a->fd != -1)
413 close (a->fd);
414 return 0;
415}
416
417void *
418clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name,
Damjan Marion567e61d2018-10-24 17:08:26 +0200419 uword size, u32 log2_page_sz, u32 numa_node)
Damjan Marion68b4da62018-09-30 18:26:20 +0200420{
421 clib_pmalloc_arena_t *a;
422 clib_pmalloc_page_t *pp;
Damjan Marion567e61d2018-10-24 17:08:26 +0200423 u32 n_pages;
424
425 clib_error_free (pm->error);
426
427 if (log2_page_sz == 0)
428 log2_page_sz = pm->def_log2_page_sz;
429 else if (log2_page_sz != pm->def_log2_page_sz &&
430 log2_page_sz != pm->sys_log2_page_sz)
431 {
432 pm->error = clib_error_create ("unsupported page size (%uKB)",
433 1 << (log2_page_sz - 10));
434 return 0;
435 }
436
437 n_pages = pmalloc_size2pages (size, pm->def_log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200438
439 if (n_pages + vec_len (pm->pages) > pm->max_pages)
440 return 0;
441
442 if (pmalloc_validate_numa_node (&numa_node))
443 return 0;
444
445 pool_get (pm->arenas, a);
446 a->index = a - pm->arenas;
447 a->name = format (0, "%s%c", name, 0);
448 a->numa_node = numa_node;
449 a->flags = CLIB_PMALLOC_ARENA_F_SHARED_MEM;
Damjan Marion567e61d2018-10-24 17:08:26 +0200450 a->log2_subpage_sz = log2_page_sz;
451 a->subpages_per_page = 1U << (pm->def_log2_page_sz - log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200452
453 if ((pp = pmalloc_map_pages (pm, a, numa_node, n_pages)) == 0)
454 {
455 vec_free (a->name);
456 memset (a, 0, sizeof (*a));
457 pool_put (pm->arenas, a);
458 return 0;
459 }
460
Kingwel Xie5efaeee2018-11-10 02:56:00 -0500461 return pm->base + ((uword) pp->index << pm->def_log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200462}
463
464static inline void *
465clib_pmalloc_alloc_inline (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
466 uword size, uword align, u32 numa_node)
467{
468 clib_pmalloc_page_t *pp;
469 u32 n_blocks, block_align, *page_index;
470
471 ASSERT (is_pow2 (align));
472
473 if (pmalloc_validate_numa_node (&numa_node))
474 return 0;
475
476 if (a == 0)
477 {
Damjan Marion567e61d2018-10-24 17:08:26 +0200478 if (size > 1ULL << pm->def_log2_page_sz)
479 return 0;
480
Damjan Marion68b4da62018-09-30 18:26:20 +0200481 vec_validate_init_empty (pm->default_arena_for_numa_node,
482 numa_node, ~0);
483 if (pm->default_arena_for_numa_node[numa_node] == ~0)
484 {
485 pool_get (pm->arenas, a);
486 pm->default_arena_for_numa_node[numa_node] = a - pm->arenas;
487 a->name = format (0, "default-numa-%u%c", numa_node, 0);
488 a->numa_node = numa_node;
Damjan Marion567e61d2018-10-24 17:08:26 +0200489 a->log2_subpage_sz = pm->def_log2_page_sz;
490 a->subpages_per_page = 1;
Damjan Marion68b4da62018-09-30 18:26:20 +0200491 }
492 else
493 a = pool_elt_at_index (pm->arenas,
494 pm->default_arena_for_numa_node[numa_node]);
495 }
Damjan Marion567e61d2018-10-24 17:08:26 +0200496 else if (size > 1ULL << a->log2_subpage_sz)
497 return 0;
Damjan Marion68b4da62018-09-30 18:26:20 +0200498
499 n_blocks = round_pow2 (size, PMALLOC_BLOCK_SZ) / PMALLOC_BLOCK_SZ;
500 block_align = align >> PMALLOC_LOG2_BLOCK_SZ;
501
502 vec_foreach (page_index, a->page_indices)
503 {
504 pp = vec_elt_at_index (pm->pages, *page_index);
505 void *rv = alloc_chunk_from_page (pm, pp, n_blocks, block_align,
506 numa_node);
507
508 if (rv)
509 return rv;
510 }
511
512 if ((a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM) == 0 &&
513 (pp = pmalloc_map_pages (pm, a, numa_node, 1)))
514 return alloc_chunk_from_page (pm, pp, n_blocks, block_align, numa_node);
515
516 return 0;
517}
518
519void *
520clib_pmalloc_alloc_aligned_on_numa (clib_pmalloc_main_t * pm, uword size,
521 uword align, u32 numa_node)
522{
523 return clib_pmalloc_alloc_inline (pm, 0, size, align, numa_node);
524}
525
526void *
527clib_pmalloc_alloc_aligned (clib_pmalloc_main_t * pm, uword size, uword align)
528{
529 return clib_pmalloc_alloc_inline (pm, 0, size, align,
530 CLIB_PMALLOC_NUMA_LOCAL);
531}
532
533void *
534clib_pmalloc_alloc_from_arena (clib_pmalloc_main_t * pm, void *arena_va,
535 uword size, uword align)
536{
537 clib_pmalloc_arena_t *a = clib_pmalloc_get_arena (pm, arena_va);
538 return clib_pmalloc_alloc_inline (pm, a, size, align, 0);
539}
540
Damjan Marion567e61d2018-10-24 17:08:26 +0200541static inline int
542pmalloc_chunks_mergeable (clib_pmalloc_arena_t * a, clib_pmalloc_page_t * pp,
543 u32 ci1, u32 ci2)
544{
545 clib_pmalloc_chunk_t *c1, *c2;
546
547 if (ci1 == ~0 || ci2 == ~0)
548 return 0;
549
550 c1 = get_chunk (pp, ci1);
551 c2 = get_chunk (pp, ci2);
552
553 if (c1->used || c2->used)
554 return 0;
555
556 if (c1->start >> (a->log2_subpage_sz - PMALLOC_LOG2_BLOCK_SZ) !=
557 c2->start >> (a->log2_subpage_sz - PMALLOC_LOG2_BLOCK_SZ))
558 return 0;
559
560 return 1;
561}
562
Damjan Marion68b4da62018-09-30 18:26:20 +0200563void
564clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va)
565{
566 clib_pmalloc_page_t *pp;
567 clib_pmalloc_chunk_t *c;
Damjan Marion567e61d2018-10-24 17:08:26 +0200568 clib_pmalloc_arena_t *a;
Damjan Marion68b4da62018-09-30 18:26:20 +0200569 uword *p;
570 u32 chunk_index, page_index;
571
572 p = hash_get (pm->chunk_index_by_va, pointer_to_uword (va));
573
574 if (p == 0)
575 os_panic ();
576
577 chunk_index = p[0];
578 page_index = clib_pmalloc_get_page_index (pm, va);
579 hash_unset (pm->chunk_index_by_va, pointer_to_uword (va));
580
581 pp = vec_elt_at_index (pm->pages, page_index);
582 c = pool_elt_at_index (pp->chunks, chunk_index);
Damjan Marion567e61d2018-10-24 17:08:26 +0200583 a = pool_elt_at_index (pm->arenas, pp->arena_index);
Damjan Marion68b4da62018-09-30 18:26:20 +0200584 c->used = 0;
585 pp->n_free_blocks += c->size;
586 pp->n_free_chunks++;
587
588 /* merge with next if free */
Damjan Marion567e61d2018-10-24 17:08:26 +0200589 if (pmalloc_chunks_mergeable (a, pp, chunk_index, c->next))
Damjan Marion68b4da62018-09-30 18:26:20 +0200590 {
591 clib_pmalloc_chunk_t *next = get_chunk (pp, c->next);
592 c->size += next->size;
593 c->next = next->next;
594 if (next->next != ~0)
595 get_chunk (pp, next->next)->prev = chunk_index;
596 memset (next, 0, sizeof (*next));
597 pool_put (pp->chunks, next);
598 pp->n_free_chunks--;
599 }
600
601 /* merge with prev if free */
Damjan Marion567e61d2018-10-24 17:08:26 +0200602 if (pmalloc_chunks_mergeable (a, pp, c->prev, chunk_index))
Damjan Marion68b4da62018-09-30 18:26:20 +0200603 {
604 clib_pmalloc_chunk_t *prev = get_chunk (pp, c->prev);
605 prev->size += c->size;
606 prev->next = c->next;
607 if (c->next != ~0)
608 get_chunk (pp, c->next)->prev = c->prev;
609 memset (c, 0, sizeof (*c));
610 pool_put (pp->chunks, c);
611 pp->n_free_chunks--;
612 }
613}
614
615static u8 *
Damjan Marion567e61d2018-10-24 17:08:26 +0200616format_log2_page_size (u8 * s, va_list * va)
617{
618 u32 log2_page_sz = va_arg (*va, u32);
619
620 if (log2_page_sz >= 30)
621 return format (s, "%uGB", 1 << (log2_page_sz - 30));
622
623 if (log2_page_sz >= 20)
624 return format (s, "%uMB", 1 << (log2_page_sz - 20));
625
626 if (log2_page_sz >= 10)
627 return format (s, "%uKB", 1 << (log2_page_sz - 10));
628
629 return format (s, "%uB", 1 << log2_page_sz);
630}
631
632
633static u8 *
Damjan Marion68b4da62018-09-30 18:26:20 +0200634format_pmalloc_page (u8 * s, va_list * va)
635{
636 clib_pmalloc_page_t *pp = va_arg (*va, clib_pmalloc_page_t *);
637 int verbose = va_arg (*va, int);
638 u32 indent = format_get_indent (s);
639
Damjan Marion68b4da62018-09-30 18:26:20 +0200640 if (pp->chunks == 0)
641 return s;
642
643 s = format (s, "free %u chunks %u free-chunks %d ",
644 (pp->n_free_blocks) << PMALLOC_LOG2_BLOCK_SZ,
645 pool_elts (pp->chunks), pp->n_free_chunks);
646
647 if (verbose >= 2)
648 {
649 clib_pmalloc_chunk_t *c;
650 c = pool_elt_at_index (pp->chunks, pp->first_chunk_index);
651 s = format (s, "\n%U%12s%12s%8s%8s%8s%8s",
652 format_white_space, indent + 2,
653 "chunk offset", "size", "used", "index", "prev", "next");
654 while (1)
655 {
656 s = format (s, "\n%U%12u%12u%8s%8d%8d%8d",
657 format_white_space, indent + 2,
658 c->start << PMALLOC_LOG2_BLOCK_SZ,
659 c->size << PMALLOC_LOG2_BLOCK_SZ,
660 c->used ? "yes" : "no",
661 c - pp->chunks, c->prev, c->next);
662 if (c->next == ~0)
663 break;
664 c = pool_elt_at_index (pp->chunks, c->next);
665 }
666 }
667 return s;
668}
669
670u8 *
671format_pmalloc (u8 * s, va_list * va)
672{
673 clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *);
674 int verbose = va_arg (*va, int);
675 u32 indent = format_get_indent (s);
676
677 clib_pmalloc_page_t *pp;
678 clib_pmalloc_arena_t *a;
679
Damjan Marion567e61d2018-10-24 17:08:26 +0200680 s = format (s, "used-pages %u reserved-pages %u default-page-size %U "
Damjan Marionc04e2b02018-10-25 15:56:04 +0200681 "lookup-page-size %U%s", vec_len (pm->pages), pm->max_pages,
Damjan Marion567e61d2018-10-24 17:08:26 +0200682 format_log2_page_size, pm->def_log2_page_sz,
Damjan Marionc04e2b02018-10-25 15:56:04 +0200683 format_log2_page_size, pm->lookup_log2_page_sz,
684 pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP ? " no-pagemap" : "");
Damjan Marion567e61d2018-10-24 17:08:26 +0200685
Damjan Marion68b4da62018-09-30 18:26:20 +0200686
687 if (verbose >= 2)
688 s = format (s, " va-start %p", pm->base);
689
690 if (pm->error)
691 s = format (s, "\n%Ulast-error: %U", format_white_space, indent + 2,
692 format_clib_error, pm->error);
693
694
695 /* *INDENT-OFF* */
696 pool_foreach (a, pm->arenas,
697 {
698 u32 *page_index;
Damjan Marion567e61d2018-10-24 17:08:26 +0200699 s = format (s, "\n%Uarena '%s' pages %u subpage-size %U numa-node %u",
700 format_white_space, indent + 2, a->name,
701 vec_len (a->page_indices), format_log2_page_size,
702 a->log2_subpage_sz, a->numa_node);
Damjan Marion68b4da62018-09-30 18:26:20 +0200703 if (a->fd != -1)
704 s = format (s, " shared fd %d", a->fd);
705 if (verbose >= 1)
706 vec_foreach (page_index, a->page_indices)
707 {
708 pp = vec_elt_at_index (pm->pages, *page_index);
709 s = format (s, "\n%U%U", format_white_space, indent + 4,
710 format_pmalloc_page, pp, verbose);
711 }
712 });
713 /* *INDENT-ON* */
714
715 return s;
716}
717
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100718u8 *
719format_pmalloc_map (u8 * s, va_list * va)
720{
721 clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *);
722
723 u32 index;
724 s = format (s, "%16s %13s %8s", "virtual-addr", "physical-addr", "size");
725 vec_foreach_index (index, pm->lookup_table)
726 {
727 uword *lookup_val, pa, va;
728 lookup_val = vec_elt_at_index (pm->lookup_table, index);
Kingwel Xiedbc34b82018-11-11 22:55:58 -0500729 va =
730 pointer_to_uword (pm->base) +
731 ((uword) index << pm->lookup_log2_page_sz);
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100732 pa = va - *lookup_val;
733 s =
734 format (s, "\n %16p %13p %8U", uword_to_pointer (va, u64),
735 uword_to_pointer (pa, u64), format_log2_page_size,
736 pm->lookup_log2_page_sz);
737 }
738 return s;
739}
740
Damjan Marion68b4da62018-09-30 18:26:20 +0200741/*
742 * fd.io coding-style-patch-verification: ON
743 *
744 * Local Variables:
745 * eval: (c-set-style "gnu")
746 * End:
747 */