You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: lkmpg.tex
+15
Original file line number
Diff line number
Diff line change
@@ -1689,6 +1689,21 @@ \subsection{Spinlocks}
1689
1689
1690
1690
\samplec{examples/example_spinlock.c}
1691
1691
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
+
1692
1707
\subsection{Read and write locks}
1693
1708
\label{sec:rwlock}
1694
1709
Read and write locks are specialised kinds of spinlocks so that you can exclusively read from something or write to something.
0 commit comments