Skip to contentSkip to conversation

zloop: protect state check with zloop_ctl_mutex locked in zloop_queue_rq

PATCH In review Yongpeng Yang 4 participants 5 messages Dec 12, 2025 – Dec 13, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 zloop: protect state check with zloop_ctl_mutex locked in zloop_queue_rq

From: Yongpeng Yang <[email protected]>

zlo->state is not an atomic variable, so checking 'zlo->state == Zlo_deleting' must be done under zloop_ctl_mutex.

---
 drivers/block/zloop.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/block/zloop.c +5 -2
index 77bd6081b244..0f29e419d8e9 100644
@@ -697,9 +697,12 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
 	struct zloop_cmd *cmd = blk_mq_rq_to_pdu(rq);
 	struct zloop_device *zlo = rq->q->queuedata;
 
-	if (zlo->state == Zlo_deleting)
+	mutex_lock(&zloop_ctl_mutex);
+	if (zlo->state == Zlo_deleting) {
+		mutex_unlock(&zloop_ctl_mutex);
 		return BLK_STS_IOERR;
-
+	}
+	mutex_unlock(&zloop_ctl_mutex);
 	/*
 	 * If we need to strongly order zone append operations, set the request
 	 * sector to the zone write pointer location now instead of when the
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 16 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

zlo->state is not an atomic variable, so checking 'zlo->state == Zlo_deleting' must be done under zloop_ctl_mutex.

---
 drivers/block/zloop.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/block/zloop.c +0 -0
index 77bd6081b244..0f29e419d8e9 100644
@@ -697,9 +697,12 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
 	struct zloop_cmd *cmd = blk_mq_rq_to_pdu(rq);

drivers/block/zloop.c static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx, · line 705

Quoted 10 lines
drivers/block/zloop.c +5 -2
 	struct zloop_device *zlo = rq->q->queuedata;
 
-	if (zlo->state == Zlo_deleting)
+	mutex_lock(&zloop_ctl_mutex);
+	if (zlo->state == Zlo_deleting) {
+		mutex_unlock(&zloop_ctl_mutex);
 		return BLK_STS_IOERR;
-
+	}
+	mutex_unlock(&zloop_ctl_mutex);

Probably a better idea to make the state checking atomic, and avoid the mutex in the queue_rq path.

Signature · 1 line
Jens Axboe
#2 lore raw
Quoted 19 lines · Jens Axboe
Quoted 18 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

zlo->state is not an atomic variable, so checking 'zlo->state == Zlo_deleting' must be done under zloop_ctl_mutex.

---
  drivers/block/zloop.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
drivers/block/zloop.c +0 -0
index 77bd6081b244..0f29e419d8e9 100644
@@ -697,9 +697,12 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
  	struct zloop_cmd *cmd = blk_mq_rq_to_pdu(rq);
  	struct zloop_device *zlo = rq->q->queuedata;
 
Quoted 12 lines

drivers/block/zloop.c static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx, · line 705

Quoted 8 lines
drivers/block/zloop.c +5 -2
-	if (zlo->state == Zlo_deleting)
+	mutex_lock(&zloop_ctl_mutex);
+	if (zlo->state == Zlo_deleting) {
+		mutex_unlock(&zloop_ctl_mutex);
  		return BLK_STS_IOERR;
-
+	}
+	mutex_unlock(&zloop_ctl_mutex);

Probably a better idea to make the state checking atomic, and avoid the mutex in the queue_rq path.

It is more reasonable, I'll fix this in v2.

Thanks
Yongpeng,

maintainer
#3 lore raw
Quoted 33 lines · Yongpeng Yang
Quoted 19 lines · Jens Axboe
Quoted 18 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

zlo->state is not an atomic variable, so checking 'zlo->state == Zlo_deleting' must be done under zloop_ctl_mutex.

---
  drivers/block/zloop.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
drivers/block/zloop.c +0 -0
index 77bd6081b244..0f29e419d8e9 100644
@@ -697,9 +697,12 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
  	struct zloop_cmd *cmd = blk_mq_rq_to_pdu(rq);
  	struct zloop_device *zlo = rq->q->queuedata;
 
Quoted 12 lines

drivers/block/zloop.c static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx, · line 705

Quoted 8 lines
drivers/block/zloop.c +5 -2
-	if (zlo->state == Zlo_deleting)
+	mutex_lock(&zloop_ctl_mutex);
+	if (zlo->state == Zlo_deleting) {
+		mutex_unlock(&zloop_ctl_mutex);
  		return BLK_STS_IOERR;
-
+	}
+	mutex_unlock(&zloop_ctl_mutex);

Probably a better idea to make the state checking atomic, and avoid the mutex in the queue_rq path.

Quoted 1 line

It is more reasonable, I'll fix this in v2.

Either what Jens proposed, or move the check to zloop_handle_cmd(), since that is run in work item context, we can block there.

Of note, is that the loop driver does the same thing in loop_queue_rq(): the device state is checked without the loop device mutex held. So a similar fix there may also be nice to have.

Quoted 3 lines

Thanks
Yongpeng,

Signature · 2 lines
Damien Le Moal
Western Digital Research
#4 lore raw
Quoted 34 lines · Damien Le Moal
Quoted 33 lines · Yongpeng Yang
Quoted 31 lines · Jens Axboe
Quoted 16 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

zlo->state is not an atomic variable, so checking 'zlo->state == Zlo_deleting' must be done under zloop_ctl_mutex.

---
   drivers/block/zloop.c | 7 +++++--
   1 file changed, 5 insertions(+), 2 deletions(-)
drivers/block/zloop.c +0 -0
index 77bd6081b244..0f29e419d8e9 100644
@@ -697,9 +697,12 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
   	struct zloop_cmd *cmd = blk_mq_rq_to_pdu(rq);

drivers/block/zloop.c static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx, · line 705

Quoted 10 lines
drivers/block/zloop.c +5 -2
   	struct zloop_device *zlo = rq->q->queuedata;
 
-	if (zlo->state == Zlo_deleting)
+	mutex_lock(&zloop_ctl_mutex);
+	if (zlo->state == Zlo_deleting) {
+		mutex_unlock(&zloop_ctl_mutex);
   		return BLK_STS_IOERR;
-
+	}
+	mutex_unlock(&zloop_ctl_mutex);

Probably a better idea to make the state checking atomic, and avoid the mutex in the queue_rq path.

Quoted 4 lines
Quoted 1 line

It is more reasonable, I'll fix this in v2.

Either what Jens proposed, or move the check to zloop_handle_cmd(), since that is run in work item context, we can block there.

Yes, both approaches are fine. The zloop driver has only 3 states, using an atomic variable seems more concise.

Quoted 4 lines

Of note, is that the loop driver does the same thing in loop_queue_rq(): the device state is checked without the loop device mutex held. So a similar fix there may also be nice to have.

Yes, the loop driver has a similar issue, and I will attempt to fix it as well.

Thanks
Yongpeng,

Quoted 6 lines
Quoted 4 lines

Thanks
Yongpeng,