From patchwork Fri Feb 9 13:26: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: 198863 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp849923dyd; Fri, 9 Feb 2024 05:28:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWm6UiBTOBe7+pkg+HMtjdITBPrRBKioMdMRkIi25Ccw8rLp7V61IuiruMbcfZtHYnvB8oLeSa/pw+axXdCtGQ92UuzTg== X-Google-Smtp-Source: AGHT+IEEOO2EhU1V994WH/5u4MC7Yi7iQPaNMdewV9Rdeau5adJYNDr/55W95bemggg5FJajmr20 X-Received: by 2002:a05:6870:e316:b0:210:7f85:c208 with SMTP id z22-20020a056870e31600b002107f85c208mr1594556oad.46.1707485313704; Fri, 09 Feb 2024 05:28:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707485313; cv=pass; d=google.com; s=arc-20160816; b=GcQFfFKR5QG0Yqe53gELq/9VCLCdDtVvgLb/j/SpOgHy28Wh+LoLPdEsfZBU9VeGL1 qUhwvHNuEzIocdQgEA7XRYlDna6T3f5LvlUZCYxJpiJdiGCn7O2rdyhY7TY01G7LWeOa WWcHHC9ec6RsTC69gyoGZ7VRVjCfzSkyv9luNfaLrghlVS/cnUql7s5MYe1gogmgE/4Q jOA7qZohu1d4HUJtGTdyYflWPyRgyqhSvixC3qEJtMR/tFAuNqNuW1uOEHIJCuJiI0dD K+d1NWzpraMyXmH5E2PaiGkE8t55YmUZkVRLpKisiCING8lldQBouapWg8kalCJVDBNy QDVw== 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=in/hL3DIoKS2D4/8vQJ7FxLa9snJv+vlnhCNCgiqvZo=; fh=AN7Y8QbX/wwa71FhV/3lbldXx53OwxT7BkGoN6dXk8I=; b=CtweSkqRi2Ba+4Jd/cfj90rZWqPv3TXiMnE+fZabLv1uQEBRpU3x/Rxvy8Y7l8hoZ7 CyyDdWQcy1MTNKov2pY/UJgKKWxj55nlWhw8NVbt4HEzAW8782r/zAq+h3Q39DJTdxrY ErSjHkMR8WiDQQhVECuXOTipubFosRcYQcmdoc+eEMfS7BksqbIJj7xaaK5/uqxa6KXj kkD+T9YopUL2OT47OaITW0HKe9QuJF54k2vFZcSndBXQdmB7UEPemUNMYhAI48FpRx/P hYTEZNy0Mtny+t28YiQFuY5czLl6CD7rU+OnusZgogsYg8qhepYSinwwqsGnXuURAzIM qruQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ezlIBHr8; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-59364-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59364-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCVHwDdiDS4S7PXwgLvihTVb/TNafxl+SwnNmCKXPcZN2DrQMqTENDpfA5FiJaA5Yb1qsQsY60z5Y2RV4GKVMB7kbdQTNA== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id bn3-20020a056a02030300b005dc35974c03si1821465pgb.342.2024.02.09.05.28.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 05:28:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59364-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=ezlIBHr8; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-59364-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59364-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 71EC0282153 for ; Fri, 9 Feb 2024 13:28:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9D22A45942; Fri, 9 Feb 2024 13:27:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ezlIBHr8" 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 72C373F9CF; Fri, 9 Feb 2024 13:27:17 +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=1707485237; cv=none; b=R5ACJ4noEvM/ULKOW2/u8OeX39E6lNlGl8BUPVbsm2zpL3is6Lo3K9ymLBx4DWhVbTbtEzCQ5IUX2Q9crmbjUhNoxYVq0TXJAOKSkcpMeY8FKotHFmrSn1BMUi20UdAYcXencOAXYDezEYSGV0XzvlhyBbXqFYHyonK0or+9Qbo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707485237; c=relaxed/simple; bh=GMZiOr4OnVwpc/E0GPtRWui1cKj2e0ptwmLyToHFo/Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pwH9jEqIfCNMPRUM+HxlvGCMFtczRXMyiOnXKuBiA53kTLXMBOSHmoZH2TR/AVJ/Az3dPDaVWEbP8P+GGnl0ajBW01z1LaXyygVMjyGNDF+kxtVodtscKDNtFeAEJeGjd4GWCXWAD5HTa1oIkl6paNHGgYck+1bbCbYvI/8f2ps= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ezlIBHr8; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 13B06C43609; Fri, 9 Feb 2024 13:27:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707485237; bh=GMZiOr4OnVwpc/E0GPtRWui1cKj2e0ptwmLyToHFo/Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ezlIBHr8FDGFwuOh83hZTndqab1UwL7ddkJb0CRxAlKuDmxN1daXTGQ3oyq/OaCaT m9Z6jqtymn8pSLdgAERrY3B8SsM7l0CvMN3s7VOsNjfz2efG5FPfVjBnt5PDJg/l/Y GtSKU7pyusqDo+eQD04NHf8nWP4C3LZwjdMt8/E8k2vwelUR0cITToD9rG38t8qcgM KbUgLU5sJRA5K8kQP4mjPWSETbdTNIeRWXsABur1BEMIBK+l9alXZRvW2/GurayjYi V+TBI3LiJHez/YLOsuWNRdjllH32w6zXZp2+EhIy9RW/lFyZcaBaqK9Qz5vgVlB3UA +PIvpNgSVxWug== From: Benjamin Tissoires Date: Fri, 09 Feb 2024 14:26:40 +0100 Subject: [PATCH RFC bpf-next 4/9] selftests/hid: Add test for hid_bpf_hw_output_report Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240209-hid-bpf-sleepable-v1-4-4cc895b5adbd@kernel.org> References: <20240209-hid-bpf-sleepable-v1-0-4cc895b5adbd@kernel.org> In-Reply-To: <20240209-hid-bpf-sleepable-v1-0-4cc895b5adbd@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=1707485215; l=5111; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=GMZiOr4OnVwpc/E0GPtRWui1cKj2e0ptwmLyToHFo/Y=; b=o2HnIQDENgl6W+qCjgE70J8fUDUz/welp73bDP4cWWdttZpRTk33rH5rJG8ELIuiEjLT5QI8o Z0KUm3vdgZLA0nf1+smE+Vb0vJpFcblhcd6BAviEPsxTBXTxoQXcNM1 X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790428120584886662 X-GMAIL-MSGID: 1790428120584886662 This time we need to ensure uhid receives it, thus the new mutex and condition. Signed-off-by: Benjamin Tissoires --- 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..a6d72c8ae7cd 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 */