| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/10/22 11:54 | flow | patch-triage |
0mArgs:null Results: {
"EnableConfigs": null,
"FocusSymbols": [],
"KMSANReasoning": "",
"NeedsKMSAN": false,
"Reasoning": "The patch only touches files under tools/testing/selftests/bpf/, which are user-space benchmark tools and BPF selftest libraries (libarena buddy allocator enhancements). None of the changes affect the compiled kernel image (vmlinux) or kernel modules, so they are not fuzzable by syzkaller.",
"WorthFuzzing": false
} |
| 1/1 | 2026/10/22 11:54 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit ed1f59733d4e60fde4deb2968edd6c86b85dcf94\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Thu Oct 22 11:54:46 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..fbbd553990195 100644\n--- a/tools/testing/selftests/bpf/bench.c\n+++ b/tools/testing/selftests/bpf/bench.c\n@@ -589,6 +589,8 @@ 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+extern const struct bench bench_libarena_malloc_xfree;\n \n static const struct bench *benchs[] = {\n \t\u0026bench_count_global,\n@@ -675,6 +677,8 @@ 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+\t\u0026bench_libarena_malloc_xfree,\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..4096b54db520a 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,62 @@ 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 malloc_xfree_setup(void)\n+{\n+\tsetup_common();\n+\tctx.bench_fd = bpf_program__fd(ctx.skel-\u003eprogs.bench_malloc_xfree);\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 +203,13 @@ 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+\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 +217,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 +237,30 @@ 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+\tlong alloc_ns, free_ns, hits, free_hits;\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+\talloc_ns = __atomic_load_n(\u0026ctx.skel-\u003ebss-\u003ebench_duration_ns, __ATOMIC_RELAXED);\n+\tfree_ns = __atomic_load_n(\u0026ctx.skel-\u003ebss-\u003ebench_free_duration_ns, __ATOMIC_RELAXED);\n+\thits = __atomic_load_n(\u0026ctx.skel-\u003ebss-\u003ebench_hits, __ATOMIC_RELAXED);\n+\tfree_hits = __atomic_load_n(\u0026ctx.skel-\u003ebss-\u003ebench_free_hits, __ATOMIC_RELAXED);\n+\n+\tif (!hits || !free_hits) {\n+\t\tprintf(\"Summary: no runs measured\\n\");\n+\t\treturn;\n+\t}\n+\n+\tprintf(\"Summary: %.3lf ns/allocation, %.3lf ns/free (%ld allocations)\\n\",\n+\t alloc_ns / (double)hits, free_ns / (double)free_hits, hits);\n }\n \n const struct bench bench_libarena_malloc = {\n@@ -208,3 +284,23 @@ 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+};\n+\n+const struct bench bench_libarena_malloc_xfree = {\n+\t.name = \"libarena-malloc-xfree\",\n+\t.argp = \u0026bench_libarena_argp,\n+\t.validate = malloc_free_validate,\n+\t.setup = malloc_xfree_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..2dfa32cd95cc5 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,19 @@ 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\n+\n+header \"libarena parallel cross-CPU 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-xfree)\"\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..dedecee6d80f1 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,9 @@ u32 bench_alloc_size;\n u32 bench_nallocs;\n long bench_hits;\n long bench_duration_ns;\n+long bench_free_duration_ns;\n+long bench_free_hits;\n+bool bench_collect;\n \n SEC(\"syscall\")\n int bench_malloc(void)\n@@ -49,3 +52,110 @@ 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 alloc_ns = 0, free_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\talloc_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\tstart_ns = bpf_ktime_get_ns();\n+\t\tarena_free(head);\n+\t\tfree_ns += bpf_ktime_get_ns() - start_ns;\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, alloc_ns);\n+\t__sync_add_and_fetch(\u0026bench_free_duration_ns, free_ns);\n+\t__sync_add_and_fetch(\u0026bench_hits, nallocs);\n+\t__sync_add_and_fetch(\u0026bench_free_hits, nallocs);\n+\treturn 0;\n+}\n+\n+/* List left behind by the last bench_malloc_xfree() invocation. */\n+struct bench_alloc __arena *bench_xfree_head;\n+\n+SEC(\"syscall\")\n+int bench_malloc_xfree(void)\n+{\n+\tstruct bench_alloc __arena *next, *obj, *old, *cur, *head = NULL;\n+\tu64 alloc_ns = 0, free_ns = 0, start_ns;\n+\tu32 i, nallocs, nfrees;\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\talloc_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+\t/*\n+\t * Publish this list and take the one left by the previous invocation,\n+\t * usually allocated by a worker on another CPU. If the exchange does\n+\t * not complete, free our own list instead.\n+\t */\n+\told = READ_ONCE(bench_xfree_head);\n+\twhile (can_loop) {\n+\t\tcur = cmpxchg(\u0026bench_xfree_head, old, head);\n+\t\tif (cur == old) {\n+\t\t\thead = old;\n+\t\t\tbreak;\n+\t\t}\n+\t\told = cur;\n+\t}\n+\n+\tfor (i = zero; head \u0026\u0026 can_loop; i++) {\n+\t\tnext = head-\u003enext;\n+\t\tstart_ns = bpf_ktime_get_ns();\n+\t\tarena_free(head);\n+\t\tfree_ns += bpf_ktime_get_ns() - start_ns;\n+\t\thead = next;\n+\t}\n+\tnfrees = i;\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, alloc_ns);\n+\t__sync_add_and_fetch(\u0026bench_free_duration_ns, free_ns);\n+\t__sync_add_and_fetch(\u0026bench_hits, nallocs);\n+\t__sync_add_and_fetch(\u0026bench_free_hits, nfrees);\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..1c62ae6e3fd0d 100644\n--- a/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h\n+++ b/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h\n@@ -49,6 +49,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@@ -65,10 +67,60 @@ struct buddy_chunk {\n \tstruct buddy_chunk __arena\t*next;\n };\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/* Objects moved to and from the buddy allocator at once, as an order. */\n+\tBUDDY_CACHE_BATCH_ORDER = 3,\n+\tBUDDY_CACHE_BATCH_SIZE = 1 \u003c\u003c BUDDY_CACHE_BATCH_ORDER,\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+\t/* Claimed by an in-flight cache operation on this CPU. */\n+\tunsigned int active;\n+\tstruct buddy_cache_order orders[BUDDY_CACHE_NUM_ORDERS];\n+};\n+\n+/* Per-order array shared between the per-CPU caches. */\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 {\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 __arena *percpu[BUDDY_CACHE_MAX_CPUS];\n+\tstruct buddy_transfer_order transfer[BUDDY_CACHE_NUM_ORDERS];\n+\tstruct buddy_deferred_free __arena *deferred_free;\n };\n \n #ifdef __BPF__\n@@ -76,6 +128,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/common.h b/tools/testing/selftests/bpf/libarena/include/libarena/common.h\nindex a6220355a6975..a20947911d340 100644\n--- a/tools/testing/selftests/bpf/libarena/include/libarena/common.h\n+++ b/tools/testing/selftests/bpf/libarena/include/libarena/common.h\n@@ -22,6 +22,10 @@\n #define __maybe_unused __attribute__((__unused__))\n #endif\n \n+#ifndef min\n+#define min(a, b) ((a) \u003c (b) ? (a) : (b))\n+#endif\n+\n #define private(name) SEC(\".data.\" #name) __hidden __attribute__((aligned(8)))\n \n #define ARENA_PAGES (1UL \u003c\u003c (32 - __builtin_ffs(__PAGE_SIZE) + 1))\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..a25326a3b9a04 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@@ -17,6 +17,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 +52,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.percpu[0]) {\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.percpu[0])\n+\t\t\treturn -EINVAL;\n \t}\n \n \treturn 0;\n@@ -210,4 +239,248 @@ __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 __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+\tcpu = bpf_get_smp_processor_id();\n+\tcache = buddy.percpu[cpu];\n+\tif (!cache) {\n+\t\tret = -EINVAL;\n+\t\tgoto out;\n+\t}\n+\tslots = \u0026cache-\u003eorders[TEST_CACHE_ORDER];\n+\ttransfer = \u0026buddy.transfer[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..bd52fd9c3146f 100644\n--- a/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c\n+++ b/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c\n@@ -6,6 +6,10 @@\n #include \u003clibarena/buddy.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 +42,298 @@\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 int idx_is_allocated(struct buddy_chunk __arena *chunk, u64 idx, bool *allocated);\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+\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 buddy_order_cached(order) false\n+#else\n+#define buddy_order_cached(order) ((order) \u003c BUDDY_CACHE_NUM_ORDERS)\n+#endif\n+\n+static __always_inline struct buddy_cache __arena *\n+buddy_cache_enter(struct buddy __arena *buddy)\n+{\n+\tstruct buddy_cache __arena *cache;\n+\n+\t/* The caches only exist between buddy_init() and buddy_destroy(). */\n+\tcache = buddy-\u003epercpu[bpf_get_smp_processor_id()];\n+\tif (unlikely(!cache))\n+\t\treturn NULL;\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+\tif (cmpxchg(\u0026cache-\u003eactive, 0, 1))\n+\t\treturn NULL;\n+\n+\treturn cache;\n+}\n+\n+static __always_inline void buddy_cache_exit(struct buddy_cache __arena *cache)\n+{\n+\tsmp_store_release(\u0026cache-\u003eactive, 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 __arena *buddy, int order,\n+\t\t\t\t struct buddy_cache_order __arena *slots)\n+{\n+\tstruct buddy_transfer_order __arena *transfer = \u0026buddy-\u003etransfer[order];\n+\tunsigned long flags;\n+\tu32 elems, start;\n+\tunsigned int i;\n+\tint ret = 0;\n+\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 __arena *buddy, int order,\n+\t\t\t\t struct buddy_cache_order __arena *slots)\n+{\n+\tstruct buddy_transfer_order __arena *transfer = \u0026buddy-\u003etransfer[order];\n+\tunsigned long flags;\n+\tu32 elems, local_elems, start;\n+\tunsigned int i;\n+\tint ret = 0;\n+\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+/*\n+ * Refill an empty cache from the shared transfer array, or with one trip to the\n+ * buddy allocator. Kept out of line so that cache hits do not pay for the refill\n+ * path.\n+ */\n+static __noinline void __arena *\n+buddy_cache_refill(struct buddy __arena *buddy, struct buddy_cache __arena *cache,\n+\t\t size_t size, int order)\n+{\n+\tstruct buddy_cache_order __arena *slots = \u0026cache-\u003eorders[order];\n+\n+\t/* Peek without the lock, buddy_transfer_take_batch() rechecks under it. */\n+\tif (READ_ONCE(buddy-\u003etransfer[order].elems) \u003e= BUDDY_CACHE_BATCH_SIZE \u0026\u0026\n+\t !buddy_transfer_take_batch(buddy, order, slots))\n+\t\treturn buddy_cache_pop(cache, order);\n+\n+\tif (buddy_alloc_bulk(buddy, size, BUDDY_CACHE_BATCH_ORDER,\n+\t\t\t (void __arena * __arena *)slots-\u003eobjects))\n+\t\treturn NULL;\n+\n+\tWRITE_ONCE(slots-\u003eelems, BUDDY_CACHE_BATCH_SIZE - 1);\n+\treturn slots-\u003eobjects[BUDDY_CACHE_BATCH_SIZE - 1];\n+}\n+\n+void __arena *buddy_cache_alloc(struct buddy __arena *buddy, size_t size, size_t order)\n+{\n+\tstruct buddy_cache __arena *cache;\n+\tvoid __arena *address;\n+\n+\tif (!buddy_order_cached(order))\n+\t\treturn NULL;\n+\n+\tcache = buddy_cache_enter(buddy);\n+\tif (!cache)\n+\t\treturn NULL;\n+\n+\t/* NMIs cannot take the buddy lock, so they only use what is already cached. */\n+\taddress = buddy_cache_pop(cache, order);\n+\tif (!address \u0026\u0026 !bpf_in_nmi())\n+\t\taddress = buddy_cache_refill(buddy, cache, size, order);\n+\n+\tbuddy_cache_exit(cache);\n+\n+\treturn address;\n+}\n+\n+/*\n+ * Retrieve the order of an allocation from the address. Reject addresses\n+ * that do not start an allocated block so that the locked path can report\n+ * them, instead of caching memory that overlaps a live allocation. Cached\n+ * objects stay marked as allocated, so freeing an object that is already\n+ * cached is not detected.\n+ */\n+static int buddy_cache_get_order(void __arena *addr)\n+{\n+\tstruct buddy_chunk __arena *chunk;\n+\tbool allocated;\n+\tu64 idx;\n+\n+\tif ((u64)addr \u0026 (BUDDY_MIN_ALLOC_BYTES - 1))\n+\t\treturn BUDDY_CHUNK_NUM_ORDERS;\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+\tif (idx_is_allocated(chunk, idx, \u0026allocated) || !allocated)\n+\t\treturn BUDDY_CHUNK_NUM_ORDERS;\n+\n+\treturn idx_get_order(chunk, idx);\n+}\n+\n+/*\n+ * Make room in a full cache by moving its top batch to the shared transfer\n+ * array, or to the buddy allocator when the array is full or busy, then cache\n+ * the freed object. Kept out of line so that frees that fit in the cache do not\n+ * pay for it.\n+ */\n+static __noinline int\n+buddy_cache_spill(struct buddy __arena *buddy, struct buddy_cache __arena *cache,\n+\t\t int order, void __arena *addr)\n+{\n+\tstruct buddy_cache_order __arena *slots = \u0026cache-\u003eorders[order];\n+\tu32 elems;\n+\tint ret = -ENOMEM;\n+\n+\t/*\n+\t * Peek without the lock, buddy_transfer_put_batch() rechecks under it.\n+\t * NMIs never take the transfer lock.\n+\t */\n+\tif (!bpf_in_nmi() \u0026\u0026\n+\t READ_ONCE(buddy-\u003etransfer[order].elems) \u003c=\n+\t BUDDY_TRANSFER_CAPACITY - BUDDY_CACHE_BATCH_SIZE)\n+\t\tret = buddy_transfer_put_batch(buddy, order, slots);\n+\n+\tif (ret) {\n+\t\telems = READ_ONCE(slots-\u003eelems) - BUDDY_CACHE_BATCH_SIZE;\n+\t\tret = buddy_free_bulk(buddy, (void __arena * __arena *)\u0026slots-\u003eobjects[elems],\n+\t\t\t\t BUDDY_CACHE_BATCH_SIZE);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\n+\t\tWRITE_ONCE(slots-\u003eelems, elems);\n+\t}\n+\n+\treturn buddy_cache_push(cache, order, addr);\n+}\n \n+int buddy_cache_free(struct buddy __arena *buddy, void __arena *addr)\n+{\n+\tstruct buddy_cache __arena *cache;\n+\tint order;\n+\tint ret = 0;\n+\n+\t/* Skip the metadata lookup when caching is compiled out. */\n+\tif (!buddy_order_cached(0))\n+\t\treturn -EINVAL;\n+\n+\torder = buddy_cache_get_order(addr);\n+\tif (!buddy_order_cached(order))\n+\t\treturn -EINVAL;\n+\n+\tcache = buddy_cache_enter(buddy);\n+\tif (!cache)\n+\t\treturn -EAGAIN;\n+\n+\tret = buddy_cache_push(cache, order, addr);\n+\tif (ret)\n+\t\tret = buddy_cache_spill(buddy, cache, order, addr);\n+\tbuddy_cache_exit(cache);\n+\n+\treturn ret;\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 +841,46 @@ static struct buddy_chunk __arena *buddy_chunk_get(struct buddy __arena *buddy)\n \treturn chunk;\n }\n \n+static int buddy_cache_init(struct buddy __arena *buddy)\n+{\n+\tstruct buddy_cache __arena *cache;\n+\tunsigned int cpu, order;\n+\tint ret;\n+\n+\tif (!buddy_order_cached(0))\n+\t\treturn 0;\n+\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(buddy-\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 *)\u0026buddy-\u003epercpu[cpu]);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\n+\t\tcache = buddy-\u003epercpu[cpu];\n+\t\tWRITE_ONCE(cache-\u003eactive, 0);\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+\treturn 0;\n+}\n+\n+static void buddy_cache_destroy(struct buddy __arena *buddy)\n+{\n+\tunsigned int cpu, order;\n+\n+\tfor (cpu = zero; cpu \u003c min(CONFIG_NR_CPUS, BUDDY_CACHE_MAX_CPUS) \u0026\u0026 can_loop; cpu++)\n+\t\tWRITE_ONCE(buddy-\u003epercpu[cpu], NULL);\n+\tfor (order = zero; order \u003c BUDDY_CACHE_NUM_ORDERS \u0026\u0026 can_loop; order++)\n+\t\tWRITE_ONCE(buddy-\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 +889,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 +915,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_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 +940,8 @@ __weak int buddy_destroy(struct buddy __arena *buddy)\n \tif (!buddy)\n \t\treturn -EINVAL;\n \n+\tbuddy_cache_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 +960,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 +973,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 +995,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 +1018,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 +1051,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 +1070,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 +1079,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 +1086,45 @@ 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+\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 +1132,81 @@ 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+\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-\tasan_unpoison(address, size);\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+\tif (!buddy)\n+\t\treturn NULL;\n+\n+\torder = size_to_order(size);\n+\taddress = buddy_cache_alloc(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,10 +1287,102 @@ 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/*\n+\t * Chain the allocations into a local linked list. Possible because\n+\t * allocations are 16 bytes minimum and so fit a pointer.\n+\t */\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/* Splice the local list onto the deferred-free list. */\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-\tint ret;\n+\tint ret, err;\n \n \tif (!buddy)\n \t\treturn -EINVAL;\n@@ -889,15 +1391,75 @@ __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_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\terr = buddy_deferred_free_complete(buddy);\n+\t\tret = ret ?: err;\n+\t}\n+\n \tbuddy_unlock(buddy, flags);\n \n \treturn ret;\n }\n \n+/*\n+ * Fails only when the buddy lock cannot be taken, in which case nothing was\n+ * freed. Objects that buddy_free_unlocked() rejects are reported there.\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 ret;\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\tbuddy_free_unlocked(buddy, (u64)addrs[i]);\n+\t}\n+\n+\t/* Drain any frees stranded during an NMI. */\n+\tif (unlikely(buddy_deferred_free_pending(buddy)))\n+\t\tbuddy_deferred_free_complete(buddy);\n+\n+\tbuddy_unlock(buddy, flags);\n+\n+\treturn 0;\n+}\n+\n __weak char _license[] SEC(\"license\") = \"GPL\";\n"
} |
| 2/1 | 2026/10/22 11:54 | agent | patch-evaluator |
0mModel:core Args: null Results: {
"EnableConfigs": null,
"FocusSymbols": [],
"Reasoning": "The patch only touches files under tools/testing/selftests/bpf/, which are user-space benchmark tools and BPF selftest libraries (libarena buddy allocator enhancements). None of the changes affect the compiled kernel image (vmlinux) or kernel modules, so they are not fuzzable by 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 ed1f59733d4e60fde4deb2968edd6c86b85dcf94
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Oct 22 11:54:46 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..fbbd553990195 100644
--- a/tools/testing/selftests/bpf/bench.c
+++ b/tools/testing/selftests/bpf/bench.c
@@ -589,6 +589,8 @@ 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;
+extern const struct bench bench_libarena_malloc_xfree;
static const struct bench *benchs[] = {
&bench_count_global,
@@ -675,6 +677,8 @@ static const struct bench *benchs[] = {
&bench_xdp_lb,
&bench_libarena_malloc,
&bench_libarena_calloc,
+ &bench_libarena_malloc_free,
+ &bench_libarena_malloc_xfree,
};
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..4096b54db520a 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,62 @@ 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 malloc_xfree_setup(void)
+{
+ setup_common();
+ ctx.bench_fd = bpf_program__fd(ctx.skel->progs.bench_malloc_xfree);
+ 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 +203,13 @@ 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)
+{
+ 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 +217,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 +237,30 @@ 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)
+{
+ long alloc_ns, free_ns, hits, free_hits;
+
+ __atomic_store_n(&ctx.stop, true, __ATOMIC_RELEASE);
+ while (__atomic_load_n(&ctx.stopped, __ATOMIC_ACQUIRE) != env.producer_cnt)
+ usleep(1000);
+
+ alloc_ns = __atomic_load_n(&ctx.skel->bss->bench_duration_ns, __ATOMIC_RELAXED);
+ free_ns = __atomic_load_n(&ctx.skel->bss->bench_free_duration_ns, __ATOMIC_RELAXED);
+ hits = __atomic_load_n(&ctx.skel->bss->bench_hits, __ATOMIC_RELAXED);
+ free_hits = __atomic_load_n(&ctx.skel->bss->bench_free_hits, __ATOMIC_RELAXED);
+
+ if (!hits || !free_hits) {
+ printf("Summary: no runs measured\n");
+ return;
+ }
+
+ printf("Summary: %.3lf ns/allocation, %.3lf ns/free (%ld allocations)\n",
+ alloc_ns / (double)hits, free_ns / (double)free_hits, hits);
}
const struct bench bench_libarena_malloc = {
@@ -208,3 +284,23 @@ 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,
+};
+
+const struct bench bench_libarena_malloc_xfree = {
+ .name = "libarena-malloc-xfree",
+ .argp = &bench_libarena_argp,
+ .validate = malloc_free_validate,
+ .setup = malloc_xfree_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..2dfa32cd95cc5 100755
--- a/tools/testing/selftests/bpf/benchs/run_bench_libarena.sh
+++ b/tools/testing/selftests/bpf/benchs/run_bench_libarena.sh
@@ -29,3 +29,19 @@ 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
+
+header "libarena parallel cross-CPU 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-xfree)"
+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..dedecee6d80f1 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,9 @@ u32 bench_alloc_size;
u32 bench_nallocs;
long bench_hits;
long bench_duration_ns;
+long bench_free_duration_ns;
+long bench_free_hits;
+bool bench_collect;
SEC("syscall")
int bench_malloc(void)
@@ -49,3 +52,110 @@ 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 alloc_ns = 0, free_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);
+ alloc_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;
+ start_ns = bpf_ktime_get_ns();
+ arena_free(head);
+ free_ns += bpf_ktime_get_ns() - start_ns;
+ head = next;
+ }
+
+ if (nallocs != bench_nallocs)
+ return -ENOMEM;
+
+ if (!collect)
+ return 0;
+
+ __sync_add_and_fetch(&bench_duration_ns, alloc_ns);
+ __sync_add_and_fetch(&bench_free_duration_ns, free_ns);
+ __sync_add_and_fetch(&bench_hits, nallocs);
+ __sync_add_and_fetch(&bench_free_hits, nallocs);
+ return 0;
+}
+
+/* List left behind by the last bench_malloc_xfree() invocation. */
+struct bench_alloc __arena *bench_xfree_head;
+
+SEC("syscall")
+int bench_malloc_xfree(void)
+{
+ struct bench_alloc __arena *next, *obj, *old, *cur, *head = NULL;
+ u64 alloc_ns = 0, free_ns = 0, start_ns;
+ u32 i, nallocs, nfrees;
+ 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);
+ alloc_ns += bpf_ktime_get_ns() - start_ns;
+ if (!obj)
+ break;
+
+ obj->next = head;
+ head = obj;
+ }
+ nallocs = i;
+
+ /*
+ * Publish this list and take the one left by the previous invocation,
+ * usually allocated by a worker on another CPU. If the exchange does
+ * not complete, free our own list instead.
+ */
+ old = READ_ONCE(bench_xfree_head);
+ while (can_loop) {
+ cur = cmpxchg(&bench_xfree_head, old, head);
+ if (cur == old) {
+ head = old;
+ break;
+ }
+ old = cur;
+ }
+
+ for (i = zero; head && can_loop; i++) {
+ next = head->next;
+ start_ns = bpf_ktime_get_ns();
+ arena_free(head);
+ free_ns += bpf_ktime_get_ns() - start_ns;
+ head = next;
+ }
+ nfrees = i;
+
+ if (nallocs != bench_nallocs)
+ return -ENOMEM;
+
+ if (!collect)
+ return 0;
+
+ __sync_add_and_fetch(&bench_duration_ns, alloc_ns);
+ __sync_add_and_fetch(&bench_free_duration_ns, free_ns);
+ __sync_add_and_fetch(&bench_hits, nallocs);
+ __sync_add_and_fetch(&bench_free_hits, nfrees);
+ 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..1c62ae6e3fd0d 100644
--- a/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h
+++ b/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h
@@ -49,6 +49,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.
*/
@@ -65,10 +67,60 @@ struct buddy_chunk {
struct buddy_chunk __arena *next;
};
+/* 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,
+
+ /* Objects moved to and from the buddy allocator at once, as an order. */
+ BUDDY_CACHE_BATCH_ORDER = 3,
+ BUDDY_CACHE_BATCH_SIZE = 1 << BUDDY_CACHE_BATCH_ORDER,
+
+ /*
+ * Objects retained locally for each allocation order.
+ */
+ BUDDY_CACHE_CAPACITY = 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 {
+ /* Claimed by an in-flight cache operation on this CPU. */
+ unsigned int active;
+ struct buddy_cache_order orders[BUDDY_CACHE_NUM_ORDERS];
+};
+
+/* Per-order array shared between the per-CPU caches. */
+struct buddy_transfer_order {
+ arena_spinlock_t lock;
+ u32 elems;
+ void __arena *objects[BUDDY_TRANSFER_CAPACITY];
+} __attribute__((aligned(64)));
+
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 __arena *percpu[BUDDY_CACHE_MAX_CPUS];
+ struct buddy_transfer_order transfer[BUDDY_CACHE_NUM_ORDERS];
+ struct buddy_deferred_free __arena *deferred_free;
};
#ifdef __BPF__
@@ -76,6 +128,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/common.h b/tools/testing/selftests/bpf/libarena/include/libarena/common.h
index a6220355a6975..a20947911d340 100644
--- a/tools/testing/selftests/bpf/libarena/include/libarena/common.h
+++ b/tools/testing/selftests/bpf/libarena/include/libarena/common.h
@@ -22,6 +22,10 @@
#define __maybe_unused __attribute__((__unused__))
#endif
+#ifndef min
+#define min(a, b) ((a) < (b) ? (a) : (b))
+#endif
+
#define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8)))
#define ARENA_PAGES (1UL << (32 - __builtin_ffs(__PAGE_SIZE) + 1))
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..a25326a3b9a04 100644
--- a/tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c
+++ b/tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c
@@ -17,6 +17,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 +52,20 @@ __weak int test_buddy_create(void)
if (ret)
return ret;
+#ifndef BPF_ARENA_ASAN
+ if (!buddy.percpu[0]) {
+ buddy_destroy(&buddy);
+ return -EINVAL;
+ }
+#endif
+
ret = buddy_destroy(&buddy);
if (ret)
return ret;
+ if (buddy.deferred_free)
+ return -EINVAL;
+ if (buddy.percpu[0])
+ return -EINVAL;
}
return 0;
@@ -210,4 +239,248 @@ __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 __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;
+
+ cpu = bpf_get_smp_processor_id();
+ cache = buddy.percpu[cpu];
+ if (!cache) {
+ ret = -EINVAL;
+ goto out;
+ }
+ slots = &cache->orders[TEST_CACHE_ORDER];
+ transfer = &buddy.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..bd52fd9c3146f 100644
--- a/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
+++ b/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
@@ -6,6 +6,10 @@
#include <libarena/buddy.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 +42,298 @@
* 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 int idx_is_allocated(struct buddy_chunk __arena *chunk, u64 idx, bool *allocated);
+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);
+
+#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 buddy_order_cached(order) false
+#else
+#define buddy_order_cached(order) ((order) < BUDDY_CACHE_NUM_ORDERS)
+#endif
+
+static __always_inline struct buddy_cache __arena *
+buddy_cache_enter(struct buddy __arena *buddy)
+{
+ struct buddy_cache __arena *cache;
+
+ /* The caches only exist between buddy_init() and buddy_destroy(). */
+ cache = buddy->percpu[bpf_get_smp_processor_id()];
+ if (unlikely(!cache))
+ return NULL;
+
+ /*
+ * 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.
+ */
+ if (cmpxchg(&cache->active, 0, 1))
+ return NULL;
+
+ return cache;
+}
+
+static __always_inline void buddy_cache_exit(struct buddy_cache __arena *cache)
+{
+ smp_store_release(&cache->active, 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 __arena *buddy, int order,
+ struct buddy_cache_order __arena *slots)
+{
+ struct buddy_transfer_order __arena *transfer = &buddy->transfer[order];
+ unsigned long flags;
+ u32 elems, start;
+ unsigned int i;
+ int ret = 0;
+
+ 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 __arena *buddy, int order,
+ struct buddy_cache_order __arena *slots)
+{
+ struct buddy_transfer_order __arena *transfer = &buddy->transfer[order];
+ unsigned long flags;
+ u32 elems, local_elems, start;
+ unsigned int i;
+ int ret = 0;
+
+ 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;
+}
+
+/*
+ * Refill an empty cache from the shared transfer array, or with one trip to the
+ * buddy allocator. Kept out of line so that cache hits do not pay for the refill
+ * path.
+ */
+static __noinline void __arena *
+buddy_cache_refill(struct buddy __arena *buddy, struct buddy_cache __arena *cache,
+ size_t size, int order)
+{
+ struct buddy_cache_order __arena *slots = &cache->orders[order];
+
+ /* Peek without the lock, buddy_transfer_take_batch() rechecks under it. */
+ if (READ_ONCE(buddy->transfer[order].elems) >= BUDDY_CACHE_BATCH_SIZE &&
+ !buddy_transfer_take_batch(buddy, order, slots))
+ return buddy_cache_pop(cache, order);
+
+ if (buddy_alloc_bulk(buddy, size, BUDDY_CACHE_BATCH_ORDER,
+ (void __arena * __arena *)slots->objects))
+ return NULL;
+
+ WRITE_ONCE(slots->elems, BUDDY_CACHE_BATCH_SIZE - 1);
+ return slots->objects[BUDDY_CACHE_BATCH_SIZE - 1];
+}
+
+void __arena *buddy_cache_alloc(struct buddy __arena *buddy, size_t size, size_t order)
+{
+ struct buddy_cache __arena *cache;
+ void __arena *address;
+
+ if (!buddy_order_cached(order))
+ return NULL;
+
+ cache = buddy_cache_enter(buddy);
+ if (!cache)
+ return NULL;
+
+ /* NMIs cannot take the buddy lock, so they only use what is already cached. */
+ address = buddy_cache_pop(cache, order);
+ if (!address && !bpf_in_nmi())
+ address = buddy_cache_refill(buddy, cache, size, order);
+
+ buddy_cache_exit(cache);
+
+ return address;
+}
+
+/*
+ * Retrieve the order of an allocation from the address. Reject addresses
+ * that do not start an allocated block so that the locked path can report
+ * them, instead of caching memory that overlaps a live allocation. Cached
+ * objects stay marked as allocated, so freeing an object that is already
+ * cached is not detected.
+ */
+static int buddy_cache_get_order(void __arena *addr)
+{
+ struct buddy_chunk __arena *chunk;
+ bool allocated;
+ u64 idx;
+
+ if ((u64)addr & (BUDDY_MIN_ALLOC_BYTES - 1))
+ return BUDDY_CHUNK_NUM_ORDERS;
+
+ chunk = (void __arena *)((u64)addr & ~BUDDY_CHUNK_OFFSET_MASK);
+ idx = ((u64)addr & BUDDY_CHUNK_OFFSET_MASK) / BUDDY_MIN_ALLOC_BYTES;
+
+ if (idx_is_allocated(chunk, idx, &allocated) || !allocated)
+ return BUDDY_CHUNK_NUM_ORDERS;
+
+ return idx_get_order(chunk, idx);
+}
+
+/*
+ * Make room in a full cache by moving its top batch to the shared transfer
+ * array, or to the buddy allocator when the array is full or busy, then cache
+ * the freed object. Kept out of line so that frees that fit in the cache do not
+ * pay for it.
+ */
+static __noinline int
+buddy_cache_spill(struct buddy __arena *buddy, struct buddy_cache __arena *cache,
+ int order, void __arena *addr)
+{
+ struct buddy_cache_order __arena *slots = &cache->orders[order];
+ u32 elems;
+ int ret = -ENOMEM;
+
+ /*
+ * Peek without the lock, buddy_transfer_put_batch() rechecks under it.
+ * NMIs never take the transfer lock.
+ */
+ if (!bpf_in_nmi() &&
+ READ_ONCE(buddy->transfer[order].elems) <=
+ BUDDY_TRANSFER_CAPACITY - BUDDY_CACHE_BATCH_SIZE)
+ ret = buddy_transfer_put_batch(buddy, order, slots);
+
+ if (ret) {
+ elems = READ_ONCE(slots->elems) - BUDDY_CACHE_BATCH_SIZE;
+ ret = buddy_free_bulk(buddy, (void __arena * __arena *)&slots->objects[elems],
+ BUDDY_CACHE_BATCH_SIZE);
+ if (ret)
+ return ret;
+
+ WRITE_ONCE(slots->elems, elems);
+ }
+
+ return buddy_cache_push(cache, order, addr);
+}
+int buddy_cache_free(struct buddy __arena *buddy, void __arena *addr)
+{
+ struct buddy_cache __arena *cache;
+ int order;
+ int ret = 0;
+
+ /* Skip the metadata lookup when caching is compiled out. */
+ if (!buddy_order_cached(0))
+ return -EINVAL;
+
+ order = buddy_cache_get_order(addr);
+ if (!buddy_order_cached(order))
+ return -EINVAL;
+
+ cache = buddy_cache_enter(buddy);
+ if (!cache)
+ return -EAGAIN;
+
+ ret = buddy_cache_push(cache, order, addr);
+ if (ret)
+ ret = buddy_cache_spill(buddy, cache, order, addr);
+ buddy_cache_exit(cache);
+
+ return ret;
+}
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 +841,46 @@ static struct buddy_chunk __arena *buddy_chunk_get(struct buddy __arena *buddy)
return chunk;
}
+static int buddy_cache_init(struct buddy __arena *buddy)
+{
+ struct buddy_cache __arena *cache;
+ unsigned int cpu, order;
+ int ret;
+
+ if (!buddy_order_cached(0))
+ return 0;
+
+ if (CONFIG_NR_CPUS > BUDDY_CACHE_MAX_CPUS)
+ return -E2BIG;
+
+ for (order = zero; order < BUDDY_CACHE_NUM_ORDERS && can_loop; order++)
+ WRITE_ONCE(buddy->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 *)&buddy->percpu[cpu]);
+ if (ret)
+ return ret;
+
+ cache = buddy->percpu[cpu];
+ WRITE_ONCE(cache->active, 0);
+ for (order = zero; order < BUDDY_CACHE_NUM_ORDERS && can_loop; order++)
+ WRITE_ONCE(cache->orders[order].elems, 0);
+ }
+
+ return 0;
+}
+
+static void buddy_cache_destroy(struct buddy __arena *buddy)
+{
+ unsigned int cpu, order;
+
+ for (cpu = zero; cpu < min(CONFIG_NR_CPUS, BUDDY_CACHE_MAX_CPUS) && can_loop; cpu++)
+ WRITE_ONCE(buddy->percpu[cpu], NULL);
+ for (order = zero; order < BUDDY_CACHE_NUM_ORDERS && can_loop; order++)
+ WRITE_ONCE(buddy->transfer[order].elems, 0);
+}
+
__weak int buddy_init(struct buddy __arena *buddy)
{
struct buddy_chunk __arena *chunk;
@@ -565,6 +889,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 +915,16 @@ __weak int buddy_init(struct buddy __arena *buddy)
buddy_unlock(buddy, flags);
- return chunk ? 0 : -ENOMEM;
+ if (!chunk)
+ return -ENOMEM;
+
+ ret = buddy_cache_init(buddy);
+ if (ret) {
+ buddy_destroy(buddy);
+ return ret;
+ }
+
+ return 0;
}
/*
@@ -606,6 +940,8 @@ __weak int buddy_destroy(struct buddy __arena *buddy)
if (!buddy)
return -EINVAL;
+ buddy_cache_destroy(buddy);
+
/*
* Traverse all buddy chunks and free them back to the arena
* with the same granularity they were allocated with.
@@ -624,11 +960,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 +973,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 +995,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 +1018,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 +1051,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 +1070,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 +1079,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 +1086,45 @@ 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 +1132,81 @@ 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;
+
+ 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(address, size);
+ 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;
+
+ if (!buddy)
+ return NULL;
+
+ order = size_to_order(size);
+ address = buddy_cache_alloc(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,10 +1287,102 @@ 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;
+
+ /*
+ * Chain the allocations into a local linked list. Possible because
+ * allocations are 16 bytes minimum and so fit a pointer.
+ */
+ 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;
+
+ /* Splice the local list onto the deferred-free list. */
+ 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;
- int ret;
+ int ret, err;
if (!buddy)
return -EINVAL;
@@ -889,15 +1391,75 @@ __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_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))) {
+ err = buddy_deferred_free_complete(buddy);
+ ret = ret ?: err;
+ }
+
buddy_unlock(buddy, flags);
return ret;
}
+/*
+ * Fails only when the buddy lock cannot be taken, in which case nothing was
+ * freed. Objects that buddy_free_unlocked() rejects are reported there.
+ */
+__weak int buddy_free_bulk(struct buddy __arena *buddy, void __arena * __arena *addrs, size_t naddrs)
+{
+ unsigned long flags;
+ unsigned int i;
+ int ret;
+
+ 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;
+
+ buddy_free_unlocked(buddy, (u64)addrs[i]);
+ }
+
+ /* Drain any frees stranded during an NMI. */
+ if (unlikely(buddy_deferred_free_pending(buddy)))
+ buddy_deferred_free_complete(buddy);
+
+ buddy_unlock(buddy, flags);
+
+ return 0;
+}
+
__weak char _license[] SEC("license") = "GPL";
|
| 3/2 | 2026/10/22 11:54 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 21133 output: 27 thoughts: 1018 Thoughts: **Patch Series Evaluation for Syzkaller Triage** Okay, so they want me to assess this patch series for syzkaller triage. My first move is always to see what files are touched. Alright, let's take a look. Looks like a bunch of files: `tools/testing/selftests/bpf/bench.c`, and a whole bunch of other files within that `tools/testing/selftests/bpf/` directory, including some stuff related to `libarena`. Wait a minute... *all* of the changes are in that `tools/testing/selftests/bpf/` path. That's interesting. I know the drill; let's check the rules to see if this is something we should immediately reject. The system says to return `WorthFuzzing=false` if the changes fall *strictly* into a few categories. One of those categories is changes within user-space directories, like `tools/`, as long as they don't affect the compiled kernel image (`vmlinux`) or kernel modules. Okay, let's double-check the `git diff` output to make sure I'm not missing anything. Nope, every single file is under that same path. This is key. Are any of these files affecting the compiled kernel image? No! The changes are all self-tests. Specifically, they seem to be modifications related to `libarena`. I know this code pretty well. It's a userspace/BPF self-test arena library and benchmark. It compiles as BPF bytecode for self-tests and user-space benchmarks. Nothing here is compiled into `vmlinux` or a kernel module. I'm almost positive but let's do a quick double-check with `git grep` or inspect the kernel files just to be 100% sure. I want to be *absolutely* certain there isn't some obscure build configuration that would pull something from `tools/` into the kernel proper. |
| 4/2 | 2026/10/22 11:54 | tool | grepper |
0mError:no matches Args: {
"Expression": "tools/testing/selftests/bpf/libarena"
}Results: {
"error": "no matches"
} |
| 5/2 | 2026/10/22 11:55 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 22613 output: 16 thoughts: 31 Thoughts: Okay, here's my summary, considering my expertise: **Checking for Code Dependencies** Alright, before I dive too deep into this, I need to understand the scope of `buddy.bpf.c`. My immediate thought is, "Where *is* this file used?" So, I'm going to quickly ascertain if there are any direct references to `buddy.bpf.c` within the core kernel source. I'll need to explore the `kernel/`, `net/`, and `include/` directories – these are the most likely locations for potential code that interacts with or relies on that BPF program. Finding those references will instantly give me insight into its role and impact. This quick check will give me a clear starting point for any deeper analysis I need to do. I need to know the dependencies before I can properly understand the implications of this file. |
| 6/2 | 2026/10/22 11:55 | tool | grepper |
0mArgs:{
"Expression": "libarena"
}Results: {
"Output": "\nFull output is too long, showing 200 out of 555 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (25 files in total):\ntools/testing/selftests/bpf/Makefile:14\ntools/testing/selftests/bpf/Makefile.buildvars:4\ntools/testing/selftests/bpf/Makefile.skel:1\ntools/testing/selftests/bpf/bench.c:10\ntools/testing/selftests/bpf/benchs/bench_libarena.c:26\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:12\ntools/testing/selftests/bpf/libarena/Makefile:19\ntools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c:3\ntools/testing/selftests/bpf/libarena/include/libarena/userspace.h:10\ntools/testing/selftests/bpf/libarena/selftests/test_asan_buddy.bpf.c:3\ntools/testing/selftests/bpf/libarena/selftests/test_bitmap.bpf.c:3\ntools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c:3\ntools/testing/selftests/bpf/libarena/selftests/test_parallel_bitmap.bpf.c:3\ntools/testing/selftests/bpf/libarena/selftests/test_parallel_spmc.bpf.c:3\ntools/testing/selftests/bpf/libarena/selftests/test_rbtree.bpf.c:3\ntools/testing/selftests/bpf/libarena/selftests/test_spmc.bpf.c:3\ntools/testing/selftests/bpf/libarena/src/asan.bpf.c:2\ntools/testing/selftests/bpf/libarena/src/bitmap.bpf.c:3\ntools/testing/selftests/bpf/libarena/src/buddy.bpf.c:3\ntools/testing/selftests/bpf/libarena/src/common.bpf.c:3\ntools/testing/selftests/bpf/libarena/src/rbtree.bpf.c:3\ntools/testing/selftests/bpf/libarena/src/spmc.bpf.c:3\ntools/testing/selftests/bpf/prog_tests/libarena.c:34\ntools/testing/selftests/bpf/prog_tests/libarena_asan.c:19\ntools/testing/selftests/bpf/progs/arena_spin_lock.c:2\n\ntools/testing/selftests/bpf/Makefile=72=override define CLEAN\n--\ntools/testing/selftests/bpf/Makefile-79-\t$(Q)$(MAKE) -C test_kmods clean\ntools/testing/selftests/bpf/Makefile:80:\t$(Q)$(MAKE) -C libarena clean\ntools/testing/selftests/bpf/Makefile-81-\t$(Q)$(MAKE) docs-clean\n--\ntools/testing/selftests/bpf/Makefile=395=LIBARENA_MAKE_ARGS = \\\n--\ntools/testing/selftests/bpf/Makefile-405-\ntools/testing/selftests/bpf/Makefile:406:LIBARENA_COMMON_DEPS := $(wildcard libarena/Makefile\t\t\\\ntools/testing/selftests/bpf/Makefile:407:\t\t\t\t libarena/include/*\t\t\\\ntools/testing/selftests/bpf/Makefile:408:\t\t\t\t libarena/include/libarena/*\t\\\ntools/testing/selftests/bpf/Makefile:409:\t\t\t\t libarena/src/*)\ntools/testing/selftests/bpf/Makefile:410:LIBARENA_BPF_DEPS := $(LIBARENA_COMMON_DEPS) $(wildcard libarena/selftests/*)\ntools/testing/selftests/bpf/Makefile:411:LIBARENA_BENCH_BPF_DEPS := $(LIBARENA_COMMON_DEPS) $(wildcard libarena/benchs/*)\ntools/testing/selftests/bpf/Makefile-412-\ntools/testing/selftests/bpf/Makefile:413:LIBARENA_BENCH_SKEL := libarena/libarena_bench.skel.h\ntools/testing/selftests/bpf/Makefile-414-\ntools/testing/selftests/bpf/Makefile=415=$(LIBARENA_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS)\ntools/testing/selftests/bpf/Makefile:416:\t+$(MAKE) -C libarena libarena.skel.h $(LIBARENA_MAKE_ARGS)\ntools/testing/selftests/bpf/Makefile-417-\ntools/testing/selftests/bpf/Makefile=418=$(LIBARENA_BENCH_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BENCH_BPF_DEPS) | $(LIBARENA_SKEL)\ntools/testing/selftests/bpf/Makefile:419:\t+$(MAKE) -C libarena benchmarks $(LIBARENA_MAKE_ARGS)\ntools/testing/selftests/bpf/Makefile-420-\n--\ntools/testing/selftests/bpf/Makefile=422=$(LIBARENA_ASAN_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) $(LIBARENA_BPF_DEPS)\ntools/testing/selftests/bpf/Makefile:423:\t+$(MAKE) -C libarena libarena_asan.skel.h $(LIBARENA_MAKE_ARGS)\ntools/testing/selftests/bpf/Makefile-424-endif\n--\ntools/testing/selftests/bpf/Makefile=637=$(OUTPUT)/bench_xdp_lb.o: $(OUTPUT)/xdp_lb_bench.skel.h bench_bpf_timing.h\ntools/testing/selftests/bpf/Makefile:638:$(OUTPUT)/bench_libarena.o: $(LIBARENA_BENCH_SKEL)\ntools/testing/selftests/bpf/Makefile-639-$(OUTPUT)/bench_bpf_timing.o: bench_bpf_timing.h\n--\ntools/testing/selftests/bpf/Makefile=704=endef\ntools/testing/selftests/bpf/Makefile-705-\ntools/testing/selftests/bpf/Makefile:706:libarena: $(LIBARENA_SKEL)\ntools/testing/selftests/bpf/Makefile-707-\ntools/testing/selftests/bpf/Makefile=708=ifneq ($(CLANG_HAS_ARENA_ASAN),)\ntools/testing/selftests/bpf/Makefile:709:libarena_asan: $(LIBARENA_ASAN_SKEL)\ntools/testing/selftests/bpf/Makefile-710-endif\n--\ntools/testing/selftests/bpf/Makefile.buildvars=71=COMMON_CFLAGS = -g $(OPT_FLAGS) -rdynamic -std=gnu11\t\t\t\t\\\n--\ntools/testing/selftests/bpf/Makefile.buildvars-76-\t -I$(TOOLSINCDIR) -I$(TOOLSARCHINCDIR) -I$(APIDIR) -I$(OUTPUT)\t\\\ntools/testing/selftests/bpf/Makefile.buildvars:77:\t -I$(CURDIR)/libarena/include\ntools/testing/selftests/bpf/Makefile.buildvars-78-LDLIBS += $(LIBELF_LIBS) -lz -lrt -lpthread\n--\ntools/testing/selftests/bpf/Makefile.buildvars=154=BPF_CFLAGS = -g -Wall -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN)\t\\\ntools/testing/selftests/bpf/Makefile.buildvars-155-\t -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR)\t\t\t\\\ntools/testing/selftests/bpf/Makefile.buildvars:156:\t -I$(TOOLSINCDIR) -I$(CURDIR)/libarena/include\t\t\\\ntools/testing/selftests/bpf/Makefile.buildvars-157-\t -I$(abspath $(OUTPUT)/../usr/include)\t\t\t\\\n--\ntools/testing/selftests/bpf/Makefile.buildvars=169=PRIVATE_KEY := $(BUILD_DIR)/signing_key.pem\ntools/testing/selftests/bpf/Makefile.buildvars-170-\ntools/testing/selftests/bpf/Makefile.buildvars:171:LIBARENA_SKEL := libarena/libarena.skel.h\ntools/testing/selftests/bpf/Makefile.buildvars-172-\ntools/testing/selftests/bpf/Makefile.buildvars=173=ifneq ($(CLANG_HAS_ARENA_ASAN),)\ntools/testing/selftests/bpf/Makefile.buildvars:174:LIBARENA_ASAN_SKEL := libarena/libarena_asan.skel.h\ntools/testing/selftests/bpf/Makefile.buildvars-175-CFLAGS += -DHAS_BPF_ARENA_ASAN\n--\ntools/testing/selftests/bpf/Makefile.skel=71=HEADERS_FOR_BPF_OBJS := $(wildcard $(BPFDIR)/*.bpf.h)\t\t\\\ntools/testing/selftests/bpf/Makefile.skel:72:\t\t\t$(wildcard $(CURDIR)/libarena/include/*.[ch])\t\\\ntools/testing/selftests/bpf/Makefile.skel-73-\t\t\t$(addprefix $(BPFDIR)/,\tbpf_core_read.h\t\\\n--\ntools/testing/selftests/bpf/bench.c=290=extern struct argp bench_xdp_lb_argp;\ntools/testing/selftests/bpf/bench.c:291:extern struct argp bench_libarena_argp;\ntools/testing/selftests/bpf/bench.c-292-\ntools/testing/selftests/bpf/bench.c=293=static const struct argp_child bench_parsers[] = {\n--\ntools/testing/selftests/bpf/bench.c-309-\t{ \u0026bench_xdp_lb_argp, 0, \"XDP load-balancer benchmark\", 0 },\ntools/testing/selftests/bpf/bench.c:310:\t{ \u0026bench_libarena_argp, 0, \"libarena allocator benchmark\", 0 },\ntools/testing/selftests/bpf/bench.c-311-\t{},\n--\ntools/testing/selftests/bpf/bench.c=589=extern const struct bench bench_xdp_lb;\ntools/testing/selftests/bpf/bench.c:590:extern const struct bench bench_libarena_malloc;\ntools/testing/selftests/bpf/bench.c:591:extern const struct bench bench_libarena_calloc;\ntools/testing/selftests/bpf/bench.c:592:extern const struct bench bench_libarena_malloc_free;\ntools/testing/selftests/bpf/bench.c:593:extern const struct bench bench_libarena_malloc_xfree;\ntools/testing/selftests/bpf/bench.c-594-\ntools/testing/selftests/bpf/bench.c=595=static const struct bench *benchs[] = {\n--\ntools/testing/selftests/bpf/bench.c-677-\t\u0026bench_xdp_lb,\ntools/testing/selftests/bpf/bench.c:678:\t\u0026bench_libarena_malloc,\ntools/testing/selftests/bpf/bench.c:679:\t\u0026bench_libarena_calloc,\ntools/testing/selftests/bpf/bench.c:680:\t\u0026bench_libarena_malloc_free,\ntools/testing/selftests/bpf/bench.c:681:\t\u0026bench_libarena_malloc_xfree,\ntools/testing/selftests/bpf/bench.c-682-};\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c-8-\ntools/testing/selftests/bpf/benchs/bench_libarena.c:9:#include \u003clibarena/common.h\u003e\ntools/testing/selftests/bpf/benchs/bench_libarena.c:10:#include \u003clibarena/asan.h\u003e\ntools/testing/selftests/bpf/benchs/bench_libarena.c:11:#include \u003clibarena/buddy.h\u003e\ntools/testing/selftests/bpf/benchs/bench_libarena.c:12:#include \u003clibarena/userspace.h\u003e\ntools/testing/selftests/bpf/benchs/bench_libarena.c-13-\ntools/testing/selftests/bpf/benchs/bench_libarena.c:14:#include \"libarena/libarena_bench.skel.h\"\ntools/testing/selftests/bpf/benchs/bench_libarena.c-15-\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c=24=static struct {\ntools/testing/selftests/bpf/benchs/bench_libarena.c:25:\tstruct libarena_bench *skel;\ntools/testing/selftests/bpf/benchs/bench_libarena.c-26-\tint bench_fd;\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c=47=static error_t parse_arg(int key, char *arg, struct argp_state *state)\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c-69-\ntools/testing/selftests/bpf/benchs/bench_libarena.c:70:const struct argp bench_libarena_argp = {\ntools/testing/selftests/bpf/benchs/bench_libarena.c-71-\t.options = opts,\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c=108=static void setup_common(void)\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c-116-\ntools/testing/selftests/bpf/benchs/bench_libarena.c:117:\tctx.skel = libarena_bench__open_and_load();\ntools/testing/selftests/bpf/benchs/bench_libarena.c-118-\tif (!ctx.skel) {\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c-122-\ntools/testing/selftests/bpf/benchs/bench_libarena.c:123:\terr = libarena_run_prog_args(\ntools/testing/selftests/bpf/benchs/bench_libarena.c-124-\t\tbpf_program__fd(ctx.skel-\u003eprogs.arena_alloc_reserve),\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c-130-\ntools/testing/selftests/bpf/benchs/bench_libarena.c:131:\terr = libarena_run_prog(\ntools/testing/selftests/bpf/benchs/bench_libarena.c-132-\t\tbpf_program__fd(ctx.skel-\u003eprogs.arena_buddy_reset));\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c=175=static void *producer(void *input)\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c-179-\twhile (!__atomic_load_n(\u0026ctx.stop, __ATOMIC_ACQUIRE)) {\ntools/testing/selftests/bpf/benchs/bench_libarena.c:180:\t\terr = libarena_run_prog(ctx.bench_fd);\ntools/testing/selftests/bpf/benchs/bench_libarena.c-181-\t\tif (err) {\ntools/testing/selftests/bpf/benchs/bench_libarena.c:182:\t\t\tfprintf(stderr, \"libarena benchmark failed: %d\\n\", err);\ntools/testing/selftests/bpf/benchs/bench_libarena.c-183-\t\t\texit(1);\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c-186-\t\tif (ctx.reset_each_run) {\ntools/testing/selftests/bpf/benchs/bench_libarena.c:187:\t\t\terr = libarena_run_prog(ctx.reset_fd);\ntools/testing/selftests/bpf/benchs/bench_libarena.c-188-\t\t\tif (err) {\ntools/testing/selftests/bpf/benchs/bench_libarena.c:189:\t\t\t\tfprintf(stderr, \"libarena alloc reset failed: %d\\n\", err);\ntools/testing/selftests/bpf/benchs/bench_libarena.c-190-\t\t\t\texit(1);\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c=244=static void malloc_free_report_final(struct bench_res res[], int res_cnt)\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c-265-\ntools/testing/selftests/bpf/benchs/bench_libarena.c:266:const struct bench bench_libarena_malloc = {\ntools/testing/selftests/bpf/benchs/bench_libarena.c:267:\t.name = \"libarena-malloc\",\ntools/testing/selftests/bpf/benchs/bench_libarena.c:268:\t.argp = \u0026bench_libarena_argp,\ntools/testing/selftests/bpf/benchs/bench_libarena.c-269-\t.validate = validate,\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c-276-\ntools/testing/selftests/bpf/benchs/bench_libarena.c:277:const struct bench bench_libarena_calloc = {\ntools/testing/selftests/bpf/benchs/bench_libarena.c:278:\t.name = \"libarena-calloc\",\ntools/testing/selftests/bpf/benchs/bench_libarena.c:279:\t.argp = \u0026bench_libarena_argp,\ntools/testing/selftests/bpf/benchs/bench_libarena.c-280-\t.validate = validate,\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c-287-\ntools/testing/selftests/bpf/benchs/bench_libarena.c:288:const struct bench bench_libarena_malloc_free = {\ntools/testing/selftests/bpf/benchs/bench_libarena.c:289:\t.name = \"libarena-malloc-free\",\ntools/testing/selftests/bpf/benchs/bench_libarena.c:290:\t.argp = \u0026bench_libarena_argp,\ntools/testing/selftests/bpf/benchs/bench_libarena.c-291-\t.validate = malloc_free_validate,\n--\ntools/testing/selftests/bpf/benchs/bench_libarena.c-297-\ntools/testing/selftests/bpf/benchs/bench_libarena.c:298:const struct bench bench_libarena_malloc_xfree = {\ntools/testing/selftests/bpf/benchs/bench_libarena.c:299:\t.name = \"libarena-malloc-xfree\",\ntools/testing/selftests/bpf/benchs/bench_libarena.c:300:\t.argp = \u0026bench_libarena_argp,\ntools/testing/selftests/bpf/benchs/bench_libarena.c-301-\t.validate = malloc_free_validate,\n--\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh=8=RUN_BENCH=\"./bench -d3 -q\"\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-9-\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:10:summarize_libarena()\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-11-{\n--\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-19-\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:20:header \"libarena sequential malloc\\n\"\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-21-\n--\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh=24=printf \"\\t-------------------\\n\"\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-25-\tfor nallocs in 10 50 100 500 1000 5000 10000; do\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:26:\t\tsummarize_libarena \"malloc:\" \\\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:27:\t\t\t\"$($RUN_BENCH --alloc_size \"$size\" --nallocs \"$nallocs\" libarena-malloc)\"\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:28:\t\tsummarize_libarena \"calloc:\" \\\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:29:\t\t\t\"$($RUN_BENCH --alloc_size \"$size\" --nallocs \"$nallocs\" libarena-calloc)\"\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-30-\tdone\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh=31=done\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-32-\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:33:header \"libarena parallel malloc/free\\n\"\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-34-\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh=35=for producers in 1 2 4 8; do\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:36:\tsummarize_libarena \"$producers producers:\" \\\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-37-\t\t\"$($RUN_BENCH -a -p \"$producers\" --alloc_size 64 --nallocs 1000 \\\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:38:\t\tlibarena-malloc-free)\"\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-39-done\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-40-\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:41:header \"libarena parallel cross-CPU malloc/free\\n\"\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-42-\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh=43=for producers in 1 2 4 8; do\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:44:\tsummarize_libarena \"$producers producers:\" \\\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-45-\t\t\"$($RUN_BENCH -a -p \"$producers\" --alloc_size 64 --nallocs 1000 \\\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh:46:\t\tlibarena-malloc-xfree)\"\ntools/testing/selftests/bpf/benchs/run_bench_libarena.sh-47-done\n--\ntools/testing/selftests/bpf/libarena/Makefile=70=vpath %.c $(LIBARENA)/src $(LIBARENA)/selftests $(LIBARENA)/benchs\ntools/testing/selftests/bpf/libarena/Makefile-71-\ntools/testing/selftests/bpf/libarena/Makefile:72:skeletons: libarena.skel.h libarena_asan.skel.h\ntools/testing/selftests/bpf/libarena/Makefile-73-.PHONY: skeletons\ntools/testing/selftests/bpf/libarena/Makefile-74-\n\n"
} |
| 7/2 | 2026/10/22 11:55 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29346 output: 97 thoughts: 644 Thoughts: **My Assessment: This Patch is Not Worth Fuzzing** Alright, let's break this down. First, I need to check the criteria for determining if this patch is worth fuzzing. I'm looking specifically at SECTION 2. The core question is whether the changes introduce new attack surfaces relevant to the kernel image. Okay, all the modified files are neatly organized under `tools/testing/selftests/bpf/`. That's a good start – it suggests a focused area of modification. I then quickly scanned the file list: `bench.c`, `bench_libarena.c`, `run_bench_libarena.sh`, and a bunch of files related to a `libarena` implementation including a buddy allocator: `bench_malloc.bpf.c`, include headers, and self-test files. Crucially, none of these seem to be compiled into `vmlinux` or kernel modules. They're all BPF self-test code, benchmarks, and library code within that dedicated testing directory. So, according to the criteria, this patch falls squarely into the "non-kernel, user-space" category. Specifically, it's modifications to "user-space directories, self-tests... that do not affect the compiled kernel image (vmlinux) or kernel modules." It seems like the patch introduces per-CPU caching and bulk alloc/free support to the selftest libarena buddy allocator, all within that self-test context. None of this touches the core kernel. Therefore, `WorthFuzzing` has to be `false`. And consequently, `FocusSymbols` and `EnableConfigs` should be empty arrays. The concise reasoning I'd use is this: "The patch exclusively modifies BPF selftest files and benchmarks within `tools/testing/selftests/bpf/` (adding per-CPU caching and bulk alloc/free support to the selftest libarena buddy allocator). None of the changes modify the kernel image (vmlinux) or any kernel modules, making them non-kernel code that is not fuzzable by syzkaller." I'll call `set-results` with this assessment. |
| 8/2 | 2026/10/22 11:55 | tool | set-results |
0mArgs:{
"EnableConfigs": [],
"FocusSymbols": [],
"Reasoning": "The patch only touches files under tools/testing/selftests/bpf/, which are user-space benchmark tools and BPF selftest libraries (libarena buddy allocator enhancements). None of the changes affect the compiled kernel image (vmlinux) or kernel modules, so they are not fuzzable by syzkaller.",
"WorthFuzzing": false
}Results: {
"EnableConfigs": null,
"FocusSymbols": [],
"Reasoning": "The patch only touches files under tools/testing/selftests/bpf/, which are user-space benchmark tools and BPF selftest libraries (libarena buddy allocator enhancements). None of the changes affect the compiled kernel image (vmlinux) or kernel modules, so they are not fuzzable by syzkaller.",
"WorthFuzzing": false
} |
| 9/1 | 2026/10/22 11:55 | action | If |
0mArgs:{
"WorthFuzzing": false
}Results: null |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|