local_ops.rst 7.2 KB

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