From patchwork Tue Sep 10 14:43:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 827159 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 CD30B199952; Tue, 10 Sep 2024 14:43:52 +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=1725979432; cv=none; b=b1VKqM6QRTTEd7DR5vPig2DHb2fteHzr/w6rLgLyI5SC21kyZlONmuFf6MPCMl55rUNy93tuUI7tylgaOIiHBQCcpXcPtA1E4ZXPFU5bwhdnPmgz3AdOy6DxJZPXEXi7qJpeLw46RLLLCZPVdPDjZLLleadiRvhuimeVJg2EIVo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725979432; c=relaxed/simple; bh=w2GuvfapADWngezjPqhaRKjXMs8AxDtj7AKQPZiO04o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=SRjnyZ9/n9lDbncTZmCjft4YGsSROHk4HB/1h1UVzaEHih3ohQWVUrO66CkAeR4zR+djGKLjCAQGK7nypQ8lilA7YEex9F0MiowtpAdeLcghdSpgspceKD7lx8idwsYXRyHspLDevCmjv2JO14tKLWSjUmqqA1eZdiD/uUazvKQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EYOlfhS2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EYOlfhS2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A845DC4CEC4; Tue, 10 Sep 2024 14:43:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725979432; bh=w2GuvfapADWngezjPqhaRKjXMs8AxDtj7AKQPZiO04o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=EYOlfhS2sa+fLm8PS0MOo5NQJ7R20BEKib5MJhHATx65nQs54O1eIN/2bKh3/qHKd qXZ5LneshTX8P4KhgRdOGaTp9ph+y/CRMQ0r9btqf2sUpY06b882Z929BhB2pkTC4p BAahp9O7GqQoUSihQOtl3NAJ/ZYlaqC81rIt2uNlHidTWBBDbPoqGynD652PUWTdLA C2o28083hoczGWII9SsZ1x1ofJko7lRZmDvixuCRdQKKgE/ItqlgEczLbY6Mu5rNf3 xHjA+FS9Ptsj3HZx8cGbsu/ZnaUPB8QCGvoEAV83jMcmFWdjkdiR1a13VJ27aFQKjQ /FAlZse1O0m+Q== From: Benjamin Tissoires Date: Tue, 10 Sep 2024 23:43:37 +0900 Subject: [PATCH HID v2 01/11] HID: bpf: move HID-BPF report descriptor fixup earlier Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240910-hid-bpf-hid-generic-v2-1-083dfc189e97@kernel.org> References: <20240910-hid-bpf-hid-generic-v2-0-083dfc189e97@kernel.org> In-Reply-To: <20240910-hid-bpf-hid-generic-v2-0-083dfc189e97@kernel.org> To: Jiri Kosina , Peter Hutterer , Vicki Pfau , Shuah Khan Cc: linux-input@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1725979428; l=7588; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=w2GuvfapADWngezjPqhaRKjXMs8AxDtj7AKQPZiO04o=; b=agec0rI0QU0hVLvpvSXvLP9nPKm6u6c+g2P0Ec0M9MCueLCNyStIERaq4dGic+T4JCsiNwtXq Bl473uUW1I/B4rMfAuh4gszoLuHMCUW3Sn8RZejb1pgj0o5NyvN0TNb X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= Currently, hid_bpf_rdesc_fixup() is called once the match between the HID device and the driver is done. This can be problematic in case the driver selected by the kernel would change the report descriptor after the fact. To give a chance for hid_bpf_rdesc_fixup() to provide hints on to how to select a dedicated driver or not, move the call to that BPF hook earlier in the .probe() process, when we get the first match. However, this means that we might get called more than once (typically once for hid-generic, and once for hid-vendor-specific). So we store the result of HID-BPF fixup in struct hid_device. If that new value is NULL, we need to use ->dev_rdesc. In order to not grow struct hid_device, some fields are re-ordered. This was the output of pahole for the first 128 bytes: struct hid_device { __u8 * dev_rdesc; /* 0 8 */ unsigned int dev_rsize; /* 8 4 */ /* XXX 4 bytes hole, try to pack */ __u8 * rdesc; /* 16 8 */ unsigned int rsize; /* 24 4 */ /* XXX 4 bytes hole, try to pack */ struct hid_collection * collection; /* 32 8 */ unsigned int collection_size; /* 40 4 */ unsigned int maxcollection; /* 44 4 */ unsigned int maxapplication; /* 48 4 */ __u16 bus; /* 52 2 */ __u16 group; /* 54 2 */ __u32 vendor; /* 56 4 */ __u32 product; /* 60 4 */ /* --- cacheline 1 boundary (64 bytes) --- */ __u32 version; /* 64 4 */ enum hid_type type; /* 68 4 */ unsigned int country; /* 72 4 */ /* XXX 4 bytes hole, try to pack */ struct hid_report_enum report_enum[3]; /* 80 6216 */ Basically, we got three holes of 4 bytes. We can reorder things a little and makes those 3 holes a continuous 12 bytes hole, which can be replaced by the new pointer and the new unsigned int we need. In terms of code allocation, when not using HID-BPF, we are back to kernel v6.2 in hid_open_report(). These multiple kmemdup() calls will be fixed in a later commit. Signed-off-by: Benjamin Tissoires --- new in v2 --- drivers/hid/bpf/hid_bpf_dispatch.c | 8 ++++++-- drivers/hid/hid-core.c | 17 +++++++++++++---- include/linux/hid.h | 18 ++++++++++-------- include/linux/hid_bpf.h | 9 ++------- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c index 8420c227e21b..58839c125fba 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.c +++ b/drivers/hid/bpf/hid_bpf_dispatch.c @@ -183,7 +183,7 @@ u8 *call_hid_bpf_rdesc_fixup(struct hid_device *hdev, const u8 *rdesc, unsigned ignore_bpf: kfree(ctx_kern.data); - return kmemdup(rdesc, *size, GFP_KERNEL); + return NULL; } EXPORT_SYMBOL_GPL(call_hid_bpf_rdesc_fixup); @@ -260,8 +260,12 @@ int hid_bpf_allocate_event_data(struct hid_device *hdev) int hid_bpf_reconnect(struct hid_device *hdev) { - if (!test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status)) + if (!test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status)) { + kfree(hdev->bpf_rdesc); + hdev->bpf_rdesc = NULL; + hdev->bpf_rsize = 0; return device_reprobe(&hdev->dev); + } return 0; } diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 311069588321..a5f5415571cb 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -708,6 +708,7 @@ void hiddev_free(struct kref *ref) hid_close_report(hid); kfree(hid->dev_rdesc); + kfree(hid->bpf_rdesc); kfree(hid); } @@ -1221,13 +1222,12 @@ int hid_open_report(struct hid_device *device) if (WARN_ON(device->status & HID_STAT_PARSED)) return -EBUSY; - start = device->dev_rdesc; + start = device->bpf_rdesc ? device->bpf_rdesc : device->dev_rdesc; + size = device->bpf_rsize; if (WARN_ON(!start)) return -ENODEV; - size = device->dev_rsize; - /* call_hid_bpf_rdesc_fixup() ensures we work on a copy of rdesc */ - buf = call_hid_bpf_rdesc_fixup(device, start, &size); + buf = kmemdup(start, size, GFP_KERNEL); if (buf == NULL) return -ENOMEM; @@ -2659,6 +2659,12 @@ static int __hid_device_probe(struct hid_device *hdev, struct hid_driver *hdrv) const struct hid_device_id *id; int ret; + if (!hdev->bpf_rsize) { + hdev->bpf_rsize = hdev->dev_rsize; + hdev->bpf_rdesc = call_hid_bpf_rdesc_fixup(hdev, hdev->dev_rdesc, + &hdev->bpf_rsize); + } + if (!hid_check_device_match(hdev, hdrv, &id)) return -ENODEV; @@ -2916,8 +2922,11 @@ static void hid_remove_device(struct hid_device *hdev) hdev->status &= ~HID_STAT_ADDED; } kfree(hdev->dev_rdesc); + kfree(hdev->bpf_rdesc); hdev->dev_rdesc = NULL; + hdev->bpf_rdesc = NULL; hdev->dev_rsize = 0; + hdev->bpf_rsize = 0; } /** diff --git a/include/linux/hid.h b/include/linux/hid.h index 3a998fa3812d..d41fa18f1e03 100644 --- a/include/linux/hid.h +++ b/include/linux/hid.h @@ -599,15 +599,17 @@ enum hid_battery_status { struct hid_driver; struct hid_ll_driver; -struct hid_device { /* device report descriptor */ - const __u8 *dev_rdesc; - unsigned dev_rsize; - const __u8 *rdesc; - unsigned rsize; +struct hid_device { + const __u8 *dev_rdesc; /* device report descriptor */ + const __u8 *bpf_rdesc; /* bpf modified report descriptor, if any */ + const __u8 *rdesc; /* currently used report descriptor */ + unsigned int dev_rsize; + unsigned int bpf_rsize; + unsigned int rsize; + unsigned int collection_size; /* Number of allocated hid_collections */ struct hid_collection *collection; /* List of HID collections */ - unsigned collection_size; /* Number of allocated hid_collections */ - unsigned maxcollection; /* Number of parsed collections */ - unsigned maxapplication; /* Number of applications */ + unsigned int maxcollection; /* Number of parsed collections */ + unsigned int maxapplication; /* Number of applications */ __u16 bus; /* BUS ID */ __u16 group; /* Report group */ __u32 vendor; /* Vendor ID */ diff --git a/include/linux/hid_bpf.h b/include/linux/hid_bpf.h index 6a47223e6460..0290833544c8 100644 --- a/include/linux/hid_bpf.h +++ b/include/linux/hid_bpf.h @@ -228,13 +228,8 @@ static inline int hid_bpf_connect_device(struct hid_device *hdev) { return 0; } static inline void hid_bpf_disconnect_device(struct hid_device *hdev) {} static inline void hid_bpf_destroy_device(struct hid_device *hid) {} static inline int hid_bpf_device_init(struct hid_device *hid) { return 0; } -/* - * This specialized allocator has to be a macro for its allocations to be - * accounted separately (to have a separate alloc_tag). The typecast is - * intentional to enforce typesafety. - */ -#define call_hid_bpf_rdesc_fixup(_hdev, _rdesc, _size) \ - ((u8 *)kmemdup(_rdesc, *(_size), GFP_KERNEL)) +static inline u8 *call_hid_bpf_rdesc_fixup(struct hid_device *hdev, u8 *rdesc, + unsigned int *size) { return NULL; } #endif /* CONFIG_HID_BPF */ From patchwork Tue Sep 10 14:43:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 827158 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 C98D719ABAB; Tue, 10 Sep 2024 14:43:56 +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=1725979436; cv=none; b=t/qWAgPtahClTxpBPDn70TgtmRgzwNfnrpTnUG3ktInh7W7aeMLT9phzbNpsMT2CSdmbUzLF/K8pVMjj6rzGvRyoKHF+DwJWoTkiezpOwTuJBnm74iUfAclh/aPcX4FOB1jfrzRjbEPf59hbZz1Y8/3wjgir4ypEzNot309nCHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725979436; c=relaxed/simple; bh=8ktPfzBt6ih236F5v0U2BPIw8M4SjLXm76j3JdVJj8M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Z15ooOlKrl9j2RtFshu8SgVBvMhEDam7oTtordfsSDyR3tgH2MdETnJZtcHX4smAEGZfmbMDe6kyBOWRLL+2D8RR0anJVCuog4eVgRm4JPHmeIOURUnPOygyCq/v1NxWborsYBbrCKJF/XPSrkElGQwTxqagWFt7h5PkcK+rx88= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ONK6tnE8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ONK6tnE8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB431C4CED0; Tue, 10 Sep 2024 14:43:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725979436; bh=8ktPfzBt6ih236F5v0U2BPIw8M4SjLXm76j3JdVJj8M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ONK6tnE8nqgbJIU2yJ5xJbPAkSq1Wtim7WQn4Eks/QBzEyGuSkXqjrBqxkZH1gNqr 2hfNs0ZABZl9eK4o0Uazikq3iEc7oSZ61z37wEjy+9U5C9Qya9J/CskVVrKN5D9Lct 0yG1j7WsbV7mQbdjPQg4AYK+oNOwAiKzd0YlZwiAfHlqFDQH4TWOY+dhnEOQXID8yr 2Out4bg8r7bCUvw8YA7M32mVOGvruI4Lf2Vd4fhphnfNM3NKETj9MfzfLJ5AVaxuJa Jo3Y50QdHYtWCtG3UTkVZF/8yiRHjC87YG7z0Lg+VnyyImEq9OknySrQkOGfA1ifgq pEYrLLAOtBQCg== From: Benjamin Tissoires Date: Tue, 10 Sep 2024 23:43:39 +0900 Subject: [PATCH HID v2 03/11] HID: core: remove one more kmemdup on .probe() Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240910-hid-bpf-hid-generic-v2-3-083dfc189e97@kernel.org> References: <20240910-hid-bpf-hid-generic-v2-0-083dfc189e97@kernel.org> In-Reply-To: <20240910-hid-bpf-hid-generic-v2-0-083dfc189e97@kernel.org> To: Jiri Kosina , Peter Hutterer , Vicki Pfau , Shuah Khan Cc: linux-input@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1725979428; l=2284; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=8ktPfzBt6ih236F5v0U2BPIw8M4SjLXm76j3JdVJj8M=; b=zXJ1whlSNOnhHWGHdrRFTCSgVa1H7tFvYXlnGwJ1z+gWdtW5BeQMnAr8dm3L21tdJjrtVMNpQ a1sV3MgyzGICy+rElYyIhvYUjZbBRr2SpdwmSh7Uu+VvuDdrtvC9AVK X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= That last kmemdup while opening the report descriptor was required to have a common kfree() on it. Move that kmemdup in the only special case it's required (if there is a .report_fixup()), and add a more elaborated check before freeing hdev->rdesc, to avoid a double free. Signed-off-by: Benjamin Tissoires --- new in v2 --- drivers/hid/hid-core.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 172746a082f9..2b70a12a2135 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -685,7 +685,14 @@ static void hid_close_report(struct hid_device *device) INIT_LIST_HEAD(&report_enum->report_list); } - kfree(device->rdesc); + /* + * If the HID driver had a rdesc_fixup() callback, dev->rdesc + * will be allocated by hid-core and needs to be freed. + * Otherwise, it is either equal to dev_rdesc or bpf_rdesc, in + * which cases it'll be freed later on device removal or destroy. + */ + if (device->rdesc != device->dev_rdesc && device->rdesc != device->bpf_rdesc) + kfree(device->rdesc); device->rdesc = NULL; device->rsize = 0; @@ -1206,7 +1213,6 @@ int hid_open_report(struct hid_device *device) struct hid_item item; unsigned int size; const __u8 *start; - __u8 *buf = NULL; const __u8 *end; const __u8 *next; int ret; @@ -1233,17 +1239,23 @@ int hid_open_report(struct hid_device *device) * on a copy of our report descriptor so it can * change it. */ - buf = kmemdup(start, size, GFP_KERNEL); + __u8 *buf = kmemdup(start, size, GFP_KERNEL); + if (buf == NULL) return -ENOMEM; start = device->driver->report_fixup(device, buf, &size); - } - start = kmemdup(start, size, GFP_KERNEL); - kfree(buf); - if (start == NULL) - return -ENOMEM; + /* + * The second kmemdup is required in case report_fixup() returns + * a static read-only memory, but we have no idea if that memory + * needs to be cleaned up or not at the end. + */ + start = kmemdup(start, size, GFP_KERNEL); + kfree(buf); + if (start == NULL) + return -ENOMEM; + } device->rdesc = start; device->rsize = size; From patchwork Tue Sep 10 14:43:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 827157 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 6D5F719FA93; Tue, 10 Sep 2024 14:44: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=1725979441; cv=none; b=Qg5w1h1bUlEwqWKNlGFCRtn6H/WirFNHl+a1d8n1SbdQ6gCqkC7wz1yFdM5SQ8QfqIiKCFTfMO434WE1wbsB+51JB2sJTYh45gfz4zKbXaUqXUReifs/Yh02SdiPITVYE9K5UWuTXn3FHoUz97m6ARVVfUYVJevqIhIOhsy+EFI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725979441; c=relaxed/simple; bh=PcylA1+94LG9KARyIPuGccijQnN2vj003tX21Zw9Tbo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VPe0DbDq25cSRCBkmF7DIbTeEJe81l2jrFIA9qZWj+tpn1IDQ1lm+wX0BnhegCUuCZ/kDSEzjsHNr24OJt8NRFxWmVVGWdus1+fNzRNV9lVj631DEEQbqGLuMZxmwTaW9FgOueG4XLv4Q0vvfCD5L31Rei+UcKrQBNqPhe0zVWI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lOe/CVQH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lOe/CVQH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 39E6EC4CEC4; Tue, 10 Sep 2024 14:43:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725979440; bh=PcylA1+94LG9KARyIPuGccijQnN2vj003tX21Zw9Tbo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=lOe/CVQHP+un+q+BINAwM4P5krDbalcG0y04Hm1hFLFHKN6GoXHy2/6onoOBiKHe9 ouJZ5UWBDk6mpkKa3QSzC6FgveU/I2c1DERinKyaZOUbVIjM8jwLAGxQUAQih/U0/P /8VSs8pwli+Dm+VOP1nLJULx3rqxe3FZ4jG/2WQJKfNYnXLI1DVQfde3yg+x6SrFzI 08uwDp9LA59pQhYMUqhZl5B3xJXsNJ0IeXRTrlygUleGP030pAqErTukaS/pJfMfIe LQ2upPEh4+cTOEe9ivEUe6vi6PVjhLtZEKPFI+ONdJbYCcHgGNwbKXOQI9s5RhKhKs AtoyRNv3hsw0A== From: Benjamin Tissoires Date: Tue, 10 Sep 2024 23:43:41 +0900 Subject: [PATCH HID v2 05/11] selftests/hid: add dependency on hid_common.h Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240910-hid-bpf-hid-generic-v2-5-083dfc189e97@kernel.org> References: <20240910-hid-bpf-hid-generic-v2-0-083dfc189e97@kernel.org> In-Reply-To: <20240910-hid-bpf-hid-generic-v2-0-083dfc189e97@kernel.org> To: Jiri Kosina , Peter Hutterer , Vicki Pfau , Shuah Khan Cc: linux-input@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1725979428; l=854; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=PcylA1+94LG9KARyIPuGccijQnN2vj003tX21Zw9Tbo=; b=tpMw5MpnEKcNrunsr9z9pW2Db9YDiTxivD3VnNkIADJv6IoDbXWXHMgu+Bb/uG073eo4skWMK eVswxdpd6ZaBwrihlr1d2LQ2kddOvMnB1Hf6b2tlQC3CKNuSu0xNtx3 X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= Allows to recompile the C tests when that file changes Signed-off-by: Benjamin Tissoires --- no changes in v2 --- tools/testing/selftests/hid/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/hid/Makefile b/tools/testing/selftests/hid/Makefile index 72be55ac4bdf..9399fa3f2f9d 100644 --- a/tools/testing/selftests/hid/Makefile +++ b/tools/testing/selftests/hid/Makefile @@ -229,7 +229,7 @@ $(BPF_SKELS): %.skel.h: %.bpf.o $(BPFTOOL) | $(OUTPUT) $(Q)$(BPFTOOL) gen object $(<:.o=.linked1.o) $< $(Q)$(BPFTOOL) gen skeleton $(<:.o=.linked1.o) name $(notdir $(<:.bpf.o=)) > $@ -$(OUTPUT)/%.o: %.c $(BPF_SKELS) +$(OUTPUT)/%.o: %.c $(BPF_SKELS) hid_common.h $(call msg,CC,,$@) $(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@ From patchwork Tue Sep 10 14:43: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: 827156 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 7C8E01A08DB; Tue, 10 Sep 2024 14:44:05 +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=1725979445; cv=none; b=rxOOtu2HDDlchasWsS+Y4isvXluIWfS7ZqUcmA6Y1AV+DEXexXlmdH5wM32HGtzixWNkY18iNuJH/eGjst+LeBz67nXWwvSINKQjgVRUIrVC0jC/DvBUYMZXWRy/It9FwqBxZ01Y3nXwaBab67nyw6S0Z3tH4r8HsJ5RxInzLmM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725979445; c=relaxed/simple; bh=JfbqgBGrbPRf4Q+i81ZCJ1xRNQLnuvbrzRkeCT0GBRk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YBLhe7KW1HxMjzZAbacaC+lnGw1apmikCnA+MJDVy6EOjFt3vnrbPq34L7Af7DaV9HfQcvIXfbsDoTXGZU9m+DucFWH6SYi/kNn735zGE89XAmlteXdzl9t0h439vQJw9YNUUoYowiMq7HUkb/78VOzUSQzvFVKbHofIhS0aJ6A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PaTE2ETl; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PaTE2ETl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A5FA8C4CEC4; Tue, 10 Sep 2024 14:44:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725979445; bh=JfbqgBGrbPRf4Q+i81ZCJ1xRNQLnuvbrzRkeCT0GBRk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=PaTE2ETlZNbgSY5WDwpH4EqdZrHAUaF95gfVOO/LIZNoojsXeQftZgywGzJeoroxT JPg2FUrmnqdKwsy4q6XZB2CsPNz/Tvme5V+TFUFtWKKYrJ4rAwEA9yFTJJ/n7pVf0L H7nBk0C9bWK8ImLKXFdtb1P8N+ywQFms7JToCURgjUmvVZHMduHpcj7bnhtR2cqLgP o4hNGk4URMagdO4ZzWJQNlaWbFaOsv66cH7b02IKHsUyneWbG4KsiskHhY2guv9A+B JL59djHRER5KfDbNPdUN29aqKuTWJiIxDgGg6CwmJYocEgI4MByR8L2JzuL5WiHerK HpxnRGBW7RVIw== From: Benjamin Tissoires Date: Tue, 10 Sep 2024 23:43:43 +0900 Subject: [PATCH HID v2 07/11] selftests/hid: allow to parametrize bus/vid/pid/rdesc on the test device Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240910-hid-bpf-hid-generic-v2-7-083dfc189e97@kernel.org> References: <20240910-hid-bpf-hid-generic-v2-0-083dfc189e97@kernel.org> In-Reply-To: <20240910-hid-bpf-hid-generic-v2-0-083dfc189e97@kernel.org> To: Jiri Kosina , Peter Hutterer , Vicki Pfau , Shuah Khan Cc: linux-input@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1725979428; l=6751; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=JfbqgBGrbPRf4Q+i81ZCJ1xRNQLnuvbrzRkeCT0GBRk=; b=xVAcS4cnlpjQsJDWLeRz+OzmlDzB3mJIKDEfA28Jxs9i4Blua3zu46Ez68CuposqGojXj8lFr i0AaiosyEqlA/eLDTdsQaxcIRmCkxzuZMPlHWfar/mjara7QNlEQQRI X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= This will be useful to introduce variants in tests to test the interactions between HID-BPF and some kernel modules. Signed-off-by: Benjamin Tissoires --- no changes in v2 --- tools/testing/selftests/hid/hid_bpf.c | 2 +- tools/testing/selftests/hid/hid_common.h | 46 ++++++++++++++++++++------------ tools/testing/selftests/hid/hidraw.c | 2 +- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c index 17ccbf5ff4b5..7eb15da62bdc 100644 --- a/tools/testing/selftests/hid/hid_bpf.c +++ b/tools/testing/selftests/hid/hid_bpf.c @@ -58,7 +58,7 @@ FIXTURE_SETUP(hid_bpf) { int err; - err = setup_uhid(_metadata, &self->hid); + err = setup_uhid(_metadata, &self->hid, BUS_USB, 0x0001, 0x0a36, rdesc, sizeof(rdesc)); ASSERT_OK(err); } diff --git a/tools/testing/selftests/hid/hid_common.h b/tools/testing/selftests/hid/hid_common.h index a7d836a35bb1..f77f69c6657d 100644 --- a/tools/testing/selftests/hid/hid_common.h +++ b/tools/testing/selftests/hid/hid_common.h @@ -23,6 +23,9 @@ struct uhid_device { int dev_id; /* uniq (random) number to identify the device */ int uhid_fd; int hid_id; /* HID device id in the system */ + __u16 bus; + __u32 vid; + __u32 pid; pthread_t tid; /* thread for reading uhid events */ }; @@ -129,7 +132,9 @@ static int uhid_write(struct __test_metadata *_metadata, int fd, const struct uh } } -static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb) +static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb, + __u16 bus, __u32 vid, __u32 pid, __u8 *rdesc, + size_t rdesc_size) { struct uhid_event ev; char buf[25]; @@ -140,10 +145,10 @@ static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb) ev.type = UHID_CREATE; strcpy((char *)ev.u.create.name, buf); ev.u.create.rd_data = rdesc; - ev.u.create.rd_size = sizeof(rdesc); - ev.u.create.bus = BUS_USB; - ev.u.create.vendor = 0x0001; - ev.u.create.product = 0x0a37; + ev.u.create.rd_size = rdesc_size; + ev.u.create.bus = bus; + ev.u.create.vendor = vid; + ev.u.create.product = pid; ev.u.create.version = 0; ev.u.create.country = 0; @@ -305,15 +310,17 @@ static int uhid_send_event(struct __test_metadata *_metadata, struct uhid_device return uhid_write(_metadata, hid->uhid_fd, &ev); } -static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *dir) +static bool match_sysfs_device(struct uhid_device *hid, const char *workdir, struct dirent *dir) { - const char *target = "0003:0001:0A37.*"; + char target[20] = ""; char phys[512]; char uevent[1024]; char temp[512]; int fd, nread; bool found = false; + snprintf(target, sizeof(target), "%04X:%04X:%04X.*", hid->bus, hid->vid, hid->pid); + if (fnmatch(target, dir->d_name, 0)) return false; @@ -324,7 +331,7 @@ static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *d if (fd < 0) return false; - sprintf(phys, "PHYS=%d", dev_id); + sprintf(phys, "PHYS=%d", hid->dev_id); nread = read(fd, temp, ARRAY_SIZE(temp)); if (nread > 0 && (strstr(temp, phys)) != NULL) @@ -335,7 +342,7 @@ static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *d return found; } -static int get_hid_id(int dev_id) +static int get_hid_id(struct uhid_device *hid) { const char *workdir = "/sys/devices/virtual/misc/uhid"; const char *str_id; @@ -350,10 +357,10 @@ static int get_hid_id(int dev_id) d = opendir(workdir); if (d) { while ((dir = readdir(d)) != NULL) { - if (!match_sysfs_device(dev_id, workdir, dir)) + if (!match_sysfs_device(hid, workdir, dir)) continue; - str_id = dir->d_name + sizeof("0003:0001:0A37."); + str_id = dir->d_name + sizeof("0000:0000:0000."); found = (int)strtol(str_id, NULL, 16); break; @@ -367,7 +374,7 @@ static int get_hid_id(int dev_id) return found; } -static int get_hidraw(int dev_id) +static int get_hidraw(struct uhid_device *hid) { const char *workdir = "/sys/devices/virtual/misc/uhid"; char sysfs[1024]; @@ -384,7 +391,7 @@ static int get_hidraw(int dev_id) continue; while ((dir = readdir(d)) != NULL) { - if (!match_sysfs_device(dev_id, workdir, dir)) + if (!match_sysfs_device(hid, workdir, dir)) continue; sprintf(sysfs, "%s/%s/hidraw", workdir, dir->d_name); @@ -416,7 +423,7 @@ static int open_hidraw(struct uhid_device *hid) int hidraw_number; char hidraw_path[64] = { 0 }; - hidraw_number = get_hidraw(hid->dev_id); + hidraw_number = get_hidraw(hid); if (hidraw_number < 0) return hidraw_number; @@ -425,7 +432,8 @@ static int open_hidraw(struct uhid_device *hid) return open(hidraw_path, O_RDWR | O_NONBLOCK); } -static int setup_uhid(struct __test_metadata *_metadata, struct uhid_device *hid) +static int setup_uhid(struct __test_metadata *_metadata, struct uhid_device *hid, + __u16 bus, __u32 vid, __u32 pid, const __u8 *rdesc, size_t rdesc_size) { const char *path = "/dev/uhid"; time_t t; @@ -435,11 +443,15 @@ static int setup_uhid(struct __test_metadata *_metadata, struct uhid_device *hid srand((unsigned int)time(&t)); hid->dev_id = rand() % 1024; + hid->bus = bus; + hid->vid = vid; + hid->pid = pid; hid->uhid_fd = open(path, O_RDWR | O_CLOEXEC); ASSERT_GE(hid->uhid_fd, 0) TH_LOG("open uhid-cdev failed; %d", hid->uhid_fd); - ret = uhid_create(_metadata, hid->uhid_fd, hid->dev_id); + ret = uhid_create(_metadata, hid->uhid_fd, hid->dev_id, bus, vid, pid, + (__u8 *)rdesc, rdesc_size); ASSERT_EQ(0, ret) { TH_LOG("create uhid device failed: %d", ret); close(hid->uhid_fd); @@ -447,7 +459,7 @@ static int setup_uhid(struct __test_metadata *_metadata, struct uhid_device *hid } /* locate the uevent file of the created device */ - hid->hid_id = get_hid_id(hid->dev_id); + hid->hid_id = get_hid_id(hid); ASSERT_GT(hid->hid_id, 0) TH_LOG("Could not locate uhid device id: %d", hid->hid_id); diff --git a/tools/testing/selftests/hid/hidraw.c b/tools/testing/selftests/hid/hidraw.c index 5934818b2036..821db37ba4bb 100644 --- a/tools/testing/selftests/hid/hidraw.c +++ b/tools/testing/selftests/hid/hidraw.c @@ -36,7 +36,7 @@ FIXTURE_SETUP(hidraw) { int err; - err = setup_uhid(_metadata, &self->hid); + err = setup_uhid(_metadata, &self->hid, BUS_USB, 0x0001, 0x0a37, rdesc, sizeof(rdesc)); ASSERT_OK(err); self->hidraw_fd = open_hidraw(&self->hid); From patchwork Tue Sep 10 14:43:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 827155 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 BBCFC1A2C25; Tue, 10 Sep 2024 14:44:09 +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=1725979449; cv=none; b=J7HDGu1V23WhP2sAY6N32nMK1Z7jepoXYRitP6+clGBDM386RP69J438GHknl/P5zhFP86yd6HqHymyl+scm1BrFhcOVRGIEKWDcXFPAPWSpa31sddFowrrQxHEIL7jBIoXmp4q2gJWKWjX1bK2zvXM4j56ccBDLQCuGKvWx9S0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725979449; c=relaxed/simple; bh=RC0OcZWXYp36gijPOdDycz5WVWZVh9ovwtf2TjypL1c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GZ4auBadlcqAkvw7MsJSnExDYzTm6XNFlTD2xdFJGZ7SSGJx1SSNBFyqAxM6aUlLYy28yQyie5VNHsYOowZfuggiCUGmAeK3CHxhKeVnqyFwVpvQuEBpV3w3A4no0Y8IjMmle2zyB+DQmpecpVkRlpdQt/geXx0vOK0uoE7k9zY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Trrra9ZH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Trrra9ZH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E6B05C4CECD; Tue, 10 Sep 2024 14:44:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725979449; bh=RC0OcZWXYp36gijPOdDycz5WVWZVh9ovwtf2TjypL1c=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Trrra9ZH6To3Y9ufxjR1LwEemwbKI6KZqedfrsWBthdBig+b/poPOc4OavedR+0C9 n1xXHKnTy8tRCYi9RF7aSXy+I+/VrczFojJLArgf9KkAvSlc+Ig2ewgpOF1H2ej3nN sj50c2u4o34qi8kz2cMp5ommctEM7SvGN0eb5jAoCXmv+8Q8Ul470JrhM3+c/5LAlF zZTkEqWOTAOe6sC2GNccg0AhOFrj06MGcjnODMdNcKVJU28d7B+3MY9ESHeQC4Dpc1 JysRVZLAae4t79awU4cqmtaJeINfNjfB83OPxqoOM2fz/plbEwBUV5DoLDghVHSzRt h//7usybeD25Q== From: Benjamin Tissoires Date: Tue, 10 Sep 2024 23:43:45 +0900 Subject: [PATCH HID v2 09/11] selftests/hid: add test for assigning a given device to hid-generic Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240910-hid-bpf-hid-generic-v2-9-083dfc189e97@kernel.org> References: <20240910-hid-bpf-hid-generic-v2-0-083dfc189e97@kernel.org> In-Reply-To: <20240910-hid-bpf-hid-generic-v2-0-083dfc189e97@kernel.org> To: Jiri Kosina , Peter Hutterer , Vicki Pfau , Shuah Khan Cc: linux-input@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1725979428; l=4691; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=RC0OcZWXYp36gijPOdDycz5WVWZVh9ovwtf2TjypL1c=; b=K1T3X/S3FcETPm5Afe9U7NGYx0EE4aQ22NbRrEZ7Ug/ASedd5wDVzOy44sw68LpR1PRdGols/ usgMKApzaUyDH+xCDisOMdXZobf/jZu5BtdE3a/4Mm/jSJwy/r9b6X4 X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= We use a well known VID/PID on a driver that doesn't need to talk to the device, ensures we created the device against the target driver, then load our program and ensure we have unbound to this driver and use hid-generic instead. Signed-off-by: Benjamin Tissoires --- changes in v2: - amended for the new API --- tools/testing/selftests/hid/hid_bpf.c | 80 +++++++++++++++++++++- tools/testing/selftests/hid/progs/hid.c | 12 ++++ .../testing/selftests/hid/progs/hid_bpf_helpers.h | 6 +- 3 files changed, 96 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c index 7eb15da62bdc..edc061b38528 100644 --- a/tools/testing/selftests/hid/hid_bpf.c +++ b/tools/testing/selftests/hid/hid_bpf.c @@ -54,11 +54,41 @@ FIXTURE_TEARDOWN(hid_bpf) { hid_bpf_teardown(_metadata, self, variant); \ } while (0) +struct specific_device { + const char test_name[64]; + __u16 bus; + __u32 vid; + __u32 pid; +}; + FIXTURE_SETUP(hid_bpf) { + const struct specific_device *match = NULL; int err; - err = setup_uhid(_metadata, &self->hid, BUS_USB, 0x0001, 0x0a36, rdesc, sizeof(rdesc)); + const struct specific_device devices[] = { + { + .test_name = "test_hid_driver_probe", + .bus = BUS_BLUETOOTH, + .vid = 0x05ac, /* USB_VENDOR_ID_APPLE */ + .pid = 0x022c, /* USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI */ + }, { + .test_name = "*", + .bus = BUS_USB, + .vid = 0x0001, + .pid = 0x0a36, + }}; + + for (int i = 0; i < ARRAY_SIZE(devices); i++) { + match = &devices[i]; + if (!strncmp(_metadata->name, devices[i].test_name, sizeof(devices[i].test_name))) + break; + } + + ASSERT_OK_PTR(match); + + err = setup_uhid(_metadata, &self->hid, match->bus, match->vid, match->pid, + rdesc, sizeof(rdesc)); ASSERT_OK(err); } @@ -855,6 +885,54 @@ TEST_F(hid_bpf, test_hid_attach_flags) ASSERT_EQ(buf[3], 3); } +static bool is_using_driver(struct __test_metadata *_metadata, struct uhid_device *hid, + const char *driver) +{ + char driver_line[512]; + char uevent[1024]; + char temp[512]; + int fd, nread; + bool found = false; + + sprintf(uevent, "/sys/bus/hid/devices/%04X:%04X:%04X.%04X/uevent", + hid->bus, hid->vid, hid->pid, hid->hid_id); + + fd = open(uevent, O_RDONLY | O_NONBLOCK); + if (fd < 0) { + TH_LOG("couldn't open '%s': %d, %d", uevent, fd, errno); + return false; + } + + sprintf(driver_line, "DRIVER=%s", driver); + + nread = read(fd, temp, ARRAY_SIZE(temp)); + if (nread > 0 && (strstr(temp, driver_line)) != NULL) + found = true; + + close(fd); + + return found; +} + +/* + * Attach hid_driver_probe to the given uhid device, + * check that the device is now using hid-generic. + */ +TEST_F(hid_bpf, test_hid_driver_probe) +{ + const struct test_program progs[] = { + { + .name = "hid_test_driver_probe", + }, + }; + + ASSERT_TRUE(is_using_driver(_metadata, &self->hid, "apple")); + + LOAD_PROGRAMS(progs); + + ASSERT_TRUE(is_using_driver(_metadata, &self->hid, "hid-generic")); +} + /* * Attach hid_rdesc_fixup to the given uhid device, * retrieve and open the matching hidraw node, diff --git a/tools/testing/selftests/hid/progs/hid.c b/tools/testing/selftests/hid/progs/hid.c index 5ecc845ef792..9b22e9a0e658 100644 --- a/tools/testing/selftests/hid/progs/hid.c +++ b/tools/testing/selftests/hid/progs/hid.c @@ -598,3 +598,15 @@ SEC(".struct_ops.link") struct hid_bpf_ops test_infinite_loop_input_report = { .hid_device_event = (void *)hid_test_infinite_loop_input_report, }; + +SEC("?struct_ops.s/hid_rdesc_fixup") +int BPF_PROG(hid_test_driver_probe, struct hid_bpf_ctx *hid_ctx) +{ + hid_ctx->hid->quirks |= HID_QUIRK_IGNORE_SPECIAL_DRIVER; + return 0; +} + +SEC(".struct_ops.link") +struct hid_bpf_ops test_driver_probe = { + .hid_rdesc_fixup = (void *)hid_test_driver_probe, +}; diff --git a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h index e5db897586bb..1a645684a117 100644 --- a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h +++ b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h @@ -84,10 +84,14 @@ struct hid_bpf_ops { struct hid_device *hdev; }; +#define BIT(n) (1U << n) + #ifndef BPF_F_BEFORE -#define BPF_F_BEFORE (1U << 3) +#define BPF_F_BEFORE BIT(3) #endif +#define HID_QUIRK_IGNORE_SPECIAL_DRIVER BIT(22) + /* following are kfuncs exported by HID for HID-BPF */ extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx, unsigned int offset, From patchwork Tue Sep 10 14:43:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 827154 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 5CE371A38F9; Tue, 10 Sep 2024 14:44:14 +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=1725979454; cv=none; b=qb3lleWqEeSihxYgvx3ocQlOKGEVuwWIO58yDdurvEF1juVeeNoGGGFlNgUIB63DtzKaTduhj+6JweAyK+pX8KIAbiKYT/3N62tAWI5TaFF+Mj5ccp4EH7Iz7fH6NZ8uMV0PW1tsh/VClbduSBk9jEcur+LqOUUb0+yVPdvVawo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725979454; c=relaxed/simple; bh=LmBG5BHGYdWXIOucMdHZKe5DqBhFcqK07cXblfY/l0Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rGPjdzdkS3RXHs3L3wc36SQsbeWpb5oY/ptTnwa6NO5ABskypcqMF8oarKUHaW8OfVjDEiQCp8ehWp+ibNY8+XHCHjLPdzV7wJ8VTpwmf34hO8DTvvPtfOPqO9FyixTjFaiwNdLHekxSaqmkw3HkgZwy8RUHs4d1x7MfNU8LqXA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=d1a24Ert; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="d1a24Ert" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 34A45C4CECE; Tue, 10 Sep 2024 14:44:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725979453; bh=LmBG5BHGYdWXIOucMdHZKe5DqBhFcqK07cXblfY/l0Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=d1a24ErtDm70VPJ1/SWrFe5tAMR7WPpUaynaU3xdzRJTFV6sMH/ZMS2vVQVQ5ybcm XKSbbcnmgj7TU6/AKRlHMD2reEwlnm35yz5Hoo78ogb05GU+YWQZYO4O6pw8uIfkqj 3CpU2eVxPebHErmS4Wr79PocLsi3ugvrBl7mKq0u8U4wzQpc3cclViAhEcypGaydvp MbI7w+7erasZTqeKKwRJ0Bq1DpUF9QhqtWKa9Bgu5bDki1q6jIsIXa/NHtN+ervRu/ B21zyh92Tlv+1p4aMJiRmo1eNfDuTQkH882m7B8bfE1tcM8jYdc/nCE5q81cwYojM5 klx6jykHzcasg== From: Benjamin Tissoires Date: Tue, 10 Sep 2024 23:43:47 +0900 Subject: [PATCH HID v2 11/11] selftests/hid: add test to disable hid-input Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240910-hid-bpf-hid-generic-v2-11-083dfc189e97@kernel.org> References: <20240910-hid-bpf-hid-generic-v2-0-083dfc189e97@kernel.org> In-Reply-To: <20240910-hid-bpf-hid-generic-v2-0-083dfc189e97@kernel.org> To: Jiri Kosina , Peter Hutterer , Vicki Pfau , Shuah Khan Cc: linux-input@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1725979428; l=5547; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=LmBG5BHGYdWXIOucMdHZKe5DqBhFcqK07cXblfY/l0Y=; b=0EYf1JyMDXPcGWcDKNKveACXh9M/f/oJ1DKRuDRxRsjir8szlhhgA+1DLiD8hRQ+kSHCgpOMe 56E2fUVaOkdAO24gPrhGHVcCacPQRl0wHVtYn6ZvXktAWdBnt9hJer/ X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= Add a test for the newly enabled feature to control the connect_mask of hid-generic. Signed-off-by: Benjamin Tissoires --- changes in v2: - amended for the new API --- tools/testing/selftests/hid/hid_bpf.c | 60 +++++++++++++++++++++- tools/testing/selftests/hid/progs/hid.c | 1 + .../testing/selftests/hid/progs/hid_bpf_helpers.h | 1 + 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c index edc061b38528..41cacc30ef8b 100644 --- a/tools/testing/selftests/hid/hid_bpf.c +++ b/tools/testing/selftests/hid/hid_bpf.c @@ -4,6 +4,38 @@ #include "hid_common.h" #include +static const __u8 mouse_rdesc[] = { + 0x05, 0x01, /* .Usage Page (Generic Desktop) 0 */ + 0x09, 0x02, /* .Usage (Mouse) 2 */ + 0xa1, 0x01, /* .Collection (Application) 4 */ + 0x09, 0x02, /* ..Usage (Mouse) 6 */ + 0xa1, 0x02, /* ..Collection (Logical) 8 */ + 0x09, 0x01, /* ...Usage (Pointer) 10 */ + 0xa1, 0x00, /* ...Collection (Physical) 12 */ + 0x05, 0x09, /* ....Usage Page (Button) 14 */ + 0x19, 0x01, /* ....Usage Minimum (1) 16 */ + 0x29, 0x03, /* ....Usage Maximum (3) 18 */ + 0x15, 0x00, /* ....Logical Minimum (0) 20 */ + 0x25, 0x01, /* ....Logical Maximum (1) 22 */ + 0x75, 0x01, /* ....Report Size (1) 24 */ + 0x95, 0x03, /* ....Report Count (3) 26 */ + 0x81, 0x02, /* ....Input (Data,Var,Abs) 28 */ + 0x75, 0x05, /* ....Report Size (5) 30 */ + 0x95, 0x01, /* ....Report Count (1) 32 */ + 0x81, 0x03, /* ....Input (Cnst,Var,Abs) 34 */ + 0x05, 0x01, /* ....Usage Page (Generic Desktop) 36 */ + 0x09, 0x30, /* ....Usage (X) 38 */ + 0x09, 0x31, /* ....Usage (Y) 40 */ + 0x15, 0x81, /* ....Logical Minimum (-127) 42 */ + 0x25, 0x7f, /* ....Logical Maximum (127) 44 */ + 0x75, 0x08, /* ....Report Size (8) 46 */ + 0x95, 0x02, /* ....Report Count (2) 48 */ + 0x81, 0x06, /* ....Input (Data,Var,Rel) 50 */ + 0xc0, /* ...End Collection 52 */ + 0xc0, /* ..End Collection 53 */ + 0xc0, /* .End Collection 54 */ +}; + struct hid_hw_request_syscall_args { __u8 data[10]; unsigned int hid; @@ -59,6 +91,8 @@ struct specific_device { __u16 bus; __u32 vid; __u32 pid; + const __u8 *rdesc; + const size_t rdesc_size; }; FIXTURE_SETUP(hid_bpf) @@ -72,11 +106,15 @@ FIXTURE_SETUP(hid_bpf) .bus = BUS_BLUETOOTH, .vid = 0x05ac, /* USB_VENDOR_ID_APPLE */ .pid = 0x022c, /* USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI */ + .rdesc = mouse_rdesc, + .rdesc_size = sizeof(mouse_rdesc), }, { .test_name = "*", .bus = BUS_USB, .vid = 0x0001, .pid = 0x0a36, + .rdesc = rdesc, + .rdesc_size = sizeof(rdesc), }}; for (int i = 0; i < ARRAY_SIZE(devices); i++) { @@ -88,7 +126,7 @@ FIXTURE_SETUP(hid_bpf) ASSERT_OK_PTR(match); err = setup_uhid(_metadata, &self->hid, match->bus, match->vid, match->pid, - rdesc, sizeof(rdesc)); + match->rdesc, match->rdesc_size); ASSERT_OK(err); } @@ -914,6 +952,24 @@ static bool is_using_driver(struct __test_metadata *_metadata, struct uhid_devic return found; } +static bool has_hid_input(struct uhid_device *hid) +{ + char input[1024]; + DIR *d; + + sprintf(input, "/sys/bus/hid/devices/%04X:%04X:%04X.%04X/input", + hid->bus, hid->vid, hid->pid, hid->hid_id); + + d = opendir(input); + if (d) { + closedir(d); + + return true; + } + + return false; +} + /* * Attach hid_driver_probe to the given uhid device, * check that the device is now using hid-generic. @@ -927,10 +983,12 @@ TEST_F(hid_bpf, test_hid_driver_probe) }; ASSERT_TRUE(is_using_driver(_metadata, &self->hid, "apple")); + ASSERT_TRUE(has_hid_input(&self->hid)) TH_LOG("input node not found"); LOAD_PROGRAMS(progs); ASSERT_TRUE(is_using_driver(_metadata, &self->hid, "hid-generic")); + ASSERT_FALSE(has_hid_input(&self->hid)) TH_LOG("input node unexpectly found"); } /* diff --git a/tools/testing/selftests/hid/progs/hid.c b/tools/testing/selftests/hid/progs/hid.c index 9b22e9a0e658..7e24bec8ef43 100644 --- a/tools/testing/selftests/hid/progs/hid.c +++ b/tools/testing/selftests/hid/progs/hid.c @@ -603,6 +603,7 @@ SEC("?struct_ops.s/hid_rdesc_fixup") int BPF_PROG(hid_test_driver_probe, struct hid_bpf_ctx *hid_ctx) { hid_ctx->hid->quirks |= HID_QUIRK_IGNORE_SPECIAL_DRIVER; + hid_ctx->hid->quirks |= HID_QUIRK_IGNORE_HIDINPUT; return 0; } diff --git a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h index 1a645684a117..9c3cfd61d992 100644 --- a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h +++ b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h @@ -91,6 +91,7 @@ struct hid_bpf_ops { #endif #define HID_QUIRK_IGNORE_SPECIAL_DRIVER BIT(22) +#define HID_QUIRK_IGNORE_HIDINPUT BIT(23) /* following are kfuncs exported by HID for HID-BPF */ extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,