HID: initial BPF implementation
authorBenjamin Tissoires <benjamin.tissoires@redhat.com>
Thu, 3 Nov 2022 15:57:44 +0000 (16:57 +0100)
committerJiri Kosina <jkosina@suse.cz>
Tue, 15 Nov 2022 15:28:28 +0000 (16:28 +0100)
commitf5c27da4e3c8a2e42fb4f41a0c685debcb9af294
treedca477e34c80d61362ddd2ebbfbe2a4f9dc6bc22
parent25621bcc89762fe1329ae9c9dab75bc70ef1aee0
HID: initial BPF implementation

Declare an entry point that can use fmod_ret BPF programs, and
also an API to access and change the incoming data.

A simpler implementation would consist in just calling
hid_bpf_device_event() for any incoming event and let users deal
with the fact that they will be called for any event of any device.

The goal of HID-BPF is to partially replace drivers, so this situation
can be problematic because we might have programs which will step on
each other toes.

For that, we add a new API hid_bpf_attach_prog() that can be called
from a syscall and we manually deal with a jump table in hid-bpf.

Whenever we add a program to the jump table (in other words, when we
attach a program to a HID device), we keep the number of time we added
this program in the jump table so we can release it whenever there are
no other users.

HID devices have an RCU protected list of available programs in the
jump table, and those programs are called one after the other thanks
to bpf_tail_call().

To achieve the detection of users losing their fds on the programs we
attached, we add 2 tracing facilities on bpf_prog_release() (for when
a fd is closed) and bpf_free_inode() (for when a pinned program gets
unpinned).

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
14 files changed:
drivers/hid/Kconfig
drivers/hid/Makefile
drivers/hid/bpf/Kconfig [new file with mode: 0644]
drivers/hid/bpf/Makefile [new file with mode: 0644]
drivers/hid/bpf/entrypoints/Makefile [new file with mode: 0644]
drivers/hid/bpf/entrypoints/README [new file with mode: 0644]
drivers/hid/bpf/entrypoints/entrypoints.bpf.c [new file with mode: 0644]
drivers/hid/bpf/entrypoints/entrypoints.lskel.h [new file with mode: 0644]
drivers/hid/bpf/hid_bpf_dispatch.c [new file with mode: 0644]
drivers/hid/bpf/hid_bpf_dispatch.h [new file with mode: 0644]
drivers/hid/bpf/hid_bpf_jmp_table.c [new file with mode: 0644]
drivers/hid/hid-core.c
include/linux/hid.h
include/linux/hid_bpf.h [new file with mode: 0644]