Skip to contentSkip to conversation

nvme: use UINT_MAX >> SECTOR_SHIFT for maximums in number of sectors

PATCH NAK Tom Yan 2 participants 3 messages Aug 16, 2021 NVMe

lore
Share & tools
lore

Tom Yan

author
#0 lore raw

PATCH 1/2 nvme: use UINT_MAX >> SECTOR_SHIFT for maximums in number of sectors

The block layer uses UINT_MAX for maximums in bytes.

---
 drivers/nvme/host/core.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/nvme/host/core.c +4 -4
index dfd9dec0c1f6..3a1bfb45da2a 100644
@@ -1640,7 +1640,7 @@ static void nvme_config_discard(struct gendisk *disk, struct nvme_ns *ns)
 	blk_queue_max_discard_segments(queue, ctrl->max_discard_segments);
 
 	if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
-		blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
+		blk_queue_max_write_zeroes_sectors(queue, UINT_MAX >> SECTOR_SHIFT);
 }
 
 static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids)
@@ -2762,7 +2762,7 @@ static inline u32 nvme_mps_to_sectors(struct nvme_ctrl *ctrl, u32 units)
 	u32 page_shift = NVME_CAP_MPSMIN(ctrl->cap) + 12, val;
 
 	if (check_shl_overflow(1U, units + page_shift - 9, &val))
-		return UINT_MAX;
+		return UINT_MAX >> SECTOR_SHIFT;
 	return val;
 }
 
@@ -2773,7 +2773,7 @@ static int nvme_init_non_mdts_limits(struct nvme_ctrl *ctrl)
 	int ret;
 
 	if (ctrl->oncs & NVME_CTRL_ONCS_DSM) {
-		ctrl->max_discard_sectors = UINT_MAX;
+		ctrl->max_discard_sectors = UINT_MAX >> SECTOR_SHIFT;
 		ctrl->max_discard_segments = NVME_DSM_MAX_RANGES;
 	} else {
 		ctrl->max_discard_sectors = 0;
@@ -2883,7 +2883,7 @@ static int nvme_init_identify(struct nvme_ctrl *ctrl)
 	if (id->mdts)
 		max_hw_sectors = nvme_mps_to_sectors(ctrl, id->mdts);
 	else
-		max_hw_sectors = UINT_MAX;
+		max_hw_sectors = UINT_MAX >> SECTOR_SHIFT;
 	ctrl->max_hw_sectors =
 		min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
 
Signature · 7 lines
2.32.0


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
author
#1 lore raw

PATCH 2/2 nvme: round max_discard_sectors down against discard_granularity

---
 drivers/nvme/host/core.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/nvme/host/core.c +3 -0
index 3a1bfb45da2a..dff165b92ff6 100644
@@ -1636,6 +1636,9 @@ static void nvme_config_discard(struct gendisk *disk, struct nvme_ns *ns)
 	if (blk_queue_flag_test_and_set(QUEUE_FLAG_DISCARD, queue))
 		return;
 
+	ctrl->max_discard_sectors = round_down(ctrl->max_discard_sectors,
+					       size >> SECTOR_SHIFT);
+
 	blk_queue_max_discard_sectors(queue, ctrl->max_discard_sectors);
 	blk_queue_max_discard_segments(queue, ctrl->max_discard_segments);
 
Signature · 7 lines
2.32.0


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
maintainer NAK
#2 lore raw
In reply to Tom Yan
Quoted 1 line · Tom Yan

The block layer uses UINT_MAX for maximums in bytes.

Nak, the code is correct as-is. A UINT_MAX number of sectors is valid when the device doesn't have an upper limit.

Quoted 38 lines
---
 drivers/nvme/host/core.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/nvme/host/core.c +3 -3
index dfd9dec0c1f6..3a1bfb45da2a 100644
@@ -1640,7 +1640,7 @@ static void nvme_config_discard(struct gendisk *disk, struct nvme_ns *ns)
 	blk_queue_max_discard_segments(queue, ctrl->max_discard_segments);
 
 	if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
-		blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
+		blk_queue_max_write_zeroes_sectors(queue, UINT_MAX >> SECTOR_SHIFT);
 }
 
 static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids)
@@ -2762,7 +2762,7 @@ static inline u32 nvme_mps_to_sectors(struct nvme_ctrl *ctrl, u32 units)
 	u32 page_shift = NVME_CAP_MPSMIN(ctrl->cap) + 12, val;
 
 	if (check_shl_overflow(1U, units + page_shift - 9, &val))
-		return UINT_MAX;
+		return UINT_MAX >> SECTOR_SHIFT;
 	return val;
 }
 
@@ -2773,7 +2773,7 @@ static int nvme_init_non_mdts_limits(struct nvme_ctrl *ctrl)
 	int ret;
 
 	if (ctrl->oncs & NVME_CTRL_ONCS_DSM) {
-		ctrl->max_discard_sectors = UINT_MAX;
+		ctrl->max_discard_sectors = UINT_MAX >> SECTOR_SHIFT;
 		ctrl->max_discard_segments = NVME_DSM_MAX_RANGES;
 	} else {
 		ctrl->max_discard_sectors = 0;

drivers/nvme/host/core.c static int nvme_init_identify(struct nvme_ctrl *ctrl) · line 2889

Quoted 8 lines
drivers/nvme/host/core.c +1 -1
@@ -2883,7 +2883,7 @@ static int nvme_init_identify(struct nvme_ctrl *ctrl)
 	if (id->mdts)
 		max_hw_sectors = nvme_mps_to_sectors(ctrl, id->mdts);
 	else
-		max_hw_sectors = UINT_MAX;
+		max_hw_sectors = UINT_MAX >> SECTOR_SHIFT;
 	ctrl->max_hw_sectors =
 		min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
 

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme