tsphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget)/translations/zh_CN/core-api/this_cpu_opsmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget)/translations/zh_TW/core-api/this_cpu_opsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget)/translations/it_IT/core-api/this_cpu_opsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget)/translations/ja_JP/core-api/this_cpu_opsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget)/translations/ko_KR/core-api/this_cpu_opsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget)/translations/sp_SP/core-api/this_cpu_opsmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhsection)}(hhh](htitle)}(hthis_cpu operationsh]hthis_cpu operations}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhC/var/lib/git/docbuild/linux/Documentation/core-api/this_cpu_ops.rsthKubh field_list)}(hhh](hfield)}(hhh](h field_name)}(hAuthorh]hAuthor}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhKubh field_body)}(h#Christoph Lameter, August 4th, 2014h]h paragraph)}(hhh]h#Christoph Lameter, August 4th, 2014}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hhh](h)}(hAuthorh]hAuthor}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhKubh)}(hPranith Kumar, Aug 2nd, 2014 h]h)}(hPranith Kumar, Aug 2nd, 2014h]hPranith Kumar, Aug 2nd, 2014}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubeh}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh)}(hX%this_cpu operations are a way of optimizing access to per cpu variables associated with the *currently* executing processor. This is done through the use of segment registers (or a dedicated register where the cpu permanently stored the beginning of the per cpu area for a specific processor).h](h\this_cpu operations are a way of optimizing access to per cpu variables associated with the }(hj'hhhNhNubhemphasis)}(h *currently*h]h currently}(hj1hhhNhNubah}(h]h ]h"]h$]h&]uh1j/hj'ubh executing processor. This is done through the use of segment registers (or a dedicated register where the cpu permanently stored the beginning of the per cpu area for a specific processor).}(hj'hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hthis_cpu operations add a per cpu variable offset to the processor specific per cpu base and encode that operation in the instruction operating on the per cpu variable.h]hthis_cpu operations add a per cpu variable offset to the processor specific per cpu base and encode that operation in the instruction operating on the per cpu variable.}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hX&This means that there are no atomicity issues between the calculation of the offset and the operation on the data. Therefore it is not necessary to disable preemption or interrupts to ensure that the processor is not changed between the calculation of the address and the operation on the data.h]hX&This means that there are no atomicity issues between the calculation of the offset and the operation on the data. Therefore it is not necessary to disable preemption or interrupts to ensure that the processor is not changed between the calculation of the address and the operation on the data.}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hXRead-modify-write operations are of particular interest. Frequently processors have special lower latency instructions that can operate without the typical synchronization overhead, but still provide some sort of relaxed atomicity guarantees. The x86, for example, can execute RMW (Read Modify Write) instructions like inc/dec/cmpxchg without the lock prefix and the associated latency penalty.h]hXRead-modify-write operations are of particular interest. Frequently processors have special lower latency instructions that can operate without the typical synchronization overhead, but still provide some sort of relaxed atomicity guarantees. The x86, for example, can execute RMW (Read Modify Write) instructions like inc/dec/cmpxchg without the lock prefix and the associated latency penalty.}(hjehhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hXMAccess to the variable without the lock prefix is not synchronized but synchronization is not necessary since we are dealing with per cpu data specific to the currently executing processor. Only the current processor should be accessing that variable and therefore there are no concurrency issues with other processors in the system.h]hXMAccess to the variable without the lock prefix is not synchronized but synchronization is not necessary since we are dealing with per cpu data specific to the currently executing processor. Only the current processor should be accessing that variable and therefore there are no concurrency issues with other processors in the system.}(hjshhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hPlease note that accesses by remote processors to a per cpu area are exceptional situations and may impact performance and/or correctness (remote write operations) of local RMW operations via this_cpu_*.h]hPlease note that accesses by remote processors to a per cpu area are exceptional situations and may impact performance and/or correctness (remote write operations) of local RMW operations via this_cpu_*.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK%hhhhubh)}(hPThe main use of the this_cpu operations has been to optimize counter operations.h]hPThe main use of the this_cpu operations has been to optimize counter operations.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK)hhhhubh)}(hThe following this_cpu() operations with implied preemption protection are defined. These operations can be used without worrying about preemption and interrupts::h]hThe following this_cpu() operations with implied preemption protection are defined. These operations can be used without worrying about preemption and interrupts:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK,hhhhubh literal_block)}(hXSthis_cpu_read(pcp) this_cpu_write(pcp, val) this_cpu_add(pcp, val) this_cpu_and(pcp, val) this_cpu_or(pcp, val) this_cpu_add_return(pcp, val) this_cpu_xchg(pcp, nval) this_cpu_cmpxchg(pcp, oval, nval) this_cpu_sub(pcp, val) this_cpu_inc(pcp) this_cpu_dec(pcp) this_cpu_sub_return(pcp, val) this_cpu_inc_return(pcp) this_cpu_dec_return(pcp)h]hXSthis_cpu_read(pcp) this_cpu_write(pcp, val) this_cpu_add(pcp, val) this_cpu_and(pcp, val) this_cpu_or(pcp, val) this_cpu_add_return(pcp, val) this_cpu_xchg(pcp, nval) this_cpu_cmpxchg(pcp, oval, nval) this_cpu_sub(pcp, val) this_cpu_inc(pcp) this_cpu_dec(pcp) this_cpu_sub_return(pcp, val) this_cpu_inc_return(pcp) this_cpu_dec_return(pcp)}hjsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1jhhhK0hhhhubh)}(hhh](h)}(h$Inner working of this_cpu operationsh]h$Inner working of this_cpu operations}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKAubh)}(hX@On x86 the fs: or the gs: segment registers contain the base of the per cpu area. It is then possible to simply use the segment override to relocate a per cpu relative address to the proper per cpu area for the processor. So the relocation to the per cpu base is encoded in the instruction via a segment register prefix.h]hX@On x86 the fs: or the gs: segment registers contain the base of the per cpu area. It is then possible to simply use the segment override to relocate a per cpu relative address to the proper per cpu area for the processor. So the relocation to the per cpu base is encoded in the instruction via a segment register prefix.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKChjhhubh)}(h For example::h]h For example:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKIhjhhubj)}(h5DEFINE_PER_CPU(int, x); int z; z = this_cpu_read(x);h]h5DEFINE_PER_CPU(int, x); int z; z = this_cpu_read(x);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhKKhjhhubh)}(h!results in a single instruction::h]h results in a single instruction:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKPhjhhubj)}(hmov ax, gs:[x]h]hmov ax, gs:[x]}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhKRhjhhubh)}(hX1instead of a sequence of calculation of the address and then a fetch from that address which occurs with the per cpu operations. Before this_cpu_ops such sequence also required preempt disable/enable to prevent the kernel from moving the thread to a different processor while the calculation is performed.h]hX1instead of a sequence of calculation of the address and then a fetch from that address which occurs with the per cpu operations. Before this_cpu_ops such sequence also required preempt disable/enable to prevent the kernel from moving the thread to a different processor while the calculation is performed.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKThjhhubh)}(h+Consider the following this_cpu operation::h]h*Consider the following this_cpu operation:}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKZhjhhubj)}(hthis_cpu_inc(x)h]hthis_cpu_inc(x)}hj0sbah}(h]h ]h"]h$]h&]jjuh1jhhhK\hjhhubh)}(hIThe above results in the following single instruction (no lock prefix!)::h]hHThe above results in the following single instruction (no lock prefix!):}(hj>hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK^hjhhubj)}(h inc gs:[x]h]h inc gs:[x]}hjLsbah}(h]h ]h"]h$]h&]jjuh1jhhhK`hjhhubh)}(hNinstead of the following operations required if there is no segment register::h]hMinstead of the following operations required if there is no segment register:}(hjZhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKbhjhhubj)}(hOint *y; int cpu; cpu = get_cpu(); y = per_cpu_ptr(&x, cpu); (*y)++; put_cpu();h]hOint *y; int cpu; cpu = get_cpu(); y = per_cpu_ptr(&x, cpu); (*y)++; put_cpu();}hjhsbah}(h]h ]h"]h$]h&]jjuh1jhhhKehjhhubh)}(hX,Note that these operations can only be used on per cpu data that is reserved for a specific processor. Without disabling preemption in the surrounding code this_cpu_inc() will only guarantee that one of the per cpu counters is correctly incremented. However, there is no guarantee that the OS will not move the process directly before or after the this_cpu instruction is executed. In general this means that the value of the individual counters for each processor are meaningless. The sum of all the per cpu counters is the only value that is of interest.h]hX,Note that these operations can only be used on per cpu data that is reserved for a specific processor. Without disabling preemption in the surrounding code this_cpu_inc() will only guarantee that one of the per cpu counters is correctly incremented. However, there is no guarantee that the OS will not move the process directly before or after the this_cpu instruction is executed. In general this means that the value of the individual counters for each processor are meaningless. The sum of all the per cpu counters is the only value that is of interest.}(hjvhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKmhjhhubh)}(hXPer cpu variables are used for performance reasons. Bouncing cache lines can be avoided if multiple processors concurrently go through the same code paths. Since each processor has its own per cpu variables no concurrent cache line updates take place. The price that has to be paid for this optimization is the need to add up the per cpu counters when the value of a counter is needed.h]hXPer cpu variables are used for performance reasons. Bouncing cache lines can be avoided if multiple processors concurrently go through the same code paths. Since each processor has its own per cpu variables no concurrent cache line updates take place. The price that has to be paid for this optimization is the need to add up the per cpu counters when the value of a counter is needed.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKwhjhhubeh}(h]$inner-working-of-this-cpu-operationsah ]h"]$inner working of this_cpu operationsah$]h&]uh1hhhhhhhhKAubh)}(hhh](h)}(hSpecial operationsh]hSpecial operations}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubj)}(hy = this_cpu_ptr(&x)h]hy = this_cpu_ptr(&x)}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhKhjhhubh)}(hX^Takes the offset of a per cpu variable (&x !) and returns the address of the per cpu variable that belongs to the currently executing processor. this_cpu_ptr avoids multiple steps that the common get_cpu/put_cpu sequence requires. No processor number is available. Instead, the offset of the local per cpu area is simply added to the per cpu offset.h]hX^Takes the offset of a per cpu variable (&x !) and returns the address of the per cpu variable that belongs to the currently executing processor. this_cpu_ptr avoids multiple steps that the common get_cpu/put_cpu sequence requires. No processor number is available. Instead, the offset of the local per cpu area is simply added to the per cpu offset.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXlNote that this operation can only be used in code segments where smp_processor_id() may be used, for example, where preemption has been disabled. The pointer is then used to access local per cpu data in a critical section. When preemption is re-enabled this pointer is usually no longer useful since it may no longer point to per cpu data of the current processor.h]hXlNote that this operation can only be used in code segments where smp_processor_id() may be used, for example, where preemption has been disabled. The pointer is then used to access local per cpu data in a critical section. When preemption is re-enabled this pointer is usually no longer useful since it may no longer point to per cpu data of the current processor.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXOThe special cases where it makes sense to obtain a per-CPU pointer in preemptible code are addressed by raw_cpu_ptr(), but such use cases need to handle cases where two different CPUs are accessing the same per cpu variable, which might well be that of a third CPU. These use cases are typically performance optimizations. For example, SRCU implements a pair of counters as a pair of per-CPU variables, and rcu_read_lock_nmisafe() uses raw_cpu_ptr() to get a pointer to some CPU's counter, and uses atomic_inc_long() to handle migration between the raw_cpu_ptr() and the atomic_inc_long().h]hXQThe special cases where it makes sense to obtain a per-CPU pointer in preemptible code are addressed by raw_cpu_ptr(), but such use cases need to handle cases where two different CPUs are accessing the same per cpu variable, which might well be that of a third CPU. These use cases are typically performance optimizations. For example, SRCU implements a pair of counters as a pair of per-CPU variables, and rcu_read_lock_nmisafe() uses raw_cpu_ptr() to get a pointer to some CPU’s counter, and uses atomic_inc_long() to handle migration between the raw_cpu_ptr() and the atomic_inc_long().}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]special-operationsah ]h"]special operationsah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hPer cpu variables and offsetsh]hPer cpu variables and offsets}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hX"Per cpu variables have *offsets* to the beginning of the per cpu area. They do not have addresses although they look like that in the code. Offsets cannot be directly dereferenced. The offset must be added to a base pointer of a per cpu area of a processor in order to form a valid address.h](hPer cpu variables have }(hjhhhNhNubj0)}(h *offsets*h]hoffsets}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j/hjubhX to the beginning of the per cpu area. They do not have addresses although they look like that in the code. Offsets cannot be directly dereferenced. The offset must be added to a base pointer of a per cpu area of a processor in order to form a valid address.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hTherefore the use of x or &x outside of the context of per cpu operations is invalid and will generally be treated like a NULL pointer dereference.h]hTherefore the use of x or &x outside of the context of per cpu operations is invalid and will generally be treated like a NULL pointer dereference.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(hDEFINE_PER_CPU(int, x);h]hDEFINE_PER_CPU(int, x);}hj*sbah}(h]h ]h"]h$]h&]jjuh1jhhhKhjhhubh)}(hIn the context of per cpu operations the above implies that x is a per cpu variable. Most this_cpu operations take a cpu variable.h]hIn the context of per cpu operations the above implies that x is a per cpu variable. Most this_cpu operations take a cpu variable.}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(hint __percpu *p = &x;h]hint __percpu *p = &x;}hjFsbah}(h]h ]h"]h$]h&]jjuh1jhhhKhjhhubh)}(h&x and hence p is the *offset* of a per cpu variable. this_cpu_ptr() takes the offset of a per cpu variable which makes this look a bit strange.h](h&x and hence p is the }(hjThhhNhNubj0)}(h*offset*h]hoffset}(hj\hhhNhNubah}(h]h ]h"]h$]h&]uh1j/hjTubhr of a per cpu variable. this_cpu_ptr() takes the offset of a per cpu variable which makes this look a bit strange.}(hjThhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]per-cpu-variables-and-offsetsah ]h"]per cpu variables and offsetsah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h,Operations on a field of a per cpu structureh]h,Operations on a field of a per cpu structure}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj|hhhhhKubh)}(h&Let's say we have a percpu structure::h]h'Let’s say we have a percpu structure:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj|hhubj)}(hm); z = this_cpu_inc_return(ps->n);h]hRstruct s __percpu *ps = &p; this_cpu_dec(ps->m); z = this_cpu_inc_return(ps->n);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhKhj|hhubh)}(hThe calculation of the pointer may require the use of this_cpu_ptr() if we do not make use of this_cpu ops later to manipulate fields::h]hThe calculation of the pointer may require the use of this_cpu_ptr() if we do not make use of this_cpu ops later to manipulate fields:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj|hhubj)}(h=struct s *pp; pp = this_cpu_ptr(&p); pp->m--; z = pp->n++;h]h=struct s *pp; pp = this_cpu_ptr(&p); pp->m--; z = pp->n++;}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhKhj|hhubeh}(h],operations-on-a-field-of-a-per-cpu-structureah ]h"],operations on a field of a per cpu structureah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hVariants of this_cpu opsh]hVariants of this_cpu ops}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hXthis_cpu ops are interrupt safe. Some architectures do not support these per cpu local operations. In that case the operation must be replaced by code that disables interrupts, then does the operations that are guaranteed to be atomic and then re-enable interrupts. Doing so is expensive. If there are other reasons why the scheduler cannot change the processor we are executing on then there is no reason to disable interrupts. For that purpose the following __this_cpu operations are provided.h]hXthis_cpu ops are interrupt safe. Some architectures do not support these per cpu local operations. In that case the operation must be replaced by code that disables interrupts, then does the operations that are guaranteed to be atomic and then re-enable interrupts. Doing so is expensive. If there are other reasons why the scheduler cannot change the processor we are executing on then there is no reason to disable interrupts. For that purpose the following __this_cpu operations are provided.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXcThese operations have no guarantee against concurrent interrupts or preemption. If a per cpu variable is not used in an interrupt context and the scheduler cannot preempt, then they are safe. If any interrupts still occur while an operation is in progress and if the interrupt too modifies the variable, then RMW actions can not be guaranteed to be safe::h]hXbThese operations have no guarantee against concurrent interrupts or preemption. If a per cpu variable is not used in an interrupt context and the scheduler cannot preempt, then they are safe. If any interrupts still occur while an operation is in progress and if the interrupt too modifies the variable, then RMW actions can not be guaranteed to be safe:}(hj$hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(hXo__this_cpu_read(pcp) __this_cpu_write(pcp, val) __this_cpu_add(pcp, val) __this_cpu_and(pcp, val) __this_cpu_or(pcp, val) __this_cpu_add_return(pcp, val) __this_cpu_xchg(pcp, nval) __this_cpu_cmpxchg(pcp, oval, nval) __this_cpu_sub(pcp, val) __this_cpu_inc(pcp) __this_cpu_dec(pcp) __this_cpu_sub_return(pcp, val) __this_cpu_inc_return(pcp) __this_cpu_dec_return(pcp)h]hXo__this_cpu_read(pcp) __this_cpu_write(pcp, val) __this_cpu_add(pcp, val) __this_cpu_and(pcp, val) __this_cpu_or(pcp, val) __this_cpu_add_return(pcp, val) __this_cpu_xchg(pcp, nval) __this_cpu_cmpxchg(pcp, oval, nval) __this_cpu_sub(pcp, val) __this_cpu_inc(pcp) __this_cpu_dec(pcp) __this_cpu_sub_return(pcp, val) __this_cpu_inc_return(pcp) __this_cpu_dec_return(pcp)}hj2sbah}(h]h ]h"]h$]h&]jjuh1jhhhKhjhhubh)}(hWill increment x and will not fall-back to code that disables interrupts on platforms that cannot accomplish atomicity through address relocation and a Read-Modify-Write operation in the same instruction.h]hWill increment x and will not fall-back to code that disables interrupts on platforms that cannot accomplish atomicity through address relocation and a Read-Modify-Write operation in the same instruction.}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]variants-of-this-cpu-opsah ]h"]variants of this_cpu opsah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h,&this_cpu_ptr(pp)->n vs this_cpu_ptr(&pp->n)h]h,&this_cpu_ptr(pp)->n vs this_cpu_ptr(&pp->n)}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjVhhhhhM ubh)}(hThe first operation takes the offset and forms an address and then adds the offset of the n field. This may result in two add instructions emitted by the compiler.h]hThe first operation takes the offset and forms an address and then adds the offset of the n field. This may result in two add instructions emitted by the compiler.}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjVhhubh)}(hThe second one first adds the two offsets and then does the relocation. IMHO the second form looks cleaner and has an easier time with (). The second form also is consistent with the way this_cpu_read() and friends are used.h]hThe second one first adds the two offsets and then does the relocation. IMHO the second form looks cleaner and has an easier time with (). The second form also is consistent with the way this_cpu_read() and friends are used.}(hjuhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjVhhubeh}(h]&this-cpu-ptr-pp-n-vs-this-cpu-ptr-pp-nah ]h"],&this_cpu_ptr(pp)->n vs this_cpu_ptr(&pp->n)ah$]h&]uh1hhhhhhhhM ubh)}(hhh](h)}(hRemote access to per cpu datah]hRemote access to per cpu data}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hPer cpu data structures are designed to be used by one cpu exclusively. If you use the variables as intended, this_cpu_ops() are guaranteed to be "atomic" as no other CPU has access to these data structures.h]hPer cpu data structures are designed to be used by one cpu exclusively. If you use the variables as intended, this_cpu_ops() are guaranteed to be “atomic” as no other CPU has access to these data structures.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hX_There are special cases where you might need to access per cpu data structures remotely. It is usually safe to do a remote read access and that is frequently done to summarize counters. Remote write access something which could be problematic because this_cpu ops do not have lock semantics. A remote write may interfere with a this_cpu RMW operation.h]hX_There are special cases where you might need to access per cpu data structures remotely. It is usually safe to do a remote read access and that is frequently done to summarize counters. Remote write access something which could be problematic because this_cpu ops do not have lock semantics. A remote write may interfere with a this_cpu RMW operation.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjhhubh)}(hRemote write accesses to percpu data structures are highly discouraged unless absolutely necessary. Please consider using an IPI to wake up the remote CPU and perform the update to its per cpu area.h]hRemote write accesses to percpu data structures are highly discouraged unless absolutely necessary. Please consider using an IPI to wake up the remote CPU and perform the update to its per cpu area.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM'hjhhubh)}(hYTo access per-cpu data structure remotely, typically the per_cpu_ptr() function is used::h]hXTo access per-cpu data structure remotely, typically the per_cpu_ptr() function is used:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM+hjhhubj)}(hODEFINE_PER_CPU(struct data, datap); struct data *p = per_cpu_ptr(&datap, cpu);h]hODEFINE_PER_CPU(struct data, datap); struct data *p = per_cpu_ptr(&datap, cpu);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhM/hjhhubh)}(hRThis makes it explicit that we are getting ready to access a percpu area remotely.h]hRThis makes it explicit that we are getting ready to access a percpu area remotely.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM3hjhhubh)}(hIYou can also do the following to convert the datap offset to an address::h]hHYou can also do the following to convert the datap offset to an address:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM6hjhhubj)}(h&struct data *p = this_cpu_ptr(&datap);h]h&struct data *p = this_cpu_ptr(&datap);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhM8hjhhubh)}(hdbut, passing of pointers calculated via this_cpu_ptr to other cpus is unusual and should be avoided.h]hdbut, passing of pointers calculated via this_cpu_ptr to other cpus is unusual and should be avoided.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM:hjhhubh)}(hRemote access are typically only for reading the status of another cpus per cpu data. Write accesses can cause unique problems due to the relaxed synchronization requirements for this_cpu operations.h]hRemote access are typically only for reading the status of another cpus per cpu data. Write accesses can cause unique problems due to the relaxed synchronization requirements for this_cpu operations.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM=hjhhubh)}(hOne example that illustrates some concerns with write operations is the following scenario that occurs because two per cpu variables share a cache-line but the relaxed synchronization is applied to only one process updating the cache-line.h]hOne example that illustrates some concerns with write operations is the following scenario that occurs because two per cpu variables share a cache-line but the relaxed synchronization is applied to only one process updating the cache-line.}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMAhjhhubh)}(h Consider the following example::h]hConsider the following example:}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMFhjhhubj)}(h_struct test { atomic_t a; int b; }; DEFINE_PER_CPU(struct test, onecacheline);h]h_struct test { atomic_t a; int b; }; DEFINE_PER_CPU(struct test, onecacheline);}hjDsbah}(h]h ]h"]h$]h&]jjuh1jhhhMIhjhhubh)}(hXThere is some concern about what would happen if the field 'a' is updated remotely from one processor and the local processor would use this_cpu ops to update field b. Care should be taken that such simultaneous accesses to data within the same cache line are avoided. Also costly synchronization may be necessary. IPIs are generally recommended in such scenarios instead of a remote write to the per cpu area of another processor.h]hXThere is some concern about what would happen if the field ‘a’ is updated remotely from one processor and the local processor would use this_cpu ops to update field b. Care should be taken that such simultaneous accesses to data within the same cache line are avoided. Also costly synchronization may be necessary. IPIs are generally recommended in such scenarios instead of a remote write to the per cpu area of another processor.}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMPhjhhubh)}(hX5Even in cases where the remote writes are rare, please bear in mind that a remote write will evict the cache line from the processor that most likely will access it. If the processor wakes up and finds a missing local cache line of a per cpu area, its performance and hence the wake up times will be affected.h]hX5Even in cases where the remote writes are rare, please bear in mind that a remote write will evict the cache line from the processor that most likely will access it. If the processor wakes up and finds a missing local cache line of a per cpu area, its performance and hence the wake up times will be affected.}(hj`hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMWhjhhubeh}(h]remote-access-to-per-cpu-dataah ]h"]remote access to per cpu dataah$]h&]uh1hhhhhhhhMubeh}(h]this-cpu-operationsah ]h"]this_cpu operationsah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerjerror_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}nameids}(j{jxjjjjjyjvjjjSjPjjjsjpu nametypes}(j{jjjyjjSjjsuh}(jxhjjjjjvjjj|jPjjjVjpju footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages]transform_messages] transformerN include_log] decorationNhhub.