mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mike Bazov via B4 Relay <devnull+mikeba.sentinelone.com@kernel.org>
To: bpf@vger.kernel.org, Alexei Starovoitov <ast@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
	 Eduard Zingerman <eddyz87@gmail.com>,
	 Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Song Liu <song@kernel.org>,
	 Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	 John Fastabend <john.fastabend@gmail.com>,
	 Emil Tsalapatis <emil@etsalapatis.com>,
	 Ihor Solodrai <ihor.solodrai@linux.dev>,
	 Steven Rostedt <rostedt@goodmis.org>,
	 Masami Hiramatsu <mhiramat@kernel.org>,
	 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	 JP Kobryn <jp.kobryn@linux.dev>,
	linux-trace-kernel@vger.kernel.org,
	 linux-kernel@vger.kernel.org,
	Mike Bazov <mikeba@sentinelone.com>
Subject: [PATCH bpf-next v4 6/6] selftests/bpf: Add preemption recursion test for tracing programs
Date: Mon, 30 Nov 2026 13:54:06 +0200	[thread overview]
Message-ID: <20261130-bpf-preempt-v2-r2-v4-6-bd28389dfd64@sentinelone.com> (raw)
In-Reply-To: <20261130-bpf-preempt-v2-r2-v4-0-bd28389dfd64@sentinelone.com>

From: Mike Bazov <mikeba@sentinelone.com>

Pin two threads to one CPU. Thread B is SCHED_FIFO and waits for a
signal that thread A sends from inside the program under test, so B
wakes up while A is inside the program and enters it on the same CPU.
Each subtest runs 20 rounds.

- fentry, fexit, fmod_ret, fentry_s, raw_tp, raw_tp_s, tp_syscall,
  may_goto: a program without per-CPU state. B preempts A inside it and
  runs it too, in every round, without recursion misses.
- *_percpu, percpu_var, this_cpu_ptr, smp_processor_id, tail_call: a
  program with a per-CPU map, a per-CPU variable, bpf_this_cpu_ptr(),
  bpf_get_smp_processor_id() or a tail call.
  It runs with preemption disabled: B runs it after A has left, in
  every round, without recursion misses. The sleepable variants have
  no protection, so B runs inside them. A constant selects the per-CPU
  state; with none selected, the same code doesn't mark the program.
- two_dispatchers: two raw tracepoint programs tail-call one handler.
  B runs it after A, and A's stack buffer stays intact.
- recursion_raw_tp, recursion_two_hops, recursion_test_run: true
  recursion is still dropped and counted: directly, through a second
  program, and from BPF_PROG_TEST_RUN.
- ext_attach: an extension that uses a per-CPU map can't be attached
  to a raw tracepoint program that doesn't, while one without can.

Signed-off-by: Mike Bazov <mikeba@sentinelone.com>
---
 .../selftests/bpf/prog_tests/trampoline_preempt.c  | 338 +++++++++++++++++++++
 .../selftests/bpf/progs/trampoline_preempt.c       | 275 +++++++++++++++++
 2 files changed, 613 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c b/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
new file mode 100644
index 000000000000..1e6c8c166b70
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
@@ -0,0 +1,338 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <pthread.h>
+#include <sched.h>
+#include <signal.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <test_progs.h>
+#include "trampoline_preempt.skel.h"
+
+#define NR_ROUNDS 20
+
+/* The kernel's own "not supported" error, which the libc headers don't have */
+#ifndef ENOTSUPP
+#define ENOTSUPP 524
+#endif
+
+/*
+ * Threads A and B are pinned to one CPU. B is SCHED_FIFO and waits for a signal:
+ *
+ *   A: prctl() -> enters the program under test
+ *                 the program sends B the signal
+ *   B:            wakes up and preempts A, which is still inside the program
+ *                 prctl() -> enters the same program on the same CPU
+ *
+ * A program without per-CPU state runs for B too, while A is preempted inside
+ * it. A program with per-CPU state runs with preemption disabled: B wakes up
+ * only when A has left the program, and then runs it. A sleepable program has
+ * to disable preemption itself, so B runs inside it too.
+ */
+
+/* The per-CPU state that a program touches, see progs/trampoline_preempt.c */
+enum { NO_PERCPU, PERCPU_MAP, PERCPU_VAR, THIS_CPU_PTR, CPU_ID };
+
+enum expect {
+	B_RUNS,		/* B runs the program while A is preempted inside it */
+	B_AFTER,	/* B runs the program after A has left it */
+};
+
+/* What threads A and B share */
+struct ctx {
+	struct trampoline_preempt *skel;
+	sigset_t sigset;
+	int cpu, err_a, err_b;
+	int overlaps;	/* rounds in which B found A still inside the program */
+};
+
+static int pin_to_cpu(int cpu)
+{
+	cpu_set_t set;
+
+	CPU_ZERO(&set);
+	CPU_SET(cpu, &set);
+	return sched_setaffinity(0, sizeof(set), &set) ? -errno : 0;
+}
+
+/* A enters the program under test, which wakes B from inside it */
+static void *worker_a(void *arg)
+{
+	struct ctx *c = arg;
+	int i;
+
+	c->err_a = pin_to_cpu(c->cpu);
+	c->skel->bss->tid_a = syscall(SYS_gettid);
+	for (i = 0; i < NR_ROUNDS && !c->err_a; i++) {
+		/* Passes every hook that the programs under test are attached to */
+		if (prctl(PR_SET_NAME, "worker_a"))
+			c->err_a = -errno;
+	}
+	return NULL;
+}
+
+/* B is SCHED_FIFO on A's CPU: A runs only while B waits, and B preempts A as soon as A wakes it */
+static void *worker_b(void *arg)
+{
+	struct sched_param sp = { .sched_priority = 1 };
+	struct timespec ts = { .tv_sec = 5 };	/* give up if the signal never comes */
+	struct ctx *c = arg;
+	int i;
+
+	c->err_b = pin_to_cpu(c->cpu);
+	if (!c->err_b && sched_setscheduler(0, SCHED_FIFO, &sp))
+		c->err_b = -errno;
+	c->skel->bss->tid_b = syscall(SYS_gettid);
+	for (i = 0; i < NR_ROUNDS && !c->err_b; i++) {
+		while (sigtimedwait(&c->sigset, NULL, &ts) < 0) {
+			if (errno != EINTR) {
+				c->err_b = -errno;
+				return NULL;
+			}
+		}
+		/* B preempted A only if A is still inside the program */
+		if (*(volatile int *)&c->skel->bss->a_inside)
+			c->overlaps++;
+		/* B's own run of the program, while A is preempted inside it */
+		if (prctl(PR_SET_NAME, "worker_b"))
+			c->err_b = -errno;
+	}
+	return NULL;
+}
+
+/* Load and attach only programs @a, @b and @c. NULL after a failure or a skip. */
+static struct trampoline_preempt *load_progs(const char *a, const char *b, const char *c,
+					     int percpu)
+{
+	struct trampoline_preempt *skel = trampoline_preempt__open();
+	struct bpf_program *p;
+	const char *name;
+	int err;
+
+	if (!ASSERT_OK_PTR(skel, "open"))
+		return NULL;
+	bpf_object__for_each_program(p, skel->obj) {
+		name = bpf_program__name(p);
+		bpf_program__set_autoload(p, !strcmp(name, a) || (b && !strcmp(name, b)) ||
+					  (c && !strcmp(name, c)));
+	}
+	skel->bss->sig = SIGUSR1;
+	skel->rodata->percpu = percpu;
+	err = trampoline_preempt__load(skel);
+	/* The kernel or its JIT doesn't support one of the programs */
+	if (err == -EOPNOTSUPP || err == -ENOTSUPP)
+		test__skip();
+	else if (ASSERT_OK(err, "load") && ASSERT_OK(trampoline_preempt__attach(skel), "attach"))
+		return skel;
+	trampoline_preempt__destroy(skel);
+	return NULL;
+}
+
+static const struct subtest {
+	const char *name;
+	const char *prog;	/* A enters it; its recursion misses are checked */
+	int percpu;		/* the per-CPU state that the program touches */
+	enum expect expect;
+	const char *prog_b;	/* two_dispatchers: B reaches the handler through this one */
+	const char *handler;	/* two_dispatchers: the target of both tail calls */
+} subtests[] = {
+	/* Each kind of program, without per-CPU state and with a per-CPU map lookup */
+	{ "fentry",		"on_fentry",		NO_PERCPU,	B_RUNS },
+	{ "fentry_percpu",	"on_fentry",		PERCPU_MAP,	B_AFTER },
+	{ "fexit",		"on_fexit",		NO_PERCPU,	B_RUNS },
+	{ "fexit_percpu",	"on_fexit",		PERCPU_MAP,	B_AFTER },
+	{ "fmod_ret",		"on_fmod_ret",		NO_PERCPU,	B_RUNS },
+	{ "fmod_ret_percpu",	"on_fmod_ret",		PERCPU_MAP,	B_AFTER },
+	{ "fentry_s",		"on_fentry_s",		NO_PERCPU,	B_RUNS },
+	{ "fentry_s_percpu",	"on_fentry_s",		PERCPU_MAP,	B_RUNS },
+	{ "raw_tp",		"on_raw_tp",		NO_PERCPU,	B_RUNS },
+	{ "raw_tp_percpu",	"on_raw_tp",		PERCPU_MAP,	B_AFTER },
+	{ "raw_tp_s",		"on_raw_tp_s",		NO_PERCPU,	B_RUNS },
+	{ "raw_tp_s_percpu",	"on_raw_tp_s",		PERCPU_MAP,	B_RUNS },
+	{ "tp_syscall",		"on_tp_syscall",	NO_PERCPU,	B_RUNS },
+	{ "tp_syscall_percpu",	"on_tp_syscall",	PERCPU_MAP,	B_AFTER },
+	/* The other things that mark a program */
+	{ "percpu_var",		"on_raw_tp",		PERCPU_VAR,	B_AFTER },
+	{ "this_cpu_ptr",	"on_raw_tp",		THIS_CPU_PTR,	B_AFTER },
+	{ "smp_processor_id",	"on_raw_tp",		CPU_ID,		B_AFTER },
+	{ "tail_call",		"on_tail_call",		NO_PERCPU,	B_AFTER },
+	/* Timed may_goto doesn't mark a program: B runs while A is inside the loop */
+	{ "may_goto",		"on_may_goto",		NO_PERCPU,	B_RUNS },
+	/* B runs the handler through the other dispatcher after A; A's stack stays intact */
+	{ "two_dispatchers",	"dispatcher_a",		NO_PERCPU,	B_AFTER,
+	  "dispatcher_b", "handler" },
+};
+
+static void run_subtest(const struct subtest *t)
+{
+	struct bpf_prog_info info = {};
+	__u32 info_len = sizeof(info), zero = 0;
+	struct ctx c = {};
+	sigset_t oldset;
+	pthread_t ta, tb;
+	int err, fd;
+
+	c.skel = load_progs(t->prog, t->prog_b, t->handler, t->percpu);
+	if (!c.skel)
+		return;
+	if (t->handler) {
+		/* Both dispatchers tail-call the handler through the prog array */
+		fd = bpf_program__fd(bpf_object__find_program_by_name(c.skel->obj, t->handler));
+		err = bpf_map_update_elem(bpf_map__fd(c.skel->maps.prog_array), &zero, &fd,
+					  BPF_ANY);
+		if (!ASSERT_OK(err, "prog_array_update"))
+			goto out;
+	}
+
+	/* Both threads go on this CPU. B takes the signal with sigtimedwait(), so block it. */
+	c.cpu = sched_getcpu();
+	sigemptyset(&c.sigset);
+	sigaddset(&c.sigset, SIGUSR1);
+	if (!ASSERT_OK(pthread_sigmask(SIG_BLOCK, &c.sigset, &oldset), "sigmask"))
+		goto out;
+	if (ASSERT_OK(pthread_create(&tb, NULL, worker_b, &c), "pthread_b")) {
+		/* A's program needs tid_b to wake B */
+		while (!c.skel->bss->tid_b)
+			usleep(1000);
+		if (ASSERT_OK(pthread_create(&ta, NULL, worker_a, &c), "pthread_a"))
+			pthread_join(ta, NULL);
+		pthread_join(tb, NULL);
+	}
+	pthread_sigmask(SIG_SETMASK, &oldset, NULL);
+
+	/* B may not be allowed to become SCHED_FIFO */
+	if (c.err_b == -EPERM || c.err_b == -EACCES) {
+		test__skip();
+		goto out;
+	}
+	ASSERT_OK(c.err_a, "worker_a");
+	ASSERT_OK(c.err_b, "worker_b");
+	ASSERT_EQ(c.skel->bss->sig_err, 0, "sig_err");
+	if (t->expect == B_RUNS && !c.overlaps) {
+		/* B never preempted A inside the program, so nothing was tested */
+		test__skip();
+		goto out;
+	}
+	ASSERT_EQ(c.overlaps, t->expect == B_RUNS ? NR_ROUNDS : 0, "overlaps");
+	ASSERT_EQ(c.skel->bss->runs_a, NR_ROUNDS, "runs_a");
+	ASSERT_EQ(c.skel->bss->runs_b, NR_ROUNDS, "runs_b");
+	if (t->handler)
+		ASSERT_EQ(c.skel->bss->stack_corrupt, 0, "stack_corrupt");
+	fd = bpf_program__fd(bpf_object__find_program_by_name(c.skel->obj, t->prog));
+	err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
+	if (!ASSERT_OK(err, "prog_info"))
+		goto out;
+	ASSERT_EQ(info.recursion_misses, 0, "recursion_misses");
+out:
+	trampoline_preempt__destroy(c.skel);
+}
+
+/*
+ * The program calls bpf_printk(), which fires the bpf_trace_printk tracepoint
+ * that it is attached to: the nested run must be rejected as recursion. With a
+ * hop, a second program runs in between, so the check has to look past the
+ * frame right below.
+ */
+static const struct recursion {
+	const char *name, *prog, *hop;
+	bool test_run;	/* Start the outer run with BPF_PROG_TEST_RUN of @prog */
+} recursions[] = {
+	{ "recursion_raw_tp", "recur_raw_tp" },
+	{ "recursion_two_hops", "recur_two_hops", "recur_hop" },
+	{ "recursion_test_run", "recur_raw_tp", .test_run = true },
+};
+
+static void run_recursion(const struct recursion *t)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+	struct trampoline_preempt *skel = load_progs(t->prog, t->hop, "recur_trigger", NO_PERCPU);
+	struct bpf_prog_info info = {};
+	__u32 info_len = sizeof(info);
+	int err, fd, run_fd;
+
+	if (!skel)
+		return;
+	fd = bpf_program__fd(bpf_object__find_program_by_name(skel->obj, t->prog));
+	/* The outer run: of @prog itself, or of a program that fires its tracepoint */
+	run_fd = t->test_run ? fd : bpf_program__fd(skel->progs.recur_trigger);
+	skel->bss->recur_tid = syscall(SYS_gettid);
+	err = bpf_prog_test_run_opts(run_fd, &opts);
+	skel->bss->recur_tid = 0;
+	/* The outer run got in; the nested one was dropped and counted */
+	if (ASSERT_OK(err, "test_run")) {
+		ASSERT_EQ(skel->bss->recur_runs, 1, "recur_runs");
+		ASSERT_EQ(skel->bss->hop_runs, t->hop ? 1 : 0, "hop_runs");
+		err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
+		if (ASSERT_OK(err, "prog_info"))
+			ASSERT_EQ(info.recursion_misses, 1, "recursion_misses");
+	}
+	trampoline_preempt__destroy(skel);
+}
+
+/*
+ * An extension runs under the recursion check of its target. A target that
+ * keeps no per-CPU state is checked per task, so an extension that does keep
+ * such state can't be attached to it.
+ */
+static void run_ext_subtest(void)
+{
+	static const struct {
+		const char *prog;
+		int err;
+	} exts[] = {
+		{ "ext_plain", 0 },		/* no per-CPU state: attaches */
+		{ "ext_percpu", -EINVAL },	/* a per-CPU map lookup: rejected */
+	};
+	struct trampoline_preempt *tgt_skel, *skel = NULL;
+	struct bpf_program *ext, *p;
+	struct bpf_link *link;
+	int i, err, tgt_fd;
+
+	tgt_skel = trampoline_preempt__open();
+	if (!ASSERT_OK_PTR(tgt_skel, "open_tgt"))
+		return;
+	bpf_object__for_each_program(p, tgt_skel->obj)
+		bpf_program__set_autoload(p, p == tgt_skel->progs.ext_target);
+	if (!ASSERT_OK(trampoline_preempt__load(tgt_skel), "load_tgt"))
+		goto out;
+	tgt_fd = bpf_program__fd(tgt_skel->progs.ext_target);
+
+	for (i = 0; i < ARRAY_SIZE(exts); i++) {
+		skel = trampoline_preempt__open();
+		if (!ASSERT_OK_PTR(skel, "open_ext"))
+			goto out;
+		ext = bpf_object__find_program_by_name(skel->obj, exts[i].prog);
+		bpf_object__for_each_program(p, skel->obj)
+			bpf_program__set_autoload(p, p == ext);
+		err = bpf_program__set_attach_target(ext, tgt_fd, "ext_target");
+		if (!ASSERT_OK(err, "set_attach_target"))
+			goto out;
+		if (!ASSERT_OK(trampoline_preempt__load(skel), "load_ext"))
+			goto out;
+
+		link = bpf_program__attach_freplace(ext, tgt_fd, "ext_target");
+		ASSERT_EQ(libbpf_get_error(link), exts[i].err, "attach_ext");
+		bpf_link__destroy(link);
+		trampoline_preempt__destroy(skel);
+		skel = NULL;
+	}
+out:
+	trampoline_preempt__destroy(skel);
+	trampoline_preempt__destroy(tgt_skel);
+}
+
+void test_trampoline_preempt(void)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(subtests); i++) {
+		if (test__start_subtest(subtests[i].name))
+			run_subtest(&subtests[i]);
+	}
+	for (i = 0; i < ARRAY_SIZE(recursions); i++) {
+		if (test__start_subtest(recursions[i].name))
+			run_recursion(&recursions[i]);
+	}
+	if (test__start_subtest("ext_attach"))
+		run_ext_subtest();
+}
diff --git a/tools/testing/selftests/bpf/progs/trampoline_preempt.c b/tools/testing/selftests/bpf/progs/trampoline_preempt.c
new file mode 100644
index 000000000000..56eb797d220c
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/trampoline_preempt.c
@@ -0,0 +1,275 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <asm/unistd.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "bpf_compiler.h"
+#include <bpf_may_goto.h>
+
+/* The programs of prog_tests/trampoline_preempt.c. The comment there describes the test. */
+
+char _license[] SEC("license") = "GPL";
+
+struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
+void bpf_task_release(struct task_struct *p) __ksym;
+int bpf_send_signal_task(struct task_struct *task, int sig, enum pid_type type, u64 value) __ksym;
+
+/* The target of the tail calls: empty for tail_call, the handler for two_dispatchers */
+struct {
+	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u32);
+} prog_array SEC(".maps");
+
+/* recur_two_hops deletes from it, which runs recur_hop */
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 1);
+	__type(key, int);
+	__type(value, int);
+} recur_map SEC(".maps");
+
+/* The per-CPU state: a map, a variable, a kernel variable for the helper, and the CPU id */
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, int);
+	__type(value, __u64);
+} percpu_map SEC(".maps");
+
+__u64 percpu_var SEC(".percpu");
+__u32 cpu_id;
+extern const struct rq runqueues __ksym;
+
+/* The per-CPU state that a program touches. The test picks one before the load. */
+enum { NO_PERCPU, PERCPU_MAP, PERCPU_VAR, THIS_CPU_PTR, CPU_ID };
+const volatile int percpu;
+
+/* Set by the test */
+int tid_a, tid_b, sig, recur_tid;
+int loops;	/* 0, read at run time so that the may_goto loop is not optimized away */
+
+/* Read by the test */
+int runs_a, runs_b, sig_err, stack_corrupt, recur_runs, hop_runs;
+int a_inside;	/* A is inside the program: B reads it to see that it preempted A there */
+
+static void signal_worker_b(void)
+{
+	struct task_struct *task = bpf_task_from_pid(tid_b);
+
+	if (!task || bpf_send_signal_task(task, sig, PIDTYPE_PID, 0))
+		sig_err = 1;
+	if (task)
+		bpf_task_release(task);
+}
+
+/*
+ * The body of every program under test: count the run of A or of B. A also
+ * wakes B from here, so B preempts A inside the program and enters it too.
+ */
+static __always_inline int record_run(void)
+{
+	__u32 tid = (__u32)bpf_get_current_pid_tgid();
+	int zero = 0;
+
+	/* The verifier walks only the selected branch, so only that one marks the program */
+	switch (percpu) {
+	case PERCPU_MAP:
+		bpf_map_lookup_elem(&percpu_map, &zero);
+		break;
+	case PERCPU_VAR:
+		percpu_var++;
+		break;
+	case THIS_CPU_PTR:
+		bpf_this_cpu_ptr(&runqueues);
+		break;
+	case CPU_ID:
+		cpu_id = bpf_get_smp_processor_id();
+		break;
+	}
+
+	if (tid == tid_a) {
+		runs_a++;
+		/* volatile: both stores must stay, B reads a_inside between them */
+		*(volatile int *)&a_inside = 1;
+		signal_worker_b();
+		*(volatile int *)&a_inside = 0;
+	} else if (tid == tid_b) {
+		runs_b++;
+	}
+	return 0;
+}
+
+/* Both threads call prctl(PR_SET_NAME), which passes each of the hooks below */
+
+SEC("fentry/__set_task_comm")
+int BPF_PROG(on_fentry)
+{
+	return record_run();
+}
+
+SEC("fexit/__set_task_comm")
+int BPF_PROG(on_fexit)
+{
+	return record_run();
+}
+
+/* Returns 0: prctl() runs as usual */
+SEC("fmod_ret/" SYS_PREFIX "sys_prctl")
+int BPF_PROG(on_fmod_ret)
+{
+	return record_run();
+}
+
+SEC("fentry.s/" SYS_PREFIX "sys_prctl")
+int BPF_PROG(on_fentry_s)
+{
+	return record_run();
+}
+
+SEC("raw_tp/task_rename")
+int BPF_PROG(on_raw_tp)
+{
+	return record_run();
+}
+
+SEC("raw_tp.s/sys_enter")
+int BPF_PROG(on_raw_tp_s, struct pt_regs *regs, long id)
+{
+	return id == __NR_prctl ? record_run() : 0;
+}
+
+SEC("tp/syscalls/sys_enter_prctl")
+int on_tp_syscall(void *ctx)
+{
+	return record_run();
+}
+
+/* The prog array is empty, so the tail call does nothing */
+SEC("raw_tp/task_rename")
+int on_tail_call(void *ctx)
+{
+	bpf_tail_call(ctx, &prog_array, 0);
+	return record_run();
+}
+
+/* can_loop is a may_goto. With timed may_goto its time budget is per CPU. */
+SEC("raw_tp/task_rename")
+int on_may_goto(void *ctx)
+{
+	int i;
+
+	for (i = loops; i < 1 && can_loop; i++)
+		;
+	return record_run();
+}
+
+/* Two dispatchers tail-call one handler: A enters through dispatcher_a, B through dispatcher_b */
+SEC("raw_tp/task_rename")
+int dispatcher_a(void *ctx)
+{
+	if ((__u32)bpf_get_current_pid_tgid() == tid_a)
+		bpf_tail_call(ctx, &prog_array, 0);
+	return 0;
+}
+
+SEC("raw_tp/sys_enter")
+int BPF_PROG(dispatcher_b, struct pt_regs *regs, long id)
+{
+	if (id == __NR_prctl && (__u32)bpf_get_current_pid_tgid() == tid_b)
+		bpf_tail_call(ctx, &prog_array, 0);
+	return 0;
+}
+
+/* The handler keeps 64 bytes of stack across the point where B runs it too */
+SEC("raw_tp")
+int handler(void *ctx)
+{
+	__u32 tid = (__u32)bpf_get_current_pid_tgid();
+	__u64 buf[8];
+
+	__pragma_loop_unroll
+	for (int i = 0; i < 8; i++)
+		buf[i] = ((__u64)tid << 32) | i;
+	/* Make the compiler keep buf on the stack across record_run() */
+	asm volatile("" : "+m"(buf));
+
+	record_run();
+
+	asm volatile("" : "+m"(buf));
+	/* Don't trust a copy of tid that may have been spilled to the stack under test */
+	tid = (__u32)bpf_get_current_pid_tgid();
+	__pragma_loop_unroll
+	for (int i = 0; i < 8; i++) {
+		if (buf[i] != (((__u64)tid << 32) | i))
+			stack_corrupt = 1;
+	}
+	return 0;
+}
+
+/* bpf_printk() fires the bpf_trace_printk tracepoint that the program is attached to */
+SEC("raw_tp/bpf_trace_printk")
+int recur_raw_tp(void *ctx)
+{
+	/* Other tasks print too: only the runs of the test's thread count */
+	if ((__u32)bpf_get_current_pid_tgid() != recur_tid)
+		return 0;
+	recur_runs++;
+	bpf_printk("recur");
+	return 0;
+}
+
+/* The test runs it with BPF_PROG_TEST_RUN to fire the tracepoint once */
+SEC("syscall")
+int recur_trigger(void *ctx)
+{
+	bpf_printk("trigger");
+	return 0;
+}
+
+/* The same through a second program: recur_two_hops -> recur_hop -> recur_two_hops */
+SEC("raw_tp/bpf_trace_printk")
+int recur_two_hops(void *ctx)
+{
+	int zero = 0;
+
+	if ((__u32)bpf_get_current_pid_tgid() != recur_tid)
+		return 0;
+	recur_runs++;
+	bpf_map_delete_elem(&recur_map, &zero);
+	return 0;
+}
+
+SEC("fentry/htab_map_delete_elem")
+int BPF_PROG(recur_hop, struct bpf_map *map)
+{
+	if (map != (struct bpf_map *)&recur_map || (__u32)bpf_get_current_pid_tgid() != recur_tid)
+		return 0;
+	hop_runs++;
+	bpf_printk("hop");
+	return 0;
+}
+
+/* For ext_attach: a target without per-CPU state, and an extension without and with it */
+SEC("raw_tp/task_rename")
+int ext_target(struct bpf_raw_tracepoint_args *ctx)
+{
+	return 0;
+}
+
+SEC("freplace")
+int ext_plain(struct bpf_raw_tracepoint_args *ctx)
+{
+	return 0;
+}
+
+SEC("freplace")
+int ext_percpu(struct bpf_raw_tracepoint_args *ctx)
+{
+	int zero = 0;
+
+	bpf_map_lookup_elem(&percpu_map, &zero);
+	return 0;
+}

-- 
2.43.0



      parent reply	other threads:[~2026-11-30 11:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 11:54 [PATCH bpf-next v4 0/6] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 1/6] bpf: Avoid per-CPU private stack in preemptible tracing progs Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 2/6] bpf: Mark programs that keep per-CPU state Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 3/6] bpf: Link the async run context without writing to it Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 4/6] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 5/6] bpf: Remove the per-CPU recursion counter of programs Mike Bazov via B4 Relay
2026-11-30 11:54 ` Mike Bazov via B4 Relay [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=20261130-bpf-preempt-v2-r2-v4-6-bd28389dfd64@sentinelone.com \
    --to=devnull+mikeba.sentinelone.com@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=jp.kobryn@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=memxor@gmail.com \
    --cc=mhiramat@kernel.org \
    --cc=mikeba@sentinelone.com \
    --cc=rostedt@goodmis.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

all inboxes | Powered by JetHome®