[PATCH v3 1/5] scsi: sd: Fix stale max_hw_wzeroes_unmap_sectors after a mode change
From: Chris Down
Date: Sun Nov 29 2026 - 14:25:33 EST
sd_config_write_same() sets max_hw_wzeroes_unmap_sectors when the
zeroing mode requires unmap. However, it does not clear it later. If
the mode changes to one that doesn't unmap, the old value stays, and
the queue still reports that write zeroes will unmap.
blk_validate_limits() also wants max_hw_wzeroes_unmap_sectors to be
either zero or equal to max_write_zeroes_sectors. This means that if
the write zeroes limit changes as well, the whole update is rejected
and none of the new limits are applied.
This can already be triggered from sysfs. On a disk that was in one of
the unmap modes, writing "write" to zeroing_mode and then 0 to
max_write_same_blocks makes that store fail with -EINVAL. From that
point forward, so does the limits update in each revalidate. The next
patch makes the zeroing mode depend on the capacity as well, and then
resizing a disk can trigger it too.
So, let's clear the limit when the mode doesn't unmap.
Writing to zeroing_mode doesn't update the limit at all, so it can go
stale in either direction there. Let's set it from the new mode in
zeroing_mode_store() as well.
Fixes: 6dffe079fe36 ("scsi: sd: set max_hw_wzeroes_unmap_sectors if device supports SD_ZERO_*_UNMAP")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
drivers/scsi/sd.c | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index dc45df819c42..13e399631fa8 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -146,6 +146,17 @@ static void sd_large_pool_destroy(void)
mutex_unlock(&sd_mutex_lock);
}
+static void sd_config_wzeroes_unmap(struct scsi_disk *sdkp,
+ struct queue_limits *lim)
+{
+ if (sdkp->zeroing_mode == SD_ZERO_WS16_UNMAP ||
+ sdkp->zeroing_mode == SD_ZERO_WS10_UNMAP)
+ lim->max_hw_wzeroes_unmap_sectors =
+ lim->max_write_zeroes_sectors;
+ else
+ lim->max_hw_wzeroes_unmap_sectors = 0;
+}
+
static void sd_disable_discard(struct scsi_disk *sdkp)
{
sdkp->provisioning_mode = SD_LBP_DISABLE;
@@ -621,7 +632,8 @@ zeroing_mode_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct scsi_disk *sdkp = to_scsi_disk(dev);
- int mode;
+ struct queue_limits lim;
+ int mode, err;
if (!capable(CAP_SYS_ADMIN))
return -EACCES;
@@ -630,8 +642,12 @@ zeroing_mode_store(struct device *dev, struct device_attribute *attr,
if (mode < 0)
return -EINVAL;
+ lim = queue_limits_start_update(sdkp->disk->queue);
sdkp->zeroing_mode = mode;
-
+ sd_config_wzeroes_unmap(sdkp, &lim);
+ err = queue_limits_commit_update_frozen(sdkp->disk->queue, &lim);
+ if (err)
+ return err;
return count;
}
static DEVICE_ATTR_RW(zeroing_mode);
@@ -1245,10 +1261,7 @@ out:
lim->max_write_zeroes_sectors =
sdkp->max_ws_blocks * (logical_block_size >> SECTOR_SHIFT);
- if (sdkp->zeroing_mode == SD_ZERO_WS16_UNMAP ||
- sdkp->zeroing_mode == SD_ZERO_WS10_UNMAP)
- lim->max_hw_wzeroes_unmap_sectors =
- lim->max_write_zeroes_sectors;
+ sd_config_wzeroes_unmap(sdkp, lim);
}
static blk_status_t sd_setup_flush_cmnd(struct scsi_cmnd *cmd)
--
2.56.1