powerpc/tm: Fix clearing MSR[TS] in current when reclaiming on signal delivery
Subsystems (3)
PATCH v3 1/3 powerpc/tm: Fix clearing MSR[TS] in current when reclaiming on signal delivery
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 Modules linked in: nft_chain_nat nf_nat nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 ip6_tables ip_tables nft_compat ip_set nf_tables nfnetlink xts vmx_crypto sg virtio_balloon r_mod cdrom virtio_net net_failover virtio_blk virtio_scsi failover dm_mirror dm_region_hash dm_log dm_mod 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.
v3: Subject and comment improvements.
v2: Fix build failure when tm is disabled.
--- arch/powerpc/kernel/signal.c | 16 ++++++++++++++-- arch/powerpc/kernel/signal_32.c | 28 ++++++++++++++-------------- arch/powerpc/kernel/signal_64.c | 22 ++++++++++------------ 3 files changed, 38 insertions(+), 28 deletions(-)
arch/powerpc/kernel/signal.c +14 -2
@@ -200,14 +200,26 @@ unsigned long get_tm_stackpointer(struct task_struct *tsk)* normal/non-checkpointed stack pointer. */ +unsigned long ret = tsk->thread.regs->gpr[1 ]; +#ifdef CONFIG_PPC_TRANSACTIONAL_MEM BUG_ON (tsk != current);if (MSR_TM_ACTIVE (tsk->thread.regs->msr)) { +preempt_disable ();tm_reclaim_current (TM_CAUSE_SIGNAL );if (MSR_TM_TRANSACTIONAL (tsk->thread.regs->msr)) -return tsk->thread.ckpt_regs.gpr[1 ]; + ret = tsk->thread.ckpt_regs.gpr[1 ]; + +/* If we treclaim, we must clear the current thread's TM bits +* before re-enabling preemption. Otherwise we might be +* preempted and have the live MSR[TS] changed behind our back +* (tm_recheckpoint_new_task() would recheckpoint). Besides, we +* enter the signal handler in non-transactional state. +*/ + tsk->thread.regs->msr &= ~MSR_TS_MASK ; +preempt_enable (); }#endif -return tsk->thread.regs->gpr[1 ]; +return ret; }
arch/powerpc/kernel/signal_32.c +14 -14
@@ -489,19 +489,11 @@ static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame,*/ static int save_tm_user_regs (struct pt_regs *regs,struct mcontext __user *frame, -struct mcontext __user *tm_frame,int sigret) +struct mcontext __user *tm_frame,int sigret, +unsigned long msr) { -unsigned long msr = regs->msr; -WARN_ON (tm_suspend_disabled); -/* Remove TM bits from thread's MSR. The MSR in the sigcontext -* just indicates to userland that we were doing a transaction, but we -* don't want to return in transactional state. This also ensures -* that flush_fp_to_thread won't set TIF_RESTORE_TM again. -*/ - regs->msr &= ~MSR_TS_MASK ; -/* Save both sets of general registers */ if (save_general_regs (¤t->thread.ckpt_regs, frame) ||save_general_regs (regs, tm_frame))
@@ -912,6 +904,10 @@ int handle_rt_signal32(struct ksignal *ksig, sigset_t *oldset,int sigret;unsigned long tramp;struct pt_regs *regs = tsk->thread.regs; +#ifdef CONFIG_PPC_TRANSACTIONAL_MEM +/* Save the thread's msr before get_tm_stackpointer() changes it */ +unsigned long msr = regs->msr; +#endif BUG_ON (tsk != current);
@@ -944,13 +940,13 @@ int handle_rt_signal32(struct ksignal *ksig, sigset_t *oldset,#ifdef CONFIG_PPC_TRANSACTIONAL_MEM tm_frame = &rt_sf->uc_transact.uc_mcontext; -if (MSR_TM_ACTIVE (regs->msr)) { +if (MSR_TM_ACTIVE (msr)) {if (__put_user ((unsigned long )&rt_sf->uc_transact, &rt_sf->uc.uc_link) ||__put_user ((unsigned long )tm_frame, &rt_sf->uc_transact.uc_regs)) goto badframe; -if (save_tm_user_regs (regs, frame, tm_frame, sigret)) +if (save_tm_user_regs (regs, frame, tm_frame, sigret, msr)) goto badframe; }else
@@ -1369,6 +1365,10 @@ int handle_signal32(struct ksignal *ksig, sigset_t *oldset,int sigret;unsigned long tramp;struct pt_regs *regs = tsk->thread.regs; +#ifdef CONFIG_PPC_TRANSACTIONAL_MEM +/* Save the thread's msr before get_tm_stackpointer() changes it */ +unsigned long msr = regs->msr; +#endif BUG_ON (tsk != current);
@@ -1402,9 +1402,9 @@ int handle_signal32(struct ksignal *ksig, sigset_t *oldset,#ifdef CONFIG_PPC_TRANSACTIONAL_MEM tm_mctx = &frame->mctx_transact; -if (MSR_TM_ACTIVE (regs->msr)) { +if (MSR_TM_ACTIVE (msr)) {if (save_tm_user_regs (regs, &frame->mctx, &frame->mctx_transact, - sigret)) + sigret, msr)) goto badframe; }else
arch/powerpc/kernel/signal_64.c +10 -12
@@ -192,7 +192,8 @@ static long setup_sigcontext(struct sigcontext __user *sc,static long setup_tm_sigcontexts (struct sigcontext __user *sc,struct sigcontext __user *tm_sc,struct task_struct *tsk, -int signr,sigset_t *set,unsigned long handler) +int signr,sigset_t *set,unsigned long handler, +unsigned long msr) {/* When CONFIG_ALTIVEC is set, we _always_ setup v_regs even if the * 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,elf_vrreg_t __user *tm_v_regs =sigcontext_vmx_regs (tm_sc);#endif struct pt_regs *regs = tsk->thread.regs; -unsigned long msr = tsk->thread.regs->msr;long err =0 ;BUG_ON (tsk != current); -BUG_ON (!MSR_TM_ACTIVE (regs->msr)); +BUG_ON (!MSR_TM_ACTIVE (msr));WARN_ON (tm_suspend_disabled);
@@ -222,13 +222,6 @@ static long setup_tm_sigcontexts(struct sigcontext __user *sc,*/ msr |= tsk->thread.ckpt_regs.msr & (MSR_FP |MSR_VEC |MSR_VSX ); -/* Remove TM bits from thread's MSR. The MSR in the sigcontext -* just indicates to userland that we were doing a transaction, but we -* don't want to return in transactional state. This also ensures -* that flush_fp_to_thread won't set TIF_RESTORE_TM again. -*/ - regs->msr &= ~MSR_TS_MASK ; -#ifdef CONFIG_ALTIVEC err |=__put_user (v_regs, &sc->v_regs); err |=__put_user (tm_v_regs, &tm_sc->v_regs);
@@ -824,6 +817,10 @@ int handle_rt_signal64(struct ksignal *ksig, sigset_t *set,unsigned long newsp =0 ;long err =0 ;struct pt_regs *regs = tsk->thread.regs; +#ifdef CONFIG_PPC_TRANSACTIONAL_MEM +/* Save the thread's msr before get_tm_stackpointer() changes it */ +unsigned long msr = regs->msr; +#endif BUG_ON (tsk != current);
@@ -841,7 +838,7 @@ int handle_rt_signal64(struct ksignal *ksig, sigset_t *set, err |=__put_user (0 , &frame->uc.uc_flags); err |=__save_altstack (&frame->uc.uc_stack, regs->gpr[1 ]);#ifdef CONFIG_PPC_TRANSACTIONAL_MEM -if (MSR_TM_ACTIVE (regs->msr)) { +if (MSR_TM_ACTIVE (msr)) {/* The ucontext_t passed to userland points to the second * ucontext_t (for transactional state) with its uc_link ptr. */
@@ -849,7 +846,8 @@ int handle_rt_signal64(struct ksignal *ksig, sigset_t *set, err |=setup_tm_sigcontexts (&frame->uc.uc_mcontext, &frame->uc_transact.uc_mcontext, tsk, ksig->sig,NULL , - (unsigned long )ksig->ka.sa.sa_handler); + (unsigned long )ksig->ka.sa.sa_handler, + msr); } else#endif {
Signature · 1 line
2.21.1
PATCH v3 2/3 selftests/powerpc: Add tm-signal-pagefault test
This test triggers a TM Bad Thing by raising a signal in transactional state and forcing a pagefault to happen in kernelspace when the kernel signal handling code first touches the user signal stack.
This is inspired by the test tm-signal-context-force-tm but uses userfaultfd to make the test deterministic. While this test always triggers the bug in one run, I had to execute tm-signal-context-force-tm several times (the test runs 5000 times each execution) to trigger the same bug.
tm-signal-context-force-tm is kept instead of replaced because, while this test is more reliable and triggers the same bug, tm-signal-context-force-tm has a better coverage, in the sense that by running the test several times it might trigger the pagefault and/or be preempted at different places.
v3: skip test if userfaultfd is unavailable.
--- tools/testing/selftests/powerpc/tm/.gitignore | 1 + tools/testing/selftests/powerpc/tm/Makefile | 3 +- .../powerpc/tm/tm-signal-pagefault.c | 284 ++++++++++++++++++ 3 files changed, 287 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/powerpc/tm/tm-signal-pagefault.c
tools/testing/selftests/powerpc/tm/.gitignore +1 -0
@@ -13,6 +13,7 @@ tm-signal-context-chk-vmx tm-signal-context-chk-vsx tm-signal-context-force-tm tm-signal-sigreturn-nt +tm-signal-pagefault tm-vmx-unavail tm-unavailable tm-trap
tools/testing/selftests/powerpc/tm/Makefile +2 -1
@@ -5,7 +5,7 @@ SIGNAL_CONTEXT_CHK_TESTS := tm-signal-context-chk-gpr tm-signal-context-chk-fpu TEST_GEN_PROGS := tm-resched-dscr tm-syscall tm-signal-msr-resv tm-signal-stack \ tm-vmxcopy tm-fork tm-tar tm-tmspr tm-vmx-unavail tm-unavailable tm-trap \ $(SIGNAL_CONTEXT_CHK_TESTS) tm-sigreturn tm-signal-sigreturn-nt \ - tm-signal-context-force-tm tm-poison + tm-signal-context-force-tm tm-poison tm-signal-pagefault top_srcdir = ../../../../.. include ../../lib.mk
@@ -22,6 +22,7 @@ $(OUTPUT)/tm-resched-dscr: ../pmu/lib.c $(OUTPUT)/tm-unavailable: CFLAGS += -O0 -pthread -m64 -Wno-error=uninitialized -mvsx $(OUTPUT)/tm-trap: CFLAGS += -O0 -pthread -m64 $(OUTPUT)/tm-signal-context-force-tm: CFLAGS += -pthread -m64 +$(OUTPUT)/tm-signal-pagefault: CFLAGS += -pthread -m64 SIGNAL_CONTEXT_CHK_TESTS := $(patsubst %,$(OUTPUT)/%,$(SIGNAL_CONTEXT_CHK_TESTS)) $(SIGNAL_CONTEXT_CHK_TESTS): tm-signal.S
tools/testing/selftests/powerpc/tm/tm-signal-pagefault.c new file +284 -0
284 lines not on this page · raw message
Signature · 1 line
2.21.1
Michael Ellerman
AppliedRe: [PATCH v3 2/3] selftests/powerpc: Add tm-signal-pagefault test
Quoted 9 lines · Gustavo Luiz Duarte
This test triggers a TM Bad Thing by raising a signal in transactional state and forcing a pagefault to happen in kernelspace when the kernel signal handling code first touches the user signal stack.
This is inspired by the test tm-signal-context-force-tm but uses userfaultfd to make the test deterministic. While this test always triggers the bug in one run, I had to execute tm-signal-context-force-tm several times (the test runs 5000 times each execution) to trigger the same bug.
Quoted 8 lines
tm-signal-context-force-tm is kept instead of replaced because, while this test is more reliable and triggers the same bug, tm-signal-context-force-tm has a better coverage, in the sense that by running the test several times it might trigger the pagefault and/or be preempted at different places.
v3: skip test if userfaultfd is unavailable.
Signed-off-by Gustavo Luiz Duarte <[email protected]>
Applied to powerpc next, thanks.
https://git.kernel.org/powerpc/c/915b7f6f9a5e232c138bb36743a1fdb0fcf2c432
cheers
PATCH v3 3/3 selftests/powerpc: Don't rely on segfault to rerun the test
In reply to Gustavo Luiz DuarteThe test case tm-signal-context-force-tm expects a segfault to happen on returning from signal handler, and then does a setcontext() to run the test again. However, the test doesn't always segfault, causing the test to run a single time.
This patch fixes the test by putting it within a loop and jumping, via setcontext, just prior to the loop in case it segfaults. This way we get the desired behavior (run the test COUNT_MAX times) regardless if it segfaults or not. This also reduces the use of setcontext for control flow logic, keeping it only in the segfault handler.
Also, since 'count' is changed within the signal handler, it is declared as volatile to prevent any compiler optimization getting confused with asynchronous changes.
--- .../powerpc/tm/tm-signal-context-force-tm.c | 79 +++++++++---------- 1 file changed, 37 insertions(+), 42 deletions(-)
tools/testing/selftests/powerpc/tm/tm-signal-context-force-tm.c +37 -42
@@ -42,9 +42,10 @@#endif /* Setting contexts because the test will crash and we want to recover */ -ucontext_t init_context, main_context; +ucontext_t init_context; -static int count, first_time; +/* count is changed in the signal handler, so it must be volatile */ +static volatile int count;void usr_signal_handler (int signo,siginfo_t *si,void *uc) {
@@ -98,11 +99,6 @@ void usr_signal_handler(int signo, siginfo_t *si, void *uc)void seg_signal_handler (int signo,siginfo_t *si,void *uc) { -if (count ==COUNT_MAX ) { -/* Return to tm_signal_force_msr() and exit */ -setcontext (&main_context); - } - count++;/* Reexecute the test */
@@ -126,37 +122,40 @@ void tm_trap_test(void)*/ getcontext (&init_context); -/* Allocated an alternative signal stack area */ - ss.ss_sp =mmap (NULL ,SIGSTKSZ ,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS ,0 ,0 ); - ss.ss_size =SIGSTKSZ ; - ss.ss_flags =0 ; - -if (ss.ss_sp == (void *)-1 ) { -perror ("mmap error\n" ); -exit (-1 ); - } - -/* Force the allocation through a page fault */ -if (madvise (ss.ss_sp,SIGSTKSZ ,MADV_DONTNEED )) { -perror ("madvise\n" ); -exit (-1 ); - } - -/* Setting an alternative stack to generate a page fault when -* the signal is raised. -*/ -if (sigaltstack (&ss,NULL )) { -perror ("sigaltstack\n" ); -exit (-1 ); +while (count <COUNT_MAX ) { +/* Allocated an alternative signal stack area */ + ss.ss_sp =mmap (NULL ,SIGSTKSZ ,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,0 ,0 ); + ss.ss_size =SIGSTKSZ ; + ss.ss_flags =0 ; + +if (ss.ss_sp == (void *)-1 ) { +perror ("mmap error\n" ); +exit (-1 ); + } + +/* Force the allocation through a page fault */ +if (madvise (ss.ss_sp,SIGSTKSZ ,MADV_DONTNEED )) { +perror ("madvise\n" ); +exit (-1 ); + } + +/* Setting an alternative stack to generate a page fault when +* the signal is raised. +*/ +if (sigaltstack (&ss,NULL )) { +perror ("sigaltstack\n" ); +exit (-1 ); + } + +/* The signal handler will enable MSR_TS */ +sigaction (SIGUSR1 , &usr_sa,NULL ); +/* If it does not crash, it might segfault, avoid it to retest */ +sigaction (SIGSEGV , &seg_sa,NULL ); + +raise (SIGUSR1 ); + count++; } - -/* The signal handler will enable MSR_TS */ -sigaction (SIGUSR1 , &usr_sa,NULL ); -/* If it does not crash, it will segfault, avoid it to retest */ -sigaction (SIGSEGV , &seg_sa,NULL ); - -raise (SIGUSR1 ); }int tm_signal_context_force_tm (void )
@@ -169,11 +168,7 @@ int tm_signal_context_force_tm(void)*/ SKIP_IF (!is_ppc64le ()); -/* Will get back here after COUNT_MAX interactions */ -getcontext (&main_context); - -if (!first_time++) -tm_trap_test (); +tm_trap_test ();return EXIT_SUCCESS ; }
Signature · 1 line
2.21.1
Michael Ellerman
AppliedRe: [PATCH v3 3/3] selftests/powerpc: Don't rely on segfault to rerun the test
Quoted 11 lines · Gustavo Luiz Duarte
The test case tm-signal-context-force-tm expects a segfault to happen on returning from signal handler, and then does a
setcontext()to run the test again. However, the test doesn't always segfault, causing the test to run a single time.This patch fixes the test by putting it within a loop and jumping, via setcontext, just prior to the loop in case it segfaults. This way we get the desired behavior (run the test
COUNT_MAXtimes) regardless if it segfaults or not. This also reduces the use of setcontext for control flow logic, keeping it only in the segfault handler.
Quoted 5 lines
Also, since 'count' is changed within the signal handler, it is declared as volatile to prevent any compiler optimization getting confused with asynchronous changes.
Signed-off-by Gustavo Luiz Duarte <[email protected]>
Applied to powerpc next, thanks.
https://git.kernel.org/powerpc/c/0f8f554e5244f56f496b4ce30ada1126fe290345
cheers
Sasha Levin
maintainerHi,
[This is an automated email]
This commit has been processed because it contains a "Fixes:" tag,
fixing commit: 2b0a576d15e0 ("powerpc: Add new transactional memory state to the signal context").
The bot has tested the following trees: v5.5.2, v5.4.18, v4.19.102, v4.14.170, v4.9.213, v4.4.213.
v5.5.2: Build OK!
v4.19.102: Build OK!
v4.14.170: Failed to apply! Possible dependencies:
1c200e63d055 ("powerpc/tm: Fix endianness flip on trap") 92fb8690bd04 ("powerpc/tm: P9 disable transactionally suspended sigcontexts")
v4.9.213: Failed to apply! Possible dependencies:
1c200e63d055 ("powerpc/tm: Fix endianness flip on trap") 92fb8690bd04 ("powerpc/tm: P9 disable transactionally suspended sigcontexts")
v4.4.213: Failed to apply! Possible dependencies:
1c200e63d055 ("powerpc/tm: Fix endianness flip on trap")
92fb8690bd04 ("powerpc/tm: P9 disable transactionally suspended sigcontexts") a7d623d4d053 ("powerpc: Move part of giveup_vsx into c")
b86fd2bd0302 ("powerpc: Simplify TM restore checks")
d11994314b2b ("powerpc: signals: Stop using current in signal code") d96f234f47af ("powerpc: Avoid load hit store in setup_sigcontext()") e1c0d66fcb17 ("powerpc: Set used_(vsr|vr|spe) in sigreturn path when MSR bits are active")
NOTE: The patch will not be queued to stable trees until it is upstream.
How should we proceed with this patch?
Signature · 2 lines
Thanks, Sasha
Quoted 9 lines
Found with tm-signal-context-force-tm kernel selftest.
v3: Subject and comment improvements.
v2: Fix build failure when tm is disabled.Fixes 2b0a576d15e0 ("powerpc: Add new transactional memory state to thesignal context")
Cc [email protected] # v3.9Signed-off-by Gustavo Luiz Duarte <[email protected]>
Michael Ellerman
AppliedQuoted 81 lines · Gustavo Luiz Duarte
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()callsget_tm_stackpointer()that treclaims the transaction usingtm_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 intodo_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 Modules linked in: nft_chain_nat nf_nat nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 ip6_tables ip_tables nft_compat ip_set nf_tables nfnetlink xts vmx_crypto sg virtio_balloon r_mod cdrom virtio_net net_failover virtio_blk virtio_scsi failover dm_mirror dm_region_hash dm_log dm_mod 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 stateThe 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-transactionalThis 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 whenCONFIG_PREEMPTis set, preempt_disable/enable() is used. It's also necessary to save the previous value of the thread's MSR beforeget_tm_stackpointer()is called so that it can be exposed to the signal handler later insetup_tm_sigcontexts()to inform the userspace MSR at the moment of the signal delivery.
Quoted 8 lines
Found with tm-signal-context-force-tm kernel selftest.
v3: Subject and comment improvements.
v2: Fix build failure when tm is disabled.Fixes 2b0a576d15e0 ("powerpc: Add new transactional memory state to the signal context")Cc [email protected] # v3.9Signed-off-by Gustavo Luiz Duarte <[email protected]>
Applied to powerpc fixes, thanks.
https://git.kernel.org/powerpc/c/2464cc4c345699adea52c7aef75707207cb8a2f6
cheers