Make wake_up_{bit,var} less fragile
v3 cover Make wake_up_{bit,var} less fragile
Hi,
this is a revised set of patches for cleaning up wake_up_bit and wake_up_var and related. They are the result of more proof reading, and of trying to make use of the new interfaces anywhere in the kernel where these interfaces are used.
This should apply after -rc1, or at least after the block tree is merged.
Note that on current master these cause a build error in bcachefs as it hasn't been updated with the new i_state bit management.
Apart from proof-reading fixes this series enhances the previous series:
- adds wait_var_event_any_lock() which can be used with any lock for which there are foo_lock and foo_unlock functions. This is useful to deal with places that want to call filemap_invalidate_unlock/lock while waiting for a var
- removes might_sleep() for wait_var_event_spinlock() as the code won't sleep until after it take the lock
- adds wait_var_event_io() for io_schedule waits - xfs can use this.
- as a bonus, softirq gets some improvements to waiting. Feel free to drop or delay that one if not completely comfortable.
Thanks,
NeilBrown
PATCH v3 1/7 sched: change wake_up_bit() and related function to expect unsigned long *
wake_up_bit() currently allows a "void *". While this isn't strictly a problem as the address is never dereferenced, it is inconsistent with the corresponding wait_on_bit() which requires "unsigned long *" and does dereference the pointer.
Any code that needs to wait for a change in something other than an unsigned long would be better served by wake_up_var()/wait_var_event().
This patch changes all related "void *" to "unsigned long *".
--- include/linux/wait_bit.h | 16 ++++++++-------- kernel/sched/wait_bit.c | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-)
include/linux/wait_bit.h +8 -8
@@ -8,7 +8,7 @@#include <linux/wait.h> struct wait_bit_key { -void *flags; +unsigned long *flags;int bit_nr;unsigned long timeout; };
@@ -23,14 +23,14 @@ struct wait_bit_queue_entry {typedef int wait_bit_action_f (struct wait_bit_key *key,int mode); -void __wake_up_bit (struct wait_queue_head *wq_head,void *word,int bit); +void __wake_up_bit (struct wait_queue_head *wq_head,unsigned long *word,int bit);int __wait_on_bit (struct wait_queue_head *wq_head,struct wait_bit_queue_entry *wbq_entry,wait_bit_action_f *action,unsigned int mode);int __wait_on_bit_lock (struct wait_queue_head *wq_head,struct wait_bit_queue_entry *wbq_entry,wait_bit_action_f *action,unsigned int mode); -void wake_up_bit (void *word,int bit); -int out_of_line_wait_on_bit (void *word,int ,wait_bit_action_f *action,unsigned int mode); -int out_of_line_wait_on_bit_timeout (void *word,int ,wait_bit_action_f *action,unsigned int mode,unsigned long timeout); -int out_of_line_wait_on_bit_lock (void *word,int ,wait_bit_action_f *action,unsigned int mode); -struct wait_queue_head *bit_waitqueue (void *word,int bit); +void wake_up_bit (unsigned long *word,int bit); +int out_of_line_wait_on_bit (unsigned long *word,int ,wait_bit_action_f *action,unsigned int mode); +int out_of_line_wait_on_bit_timeout (unsigned long *word,int ,wait_bit_action_f *action,unsigned int mode,unsigned long timeout); +int out_of_line_wait_on_bit_lock (unsigned long *word,int ,wait_bit_action_f *action,unsigned int mode); +struct wait_queue_head *bit_waitqueue (unsigned long *word,int bit);extern void __init wait_bit_init (void );int wake_bit_function (struct wait_queue_entry *wq_entry,unsigned mode,int sync,void *key);
@@ -327,7 +327,7 @@ do { \* You can use this helper if bitflags are manipulated atomically rather than * non-atomically under a lock. */ -static inline void clear_and_wake_up_bit (int bit,void *word) +static inline void clear_and_wake_up_bit (int bit,unsigned long *word) {clear_bit_unlock (bit, word);/* See wake_up_bit() for which memory barrier you need to use. */
kernel/sched/wait_bit.c +6 -6
@@ -9,7 +9,7 @@ static wait_queue_head_t bit_wait_table[WAIT_TABLE_SIZE] __cacheline_aligned; -wait_queue_head_t *bit_waitqueue (void *word,int bit) +wait_queue_head_t *bit_waitqueue (unsigned long *word,int bit) {const int shift =BITS_PER_LONG ==32 ?5 :6 ;unsigned long val = (unsigned long )word << shift | bit;
@@ -55,7 +55,7 @@ __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_ }EXPORT_SYMBOL (__wait_on_bit); -int __sched out_of_line_wait_on_bit(void *word, int bit, +int __sched out_of_line_wait_on_bit(unsigned long *word, int bit,wait_bit_action_f *action,unsigned mode) {struct wait_queue_head *wq_head =bit_waitqueue (word, bit);
@@ -66,7 +66,7 @@ int __sched out_of_line_wait_on_bit(void *word, int bit,EXPORT_SYMBOL (out_of_line_wait_on_bit); int __sched out_of_line_wait_on_bit_timeout( -void *word,int bit,wait_bit_action_f *action, +unsigned long *word,int bit,wait_bit_action_f *action,unsigned mode,unsigned long timeout) {struct wait_queue_head *wq_head =bit_waitqueue (word, bit);
@@ -108,7 +108,7 @@ __wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry }EXPORT_SYMBOL (__wait_on_bit_lock); -int __sched out_of_line_wait_on_bit_lock(void *word, int bit, +int __sched out_of_line_wait_on_bit_lock(unsigned long *word, int bit,wait_bit_action_f *action,unsigned mode) {struct wait_queue_head *wq_head =bit_waitqueue (word, bit);
@@ -118,7 +118,7 @@ int __sched out_of_line_wait_on_bit_lock(void *word, int bit, }EXPORT_SYMBOL (out_of_line_wait_on_bit_lock); -void __wake_up_bit (struct wait_queue_head *wq_head,void *word,int bit) +void __wake_up_bit (struct wait_queue_head *wq_head,unsigned long *word,int bit) {struct wait_bit_key key =__WAIT_BIT_KEY_INITIALIZER (word, bit);
@@ -144,7 +144,7 @@ EXPORT_SYMBOL(__wake_up_bit);* may need to use a less regular barrier, such fs/inode.c's smp_mb(), * because spin_unlock() does not guarantee a memory barrier. */ -void wake_up_bit (void *word,int bit) +void wake_up_bit (unsigned long *word,int bit) {__wake_up_bit (bit_waitqueue (word, bit), word, bit); }
Signature · 1 line
2.46.0
[tip: sched/core] sched: change wake_up_bit() and related function to expect unsigned long *
The following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: 2382d68d7d43873ba856baf567cab0d5c523f23b Gitweb: https://git.kernel.org/tip/2382d68d7d43873ba856baf567cab0d5c523f23b Author: NeilBrown <[email protected]> AuthorDate: Wed, 25 Sep 2024 15:31:38 +10:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 07 Oct 2024 09:28:37 +02:00
sched: change wake_up_bit() and related function to expect unsigned long *
wake_up_bit() currently allows a "void *". While this isn't strictly a problem as the address is never dereferenced, it is inconsistent with the corresponding wait_on_bit() which requires "unsigned long *" and does dereference the pointer.
Any code that needs to wait for a change in something other than an unsigned long would be better served by wake_up_var()/wait_var_event().
This patch changes all related "void *" to "unsigned long *".
--- include/linux/wait_bit.h | 16 ++++++++-------- kernel/sched/wait_bit.c | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-)
include/linux/wait_bit.h +8 -8
@@ -8,7 +8,7 @@#include <linux/wait.h> struct wait_bit_key { -void *flags; +unsigned long *flags;int bit_nr;unsigned long timeout; };
@@ -23,14 +23,14 @@ struct wait_bit_queue_entry {typedef int wait_bit_action_f (struct wait_bit_key *key,int mode); -void __wake_up_bit (struct wait_queue_head *wq_head,void *word,int bit); +void __wake_up_bit (struct wait_queue_head *wq_head,unsigned long *word,int bit);int __wait_on_bit (struct wait_queue_head *wq_head,struct wait_bit_queue_entry *wbq_entry,wait_bit_action_f *action,unsigned int mode);int __wait_on_bit_lock (struct wait_queue_head *wq_head,struct wait_bit_queue_entry *wbq_entry,wait_bit_action_f *action,unsigned int mode); -void wake_up_bit (void *word,int bit); -int out_of_line_wait_on_bit (void *word,int ,wait_bit_action_f *action,unsigned int mode); -int out_of_line_wait_on_bit_timeout (void *word,int ,wait_bit_action_f *action,unsigned int mode,unsigned long timeout); -int out_of_line_wait_on_bit_lock (void *word,int ,wait_bit_action_f *action,unsigned int mode); -struct wait_queue_head *bit_waitqueue (void *word,int bit); +void wake_up_bit (unsigned long *word,int bit); +int out_of_line_wait_on_bit (unsigned long *word,int ,wait_bit_action_f *action,unsigned int mode); +int out_of_line_wait_on_bit_timeout (unsigned long *word,int ,wait_bit_action_f *action,unsigned int mode,unsigned long timeout); +int out_of_line_wait_on_bit_lock (unsigned long *word,int ,wait_bit_action_f *action,unsigned int mode); +struct wait_queue_head *bit_waitqueue (unsigned long *word,int bit);extern void __init wait_bit_init (void );int wake_bit_function (struct wait_queue_entry *wq_entry,unsigned mode,int sync,void *key);
@@ -327,7 +327,7 @@ do { \* You can use this helper if bitflags are manipulated atomically rather than * non-atomically under a lock. */ -static inline void clear_and_wake_up_bit (int bit,void *word) +static inline void clear_and_wake_up_bit (int bit,unsigned long *word) {clear_bit_unlock (bit, word);/* See wake_up_bit() for which memory barrier you need to use. */
kernel/sched/wait_bit.c +6 -6
@@ -9,7 +9,7 @@ static wait_queue_head_t bit_wait_table[WAIT_TABLE_SIZE] __cacheline_aligned; -wait_queue_head_t *bit_waitqueue (void *word,int bit) +wait_queue_head_t *bit_waitqueue (unsigned long *word,int bit) {const int shift =BITS_PER_LONG ==32 ?5 :6 ;unsigned long val = (unsigned long )word << shift | bit;
@@ -55,7 +55,7 @@ __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_ }EXPORT_SYMBOL (__wait_on_bit); -int __sched out_of_line_wait_on_bit(void *word, int bit, +int __sched out_of_line_wait_on_bit(unsigned long *word, int bit,wait_bit_action_f *action,unsigned mode) {struct wait_queue_head *wq_head =bit_waitqueue (word, bit);
@@ -66,7 +66,7 @@ int __sched out_of_line_wait_on_bit(void *word, int bit,EXPORT_SYMBOL (out_of_line_wait_on_bit); int __sched out_of_line_wait_on_bit_timeout( -void *word,int bit,wait_bit_action_f *action, +unsigned long *word,int bit,wait_bit_action_f *action,unsigned mode,unsigned long timeout) {struct wait_queue_head *wq_head =bit_waitqueue (word, bit);
@@ -108,7 +108,7 @@ __wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry }EXPORT_SYMBOL (__wait_on_bit_lock); -int __sched out_of_line_wait_on_bit_lock(void *word, int bit, +int __sched out_of_line_wait_on_bit_lock(unsigned long *word, int bit,wait_bit_action_f *action,unsigned mode) {struct wait_queue_head *wq_head =bit_waitqueue (word, bit);
@@ -118,7 +118,7 @@ int __sched out_of_line_wait_on_bit_lock(void *word, int bit, }EXPORT_SYMBOL (out_of_line_wait_on_bit_lock); -void __wake_up_bit (struct wait_queue_head *wq_head,void *word,int bit) +void __wake_up_bit (struct wait_queue_head *wq_head,unsigned long *word,int bit) {struct wait_bit_key key =__WAIT_BIT_KEY_INITIALIZER (word, bit);
@@ -144,7 +144,7 @@ EXPORT_SYMBOL(__wake_up_bit);* may need to use a less regular barrier, such fs/inode.c's smp_mb(), * because spin_unlock() does not guarantee a memory barrier. */ -void wake_up_bit (void *word,int bit) +void wake_up_bit (unsigned long *word,int bit) {__wake_up_bit (bit_waitqueue (word, bit), word, bit); }
PATCH v3 2/7 sched: Improve documentation for wake_up_bit/wait_on_bit family of functions
In reply to NeilBrownThis patch revises the documention for wake_up_bit(), clear_and_wake_up_bit(), and all the wait_on_bit() family of functions.
The new documentation places less emphasis on the pool of waitqueues used (an implementation detail) and focuses instead on details of how the functions behave.
The barriers included in the wait functions and clear_and_wake_up_bit() and those required for wake_up_bit() are spelled out more clearly.
The error statuses returned are given explicitly.
The fact that the wait_on_bit_lock() function sets the bit is made more obvious.
--- include/linux/wait_bit.h | 159 +++++++++++++++++++++------------------ kernel/sched/wait_bit.c | 34 +++++---- 2 files changed, 107 insertions(+), 86 deletions(-)
include/linux/wait_bit.h +86 -73
@@ -53,19 +53,21 @@ extern int bit_wait_io_timeout(struct wait_bit_key *key, int mode);/** * wait_on_bit - wait for a bit to be cleared -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* @word: the address containing the bit being waited on +* @bit: the bit at that address being waited on * @mode: the task state to sleep in * -* There is a standard hashed waitqueue table for generic use. This -* is the part of the hashtable's accessor API that waits on a bit. -* For instance, if one were to have waiters on a bitflag, one would -* call wait_on_bit() in threads waiting for the bit to clear. -* One uses wait_on_bit() where one is waiting for the bit to clear, -* but has no intention of setting it. -* Returned value will be zero if the bit was cleared, or non-zero -* if the process received a signal and the mode permitted wakeup -* on that signal. +* Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP()) +* to be cleared. The clearing of the bit must be signalled with +* wake_up_bit(), often as clear_and_wake_up_bit(). +* +* The process will wait on a waitqueue selected by hash from a shared +* pool. It will only be woken on a wake_up for the target bit, even +* if other processes on the same queue are waiting for other bits. +* +* Returned value will be zero if the bit was cleared in which case the +* call has ACQUIRE semantics, or %-EINTR if the process received a +* signal and the mode permitted wake up on that signal. */ static inline int wait_on_bit (unsigned long *word,int bit,unsigned mode)
@@ -80,17 +82,20 @@ wait_on_bit(unsigned long *word, int bit, unsigned mode)/** * wait_on_bit_io - wait for a bit to be cleared -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* @word: the address containing the bit being waited on +* @bit: the bit at that address being waited on * @mode: the task state to sleep in * -* Use the standard hashed waitqueue table to wait for a bit -* to be cleared. This is similar to wait_on_bit(), but calls -* io_schedule() instead of schedule() for the actual waiting. +* Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP()) +* to be cleared. The clearing of the bit must be signalled with +* wake_up_bit(), often as clear_and_wake_up_bit(). +* +* This is similar to wait_on_bit(), but calls io_schedule() instead of +* schedule() for the actual waiting. * -* Returned value will be zero if the bit was cleared, or non-zero -* if the process received a signal and the mode permitted wakeup -* on that signal. +* Returned value will be zero if the bit was cleared in which case the +* call has ACQUIRE semantics, or %-EINTR if the process received a +* signal and the mode permitted wake up on that signal. */ static inline int wait_on_bit_io (unsigned long *word,int bit,unsigned mode)
@@ -104,19 +109,24 @@ wait_on_bit_io(unsigned long *word, int bit, unsigned mode) }/** -* wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* wait_on_bit_timeout - wait for a bit to be cleared or a timeout to elapse +* @word: the address containing the bit being waited on +* @bit: the bit at that address being waited on * @mode: the task state to sleep in * @timeout: timeout, in jiffies * -* Use the standard hashed waitqueue table to wait for a bit -* to be cleared. This is similar to wait_on_bit(), except also takes a -* timeout parameter. +* Wait for the given bit in an unsigned long or bitmap (see +* DECLARE_BITMAP()) to be cleared, or for a timeout to expire. The +* clearing of the bit must be signalled with wake_up_bit(), often as +* clear_and_wake_up_bit(). * -* Returned value will be zero if the bit was cleared before the -* @timeout elapsed, or non-zero if the @timeout elapsed or process -* received a signal and the mode permitted wakeup on that signal. +* This is similar to wait_on_bit(), except it also takes a timeout +* parameter. +* +* Returned value will be zero if the bit was cleared in which case the +* call has ACQUIRE semantics, or %-EINTR if the process received a +* signal and the mode permitted wake up on that signal, or %-EAGAIN if the +* timeout elapsed. */ static inline int wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
@@ -132,19 +142,21 @@ wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,/** * wait_on_bit_action - wait for a bit to be cleared -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* @word: the address containing the bit waited on +* @bit: the bit at that address being waited on * @action: the function used to sleep, which may take special actions * @mode: the task state to sleep in * -* Use the standard hashed waitqueue table to wait for a bit -* to be cleared, and allow the waiting action to be specified. -* This is like wait_on_bit() but allows fine control of how the waiting -* is done. +* Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP()) +* to be cleared. The clearing of the bit must be signalled with +* wake_up_bit(), often as clear_and_wake_up_bit(). +* +* This is similar to wait_on_bit(), but calls @action() instead of +* schedule() for the actual waiting. * -* Returned value will be zero if the bit was cleared, or non-zero -* if the process received a signal and the mode permitted wakeup -* on that signal. +* Returned value will be zero if the bit was cleared in which case the +* call has ACQUIRE semantics, or the error code returned by @action if +* that call returned non-zero. */ static inline int wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
@@ -157,23 +169,22 @@ wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action, }/** -* wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* wait_on_bit_lock - wait for a bit to be cleared, then set it +* @word: the address containing the bit being waited on +* @bit: the bit of the word being waited on and set * @mode: the task state to sleep in * -* There is a standard hashed waitqueue table for generic use. This -* is the part of the hashtable's accessor API that waits on a bit -* when one intends to set it, for instance, trying to lock bitflags. -* For instance, if one were to have waiters trying to set bitflag -* and waiting for it to clear before setting it, one would call -* wait_on_bit() in threads waiting to be able to set the bit. -* One uses wait_on_bit_lock() where one is waiting for the bit to -* clear with the intention of setting it, and when done, clearing it. +* Wait for the given bit in an unsigned long or bitmap (see +* DECLARE_BITMAP()) to be cleared. The clearing of the bit must be +* signalled with wake_up_bit(), often as clear_and_wake_up_bit(). As +* soon as it is clear, atomically set it and return. * -* Returns zero if the bit was (eventually) found to be clear and was -* set. Returns non-zero if a signal was delivered to the process and -* the @mode allows that signal to wake the process. +* This is similar to wait_on_bit(), but sets the bit before returning. +* +* Returned value will be zero if the bit was successfully set in which +* case the call has the same memory sequencing semantics as +* test_and_clear_bit(), or %-EINTR if the process received a signal and +* the mode permitted wake up on that signal. */ static inline int wait_on_bit_lock (unsigned long *word,int bit,unsigned mode)
@@ -185,15 +196,18 @@ wait_on_bit_lock(unsigned long *word, int bit, unsigned mode) }/** -* wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* wait_on_bit_lock_io - wait for a bit to be cleared, then set it +* @word: the address containing the bit being waited on +* @bit: the bit of the word being waited on and set * @mode: the task state to sleep in * -* Use the standard hashed waitqueue table to wait for a bit -* to be cleared and then to atomically set it. This is similar -* to wait_on_bit(), but calls io_schedule() instead of schedule() -* for the actual waiting. +* Wait for the given bit in an unsigned long or bitmap (see +* DECLARE_BITMAP()) to be cleared. The clearing of the bit must be +* signalled with wake_up_bit(), often as clear_and_wake_up_bit(). As +* soon as it is clear, atomically set it and return. +* +* This is similar to wait_on_bit_lock(), but calls io_schedule() instead +* of schedule(). * * Returns zero if the bit was (eventually) found to be clear and was * set. Returns non-zero if a signal was delivered to the process and
@@ -209,21 +223,19 @@ wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode) }/** -* wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* wait_on_bit_lock_action - wait for a bit to be cleared, then set it +* @word: the address containing the bit being waited on +* @bit: the bit of the word being waited on and set * @action: the function used to sleep, which may take special actions * @mode: the task state to sleep in * -* Use the standard hashed waitqueue table to wait for a bit -* to be cleared and then to set it, and allow the waiting action -* to be specified. -* This is like wait_on_bit() but allows fine control of how the waiting -* is done. +* This is similar to wait_on_bit_lock(), but calls @action() instead of +* schedule() for the actual waiting. * -* Returns zero if the bit was (eventually) found to be clear and was -* set. Returns non-zero if a signal was delivered to the process and -* the @mode allows that signal to wake the process. +* Returned value will be zero if the bit was successfully set in which +* case the call has the same memory sequencing semantics as +* test_and_clear_bit(), or the error code returned by @action if that +* call returned non-zero. */ static inline int wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
@@ -320,12 +332,13 @@ do { \/** * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit -* * @bit: the bit of the word being waited on -* @word: the word being waited on, a kernel virtual address +* @word: the address containing the bit being waited on * -* You can use this helper if bitflags are manipulated atomically rather than -* non-atomically under a lock. +* The designated bit is cleared and any tasks waiting in wait_on_bit() +* or similar will be woken. This call has RELEASE semantics so that +* any changes to memory made before this call are guaranteed to be visible +* after the corresponding wait_on_bit() completes. */ static inline void clear_and_wake_up_bit (int bit,unsigned long *word) {
kernel/sched/wait_bit.c +21 -13
@@ -128,21 +128,29 @@ void __wake_up_bit(struct wait_queue_head *wq_head, unsigned long *word, int bitEXPORT_SYMBOL (__wake_up_bit);/** -* wake_up_bit - wake up a waiter on a bit -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* wake_up_bit - wake up waiters on a bit +* @word: the address containing the bit being waited on +* @bit: the bit at that address being waited on * -* There is a standard hashed waitqueue table for generic use. This -* is the part of the hash-table's accessor API that wakes up waiters -* on a bit. For instance, if one were to have waiters on a bitflag, -* one would call wake_up_bit() after clearing the bit. +* Wake up any process waiting in wait_on_bit() or similar for the +* given bit to be cleared. * -* In order for this to function properly, as it uses waitqueue_active() -* internally, some kind of memory barrier must be done prior to calling -* this. Typically, this will be smp_mb__after_atomic(), but in some -* cases where bitflags are manipulated non-atomically under a lock, one -* may need to use a less regular barrier, such fs/inode.c's smp_mb(), -* because spin_unlock() does not guarantee a memory barrier. +* The wake-up is sent to tasks in a waitqueue selected by hash from a +* shared pool. Only those tasks on that queue which have requested +* wake_up on this specific address and bit will be woken, and only if the +* bit is clear. +* +* In order for this to function properly there must be a full memory +* barrier after the bit is cleared and before this function is called. +* If the bit was cleared atomically, such as a by clear_bit() then +* smb_mb__after_atomic() can be used, othwewise smb_mb() is needed. +* If the bit was cleared with a fully-ordered operation, no further +* barrier is required. +* +* Normally the bit should be cleared by an operation with RELEASE +* semantics so that any changes to memory made before the bit is +* cleared are guaranteed to be visible after the matching wait_on_bit() +* completes. */ void wake_up_bit (unsigned long *word,int bit) {
Signature · 1 line
2.46.0
[tip: sched/core] sched: Improve documentation for wake_up_bit/wait_on_bit family of functions
The following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: 3cdee6b359f134da22f7fd4606e0338413cfd79e Gitweb: https://git.kernel.org/tip/3cdee6b359f134da22f7fd4606e0338413cfd79e Author: NeilBrown <[email protected]> AuthorDate: Wed, 25 Sep 2024 15:31:39 +10:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 07 Oct 2024 09:28:37 +02:00
sched: Improve documentation for wake_up_bit/wait_on_bit family of functions
This patch revises the documention for wake_up_bit(), clear_and_wake_up_bit(), and all the wait_on_bit() family of functions.
The new documentation places less emphasis on the pool of waitqueues used (an implementation detail) and focuses instead on details of how the functions behave.
The barriers included in the wait functions and clear_and_wake_up_bit() and those required for wake_up_bit() are spelled out more clearly.
The error statuses returned are given explicitly.
The fact that the wait_on_bit_lock() function sets the bit is made more obvious.
--- include/linux/wait_bit.h | 159 ++++++++++++++++++++------------------ kernel/sched/wait_bit.c | 34 ++++---- 2 files changed, 107 insertions(+), 86 deletions(-)
include/linux/wait_bit.h +86 -73
@@ -53,19 +53,21 @@ extern int bit_wait_io_timeout(struct wait_bit_key *key, int mode);/** * wait_on_bit - wait for a bit to be cleared -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* @word: the address containing the bit being waited on +* @bit: the bit at that address being waited on * @mode: the task state to sleep in * -* There is a standard hashed waitqueue table for generic use. This -* is the part of the hashtable's accessor API that waits on a bit. -* For instance, if one were to have waiters on a bitflag, one would -* call wait_on_bit() in threads waiting for the bit to clear. -* One uses wait_on_bit() where one is waiting for the bit to clear, -* but has no intention of setting it. -* Returned value will be zero if the bit was cleared, or non-zero -* if the process received a signal and the mode permitted wakeup -* on that signal. +* Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP()) +* to be cleared. The clearing of the bit must be signalled with +* wake_up_bit(), often as clear_and_wake_up_bit(). +* +* The process will wait on a waitqueue selected by hash from a shared +* pool. It will only be woken on a wake_up for the target bit, even +* if other processes on the same queue are waiting for other bits. +* +* Returned value will be zero if the bit was cleared in which case the +* call has ACQUIRE semantics, or %-EINTR if the process received a +* signal and the mode permitted wake up on that signal. */ static inline int wait_on_bit (unsigned long *word,int bit,unsigned mode)
@@ -80,17 +82,20 @@ wait_on_bit(unsigned long *word, int bit, unsigned mode)/** * wait_on_bit_io - wait for a bit to be cleared -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* @word: the address containing the bit being waited on +* @bit: the bit at that address being waited on * @mode: the task state to sleep in * -* Use the standard hashed waitqueue table to wait for a bit -* to be cleared. This is similar to wait_on_bit(), but calls -* io_schedule() instead of schedule() for the actual waiting. +* Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP()) +* to be cleared. The clearing of the bit must be signalled with +* wake_up_bit(), often as clear_and_wake_up_bit(). +* +* This is similar to wait_on_bit(), but calls io_schedule() instead of +* schedule() for the actual waiting. * -* Returned value will be zero if the bit was cleared, or non-zero -* if the process received a signal and the mode permitted wakeup -* on that signal. +* Returned value will be zero if the bit was cleared in which case the +* call has ACQUIRE semantics, or %-EINTR if the process received a +* signal and the mode permitted wake up on that signal. */ static inline int wait_on_bit_io (unsigned long *word,int bit,unsigned mode)
@@ -104,19 +109,24 @@ wait_on_bit_io(unsigned long *word, int bit, unsigned mode) }/** -* wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* wait_on_bit_timeout - wait for a bit to be cleared or a timeout to elapse +* @word: the address containing the bit being waited on +* @bit: the bit at that address being waited on * @mode: the task state to sleep in * @timeout: timeout, in jiffies * -* Use the standard hashed waitqueue table to wait for a bit -* to be cleared. This is similar to wait_on_bit(), except also takes a -* timeout parameter. +* Wait for the given bit in an unsigned long or bitmap (see +* DECLARE_BITMAP()) to be cleared, or for a timeout to expire. The +* clearing of the bit must be signalled with wake_up_bit(), often as +* clear_and_wake_up_bit(). * -* Returned value will be zero if the bit was cleared before the -* @timeout elapsed, or non-zero if the @timeout elapsed or process -* received a signal and the mode permitted wakeup on that signal. +* This is similar to wait_on_bit(), except it also takes a timeout +* parameter. +* +* Returned value will be zero if the bit was cleared in which case the +* call has ACQUIRE semantics, or %-EINTR if the process received a +* signal and the mode permitted wake up on that signal, or %-EAGAIN if the +* timeout elapsed. */ static inline int wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
@@ -132,19 +142,21 @@ wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,/** * wait_on_bit_action - wait for a bit to be cleared -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* @word: the address containing the bit waited on +* @bit: the bit at that address being waited on * @action: the function used to sleep, which may take special actions * @mode: the task state to sleep in * -* Use the standard hashed waitqueue table to wait for a bit -* to be cleared, and allow the waiting action to be specified. -* This is like wait_on_bit() but allows fine control of how the waiting -* is done. +* Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP()) +* to be cleared. The clearing of the bit must be signalled with +* wake_up_bit(), often as clear_and_wake_up_bit(). +* +* This is similar to wait_on_bit(), but calls @action() instead of +* schedule() for the actual waiting. * -* Returned value will be zero if the bit was cleared, or non-zero -* if the process received a signal and the mode permitted wakeup -* on that signal. +* Returned value will be zero if the bit was cleared in which case the +* call has ACQUIRE semantics, or the error code returned by @action if +* that call returned non-zero. */ static inline int wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
@@ -157,23 +169,22 @@ wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action, }/** -* wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* wait_on_bit_lock - wait for a bit to be cleared, then set it +* @word: the address containing the bit being waited on +* @bit: the bit of the word being waited on and set * @mode: the task state to sleep in * -* There is a standard hashed waitqueue table for generic use. This -* is the part of the hashtable's accessor API that waits on a bit -* when one intends to set it, for instance, trying to lock bitflags. -* For instance, if one were to have waiters trying to set bitflag -* and waiting for it to clear before setting it, one would call -* wait_on_bit() in threads waiting to be able to set the bit. -* One uses wait_on_bit_lock() where one is waiting for the bit to -* clear with the intention of setting it, and when done, clearing it. +* Wait for the given bit in an unsigned long or bitmap (see +* DECLARE_BITMAP()) to be cleared. The clearing of the bit must be +* signalled with wake_up_bit(), often as clear_and_wake_up_bit(). As +* soon as it is clear, atomically set it and return. * -* Returns zero if the bit was (eventually) found to be clear and was -* set. Returns non-zero if a signal was delivered to the process and -* the @mode allows that signal to wake the process. +* This is similar to wait_on_bit(), but sets the bit before returning. +* +* Returned value will be zero if the bit was successfully set in which +* case the call has the same memory sequencing semantics as +* test_and_clear_bit(), or %-EINTR if the process received a signal and +* the mode permitted wake up on that signal. */ static inline int wait_on_bit_lock (unsigned long *word,int bit,unsigned mode)
@@ -185,15 +196,18 @@ wait_on_bit_lock(unsigned long *word, int bit, unsigned mode) }/** -* wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* wait_on_bit_lock_io - wait for a bit to be cleared, then set it +* @word: the address containing the bit being waited on +* @bit: the bit of the word being waited on and set * @mode: the task state to sleep in * -* Use the standard hashed waitqueue table to wait for a bit -* to be cleared and then to atomically set it. This is similar -* to wait_on_bit(), but calls io_schedule() instead of schedule() -* for the actual waiting. +* Wait for the given bit in an unsigned long or bitmap (see +* DECLARE_BITMAP()) to be cleared. The clearing of the bit must be +* signalled with wake_up_bit(), often as clear_and_wake_up_bit(). As +* soon as it is clear, atomically set it and return. +* +* This is similar to wait_on_bit_lock(), but calls io_schedule() instead +* of schedule(). * * Returns zero if the bit was (eventually) found to be clear and was * set. Returns non-zero if a signal was delivered to the process and
@@ -209,21 +223,19 @@ wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode) }/** -* wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* wait_on_bit_lock_action - wait for a bit to be cleared, then set it +* @word: the address containing the bit being waited on +* @bit: the bit of the word being waited on and set * @action: the function used to sleep, which may take special actions * @mode: the task state to sleep in * -* Use the standard hashed waitqueue table to wait for a bit -* to be cleared and then to set it, and allow the waiting action -* to be specified. -* This is like wait_on_bit() but allows fine control of how the waiting -* is done. +* This is similar to wait_on_bit_lock(), but calls @action() instead of +* schedule() for the actual waiting. * -* Returns zero if the bit was (eventually) found to be clear and was -* set. Returns non-zero if a signal was delivered to the process and -* the @mode allows that signal to wake the process. +* Returned value will be zero if the bit was successfully set in which +* case the call has the same memory sequencing semantics as +* test_and_clear_bit(), or the error code returned by @action if that +* call returned non-zero. */ static inline int wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
@@ -320,12 +332,13 @@ do { \/** * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit -* * @bit: the bit of the word being waited on -* @word: the word being waited on, a kernel virtual address +* @word: the address containing the bit being waited on * -* You can use this helper if bitflags are manipulated atomically rather than -* non-atomically under a lock. +* The designated bit is cleared and any tasks waiting in wait_on_bit() +* or similar will be woken. This call has RELEASE semantics so that +* any changes to memory made before this call are guaranteed to be visible +* after the corresponding wait_on_bit() completes. */ static inline void clear_and_wake_up_bit (int bit,unsigned long *word) {
kernel/sched/wait_bit.c +21 -13
@@ -128,21 +128,29 @@ void __wake_up_bit(struct wait_queue_head *wq_head, unsigned long *word, int bitEXPORT_SYMBOL (__wake_up_bit);/** -* wake_up_bit - wake up a waiter on a bit -* @word: the word being waited on, a kernel virtual address -* @bit: the bit of the word being waited on +* wake_up_bit - wake up waiters on a bit +* @word: the address containing the bit being waited on +* @bit: the bit at that address being waited on * -* There is a standard hashed waitqueue table for generic use. This -* is the part of the hash-table's accessor API that wakes up waiters -* on a bit. For instance, if one were to have waiters on a bitflag, -* one would call wake_up_bit() after clearing the bit. +* Wake up any process waiting in wait_on_bit() or similar for the +* given bit to be cleared. * -* In order for this to function properly, as it uses waitqueue_active() -* internally, some kind of memory barrier must be done prior to calling -* this. Typically, this will be smp_mb__after_atomic(), but in some -* cases where bitflags are manipulated non-atomically under a lock, one -* may need to use a less regular barrier, such fs/inode.c's smp_mb(), -* because spin_unlock() does not guarantee a memory barrier. +* The wake-up is sent to tasks in a waitqueue selected by hash from a +* shared pool. Only those tasks on that queue which have requested +* wake_up on this specific address and bit will be woken, and only if the +* bit is clear. +* +* In order for this to function properly there must be a full memory +* barrier after the bit is cleared and before this function is called. +* If the bit was cleared atomically, such as a by clear_bit() then +* smb_mb__after_atomic() can be used, othwewise smb_mb() is needed. +* If the bit was cleared with a fully-ordered operation, no further +* barrier is required. +* +* Normally the bit should be cleared by an operation with RELEASE +* semantics so that any changes to memory made before the bit is +* cleared are guaranteed to be visible after the matching wait_on_bit() +* completes. */ void wake_up_bit (unsigned long *word,int bit) {
PATCH v3 3/7 sched: Document wait_var_event() family of functions and wake_up_var()
In reply to NeilBrownwake_up_var(), wait_var_event() and related interfaces are not documented but have important ordering requirements. This patch adds documentation and makes these requirements explicit.
The return values for those wait_var_event_* functions which return a value are documented. Note that these are, perhaps surprisingly, sometimes different from comparable wait_on_bit() functions.
--- include/linux/wait_bit.h | 71 ++++++++++++++++++++++++++++++++++++++++ kernel/sched/wait_bit.c | 30 +++++++++++++++++ 2 files changed, 101 insertions(+)
include/linux/wait_bit.h +71 -0
@@ -282,6 +282,22 @@ __out: __ret; \___wait_var_event (var, condition,TASK_UNINTERRUPTIBLE ,0 ,0 , \schedule ()) +/** +* wait_var_event - wait for a variable to be updated and notified +* @var: the address of variable being waited on +* @condition: the condition to wait for +* +* Wait for a @condition to be true, only re-checking when a wake up is +* received for the given @var (an arbitrary kernel address which need +* not be directly related to the given condition, but usually is). +* +* The process will wait on a waitqueue selected by hash from a shared +* pool. It will only be woken on a wake_up for the given address. +* +* The condition should normally use smp_load_acquire() or a similarly +* ordered access to ensure that any changes to memory made before the +* condition became true will be visible after the wait completes. + */#define wait_var_event (var, condition) \ do { \ might_sleep(); \
@@ -294,6 +310,24 @@ do { \___wait_var_event (var, condition,TASK_KILLABLE ,0 ,0 , \schedule ()) +/** +* wait_var_event_killable - wait for a variable to be updated and notified +* @var: the address of variable being waited on +* @condition: the condition to wait for +* +* Wait for a @condition to be true or a fatal signal to be received, +* only re-checking the condition when a wake up is received for the given +* @var (an arbitrary kernel address which need not be directly related +* to the given condition, but usually is). +* +* This is similar to wait_var_event() but returns a value which is +* 0 if the condition became true, or %-ERESTARTSYS if a fatal signal +* was received. +* +* The condition should normally use smp_load_acquire() or a similarly +* ordered access to ensure that any changes to memory made before the +* condition became true will be visible after the wait completes. + */#define wait_var_event_killable (var, condition) \ ({ \ int __ret = 0; \
@@ -308,6 +342,26 @@ do { \ TASK_UNINTERRUPTIBLE, 0, timeout, \ __ret = schedule_timeout(__ret)) +/** +* wait_var_event_timeout - wait for a variable to be updated or a timeout to expire +* @var: the address of variable being waited on +* @condition: the condition to wait for +* @timeout: maximum time to wait in jiffies +* +* Wait for a @condition to be true or a timeout to expire, only +* re-checking the condition when a wake up is received for the given +* @var (an arbitrary kernel address which need not be directly related +* to the given condition, but usually is). +* +* This is similar to wait_var_event() but returns a value which is 0 if +* the timeout expired and the condition was still false, or the +* remaining time left in the timeout (but at least 1) if the condition +* was found to be true. +* +* The condition should normally use smp_load_acquire() or a similarly +* ordered access to ensure that any changes to memory made before the +* condition became true will be visible after the wait completes. +*/ #define wait_var_event_timeout (var, condition, timeout) \ ({ \ long __ret = timeout; \
@@ -321,6 +375,23 @@ do { \___wait_var_event (var, condition,TASK_INTERRUPTIBLE ,0 ,0 , \schedule ()) +/** +* wait_var_event_killable - wait for a variable to be updated and notified +* @var: the address of variable being waited on +* @condition: the condition to wait for +* +* Wait for a @condition to be true or a signal to be received, only +* re-checking the condition when a wake up is received for the given +* @var (an arbitrary kernel address which need not be directly related +* to the given condition, but usually is). +* +* This is similar to wait_var_event() but returns a value which is 0 if +* the condition became true, or %-ERESTARTSYS if a signal was received. +* +* The condition should normally use smp_load_acquire() or a similarly +* ordered access to ensure that any changes to memory made before the +* condition became true will be visible after the wait completes. + */#define wait_var_event_interruptible (var, condition) \ ({ \ int __ret = 0; \
kernel/sched/wait_bit.c +30 -0
@@ -196,6 +196,36 @@ void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int }EXPORT_SYMBOL (init_wait_var_entry); +/** +* wake_up_var - wake up waiters on a variable (kernel address) +* @var: the address of the variable being waited on +* +* Wake up any process waiting in wait_var_event() or similar for the +* given variable to change. wait_var_event() can be waiting for an +* arbitrary condition to be true and associates that condition with an +* address. Calling wake_up_var() suggests that the condition has been +* made true, but does not strictly require the condtion to use the +* address given. +* +* The wake-up is sent to tasks in a waitqueue selected by hash from a +* shared pool. Only those tasks on that queue which have requested +* wake_up on this specific address will be woken. +* +* In order for this to function properly there must be a full memory +* barrier after the variable is updated (or more accurately, after the +* condition waited on has been made to be true) and before this function +* is called. If the variable was updated atomically, such as a by +* atomic_dec() then smb_mb__after_atomic() can be used. If the +* variable was updated by a fully ordered operation such as +* atomic_dec_and_test() then no extra barrier is required. Otherwise +* smb_mb() is needed. +* +* Normally the variable should be updated (the condition should be made +* to be true) by an operation with RELEASE semantics such as +* smp_store_release() so that any changes to memory made before the +* variable was updated are guaranteed to be visible after the matching +* wait_var_event() completes. +*/ void wake_up_var (void *var) {__wake_up_bit (__var_waitqueue (var), var,-1 );
Signature · 1 line
2.46.0
[tip: sched/core] sched: Document wait_var_event() family of functions and wake_up_var()
The following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: bf39882edc798279765ca31751f6e679b50b97ef Gitweb: https://git.kernel.org/tip/bf39882edc798279765ca31751f6e679b50b97ef Author: NeilBrown <[email protected]> AuthorDate: Wed, 25 Sep 2024 15:31:40 +10:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 07 Oct 2024 09:28:38 +02:00
sched: Document wait_var_event() family of functions and wake_up_var()
wake_up_var(), wait_var_event() and related interfaces are not documented but have important ordering requirements. This patch adds documentation and makes these requirements explicit.
The return values for those wait_var_event_* functions which return a value are documented. Note that these are, perhaps surprisingly, sometimes different from comparable wait_on_bit() functions.
--- include/linux/wait_bit.h | 71 +++++++++++++++++++++++++++++++++++++++- kernel/sched/wait_bit.c | 30 ++++++++++++++++- 2 files changed, 101 insertions(+)
include/linux/wait_bit.h +71 -0
@@ -282,6 +282,22 @@ __out: __ret; \___wait_var_event (var, condition,TASK_UNINTERRUPTIBLE ,0 ,0 , \schedule ()) +/** +* wait_var_event - wait for a variable to be updated and notified +* @var: the address of variable being waited on +* @condition: the condition to wait for +* +* Wait for a @condition to be true, only re-checking when a wake up is +* received for the given @var (an arbitrary kernel address which need +* not be directly related to the given condition, but usually is). +* +* The process will wait on a waitqueue selected by hash from a shared +* pool. It will only be woken on a wake_up for the given address. +* +* The condition should normally use smp_load_acquire() or a similarly +* ordered access to ensure that any changes to memory made before the +* condition became true will be visible after the wait completes. + */#define wait_var_event (var, condition) \ do { \ might_sleep(); \
@@ -294,6 +310,24 @@ do { \___wait_var_event (var, condition,TASK_KILLABLE ,0 ,0 , \schedule ()) +/** +* wait_var_event_killable - wait for a variable to be updated and notified +* @var: the address of variable being waited on +* @condition: the condition to wait for +* +* Wait for a @condition to be true or a fatal signal to be received, +* only re-checking the condition when a wake up is received for the given +* @var (an arbitrary kernel address which need not be directly related +* to the given condition, but usually is). +* +* This is similar to wait_var_event() but returns a value which is +* 0 if the condition became true, or %-ERESTARTSYS if a fatal signal +* was received. +* +* The condition should normally use smp_load_acquire() or a similarly +* ordered access to ensure that any changes to memory made before the +* condition became true will be visible after the wait completes. + */#define wait_var_event_killable (var, condition) \ ({ \ int __ret = 0; \
@@ -308,6 +342,26 @@ do { \ TASK_UNINTERRUPTIBLE, 0, timeout, \ __ret = schedule_timeout(__ret)) +/** +* wait_var_event_timeout - wait for a variable to be updated or a timeout to expire +* @var: the address of variable being waited on +* @condition: the condition to wait for +* @timeout: maximum time to wait in jiffies +* +* Wait for a @condition to be true or a timeout to expire, only +* re-checking the condition when a wake up is received for the given +* @var (an arbitrary kernel address which need not be directly related +* to the given condition, but usually is). +* +* This is similar to wait_var_event() but returns a value which is 0 if +* the timeout expired and the condition was still false, or the +* remaining time left in the timeout (but at least 1) if the condition +* was found to be true. +* +* The condition should normally use smp_load_acquire() or a similarly +* ordered access to ensure that any changes to memory made before the +* condition became true will be visible after the wait completes. +*/ #define wait_var_event_timeout (var, condition, timeout) \ ({ \ long __ret = timeout; \
@@ -321,6 +375,23 @@ do { \___wait_var_event (var, condition,TASK_INTERRUPTIBLE ,0 ,0 , \schedule ()) +/** +* wait_var_event_killable - wait for a variable to be updated and notified +* @var: the address of variable being waited on +* @condition: the condition to wait for +* +* Wait for a @condition to be true or a signal to be received, only +* re-checking the condition when a wake up is received for the given +* @var (an arbitrary kernel address which need not be directly related +* to the given condition, but usually is). +* +* This is similar to wait_var_event() but returns a value which is 0 if +* the condition became true, or %-ERESTARTSYS if a signal was received. +* +* The condition should normally use smp_load_acquire() or a similarly +* ordered access to ensure that any changes to memory made before the +* condition became true will be visible after the wait completes. + */#define wait_var_event_interruptible (var, condition) \ ({ \ int __ret = 0; \
kernel/sched/wait_bit.c +30 -0
@@ -196,6 +196,36 @@ void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int }EXPORT_SYMBOL (init_wait_var_entry); +/** +* wake_up_var - wake up waiters on a variable (kernel address) +* @var: the address of the variable being waited on +* +* Wake up any process waiting in wait_var_event() or similar for the +* given variable to change. wait_var_event() can be waiting for an +* arbitrary condition to be true and associates that condition with an +* address. Calling wake_up_var() suggests that the condition has been +* made true, but does not strictly require the condtion to use the +* address given. +* +* The wake-up is sent to tasks in a waitqueue selected by hash from a +* shared pool. Only those tasks on that queue which have requested +* wake_up on this specific address will be woken. +* +* In order for this to function properly there must be a full memory +* barrier after the variable is updated (or more accurately, after the +* condition waited on has been made to be true) and before this function +* is called. If the variable was updated atomically, such as a by +* atomic_dec() then smb_mb__after_atomic() can be used. If the +* variable was updated by a fully ordered operation such as +* atomic_dec_and_test() then no extra barrier is required. Otherwise +* smb_mb() is needed. +* +* Normally the variable should be updated (the condition should be made +* to be true) by an operation with RELEASE semantics such as +* smp_store_release() so that any changes to memory made before the +* variable was updated are guaranteed to be visible after the matching +* wait_var_event() completes. +*/ void wake_up_var (void *var) {__wake_up_bit (__var_waitqueue (var), var,-1 );
PATCH v3 4/7 sched: Add test_and_clear_wake_up_bit() and atomic_dec_and_wake_up()
In reply to NeilBrownThere are common patterns in the kernel of using test_and_clear_bit() before wake_up_bit(), and atomic_dec_and_test() before wake_up_var().
These combinations don't need extra barriers but sometimes include them unnecessarily.
To help avoid the unnecessary barriers and to help discourage the general use of wake_up_bit/var (which is a fragile interface) introduce two combined functions which implement these patterns.
Also add store_release_wake_up() which supports the task of simply setting a non-atomic variable and sending a wakeup. This pattern requires barriers which are often omitted.
--- include/linux/wait_bit.h | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+)
include/linux/wait_bit.h +60 -0
@@ -419,4 +419,64 @@ static inline void clear_and_wake_up_bit(int bit, unsigned long *word) wake_up_bit(word, bit); } +/** +* test_and_clear_wake_up_bit - clear a bit if it was set: wake up anyone waiting on that bit +* @bit: the bit of the word being waited on +* @word: the address of memory containing that bit +* +* If the bit is set and can be atomically cleared, any tasks waiting in +* wait_on_bit() or similar will be woken. This call has the same +* complete ordering semantics as test_and_clear_bit(). Any changes to +* memory made before this call are guaranteed to be visible after the +* corresponding wait_on_bit() completes. +* +* Returns %true if the bit was successfully set and the wake up was sent. +*/ +static inline bool test_and_clear_wake_up_bit (int bit,unsigned long *word) +{ +if (!test_and_clear_bit (bit, word)) +return false; +/* no extra barrier required */ +wake_up_bit (word, bit); +return true; +} + +/** +* atomic_dec_and_wake_up - decrement an atomic_t and if zero, wake up waiters +* @var: the variable to dec and test +* +* Decrements the atomic variable and if it reaches zero, send a wake_up to any +* processes waiting on the variable. +* +* This function has the same complete ordering semantics as atomic_dec_and_test. +* +* Returns %true is the variable reaches zero and the wake up was sent. +*/ + +static inline bool atomic_dec_and_wake_up (atomic_t *var) +{ +if (!atomic_dec_and_test (var)) +return false; +/* No extra barrier required */ +wake_up_var (var); +return true; +} + +/** +* store_release_wake_up - update a variable and send a wake_up +* @var: the address of the variable to be updated and woken +* @val: the value to store in the variable. +* +* Store the given value in the variable send a wake up to any tasks +* waiting on the variable. All necessary barriers are included to ensure +* the task calling wait_var_event() sees the new value and all values +* written to memory before this call. +*/ +#define store_release_wake_up (var, val) \ +do { \ + smp_store_release(var, val); \ + smp_mb(); \ + wake_up_var(var); \ +} while (0) +#endif /* _LINUX_WAIT_BIT_H */
Signature · 1 line
2.46.0
[tip: sched/core] sched: Add test_and_clear_wake_up_bit() and atomic_dec_and_wake_up()
The following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: 52d633def56c10fe3e82a2c5d88c3ecb3f4e4852 Gitweb: https://git.kernel.org/tip/52d633def56c10fe3e82a2c5d88c3ecb3f4e4852 Author: NeilBrown <[email protected]> AuthorDate: Wed, 25 Sep 2024 15:31:41 +10:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 07 Oct 2024 09:28:38 +02:00
sched: Add test_and_clear_wake_up_bit() and atomic_dec_and_wake_up()
There are common patterns in the kernel of using test_and_clear_bit() before wake_up_bit(), and atomic_dec_and_test() before wake_up_var().
These combinations don't need extra barriers but sometimes include them unnecessarily.
To help avoid the unnecessary barriers and to help discourage the general use of wake_up_bit/var (which is a fragile interface) introduce two combined functions which implement these patterns.
Also add store_release_wake_up() which supports the task of simply setting a non-atomic variable and sending a wakeup. This pattern requires barriers which are often omitted.
--- include/linux/wait_bit.h | 60 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+)
include/linux/wait_bit.h +60 -0
@@ -419,4 +419,64 @@ static inline void clear_and_wake_up_bit(int bit, unsigned long *word) wake_up_bit(word, bit); } +/** +* test_and_clear_wake_up_bit - clear a bit if it was set: wake up anyone waiting on that bit +* @bit: the bit of the word being waited on +* @word: the address of memory containing that bit +* +* If the bit is set and can be atomically cleared, any tasks waiting in +* wait_on_bit() or similar will be woken. This call has the same +* complete ordering semantics as test_and_clear_bit(). Any changes to +* memory made before this call are guaranteed to be visible after the +* corresponding wait_on_bit() completes. +* +* Returns %true if the bit was successfully set and the wake up was sent. +*/ +static inline bool test_and_clear_wake_up_bit (int bit,unsigned long *word) +{ +if (!test_and_clear_bit (bit, word)) +return false; +/* no extra barrier required */ +wake_up_bit (word, bit); +return true; +} + +/** +* atomic_dec_and_wake_up - decrement an atomic_t and if zero, wake up waiters +* @var: the variable to dec and test +* +* Decrements the atomic variable and if it reaches zero, send a wake_up to any +* processes waiting on the variable. +* +* This function has the same complete ordering semantics as atomic_dec_and_test. +* +* Returns %true is the variable reaches zero and the wake up was sent. +*/ + +static inline bool atomic_dec_and_wake_up (atomic_t *var) +{ +if (!atomic_dec_and_test (var)) +return false; +/* No extra barrier required */ +wake_up_var (var); +return true; +} + +/** +* store_release_wake_up - update a variable and send a wake_up +* @var: the address of the variable to be updated and woken +* @val: the value to store in the variable. +* +* Store the given value in the variable send a wake up to any tasks +* waiting on the variable. All necessary barriers are included to ensure +* the task calling wait_var_event() sees the new value and all values +* written to memory before this call. +*/ +#define store_release_wake_up (var, val) \ +do { \ + smp_store_release(var, val); \ + smp_mb(); \ + wake_up_var(var); \ +} while (0) +#endif /* _LINUX_WAIT_BIT_H */
PATCH v3 5/7 sched: Add wait/wake interface for variable updated under a lock.
In reply to NeilBrownSometimes we need to wait for a condition to be true which must be testing while holding a lock. Correspondingly the condition is made true while holding the lock and the wake up is sent under the lock.
This patch provides wake and wait interfaces which can be used for this situation when the lock is a mutex or a spinlock, or any other lock for which there are foo_lock() and foo_unlock() functions.
--- include/linux/wait_bit.h | 106 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+)
include/linux/wait_bit.h +106 -0
@@ -401,6 +401,112 @@ do { \ __ret; \ }) +/** +* wait_var_event_any_lock - wait for a variable to be updated under a lock +* @var: the address of the variable being waited on +* @condition: condition to wait for +* @lock: the object that is locked to protect updates to the variable +* @type: prefix on lock and unlock operations +* @state: waiting state, %TASK_UNINTERRUPTIBLE etc. +* +* Wait for a condition which can only be reliably tested while holding +* a lock. The variables assessed in the condition will normal be updated +* under the same lock, and the wake up should be signalled with +* wake_up_var_locked() under the same lock. +* +* This is similar to wait_var_event(), but assumes a lock is held +* while calling this function and while updating the variable. +* +* This must be called while the given lock is held and the lock will be +* dropped when schedule() is called to wait for a wake up, and will be +* reclaimed before testing the condition again. The functions used to +* unlock and lock the object are constructed by appending _unlock and _lock +* to @type. +* +* Return %-ERESTARTSYS if a signal arrives which is allowed to interrupt +* the wait according to @state. +*/ +#define wait_var_event_any_lock (var, condition, lock, type, state) \ +({ \ + int __ret = 0; \ + if (!(condition)) \ + __ret = ___wait_var_event(var, condition, state, 0, 0, \ + type ## _unlock(lock); \ + schedule(); \ + type ## _lock(lock)); \ + __ret; \ +}) + +/** +* wait_var_event_spinlock - wait for a variable to be updated under a spinlock +* @var: the address of the variable being waited on +* @condition: condition to wait for +* @lock: the spinlock which protects updates to the variable +* +* Wait for a condition which can only be reliably tested while holding +* a spinlock. The variables assessed in the condition will normal be updated +* under the same spinlock, and the wake up should be signalled with +* wake_up_var_locked() under the same spinlock. +* +* This is similar to wait_var_event(), but assumes a spinlock is held +* while calling this function and while updating the variable. +* +* This must be called while the given lock is held and the lock will be +* dropped when schedule() is called to wait for a wake up, and will be +* reclaimed before testing the condition again. +*/ +#define wait_var_event_spinlock (var, condition, lock) \ + wait_var_event_any_lock(var, condition, lock, spin, TASK_UNINTERRUPTIBLE) + +/** +* wait_var_event_mutex - wait for a variable to be updated under a mutex +* @var: the address of the variable being waited on +* @condition: condition to wait for +* @mutex: the mutex which protects updates to the variable +* +* Wait for a condition which can only be reliably tested while holding +* a mutex. The variables assessed in the condition will normal be +* updated under the same mutex, and the wake up should be signalled +* with wake_up_var_locked() under the same mutex. +* +* This is similar to wait_var_event(), but assumes a mutex is held +* while calling this function and while updating the variable. +* +* This must be called while the given mutex is held and the mutex will be +* dropped when schedule() is called to wait for a wake up, and will be +* reclaimed before testing the condition again. +*/ +#define wait_var_event_mutex (var, condition, lock) \ + wait_var_event_any_lock(var, condition, lock, mutex, TASK_UNINTERRUPTIBLE) + +/** +* wake_up_var_protected - wake up waiters for a variable asserting that it is safe +* @var: the address of the variable being waited on +* @cond: the condition which afirms this is safe +* +* When waking waiters which use wait_var_event_any_lock() the waker must be +* holding the reelvant lock to avoid races. This version of wake_up_var() +* asserts that the relevant lock is held and so no barrier is needed. +* The @cond is only tested when CONFIG_LOCKDEP is enabled. +*/ +#define wake_up_var_protected (var, cond) \ +do { \ + lockdep_assert(cond); \ + wake_up_var(var); \ +} while (0) + +/** +* wake_up_var_locked - wake up waiters for a variable while holding a spinlock or mutex +* @var: the address of the variable being waited on +* @lock: The spinlock or mutex what protects the variable +* +* Send a wake up for the given variable which should be waited for with +* wait_var_event_spinlock() or wait_var_event_mutex(). Unlike wake_up_var(), +* no extra barriers are needed as the locking provides sufficient sequencing. +*/ +#define wake_up_var_locked (var, lock) \ + wake_up_var_protected(var, lockdep_is_held(lock)) +/** * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit * @bit: the bit of the word being waited on
Signature · 1 line
2.46.0
[tip: sched/core] sched: Add wait/wake interface for variable updated under a lock.
The following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: cc2e1c82d7e474753681a38b07b63034e107e369 Gitweb: https://git.kernel.org/tip/cc2e1c82d7e474753681a38b07b63034e107e369 Author: NeilBrown <[email protected]> AuthorDate: Wed, 25 Sep 2024 15:31:42 +10:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 07 Oct 2024 09:28:38 +02:00
sched: Add wait/wake interface for variable updated under a lock.
Sometimes we need to wait for a condition to be true which must be testing while holding a lock. Correspondingly the condition is made true while holding the lock and the wake up is sent under the lock.
This patch provides wake and wait interfaces which can be used for this situation when the lock is a mutex or a spinlock, or any other lock for which there are foo_lock() and foo_unlock() functions.
--- include/linux/wait_bit.h | 106 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+)
include/linux/wait_bit.h +106 -0
@@ -402,6 +402,112 @@ do { \ })/** +* wait_var_event_any_lock - wait for a variable to be updated under a lock +* @var: the address of the variable being waited on +* @condition: condition to wait for +* @lock: the object that is locked to protect updates to the variable +* @type: prefix on lock and unlock operations +* @state: waiting state, %TASK_UNINTERRUPTIBLE etc. +* +* Wait for a condition which can only be reliably tested while holding +* a lock. The variables assessed in the condition will normal be updated +* under the same lock, and the wake up should be signalled with +* wake_up_var_locked() under the same lock. +* +* This is similar to wait_var_event(), but assumes a lock is held +* while calling this function and while updating the variable. +* +* This must be called while the given lock is held and the lock will be +* dropped when schedule() is called to wait for a wake up, and will be +* reclaimed before testing the condition again. The functions used to +* unlock and lock the object are constructed by appending _unlock and _lock +* to @type. +* +* Return %-ERESTARTSYS if a signal arrives which is allowed to interrupt +* the wait according to @state. +*/ +#define wait_var_event_any_lock (var, condition, lock, type, state) \ +({ \ + int __ret = 0; \ + if (!(condition)) \ + __ret = ___wait_var_event(var, condition, state, 0, 0, \ + type ## _unlock(lock); \ + schedule(); \ + type ## _lock(lock)); \ + __ret; \ +}) + +/** +* wait_var_event_spinlock - wait for a variable to be updated under a spinlock +* @var: the address of the variable being waited on +* @condition: condition to wait for +* @lock: the spinlock which protects updates to the variable +* +* Wait for a condition which can only be reliably tested while holding +* a spinlock. The variables assessed in the condition will normal be updated +* under the same spinlock, and the wake up should be signalled with +* wake_up_var_locked() under the same spinlock. +* +* This is similar to wait_var_event(), but assumes a spinlock is held +* while calling this function and while updating the variable. +* +* This must be called while the given lock is held and the lock will be +* dropped when schedule() is called to wait for a wake up, and will be +* reclaimed before testing the condition again. +*/ +#define wait_var_event_spinlock (var, condition, lock) \ + wait_var_event_any_lock(var, condition, lock, spin, TASK_UNINTERRUPTIBLE) + +/** +* wait_var_event_mutex - wait for a variable to be updated under a mutex +* @var: the address of the variable being waited on +* @condition: condition to wait for +* @mutex: the mutex which protects updates to the variable +* +* Wait for a condition which can only be reliably tested while holding +* a mutex. The variables assessed in the condition will normal be +* updated under the same mutex, and the wake up should be signalled +* with wake_up_var_locked() under the same mutex. +* +* This is similar to wait_var_event(), but assumes a mutex is held +* while calling this function and while updating the variable. +* +* This must be called while the given mutex is held and the mutex will be +* dropped when schedule() is called to wait for a wake up, and will be +* reclaimed before testing the condition again. +*/ +#define wait_var_event_mutex (var, condition, lock) \ + wait_var_event_any_lock(var, condition, lock, mutex, TASK_UNINTERRUPTIBLE) + +/** +* wake_up_var_protected - wake up waiters for a variable asserting that it is safe +* @var: the address of the variable being waited on +* @cond: the condition which afirms this is safe +* +* When waking waiters which use wait_var_event_any_lock() the waker must be +* holding the reelvant lock to avoid races. This version of wake_up_var() +* asserts that the relevant lock is held and so no barrier is needed. +* The @cond is only tested when CONFIG_LOCKDEP is enabled. +*/ +#define wake_up_var_protected (var, cond) \ +do { \ + lockdep_assert(cond); \ + wake_up_var(var); \ +} while (0) + +/** +* wake_up_var_locked - wake up waiters for a variable while holding a spinlock or mutex +* @var: the address of the variable being waited on +* @lock: The spinlock or mutex what protects the variable +* +* Send a wake up for the given variable which should be waited for with +* wait_var_event_spinlock() or wait_var_event_mutex(). Unlike wake_up_var(), +* no extra barriers are needed as the locking provides sufficient sequencing. +*/ +#define wake_up_var_locked (var, lock) \ + wake_up_var_protected(var, lockdep_is_held(lock)) + +/** * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit * @bit: the bit of the word being waited on * @word: the address containing the bit being waited on
PATCH v3 6/7 sched: add wait_var_event_io()
In reply to NeilBrownIt is not currently possible to wait wait_var_event for an io_schedule() style wait. This patch adds wait_var_event_io() for that purpose.
--- include/linux/wait_bit.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
include/linux/wait_bit.h +31 -0
@@ -281,6 +281,9 @@ __out: __ret; \#define __wait_var_event (var, condition) \ ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ schedule()) +#define __wait_var_event_io (var, condition) \ + ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ + io_schedule())/** * wait_var_event - wait for a variable to be updated and notified
@@ -306,6 +309,34 @@ do { \__wait_var_event (var, condition); \ }while (0 ) +/** +* wait_var_event_io - wait for a variable to be updated and notified +* @var: the address of variable being waited on +* @condition: the condition to wait for +* +* Wait for an IO related @condition to be true, only re-checking when a +* wake up is received for the given @var (an arbitrary kernel address +* which need not be directly related to the given condition, but +* usually is). +* +* The process will wait on a waitqueue selected by hash from a shared +* pool. It will only be woken on a wake_up for the given address. +* +* This is similar to wait_var_event(), but calls io_schedule() instead +* of schedule(). +* +* The condition should normally use smp_load_acquire() or a similarly +* ordered access to ensure that any changes to memory made before the +* condition became true will be visible after the wait completes. + */ +#define wait_var_event_io (var, condition) \ +do { \ + might_sleep(); \ + if (condition) \ + break; \ + __wait_var_event_io(var, condition); \ +} while (0) +#define __wait_var_event_killable (var, condition) \ ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \ schedule())
Signature · 1 line
2.46.0
[tip: sched/core] sched: add wait_var_event_io()
The following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: 80681c04c5e8e4297b9ebf201ca3ce6242aa16c3 Gitweb: https://git.kernel.org/tip/80681c04c5e8e4297b9ebf201ca3ce6242aa16c3 Author: NeilBrown <[email protected]> AuthorDate: Wed, 25 Sep 2024 15:31:43 +10:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 07 Oct 2024 09:28:39 +02:00
sched: add wait_var_event_io()
It is not currently possible to wait wait_var_event for an io_schedule() style wait. This patch adds wait_var_event_io() for that purpose.
--- include/linux/wait_bit.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
include/linux/wait_bit.h +31 -0
@@ -281,6 +281,9 @@ __out: __ret; \#define __wait_var_event (var, condition) \ ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ schedule()) +#define __wait_var_event_io (var, condition) \ + ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ + io_schedule())/** * wait_var_event - wait for a variable to be updated and notified
@@ -306,6 +309,34 @@ do { \__wait_var_event (var, condition); \ }while (0 ) +/** +* wait_var_event_io - wait for a variable to be updated and notified +* @var: the address of variable being waited on +* @condition: the condition to wait for +* +* Wait for an IO related @condition to be true, only re-checking when a +* wake up is received for the given @var (an arbitrary kernel address +* which need not be directly related to the given condition, but +* usually is). +* +* The process will wait on a waitqueue selected by hash from a shared +* pool. It will only be woken on a wake_up for the given address. +* +* This is similar to wait_var_event(), but calls io_schedule() instead +* of schedule(). +* +* The condition should normally use smp_load_acquire() or a similarly +* ordered access to ensure that any changes to memory made before the +* condition became true will be visible after the wait completes. + */ +#define wait_var_event_io (var, condition) \ +do { \ + might_sleep(); \ + if (condition) \ + break; \ + __wait_var_event_io(var, condition); \ +} while (0) +#define __wait_var_event_killable (var, condition) \ ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \ schedule())
PATCH v3 7/7 softirq: use bit waits instead of var waits.
In reply to NeilBrownThe waiting in softirq.c is always waiting for a bit to be cleared. This makes the bit wait functions seem more suitable.
By switching over we can rid of all explicit barriers. We also use wait_on_bit_lock() to avoid an explicit loop.
--- kernel/softirq.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
kernel/softirq.c +4 -10
@@ -748,10 +748,8 @@ EXPORT_SYMBOL(__tasklet_hi_schedule);static bool tasklet_clear_sched (struct tasklet_struct *t) { -if (test_and_clear_bit (TASKLET_STATE_SCHED , &t->state)) { -wake_up_var (&t->state); +if (test_and_clear_wake_up_bit (TASKLET_STATE_SCHED , &t->state))return true; - } WARN_ONCE(1, "tasklet SCHED state not set: %s %pS\n", t->use_callback ? "callback" : "func",
@@ -871,8 +869,7 @@ void tasklet_kill(struct tasklet_struct *t)if (in_interrupt ())pr_notice ("Attempt to kill tasklet from interrupt\n" ); -while (test_and_set_bit (TASKLET_STATE_SCHED , &t->state)) -wait_var_event (&t->state, !test_bit (TASKLET_STATE_SCHED , &t->state)); +wait_on_bit_lock (&t->state,TASKLET_STATE_SCHED ,TASK_UNINTERRUPTIBLE );tasklet_unlock_wait (t);tasklet_clear_sched (t);
@@ -882,16 +879,13 @@ EXPORT_SYMBOL(tasklet_kill);#if defined(CONFIG_SMP ) || defined(CONFIG_PREEMPT_RT )void tasklet_unlock (struct tasklet_struct *t) { -smp_mb__before_atomic (); -clear_bit (TASKLET_STATE_RUN , &t->state); -smp_mb__after_atomic (); -wake_up_var (&t->state); +clear_and_wake_up_bit (TASKLET_STATE_RUN , &t->state); }EXPORT_SYMBOL_GPL (tasklet_unlock);void tasklet_unlock_wait (struct tasklet_struct *t) { -wait_var_event (&t->state, !test_bit (TASKLET_STATE_RUN , &t->state)); +wait_on_bit (&t->state,TASKLET_STATE_RUN ,TASK_UNINTERRUPTIBLE ); }EXPORT_SYMBOL_GPL (tasklet_unlock_wait);#endif
Signature · 1 line
2.46.0
[tip: sched/core] softirq: use bit waits instead of var waits.
The following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: 49994911b401c5f6b979060ffbc834949a024d8a Gitweb: https://git.kernel.org/tip/49994911b401c5f6b979060ffbc834949a024d8a Author: NeilBrown <[email protected]> AuthorDate: Wed, 25 Sep 2024 15:31:44 +10:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 07 Oct 2024 09:28:39 +02:00
softirq: use bit waits instead of var waits.
The waiting in softirq.c is always waiting for a bit to be cleared. This makes the bit wait functions seem more suitable.
By switching over we can rid of all explicit barriers. We also use wait_on_bit_lock() to avoid an explicit loop.
--- kernel/softirq.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
kernel/softirq.c +4 -10
@@ -748,10 +748,8 @@ EXPORT_SYMBOL(__tasklet_hi_schedule);static bool tasklet_clear_sched (struct tasklet_struct *t) { -if (test_and_clear_bit (TASKLET_STATE_SCHED , &t->state)) { -wake_up_var (&t->state); +if (test_and_clear_wake_up_bit (TASKLET_STATE_SCHED , &t->state))return true; - } WARN_ONCE(1, "tasklet SCHED state not set: %s %pS\n", t->use_callback ? "callback" : "func",
@@ -871,8 +869,7 @@ void tasklet_kill(struct tasklet_struct *t)if (in_interrupt ())pr_notice ("Attempt to kill tasklet from interrupt\n" ); -while (test_and_set_bit (TASKLET_STATE_SCHED , &t->state)) -wait_var_event (&t->state, !test_bit (TASKLET_STATE_SCHED , &t->state)); +wait_on_bit_lock (&t->state,TASKLET_STATE_SCHED ,TASK_UNINTERRUPTIBLE );tasklet_unlock_wait (t);tasklet_clear_sched (t);
@@ -882,16 +879,13 @@ EXPORT_SYMBOL(tasklet_kill);#if defined(CONFIG_SMP ) || defined(CONFIG_PREEMPT_RT )void tasklet_unlock (struct tasklet_struct *t) { -smp_mb__before_atomic (); -clear_bit (TASKLET_STATE_RUN , &t->state); -smp_mb__after_atomic (); -wake_up_var (&t->state); +clear_and_wake_up_bit (TASKLET_STATE_RUN , &t->state); }EXPORT_SYMBOL_GPL (tasklet_unlock);void tasklet_unlock_wait (struct tasklet_struct *t) { -wait_var_event (&t->state, !test_bit (TASKLET_STATE_RUN , &t->state)); +wait_on_bit (&t->state,TASKLET_STATE_RUN ,TASK_UNINTERRUPTIBLE ); }EXPORT_SYMBOL_GPL (tasklet_unlock_wait);#endif
Quoted 4 lines · NeilBrown
this is a revised set of patches for cleaning up
wake_up_bitandwake_up_varand related. They are the result of more proof reading, and of trying to make use of the new interfaces anywhere in the kernel where these interfaces are used.
Now I have two variants of a v3 patch set in my inbox. One with 7 and one with 8 patches. Both sent within a couple of minutes....
Confused.
Thanks,
tglx
Quoted 10 lines · Thomas Gleixner
Quoted 4 lines · NeilBrown
this is a revised set of patches for cleaning up
wake_up_bitandwake_up_varand related. They are the result of more proof reading, and of trying to make use of the new interfaces anywhere in the kernel where these interfaces are used.Now I have two variants of a v3 patch set in my inbox. One with 7 and one with 8 patches. Both sent within a couple of minutes....
Confused.
Arg - sorry about that. I realise now what went wrong.
Please use the one with 7 patches. The first patch in the set of 8 has already been accepted by Jens for the block tree and has now been merged (as of Wednesday afternoon).
Thanks,
NeilBrown