Skip to content

Commit 3e472c8

Browse files
authored
Describe atomic context in spinlock section (#254)
Aquiring a spinlock makes the holder enter atomic context. Extra attention is needed in atomic context. In particular, functions that may sleep must not be used. Add this detail to the spinlock section.
1 parent e1b4457 commit 3e472c8

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lkmpg.tex

+15
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,21 @@ \subsection{Spinlocks}
16891689

16901690
\samplec{examples/example_spinlock.c}
16911691

1692+
Taking 100\% of a CPU's resources comes with greater responsibility.
1693+
Situations where the kernel code monopolizes a CPU are called \textbf{atomic contexts}.
1694+
Holding a spinlock is one of those situations.
1695+
Sleeping in atomic contexts may leave the system hanging, as the occupied CPU devotes 100\% of its resources doing nothing but sleeping.
1696+
In some worse cases the system may crash.
1697+
Thus, sleeping in atomic contexts is considered a bug in the kernel.
1698+
They are sometimes called ``sleep-in-atomic-context'' in some materials.
1699+
1700+
Note that sleeping here is not limited to calling the sleep functions explicitly.
1701+
If subsequent function calls eventually invoke a function that sleeps, it is also considered sleeping.
1702+
Thus, it is important to pay attention to functions being used in atomic context.
1703+
There's no documentation recording all such functions, but code comments may help.
1704+
Sometimes you may find comments in kernel source code stating that a function ``may sleep'', ``might sleep'', or more explicitly ``the caller should not hold a spinlock''.
1705+
Those comments are hints that a function may implicitly sleep and must not be called in atomic contexts.
1706+
16921707
\subsection{Read and write locks}
16931708
\label{sec:rwlock}
16941709
Read and write locks are specialised kinds of spinlocks so that you can exclusively read from something or write to something.

0 commit comments

Comments
 (0)