Skip to content

kpatch-build: Support Clang/LLVM kernel builds and fix a call padding issue - #1535

Open
howlett wants to merge 10 commits into
dynup:masterfrom
howlett:clang_20
Open

howlett wants to merge 10 commits into
dynup:masterfrom
howlett:clang_20

Conversation

@howlett

@howlett howlett commented Nov 10, 2026

Copy link
Copy Markdown

Even though kpatch is on its way out, there are a number of users on LTS releases that cannot move to klp just yet. Adding support for clang 20 help those users live patch kernels until they are able to move to the newer live patching available on newer kernels.

This series fixes an x86_64 fentry NOP replacement bug on kernels built with function prefix padding and enables kpatch-build to create live patches for kernels built with Clang/LLD/LLVM. Tested against 6.18 and clang 20.

When a kernel is built with function prefix padding (such as
CONFIG_CALL_PADDING / -fpatchable-function-entry=16,16 or CONFIG_FINEIBT),
the function entry point and its __fentry__ NOP are located at
insn_offset (rela->offset - 1) rather than at offset 0 of the function
section.

kpatch_create_ftrace_callsite_sections() already computes insn_offset
from rela->offset - 1, but when converting an R_X86_64_NONE relocation
into a call to __fentry__, it pointed insn at the start of the section
buffer (newdata) instead of newdata + insn_offset, failing with:

  unexpected instruction at the start of the function

Offset insn by insn_offset and drop the redundant reassignment to
sym->sec->data->d_buf.

Fixes: b958ed6 ("create-diff-object: Add ppc64le mcount support")
Signed-off-by: Liam R. Howlett (Google) <[email protected]>
@joe-lawrence

Copy link
Copy Markdown
Contributor

Hi @howlett thanks for MR, I'll try to review some time next week.

@joe-lawrence joe-lawrence left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See inline comments, and also commit msg nitpick ("create-diff-object: ignore Clang .compoundliteral symbols in static local checks"):

"Since .compoundliteral. contains a dot and does not start with .L, kpatch_is_normal_static_local() identifies them as compiler generated function static local variables and aborts in find_local_syms()."

Comment thread kpatch-build/lookup.c Outdated

return false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a while since I've wrapped my head around the fuzzy symbol matching, a few maybe dumb questions:

  1. This would match "foo.1" with "foo.2", do we need to worry about "foo" and "foo.1" ?
  2. The corresponding commit message mentioned, ".__UNIQUE_ID_ddebug" with no '.' character -- is that a typo, or does this function handle "...ddebug10" correctly?
  3. Related to (1) and (2): which specific symbols are hitting this fallback in lookup_local_symbol()? Could we share or refactor the logic with kpatch_mangled_strcmp() ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Correct. The lookup is trying to pick a numbered mangled symbol and not the bare named one. The builds are changing the numbers.

  2. Yes, typo in the commit message

  3. Clang's function-scoped static with a different number. We could use the same loop for the character matching, but the number part is where it falls apart. Even if all the assets are saved, it can still drift so there's no way around the numbering issue that I have found.

Comment thread kpatch-build/lookup.c
break;

if (sym->bind == STB_LOCAL &&
sym->type == lookup_sym->type &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is comparing the types only important to this new fallback / local mangled check? Should it also apply to the first (pre-existing) comparison a few lines up the function?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. When the fallback happens, then we're using a weaker matching that could be pared with an object from the same stem. The first per-existing comparison doesn't need to be so strict because the function name match is exact.

@joe-lawrence

Copy link
Copy Markdown
Contributor

@liu-song-6 : I don't know if you're still using kpatch-build anymore, but perhaps these Clang/LLVM changes might be interesting to you?

@ykopkova

Copy link
Copy Markdown
Contributor

/packit test

@ykopkova

Copy link
Copy Markdown
Contributor

/packit test --id integration-upstream

@howlett

howlett commented Nov 23, 2026

Copy link
Copy Markdown
Author

It looks like x86_64 and s390 testing failed due to infrastructure issues?
The remaining test was aarch64, which I'm not sure ever succeeded?

@joe-lawrence

Copy link
Copy Markdown
Contributor

Hi @howlett, from the x86_64 (may have been internal only) integration tests run:

:: [  PASS  ] :: Test kernel_clang_install
make -C kpatch-build
make[1]: Entering directory '/mnt/build/kpatch/kpatch-build'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/mnt/build/kpatch/kpatch-build'
make -C kpatch
make[1]: Entering directory '/mnt/build/kpatch/kpatch'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/mnt/build/kpatch/kpatch'
make -C kmod
make[1]: Entering directory '/mnt/build/kpatch/kmod'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/mnt/build/kpatch/kmod'
make -C test/integration quick
make[1]: Entering directory '/mnt/build/kpatch/test/integration'
rm -f *.ko *.log COMBINED.patch
./kpatch-test --kpatch-build-opts="-s /mnt/build/linux-6.17" -d linux-6.17.0 --quick 
build: symvers-disagreement-FAIL
build: warn-detect-FAIL
combine: skipping linux-6.17.0/symvers-disagreement-FAIL.patch
combine: skipping linux-6.17.0/warn-detect-FAIL.patch
build: combined module
ERROR: combined build failed
can't find test-COMBINED.ko, skipping
1 errors encountered
see test.log for more information
make[1]: *** [Makefile:16: quick] Error 1
make[1]: Leaving directory '/mnt/build/kpatch/test/integration'
make: *** [Makefile:50: integration-quick] Error 2
:: [  FAIL  ] :: Test integration_clang

and on the system itself:

$ grep ERROR ~/.kpatch/build.log 
ERROR: net/netlink/af_netlink.o: symbol changed sections: .Ltmp193
ERROR: drivers/tty/n_tty.o: symbol changed sections: .Ltmp10
ERROR: drivers/input/joydev.o: symbol changed sections: .Ltmp6
ERROR: fs/aio.o: symbol changed sections: .Ltmp4
ERROR: fs/proc/cmdline.o: symbol changed sections: .Ltmp1

$ uname -r
6.17.0

$ clang --version
clang version 21.1.8 (Fedora 21.1.8-6.fc43)
Target: x86_64-redhat-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Configuration file: /etc/clang/x86_64-redhat-linux-gnu-clang.cfg

This reproduces when manually trying to build the new-globals.patch by itself:

/kpatch-build/kpatch-build -s /mnt/build/linux-6.17 test/integration/linux-6.17.0/new-globals.patch 
Using source directory at /mnt/build/linux-6.17
WARNING: Clang support is experimental
Testing patch file(s)
Reading special section data
Building original source
Building patched source
Extracting new and modified ELF sections
ERROR: fs/proc/cmdline.o: symbol changed sections: .Ltmp1
create-diff-object: unreconcilable difference
fs/proc/meminfo.o: changed function: meminfo_proc_show
ERROR: 1 error(s) encountered. Check /root/.kpatch/build.log for more details.

FWIW I see those same errors before this MR's commit (we also blow up on the instruction padding problem that this MR fixes). I'd assume the changed section complains aren't related, but @howlett do any of the integration tests in test/integration/linux-6.17.0/ build for your llvm/clang/kernel-config combination?

config.linux-6.17.0.clang.x86_64.txt

@howlett

howlett commented Nov 26, 2026

Copy link
Copy Markdown
Author

Thanks for running the tests and providing the results.

Yes, the failure already existed and I'll need to fix this as well. I'll do this all in a single patch set since it looks like some environment variables are involved and make logical sense to keep together.

…ocal checks

Clang emits local STT_OBJECT symbols named .compoundliteral (and
.compoundliteral.<N> when multiple compound literals exist in a
translation unit) for C99 compound literals.

Since .compoundliteral.<N> contains a dot and does not start with .L,
kpatch_is_normal_static_local() identifies them as function static local
variables and aborts in find_local_syms().

Don't include .compoundliteral* symbols in
kpatch_is_normal_static_local().

Signed-off-by: Liam R. Howlett (Google) <[email protected]>
…atch()

MODULE_DEVICE_TABLE() generates local __mod_device_table__* symbols in
per-object files that are discarded during linking vmlinux. Add
__mod_device_table__* to maybe_discarded_sym() after __export_symbol_*
and __gendwarfksyms_*, and skip the symbols in locals_match() by
checking maybe_discarded_sym().

Signed-off-by: Liam R. Howlett (Google) <[email protected]>
…ed name

When building livepatches against Clang-compiled kernels,
compiler-generated local symbols such as <func>.<var>.<N> and
<func>.__UNIQUE_ID_ddebug<N> can receive different numerical suffixes
between the original vmlinux symbol table and the rebuilt object.

Move kpatch_mangled_strcmp() to kpatch-elf.c so it can be used in
locals_match() for correlating mangled numeric suffixes and LLVM dynamic
debug ids, and add kpatch_mangled_sym_equal() to share the '.<digits>'
matching logic when both symbols carry a numeric suffix.

Only fall back to kpatch_mangled_sym_equal() in lookup_local_symbol() if
an exact match is not found within the STT_FILE.

Finally, store the resolved symbol name in struct lookup_result::name,
allowing lookup_local_symbol() to match the target's symbol name in
.kpatch.strings and .symbols.

Signed-off-by: Liam R. Howlett (Google) <[email protected]>
Pass through LLVM setting if the correct flags are set
(CONFIG_CC_IS_CLANG, CONFIG_LD_IS_LLD, and CONFIG_AS_IS_LLVM), and the
llvm-objcopy command is accessible.

Signed-off-by: Liam R. Howlett (Google) <[email protected]>
Pass any KCFLAGS from the environment when kpatch-build exports KCFLAGS
for object compilation and the final module build.

Signed-off-by: Liam R. Howlett (Google) <[email protected]>
scripts/Makefile.clang defaults to the integrated assembler unless
LLVM_IAS=0 is passed on the make command line. If the target kernel was
built with Clang and GNU as (CONFIG_CC_IS_CLANG=y without
CONFIG_AS_IS_LLVM), kpatch-build ends up rebuilding objects with the
wrong assembler.

Add LLVM_IAS=0 to MAKEVARS when CONFIG_CC_IS_CLANG is set and
CONFIG_AS_IS_LLVM is not.

Signed-off-by: Liam R. Howlett (Google) <[email protected]>
Clang's integrated assembler can leave local .Ltmp<N> symbols in
.symtab. When a patch adds or splits functions, the .Ltmp<N> numbering
shifts across sections and create-diff-object fails with:

  symbol changed sections: .Ltmp1

Extend the LoongArch local-label pass in kpatch-elf.c to convert
relocations against .Ltmp* labels to section symbols and drop the .Ltmp*
symbols from the symbol list. Only match .Ltmp* on non-LoongArch so
mapping symbols like $x and $d on aarch64 are left alone.

If a section had no STT_SECTION symbol of its own, create one when
rewriting the relocation and check both sides have secsym before
correlating them in kpatch_correlate_section().

Signed-off-by: Liam R. Howlett (Google) <[email protected]>
symbol_get() emits a local __notrim[] char array in .no_trim_symbol for
modpost (__notrim.<N> on GCC, <func>.__notrim on Clang), which the
linker scripts discard from vmlinux and modules.

Ignore .no_trim_symbol alongside .discard and treat its symbols as
special statics in create-diff-object. Also skip __notrim symbols in
maybe_discarded_sym() so locals_match() does not fail matching the
object's local symbols against vmlinux.

Signed-off-by: Liam R. Howlett (Google) <[email protected]>
x86's SYM_PIC_ALIAS() creates global __pi_<sym> aliases in object files
for the early startup code. The startup objects reference them with
hidden visibility, so the linker turns __pi_<sym> into local symbols in
vmlinux and locals_match() fails to match the object file.

Add __pi_* to maybe_discarded_sym() so locals_match() skips them.

Signed-off-by: Liam R. Howlett (Google) <[email protected]>
@howlett

howlett commented Nov 26, 2026

Copy link
Copy Markdown
Author

Here is the updated series. It addresses the review comments and split sout KCFLAGS into its own commit, the other variable LLVM_IAS as well - which explains your .Ltmp symbol issue.

My testing shows this passing, and I would appreciate it if you ran your tests again. Thanks again for looking at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants