Skip to contentSkip to conversation

nvme discard limits fix and tidy ups

PATCH In review Christoph Hellwig 3 participants 9 messages Dec 26, 2023 – Jan 2, 2024

Subsystems (2)
lore
Share & tools
lore

Christoph Hellwig

author maintainer
#0 lore raw

Hi all,

this series contains a fix that makes sure that the discard limits are properly calculated based on the LBA size for each namespaces, and also tidies up some code around that.

Diffstat:

 core.c |   41 ++++++++++++++++++++---------------------
 nvme.h |    3 +--
 2 files changed, 21 insertions(+), 23 deletions(-)
author maintainer
#1 lore raw

PATCH 1/4 nvme: update the explanation for not updating the limits in nvme_config_discard

Expeand the comment a bit to explain what is going on.

---
 drivers/nvme/host/core.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/nvme/host/core.c +7 -1
index d144d1acb09a05..56107cfc97b7bc 100644
@@ -1743,7 +1743,13 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
 
 	queue->limits.discard_granularity = size;
 
-	/* If discard is already enabled, don't reset queue limits */
+	/*
+	 * If discard is already enabled, don't reset queue limits.
+	 *
+	 * This works around the fact that the block layer can't cope well with
+	 * updating the hardware limits when overridden through sysfs.  This is
+	 * harmless because discard limits in NVMe are purely advisory.
+	 */
 	if (queue->limits.max_discard_sectors)
 		return;
 
Signature · 1 line
2.39.2
author maintainer
#2 lore raw

PATCH 2/4 nvme: also skip discard granularity updates in nvme_config_discard

In reply to Christoph Hellwig

Don't just skip the discard sectors and segments but also the granularity if a value was already set before.

---
 drivers/nvme/host/core.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/nvme/host/core.c +1 -3
index 56107cfc97b7bc..6c52b0ab382c85 100644
@@ -1727,7 +1727,6 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
 		struct nvme_ns_head *head)
 {
 	struct request_queue *queue = disk->queue;
-	u32 size = queue_logical_block_size(queue);
 
 	if (ctrl->dmrsl && ctrl->dmrsl <= nvme_sect_to_lba(head, UINT_MAX))
 		ctrl->max_discard_sectors =
@@ -1741,8 +1740,6 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
 	BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
 			NVME_DSM_MAX_RANGES);
 
-	queue->limits.discard_granularity = size;
-
 	/*
 	 * If discard is already enabled, don't reset queue limits.
 	 *
@@ -1755,6 +1752,7 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
 
 	blk_queue_max_discard_sectors(queue, ctrl->max_discard_sectors);
 	blk_queue_max_discard_segments(queue, ctrl->max_discard_segments);
+	queue->limits.discard_granularity = queue_logical_block_size(queue);
 
 	if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
 		blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
Signature · 1 line
2.39.2
Reviewed-by on v1 2/4
#3 lore raw

Re: [PATCH 2/4] nvme: also skip discard granularity updates in nvme_config_discard

Quoted 30 lines · Christoph Hellwig

Don't just skip the discard sectors and segments but also the granularity if a value was already set before.

---
  drivers/nvme/host/core.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
drivers/nvme/host/core.c +0 -3
index 56107cfc97b7bc..6c52b0ab382c85 100644
@@ -1727,7 +1727,6 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
  		struct nvme_ns_head *head)
  {
  	struct request_queue *queue = disk->queue;
-	u32 size = queue_logical_block_size(queue);
 
  	if (ctrl->dmrsl && ctrl->dmrsl <= nvme_sect_to_lba(head, UINT_MAX))
  		ctrl->max_discard_sectors =
@@ -1741,8 +1740,6 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
  	BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
  			NVME_DSM_MAX_RANGES);
 
-	queue->limits.discard_granularity = size;
-
  	/*
  	 * If discard is already enabled, don't reset queue limits.
  	 *

drivers/nvme/host/core.c static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk, · line 1758

Quoted 7 lines
drivers/nvme/host/core.c +1 -0
@@ -1755,6 +1752,7 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
 
  	blk_queue_max_discard_sectors(queue, ctrl->max_discard_sectors);
  	blk_queue_max_discard_segments(queue, ctrl->max_discard_segments);
+	queue->limits.discard_granularity = queue_logical_block_size(queue);
 
  	if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
  		blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);

maybe worth to add a small helper like we have for other discard limits:

block/blk-settings.c +11 -0
index 0046b447268f..b7eee29ca8ec 100644
@@ -369,6 +369,17 @@ void blk_queue_zone_write_granularity(struct request_queue *q,
 }
 EXPORT_SYMBOL_GPL(blk_queue_zone_write_granularity);
 
+/**
+ * blk_queue_discard_granularity - set discard granularity for the queue
+ * @q:  the request queue for the device
+ * @size:  the discard granularity size, in bytes
+ */
+void blk_queue_discard_granularity(struct request_queue *q, unsigned int size)
+{
+       q->limits.discard_granularity = size;
+}
+EXPORT_SYMBOL_GPL(blk_queue_discard_granularity);
+
 /**
  * blk_queue_alignment_offset - set physical block alignment offset
  * @q: the request queue for the device

otherwise looks good,

author maintainer
#4 lore raw

Re: [PATCH 2/4] nvme: also skip discard granularity updates in nvme_config_discard

Quoted 1 line · Max Gurtovoy

maybe worth to add a small helper like we have for other discard limits:

I'd rather not add more wrappers like this. They dont really scale, so I'm looking into atomic bulk updates of all settings right now, and the last I need is random churn due to a new helper..

author maintainer
#5 lore raw

PATCH 3/4 nvme: fix max_discard_sectors calculation

In reply to Christoph Hellwig

ctrl->max_discard_sectors stores a value that is potentially based of the DMRSL field in Identify Controller, which is in units of LBAs and thus dependent on the Format of a namespace.

Fix this by moving the calculation of max_discard_sectors entirely into nvme_config_discard and replacing the ctrl->max_discard_sectors value with a local variable so that the calculation is always namespace-specific.

Fixes 1a86924e4f46 ("nvme: fix interpretation of DMRSL")
---
 drivers/nvme/host/core.c | 20 +++++++++-----------
 drivers/nvme/host/nvme.h |  1 -
 2 files changed, 9 insertions(+), 12 deletions(-)
drivers/nvme/host/core.c +9 -11
index 6c52b0ab382c85..86b9a1c4876f5f 100644
@@ -1727,12 +1727,13 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
 		struct nvme_ns_head *head)
 {
 	struct request_queue *queue = disk->queue;
+	u32 max_discard_sectors;
 
-	if (ctrl->dmrsl && ctrl->dmrsl <= nvme_sect_to_lba(head, UINT_MAX))
-		ctrl->max_discard_sectors =
-			nvme_lba_to_sect(head, ctrl->dmrsl);
-
-	if (ctrl->max_discard_sectors == 0) {
+	if (ctrl->dmrsl && ctrl->dmrsl <= nvme_sect_to_lba(head, UINT_MAX)) {
+		max_discard_sectors = nvme_lba_to_sect(head, ctrl->dmrsl);
+	} else if (ctrl->oncs & NVME_CTRL_ONCS_DSM) {
+		max_discard_sectors = UINT_MAX;
+	} else {
 		blk_queue_max_discard_sectors(queue, 0);
 		return;
 	}
@@ -1750,7 +1751,7 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
 	if (queue->limits.max_discard_sectors)
 		return;
 
-	blk_queue_max_discard_sectors(queue, ctrl->max_discard_sectors);
+	blk_queue_max_discard_sectors(queue, max_discard_sectors);
 	blk_queue_max_discard_segments(queue, ctrl->max_discard_segments);
 	queue->limits.discard_granularity = queue_logical_block_size(queue);
 
@@ -2911,13 +2912,10 @@ static int nvme_init_non_mdts_limits(struct nvme_ctrl *ctrl)
 	struct nvme_id_ctrl_nvm *id;
 	int ret;
 
-	if (ctrl->oncs & NVME_CTRL_ONCS_DSM) {
-		ctrl->max_discard_sectors = UINT_MAX;
+	if (ctrl->oncs & NVME_CTRL_ONCS_DSM)
 		ctrl->max_discard_segments = NVME_DSM_MAX_RANGES;
-	} else {
-		ctrl->max_discard_sectors = 0;
+	else
 		ctrl->max_discard_segments = 0;
-	}
 
 	/*
 	 * Even though NVMe spec explicitly states that MDTS is not applicable
drivers/nvme/host/nvme.h +0 -1
index 3dbd187896d8d8..9a698c49ea0364 100644
@@ -297,7 +297,6 @@ struct nvme_ctrl {
 	u32 max_hw_sectors;
 	u32 max_segments;
 	u32 max_integrity_segments;
-	u32 max_discard_sectors;
 	u32 max_discard_segments;
 	u32 max_zeroes_sectors;
 #ifdef CONFIG_BLK_DEV_ZONED
Signature · 1 line
2.39.2
maintainer
#6 lore raw

Re: [PATCH 3/4] nvme: fix max_discard_sectors calculation

Quoted 3 lines · Christoph Hellwig

ctrl->max_discard_sectors stores a value that is potentially based of the DMRSL field in Identify Controller, which is in units of LBAs and thus dependent on the Format of a namespace.

Is it just me, or is this one of the more annoying conventions in NVMe
standard: controller scoped identification fields are in units of namespace specific formats?! Why not define these in namespace identifications, or provide a fixed unit size? As it is now, these types of fields would be in units of the largest LBA size of any attached namespace, and that's kind of awkward in a multi-namespace environment.

drivers/nvme/host/core.c static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk, · line 1752

Quoted 3 lines
drivers/nvme/host/core.c +0 -0
@@ -1750,7 +1751,7 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
 	if (queue->limits.max_discard_sectors)
 		return;

Since DMRSL can change when you format a namespace to a new LBA size, might the old limit be using the wrong max_discard_size if we skip updating it here? Probably not a big deal.

And let's say the user explicitly disabled max_discard_sectors through sysfs. The next driver namespace rescan will turn it back on, probably against the user's expectations. Should this limit check use 'max_hw_discard_sectors' instead?

Anyway, the driver was like that already, and the series looks good to me. I just noticed this weirdness while reviewing.

Quoted 4 lines
drivers/nvme/host/core.c +1 -1
-	blk_queue_max_discard_sectors(queue, ctrl->max_discard_sectors);
+	blk_queue_max_discard_sectors(queue, max_discard_sectors);
 	blk_queue_max_discard_segments(queue, ctrl->max_discard_segments);
 	queue->limits.discard_granularity = queue_logical_block_size(queue);
author maintainer
#7 lore raw

PATCH 4/4 nvme: simplify the max_discard_segments calculation

In reply to Christoph Hellwig

Just stash away the DMRL value in the nvme_ctrl struture, and leave all interpretation to nvme_config_discard, where we know DSM is supported by the time we're configuring the number of segments.

---
 drivers/nvme/host/core.c | 13 +++++--------
 drivers/nvme/host/nvme.h |  2 +-
 2 files changed, 6 insertions(+), 9 deletions(-)
drivers/nvme/host/core.c +5 -8
index 86b9a1c4876f5f..50818dbcfa1ae1 100644
@@ -1752,7 +1752,10 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
 		return;
 
 	blk_queue_max_discard_sectors(queue, max_discard_sectors);
-	blk_queue_max_discard_segments(queue, ctrl->max_discard_segments);
+	if (ctrl->dmrl)
+		blk_queue_max_discard_segments(queue, ctrl->dmrl);
+	else
+		blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES);
 	queue->limits.discard_granularity = queue_logical_block_size(queue);
 
 	if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
@@ -2912,11 +2915,6 @@ static int nvme_init_non_mdts_limits(struct nvme_ctrl *ctrl)
 	struct nvme_id_ctrl_nvm *id;
 	int ret;
 
-	if (ctrl->oncs & NVME_CTRL_ONCS_DSM)
-		ctrl->max_discard_segments = NVME_DSM_MAX_RANGES;
-	else
-		ctrl->max_discard_segments = 0;
-
 	/*
 	 * Even though NVMe spec explicitly states that MDTS is not applicable
 	 * to the write-zeroes, we are cautious and limit the size to the
@@ -2946,8 +2944,7 @@ static int nvme_init_non_mdts_limits(struct nvme_ctrl *ctrl)
 	if (ret)
 		goto free_data;
 
-	if (id->dmrl)
-		ctrl->max_discard_segments = id->dmrl;
+	ctrl->dmrl = id->dmrl;
 	ctrl->dmrsl = le32_to_cpu(id->dmrsl);
 	if (id->wzsl)
 		ctrl->max_zeroes_sectors = nvme_mps_to_sectors(ctrl, id->wzsl);
drivers/nvme/host/nvme.h +1 -1
index 9a698c49ea0364..297b80430f1b69 100644
@@ -297,13 +297,13 @@ struct nvme_ctrl {
 	u32 max_hw_sectors;
 	u32 max_segments;
 	u32 max_integrity_segments;
-	u32 max_discard_segments;
 	u32 max_zeroes_sectors;
 #ifdef CONFIG_BLK_DEV_ZONED
 	u32 max_zone_append;
 #endif
 	u16 crdt[3];
 	u16 oncs;
+	u8 dmrl;
 	u32 dmrsl;
 	u16 oacs;
 	u16 sqsize;
Signature · 1 line
2.39.2
maintainer Applied
#8 lore raw
In reply to Christoph Hellwig
Quoted 5 lines · Christoph Hellwig

Hi all,

this series contains a fix that makes sure that the discard limits are properly calculated based on the LBA size for each namespaces, and also tidies up some code around that.

Applied to nvme-6.8.