From: Peter Zijlstra <peterz@infradead.org>
To: Rustam Kovhaev <rkovhaev@gmail.com>
Cc: Sami Tolvanen <samitolvanen@google.com>,
Alexei Starovoitov <ast@kernel.org>,
linux-kernel@vger.kernel.org, bpf@vger.kernel.org
Subject: Re: kernel crashes in BPF JIT code with kCFI and clang on x86
Date: Wed, 7 Jan 2026 10:36:39 +0100 [thread overview]
Message-ID: <20260107093639.GC3707891@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20251223034332.GA2008178@nuc10>
On Mon, Dec 22, 2025 at 07:43:32PM -0800, Rustam Kovhaev wrote:
> Here is the patch that fixed it for me:
>
> diff --git a/arch/x86/include/asm/cfi.h b/arch/x86/include/asm/cfi.h
> index c40b9ebc1fb4..48f232d4b9d6 100644
> --- a/arch/x86/include/asm/cfi.h
> +++ b/arch/x86/include/asm/cfi.h
> @@ -121,6 +121,8 @@ static inline int cfi_get_offset(void)
> case CFI_FINEIBT:
> return 16;
> case CFI_KCFI:
> + if (IS_ENABLED(CONFIG_CC_IS_CLANG) && IS_ENABLED(CONFIG_CALL_PADDING))
> + return CONFIG_FUNCTION_PADDING_CFI + 5;
> if (IS_ENABLED(CONFIG_CALL_PADDING))
> return 16;
> return 5;
> diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
> index b0bac2a66eff..f8706d5b155f 100644
> --- a/arch/x86/net/bpf_jit_comp.c
> +++ b/arch/x86/net/bpf_jit_comp.c
> @@ -435,20 +435,16 @@ static void emit_fineibt(u8 **pprog, u8 *ip, u32 hash, int arity)
> static void emit_kcfi(u8 **pprog, u32 hash)
> {
> u8 *prog = *pprog;
> + size_t nop_len = 11;
> + if (IS_ENABLED(CONFIG_CC_IS_CLANG) && IS_ENABLED(CONFIG_CALL_PADDING))
> + nop_len = 55;
>
> EMIT1_off32(0xb8, hash); /* movl $hash, %eax */
> #ifdef CONFIG_CALL_PADDING
> - EMIT1(0x90);
> - EMIT1(0x90);
> - EMIT1(0x90);
> - EMIT1(0x90);
> - EMIT1(0x90);
> - EMIT1(0x90);
> - EMIT1(0x90);
> - EMIT1(0x90);
> - EMIT1(0x90);
> - EMIT1(0x90);
> - EMIT1(0x90);
> + while( nop_len > 0) {
> + EMIT1(0x90);
> + nop_len--;
> + }
> #endif
> EMIT_ENDBR();
>
> After switching to clang kbuild always generates these huge paddings in my kernel config:
> rusty@nuc10:~/code/kbuild_rust$ grep -e IBT -e PADDING .config
> CONFIG_CC_HAS_IBT=y
> CONFIG_X86_KERNEL_IBT=y
> CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING=0xa
> CONFIG_CC_HAS_ENTRY_PADDING=y
> CONFIG_FUNCTION_PADDING_CFI=59
> CONFIG_FUNCTION_PADDING_BYTES=59
> CONFIG_CALL_PADDING=y
> CONFIG_FINEIBT=y
Oh gawd, you have FUNCTION_ALIGNMENT_64B. Yeah, I suppose that wasn't
tested very well.
Let me go check all that code.
next prev parent reply other threads:[~2026-01-07 9:36 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-23 3:43 kernel crashes in BPF JIT code with kCFI and clang on x86 Rustam Kovhaev
2026-01-07 9:36 ` Peter Zijlstra [this message]
2026-01-07 15:36 ` Peter Zijlstra
2026-01-10 7:58 ` Rustam Kovhaev
2026-01-07 15:44 ` Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260107093639.GC3707891@noisy.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rkovhaev@gmail.com \
--cc=samitolvanen@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox