All of lore.kernel.org
 help / color / mirror / Atom feed
From: George Guo <dongtai.guo@linux.dev>
To: joe.lawrence@redhat.com
Cc: chenhuacai@kernel.org, jpoimboe@kernel.org, peterz@infradead.org,
	jikos@kernel.org, mbenes@suse.cz, pmladek@suse.com,
	kernel@xen0n.name, rostedt@goodmis.org, ardb@kernel.org,
	nathan@kernel.org, nick.desaulniers+lkml@gmail.com,
	yangtiezhu@loongson.cn, jiaxun.yang@flygoat.com,
	liukexin@kylinos.cn, guodongtai@kylinos.cn, xry111@xry111.site,
	wangyuli@aosc.io, loongarch@lists.linux.dev,
	live-patching@vger.kernel.org, llvm@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 02/12] LoongArch: Mark special sections for KLP support
Date: Mon, 13 Jul 2026 22:33:24 +0800	[thread overview]
Message-ID: <20260713143324.16887-1-dongtai.guo@linux.dev> (raw)
In-Reply-To: <ak7QBSm3mmDlB-4u@redhat.com>

Hi Joe,

Thanks for the test case.

> 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(-)

diff --git a/arch/loongarch/include/asm/alternative-asm.h b/arch/loongarch/include/asm/alternative-asm.h
index d940d9d2c0e1..92122a258a8e 100644
--- a/arch/loongarch/include/asm/alternative-asm.h
+++ b/arch/loongarch/include/asm/alternative-asm.h
@@ -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

diff --git a/arch/loongarch/include/asm/alternative.h b/arch/loongarch/include/asm/alternative.h
index 8f7712ed2f4e..1d3ac2f667d1 100644
--- a/arch/loongarch/include/asm/alternative.h
+++ b/arch/loongarch/include/asm/alternative.h
@@ -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"

 /*
diff --git a/tools/objtool/arch/loongarch/include/arch/elf.h b/tools/objtool/arch/loongarch/include/arch/elf.h
index 0103f27fccfc..e2843679f24e 100644
--- a/tools/objtool/arch/loongarch/include/arch/elf.h
+++ b/tools/objtool/arch/loongarch/include/arch/elf.h
@@ -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	1

 struct elf;

Thanks,
George

  reply	other threads:[~2026-07-13 14:33 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07  7:20 [PATCH v3 00/12] LoongArch: Add livepatch build (KLP) support George Guo
2026-07-07  7:20 ` [PATCH v3 01/12] objtool/LoongArch: Add arch_adjusted_addend() for KLP support George Guo
2026-07-07  7:20 ` [PATCH v3 02/12] LoongArch: Mark special sections " George Guo
2026-07-08 22:32   ` Joe Lawrence
2026-07-13 14:33     ` George Guo [this message]
2026-07-07  7:20 ` [PATCH v3 03/12] livepatch/klp-build: disable direct-extern-access for LoongArch to fix kernel panic George Guo
2026-07-07  7:20 ` [PATCH v3 04/12] livepatch/klp-build: build LoongArch with -fPIC to keep GOT-indirect symbol references George Guo
2026-07-08 15:05   ` Joe Lawrence
2026-07-10  7:38     ` George Guo
2026-07-07  7:20 ` [PATCH v3 05/12] LoongArch: Fix EFI linking with -fdata-sections George Guo
2026-07-08 13:54   ` Joe Lawrence
2026-07-08 16:07     ` Joe Lawrence
2026-07-10  7:46       ` George Guo
2026-07-07  7:20 ` [PATCH v3 06/12] objtool/klp: Add LoongArch jump opcode bytes support George Guo
2026-07-07  7:20 ` [PATCH v3 07/12] klp-build: Add LoongArch syscall patching macro George Guo
2026-07-08 19:18   ` Joe Lawrence
2026-07-10  7:49     ` George Guo
2026-07-07  7:20 ` [PATCH v3 08/12] LoongArch: Add livepatch build (KLP) support George Guo
2026-07-07  7:20 ` [PATCH v3 09/12] LoongArch: Select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY George Guo
2026-07-07  7:20 ` [PATCH v3 10/12] objtool/klp: Convert local label references in special sections George Guo
2026-07-07  7:20 ` [PATCH v3 11/12] objtool/klp: Fix ANNOTATE_DATA_SPECIAL parsing for local label references George Guo
2026-07-07  7:20 ` [PATCH v3 12/12] objtool/klp: Fold LoongArch paired ADD/SUB relocations into PCREL George Guo
2026-07-08 13:29   ` Xi Ruoyao
2026-07-09  9:43 ` [PATCH v3 00/12] LoongArch: Add livepatch build (KLP) support Huacai Chen
2026-07-09 10:35   ` WangYuli
2026-07-09 13:21   ` George Guo
2026-07-10 14:34     ` Huacai Chen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260713143324.16887-1-dongtai.guo@linux.dev \
    --to=dongtai.guo@linux.dev \
    --cc=ardb@kernel.org \
    --cc=chenhuacai@kernel.org \
    --cc=guodongtai@kylinos.cn \
    --cc=jiaxun.yang@flygoat.com \
    --cc=jikos@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=kernel@xen0n.name \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liukexin@kylinos.cn \
    --cc=live-patching@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=loongarch@lists.linux.dev \
    --cc=mbenes@suse.cz \
    --cc=nathan@kernel.org \
    --cc=nick.desaulniers+lkml@gmail.com \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=wangyuli@aosc.io \
    --cc=xry111@xry111.site \
    --cc=yangtiezhu@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.