Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2311834pxp; Mon, 21 Mar 2022 16:37:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzBjhqduQgFNji6gRsKHvo4LPFy2m0dHUjLYi2Dmvan57Y1W0NT5p7SpOC8olalKREwf2W2 X-Received: by 2002:a17:90a:5b06:b0:1b8:b705:470b with SMTP id o6-20020a17090a5b0600b001b8b705470bmr1674992pji.168.1647905845751; Mon, 21 Mar 2022 16:37:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647905845; cv=none; d=google.com; s=arc-20160816; b=j5xjOkBu+N0Jy4aHxlTzAm4zEiR3I5AJhomHd202phXvUe8NeavKjHlK3PEcnp1lF5 lIdDBoRBBLOmen9yzPMNaI7vbYcZBnA4xMKf+0/mNpifBJaHTHVqVPL1Ii6ZMawCez5c Gb+6t7QWS+Jyau3XqONwP+9dhtI9muYxZq2afbTWw8zXo4ocscHtqdK/UsMqWXxlde4t AQXnYBqUd4IqBBfdZeXXVg1Yv0JtZlng4uBkl70waOrvhxCDeaffnuWB0MZtTQQorD+0 /uUEabacVloYcJdo5sWXvfkU1wxV5M1YQnF7gqvhf7QJlBUCTiXcdf7863UYzMYIKx3w ayYw== 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=dYf8CDux3IvCuOhHaeip7wgFNWLlzQXSgcdy7Ivk1Lw=; b=a+6Xo7De3yK+Xlf4RhCRUF6UhAfKu9lRXm9RRc3vWwbAbHwlfyrw5lnLR4Lij9GqK2 umz5yka/jLsCgIv9yhs8e9Od2NaT8Y0Gte9o0dCLcwTO0SVQFtmqImXURHzDqZnTs9Om kErdXIScueQA8fCzhNsvNf3ZHghujlTTuphmGuPOsTBuOBokoyRqHiXy8XdWW+vEboE0 n/hJkr/PlCQudd7uTbBGWZEhTj7p2lJ3pQXo5iX0fDSB1xBVIiHiuARzGeGE5b7uA1y4 VnK7TqpGH0JpZ8xTlBlRZRNyAvbQC0/f5MHAvMmgTLCASDitvJUIOcVa1tsLjzhmeQ68 IQzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UcdkgUCR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id q17-20020a056a00151100b004fa5a577b93si10239258pfu.51.2022.03.21.16.37.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 16:37:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UcdkgUCR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D15EB243176; Mon, 21 Mar 2022 15:46:44 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229688AbiCUWsA (ORCPT + 99 others); Mon, 21 Mar 2022 18:48:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229797AbiCUWrf (ORCPT ); Mon, 21 Mar 2022 18:47:35 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E612636E819; Mon, 21 Mar 2022 15:27:33 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 6D205B81A4D; Mon, 21 Mar 2022 22:03:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C557C340F8; Mon, 21 Mar 2022 22:03:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1647900223; bh=Q7gazwmOiF1+pGMUPd49Zh6pNO4LTkSrTEg1R9lEwig=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=UcdkgUCRnjf+T4JeLeEPezb1Ad/rZaYpJCzlzw+MAAsHZhewgbzIci6bVvXeWCs3C NzLq0oOHw++hHBtel/l5NqWbtchprKX2E14ME0S7FxWGfQvfi7reAx0ZORM37A0AFS 4XjpUf2TOohmM14Hs9nJ99b1zg9xmXHgBi8RZgF7Rq9IxmH7SW14mSK9MQBLb9eELd 7XFOSDInMs4wfF8UxggDApnwTbmYaeaFdMIpzIKYrkeRFrJbRlEcBna5w7AimpJSLk Ah3ogsoHbm7In2pe4B/cEo5qE2fknhVVv35bIDnqPjdO/wY6ZRoSiubeWOg2KKj67b 6HqQcv3FYlyrA== Received: by mail-yb1-f171.google.com with SMTP id z8so30552512ybh.7; Mon, 21 Mar 2022 15:03:43 -0700 (PDT) X-Gm-Message-State: AOAM53198xCckhrOc75EsO+z744t/vpjDxCgYcQH61yC3eBCY4R3tamZ gnJbi8J2ITYvz2wC+9x5ENVg26ptNpkMecKnH9Q= X-Received: by 2002:a25:8b81:0:b0:629:17d5:68c1 with SMTP id j1-20020a258b81000000b0062917d568c1mr23210434ybl.449.1647900222062; Mon, 21 Mar 2022 15:03:42 -0700 (PDT) MIME-Version: 1.0 References: <20220318161528.1531164-1-benjamin.tissoires@redhat.com> <20220318161528.1531164-7-benjamin.tissoires@redhat.com> In-Reply-To: From: Song Liu Date: Mon, 21 Mar 2022 15:03:31 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH bpf-next v3 06/17] HID: allow to change the report descriptor from an eBPF program To: Benjamin Tissoires 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 , Jonathan Corbet , Tero Kristo , open list , "open list:HID CORE LAYER" , Networking , bpf , linux-kselftest@vger.kernel.org, Linux Doc Mailing List Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 Mon, Mar 21, 2022 at 9:20 AM Benjamin Tissoires wrote: > > On Fri, Mar 18, 2022 at 10:10 PM Song Liu wrote: > > > > On Fri, Mar 18, 2022 at 9:17 AM Benjamin Tissoires > > wrote: > > > > > > Make use of BPF_HID_ATTACH_RDESC_FIXUP so we can trigger an rdesc fixup > > > in the bpf world. > > > > > > Whenever the program gets attached/detached, the device is reconnected > > > meaning that userspace will see it disappearing and reappearing with > > > the new report descriptor. > > > > > > Signed-off-by: Benjamin Tissoires > > > > > > --- > > > > > > changes in v3: > > > - ensure the ctx.size is properly bounded by allocated size > > > - s/link_attached/post_link_attach/ > > > - removed the switch statement with only one case > > > > > > changes in v2: > > > - split the series by bpf/libbpf/hid/selftests and samples > > > --- > > > drivers/hid/hid-bpf.c | 62 ++++++++++++++++++++++++++++++++++++++++++ > > > drivers/hid/hid-core.c | 3 +- > > > include/linux/hid.h | 6 ++++ > > > 3 files changed, 70 insertions(+), 1 deletion(-) > > > > > > diff --git a/drivers/hid/hid-bpf.c b/drivers/hid/hid-bpf.c > > > index 5060ebcb9979..45c87ff47324 100644 > > > --- a/drivers/hid/hid-bpf.c > > > +++ b/drivers/hid/hid-bpf.c > > > @@ -50,6 +50,14 @@ static struct hid_device *hid_bpf_fd_to_hdev(int fd) > > > return hdev; > > > } > > > > > > +static int hid_reconnect(struct hid_device *hdev) > > > +{ > > > + if (!test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status)) > > > + return device_reprobe(&hdev->dev); > > > + > > > + return 0; > > > +} > > > + > > > static int hid_bpf_pre_link_attach(struct hid_device *hdev, enum bpf_hid_attach_type type) > > > { > > > int err = 0; > > > @@ -92,6 +100,12 @@ static int hid_bpf_pre_link_attach(struct hid_device *hdev, enum bpf_hid_attach_ > > > return err; > > > } > > > > > > +static void hid_bpf_post_link_attach(struct hid_device *hdev, enum bpf_hid_attach_type type) > > > +{ > > > + if (type == BPF_HID_ATTACH_RDESC_FIXUP) > > > + hid_reconnect(hdev); > > > +} > > > + > > > static void hid_bpf_array_detach(struct hid_device *hdev, enum bpf_hid_attach_type type) > > > { > > > switch (type) { > > > @@ -99,6 +113,9 @@ static void hid_bpf_array_detach(struct hid_device *hdev, enum bpf_hid_attach_ty > > > kfree(hdev->bpf.device_data); > > > hdev->bpf.device_data = NULL; > > > break; > > > + case BPF_HID_ATTACH_RDESC_FIXUP: > > > + hid_reconnect(hdev); > > > + break; > > > default: > > > /* do nothing */ > > > break; > > > @@ -116,6 +133,9 @@ static int hid_bpf_run_progs(struct hid_device *hdev, struct hid_bpf_ctx_kern *c > > > case HID_BPF_DEVICE_EVENT: > > > type = BPF_HID_ATTACH_DEVICE_EVENT; > > > break; > > > + case HID_BPF_RDESC_FIXUP: > > > + type = BPF_HID_ATTACH_RDESC_FIXUP; > > > + break; > > > default: > > > return -EINVAL; > > > } > > > @@ -155,11 +175,53 @@ u8 *hid_bpf_raw_event(struct hid_device *hdev, u8 *data, int *size) > > > return ctx.data; > > > } > > > > > > +u8 *hid_bpf_report_fixup(struct hid_device *hdev, u8 *rdesc, unsigned int *size) > > > +{ > > > + int ret; > > > + struct hid_bpf_ctx_kern ctx = { > > > + .type = HID_BPF_RDESC_FIXUP, > > > + .hdev = hdev, > > > + .size = *size, > > > + }; > > > + > > > + if (bpf_hid_link_empty(&hdev->bpf, BPF_HID_ATTACH_RDESC_FIXUP)) > > > > Do we need to lock bpf_hid_mutex before calling bpf_hid_link_empty()? > > (or maybe we > > already did?) > > The mutex is not locked before this call, indeed. > > However, bpf_hid_link_empty() is an inlined function that just calls > in the end list_empty(). Given that all the list heads are created > just once for the entire life of the HID device, I *think* this is > thread safe and does not require mutex locking. Hmm.. I guess you are right. > > (I might be wrong) > > So when first plugging in the device, if there is a fighting process > that attempts to add a program, if the program managed to insert > itself before we enter this code, then the list won't be empty and we > will execute BPF_PROG_RUN_ARRAY(), and if not, well, we ignore it and > wait for reconnect(). > > But now I am starting to wonder if I need to also protect > BPF_PROG_RUN_ARRAY() under bpf_hid_mutex... I think this is not necessary. Thanks, Song