[PATCH 4/8] livepatch/klp-build: disable direct-extern-access for LoongArch to fix kernel panic
George Guo <[email protected]> Thu, 4 Jun 2026 14:53:13 +0800
| Newsgroups | org.kernel.vger.live-patching,dev.linux.lists.llvm,dev.linux.lists.loongarch,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <[email protected]> |
From: George Guo <[email protected]> On LoongArch systems, livepatch modules containing references to EXTERNAL global variables trigger kernel panics when the core kernel is built with the -mdirect-extern-access optimization. Root cause: The -mdirect-extern-access optimization replaces GOT-based external symbol access with direct addressing for improved performance. However, this breaks the kernel module loading mechanism, which relies on GOT entries for proper relocation of EXTERNAL symbol references. Direct access to global variables from livepatch modules causes invalid memory accesses and kernel panics. Solution: For LoongArch klp builds, conditionally disable direct-extern-access by adding: - -mno-direct-extern-access for GCC builds - -fno-direct-access-external-data for Clang builds See also commit 38b10b269d04 ("LoongArch: Tweak CFLAGS for Clang compatibility"), which added -mdirect-extern-access to the kernel as a nice-to-have optimization that reduces GOT accesses. Co-developed-by: Kexin Liu <[email protected]> Signed-off-by: Kexin Liu <[email protected]> Signed-off-by: George Guo <[email protected]> --- scripts/livepatch/klp-build | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/scripts/livepatch/klp-build b/scripts/livepatch/klp-build index e83973567c87..529437d75346 100755 --- a/scripts/livepatch/klp-build +++ b/scripts/livepatch/klp-build @@ -542,6 +542,17 @@ fix_patches() { clean_kernel() { local cmd=() + local ARCH_KBUILD_CFLAGS_KERNEL="" + + if [[ -v CONFIG_LOONGARCH && "$CONFIG_LOONGARCH" == "y" ]]; then + if [[ -n "$CONFIG_CC_IS_CLANG" ]]; 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") cmd+=("--silent") @@ -582,6 +593,7 @@ build_kernel() { fi cmd+=("-j$JOBS") cmd+=("KCFLAGS=-ffunction-sections -fdata-sections") + cmd+=("KBUILD_CFLAGS_KERNEL=$ARCH_KBUILD_CFLAGS_KERNEL") cmd+=("vmlinux") cmd+=("modules") -- 2.25.1