Table of Contents
Pete Zaitcev gives the following summary:
If you are in a process context (any syscall) and want to
lock other process out, use a mutex. You can take a mutex
and sleep (copy_from_user*(
or
kmalloc(x,GFP_KERNEL)
).
Otherwise (== data can be touched in an interrupt), use
spin_lock_irqsave()
and
spin_unlock_irqrestore()
.
Avoid holding spinlock for more than 5 lines of code and
across any function call (except accessors like
readb
).
The following table lists the minimum locking requirements between various contexts. In some cases, the same context can only be running on one CPU at a time, so no locking is required for that context (eg. a particular thread can only run on one CPU at a time, but if it needs shares data with another thread, locking is required).
Remember the advice above: you can always use
spin_lock_irqsave()
, which is a superset
of all other spinlock primitives.
Table 5.1. Table of Locking Requirements
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 5.2. Legend for Locking Requirements Table
SLIS | spin_lock_irqsave |
SLI | spin_lock_irq |
SL | spin_lock |
SLBH | spin_lock_bh |
MLI | mutex_lock_interruptible |