Skip to content

Commit 1a99e6c

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 <[email protected]>
1 parent eab48a0 commit 1a99e6c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

kpatch-build/kpatch-build

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ KLP_REPLACE=1
6161

6262
GCC="${CROSS_COMPILE:-}gcc"
6363
CLANG="${CROSS_COMPILE:-}clang"
64+
CC_OPTION=""
6465
LD="${CROSS_COMPILE:-}ld"
6566
LLD="${CROSS_COMPILE:-}ld.lld"
6667
READELF="${CROSS_COMPILE:-}readelf"
@@ -382,6 +383,21 @@ clang_version_check() {
382383
return
383384
}
384385

386+
cc_option_check() {
387+
local option="$1"
388+
local compiler=""
389+
390+
if [[ -n "$CONFIG_CC_IS_CLANG" ]]; then
391+
compiler="$CLANG"
392+
else
393+
compiler="$GCC"
394+
fi
395+
396+
if $compiler -Werror "$option" -c -x c /dev/null -o /dev/null 2>/dev/null; then
397+
CC_OPTION+=" $option";
398+
fi
399+
}
400+
385401
find_special_section_data() {
386402
local -A check
387403

@@ -1291,7 +1307,15 @@ declare -a MAKEVARS
12911307
if [[ -n "$CONFIG_CC_IS_CLANG" ]]; then
12921308
MAKEVARS+=("CC=${KPATCH_CC_PREFIX}${CLANG}")
12931309
MAKEVARS+=("HOSTCC=clang")
1310+
if [[ "$ARCH" = "loongarch64" ]]; then
1311+
cc_option_check "-fno-direct-access-external-data"
1312+
MAKEVARS+=("KBUILD_CFLAGS_KERNEL+=${CC_OPTION}")
1313+
fi
12941314
else
1315+
if [[ "$ARCH" = "loongarch64" ]]; then
1316+
cc_option_check "-mno-direct-extern-access"
1317+
MAKEVARS+=("KBUILD_CFLAGS_KERNEL+=${CC_OPTION}")
1318+
fi
12951319
MAKEVARS+=("CC=${KPATCH_CC_PREFIX}${GCC}")
12961320
fi
12971321

0 commit comments

Comments
 (0)