Re: [PATCH bpf-next v2 02/28] bpf: introduce hid program type

From: Song Liu
Date: Fri Mar 04 2022 - 19:03:09 EST


On Fri, Mar 4, 2022 at 9:31 AM Benjamin Tissoires
<benjamin.tissoires@xxxxxxxxxx> wrote:
>
> HID is a protocol that could benefit from using BPF too.

[...]

> +#include <linux/list.h>
> +#include <linux/slab.h>
> +
> +struct bpf_prog;
> +struct bpf_prog_array;
> +struct hid_device;
> +
> +enum bpf_hid_attach_type {
> + BPF_HID_ATTACH_INVALID = -1,
> + BPF_HID_ATTACH_DEVICE_EVENT = 0,
> + MAX_BPF_HID_ATTACH_TYPE

Is it typical to have different BPF programs for different attach types?
Otherwise, (different types may have similar BPF programs), maybe
we can pass type as an argument to the program (shared among
different types)?

[...]

> +struct hid_device;
> +
> +enum hid_bpf_event {
> + HID_BPF_UNDEF = 0,
> + HID_BPF_DEVICE_EVENT, /* when attach type is BPF_HID_DEVICE_EVENT */
> +};
> +
> +struct hid_bpf_ctx {
> + enum hid_bpf_event type; /* read-only */
> + __u16 allocated_size; /* the allocated size of data below (RO) */

There is a (6-byte?) hole here.

> + struct hid_device *hdev; /* read-only */
> +
> + __u16 size; /* used size in data (RW) */
> + __u8 data[]; /* data buffer (RW) */
> +};

Do we really need hit_bpf_ctx in uapi? Maybe we can just use it
from vmlinuxh?

[...]

> +
> +static bool hid_is_valid_access(int off, int size,
> + enum bpf_access_type access_type,
> + const struct bpf_prog *prog,
> + struct bpf_insn_access_aux *info)
> +{
> + /* everything not in ctx is prohibited */
> + if (off < 0 || off + size > sizeof(struct hid_bpf_ctx) + HID_BPF_MIN_BUFFER_SIZE)
> + return false;

Mabe add the following here to fail unaligned accesses

if (off % size != 0)
return false;
[...]