linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: bpf@vger.kernel.org
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>, Shuah Khan <shuah@kernel.org>,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: [PATCH bpf-next v1 2/2] selftests/bpf: Add selftests for context detection kfuncs
Date: Wed, 22 Oct 2025 19:33:33 +0800	[thread overview]
Message-ID: <20251022113412.352307-3-jiayuan.chen@linux.dev> (raw)
In-Reply-To: <20251022113412.352307-1-jiayuan.chen@linux.dev>

Add selftests for the newly introduced context detection kfuncs.

The tests verify that each kfunc correctly identifies its respective
execution context by triggering different contexts

Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
 .../selftests/bpf/prog_tests/context.c        | 32 ++++++++++++++++++
 .../selftests/bpf/progs/context_prog.c        | 33 +++++++++++++++++++
 2 files changed, 65 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/context.c
 create mode 100644 tools/testing/selftests/bpf/progs/context_prog.c

diff --git a/tools/testing/selftests/bpf/prog_tests/context.c b/tools/testing/selftests/bpf/prog_tests/context.c
new file mode 100644
index 000000000000..f09d24069941
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/context.c
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <error.h>
+#include <test_progs.h>
+#include "context_prog.skel.h"
+
+void test_context(void)
+{
+	struct context_prog *skel = NULL;
+
+	skel = context_prog__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "loading prog fail"))
+		return;
+
+	context_prog__attach(skel);
+	getuid();
+	sleep(5);
+
+	if (!ASSERT_EQ(1, skel->bss->in_hardirq, "hardirq not triggered"))
+		goto out;
+	if (!ASSERT_EQ(1, skel->bss->in_softriq, "softirq not triggered"))
+		goto out;
+	if (!ASSERT_EQ(1, skel->bss->in_task, "task context not triggered"))
+		goto out;
+out:
+	context_prog__destroy(skel);
+}
+
+void test_bpf_context(void)
+{
+	if (test__start_subtest("context"))
+		test_context();
+}
diff --git a/tools/testing/selftests/bpf/progs/context_prog.c b/tools/testing/selftests/bpf/progs/context_prog.c
new file mode 100644
index 000000000000..81e21f684a84
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/context_prog.c
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+
+int in_hardirq = 0;
+int in_softriq = 0;
+int in_task = 0;
+
+SEC("tp/irq/irq_handler_entry")
+int trace_irq_handler_entry(const void *ctx)
+{
+	in_hardirq = bpf_in_hardirq_context();
+	return 0;
+}
+
+SEC("tp/irq/softirq_entry")
+int trace_softirq_entry(const void *ctx)
+{
+	in_softriq = bpf_in_softirq_context();
+	return 0;
+}
+
+SEC("tp/syscalls/sys_enter_getuid")
+int trace_syscall(const void *ctx)
+{
+	in_task = bpf_in_task_context();
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
-- 
2.43.0


      parent reply	other threads:[~2025-10-22 11:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-22 11:33 [PATCH bpf-next v1 0/2] bpf: Add kfuncs and selftests for detecting execution context and selftests Jiayuan Chen
2025-10-22 11:33 ` [PATCH bpf-next v1 1/2] bpf: Add kfuncs for detecting execution context Jiayuan Chen
2025-10-22 12:55   ` Leon Hwang
2025-10-24 16:14   ` kernel test robot
2025-10-22 11:33 ` Jiayuan Chen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251022113412.352307-3-jiayuan.chen@linux.dev \
    --to=jiayuan.chen@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).