kernel-locking.tmpl 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
  4. <book id="LKLockingGuide">
  5. <bookinfo>
  6. <title>Unreliable Guide To Locking</title>
  7. <authorgroup>
  8. <author>
  9. <firstname>Rusty</firstname>
  10. <surname>Russell</surname>
  11. <affiliation>
  12. <address>
  13. <email>rusty@rustcorp.com.au</email>
  14. </address>
  15. </affiliation>
  16. </author>
  17. </authorgroup>
  18. <copyright>
  19. <year>2003</year>
  20. <holder>Rusty Russell</holder>
  21. </copyright>
  22. <legalnotice>
  23. <para>
  24. This documentation is free software; you can redistribute
  25. it and/or modify it under the terms of the GNU General Public
  26. License as published by the Free Software Foundation; either
  27. version 2 of the License, or (at your option) any later
  28. version.
  29. </para>
  30. <para>
  31. This program is distributed in the hope that it will be
  32. useful, but WITHOUT ANY WARRANTY; without even the implied
  33. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  34. See the GNU General Public License for more details.
  35. </para>
  36. <para>
  37. You should have received a copy of the GNU General Public
  38. License along with this program; if not, write to the Free
  39. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  40. MA 02111-1307 USA
  41. </para>
  42. <para>
  43. For more details see the file COPYING in the source
  44. distribution of Linux.
  45. </para>
  46. </legalnotice>
  47. </bookinfo>
  48. <toc></toc>
  49. <chapter id="intro">
  50. <title>Introduction</title>
  51. <para>
  52. Welcome, to Rusty's Remarkably Unreliable Guide to Kernel
  53. Locking issues. This document describes the locking systems in
  54. the Linux Kernel in 2.6.
  55. </para>
  56. <para>
  57. With the wide availability of HyperThreading, and <firstterm
  58. linkend="gloss-preemption">preemption </firstterm> in the Linux
  59. Kernel, everyone hacking on the kernel needs to know the
  60. fundamentals of concurrency and locking for
  61. <firstterm linkend="gloss-smp"><acronym>SMP</acronym></firstterm>.
  62. </para>
  63. </chapter>
  64. <chapter id="races">
  65. <title>The Problem With Concurrency</title>
  66. <para>
  67. (Skip this if you know what a Race Condition is).
  68. </para>
  69. <para>
  70. In a normal program, you can increment a counter like so:
  71. </para>
  72. <programlisting>
  73. very_important_count++;
  74. </programlisting>
  75. <para>
  76. This is what they would expect to happen:
  77. </para>
  78. <table>
  79. <title>Expected Results</title>
  80. <tgroup cols="2" align="left">
  81. <thead>
  82. <row>
  83. <entry>Instance 1</entry>
  84. <entry>Instance 2</entry>
  85. </row>
  86. </thead>
  87. <tbody>
  88. <row>
  89. <entry>read very_important_count (5)</entry>
  90. <entry></entry>
  91. </row>
  92. <row>
  93. <entry>add 1 (6)</entry>
  94. <entry></entry>
  95. </row>
  96. <row>
  97. <entry>write very_important_count (6)</entry>
  98. <entry></entry>
  99. </row>
  100. <row>
  101. <entry></entry>
  102. <entry>read very_important_count (6)</entry>
  103. </row>
  104. <row>
  105. <entry></entry>
  106. <entry>add 1 (7)</entry>
  107. </row>
  108. <row>
  109. <entry></entry>
  110. <entry>write very_important_count (7)</entry>
  111. </row>
  112. </tbody>
  113. </tgroup>
  114. </table>
  115. <para>
  116. This is what might happen:
  117. </para>
  118. <table>
  119. <title>Possible Results</title>
  120. <tgroup cols="2" align="left">
  121. <thead>
  122. <row>
  123. <entry>Instance 1</entry>
  124. <entry>Instance 2</entry>
  125. </row>
  126. </thead>
  127. <tbody>
  128. <row>
  129. <entry>read very_important_count (5)</entry>
  130. <entry></entry>
  131. </row>
  132. <row>
  133. <entry></entry>
  134. <entry>read very_important_count (5)</entry>
  135. </row>
  136. <row>
  137. <entry>add 1 (6)</entry>
  138. <entry></entry>
  139. </row>
  140. <row>
  141. <entry></entry>
  142. <entry>add 1 (6)</entry>
  143. </row>
  144. <row>
  145. <entry>write very_important_count (6)</entry>
  146. <entry></entry>
  147. </row>
  148. <row>
  149. <entry></entry>
  150. <entry>write very_important_count (6)</entry>
  151. </row>
  152. </tbody>
  153. </tgroup>
  154. </table>
  155. <sect1 id="race-condition">
  156. <title>Race Conditions and Critical Regions</title>
  157. <para>
  158. This overlap, where the result depends on the
  159. relative timing of multiple tasks, is called a <firstterm>race condition</firstterm>.
  160. The piece of code containing the concurrency issue is called a
  161. <firstterm>critical region</firstterm>. And especially since Linux starting running
  162. on SMP machines, they became one of the major issues in kernel
  163. design and implementation.
  164. </para>
  165. <para>
  166. Preemption can have the same effect, even if there is only one
  167. CPU: by preempting one task during the critical region, we have
  168. exactly the same race condition. In this case the thread which
  169. preempts might run the critical region itself.
  170. </para>
  171. <para>
  172. The solution is to recognize when these simultaneous accesses
  173. occur, and use locks to make sure that only one instance can
  174. enter the critical region at any time. There are many
  175. friendly primitives in the Linux kernel to help you do this.
  176. And then there are the unfriendly primitives, but I'll pretend
  177. they don't exist.
  178. </para>
  179. </sect1>
  180. </chapter>
  181. <chapter id="locks">
  182. <title>Locking in the Linux Kernel</title>
  183. <para>
  184. If I could give you one piece of advice: never sleep with anyone
  185. crazier than yourself. But if I had to give you advice on
  186. locking: <emphasis>keep it simple</emphasis>.
  187. </para>
  188. <para>
  189. Be reluctant to introduce new locks.
  190. </para>
  191. <para>
  192. Strangely enough, this last one is the exact reverse of my advice when
  193. you <emphasis>have</emphasis> slept with someone crazier than yourself.
  194. And you should think about getting a big dog.
  195. </para>
  196. <sect1 id="lock-intro">
  197. <title>Two Main Types of Kernel Locks: Spinlocks and Semaphores</title>
  198. <para>
  199. There are three main types of kernel locks. The fundamental type
  200. is the spinlock
  201. (<filename class="headerfile">include/asm/spinlock.h</filename>),
  202. which is a very simple single-holder lock: if you can't get the
  203. spinlock, you keep trying (spinning) until you can. Spinlocks are
  204. very small and fast, and can be used anywhere.
  205. </para>
  206. <para>
  207. The second type is a mutex
  208. (<filename class="headerfile">include/linux/mutex.h</filename>): it
  209. is like a spinlock, but you may block holding a mutex.
  210. If you can't lock a mutex, your task will suspend itself, and be woken
  211. up when the mutex is released. This means the CPU can do something
  212. else while you are waiting. There are many cases when you simply
  213. can't sleep (see <xref linkend="sleeping-things"/>), and so have to
  214. use a spinlock instead.
  215. </para>
  216. <para>
  217. The third type is a semaphore
  218. (<filename class="headerfile">include/asm/semaphore.h</filename>): it
  219. can have more than one holder at any time (the number decided at
  220. initialization time), although it is most commonly used as a
  221. single-holder lock (a mutex). If you can't get a semaphore, your
  222. task will be suspended and later on woken up - just like for mutexes.
  223. </para>
  224. <para>
  225. Neither type of lock is recursive: see
  226. <xref linkend="deadlock"/>.
  227. </para>
  228. </sect1>
  229. <sect1 id="uniprocessor">
  230. <title>Locks and Uniprocessor Kernels</title>
  231. <para>
  232. For kernels compiled without <symbol>CONFIG_SMP</symbol>, and
  233. without <symbol>CONFIG_PREEMPT</symbol> spinlocks do not exist at
  234. all. This is an excellent design decision: when no-one else can
  235. run at the same time, there is no reason to have a lock.
  236. </para>
  237. <para>
  238. If the kernel is compiled without <symbol>CONFIG_SMP</symbol>,
  239. but <symbol>CONFIG_PREEMPT</symbol> is set, then spinlocks
  240. simply disable preemption, which is sufficient to prevent any
  241. races. For most purposes, we can think of preemption as
  242. equivalent to SMP, and not worry about it separately.
  243. </para>
  244. <para>
  245. You should always test your locking code with <symbol>CONFIG_SMP</symbol>
  246. and <symbol>CONFIG_PREEMPT</symbol> enabled, even if you don't have an SMP test box, because it
  247. will still catch some kinds of locking bugs.
  248. </para>
  249. <para>
  250. Semaphores still exist, because they are required for
  251. synchronization between <firstterm linkend="gloss-usercontext">user
  252. contexts</firstterm>, as we will see below.
  253. </para>
  254. </sect1>
  255. <sect1 id="usercontextlocking">
  256. <title>Locking Only In User Context</title>
  257. <para>
  258. If you have a data structure which is only ever accessed from
  259. user context, then you can use a simple semaphore
  260. (<filename>linux/asm/semaphore.h</filename>) to protect it. This
  261. is the most trivial case: you initialize the semaphore to the number
  262. of resources available (usually 1), and call
  263. <function>down_interruptible()</function> to grab the semaphore, and
  264. <function>up()</function> to release it. There is also a
  265. <function>down()</function>, which should be avoided, because it
  266. will not return if a signal is received.
  267. </para>
  268. <para>
  269. Example: <filename>linux/net/core/netfilter.c</filename> allows
  270. registration of new <function>setsockopt()</function> and
  271. <function>getsockopt()</function> calls, with
  272. <function>nf_register_sockopt()</function>. Registration and
  273. de-registration are only done on module load and unload (and boot
  274. time, where there is no concurrency), and the list of registrations
  275. is only consulted for an unknown <function>setsockopt()</function>
  276. or <function>getsockopt()</function> system call. The
  277. <varname>nf_sockopt_mutex</varname> is perfect to protect this,
  278. especially since the setsockopt and getsockopt calls may well
  279. sleep.
  280. </para>
  281. </sect1>
  282. <sect1 id="lock-user-bh">
  283. <title>Locking Between User Context and Softirqs</title>
  284. <para>
  285. If a <firstterm linkend="gloss-softirq">softirq</firstterm> shares
  286. data with user context, you have two problems. Firstly, the current
  287. user context can be interrupted by a softirq, and secondly, the
  288. critical region could be entered from another CPU. This is where
  289. <function>spin_lock_bh()</function>
  290. (<filename class="headerfile">include/linux/spinlock.h</filename>) is
  291. used. It disables softirqs on that CPU, then grabs the lock.
  292. <function>spin_unlock_bh()</function> does the reverse. (The
  293. '_bh' suffix is a historical reference to "Bottom Halves", the
  294. old name for software interrupts. It should really be
  295. called spin_lock_softirq()' in a perfect world).
  296. </para>
  297. <para>
  298. Note that you can also use <function>spin_lock_irq()</function>
  299. or <function>spin_lock_irqsave()</function> here, which stop
  300. hardware interrupts as well: see <xref linkend="hardirq-context"/>.
  301. </para>
  302. <para>
  303. This works perfectly for <firstterm linkend="gloss-up"><acronym>UP
  304. </acronym></firstterm> as well: the spin lock vanishes, and this macro
  305. simply becomes <function>local_bh_disable()</function>
  306. (<filename class="headerfile">include/linux/interrupt.h</filename>), which
  307. protects you from the softirq being run.
  308. </para>
  309. </sect1>
  310. <sect1 id="lock-user-tasklet">
  311. <title>Locking Between User Context and Tasklets</title>
  312. <para>
  313. This is exactly the same as above, because <firstterm
  314. linkend="gloss-tasklet">tasklets</firstterm> are actually run
  315. from a softirq.
  316. </para>
  317. </sect1>
  318. <sect1 id="lock-user-timers">
  319. <title>Locking Between User Context and Timers</title>
  320. <para>
  321. This, too, is exactly the same as above, because <firstterm
  322. linkend="gloss-timers">timers</firstterm> are actually run from
  323. a softirq. From a locking point of view, tasklets and timers
  324. are identical.
  325. </para>
  326. </sect1>
  327. <sect1 id="lock-tasklets">
  328. <title>Locking Between Tasklets/Timers</title>
  329. <para>
  330. Sometimes a tasklet or timer might want to share data with
  331. another tasklet or timer.
  332. </para>
  333. <sect2 id="lock-tasklets-same">
  334. <title>The Same Tasklet/Timer</title>
  335. <para>
  336. Since a tasklet is never run on two CPUs at once, you don't
  337. need to worry about your tasklet being reentrant (running
  338. twice at once), even on SMP.
  339. </para>
  340. </sect2>
  341. <sect2 id="lock-tasklets-different">
  342. <title>Different Tasklets/Timers</title>
  343. <para>
  344. If another tasklet/timer wants
  345. to share data with your tasklet or timer , you will both need to use
  346. <function>spin_lock()</function> and
  347. <function>spin_unlock()</function> calls.
  348. <function>spin_lock_bh()</function> is
  349. unnecessary here, as you are already in a tasklet, and
  350. none will be run on the same CPU.
  351. </para>
  352. </sect2>
  353. </sect1>
  354. <sect1 id="lock-softirqs">
  355. <title>Locking Between Softirqs</title>
  356. <para>
  357. Often a softirq might
  358. want to share data with itself or a tasklet/timer.
  359. </para>
  360. <sect2 id="lock-softirqs-same">
  361. <title>The Same Softirq</title>
  362. <para>
  363. The same softirq can run on the other CPUs: you can use a
  364. per-CPU array (see <xref linkend="per-cpu"/>) for better
  365. performance. If you're going so far as to use a softirq,
  366. you probably care about scalable performance enough
  367. to justify the extra complexity.
  368. </para>
  369. <para>
  370. You'll need to use <function>spin_lock()</function> and
  371. <function>spin_unlock()</function> for shared data.
  372. </para>
  373. </sect2>
  374. <sect2 id="lock-softirqs-different">
  375. <title>Different Softirqs</title>
  376. <para>
  377. You'll need to use <function>spin_lock()</function> and
  378. <function>spin_unlock()</function> for shared data, whether it
  379. be a timer, tasklet, different softirq or the same or another
  380. softirq: any of them could be running on a different CPU.
  381. </para>
  382. </sect2>
  383. </sect1>
  384. </chapter>
  385. <chapter id="hardirq-context">
  386. <title>Hard IRQ Context</title>
  387. <para>
  388. Hardware interrupts usually communicate with a
  389. tasklet or softirq. Frequently this involves putting work in a
  390. queue, which the softirq will take out.
  391. </para>
  392. <sect1 id="hardirq-softirq">
  393. <title>Locking Between Hard IRQ and Softirqs/Tasklets</title>
  394. <para>
  395. If a hardware irq handler shares data with a softirq, you have
  396. two concerns. Firstly, the softirq processing can be
  397. interrupted by a hardware interrupt, and secondly, the
  398. critical region could be entered by a hardware interrupt on
  399. another CPU. This is where <function>spin_lock_irq()</function> is
  400. used. It is defined to disable interrupts on that cpu, then grab
  401. the lock. <function>spin_unlock_irq()</function> does the reverse.
  402. </para>
  403. <para>
  404. The irq handler does not to use
  405. <function>spin_lock_irq()</function>, because the softirq cannot
  406. run while the irq handler is running: it can use
  407. <function>spin_lock()</function>, which is slightly faster. The
  408. only exception would be if a different hardware irq handler uses
  409. the same lock: <function>spin_lock_irq()</function> will stop
  410. that from interrupting us.
  411. </para>
  412. <para>
  413. This works perfectly for UP as well: the spin lock vanishes,
  414. and this macro simply becomes <function>local_irq_disable()</function>
  415. (<filename class="headerfile">include/asm/smp.h</filename>), which
  416. protects you from the softirq/tasklet/BH being run.
  417. </para>
  418. <para>
  419. <function>spin_lock_irqsave()</function>
  420. (<filename>include/linux/spinlock.h</filename>) is a variant
  421. which saves whether interrupts were on or off in a flags word,
  422. which is passed to <function>spin_unlock_irqrestore()</function>. This
  423. means that the same code can be used inside an hard irq handler (where
  424. interrupts are already off) and in softirqs (where the irq
  425. disabling is required).
  426. </para>
  427. <para>
  428. Note that softirqs (and hence tasklets and timers) are run on
  429. return from hardware interrupts, so
  430. <function>spin_lock_irq()</function> also stops these. In that
  431. sense, <function>spin_lock_irqsave()</function> is the most
  432. general and powerful locking function.
  433. </para>
  434. </sect1>
  435. <sect1 id="hardirq-hardirq">
  436. <title>Locking Between Two Hard IRQ Handlers</title>
  437. <para>
  438. It is rare to have to share data between two IRQ handlers, but
  439. if you do, <function>spin_lock_irqsave()</function> should be
  440. used: it is architecture-specific whether all interrupts are
  441. disabled inside irq handlers themselves.
  442. </para>
  443. </sect1>
  444. </chapter>
  445. <chapter id="cheatsheet">
  446. <title>Cheat Sheet For Locking</title>
  447. <para>
  448. Pete Zaitcev gives the following summary:
  449. </para>
  450. <itemizedlist>
  451. <listitem>
  452. <para>
  453. If you are in a process context (any syscall) and want to
  454. lock other process out, use a semaphore. You can take a semaphore
  455. and sleep (<function>copy_from_user*(</function> or
  456. <function>kmalloc(x,GFP_KERNEL)</function>).
  457. </para>
  458. </listitem>
  459. <listitem>
  460. <para>
  461. Otherwise (== data can be touched in an interrupt), use
  462. <function>spin_lock_irqsave()</function> and
  463. <function>spin_unlock_irqrestore()</function>.
  464. </para>
  465. </listitem>
  466. <listitem>
  467. <para>
  468. Avoid holding spinlock for more than 5 lines of code and
  469. across any function call (except accessors like
  470. <function>readb</function>).
  471. </para>
  472. </listitem>
  473. </itemizedlist>
  474. <sect1 id="minimum-lock-reqirements">
  475. <title>Table of Minimum Requirements</title>
  476. <para> The following table lists the <emphasis>minimum</emphasis>
  477. locking requirements between various contexts. In some cases,
  478. the same context can only be running on one CPU at a time, so
  479. no locking is required for that context (eg. a particular
  480. thread can only run on one CPU at a time, but if it needs
  481. shares data with another thread, locking is required).
  482. </para>
  483. <para>
  484. Remember the advice above: you can always use
  485. <function>spin_lock_irqsave()</function>, which is a superset
  486. of all other spinlock primitives.
  487. </para>
  488. <table>
  489. <title>Table of Locking Requirements</title>
  490. <tgroup cols="11">
  491. <tbody>
  492. <row>
  493. <entry></entry>
  494. <entry>IRQ Handler A</entry>
  495. <entry>IRQ Handler B</entry>
  496. <entry>Softirq A</entry>
  497. <entry>Softirq B</entry>
  498. <entry>Tasklet A</entry>
  499. <entry>Tasklet B</entry>
  500. <entry>Timer A</entry>
  501. <entry>Timer B</entry>
  502. <entry>User Context A</entry>
  503. <entry>User Context B</entry>
  504. </row>
  505. <row>
  506. <entry>IRQ Handler A</entry>
  507. <entry>None</entry>
  508. </row>
  509. <row>
  510. <entry>IRQ Handler B</entry>
  511. <entry>spin_lock_irqsave</entry>
  512. <entry>None</entry>
  513. </row>
  514. <row>
  515. <entry>Softirq A</entry>
  516. <entry>spin_lock_irq</entry>
  517. <entry>spin_lock_irq</entry>
  518. <entry>spin_lock</entry>
  519. </row>
  520. <row>
  521. <entry>Softirq B</entry>
  522. <entry>spin_lock_irq</entry>
  523. <entry>spin_lock_irq</entry>
  524. <entry>spin_lock</entry>
  525. <entry>spin_lock</entry>
  526. </row>
  527. <row>
  528. <entry>Tasklet A</entry>
  529. <entry>spin_lock_irq</entry>
  530. <entry>spin_lock_irq</entry>
  531. <entry>spin_lock</entry>
  532. <entry>spin_lock</entry>
  533. <entry>None</entry>
  534. </row>
  535. <row>
  536. <entry>Tasklet B</entry>
  537. <entry>spin_lock_irq</entry>
  538. <entry>spin_lock_irq</entry>
  539. <entry>spin_lock</entry>
  540. <entry>spin_lock</entry>
  541. <entry>spin_lock</entry>
  542. <entry>None</entry>
  543. </row>
  544. <row>
  545. <entry>Timer A</entry>
  546. <entry>spin_lock_irq</entry>
  547. <entry>spin_lock_irq</entry>
  548. <entry>spin_lock</entry>
  549. <entry>spin_lock</entry>
  550. <entry>spin_lock</entry>
  551. <entry>spin_lock</entry>
  552. <entry>None</entry>
  553. </row>
  554. <row>
  555. <entry>Timer B</entry>
  556. <entry>spin_lock_irq</entry>
  557. <entry>spin_lock_irq</entry>
  558. <entry>spin_lock</entry>
  559. <entry>spin_lock</entry>
  560. <entry>spin_lock</entry>
  561. <entry>spin_lock</entry>
  562. <entry>spin_lock</entry>
  563. <entry>None</entry>
  564. </row>
  565. <row>
  566. <entry>User Context A</entry>
  567. <entry>spin_lock_irq</entry>
  568. <entry>spin_lock_irq</entry>
  569. <entry>spin_lock_bh</entry>
  570. <entry>spin_lock_bh</entry>
  571. <entry>spin_lock_bh</entry>
  572. <entry>spin_lock_bh</entry>
  573. <entry>spin_lock_bh</entry>
  574. <entry>spin_lock_bh</entry>
  575. <entry>None</entry>
  576. </row>
  577. <row>
  578. <entry>User Context B</entry>
  579. <entry>spin_lock_irq</entry>
  580. <entry>spin_lock_irq</entry>
  581. <entry>spin_lock_bh</entry>
  582. <entry>spin_lock_bh</entry>
  583. <entry>spin_lock_bh</entry>
  584. <entry>spin_lock_bh</entry>
  585. <entry>spin_lock_bh</entry>
  586. <entry>spin_lock_bh</entry>
  587. <entry>down_interruptible</entry>
  588. <entry>None</entry>
  589. </row>
  590. </tbody>
  591. </tgroup>
  592. </table>
  593. </sect1>
  594. </chapter>
  595. <chapter id="Examples">
  596. <title>Common Examples</title>
  597. <para>
  598. Let's step through a simple example: a cache of number to name
  599. mappings. The cache keeps a count of how often each of the objects is
  600. used, and when it gets full, throws out the least used one.
  601. </para>
  602. <sect1 id="examples-usercontext">
  603. <title>All In User Context</title>
  604. <para>
  605. For our first example, we assume that all operations are in user
  606. context (ie. from system calls), so we can sleep. This means we can
  607. use a semaphore to protect the cache and all the objects within
  608. it. Here's the code:
  609. </para>
  610. <programlisting>
  611. #include &lt;linux/list.h&gt;
  612. #include &lt;linux/slab.h&gt;
  613. #include &lt;linux/string.h&gt;
  614. #include &lt;asm/semaphore.h&gt;
  615. #include &lt;asm/errno.h&gt;
  616. struct object
  617. {
  618. struct list_head list;
  619. int id;
  620. char name[32];
  621. int popularity;
  622. };
  623. /* Protects the cache, cache_num, and the objects within it */
  624. static DECLARE_MUTEX(cache_lock);
  625. static LIST_HEAD(cache);
  626. static unsigned int cache_num = 0;
  627. #define MAX_CACHE_SIZE 10
  628. /* Must be holding cache_lock */
  629. static struct object *__cache_find(int id)
  630. {
  631. struct object *i;
  632. list_for_each_entry(i, &amp;cache, list)
  633. if (i-&gt;id == id) {
  634. i-&gt;popularity++;
  635. return i;
  636. }
  637. return NULL;
  638. }
  639. /* Must be holding cache_lock */
  640. static void __cache_delete(struct object *obj)
  641. {
  642. BUG_ON(!obj);
  643. list_del(&amp;obj-&gt;list);
  644. kfree(obj);
  645. cache_num--;
  646. }
  647. /* Must be holding cache_lock */
  648. static void __cache_add(struct object *obj)
  649. {
  650. list_add(&amp;obj-&gt;list, &amp;cache);
  651. if (++cache_num > MAX_CACHE_SIZE) {
  652. struct object *i, *outcast = NULL;
  653. list_for_each_entry(i, &amp;cache, list) {
  654. if (!outcast || i-&gt;popularity &lt; outcast-&gt;popularity)
  655. outcast = i;
  656. }
  657. __cache_delete(outcast);
  658. }
  659. }
  660. int cache_add(int id, const char *name)
  661. {
  662. struct object *obj;
  663. if ((obj = kmalloc(sizeof(*obj), GFP_KERNEL)) == NULL)
  664. return -ENOMEM;
  665. strlcpy(obj-&gt;name, name, sizeof(obj-&gt;name));
  666. obj-&gt;id = id;
  667. obj-&gt;popularity = 0;
  668. down(&amp;cache_lock);
  669. __cache_add(obj);
  670. up(&amp;cache_lock);
  671. return 0;
  672. }
  673. void cache_delete(int id)
  674. {
  675. down(&amp;cache_lock);
  676. __cache_delete(__cache_find(id));
  677. up(&amp;cache_lock);
  678. }
  679. int cache_find(int id, char *name)
  680. {
  681. struct object *obj;
  682. int ret = -ENOENT;
  683. down(&amp;cache_lock);
  684. obj = __cache_find(id);
  685. if (obj) {
  686. ret = 0;
  687. strcpy(name, obj-&gt;name);
  688. }
  689. up(&amp;cache_lock);
  690. return ret;
  691. }
  692. </programlisting>
  693. <para>
  694. Note that we always make sure we have the cache_lock when we add,
  695. delete, or look up the cache: both the cache infrastructure itself and
  696. the contents of the objects are protected by the lock. In this case
  697. it's easy, since we copy the data for the user, and never let them
  698. access the objects directly.
  699. </para>
  700. <para>
  701. There is a slight (and common) optimization here: in
  702. <function>cache_add</function> we set up the fields of the object
  703. before grabbing the lock. This is safe, as no-one else can access it
  704. until we put it in cache.
  705. </para>
  706. </sect1>
  707. <sect1 id="examples-interrupt">
  708. <title>Accessing From Interrupt Context</title>
  709. <para>
  710. Now consider the case where <function>cache_find</function> can be
  711. called from interrupt context: either a hardware interrupt or a
  712. softirq. An example would be a timer which deletes object from the
  713. cache.
  714. </para>
  715. <para>
  716. The change is shown below, in standard patch format: the
  717. <symbol>-</symbol> are lines which are taken away, and the
  718. <symbol>+</symbol> are lines which are added.
  719. </para>
  720. <programlisting>
  721. --- cache.c.usercontext 2003-12-09 13:58:54.000000000 +1100
  722. +++ cache.c.interrupt 2003-12-09 14:07:49.000000000 +1100
  723. @@ -12,7 +12,7 @@
  724. int popularity;
  725. };
  726. -static DECLARE_MUTEX(cache_lock);
  727. +static spinlock_t cache_lock = SPIN_LOCK_UNLOCKED;
  728. static LIST_HEAD(cache);
  729. static unsigned int cache_num = 0;
  730. #define MAX_CACHE_SIZE 10
  731. @@ -55,6 +55,7 @@
  732. int cache_add(int id, const char *name)
  733. {
  734. struct object *obj;
  735. + unsigned long flags;
  736. if ((obj = kmalloc(sizeof(*obj), GFP_KERNEL)) == NULL)
  737. return -ENOMEM;
  738. @@ -63,30 +64,33 @@
  739. obj-&gt;id = id;
  740. obj-&gt;popularity = 0;
  741. - down(&amp;cache_lock);
  742. + spin_lock_irqsave(&amp;cache_lock, flags);
  743. __cache_add(obj);
  744. - up(&amp;cache_lock);
  745. + spin_unlock_irqrestore(&amp;cache_lock, flags);
  746. return 0;
  747. }
  748. void cache_delete(int id)
  749. {
  750. - down(&amp;cache_lock);
  751. + unsigned long flags;
  752. +
  753. + spin_lock_irqsave(&amp;cache_lock, flags);
  754. __cache_delete(__cache_find(id));
  755. - up(&amp;cache_lock);
  756. + spin_unlock_irqrestore(&amp;cache_lock, flags);
  757. }
  758. int cache_find(int id, char *name)
  759. {
  760. struct object *obj;
  761. int ret = -ENOENT;
  762. + unsigned long flags;
  763. - down(&amp;cache_lock);
  764. + spin_lock_irqsave(&amp;cache_lock, flags);
  765. obj = __cache_find(id);
  766. if (obj) {
  767. ret = 0;
  768. strcpy(name, obj-&gt;name);
  769. }
  770. - up(&amp;cache_lock);
  771. + spin_unlock_irqrestore(&amp;cache_lock, flags);
  772. return ret;
  773. }
  774. </programlisting>
  775. <para>
  776. Note that the <function>spin_lock_irqsave</function> will turn off
  777. interrupts if they are on, otherwise does nothing (if we are already
  778. in an interrupt handler), hence these functions are safe to call from
  779. any context.
  780. </para>
  781. <para>
  782. Unfortunately, <function>cache_add</function> calls
  783. <function>kmalloc</function> with the <symbol>GFP_KERNEL</symbol>
  784. flag, which is only legal in user context. I have assumed that
  785. <function>cache_add</function> is still only called in user context,
  786. otherwise this should become a parameter to
  787. <function>cache_add</function>.
  788. </para>
  789. </sect1>
  790. <sect1 id="examples-refcnt">
  791. <title>Exposing Objects Outside This File</title>
  792. <para>
  793. If our objects contained more information, it might not be sufficient
  794. to copy the information in and out: other parts of the code might want
  795. to keep pointers to these objects, for example, rather than looking up
  796. the id every time. This produces two problems.
  797. </para>
  798. <para>
  799. The first problem is that we use the <symbol>cache_lock</symbol> to
  800. protect objects: we'd need to make this non-static so the rest of the
  801. code can use it. This makes locking trickier, as it is no longer all
  802. in one place.
  803. </para>
  804. <para>
  805. The second problem is the lifetime problem: if another structure keeps
  806. a pointer to an object, it presumably expects that pointer to remain
  807. valid. Unfortunately, this is only guaranteed while you hold the
  808. lock, otherwise someone might call <function>cache_delete</function>
  809. and even worse, add another object, re-using the same address.
  810. </para>
  811. <para>
  812. As there is only one lock, you can't hold it forever: no-one else would
  813. get any work done.
  814. </para>
  815. <para>
  816. The solution to this problem is to use a reference count: everyone who
  817. has a pointer to the object increases it when they first get the
  818. object, and drops the reference count when they're finished with it.
  819. Whoever drops it to zero knows it is unused, and can actually delete it.
  820. </para>
  821. <para>
  822. Here is the code:
  823. </para>
  824. <programlisting>
  825. --- cache.c.interrupt 2003-12-09 14:25:43.000000000 +1100
  826. +++ cache.c.refcnt 2003-12-09 14:33:05.000000000 +1100
  827. @@ -7,6 +7,7 @@
  828. struct object
  829. {
  830. struct list_head list;
  831. + unsigned int refcnt;
  832. int id;
  833. char name[32];
  834. int popularity;
  835. @@ -17,6 +18,35 @@
  836. static unsigned int cache_num = 0;
  837. #define MAX_CACHE_SIZE 10
  838. +static void __object_put(struct object *obj)
  839. +{
  840. + if (--obj-&gt;refcnt == 0)
  841. + kfree(obj);
  842. +}
  843. +
  844. +static void __object_get(struct object *obj)
  845. +{
  846. + obj-&gt;refcnt++;
  847. +}
  848. +
  849. +void object_put(struct object *obj)
  850. +{
  851. + unsigned long flags;
  852. +
  853. + spin_lock_irqsave(&amp;cache_lock, flags);
  854. + __object_put(obj);
  855. + spin_unlock_irqrestore(&amp;cache_lock, flags);
  856. +}
  857. +
  858. +void object_get(struct object *obj)
  859. +{
  860. + unsigned long flags;
  861. +
  862. + spin_lock_irqsave(&amp;cache_lock, flags);
  863. + __object_get(obj);
  864. + spin_unlock_irqrestore(&amp;cache_lock, flags);
  865. +}
  866. +
  867. /* Must be holding cache_lock */
  868. static struct object *__cache_find(int id)
  869. {
  870. @@ -35,6 +65,7 @@
  871. {
  872. BUG_ON(!obj);
  873. list_del(&amp;obj-&gt;list);
  874. + __object_put(obj);
  875. cache_num--;
  876. }
  877. @@ -63,6 +94,7 @@
  878. strlcpy(obj-&gt;name, name, sizeof(obj-&gt;name));
  879. obj-&gt;id = id;
  880. obj-&gt;popularity = 0;
  881. + obj-&gt;refcnt = 1; /* The cache holds a reference */
  882. spin_lock_irqsave(&amp;cache_lock, flags);
  883. __cache_add(obj);
  884. @@ -79,18 +111,15 @@
  885. spin_unlock_irqrestore(&amp;cache_lock, flags);
  886. }
  887. -int cache_find(int id, char *name)
  888. +struct object *cache_find(int id)
  889. {
  890. struct object *obj;
  891. - int ret = -ENOENT;
  892. unsigned long flags;
  893. spin_lock_irqsave(&amp;cache_lock, flags);
  894. obj = __cache_find(id);
  895. - if (obj) {
  896. - ret = 0;
  897. - strcpy(name, obj-&gt;name);
  898. - }
  899. + if (obj)
  900. + __object_get(obj);
  901. spin_unlock_irqrestore(&amp;cache_lock, flags);
  902. - return ret;
  903. + return obj;
  904. }
  905. </programlisting>
  906. <para>
  907. We encapsulate the reference counting in the standard 'get' and 'put'
  908. functions. Now we can return the object itself from
  909. <function>cache_find</function> which has the advantage that the user
  910. can now sleep holding the object (eg. to
  911. <function>copy_to_user</function> to name to userspace).
  912. </para>
  913. <para>
  914. The other point to note is that I said a reference should be held for
  915. every pointer to the object: thus the reference count is 1 when first
  916. inserted into the cache. In some versions the framework does not hold
  917. a reference count, but they are more complicated.
  918. </para>
  919. <sect2 id="examples-refcnt-atomic">
  920. <title>Using Atomic Operations For The Reference Count</title>
  921. <para>
  922. In practice, <type>atomic_t</type> would usually be used for
  923. <structfield>refcnt</structfield>. There are a number of atomic
  924. operations defined in
  925. <filename class="headerfile">include/asm/atomic.h</filename>: these are
  926. guaranteed to be seen atomically from all CPUs in the system, so no
  927. lock is required. In this case, it is simpler than using spinlocks,
  928. although for anything non-trivial using spinlocks is clearer. The
  929. <function>atomic_inc</function> and
  930. <function>atomic_dec_and_test</function> are used instead of the
  931. standard increment and decrement operators, and the lock is no longer
  932. used to protect the reference count itself.
  933. </para>
  934. <programlisting>
  935. --- cache.c.refcnt 2003-12-09 15:00:35.000000000 +1100
  936. +++ cache.c.refcnt-atomic 2003-12-11 15:49:42.000000000 +1100
  937. @@ -7,7 +7,7 @@
  938. struct object
  939. {
  940. struct list_head list;
  941. - unsigned int refcnt;
  942. + atomic_t refcnt;
  943. int id;
  944. char name[32];
  945. int popularity;
  946. @@ -18,33 +18,15 @@
  947. static unsigned int cache_num = 0;
  948. #define MAX_CACHE_SIZE 10
  949. -static void __object_put(struct object *obj)
  950. -{
  951. - if (--obj-&gt;refcnt == 0)
  952. - kfree(obj);
  953. -}
  954. -
  955. -static void __object_get(struct object *obj)
  956. -{
  957. - obj-&gt;refcnt++;
  958. -}
  959. -
  960. void object_put(struct object *obj)
  961. {
  962. - unsigned long flags;
  963. -
  964. - spin_lock_irqsave(&amp;cache_lock, flags);
  965. - __object_put(obj);
  966. - spin_unlock_irqrestore(&amp;cache_lock, flags);
  967. + if (atomic_dec_and_test(&amp;obj-&gt;refcnt))
  968. + kfree(obj);
  969. }
  970. void object_get(struct object *obj)
  971. {
  972. - unsigned long flags;
  973. -
  974. - spin_lock_irqsave(&amp;cache_lock, flags);
  975. - __object_get(obj);
  976. - spin_unlock_irqrestore(&amp;cache_lock, flags);
  977. + atomic_inc(&amp;obj-&gt;refcnt);
  978. }
  979. /* Must be holding cache_lock */
  980. @@ -65,7 +47,7 @@
  981. {
  982. BUG_ON(!obj);
  983. list_del(&amp;obj-&gt;list);
  984. - __object_put(obj);
  985. + object_put(obj);
  986. cache_num--;
  987. }
  988. @@ -94,7 +76,7 @@
  989. strlcpy(obj-&gt;name, name, sizeof(obj-&gt;name));
  990. obj-&gt;id = id;
  991. obj-&gt;popularity = 0;
  992. - obj-&gt;refcnt = 1; /* The cache holds a reference */
  993. + atomic_set(&amp;obj-&gt;refcnt, 1); /* The cache holds a reference */
  994. spin_lock_irqsave(&amp;cache_lock, flags);
  995. __cache_add(obj);
  996. @@ -119,7 +101,7 @@
  997. spin_lock_irqsave(&amp;cache_lock, flags);
  998. obj = __cache_find(id);
  999. if (obj)
  1000. - __object_get(obj);
  1001. + object_get(obj);
  1002. spin_unlock_irqrestore(&amp;cache_lock, flags);
  1003. return obj;
  1004. }
  1005. </programlisting>
  1006. </sect2>
  1007. </sect1>
  1008. <sect1 id="examples-lock-per-obj">
  1009. <title>Protecting The Objects Themselves</title>
  1010. <para>
  1011. In these examples, we assumed that the objects (except the reference
  1012. counts) never changed once they are created. If we wanted to allow
  1013. the name to change, there are three possibilities:
  1014. </para>
  1015. <itemizedlist>
  1016. <listitem>
  1017. <para>
  1018. You can make <symbol>cache_lock</symbol> non-static, and tell people
  1019. to grab that lock before changing the name in any object.
  1020. </para>
  1021. </listitem>
  1022. <listitem>
  1023. <para>
  1024. You can provide a <function>cache_obj_rename</function> which grabs
  1025. this lock and changes the name for the caller, and tell everyone to
  1026. use that function.
  1027. </para>
  1028. </listitem>
  1029. <listitem>
  1030. <para>
  1031. You can make the <symbol>cache_lock</symbol> protect only the cache
  1032. itself, and use another lock to protect the name.
  1033. </para>
  1034. </listitem>
  1035. </itemizedlist>
  1036. <para>
  1037. Theoretically, you can make the locks as fine-grained as one lock for
  1038. every field, for every object. In practice, the most common variants
  1039. are:
  1040. </para>
  1041. <itemizedlist>
  1042. <listitem>
  1043. <para>
  1044. One lock which protects the infrastructure (the <symbol>cache</symbol>
  1045. list in this example) and all the objects. This is what we have done
  1046. so far.
  1047. </para>
  1048. </listitem>
  1049. <listitem>
  1050. <para>
  1051. One lock which protects the infrastructure (including the list
  1052. pointers inside the objects), and one lock inside the object which
  1053. protects the rest of that object.
  1054. </para>
  1055. </listitem>
  1056. <listitem>
  1057. <para>
  1058. Multiple locks to protect the infrastructure (eg. one lock per hash
  1059. chain), possibly with a separate per-object lock.
  1060. </para>
  1061. </listitem>
  1062. </itemizedlist>
  1063. <para>
  1064. Here is the "lock-per-object" implementation:
  1065. </para>
  1066. <programlisting>
  1067. --- cache.c.refcnt-atomic 2003-12-11 15:50:54.000000000 +1100
  1068. +++ cache.c.perobjectlock 2003-12-11 17:15:03.000000000 +1100
  1069. @@ -6,11 +6,17 @@
  1070. struct object
  1071. {
  1072. + /* These two protected by cache_lock. */
  1073. struct list_head list;
  1074. + int popularity;
  1075. +
  1076. atomic_t refcnt;
  1077. +
  1078. + /* Doesn't change once created. */
  1079. int id;
  1080. +
  1081. + spinlock_t lock; /* Protects the name */
  1082. char name[32];
  1083. - int popularity;
  1084. };
  1085. static spinlock_t cache_lock = SPIN_LOCK_UNLOCKED;
  1086. @@ -77,6 +84,7 @@
  1087. obj-&gt;id = id;
  1088. obj-&gt;popularity = 0;
  1089. atomic_set(&amp;obj-&gt;refcnt, 1); /* The cache holds a reference */
  1090. + spin_lock_init(&amp;obj-&gt;lock);
  1091. spin_lock_irqsave(&amp;cache_lock, flags);
  1092. __cache_add(obj);
  1093. </programlisting>
  1094. <para>
  1095. Note that I decide that the <structfield>popularity</structfield>
  1096. count should be protected by the <symbol>cache_lock</symbol> rather
  1097. than the per-object lock: this is because it (like the
  1098. <structname>struct list_head</structname> inside the object) is
  1099. logically part of the infrastructure. This way, I don't need to grab
  1100. the lock of every object in <function>__cache_add</function> when
  1101. seeking the least popular.
  1102. </para>
  1103. <para>
  1104. I also decided that the <structfield>id</structfield> member is
  1105. unchangeable, so I don't need to grab each object lock in
  1106. <function>__cache_find()</function> to examine the
  1107. <structfield>id</structfield>: the object lock is only used by a
  1108. caller who wants to read or write the <structfield>name</structfield>
  1109. field.
  1110. </para>
  1111. <para>
  1112. Note also that I added a comment describing what data was protected by
  1113. which locks. This is extremely important, as it describes the runtime
  1114. behavior of the code, and can be hard to gain from just reading. And
  1115. as Alan Cox says, <quote>Lock data, not code</quote>.
  1116. </para>
  1117. </sect1>
  1118. </chapter>
  1119. <chapter id="common-problems">
  1120. <title>Common Problems</title>
  1121. <sect1 id="deadlock">
  1122. <title>Deadlock: Simple and Advanced</title>
  1123. <para>
  1124. There is a coding bug where a piece of code tries to grab a
  1125. spinlock twice: it will spin forever, waiting for the lock to
  1126. be released (spinlocks, rwlocks and semaphores are not
  1127. recursive in Linux). This is trivial to diagnose: not a
  1128. stay-up-five-nights-talk-to-fluffy-code-bunnies kind of
  1129. problem.
  1130. </para>
  1131. <para>
  1132. For a slightly more complex case, imagine you have a region
  1133. shared by a softirq and user context. If you use a
  1134. <function>spin_lock()</function> call to protect it, it is
  1135. possible that the user context will be interrupted by the softirq
  1136. while it holds the lock, and the softirq will then spin
  1137. forever trying to get the same lock.
  1138. </para>
  1139. <para>
  1140. Both of these are called deadlock, and as shown above, it can
  1141. occur even with a single CPU (although not on UP compiles,
  1142. since spinlocks vanish on kernel compiles with
  1143. <symbol>CONFIG_SMP</symbol>=n. You'll still get data corruption
  1144. in the second example).
  1145. </para>
  1146. <para>
  1147. This complete lockup is easy to diagnose: on SMP boxes the
  1148. watchdog timer or compiling with <symbol>DEBUG_SPINLOCKS</symbol> set
  1149. (<filename>include/linux/spinlock.h</filename>) will show this up
  1150. immediately when it happens.
  1151. </para>
  1152. <para>
  1153. A more complex problem is the so-called 'deadly embrace',
  1154. involving two or more locks. Say you have a hash table: each
  1155. entry in the table is a spinlock, and a chain of hashed
  1156. objects. Inside a softirq handler, you sometimes want to
  1157. alter an object from one place in the hash to another: you
  1158. grab the spinlock of the old hash chain and the spinlock of
  1159. the new hash chain, and delete the object from the old one,
  1160. and insert it in the new one.
  1161. </para>
  1162. <para>
  1163. There are two problems here. First, if your code ever
  1164. tries to move the object to the same chain, it will deadlock
  1165. with itself as it tries to lock it twice. Secondly, if the
  1166. same softirq on another CPU is trying to move another object
  1167. in the reverse direction, the following could happen:
  1168. </para>
  1169. <table>
  1170. <title>Consequences</title>
  1171. <tgroup cols="2" align="left">
  1172. <thead>
  1173. <row>
  1174. <entry>CPU 1</entry>
  1175. <entry>CPU 2</entry>
  1176. </row>
  1177. </thead>
  1178. <tbody>
  1179. <row>
  1180. <entry>Grab lock A -&gt; OK</entry>
  1181. <entry>Grab lock B -&gt; OK</entry>
  1182. </row>
  1183. <row>
  1184. <entry>Grab lock B -&gt; spin</entry>
  1185. <entry>Grab lock A -&gt; spin</entry>
  1186. </row>
  1187. </tbody>
  1188. </tgroup>
  1189. </table>
  1190. <para>
  1191. The two CPUs will spin forever, waiting for the other to give up
  1192. their lock. It will look, smell, and feel like a crash.
  1193. </para>
  1194. </sect1>
  1195. <sect1 id="techs-deadlock-prevent">
  1196. <title>Preventing Deadlock</title>
  1197. <para>
  1198. Textbooks will tell you that if you always lock in the same
  1199. order, you will never get this kind of deadlock. Practice
  1200. will tell you that this approach doesn't scale: when I
  1201. create a new lock, I don't understand enough of the kernel
  1202. to figure out where in the 5000 lock hierarchy it will fit.
  1203. </para>
  1204. <para>
  1205. The best locks are encapsulated: they never get exposed in
  1206. headers, and are never held around calls to non-trivial
  1207. functions outside the same file. You can read through this
  1208. code and see that it will never deadlock, because it never
  1209. tries to grab another lock while it has that one. People
  1210. using your code don't even need to know you are using a
  1211. lock.
  1212. </para>
  1213. <para>
  1214. A classic problem here is when you provide callbacks or
  1215. hooks: if you call these with the lock held, you risk simple
  1216. deadlock, or a deadly embrace (who knows what the callback
  1217. will do?). Remember, the other programmers are out to get
  1218. you, so don't do this.
  1219. </para>
  1220. <sect2 id="techs-deadlock-overprevent">
  1221. <title>Overzealous Prevention Of Deadlocks</title>
  1222. <para>
  1223. Deadlocks are problematic, but not as bad as data
  1224. corruption. Code which grabs a read lock, searches a list,
  1225. fails to find what it wants, drops the read lock, grabs a
  1226. write lock and inserts the object has a race condition.
  1227. </para>
  1228. <para>
  1229. If you don't see why, please stay the fuck away from my code.
  1230. </para>
  1231. </sect2>
  1232. </sect1>
  1233. <sect1 id="racing-timers">
  1234. <title>Racing Timers: A Kernel Pastime</title>
  1235. <para>
  1236. Timers can produce their own special problems with races.
  1237. Consider a collection of objects (list, hash, etc) where each
  1238. object has a timer which is due to destroy it.
  1239. </para>
  1240. <para>
  1241. If you want to destroy the entire collection (say on module
  1242. removal), you might do the following:
  1243. </para>
  1244. <programlisting>
  1245. /* THIS CODE BAD BAD BAD BAD: IF IT WAS ANY WORSE IT WOULD USE
  1246. HUNGARIAN NOTATION */
  1247. spin_lock_bh(&amp;list_lock);
  1248. while (list) {
  1249. struct foo *next = list-&gt;next;
  1250. del_timer(&amp;list-&gt;timer);
  1251. kfree(list);
  1252. list = next;
  1253. }
  1254. spin_unlock_bh(&amp;list_lock);
  1255. </programlisting>
  1256. <para>
  1257. Sooner or later, this will crash on SMP, because a timer can
  1258. have just gone off before the <function>spin_lock_bh()</function>,
  1259. and it will only get the lock after we
  1260. <function>spin_unlock_bh()</function>, and then try to free
  1261. the element (which has already been freed!).
  1262. </para>
  1263. <para>
  1264. This can be avoided by checking the result of
  1265. <function>del_timer()</function>: if it returns
  1266. <returnvalue>1</returnvalue>, the timer has been deleted.
  1267. If <returnvalue>0</returnvalue>, it means (in this
  1268. case) that it is currently running, so we can do:
  1269. </para>
  1270. <programlisting>
  1271. retry:
  1272. spin_lock_bh(&amp;list_lock);
  1273. while (list) {
  1274. struct foo *next = list-&gt;next;
  1275. if (!del_timer(&amp;list-&gt;timer)) {
  1276. /* Give timer a chance to delete this */
  1277. spin_unlock_bh(&amp;list_lock);
  1278. goto retry;
  1279. }
  1280. kfree(list);
  1281. list = next;
  1282. }
  1283. spin_unlock_bh(&amp;list_lock);
  1284. </programlisting>
  1285. <para>
  1286. Another common problem is deleting timers which restart
  1287. themselves (by calling <function>add_timer()</function> at the end
  1288. of their timer function). Because this is a fairly common case
  1289. which is prone to races, you should use <function>del_timer_sync()</function>
  1290. (<filename class="headerfile">include/linux/timer.h</filename>)
  1291. to handle this case. It returns the number of times the timer
  1292. had to be deleted before we finally stopped it from adding itself back
  1293. in.
  1294. </para>
  1295. </sect1>
  1296. </chapter>
  1297. <chapter id="Efficiency">
  1298. <title>Locking Speed</title>
  1299. <para>
  1300. There are three main things to worry about when considering speed of
  1301. some code which does locking. First is concurrency: how many things
  1302. are going to be waiting while someone else is holding a lock. Second
  1303. is the time taken to actually acquire and release an uncontended lock.
  1304. Third is using fewer, or smarter locks. I'm assuming that the lock is
  1305. used fairly often: otherwise, you wouldn't be concerned about
  1306. efficiency.
  1307. </para>
  1308. <para>
  1309. Concurrency depends on how long the lock is usually held: you should
  1310. hold the lock for as long as needed, but no longer. In the cache
  1311. example, we always create the object without the lock held, and then
  1312. grab the lock only when we are ready to insert it in the list.
  1313. </para>
  1314. <para>
  1315. Acquisition times depend on how much damage the lock operations do to
  1316. the pipeline (pipeline stalls) and how likely it is that this CPU was
  1317. the last one to grab the lock (ie. is the lock cache-hot for this
  1318. CPU): on a machine with more CPUs, this likelihood drops fast.
  1319. Consider a 700MHz Intel Pentium III: an instruction takes about 0.7ns,
  1320. an atomic increment takes about 58ns, a lock which is cache-hot on
  1321. this CPU takes 160ns, and a cacheline transfer from another CPU takes
  1322. an additional 170 to 360ns. (These figures from Paul McKenney's
  1323. <ulink url="http://www.linuxjournal.com/article.php?sid=6993"> Linux
  1324. Journal RCU article</ulink>).
  1325. </para>
  1326. <para>
  1327. These two aims conflict: holding a lock for a short time might be done
  1328. by splitting locks into parts (such as in our final per-object-lock
  1329. example), but this increases the number of lock acquisitions, and the
  1330. results are often slower than having a single lock. This is another
  1331. reason to advocate locking simplicity.
  1332. </para>
  1333. <para>
  1334. The third concern is addressed below: there are some methods to reduce
  1335. the amount of locking which needs to be done.
  1336. </para>
  1337. <sect1 id="efficiency-rwlocks">
  1338. <title>Read/Write Lock Variants</title>
  1339. <para>
  1340. Both spinlocks and semaphores have read/write variants:
  1341. <type>rwlock_t</type> and <structname>struct rw_semaphore</structname>.
  1342. These divide users into two classes: the readers and the writers. If
  1343. you are only reading the data, you can get a read lock, but to write to
  1344. the data you need the write lock. Many people can hold a read lock,
  1345. but a writer must be sole holder.
  1346. </para>
  1347. <para>
  1348. If your code divides neatly along reader/writer lines (as our
  1349. cache code does), and the lock is held by readers for
  1350. significant lengths of time, using these locks can help. They
  1351. are slightly slower than the normal locks though, so in practice
  1352. <type>rwlock_t</type> is not usually worthwhile.
  1353. </para>
  1354. </sect1>
  1355. <sect1 id="efficiency-read-copy-update">
  1356. <title>Avoiding Locks: Read Copy Update</title>
  1357. <para>
  1358. There is a special method of read/write locking called Read Copy
  1359. Update. Using RCU, the readers can avoid taking a lock
  1360. altogether: as we expect our cache to be read more often than
  1361. updated (otherwise the cache is a waste of time), it is a
  1362. candidate for this optimization.
  1363. </para>
  1364. <para>
  1365. How do we get rid of read locks? Getting rid of read locks
  1366. means that writers may be changing the list underneath the
  1367. readers. That is actually quite simple: we can read a linked
  1368. list while an element is being added if the writer adds the
  1369. element very carefully. For example, adding
  1370. <symbol>new</symbol> to a single linked list called
  1371. <symbol>list</symbol>:
  1372. </para>
  1373. <programlisting>
  1374. new-&gt;next = list-&gt;next;
  1375. wmb();
  1376. list-&gt;next = new;
  1377. </programlisting>
  1378. <para>
  1379. The <function>wmb()</function> is a write memory barrier. It
  1380. ensures that the first operation (setting the new element's
  1381. <symbol>next</symbol> pointer) is complete and will be seen by
  1382. all CPUs, before the second operation is (putting the new
  1383. element into the list). This is important, since modern
  1384. compilers and modern CPUs can both reorder instructions unless
  1385. told otherwise: we want a reader to either not see the new
  1386. element at all, or see the new element with the
  1387. <symbol>next</symbol> pointer correctly pointing at the rest of
  1388. the list.
  1389. </para>
  1390. <para>
  1391. Fortunately, there is a function to do this for standard
  1392. <structname>struct list_head</structname> lists:
  1393. <function>list_add_rcu()</function>
  1394. (<filename>include/linux/list.h</filename>).
  1395. </para>
  1396. <para>
  1397. Removing an element from the list is even simpler: we replace
  1398. the pointer to the old element with a pointer to its successor,
  1399. and readers will either see it, or skip over it.
  1400. </para>
  1401. <programlisting>
  1402. list-&gt;next = old-&gt;next;
  1403. </programlisting>
  1404. <para>
  1405. There is <function>list_del_rcu()</function>
  1406. (<filename>include/linux/list.h</filename>) which does this (the
  1407. normal version poisons the old object, which we don't want).
  1408. </para>
  1409. <para>
  1410. The reader must also be careful: some CPUs can look through the
  1411. <symbol>next</symbol> pointer to start reading the contents of
  1412. the next element early, but don't realize that the pre-fetched
  1413. contents is wrong when the <symbol>next</symbol> pointer changes
  1414. underneath them. Once again, there is a
  1415. <function>list_for_each_entry_rcu()</function>
  1416. (<filename>include/linux/list.h</filename>) to help you. Of
  1417. course, writers can just use
  1418. <function>list_for_each_entry()</function>, since there cannot
  1419. be two simultaneous writers.
  1420. </para>
  1421. <para>
  1422. Our final dilemma is this: when can we actually destroy the
  1423. removed element? Remember, a reader might be stepping through
  1424. this element in the list right now: if we free this element and
  1425. the <symbol>next</symbol> pointer changes, the reader will jump
  1426. off into garbage and crash. We need to wait until we know that
  1427. all the readers who were traversing the list when we deleted the
  1428. element are finished. We use <function>call_rcu()</function> to
  1429. register a callback which will actually destroy the object once
  1430. the readers are finished.
  1431. </para>
  1432. <para>
  1433. But how does Read Copy Update know when the readers are
  1434. finished? The method is this: firstly, the readers always
  1435. traverse the list inside
  1436. <function>rcu_read_lock()</function>/<function>rcu_read_unlock()</function>
  1437. pairs: these simply disable preemption so the reader won't go to
  1438. sleep while reading the list.
  1439. </para>
  1440. <para>
  1441. RCU then waits until every other CPU has slept at least once:
  1442. since readers cannot sleep, we know that any readers which were
  1443. traversing the list during the deletion are finished, and the
  1444. callback is triggered. The real Read Copy Update code is a
  1445. little more optimized than this, but this is the fundamental
  1446. idea.
  1447. </para>
  1448. <programlisting>
  1449. --- cache.c.perobjectlock 2003-12-11 17:15:03.000000000 +1100
  1450. +++ cache.c.rcupdate 2003-12-11 17:55:14.000000000 +1100
  1451. @@ -1,15 +1,18 @@
  1452. #include &lt;linux/list.h&gt;
  1453. #include &lt;linux/slab.h&gt;
  1454. #include &lt;linux/string.h&gt;
  1455. +#include &lt;linux/rcupdate.h&gt;
  1456. #include &lt;asm/semaphore.h&gt;
  1457. #include &lt;asm/errno.h&gt;
  1458. struct object
  1459. {
  1460. - /* These two protected by cache_lock. */
  1461. + /* This is protected by RCU */
  1462. struct list_head list;
  1463. int popularity;
  1464. + struct rcu_head rcu;
  1465. +
  1466. atomic_t refcnt;
  1467. /* Doesn't change once created. */
  1468. @@ -40,7 +43,7 @@
  1469. {
  1470. struct object *i;
  1471. - list_for_each_entry(i, &amp;cache, list) {
  1472. + list_for_each_entry_rcu(i, &amp;cache, list) {
  1473. if (i-&gt;id == id) {
  1474. i-&gt;popularity++;
  1475. return i;
  1476. @@ -49,19 +52,25 @@
  1477. return NULL;
  1478. }
  1479. +/* Final discard done once we know no readers are looking. */
  1480. +static void cache_delete_rcu(void *arg)
  1481. +{
  1482. + object_put(arg);
  1483. +}
  1484. +
  1485. /* Must be holding cache_lock */
  1486. static void __cache_delete(struct object *obj)
  1487. {
  1488. BUG_ON(!obj);
  1489. - list_del(&amp;obj-&gt;list);
  1490. - object_put(obj);
  1491. + list_del_rcu(&amp;obj-&gt;list);
  1492. cache_num--;
  1493. + call_rcu(&amp;obj-&gt;rcu, cache_delete_rcu, obj);
  1494. }
  1495. /* Must be holding cache_lock */
  1496. static void __cache_add(struct object *obj)
  1497. {
  1498. - list_add(&amp;obj-&gt;list, &amp;cache);
  1499. + list_add_rcu(&amp;obj-&gt;list, &amp;cache);
  1500. if (++cache_num > MAX_CACHE_SIZE) {
  1501. struct object *i, *outcast = NULL;
  1502. list_for_each_entry(i, &amp;cache, list) {
  1503. @@ -85,6 +94,7 @@
  1504. obj-&gt;popularity = 0;
  1505. atomic_set(&amp;obj-&gt;refcnt, 1); /* The cache holds a reference */
  1506. spin_lock_init(&amp;obj-&gt;lock);
  1507. + INIT_RCU_HEAD(&amp;obj-&gt;rcu);
  1508. spin_lock_irqsave(&amp;cache_lock, flags);
  1509. __cache_add(obj);
  1510. @@ -104,12 +114,11 @@
  1511. struct object *cache_find(int id)
  1512. {
  1513. struct object *obj;
  1514. - unsigned long flags;
  1515. - spin_lock_irqsave(&amp;cache_lock, flags);
  1516. + rcu_read_lock();
  1517. obj = __cache_find(id);
  1518. if (obj)
  1519. object_get(obj);
  1520. - spin_unlock_irqrestore(&amp;cache_lock, flags);
  1521. + rcu_read_unlock();
  1522. return obj;
  1523. }
  1524. </programlisting>
  1525. <para>
  1526. Note that the reader will alter the
  1527. <structfield>popularity</structfield> member in
  1528. <function>__cache_find()</function>, and now it doesn't hold a lock.
  1529. One solution would be to make it an <type>atomic_t</type>, but for
  1530. this usage, we don't really care about races: an approximate result is
  1531. good enough, so I didn't change it.
  1532. </para>
  1533. <para>
  1534. The result is that <function>cache_find()</function> requires no
  1535. synchronization with any other functions, so is almost as fast on SMP
  1536. as it would be on UP.
  1537. </para>
  1538. <para>
  1539. There is a furthur optimization possible here: remember our original
  1540. cache code, where there were no reference counts and the caller simply
  1541. held the lock whenever using the object? This is still possible: if
  1542. you hold the lock, noone can delete the object, so you don't need to
  1543. get and put the reference count.
  1544. </para>
  1545. <para>
  1546. Now, because the 'read lock' in RCU is simply disabling preemption, a
  1547. caller which always has preemption disabled between calling
  1548. <function>cache_find()</function> and
  1549. <function>object_put()</function> does not need to actually get and
  1550. put the reference count: we could expose
  1551. <function>__cache_find()</function> by making it non-static, and
  1552. such callers could simply call that.
  1553. </para>
  1554. <para>
  1555. The benefit here is that the reference count is not written to: the
  1556. object is not altered in any way, which is much faster on SMP
  1557. machines due to caching.
  1558. </para>
  1559. </sect1>
  1560. <sect1 id="per-cpu">
  1561. <title>Per-CPU Data</title>
  1562. <para>
  1563. Another technique for avoiding locking which is used fairly
  1564. widely is to duplicate information for each CPU. For example,
  1565. if you wanted to keep a count of a common condition, you could
  1566. use a spin lock and a single counter. Nice and simple.
  1567. </para>
  1568. <para>
  1569. If that was too slow (it's usually not, but if you've got a
  1570. really big machine to test on and can show that it is), you
  1571. could instead use a counter for each CPU, then none of them need
  1572. an exclusive lock. See <function>DEFINE_PER_CPU()</function>,
  1573. <function>get_cpu_var()</function> and
  1574. <function>put_cpu_var()</function>
  1575. (<filename class="headerfile">include/linux/percpu.h</filename>).
  1576. </para>
  1577. <para>
  1578. Of particular use for simple per-cpu counters is the
  1579. <type>local_t</type> type, and the
  1580. <function>cpu_local_inc()</function> and related functions,
  1581. which are more efficient than simple code on some architectures
  1582. (<filename class="headerfile">include/asm/local.h</filename>).
  1583. </para>
  1584. <para>
  1585. Note that there is no simple, reliable way of getting an exact
  1586. value of such a counter, without introducing more locks. This
  1587. is not a problem for some uses.
  1588. </para>
  1589. </sect1>
  1590. <sect1 id="mostly-hardirq">
  1591. <title>Data Which Mostly Used By An IRQ Handler</title>
  1592. <para>
  1593. If data is always accessed from within the same IRQ handler, you
  1594. don't need a lock at all: the kernel already guarantees that the
  1595. irq handler will not run simultaneously on multiple CPUs.
  1596. </para>
  1597. <para>
  1598. Manfred Spraul points out that you can still do this, even if
  1599. the data is very occasionally accessed in user context or
  1600. softirqs/tasklets. The irq handler doesn't use a lock, and
  1601. all other accesses are done as so:
  1602. </para>
  1603. <programlisting>
  1604. spin_lock(&amp;lock);
  1605. disable_irq(irq);
  1606. ...
  1607. enable_irq(irq);
  1608. spin_unlock(&amp;lock);
  1609. </programlisting>
  1610. <para>
  1611. The <function>disable_irq()</function> prevents the irq handler
  1612. from running (and waits for it to finish if it's currently
  1613. running on other CPUs). The spinlock prevents any other
  1614. accesses happening at the same time. Naturally, this is slower
  1615. than just a <function>spin_lock_irq()</function> call, so it
  1616. only makes sense if this type of access happens extremely
  1617. rarely.
  1618. </para>
  1619. </sect1>
  1620. </chapter>
  1621. <chapter id="sleeping-things">
  1622. <title>What Functions Are Safe To Call From Interrupts?</title>
  1623. <para>
  1624. Many functions in the kernel sleep (ie. call schedule())
  1625. directly or indirectly: you can never call them while holding a
  1626. spinlock, or with preemption disabled. This also means you need
  1627. to be in user context: calling them from an interrupt is illegal.
  1628. </para>
  1629. <sect1 id="sleeping">
  1630. <title>Some Functions Which Sleep</title>
  1631. <para>
  1632. The most common ones are listed below, but you usually have to
  1633. read the code to find out if other calls are safe. If everyone
  1634. else who calls it can sleep, you probably need to be able to
  1635. sleep, too. In particular, registration and deregistration
  1636. functions usually expect to be called from user context, and can
  1637. sleep.
  1638. </para>
  1639. <itemizedlist>
  1640. <listitem>
  1641. <para>
  1642. Accesses to
  1643. <firstterm linkend="gloss-userspace">userspace</firstterm>:
  1644. </para>
  1645. <itemizedlist>
  1646. <listitem>
  1647. <para>
  1648. <function>copy_from_user()</function>
  1649. </para>
  1650. </listitem>
  1651. <listitem>
  1652. <para>
  1653. <function>copy_to_user()</function>
  1654. </para>
  1655. </listitem>
  1656. <listitem>
  1657. <para>
  1658. <function>get_user()</function>
  1659. </para>
  1660. </listitem>
  1661. <listitem>
  1662. <para>
  1663. <function> put_user()</function>
  1664. </para>
  1665. </listitem>
  1666. </itemizedlist>
  1667. </listitem>
  1668. <listitem>
  1669. <para>
  1670. <function>kmalloc(GFP_KERNEL)</function>
  1671. </para>
  1672. </listitem>
  1673. <listitem>
  1674. <para>
  1675. <function>down_interruptible()</function> and
  1676. <function>down()</function>
  1677. </para>
  1678. <para>
  1679. There is a <function>down_trylock()</function> which can be
  1680. used inside interrupt context, as it will not sleep.
  1681. <function>up()</function> will also never sleep.
  1682. </para>
  1683. </listitem>
  1684. </itemizedlist>
  1685. </sect1>
  1686. <sect1 id="dont-sleep">
  1687. <title>Some Functions Which Don't Sleep</title>
  1688. <para>
  1689. Some functions are safe to call from any context, or holding
  1690. almost any lock.
  1691. </para>
  1692. <itemizedlist>
  1693. <listitem>
  1694. <para>
  1695. <function>printk()</function>
  1696. </para>
  1697. </listitem>
  1698. <listitem>
  1699. <para>
  1700. <function>kfree()</function>
  1701. </para>
  1702. </listitem>
  1703. <listitem>
  1704. <para>
  1705. <function>add_timer()</function> and <function>del_timer()</function>
  1706. </para>
  1707. </listitem>
  1708. </itemizedlist>
  1709. </sect1>
  1710. </chapter>
  1711. <chapter id="references">
  1712. <title>Further reading</title>
  1713. <itemizedlist>
  1714. <listitem>
  1715. <para>
  1716. <filename>Documentation/spinlocks.txt</filename>:
  1717. Linus Torvalds' spinlocking tutorial in the kernel sources.
  1718. </para>
  1719. </listitem>
  1720. <listitem>
  1721. <para>
  1722. Unix Systems for Modern Architectures: Symmetric
  1723. Multiprocessing and Caching for Kernel Programmers:
  1724. </para>
  1725. <para>
  1726. Curt Schimmel's very good introduction to kernel level
  1727. locking (not written for Linux, but nearly everything
  1728. applies). The book is expensive, but really worth every
  1729. penny to understand SMP locking. [ISBN: 0201633388]
  1730. </para>
  1731. </listitem>
  1732. </itemizedlist>
  1733. </chapter>
  1734. <chapter id="thanks">
  1735. <title>Thanks</title>
  1736. <para>
  1737. Thanks to Telsa Gwynne for DocBooking, neatening and adding
  1738. style.
  1739. </para>
  1740. <para>
  1741. Thanks to Martin Pool, Philipp Rumpf, Stephen Rothwell, Paul
  1742. Mackerras, Ruedi Aschwanden, Alan Cox, Manfred Spraul, Tim
  1743. Waugh, Pete Zaitcev, James Morris, Robert Love, Paul McKenney,
  1744. John Ashby for proofreading, correcting, flaming, commenting.
  1745. </para>
  1746. <para>
  1747. Thanks to the cabal for having no influence on this document.
  1748. </para>
  1749. </chapter>
  1750. <glossary id="glossary">
  1751. <title>Glossary</title>
  1752. <glossentry id="gloss-preemption">
  1753. <glossterm>preemption</glossterm>
  1754. <glossdef>
  1755. <para>
  1756. Prior to 2.5, or when <symbol>CONFIG_PREEMPT</symbol> is
  1757. unset, processes in user context inside the kernel would not
  1758. preempt each other (ie. you had that CPU until you have it up,
  1759. except for interrupts). With the addition of
  1760. <symbol>CONFIG_PREEMPT</symbol> in 2.5.4, this changed: when
  1761. in user context, higher priority tasks can "cut in": spinlocks
  1762. were changed to disable preemption, even on UP.
  1763. </para>
  1764. </glossdef>
  1765. </glossentry>
  1766. <glossentry id="gloss-bh">
  1767. <glossterm>bh</glossterm>
  1768. <glossdef>
  1769. <para>
  1770. Bottom Half: for historical reasons, functions with
  1771. '_bh' in them often now refer to any software interrupt, e.g.
  1772. <function>spin_lock_bh()</function> blocks any software interrupt
  1773. on the current CPU. Bottom halves are deprecated, and will
  1774. eventually be replaced by tasklets. Only one bottom half will be
  1775. running at any time.
  1776. </para>
  1777. </glossdef>
  1778. </glossentry>
  1779. <glossentry id="gloss-hwinterrupt">
  1780. <glossterm>Hardware Interrupt / Hardware IRQ</glossterm>
  1781. <glossdef>
  1782. <para>
  1783. Hardware interrupt request. <function>in_irq()</function> returns
  1784. <returnvalue>true</returnvalue> in a hardware interrupt handler.
  1785. </para>
  1786. </glossdef>
  1787. </glossentry>
  1788. <glossentry id="gloss-interruptcontext">
  1789. <glossterm>Interrupt Context</glossterm>
  1790. <glossdef>
  1791. <para>
  1792. Not user context: processing a hardware irq or software irq.
  1793. Indicated by the <function>in_interrupt()</function> macro
  1794. returning <returnvalue>true</returnvalue>.
  1795. </para>
  1796. </glossdef>
  1797. </glossentry>
  1798. <glossentry id="gloss-smp">
  1799. <glossterm><acronym>SMP</acronym></glossterm>
  1800. <glossdef>
  1801. <para>
  1802. Symmetric Multi-Processor: kernels compiled for multiple-CPU
  1803. machines. (CONFIG_SMP=y).
  1804. </para>
  1805. </glossdef>
  1806. </glossentry>
  1807. <glossentry id="gloss-softirq">
  1808. <glossterm>Software Interrupt / softirq</glossterm>
  1809. <glossdef>
  1810. <para>
  1811. Software interrupt handler. <function>in_irq()</function> returns
  1812. <returnvalue>false</returnvalue>; <function>in_softirq()</function>
  1813. returns <returnvalue>true</returnvalue>. Tasklets and softirqs
  1814. both fall into the category of 'software interrupts'.
  1815. </para>
  1816. <para>
  1817. Strictly speaking a softirq is one of up to 32 enumerated software
  1818. interrupts which can run on multiple CPUs at once.
  1819. Sometimes used to refer to tasklets as
  1820. well (ie. all software interrupts).
  1821. </para>
  1822. </glossdef>
  1823. </glossentry>
  1824. <glossentry id="gloss-tasklet">
  1825. <glossterm>tasklet</glossterm>
  1826. <glossdef>
  1827. <para>
  1828. A dynamically-registrable software interrupt,
  1829. which is guaranteed to only run on one CPU at a time.
  1830. </para>
  1831. </glossdef>
  1832. </glossentry>
  1833. <glossentry id="gloss-timers">
  1834. <glossterm>timer</glossterm>
  1835. <glossdef>
  1836. <para>
  1837. A dynamically-registrable software interrupt, which is run at
  1838. (or close to) a given time. When running, it is just like a
  1839. tasklet (in fact, they are called from the TIMER_SOFTIRQ).
  1840. </para>
  1841. </glossdef>
  1842. </glossentry>
  1843. <glossentry id="gloss-up">
  1844. <glossterm><acronym>UP</acronym></glossterm>
  1845. <glossdef>
  1846. <para>
  1847. Uni-Processor: Non-SMP. (CONFIG_SMP=n).
  1848. </para>
  1849. </glossdef>
  1850. </glossentry>
  1851. <glossentry id="gloss-usercontext">
  1852. <glossterm>User Context</glossterm>
  1853. <glossdef>
  1854. <para>
  1855. The kernel executing on behalf of a particular process (ie. a
  1856. system call or trap) or kernel thread. You can tell which
  1857. process with the <symbol>current</symbol> macro.) Not to
  1858. be confused with userspace. Can be interrupted by software or
  1859. hardware interrupts.
  1860. </para>
  1861. </glossdef>
  1862. </glossentry>
  1863. <glossentry id="gloss-userspace">
  1864. <glossterm>Userspace</glossterm>
  1865. <glossdef>
  1866. <para>
  1867. A process executing its own code outside the kernel.
  1868. </para>
  1869. </glossdef>
  1870. </glossentry>
  1871. </glossary>
  1872. </book>