powerpc/tm: Various transactional memory fixes
Subsystems (2)
cover powerpc/tm: Various transactional memory fixes
Bunch of fixes for transactional memory.
Michael Neuling (5):
powerpc/tm: Make room for hypervisor in abort cause codes powerpc/tm: Update cause codes documentation powerpc/tm: Abort on emulation and alignment faults powerpc/tm: Move TM abort cause codes to uapi powerpc/tm: Fix userspace stack corruption on signal delivery for
active transactions
Documentation/powerpc/transactional_memory.txt | 27 ++++++++++++++-- arch/powerpc/include/asm/processor.h | 13 +++----- arch/powerpc/include/asm/reg.h | 11 ------- arch/powerpc/include/asm/signal.h | 3 ++ arch/powerpc/include/asm/tm.h | 2 ++ arch/powerpc/include/uapi/asm/tm.h | 18 +++++++++++ arch/powerpc/kernel/signal.c | 40 ++++++++++++++++++++++-- arch/powerpc/kernel/signal.h | 2 +- arch/powerpc/kernel/signal_32.c | 10 ++---- arch/powerpc/kernel/signal_64.c | 23 +++++--------- arch/powerpc/kernel/traps.c | 29 +++++++++++++++++ 11 files changed, 129 insertions(+), 49 deletions(-) create mode 100644 arch/powerpc/include/uapi/asm/tm.h
Signature · 1 line
1.7.10.4
PATCH 1/5 powerpc/tm: Make room for hypervisor in abort cause codes
PAPR carves out 0xff-0xe0 for hypervisor use of transactional memory software abort cause codes. Unfortunately we don't respect this currently.
Below fixes this to move our cause codes to below this region.
--- arch/powerpc/include/asm/reg.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
arch/powerpc/include/asm/reg.h +8 -7
@@ -113,14 +113,15 @@/* Reason codes describing kernel causes for transaction aborts. By convention, bit0 is copied to TEXASR[56] (IBM bit 7) which is set if -the failure is persistent. +the failure is persistent. PAPR saves 0xff-0xe0 for the hypervisor. */ -#define TM_CAUSE_RESCHED 0xfe -#define TM_CAUSE_TLBI 0xfc -#define TM_CAUSE_FAC_UNAV 0xfa -#define TM_CAUSE_SYSCALL 0xf9/* Persistent */ -#define TM_CAUSE_MISC 0xf6 -#define TM_CAUSE_SIGNAL 0xf4 +#define TM_CAUSE_PERSISTENT 0x01 +#define TM_CAUSE_RESCHED 0xde +#define TM_CAUSE_TLBI 0xdc +#define TM_CAUSE_FAC_UNAV 0xda +#define TM_CAUSE_SYSCALL 0xd8/* future use */ +#define TM_CAUSE_MISC 0xd6/* future use */ +#define TM_CAUSE_SIGNAL 0xd4#if defined(CONFIG_PPC_BOOK3S_64 )#define MSR_64BIT MSR_SF
Signature · 1 line
1.7.10.4
PATCH 2/5 powerpc/tm: Update cause codes documentation
In reply to Michael Neuling--- Documentation/powerpc/transactional_memory.txt | 1 + 1 file changed, 1 insertion(+)
Documentation/powerpc/transactional_memory.txt +1 -0
@@ -155,6 +155,7 @@ These are defined in <asm/reg.h>, and distinguish different reasons why the kernel aborted a transaction: TM_CAUSE_RESCHED Thread was rescheduled. + TM_CAUSE_TLBI Software TLB invalide. TM_CAUSE_FAC_UNAV FP/VEC/VSX unavailable trap. TM_CAUSE_SYSCALL Currently unused; future syscalls that must abort transactions for consistency will use this.
Signature · 1 line
1.7.10.4
PATCH 3/5 powerpc/tm: Abort on emulation and alignment faults
In reply to Michael NeulingIf we are emulating an instruction inside an active user transaction that touches memory, the kernel can't emulate it as it operates in transactional suspend context. We need to abort these transactions and send them back to userspace for the hardware to rollback.
We can service these if the user transaction is in suspend mode, since the kernel will operate in the same suspend context.
This adds a check to all alignment faults and to specific instruction emulations (only string instructions for now). If the user process is in an active (non-suspended) transaction, we abort the transaction go back to userspace allowing the HW to roll back the transaction and tell the user of the failure. This also adds new tm abort cause codes to report the reason of the persistent error to the user.
Crappy test case here http://neuling.org/devel/junkcode/aligntm.c
--- Documentation/powerpc/transactional_memory.txt | 7 ++++-- arch/powerpc/include/asm/reg.h | 2 ++ arch/powerpc/kernel/traps.c | 29 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-)
Documentation/powerpc/transactional_memory.txt +5 -2
@@ -161,9 +161,12 @@ kernel aborted a transaction: transactions for consistency will use this. TM_CAUSE_SIGNAL Signal delivered. TM_CAUSE_MISC Currently unused. + TM_CAUSE_ALIGNMENT Alignment fault. + TM_CAUSE_EMULATE Emulation that touched memory. -These can be checked by the user program's abort handler as TEXASR[0:7]. - +These can be checked by the user program's abort handler as TEXASR[0:7]. If +bit 7 is set, it indicates that the error is consider persistent. For example +a TM_CAUSE_ALIGNMENT will be persistent while a TM_CAUSE_RESCHED will not.q GDB ===
arch/powerpc/include/asm/reg.h +2 -0
@@ -122,6 +122,8 @@#define TM_CAUSE_SYSCALL 0xd8/* future use */ #define TM_CAUSE_MISC 0xd6/* future use */ #define TM_CAUSE_SIGNAL 0xd4 +#define TM_CAUSE_ALIGNMENT 0xd2 +#define TM_CAUSE_EMULATE 0xd0#if defined(CONFIG_PPC_BOOK3S_64 )#define MSR_64BIT MSR_SF
arch/powerpc/kernel/traps.c +29 -0
@@ -53,6 +53,7 @@#ifdef CONFIG_PPC64 #include <asm/firmware.h> #include <asm/processor.h> +#include <asm/tm.h> #endif #include <asm/kexec.h> #include <asm/ppc-opcode.h>
@@ -932,6 +933,28 @@ static int emulate_isel(struct pt_regs *regs, u32 instword)return 0 ; } +#ifdef CONFIG_PPC_TRANSACTIONAL_MEM +static inline bool tm_abort_check (struct pt_regs *regs,int cause) +{ +/* If we're emulating a load/store in an active transaction, we cannot +* emulate it as the kernel operates in transaction suspended context. +* We need to abort the transaction. This creates a persistent TM +* abort so tell the user what caused it with a new code. +*/ +if (MSR_TM_TRANSACTIONAL (regs->msr)) { +tm_enable (); +tm_abort (cause); +return true; + } +return false; +} +#else +static inline bool tm_abort_check (struct pt_regs *regs,int reason) +{ +return false; +} +#endif +static int emulate_instruction (struct pt_regs *regs) {u32 instword;
@@ -971,6 +994,9 @@ static int emulate_instruction(struct pt_regs *regs)/* Emulate load/store string insn. */ if ((instword &PPC_INST_STRING_GEN_MASK ) ==PPC_INST_STRING ) { +if (tm_abort_check (regs, +TM_CAUSE_EMULATE |TM_CAUSE_PERSISTENT )) +return -EINVAL ;PPC_WARN_EMULATED (string, regs);return emulate_string_inst (regs, instword); }
@@ -1148,6 +1174,9 @@ void alignment_exception(struct pt_regs *regs)if (!arch_irq_disabled_regs (regs))local_irq_enable (); +if (tm_abort_check (regs,TM_CAUSE_ALIGNMENT |TM_CAUSE_PERSISTENT )) + goto bail; +/* we don't implement logging of alignment exceptions */ if (!(current->thread.align_ctl &PR_UNALIGN_SIGBUS )) fixed =fix_alignment (regs);
Signature · 1 line
1.7.10.4
PATCH 4/5 powerpc/tm: Move TM abort cause codes to uapi
In reply to Michael NeulingThese cause codes are usable by userspace, so let's export to uapi.
--- arch/powerpc/include/asm/reg.h | 14 -------------- arch/powerpc/include/asm/tm.h | 2 ++ arch/powerpc/include/uapi/asm/tm.h | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 arch/powerpc/include/uapi/asm/tm.h
arch/powerpc/include/asm/reg.h +0 -14
@@ -111,20 +111,6 @@#define MSR_TM_TRANSACTIONAL (x) (((x) & MSR_TS_MASK) == MSR_TS_T)#define MSR_TM_SUSPENDED (x) (((x) & MSR_TS_MASK) == MSR_TS_S) -/* Reason codes describing kernel causes for transaction aborts. By -convention, bit0 is copied to TEXASR[56] (IBM bit 7) which is set if -the failure is persistent. PAPR saves 0xff-0xe0 for the hypervisor. -*/ -#define TM_CAUSE_PERSISTENT 0x01 -#define TM_CAUSE_RESCHED 0xde -#define TM_CAUSE_TLBI 0xdc -#define TM_CAUSE_FAC_UNAV 0xda -#define TM_CAUSE_SYSCALL 0xd8/* future use */ -#define TM_CAUSE_MISC 0xd6/* future use */ -#define TM_CAUSE_SIGNAL 0xd4 -#define TM_CAUSE_ALIGNMENT 0xd2 -#define TM_CAUSE_EMULATE 0xd0 -#if defined(CONFIG_PPC_BOOK3S_64 )#define MSR_64BIT MSR_SF
arch/powerpc/include/asm/tm.h +2 -0
@@ -5,6 +5,8 @@* Copyright 2012 Matt Evans & Michael Neuling, IBM Corporation. */ +#include <uapi/asm/tm.h> +#ifdef CONFIG_PPC_TRANSACTIONAL_MEM extern void do_load_up_transact_fpu (struct thread_struct *thread);extern void do_load_up_transact_altivec (struct thread_struct *thread);
arch/powerpc/include/uapi/asm/tm.h new file +18 -0
@@ -0,0 +1,18 @@ +#ifndef _ASM_POWERPC_TM_H +#define _ASM_POWERPC_TM_H + +/* Reason codes describing kernel causes for transaction aborts. By +* convention, bit0 is copied to TEXASR[56] (IBM bit 7) which is set if +* the failure is persistent. PAPR saves 0xff-0xe0 for the hypervisor. +*/ +#define TM_CAUSE_PERSISTENT 0x01 +#define TM_CAUSE_RESCHED 0xde +#define TM_CAUSE_TLBI 0xdc +#define TM_CAUSE_FAC_UNAV 0xda +#define TM_CAUSE_SYSCALL 0xd8/* future use */ +#define TM_CAUSE_MISC 0xd6/* future use */ +#define TM_CAUSE_SIGNAL 0xd4 +#define TM_CAUSE_ALIGNMENT 0xd2 +#define TM_CAUSE_EMULATE 0xd0 + +#endif
Signature · 1 line
1.7.10.4
PATCH v2 4/5 powerpc/tm: Move TM abort cause codes to uapi
These cause codes are usable by userspace, so let's export to uapi.
--
v2:
add uapi/tm.h to Kbuild so it gets exported correctly
arch/powerpc/include/asm/reg.h +0 -14
@@ -111,20 +111,6 @@#define MSR_TM_TRANSACTIONAL (x) (((x) & MSR_TS_MASK) == MSR_TS_T)#define MSR_TM_SUSPENDED (x) (((x) & MSR_TS_MASK) == MSR_TS_S) -/* Reason codes describing kernel causes for transaction aborts. By -convention, bit0 is copied to TEXASR[56] (IBM bit 7) which is set if -the failure is persistent. PAPR saves 0xff-0xe0 for the hypervisor. -*/ -#define TM_CAUSE_PERSISTENT 0x01 -#define TM_CAUSE_RESCHED 0xde -#define TM_CAUSE_TLBI 0xdc -#define TM_CAUSE_FAC_UNAV 0xda -#define TM_CAUSE_SYSCALL 0xd8/* future use */ -#define TM_CAUSE_MISC 0xd6/* future use */ -#define TM_CAUSE_SIGNAL 0xd4 -#define TM_CAUSE_ALIGNMENT 0xd2 -#define TM_CAUSE_EMULATE 0xd0 -#if defined(CONFIG_PPC_BOOK3S_64 )#define MSR_64BIT MSR_SF
arch/powerpc/include/asm/tm.h +2 -0
@@ -5,6 +5,8 @@* Copyright 2012 Matt Evans & Michael Neuling, IBM Corporation. */ +#include <uapi/asm/tm.h> +#ifdef CONFIG_PPC_TRANSACTIONAL_MEM extern void do_load_up_transact_fpu (struct thread_struct *thread);extern void do_load_up_transact_altivec (struct thread_struct *thread);
arch/powerpc/include/uapi/asm/Kbuild +1 -0
@@ -40,6 +40,7 @@ header-y += statfs.h header-y += swab.h header-y += termbits.h header-y += termios.h +header-y += tm.h header-y += types.h header-y += ucontext.h header-y += unistd.h
arch/powerpc/include/uapi/asm/tm.h new file +18 -0
@@ -0,0 +1,18 @@ +#ifndef _ASM_POWERPC_TM_H +#define _ASM_POWERPC_TM_H + +/* Reason codes describing kernel causes for transaction aborts. By +* convention, bit0 is copied to TEXASR[56] (IBM bit 7) which is set if +* the failure is persistent. PAPR saves 0xff-0xe0 for the hypervisor. +*/ +#define TM_CAUSE_PERSISTENT 0x01 +#define TM_CAUSE_RESCHED 0xde +#define TM_CAUSE_TLBI 0xdc +#define TM_CAUSE_FAC_UNAV 0xda +#define TM_CAUSE_SYSCALL 0xd8/* future use */ +#define TM_CAUSE_MISC 0xd6/* future use */ +#define TM_CAUSE_SIGNAL 0xd4 +#define TM_CAUSE_ALIGNMENT 0xd2 +#define TM_CAUSE_EMULATE 0xd0 + +#endif
PATCH 5/5 powerpc/tm: Fix userspace stack corruption on signal delivery for active transactions
In reply to Michael NeulingWhen in an active transaction that takes a signal, we need to be careful with the stack. It's possible that the stack has moved back up after the tbegin. The obvious case here is when the tbegin is called inside a function that returns before a tend. In this case, the stack is part of the checkpointed transactional memory state. If we write over this non transactionally or in suspend, we are in trouble because if we get a tm abort, the program counter and stack pointer will be back at the tbegin but our in memory stack won't be valid anymore.
To avoid this, when taking a signal in an active transaction, we need to use the stack pointer from the checkpointed state, rather than the speculated state. This ensures that the signal context (written tm suspended) will be written below the stack required for the rollback. The transaction is aborted becuase of the treclaim, so any memory written between the tbegin and the signal will be rolled back anyway.
For signals taken in non-TM or suspended mode, we use the normal/non-checkpointed stack pointer.
Tested with 64 and 32 bit signals
--- Documentation/powerpc/transactional_memory.txt | 19 +++++++++++ arch/powerpc/include/asm/processor.h | 13 +++----- arch/powerpc/include/asm/signal.h | 3 ++ arch/powerpc/kernel/signal.c | 40 ++++++++++++++++++++++-- arch/powerpc/kernel/signal.h | 2 +- arch/powerpc/kernel/signal_32.c | 10 ++---- arch/powerpc/kernel/signal_64.c | 23 +++++--------- 7 files changed, 74 insertions(+), 36 deletions(-)
Documentation/powerpc/transactional_memory.txt +19 -0
@@ -147,6 +147,25 @@ Example signal handler: fix_the_problem(ucp->dar); } +When in an active transaction that takes a signal, we need to be careful with +the stack. It's possible that the stack has moved back up after the tbegin. +The obvious case here is when the tbegin is called inside a function that +returns before a tend. In this case, the stack is part of the checkpointed +transactional memory state. If we write over this non transactionally or in +suspend, we are in trouble because if we get a tm abort, the program counter and +stack pointer will be back at the tbegin but our in memory stack won't be valid +anymore. + +To avoid this, when taking a signal in an active transaction, we need to use +the stack pointer from the checkpointed state, rather than the speculated +state. This ensures that the signal context (written tm suspended) will be +written below the stack required for the rollback. The transaction is aborted +becuase of the treclaim, so any memory written between the tbegin and the +signal will be rolled back anyway. + +For signals taken in non-TM or suspended mode, we use the +normal/non-checkpointed stack pointer. + Failure cause codes used by kernel ==================================
arch/powerpc/include/asm/processor.h +4 -9
@@ -409,21 +409,16 @@ static inline void prefetchw(const void *x)#endif #ifdef CONFIG_PPC64 -static inline unsigned long get_clean_sp (struct pt_regs *regs,int is_32) +static inline unsigned long get_clean_sp (unsigned long sp,int is_32) { -unsigned long sp; -if (is_32) - sp = regs->gpr[1 ] &0x0ffffffffUL ; -else - sp = regs->gpr[1 ]; - +return sp &0x0ffffffffUL ;return sp; }#else -static inline unsigned long get_clean_sp (struct pt_regs *regs,int is_32) +static inline unsigned long get_clean_sp (unsigned long sp,int is_32) { -return regs->gpr[1 ]; +return sp; }#endif
arch/powerpc/include/asm/signal.h +3 -0
@@ -3,5 +3,8 @@#define __ARCH_HAS_SA_RESTORER#include <uapi/asm/signal.h> +#include <uapi/asm/ptrace.h> + +extern unsigned long get_tm_stackpointer (struct pt_regs *regs);#endif /* _ASM_POWERPC_SIGNAL_H */
arch/powerpc/kernel/signal.c +38 -2
@@ -18,6 +18,7 @@#include <asm/uaccess.h> #include <asm/unistd.h> #include <asm/debug.h> +#include <asm/tm.h> #include "signal.h"
@@ -30,13 +31,13 @@ int show_unhandled_signals = 1;/* * Allocate space for the signal frame */ -void __user *get_sigframe (struct k_sigaction *ka,struct pt_regs *regs, +void __user *get_sigframe (struct k_sigaction *ka,unsigned long sp,size_t frame_size,int is_32) {unsigned long oldsp, newsp;/* Default to using normal stack */ - oldsp =get_clean_sp (regs, is_32); + oldsp =get_clean_sp (sp, is_32);/* Check for alt stack */ if ((ka->sa.sa_flags & SA_ONSTACK) &&
@@ -175,3 +176,38 @@ void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)user_enter (); } + +unsigned long get_tm_stackpointer (struct pt_regs *regs) +{ +/* When in an active transaction that takes a signal, we need to be +* careful with the stack. It's possible that the stack has moved back +* up after the tbegin. The obvious case here is when the tbegin is +* called inside a function that returns before a tend. In this case, +* the stack is part of the checkpointed transactional memory state. +* If we write over this non transactionally or in suspend, we are in +* trouble because if we get a tm abort, the program counter and stack +* pointer will be back at the tbegin but our in memory stack won't be +* valid anymore. +* +* To avoid this, when taking a signal in an active transaction, we +* need to use the stack pointer from the checkpointed state, rather +* than the speculated state. This ensures that the signal context +* (written tm suspended) will be written below the stack required for +* the rollback. The transaction is aborted becuase of the treclaim, +* so any memory written between the tbegin and the signal will be +* rolled back anyway. +* +* For signals taken in non-TM or suspended mode, we use the +* normal/non-checkpointed stack pointer. +*/ + +#ifdef CONFIG_PPC_TRANSACTIONAL_MEM +if (MSR_TM_ACTIVE (regs->msr)) { +tm_enable (); +tm_reclaim (¤t->thread, regs->msr,TM_CAUSE_SIGNAL ); +if (MSR_TM_TRANSACTIONAL (regs->msr)) +return current->thread.ckpt_regs.gpr[1 ]; + } +#endif +return regs->gpr[1 ]; +}
arch/powerpc/kernel/signal.h +1 -1
@@ -12,7 +12,7 @@extern void do_notify_resume (struct pt_regs *regs,unsigned long thread_info_flags); -extern void __user *get_sigframe (struct k_sigaction *ka,struct pt_regs *regs, +extern void __user *get_sigframe (struct k_sigaction *ka,unsigned long sp,size_t frame_size,int is_32); extern int handle_signal32(unsigned long sig, struct k_sigaction *ka,
arch/powerpc/kernel/signal_32.c +2 -8
@@ -503,12 +503,6 @@ static int save_tm_user_regs(struct pt_regs *regs, {unsigned long msr = regs->msr; -/* tm_reclaim rolls back all reg states, updating thread.ckpt_regs, -* thread.transact_fpr[], thread.transact_vr[], etc. -*/ -tm_enable (); -tm_reclaim (¤t->thread, msr,TM_CAUSE_SIGNAL ); -/* Make sure floating point registers are stored in regs */ flush_fp_to_thread (current);
@@ -965,7 +959,7 @@ int handle_rt_signal32(unsigned long sig, struct k_sigaction *ka,/* Set up Signal Frame */ /* Put a Real Time Context onto stack */ - rt_sf =get_sigframe (ka, regs,sizeof (*rt_sf),1 ); + rt_sf =get_sigframe (ka,get_tm_stackpointer (regs),sizeof (*rt_sf),1 ); addr = rt_sf;if (unlikely (rt_sf ==NULL )) goto badframe;
@@ -1403,7 +1397,7 @@ int handle_signal32(unsigned long sig, struct k_sigaction *ka,unsigned long tramp;/* Set up Signal Frame */ - frame =get_sigframe (ka, regs,sizeof (*frame),1 ); + frame =get_sigframe (ka,get_tm_stackpointer (regs),sizeof (*frame),1 );if (unlikely (frame ==NULL )) goto badframe; sc = (struct sigcontext __user *) &frame->sctx;
arch/powerpc/kernel/signal_64.c +7 -16
@@ -154,11 +154,12 @@ static long setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,* As above, but Transactional Memory is in use, so deliver sigcontexts * containing checkpointed and transactional register states. * -* To do this, we treclaim to gather both sets of registers and set up the -* 'normal' sigcontext registers with rolled-back register values such that a -* simple signal handler sees a correct checkpointed register state. -* If interested, a TM-aware sighandler can examine the transactional registers -* in the 2nd sigcontext to determine the real origin of the signal. +* To do this, we treclaim (done before entering here) to gather both sets of +* registers and set up the 'normal' sigcontext registers with rolled-back +* register values such that a simple signal handler sees a correct +* checkpointed register state. If interested, a TM-aware sighandler can +* examine the transactional registers in the 2nd sigcontext to determine the +* real origin of the signal. */ static long setup_tm_sigcontexts(struct sigcontext __user *sc, struct sigcontext __user *tm_sc,
@@ -184,16 +185,6 @@ static long setup_tm_sigcontexts(struct sigcontext __user *sc,BUG_ON (!MSR_TM_ACTIVE (regs->msr)); -/* tm_reclaim rolls back all reg states, saving checkpointed (older) -* GPRs to thread.ckpt_regs and (if used) FPRs to (newer) -* thread.transact_fp and/or VRs to (newer) thread.transact_vr. -* THEN we save out FP/VRs, if necessary, to the checkpointed (older) -* thread.fr[]/vr[]s. The transactional (newer) GPRs are on the -* stack, in *regs. -*/ -tm_enable (); -tm_reclaim (¤t->thread, msr,TM_CAUSE_SIGNAL ); -flush_fp_to_thread (current);#ifdef CONFIG_ALTIVEC
@@ -711,7 +702,7 @@ int handle_rt_signal64(int signr, struct k_sigaction *ka, siginfo_t *info,unsigned long newsp =0 ;long err =0 ; - frame =get_sigframe (ka, regs,sizeof (*frame),0 ); + frame =get_sigframe (ka,get_tm_stackpointer (regs),sizeof (*frame),0 );if (unlikely (frame ==NULL )) goto badframe;
Signature · 1 line
1.7.10.4