1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Chapter 3. Locking in the Linux Kernel</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="races.html" title="Chapter 2. The Problem With Concurrency"><link rel="next" href="uniprocessor.html" title="Locks and Uniprocessor Kernels"></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 3. Locking in the Linux Kernel</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="races.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="uniprocessor.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="locks"></a>Chapter 3. Locking in the Linux Kernel</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="sect1"><a href="locks.html#lock-intro">Two Main Types of Kernel Locks: Spinlocks and Mutexes</a></span></dt><dt><span class="sect1"><a href="uniprocessor.html">Locks and Uniprocessor Kernels</a></span></dt><dt><span class="sect1"><a href="usercontextlocking.html">Locking Only In User Context</a></span></dt><dt><span class="sect1"><a href="lock-user-bh.html">Locking Between User Context and Softirqs</a></span></dt><dt><span class="sect1"><a href="lock-user-tasklet.html">Locking Between User Context and Tasklets</a></span></dt><dt><span class="sect1"><a href="lock-user-timers.html">Locking Between User Context and Timers</a></span></dt><dt><span class="sect1"><a href="lock-tasklets.html">Locking Between Tasklets/Timers</a></span></dt><dd><dl><dt><span class="sect2"><a href="lock-tasklets.html#lock-tasklets-same">The Same Tasklet/Timer</a></span></dt><dt><span class="sect2"><a href="lock-tasklets.html#lock-tasklets-different">Different Tasklets/Timers</a></span></dt></dl></dd><dt><span class="sect1"><a href="lock-softirqs.html">Locking Between Softirqs</a></span></dt><dd><dl><dt><span class="sect2"><a href="lock-softirqs.html#lock-softirqs-same">The Same Softirq</a></span></dt><dt><span class="sect2"><a href="lock-softirqs.html#lock-softirqs-different">Different Softirqs</a></span></dt></dl></dd></dl></div><p> 2 If I could give you one piece of advice: never sleep with anyone 3 crazier than yourself. But if I had to give you advice on 4 locking: <span class="emphasis"><em>keep it simple</em></span>. 5 </p><p> 6 Be reluctant to introduce new locks. 7 </p><p> 8 Strangely enough, this last one is the exact reverse of my advice when 9 you <span class="emphasis"><em>have</em></span> slept with someone crazier than yourself. 10 And you should think about getting a big dog. 11 </p><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="lock-intro"></a>Two Main Types of Kernel Locks: Spinlocks and Mutexes</h2></div></div></div><p> 12 There are two main types of kernel locks. The fundamental type 13 is the spinlock 14 (<code class="filename">include/asm/spinlock.h</code>), 15 which is a very simple single-holder lock: if you can't get the 16 spinlock, you keep trying (spinning) until you can. Spinlocks are 17 very small and fast, and can be used anywhere. 18 </p><p> 19 The second type is a mutex 20 (<code class="filename">include/linux/mutex.h</code>): it 21 is like a spinlock, but you may block holding a mutex. 22 If you can't lock a mutex, your task will suspend itself, and be woken 23 up when the mutex is released. This means the CPU can do something 24 else while you are waiting. There are many cases when you simply 25 can't sleep (see <a class="xref" href="sleeping-things.html" title="Chapter 10. What Functions Are Safe To Call From Interrupts?">Chapter 10, <i>What Functions Are Safe To Call From Interrupts?</i></a>), and so have to 26 use a spinlock instead. 27 </p><p> 28 Neither type of lock is recursive: see 29 <a class="xref" href="common-problems.html#deadlock" title="Deadlock: Simple and Advanced">the section called “Deadlock: Simple and Advanced”</a>. 30 </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="races.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="uniprocessor.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 2. The Problem With Concurrency </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Locks and Uniprocessor Kernels</td></tr></table></div></body></html> 31