Skip to contentSkip to conversation

RFC: nvme_update_ns_info rework

PATCH In review Christoph Hellwig 3 participants 11 messages Aug 29, 2024 – Sep 2, 2024 NVMe

lore
Share & tools
lore

Christoph Hellwig

author maintainer
#0 lore raw

Hi all,

this series implements my suggest prep steps for the multipath queue freeing speedups from Martin. Very lightly tested and written while drinking my vacation morning coffee, so handle with care.

author maintainer
#1 lore raw

PATCH 1/3 nvme: split out a nvme_update_ns_info_mpath helper

Add a separate helper to update the multipath disk information to declutter nvme_update_ns_info.

---
 drivers/nvme/host/core.c | 84 +++++++++++++++++++++-------------------
 1 file changed, 44 insertions(+), 40 deletions(-)
drivers/nvme/host/core.c +44 -40
index 73968130f50926..51cb3b001b3757 100644
@@ -2095,6 +2095,48 @@ static void nvme_set_chunk_sectors(struct nvme_ns *ns, struct nvme_id_ns *id,
 	lim->chunk_sectors = iob;
 }
 
+/*
+ * The queue_limits structure mixes values that are the hardware limitations for
+ * bio splitting with what is the device configuration.
+ *
+ * For NVMe the device configuration can change after e.g. a Format command, and
+ * we really want to pick up the new format value here.  But we must still stack
+ * the queue limits to the least common denominator for multipathing to split
+ * the bios properly.
+ *
+ * To work around this, we explicitly set the device configuration to the values
+ * that we just queried, but stack the splitting limits in to make sure we still
+ * obey possibly lower limitations of other controllers.
+ */
+static int nvme_update_ns_info_mpath(struct nvme_ns *ns,
+		struct nvme_ns_info *info, bool unsupported)
+{
+	struct queue_limits *ns_lim = &ns->disk->queue->limits;
+	struct gendisk *disk = ns->head->disk;
+	struct queue_limits lim;
+	int ret;
+
+	blk_mq_freeze_queue(disk->queue);
+	lim = queue_limits_start_update(disk->queue);
+	lim.logical_block_size = ns_lim->logical_block_size;
+	lim.physical_block_size = ns_lim->physical_block_size;
+	lim.io_min = ns_lim->io_min;
+	lim.io_opt = ns_lim->io_opt;
+	queue_limits_stack_bdev(&lim, ns->disk->part0, 0, disk->disk_name);
+	if (unsupported)
+		disk->flags |= GENHD_FL_HIDDEN;
+	else
+		nvme_init_integrity(disk, ns->head, &lim);
+	ret = queue_limits_commit_update(disk->queue, &lim);
+
+	set_capacity_and_notify(disk, get_capacity(ns->disk));
+	set_disk_ro(disk, nvme_ns_is_readonly(ns, info));
+	nvme_mpath_revalidate_paths(ns);
+	blk_mq_unfreeze_queue(disk->queue);
+
+	return ret;
+}
+
 static int nvme_update_ns_info_generic(struct nvme_ns *ns,
 		struct nvme_ns_info *info)
 {
@@ -2251,46 +2293,8 @@ static int nvme_update_ns_info(struct nvme_ns *ns, struct nvme_ns_info *info)
 		ret = 0;
 	}
 
-	if (!ret && nvme_ns_head_multipath(ns->head)) {
-		struct queue_limits *ns_lim = &ns->disk->queue->limits;
-		struct queue_limits lim;
-
-		blk_mq_freeze_queue(ns->head->disk->queue);
-		/*
-		 * queue_limits mixes values that are the hardware limitations
-		 * for bio splitting with what is the device configuration.
-		 *
-		 * For NVMe the device configuration can change after e.g. a
-		 * Format command, and we really want to pick up the new format
-		 * value here.  But we must still stack the queue limits to the
-		 * least common denominator for multipathing to split the bios
-		 * properly.
-		 *
-		 * To work around this, we explicitly set the device
-		 * configuration to those that we just queried, but only stack
-		 * the splitting limits in to make sure we still obey possibly
-		 * lower limitations of other controllers.
-		 */
-		lim = queue_limits_start_update(ns->head->disk->queue);
-		lim.logical_block_size = ns_lim->logical_block_size;
-		lim.physical_block_size = ns_lim->physical_block_size;
-		lim.io_min = ns_lim->io_min;
-		lim.io_opt = ns_lim->io_opt;
-		queue_limits_stack_bdev(&lim, ns->disk->part0, 0,
-					ns->head->disk->disk_name);
-		if (unsupported)
-			ns->head->disk->flags |= GENHD_FL_HIDDEN;
-		else
-			nvme_init_integrity(ns->head->disk, ns->head, &lim);
-		ret = queue_limits_commit_update(ns->head->disk->queue, &lim);
-
-		set_capacity_and_notify(ns->head->disk, get_capacity(ns->disk));
-		set_disk_ro(ns->head->disk, nvme_ns_is_readonly(ns, info));
-		nvme_mpath_revalidate_paths(ns);
-
-		blk_mq_unfreeze_queue(ns->head->disk->queue);
-	}
-
+	if (nvme_ns_head_multipath(ns->head) && !ret)
+		ret = nvme_update_ns_info_mpath(ns, info, unsupported);
 	return ret;
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] nvme: split out a nvme_update_ns_info_mpath helper

Quoted 8 lines · Christoph Hellwig

Add a separate helper to update the multipath disk information to declutter nvme_update_ns_info.

---
  drivers/nvme/host/core.c | 84 +++++++++++++++++++++-------------------
  1 file changed, 44 insertions(+), 40 deletions(-)

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author maintainer
#3 lore raw

PATCH 2/3 nvme: move hidden device and multipath handling out of nvme_update_ns_info

In reply to Christoph Hellwig

Turn nvme_update_ns_info into a pure multiplexer and move the real work into nvme_update_ns_info_generic and nvme_update_ns_block, which allows to set the flags more coherently while the queue is still frozen.

---
 drivers/nvme/host/core.c | 55 ++++++++++++++++++----------------------
 1 file changed, 25 insertions(+), 30 deletions(-)
drivers/nvme/host/core.c +25 -30
index 51cb3b001b3757..81d2a09dd00f30 100644
@@ -2147,12 +2147,18 @@ static int nvme_update_ns_info_generic(struct nvme_ns *ns,
 	lim = queue_limits_start_update(ns->disk->queue);
 	nvme_set_ctrl_limits(ns->ctrl, &lim);
 	ret = queue_limits_commit_update(ns->disk->queue, &lim);
-	set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
-	blk_mq_unfreeze_queue(ns->disk->queue);
+	if (ret)
+		goto out_unfreeze;
 
 	/* Hide the block-interface for these devices */
-	if (!ret)
-		ret = -ENODEV;
+	ns->disk->flags |= GENHD_FL_HIDDEN;
+	set_bit(NVME_NS_READY, &ns->flags);
+	set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
+
+out_unfreeze:
+	blk_mq_unfreeze_queue(ns->disk->queue);
+	if (nvme_ns_head_multipath(ns->head) && !ret)
+		ret = nvme_update_ns_info_mpath(ns, info, true);
 	return ret;
 }
 
@@ -2163,6 +2169,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
 	struct nvme_id_ns_nvm *nvm = NULL;
 	struct nvme_zone_info zi = {};
 	struct nvme_id_ns *id;
+	bool unsupported = false;
 	sector_t capacity;
 	unsigned lbaf;
 	int ret;
@@ -2188,6 +2195,11 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
 	if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) &&
 	    ns->head->ids.csi == NVME_CSI_ZNS) {
 		ret = nvme_query_zone_info(ns, lbaf, &zi);
+		if (ret == -ENODEV) {
+			unsupported = true;
+			ns->disk->flags |= GENHD_FL_HIDDEN;
+			goto done;
+		}
 		if (ret < 0)
 			goto out;
 	}
@@ -2238,6 +2250,8 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
 	 */
 	if ((id->dlfeat & 0x7) == 0x1 && (id->dlfeat & (1 << 3)))
 		ns->head->features |= NVME_NS_DEAC;
+
+done:
 	set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
 	set_bit(NVME_NS_READY, &ns->flags);
 	blk_mq_unfreeze_queue(ns->disk->queue);
@@ -2252,50 +2266,31 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
 out:
 	kfree(nvm);
 	kfree(id);
+
+	if (nvme_ns_head_multipath(ns->head) && !ret)
+		ret = nvme_update_ns_info_mpath(ns, info, unsupported);
 	return ret;
 }
 
 static int nvme_update_ns_info(struct nvme_ns *ns, struct nvme_ns_info *info)
 {
-	bool unsupported = false;
-	int ret;
-
 	switch (info->ids.csi) {
 	case NVME_CSI_ZNS:
 		if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
 			dev_info(ns->ctrl->device,
 	"block device for nsid %u not supported without CONFIG_BLK_DEV_ZONED\n",
 				info->nsid);
-			ret = nvme_update_ns_info_generic(ns, info);
-			break;
+			return nvme_update_ns_info_generic(ns, info);
 		}
-		ret = nvme_update_ns_info_block(ns, info);
-		break;
+		return nvme_update_ns_info_block(ns, info);
 	case NVME_CSI_NVM:
-		ret = nvme_update_ns_info_block(ns, info);
-		break;
+		return nvme_update_ns_info_block(ns, info);
 	default:
 		dev_info(ns->ctrl->device,
 			"block device for nsid %u not supported (csi %u)\n",
 			info->nsid, info->ids.csi);
-		ret = nvme_update_ns_info_generic(ns, info);
-		break;
+		return nvme_update_ns_info_generic(ns, info);
 	}
-
-	/*
-	 * If probing fails due an unsupported feature, hide the block device,
-	 * but still allow other access.
-	 */
-	if (ret == -ENODEV) {
-		ns->disk->flags |= GENHD_FL_HIDDEN;
-		set_bit(NVME_NS_READY, &ns->flags);
-		unsupported = true;
-		ret = 0;
-	}
-
-	if (nvme_ns_head_multipath(ns->head) && !ret)
-		ret = nvme_update_ns_info_mpath(ns, info, unsupported);
-	return ret;
 }
 
 int nvme_ns_get_unique_id(struct nvme_ns *ns, u8 id[16],
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 2/3] nvme: move hidden device and multipath handling out of nvme_update_ns_info

Quoted 54 lines · Christoph Hellwig

Turn nvme_update_ns_info into a pure multiplexer and move the real work
into nvme_update_ns_info_generic and nvme_update_ns_block, which allows
to set the flags more coherently while the queue is still frozen.

---
 drivers/nvme/host/core.c | 55 ++++++++++++++++++-------------------- --

 1 file changed, 25 insertions(+), 30 deletions(-)
drivers/nvme/host/core.c +2 -2
index 51cb3b001b3757..81d2a09dd00f30 100644
@@ -2147,12 +2147,18 @@ static int nvme_update_ns_info_generic(struct
 nvme_ns *ns,
  	lim = queue_limits_start_update(ns->disk->queue);
  	nvme_set_ctrl_limits(ns->ctrl, &lim);
  	ret = queue_limits_commit_update(ns->disk->queue, &lim);
-	set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
-	blk_mq_unfreeze_queue(ns->disk->queue);
+	if (ret)
+		goto out_unfreeze;


  /* Hide the block-interface for these devices */

drivers/nvme/host/core.c +8 -2
-	if (!ret)
-		ret = -ENODEV;
+	ns->disk->flags |= GENHD_FL_HIDDEN;
+	set_bit(NVME_NS_READY, &ns->flags);
+	set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
+
+out_unfreeze:
+	blk_mq_unfreeze_queue(ns->disk->queue);
+	if (nvme_ns_head_multipath(ns->head) && !ret)
+		ret = nvme_update_ns_info_mpath(ns, info, true);

  return ret;
 }

drivers/nvme/host/core.c +1 -0
@@ -2163,6 +2169,7 @@ static int nvme_update_ns_info_block(struct
 nvme_ns *ns,
  	struct nvme_id_ns_nvm *nvm = NULL;
  	struct nvme_zone_info zi = {};
  	struct nvme_id_ns *id;
+	bool unsupported = false;
  	sector_t capacity;
  	unsigned lbaf;

  int ret;

drivers/nvme/host/core.c +0 -0
@@ -2188,6 +2195,11 @@ static int nvme_update_ns_info_block(struct
 nvme_ns *ns,

  if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) && ns->head->ids.csi == NVME_CSI_ZNS) { ret = nvme_query_zone_info(ns, lbaf, &zi);

drivers/nvme/host/core.c static int nvme_update_ns_info_block(struct nvme_ns *ns, · line 2201

Quoted 4 lines
drivers/nvme/host/core.c +4 -0
+		if (ret == -ENODEV) {
+			unsupported = true;
+			ns->disk->flags |= GENHD_FL_HIDDEN;
+			goto done;

This looks wrong to me. We call blk_mq_unfreeze_queue() after the "done" label, although the queue has not been frozen yet.

Quoted 64 lines
drivers/nvme/host/core.c +1 -0
+		}

  if (ret < 0)
  goto out;
  }

drivers/nvme/host/core.c +2 -0
@@ -2238,6 +2250,8 @@ static int nvme_update_ns_info_block(struct
 nvme_ns *ns,
  	 */
  	if ((id->dlfeat & 0x7) == 0x1 && (id->dlfeat & (1 << 3)))
  		ns->head->features |= NVME_NS_DEAC;
+
+done:
  	set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
  	set_bit(NVME_NS_READY, &ns->flags);

  blk_mq_unfreeze_queue(ns->disk->queue);

drivers/nvme/host/core.c +0 -0
@@ -2252,50 +2266,31 @@ static int nvme_update_ns_info_block(struct
 nvme_ns *ns,

 out:
  kfree(nvm);
  kfree(id);

drivers/nvme/host/core.c +3 -0
+
+	if (nvme_ns_head_multipath(ns->head) && !ret)
+		ret = nvme_update_ns_info_mpath(ns, info,

unsupported);
  return ret;
 }

 static int nvme_update_ns_info(struct nvme_ns *ns, struct nvme_ns_info *info)
 {

drivers/nvme/host/core.c +0 -3
-	bool unsupported = false;
-	int ret;
-

  switch (info->ids.csi) {
  case NVME_CSI_ZNS:
  if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED)) { dev_info(ns->ctrl->device, "block device for nsid %u not supported without CONFIG_BLK_DEV_ZONED\n",
  info->nsid);

drivers/nvme/host/core.c +1 -2
-			ret = nvme_update_ns_info_generic(ns, info);
-			break;
+			return nvme_update_ns_info_generic(ns,

info);
  }

drivers/nvme/host/core.c +1 -2
-		ret = nvme_update_ns_info_block(ns, info);
-		break;
+		return nvme_update_ns_info_block(ns, info);

  case NVME_CSI_NVM:

drivers/nvme/host/core.c +1 -2
-		ret = nvme_update_ns_info_block(ns, info);
-		break;
+		return nvme_update_ns_info_block(ns, info);

  default:
  dev_info(ns->ctrl->device,
  "block device for nsid %u not supported (csi %u)\n",
  info->nsid, info->ids.csi);

drivers/nvme/host/core.c +1 -2
-		ret = nvme_update_ns_info_generic(ns, info);
-		break;
+		return nvme_update_ns_info_generic(ns, info);

  }

drivers/nvme/host/core.c +0 -3
-
-	/*
-	 * If probing fails due an unsupported feature, hide the

block device,

drivers/nvme/host/core.c static int nvme_update_ns_info_block(struct nvme_ns *ns, · line 2294

Quoted 8 lines
drivers/nvme/host/core.c +0 -8
-	 * but still allow other access.
-	 */
-	if (ret == -ENODEV) {
-		ns->disk->flags |= GENHD_FL_HIDDEN;
-		set_bit(NVME_NS_READY, &ns->flags);
-		unsupported = true;
-		ret = 0;
-	}

Are we certain that the two cases above (nvme_update_ns_info_generic() and failure of nvme_query_zone_info()) are the only cases in which ret == -ENODEV would have been true here?

Regards
Martin

Quoted 8 lines
drivers/nvme/host/core.c +0 -3
-
-	if (nvme_ns_head_multipath(ns->head) && !ret)
-		ret = nvme_update_ns_info_mpath(ns, info,

unsupported);

drivers/nvme/host/core.c +0 -1
-	return ret;

 }

 int nvme_ns_get_unique_id(struct nvme_ns *ns, u8 id[16],

Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] nvme: move hidden device and multipath handling out of nvme_update_ns_info

In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig

Turn nvme_update_ns_info into a pure multiplexer and move the real work into nvme_update_ns_info_generic and nvme_update_ns_block, which allows to set the flags more coherently while the queue is still frozen.

---
  drivers/nvme/host/core.c | 55 ++++++++++++++++++----------------------
  1 file changed, 25 insertions(+), 30 deletions(-)

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author maintainer
#6 lore raw

PATCH 3/3 nvme: freeze both the ns and multipath queues whe updating queue limits

In reply to Christoph Hellwig

We really should not have any I/O outstanding while updating the queue limits, as that could introduce inconsistencies between the multipath and underlying nodes. So always freeze the multipath node first and then the underlying one, matching the order in nvme_passthru_start.

---
 drivers/nvme/host/core.c | 41 ++++++++++++++++++++++++++++------------
 1 file changed, 29 insertions(+), 12 deletions(-)
drivers/nvme/host/core.c +29 -12
index 81d2a09dd00f30..0f01a0bf976fd3 100644
@@ -2095,6 +2095,24 @@ static void nvme_set_chunk_sectors(struct nvme_ns *ns, struct nvme_id_ns *id,
 	lim->chunk_sectors = iob;
 }
 
+/*
+ * Freeze all I/O for a namespace, including clearing out anything on the
+ * multipath node.
+ */
+static void nvme_ns_freeze(struct nvme_ns *ns)
+{
+	if (nvme_ns_head_multipath(ns->head))
+		blk_mq_freeze_queue(ns->head->disk->queue);
+	blk_mq_freeze_queue(ns->disk->queue);
+}
+
+static void nvme_ns_unfreeze(struct nvme_ns *ns)
+{
+	blk_mq_unfreeze_queue(ns->disk->queue);
+	if (nvme_ns_head_multipath(ns->head))
+		blk_mq_unfreeze_queue(ns->head->disk->queue);
+}
+
 /*
  * The queue_limits structure mixes values that are the hardware limitations for
  * bio splitting with what is the device configuration.
@@ -2116,7 +2134,8 @@ static int nvme_update_ns_info_mpath(struct nvme_ns *ns,
 	struct queue_limits lim;
 	int ret;
 
-	blk_mq_freeze_queue(disk->queue);
+	WARN_ON_ONCE(disk->queue->mq_freeze_depth < 1);
+
 	lim = queue_limits_start_update(disk->queue);
 	lim.logical_block_size = ns_lim->logical_block_size;
 	lim.physical_block_size = ns_lim->physical_block_size;
@@ -2132,7 +2151,6 @@ static int nvme_update_ns_info_mpath(struct nvme_ns *ns,
 	set_capacity_and_notify(disk, get_capacity(ns->disk));
 	set_disk_ro(disk, nvme_ns_is_readonly(ns, info));
 	nvme_mpath_revalidate_paths(ns);
-	blk_mq_unfreeze_queue(disk->queue);
 
 	return ret;
 }
@@ -2143,7 +2161,7 @@ static int nvme_update_ns_info_generic(struct nvme_ns *ns,
 	struct queue_limits lim;
 	int ret;
 
-	blk_mq_freeze_queue(ns->disk->queue);
+	nvme_ns_freeze(ns);
 	lim = queue_limits_start_update(ns->disk->queue);
 	nvme_set_ctrl_limits(ns->ctrl, &lim);
 	ret = queue_limits_commit_update(ns->disk->queue, &lim);
@@ -2154,11 +2172,11 @@ static int nvme_update_ns_info_generic(struct nvme_ns *ns,
 	ns->disk->flags |= GENHD_FL_HIDDEN;
 	set_bit(NVME_NS_READY, &ns->flags);
 	set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
+	if (nvme_ns_head_multipath(ns->head))
+		ret = nvme_update_ns_info_mpath(ns, info, true);
 
 out_unfreeze:
-	blk_mq_unfreeze_queue(ns->disk->queue);
-	if (nvme_ns_head_multipath(ns->head) && !ret)
-		ret = nvme_update_ns_info_mpath(ns, info, true);
+	nvme_ns_unfreeze(ns);
 	return ret;
 }
 
@@ -2204,7 +2222,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
 			goto out;
 	}
 
-	blk_mq_freeze_queue(ns->disk->queue);
+	nvme_ns_freeze(ns);
 	ns->head->lba_shift = id->lbaf[lbaf].ds;
 	ns->head->nuse = le64_to_cpu(id->nuse);
 	capacity = nvme_lba_to_sect(ns->head, le64_to_cpu(id->nsze));
@@ -2236,7 +2254,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
 
 	ret = queue_limits_commit_update(ns->disk->queue, &lim);
 	if (ret) {
-		blk_mq_unfreeze_queue(ns->disk->queue);
+		nvme_ns_unfreeze(ns);
 		goto out;
 	}
 
@@ -2254,7 +2272,9 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
 done:
 	set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
 	set_bit(NVME_NS_READY, &ns->flags);
-	blk_mq_unfreeze_queue(ns->disk->queue);
+	if (nvme_ns_head_multipath(ns->head))
+		ret = nvme_update_ns_info_mpath(ns, info, unsupported);
+	nvme_ns_unfreeze(ns);
 
 	if (blk_queue_is_zoned(ns->queue)) {
 		ret = blk_revalidate_disk_zones(ns->disk);
@@ -2266,9 +2286,6 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
 out:
 	kfree(nvm);
 	kfree(id);
-
-	if (nvme_ns_head_multipath(ns->head) && !ret)
-		ret = nvme_update_ns_info_mpath(ns, info, unsupported);
 	return ret;
 }
 
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH 3/3] nvme: freeze both the ns and multipath queues whe updating queue limits

Quoted 100 lines · Christoph Hellwig

We really should not have any I/O outstanding while updating the queue limits, as that could introduce inconsistencies between the multipath and underlying nodes. So always freeze the multipath node first and then the underlying one, matching the order in nvme_passthru_start.

---
  drivers/nvme/host/core.c | 41 ++++++++++++++++++++++++++++------------
  1 file changed, 29 insertions(+), 12 deletions(-)
drivers/nvme/host/core.c +26 -8
index 81d2a09dd00f30..0f01a0bf976fd3 100644
@@ -2095,6 +2095,24 @@ static void nvme_set_chunk_sectors(struct nvme_ns *ns, struct nvme_id_ns *id,
  	lim->chunk_sectors = iob;
  }
 
+/*
+ * Freeze all I/O for a namespace, including clearing out anything on the
+ * multipath node.
+ */
+static void nvme_ns_freeze(struct nvme_ns *ns)
+{
+	if (nvme_ns_head_multipath(ns->head))
+		blk_mq_freeze_queue(ns->head->disk->queue);
+	blk_mq_freeze_queue(ns->disk->queue);
+}
+
+static void nvme_ns_unfreeze(struct nvme_ns *ns)
+{
+	blk_mq_unfreeze_queue(ns->disk->queue);
+	if (nvme_ns_head_multipath(ns->head))
+		blk_mq_unfreeze_queue(ns->head->disk->queue);
+}
+
  /*
   * The queue_limits structure mixes values that are the hardware limitations for
   * bio splitting with what is the device configuration.
@@ -2116,7 +2134,8 @@ static int nvme_update_ns_info_mpath(struct nvme_ns *ns,
  	struct queue_limits lim;
  	int ret;
 
-	blk_mq_freeze_queue(disk->queue);
+	WARN_ON_ONCE(disk->queue->mq_freeze_depth < 1);
+
  	lim = queue_limits_start_update(disk->queue);
  	lim.logical_block_size = ns_lim->logical_block_size;
  	lim.physical_block_size = ns_lim->physical_block_size;
@@ -2132,7 +2151,6 @@ static int nvme_update_ns_info_mpath(struct nvme_ns *ns,
  	set_capacity_and_notify(disk, get_capacity(ns->disk));
  	set_disk_ro(disk, nvme_ns_is_readonly(ns, info));
  	nvme_mpath_revalidate_paths(ns);
-	blk_mq_unfreeze_queue(disk->queue);
 
  	return ret;
  }
@@ -2143,7 +2161,7 @@ static int nvme_update_ns_info_generic(struct nvme_ns *ns,
  	struct queue_limits lim;
  	int ret;
 
-	blk_mq_freeze_queue(ns->disk->queue);
+	nvme_ns_freeze(ns);
  	lim = queue_limits_start_update(ns->disk->queue);
  	nvme_set_ctrl_limits(ns->ctrl, &lim);
  	ret = queue_limits_commit_update(ns->disk->queue, &lim);
@@ -2154,11 +2172,11 @@ static int nvme_update_ns_info_generic(struct nvme_ns *ns,
  	ns->disk->flags |= GENHD_FL_HIDDEN;
  	set_bit(NVME_NS_READY, &ns->flags);
  	set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
+	if (nvme_ns_head_multipath(ns->head))
+		ret = nvme_update_ns_info_mpath(ns, info, true);
 
  out_unfreeze:
-	blk_mq_unfreeze_queue(ns->disk->queue);
-	if (nvme_ns_head_multipath(ns->head) && !ret)
-		ret = nvme_update_ns_info_mpath(ns, info, true);
+	nvme_ns_unfreeze(ns);
  	return ret;
  }
 
@@ -2204,7 +2222,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
  			goto out;
  	}
 
-	blk_mq_freeze_queue(ns->disk->queue);
+	nvme_ns_freeze(ns);
  	ns->head->lba_shift = id->lbaf[lbaf].ds;
  	ns->head->nuse = le64_to_cpu(id->nuse);
  	capacity = nvme_lba_to_sect(ns->head, le64_to_cpu(id->nsze));
@@ -2236,7 +2254,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
 
  	ret = queue_limits_commit_update(ns->disk->queue, &lim);
  	if (ret) {
-		blk_mq_unfreeze_queue(ns->disk->queue);
+		nvme_ns_unfreeze(ns);
  		goto out;
  	}
 

drivers/nvme/host/core.c static int nvme_update_ns_info_block(struct nvme_ns *ns, · line 2280

Quoted 10 lines
drivers/nvme/host/core.c +3 -1
@@ -2254,7 +2272,9 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
  done:
  	set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
  	set_bit(NVME_NS_READY, &ns->flags);
-	blk_mq_unfreeze_queue(ns->disk->queue);
+	if (nvme_ns_head_multipath(ns->head))
+		ret = nvme_update_ns_info_mpath(ns, info, unsupported);
+	nvme_ns_unfreeze(ns);
 
  	if (blk_queue_is_zoned(ns->queue)) {
  		ret = blk_revalidate_disk_zones(ns->disk);

While we're shuffling things around; doesn't the mean that we unfreeze the queue (and allow I/O) before we've validated all zones? Wouldn't it be better if we validate zoned before unfreezing queues?

Otherwise it looks good; I'll give it a spin on my map/unmap testcases.

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), GF: Ivo Totev, Andrew McDonald,
Werner Knoblich
#8 lore raw

Re: [PATCH 3/3] nvme: freeze both the ns and multipath queues whe updating queue limits

Quoted 15 lines · Hannes Reinecke
Quoted 14 lines · Christoph Hellwig

We really should not have any I/O outstanding while updating the queue limits, as that could introduce inconsistencies between the multipath and underlying nodes.  So always freeze the multipath node first and then the underlying one, matching the order in nvme_passthru_start.

---
  drivers/nvme/host/core.c | 41 ++++++++++++++++++++++++++++------------

  1 file changed, 29 insertions(+), 12 deletions(-)
drivers/nvme/host/core.c +0 -0
index 81d2a09dd00f30..0f01a0bf976fd3 100644

[ .. ]

Quoted 15 lines
Quoted 11 lines
drivers/nvme/host/core.c +0 -0
@@ -2254,7 +2272,9 @@ static int nvme_update_ns_info_block(struct 
 nvme_ns *ns,

  done:
      set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info)); set_bit(NVME_NS_READY, &ns->flags);
-    blk_mq_unfreeze_queue(ns->disk->queue);
+    if (nvme_ns_head_multipath(ns->head))
+        ret = nvme_update_ns_info_mpath(ns, info, unsupported); +    nvme_ns_unfreeze(ns);
      if (blk_queue_is_zoned(ns->queue)) {
          ret = blk_revalidate_disk_zones(ns->disk);

While we're shuffling things around; doesn't the mean that we unfreeze the queue (and allow I/O) before we've validated all zones? Wouldn't it be better if we validate zoned before unfreezing queues?

Quoted 2 lines

Otherwise it looks good; I'll give it a spin on my map/unmap testcases.

Hmm. Turns out we're not quite there yet.
We call 'set_capacity_and_notify()' unconditionally a few lines up, before setting the namespace to 'READY'. And then we're doing another
call to 'set_capacity_and_notify()' (from nvme_update_ns_info_mpath()).
In my map/unmap tests I've seen a stall with udev, where the initial
scanning process it stuck in device_add_disk() trying to read the partition table (and waiting for the uptodate bit to clear), and the udev process closing the bdev and waiting on 'truncate_filemap_range').

With this patch:

drivers/nvme/host/core.c +2 -2
index fdd67a55a477..8858e5bf49de 100644
@@ -2258,8 +2258,6 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
                goto out;
        }
 
-       set_capacity_and_notify(ns->disk, capacity);
-
        /*
         * Only set the DEAC bit if the device guarantees that reads from
         * deallocated data return zeroes.  While the DEAC bit does not
@@ -2274,6 +2272,8 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
        set_bit(NVME_NS_READY, &ns->flags);
        if (nvme_ns_head_multipath(ns->head))
                ret = nvme_update_ns_info_mpath(ns, info, unsupported);
+       else
+               set_capacity_and_notify(ns->disk, capacity);
        nvme_ns_unfreeze(ns);
 
        if (blk_queue_is_zoned(ns->queue)) {

on top of your patchset I no longer see the problem.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#9 lore raw

Re: [PATCH 3/3] nvme: freeze both the ns and multipath queues whe updating queue limits

Quoted 42 lines · Hannes Reinecke
Quoted 19 lines · Hannes Reinecke
Quoted 18 lines · Christoph Hellwig

We really should not have any I/O outstanding while updating the queue
limits, as that could introduce inconsistencies between the multipath
and underlying nodes.  So always freeze the multipath node first and then
the underlying one, matching the order in nvme_passthru_start.

---
  drivers/nvme/host/core.c | 41 ++++++++++++++++++++++++++++-----
-------

  1 file changed, 29 insertions(+), 12 deletions(-)
drivers/nvme/host/core.c +0 -0
index 81d2a09dd00f30..0f01a0bf976fd3 100644

[  .. ]

Quoted 18 lines
Quoted 11 lines
drivers/nvme/host/core.c +0 -0
@@ -2254,7 +2272,9 @@ static int nvme_update_ns_info_block(struct
 nvme_ns *ns,

  done:
      set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info)); set_bit(NVME_NS_READY, &ns->flags);
-    blk_mq_unfreeze_queue(ns->disk->queue);
+    if (nvme_ns_head_multipath(ns->head))
+        ret = nvme_update_ns_info_mpath(ns, info, unsupported); +    nvme_ns_unfreeze(ns);
      if (blk_queue_is_zoned(ns->queue)) {
          ret = blk_revalidate_disk_zones(ns->disk);

While we're shuffling things around; doesn't the mean that we unfreeze
the queue (and allow I/O) before we've validated all zones? Wouldn't it be better if we validate zoned before unfreezing queues?

Quoted 3 lines

Otherwise it looks good; I'll give it a spin on my map/unmap testcases.

Quoted 10 lines

Hmm. Turns out we're not quite there yet.
We call 'set_capacity_and_notify()' unconditionally a few lines up, before setting the namespace to 'READY'. And then we're doing another call to 'set_capacity_and_notify()' (from nvme_update_ns_info_mpath()).
In my map/unmap tests I've seen a stall with udev, where the initial scanning process it stuck in device_add_disk() trying to read the partition table (and waiting for the uptodate bit to clear), and the udev process closing the bdev and waiting on 'truncate_filemap_range').

I don't understand how the udev stall came to pass. The path device (ns->disk) is hidden, thus the set_capacity_and_notify() call for it doesn't trigger an uevent.

Quoted 21 lines

With this patch:

drivers/nvme/host/core.c +0 -2
index fdd67a55a477..8858e5bf49de 100644
@@ -2258,8 +2258,6 @@ static int nvme_update_ns_info_block(struct 
 nvme_ns *ns,
                  goto out;
          }
 
-       set_capacity_and_notify(ns->disk, capacity);
-
          /*
           * Only set the DEAC bit if the device guarantees that reads

from
          * deallocated data return zeroes.  While the DEAC bit does not

drivers/nvme/host/core.c static int nvme_update_ns_info_block(struct · line 2279

Quoted 8 lines
drivers/nvme/host/core.c +2 -0
@@ -2274,6 +2272,8 @@ static int nvme_update_ns_info_block(struct 
 nvme_ns *ns,
          set_bit(NVME_NS_READY, &ns->flags);
          if (nvme_ns_head_multipath(ns->head))
                  ret = nvme_update_ns_info_mpath(ns, info,
 unsupported);
+       else
+               set_capacity_and_notify(ns->disk, capacity);
          nvme_ns_unfreeze(ns);

If the capacity actually changes, this would mean that we don't call set_capacity() any more for ns->disk. Which means that bdev_nr_sectors(ns->disk->part0) will not be updated, and that the later call to set_capacity_and_notify() for the multipath device will use the outdated value, as it uses the path device's capacity to update the multipath device's:

	set_capacity_and_notify(disk, get_capacity(ns->disk));

Regards,
Martin

#10 lore raw

Re: [PATCH 3/3] nvme: freeze both the ns and multipath queues whe updating queue limits

In reply to Christoph Hellwig
Quoted 133 lines · Christoph Hellwig

We really should not have any I/O outstanding while updating the queue
limits, as that could introduce inconsistencies between the multipath and underlying nodes.  So always freeze the multipath node first and then
the underlying one, matching the order in nvme_passthru_start.

---
 drivers/nvme/host/core.c | 41 ++++++++++++++++++++++++++++---------- --

 1 file changed, 29 insertions(+), 12 deletions(-)
drivers/nvme/host/core.c +0 -0
index 81d2a09dd00f30..0f01a0bf976fd3 100644
@@ -2095,6 +2095,24 @@ static void nvme_set_chunk_sectors(struct
 nvme_ns *ns, struct nvme_id_ns *id,
  	lim->chunk_sectors = iob;
  }


+/*
+ * Freeze all I/O for a namespace, including clearing out anything on the
+ * multipath node.
+ */

drivers/nvme/host/core.c +14 -0
+static void nvme_ns_freeze(struct nvme_ns *ns)
+{
+	if (nvme_ns_head_multipath(ns->head))
+		blk_mq_freeze_queue(ns->head->disk->queue);
+	blk_mq_freeze_queue(ns->disk->queue);
+}
+
+static void nvme_ns_unfreeze(struct nvme_ns *ns)
+{
+	blk_mq_unfreeze_queue(ns->disk->queue);
+	if (nvme_ns_head_multipath(ns->head))
+		blk_mq_unfreeze_queue(ns->head->disk->queue);
+}
+

 /*
  * The queue_limits structure mixes values that are the hardware limitations for
  * bio splitting with what is the device configuration.

drivers/nvme/host/core.c +0 -0
@@ -2116,7 +2134,8 @@ static int nvme_update_ns_info_mpath(struct
 nvme_ns *ns,
  	struct queue_limits lim;
  	int ret;

drivers/nvme/host/core.c +2 -1
-	blk_mq_freeze_queue(disk->queue);
+	WARN_ON_ONCE(disk->queue->mq_freeze_depth < 1);
+

  lim = queue_limits_start_update(disk->queue); lim.logical_block_size = ns_lim->logical_block_size; lim.physical_block_size = ns_lim->physical_block_size;

drivers/nvme/host/core.c +0 -1
@@ -2132,7 +2151,6 @@ static int nvme_update_ns_info_mpath(struct
 nvme_ns *ns,
  	set_capacity_and_notify(disk, get_capacity(ns->disk));
  	set_disk_ro(disk, nvme_ns_is_readonly(ns, info));
  	nvme_mpath_revalidate_paths(ns);
-	blk_mq_unfreeze_queue(disk->queue);


  return ret;
 }

drivers/nvme/host/core.c +0 -0
@@ -2143,7 +2161,7 @@ static int nvme_update_ns_info_generic(struct
 nvme_ns *ns,
  	struct queue_limits lim;
  	int ret;

drivers/nvme/host/core.c +1 -1
-	blk_mq_freeze_queue(ns->disk->queue);
+	nvme_ns_freeze(ns);

  lim = queue_limits_start_update(ns->disk->queue); nvme_set_ctrl_limits(ns->ctrl, &lim);
  ret = queue_limits_commit_update(ns->disk->queue, &lim);

drivers/nvme/host/core.c +2 -0
@@ -2154,11 +2172,11 @@ static int nvme_update_ns_info_generic(struct
 nvme_ns *ns,
  	ns->disk->flags |= GENHD_FL_HIDDEN;
  	set_bit(NVME_NS_READY, &ns->flags);
  	set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
+	if (nvme_ns_head_multipath(ns->head))
+		ret = nvme_update_ns_info_mpath(ns, info, true);


 out_unfreeze:

drivers/nvme/host/core.c +1 -3
-	blk_mq_unfreeze_queue(ns->disk->queue);
-	if (nvme_ns_head_multipath(ns->head) && !ret)
-		ret = nvme_update_ns_info_mpath(ns, info, true);
+	nvme_ns_unfreeze(ns);

  return ret;
 }

drivers/nvme/host/core.c +0 -0
@@ -2204,7 +2222,7 @@ static int nvme_update_ns_info_block(struct
 nvme_ns *ns,
  			goto out;
  	}

drivers/nvme/host/core.c +1 -1
-	blk_mq_freeze_queue(ns->disk->queue);
+	nvme_ns_freeze(ns);

  ns->head->lba_shift = id->lbaf[lbaf].ds; ns->head->nuse = le64_to_cpu(id->nuse); capacity = nvme_lba_to_sect(ns->head, le64_to_cpu(id-

Quoted 1 line

nsze));

drivers/nvme/host/core.c +0 -0
@@ -2236,7 +2254,7 @@ static int nvme_update_ns_info_block(struct
 nvme_ns *ns,


  ret = queue_limits_commit_update(ns->disk->queue, &lim); if (ret) {

drivers/nvme/host/core.c +1 -1
-		blk_mq_unfreeze_queue(ns->disk->queue);
+		nvme_ns_unfreeze(ns);

  goto out;
  }
 __

drivers/nvme/host/core.c +0 -0
@@ -2254,7 +2272,9 @@ static int nvme_update_ns_info_block(struct
 nvme_ns *ns,

 done:
  set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info)); set_bit(NVME_NS_READY, &ns->flags);

drivers/nvme/host/core.c +2 -1
-	blk_mq_unfreeze_queue(ns->disk->queue);
+	if (nvme_ns_head_multipath(ns->head))
+		ret = nvme_update_ns_info_mpath(ns, info,

unsupported);

drivers/nvme/host/core.c +1 -0
+	nvme_ns_unfreeze(ns);


  if (blk_queue_is_zoned(ns->queue)) {
  ret = blk_revalidate_disk_zones(ns->disk);

drivers/nvme/host/core.c +0 -0
@@ -2266,9 +2286,6 @@ static int nvme_update_ns_info_block(struct
 nvme_ns *ns,

 out:
  kfree(nvm);
  kfree(id);

drivers/nvme/host/core.c +0 -3
-
-	if (nvme_ns_head_multipath(ns->head) && !ret)
-		ret = nvme_update_ns_info_mpath(ns, info,
Quoted 4 lines

unsupported);
  return ret;
 }

Moving the call to nvme_update_ns_info_mpath() up before the "out" label has the effect that the prior error conditions that jump to "out" (for example, failure in queue_limits_commit_update()) will not run the multipath code any more, while they previously did. I am not saying this is wrong, but I wanted to make sure we make this change conciously. I think it should be mentioned in the commit message.

While I was staring at this code yesterday, I made no progress because my brain went in circles about this change. Looking at it now, it seems to me that when we jump to "out", nothing has been commited to the namespace queue, and thus we don't need to apply changes to the multipath queue either, and the change is thus ok.

Regards
Martin