AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/10/21 04:22 flow patch-triage
0m Args:
null

Results:
{
  "EnableConfigs": null,
  "FocusSymbols": [],
  "KMSANReasoning": "",
  "NeedsKMSAN": false,
  "Reasoning": "The patch series only modifies code in tools/testing/selftests/bpf/ (BPF selftest benchmark and libarena buddy cache allocator implementation for selftests). It does not modify the compiled kernel image (vmlinux) or kernel modules. Therefore, it is not worth fuzzing with syzkaller.",
  "WorthFuzzing": false
}

1/1 2026/10/21 04:22 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 02b11b0275ad1f6550918aaa229b37ec791c1532\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Wed Oct 21 04:22:06 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/bpf/bench.c\nindex d5f494184f9d1..7d2a3587971fb 100644\n--- a/tools/testing/selftests/bpf/bench.c\n+++ b/tools/testing/selftests/bpf/bench.c\n@@ -589,6 +589,7 @@ extern const struct bench bench_bpf_nop;\n extern const struct bench bench_xdp_lb;\n extern const struct bench bench_libarena_malloc;\n extern const struct bench bench_libarena_calloc;\n+extern const struct bench bench_libarena_malloc_free;\n \n static const struct bench *benchs[] = {\n \t\u0026bench_count_global,\n@@ -675,6 +676,7 @@ static const struct bench *benchs[] = {\n \t\u0026bench_xdp_lb,\n \t\u0026bench_libarena_malloc,\n \t\u0026bench_libarena_calloc,\n+\t\u0026bench_libarena_malloc_free,\n };\n \n static void find_benchmark(void)\ndiff --git a/tools/testing/selftests/bpf/benchs/bench_libarena.c b/tools/testing/selftests/bpf/benchs/bench_libarena.c\nindex 24e432244bf5d..1185a471123c1 100644\n--- a/tools/testing/selftests/bpf/benchs/bench_libarena.c\n+++ b/tools/testing/selftests/bpf/benchs/bench_libarena.c\n@@ -25,6 +25,10 @@ static struct {\n \tstruct libarena_bench *skel;\n \tint bench_fd;\n \tint reset_fd;\n+\tbool reset_each_run;\n+\tbool stop;\n+\tint stopped;\n+\tint samples;\n } ctx;\n \n enum {\n@@ -36,7 +40,7 @@ static const struct argp_option opts[] = {\n \t{ \"alloc_size\", ARG_LIBARENA_ALLOC_SIZE, \"BYTES\", 0,\n \t  \"Size of each arena allocation\" },\n \t{ \"nallocs\", ARG_LIBARENA_NALLOCS, \"ITERS\", 0,\n-\t  \"Number of allocation per measurement\" },\n+\t  \"Number of allocations per invocation\" },\n \t{},\n };\n \n@@ -68,12 +72,17 @@ const struct argp bench_libarena_argp = {\n \t.parser = parse_arg,\n };\n \n-static void validate(void)\n+static void validate_common(void)\n {\n \tif (env.consumer_cnt != 0) {\n \t\tfprintf(stderr, \"benchmark doesn't support consumers\\n\");\n \t\texit(1);\n \t}\n+}\n+\n+static void validate(void)\n+{\n+\tvalidate_common();\n \n \tif (env.producer_cnt != 1) {\n \t\tfprintf(stderr, \"benchmark supports exactly one producer\\n\");\n@@ -81,6 +90,21 @@ static void validate(void)\n \t}\n }\n \n+static void malloc_free_validate(void)\n+{\n+\tvalidate_common();\n+\n+\tif (env.producer_cnt \u003c 1) {\n+\t\tfprintf(stderr, \"benchmark requires at least one producer\\n\");\n+\t\texit(1);\n+\t}\n+\n+\tif (args.alloc_size \u003c sizeof(void *)) {\n+\t\tfprintf(stderr, \"allocation size must hold a pointer\\n\");\n+\t\texit(1);\n+\t}\n+}\n+\n static void setup_common(void)\n {\n \tstruct arena_alloc_reserve_args reserve_args = {\n@@ -114,38 +138,55 @@ static void setup_common(void)\n \tctx.skel-\u003ebss-\u003ebench_alloc_size = args.alloc_size;\n \tctx.skel-\u003ebss-\u003ebench_nallocs = args.nallocs;\n \tctx.reset_fd = bpf_program__fd(ctx.skel-\u003eprogs.arena_buddy_reset);\n+\tctx.reset_each_run = false;\n+\tctx.stop = false;\n+\tctx.stopped = 0;\n+\tctx.samples = 0;\n }\n \n static void malloc_setup(void)\n {\n \tsetup_common();\n \tctx.bench_fd = bpf_program__fd(ctx.skel-\u003eprogs.bench_malloc);\n+\tctx.reset_each_run = true;\n }\n \n static void calloc_setup(void)\n {\n \tsetup_common();\n \tctx.bench_fd = bpf_program__fd(ctx.skel-\u003eprogs.bench_calloc);\n+\tctx.reset_each_run = true;\n+}\n+\n+static void malloc_free_setup(void)\n+{\n+\tsetup_common();\n+\tctx.bench_fd = bpf_program__fd(ctx.skel-\u003eprogs.bench_malloc_free);\n+\tctx.skel-\u003ebss-\u003ebench_collect = false;\n }\n \n static void *producer(void *input)\n {\n \tint err;\n \n-\twhile (true) {\n+\twhile (!__atomic_load_n(\u0026ctx.stop, __ATOMIC_ACQUIRE)) {\n \t\terr = libarena_run_prog(ctx.bench_fd);\n \t\tif (err) {\n \t\t\tfprintf(stderr, \"libarena benchmark failed: %d\\n\", err);\n \t\t\texit(1);\n \t\t}\n \n-\t\terr = libarena_run_prog(ctx.reset_fd);\n-\t\tif (err) {\n-\t\t\tfprintf(stderr, \"libarena alloc reset failed: %d\\n\", err);\n-\t\t\texit(1);\n+\t\tif (ctx.reset_each_run) {\n+\t\t\terr = libarena_run_prog(ctx.reset_fd);\n+\t\t\tif (err) {\n+\t\t\t\tfprintf(stderr, \"libarena alloc reset failed: %d\\n\", err);\n+\t\t\t\texit(1);\n+\t\t\t}\n \t\t}\n \t}\n \n+\t__atomic_add_fetch(\u0026ctx.stopped, 1, __ATOMIC_RELEASE);\n+\n \treturn NULL;\n }\n \n@@ -155,6 +196,15 @@ static void measure(struct bench_res *res)\n \tres-\u003ehits = atomic_swap(\u0026ctx.skel-\u003ebss-\u003ebench_hits, 0);\n }\n \n+static void malloc_free_measure(struct bench_res *res)\n+{\n+\t(void)res;\n+\n+\tif (++ctx.samples == env.warmup_sec)\n+\t\t__atomic_store_n(\u0026ctx.skel-\u003ebss-\u003ebench_collect, true,\n+\t\t\t\t __ATOMIC_RELAXED);\n+}\n+\n static void report_progress(int iter, struct bench_res *res, long delta_ns)\n {\n \tdouble latency_ns = 0.0;\n@@ -162,7 +212,7 @@ static void report_progress(int iter, struct bench_res *res, long delta_ns)\n \tif (res-\u003ehits)\n \t\tlatency_ns = res-\u003eduration_ns / (double)res-\u003ehits;\n \n-\tprintf(\"Iter %3d (%7.3lfus): latency %8.3lf ns/op (%ld allocations)\\n\",\n+\tprintf(\"Iter %3d (%7.3lfus): latency %8.3lf ns/allocation (%ld allocations)\\n\",\n \t       iter, (delta_ns - 1000000000) / 1000.0, latency_ns, res-\u003ehits);\n }\n \n@@ -182,9 +232,27 @@ static void report_final(struct bench_res res[], int res_cnt)\n \t\treturn;\n \t}\n \n-\tprintf(\"Summary: %.3lf ns/op, %.0lf invocations for %u allocations/invocation)\\n\",\n-\t       duration_ns / (double)hits, hits / (double)res_cnt,\n-\t       ctx.skel-\u003ebss-\u003ebench_nallocs);\n+\tprintf(\"Summary: %.3lf ns/allocation (%ld allocations)\\n\",\n+\t       duration_ns / (double)hits, hits);\n+}\n+\n+static void malloc_free_report_final(struct bench_res res[], int res_cnt)\n+{\n+\tstruct bench_res final = {};\n+\n+\t(void)res;\n+\t(void)res_cnt;\n+\n+\t__atomic_store_n(\u0026ctx.stop, true, __ATOMIC_RELEASE);\n+\twhile (__atomic_load_n(\u0026ctx.stopped, __ATOMIC_ACQUIRE) != env.producer_cnt)\n+\t\tusleep(1000);\n+\n+\tfinal.duration_ns = __atomic_load_n(\n+\t\t\u0026ctx.skel-\u003ebss-\u003ebench_duration_ns, __ATOMIC_RELAXED);\n+\tfinal.hits = __atomic_load_n(\n+\t\t\u0026ctx.skel-\u003ebss-\u003ebench_hits, __ATOMIC_RELAXED);\n+\n+\treport_final(\u0026final, 1);\n }\n \n const struct bench bench_libarena_malloc = {\n@@ -208,3 +276,13 @@ const struct bench bench_libarena_calloc = {\n \t.report_progress = report_progress,\n \t.report_final = report_final,\n };\n+\n+const struct bench bench_libarena_malloc_free = {\n+\t.name = \"libarena-malloc-free\",\n+\t.argp = \u0026bench_libarena_argp,\n+\t.validate = malloc_free_validate,\n+\t.setup = malloc_free_setup,\n+\t.producer_thread = producer,\n+\t.measure = malloc_free_measure,\n+\t.report_final = malloc_free_report_final,\n+};\ndiff --git a/tools/testing/selftests/bpf/benchs/run_bench_libarena.sh b/tools/testing/selftests/bpf/benchs/run_bench_libarena.sh\nindex 10afe4d52ebf7..5d5f84310b26a 100755\n--- a/tools/testing/selftests/bpf/benchs/run_bench_libarena.sh\n+++ b/tools/testing/selftests/bpf/benchs/run_bench_libarena.sh\n@@ -29,3 +29,11 @@ printf \"\\t-------------------\\n\"\n \t\t\t\"$($RUN_BENCH --alloc_size \"$size\" --nallocs \"$nallocs\" libarena-calloc)\"\n \tdone\n done\n+\n+header \"libarena parallel malloc/free\\n\"\n+\n+for producers in 1 2 4 8; do\n+\tsummarize_libarena \"$producers producers:\" \\\n+\t\t\"$($RUN_BENCH -a -p \"$producers\" --alloc_size 64 --nallocs 1000 \\\n+\t\tlibarena-malloc-free)\"\n+done\ndiff --git a/tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c b/tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c\nindex e06397dc9a40d..3c9235c56b007 100644\n--- a/tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c\n+++ b/tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c\n@@ -9,6 +9,7 @@ u32 bench_alloc_size;\n u32 bench_nallocs;\n long bench_hits;\n long bench_duration_ns;\n+bool bench_collect;\n \n SEC(\"syscall\")\n int bench_malloc(void)\n@@ -49,3 +50,45 @@ int bench_calloc(void)\n \t__sync_add_and_fetch(\u0026bench_hits, i);\n \treturn 0;\n }\n+\n+struct bench_alloc {\n+\tstruct bench_alloc __arena *next;\n+};\n+\n+SEC(\"syscall\")\n+int bench_malloc_free(void)\n+{\n+\tstruct bench_alloc __arena *next, *obj, *head = NULL;\n+\tu64 duration_ns = 0, start_ns;\n+\tu32 i, nallocs;\n+\tbool collect;\n+\n+\tcollect = READ_ONCE(bench_collect);\n+\tfor (i = zero; i \u003c bench_nallocs \u0026\u0026 can_loop; i++) {\n+\t\tstart_ns = bpf_ktime_get_ns();\n+\t\tobj = arena_malloc(bench_alloc_size);\n+\t\tduration_ns += bpf_ktime_get_ns() - start_ns;\n+\t\tif (!obj)\n+\t\t\tbreak;\n+\n+\t\tobj-\u003enext = head;\n+\t\thead = obj;\n+\t}\n+\tnallocs = i;\n+\n+\tfor (i = zero; head \u0026\u0026 can_loop; i++) {\n+\t\tnext = head-\u003enext;\n+\t\tarena_free(head);\n+\t\thead = next;\n+\t}\n+\n+\tif (nallocs != bench_nallocs)\n+\t\treturn -ENOMEM;\n+\n+\tif (!collect)\n+\t\treturn 0;\n+\n+\t__sync_add_and_fetch(\u0026bench_duration_ns, duration_ns);\n+\t__sync_add_and_fetch(\u0026bench_hits, nallocs);\n+\treturn 0;\n+}\ndiff --git a/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h b/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h\nindex 528c69a1f38e0..655b3f5624bf3 100644\n--- a/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h\n+++ b/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h\n@@ -2,6 +2,8 @@\n /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n #pragma once\n \n+#include \u003clibarena/buddy_cache.h\u003e\n+\n enum buddy_consts {\n \t/*\n \t * Minimum allocation is 1 \u003c\u003c BUDDY_MIN_ALLOC_SHIFT.\n@@ -49,6 +51,8 @@ struct buddy_header {\n \tu32 next_index; /* Same for the next allocation. */\n };\n \n+struct buddy_deferred_free;\n+\n /*\n  * We bring memory into the allocator 1 MiB at a time.\n  */\n@@ -69,6 +73,8 @@ struct buddy {\n \tstruct buddy_chunk __arena *first_chunk;\t\t/* Pointer to the chunk linked list. */\n \tarena_spinlock_t lock;\t\t\t/* Allocator lock */\n \tu64 vaddr;\t\t\t\t/* Allocation into reserved vaddr */\n+\tstruct buddy_cache_state cache_state;\n+\tstruct buddy_deferred_free __arena *deferred_free;\n };\n \n #ifdef __BPF__\n@@ -76,6 +82,10 @@ struct buddy {\n int buddy_init(struct buddy __arena *buddy);\n int buddy_destroy(struct buddy __arena *buddy);\n int buddy_free(struct buddy __arena *buddy, void __arena *free);\n+int buddy_free_bulk(struct buddy __arena *buddy, void __arena * __arena *addrs,\n+\tsize_t naddrs);\n void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size);\n+int buddy_alloc_bulk(struct buddy __arena *buddy, size_t size, size_t norder,\n+\tvoid __arena * __arena * out);\n \n #endif /* __BPF__  */\ndiff --git a/tools/testing/selftests/bpf/libarena/include/libarena/buddy_cache.h b/tools/testing/selftests/bpf/libarena/include/libarena/buddy_cache.h\nnew file mode 100644\nindex 0000000000000..9fa90097dc98e\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/libarena/include/libarena/buddy_cache.h\n@@ -0,0 +1,61 @@\n+// SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+#pragma once\n+\n+/* Constants related to the buddy allocator's object caches. */\n+enum buddy_cache_consts {\n+\t/*\n+\t * Do not cache larger allocations to avoid excessive\n+\t * memory consumption from the per-cpu lists.\n+\t */\n+\tBUDDY_CACHE_NUM_ORDERS = 1 \u003c\u003c 3,\n+\n+\t/*\n+\t * Just like arena spinlocks, hardcode percpu state to a maximum number of CPUs.\n+\t * Since arena spinlocks are a hard dependency, use the exact same limit.\n+\t */\n+\tBUDDY_CACHE_MAX_CPUS = 1024,\n+\n+\t/*\n+\t * Items transferred as one batch. This must remain a power of two because\n+\t * buddy_alloc_bulk() expresses the number of objects as an order.\n+\t */\n+\tBUDDY_CACHE_BATCH_SIZE = 8,\n+\n+\t/*\n+\t * Objects retained locally for each allocation order.\n+\t */\n+\tBUDDY_CACHE_CAPACITY = BUDDY_CACHE_BATCH_SIZE * 2,\n+\n+\t/* Number of objects shared between per-CPU caches for each order. */\n+\tBUDDY_TRANSFER_CAPACITY = BUDDY_CACHE_BATCH_SIZE * 8,\n+};\n+\n+struct buddy_cache_order {\n+\tu32 elems;\n+\tu32 __pad;\n+\tvoid __arena *objects[BUDDY_CACHE_CAPACITY];\n+} __attribute__((aligned(64)));\n+\n+/* Dynamically allocated packed per-CPU slot slab. */\n+struct buddy_cache {\n+\tstruct buddy_cache_order orders[BUDDY_CACHE_NUM_ORDERS];\n+};\n+\n+struct buddy_transfer_order {\n+\tarena_spinlock_t lock;\n+\tu32 elems;\n+\tvoid __arena *objects[BUDDY_TRANSFER_CAPACITY];\n+} __attribute__((aligned(64)));\n+\n+struct buddy_cache_state {\n+\tstruct buddy_cache __arena *percpu[BUDDY_CACHE_MAX_CPUS];\n+\tstruct buddy_transfer_order transfer[BUDDY_CACHE_NUM_ORDERS];\n+\tbool valid;\n+};\n+\n+/* Per-CPU guard against nested or preempting cache operations. */\n+\n+struct __attribute__((aligned(64))) buddy_cache_active {\n+\tunsigned int value;\n+};\ndiff --git a/tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c b/tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c\nindex 5628f09870121..f14c2945d1e11 100644\n--- a/tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c\n+++ b/tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c\n@@ -4,6 +4,7 @@\n #include \u003clibarena/common.h\u003e\n \n #include \u003clibarena/asan.h\u003e\n+#include \u003clibarena/buddy_cache.h\u003e\n #include \u003clibarena/buddy.h\u003e\n \n extern struct buddy __arena buddy;\n@@ -17,6 +18,24 @@ struct segarr_entry {\n #define SEGARRLEN (512)\n static struct segarr_entry __arena segarr[SEGARRLEN];\n static void __arena *ptrs[17];\n+\n+enum buddy_alloc_path_test_consts {\n+\tTEST_BULK_NORDER = 3,\n+\tTEST_BULK_ITEMS = 1 \u003c\u003c TEST_BULK_NORDER,\n+\tTEST_BULK_SIZE = 17,\n+\tTEST_BULK_STEP = 32,\n+\tTEST_CACHE_ORDER = 2,\n+\tTEST_CACHE_SIZE = BUDDY_MIN_ALLOC_BYTES \u003c\u003c TEST_CACHE_ORDER,\n+};\n+\n+struct buddy_alloc_path_test_storage {\n+\tvoid __arena *bulk[BUDDY_CACHE_CAPACITY + 1];\n+\tvoid __arena *to_free[BUDDY_CACHE_CAPACITY + 1];\n+\tvoid __arena *drain[BUDDY_CACHE_BATCH_SIZE];\n+};\n+\n+static struct buddy_alloc_path_test_storage __arena alloc_path_test;\n+\n size_t __arena alloc_sizes[] = { 3, 17, 1025, 129, 16350, 333, 9, 517 };\n size_t __arena alloc_multiple_sizes[] = { 3, 17, 1025, 129, 16350, 333, 9, 517, 2099 };\n size_t __arena alloc_free_sizes[] = { 3, 17, 64, 129, 256, 333, 512, 517 };\n@@ -34,9 +53,20 @@ __weak int test_buddy_create(void)\n \t\tif (ret)\n \t\t\treturn ret;\n \n+#ifndef BPF_ARENA_ASAN\n+\t\tif (!buddy.cache_state.valid) {\n+\t\t\tbuddy_destroy(\u0026buddy);\n+\t\t\treturn -EINVAL;\n+\t\t}\n+#endif\n+\n \t\tret = buddy_destroy(\u0026buddy);\n \t\tif (ret)\n \t\t\treturn ret;\n+\t\tif (buddy.deferred_free)\n+\t\t\treturn -EINVAL;\n+\t\tif (buddy.cache_state.valid)\n+\t\t\treturn -EINVAL;\n \t}\n \n \treturn 0;\n@@ -210,4 +240,251 @@ __weak int test_buddy_alignment(void)\n \treturn 0;\n }\n \n+SEC(\"syscall\")\n+__weak int test_buddy_bulk_alloc_free(void)\n+{\n+\tconst size_t uncached_size =\n+\t\t(BUDDY_MIN_ALLOC_BYTES \u003c\u003c (BUDDY_CACHE_NUM_ORDERS - 1)) + 1;\n+\tvoid __arena *sentinel = (void __arena *)\u0026alloc_path_test;\n+\tu8 __arena *mem;\n+\tunsigned int i, j, idx;\n+\tint ret, destroy_ret;\n+\n+\tret = buddy_init(\u0026buddy);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\t/*\n+\t * A 17-byte request rounds up to a 32-byte buddy allocation. Verify\n+\t * that the bulk allocator returns every child at the expected stride,\n+\t * without writing beyond the requested output array.\n+\t */\n+\talloc_path_test.bulk[TEST_BULK_ITEMS] = sentinel;\n+\tret = buddy_alloc_bulk(\u0026buddy, TEST_BULK_SIZE, TEST_BULK_NORDER,\n+\t\t\t       alloc_path_test.bulk);\n+\tif (ret)\n+\t\tgoto out;\n+\tif (alloc_path_test.bulk[TEST_BULK_ITEMS] != sentinel) {\n+\t\tret = -EOVERFLOW;\n+\t\tgoto out;\n+\t}\n+\n+\tfor (i = zero; i \u003c TEST_BULK_ITEMS \u0026\u0026 can_loop; i++) {\n+\t\tmem = alloc_path_test.bulk[i];\n+\t\tif (!mem) {\n+\t\t\tret = -ENOMEM;\n+\t\t\tgoto out;\n+\t\t}\n+\t\tif (i \u0026\u0026 (u64)mem !=\n+\t\t\t (u64)alloc_path_test.bulk[i - 1] + TEST_BULK_STEP) {\n+\t\t\tret = -EINVAL;\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tfor (j = zero; j \u003c TEST_BULK_SIZE \u0026\u0026 can_loop; j++)\n+\t\t\tmem[j] = (u8)(i + 1);\n+\t}\n+\n+\tfor (i = zero; i \u003c TEST_BULK_ITEMS \u0026\u0026 can_loop; i++) {\n+\t\tmem = alloc_path_test.bulk[i];\n+\t\tfor (j = zero; j \u003c TEST_BULK_SIZE \u0026\u0026 can_loop; j++) {\n+\t\t\tif (mem[j] != (u8)(i + 1)) {\n+\t\t\t\tret = -EINVAL;\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t}\n+\n+\t\t/* Five is coprime to eight, producing a permutation of all items. */\n+\t\tidx = (i * 5) \u0026 (TEST_BULK_ITEMS - 1);\n+\t\talloc_path_test.to_free[i] = alloc_path_test.bulk[idx];\n+\t}\n+\talloc_path_test.to_free[TEST_BULK_ITEMS] = NULL;\n+\n+\tret = buddy_free_bulk(\u0026buddy, alloc_path_test.to_free,\n+\t\t\t      TEST_BULK_ITEMS + 1);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\t/*\n+\t * Order eight is immediately above the per-CPU cache range. Free a bulk\n+\t * allocation one child at a time in a non-sequential order to exercise\n+\t * independent metadata and coalescing without the cache intercepting it.\n+\t */\n+\tret = buddy_alloc_bulk(\u0026buddy, uncached_size, TEST_BULK_NORDER,\n+\t\t\t       alloc_path_test.bulk);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tfor (i = zero; i \u003c TEST_BULK_ITEMS \u0026\u0026 can_loop; i++) {\n+\t\tidx = (i * 5) \u0026 (TEST_BULK_ITEMS - 1);\n+\t\tret = buddy_free(\u0026buddy, alloc_path_test.bulk[idx]);\n+\t\tif (ret)\n+\t\t\tgoto out;\n+\t}\n+\n+out:\n+\tdestroy_ret = buddy_destroy(\u0026buddy);\n+\treturn ret ?: destroy_ret;\n+}\n+\n+SEC(\"syscall\")\n+__weak int test_buddy_cache_paths(void)\n+{\n+#ifdef BPF_ARENA_ASAN\n+\treturn 0;\n+#else\n+\tstruct buddy_cache_state __arena *state;\n+\tstruct buddy_cache __arena *cache;\n+\tstruct buddy_cache_order __arena *slots;\n+\tstruct buddy_transfer_order __arena *transfer;\n+\tvoid __arena *direct, *cached, *reused, *refilled, *overflow_reused;\n+\tvoid __arena *transferred;\n+\tunsigned int cpu, i;\n+\tint ret, destroy_ret;\n+\n+\tret = buddy_init(\u0026buddy);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tstate = \u0026buddy.cache_state;\n+\n+\tcpu = bpf_get_smp_processor_id();\n+\tcache = state-\u003epercpu[cpu];\n+\tif (!cache) {\n+\t\tret = -EINVAL;\n+\t\tgoto out;\n+\t}\n+\tslots = \u0026cache-\u003eorders[TEST_CACHE_ORDER];\n+\ttransfer = \u0026state-\u003etransfer[TEST_CACHE_ORDER];\n+\tif (READ_ONCE(slots-\u003eelems) || READ_ONCE(transfer-\u003eelems)) {\n+\t\tret = -EINVAL;\n+\t\tgoto out;\n+\t}\n+\n+\t/* A miss bulk-refills the local order and returns one object directly. */\n+\tdirect = buddy_alloc(\u0026buddy, TEST_CACHE_SIZE);\n+\tif (!direct || READ_ONCE(slots-\u003eelems) != BUDDY_CACHE_BATCH_SIZE - 1) {\n+\t\tret = -ENOMEM;\n+\t\tgoto out;\n+\t}\n+\n+\t/* Subsequent allocations and frees stay on the packed local stack. */\n+\tcached = buddy_alloc(\u0026buddy, TEST_CACHE_SIZE);\n+\tif (!cached || READ_ONCE(slots-\u003eelems) != BUDDY_CACHE_BATCH_SIZE - 2) {\n+\t\tret = -EINVAL;\n+\t\tgoto out;\n+\t}\n+\n+\tret = buddy_free(\u0026buddy, cached);\n+\tif (ret)\n+\t\tgoto out;\n+\treused = buddy_alloc(\u0026buddy, TEST_CACHE_SIZE);\n+\tif (reused != cached) {\n+\t\tret = -EINVAL;\n+\t\tgoto out;\n+\t}\n+\n+\t/* Drain the local stack, then verify that the next miss refills it. */\n+\tfor (i = zero; i \u003c BUDDY_CACHE_BATCH_SIZE - 2 \u0026\u0026 can_loop; i++) {\n+\t\talloc_path_test.drain[i] = buddy_alloc(\u0026buddy, TEST_CACHE_SIZE);\n+\t\tif (!alloc_path_test.drain[i]) {\n+\t\t\tret = -ENOMEM;\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\tif (READ_ONCE(slots-\u003eelems)) {\n+\t\tret = -EINVAL;\n+\t\tgoto out;\n+\t}\n+\n+\trefilled = buddy_alloc(\u0026buddy, TEST_CACHE_SIZE);\n+\tif (!refilled ||\n+\t    READ_ONCE(slots-\u003eelems) != BUDDY_CACHE_BATCH_SIZE - 1) {\n+\t\tret = -EINVAL;\n+\t\tgoto out;\n+\t}\n+\n+\t/* Bulk allocation bypasses the cache. Fill the remaining local slots. */\n+\tret = buddy_alloc_bulk(\u0026buddy, TEST_CACHE_SIZE,\n+\t\t\t       arena_fls(BUDDY_CACHE_CAPACITY) - 1,\n+\t\t\t       alloc_path_test.bulk);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tret = buddy_free(\u0026buddy, alloc_path_test.bulk[0]);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tret = buddy_free(\u0026buddy, alloc_path_test.bulk[1]);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tfor (i = 2;\n+\t     i \u003c= BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE \u0026\u0026 can_loop;\n+\t     i++) {\n+\t\tret = buddy_free(\u0026buddy, alloc_path_test.bulk[i]);\n+\t\tif (ret)\n+\t\t\tgoto out;\n+\t}\n+\tif (READ_ONCE(slots-\u003eelems) != BUDDY_CACHE_CAPACITY) {\n+\t\tret = -EINVAL;\n+\t\tgoto out;\n+\t}\n+\n+\t/* A free beyond local capacity transfers a batch to the shared cache. */\n+\tret = buddy_free(\u0026buddy,\n+\t\t\t alloc_path_test.bulk[BUDDY_CACHE_CAPACITY -\n+\t\t\t\t\t      BUDDY_CACHE_BATCH_SIZE + 1]);\n+\tif (ret)\n+\t\tgoto out;\n+\tif (READ_ONCE(slots-\u003eelems) !=\n+\t\t\tBUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE + 1 ||\n+\t    READ_ONCE(transfer-\u003eelems) != BUDDY_CACHE_BATCH_SIZE) {\n+\t\tret = -EINVAL;\n+\t\tgoto out;\n+\t}\n+\n+\toverflow_reused = buddy_alloc(\u0026buddy, TEST_CACHE_SIZE);\n+\tif (overflow_reused != alloc_path_test.bulk[BUDDY_CACHE_CAPACITY -\n+\t\t\t\t\t\t\t BUDDY_CACHE_BATCH_SIZE + 1] ||\n+\t    READ_ONCE(slots-\u003eelems) !=\n+\t\t\tBUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE) {\n+\t\tret = -EINVAL;\n+\t\tgoto out;\n+\t}\n+\n+\t/* Drain locally, then consume the batch without touching buddy. */\n+\tfor (i = zero;\n+\t     i \u003c BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE \u0026\u0026 can_loop;\n+\t     i++) {\n+\t\talloc_path_test.drain[i] = buddy_alloc(\u0026buddy, TEST_CACHE_SIZE);\n+\t\tif (!alloc_path_test.drain[i]) {\n+\t\t\tret = -ENOMEM;\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\ttransferred = buddy_alloc(\u0026buddy, TEST_CACHE_SIZE);\n+\tif (transferred != alloc_path_test.bulk[BUDDY_CACHE_CAPACITY -\n+\t\t\t\t\t\t     BUDDY_CACHE_BATCH_SIZE] ||\n+\t    READ_ONCE(slots-\u003eelems) != BUDDY_CACHE_BATCH_SIZE - 1 ||\n+\t    READ_ONCE(transfer-\u003eelems)) {\n+\t\tret = -EINVAL;\n+\t\tgoto out;\n+\t}\n+\n+\t/* Interior pointers are rejected without consuming the allocation. */\n+\ti = BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE + 2;\n+\tif (!buddy_free(\u0026buddy, (u8 __arena *)alloc_path_test.bulk[i] +\n+\t\t\t\t BUDDY_MIN_ALLOC_BYTES)) {\n+\t\tret = -EINVAL;\n+\t\tgoto out;\n+\t}\n+\tret = buddy_free(\u0026buddy, alloc_path_test.bulk[i]);\n+\n+out:\n+\tdestroy_ret = buddy_destroy(\u0026buddy);\n+\treturn ret ?: destroy_ret;\n+#endif\n+}\n+\n __weak char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c b/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c\nindex 2490ab1396de0..796dacf3c6a2f 100644\n--- a/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c\n+++ b/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c\n@@ -4,8 +4,13 @@\n #include \u003clibarena/common.h\u003e\n #include \u003clibarena/asan.h\u003e\n #include \u003clibarena/buddy.h\u003e\n+#include \u003clibarena/buddy_cache.h\u003e\n \n #include \u003cbpf_arena_spin_lock.h\u003e\n+#include \u003cbpf_experimental.h\u003e\n+\n+_Static_assert(BUDDY_CACHE_MAX_CPUS == _Q_MAX_CPUS,\n+\t       \"buddy caches must cover every CPU arena spinlocks support\");\n \n /*\n  * Buddy allocator arena-based implementation.\n@@ -38,18 +43,303 @@\n  * and their allocation state. It also includes the freelist\n  * heads for the allocation itself.\n  */\n+static u8 idx_get_order(struct buddy_chunk __arena *chunk, u64 idx);\n+static u64 arena_next_pow2(__u64 n);\n+static u64 size_to_order(size_t size);\n+static void __arena *buddy_alloc_memory(struct buddy __arena *buddy, size_t size,\n+\t\t\t\t\tint order, size_t norder);\n+static int buddy_free_unlocked(struct buddy __arena *buddy, u64 addr);\n+static bool buddy_deferred_free_pending(struct buddy __arena *buddy);\n+static int buddy_deferred_free_complete(struct buddy __arena *buddy);\n+static struct buddy_cache_active active[BUDDY_CACHE_MAX_CPUS];\n+\n+struct buddy_cache_ctx {\n+\tstruct buddy_cache __arena *cache;\n+\tstruct buddy_cache_active *active;\n+};\n+\n+#define buddy_lock(buddy, flags) (arena_spin_lock_irqsave(\u0026(buddy)-\u003elock, (flags)))\n+#define buddy_unlock(buddy, flags) (arena_spin_unlock_irqrestore(\u0026(buddy)-\u003elock, (flags)))\n+\n+/*\n+ * Per-CPU caching is disabled in ASAN builds because its additional call-chain\n+ * stack usage causes instrumented programs to exceed BPF's 512-byte stack limit.\n+ */\n+#ifdef BPF_ARENA_ASAN\n+#define DISABLE_CACHE_IF_ASAN(...) do { return (__VA_ARGS__); } while (0)\n+#else\n+#define DISABLE_CACHE_IF_ASAN(...)\n+#endif\n+\n+static __always_inline int\n+buddy_cache_enter(struct buddy_cache_state __arena *state,\n+\t\t  struct buddy_cache_ctx *ctx)\n+{\n+\tunsigned int cpu;\n+\n+\t/*\n+\t * No support for cache operations from contexts that interrupted or\n+\t * preempted an in-flight operation on the same CPU, just like the\n+\t * kernel's BPF memory allocator.\n+\t */\n+\tcpu = bpf_get_smp_processor_id();\n+\tctx-\u003ecache = state-\u003epercpu[cpu];\n+\tctx-\u003eactive = \u0026active[cpu];\n+\tif (cmpxchg(\u0026ctx-\u003eactive-\u003evalue, 0, 1))\n+\t\treturn -EAGAIN;\n+\n+\treturn 0;\n+}\n+\n+static __always_inline void buddy_cache_exit(struct buddy_cache_ctx *ctx)\n+{\n+\tsmp_store_release(\u0026ctx-\u003eactive-\u003evalue, 0);\n+}\n+\n+static __always_inline void __arena *\n+buddy_cache_pop(struct buddy_cache __arena *cache, int order)\n+{\n+\tstruct buddy_cache_order __arena *slots;\n+\tvoid __arena *addr;\n+\tu32 elems;\n+\n+\tslots = \u0026cache-\u003eorders[order];\n+\telems = READ_ONCE(slots-\u003eelems);\n+\tif (!elems)\n+\t\treturn NULL;\n+\n+\taddr = READ_ONCE(slots-\u003eobjects[elems - 1]);\n+\tWRITE_ONCE(slots-\u003eelems, elems - 1);\n+\n+\treturn addr;\n+}\n+\n+static __always_inline int buddy_cache_push(struct buddy_cache __arena *cache,\n+\t\t\t\t\t    int order, void __arena *obj)\n+{\n+\tstruct buddy_cache_order __arena *slots;\n+\tu32 elems;\n+\n+\tslots = \u0026cache-\u003eorders[order];\n+\telems = READ_ONCE(slots-\u003eelems);\n+\tif (elems \u003e= BUDDY_CACHE_CAPACITY)\n+\t\treturn -ENOMEM;\n+\n+\tWRITE_ONCE(slots-\u003eobjects[elems], obj);\n+\tWRITE_ONCE(slots-\u003eelems, elems + 1);\n+\n+\treturn 0;\n+}\n+\n+static int buddy_transfer_take_batch(struct buddy_cache_state __arena *state,\n+\t\t\t\t     int order,\n+\t\t\t\t     struct buddy_cache_order __arena *slots)\n+{\n+\tstruct buddy_transfer_order __arena *transfer;\n+\tunsigned long flags;\n+\tu32 elems, start;\n+\tunsigned int i;\n+\tint ret = 0;\n+\n+\ttransfer = \u0026state-\u003etransfer[order];\n+\tif (arena_spin_lock_irqsave(\u0026transfer-\u003elock, flags))\n+\t\treturn -EAGAIN;\n+\n+\telems = READ_ONCE(transfer-\u003eelems);\n+\tif (elems \u003c BUDDY_CACHE_BATCH_SIZE) {\n+\t\tret = -ENOENT;\n+\t\tgoto out_unlock;\n+\t}\n+\n+\tstart = elems - BUDDY_CACHE_BATCH_SIZE;\n+\tfor (i = zero; i \u003c BUDDY_CACHE_BATCH_SIZE \u0026\u0026 can_loop; i++)\n+\t\tWRITE_ONCE(slots-\u003eobjects[i],\n+\t\t\t   READ_ONCE(transfer-\u003eobjects[start + i]));\n+\n+\tWRITE_ONCE(transfer-\u003eelems, start);\n+\tWRITE_ONCE(slots-\u003eelems, BUDDY_CACHE_BATCH_SIZE);\n+\n+out_unlock:\n+\tarena_spin_unlock_irqrestore(\u0026transfer-\u003elock, flags);\n+\treturn ret;\n+}\n+\n+static int buddy_transfer_put_batch(struct buddy_cache_state __arena *state,\n+\t\t\t\t    int order,\n+\t\t\t\t    struct buddy_cache_order __arena *slots)\n+{\n+\tstruct buddy_transfer_order __arena *transfer;\n+\tunsigned long flags;\n+\tu32 elems, local_elems, start;\n+\tunsigned int i;\n+\tint ret = 0;\n+\n+\ttransfer = \u0026state-\u003etransfer[order];\n+\tif (arena_spin_lock_irqsave(\u0026transfer-\u003elock, flags))\n+\t\treturn -EAGAIN;\n+\n+\telems = READ_ONCE(transfer-\u003eelems);\n+\tif (elems \u003e BUDDY_TRANSFER_CAPACITY - BUDDY_CACHE_BATCH_SIZE) {\n+\t\tret = -ENOMEM;\n+\t\tgoto out_unlock;\n+\t}\n+\n+\tlocal_elems = READ_ONCE(slots-\u003eelems);\n+\tif (local_elems \u003c BUDDY_CACHE_BATCH_SIZE) {\n+\t\tret = -EINVAL;\n+\t\tgoto out_unlock;\n+\t}\n+\n+\tstart = local_elems - BUDDY_CACHE_BATCH_SIZE;\n+\tfor (i = zero; i \u003c BUDDY_CACHE_BATCH_SIZE \u0026\u0026 can_loop; i++)\n+\t\tWRITE_ONCE(transfer-\u003eobjects[elems + i],\n+\t\t\t   READ_ONCE(slots-\u003eobjects[start + i]));\n+\n+\tWRITE_ONCE(slots-\u003eelems, start);\n+\tWRITE_ONCE(transfer-\u003eelems, elems + BUDDY_CACHE_BATCH_SIZE);\n+\n+out_unlock:\n+\tarena_spin_unlock_irqrestore(\u0026transfer-\u003elock, flags);\n+\treturn ret;\n+}\n+\n+void __arena *buddy_cache_alloc(struct buddy __arena *buddy, size_t order)\n+{\n+\tstruct buddy_cache_state __arena *state = \u0026buddy-\u003ecache_state;\n+\tstruct buddy_cache_ctx ctx;\n+\tvoid __arena *address = NULL;\n+\n+\tDISABLE_CACHE_IF_ASAN(NULL);\n+\n+\tif (order \u003e= BUDDY_CACHE_NUM_ORDERS)\n+\t\treturn NULL;\n+\n+\tif (buddy_cache_enter(state, \u0026ctx))\n+\t\treturn NULL;\n+\n+\taddress = buddy_cache_pop(ctx.cache, order);\n+\tbuddy_cache_exit(\u0026ctx);\n+\n+\treturn address;\n+}\n+\n+/* Retrieve the order of an allocation from the address. */\n+static int buddy_cache_get_order(void __arena *addr)\n+{\n+\tstruct buddy_chunk __arena *chunk;\n+\tu64 idx;\n \n+\tchunk = (void __arena *)((u64)addr \u0026 ~BUDDY_CHUNK_OFFSET_MASK);\n+\tidx = ((u64)addr \u0026 BUDDY_CHUNK_OFFSET_MASK) / BUDDY_MIN_ALLOC_BYTES;\n+\n+\treturn idx_get_order(chunk, idx);\n+}\n+\n+int buddy_cache_free(struct buddy __arena *buddy, void __arena *addr)\n+{\n+\tstruct buddy_cache_state __arena *state = \u0026buddy-\u003ecache_state;\n+\tstruct buddy_cache_ctx ctx;\n+\tint order;\n+\tint ret = 0;\n+\n+\tDISABLE_CACHE_IF_ASAN(-EINVAL);\n+\n+\torder = buddy_cache_get_order(addr);\n+\tif (order \u003e= BUDDY_CACHE_NUM_ORDERS)\n+\t\treturn -EINVAL;\n+\n+\tif (buddy_cache_enter(state, \u0026ctx))\n+\t\treturn -EAGAIN;\n+\n+\tret = buddy_cache_push(ctx.cache, order, addr);\n+\tbuddy_cache_exit(\u0026ctx);\n+\n+\treturn ret;\n+}\n+\n+static int buddy_cache_transfer_free(struct buddy __arena *buddy,\n+\t\t\t\t     void __arena *addr)\n+{\n+\tstruct buddy_cache_state __arena *state = \u0026buddy-\u003ecache_state;\n+\tstruct buddy_cache_order __arena *slots;\n+\tstruct buddy_cache_ctx ctx;\n+\tint order, ret;\n+\n+\tDISABLE_CACHE_IF_ASAN(-EINVAL);\n+\n+\torder = buddy_cache_get_order(addr);\n+\tif (order \u003e= BUDDY_CACHE_NUM_ORDERS)\n+\t\treturn -EINVAL;\n+\n+\tif (buddy_cache_enter(state, \u0026ctx))\n+\t\treturn -EAGAIN;\n+\n+\t/* An allocation may have made room since the first local attempt. */\n+\tret = buddy_cache_push(ctx.cache, order, addr);\n+\tif (!ret)\n+\t\tgoto done;\n+\n+\tslots = \u0026ctx.cache-\u003eorders[order];\n+\tret = buddy_transfer_put_batch(state, order, slots);\n+\tif (!ret)\n+\t\tret = buddy_cache_push(ctx.cache, order, addr);\n+\n+done:\n+\tbuddy_cache_exit(\u0026ctx);\n+\treturn ret;\n+}\n+\n+static void __arena *buddy_cache_refill(struct buddy __arena *buddy,\n+\t\t\t\t\tsize_t size, int order)\n+{\n+\tstruct buddy_cache_state __arena *state = \u0026buddy-\u003ecache_state;\n+\tstruct buddy_cache_order __arena *slots;\n+\tstruct buddy_cache_ctx ctx;\n+\tvoid __arena *address = NULL;\n+\tint ret;\n+\n+\tDISABLE_CACHE_IF_ASAN(NULL);\n+\n+\tif (order \u003e= BUDDY_CACHE_NUM_ORDERS)\n+\t\treturn NULL;\n+\n+\tif (buddy_cache_enter(state, \u0026ctx))\n+\t\treturn NULL;\n+\n+\t/* A free may have populated the cache between the miss and refill. */\n+\taddress = buddy_cache_pop(ctx.cache, order);\n+\tif (address)\n+\t\tgoto done;\n+\n+\tslots = \u0026ctx.cache-\u003eorders[order];\n+\tret = buddy_transfer_take_batch(state, order, slots);\n+\tif (!ret) {\n+\t\taddress = buddy_cache_pop(ctx.cache, order);\n+\t\tgoto done;\n+\t}\n+\n+\tret = buddy_alloc_bulk(buddy, size,\n+\t\t\t\tarena_fls(BUDDY_CACHE_BATCH_SIZE) - 1,\n+\t\t\t\t(void __arena * __arena *)slots-\u003eobjects);\n+\tif (ret)\n+\t\tgoto done;\n+\n+\taddress = slots-\u003eobjects[BUDDY_CACHE_BATCH_SIZE - 1];\n+\tWRITE_ONCE(slots-\u003eelems, BUDDY_CACHE_BATCH_SIZE - 1);\n+\n+done:\n+\tbuddy_cache_exit(\u0026ctx);\n+\n+\treturn address;\n+}\n \n enum {\n \tBUDDY_POISONED = (s8)0xef,\n \n \t/* Number of pages to be allocated per chunk. */\n-\tBUDDY_CHUNK_PAGES\t= BUDDY_CHUNK_BYTES / __PAGE_SIZE\n+\tBUDDY_CHUNK_PAGES\t= BUDDY_CHUNK_BYTES / __PAGE_SIZE,\n };\n \n-#define buddy_lock(buddy, flags) (arena_spin_lock_irqsave(\u0026(buddy)-\u003elock, (flags)))\n-#define buddy_unlock(buddy, flags) (arena_spin_unlock_irqrestore(\u0026(buddy)-\u003elock, (flags)))\n-\n /*\n  * Reserve part of the arena address space for the allocator. We use\n  * this to get aligned addresses for the chunks, since the arena\n@@ -557,6 +847,49 @@ static struct buddy_chunk __arena *buddy_chunk_get(struct buddy __arena *buddy)\n \treturn chunk;\n }\n \n+static int buddy_cache_state_init(struct buddy __arena *buddy)\n+{\n+\tstruct buddy_cache_state __arena *state = \u0026buddy-\u003ecache_state;\n+\tstruct buddy_cache __arena *cache;\n+\tunsigned int cpu, order;\n+\tint ret;\n+\n+\tDISABLE_CACHE_IF_ASAN(0);\n+\n+\tWRITE_ONCE(state-\u003evalid, false);\n+\tif (CONFIG_NR_CPUS \u003e BUDDY_CACHE_MAX_CPUS)\n+\t\treturn -E2BIG;\n+\n+\tfor (order = zero; order \u003c BUDDY_CACHE_NUM_ORDERS \u0026\u0026 can_loop; order++)\n+\t\tWRITE_ONCE(state-\u003etransfer[order].elems, 0);\n+\n+\tfor (cpu = zero; cpu \u003c CONFIG_NR_CPUS \u0026\u0026 can_loop; cpu++) {\n+\t\tret = buddy_alloc_bulk(buddy, sizeof(*cache), 0,\n+\t\t\t\t       (void __arena * __arena *)\u0026state-\u003epercpu[cpu]);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\n+\t\tcache = state-\u003epercpu[cpu];\n+\t\tfor (order = zero; order \u003c BUDDY_CACHE_NUM_ORDERS \u0026\u0026 can_loop; order++)\n+\t\t\tWRITE_ONCE(cache-\u003eorders[order].elems, 0);\n+\t}\n+\n+\tWRITE_ONCE(state-\u003evalid, true);\n+\treturn 0;\n+}\n+\n+static void buddy_cache_state_destroy(struct buddy __arena *buddy)\n+{\n+\tstruct buddy_cache_state __arena *state = \u0026buddy-\u003ecache_state;\n+\tunsigned int cpu, order;\n+\n+\tWRITE_ONCE(state-\u003evalid, false);\n+\tfor (cpu = zero; cpu \u003c CONFIG_NR_CPUS \u0026\u0026 can_loop; cpu++)\n+\t\tWRITE_ONCE(state-\u003epercpu[cpu], NULL);\n+\tfor (order = zero; order \u003c BUDDY_CACHE_NUM_ORDERS \u0026\u0026 can_loop; order++)\n+\t\tWRITE_ONCE(state-\u003etransfer[order].elems, 0);\n+}\n+\n __weak int buddy_init(struct buddy __arena *buddy)\n {\n \tstruct buddy_chunk __arena *chunk;\n@@ -565,6 +898,7 @@ __weak int buddy_init(struct buddy __arena *buddy)\n \n \tif (!asan_ready())\n \t\treturn -EINVAL;\n+\tbuddy-\u003edeferred_free = NULL;\n \n \t/* Reserve enough address space to ensure allocations are aligned. */\n \tret = buddy_reserve_arena_vaddr(buddy);\n@@ -590,7 +924,16 @@ __weak int buddy_init(struct buddy __arena *buddy)\n \n \tbuddy_unlock(buddy, flags);\n \n-\treturn chunk ? 0 : -ENOMEM;\n+\tif (!chunk)\n+\t\treturn -ENOMEM;\n+\n+\tret = buddy_cache_state_init(buddy);\n+\tif (ret) {\n+\t\tbuddy_destroy(buddy);\n+\t\treturn ret;\n+\t}\n+\n+\treturn 0;\n }\n \n /*\n@@ -606,6 +949,8 @@ __weak int buddy_destroy(struct buddy __arena *buddy)\n \tif (!buddy)\n \t\treturn -EINVAL;\n \n+\tbuddy_cache_state_destroy(buddy);\n+\n \t/*\n \t * Traverse all buddy chunks and free them back to the arena\n \t * with the same granularity they were allocated with.\n@@ -624,11 +969,12 @@ __weak int buddy_destroy(struct buddy __arena *buddy)\n \n \t/* Clear all fields. */\n \tbuddy-\u003efirst_chunk = NULL;\n+\tbuddy-\u003edeferred_free = NULL;\n \n \treturn 0;\n }\n \n-__weak u64 buddy_chunk_alloc(struct buddy_chunk __arena *chunk, int order_req)\n+__weak u64 buddy_chunk_alloc(struct buddy_chunk __arena *chunk, int order_req, int order_allocs)\n {\n \tstruct buddy_header __arena *header, *tmp_header, *next_header;\n \tu32 idx, tmpidx, retidx;\n@@ -636,7 +982,7 @@ __weak u64 buddy_chunk_alloc(struct buddy_chunk __arena *chunk, int order_req)\n \tu64 order = 0;\n \tu64 i;\n \n-\tfor (order = order_req; order \u003c BUDDY_CHUNK_NUM_ORDERS \u0026\u0026 can_loop; order++) {\n+\tfor (order = order_req + order_allocs; order \u003c BUDDY_CHUNK_NUM_ORDERS \u0026\u0026 can_loop; order++) {\n \t\tif (chunk-\u003efreelists[order] != BUDDY_CHUNK_ITEMS)\n \t\t\tbreak;\n \t}\n@@ -658,11 +1004,19 @@ __weak u64 buddy_chunk_alloc(struct buddy_chunk __arena *chunk, int order_req)\n \n \theader-\u003eprev_index = BUDDY_CHUNK_ITEMS;\n \theader-\u003enext_index = BUDDY_CHUNK_ITEMS;\n-\tif (idx_set_order(chunk, retidx, order_req))\n-\t\treturn (u64)NULL;\n+\t/*\n+\t * For a bulk allocation, record the allocation state and size\n+\t * of each item separately so each can be freed independently.\n+\t */\n+\tfor (i = zero, idx = retidx; i \u003c (1U \u003c\u003c order_allocs) \u0026\u0026 can_loop; i++) {\n+\t\tif (idx_set_order(chunk, idx, order_req))\n+\t\t\treturn (u64)NULL;\n \n-\tif (idx_set_allocated(chunk, retidx, true))\n-\t\treturn (u64)NULL;\n+\t\tif (idx_set_allocated(chunk, idx, true))\n+\t\t\treturn (u64)NULL;\n+\n+\t\tidx += 1U \u003c\u003c order_req;\n+\t}\n \n \t/*\n \t * Do not unpoison the address yet, will be done by the caller\n@@ -673,7 +1027,7 @@ __weak u64 buddy_chunk_alloc(struct buddy_chunk __arena *chunk, int order_req)\n \t\treturn (u64)NULL;\n \n \t/* If we allocated from a larger-order chunk, split the buddies. */\n-\tfor (i = order_req; i \u003c order \u0026\u0026 can_loop; i++) {\n+\tfor (i = order_req + order_allocs; i \u003c order \u0026\u0026 can_loop; i++) {\n \t\t/*\n \t\t * Flip the bit for the current order (the bit is guaranteed\n \t\t * to be 0, so just add 1 \u003c\u003c i).\n@@ -706,14 +1060,14 @@ __weak u64 buddy_chunk_alloc(struct buddy_chunk __arena *chunk, int order_req)\n }\n \n /* Scan the existing chunks for available memory. */\n-static u64 buddy_alloc_from_existing_chunks(struct buddy __arena *buddy, int order)\n+static u64 buddy_alloc_from_existing_chunks(struct buddy __arena *buddy, int order, int norder)\n {\n \tstruct buddy_chunk __arena *chunk;\n \tu64 address;\n \n \tfor (chunk = buddy-\u003efirst_chunk; chunk != NULL \u0026\u0026 can_loop;\n \t     chunk = chunk-\u003enext) {\n-\t\taddress = buddy_chunk_alloc(chunk, order);\n+\t\taddress = buddy_chunk_alloc(chunk, order, norder);\n \t\tif (address)\n \t\t\treturn address;\n \t}\n@@ -725,7 +1079,8 @@ static u64 buddy_alloc_from_existing_chunks(struct buddy __arena *buddy, int ord\n  * Try an allocation from a newly allocated chunk. Also\n  * incorporate the chunk into the linked list.\n  */\n-static u64 buddy_alloc_from_new_chunk(struct buddy __arena *buddy, struct buddy_chunk __arena *chunk, int order)\n+static u64 buddy_alloc_from_new_chunk(struct buddy __arena *buddy, struct buddy_chunk __arena *chunk,\n+\t\tint order, int norder)\n {\n \tunsigned long flags;\n \tu64 address;\n@@ -733,7 +1088,6 @@ static u64 buddy_alloc_from_new_chunk(struct buddy __arena *buddy, struct buddy_\n \tif (buddy_lock(buddy, flags))\n \t\treturn (u64)NULL;\n \n-\n \t/*\n \t * Add the chunk into the allocator and try\n \t * to allocate specifically from that chunk.\n@@ -741,33 +1095,44 @@ static u64 buddy_alloc_from_new_chunk(struct buddy __arena *buddy, struct buddy_\n \tchunk-\u003enext = buddy-\u003efirst_chunk;\n \tbuddy-\u003efirst_chunk = chunk;\n \n-\taddress = buddy_chunk_alloc(buddy-\u003efirst_chunk, order);\n+\taddress = buddy_chunk_alloc(buddy-\u003efirst_chunk, order, norder);\n \n \tbuddy_unlock(buddy, flags);\n \n \treturn (u64)address;\n }\n-__weak\n-void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size)\n+static void __arena *buddy_alloc_memory(struct buddy __arena *buddy, size_t size,\n+\t\tint order, size_t norder)\n {\n \tvoid __arena *address = NULL;\n+\tu8 __arena *object;\n \tstruct buddy_chunk __arena *chunk;\n+\tunsigned int alloc_size;\n+\tunsigned int i, offset;\n \tunsigned long flags;\n-\tint order;\n \n \tif (!buddy)\n \t\treturn NULL;\n \n-\torder = size_to_order(size);\n-\tif (order \u003e= BUDDY_CHUNK_NUM_ORDERS || order \u003c 0) {\n-\t\tarena_stderr(\"invalid order %d (sz %lu)\\n\", order, size);\n+\tif (order \u003c 0 || order \u003e= BUDDY_CHUNK_NUM_ORDERS || norder \u003e= BUDDY_CHUNK_NUM_ORDERS - order) {\n+\t\tarena_stderr(\"invalid order %d (norder %lu) (sz %lu)\\n\", order, norder, size);\n \t\treturn NULL;\n \t}\n \n+\tif (bpf_in_nmi())\n+\t\treturn NULL;\n+\n \tif (buddy_lock(buddy, flags))\n \t\treturn NULL;\n \n-\taddress = (u8 __arena *)buddy_alloc_from_existing_chunks(buddy, order);\n+\tif (unlikely(buddy_deferred_free_pending(buddy))) {\n+\t\tif (buddy_deferred_free_complete(buddy)) {\n+\t\t\tbuddy_unlock(buddy, flags);\n+\t\t\treturn NULL;\n+\t\t}\n+\t}\n+\n+\taddress = (u8 __arena *)buddy_alloc_from_existing_chunks(buddy, order, norder);\n \tbuddy_unlock(buddy, flags);\n \tif (address)\n \t\tgoto done;\n@@ -775,28 +1140,85 @@ void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size)\n \t/* Get a new chunk. */\n \tchunk = buddy_chunk_get(buddy);\n \tif (chunk)\n-\t\taddress = (u8 __arena *)buddy_alloc_from_new_chunk(buddy, chunk, order);\n+\t\taddress = (u8 __arena *)buddy_alloc_from_new_chunk(buddy, chunk, order, norder);\n \n done:\n \t/* If we failed to allocate memory, return NULL. */\n \tif (!address)\n \t\treturn NULL;\n \n+\talloc_size = BUDDY_MIN_ALLOC_BYTES \u003c\u003c order;\n+\n \t/*\n-\t * Unpoison exactly the amount of bytes requested. If the\n-\t * data is smaller than the header, we must poison any\n-\t * unused bytes that were part of the header.\n+\t * Unpoison exactly the requested bytes in each allocation. Keep\n+\t * the padding between the requested size and the rounded buddy\n+\t * allocation poisoned.\n \t */\n-\tif (size \u003c BUDDY_HEADER_OFF + sizeof(struct buddy_header __arena))\n-\t\tasan_poison(address + BUDDY_HEADER_OFF, BUDDY_POISONED,\n-\t\t\t    sizeof(struct buddy_header __arena));\n+\tfor (i = zero, offset = zero; i \u003c (1U \u003c\u003c norder) \u0026\u0026 can_loop; i++, offset += alloc_size) {\n+\t\tobject = (u8 __arena *)address + offset;\n \n-\tasan_unpoison(address, size);\n+\t\tif (size \u003c BUDDY_HEADER_OFF + sizeof(struct buddy_header __arena))\n+\t\t\tasan_poison(object + BUDDY_HEADER_OFF,\n+\t\t\t\t    BUDDY_POISONED,\n+\t\t\t\t    sizeof(struct buddy_header __arena));\n+\n+\t\tasan_unpoison(object, size);\n+\t}\n \n \treturn address;\n }\n \n-static __always_inline int buddy_free_unlocked(struct buddy __arena *buddy, u64 addr)\n+__weak\n+int buddy_alloc_bulk(struct buddy __arena *buddy, size_t size, size_t norder,\n+\t\tvoid __arena * __arena * out)\n+{\n+\tvoid __arena *address;\n+\tunsigned int i, off;\n+\tunsigned int step;\n+\tint order;\n+\n+\tif (!buddy || !out || norder \u003e= BUDDY_CHUNK_NUM_ORDERS)\n+\t\treturn -EINVAL;\n+\n+\t/*\n+\t * Bulk allocations bypass the per-cpu caches and reserve one buddy\n+\t * block covering all returned objects. The buddy metadata records\n+\t * each object separately so it can later be freed independently.\n+\t */\n+\torder = size_to_order(size);\n+\taddress = buddy_alloc_memory(buddy, size, order, norder);\n+\tif (!address)\n+\t\treturn -ENOMEM;\n+\n+\tstep = BUDDY_MIN_ALLOC_BYTES \u003c\u003c order;\n+\tfor (i = zero, off = zero; i \u003c (1U \u003c\u003c norder) \u0026\u0026 can_loop; i++, off += step)\n+\t\tout[i] = \u0026address[off];\n+\n+\treturn 0;\n+}\n+\n+__weak\n+void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size)\n+{\n+\tvoid __arena *address;\n+\tint order;\n+\n+\torder = size_to_order(size);\n+\taddress = buddy_cache_alloc(buddy, order);\n+\tif (address)\n+\t\treturn address;\n+\n+\tif (bpf_in_nmi())\n+\t\treturn NULL;\n+\n+\taddress = buddy_cache_refill(buddy, size, order);\n+\tif (address)\n+\t\treturn address;\n+\n+\treturn buddy_alloc_memory(buddy, size, order, 0);\n+}\n+\n+static int buddy_free_unlocked(struct buddy __arena *buddy, u64 addr)\n {\n \tstruct buddy_header __arena *header, *buddy_header;\n \tu64 idx, buddy_idx, tmp_idx;\n@@ -877,6 +1299,98 @@ static __always_inline int buddy_free_unlocked(struct buddy __arena *buddy, u64\n \treturn 0;\n }\n \n+static bool buddy_deferred_free_pending(struct buddy __arena *buddy)\n+{\n+\treturn buddy-\u003edeferred_free != NULL;\n+}\n+\n+static void buddy_deferred_free_start(struct buddy __arena *buddy, void __arena *addr)\n+{\n+\tvoid __arena *head;\n+\tvoid __arena * __arena *new;\n+\n+\t/*\n+\t * Make a linked list by chaining the allocations into a linked list.\n+\t * Possible because allocations are 16 bytes minimum and so fit a pointer.\n+\t */\n+\tnew = (void __arena * __arena *)addr;\n+\tasan_unpoison(addr, sizeof(void *));\n+\tdo  {\n+\t\thead = buddy-\u003edeferred_free;\n+\t\t*new = head;\n+\t} while (cmpxchg((void __arena * __arena *)\u0026buddy-\u003edeferred_free,\n+\t\t\t head, (void __arena *)new) != head \u0026\u0026 can_loop);\n+}\n+\n+static void buddy_deferred_free_start_bulk(struct buddy __arena *buddy,\n+\t\t void __arena * __arena *addrs, size_t naddrs)\n+{\n+\tvoid __arena *head, *first, *last, *addr;\n+\tunsigned int i;\n+\n+\t/* Create a linked list locally. */\n+\tfirst = NULL;\n+\tlast = NULL;\n+\tfor (i = zero; i \u003c naddrs \u0026\u0026 can_loop; i++) {\n+\t\taddr = addrs[i];\n+\t\tif (!addr)\n+\t\t\tcontinue;\n+\n+\t\tasan_unpoison(addr, sizeof(void *));\n+\n+\t\tif (!first)\n+\t\t\tfirst = addr;\n+\t\telse\n+\t\t\t*(void __arena * __arena *)last = addr;\n+\n+\t\tlast = addr;\n+\t}\n+\n+\tif (!first)\n+\t\treturn;\n+\n+\t/*\n+\t * Make a linked list by chaining the allocations into a linked list.\n+\t * Possible because allocations are 16 bytes minimum and so fit a pointer.\n+\t */\n+\tdo  {\n+\t\thead = buddy-\u003edeferred_free;\n+\t\t*(void __arena * __arena *)last = head;\n+\t} while (cmpxchg((void __arena * __arena *)\u0026buddy-\u003edeferred_free,\n+\t\t\t head, first) != head \u0026\u0026 can_loop);\n+}\n+\n+static int buddy_deferred_free_complete(struct buddy __arena *buddy)\n+{\n+\tvoid __arena *head, *old;\n+\tint ret = 0, err;\n+\n+\tdo {\n+\t\thead = buddy-\u003edeferred_free;\n+\t\tif (!head)\n+\t\t\tbreak;\n+\t\told = head;\n+\n+\t\t/*\n+\t\t * The cmpxchg is safe because we never pop from the list\n+\t\t * without holding the buddy lock. The next pointer of\n+\t\t * the element then cannot have been gotten invalidated.\n+\t\t */\n+\n+\t} while (cmpxchg((void __arena * __arena *)\u0026buddy-\u003edeferred_free,\n+\t\t\t old, NULL) != old \u0026\u0026 can_loop);\n+\n+\twhile (head \u0026\u0026 can_loop) {\n+\t\told = head;\n+\t\thead = *(void __arena * __arena *)head;\n+\n+\t\terr = buddy_free_unlocked(buddy, (u64)old);\n+\t\tret = ret ?: err;\n+\t}\n+\n+\treturn ret;\n+}\n+\n __weak int buddy_free(struct buddy __arena *buddy, void __arena *addr)\n {\n \tunsigned long flags;\n@@ -889,12 +1403,78 @@ __weak int buddy_free(struct buddy __arena *buddy, void __arena *addr)\n \tif (!addr)\n \t\treturn 0;\n \n+\tret = buddy_cache_free(buddy, addr);\n+\tif (!ret)\n+\t\treturn 0;\n+\n+\tif (bpf_in_nmi()) {\n+\t\tbuddy_deferred_free_start(buddy, addr);\n+\t\treturn 0;\n+\t}\n+\n+\tret = buddy_cache_transfer_free(buddy, addr);\n+\tif (!ret)\n+\t\treturn 0;\n+\n \tret = buddy_lock(buddy, flags);\n \tif (ret)\n \t\treturn ret;\n \n \tret = buddy_free_unlocked(buddy, (u64)addr);\n \n+\t/* Drain any frees stranded during an NMI. */\n+\tif (unlikely(buddy_deferred_free_pending(buddy))) {\n+\t\tret = buddy_deferred_free_complete(buddy);\n+\t\tif (ret)\n+\t\t\tgoto out_unlock;\n+\t}\n+\n+out_unlock:\n+\tbuddy_unlock(buddy, flags);\n+\n+\treturn ret;\n+}\n+\n+__weak int buddy_free_bulk(struct buddy __arena *buddy, void __arena * __arena *addrs, size_t naddrs)\n+{\n+\tunsigned long flags;\n+\tunsigned int i;\n+\tint err;\n+\tint ret = 0;\n+\n+\tif (!buddy)\n+\t\treturn -EINVAL;\n+\n+\tif (!naddrs)\n+\t\treturn 0;\n+\n+\tif (!addrs)\n+\t\treturn -EINVAL;\n+\n+\tif (bpf_in_nmi()) {\n+\t\tbuddy_deferred_free_start_bulk(buddy, addrs, naddrs);\n+\t\treturn 0;\n+\t}\n+\n+\tret = buddy_lock(buddy, flags);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tfor (i = zero; i \u003c naddrs \u0026\u0026 can_loop; i++) {\n+\t\t/* NULL pointers are ignored. */\n+\t\tif (!addrs[i])\n+\t\t\tcontinue;\n+\n+\t\terr = buddy_free_unlocked(buddy, (u64)addrs[i]);\n+\t\tret = ret ?: err;\n+\t}\n+\n+\t/* Drain any frees stranded during an NMI. */\n+\tif (unlikely(buddy_deferred_free_pending(buddy))) {\n+\t\terr = buddy_deferred_free_complete(buddy);\n+\t\tret = ret ?: err;\n+\t}\n+\n \tbuddy_unlock(buddy, flags);\n \n \treturn ret;\n"
}

2/1 2026/10/21 04:22 agent patch-evaluator
0m Model:
core

Args:
null

Results:
{
  "EnableConfigs": null,
  "FocusSymbols": [],
  "Reasoning": "The patch series only modifies code in tools/testing/selftests/bpf/ (BPF selftest benchmark and libarena buddy cache allocator implementation for selftests). It does not modify the compiled kernel image (vmlinux) or kernel modules. Therefore, it is not worth fuzzing with syzkaller.",
  "WorthFuzzing": false
}

Instruction:
You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.

================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.

- REACHABILITY IS THE PRIMARY GATE:
  Fuzzing can only discover bugs in code that can actually execute in standard virtualized
  environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
  If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
  regardless of whether it adds assertions or complex logic.

- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
  Patch authors routinely label changes as "cleanups", "refactorings", or state
  "No functional change intended". Do NOT take these claims at face value.
  Code refactorings that rearrange logic, introduce helper functions, or alter state management
  in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
  If reachable executable code is modified or refactored, it MUST be fuzzed.

- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
  When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
  BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
  Even if the author believes the invariant always holds, fuzzing is essential to verify whether
  an unusual sequence of operations can violate it.

================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:

- Non-kernel and non-executable changes:
  * Modifications to Documentation/, comments, or spelling fixes.
  * User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
    that do not affect the compiled kernel image (vmlinux) or kernel modules.
  * Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
    that do not alter control flow or data structures.
  * Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
  * Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
    ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
  * Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
    physical PCI hot-unplug or manual sysfs driver unbinding.
  * Code paths exclusive to architectures other than the target architecture.

================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
  * Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
    BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
  * Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
  * Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
  * Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).

================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:

- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
  Do NOT list generic, ubiquitous functions called by almost every program in the corpus
  (including, but not limited to: general memory allocators and deallocators, page fault
  and trap handlers, or core synchronization primitives; this is not an exhaustive list).
  Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
  which severely dilutes fuzzing effort away from the actual changes.

- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
  List functions that specifically implement the logic being added or altered, or direct API entrypoints
  for the subsystem feature under review.

- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
  Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
  distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
  If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
  functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).

================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 02b11b0275ad1f6550918aaa229b37ec791c1532
Author: syz-cluster <triage@syzkaller.com>
Date:   Wed Oct 21 04:22:06 2026 +0000

    syz-cluster: applied patch under review

diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/bpf/bench.c
index d5f494184f9d1..7d2a3587971fb 100644
--- a/tools/testing/selftests/bpf/bench.c
+++ b/tools/testing/selftests/bpf/bench.c
@@ -589,6 +589,7 @@ extern const struct bench bench_bpf_nop;
 extern const struct bench bench_xdp_lb;
 extern const struct bench bench_libarena_malloc;
 extern const struct bench bench_libarena_calloc;
+extern const struct bench bench_libarena_malloc_free;
 
 static const struct bench *benchs[] = {
 	&bench_count_global,
@@ -675,6 +676,7 @@ static const struct bench *benchs[] = {
 	&bench_xdp_lb,
 	&bench_libarena_malloc,
 	&bench_libarena_calloc,
+	&bench_libarena_malloc_free,
 };
 
 static void find_benchmark(void)
diff --git a/tools/testing/selftests/bpf/benchs/bench_libarena.c b/tools/testing/selftests/bpf/benchs/bench_libarena.c
index 24e432244bf5d..1185a471123c1 100644
--- a/tools/testing/selftests/bpf/benchs/bench_libarena.c
+++ b/tools/testing/selftests/bpf/benchs/bench_libarena.c
@@ -25,6 +25,10 @@ static struct {
 	struct libarena_bench *skel;
 	int bench_fd;
 	int reset_fd;
+	bool reset_each_run;
+	bool stop;
+	int stopped;
+	int samples;
 } ctx;
 
 enum {
@@ -36,7 +40,7 @@ static const struct argp_option opts[] = {
 	{ "alloc_size", ARG_LIBARENA_ALLOC_SIZE, "BYTES", 0,
 	  "Size of each arena allocation" },
 	{ "nallocs", ARG_LIBARENA_NALLOCS, "ITERS", 0,
-	  "Number of allocation per measurement" },
+	  "Number of allocations per invocation" },
 	{},
 };
 
@@ -68,12 +72,17 @@ const struct argp bench_libarena_argp = {
 	.parser = parse_arg,
 };
 
-static void validate(void)
+static void validate_common(void)
 {
 	if (env.consumer_cnt != 0) {
 		fprintf(stderr, "benchmark doesn't support consumers\n");
 		exit(1);
 	}
+}
+
+static void validate(void)
+{
+	validate_common();
 
 	if (env.producer_cnt != 1) {
 		fprintf(stderr, "benchmark supports exactly one producer\n");
@@ -81,6 +90,21 @@ static void validate(void)
 	}
 }
 
+static void malloc_free_validate(void)
+{
+	validate_common();
+
+	if (env.producer_cnt < 1) {
+		fprintf(stderr, "benchmark requires at least one producer\n");
+		exit(1);
+	}
+
+	if (args.alloc_size < sizeof(void *)) {
+		fprintf(stderr, "allocation size must hold a pointer\n");
+		exit(1);
+	}
+}
+
 static void setup_common(void)
 {
 	struct arena_alloc_reserve_args reserve_args = {
@@ -114,38 +138,55 @@ static void setup_common(void)
 	ctx.skel->bss->bench_alloc_size = args.alloc_size;
 	ctx.skel->bss->bench_nallocs = args.nallocs;
 	ctx.reset_fd = bpf_program__fd(ctx.skel->progs.arena_buddy_reset);
+	ctx.reset_each_run = false;
+	ctx.stop = false;
+	ctx.stopped = 0;
+	ctx.samples = 0;
 }
 
 static void malloc_setup(void)
 {
 	setup_common();
 	ctx.bench_fd = bpf_program__fd(ctx.skel->progs.bench_malloc);
+	ctx.reset_each_run = true;
 }
 
 static void calloc_setup(void)
 {
 	setup_common();
 	ctx.bench_fd = bpf_program__fd(ctx.skel->progs.bench_calloc);
+	ctx.reset_each_run = true;
+}
+
+static void malloc_free_setup(void)
+{
+	setup_common();
+	ctx.bench_fd = bpf_program__fd(ctx.skel->progs.bench_malloc_free);
+	ctx.skel->bss->bench_collect = false;
 }
 
 static void *producer(void *input)
 {
 	int err;
 
-	while (true) {
+	while (!__atomic_load_n(&ctx.stop, __ATOMIC_ACQUIRE)) {
 		err = libarena_run_prog(ctx.bench_fd);
 		if (err) {
 			fprintf(stderr, "libarena benchmark failed: %d\n", err);
 			exit(1);
 		}
 
-		err = libarena_run_prog(ctx.reset_fd);
-		if (err) {
-			fprintf(stderr, "libarena alloc reset failed: %d\n", err);
-			exit(1);
+		if (ctx.reset_each_run) {
+			err = libarena_run_prog(ctx.reset_fd);
+			if (err) {
+				fprintf(stderr, "libarena alloc reset failed: %d\n", err);
+				exit(1);
+			}
 		}
 	}
 
+	__atomic_add_fetch(&ctx.stopped, 1, __ATOMIC_RELEASE);
+
 	return NULL;
 }
 
@@ -155,6 +196,15 @@ static void measure(struct bench_res *res)
 	res->hits = atomic_swap(&ctx.skel->bss->bench_hits, 0);
 }
 
+static void malloc_free_measure(struct bench_res *res)
+{
+	(void)res;
+
+	if (++ctx.samples == env.warmup_sec)
+		__atomic_store_n(&ctx.skel->bss->bench_collect, true,
+				 __ATOMIC_RELAXED);
+}
+
 static void report_progress(int iter, struct bench_res *res, long delta_ns)
 {
 	double latency_ns = 0.0;
@@ -162,7 +212,7 @@ static void report_progress(int iter, struct bench_res *res, long delta_ns)
 	if (res->hits)
 		latency_ns = res->duration_ns / (double)res->hits;
 
-	printf("Iter %3d (%7.3lfus): latency %8.3lf ns/op (%ld allocations)\n",
+	printf("Iter %3d (%7.3lfus): latency %8.3lf ns/allocation (%ld allocations)\n",
 	       iter, (delta_ns - 1000000000) / 1000.0, latency_ns, res->hits);
 }
 
@@ -182,9 +232,27 @@ static void report_final(struct bench_res res[], int res_cnt)
 		return;
 	}
 
-	printf("Summary: %.3lf ns/op, %.0lf invocations for %u allocations/invocation)\n",
-	       duration_ns / (double)hits, hits / (double)res_cnt,
-	       ctx.skel->bss->bench_nallocs);
+	printf("Summary: %.3lf ns/allocation (%ld allocations)\n",
+	       duration_ns / (double)hits, hits);
+}
+
+static void malloc_free_report_final(struct bench_res res[], int res_cnt)
+{
+	struct bench_res final = {};
+
+	(void)res;
+	(void)res_cnt;
+
+	__atomic_store_n(&ctx.stop, true, __ATOMIC_RELEASE);
+	while (__atomic_load_n(&ctx.stopped, __ATOMIC_ACQUIRE) != env.producer_cnt)
+		usleep(1000);
+
+	final.duration_ns = __atomic_load_n(
+		&ctx.skel->bss->bench_duration_ns, __ATOMIC_RELAXED);
+	final.hits = __atomic_load_n(
+		&ctx.skel->bss->bench_hits, __ATOMIC_RELAXED);
+
+	report_final(&final, 1);
 }
 
 const struct bench bench_libarena_malloc = {
@@ -208,3 +276,13 @@ const struct bench bench_libarena_calloc = {
 	.report_progress = report_progress,
 	.report_final = report_final,
 };
+
+const struct bench bench_libarena_malloc_free = {
+	.name = "libarena-malloc-free",
+	.argp = &bench_libarena_argp,
+	.validate = malloc_free_validate,
+	.setup = malloc_free_setup,
+	.producer_thread = producer,
+	.measure = malloc_free_measure,
+	.report_final = malloc_free_report_final,
+};
diff --git a/tools/testing/selftests/bpf/benchs/run_bench_libarena.sh b/tools/testing/selftests/bpf/benchs/run_bench_libarena.sh
index 10afe4d52ebf7..5d5f84310b26a 100755
--- a/tools/testing/selftests/bpf/benchs/run_bench_libarena.sh
+++ b/tools/testing/selftests/bpf/benchs/run_bench_libarena.sh
@@ -29,3 +29,11 @@ printf "\t-------------------\n"
 			"$($RUN_BENCH --alloc_size "$size" --nallocs "$nallocs" libarena-calloc)"
 	done
 done
+
+header "libarena parallel malloc/free\n"
+
+for producers in 1 2 4 8; do
+	summarize_libarena "$producers producers:" \
+		"$($RUN_BENCH -a -p "$producers" --alloc_size 64 --nallocs 1000 \
+		libarena-malloc-free)"
+done
diff --git a/tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c b/tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c
index e06397dc9a40d..3c9235c56b007 100644
--- a/tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c
+++ b/tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c
@@ -9,6 +9,7 @@ u32 bench_alloc_size;
 u32 bench_nallocs;
 long bench_hits;
 long bench_duration_ns;
+bool bench_collect;
 
 SEC("syscall")
 int bench_malloc(void)
@@ -49,3 +50,45 @@ int bench_calloc(void)
 	__sync_add_and_fetch(&bench_hits, i);
 	return 0;
 }
+
+struct bench_alloc {
+	struct bench_alloc __arena *next;
+};
+
+SEC("syscall")
+int bench_malloc_free(void)
+{
+	struct bench_alloc __arena *next, *obj, *head = NULL;
+	u64 duration_ns = 0, start_ns;
+	u32 i, nallocs;
+	bool collect;
+
+	collect = READ_ONCE(bench_collect);
+	for (i = zero; i < bench_nallocs && can_loop; i++) {
+		start_ns = bpf_ktime_get_ns();
+		obj = arena_malloc(bench_alloc_size);
+		duration_ns += bpf_ktime_get_ns() - start_ns;
+		if (!obj)
+			break;
+
+		obj->next = head;
+		head = obj;
+	}
+	nallocs = i;
+
+	for (i = zero; head && can_loop; i++) {
+		next = head->next;
+		arena_free(head);
+		head = next;
+	}
+
+	if (nallocs != bench_nallocs)
+		return -ENOMEM;
+
+	if (!collect)
+		return 0;
+
+	__sync_add_and_fetch(&bench_duration_ns, duration_ns);
+	__sync_add_and_fetch(&bench_hits, nallocs);
+	return 0;
+}
diff --git a/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h b/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h
index 528c69a1f38e0..655b3f5624bf3 100644
--- a/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h
+++ b/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h
@@ -2,6 +2,8 @@
 /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
 #pragma once
 
+#include <libarena/buddy_cache.h>
+
 enum buddy_consts {
 	/*
 	 * Minimum allocation is 1 << BUDDY_MIN_ALLOC_SHIFT.
@@ -49,6 +51,8 @@ struct buddy_header {
 	u32 next_index; /* Same for the next allocation. */
 };
 
+struct buddy_deferred_free;
+
 /*
  * We bring memory into the allocator 1 MiB at a time.
  */
@@ -69,6 +73,8 @@ struct buddy {
 	struct buddy_chunk __arena *first_chunk;		/* Pointer to the chunk linked list. */
 	arena_spinlock_t lock;			/* Allocator lock */
 	u64 vaddr;				/* Allocation into reserved vaddr */
+	struct buddy_cache_state cache_state;
+	struct buddy_deferred_free __arena *deferred_free;
 };
 
 #ifdef __BPF__
@@ -76,6 +82,10 @@ struct buddy {
 int buddy_init(struct buddy __arena *buddy);
 int buddy_destroy(struct buddy __arena *buddy);
 int buddy_free(struct buddy __arena *buddy, void __arena *free);
+int buddy_free_bulk(struct buddy __arena *buddy, void __arena * __arena *addrs,
+	size_t naddrs);
 void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size);
+int buddy_alloc_bulk(struct buddy __arena *buddy, size_t size, size_t norder,
+	void __arena * __arena * out);
 
 #endif /* __BPF__  */
diff --git a/tools/testing/selftests/bpf/libarena/include/libarena/buddy_cache.h b/tools/testing/selftests/bpf/libarena/include/libarena/buddy_cache.h
new file mode 100644
index 0000000000000..9fa90097dc98e
--- /dev/null
+++ b/tools/testing/selftests/bpf/libarena/include/libarena/buddy_cache.h
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#pragma once
+
+/* Constants related to the buddy allocator's object caches. */
+enum buddy_cache_consts {
+	/*
+	 * Do not cache larger allocations to avoid excessive
+	 * memory consumption from the per-cpu lists.
+	 */
+	BUDDY_CACHE_NUM_ORDERS = 1 << 3,
+
+	/*
+	 * Just like arena spinlocks, hardcode percpu state to a maximum number of CPUs.
+	 * Since arena spinlocks are a hard dependency, use the exact same limit.
+	 */
+	BUDDY_CACHE_MAX_CPUS = 1024,
+
+	/*
+	 * Items transferred as one batch. This must remain a power of two because
+	 * buddy_alloc_bulk() expresses the number of objects as an order.
+	 */
+	BUDDY_CACHE_BATCH_SIZE = 8,
+
+	/*
+	 * Objects retained locally for each allocation order.
+	 */
+	BUDDY_CACHE_CAPACITY = BUDDY_CACHE_BATCH_SIZE * 2,
+
+	/* Number of objects shared between per-CPU caches for each order. */
+	BUDDY_TRANSFER_CAPACITY = BUDDY_CACHE_BATCH_SIZE * 8,
+};
+
+struct buddy_cache_order {
+	u32 elems;
+	u32 __pad;
+	void __arena *objects[BUDDY_CACHE_CAPACITY];
+} __attribute__((aligned(64)));
+
+/* Dynamically allocated packed per-CPU slot slab. */
+struct buddy_cache {
+	struct buddy_cache_order orders[BUDDY_CACHE_NUM_ORDERS];
+};
+
+struct buddy_transfer_order {
+	arena_spinlock_t lock;
+	u32 elems;
+	void __arena *objects[BUDDY_TRANSFER_CAPACITY];
+} __attribute__((aligned(64)));
+
+struct buddy_cache_state {
+	struct buddy_cache __arena *percpu[BUDDY_CACHE_MAX_CPUS];
+	struct buddy_transfer_order transfer[BUDDY_CACHE_NUM_ORDERS];
+	bool valid;
+};
+
+/* Per-CPU guard against nested or preempting cache operations. */
+
+struct __attribute__((aligned(64))) buddy_cache_active {
+	unsigned int value;
+};
diff --git a/tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c b/tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c
index 5628f09870121..f14c2945d1e11 100644
--- a/tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c
+++ b/tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c
@@ -4,6 +4,7 @@
 #include <libarena/common.h>
 
 #include <libarena/asan.h>
+#include <libarena/buddy_cache.h>
 #include <libarena/buddy.h>
 
 extern struct buddy __arena buddy;
@@ -17,6 +18,24 @@ struct segarr_entry {
 #define SEGARRLEN (512)
 static struct segarr_entry __arena segarr[SEGARRLEN];
 static void __arena *ptrs[17];
+
+enum buddy_alloc_path_test_consts {
+	TEST_BULK_NORDER = 3,
+	TEST_BULK_ITEMS = 1 << TEST_BULK_NORDER,
+	TEST_BULK_SIZE = 17,
+	TEST_BULK_STEP = 32,
+	TEST_CACHE_ORDER = 2,
+	TEST_CACHE_SIZE = BUDDY_MIN_ALLOC_BYTES << TEST_CACHE_ORDER,
+};
+
+struct buddy_alloc_path_test_storage {
+	void __arena *bulk[BUDDY_CACHE_CAPACITY + 1];
+	void __arena *to_free[BUDDY_CACHE_CAPACITY + 1];
+	void __arena *drain[BUDDY_CACHE_BATCH_SIZE];
+};
+
+static struct buddy_alloc_path_test_storage __arena alloc_path_test;
+
 size_t __arena alloc_sizes[] = { 3, 17, 1025, 129, 16350, 333, 9, 517 };
 size_t __arena alloc_multiple_sizes[] = { 3, 17, 1025, 129, 16350, 333, 9, 517, 2099 };
 size_t __arena alloc_free_sizes[] = { 3, 17, 64, 129, 256, 333, 512, 517 };
@@ -34,9 +53,20 @@ __weak int test_buddy_create(void)
 		if (ret)
 			return ret;
 
+#ifndef BPF_ARENA_ASAN
+		if (!buddy.cache_state.valid) {
+			buddy_destroy(&buddy);
+			return -EINVAL;
+		}
+#endif
+
 		ret = buddy_destroy(&buddy);
 		if (ret)
 			return ret;
+		if (buddy.deferred_free)
+			return -EINVAL;
+		if (buddy.cache_state.valid)
+			return -EINVAL;
 	}
 
 	return 0;
@@ -210,4 +240,251 @@ __weak int test_buddy_alignment(void)
 	return 0;
 }
 
+SEC("syscall")
+__weak int test_buddy_bulk_alloc_free(void)
+{
+	const size_t uncached_size =
+		(BUDDY_MIN_ALLOC_BYTES << (BUDDY_CACHE_NUM_ORDERS - 1)) + 1;
+	void __arena *sentinel = (void __arena *)&alloc_path_test;
+	u8 __arena *mem;
+	unsigned int i, j, idx;
+	int ret, destroy_ret;
+
+	ret = buddy_init(&buddy);
+	if (ret)
+		return ret;
+
+	/*
+	 * A 17-byte request rounds up to a 32-byte buddy allocation. Verify
+	 * that the bulk allocator returns every child at the expected stride,
+	 * without writing beyond the requested output array.
+	 */
+	alloc_path_test.bulk[TEST_BULK_ITEMS] = sentinel;
+	ret = buddy_alloc_bulk(&buddy, TEST_BULK_SIZE, TEST_BULK_NORDER,
+			       alloc_path_test.bulk);
+	if (ret)
+		goto out;
+	if (alloc_path_test.bulk[TEST_BULK_ITEMS] != sentinel) {
+		ret = -EOVERFLOW;
+		goto out;
+	}
+
+	for (i = zero; i < TEST_BULK_ITEMS && can_loop; i++) {
+		mem = alloc_path_test.bulk[i];
+		if (!mem) {
+			ret = -ENOMEM;
+			goto out;
+		}
+		if (i && (u64)mem !=
+			 (u64)alloc_path_test.bulk[i - 1] + TEST_BULK_STEP) {
+			ret = -EINVAL;
+			goto out;
+		}
+
+		for (j = zero; j < TEST_BULK_SIZE && can_loop; j++)
+			mem[j] = (u8)(i + 1);
+	}
+
+	for (i = zero; i < TEST_BULK_ITEMS && can_loop; i++) {
+		mem = alloc_path_test.bulk[i];
+		for (j = zero; j < TEST_BULK_SIZE && can_loop; j++) {
+			if (mem[j] != (u8)(i + 1)) {
+				ret = -EINVAL;
+				goto out;
+			}
+		}
+
+		/* Five is coprime to eight, producing a permutation of all items. */
+		idx = (i * 5) & (TEST_BULK_ITEMS - 1);
+		alloc_path_test.to_free[i] = alloc_path_test.bulk[idx];
+	}
+	alloc_path_test.to_free[TEST_BULK_ITEMS] = NULL;
+
+	ret = buddy_free_bulk(&buddy, alloc_path_test.to_free,
+			      TEST_BULK_ITEMS + 1);
+	if (ret)
+		goto out;
+
+	/*
+	 * Order eight is immediately above the per-CPU cache range. Free a bulk
+	 * allocation one child at a time in a non-sequential order to exercise
+	 * independent metadata and coalescing without the cache intercepting it.
+	 */
+	ret = buddy_alloc_bulk(&buddy, uncached_size, TEST_BULK_NORDER,
+			       alloc_path_test.bulk);
+	if (ret)
+		goto out;
+
+	for (i = zero; i < TEST_BULK_ITEMS && can_loop; i++) {
+		idx = (i * 5) & (TEST_BULK_ITEMS - 1);
+		ret = buddy_free(&buddy, alloc_path_test.bulk[idx]);
+		if (ret)
+			goto out;
+	}
+
+out:
+	destroy_ret = buddy_destroy(&buddy);
+	return ret ?: destroy_ret;
+}
+
+SEC("syscall")
+__weak int test_buddy_cache_paths(void)
+{
+#ifdef BPF_ARENA_ASAN
+	return 0;
+#else
+	struct buddy_cache_state __arena *state;
+	struct buddy_cache __arena *cache;
+	struct buddy_cache_order __arena *slots;
+	struct buddy_transfer_order __arena *transfer;
+	void __arena *direct, *cached, *reused, *refilled, *overflow_reused;
+	void __arena *transferred;
+	unsigned int cpu, i;
+	int ret, destroy_ret;
+
+	ret = buddy_init(&buddy);
+	if (ret)
+		return ret;
+
+	state = &buddy.cache_state;
+
+	cpu = bpf_get_smp_processor_id();
+	cache = state->percpu[cpu];
+	if (!cache) {
+		ret = -EINVAL;
+		goto out;
+	}
+	slots = &cache->orders[TEST_CACHE_ORDER];
+	transfer = &state->transfer[TEST_CACHE_ORDER];
+	if (READ_ONCE(slots->elems) || READ_ONCE(transfer->elems)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* A miss bulk-refills the local order and returns one object directly. */
+	direct = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+	if (!direct || READ_ONCE(slots->elems) != BUDDY_CACHE_BATCH_SIZE - 1) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	/* Subsequent allocations and frees stay on the packed local stack. */
+	cached = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+	if (!cached || READ_ONCE(slots->elems) != BUDDY_CACHE_BATCH_SIZE - 2) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	ret = buddy_free(&buddy, cached);
+	if (ret)
+		goto out;
+	reused = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+	if (reused != cached) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Drain the local stack, then verify that the next miss refills it. */
+	for (i = zero; i < BUDDY_CACHE_BATCH_SIZE - 2 && can_loop; i++) {
+		alloc_path_test.drain[i] = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+		if (!alloc_path_test.drain[i]) {
+			ret = -ENOMEM;
+			goto out;
+		}
+	}
+	if (READ_ONCE(slots->elems)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	refilled = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+	if (!refilled ||
+	    READ_ONCE(slots->elems) != BUDDY_CACHE_BATCH_SIZE - 1) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Bulk allocation bypasses the cache. Fill the remaining local slots. */
+	ret = buddy_alloc_bulk(&buddy, TEST_CACHE_SIZE,
+			       arena_fls(BUDDY_CACHE_CAPACITY) - 1,
+			       alloc_path_test.bulk);
+	if (ret)
+		goto out;
+
+	ret = buddy_free(&buddy, alloc_path_test.bulk[0]);
+	if (ret)
+		goto out;
+
+	ret = buddy_free(&buddy, alloc_path_test.bulk[1]);
+	if (ret)
+		goto out;
+
+	for (i = 2;
+	     i <= BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE && can_loop;
+	     i++) {
+		ret = buddy_free(&buddy, alloc_path_test.bulk[i]);
+		if (ret)
+			goto out;
+	}
+	if (READ_ONCE(slots->elems) != BUDDY_CACHE_CAPACITY) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* A free beyond local capacity transfers a batch to the shared cache. */
+	ret = buddy_free(&buddy,
+			 alloc_path_test.bulk[BUDDY_CACHE_CAPACITY -
+					      BUDDY_CACHE_BATCH_SIZE + 1]);
+	if (ret)
+		goto out;
+	if (READ_ONCE(slots->elems) !=
+			BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE + 1 ||
+	    READ_ONCE(transfer->elems) != BUDDY_CACHE_BATCH_SIZE) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	overflow_reused = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+	if (overflow_reused != alloc_path_test.bulk[BUDDY_CACHE_CAPACITY -
+							 BUDDY_CACHE_BATCH_SIZE + 1] ||
+	    READ_ONCE(slots->elems) !=
+			BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Drain locally, then consume the batch without touching buddy. */
+	for (i = zero;
+	     i < BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE && can_loop;
+	     i++) {
+		alloc_path_test.drain[i] = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+		if (!alloc_path_test.drain[i]) {
+			ret = -ENOMEM;
+			goto out;
+		}
+	}
+	transferred = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+	if (transferred != alloc_path_test.bulk[BUDDY_CACHE_CAPACITY -
+						     BUDDY_CACHE_BATCH_SIZE] ||
+	    READ_ONCE(slots->elems) != BUDDY_CACHE_BATCH_SIZE - 1 ||
+	    READ_ONCE(transfer->elems)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Interior pointers are rejected without consuming the allocation. */
+	i = BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE + 2;
+	if (!buddy_free(&buddy, (u8 __arena *)alloc_path_test.bulk[i] +
+				 BUDDY_MIN_ALLOC_BYTES)) {
+		ret = -EINVAL;
+		goto out;
+	}
+	ret = buddy_free(&buddy, alloc_path_test.bulk[i]);
+
+out:
+	destroy_ret = buddy_destroy(&buddy);
+	return ret ?: destroy_ret;
+#endif
+}
+
 __weak char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c b/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
index 2490ab1396de0..796dacf3c6a2f 100644
--- a/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
+++ b/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
@@ -4,8 +4,13 @@
 #include <libarena/common.h>
 #include <libarena/asan.h>
 #include <libarena/buddy.h>
+#include <libarena/buddy_cache.h>
 
 #include <bpf_arena_spin_lock.h>
+#include <bpf_experimental.h>
+
+_Static_assert(BUDDY_CACHE_MAX_CPUS == _Q_MAX_CPUS,
+	       "buddy caches must cover every CPU arena spinlocks support");
 
 /*
  * Buddy allocator arena-based implementation.
@@ -38,18 +43,303 @@
  * and their allocation state. It also includes the freelist
  * heads for the allocation itself.
  */
+static u8 idx_get_order(struct buddy_chunk __arena *chunk, u64 idx);
+static u64 arena_next_pow2(__u64 n);
+static u64 size_to_order(size_t size);
+static void __arena *buddy_alloc_memory(struct buddy __arena *buddy, size_t size,
+					int order, size_t norder);
+static int buddy_free_unlocked(struct buddy __arena *buddy, u64 addr);
+static bool buddy_deferred_free_pending(struct buddy __arena *buddy);
+static int buddy_deferred_free_complete(struct buddy __arena *buddy);
+static struct buddy_cache_active active[BUDDY_CACHE_MAX_CPUS];
+
+struct buddy_cache_ctx {
+	struct buddy_cache __arena *cache;
+	struct buddy_cache_active *active;
+};
+
+#define buddy_lock(buddy, flags) (arena_spin_lock_irqsave(&(buddy)->lock, (flags)))
+#define buddy_unlock(buddy, flags) (arena_spin_unlock_irqrestore(&(buddy)->lock, (flags)))
+
+/*
+ * Per-CPU caching is disabled in ASAN builds because its additional call-chain
+ * stack usage causes instrumented programs to exceed BPF's 512-byte stack limit.
+ */
+#ifdef BPF_ARENA_ASAN
+#define DISABLE_CACHE_IF_ASAN(...) do { return (__VA_ARGS__); } while (0)
+#else
+#define DISABLE_CACHE_IF_ASAN(...)
+#endif
+
+static __always_inline int
+buddy_cache_enter(struct buddy_cache_state __arena *state,
+		  struct buddy_cache_ctx *ctx)
+{
+	unsigned int cpu;
+
+	/*
+	 * No support for cache operations from contexts that interrupted or
+	 * preempted an in-flight operation on the same CPU, just like the
+	 * kernel's BPF memory allocator.
+	 */
+	cpu = bpf_get_smp_processor_id();
+	ctx->cache = state->percpu[cpu];
+	ctx->active = &active[cpu];
+	if (cmpxchg(&ctx->active->value, 0, 1))
+		return -EAGAIN;
+
+	return 0;
+}
+
+static __always_inline void buddy_cache_exit(struct buddy_cache_ctx *ctx)
+{
+	smp_store_release(&ctx->active->value, 0);
+}
+
+static __always_inline void __arena *
+buddy_cache_pop(struct buddy_cache __arena *cache, int order)
+{
+	struct buddy_cache_order __arena *slots;
+	void __arena *addr;
+	u32 elems;
+
+	slots = &cache->orders[order];
+	elems = READ_ONCE(slots->elems);
+	if (!elems)
+		return NULL;
+
+	addr = READ_ONCE(slots->objects[elems - 1]);
+	WRITE_ONCE(slots->elems, elems - 1);
+
+	return addr;
+}
+
+static __always_inline int buddy_cache_push(struct buddy_cache __arena *cache,
+					    int order, void __arena *obj)
+{
+	struct buddy_cache_order __arena *slots;
+	u32 elems;
+
+	slots = &cache->orders[order];
+	elems = READ_ONCE(slots->elems);
+	if (elems >= BUDDY_CACHE_CAPACITY)
+		return -ENOMEM;
+
+	WRITE_ONCE(slots->objects[elems], obj);
+	WRITE_ONCE(slots->elems, elems + 1);
+
+	return 0;
+}
+
+static int buddy_transfer_take_batch(struct buddy_cache_state __arena *state,
+				     int order,
+				     struct buddy_cache_order __arena *slots)
+{
+	struct buddy_transfer_order __arena *transfer;
+	unsigned long flags;
+	u32 elems, start;
+	unsigned int i;
+	int ret = 0;
+
+	transfer = &state->transfer[order];
+	if (arena_spin_lock_irqsave(&transfer->lock, flags))
+		return -EAGAIN;
+
+	elems = READ_ONCE(transfer->elems);
+	if (elems < BUDDY_CACHE_BATCH_SIZE) {
+		ret = -ENOENT;
+		goto out_unlock;
+	}
+
+	start = elems - BUDDY_CACHE_BATCH_SIZE;
+	for (i = zero; i < BUDDY_CACHE_BATCH_SIZE && can_loop; i++)
+		WRITE_ONCE(slots->objects[i],
+			   READ_ONCE(transfer->objects[start + i]));
+
+	WRITE_ONCE(transfer->elems, start);
+	WRITE_ONCE(slots->elems, BUDDY_CACHE_BATCH_SIZE);
+
+out_unlock:
+	arena_spin_unlock_irqrestore(&transfer->lock, flags);
+	return ret;
+}
+
+static int buddy_transfer_put_batch(struct buddy_cache_state __arena *state,
+				    int order,
+				    struct buddy_cache_order __arena *slots)
+{
+	struct buddy_transfer_order __arena *transfer;
+	unsigned long flags;
+	u32 elems, local_elems, start;
+	unsigned int i;
+	int ret = 0;
+
+	transfer = &state->transfer[order];
+	if (arena_spin_lock_irqsave(&transfer->lock, flags))
+		return -EAGAIN;
+
+	elems = READ_ONCE(transfer->elems);
+	if (elems > BUDDY_TRANSFER_CAPACITY - BUDDY_CACHE_BATCH_SIZE) {
+		ret = -ENOMEM;
+		goto out_unlock;
+	}
+
+	local_elems = READ_ONCE(slots->elems);
+	if (local_elems < BUDDY_CACHE_BATCH_SIZE) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+
+	start = local_elems - BUDDY_CACHE_BATCH_SIZE;
+	for (i = zero; i < BUDDY_CACHE_BATCH_SIZE && can_loop; i++)
+		WRITE_ONCE(transfer->objects[elems + i],
+			   READ_ONCE(slots->objects[start + i]));
+
+	WRITE_ONCE(slots->elems, start);
+	WRITE_ONCE(transfer->elems, elems + BUDDY_CACHE_BATCH_SIZE);
+
+out_unlock:
+	arena_spin_unlock_irqrestore(&transfer->lock, flags);
+	return ret;
+}
+
+void __arena *buddy_cache_alloc(struct buddy __arena *buddy, size_t order)
+{
+	struct buddy_cache_state __arena *state = &buddy->cache_state;
+	struct buddy_cache_ctx ctx;
+	void __arena *address = NULL;
+
+	DISABLE_CACHE_IF_ASAN(NULL);
+
+	if (order >= BUDDY_CACHE_NUM_ORDERS)
+		return NULL;
+
+	if (buddy_cache_enter(state, &ctx))
+		return NULL;
+
+	address = buddy_cache_pop(ctx.cache, order);
+	buddy_cache_exit(&ctx);
+
+	return address;
+}
+
+/* Retrieve the order of an allocation from the address. */
+static int buddy_cache_get_order(void __arena *addr)
+{
+	struct buddy_chunk __arena *chunk;
+	u64 idx;
 
+	chunk = (void __arena *)((u64)addr & ~BUDDY_CHUNK_OFFSET_MASK);
+	idx = ((u64)addr & BUDDY_CHUNK_OFFSET_MASK) / BUDDY_MIN_ALLOC_BYTES;
+
+	return idx_get_order(chunk, idx);
+}
+
+int buddy_cache_free(struct buddy __arena *buddy, void __arena *addr)
+{
+	struct buddy_cache_state __arena *state = &buddy->cache_state;
+	struct buddy_cache_ctx ctx;
+	int order;
+	int ret = 0;
+
+	DISABLE_CACHE_IF_ASAN(-EINVAL);
+
+	order = buddy_cache_get_order(addr);
+	if (order >= BUDDY_CACHE_NUM_ORDERS)
+		return -EINVAL;
+
+	if (buddy_cache_enter(state, &ctx))
+		return -EAGAIN;
+
+	ret = buddy_cache_push(ctx.cache, order, addr);
+	buddy_cache_exit(&ctx);
+
+	return ret;
+}
+
+static int buddy_cache_transfer_free(struct buddy __arena *buddy,
+				     void __arena *addr)
+{
+	struct buddy_cache_state __arena *state = &buddy->cache_state;
+	struct buddy_cache_order __arena *slots;
+	struct buddy_cache_ctx ctx;
+	int order, ret;
+
+	DISABLE_CACHE_IF_ASAN(-EINVAL);
+
+	order = buddy_cache_get_order(addr);
+	if (order >= BUDDY_CACHE_NUM_ORDERS)
+		return -EINVAL;
+
+	if (buddy_cache_enter(state, &ctx))
+		return -EAGAIN;
+
+	/* An allocation may have made room since the first local attempt. */
+	ret = buddy_cache_push(ctx.cache, order, addr);
+	if (!ret)
+		goto done;
+
+	slots = &ctx.cache->orders[order];
+	ret = buddy_transfer_put_batch(state, order, slots);
+	if (!ret)
+		ret = buddy_cache_push(ctx.cache, order, addr);
+
+done:
+	buddy_cache_exit(&ctx);
+	return ret;
+}
+
+static void __arena *buddy_cache_refill(struct buddy __arena *buddy,
+					size_t size, int order)
+{
+	struct buddy_cache_state __arena *state = &buddy->cache_state;
+	struct buddy_cache_order __arena *slots;
+	struct buddy_cache_ctx ctx;
+	void __arena *address = NULL;
+	int ret;
+
+	DISABLE_CACHE_IF_ASAN(NULL);
+
+	if (order >= BUDDY_CACHE_NUM_ORDERS)
+		return NULL;
+
+	if (buddy_cache_enter(state, &ctx))
+		return NULL;
+
+	/* A free may have populated the cache between the miss and refill. */
+	address = buddy_cache_pop(ctx.cache, order);
+	if (address)
+		goto done;
+
+	slots = &ctx.cache->orders[order];
+	ret = buddy_transfer_take_batch(state, order, slots);
+	if (!ret) {
+		address = buddy_cache_pop(ctx.cache, order);
+		goto done;
+	}
+
+	ret = buddy_alloc_bulk(buddy, size,
+				arena_fls(BUDDY_CACHE_BATCH_SIZE) - 1,
+				(void __arena * __arena *)slots->objects);
+	if (ret)
+		goto done;
+
+	address = slots->objects[BUDDY_CACHE_BATCH_SIZE - 1];
+	WRITE_ONCE(slots->elems, BUDDY_CACHE_BATCH_SIZE - 1);
+
+done:
+	buddy_cache_exit(&ctx);
+
+	return address;
+}
 
 enum {
 	BUDDY_POISONED = (s8)0xef,
 
 	/* Number of pages to be allocated per chunk. */
-	BUDDY_CHUNK_PAGES	= BUDDY_CHUNK_BYTES / __PAGE_SIZE
+	BUDDY_CHUNK_PAGES	= BUDDY_CHUNK_BYTES / __PAGE_SIZE,
 };
 
-#define buddy_lock(buddy, flags) (arena_spin_lock_irqsave(&(buddy)->lock, (flags)))
-#define buddy_unlock(buddy, flags) (arena_spin_unlock_irqrestore(&(buddy)->lock, (flags)))
-
 /*
  * Reserve part of the arena address space for the allocator. We use
  * this to get aligned addresses for the chunks, since the arena
@@ -557,6 +847,49 @@ static struct buddy_chunk __arena *buddy_chunk_get(struct buddy __arena *buddy)
 	return chunk;
 }
 
+static int buddy_cache_state_init(struct buddy __arena *buddy)
+{
+	struct buddy_cache_state __arena *state = &buddy->cache_state;
+	struct buddy_cache __arena *cache;
+	unsigned int cpu, order;
+	int ret;
+
+	DISABLE_CACHE_IF_ASAN(0);
+
+	WRITE_ONCE(state->valid, false);
+	if (CONFIG_NR_CPUS > BUDDY_CACHE_MAX_CPUS)
+		return -E2BIG;
+
+	for (order = zero; order < BUDDY_CACHE_NUM_ORDERS && can_loop; order++)
+		WRITE_ONCE(state->transfer[order].elems, 0);
+
+	for (cpu = zero; cpu < CONFIG_NR_CPUS && can_loop; cpu++) {
+		ret = buddy_alloc_bulk(buddy, sizeof(*cache), 0,
+				       (void __arena * __arena *)&state->percpu[cpu]);
+		if (ret)
+			return ret;
+
+		cache = state->percpu[cpu];
+		for (order = zero; order < BUDDY_CACHE_NUM_ORDERS && can_loop; order++)
+			WRITE_ONCE(cache->orders[order].elems, 0);
+	}
+
+	WRITE_ONCE(state->valid, true);
+	return 0;
+}
+
+static void buddy_cache_state_destroy(struct buddy __arena *buddy)
+{
+	struct buddy_cache_state __arena *state = &buddy->cache_state;
+	unsigned int cpu, order;
+
+	WRITE_ONCE(state->valid, false);
+	for (cpu = zero; cpu < CONFIG_NR_CPUS && can_loop; cpu++)
+		WRITE_ONCE(state->percpu[cpu], NULL);
+	for (order = zero; order < BUDDY_CACHE_NUM_ORDERS && can_loop; order++)
+		WRITE_ONCE(state->transfer[order].elems, 0);
+}
+
 __weak int buddy_init(struct buddy __arena *buddy)
 {
 	struct buddy_chunk __arena *chunk;
@@ -565,6 +898,7 @@ __weak int buddy_init(struct buddy __arena *buddy)
 
 	if (!asan_ready())
 		return -EINVAL;
+	buddy->deferred_free = NULL;
 
 	/* Reserve enough address space to ensure allocations are aligned. */
 	ret = buddy_reserve_arena_vaddr(buddy);
@@ -590,7 +924,16 @@ __weak int buddy_init(struct buddy __arena *buddy)
 
 	buddy_unlock(buddy, flags);
 
-	return chunk ? 0 : -ENOMEM;
+	if (!chunk)
+		return -ENOMEM;
+
+	ret = buddy_cache_state_init(buddy);
+	if (ret) {
+		buddy_destroy(buddy);
+		return ret;
+	}
+
+	return 0;
 }
 
 /*
@@ -606,6 +949,8 @@ __weak int buddy_destroy(struct buddy __arena *buddy)
 	if (!buddy)
 		return -EINVAL;
 
+	buddy_cache_state_destroy(buddy);
+
 	/*
 	 * Traverse all buddy chunks and free them back to the arena
 	 * with the same granularity they were allocated with.
@@ -624,11 +969,12 @@ __weak int buddy_destroy(struct buddy __arena *buddy)
 
 	/* Clear all fields. */
 	buddy->first_chunk = NULL;
+	buddy->deferred_free = NULL;
 
 	return 0;
 }
 
-__weak u64 buddy_chunk_alloc(struct buddy_chunk __arena *chunk, int order_req)
+__weak u64 buddy_chunk_alloc(struct buddy_chunk __arena *chunk, int order_req, int order_allocs)
 {
 	struct buddy_header __arena *header, *tmp_header, *next_header;
 	u32 idx, tmpidx, retidx;
@@ -636,7 +982,7 @@ __weak u64 buddy_chunk_alloc(struct buddy_chunk __arena *chunk, int order_req)
 	u64 order = 0;
 	u64 i;
 
-	for (order = order_req; order < BUDDY_CHUNK_NUM_ORDERS && can_loop; order++) {
+	for (order = order_req + order_allocs; order < BUDDY_CHUNK_NUM_ORDERS && can_loop; order++) {
 		if (chunk->freelists[order] != BUDDY_CHUNK_ITEMS)
 			break;
 	}
@@ -658,11 +1004,19 @@ __weak u64 buddy_chunk_alloc(struct buddy_chunk __arena *chunk, int order_req)
 
 	header->prev_index = BUDDY_CHUNK_ITEMS;
 	header->next_index = BUDDY_CHUNK_ITEMS;
-	if (idx_set_order(chunk, retidx, order_req))
-		return (u64)NULL;
+	/*
+	 * For a bulk allocation, record the allocation state and size
+	 * of each item separately so each can be freed independently.
+	 */
+	for (i = zero, idx = retidx; i < (1U << order_allocs) && can_loop; i++) {
+		if (idx_set_order(chunk, idx, order_req))
+			return (u64)NULL;
 
-	if (idx_set_allocated(chunk, retidx, true))
-		return (u64)NULL;
+		if (idx_set_allocated(chunk, idx, true))
+			return (u64)NULL;
+
+		idx += 1U << order_req;
+	}
 
 	/*
 	 * Do not unpoison the address yet, will be done by the caller
@@ -673,7 +1027,7 @@ __weak u64 buddy_chunk_alloc(struct buddy_chunk __arena *chunk, int order_req)
 		return (u64)NULL;
 
 	/* If we allocated from a larger-order chunk, split the buddies. */
-	for (i = order_req; i < order && can_loop; i++) {
+	for (i = order_req + order_allocs; i < order && can_loop; i++) {
 		/*
 		 * Flip the bit for the current order (the bit is guaranteed
 		 * to be 0, so just add 1 << i).
@@ -706,14 +1060,14 @@ __weak u64 buddy_chunk_alloc(struct buddy_chunk __arena *chunk, int order_req)
 }
 
 /* Scan the existing chunks for available memory. */
-static u64 buddy_alloc_from_existing_chunks(struct buddy __arena *buddy, int order)
+static u64 buddy_alloc_from_existing_chunks(struct buddy __arena *buddy, int order, int norder)
 {
 	struct buddy_chunk __arena *chunk;
 	u64 address;
 
 	for (chunk = buddy->first_chunk; chunk != NULL && can_loop;
 	     chunk = chunk->next) {
-		address = buddy_chunk_alloc(chunk, order);
+		address = buddy_chunk_alloc(chunk, order, norder);
 		if (address)
 			return address;
 	}
@@ -725,7 +1079,8 @@ static u64 buddy_alloc_from_existing_chunks(struct buddy __arena *buddy, int ord
  * Try an allocation from a newly allocated chunk. Also
  * incorporate the chunk into the linked list.
  */
-static u64 buddy_alloc_from_new_chunk(struct buddy __arena *buddy, struct buddy_chunk __arena *chunk, int order)
+static u64 buddy_alloc_from_new_chunk(struct buddy __arena *buddy, struct buddy_chunk __arena *chunk,
+		int order, int norder)
 {
 	unsigned long flags;
 	u64 address;
@@ -733,7 +1088,6 @@ static u64 buddy_alloc_from_new_chunk(struct buddy __arena *buddy, struct buddy_
 	if (buddy_lock(buddy, flags))
 		return (u64)NULL;
 
-
 	/*
 	 * Add the chunk into the allocator and try
 	 * to allocate specifically from that chunk.
@@ -741,33 +1095,44 @@ static u64 buddy_alloc_from_new_chunk(struct buddy __arena *buddy, struct buddy_
 	chunk->next = buddy->first_chunk;
 	buddy->first_chunk = chunk;
 
-	address = buddy_chunk_alloc(buddy->first_chunk, order);
+	address = buddy_chunk_alloc(buddy->first_chunk, order, norder);
 
 	buddy_unlock(buddy, flags);
 
 	return (u64)address;
 }
-__weak
-void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size)
+static void __arena *buddy_alloc_memory(struct buddy __arena *buddy, size_t size,
+		int order, size_t norder)
 {
 	void __arena *address = NULL;
+	u8 __arena *object;
 	struct buddy_chunk __arena *chunk;
+	unsigned int alloc_size;
+	unsigned int i, offset;
 	unsigned long flags;
-	int order;
 
 	if (!buddy)
 		return NULL;
 
-	order = size_to_order(size);
-	if (order >= BUDDY_CHUNK_NUM_ORDERS || order < 0) {
-		arena_stderr("invalid order %d (sz %lu)\n", order, size);
+	if (order < 0 || order >= BUDDY_CHUNK_NUM_ORDERS || norder >= BUDDY_CHUNK_NUM_ORDERS - order) {
+		arena_stderr("invalid order %d (norder %lu) (sz %lu)\n", order, norder, size);
 		return NULL;
 	}
 
+	if (bpf_in_nmi())
+		return NULL;
+
 	if (buddy_lock(buddy, flags))
 		return NULL;
 
-	address = (u8 __arena *)buddy_alloc_from_existing_chunks(buddy, order);
+	if (unlikely(buddy_deferred_free_pending(buddy))) {
+		if (buddy_deferred_free_complete(buddy)) {
+			buddy_unlock(buddy, flags);
+			return NULL;
+		}
+	}
+
+	address = (u8 __arena *)buddy_alloc_from_existing_chunks(buddy, order, norder);
 	buddy_unlock(buddy, flags);
 	if (address)
 		goto done;
@@ -775,28 +1140,85 @@ void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size)
 	/* Get a new chunk. */
 	chunk = buddy_chunk_get(buddy);
 	if (chunk)
-		address = (u8 __arena *)buddy_alloc_from_new_chunk(buddy, chunk, order);
+		address = (u8 __arena *)buddy_alloc_from_new_chunk(buddy, chunk, order, norder);
 
 done:
 	/* If we failed to allocate memory, return NULL. */
 	if (!address)
 		return NULL;
 
+	alloc_size = BUDDY_MIN_ALLOC_BYTES << order;
+
 	/*
-	 * Unpoison exactly the amount of bytes requested. If the
-	 * data is smaller than the header, we must poison any
-	 * unused bytes that were part of the header.
+	 * Unpoison exactly the requested bytes in each allocation. Keep
+	 * the padding between the requested size and the rounded buddy
+	 * allocation poisoned.
 	 */
-	if (size < BUDDY_HEADER_OFF + sizeof(struct buddy_header __arena))
-		asan_poison(address + BUDDY_HEADER_OFF, BUDDY_POISONED,
-			    sizeof(struct buddy_header __arena));
+	for (i = zero, offset = zero; i < (1U << norder) && can_loop; i++, offset += alloc_size) {
+		object = (u8 __arena *)address + offset;
 
-	asan_unpoison(address, size);
+		if (size < BUDDY_HEADER_OFF + sizeof(struct buddy_header __arena))
+			asan_poison(object + BUDDY_HEADER_OFF,
+				    BUDDY_POISONED,
+				    sizeof(struct buddy_header __arena));
+
+		asan_unpoison(object, size);
+	}
 
 	return address;
 }
 
-static __always_inline int buddy_free_unlocked(struct buddy __arena *buddy, u64 addr)
+__weak
+int buddy_alloc_bulk(struct buddy __arena *buddy, size_t size, size_t norder,
+		void __arena * __arena * out)
+{
+	void __arena *address;
+	unsigned int i, off;
+	unsigned int step;
+	int order;
+
+	if (!buddy || !out || norder >= BUDDY_CHUNK_NUM_ORDERS)
+		return -EINVAL;
+
+	/*
+	 * Bulk allocations bypass the per-cpu caches and reserve one buddy
+	 * block covering all returned objects. The buddy metadata records
+	 * each object separately so it can later be freed independently.
+	 */
+	order = size_to_order(size);
+	address = buddy_alloc_memory(buddy, size, order, norder);
+	if (!address)
+		return -ENOMEM;
+
+	step = BUDDY_MIN_ALLOC_BYTES << order;
+	for (i = zero, off = zero; i < (1U << norder) && can_loop; i++, off += step)
+		out[i] = &address[off];
+
+	return 0;
+}
+
+__weak
+void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size)
+{
+	void __arena *address;
+	int order;
+
+	order = size_to_order(size);
+	address = buddy_cache_alloc(buddy, order);
+	if (address)
+		return address;
+
+	if (bpf_in_nmi())
+		return NULL;
+
+	address = buddy_cache_refill(buddy, size, order);
+	if (address)
+		return address;
+
+	return buddy_alloc_memory(buddy, size, order, 0);
+}
+
+static int buddy_free_unlocked(struct buddy __arena *buddy, u64 addr)
 {
 	struct buddy_header __arena *header, *buddy_header;
 	u64 idx, buddy_idx, tmp_idx;
@@ -877,6 +1299,98 @@ static __always_inline int buddy_free_unlocked(struct buddy __arena *buddy, u64
 	return 0;
 }
 
+static bool buddy_deferred_free_pending(struct buddy __arena *buddy)
+{
+	return buddy->deferred_free != NULL;
+}
+
+static void buddy_deferred_free_start(struct buddy __arena *buddy, void __arena *addr)
+{
+	void __arena *head;
+	void __arena * __arena *new;
+
+	/*
+	 * Make a linked list by chaining the allocations into a linked list.
+	 * Possible because allocations are 16 bytes minimum and so fit a pointer.
+	 */
+	new = (void __arena * __arena *)addr;
+	asan_unpoison(addr, sizeof(void *));
+	do  {
+		head = buddy->deferred_free;
+		*new = head;
+	} while (cmpxchg((void __arena * __arena *)&buddy->deferred_free,
+			 head, (void __arena *)new) != head && can_loop);
+}
+
+static void buddy_deferred_free_start_bulk(struct buddy __arena *buddy,
+		 void __arena * __arena *addrs, size_t naddrs)
+{
+	void __arena *head, *first, *last, *addr;
+	unsigned int i;
+
+	/* Create a linked list locally. */
+	first = NULL;
+	last = NULL;
+	for (i = zero; i < naddrs && can_loop; i++) {
+		addr = addrs[i];
+		if (!addr)
+			continue;
+
+		asan_unpoison(addr, sizeof(void *));
+
+		if (!first)
+			first = addr;
+		else
+			*(void __arena * __arena *)last = addr;
+
+		last = addr;
+	}
+
+	if (!first)
+		return;
+
+	/*
+	 * Make a linked list by chaining the allocations into a linked list.
+	 * Possible because allocations are 16 bytes minimum and so fit a pointer.
+	 */
+	do  {
+		head = buddy->deferred_free;
+		*(void __arena * __arena *)last = head;
+	} while (cmpxchg((void __arena * __arena *)&buddy->deferred_free,
+			 head, first) != head && can_loop);
+}
+
+static int buddy_deferred_free_complete(struct buddy __arena *buddy)
+{
+	void __arena *head, *old;
+	int ret = 0, err;
+
+	do {
+		head = buddy->deferred_free;
+		if (!head)
+			break;
+		old = head;
+
+		/*
+		 * The cmpxchg is safe because we never pop from the list
+		 * without holding the buddy lock. The next pointer of
+		 * the element then cannot have been gotten invalidated.
+		 */
+
+	} while (cmpxchg((void __arena * __arena *)&buddy->deferred_free,
+			 old, NULL) != old && can_loop);
+
+	while (head && can_loop) {
+		old = head;
+		head = *(void __arena * __arena *)head;
+
+		err = buddy_free_unlocked(buddy, (u64)old);
+		ret = ret ?: err;
+	}
+
+	return ret;
+}
+
 __weak int buddy_free(struct buddy __arena *buddy, void __arena *addr)
 {
 	unsigned long flags;
@@ -889,12 +1403,78 @@ __weak int buddy_free(struct buddy __arena *buddy, void __arena *addr)
 	if (!addr)
 		return 0;
 
+	ret = buddy_cache_free(buddy, addr);
+	if (!ret)
+		return 0;
+
+	if (bpf_in_nmi()) {
+		buddy_deferred_free_start(buddy, addr);
+		return 0;
+	}
+
+	ret = buddy_cache_transfer_free(buddy, addr);
+	if (!ret)
+		return 0;
+
 	ret = buddy_lock(buddy, flags);
 	if (ret)
 		return ret;
 
 	ret = buddy_free_unlocked(buddy, (u64)addr);
 
+	/* Drain any frees stranded during an NMI. */
+	if (unlikely(buddy_deferred_free_pending(buddy))) {
+		ret = buddy_deferred_free_complete(buddy);
+		if (ret)
+			goto out_unlock;
+	}
+
+out_unlock:
+	buddy_unlock(buddy, flags);
+
+	return ret;
+}
+
+__weak int buddy_free_bulk(struct buddy __arena *buddy, void __arena * __arena *addrs, size_t naddrs)
+{
+	unsigned long flags;
+	unsigned int i;
+	int err;
+	int ret = 0;
+
+	if (!buddy)
+		return -EINVAL;
+
+	if (!naddrs)
+		return 0;
+
+	if (!addrs)
+		return -EINVAL;
+
+	if (bpf_in_nmi()) {
+		buddy_deferred_free_start_bulk(buddy, addrs, naddrs);
+		return 0;
+	}
+
+	ret = buddy_lock(buddy, flags);
+	if (ret)
+		return ret;
+
+	for (i = zero; i < naddrs && can_loop; i++) {
+		/* NULL pointers are ignored. */
+		if (!addrs[i])
+			continue;
+
+		err = buddy_free_unlocked(buddy, (u64)addrs[i]);
+		ret = ret ?: err;
+	}
+
+	/* Drain any frees stranded during an NMI. */
+	if (unlikely(buddy_deferred_free_pending(buddy))) {
+		err = buddy_deferred_free_complete(buddy);
+		ret = ret ?: err;
+	}
+
 	buddy_unlock(buddy, flags);
 
 	return ret;

3/2 2026/10/21 04:22 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 20062
							output: 17
							thoughts: 466
						

Thoughts:
**Analyzing this Patch for Fuzzing Worthiness**

Okay, so I've been tasked with assessing this patch and determining if it's worth fuzzing. The first thing I need to do is get a handle on what files it's touching. Alright, looks like we have a list:

*   `tools/testing/selftests/bpf/bench.c`
*   `tools/testing/selftests/bpf/benchs/bench_libarena.c`
*   `tools/testing/selftests/bpf/benchs/run_bench_libarena.sh`
*   `tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c`
*   `tools/testing/selftests/bpf/libarena/include/libarena/buddy.h`
*   `tools/testing/selftests/bpf/libarena/include/libarena/buddy_cache.h`
*   `tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c`
*   `tools/testing/selftests/bpf/libarena/src/buddy.bpf.c`

Wait a minute, hang on a second... looking at those paths, they *all* fall under `tools/testing/selftests/bpf/`. That's important. Now, I need to check the criteria I'm working with. Let's see... Section 2: "WHEN TO RETURN WorthFuzzing=false" - okay, that's what I want to avoid. The crucial bit here is: "Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories..." and it goes on to list specific negative criteria: basically, non-kernel stuff, like documentation, user-space tools, self-tests, samples, scripts... things that *don't* affect the compiled kernel image.

Since all the modifications are in `tools/testing/selftests/bpf/`, this looks very suspect for NOT being worth fuzzing, but I need to make sure I am not missing something. Just to be sure, I should double check with `git diff` or `git status` to be absolutely certain there aren't any sneaky files being changed outside of the path that was initially observed. Gotta be thorough.



4/2 2026/10/21 04:22 tool grepper
0m Args:
{
  "Expression": "diff --git"
}

Results:
{
  "Output": "Documentation/bpf/drgn.rst=99=For example, to print ``struct bpf_prog_aux`` for BPF program id 53077:\n--\nDocumentation/bpf/drgn.rst-103-    % git diff\nDocumentation/bpf/drgn.rst:104:    diff --git a/tools/bpf_inspect.py b/tools/bpf_inspect.py\nDocumentation/bpf/drgn.rst-105-    index 650e228..aea2357 100755\n--\nDocumentation/dev-tools/checkuapi.rst=54=won't break userspace::\n--\nDocumentation/dev-tools/checkuapi.rst-69-      *  comp2_t is 24-bit with 5-bit base 2 exponent and 20 bit fraction\nDocumentation/dev-tools/checkuapi.rst:70:    diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h\nDocumentation/dev-tools/checkuapi.rst-71-    EOF\n--\nDocumentation/dev-tools/checkuapi.rst=310=Consider this change::\n--\nDocumentation/dev-tools/checkuapi.rst-312-    cat \u003c\u003c 'EOF' | patch -l -p1\nDocumentation/dev-tools/checkuapi.rst:313:    diff --git a/include/uapi/asm-generic/Kbuild b/include/uapi/asm-generic/Kbuild\nDocumentation/dev-tools/checkuapi.rst-314-    index ebb180aac74e..a9c88b0a8b3b 100644\n--\nDocumentation/doc-guide/contributing.rst=84=looked like this::\n--\nDocumentation/doc-guide/contributing.rst-102-\nDocumentation/doc-guide/contributing.rst:103:  diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c\nDocumentation/doc-guide/contributing.rst-104-  index 57f6944d65a6..00c9b80b3d33 100644\n--\nDocumentation/mm/transhuge.rst=73=change::\nDocumentation/mm/transhuge.rst-74-\nDocumentation/mm/transhuge.rst:75:\tdiff --git a/mm/mremap.c b/mm/mremap.c\nDocumentation/mm/transhuge.rst-76-\t--- a/mm/mremap.c\n--\nDocumentation/translations/it_IT/doc-guide/contributing.rst=91=patch risultante era questa::\n--\nDocumentation/translations/it_IT/doc-guide/contributing.rst-109-\nDocumentation/translations/it_IT/doc-guide/contributing.rst:110:  diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c\nDocumentation/translations/it_IT/doc-guide/contributing.rst-111-  index 57f6944d65a6..00c9b80b3d33 100644\n--\nDocumentation/translations/zh_CN/doc-guide/contributing.rst=36=C代码编译器发出的警告常常会被视为误报,从而导致出现了旨在让编译器闭嘴的补丁。\n--\nDocumentation/translations/zh_CN/doc-guide/contributing.rst-86-\nDocumentation/translations/zh_CN/doc-guide/contributing.rst:87:  diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c\nDocumentation/translations/zh_CN/doc-guide/contributing.rst-88-  index 57f6944d65a6..00c9b80b3d33 100644\n--\nscripts/checkpatch.pl=2671=sub process {\n--\nscripts/checkpatch.pl-2844-\t\t    ($line =~ /^rename (?:from|to) \\S+\\s*$/ ||\nscripts/checkpatch.pl:2845:\t\t     $line =~ /^diff --git a\\/[\\w\\/\\.\\_\\-]+ b\\/\\S+\\s*$/))) {\nscripts/checkpatch.pl-2846-\t\t\t$is_patch = 1;\n--\nscripts/checkpatch.pl-2902-\t\t# extract the filename as it passes\nscripts/checkpatch.pl:2903:\t\tif ($line =~ /^diff --git.*?(\\S+)$/) {\nscripts/checkpatch.pl-2904-\t\t\t$realfile = $1;\n--\nscripts/get_maintainer.pl=552=foreach my $file (@ARGV) {\n--\nscripts/get_maintainer.pl-601-\t\tpush(@files, $filename);\nscripts/get_maintainer.pl:602:\t    } elsif (m/^diff --git a\\/(\\S+) b\\/(\\S+)\\s*$/) {\nscripts/get_maintainer.pl-603-\t\tmy $filename1 = $1;\n"
}

5/2 2026/10/21 04:22 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 22357
							output: 97
							thoughts: 1024
						

Thoughts:
**Analysis of the Provided Git Diff**

Okay, let's break this down. My initial thought was to use `git log` or `git show` to get a file list, but I'm reminded that I don't have direct access to the git CLI. Fortunately, the full diff is provided, which is perfect.

Quick scan of the diff confirms that the changes are strictly within `tools/testing/selftests/bpf/`. Now I need to determine if this merits fuzzing. Remembering the guidelines, I need to check the negative criteria.

This looks straightforward. The changes are all within the `tools/testing/selftests/bpf/` directory, which indicates user-space selftests, specifically for BPF and a `libarena`. This type of code doesn't impact the core kernel, vmlinux, or any kernel modules. Therefore, this patch absolutely falls under "User-space directories, self-tests, samples, or scripts... that do not affect the compiled kernel image (vmlinux) or kernel modules."

Given this, I have to set `WorthFuzzing` to `false`. Let's gather the necessary parameters for the `set-results` tool.

I need:
*   `WorthFuzzing`: false
*   `Reasoning`: "The patch only modifies user-space selftests and benchmark code under `tools/testing/selftests/bpf/` (specifically BPF selftest libarena buddy cache and benchmark harness). It does not alter any kernel core, driver code, vmlinux, or kernel modules."
*   `FocusSymbols`: \[\]
*   `EnableConfigs`: \[\]

Everything aligns with the instructions, so it's time to call the `set-results` tool.



6/2 2026/10/21 04:22 tool set-results
0m Args:
{
  "EnableConfigs": [],
  "FocusSymbols": [],
  "Reasoning": "The patch series only modifies code in tools/testing/selftests/bpf/ (BPF selftest benchmark and libarena buddy cache allocator implementation for selftests). It does not modify the compiled kernel image (vmlinux) or kernel modules. Therefore, it is not worth fuzzing with syzkaller.",
  "WorthFuzzing": false
}

Results:
{
  "EnableConfigs": null,
  "FocusSymbols": [],
  "Reasoning": "The patch series only modifies code in tools/testing/selftests/bpf/ (BPF selftest benchmark and libarena buddy cache allocator implementation for selftests). It does not modify the compiled kernel image (vmlinux) or kernel modules. Therefore, it is not worth fuzzing with syzkaller.",
  "WorthFuzzing": false
}

7/1 2026/10/21 04:22 action If
0m Args:
{
  "WorthFuzzing": false
}

Results:
null

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)