local_ops.txt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. Semantics and Behavior of Local Atomic Operations
  2. Mathieu Desnoyers
  3. This document explains the purpose of the local atomic operations, how
  4. to implement them for any given architecture and shows how they can be used
  5. properly. It also stresses on the precautions that must be taken when reading
  6. those local variables across CPUs when the order of memory writes matters.
  7. Note that local_t based operations are not recommended for general kernel use.
  8. Please use the this_cpu operations instead unless there is really a special purpose.
  9. Most uses of local_t in the kernel have been replaced by this_cpu operations.
  10. this_cpu operations combine the relocation with the local_t like semantics in
  11. a single instruction and yield more compact and faster executing code.
  12. * Purpose of local atomic operations
  13. Local atomic operations are meant to provide fast and highly reentrant per CPU
  14. counters. They minimize the performance cost of standard atomic operations by
  15. removing the LOCK prefix and memory barriers normally required to synchronize
  16. across CPUs.
  17. Having fast per CPU atomic counters is interesting in many cases : it does not
  18. require disabling interrupts to protect from interrupt handlers and it permits
  19. coherent counters in NMI handlers. It is especially useful for tracing purposes
  20. and for various performance monitoring counters.
  21. Local atomic operations only guarantee variable modification atomicity wrt the
  22. CPU which owns the data. Therefore, care must taken to make sure that only one
  23. CPU writes to the local_t data. This is done by using per cpu data and making
  24. sure that we modify it from within a preemption safe context. It is however
  25. permitted to read local_t data from any CPU : it will then appear to be written
  26. out of order wrt other memory writes by the owner CPU.
  27. * Implementation for a given architecture
  28. It can be done by slightly modifying the standard atomic operations : only
  29. their UP variant must be kept. It typically means removing LOCK prefix (on
  30. i386 and x86_64) and any SMP synchronization barrier. If the architecture does
  31. not have a different behavior between SMP and UP, including asm-generic/local.h
  32. in your architecture's local.h is sufficient.
  33. The local_t type is defined as an opaque signed long by embedding an
  34. atomic_long_t inside a structure. This is made so a cast from this type to a
  35. long fails. The definition looks like :
  36. typedef struct { atomic_long_t a; } local_t;
  37. * Rules to follow when using local atomic operations
  38. - Variables touched by local ops must be per cpu variables.
  39. - _Only_ the CPU owner of these variables must write to them.
  40. - This CPU can use local ops from any context (process, irq, softirq, nmi, ...)
  41. to update its local_t variables.
  42. - Preemption (or interrupts) must be disabled when using local ops in
  43. process context to make sure the process won't be migrated to a
  44. different CPU between getting the per-cpu variable and doing the
  45. actual local op.
  46. - When using local ops in interrupt context, no special care must be
  47. taken on a mainline kernel, since they will run on the local CPU with
  48. preemption already disabled. I suggest, however, to explicitly
  49. disable preemption anyway to make sure it will still work correctly on
  50. -rt kernels.
  51. - Reading the local cpu variable will provide the current copy of the
  52. variable.
  53. - Reads of these variables can be done from any CPU, because updates to
  54. "long", aligned, variables are always atomic. Since no memory
  55. synchronization is done by the writer CPU, an outdated copy of the
  56. variable can be read when reading some _other_ cpu's variables.
  57. * How to use local atomic operations
  58. #include <linux/percpu.h>
  59. #include <asm/local.h>
  60. static DEFINE_PER_CPU(local_t, counters) = LOCAL_INIT(0);
  61. * Counting
  62. Counting is done on all the bits of a signed long.
  63. In preemptible context, use get_cpu_var() and put_cpu_var() around local atomic
  64. operations : it makes sure that preemption is disabled around write access to
  65. the per cpu variable. For instance :
  66. local_inc(&get_cpu_var(counters));
  67. put_cpu_var(counters);
  68. If you are already in a preemption-safe context, you can use
  69. this_cpu_ptr() instead.
  70. local_inc(this_cpu_ptr(&counters));
  71. * Reading the counters
  72. Those local counters can be read from foreign CPUs to sum the count. Note that
  73. the data seen by local_read across CPUs must be considered to be out of order
  74. relatively to other memory writes happening on the CPU that owns the data.
  75. long sum = 0;
  76. for_each_online_cpu(cpu)
  77. sum += local_read(&per_cpu(counters, cpu));
  78. If you want to use a remote local_read to synchronize access to a resource
  79. between CPUs, explicit smp_wmb() and smp_rmb() memory barriers must be used
  80. respectively on the writer and the reader CPUs. It would be the case if you use
  81. the local_t variable as a counter of bytes written in a buffer : there should
  82. be a smp_wmb() between the buffer write and the counter increment and also a
  83. smp_rmb() between the counter read and the buffer read.
  84. Here is a sample module which implements a basic per cpu counter using local.h.
  85. --- BEGIN ---
  86. /* test-local.c
  87. *
  88. * Sample module for local.h usage.
  89. */
  90. #include <asm/local.h>
  91. #include <linux/module.h>
  92. #include <linux/timer.h>
  93. static DEFINE_PER_CPU(local_t, counters) = LOCAL_INIT(0);
  94. static struct timer_list test_timer;
  95. /* IPI called on each CPU. */
  96. static void test_each(void *info)
  97. {
  98. /* Increment the counter from a non preemptible context */
  99. printk("Increment on cpu %d\n", smp_processor_id());
  100. local_inc(this_cpu_ptr(&counters));
  101. /* This is what incrementing the variable would look like within a
  102. * preemptible context (it disables preemption) :
  103. *
  104. * local_inc(&get_cpu_var(counters));
  105. * put_cpu_var(counters);
  106. */
  107. }
  108. static void do_test_timer(unsigned long data)
  109. {
  110. int cpu;
  111. /* Increment the counters */
  112. on_each_cpu(test_each, NULL, 1);
  113. /* Read all the counters */
  114. printk("Counters read from CPU %d\n", smp_processor_id());
  115. for_each_online_cpu(cpu) {
  116. printk("Read : CPU %d, count %ld\n", cpu,
  117. local_read(&per_cpu(counters, cpu)));
  118. }
  119. del_timer(&test_timer);
  120. test_timer.expires = jiffies + 1000;
  121. add_timer(&test_timer);
  122. }
  123. static int __init test_init(void)
  124. {
  125. /* initialize the timer that will increment the counter */
  126. init_timer(&test_timer);
  127. test_timer.function = do_test_timer;
  128. test_timer.expires = jiffies + 1;
  129. add_timer(&test_timer);
  130. return 0;
  131. }
  132. static void __exit test_exit(void)
  133. {
  134. del_timer_sync(&test_timer);
  135. }
  136. module_init(test_init);
  137. module_exit(test_exit);
  138. MODULE_LICENSE("GPL");
  139. MODULE_AUTHOR("Mathieu Desnoyers");
  140. MODULE_DESCRIPTION("Local Atomic Ops");
  141. --- END ---