Skip to content

Commit 49edda5

Browse files
leitaogregkh
authored andcommitted
powerpc/tm: Set MSR[TS] just prior to recheckpoint
commit e1c3743e1a20647c53b719dbf28b48f45d23f2cd upstream. On a signal handler return, the user could set a context with MSR[TS] bits set, and these bits would be copied to task regs->msr. At restore_tm_sigcontexts(), after current task regs->msr[TS] bits are set, several __get_user() are called and then a recheckpoint is executed. This is a problem since a page fault (in kernel space) could happen when calling __get_user(). If it happens, the process MSR[TS] bits were already set, but recheckpoint was not executed, and SPRs are still invalid. The page fault can cause the current process to be de-scheduled, with MSR[TS] active and without tm_recheckpoint() being called. More importantly, without TEXASR[FS] bit set also. Since TEXASR might not have the FS bit set, and when the process is scheduled back, it will try to reclaim, which will be aborted because of the CPU is not in the suspended state, and, then, recheckpoint. This recheckpoint will restore thread->texasr into TEXASR SPR, which might be zero, hitting a BUG_ON(). kernel BUG at /build/linux-sf3Co9/linux-4.9.30/arch/powerpc/kernel/tm.S:434! cpu 0xb: Vector: 700 (Program Check) at [c00000041f1576d0] pc: c000000000054550: restore_gprs+0xb0/0x180 lr: 0000000000000000 sp: c00000041f157950 msr: 8000000100021033 current = 0xc00000041f143000 paca = 0xc00000000fb86300 softe: 0 irq_happened: 0x01 pid = 1021, comm = kworker/11:1 kernel BUG at /build/linux-sf3Co9/linux-4.9.30/arch/powerpc/kernel/tm.S:434! Linux version 4.9.0-3-powerpc64le ([email protected]) (gcc version 6.3.0 20170516 (Debian 6.3.0-18) ) #1 SMP Debian 4.9.30-2+deb9u2 (2017-06-26) enter ? for help [c00000041f157b30] c00000000001bc3c tm_recheckpoint.part.11+0x6c/0xa0 [c00000041f157b70] c00000000001d184 __switch_to+0x1e4/0x4c0 [c00000041f157bd0] c00000000082eeb8 __schedule+0x2f8/0x990 [c00000041f157cb0] c00000000082f598 schedule+0x48/0xc0 [c00000041f157ce0] c0000000000f0d28 worker_thread+0x148/0x610 [c00000041f157d80] c0000000000f96b0 kthread+0x120/0x140 [c00000041f157e30] c00000000000c0e0 ret_from_kernel_thread+0x5c/0x7c This patch simply delays the MSR[TS] set, so, if there is any page fault in the __get_user() section, it does not have regs->msr[TS] set, since the TM structures are still invalid, thus avoiding doing TM operations for in-kernel exceptions and possible process reschedule. With this patch, the MSR[TS] will only be set just before recheckpointing and setting TEXASR[FS] = 1, thus avoiding an interrupt with TM registers in invalid state. Other than that, if CONFIG_PREEMPT is set, there might be a preemption just after setting MSR[TS] and before tm_recheckpoint(), thus, this block must be atomic from a preemption perspective, thus, calling preempt_disable/enable() on this code. It is not possible to move tm_recheckpoint to happen earlier, because it is required to get the checkpointed registers from userspace, with __get_user(), thus, the only way to avoid this undesired behavior is delaying the MSR[TS] set. The 32-bits signal handler seems to be safe this current issue, but, it might be exposed to the preemption issue, thus, disabling preemption in this chunk of code. Changes from v2: * Run the critical section with preempt_disable. Fixes: 87b4e53 ("powerpc/tm: Fix return of active 64bit signals") Cc: [email protected] (v3.9+) Signed-off-by: Breno Leitao <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 0e36c79 commit 49edda5

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

arch/powerpc/kernel/signal_32.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,23 @@ static long restore_tm_user_regs(struct pt_regs *regs,
866866
/* If TM bits are set to the reserved value, it's an invalid context */
867867
if (MSR_TM_RESV(msr_hi))
868868
return 1;
869-
/* Pull in the MSR TM bits from the user context */
869+
870+
/*
871+
* Disabling preemption, since it is unsafe to be preempted
872+
* with MSR[TS] set without recheckpointing.
873+
*/
874+
preempt_disable();
875+
876+
/*
877+
* CAUTION:
878+
* After regs->MSR[TS] being updated, make sure that get_user(),
879+
* put_user() or similar functions are *not* called. These
880+
* functions can generate page faults which will cause the process
881+
* to be de-scheduled with MSR[TS] set but without calling
882+
* tm_recheckpoint(). This can cause a bug.
883+
*
884+
* Pull in the MSR TM bits from the user context
885+
*/
870886
regs->msr = (regs->msr & ~MSR_TS_MASK) | (msr_hi & MSR_TS_MASK);
871887
/* Now, recheckpoint. This loads up all of the checkpointed (older)
872888
* registers, including FP and V[S]Rs. After recheckpointing, the
@@ -891,6 +907,8 @@ static long restore_tm_user_regs(struct pt_regs *regs,
891907
}
892908
#endif
893909

910+
preempt_enable();
911+
894912
return 0;
895913
}
896914
#endif

arch/powerpc/kernel/signal_64.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -452,20 +452,6 @@ static long restore_tm_sigcontexts(struct task_struct *tsk,
452452
if (MSR_TM_RESV(msr))
453453
return -EINVAL;
454454

455-
/* pull in MSR TS bits from user context */
456-
regs->msr = (regs->msr & ~MSR_TS_MASK) | (msr & MSR_TS_MASK);
457-
458-
/*
459-
* Ensure that TM is enabled in regs->msr before we leave the signal
460-
* handler. It could be the case that (a) user disabled the TM bit
461-
* through the manipulation of the MSR bits in uc_mcontext or (b) the
462-
* TM bit was disabled because a sufficient number of context switches
463-
* happened whilst in the signal handler and load_tm overflowed,
464-
* disabling the TM bit. In either case we can end up with an illegal
465-
* TM state leading to a TM Bad Thing when we return to userspace.
466-
*/
467-
regs->msr |= MSR_TM;
468-
469455
/* pull in MSR LE from user context */
470456
regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
471457

@@ -557,6 +543,34 @@ static long restore_tm_sigcontexts(struct task_struct *tsk,
557543
tm_enable();
558544
/* Make sure the transaction is marked as failed */
559545
tsk->thread.tm_texasr |= TEXASR_FS;
546+
547+
/*
548+
* Disabling preemption, since it is unsafe to be preempted
549+
* with MSR[TS] set without recheckpointing.
550+
*/
551+
preempt_disable();
552+
553+
/* pull in MSR TS bits from user context */
554+
regs->msr = (regs->msr & ~MSR_TS_MASK) | (msr & MSR_TS_MASK);
555+
556+
/*
557+
* Ensure that TM is enabled in regs->msr before we leave the signal
558+
* handler. It could be the case that (a) user disabled the TM bit
559+
* through the manipulation of the MSR bits in uc_mcontext or (b) the
560+
* TM bit was disabled because a sufficient number of context switches
561+
* happened whilst in the signal handler and load_tm overflowed,
562+
* disabling the TM bit. In either case we can end up with an illegal
563+
* TM state leading to a TM Bad Thing when we return to userspace.
564+
*
565+
* CAUTION:
566+
* After regs->MSR[TS] being updated, make sure that get_user(),
567+
* put_user() or similar functions are *not* called. These
568+
* functions can generate page faults which will cause the process
569+
* to be de-scheduled with MSR[TS] set but without calling
570+
* tm_recheckpoint(). This can cause a bug.
571+
*/
572+
regs->msr |= MSR_TM;
573+
560574
/* This loads the checkpointed FP/VEC state, if used */
561575
tm_recheckpoint(&tsk->thread, msr);
562576

@@ -570,6 +584,8 @@ static long restore_tm_sigcontexts(struct task_struct *tsk,
570584
regs->msr |= MSR_VEC;
571585
}
572586

587+
preempt_enable();
588+
573589
return err;
574590
}
575591
#endif

0 commit comments

Comments
 (0)