this_cpu_ops.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. this_cpu operations
  2. -------------------
  3. this_cpu operations are a way of optimizing access to per cpu
  4. variables associated with the *currently* executing processor through
  5. the use of segment registers (or a dedicated register where the cpu
  6. permanently stored the beginning of the per cpu area for a specific
  7. processor).
  8. The this_cpu operations add a per cpu variable offset to the processor
  9. specific percpu base and encode that operation in the instruction
  10. operating on the per cpu variable.
  11. This means there are no atomicity issues between the calculation of
  12. the offset and the operation on the data. Therefore it is not
  13. necessary to disable preempt or interrupts to ensure that the
  14. processor is not changed between the calculation of the address and
  15. the operation on the data.
  16. Read-modify-write operations are of particular interest. Frequently
  17. processors have special lower latency instructions that can operate
  18. without the typical synchronization overhead but still provide some
  19. sort of relaxed atomicity guarantee. The x86 for example can execute
  20. RMV (Read Modify Write) instructions like inc/dec/cmpxchg without the
  21. lock prefix and the associated latency penalty.
  22. Access to the variable without the lock prefix is not synchronized but
  23. synchronization is not necessary since we are dealing with per cpu
  24. data specific to the currently executing processor. Only the current
  25. processor should be accessing that variable and therefore there are no
  26. concurrency issues with other processors in the system.
  27. On x86 the fs: or the gs: segment registers contain the base of the
  28. per cpu area. It is then possible to simply use the segment override
  29. to relocate a per cpu relative address to the proper per cpu area for
  30. the processor. So the relocation to the per cpu base is encoded in the
  31. instruction via a segment register prefix.
  32. For example:
  33. DEFINE_PER_CPU(int, x);
  34. int z;
  35. z = this_cpu_read(x);
  36. results in a single instruction
  37. mov ax, gs:[x]
  38. instead of a sequence of calculation of the address and then a fetch
  39. from that address which occurs with the percpu operations. Before
  40. this_cpu_ops such sequence also required preempt disable/enable to
  41. prevent the kernel from moving the thread to a different processor
  42. while the calculation is performed.
  43. The main use of the this_cpu operations has been to optimize counter
  44. operations.
  45. this_cpu_inc(x)
  46. results in the following single instruction (no lock prefix!)
  47. inc gs:[x]
  48. instead of the following operations required if there is no segment
  49. register.
  50. int *y;
  51. int cpu;
  52. cpu = get_cpu();
  53. y = per_cpu_ptr(&x, cpu);
  54. (*y)++;
  55. put_cpu();
  56. Note that these operations can only be used on percpu data that is
  57. reserved for a specific processor. Without disabling preemption in the
  58. surrounding code this_cpu_inc() will only guarantee that one of the
  59. percpu counters is correctly incremented. However, there is no
  60. guarantee that the OS will not move the process directly before or
  61. after the this_cpu instruction is executed. In general this means that
  62. the value of the individual counters for each processor are
  63. meaningless. The sum of all the per cpu counters is the only value
  64. that is of interest.
  65. Per cpu variables are used for performance reasons. Bouncing cache
  66. lines can be avoided if multiple processors concurrently go through
  67. the same code paths. Since each processor has its own per cpu
  68. variables no concurrent cacheline updates take place. The price that
  69. has to be paid for this optimization is the need to add up the per cpu
  70. counters when the value of the counter is needed.
  71. Special operations:
  72. -------------------
  73. y = this_cpu_ptr(&x)
  74. Takes the offset of a per cpu variable (&x !) and returns the address
  75. of the per cpu variable that belongs to the currently executing
  76. processor. this_cpu_ptr avoids multiple steps that the common
  77. get_cpu/put_cpu sequence requires. No processor number is
  78. available. Instead the offset of the local per cpu area is simply
  79. added to the percpu offset.
  80. Per cpu variables and offsets
  81. -----------------------------
  82. Per cpu variables have *offsets* to the beginning of the percpu
  83. area. They do not have addresses although they look like that in the
  84. code. Offsets cannot be directly dereferenced. The offset must be
  85. added to a base pointer of a percpu area of a processor in order to
  86. form a valid address.
  87. Therefore the use of x or &x outside of the context of per cpu
  88. operations is invalid and will generally be treated like a NULL
  89. pointer dereference.
  90. In the context of per cpu operations
  91. x is a per cpu variable. Most this_cpu operations take a cpu
  92. variable.
  93. &x is the *offset* a per cpu variable. this_cpu_ptr() takes
  94. the offset of a per cpu variable which makes this look a bit
  95. strange.
  96. Operations on a field of a per cpu structure
  97. --------------------------------------------
  98. Let's say we have a percpu structure
  99. struct s {
  100. int n,m;
  101. };
  102. DEFINE_PER_CPU(struct s, p);
  103. Operations on these fields are straightforward
  104. this_cpu_inc(p.m)
  105. z = this_cpu_cmpxchg(p.m, 0, 1);
  106. If we have an offset to struct s:
  107. struct s __percpu *ps = &p;
  108. z = this_cpu_dec(ps->m);
  109. z = this_cpu_inc_return(ps->n);
  110. The calculation of the pointer may require the use of this_cpu_ptr()
  111. if we do not make use of this_cpu ops later to manipulate fields:
  112. struct s *pp;
  113. pp = this_cpu_ptr(&p);
  114. pp->m--;
  115. z = pp->n++;
  116. Variants of this_cpu ops
  117. -------------------------
  118. this_cpu ops are interrupt safe. Some architecture do not support
  119. these per cpu local operations. In that case the operation must be
  120. replaced by code that disables interrupts, then does the operations
  121. that are guaranteed to be atomic and then reenable interrupts. Doing
  122. so is expensive. If there are other reasons why the scheduler cannot
  123. change the processor we are executing on then there is no reason to
  124. disable interrupts. For that purpose the __this_cpu operations are
  125. provided. For example.
  126. __this_cpu_inc(x);
  127. Will increment x and will not fallback to code that disables
  128. interrupts on platforms that cannot accomplish atomicity through
  129. address relocation and a Read-Modify-Write operation in the same
  130. instruction.
  131. &this_cpu_ptr(pp)->n vs this_cpu_ptr(&pp->n)
  132. --------------------------------------------
  133. The first operation takes the offset and forms an address and then
  134. adds the offset of the n field.
  135. The second one first adds the two offsets and then does the
  136. relocation. IMHO the second form looks cleaner and has an easier time
  137. with (). The second form also is consistent with the way
  138. this_cpu_read() and friends are used.
  139. Christoph Lameter, April 3rd, 2013