Cache misses and full-cache frees take the buddy lock for each object. Batch cache refills and spills to amortize locking and metadata work. Refill without growing the allocator, falling back to individual allocation when a batch is unavailable. Signed-off-by: Emil Tsalapatis --- .../bpf/libarena/include/libarena/buddy.h | 6 ++- .../selftests/bpf/libarena/src/buddy.bpf.c | 54 +++++++++++++++++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h b/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h index 7553ad5a879d..a4d96909f5b0 100644 --- a/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h +++ b/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h @@ -81,10 +81,14 @@ enum buddy_cache_consts { */ BUDDY_CACHE_MAX_CPUS = 8192, + /* Objects moved to and from the buddy allocator at once, as an order. */ + BUDDY_CACHE_BATCH_ORDER = 3, + BUDDY_CACHE_BATCH_SIZE = 1 << BUDDY_CACHE_BATCH_ORDER, + /* * Objects retained locally for each allocation order. */ - BUDDY_CACHE_CAPACITY = 16, + BUDDY_CACHE_CAPACITY = BUDDY_CACHE_BATCH_SIZE * 2, }; struct buddy_cache_order { diff --git a/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c b/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c index 4d4d21356935..a23da811e484 100644 --- a/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +++ b/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c @@ -128,10 +128,28 @@ static __always_inline int buddy_cache_push(struct buddy_cache __arena *cache, return 0; } -void __arena *buddy_cache_alloc(struct buddy __arena *buddy, size_t order) +/* + * Refill an empty cache with one trip to the buddy allocator. Kept out of line + * so that cache hits do not pay for the refill path. + */ +static __noinline void __arena * +buddy_cache_refill(struct buddy __arena *buddy, struct buddy_cache __arena *cache, + size_t size, int order) +{ + struct buddy_cache_order __arena *slots = &cache->orders[order]; + + if (buddy_alloc_bulk_nogrow(buddy, size, BUDDY_CACHE_BATCH_ORDER, + (void __arena * __arena *)slots->objects)) + return NULL; + + WRITE_ONCE(slots->elems, BUDDY_CACHE_BATCH_SIZE - 1); + return slots->objects[BUDDY_CACHE_BATCH_SIZE - 1]; +} + +void __arena *buddy_cache_alloc(struct buddy __arena *buddy, size_t size, size_t order) { struct buddy_cache __arena *cache; - void __arena *address = NULL; + void __arena *address; if (!buddy_order_cached(order)) return NULL; @@ -140,7 +158,11 @@ void __arena *buddy_cache_alloc(struct buddy __arena *buddy, size_t order) if (!cache) return NULL; + /* NMIs cannot take the buddy lock, so they only use what is already cached. */ address = buddy_cache_pop(cache, order); + if (!address && !bpf_in_nmi()) + address = buddy_cache_refill(buddy, cache, size, order); + buddy_cache_exit(cache); return address; @@ -171,6 +193,30 @@ static int buddy_cache_get_order(void __arena *addr) return idx_get_order(chunk, idx); } +/* + * Make room in a full cache by freeing its top batch to the buddy allocator, + * then cache the freed object. Kept out of line so that frees that fit in the + * cache do not pay for it. + */ +static __noinline int +buddy_cache_spill(struct buddy __arena *buddy, struct buddy_cache __arena *cache, + int order, void __arena *addr) +{ + struct buddy_cache_order __arena *slots = &cache->orders[order]; + u32 elems; + int ret; + + elems = READ_ONCE(slots->elems) - BUDDY_CACHE_BATCH_SIZE; + ret = buddy_free_bulk(buddy, (void __arena * __arena *)&slots->objects[elems], + BUDDY_CACHE_BATCH_SIZE); + if (ret) + return ret; + + WRITE_ONCE(slots->elems, elems); + + return buddy_cache_push(cache, order, addr); +} + __always_inline int buddy_cache_free(struct buddy __arena *buddy, void __arena *addr) { struct buddy_cache __arena *cache; @@ -190,6 +236,8 @@ __always_inline int buddy_cache_free(struct buddy __arena *buddy, void __arena * return -EAGAIN; ret = buddy_cache_push(cache, order, addr); + if (ret) + ret = buddy_cache_spill(buddy, cache, order, addr); buddy_cache_exit(cache); return ret; @@ -1033,7 +1081,7 @@ void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size) return NULL; order = size_to_order(size); - address = buddy_cache_alloc(buddy, order); + address = buddy_cache_alloc(buddy, size, order); if (address) return address; -- 2.52.0