blob: 41309dd1d99e9f93130a2d850a28654e323e83c6 [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>
21#include <linux/mempolicy.h>
22#include <linux/memfd.h>
23
24#include <vppinfra/format.h>
25#include <vppinfra/linux/syscall.h>
26#include <vppinfra/linux/sysfs.h>
27#include <vppinfra/mem.h>
28#include <vppinfra/hash.h>
29#include <vppinfra/pmalloc.h>
30
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 Marion68b4da62018-09-30 18:26:20 +020049static inline int
50pmalloc_validate_numa_node (u32 * numa_node)
51{
52 if (*numa_node == CLIB_PMALLOC_NUMA_LOCAL)
53 {
54 u32 cpu;
55 if (getcpu (&cpu, numa_node, 0) != 0)
56 return 1;
57 }
58 return 0;
59}
60
61int
62clib_pmalloc_init (clib_pmalloc_main_t * pm, uword size)
63{
Damjan Marion68b4da62018-09-30 18:26:20 +020064 uword off, pagesize;
Damjan Marionc04e2b02018-10-25 15:56:04 +020065 u64 *pt = 0;
Damjan Marion68b4da62018-09-30 18:26:20 +020066
67 ASSERT (pm->error == 0);
68
Damjan Marion9787f5f2018-10-24 12:56:32 +020069 pagesize = clib_mem_get_default_hugepage_size ();
Damjan Marion567e61d2018-10-24 17:08:26 +020070 pm->def_log2_page_sz = min_log2 (pagesize);
71 pm->sys_log2_page_sz = min_log2 (sysconf (_SC_PAGESIZE));
72 pm->lookup_log2_page_sz = pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +020073
Damjan Marionc04e2b02018-10-25 15:56:04 +020074 /* check if pagemap is accessible */
75 pt = clib_mem_vm_get_paddr (&pt, pm->sys_log2_page_sz, 1);
76 if (pt == 0 || pt[0] == 0)
77 pm->flags |= CLIB_PMALLOC_F_NO_PAGEMAP;
78
Damjan Marion68b4da62018-09-30 18:26:20 +020079 size = size ? size : ((u64) DEFAULT_RESERVED_MB) << 20;
80 size = round_pow2 (size, pagesize);
81
Damjan Marion567e61d2018-10-24 17:08:26 +020082 pm->max_pages = size >> pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +020083
84 /* reserve VA space for future growth */
85 pm->base = mmap (0, size + pagesize, PROT_NONE,
86 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
87
88 if (pm->base == MAP_FAILED)
89 {
90 pm->error = clib_error_return_unix (0, "failed to reserve %u pages");
91 return -1;
92 }
93
94 off = round_pow2 (pointer_to_uword (pm->base), pagesize) -
95 pointer_to_uword (pm->base);
96
97 /* trim start and end of reservation to be page aligned */
98 if (off)
99 {
100 munmap (pm->base, off);
101 pm->base += off;
102 }
103
Damjan Marion878b65a2018-10-26 10:29:35 +0200104 munmap (pm->base + ((uword) pm->max_pages * pagesize), pagesize - off);
Damjan Marion68b4da62018-09-30 18:26:20 +0200105 return 0;
106}
107
108static inline void *
109alloc_chunk_from_page (clib_pmalloc_main_t * pm, clib_pmalloc_page_t * pp,
110 u32 n_blocks, u32 block_align, u32 numa_node)
111{
Damjan Marion567e61d2018-10-24 17:08:26 +0200112 clib_pmalloc_chunk_t *c = 0;
113 clib_pmalloc_arena_t *a;
Damjan Marion68b4da62018-09-30 18:26:20 +0200114 void *va;
115 u32 off;
116 u32 alloc_chunk_index;
117
Damjan Marion567e61d2018-10-24 17:08:26 +0200118 a = pool_elt_at_index (pm->arenas, pp->arena_index);
119
Damjan Marion68b4da62018-09-30 18:26:20 +0200120 if (pp->chunks == 0)
121 {
Damjan Marion567e61d2018-10-24 17:08:26 +0200122 u32 i, start = 0, prev = ~0;
123
124 for (i = 0; i < a->subpages_per_page; i++)
125 {
126 pool_get (pp->chunks, c);
127 c->start = start;
128 c->prev = prev;
129 c->size = pp->n_free_blocks / a->subpages_per_page;
130 start += c->size;
131 if (prev == ~0)
132 pp->first_chunk_index = c - pp->chunks;
133 else
134 pp->chunks[prev].next = c - pp->chunks;
135 prev = c - pp->chunks;
136 }
137 c->next = ~0;
138 pp->n_free_chunks = a->subpages_per_page;
Damjan Marion68b4da62018-09-30 18:26:20 +0200139 }
140
141 alloc_chunk_index = pp->first_chunk_index;
142
143next_chunk:
144 c = pool_elt_at_index (pp->chunks, alloc_chunk_index);
145 off = (block_align - (c->start & (block_align - 1))) & (block_align - 1);
146
147 if (c->used || n_blocks + off > c->size)
148 {
149 if (c->next == ~0)
150 return 0;
151 alloc_chunk_index = c->next;
152 goto next_chunk;
153 }
154
155 /* if alignment is needed create new empty chunk */
156 if (off)
157 {
158 u32 offset_chunk_index;
159 clib_pmalloc_chunk_t *co;
160 pool_get (pp->chunks, c);
161 pp->n_free_chunks++;
162 offset_chunk_index = alloc_chunk_index;
163 alloc_chunk_index = c - pp->chunks;
164
165 co = pool_elt_at_index (pp->chunks, offset_chunk_index);
166 c->size = co->size - off;
167 c->next = co->next;
168 c->start = co->start + off;
169 c->prev = offset_chunk_index;
170 co->size = off;
171 co->next = alloc_chunk_index;
172 }
173
174 c->used = 1;
175 if (c->size > n_blocks)
176 {
177 u32 tail_chunk_index;
178 clib_pmalloc_chunk_t *ct;
179 pool_get (pp->chunks, ct);
180 pp->n_free_chunks++;
181 tail_chunk_index = ct - pp->chunks;
182 c = pool_elt_at_index (pp->chunks, alloc_chunk_index);
183 ct->size = c->size - n_blocks;
184 ct->next = c->next;
185 ct->prev = alloc_chunk_index;
186 ct->start = c->start + n_blocks;
187
188 c->size = n_blocks;
189 c->next = tail_chunk_index;
190 if (ct->next != ~0)
191 pool_elt_at_index (pp->chunks, ct->next)->prev = tail_chunk_index;
192 }
193 else if (c->next != ~0)
194 pool_elt_at_index (pp->chunks, c->next)->prev = alloc_chunk_index;
195
196 c = get_chunk (pp, alloc_chunk_index);
Damjan Marion567e61d2018-10-24 17:08:26 +0200197 va = pm->base + ((pp - pm->pages) << pm->def_log2_page_sz) +
Damjan Marion68b4da62018-09-30 18:26:20 +0200198 (c->start << PMALLOC_LOG2_BLOCK_SZ);
199 hash_set (pm->chunk_index_by_va, pointer_to_uword (va), alloc_chunk_index);
200 pp->n_free_blocks -= n_blocks;
201 pp->n_free_chunks--;
202 return va;
203}
204
Damjan Marion567e61d2018-10-24 17:08:26 +0200205static void
206pmalloc_update_lookup_table (clib_pmalloc_main_t * pm, u32 first, u32 count)
207{
208 uword seek, va, pa, p;
209 int fd;
210 u32 elts_per_page = 1U << (pm->def_log2_page_sz - pm->lookup_log2_page_sz);
211
212 vec_validate_aligned (pm->lookup_table, vec_len (pm->pages) *
213 elts_per_page - 1, CLIB_CACHE_LINE_BYTES);
214
Dave Barach96e2d442018-11-14 11:42:03 -0500215 p = (uword) first *elts_per_page;
Damjan Marionc04e2b02018-10-25 15:56:04 +0200216 if (pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP)
217 {
Damjan Marion878b65a2018-10-26 10:29:35 +0200218 while (p < (uword) elts_per_page * count)
Damjan Marionc04e2b02018-10-25 15:56:04 +0200219 {
220 pm->lookup_table[p] = pointer_to_uword (pm->base) +
221 (p << pm->lookup_log2_page_sz);
222 p++;
223 }
224 return;
225 }
226
227 fd = open ((char *) "/proc/self/pagemap", O_RDONLY);
Damjan Marion878b65a2018-10-26 10:29:35 +0200228 while (p < (uword) elts_per_page * count)
Damjan Marion567e61d2018-10-24 17:08:26 +0200229 {
230 va = pointer_to_uword (pm->base) + (p << pm->lookup_log2_page_sz);
Damjan Marion878b65a2018-10-26 10:29:35 +0200231 pa = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +0200232 seek = (va >> pm->sys_log2_page_sz) * sizeof (pa);
233 if (fd != -1 && lseek (fd, seek, SEEK_SET) == seek &&
234 read (fd, &pa, sizeof (pa)) == (sizeof (pa)) &&
235 pa & (1ULL << 63) /* page present bit */ )
236 {
237 pa = (pa & pow2_mask (55)) << pm->sys_log2_page_sz;
238 }
239 pm->lookup_table[p] = va - pa;
240 p++;
241 }
242
243 if (fd != -1)
244 close (fd);
245}
246
Damjan Marion68b4da62018-09-30 18:26:20 +0200247static inline clib_pmalloc_page_t *
248pmalloc_map_pages (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
249 u32 numa_node, u32 n_pages)
250{
251 clib_pmalloc_page_t *pp = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +0200252 int status, rv, i, mmap_flags;
Damjan Marion68b4da62018-09-30 18:26:20 +0200253 void *va;
254 int old_mpol = -1;
255 long unsigned int mask[16] = { 0 };
256 long unsigned int old_mask[16] = { 0 };
Damjan Marion567e61d2018-10-24 17:08:26 +0200257 uword size = (uword) n_pages << pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +0200258
259 clib_error_free (pm->error);
260
261 if (pm->max_pages <= vec_len (pm->pages))
262 {
263 pm->error = clib_error_return (0, "maximum number of pages reached");
264 return 0;
265 }
266
Damjan Marion567e61d2018-10-24 17:08:26 +0200267 if (a->log2_subpage_sz != pm->sys_log2_page_sz)
268 {
269 pm->error = clib_sysfs_prealloc_hugepages (numa_node,
270 a->log2_subpage_sz, n_pages);
Damjan Marion68b4da62018-09-30 18:26:20 +0200271
Damjan Marion567e61d2018-10-24 17:08:26 +0200272 if (pm->error)
273 return 0;
274 }
Damjan Marion68b4da62018-09-30 18:26:20 +0200275
276 rv = get_mempolicy (&old_mpol, old_mask, sizeof (old_mask) * 8 + 1, 0, 0);
277 /* failure to get mempolicy means we can only proceed with numa 0 maps */
278 if (rv == -1 && numa_node != 0)
279 {
280 pm->error = clib_error_return_unix (0, "failed to get mempolicy");
281 return 0;
282 }
283
284 mask[0] = 1 << numa_node;
285 rv = set_mempolicy (MPOL_BIND, mask, sizeof (mask) * 8 + 1);
286 if (rv == -1 && numa_node != 0)
287 {
288 pm->error = clib_error_return_unix (0, "failed to set mempolicy for "
289 "numa node %u", numa_node);
290 return 0;
291 }
292
Damjan Marion54e8e392018-11-07 17:55:26 +0100293 mmap_flags = MAP_FIXED;
Damjan Marionc04e2b02018-10-25 15:56:04 +0200294
295 if ((pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP) == 0)
296 mmap_flags |= MAP_LOCKED;
Damjan Marion567e61d2018-10-24 17:08:26 +0200297
Damjan Marion68b4da62018-09-30 18:26:20 +0200298 if (a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM)
299 {
300 mmap_flags |= MAP_SHARED;
Damjan Marion8ebd7922018-11-28 10:46:03 +0100301 if (a->log2_subpage_sz != pm->sys_log2_page_sz)
Damjan Marion567e61d2018-10-24 17:08:26 +0200302 pm->error = clib_mem_create_hugetlb_fd ((char *) a->name, &a->fd);
303 else
304 pm->error = clib_mem_create_fd ((char *) a->name, &a->fd);
Damjan Marion68b4da62018-09-30 18:26:20 +0200305 if (a->fd == -1)
306 goto error;
Damjan Marion54e8e392018-11-07 17:55:26 +0100307 if ((ftruncate (a->fd, size)) == -1)
308 goto error;
Damjan Marion68b4da62018-09-30 18:26:20 +0200309 }
310 else
311 {
Damjan Marion8ebd7922018-11-28 10:46:03 +0100312 if (a->log2_subpage_sz != pm->sys_log2_page_sz)
313 mmap_flags |= MAP_HUGETLB;
314
Damjan Marion54e8e392018-11-07 17:55:26 +0100315 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
Damjan Marion68b4da62018-09-30 18:26:20 +0200316 a->fd = -1;
317 }
318
Damjan Marion567e61d2018-10-24 17:08:26 +0200319 va = pm->base + (((uword) vec_len (pm->pages)) << pm->def_log2_page_sz);
320 if (mmap (va, size, PROT_READ | PROT_WRITE, mmap_flags, a->fd, 0) ==
321 MAP_FAILED)
Damjan Marion68b4da62018-09-30 18:26:20 +0200322 {
323 pm->error = clib_error_return_unix (0, "failed to mmap %u pages at %p "
324 "fd %d numa %d flags 0x%x", n_pages,
325 va, a->fd, numa_node, mmap_flags);
326 goto error;
327 }
328
Damjan Marion567e61d2018-10-24 17:08:26 +0200329 clib_memset (va, 0, size);
330
Damjan Marion68b4da62018-09-30 18:26:20 +0200331 rv = set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1);
332 if (rv == -1 && numa_node != 0)
333 {
334 pm->error = clib_error_return_unix (0, "failed to restore mempolicy");
335 goto error;
336 }
337
338 /* we tolerate move_pages failure only if request os for numa node 0
339 to support non-numa kernels */
340 rv = move_pages (0, 1, &va, 0, &status, 0);
341 if ((rv == 0 && status != numa_node) || (rv != 0 && numa_node != 0))
342 {
343 pm->error = rv == -1 ?
344 clib_error_return_unix (0, "page allocated on wrong node, numa node "
345 "%u status %d", numa_node, status) :
346 clib_error_return (0, "page allocated on wrong node, numa node "
347 "%u status %d", numa_node, status);
348
349 /* unmap & reesrve */
Damjan Marion567e61d2018-10-24 17:08:26 +0200350 munmap (va, size);
351 mmap (va, size, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
352 -1, 0);
Damjan Marion68b4da62018-09-30 18:26:20 +0200353 goto error;
354 }
355
Damjan Marion68b4da62018-09-30 18:26:20 +0200356 for (i = 0; i < n_pages; i++)
357 {
Damjan Marion68b4da62018-09-30 18:26:20 +0200358 vec_add2 (pm->pages, pp, 1);
Damjan Marion567e61d2018-10-24 17:08:26 +0200359 pp->n_free_blocks = 1 << (pm->def_log2_page_sz - PMALLOC_LOG2_BLOCK_SZ);
Damjan Marion68b4da62018-09-30 18:26:20 +0200360 pp->index = pp - pm->pages;
361 pp->arena_index = a->index;
Damjan Marion68b4da62018-09-30 18:26:20 +0200362 vec_add1 (a->page_indices, pp->index);
363 a->n_pages++;
Damjan Marion68b4da62018-09-30 18:26:20 +0200364 }
365
Damjan Marion567e61d2018-10-24 17:08:26 +0200366
367 /* if new arena is using smaller page size, we need to rebuild whole
368 lookup table */
369 if (a->log2_subpage_sz < pm->lookup_log2_page_sz)
370 {
371 pm->lookup_log2_page_sz = a->log2_subpage_sz;
372 pmalloc_update_lookup_table (pm, vec_len (pm->pages) - n_pages,
373 n_pages);
374 }
375 else
376 pmalloc_update_lookup_table (pm, 0, vec_len (pm->pages));
Damjan Marion68b4da62018-09-30 18:26:20 +0200377
378 /* return pointer to 1st page */
379 return pp - (n_pages - 1);
380
381error:
382 if (a->fd != -1)
383 close (a->fd);
384 return 0;
385}
386
387void *
388clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name,
Damjan Marion567e61d2018-10-24 17:08:26 +0200389 uword size, u32 log2_page_sz, u32 numa_node)
Damjan Marion68b4da62018-09-30 18:26:20 +0200390{
391 clib_pmalloc_arena_t *a;
392 clib_pmalloc_page_t *pp;
Damjan Marion567e61d2018-10-24 17:08:26 +0200393 u32 n_pages;
394
395 clib_error_free (pm->error);
396
397 if (log2_page_sz == 0)
398 log2_page_sz = pm->def_log2_page_sz;
399 else if (log2_page_sz != pm->def_log2_page_sz &&
400 log2_page_sz != pm->sys_log2_page_sz)
401 {
402 pm->error = clib_error_create ("unsupported page size (%uKB)",
403 1 << (log2_page_sz - 10));
404 return 0;
405 }
406
407 n_pages = pmalloc_size2pages (size, pm->def_log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200408
409 if (n_pages + vec_len (pm->pages) > pm->max_pages)
410 return 0;
411
412 if (pmalloc_validate_numa_node (&numa_node))
413 return 0;
414
415 pool_get (pm->arenas, a);
416 a->index = a - pm->arenas;
417 a->name = format (0, "%s%c", name, 0);
418 a->numa_node = numa_node;
419 a->flags = CLIB_PMALLOC_ARENA_F_SHARED_MEM;
Damjan Marion567e61d2018-10-24 17:08:26 +0200420 a->log2_subpage_sz = log2_page_sz;
421 a->subpages_per_page = 1U << (pm->def_log2_page_sz - log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200422
423 if ((pp = pmalloc_map_pages (pm, a, numa_node, n_pages)) == 0)
424 {
425 vec_free (a->name);
426 memset (a, 0, sizeof (*a));
427 pool_put (pm->arenas, a);
428 return 0;
429 }
430
Kingwel Xie5efaeee2018-11-10 02:56:00 -0500431 return pm->base + ((uword) pp->index << pm->def_log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200432}
433
434static inline void *
435clib_pmalloc_alloc_inline (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
436 uword size, uword align, u32 numa_node)
437{
438 clib_pmalloc_page_t *pp;
439 u32 n_blocks, block_align, *page_index;
440
441 ASSERT (is_pow2 (align));
442
443 if (pmalloc_validate_numa_node (&numa_node))
444 return 0;
445
446 if (a == 0)
447 {
Damjan Marion567e61d2018-10-24 17:08:26 +0200448 if (size > 1ULL << pm->def_log2_page_sz)
449 return 0;
450
Damjan Marion68b4da62018-09-30 18:26:20 +0200451 vec_validate_init_empty (pm->default_arena_for_numa_node,
452 numa_node, ~0);
453 if (pm->default_arena_for_numa_node[numa_node] == ~0)
454 {
455 pool_get (pm->arenas, a);
456 pm->default_arena_for_numa_node[numa_node] = a - pm->arenas;
457 a->name = format (0, "default-numa-%u%c", numa_node, 0);
458 a->numa_node = numa_node;
Damjan Marion567e61d2018-10-24 17:08:26 +0200459 a->log2_subpage_sz = pm->def_log2_page_sz;
460 a->subpages_per_page = 1;
Damjan Marion68b4da62018-09-30 18:26:20 +0200461 }
462 else
463 a = pool_elt_at_index (pm->arenas,
464 pm->default_arena_for_numa_node[numa_node]);
465 }
Damjan Marion567e61d2018-10-24 17:08:26 +0200466 else if (size > 1ULL << a->log2_subpage_sz)
467 return 0;
Damjan Marion68b4da62018-09-30 18:26:20 +0200468
469 n_blocks = round_pow2 (size, PMALLOC_BLOCK_SZ) / PMALLOC_BLOCK_SZ;
470 block_align = align >> PMALLOC_LOG2_BLOCK_SZ;
471
472 vec_foreach (page_index, a->page_indices)
473 {
474 pp = vec_elt_at_index (pm->pages, *page_index);
475 void *rv = alloc_chunk_from_page (pm, pp, n_blocks, block_align,
476 numa_node);
477
478 if (rv)
479 return rv;
480 }
481
482 if ((a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM) == 0 &&
483 (pp = pmalloc_map_pages (pm, a, numa_node, 1)))
484 return alloc_chunk_from_page (pm, pp, n_blocks, block_align, numa_node);
485
486 return 0;
487}
488
489void *
490clib_pmalloc_alloc_aligned_on_numa (clib_pmalloc_main_t * pm, uword size,
491 uword align, u32 numa_node)
492{
493 return clib_pmalloc_alloc_inline (pm, 0, size, align, numa_node);
494}
495
496void *
497clib_pmalloc_alloc_aligned (clib_pmalloc_main_t * pm, uword size, uword align)
498{
499 return clib_pmalloc_alloc_inline (pm, 0, size, align,
500 CLIB_PMALLOC_NUMA_LOCAL);
501}
502
503void *
504clib_pmalloc_alloc_from_arena (clib_pmalloc_main_t * pm, void *arena_va,
505 uword size, uword align)
506{
507 clib_pmalloc_arena_t *a = clib_pmalloc_get_arena (pm, arena_va);
508 return clib_pmalloc_alloc_inline (pm, a, size, align, 0);
509}
510
Damjan Marion567e61d2018-10-24 17:08:26 +0200511static inline int
512pmalloc_chunks_mergeable (clib_pmalloc_arena_t * a, clib_pmalloc_page_t * pp,
513 u32 ci1, u32 ci2)
514{
515 clib_pmalloc_chunk_t *c1, *c2;
516
517 if (ci1 == ~0 || ci2 == ~0)
518 return 0;
519
520 c1 = get_chunk (pp, ci1);
521 c2 = get_chunk (pp, ci2);
522
523 if (c1->used || c2->used)
524 return 0;
525
526 if (c1->start >> (a->log2_subpage_sz - PMALLOC_LOG2_BLOCK_SZ) !=
527 c2->start >> (a->log2_subpage_sz - PMALLOC_LOG2_BLOCK_SZ))
528 return 0;
529
530 return 1;
531}
532
Damjan Marion68b4da62018-09-30 18:26:20 +0200533void
534clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va)
535{
536 clib_pmalloc_page_t *pp;
537 clib_pmalloc_chunk_t *c;
Damjan Marion567e61d2018-10-24 17:08:26 +0200538 clib_pmalloc_arena_t *a;
Damjan Marion68b4da62018-09-30 18:26:20 +0200539 uword *p;
540 u32 chunk_index, page_index;
541
542 p = hash_get (pm->chunk_index_by_va, pointer_to_uword (va));
543
544 if (p == 0)
545 os_panic ();
546
547 chunk_index = p[0];
548 page_index = clib_pmalloc_get_page_index (pm, va);
549 hash_unset (pm->chunk_index_by_va, pointer_to_uword (va));
550
551 pp = vec_elt_at_index (pm->pages, page_index);
552 c = pool_elt_at_index (pp->chunks, chunk_index);
Damjan Marion567e61d2018-10-24 17:08:26 +0200553 a = pool_elt_at_index (pm->arenas, pp->arena_index);
Damjan Marion68b4da62018-09-30 18:26:20 +0200554 c->used = 0;
555 pp->n_free_blocks += c->size;
556 pp->n_free_chunks++;
557
558 /* merge with next if free */
Damjan Marion567e61d2018-10-24 17:08:26 +0200559 if (pmalloc_chunks_mergeable (a, pp, chunk_index, c->next))
Damjan Marion68b4da62018-09-30 18:26:20 +0200560 {
561 clib_pmalloc_chunk_t *next = get_chunk (pp, c->next);
562 c->size += next->size;
563 c->next = next->next;
564 if (next->next != ~0)
565 get_chunk (pp, next->next)->prev = chunk_index;
566 memset (next, 0, sizeof (*next));
567 pool_put (pp->chunks, next);
568 pp->n_free_chunks--;
569 }
570
571 /* merge with prev if free */
Damjan Marion567e61d2018-10-24 17:08:26 +0200572 if (pmalloc_chunks_mergeable (a, pp, c->prev, chunk_index))
Damjan Marion68b4da62018-09-30 18:26:20 +0200573 {
574 clib_pmalloc_chunk_t *prev = get_chunk (pp, c->prev);
575 prev->size += c->size;
576 prev->next = c->next;
577 if (c->next != ~0)
578 get_chunk (pp, c->next)->prev = c->prev;
579 memset (c, 0, sizeof (*c));
580 pool_put (pp->chunks, c);
581 pp->n_free_chunks--;
582 }
583}
584
585static u8 *
Damjan Marion567e61d2018-10-24 17:08:26 +0200586format_log2_page_size (u8 * s, va_list * va)
587{
588 u32 log2_page_sz = va_arg (*va, u32);
589
590 if (log2_page_sz >= 30)
591 return format (s, "%uGB", 1 << (log2_page_sz - 30));
592
593 if (log2_page_sz >= 20)
594 return format (s, "%uMB", 1 << (log2_page_sz - 20));
595
596 if (log2_page_sz >= 10)
597 return format (s, "%uKB", 1 << (log2_page_sz - 10));
598
599 return format (s, "%uB", 1 << log2_page_sz);
600}
601
602
603static u8 *
Damjan Marion68b4da62018-09-30 18:26:20 +0200604format_pmalloc_page (u8 * s, va_list * va)
605{
606 clib_pmalloc_page_t *pp = va_arg (*va, clib_pmalloc_page_t *);
607 int verbose = va_arg (*va, int);
608 u32 indent = format_get_indent (s);
609
Damjan Marion68b4da62018-09-30 18:26:20 +0200610 if (pp->chunks == 0)
611 return s;
612
613 s = format (s, "free %u chunks %u free-chunks %d ",
614 (pp->n_free_blocks) << PMALLOC_LOG2_BLOCK_SZ,
615 pool_elts (pp->chunks), pp->n_free_chunks);
616
617 if (verbose >= 2)
618 {
619 clib_pmalloc_chunk_t *c;
620 c = pool_elt_at_index (pp->chunks, pp->first_chunk_index);
621 s = format (s, "\n%U%12s%12s%8s%8s%8s%8s",
622 format_white_space, indent + 2,
623 "chunk offset", "size", "used", "index", "prev", "next");
624 while (1)
625 {
626 s = format (s, "\n%U%12u%12u%8s%8d%8d%8d",
627 format_white_space, indent + 2,
628 c->start << PMALLOC_LOG2_BLOCK_SZ,
629 c->size << PMALLOC_LOG2_BLOCK_SZ,
630 c->used ? "yes" : "no",
631 c - pp->chunks, c->prev, c->next);
632 if (c->next == ~0)
633 break;
634 c = pool_elt_at_index (pp->chunks, c->next);
635 }
636 }
637 return s;
638}
639
640u8 *
641format_pmalloc (u8 * s, va_list * va)
642{
643 clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *);
644 int verbose = va_arg (*va, int);
645 u32 indent = format_get_indent (s);
646
647 clib_pmalloc_page_t *pp;
648 clib_pmalloc_arena_t *a;
649
Damjan Marion567e61d2018-10-24 17:08:26 +0200650 s = format (s, "used-pages %u reserved-pages %u default-page-size %U "
Damjan Marionc04e2b02018-10-25 15:56:04 +0200651 "lookup-page-size %U%s", vec_len (pm->pages), pm->max_pages,
Damjan Marion567e61d2018-10-24 17:08:26 +0200652 format_log2_page_size, pm->def_log2_page_sz,
Damjan Marionc04e2b02018-10-25 15:56:04 +0200653 format_log2_page_size, pm->lookup_log2_page_sz,
654 pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP ? " no-pagemap" : "");
Damjan Marion567e61d2018-10-24 17:08:26 +0200655
Damjan Marion68b4da62018-09-30 18:26:20 +0200656
657 if (verbose >= 2)
658 s = format (s, " va-start %p", pm->base);
659
660 if (pm->error)
661 s = format (s, "\n%Ulast-error: %U", format_white_space, indent + 2,
662 format_clib_error, pm->error);
663
664
665 /* *INDENT-OFF* */
666 pool_foreach (a, pm->arenas,
667 {
668 u32 *page_index;
Damjan Marion567e61d2018-10-24 17:08:26 +0200669 s = format (s, "\n%Uarena '%s' pages %u subpage-size %U numa-node %u",
670 format_white_space, indent + 2, a->name,
671 vec_len (a->page_indices), format_log2_page_size,
672 a->log2_subpage_sz, a->numa_node);
Damjan Marion68b4da62018-09-30 18:26:20 +0200673 if (a->fd != -1)
674 s = format (s, " shared fd %d", a->fd);
675 if (verbose >= 1)
676 vec_foreach (page_index, a->page_indices)
677 {
678 pp = vec_elt_at_index (pm->pages, *page_index);
679 s = format (s, "\n%U%U", format_white_space, indent + 4,
680 format_pmalloc_page, pp, verbose);
681 }
682 });
683 /* *INDENT-ON* */
684
685 return s;
686}
687
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100688u8 *
689format_pmalloc_map (u8 * s, va_list * va)
690{
691 clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *);
692
693 u32 index;
694 s = format (s, "%16s %13s %8s", "virtual-addr", "physical-addr", "size");
695 vec_foreach_index (index, pm->lookup_table)
696 {
697 uword *lookup_val, pa, va;
698 lookup_val = vec_elt_at_index (pm->lookup_table, index);
Kingwel Xiedbc34b82018-11-11 22:55:58 -0500699 va =
700 pointer_to_uword (pm->base) +
701 ((uword) index << pm->lookup_log2_page_sz);
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100702 pa = va - *lookup_val;
703 s =
704 format (s, "\n %16p %13p %8U", uword_to_pointer (va, u64),
705 uword_to_pointer (pa, u64), format_log2_page_size,
706 pm->lookup_log2_page_sz);
707 }
708 return s;
709}
710
Damjan Marion68b4da62018-09-30 18:26:20 +0200711/*
712 * fd.io coding-style-patch-verification: ON
713 *
714 * Local Variables:
715 * eval: (c-set-style "gnu")
716 * End:
717 */