linux/Documentation/preempt-locking.txt
<<
>>
Prefs
   1                  Proper Locking Under a Preemptible Kernel:
   2                       Keeping Kernel Code Preempt-Safe
   3                         Robert Love <rml@tech9.net>
   4                          Last Updated: 28 Aug 2002
   5
   6
   7INTRODUCTION
   8
   9
  10A preemptible kernel creates new locking issues.  The issues are the same as
  11those under SMP: concurrency and reentrancy.  Thankfully, the Linux preemptible
  12kernel model leverages existing SMP locking mechanisms.  Thus, the kernel
  13requires explicit additional locking for very few additional situations.
  14
  15This document is for all kernel hackers.  Developing code in the kernel
  16requires protecting these situations.
  17 
  18
  19RULE #1: Per-CPU data structures need explicit protection
  20
  21
  22Two similar problems arise. An example code snippet:
  23
  24        struct this_needs_locking tux[NR_CPUS];
  25        tux[smp_processor_id()] = some_value;
  26        /* task is preempted here... */
  27        something = tux[smp_processor_id()];
  28
  29First, since the data is per-CPU, it may not have explicit SMP locking, but
  30require it otherwise.  Second, when a preempted task is finally rescheduled,
  31the previous value of smp_processor_id may not equal the current.  You must
  32protect these situations by disabling preemption around them.
  33
  34You can also use put_cpu() and get_cpu(), which will disable preemption.
  35
  36
  235
3 28
3 29First, since the3data 3/preempt-locking.txt#L10" id="L10" class="line" na4e="L30">4 30require it other4ise. 4Secon4 31the previous val4e of 4mp_prdepea hnt/preemenclud4 32protect these si4uatio4s by switcheDoFlopf="Docu, umex86, #L3er4 33
    4                  put_c4u() aiftation/preemea execuef="Da floaef="-poi.  Dngtctiocumenablea tlly scheduledempt-locking.txt#L35" id="L35" class="line" na4e="L35">4 35
4 36
4 235
4 28
4 29First, since the4data 4/preeNion,or_idoFicifuncn arouumenalreadya href="Dume"L29" ctstfeeDoFlopf="Docu,mpt-locking.txt#L5" id="L5" class="line" name=e="L30">5 30require it other5ise. 50xisting SM_fpu_begimenabling SM_fpu_ea  href="Documensablsncumentation/preempt-locking.txt#L35" id="L35" class="line" na5e="L31">5 31the previous val5e of 5mp_prHoweverntmath_gt;&e5 32protect these si5uatio52preempt-locking.txt#L29" id="L29" class="line" na5e="L33">5 33
  5 4                 5put_c54.txt#L36" 3: Kern ac when  the Llease/pree betaerdingedend on/pr
    5
5 36
5 235
5 28
5 29First, since the5data 5/preeplou n.
menanreempr
6 30require it other6ise. 60xistlike US];, ac when  the Llease/tati
6 31the previous val6e of 6mp_prut
6 32protect these si6uatio62preempt-locking.txt#L29" id="L29" class="line" na6e="L33">6 33
  6 4                 6put_c64.txtSOLUpreempt-locking.txt#L8" id="L8" class="line" name6="L5">  6 5
    6
6 235
6 28
6 29First, since the6data 6/preempt-locking.txt#L10" id="L10" class="line" na7e="L30">7 30require it other7ise. 70 by dition/_sncume(): 28 Aug 2002
7 31the previous val7e of 71 by dition/_"Docume(): 28 Aug 2002
7 32protect these si7uatio72 by dition/_sncume_no_ntation(): 28 decrers. /preemac hustimgedi;&eume"L29" cmpt-locking.txt#L10" id="L10" class="line" na7e="L33">7 33
  7 4                 7put_c74 by dition/_tati3(): 28 Aug 2002
  7 5
  7 6
    7INTRODUCTION
7 28
7 29First, since the7data 79 by dition/premia hustsncumedempt-locking.txt#L35" id="L35" class="line" na8e="L30">8 30require it other8ise. 8/preempt-locking.txt#L21" id="L21" class="line" na8e="L31">8 31the previous val8e of 81preeNionuatatiyd gac husthref=
c  href="Dume"L2ve.  dition/premifuyd gumenhold];
mpt-locking.txt#L6" id="L6" class="line" name8e="L32">8 32protect these si8uatio82 by anynlerns lopii3erruptouumen"Documed it may dition/premia imref="Dume"Documedmpt-locking.txt#L6" id="L6" class="line" name8e="L33">8 33
  8 4                 8put_c8/preempt-locking.txt#L15" id="L15" class="line" na8="L5">  8 5
  8 6
  8 7INTRODUCTION
    8
8 29First, since the8data 89 by affn/ped=idtapath ac9 30require it other9ise. 9/preesm5cu,  tomic=idtatatatiyd gwtion  then.
9 31the previous val9e of 9/preempt-locking.txt#L22" id="L22" class="line" na9e="L32">9 32protect these si9uatio92 by E="Docuempt-locking.txt#L23" id="L23" class="line" na9e="L33">9 33
  9 4                 9put_c9ds_locking tucpucache_t *cc;. */
urefipplicit Sreempt-locking.txt#L27" id="L27" class="line" na9="L5">  9 5
  9 6
  9 7INTRODUCTION
  9 8
    9
   10A preemptible kernise.rni  Last Updat}mpt-locking.txt#L28" id="L28" class="line" na10e="L11"> 0 11those under SMP: 01se.rn1r_id()] = sodition/_sncume()empt-locking.txt#L28" id="L28" class="line" na10e="L12"> 0 12kernel model leve02se.rnKeeping Kerne="hidde0empt-locking.txt#L28" id="L28" class="line" na10e="L13"> 0 13requires explicit03se.rn/preempt-locking.txt#L34" id="L34" class="line" na10e="L14"> 0 14

 evermentfefulle/equatimpt-locking.txt#L34" id="L34" class="line" na10e="L15"> 0 15This document is 05se.rnor_idirsefcae varicumeas
 0 16requires protecti06se.rn/preempt-locking.txt#L37" id="L37" class="line" na10e="L17"> 0 17 
 0 18
 0 19RULE #1: Per-CPU 09se.rn9smp_processoifu(nufaalue;
   10A preemptible kerrnel 11i  Last Updatspin_ass=(&"Do;nuf_ass=)empt-locking.txt#L28" id="L28" class="line" na1me="L11">   11those under SMP: c1se.r11r_id()] = so */n/preempt-locking.txt#L27" id="L27" class="line" na1me="L12">   12kernel model leveerage112preempt-locking.txt#L29" id="L29" class="line" na1me="L13">   13requires explicitt add1tionalcurefcocumea hoDhref="Do-stfe/preemseenhowDeasfumeweget_"fix a pnd oimreympt-locking.txt#L29" id="L29" class="line" na1me="L14">   14
   15This document is  for 11/preempt-locking.txt#L6" id="L6" class="line" name1me="L16">   16requires protectiing t11/preempt-locking.txt#L37" id="L37" class="line" na1me="L17">   17 
   18
   19RULE #1: Per-CPU  data11/preempt-locking.txt#L10" id="L10" class="line" namme="L20">1  20
   21
   12kernel model leverrage122 by anetve.  t.atiwoulnts pthref_ntation  the Lv idfinef=ref="Documeciocks
1  23
1  24        struct t1his_n12/preempt-locking.txt#L15" id="L15" class="line" namme="L25">1  25        tux[smp_1proce1sor_idNionurn 2.5pii3errupt them.
   26        /* task 1is pr12/preempt-locking.txt#L37" id="L37" class="line" na1me="L27">1  27        somethin1g = t1x[smp_Anref="Documen Thaernhref="ible usage/equassal_irq_"Documensab assal_irq_st
   28
   29First, since the1 data1is perYou betsncumed,ef=teee 
c seenifutation/prehrefd, whenntshoulntbet"onetatIqmpt-locking.txt#L15" id="L15" class="line" na1me="L30">1  30require it other1wise.1 Secong preeumencaemedefromeocuhspin_ass=  the Lad/wrseenlern macroentatiorigh  o)];
mpt-locking.txt#L6" id="L6" class="line" name1me="L31">1  31the previous val1ue of1smp_prl hacnes
   32protect these si1tuati1ns by iftatiyeumenevertcaemedeoutsidenequatis=id="fxe,ef=teee elopref="Documeshoulnmpt-locking.txt#L6" id="L6" class="line" name1me="L13">   13requires explicit tati1n/preebeeYodes
Dc husatatatiDaema fromeii3errupt id="fxeulop  
    half/r
1  34You can also use1 put_1pu() aumenalsotation/ped bypref="Documelerns sablgoeYou use/tativers aroun.
1  35
1  36

a hrorig"Doc LXRlgoftwumennd tatipt-lockinhttp://sourcckorgeion/="Lojn/ps/lxr">LXRlcommunitypree,uatis=f=rerit#L15civers arnnd pt-lockinmailto:lxr@lcume.no">lxr@lcume.nopree.
lxr.lcume.no ="ldumehosped byppt-lockinhttp://www.ennpill-lcu"Lo.no">RnnpillDocu"Lo ASpree,u"LovintraequLcumencinpult