Hi Benjamin,
On 04/03/2022 19:28, Benjamin Tissoires wrote:
Now that BPF can be compatible with HID, add the capability into HID. drivers/hid/hid-bpf.c takes care of the glue between bpf and HID, and hid-core can then inject any incoming event from the device into a BPF program to filter/analyze it.
Signed-off-by: Benjamin Tissoires benjamin.tissoires@redhat.com
changes in v2:
- split the series by bpf/libbpf/hid/selftests and samples
- addressed review comments from v1
drivers/hid/Makefile | 1 + drivers/hid/hid-bpf.c | 157 +++++++++++++++++++++++++++++++++++++++++ drivers/hid/hid-core.c | 21 +++++- include/linux/hid.h | 11 +++ 4 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 drivers/hid/hid-bpf.c
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index 6d3e630e81af..08d2d7619937 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -4,6 +4,7 @@ # hid-y := hid-core.o hid-input.o hid-quirks.o hid-$(CONFIG_DEBUG_FS) += hid-debug.o +hid-$(CONFIG_BPF) += hid-bpf.o obj-$(CONFIG_HID) += hid.o obj-$(CONFIG_UHID) += uhid.o diff --git a/drivers/hid/hid-bpf.c b/drivers/hid/hid-bpf.c new file mode 100644 index 000000000000..8120e598de9f --- /dev/null +++ b/drivers/hid/hid-bpf.c @@ -0,0 +1,157 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/*
- BPF in HID support for Linux
- Copyright (c) 2022 Benjamin Tissoires
- */
+#include <linux/filter.h> +#include <linux/mutex.h> +#include <linux/slab.h>
+#include <uapi/linux/bpf_hid.h> +#include <linux/hid.h>
+static int __hid_bpf_match_sysfs(struct device *dev, const void *data) +{
- struct kernfs_node *kn = dev->kobj.sd;
- struct kernfs_node *uevent_kn;
- uevent_kn = kernfs_find_and_get_ns(kn, "uevent", NULL);
- return uevent_kn == data;
+}
+static struct hid_device *hid_bpf_fd_to_hdev(int fd) +{
- struct device *dev;
- struct hid_device *hdev;
- struct fd f = fdget(fd);
- struct inode *inode;
- struct kernfs_node *node;
- if (!f.file) {
hdev = ERR_PTR(-EBADF);
goto out;
- }
- inode = file_inode(f.file);
- node = inode->i_private;
- dev = bus_find_device(&hid_bus_type, NULL, node, __hid_bpf_match_sysfs);
- if (dev)
hdev = to_hid_device(dev);
- else
hdev = ERR_PTR(-EINVAL);
- out:
- fdput(f);
- return hdev;
+}
+static int hid_bpf_link_attach(struct hid_device *hdev, enum bpf_hid_attach_type type) +{
- int err = 0;
- switch (type) {
- case BPF_HID_ATTACH_DEVICE_EVENT:
if (!hdev->bpf.ctx) {
hdev->bpf.ctx = bpf_hid_allocate_ctx(hdev, HID_BPF_MAX_BUFFER_SIZE);
if (IS_ERR(hdev->bpf.ctx)) {
err = PTR_ERR(hdev->bpf.ctx);
hdev->bpf.ctx = NULL;
}
}
break;
- default:
/* do nothing */
These cause following error:
CC drivers/hid/hid-bpf.o drivers/hid/hid-bpf.c: In function ‘hid_bpf_link_attach’: drivers/hid/hid-bpf.c:88:2: error: label at end of compound statement 88 | default: | ^~~~~~~ drivers/hid/hid-bpf.c: In function ‘hid_bpf_link_attached’: drivers/hid/hid-bpf.c:101:2: error: label at end of compound statement 101 | default: | ^~~~~~~ drivers/hid/hid-bpf.c: In function ‘hid_bpf_array_detached’: drivers/hid/hid-bpf.c:116:2: error: label at end of compound statement 116 | default: | ^~~~~~~ make[2]: *** [scripts/Makefile.build:288: drivers/hid/hid-bpf.o] Error 1 make[1]: *** [scripts/Makefile.build:550: drivers/hid] Error 2 make[1]: *** Waiting for unfinished jobs.... make: *** [Makefile:1831: drivers] Error 2
To fix that, you need to add a break statement at end:
default:
/* do nothing */
break;
Same for couple of other occurrences in the file.
-Tero