2022-09-02 14:48:11

by Benjamin Tissoires

[permalink] [raw]
Subject: [PATCH bpf-next v10 00/23] Introduce eBPF support for HID devices

Hi,

here comes the v10 of the HID-BPF series.

Again, for a full explanation of HID-BPF, please refer to the last patch
in this series (23/23).

Hopefully we are getting closer to merging the bpf-core changes that
are pre-requesite of the HID work.

This revision of the series focused on those bpf-core changes with
a hopefully proper way of fixing access to ctx pointers, and a few more
selftests to cover those changes.

Once those bpf changes are in, the HID changes are pretty much self
consistent, which is a good thing, but I still wonder how we are going
to merge the selftests. I'd rather have the selftests in the bpf tree to
prevent any regression on bpf-core changes, but that might require some
coordination between the HID and bpf trees.

Anyway, let's hope we are getting closer to the end of those revisions :)

Cheers,
Benjamin


Benjamin Tissoires (23):
selftests/bpf: regroup and declare similar kfuncs selftests in an
array
bpf: split btf_check_subprog_arg_match in two
bpf/verifier: allow all functions to read user provided context
selftests/bpf: add test for accessing ctx from syscall program type
bpf/btf: bump BTF_KFUNC_SET_MAX_CNT
bpf/verifier: allow kfunc to return an allocated mem
selftests/bpf: Add tests for kfunc returning a memory pointer
HID: core: store the unique system identifier in hid_device
HID: export hid_report_type to uapi
HID: convert defines of HID class requests into a proper enum
HID: Kconfig: split HID support and hid-core compilation
HID: initial BPF implementation
selftests/bpf: add tests for the HID-bpf initial implementation
HID: bpf: allocate data memory for device_event BPF programs
selftests/bpf/hid: add test to change the report size
HID: bpf: introduce hid_hw_request()
selftests/bpf: add tests for bpf_hid_hw_request
HID: bpf: allow to change the report descriptor
selftests/bpf: add report descriptor fixup tests
selftests/bpf: Add a test for BPF_F_INSERT_HEAD
samples/bpf: HID: add new hid_mouse example
samples/bpf: HID: add Surface Dial example
Documentation: add HID-BPF docs

Documentation/hid/hid-bpf.rst | 513 +++++++++
Documentation/hid/index.rst | 1 +
drivers/Makefile | 2 +-
drivers/hid/Kconfig | 20 +-
drivers/hid/Makefile | 2 +
drivers/hid/bpf/Kconfig | 17 +
drivers/hid/bpf/Makefile | 11 +
drivers/hid/bpf/entrypoints/Makefile | 93 ++
drivers/hid/bpf/entrypoints/README | 4 +
drivers/hid/bpf/entrypoints/entrypoints.bpf.c | 66 ++
.../hid/bpf/entrypoints/entrypoints.lskel.h | 682 ++++++++++++
drivers/hid/bpf/hid_bpf_dispatch.c | 526 ++++++++++
drivers/hid/bpf/hid_bpf_dispatch.h | 28 +
drivers/hid/bpf/hid_bpf_jmp_table.c | 577 ++++++++++
drivers/hid/hid-core.c | 49 +-
include/linux/bpf.h | 11 +-
include/linux/bpf_verifier.h | 2 +
include/linux/btf.h | 10 +
include/linux/hid.h | 38 +-
include/linux/hid_bpf.h | 148 +++
include/uapi/linux/hid.h | 26 +-
include/uapi/linux/hid_bpf.h | 25 +
kernel/bpf/btf.c | 149 ++-
kernel/bpf/verifier.c | 66 +-
net/bpf/test_run.c | 37 +
samples/bpf/.gitignore | 2 +
samples/bpf/Makefile | 27 +
samples/bpf/hid_mouse.bpf.c | 134 +++
samples/bpf/hid_mouse.c | 161 +++
samples/bpf/hid_surface_dial.bpf.c | 161 +++
samples/bpf/hid_surface_dial.c | 232 ++++
tools/include/uapi/linux/hid.h | 62 ++
tools/include/uapi/linux/hid_bpf.h | 25 +
tools/testing/selftests/bpf/Makefile | 2 +-
tools/testing/selftests/bpf/config | 3 +
tools/testing/selftests/bpf/prog_tests/hid.c | 990 ++++++++++++++++++
.../selftests/bpf/prog_tests/kfunc_call.c | 182 +++-
tools/testing/selftests/bpf/progs/hid.c | 206 ++++
.../selftests/bpf/progs/kfunc_call_fail.c | 160 +++
.../selftests/bpf/progs/kfunc_call_test.c | 71 ++
40 files changed, 5416 insertions(+), 105 deletions(-)
create mode 100644 Documentation/hid/hid-bpf.rst
create mode 100644 drivers/hid/bpf/Kconfig
create mode 100644 drivers/hid/bpf/Makefile
create mode 100644 drivers/hid/bpf/entrypoints/Makefile
create mode 100644 drivers/hid/bpf/entrypoints/README
create mode 100644 drivers/hid/bpf/entrypoints/entrypoints.bpf.c
create mode 100644 drivers/hid/bpf/entrypoints/entrypoints.lskel.h
create mode 100644 drivers/hid/bpf/hid_bpf_dispatch.c
create mode 100644 drivers/hid/bpf/hid_bpf_dispatch.h
create mode 100644 drivers/hid/bpf/hid_bpf_jmp_table.c
create mode 100644 include/linux/hid_bpf.h
create mode 100644 include/uapi/linux/hid_bpf.h
create mode 100644 samples/bpf/hid_mouse.bpf.c
create mode 100644 samples/bpf/hid_mouse.c
create mode 100644 samples/bpf/hid_surface_dial.bpf.c
create mode 100644 samples/bpf/hid_surface_dial.c
create mode 100644 tools/include/uapi/linux/hid.h
create mode 100644 tools/include/uapi/linux/hid_bpf.h
create mode 100644 tools/testing/selftests/bpf/prog_tests/hid.c
create mode 100644 tools/testing/selftests/bpf/progs/hid.c
create mode 100644 tools/testing/selftests/bpf/progs/kfunc_call_fail.c

--
2.36.1


2022-09-02 14:48:51

by Benjamin Tissoires

[permalink] [raw]
Subject: [PATCH bpf-next v10 02/23] bpf: split btf_check_subprog_arg_match in two

btf_check_subprog_arg_match() was used twice in verifier.c:
- when checking for the type mismatches between a (sub)prog declaration
and BTF
- when checking the call of a subprog to see if the provided arguments
are correct and valid

This is problematic when we check if the first argument of a program
(pointer to ctx) is correctly accessed:
To be able to ensure we access a valid memory in the ctx, the verifier
assumes the pointer to context is not null.
This has the side effect of marking the program accessing the entire
context, even if the context is never dereferenced.

For example, by checking the context access with the current code, the
following eBPF program would fail with -EINVAL if the ctx is set to null
from the userspace:

```
SEC("syscall")
int prog(struct my_ctx *args) {
return 0;
}
```

In that particular case, we do not want to actually check that the memory
is correct while checking for the BTF validity, but we just want to
ensure that the (sub)prog definition matches the BTF we have.

So split btf_check_subprog_arg_match() in two so we can actually check
for the memory used when in a call, and ignore that part when not.

Note that a further patch is in preparation to disentangled
btf_check_func_arg_match() from these two purposes, and so right now we
just add a new hack around that by adding a boolean to this function.

Signed-off-by: Benjamin Tissoires <[email protected]>

---

new in v10
---
include/linux/bpf.h | 2 ++
kernel/bpf/btf.c | 54 +++++++++++++++++++++++++++++++++++++++----
kernel/bpf/verifier.c | 2 +-
3 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 9c1674973e03..c9c72a089579 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1943,6 +1943,8 @@ int btf_distill_func_proto(struct bpf_verifier_log *log,
struct bpf_reg_state;
int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
struct bpf_reg_state *regs);
+int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog,
+ struct bpf_reg_state *regs);
int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
const struct btf *btf, u32 func_id,
struct bpf_reg_state *regs,
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 903719b89238..eca9ea78ee5f 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -6170,7 +6170,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
const struct btf *btf, u32 func_id,
struct bpf_reg_state *regs,
bool ptr_to_mem_ok,
- u32 kfunc_flags)
+ u32 kfunc_flags,
+ bool processing_call)
{
enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
bool rel = false, kptr_get = false, trusted_arg = false;
@@ -6356,7 +6357,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
reg_ref_tname);
return -EINVAL;
}
- } else if (ptr_to_mem_ok) {
+ } else if (ptr_to_mem_ok && processing_call) {
const struct btf_type *resolve_ret;
u32 type_size;

@@ -6431,7 +6432,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
return rel ? ref_regno : 0;
}

-/* Compare BTF of a function with given bpf_reg_state.
+/* Compare BTF of a function declaration with given bpf_reg_state.
* Returns:
* EFAULT - there is a verifier bug. Abort verification.
* EINVAL - there is a type mismatch or BTF is not available.
@@ -6458,7 +6459,50 @@ int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
return -EINVAL;

is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL;
- err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, 0);
+ err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, 0, false);
+
+ /* Compiler optimizations can remove arguments from static functions
+ * or mismatched type can be passed into a global function.
+ * In such cases mark the function as unreliable from BTF point of view.
+ */
+ if (err)
+ prog->aux->func_info_aux[subprog].unreliable = true;
+ return err;
+}
+
+/* Compare BTF of a function call with given bpf_reg_state.
+ * Returns:
+ * EFAULT - there is a verifier bug. Abort verification.
+ * EINVAL - there is a type mismatch or BTF is not available.
+ * 0 - BTF matches with what bpf_reg_state expects.
+ * Only PTR_TO_CTX and SCALAR_VALUE states are recognized.
+ *
+ * NOTE: the code is duplicated from btf_check_subprog_arg_match()
+ * because btf_check_func_arg_match() is still doing both. Once that
+ * function is split in 2, we can call from here btf_check_subprog_arg_match()
+ * first, and then treat the calling part in a new code path.
+ */
+int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog,
+ struct bpf_reg_state *regs)
+{
+ struct bpf_prog *prog = env->prog;
+ struct btf *btf = prog->aux->btf;
+ bool is_global;
+ u32 btf_id;
+ int err;
+
+ if (!prog->aux->func_info)
+ return -EINVAL;
+
+ btf_id = prog->aux->func_info[subprog].type_id;
+ if (!btf_id)
+ return -EFAULT;
+
+ if (prog->aux->func_info_aux[subprog].unreliable)
+ return -EINVAL;
+
+ is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL;
+ err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, 0, true);

/* Compiler optimizations can remove arguments from static functions
* or mismatched type can be passed into a global function.
@@ -6474,7 +6518,7 @@ int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
struct bpf_reg_state *regs,
u32 kfunc_flags)
{
- return btf_check_func_arg_match(env, btf, func_id, regs, true, kfunc_flags);
+ return btf_check_func_arg_match(env, btf, func_id, regs, true, kfunc_flags, true);
}

/* Convert BTF of a function into bpf_reg_state if possible
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 0194a36d0b36..d27fae3ce949 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -6626,7 +6626,7 @@ static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn
func_info_aux = env->prog->aux->func_info_aux;
if (func_info_aux)
is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL;
- err = btf_check_subprog_arg_match(env, subprog, caller->regs);
+ err = btf_check_subprog_call(env, subprog, caller->regs);
if (err == -EFAULT)
return err;
if (is_global) {
--
2.36.1

2022-09-02 14:52:03

by Benjamin Tissoires

[permalink] [raw]
Subject: [PATCH bpf-next v10 20/23] selftests/bpf: Add a test for BPF_F_INSERT_HEAD

Insert 3 programs to check that we are doing the correct thing:
'2', '1', '3' are inserted, but '1' is supposed to be executed first.

Signed-off-by: Benjamin Tissoires <[email protected]>

---

no changes in v10

no changes in v9

no changes in v8

no changes in v7

changes in v6:
- fixed copy/paste in ASSERT_OK and test execution order

changes in v5:
- use the new API

not in v4

changes in v3:
- use the new hid_get_data API

new in v2
---
tools/testing/selftests/bpf/prog_tests/hid.c | 107 +++++++++++++++++++
tools/testing/selftests/bpf/progs/hid.c | 54 +++++++++-
2 files changed, 160 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/hid.c b/tools/testing/selftests/bpf/prog_tests/hid.c
index 9dc5f0038472..a86e16554e68 100644
--- a/tools/testing/selftests/bpf/prog_tests/hid.c
+++ b/tools/testing/selftests/bpf/prog_tests/hid.c
@@ -9,6 +9,7 @@
#include <dirent.h>
#include <poll.h>
#include <stdbool.h>
+#include <linux/hid_bpf.h>
#include <linux/hidraw.h>
#include <linux/uhid.h>

@@ -83,6 +84,7 @@ static u8 feature_data[] = { 1, 2 };
struct attach_prog_args {
int prog_fd;
unsigned int hid;
+ unsigned int flags;
int retval;
};

@@ -770,6 +772,109 @@ static int test_hid_user_raw_request_call(int uhid_fd, int dev_id)
return ret;
}

+/*
+ * Attach hid_insert{0,1,2} to the given uhid device,
+ * retrieve and open the matching hidraw node,
+ * inject one event in the uhid device,
+ * check that the programs have been inserted in the correct order.
+ */
+static int test_hid_attach_flags(int uhid_fd, int dev_id)
+{
+ struct hid *hid_skel = NULL;
+ u8 buf[64] = {0};
+ int hidraw_fd = -1;
+ int hid_id, attach_fd, err = -EINVAL;
+ struct attach_prog_args args = {
+ .retval = -1,
+ };
+ DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattr,
+ .ctx_in = &args,
+ .ctx_size_in = sizeof(args),
+ );
+
+ /* locate the uevent file of the created device */
+ hid_id = get_hid_id(dev_id);
+ if (!ASSERT_GE(hid_id, 0, "locate uhid device id"))
+ goto cleanup;
+
+ args.hid = hid_id;
+
+ hid_skel = hid__open();
+ if (!ASSERT_OK_PTR(hid_skel, "hid_skel_open"))
+ goto cleanup;
+
+ bpf_program__set_autoload(hid_skel->progs.hid_test_insert1, true);
+ bpf_program__set_autoload(hid_skel->progs.hid_test_insert2, true);
+ bpf_program__set_autoload(hid_skel->progs.hid_test_insert3, true);
+
+ err = hid__load(hid_skel);
+ if (!ASSERT_OK(err, "hid_skel_load"))
+ goto cleanup;
+
+ attach_fd = bpf_program__fd(hid_skel->progs.attach_prog);
+ if (!ASSERT_GE(attach_fd, 0, "locate attach_prog")) {
+ err = attach_fd;
+ goto cleanup;
+ }
+
+ /* attach hid_test_insert2 program */
+ args.prog_fd = bpf_program__fd(hid_skel->progs.hid_test_insert2);
+ args.flags = HID_BPF_FLAG_NONE;
+ args.retval = 1;
+ err = bpf_prog_test_run_opts(attach_fd, &tattr);
+ if (!ASSERT_EQ(args.retval, 0, "attach_hid_test_insert2"))
+ goto cleanup;
+
+ /* then attach hid_test_insert1 program before the previous*/
+ args.prog_fd = bpf_program__fd(hid_skel->progs.hid_test_insert1);
+ args.flags = HID_BPF_FLAG_INSERT_HEAD;
+ args.retval = 1;
+ err = bpf_prog_test_run_opts(attach_fd, &tattr);
+ if (!ASSERT_EQ(args.retval, 0, "attach_hid_test_insert1"))
+ goto cleanup;
+
+ /* finally attach hid_test_insert3 at the end */
+ args.prog_fd = bpf_program__fd(hid_skel->progs.hid_test_insert3);
+ args.flags = HID_BPF_FLAG_NONE;
+ args.retval = 1;
+ err = bpf_prog_test_run_opts(attach_fd, &tattr);
+ if (!ASSERT_EQ(args.retval, 0, "attach_hid_test_insert3"))
+ goto cleanup;
+
+ hidraw_fd = open_hidraw(dev_id);
+ if (!ASSERT_GE(hidraw_fd, 0, "open_hidraw"))
+ goto cleanup;
+
+ /* inject one event */
+ buf[0] = 1;
+ send_event(uhid_fd, buf, 6);
+
+ /* read the data from hidraw */
+ memset(buf, 0, sizeof(buf));
+ err = read(hidraw_fd, buf, sizeof(buf));
+ if (!ASSERT_EQ(err, 6, "read_hidraw"))
+ goto cleanup;
+
+ if (!ASSERT_EQ(buf[1], 1, "hid_test_insert1"))
+ goto cleanup;
+
+ if (!ASSERT_EQ(buf[2], 2, "hid_test_insert2"))
+ goto cleanup;
+
+ if (!ASSERT_EQ(buf[3], 3, "hid_test_insert3"))
+ goto cleanup;
+
+ err = 0;
+
+ cleanup:
+ if (hidraw_fd >= 0)
+ close(hidraw_fd);
+
+ hid__destroy(hid_skel);
+
+ return err;
+}
+
/*
* Attach hid_rdesc_fixup to the given uhid device,
* retrieve and open the matching hidraw node,
@@ -866,6 +971,8 @@ void serial_test_hid_bpf(void)
ASSERT_OK(err, "hid_change_report");
err = test_hid_user_raw_request_call(uhid_fd, dev_id);
ASSERT_OK(err, "hid_user_raw_request");
+ err = test_hid_attach_flags(uhid_fd, dev_id);
+ ASSERT_OK(err, "hid_attach_flags");

/*
* this test should be run last because we disconnect/reconnect
diff --git a/tools/testing/selftests/bpf/progs/hid.c b/tools/testing/selftests/bpf/progs/hid.c
index 815ff94321c9..eb869a80c254 100644
--- a/tools/testing/selftests/bpf/progs/hid.c
+++ b/tools/testing/selftests/bpf/progs/hid.c
@@ -21,6 +21,7 @@ extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx,
struct attach_prog_args {
int prog_fd;
unsigned int hid;
+ unsigned int flags;
int retval;
};

@@ -60,7 +61,7 @@ int attach_prog(struct attach_prog_args *ctx)
{
ctx->retval = hid_bpf_attach_prog(ctx->hid,
ctx->prog_fd,
- 0);
+ ctx->flags);
return 0;
}

@@ -152,3 +153,54 @@ 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)
+{
+ __u8 *data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 4 /* size */);
+
+ if (!data)
+ return 0; /* EPERM check */
+
+ /* we need to be run first */
+ if (data[2] || data[3])
+ return -1;
+
+ data[1] = 1;
+
+ return 0;
+}
+
+SEC("?fmod_ret/hid_bpf_device_event")
+int BPF_PROG(hid_test_insert2, struct hid_bpf_ctx *hid_ctx)
+{
+ __u8 *data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 4 /* size */);
+
+ if (!data)
+ return 0; /* EPERM check */
+
+ /* after insert0 and before insert2 */
+ if (!data[1] || data[3])
+ return -1;
+
+ data[2] = 2;
+
+ return 0;
+}
+
+SEC("?fmod_ret/hid_bpf_device_event")
+int BPF_PROG(hid_test_insert3, struct hid_bpf_ctx *hid_ctx)
+{
+ __u8 *data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 4 /* size */);
+
+ if (!data)
+ return 0; /* EPERM check */
+
+ /* at the end */
+ if (!data[1] || !data[2])
+ return -1;
+
+ data[3] = 3;
+
+ return 0;
+}
--
2.36.1

2022-09-02 14:52:10

by Benjamin Tissoires

[permalink] [raw]
Subject: [PATCH bpf-next v10 09/23] HID: export hid_report_type to uapi

When we are dealing with eBPF, we need to have access to the report type.
Currently our implementation differs from the USB standard, making it
impossible for users to know the exact value besides hardcoding it
themselves.

And instead of a blank define, convert it as an enum.

Note that we need to also do change in the ll_driver API, but given
that this will have a wider impact outside of this tree, we leave this
as a TODO for the future.

Reviewed-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Benjamin Tissoires <[email protected]>

---

no changes in v10

no changes in v9

no changes in v8

no changes in v7

changes in v6:
- add missing change for hid_hw_raw_request()

new in v5
---
drivers/hid/hid-core.c | 13 +++++++------
include/linux/hid.h | 24 ++++++++----------------
include/uapi/linux/hid.h | 12 ++++++++++++
3 files changed, 27 insertions(+), 22 deletions(-)

diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index a00dd43db8bf..ab98754522d9 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -55,7 +55,7 @@ MODULE_PARM_DESC(ignore_special_drivers, "Ignore any special drivers and handle
*/

struct hid_report *hid_register_report(struct hid_device *device,
- unsigned int type, unsigned int id,
+ enum hid_report_type type, unsigned int id,
unsigned int application)
{
struct hid_report_enum *report_enum = device->report_enum + type;
@@ -967,7 +967,7 @@ static const char * const hid_report_names[] = {
* parsing.
*/
struct hid_report *hid_validate_values(struct hid_device *hid,
- unsigned int type, unsigned int id,
+ enum hid_report_type type, unsigned int id,
unsigned int field_index,
unsigned int report_counts)
{
@@ -1954,8 +1954,8 @@ int __hid_request(struct hid_device *hid, struct hid_report *report,
}
EXPORT_SYMBOL_GPL(__hid_request);

-int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
- int interrupt)
+int hid_report_raw_event(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
+ int interrupt)
{
struct hid_report_enum *report_enum = hid->report_enum + type;
struct hid_report *report;
@@ -2019,7 +2019,8 @@ EXPORT_SYMBOL_GPL(hid_report_raw_event);
*
* This is data entry for lower layers.
*/
-int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int interrupt)
+int hid_input_report(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
+ int interrupt)
{
struct hid_report_enum *report_enum;
struct hid_driver *hdrv;
@@ -2377,7 +2378,7 @@ EXPORT_SYMBOL_GPL(hid_hw_request);
*/
int hid_hw_raw_request(struct hid_device *hdev,
unsigned char reportnum, __u8 *buf,
- size_t len, unsigned char rtype, int reqtype)
+ size_t len, enum hid_report_type rtype, int reqtype)
{
if (len < 1 || len > HID_MAX_BUFFER_SIZE || !buf)
return -EINVAL;
diff --git a/include/linux/hid.h b/include/linux/hid.h
index a43dd17bc78f..b1a33dbbc78e 100644
--- a/include/linux/hid.h
+++ b/include/linux/hid.h
@@ -314,15 +314,6 @@ struct hid_item {
#define HID_BAT_ABSOLUTESTATEOFCHARGE 0x00850065

#define HID_VD_ASUS_CUSTOM_MEDIA_KEYS 0xff310076
-/*
- * HID report types --- Ouch! HID spec says 1 2 3!
- */
-
-#define HID_INPUT_REPORT 0
-#define HID_OUTPUT_REPORT 1
-#define HID_FEATURE_REPORT 2
-
-#define HID_REPORT_TYPES 3

/*
* HID connect requests
@@ -509,7 +500,7 @@ struct hid_report {
struct list_head hidinput_list;
struct list_head field_entry_list; /* ordered list of input fields */
unsigned int id; /* id of this report */
- unsigned int type; /* report type */
+ enum hid_report_type type; /* report type */
unsigned int application; /* application usage for this report */
struct hid_field *field[HID_MAX_FIELDS]; /* fields of the report */
struct hid_field_entry *field_entries; /* allocated memory of input field_entry */
@@ -926,7 +917,8 @@ extern int hidinput_connect(struct hid_device *hid, unsigned int force);
extern void hidinput_disconnect(struct hid_device *);

int hid_set_field(struct hid_field *, unsigned, __s32);
-int hid_input_report(struct hid_device *, int type, u8 *, u32, int);
+int hid_input_report(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
+ int interrupt);
struct hid_field *hidinput_get_led_field(struct hid_device *hid);
unsigned int hidinput_count_leds(struct hid_device *hid);
__s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code);
@@ -935,11 +927,11 @@ int __hid_request(struct hid_device *hid, struct hid_report *rep, int reqtype);
u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags);
struct hid_device *hid_allocate_device(void);
struct hid_report *hid_register_report(struct hid_device *device,
- unsigned int type, unsigned int id,
+ enum hid_report_type type, unsigned int id,
unsigned int application);
int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size);
struct hid_report *hid_validate_values(struct hid_device *hid,
- unsigned int type, unsigned int id,
+ enum hid_report_type type, unsigned int id,
unsigned int field_index,
unsigned int report_counts);

@@ -1111,7 +1103,7 @@ void hid_hw_request(struct hid_device *hdev,
struct hid_report *report, int reqtype);
int hid_hw_raw_request(struct hid_device *hdev,
unsigned char reportnum, __u8 *buf,
- size_t len, unsigned char rtype, int reqtype);
+ size_t len, enum hid_report_type rtype, int reqtype);
int hid_hw_output_report(struct hid_device *hdev, __u8 *buf, size_t len);

/**
@@ -1184,8 +1176,8 @@ static inline u32 hid_report_len(struct hid_report *report)
return DIV_ROUND_UP(report->size, 8) + (report->id > 0);
}

-int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
- int interrupt);
+int hid_report_raw_event(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
+ int interrupt);

/* HID quirks API */
unsigned long hid_lookup_quirk(const struct hid_device *hdev);
diff --git a/include/uapi/linux/hid.h b/include/uapi/linux/hid.h
index b34492a87a8a..b25b0bacaff2 100644
--- a/include/uapi/linux/hid.h
+++ b/include/uapi/linux/hid.h
@@ -42,6 +42,18 @@
#define USB_INTERFACE_PROTOCOL_KEYBOARD 1
#define USB_INTERFACE_PROTOCOL_MOUSE 2

+/*
+ * HID report types --- Ouch! HID spec says 1 2 3!
+ */
+
+enum hid_report_type {
+ HID_INPUT_REPORT = 0,
+ HID_OUTPUT_REPORT = 1,
+ HID_FEATURE_REPORT = 2,
+
+ HID_REPORT_TYPES,
+};
+
/*
* HID class requests
*/
--
2.36.1

2022-09-20 14:25:40

by Benjamin Tissoires

[permalink] [raw]
Subject: Re: [PATCH bpf-next v10 00/23] Introduce eBPF support for HID devices

On Fri, Sep 2, 2022 at 3:29 PM Benjamin Tissoires
<[email protected]> wrote:
>
> Hi,
>
> here comes the v10 of the HID-BPF series.
>
> Again, for a full explanation of HID-BPF, please refer to the last patch
> in this series (23/23).
>
> Hopefully we are getting closer to merging the bpf-core changes that
> are pre-requesite of the HID work.
>
> This revision of the series focused on those bpf-core changes with
> a hopefully proper way of fixing access to ctx pointers, and a few more
> selftests to cover those changes.
>
> Once those bpf changes are in, the HID changes are pretty much self
> consistent, which is a good thing, but I still wonder how we are going
> to merge the selftests. I'd rather have the selftests in the bpf tree to
> prevent any regression on bpf-core changes, but that might require some
> coordination between the HID and bpf trees.
>
> Anyway, let's hope we are getting closer to the end of those revisions :)

FWIW, I have now applied the HID patches 8, 9, and 10 to hid.git. They
are independent of the bpf work and given how close we are to 6.1, we
can take them just now.
Patch 11 is having a conflict with the HID tree, so I'll need to
handle it in v11 for the HID part.

The first few patches have already been applied in the bpf-next tree,
as part of the v11 subset of those patches.

The plan is now to wait for all of these to land in 6.1-rc1, and then
submit only the HID changes as a followup series for 6.2.

Cheers,
Benjamin

>
> Cheers,
> Benjamin
>
>
> Benjamin Tissoires (23):
> selftests/bpf: regroup and declare similar kfuncs selftests in an
> array
> bpf: split btf_check_subprog_arg_match in two
> bpf/verifier: allow all functions to read user provided context
> selftests/bpf: add test for accessing ctx from syscall program type
> bpf/btf: bump BTF_KFUNC_SET_MAX_CNT
> bpf/verifier: allow kfunc to return an allocated mem
> selftests/bpf: Add tests for kfunc returning a memory pointer
> HID: core: store the unique system identifier in hid_device
> HID: export hid_report_type to uapi
> HID: convert defines of HID class requests into a proper enum
> HID: Kconfig: split HID support and hid-core compilation
> HID: initial BPF implementation
> selftests/bpf: add tests for the HID-bpf initial implementation
> HID: bpf: allocate data memory for device_event BPF programs
> selftests/bpf/hid: add test to change the report size
> HID: bpf: introduce hid_hw_request()
> selftests/bpf: add tests for bpf_hid_hw_request
> HID: bpf: allow to change the report descriptor
> selftests/bpf: add report descriptor fixup tests
> selftests/bpf: Add a test for BPF_F_INSERT_HEAD
> samples/bpf: HID: add new hid_mouse example
> samples/bpf: HID: add Surface Dial example
> Documentation: add HID-BPF docs
>
> Documentation/hid/hid-bpf.rst | 513 +++++++++
> Documentation/hid/index.rst | 1 +
> drivers/Makefile | 2 +-
> drivers/hid/Kconfig | 20 +-
> drivers/hid/Makefile | 2 +
> drivers/hid/bpf/Kconfig | 17 +
> drivers/hid/bpf/Makefile | 11 +
> drivers/hid/bpf/entrypoints/Makefile | 93 ++
> drivers/hid/bpf/entrypoints/README | 4 +
> drivers/hid/bpf/entrypoints/entrypoints.bpf.c | 66 ++
> .../hid/bpf/entrypoints/entrypoints.lskel.h | 682 ++++++++++++
> drivers/hid/bpf/hid_bpf_dispatch.c | 526 ++++++++++
> drivers/hid/bpf/hid_bpf_dispatch.h | 28 +
> drivers/hid/bpf/hid_bpf_jmp_table.c | 577 ++++++++++
> drivers/hid/hid-core.c | 49 +-
> include/linux/bpf.h | 11 +-
> include/linux/bpf_verifier.h | 2 +
> include/linux/btf.h | 10 +
> include/linux/hid.h | 38 +-
> include/linux/hid_bpf.h | 148 +++
> include/uapi/linux/hid.h | 26 +-
> include/uapi/linux/hid_bpf.h | 25 +
> kernel/bpf/btf.c | 149 ++-
> kernel/bpf/verifier.c | 66 +-
> net/bpf/test_run.c | 37 +
> samples/bpf/.gitignore | 2 +
> samples/bpf/Makefile | 27 +
> samples/bpf/hid_mouse.bpf.c | 134 +++
> samples/bpf/hid_mouse.c | 161 +++
> samples/bpf/hid_surface_dial.bpf.c | 161 +++
> samples/bpf/hid_surface_dial.c | 232 ++++
> tools/include/uapi/linux/hid.h | 62 ++
> tools/include/uapi/linux/hid_bpf.h | 25 +
> tools/testing/selftests/bpf/Makefile | 2 +-
> tools/testing/selftests/bpf/config | 3 +
> tools/testing/selftests/bpf/prog_tests/hid.c | 990 ++++++++++++++++++
> .../selftests/bpf/prog_tests/kfunc_call.c | 182 +++-
> tools/testing/selftests/bpf/progs/hid.c | 206 ++++
> .../selftests/bpf/progs/kfunc_call_fail.c | 160 +++
> .../selftests/bpf/progs/kfunc_call_test.c | 71 ++
> 40 files changed, 5416 insertions(+), 105 deletions(-)
> create mode 100644 Documentation/hid/hid-bpf.rst
> create mode 100644 drivers/hid/bpf/Kconfig
> create mode 100644 drivers/hid/bpf/Makefile
> create mode 100644 drivers/hid/bpf/entrypoints/Makefile
> create mode 100644 drivers/hid/bpf/entrypoints/README
> create mode 100644 drivers/hid/bpf/entrypoints/entrypoints.bpf.c
> create mode 100644 drivers/hid/bpf/entrypoints/entrypoints.lskel.h
> create mode 100644 drivers/hid/bpf/hid_bpf_dispatch.c
> create mode 100644 drivers/hid/bpf/hid_bpf_dispatch.h
> create mode 100644 drivers/hid/bpf/hid_bpf_jmp_table.c
> create mode 100644 include/linux/hid_bpf.h
> create mode 100644 include/uapi/linux/hid_bpf.h
> create mode 100644 samples/bpf/hid_mouse.bpf.c
> create mode 100644 samples/bpf/hid_mouse.c
> create mode 100644 samples/bpf/hid_surface_dial.bpf.c
> create mode 100644 samples/bpf/hid_surface_dial.c
> create mode 100644 tools/include/uapi/linux/hid.h
> create mode 100644 tools/include/uapi/linux/hid_bpf.h
> create mode 100644 tools/testing/selftests/bpf/prog_tests/hid.c
> create mode 100644 tools/testing/selftests/bpf/progs/hid.c
> create mode 100644 tools/testing/selftests/bpf/progs/kfunc_call_fail.c
>
> --
> 2.36.1
>