From patchwork Fri Mar 15 14:44:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 13593562 X-Patchwork-Delegate: jikos@jikos.cz 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 99DFE1BDE0; Fri, 15 Mar 2024 14:44:50 +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=1710513890; cv=none; b=hR9YHdbUuZ09oKtTUnSaNBLEtg4VGclo1DEu6u8IyTR6/IY8fXNgZ+5o6BgQk3Ycxi+V2mz58oN9ZJtnmUFOhhWXnVh3jRaQNCc+obdUaigl+jzB0sCngf/LPsSHC8IXUBuGJQzZP7CQajxQ6+hg+rWYMsgLJKNvV2/nu4CBzIk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710513890; c=relaxed/simple; bh=oLZvjVXr7zNxl0tYVVqGxzqOMyHkWY4EukDLBuqjtGw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=c6LS6MEM4vR/G49m7zRklPiLm9cuBTKUrQ+cK+JKo/b8Lz0L5ltOAO4Z8MyVoINoRqMhXgaxH61FbZFuG5V4pWLuo8L2ooenEWvxhDbAGHKepgcGm90E4a4Pzftx2mf1xZ3qOZSL8yNSHuk1rdKY13FE7rL9aC1PVf5T9dYwl9Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CQZgPncf; 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="CQZgPncf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6C57CC43399; Fri, 15 Mar 2024 14:44:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710513890; bh=oLZvjVXr7zNxl0tYVVqGxzqOMyHkWY4EukDLBuqjtGw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=CQZgPncfGTG/5mpBofStzL0F7GrEfhlqrhzzu3wdI73bQWSoEOGI8rtOK2tP69Ecb /dvjQSRSNMfuJoENEpB/EiWP+pXrw6d8fil9PGozEsLfFdTYa91Q3Jh9NFcj9NEXdc ojUF1Hf3xLXrxFApJhZ8EB/CS6SDlD83tvKKCvaPbeszLQLjxAawmFTq2lZV9EWqE5 4A6ohzkAMyBsO8VXyIRoYl+9Hr9Qx1N6+ci0i3tGelWIREaqNUmDD9/yJJ/NVe5J+Q fIJrVXPhoP08PmXR/Sewi5CCml8BL03rDOJAL64EuihQSbTdGoBBLmZ0ePOSZlWo0u KyBoG3ww6C8VQ== From: Benjamin Tissoires Date: Fri, 15 Mar 2024 15:44:38 +0100 Subject: [PATCH v4 1/7] HID: bpf/dispatch: regroup kfuncs definitions Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240315-b4-hid-bpf-new-funcs-v4-1-079c282469d3@kernel.org> References: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> In-Reply-To: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> To: Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: Benjamin Tissoires , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1710513885; l=3541; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=oLZvjVXr7zNxl0tYVVqGxzqOMyHkWY4EukDLBuqjtGw=; b=/ra8+t0jpst1C0YN+qdKGFMxcgj5YhI9W4lmREI9BNtYg+4l0B24KuWZodmipU96Q2SwV3Q6+ ltel/lxtfV2A0OyTGDD1H1V6nFHwuqmXQteFsfrKYM3+zDYWoh5maXr X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= No code change, just move down the hid_bpf_get_data() kfunc definition so we have only one block of __bpf_kfunc_start/end_defs() Signed-off-by: Benjamin Tissoires --- no changes in v4 no changes in v3 no changes in v2 --- drivers/hid/bpf/hid_bpf_dispatch.c | 80 ++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c index e630caf644e8..52abb27426f4 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.c +++ b/drivers/hid/bpf/hid_bpf_dispatch.c @@ -143,48 +143,6 @@ 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 */ -__bpf_kfunc_start_defs(); - -/** - * hid_bpf_get_data - Get the kernel memory pointer associated with the context @ctx - * - * @ctx: The HID-BPF context - * @offset: The offset within the memory - * @rdwr_buf_size: the const size of the buffer - * - * @returns %NULL on error, an %__u8 memory pointer on success - */ -__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; - - if (!ctx) - return NULL; - - ctx_kern = container_of(ctx, struct hid_bpf_ctx_kern, ctx); - - if (rdwr_buf_size + offset > ctx->allocated_size) - return NULL; - - return ctx_kern->data + offset; -} -__bpf_kfunc_end_defs(); - -/* - * The following set contains all functions we agree BPF programs - * can use. - */ -BTF_KFUNCS_START(hid_bpf_kfunc_ids) -BTF_ID_FLAGS(func, hid_bpf_get_data, KF_RET_NULL) -BTF_KFUNCS_END(hid_bpf_kfunc_ids) - -static const struct btf_kfunc_id_set hid_bpf_kfunc_set = { - .owner = THIS_MODULE, - .set = &hid_bpf_kfunc_ids, -}; - static int device_match_id(struct device *dev, const void *id) { struct hid_device *hdev = to_hid_device(dev); @@ -281,6 +239,31 @@ static int do_hid_bpf_attach_prog(struct hid_device *hdev, int prog_fd, struct b /* Disables missing prototype warnings */ __bpf_kfunc_start_defs(); +/** + * hid_bpf_get_data - Get the kernel memory pointer associated with the context @ctx + * + * @ctx: The HID-BPF context + * @offset: The offset within the memory + * @rdwr_buf_size: the const size of the buffer + * + * @returns %NULL on error, an %__u8 memory pointer on success + */ +__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; + + if (!ctx) + return NULL; + + ctx_kern = container_of(ctx, struct hid_bpf_ctx_kern, ctx); + + if (rdwr_buf_size + offset > ctx->allocated_size) + return NULL; + + return ctx_kern->data + offset; +} + /** * hid_bpf_attach_prog - Attach the given @prog_fd to the given HID device * @@ -474,6 +457,19 @@ hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, } __bpf_kfunc_end_defs(); +/* + * The following set contains all functions we agree BPF programs + * can use. + */ +BTF_KFUNCS_START(hid_bpf_kfunc_ids) +BTF_ID_FLAGS(func, hid_bpf_get_data, KF_RET_NULL) +BTF_KFUNCS_END(hid_bpf_kfunc_ids) + +static const struct btf_kfunc_id_set hid_bpf_kfunc_set = { + .owner = THIS_MODULE, + .set = &hid_bpf_kfunc_ids, +}; + /* our HID-BPF entrypoints */ BTF_SET8_START(hid_bpf_fmodret_ids) BTF_ID_FLAGS(func, hid_bpf_device_event) From patchwork Fri Mar 15 14:44: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: 13593563 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 150243D0CA; Fri, 15 Mar 2024 14:44:53 +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=1710513893; cv=none; b=bNC9HHatI7sGuFvDsTbYpCS1sNdNr+xtGRgu3KpDuWuzerl6lW6yNdxZX/QnUjeARZERQJE/kDhktz+2pNECyoZ2+S885X8PgRg1Inwj0EWoCGpyhOXNgALDy3EL6+C2v2yyf3sDSr2l7+C5fFlK45dIfA0mkjmJA/xEatkC1nY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710513893; c=relaxed/simple; bh=zq66QKtjnXJ4Qer6GMlak1OP/mCYAo+o7m/hoYuei0o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ss2HjfHokVXmNTpKklr1AxZcUPVWAD2X/6LIYxwneiUI4NSpf8g4kEQAkgcLn3jBhpEv9AQCcP2YblLJ3w5vQIynr7/EUqdeDB4YR93t6oT44f0NpeMH1TnR6mS26JWMYzbMwCyEgiyohPlqOWR4gMfL8aZbIPPGIk3NazhMrbk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=J34MSr1p; 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="J34MSr1p" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DD460C32784; Fri, 15 Mar 2024 14:44:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710513892; bh=zq66QKtjnXJ4Qer6GMlak1OP/mCYAo+o7m/hoYuei0o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=J34MSr1pP1mHZVnp9Lcni2ceQKxT/azQc7edzymyCKZWwb+iyL9g/IbNuRZ9cRSTr 4uvW1zWQjyTYBZ45BGwAKxjrfI10xQ3DAxmWzqKMUR62TvZnaKstIBYShyWqfLsQKL sAVdgf5FIhvy9rD/P+4aNl/ch97Qx6uSYJ5YDYUcvmw4d0D0Xwir6i8yXscoo5g+8c 83a54CSetuS7vQEzkGtyDT2/SjdoROVowWQEPFdfV4s2q5CXfjcDtPBnrMfu98p+NK RZDL7gjEtHj0X/napdKvs3rdz2jRuhOUNqQDeOICcPFi96Vk/tplNnchsFPvYYSoCx ZC4mAAUJF8CAQ== From: Benjamin Tissoires Date: Fri, 15 Mar 2024 15:44:39 +0100 Subject: [PATCH v4 2/7] HID: bpf: export hid_hw_output_report as a BPF kfunc Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240315-b4-hid-bpf-new-funcs-v4-2-079c282469d3@kernel.org> References: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> In-Reply-To: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> To: Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: Benjamin Tissoires , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1710513885; l=6547; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=zq66QKtjnXJ4Qer6GMlak1OP/mCYAo+o7m/hoYuei0o=; b=cnsXrVOWz3Ti0W+sZSx3G5daPohvB0026JiATxm4hhzvVmUdHLCYCkiOQsBV1hXVie570p1Qs uapkg6Jf4TjCyslK3XpaTmDXttpMnNK9/FnCL6tPmTAiM1Mo7wTjy6p X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= We currently only export hid_hw_raw_request() as a BPF kfunc. However, some devices require an explicit write on the Output Report instead of the use of the control channel. So also export hid_hw_output_report to BPF Signed-off-by: Benjamin Tissoires --- no changes in v4 no changes in v3 no changes in v2 --- Documentation/hid/hid-bpf.rst | 2 +- drivers/hid/bpf/hid_bpf_dispatch.c | 112 +++++++++++++++++++++++++++---------- drivers/hid/hid-core.c | 1 + include/linux/hid_bpf.h | 1 + 4 files changed, 86 insertions(+), 30 deletions(-) diff --git a/Documentation/hid/hid-bpf.rst b/Documentation/hid/hid-bpf.rst index 4fad83a6ebc3..a575004d9025 100644 --- a/Documentation/hid/hid-bpf.rst +++ b/Documentation/hid/hid-bpf.rst @@ -179,7 +179,7 @@ Available API that can be used in syscall HID-BPF programs: ----------------------------------------------------------- .. kernel-doc:: drivers/hid/bpf/hid_bpf_dispatch.c - :functions: hid_bpf_attach_prog hid_bpf_hw_request hid_bpf_allocate_context hid_bpf_release_context + :functions: hid_bpf_attach_prog hid_bpf_hw_request hid_bpf_hw_output_report hid_bpf_allocate_context hid_bpf_release_context General overview of a HID-BPF program ===================================== diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c index 52abb27426f4..a5b88b491b80 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.c +++ b/drivers/hid/bpf/hid_bpf_dispatch.c @@ -376,6 +376,46 @@ hid_bpf_release_context(struct hid_bpf_ctx *ctx) put_device(&hid->dev); } +static int +__hid_bpf_hw_check_params(struct hid_bpf_ctx *ctx, __u8 *buf, size_t *buf__sz, + enum hid_report_type rtype) +{ + struct hid_report_enum *report_enum; + struct hid_report *report; + struct hid_device *hdev; + u32 report_len; + + /* check arguments */ + if (!ctx || !hid_bpf_ops || !buf) + return -EINVAL; + + switch (rtype) { + case HID_INPUT_REPORT: + case HID_OUTPUT_REPORT: + case HID_FEATURE_REPORT: + break; + default: + return -EINVAL; + } + + if (*buf__sz < 1) + return -EINVAL; + + hdev = (struct hid_device *)ctx->hid; /* discard const */ + + report_enum = hdev->report_enum + rtype; + report = hid_bpf_ops->hid_get_report(report_enum, buf); + if (!report) + return -EINVAL; + + report_len = hid_report_len(report); + + if (*buf__sz > report_len) + *buf__sz = report_len; + + return 0; +} + /** * hid_bpf_hw_request - Communicate with a HID device * @@ -392,24 +432,14 @@ 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_device *hdev; - struct hid_report *report; - struct hid_report_enum *report_enum; + size_t size = buf__sz; u8 *dma_data; - u32 report_len; int ret; /* check arguments */ - if (!ctx || !hid_bpf_ops || !buf) - return -EINVAL; - - switch (rtype) { - case HID_INPUT_REPORT: - case HID_OUTPUT_REPORT: - case HID_FEATURE_REPORT: - break; - default: - return -EINVAL; - } + ret = __hid_bpf_hw_check_params(ctx, buf, &size, rtype); + if (ret) + return ret; switch (reqtype) { case HID_REQ_GET_REPORT: @@ -423,29 +453,16 @@ hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, return -EINVAL; } - if (buf__sz < 1) - return -EINVAL; - hdev = (struct hid_device *)ctx->hid; /* discard const */ - report_enum = hdev->report_enum + rtype; - report = hid_bpf_ops->hid_get_report(report_enum, buf); - if (!report) - return -EINVAL; - - report_len = hid_report_len(report); - - if (buf__sz > report_len) - buf__sz = report_len; - - dma_data = kmemdup(buf, buf__sz, GFP_KERNEL); + dma_data = kmemdup(buf, size, GFP_KERNEL); if (!dma_data) return -ENOMEM; ret = hid_bpf_ops->hid_hw_raw_request(hdev, dma_data[0], dma_data, - buf__sz, + size, rtype, reqtype); @@ -455,6 +472,42 @@ hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, kfree(dma_data); return ret; } + +/** + * hid_bpf_hw_output_report - Send an output report to a HID device + * + * @ctx: the HID-BPF context previously allocated in hid_bpf_allocate_context() + * @buf: a %PTR_TO_MEM buffer + * @buf__sz: the size of the data to transfer + * + * @returns the number of bytes transferred on success, a negative error code otherwise. + */ +__bpf_kfunc int +hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz) +{ + struct hid_device *hdev; + size_t size = buf__sz; + u8 *dma_data; + int ret; + + /* check arguments */ + ret = __hid_bpf_hw_check_params(ctx, buf, &size, HID_OUTPUT_REPORT); + if (ret) + return ret; + + hdev = (struct hid_device *)ctx->hid; /* discard const */ + + dma_data = kmemdup(buf, size, GFP_KERNEL); + if (!dma_data) + return -ENOMEM; + + ret = hid_bpf_ops->hid_hw_output_report(hdev, + dma_data, + size); + + kfree(dma_data); + return ret; +} __bpf_kfunc_end_defs(); /* @@ -488,6 +541,7 @@ BTF_ID_FLAGS(func, hid_bpf_attach_prog) BTF_ID_FLAGS(func, hid_bpf_allocate_context, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, hid_bpf_release_context, KF_RELEASE) BTF_ID_FLAGS(func, hid_bpf_hw_request) +BTF_ID_FLAGS(func, hid_bpf_hw_output_report) BTF_KFUNCS_END(hid_bpf_syscall_kfunc_ids) static const struct btf_kfunc_id_set hid_bpf_syscall_kfunc_set = { diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index de7a477d6665..1243595890ba 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -2974,6 +2974,7 @@ EXPORT_SYMBOL_GPL(hid_check_keys_pressed); static struct hid_bpf_ops hid_ops = { .hid_get_report = hid_get_report, .hid_hw_raw_request = hid_hw_raw_request, + .hid_hw_output_report = hid_hw_output_report, .owner = THIS_MODULE, .bus_type = &hid_bus_type, }; diff --git a/include/linux/hid_bpf.h b/include/linux/hid_bpf.h index 7118ac28d468..5c7ff93dc73e 100644 --- a/include/linux/hid_bpf.h +++ b/include/linux/hid_bpf.h @@ -103,6 +103,7 @@ struct hid_bpf_ops { unsigned char reportnum, __u8 *buf, size_t len, enum hid_report_type rtype, enum hid_class_request reqtype); + int (*hid_hw_output_report)(struct hid_device *hdev, __u8 *buf, size_t len); struct module *owner; const struct bus_type *bus_type; }; From patchwork Fri Mar 15 14:44:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 13593564 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 C5F8C3D98E; Fri, 15 Mar 2024 14:44:55 +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=1710513895; cv=none; b=V58nruOjKnbU6zOgz0YPrucBlEstF0h2T5dC67f0Z7nJK9afXmrRihKYpNf0/PhE12dFhScawRVee1LdIFxTdPgZ7TXSqDRHF/W8S1mMyIOABVR4F1xY8IQTEP35XIQQqIlMnsFAunF5z/grUCrClhUjNYMYoInTZNdn59vn4CE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710513895; c=relaxed/simple; bh=pz9UPHIhjuioBSALFwsu3RbtJ4LI8q55dYQ9cq39NYA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=onIiQpfNKPmMXRGHnLF6WFLsJ/6FKruBRMClkKQTBOxuyS3ToB7tkwcGrDHSGOmH+wB5s0DtvbsuqZvv2osv7eaAYgKfSeQlLHnXbSHBxsw/VtI1gdJNIdIdOXLZbDpM8STrmee0cpZTihx2HFfM/UwncyLDLk6XfQIQV6saWN8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Vsfcneb+; 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="Vsfcneb+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5AA6DC43609; Fri, 15 Mar 2024 14:44:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710513895; bh=pz9UPHIhjuioBSALFwsu3RbtJ4LI8q55dYQ9cq39NYA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Vsfcneb+GRbkDzs4fcHCgbYkwF3IEXRVaFuqq827DTW9SAeb9QBZW+gJIChC6dxz6 bPYloV/LAYe+1M0A2XCSU2rOakA8lL69iWvS3WsUNBVhHjYT341D1ZvpkJ9IPbN63S mPty90JAEGDPnMMLEM689zBuxQMkoM8k5zKWcDs7yQsfk2rn8ZTmsBwF9ve//AHU6k qfpSX5crE9s1K59wobt2XD8uELm+1ZVm1P6bP3xB1ijCUChwKldBs16SL2so2TfmCl dplJ/HdvkkMtE2q38RrOEDSOPX0tdGXvitFBbdq3GQ73Oz1kIcxibDXmdI/cyo4/EN SQx80W/Oz6OLA== From: Benjamin Tissoires Date: Fri, 15 Mar 2024 15:44:40 +0100 Subject: [PATCH v4 3/7] selftests/hid: add KASAN to the VM tests Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240315-b4-hid-bpf-new-funcs-v4-3-079c282469d3@kernel.org> References: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> In-Reply-To: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> To: Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: Benjamin Tissoires , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1710513885; l=620; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=pz9UPHIhjuioBSALFwsu3RbtJ4LI8q55dYQ9cq39NYA=; b=47C81U4y5iw3hxTvaF68SSNG4+RtTfaUBYWWl2t1emxM6sB4YZsDd1cH4US4864vx0SJ0te5J ZXnkEblM+1mCcdutJkWq+wsdJ2q/DPugifB1Y2H6ssG8Ir/350Q8wYx X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= It's always a good idea to have KASAN in tests. Signed-off-by: Benjamin Tissoires --- no changes in v4 new in v3 --- tools/testing/selftests/hid/config.common | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/hid/config.common b/tools/testing/selftests/hid/config.common index 0f456dbab62f..45b5570441ce 100644 --- a/tools/testing/selftests/hid/config.common +++ b/tools/testing/selftests/hid/config.common @@ -238,3 +238,4 @@ CONFIG_VLAN_8021Q=y CONFIG_XFRM_SUB_POLICY=y CONFIG_XFRM_USER=y CONFIG_ZEROPLUS_FF=y +CONFIG_KASAN=y From patchwork Fri Mar 15 14:44: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: 13593565 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 4AC373FB9B; Fri, 15 Mar 2024 14:44:57 +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=1710513898; cv=none; b=J143OyNVIK9DgsaImyYfGVTw06RT1UkQl90u6A3Uju8wKAc43Y9EyWWKsLh8coyst3TkIRComhsj92JRMPsXDo3LvhVF8eva+B3Cp4bCp/82s1hY58vKL7RT8UkAyW/2ue6knW6WLdWfl/XrehEV8oBjkYY7LbAw1OPJ05R7Z90= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710513898; c=relaxed/simple; bh=og1diMuOpRLjjaT5oLEkhUJW8uHYEsPC/jAXiaYtsWU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ADc/jkiZFHEbJY2lCyGN5E6w5mJi6epSbvG5s9cG7NCDIZE99TEmYKXgu1C+4adFW7PiyaUVXcImCe1uUQNdf8foMS12w4UjElEn5GA42Tb9hRs2/gt2qDD1PCNl0s5bmJPPzucWH/r0k/oekZ/aGDVaDEBV/bfp+iRCiG8MBko= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hstzs9Pg; 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="hstzs9Pg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD451C433F1; Fri, 15 Mar 2024 14:44:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710513897; bh=og1diMuOpRLjjaT5oLEkhUJW8uHYEsPC/jAXiaYtsWU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hstzs9PgAsRsEQxJvGiPgckoocD3RDGxlxQXobwcKnjTzW5eEWG40KeGH5RwmUFDQ MXD9dnRjN1XWrLFaeGSQvaoqS/+xuVrBgmgGGSFRCWIZjJFH4CnlduDT3K02wzRcvp UZjbbQZ/ozUbw/a/ipUkuE/TiBlG1xnxXXnSie59uqVGE+C2mDqcArefJR1U2F21m0 ResN75dye9TIMhKR3aw3mkEOHkHro24aTZtLYXdcvVvyGJynEtBc8WBtNdqzBJ6Up8 Q3pqQFUaVwaW8wSKIwtqrLvWsASjFDo283WwxA6uROM4cG4k/67/D8Lie9kMKLOzpO 9V1h+j0eY/2kQ== From: Benjamin Tissoires Date: Fri, 15 Mar 2024 15:44:41 +0100 Subject: [PATCH v4 4/7] selftests/hid: Add test for hid_bpf_hw_output_report Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240315-b4-hid-bpf-new-funcs-v4-4-079c282469d3@kernel.org> References: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> In-Reply-To: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> To: Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: Benjamin Tissoires , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1710513885; l=5165; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=og1diMuOpRLjjaT5oLEkhUJW8uHYEsPC/jAXiaYtsWU=; b=VwBEMMaZ3XIy1VY6TIYw/FrHMH9e/UHvqgEIGmLKS9oyZtlchxLGbXBy1YTq5YyjdoUC4mGuJ s+gqIYDUlkCAEyeT3Se+lLsPcknuVosDBCuhoYAQNqZ9GNfcT9C+W3W X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= This time we need to ensure uhid receives it, thus the new mutex and condition. Signed-off-by: Benjamin Tissoires --- no changes in v4 no changes in v3 no changes in v2 --- tools/testing/selftests/hid/hid_bpf.c | 63 ++++++++++++++++++++++ tools/testing/selftests/hid/progs/hid.c | 24 +++++++++ .../testing/selftests/hid/progs/hid_bpf_helpers.h | 2 + 3 files changed, 89 insertions(+) diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c index 2cf96f818f25..8332014838b0 100644 --- a/tools/testing/selftests/hid/hid_bpf.c +++ b/tools/testing/selftests/hid/hid_bpf.c @@ -16,6 +16,11 @@ #define SHOW_UHID_DEBUG 0 +#define min(a, b) \ + ({ __typeof__(a) _a = (a); \ + __typeof__(b) _b = (b); \ + _a < _b ? _a : _b; }) + static unsigned char rdesc[] = { 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 0x09, 0x21, /* Usage (Vendor Usage 0x21) */ @@ -111,6 +116,10 @@ struct hid_hw_request_syscall_args { static pthread_mutex_t uhid_started_mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t uhid_started = PTHREAD_COND_INITIALIZER; +static pthread_mutex_t uhid_output_mtx = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t uhid_output_cond = PTHREAD_COND_INITIALIZER; +static unsigned char output_report[10]; + /* no need to protect uhid_stopped, only one thread accesses it */ static bool uhid_stopped; @@ -205,6 +214,13 @@ static int uhid_event(struct __test_metadata *_metadata, int fd) break; case UHID_OUTPUT: UHID_LOG("UHID_OUTPUT from uhid-dev"); + + pthread_mutex_lock(&uhid_output_mtx); + memcpy(output_report, + ev.u.output.data, + min(ev.u.output.size, sizeof(output_report))); + pthread_cond_signal(&uhid_output_cond); + pthread_mutex_unlock(&uhid_output_mtx); break; case UHID_GET_REPORT: UHID_LOG("UHID_GET_REPORT from uhid-dev"); @@ -733,6 +749,53 @@ TEST_F(hid_bpf, test_hid_change_report) ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test"); } +/* + * Call hid_bpf_hw_output_report against the given uhid device, + * check that the program is called and does the expected. + */ +TEST_F(hid_bpf, test_hid_user_output_report_call) +{ + struct hid_hw_request_syscall_args args = { + .retval = -1, + .size = 10, + }; + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs, + .ctx_in = &args, + .ctx_size_in = sizeof(args), + ); + int err, cond_err, prog_fd; + struct timespec time_to_wait; + + LOAD_BPF; + + args.hid = self->hid_id; + args.data[0] = 1; /* report ID */ + args.data[1] = 2; /* report ID */ + args.data[2] = 42; /* report ID */ + + prog_fd = bpf_program__fd(self->skel->progs.hid_user_output_report); + + pthread_mutex_lock(&uhid_output_mtx); + + memset(output_report, 0, sizeof(output_report)); + clock_gettime(CLOCK_REALTIME, &time_to_wait); + time_to_wait.tv_sec += 2; + + err = bpf_prog_test_run_opts(prog_fd, &tattrs); + cond_err = pthread_cond_timedwait(&uhid_output_cond, &uhid_output_mtx, &time_to_wait); + + ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts"); + ASSERT_OK(cond_err) TH_LOG("error while calling waiting for the condition"); + + ASSERT_EQ(args.retval, 3); + + ASSERT_EQ(output_report[0], 1); + ASSERT_EQ(output_report[1], 2); + ASSERT_EQ(output_report[2], 42); + + pthread_mutex_unlock(&uhid_output_mtx); +} + /* * Attach hid_user_raw_request to the given uhid device, * call the bpf program from userspace diff --git a/tools/testing/selftests/hid/progs/hid.c b/tools/testing/selftests/hid/progs/hid.c index 1e558826b809..2c2b679a83b1 100644 --- a/tools/testing/selftests/hid/progs/hid.c +++ b/tools/testing/selftests/hid/progs/hid.c @@ -101,6 +101,30 @@ int hid_user_raw_request(struct hid_hw_request_syscall_args *args) return 0; } +SEC("syscall") +int hid_user_output_report(struct hid_hw_request_syscall_args *args) +{ + struct hid_bpf_ctx *ctx; + const size_t size = args->size; + int i, ret = 0; + + if (size > sizeof(args->data)) + return -7; /* -E2BIG */ + + ctx = hid_bpf_allocate_context(args->hid); + if (!ctx) + return -1; /* EPERM check */ + + ret = hid_bpf_hw_output_report(ctx, + args->data, + size); + args->retval = ret; + + hid_bpf_release_context(ctx); + + return 0; +} + static const __u8 rdesc[] = { 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ 0x09, 0x32, /* USAGE (Z) */ diff --git a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h index 65e657ac1198..50c6a0d5765e 100644 --- a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h +++ b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h @@ -94,5 +94,7 @@ extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx, size_t buf__sz, enum hid_report_type type, enum hid_class_request reqtype) __ksym; +extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx, + __u8 *buf, size_t buf__sz) __ksym; #endif /* __HID_BPF_HELPERS_H */ From patchwork Fri Mar 15 14:44:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 13593566 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 BC73D4500B; Fri, 15 Mar 2024 14:45:00 +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=1710513900; cv=none; b=PM51em9SuD32GvET+ZI1/AxcVkK4DFPx2YXmdfP8YIwcFZZponxlbPYSLr14x1EFDwUXBqQFDERFqpsFDzMLhGWBc1j+1V86PFHzm82Wana2ha8xx/BNtn/r8o6na1/xh/kmVSsSpslhRulEp0lNqTbRmMN5EBCqb5mwe/s+Z9Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710513900; c=relaxed/simple; bh=/oHVc4ITn2gHZ2tfhFRi9yxxyiWXm+F07UEEsHeUq6M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jyoVCuZSW0O3VE3Ssf/EYkAVd8zBM6ESEMYPtCF5/qQ7bC3ptfPTnlA0Gz3tfnoIk0yxgtJUpZmcGHjd8mFxuQg7E8KFIlyXzRtDyvaqdo7lrTi8j+LWzWEUv5LEc8tRgQiy1wGXDUSgBw6MnbdADxuzP4OwcYZ5cu1JjHPXiGs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tt1YoZjW; 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="tt1YoZjW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B3AEC43609; Fri, 15 Mar 2024 14:44:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710513900; bh=/oHVc4ITn2gHZ2tfhFRi9yxxyiWXm+F07UEEsHeUq6M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=tt1YoZjWjVTv5x+nxf9fa5f8NhDuD10Mu+izqemGgJxl2pSrpFPiMV0KjoBZ2ktOt byinqVtY9egGMYIOfVAC3ZFkyawgnOa5STwJvEgZGFSRmyv/cyrXv5+HUakEekBMqU P//rrquI8EI3qwGyanGY7uVjeU8Cfiiw4wuoR1lM7i58rqWdFo3e0KmCTv/CHnQPmg IeI/ht6Yi9/AI0Fd/h/CzE0VbuWnyOCz2EhnVibBqwEewLHzo3qr2FycbigDDVGAYF nDSAHdJOZ+beZ74fL5NZrHti2GIzeLVQpRWVXmjPwNWmbitUI1q7MvO5pF2doMkti9 +rJzFAoY7Fylw== From: Benjamin Tissoires Date: Fri, 15 Mar 2024 15:44:42 +0100 Subject: [PATCH v4 5/7] HID: bpf: allow to inject HID event from BPF Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240315-b4-hid-bpf-new-funcs-v4-5-079c282469d3@kernel.org> References: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> In-Reply-To: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> To: Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: Benjamin Tissoires , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1710513885; l=4162; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=/oHVc4ITn2gHZ2tfhFRi9yxxyiWXm+F07UEEsHeUq6M=; b=pmNfz8SNJ365ckpVlztGoaqjLKyJnC67YDeKUoWiRbm9qrXqhlz0CRdXkETKByFODZdTYeeEp kI5oJb3zMjkA1dtR/9UwlJV8V9S4//QEirXbuxf3ger8IuZxwyQf9RN X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= It can be interesting to inject events from BPF as if the event were to come from the device. For example, some multitouch devices do not all the time send a proximity out event, and we might want to send it for the physical device. Compared to uhid, we can now inject events on any physical device, not just uhid virtual ones. Signed-off-by: Benjamin Tissoires --- no changes in v4 no changes in v3 no changes in v2 --- Documentation/hid/hid-bpf.rst | 2 +- drivers/hid/bpf/hid_bpf_dispatch.c | 29 +++++++++++++++++++++++++++++ drivers/hid/hid-core.c | 1 + include/linux/hid_bpf.h | 2 ++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Documentation/hid/hid-bpf.rst b/Documentation/hid/hid-bpf.rst index a575004d9025..0765b3298ecf 100644 --- a/Documentation/hid/hid-bpf.rst +++ b/Documentation/hid/hid-bpf.rst @@ -179,7 +179,7 @@ Available API that can be used in syscall HID-BPF programs: ----------------------------------------------------------- .. kernel-doc:: drivers/hid/bpf/hid_bpf_dispatch.c - :functions: hid_bpf_attach_prog hid_bpf_hw_request hid_bpf_hw_output_report hid_bpf_allocate_context hid_bpf_release_context + :functions: hid_bpf_attach_prog hid_bpf_hw_request hid_bpf_hw_output_report hid_bpf_input_report hid_bpf_allocate_context hid_bpf_release_context General overview of a HID-BPF program ===================================== diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c index a5b88b491b80..e1a650f4a626 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.c +++ b/drivers/hid/bpf/hid_bpf_dispatch.c @@ -508,6 +508,34 @@ hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz) kfree(dma_data); return ret; } + +/** + * hid_bpf_input_report - Inject a HID report in the kernel from a HID device + * + * @ctx: the HID-BPF context previously allocated in hid_bpf_allocate_context() + * @type: the type of the report (%HID_INPUT_REPORT, %HID_FEATURE_REPORT, %HID_OUTPUT_REPORT) + * @buf: a %PTR_TO_MEM buffer + * @buf__sz: the size of the data to transfer + * + * @returns %0 on success, a negative error code otherwise. + */ +__bpf_kfunc int +hid_bpf_input_report(struct hid_bpf_ctx *ctx, enum hid_report_type type, u8 *buf, + const size_t buf__sz) +{ + struct hid_device *hdev; + size_t size = buf__sz; + int ret; + + /* check arguments */ + ret = __hid_bpf_hw_check_params(ctx, buf, &size, type); + if (ret) + return ret; + + hdev = (struct hid_device *)ctx->hid; /* discard const */ + + return hid_input_report(hdev, type, buf, size, 0); +} __bpf_kfunc_end_defs(); /* @@ -542,6 +570,7 @@ BTF_ID_FLAGS(func, hid_bpf_allocate_context, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, hid_bpf_release_context, KF_RELEASE) BTF_ID_FLAGS(func, hid_bpf_hw_request) BTF_ID_FLAGS(func, hid_bpf_hw_output_report) +BTF_ID_FLAGS(func, hid_bpf_input_report) BTF_KFUNCS_END(hid_bpf_syscall_kfunc_ids) static const struct btf_kfunc_id_set hid_bpf_syscall_kfunc_set = { diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 1243595890ba..b1fa0378e8f4 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -2975,6 +2975,7 @@ static struct hid_bpf_ops hid_ops = { .hid_get_report = hid_get_report, .hid_hw_raw_request = hid_hw_raw_request, .hid_hw_output_report = hid_hw_output_report, + .hid_input_report = hid_input_report, .owner = THIS_MODULE, .bus_type = &hid_bus_type, }; diff --git a/include/linux/hid_bpf.h b/include/linux/hid_bpf.h index 5c7ff93dc73e..17b08f500098 100644 --- a/include/linux/hid_bpf.h +++ b/include/linux/hid_bpf.h @@ -104,6 +104,8 @@ struct hid_bpf_ops { size_t len, enum hid_report_type rtype, enum hid_class_request reqtype); int (*hid_hw_output_report)(struct hid_device *hdev, __u8 *buf, size_t len); + int (*hid_input_report)(struct hid_device *hid, enum hid_report_type type, + u8 *data, u32 size, int interrupt); struct module *owner; const struct bus_type *bus_type; }; From patchwork Fri Mar 15 14:44: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: 13593567 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 46DDF46544; Fri, 15 Mar 2024 14:45:02 +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=1710513903; cv=none; b=dTSZpX0mvoUord0wmCYmVRJSzFgc2FGd1b+zGdSsUJ9k0iNfapA1KJz+5g3PvD3Ye6lx9bks2VAqDEF1H71uzfirgL24+XSeWL5EMkDdKSqzP56PkqbAyAbQKX0ySbDAhrkNGlz7erZ/aUTR2vDuB9ZaTtnXwINRduM/bOHLWAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710513903; c=relaxed/simple; bh=lYHlpbXv8wmdpHJLSDMirre3aCp7tdPr2ucjfV4U1U8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CVnAUiyDtn/Km7TkC+jS/Nd03N0BNn4mVo0qy7+gf5yQxDFjF5/Hv0/z/XFYRF1D4++pRtNcJE9AAXnahsYL0WznPcMUQ72BLbhWjhalQm6kxLUIfnNfhU8sUHno7PqSpbZem38JSYe/U0gxzkdvEpBbQH1OkwAGWilAgT1QasU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VZ0dtYY+; 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="VZ0dtYY+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF1B9C433F1; Fri, 15 Mar 2024 14:45:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710513902; bh=lYHlpbXv8wmdpHJLSDMirre3aCp7tdPr2ucjfV4U1U8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=VZ0dtYY+oDlLwucql1aC6sy0cTEuCi3fEdRQ4dhwzugjxByCeHGKnflp42Ed7KxaY /Sew4SrSWKD9HTtgtOWPVNdjg9vCJMObMdB8BoVfQPP/9P+PSVVUQgivmI5lt/FyAf bKtztad+76f9XD2tJglys/BBvB3Pt1dKtC8o6gbcmHJQXdVx7jHqs5qEg8U9GWw9kT iyqCDEXc4p9g8yRQSFHFfMv4nYnA8uFOizaq0t7Mao4xPdwfbpG3VTPv+9pZi4bI7D RsVbvd1ayJJ4BUOxGSExeLI6cHpj9Drh7YBIDOn38qIXpuHiOjNg8+7MqVAuYc4d1J IAF25MZJamuuA== From: Benjamin Tissoires Date: Fri, 15 Mar 2024 15:44:43 +0100 Subject: [PATCH v4 6/7] selftests/hid: add tests for hid_bpf_input_report Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240315-b4-hid-bpf-new-funcs-v4-6-079c282469d3@kernel.org> References: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> In-Reply-To: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> To: Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: Benjamin Tissoires , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1710513885; l=4246; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=lYHlpbXv8wmdpHJLSDMirre3aCp7tdPr2ucjfV4U1U8=; b=IIe/kVEZQ/jDRK2mnYLrzNf7zUvgbeEt3ejpPJX82UWsH0HVPIsWT8VFNroSxg7DNnzBPLM7f +FeZxznY/E1C+j3MsVXYOul9vrKfmH5SKdVsrwesBLPOJ9SfWllyd1h X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= Usual way of testing, we call the function and ensures we receive the event Signed-off-by: Benjamin Tissoires --- no changes in v4 no changes in v3 no changes in v2 --- tools/testing/selftests/hid/hid_bpf.c | 49 +++++++++++++++++++++- tools/testing/selftests/hid/progs/hid.c | 22 ++++++++++ .../testing/selftests/hid/progs/hid_bpf_helpers.h | 4 ++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c index 8332014838b0..f825623e3edc 100644 --- a/tools/testing/selftests/hid/hid_bpf.c +++ b/tools/testing/selftests/hid/hid_bpf.c @@ -749,6 +749,52 @@ TEST_F(hid_bpf, test_hid_change_report) ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test"); } +/* + * Call hid_bpf_input_report against the given uhid device, + * check that the program is called and does the expected. + */ +TEST_F(hid_bpf, test_hid_user_input_report_call) +{ + struct hid_hw_request_syscall_args args = { + .retval = -1, + .size = 10, + }; + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs, + .ctx_in = &args, + .ctx_size_in = sizeof(args), + ); + __u8 buf[10] = {0}; + int err, prog_fd; + + LOAD_BPF; + + args.hid = self->hid_id; + args.data[0] = 1; /* report ID */ + args.data[1] = 2; /* report ID */ + args.data[2] = 42; /* report ID */ + + prog_fd = bpf_program__fd(self->skel->progs.hid_user_input_report); + + /* check that there is no data to read from hidraw */ + memset(buf, 0, sizeof(buf)); + err = read(self->hidraw_fd, buf, sizeof(buf)); + ASSERT_EQ(err, -1) TH_LOG("read_hidraw"); + + err = bpf_prog_test_run_opts(prog_fd, &tattrs); + + ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts"); + + ASSERT_EQ(args.retval, 0); + + /* read the data from hidraw */ + memset(buf, 0, sizeof(buf)); + err = read(self->hidraw_fd, buf, sizeof(buf)); + ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); + ASSERT_EQ(buf[0], 1); + ASSERT_EQ(buf[1], 2); + ASSERT_EQ(buf[2], 42); +} + /* * Call hid_bpf_hw_output_report against the given uhid device, * check that the program is called and does the expected. @@ -797,8 +843,7 @@ TEST_F(hid_bpf, test_hid_user_output_report_call) } /* - * Attach hid_user_raw_request to the given uhid device, - * call the bpf program from userspace + * Call hid_hw_raw_request against the given uhid device, * check that the program is called and does the expected. */ TEST_F(hid_bpf, test_hid_user_raw_request_call) diff --git a/tools/testing/selftests/hid/progs/hid.c b/tools/testing/selftests/hid/progs/hid.c index 2c2b679a83b1..f67d35def142 100644 --- a/tools/testing/selftests/hid/progs/hid.c +++ b/tools/testing/selftests/hid/progs/hid.c @@ -125,6 +125,28 @@ int hid_user_output_report(struct hid_hw_request_syscall_args *args) return 0; } +SEC("syscall") +int hid_user_input_report(struct hid_hw_request_syscall_args *args) +{ + struct hid_bpf_ctx *ctx; + const size_t size = args->size; + int i, ret = 0; + + if (size > sizeof(args->data)) + return -7; /* -E2BIG */ + + ctx = hid_bpf_allocate_context(args->hid); + if (!ctx) + return -1; /* EPERM check */ + + ret = hid_bpf_input_report(ctx, HID_INPUT_REPORT, args->data, size); + args->retval = ret; + + hid_bpf_release_context(ctx); + + return 0; +} + static const __u8 rdesc[] = { 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ 0x09, 0x32, /* USAGE (Z) */ diff --git a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h index 50c6a0d5765e..9cd56821d0f1 100644 --- a/tools/testing/selftests/hid/progs/hid_bpf_helpers.h +++ b/tools/testing/selftests/hid/progs/hid_bpf_helpers.h @@ -96,5 +96,9 @@ extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx, enum hid_class_request reqtype) __ksym; extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz) __ksym; +extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx, + enum hid_report_type type, + __u8 *data, + size_t buf__sz) __ksym; #endif /* __HID_BPF_HELPERS_H */ From patchwork Fri Mar 15 14:44: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: 13593568 X-Patchwork-Delegate: jikos@jikos.cz 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 C6046487B5; Fri, 15 Mar 2024 14:45: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=1710513905; cv=none; b=RaHlV50ODyKx0R3v4hOO9GGCtEJlbvp1Elrpo9Jksn5vJl2p9RNDG63MLD/jbT+WvayRMJT2KJRdGSdFe08Tg2+BvqSSW/8AJNunSOD9KKZSYQ9t/jKOPBEB6lmtLE6/qxmBvWueEfCEd97jWnVM2T4k99iLPnRBSC+vhFIWfBk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710513905; c=relaxed/simple; bh=pHSHMrnqR0ZCUlfOmkPFExxeq+HQHfhP88ZBOTpK7+U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YNqxOdDpu5N5/ynQcyLx5R6YIfi4MFo2WxyVtK4wEvtvtXZA4x/yEKuYt5Ss7CWVclNwm8/iyq3f9193uarRMastLCsb3GLmUJWCw0SshoXeGzaDsgrSyZ0YUtZAn1nN9YSyobvIS5C2l0WPtXtQSrbmlBpONxx80FGheIfX/AY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=k5L7FB7Z; 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="k5L7FB7Z" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EF12C433C7; Fri, 15 Mar 2024 14:45:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710513905; bh=pHSHMrnqR0ZCUlfOmkPFExxeq+HQHfhP88ZBOTpK7+U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=k5L7FB7ZPJtTBZL1lfjZb4NkQC98oCeF4hiesYmJWVwHzkLUHvfqxpwCFRRn2kLr7 3Sk8riGIe8rYazkDUkbaNk3wjmXBYybetTp0iyIw9JJLxusH2eLYFaz/mgxoMPsB9x 1BWcqe83XsD5fzeHuS9DJS6AT/jdyfLPXPCHCzJFylPJudi6QXWDGLIQx/atyOkaZO nknUIiRPIm2cPbsmqTVMaro3/xY32t/CTKgJFeMpjZkN7tdW8vWW3pyP0wHs5ghMmH /NulkRAvne/o8lYJak4oHcXaZHWRVjibwsJkNHxqMV4UiWftOhisHeEUNYT0KJnH/R mw2QeyKmdVDtQ== From: Benjamin Tissoires Date: Fri, 15 Mar 2024 15:44:44 +0100 Subject: [PATCH v4 7/7] HID: bpf: allow to use bpf_timer_set_sleepable_cb() in tracing callbacks. Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240315-b4-hid-bpf-new-funcs-v4-7-079c282469d3@kernel.org> References: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> In-Reply-To: <20240315-b4-hid-bpf-new-funcs-v4-0-079c282469d3@kernel.org> To: Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: Benjamin Tissoires , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1710513885; l=1287; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=pHSHMrnqR0ZCUlfOmkPFExxeq+HQHfhP88ZBOTpK7+U=; b=neXHs6HmauwDcYyxZkB2zyEGDGkjjsb/jB2ZqXOyT2E7aR7xhzlaptJaXq5rkpXzM0p9yQoUE 2jmWyF+kMQFBp22CSeJfCS4DRsYQJC8xJunXMCcIiD+QUwgS+VGwPxv X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= Export the sleepable kfuncs we have on HID-BPF in tracing bpf programs, but with the condition of being used in a sleepable context. This allows to use the bpf_timer when used in a sleepable context through bpf_timer_set_sleepable_cb() and initiate work from a device event. Signed-off-by: Benjamin Tissoires --- no changes in v4 no changes in v3 new in v2 --- drivers/hid/bpf/hid_bpf_dispatch.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/hid/bpf/hid_bpf_dispatch.c b/drivers/hid/bpf/hid_bpf_dispatch.c index e1a650f4a626..e27c2ffb4f60 100644 --- a/drivers/hid/bpf/hid_bpf_dispatch.c +++ b/drivers/hid/bpf/hid_bpf_dispatch.c @@ -544,6 +544,11 @@ __bpf_kfunc_end_defs(); */ BTF_KFUNCS_START(hid_bpf_kfunc_ids) BTF_ID_FLAGS(func, hid_bpf_get_data, KF_RET_NULL) +BTF_ID_FLAGS(func, hid_bpf_allocate_context, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE) +BTF_ID_FLAGS(func, hid_bpf_release_context, KF_RELEASE | KF_SLEEPABLE) +BTF_ID_FLAGS(func, hid_bpf_hw_request, KF_SLEEPABLE) +BTF_ID_FLAGS(func, hid_bpf_hw_output_report, KF_SLEEPABLE) +BTF_ID_FLAGS(func, hid_bpf_input_report, KF_SLEEPABLE) BTF_KFUNCS_END(hid_bpf_kfunc_ids) static const struct btf_kfunc_id_set hid_bpf_kfunc_set = {