LoongArch: Add livepatch build (KLP) support
Subsystems (4)
v3 cover LoongArch: Add livepatch build (KLP) support
This series adds LoongArch support for the klp-build livepatch tooling, enabling automated livepatch module generation using objtool on LoongArch.
It is based on Josh Poimboeuf's klp-build series [1] (see base-commit below, unchanged from v1/v2) and extends it with the LoongArch-specific objtool and build pieces, plus toolchain/relocation fixes required to make livepatch modules load, generate, and run correctly under both GCC and Clang.
Overview:
- Patches 1-8 are unchanged from v2 [3]: the LoongArch objtool hooks, special-section marking, the -fPIC/-mdirect-extern-access fixes, EFI linking, KLP_SYSCALL_DEFINEx() support, and finally wiring up LoongArch klp-build (kept last so the series stays bisectable).
- Patches 9-12 are new in v3, fixing issues Joe Lawrence found across two rounds of review by actually running the full klp-build flow (see Changes since v2 below for details).
All new patches (9-12) fix bugs in objtool/klp-diff.c and the LoongArch Kconfig; none of them touch other architectures.
Testing:
GCC on LoongArch hardware: livepatch modules generated, loaded, and exercised (unchanged from v2).
Clang 21.1.8 on LoongArch hardware, reproduced end to end with Joe's exact test case (test-joe.patch below), since it happens to exercise patches 10-12 together: netif_rx_internal() has __ex_table entries, a __bug_table entry, and three __jump_table static keys, one of which (a tracepoint's key) is unexported and not defined in the same translation unit -- exactly the case patch 12 fixes.
$ cat test-joe.patch
diff --git a/net/core/dev.c b/net/core/dev.c
index 06c195906231..14fd3d5f351a 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -5717,6 +5717,7 @@ static int netif_rx_internal(struct sk_buff *skb)
unsigned int qtail; ret = enqueue_to_backlog(skb, smp_processor_id(), &qtail);
+ pr_info("test");
}
return ret;}
$ LLVM=/path/to/clang-21/bin/ ./scripts/livepatch/klp-build -T test-joe.patch ... $ ls livepatch-test-joe.ko
klp-build completes cleanly: no "duplicate reloc" (patch 12), no "Cannot find symbol for section" (patch 9), and the __ex_table/ __jump_table entries for netif_rx_internal are present in the output module (patches 10-11). The module was then loaded into a VM running a matching Clang-built kernel:
# insmod livepatch-test-joe.ko [ 105.584252] livepatch: enabling patch 'livepatch_test_joe' [ 105.586141] livepatch: 'livepatch_test_joe': starting patching transition [ 106.824310] livepatch: 'livepatch_test_joe': patching complete [ 120.036530] test
# cat /sys/kernel/livepatch/livepatch_test_joe/{enabled,transition}
1
0Toggling the netif_rx tracepoint exercises the jump-table relocation patch 12 folds for the unexported __tracepoint_netif_rx key, and lets us cross-check the patch's own pr_info() against independent evidence that the static branch actually flipped:
# echo 1 > /sys/kernel/debug/tracing/events/net/netif_rx/enable
# ping -c2 127.0.0.1
# cat /sys/kernel/debug/tracing/trace | tail -4
ping-1267 [001] b.... 472.753651: netif_rx: dev=lo skbaddr=... len=84
ping-1267 [001] b.s2. 472.753671: netif_rx: dev=lo skbaddr=... len=84
ping-1267 [001] b.... 473.762993: netif_rx: dev=lo skbaddr=... len=84
ping-1267 [001] b.s2. 473.763096: netif_rx: dev=lo skbaddr=... len=84
# dmesg | tail -4
[ 472.753653] test
[ 472.753671] test
[ 473.762994] test
[ 473.763096] testEach traced packet has a matching "test" line at the same timestamp (microsecond-aligned), confirming the tracepoint's static-key relocation and the patch's own code path are both live at once. Disabling and unloading works cleanly:
# echo 0 > /sys/kernel/livepatch/livepatch_test_joe/enabled [ 557.922932] livepatch: 'livepatch_test_joe': starting unpatching transition [ 558.905248] livepatch: 'livepatch_test_joe': unpatching complete # rmmod livepatch_test_joe
Changes since v2 [3]:
- Patches 1-8 are unchanged.
- New patch 9: select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY. arch/loongarch/Makefile already builds with -fpatchable-function-entry=2, but nothing selected this, so kbuild ran a pointless recordmcount pass on every object. Harmless under GCC (GAS always emits a section symbol for recordmcount to anchor on), but it fails under Clang with -ffunction-sections (as klp-build uses) for any __weak function that ends up alone in its section, e.g. sched_clock. (Joe Lawrence)
- New patch 10: convert local-label references in special sections. GCC/GAS on LoongArch reference __ex_table/__bug_table/__jump_table entries via a local text label instead of the usual "section symbol
+ offset" that objtool's klp-diff.c assumes; the label is never cloned into the livepatch module, so such entries were silently dropped from GCC-built modules with no error at build or load time. (Joe Lawrence)
- New patch 11: fix ANNOTATE_DATA_SPECIAL parsing for the same local label references. create_fake_symbols() computed each annotated entry's offset from the relocation's addend alone, which is only correct for the section-symbol form; with local labels, every entry in a special section got offset 0, producing a single fake symbol that covered (and pulled into the output module) the section's entire contents instead of just the patched function's entries. Found while verifying patch 10 with a real special-section entry.
- New patch 12: fold LoongArch's paired ADD/SUB relocations into a single PC-relative one. The "key - ." field of a __jump_table entry can come out as two relocations at the same offset (R_LARCH_ADD64 + R_LARCH_SUB64) when Clang's integrated assembler encounters a key that isn't defined in the same translation unit (e.g. a tracepoint's static key); objtool only allows one relocation per offset, so cloning such an entry failed with "duplicate reloc". (Joe Lawrence)
[1] https://git.kernel.org/pub/scm/linux/kernel/git/jpoimboe/linux.git/log/?h=klp-build-arm64
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
George Guo (12):
objtool/LoongArch: Add arch_adjusted_addend() for KLP support
LoongArch: Mark special sections for KLP support livepatch/klp-build: disable direct-extern-access for LoongArch to fix
kernel panic
livepatch/klp-build: build LoongArch with -fPIC to keep GOT-indirect
symbol references
LoongArch: Fix EFI linking with -fdata-sections objtool/klp: Add LoongArch jump opcode bytes support
klp-build: Add LoongArch syscall patching macro
LoongArch: Add livepatch build (KLP) support
LoongArch: Select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY objtool/klp: Convert local label references in special sections objtool/klp: Fix ANNOTATE_DATA_SPECIAL parsing for local label
references
objtool/klp: Fold LoongArch paired ADD/SUB relocations into PCREL
arch/loongarch/Kconfig | 2 + arch/loongarch/include/asm/alternative-asm.h | 5 +- arch/loongarch/include/asm/alternative.h | 6 +- arch/loongarch/include/asm/asm-extable.h | 10 +- arch/loongarch/include/asm/bug.h | 1 + arch/loongarch/include/asm/jump_label.h | 2 + arch/loongarch/kernel/asm-offsets.c | 20 ++++ arch/loongarch/kernel/vmlinux.lds.S | 2 +- include/linux/livepatch_helpers.h | 22 ++++ scripts/livepatch/klp-build | 36 +++++- tools/objtool/Makefile | 3 +- tools/objtool/arch/loongarch/decode.c | 108 ++++++++++++++++++ .../objtool/arch/loongarch/include/arch/elf.h | 18 +++ tools/objtool/klp-diff.c | 57 ++++++++- 14 files changed, 280 insertions(+), 12 deletions(-)
base-commit: 85afaba140a4b9f20fe8c8a64b24fc85f022d981
Signature · 1 line
2.25.1
PATCH v3 1/12 objtool/LoongArch: Add arch_adjusted_addend() for KLP support
From: George Guo <[email protected]>
Implement arch_adjusted_addend() to return the relocation addend value.
--- tools/objtool/arch/loongarch/decode.c | 5 +++++ 1 file changed, 5 insertions(+)
tools/objtool/arch/loongarch/decode.c +5 -0
@@ -34,6 +34,11 @@ s64 arch_insn_adjusted_addend(struct instruction *insn, struct reloc *reloc)return reloc_addend (reloc); } +u64 arch_adjusted_addend (struct reloc *reloc) +{ +return reloc_addend (reloc); +} +bool arch_pc_relative_reloc (struct reloc *reloc) {return false;
Signature · 1 line
2.25.1
PATCH v3 2/12 LoongArch: Mark special sections for KLP support
In reply to George GuoFrom: George Guo <[email protected]>
objtool needs to split the kernel's special sections into per-entry symbols when generating livepatch modules. Mark them so it can:
- .altinstructions and __ex_table are read-only, so set an explicit entry size via SHF_MERGE (ALT_INSTR_SIZE / EXTABLE_SIZE).
- __bug_table and __jump_table are writable. SHF_MERGE cannot be combined with SHF_WRITE (Clang rejects it), so annotate each entry with ANNOTATE_DATA_SPECIAL instead, mirroring x86/arm64.
--- arch/loongarch/include/asm/alternative-asm.h | 5 +++-- arch/loongarch/include/asm/alternative.h | 6 ++++-- arch/loongarch/include/asm/asm-extable.h | 10 ++++++++-- arch/loongarch/include/asm/bug.h | 1 + arch/loongarch/include/asm/jump_label.h | 2 ++ arch/loongarch/kernel/asm-offsets.c | 20 ++++++++++++++++++++ 6 files changed, 38 insertions(+), 6 deletions(-)
arch/loongarch/include/asm/alternative-asm.h +3 -2
@@ -5,6 +5,7 @@#ifdef __ASSEMBLER__#include <asm/asm.h> +#include <generated/asm-offsets.h> /* * Issue one struct alt_instr descriptor entry (need to put it into
@@ -33,7 +34,7 @@ .fill - (((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)) / 4, 4, 0x03400000 142 : - .pushsection .altinstructions,"a" + .pushsection .altinstructions, "aM", @progbits, ALT_INSTR_SIZE altinstruction_entry 140b, 143f, \feature, 142b-140b, 144f-143f .popsection
@@ -63,7 +64,7 @@ (alt_max_short (new_len1, new_len2) - (old_len)) /4 ,4 ,0x03400000 142 : - .pushsection .altinstructions,"a" + .pushsection .altinstructions, "aM", @progbits, ALT_INSTR_SIZE altinstruction_entry 140b, 143f, \feature1, 142b-140b, 144f-143f, 142b-141b altinstruction_entry 140b, 144f, \feature2, 142b-140b, 145f-144f, 142b-141b .popsection
arch/loongarch/include/asm/alternative.h +4 -2
@@ -70,7 +70,8 @@ extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);/* alternative assembly primitive: */ #define ALTERNATIVE (oldinstr, newinstr, feature) \ OLDINSTR(oldinstr, 1) \ - ".pushsection .altinstructions,\"a\"\n" \ + ".pushsection .altinstructions, \"aM\", @progbits, " \ + __stringify(ALT_INSTR_SIZE) "\n" \ ALTINSTR_ENTRY(feature, 1) \ ".popsection\n" \ ".subsection 1\n" \
@@ -79,7 +80,8 @@ extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);#define ALTERNATIVE_2 (oldinstr, newinstr1, feature1, newinstr2, feature2)\ OLDINSTR_2(oldinstr, 1, 2) \ - ".pushsection .altinstructions,\"a\"\n" \ + ".pushsection .altinstructions, \"aM\", @progbits, " \ + __stringify(ALT_INSTR_SIZE) "\n" \ ALTINSTR_ENTRY(feature1, 1) \ ALTINSTR_ENTRY(feature2, 2) \ ".popsection\n" \
arch/loongarch/include/asm/asm-extable.h +8 -2
@@ -7,10 +7,15 @@#define EX_TYPE_UACCESS_ERR_ZERO 2#define EX_TYPE_BPF 3 +#ifndef COMPILE_OFFSETS +#include <asm/asm-offsets.h> +#endif +#ifdef __ASSEMBLER__#define __ASM_EXTABLE_RAW (insn, fixup, type, data) \ - .pushsection __ex_table, "a"; \ + .pushsection __ex_table, "aM", @progbits, \ + EXTABLE_SIZE; \ .balign 4; \ .long ((insn) - .); \ .long ((fixup) - .); \
@@ -29,7 +34,8 @@#include <asm/gpr-num.h> #define __ASM_EXTABLE_RAW (insn, fixup, type, data) \ - ".pushsection __ex_table, \"a\"\n" \ + ".pushsection __ex_table, \"aM\", @progbits, "\ + __stringify(EXTABLE_SIZE) "\n" \ ".balign 4\n" \ ".long ((" insn ") - .)\n" \ ".long ((" fixup ") - .)\n" \
arch/loongarch/include/asm/bug.h +1 -0
@@ -25,6 +25,7 @@#define __BUG_ENTRY (cond_str, flags) \ .pushsection __bug_table, "aw"; \ .align 2; \ + __ANNOTATE_DATA_SPECIAL; \ 10000: .long 10001f - .; \ _BUGVERBOSE_LOCATION(WARN_CONDITION_STR(cond_str) __FILE__, __LINE__) \ .short flags; \
arch/loongarch/include/asm/jump_label.h +2 -0
@@ -12,6 +12,7 @@#include <linux/types.h> #include <linux/stringify.h> #include <asm/asm.h> +#include <linux/objtool.h> #define HAVE_JUMP_LABEL_BATCH
@@ -27,6 +28,7 @@#define JUMP_TABLE_ENTRY (key, label) \ ".pushsection __jump_table, \"aw\" \n\t" \ ".align " __stringify(PTRLOG) " \n\t" \ + ANNOTATE_DATA_SPECIAL "\n\t" \ ".long 1b - ., " label " - . \n\t" \ JUMP_LABEL_TYPE key " - . \n\t" \ ".popsection \n\t"
arch/loongarch/kernel/asm-offsets.c +20 -0
@@ -18,6 +18,8 @@#include <asm/ftrace.h> #include <asm/sigframe.h> #include <vdso/datapage.h> +#include <asm/alternative.h> +#include <asm/extable.h> static void __used output_ptreg_defines (void ) {
@@ -323,3 +325,21 @@ static void __used output_vdso_defines(void)DEFINE (__VDSO_PAGES,VDSO_NR_PAGES );BLANK (); } + +static void __used output_extable_defines (void ) +{ +COMMENT ("LoongArch exception table entry offsets." ); + +DEFINE (EXTABLE_SIZE ,sizeof (struct exception_table_entry )); +BLANK (); +} + +static void __used output_alt_instr_defines (void ) +{ +COMMENT ("LoongArch alternative instructions offsets." ); + +DEFINE (ALT_INSTR_SIZE ,sizeof (struct alt_instr )); +BLANK (); +} + +
Signature · 1 line
2.25.1
Joe Lawrence
maintainerRe: [PATCH v3 02/12] LoongArch: Mark special sections for KLP support
Quoted 179 lines · George Guo
From: George Guo <[email protected]>
objtool needs to split the kernel's special sections into per-entry symbols when generating livepatch modules. Mark them so it can:
- .altinstructions and
__ex_tableare read-only, so set an explicit entry size viaSHF_MERGE(ALT_INSTR_SIZE/EXTABLE_SIZE).-
__bug_tableand__jump_tableare writable.SHF_MERGEcannot be combined withSHF_WRITE(Clang rejects it), so annotate each entry withANNOTATE_DATA_SPECIALinstead, mirroring x86/arm64.Co-developed-by Kexin Liu <[email protected]>Signed-off-by Kexin Liu <[email protected]>Signed-off-by George Guo <[email protected]>--- arch/loongarch/include/asm/alternative-asm.h | 5 +++-- arch/loongarch/include/asm/alternative.h | 6 ++++-- arch/loongarch/include/asm/asm-extable.h | 10 ++++++++-- arch/loongarch/include/asm/bug.h | 1 + arch/loongarch/include/asm/jump_label.h | 2 ++ arch/loongarch/kernel/asm-offsets.c | 20 ++++++++++++++++++++ 6 files changed, 38 insertions(+), 6 deletions(-)arch/loongarch/include/asm/alternative-asm.h +3
-2@@ -5,6 +5,7 @@#ifdef __ASSEMBLER__#include <asm/asm.h> +#include <generated/asm-offsets.h> /* * Issue one struct alt_instr descriptor entry (need to put it into @@ -33,7 +34,7 @@ .fill - (((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)) / 4, 4, 0x03400000 142 : - .pushsection .altinstructions,"a" + .pushsection .altinstructions, "aM", @progbits, ALT_INSTR_SIZE altinstruction_entry 140b, 143f, \feature, 142b-140b, 144f-143f .popsection@@ -63,7 +64,7 @@ (alt_max_short (new_len1, new_len2) - (old_len)) /4 ,4 ,0x03400000 142 : - .pushsection .altinstructions,"a" + .pushsection .altinstructions, "aM", @progbits, ALT_INSTR_SIZE altinstruction_entry 140b, 143f, \feature1, 142b-140b, 144f-143f, 142b-141b altinstruction_entry 140b, 144f, \feature2, 142b-140b, 145f-144f, 142b-141b .popsectionarch/loongarch/include/asm/alternative.h +4
-2@@ -70,7 +70,8 @@ extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);/* alternative assembly primitive: */ #define ALTERNATIVE (oldinstr, newinstr, feature) \ OLDINSTR(oldinstr, 1) \ - ".pushsection .altinstructions,\"a\"\n" \ + ".pushsection .altinstructions, \"aM\", @progbits, " \ + __stringify(ALT_INSTR_SIZE) "\n" \ ALTINSTR_ENTRY(feature, 1) \ ".popsection\n" \ ".subsection 1\n" \@@ -79,7 +80,8 @@ extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);#define ALTERNATIVE_2 (oldinstr, newinstr1, feature1, newinstr2, feature2)\ OLDINSTR_2(oldinstr, 1, 2) \ - ".pushsection .altinstructions,\"a\"\n" \ + ".pushsection .altinstructions, \"aM\", @progbits, " \ + __stringify(ALT_INSTR_SIZE) "\n" \ ALTINSTR_ENTRY(feature1, 1) \ ALTINSTR_ENTRY(feature2, 2) \ ".popsection\n" \arch/loongarch/include/asm/asm-extable.h +8
-2@@ -7,10 +7,15 @@#define EX_TYPE_UACCESS_ERR_ZERO 2#define EX_TYPE_BPF 3 +#ifndef COMPILE_OFFSETS +#include <asm/asm-offsets.h> +#endif +#ifdef __ASSEMBLER__#define __ASM_EXTABLE_RAW (insn, fixup, type, data) \ - .pushsection __ex_table, "a"; \ + .pushsection __ex_table, "aM", @progbits, \ + EXTABLE_SIZE; \ .balign 4; \ .long ((insn) - .); \ .long ((fixup) - .); \@@ -29,7 +34,8 @@#include <asm/gpr-num.h> #define __ASM_EXTABLE_RAW (insn, fixup, type, data) \ - ".pushsection __ex_table, \"a\"\n" \ + ".pushsection __ex_table, \"aM\", @progbits, "\ + __stringify(EXTABLE_SIZE) "\n" \ ".balign 4\n" \ ".long ((" insn ") - .)\n" \ ".long ((" fixup ") - .)\n" \arch/loongarch/include/asm/bug.h +1
-0@@ -25,6 +25,7 @@#define __BUG_ENTRY (cond_str, flags) \ .pushsection __bug_table, "aw"; \ .align 2; \ + __ANNOTATE_DATA_SPECIAL; \ 10000: .long 10001f - .; \ _BUGVERBOSE_LOCATION(WARN_CONDITION_STR(cond_str) __FILE__, __LINE__) \ .short flags; \arch/loongarch/include/asm/jump_label.h +2
-0@@ -12,6 +12,7 @@#include <linux/types.h> #include <linux/stringify.h> #include <asm/asm.h> +#include <linux/objtool.h> #define HAVE_JUMP_LABEL_BATCH @@ -27,6 +28,7 @@#define JUMP_TABLE_ENTRY (key, label) \ ".pushsection __jump_table, \"aw\" \n\t" \ ".align " __stringify(PTRLOG) " \n\t" \ + ANNOTATE_DATA_SPECIAL "\n\t" \ ".long 1b - ., " label " - . \n\t" \ JUMP_LABEL_TYPE key " - . \n\t" \ ".popsection \n\t"arch/loongarch/kernel/asm-offsets.c +20
-0@@ -18,6 +18,8 @@#include <asm/ftrace.h> #include <asm/sigframe.h> #include <vdso/datapage.h> +#include <asm/alternative.h> +#include <asm/extable.h> static void __used output_ptreg_defines (void ) {@@ -323,3 +325,21 @@ static void __used output_vdso_defines(void)DEFINE (__VDSO_PAGES,VDSO_NR_PAGES );BLANK (); } + +static void __used output_extable_defines (void ) +{ +COMMENT ("LoongArch exception table entry offsets." ); + +DEFINE (EXTABLE_SIZE ,sizeof (struct exception_table_entry )); +BLANK (); +} + +static void __used output_alt_instr_defines (void ) +{ +COMMENT ("LoongArch alternative instructions offsets." ); + +DEFINE (ALT_INSTR_SIZE ,sizeof (struct alt_instr )); +BLANK (); +} + +Signature · 1 line
2.25.1
Hello George,
I've run into another test case while exercising v3 on LoongArch (Clang build).I
a
Setup
=====
Maybe my config was too small, but I couldn't find any existing vmlinux .c file that emits .altinstructions on LoongArch, all in-tree ALTERNATIVE() sites are in arch/loongarch/lib/*.S, which we can't modify with klp-build.
I added a minimal dummy function to arch/loongarch/kernel/alternative.c so I could test:
$ cat base-vmlinux.patch diff --git a/arch/loongarch/kernel/alternative.c b/arch/loongarch/kernel/alternative.c index 0e0c766df1e3..01d81db79b3f 100644 --- a/arch/loongarch/kernel/alternative.c +++ b/arch/loongarch/kernel/alternative.c @@ -3,6 +3,7 @@ #include <linux/mm.h> #include <linux/module.h> #include <asm/alternative.h> +#include <asm/cpu.h> #include <asm/cacheflush.h> #include <asm/inst.h> #include <asm/sections.h> @@ -245,3 +246,15 @@ void __init alternative_instructions(void)
alternatives_patched = 1;
}
+
+/*
+ * klp-build repro hook: vmlinux .c function with ALTERNATIVE().
+ */I
+staatic int klp_build_alt_test_base = 10;
+
+static noinline int __used klp_build_alt_test_compute(int input) +{
+ asm volatile(ALTERNATIVE("nop", "nop", CPU_FEATURE_FPU) : : : "memory");
+
+ return input + klp_build_alt_test_base;
+}
Then a one-line klp-build patch to modify the function:
$ cat test-alternatives.patch
diff --git a/arch/loongarch/kernel/alternative.c b/arch/loongarch/kernel/alternative.c
index 01d81db79b3f..f5d5c4f0d8ef 100644
--- a/arch/loongarch/kernel/alternative.c
+++ b/arch/loongarch/kernel/alternative.c
@@ -256,5 +256,5 @@ static noinline int __used klp_build_alt_test_compute(int input)
{
asm volatile(ALTERNATIVE("nop", "nop", CPU_FEATURE_FPU) : : : "memory"); - return input + klp_build_alt_test_base;
+ return input + klp_build_alt_test_base + 1;
Repro
=====
Build the kernel as usual, then klp-build the test-alternatives.patch:
$ patch -p1 < base-vmlinux.patch
patching file arch/loongarch/kernel/alternative.c
$ ARCH=loongarch LLVM=1 make -j$(nproc)
...
$ ARCH=loongarch LLVM=1 ./sIcripts/livepatch/klp-build -T test-alternatives.patch
a
Result
======klp-build gets through the diff step, then fails linking the livepatch module:
...
Diffing objects
vmlinux.o: changed function: klp_build_alt_test_compute
Building patch module: livepatch-test-alternatives.ko
livepatch-test-alternatives.o: error: objtool: .text.klp_build_alt_test_compute+0x14: special: can't find new instruction make[3]: *** [/home/jolawren/src/linux/scripts/Makefile.build:507: livepatch-test-alternatives.o] Error 255 make[3]: *** Deleting file 'livepatch-test-alternatives.o'
make[2]: *** [/home/jolawren/src/linux/Makefile:2141: .] Error 2
make[1]: *** [/home/jolawren/src/linux/Makefile:248: __sub-make] Error 2
make: *** [Makefile:248: __sub-make] Error 2
error: klp-build: line 964: '"${cmd[@]}" > >(tee -a "$log") 2> >(tee -a "$log" 1>&2)'
Should v3 (or a follow-up patch) include the inline alternative cloning support? Or is patching functions that contain ALTERNATIVE() intentionally out of scope for LoongArch KLP for now?
If the latter, it would be worth calling out explicitly as this patch reads as though .altinstructions is supported end-to-end.
Regards,
--
Joe
Re: [PATCH v3 02/12] LoongArch: Mark special sections for KLP support
Hi Joe,
Thanks for the test case.
Quoted 3 lines
Should v3 (or a follow-up patch) include the inline alternative cloning support? Or is patching functions that contain
ALTERNATIVE()intentionally out of scope for LoongArch KLP for now?
It should be supported. It is a real gap, and there is a fix.
LoongArch emits the ALTERNATIVE replacement into ".subsection 1", the same as arm64. The replacement is appended to the function's own section, after the body. objtool already handles that layout for arm64, through two pieces that LoongArch was missing:
- arm64's ALTERNATIVE macros wrap the replacement with ANNOTATE_DATA_SPECIAL / ANNOTATE_DATA_SPECIAL_END, so create_fake_symbols() makes a fake symbol for the replacement block.
- arm64 defines ARCH_HAS_INLINE_ALTS, so clone_inline_alternatives() clones that block after the function.
LoongArch had neither. So clone_inline_alternatives() was a no-op, the replacement was never cloned, and objtool reported "can't find new instruction".
The fix adds both, matching arm64: annotate the replacement in the ALTERNATIVE and ALTERNATIVE_2 macros (asm and inline-asm), and define ARCH_HAS_INLINE_ALTS in objtool. It keeps the .subsection layout, so no objtool logic changes.
With it, your exact test case builds. I verified on LoongArch hardware with Clang 21. The replacement is cloned as its own fake symbol, and the .altinstructions entry points at it:
DEBUG: vmlinux.o: _text_klp_build_alt_test_compute_110 [+DATA]
DEBUG: vmlinux.o: .altinstructions+0x4: _text_klp_build_alt_test_compute_110+0x0 [NOTYPE LOCAL] vmlinux.o: changed function: klp_build_alt_test_compute
Building patch module: livepatch-test-alternatives.ko
SUCCESS
I will fold this into the next revision. In the series it splits in two:
the annotation goes with the special-section marking, which is this patch (LoongArch: Mark special sections for KLP support), and the ARCH_HAS_INLINE_ALTS define goes with the objtool arch hooks.
Diff below.
--- arch/loongarch/include/asm/alternative-asm.h | 5 ++++- arch/loongarch/include/asm/alternative.h | 5 ++++- tools/objtool/arch/loongarch/include/arch/elf.h | 1 + 3 files changed, 9 insertions(+), 2 deletions(-)
arch/loongarch/include/asm/alternative-asm.h +5 -0
@@ -6,6 +6,7 @@#include <asm/asm.h> #include <generated/asm-offsets.h> +#include <linux/annotate.h> /* * Issue one struct alt_instr descriptor entry (need to put it into
@@ -39,9 +40,11 @@ .popsection .subsection 1 + ANNOTATE_DATA_SPECIAL 143 : \newinstr 144 : +ANNOTATE_DATA_SPECIAL_END .previous .endm
@@ -70,11 +73,13 @@ .popsection .subsection 1 + ANNOTATE_DATA_SPECIAL 143 : \newinstr1 144 : \newinstr2 145 : +ANNOTATE_DATA_SPECIAL_END .previous .endm
arch/loongarch/include/asm/alternative.h +5 -0
@@ -7,6 +7,7 @@#include <linux/types.h> #include <linux/stddef.h> #include <linux/stringify.h> +#include <linux/objtool.h> #include <asm/asm.h> struct alt_instr {
@@ -75,7 +76,9 @@ extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end); ALTINSTR_ENTRY(feature, 1) \".popsection\n" \".subsection 1\n" \ +ANNOTATE_DATA_SPECIAL "\n" \ ALTINSTR_REPLACEMENT(newinstr, feature, 1) \ +ANNOTATE_DATA_SPECIAL_END "\n" \".previous\n" #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
@@ -86,8 +89,10 @@ extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end); ALTINSTR_ENTRY(feature2, 2) \".popsection\n" \".subsection 1\n" \ +ANNOTATE_DATA_SPECIAL "\n" \ ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \ ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \ +ANNOTATE_DATA_SPECIAL_END "\n" \".previous\n" /*
tools/objtool/arch/loongarch/include/arch/elf.h +1 -0
@@ -46,6 +46,7 @@#define R_TEXT32 R_LARCH_32_PCREL#define R_TEXT64 R_LARCH_32_PCREL +#define ARCH_HAS_INLINE_ALTS 1#define ARCH_HAS_PAIRED_RELOCS 1struct elf ;
Thanks,
George
PATCH v3 3/12 livepatch/klp-build: disable direct-extern-access for LoongArch to fix kernel panic
In reply to George GuoFrom: George Guo <[email protected]>
On LoongArch systems, livepatch modules containing references to EXTERNAL global variables trigger kernel panics when the core kernel is built with the -mdirect-extern-access optimization.
Root cause:
The -mdirect-extern-access optimization replaces GOT-based external symbol access with direct addressing for improved performance. However, this breaks the kernel module loading mechanism, which relies on GOT entries for proper relocation of EXTERNAL symbol references. Direct access to global variables from livepatch modules causes invalid memory accesses and kernel panics.
Solution:
For LoongArch klp builds, conditionally disable direct-extern-access by adding:
- -mno-direct-extern-access for GCC builds
- -fno-direct-access-external-data for Clang builds
See also commit 38b10b269d04 ("LoongArch: Tweak CFLAGS for Clang compatibility"), which added -mdirect-extern-access to the kernel as a nice-to-have optimization that reduces GOT accesses.
Note this is passed as a command-line KBUILD_CFLAGS_KERNEL= assignment, which replaces arch/loongarch/Makefile's KBUILD_CFLAGS_KERNEL wholesale rather than appending to it. As a side effect it also drops the -fPIE that the arch adds under CONFIG_RELOCATABLE. That is intentional and relied upon by the next patch ("LoongArch: ... -fPIC ..."), which adds -fPIC via KCFLAGS: kbuild applies KBUILD_CFLAGS_KERNEL after KCFLAGS for built-in objects (see modkern_cflags in scripts/Makefile.lib), so a retained -fPIE would otherwise win over that -fPIC. The two patches must stay together.
--- scripts/livepatch/klp-build | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
scripts/livepatch/klp-build +19 -0
@@ -556,6 +556,24 @@ build_kernel() { local log="$TMP_DIR/build.log" local cmd=() + local ARCH_KBUILD_CFLAGS_KERNEL="" + + if [[ -v CONFIG_LOONGARCH && "$CONFIG_LOONGARCH" == "y" ]]; then + # -mdirect-extern-access only exists under explicit relocs, and this + # function replaces KBUILD_CFLAGS_KERNEL wholesale (safe only then; + # the non-explicit build puts -Wa,-mla-global-with-pcrel there). + [[ "${CONFIG_AS_HAS_EXPLICIT_RELOCS:-}" == "y" ]] || \ + die "LoongArch klp-build requires CONFIG_AS_HAS_EXPLICIT_RELOCS=y" + + if [[ "${CONFIG_CC_IS_CLANG:-}" == "y" ]]; then + ARCH_KBUILD_CFLAGS_KERNEL="-fno-direct-access-external-data" + else + ARCH_KBUILD_CFLAGS_KERNEL="-mno-direct-extern-access" + fi + + status "LoongArch detected: adding $ARCH_KBUILD_CFLAGS_KERNEL to KBUILD_CFLAGS_KERNEL" + fi + cmd=("make") # When a patch to a kernel module references a newly created unexported
@@ -582,6 +600,7 @@ build_kernel() { fi cmd+=("-j$JOBS") cmd+=("KCFLAGS=-ffunction-sections -fdata-sections") + cmd+=("KBUILD_CFLAGS_KERNEL=$ARCH_KBUILD_CFLAGS_KERNEL") cmd+=("vmlinux") cmd+=("modules")
Signature · 1 line
2.25.1
PATCH v3 4/12 livepatch/klp-build: build LoongArch with -fPIC to keep GOT-indirect symbol references
In reply to George GuoFrom: George Guo <[email protected]>
On LoongArch, klp-build livepatch modules panic when a patched function references a global defined in the same compilation unit (e.g. SYSCALL_DEFINE1(newuname) -> 'uts_sem' in kernel/sys.c).
With CONFIG_RELOCATABLE=y the kernel is already -fPIE, so this is not absolute addressing; the problem is GOT indirection. For a same-unit global, -fPIE emits a direct PC-relative reference (R_LARCH_PCALA_*) and skips the GOT, while -fPIC routes it through the GOT (R_LARCH_GOT_PC_*). klp-build extracts the patched function into a separate module while 'uts_sem' stays in the core kernel, and the klp relocation machinery can only redirect such a cross-object reference through a GOT entry. The direct -fPIE reference has no GOT slot to fix up, so once the function is relocated its target is wrong and it faults.
Force -fPIC for LoongArch KLP builds; -fPIE is not enough, as it optimizes away the very GOT indirection KLP relies on.
This depends on the preceding patch: -fPIC is passed via KCFLAGS, but the arch adds -fPIE via KBUILD_CFLAGS_KERNEL, which kbuild applies after KCFLAGS (so -fPIE would win). That patch's command-line KBUILD_CFLAGS_KERNEL= assignment replaces the arch value and drops -fPIE, letting -fPIC take effect. The two patches must stay together.
--- scripts/livepatch/klp-build | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
scripts/livepatch/klp-build +17 -2
@@ -557,8 +557,15 @@ build_kernel() { local cmd=() local ARCH_KBUILD_CFLAGS_KERNEL="" + local ARCH_KCFLAGS="" if [[ -v CONFIG_LOONGARCH && "$CONFIG_LOONGARCH" == "y" ]]; then + # -fPIC replaces the kernel's -fPIE (added under CONFIG_RELOCATABLE); + # without that config there is no -fPIE to replace. + [[ "${CONFIG_RELOCATABLE:-}" == "y" ]] || \ + die "LoongArch klp-build requires CONFIG_RELOCATABLE=y" + ARCH_KCFLAGS="-fPIC" + # -mdirect-extern-access only exists under explicit relocs, and this # function replaces KBUILD_CFLAGS_KERNEL wholesale (safe only then; # the non-explicit build puts -Wa,-mla-global-with-pcrel there).
@@ -599,8 +606,16 @@ build_kernel() { cmd+=("-s") fi cmd+=("-j$JOBS") - cmd+=("KCFLAGS=-ffunction-sections -fdata-sections") - cmd+=("KBUILD_CFLAGS_KERNEL=$ARCH_KBUILD_CFLAGS_KERNEL") + cmd+=("KCFLAGS=-ffunction-sections -fdata-sections${ARCH_KCFLAGS:+ $ARCH_KCFLAGS}") + # -fPIC is added for KLP via KCFLAGS above; the arch adds -fPIE via + # KBUILD_CFLAGS_KERNEL, which kbuild places after KCFLAGS on the + # built-in compile line. -fPIC/-fPIE is last-one-wins, so -fPIE would + # win. Setting KBUILD_CFLAGS_KERNEL on the command line replaces the + # arch value (not append), which drops -fPIE and lets -fPIC win. Only + # do this when an arch needs it (LoongArch). + if [[ -n "$ARCH_KBUILD_CFLAGS_KERNEL" ]]; then + cmd+=("KBUILD_CFLAGS_KERNEL=$ARCH_KBUILD_CFLAGS_KERNEL") + fi cmd+=("vmlinux") cmd+=("modules")
Signature · 1 line
2.25.1
Joe Lawrence
maintainerRe: [PATCH v3 04/12] livepatch/klp-build: build LoongArch with -fPIC to keep GOT-indirect symbol references
Quoted 16 lines · George Guo
From: George Guo <[email protected]>
On LoongArch, klp-build livepatch modules panic when a patched function references a global defined in the same compilation unit (e.g.
SYSCALL_DEFINE1(newuname)-> 'uts_sem' inkernel/sys.c).With
CONFIG_RELOCATABLE=ythe kernel is already -fPIE, so this is not absolute addressing; the problem is GOT indirection. For a same-unit global, -fPIE emits a direct PC-relative reference (R_LARCH_PCALA_*) and skips the GOT, while -fPIC routes it through the GOT (R_LARCH_GOT_PC_*). klp-build extracts the patched function into a separate module while 'uts_sem' stays in the core kernel, and the klp relocation machinery can only redirect such a cross-object reference through a GOT entry. The direct -fPIE reference has no GOT slot to fix up, so once the function is relocated its target is wrong and it faults.
Quoted 9 lines
Force -fPIC for LoongArch KLP builds; -fPIE is not enough, as it optimizes away the very GOT indirection KLP relies on.
This depends on the preceding patch: -fPIC is passed via KCFLAGS, but the arch adds -fPIE via
KBUILD_CFLAGS_KERNEL, which kbuild applies after KCFLAGS (so -fPIE would win). That patch's command-line KBUILD_CFLAGS_KERNEL= assignment replaces the arch value and drops -fPIE, letting -fPIC take effect. The two patches must stay together.
This might be my personal preference, but I think it would be quicker to review patches: ("livepatch/klp-build: disable direct-extern-access for LoongArch to fix kernel panic") ("livepatch/klp-build: build LoongArch with -fPIC to keep GOT-indirect symbol references") if they were squashed into one patch.
With that, the commit messages can drop the forward/backward references, while the combined commit remains complete thought, i.e. for LoongArch KLP builds, replace the arch's KBUILD_CFLAGS_KERNEL (dropping -fPIE and -mdirect-extern-access) and add -fPIC via KCFLAGS instead.
Quoted 36 lines
Co-developed-by Kexin Liu <[email protected]>Signed-off-by Kexin Liu <[email protected]>Signed-off-by George Guo <[email protected]>--- scripts/livepatch/klp-build | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)scripts/livepatch/klp-build +10
-2@@ -557,8 +557,15 @@ build_kernel() { local cmd=() local ARCH_KBUILD_CFLAGS_KERNEL="" + local ARCH_KCFLAGS="" if [[ -v CONFIG_LOONGARCH && "$CONFIG_LOONGARCH" == "y" ]]; then + # -fPIC replaces the kernel's -fPIE (added under CONFIG_RELOCATABLE); + # without that config there is no -fPIE to replace. + [[ "${CONFIG_RELOCATABLE:-}" == "y" ]] || \ + die "LoongArch klp-build requires CONFIG_RELOCATABLE=y" + ARCH_KCFLAGS="-fPIC" + # -mdirect-extern-access only exists under explicit relocs, and this # function replaces KBUILD_CFLAGS_KERNEL wholesale (safe only then; # the non-explicit build puts -Wa,-mla-global-with-pcrel there).@@ -599,8 +606,16 @@ build_kernel() { cmd+=("-s") fi cmd+=("-j$JOBS") - cmd+=("KCFLAGS=-ffunction-sections -fdata-sections") - cmd+=("KBUILD_CFLAGS_KERNEL=$ARCH_KBUILD_CFLAGS_KERNEL") + cmd+=("KCFLAGS=-ffunction-sections -fdata-sections${ARCH_KCFLAGS:+ $ARCH_KCFLAGS}") + # -fPIC is added for KLP via KCFLAGS above; the arch adds -fPIE via + # KBUILD_CFLAGS_KERNEL, which kbuild places after KCFLAGS on the
scripts/livepatch/klp-build build_kernel() { · line 621
Quoted 10 lines
scripts/livepatch/klp-build +7
-0+ # built-in compile line. -fPIC/-fPIE is last-one-wins, so -fPIE would + # win. Setting KBUILD_CFLAGS_KERNEL on the command line replaces the + # arch value (not append), which drops -fPIE and lets -fPIC win. Only + # do this when an arch needs it (LoongArch). + if [[ -n "$ARCH_KBUILD_CFLAGS_KERNEL" ]]; then + cmd+=("KBUILD_CFLAGS_KERNEL=$ARCH_KBUILD_CFLAGS_KERNEL") + fi cmd+=("vmlinux") cmd+=("modules")
I'd also pull out the configuration checks into validate_config() to be consistent:
if [[ -v CONFIG_LOONGARCH ]]; then
[[ -v CONFIG_AS_HAS_EXPLICIT_RELOCS ]] || \
die "LoongArch klp-build requires CONFIG_AS_HAS_EXPLICIT_RELOCS=y"
[[ -v CONFIG_RELOCATABLE ]] || \
die "LoongArch klp-build requires CONFIG_RELOCATABLE=y"
fiand then build_kernel() only needs to adjust the cmd / flags accordingly.
--
Joe
Re: [PATCH v3 04/12] livepatch/klp-build: build LoongArch with -fPIC to keep GOT-indirect symbol references
Hi Joe,
Quoted 2 lines
This might be my personal preference, but I think it would be quicker to review patches: [...] if they were squashed into one patch.
[...]
Quoted 3 lines
the combined commit remains complete thought, i.e. for LoongArch KLP builds, replace the arch's
KBUILD_CFLAGS_KERNEL(dropping -fPIE and -mdirect-extern-access) and add -fPIC via KCFLAGS instead.
Makes sense. I'll squash patches 03 and 04 into a single patch and drop the forward/backward references from the commit message.
Quoted 2 lines
I'd also pull out the configuration checks into
validate_config()to be consistent:
[...]
Quoted 1 line
and then
build_kernel()only needs to adjust the cmd / flags accordingly.
Agreed. I'll move the CONFIG_RELOCATABLE / CONFIG_AS_HAS_EXPLICIT_RELOCS checks into validate_config() and leave build_kernel() to just set the flags.
Thanks,
George
PATCH v3 5/12 LoongArch: Fix EFI linking with -fdata-sections
In reply to George GuoFrom: George Guo <[email protected]>
When building with func-fdata-sections, the .init.bss section gets split up into a bunch of .init.bss.<var> sections. Make sure they get linked into .init.data.
--- arch/loongarch/kernel/vmlinux.lds.S | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/loongarch/kernel/vmlinux.lds.S +1 -1
@@ -96,7 +96,7 @@ SECTIONS #endif .init.bss : { - *(.init.bss) + *(.init.bss .init.bss.*) } . = ALIGN(PECOFF_SEGMENT_ALIGN); __initdata_end = .;
Signature · 1 line
2.25.1
Joe Lawrence
maintainerRe: [PATCH v3 05/12] LoongArch: Fix EFI linking with -fdata-sections
Quoted 3 lines · George Guo
From: George Guo <[email protected]>
When building with func-fdata-sections, the .init.bss section gets split
nit: s/func-fdata-sections/-fdata-sections
Quoted 15 lines
up into a bunch of .init.bss.<var> sections. Make sure they get linked into .init.data.
Co-developed-by Kexin Liu <[email protected]>Signed-off-by Kexin Liu <[email protected]>Signed-off-by George Guo <[email protected]>--- arch/loongarch/kernel/vmlinux.lds.S | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/loongarch/kernel/vmlinux.lds.S +0
-0
arch/loongarch/kernel/vmlinux.lds.S SECTIONS · line 99
Quoted 5 lines
arch/loongarch/kernel/vmlinux.lds.S +1
-1@@ -96,7 +96,7 @@ SECTIONS #endif .init.bss : { - *(.init.bss) + *(.init.bss .init.bss.*)
Both arch/arm64/kernel/vmlinux.lds.S and arch/loongarch/kernel/vmlinux.lds.S contain a "/* from the EFI stub */" comment documenting the wildcard. Might be worth adding for v4.
--
Joe
Joe Lawrence
maintainerRe: [PATCH v3 05/12] LoongArch: Fix EFI linking with -fdata-sections
Quoted 24 lines · Joe Lawrence
Quoted 3 lines · George Guo
From: George Guo <[email protected]>
When building with func-fdata-sections, the .init.bss section gets split
nit: s/func-fdata-sections/-fdata-sections
Quoted 17 lines
up into a bunch of .init.bss.<var> sections. Make sure they get linked into .init.data.
Co-developed-by Kexin Liu <[email protected]>Signed-off-by Kexin Liu <[email protected]>Signed-off-by George Guo <[email protected]>--- arch/loongarch/kernel/vmlinux.lds.S | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/loongarch/kernel/vmlinux.lds.S +0
-0@@ -96,7 +96,7 @@ SECTIONS #endif
Quoted 6 lines
arch/loongarch/kernel/vmlinux.lds.S SECTIONS · line 99
Quoted 3 lines
arch/loongarch/kernel/vmlinux.lds.S +1
-1.init.bss : { - *(.init.bss) + *(.init.bss .init.bss.*)Both
arch/arm64/kernel/vmlinux.lds.Sandarch/loongarch/kernel/vmlinux.lds.Scontain a "/* from the EFI stub */"
^^ typo: I meant the riscv version as well
Also the commit message says, "Make sure they get linked into .init.data," but should that read, "... into .init.bss"?
--
Joe
Re: [PATCH v3 05/12] LoongArch: Fix EFI linking with -fdata-sections
Hi Joe,
Quoted 1 line
nit: s/func-fdata-sections/-fdata-sections
[...]
Quoted 2 lines
Also the commit message says, "Make sure they get linked into .init.data," but should that read, "... into .init.bss"?
Yes to both. I'll fix the "func-fdata-sections" typo and change ".init.data" to ".init.bss" in the commit message -- the .init.bss.* globs land in .init.bss, not .init.data.
Quoted 3 lines
Both
arch/arm64/kernel/vmlinux.lds.Sandarch/loongarch/kernel/vmlinux.lds.Scontain a "/* from the EFI stub */" comment documenting the wildcard.
[...]
Quoted 1 line
^^ typo: I meant the riscv version as well
Will do -- I'll add the "/* from the EFI stub */" comment next to the wildcard, matching arm64 and riscv.
Thanks,
George
PATCH v3 6/12 objtool/klp: Add LoongArch jump opcode bytes support
In reply to George GuoFrom: George Guo <[email protected]>
Implement arch_jump_opcode_bytes() for LoongArch so that klp checksums for jump/call instructions with non-relocated destination offsets are position-independent, mirroring the x86 and arm64 implementations.
Mask out the embedded branch offset while preserving the opcode and the register operands, which differ per instruction format:
- B/BL (reg0i26): no regs -> 0xfc000000
- BEQZ/BNEZ/BCEQZ/BCNEZ (reg1i21): keep rj/cj -> 0xfc0003e0
- JIRL/BEQ/BNE/.../BGEU (reg2i16): keep rj/rd -> 0xfc0003ff
--- tools/objtool/arch/loongarch/decode.c | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)
tools/objtool/arch/loongarch/decode.c +41 -0
@@ -432,6 +432,47 @@ unsigned long arch_jump_table_sym_offset(struct reloc *reloc, struct reloc *tabl } } +size_t arch_jump_opcode_bytes (struct objtool_file *file,struct instruction *insn, +unsigned char *buf) +{ +union loongarch_instruction *code; +u32 insn_word; + + insn_word =le32toh (*(u32 *)(insn->sec->data->d_buf + insn->offset)); + code = (union loongarch_instruction *)&insn_word; + +switch (code->reg0i26_format.opcode) { +case b_op: +case bl_op: +/* reg0i26: 26-bit offset, no register operands */ + insn_word &=0xfc000000 ; +break ; +case beqz_op: +case bnez_op: +case bceqz_op:/* == bcnez_op */ +/* reg1i21: keep opcode + rj/cj at bits[9:5] */ + insn_word &=0xfc0003e0 ; +break ; +case jirl_op: +case beq_op: +case bne_op: +case blt_op: +case bge_op: +case bltu_op: +case bgeu_op: +/* reg2i16: keep opcode + rj/rd at bits[9:0] */ + insn_word &=0xfc0003ff ; +break ; +default : +break ; + } + + insn_word =htole32 (insn_word); +memcpy (buf, &insn_word,sizeof (insn_word)); + +return LOONGARCH_INSN_SIZE ; +} +#ifdef DISAS int arch_disas_info_init (struct disassemble_info *dinfo)
Signature · 1 line
2.25.1
PATCH v3 7/12 klp-build: Add LoongArch syscall patching macro
In reply to George GuoFrom: George Guo <[email protected]>
Add LoongArch support for KLP_SYSCALL_DEFINEx(), mirroring the generic __SYSCALL_DEFINEx() pattern from include/linux/syscalls.h.
--- include/linux/livepatch_helpers.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
include/linux/livepatch_helpers.h +22 -0
@@ -91,6 +91,28 @@ } \ static inline long __klp_do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)) +#elif defined(CONFIG_LOONGARCH ) + +#define __KLP_SYSCALL_DEFINEx (x, name, ...) \ + __diag_push(); \ + __diag_ignore(GCC, 8, "-Wattribute-alias", \ + "Type aliasing is used to sanitize syscall arguments");\ + asmlinkage long sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)) \ + __attribute__((alias(__stringify(__se_sys##name)))); \ + ALLOW_ERROR_INJECTION(sys##name, ERRNO); \ + static inline long __klp_do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\ + asmlinkage long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \ + asmlinkage long __attribute__((optimize("-fno-optimize-sibling-calls")))\ + __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)) \ + { \ + long ret = __klp_do_sys##name(__MAP(x,__SC_CAST,__VA_ARGS__));\ + __MAP(x,__SC_TEST,__VA_ARGS__); \ + __PROTECT(x, ret,__MAP(x,__SC_ARGS,__VA_ARGS__)); \ + return ret; \ + } \ + __diag_pop(); \ + static inline long __klp_do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)) +#endif #endif /* _LINUX_LIVEPATCH_HELPERS_H */
Signature · 1 line
2.25.1
Joe Lawrence
maintainerRe: [PATCH v3 07/12] klp-build: Add LoongArch syscall patching macro
Quoted 33 lines · George Guo
From: George Guo <[email protected]>
Add LoongArch support for
KLP_SYSCALL_DEFINEx(), mirroring the generic__SYSCALL_DEFINEx()pattern frominclude/linux/syscalls.h.Co-developed-by Kexin Liu <[email protected]>Signed-off-by Kexin Liu <[email protected]>Signed-off-by George Guo <[email protected]>--- include/linux/livepatch_helpers.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)include/linux/livepatch_helpers.h +13
-0@@ -91,6 +91,28 @@ } \ static inline long __klp_do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)) +#elif defined(CONFIG_LOONGARCH ) + +#define __KLP_SYSCALL_DEFINEx (x, name, ...) \ + __diag_push(); \ + __diag_ignore(GCC, 8, "-Wattribute-alias", \ + "Type aliasing is used to sanitize syscall arguments");\ + asmlinkage long sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)) \ + __attribute__((alias(__stringify(__se_sys##name)))); \ + ALLOW_ERROR_INJECTION(sys##name, ERRNO); \ + static inline long __klp_do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\ + asmlinkage long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \ + asmlinkage long __attribute__((optimize("-fno-optimize-sibling-calls")))\ + __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)) \
include/linux/livepatch_helpers.h line 116
Quoted 10 lines
include/linux/livepatch_helpers.h +9
-0+ { \ + long ret = __klp_do_sys##name(__MAP(x,__SC_CAST,__VA_ARGS__));\ + __MAP(x,__SC_TEST,__VA_ARGS__); \ + __PROTECT(x, ret,__MAP(x,__SC_ARGS,__VA_ARGS__)); \ + return ret; \ + } \ + __diag_pop(); \ + static inline long __klp_do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)) + #endif
Nit: The other macros in livepatch_helpers use tabs to pad out to the line continuation '\'.
--
Joe
Re: [PATCH v3 07/12] klp-build: Add LoongArch syscall patching macro
Hi Joe,
Quoted 2 lines
Nit: The other macros in
livepatch_helpersuse tabs to pad out to the line continuation '\'.
Right, I'll pad the LoongArch macro with tabs to match the others.
Thanks,
George
PATCH v3 8/12 LoongArch: Add livepatch build (KLP) support
In reply to George GuoFrom: George Guo <[email protected]>
This allows automated livepatch module generation using objtool.
--- arch/loongarch/Kconfig | 1 + tools/objtool/Makefile | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)
arch/loongarch/Kconfig +1 -0
@@ -159,6 +159,7 @@ config LOONGARCH select HAVE_IOREMAP_PROT select HAVE_IRQ_EXIT_ON_IRQ_STACK select HAVE_IRQ_TIME_ACCOUNTING + select HAVE_KLP_BUILD select HAVE_KPROBES select HAVE_KPROBES_ON_FTRACE select HAVE_KRETPROBES
tools/objtool/Makefile +2 -1
@@ -8,7 +8,8 @@ ifeq ($(SRCARCH),x86) endif ifeq ($(SRCARCH),loongarch) - BUILD_ORC := y + BUILD_ORC := y + ARCH_HAS_KLP := y endif ifeq ($(SRCARCH),arm64)
Signature · 1 line
2.25.1
PATCH v3 9/12 LoongArch: Select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY
In reply to George GuoFrom: George Guo <[email protected]>
arch/loongarch/Makefile uses -fpatchable-function-entry=2 when CONFIG_DYNAMIC_FTRACE is set, but nothing selects FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY, so kbuild falls back to FTRACE_MCOUNT_USE_RECORDMCOUNT and runs recordmcount on every object. The pass is useless here: there are no _mcount calls to find, and the real ftrace sites come from the __patchable_function_entries section anyway.
It is not just wasted time. recordmcount needs a non-weak symbol in each text section, to use as the base of the __mcount_loc relocations it writes (see find_secsym_ndx()). GAS emits a section symbol for every section, so GCC objects always have one. Clang's integrated assembler drops unreferenced section symbols, so a clang object must use the function's own symbol -- but find_secsym_ndx() skips weak symbols. So with -ffunction-sections (which klp-build uses), a __weak function like sched_clock is alone in its section with no usable symbol, and recordmcount fails:
Cannot find symbol for section 5: .text.sched_clock. kernel/sched/build_utility.o: failed
Select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY like arm64 and riscv do, which disables the recordmcount pass altogether.
--- arch/loongarch/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/loongarch/Kconfig +1 -0
@@ -88,6 +88,7 @@ config LOONGARCH select CPU_PM select EDAC_SUPPORT select EFI + select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY if DYNAMIC_FTRACE select GENERIC_ATOMIC64 if 32BIT select GENERIC_CLOCKEVENTS select GENERIC_CMOS_UPDATE
Signature · 1 line
2.25.1
PATCH v3 10/12 objtool/klp: Convert local label references in special sections
In reply to George GuoFrom: George Guo <[email protected]>
GCC/GAS on LoongArch references special section entries (__ex_table, __bug_table, __jump_table, .altinstructions) through local text labels (.L*) instead of a section symbol plus offset. The assembler keeps the label because of linker relaxation: a "section + constant offset" reference would go stale when the linker deletes or shrinks instructions, while a symbol reference can be recomputed after relaxation.
convert_reloc_secsym_to_sym() only handles the section symbol form and returns early for such label references. The label is never cloned into the livepatch object, so should_keep_special_sym() cannot correlate the entry with an included function and silently drops it: a GCC-built livepatch module ends up missing the patched function's __ex_table / __bug_table / __jump_table entries, with no error at build or load time.
Redirect label references in text sections to the containing function symbol, mirroring the section symbol case. A label with no containing function symbol (e.g. hand-written asm in a plain .text section) can't be correlated; skip it rather than failing, since such entries belong to unchanged code and are dropped anyway.
--- tools/objtool/klp-diff.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
tools/objtool/klp-diff.c +34 -1
@@ -1423,8 +1423,41 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)if (!strcmp (reloc->sec->name,".rela__patchable_function_entries" ))return convert_pfe_reloc (elf, reloc); -if (!is_sec_sym (sym)) +if (!is_sec_sym (sym)) { +/* +* Most toolchains reference special-section entries via the +* section symbol plus an offset. GCC/GAS on LoongArch instead +* references a local text label (.L*): LoongArch linker +* relaxation is the reason GAS keeps the label rather than +* reducing it to a section symbol reference. Such a label is +* never cloned into the livepatch object, so the entry would be +* silently dropped. Redirect the relocation to the containing +* function, mirroring the section-symbol case below. +*/ +if (is_local_label (sym) &&is_text_sec (sec)) { +unsigned long offset = sym->offset +reloc_addend (reloc); + + sym =find_symbol_containing_inclusive (sec, offset); +if (!sym) { +/* +* A local label with no containing function +* symbol (e.g. hand-written asm in a plain .text +* section). It can't be correlated to a function, +* so skip it rather than failing the build; such +* entries belong to unchanged code and are dropped +* anyway. +*/ +return 1 ; + } + + reloc->sym = sym; +set_reloc_sym (elf, reloc, sym->idx); +set_reloc_addend (elf, reloc, offset - sym->offset); +return 0 ; + } +return 0 ; + } sym =find_symbol_containing_inclusive (sec,arch_adjusted_addend (reloc));if (!sym) {
Signature · 1 line
2.25.1
PATCH v3 11/12 objtool/klp: Fix ANNOTATE_DATA_SPECIAL parsing for local label references
In reply to George GuoFrom: George Guo <[email protected]>
create_fake_symbols() takes each annotated entry's offset from the annotate relocation's addend alone. That works only when the relocation points at the special section's section symbol. GCC/GAS on LoongArch points it at the entry's local label instead, with a zero addend: every entry then gets offset 0, the "distance to the next annotation" size becomes 0, and the fallback sets the size to the rest of the section.
So every fake symbol starts at offset 0 and covers the whole special section. If any entry in the section belongs to a patched function, should_keep_special_sym() keeps such a symbol, and the output module gets the entire __bug_table (thousands of entries and klp relocations) instead of just the patched function's entries.
Compute the offset as sym->offset + addend, which handles both the section symbol form (sym->offset == 0) and the local label form (addend == 0).
--- tools/objtool/klp-diff.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
tools/objtool/klp-diff.c +7 -2
@@ -1863,7 +1863,12 @@ static int create_fake_symbols(struct elf *elf)if (annotype (elf, sec, reloc) !=ANNOTYPE_DATA_SPECIAL )continue ; - offset =reloc_addend (reloc); +/* +* The annotated location may be referenced via the section +* symbol plus addend, or via a local label (GCC/GAS on +* LoongArch): sym->offset + addend covers both forms. +*/ + offset = reloc->sym->offset +reloc_addend (reloc); size =0 ; next_reloc = reloc;
@@ -1875,7 +1880,7 @@ static int create_fake_symbols(struct elf *elf) next_reloc->sym->sec != reloc->sym->sec)continue ; - size =reloc_addend (next_reloc) - offset; + size = (next_reloc->sym->offset +reloc_addend (next_reloc)) - offset;break ; }
Signature · 1 line
2.25.1
PATCH v3 12/12 objtool/klp: Fold LoongArch paired ADD/SUB relocations into PCREL
In reply to George GuoFrom: George Guo <[email protected]>
On LoongArch, the "key - ." field of a __jump_table entry can come out as two relocations at the same offset, R_LARCH_ADD64 plus R_LARCH_SUB64. clang's integrated assembler emits this pair when the key symbol is not defined in the same translation unit (for example __tracepoint_netif_rx); GAS, and clang for locally-defined keys, produce a single R_LARCH_64_PCREL instead, which is why GCC builds do not hit this. objtool's elf_create_reloc() allows only one relocation per offset, so cloning such an entry fails with:
vmlinux.o: error: objtool: __jump_table_23683+0x8: duplicate reloc
When the SUB half points at the relocation's own position (the "sym - ." pattern), the pair means the same thing as one PC-relative relocation. Add arch_normalize_paired_reloc() which rewrites the ADD half to R_LARCH_32_PCREL/R_LARCH_64_PCREL and skips the SUB half. The LoongArch module loader supports both forms.
--- tools/objtool/arch/loongarch/decode.c | 62 +++++++++++++++++++ .../objtool/arch/loongarch/include/arch/elf.h | 18 ++++++ tools/objtool/klp-diff.c | 13 ++++ 3 files changed, 93 insertions(+)
tools/objtool/arch/loongarch/decode.c +62 -0
@@ -39,6 +39,68 @@ u64 arch_adjusted_addend(struct reloc *reloc)return reloc_addend (reloc); } +/* +* A cross-section difference like the "key - ." field of a __jump_table +* entry may come out as a paired R_LARCH_ADD plus R_LARCH_SUB relocation +* at the same offset: clang's integrated assembler does this when the +* symbol is not defined in the same translation unit (GAS, and clang for +* locally-defined symbols, emit a single PCREL instead). objtool allows +* only one relocation per offset, so the pair breaks cloning. +* +* When the SUB half points at the reloc's own position ("sym - ."), the +* pair means the same as a single PC-relative relocation, which the +* module loader also supports: rewrite the ADD half to R_LARCH_*_PCREL +* and tell the caller to skip the SUB half. +* +* Return 1 to skip the reloc, 0 to proceed, -1 on error. +*/ +int arch_normalize_paired_reloc (struct elf *elf,struct reloc *reloc) +{ +struct section *rsec = reloc->sec; +unsigned int sub_type, pcrel_type; +struct reloc *sub, *add; + +switch (reloc_type (reloc)) { +case R_LARCH_ADD32 : + sub_type =R_LARCH_SUB32 ; + pcrel_type =R_LARCH_32_PCREL ; +break ; +case R_LARCH_ADD64 : + sub_type =R_LARCH_SUB64 ; + pcrel_type =R_LARCH_64_PCREL ; +break ; +case R_LARCH_SUB32 : +case R_LARCH_SUB64 : +/* +* Skip only if the paired ADD (the preceding reloc) has +* been rewritten to PCREL; otherwise leave the pair +* intact so a failed conversion stays loud. +*/ + add =reloc_idx (reloc) ? reloc -1 :NULL ; +if (add &&reloc_offset (add) ==reloc_offset (reloc) && + (reloc_type (add) ==R_LARCH_32_PCREL || +reloc_type (add) ==R_LARCH_64_PCREL )) +return 1 ; +return 0 ; +default : +return 0 ; + } + +/* The paired SUB reloc immediately follows the ADD */ + sub =rsec_next_reloc (rsec, reloc); +if (!sub ||reloc_offset (sub) !=reloc_offset (reloc) || +reloc_type (sub) != sub_type) +return 0 ; + +/* Only a "sym - ." difference is PC-relative */ +if (sub->sym->sec != rsec->base || + sub->sym->offset +reloc_addend (sub) !=reloc_offset (sub)) +return 0 ; + +set_reloc_type (elf, reloc, pcrel_type); +return 0 ; +} +bool arch_pc_relative_reloc (struct reloc *reloc) {return false;
tools/objtool/arch/loongarch/include/arch/elf.h +18 -0
@@ -15,6 +15,18 @@#ifndef R_LARCH_64 #define R_LARCH_64 2#endif +#ifndef R_LARCH_ADD32 +#define R_LARCH_ADD32 50 +#endif +#ifndef R_LARCH_ADD64 +#define R_LARCH_ADD64 51 +#endif +#ifndef R_LARCH_SUB32 +#define R_LARCH_SUB32 55 +#endif +#ifndef R_LARCH_SUB64 +#define R_LARCH_SUB64 56 +#endif #ifndef R_LARCH_32_PCREL #define R_LARCH_32_PCREL 99#endif
@@ -34,4 +46,10 @@#define R_TEXT32 R_LARCH_32_PCREL#define R_TEXT64 R_LARCH_32_PCREL +#define ARCH_HAS_PAIRED_RELOCS 1 + +struct elf ; +struct reloc ; +int arch_normalize_paired_reloc (struct elf *elf,struct reloc *reloc); +#endif /* _OBJTOOL_ARCH_ELF_H */
tools/objtool/klp-diff.c +13 -0
@@ -1504,6 +1504,13 @@ static bool is_uncorrelated_section(struct section *sec)strstarts (sec->name,".data..Lanon." );/* Clang */ } +#ifndef ARCH_HAS_PAIRED_RELOCS +static inline int arch_normalize_paired_reloc (struct elf *elf,struct reloc *reloc) +{ +return 0 ; +} +#endif +/* * Convert a relocation symbol reference to the needed format: either a section * symbol or the underlying symbol itself. Return -1 error, 0 success, 1 skip.
@@ -1511,10 +1518,16 @@ static bool is_uncorrelated_section(struct section *sec)static int convert_reloc_sym (struct elf *elf,struct reloc *reloc) {struct section *sec = reloc->sym->sec; +int ret;if (reloc_type (reloc) ==R_NONE )return 1 ; +/* Fold paired ADD/SUB relocs (LoongArch) into a single PCREL */ + ret =arch_normalize_paired_reloc (elf, reloc); +if (ret) +return ret; +if (is_uncorrelated_section (sec))return convert_reloc_sym_to_secsym (elf, reloc);
Signature · 1 line
2.25.1
Re: [PATCH v3 12/12] objtool/klp: Fold LoongArch paired ADD/SUB relocations into PCREL
Quoted 9 lines · George Guo
From: George Guo <[email protected]>
On LoongArch, the "key - ." field of a
__jump_tableentry can come out as two relocations at the same offset,R_LARCH_ADD64plus R_LARCH_SUB64. clang's integrated assembler emits this pair when the key symbol is not defined in the same translation unit (for example__tracepoint_netif_rx); GAS, and clang for locally-defined keys, produce a singleR_LARCH_64_PCRELinstead, which is why GCC builds do not hit this.
Hmm, is this a flaw in LLVM? We have
config AS_HAS_THIN_ADD_SUB
def_bool $(cc-option,-Wa$(comma)-mthin-add-sub) || AS_IS_LLVM
so we are already expecting LLVM to emit R_LARCH_64_PCREL always instead of ADD64/SUB64.
Signature · 1 line
Xi Ruoyao <[email protected]>
Re: [PATCH v3 12/12] objtool/klp: Fold LoongArch paired ADD/SUB relocations into PCREL
Hi Xi Ruoyao,
Quoted 7 lines
Hmm, is this a flaw in LLVM? We have
config
AS_HAS_THIN_ADD_SUBdef_bool $(cc-option,-Wa$(comma)-mthin-add-sub) || AS_IS_LLVMso we are already expecting LLVM to emit
R_LARCH_64_PCRELalways instead of ADD64/SUB64.
Yes, it looks like a gap in clang. I measured the full matrix for ".quad sym - ." on the LoongArch box (GAS 2.43.50, GCC 14.2.1, clang 21.1.8):
default -Wa,-mthin-add-sub as / gcc, local sym PAIR THIN as / gcc, extern sym PAIR THIN clang, local sym THIN rejected (no such flag) clang, extern sym PAIR rejected (no such flag)
THIN = single R_LARCH_64_PCREL
PAIR = R_LARCH_ADD64 + R_LARCH_SUB64 at one offset
Two facts fall out.
GAS defaults to the pair and needs -mthin-add-sub to thin. The kernel passes that flag (arch/loongarch/Makefile), so on GAS both cases are thin.
clang has no -mthin-add-sub. It rejects both -mthin-add-sub and -Wa,-mthin-add-sub as unknown arguments, so cc-option drops the flag and clang uses its default. Its default already thins a locally-defined "sym - .". The logic is there. But it does not cover an extern "sym - .", which stays a pair. There is no lever to force it.
So the AS_IS_LLVM branch in AS_HAS_THIN_ADD_SUB (commit a7e083772456) reflects clang's default, and that default thins local but not extern. This is the case your commit 120dd4118e58 flagged ("Clang has some other issues"), still present in clang 21.
In the kernel it shows up in __jump_table. A static key defined in the same file (e.g. netstamp_needed_key) gets the thin form. A tracepoint's key (__tracepoint_netif_rx, defined in another TU) gets the pair.
It only breaks the klp-diff path, not a normal build. The pair is rejected by elf_create_reloc(), which allows one relocation per offset. klp-diff hits that when it clones the __jump_table entry and re-creates its relocations. A normal objtool run only reads relocations, so it never creates two at one offset. That is why "make LLVM=1" builds fine but klp-build fails with "duplicate reloc".
This patch folds the pair back into a single R_LARCH_64_PCREL, but only in the klp-diff clone path (convert_reloc_sym), and only when the SUB half targets the relocation's own position, i.e. a real "sym - .".
I am not sure this belongs in objtool, though. Would you prefer:
a) keep this, so klp-diff tolerates the pair on current clang; or
b) treat it as an LLVM issue to fix (clang should thin the extern case too, matching what it already does for a local symbol), and leave objtool as is?
I have not found an existing LLVM report for the extern case. I can file one.
Thanks,
George
Huacai Chen
maintainerHi,George,
Quoted 10 lines · George Guo
This series adds LoongArch support for the klp-build livepatch tooling, enabling automated livepatch module generation using objtool on LoongArch.
It is based on Josh Poimboeuf's klp-build series [1] (see base-commit below, unchanged from v1/v2) and extends it with the LoongArch-specific objtool and build pieces, plus toolchain/relocation fixes required to make livepatch modules load, generate, and run correctly under both GCC and Clang.
1. Wang Yuli released his version first; re-releasing it without any prior communication is simply rude.
https://lore.kernel.org/loongarch/[email protected]/
2. Perhaps you were genuinely unaware about his earlier patches, but Wang Yuli had already alerted you in your v1. At that point, you had two options: a) abandon your own patches and let Wang Yuli continue his development, or b) thank Wang Yuli for the reminder and assistance, then seek to collaborate on development. However, you did neither; instead, you released new versions on your own again and again. This is highly unethical.
3. You might argue that Wang Yuli’s version is incomplete, but is yours complete? Since you’re releasing v3, it’s clear that your v1 and v2 were also incomplete. If you can simply take over someone else’s project just because their version is incomplete, does that mean Wang Yuli can now throw yours in the trash when he releases his v2?
Huacai
Quoted 164 lines
Overview:
- Patches 1-8 are unchanged from v2 [3]: the LoongArch objtool hooks, special-section marking, the -fPIC/-mdirect-extern-access fixes, EFI linking,
KLP_SYSCALL_DEFINEx()support, and finally wiring up LoongArch klp-build (kept last so the series stays bisectable).
- Patches 9-12 are new in v3, fixing issues Joe Lawrence found across two rounds of review by actually running the full klp-build flow (see Changes since v2 below for details).All new patches (9-12) fix bugs in
objtool/klp-diff.cand the LoongArchKconfig; none of them touch other architectures.Testing:
GCC on LoongArch hardware: livepatch modules generated, loaded, and exercised (unchanged from v2).
Clang 21.1.8 on LoongArch hardware, reproduced end to end with Joe's exact test case (test-joe.patch below), since it happens to exercise patches 10-12 together:
netif_rx_internal()has__ex_tableentries, a__bug_tableentry, and three__jump_tablestatic keys, one of which (a tracepoint's key) is unexported and not defined in the same translation unit -- exactly the case patch 12 fixes.$ cat test-joe.patch diff --git a/net/core/dev.c b/net/core/dev.c index 06c195906231..14fd3d5f351a 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -5717,6 +5717,7 @@ static int netif_rx_internal(struct sk_buff *skb) unsigned int qtail;ret = enqueue_to_backlog(skb, smp_processor_id(), &qtail); + pr_info("test"); } return ret;}
$ LLVM=/path/to/clang-21/bin/ ./scripts/livepatch/klp-build -T test-joe.patch ... $ ls livepatch-test-joe.koklp-build completes cleanly: no "duplicate reloc" (patch 12), no "Cannot find symbol for section" (patch 9), and the __ex_table/
__jump_tableentries fornetif_rx_internalare present in the output module (patches 10-11). The module was then loaded into a VM running a matching Clang-built kernel:# insmod livepatch-test-joe.ko [ 105.584252] livepatch: enabling patch 'livepatch_test_joe' [ 105.586141] livepatch: 'livepatch_test_joe': starting patching transition [ 106.824310] livepatch: 'livepatch_test_joe': patching complete [ 120.036530] test# cat /sys/kernel/livepatch/livepatch_test_joe/{enabled,transition} 1 0Toggling the
netif_rxtracepoint exercises the jump-table relocation patch 12 folds for the unexported__tracepoint_netif_rxkey, and lets us cross-check the patch's ownpr_info()against independent evidence that the static branch actually flipped:# echo 1 > /sys/kernel/debug/tracing/events/net/netif_rx/enable # ping -c2 127.0.0.1 # cat /sys/kernel/debug/tracing/trace | tail -4 ping-1267 [001] b.... 472.753651: netif_rx: dev=lo skbaddr=... len=84 ping-1267 [001] b.s2. 472.753671: netif_rx: dev=lo skbaddr=... len=84 ping-1267 [001] b.... 473.762993: netif_rx: dev=lo skbaddr=... len=84 ping-1267 [001] b.s2. 473.763096: netif_rx: dev=lo skbaddr=... len=84 # dmesg | tail -4 [ 472.753653] test [ 472.753671] test [ 473.762994] test [ 473.763096] testEach traced packet has a matching "test" line at the same timestamp (microsecond-aligned), confirming the tracepoint's static-key relocation and the patch's own code path are both live at once. Disabling and unloading works cleanly:
# echo 0 > /sys/kernel/livepatch/livepatch_test_joe/enabled [ 557.922932] livepatch: 'livepatch_test_joe': starting unpatching transition [ 558.905248] livepatch: 'livepatch_test_joe': unpatching complete # rmmod livepatch_test_joeChanges since v2 [3]:
- Patches 1-8 are unchanged.
- New patch 9: selectFTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY.arch/loongarch/Makefilealready builds with -fpatchable-function-entry=2, but nothing selected this, so kbuild ran a pointless recordmcount pass on every object. Harmless under GCC (GAS always emits a section symbol for recordmcount to anchor on), but it fails under Clang with -ffunction-sections (as klp-build uses) for any__weakfunction that ends up alone in its section, e.g.sched_clock. (Joe Lawrence)
- New patch 10: convert local-label references in special sections. GCC/GAS on LoongArch reference __ex_table/__bug_table/__jump_table entries via a local text label instead of the usual "section symbol
+ offset" that objtool'sklp-diff.cassumes; the label is never cloned into the livepatch module, so such entries were silently dropped from GCC-built modules with no error at build or load time. (Joe Lawrence)
- New patch 11: fixANNOTATE_DATA_SPECIALparsing for the same local label references.create_fake_symbols()computed each annotated entry's offset from the relocation's addend alone, which is only correct for the section-symbol form; with local labels, every entry in a special section got offset 0, producing a single fake symbol that covered (and pulled into the output module) the section's entire contents instead of just the patched function's entries. Found while verifying patch 10 with a real special-section entry.
- New patch 12: fold LoongArch's paired ADD/SUB relocations into a single PC-relative one. The "key - ." field of a__jump_tableentry can come out as two relocations at the same offset (R_LARCH_ADD64+R_LARCH_SUB64) when Clang's integrated assembler encounters a key that isn't defined in the same translation unit (e.g. a tracepoint's static key); objtool only allows one relocation per offset, so cloning such an entry failed with "duplicate reloc". (Joe Lawrence)[1] https://git.kernel.org/pub/scm/linux/kernel/git/jpoimboe/linux.git/log/?h=klp-build-arm64
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/George Guo (12):
objtool/LoongArch: Addarch_adjusted_addend()for KLP support
LoongArch: Mark special sections for KLP support livepatch/klp-build: disable direct-extern-access for LoongArch to fix
kernel panic
livepatch/klp-build: build LoongArch with -fPIC to keep GOT-indirect
symbol references
LoongArch: Fix EFI linking with -fdata-sections objtool/klp: Add LoongArch jump opcode bytes support
klp-build: Add LoongArch syscall patching macro
LoongArch: Add livepatch build (KLP) support
LoongArch: SelectFTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRYobjtool/klp: Convert local label references in special sections objtool/klp: FixANNOTATE_DATA_SPECIALparsing for local label
references
objtool/klp: Fold LoongArch paired ADD/SUB relocations into PCRELarch/loongarch/Kconfig | 2 + arch/loongarch/include/asm/alternative-asm.h | 5 +- arch/loongarch/include/asm/alternative.h | 6 +- arch/loongarch/include/asm/asm-extable.h | 10 +- arch/loongarch/include/asm/bug.h | 1 + arch/loongarch/include/asm/jump_label.h | 2 + arch/loongarch/kernel/asm-offsets.c | 20 ++++ arch/loongarch/kernel/vmlinux.lds.S | 2 +- include/linux/livepatch_helpers.h | 22 ++++ scripts/livepatch/klp-build | 36 +++++- tools/objtool/Makefile | 3 +- tools/objtool/arch/loongarch/decode.c | 108 ++++++++++++++++++ .../objtool/arch/loongarch/include/arch/elf.h | 18 +++ tools/objtool/klp-diff.c | 57 ++++++++- 14 files changed, 280 insertions(+), 12 deletions(-)base-commit:
85afaba140a4b9f20fe8c8a64b24fc85f022d981--
2.25.1
Hi Huacai,
Thanks a lot for pointing this out!
Actually, I haven't stopped working on this patch series; rather, I've been continuously refining it. During the process of perfecting this feature, I realized that making it fully robust is no small feat. As a result, I have been iteratively polishing it in my own local tree. You can find my current progress here [1].
I did notice that Dongtai went ahead and sent out a version of this series. However, as pointed out by other reviewers, that patchset still has some issues—which is quite understandable given the complexity.
Out of caution, and since I personally feel this feature is not extremely urgent for the LoongArch architecture, I previously chose not to send a "half-baked" version to the mailing list. Livepatching (KLP) involves far too many corner cases. That is also why I remained quiet while Dongtai was iterating on his version.
Furthermore, to avoid any unnecessary conflicts, I have just updated my patchset [1] to attribute the overlapping parts to Dongtai (setting him as the author), while keeping my original commit titles and messages.
I believe we can slow down the pace a bit and give everyone enough room to get things right the first time, which is much better than rushing it.
Lastly, my apologies for not keeping everyone updated on my work earlier.
Thanks,
WangYuli
[1]: https://github.com/Avenger-285714/wyl-linux-dev/pull/4
Quoted 197 lines · Huacai Chen
Hi,George,
Quoted 9 lines · George Guo
This series adds LoongArch support for the klp-build livepatch tooling, enabling automated livepatch module generation using objtool on LoongArch.
It is based on Josh Poimboeuf's klp-build series [1] (see base-commit below, unchanged from v1/v2) and extends it with the LoongArch-specific objtool and build pieces, plus toolchain/relocation fixes required to make livepatch modules load, generate, and run correctly under both GCC and Clang.
1. Wang Yuli released his version first; re-releasing it without any prior communication is simply rude.
https://lore.kernel.org/loongarch/[email protected]/2. Perhaps you were genuinely unaware about his earlier patches, but Wang Yuli had already alerted you in your v1. At that point, you had two options: a) abandon your own patches and let Wang Yuli continue his development, or b) thank Wang Yuli for the reminder and assistance, then seek to collaborate on development. However, you did neither; instead, you released new versions on your own again and again. This is highly unethical.
3. You might argue that Wang Yuli’s version is incomplete, but is yours complete? Since you’re releasing v3, it’s clear that your v1 and v2 were also incomplete. If you can simply take over someone else’s project just because their version is incomplete, does that mean Wang Yuli can now throw yours in the trash when he releases his v2?
Huacai
Quoted 163 lines
Overview:
- Patches 1-8 are unchanged from v2 [3]: the LoongArch objtool hooks, special-section marking, the -fPIC/-mdirect-extern-access fixes, EFI linking,
KLP_SYSCALL_DEFINEx()support, and finally wiring up LoongArch klp-build (kept last so the series stays bisectable).
- Patches 9-12 are new in v3, fixing issues Joe Lawrence found across two rounds of review by actually running the full klp-build flow (see Changes since v2 below for details).All new patches (9-12) fix bugs in
objtool/klp-diff.cand the LoongArchKconfig; none of them touch other architectures.Testing:
GCC on LoongArch hardware: livepatch modules generated, loaded, and exercised (unchanged from v2).
Clang 21.1.8 on LoongArch hardware, reproduced end to end with Joe's exact test case (test-joe.patch below), since it happens to exercise patches 10-12 together:
netif_rx_internal()has__ex_tableentries, a__bug_tableentry, and three__jump_tablestatic keys, one of which (a tracepoint's key) is unexported and not defined in the same translation unit -- exactly the case patch 12 fixes.$ cat test-joe.patch diff --git a/net/core/dev.c b/net/core/dev.c index 06c195906231..14fd3d5f351a 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -5717,6 +5717,7 @@ static int netif_rx_internal(struct sk_buff *skb) unsigned int qtail;ret = enqueue_to_backlog(skb, smp_processor_id(), &qtail); + pr_info("test"); } return ret; }$ LLVM=/path/to/clang-21/bin/ ./scripts/livepatch/klp-build -T test-joe.patch ... $ ls livepatch-test-joe.koklp-build completes cleanly: no "duplicate reloc" (patch 12), no "Cannot find symbol for section" (patch 9), and the __ex_table/
__jump_tableentries fornetif_rx_internalare present in the output module (patches 10-11). The module was then loaded into a VM running a matching Clang-built kernel:# insmod livepatch-test-joe.ko [ 105.584252] livepatch: enabling patch 'livepatch_test_joe' [ 105.586141] livepatch: 'livepatch_test_joe': starting patching transition [ 106.824310] livepatch: 'livepatch_test_joe': patching complete [ 120.036530] test# cat /sys/kernel/livepatch/livepatch_test_joe/{enabled,transition} 1 0Toggling the
netif_rxtracepoint exercises the jump-table relocation patch 12 folds for the unexported__tracepoint_netif_rxkey, and lets us cross-check the patch's ownpr_info()against independent evidence that the static branch actually flipped:# echo 1 > /sys/kernel/debug/tracing/events/net/netif_rx/enable # ping -c2 127.0.0.1 # cat /sys/kernel/debug/tracing/trace | tail -4 ping-1267 [001] b.... 472.753651: netif_rx: dev=lo skbaddr=... len=84 ping-1267 [001] b.s2. 472.753671: netif_rx: dev=lo skbaddr=... len=84 ping-1267 [001] b.... 473.762993: netif_rx: dev=lo skbaddr=... len=84 ping-1267 [001] b.s2. 473.763096: netif_rx: dev=lo skbaddr=... len=84 # dmesg | tail -4 [ 472.753653] test [ 472.753671] test [ 473.762994] test [ 473.763096] testEach traced packet has a matching "test" line at the same timestamp (microsecond-aligned), confirming the tracepoint's static-key relocation and the patch's own code path are both live at once. Disabling and unloading works cleanly:
# echo 0 > /sys/kernel/livepatch/livepatch_test_joe/enabled [ 557.922932] livepatch: 'livepatch_test_joe': starting unpatching transition [ 558.905248] livepatch: 'livepatch_test_joe': unpatching complete # rmmod livepatch_test_joeChanges since v2 [3]:
- Patches 1-8 are unchanged.
- New patch 9: selectFTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY.arch/loongarch/Makefilealready builds with -fpatchable-function-entry=2, but nothing selected this, so kbuild ran a pointless recordmcount pass on every object. Harmless under GCC (GAS always emits a section symbol for recordmcount to anchor on), but it fails under Clang with -ffunction-sections (as klp-build uses) for any__weakfunction that ends up alone in its section, e.g.sched_clock. (Joe Lawrence)
- New patch 10: convert local-label references in special sections. GCC/GAS on LoongArch reference __ex_table/__bug_table/__jump_table entries via a local text label instead of the usual "section symbol
+ offset" that objtool'sklp-diff.cassumes; the label is never cloned into the livepatch module, so such entries were silently dropped from GCC-built modules with no error at build or load time. (Joe Lawrence)
- New patch 11: fixANNOTATE_DATA_SPECIALparsing for the same local label references.create_fake_symbols()computed each annotated entry's offset from the relocation's addend alone, which is only correct for the section-symbol form; with local labels, every entry in a special section got offset 0, producing a single fake symbol that covered (and pulled into the output module) the section's entire contents instead of just the patched function's entries. Found while verifying patch 10 with a real special-section entry.
- New patch 12: fold LoongArch's paired ADD/SUB relocations into a single PC-relative one. The "key - ." field of a__jump_tableentry can come out as two relocations at the same offset (R_LARCH_ADD64+R_LARCH_SUB64) when Clang's integrated assembler encounters a key that isn't defined in the same translation unit (e.g. a tracepoint's static key); objtool only allows one relocation per offset, so cloning such an entry failed with "duplicate reloc". (Joe Lawrence)[1] https://git.kernel.org/pub/scm/linux/kernel/git/jpoimboe/linux.git/log/?h=klp-build-arm64
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/George Guo (12):
objtool/LoongArch: Addarch_adjusted_addend()for KLP support
LoongArch: Mark special sections for KLP support livepatch/klp-build: disable direct-extern-access for LoongArch to fix
kernel panic
livepatch/klp-build: build LoongArch with -fPIC to keep GOT-indirect
symbol references
LoongArch: Fix EFI linking with -fdata-sections objtool/klp: Add LoongArch jump opcode bytes support
klp-build: Add LoongArch syscall patching macro
LoongArch: Add livepatch build (KLP) support
LoongArch: SelectFTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRYobjtool/klp: Convert local label references in special sections objtool/klp: FixANNOTATE_DATA_SPECIALparsing for local label
references
objtool/klp: Fold LoongArch paired ADD/SUB relocations into PCRELarch/loongarch/Kconfig | 2 + arch/loongarch/include/asm/alternative-asm.h | 5 +- arch/loongarch/include/asm/alternative.h | 6 +- arch/loongarch/include/asm/asm-extable.h | 10 +- arch/loongarch/include/asm/bug.h | 1 + arch/loongarch/include/asm/jump_label.h | 2 + arch/loongarch/kernel/asm-offsets.c | 20 ++++ arch/loongarch/kernel/vmlinux.lds.S | 2 +- include/linux/livepatch_helpers.h | 22 ++++ scripts/livepatch/klp-build | 36 +++++- tools/objtool/Makefile | 3 +- tools/objtool/arch/loongarch/decode.c | 108 ++++++++++++++++++ .../objtool/arch/loongarch/include/arch/elf.h | 18 +++ tools/objtool/klp-diff.c | 57 ++++++++- 14 files changed, 280 insertions(+), 12 deletions(-)base-commit:
85afaba140a4b9f20fe8c8a64b24fc85f022d981--
2.25.1
Hi Huacai,
Thank you for raising this -- I take the concern seriously, and I'd like to lay out the full history so everyone is working from the same facts.
Quoted 4 lines
2. ... Wang Yuli had already alerted you in your v1. At that point, you had two options: a) abandon your own patches ... or b) thank Wang Yuli for the reminder and assistance, then seek to collaborate on development. However, you did neither
I did take option (b). Wang Yuli's v1 messages were code review, and I thanked him on-list and folded every point into v2:
https://lore.kernel.org/all/[email protected]/
"Thanks for the review -- all three points are addressed in v2. ...
You were right that this had not been properly exercised before ..."
His "awM is dangerous, see how x86 did" comment in particular led directly to the ANNOTATE_DATA_SPECIAL rework that is in the series now. The review made these patches better and I'm grateful for it.
Quoted 2 lines
1. Wang Yuli released his version first; re-releasing it without any prior communication is simply rude.
On priority: this series is not a fork of Wang Yuli's. I have been carrying LoongArch livepatch tooling since kpatch, which is klp-build's predecessor (same maintainer, later folded into the kernel). My kpatch LoongArch support was posted on 2024-12-05 and merged 2026-01-12:
https://github.com/dynup/kpatch/pull/1427
developed over several review rounds with Joe Lawrence. When Josh announced the kpatch -> klp-build transition, he listed the LoongArch port as coming from me:
https://github.com/dynup/kpatch/issues/1498
("loongarch ... shared by @georgejguo below, coming soon")
So this klp-build series is the continuation of my own earlier work, not a takeover of someone else's.
On scope: Wang Yuli's own suggestion in v1 was to use his patch to replace "these two patches" -- i.e. the basic arch-enablement piece. This series is the objtool/klp-diff and build-tooling integration (12 patches), including four fixes Joe found by running the full klp-build flow end to end. The overlap is just the arch-enablement part.
Quoted 2 lines
does that mean Wang Yuli can now throw yours in the trash when he releases his v2?
I don't see this as anyone's to throw away -- it's shared work toward the same goal, and I'd much rather converge than duplicate. Concretely:
I'm happy to fold the overlapping arch-enablement patch in with Wang Yuli as author (or Co-developed-by), and to work with him on the rest.
Wang Yuli, if you're up for it, let's coordinate -- I can take the objtool/klp-diff pieces and we land one clean series together.
Thanks,
George
Huacai Chen
maintainerQuoted 14 lines · George Guo
Hi Huacai,
Thank you for raising this -- I take the concern seriously, and I'd like to lay out the full history so everyone is working from the same facts.
Quoted 4 lines
2. ... Wang Yuli had already alerted you in your v1. At that point, you had two options: a) abandon your own patches ... or b) thank Wang Yuli for the reminder and assistance, then seek to collaborate on development. However, you did neither
I did take option (b). Wang Yuli's v1 messages were code review, and I thanked him on-list and folded every point into v2:
Quoted 8 lines
https://lore.kernel.org/all/[email protected]/
"Thanks for the review -- all three points are addressed in v2. ...
You were right that this had not been properly exercised before ..."His "awM is dangerous, see how x86 did" comment in particular led directly to the
ANNOTATE_DATA_SPECIALrework that is in the series now. The review made these patches better and I'm grateful for it.
There is no "Co-developed-by: Yuli Wang" in v2 and v3, right?
Quoted 8 lines
Quoted 3 lines
1. Wang Yuli released his version first; re-releasing it without any prior communication is simply rude.
On priority: this series is not a fork of Wang Yuli's. I have been carrying LoongArch livepatch tooling since kpatch, which is klp-build's predecessor (same maintainer, later folded into the kernel). My kpatch LoongArch support was posted on 2024-12-05 and merged 2026-01-12:
Whether it is a fork is not important, the most important thing is that his klp-build patches appeared in the maillist first.
Quoted 22 lines
https://github.com/dynup/kpatch/pull/1427
developed over several review rounds with Joe Lawrence. When Josh announced the kpatch -> klp-build transition, he listed the LoongArch port as coming from me:
https://github.com/dynup/kpatch/issues/1498
("loongarch ... shared by @georgejguo below, coming soon")So this klp-build series is the continuation of my own earlier work, not a takeover of someone else's.
On scope: Wang Yuli's own suggestion in v1 was to use his patch to replace "these two patches" -- i.e. the basic arch-enablement piece. This series is the objtool/klp-diff and build-tooling integration (12 patches), including four fixes Joe found by running the full klp-build flow end to end. The overlap is just the arch-enablement part.
Quoted 2 lines
does that mean Wang Yuli can now throw yours in the trash when he releases his v2?
Quoted 7 lines
I don't see this as anyone's to throw away -- it's shared work toward the same goal, and I'd much rather converge than duplicate. Concretely:
I'm happy to fold the overlapping arch-enablement patch in with Wang Yuli as author (or Co-developed-by), and to work with him on the rest.Wang Yuli, if you're up for it, let's coordinate -- I can take the objtool/klp-diff pieces and we land one clean series together.
That's great, later is better than never. I hope the final version is a complete and elegant one, and reflects both of your efforts.
Huacai
Quoted 4 lines
Thanks,
George