From patchwork Tue Jan 23 16:40:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 191092 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp475333dyi; Tue, 23 Jan 2024 09:07:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IEbT6rcwqvlFiWlaPXr+ozQHAQ/TK44pMrFH1N/Q47pU2R+T97hx03SzwXgb8iVsTVPX0Gb X-Received: by 2002:a05:6402:391:b0:55c:4c16:765e with SMTP id o17-20020a056402039100b0055c4c16765emr1014446edv.5.1706029633456; Tue, 23 Jan 2024 09:07:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706029633; cv=pass; d=google.com; s=arc-20160816; b=AoDpupAQWkTIe6wLa2Ny5m/fa9ZXm0lCHNZ75FL3EGF+XTxubcz5zqiMTMX8zJiQFR OKQlFpdKMrpo61TKd3AWuSqtu7qcwn84a08sYL2qUfds+vvHGNh/ATdK1K7v/wjGq+gJ 7qqnpIIEN4hZWCog7+n8j1TER2OuMxaDFFakqE42OU18voN7XZs3zVu5onxPaEAnY0w6 DT2nciTw3D4FgFzY6ImDBE+kC49QXclrwJve3+s6ZBQ0D2V0wI53SA8sxu12fGxJmRCI d1/BGB1RcbjGFM4G/Bulsq0o4QCv3kFr4B6Nbsos8fwmhB0ArITsvzB5vkB/j+RmzKOu bwHw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=xZUetoDXJO4cJQ7OJldU96yfEIuJ2xZN2N3dhnBjIyk=; fh=X3E0985Tl39WwlfcJ6B6yAXTRA1gm7OdSLYP2FJoOZI=; b=OXmFv05aSnPJ4rnhGwWsKDgQ67ErVE2AZP7CFdtSAqcchuPGuQoJLvdqtoqqSo7FVg IXMoMuyEe58AoKxATHb7+j8DynAFS1SLr7tQJxf33Mvqt6R3svrWpoOCNIaJ7uhFMW2c 3Z7YQGOVfwNgnt7yMZ5eCZkfdpQ29efKJol+r65KIuInYyIerDJYtCxpfVeVLYU/M4nx gRL8vxuO2pS5pfvdzJ/9YUz4W9hmtWxkDIF5AmRd6ByvUs9gLqOhU8GmebhpshICz5Em J1rSNfA6IKEWMo7qSG9w+jfnqaXk2YnmcA/hXsO6K9OkPmatLJAQ9kr2/8NL3LotpGDU QP1g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iaHV1lvm; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-35687-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35687-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id es13-20020a056402380d00b00557766c0f78si12835127edb.77.2024.01.23.09.07.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 09:07:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35687-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iaHV1lvm; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-35687-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35687-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 9B3FA1F2B9F0 for ; Tue, 23 Jan 2024 16:47:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A0E3E82D9A; Tue, 23 Jan 2024 16:41:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iaHV1lvm" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 66C25823D5; Tue, 23 Jan 2024 16:41:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706028061; cv=none; b=sfedGh95Jpuxx+W6zfeJ6KNMCzdx6shsKVOLWZMJi13ko8rnIRtlOKYYfUm8rpkKyrrWsN9UEGk11WogTuwHBp6JUpAbWRt+5kldaKCJAXGq+C6FbYhjA/xNwPr3I/RAFdPNBs/Mstyh+WK/1GbWIShP2/g6kyK5DHAiZYAZeSI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706028061; c=relaxed/simple; bh=sCdPQ4YhBeymXhJimO5RK/sYdEZSwG7jOkn2T00muOY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=f2nwTlpwn2oQ0iCQi/FaGuQ2KkctyAGUENHDz0F8Shv5XCIS9Y+h728c7wrqEQ2pkX2vBDDdQbvE1BfQkWHKRRDas2HTreDmEdwUKyxgu+bf+diMFHEjHixVUlzkYYNvRYF0Qqw9gogIXo8K53QM7YNpYyJokr2A6aFtR7KKF8I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iaHV1lvm; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 37771C43390; Tue, 23 Jan 2024 16:40:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706028060; bh=sCdPQ4YhBeymXhJimO5RK/sYdEZSwG7jOkn2T00muOY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=iaHV1lvmINnPhODKpmO4Q0NmgmCOTgoRdI0W/Nie0TU6pedJVe3Rbtnx1LZGKsCgR 3UWGQUIzaJ7OxcklBMUWn7fPlxdparorG2WwlsLFRwCMm2G5mgjLmAYeYAlx7Gbsyw RRUNPwmUPPGkpnzexWaJCEngfbh7APJfmb6UK5KIcsF4L5vhwEGZYC+pWZ7H/nj54u 31xigk7JOMDWQIqZLxbjTJntUAL4NyInJXmO4Sfq99kOuj61EAETMl/XPE2tlmkuwL FOpzNxrEofHHlNBH8J++Iu59/ULOjDC9cB2+XykOHn0wKqgQr9tpNDK0O4+JHhqA0+ XNKZxIJQmrakA== From: Benjamin Tissoires Date: Tue, 23 Jan 2024 17:40:43 +0100 Subject: [PATCH 1/2] HID: bpf: remove double fdget() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240123-b4-hid-bpf-fixes-v1-1-aa1fac734377@kernel.org> References: <20240123-b4-hid-bpf-fixes-v1-0-aa1fac734377@kernel.org> In-Reply-To: <20240123-b4-hid-bpf-fixes-v1-0-aa1fac734377@kernel.org> To: Jiri Kosina , Benjamin Tissoires , Dan Carpenter , Daniel Borkmann Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Benjamin Tissoires , stable@vger.kernel.org X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1706028056; l=5900; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=sCdPQ4YhBeymXhJimO5RK/sYdEZSwG7jOkn2T00muOY=; b=UoeV+HyX1qYOXe+AaUpD4BFYfu0Tt4awtWhz3OIx81Ptoho+cQQotkpFYDNRpwX8+th9XWXbF xWxpxCcDuyfBJGvEouExda03CVKXnH8Rb441cYVvYdct0AOND9l5UHq X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788901728603237772 X-GMAIL-MSGID: 1788901728603237772 When the kfunc hid_bpf_attach_prog() is called, we called twice fdget(): one for fetching the type of the bpf program, and one for actually attaching the program to the device. The problem is that between those two calls, we have no guarantees that the prog_fd is still the same file descriptor for the given program. Solve this by calling bpf_prog_get() earlier, and use this to fetch the program type. Reported-by: Dan Carpenter Link: https://lore.kernel.org/bpf/CAO-hwJJ8vh8JD3-P43L-_CLNmPx0hWj44aom0O838vfP4=_1CA@mail.gmail.com/T/#t Cc: stable@vger.kernel.org Signed-off-by: Benjamin Tissoires --- drivers/hid/bpf/hid_bpf_dispatch.c | 66 ++++++++++++++++++++++++------------- drivers/hid/bpf/hid_bpf_dispatch.h | 4 +-- drivers/hid/bpf/hid_bpf_jmp_table.c | 20 ++--------- 3 files changed, 49 insertions(+), 41 deletions(-) diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c index d9ef45fcaeab..5111d1fef0d3 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.c +++ b/drivers/hid/bpf/hid_bpf_dispatch.c @@ -241,6 +241,39 @@ int hid_bpf_reconnect(struct hid_device *hdev) return 0; } +static int do_hid_bpf_attach_prog(struct hid_device *hdev, int prog_fd, struct bpf_prog *prog, + __u32 flags) +{ + int fd, err, prog_type; + + prog_type = hid_bpf_get_prog_attach_type(prog); + if (prog_type < 0) + return prog_type; + + if (prog_type >= HID_BPF_PROG_TYPE_MAX) + return -EINVAL; + + if (prog_type == HID_BPF_PROG_TYPE_DEVICE_EVENT) { + err = hid_bpf_allocate_event_data(hdev); + if (err) + return err; + } + + fd = __hid_bpf_attach_prog(hdev, prog_type, prog_fd, prog, flags); + if (fd < 0) + return fd; + + if (prog_type == HID_BPF_PROG_TYPE_RDESC_FIXUP) { + err = hid_bpf_reconnect(hdev); + if (err) { + close_fd(fd); + return err; + } + } + + return fd; +} + /** * hid_bpf_attach_prog - Attach the given @prog_fd to the given HID device * @@ -257,18 +290,13 @@ noinline int hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, __u32 flags) { struct hid_device *hdev; + struct bpf_prog *prog; struct device *dev; - int fd, err, prog_type = hid_bpf_get_prog_attach_type(prog_fd); + int fd; if (!hid_bpf_ops) return -EINVAL; - if (prog_type < 0) - return prog_type; - - if (prog_type >= HID_BPF_PROG_TYPE_MAX) - return -EINVAL; - if ((flags & ~HID_BPF_FLAG_MASK)) return -EINVAL; @@ -278,23 +306,17 @@ hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, __u32 flags) hdev = to_hid_device(dev); - if (prog_type == HID_BPF_PROG_TYPE_DEVICE_EVENT) { - err = hid_bpf_allocate_event_data(hdev); - if (err) - return err; - } + /* + * take a ref on the prog itself, it will be released + * on errors or when it'll be detached + */ + prog = bpf_prog_get(prog_fd); + if (IS_ERR(prog)) + return PTR_ERR(prog); - fd = __hid_bpf_attach_prog(hdev, prog_type, prog_fd, flags); + fd = do_hid_bpf_attach_prog(hdev, prog_fd, prog, flags); if (fd < 0) - return fd; - - if (prog_type == HID_BPF_PROG_TYPE_RDESC_FIXUP) { - err = hid_bpf_reconnect(hdev); - if (err) { - close_fd(fd); - return err; - } - } + bpf_prog_put(prog); return fd; } diff --git a/drivers/hid/bpf/hid_bpf_dispatch.h b/drivers/hid/bpf/hid_bpf_dispatch.h index 63dfc8605cd2..fbe0639d09f2 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.h +++ b/drivers/hid/bpf/hid_bpf_dispatch.h @@ -12,9 +12,9 @@ struct hid_bpf_ctx_kern { int hid_bpf_preload_skel(void); void hid_bpf_free_links_and_skel(void); -int hid_bpf_get_prog_attach_type(int prog_fd); +int hid_bpf_get_prog_attach_type(struct bpf_prog *prog); int __hid_bpf_attach_prog(struct hid_device *hdev, enum hid_bpf_prog_type prog_type, int prog_fd, - __u32 flags); + struct bpf_prog *prog, __u32 flags); void __hid_bpf_destroy_device(struct hid_device *hdev); int hid_bpf_prog_run(struct hid_device *hdev, enum hid_bpf_prog_type type, struct hid_bpf_ctx_kern *ctx_kern); diff --git a/drivers/hid/bpf/hid_bpf_jmp_table.c b/drivers/hid/bpf/hid_bpf_jmp_table.c index eca34b7372f9..12f7cebddd73 100644 --- a/drivers/hid/bpf/hid_bpf_jmp_table.c +++ b/drivers/hid/bpf/hid_bpf_jmp_table.c @@ -333,15 +333,10 @@ static int hid_bpf_insert_prog(int prog_fd, struct bpf_prog *prog) return err; } -int hid_bpf_get_prog_attach_type(int prog_fd) +int hid_bpf_get_prog_attach_type(struct bpf_prog *prog) { - struct bpf_prog *prog = NULL; - int i; int prog_type = HID_BPF_PROG_TYPE_UNDEF; - - prog = bpf_prog_get(prog_fd); - if (IS_ERR(prog)) - return PTR_ERR(prog); + int i; for (i = 0; i < HID_BPF_PROG_TYPE_MAX; i++) { if (hid_bpf_btf_ids[i] == prog->aux->attach_btf_id) { @@ -350,8 +345,6 @@ int hid_bpf_get_prog_attach_type(int prog_fd) } } - bpf_prog_put(prog); - return prog_type; } @@ -388,19 +381,13 @@ static const struct bpf_link_ops hid_bpf_link_lops = { /* called from syscall */ noinline int __hid_bpf_attach_prog(struct hid_device *hdev, enum hid_bpf_prog_type prog_type, - int prog_fd, __u32 flags) + int prog_fd, struct bpf_prog *prog, __u32 flags) { struct bpf_link_primer link_primer; struct hid_bpf_link *link; - struct bpf_prog *prog = NULL; struct hid_bpf_prog_entry *prog_entry; int cnt, err = -EINVAL, prog_table_idx = -1; - /* take a ref on the prog itself */ - prog = bpf_prog_get(prog_fd); - if (IS_ERR(prog)) - return PTR_ERR(prog); - mutex_lock(&hid_bpf_attach_lock); link = kzalloc(sizeof(*link), GFP_USER); @@ -467,7 +454,6 @@ __hid_bpf_attach_prog(struct hid_device *hdev, enum hid_bpf_prog_type prog_type, err_unlock: mutex_unlock(&hid_bpf_attach_lock); - bpf_prog_put(prog); kfree(link); return err; From patchwork Tue Jan 23 16:40:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 191093 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp475430dyi; Tue, 23 Jan 2024 09:07:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IGY6VO4QUWlk/iIL7noy9R1zC4f5JQG95huGMxgsvXz3FNxhkoUGxnl3ENCOmEHS6li2339 X-Received: by 2002:a05:6512:3b20:b0:50f:9ca:187e with SMTP id f32-20020a0565123b2000b0050f09ca187emr3194418lfv.100.1706029639915; Tue, 23 Jan 2024 09:07:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706029639; cv=pass; d=google.com; s=arc-20160816; b=q7r7NJXDskjG8DYCW7jKQpq+pBdbOaGHZpVlVlCcUcQbQI6Q0yVk1yMJD+MChraOjQ RmsW8Z5BhIxxuvTt0tnY5JafCyoiCKOYIyHAZ2bjYfezh7Qwi9bqxS6tT4vUI8efHFq8 vBbvJ7pWdSyvbtQWKvvS/cVTH2WGLQyEqmvFhTCmyPjjHEHkQD9tFrPu3meAsUKytjQZ jSP+r/OEJSNBbUPIJ6vmNZaJ8NKtfAiSl7D4pv0ioU4liAQcO/n/n8t+ah5/0fANmEHI WQYOz8F3d5IYJ2aV1+pXRU7i+Yvhs997ueg3T9P6oJkiOvMNITNoC1iRI+3ApwdK6XTW 8ssA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=TJEXGqSDxZVVXZGm0PyWWhJuh0DDp8fcvHzsCgYuhd8=; fh=i2t4Sb1GRO14SyJTVefqTyUif9vFoNbaO4e8Hmm6C8I=; b=hqlKpMm+gfmYOpcASkEHXnqvmctGGP8zAXldrjuX57NOzaPZGFOHQaxpxbyUe7B/MB t9bggOEer36FZorTjtGUpzo4TrzxkdyeACrBjNSmxf99RKNgQVkocfg4psea9GV6qcFy pxfw0vfLgjei2OUtpVOuDtjZ4c/f14DJPaB5PZKbhhPMa81ZxRZigRCDhiFUGxHwI08S 9Hfa0KnjZsT/XRLHXdOBqSn3uLv/Kuaf4g9VBa2RLMUYe8NnQvA8jpoTcHaAKwUs38O8 yhzWYua/ET8L8ALjY9nrIhinOfLT9T+2WKiBLti8xnYS/MZIJhOPw8e/bbSCC6zJqUZZ VuSQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Zfhb0bcm; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-35688-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35688-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id z20-20020a05640235d400b0055c58c93e75si1912971edc.133.2024.01.23.09.07.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 09:07:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35688-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Zfhb0bcm; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-35688-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35688-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id E9D651F22E90 for ; Tue, 23 Jan 2024 16:47:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5993285C50; Tue, 23 Jan 2024 16:41:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Zfhb0bcm" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 55E2F82D7C; Tue, 23 Jan 2024 16:41:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706028063; cv=none; b=EnG0X/8qw6dLaRzL/zEjVQ2SrAT+5IM5VpJ2yu4AGUVHOsYf8ZeGXTHeon8wKnESOsDq7GZtoHg2J1cNZZelY0F/EsVTTniFjjcXsQKTOYy+cUNqt4xbR/E7lo0r+WK0SWsW0hga7S7q6vlvXAFsJ7OsKC7uP3J3vToVy3SPW/I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706028063; c=relaxed/simple; bh=k8S+SH5DCWnhTkXa53WVFA61i4bptiV+YN7U96SThXg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eteccKHG5XfjUJCNJJobhcZc3N12WkfgXZNMbY9yKBXpudJk5AVXGB8cIjsrAUkxQvulXYfmSGSBzMumqI5nt59Jh613f8MkJESbhvC2R+KHbVtlgtpDK+Lj4H8g3f1FU+XCsOwfSjJdOavpp+zX66dgjBmKf+6qiTS1iQLr+nY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Zfhb0bcm; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D597C43394; Tue, 23 Jan 2024 16:41:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706028062; bh=k8S+SH5DCWnhTkXa53WVFA61i4bptiV+YN7U96SThXg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Zfhb0bcmmpNNXl1tRUjGGrIxhWExV75hs6steNvB46IBMMwsJtf/0gv+gnLnMfAov eNSonxBFI7udiXbyi88HCdnylw06odPVuK01sIRFLtjRlyuLYI4Z215ghLpAti5zxR FhxAb6DekkMMfB+loBiaqPC0zA72IkRBy3t0nmOqz1lrzdl+7dgZ/o6Q8FreAW5Vuq Cw/2lIPsKyGZ64Bn0vFWNaPsstOfbjTNeyyDkCXrBPnATOhpfQIWQxiBDLJezVcXIO 5gJlqc6c/hJGwXMPfCX4cOqmx6pjf4ktY6wpfgJa0EoqFtmXhlFC0k8eckAKgmWQjZ 1yjGX+9jjm27w== From: Benjamin Tissoires Date: Tue, 23 Jan 2024 17:40:44 +0100 Subject: [PATCH 2/2] HID: bpf: use __bpf_kfunc instead of noinline Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240123-b4-hid-bpf-fixes-v1-2-aa1fac734377@kernel.org> References: <20240123-b4-hid-bpf-fixes-v1-0-aa1fac734377@kernel.org> In-Reply-To: <20240123-b4-hid-bpf-fixes-v1-0-aa1fac734377@kernel.org> To: Jiri Kosina , Benjamin Tissoires , Dan Carpenter , Daniel Borkmann Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1706028056; l=4626; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=k8S+SH5DCWnhTkXa53WVFA61i4bptiV+YN7U96SThXg=; b=Inil0fQhKQFZtbu547ZVCCFw83S3vcuZ5g+odjPhLN7Fq//HjtK3YoCZ7xV6xbvHSaTQWy9wm 2navsiW7JaSAFcpU1GuMeRJEsJ2CYXKwNIBW4toGoRedOlB5xdtFvUM X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788901735560774575 X-GMAIL-MSGID: 1788901735560774575 Follow the docs at Documentation/bpf/kfuncs.rst: - declare the function with `__bpf_kfunc` - disables missing prototype warnings, which allows to remove them from include/linux/hid-bpf.h Removing the prototypes is not an issue because we currently have to redeclare them when writing the BPF program. They will eventually be generated by bpftool directly AFAIU. Signed-off-by: Benjamin Tissoires --- drivers/hid/bpf/hid_bpf_dispatch.c | 22 +++++++++++++++++----- include/linux/hid_bpf.h | 11 ----------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c index 5111d1fef0d3..119e4f03df55 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.c +++ b/drivers/hid/bpf/hid_bpf_dispatch.c @@ -143,6 +143,11 @@ u8 *call_hid_bpf_rdesc_fixup(struct hid_device *hdev, u8 *rdesc, unsigned int *s } EXPORT_SYMBOL_GPL(call_hid_bpf_rdesc_fixup); +/* Disables missing prototype warnings */ +__diag_push(); +__diag_ignore_all("-Wmissing-prototypes", + "Global kfuncs as their definitions will be in BTF"); + /** * hid_bpf_get_data - Get the kernel memory pointer associated with the context @ctx * @@ -152,7 +157,7 @@ EXPORT_SYMBOL_GPL(call_hid_bpf_rdesc_fixup); * * @returns %NULL on error, an %__u8 memory pointer on success */ -noinline __u8 * +__bpf_kfunc __u8 * hid_bpf_get_data(struct hid_bpf_ctx *ctx, unsigned int offset, const size_t rdwr_buf_size) { struct hid_bpf_ctx_kern *ctx_kern; @@ -167,6 +172,7 @@ hid_bpf_get_data(struct hid_bpf_ctx *ctx, unsigned int offset, const size_t rdwr return ctx_kern->data + offset; } +__diag_pop(); /* missing prototype warnings */ /* * The following set contains all functions we agree BPF programs @@ -274,6 +280,11 @@ static int do_hid_bpf_attach_prog(struct hid_device *hdev, int prog_fd, struct b return fd; } +/* Disables missing prototype warnings */ +__diag_push(); +__diag_ignore_all("-Wmissing-prototypes", + "Global kfuncs as their definitions will be in BTF"); + /** * hid_bpf_attach_prog - Attach the given @prog_fd to the given HID device * @@ -286,7 +297,7 @@ static int do_hid_bpf_attach_prog(struct hid_device *hdev, int prog_fd, struct b * is pinned to the BPF file system). */ /* called from syscall */ -noinline int +__bpf_kfunc int hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, __u32 flags) { struct hid_device *hdev; @@ -328,7 +339,7 @@ hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, __u32 flags) * * @returns A pointer to &struct hid_bpf_ctx on success, %NULL on error. */ -noinline struct hid_bpf_ctx * +__bpf_kfunc struct hid_bpf_ctx * hid_bpf_allocate_context(unsigned int hid_id) { struct hid_device *hdev; @@ -359,7 +370,7 @@ hid_bpf_allocate_context(unsigned int hid_id) * @ctx: the HID-BPF context to release * */ -noinline void +__bpf_kfunc void hid_bpf_release_context(struct hid_bpf_ctx *ctx) { struct hid_bpf_ctx_kern *ctx_kern; @@ -380,7 +391,7 @@ hid_bpf_release_context(struct hid_bpf_ctx *ctx) * * @returns %0 on success, a negative error code otherwise. */ -noinline int +__bpf_kfunc int hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, enum hid_report_type rtype, enum hid_class_request reqtype) { @@ -448,6 +459,7 @@ hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, kfree(dma_data); return ret; } +__diag_pop(); /* missing prototype warnings */ /* our HID-BPF entrypoints */ BTF_SET8_START(hid_bpf_fmodret_ids) diff --git a/include/linux/hid_bpf.h b/include/linux/hid_bpf.h index 840cd254172d..7118ac28d468 100644 --- a/include/linux/hid_bpf.h +++ b/include/linux/hid_bpf.h @@ -77,17 +77,6 @@ enum hid_bpf_attach_flags { int hid_bpf_device_event(struct hid_bpf_ctx *ctx); int hid_bpf_rdesc_fixup(struct hid_bpf_ctx *ctx); -/* Following functions are kfunc that we export to BPF programs */ -/* available everywhere in HID-BPF */ -__u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx, unsigned int offset, const size_t __sz); - -/* only available in syscall */ -int hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, __u32 flags); -int hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, - enum hid_report_type rtype, enum hid_class_request reqtype); -struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id); -void hid_bpf_release_context(struct hid_bpf_ctx *ctx); - /* * Below is HID internal */