powerpc: Don't enable FP/Altivec if not checkpointed
PATCH 1/2 powerpc: Don't enable FP/Altivec if not checkpointed
Lazy save and restore of FP/Altivec means that a userspace process can be sent to userspace with FP or Altivec disabled and loaded only as required (by way of an FP/Altivec unavailable exception). Transactional Memory complicates this situation as a transaction could be started without FP/Altivec being loaded up. This causes the hardware to checkpoint incorrect registers. Handling FP/Altivec unavailable exceptions while a thread is transactional requires a reclaim and recheckpoint to ensure the CPU has correct state for both sets of registers.
Lazy save and restore of FP/Altivec cannot be done if a process is transactional. If a facility was enabled it must remain enabled whenever a thread is transactional.
Commit dc16b553c949 ("powerpc: Always restore FPU/VEC/VSX if hardware transactional memory in use") ensures that the facilities are always enabled if a thread is transactional. A bug in the introduced code may cause it to inadvertently enable a facility that was (and should remain) disabled. The problem with this extraneous enablement is that the registers for the erroneously enabled facility have not been correctly recheckpointed - the recheckpointing code assumed the facility would remain disabled.
This causes transactional threads which return to their failure handler to observe incorrect checkpointed registers. Perhaps an example will help illustrate the problem:
A userspace process is running and uses both FP and Altivec registers. This process then continues to run for some time without touching either sets of registers. The kernel subsequently disables the facilities as part of lazy save and restore. The userspace process then performs a tbegin and the CPU checkpoints 'junk' FP and Altivec registers. The process then performs a floating point instruction triggering a fp unavailable exception in the kernel.
The kernel then loads the FP registers - and only the FP registers. Since the thread is transactional it must perform a reclaim and recheckpoint to ensure both the checkpointed registers and the transactional registers are correct. It then (correctly) enables MSR[FP] for the process. Later (on exception exist) the kernel also (inadvertently) enables MSR[VEC]. The process is then returned to userspace.
Since the act of loading the FP registers doomed the transaction we know CPU will fail the transaction, restore its checkpointed registers, and return the process to its failure handler. The problem is that we're now running with Altivec enabled and the 'junk' checkpointed registers are restored. The kernel had only recheckpointed FP.
This patch solves this by only activating FP/Altivec if userspace was using them when it entered the kernel and not simply if the process is transactional.
transactional memory in use")
--- arch/powerpc/kernel/process.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
arch/powerpc/kernel/process.c +15 -2
@@ -230,9 +230,15 @@ void enable_kernel_fp(void) }EXPORT_SYMBOL (enable_kernel_fp); +static int is_transactionally_fp (struct task_struct *tsk) +{ +return msr_tm_active (tsk->thread.regs->msr) && + (tsk->thread.ckpt_regs.msr &MSR_FP ); +} +static int restore_fp (struct task_struct *tsk) { -if (tsk->thread.load_fp ||msr_tm_active (tsk->thread.regs->msr)) { +if (tsk->thread.load_fp ||is_transactionally_fp (tsk)) {load_fp_state (¤t->thread.fp_state); current->thread.load_fp++;return 1 ;
@@ -311,10 +317,17 @@ void flush_altivec_to_thread(struct task_struct *tsk) }EXPORT_SYMBOL_GPL (flush_altivec_to_thread); +static int is_transactionally_altivec (struct task_struct *tsk) +{ +return msr_tm_active (tsk->thread.regs->msr) && + (tsk->thread.ckpt_regs.msr &MSR_VEC ); +} + +static int restore_altivec (struct task_struct *tsk) {if (cpu_has_feature (CPU_FTR_ALTIVEC ) && - (tsk->thread.load_vec ||msr_tm_active (tsk->thread.regs->msr))) { + (tsk->thread.load_vec ||is_transactionally_altivec (tsk))) {load_vr_state (&tsk->thread.vr_state); tsk->thread.used_vr =1 ; tsk->thread.load_vec++;
Signature · 1 line
2.14.3
PATCH 2/2 powerpc: Always save/restore checkpointed regs during treclaim/trecheckpoint
Lazy save and restore of FP/Altivec means that a userspace process can be sent to userspace with FP or Altivec disabled and loaded only as required (by way of an FP/Altivec unavailable exception). Transactional Memory complicates this situation as a transaction could be started without FP/Altivec being loaded up. This causes the hardware to checkpoint incorrect registers. Handling FP/Altivec unavailable exceptions while a thread is transactional requires a reclaim and recheckpoint to ensure the CPU has correct state for both sets of registers.
tm_reclaim() has optimisations to not always save the FP/Altivec registers to the checkpointed save area. This was originally done because the caller might have information that the checkpointed registers aren't valid due to lazy save and restore. We've also been a little vague as to how tm_reclaim() leaves the FP/Altivec state since it doesn't necessarily always save it to the thread struct. This has lead to an (incorrect) assumption that it leaves the checkpointed state on the CPU.
tm_recheckpoint() has similar optimisations in reverse. It may not always reload the checkpointed FP/Altivec registers from the thread struct before the trecheckpoint. It is therefore quite unclear where it expects to get the state from. This didn't help with the assumption made about tm_reclaim().
These optimisations sit in what is by definition a slow path. If a process has to go through a reclaim/recheckpoint then its transaction will be doomed on returning to userspace. This mean that the process will be unable to complete its transaction and be forced to its failure handler. This is already an out if line case for userspace. Furthermore, the cost of copying 64 times 128 bits from registers isn't very long[0] (at all) on modern processors. As such it appears these optimisations have only served to increase code complexity and are unlikely to have had a measurable performance impact.
Our transactional memory handling has been riddled with bugs. A cause of this has been difficulty in following the code flow, code complexity has not been our friend here. It makes sense to remove these optimisations in favour of a (hopefully) more stable implementation.
This patch does mean that some times the assembly will needlessly save 'junk' registers which will subsequently get overwritten with the correct value by the C code which calls the assembly function. This small inefficiency is far outweighed by the reduction in complexity for general TM code, context switching paths, and transactional facility unavailable exception handler.
0: I tried to measure it once for other work and found that it was hiding in the noise of everything else I was working with. I find it exceedingly likely this will be the case here.
--- arch/powerpc/include/asm/tm.h | 5 ++-- arch/powerpc/kernel/process.c | 26 +++++++----------- arch/powerpc/kernel/signal_32.c | 2 +- arch/powerpc/kernel/signal_64.c | 2 +- arch/powerpc/kernel/tm.S | 59 ++++++++++++----------------------------- arch/powerpc/kernel/traps.c | 23 +++++----------- 6 files changed, 37 insertions(+), 80 deletions(-)
arch/powerpc/include/asm/tm.h +2 -3
@@ -11,10 +11,9 @@extern void tm_enable (void );extern void tm_reclaim (struct thread_struct *thread, -unsigned long orig_msr,uint8_t cause); +uint8_t cause);extern void tm_reclaim_current (uint8_t cause); -extern void tm_recheckpoint (struct thread_struct *thread, -unsigned long orig_msr); +extern void tm_recheckpoint (struct thread_struct *thread);extern void tm_abort (uint8_t cause);extern void tm_save_sprs (struct thread_struct *thread);extern void tm_restore_sprs (struct thread_struct *thread);
arch/powerpc/kernel/process.c +10 -16
@@ -866,6 +866,10 @@ static void tm_reclaim_thread(struct thread_struct *thr,if (!MSR_TM_SUSPENDED (mfmsr ()))return ; +giveup_all (container_of(thr ,struct task_struct ,thread )); + +tm_reclaim (thr, cause); +/* * If we are in a transaction and FP is off then we can't have * used FP inside that transaction. Hence the checkpointed
@@ -884,10 +888,6 @@ static void tm_reclaim_thread(struct thread_struct *thr,if ((thr->ckpt_regs.msr &MSR_VEC ) ==0 )memcpy (&thr->ckvr_state, &thr->vr_state,sizeof (struct thread_vr_state )); - -giveup_all (container_of(thr ,struct task_struct ,thread )); - -tm_reclaim (thr, thr->ckpt_regs.msr, cause); }void tm_reclaim_current (uint8_t cause)
@@ -936,11 +936,9 @@ static inline void tm_reclaim_task(struct task_struct *tsk)tm_save_sprs (thr); } -extern void __tm_recheckpoint (struct thread_struct *thread, -unsigned long orig_msr); +extern void __tm_recheckpoint (struct thread_struct *thread); -void tm_recheckpoint (struct thread_struct *thread, -unsigned long orig_msr) +void tm_recheckpoint (struct thread_struct *thread) {unsigned long flags;
@@ -959,15 +957,13 @@ void tm_recheckpoint(struct thread_struct *thread,*/ tm_restore_sprs (thread); -__tm_recheckpoint (thread, orig_msr); +__tm_recheckpoint (thread);local_irq_restore (flags); }static inline void tm_recheckpoint_new_task (struct task_struct *new) { -unsigned long msr; -if (!cpu_has_feature (CPU_FTR_TM ))return ;
@@ -986,13 +982,11 @@ static inline void tm_recheckpoint_new_task(struct task_struct *new)tm_restore_sprs (&new->thread);return ; } - msr = new->thread.ckpt_regs.msr;/* Recheckpoint to restore original checkpointed register state. */ -TM_DEBUG ("*** tm_recheckpoint of pid %d " -"(new->msr 0x%lx, new->origmsr 0x%lx)\n" , - new->pid, new->thread.regs->msr, msr); +TM_DEBUG ("*** tm_recheckpoint of pid %d (new->msr 0x%lx)\n" , + new->pid, new->thread.regs->msr); -tm_recheckpoint (&new->thread, msr); +tm_recheckpoint (&new->thread);/* * The checkpointed state has been restored but the live state has
arch/powerpc/kernel/signal_32.c +1 -1
@@ -876,7 +876,7 @@ static long restore_tm_user_regs(struct pt_regs *regs,/* Make sure the transaction is marked as failed */ current->thread.tm_texasr |=TEXASR_FS ;/* This loads the checkpointed FP/VEC state, if used */ -tm_recheckpoint (¤t->thread, msr); +tm_recheckpoint (¤t->thread);/* This loads the speculative FP/VEC state, if used */ msr_check_and_set (msr & (MSR_FP |MSR_VEC ));
arch/powerpc/kernel/signal_64.c +1 -1
@@ -558,7 +558,7 @@ static long restore_tm_sigcontexts(struct task_struct *tsk,/* Make sure the transaction is marked as failed */ tsk->thread.tm_texasr |=TEXASR_FS ;/* This loads the checkpointed FP/VEC state, if used */ -tm_recheckpoint (&tsk->thread, msr); +tm_recheckpoint (&tsk->thread);msr_check_and_set (msr & (MSR_FP |MSR_VEC ));if (msr &MSR_FP ) {
arch/powerpc/kernel/tm.S +17 -42
153 lines not on this page · raw message
arch/powerpc/kernel/traps.c +6 -17
@@ -1495,7 +1495,7 @@ void fp_unavailable_tm(struct pt_regs *regs)* If VMX is in use, the VRs now hold checkpointed values, * so we don't want to load the VRs from the thread_struct. */ -tm_recheckpoint (¤t->thread,MSR_FP ); +tm_recheckpoint (¤t->thread);/* If VMX is in use, get the transactional values back */ if (regs->msr &MSR_VEC ) {
@@ -1517,7 +1517,7 @@ void altivec_unavailable_tm(struct pt_regs *regs) regs->nip, regs->msr);tm_reclaim_current (TM_CAUSE_FAC_UNAV ); regs->msr |=MSR_VEC ; -tm_recheckpoint (¤t->thread,MSR_VEC ); +tm_recheckpoint (¤t->thread); current->thread.used_vr =1 ;if (regs->msr &MSR_FP ) {
@@ -1529,8 +1529,6 @@ void altivec_unavailable_tm(struct pt_regs *regs)void vsx_unavailable_tm (struct pt_regs *regs) { -unsigned long orig_msr = regs->msr; -/* See the comments in fp_unavailable_tm(). This works similarly, * though we're loading both FP and VEC registers in here. *
@@ -1544,12 +1542,6 @@ void vsx_unavailable_tm(struct pt_regs *regs) current->thread.used_vsr =1 ; -/* If FP and VMX are already loaded, we have all the state we need */ -if ((orig_msr & (MSR_FP |MSR_VEC )) == (MSR_FP |MSR_VEC )) { - regs->msr |=MSR_VSX ; -return ; - } -/* This reclaims FP and/or VR regs if they're already enabled */ tm_reclaim_current (TM_CAUSE_FAC_UNAV );
@@ -1559,14 +1551,11 @@ void vsx_unavailable_tm(struct pt_regs *regs)/* This loads & recheckpoints FP and VRs; but we have * to be sure not to overwrite previously-valid state. */ -tm_recheckpoint (¤t->thread, regs->msr & ~orig_msr); - -msr_check_and_set (orig_msr & (MSR_FP |MSR_VEC )); +tm_recheckpoint (¤t->thread); -if (orig_msr &MSR_FP ) -load_fp_state (¤t->thread.fp_state); -if (orig_msr &MSR_VEC ) - load_vr_state(¤t->thread.vr_state); +msr_check_and_set (MSR_FP |MSR_VEC |MSR_VSX ); +load_fp_state (¤t->thread.fp_state); + load_vr_state(¤t->thread.vr_state); }#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
Signature · 1 line
2.14.3
kbuild test robot
build botThank you for the patch! Yet something to improve:
Hi Cyril,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on powerpc/next]
[also build test ERROR on v4.14-rc7 next-20171018]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Cyril-Bur/powerpc-Don-t-enable-FP-Altivec-if-not-checkpointed/20171102-073816 base: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next config: powerpc-asp8347_defconfig (attached as .config) compiler: powerpc-linux-gnu-gcc (Debian 6.1.1-9) 6.1.1 20160705
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=powerpc
All errors (new ones prefixed by >>):
arch/powerpc/kernel/process.c: In function 'is_transactionally_fp':
>> arch/powerpc/kernel/process.c:243:15: error: 'struct thread_struct' has no member named 'ckpt_regs'
(tsk->thread.ckpt_regs.msr & MSR_FP);
^
arch/powerpc/kernel/process.c:244:1: error: control reaches end of non-void function [-Werror=return-type]
}
^cc1: all warnings being treated as errors
vim +243 arch/powerpc/kernel/process.c
239
240 static int is_transactionally_fp(struct task_struct *tsk)
241 {
242 return msr_tm_active(tsk->thread.regs->msr) &&
> 243 (tsk->thread.ckpt_regs.msr & MSR_FP);
244 }
245
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Quoted 4 lines · kbuild test robot
Hi Cyril,
Thank you for the patch! Yet something to improve:
Once again robot, you have done brilliantly! You're 100% correct and the last thing I want to do is break the build with CONFIG_PPC_TRANSACTIONAL_MEM turned off.
Life saver,
Thanks so much kbuild.
Cyril
Quoted 39 lines
[auto build test ERROR on powerpc/next]
[also build test ERROR on v4.14-rc7 next-20171018]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]url: https://github.com/0day-ci/linux/commits/Cyril-Bur/powerpc-Don-t-enable-FP-Altivec-if-not-checkpointed/20171102-073816 base: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next config: powerpc-asp8347_defconfig (attached as .config) compiler: powerpc-linux-gnu-gcc (Debian 6.1.1-9) 6.1.1 20160705reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=powerpcAll errors (new ones prefixed by >>):
arch/powerpc/kernel/process.c: In function 'is_transactionally_fp':Quoted 1 line
Quoted 1 line
arch/powerpc/kernel/process.c:243:15: error: 'structthread_struct' has no member named 'ckpt_regs'(tsk->thread.ckpt_regs.msr & MSR_FP); ^ arch/powerpc/kernel/process.c:244:1: error: control reaches end of non-void function [-Werror=return-type] } ^cc1: all warnings being treated as errors
vim +243
arch/powerpc/kernel/process.c239 240 static int is_transactionally_fp(struct task_struct *tsk) 241 { 242 return msr_tm_active(tsk->thread.regs->msr) &&Quoted 1 line
243 (
tsk->thread.ckpt_regs.msr&MSR_FP);244 }
245---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Quoted 6 lines · Cyril Bur
Quoted 4 lines · kbuild test robot
Hi Cyril,
Thank you for the patch! Yet something to improve:
Quoted 6 lines
Once again robot, you have done brilliantly! You're 100% correct and the last thing I want to do is break the build with
CONFIG_PPC_TRANSACTIONAL_MEMturned off.Life saver,
Thanks so much kbuild.
Thanks Cyril, we are glad to do some help.
Quoted 43 lines
Cyril
Quoted 39 lines
[auto build test ERROR on powerpc/next]
[also build test ERROR on v4.14-rc7 next-20171018]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]url: https://github.com/0day-ci/linux/commits/Cyril-Bur/powerpc-Don-t-enable-FP-Altivec-if-not-checkpointed/20171102-073816 base: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next config: powerpc-asp8347_defconfig (attached as .config) compiler: powerpc-linux-gnu-gcc (Debian 6.1.1-9) 6.1.1 20160705reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=powerpcAll errors (new ones prefixed by >>):
arch/powerpc/kernel/process.c: In function 'is_transactionally_fp':Quoted 1 line
Quoted 1 line
arch/powerpc/kernel/process.c:243:15: error: 'structthread_struct' has no member named 'ckpt_regs'(tsk->thread.ckpt_regs.msr & MSR_FP); ^ arch/powerpc/kernel/process.c:244:1: error: control reaches end of non-void function [-Werror=return-type] } ^cc1: all warnings being treated as errors
vim +243
arch/powerpc/kernel/process.c239 240 static int is_transactionally_fp(struct task_struct *tsk) 241 { 242 return msr_tm_active(tsk->thread.regs->msr) &&Quoted 1 line
243 (
tsk->thread.ckpt_regs.msr&MSR_FP);244 }
245---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Fengguang Wu
build botYou are welcome! :)
You are welcome! :)
On Thu, Nov 02, 2017 at 01:44:07PM +1100, Cyril Bur wrote:
>On Thu, 2017-11-02 at 10:19 +0800, kbuild test robot wrote: >> Hi Cyril, >> >> Thank you for the patch! Yet something to improve: >> > >Once again robot, you have done brilliantly! You're 100% correct and >the last thing I want to do is break the build with >CONFIG_PPC_TRANSACTIONAL_MEM turned off. > >Life saver, >Thanks so much kbuild. > >Cyril > >> [auto build test ERROR on powerpc/next] >> [also build test ERROR on v4.14-rc7 next-20171018] >> [if your patch is applied to the wrong git tree, please drop us a note to help improve the system] >> >> url: https://github.com/0day-ci/linux/commits/Cyril-Bur/powerpc-Don-t-enable-FP-Altivec-if-not-checkpointed/20171102-073816 >> base: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next >> config: powerpc-asp8347_defconfig (attached as .config) >> compiler: powerpc-linux-gnu-gcc (Debian 6.1.1-9) 6.1.1 20160705 >> reproduce: >> wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross >> chmod +x ~/bin/make.cross >> # save the attached .config to linux build tree >> make.cross ARCH=powerpc >> >> All errors (new ones prefixed by >>): >> >> arch/powerpc/kernel/process.c: In function 'is_transactionally_fp': >> > > arch/powerpc/kernel/process.c:243:15: error: 'struct thread_struct' has no member named 'ckpt_regs' >> >> (tsk->thread.ckpt_regs.msr & MSR_FP); >> ^ >> arch/powerpc/kernel/process.c:244:1: error: control reaches end of non-void function [-Werror=return-type] >> } >> ^ >> cc1: all warnings being treated as errors >> >> vim +243 arch/powerpc/kernel/process.c >> >> 239 >> 240 static int is_transactionally_fp(struct task_struct *tsk) >> 241 { >> 242 return msr_tm_active(tsk->thread.regs->msr) && >> > 243 (tsk->thread.ckpt_regs.msr & MSR_FP); >> 244 } >> 245 >> >> --- >> 0-DAY kernel test infrastructure Open Source Technology Center >> https://lists.01.org/pipermail/kbuild-all Intel Corporation >