Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp1124285lqt; Fri, 7 Jun 2024 08:37:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWoAagr/7t8ipm1Ii4lwRAG0YSB0Y9TzuoQGhVgGLqaYWfqxwkj0b9KNF8klguDAAw1r0uTfikbuTneH0f1RJ5FqkSAt0RyKcDzKyihiw== X-Google-Smtp-Source: AGHT+IH3ASH7G2WElKBB80f7isDUFxUHx2evupu1M6qGVSLPiJTveRsahWL0tDchFP8tNunmhFDj X-Received: by 2002:a05:6e02:144d:b0:374:9c31:1206 with SMTP id e9e14a558f8ab-3758034c69emr33725295ab.17.1717774667423; Fri, 07 Jun 2024 08:37:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717774667; cv=pass; d=google.com; s=arc-20160816; b=yZlV1G/qu8A6V7bIZXKqoCAb6shkWwzQSJF27dM9D8u2Qzh45F7+tdulJnm4V3Vh+O wfAr6d/9G+jGo+Nvgb5+QQhFalmPCNOVJJPqFuz9DgoHNSwYL1+tUcll3DP1CUFPoaJP B8XxFh/dmb8l5o8Yo+YlXK+Wk6eufK9S3MYR6ShBFSbOKcYbmalEcJx4DQt/x6AT+l0V ZfSqGCT4/6qQkWbcjhb2H47aGBc9mq8LtJ5XxrsiOngquoaxgz6PjmLEg8oI8BGZEzYZ 5DkuOJ3KjRcmQWyB/3deR/UQfU96+D518miKbJeRWmgKkVo6/6pM90LIqbklro9+Wusw MZJQ== 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=/cm3f5L86+BmiHSXgCAev282o7N45Hcsf20CeFIYJkM=; fh=YpHnV/6uyiPbG6kMterX8knOBa279otQWiSdSsr99Bc=; b=LjcSWRub0SQNAEqp0iWE0xD+Ncruh6CqjMcqj8ZxHyORxMHE2soYbMLvKkbCDbaBkG 1LqyodqYYbb8WVWs88rleF73O4ma4KyVoprDOa3jcRkqxIos02SMWFbq96Mx4O5299BU i3ocf+R7xI06UyZCKShNfopyi+ds+1PFzKDGGnbBl8sMkkdhXLGzLi+RM6I3fInNFjyG W4nW5Gyjm+xDQwOFg1gqwRHYegoE2CyjwoLHZ6Y771NDs+YJJ6Jo/eLmnMe2PpEI/t9B TZeS/pa8giRBePp8yP4jJpPqg9QW/gOhaetseXzRRnYgmhVWfnzjj6VwhOwKIdY3DDr3 iaFw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KkPmT2Pq; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-206400-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206400-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6de2a186079si1630927a12.783.2024.06.07.08.37.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 08:37:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-206400-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KkPmT2Pq; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-206400-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206400-linux.lists.archive=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 56FE7284737 for ; Fri, 7 Jun 2024 15:31:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D4AFA19D082; Fri, 7 Jun 2024 15:28:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KkPmT2Pq" 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 DC3121990CE; Fri, 7 Jun 2024 15:28:53 +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=1717774134; cv=none; b=Uo35OXDP/FSi2MkGn7fjvmcFvfVojg51BAmqveEjYzmIGylFOtpLm30zo71Gv4TjYCndWG10+2JIaVNFKKrz/jhqEkqtb+YRhNxDcPnal7etA6FmJwJY4YaaiGxVhh6aw2+kgSwPSHc4LGMUrcanZAGq5bVmfxw7pCrFj2IjS2I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717774134; c=relaxed/simple; bh=A/OgJAoyz1jfOP1ujKz9EudGw2c1CaLUc4HxTAiFUBw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CsMfJbxIvydrU5XdDBKzb8XQmBWmoJZ5m5tL0Wjf9QTjwX0mHNwSRmqYHKxR0TKcAIDjU5qkD3HTcjpjjmAfek65fvJbp7IsTZSYWhIEzJfVhCmupEIwM7zeSeoVT+drCRJqxPTCozFPCDh5gMaBhR3zmUedqC+0m9rePocWkZY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KkPmT2Pq; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9752BC4AF07; Fri, 7 Jun 2024 15:28:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717774133; bh=A/OgJAoyz1jfOP1ujKz9EudGw2c1CaLUc4HxTAiFUBw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=KkPmT2Pq87JghkUNxCrqiCLkgwsimlIIj+YaVDk0G+ThK8FmOy1StGdnEaiGiCNIw Dgd+piOfgVToDVLdBcF89vSMIpZpN4EIgtFgTghgdaMuOL7I9+hv0VgOkdD67uS7C5 w4buZHMo7JwRLW/Aa209eIu1LnlFzWH0RYI0YKged0aTWGaytGsV8lFUEvCcMZzBrr T6DrmIJaoW4BtydPEuYVMxMFlQMnAuYp7m2hbObm1oeGvoBJdQJl7Q68xZjXMbu7Y6 3ucE687Ma0n4IlGf7H/bVoHj3coTfCUi/JDf+qI0TJ8fxOZTU95fJbIQocoLSDnjcz QAPffffkug5YQ== From: Benjamin Tissoires Date: Fri, 07 Jun 2024 17:28:29 +0200 Subject: [PATCH HID v2 09/16] selftests/hid: add subprog call test Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240607-hid_bpf_struct_ops-v2-9-3f95f4d02292@kernel.org> References: <20240607-hid_bpf_struct_ops-v2-0-3f95f4d02292@kernel.org> In-Reply-To: <20240607-hid_bpf_struct_ops-v2-0-3f95f4d02292@kernel.org> To: Shuah Khan , Jiri Kosina , Jonathan Corbet , Alexei Starovoitov Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1717774109; l=2901; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=A/OgJAoyz1jfOP1ujKz9EudGw2c1CaLUc4HxTAiFUBw=; b=WKHdgp6sz43nGVMHWLGQNJDDB4b1BpfKEc2/DH3NXYA79bUelM6WvtPrR743fW19VfA2TTW+8 ErNo+3thuI4Ai7weQ7lu4CFzbohtb+vDXma/AFQ+6VFXt5UflzCYuDN X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= I got a weird verifier error with a subprog once, so let's have a test for it. Signed-off-by: Benjamin Tissoires --- no changes in v2 --- tools/testing/selftests/hid/hid_bpf.c | 41 +++++++++++++++++++++++++++++++++ tools/testing/selftests/hid/progs/hid.c | 24 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftests/hid/hid_bpf.c index 967dfe6b58cb..45e173db35bd 100644 --- a/tools/testing/selftests/hid/hid_bpf.c +++ b/tools/testing/selftests/hid/hid_bpf.c @@ -638,6 +638,47 @@ TEST_F(hid_bpf, raw_event) ASSERT_EQ(buf[2], 52); } +/* + * Attach hid_first_event to the given uhid device, + * retrieve and open the matching hidraw node, + * inject one event in the uhid device, + * check that the program sees it and can change the data + */ +TEST_F(hid_bpf, subprog_raw_event) +{ + const struct test_program progs[] = { + { .name = "hid_subprog_first_event" }, + }; + __u8 buf[10] = {0}; + int err; + + LOAD_PROGRAMS(progs); + + /* inject one event */ + buf[0] = 1; + buf[1] = 42; + uhid_send_event(_metadata, self->uhid_fd, buf, 6); + + /* 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[2], 47); + + /* inject another event */ + memset(buf, 0, sizeof(buf)); + buf[0] = 1; + buf[1] = 47; + uhid_send_event(_metadata, self->uhid_fd, buf, 6); + + /* 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[2], 52); +} + /* * Ensures that we can attach/detach programs */ diff --git a/tools/testing/selftests/hid/progs/hid.c b/tools/testing/selftests/hid/progs/hid.c index 614f1aa32649..2e7e5a736dc6 100644 --- a/tools/testing/selftests/hid/progs/hid.c +++ b/tools/testing/selftests/hid/progs/hid.c @@ -35,6 +35,30 @@ struct hid_bpf_ops first_event = { .hid_id = 2, }; +int __hid_subprog_first_event(struct hid_bpf_ctx *hid_ctx, enum hid_report_type type) +{ + __u8 *rw_data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 3 /* size */); + + if (!rw_data) + return 0; /* EPERM check */ + + rw_data[2] = rw_data[1] + 5; + + return hid_ctx->size; +} + +SEC("?struct_ops/hid_device_event") +int BPF_PROG(hid_subprog_first_event, struct hid_bpf_ctx *hid_ctx, enum hid_report_type type) +{ + return __hid_subprog_first_event(hid_ctx, type); +} + +SEC(".struct_ops.link") +struct hid_bpf_ops subprog_first_event = { + .hid_device_event = (void *)hid_subprog_first_event, + .hid_id = 2, +}; + SEC("?struct_ops/hid_device_event") int BPF_PROG(hid_second_event, struct hid_bpf_ctx *hid_ctx, enum hid_report_type type) { -- 2.44.0