Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2265141pxm; Fri, 4 Mar 2022 12:39:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJy4CL8C75XCfzlgGuQ8QrEOgzSlb76KLRIjFCSJ1SbMbxcbI6AOsgw9QLVcoB0MKvHX2TTo X-Received: by 2002:a17:902:6b8b:b0:14d:66c4:f704 with SMTP id p11-20020a1709026b8b00b0014d66c4f704mr207153plk.53.1646426390554; Fri, 04 Mar 2022 12:39:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646426390; cv=none; d=google.com; s=arc-20160816; b=wQWvSVreLbh2gGntqDqjKmD/p071zoHlc0vSrtkjWNFE/kIA4x75yiZRcuOQzP3JQn Y1BF2N4dY0aOAzujJT/MEoYiwSHR21Cmi/3fzro2tYDtqWA8McJ003wOW4pxqp5QfcZP 2nca5dieU9z8XgPD6/nMIxevM4EvYcJ0Aobc3lAySTLEj81YieOqdQxVdfAC7nL4D+ws 3JUqv/umXs1v8tJoqU19blt2kuMcmvUcOMjOCxlwcdMsFb7krUGEtJOy1DEcfGw3YanQ qPA76f3zmMNgZEmIrs/jsbj1A0LNMYr5Z5aj5NWS4DhWqNSXA4fMWi0/vHnyQveZX4Fk jxEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=WeTIWQC6fVAlk6Saj5kWD691Ja7Vds/ypH8GpPfWruc=; b=CqqNgmuMO9A8UVQhMTqniroQo6zVyRPXRlmKMknc0aOZoOBWmDswa7RViqMSm+UB0t vNphwA5Ztc7HGXb7d9Z6QUmWfLZ90UCgkKM4zF6SFHD08lER2AvoMm6dE/qKEvWeK92p gY9F0zEA7L8GlJjt70QJt1D3xyQK5gC955U7EsyMJRSmtGY/F+cTbXXVe3/ZUUnfPCB+ pgKwA/1F5lNPPFMVuk2ctys2W8KXrtgLMDgRRMZ1af2yjMB+AcAVt9TUcUNozLeWdMhA cDRr7JSsKicpc37erFmJUcpNauiUEhahV4to+CVLpmhGRUapF/dIpOAAea6e1I9lL4hL Q4Cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Qq/QFceh"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id 8-20020a056a00070800b004ca9f0a3a29si5455760pfl.267.2022.03.04.12.39.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 12:39:50 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Qq/QFceh"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7015622FDB8; Fri, 4 Mar 2022 11:39:55 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241297AbiCDReJ (ORCPT + 99 others); Fri, 4 Mar 2022 12:34:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241219AbiCDRdt (ORCPT ); Fri, 4 Mar 2022 12:33:49 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C6CA9275E2 for ; Fri, 4 Mar 2022 09:32:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646415167; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WeTIWQC6fVAlk6Saj5kWD691Ja7Vds/ypH8GpPfWruc=; b=Qq/QFcehw+WHXNYiQumYzGr3D5khM3jfGfLoWlRK1tK0xxniP3m10I3avXArNII93j0Ktq g2wP7no/elJ3Y1IPwvF6B21DojuLHRgWSV1r916ND/hqgAxRVoOMn2ZtV1+Xp49ulF4hME /MsNssYTZZbQTDsslvOndVUjMiT32OU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-394-0nhDjJoCOhe0PWVuPWThqg-1; Fri, 04 Mar 2022 12:32:42 -0500 X-MC-Unique: 0nhDjJoCOhe0PWVuPWThqg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5BF2B8066F3; Fri, 4 Mar 2022 17:32:40 +0000 (UTC) Received: from plouf.redhat.com (unknown [10.39.192.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2F2038659E; Fri, 4 Mar 2022 17:32:36 +0000 (UTC) From: Benjamin Tissoires To: Greg KH , Jiri Kosina , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Shuah Khan , Dave Marchevsky , Joe Stringer Cc: Tero Kristo , linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires Subject: [PATCH bpf-next v2 13/28] HID: bpf: implement hid_bpf_get|set_data Date: Fri, 4 Mar 2022 18:28:37 +0100 Message-Id: <20220304172852.274126-14-benjamin.tissoires@redhat.com> In-Reply-To: <20220304172852.274126-1-benjamin.tissoires@redhat.com> References: <20220304172852.274126-1-benjamin.tissoires@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We have 2 cases of usage here: - either n <= 32: we are addressing individual bits at the given offset - either n > 32: we are using a memcpy to transmit the data to the caller, meaning that we need to be byte-aligned. Signed-off-by: Benjamin Tissoires --- changes in v2: - split the series by bpf/libbpf/hid/selftests and samples - allow for n > 32, by relying on memcpy --- drivers/hid/hid-bpf.c | 68 ++++++++++++++++++++++++++++++++++++++++++ drivers/hid/hid-core.c | 4 +-- include/linux/hid.h | 2 ++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/drivers/hid/hid-bpf.c b/drivers/hid/hid-bpf.c index 510e24f4307c..8ae247fba5bc 100644 --- a/drivers/hid/hid-bpf.c +++ b/drivers/hid/hid-bpf.c @@ -105,6 +105,72 @@ static void hid_bpf_array_detached(struct hid_device *hdev, enum bpf_hid_attach_ } } +int hid_bpf_get_data(struct hid_device *hdev, u8 *buf, size_t buf_size, u64 offset, u32 n, + u8 *data, u64 data_size) +{ + u32 *value = (u32 *)data; + + if (((offset + n) >> 3) >= buf_size) + return -E2BIG; + + if (n <= 32) { + /* data must be a pointer to a u32 */ + if (data_size != 4) + return -EINVAL; + + *value = hid_field_extract(hdev, buf, offset, n); + return 4; + } + + /* if n > 32, use memcpy, but ensure we are dealing with full bytes */ + if ((n | offset) & 0x7) + return -EINVAL; + + /* work on bytes now */ + offset = offset >> 3; + n = n >> 3; + + if (n > data_size) + return -EINVAL; + + memcpy(data, buf + offset, n); + + return n; +} + +int hid_bpf_set_data(struct hid_device *hdev, u8 *buf, size_t buf_size, u64 offset, u32 n, + u8 *data, u64 data_size) +{ + u32 *value = (u32 *)data; + + if (((offset + n) >> 3) >= buf_size) + return -E2BIG; + + if (n <= 32) { + /* data must be a pointer to a u32 */ + if (data_size != 4) + return -EINVAL; + + implement(hdev, buf, offset, n, *value); + return 4; + } + + /* if n > 32, use memcpy, but ensure we are dealing with full bytes */ + if ((n | offset) & 0x7) + return -EINVAL; + + /* work on bytes now */ + offset = offset >> 3; + n = n >> 3; + + if (n > data_size) + return -EINVAL; + + memcpy(buf + offset, data, n); + + return n; +} + static int hid_bpf_run_progs(struct hid_device *hdev, enum bpf_hid_attach_type type, struct hid_bpf_ctx *ctx, u8 *data, int size) { @@ -204,6 +270,8 @@ int __init hid_bpf_module_init(void) .link_attach = hid_bpf_link_attach, .link_attached = hid_bpf_link_attached, .array_detached = hid_bpf_array_detached, + .hid_get_data = hid_bpf_get_data, + .hid_set_data = hid_bpf_set_data, }; bpf_hid_set_hooks(&hooks); diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 0eb8189faaee..d3f4499ee4cd 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -1416,8 +1416,8 @@ static void __implement(u8 *report, unsigned offset, int n, u32 value) } } -static void implement(const struct hid_device *hid, u8 *report, - unsigned offset, unsigned n, u32 value) +void implement(const struct hid_device *hid, u8 *report, unsigned int offset, unsigned int n, + u32 value) { if (unlikely(n > 32)) { hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n", diff --git a/include/linux/hid.h b/include/linux/hid.h index 66d949d10b78..7454e844324c 100644 --- a/include/linux/hid.h +++ b/include/linux/hid.h @@ -944,6 +944,8 @@ bool hid_compare_device_paths(struct hid_device *hdev_a, s32 hid_snto32(__u32 value, unsigned n); __u32 hid_field_extract(const struct hid_device *hid, __u8 *report, unsigned offset, unsigned n); +void implement(const struct hid_device *hid, u8 *report, unsigned int offset, unsigned int n, + u32 value); #ifdef CONFIG_PM int hid_driver_suspend(struct hid_device *hdev, pm_message_t state); -- 2.35.1