rq-qos: fix missed wake-ups in rq_qos_throttle try two
authorJan Kara <jack@suse.cz>
Mon, 7 Jun 2021 11:26:13 +0000 (13:26 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 19 Jul 2021 06:53:16 +0000 (08:53 +0200)
commit 11c7aa0ddea8611007768d3e6b58d45dc60a19e1 upstream.

Commit 545fbd0775ba ("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: 545fbd0775ba ("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>
block/blk-rq-qos.c
include/linux/wait.h
kernel/sched/wait.c

index 656460636ad34a176297bfb991aba977b3c0e9ca..e83af7bc759194126a645bea59eef2b841603961 100644 (file)
@@ -266,8 +266,8 @@ void rq_qos_wait(struct rq_wait *rqw, void *private_data,
        if (!has_sleeper && acquire_inflight_cb(rqw, private_data))
                return;
 
-       prepare_to_wait_exclusive(&rqw->wait, &data.wq, TASK_UNINTERRUPTIBLE);
-       has_sleeper = !wq_has_single_sleeper(&rqw->wait);
+       has_sleeper = !prepare_to_wait_exclusive(&rqw->wait, &data.wq,
+                                                TASK_UNINTERRUPTIBLE);
        do {
                /* The memory barrier in set_task_state saves us here. */
                if (data.got_token)
index 3eb7cae8206c38d153ecdb72f0c7666b4fec42b7..032ae61c22a2b858a983c7e13e6ed019b2a10c8a 100644 (file)
@@ -1121,7 +1121,7 @@ do {                                                                              \
  * Waitqueues which are removed from the waitqueue_head at wakeup time
  */
 void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
-void prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
+bool prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
 long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
index c1e566a114ca64d5ef4d27a7db41c2d66f8039c0..84bd05117dc22a877a46636f7451e36736073ab4 100644 (file)
@@ -232,17 +232,22 @@ prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_ent
 }
 EXPORT_SYMBOL(prepare_to_wait);
 
-void
+/* Returns true if we are the first waiter in the queue, false otherwise. */
+bool
 prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
 {
        unsigned long flags;
+       bool was_empty = false;
 
        wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
        spin_lock_irqsave(&wq_head->lock, flags);
-       if (list_empty(&wq_entry->entry))
+       if (list_empty(&wq_entry->entry)) {
+               was_empty = list_empty(&wq_head->head);
                __add_wait_queue_entry_tail(wq_head, wq_entry);
+       }
        set_current_state(state);
        spin_unlock_irqrestore(&wq_head->lock, flags);
+       return was_empty;
 }
 EXPORT_SYMBOL(prepare_to_wait_exclusive);