Commit 78305de2 authored by Matthew Wilcox's avatar Matthew Wilcox Committed by Matthew Wilcox

Remove mention of semaphores from kernel-locking

Since the consensus seems to be to eliminate semaphores where possible,
we shouldn't be educating people about how to use them as locks.  Use
mutexes instead.  Semaphores should be described in a separate document
if we end up keeping them.
Signed-off-by: default avatarMatthew Wilcox <willy@linux.intel.com>
Acked-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 338b9bb3
...@@ -219,10 +219,10 @@ ...@@ -219,10 +219,10 @@
</para> </para>
<sect1 id="lock-intro"> <sect1 id="lock-intro">
<title>Three Main Types of Kernel Locks: Spinlocks, Mutexes and Semaphores</title> <title>Two Main Types of Kernel Locks: Spinlocks and Mutexes</title>
<para> <para>
There are three main types of kernel locks. The fundamental type There are two main types of kernel locks. The fundamental type
is the spinlock is the spinlock
(<filename class="headerfile">include/asm/spinlock.h</filename>), (<filename class="headerfile">include/asm/spinlock.h</filename>),
which is a very simple single-holder lock: if you can't get the which is a very simple single-holder lock: if you can't get the
...@@ -239,14 +239,6 @@ ...@@ -239,14 +239,6 @@
can't sleep (see <xref linkend="sleeping-things"/>), and so have to can't sleep (see <xref linkend="sleeping-things"/>), and so have to
use a spinlock instead. use a spinlock instead.
</para> </para>
<para>
The third type is a semaphore
(<filename class="headerfile">include/linux/semaphore.h</filename>): it
can have more than one holder at any time (the number decided at
initialization time), although it is most commonly used as a
single-holder lock (a mutex). If you can't get a semaphore, your
task will be suspended and later on woken up - just like for mutexes.
</para>
<para> <para>
Neither type of lock is recursive: see Neither type of lock is recursive: see
<xref linkend="deadlock"/>. <xref linkend="deadlock"/>.
...@@ -278,7 +270,7 @@ ...@@ -278,7 +270,7 @@
</para> </para>
<para> <para>
Semaphores still exist, because they are required for Mutexes still exist, because they are required for
synchronization between <firstterm linkend="gloss-usercontext">user synchronization between <firstterm linkend="gloss-usercontext">user
contexts</firstterm>, as we will see below. contexts</firstterm>, as we will see below.
</para> </para>
...@@ -289,18 +281,17 @@ ...@@ -289,18 +281,17 @@
<para> <para>
If you have a data structure which is only ever accessed from If you have a data structure which is only ever accessed from
user context, then you can use a simple semaphore user context, then you can use a simple mutex
(<filename>linux/linux/semaphore.h</filename>) to protect it. This (<filename>include/linux/mutex.h</filename>) to protect it. This
is the most trivial case: you initialize the semaphore to the number is the most trivial case: you initialize the mutex. Then you can
of resources available (usually 1), and call call <function>mutex_lock_interruptible()</function> to grab the mutex,
<function>down_interruptible()</function> to grab the semaphore, and and <function>mutex_unlock()</function> to release it. There is also a
<function>up()</function> to release it. There is also a <function>mutex_lock()</function>, which should be avoided, because it
<function>down()</function>, which should be avoided, because it
will not return if a signal is received. will not return if a signal is received.
</para> </para>
<para> <para>
Example: <filename>linux/net/core/netfilter.c</filename> allows Example: <filename>net/netfilter/nf_sockopt.c</filename> allows
registration of new <function>setsockopt()</function> and registration of new <function>setsockopt()</function> and
<function>getsockopt()</function> calls, with <function>getsockopt()</function> calls, with
<function>nf_register_sockopt()</function>. Registration and <function>nf_register_sockopt()</function>. Registration and
...@@ -515,7 +506,7 @@ ...@@ -515,7 +506,7 @@
<listitem> <listitem>
<para> <para>
If you are in a process context (any syscall) and want to If you are in a process context (any syscall) and want to
lock other process out, use a semaphore. You can take a semaphore lock other process out, use a mutex. You can take a mutex
and sleep (<function>copy_from_user*(</function> or and sleep (<function>copy_from_user*(</function> or
<function>kmalloc(x,GFP_KERNEL)</function>). <function>kmalloc(x,GFP_KERNEL)</function>).
</para> </para>
...@@ -662,7 +653,7 @@ ...@@ -662,7 +653,7 @@
<entry>SLBH</entry> <entry>SLBH</entry>
<entry>SLBH</entry> <entry>SLBH</entry>
<entry>SLBH</entry> <entry>SLBH</entry>
<entry>DI</entry> <entry>MLI</entry>
<entry>None</entry> <entry>None</entry>
</row> </row>
...@@ -692,8 +683,8 @@ ...@@ -692,8 +683,8 @@
<entry>spin_lock_bh</entry> <entry>spin_lock_bh</entry>
</row> </row>
<row> <row>
<entry>DI</entry> <entry>MLI</entry>
<entry>down_interruptible</entry> <entry>mutex_lock_interruptible</entry>
</row> </row>
</tbody> </tbody>
...@@ -1310,7 +1301,7 @@ as Alan Cox says, <quote>Lock data, not code</quote>. ...@@ -1310,7 +1301,7 @@ as Alan Cox says, <quote>Lock data, not code</quote>.
<para> <para>
There is a coding bug where a piece of code tries to grab a There is a coding bug where a piece of code tries to grab a
spinlock twice: it will spin forever, waiting for the lock to spinlock twice: it will spin forever, waiting for the lock to
be released (spinlocks, rwlocks and semaphores are not be released (spinlocks, rwlocks and mutexes are not
recursive in Linux). This is trivial to diagnose: not a recursive in Linux). This is trivial to diagnose: not a
stay-up-five-nights-talk-to-fluffy-code-bunnies kind of stay-up-five-nights-talk-to-fluffy-code-bunnies kind of
problem. problem.
...@@ -1335,7 +1326,7 @@ as Alan Cox says, <quote>Lock data, not code</quote>. ...@@ -1335,7 +1326,7 @@ as Alan Cox says, <quote>Lock data, not code</quote>.
<para> <para>
This complete lockup is easy to diagnose: on SMP boxes the This complete lockup is easy to diagnose: on SMP boxes the
watchdog timer or compiling with <symbol>DEBUG_SPINLOCKS</symbol> set watchdog timer or compiling with <symbol>DEBUG_SPINLOCK</symbol> set
(<filename>include/linux/spinlock.h</filename>) will show this up (<filename>include/linux/spinlock.h</filename>) will show this up
immediately when it happens. immediately when it happens.
</para> </para>
...@@ -1558,7 +1549,7 @@ the amount of locking which needs to be done. ...@@ -1558,7 +1549,7 @@ the amount of locking which needs to be done.
<title>Read/Write Lock Variants</title> <title>Read/Write Lock Variants</title>
<para> <para>
Both spinlocks and semaphores have read/write variants: Both spinlocks and mutexes have read/write variants:
<type>rwlock_t</type> and <structname>struct rw_semaphore</structname>. <type>rwlock_t</type> and <structname>struct rw_semaphore</structname>.
These divide users into two classes: the readers and the writers. If These divide users into two classes: the readers and the writers. If
you are only reading the data, you can get a read lock, but to write to you are only reading the data, you can get a read lock, but to write to
...@@ -1681,7 +1672,7 @@ the amount of locking which needs to be done. ...@@ -1681,7 +1672,7 @@ the amount of locking which needs to be done.
#include &lt;linux/slab.h&gt; #include &lt;linux/slab.h&gt;
#include &lt;linux/string.h&gt; #include &lt;linux/string.h&gt;
+#include &lt;linux/rcupdate.h&gt; +#include &lt;linux/rcupdate.h&gt;
#include &lt;linux/semaphore.h&gt; #include &lt;linux/mutex.h&gt;
#include &lt;asm/errno.h&gt; #include &lt;asm/errno.h&gt;
struct object struct object
...@@ -1913,7 +1904,7 @@ machines due to caching. ...@@ -1913,7 +1904,7 @@ machines due to caching.
</listitem> </listitem>
<listitem> <listitem>
<para> <para>
<function> put_user()</function> <function>put_user()</function>
</para> </para>
</listitem> </listitem>
</itemizedlist> </itemizedlist>
...@@ -1927,13 +1918,13 @@ machines due to caching. ...@@ -1927,13 +1918,13 @@ machines due to caching.
<listitem> <listitem>
<para> <para>
<function>down_interruptible()</function> and <function>mutex_lock_interruptible()</function> and
<function>down()</function> <function>mutex_lock()</function>
</para> </para>
<para> <para>
There is a <function>down_trylock()</function> which can be There is a <function>mutex_trylock()</function> which can be
used inside interrupt context, as it will not sleep. used inside interrupt context, as it will not sleep.
<function>up()</function> will also never sleep. <function>mutex_unlock()</function> will also never sleep.
</para> </para>
</listitem> </listitem>
</itemizedlist> </itemizedlist>
...@@ -2023,7 +2014,7 @@ machines due to caching. ...@@ -2023,7 +2014,7 @@ machines due to caching.
<para> <para>
Prior to 2.5, or when <symbol>CONFIG_PREEMPT</symbol> is Prior to 2.5, or when <symbol>CONFIG_PREEMPT</symbol> is
unset, processes in user context inside the kernel would not unset, processes in user context inside the kernel would not
preempt each other (ie. you had that CPU until you have it up, preempt each other (ie. you had that CPU until you gave it up,
except for interrupts). With the addition of except for interrupts). With the addition of
<symbol>CONFIG_PREEMPT</symbol> in 2.5.4, this changed: when <symbol>CONFIG_PREEMPT</symbol> in 2.5.4, this changed: when
in user context, higher priority tasks can "cut in": spinlocks in user context, higher priority tasks can "cut in": spinlocks
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment