summaryrefslogtreecommitdiff
path: root/schulung_tools/notes/kernel-locking.txt
blob: 1757227ce9db137fa3da467dd0eb7720f0eb90d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  ++ locking
   Documentation/kernel-hacking/locking.rst
   - atomic operations
       Documentation/atomic_t.txt
       atomic_t atomic64_t and atomic_long_t
         atomic_read(), atomic_set()
         atomic[_fetch]_{add,sub,inc,dec}()
         atomic[_fetch]_{and,or,xor,andnot}()
         ..
   - mutex
       waiter can sleep, not usable in tasklets/timer/irqs
   - spinlock / rwlock
       spin_lock # use if lock is shared in different tasklets / only in user context
       spin_lock_bh / disables softirqs  # use if lock is shared in user context and softirqs (tasklets/timer)
       spin_lock_irqsave / disables irqs # use if lock is shared in user context and irq context
      --> Use irqsave variant if you're unsure
      --> Avoid holding spinlock for more than 5 lines of code and across any
             function call (except accessors like :c:func:`readb()`).

============== ============= ============= ========= ========= ========= ========= ======= ======= ============== ==============
.              IRQ Handler A IRQ Handler B Softirq A Softirq B Tasklet A Tasklet B Timer A Timer B User Context A User Context B
============== ============= ============= ========= ========= ========= ========= ======= ======= ============== ==============
IRQ Handler A  None
IRQ Handler B  SLIS          None
Softirq A      SLI           SLI           SL
Softirq B      SLI           SLI           SL        SL
Tasklet A      SLI           SLI           SL        SL        None
Tasklet B      SLI           SLI           SL        SL        SL        None
Timer A        SLI           SLI           SL        SL        SL        SL        None
Timer B        SLI           SLI           SL        SL        SL        SL        SL      None
User Context A SLI           SLI           SLBH      SLBH      SLBH      SLBH      SLBH    SLBH    None
User Context B SLI           SLI           SLBH      SLBH      SLBH      SLBH      SLBH    SLBH    MLI            None
============== ============= ============= ========= ========= ========= ========= ======= ======= ============== ==============

Table: Table of Locking Requirements

+--------+----------------------------+
| SLIS   | spin_lock_irqsave          |
+--------+----------------------------+
| SLI    | spin_lock_irq              |
+--------+----------------------------+
| SL     | spin_lock                  |
+--------+----------------------------+
| SLBH   | spin_lock_bh               |
+--------+----------------------------+
| MLI    | mutex_lock_interruptible   |
+--------+----------------------------+

Table: Legend for Locking Requirements Table