Live Patching
 help / color / mirror / Atom feed
From: George Guo <dongtai.guo@linux.dev>
To: xry111@xry111.site, wangrui@loongson.cn, chenhuacai@kernel.org,
	jpoimboe@kernel.org, peterz@infradead.org, jikos@kernel.org,
	mbenes@suse.cz, pmladek@suse.com
Cc: kernel@xen0n.name, joe.lawrence@redhat.com, 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, 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 12/12] objtool/klp: Fold LoongArch paired ADD/SUB relocations into PCREL
Date: Thu, 16 Jul 2026 16:33:21 +0800	[thread overview]
Message-ID: <20260716083321.14515-1-dongtai.guo@linux.dev> (raw)
In-Reply-To: <d89e9c660f06489c59c2f1f7fae0547abbefac71.camel@xry111.site>

Hi Xi Ruoyao,

> 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.

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

  reply	other threads:[~2026-07-16  8:33 UTC|newest]

Thread overview: 28+ 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
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-16  8:33     ` George Guo [this message]
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=20260716083321.14515-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=wangrui@loongson.cn \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox