From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.181 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796159556; b=RciQ3B7KYP7/5MUFVLNJaoq1l92Ne3+6106IW15HChgL5H19Wv4GU64cRNc0VRutt4HF HRTzeEpbJhYVVnNmCPJI8mviobIDWChU/SDNX6uEpokwbYbbHhmQ3GXF/Ce6w9AccNENp hmNarXctzHJfdQ/3ek8Aq4IsYX1pnxoWgU/5IVDyNsGylnnxajE0kAuaLGcF/LBsIl029 jqhqq2h8HYTEkmMNz6PImgFX3QjqSCs2cDtYzFsxPlma02uZdGU4JyRXOp6cS4oo6GeSs T28k1pAUAWiaxJXB88aS+eJ7qpBsMm0vROJiYh/CS8PzR7rJvCxjga99aggpySyhMhA== 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=5iK3jjKDRKudX4/4KRaE4FrNQ5HHAYEiU00AXAmuqaWo1M5EiM1NMHvAqa/MhAmanefm AW5yjzB+Elf3gfRhTCXyYZh7BJM6QMMtiOXUWERoKqxc+sQbHgAGugxLgxcfryqK/g9HP i8MKIrnXyakDgDpB8Y4Pzab7pPjANAcgrnjqdlIJqRp39oiAQyMCs1jpfoi0DWnXCEXTS XQkqp3ym5F/gJpcLBpRujg9PnZ5FOC4/GGdEzqEiXIFLhQgHLyVq7NDTMcmcJ3q0zZ9Pn lbGMOHpc98/WwHS2BgkmqbhlbDeVD5WMBHsfYIGq5/5Ba2J4BOV/uT9HVqxWVRhDjKg== 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.181 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 768C3606A31A for ; Tue, 1 Dec 2026 21:12:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 768C3606A31A 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-f181.google.com with SMTP id d9443c01a7336-2efe591a1f6so39846995ad.2 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=kBTwI1QYgIl2QqguwlwpzhTFV8B+8FZWXGiwt0ZwfxO1Uzm65mr4OND1R+aqISFN6E nQFh9hnmVIjqi/Bh+RyKriJnzv6sUg9c6wORyw0i4aT5xYvYUMwWsMB4VAxR3SQ5ZyQj 5gAm48vWkwT62l2ia5ehw3V/T+qSurGUGUkBYcRb/Td9FuShQTOlRd8uZMpW0AlVi/Pp GVeFWnLXEU7o95buWSK2U253K3eXDgcHdcbEx5yjIN5mOTCsr8/zTNuRZ3aw6ET9vz59 bWKIXex0Yl8rZv/cHT8pHskB6MW+GDpjQh7U3uIzTw4Wd3OkMADBTeg9ALwwNY/jl9j8 AS4g== X-Forwarded-Encrypted: i=1; AL+hErH2saowzo74l4G7vzaJ/vawibScqjetRpStdPrvoXlukue1lv36nRQnHeixakZLhomjv1H3fdj7RAWpHPw=@vger.kernel.org X-Gm-Message-State: APuU4Z/hcpgQsvzyaw55wXqFcH4pK7Ob78R836GfHDtWjnhPCusxIRDd BK1beqwUqnazfqrhOpCpzWL/OvgWTbV8IjvSg5uCyiYcDOBob4WR4Tvk X-Gm-Gg: AZFdTZctoaR7qc2FoDfqeIzZHNjKvzhigodPWaPRRIWkWgg+Z6+9N0swsuxHKjh8t/a IVeAxoX7S0fDbkd7CshDH6Cg0VnKAASKYw5gJ3NieEdmiDW2uYKQMzWATDNNCGMmx4cje7JVmGh eCjHvMsPM62nMElwAHXTiLyjxr7izbC6TmVZH3kONXthGf0OExMbQjqWKisrKs2a5tY1oIiqJYR k72iR0RoDFKzcbtXbFMSgFNggVWlAgXzlAHR23PDwyXd3zp65DS+ZcwviDK540NFNZmhP5Oykir SrWXutCAW0nCkr9GB9m17HtiXecwY9fQ/Kgxa+6k4xQ3gqz5vQufMTF6gvxBzkzK5JgqDmT0C80 J1WneNllPPZZEUPLAXjNCYJ1/jiUXHL/Vs0ydBc+HciyVn6/FZYZhveUEONnPvU3FvXr6G359gz dF3YVYANWnHTLeoyEPRMJJuT6M1M3TvIJOCcZYBitt2vnnyp4tgAyQUi8r2xPhGcjeadjf4x+ff zPlszN8B/cPZDF9fiHJiL2JiZUMmyfRX3/RpVKhy1oi5iMxb6zoB7owAPn1n6KOuSOmkgjuQg81 f+ZHUvzY9tgCCknJSOy4NNzwB6vxA36eWCT89hDidjPR5HE5TxPFiqm0ahZ6lHRPFtzQGUanmLY ux5LVGuoQWG1BuEZ2kBA7ExMjyevmNWEmc+Tf1T+2TrOXhdJ8rPlkFW0= 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-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);