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
Copy file name to clipboardExpand all lines: lkmpg.tex
+33
Original file line number
Diff line number
Diff line change
@@ -1682,8 +1682,41 @@ \subsection{Mutex}
1682
1682
You can use kernel mutexes (mutual exclusions) in much the same manner that you might deploy them in userland.
1683
1683
This may be all that is needed to avoid collisions in most cases.
1684
1684
1685
+
Mutexes in the Linux kernel enforce strict ownership: only the task that successfully acquired the mutex can release (or unlock) it.
1686
+
Attempting to release a mutex held by another task or releasing an unheld mutex multiple times by the same task typically leads to errors or undefined behavior.
1687
+
If a task tries to lock a mutex it already holds, it may be blocked or sleep, where the task waits for itself to release the lock.
1688
+
1689
+
Before use, a mutex must be initialized through specific APIs (such as \cpp|mutex_init| or by using the \cpp|DEFINE_MUTEX| macro for compile-time initialization).
1690
+
And it is prohibited to directly modify the internal structure of a mutex using a memory manipulation function like \cpp|memset|.
1691
+
1685
1692
\samplec{examples/example_mutex.c}
1686
1693
1694
+
The various suffixes appended to mutex functions in the Linux kernel primarily dictate how a task waiting to acquire a lock will behave,
1695
+
particularly concerning its interruptibility.
1696
+
1697
+
When a task calls \cpp|mutex_lock()|, and if the mutex is currently unavailable,
1698
+
the task enters a sleep state until it can successfully obtain the lock.
1699
+
During this period, the task cannot be interrupted.
1700
+
In contrast, functions with the \cpp|_interruptible| suffix, such as \cpp|mutex_lock_interruptible()|,
1701
+
behave similarly to \cpp|mutex_lock()| but allow the waiting process to be interrupted by signals.
1702
+
If a task receives a signal (like a termination signal) while waiting for the lock,
1703
+
it will exit the waiting state and return an error code (\cpp|-EINTR|).
1704
+
This is useful for applications that need to handle external events even while waiting for a lock.
1705
+
1706
+
Beyond these fundamental locking behaviors, other mutex functions offer specialized capabilities.
1707
+
Functions like \cpp|mutex_lock_nested| and \cpp|mutex_lock_interruptible_nested()| incorporate the \cpp|__nested()| functionality,
1708
+
providing support for nested locking.
1709
+
This prior locking mechanism aids in managing lock acquisition and preventing deadlocks,
1710
+
often employing a subclass parameter for more precise deadlock detection.
1711
+
The latter variant combines nested locking with the ability for the waiting process to be interrupted by signals.
1712
+
Another function is \cpp|mutex_trylock()|, which attempts to acquire the mutex without blocking.
1713
+
It returns 1 if the lock is successfully acquired and 0 if the mutex is already held by another task.
1714
+
1715
+
Despite the fact that \cpp|mutex_trylock| does not sleep,
1716
+
it is still generally not safe for use in interrupt context because its implementation isn't atomic.
1717
+
If an interrupt occurs between checking the lock's availability and its acquisition,
1718
+
this can lead to race conditions and potential data corruption.
1719
+
1687
1720
\subsection{Spinlocks}
1688
1721
\label{sec:spinlock}
1689
1722
As the name suggests, spinlocks lock up the CPU that the code is running on, taking 100\% of its resources.
0 commit comments