Skip to content

Commit 8cc58a6

Browse files
jankaragregkh
authored andcommitted
rq-qos: fix missed wake-ups in rq_qos_throttle try two
commit 11c7aa0 upstream. Commit 545fbd0 ("rq-qos: fix missed wake-ups in rq_qos_throttle") tried to fix a problem that a process could be sleeping in rq_qos_wait() without anyone to wake it up. However the fix is not complete and the following can still happen: CPU1 (waiter1) CPU2 (waiter2) CPU3 (waker) rq_qos_wait() rq_qos_wait() acquire_inflight_cb() -> fails acquire_inflight_cb() -> fails completes IOs, inflight decreased prepare_to_wait_exclusive() prepare_to_wait_exclusive() has_sleeper = !wq_has_single_sleeper() -> true as there are two sleepers has_sleeper = !wq_has_single_sleeper() -> true io_schedule() io_schedule() Deadlock as now there's nobody to wakeup the two waiters. The logic automatically blocking when there are already sleepers is really subtle and the only way to make it work reliably is that we check whether there are some waiters in the queue when adding ourselves there. That way, we are guaranteed that at least the first process to enter the wait queue will recheck the waiting condition before going to sleep and thus guarantee forward progress. Fixes: 545fbd0 ("rq-qos: fix missed wake-ups in rq_qos_throttle") CC: stable@vger.kernel.org Signed-off-by: Jan Kara <jack@suse.cz> Link: https://lore.kernel.org/r/20210607112613.25344-1-jack@suse.cz Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f9fb498 commit 8cc58a6

3 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎block/blk-rq-qos.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ void rq_qos_wait(struct rq_wait *rqw, void *private_data,
266266
if (!has_sleeper && acquire_inflight_cb(rqw, private_data))
267267
return;
268268

269-
prepare_to_wait_exclusive(&rqw->wait, &data.wq, TASK_UNINTERRUPTIBLE);
270-
has_sleeper = !wq_has_single_sleeper(&rqw->wait);
269+
has_sleeper = !prepare_to_wait_exclusive(&rqw->wait, &data.wq,
270+
TASK_UNINTERRUPTIBLE);
271271
do {
272272
/* The memory barrier in set_task_state saves us here. */
273273
if (data.got_token)

‎include/linux/wait.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ do { \
11261126
* Waitqueues which are removed from the waitqueue_head at wakeup time
11271127
*/
11281128
void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1129-
void prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1129+
bool prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
11301130
long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
11311131
void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
11321132
long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);

‎kernel/sched/wait.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,22 @@ prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_ent
249249
}
250250
EXPORT_SYMBOL(prepare_to_wait);
251251

252-
void
252+
/* Returns true if we are the first waiter in the queue, false otherwise. */
253+
bool
253254
prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
254255
{
255256
unsigned long flags;
257+
bool was_empty = false;
256258

257259
wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
258260
spin_lock_irqsave(&wq_head->lock, flags);
259-
if (list_empty(&wq_entry->entry))
261+
if (list_empty(&wq_entry->entry)) {
262+
was_empty = list_empty(&wq_head->head);
260263
__add_wait_queue_entry_tail(wq_head, wq_entry);
264+
}
261265
set_current_state(state);
262266
spin_unlock_irqrestore(&wq_head->lock, flags);
267+
return was_empty;
263268
}
264269
EXPORT_SYMBOL(prepare_to_wait_exclusive);
265270

0 commit comments

Comments
 (0)