From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796159556; b=YpPFPG34YsY09bDJaC0EnPiU/uzC7hxaoLg8pTpA+TVZz7azNnN/hpHW5P4PMhQjU85F 6/sb2ufUc/qFu/8pBeSDgGOGRK7rupLa6pic0DxSSaOCfOppgQupEml6JhFJOSF/gxM5k MOXaPMcRfzLdK4Tk+jTWEJ/xM6/MOeqoAB1El+3MMYXSEPB5DoSaM731mS+BoDz/Wft8k zeVpL3PUvnHVa2sDHm0doEPynjnsVpwNMHdiaFaT9Z48ehjMzIHiDkqdhCZ7EBbFErSC/ S5GGxmp/NDCMcGfv7ucACKAKbaDDO1YOU2NAqRXSWXb4ZoasKh5rYNa07ZyhvrHogxw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796159556; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Content-Type:Date:Message-Id:To:Cc:Subject:From:In-Reply-To: References:Content-Transfer-Encoding:MIME-Version; bh=anVqFn6wP105WrdQEbyqi1H0e8tWCyROYViRlZwRXnE=; b=MJoGthOze57rQqMLxt8fEp7rK5+cmWl+bLHp3K5+9LMpmIcbfcnUMjMJ9Fy+jodl68IE W64zg478fAham5AZ2/OPpSrKye5j02krf8biMFvWGhiKUTT5KV/mKJNSGcxVzndRSWlMK Amvz7lZngDSNUlQB9FOQ03HJlZEybclaFN5h/MYRc/XRVhyQzoLSKjvoYWuJbzKx9j5O7 YKnbmLLOoFO8k/1ueigVsYdDaUgbgWlKm5cHmr4jJg46oARzs+mrQmdWWWMG5YTRBxVep drNnBT/Y6WCbMtLU7xTe/ksc1owzxp/GfLv37G1840y3cvtSyS30QaQ/HomAYBR1Psg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="NeRU/Rvx"; arc=none smtp.remote-ip=209.85.214.179 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7DCD6606A322 for ; Tue, 1 Dec 2026 21:12:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7DCD6606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=NeRU/Rvx Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2f0464dd6b6so75171485ad.1 for ; Tue, 01 Dec 2026 13:12:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796159555; x=1796764355; darn=vger.kernel.org; h=mime-version:content-transfer-encoding:references:in-reply-to:from :subject:cc:to:message-id:date:content-type:from:to:cc:subject:date :message-id:reply-to:content-type; bh=anVqFn6wP105WrdQEbyqi1H0e8tWCyROYViRlZwRXnE=; b=NeRU/RvxSI554FhyPe9ls+TtwMqQVhEFBTVZ+ZpBMIBzEOs6jtJdGa+Kp+z0B8hDe4 gIDYg81B8lQ0UAZ8hdQAwQAhvq2R9+evL1ZJVmcNiwEnWdyT1YKqOcsXD7ijvVF/mA5t CmzivgjzPkdamwUcw53bZOu/WQw3vOzD+t5yK/UoyMDaIbej9ufLH3FETBkavgUfjWeJ tjnCvt3UgvXFwTi7EnAnbblx5OUswsS3ezfO1I/wK+8Riq0ET98ddPoDWeLWumiaVa/Q fG50PLsMj7XH7ct2lDt4aR9HC/04C9ssh18/6lBnyIe7B2rL7dHvCcTV/2zdn5Mwa654 O6Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796159555; x=1796764355; h=mime-version:content-transfer-encoding:references:in-reply-to:from :subject:cc:to:message-id:date:content-type:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=anVqFn6wP105WrdQEbyqi1H0e8tWCyROYViRlZwRXnE=; b=mUamDJ0vOU1NXZ/UutDmWTvHjKVRZY8ETsOBXXHaJX8myjhfG85LzFLlKbwBq1HxfS BuTd0v9iF/LxL+83kPt8yPWJBJ1ZB8dpi7SQK0+SgE9jnXD02kYhTvTJYGpmbAXQoRdR iY/aUUJrf7RYJr56QhQFQBcqBHHk2BPX36QgdvKpXtJqSg+XhkmHvZbNb/JvkvsHl4/M kGdzJeauE/bhuK5k9NKY470spXOGC0iT9KbRqrUayAvw820t5/HpUJdAGtBqZ0iG4fFE XpsYL2dEkhN7dt+pnODl9X+hIcuTaJ9LQcOdHFuAor/tjv7wgqZsuceawIuW8Ai2ADBg BM+A== X-Forwarded-Encrypted: i=1; AL+hErEXmt5U4yNbDips/PlfS26aafvAuKhx0CCTyeVL6RRiIlTk5vYfqBybk+PSUAgKN5TzuGH7MfrNv2RMqY/rV7BGUN0=@vger.kernel.org X-Gm-Message-State: APuU4Z/A0itBfx/g5x5QY+QeFaAR01FCxJw9+00LEi916SIG7x/dyYFM Vj56VW0rabsysqy9drUrPBZs4rsSQmCtiddNPpYzF2Ayai4S0usJ+BB7 X-Gm-Gg: AZFdTZe30QIGDS39lQbIYkCtKVokDcy6JyA+ae+oCykfWGBe+BojJgh+dQmPuKBAjrA x9kXrX5BMS5yacgVQcnIXmdeDVIgioSrNqtXjKFKaGGmcChdj4km6ZAFTrg8V0L/KFntUFUxpSP rkBzHoziIVX8CBx5SlkcvmYVN3elFBEMN9KG9mVXQr+auyN56Hi4PRldRIS56mfNCrOm6Jb2KQj haS8Ay4gzXY0/jG9qlI7lBuARCj+DFUzggX7DmGWGlHKqSTU6a8Qolx8bay4PiT8gRxa+H9Cvra tJXrsIusG1geS4+ppeVvQgP8yeCj35734KEnoTi7tV8OnmMEuojjmzqpXqjffwxglEe/wb85YZC R8VUkd79+TID4vci5gNh2ZXixVUAbEnDzrufGcX6w8niCfrmOWw+ctbgK1biUL/nxdsKrvBHQNE LZ0z6rW1fX5I9YetM0FgoADbRhyCNoe7zIw9y+XPO0TAHlmef10rWSHGnz8xtnxcNZ5dpefBYZf GpsrnxWOE2sQ/UrmLCSC9T8MWltQ5QW5/0vTMlLqDnW18AdOqugXFxYWY7WS6XhYTtuolbB6RVE B+LaV4olgdCJ906uenSbsro8+NmaZCRgAThhhdz3bVrBOC8pD5UDvIo5///Gx+51JkuOo7U4QlV uW9GFm24SB9ORLyTGUpx3D3NSd874REN0FMk8nXmt/hqQf/E9kYPk+aA= X-Received: by 2002:a17:903:4586:b0:2ed:38c0:3328 with SMTP id d9443c01a7336-2f03f170031mr400740845ad.28.1796159554335; Tue, 01 Dec 2026 13:12:34 -0800 (PST) Received: from localhost ([153.61.198.252]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f03ee84425sm192475985ad.6.2026.12.01.13.12.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 01 Dec 2026 13:12:33 -0800 (PST) Content-Type: text/plain; charset=UTF-8 Date: Tue, 01 Dec 2026 21:12:33 +0000 Message-Id: To: , "Mike Bazov via B4 Relay" , , "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" , , Subject: Re: [PATCH bpf-next v5 3/5] bpf: Check program recursion per task via bpf_ctx From: "Alexei Starovoitov" In-Reply-To: <20261201-bpf-preempt-v2-r2-v5-3-a6bb30daae35@sentinelone.com> References: <20261201-bpf-preempt-v2-r2-v5-0-a6bb30daae35@sentinelone.com> <20261201-bpf-preempt-v2-r2-v5-3-a6bb30daae35@sentinelone.com> Content-Transfer-Encoding: 8bit X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 On Tue, Dec 01, 2026 at 07:32 AM Mike Bazov via B4 Relay wrote: > - On PREEMPT_RT a marked program runs non-preemptibly too, as kprobe > programs do. Applied, but let's keep a per-cpu counter for marked progs on RT. Pls send a followup with something like: diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 648a8b9..0f9398e 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? */ @@ -2863,10 +2872,13 @@ 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) @@ -2885,10 +2897,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 06f1286..20e7c52 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. > + * programs that get a private stack run with preemption disabled, see > + * bpf_prog_enter_run_ctx(). A sleepable one can't, so it gets none. There is an existing bug here, when the struct_ops prog is the target of a tail call. Pls follow up with a fix like: diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index 508706d..7b294c8 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -1043,7 +1043,9 @@ static void *prog_fd_array_get_ptr(struct bpf_map *map, if (IS_ERR(prog)) return prog; if (prog->type == BPF_PROG_TYPE_EXT || + (prog->type == BPF_PROG_TYPE_STRUCT_OPS && prog->aux->jits_use_priv_stack) || !bpf_prog_map_compatible(map, prog)) { bpf_prog_put(prog); return ERR_PTR(-EINVAL);