Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add kprobe multi write ctx attach test

Adding test to check we can't attach kprobe multi program
that writes to the context.

It's x86_64 specific test.

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/r/20250916215301.664963-7-jolsa@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Jiri Olsa and committed by
Alexei Starovoitov
3d237467 1b881ee2

+34
+27
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
··· 7 7 #include "kprobe_multi_session.skel.h" 8 8 #include "kprobe_multi_session_cookie.skel.h" 9 9 #include "kprobe_multi_verifier.skel.h" 10 + #include "kprobe_write_ctx.skel.h" 10 11 #include "bpf/libbpf_internal.h" 11 12 #include "bpf/hashmap.h" 12 13 ··· 540 539 kprobe_multi_override__destroy(skel); 541 540 } 542 541 542 + #ifdef __x86_64__ 543 + static void test_attach_write_ctx(void) 544 + { 545 + struct kprobe_write_ctx *skel = NULL; 546 + struct bpf_link *link = NULL; 547 + 548 + skel = kprobe_write_ctx__open_and_load(); 549 + if (!ASSERT_OK_PTR(skel, "kprobe_write_ctx__open_and_load")) 550 + return; 551 + 552 + link = bpf_program__attach_kprobe_opts(skel->progs.kprobe_multi_write_ctx, 553 + "bpf_fentry_test1", NULL); 554 + if (!ASSERT_ERR_PTR(link, "bpf_program__attach_kprobe_opts")) 555 + bpf_link__destroy(link); 556 + 557 + kprobe_write_ctx__destroy(skel); 558 + } 559 + #else 560 + static void test_attach_write_ctx(void) 561 + { 562 + test__skip(); 563 + } 564 + #endif 565 + 543 566 void serial_test_kprobe_multi_bench_attach(void) 544 567 { 545 568 if (test__start_subtest("kernel")) ··· 603 578 test_session_cookie_skel_api(); 604 579 if (test__start_subtest("unique_match")) 605 580 test_unique_match(); 581 + if (test__start_subtest("attach_write_ctx")) 582 + test_attach_write_ctx(); 606 583 RUN_TESTS(kprobe_multi_verifier); 607 584 }
+7
tools/testing/selftests/bpf/progs/kprobe_write_ctx.c
··· 12 12 ctx->ax = 0; 13 13 return 0; 14 14 } 15 + 16 + SEC("kprobe.multi") 17 + int kprobe_multi_write_ctx(struct pt_regs *ctx) 18 + { 19 + ctx->ax = 0; 20 + return 0; 21 + } 15 22 #endif