]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
powerpc: Don't clobber f0/vs0 during fp|altivec register save
authorTimothy Pearson <tpearson@raptorengineering.com>
Sun, 19 Nov 2023 15:18:02 +0000 (09:18 -0600)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 8 Dec 2023 07:42:00 +0000 (08:42 +0100)
commit 5e1d824f9a283cbf90f25241b66d1f69adb3835b upstream.

During floating point and vector save to thread data f0/vs0 are
clobbered by the FPSCR/VSCR store routine. This has been obvserved to
lead to userspace register corruption and application data corruption
with io-uring.

Fix it by restoring f0/vs0 after FPSCR/VSCR store has completed for
all the FP, altivec, VMX register save paths.

Tested under QEMU in kvm mode, running on a Talos II workstation with
dual POWER9 DD2.2 CPUs.

Additional detail (mpe):

Typically save_fpu() is called from __giveup_fpu() which saves the FP
regs and also *turns off FP* in the tasks MSR, meaning the kernel will
reload the FP regs from the thread struct before letting the task use FP
again. So in that case save_fpu() is free to clobber f0 because the FP
regs no longer hold live values for the task.

There is another case though, which is the path via:
  sys_clone()
    ...
    copy_process()
      dup_task_struct()
        arch_dup_task_struct()
          flush_all_to_thread()
            save_all()

That path saves the FP regs but leaves them live. That's meant as an
optimisation for a process that's using FP/VSX and then calls fork(),
leaving the regs live means the parent process doesn't have to take a
fault after the fork to get its FP regs back. The optimisation was added
in commit 8792468da5e1 ("powerpc: Add the ability to save FPU without
giving it up").

That path does clobber f0, but f0 is volatile across function calls,
and typically programs reach copy_process() from userspace via a syscall
wrapper function. So in normal usage f0 being clobbered across a
syscall doesn't cause visible data corruption.

But there is now a new path, because io-uring can call copy_process()
via create_io_thread() from the signal handling path. That's OK if the
signal is handled as part of syscall return, but it's not OK if the
signal is handled due to some other interrupt.

That path is:

interrupt_return_srr_user()
  interrupt_exit_user_prepare()
    interrupt_exit_user_prepare_main()
      do_notify_resume()
        get_signal()
          task_work_run()
            create_worker_cb()
              create_io_worker()
                copy_process()
                  dup_task_struct()
                    arch_dup_task_struct()
                      flush_all_to_thread()
                        save_all()
                          if (tsk->thread.regs->msr & MSR_FP)
                            save_fpu()
                            # f0 is clobbered and potentially live in userspace

Note the above discussion applies equally to save_altivec().

Fixes: 8792468da5e1 ("powerpc: Add the ability to save FPU without giving it up")
Cc: stable@vger.kernel.org # v4.6+
Closes: https://lore.kernel.org/all/480932026.45576726.1699374859845.JavaMail.zimbra@raptorengineeringinc.com/
Closes: https://lore.kernel.org/linuxppc-dev/480221078.47953493.1700206777956.JavaMail.zimbra@raptorengineeringinc.com/
Tested-by: Timothy Pearson <tpearson@raptorengineering.com>
Tested-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Timothy Pearson <tpearson@raptorengineering.com>
[mpe: Reword change log to describe exact path of corruption & other minor tweaks]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://msgid.link/1921539696.48534988.1700407082933.JavaMail.zimbra@raptorengineeringinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/powerpc/kernel/fpu.S
arch/powerpc/kernel/vector.S

index 6c509f39bbdeb97c7c18a28e4479792a1d0fb7bf..6363b35ed39e2b313661719dd4c6c93da271ca14 100644 (file)
 #include <asm/export.h>
 
 #ifdef CONFIG_VSX
+#define __REST_1FPVSR(n,c,base)                                                \
+BEGIN_FTR_SECTION                                                      \
+       b       2f;                                                     \
+END_FTR_SECTION_IFSET(CPU_FTR_VSX);                                    \
+       REST_FPR(n,base);                                               \
+       b       3f;                                                     \
+2:     REST_VSR(n,c,base);                                             \
+3:
+
 #define __REST_32FPVSRS(n,c,base)                                      \
 BEGIN_FTR_SECTION                                                      \
        b       2f;                                                     \
@@ -45,9 +54,11 @@ END_FTR_SECTION_IFSET(CPU_FTR_VSX);                                  \
 2:     SAVE_32VSRS(n,c,base);                                          \
 3:
 #else
+#define __REST_1FPVSR(n,b,base)                REST_FPR(n, base)
 #define __REST_32FPVSRS(n,b,base)      REST_32FPRS(n, base)
 #define __SAVE_32FPVSRS(n,b,base)      SAVE_32FPRS(n, base)
 #endif
+#define REST_1FPVSR(n,c,base)   __REST_1FPVSR(n,__REG_##c,__REG_##base)
 #define REST_32FPVSRS(n,c,base) __REST_32FPVSRS(n,__REG_##c,__REG_##base)
 #define SAVE_32FPVSRS(n,c,base) __SAVE_32FPVSRS(n,__REG_##c,__REG_##base)
 
@@ -70,6 +81,7 @@ _GLOBAL(store_fp_state)
        SAVE_32FPVSRS(0, R4, R3)
        mffs    fr0
        stfd    fr0,FPSTATE_FPSCR(r3)
+       REST_1FPVSR(0, R4, R3)
        blr
 EXPORT_SYMBOL(store_fp_state)
 
@@ -134,6 +146,7 @@ _GLOBAL(save_fpu)
 2:     SAVE_32FPVSRS(0, R4, R6)
        mffs    fr0
        stfd    fr0,FPSTATE_FPSCR(r6)
+       REST_1FPVSR(0, R4, R6)
        blr
 
 /*
index f314fd475491c35437da4ae392aed93ca7274d7f..80729cfb3306067ed513f240c23ae10e84c3e0bd 100644 (file)
@@ -30,6 +30,7 @@ _GLOBAL(store_vr_state)
        mfvscr  v0
        li      r4, VRSTATE_VSCR
        stvx    v0, r4, r3
+       lvx     v0, 0, r3
        blr
 EXPORT_SYMBOL(store_vr_state)
 
@@ -100,6 +101,7 @@ _GLOBAL(save_altivec)
        mfvscr  v0
        li      r4,VRSTATE_VSCR
        stvx    v0,r4,r7
+       lvx     v0,0,r7
        blr
 
 #ifdef CONFIG_VSX