Skip to contentSkip to conversation

block: release freeze lockdep map under mq_freeze_lock

PATCH New Nguyen Ngoc Thang 1 participant 1 message Nov 24 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: release freeze lockdep map under mq_freeze_lock

The outermost freezer records the lockdep state of the queue in q->mq_freeze_disk_dead and q->mq_freeze_queue_dying, and the unfreeze path checks them again to decide which lockdep maps to release. That check runs in blk_mq_unfreeze_queue_nomemrestore() after __blk_mq_unfreeze_queue() has dropped mq_freeze_lock. At that point mq_freeze_depth may already be zero, so another task can start a new freeze and overwrite both flags before the old owner reads them.

syzbot hits this with device-mapper: DM_TABLE_LOAD calls add_disk() and freezes the queue in elevator_change() while the queue is still alive, so q_lockdep_map is acquired. A concurrent DM_DEV_REMOVE calls blk_mark_disk_dead(), which sets QUEUE_FLAG_DYING and starts a new freeze right after the elevator unfreeze drops mq_freeze_lock. The new freeze sets mq_freeze_queue_dying, the old owner skips rwsem_release(), and lockdep reports:

WARNING: lock held when returning to user space!
#0: (&q->q_usage_counter(queue)#22){+.+.}-{0:0}, at: elevator_change+0x194/0x480

Release the lockdep maps in __blk_mq_unfreeze_queue() while mq_freeze_lock is still held, so a new freezer cannot change the flags first. __del_gendisk() now relies on that release in its unfreeze branch. In its GD_OWNS_QUEUE branch the queue stays frozen, so it still releases the maps directly.

Fixes f6661b1d0525 ("block: track queue dying state automatically for modeling queue freeze lockdep")
Reported-by [email protected]

---
Tested in QEMU (x86_64, 4 vCPUs, PROVE_LOCKING) with a C reproducer that races DM_TABLE_LOAD of a request-based multipath table against DM_DEV_REMOVE. With a debug-only msleep(5) right after mutex_unlock() in __blk_mq_unfreeze_queue() to widen the window:

base: 3/3 runs hit "lock held when returning to user space" within 1s
fix: 5/5 runs clean for 120s (~7.5k load/remove collisions each)

Without the delay, the fixed kernel ran 2x120s clean (~5k collisions each) with debug_locks still 1.

 block/blk-mq.c | 16 ++++++++--------
 block/blk.h    |  2 +-
 block/genhd.c  | 14 ++++++++------
 3 files changed, 17 insertions(+), 15 deletions(-)
block/blk-mq.c +8 -8
index 54ccfc43c27c2..11abb296c3bb4 100644
@@ -208,10 +208,8 @@ void blk_mq_freeze_queue_nomemsave(struct request_queue *q)
 }
 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_nomemsave);
 
-bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)
+void __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)
 {
-	bool unfreeze;
-
 	mutex_lock(&q->mq_freeze_lock);
 	if (force_atomic)
 		q->q_usage_counter.data->force_atomic = true;
@@ -221,16 +219,18 @@ bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)
 		percpu_ref_resurrect(&q->q_usage_counter);
 		wake_up_all(&q->mq_freeze_wq);
 	}
-	unfreeze = blk_unfreeze_check_owner(q);
+	/*
+	 * Release under mq_freeze_lock: once it is dropped, a new freezer may
+	 * overwrite the mq_freeze_{disk_dead,queue_dying} flags it checks.
+	 */
+	if (blk_unfreeze_check_owner(q))
+		blk_unfreeze_release_lock(q);
 	mutex_unlock(&q->mq_freeze_lock);
-
-	return unfreeze;
 }
 
 void blk_mq_unfreeze_queue_nomemrestore(struct request_queue *q)
 {
-	if (__blk_mq_unfreeze_queue(q, false))
-		blk_unfreeze_release_lock(q);
+	__blk_mq_unfreeze_queue(q, false);
 }
 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue_nomemrestore);
 
block/blk.h +1 -1
index 105fff157bfbd..13648d3483930 100644
@@ -55,7 +55,7 @@ const char *blk_status_to_tag(blk_status_t status);
 bool tag_to_blk_status(const char *tag, blk_status_t *status);
 enum req_op str_to_blk_op(const char *op);
 
-bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic);
+void __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic);
 bool blk_queue_start_drain(struct request_queue *q);
 bool __blk_freeze_queue_start(struct request_queue *q,
 			      struct task_struct *owner);
block/genhd.c +8 -6
index 69204d44adae1..2f478f320aa48 100644
@@ -772,13 +772,15 @@ static void __del_gendisk(struct gendisk *disk)
 	 * If the disk does not own the queue, allow using passthrough requests
 	 * again.  Else leave the queue frozen to fail all I/O.
 	 */
-	if (!test_bit(GD_OWNS_QUEUE, &disk->state))
+	if (!test_bit(GD_OWNS_QUEUE, &disk->state)) {
 		__blk_mq_unfreeze_queue(q, true);
-	else if (queue_is_mq(q))
-		blk_mq_exit_queue(q);
-
-	if (start_drain)
-		blk_unfreeze_release_lock(q);
+	} else {
+		if (queue_is_mq(q))
+			blk_mq_exit_queue(q);
+		/* still frozen, so the freeze flags are stable */
+		if (start_drain)
+			blk_unfreeze_release_lock(q);
+	}
 }
 
 static void disable_elv_switch(struct request_queue *q)
Signature · 1 line
2.43.0