From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796159555; b=hpQUuFkrcIcrRK4gS4czop3u4bPYy3YjhhnQpS4H6Y/rNoFs1niLzxGHCKAOTmI3fFTd DnxGeqSptHROUBwjzESCr1ZUSnOmsWJyBXwRBrOHLPnpsM0LKu9vZXs0YNUu4W41DG7MZ VefghhRXQSAsZ5tQ2ja4ujHY5/luGj/T4UXNnScvsMXQCe+4odndzACSwxeFhIfvMqMN1 fIJLkGZdrKWOKxiUOMdI5I0u26ZJ/Ghb199Qb7ixqCLtiVDng4F/cFZuPxIMVSD9jPbj/ vAuPxz6D7sNnYAUDTl5yBTnp18rjAREKi9oYumBwKOu6s2fZxA7st2LvyI0L153z5Bw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796159555; 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=b2le/ptZ9YjxKIBw7f591LSiHL5C7xH52TWgCbqdUne1YioUnb+O20q3iRVeuMKYbKDe VlfVSNnBDtOAM9YKkUWGhDLk7Jt+VeX5CFo2lzolfODI69nwLT3BgsWDFrX//h8TZAEoA fjOyQu7ZKskgqH7ygyahoncTR9v/96jKMLuQ29FJx/qpxNRMgUSA0XhbwZtnobCMcbBz3 U/bILJ7ioV7Jtx3BYDOUd2pPDCgiFL3YtcObYoXK/sTHC1p+1Y8fOAN0fiY07n9+Wf3KV ks4XZJSGGuVpsZ94Pp8VIe1lKADoxJEg8qcZajexOBAa22Jl4kXwiQsPAUAP/9cTVRQ== 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.172 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 7C559606A320 for ; Tue, 1 Dec 2026 21:12:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7C559606A320 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-f172.google.com with SMTP id d9443c01a7336-2f04b676219so66494755ad.0 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=y66njHpxy1yPzmBWT2QPxPDE8RsRvSCK14o0/NfISPEVuawwMKwD2cmamEV87WyZJa sPT89QB1uHabPNYxCDzhtF0V7p6dK0kau95qyy5ObFxMaedqMYxf8V2Vnm+0tpK4X85Y 1TmFXtp7aRmOJyChGDsYtoMXpHx2J8pCtIkQff2WejLwN7957V7M0LTMPzCNv25E390c ARz3+blSNTvgi0ZQzAFv8tUVtmnVkUP1ROZinRsYWUYKUjCIxfA7Jw/SMZFj5w4mOijl tv8KEA/kZW4WHINlAOLn5y2x601q4RCKqiNorTZtFJLRkAqfozFBztz/uBIHhS7bp0PN eHuQ== X-Forwarded-Encrypted: i=1; AL+hErHLGnHk+/UY2V+2oGu7xcAZwe8SbS8DmV/QcAs1wjzxHhQOmQVxLKh2dr2Nfb6r+HWsPV8=@vger.kernel.org X-Gm-Message-State: APuU4Z9WSeFFKK85+P6Rsr6fvImFxXGopIDhZmlsTHBLJLtidmJ97qga hnLDcjAdbtRPhccUf/IKqZ57d/s25ejOT/jmXZfW2MMdW17rVFRYikLP X-Gm-Gg: AZFdTZeA/yEOtTTKPC9xPWkcmGGSrVe/bSHlbN2r8QJVoAN+bTWigfRuO3FBksOsHIM Kss0DNOY1QNsReRi5wcWl05noHKbrauk1fYmZmpx6T3I2IQjEjfugRK8g1Z1zJIsyle1h2LRg5Q FyrcUDbtlaTne8CwRAIj68yGxz2wwZAOSjCw7SwgkXLvl/nhjI434YLA99/1tnO+h8Og9VkZfKX EUx8MR1FvZ3iogZTtBQSd24KPHjzwYWAjC9wClbFYL95Zj05Jrwx/OWz3N/CcgX7I5mhxkqwrPf IvMUmQ+yAT8j5eoccMf26S8c3YBIbfu857iCja9hdF5DQMPggwmzIwxQYyqFzLmKxPZQ939wGfB AGcGXACPwo5opUsXIKaRygywCyaJEWl9isqvPk9fSzZhXXNY+9fthcQmwQwZZqLHRhzbWM+9O/L FRPLAmS7au/qcC6sFig0iumiM6AVPJME1pG8DA8XGAssp14BD0YojnqSBFJpUnbhhhHK4/Toeik ze5pRlDucSJxVjffWtF5GGjjLimObJ3Rsm0ygbrvdIxY9Fcg3KmUnNqX+d9lKn2Mtkup+H0Rppd BHxBHtMWtBLiGBDjggb0YXdEXEPlfuC2h4bcaK0/XkZjopKBm0lYnALTvuRrrHXdtb3WNUphUou IqynJM08xtRGK3BPXkn6jEA22StUC9XENeluu6a6CgTNyDIC5bW2F3G4= 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: bpf@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);