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=1796139183; b=mXodv+sMndQWztLgh+/sgBQTwkywqi5DqmkrcRdjlmtoF1ooH4XUncHTTqpLXGbZaCzU nmLAdgJo24dgxlpkPuQ+229lf8UUXhDsBcRNmj6YAjsxZaPCWCl00bI1u9F073jXjNhpA Im9OvRaU4XRxS/DZQZJWfcwF3h0pXkfl80aUMzG64jqcjOKuWvtOHJJza+q0vIpt7GlDg yduzajjFob8+0FHcJAhUZD2RsnZVRcD6U6W/tkx9DJ8l8Bb/mExBRApVBYxVaUnxRK5zw db6uAYWwm1IGqgg+On9a/HQIPnKsGg1IqHzWfrdUOX+QlU7yvICnPwRCmGTbZG173qA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796139183; 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=hft2dhO7WqFK8OBU5JA7r4puOBdy6x7OuZLIAoIOzBA=; b=RSpI04unr/fDVvJuzfqpHaCPy/3axdtgW18sTrDqJ0g2ReXzqt1WLydVc82V94yvyC1e BYW+3dpzXdyI7Ad8q6bEP2i9P9fpSiOcdQxV3TWXDCkDhP5GbVjMZzWGC9UXpL1PY61Ik nXPPkljVSRKLpARXw0PjjathBSjPKgT8jMeZZ91ZCkyi/46/3dYpYHMRZQZwC78qYTiLN ukSNW866c6bhB8Fh/O8zIKvTMPeQGDODHtHcGgf31GNMI4PCST2O1qGAenGc4fUSeN5Ef 2wBhGak7lGNUJHXFqPdtujt/4xrEQdJTVOA1gsJ+IvIQUZOa/I2WCF9VHJBp1Rvc1vA== 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=HiK+RJWr; 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 7120A606A33E; Tue, 1 Dec 2026 15:33:03 +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=HiK+RJWr 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=1796139183; b=ic5xeZCoWchw69ZYkfDFK3Ix1pkW9PmnS8+2uk69DhtFuTiLA/Fl7ixwS3NAOVIHOkGu +8FTxrAE2b47r++cduwe74Pxq0Mv9RADE32Dx00QsP2r+Li86sYda+0S5rhBNbdN5PbMJ 8pQJjB/vqFcm8XDZfzOeAoeUek9S1CgIPDnL70hpVcaLQMS44iil5JIVVnpRTFV8QPOC6 R6cwD5IZpom31FOyKTZbPLgfBC92Y7nZRAGcAFmGHmXtCIqmx+/Vc9Nz2bqxcN0XytNqt HHInZp/fbFqlt9NPoJKUvRJKswk3clPtLdWMKBRDqHqw2qeccyrbqZFgiCMDjgzu+xw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796139183; 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=hft2dhO7WqFK8OBU5JA7r4puOBdy6x7OuZLIAoIOzBA=; b=2B1cxh6MMWlRDPtFtizFUTueqHWNxNcnVIIPRO8HKXA59nh+m8sFVfk3DaMJUT5aIjmj FF4fLEGFiSVyXOmDsAoiE1VRiP5BxTHgECplIpAHrR/focf6oQz4RiCl1R4g2opWiDiDe hQCec605ZZtRqFzl+v3Antp6DwwVB7EL9c+6S2y2/mxB/EaVTNBGqZRMf0kOKGLPCYWTR rxFwjwI9Oz7ZH7FQH73AxyAhmI6mdXq/3Vy54VqUH7S092Rhm/CmPHk4412ZQajE/DW5W XjOl2jyo+VUzE7rL4Q1HLRAEuCTTdWPFVUVf9Hn+JQyZS8h1ET71eNjaqDfmJlkueSg== 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 49A151F00933; Tue, 1 Dec 2026 15:32:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796139183; bh=hft2dhO7WqFK8OBU5JA7r4puOBdy6x7OuZLIAoIOzBA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=HiK+RJWrZcTNxCTnUsCrrJNuZnAXxAdp8ojp0KRz7RlqAqJ3FXIE+2zajvdGf+7+q Fi6lQEFvDbTP7HwqoVjxH2tsBohh1J6URSk7kTCPJsmeZhmZguDwAGk1boLCUk6qgU 7Na7BYsSU+Mj0d8Ldslty9rfRxsd/oVvhh5Iu1ZCxBb7Aqxck+BbIki1AU16OEsw9E FEApy+1FzyY0NZ/YRF+BtlwMSwhn9kbQJEdJk6g/hgJAuQlXABHQK9Xv1c7BSHoHgR 2iMtGMQdq6FCVDxZO7TidK1BSRNlrd32MM7MXzDupNN9YZtMh/+t0B8xY3PhClcn60 sRuaqhhl9Bcpg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id C225B4EE78; Tue, 1 Dec 2026 15:32:33 +0000 (UTC) From: Mike Bazov via B4 Relay Date: Tue, 01 Dec 2026 17:32:13 +0200 Subject: [PATCH bpf-next v5 2/5] bpf: Mark programs that keep per-CPU state 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: <20261201-bpf-preempt-v2-r2-v5-2-a6bb30daae35@sentinelone.com> References: <20261201-bpf-preempt-v2-r2-v5-0-a6bb30daae35@sentinelone.com> In-Reply-To: <20261201-bpf-preempt-v2-r2-v5-0-a6bb30daae35@sentinelone.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=1796139152; l=5020; i=mikeba@sentinelone.com; s=20261022; h=from:subject:message-id; bh=MAN4BsOQsF+2236yMU2IzLuGAdc8pvBefiw3r1JXMCY=; b=/XvuD2BIDcSsLT+0vXD7YcmiYwlIcPrf1vKF27XNnt1YpbtZ+iy646xblTyWBC8OhKbLcp3W4 FfrQ57BZOzFBAlxc5CZDBA/sX0PhCsRov6mIyjTVUzbJRBKR2S0qV3m 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 Content-Transfer-Encoding: 7bit From: Mike Bazov Once recursion is checked per task, a second task can enter a tracing or raw tracepoint program while another task is preempted inside it on the same CPU. Some programs rely on that not happening: - A per-CPU map value that a program looked up stays its own until the run ends: programs use these maps as scratch space. - A private stack is per CPU. Of the programs whose recursion check becomes per task, only struct_ops programs that request one have it. Mark them at load time, so that their runs on a CPU can be kept apart. Set prog->percpu_state when the verifier sees: - a helper called with a per-CPU map, also through an inner map, - the address of a per-CPU array value loaded directly, - bpf_this_cpu_ptr() or bpf_per_cpu_ptr(), - bpf_get_smp_processor_id(): the CPU id often indexes per-CPU slots that the program keeps itself, e.g. in a regular array, - a tail call: the target runs in the caller's run and may use a per-CPU map, - a private stack. Only code the verifier walks counts: a per-CPU map referenced in a branch that a constant in .rodata disables doesn't mark the program. Timed may_goto doesn't mark a program: a marked program would run the loop with preemption disabled, for up to 250 ms. Its budget is per-CPU memory that the first insns of a run reset, so a run that preempts another one can renew the other's budget. lsm, cgroup and uprobe progs, and struct_ops progs without a private stack, already share the budget this way. No functional change: nothing reads the flag yet. Trade-off: every program that calls bpf_get_smp_processor_id() is marked, also one that only records the CPU id. Signed-off-by: Mike Bazov --- include/linux/bpf.h | 3 ++- kernel/bpf/verifier.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 4f9c2bf64643..2dfd963a2703 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2075,7 +2075,8 @@ struct bpf_prog { call_get_func_ip:1, /* Do we call get_func_ip() */ call_session_cookie:1, /* Do we call bpf_session_cookie() */ tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */ - sleepable:1; /* BPF program is sleepable */ + sleepable:1, /* BPF program is sleepable */ + percpu_state:1; /* Runs must not overlap on a CPU */ enum bpf_prog_type type; /* Type of BPF program */ enum bpf_attach_type expected_attach_type; /* For some prog types */ u32 len; /* Number of filter blocks */ diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index d43b54caa9a1..dd35abb37a23 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -6158,6 +6158,7 @@ static int check_max_stack_depth(struct bpf_verifier_env *env) if (round_up_stack_depth(env, si[s].stack_depth) >= BPF_PRIV_STACK_MIN_SIZE) { si[s].priv_stack_mode = PRIV_STACK_ADAPTIVE; env->prog->aux->jits_use_priv_stack = true; + env->prog->percpu_state = true; } } @@ -13106,6 +13107,10 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn if (err) return err; + /* A per-CPU value stays the run's own only while no other run starts on the CPU */ + if (meta.map.ptr && bpf_map_is_percpu_map(meta.map.ptr->map_type)) + env->prog->percpu_state = true; + err = record_func_map(env, &meta, func_id, insn_idx); if (err) return err; @@ -13132,10 +13137,16 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn switch (func_id) { case BPF_FUNC_tail_call: + /* The target runs without a recursion check of its own */ + env->prog->percpu_state = true; err = check_resource_leak(env, false, true, "tail_call"); if (err) return err; break; + case BPF_FUNC_get_smp_processor_id: + /* The CPU id often indexes per-CPU slots that the program keeps itself */ + env->prog->percpu_state = true; + break; case BPF_FUNC_get_local_storage: /* check that flags argument in get_local_storage(map, flags) is 0, * this is required because get_local_storage() can't return an error. @@ -13247,6 +13258,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn struct bpf_reg_state *reg = ®s[BPF_REG_1]; const struct btf_type *type; + env->prog->percpu_state = true; + if (reg->type & MEM_RCU) { type = btf_type_by_id(reg->btf, reg->btf_id); if (!type || !btf_type_is_struct(type)) { @@ -19638,6 +19651,8 @@ static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) __mark_reg_known(dst_reg, aux->map_off); dst_reg->type = PTR_TO_MAP_VALUE; dst_reg->map_ptr = map; + if (bpf_map_is_percpu_map(map->map_type)) + env->prog->percpu_state = true; WARN_ON_ONCE(map->map_type != BPF_MAP_TYPE_INSN_ARRAY && map->max_entries != 1); /* We want reg->id to be same (0) as map_value is not distinct */ -- 2.43.0