]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
selftests/hid: add wq test for hid_bpf_input_report()
authorBenjamin Tissoires <bentiss@kernel.org>
Wed, 26 Jun 2024 13:46:31 +0000 (15:46 +0200)
committerBenjamin Tissoires <bentiss@kernel.org>
Thu, 27 Jun 2024 09:00:48 +0000 (11:00 +0200)
Now that bpf_wq is available, we can write a test with it. Having
hid_bpf_input_report() waiting for the device means that we can
directly call it, and we get that event when the device is ready.

Link: https://patch.msgid.link/20240626-hid_hw_req_bpf-v2-10-cfd60fb6c79f@kernel.org
Acked-by: Jiri Kosina <jkosina@suse.com>
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
tools/testing/selftests/hid/hid_bpf.c
tools/testing/selftests/hid/progs/hid.c
tools/testing/selftests/hid/progs/hid_bpf_helpers.h

index 40aedd1d9dc58973e8582bd474deee735ee2b673..31637b3b8db557d573c4c7dcb86787c302a133c8 100644 (file)
@@ -1130,6 +1130,44 @@ TEST_F(hid_bpf, test_hid_infinite_loop_output_report_call)
                                 err);
 }
 
+/*
+ * Attach hid_multiply_event_wq to the given uhid device,
+ * retrieve and open the matching hidraw node,
+ * inject one event in the uhid device,
+ * check that the program sees it and can add extra data
+ */
+TEST_F(hid_bpf, test_multiply_events_wq)
+{
+       const struct test_program progs[] = {
+               { .name = "hid_test_multiply_events_wq" },
+       };
+       __u8 buf[10] = {0};
+       int err;
+
+       LOAD_PROGRAMS(progs);
+
+       /* inject one event */
+       buf[0] = 1;
+       buf[1] = 42;
+       uhid_send_event(_metadata, self->uhid_fd, buf, 6);
+
+       /* read the data from hidraw */
+       memset(buf, 0, sizeof(buf));
+       err = read(self->hidraw_fd, buf, sizeof(buf));
+       ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
+       ASSERT_EQ(buf[0], 1);
+       ASSERT_EQ(buf[1], 47);
+
+       usleep(100000);
+
+       /* read the data from hidraw */
+       memset(buf, 0, sizeof(buf));
+       err = read(self->hidraw_fd, buf, sizeof(buf));
+       ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
+       ASSERT_EQ(buf[0], 2);
+       ASSERT_EQ(buf[1], 3);
+}
+
 /*
  * Attach hid_insert{0,1,2} to the given uhid device,
  * retrieve and open the matching hidraw node,
index 1fa288b76cd5024270d18b9369c3099c6a27ddfc..f539a7a223cf88ddc20d58e90c76e9138e3f3671 100644 (file)
@@ -443,3 +443,82 @@ SEC(".struct_ops.link")
 struct hid_bpf_ops test_infinite_loop_output_report = {
        .hid_hw_output_report = (void *)hid_test_infinite_loop_output_report,
 };
+
+struct elem {
+       struct bpf_wq work;
+};
+
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __uint(max_entries, 1);
+       __type(key, int);
+       __type(value, struct elem);
+} hmap SEC(".maps");
+
+static int wq_cb_sleepable(void *map, int *key, struct bpf_wq *work)
+{
+       __u8 buf[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
+       struct hid_bpf_ctx *hid_ctx;
+
+       hid_ctx = hid_bpf_allocate_context(*key);
+       if (!hid_ctx)
+               return 0; /* EPERM check */
+
+       hid_bpf_input_report(hid_ctx, HID_INPUT_REPORT, buf, sizeof(buf));
+
+       hid_bpf_release_context(hid_ctx);
+
+       return 0;
+}
+
+static int test_inject_input_report_callback(int *key)
+{
+       struct elem init = {}, *val;
+       struct bpf_wq *wq;
+
+       if (bpf_map_update_elem(&hmap, key, &init, 0))
+               return -1;
+
+       val = bpf_map_lookup_elem(&hmap, key);
+       if (!val)
+               return -2;
+
+       wq = &val->work;
+       if (bpf_wq_init(wq, &hmap, 0) != 0)
+               return -3;
+
+       if (bpf_wq_set_callback(wq, wq_cb_sleepable, 0))
+               return -4;
+
+       if (bpf_wq_start(wq, 0))
+               return -5;
+
+       return 0;
+}
+
+SEC("?struct_ops/hid_device_event")
+int BPF_PROG(hid_test_multiply_events_wq, struct hid_bpf_ctx *hid_ctx, enum hid_report_type type)
+{
+       __u8 *data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 9 /* size */);
+       int hid = hid_ctx->hid->id;
+       int ret;
+
+       if (!data)
+               return 0; /* EPERM check */
+
+       if (data[0] != 1)
+               return 0;
+
+       ret = test_inject_input_report_callback(&hid);
+       if (ret)
+               return ret;
+
+       data[1] += 5;
+
+       return 0;
+}
+
+SEC(".struct_ops.link")
+struct hid_bpf_ops test_multiply_events_wq = {
+       .hid_device_event = (void *)hid_test_multiply_events_wq,
+};
index e02e24e3eab334bd835be11ca7a2cbb6c38433ca..8014383846d211eecf27977f6579e49e65e1b699 100644 (file)
@@ -90,4 +90,13 @@ extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx,
                                __u8 *data,
                                size_t buf__sz) __ksym;
 
+/* bpf_wq implementation */
+extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;
+extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;
+extern int bpf_wq_set_callback_impl(struct bpf_wq *wq,
+               int (callback_fn)(void *map, int *key, struct bpf_wq *wq),
+               unsigned int flags__k, void *aux__ign) __ksym;
+#define bpf_wq_set_callback(timer, cb, flags) \
+       bpf_wq_set_callback_impl(timer, cb, flags, NULL)
+
 #endif /* __HID_BPF_HELPERS_H */