Live Patching
 help / color / mirror / Atom feed
From: George Guo <dongtai.guo@linux.dev>
To: 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, xry111@xry111.site, 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: [PATCH 5/8] LoongArch: fix kernel panic with -fPIC for same-compilation-unit symbol references
Date: Thu,  4 Jun 2026 14:53:14 +0800	[thread overview]
Message-ID: <20260604065317.219777-6-dongtai.guo@linux.dev> (raw)
In-Reply-To: <20260604065317.219777-1-dongtai.guo@linux.dev>

From: George Guo <guodongtai@kylinos.cn>

Add architecture-specific -fPIC compiler flag for LoongArch64 to prevent
kernel panics when applying livepatches containing references to symbols
defined in the same compilation unit.

Root cause:
In the KLP workflow, when a function is livepatched, it's extracted
from the original object file and compiled into a separate kernel
module. When the patched function references symbols defined in the
same compilation unit (like 'uts_sem' in kernel/sys.c), these
references break if not compiled as position-independent code.

On LoongArch64, without -fPIC, references to same-compilation-unit symbols
use absolute addressing that assumes fixed memory locations. When the
function is relocated into the livepatch module, these absolute addresses
become invalid, causing kernel panics.

Example failure case:
- SYSCALL_DEFINE1(newuname) references the same-compilation-unit
  symbol 'uts_sem'
- When kpatch extracts this function into a module, the reference to
  'uts_sem' must be properly relocated
- Without -fPIC, the absolute address reference causes invalid memory
  access and kernel panic

Solution:
Force -fPIC compilation for all LoongArch64 KLP builds. This ensures
that references to same-compilation-unit symbols use position-independent
addressing, allowing proper relocation by the kernel module loader and
preventing kernel panics in livepatch scenarios.

Co-developed-by: Kexin Liu <liukexin@kylinos.cn>
Signed-off-by: Kexin Liu <liukexin@kylinos.cn>
Signed-off-by: George Guo <guodongtai@kylinos.cn>
---
 scripts/livepatch/klp-build | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/scripts/livepatch/klp-build b/scripts/livepatch/klp-build
index 529437d75346..83a43e0df3b9 100755
--- a/scripts/livepatch/klp-build
+++ b/scripts/livepatch/klp-build
@@ -543,8 +543,10 @@ fix_patches() {
 clean_kernel() {
 	local cmd=()
 	local ARCH_KBUILD_CFLAGS_KERNEL=""
+	local ARCH_KCFLAGS=""
 
 	if [[ -v CONFIG_LOONGARCH && "$CONFIG_LOONGARCH" == "y" ]]; then
+		ARCH_KCFLAGS="-fPIC"
 		if [[ -n "$CONFIG_CC_IS_CLANG" ]]; then
 			ARCH_KBUILD_CFLAGS_KERNEL="-fno-direct-access-external-data"
 		else
@@ -592,7 +594,7 @@ build_kernel() {
 		cmd+=("-s")
 	fi
 	cmd+=("-j$JOBS")
-	cmd+=("KCFLAGS=-ffunction-sections -fdata-sections")
+	cmd+=("KCFLAGS=-ffunction-sections -fdata-sections $ARCH_KCFLAGS")
 	cmd+=("KBUILD_CFLAGS_KERNEL=$ARCH_KBUILD_CFLAGS_KERNEL")
 	cmd+=("vmlinux")
 	cmd+=("modules")
-- 
2.25.1


  parent reply	other threads:[~2026-06-04  6:56 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 ` George Guo [this message]
2026-06-05  8:10   ` [PATCH 5/8] LoongArch: fix kernel panic with -fPIC for same-compilation-unit symbol references Xi Ruoyao
2026-06-08 10:23     ` George Guo
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=20260604065317.219777-6-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