blob: 5662a36939ec911e2a79e73996936b316f7984e7 [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
Damjan Marion78c0ff72019-01-23 12:50:24 +0100141 if (pp->n_free_blocks < n_blocks)
142 return 0;
143
Damjan Marion68b4da62018-09-30 18:26:20 +0200144 alloc_chunk_index = pp->first_chunk_index;
145
146next_chunk:
147 c = pool_elt_at_index (pp->chunks, alloc_chunk_index);
148 off = (block_align - (c->start & (block_align - 1))) & (block_align - 1);
149
150 if (c->used || n_blocks + off > c->size)
151 {
152 if (c->next == ~0)
153 return 0;
154 alloc_chunk_index = c->next;
155 goto next_chunk;
156 }
157
158 /* if alignment is needed create new empty chunk */
159 if (off)
160 {
161 u32 offset_chunk_index;
162 clib_pmalloc_chunk_t *co;
163 pool_get (pp->chunks, c);
164 pp->n_free_chunks++;
165 offset_chunk_index = alloc_chunk_index;
166 alloc_chunk_index = c - pp->chunks;
167
168 co = pool_elt_at_index (pp->chunks, offset_chunk_index);
169 c->size = co->size - off;
170 c->next = co->next;
171 c->start = co->start + off;
172 c->prev = offset_chunk_index;
173 co->size = off;
174 co->next = alloc_chunk_index;
175 }
176
177 c->used = 1;
178 if (c->size > n_blocks)
179 {
180 u32 tail_chunk_index;
181 clib_pmalloc_chunk_t *ct;
182 pool_get (pp->chunks, ct);
183 pp->n_free_chunks++;
184 tail_chunk_index = ct - pp->chunks;
185 c = pool_elt_at_index (pp->chunks, alloc_chunk_index);
186 ct->size = c->size - n_blocks;
187 ct->next = c->next;
188 ct->prev = alloc_chunk_index;
189 ct->start = c->start + n_blocks;
190
191 c->size = n_blocks;
192 c->next = tail_chunk_index;
193 if (ct->next != ~0)
194 pool_elt_at_index (pp->chunks, ct->next)->prev = tail_chunk_index;
195 }
196 else if (c->next != ~0)
197 pool_elt_at_index (pp->chunks, c->next)->prev = alloc_chunk_index;
198
199 c = get_chunk (pp, alloc_chunk_index);
Damjan Marion567e61d2018-10-24 17:08:26 +0200200 va = pm->base + ((pp - pm->pages) << pm->def_log2_page_sz) +
Damjan Marion68b4da62018-09-30 18:26:20 +0200201 (c->start << PMALLOC_LOG2_BLOCK_SZ);
202 hash_set (pm->chunk_index_by_va, pointer_to_uword (va), alloc_chunk_index);
203 pp->n_free_blocks -= n_blocks;
204 pp->n_free_chunks--;
205 return va;
206}
207
Damjan Marion567e61d2018-10-24 17:08:26 +0200208static void
209pmalloc_update_lookup_table (clib_pmalloc_main_t * pm, u32 first, u32 count)
210{
211 uword seek, va, pa, p;
212 int fd;
213 u32 elts_per_page = 1U << (pm->def_log2_page_sz - pm->lookup_log2_page_sz);
214
215 vec_validate_aligned (pm->lookup_table, vec_len (pm->pages) *
216 elts_per_page - 1, CLIB_CACHE_LINE_BYTES);
217
Dave Barach96e2d442018-11-14 11:42:03 -0500218 p = (uword) first *elts_per_page;
Damjan Marionc04e2b02018-10-25 15:56:04 +0200219 if (pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP)
220 {
Damjan Marion878b65a2018-10-26 10:29:35 +0200221 while (p < (uword) elts_per_page * count)
Damjan Marionc04e2b02018-10-25 15:56:04 +0200222 {
223 pm->lookup_table[p] = pointer_to_uword (pm->base) +
224 (p << pm->lookup_log2_page_sz);
225 p++;
226 }
227 return;
228 }
229
230 fd = open ((char *) "/proc/self/pagemap", O_RDONLY);
Damjan Marion878b65a2018-10-26 10:29:35 +0200231 while (p < (uword) elts_per_page * count)
Damjan Marion567e61d2018-10-24 17:08:26 +0200232 {
233 va = pointer_to_uword (pm->base) + (p << pm->lookup_log2_page_sz);
Damjan Marion878b65a2018-10-26 10:29:35 +0200234 pa = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +0200235 seek = (va >> pm->sys_log2_page_sz) * sizeof (pa);
236 if (fd != -1 && lseek (fd, seek, SEEK_SET) == seek &&
237 read (fd, &pa, sizeof (pa)) == (sizeof (pa)) &&
238 pa & (1ULL << 63) /* page present bit */ )
239 {
240 pa = (pa & pow2_mask (55)) << pm->sys_log2_page_sz;
241 }
242 pm->lookup_table[p] = va - pa;
243 p++;
244 }
245
246 if (fd != -1)
247 close (fd);
248}
249
Damjan Marion68b4da62018-09-30 18:26:20 +0200250static inline clib_pmalloc_page_t *
251pmalloc_map_pages (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
252 u32 numa_node, u32 n_pages)
253{
254 clib_pmalloc_page_t *pp = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +0200255 int status, rv, i, mmap_flags;
Damjan Marion68b4da62018-09-30 18:26:20 +0200256 void *va;
257 int old_mpol = -1;
258 long unsigned int mask[16] = { 0 };
259 long unsigned int old_mask[16] = { 0 };
Damjan Marion567e61d2018-10-24 17:08:26 +0200260 uword size = (uword) n_pages << pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +0200261
262 clib_error_free (pm->error);
263
264 if (pm->max_pages <= vec_len (pm->pages))
265 {
266 pm->error = clib_error_return (0, "maximum number of pages reached");
267 return 0;
268 }
269
Damjan Marion567e61d2018-10-24 17:08:26 +0200270 if (a->log2_subpage_sz != pm->sys_log2_page_sz)
271 {
272 pm->error = clib_sysfs_prealloc_hugepages (numa_node,
273 a->log2_subpage_sz, n_pages);
Damjan Marion68b4da62018-09-30 18:26:20 +0200274
Damjan Marion567e61d2018-10-24 17:08:26 +0200275 if (pm->error)
276 return 0;
277 }
Damjan Marion68b4da62018-09-30 18:26:20 +0200278
279 rv = get_mempolicy (&old_mpol, old_mask, sizeof (old_mask) * 8 + 1, 0, 0);
280 /* failure to get mempolicy means we can only proceed with numa 0 maps */
281 if (rv == -1 && numa_node != 0)
282 {
283 pm->error = clib_error_return_unix (0, "failed to get mempolicy");
284 return 0;
285 }
286
287 mask[0] = 1 << numa_node;
288 rv = set_mempolicy (MPOL_BIND, mask, sizeof (mask) * 8 + 1);
289 if (rv == -1 && numa_node != 0)
290 {
291 pm->error = clib_error_return_unix (0, "failed to set mempolicy for "
292 "numa node %u", numa_node);
293 return 0;
294 }
295
Damjan Marion54e8e392018-11-07 17:55:26 +0100296 mmap_flags = MAP_FIXED;
Damjan Marionc04e2b02018-10-25 15:56:04 +0200297
298 if ((pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP) == 0)
299 mmap_flags |= MAP_LOCKED;
Damjan Marion567e61d2018-10-24 17:08:26 +0200300
Damjan Marion68b4da62018-09-30 18:26:20 +0200301 if (a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM)
302 {
303 mmap_flags |= MAP_SHARED;
Damjan Marion8ebd7922018-11-28 10:46:03 +0100304 if (a->log2_subpage_sz != pm->sys_log2_page_sz)
Damjan Marion567e61d2018-10-24 17:08:26 +0200305 pm->error = clib_mem_create_hugetlb_fd ((char *) a->name, &a->fd);
306 else
307 pm->error = clib_mem_create_fd ((char *) a->name, &a->fd);
Damjan Marion68b4da62018-09-30 18:26:20 +0200308 if (a->fd == -1)
309 goto error;
Damjan Marion54e8e392018-11-07 17:55:26 +0100310 if ((ftruncate (a->fd, size)) == -1)
311 goto error;
Damjan Marion68b4da62018-09-30 18:26:20 +0200312 }
313 else
314 {
Damjan Marion8ebd7922018-11-28 10:46:03 +0100315 if (a->log2_subpage_sz != pm->sys_log2_page_sz)
316 mmap_flags |= MAP_HUGETLB;
317
Damjan Marion54e8e392018-11-07 17:55:26 +0100318 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
Damjan Marion68b4da62018-09-30 18:26:20 +0200319 a->fd = -1;
320 }
321
Damjan Marion567e61d2018-10-24 17:08:26 +0200322 va = pm->base + (((uword) vec_len (pm->pages)) << pm->def_log2_page_sz);
323 if (mmap (va, size, PROT_READ | PROT_WRITE, mmap_flags, a->fd, 0) ==
324 MAP_FAILED)
Damjan Marion68b4da62018-09-30 18:26:20 +0200325 {
326 pm->error = clib_error_return_unix (0, "failed to mmap %u pages at %p "
327 "fd %d numa %d flags 0x%x", n_pages,
328 va, a->fd, numa_node, mmap_flags);
329 goto error;
330 }
331
Damjan Marion567e61d2018-10-24 17:08:26 +0200332 clib_memset (va, 0, size);
333
Damjan Marion68b4da62018-09-30 18:26:20 +0200334 rv = set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1);
335 if (rv == -1 && numa_node != 0)
336 {
337 pm->error = clib_error_return_unix (0, "failed to restore mempolicy");
338 goto error;
339 }
340
341 /* we tolerate move_pages failure only if request os for numa node 0
342 to support non-numa kernels */
343 rv = move_pages (0, 1, &va, 0, &status, 0);
344 if ((rv == 0 && status != numa_node) || (rv != 0 && numa_node != 0))
345 {
346 pm->error = rv == -1 ?
347 clib_error_return_unix (0, "page allocated on wrong node, numa node "
348 "%u status %d", numa_node, status) :
349 clib_error_return (0, "page allocated on wrong node, numa node "
350 "%u status %d", numa_node, status);
351
352 /* unmap & reesrve */
Damjan Marion567e61d2018-10-24 17:08:26 +0200353 munmap (va, size);
354 mmap (va, size, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
355 -1, 0);
Damjan Marion68b4da62018-09-30 18:26:20 +0200356 goto error;
357 }
358
Damjan Marion68b4da62018-09-30 18:26:20 +0200359 for (i = 0; i < n_pages; i++)
360 {
Damjan Marion68b4da62018-09-30 18:26:20 +0200361 vec_add2 (pm->pages, pp, 1);
Damjan Marion567e61d2018-10-24 17:08:26 +0200362 pp->n_free_blocks = 1 << (pm->def_log2_page_sz - PMALLOC_LOG2_BLOCK_SZ);
Damjan Marion68b4da62018-09-30 18:26:20 +0200363 pp->index = pp - pm->pages;
364 pp->arena_index = a->index;
Damjan Marion68b4da62018-09-30 18:26:20 +0200365 vec_add1 (a->page_indices, pp->index);
366 a->n_pages++;
Damjan Marion68b4da62018-09-30 18:26:20 +0200367 }
368
Damjan Marion567e61d2018-10-24 17:08:26 +0200369
370 /* if new arena is using smaller page size, we need to rebuild whole
371 lookup table */
372 if (a->log2_subpage_sz < pm->lookup_log2_page_sz)
373 {
374 pm->lookup_log2_page_sz = a->log2_subpage_sz;
375 pmalloc_update_lookup_table (pm, vec_len (pm->pages) - n_pages,
376 n_pages);
377 }
378 else
379 pmalloc_update_lookup_table (pm, 0, vec_len (pm->pages));
Damjan Marion68b4da62018-09-30 18:26:20 +0200380
381 /* return pointer to 1st page */
382 return pp - (n_pages - 1);
383
384error:
385 if (a->fd != -1)
386 close (a->fd);
387 return 0;
388}
389
390void *
391clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name,
Damjan Marion567e61d2018-10-24 17:08:26 +0200392 uword size, u32 log2_page_sz, u32 numa_node)
Damjan Marion68b4da62018-09-30 18:26:20 +0200393{
394 clib_pmalloc_arena_t *a;
395 clib_pmalloc_page_t *pp;
Damjan Marion567e61d2018-10-24 17:08:26 +0200396 u32 n_pages;
397
398 clib_error_free (pm->error);
399
400 if (log2_page_sz == 0)
401 log2_page_sz = pm->def_log2_page_sz;
402 else if (log2_page_sz != pm->def_log2_page_sz &&
403 log2_page_sz != pm->sys_log2_page_sz)
404 {
405 pm->error = clib_error_create ("unsupported page size (%uKB)",
406 1 << (log2_page_sz - 10));
407 return 0;
408 }
409
410 n_pages = pmalloc_size2pages (size, pm->def_log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200411
412 if (n_pages + vec_len (pm->pages) > pm->max_pages)
413 return 0;
414
415 if (pmalloc_validate_numa_node (&numa_node))
416 return 0;
417
418 pool_get (pm->arenas, a);
419 a->index = a - pm->arenas;
420 a->name = format (0, "%s%c", name, 0);
421 a->numa_node = numa_node;
422 a->flags = CLIB_PMALLOC_ARENA_F_SHARED_MEM;
Damjan Marion567e61d2018-10-24 17:08:26 +0200423 a->log2_subpage_sz = log2_page_sz;
424 a->subpages_per_page = 1U << (pm->def_log2_page_sz - log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200425
426 if ((pp = pmalloc_map_pages (pm, a, numa_node, n_pages)) == 0)
427 {
428 vec_free (a->name);
429 memset (a, 0, sizeof (*a));
430 pool_put (pm->arenas, a);
431 return 0;
432 }
433
Kingwel Xie5efaeee2018-11-10 02:56:00 -0500434 return pm->base + ((uword) pp->index << pm->def_log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200435}
436
437static inline void *
438clib_pmalloc_alloc_inline (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
439 uword size, uword align, u32 numa_node)
440{
441 clib_pmalloc_page_t *pp;
442 u32 n_blocks, block_align, *page_index;
443
444 ASSERT (is_pow2 (align));
445
446 if (pmalloc_validate_numa_node (&numa_node))
447 return 0;
448
449 if (a == 0)
450 {
Damjan Marion567e61d2018-10-24 17:08:26 +0200451 if (size > 1ULL << pm->def_log2_page_sz)
452 return 0;
453
Damjan Marion68b4da62018-09-30 18:26:20 +0200454 vec_validate_init_empty (pm->default_arena_for_numa_node,
455 numa_node, ~0);
456 if (pm->default_arena_for_numa_node[numa_node] == ~0)
457 {
458 pool_get (pm->arenas, a);
459 pm->default_arena_for_numa_node[numa_node] = a - pm->arenas;
460 a->name = format (0, "default-numa-%u%c", numa_node, 0);
461 a->numa_node = numa_node;
Damjan Marion567e61d2018-10-24 17:08:26 +0200462 a->log2_subpage_sz = pm->def_log2_page_sz;
463 a->subpages_per_page = 1;
Damjan Marion68b4da62018-09-30 18:26:20 +0200464 }
465 else
466 a = pool_elt_at_index (pm->arenas,
467 pm->default_arena_for_numa_node[numa_node]);
468 }
Damjan Marion567e61d2018-10-24 17:08:26 +0200469 else if (size > 1ULL << a->log2_subpage_sz)
470 return 0;
Damjan Marion68b4da62018-09-30 18:26:20 +0200471
472 n_blocks = round_pow2 (size, PMALLOC_BLOCK_SZ) / PMALLOC_BLOCK_SZ;
473 block_align = align >> PMALLOC_LOG2_BLOCK_SZ;
474
475 vec_foreach (page_index, a->page_indices)
476 {
477 pp = vec_elt_at_index (pm->pages, *page_index);
478 void *rv = alloc_chunk_from_page (pm, pp, n_blocks, block_align,
479 numa_node);
480
481 if (rv)
482 return rv;
483 }
484
485 if ((a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM) == 0 &&
486 (pp = pmalloc_map_pages (pm, a, numa_node, 1)))
487 return alloc_chunk_from_page (pm, pp, n_blocks, block_align, numa_node);
488
489 return 0;
490}
491
492void *
493clib_pmalloc_alloc_aligned_on_numa (clib_pmalloc_main_t * pm, uword size,
494 uword align, u32 numa_node)
495{
496 return clib_pmalloc_alloc_inline (pm, 0, size, align, numa_node);
497}
498
499void *
500clib_pmalloc_alloc_aligned (clib_pmalloc_main_t * pm, uword size, uword align)
501{
502 return clib_pmalloc_alloc_inline (pm, 0, size, align,
503 CLIB_PMALLOC_NUMA_LOCAL);
504}
505
506void *
507clib_pmalloc_alloc_from_arena (clib_pmalloc_main_t * pm, void *arena_va,
508 uword size, uword align)
509{
510 clib_pmalloc_arena_t *a = clib_pmalloc_get_arena (pm, arena_va);
511 return clib_pmalloc_alloc_inline (pm, a, size, align, 0);
512}
513
Damjan Marion567e61d2018-10-24 17:08:26 +0200514static inline int
515pmalloc_chunks_mergeable (clib_pmalloc_arena_t * a, clib_pmalloc_page_t * pp,
516 u32 ci1, u32 ci2)
517{
518 clib_pmalloc_chunk_t *c1, *c2;
519
520 if (ci1 == ~0 || ci2 == ~0)
521 return 0;
522
523 c1 = get_chunk (pp, ci1);
524 c2 = get_chunk (pp, ci2);
525
526 if (c1->used || c2->used)
527 return 0;
528
529 if (c1->start >> (a->log2_subpage_sz - PMALLOC_LOG2_BLOCK_SZ) !=
530 c2->start >> (a->log2_subpage_sz - PMALLOC_LOG2_BLOCK_SZ))
531 return 0;
532
533 return 1;
534}
535
Damjan Marion68b4da62018-09-30 18:26:20 +0200536void
537clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va)
538{
539 clib_pmalloc_page_t *pp;
540 clib_pmalloc_chunk_t *c;
Damjan Marion567e61d2018-10-24 17:08:26 +0200541 clib_pmalloc_arena_t *a;
Damjan Marion68b4da62018-09-30 18:26:20 +0200542 uword *p;
543 u32 chunk_index, page_index;
544
545 p = hash_get (pm->chunk_index_by_va, pointer_to_uword (va));
546
547 if (p == 0)
548 os_panic ();
549
550 chunk_index = p[0];
551 page_index = clib_pmalloc_get_page_index (pm, va);
552 hash_unset (pm->chunk_index_by_va, pointer_to_uword (va));
553
554 pp = vec_elt_at_index (pm->pages, page_index);
555 c = pool_elt_at_index (pp->chunks, chunk_index);
Damjan Marion567e61d2018-10-24 17:08:26 +0200556 a = pool_elt_at_index (pm->arenas, pp->arena_index);
Damjan Marion68b4da62018-09-30 18:26:20 +0200557 c->used = 0;
558 pp->n_free_blocks += c->size;
559 pp->n_free_chunks++;
560
561 /* merge with next if free */
Damjan Marion567e61d2018-10-24 17:08:26 +0200562 if (pmalloc_chunks_mergeable (a, pp, chunk_index, c->next))
Damjan Marion68b4da62018-09-30 18:26:20 +0200563 {
564 clib_pmalloc_chunk_t *next = get_chunk (pp, c->next);
565 c->size += next->size;
566 c->next = next->next;
567 if (next->next != ~0)
568 get_chunk (pp, next->next)->prev = chunk_index;
569 memset (next, 0, sizeof (*next));
570 pool_put (pp->chunks, next);
571 pp->n_free_chunks--;
572 }
573
574 /* merge with prev if free */
Damjan Marion567e61d2018-10-24 17:08:26 +0200575 if (pmalloc_chunks_mergeable (a, pp, c->prev, chunk_index))
Damjan Marion68b4da62018-09-30 18:26:20 +0200576 {
577 clib_pmalloc_chunk_t *prev = get_chunk (pp, c->prev);
578 prev->size += c->size;
579 prev->next = c->next;
580 if (c->next != ~0)
581 get_chunk (pp, c->next)->prev = c->prev;
582 memset (c, 0, sizeof (*c));
583 pool_put (pp->chunks, c);
584 pp->n_free_chunks--;
585 }
586}
587
588static u8 *
Damjan Marion567e61d2018-10-24 17:08:26 +0200589format_log2_page_size (u8 * s, va_list * va)
590{
591 u32 log2_page_sz = va_arg (*va, u32);
592
593 if (log2_page_sz >= 30)
594 return format (s, "%uGB", 1 << (log2_page_sz - 30));
595
596 if (log2_page_sz >= 20)
597 return format (s, "%uMB", 1 << (log2_page_sz - 20));
598
599 if (log2_page_sz >= 10)
600 return format (s, "%uKB", 1 << (log2_page_sz - 10));
601
602 return format (s, "%uB", 1 << log2_page_sz);
603}
604
605
606static u8 *
Damjan Marion68b4da62018-09-30 18:26:20 +0200607format_pmalloc_page (u8 * s, va_list * va)
608{
609 clib_pmalloc_page_t *pp = va_arg (*va, clib_pmalloc_page_t *);
610 int verbose = va_arg (*va, int);
611 u32 indent = format_get_indent (s);
612
Damjan Marion68b4da62018-09-30 18:26:20 +0200613 if (pp->chunks == 0)
614 return s;
615
616 s = format (s, "free %u chunks %u free-chunks %d ",
617 (pp->n_free_blocks) << PMALLOC_LOG2_BLOCK_SZ,
618 pool_elts (pp->chunks), pp->n_free_chunks);
619
620 if (verbose >= 2)
621 {
622 clib_pmalloc_chunk_t *c;
623 c = pool_elt_at_index (pp->chunks, pp->first_chunk_index);
624 s = format (s, "\n%U%12s%12s%8s%8s%8s%8s",
625 format_white_space, indent + 2,
626 "chunk offset", "size", "used", "index", "prev", "next");
627 while (1)
628 {
629 s = format (s, "\n%U%12u%12u%8s%8d%8d%8d",
630 format_white_space, indent + 2,
631 c->start << PMALLOC_LOG2_BLOCK_SZ,
632 c->size << PMALLOC_LOG2_BLOCK_SZ,
633 c->used ? "yes" : "no",
634 c - pp->chunks, c->prev, c->next);
635 if (c->next == ~0)
636 break;
637 c = pool_elt_at_index (pp->chunks, c->next);
638 }
639 }
640 return s;
641}
642
643u8 *
644format_pmalloc (u8 * s, va_list * va)
645{
646 clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *);
647 int verbose = va_arg (*va, int);
648 u32 indent = format_get_indent (s);
649
650 clib_pmalloc_page_t *pp;
651 clib_pmalloc_arena_t *a;
652
Damjan Marion567e61d2018-10-24 17:08:26 +0200653 s = format (s, "used-pages %u reserved-pages %u default-page-size %U "
Damjan Marionc04e2b02018-10-25 15:56:04 +0200654 "lookup-page-size %U%s", vec_len (pm->pages), pm->max_pages,
Damjan Marion567e61d2018-10-24 17:08:26 +0200655 format_log2_page_size, pm->def_log2_page_sz,
Damjan Marionc04e2b02018-10-25 15:56:04 +0200656 format_log2_page_size, pm->lookup_log2_page_sz,
657 pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP ? " no-pagemap" : "");
Damjan Marion567e61d2018-10-24 17:08:26 +0200658
Damjan Marion68b4da62018-09-30 18:26:20 +0200659
660 if (verbose >= 2)
661 s = format (s, " va-start %p", pm->base);
662
663 if (pm->error)
664 s = format (s, "\n%Ulast-error: %U", format_white_space, indent + 2,
665 format_clib_error, pm->error);
666
667
668 /* *INDENT-OFF* */
669 pool_foreach (a, pm->arenas,
670 {
671 u32 *page_index;
Damjan Marion567e61d2018-10-24 17:08:26 +0200672 s = format (s, "\n%Uarena '%s' pages %u subpage-size %U numa-node %u",
673 format_white_space, indent + 2, a->name,
674 vec_len (a->page_indices), format_log2_page_size,
675 a->log2_subpage_sz, a->numa_node);
Damjan Marion68b4da62018-09-30 18:26:20 +0200676 if (a->fd != -1)
677 s = format (s, " shared fd %d", a->fd);
678 if (verbose >= 1)
679 vec_foreach (page_index, a->page_indices)
680 {
681 pp = vec_elt_at_index (pm->pages, *page_index);
682 s = format (s, "\n%U%U", format_white_space, indent + 4,
683 format_pmalloc_page, pp, verbose);
684 }
685 });
686 /* *INDENT-ON* */
687
688 return s;
689}
690
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100691u8 *
692format_pmalloc_map (u8 * s, va_list * va)
693{
694 clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *);
695
696 u32 index;
697 s = format (s, "%16s %13s %8s", "virtual-addr", "physical-addr", "size");
698 vec_foreach_index (index, pm->lookup_table)
699 {
700 uword *lookup_val, pa, va;
701 lookup_val = vec_elt_at_index (pm->lookup_table, index);
Kingwel Xiedbc34b82018-11-11 22:55:58 -0500702 va =
703 pointer_to_uword (pm->base) +
704 ((uword) index << pm->lookup_log2_page_sz);
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100705 pa = va - *lookup_val;
706 s =
707 format (s, "\n %16p %13p %8U", uword_to_pointer (va, u64),
708 uword_to_pointer (pa, u64), format_log2_page_size,
709 pm->lookup_log2_page_sz);
710 }
711 return s;
712}
713
Damjan Marion68b4da62018-09-30 18:26:20 +0200714/*
715 * fd.io coding-style-patch-verification: ON
716 *
717 * Local Variables:
718 * eval: (c-set-style "gnu")
719 * End:
720 */