为何使用wake_up_all()而非wake_up_interruptible()?二者是否等同?
wake_up_all in TTM Read Unlock and Its Difference from wake_up_interruptible Great question! Let's break this down step by step, looking at the TTM lock code you provided and Linux kernel waiting queue behavior.
Why use wake_up_all(&lock->queue) in ttm_read_unlock?
First, let's recap how this TTM read-write lock works:
- The
lock->rwfield tracks lock state: positive values mean multiple readers hold the lock; negative values mean a single writer holds it;0means no holders. - When the last reader calls
ttm_read_unlock,--lock->rwhits0, signaling the lock is now free.
The reason for wake_up_all instead of a single wake-up call is tied to the waiting queue's contents and lock fairness:
- Multiple waiting processes: The queue may have multiple processes waiting—both readers and writers. Using
wake_up_allensures all of them get a chance to attempt acquiring the lock, rather than just one. This helps avoid starvation (e.g., a writer waiting indefinitely while only readers are woken up one at a time). - Mixed wait states: Looking at
ttm_read_lock, processes can wait in two modes:wait_event_interruptible: Puts the process inTASK_INTERRUPTIBLE(can be woken by signals)wait_event: Puts the process inTASK_UNINTERRUPTIBLE(cannot be woken by signals)wake_up_allwakes all processes in both states, which is necessary here since both types of waiters exist in the queue.
If we used a single wake-up (like plain wake_up), we might wake a reader that can't acquire the lock (e.g., if a writer is waiting and the lock's flags block new readers), leaving other waiters (including writers) asleep unnecessarily. wake_up_all eliminates this risk by letting all waiters compete for the lock.
Is wake_up_all the same as wake_up_interruptible?
No, they are distinct macros with key differences:
wake_up_all(&queue):- Wakes all processes waiting on the queue, regardless of their state (
TASK_UNINTERRUPTIBLEorTASK_INTERRUPTIBLE). - Used when you need to ensure every waiting process gets a chance to check the lock condition.
- Wakes all processes waiting on the queue, regardless of their state (
wake_up_interruptible(&queue):- Only wakes one process in
TASK_INTERRUPTIBLEstate (can be interrupted by signals). - If you wanted to wake all interruptible waiters, you'd use
wake_up_interruptible_all(&queue)—but this still doesn't touch uninterruptible waiters.
- Only wakes one process in
In the TTM code, since both interruptible and uninterruptible waiters exist, wake_up_all is the correct choice to cover all cases.
内容的提问来源于stack exchange,提问作者Rajan

