18c2ecf20Sopenharmony_ci 28c2ecf20Sopenharmony_ci.. _local_ops: 38c2ecf20Sopenharmony_ci 48c2ecf20Sopenharmony_ci================================================= 58c2ecf20Sopenharmony_ciSemantics and Behavior of Local Atomic Operations 68c2ecf20Sopenharmony_ci================================================= 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci:Author: Mathieu Desnoyers 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ciThis document explains the purpose of the local atomic operations, how 128c2ecf20Sopenharmony_cito implement them for any given architecture and shows how they can be used 138c2ecf20Sopenharmony_ciproperly. It also stresses on the precautions that must be taken when reading 148c2ecf20Sopenharmony_cithose local variables across CPUs when the order of memory writes matters. 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci.. note:: 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci Note that ``local_t`` based operations are not recommended for general 198c2ecf20Sopenharmony_ci kernel use. Please use the ``this_cpu`` operations instead unless there is 208c2ecf20Sopenharmony_ci really a special purpose. Most uses of ``local_t`` in the kernel have been 218c2ecf20Sopenharmony_ci replaced by ``this_cpu`` operations. ``this_cpu`` operations combine the 228c2ecf20Sopenharmony_ci relocation with the ``local_t`` like semantics in a single instruction and 238c2ecf20Sopenharmony_ci yield more compact and faster executing code. 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ciPurpose of local atomic operations 278c2ecf20Sopenharmony_ci================================== 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ciLocal atomic operations are meant to provide fast and highly reentrant per CPU 308c2ecf20Sopenharmony_cicounters. They minimize the performance cost of standard atomic operations by 318c2ecf20Sopenharmony_ciremoving the LOCK prefix and memory barriers normally required to synchronize 328c2ecf20Sopenharmony_ciacross CPUs. 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ciHaving fast per CPU atomic counters is interesting in many cases: it does not 358c2ecf20Sopenharmony_cirequire disabling interrupts to protect from interrupt handlers and it permits 368c2ecf20Sopenharmony_cicoherent counters in NMI handlers. It is especially useful for tracing purposes 378c2ecf20Sopenharmony_ciand for various performance monitoring counters. 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ciLocal atomic operations only guarantee variable modification atomicity wrt the 408c2ecf20Sopenharmony_ciCPU which owns the data. Therefore, care must taken to make sure that only one 418c2ecf20Sopenharmony_ciCPU writes to the ``local_t`` data. This is done by using per cpu data and 428c2ecf20Sopenharmony_cimaking sure that we modify it from within a preemption safe context. It is 438c2ecf20Sopenharmony_cihowever permitted to read ``local_t`` data from any CPU: it will then appear to 448c2ecf20Sopenharmony_cibe written out of order wrt other memory writes by the owner CPU. 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ciImplementation for a given architecture 488c2ecf20Sopenharmony_ci======================================= 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ciIt can be done by slightly modifying the standard atomic operations: only 518c2ecf20Sopenharmony_citheir UP variant must be kept. It typically means removing LOCK prefix (on 528c2ecf20Sopenharmony_cii386 and x86_64) and any SMP synchronization barrier. If the architecture does 538c2ecf20Sopenharmony_cinot have a different behavior between SMP and UP, including 548c2ecf20Sopenharmony_ci``asm-generic/local.h`` in your architecture's ``local.h`` is sufficient. 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ciThe ``local_t`` type is defined as an opaque ``signed long`` by embedding an 578c2ecf20Sopenharmony_ci``atomic_long_t`` inside a structure. This is made so a cast from this type to 588c2ecf20Sopenharmony_cia ``long`` fails. The definition looks like:: 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci typedef struct { atomic_long_t a; } local_t; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ciRules to follow when using local atomic operations 648c2ecf20Sopenharmony_ci================================================== 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci* Variables touched by local ops must be per cpu variables. 678c2ecf20Sopenharmony_ci* *Only* the CPU owner of these variables must write to them. 688c2ecf20Sopenharmony_ci* This CPU can use local ops from any context (process, irq, softirq, nmi, ...) 698c2ecf20Sopenharmony_ci to update its ``local_t`` variables. 708c2ecf20Sopenharmony_ci* Preemption (or interrupts) must be disabled when using local ops in 718c2ecf20Sopenharmony_ci process context to make sure the process won't be migrated to a 728c2ecf20Sopenharmony_ci different CPU between getting the per-cpu variable and doing the 738c2ecf20Sopenharmony_ci actual local op. 748c2ecf20Sopenharmony_ci* When using local ops in interrupt context, no special care must be 758c2ecf20Sopenharmony_ci taken on a mainline kernel, since they will run on the local CPU with 768c2ecf20Sopenharmony_ci preemption already disabled. I suggest, however, to explicitly 778c2ecf20Sopenharmony_ci disable preemption anyway to make sure it will still work correctly on 788c2ecf20Sopenharmony_ci -rt kernels. 798c2ecf20Sopenharmony_ci* Reading the local cpu variable will provide the current copy of the 808c2ecf20Sopenharmony_ci variable. 818c2ecf20Sopenharmony_ci* Reads of these variables can be done from any CPU, because updates to 828c2ecf20Sopenharmony_ci "``long``", aligned, variables are always atomic. Since no memory 838c2ecf20Sopenharmony_ci synchronization is done by the writer CPU, an outdated copy of the 848c2ecf20Sopenharmony_ci variable can be read when reading some *other* cpu's variables. 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ciHow to use local atomic operations 888c2ecf20Sopenharmony_ci================================== 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci:: 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci #include <linux/percpu.h> 938c2ecf20Sopenharmony_ci #include <asm/local.h> 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci static DEFINE_PER_CPU(local_t, counters) = LOCAL_INIT(0); 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ciCounting 998c2ecf20Sopenharmony_ci======== 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ciCounting is done on all the bits of a signed long. 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ciIn preemptible context, use ``get_cpu_var()`` and ``put_cpu_var()`` around 1048c2ecf20Sopenharmony_cilocal atomic operations: it makes sure that preemption is disabled around write 1058c2ecf20Sopenharmony_ciaccess to the per cpu variable. For instance:: 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci local_inc(&get_cpu_var(counters)); 1088c2ecf20Sopenharmony_ci put_cpu_var(counters); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ciIf you are already in a preemption-safe context, you can use 1118c2ecf20Sopenharmony_ci``this_cpu_ptr()`` instead:: 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci local_inc(this_cpu_ptr(&counters)); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ciReading the counters 1188c2ecf20Sopenharmony_ci==================== 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ciThose local counters can be read from foreign CPUs to sum the count. Note that 1218c2ecf20Sopenharmony_cithe data seen by local_read across CPUs must be considered to be out of order 1228c2ecf20Sopenharmony_cirelatively to other memory writes happening on the CPU that owns the data:: 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci long sum = 0; 1258c2ecf20Sopenharmony_ci for_each_online_cpu(cpu) 1268c2ecf20Sopenharmony_ci sum += local_read(&per_cpu(counters, cpu)); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ciIf you want to use a remote local_read to synchronize access to a resource 1298c2ecf20Sopenharmony_cibetween CPUs, explicit ``smp_wmb()`` and ``smp_rmb()`` memory barriers must be used 1308c2ecf20Sopenharmony_cirespectively on the writer and the reader CPUs. It would be the case if you use 1318c2ecf20Sopenharmony_cithe ``local_t`` variable as a counter of bytes written in a buffer: there should 1328c2ecf20Sopenharmony_cibe a ``smp_wmb()`` between the buffer write and the counter increment and also a 1338c2ecf20Sopenharmony_ci``smp_rmb()`` between the counter read and the buffer read. 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ciHere is a sample module which implements a basic per cpu counter using 1378c2ecf20Sopenharmony_ci``local.h``:: 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci /* test-local.c 1408c2ecf20Sopenharmony_ci * 1418c2ecf20Sopenharmony_ci * Sample module for local.h usage. 1428c2ecf20Sopenharmony_ci */ 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci #include <asm/local.h> 1468c2ecf20Sopenharmony_ci #include <linux/module.h> 1478c2ecf20Sopenharmony_ci #include <linux/timer.h> 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci static DEFINE_PER_CPU(local_t, counters) = LOCAL_INIT(0); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci static struct timer_list test_timer; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci /* IPI called on each CPU. */ 1548c2ecf20Sopenharmony_ci static void test_each(void *info) 1558c2ecf20Sopenharmony_ci { 1568c2ecf20Sopenharmony_ci /* Increment the counter from a non preemptible context */ 1578c2ecf20Sopenharmony_ci printk("Increment on cpu %d\n", smp_processor_id()); 1588c2ecf20Sopenharmony_ci local_inc(this_cpu_ptr(&counters)); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci /* This is what incrementing the variable would look like within a 1618c2ecf20Sopenharmony_ci * preemptible context (it disables preemption) : 1628c2ecf20Sopenharmony_ci * 1638c2ecf20Sopenharmony_ci * local_inc(&get_cpu_var(counters)); 1648c2ecf20Sopenharmony_ci * put_cpu_var(counters); 1658c2ecf20Sopenharmony_ci */ 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci static void do_test_timer(unsigned long data) 1698c2ecf20Sopenharmony_ci { 1708c2ecf20Sopenharmony_ci int cpu; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci /* Increment the counters */ 1738c2ecf20Sopenharmony_ci on_each_cpu(test_each, NULL, 1); 1748c2ecf20Sopenharmony_ci /* Read all the counters */ 1758c2ecf20Sopenharmony_ci printk("Counters read from CPU %d\n", smp_processor_id()); 1768c2ecf20Sopenharmony_ci for_each_online_cpu(cpu) { 1778c2ecf20Sopenharmony_ci printk("Read : CPU %d, count %ld\n", cpu, 1788c2ecf20Sopenharmony_ci local_read(&per_cpu(counters, cpu))); 1798c2ecf20Sopenharmony_ci } 1808c2ecf20Sopenharmony_ci mod_timer(&test_timer, jiffies + 1000); 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci static int __init test_init(void) 1848c2ecf20Sopenharmony_ci { 1858c2ecf20Sopenharmony_ci /* initialize the timer that will increment the counter */ 1868c2ecf20Sopenharmony_ci timer_setup(&test_timer, do_test_timer, 0); 1878c2ecf20Sopenharmony_ci mod_timer(&test_timer, jiffies + 1); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci return 0; 1908c2ecf20Sopenharmony_ci } 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci static void __exit test_exit(void) 1938c2ecf20Sopenharmony_ci { 1948c2ecf20Sopenharmony_ci del_timer_sync(&test_timer); 1958c2ecf20Sopenharmony_ci } 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci module_init(test_init); 1988c2ecf20Sopenharmony_ci module_exit(test_exit); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci MODULE_LICENSE("GPL"); 2018c2ecf20Sopenharmony_ci MODULE_AUTHOR("Mathieu Desnoyers"); 2028c2ecf20Sopenharmony_ci MODULE_DESCRIPTION("Local Atomic Ops"); 203