Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp985755iob; Fri, 13 May 2022 18:37:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzhKt3DrZXYkq3nXp0fKoX9z9LmQmQ5CogSbyqiNLzPjpSwuvbhuQdvKNl8DCGt4Zf1uczG X-Received: by 2002:a5d:64a6:0:b0:20c:64ef:c9cc with SMTP id m6-20020a5d64a6000000b0020c64efc9ccmr6368285wrp.190.1652492271002; Fri, 13 May 2022 18:37:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652492270; cv=none; d=google.com; s=arc-20160816; b=KQjdSqAeFY9t5U0yLzW2K+b2HQnT2ONubFe3mvgWj7eR2SH7eBESO4Y6679svC3UXY pG+IhJf04gHzeIprV5RbPJvTLckvDXJTHPMoHDkenxOctg0ApVrjH/sfjSFn9X7Xm7gt 6hxxNF0vMeuD4AFtqHiYReZlCWeJPO84iEloWQdaskjBin1Ir4h95OBHAdI+4dnOHgei e9EzoDTIJ/6q1WpJHjCEjLqDUfzaN4Wm3NuvmQTai+jngwop4ULgFMQAZis6O+4HcY/h oCItDBs0JMuoBZ5bM5oZq2a6E7aQTPnV/IMRoHzxixnCPD5Hac05X9UYvM7wKS53tGDf P0lQ== 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=UpuYzgBorX/kwU/c/lW2Z6Gvah66kJD+vnpuI+T23y0=; b=YlpbAC23qxzPBXb4EiMhxqnqZAhelynSZMZaR0ubLWGoXAVtvfNk8/UD0lu+0AUSRQ Cy9U12Py6W50wffu0ePdKQh5CHYEbNnK3E6cMdy/WOJ+vPela5+H78Dv+QvR57xzg7CF xqe6rnyQNLVxTNp0mi/hTv8TjxaBHiwUyfoMWyfPU56g51ceiP2hK7SeqQMb/U8DxSx9 hwqpYXqflQmxVzGA1BJqIngzxGV5zUUrjrFXYQHOAa8iztqIHeHP7EoiVF2PNhKHj7t5 VjJTLfG85G4mwW/DkgxWuNI5L6qNOTPxr4GoE7+tnb8HxCnpu+BCPPy9Z0EmPuOYew6y 5ltw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HXIqXWkh; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id o14-20020a5d648e000000b0020adc2b1d9esi4821782wri.188.2022.05.13.18.37.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 18:37:50 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HXIqXWkh; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DE3174185DA; Fri, 13 May 2022 17:04:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382709AbiEMRAi (ORCPT + 99 others); Fri, 13 May 2022 13:00:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382703AbiEMRAb (ORCPT ); Fri, 13 May 2022 13:00:31 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 03D8256C2E for ; Fri, 13 May 2022 10:00:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652461211; 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=UpuYzgBorX/kwU/c/lW2Z6Gvah66kJD+vnpuI+T23y0=; b=HXIqXWkhVxXKy93/smkp+xaeaBBOFYvvxXMdH3NLA1WtPGj+vcVCdmpAWLusB3cVdxMCF8 W9z+G+c/PbteXWHyUslNI9dY6/ocT3QIAAl0zoUVL5RttZy0AKMxVO6lqo7GPSWLPLraPY 0miDltxDLcG+lZFOY6dxTTiwMJiwNFk= Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-117-7mVR_bL5Nt6605Yq0oR7vA-1; Fri, 13 May 2022 13:00:02 -0400 X-MC-Unique: 7mVR_bL5Nt6605Yq0oR7vA-1 Received: by mail-pg1-f199.google.com with SMTP id s68-20020a637747000000b003aaff19b95bso4460779pgc.1 for ; Fri, 13 May 2022 10:00:02 -0700 (PDT) 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=UpuYzgBorX/kwU/c/lW2Z6Gvah66kJD+vnpuI+T23y0=; b=dNtYpsztzO/Ii6wb+dAPKpIqZPc7uwJzTgUgkRce2nObouvO7gtnRiXuRzDq6traUO DSNDK+5xKQK9EFzrS+bEFxGSE0oN2b6c+PEmdus7p6n4ewElQzakQwrpPemvs2kMVeB+ uMu0RM63EKnY3ydSZxfK3opsnMomoK0YA3RJYPNgcSvQwqrs6HO5fLSFsr6HkaG4ca2R L2JoFvQVsoU1PRCSjdzJ0AnNWG5dT8qss27ZzPT0kVQmOPRuvYL2yTckafc30zk+RpWO gXYxfq1MJM78w0bsXrnAppEF+6ZpGGMrlH+r9QD/sMtmY6nQ8mL5d1+koDme4yafduz2 +opg== X-Gm-Message-State: AOAM533jRhg4+8xHjNgCX36lSpvq/FN5hFobkox3oKX93juIeyj8upCx IojrR6/B0st+YJe2HH7UF/3cRyk2k1s4oaMHTCGfi+DsmWd/mfaosAEvjQHAGJJRxmT4zli0SJg widcCUYAuorFpYmuOc5mze/Xs1ZAOOAhjxwcjjmmd X-Received: by 2002:a17:902:c409:b0:15e:bdd3:1fa3 with SMTP id k9-20020a170902c40900b0015ebdd31fa3mr5717806plk.67.1652461201159; Fri, 13 May 2022 10:00:01 -0700 (PDT) X-Received: by 2002:a17:902:c409:b0:15e:bdd3:1fa3 with SMTP id k9-20020a170902c40900b0015ebdd31fa3mr5717783plk.67.1652461200888; Fri, 13 May 2022 10:00:00 -0700 (PDT) MIME-Version: 1.0 References: <20220421140740.459558-1-benjamin.tissoires@redhat.com> <20220426040314.ez3cdpv2w45vbgkk@MBP-98dd607d3435.dhcp.thefacebook.com> <20220512041607.zloscyl3k5z3sdp2@MBP-98dd607d3435.dhcp.thefacebook.com> In-Reply-To: <20220512041607.zloscyl3k5z3sdp2@MBP-98dd607d3435.dhcp.thefacebook.com> From: Benjamin Tissoires Date: Fri, 13 May 2022 18:59:50 +0200 Message-ID: Subject: Re: [RFC bpf-next v4 0/7] Introduce eBPF support for HID devices (new attempt) To: Alexei Starovoitov Cc: Greg KH , Jiri Kosina , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Tero Kristo , lkml , "open list:HID CORE LAYER" , bpf 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,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_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 Thu, May 12, 2022 at 6:16 AM Alexei Starovoitov wrote: > > On Mon, May 02, 2022 at 11:43:51PM +0200, Benjamin Tissoires wrote: > > On Sat, Apr 30, 2022 at 9:12 AM Benjamin Tissoires > > wrote: > > > > > [...] > > > This is roughly what I have now: > > > > > > - hid-core is not aware of BPF except for a few __weak > > > ALLOW_ERROR_INJECTION hooks (dispatch_hid_bpf_device_event for > > > example) > > > - I have a separate hid-bpf module that attaches BPF traces to these > > > hooks and calls a "dispatch" kfunc within hid-bpf > > > - the dispatch function then do a succession of BPF calls to programs > > > attached to it by using bpf_tail_call(prog_array, hid_id) > > > > > > - for the clients, they define one or more > > > SEC("fmod_ret/hid_bpf_device_event"). That __weak hook is declared in > > > the kernel by hid-bpf but is never called, it's just an API > > > declaration > > > - then clients call in a SEC("syscall") > > > hid_bpf_attach_prog(ctx->prog_fd, ctx->hid_id, ctx->flags); > > > - hid_bpf_attach_prog is a kfunc that takes a ref on the struct > > > bpf_prog*, and stores that program in the correct struct bpf_map *for > > > the given attached_btf_id (hid_bpf_device_event in our case) > > > > > > And that's about it. > > > I still need to handle automatic release of the bpf prog when there is > > > no userspace open fd on it unless it's pinned but I think this should > > > be working fine. > > > > > > I also probably need to pin some SEC("syscall") (hid_bpf_attach_prog > > > and hid_bpf_dettach_prog) so users don't have to write them down and > > > can just use the ones provided by the kernel. > > > > > > The nice thing is that I can define my own API for the attach call > > > without dealing with bpf core. I can thus add a priority flag that is > > > relevant here because the data coming through the bpf program can be > > > modified. > > > > > > The other thing is that now, I don't care which function we are in to > > > decide if a RET_PTR_MEM is read only or not. I can deal with that by > > > either playing with the flags or even replacing entirely the dispatch > > > trace prog from userspace if I want to access the raw events. > > > > > > However, the downsides are: > > > - I need to also define kfuncs for BPF_PROG_TYPE_SYSCALL (I don't > > > think It'll be a big issue) > > > - The only way I could store the bpf_prog into the map was to hack > > > around the map ops, because the fd of the map in the skel is not > > > available while doing a SEC("syscall") from a different process. > > > > Update on this side: I realized that I could use the syscall > > BPF_MAP_GET_FD_BY_ID instead to get an fd for the current task. > > However, I've been bitten quite hard today because I was using > > bpf_map_get() instead of bpf_map_get_with_uref(), and so every time I > > closed the fd in the syscall the map was cleared... > > > > But now I would like to have more than one program of a type per hid > > device, meaning that I can not have only one bpf_map of type > > BPF_MAP_TYPE_PROG_ARRAY. > > I have explored BPF_MAP_TYPE_HASH_OF_MAPS, but we can not have > > BPF_MAP_TYPE_PROG_ARRAY as inner maps with the current code. And I'd > > need 2 levels of nesting (which is not authorized today): > > - hid_jmp_table (key: HID id) > > - array of different program type per HID device (key: HID_BPF_PROG_TYPE) > > - BPF_MAP_TYPE_PROG_ARRAY with the actual progs (key: int) > > > > The other solution would be to be able to create a map when needed, > > store it in struct hid_device, and then call bpf_tail_call on this > > map. The problem is that I need a way to teach the verifier that the > > struct bpf_map pointer I have in the context is a true bpf_map... > > We have kptr feature now. > So bpf progs can store pointers to specific kernel data structures > inside map values. > Storing 'struct bpf_map *' in a map value would be something :) > Circular dependency issues to address. Maybe it's doable. > > Would hash based prog_array work ? > Then the key can be an arbitrary combination. > There is fd_htab logic. It's used for map-in-map. > We can tweak it to store progs in a hash map. > In the end, I am just using a global prog_array map, and handling the indexes myself. It is probably not the cleaner and the most reusable, but it allows me at least to move forward. FWIW, I should be able to send v5 next week. I am almost done reimplementing everything I had in v3, and I am now fighting with hid.ko as a module (should be solved soon enough). Cheers, Benjamin