1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Chapter 5. Cheat Sheet For Locking</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Unreliable Guide To Locking"><link rel="up" href="index.html" title="Unreliable Guide To Locking"><link rel="prev" href="hardirq-hardirq.html" title="Locking Between Two Hard IRQ Handlers"><link rel="next" href="trylock-functions.html" title="Chapter 6. The trylock Functions"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 5. Cheat Sheet For Locking</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="hardirq-hardirq.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="trylock-functions.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="cheatsheet"></a>Chapter 5. Cheat Sheet For Locking</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="sect1"><a href="cheatsheet.html#minimum-lock-reqirements">Table of Minimum Requirements</a></span></dt></dl></div><p> 2 Pete Zaitcev gives the following summary: 3 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p> 4 If you are in a process context (any syscall) and want to 5 lock other process out, use a mutex. You can take a mutex 6 and sleep (<code class="function">copy_from_user*(</code> or 7 <code class="function">kmalloc(x,GFP_KERNEL)</code>). 8 </p></li><li class="listitem"><p> 9 Otherwise (== data can be touched in an interrupt), use 10 <code class="function">spin_lock_irqsave()</code> and 11 <code class="function">spin_unlock_irqrestore()</code>. 12 </p></li><li class="listitem"><p> 13 Avoid holding spinlock for more than 5 lines of code and 14 across any function call (except accessors like 15 <code class="function">readb</code>). 16 </p></li></ul></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="minimum-lock-reqirements"></a>Table of Minimum Requirements</h2></div></div></div><p> The following table lists the <span class="emphasis"><em>minimum</em></span> 17 locking requirements between various contexts. In some cases, 18 the same context can only be running on one CPU at a time, so 19 no locking is required for that context (eg. a particular 20 thread can only run on one CPU at a time, but if it needs 21 shares data with another thread, locking is required). 22 </p><p> 23 Remember the advice above: you can always use 24 <code class="function">spin_lock_irqsave()</code>, which is a superset 25 of all other spinlock primitives. 26 </p><div class="table"><a name="idp1123787068"></a><p class="title"><b>Table 5.1. Table of Locking Requirements</b></p><div class="table-contents"><table summary="Table of Locking Requirements" border="1"><colgroup><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody><tr><td> </td><td>IRQ Handler A</td><td>IRQ Handler B</td><td>Softirq A</td><td>Softirq B</td><td>Tasklet A</td><td>Tasklet B</td><td>Timer A</td><td>Timer B</td><td>User Context A</td><td>User Context B</td></tr><tr><td>IRQ Handler A</td><td>None</td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td></tr><tr><td>IRQ Handler B</td><td>SLIS</td><td>None</td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td></tr><tr><td>Softirq A</td><td>SLI</td><td>SLI</td><td>SL</td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td></tr><tr><td>Softirq B</td><td>SLI</td><td>SLI</td><td>SL</td><td>SL</td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td></tr><tr><td>Tasklet A</td><td>SLI</td><td>SLI</td><td>SL</td><td>SL</td><td>None</td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td></tr><tr><td>Tasklet B</td><td>SLI</td><td>SLI</td><td>SL</td><td>SL</td><td>SL</td><td>None</td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td></tr><tr><td>Timer A</td><td>SLI</td><td>SLI</td><td>SL</td><td>SL</td><td>SL</td><td>SL</td><td>None</td><td class="auto-generated"> </td><td class="auto-generated"> </td><td class="auto-generated"> </td></tr><tr><td>Timer B</td><td>SLI</td><td>SLI</td><td>SL</td><td>SL</td><td>SL</td><td>SL</td><td>SL</td><td>None</td><td class="auto-generated"> </td><td class="auto-generated"> </td></tr><tr><td>User Context A</td><td>SLI</td><td>SLI</td><td>SLBH</td><td>SLBH</td><td>SLBH</td><td>SLBH</td><td>SLBH</td><td>SLBH</td><td>None</td><td class="auto-generated"> </td></tr><tr><td>User Context B</td><td>SLI</td><td>SLI</td><td>SLBH</td><td>SLBH</td><td>SLBH</td><td>SLBH</td><td>SLBH</td><td>SLBH</td><td>MLI</td><td>None</td></tr></tbody></table></div></div><br class="table-break"><div class="table"><a name="idp1123093084"></a><p class="title"><b>Table 5.2. Legend for Locking Requirements Table</b></p><div class="table-contents"><table summary="Legend for Locking Requirements Table" border="1"><colgroup><col><col></colgroup><tbody><tr><td>SLIS</td><td>spin_lock_irqsave</td></tr><tr><td>SLI</td><td>spin_lock_irq</td></tr><tr><td>SL</td><td>spin_lock</td></tr><tr><td>SLBH</td><td>spin_lock_bh</td></tr><tr><td>MLI</td><td>mutex_lock_interruptible</td></tr></tbody></table></div></div><br class="table-break"></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="hardirq-hardirq.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="trylock-functions.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Locking Between Two Hard IRQ Handlers </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 6. The trylock Functions</td></tr></table></div></body></html> 27