Skip to content

Commit 79b4354

Browse files
committed
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 bca19e9 commit 79b4354

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

@@ -1288,7 +1301,15 @@ declare -a MAKEVARS
12881301
if [[ -n "$CONFIG_CC_IS_CLANG" ]]; then
12891302
MAKEVARS+=("CC=${KPATCH_CC_PREFIX}${CLANG}")
12901303
MAKEVARS+=("HOSTCC=clang")
1304+
if [[ "$ARCH" = "loongarch64" ]]; then
1305+
cc_option_check "-fno-direct-access-external-data"
1306+
MAKEVARS+=("KBUILD_CFLAGS_KERNEL+="$CC_OPTION"")
1307+
fi
12911308
else
1309+
if [[ "$ARCH" = "loongarch64" ]]; then
1310+
cc_option_check "-mno-direct-extern-access"
1311+
MAKEVARS+=("KBUILD_CFLAGS_KERNEL+="$CC_OPTION"")
1312+
fi
12921313
MAKEVARS+=("CC=${KPATCH_CC_PREFIX}${GCC}")
12931314
fi
12941315

0 commit comments

Comments
 (0)