From: George Guo <dongtai.guo@linux.dev>
To: Xi Ruoyao <xry111@xry111.site>,
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,
loongarch@lists.linux.dev, live-patching@vger.kernel.org,
llvm@lists.linux.dev, linux-kernel@vger.kernel.org,
George Guo <guodongtai@kylinos.cn>
Subject: Re: [PATCH 5/8] LoongArch: fix kernel panic with -fPIC for same-compilation-unit symbol references
Date: Mon, 8 Jun 2026 18:23:26 +0800 [thread overview]
Message-ID: <20260608102326.334246-1-dongtai.guo@linux.dev> (raw)
In-Reply-To: <046da9c8886dcb35d6427b7467ce551ef1426480.camel@xry111.site>
On Fri, 2026-06-05 at 16:10 +0800, Xi Ruoyao wrote:
> This is incorrect.
>
> With GCC and Clang, -fPIC does not mean generating position independent
> code. It means generating position independent code *suitable for
> SVR4-style shared objects*.
>
> The kernel has nothing to do with SVR4-style shared objects. The
> kernel modules are not SVR4 shared objects but relocatable ELF object
> files.
You are right, and thank you for the correction. The commit message was
wrong: with CONFIG_RELOCATABLE=y the kernel is already built with -fPIE,
so the reference is not "absolute addressing" as I claimed. I will fix
the description in v2.
> If you only need position independent code (without the shared object
> stuff) you should use -fPIE instead. The kernel is already built with
> -fPIE if CONFIG_RELOCATABLE=y.
>
> Thus if you only need position independent code you should make live
> patching depend on CONFIG_RELOCATABLE.
This is the part I should have explained better. CONFIG_RELOCATABLE=y is
already set in my config, so the kernel is already -fPIE, and the panic
still happens. I also tried building the KLP objects with -fPIE
explicitly and it does not help -- only -fPIC fixes it.
The reason is not "position independence" but GOT indirection, which is
exactly the SVR4-shared-object behaviour you mention. A minimal
'return uts_sem;' (uts_sem defined in the same TU) compiles like this on
loongarch64 -- same conclusion under both gcc 14.2.1 and clang 19.1.6:
-fPIE:
pcalau12i $a0, ... R_LARCH_PCALA_HI20 uts_sem
addi.d $a0, $a0, ... R_LARCH_PCALA_LO12 uts_sem
ld.w $a0, $a0, 0
-fPIC:
pcalau12i $a0, ... R_LARCH_GOT_PC_HI20 uts_sem
ld.d $a0, $a0, ... R_LARCH_GOT_PC_LO12 uts_sem
ld.w $a0, $a0, 0
So under -fPIE the access is direct (no GOT); under -fPIC it goes through
the GOT. (As an aside, gcc additionally collapses the -fPIE reference
onto a local anchor .LANCHOR0, leaving no named 'uts_sem' relocation at
all; clang keeps the name but still emits the direct, non-GOT form.)
klp-build extracts the patched function out of its original object file
and links it into a separate livepatch module, while the referenced
symbol (e.g. 'uts_sem') stays in the core kernel. The klp relocation
machinery resolves such cross-object references through GOT entries it
creates in the patch module. The direct R_LARCH_PCALA reference emitted
under -fPIE has no GOT slot for it to fix up, so once the function is
moved into the module its target is wrong and it faults.
So this is one of the cases where the KLP machinery genuinely relies on
the GOT-indirect ("shared object style") access that -fPIC provides and
-fPIE deliberately optimizes away. In v2 I will keep -fPIC but rewrite
the commit message to explain this GOT-vs-direct-PCALA reason instead of
the incorrect "absolute addressing" wording.
I can also include the readelf -r / objdump diff (R_LARCH_PCALA_* under
-fPIE vs R_LARCH_GOT_PC_* under -fPIC) in the v2 commit message if that
is useful.
Thanks again for the review.
next prev parent reply other threads:[~2026-06-08 10:23 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-04 6:53 [PATCH 0/8] LoongArch: Add livepatch build (KLP) support George Guo
2026-06-04 6:53 ` [PATCH 1/8] " George Guo
2026-06-04 6:53 ` [PATCH 2/8] objtool/LoongArch: Add arch_adjusted_addend() for KLP support George Guo
2026-06-04 6:53 ` [PATCH 3/8] LoongArch: Add special section entry sizes " George Guo
2026-06-05 8:38 ` WangYuli
2026-06-08 10:33 ` George Guo
2026-06-04 6:53 ` [PATCH 4/8] livepatch/klp-build: disable direct-extern-access for LoongArch to fix kernel panic George Guo
2026-06-05 8:53 ` WangYuli
2026-06-04 6:53 ` [PATCH 5/8] LoongArch: fix kernel panic with -fPIC for same-compilation-unit symbol references George Guo
2026-06-05 8:10 ` Xi Ruoyao
2026-06-08 10:23 ` George Guo [this message]
2026-06-04 6:53 ` [PATCH 6/8] LoongArch: Fix EFI linking with -fdata-sections George Guo
2026-06-04 6:53 ` [PATCH 7/8] objtool/klp: Add LoongArch jump opcode bytes support George Guo
2026-06-04 6:53 ` [PATCH 8/8] klp-build: Add LoongArch syscall patching macro George Guo
2026-06-05 7:57 ` [PATCH 0/8] LoongArch: Add livepatch build (KLP) support WangYuli
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=20260608102326.334246-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=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