Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp3200636pxp; Tue, 8 Mar 2022 09:28:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJyR8v4iYfParErD+gT+u+M0M2NQ271LetpUA5CdeiMMrlOLrRty0tZEqzmUNpFTWH049kLQ X-Received: by 2002:a05:6402:1c1e:b0:416:5b93:eacf with SMTP id ck30-20020a0564021c1e00b004165b93eacfmr8156222edb.302.1646760521617; Tue, 08 Mar 2022 09:28:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646760521; cv=none; d=google.com; s=arc-20160816; b=kZXElkbXlFvzFCP/xY1DWw80VJkh6e2csmONvJgFGp4EivB/WAaGcKVtuyA8oXeyQF b0euddNeqBL5Z50p/LuUFgX3dz4nVQSHY5ZZHailN50yi7ADJ2Ion3CuwFqvJbifp908 Y8RNm0zGFaLSgbP+esXruukK6kpJr6GJvjtE9OBg4J4Cd8yrdPvqNuVs0eMcLI3Riw0E Bn/0qQljwJwH6re09rNiXl+kYbP2StgMSCIaDC1cepYkhlUmyoWVS7QsotTvEWqc+THf PwXDD3+PLMqOVErIw1Zjl0ilZfN8Z+GdCGc2Ag8ypx66PTRce+t3J2QA+ZnYwot0KIM8 QFFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=BHJNKcefmBPCZQfETqNljYPzoWjRgF0JS6C9KGiRgtk=; b=QyFTWfPcZ4s2NwZOuMa9hLC1xpYWwn+orR4Ucb4nuDrUXBzfywYZhw1uPaVerJV0af z/w1kylZaD2x2ViW/6C/LUOyZoiJExf+1XGcCkZtS9iYmJKc4BinDk2E+tL5hOMvaipm 8ySlhYGPuxJjmfKWRmO87qiaqhYBWDgW6Y+m44Azx1BKQaH56m/wh0cjxZw1ZJ0hUyB4 gJYWE1igtjJ1/hpuqgEdxkZ59A24knb6fInHAS9UoF6yrZsNWWnamrzLHY/yZdgkO74+ A3fcdwdPnOeHSUjEjG+gucJgfiDtHAl3/fEZTUl7V8wnGcDGslJYnwnZMdMGYsotmNrL bvFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PvgWUD9v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id se21-20020a170906ce5500b006db099d3e82si6536362ejb.653.2022.03.08.09.28.15; Tue, 08 Mar 2022 09:28:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PvgWUD9v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345307AbiCHJVU (ORCPT + 99 others); Tue, 8 Mar 2022 04:21:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244519AbiCHJVR (ORCPT ); Tue, 8 Mar 2022 04:21:17 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EBCAA3D1EA for ; Tue, 8 Mar 2022 01:20:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646731220; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=BHJNKcefmBPCZQfETqNljYPzoWjRgF0JS6C9KGiRgtk=; b=PvgWUD9v7LgR7eO+wwEnQGvMQ8iKIPNUWauk2lDqTZAzr/b2yInzUUmuTt/uMbzUCTGQh+ cEsiUfJ0Fj93YRD0a8tOiCkpQLk2C34JhYdtvdqtlDll70GJB83adgw5JpS54zWEmr0olf XJLqToScf1upxDkZ3yNtcCS7NcjjaC4= Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-668-AgNvwbm1NWupDhdvBMw_CA-1; Tue, 08 Mar 2022 04:20:19 -0500 X-MC-Unique: AgNvwbm1NWupDhdvBMw_CA-1 Received: by mail-pf1-f198.google.com with SMTP id y193-20020a62ceca000000b004f6f5bbaf7cso4008360pfg.16 for ; Tue, 08 Mar 2022 01:20:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=BHJNKcefmBPCZQfETqNljYPzoWjRgF0JS6C9KGiRgtk=; b=QVE+QVcmdBlsugD6gfaUyCxVdYPbskf5KV5KF+QFvAIyuc6Rk11V43zwsaC2tHGnJ2 GZyIoPRM5miz8zZerMiGaKj0fUB4B+BsiNnMRttkSuyD09gngtmioDo3U7Y0JNQsW4nW /nwbXZg0oL88jtEgFvzARekY33KdxvqubF1dmrvcWPbIInns1HYvEcYGkJ6yJlwViGda 1HhFS8rHwLRKt0/+TQySP+URnGgkW/6lJkmwsk/mVsGFVN4RKOknSlU1f+Frew8Izhdp XKKF+05bvW2MDSGozadTHuge3MLv5P8IgO09c/6Z5a2V6pO1m/Jl/F0e31s5HGZwlD+B BFcg== X-Gm-Message-State: AOAM532heqLZCypVsbtKUnmPeHSc4zgGIQ9Nrj3RDhrW59Zh9qmp5VRx 9NSNk4pgSzvbcy1axM3861KZ9dI23TjYkxIb0G4ePYb+buAxRA7NcrZlVCYCF1gXBxRO9Aq30iI 1cE43AZa9cAjHZDFkXjjJOfe5tYfv+bEpWXd+qJCj X-Received: by 2002:a17:90a:560a:b0:1bc:72e7:3c13 with SMTP id r10-20020a17090a560a00b001bc72e73c13mr3561598pjf.246.1646731217683; Tue, 08 Mar 2022 01:20:17 -0800 (PST) X-Received: by 2002:a17:90a:560a:b0:1bc:72e7:3c13 with SMTP id r10-20020a17090a560a00b001bc72e73c13mr3561553pjf.246.1646731217191; Tue, 08 Mar 2022 01:20:17 -0800 (PST) MIME-Version: 1.0 References: <20220304172852.274126-1-benjamin.tissoires@redhat.com> <20220304172852.274126-3-benjamin.tissoires@redhat.com> In-Reply-To: From: Benjamin Tissoires Date: Tue, 8 Mar 2022 10:20:06 +0100 Message-ID: Subject: Re: [PATCH bpf-next v2 02/28] bpf: introduce hid program type To: Song Liu Cc: Greg KH , Jiri Kosina , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Shuah Khan , Dave Marchevsky , Joe Stringer , Tero Kristo , open list , "open list:HID CORE LAYER" , Networking , bpf , linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 8, 2022 at 1:57 AM Song Liu wrote: > > On Mon, Mar 7, 2022 at 10:39 AM Benjamin Tissoires > wrote: > > > > On Sat, Mar 5, 2022 at 1:03 AM Song Liu wrote: > > > > > > On Fri, Mar 4, 2022 at 9:31 AM Benjamin Tissoires > > > wrote: > > > > > > > > HID is a protocol that could benefit from using BPF too. > > > > > > [...] > > > > > > > +#include > > > > +#include > > > > + > > > > +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. > > Thanks for this information. Different attach_types sound right for the use > case. > > > > > > > > > [...] > > > > > > > +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? > > I think .convert_ctx_access is the way to go if we want to access the data > buffer without memcpy. I am not sure how much work is needed to make > it compatible with CORE though. > > To make sure I understand the case, do we want something like > > bpf_prog(struct hid_bpf_ctx *ctx) > { > /* makes sure n < ctx->size */ > x = ctx->data[n]; /* read data */ > ctx->data[n] = ; /* write data */ > ctx->size = ; /* change data size */ > } > > We also need it to be CORE, so that we may modify hid_bpf_ctx by > inserting more members to it before data. > > Is this accurate? > Yes, you pretty much summed it all (except maybe that we might want to have allocated_size in addition to size so we can also grow the value of .size within the allocated limit). All in all, what I want for HID bpf programs is to be able to read and write an array of bytes, and change its size within an allocated kernel limit. This will apply to every HID bpf attach type, to the exception of some BPF_HID_ATTACH_DRIVER_EVENT when we are receiving a suspend/resume notification. Though in the suspend/resume case we won't have the data array available, so it won't matter much. I want the HID bpf programs to be CORE, but if you tell me that it would matter only if we need to reshuffle hid_bpf_ctx, I would be fine simply put a comment "new fields must be added at the end" like some other definitions of contexts are doing. Besides that, I currently do not want to allow access to the content of struct hid_device (or any other kernel struct) in HID BPF programs. That might be of interest at some point for debugging, but with just the array capability I should be able to achieve all of my use cases. Cheers, Benjamin