Skip to content

Commit f47fd84

Browse files
dmarionflorincoras
authored andcommitted
vppinfra: add coherent physmem allocation
Add per-NUMA coherent arenas with a page remapping callback. Type: feature Change-Id: I5f59d35f376df96148a409d63c56429020ebaa2f Signed-off-by: Damjan Marion <damarion@cisco.com>
1 parent 29017ac commit f47fd84

3 files changed

Lines changed: 99 additions & 25 deletions

File tree

‎src/vlib/physmem_funcs.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ vlib_physmem_alloc_aligned_on_numa (vlib_main_t * vm, uword n_bytes,
3636
numa_node);
3737
}
3838

39+
always_inline void *
40+
vlib_physmem_alloc_aligned_on_numa_coherent (vlib_main_t *vm, uword n_bytes, uword alignment,
41+
u32 numa_node)
42+
{
43+
clib_pmalloc_main_t *pm = vm->physmem_main.pmalloc_main;
44+
return clib_pmalloc_alloc_aligned_on_numa_coherent (pm, n_bytes, alignment, numa_node);
45+
}
46+
47+
always_inline void
48+
vlib_physmem_set_coherent_page_map_fn (vlib_main_t *vm, int (*fn) (void *, uword, uword *))
49+
{
50+
clib_pmalloc_main_t *pm = vm->physmem_main.pmalloc_main;
51+
clib_pmalloc_set_coherent_page_map_fn (pm, fn);
52+
}
53+
3954
/* By default allocate I/O memory with cache line alignment. */
4055
always_inline void *
4156
vlib_physmem_alloc (vlib_main_t * vm, uword n_bytes)

‎src/vppinfra/pmalloc.c‎

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ pmalloc_update_lookup_table (clib_pmalloc_main_t *pm, u32 first, u32 count)
274274
fd = open ((char *) "/proc/self/pagemap", O_RDONLY);
275275
while (p < (uword) elts_per_page * count)
276276
{
277+
clib_pmalloc_page_t *pp = vec_elt_at_index (pm->pages, p / elts_per_page);
278+
clib_pmalloc_arena_t *pa_arena = pm->arenas + pp->arena_index;
279+
280+
if (pa_arena->coherent)
281+
{
282+
p++;
283+
continue;
284+
}
285+
277286
va = pointer_to_uword (pm->base) + (p << pm->lookup_log2_page_sz);
278287
pa = 0;
279288
seek = (va >> clib_mem_get_log2_page_size ()) * sizeof (pa);
@@ -352,6 +361,7 @@ pmalloc_map_pages (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
352361
void *va = MAP_FAILED;
353362
uword size = (uword) n_pages << pm->def_log2_page_sz;
354363
uword page;
364+
uword coherent_pa = 0;
355365

356366
clib_error_free (pm->error);
357367

@@ -388,7 +398,7 @@ pmalloc_map_pages (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
388398
pmalloc_order_hugepages (a->log2_subpage_sz, size >> a->log2_subpage_sz);
389399
#endif /* __linux__ */
390400

391-
if (a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM)
401+
if (a->shared_mem)
392402
{
393403
mmap_flags |= MAP_SHARED;
394404
a->fd = clib_mem_vm_create_fd (a->log2_subpage_sz, "%s", a->name);
@@ -450,21 +460,35 @@ pmalloc_map_pages (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
450460
{
451461
u16 allocated_at = ~0;
452462
if (stats.unknown)
453-
clib_error_return (0,
454-
"unable to get information about numa allocation");
463+
pm->error = clib_error_return (0, "unable to get information about numa allocation");
455464

456465
for (u16 i = 0; i < CLIB_MAX_NUMAS; i++)
457466
if (stats.per_numa[i] == 1)
458467
allocated_at = i;
459468

460-
clib_error_return (0,
461-
"page allocated on the wrong numa node (%u), "
462-
"expected %u",
463-
allocated_at, numa_node);
469+
if (pm->error == 0)
470+
pm->error = clib_error_return (0,
471+
"page allocated on the wrong numa node (%u), "
472+
"expected %u",
473+
allocated_at, numa_node);
464474

465475
goto error;
466476
}
467477

478+
if (a->coherent)
479+
{
480+
if (pm->coherent_page_map_fn == 0)
481+
{
482+
pm->error = clib_error_return (0, "no coherent page map handler registered");
483+
goto error;
484+
}
485+
if (pm->coherent_page_map_fn (va, size, &coherent_pa) != 0)
486+
{
487+
pm->error = clib_error_return (0, "coherent page map handler failed");
488+
goto error;
489+
}
490+
}
491+
468492
for (i = 0; i < n_pages; i++)
469493
{
470494
vec_add2 (pm->pages, pp, 1);
@@ -487,6 +511,18 @@ pmalloc_map_pages (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
487511
else
488512
pmalloc_update_lookup_table (pm, 0, vec_len (pm->pages));
489513

514+
if (a->coherent)
515+
{
516+
u32 elts_per_page = 1U << (pm->def_log2_page_sz - pm->lookup_log2_page_sz);
517+
uword offset = pointer_to_uword (va) - coherent_pa;
518+
uword first_elt = (pp - (n_pages - 1))->index * elts_per_page;
519+
uword n_elts = n_pages * elts_per_page;
520+
uword k;
521+
522+
for (k = 0; k < n_elts; k++)
523+
pm->lookup_table[first_elt + k] = offset;
524+
}
525+
490526
/* return pointer to 1st page */
491527
return pp - (n_pages - 1);
492528

@@ -535,7 +571,7 @@ clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name,
535571
a->index = a - pm->arenas;
536572
a->name = format (0, "%s%c", name, 0);
537573
a->numa_node = numa_node;
538-
a->flags = CLIB_PMALLOC_ARENA_F_SHARED_MEM;
574+
a->shared_mem = 1;
539575
a->log2_subpage_sz = log2_page_sz;
540576
a->subpages_per_page = 1U << (pm->def_log2_page_sz - log2_page_sz);
541577

@@ -551,8 +587,8 @@ clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name,
551587
}
552588

553589
static inline void *
554-
clib_pmalloc_alloc_inline (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
555-
uword size, uword align, u32 numa_node)
590+
clib_pmalloc_alloc_inline (clib_pmalloc_main_t *pm, clib_pmalloc_arena_t *a, uword size,
591+
uword align, u32 numa_node, u8 coherent)
556592
{
557593
clib_pmalloc_page_t *pp;
558594
u32 n_blocks, block_align, *page_index;
@@ -564,24 +600,28 @@ clib_pmalloc_alloc_inline (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
564600

565601
if (a == 0)
566602
{
603+
u32 **default_arena_for_numa_node =
604+
coherent ? &pm->coherent_arena_for_numa_node : &pm->default_arena_for_numa_node;
605+
char *default_arena_name = coherent ? "coherent-numa-%u%c" : "default-numa-%u%c";
606+
567607
if (size > 1ULL << pm->def_log2_page_sz)
568608
return 0;
569609

570-
vec_validate_init_empty (pm->default_arena_for_numa_node,
571-
numa_node, ~0);
572-
if (pm->default_arena_for_numa_node[numa_node] == ~0)
610+
vec_validate_init_empty (*default_arena_for_numa_node, numa_node, ~0);
611+
if ((*default_arena_for_numa_node)[numa_node] == ~0)
573612
{
574613
pool_get (pm->arenas, a);
575614
a->index = a - pm->arenas;
576-
pm->default_arena_for_numa_node[numa_node] = a->index;
577-
a->name = format (0, "default-numa-%u%c", numa_node, 0);
615+
(*default_arena_for_numa_node)[numa_node] = a->index;
616+
a->name = format (0, default_arena_name, numa_node, 0);
578617
a->numa_node = numa_node;
579618
a->log2_subpage_sz = pm->def_log2_page_sz;
580619
a->subpages_per_page = 1;
620+
if (coherent)
621+
a->coherent = 1;
581622
}
582623
else
583-
a = pool_elt_at_index (pm->arenas,
584-
pm->default_arena_for_numa_node[numa_node]);
624+
a = pool_elt_at_index (pm->arenas, (*default_arena_for_numa_node)[numa_node]);
585625
}
586626
else if (size > 1ULL << a->log2_subpage_sz)
587627
return 0;
@@ -599,8 +639,7 @@ clib_pmalloc_alloc_inline (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
599639
return rv;
600640
}
601641

602-
if ((a->flags & CLIB_PMALLOC_ARENA_F_SHARED_MEM) == 0 &&
603-
(pp = pmalloc_map_pages (pm, a, numa_node, 1)))
642+
if (!a->shared_mem && (pp = pmalloc_map_pages (pm, a, numa_node, 1)))
604643
return alloc_chunk_from_page (pm, pp, n_blocks, block_align, numa_node);
605644

606645
return 0;
@@ -610,22 +649,28 @@ __clib_export void *
610649
clib_pmalloc_alloc_aligned_on_numa (clib_pmalloc_main_t * pm, uword size,
611650
uword align, u32 numa_node)
612651
{
613-
return clib_pmalloc_alloc_inline (pm, 0, size, align, numa_node);
652+
return clib_pmalloc_alloc_inline (pm, 0, size, align, numa_node, 0);
653+
}
654+
655+
__clib_export void *
656+
clib_pmalloc_alloc_aligned_on_numa_coherent (clib_pmalloc_main_t *pm, uword size, uword align,
657+
u32 numa_node)
658+
{
659+
return clib_pmalloc_alloc_inline (pm, 0, size, align, numa_node, 1);
614660
}
615661

616662
__clib_export void *
617663
clib_pmalloc_alloc_aligned (clib_pmalloc_main_t *pm, uword size, uword align)
618664
{
619-
return clib_pmalloc_alloc_inline (pm, 0, size, align,
620-
CLIB_PMALLOC_NUMA_LOCAL);
665+
return clib_pmalloc_alloc_inline (pm, 0, size, align, CLIB_PMALLOC_NUMA_LOCAL, 0);
621666
}
622667

623668
__clib_export void *
624669
clib_pmalloc_alloc_from_arena (clib_pmalloc_main_t *pm, void *arena_va,
625670
uword size, uword align)
626671
{
627672
clib_pmalloc_arena_t *a = clib_pmalloc_get_arena (pm, arena_va);
628-
return clib_pmalloc_alloc_inline (pm, a, size, align, 0);
673+
return clib_pmalloc_alloc_inline (pm, a, size, align, 0, 0);
629674
}
630675

631676
static inline int
@@ -650,6 +695,12 @@ pmalloc_chunks_mergeable (clib_pmalloc_arena_t * a, clib_pmalloc_page_t * pp,
650695
return 1;
651696
}
652697

698+
__clib_export void
699+
clib_pmalloc_set_coherent_page_map_fn (clib_pmalloc_main_t *pm, int (*fn) (void *, uword, uword *))
700+
{
701+
pm->coherent_page_map_fn = fn;
702+
}
703+
653704
__clib_export void
654705
clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va)
655706
{

‎src/vppinfra/pmalloc.h‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ typedef struct
3535
typedef struct
3636
{
3737
u32 index;
38-
u32 flags;
39-
#define CLIB_PMALLOC_ARENA_F_SHARED_MEM (1 << 0)
38+
u32 shared_mem : 1;
39+
u32 coherent : 1;
4040
int fd;
4141
u32 numa_node;
4242
u32 first_page_index;
@@ -79,6 +79,10 @@ typedef struct
7979
each numa node have own default privat alloc arena */
8080
u32 *default_arena_for_numa_node;
8181

82+
u32 *coherent_arena_for_numa_node;
83+
84+
int (*coherent_page_map_fn) (void *va, uword size, uword *pa);
85+
8286
/* VA to PA lookup table */
8387
uword *lookup_table;
8488
uword linear_pa_offset;
@@ -98,6 +102,10 @@ void *clib_pmalloc_alloc_aligned_on_numa (clib_pmalloc_main_t * pm,
98102
u32 numa_node);
99103
void *clib_pmalloc_alloc_aligned (clib_pmalloc_main_t * pm, uword size,
100104
uword align);
105+
void *clib_pmalloc_alloc_aligned_on_numa_coherent (clib_pmalloc_main_t *pm, uword size, uword align,
106+
u32 numa_node);
107+
void clib_pmalloc_set_coherent_page_map_fn (clib_pmalloc_main_t *pm,
108+
int (*fn) (void *, uword, uword *));
101109
void clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va);
102110

103111
void *clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name,

0 commit comments

Comments
 (0)