All of lore.kernel.org
 help / color / mirror / Atom feed
From: Benjamin Tissoires <benjamin.tissoires@redhat.com>
To: Song Liu <song@kernel.org>
Cc: Greg KH <gregkh@linuxfoundation.org>,
	Jiri Kosina <jikos@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>, Shuah Khan <shuah@kernel.org>,
	Dave Marchevsky <davemarchevsky@fb.com>,
	Joe Stringer <joe@cilium.io>,
	Tero Kristo <tero.kristo@linux.intel.com>,
	open list <linux-kernel@vger.kernel.org>,
	"open list:HID CORE LAYER" <linux-input@vger.kernel.org>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	linux-kselftest@vger.kernel.org
Subject: Re: [PATCH bpf-next v2 02/28] bpf: introduce hid program type
Date: Mon, 7 Mar 2022 19:39:40 +0100	[thread overview]
Message-ID: <CAO-hwJKFE4Ps962BBubn8=1K0k9mC2qi8VerFbZo1sqpp6yekg@mail.gmail.com> (raw)
In-Reply-To: <CAPhsuW5CYF9isR4ffRdm3xA_n_FBoL+AGFkzNn4dn2LgRaQQkg@mail.gmail.com>

On Sat, Mar 5, 2022 at 1:03 AM Song Liu <song@kernel.org> wrote:
>
> On Fri, Mar 4, 2022 at 9:31 AM Benjamin Tissoires
> <benjamin.tissoires@redhat.com> 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)?

Not quite sure I am entirely following you, but I consider the various
attach types to be quite different and thus you can not really reuse
the same BPF program with 2 different attach types.

In my view, we have 4 attach types:
- BPF_HID_ATTACH_DEVICE_EVENT: called whenever we receive an IRQ from
the given device (so this is net-like event stream)
- BPF_HID_ATTACH_RDESC_FIXUP: there can be only one of this type, and
this is called to change the device capabilities. So you can not reuse
the other programs for this one
- BPF_HID_ATTACH_USER_EVENT: called explicitly by the userspace
process owning the program. There we can use functions that are
sleeping (we are not in IRQ context), so this is also fundamentally
different from the 3 others.
- BPF_HID_ATTACH_DRIVER_EVENT: whenever the driver gets called into,
we get a bpf program run. This can be suspend/resume, or even specific
request to the device (change a feature on the device or get its
current state). Again, IMO fundamentally different from the others.

So I'm open to any suggestions, but if we can keep the userspace API
being defined with different SEC in libbpf, that would be the best.

>
> [...]
>
> > +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?

I had a thought at this context today, and I think I am getting to the
limit of what I understand.

My first worry is that the way I wrote it there, with a variable data
field length is that this is not forward compatible. Unless BTF and
CORE are making magic, this will bite me in the long run IMO.

But then, you are talking about not using uapi, and I am starting to
wonder: am I doing the things correctly?

To solve my first issue (and the weird API I had to introduce in the
bpf_hid_get/set_data), I came up to the following:
instead of exporting the data directly in the context, I could create
a helper bpf_hid_get_data_buf(ctx, const uint size) that returns a
RET_PTR_TO_ALLOC_MEM_OR_NULL in the same way bpf_ringbuf_reserve()
does.

This way, I can directly access the fields within the bpf program
without having to worry about the size.

But now, I am wondering whether the uapi I defined here is correct in
the way CORE works.

My goal is to have HID-BPF programs to be CORE compatible, and not
have to recompile them depending on the underlying kernel.

I can not understand right now if I need to add some other BTF helpers
in the same way the access to struct xdp_md and struct xdp_buff are
converted between one and other, or if defining a forward compatible
struct hid_bpf_ctx is enough.
As far as I understand, .convert_ctx_access allows to export a stable
uapi to the bpf prog users with the verifier doing the conversion
between the structs for me. But is this really required for all the
BPF programs if we want them to be CORE?

Also, I am starting to wonder if I should not hide fields in the
context to the users. The .data field could be a pointer and only
accessed through the helper I mentioned above. This would be forward
compatible, and also allows to use whatever available memory in the
kernel to be forwarded to the BPF program. This way I can skip the
memcpy part and work directly with the incoming dma data buffer from
the IRQ.

But is it best practice to do such a thing?

Cheers,
Benjamin

>
> [...]
>
> > +
> > +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;
> [...]
>


  reply	other threads:[~2022-03-07 18:40 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-04 17:28 [PATCH bpf-next v2 00/28] Introduce eBPF support for HID devices Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 01/28] bpf: add new is_sys_admin_prog_type() helper Benjamin Tissoires
2022-03-04 23:12   ` Song Liu
2022-03-05 10:07     ` Benjamin Tissoires
2022-03-05 16:58       ` Sean Young
2022-03-07 18:26         ` Song Liu
2022-03-07 18:23       ` Song Liu
2022-03-09  8:27   ` Sean Young
2022-03-04 17:28 ` [PATCH bpf-next v2 02/28] bpf: introduce hid program type Benjamin Tissoires
2022-03-04 18:21   ` Greg KH
2022-03-07 17:57     ` Benjamin Tissoires
2022-03-05  0:02   ` Song Liu
2022-03-07 18:39     ` Benjamin Tissoires [this message]
2022-03-08  0:56       ` Song Liu
2022-03-08  9:20         ` Benjamin Tissoires
2022-03-11 17:16           ` Benjamin Tissoires
2022-03-05  0:20   ` Song Liu
2022-03-04 17:28 ` [PATCH bpf-next v2 03/28] HID: hook up with bpf Benjamin Tissoires
2022-03-04 18:24   ` Greg KH
2022-03-05  0:23   ` Song Liu
2022-03-15 16:29   ` Tero Kristo
2022-03-04 17:28 ` [PATCH bpf-next v2 04/28] libbpf: add HID program type and API Benjamin Tissoires
2022-03-05  0:31   ` Song Liu
2022-03-08  1:30   ` Andrii Nakryiko
2022-03-08  1:38     ` Song Liu
2022-03-08  5:52       ` Andrii Nakryiko
2022-03-04 17:28 ` [PATCH bpf-next v2 05/28] selftests/bpf: add tests for the HID-bpf initial implementation Benjamin Tissoires
2022-03-05  0:41   ` Song Liu
2022-03-05 10:10     ` Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 06/28] samples/bpf: add new hid_mouse example Benjamin Tissoires
2022-03-04 18:26   ` Greg KH
2022-03-04 17:28 ` [PATCH bpf-next v2 07/28] bpf/hid: add a new attach type to change the report descriptor Benjamin Tissoires
2022-03-04 18:32   ` Greg KH
2022-03-04 17:28 ` [PATCH bpf-next v2 08/28] HID: allow to change the report descriptor from an eBPF program Benjamin Tissoires
2022-03-04 18:32   ` Greg KH
2022-03-04 17:28 ` [PATCH bpf-next v2 09/28] libbpf: add new attach type BPF_HID_RDESC_FIXUP Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 10/28] selftests/bpf: add report descriptor fixup tests Benjamin Tissoires
2022-03-04 18:34   ` Greg KH
2022-03-04 17:28 ` [PATCH bpf-next v2 11/28] samples/bpf: add a report descriptor fixup Benjamin Tissoires
2022-03-04 18:36   ` Greg KH
2022-03-04 17:28 ` [PATCH bpf-next v2 12/28] bpf/hid: add hid_{get|set}_data helpers Benjamin Tissoires
2022-03-04 18:37   ` Greg KH
2022-03-04 18:41   ` Greg KH
2022-03-05 10:33     ` Benjamin Tissoires
2022-03-05 10:47       ` Greg KH
2022-03-11  0:40         ` Song Liu
2022-03-11 15:08           ` Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 13/28] HID: bpf: implement hid_bpf_get|set_data Benjamin Tissoires
2022-03-04 18:39   ` Greg KH
2022-03-04 17:28 ` [PATCH bpf-next v2 14/28] selftests/bpf: add tests for hid_{get|set}_data helpers Benjamin Tissoires
2022-03-15 16:49   ` Tero Kristo
2022-03-15 17:02     ` Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 15/28] bpf/hid: add new BPF type to trigger commands from userspace Benjamin Tissoires
2022-03-11  0:46   ` Song Liu
2022-03-04 17:28 ` [PATCH bpf-next v2 16/28] libbpf: add new attach type BPF_HID_USER_EVENT Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 17/28] selftests/bpf: add test for user call of HID bpf programs Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 18/28] selftests/bpf: hid: rely on uhid event to know if a test device is ready Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 19/28] bpf/hid: add bpf_hid_raw_request helper function Benjamin Tissoires
2022-03-11  0:50   ` Song Liu
2022-03-04 17:28 ` [PATCH bpf-next v2 20/28] HID: add implementation of bpf_hid_raw_request Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 21/28] selftests/bpf: add tests for bpf_hid_hw_request Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 22/28] bpf/verifier: prevent non GPL programs to be loaded against HID Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 23/28] HID: bpf: compute only the required buffer size for the device Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 24/28] HID: bpf: only call hid_bpf_raw_event() if a ctx is available Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 25/28] bpf/hid: Add a flag to add the program at the beginning of the list Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 26/28] libbpf: add handling for BPF_F_INSERT_HEAD in HID programs Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 27/28] selftests/bpf: Add a test for BPF_F_INSERT_HEAD Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 28/28] samples/bpf: fix bpf_program__attach_hid() api change Benjamin Tissoires
2022-03-05  1:13 ` [PATCH bpf-next v2 00/28] Introduce eBPF support for HID devices Song Liu
2022-03-05 10:23   ` Benjamin Tissoires
2022-03-07 18:11     ` Song Liu
2022-03-08 13:37       ` Benjamin Tissoires
2022-03-15 17:04 ` Tero Kristo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAO-hwJKFE4Ps962BBubn8=1K0k9mC2qi8VerFbZo1sqpp6yekg@mail.gmail.com' \
    --to=benjamin.tissoires@redhat.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davemarchevsky@fb.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jikos@kernel.org \
    --cc=joe@cilium.io \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=songliubraving@fb.com \
    --cc=tero.kristo@linux.intel.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.