Skip to content

Commit cea6c4d

Browse files
bentissJiri Kosina
authored andcommitted
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 <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 633ba3b commit cea6c4d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

tools/testing/selftests/hid/hid_bpf.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ TEST_F(hid_bpf, test_attach_detach)
616616
{
617617
const struct test_program progs[] = {
618618
{ .name = "hid_first_event" },
619+
{ .name = "hid_second_event" },
619620
};
620621
__u8 buf[10] = {0};
621622
int err;
@@ -634,7 +635,10 @@ TEST_F(hid_bpf, test_attach_detach)
634635
ASSERT_EQ(buf[0], 1);
635636
ASSERT_EQ(buf[2], 47);
636637

637-
/* pin the program and immediately unpin it */
638+
/* make sure both programs are run */
639+
ASSERT_EQ(buf[3], 52);
640+
641+
/* pin the first program and immediately unpin it */
638642
#define PIN_PATH "/sys/fs/bpf/hid_first_event"
639643
bpf_program__pin(self->skel->progs.hid_first_event, PIN_PATH);
640644
remove(PIN_PATH);
@@ -660,6 +664,7 @@ TEST_F(hid_bpf, test_attach_detach)
660664
ASSERT_EQ(buf[0], 1);
661665
ASSERT_EQ(buf[1], 47);
662666
ASSERT_EQ(buf[2], 0);
667+
ASSERT_EQ(buf[3], 0);
663668

664669
/* re-attach our program */
665670

@@ -677,6 +682,7 @@ TEST_F(hid_bpf, test_attach_detach)
677682
ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
678683
ASSERT_EQ(buf[0], 1);
679684
ASSERT_EQ(buf[2], 47);
685+
ASSERT_EQ(buf[3], 52);
680686
}
681687

682688
/*

tools/testing/selftests/hid/progs/hid.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ int BPF_PROG(hid_first_event, struct hid_bpf_ctx *hid_ctx)
3232
return hid_ctx->size;
3333
}
3434

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+
3548
SEC("?fmod_ret/hid_bpf_device_event")
3649
int BPF_PROG(hid_change_report_id, struct hid_bpf_ctx *hid_ctx)
3750
{

0 commit comments

Comments
 (0)