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

selftests/bpf: add raw_tp/tp_btf BPF cookie subtests

Add test validating BPF cookie can be passed during raw_tp/tp_btf
attachment and can be retried at runtime with bpf_get_attach_cookie()
helper.

Acked-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Message-ID: <20240319233852.1977493-6-andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
51146ff0 36ffb202

+129 -1
+113 -1
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
··· 573 573 close(lsm_fd); 574 574 } 575 575 576 + static void tp_btf_subtest(struct test_bpf_cookie *skel) 577 + { 578 + __u64 cookie; 579 + int prog_fd, link_fd = -1; 580 + struct bpf_link *link = NULL; 581 + LIBBPF_OPTS(bpf_link_create_opts, link_opts); 582 + LIBBPF_OPTS(bpf_raw_tp_opts, raw_tp_opts); 583 + LIBBPF_OPTS(bpf_trace_opts, trace_opts); 584 + 585 + /* There are three different ways to attach tp_btf (BTF-aware raw 586 + * tracepoint) programs. Let's test all of them. 587 + */ 588 + prog_fd = bpf_program__fd(skel->progs.handle_tp_btf); 589 + 590 + /* low-level BPF_RAW_TRACEPOINT_OPEN command wrapper */ 591 + skel->bss->tp_btf_res = 0; 592 + 593 + raw_tp_opts.cookie = cookie = 0x11000000000000L; 594 + link_fd = bpf_raw_tracepoint_open_opts(prog_fd, &raw_tp_opts); 595 + if (!ASSERT_GE(link_fd, 0, "bpf_raw_tracepoint_open_opts")) 596 + goto cleanup; 597 + 598 + usleep(1); /* trigger */ 599 + close(link_fd); /* detach */ 600 + link_fd = -1; 601 + 602 + ASSERT_EQ(skel->bss->tp_btf_res, cookie, "raw_tp_open_res"); 603 + 604 + /* low-level generic bpf_link_create() API */ 605 + skel->bss->tp_btf_res = 0; 606 + 607 + link_opts.tracing.cookie = cookie = 0x22000000000000L; 608 + link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_RAW_TP, &link_opts); 609 + if (!ASSERT_GE(link_fd, 0, "bpf_link_create")) 610 + goto cleanup; 611 + 612 + usleep(1); /* trigger */ 613 + close(link_fd); /* detach */ 614 + link_fd = -1; 615 + 616 + ASSERT_EQ(skel->bss->tp_btf_res, cookie, "link_create_res"); 617 + 618 + /* high-level bpf_link-based bpf_program__attach_trace_opts() API */ 619 + skel->bss->tp_btf_res = 0; 620 + 621 + trace_opts.cookie = cookie = 0x33000000000000L; 622 + link = bpf_program__attach_trace_opts(skel->progs.handle_tp_btf, &trace_opts); 623 + if (!ASSERT_OK_PTR(link, "attach_trace_opts")) 624 + goto cleanup; 625 + 626 + usleep(1); /* trigger */ 627 + bpf_link__destroy(link); /* detach */ 628 + link = NULL; 629 + 630 + ASSERT_EQ(skel->bss->tp_btf_res, cookie, "attach_trace_opts_res"); 631 + 632 + cleanup: 633 + if (link_fd >= 0) 634 + close(link_fd); 635 + bpf_link__destroy(link); 636 + } 637 + 638 + static void raw_tp_subtest(struct test_bpf_cookie *skel) 639 + { 640 + __u64 cookie; 641 + int prog_fd, link_fd = -1; 642 + struct bpf_link *link = NULL; 643 + LIBBPF_OPTS(bpf_raw_tp_opts, raw_tp_opts); 644 + LIBBPF_OPTS(bpf_raw_tracepoint_opts, opts); 645 + 646 + /* There are two different ways to attach raw_tp programs */ 647 + prog_fd = bpf_program__fd(skel->progs.handle_raw_tp); 648 + 649 + /* low-level BPF_RAW_TRACEPOINT_OPEN command wrapper */ 650 + skel->bss->raw_tp_res = 0; 651 + 652 + raw_tp_opts.tp_name = "sys_enter"; 653 + raw_tp_opts.cookie = cookie = 0x55000000000000L; 654 + link_fd = bpf_raw_tracepoint_open_opts(prog_fd, &raw_tp_opts); 655 + if (!ASSERT_GE(link_fd, 0, "bpf_raw_tracepoint_open_opts")) 656 + goto cleanup; 657 + 658 + usleep(1); /* trigger */ 659 + close(link_fd); /* detach */ 660 + link_fd = -1; 661 + 662 + ASSERT_EQ(skel->bss->raw_tp_res, cookie, "raw_tp_open_res"); 663 + 664 + /* high-level bpf_link-based bpf_program__attach_raw_tracepoint_opts() API */ 665 + skel->bss->raw_tp_res = 0; 666 + 667 + opts.cookie = cookie = 0x66000000000000L; 668 + link = bpf_program__attach_raw_tracepoint_opts(skel->progs.handle_raw_tp, 669 + "sys_enter", &opts); 670 + if (!ASSERT_OK_PTR(link, "attach_raw_tp_opts")) 671 + goto cleanup; 672 + 673 + usleep(1); /* trigger */ 674 + bpf_link__destroy(link); /* detach */ 675 + link = NULL; 676 + 677 + ASSERT_EQ(skel->bss->raw_tp_res, cookie, "attach_raw_tp_opts_res"); 678 + 679 + cleanup: 680 + if (link_fd >= 0) 681 + close(link_fd); 682 + bpf_link__destroy(link); 683 + } 684 + 576 685 void test_bpf_cookie(void) 577 686 { 578 687 struct test_bpf_cookie *skel; ··· 710 601 tracing_subtest(skel); 711 602 if (test__start_subtest("lsm")) 712 603 lsm_subtest(skel); 713 - 604 + if (test__start_subtest("tp_btf")) 605 + tp_btf_subtest(skel); 606 + if (test__start_subtest("raw_tp")) 607 + raw_tp_subtest(skel); 714 608 test_bpf_cookie__destroy(skel); 715 609 }
+16
tools/testing/selftests/bpf/progs/test_bpf_cookie.c
··· 15 15 __u64 uretprobe_res; 16 16 __u64 tp_res; 17 17 __u64 pe_res; 18 + __u64 raw_tp_res; 19 + __u64 tp_btf_res; 18 20 __u64 fentry_res; 19 21 __u64 fexit_res; 20 22 __u64 fmod_ret_res; ··· 86 84 int handle_pe(struct pt_regs *ctx) 87 85 { 88 86 update(ctx, &pe_res); 87 + return 0; 88 + } 89 + 90 + SEC("raw_tp/sys_enter") 91 + int handle_raw_tp(void *ctx) 92 + { 93 + update(ctx, &raw_tp_res); 94 + return 0; 95 + } 96 + 97 + SEC("tp_btf/sys_enter") 98 + int handle_tp_btf(void *ctx) 99 + { 100 + update(ctx, &tp_btf_res); 89 101 return 0; 90 102 } 91 103