transactional_memory.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. ============================
  2. Transactional Memory support
  3. ============================
  4. POWER kernel support for this feature is currently limited to supporting
  5. its use by user programs. It is not currently used by the kernel itself.
  6. This file aims to sum up how it is supported by Linux and what behaviour you
  7. can expect from your user programs.
  8. Basic overview
  9. ==============
  10. Hardware Transactional Memory is supported on POWER8 processors, and is a
  11. feature that enables a different form of atomic memory access. Several new
  12. instructions are presented to delimit transactions; transactions are
  13. guaranteed to either complete atomically or roll back and undo any partial
  14. changes.
  15. A simple transaction looks like this::
  16. begin_move_money:
  17. tbegin
  18. beq abort_handler
  19. ld r4, SAVINGS_ACCT(r3)
  20. ld r5, CURRENT_ACCT(r3)
  21. subi r5, r5, 1
  22. addi r4, r4, 1
  23. std r4, SAVINGS_ACCT(r3)
  24. std r5, CURRENT_ACCT(r3)
  25. tend
  26. b continue
  27. abort_handler:
  28. ... test for odd failures ...
  29. /* Retry the transaction if it failed because it conflicted with
  30. * someone else: */
  31. b begin_move_money
  32. The 'tbegin' instruction denotes the start point, and 'tend' the end point.
  33. Between these points the processor is in 'Transactional' state; any memory
  34. references will complete in one go if there are no conflicts with other
  35. transactional or non-transactional accesses within the system. In this
  36. example, the transaction completes as though it were normal straight-line code
  37. IF no other processor has touched SAVINGS_ACCT(r3) or CURRENT_ACCT(r3); an
  38. atomic move of money from the current account to the savings account has been
  39. performed. Even though the normal ld/std instructions are used (note no
  40. lwarx/stwcx), either *both* SAVINGS_ACCT(r3) and CURRENT_ACCT(r3) will be
  41. updated, or neither will be updated.
  42. If, in the meantime, there is a conflict with the locations accessed by the
  43. transaction, the transaction will be aborted by the CPU. Register and memory
  44. state will roll back to that at the 'tbegin', and control will continue from
  45. 'tbegin+4'. The branch to abort_handler will be taken this second time; the
  46. abort handler can check the cause of the failure, and retry.
  47. Checkpointed registers include all GPRs, FPRs, VRs/VSRs, LR, CCR/CR, CTR, FPCSR
  48. and a few other status/flag regs; see the ISA for details.
  49. Causes of transaction aborts
  50. ============================
  51. - Conflicts with cache lines used by other processors
  52. - Signals
  53. - Context switches
  54. - See the ISA for full documentation of everything that will abort transactions.
  55. Syscalls
  56. ========
  57. Syscalls made from within an active transaction will not be performed and the
  58. transaction will be doomed by the kernel with the failure code TM_CAUSE_SYSCALL
  59. | TM_CAUSE_PERSISTENT.
  60. Syscalls made from within a suspended transaction are performed as normal and
  61. the transaction is not explicitly doomed by the kernel. However, what the
  62. kernel does to perform the syscall may result in the transaction being doomed
  63. by the hardware. The syscall is performed in suspended mode so any side
  64. effects will be persistent, independent of transaction success or failure. No
  65. guarantees are provided by the kernel about which syscalls will affect
  66. transaction success.
  67. Care must be taken when relying on syscalls to abort during active transactions
  68. if the calls are made via a library. Libraries may cache values (which may
  69. give the appearance of success) or perform operations that cause transaction
  70. failure before entering the kernel (which may produce different failure codes).
  71. Examples are glibc's getpid() and lazy symbol resolution.
  72. Signals
  73. =======
  74. Delivery of signals (both sync and async) during transactions provides a second
  75. thread state (ucontext/mcontext) to represent the second transactional register
  76. state. Signal delivery 'treclaim's to capture both register states, so signals
  77. abort transactions. The usual ucontext_t passed to the signal handler
  78. represents the checkpointed/original register state; the signal appears to have
  79. arisen at 'tbegin+4'.
  80. If the sighandler ucontext has uc_link set, a second ucontext has been
  81. delivered. For future compatibility the MSR.TS field should be checked to
  82. determine the transactional state -- if so, the second ucontext in uc->uc_link
  83. represents the active transactional registers at the point of the signal.
  84. For 64-bit processes, uc->uc_mcontext.regs->msr is a full 64-bit MSR and its TS
  85. field shows the transactional mode.
  86. For 32-bit processes, the mcontext's MSR register is only 32 bits; the top 32
  87. bits are stored in the MSR of the second ucontext, i.e. in
  88. uc->uc_link->uc_mcontext.regs->msr. The top word contains the transactional
  89. state TS.
  90. However, basic signal handlers don't need to be aware of transactions
  91. and simply returning from the handler will deal with things correctly:
  92. Transaction-aware signal handlers can read the transactional register state
  93. from the second ucontext. This will be necessary for crash handlers to
  94. determine, for example, the address of the instruction causing the SIGSEGV.
  95. Example signal handler::
  96. void crash_handler(int sig, siginfo_t *si, void *uc)
  97. {
  98. ucontext_t *ucp = uc;
  99. ucontext_t *transactional_ucp = ucp->uc_link;
  100. if (ucp_link) {
  101. u64 msr = ucp->uc_mcontext.regs->msr;
  102. /* May have transactional ucontext! */
  103. #ifndef __powerpc64__
  104. msr |= ((u64)transactional_ucp->uc_mcontext.regs->msr) << 32;
  105. #endif
  106. if (MSR_TM_ACTIVE(msr)) {
  107. /* Yes, we crashed during a transaction. Oops. */
  108. fprintf(stderr, "Transaction to be restarted at 0x%llx, but "
  109. "crashy instruction was at 0x%llx\n",
  110. ucp->uc_mcontext.regs->nip,
  111. transactional_ucp->uc_mcontext.regs->nip);
  112. }
  113. }
  114. fix_the_problem(ucp->dar);
  115. }
  116. When in an active transaction that takes a signal, we need to be careful with
  117. the stack. It's possible that the stack has moved back up after the tbegin.
  118. The obvious case here is when the tbegin is called inside a function that
  119. returns before a tend. In this case, the stack is part of the checkpointed
  120. transactional memory state. If we write over this non transactionally or in
  121. suspend, we are in trouble because if we get a tm abort, the program counter and
  122. stack pointer will be back at the tbegin but our in memory stack won't be valid
  123. anymore.
  124. To avoid this, when taking a signal in an active transaction, we need to use
  125. the stack pointer from the checkpointed state, rather than the speculated
  126. state. This ensures that the signal context (written tm suspended) will be
  127. written below the stack required for the rollback. The transaction is aborted
  128. because of the treclaim, so any memory written between the tbegin and the
  129. signal will be rolled back anyway.
  130. For signals taken in non-TM or suspended mode, we use the
  131. normal/non-checkpointed stack pointer.
  132. Any transaction initiated inside a sighandler and suspended on return
  133. from the sighandler to the kernel will get reclaimed and discarded.
  134. Failure cause codes used by kernel
  135. ==================================
  136. These are defined in <asm/reg.h>, and distinguish different reasons why the
  137. kernel aborted a transaction:
  138. ====================== ================================
  139. TM_CAUSE_RESCHED Thread was rescheduled.
  140. TM_CAUSE_TLBI Software TLB invalid.
  141. TM_CAUSE_FAC_UNAV FP/VEC/VSX unavailable trap.
  142. TM_CAUSE_SYSCALL Syscall from active transaction.
  143. TM_CAUSE_SIGNAL Signal delivered.
  144. TM_CAUSE_MISC Currently unused.
  145. TM_CAUSE_ALIGNMENT Alignment fault.
  146. TM_CAUSE_EMULATE Emulation that touched memory.
  147. ====================== ================================
  148. These can be checked by the user program's abort handler as TEXASR[0:7]. If
  149. bit 7 is set, it indicates that the error is considered persistent. For example
  150. a TM_CAUSE_ALIGNMENT will be persistent while a TM_CAUSE_RESCHED will not.
  151. GDB
  152. ===
  153. GDB and ptrace are not currently TM-aware. If one stops during a transaction,
  154. it looks like the transaction has just started (the checkpointed state is
  155. presented). The transaction cannot then be continued and will take the failure
  156. handler route. Furthermore, the transactional 2nd register state will be
  157. inaccessible. GDB can currently be used on programs using TM, but not sensibly
  158. in parts within transactions.
  159. POWER9
  160. ======
  161. TM on POWER9 has issues with storing the complete register state. This
  162. is described in this commit::
  163. commit 4bb3c7a0208fc13ca70598efd109901a7cd45ae7
  164. Author: Paul Mackerras <[email protected]>
  165. Date: Wed Mar 21 21:32:01 2018 +1100
  166. KVM: PPC: Book3S HV: Work around transactional memory bugs in POWER9
  167. To account for this different POWER9 chips have TM enabled in
  168. different ways.
  169. On POWER9N DD2.01 and below, TM is disabled. ie
  170. HWCAP2[PPC_FEATURE2_HTM] is not set.
  171. On POWER9N DD2.1 TM is configured by firmware to always abort a
  172. transaction when tm suspend occurs. So tsuspend will cause a
  173. transaction to be aborted and rolled back. Kernel exceptions will also
  174. cause the transaction to be aborted and rolled back and the exception
  175. will not occur. If userspace constructs a sigcontext that enables TM
  176. suspend, the sigcontext will be rejected by the kernel. This mode is
  177. advertised to users with HWCAP2[PPC_FEATURE2_HTM_NO_SUSPEND] set.
  178. HWCAP2[PPC_FEATURE2_HTM] is not set in this mode.
  179. On POWER9N DD2.2 and above, KVM and POWERVM emulate TM for guests (as
  180. described in commit 4bb3c7a0208f), hence TM is enabled for guests
  181. ie. HWCAP2[PPC_FEATURE2_HTM] is set for guest userspace. Guests that
  182. makes heavy use of TM suspend (tsuspend or kernel suspend) will result
  183. in traps into the hypervisor and hence will suffer a performance
  184. degradation. Host userspace has TM disabled
  185. ie. HWCAP2[PPC_FEATURE2_HTM] is not set. (although we make enable it
  186. at some point in the future if we bring the emulation into host
  187. userspace context switching).
  188. POWER9C DD1.2 and above are only available with POWERVM and hence
  189. Linux only runs as a guest. On these systems TM is emulated like on
  190. POWER9N DD2.2.
  191. Guest migration from POWER8 to POWER9 will work with POWER9N DD2.2 and
  192. POWER9C DD1.2. Since earlier POWER9 processors don't support TM
  193. emulation, migration from POWER8 to POWER9 is not supported there.
  194. Kernel implementation
  195. =====================
  196. h/rfid mtmsrd quirk
  197. -------------------
  198. As defined in the ISA, rfid has a quirk which is useful in early
  199. exception handling. When in a userspace transaction and we enter the
  200. kernel via some exception, MSR will end up as TM=0 and TS=01 (ie. TM
  201. off but TM suspended). Regularly the kernel will want change bits in
  202. the MSR and will perform an rfid to do this. In this case rfid can
  203. have SRR0 TM = 0 and TS = 00 (ie. TM off and non transaction) and the
  204. resulting MSR will retain TM = 0 and TS=01 from before (ie. stay in
  205. suspend). This is a quirk in the architecture as this would normally
  206. be a transition from TS=01 to TS=00 (ie. suspend -> non transactional)
  207. which is an illegal transition.
  208. This quirk is described the architecture in the definition of rfid
  209. with these lines:
  210. if (MSR 29:31 ¬ = 0b010 | SRR1 29:31 ¬ = 0b000) then
  211. MSR 29:31 <- SRR1 29:31
  212. hrfid and mtmsrd have the same quirk.
  213. The Linux kernel uses this quirk in its early exception handling.