Skip to contentSkip to conversation

hid: bpf: avoid building struct ops without JIT

PATCH In review Arnd Bergmann 3 participants 4 messages Jul 19, 2024 – Jul 22, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hid: bpf: avoid building struct ops without JIT

From: Arnd Bergmann <[email protected]>

The module does not do anything when the JIT is disabled, but instead causes a warning:

In file included from include/linux/bpf_verifier.h:7,
from drivers/hid/bpf/hid_bpf_struct_ops.c:10:
drivers/hid/bpf/hid_bpf_struct_ops.c: In function 'hid_bpf_struct_ops_init':
include/linux/bpf.h:1853:50: error: statement with no effect [-Werror=unused-value]
1853 | #define register_bpf_struct_ops(st_ops, type) ({ (void *)(st_ops); 0; })

      |                                                  ^~~~~~~~~~~~~~~~
drivers/hid/bpf/hid_bpf_struct_ops.c:305:16: note: in expansion of macro 'register_bpf_struct_ops'
  305 |         return register_bpf_struct_ops(&bpf_hid_bpf_ops, hid_bpf_ops);
      |                ^~~~~~~~~~~~~~~~~~~~~~~

This could be avoided by making HID-BPF just depend on JIT, but that is probably not what we want here. Checking the other users of struct_ops, I see that those just leave out the struct_ops usage, so do the same here.

Fixes ebc0d8093e8c ("HID: bpf: implement HID-BPF through bpf_struct_ops")
---
 drivers/hid/bpf/Makefile           | 3 ++-
 drivers/hid/bpf/hid_bpf_dispatch.h | 6 ++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/hid/bpf/Makefile +2 -1
index d1f2b81788ca..7566be8eefba 100644
@@ -8,4 +8,5 @@ LIBBPF_INCLUDE = $(srctree)/tools/lib
 obj-$(CONFIG_HID_BPF) += hid_bpf.o
 CFLAGS_hid_bpf_dispatch.o += -I$(LIBBPF_INCLUDE)
 CFLAGS_hid_bpf_jmp_table.o += -I$(LIBBPF_INCLUDE)
-hid_bpf-objs += hid_bpf_dispatch.o hid_bpf_struct_ops.o
+hid_bpf-y += hid_bpf_dispatch.o
+hid_bpf-$(CONFIG_BPF_JIT) += hid_bpf_struct_ops.o
drivers/hid/bpf/hid_bpf_dispatch.h +6 -0
index 44c6ea22233f..577572f41454 100644
@@ -14,7 +14,13 @@ struct hid_bpf_ctx_kern {
 struct hid_device *hid_get_device(unsigned int hid_id);
 void hid_put_device(struct hid_device *hid);
 int hid_bpf_allocate_event_data(struct hid_device *hdev);
+#ifdef CONFIG_BPF_JIT
 void __hid_bpf_ops_destroy_device(struct hid_device *hdev);
+#else
+static inline void __hid_bpf_ops_destroy_device(struct hid_device *hdev)
+{
+}
+#endif
 int hid_bpf_reconnect(struct hid_device *hdev);
 
 struct bpf_prog;
Signature · 1 line
2.39.2
#1 lore raw
Quoted 10 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The module does not do anything when the JIT is disabled, but instead causes a warning:

In file included from include/linux/bpf_verifier.h:7,
from drivers/hid/bpf/hid_bpf_struct_ops.c:10:
drivers/hid/bpf/hid_bpf_struct_ops.c: In function 'hid_bpf_struct_ops_init':
include/linux/bpf.h:1853:50: error: statement with no effect [-Werror=unused-value]
1853 | #define register_bpf_struct_ops(st_ops, type) ({ (void *)(st_ops); 0; })

Quoted 8 lines
      |                                                  ^~~~~~~~~~~~~~~~
drivers/hid/bpf/hid_bpf_struct_ops.c:305:16: note: in expansion of macro 'register_bpf_struct_ops'
  305 |         return register_bpf_struct_ops(&bpf_hid_bpf_ops, hid_bpf_ops);
      |                ^~~~~~~~~~~~~~~~~~~~~~~

This could be avoided by making HID-BPF just depend on JIT, but that is probably not what we want here. Checking the other users of struct_ops, I see that those just leave out the struct_ops usage, so do the same here.

Actually, if we make the struct_ops part only depend on JIT HID-BPF is kind of moot. All we could do is use HID-BPF to communicate with the device, without getting any feedback, so nothing much more than what hidraw provides.

The only "interesting" bit we could do is inject a new event on a device as if it were originated from the device itself, but I really do not see the point without the struct_ops hooks.

So I think struct_ops is now the base for HID-BPF, and if it's not available, we should not have HID-BPF at all.

Quoted 13 lines
Fixes ebc0d8093e8c ("HID: bpf: implement HID-BPF through bpf_struct_ops")
---
 drivers/hid/bpf/Makefile           | 3 ++-
 drivers/hid/bpf/hid_bpf_dispatch.h | 6 ++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/hid/bpf/Makefile +0 -0
index d1f2b81788ca..7566be8eefba 100644

drivers/hid/bpf/Makefile LIBBPF_INCLUDE = $(srctree)/tools/lib · line 10

Quoted 3 lines
drivers/hid/bpf/Makefile +0 -0
@@ -8,4 +8,5 @@ LIBBPF_INCLUDE = $(srctree)/tools/lib
 obj-$(CONFIG_HID_BPF) += hid_bpf.o
 CFLAGS_hid_bpf_dispatch.o += -I$(LIBBPF_INCLUDE)
 CFLAGS_hid_bpf_jmp_table.o += -I$(LIBBPF_INCLUDE)

Unrelated to your patch, but looks like I forgot to remove that entry from the Makefile now that hid_bpf_jmp_table.c is not available :)

Cheers,
Benjamin

Quoted 24 lines
drivers/hid/bpf/Makefile +2 -1
-hid_bpf-objs += hid_bpf_dispatch.o hid_bpf_struct_ops.o
+hid_bpf-y += hid_bpf_dispatch.o
+hid_bpf-$(CONFIG_BPF_JIT) += hid_bpf_struct_ops.o
drivers/hid/bpf/hid_bpf_dispatch.h +6 -0
index 44c6ea22233f..577572f41454 100644
@@ -14,7 +14,13 @@ struct hid_bpf_ctx_kern {
 struct hid_device *hid_get_device(unsigned int hid_id);
 void hid_put_device(struct hid_device *hid);
 int hid_bpf_allocate_event_data(struct hid_device *hdev);
+#ifdef CONFIG_BPF_JIT
 void __hid_bpf_ops_destroy_device(struct hid_device *hdev);
+#else
+static inline void __hid_bpf_ops_destroy_device(struct hid_device *hdev)
+{
+}
+#endif
 int hid_bpf_reconnect(struct hid_device *hdev);
 
 struct bpf_prog;
Signature · 1 line
2.39.2
#2 lore raw
Quoted 11 lines · Benjamin Tissoires
Quoted 4 lines · Arnd Bergmann

This could be avoided by making HID-BPF just depend on JIT, but that is probably not what we want here. Checking the other users of struct_ops, I see that those just leave out the struct_ops usage, so do the same here.

Actually, if we make the struct_ops part only depend on JIT HID-BPF is kind of moot. All we could do is use HID-BPF to communicate with the device, without getting any feedback, so nothing much more than what hidraw provides.

Quoted 7 lines

The only "interesting" bit we could do is inject a new event on a device as if it were originated from the device itself, but I really do not see the point without the struct_ops hooks.

So I think struct_ops is now the base for HID-BPF, and if it's not available, we should not have HID-BPF at all.

Ok, got it. So my original patch was correct after all. I had tried this version and then discarded it.

Arnd

8<------
Subject: [PATCH] hid: bpf: add BPF_JIT dependency

The module does not do anything when the JIT is disabled, but instead causes a warning:

In file included from include/linux/bpf_verifier.h:7,
from drivers/hid/bpf/hid_bpf_struct_ops.c:10:
drivers/hid/bpf/hid_bpf_struct_ops.c: In function 'hid_bpf_struct_ops_init':
include/linux/bpf.h:1853:50: error: statement with no effect [-Werror=unused-value]
1853 | #define register_bpf_struct_ops(st_ops, type) ({ (void *)(st_ops); 0; })

      |                                                  ^~~~~~~~~~~~~~~~
drivers/hid/bpf/hid_bpf_struct_ops.c:305:16: note: in expansion of macro 'register_bpf_struct_ops'
  305 |         return register_bpf_struct_ops(&bpf_hid_bpf_ops, hid_bpf_ops);
      |                ^~~~~~~~~~~~~~~~~~~~~~~

Add a Kconfig dependency to only allow building the HID-BPF support when a JIT is enabled.

Fixes ebc0d8093e8c ("HID: bpf: implement HID-BPF through bpf_struct_ops")

----

drivers/hid/bpf/Kconfig +1 -1
index 83214bae6768..d65482e02a6c 100644
@@ -3,7 +3,7 @@ menu "HID-BPF support"
 
 config HID_BPF
        bool "HID-BPF support"
-       depends on BPF
+       depends on BPF_JIT
        depends on BPF_SYSCALL
        depends on DYNAMIC_FTRACE_WITH_DIRECT_CALLS
        help
#3 lore raw
Quoted 52 lines · Arnd Bergmann
Quoted 11 lines · Benjamin Tissoires
Quoted 4 lines · Arnd Bergmann

This could be avoided by making HID-BPF just depend on JIT, but that is probably not what we want here. Checking the other users of struct_ops, I see that those just leave out the struct_ops usage, so do the same here.

Actually, if we make the struct_ops part only depend on JIT HID-BPF is kind of moot. All we could do is use HID-BPF to communicate with the device, without getting any feedback, so nothing much more than what hidraw provides.

Quoted 7 lines

The only "interesting" bit we could do is inject a new event on a device as if it were originated from the device itself, but I really do not see the point without the struct_ops hooks.

So I think struct_ops is now the base for HID-BPF, and if it's not available, we should not have HID-BPF at all.

Ok, got it. So my original patch was correct after all. I had tried this version and then discarded it.

Arnd

8<------
Subject: [PATCH] hid: bpf: add BPF_JIT dependency

The module does not do anything when the JIT is disabled, but instead causes a warning:

In file included from include/linux/bpf_verifier.h:7,
from drivers/hid/bpf/hid_bpf_struct_ops.c:10:
drivers/hid/bpf/hid_bpf_struct_ops.c: In function 'hid_bpf_struct_ops_init':
include/linux/bpf.h:1853:50: error: statement with no effect [-Werror=unused-value]
1853 | #define register_bpf_struct_ops(st_ops, type) ({ (void *)(st_ops); 0; })

      |                                                  ^~~~~~~~~~~~~~~~
drivers/hid/bpf/hid_bpf_struct_ops.c:305:16: note: in expansion of macro 'register_bpf_struct_ops'
  305 |         return register_bpf_struct_ops(&bpf_hid_bpf_ops, hid_bpf_ops);
      |                ^~~~~~~~~~~~~~~~~~~~~~~

Add a Kconfig dependency to only allow building the HID-BPF support when a JIT is enabled.

Fixes ebc0d8093e8c ("HID: bpf: implement HID-BPF through bpf_struct_ops")

----

drivers/hid/bpf/Kconfig +0 -0
index 83214bae6768..d65482e02a6c 100644

drivers/hid/bpf/Kconfig menu "HID-BPF support" · line 9

Quoted 8 lines
drivers/hid/bpf/Kconfig +1 -1
@@ -3,7 +3,7 @@ menu "HID-BPF support"
 
 config HID_BPF
        bool "HID-BPF support"
-       depends on BPF
+       depends on BPF_JIT
        depends on BPF_SYSCALL
        depends on DYNAMIC_FTRACE_WITH_DIRECT_CALLS
        help

Thanks. I've applied this patch to for-6.11/upstream-fixes in the HID tree.

Cheers,
Benjamin