162306a36Sopenharmony_ci
262306a36Sopenharmony_ci.. _local_ops:
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci=================================================
562306a36Sopenharmony_ciSemantics and Behavior of Local Atomic Operations
662306a36Sopenharmony_ci=================================================
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci:Author: Mathieu Desnoyers
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciThis document explains the purpose of the local atomic operations, how
1262306a36Sopenharmony_cito implement them for any given architecture and shows how they can be used
1362306a36Sopenharmony_ciproperly. It also stresses on the precautions that must be taken when reading
1462306a36Sopenharmony_cithose local variables across CPUs when the order of memory writes matters.
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci.. note::
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci    Note that ``local_t`` based operations are not recommended for general
1962306a36Sopenharmony_ci    kernel use. Please use the ``this_cpu`` operations instead unless there is
2062306a36Sopenharmony_ci    really a special purpose. Most uses of ``local_t`` in the kernel have been
2162306a36Sopenharmony_ci    replaced by ``this_cpu`` operations. ``this_cpu`` operations combine the
2262306a36Sopenharmony_ci    relocation with the ``local_t`` like semantics in a single instruction and
2362306a36Sopenharmony_ci    yield more compact and faster executing code.
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ciPurpose of local atomic operations
2762306a36Sopenharmony_ci==================================
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciLocal atomic operations are meant to provide fast and highly reentrant per CPU
3062306a36Sopenharmony_cicounters. They minimize the performance cost of standard atomic operations by
3162306a36Sopenharmony_ciremoving the LOCK prefix and memory barriers normally required to synchronize
3262306a36Sopenharmony_ciacross CPUs.
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ciHaving fast per CPU atomic counters is interesting in many cases: it does not
3562306a36Sopenharmony_cirequire disabling interrupts to protect from interrupt handlers and it permits
3662306a36Sopenharmony_cicoherent counters in NMI handlers. It is especially useful for tracing purposes
3762306a36Sopenharmony_ciand for various performance monitoring counters.
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ciLocal atomic operations only guarantee variable modification atomicity wrt the
4062306a36Sopenharmony_ciCPU which owns the data. Therefore, care must taken to make sure that only one
4162306a36Sopenharmony_ciCPU writes to the ``local_t`` data. This is done by using per cpu data and
4262306a36Sopenharmony_cimaking sure that we modify it from within a preemption safe context. It is
4362306a36Sopenharmony_cihowever permitted to read ``local_t`` data from any CPU: it will then appear to
4462306a36Sopenharmony_cibe written out of order wrt other memory writes by the owner CPU.
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciImplementation for a given architecture
4862306a36Sopenharmony_ci=======================================
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ciIt can be done by slightly modifying the standard atomic operations: only
5162306a36Sopenharmony_citheir UP variant must be kept. It typically means removing LOCK prefix (on
5262306a36Sopenharmony_cii386 and x86_64) and any SMP synchronization barrier. If the architecture does
5362306a36Sopenharmony_cinot have a different behavior between SMP and UP, including
5462306a36Sopenharmony_ci``asm-generic/local.h`` in your architecture's ``local.h`` is sufficient.
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ciThe ``local_t`` type is defined as an opaque ``signed long`` by embedding an
5762306a36Sopenharmony_ci``atomic_long_t`` inside a structure. This is made so a cast from this type to
5862306a36Sopenharmony_cia ``long`` fails. The definition looks like::
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci    typedef struct { atomic_long_t a; } local_t;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciRules to follow when using local atomic operations
6462306a36Sopenharmony_ci==================================================
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci* Variables touched by local ops must be per cpu variables.
6762306a36Sopenharmony_ci* *Only* the CPU owner of these variables must write to them.
6862306a36Sopenharmony_ci* This CPU can use local ops from any context (process, irq, softirq, nmi, ...)
6962306a36Sopenharmony_ci  to update its ``local_t`` variables.
7062306a36Sopenharmony_ci* Preemption (or interrupts) must be disabled when using local ops in
7162306a36Sopenharmony_ci  process context to make sure the process won't be migrated to a
7262306a36Sopenharmony_ci  different CPU between getting the per-cpu variable and doing the
7362306a36Sopenharmony_ci  actual local op.
7462306a36Sopenharmony_ci* When using local ops in interrupt context, no special care must be
7562306a36Sopenharmony_ci  taken on a mainline kernel, since they will run on the local CPU with
7662306a36Sopenharmony_ci  preemption already disabled. I suggest, however, to explicitly
7762306a36Sopenharmony_ci  disable preemption anyway to make sure it will still work correctly on
7862306a36Sopenharmony_ci  -rt kernels.
7962306a36Sopenharmony_ci* Reading the local cpu variable will provide the current copy of the
8062306a36Sopenharmony_ci  variable.
8162306a36Sopenharmony_ci* Reads of these variables can be done from any CPU, because updates to
8262306a36Sopenharmony_ci  "``long``", aligned, variables are always atomic. Since no memory
8362306a36Sopenharmony_ci  synchronization is done by the writer CPU, an outdated copy of the
8462306a36Sopenharmony_ci  variable can be read when reading some *other* cpu's variables.
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciHow to use local atomic operations
8862306a36Sopenharmony_ci==================================
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci::
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci    #include <linux/percpu.h>
9362306a36Sopenharmony_ci    #include <asm/local.h>
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci    static DEFINE_PER_CPU(local_t, counters) = LOCAL_INIT(0);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ciCounting
9962306a36Sopenharmony_ci========
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ciCounting is done on all the bits of a signed long.
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ciIn preemptible context, use ``get_cpu_var()`` and ``put_cpu_var()`` around
10462306a36Sopenharmony_cilocal atomic operations: it makes sure that preemption is disabled around write
10562306a36Sopenharmony_ciaccess to the per cpu variable. For instance::
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci    local_inc(&get_cpu_var(counters));
10862306a36Sopenharmony_ci    put_cpu_var(counters);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciIf you are already in a preemption-safe context, you can use
11162306a36Sopenharmony_ci``this_cpu_ptr()`` instead::
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci    local_inc(this_cpu_ptr(&counters));
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ciReading the counters
11862306a36Sopenharmony_ci====================
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ciThose local counters can be read from foreign CPUs to sum the count. Note that
12162306a36Sopenharmony_cithe data seen by local_read across CPUs must be considered to be out of order
12262306a36Sopenharmony_cirelatively to other memory writes happening on the CPU that owns the data::
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci    long sum = 0;
12562306a36Sopenharmony_ci    for_each_online_cpu(cpu)
12662306a36Sopenharmony_ci            sum += local_read(&per_cpu(counters, cpu));
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciIf you want to use a remote local_read to synchronize access to a resource
12962306a36Sopenharmony_cibetween CPUs, explicit ``smp_wmb()`` and ``smp_rmb()`` memory barriers must be used
13062306a36Sopenharmony_cirespectively on the writer and the reader CPUs. It would be the case if you use
13162306a36Sopenharmony_cithe ``local_t`` variable as a counter of bytes written in a buffer: there should
13262306a36Sopenharmony_cibe a ``smp_wmb()`` between the buffer write and the counter increment and also a
13362306a36Sopenharmony_ci``smp_rmb()`` between the counter read and the buffer read.
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ciHere is a sample module which implements a basic per cpu counter using
13762306a36Sopenharmony_ci``local.h``::
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci    /* test-local.c
14062306a36Sopenharmony_ci     *
14162306a36Sopenharmony_ci     * Sample module for local.h usage.
14262306a36Sopenharmony_ci     */
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci    #include <asm/local.h>
14662306a36Sopenharmony_ci    #include <linux/module.h>
14762306a36Sopenharmony_ci    #include <linux/timer.h>
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci    static DEFINE_PER_CPU(local_t, counters) = LOCAL_INIT(0);
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci    static struct timer_list test_timer;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci    /* IPI called on each CPU. */
15462306a36Sopenharmony_ci    static void test_each(void *info)
15562306a36Sopenharmony_ci    {
15662306a36Sopenharmony_ci            /* Increment the counter from a non preemptible context */
15762306a36Sopenharmony_ci            printk("Increment on cpu %d\n", smp_processor_id());
15862306a36Sopenharmony_ci            local_inc(this_cpu_ptr(&counters));
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci            /* This is what incrementing the variable would look like within a
16162306a36Sopenharmony_ci             * preemptible context (it disables preemption) :
16262306a36Sopenharmony_ci             *
16362306a36Sopenharmony_ci             * local_inc(&get_cpu_var(counters));
16462306a36Sopenharmony_ci             * put_cpu_var(counters);
16562306a36Sopenharmony_ci             */
16662306a36Sopenharmony_ci    }
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci    static void do_test_timer(unsigned long data)
16962306a36Sopenharmony_ci    {
17062306a36Sopenharmony_ci            int cpu;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci            /* Increment the counters */
17362306a36Sopenharmony_ci            on_each_cpu(test_each, NULL, 1);
17462306a36Sopenharmony_ci            /* Read all the counters */
17562306a36Sopenharmony_ci            printk("Counters read from CPU %d\n", smp_processor_id());
17662306a36Sopenharmony_ci            for_each_online_cpu(cpu) {
17762306a36Sopenharmony_ci                    printk("Read : CPU %d, count %ld\n", cpu,
17862306a36Sopenharmony_ci                            local_read(&per_cpu(counters, cpu)));
17962306a36Sopenharmony_ci            }
18062306a36Sopenharmony_ci            mod_timer(&test_timer, jiffies + 1000);
18162306a36Sopenharmony_ci    }
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci    static int __init test_init(void)
18462306a36Sopenharmony_ci    {
18562306a36Sopenharmony_ci            /* initialize the timer that will increment the counter */
18662306a36Sopenharmony_ci            timer_setup(&test_timer, do_test_timer, 0);
18762306a36Sopenharmony_ci            mod_timer(&test_timer, jiffies + 1);
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci            return 0;
19062306a36Sopenharmony_ci    }
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci    static void __exit test_exit(void)
19362306a36Sopenharmony_ci    {
19462306a36Sopenharmony_ci            timer_shutdown_sync(&test_timer);
19562306a36Sopenharmony_ci    }
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci    module_init(test_init);
19862306a36Sopenharmony_ci    module_exit(test_exit);
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci    MODULE_LICENSE("GPL");
20162306a36Sopenharmony_ci    MODULE_AUTHOR("Mathieu Desnoyers");
20262306a36Sopenharmony_ci    MODULE_DESCRIPTION("Local Atomic Ops");
203