Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.74 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100190; b=hgC5yfKHkDA7mNPUCBZb3f9aSAxdwqPNnT0G6hDVJrbRr2Lg0bvib3+sZyAVoRnfRGuo O2f77RGqBKcIaU+gWvsCBx13VtvZs3Bi62f1ncjaEhh3hJNSvZVPamWlP3xtXUOyO4Lt5 ikwRnFDo48L7FSceX5QnlYTD0WSw/4b3UzHkIuNsgBT0NCSalALhX637YiZ3b3+zIKRrK /CI8R+XxlRdKVk+KISb4GOv5JJfStlz+8n9xk2MF22OCKEtVa2Wtv66kq8eG8eO26qrJn mU7BgmJeAlFCSD5AN0N1wdq0OwIHt5tXW2vHBpJPPl8qlYLtb0ltvnZHnWC8Y7HNVGg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100190; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: X-Developer-Signature:X-Developer-Key:Content-Transfer-Encoding; bh=k0EFKtiYzvS/slfM1niSXCLt6PdMn06KK6RrhLfFfA0=; b=j/EHTsP2BPP+bBySJmT1pN336xywaZb7/23sVBP/QEHniEse0NMcsn2P+1xso5d1LrPe bc6Tp6QrC9pIR4Q4YogKfLOKRFfxd6oF6B7RHsYYVzPYQAfO31YJdvkIymo700pyK7Cb+ vdrNtiUcL9aUrFgbc94KdDsVUarikXZsyQ1kHXjG87BjhUTFTYXBM1frtCw+FbovA64D7 Vqlk5GWzkFyRVPxIn6goEIic434QcBm2RYy2P0rppO+OclKfFAnh/HdsqejLvaCscBHPu zetC/HJVLOswrLSF2/uliS7u1lZyihBIRwbI4BQWwQ/1OZaaIYfqNQiW1lgHaOf/YLg== 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="qz30/TQo"; arc=none smtp.remote-ip=74.125.231.74 Received: from mail-oa2-f10.google.com (mail-oa2-f10.google.com [74.125.231.74]) (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 DE42A606A31A for ; Tue, 1 Dec 2026 04:43:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DE42A606A31A 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=qz30/TQo Received: by mail-oa2-f10.google.com with SMTP id 586e51a60fabf-4ae5a7c2df9so8539477fac.1 for ; Mon, 30 Nov 2026 20:43:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100189; x=1796704989; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=k0EFKtiYzvS/slfM1niSXCLt6PdMn06KK6RrhLfFfA0=; b=qz30/TQoa3rgPVxbuygsAM3Kx8y5JecBAgLm8X/i/FY4Jfi560VuAPLrKtMuJEOXdr 5BSwCKmDAAzmCLEvzgI2nQ7sXruD435e/xsVCcNbEYm6R1axlTpAPfUXuYStoG0jAwmW ANZFX64m7TJ/ybOBKntGr6g/h/HsE19U5PSjPGm57b6nTxZYPxoscSKnAbnO4fbSPuYV iqOvyW3kE1fh3r84jcRABLh1yOeDXiRfZD4r5laWb7hia3E3Cqe7jlS35lYRC6Ql36Tt 2xdU1A8j1MYFNkBzRZXNgvyZuhiPzFfWiCjwfofcSnGIc4FAmYXoUuok8dlHH5V/iboM 9Z8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100189; x=1796704989; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=k0EFKtiYzvS/slfM1niSXCLt6PdMn06KK6RrhLfFfA0=; b=IrTjlgUz2Z1RTS54bwVgwAq1+KAqhvv9L2IZIoigXddLtURlejG9h52hhsuzv+hAHA ywH04kY3y9hN36i8vLRnZudryw2tz3zvkECqd0WdZz82ARKMfruXMikQH/zWMCaN0EWq XrKCXVHVVnaubvjHenkzxRtkJHTlRqANMrffy6yWG6fnloKZHlm0keJW1oQmQItfNlq0 EFqgZPRrGaXmhmJ1sF76zKkdbCAizm9rdN4TCrO4qrSUOJ+aPeQJM4V23HcqvWIa/FX7 zIdnbiEyELPpiZmr3HM3jLqHBoWYMGPRO5KJsIVwZ5fxoGv3W4hNdXoqj6dnjNEnwYyx 99qw== X-Gm-Message-State: APuU4Z94Wa6FJvrPuvHToS4uUH28SRyQb+2dAUydThmW1hYMHGRJqw7E Ov0cQBwv927e1q6FaakOzHdkNT3jEh8QNLTg10IPrfnXKLhs3UUesqSx6CORScVPVt0= X-Gm-Gg: AZFdTZfIief6WRmxA0xchuCCm1bL28MrXsWogdkDkGksCt/DzL3V+Qc+m1TrisPayMG W1SLMpuYrt9GPsG2rhhm9/gKYiYyd5c2wFKSt77z4ClvCrSEQNvmAnlBj8FEIdaaX2OsjidwVwU i5eO6+LPWjJSqE9Q1fGbrE1P9FWNhEcyBcpSQybUS6Bi6G8gG3JKdEtgtoMDqOuFwYzbv5VkOME k3kkmUIpIOz8NvJyNorm/21Rts/1xHcQ3y1R0V0SNpaQoldW0nd/dJ1c7VYcW2m1u3VezuDGZS5 pFLpoWYuoQmI25CBLRvnrXZVWZwHQr3zD4LTbgXVAXo89fu5k6gAXtijWguEJL4vAxSm7gUF3hp 8aEULu2cl0oiHcYN5VoWFemAWJMPqkBZ0jWpWhN4H62NSNC9DofTAWjnO6JzJGCsZM5uXSmwmnL t1CbOmqbrBCgx3dCRv8CFlftgA7pE0SH3NzLuid0QllDkgqMrNXRX13utuflTsluTr3QuAniBi7 n7eU1LFpRGGr6taLZcQ/ljFvJegcr3/Ew8iarBGwPKMqLWSkw3VLLEYzABrmXYeRDkH86+PKuWh 63xe5+DTJKNFBLUGxvXaWLQ6DeIbgsBZOzyQKzGOrxW521J7v0f0NzZXLC/q9HU9PzKbjyXP6Qb fk2fRpxhUUMLMQzaWC5kd5SaVbAeVuPfujtFD X-Received: by 2002:a05:6820:c451:10b0:71a:f8c9:c6bc with SMTP id 006d021491bc7-71b39507d6fmr22045170eaf.55.1796100188970; Mon, 30 Nov 2026 20:43:08 -0800 (PST) Received: from localhost ([2a03:2880:10ff:47::]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71bbfc14667sm2388759eaf.5.2026.11.30.20.43.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:43:07 -0800 (PST) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Emil Tsalapatis , Ihor Solodrai , kkd@meta.com, kernel-team@meta.com Subject: [PATCH bpf-next v1 60/73] bpf: Don't let hidden stack words turn on the private stack Date: Tue, 1 Dec 2026 05:39:27 +0100 Message-ID: X-Mailer: git-send-email 2.53.1 In-Reply-To: References: 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 X-Developer-Signature: v=1; a=openpgp-sha256; l=6857; h=from:subject:message-id; bh=JA/Dca8gsvfk9krwD0iMU78GgWpjcO0veC0uPyiUHpY=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb7HSceuUhMkfymI+V+w+/zf89KesyU/vCiSdMMwPVr6h EZrQUcrCIMbFICumyFLyfx+T8YnK34G2y7hh5rAygQxh4OIUgInsWcXwP+7v2R6f/WmCUWoOfu1q/1 fdPZmb7iIxefLv6+/fbFmso8LwT2GKUovfR6XwOfZ3Mk0OxU5Oraz2F984d41HLsdKZo9SLgA= X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit Currently the hidden words that the verifier adds for landing pads count as stack of the program when check_max_stack_depth() decides whether a function gets a private stack. One condition is that the stack, as the JIT rounds it, has 64 bytes or more, and a private stack makes the prologue and every call in the function more expensive. So one hidden word takes a function of 48 bytes to 56, which rounds to 64. Every function in which a cancellation enters a cleanup pad is going to have a hidden word of its own, the "cancelled" word. Decide by what the insns of the program use. Record that stack_depth includes the hidden words ('hidden_in_depth'), and take them off before the depth is compared with BPF_PRIV_STACK_MIN_SIZE. The slots of bpf_fastcall spills that were removed need the same. They stay in a frame that has hidden words below them, and so far they count as stack of the program there. Record that in 'fastcall_spills_gone', and count only what lies above them. Where the hidden words are kept does not change: on the private stack of a function that has one, and on the kernel stack of any other. Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf_verifier.h | 30 ++++++++++++++++++++++++++++++ kernel/bpf/pads.c | 7 ++----- kernel/bpf/verifier.c | 29 ++++++++++++++++++++--------- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 66771c403f95..e578977518fb 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1574,6 +1574,11 @@ struct bpf_subprog_info { bool ret_reg_pair: 1; /* true if bpf_fastcall stack region is used by functions that can't be inlined */ bool keep_fastcall_stack: 1; + /* + * Its bpf_fastcall spills are removed, but their slots stay in the frame, because the + * hidden words are below them, see check_max_stack_depth() + */ + bool fastcall_spills_gone: 1; bool changes_pkt_data: 1; bool might_sleep: 1; bool uses_arena_sp: 1; @@ -1665,6 +1670,8 @@ struct bpf_subprog_info { u8 owns_args; /* its hidden slots and words take that many words of 8 bytes below its stack */ u8 hidden_words; + /* stack_depth includes the hidden words, see bpf_stack_take_hidden_words() */ + bool hidden_in_depth: 1; u8 code_stack_args; /* the pad that releases nothing, for all accesses to arena with 'fault_unwinds'. 0: none */ u32 fault_pad; @@ -1708,6 +1715,29 @@ static inline u32 bpf_stack_depth_with_slots(const struct bpf_subprog_info *sub) return round_up(sub->stack_depth, BPF_REG_SIZE) + sub->hidden_words * BPF_REG_SIZE; } +/* + * The hidden words become part of the stack of @sub for good. They are below the slots of its + * bpf_fastcall spills, so check_max_stack_depth() no longer cuts those slots off the frame. + */ +static inline void bpf_stack_take_hidden_words(struct bpf_subprog_info *sub) +{ + sub->stack_depth = bpf_stack_depth_with_slots(sub); + sub->hidden_in_depth = true; +} + +/* + * How much stack the insns of the program use in @sub: without the hidden words, and without the + * slots of removed bpf_fastcall spills, which stay in a frame that has hidden words below them + */ +static inline u32 bpf_stack_depth_of_prog(const struct bpf_subprog_info *sub) +{ + if (!sub->hidden_in_depth) + return sub->stack_depth; + if (sub->fastcall_spills_gone) + return -sub->fastcall_stack_off; + return sub->stack_depth - sub->hidden_words * BPF_REG_SIZE; +} + static inline u16 bpf_in_stack_arg_cnt(const struct bpf_subprog_info *sub) { if (sub->stack_args_from_code) diff --git a/kernel/bpf/pads.c b/kernel/bpf/pads.c index e4a9f9600822..e84867527b71 100644 --- a/kernel/bpf/pads.c +++ b/kernel/bpf/pads.c @@ -3476,11 +3476,8 @@ int bpf_pads_settle(struct bpf_verifier_env *env) /* all accesses of the program to its stack are known: the hidden words go below them */ for (s = 0; !env->faults_late && s < env->subprog_cnt; s++) { sub = &env->subprog_info[s]; - if (!sub->hidden_words) - continue; - sub->stack_depth = bpf_stack_depth_with_slots(sub); - /* check_max_stack_depth() would put the stack of the callees there */ - sub->keep_fastcall_stack = true; + if (sub->hidden_words) + bpf_stack_take_hidden_words(sub); } return 0; } diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 76ba16991a24..99c7c1370211 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -6345,10 +6345,21 @@ static int check_max_stack_depth(struct bpf_verifier_env *env) find_subprog_properties(env, true); /* see bpf_remove_fastcall_spills_fills() */ - for (s = 0; s < env->subprog_cnt; s++) - for (int i = si[s].start; i < si[s + 1].start && !si[s].keep_fastcall_stack; i++) - if (env->insn_aux_data[i].fastcall_spills_num) + for (s = 0; s < env->subprog_cnt; s++) { + for (int i = si[s].start; i < si[s + 1].start && !si[s].keep_fastcall_stack; i++) { + if (!env->insn_aux_data[i].fastcall_spills_num) + continue; + /* + * The hidden words lie below the slots of the removed spills, so the frame + * cannot shrink: only record that the program no longer uses the slots. + */ + if (si[s].hidden_in_depth) + si[s].fastcall_spills_gone = true; + else si[s].stack_depth = -si[s].fastcall_stack_off; + break; + } + } for (int i = 0; i < env->subprog_cnt; i++) { if (si[i].has_tail_call) { @@ -6395,8 +6406,11 @@ static int check_max_stack_depth(struct bpf_verifier_env *env) * Request private stack support only if the subprog stack * depth is no less than BPF_PRIV_STACK_MIN_SIZE. This is to * avoid jit penalty if the stack usage is small. + * Only the stack that the insns of the program use counts: the hidden words of + * the landing pads must not make a small function pay for a private stack. */ - if (round_up_stack_depth(env, si[s].stack_depth) >= BPF_PRIV_STACK_MIN_SIZE) { + if (round_up_stack_depth(env, bpf_stack_depth_of_prog(&si[s])) >= + BPF_PRIV_STACK_MIN_SIZE) { si[s].priv_stack_mode = PRIV_STACK_ADAPTIVE; env->prog->aux->jits_use_priv_stack = true; } @@ -6426,11 +6440,8 @@ static int try_max_stack_depth(struct bpf_verifier_env *env, bool with_slots) return -ENOMEM; for (s = 0; with_slots && s < env->subprog_cnt; s++) { sub = &env->subprog_info[s]; - if (!sub->hidden_words) - continue; - sub->stack_depth = bpf_stack_depth_with_slots(sub); - /* or check_max_stack_depth() takes the spills of bpf_fastcall off the stack */ - sub->keep_fastcall_stack = true; + if (sub->hidden_words) + bpf_stack_take_hidden_words(sub); } env->log.level = 0; err = check_max_stack_depth(env); -- 2.53.1-Meta