acrn-kernel/arch/powerpc/kernel
Timothy Pearson c23b9eaca8 powerpc: Don't clobber f0/vs0 during fp|altivec register save
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 8792468da5 ("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: 8792468da5 ("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>
2023-12-08 08:51:15 +01:00
..
ptrace powerpc: Don't try to copy PPR for task with NULL pt_regs 2023-04-06 12:10:53 +02:00
syscalls
trace powerpc/ftrace: Create a dummy stackframe to fix stack unwind 2023-08-11 12:08:21 +02:00
vdso
.gitignore
85xx_entry_mapping.S
Makefile
align.c
asm-offsets.c
audit.c
btext.c
cacheinfo.c
cacheinfo.h
compat_audit.c
cpu_setup_6xx.S
cpu_setup_44x.S
cpu_setup_e500.S
cpu_setup_pa6t.S
cpu_setup_power.c
cpu_setup_ppc970.S
cpu_specs.h
cpu_specs_8xx.h
cpu_specs_40x.h
cpu_specs_44x.h
cpu_specs_47x.h
cpu_specs_85xx.h
cpu_specs_book3s_32.h
cpu_specs_book3s_64.h
cpu_specs_e500mc.h
cputable.c
crash_dump.c
dawr.c
dbell.c powerpc/64: Fix perf profiling asynchronous interrupt handlers 2023-02-22 12:59:43 +01:00
dma-iommu.c powerpc/iommu: Incorrect DDW Table is referenced for SR-IOV device 2023-05-24 17:32:52 +01:00
dma-mask.c
dma-swiotlb.c
dt_cpu_ftrs.c
early_32.c
eeh.c
eeh_cache.c
eeh_driver.c
eeh_event.c
eeh_pe.c
eeh_sysfs.c
entry_32.S powerpc/47x: Fix 47x syscall return crash 2023-10-19 23:08:56 +02:00
entry_64.S
epapr_hcalls.S
epapr_paravirt.c
exceptions-64e.S
exceptions-64s.S
fadump.c powerpc/fadump: reset dump area size if fadump memory reserve fails 2023-09-13 09:42:47 +02:00
firmware.c
fpu.S powerpc: Don't clobber f0/vs0 during fp|altivec register save 2023-12-08 08:51:15 +01:00
head_8xx.S
head_32.h
head_40x.S
head_44x.S
head_64.S
head_85xx.S powerpc/85xx: Fix math emulation exception 2023-11-08 14:10:58 +01:00
head_book3s_32.S
head_booke.h
hw_breakpoint.c powerpc/watchpoints: Annotate atomic context in more places 2023-10-06 14:56:57 +02:00
hw_breakpoint_constraints.c powerpc/watchpoint: Disable pagefaults when getting user instruction 2023-10-06 14:56:57 +02:00
idle.c
idle_6xx.S
idle_64e.S
idle_85xx.S
idle_book3s.S
ima_arch.c
interrupt.c powerpc/interrupt: Don't read MSR from interrupt_exit_kernel_prepare() 2023-07-19 16:21:37 +02:00
interrupt_64.S
io-workarounds.c
io.c
iomap.c
iommu.c powerpc/iommu: Fix notifiers being shared by PCI and VIO buses 2023-09-13 09:42:48 +02:00
irq.c powerpc/64: Fix perf profiling asynchronous interrupt handlers 2023-02-22 12:59:43 +01:00
irq_64.c powerpc/64: Don't recurse irq replay 2023-03-17 08:50:30 +01:00
isa-bridge.c
jump_label.c
kdebugfs.c
kgdb.c
kprobes-ftrace.c
kprobes.c
kvm.c
kvm_emul.S
l2cr_6xx.S
legacy_serial.c
mce.c
mce_power.c
misc.S
misc_32.S
misc_64.S
module.c
module_32.c
module_64.c
msi.c
note.S
nvram_64.c
of_platform.c
optprobes.c
optprobes_head.S
paca.c
pci-common.c
pci-hotplug.c
pci_32.c
pci_64.c
pci_dn.c
pci_of_scan.c
pmc.c
ppc_save_regs.S powerpc: update ppc_save_regs to save current r1 in pt_regs 2023-07-19 16:21:39 +02:00
proc_powerpc.c
process.c KVM: PPC: Book3S HV: Fix KVM_RUN clobbering FP/VEC user registers 2023-12-08 08:51:15 +01:00
prom.c powerpc/64: Move paca allocation to early_setup() 2023-03-17 08:50:31 +01:00
prom_init.c
prom_init_check.sh
prom_parse.c
reloc_32.S
reloc_64.S
rtas-proc.c
rtas-rtc.c
rtas.c powerpc/rtas: use memmove for potentially overlapping buffer copy 2023-05-11 23:03:31 +09:00
rtas_entry.S
rtas_flash.c powerpc/rtas_flash: allow user copy to flash block cache objects 2023-08-23 17:52:30 +02:00
rtas_pci.c
rtasd.c
secure_boot.c
security.c powerpc/security: Fix Speculation_Store_Bypass reporting on Power10 2023-07-23 13:49:32 +02:00
secvar-ops.c
secvar-sysfs.c
setup-common.c powerpc/mm: Fix boot crash with FLATMEM 2023-11-08 14:11:02 +01:00
setup.h
setup_32.c
setup_64.c powerpc/64: Move paca allocation to early_setup() 2023-03-17 08:50:31 +01:00
signal.c
signal.h
signal_32.c powerpc/signal32: Force inlining of __unsafe_save_user_regs() and save_tm_user_regs_unsafe() 2023-07-19 16:21:37 +02:00
signal_64.c
smp-tbsync.c
smp.c
stacktrace.c
static_call.c
suspend.c
swsusp.c
swsusp_32.S
swsusp_64.c
swsusp_85xx.S
swsusp_asm64.S
sys_ppc32.c
syscall.c
syscalls.c
sysfs.c
systbl.c
tau_6xx.c
time.c powerpc/kcsan: Exclude udelay to prevent recursive instrumentation 2023-03-17 08:50:31 +01:00
tm.S
traps.c powerpc: Only define __parse_fpscr() when required 2023-11-20 11:52:12 +01:00
ucall.S
udbg.c
udbg_16550.c
uprobes.c
vdso.c
vdso32_wrapper.S
vdso64_wrapper.S
vecemu.c
vector.S powerpc: Don't clobber f0/vs0 during fp|altivec register save 2023-12-08 08:51:15 +01:00
vmlinux.lds.S powerpc/vmlinux.lds: Don't discard .rela* for relocatable builds 2023-02-25 11:25:42 +01:00
watchdog.c