Skip to content

Commit f29886c

Browse files
Christoph Hellwigkeithbusch
authored andcommitted
nvme: fix max_discard_sectors calculation
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: 1a86924 ("nvme: fix interpretation of DMRSL") Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent a4be967 commit f29886c

2 files changed

Lines changed: 9 additions & 12 deletions

File tree

drivers/nvme/host/core.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,12 +1727,13 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
17271727
struct nvme_ns_head *head)
17281728
{
17291729
struct request_queue *queue = disk->queue;
1730+
u32 max_discard_sectors;
17301731

1731-
if (ctrl->dmrsl && ctrl->dmrsl <= nvme_sect_to_lba(head, UINT_MAX))
1732-
ctrl->max_discard_sectors =
1733-
nvme_lba_to_sect(head, ctrl->dmrsl);
1734-
1735-
if (ctrl->max_discard_sectors == 0) {
1732+
if (ctrl->dmrsl && ctrl->dmrsl <= nvme_sect_to_lba(head, UINT_MAX)) {
1733+
max_discard_sectors = nvme_lba_to_sect(head, ctrl->dmrsl);
1734+
} else if (ctrl->oncs & NVME_CTRL_ONCS_DSM) {
1735+
max_discard_sectors = UINT_MAX;
1736+
} else {
17361737
blk_queue_max_discard_sectors(queue, 0);
17371738
return;
17381739
}
@@ -1750,7 +1751,7 @@ static void nvme_config_discard(struct nvme_ctrl *ctrl, struct gendisk *disk,
17501751
if (queue->limits.max_discard_sectors)
17511752
return;
17521753

1753-
blk_queue_max_discard_sectors(queue, ctrl->max_discard_sectors);
1754+
blk_queue_max_discard_sectors(queue, max_discard_sectors);
17541755
blk_queue_max_discard_segments(queue, ctrl->max_discard_segments);
17551756
queue->limits.discard_granularity = queue_logical_block_size(queue);
17561757

@@ -2911,13 +2912,10 @@ static int nvme_init_non_mdts_limits(struct nvme_ctrl *ctrl)
29112912
struct nvme_id_ctrl_nvm *id;
29122913
int ret;
29132914

2914-
if (ctrl->oncs & NVME_CTRL_ONCS_DSM) {
2915-
ctrl->max_discard_sectors = UINT_MAX;
2915+
if (ctrl->oncs & NVME_CTRL_ONCS_DSM)
29162916
ctrl->max_discard_segments = NVME_DSM_MAX_RANGES;
2917-
} else {
2918-
ctrl->max_discard_sectors = 0;
2917+
else
29192918
ctrl->max_discard_segments = 0;
2920-
}
29212919

29222920
/*
29232921
* Even though NVMe spec explicitly states that MDTS is not applicable

drivers/nvme/host/nvme.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ struct nvme_ctrl {
297297
u32 max_hw_sectors;
298298
u32 max_segments;
299299
u32 max_integrity_segments;
300-
u32 max_discard_sectors;
301300
u32 max_discard_segments;
302301
u32 max_zeroes_sectors;
303302
#ifdef CONFIG_BLK_DEV_ZONED

0 commit comments

Comments
 (0)