xref: /openbmc/linux/Documentation/core-api/local_ops.rst (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1c232694eSSilvio Fricke
2c232694eSSilvio Fricke.. _local_ops:
3c232694eSSilvio Fricke
4c232694eSSilvio Fricke=================================================
5c232694eSSilvio FrickeSemantics and Behavior of Local Atomic Operations
6c232694eSSilvio Fricke=================================================
7c232694eSSilvio Fricke
8c232694eSSilvio Fricke:Author: Mathieu Desnoyers
9c232694eSSilvio Fricke
10c232694eSSilvio Fricke
11c232694eSSilvio FrickeThis document explains the purpose of the local atomic operations, how
12c232694eSSilvio Fricketo implement them for any given architecture and shows how they can be used
13c232694eSSilvio Frickeproperly. It also stresses on the precautions that must be taken when reading
14c232694eSSilvio Frickethose local variables across CPUs when the order of memory writes matters.
15c232694eSSilvio Fricke
16c232694eSSilvio Fricke.. note::
17c232694eSSilvio Fricke
18c232694eSSilvio Fricke    Note that ``local_t`` based operations are not recommended for general
19c232694eSSilvio Fricke    kernel use. Please use the ``this_cpu`` operations instead unless there is
20c232694eSSilvio Fricke    really a special purpose. Most uses of ``local_t`` in the kernel have been
21c232694eSSilvio Fricke    replaced by ``this_cpu`` operations. ``this_cpu`` operations combine the
22c232694eSSilvio Fricke    relocation with the ``local_t`` like semantics in a single instruction and
23c232694eSSilvio Fricke    yield more compact and faster executing code.
24c232694eSSilvio Fricke
25c232694eSSilvio Fricke
26c232694eSSilvio FrickePurpose of local atomic operations
27c232694eSSilvio Fricke==================================
28c232694eSSilvio Fricke
29c232694eSSilvio FrickeLocal atomic operations are meant to provide fast and highly reentrant per CPU
30c232694eSSilvio Frickecounters. They minimize the performance cost of standard atomic operations by
31c232694eSSilvio Frickeremoving the LOCK prefix and memory barriers normally required to synchronize
32c232694eSSilvio Frickeacross CPUs.
33c232694eSSilvio Fricke
34c232694eSSilvio FrickeHaving fast per CPU atomic counters is interesting in many cases: it does not
35c232694eSSilvio Frickerequire disabling interrupts to protect from interrupt handlers and it permits
36c232694eSSilvio Frickecoherent counters in NMI handlers. It is especially useful for tracing purposes
37c232694eSSilvio Frickeand for various performance monitoring counters.
38c232694eSSilvio Fricke
39c232694eSSilvio FrickeLocal atomic operations only guarantee variable modification atomicity wrt the
40c232694eSSilvio FrickeCPU which owns the data. Therefore, care must taken to make sure that only one
41c232694eSSilvio FrickeCPU writes to the ``local_t`` data. This is done by using per cpu data and
42c232694eSSilvio Frickemaking sure that we modify it from within a preemption safe context. It is
43c232694eSSilvio Frickehowever permitted to read ``local_t`` data from any CPU: it will then appear to
44c232694eSSilvio Frickebe written out of order wrt other memory writes by the owner CPU.
45c232694eSSilvio Fricke
46c232694eSSilvio Fricke
47c232694eSSilvio FrickeImplementation for a given architecture
48c232694eSSilvio Fricke=======================================
49c232694eSSilvio Fricke
50c232694eSSilvio FrickeIt can be done by slightly modifying the standard atomic operations: only
51c232694eSSilvio Fricketheir UP variant must be kept. It typically means removing LOCK prefix (on
52c232694eSSilvio Frickei386 and x86_64) and any SMP synchronization barrier. If the architecture does
53c232694eSSilvio Frickenot have a different behavior between SMP and UP, including
54c232694eSSilvio Fricke``asm-generic/local.h`` in your architecture's ``local.h`` is sufficient.
55c232694eSSilvio Fricke
56c232694eSSilvio FrickeThe ``local_t`` type is defined as an opaque ``signed long`` by embedding an
57c232694eSSilvio Fricke``atomic_long_t`` inside a structure. This is made so a cast from this type to
58c232694eSSilvio Frickea ``long`` fails. The definition looks like::
59c232694eSSilvio Fricke
60c232694eSSilvio Fricke    typedef struct { atomic_long_t a; } local_t;
61c232694eSSilvio Fricke
62c232694eSSilvio Fricke
63c232694eSSilvio FrickeRules to follow when using local atomic operations
64c232694eSSilvio Fricke==================================================
65c232694eSSilvio Fricke
66c232694eSSilvio Fricke* Variables touched by local ops must be per cpu variables.
67c232694eSSilvio Fricke* *Only* the CPU owner of these variables must write to them.
68c232694eSSilvio Fricke* This CPU can use local ops from any context (process, irq, softirq, nmi, ...)
69c232694eSSilvio Fricke  to update its ``local_t`` variables.
70c232694eSSilvio Fricke* Preemption (or interrupts) must be disabled when using local ops in
71c232694eSSilvio Fricke  process context to make sure the process won't be migrated to a
72c232694eSSilvio Fricke  different CPU between getting the per-cpu variable and doing the
73c232694eSSilvio Fricke  actual local op.
74c232694eSSilvio Fricke* When using local ops in interrupt context, no special care must be
75c232694eSSilvio Fricke  taken on a mainline kernel, since they will run on the local CPU with
76c232694eSSilvio Fricke  preemption already disabled. I suggest, however, to explicitly
77c232694eSSilvio Fricke  disable preemption anyway to make sure it will still work correctly on
78c232694eSSilvio Fricke  -rt kernels.
79c232694eSSilvio Fricke* Reading the local cpu variable will provide the current copy of the
80c232694eSSilvio Fricke  variable.
81c232694eSSilvio Fricke* Reads of these variables can be done from any CPU, because updates to
82c232694eSSilvio Fricke  "``long``", aligned, variables are always atomic. Since no memory
83c232694eSSilvio Fricke  synchronization is done by the writer CPU, an outdated copy of the
84c232694eSSilvio Fricke  variable can be read when reading some *other* cpu's variables.
85c232694eSSilvio Fricke
86c232694eSSilvio Fricke
87c232694eSSilvio FrickeHow to use local atomic operations
88c232694eSSilvio Fricke==================================
89c232694eSSilvio Fricke
90c232694eSSilvio Fricke::
91c232694eSSilvio Fricke
92c232694eSSilvio Fricke    #include <linux/percpu.h>
93c232694eSSilvio Fricke    #include <asm/local.h>
94c232694eSSilvio Fricke
95c232694eSSilvio Fricke    static DEFINE_PER_CPU(local_t, counters) = LOCAL_INIT(0);
96c232694eSSilvio Fricke
97c232694eSSilvio Fricke
98c232694eSSilvio FrickeCounting
99c232694eSSilvio Fricke========
100c232694eSSilvio Fricke
101c232694eSSilvio FrickeCounting is done on all the bits of a signed long.
102c232694eSSilvio Fricke
103c232694eSSilvio FrickeIn preemptible context, use ``get_cpu_var()`` and ``put_cpu_var()`` around
104c232694eSSilvio Frickelocal atomic operations: it makes sure that preemption is disabled around write
105c232694eSSilvio Frickeaccess to the per cpu variable. For instance::
106c232694eSSilvio Fricke
107c232694eSSilvio Fricke    local_inc(&get_cpu_var(counters));
108c232694eSSilvio Fricke    put_cpu_var(counters);
109c232694eSSilvio Fricke
110c232694eSSilvio FrickeIf you are already in a preemption-safe context, you can use
111c232694eSSilvio Fricke``this_cpu_ptr()`` instead::
112c232694eSSilvio Fricke
113c232694eSSilvio Fricke    local_inc(this_cpu_ptr(&counters));
114c232694eSSilvio Fricke
115c232694eSSilvio Fricke
116c232694eSSilvio Fricke
117c232694eSSilvio FrickeReading the counters
118c232694eSSilvio Fricke====================
119c232694eSSilvio Fricke
120c232694eSSilvio FrickeThose local counters can be read from foreign CPUs to sum the count. Note that
121c232694eSSilvio Frickethe data seen by local_read across CPUs must be considered to be out of order
122c232694eSSilvio Frickerelatively to other memory writes happening on the CPU that owns the data::
123c232694eSSilvio Fricke
124c232694eSSilvio Fricke    long sum = 0;
125c232694eSSilvio Fricke    for_each_online_cpu(cpu)
126c232694eSSilvio Fricke            sum += local_read(&per_cpu(counters, cpu));
127c232694eSSilvio Fricke
128c232694eSSilvio FrickeIf you want to use a remote local_read to synchronize access to a resource
129c232694eSSilvio Frickebetween CPUs, explicit ``smp_wmb()`` and ``smp_rmb()`` memory barriers must be used
130c232694eSSilvio Frickerespectively on the writer and the reader CPUs. It would be the case if you use
131c232694eSSilvio Frickethe ``local_t`` variable as a counter of bytes written in a buffer: there should
132c232694eSSilvio Frickebe a ``smp_wmb()`` between the buffer write and the counter increment and also a
133c232694eSSilvio Fricke``smp_rmb()`` between the counter read and the buffer read.
134c232694eSSilvio Fricke
135c232694eSSilvio Fricke
136c232694eSSilvio FrickeHere is a sample module which implements a basic per cpu counter using
137c232694eSSilvio Fricke``local.h``::
138c232694eSSilvio Fricke
139c232694eSSilvio Fricke    /* test-local.c
140c232694eSSilvio Fricke     *
141c232694eSSilvio Fricke     * Sample module for local.h usage.
142c232694eSSilvio Fricke     */
143c232694eSSilvio Fricke
144c232694eSSilvio Fricke
145c232694eSSilvio Fricke    #include <asm/local.h>
146c232694eSSilvio Fricke    #include <linux/module.h>
147c232694eSSilvio Fricke    #include <linux/timer.h>
148c232694eSSilvio Fricke
149c232694eSSilvio Fricke    static DEFINE_PER_CPU(local_t, counters) = LOCAL_INIT(0);
150c232694eSSilvio Fricke
151c232694eSSilvio Fricke    static struct timer_list test_timer;
152c232694eSSilvio Fricke
153c232694eSSilvio Fricke    /* IPI called on each CPU. */
154c232694eSSilvio Fricke    static void test_each(void *info)
155c232694eSSilvio Fricke    {
156c232694eSSilvio Fricke            /* Increment the counter from a non preemptible context */
157c232694eSSilvio Fricke            printk("Increment on cpu %d\n", smp_processor_id());
158c232694eSSilvio Fricke            local_inc(this_cpu_ptr(&counters));
159c232694eSSilvio Fricke
160c232694eSSilvio Fricke            /* This is what incrementing the variable would look like within a
161c232694eSSilvio Fricke             * preemptible context (it disables preemption) :
162c232694eSSilvio Fricke             *
163c232694eSSilvio Fricke             * local_inc(&get_cpu_var(counters));
164c232694eSSilvio Fricke             * put_cpu_var(counters);
165c232694eSSilvio Fricke             */
166c232694eSSilvio Fricke    }
167c232694eSSilvio Fricke
168c232694eSSilvio Fricke    static void do_test_timer(unsigned long data)
169c232694eSSilvio Fricke    {
170c232694eSSilvio Fricke            int cpu;
171c232694eSSilvio Fricke
172c232694eSSilvio Fricke            /* Increment the counters */
173c232694eSSilvio Fricke            on_each_cpu(test_each, NULL, 1);
174c232694eSSilvio Fricke            /* Read all the counters */
175c232694eSSilvio Fricke            printk("Counters read from CPU %d\n", smp_processor_id());
176c232694eSSilvio Fricke            for_each_online_cpu(cpu) {
177c232694eSSilvio Fricke                    printk("Read : CPU %d, count %ld\n", cpu,
178c232694eSSilvio Fricke                            local_read(&per_cpu(counters, cpu)));
179c232694eSSilvio Fricke            }
1807eeb6b89SKees Cook            mod_timer(&test_timer, jiffies + 1000);
181c232694eSSilvio Fricke    }
182c232694eSSilvio Fricke
183c232694eSSilvio Fricke    static int __init test_init(void)
184c232694eSSilvio Fricke    {
185c232694eSSilvio Fricke            /* initialize the timer that will increment the counter */
1867eeb6b89SKees Cook            timer_setup(&test_timer, do_test_timer, 0);
1877eeb6b89SKees Cook            mod_timer(&test_timer, jiffies + 1);
188c232694eSSilvio Fricke
189c232694eSSilvio Fricke            return 0;
190c232694eSSilvio Fricke    }
191c232694eSSilvio Fricke
192c232694eSSilvio Fricke    static void __exit test_exit(void)
193c232694eSSilvio Fricke    {
194*a31323beSSteven Rostedt (Google)            timer_shutdown_sync(&test_timer);
195c232694eSSilvio Fricke    }
196c232694eSSilvio Fricke
197c232694eSSilvio Fricke    module_init(test_init);
198c232694eSSilvio Fricke    module_exit(test_exit);
199c232694eSSilvio Fricke
200c232694eSSilvio Fricke    MODULE_LICENSE("GPL");
201c232694eSSilvio Fricke    MODULE_AUTHOR("Mathieu Desnoyers");
202c232694eSSilvio Fricke    MODULE_DESCRIPTION("Local Atomic Ops");
203