Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp1530587lqt; Sat, 8 Jun 2024 02:02:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVebNbzRDiZl61I6LaJyV2Ee6JnaLN9Z+hDGIocqyC3HK3Yp5bdyBCfn/MpJJghRHLmcOZxaO1CrY6IC1o9iQDrC3SgSGNWsk12H+crUA== X-Google-Smtp-Source: AGHT+IH8iuYzNNmkqVU8rU5HCJAsaS8278t1v85wuePeL/ikqykvYSeNh1SiJ6XbUNK6ClIpsXqi X-Received: by 2002:a9d:6a84:0:b0:6f0:e4e9:af19 with SMTP id 46e09a7af769-6f957290227mr4646997a34.20.1717837333032; Sat, 08 Jun 2024 02:02:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717837333; cv=pass; d=google.com; s=arc-20160816; b=rXusaucmhsTZM1Xk6h47aNATtGxX96d3ZiLtvsK+NG3cVzLldFKkoqMeCqAkRlXffm 0JjY3+ATk4Uni7dCp/VFH0ew7mpJByUZqYMZ4tC9nXF3+c8Qxd0RDRytbp6ByaMuE/0t cWiZWhfB28nhfbp6/xLZRmUF4mOpWQmz+2ueV620dsg9CaQM2T2m9RAeyKhU3FMkIBxK xg1C565wufGXmbV5b0w3+EjtR33AQRHHD8wb2e/mZqDoQl97OyaTlWzrfK09rHbiFYky QhnpYZUOnxBl4bZUL9SOFMNqD43AKKA05Dti+h393yQfK3w14QWEz6DWiiIVv09/7UKC NEow== 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=am5EJ0jqgS7yNXr4vHoxCsxx4/ju2x/E2noO03+exfo=; fh=YpHnV/6uyiPbG6kMterX8knOBa279otQWiSdSsr99Bc=; b=W+ypPwHQENknukWggux8QnHcKBJ83woxuPddBoF5xQ0SdAG5R+19bvhCBX0Cvrn9DC mg2t7Q80YBQIUVU7lv+u4tebUig9XgFgPssEw9sFCJAtfbaFI6J8thitzOgOMRHNgsL3 qB9BWFmMIPTFy26848AzzPRrqJdtMbT1dEFNfBYWPDFMXaMxWgQhCApidveGvZ5gc7X2 iIVZEyMUDkhtYz1FVSlPHxoTGuBirfuYH9j8AYBbR6gL08xuWKwAaqL9VX0eX9i6vecE gctL11RcJrfTF2/FV+aW/sfZjwj7BlEVanzhWkBPSuQGaI/tas6pkTlFrjPfks4zEu7r s2iA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dXnmsuem; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-206966-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206966-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-79558be283csi39405085a.291.2024.06.08.02.02.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Jun 2024 02:02:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-206966-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=dXnmsuem; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-206966-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206966-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 B2C391C21763 for ; Sat, 8 Jun 2024 09:02:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 96D6417839A; Sat, 8 Jun 2024 09:01:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dXnmsuem" 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 AB7C3178371; Sat, 8 Jun 2024 09:01:26 +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=1717837286; cv=none; b=gvfYFOZpZ7pkIOfB0AjT15xgrxp9bVxKSuSlOwDP/wOBOaVB1AmCC5PK16gBHkgQIO6iIaDxsUtHPOfWp+VhzFXLitqsVmUeLgH5+ogNdu8BgZmUWXGunm1E0pbfFxmXHj9k9I0HrfQkNSwAZsfJ3K2wHsrhAGnhxdYMDNOw/U8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717837286; c=relaxed/simple; bh=NoLrJd5sEhcFvxDx8M3VYx3BFl64wagoJzWAatOgIx8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=en+2f085fsKLNne2OXTzXTgx40m4ftvHfIXHhtaj25TCNLSO3gexUcnPkR5WnaMIgEaTmfZ5RDWiy9EiNeb9uD1HagmzFrYgtJI4Bq5IiB8FXqA9/YN6yrja95TMHJV3O/Mjm5fazZMusL/XKd1lfrKiMOItHvnRGbSXgKlrmWc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dXnmsuem; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC037C4AF0A; Sat, 8 Jun 2024 09:01:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717837286; bh=NoLrJd5sEhcFvxDx8M3VYx3BFl64wagoJzWAatOgIx8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=dXnmsuemeQxjAA4XasfeL7d4V576vK9yxYH4lD2T57IacO1Ev49h49Lq328En9EWO 2nbAm1ejPl+9ypiKZLFWZE/RLp7s8FHCHiWzSxqRqHC7YEWmerf0zMST++cYHfwo1k xd09XnJ5pzgiBu0lBbLwUiokW/MKd2kcWsjrzPhp2qqMZVzEqYZevN3n8uZB1xoP80 UU6oqR1fcnz4+bbUpfp5QXe21TtmefMapoQp2/efNSb3Uf5weVgDebhsEmPax2R2VL wil9b48L2O3gNz9vpwhZTeqHSSPhX6zo1Gsq66PElrS5xWVDoYVdzhXRiIa0MCk8Iz kjQlHrMxTfEFQ== From: Benjamin Tissoires Date: Sat, 08 Jun 2024 11:01:14 +0200 Subject: [PATCH HID v3 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: <20240608-hid_bpf_struct_ops-v3-2-6ac6ade58329@kernel.org> References: <20240608-hid_bpf_struct_ops-v3-0-6ac6ade58329@kernel.org> In-Reply-To: <20240608-hid_bpf_struct_ops-v3-0-6ac6ade58329@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=1717837279; l=2742; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=NoLrJd5sEhcFvxDx8M3VYx3BFl64wagoJzWAatOgIx8=; b=wOSu1fuRpEKlVNqrOkUqe6GzMxDRCQHI/7fp9HQi+CGJSRCuXY6YUIaVXEA31aabzlvHb0mc2 h65LP1FKmGpBflAOxnsNtjZWHPQ6U1VGePku2hbfX+/pFxFxsz4iTeR 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 v3 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