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" []>
5 <book id="LKLockingGuide">
7 <title>Unreliable Guide To Locking</title>
11 <firstname>Rusty</firstname>
12 <surname>Russell</surname>
15 <email>rusty@rustcorp.com.au</email>
23 <holder>Rusty Russell</holder>
28 This documentation is free software; you can redistribute
29 it and/or modify it under the terms of the GNU General Public
30 License as published by the Free Software Foundation; either
31 version 2 of the License, or (at your option) any later
36 This program is distributed in the hope that it will be
37 useful, but WITHOUT ANY WARRANTY; without even the implied
38 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
39 See the GNU General Public License for more details.
43 You should have received a copy of the GNU General Public
44 License along with this program; if not, write to the Free
45 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
50 For more details see the file COPYING in the source
51 distribution of Linux.
58 <title>Introduction</title>
60 Welcome, to Rusty's Remarkably Unreliable Guide to Kernel
61 Locking issues. This document describes the locking systems in
62 the Linux Kernel in 2.6.
65 With the wide availability of HyperThreading, and <firstterm
66 linkend="gloss-preemption">preemption </firstterm> in the Linux
67 Kernel, everyone hacking on the kernel needs to know the
68 fundamentals of concurrency and locking for
69 <firstterm linkend="gloss-smp"><acronym>SMP</acronym></firstterm>.
74 <title>The Problem With Concurrency</title>
76 (Skip this if you know what a Race Condition is).
79 In a normal program, you can increment a counter like so:
82 very_important_count++;
86 This is what they would expect to happen:
90 <title>Expected Results</title>
92 <tgroup cols="2" align="left">
96 <entry>Instance 1</entry>
97 <entry>Instance 2</entry>
103 <entry>read very_important_count (5)</entry>
107 <entry>add 1 (6)</entry>
111 <entry>write very_important_count (6)</entry>
116 <entry>read very_important_count (6)</entry>
120 <entry>add 1 (7)</entry>
124 <entry>write very_important_count (7)</entry>
132 This is what might happen:
136 <title>Possible Results</title>
138 <tgroup cols="2" align="left">
141 <entry>Instance 1</entry>
142 <entry>Instance 2</entry>
148 <entry>read very_important_count (5)</entry>
153 <entry>read very_important_count (5)</entry>
156 <entry>add 1 (6)</entry>
161 <entry>add 1 (6)</entry>
164 <entry>write very_important_count (6)</entry>
169 <entry>write very_important_count (6)</entry>
175 <sect1 id="race-condition">
176 <title>Race Conditions and Critical Regions</title>
178 This overlap, where the result depends on the
179 relative timing of multiple tasks, is called a <firstterm>race condition</firstterm>.
180 The piece of code containing the concurrency issue is called a
181 <firstterm>critical region</firstterm>. And especially since Linux starting running
182 on SMP machines, they became one of the major issues in kernel
183 design and implementation.
186 Preemption can have the same effect, even if there is only one
187 CPU: by preempting one task during the critical region, we have
188 exactly the same race condition. In this case the thread which
189 preempts might run the critical region itself.
192 The solution is to recognize when these simultaneous accesses
193 occur, and use locks to make sure that only one instance can
194 enter the critical region at any time. There are many
195 friendly primitives in the Linux kernel to help you do this.
196 And then there are the unfriendly primitives, but I'll pretend
203 <title>Locking in the Linux Kernel</title>
206 If I could give you one piece of advice: never sleep with anyone
207 crazier than yourself. But if I had to give you advice on
208 locking: <emphasis>keep it simple</emphasis>.
212 Be reluctant to introduce new locks.
216 Strangely enough, this last one is the exact reverse of my advice when
217 you <emphasis>have</emphasis> slept with someone crazier than yourself.
218 And you should think about getting a big dog.
221 <sect1 id="lock-intro">
222 <title>Two Main Types of Kernel Locks: Spinlocks and Semaphores</title>
225 There are two main types of kernel locks. The fundamental type
227 (<filename class="headerfile">include/asm/spinlock.h</filename>),
228 which is a very simple single-holder lock: if you can't get the
229 spinlock, you keep trying (spinning) until you can. Spinlocks are
230 very small and fast, and can be used anywhere.
233 The second type is a semaphore
234 (<filename class="headerfile">include/asm/semaphore.h</filename>): it
235 can have more than one holder at any time (the number decided at
236 initialization time), although it is most commonly used as a
237 single-holder lock (a mutex). If you can't get a semaphore,
238 your task will put itself on the queue, and be woken up when the
239 semaphore is released. This means the CPU will do something
240 else while you are waiting, but there are many cases when you
241 simply can't sleep (see <xref linkend="sleeping-things"/>), and so
242 have to use a spinlock instead.
245 Neither type of lock is recursive: see
246 <xref linkend="deadlock"/>.
250 <sect1 id="uniprocessor">
251 <title>Locks and Uniprocessor Kernels</title>
254 For kernels compiled without <symbol>CONFIG_SMP</symbol>, and
255 without <symbol>CONFIG_PREEMPT</symbol> spinlocks do not exist at
256 all. This is an excellent design decision: when no-one else can
257 run at the same time, there is no reason to have a lock.
261 If the kernel is compiled without <symbol>CONFIG_SMP</symbol>,
262 but <symbol>CONFIG_PREEMPT</symbol> is set, then spinlocks
263 simply disable preemption, which is sufficient to prevent any
264 races. For most purposes, we can think of preemption as
265 equivalent to SMP, and not worry about it separately.
269 You should always test your locking code with <symbol>CONFIG_SMP</symbol>
270 and <symbol>CONFIG_PREEMPT</symbol> enabled, even if you don't have an SMP test box, because it
271 will still catch some kinds of locking bugs.
275 Semaphores still exist, because they are required for
276 synchronization between <firstterm linkend="gloss-usercontext">user
277 contexts</firstterm>, as we will see below.
281 <sect1 id="usercontextlocking">
282 <title>Locking Only In User Context</title>
285 If you have a data structure which is only ever accessed from
286 user context, then you can use a simple semaphore
287 (<filename>linux/asm/semaphore.h</filename>) to protect it. This
288 is the most trivial case: you initialize the semaphore to the number
289 of resources available (usually 1), and call
290 <function>down_interruptible()</function> to grab the semaphore, and
291 <function>up()</function> to release it. There is also a
292 <function>down()</function>, which should be avoided, because it
293 will not return if a signal is received.
297 Example: <filename>linux/net/core/netfilter.c</filename> allows
298 registration of new <function>setsockopt()</function> and
299 <function>getsockopt()</function> calls, with
300 <function>nf_register_sockopt()</function>. Registration and
301 de-registration are only done on module load and unload (and boot
302 time, where there is no concurrency), and the list of registrations
303 is only consulted for an unknown <function>setsockopt()</function>
304 or <function>getsockopt()</function> system call. The
305 <varname>nf_sockopt_mutex</varname> is perfect to protect this,
306 especially since the setsockopt and getsockopt calls may well
311 <sect1 id="lock-user-bh">
312 <title>Locking Between User Context and Softirqs</title>
315 If a <firstterm linkend="gloss-softirq">softirq</firstterm> shares
316 data with user context, you have two problems. Firstly, the current
317 user context can be interrupted by a softirq, and secondly, the
318 critical region could be entered from another CPU. This is where
319 <function>spin_lock_bh()</function>
320 (<filename class="headerfile">include/linux/spinlock.h</filename>) is
321 used. It disables softirqs on that CPU, then grabs the lock.
322 <function>spin_unlock_bh()</function> does the reverse. (The
323 '_bh' suffix is a historical reference to "Bottom Halves", the
324 old name for software interrupts. It should really be
325 called spin_lock_softirq()' in a perfect world).
329 Note that you can also use <function>spin_lock_irq()</function>
330 or <function>spin_lock_irqsave()</function> here, which stop
331 hardware interrupts as well: see <xref linkend="hardirq-context"/>.
335 This works perfectly for <firstterm linkend="gloss-up"><acronym>UP
336 </acronym></firstterm> as well: the spin lock vanishes, and this macro
337 simply becomes <function>local_bh_disable()</function>
338 (<filename class="headerfile">include/linux/interrupt.h</filename>), which
339 protects you from the softirq being run.
343 <sect1 id="lock-user-tasklet">
344 <title>Locking Between User Context and Tasklets</title>
347 This is exactly the same as above, because <firstterm
348 linkend="gloss-tasklet">tasklets</firstterm> are actually run
353 <sect1 id="lock-user-timers">
354 <title>Locking Between User Context and Timers</title>
357 This, too, is exactly the same as above, because <firstterm
358 linkend="gloss-timers">timers</firstterm> are actually run from
359 a softirq. From a locking point of view, tasklets and timers
364 <sect1 id="lock-tasklets">
365 <title>Locking Between Tasklets/Timers</title>
368 Sometimes a tasklet or timer might want to share data with
369 another tasklet or timer.
372 <sect2 id="lock-tasklets-same">
373 <title>The Same Tasklet/Timer</title>
375 Since a tasklet is never run on two CPUs at once, you don't
376 need to worry about your tasklet being reentrant (running
377 twice at once), even on SMP.
381 <sect2 id="lock-tasklets-different">
382 <title>Different Tasklets/Timers</title>
384 If another tasklet/timer wants
385 to share data with your tasklet or timer , you will both need to use
386 <function>spin_lock()</function> and
387 <function>spin_unlock()</function> calls.
388 <function>spin_lock_bh()</function> is
389 unnecessary here, as you are already in a tasklet, and
390 none will be run on the same CPU.
395 <sect1 id="lock-softirqs">
396 <title>Locking Between Softirqs</title>
399 Often a softirq might
400 want to share data with itself or a tasklet/timer.
403 <sect2 id="lock-softirqs-same">
404 <title>The Same Softirq</title>
407 The same softirq can run on the other CPUs: you can use a
408 per-CPU array (see <xref linkend="per-cpu"/>) for better
409 performance. If you're going so far as to use a softirq,
410 you probably care about scalable performance enough
411 to justify the extra complexity.
415 You'll need to use <function>spin_lock()</function> and
416 <function>spin_unlock()</function> for shared data.
420 <sect2 id="lock-softirqs-different">
421 <title>Different Softirqs</title>
424 You'll need to use <function>spin_lock()</function> and
425 <function>spin_unlock()</function> for shared data, whether it
426 be a timer, tasklet, different softirq or the same or another
427 softirq: any of them could be running on a different CPU.
433 <chapter id="hardirq-context">
434 <title>Hard IRQ Context</title>
437 Hardware interrupts usually communicate with a
438 tasklet or softirq. Frequently this involves putting work in a
439 queue, which the softirq will take out.
442 <sect1 id="hardirq-softirq">
443 <title>Locking Between Hard IRQ and Softirqs/Tasklets</title>
446 If a hardware irq handler shares data with a softirq, you have
447 two concerns. Firstly, the softirq processing can be
448 interrupted by a hardware interrupt, and secondly, the
449 critical region could be entered by a hardware interrupt on
450 another CPU. This is where <function>spin_lock_irq()</function> is
451 used. It is defined to disable interrupts on that cpu, then grab
452 the lock. <function>spin_unlock_irq()</function> does the reverse.
456 The irq handler does not to use
457 <function>spin_lock_irq()</function>, because the softirq cannot
458 run while the irq handler is running: it can use
459 <function>spin_lock()</function>, which is slightly faster. The
460 only exception would be if a different hardware irq handler uses
461 the same lock: <function>spin_lock_irq()</function> will stop
462 that from interrupting us.
466 This works perfectly for UP as well: the spin lock vanishes,
467 and this macro simply becomes <function>local_irq_disable()</function>
468 (<filename class="headerfile">include/asm/smp.h</filename>), which
469 protects you from the softirq/tasklet/BH being run.
473 <function>spin_lock_irqsave()</function>
474 (<filename>include/linux/spinlock.h</filename>) is a variant
475 which saves whether interrupts were on or off in a flags word,
476 which is passed to <function>spin_unlock_irqrestore()</function>. This
477 means that the same code can be used inside an hard irq handler (where
478 interrupts are already off) and in softirqs (where the irq
479 disabling is required).
483 Note that softirqs (and hence tasklets and timers) are run on
484 return from hardware interrupts, so
485 <function>spin_lock_irq()</function> also stops these. In that
486 sense, <function>spin_lock_irqsave()</function> is the most
487 general and powerful locking function.
491 <sect1 id="hardirq-hardirq">
492 <title>Locking Between Two Hard IRQ Handlers</title>
494 It is rare to have to share data between two IRQ handlers, but
495 if you do, <function>spin_lock_irqsave()</function> should be
496 used: it is architecture-specific whether all interrupts are
497 disabled inside irq handlers themselves.
503 <chapter id="cheatsheet">
504 <title>Cheat Sheet For Locking</title>
506 Pete Zaitcev gives the following summary:
511 If you are in a process context (any syscall) and want to
512 lock other process out, use a semaphore. You can take a semaphore
513 and sleep (<function>copy_from_user*(</function> or
514 <function>kmalloc(x,GFP_KERNEL)</function>).
519 Otherwise (== data can be touched in an interrupt), use
520 <function>spin_lock_irqsave()</function> and
521 <function>spin_unlock_irqrestore()</function>.
526 Avoid holding spinlock for more than 5 lines of code and
527 across any function call (except accessors like
528 <function>readb</function>).
533 <sect1 id="minimum-lock-reqirements">
534 <title>Table of Minimum Requirements</title>
536 <para> The following table lists the <emphasis>minimum</emphasis>
537 locking requirements between various contexts. In some cases,
538 the same context can only be running on one CPU at a time, so
539 no locking is required for that context (eg. a particular
540 thread can only run on one CPU at a time, but if it needs
541 shares data with another thread, locking is required).
544 Remember the advice above: you can always use
545 <function>spin_lock_irqsave()</function>, which is a superset
546 of all other spinlock primitives.
549 <title>Table of Locking Requirements</title>
554 <entry>IRQ Handler A</entry>
555 <entry>IRQ Handler B</entry>
556 <entry>Softirq A</entry>
557 <entry>Softirq B</entry>
558 <entry>Tasklet A</entry>
559 <entry>Tasklet B</entry>
560 <entry>Timer A</entry>
561 <entry>Timer B</entry>
562 <entry>User Context A</entry>
563 <entry>User Context B</entry>
567 <entry>IRQ Handler A</entry>
572 <entry>IRQ Handler B</entry>
573 <entry>spin_lock_irqsave</entry>
578 <entry>Softirq A</entry>
579 <entry>spin_lock_irq</entry>
580 <entry>spin_lock_irq</entry>
581 <entry>spin_lock</entry>
585 <entry>Softirq B</entry>
586 <entry>spin_lock_irq</entry>
587 <entry>spin_lock_irq</entry>
588 <entry>spin_lock</entry>
589 <entry>spin_lock</entry>
593 <entry>Tasklet A</entry>
594 <entry>spin_lock_irq</entry>
595 <entry>spin_lock_irq</entry>
596 <entry>spin_lock</entry>
597 <entry>spin_lock</entry>
602 <entry>Tasklet B</entry>
603 <entry>spin_lock_irq</entry>
604 <entry>spin_lock_irq</entry>
605 <entry>spin_lock</entry>
606 <entry>spin_lock</entry>
607 <entry>spin_lock</entry>
612 <entry>Timer A</entry>
613 <entry>spin_lock_irq</entry>
614 <entry>spin_lock_irq</entry>
615 <entry>spin_lock</entry>
616 <entry>spin_lock</entry>
617 <entry>spin_lock</entry>
618 <entry>spin_lock</entry>
623 <entry>Timer B</entry>
624 <entry>spin_lock_irq</entry>
625 <entry>spin_lock_irq</entry>
626 <entry>spin_lock</entry>
627 <entry>spin_lock</entry>
628 <entry>spin_lock</entry>
629 <entry>spin_lock</entry>
630 <entry>spin_lock</entry>
635 <entry>User Context A</entry>
636 <entry>spin_lock_irq</entry>
637 <entry>spin_lock_irq</entry>
638 <entry>spin_lock_bh</entry>
639 <entry>spin_lock_bh</entry>
640 <entry>spin_lock_bh</entry>
641 <entry>spin_lock_bh</entry>
642 <entry>spin_lock_bh</entry>
643 <entry>spin_lock_bh</entry>
648 <entry>User Context B</entry>
649 <entry>spin_lock_irq</entry>
650 <entry>spin_lock_irq</entry>
651 <entry>spin_lock_bh</entry>
652 <entry>spin_lock_bh</entry>
653 <entry>spin_lock_bh</entry>
654 <entry>spin_lock_bh</entry>
655 <entry>spin_lock_bh</entry>
656 <entry>spin_lock_bh</entry>
657 <entry>down_interruptible</entry>
667 <chapter id="Examples">
668 <title>Common Examples</title>
670 Let's step through a simple example: a cache of number to name
671 mappings. The cache keeps a count of how often each of the objects is
672 used, and when it gets full, throws out the least used one.
676 <sect1 id="examples-usercontext">
677 <title>All In User Context</title>
679 For our first example, we assume that all operations are in user
680 context (ie. from system calls), so we can sleep. This means we can
681 use a semaphore to protect the cache and all the objects within
686 #include <linux/list.h>
687 #include <linux/slab.h>
688 #include <linux/string.h>
689 #include <asm/semaphore.h>
690 #include <asm/errno.h>
694 struct list_head list;
700 /* Protects the cache, cache_num, and the objects within it */
701 static DECLARE_MUTEX(cache_lock);
702 static LIST_HEAD(cache);
703 static unsigned int cache_num = 0;
704 #define MAX_CACHE_SIZE 10
706 /* Must be holding cache_lock */
707 static struct object *__cache_find(int id)
711 list_for_each_entry(i, &cache, list)
712 if (i->id == id) {
719 /* Must be holding cache_lock */
720 static void __cache_delete(struct object *obj)
723 list_del(&obj->list);
728 /* Must be holding cache_lock */
729 static void __cache_add(struct object *obj)
731 list_add(&obj->list, &cache);
732 if (++cache_num > MAX_CACHE_SIZE) {
733 struct object *i, *outcast = NULL;
734 list_for_each_entry(i, &cache, list) {
735 if (!outcast || i->popularity < outcast->popularity)
738 __cache_delete(outcast);
742 int cache_add(int id, const char *name)
746 if ((obj = kmalloc(sizeof(*obj), GFP_KERNEL)) == NULL)
749 strlcpy(obj->name, name, sizeof(obj->name));
751 obj->popularity = 0;
753 down(&cache_lock);
759 void cache_delete(int id)
761 down(&cache_lock);
762 __cache_delete(__cache_find(id));
766 int cache_find(int id, char *name)
771 down(&cache_lock);
772 obj = __cache_find(id);
775 strcpy(name, obj->name);
783 Note that we always make sure we have the cache_lock when we add,
784 delete, or look up the cache: both the cache infrastructure itself and
785 the contents of the objects are protected by the lock. In this case
786 it's easy, since we copy the data for the user, and never let them
787 access the objects directly.
790 There is a slight (and common) optimization here: in
791 <function>cache_add</function> we set up the fields of the object
792 before grabbing the lock. This is safe, as no-one else can access it
793 until we put it in cache.
797 <sect1 id="examples-interrupt">
798 <title>Accessing From Interrupt Context</title>
800 Now consider the case where <function>cache_find</function> can be
801 called from interrupt context: either a hardware interrupt or a
802 softirq. An example would be a timer which deletes object from the
806 The change is shown below, in standard patch format: the
807 <symbol>-</symbol> are lines which are taken away, and the
808 <symbol>+</symbol> are lines which are added.
811 --- cache.c.usercontext 2003-12-09 13:58:54.000000000 +1100
812 +++ cache.c.interrupt 2003-12-09 14:07:49.000000000 +1100
817 -static DECLARE_MUTEX(cache_lock);
818 +static spinlock_t cache_lock = SPIN_LOCK_UNLOCKED;
819 static LIST_HEAD(cache);
820 static unsigned int cache_num = 0;
821 #define MAX_CACHE_SIZE 10
823 int cache_add(int id, const char *name)
826 + unsigned long flags;
828 if ((obj = kmalloc(sizeof(*obj), GFP_KERNEL)) == NULL)
832 obj->popularity = 0;
834 - down(&cache_lock);
835 + spin_lock_irqsave(&cache_lock, flags);
837 - up(&cache_lock);
838 + spin_unlock_irqrestore(&cache_lock, flags);
842 void cache_delete(int id)
844 - down(&cache_lock);
845 + unsigned long flags;
847 + spin_lock_irqsave(&cache_lock, flags);
848 __cache_delete(__cache_find(id));
849 - up(&cache_lock);
850 + spin_unlock_irqrestore(&cache_lock, flags);
853 int cache_find(int id, char *name)
857 + unsigned long flags;
859 - down(&cache_lock);
860 + spin_lock_irqsave(&cache_lock, flags);
861 obj = __cache_find(id);
864 strcpy(name, obj->name);
866 - up(&cache_lock);
867 + spin_unlock_irqrestore(&cache_lock, flags);
873 Note that the <function>spin_lock_irqsave</function> will turn off
874 interrupts if they are on, otherwise does nothing (if we are already
875 in an interrupt handler), hence these functions are safe to call from
879 Unfortunately, <function>cache_add</function> calls
880 <function>kmalloc</function> with the <symbol>GFP_KERNEL</symbol>
881 flag, which is only legal in user context. I have assumed that
882 <function>cache_add</function> is still only called in user context,
883 otherwise this should become a parameter to
884 <function>cache_add</function>.
887 <sect1 id="examples-refcnt">
888 <title>Exposing Objects Outside This File</title>
890 If our objects contained more information, it might not be sufficient
891 to copy the information in and out: other parts of the code might want
892 to keep pointers to these objects, for example, rather than looking up
893 the id every time. This produces two problems.
896 The first problem is that we use the <symbol>cache_lock</symbol> to
897 protect objects: we'd need to make this non-static so the rest of the
898 code can use it. This makes locking trickier, as it is no longer all
902 The second problem is the lifetime problem: if another structure keeps
903 a pointer to an object, it presumably expects that pointer to remain
904 valid. Unfortunately, this is only guaranteed while you hold the
905 lock, otherwise someone might call <function>cache_delete</function>
906 and even worse, add another object, re-using the same address.
909 As there is only one lock, you can't hold it forever: no-one else would
913 The solution to this problem is to use a reference count: everyone who
914 has a pointer to the object increases it when they first get the
915 object, and drops the reference count when they're finished with it.
916 Whoever drops it to zero knows it is unused, and can actually delete it.
923 --- cache.c.interrupt 2003-12-09 14:25:43.000000000 +1100
924 +++ cache.c.refcnt 2003-12-09 14:33:05.000000000 +1100
928 struct list_head list;
929 + unsigned int refcnt;
934 static unsigned int cache_num = 0;
935 #define MAX_CACHE_SIZE 10
937 +static void __object_put(struct object *obj)
939 + if (--obj->refcnt == 0)
943 +static void __object_get(struct object *obj)
948 +void object_put(struct object *obj)
950 + unsigned long flags;
952 + spin_lock_irqsave(&cache_lock, flags);
954 + spin_unlock_irqrestore(&cache_lock, flags);
957 +void object_get(struct object *obj)
959 + unsigned long flags;
961 + spin_lock_irqsave(&cache_lock, flags);
963 + spin_unlock_irqrestore(&cache_lock, flags);
966 /* Must be holding cache_lock */
967 static struct object *__cache_find(int id)
972 list_del(&obj->list);
978 strlcpy(obj->name, name, sizeof(obj->name));
980 obj->popularity = 0;
981 + obj->refcnt = 1; /* The cache holds a reference */
983 spin_lock_irqsave(&cache_lock, flags);
986 spin_unlock_irqrestore(&cache_lock, flags);
989 -int cache_find(int id, char *name)
990 +struct object *cache_find(int id)
996 spin_lock_irqsave(&cache_lock, flags);
997 obj = __cache_find(id);
1000 - strcpy(name, obj->name);
1003 + __object_get(obj);
1004 spin_unlock_irqrestore(&cache_lock, flags);
1011 We encapsulate the reference counting in the standard 'get' and 'put'
1012 functions. Now we can return the object itself from
1013 <function>cache_find</function> which has the advantage that the user
1014 can now sleep holding the object (eg. to
1015 <function>copy_to_user</function> to name to userspace).
1018 The other point to note is that I said a reference should be held for
1019 every pointer to the object: thus the reference count is 1 when first
1020 inserted into the cache. In some versions the framework does not hold
1021 a reference count, but they are more complicated.
1024 <sect2 id="examples-refcnt-atomic">
1025 <title>Using Atomic Operations For The Reference Count</title>
1027 In practice, <type>atomic_t</type> would usually be used for
1028 <structfield>refcnt</structfield>. There are a number of atomic
1029 operations defined in
1031 <filename class="headerfile">include/asm/atomic.h</filename>: these are
1032 guaranteed to be seen atomically from all CPUs in the system, so no
1033 lock is required. In this case, it is simpler than using spinlocks,
1034 although for anything non-trivial using spinlocks is clearer. The
1035 <function>atomic_inc</function> and
1036 <function>atomic_dec_and_test</function> are used instead of the
1037 standard increment and decrement operators, and the lock is no longer
1038 used to protect the reference count itself.
1042 --- cache.c.refcnt 2003-12-09 15:00:35.000000000 +1100
1043 +++ cache.c.refcnt-atomic 2003-12-11 15:49:42.000000000 +1100
1047 struct list_head list;
1048 - unsigned int refcnt;
1054 static unsigned int cache_num = 0;
1055 #define MAX_CACHE_SIZE 10
1057 -static void __object_put(struct object *obj)
1059 - if (--obj->refcnt == 0)
1063 -static void __object_get(struct object *obj)
1068 void object_put(struct object *obj)
1070 - unsigned long flags;
1072 - spin_lock_irqsave(&cache_lock, flags);
1073 - __object_put(obj);
1074 - spin_unlock_irqrestore(&cache_lock, flags);
1075 + if (atomic_dec_and_test(&obj->refcnt))
1079 void object_get(struct object *obj)
1081 - unsigned long flags;
1083 - spin_lock_irqsave(&cache_lock, flags);
1084 - __object_get(obj);
1085 - spin_unlock_irqrestore(&cache_lock, flags);
1086 + atomic_inc(&obj->refcnt);
1089 /* Must be holding cache_lock */
1093 list_del(&obj->list);
1094 - __object_put(obj);
1100 strlcpy(obj->name, name, sizeof(obj->name));
1102 obj->popularity = 0;
1103 - obj->refcnt = 1; /* The cache holds a reference */
1104 + atomic_set(&obj->refcnt, 1); /* The cache holds a reference */
1106 spin_lock_irqsave(&cache_lock, flags);
1109 spin_lock_irqsave(&cache_lock, flags);
1110 obj = __cache_find(id);
1112 - __object_get(obj);
1114 spin_unlock_irqrestore(&cache_lock, flags);
1121 <sect1 id="examples-lock-per-obj">
1122 <title>Protecting The Objects Themselves</title>
1124 In these examples, we assumed that the objects (except the reference
1125 counts) never changed once they are created. If we wanted to allow
1126 the name to change, there are three possibilities:
1131 You can make <symbol>cache_lock</symbol> non-static, and tell people
1132 to grab that lock before changing the name in any object.
1137 You can provide a <function>cache_obj_rename</function> which grabs
1138 this lock and changes the name for the caller, and tell everyone to
1144 You can make the <symbol>cache_lock</symbol> protect only the cache
1145 itself, and use another lock to protect the name.
1151 Theoretically, you can make the locks as fine-grained as one lock for
1152 every field, for every object. In practice, the most common variants
1158 One lock which protects the infrastructure (the <symbol>cache</symbol>
1159 list in this example) and all the objects. This is what we have done
1165 One lock which protects the infrastructure (including the list
1166 pointers inside the objects), and one lock inside the object which
1167 protects the rest of that object.
1172 Multiple locks to protect the infrastructure (eg. one lock per hash
1173 chain), possibly with a separate per-object lock.
1179 Here is the "lock-per-object" implementation:
1182 --- cache.c.refcnt-atomic 2003-12-11 15:50:54.000000000 +1100
1183 +++ cache.c.perobjectlock 2003-12-11 17:15:03.000000000 +1100
1188 + /* These two protected by cache_lock. */
1189 struct list_head list;
1194 + /* Doesn't change once created. */
1197 + spinlock_t lock; /* Protects the name */
1202 static spinlock_t cache_lock = SPIN_LOCK_UNLOCKED;
1205 obj->popularity = 0;
1206 atomic_set(&obj->refcnt, 1); /* The cache holds a reference */
1207 + spin_lock_init(&obj->lock);
1209 spin_lock_irqsave(&cache_lock, flags);
1214 Note that I decide that the <structfield>popularity</structfield>
1215 count should be protected by the <symbol>cache_lock</symbol> rather
1216 than the per-object lock: this is because it (like the
1217 <structname>struct list_head</structname> inside the object) is
1218 logically part of the infrastructure. This way, I don't need to grab
1219 the lock of every object in <function>__cache_add</function> when
1220 seeking the least popular.
1224 I also decided that the <structfield>id</structfield> member is
1225 unchangeable, so I don't need to grab each object lock in
1226 <function>__cache_find()</function> to examine the
1227 <structfield>id</structfield>: the object lock is only used by a
1228 caller who wants to read or write the <structfield>name</structfield>
1233 Note also that I added a comment describing what data was protected by
1234 which locks. This is extremely important, as it describes the runtime
1235 behavior of the code, and can be hard to gain from just reading. And
1236 as Alan Cox says, <quote>Lock data, not code</quote>.
1241 <chapter id="common-problems">
1242 <title>Common Problems</title>
1243 <sect1 id="deadlock">
1244 <title>Deadlock: Simple and Advanced</title>
1247 There is a coding bug where a piece of code tries to grab a
1248 spinlock twice: it will spin forever, waiting for the lock to
1249 be released (spinlocks, rwlocks and semaphores are not
1250 recursive in Linux). This is trivial to diagnose: not a
1251 stay-up-five-nights-talk-to-fluffy-code-bunnies kind of
1256 For a slightly more complex case, imagine you have a region
1257 shared by a softirq and user context. If you use a
1258 <function>spin_lock()</function> call to protect it, it is
1259 possible that the user context will be interrupted by the softirq
1260 while it holds the lock, and the softirq will then spin
1261 forever trying to get the same lock.
1265 Both of these are called deadlock, and as shown above, it can
1266 occur even with a single CPU (although not on UP compiles,
1267 since spinlocks vanish on kernel compiles with
1268 <symbol>CONFIG_SMP</symbol>=n. You'll still get data corruption
1269 in the second example).
1273 This complete lockup is easy to diagnose: on SMP boxes the
1274 watchdog timer or compiling with <symbol>DEBUG_SPINLOCKS</symbol> set
1275 (<filename>include/linux/spinlock.h</filename>) will show this up
1276 immediately when it happens.
1280 A more complex problem is the so-called 'deadly embrace',
1281 involving two or more locks. Say you have a hash table: each
1282 entry in the table is a spinlock, and a chain of hashed
1283 objects. Inside a softirq handler, you sometimes want to
1284 alter an object from one place in the hash to another: you
1285 grab the spinlock of the old hash chain and the spinlock of
1286 the new hash chain, and delete the object from the old one,
1287 and insert it in the new one.
1291 There are two problems here. First, if your code ever
1292 tries to move the object to the same chain, it will deadlock
1293 with itself as it tries to lock it twice. Secondly, if the
1294 same softirq on another CPU is trying to move another object
1295 in the reverse direction, the following could happen:
1299 <title>Consequences</title>
1301 <tgroup cols="2" align="left">
1305 <entry>CPU 1</entry>
1306 <entry>CPU 2</entry>
1312 <entry>Grab lock A -> OK</entry>
1313 <entry>Grab lock B -> OK</entry>
1316 <entry>Grab lock B -> spin</entry>
1317 <entry>Grab lock A -> spin</entry>
1324 The two CPUs will spin forever, waiting for the other to give up
1325 their lock. It will look, smell, and feel like a crash.
1329 <sect1 id="techs-deadlock-prevent">
1330 <title>Preventing Deadlock</title>
1333 Textbooks will tell you that if you always lock in the same
1334 order, you will never get this kind of deadlock. Practice
1335 will tell you that this approach doesn't scale: when I
1336 create a new lock, I don't understand enough of the kernel
1337 to figure out where in the 5000 lock hierarchy it will fit.
1341 The best locks are encapsulated: they never get exposed in
1342 headers, and are never held around calls to non-trivial
1343 functions outside the same file. You can read through this
1344 code and see that it will never deadlock, because it never
1345 tries to grab another lock while it has that one. People
1346 using your code don't even need to know you are using a
1351 A classic problem here is when you provide callbacks or
1352 hooks: if you call these with the lock held, you risk simple
1353 deadlock, or a deadly embrace (who knows what the callback
1354 will do?). Remember, the other programmers are out to get
1355 you, so don't do this.
1358 <sect2 id="techs-deadlock-overprevent">
1359 <title>Overzealous Prevention Of Deadlocks</title>
1362 Deadlocks are problematic, but not as bad as data
1363 corruption. Code which grabs a read lock, searches a list,
1364 fails to find what it wants, drops the read lock, grabs a
1365 write lock and inserts the object has a race condition.
1369 If you don't see why, please stay the fuck away from my code.
1374 <sect1 id="racing-timers">
1375 <title>Racing Timers: A Kernel Pastime</title>
1378 Timers can produce their own special problems with races.
1379 Consider a collection of objects (list, hash, etc) where each
1380 object has a timer which is due to destroy it.
1384 If you want to destroy the entire collection (say on module
1385 removal), you might do the following:
1389 /* THIS CODE BAD BAD BAD BAD: IF IT WAS ANY WORSE IT WOULD USE
1390 HUNGARIAN NOTATION */
1391 spin_lock_bh(&list_lock);
1394 struct foo *next = list->next;
1395 del_timer(&list->timer);
1400 spin_unlock_bh(&list_lock);
1404 Sooner or later, this will crash on SMP, because a timer can
1405 have just gone off before the <function>spin_lock_bh()</function>,
1406 and it will only get the lock after we
1407 <function>spin_unlock_bh()</function>, and then try to free
1408 the element (which has already been freed!).
1412 This can be avoided by checking the result of
1413 <function>del_timer()</function>: if it returns
1414 <returnvalue>1</returnvalue>, the timer has been deleted.
1415 If <returnvalue>0</returnvalue>, it means (in this
1416 case) that it is currently running, so we can do:
1421 spin_lock_bh(&list_lock);
1424 struct foo *next = list->next;
1425 if (!del_timer(&list->timer)) {
1426 /* Give timer a chance to delete this */
1427 spin_unlock_bh(&list_lock);
1434 spin_unlock_bh(&list_lock);
1438 Another common problem is deleting timers which restart
1439 themselves (by calling <function>add_timer()</function> at the end
1440 of their timer function). Because this is a fairly common case
1441 which is prone to races, you should use <function>del_timer_sync()</function>
1442 (<filename class="headerfile">include/linux/timer.h</filename>)
1443 to handle this case. It returns the number of times the timer
1444 had to be deleted before we finally stopped it from adding itself back
1451 <chapter id="Efficiency">
1452 <title>Locking Speed</title>
1455 There are three main things to worry about when considering speed of
1456 some code which does locking. First is concurrency: how many things
1457 are going to be waiting while someone else is holding a lock. Second
1458 is the time taken to actually acquire and release an uncontended lock.
1459 Third is using fewer, or smarter locks. I'm assuming that the lock is
1460 used fairly often: otherwise, you wouldn't be concerned about
1464 Concurrency depends on how long the lock is usually held: you should
1465 hold the lock for as long as needed, but no longer. In the cache
1466 example, we always create the object without the lock held, and then
1467 grab the lock only when we are ready to insert it in the list.
1470 Acquisition times depend on how much damage the lock operations do to
1471 the pipeline (pipeline stalls) and how likely it is that this CPU was
1472 the last one to grab the lock (ie. is the lock cache-hot for this
1473 CPU): on a machine with more CPUs, this likelihood drops fast.
1474 Consider a 700MHz Intel Pentium III: an instruction takes about 0.7ns,
1475 an atomic increment takes about 58ns, a lock which is cache-hot on
1476 this CPU takes 160ns, and a cacheline transfer from another CPU takes
1477 an additional 170 to 360ns. (These figures from Paul McKenney's
1478 <ulink url="http://www.linuxjournal.com/article.php?sid=6993"> Linux
1479 Journal RCU article</ulink>).
1482 These two aims conflict: holding a lock for a short time might be done
1483 by splitting locks into parts (such as in our final per-object-lock
1484 example), but this increases the number of lock acquisitions, and the
1485 results are often slower than having a single lock. This is another
1486 reason to advocate locking simplicity.
1489 The third concern is addressed below: there are some methods to reduce
1490 the amount of locking which needs to be done.
1493 <sect1 id="efficiency-rwlocks">
1494 <title>Read/Write Lock Variants</title>
1497 Both spinlocks and semaphores have read/write variants:
1498 <type>rwlock_t</type> and <structname>struct rw_semaphore</structname>.
1499 These divide users into two classes: the readers and the writers. If
1500 you are only reading the data, you can get a read lock, but to write to
1501 the data you need the write lock. Many people can hold a read lock,
1502 but a writer must be sole holder.
1506 If your code divides neatly along reader/writer lines (as our
1507 cache code does), and the lock is held by readers for
1508 significant lengths of time, using these locks can help. They
1509 are slightly slower than the normal locks though, so in practice
1510 <type>rwlock_t</type> is not usually worthwhile.
1514 <sect1 id="efficiency-read-copy-update">
1515 <title>Avoiding Locks: Read Copy Update</title>
1518 There is a special method of read/write locking called Read Copy
1519 Update. Using RCU, the readers can avoid taking a lock
1520 altogether: as we expect our cache to be read more often than
1521 updated (otherwise the cache is a waste of time), it is a
1522 candidate for this optimization.
1526 How do we get rid of read locks? Getting rid of read locks
1527 means that writers may be changing the list underneath the
1528 readers. That is actually quite simple: we can read a linked
1529 list while an element is being added if the writer adds the
1530 element very carefully. For example, adding
1531 <symbol>new</symbol> to a single linked list called
1532 <symbol>list</symbol>:
1536 new->next = list->next;
1538 list->next = new;
1542 The <function>wmb()</function> is a write memory barrier. It
1543 ensures that the first operation (setting the new element's
1544 <symbol>next</symbol> pointer) is complete and will be seen by
1545 all CPUs, before the second operation is (putting the new
1546 element into the list). This is important, since modern
1547 compilers and modern CPUs can both reorder instructions unless
1548 told otherwise: we want a reader to either not see the new
1549 element at all, or see the new element with the
1550 <symbol>next</symbol> pointer correctly pointing at the rest of
1554 Fortunately, there is a function to do this for standard
1555 <structname>struct list_head</structname> lists:
1556 <function>list_add_rcu()</function>
1557 (<filename>include/linux/list.h</filename>).
1560 Removing an element from the list is even simpler: we replace
1561 the pointer to the old element with a pointer to its successor,
1562 and readers will either see it, or skip over it.
1565 list->next = old->next;
1568 There is <function>list_del_rcu()</function>
1569 (<filename>include/linux/list.h</filename>) which does this (the
1570 normal version poisons the old object, which we don't want).
1573 The reader must also be careful: some CPUs can look through the
1574 <symbol>next</symbol> pointer to start reading the contents of
1575 the next element early, but don't realize that the pre-fetched
1576 contents is wrong when the <symbol>next</symbol> pointer changes
1577 underneath them. Once again, there is a
1578 <function>list_for_each_entry_rcu()</function>
1579 (<filename>include/linux/list.h</filename>) to help you. Of
1580 course, writers can just use
1581 <function>list_for_each_entry()</function>, since there cannot
1582 be two simultaneous writers.
1585 Our final dilemma is this: when can we actually destroy the
1586 removed element? Remember, a reader might be stepping through
1587 this element in the list right now: it we free this element and
1588 the <symbol>next</symbol> pointer changes, the reader will jump
1589 off into garbage and crash. We need to wait until we know that
1590 all the readers who were traversing the list when we deleted the
1591 element are finished. We use <function>call_rcu()</function> to
1592 register a callback which will actually destroy the object once
1593 the readers are finished.
1596 But how does Read Copy Update know when the readers are
1597 finished? The method is this: firstly, the readers always
1598 traverse the list inside
1599 <function>rcu_read_lock()</function>/<function>rcu_read_unlock()</function>
1600 pairs: these simply disable preemption so the reader won't go to
1601 sleep while reading the list.
1604 RCU then waits until every other CPU has slept at least once:
1605 since readers cannot sleep, we know that any readers which were
1606 traversing the list during the deletion are finished, and the
1607 callback is triggered. The real Read Copy Update code is a
1608 little more optimized than this, but this is the fundamental
1613 --- cache.c.perobjectlock 2003-12-11 17:15:03.000000000 +1100
1614 +++ cache.c.rcupdate 2003-12-11 17:55:14.000000000 +1100
1616 #include <linux/list.h>
1617 #include <linux/slab.h>
1618 #include <linux/string.h>
1619 +#include <linux/rcupdate.h>
1620 #include <asm/semaphore.h>
1621 #include <asm/errno.h>
1625 - /* These two protected by cache_lock. */
1626 + /* This is protected by RCU */
1627 struct list_head list;
1630 + struct rcu_head rcu;
1634 /* Doesn't change once created. */
1639 - list_for_each_entry(i, &cache, list) {
1640 + list_for_each_entry_rcu(i, &cache, list) {
1641 if (i->id == id) {
1648 +/* Final discard done once we know no readers are looking. */
1649 +static void cache_delete_rcu(void *arg)
1654 /* Must be holding cache_lock */
1655 static void __cache_delete(struct object *obj)
1658 - list_del(&obj->list);
1660 + list_del_rcu(&obj->list);
1662 + call_rcu(&obj->rcu, cache_delete_rcu, obj);
1665 /* Must be holding cache_lock */
1666 static void __cache_add(struct object *obj)
1668 - list_add(&obj->list, &cache);
1669 + list_add_rcu(&obj->list, &cache);
1670 if (++cache_num > MAX_CACHE_SIZE) {
1671 struct object *i, *outcast = NULL;
1672 list_for_each_entry(i, &cache, list) {
1674 obj->popularity = 0;
1675 atomic_set(&obj->refcnt, 1); /* The cache holds a reference */
1676 spin_lock_init(&obj->lock);
1677 + INIT_RCU_HEAD(&obj->rcu);
1679 spin_lock_irqsave(&cache_lock, flags);
1681 @@ -104,12 +114,11 @@
1682 struct object *cache_find(int id)
1685 - unsigned long flags;
1687 - spin_lock_irqsave(&cache_lock, flags);
1689 obj = __cache_find(id);
1692 - spin_unlock_irqrestore(&cache_lock, flags);
1693 + rcu_read_unlock();
1699 Note that the reader will alter the
1700 <structfield>popularity</structfield> member in
1701 <function>__cache_find()</function>, and now it doesn't hold a lock.
1702 One solution would be to make it an <type>atomic_t</type>, but for
1703 this usage, we don't really care about races: an approximate result is
1704 good enough, so I didn't change it.
1708 The result is that <function>cache_find()</function> requires no
1709 synchronization with any other functions, so is almost as fast on SMP
1710 as it would be on UP.
1714 There is a furthur optimization possible here: remember our original
1715 cache code, where there were no reference counts and the caller simply
1716 held the lock whenever using the object? This is still possible: if
1717 you hold the lock, noone can delete the object, so you don't need to
1718 get and put the reference count.
1722 Now, because the 'read lock' in RCU is simply disabling preemption, a
1723 caller which always has preemption disabled between calling
1724 <function>cache_find()</function> and
1725 <function>object_put()</function> does not need to actually get and
1726 put the reference count: we could expose
1727 <function>__cache_find()</function> by making it non-static, and
1728 such callers could simply call that.
1731 The benefit here is that the reference count is not written to: the
1732 object is not altered in any way, which is much faster on SMP
1733 machines due to caching.
1737 <sect1 id="per-cpu">
1738 <title>Per-CPU Data</title>
1741 Another technique for avoiding locking which is used fairly
1742 widely is to duplicate information for each CPU. For example,
1743 if you wanted to keep a count of a common condition, you could
1744 use a spin lock and a single counter. Nice and simple.
1748 If that was too slow (it's usually not, but if you've got a
1749 really big machine to test on and can show that it is), you
1750 could instead use a counter for each CPU, then none of them need
1751 an exclusive lock. See <function>DEFINE_PER_CPU()</function>,
1752 <function>get_cpu_var()</function> and
1753 <function>put_cpu_var()</function>
1754 (<filename class="headerfile">include/linux/percpu.h</filename>).
1758 Of particular use for simple per-cpu counters is the
1759 <type>local_t</type> type, and the
1760 <function>cpu_local_inc()</function> and related functions,
1761 which are more efficient than simple code on some architectures
1762 (<filename class="headerfile">include/asm/local.h</filename>).
1766 Note that there is no simple, reliable way of getting an exact
1767 value of such a counter, without introducing more locks. This
1768 is not a problem for some uses.
1772 <sect1 id="mostly-hardirq">
1773 <title>Data Which Mostly Used By An IRQ Handler</title>
1776 If data is always accessed from within the same IRQ handler, you
1777 don't need a lock at all: the kernel already guarantees that the
1778 irq handler will not run simultaneously on multiple CPUs.
1781 Manfred Spraul points out that you can still do this, even if
1782 the data is very occasionally accessed in user context or
1783 softirqs/tasklets. The irq handler doesn't use a lock, and
1784 all other accesses are done as so:
1788 spin_lock(&lock);
1792 spin_unlock(&lock);
1795 The <function>disable_irq()</function> prevents the irq handler
1796 from running (and waits for it to finish if it's currently
1797 running on other CPUs). The spinlock prevents any other
1798 accesses happening at the same time. Naturally, this is slower
1799 than just a <function>spin_lock_irq()</function> call, so it
1800 only makes sense if this type of access happens extremely
1806 <chapter id="sleeping-things">
1807 <title>What Functions Are Safe To Call From Interrupts?</title>
1810 Many functions in the kernel sleep (ie. call schedule())
1811 directly or indirectly: you can never call them while holding a
1812 spinlock, or with preemption disabled. This also means you need
1813 to be in user context: calling them from an interrupt is illegal.
1816 <sect1 id="sleeping">
1817 <title>Some Functions Which Sleep</title>
1820 The most common ones are listed below, but you usually have to
1821 read the code to find out if other calls are safe. If everyone
1822 else who calls it can sleep, you probably need to be able to
1823 sleep, too. In particular, registration and deregistration
1824 functions usually expect to be called from user context, and can
1832 <firstterm linkend="gloss-userspace">userspace</firstterm>:
1837 <function>copy_from_user()</function>
1842 <function>copy_to_user()</function>
1847 <function>get_user()</function>
1852 <function> put_user()</function>
1860 <function>kmalloc(GFP_KERNEL)</function>
1866 <function>down_interruptible()</function> and
1867 <function>down()</function>
1870 There is a <function>down_trylock()</function> which can be
1871 used inside interrupt context, as it will not sleep.
1872 <function>up()</function> will also never sleep.
1878 <sect1 id="dont-sleep">
1879 <title>Some Functions Which Don't Sleep</title>
1882 Some functions are safe to call from any context, or holding
1889 <function>printk()</function>
1894 <function>kfree()</function>
1899 <function>add_timer()</function> and <function>del_timer()</function>
1906 <chapter id="references">
1907 <title>Further reading</title>
1912 <filename>Documentation/spinlocks.txt</filename>:
1913 Linus Torvalds' spinlocking tutorial in the kernel sources.
1919 Unix Systems for Modern Architectures: Symmetric
1920 Multiprocessing and Caching for Kernel Programmers:
1924 Curt Schimmel's very good introduction to kernel level
1925 locking (not written for Linux, but nearly everything
1926 applies). The book is expensive, but really worth every
1927 penny to understand SMP locking. [ISBN: 0201633388]
1933 <chapter id="thanks">
1934 <title>Thanks</title>
1937 Thanks to Telsa Gwynne for DocBooking, neatening and adding
1942 Thanks to Martin Pool, Philipp Rumpf, Stephen Rothwell, Paul
1943 Mackerras, Ruedi Aschwanden, Alan Cox, Manfred Spraul, Tim
1944 Waugh, Pete Zaitcev, James Morris, Robert Love, Paul McKenney,
1945 John Ashby for proofreading, correcting, flaming, commenting.
1949 Thanks to the cabal for having no influence on this document.
1953 <glossary id="glossary">
1954 <title>Glossary</title>
1956 <glossentry id="gloss-preemption">
1957 <glossterm>preemption</glossterm>
1960 Prior to 2.5, or when <symbol>CONFIG_PREEMPT</symbol> is
1961 unset, processes in user context inside the kernel would not
1962 preempt each other (ie. you had that CPU until you have it up,
1963 except for interrupts). With the addition of
1964 <symbol>CONFIG_PREEMPT</symbol> in 2.5.4, this changed: when
1965 in user context, higher priority tasks can "cut in": spinlocks
1966 were changed to disable preemption, even on UP.
1971 <glossentry id="gloss-bh">
1972 <glossterm>bh</glossterm>
1975 Bottom Half: for historical reasons, functions with
1976 '_bh' in them often now refer to any software interrupt, e.g.
1977 <function>spin_lock_bh()</function> blocks any software interrupt
1978 on the current CPU. Bottom halves are deprecated, and will
1979 eventually be replaced by tasklets. Only one bottom half will be
1980 running at any time.
1985 <glossentry id="gloss-hwinterrupt">
1986 <glossterm>Hardware Interrupt / Hardware IRQ</glossterm>
1989 Hardware interrupt request. <function>in_irq()</function> returns
1990 <returnvalue>true</returnvalue> in a hardware interrupt handler.
1995 <glossentry id="gloss-interruptcontext">
1996 <glossterm>Interrupt Context</glossterm>
1999 Not user context: processing a hardware irq or software irq.
2000 Indicated by the <function>in_interrupt()</function> macro
2001 returning <returnvalue>true</returnvalue>.
2006 <glossentry id="gloss-smp">
2007 <glossterm><acronym>SMP</acronym></glossterm>
2010 Symmetric Multi-Processor: kernels compiled for multiple-CPU
2011 machines. (CONFIG_SMP=y).
2016 <glossentry id="gloss-softirq">
2017 <glossterm>Software Interrupt / softirq</glossterm>
2020 Software interrupt handler. <function>in_irq()</function> returns
2021 <returnvalue>false</returnvalue>; <function>in_softirq()</function>
2022 returns <returnvalue>true</returnvalue>. Tasklets and softirqs
2023 both fall into the category of 'software interrupts'.
2026 Strictly speaking a softirq is one of up to 32 enumerated software
2027 interrupts which can run on multiple CPUs at once.
2028 Sometimes used to refer to tasklets as
2029 well (ie. all software interrupts).
2034 <glossentry id="gloss-tasklet">
2035 <glossterm>tasklet</glossterm>
2038 A dynamically-registrable software interrupt,
2039 which is guaranteed to only run on one CPU at a time.
2044 <glossentry id="gloss-timers">
2045 <glossterm>timer</glossterm>
2048 A dynamically-registrable software interrupt, which is run at
2049 (or close to) a given time. When running, it is just like a
2050 tasklet (in fact, they are called from the TIMER_SOFTIRQ).
2055 <glossentry id="gloss-up">
2056 <glossterm><acronym>UP</acronym></glossterm>
2059 Uni-Processor: Non-SMP. (CONFIG_SMP=n).
2064 <glossentry id="gloss-usercontext">
2065 <glossterm>User Context</glossterm>
2068 The kernel executing on behalf of a particular process (ie. a
2069 system call or trap) or kernel thread. You can tell which
2070 process with the <symbol>current</symbol> macro.) Not to
2071 be confused with userspace. Can be interrupted by software or
2072 hardware interrupts.
2077 <glossentry id="gloss-userspace">
2078 <glossterm>Userspace</glossterm>
2081 A process executing its own code outside the kernel.