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=1796039659; b=HfVHg5fk7I2R6+wJhC1Zx1w7mt1Rixj2VhHC3jg72VtxqwjY1nqbRwpsbez0u3HLlCMO MkzdQlHSCwsD3vLmjxlFF45cMoX1yHMqBBFG8tVK8VNj/7iEHPKCs62hnPe2uONB9MjFp 38QrJTHmqpJwLbfJdBwmRc2zdYm8Tfr7AfRb+esQUq7aztfiQJq0822DA0nfQrYhYRxU3 EfhTLapV2AQklNfpi5Xy62+dVS81ULe2Y4+nycWc+B6qbtkYSb+mgEpYfKmcvGDsN+lgg +hdhplQAIN+0F0tnJkygEVhgUal6QG4SG4w+/xPm+dOyEshAEoFfe7YLgj2MCYiV9jg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796039659; 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=u3sqrBiTE9vqNWsffKxRkseHt6PugOYiwL2gf7m68pY=; b=p4/ppuMELNr3Bo32io3YBDDe00PHMguQ8imEbhytDldwIzhStz/8/34Mar1qvVgE0qrr wGMjaYHSQUSFhb51aIDBN+yCxXowIaOU5sbX+BRMz49tQinNNtxBE5TmMORAImhymFh63 OC+Oxo7s88THr0oJqwaIlZwDoD0IJM2qT4UsaRr4zuuXdeKk++timK+d3aveT7R8WKSQm S9EnLVUIWuP19qh6hT4r6fF4WZpY5aHrYg/Qj9xTp5oV/7juA76kKKdXGtn+5NO1o14mC ehwo091YzAky3yYRkp1MQn2V09U1ad78yGklgBbL4pVIqJwOTKPTkgeDx6y5MR1eCaA== 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=GAk45PbE; 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 31FE7606A32E; Mon, 30 Nov 2026 11:54:19 +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=GAk45PbE 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=1796039658; b=wWNfucDAbFMacx1CvD3I9ffowPgCQhkDdBvKoe0kWdWHT/Wbhr145Dn17yK2pleQDttv Fw+mI4tb8lHXspLgmAYUU9K7z2FM2NbNijQ1fhTuBR1CyS85RlT2lrLsASqzbmPHvp8Ks wv4Fa40bYFu5dnf1/RxUZyEhTmWCfG+WcfeR57ZPYBEJeLcJmpFDxGRUw2QPdvO3Q0fM8 dNqeEjUY1iaGUYFBR25ilqfcxLFhxMr68VwKR5Gw7TkNMCGfOPPjcsxcBRtKdCTE8+5kS zLJs4Re19mjKy/ooQoen5fo6rkqwAGqMlF5q9wqc/90Bxz0Bb1CqreWCS91HTMG+hJA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796039658; 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=u3sqrBiTE9vqNWsffKxRkseHt6PugOYiwL2gf7m68pY=; b=oDwneHQBYjN3hVrvw1dv8doZK7SotlTk6FkG2Cnq4L1LowlAJJdTV7C40D1VcHM2XQlV mSUq3U1LfKlDTyLe+3fJ6ip+zPKW8V3H9HbFHK2RfAXAdRKAIuzYWPG8aR5O0FEOIo7ou CMqh1MLg0sbISwaWlhxW6/xVgrxFd8+8oNL9zVeozVPDFZJTtvVgn6z0EVWINJ/qbHAUd sR7DkmGs9W3ch/LFT7MfE0jQ5UIlbz0e5ee3gz1ILzTjLjUA1Sss8Sf7h2a+//RxY96YG u43iiOgkVKwT8pN+EXrk5kgJpORH8rGl6DdF2McA/gJknY8Y1msDHrXV94jzsd9PJ2w== 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 D44411F000C1; Mon, 30 Nov 2026 11:54:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796039658; bh=u3sqrBiTE9vqNWsffKxRkseHt6PugOYiwL2gf7m68pY=; h=From:Subject:Date:To:Cc:Reply-To; b=GAk45PbEjKFa+4TkOtZnmoViLm6+KTymqbDQb4XSlCvlppeDy3op7zEiGMxCfNUrb am7zAcpiO/vXj9LHBJ51/Wxi76PCbwaKn/27BxJN5G4e27TZlrLBBvWhMg6u391PJ1 NfLoMnDfy7CRscGR7i2LC7cf+z7uCxj5NQZlejOhJEgiDFbLiel2CSAFGDvJImDpUe NjxPcf16UB86aD9OZss63yk4qQuF68kWesWBJqRlJ4I4aEYW7ZAHcqcURVaQepvL1j OSLHcjoh+W+/A/2S1f8mFF1bXahXtUP6+RbHakpsqDwsFvDd49n6+9KeXYuTYo9EzK hw4mOk1qJUH+g== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 420584E7FB; Mon, 30 Nov 2026 11:54:05 +0000 (UTC) From: Mike Bazov via B4 Relay Subject: [PATCH bpf-next v4 0/6] bpf: Check recursion per task for preemptible programs Date: Mon, 30 Nov 2026 13:54:00 +0200 Message-Id: <20261130-bpf-preempt-v2-r2-v4-0-bd28389dfd64@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=H4sIANhjDWsC/32OwQ6CMAyGX4XsbA0OGMyT72E8bKyTJTrIthAM4 d3tOHkg9tDkb/t/f1cWMTiM7FqsLODsohs9ifpUsH5Q/ongDGnGSy4uVKAnC1NAfE8JZg6Bg5V WqtJo05iakY+21i07887yucclsQdttIoIOijfD5lJ9jzJnsHFNIbP/sbMd+efROolSNNZoWXDl eG3iD45j6/R47kf33vaXP1y2iNORZymE1UrsK9Ee8DZtu0Lron+fiQBAAA= 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=1796039644; l=7789; i=mikeba@sentinelone.com; s=20261022; h=from:subject:message-id; bh=2KhjFKPQFjWu1l4SMiwvtRTlxMSLUDxzuMO+r+JUVLA=; b=Mvy86vFmkTBYOo8J26KOlwOD/qpXQWtb/qHMoOtPOEi3bfX+wix+jCcse9REoA7pnKRXG8Oba ts3MyL69hlsBgJF4llQf7G1vneJfHblrkDD0et/2S9xN8iqU1yrbb/k 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 9,254 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. - An async callback starts a chain of its own, so a program that runs in the task it interrupted can run once more inside the callback. - 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 an unmarked raw_tp, tp_btf, tracepoint or fmod_ret program: -EINVAL. 1. bpf: Avoid per-CPU private stack in preemptible tracing progs 2. bpf: Mark programs that keep per-CPU state 3. bpf: Link the async run context without writing to it 4. bpf: Check program recursion per task via bpf_ctx 5. bpf: Remove the per-CPU recursion counter of programs 6. 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 24 subtests pass. On bpf-next 21 fail: B is dropped in every round, and the freplace attach is not rejected. Only the three that check true recursion pass on both. 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 9 runs over 3 boots per kernel, change vs bpf-next. Every run is within 0.5% of its median. trig-fentry trig-fexit trig-rawtp bpf-next 47.05 38.19 47.88 series 47.09 (+0.1%) 38.42 (+0.6%) 44.33 (-7.4%) series *without* the bpf_get_smp_processor_id() mark of patch 2: 48.74 (+3.6%) 39.61 (+3.7%) 46.03 (-3.9%) Without the mark, fentry and fexit get faster without the per-CPU counter, and raw_tp gets about 2 cycles per run slower: the bench runs its program from a driver program, so the walk visits one frame, and the program's atomic increment of its hit counter waits for the stores that link the frame. 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: fentry and fexit lose their gain, and trig-rawtp another 3.5%. Should the bench programs stop calling the helper? unlikely() on the walk made no measurable difference, so the series doesn't use it. Changes in v4: - Marked programs run with preemption disabled instead of keeping the per-CPU counter, so they don't drop runs any more. - The per-CPU counter is gone, and every program records itself in its frame. - The runners link the frame first and then check, walking from the outer frame. The program is recorded when the frame is linked, by bpf_set_run_ctx_prog(), so each field of the frame is written once. - Timed may_goto no longer marks a program. In v3 a mark kept the per-CPU counter; in v4 it disables preemption for the whole run, which would make a may_goto loop non-preemptible for up to 250 ms. Saving and restoring the budget around each run doesn't cover preemption, see the v3 thread. - Programs that call bpf_get_smp_processor_id() are marked. - New patch 3: bpf_async_run_ctx, which all CPUs share, is linked without writing to it. With the frame fields of patch 4, linking it with bpf_set_run_ctx() would let a program inside an async callback walk into another task's frames. - Selftest: marked programs now expect B to run after A; new fexit, fmod_ret and smp_processor_id rows. 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 (6): bpf: Avoid per-CPU private stack in preemptible tracing progs bpf: Mark programs that keep per-CPU state bpf: Link the async run context without writing to it 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 | 126 +++++--- include/linux/bpf_verifier.h | 23 ++ kernel/bpf/core.c | 12 - kernel/bpf/helpers.c | 4 +- kernel/bpf/syscall.c | 10 + kernel/bpf/trampoline.c | 34 ++- kernel/bpf/verifier.c | 58 +++- kernel/trace/bpf_trace.c | 12 +- net/bpf/test_run.c | 10 +- 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 | 338 +++++++++++++++++++++ .../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 | 275 +++++++++++++++++ .../selftests/bpf/progs/verifier_private_stack.c | 81 ++--- .../testing/selftests/bpf/progs/verifier_unwind.c | 33 +- 19 files changed, 919 insertions(+), 154 deletions(-) --- base-commit: b81e432151a69b8dc07f4a398e2166268bee9a85 change-id: 20261111-bpf-preempt-v2-r2-f9f9a0dbd5d4 Best regards, -- Mike Bazov