Building the Linux kernel with Clang and LLVM
 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, wangrui@loongson.cn,
	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: [PATCH v4 03/14] livepatch/klp-build: use -fPIC and drop direct-extern-access on LoongArch
Date: Fri, 24 Jul 2026 19:41:16 +0800	[thread overview]
Message-ID: <20260724114128.31451-4-dongtai.guo@linux.dev> (raw)
In-Reply-To: <20260724114128.31451-1-dongtai.guo@linux.dev>

From: George Guo <guodongtai@kylinos.cn>

On LoongArch, the klp relocation machinery redirects a livepatch's
reference to a core-kernel symbol through a GOT entry. klp-build extracts
the patched function into a separate module while the referenced symbol
stays in the core kernel, so the reference must be GOT-indirect for that
redirect to work. Two default compiler behaviours defeat this and make a
loaded livepatch module fault.

The first is a same-unit global (the syscall test patches sys_newuname()
in kernel/sys.c, which reads 'uts_sem', a rw_semaphore defined in the
same file). With CONFIG_RELOCATABLE=y the kernel is built -fPIE. For
a global defined in the same unit, -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_*). Once klp-build moves the patched
function into the livepatch module, the direct reference has no GOT
slot to redirect and faults.

The second is an extern global. -mdirect-extern-access replaces GOT-based
external symbol access with direct addressing. A livepatch reference to
such a symbol then has no GOT slot to fix up, and the wrong address faults
the kernel. This optimization was added by commit 38b10b269d04
("LoongArch: Tweak CFLAGS for Clang compatibility") as a nice-to-have that
reduces GOT accesses.

For LoongArch KLP builds, keep both forms GOT-indirect:

 - Add -fPIC via KCFLAGS. -fPIE is not enough; it optimizes away the very
   GOT indirection KLP relies on.
 - Disable direct-extern-access: -mno-direct-extern-access for GCC,
   -fno-direct-access-external-data for Clang.

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 | 43 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/scripts/livepatch/klp-build b/scripts/livepatch/klp-build
index e83973567c87..27463def08f8 100755
--- a/scripts/livepatch/klp-build
+++ b/scripts/livepatch/klp-build
@@ -278,6 +278,13 @@ validate_config() {
 	[[ -x "$OBJTOOL" ]] && "$OBJTOOL" klp 2>&1 | command grep -q "not implemented" && \
 		die "objtool not built with KLP support; install xxhash-devel/libxxhash-dev (version >= 0.8) and recompile"
 
+	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"
+	fi
+
 	return 0
 }
 
@@ -556,6 +563,31 @@ build_kernel() {
 	local log="$TMP_DIR/build.log"
 	local cmd=()
 
+	local ARCH_KBUILD_CFLAGS_KERNEL=""
+	local ARCH_KCFLAGS=""
+
+	# For KLP, LoongArch symbol references must stay GOT-indirect so the klp
+	# relocation machinery can redirect a cross-object reference through a
+	# GOT entry.  Two default behaviours defeat that (CONFIG_RELOCATABLE and
+	# CONFIG_AS_HAS_EXPLICIT_RELOCS are checked in validate_config()):
+	#
+	#  - -fPIE (added under CONFIG_RELOCATABLE) emits a direct PC-relative
+	#    reference for a same-unit global and skips the GOT; -fPIC routes it
+	#    through the GOT.  Add -fPIC via KCFLAGS.
+	#  - -mdirect-extern-access replaces GOT access for an extern global with
+	#    direct addressing.  Disable it.
+	if [[ -v CONFIG_LOONGARCH ]]; then
+		ARCH_KCFLAGS="-fPIC"
+
+		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
@@ -581,7 +613,16 @@ build_kernel() {
 		cmd+=("-s")
 	fi
 	cmd+=("-j$JOBS")
-	cmd+=("KCFLAGS=-ffunction-sections -fdata-sections")
+	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")
 
-- 
2.53.0


  parent reply	other threads:[~2026-07-24 11:42 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-24 11:41 [PATCH v4 00/14] LoongArch: Add livepatch build (KLP) support George Guo
2026-07-24 11:41 ` [PATCH v4 01/14] objtool/LoongArch: Add arch_adjusted_addend() for KLP support George Guo
2026-07-24 11:41 ` [PATCH v4 02/14] LoongArch: Mark special sections " George Guo
2026-07-24 11:41 ` George Guo [this message]
2026-07-24 11:41 ` [PATCH v4 04/14] LoongArch: Fix EFI linking with -fdata-sections George Guo
2026-07-24 11:41 ` [PATCH v4 05/14] objtool/klp: Add LoongArch jump opcode bytes support George Guo
2026-07-24 11:41 ` [PATCH v4 06/14] klp-build: Add LoongArch syscall patching macro George Guo
2026-07-24 11:41 ` [PATCH v4 07/14] LoongArch: Add livepatch build (KLP) support George Guo
2026-07-24 11:41 ` [PATCH v4 08/14] LoongArch: Select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY George Guo
2026-07-24 11:41 ` [PATCH v4 09/14] objtool/klp: Convert local label references George Guo
2026-07-24 11:41 ` [PATCH v4 10/14] objtool/klp: Fix ANNOTATE_DATA_SPECIAL parsing for " George Guo
2026-07-24 11:41 ` [PATCH v4 11/14] objtool/klp: Fold LoongArch paired ADD/SUB relocations into PCREL George Guo
2026-07-24 11:41 ` [PATCH v4 12/14] objtool/LoongArch: Enable inline alternative cloning for KLP George Guo
2026-07-24 11:41 ` [PATCH v4 13/14] objtool/klp: Keep LoongArch tablejump annotation table entries George Guo
2026-07-24 11:41 ` [PATCH v4 14/14] objtool/klp: Rewrite PC-relative data references to GOT on LoongArch George Guo

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=20260724114128.31451-4-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