Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2561301lqp; Mon, 25 Mar 2024 02:39:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVYAoL6Ybt9XFQTqaV6t7yuhO/RZ2WMu7eprsu7Riq0vqBZpXLfP2f+QoaUZ8Lcxzw5/erwrn77u+20ibqQLhn9AjYRfCYSs0fT9bLIXA== X-Google-Smtp-Source: AGHT+IH8Euae2QuxHctaxRLyndejL3EWVcOM9FcAB7cTz7ZPPMNpG1nJnMDVERnl/WxKEZThKbbE X-Received: by 2002:ac8:5753:0:b0:431:3478:de57 with SMTP id 19-20020ac85753000000b004313478de57mr7888455qtx.39.1711359557380; Mon, 25 Mar 2024 02:39:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711359557; cv=pass; d=google.com; s=arc-20160816; b=JJ2z+jNjgJnjQVXnsDvKuUEVBQag+76I+q54fpW6UB3RjhGZAKyjlCiJZZzRq9z3ay 04oOzve/p5+z/aJqUd7V+YYW8pImBozEThYPJAosASX33LkWQZhTL8+/ajHRmnZKnrzf VfkODTjuYqaYiGyaQlOicbv8XiLp1PDTxoesGBK2v6L8VfXRjETQUce1U1GM/t4+Onyy 0ebDbbXdQTcFQOLdLQkx1UZGQh2/x10z+lO8pRM1wWrukHujQk7E+1ldIRaUiTNX+Z9y NdVADP/bmVqT4taBWATdARFMZcDdDRCfOsBeeisGLEmyCFo9Ng/XNrppMXqdTq6RMrru 3+cg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=UqviKGE8RFcazjECQw8OJkRo9zcJ9/cBX1V5g3ayoyg=; fh=Ur4MlneoUEGzIyrtMhx6JOJ/io/L0PD0ZIm9gJYuFno=; b=h8oldrMpcYezz70gNKDAFoHZu5CDSkbEwaVfeEBRamfzK01B7LnoE+LYkeIqu3Ws7M hfjcweb41i5JqmabqIP4Xl1vqY8U2EC6+XGKQkKZHdupFNMOAM03PKgspdZwPrIKnWI2 UmxVeRQADLkYV+RAjD2bT7B4NHEW5gTagJSC9pZJLUhoclOxJ2OrIt14NADbPt6thspT YOam05DWFBJuF3WZw0NdNUtow+Vr/qKkWCJ50CDJH6euIAkKH9L/8h4xeXX9yJExEU3C 4jb9My5qp+EFW162T31hMyLpkt8U1+3KdsT8QJ+Z7nKMqGpNC+mNBge5RWbXAJBF4Yr2 AEmw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OcQC7QF5; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-115881-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-115881-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id j18-20020ac85f92000000b0043148951be8si3775824qta.769.2024.03.25.02.39.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 02:39:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-115881-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OcQC7QF5; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-115881-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-115881-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 1A3191C31BAF for ; Mon, 25 Mar 2024 09:39:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F38AD264D7F; Mon, 25 Mar 2024 03:02:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OcQC7QF5" 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 481E0146019; Sun, 24 Mar 2024 23:14:36 +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=1711322076; cv=none; b=a1VdmT6VeV9giz4fAV1EpVEWb0NG8TEAsgkJsBcWF0k3FfSHjtaJk2G9cvBcNExDsi3HIqLEtESBjtv7mHRRQBEClGyHelDbrpckvvQKAT6HNElSdGEITENyPnZWdm4gxriYFi1vBYWQZReEu7DvHvamN8Ns82Lqe6lvJFh4jYQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711322076; c=relaxed/simple; bh=fX80GMalYyvbWjTGR43Qrf2n8NK9uE12CRuupmtEfWE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EUki/146IjljTBSRiqHoJIcq2lsH8No9a2UELrYwA98Qemr2VbrI7IzJMHXxi9L/uMO5OazEqnaRGcmlGpQSsKx7Qs/2T1i9QUlQnHOxDm/agZXKOiwitaPbv6DJkAdRLbQ3ZLTyS4TfkufX5kK3JsSoqay+uVsZnSv7HRMWHfI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OcQC7QF5; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6FA2CC433C7; Sun, 24 Mar 2024 23:14:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711322076; bh=fX80GMalYyvbWjTGR43Qrf2n8NK9uE12CRuupmtEfWE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OcQC7QF5Tmk8os6GY0pZEyxq+GNDwnWj8UcxbvwafTmcSpz9v0Z5u0r/LrWZZGcDi QTwdOF//0wicKR/U4Vhe3njFKZUsZ2VYeEKUZ6vPXCZHb78ZeBTn9+dFOi2qavpqi9 l2SNYGb8gu0+BgGUkdLJpBPLQuTTMiHGO1SUZc95JfhpJPdrRSELSyzxv3woU42tEP S1/3+IqhHhwdGdkb9jIgwyLmGTwD2+vlkdiRcDIOcFpA9AP85zJ3B6te3+VrNxJIQl OapXWnJU4DpCKiXaPIsptb9F5ZXEbI5NYw40tjCBJkkH6JFSMR3CV+0jwA3gMDPt+H ACiiP7KNA6oOQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Andrii Nakryiko , Daniel Borkmann , Stanislav Fomichev , Sasha Levin Subject: [PATCH 6.1 149/451] selftests/bpf: Add global subprog context passing tests Date: Sun, 24 Mar 2024 19:07:05 -0400 Message-ID: <20240324231207.1351418-150-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324231207.1351418-1-sashal@kernel.org> References: <20240324231207.1351418-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Andrii Nakryiko [ Upstream commit e2b5cfc978f871996d1f8667515c0e06b33e620e ] Add tests validating that it's possible to pass context arguments into global subprogs for various types of programs, including a particularly tricky KPROBE programs (which cover kprobes, uprobes, USDTs, a vast and important class of programs). Signed-off-by: Andrii Nakryiko Signed-off-by: Daniel Borkmann Acked-by: Stanislav Fomichev Link: https://lore.kernel.org/bpf/20230216045954.3002473-4-andrii@kernel.org Stable-dep-of: 879bbe7aa4af ("bpf: don't infer PTR_TO_CTX for programs with unnamed context type") Signed-off-by: Sasha Levin --- .../bpf/prog_tests/test_global_funcs.c | 2 + .../bpf/progs/test_global_func_ctx_args.c | 104 ++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/test_global_func_ctx_args.c diff --git a/tools/testing/selftests/bpf/prog_tests/test_global_funcs.c b/tools/testing/selftests/bpf/prog_tests/test_global_funcs.c index 2ff4d5c7abfce..e0879df38639b 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_global_funcs.c +++ b/tools/testing/selftests/bpf/prog_tests/test_global_funcs.c @@ -18,6 +18,7 @@ #include "test_global_func15.skel.h" #include "test_global_func16.skel.h" #include "test_global_func17.skel.h" +#include "test_global_func_ctx_args.skel.h" void test_test_global_funcs(void) { @@ -38,4 +39,5 @@ void test_test_global_funcs(void) RUN_TESTS(test_global_func15); RUN_TESTS(test_global_func16); RUN_TESTS(test_global_func17); + RUN_TESTS(test_global_func_ctx_args); } diff --git a/tools/testing/selftests/bpf/progs/test_global_func_ctx_args.c b/tools/testing/selftests/bpf/progs/test_global_func_ctx_args.c new file mode 100644 index 0000000000000..7faa8eef0598b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_global_func_ctx_args.c @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ +#include "vmlinux.h" +#include +#include +#include +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +static long stack[256]; + +/* + * KPROBE contexts + */ + +__weak int kprobe_typedef_ctx_subprog(bpf_user_pt_regs_t *ctx) +{ + return bpf_get_stack(ctx, &stack, sizeof(stack), 0); +} + +SEC("?kprobe") +__success +int kprobe_typedef_ctx(void *ctx) +{ + return kprobe_typedef_ctx_subprog(ctx); +} + +#define pt_regs_struct_t typeof(*(__PT_REGS_CAST((struct pt_regs *)NULL))) + +__weak int kprobe_struct_ctx_subprog(pt_regs_struct_t *ctx) +{ + return bpf_get_stack((void *)ctx, &stack, sizeof(stack), 0); +} + +SEC("?kprobe") +__success +int kprobe_resolved_ctx(void *ctx) +{ + return kprobe_struct_ctx_subprog(ctx); +} + +/* this is current hack to make this work on old kernels */ +struct bpf_user_pt_regs_t {}; + +__weak int kprobe_workaround_ctx_subprog(struct bpf_user_pt_regs_t *ctx) +{ + return bpf_get_stack(ctx, &stack, sizeof(stack), 0); +} + +SEC("?kprobe") +__success +int kprobe_workaround_ctx(void *ctx) +{ + return kprobe_workaround_ctx_subprog(ctx); +} + +/* + * RAW_TRACEPOINT contexts + */ + +__weak int raw_tp_ctx_subprog(struct bpf_raw_tracepoint_args *ctx) +{ + return bpf_get_stack(ctx, &stack, sizeof(stack), 0); +} + +SEC("?raw_tp") +__success +int raw_tp_ctx(void *ctx) +{ + return raw_tp_ctx_subprog(ctx); +} + +/* + * RAW_TRACEPOINT_WRITABLE contexts + */ + +__weak int raw_tp_writable_ctx_subprog(struct bpf_raw_tracepoint_args *ctx) +{ + return bpf_get_stack(ctx, &stack, sizeof(stack), 0); +} + +SEC("?raw_tp") +__success +int raw_tp_writable_ctx(void *ctx) +{ + return raw_tp_writable_ctx_subprog(ctx); +} + +/* + * PERF_EVENT contexts + */ + +__weak int perf_event_ctx_subprog(struct bpf_perf_event_data *ctx) +{ + return bpf_get_stack(ctx, &stack, sizeof(stack), 0); +} + +SEC("?perf_event") +__success +int perf_event_ctx(void *ctx) +{ + return perf_event_ctx_subprog(ctx); +} -- 2.43.0