mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chris Down <chris@chrisdown.name>
To: "Martin K. Petersen" <mkp@kernel.org>,
	"James E.J. Bottomley" <James.Bottomley@hansenpartnership.com>
Cc: Christoph Hellwig <hch@lst.de>,
	linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org,
	kernel-team@meta.com, sashiko-bot@kernel.org,
	Bart Van Assche <bvanassche@acm.org>
Subject: [PATCH v3 1/5] scsi: sd: Fix stale max_hw_wzeroes_unmap_sectors after a mode change
Date: Sun, 29 Nov 2026 11:25:22 -0800	[thread overview]
Message-ID: <e7f5fc5e63a1b6b3dec03ab94886b087b5070880.1795967558.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1795967558.git.chris@chrisdown.name>

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@vger.kernel.org
Signed-off-by: Chris Down <chris@chrisdown.name>
---
 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


  reply	other threads:[~2026-11-29 19:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 19:25 [PATCH v3 0/5] scsi: sd: Fix WRITE SAME(10) data corruption Chris Down
2026-11-29 19:25 ` Chris Down [this message]
2026-11-29 19:25 ` [PATCH v3 2/5] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes Chris Down
2026-11-29 19:25 ` [PATCH v3 3/5] scsi: sd: Fix data corruption in WRITE SAME(10) discards Chris Down
2026-11-29 19:25 ` [PATCH v3 4/5] scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes Chris Down
2026-11-29 19:25 ` [PATCH v3 5/5] scsi: sd: Fix truncation of WRITE SAME(10) requests that don't fit Chris Down

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e7f5fc5e63a1b6b3dec03ab94886b087b5070880.1795967558.git.chris@chrisdown.name \
    --to=chris@chrisdown.name \
    --cc=James.Bottomley@hansenpartnership.com \
    --cc=bvanassche@acm.org \
    --cc=hch@lst.de \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=mkp@kernel.org \
    --cc=sashiko-bot@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®