[PATCH RFC HID 2/7] selftests/hid: fix bpf programs for the new attachment logic

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Now each program needs to ship its own attach prog. To make things
simpler we define __HID_BPF_PROG() and the various sub-macros per
hid-bpf type of program.

Signed-off-by: Benjamin Tissoires <bentiss@xxxxxxxxxx>
---
 tools/testing/selftests/hid/hid_bpf.c              |  6 +---
 tools/testing/selftests/hid/progs/hid.c            | 40 +++++-----------------
 .../testing/selftests/hid/progs/hid_bpf_helpers.h  | 29 +++++++++++++++-
 3 files changed, 37 insertions(+), 38 deletions(-)

diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c
index f825623e3edc..7fed9f599b62 100644
--- a/tools/testing/selftests/hid/hid_bpf.c
+++ b/tools/testing/selftests/hid/hid_bpf.c
@@ -90,7 +90,6 @@ static unsigned char rdesc[] = {
 static __u8 feature_data[] = { 1, 2 };
 
 struct attach_prog_args {
-	int prog_fd;
 	unsigned int hid;
 	int retval;
 	int insert_head;
@@ -556,9 +555,6 @@ static void load_programs(const struct test_program programs[],
 	err = hid__load(self->skel);
 	ASSERT_OK(err) TH_LOG("hid_skel_load failed: %d", err);
 
-	attach_fd = bpf_program__fd(self->skel->progs.attach_prog);
-	ASSERT_GE(attach_fd, 0) TH_LOG("locate attach_prog: %d", attach_fd);
-
 	for (int i = 0; i < progs_count; i++) {
 		struct bpf_program *prog;
 
@@ -566,7 +562,7 @@ static void load_programs(const struct test_program programs[],
 							programs[i].name);
 		ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name);
 
-		args.prog_fd = bpf_program__fd(prog);
+		attach_fd = bpf_program__fd(prog);
 		args.hid = self->hid_id;
 		args.insert_head = programs[i].insert_head;
 		err = bpf_prog_test_run_opts(attach_fd, &tattr);
diff --git a/tools/testing/selftests/hid/progs/hid.c b/tools/testing/selftests/hid/progs/hid.c
index f67d35def142..b721d1256836 100644
--- a/tools/testing/selftests/hid/progs/hid.c
+++ b/tools/testing/selftests/hid/progs/hid.c
@@ -4,18 +4,10 @@
 
 char _license[] SEC("license") = "GPL";
 
-struct attach_prog_args {
-	int prog_fd;
-	unsigned int hid;
-	int retval;
-	int insert_head;
-};
-
 __u64 callback_check = 52;
 __u64 callback2_check = 52;
 
-SEC("?fmod_ret/hid_bpf_device_event")
-int BPF_PROG(hid_first_event, struct hid_bpf_ctx *hid_ctx)
+HID_BPF_DEVICE_EVENT(hid_first_event, struct hid_bpf_ctx *hid_ctx)
 {
 	__u8 *rw_data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 3 /* size */);
 
@@ -29,8 +21,7 @@ int BPF_PROG(hid_first_event, struct hid_bpf_ctx *hid_ctx)
 	return hid_ctx->size;
 }
 
-SEC("?fmod_ret/hid_bpf_device_event")
-int BPF_PROG(hid_second_event, struct hid_bpf_ctx *hid_ctx)
+HID_BPF_DEVICE_EVENT(hid_second_event, struct hid_bpf_ctx *hid_ctx)
 {
 	__u8 *rw_data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 4 /* size */);
 
@@ -42,8 +33,7 @@ int BPF_PROG(hid_second_event, struct hid_bpf_ctx *hid_ctx)
 	return hid_ctx->size;
 }
 
-SEC("?fmod_ret/hid_bpf_device_event")
-int BPF_PROG(hid_change_report_id, struct hid_bpf_ctx *hid_ctx)
+HID_BPF_DEVICE_EVENT(hid_change_report_id, struct hid_bpf_ctx *hid_ctx)
 {
 	__u8 *rw_data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 3 /* size */);
 
@@ -55,16 +45,6 @@ int BPF_PROG(hid_change_report_id, struct hid_bpf_ctx *hid_ctx)
 	return 9;
 }
 
-SEC("syscall")
-int attach_prog(struct attach_prog_args *ctx)
-{
-	ctx->retval = hid_bpf_attach_prog(ctx->hid,
-					  ctx->prog_fd,
-					  ctx->insert_head ? HID_BPF_FLAG_INSERT_HEAD :
-							     HID_BPF_FLAG_NONE);
-	return 0;
-}
-
 struct hid_hw_request_syscall_args {
 	/* data needs to come at offset 0 so we can use it in calls */
 	__u8 data[10];
@@ -181,13 +161,12 @@ static const __u8 rdesc[] = {
 	0xc0,			/* END_COLLECTION */
 };
 
-SEC("?fmod_ret/hid_bpf_rdesc_fixup")
-int BPF_PROG(hid_rdesc_fixup, struct hid_bpf_ctx *hid_ctx)
+HID_BPF_RDESC_FIXUP(hid_rdesc_fixup, struct hid_bpf_ctx *hid_ctx)
 {
 	__u8 *data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 4096 /* size */);
 
 	if (!data)
-		return 0; /* EPERM check */
+		return -22; /* EPERM check */
 
 	callback2_check = data[4];
 
@@ -200,8 +179,7 @@ int BPF_PROG(hid_rdesc_fixup, struct hid_bpf_ctx *hid_ctx)
 	return sizeof(rdesc) + 73;
 }
 
-SEC("?fmod_ret/hid_bpf_device_event")
-int BPF_PROG(hid_test_insert1, struct hid_bpf_ctx *hid_ctx)
+HID_BPF_DEVICE_EVENT(hid_test_insert1, struct hid_bpf_ctx *hid_ctx)
 {
 	__u8 *data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 4 /* size */);
 
@@ -217,8 +195,7 @@ int BPF_PROG(hid_test_insert1, struct hid_bpf_ctx *hid_ctx)
 	return 0;
 }
 
-SEC("?fmod_ret/hid_bpf_device_event")
-int BPF_PROG(hid_test_insert2, struct hid_bpf_ctx *hid_ctx)
+HID_BPF_DEVICE_EVENT(hid_test_insert2, struct hid_bpf_ctx *hid_ctx)
 {
 	__u8 *data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 4 /* size */);
 
@@ -234,8 +211,7 @@ int BPF_PROG(hid_test_insert2, struct hid_bpf_ctx *hid_ctx)
 	return 0;
 }
 
-SEC("?fmod_ret/hid_bpf_device_event")
-int BPF_PROG(hid_test_insert3, struct hid_bpf_ctx *hid_ctx)
+HID_BPF_DEVICE_EVENT(hid_test_insert3, struct hid_bpf_ctx *hid_ctx)
 {
 	__u8 *data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 4 /* size */);
 
diff --git a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h
index 9cd56821d0f1..9826880e88d1 100644
--- a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h
+++ b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h
@@ -82,11 +82,20 @@ enum hid_bpf_attach_flags {
 	HID_BPF_FLAG_MAX,
 };
 
+struct attach_prog_args {
+	unsigned int hid;
+	int retval;
+	int insert_head;
+};
+
 /* following are kfuncs exported by HID for HID-BPF */
 extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,
 			      unsigned int offset,
 			      const size_t __sz) __ksym;
-extern int hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, u32 flags) __ksym;
+extern int hid_bpf_attach_prog_impl(unsigned int hid_id,
+				    enum hid_bpf_prog_type type,
+				    int (prog_fn)(struct hid_bpf_ctx *hid_ctx),
+				    u32 flags, void *aux) __ksym;
 extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __ksym;
 extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __ksym;
 extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx,
@@ -101,4 +110,22 @@ extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx,
 				__u8 *data,
 				size_t buf__sz) __ksym;
 
+#define __HID_BPF_PROG(type, name, arg)                                                            \
+	static int __##name(arg);                                                                  \
+	SEC("syscall")                                                                             \
+	int name(struct attach_prog_args *ctx)                                                     \
+	{                                                                                          \
+		ctx->retval = hid_bpf_attach_prog_impl(ctx->hid,                                   \
+						  type,                                            \
+						  __##name,                                        \
+						  ctx->insert_head ? HID_BPF_FLAG_INSERT_HEAD :    \
+								     HID_BPF_FLAG_NONE,            \
+						  NULL);                                           \
+		return 0;                                                                          \
+	}                                                                                          \
+	static int __##name(arg)
+
+#define HID_BPF_DEVICE_EVENT(name, arg) __HID_BPF_PROG(HID_BPF_PROG_TYPE_DEVICE_EVENT, name, arg)
+#define HID_BPF_RDESC_FIXUP(name, arg) __HID_BPF_PROG(HID_BPF_PROG_TYPE_RDESC_FIXUP, name, arg)
+
 #endif /* __HID_BPF_HELPERS_H */

-- 
2.44.0





[Index of Archives]     [Linux Media Devel]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Linux Wireless Networking]     [Linux Omap]

  Powered by Linux