Skip to content

Commit e4770a6

Browse files
georgejguogeorge
authored andcommitted
kpatch/LoongArch: fix kernel panic by disabling direct-extern-access for livepatches
On LoongArch systems, livepatch modules containing references to global variables trigger kernel panics when the core kernel is built with -mdirect-extern-access optimization. Root cause: The -mdirect-extern-access optimization replaces GOT-based symbol access with direct addressing. While this improves performance, it breaks the module loading mechanism which relies on GOT entries for proper symbol relocation. Direct access to global variables from livepatch modules causes invalid memory accesses and kernel panics. Solution: For LoongArch kpatch builds, conditionally disable direct-extern-access by adding: - -mno-direct-extern-access for GCC builds - -fno-direct-access-external-data for Clang builds Signed-off-by: george <guodongtai@kylinos.cn>
1 parent eab48a0 commit e4770a6

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

kpatch-build/kpatch-build

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,19 @@ clang_version_check() {
382382
return
383383
}
384384

385+
cc_option_check() {
386+
local option="$1"
387+
388+
if $GCC -Werror "$option" -c -x c /dev/null -o /dev/null 2>/dev/null; then
389+
CC_OPTION+="$option";
390+
fi
391+
392+
if $CLANG -Werror "$option" -c -x c /dev/null -o /dev/null 2>/dev/null; then
393+
CC_OPTION+="$option";
394+
fi
395+
396+
}
397+
385398
find_special_section_data() {
386399
local -A check
387400

@@ -1291,7 +1304,15 @@ declare -a MAKEVARS
12911304
if [[ -n "$CONFIG_CC_IS_CLANG" ]]; then
12921305
MAKEVARS+=("CC=${KPATCH_CC_PREFIX}${CLANG}")
12931306
MAKEVARS+=("HOSTCC=clang")
1307+
if [[ "$ARCH" = "loongarch64" ]]; then
1308+
cc_option_check "-fno-direct-access-external-data"
1309+
MAKEVARS+=("KBUILD_CFLAGS_KERNEL+="$CC_OPTION"")
1310+
fi
12941311
else
1312+
if [[ "$ARCH" = "loongarch64" ]]; then
1313+
cc_option_check "-mno-direct-extern-access"
1314+
MAKEVARS+=("KBUILD_CFLAGS_KERNEL+="$CC_OPTION"")
1315+
fi
12951316
MAKEVARS+=("CC=${KPATCH_CC_PREFIX}${GCC}")
12961317
fi
12971318

0 commit comments

Comments
 (0)