Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2247133pxm; Fri, 4 Mar 2022 12:15:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJwsZicFtkOaYdaJB7BBSFaW+OtBsauveN1heYIFx3HKMQE/e55H5cjfMvgEKnrBo0egt8av X-Received: by 2002:aa7:8890:0:b0:4e1:b25b:634c with SMTP id z16-20020aa78890000000b004e1b25b634cmr294805pfe.31.1646424933366; Fri, 04 Mar 2022 12:15:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646424933; cv=none; d=google.com; s=arc-20160816; b=AVLNggqujJ9bmF6R6LohPErVhkpkWRknudGXCzEE/1/kC+4l6IgcgvsdFNnZhn/2K9 L71kYPM0ss39236+GTQ8Bm3UOuW3cJDh2iMMI1eJnVdciA9xcllksRxXtE93OL5RWUmw PuNO9SqlBmjwmxu2kkH6hge8NKk+r2z5OHl069a64fX2JPOUZ0oxGa0kEvJ39GHK4VVP Amg42dUhY5qm171O8FuwACiY46i48lK+GICyjw6lQvq9pUCxKH6NyZHkg7OuFGd0zIfO MUT9+E2b6xBIx6GRUm93JIl5BmOwqEPXPVYo9jkYqZsSmv9Vi0EmI82GV04wv2QVoD2F SMdA== 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=6gple0WPBUTESb5NzCZwiTEsdsSCzabkmqhh6wKvZG0=; b=Qa4vpBwgC15ipe07Owm7e/lxMPILh4XqvZ8s3oTG45ml4nHM4AJpcX+Z4YctsMMdyU SiMV/2XILLgfV1qNF7NJeBvEa188BEG05nRs0fDwg6lcldZ/6mC59MX8v2T5bfj38jeU w9jk1mRFo4NhV5QAuv1vES7B2ZY9c5qo8f1x1UHE7vM3SczkcIdihAJ2RoAE1k1zn4XL OTMIExVrQzqFKiziqZ24wzk3XgxNesazSuMSIl86TJRDC7nc6ZzPXBCPtWXNtSSfKrgD oMEh8GsgVAID978MS5X9I1VZEp/2vLJ3eKYn8x5JpSrxY83knIeUW2HO8jX0/CnIKxxA BWZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PJrJnQ61; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id v14-20020a63480e000000b003728e29fef1si5215378pga.491.2022.03.04.12.15.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 12:15:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PJrJnQ61; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 14D5812552D; Fri, 4 Mar 2022 11:28:36 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235942AbiCDRfu (ORCPT + 99 others); Fri, 4 Mar 2022 12:35:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241394AbiCDRfR (ORCPT ); Fri, 4 Mar 2022 12:35:17 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 779061D21FA for ; Fri, 4 Mar 2022 09:34:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646415248; 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=6gple0WPBUTESb5NzCZwiTEsdsSCzabkmqhh6wKvZG0=; b=PJrJnQ61yVIMxErFmFEmm7l8Gv8PDcjjM431BTcOylyYcjpkf9lYAT31qdRz/6PzslLmX3 jQOilvWraA9vDmEGr3IeB2qFH/fjzFUt9TGw9evjXqwTBF+RQZ59EE5idpQtA6m6ebANg6 /FJ/15v1RfEO5Jt+ytIoASgO/JmQJeo= 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-643-s2olJurQOcWQo2M85ZZEbQ-1; Fri, 04 Mar 2022 12:34:05 -0500 X-MC-Unique: s2olJurQOcWQo2M85ZZEbQ-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 BE03E18766D0; Fri, 4 Mar 2022 17:34:02 +0000 (UTC) Received: from plouf.redhat.com (unknown [10.39.192.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 50CC686596; Fri, 4 Mar 2022 17:33: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 19/28] bpf/hid: add bpf_hid_raw_request helper function Date: Fri, 4 Mar 2022 18:28:43 +0100 Message-Id: <20220304172852.274126-20-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 When we are in a user_event context, we can talk to the device to fetch or set features/outputs/inputs reports. Add a bpf helper to do so. This helper is thus only available to user_events, because calling this function while in IRQ context (any other BPF type) is forbidden. Signed-off-by: Benjamin Tissoires --- changes in v2: - split the series by bpf/libbpf/hid/selftests and samples --- include/linux/bpf-hid.h | 2 ++ include/uapi/linux/bpf.h | 8 ++++++++ kernel/bpf/hid.c | 26 ++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 8 ++++++++ 4 files changed, 44 insertions(+) diff --git a/include/linux/bpf-hid.h b/include/linux/bpf-hid.h index 4cf2e99109fe..bd548f6a4a26 100644 --- a/include/linux/bpf-hid.h +++ b/include/linux/bpf-hid.h @@ -100,6 +100,8 @@ struct bpf_hid_hooks { u64 offset, u32 n, u8 *data, u64 data_size); int (*hid_set_data)(struct hid_device *hdev, u8 *buf, size_t buf_size, u64 offset, u32 n, u8 *data, u64 data_size); + int (*hid_raw_request)(struct hid_device *hdev, u8 *buf, size_t size, + u8 rtype, u8 reqtype); }; #ifdef CONFIG_BPF diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index b3063384d380..417cf1c31579 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5121,6 +5121,13 @@ union bpf_attr { * Return * The length of data copied into ctx->event.data. On error, a negative * value is returned. + * + * int bpf_hid_raw_request(void *ctx, void *buf, u64 size, u8 rtype, u8 reqtype) + * Description + * communicate with the HID device + * Return + * 0 on success. + * negative value on error. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -5317,6 +5324,7 @@ union bpf_attr { FN(copy_from_user_task), \ FN(hid_get_data), \ FN(hid_set_data), \ + FN(hid_raw_request), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/kernel/bpf/hid.c b/kernel/bpf/hid.c index de003dbd7d01..653d10c0f4e6 100644 --- a/kernel/bpf/hid.c +++ b/kernel/bpf/hid.c @@ -86,6 +86,28 @@ static const struct bpf_func_proto bpf_hid_set_data_proto = { .arg5_type = ARG_CONST_SIZE_OR_ZERO, }; +BPF_CALL_5(bpf_hid_raw_request, void*, ctx, void*, buf, u64, size, + u8, rtype, u8, reqtype) +{ + struct hid_bpf_ctx *bpf_ctx = ctx; + + if (!hid_hooks.hid_raw_request) + return -EOPNOTSUPP; + + return hid_hooks.hid_raw_request(bpf_ctx->hdev, buf, size, rtype, reqtype); +} + +static const struct bpf_func_proto bpf_hid_raw_request_proto = { + .func = bpf_hid_raw_request, + .gpl_only = true, /* hid_raw_request is EXPORT_SYMBOL_GPL */ + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_PTR_TO_MEM, + .arg3_type = ARG_CONST_SIZE_OR_ZERO, + .arg4_type = ARG_ANYTHING, + .arg5_type = ARG_ANYTHING, +}; + static const struct bpf_func_proto * hid_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -94,6 +116,10 @@ hid_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_hid_get_data_proto; case BPF_FUNC_hid_set_data: return &bpf_hid_set_data_proto; + case BPF_FUNC_hid_raw_request: + if (prog->expected_attach_type != BPF_HID_DEVICE_EVENT) + return &bpf_hid_raw_request_proto; + return NULL; default: return bpf_base_func_proto(func_id); } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index b3063384d380..417cf1c31579 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5121,6 +5121,13 @@ union bpf_attr { * Return * The length of data copied into ctx->event.data. On error, a negative * value is returned. + * + * int bpf_hid_raw_request(void *ctx, void *buf, u64 size, u8 rtype, u8 reqtype) + * Description + * communicate with the HID device + * Return + * 0 on success. + * negative value on error. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -5317,6 +5324,7 @@ union bpf_attr { FN(copy_from_user_task), \ FN(hid_get_data), \ FN(hid_set_data), \ + FN(hid_raw_request), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper -- 2.35.1