Skip to content

Commit 2574917

Browse files
bentissJiri Kosina
authored andcommitted
selftests: hid: prepare tests for HID_BPF API change
We plan on changing the return value of hid_bpf_attach_prog(). Instead of returning an error code, it will return an fd to a bpf_link. This bpf_link is responsible for the binding between the bpf program and the hid device. Add a fallback mechanism to not break bisections by pinning the program when we run this test against the non changed kernel. Signed-off-by: Benjamin Tissoires <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent d9db1bb commit 2574917

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

tools/testing/selftests/hid/hid_bpf.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,21 @@ FIXTURE(hid_bpf) {
444444
int hid_id;
445445
pthread_t tid;
446446
struct hid *skel;
447+
int hid_links[3]; /* max number of programs loaded in a single test */
447448
};
448449
static void detach_bpf(FIXTURE_DATA(hid_bpf) * self)
449450
{
451+
int i;
452+
450453
if (self->hidraw_fd)
451454
close(self->hidraw_fd);
452455
self->hidraw_fd = 0;
453456

457+
for (i = 0; i < ARRAY_SIZE(self->hid_links); i++) {
458+
if (self->hid_links[i])
459+
close(self->hid_links[i]);
460+
}
461+
454462
hid__destroy(self->skel);
455463
self->skel = NULL;
456464
}
@@ -512,6 +520,9 @@ static void load_programs(const struct test_program programs[],
512520
.ctx_size_in = sizeof(args),
513521
);
514522

523+
ASSERT_LE(progs_count, ARRAY_SIZE(self->hid_links))
524+
TH_LOG("too many programs are to be loaded");
525+
515526
/* open the bpf file */
516527
self->skel = hid__open();
517528
ASSERT_OK_PTR(self->skel) TEARDOWN_LOG("Error while calling hid__open");
@@ -543,7 +554,10 @@ static void load_programs(const struct test_program programs[],
543554
args.hid = self->hid_id;
544555
args.insert_head = programs[i].insert_head;
545556
err = bpf_prog_test_run_opts(attach_fd, &tattr);
546-
ASSERT_OK(args.retval) TH_LOG("attach_hid(%s): %d", programs[i].name, args.retval);
557+
ASSERT_GE(args.retval, 0)
558+
TH_LOG("attach_hid(%s): %d", programs[i].name, args.retval);
559+
560+
self->hid_links[i] = args.retval;
547561
}
548562

549563
self->hidraw_fd = open_hidraw(self->dev_id);
@@ -619,10 +633,17 @@ TEST_F(hid_bpf, test_attach_detach)
619633
{ .name = "hid_second_event" },
620634
};
621635
__u8 buf[10] = {0};
622-
int err;
636+
int err, link;
623637

624638
LOAD_PROGRAMS(progs);
625639

640+
link = self->hid_links[0];
641+
/* we might not be using the new code path where hid_bpf_attach_prog()
642+
* returns a link.
643+
*/
644+
if (!link)
645+
link = bpf_program__fd(self->skel->progs.hid_first_event);
646+
626647
/* inject one event */
627648
buf[0] = 1;
628649
buf[1] = 42;
@@ -640,8 +661,8 @@ TEST_F(hid_bpf, test_attach_detach)
640661

641662
/* pin the first program and immediately unpin it */
642663
#define PIN_PATH "/sys/fs/bpf/hid_first_event"
643-
err = bpf_program__pin(self->skel->progs.hid_first_event, PIN_PATH);
644-
ASSERT_OK(err) TH_LOG("error while calling bpf_program__pin");
664+
err = bpf_obj_pin(link, PIN_PATH);
665+
ASSERT_OK(err) TH_LOG("error while calling bpf_obj_pin");
645666
remove(PIN_PATH);
646667
#undef PIN_PATH
647668
usleep(100000);

0 commit comments

Comments
 (0)