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=1796217359; b=MK2/gOoAPa7q+wmIuze3nTC2iWBou8STOObUD0a6TpQg+lEjboHeYst60v+7pZrmE69Z ZwRiq/Ux4DHqeCJELWOUSNpkHZTEw0h3AhKWHHxmVvHZuXiFWqRsjLy10iTff4RTWJo2a HUNJrd1IuHsDSnDzz7yQJbPbZJgLx6LFJzIP3bc4D4i4Fgubu1ldS+WUVQCmoxiuqxOzc /FXpqz/2RsKx580wH0ow2ZY6YIlbBn8XoCrdxBtkHhWoJO2gHm/KzCWXSFacDRVSQosFf ePfOyhqnqK4vVpmuJnjuAEr6mYci3hZ3E2+3H4EfAqTn0zn4otXXdaf91RTZ7tqW5fg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796217359; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=89wiJ9OkpMHCS0wBY+za4s2txu8ce10uaaZW7lz6BLY=; b=QUFq9Xm6HBwghci9DNd9SERpWXOqCXNV8wBdRurA7HTGl3utHB4WeeT2qq0rVEjoNCME L0IX24NFNd+fZu8F42I02boYfK8L2yQzh42Bs7Kb0LMvA/sqlPjzIzAxEtOZNPCyeXFtO IfCaYH4/YcuFqLQNtApdGjor/vutNUeOlN4jRxIIZRx6bfgWV3UHSq0C5sK01hRX54t4f lYpQF13FdFgaHkcVc6C+fZX6RTAOVHsMLC/ZN3GkyofoiB0YfyI+dd3cL2ix1eAfgC+45 y8blz5AfBbAMzemvK8VLUQy6F1z5fdVDaP91pNgEJe13uxGxZkJ/ufr1zUlD0UklYWQ== 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=jzt87+Ib; 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 AA6A6606A325; Wed, 2 Dec 2026 13:15:59 +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=jzt87+Ib 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=1796217359; b=Y0WUm71GY4EIDVYEvh89j9BGEavZ5+4oydBJgVC0J77X94lvQBly/7EAmBVzwZiUOVg/ INr5Si41HR+HG6p8GL0vDYpH1f2i+M+lzNVodqPVPuda4DFoYKwQ1v5mbbsCujia8S/Gg 2NtnvmduCjnljxoLIZnM8IfeEAJQsxxIWCpA2pLFTpdYcqOfZgrqNz9WUg8rp/gBjxZp+ qdB1af9QL+S2wIqB/Ty2g41SRJanVx4mAqEdXJzFQDOG7Zo84/FJRmkEH/8qT76W43DBO AQCpc9dyEXuwschAq6JQczvXttI/q22p283D/uV2p0QLfg+d64ehcB1d2xw9Slh1lEw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796217359; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=89wiJ9OkpMHCS0wBY+za4s2txu8ce10uaaZW7lz6BLY=; b=uacElfzbUtnRgEnamrkRdX4eSXE3D6pD2d2Fk6kTlvE+Gj00vQkWtDr0nVfw5fcnIdmF k6HVRoKkKm3kMfh01EiHFL86ZqtqiHHEnZqNmJQabBredb7ZIAtDSc2dxFYzVUkjGqD6m iId+INDf12xMQW/R0jTK8B4ROrwnKfABn4WhpMjFyEk8cnaGbS8fxMsWLUInf9PB3CLtW 8ou/n4JmrR9A6zDIQTt1VqPUL+nlzFxm85JfiRqRt8naky8o9NmRw03p7U1tk7bKtszow nN9JSfLEEWRhuCB+vR/KJb5njVIkwtcdk64EqPm/ZSG9HkzCCaAJ9TSkbc/7zswrgxw== 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 5F35A1F00897; Wed, 2 Dec 2026 13:15:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796217359; bh=89wiJ9OkpMHCS0wBY+za4s2txu8ce10uaaZW7lz6BLY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=jzt87+IbcKV9buHvR0UNM6pMxAAHLV3gqJxDIsCPi1SlVwSyNLj9ubtl0mZ4WeRwG hzcX8qQ7cxjEimy2A8X9Nn8eXtNLjoQYgnqn++9wBLP+anBW5IskNy2ANvSIXEOj84 e8Q5y/zv1p1XFdlW4Ha0iX93y2vqMCSuaukCafdCb4hH8CHi36XcpaUnGX+xtUxJU3 6kiWVbAyQe0yK4M7PikMxVDjQF6e1QC/MMuEMdGo2uVvx/ukmoTg+/JTMbHbUz5bNH i4L9w/5Y1QKVzBFFz5/gBJHDlxnk4rgX7RESjybHxiA7ZzwKvGZsCmMOAI3Z0qdvGN TtoKkl0cCpL1g== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id E1A434F404; Wed, 2 Dec 2026 13:15:45 +0000 (UTC) From: Mike Bazov via B4 Relay Date: Wed, 02 Dec 2026 15:15:14 +0200 Subject: [PATCH bpf-next 2/4] bpf: Keep a per-CPU counter for marked programs on PREEMPT_RT 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" Message-Id: <20261202-followup-rt-stops-v1-2-22562be9ddb0@gmail.com> References: <20261202-followup-rt-stops-v1-0-22562be9ddb0@gmail.com> In-Reply-To: <20261202-followup-rt-stops-v1-0-22562be9ddb0@gmail.com> 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=1796217344; l=4547; i=mike.bazov@gmail.com; s=gmail-20261116; h=from:subject:message-id; bh=w7gLrqfdQqjQB7LMDdsxraMMw1+dqlIANy+RZk1f0dU=; b=zwHVh933ApTJJF96b7gdQ8FnQlu1xJFcdl+r/3sTDgvBti3ZCI0zymkWvaID6utVCBhHtb03F 8Dc8wF/7yc2DyhLqkgZIKTFNQE6V75duRYU1AkhxXizquyCovJxmjnz X-Developer-Key: i=mike.bazov@gmail.com; a=ed25519; pk=Rr9fagn0pNA+bs8tgc6DXZpRAhl/maU6BeqkttIR7DA= X-Endpoint-Received: by B4 Relay for mike.bazov@gmail.com/gmail-20261116 with auth_id=1544 X-Original-From: Mike Bazov Reply-To: mike.bazov@gmail.com Content-Transfer-Encoding: 7bit From: Mike Bazov A program with per-CPU state runs with preemption disabled, so that no other run of it starts on its CPU before it exits. On PREEMPT_RT that makes the whole program, and everything it calls, non-preemptible. Keep such a program preemptible on PREEMPT_RT, and refuse another run of it on the CPU with a per-CPU counter in its stats instead. A refused run is counted as a recursion miss. Move struct bpf_prog_stats to bpf.h, where bpf_prog_enter_run_ctx() uses the counter. Trade-off: on PREEMPT_RT, a marked program drops the runs of other tasks that start on its CPU while it is preempted inside it. Suggested-by: Alexei Starovoitov Signed-off-by: Mike Bazov --- include/linux/bpf.h | 33 ++++++++++++++++++++++++--------- include/linux/filter.h | 7 ------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 0fef8e2f43c..7125557fab0 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -37,6 +37,7 @@ #include #include #include +#include #include struct bpf_verifier_env; @@ -2055,6 +2056,14 @@ struct bpf_prog_aux { struct bpf_map __rcu *st_ops_assoc; }; +struct bpf_prog_stats { + u64_stats_t cnt; + u64_stats_t nsecs; + u64_stats_t misses; + struct u64_stats_sync syncp; + int active; /* PREEMPT_RT only, see bpf_prog_enter_run_ctx() */ +} __aligned(2 * sizeof(u64)); + struct bpf_prog { u16 pages; /* Number of allocated pages */ u32 jited:1, /* Is our filter JIT'ed? */ @@ -2859,10 +2868,12 @@ static __always_inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx) * task they interrupt, so recursion through them is caught too. * * A program with per-CPU state (prog->percpu_state) runs with preemption - * disabled, so that no other run of it starts on this CPU before it exits. A - * sleepable one has to use bpf_preempt_disable() itself. The frame of a - * non-sleepable one is linked only while preemption is disabled: a task switch - * runs the scheduler, and a program there must not find @prog in the chain. + * disabled, so that no other run of it starts on this CPU before it exits, and + * its frame is linked only while preemption is disabled: a task switch runs the + * scheduler, and a program there must not find @prog in the chain. On + * PREEMPT_RT it stays preemptible, and a per-CPU counter refuses another run of + * it on this CPU instead. A sleepable one has to use bpf_preempt_disable() + * itself. * * Returns false if @prog must not run. Call bpf_prog_exit_run_ctx() either way. * Always inline: the callers are notrace. @@ -2877,10 +2888,12 @@ static __always_inline bool bpf_prog_enter_run_ctx(struct bpf_prog *prog, #endif run_ctx->saved_run_ctx = old_ctx; run_ctx->prog = prog; - if (prog->percpu_state) { - if (!prog->sleepable) + /* sleepable prog should be using explicit bpf_preempt_disable() */ + if (prog->percpu_state && !prog->sleepable) { + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) preempt_disable_notrace(); - /* sleepable prog should be using explicit bpf_preempt_disable() */ + else if (this_cpu_inc_return(prog->stats->active) != 1) + return false; } for (ctx = old_ctx; ctx; ctx = ctx->saved_run_ctx) { if (ctx->prog == prog) @@ -2899,10 +2912,12 @@ static __always_inline void bpf_prog_exit_run_ctx(struct bpf_prog *prog, struct bpf_run_ctx *run_ctx) { bpf_reset_run_ctx(run_ctx->saved_run_ctx); - if (!prog->percpu_state) + if (!prog->percpu_state || prog->sleepable) return; - if (!prog->sleepable) + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) preempt_enable_notrace(); + else + this_cpu_dec(prog->stats->active); } /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */ diff --git a/include/linux/filter.h b/include/linux/filter.h index 06f128632d5..20e7c52b741 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -807,13 +807,6 @@ struct bpf_binary_header { u8 image[] __aligned(BPF_IMAGE_ALIGNMENT); }; -struct bpf_prog_stats { - u64_stats_t cnt; - u64_stats_t nsecs; - u64_stats_t misses; - struct u64_stats_sync syncp; -} __aligned(2 * sizeof(u64)); - /* * Per-cpu memory of a prog. All may_goto of one invocation of the prog use it. An invocation is * one call from the kernel into the main prog, with the subprogs and sync callbacks it reaches. -- 2.43.0