From patchwork Wed Feb 14 17:18: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: 201062 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1372809dyb; Wed, 14 Feb 2024 09:24:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXLsMxABX2YuTNYXXQJheoIje7j25MdcmjRMqHYFJ50gULw6/L8705b3vi5eYMB/5bVX7UQNNgQZKY+faP9SeZC5w84mA== X-Google-Smtp-Source: AGHT+IGOintDo2F7ZDR6k8UwyOcEhfLo/ANUI5SwJy/Oz7xvbA9pr6yPPQgIRVbjbDprfbQkbZtg X-Received: by 2002:a05:6a21:3a87:b0:1a0:70db:43e5 with SMTP id zv7-20020a056a213a8700b001a070db43e5mr1657461pzb.5.1707931478028; Wed, 14 Feb 2024 09:24:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707931478; cv=pass; d=google.com; s=arc-20160816; b=TyySuKdoogS2LhT4/6P9jMjpuM8UFwomS+fdPyUp2SsdILQ+LUZHL97lvwggvIGA69 gIGWtTyuqaGBqvS51H2ttfk2xPRrt5kEJKlbmzRWYzubgeXvEvbp2tOqWnLfCZAiKXJj FA7ShS5/MYWkqfkE8XjdVruTpCVbIZoV3OvZFxARz1emjffV4EF+YgTdR3s4VuKkN2fj Tyx1ME5bzKr3Lav4SDCNrobqhB1JhOkXwN/z4zqeTPG3kSlv2b0Y5DikKAvrAkCCByFS LDjjvAI1wbVs2Ddu4TBRTQ+vZEALFHuce8m4CRpzbKaRfg2/nScyTlsCXRLp+DhLmVI+ jiBA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=WAUzfG6hYgBcOOGAYlUUPkzDYccZxvYPsLBr1ZdP/hs=; fh=698MDQ+r6VOgyBxcKOw8SdWV5XKXov7ABtfDpXTRaro=; b=RVs+3HEKct7VGfL7M3ZqwNldZfRJk+lrCrxuqqCvGjfY0KOpC1QoOH85neJL+u8SX6 ZSj5MofVUUFlv0R8e30Lt/eToMpl1wychLuhOFCzfOZynFzI1FWpZR1+z6mWSguqDxt8 W9Th28+U38P/dqIOxnNmhPdSj6VV8x4paPmTIqyxoXzDxAd9TNRh9xFxoSRB5XfqDPGq w8upbFYWNEpvLLaTEEbjfbbom/5QfJJO5/vMqpTvXvJxO7aX+vP77eXCtNlu+xtLdtWG f1x7WQNDQiddaKLYBkzWntp2mIhU6UME3hP4Wh3cb4acKFp5mNVCBqcAkUBeFJid4EZJ aurw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hjHMlKL5; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65647-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65647-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCWYnC0DTtrMz4we7vHqdSuyFz3q3Yo4/Rnfe5y9Aq+ig28zrWMJ4tCWPZODBnFGetsEYt6rNmdaH6CYWauSctI74UJz7A== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id c21-20020a056a000ad500b006e03381d1ddsi8932921pfl.343.2024.02.14.09.24.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:24:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65647-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hjHMlKL5; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-65647-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65647-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 926372866B1 for ; Wed, 14 Feb 2024 17:22:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 97EC91272B9; Wed, 14 Feb 2024 17:19:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hjHMlKL5" 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 A291286635; Wed, 14 Feb 2024 17:19:35 +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=1707931175; cv=none; b=Yx1QaHOQyCfyyLwC7pHzA5CCwv8vyOJwjuvIPqDO+rKfNQulnWR4L60pWH9B7jKktNVocZ+zPk68hcPqQyX4IoMZeVdjvZrQSrRBefTjXHpJYS4dKQ2SEIIR7/e/0p6NtURD1/7ofH7q7/fVAJ5WCH1L++NxijsXUpwlP1kZXw4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707931175; c=relaxed/simple; bh=/pTARDtbfM9ZCY8QYIJ5HFNo8IvYrXNQ2OhOc+QCWlE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lx2UwsnVsquWwCWKq5LJROL2OOZENR9DOVVmdvH9lmfe+l/Ej9tDdKv7xIWNMvfWnsJFLfV7/ZsVy7NkVLO2f/meTGuLFYUgKpA5g6nweJFdlC2qlZLF6h8da3SA8PzB0yr6Y5nnWCtz8UnYOSxPyieboYqJtq94hr/FrSnm5L4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hjHMlKL5; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2CCA7C433F1; Wed, 14 Feb 2024 17:19:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707931175; bh=/pTARDtbfM9ZCY8QYIJ5HFNo8IvYrXNQ2OhOc+QCWlE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hjHMlKL5v533+lxXsepilERKuJP0vI5wZnKexrHsEk16eWjwAGAh3HKGr8uK0y5Av vRQkk1ZcIYw+ZGEZEOWaYwTq0b/YDnUjOsyl3e/KfineoEYC983Yu7GvhVmSIBLzbx Z7oTG2PYY3UDHg8FQ+e2NkjSd/FJ92Rc5VYAiP9pjeplXWKFE+mzPKYcmksULLJ6uT c+14mu5FOqIuzPLqLlncT5+gcpy054dgL4wLVnI+Df6TWcjoDDU38+cG8cdffZ9hNY 5dlZWSTpGKboRxki1CPdEk5SLvW0v9huIkQ+0siIm68k+gsw+Pju3SkEVNxUptlXb3 qktflmntflJEw== From: Benjamin Tissoires Date: Wed, 14 Feb 2024 18:18:37 +0100 Subject: [PATCH RFC bpf-next v2 08/10] selftests/hid: add tests for hid_bpf_input_report Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240214-hid-bpf-sleepable-v2-8-5756b054724d@kernel.org> References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> In-Reply-To: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707931135; l=4206; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=/pTARDtbfM9ZCY8QYIJ5HFNo8IvYrXNQ2OhOc+QCWlE=; b=0e+T6btGJ2ogRaR6qMnKUh8WvJ9REB9kjkC3JsahqEIX/AkTon8LzNinPzSMManOE0T/+3O82 EcoHAveRee2Dd3pj5VtCyjmz8X3KQ0g51uFOgF76N96kKls0hpYYLzA X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790895957853126224 X-GMAIL-MSGID: 1790895957853126224 Usual way of testing, we call the function and ensures we receive the event Signed-off-by: Benjamin Tissoires --- 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 */