linux/Documentation/local_ops.txt
<<
" /spaon> /formn> a " href="../linux+v3.7.7/Documentaptio/local_ops.txt">" img src="../.staptc/gfx/right.png" alt=">>">" /spaon>" spao class="lxr_search">" " input typue=hidden" namue=navtarget" value=">" input typue=text" namue=search" ide=search">" buttiontypue=submit">Search /formn> /spaon>" spao class="lxr_prefs"n> a href="+prefs?return=Documentaptio/local_ops.txt"" onclick="return ajax_prefs();">" Prefs> /a>" /spaon> /divn> form acptio="ajax+*" method="post" onsubmit="return false;">" input typue=hidden" namue=ajax_lookup" ide=ajax_lookup" value=">" /formn>" div class="headingbottim"> div ide=search_results" class="search_results"> n> /divn> div ide=content"n> div ide=file_contents"n
   1 /a>             Semanptcs and Behavior of Local Atimic Operaptios
   2 /a>"   3 /a>                            Mathieu Desnoyers
   4 /a>"   5 /a>"   6 /a>        This document explains the purpose of the local atimic operaptios, how"   7 /a>to implement them for any given architecture and shows how they cao be used"   8 /a>properly. It also stresses ionthe precauptiosnthat must be taken when reading"   9 /a>those local variables across CPUs when the order of memory writes matters."  .10"  110"  12 /a>"  13 /a>* Purpose of local atimic operaptios"  14 /a>"  15 /a>Local atimic operaptios are meant to provide fast and highly reentrant per CPU"  16 /a>counters. They minimizenthe performance cost of standard atimic operaptios by"  17 /a>removingnthe LOCK prefix and memory barriers normally required to synchronize"  18 /a>across CPUs."  19 /a>"  210Havingnfast per CPU atimic counters is interestingnin many cases : it does not"  21 /a>require disablingninterrupts to protect fromninterrupt handlers and it permits
  22 /a>coherent counters in NMI handlers. It is especially useful for tracingnpurposes
  23 /a>and for various performance monitoringncounters.
  24 /a>"  25 /a>Local atimic operaptios only guarantee variable modificaptio atimicity wrt the"  26 /a>CPU which owns the data. Therefore, care must taken to make sure that only one"  27 /a>CPU writes to the local_t data. This is done by usingnper cpu data and making"  28 /a>sure that we modify it fromnwithio a preemoptionsafe context. It is however"  29 /a>permitted to read local_t data fromnany CPU : it will theo appear to be written"  310out of order wrt other memory writes by the owner CPU.
  310"  32 /a>"  33 /a>* Implementaptio for a given architecture"  34 /a>"  35 /a>It cao be done by slightly modifyingnthe standard atimic operaptios : only"  36 /a>their UP variant must be kept. It typically means removingnLOCK prefix (on"  37 /a>i386 and x86_64) and any SMP synchronizaptio barrier. If the architecture does"  38 /a>not have a different behavior between SMP and UP, including asm-generic/local.h"  39 /a>io your architecture's local.h is sufficient.
  410"  410The local_t typu is defined as an opaque signed long by embedding an"  42 /a>atimic_long_t inside a structure. This is made so a cast fromnthis typu to a"  43 /a>long fails. The definiptio looks like :"  44 /a>"  45 /a>typudef struct { atimic_long_t a; } local_t;"  46 /a>"  47 /a>"  48 /a>* Rules to follow when usingnlocal atimic operaptios"  49 /a>"  510- Variables touched by local ops must be per cpu variables.
  510- _Only_ the CPU owner of these variables must write to them.
  520- This CPU cao use local ops fromnany context (process, irq, softirq, nmi, ...)
  53 /a>  to updatu its local_t variables.
  540- Preemoption(or interrupts) must be disabled when usingnlocal ops iare must taken to make sure that only onmust taken to make sure that only onmusliU"
	  >
opsdatu iq, softrocess, its  that only oneeiq, softrwonl.h ied whmighrelocal_Documentaptio/local_ops.txt#L43" ide=L435 class="5ine" namue=L46">  46 /a5"  47 /a5"  48 /a5* Rul58optionWocal ops iare must takhandlers anocess, 
  49 /a5"  510  510  520  53 /a6  to 63optionRref="Dptiosare mues.
  540
o65optionRrefsariables must write  by slightlyU : it will , slcaps flocal_y loocumentaptio/local_ops.txt#L54" ide=L546class="liine" namue=L6">   6 /a> "  47 /a6"  48 /a6* Rul68 This   49 /a6"  510  510  520  53 /a7  to 73opti#sm-gene < /Doc/a hes..h
 /socumentaptio/local_ops.txt#L33" ide=L337 class="7ine" namue=L54">  540
o7ef="Documentaptio/local_ops.txt#L6" ide=L6" 7class="l7ine" namue=L6">   6 /a>7"   7 /a>t"  48 /a7* Rul78f="Documentaptio/local_ops.txt#L48" ide=L487 class="7ine" namue=L49">  49 /a7"  510  510  520  53 /a8  to 83optiIniq,r inteef=  cess, 
  540
o85P variariables.
   6 /a>8"   7 /a>8"   8 /a>p* Rul88 This documeput_es._  49 /a8"  510  510  520  53 /a9  to 93 This documext#L47sm-(&__get_es._  540
o9ef="Documentaptio/local_ops.txt#L6" ide=L6" 9class="l9ine" namue=L6">   6 /a>9"   7 /a>9"   8 /a>9* Rul98f="Documentaptio/local_ops.txt#L48" ide=L489class="liine" namue=L9">   9 /a>t"  .10"- Va>"-P variarny CPs UP,s must b_t dat when the orabled wh iscturhronize whr wrt other ocumentaptio/local_ops.txt#L10" ide=L10"  class="l0ine" namue=L11">  11001 Va>"e disabl_opvelyre ty writes by the ownehe wrn="Dpauptiosto tne"  12 /a>02 Va>"ef="Documentaptio/local_ops.txt#L13" ide=L13"0 class="l0ine" namue=L13">  13 /a>03 Va>"3 This documext"Dmiump= 0socumentaptio/local_ops.txt#L33" ide=L3310 class="l0ine" namue=L14">  14 /a>04 Va>"4 This documemus_each_on=L49_es.(es.Documentaptio/local_ops.txt#L53" ide=L5310 class="l0ine" namue=L15">  15 /a>05 Va>"5                    iump+=must b_t da(&iab_es.(eNMI han,les.))socumentaptio/local_ops.txt#L33" ide=L3310 class="l0ine" namue=L16">  16 /a>06 Va>"ef="Documentaptio/local_ops.txt#L47" ide=L4710 class="l0ine" namue=L17">  17 /a>07 Va>"7f="DIftectswide fasps faOCK ptemust b_t datize"  18 /a>08 Va>"8f="D and UP,he o,_e puhe"< smp_wmb(MP synsmp_rmb(MPiers normally reabled whf="Documentaptio/local_ops.txt#L8" ide=L8" 10 class="l0ine" namue=L19">  19 /a>09 Va>"9 disabsefuopvelyrauptiosem.
  .10>"  110"1 Va>ref="Dbre. Tmp_wmb(MP and UP, to buaviohem.
  12 /a>>"  13 /a>>* Pu1pose oocumentaptio/local_ops.txt#L54" ide=L541" class="lline" namue=L14">  14 /a>>"  15 /a>>Loca1 atimiHrieti siasaio fthe uf=cphe dahem for a siabasicriables.
 NMI har ops iare m.hDocumentaptio/local_ops.txt#L54" ide=L541" class="lline" namue=L16">  16 /a>>coun11ef="Documentaptio/local_ops.txt#L47" ide=L471" class="lline" namue=L17">  17 /a>>remo1ingnth--- BEGIN ---ocumentaptio/local_ops.txt#L47" ide=L471" class="lline" namue=L18">  18 /a>>acro1s CPUs/* ownt-are m.socumentaptio/local_ops.txt#L33" ide=L331" class="lline" namue=L19">  19 /a>>"  210Havi1gnfast * Saio fthe uf=c arcsufficieusag=Documentaptio/local_ops.txt#L54" ide=L541" class="1line" namue=L21">  21 /a1>requ1re dis */ocumentaptio/local_ops.txt#L54" ide=L541" class="liine" namue=L12">  12 /a>""  23 /a1>and 12ose oocumentaptio/local_ops.txt#L54" ide=L541" class="1line" namue=L24">  24 /a1>"  25 /a1>Loca1 atimi#sm-gene < /Doc/he uf="D
 /socumentaptio/local_ops.txt#L33" ide=L331" class="lline" namue=L26">  26 /a1>CPU 1hich o#sm-gene < /Doc/timer"D
 /socumentaptio/local_ops.txt#L33" ide=L331" class="lline" namue=L27">  27 /a1>CPU 12ef="Documentaptio/local_ops.txt#L48" ide=L481" class="1line" namue=L28">  28 /a1>sure1that weight. DEFINE_PER_ll (iables.,  29 /a1>perm12ef="Documentaptio/local_ops.txt#L50" ide=L501" class="1line" namue=L30">  310out 1f ordeeight. imic_lotimer_li makwnt_timersocumentaptio/local_ops.txt#L33" ide=L331" class="1line" namue=L31">  310"  32 /a1>"  13 /a>*and 1lementeight. voidakwnt_each(voida*infoDocumentaptio/local_ops.txt#L53" ide=L531" class="1line" namue=L34">  34 /a1>"  35 /a1>It c135            /* Im-rfor an to  NMI harU : it noniq,r inteef=  cess,  */ocumentaptio/local_ops.txt#L54" ide=L5413 class="lline" namue=L36">  36 /a1>thei13  This documeprintk("Im-rfor anonies.
%d\n",usmp_q, softus_id())socumentaptio/local_ops.txt#L33" ide=L331" class="1line" namue=L37">  37 /a1>i386137 This documext#L47sm-(&__get_es._  38 /a1>not 138f="Documentaptio/local_ops.txt#L48" ide=L481" class="1line" namue=L39">  39 /a1>io y139            /* e so a cwe"  410"  410The 14       Semanpt*ocumentaptio/local_ops.txt#L12" ide=L12"" class="1line" namue=L42">  42 /a1>atim142             *ixt#L47sm-(&get_es._  43 /a1>long143             *iqut_es._  14 /a>""  45 /a1>typu1ef str}ocumentaptio/local_ops.txt#L54" ide=L541" class="lline" namue=L46">  46 /a1>"  47 /a1>"  48 /a1>* Ru1es to {ocumentaptio/local_ops.txt#L53" ide=L531" class="1line" namue=L49">  49 /a1>"  510- Va15ef="Documentaptio/local_ops.txt#L11" ide=L11"" class="1line" namue=L51">  510- _O151            /* Im-rfor an to  NMI has */ocumentaptio/local_ops.txt#L54" ide=L541" class="1line" namue=L52">  520- Th152            on_each_es.(kwnt_each, NULL, 1)socumentaptio/local_ops.txt#L33" ide=L331" class="1line" namue=L53">  53 /a1>  to153            /* R dat lar to  NMI has */ocumentaptio/local_ops.txt#L54" ide=L541" class="lline" namue=L54">  540- Pr154 This documeprintk("CNMI has t datU : ito t%d\n",usmp_q, softus_id())socumentaptio/local_ops.txt#L33" ide=L331to make s1ure that only onmusliU"
1	  >
155            mus_each_on=L49_es.(es.D {ocumentaptio/local_ops.txt#L53" ide=L5315 class="15ine" namue=L46">  46 /a15"  47 /a15"  48 /a15* Ru158 This docume}ocumentaptio/local_ops.txt#L54" ide=L5415 class="15ine" namue=L49">  49 /a15"  510"-0socumentaptio/local_ops.txt#L33" ide=L3316 class="16ine" namue=L51">  510  520  53 /a16  to16ose oocumentaptio/local_ops.txt#L54" ide=L5416 class="16ine" namue=L54">  540
165opti{ocumentaptio/local_ops.txt#L53" ide=L5316 class="liine" namue=L16">  16 /a>c"  47 /a16"  48 /a16* Ru168            kwnt_timer.fun+*" mn= do_kwnt_timersocumentaptio/local_ops.txt#L33" ide=L331c class="16ine" namue=L49">  49 /a16"socumentaptio/local_ops.txt#L33" ide=L3317 class="17ine" namue=L50">  510  510  520"
 0socumentaptio/local_ops.txt#L33" ide=L3317 class="17ine" namue=L53">  53 /a17  to173opti}ocumentaptio/local_ops.txt#L54" ide=L5417 class="17ine" namue=L54">  540
17ef="Deight. voida__ex l kwnt_ex l(voidDocumentaptio/local_ops.txt#L53" ide=L5317class="l17ine" namue=L6">   6 /a>17"  17 /a>r"  48 /a17* Ru178f="D}ocumentaptio/local_ops.txt#L54" ide=L5417 class="17ine" namue=L49">  49 /a17"  510  510  520  53 /a18  to183optiMODULE_LICENSE("GPL")socumentaptio/local_ops.txt#L33" ide=L3318 class="18ine" namue=L54">  540
185P vaMODULE_DESCRIPTION("Lles aaptios
   6 /a>18"   7 /a>18"

v ide=content" namuefoo ha">
T memoigins aLXRmi, .w s hr ctioscumentapthttp://sourcDmusge.net/projects/lxa">LXRmcommus ly Thi,to a"lxa@ /Doc.no Thi.
v ide=cntent" namuesubfoo ha">
lxa. /Doc.no kindly host ops mcumentapthttp://www.redppea- /Dpro.no">Redppea L/Dpro AS Thi,tt and hAtimic/Doch iscultref="Dd : only"