this_cpu_ops.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. ===================
  2. this_cpu operations
  3. ===================
  4. :Author: Christoph Lameter, August 4th, 2014
  5. :Author: Pranith Kumar, Aug 2nd, 2014
  6. this_cpu operations are a way of optimizing access to per cpu
  7. variables associated with the *currently* executing processor. This is
  8. done through the use of segment registers (or a dedicated register where
  9. the cpu permanently stored the beginning of the per cpu area for a
  10. specific processor).
  11. this_cpu operations add a per cpu variable offset to the processor
  12. specific per cpu base and encode that operation in the instruction
  13. operating on the per cpu variable.
  14. This means that there are no atomicity issues between the calculation of
  15. the offset and the operation on the data. Therefore it is not
  16. necessary to disable preemption or interrupts to ensure that the
  17. processor is not changed between the calculation of the address and
  18. the operation on the data.
  19. Read-modify-write operations are of particular interest. Frequently
  20. processors have special lower latency instructions that can operate
  21. without the typical synchronization overhead, but still provide some
  22. sort of relaxed atomicity guarantees. The x86, for example, can execute
  23. RMW (Read Modify Write) instructions like inc/dec/cmpxchg without the
  24. lock prefix and the associated latency penalty.
  25. Access to the variable without the lock prefix is not synchronized but
  26. synchronization is not necessary since we are dealing with per cpu
  27. data specific to the currently executing processor. Only the current
  28. processor should be accessing that variable and therefore there are no
  29. concurrency issues with other processors in the system.
  30. Please note that accesses by remote processors to a per cpu area are
  31. exceptional situations and may impact performance and/or correctness
  32. (remote write operations) of local RMW operations via this_cpu_*.
  33. The main use of the this_cpu operations has been to optimize counter
  34. operations.
  35. The following this_cpu() operations with implied preemption protection
  36. are defined. These operations can be used without worrying about
  37. preemption and interrupts::
  38. this_cpu_read(pcp)
  39. this_cpu_write(pcp, val)
  40. this_cpu_add(pcp, val)
  41. this_cpu_and(pcp, val)
  42. this_cpu_or(pcp, val)
  43. this_cpu_add_return(pcp, val)
  44. this_cpu_xchg(pcp, nval)
  45. this_cpu_cmpxchg(pcp, oval, nval)
  46. this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  47. this_cpu_sub(pcp, val)
  48. this_cpu_inc(pcp)
  49. this_cpu_dec(pcp)
  50. this_cpu_sub_return(pcp, val)
  51. this_cpu_inc_return(pcp)
  52. this_cpu_dec_return(pcp)
  53. Inner working of this_cpu operations
  54. ------------------------------------
  55. On x86 the fs: or the gs: segment registers contain the base of the
  56. per cpu area. It is then possible to simply use the segment override
  57. to relocate a per cpu relative address to the proper per cpu area for
  58. the processor. So the relocation to the per cpu base is encoded in the
  59. instruction via a segment register prefix.
  60. For example::
  61. DEFINE_PER_CPU(int, x);
  62. int z;
  63. z = this_cpu_read(x);
  64. results in a single instruction::
  65. mov ax, gs:[x]
  66. instead of a sequence of calculation of the address and then a fetch
  67. from that address which occurs with the per cpu operations. Before
  68. this_cpu_ops such sequence also required preempt disable/enable to
  69. prevent the kernel from moving the thread to a different processor
  70. while the calculation is performed.
  71. Consider the following this_cpu operation::
  72. this_cpu_inc(x)
  73. The above results in the following single instruction (no lock prefix!)::
  74. inc gs:[x]
  75. instead of the following operations required if there is no segment
  76. register::
  77. int *y;
  78. int cpu;
  79. cpu = get_cpu();
  80. y = per_cpu_ptr(&x, cpu);
  81. (*y)++;
  82. put_cpu();
  83. Note that these operations can only be used on per cpu data that is
  84. reserved for a specific processor. Without disabling preemption in the
  85. surrounding code this_cpu_inc() will only guarantee that one of the
  86. per cpu counters is correctly incremented. However, there is no
  87. guarantee that the OS will not move the process directly before or
  88. after the this_cpu instruction is executed. In general this means that
  89. the value of the individual counters for each processor are
  90. meaningless. The sum of all the per cpu counters is the only value
  91. that is of interest.
  92. Per cpu variables are used for performance reasons. Bouncing cache
  93. lines can be avoided if multiple processors concurrently go through
  94. the same code paths. Since each processor has its own per cpu
  95. variables no concurrent cache line updates take place. The price that
  96. has to be paid for this optimization is the need to add up the per cpu
  97. counters when the value of a counter is needed.
  98. Special operations
  99. ------------------
  100. ::
  101. y = this_cpu_ptr(&x)
  102. Takes the offset of a per cpu variable (&x !) and returns the address
  103. of the per cpu variable that belongs to the currently executing
  104. processor. this_cpu_ptr avoids multiple steps that the common
  105. get_cpu/put_cpu sequence requires. No processor number is
  106. available. Instead, the offset of the local per cpu area is simply
  107. added to the per cpu offset.
  108. Note that this operation is usually used in a code segment when
  109. preemption has been disabled. The pointer is then used to
  110. access local per cpu data in a critical section. When preemption
  111. is re-enabled this pointer is usually no longer useful since it may
  112. no longer point to per cpu data of the current processor.
  113. Per cpu variables and offsets
  114. -----------------------------
  115. Per cpu variables have *offsets* to the beginning of the per cpu
  116. area. They do not have addresses although they look like that in the
  117. code. Offsets cannot be directly dereferenced. The offset must be
  118. added to a base pointer of a per cpu area of a processor in order to
  119. form a valid address.
  120. Therefore the use of x or &x outside of the context of per cpu
  121. operations is invalid and will generally be treated like a NULL
  122. pointer dereference.
  123. ::
  124. DEFINE_PER_CPU(int, x);
  125. In the context of per cpu operations the above implies that x is a per
  126. cpu variable. Most this_cpu operations take a cpu variable.
  127. ::
  128. int __percpu *p = &x;
  129. &x and hence p is the *offset* of a per cpu variable. this_cpu_ptr()
  130. takes the offset of a per cpu variable which makes this look a bit
  131. strange.
  132. Operations on a field of a per cpu structure
  133. --------------------------------------------
  134. Let's say we have a percpu structure::
  135. struct s {
  136. int n,m;
  137. };
  138. DEFINE_PER_CPU(struct s, p);
  139. Operations on these fields are straightforward::
  140. this_cpu_inc(p.m)
  141. z = this_cpu_cmpxchg(p.m, 0, 1);
  142. If we have an offset to struct s::
  143. struct s __percpu *ps = &p;
  144. this_cpu_dec(ps->m);
  145. z = this_cpu_inc_return(ps->n);
  146. The calculation of the pointer may require the use of this_cpu_ptr()
  147. if we do not make use of this_cpu ops later to manipulate fields::
  148. struct s *pp;
  149. pp = this_cpu_ptr(&p);
  150. pp->m--;
  151. z = pp->n++;
  152. Variants of this_cpu ops
  153. ------------------------
  154. this_cpu ops are interrupt safe. Some architectures do not support
  155. these per cpu local operations. In that case the operation must be
  156. replaced by code that disables interrupts, then does the operations
  157. that are guaranteed to be atomic and then re-enable interrupts. Doing
  158. so is expensive. If there are other reasons why the scheduler cannot
  159. change the processor we are executing on then there is no reason to
  160. disable interrupts. For that purpose the following __this_cpu operations
  161. are provided.
  162. These operations have no guarantee against concurrent interrupts or
  163. preemption. If a per cpu variable is not used in an interrupt context
  164. and the scheduler cannot preempt, then they are safe. If any interrupts
  165. still occur while an operation is in progress and if the interrupt too
  166. modifies the variable, then RMW actions can not be guaranteed to be
  167. safe::
  168. __this_cpu_read(pcp)
  169. __this_cpu_write(pcp, val)
  170. __this_cpu_add(pcp, val)
  171. __this_cpu_and(pcp, val)
  172. __this_cpu_or(pcp, val)
  173. __this_cpu_add_return(pcp, val)
  174. __this_cpu_xchg(pcp, nval)
  175. __this_cpu_cmpxchg(pcp, oval, nval)
  176. __this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
  177. __this_cpu_sub(pcp, val)
  178. __this_cpu_inc(pcp)
  179. __this_cpu_dec(pcp)
  180. __this_cpu_sub_return(pcp, val)
  181. __this_cpu_inc_return(pcp)
  182. __this_cpu_dec_return(pcp)
  183. Will increment x and will not fall-back to code that disables
  184. interrupts on platforms that cannot accomplish atomicity through
  185. address relocation and a Read-Modify-Write operation in the same
  186. instruction.
  187. &this_cpu_ptr(pp)->n vs this_cpu_ptr(&pp->n)
  188. --------------------------------------------
  189. The first operation takes the offset and forms an address and then
  190. adds the offset of the n field. This may result in two add
  191. instructions emitted by the compiler.
  192. The second one first adds the two offsets and then does the
  193. relocation. IMHO the second form looks cleaner and has an easier time
  194. with (). The second form also is consistent with the way
  195. this_cpu_read() and friends are used.
  196. Remote access to per cpu data
  197. ------------------------------
  198. Per cpu data structures are designed to be used by one cpu exclusively.
  199. If you use the variables as intended, this_cpu_ops() are guaranteed to
  200. be "atomic" as no other CPU has access to these data structures.
  201. There are special cases where you might need to access per cpu data
  202. structures remotely. It is usually safe to do a remote read access
  203. and that is frequently done to summarize counters. Remote write access
  204. something which could be problematic because this_cpu ops do not
  205. have lock semantics. A remote write may interfere with a this_cpu
  206. RMW operation.
  207. Remote write accesses to percpu data structures are highly discouraged
  208. unless absolutely necessary. Please consider using an IPI to wake up
  209. the remote CPU and perform the update to its per cpu area.
  210. To access per-cpu data structure remotely, typically the per_cpu_ptr()
  211. function is used::
  212. DEFINE_PER_CPU(struct data, datap);
  213. struct data *p = per_cpu_ptr(&datap, cpu);
  214. This makes it explicit that we are getting ready to access a percpu
  215. area remotely.
  216. You can also do the following to convert the datap offset to an address::
  217. struct data *p = this_cpu_ptr(&datap);
  218. but, passing of pointers calculated via this_cpu_ptr to other cpus is
  219. unusual and should be avoided.
  220. Remote access are typically only for reading the status of another cpus
  221. per cpu data. Write accesses can cause unique problems due to the
  222. relaxed synchronization requirements for this_cpu operations.
  223. One example that illustrates some concerns with write operations is
  224. the following scenario that occurs because two per cpu variables
  225. share a cache-line but the relaxed synchronization is applied to
  226. only one process updating the cache-line.
  227. Consider the following example::
  228. struct test {
  229. atomic_t a;
  230. int b;
  231. };
  232. DEFINE_PER_CPU(struct test, onecacheline);
  233. There is some concern about what would happen if the field 'a' is updated
  234. remotely from one processor and the local processor would use this_cpu ops
  235. to update field b. Care should be taken that such simultaneous accesses to
  236. data within the same cache line are avoided. Also costly synchronization
  237. may be necessary. IPIs are generally recommended in such scenarios instead
  238. of a remote write to the per cpu area of another processor.
  239. Even in cases where the remote writes are rare, please bear in
  240. mind that a remote write will evict the cache line from the processor
  241. that most likely will access it. If the processor wakes up and finds a
  242. missing local cache line of a per cpu area, its performance and hence
  243. the wake up times will be affected.