Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp1118933lqt; Fri, 7 Jun 2024 08:29:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX00PsJpm+osAKUbjY1CDH9Mywc7zfdTYafcJ65ydvmZWILpYd5kwdTCBd30tTWNKo8QN5Znv/EM+0+2nJdMHHN06XuiRqalBnNvJ/E+Q== X-Google-Smtp-Source: AGHT+IFaq5jKZ3xepwBU6TfR7w97Mxq4me1vAHIheJ0OK73Vjmo7APRmAQGCzUP0K7U0K/lQDiFq X-Received: by 2002:a05:6870:a70d:b0:254:8dd7:5af5 with SMTP id 586e51a60fabf-2548dd7ccafmr37576fac.23.1717774167273; Fri, 07 Jun 2024 08:29:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717774167; cv=pass; d=google.com; s=arc-20160816; b=alnPOHvlSLMHmKcGRWL7JxKkKbYekR1xlqTkZmZ+ilbjmPBdQvCpwRXyQLFJumMP11 H9BogxO6YzMC2qehfX4w/SEBXsoXKVPUl9NgD9JwrbV4gqP2OWyqj4yQUcT/X7/PypfC +B2KsZEsdLOhN89doCUhSh0JsNYC3U9WNV1+mQxBagq/UNqaW7z2racBsldUIXw1ZvA3 I5K0dZ++JETYtA6dduOZspgevPgDdQ/MB5Lv01Cu7+4wh40dbfitARpzG+wdi7EaV1TJ c82oXDNji/Y76BrySZUQkwPtoWzEznpAH3BfHcRsuTsox0GI/5IArM9vneBSDNONJEwT GkJQ== 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=/KW/QNZgSTjLcx7wrr+KldJQUITpZKRzAk/XhJ6kk2g=; fh=YpHnV/6uyiPbG6kMterX8knOBa279otQWiSdSsr99Bc=; b=tZvaTI38XpKgcryut5WmziE1Ldrr4E3LfCCcM2B+16FCAVjNLMsBboGyb3SM/kGBuY aSisJCqmaSZtvK9z0Uor6si4o99L1oONlNe4g1Mwag2LKPmjRD1WqoJmudq9YavlBNAt 7gW9YO9kV/7CRo0kNescpmG4W5V7Il/mRPSHW4W8/ziLh8qLyEHE51v7IMxVOt9HHqFL mdPttMjLzMV8pP9swfg7cYmudc8ef4NUFz9k1B8YxDxgRK++ML2fki5LuflaqxZg+9oa W/TlA+a2JHMynw2TJsabK1hVxCj2KEQi3BxebekSy0Vl6WnGxOXDtzcleAOmMQy5iXGg 59Ug==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Km3XZlQD; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-206393-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206393-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-79532854065si94083085a.49.2024.06.07.08.29.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 08:29:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-206393-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Km3XZlQD; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-206393-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206393-linux.lists.archive=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id E39A01C21C05 for ; Fri, 7 Jun 2024 15:29:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3025219A29A; Fri, 7 Jun 2024 15:28:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Km3XZlQD" 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 44DDC199E98; Fri, 7 Jun 2024 15:28:36 +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=1717774117; cv=none; b=BbCWImmRFo9N8HfkWkNSXBxpY4FOT7SWsAs23lfTn5PpIWlSdLSkhaKFBuRnGl8mep6iHjFnrmAg/AddFvqFA5xwwyJrbxLiApRW7/qwPQBmxR9TSl8QNCppHv/X3/71bEmRYfzHlzDXQ+Fyx4ZELk3dQNsYAkOaqsnY3jvxdyE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717774117; c=relaxed/simple; bh=/R7kYdkB8bgtpMVTXS+2jlG8Z27/TD1w+IJxULW99sM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GEdf+hsPU96UUIwZhZ9tSBcvIiZn+91VQCH2SLG7Otfb6IKDTF6cL5w7nGYj51E+DOSH3PZ8PWsRqxG97lzyRLWOKgLU5gt/icR2HnZQy17WfhidvYlOSNFD698y7/Ov4ikY+cHIhwu58JSz6YKYSe7lWxJSEPz8W7Y4c4f+hIQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Km3XZlQD; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E9B5CC4AF07; Fri, 7 Jun 2024 15:28:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717774116; bh=/R7kYdkB8bgtpMVTXS+2jlG8Z27/TD1w+IJxULW99sM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Km3XZlQD5bFY059Zy/DQc6YvK6QaIC1qBmRVjP6npae1yV/hXa1nxob9eZxcmSAHU TjEHvAtm7nqRCo3zcYH1F+Ln4pNDkc37M2L2YGlI+ACuOstgEDHEX0UhUW2v6yKQUd j34tv2SXrgF4ULYLc4RzC8p+/WLfAl8j/Re7BztOvqCR3759jTlQ1MgdTgXcU5+WNf TF41L/RJURsqLVLjVDCcdmEeTwuR2CInAKx999T2p2bVf5FzYEO1mDBMGGU9GaeT+x ruCXpuoMlVvFfUqpdSZY53CDdwCCbo+dEUo8uO1cRkPpIYVh0sbCbp8iAeCRZj0kqY fUwEsqBXCT9DA== From: Benjamin Tissoires Date: Fri, 07 Jun 2024 17:28:22 +0200 Subject: [PATCH HID v2 02/16] HID: bpf: add hid_get/put_device() helpers Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240607-hid_bpf_struct_ops-v2-2-3f95f4d02292@kernel.org> References: <20240607-hid_bpf_struct_ops-v2-0-3f95f4d02292@kernel.org> In-Reply-To: <20240607-hid_bpf_struct_ops-v2-0-3f95f4d02292@kernel.org> To: Shuah Khan , Jiri Kosina , Jonathan Corbet , Alexei Starovoitov Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1717774109; l=2722; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=/R7kYdkB8bgtpMVTXS+2jlG8Z27/TD1w+IJxULW99sM=; b=GVk2LoaP8ORGRh4w4xTOl6sBH9nMzRxXJHN0EOT7odpSU3zjIBkTQcF/8h1aQKUR0c4i15HR1 RjLFfdkSCeOAsTo48QiCgPBCXREXvEtSMC3jH+F1d0jFiiP7sONC4tJ X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= no code change, but this way we reduce code duplication and we can export it later. Signed-off-by: Benjamin Tissoires --- no changes in v2 --- drivers/hid/bpf/hid_bpf_dispatch.c | 47 ++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c index 55c9f82fdef0..c8bb79ce2354 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.c +++ b/drivers/hid/bpf/hid_bpf_dispatch.c @@ -150,6 +150,25 @@ static int device_match_id(struct device *dev, const void *id) return hdev->id == *(int *)id; } +static struct hid_device *hid_get_device(unsigned int hid_id) +{ + struct device *dev; + + if (!hid_ops) + return ERR_PTR(-EINVAL); + + dev = bus_find_device(hid_ops->bus_type, NULL, &hid_id, device_match_id); + if (!dev) + return ERR_PTR(-EINVAL); + + return to_hid_device(dev); +} + +static void hid_put_device(struct hid_device *hid) +{ + put_device(&hid->dev); +} + static int __hid_bpf_allocate_data(struct hid_device *hdev, u8 **data, u32 *size) { u8 *alloc_data; @@ -281,20 +300,14 @@ 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 err, fd; - if (!hid_ops) - return -EINVAL; - if ((flags & ~HID_BPF_FLAG_MASK)) return -EINVAL; - dev = bus_find_device(hid_ops->bus_type, NULL, &hid_id, device_match_id); - if (!dev) - return -EINVAL; - - hdev = to_hid_device(dev); + hdev = hid_get_device(hid_id); + if (IS_ERR(hdev)) + return PTR_ERR(hdev); /* * take a ref on the prog itself, it will be released @@ -317,7 +330,7 @@ hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, __u32 flags) out_prog_put: bpf_prog_put(prog); out_dev_put: - put_device(dev); + hid_put_device(hdev); return err; } @@ -333,20 +346,14 @@ hid_bpf_allocate_context(unsigned int hid_id) { struct hid_device *hdev; struct hid_bpf_ctx_kern *ctx_kern = NULL; - struct device *dev; - - if (!hid_ops) - return NULL; - dev = bus_find_device(hid_ops->bus_type, NULL, &hid_id, device_match_id); - if (!dev) + hdev = hid_get_device(hid_id); + if (IS_ERR(hdev)) return NULL; - hdev = to_hid_device(dev); - ctx_kern = kzalloc(sizeof(*ctx_kern), GFP_KERNEL); if (!ctx_kern) { - put_device(dev); + hid_put_device(hdev); return NULL; } @@ -373,7 +380,7 @@ hid_bpf_release_context(struct hid_bpf_ctx *ctx) kfree(ctx_kern); /* get_device() is called by bus_find_device() */ - put_device(&hid->dev); + hid_put_device(hid); } static int -- 2.44.0