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

selftests: hid: attach/detach 2 bpf programs, not just one

Add a second BPF program to attach to the device, as the development of
this feature showed that we also need to ensure we can detach multiple
programs to a device (hid_bpf_link->hid_table_index was actually not set
initially, and this lead to any BPF program not being released except for
the first one).

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>

authored by

Benjamin Tissoires and committed by
Jiri Kosina
cea6c4d9 633ba3be

+20 -1
+7 -1
tools/testing/selftests/hid/hid_bpf.c
··· 616 616 { 617 617 const struct test_program progs[] = { 618 618 { .name = "hid_first_event" }, 619 + { .name = "hid_second_event" }, 619 620 }; 620 621 __u8 buf[10] = {0}; 621 622 int err; ··· 635 634 ASSERT_EQ(buf[0], 1); 636 635 ASSERT_EQ(buf[2], 47); 637 636 638 - /* pin the program and immediately unpin it */ 637 + /* make sure both programs are run */ 638 + ASSERT_EQ(buf[3], 52); 639 + 640 + /* pin the first program and immediately unpin it */ 639 641 #define PIN_PATH "/sys/fs/bpf/hid_first_event" 640 642 bpf_program__pin(self->skel->progs.hid_first_event, PIN_PATH); 641 643 remove(PIN_PATH); ··· 664 660 ASSERT_EQ(buf[0], 1); 665 661 ASSERT_EQ(buf[1], 47); 666 662 ASSERT_EQ(buf[2], 0); 663 + ASSERT_EQ(buf[3], 0); 667 664 668 665 /* re-attach our program */ 669 666 ··· 682 677 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 683 678 ASSERT_EQ(buf[0], 1); 684 679 ASSERT_EQ(buf[2], 47); 680 + ASSERT_EQ(buf[3], 52); 685 681 } 686 682 687 683 /*
+13
tools/testing/selftests/hid/progs/hid.c
··· 33 33 } 34 34 35 35 SEC("?fmod_ret/hid_bpf_device_event") 36 + int BPF_PROG(hid_second_event, struct hid_bpf_ctx *hid_ctx) 37 + { 38 + __u8 *rw_data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 4 /* size */); 39 + 40 + if (!rw_data) 41 + return 0; /* EPERM check */ 42 + 43 + rw_data[3] = rw_data[2] + 5; 44 + 45 + return hid_ctx->size; 46 + } 47 + 48 + SEC("?fmod_ret/hid_bpf_device_event") 36 49 int BPF_PROG(hid_change_report_id, struct hid_bpf_ctx *hid_ctx) 37 50 { 38 51 __u8 *rw_data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 3 /* size */);