1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Chapter&#160;8.&#160;Common Problems</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="examples-lock-per-obj.html" title="Protecting The Objects Themselves"><link rel="next" href="techs-deadlock-prevent.html" title="Preventing Deadlock"></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&#160;8.&#160;Common Problems</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="examples-lock-per-obj.html">Prev</a>&#160;</td><th width="60%" align="center">&#160;</th><td width="20%" align="right">&#160;<a accesskey="n" href="techs-deadlock-prevent.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="common-problems"></a>Chapter&#160;8.&#160;Common Problems</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="sect1"><a href="common-problems.html#deadlock">Deadlock: Simple and Advanced</a></span></dt><dt><span class="sect1"><a href="techs-deadlock-prevent.html">Preventing Deadlock</a></span></dt><dd><dl><dt><span class="sect2"><a href="techs-deadlock-prevent.html#techs-deadlock-overprevent">Overzealous Prevention Of Deadlocks</a></span></dt></dl></dd><dt><span class="sect1"><a href="racing-timers.html">Racing Timers: A Kernel Pastime</a></span></dt></dl></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="deadlock"></a>Deadlock: Simple and Advanced</h2></div></div></div><p>
2      There is a coding bug where a piece of code tries to grab a
3      spinlock twice: it will spin forever, waiting for the lock to
4      be released (spinlocks, rwlocks and mutexes are not
5      recursive in Linux).  This is trivial to diagnose: not a
6      stay-up-five-nights-talk-to-fluffy-code-bunnies kind of
7      problem.
8    </p><p>
9      For a slightly more complex case, imagine you have a region
10      shared by a softirq and user context.  If you use a
11      <code class="function">spin_lock()</code> call to protect it, it is 
12      possible that the user context will be interrupted by the softirq
13      while it holds the lock, and the softirq will then spin
14      forever trying to get the same lock.
15    </p><p>
16      Both of these are called deadlock, and as shown above, it can
17      occur even with a single CPU (although not on UP compiles,
18      since spinlocks vanish on kernel compiles with 
19      <span class="symbol">CONFIG_SMP</span>=n. You'll still get data corruption 
20      in the second example).
21    </p><p>
22      This complete lockup is easy to diagnose: on SMP boxes the
23      watchdog timer or compiling with <span class="symbol">DEBUG_SPINLOCK</span> set
24      (<code class="filename">include/linux/spinlock.h</code>) will show this up 
25      immediately when it happens.
26    </p><p>
27      A more complex problem is the so-called 'deadly embrace',
28      involving two or more locks.  Say you have a hash table: each
29      entry in the table is a spinlock, and a chain of hashed
30      objects.  Inside a softirq handler, you sometimes want to
31      alter an object from one place in the hash to another: you
32      grab the spinlock of the old hash chain and the spinlock of
33      the new hash chain, and delete the object from the old one,
34      and insert it in the new one.
35    </p><p>
36      There are two problems here.  First, if your code ever
37      tries to move the object to the same chain, it will deadlock
38      with itself as it tries to lock it twice.  Secondly, if the
39      same softirq on another CPU is trying to move another object
40      in the reverse direction, the following could happen:
41    </p><div class="table"><a name="idp1121820172"></a><p class="title"><b>Table&#160;8.1.&#160;Consequences</b></p><div class="table-contents"><table summary="Consequences" border="1"><colgroup><col><col></colgroup><thead><tr><th align="left">CPU 1</th><th align="left">CPU 2</th></tr></thead><tbody><tr><td align="left">Grab lock A -&gt; OK</td><td align="left">Grab lock B -&gt; OK</td></tr><tr><td align="left">Grab lock B -&gt; spin</td><td align="left">Grab lock A -&gt; spin</td></tr></tbody></table></div></div><br class="table-break"><p>
42      The two CPUs will spin forever, waiting for the other to give up
43      their lock.  It will look, smell, and feel like a crash.
44    </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="examples-lock-per-obj.html">Prev</a>&#160;</td><td width="20%" align="center">&#160;</td><td width="40%" align="right">&#160;<a accesskey="n" href="techs-deadlock-prevent.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Protecting The Objects Themselves&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Preventing Deadlock</td></tr></table></div></body></html>
45