[PATCH] LoongArch: Avoid livelock when saving sctx_info in signal frame

From: Haoyu Sun

Date: Sun Dec 06 2026 - 06:05:14 EST


In a LoongArch signal frame, each optional extended context (FPU, LSX,
LASX, and LBT) is laid out with a 16-byte sctx_info header immediately
preceding the context body:

struct extended_context {
// unwritable page!
struct sctx_info info;
// --- page boundary ---
struct {fpu,lsx,lasx,lbt}_context ctx;
}

Currently, protected_save_{fpu,lsx,lasx,lbt}_context() write info->magic
and info->size inside the while (1) retry loop, after unlocking FPU/LBT
ownership. When a page boundary falls between info and ctx, with info
on an unwritable page and ctx on a writable page, saving ctx succeeds,
__put_user() to info->magic fails with -EFAULT, and then the retry path
only probes ctx on the writable page. That probe succeeds and resets
err to 0, causing an infinite livelock in kernel mode.

While one could also touch info in the retry path, the retry loop only
exists because saving the hardware context runs inside
lock_{fpu,lbt}_owner() with page faults disabled, so resolvable faults
such as COW cannot be handled inline. Writing sctx_info, however, runs
with page faults enabled: resolvable faults are handled transparently
by do_page_fault(), so an -EFAULT here is already unrecoverable and
should never be retried. Move the __put_user() calls for info->magic
and info->size before the while (1) loop and return immediately
on error.

Found with a signal frame page-boundary scanner while investigating
related LBT signal frame fault-handling issues:
https://lore.kernel.org/loongarch/20261129122728.48472-1-shyliuli@xxxxxxx/

I tested this patch on Loongson-3B6000 (bare metal and KVM).

Antigravity assisted with writing the reproducer and translating this
commit message.

Fixes: b74baf4ad05b ("LoongArch: Add signal handling support")
Fixes: 616500232e63 ("LoongArch: Add vector extensions support")
Fixes: bd3c5798484a ("LoongArch: Add Loongson Binary Translation (LBT) extension support")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Haoyu Sun <shyliuli@xxxxxxx>
---
Based on top of the LBT signal frame fault handling series [1] (already
applied). The scanner below sweeps signal-frame page boundaries across
all save and restore paths.

Tested together with [1] on mainline on Loongson-3B6000 (bare metal and
KVM): all four modes complete cleanly with other=0 and hang=0. Note that
if you run this scanner with only this patch applied (without [1]),
save_lo passes cleanly, while save_hi will still hit the kernel oops in
_save_ftop_context() fixed by [1], which is expected.

[1] https://lore.kernel.org/loongarch/20261129122728.48472-1-shyliuli@xxxxxxx/

Reproducer (scan_sigreturn.c):

#define _GNU_SOURCE
#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <unistd.h>

void *g_prot_addr;
size_t g_pgsz;

/*
* if (g_prot_addr != NULL)
* mprotect(g_prot_addr, g_pgsz, PROT_NONE); // syscall 226
* rt_sigreturn(); // syscall 139
*/
extern void raw_handler(int sig);
__asm__(".text\n"
".globl raw_handler\n"
"raw_handler:\n"
" la.local $t0, g_prot_addr\n"
" ld.d $a0, $t0, 0\n"
" beqz $a0, 1f\n"
" la.local $t0, g_pgsz\n"
" ld.d $a1, $t0, 0\n"
" ori $a2, $zero, 0\n"
" ori $a7, $zero, 226\n"
" syscall 0\n"
"1:\n"
" ori $a7, $zero, 139\n"
" syscall 0\n");

static void run_one(int mode, size_t off)
{
/*
* Disable coredumps for expected SIGSEGV children so systemd-coredump
* does not slow them down and cause false-positive hang timeouts.
*/
prctl(PR_SET_DUMPABLE, 0);
g_pgsz = (size_t)sysconf(_SC_PAGESIZE);
char *base = mmap(NULL, 2 * g_pgsz, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (base == MAP_FAILED)
_exit(99);
memset(base, 0x41, 2 * g_pgsz);

if (mode == 2) {
if (mprotect(base + g_pgsz, g_pgsz, PROT_NONE) != 0)
_exit(98);
g_prot_addr = NULL;
} else if (mode == 3) {
if (mprotect(base, g_pgsz, PROT_NONE) != 0)
_exit(98);
g_prot_addr = NULL;
} else if (mode == 0) {
g_prot_addr = base;
} else {
g_prot_addr = base + g_pgsz;
}

stack_t ss = { .ss_sp = base, .ss_size = g_pgsz + off, .ss_flags = 0 };
if (sigaltstack(&ss, NULL) != 0)
_exit(97);

struct sigaction sa = { 0 };
sa.sa_handler = raw_handler;
sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
if (sigaction(SIGUSR1, &sa, NULL) != 0)
_exit(96);

/* Activate FPU, LASX, and LBT state */
__asm__ volatile("movgr2fr.d $f0, $zero\n"
"xvldi $xr0, 0\n"
"movgr2scr $scr0, $zero\n");

raise(SIGUSR1);
_exit(0);
}

int main(void)
{
setvbuf(stdout, NULL, _IONBF, 0);
size_t pgsz = (size_t)sysconf(_SC_PAGESIZE);
const char *names[4] = { "restore_lo", "restore_hi", "save_hi", "save_lo" };
printf("Page size: %zu bytes\n", pgsz);

for (int mode = 0; mode < 4; mode++) {
int n_ok = 0, n_segv = 0, n_other = 0, n_hang = 0;
printf("=== mode: %s ===\n", names[mode]);
for (size_t off = 16; off <= 2048; off += 16) {
pid_t pid = fork();
if (pid == 0)
run_one(mode, off);
int status = 0, waited = 0;
for (int i = 0; i < 50; i++) {
if (waitpid(pid, &status, WNOHANG) == pid) {
waited = 1;
break;
}
usleep(10000);
}
if (!waited) {
n_hang++;
printf("[%s] off=%4zu: HANG/LIVELOCK!\n", names[mode], off);
kill(pid, SIGKILL);
usleep(50000);
waitpid(pid, &status, WNOHANG);
} else if (WIFSIGNALED(status)) {
int s = WTERMSIG(status);
if (s == SIGSEGV)
n_segv++;
else {
n_other++;
printf("[%s] off=%4zu: sig=%d\n", names[mode], off, s);
}
} else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
n_other++;
printf("[%s] off=%4zu: exit=%d\n", names[mode], off,
WEXITSTATUS(status));
} else {
n_ok++;
}
}
printf("[%s] summary: ok=%d segv=%d other=%d hang=%d\n",
names[mode], n_ok, n_segv, n_other, n_hang);
}
printf("DONE\n");
return 0;
}

arch/loongarch/kernel/signal.c | 36 +++++++++++++++++++---------------
1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/arch/loongarch/kernel/signal.c b/arch/loongarch/kernel/signal.c
index 57231a4d580d..843607173935 100644
--- a/arch/loongarch/kernel/signal.c
+++ b/arch/loongarch/kernel/signal.c
@@ -373,6 +373,11 @@ static int protected_save_fpu_context(struct extctx_layout *extctx)
uint64_t __user *fcc = &fpu_ctx->fcc;
uint32_t __user *fcsr = &fpu_ctx->fcsr;

+ err |= __put_user(FPU_CTX_MAGIC, &info->magic);
+ err |= __put_user(extctx->fpu.size, &info->size);
+ if (err)
+ return err;
+
while (1) {
lock_fpu_owner();
if (is_fpu_owner())
@@ -380,10 +385,6 @@ static int protected_save_fpu_context(struct extctx_layout *extctx)
else
err = copy_fpu_to_sigcontext(fpu_ctx);
unlock_fpu_owner();
-
- err |= __put_user(FPU_CTX_MAGIC, &info->magic);
- err |= __put_user(extctx->fpu.size, &info->size);
-
if (likely(!err))
break;
/* Touch the FPU context and try again */
@@ -442,6 +443,11 @@ static int protected_save_lsx_context(struct extctx_layout *extctx)
uint64_t __user *fcc = &lsx_ctx->fcc;
uint32_t __user *fcsr = &lsx_ctx->fcsr;

+ err |= __put_user(LSX_CTX_MAGIC, &info->magic);
+ err |= __put_user(extctx->lsx.size, &info->size);
+ if (err)
+ return err;
+
while (1) {
lock_fpu_owner();
if (is_lsx_enabled())
@@ -452,10 +458,6 @@ static int protected_save_lsx_context(struct extctx_layout *extctx)
err = copy_lsx_to_sigcontext(lsx_ctx);
}
unlock_fpu_owner();
-
- err |= __put_user(LSX_CTX_MAGIC, &info->magic);
- err |= __put_user(extctx->lsx.size, &info->size);
-
if (likely(!err))
break;
/* Touch the LSX context and try again */
@@ -518,6 +520,11 @@ static int protected_save_lasx_context(struct extctx_layout *extctx)
uint64_t __user *fcc = &lasx_ctx->fcc;
uint32_t __user *fcsr = &lasx_ctx->fcsr;

+ err |= __put_user(LASX_CTX_MAGIC, &info->magic);
+ err |= __put_user(extctx->lasx.size, &info->size);
+ if (err)
+ return err;
+
while (1) {
lock_fpu_owner();
if (is_lasx_enabled())
@@ -530,10 +537,6 @@ static int protected_save_lasx_context(struct extctx_layout *extctx)
err = copy_lasx_to_sigcontext(lasx_ctx);
}
unlock_fpu_owner();
-
- err |= __put_user(LASX_CTX_MAGIC, &info->magic);
- err |= __put_user(extctx->lasx.size, &info->size);
-
if (likely(!err))
break;
/* Touch the LASX context and try again */
@@ -599,6 +602,11 @@ static int protected_save_lbt_context(struct extctx_layout *extctx)
uint64_t __user *regs = (uint64_t *)&lbt_ctx->regs;
uint32_t __user *ftop = &lbt_ctx->ftop;

+ err |= __put_user(LBT_CTX_MAGIC, &info->magic);
+ err |= __put_user(extctx->lbt.size, &info->size);
+ if (err)
+ return err;
+
while (1) {
lock_lbt_owner();
if (is_lbt_owner())
@@ -610,10 +618,6 @@ static int protected_save_lbt_context(struct extctx_layout *extctx)
else
err |= copy_ftop_to_sigcontext(lbt_ctx);
unlock_lbt_owner();
-
- err |= __put_user(LBT_CTX_MAGIC, &info->magic);
- err |= __put_user(extctx->lbt.size, &info->size);
-
if (likely(!err))
break;
/* Touch the LBT context and try again */
--
2.56.0