blob: ce78ca00334fbe2baf0705763fda9db75900eeee [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
63int
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{
Damjan Marion68b4da62018-09-30 18:26:20 +020066 uword off, pagesize;
Damjan Marionc04e2b02018-10-25 15:56:04 +020067 u64 *pt = 0;
Damjan Marion5a6c8092019-02-21 14:44:59 +010068 int mmap_flags;
Damjan Marion68b4da62018-09-30 18:26:20 +020069
70 ASSERT (pm->error == 0);
71
Damjan Marion9787f5f2018-10-24 12:56:32 +020072 pagesize = clib_mem_get_default_hugepage_size ();
Damjan Marion567e61d2018-10-24 17:08:26 +020073 pm->def_log2_page_sz = min_log2 (pagesize);
74 pm->sys_log2_page_sz = min_log2 (sysconf (_SC_PAGESIZE));
75 pm->lookup_log2_page_sz = pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +020076
Damjan Marionc04e2b02018-10-25 15:56:04 +020077 /* check if pagemap is accessible */
78 pt = clib_mem_vm_get_paddr (&pt, pm->sys_log2_page_sz, 1);
79 if (pt == 0 || pt[0] == 0)
80 pm->flags |= CLIB_PMALLOC_F_NO_PAGEMAP;
81
Damjan Marion68b4da62018-09-30 18:26:20 +020082 size = size ? size : ((u64) DEFAULT_RESERVED_MB) << 20;
83 size = round_pow2 (size, pagesize);
84
Damjan Marion567e61d2018-10-24 17:08:26 +020085 pm->max_pages = size >> pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +020086
87 /* reserve VA space for future growth */
Damjan Marion5a6c8092019-02-21 14:44:59 +010088 mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS;
89
90 if (base_addr)
91 mmap_flags |= MAP_FIXED;
92
93 pm->base = mmap (uword_to_pointer (base_addr, void *), size + pagesize,
94 PROT_NONE, mmap_flags, -1, 0);
Damjan Marion68b4da62018-09-30 18:26:20 +020095
96 if (pm->base == MAP_FAILED)
97 {
98 pm->error = clib_error_return_unix (0, "failed to reserve %u pages");
99 return -1;
100 }
101
102 off = round_pow2 (pointer_to_uword (pm->base), pagesize) -
103 pointer_to_uword (pm->base);
104
105 /* trim start and end of reservation to be page aligned */
106 if (off)
107 {
108 munmap (pm->base, off);
109 pm->base += off;
110 }
111
Damjan Marion878b65a2018-10-26 10:29:35 +0200112 munmap (pm->base + ((uword) pm->max_pages * pagesize), pagesize - off);
Damjan Marion68b4da62018-09-30 18:26:20 +0200113 return 0;
114}
115
116static inline void *
117alloc_chunk_from_page (clib_pmalloc_main_t * pm, clib_pmalloc_page_t * pp,
118 u32 n_blocks, u32 block_align, u32 numa_node)
119{
Damjan Marion567e61d2018-10-24 17:08:26 +0200120 clib_pmalloc_chunk_t *c = 0;
121 clib_pmalloc_arena_t *a;
Damjan Marion68b4da62018-09-30 18:26:20 +0200122 void *va;
123 u32 off;
124 u32 alloc_chunk_index;
125
Damjan Marion567e61d2018-10-24 17:08:26 +0200126 a = pool_elt_at_index (pm->arenas, pp->arena_index);
127
Damjan Marion68b4da62018-09-30 18:26:20 +0200128 if (pp->chunks == 0)
129 {
Damjan Marion567e61d2018-10-24 17:08:26 +0200130 u32 i, start = 0, prev = ~0;
131
132 for (i = 0; i < a->subpages_per_page; i++)
133 {
134 pool_get (pp->chunks, c);
135 c->start = start;
136 c->prev = prev;
137 c->size = pp->n_free_blocks / a->subpages_per_page;
138 start += c->size;
139 if (prev == ~0)
140 pp->first_chunk_index = c - pp->chunks;
141 else
142 pp->chunks[prev].next = c - pp->chunks;
143 prev = c - pp->chunks;
144 }
145 c->next = ~0;
146 pp->n_free_chunks = a->subpages_per_page;
Damjan Marion68b4da62018-09-30 18:26:20 +0200147 }
148
Damjan Marion78c0ff72019-01-23 12:50:24 +0100149 if (pp->n_free_blocks < n_blocks)
150 return 0;
151
Damjan Marion68b4da62018-09-30 18:26:20 +0200152 alloc_chunk_index = pp->first_chunk_index;
153
154next_chunk:
155 c = pool_elt_at_index (pp->chunks, alloc_chunk_index);
156 off = (block_align - (c->start & (block_align - 1))) & (block_align - 1);
157
158 if (c->used || n_blocks + off > c->size)
159 {
160 if (c->next == ~0)
161 return 0;
162 alloc_chunk_index = c->next;
163 goto next_chunk;
164 }
165
166 /* if alignment is needed create new empty chunk */
167 if (off)
168 {
169 u32 offset_chunk_index;
170 clib_pmalloc_chunk_t *co;
171 pool_get (pp->chunks, c);
172 pp->n_free_chunks++;
173 offset_chunk_index = alloc_chunk_index;
174 alloc_chunk_index = c - pp->chunks;
175
176 co = pool_elt_at_index (pp->chunks, offset_chunk_index);
177 c->size = co->size - off;
178 c->next = co->next;
179 c->start = co->start + off;
180 c->prev = offset_chunk_index;
181 co->size = off;
182 co->next = alloc_chunk_index;
183 }
184
185 c->used = 1;
186 if (c->size > n_blocks)
187 {
188 u32 tail_chunk_index;
189 clib_pmalloc_chunk_t *ct;
190 pool_get (pp->chunks, ct);
191 pp->n_free_chunks++;
192 tail_chunk_index = ct - pp->chunks;
193 c = pool_elt_at_index (pp->chunks, alloc_chunk_index);
194 ct->size = c->size - n_blocks;
195 ct->next = c->next;
196 ct->prev = alloc_chunk_index;
197 ct->start = c->start + n_blocks;
198
199 c->size = n_blocks;
200 c->next = tail_chunk_index;
201 if (ct->next != ~0)
202 pool_elt_at_index (pp->chunks, ct->next)->prev = tail_chunk_index;
203 }
204 else if (c->next != ~0)
205 pool_elt_at_index (pp->chunks, c->next)->prev = alloc_chunk_index;
206
207 c = get_chunk (pp, alloc_chunk_index);
Damjan Marion567e61d2018-10-24 17:08:26 +0200208 va = pm->base + ((pp - pm->pages) << pm->def_log2_page_sz) +
Damjan Marion68b4da62018-09-30 18:26:20 +0200209 (c->start << PMALLOC_LOG2_BLOCK_SZ);
210 hash_set (pm->chunk_index_by_va, pointer_to_uword (va), alloc_chunk_index);
211 pp->n_free_blocks -= n_blocks;
212 pp->n_free_chunks--;
213 return va;
214}
215
Damjan Marion567e61d2018-10-24 17:08:26 +0200216static void
217pmalloc_update_lookup_table (clib_pmalloc_main_t * pm, u32 first, u32 count)
218{
219 uword seek, va, pa, p;
220 int fd;
221 u32 elts_per_page = 1U << (pm->def_log2_page_sz - pm->lookup_log2_page_sz);
222
223 vec_validate_aligned (pm->lookup_table, vec_len (pm->pages) *
224 elts_per_page - 1, CLIB_CACHE_LINE_BYTES);
225
Dave Barach96e2d442018-11-14 11:42:03 -0500226 p = (uword) first *elts_per_page;
Damjan Marionc04e2b02018-10-25 15:56:04 +0200227 if (pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP)
228 {
Damjan Marion878b65a2018-10-26 10:29:35 +0200229 while (p < (uword) elts_per_page * count)
Damjan Marionc04e2b02018-10-25 15:56:04 +0200230 {
231 pm->lookup_table[p] = pointer_to_uword (pm->base) +
232 (p << pm->lookup_log2_page_sz);
233 p++;
234 }
235 return;
236 }
237
238 fd = open ((char *) "/proc/self/pagemap", O_RDONLY);
Damjan Marion878b65a2018-10-26 10:29:35 +0200239 while (p < (uword) elts_per_page * count)
Damjan Marion567e61d2018-10-24 17:08:26 +0200240 {
241 va = pointer_to_uword (pm->base) + (p << pm->lookup_log2_page_sz);
Damjan Marion878b65a2018-10-26 10:29:35 +0200242 pa = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +0200243 seek = (va >> pm->sys_log2_page_sz) * sizeof (pa);
244 if (fd != -1 && lseek (fd, seek, SEEK_SET) == seek &&
245 read (fd, &pa, sizeof (pa)) == (sizeof (pa)) &&
246 pa & (1ULL << 63) /* page present bit */ )
247 {
248 pa = (pa & pow2_mask (55)) << pm->sys_log2_page_sz;
249 }
250 pm->lookup_table[p] = va - pa;
251 p++;
252 }
253
254 if (fd != -1)
255 close (fd);
256}
257
Damjan Marion68b4da62018-09-30 18:26:20 +0200258static inline clib_pmalloc_page_t *
259pmalloc_map_pages (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
260 u32 numa_node, u32 n_pages)
261{
262 clib_pmalloc_page_t *pp = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +0200263 int status, rv, i, mmap_flags;
Damjan Marion801c7012019-10-30 18:07:35 +0100264 void *va = MAP_FAILED;
Damjan Marion68b4da62018-09-30 18:26:20 +0200265 int old_mpol = -1;
266 long unsigned int mask[16] = { 0 };
267 long unsigned int old_mask[16] = { 0 };
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
Damjan Marion68b4da62018-09-30 18:26:20 +0200306 if (a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM)
307 {
308 mmap_flags |= MAP_SHARED;
Damjan Marion8ebd7922018-11-28 10:46:03 +0100309 if (a->log2_subpage_sz != pm->sys_log2_page_sz)
Damjan Marion567e61d2018-10-24 17:08:26 +0200310 pm->error = clib_mem_create_hugetlb_fd ((char *) a->name, &a->fd);
311 else
312 pm->error = clib_mem_create_fd ((char *) a->name, &a->fd);
Damjan Marion68b4da62018-09-30 18:26:20 +0200313 if (a->fd == -1)
314 goto error;
Damjan Marion54e8e392018-11-07 17:55:26 +0100315 if ((ftruncate (a->fd, size)) == -1)
316 goto error;
Damjan Marion68b4da62018-09-30 18:26:20 +0200317 }
318 else
319 {
Damjan Marion8ebd7922018-11-28 10:46:03 +0100320 if (a->log2_subpage_sz != pm->sys_log2_page_sz)
321 mmap_flags |= MAP_HUGETLB;
322
Damjan Marion54e8e392018-11-07 17:55:26 +0100323 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
Damjan Marion68b4da62018-09-30 18:26:20 +0200324 a->fd = -1;
325 }
326
Damjan Marion567e61d2018-10-24 17:08:26 +0200327 va = pm->base + (((uword) vec_len (pm->pages)) << pm->def_log2_page_sz);
328 if (mmap (va, size, PROT_READ | PROT_WRITE, mmap_flags, a->fd, 0) ==
329 MAP_FAILED)
Damjan Marion68b4da62018-09-30 18:26:20 +0200330 {
331 pm->error = clib_error_return_unix (0, "failed to mmap %u pages at %p "
332 "fd %d numa %d flags 0x%x", n_pages,
333 va, a->fd, numa_node, mmap_flags);
334 goto error;
335 }
336
Damjan Marion801c7012019-10-30 18:07:35 +0100337 if (a->log2_subpage_sz != pm->sys_log2_page_sz && mlock (va, size) != 0)
Artem Belovf6defa12019-02-26 01:47:34 +0000338 {
Damjan Marion801c7012019-10-30 18:07:35 +0100339 pm->error = clib_error_return_unix (0, "Unable to lock pages");
340 goto error;
Artem Belovf6defa12019-02-26 01:47:34 +0000341 }
342
Damjan Marion567e61d2018-10-24 17:08:26 +0200343 clib_memset (va, 0, size);
344
Damjan Marion68b4da62018-09-30 18:26:20 +0200345 rv = set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1);
346 if (rv == -1 && numa_node != 0)
347 {
348 pm->error = clib_error_return_unix (0, "failed to restore mempolicy");
349 goto error;
350 }
351
352 /* we tolerate move_pages failure only if request os for numa node 0
353 to support non-numa kernels */
354 rv = move_pages (0, 1, &va, 0, &status, 0);
355 if ((rv == 0 && status != numa_node) || (rv != 0 && numa_node != 0))
356 {
357 pm->error = rv == -1 ?
358 clib_error_return_unix (0, "page allocated on wrong node, numa node "
359 "%u status %d", numa_node, status) :
360 clib_error_return (0, "page allocated on wrong node, numa node "
361 "%u status %d", numa_node, status);
362
Damjan Marion68b4da62018-09-30 18:26:20 +0200363 goto error;
364 }
365
Damjan Marion68b4da62018-09-30 18:26:20 +0200366 for (i = 0; i < n_pages; i++)
367 {
Damjan Marion68b4da62018-09-30 18:26:20 +0200368 vec_add2 (pm->pages, pp, 1);
Damjan Marion567e61d2018-10-24 17:08:26 +0200369 pp->n_free_blocks = 1 << (pm->def_log2_page_sz - PMALLOC_LOG2_BLOCK_SZ);
Damjan Marion68b4da62018-09-30 18:26:20 +0200370 pp->index = pp - pm->pages;
371 pp->arena_index = a->index;
Damjan Marion68b4da62018-09-30 18:26:20 +0200372 vec_add1 (a->page_indices, pp->index);
373 a->n_pages++;
Damjan Marion68b4da62018-09-30 18:26:20 +0200374 }
375
Damjan Marion567e61d2018-10-24 17:08:26 +0200376
377 /* if new arena is using smaller page size, we need to rebuild whole
378 lookup table */
379 if (a->log2_subpage_sz < pm->lookup_log2_page_sz)
380 {
381 pm->lookup_log2_page_sz = a->log2_subpage_sz;
382 pmalloc_update_lookup_table (pm, vec_len (pm->pages) - n_pages,
383 n_pages);
384 }
385 else
386 pmalloc_update_lookup_table (pm, 0, vec_len (pm->pages));
Damjan Marion68b4da62018-09-30 18:26:20 +0200387
388 /* return pointer to 1st page */
389 return pp - (n_pages - 1);
390
391error:
Damjan Marion801c7012019-10-30 18:07:35 +0100392 if (va != MAP_FAILED)
393 {
394 /* unmap & reserve */
395 munmap (va, size);
396 mmap (va, size, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
397 -1, 0);
398 }
Damjan Marion68b4da62018-09-30 18:26:20 +0200399 if (a->fd != -1)
400 close (a->fd);
401 return 0;
402}
403
404void *
405clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name,
Damjan Marion567e61d2018-10-24 17:08:26 +0200406 uword size, u32 log2_page_sz, u32 numa_node)
Damjan Marion68b4da62018-09-30 18:26:20 +0200407{
408 clib_pmalloc_arena_t *a;
409 clib_pmalloc_page_t *pp;
Damjan Marion567e61d2018-10-24 17:08:26 +0200410 u32 n_pages;
411
412 clib_error_free (pm->error);
413
414 if (log2_page_sz == 0)
415 log2_page_sz = pm->def_log2_page_sz;
416 else if (log2_page_sz != pm->def_log2_page_sz &&
417 log2_page_sz != pm->sys_log2_page_sz)
418 {
419 pm->error = clib_error_create ("unsupported page size (%uKB)",
420 1 << (log2_page_sz - 10));
421 return 0;
422 }
423
424 n_pages = pmalloc_size2pages (size, pm->def_log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200425
426 if (n_pages + vec_len (pm->pages) > pm->max_pages)
427 return 0;
428
429 if (pmalloc_validate_numa_node (&numa_node))
430 return 0;
431
432 pool_get (pm->arenas, a);
433 a->index = a - pm->arenas;
434 a->name = format (0, "%s%c", name, 0);
435 a->numa_node = numa_node;
436 a->flags = CLIB_PMALLOC_ARENA_F_SHARED_MEM;
Damjan Marion567e61d2018-10-24 17:08:26 +0200437 a->log2_subpage_sz = log2_page_sz;
438 a->subpages_per_page = 1U << (pm->def_log2_page_sz - log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200439
440 if ((pp = pmalloc_map_pages (pm, a, numa_node, n_pages)) == 0)
441 {
442 vec_free (a->name);
443 memset (a, 0, sizeof (*a));
444 pool_put (pm->arenas, a);
445 return 0;
446 }
447
Kingwel Xie5efaeee2018-11-10 02:56:00 -0500448 return pm->base + ((uword) pp->index << pm->def_log2_page_sz);
Damjan Marion68b4da62018-09-30 18:26:20 +0200449}
450
451static inline void *
452clib_pmalloc_alloc_inline (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
453 uword size, uword align, u32 numa_node)
454{
455 clib_pmalloc_page_t *pp;
456 u32 n_blocks, block_align, *page_index;
457
458 ASSERT (is_pow2 (align));
459
460 if (pmalloc_validate_numa_node (&numa_node))
461 return 0;
462
463 if (a == 0)
464 {
Damjan Marion567e61d2018-10-24 17:08:26 +0200465 if (size > 1ULL << pm->def_log2_page_sz)
466 return 0;
467
Damjan Marion68b4da62018-09-30 18:26:20 +0200468 vec_validate_init_empty (pm->default_arena_for_numa_node,
469 numa_node, ~0);
470 if (pm->default_arena_for_numa_node[numa_node] == ~0)
471 {
472 pool_get (pm->arenas, a);
473 pm->default_arena_for_numa_node[numa_node] = a - pm->arenas;
474 a->name = format (0, "default-numa-%u%c", numa_node, 0);
475 a->numa_node = numa_node;
Damjan Marion567e61d2018-10-24 17:08:26 +0200476 a->log2_subpage_sz = pm->def_log2_page_sz;
477 a->subpages_per_page = 1;
Damjan Marion68b4da62018-09-30 18:26:20 +0200478 }
479 else
480 a = pool_elt_at_index (pm->arenas,
481 pm->default_arena_for_numa_node[numa_node]);
482 }
Damjan Marion567e61d2018-10-24 17:08:26 +0200483 else if (size > 1ULL << a->log2_subpage_sz)
484 return 0;
Damjan Marion68b4da62018-09-30 18:26:20 +0200485
486 n_blocks = round_pow2 (size, PMALLOC_BLOCK_SZ) / PMALLOC_BLOCK_SZ;
487 block_align = align >> PMALLOC_LOG2_BLOCK_SZ;
488
489 vec_foreach (page_index, a->page_indices)
490 {
491 pp = vec_elt_at_index (pm->pages, *page_index);
492 void *rv = alloc_chunk_from_page (pm, pp, n_blocks, block_align,
493 numa_node);
494
495 if (rv)
496 return rv;
497 }
498
499 if ((a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM) == 0 &&
500 (pp = pmalloc_map_pages (pm, a, numa_node, 1)))
501 return alloc_chunk_from_page (pm, pp, n_blocks, block_align, numa_node);
502
503 return 0;
504}
505
506void *
507clib_pmalloc_alloc_aligned_on_numa (clib_pmalloc_main_t * pm, uword size,
508 uword align, u32 numa_node)
509{
510 return clib_pmalloc_alloc_inline (pm, 0, size, align, numa_node);
511}
512
513void *
514clib_pmalloc_alloc_aligned (clib_pmalloc_main_t * pm, uword size, uword align)
515{
516 return clib_pmalloc_alloc_inline (pm, 0, size, align,
517 CLIB_PMALLOC_NUMA_LOCAL);
518}
519
520void *
521clib_pmalloc_alloc_from_arena (clib_pmalloc_main_t * pm, void *arena_va,
522 uword size, uword align)
523{
524 clib_pmalloc_arena_t *a = clib_pmalloc_get_arena (pm, arena_va);
525 return clib_pmalloc_alloc_inline (pm, a, size, align, 0);
526}
527
Damjan Marion567e61d2018-10-24 17:08:26 +0200528static inline int
529pmalloc_chunks_mergeable (clib_pmalloc_arena_t * a, clib_pmalloc_page_t * pp,
530 u32 ci1, u32 ci2)
531{
532 clib_pmalloc_chunk_t *c1, *c2;
533
534 if (ci1 == ~0 || ci2 == ~0)
535 return 0;
536
537 c1 = get_chunk (pp, ci1);
538 c2 = get_chunk (pp, ci2);
539
540 if (c1->used || c2->used)
541 return 0;
542
543 if (c1->start >> (a->log2_subpage_sz - PMALLOC_LOG2_BLOCK_SZ) !=
544 c2->start >> (a->log2_subpage_sz - PMALLOC_LOG2_BLOCK_SZ))
545 return 0;
546
547 return 1;
548}
549
Damjan Marion68b4da62018-09-30 18:26:20 +0200550void
551clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va)
552{
553 clib_pmalloc_page_t *pp;
554 clib_pmalloc_chunk_t *c;
Damjan Marion567e61d2018-10-24 17:08:26 +0200555 clib_pmalloc_arena_t *a;
Damjan Marion68b4da62018-09-30 18:26:20 +0200556 uword *p;
557 u32 chunk_index, page_index;
558
559 p = hash_get (pm->chunk_index_by_va, pointer_to_uword (va));
560
561 if (p == 0)
562 os_panic ();
563
564 chunk_index = p[0];
565 page_index = clib_pmalloc_get_page_index (pm, va);
566 hash_unset (pm->chunk_index_by_va, pointer_to_uword (va));
567
568 pp = vec_elt_at_index (pm->pages, page_index);
569 c = pool_elt_at_index (pp->chunks, chunk_index);
Damjan Marion567e61d2018-10-24 17:08:26 +0200570 a = pool_elt_at_index (pm->arenas, pp->arena_index);
Damjan Marion68b4da62018-09-30 18:26:20 +0200571 c->used = 0;
572 pp->n_free_blocks += c->size;
573 pp->n_free_chunks++;
574
575 /* merge with next if free */
Damjan Marion567e61d2018-10-24 17:08:26 +0200576 if (pmalloc_chunks_mergeable (a, pp, chunk_index, c->next))
Damjan Marion68b4da62018-09-30 18:26:20 +0200577 {
578 clib_pmalloc_chunk_t *next = get_chunk (pp, c->next);
579 c->size += next->size;
580 c->next = next->next;
581 if (next->next != ~0)
582 get_chunk (pp, next->next)->prev = chunk_index;
583 memset (next, 0, sizeof (*next));
584 pool_put (pp->chunks, next);
585 pp->n_free_chunks--;
586 }
587
588 /* merge with prev if free */
Damjan Marion567e61d2018-10-24 17:08:26 +0200589 if (pmalloc_chunks_mergeable (a, pp, c->prev, chunk_index))
Damjan Marion68b4da62018-09-30 18:26:20 +0200590 {
591 clib_pmalloc_chunk_t *prev = get_chunk (pp, c->prev);
592 prev->size += c->size;
593 prev->next = c->next;
594 if (c->next != ~0)
595 get_chunk (pp, c->next)->prev = c->prev;
596 memset (c, 0, sizeof (*c));
597 pool_put (pp->chunks, c);
598 pp->n_free_chunks--;
599 }
600}
601
602static u8 *
Damjan Marion567e61d2018-10-24 17:08:26 +0200603format_log2_page_size (u8 * s, va_list * va)
604{
605 u32 log2_page_sz = va_arg (*va, u32);
606
607 if (log2_page_sz >= 30)
608 return format (s, "%uGB", 1 << (log2_page_sz - 30));
609
610 if (log2_page_sz >= 20)
611 return format (s, "%uMB", 1 << (log2_page_sz - 20));
612
613 if (log2_page_sz >= 10)
614 return format (s, "%uKB", 1 << (log2_page_sz - 10));
615
616 return format (s, "%uB", 1 << log2_page_sz);
617}
618
619
620static u8 *
Damjan Marion68b4da62018-09-30 18:26:20 +0200621format_pmalloc_page (u8 * s, va_list * va)
622{
623 clib_pmalloc_page_t *pp = va_arg (*va, clib_pmalloc_page_t *);
624 int verbose = va_arg (*va, int);
625 u32 indent = format_get_indent (s);
626
Damjan Marion68b4da62018-09-30 18:26:20 +0200627 if (pp->chunks == 0)
628 return s;
629
630 s = format (s, "free %u chunks %u free-chunks %d ",
631 (pp->n_free_blocks) << PMALLOC_LOG2_BLOCK_SZ,
632 pool_elts (pp->chunks), pp->n_free_chunks);
633
634 if (verbose >= 2)
635 {
636 clib_pmalloc_chunk_t *c;
637 c = pool_elt_at_index (pp->chunks, pp->first_chunk_index);
638 s = format (s, "\n%U%12s%12s%8s%8s%8s%8s",
639 format_white_space, indent + 2,
640 "chunk offset", "size", "used", "index", "prev", "next");
641 while (1)
642 {
643 s = format (s, "\n%U%12u%12u%8s%8d%8d%8d",
644 format_white_space, indent + 2,
645 c->start << PMALLOC_LOG2_BLOCK_SZ,
646 c->size << PMALLOC_LOG2_BLOCK_SZ,
647 c->used ? "yes" : "no",
648 c - pp->chunks, c->prev, c->next);
649 if (c->next == ~0)
650 break;
651 c = pool_elt_at_index (pp->chunks, c->next);
652 }
653 }
654 return s;
655}
656
657u8 *
658format_pmalloc (u8 * s, va_list * va)
659{
660 clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *);
661 int verbose = va_arg (*va, int);
662 u32 indent = format_get_indent (s);
663
664 clib_pmalloc_page_t *pp;
665 clib_pmalloc_arena_t *a;
666
Damjan Marion567e61d2018-10-24 17:08:26 +0200667 s = format (s, "used-pages %u reserved-pages %u default-page-size %U "
Damjan Marionc04e2b02018-10-25 15:56:04 +0200668 "lookup-page-size %U%s", vec_len (pm->pages), pm->max_pages,
Damjan Marion567e61d2018-10-24 17:08:26 +0200669 format_log2_page_size, pm->def_log2_page_sz,
Damjan Marionc04e2b02018-10-25 15:56:04 +0200670 format_log2_page_size, pm->lookup_log2_page_sz,
671 pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP ? " no-pagemap" : "");
Damjan Marion567e61d2018-10-24 17:08:26 +0200672
Damjan Marion68b4da62018-09-30 18:26:20 +0200673
674 if (verbose >= 2)
675 s = format (s, " va-start %p", pm->base);
676
677 if (pm->error)
678 s = format (s, "\n%Ulast-error: %U", format_white_space, indent + 2,
679 format_clib_error, pm->error);
680
681
682 /* *INDENT-OFF* */
683 pool_foreach (a, pm->arenas,
684 {
685 u32 *page_index;
Damjan Marion567e61d2018-10-24 17:08:26 +0200686 s = format (s, "\n%Uarena '%s' pages %u subpage-size %U numa-node %u",
687 format_white_space, indent + 2, a->name,
688 vec_len (a->page_indices), format_log2_page_size,
689 a->log2_subpage_sz, a->numa_node);
Damjan Marion68b4da62018-09-30 18:26:20 +0200690 if (a->fd != -1)
691 s = format (s, " shared fd %d", a->fd);
692 if (verbose >= 1)
693 vec_foreach (page_index, a->page_indices)
694 {
695 pp = vec_elt_at_index (pm->pages, *page_index);
696 s = format (s, "\n%U%U", format_white_space, indent + 4,
697 format_pmalloc_page, pp, verbose);
698 }
699 });
700 /* *INDENT-ON* */
701
702 return s;
703}
704
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100705u8 *
706format_pmalloc_map (u8 * s, va_list * va)
707{
708 clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *);
709
710 u32 index;
711 s = format (s, "%16s %13s %8s", "virtual-addr", "physical-addr", "size");
712 vec_foreach_index (index, pm->lookup_table)
713 {
714 uword *lookup_val, pa, va;
715 lookup_val = vec_elt_at_index (pm->lookup_table, index);
Kingwel Xiedbc34b82018-11-11 22:55:58 -0500716 va =
717 pointer_to_uword (pm->base) +
718 ((uword) index << pm->lookup_log2_page_sz);
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100719 pa = va - *lookup_val;
720 s =
721 format (s, "\n %16p %13p %8U", uword_to_pointer (va, u64),
722 uword_to_pointer (pa, u64), format_log2_page_size,
723 pm->lookup_log2_page_sz);
724 }
725 return s;
726}
727
Damjan Marion68b4da62018-09-30 18:26:20 +0200728/*
729 * fd.io coding-style-patch-verification: ON
730 *
731 * Local Variables:
732 * eval: (c-set-style "gnu")
733 * End:
734 */