Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Ankit Gupta <ankit6791@gmail.com>
To: linux-nvme@lists.infradead.org
Cc: Ankit Gupta <ankit6791@gmail.com>,
	Keith Busch <kbusch@kernel.org>, Jens Axboe <axboe@kernel.dk>,
	Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>,
	Chaitanya Kulkarni <kch@nvidia.com>,
	stable@vger.kernel.org
Subject: [PATCH v2] nvme: limit Write Zeroes to 65536 logical blocks
Date: Mon, 16 Nov 2026 17:10:49 -0800	[thread overview]
Message-ID: <20261117011230.10288-1-ankit6791@gmail.com> (raw)

With nvme-tcp connected to nvmet-tcp at default settings, the target
reports MDTS 0 and WZSL 0. A 33 MiB Write Zeroes request on a
512-byte-LBA namespace then completes successfully after zeroing only
the first 1 MiB.

The native Write Zeroes command encodes the number of logical blocks
in a zero-based 16-bit field. The namespace queue can advertise a
larger limit, either through the MDTS fallback or through WZSL. In
that case the host truncates the count when building the command and
reports success after zeroing only part of the requested range.

Cap the native Write Zeroes limit at the number of logical blocks the
command can encode. Convert that limit to sectors for each namespace
and retain any smaller controller limit. The DSM deallocation quirk
continues to use its own limit.

Fixes: b94e8cd2e6a9 ("nvme: fix Write Zeroes limitations")
Cc: stable@vger.kernel.org
Signed-off-by: Ankit Gupta <ankit6791@gmail.com>
---
Tested on arm64 at v7.4-rc3 (aebb72744764), over TCP with the stock
nvmet target. Full-range read-back passed for 1, 32, 33, 64, 256 and
1024 MiB with 512-byte LBAs, and 256, 257, 512 and 1024 MiB with
4 KiB LBAs. Host and target traces confirmed the expected command
splitting. A smaller 1 MiB MDTS-derived limit was preserved, and XFS
formatted and mounted successfully.

Claude and Codex assisted with the investigation, patch preparation,
and validation. I also reproduced the 33 MiB failure and checked the
trace by hand on an unpatched kernel.

I will follow up with a blktests regression test for this, as
Christoph suggested.

Changes since v1 (sent to the maintainers off-list):
  - Move the calculation into nvme_max_write_zeroes_sectors() and
    derive the maximum block count from the command's length field.
  - Describe the affected configuration and Cc stable.

 drivers/nvme/host/core.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index f4aa02869b79..441d70001920 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -2093,6 +2093,16 @@ static void nvme_set_ctrl_limits(struct nvme_ctrl *ctrl,
 		lim->dma_alignment = 3;
 }
 
+static sector_t nvme_max_write_zeroes_sectors(struct nvme_ns *ns)
+{
+	const size_t nlb_bytes =
+		sizeof_field(struct nvme_write_zeroes_cmd, length);
+	const u64 max_lbas = BIT_ULL(BYTES_TO_BITS(nlb_bytes));
+
+	return min_t(sector_t, ns->ctrl->max_zeroes_sectors,
+		     nvme_lba_to_sect(ns->head, max_lbas));
+}
+
 static bool nvme_update_disk_info(struct nvme_ns *ns, struct nvme_id_ns *id,
 		struct nvme_id_ns_nvm *nvm, struct queue_limits *lim)
 {
@@ -2143,7 +2153,8 @@ static bool nvme_update_disk_info(struct nvme_ns *ns, struct nvme_id_ns *id,
 	    (ctrl->oncs & NVME_CTRL_ONCS_DSM))
 		lim->max_write_zeroes_sectors = UINT_MAX;
 	else
-		lim->max_write_zeroes_sectors = ctrl->max_zeroes_sectors;
+		lim->max_write_zeroes_sectors =
+			nvme_max_write_zeroes_sectors(ns);
 
 	if (ctrl->dmrsl && ctrl->dmrsl <= nvme_sect_to_lba(ns->head, UINT_MAX))
 		lim->max_hw_discard_sectors =


                 reply	other threads:[~2026-11-17  1:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261117011230.10288-1-ankit6791@gmail.com \
    --to=ankit6791@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=kch@nvidia.com \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    --cc=stable@vger.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