Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp63850pxp; Fri, 18 Mar 2022 18:22:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwGg5cEx1f3cwDwFsQPD1qAS/SKKQxNWLWCjm1pRnLxFbUz/XylUtuO2e4T0a49H6WQPC25 X-Received: by 2002:a17:90a:2c0b:b0:1be:da5a:b294 with SMTP id m11-20020a17090a2c0b00b001beda5ab294mr14085435pjd.9.1647652949620; Fri, 18 Mar 2022 18:22:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647652949; cv=none; d=google.com; s=arc-20160816; b=F1/EvUCgj1DEjxSt+DQ5bBZd+2Ts66sZOVgNcuZGD3Ngz49Z0mf/UtTXFXxENPqwnG X8wh6yJXgE3V2d7sHmtc4ktXa1q5NmuHuze3rKRfdjvR2zRr4wdv32fGcc6aPmQ8PcIY bBZL5/ubXQYPcEcidfsFdmzg6Ls9GwDml1VXgmAlb7Soifo93AjaaySM6iv/21jIx8Xm g6Qxr9S2s41Vv1JEn5mw4aaN6uem7jCx3kTLoOkNXgReKzVh2D0UFDm5uuqTXX+z4y/n 6DVzaB8GhCsVE7/KUA3Ld/XtpBqvR3CWu4hXhccJ0iiemjMLCCsyN4wlretidhG/keGc o+hA== 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=8nJYYlBY7G+EkGr1cCCnY5dDxM56e+iZoTRwhxTl+8k=; b=C6k0LRthltlSyPTM2gttmZc9TuhBLq7YO/mKGS3CnnpiEE38tZ4SRmmc+9O8INiKhK 8RHEm6eWbigbM0gw5vY6gzcN0lT+EDh3LZeYVPfjzlJvxMpLK4jDnJ3dADwIZq0hDBUo 6PoN0QCusK8CKBRc/5KwfX6wdag0bR+GITi1zRH4sJTg8DGXjTV42mLygdMUOZrRCBT8 HLkO5VKEUEXWLhrXCRUunrMOs7/q15UY95sM/N27J3+YL0azFEL9FzR2PgkgK69K4kMJ sNVeFkl49gSXsDvp99o1X50Ad4EHq/W2o7sg/U3XgfdD3D19umt4xb04LfdSGNJ8Szt9 Gkmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=c3yir5zX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t2-20020a056a00138200b004fa3a8dff73si943879pfg.42.2022.03.18.18.21.45; Fri, 18 Mar 2022 18:22:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=c3yir5zX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238825AbiCRQVI (ORCPT + 99 others); Fri, 18 Mar 2022 12:21:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239168AbiCRQTb (ORCPT ); Fri, 18 Mar 2022 12:19:31 -0400 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 51BD617DC90 for ; Fri, 18 Mar 2022 09:18:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647620280; 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=8nJYYlBY7G+EkGr1cCCnY5dDxM56e+iZoTRwhxTl+8k=; b=c3yir5zXJj6d/7VYSSBZV0RSgc+bM87fyYOFPSy9HIGL2uHJS25wpGgQnLOOzhI1MEAFB3 a2jk7hN6zyh4EKwp3zAn9+UF/8FvGSvUvusAUfVEjZ652uDz+GeMglqSS6pS/vNiW9EN2a BUql0KWTYbHKjHwvjAFnGKlbVR7QDiE= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-622-qlLdcQmqOwKSVMP1eIqycg-1; Fri, 18 Mar 2022 12:17:55 -0400 X-MC-Unique: qlLdcQmqOwKSVMP1eIqycg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D534D38035BB; Fri, 18 Mar 2022 16:17:53 +0000 (UTC) Received: from plouf.redhat.com (unknown [10.39.192.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id B208A7AD1; Fri, 18 Mar 2022 16:17:50 +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 , Jonathan Corbet 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, linux-doc@vger.kernel.org, Benjamin Tissoires Subject: [PATCH bpf-next v3 13/17] HID: bpf: implement hid_bpf_get|set_bits Date: Fri, 18 Mar 2022 17:15:24 +0100 Message-Id: <20220318161528.1531164-14-benjamin.tissoires@redhat.com> In-Reply-To: <20220318161528.1531164-1-benjamin.tissoires@redhat.com> References: <20220318161528.1531164-1-benjamin.tissoires@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_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 Export implement() outside of hid-core.c and use this and hid_field_extract() to implement the helprs for hid-bpf. Signed-off-by: Benjamin Tissoires --- changes in v3: - renamed hid_{get|set}_data into hid_{get|set}_bits 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 | 29 +++++++++++++++++++++++++++++ drivers/hid/hid-core.c | 4 ++-- include/linux/hid.h | 2 ++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/drivers/hid/hid-bpf.c b/drivers/hid/hid-bpf.c index 45c87ff47324..650dd5e54919 100644 --- a/drivers/hid/hid-bpf.c +++ b/drivers/hid/hid-bpf.c @@ -122,6 +122,33 @@ static void hid_bpf_array_detach(struct hid_device *hdev, enum bpf_hid_attach_ty } } +static int hid_bpf_get_bits(struct hid_device *hdev, u8 *buf, size_t buf_size, u64 offset, u32 n, + u32 *data) +{ + if (n > 32) + return -EINVAL; + + if (((offset + n) >> 3) >= buf_size) + return -E2BIG; + + *data = hid_field_extract(hdev, buf, offset, n); + return n; +} + +static int hid_bpf_set_bits(struct hid_device *hdev, u8 *buf, size_t buf_size, u64 offset, u32 n, + u32 data) +{ + if (n > 32) + return -EINVAL; + + if (((offset + n) >> 3) >= buf_size) + return -E2BIG; + + /* data must be a pointer to a u32 */ + implement(hdev, buf, offset, n, data); + return n; +} + static int hid_bpf_run_progs(struct hid_device *hdev, struct hid_bpf_ctx_kern *ctx) { enum bpf_hid_attach_type type; @@ -223,6 +250,8 @@ int __init hid_bpf_module_init(void) .pre_link_attach = hid_bpf_pre_link_attach, .post_link_attach = hid_bpf_post_link_attach, .array_detach = hid_bpf_array_detach, + .hid_get_bits = hid_bpf_get_bits, + .hid_set_bits = hid_bpf_set_bits, }; bpf_hid_set_hooks(&hooks); diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 3182c39db006..4f669dcddc08 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