Skip to contentSkip to conversation

atomic queue limits updates for nbd

PATCH Applied Christoph Hellwig 2 participants 5 messages Feb 29, 2024 – Mar 1, 2024

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

Hi Josef, hi Jens,

this series converts nbd to the atomic queue limits API.

It makes nbd/001 a lot more likely to fail due to widening an existing race window. I've send a blktests patch titled "nbd/001: wait for the device node to show up before running parted" a short while ago to fix the race in the test case.

#1 lore raw

PATCH 1/3 nbd: don't clear discard_sectors in nbd_config_put

nbd_config_put currently clears discard_sectors when unusing a device. This is pretty odd behavior and different from the sector size configuration which is simply left in places and then reconfigured when nbd_set_size is as part of configuring the device. Change nbd_set_size to clear discard_sectors if discard is not supported so that all the queue limits changes are handled in one place.

---
 drivers/block/nbd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/block/nbd.c +2 -1
index 9ee9587375fac3..384750d5259fb8 100644
@@ -336,6 +336,8 @@ static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
 
 	if (nbd->config->flags & NBD_FLAG_SEND_TRIM)
 		blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
+	else
+		blk_queue_max_discard_sectors(nbd->disk->queue, 0);
 	blk_queue_logical_block_size(nbd->disk->queue, blksize);
 	blk_queue_physical_block_size(nbd->disk->queue, blksize);
 
@@ -1351,7 +1353,6 @@ static void nbd_config_put(struct nbd_device *nbd)
 		nbd->config = NULL;
 
 		nbd->tag_set.timeout = 0;
-		blk_queue_max_discard_sectors(nbd->disk->queue, 0);
 
 		mutex_unlock(&nbd->config_lock);
 		nbd_put(nbd);
Signature · 1 line
2.39.2
maintainer Applied
#2 lore raw

Re: [PATCH 1/3] nbd: don't clear discard_sectors in nbd_config_put

Quoted 8 lines · Christoph Hellwig

nbd_config_put currently clears discard_sectors when unusing a device. This is pretty odd behavior and different from the sector size configuration which is simply left in places and then reconfigured when nbd_set_size is as part of configuring the device. Change nbd_set_size to clear discard_sectors if discard is not supported so that all the queue limits changes are handled in one place.

[...]

Applied, thanks!

[1/3] nbd: don't clear discard_sectors in nbd_config_put

      commit: 7ea201f2cc1da999b9a0a23ea20b64eb2c4719a9
[2/3] nbd: freeze the queue for queue limits updates
      commit: 242a49e5c8784e93a99e4dc4277b28a8ba85eac5
[3/3] nbd: use the atomic queue limits API in nbd_set_size
      commit: 268283244c0f018dec8bf4a9c69ce50684561f46

Best regards,

Signature · 1 line
Jens Axboe
#3 lore raw

PATCH 2/3 nbd: freeze the queue for queue limits updates

In reply to Christoph Hellwig

nbd currently updates the logical and physical block sizes as well as the discard_sectors on a live queue. Freeze the queue first to make sure there are not commands in flight that can see torn or inconsistent limits.

---
 drivers/block/nbd.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/block/nbd.c +13 -1
index 384750d5259fb8..22ee0ed9aa6db0 100644
@@ -316,7 +316,7 @@ static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
 	nsock->sent = 0;
 }
 
-static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
+static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
 		loff_t blksize)
 {
 	if (!blksize)
@@ -348,6 +348,18 @@ static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
 	return 0;
 }
 
+static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
+		loff_t blksize)
+{
+	int error;
+
+	blk_mq_freeze_queue(nbd->disk->queue);
+	error = __nbd_set_size(nbd, bytesize, blksize);
+	blk_mq_unfreeze_queue(nbd->disk->queue);
+
+	return error;
+}
+
 static void nbd_complete_rq(struct request *req)
 {
 	struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
Signature · 1 line
2.39.2
#4 lore raw

PATCH 3/3 nbd: use the atomic queue limits API in nbd_set_size

In reply to Christoph Hellwig

Use queue_limits_start_update / queue_limits_commit_update to update all the limits in one go and with proper sanity checking.

---
 drivers/block/nbd.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/block/nbd.c +11 -4
index 22ee0ed9aa6db0..9d4ec9273bf954 100644
@@ -319,6 +319,9 @@ static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
 static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
 		loff_t blksize)
 {
+	struct queue_limits lim;
+	int error;
+
 	if (!blksize)
 		blksize = 1u << NBD_DEF_BLKSIZE_BITS;
 
@@ -334,12 +337,16 @@ static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
 	if (!nbd->pid)
 		return 0;
 
+	lim = queue_limits_start_update(nbd->disk->queue);
 	if (nbd->config->flags & NBD_FLAG_SEND_TRIM)
-		blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
+		lim.max_hw_discard_sectors = UINT_MAX;
 	else
-		blk_queue_max_discard_sectors(nbd->disk->queue, 0);
-	blk_queue_logical_block_size(nbd->disk->queue, blksize);
-	blk_queue_physical_block_size(nbd->disk->queue, blksize);
+		lim.max_hw_discard_sectors = 0;
+	lim.logical_block_size = blksize;
+	lim.physical_block_size = blksize;
+	error = queue_limits_commit_update(nbd->disk->queue, &lim);
+	if (error)
+		return error;
 
 	if (max_part)
 		set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
Signature · 1 line
2.39.2