riscv: signal: fix the FP state restore in rt_sigreturn()
v3 cover riscv: signal: fix the FP state restore in rt_sigreturn()
If rt_sigreturn() is preempted or sleeps on a page fault while it copies the FP registers from the signal frame, the interrupted code resumes with the signal handler's FP registers. Patch 1 fixes this, patch 2 adds a selftest that reproduces it with userfaultfd.
Tested under QEMU with riscv64 defconfig plus CONFIG_USERFAULTFD: the new test, built with GCC 15 through the kselftest Makefile and with clang 21, fails 10 of 10 runs without patch 1 and passes 10 of 10 with it.
This series was prepared with the help of an AI tool.
Changes in v3:
- selftest: load the tgkill() arguments inside the asm statement. v2 bound them to local register variables, which GCC does not keep across the sigaltstack() call before the asm, so the signal was never sent and the test failed with or without patch 1 (Andy Chiu).
- Rebased onto v7.4-rc3.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Nickolai Zeldovich (2):
riscv: signal: mark the FP state clean before restoring it from the
frame
selftests: riscv: test FP state restore when rt_sigreturn() faults
arch/riscv/kernel/signal.c | 7 + .../selftests/riscv/sigreturn/.gitignore | 1 + .../selftests/riscv/sigreturn/Makefile | 5 +- .../selftests/riscv/sigreturn/fp_restore.c | 147 ++++++++++++++++++ 4 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/riscv/sigreturn/fp_restore.c
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.53.0
PATCH v3 1/2 riscv: signal: mark the FP state clean before restoring it from the frame
If rt_sigreturn() is preempted or sleeps on a page fault while it copies the FP registers from the signal frame, the interrupted code resumes with the signal handler's FP registers instead of its own.
restore_fp_state() copies the frame into current->thread.fstate and then loads it with fstate_restore(). If the handler used the FPU, FS is still Dirty during the copy, so a context switch makes fstate_save() store the live registers, the handler's, over the partly copied image.
Mark the FP state clean before the copy, as commit c27fa53b858b ("riscv:
Fix vector state restore in rt_sigreturn()") did for the vector state. Preemption is disabled around it because the scheduler also writes regs->status.
--- arch/riscv/kernel/signal.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/riscv/kernel/signal.c +7 -0
@@ -47,6 +47,13 @@ static long restore_fp_state(struct pt_regs *regs,long err;struct __riscv_d_ext_state __user *state = &sc_fpregs->d; +/* Discard the handler's FP registers before the copy can sleep */ +if ((regs->status &SR_FS ) !=SR_FS_OFF ) { +preempt_disable (); +__fstate_clean (regs); +preempt_enable (); + } + err =__copy_from_user (¤t->thread.fstate, state,sizeof (*state));if (unlikely (err))return err;
Signature · 1 line
2.53.0
PATCH v3 2/2 selftests: riscv: test FP state restore when rt_sigreturn() faults
In reply to Nickolai ZeldovichAdd a test for the previous patch: the signal frame sits on a userfaultfd page that the handler drops after dirtying the FP registers, so rt_sigreturn() sleeps in the middle of copying the FP registers from the frame. The interrupted code must get its own registers back.
The test is skipped without the D extension or when userfaultfd is not available to the caller.
--- .../selftests/riscv/sigreturn/.gitignore | 1 + .../selftests/riscv/sigreturn/Makefile | 5 +- .../selftests/riscv/sigreturn/fp_restore.c | 147 ++++++++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/riscv/sigreturn/fp_restore.c
tools/testing/selftests/riscv/sigreturn/.gitignore +1 -0
@@ -1 +1,2 @@ +fp_restore sigreturn
tools/testing/selftests/riscv/sigreturn/Makefile +4 -1
@@ -4,9 +4,12 @@ CFLAGS += -I$(top_srcdir)/tools/include -TEST_GEN_PROGS := sigreturn +TEST_GEN_PROGS := sigreturn fp_restore include ../../lib.mk $(OUTPUT)/sigreturn: sigreturn.c $(CC) -static -o $@ $(CFLAGS) $(LDFLAGS) $^ + +$(OUTPUT)/fp_restore: fp_restore.c + $(CC) -static -pthread -o $@ $(CFLAGS) $(LDFLAGS) $^
tools/testing/selftests/riscv/sigreturn/fp_restore.c new file +147 -0
147 lines not on this page · raw message
Signature · 1 line
2.53.0