local_ops.txt 6.1 KB

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