Skip to contentSkip to conversation

blk-mq: fix hang issue in blk_queue_enter()

PATCH In review Yang Yang 2 participants 2 messages Sep 14, 2020 Block layer

lore
Share & tools
lore

Yang Yang

author
#0 lore raw

PATCH 1/1 blk-mq: fix hang issue in blk_queue_enter()

There is a race between blk_queue_enter() and block layer's runtime suspend.

 CPU0                                CPU1
 ---------------------------------   -------------------------------
 blk_pre_runtime_suspend(q) {        blk_queue_enter() {
   /* q->rpm_status=RPM_ACTIVE */
   blk_set_pm_only(q)
   /* q->pm_only=1 */
   blk_freeze_queue_start(q)
   blk_mq_unfreeze_queue(q)
                                       if (percpu_ref_tryget_live()) {
                                         /* pm=0 && q->pm_only=1 */
                                         if (pm || !blk_queue_pm_only(q)) {
                                         } else {
                                           percpu_ref_put()
                                         }
                                       }
                                       wait_event(q->mq_freeze_wq,
                                         (!q->mq_freeze_depth &&
                                         /* q->rpm_status=RPM_ACTIVE
                                            q->pm_only=1 */
                                         (pm || (blk_pm_request_resume(q),
                                          !blk_queue_pm_only(q)))) ||
                                          blk_queue_dying(q))
                                     }

spin_lock_irq(&q->queue_lock)
q->rpm_status = RPM_SUSPENDING
spin_unlock_irq(&q->queue_lock)
}

At this point blk_pm_request_resume() missed the chance to resume the queue, so blk_queue_enter() may wait here forever.
The solution is to wake up the mq_freeze_wq after runtime suspend completed, make blk_pm_request_resume() reexamine the q->rpm_status flag.

---
 block/blk-pm.c | 2 ++
 1 file changed, 2 insertions(+)
block/blk-pm.c +2 -0
index b85234d758f7..dec7d0aef606 100644
@@ -132,6 +132,8 @@ void blk_post_runtime_suspend(struct request_queue *q, int err)
 
 	if (err)
 		blk_clear_pm_only(q);
+	else
+		wake_up_all(&q->mq_freeze_wq);
 }
 EXPORT_SYMBOL(blk_post_runtime_suspend);
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 46 lines · Yang Yang

There is a race between blk_queue_enter() and block layer's runtime suspend.

 CPU0                                CPU1
 ---------------------------------   -------------------------------
 blk_pre_runtime_suspend(q) {        blk_queue_enter() {
   /* q->rpm_status=RPM_ACTIVE */
   blk_set_pm_only(q)
   /* q->pm_only=1 */
   blk_freeze_queue_start(q)
   blk_mq_unfreeze_queue(q)
                                       if (percpu_ref_tryget_live()) {
                                         /* pm=0 && q->pm_only=1 */
                                         if (pm || !blk_queue_pm_only(q)) {
                                         } else {
                                           percpu_ref_put()
                                         }
                                       }
                                       wait_event(q->mq_freeze_wq,
                                         (!q->mq_freeze_depth &&
                                         /* q->rpm_status=RPM_ACTIVE
                                            q->pm_only=1 */
                                         (pm || (blk_pm_request_resume(q),
                                          !blk_queue_pm_only(q)))) ||
                                          blk_queue_dying(q))
                                     }

spin_lock_irq(&q->queue_lock)
q->rpm_status = RPM_SUSPENDING
spin_unlock_irq(&q->queue_lock)
}

At this point blk_pm_request_resume() missed the chance to resume the queue, so blk_queue_enter() may wait here forever.
The solution is to wake up the mq_freeze_wq after runtime suspend completed, make blk_pm_request_resume() reexamine the q->rpm_status flag.

---
 block/blk-pm.c | 2 ++
 1 file changed, 2 insertions(+)
block/blk-pm.c +0 -0
index b85234d758f7..dec7d0aef606 100644

block/blk-pm.c void blk_post_runtime_suspend(struct request_queue *q, int err) · line 138

Quoted 7 lines
block/blk-pm.c +2 -0
@@ -132,6 +132,8 @@ void blk_post_runtime_suspend(struct request_queue *q, int err)
 
 	if (err)
 		blk_clear_pm_only(q);
+	else
+		wake_up_all(&q->mq_freeze_wq);
 }
 EXPORT_SYMBOL(blk_post_runtime_suspend);

Please verify whether the following patch series also fixes the reported
hang: https://lore.kernel.org/linux-block/[email protected]/T/#t

Thanks,

Bart.