Skip to content

Commit 3cdee6b

Browse files
neilbrownPeter Zijlstra
authored andcommitted
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
1 parent 2382d68 commit 3cdee6b

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
@@ -53,19 +53,21 @@ extern int bit_wait_io_timeout(struct wait_bit_key *key, int mode);
5353

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

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

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

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

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

187198
/**
188-
* wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
189-
* @word: the word being waited on, a kernel virtual address
190-
* @bit: the bit of the word being waited on
199+
* wait_on_bit_lock_io - wait for a bit to be cleared, then set it
200+
* @word: the address containing the bit being waited on
201+
* @bit: the bit of the word being waited on and set
191202
* @mode: the task state to sleep in
192203
*
193-
* Use the standard hashed waitqueue table to wait for a bit
194-
* to be cleared and then to atomically set it. This is similar
195-
* to wait_on_bit(), but calls io_schedule() instead of schedule()
196-
* for the actual waiting.
204+
* Wait for the given bit in an unsigned long or bitmap (see
205+
* DECLARE_BITMAP()) to be cleared. The clearing of the bit must be
206+
* signalled with wake_up_bit(), often as clear_and_wake_up_bit(). As
207+
* soon as it is clear, atomically set it and return.
208+
*
209+
* This is similar to wait_on_bit_lock(), but calls io_schedule() instead
210+
* of schedule().
197211
*
198212
* Returns zero if the bit was (eventually) found to be clear and was
199213
* 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)
209223
}
210224

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

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

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)