From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796139180; b=rdznQAAOXSBLyoGfUpuzUeN8WNdHbh+y6+CK6iGMmSemrKuipqmBK4Gg3dwi1yLA660K EgsSWbhs2YcMP/ET0OOIxmeAeZioTX8bGpZ0AsAujOBShBY0GztnvRQs44g/tcqVkKnME /jI7GDbal6m+IZ/TyKGQ6BMTQBXbVQ9BGzJUfxAdXoZJ1vlQKzxNVE6ZhnrWI0ApSofBW FWSFCF/u8BlZYvtygFjiUyUjEzN7Ae1bM+BjIiVXlU5ohLYukwUUtgID8tkB6ySs83jYf C+5I8ini89mBYM8WYXa4GCB+Rk2xYe1xvsii97P0Gu7L8GcJxz/YeuBH+R8shz0KeKA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796139180; h=Received:DKIM-Signature:Received:From:Subject:Date:Message-Id: MIME-Version:Content-Type:Content-Transfer-Encoding:X-B4-Tracking:To: Cc:X-Mailer:X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To; bh=L4HE85hFnu+2Q5GxJfsE4qf6v6eCnwrlneVgDEXXzeE=; b=ZhYcZkGsQyq86NhRcwxB/SZlAMXoGGhRPVG9AZmBIHJ0rDnVoFNAe5+Yd2+UHrvhxrDH JmR3TJWsNN/ZQvq/Gu1T4o4YadAalN8zAUTFg6n762TjJjwBzJDb9jWzOP1hsxZRZ5db8 xGscFHvh7yrJexYGKK50M6sQuMugkAghy1ubyflmmla5s/qAzNvVfG+j2g5ylx6uGWAi7 SxgQznPymS+wnYOpXOkuED02zWkn/WOqB6wLXDLi6chebIN2/nMrNV6Qmm7F5fGGrxZFs N13RRgKKLWZZJpzEb9oudByuE0Fon7kh8+9IvOEkjVpgyrO5JuhyNPMuUKIPKS1m2cg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=EoKC7kzl; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE04D606A327; Tue, 1 Dec 2026 15:33:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=EoKC7kzl Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796139180; b=jWiBdwq6znuaR2AV+8FmFQeoepQf7je1s/2l7mOJfpE0e2yHT3wejyly6gIxRSPX1i1l nt/awNZnOT+gl6cww4tyoms61HXzs6RiXwvCD7BrSgbanCP5PooRchODeJAm0/HGrYwjc jnK2B2D1/zpEcZCwrN9PR0QQkMKxmx6k71lM7NUOplg4+3NiPGg1Lh8knetDf9f6pNAsV eKZvzJgGXi95yDhJeHYtQbTWvXs1KjUtHukhLXiqOOCm/gqpfiRb0vn/QzEBc2h24xtmP 5KUeKoiD9jbaT+Y77KIIcmh/ZYK/LvUtxyo0CXDf8zgFi2ZCi36Iu1AjmjXXXtZ19mg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796139180; h=DKIM-Signature:Received:From:Subject:Date:Message-Id:MIME-Version: Content-Type:Content-Transfer-Encoding:X-B4-Tracking:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To; bh=L4HE85hFnu+2Q5GxJfsE4qf6v6eCnwrlneVgDEXXzeE=; b=WmCFIs8EovnohdO8I6Se+pPinv/SOqORuiQbc8p+aKIv54qbUaIm+pojn7VuiSLAofTk SvaCWf6Zi/BOGJsExr7uHcvEPyVAvDGxOF7fFfKYlxhWjj4aYnT34wZtdxwEO8v7yNIo1 v+6geNwvPedjlXz/LBPhSpsYzqFS/el8QPsiktUPkc5vfD+Am1rvo2hkCJrSu7i4ZbbdR lHkii5TwhcuRSyZNZ8FJYeroUBOgbt+imdC5lAHqFlb/70P4ElG1NF5fxqciwKHbnoSqL yVTyL9gbtDuL2AFSlvVMNGkM7Lm5TNRepos5Wg+SOS1UOP46uSEmprNqiqu6W3C9G7A== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 259861F00927; Tue, 1 Dec 2026 15:32:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796139180; bh=L4HE85hFnu+2Q5GxJfsE4qf6v6eCnwrlneVgDEXXzeE=; h=From:Subject:Date:To:Cc:Reply-To; b=EoKC7kzlDR6U0r1PbWXRNm27b18XGf4F8MWq52C85F9e64SwTAdXc/ZxQcILRRgwe lEk3eYhnWqKE/TzIF/5CkxBczqYA3WlwiEbk8hZdaJi0EN/ggNdzkXjy+fQm1kU2No Z9o3FChDqANjlLcxHYCq2fVHaxNEm52D/oxMz8DUKXmPRk1mWH8jqE++ovKxMHDKAG h1wSQZvV6XqcMAvubLHIt/tg76PT6ncIzSP3/LZyEV9eWdAnNZf2kCZO+Ik5jBUAyX X/nMxOZgzsOsKPM4U0xqLJ+U8UnhvGhubbVL8FVQKv3q+K4Frpgzv47yiP/hp4ZCBt CdWy5ldEvfvBA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 700954EE74; Tue, 1 Dec 2026 15:32:33 +0000 (UTC) From: Mike Bazov via B4 Relay Subject: [PATCH bpf-next v5 0/5] bpf: Check recursion per task for preemptible programs Date: Tue, 01 Dec 2026 17:32:11 +0200 Message-Id: <20261201-bpf-preempt-v2-r2-v5-0-a6bb30daae35@sentinelone.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-B4-Tracking: v=1; b=H4sIAHvoDmsC/33OwQ6DIAwG4FdZOI/FgaLstPdYdgBbJsmGBoxxM b77qieTuXEgacv/lYkljB4TuxwmFnHwybeBiuJ4YHVjwgO5B6qZyIQ60+G2c7yLiK+u54PgUXC nnTYZWCggZ5SjqfPjat7Y8jzg2LM7TaxJyG00oW4Wk+JLZ8k0PvVtfK/fGMSa/LOR7oxrqJyyu hAGxDVh6H3AZxvwVLevddsgt06550hyikrJUmEtVfnDyTeOzPacnBwLopKVBgcq/3bmef4A5ir 6FWwBAAA= To: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Eduard Zingerman , Kumar Kartikeya Dwivedi , Song Liu , Yonghong Song , Jiri Olsa , John Fastabend , Emil Tsalapatis , Ihor Solodrai , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , JP Kobryn , linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, Mike Bazov X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796139152; l=7063; i=mikeba@sentinelone.com; s=20261022; h=from:subject:message-id; bh=ZAqVTPLDszRvVzEpzvZ9FeGJ1zhBzOpjIDSiz86cAV4=; b=AJfEqFS5KyX8PeGr+ya5LfDAg583jpviC5GhzrX3Eixbg5JFpukung82ZAnLY8HSqwmGbHTji KCy3Z8XzspYAGyjVRkPEPNADdM4ObzgyBoeLjCKY2VPV3lC2l6ErHiz X-Developer-Key: i=mikeba@sentinelone.com; a=ed25519; pk=DBXcQ0qoCkgacRsRwT1JU7oOWpWSYuTyLiXMfNLcP+A= X-Endpoint-Received: by B4 Relay for mikeba@sentinelone.com/20261022 with auth_id=1284 X-Original-From: Mike Bazov Reply-To: mikeba@sentinelone.com fentry, fexit, raw_tp and syscall tracepoint programs can be preempted while they run, but their recursion check is a per-CPU counter: task A enters prog on CPU 0 prog->active = 1 task A is preempted inside prog task B enters prog on CPU 0 dropped, counted as recursion miss Nothing recursed, but B's event is lost. Since 7.0, x86 and arm64 have only lazy and full preemption, so this happens on stock kernels. Our fentry/fexit probe on inet_sendmsg() and inet_recvmsg() loses 8,572 of 8 million runs under HTTP load on a 4-CPU VM. With the series it loses none. The series checks recursion per task instead, and removes the per-CPU counter: a program is recursing only if the current task is already inside it. A program that keeps per-CPU state relies on no other run of it starting on the CPU while it runs. The verifier marks such programs, and they run with preemption disabled instead of dropping other runs: - per-CPU maps, bpf_this_cpu_ptr(), bpf_per_cpu_ptr() - bpf_get_smp_processor_id(): the CPU id often indexes per-CPU slots that the program keeps itself - tail calls: the target runs in the caller's run - a private stack (struct_ops that request one) That also keeps apart programs that share a per-CPU map, which the per-program counter never did. Trade-offs: - A marked program can't be preempted until it exits, and a program that tail-calls is marked. - A sleepable program with per-CPU state gets no protection; it has to use bpf_preempt_disable() itself. - Every program that calls bpf_get_smp_processor_id() is marked, also one that only records the CPU id. Per-CPU state that the verifier doesn't see at all stays unprotected. - A run that preempts another one inside a timed may_goto loop can renew the other's 250 ms budget, as it already can for lsm programs. A fix for all prog types belongs to may_goto and is left for a separate series. - Tracing, raw_tp and tracepoint programs lose the private stack (patch 1). It is already unsafe for them: two raw_tp programs that tail-call one handler let two tasks share its stack today. A program with two 2000-byte frames no longer loads. - A marked freplace program can't be attached to a raw_tp or tracepoint program that is unmarked or sleepable: -EINVAL. 1. bpf: Avoid per-CPU private stack in preemptible tracing progs 2. bpf: Mark programs that keep per-CPU state 3. bpf: Check program recursion per task via bpf_ctx 4. bpf: Remove the per-CPU recursion counter of programs 5. selftests/bpf: Add preemption recursion test for tracing programs Testing, x86-64: The selftest pins two tasks to one CPU and makes B enter the program while A is preempted inside it, 20 rounds per subtest. With the series all 25 subtests pass. On bpf-next 21 fail: B is dropped in every round, or in two_dispatchers runs the shared handler inside A and corrupts A's stack, and the freplace attach is not rejected. Only the four that check true recursion pass on both. On v4, timer_cb and ext_attach fail. The bench ran on an idle bare-metal x86 machine, used only for this test, to avoid the variation of a VM: there, the same kernels moved by up to 2% between boots. bench -w3 -d9 --prod-affinity 5 --trig-batch-iters 1000 trig-, M/s, median of 3 runs, change vs bpf-next. Every run is within 0.1% of its median. trig-fentry trig-fexit trig-rawtp bpf-next 46.92 38.18 47.88 series 47.06 (+0.3%) 38.23 (+0.1%) 44.34 (-7.4%) Every trig bench program calls bpf_get_smp_processor_id() to index its hit counter, so with the mark they all run with preemption disabled. Most of the raw_tp cost comes from that, see the v4 thread. Changes in v5: - Enter and exit follow Alexei's suggestion in v3: the frame of a marked program is linked and unlinked only while preemption is disabled, in every runner. v4's split layout is reverted: the order of linking and preemption has to stay in one place because of scheduling. - An async callback links a frame of its own on the stack, per Alexei's suggestion in v4, so a program that the callback runs into is checked against the frames below. bpf_async_run_ctx is gone, and the async patch is folded into patch 3, which drops the v4 trade-off. - The freplace check is done in bpf_tracing_prog_attach() without the helpers, rejects sleepable targets too, and logs why. RAW_TRACEPOINT_WRITABLE isn't listed: bpf_check_attach_target() already refuses to extend it. - A sleepable struct_ops program no longer gets a private stack: it can't run with preemption disabled. - __always_inline on the run context helpers. - Selftest: new timer_cb subtest, and ext_attach covers sleepable and marked targets. - Rebased on bpf-next. v4: https://lore.kernel.org/bpf/20261130-bpf-preempt-v2-r2-v4-0-bd28389dfd64@sentinelone.com/ v3: https://lore.kernel.org/bpf/20261117-bpf-preempt-v2-r2-v3-0-586376ec3672@sentinelone.com/ v2: https://lore.kernel.org/bpf/20261111-bpf-preempt-v2-r2-v2-0-9d8f6b952ad2@sentinelone.com/ v1: https://lore.kernel.org/bpf/20261025-b4-bpf-trampoline-preempt-v1-0-198dcdc0c6ac@sentinelone.com/ --- Mike Bazov (5): bpf: Avoid per-CPU private stack in preemptible tracing progs bpf: Mark programs that keep per-CPU state bpf: Check program recursion per task via bpf_ctx bpf: Remove the per-CPU recursion counter of programs selftests/bpf: Add preemption recursion test for tracing programs include/linux/bpf.h | 160 ++++++--- kernel/bpf/core.c | 12 - kernel/bpf/helpers.c | 5 +- kernel/bpf/syscall.c | 15 + kernel/bpf/trampoline.c | 29 +- kernel/bpf/verifier.c | 58 +++- kernel/trace/bpf_trace.c | 11 +- net/bpf/test_run.c | 9 +- net/ipv4/bpf_tcp_ops.c | 6 +- .../selftests/bpf/prog_tests/may_goto_priv_stack.c | 6 +- .../selftests/bpf/prog_tests/timed_may_goto.c | 6 +- .../selftests/bpf/prog_tests/trampoline_preempt.c | 383 +++++++++++++++++++++ .../selftests/bpf/progs/may_goto_priv_stack.c | 14 +- .../selftests/bpf/progs/test_global_func3.c | 9 + .../selftests/bpf/progs/timed_may_goto_user.c | 16 +- .../selftests/bpf/progs/trampoline_preempt.c | 382 ++++++++++++++++++++ .../selftests/bpf/progs/verifier_private_stack.c | 81 +++-- .../selftests/bpf/progs/verifier_stack_arg.c | 38 +- .../testing/selftests/bpf/progs/verifier_unwind.c | 33 +- 19 files changed, 1101 insertions(+), 172 deletions(-) --- base-commit: 595782f6799a3eeed50b3dd1fb954cc8ce3689ac change-id: 20261111-bpf-preempt-v2-r2-f9f9a0dbd5d4 Best regards, -- Mike Bazov