Skip to content

Commit 04e3f1d

Browse files
gustavoldgregkh
authored andcommitted
powerpc/tm: Fix clearing MSR[TS] in current when reclaiming on signal delivery
commit 2464cc4 upstream. After a treclaim, we expect to be in non-transactional state. If we don't clear the current thread's MSR[TS] before we get preempted, then tm_recheckpoint_new_task() will recheckpoint and we get rescheduled in suspended transaction state. When handling a signal caught in transactional state, handle_rt_signal64() calls get_tm_stackpointer() that treclaims the transaction using tm_reclaim_current() but without clearing the thread's MSR[TS]. This can cause the TM Bad Thing exception below if later we pagefault and get preempted trying to access the user's sigframe, using __put_user(). Afterwards, when we are rescheduled back into do_page_fault() (but now in suspended state since the thread's MSR[TS] was not cleared), upon executing 'rfid' after completion of the page fault handling, the exception is raised because a transition from suspended to non-transactional state is invalid. Unexpected TM Bad Thing exception at c00000000000de44 (msr 0x8000000302a03031) tm_scratch=800000010280b033 Oops: Unrecoverable exception, sig: 6 [#1] LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries CPU: 25 PID: 15547 Comm: a.out Not tainted 5.4.0-rc2 #32 NIP: c00000000000de44 LR: c000000000034728 CTR: 0000000000000000 REGS: c00000003fe7bd70 TRAP: 0700 Not tainted (5.4.0-rc2) MSR: 8000000302a03031 <SF,VEC,VSX,FP,ME,IR,DR,LE,TM[SE]> CR: 44000884 XER: 00000000 CFAR: c00000000000dda4 IRQMASK: 0 PACATMSCRATCH: 800000010280b033 GPR00: c000000000034728 c000000f65a17c80 c000000001662800 00007fffacf3fd78 GPR04: 0000000000001000 0000000000001000 0000000000000000 c000000f611f8af0 GPR08: 0000000000000000 0000000078006001 0000000000000000 000c000000000000 GPR12: c000000f611f84b0 c00000003ffcb200 0000000000000000 0000000000000000 GPR16: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR20: 0000000000000000 0000000000000000 0000000000000000 c000000f611f8140 GPR24: 0000000000000000 00007fffacf3fd68 c000000f65a17d90 c000000f611f7800 GPR28: c000000f65a17e90 c000000f65a17e90 c000000001685e18 00007fffacf3f000 NIP [c00000000000de44] fast_exception_return+0xf4/0x1b0 LR [c000000000034728] handle_rt_signal64+0x78/0xc50 Call Trace: [c000000f65a17c80] [c000000000034710] handle_rt_signal64+0x60/0xc50 (unreliable) [c000000f65a17d30] [c000000000023640] do_notify_resume+0x330/0x460 [c000000f65a17e20] [c00000000000dcc4] ret_from_except_lite+0x70/0x74 Instruction dump: 7c4ff120 e8410170 7c5a03a6 38400000 f8410060 e8010070 e8410080 e8610088 60000000 60000000 e8810090 e8210078 <4c000024> 48000000 e8610178 88ed0989 ---[ end trace 93094aa44b442f87 ]--- The simplified sequence of events that triggers the above exception is: ... # userspace in NON-TRANSACTIONAL state tbegin # userspace in TRANSACTIONAL state signal delivery # kernelspace in SUSPENDED state handle_rt_signal64() get_tm_stackpointer() treclaim # kernelspace in NON-TRANSACTIONAL state __put_user() page fault happens. We will never get back here because of the TM Bad Thing exception. page fault handling kicks in and we voluntarily preempt ourselves do_page_fault() __schedule() __switch_to(other_task) our task is rescheduled and we recheckpoint because the thread's MSR[TS] was not cleared __switch_to(our_task) switch_to_tm() tm_recheckpoint_new_task() trechkpt # kernelspace in SUSPENDED state The page fault handling resumes, but now we are in suspended transaction state do_page_fault() completes rfid <----- trying to get back where the page fault happened (we were non-transactional back then) TM Bad Thing # illegal transition from suspended to non-transactional This patch fixes that issue by clearing the current thread's MSR[TS] just after treclaim in get_tm_stackpointer() so that we stay in non-transactional state in case we are preempted. In order to make treclaim and clearing the thread's MSR[TS] atomic from a preemption perspective when CONFIG_PREEMPT is set, preempt_disable/enable() is used. It's also necessary to save the previous value of the thread's MSR before get_tm_stackpointer() is called so that it can be exposed to the signal handler later in setup_tm_sigcontexts() to inform the userspace MSR at the moment of the signal delivery. Found with tm-signal-context-force-tm kernel selftest. Fixes: 2b0a576 ("powerpc: Add new transactional memory state to the signal context") Cc: [email protected] # v3.9 Signed-off-by: Gustavo Luiz Duarte <[email protected]> Acked-by: Michael Neuling <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent a03b3ce commit 04e3f1d

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

arch/powerpc/kernel/signal.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,27 @@ unsigned long get_tm_stackpointer(struct task_struct *tsk)
200200
* normal/non-checkpointed stack pointer.
201201
*/
202202

203+
unsigned long ret = tsk->thread.regs->gpr[1];
204+
203205
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
204206
BUG_ON(tsk != current);
205207

206208
if (MSR_TM_ACTIVE(tsk->thread.regs->msr)) {
209+
preempt_disable();
207210
tm_reclaim_current(TM_CAUSE_SIGNAL);
208211
if (MSR_TM_TRANSACTIONAL(tsk->thread.regs->msr))
209-
return tsk->thread.ckpt_regs.gpr[1];
212+
ret = tsk->thread.ckpt_regs.gpr[1];
213+
214+
/*
215+
* If we treclaim, we must clear the current thread's TM bits
216+
* before re-enabling preemption. Otherwise we might be
217+
* preempted and have the live MSR[TS] changed behind our back
218+
* (tm_recheckpoint_new_task() would recheckpoint). Besides, we
219+
* enter the signal handler in non-transactional state.
220+
*/
221+
tsk->thread.regs->msr &= ~MSR_TS_MASK;
222+
preempt_enable();
210223
}
211224
#endif
212-
return tsk->thread.regs->gpr[1];
225+
return ret;
213226
}

arch/powerpc/kernel/signal_32.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -489,19 +489,11 @@ static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame,
489489
*/
490490
static int save_tm_user_regs(struct pt_regs *regs,
491491
struct mcontext __user *frame,
492-
struct mcontext __user *tm_frame, int sigret)
492+
struct mcontext __user *tm_frame, int sigret,
493+
unsigned long msr)
493494
{
494-
unsigned long msr = regs->msr;
495-
496495
WARN_ON(tm_suspend_disabled);
497496

498-
/* Remove TM bits from thread's MSR. The MSR in the sigcontext
499-
* just indicates to userland that we were doing a transaction, but we
500-
* don't want to return in transactional state. This also ensures
501-
* that flush_fp_to_thread won't set TIF_RESTORE_TM again.
502-
*/
503-
regs->msr &= ~MSR_TS_MASK;
504-
505497
/* Save both sets of general registers */
506498
if (save_general_regs(&current->thread.ckpt_regs, frame)
507499
|| save_general_regs(regs, tm_frame))
@@ -912,6 +904,10 @@ int handle_rt_signal32(struct ksignal *ksig, sigset_t *oldset,
912904
int sigret;
913905
unsigned long tramp;
914906
struct pt_regs *regs = tsk->thread.regs;
907+
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
908+
/* Save the thread's msr before get_tm_stackpointer() changes it */
909+
unsigned long msr = regs->msr;
910+
#endif
915911

916912
BUG_ON(tsk != current);
917913

@@ -944,13 +940,13 @@ int handle_rt_signal32(struct ksignal *ksig, sigset_t *oldset,
944940

945941
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
946942
tm_frame = &rt_sf->uc_transact.uc_mcontext;
947-
if (MSR_TM_ACTIVE(regs->msr)) {
943+
if (MSR_TM_ACTIVE(msr)) {
948944
if (__put_user((unsigned long)&rt_sf->uc_transact,
949945
&rt_sf->uc.uc_link) ||
950946
__put_user((unsigned long)tm_frame,
951947
&rt_sf->uc_transact.uc_regs))
952948
goto badframe;
953-
if (save_tm_user_regs(regs, frame, tm_frame, sigret))
949+
if (save_tm_user_regs(regs, frame, tm_frame, sigret, msr))
954950
goto badframe;
955951
}
956952
else
@@ -1369,6 +1365,10 @@ int handle_signal32(struct ksignal *ksig, sigset_t *oldset,
13691365
int sigret;
13701366
unsigned long tramp;
13711367
struct pt_regs *regs = tsk->thread.regs;
1368+
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1369+
/* Save the thread's msr before get_tm_stackpointer() changes it */
1370+
unsigned long msr = regs->msr;
1371+
#endif
13721372

13731373
BUG_ON(tsk != current);
13741374

@@ -1402,9 +1402,9 @@ int handle_signal32(struct ksignal *ksig, sigset_t *oldset,
14021402

14031403
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
14041404
tm_mctx = &frame->mctx_transact;
1405-
if (MSR_TM_ACTIVE(regs->msr)) {
1405+
if (MSR_TM_ACTIVE(msr)) {
14061406
if (save_tm_user_regs(regs, &frame->mctx, &frame->mctx_transact,
1407-
sigret))
1407+
sigret, msr))
14081408
goto badframe;
14091409
}
14101410
else

arch/powerpc/kernel/signal_64.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ static long setup_sigcontext(struct sigcontext __user *sc,
192192
static long setup_tm_sigcontexts(struct sigcontext __user *sc,
193193
struct sigcontext __user *tm_sc,
194194
struct task_struct *tsk,
195-
int signr, sigset_t *set, unsigned long handler)
195+
int signr, sigset_t *set, unsigned long handler,
196+
unsigned long msr)
196197
{
197198
/* When CONFIG_ALTIVEC is set, we _always_ setup v_regs even if the
198199
* process never used altivec yet (MSR_VEC is zero in pt_regs of
@@ -207,12 +208,11 @@ static long setup_tm_sigcontexts(struct sigcontext __user *sc,
207208
elf_vrreg_t __user *tm_v_regs = sigcontext_vmx_regs(tm_sc);
208209
#endif
209210
struct pt_regs *regs = tsk->thread.regs;
210-
unsigned long msr = tsk->thread.regs->msr;
211211
long err = 0;
212212

213213
BUG_ON(tsk != current);
214214

215-
BUG_ON(!MSR_TM_ACTIVE(regs->msr));
215+
BUG_ON(!MSR_TM_ACTIVE(msr));
216216

217217
WARN_ON(tm_suspend_disabled);
218218

@@ -222,13 +222,6 @@ static long setup_tm_sigcontexts(struct sigcontext __user *sc,
222222
*/
223223
msr |= tsk->thread.ckpt_regs.msr & (MSR_FP | MSR_VEC | MSR_VSX);
224224

225-
/* Remove TM bits from thread's MSR. The MSR in the sigcontext
226-
* just indicates to userland that we were doing a transaction, but we
227-
* don't want to return in transactional state. This also ensures
228-
* that flush_fp_to_thread won't set TIF_RESTORE_TM again.
229-
*/
230-
regs->msr &= ~MSR_TS_MASK;
231-
232225
#ifdef CONFIG_ALTIVEC
233226
err |= __put_user(v_regs, &sc->v_regs);
234227
err |= __put_user(tm_v_regs, &tm_sc->v_regs);
@@ -824,6 +817,10 @@ int handle_rt_signal64(struct ksignal *ksig, sigset_t *set,
824817
unsigned long newsp = 0;
825818
long err = 0;
826819
struct pt_regs *regs = tsk->thread.regs;
820+
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
821+
/* Save the thread's msr before get_tm_stackpointer() changes it */
822+
unsigned long msr = regs->msr;
823+
#endif
827824

828825
BUG_ON(tsk != current);
829826

@@ -841,15 +838,16 @@ int handle_rt_signal64(struct ksignal *ksig, sigset_t *set,
841838
err |= __put_user(0, &frame->uc.uc_flags);
842839
err |= __save_altstack(&frame->uc.uc_stack, regs->gpr[1]);
843840
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
844-
if (MSR_TM_ACTIVE(regs->msr)) {
841+
if (MSR_TM_ACTIVE(msr)) {
845842
/* The ucontext_t passed to userland points to the second
846843
* ucontext_t (for transactional state) with its uc_link ptr.
847844
*/
848845
err |= __put_user(&frame->uc_transact, &frame->uc.uc_link);
849846
err |= setup_tm_sigcontexts(&frame->uc.uc_mcontext,
850847
&frame->uc_transact.uc_mcontext,
851848
tsk, ksig->sig, NULL,
852-
(unsigned long)ksig->ka.sa.sa_handler);
849+
(unsigned long)ksig->ka.sa.sa_handler,
850+
msr);
853851
} else
854852
#endif
855853
{

0 commit comments

Comments
 (0)