atomic_t.txt 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. On atomic types (atomic_t atomic64_t and atomic_long_t).
  2. The atomic type provides an interface to the architecture's means of atomic
  3. RMW operations between CPUs (atomic operations on MMIO are not supported and
  4. can lead to fatal traps on some platforms).
  5. API
  6. ---
  7. The 'full' API consists of (atomic64_ and atomic_long_ prefixes omitted for
  8. brevity):
  9. Non-RMW ops:
  10. atomic_read(), atomic_set()
  11. atomic_read_acquire(), atomic_set_release()
  12. RMW atomic operations:
  13. Arithmetic:
  14. atomic_{add,sub,inc,dec}()
  15. atomic_{add,sub,inc,dec}_return{,_relaxed,_acquire,_release}()
  16. atomic_fetch_{add,sub,inc,dec}{,_relaxed,_acquire,_release}()
  17. Bitwise:
  18. atomic_{and,or,xor,andnot}()
  19. atomic_fetch_{and,or,xor,andnot}{,_relaxed,_acquire,_release}()
  20. Swap:
  21. atomic_xchg{,_relaxed,_acquire,_release}()
  22. atomic_cmpxchg{,_relaxed,_acquire,_release}()
  23. atomic_try_cmpxchg{,_relaxed,_acquire,_release}()
  24. Reference count (but please see refcount_t):
  25. atomic_add_unless(), atomic_inc_not_zero()
  26. atomic_sub_and_test(), atomic_dec_and_test()
  27. Misc:
  28. atomic_inc_and_test(), atomic_add_negative()
  29. atomic_dec_unless_positive(), atomic_inc_unless_negative()
  30. Barriers:
  31. smp_mb__{before,after}_atomic()
  32. SEMANTICS
  33. ---------
  34. Non-RMW ops:
  35. The non-RMW ops are (typically) regular LOADs and STOREs and are canonically
  36. implemented using READ_ONCE(), WRITE_ONCE(), smp_load_acquire() and
  37. smp_store_release() respectively.
  38. The one detail to this is that atomic_set{}() should be observable to the RMW
  39. ops. That is:
  40. C atomic-set
  41. {
  42. atomic_set(v, 1);
  43. }
  44. P1(atomic_t *v)
  45. {
  46. atomic_add_unless(v, 1, 0);
  47. }
  48. P2(atomic_t *v)
  49. {
  50. atomic_set(v, 0);
  51. }
  52. exists
  53. (v=2)
  54. In this case we would expect the atomic_set() from CPU1 to either happen
  55. before the atomic_add_unless(), in which case that latter one would no-op, or
  56. _after_ in which case we'd overwrite its result. In no case is "2" a valid
  57. outcome.
  58. This is typically true on 'normal' platforms, where a regular competing STORE
  59. will invalidate a LL/SC or fail a CMPXCHG.
  60. The obvious case where this is not so is when we need to implement atomic ops
  61. with a lock:
  62. CPU0 CPU1
  63. atomic_add_unless(v, 1, 0);
  64. lock();
  65. ret = READ_ONCE(v->counter); // == 1
  66. atomic_set(v, 0);
  67. if (ret != u) WRITE_ONCE(v->counter, 0);
  68. WRITE_ONCE(v->counter, ret + 1);
  69. unlock();
  70. the typical solution is to then implement atomic_set{}() with atomic_xchg().
  71. RMW ops:
  72. These come in various forms:
  73. - plain operations without return value: atomic_{}()
  74. - operations which return the modified value: atomic_{}_return()
  75. these are limited to the arithmetic operations because those are
  76. reversible. Bitops are irreversible and therefore the modified value
  77. is of dubious utility.
  78. - operations which return the original value: atomic_fetch_{}()
  79. - swap operations: xchg(), cmpxchg() and try_cmpxchg()
  80. - misc; the special purpose operations that are commonly used and would,
  81. given the interface, normally be implemented using (try_)cmpxchg loops but
  82. are time critical and can, (typically) on LL/SC architectures, be more
  83. efficiently implemented.
  84. All these operations are SMP atomic; that is, the operations (for a single
  85. atomic variable) can be fully ordered and no intermediate state is lost or
  86. visible.
  87. ORDERING (go read memory-barriers.txt first)
  88. --------
  89. The rule of thumb:
  90. - non-RMW operations are unordered;
  91. - RMW operations that have no return value are unordered;
  92. - RMW operations that have a return value are fully ordered;
  93. - RMW operations that are conditional are unordered on FAILURE,
  94. otherwise the above rules apply.
  95. Except of course when an operation has an explicit ordering like:
  96. {}_relaxed: unordered
  97. {}_acquire: the R of the RMW (or atomic_read) is an ACQUIRE
  98. {}_release: the W of the RMW (or atomic_set) is a RELEASE
  99. Where 'unordered' is against other memory locations. Address dependencies are
  100. not defeated.
  101. Fully ordered primitives are ordered against everything prior and everything
  102. subsequent. Therefore a fully ordered primitive is like having an smp_mb()
  103. before and an smp_mb() after the primitive.
  104. The barriers:
  105. smp_mb__{before,after}_atomic()
  106. only apply to the RMW ops and can be used to augment/upgrade the ordering
  107. inherent to the used atomic op. These barriers provide a full smp_mb().
  108. These helper barriers exist because architectures have varying implicit
  109. ordering on their SMP atomic primitives. For example our TSO architectures
  110. provide full ordered atomics and these barriers are no-ops.
  111. NOTE: when the atomic RmW ops are fully ordered, they should also imply a
  112. compiler barrier.
  113. Thus:
  114. atomic_fetch_add();
  115. is equivalent to:
  116. smp_mb__before_atomic();
  117. atomic_fetch_add_relaxed();
  118. smp_mb__after_atomic();
  119. However the atomic_fetch_add() might be implemented more efficiently.
  120. Further, while something like:
  121. smp_mb__before_atomic();
  122. atomic_dec(&X);
  123. is a 'typical' RELEASE pattern, the barrier is strictly stronger than
  124. a RELEASE. Similarly for something like:
  125. atomic_inc(&X);
  126. smp_mb__after_atomic();
  127. is an ACQUIRE pattern (though very much not typical), but again the barrier is
  128. strictly stronger than ACQUIRE. As illustrated:
  129. C strong-acquire
  130. {
  131. }
  132. P1(int *x, atomic_t *y)
  133. {
  134. r0 = READ_ONCE(*x);
  135. smp_rmb();
  136. r1 = atomic_read(y);
  137. }
  138. P2(int *x, atomic_t *y)
  139. {
  140. atomic_inc(y);
  141. smp_mb__after_atomic();
  142. WRITE_ONCE(*x, 1);
  143. }
  144. exists
  145. (r0=1 /\ r1=0)
  146. This should not happen; but a hypothetical atomic_inc_acquire() --
  147. (void)atomic_fetch_inc_acquire() for instance -- would allow the outcome,
  148. since then:
  149. P1 P2
  150. t = LL.acq *y (0)
  151. t++;
  152. *x = 1;
  153. r0 = *x (1)
  154. RMB
  155. r1 = *y (0)
  156. SC *y, t;
  157. is allowed.