Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp2645543lqb; Tue, 28 May 2024 06:31:15 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVtUNS/spBAJW7epANlAK9YBBA12qrhMI0D/sDa5rvdsHDKjLJKNn/NXPnIMEJgKbubJnd64ka2aKtTskGxEOAf6HukIsFQhKZ7nKSu7Q== X-Google-Smtp-Source: AGHT+IEHbA952/SG7oKE15NDh21qUnT3h15VBnvfERcq2kWSNXZ2Eg9eLEkMeTySrowcG9ZPc+so X-Received: by 2002:a05:6a00:4489:b0:6f4:b20e:a08a with SMTP id d2e1a72fcca58-6f8f3f9711bmr13643618b3a.29.1716903075307; Tue, 28 May 2024 06:31:15 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716903075; cv=pass; d=google.com; s=arc-20160816; b=S4PUWzN8LhZtYrZ7Y4P1TyU4oTgftTvtQ76aazE64mRt36J8GNmy0h2qabmr5VDCX3 CoqsUW4lR+k2UsQl2yFGnPUGTfIrHHKBr3ZUSg6HrgCWWGi+7+nI6d9/fUNxfRWnssI2 c6lbgRGYTEVmXVr6XnpRb6Q8OoZde/Y+PyGlYkt2xJcaD1h4lOq1S2uj/OAqY51zWHwM veuCW+Laoc7yPymRoI7SHsEMSQpVYJGctDy3KJnP+MN+XQMLvhzjD0hqyuSfaIWpmuY8 iahvHMc7SJxGndMtkQZea6PI2DeUQJoMGSBZi84jNggbWNlBGCWMc1cplGo4lNQqXw7x SL1g== 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=jp8lZetEhILPLIXMFJH1A1zP+/IdU/VnRxnA5C/U9Io=; fh=YpHnV/6uyiPbG6kMterX8knOBa279otQWiSdSsr99Bc=; b=Evf6CAaY9KOSYzEhcQkx9m5YUTb1xLiBcQmvckC+ooaMnOGU5SW8ba7w41o3g1CBwS 4YthSPx5USnWgcMtDg3tLspNy28wfOLspGL90NkHm5nLB5LsO/onnEbRtnoi0bBO41o5 lLYpLKqnF/2YmXK2h20/LW08pgxcUyZKrFsKTTGeg72j5rd/pnGYekIxFBHkH7V1HyWi taWkHw3Ysl8h092RvBaCs26bW9Sv7tKdZ/pxKOuHUDjSeOKBtN2BYdVXQK9F5SjkvVEN TVgZG2Jr+iXPQYaRcPYIljn1t+pHF7HOuwBu0vd4ekwTUUnN/ZJ9xqmD5jhBEzOqfaIg 1B3Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=a4R+nlNL; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-192386-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192386-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d2e1a72fcca58-6f8fc36c70csi7910509b3a.98.2024.05.28.06.31.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 06:31:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-192386-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=a4R+nlNL; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-192386-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192386-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 225BEB24442 for ; Tue, 28 May 2024 13:18:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 581BE171E54; Tue, 28 May 2024 13:15:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="a4R+nlNL" 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 72B5E171679; Tue, 28 May 2024 13:15:37 +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=1716902137; cv=none; b=eTx6B2MUjS+1osp8SFMCLwk895QC+pAy6AeYMfE4/6A6ZnE10uig/yhfswZBpTNnYTdOoKkDIaq0L/N5qJCA6WcvJSUylW8oc5HW4zpkqnJx5/RlwkxSmxiFZ3uxNLTZhD8XA+Y3n+jdR0GdK/kJSRJ3nY+noBLQjWwB+KcaQRw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716902137; c=relaxed/simple; bh=r5PRmmxJaUk08rAPSCBzO9oSNG9qSKMUcqLqCcrKXiQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NTl8pU3S6m8Mb3m8G6TeRi9guMXkdnD0WO8cX9M1x9N0QoRejp3d9KPLpBpNe13JE36ZTGBiLt0C8JHTX2pq3EonHIWFA08NcyX+cdzzLtvrpn3oYJqdnyDWEoM+7Kg8+4PdmfPYpxZcopn7ySbWljtIhk5kuCjIzwvKiBctABw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=a4R+nlNL; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74A32C3277B; Tue, 28 May 2024 13:15:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716902137; bh=r5PRmmxJaUk08rAPSCBzO9oSNG9qSKMUcqLqCcrKXiQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=a4R+nlNLumi2QM5Rv8uuN48nmEzt4Pj1X60btY+MADHG3O4nmsdzG1UjPRdEskAUl Ql0R47Bd6wyKxm/tPeHOonVj6DTzPGdZAnIb/lZxylw3c64xx7/aITCLFc/MCYFepW 8gsQtDBedaEpJbFGKGJOOOCyDnQgy096qcRGnZewCQCDYPgKPE2p/PGPcLyNikQ0mz /RwhOOEvyrdqQmujoBPkpZ7pI1etcYXcGbqZwI/JQs6RjZQv4X0dRXtn0ikW5LdhIc qFbSWxFyP7gnix/fKuxSMACmsZoT5YyAElCoduX5sBzY1GVrqLfDFNPmAq8lan19jt CY59CIpm7FFUA== From: Benjamin Tissoires Date: Tue, 28 May 2024 15:14:47 +0200 Subject: [PATCH HID 09/13] 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: <20240528-hid_bpf_struct_ops-v1-9-8c6663df27d8@kernel.org> References: <20240528-hid_bpf_struct_ops-v1-0-8c6663df27d8@kernel.org> In-Reply-To: <20240528-hid_bpf_struct_ops-v1-0-8c6663df27d8@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=1716902113; l=2874; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=r5PRmmxJaUk08rAPSCBzO9oSNG9qSKMUcqLqCcrKXiQ=; b=Bb/kgLvMa2H3hEIhee/vec2iqeL/yB3axdtRrtdDBokZLYyrCJOgC7ir2UEXwkV6TTffDYk5r EdVtWZdCRUKB+HLe/6fAzN1E9G7QqrJmHMO38lFOEg/HkoJhS0gQ8UR 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 --- 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 9adace26e8aa..efa3c7d292bc 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