d31626f70b
Currently, when we have a process using the transactional memory facilities on POWER8 (that is, the processor is in transactional or suspended state), and the process enters the kernel and the kernel then uses the floating-point or vector (VMX/Altivec) facility, we end up corrupting the user-visible FP/VMX/VSX state. This happens, for example, if a page fault causes a copy-on-write operation, because the copy_page function will use VMX to do the copy on POWER8. The test program below demonstrates the bug. The bug happens because when FP/VMX state for a transactional process is stored in the thread_struct, we store the checkpointed state in .fp_state/.vr_state and the transactional (current) state in .transact_fp/.transact_vr. However, when the kernel wants to use FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(), which saves the current state in .fp_state/.vr_state. Furthermore, when we return to the user process we return with FP/VMX/VSX disabled. The next time the process uses FP/VMX/VSX, we don't know which set of state (the current register values, .fp_state/.vr_state, or .transact_fp/.transact_vr) we should be using, since we have no way to tell if we are still in the same transaction, and if not, whether the previous transaction succeeded or failed. Thus it is necessary to strictly adhere to the rule that if FP has been enabled at any point in a transaction, we must keep FP enabled for the user process with the current transactional state in the FP registers, until we detect that it is no longer in a transaction. Similarly for VMX; once enabled it must stay enabled until the process is no longer transactional. In order to keep this rule, we add a new thread_info flag which we test when returning from the kernel to userspace, called TIF_RESTORE_TM. This flag indicates that there is FP/VMX/VSX state to be restored before entering userspace, and when it is set the .tm_orig_msr field in the thread_struct indicates what state needs to be restored. The restoration is done by restore_tm_state(). The TIF_RESTORE_TM bit is set by new giveup_fpu/altivec_maybe_transactional helpers, which are called from enable_kernel_fp/altivec, giveup_vsx, and flush_fp/altivec_to_thread instead of giveup_fpu/altivec. The other thing to be done is to get the transactional FP/VMX/VSX state from .fp_state/.vr_state when doing reclaim, if that state has been saved there by giveup_fpu/altivec_maybe_transactional. Having done this, we set the FP/VMX bit in the thread's MSR after reclaim to indicate that that part of the state is now valid (having been reclaimed from the processor's checkpointed state). Finally, in the signal handling code, we move the clearing of the transactional state bits in the thread's MSR a bit earlier, before calling flush_fp_to_thread(), so that we don't unnecessarily set the TIF_RESTORE_TM bit. This is the test program: /* Michael Neuling 4/12/2013 * * See if the altivec state is leaked out of an aborted transaction due to * kernel vmx copy loops. * * gcc -m64 htm_vmxcopy.c -o htm_vmxcopy * */ /* We don't use all of these, but for reference: */ int main(int argc, char *argv[]) { long double vecin = 1.3; long double vecout; unsigned long pgsize = getpagesize(); int i; int fd; int size = pgsize*16; char tmpfile[] = "/tmp/page_faultXXXXXX"; char buf[pgsize]; char *a; uint64_t aborted = 0; fd = mkstemp(tmpfile); assert(fd >= 0); memset(buf, 0, pgsize); for (i = 0; i < size; i += pgsize) assert(write(fd, buf, pgsize) == pgsize); unlink(tmpfile); a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); assert(a != MAP_FAILED); asm __volatile__( "lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value TBEGIN "beq 3f ;" TSUSPEND "xxlxor 40,40,40 ; " // set 40 to 0 "std 5, 0(%[map]) ;" // cause kernel vmx copy page TABORT TRESUME TEND "li %[res], 0 ;" "b 5f ;" "3: ;" // Abort handler "li %[res], 1 ;" "5: ;" "stxvd2x 40,0,%[vecoutptr] ; " : [res]"=r"(aborted) : [vecinptr]"r"(&vecin), [vecoutptr]"r"(&vecout), [map]"r"(a) : "memory", "r0", "r3", "r4", "r5", "r6", "r7"); if (aborted && (vecin != vecout)){ printf("FAILED: vector state leaked on abort %f != %f\n", (double)vecin, (double)vecout); exit(1); } munmap(a, size); close(fd); printf("PASSED!\n"); return 0; } Signed-off-by: Paul Mackerras <paulus@samba.org> Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org> |
||
---|---|---|
.. | ||
vdso32 | ||
vdso64 | ||
.gitignore | ||
align.c | ||
asm-offsets.c | ||
audit.c | ||
btext.c | ||
cacheinfo.c | ||
cacheinfo.h | ||
clock.c | ||
compat_audit.c | ||
cpu_setup_6xx.S | ||
cpu_setup_44x.S | ||
cpu_setup_a2.S | ||
cpu_setup_fsl_booke.S | ||
cpu_setup_pa6t.S | ||
cpu_setup_power.S | ||
cpu_setup_ppc970.S | ||
cputable.c | ||
crash.c | ||
crash_dump.c | ||
dbell.c | ||
dma-iommu.c | ||
dma-swiotlb.c | ||
dma.c | ||
eeh.c | ||
eeh_cache.c | ||
eeh_dev.c | ||
eeh_driver.c | ||
eeh_event.c | ||
eeh_pe.c | ||
eeh_sysfs.c | ||
entry_32.S | ||
entry_64.S | ||
epapr_hcalls.S | ||
epapr_paravirt.c | ||
exceptions-64e.S | ||
exceptions-64s.S | ||
fadump.c | ||
firmware.c | ||
fpu.S | ||
fsl_booke_entry_mapping.S | ||
ftrace.c | ||
head_8xx.S | ||
head_32.S | ||
head_40x.S | ||
head_44x.S | ||
head_64.S | ||
head_booke.h | ||
head_fsl_booke.S | ||
hw_breakpoint.c | ||
ibmebus.c | ||
idle.c | ||
idle_6xx.S | ||
idle_book3e.S | ||
idle_e500.S | ||
idle_power4.S | ||
idle_power7.S | ||
io-workarounds.c | ||
io.c | ||
iomap.c | ||
iommu.c | ||
irq.c | ||
isa-bridge.c | ||
jump_label.c | ||
kgdb.c | ||
kprobes.c | ||
kvm.c | ||
kvm_emul.S | ||
l2cr_6xx.S | ||
legacy_serial.c | ||
machine_kexec.c | ||
machine_kexec_32.c | ||
machine_kexec_64.c | ||
Makefile | ||
mce.c | ||
mce_power.c | ||
misc.S | ||
misc_32.S | ||
misc_64.S | ||
module.c | ||
module_32.c | ||
module_64.c | ||
msi.c | ||
nvram_64.c | ||
of_platform.c | ||
paca.c | ||
pci-common.c | ||
pci-hotplug.c | ||
pci_32.c | ||
pci_64.c | ||
pci_dn.c | ||
pci_of_scan.c | ||
pmc.c | ||
ppc32.h | ||
ppc_ksyms.c | ||
ppc_save_regs.S | ||
proc_powerpc.c | ||
process.c | ||
prom.c | ||
prom_init.c | ||
prom_init_check.sh | ||
prom_parse.c | ||
ptrace.c | ||
ptrace32.c | ||
reloc_32.S | ||
reloc_64.S | ||
rtas-proc.c | ||
rtas-rtc.c | ||
rtas.c | ||
rtas_flash.c | ||
rtas_pci.c | ||
rtasd.c | ||
setup-common.c | ||
setup_32.c | ||
setup_64.c | ||
signal.c | ||
signal.h | ||
signal_32.c | ||
signal_64.c | ||
smp-tbsync.c | ||
smp.c | ||
stacktrace.c | ||
suspend.c | ||
swsusp.c | ||
swsusp_32.S | ||
swsusp_64.c | ||
swsusp_asm64.S | ||
swsusp_booke.S | ||
sys_ppc32.c | ||
syscalls.c | ||
sysfs.c | ||
systbl.S | ||
systbl_chk.c | ||
systbl_chk.sh | ||
tau_6xx.c | ||
time.c | ||
tm.S | ||
traps.c | ||
udbg.c | ||
udbg_16550.c | ||
uprobes.c | ||
vdso.c | ||
vecemu.c | ||
vector.S | ||
vio.c | ||
vmlinux.lds.S |