linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Tissoires <bentiss@kernel.org>
To: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Cc: linux-kernel@vger.kernel.org, linux-input@vger.kernel.org,
	 bpf@vger.kernel.org, Benjamin Tissoires <bentiss@kernel.org>
Subject: [PATCH RFC HID 2/7] selftests/hid: fix bpf programs for the new attachment logic
Date: Wed, 08 May 2024 12:26:37 +0200	[thread overview]
Message-ID: <20240508-hid_bpf_async_fun-v1-2-558375a25657@kernel.org> (raw)
In-Reply-To: <20240508-hid_bpf_async_fun-v1-0-558375a25657@kernel.org>

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@kernel.org>
---
 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


  parent reply	other threads:[~2024-05-08 10:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-08 10:26 [PATCH RFC HID 0/7] Use the new __s_async for HID-BPF Benjamin Tissoires
2024-05-08 10:26 ` [PATCH RFC HID 1/7] HID: bpf: change the prog run logic Benjamin Tissoires
2024-05-08 10:26 ` Benjamin Tissoires [this message]
2024-05-08 10:26 ` [PATCH RFC HID 3/7] HID: bpf: allow for sleepable bpf hooks Benjamin Tissoires
2024-05-08 10:26 ` [PATCH RFC HID 4/7] HID: add source argument to HID low level functions Benjamin Tissoires
2024-05-08 10:26 ` [PATCH RFC HID 5/7] WIP: add HID-BPF hooks for hid_hw_raw_requests Benjamin Tissoires
2024-05-08 10:26 ` [PATCH RFC HID 6/7] WIP: selftests/hid: add tests for hid_hw_raw_request HID-BPF hooks Benjamin Tissoires
2024-05-08 10:26 ` [PATCH RFC HID 7/7] HID: bpf: prevent infinite recursions with hid_hw_raw_requests hooks Benjamin Tissoires

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240508-hid_bpf_async_fun-v1-2-558375a25657@kernel.org \
    --to=bentiss@kernel.org \
    --cc=benjamin.tissoires@redhat.com \
    --cc=bpf@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).