Skip to content

Commit 99ea334

Browse files
committed
sched: Improve documentation for wake_up_bit/wait_on_bit family of functions
JIRA: https://issues.redhat.com/browse/RHEL-117497 commit 3cdee6b Author: NeilBrown <neilb@suse.de> Date: Wed Sep 25 15:31:39 2024 +1000 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. Signed-off-by: NeilBrown <neilb@suse.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/r/20240925053405.3960701-3-neilb@suse.de Signed-off-by: Benjamin Coddington <bcodding@redhat.com>
1 parent 7aaa713 commit 99ea334

File tree

2 files changed

+107
-86
lines changed

2 files changed

+107
-86
lines changed

include/linux/wait_bit.h

Lines changed: 86 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,21 @@ extern int bit_wait_timeout(struct wait_bit_key *key, int mode);
5252

5353
/**
5454
* wait_on_bit - wait for a bit to be cleared
55-
* @word: the word being waited on, a kernel virtual address
56-
* @bit: the bit of the word being waited on
55+
* @word: the address containing the bit being waited on
56+
* @bit: the bit at that address being waited on
5757
* @mode: the task state to sleep in
5858
*
59-
* There is a standard hashed waitqueue table for generic use. This
60-
* is the part of the hashtable's accessor API that waits on a bit.
61-
* For instance, if one were to have waiters on a bitflag, one would
62-
* call wait_on_bit() in threads waiting for the bit to clear.
63-
* One uses wait_on_bit() where one is waiting for the bit to clear,
64-
* but has no intention of setting it.
65-
* Returned value will be zero if the bit was cleared, or non-zero
66-
* if the process received a signal and the mode permitted wakeup
67-
* on that signal.
59+
* Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP())
60+
* to be cleared. The clearing of the bit must be signalled with
61+
* wake_up_bit(), often as clear_and_wake_up_bit().
62+
*
63+
* The process will wait on a waitqueue selected by hash from a shared
64+
* pool. It will only be woken on a wake_up for the target bit, even
65+
* if other processes on the same queue are waiting for other bits.
66+
*
67+
* Returned value will be zero if the bit was cleared in which case the
68+
* call has ACQUIRE semantics, or %-EINTR if the process received a
69+
* signal and the mode permitted wake up on that signal.
6870
*/
6971
static inline int
7072
wait_on_bit(unsigned long *word, int bit, unsigned mode)
@@ -79,17 +81,20 @@ wait_on_bit(unsigned long *word, int bit, unsigned mode)
7981

8082
/**
8183
* wait_on_bit_io - wait for a bit to be cleared
82-
* @word: the word being waited on, a kernel virtual address
83-
* @bit: the bit of the word being waited on
84+
* @word: the address containing the bit being waited on
85+
* @bit: the bit at that address being waited on
8486
* @mode: the task state to sleep in
8587
*
86-
* Use the standard hashed waitqueue table to wait for a bit
87-
* to be cleared. This is similar to wait_on_bit(), but calls
88-
* io_schedule() instead of schedule() for the actual waiting.
88+
* Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP())
89+
* to be cleared. The clearing of the bit must be signalled with
90+
* wake_up_bit(), often as clear_and_wake_up_bit().
91+
*
92+
* This is similar to wait_on_bit(), but calls io_schedule() instead of
93+
* schedule() for the actual waiting.
8994
*
90-
* Returned value will be zero if the bit was cleared, or non-zero
91-
* if the process received a signal and the mode permitted wakeup
92-
* on that signal.
95+
* Returned value will be zero if the bit was cleared in which case the
96+
* call has ACQUIRE semantics, or %-EINTR if the process received a
97+
* signal and the mode permitted wake up on that signal.
9398
*/
9499
static inline int
95100
wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
@@ -103,19 +108,24 @@ wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
103108
}
104109

105110
/**
106-
* wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
107-
* @word: the word being waited on, a kernel virtual address
108-
* @bit: the bit of the word being waited on
111+
* wait_on_bit_timeout - wait for a bit to be cleared or a timeout to elapse
112+
* @word: the address containing the bit being waited on
113+
* @bit: the bit at that address being waited on
109114
* @mode: the task state to sleep in
110115
* @timeout: timeout, in jiffies
111116
*
112-
* Use the standard hashed waitqueue table to wait for a bit
113-
* to be cleared. This is similar to wait_on_bit(), except also takes a
114-
* timeout parameter.
117+
* Wait for the given bit in an unsigned long or bitmap (see
118+
* DECLARE_BITMAP()) to be cleared, or for a timeout to expire. The
119+
* clearing of the bit must be signalled with wake_up_bit(), often as
120+
* clear_and_wake_up_bit().
115121
*
116-
* Returned value will be zero if the bit was cleared before the
117-
* @timeout elapsed, or non-zero if the @timeout elapsed or process
118-
* received a signal and the mode permitted wakeup on that signal.
122+
* This is similar to wait_on_bit(), except it also takes a timeout
123+
* parameter.
124+
*
125+
* Returned value will be zero if the bit was cleared in which case the
126+
* call has ACQUIRE semantics, or %-EINTR if the process received a
127+
* signal and the mode permitted wake up on that signal, or %-EAGAIN if the
128+
* timeout elapsed.
119129
*/
120130
static inline int
121131
wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
@@ -131,19 +141,21 @@ wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
131141

132142
/**
133143
* wait_on_bit_action - wait for a bit to be cleared
134-
* @word: the word being waited on, a kernel virtual address
135-
* @bit: the bit of the word being waited on
144+
* @word: the address containing the bit waited on
145+
* @bit: the bit at that address being waited on
136146
* @action: the function used to sleep, which may take special actions
137147
* @mode: the task state to sleep in
138148
*
139-
* Use the standard hashed waitqueue table to wait for a bit
140-
* to be cleared, and allow the waiting action to be specified.
141-
* This is like wait_on_bit() but allows fine control of how the waiting
142-
* is done.
149+
* Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP())
150+
* to be cleared. The clearing of the bit must be signalled with
151+
* wake_up_bit(), often as clear_and_wake_up_bit().
152+
*
153+
* This is similar to wait_on_bit(), but calls @action() instead of
154+
* schedule() for the actual waiting.
143155
*
144-
* Returned value will be zero if the bit was cleared, or non-zero
145-
* if the process received a signal and the mode permitted wakeup
146-
* on that signal.
156+
* Returned value will be zero if the bit was cleared in which case the
157+
* call has ACQUIRE semantics, or the error code returned by @action if
158+
* that call returned non-zero.
147159
*/
148160
static inline int
149161
wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
@@ -156,23 +168,22 @@ wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
156168
}
157169

158170
/**
159-
* wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
160-
* @word: the word being waited on, a kernel virtual address
161-
* @bit: the bit of the word being waited on
171+
* wait_on_bit_lock - wait for a bit to be cleared, then set it
172+
* @word: the address containing the bit being waited on
173+
* @bit: the bit of the word being waited on and set
162174
* @mode: the task state to sleep in
163175
*
164-
* There is a standard hashed waitqueue table for generic use. This
165-
* is the part of the hashtable's accessor API that waits on a bit
166-
* when one intends to set it, for instance, trying to lock bitflags.
167-
* For instance, if one were to have waiters trying to set bitflag
168-
* and waiting for it to clear before setting it, one would call
169-
* wait_on_bit() in threads waiting to be able to set the bit.
170-
* One uses wait_on_bit_lock() where one is waiting for the bit to
171-
* clear with the intention of setting it, and when done, clearing it.
176+
* Wait for the given bit in an unsigned long or bitmap (see
177+
* DECLARE_BITMAP()) to be cleared. The clearing of the bit must be
178+
* signalled with wake_up_bit(), often as clear_and_wake_up_bit(). As
179+
* soon as it is clear, atomically set it and return.
172180
*
173-
* Returns zero if the bit was (eventually) found to be clear and was
174-
* set. Returns non-zero if a signal was delivered to the process and
175-
* the @mode allows that signal to wake the process.
181+
* This is similar to wait_on_bit(), but sets the bit before returning.
182+
*
183+
* Returned value will be zero if the bit was successfully set in which
184+
* case the call has the same memory sequencing semantics as
185+
* test_and_clear_bit(), or %-EINTR if the process received a signal and
186+
* the mode permitted wake up on that signal.
176187
*/
177188
static inline int
178189
wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
@@ -184,15 +195,18 @@ wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
184195
}
185196

186197
/**
187-
* wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
188-
* @word: the word being waited on, a kernel virtual address
189-
* @bit: the bit of the word being waited on
198+
* wait_on_bit_lock_io - wait for a bit to be cleared, then set it
199+
* @word: the address containing the bit being waited on
200+
* @bit: the bit of the word being waited on and set
190201
* @mode: the task state to sleep in
191202
*
192-
* Use the standard hashed waitqueue table to wait for a bit
193-
* to be cleared and then to atomically set it. This is similar
194-
* to wait_on_bit(), but calls io_schedule() instead of schedule()
195-
* for the actual waiting.
203+
* Wait for the given bit in an unsigned long or bitmap (see
204+
* DECLARE_BITMAP()) to be cleared. The clearing of the bit must be
205+
* signalled with wake_up_bit(), often as clear_and_wake_up_bit(). As
206+
* soon as it is clear, atomically set it and return.
207+
*
208+
* This is similar to wait_on_bit_lock(), but calls io_schedule() instead
209+
* of schedule().
196210
*
197211
* Returns zero if the bit was (eventually) found to be clear and was
198212
* set. Returns non-zero if a signal was delivered to the process and
@@ -208,21 +222,19 @@ wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
208222
}
209223

210224
/**
211-
* wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
212-
* @word: the word being waited on, a kernel virtual address
213-
* @bit: the bit of the word being waited on
225+
* wait_on_bit_lock_action - wait for a bit to be cleared, then set it
226+
* @word: the address containing the bit being waited on
227+
* @bit: the bit of the word being waited on and set
214228
* @action: the function used to sleep, which may take special actions
215229
* @mode: the task state to sleep in
216230
*
217-
* Use the standard hashed waitqueue table to wait for a bit
218-
* to be cleared and then to set it, and allow the waiting action
219-
* to be specified.
220-
* This is like wait_on_bit() but allows fine control of how the waiting
221-
* is done.
231+
* This is similar to wait_on_bit_lock(), but calls @action() instead of
232+
* schedule() for the actual waiting.
222233
*
223-
* Returns zero if the bit was (eventually) found to be clear and was
224-
* set. Returns non-zero if a signal was delivered to the process and
225-
* the @mode allows that signal to wake the process.
234+
* Returned value will be zero if the bit was successfully set in which
235+
* case the call has the same memory sequencing semantics as
236+
* test_and_clear_bit(), or the error code returned by @action if that
237+
* call returned non-zero.
226238
*/
227239
static inline int
228240
wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
@@ -319,12 +331,13 @@ do { \
319331

320332
/**
321333
* clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit
322-
*
323334
* @bit: the bit of the word being waited on
324-
* @word: the word being waited on, a kernel virtual address
335+
* @word: the address containing the bit being waited on
325336
*
326-
* You can use this helper if bitflags are manipulated atomically rather than
327-
* non-atomically under a lock.
337+
* The designated bit is cleared and any tasks waiting in wait_on_bit()
338+
* or similar will be woken. This call has RELEASE semantics so that
339+
* any changes to memory made before this call are guaranteed to be visible
340+
* after the corresponding wait_on_bit() completes.
328341
*/
329342
static inline void clear_and_wake_up_bit(int bit, unsigned long *word)
330343
{

kernel/sched/wait_bit.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,29 @@ void __wake_up_bit(struct wait_queue_head *wq_head, unsigned long *word, int bit
128128
EXPORT_SYMBOL(__wake_up_bit);
129129

130130
/**
131-
* wake_up_bit - wake up a waiter on a bit
132-
* @word: the word being waited on, a kernel virtual address
133-
* @bit: the bit of the word being waited on
131+
* wake_up_bit - wake up waiters on a bit
132+
* @word: the address containing the bit being waited on
133+
* @bit: the bit at that address being waited on
134134
*
135-
* There is a standard hashed waitqueue table for generic use. This
136-
* is the part of the hash-table's accessor API that wakes up waiters
137-
* on a bit. For instance, if one were to have waiters on a bitflag,
138-
* one would call wake_up_bit() after clearing the bit.
135+
* Wake up any process waiting in wait_on_bit() or similar for the
136+
* given bit to be cleared.
139137
*
140-
* In order for this to function properly, as it uses waitqueue_active()
141-
* internally, some kind of memory barrier must be done prior to calling
142-
* this. Typically, this will be smp_mb__after_atomic(), but in some
143-
* cases where bitflags are manipulated non-atomically under a lock, one
144-
* may need to use a less regular barrier, such fs/inode.c's smp_mb(),
145-
* because spin_unlock() does not guarantee a memory barrier.
138+
* The wake-up is sent to tasks in a waitqueue selected by hash from a
139+
* shared pool. Only those tasks on that queue which have requested
140+
* wake_up on this specific address and bit will be woken, and only if the
141+
* bit is clear.
142+
*
143+
* In order for this to function properly there must be a full memory
144+
* barrier after the bit is cleared and before this function is called.
145+
* If the bit was cleared atomically, such as a by clear_bit() then
146+
* smb_mb__after_atomic() can be used, othwewise smb_mb() is needed.
147+
* If the bit was cleared with a fully-ordered operation, no further
148+
* barrier is required.
149+
*
150+
* Normally the bit should be cleared by an operation with RELEASE
151+
* semantics so that any changes to memory made before the bit is
152+
* cleared are guaranteed to be visible after the matching wait_on_bit()
153+
* completes.
146154
*/
147155
void wake_up_bit(unsigned long *word, int bit)
148156
{

0 commit comments

Comments
 (0)