local_ops.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 on 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 sychronization barrier. If the architecture does
  26. not have a different behavior between SMP and UP, including asm-generic/local.h
  27. in your archtecture'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. * How to use local atomic operations
  33. #include <linux/percpu.h>
  34. #include <asm/local.h>
  35. static DEFINE_PER_CPU(local_t, counters) = LOCAL_INIT(0);
  36. * Counting
  37. Counting is done on all the bits of a signed long.
  38. In preemptible context, use get_cpu_var() and put_cpu_var() around local atomic
  39. operations : it makes sure that preemption is disabled around write access to
  40. the per cpu variable. For instance :
  41. local_inc(&get_cpu_var(counters));
  42. put_cpu_var(counters);
  43. If you are already in a preemption-safe context, you can directly use
  44. __get_cpu_var() instead.
  45. local_inc(&__get_cpu_var(counters));
  46. * Reading the counters
  47. Those local counters can be read from foreign CPUs to sum the count. Note that
  48. the data seen by local_read across CPUs must be considered to be out of order
  49. relatively to other memory writes happening on the CPU that owns the data.
  50. long sum = 0;
  51. for_each_online_cpu(cpu)
  52. sum += local_read(&per_cpu(counters, cpu));
  53. If you want to use a remote local_read to synchronize access to a resource
  54. between CPUs, explicit smp_wmb() and smp_rmb() memory barriers must be used
  55. respectively on the writer and the reader CPUs. It would be the case if you use
  56. the local_t variable as a counter of bytes written in a buffer : there should
  57. be a smp_wmb() between the buffer write and the counter increment and also a
  58. smp_rmb() between the counter read and the buffer read.
  59. Here is a sample module which implements a basic per cpu counter using local.h.
  60. --- BEGIN ---
  61. /* test-local.c
  62. *
  63. * Sample module for local.h usage.
  64. */
  65. #include <asm/local.h>
  66. #include <linux/module.h>
  67. #include <linux/timer.h>
  68. static DEFINE_PER_CPU(local_t, counters) = LOCAL_INIT(0);
  69. static struct timer_list test_timer;
  70. /* IPI called on each CPU. */
  71. static void test_each(void *info)
  72. {
  73. /* Increment the counter from a non preemptible context */
  74. printk("Increment on cpu %d\n", smp_processor_id());
  75. local_inc(&__get_cpu_var(counters));
  76. /* This is what incrementing the variable would look like within a
  77. * preemptible context (it disables preemption) :
  78. *
  79. * local_inc(&get_cpu_var(counters));
  80. * put_cpu_var(counters);
  81. */
  82. }
  83. static void do_test_timer(unsigned long data)
  84. {
  85. int cpu;
  86. /* Increment the counters */
  87. on_each_cpu(test_each, NULL, 0, 1);
  88. /* Read all the counters */
  89. printk("Counters read from CPU %d\n", smp_processor_id());
  90. for_each_online_cpu(cpu) {
  91. printk("Read : CPU %d, count %ld\n", cpu,
  92. local_read(&per_cpu(counters, cpu)));
  93. }
  94. del_timer(&test_timer);
  95. test_timer.expires = jiffies + 1000;
  96. add_timer(&test_timer);
  97. }
  98. static int __init test_init(void)
  99. {
  100. /* initialize the timer that will increment the counter */
  101. init_timer(&test_timer);
  102. test_timer.function = do_test_timer;
  103. test_timer.expires = jiffies + 1;
  104. add_timer(&test_timer);
  105. return 0;
  106. }
  107. static void __exit test_exit(void)
  108. {
  109. del_timer_sync(&test_timer);
  110. }
  111. module_init(test_init);
  112. module_exit(test_exit);
  113. MODULE_LICENSE("GPL");
  114. MODULE_AUTHOR("Mathieu Desnoyers");
  115. MODULE_DESCRIPTION("Local Atomic Ops");
  116. --- END ---