Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme: round physical block size down to a power of two
@ 2026-12-04  6:35 Li Youhong
  2026-12-04  6:57 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Li Youhong @ 2026-12-04  6:35 UTC (permalink / raw)
  To: kbusch, axboe, hch, sagi; +Cc: linux-nvme, linux-kernel, Li Youhong

From: Li Youhong <liyouhong@kylinos.cn>

NPWG is a preferred write granularity and is not required to produce a
power-of-two byte size. Combined with Atomic Write Unit Power Fail,
nvme_update_disk_info() can therefore set physical_block_size to a
non-power-of-two value such as 1536 when LBA size is 512, NPWG is 2
(3 LBAs) and the atomic unit is larger.

The block layer assumes physical_block_size is a power of two: stacking
checks use bitmask arithmetic, and blk_validate_limits() rejects other
values since commit 1da67b5b1754 ("block: Enforce power-of-2 physical
block size").

Round the derived physical block size down to the nearest power of two.
Leave io_min as the NPWG-derived phys_bs so the preferred write
granularity is still advertised; if that is not a multiple of the
rounded physical block size, blk_stack_limits() will report the
topology as misaligned.

Fixes: 81adb8633491 ("nvme: set physical block size and optimal I/O size")
Signed-off-by: Li Youhong <liyouhong@kylinos.cn>
---
diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index f4aa02869b79..952ecf7ec01a 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -2137,6 +2137,14 @@ static bool nvme_update_disk_info(struct nvme_ns *ns, struct nvme_id_ns *id,
 	 */
 	lim->logical_block_size = bs;
 	lim->physical_block_size = min(phys_bs, atomic_bs);
+	/*
+	 * NPWG is only a preferred write granularity and need not yield a
+	 * power-of-two size (e.g. LBA=512, NPWG=2 -> 1536). The block layer
+	 * requires physical_block_size to be a power of two.
+	 */
+	if (!is_power_of_2(lim->physical_block_size))
+		lim->physical_block_size =
+			rounddown_pow_of_two(lim->physical_block_size);
 	lim->io_min = phys_bs;
 	lim->io_opt = io_opt;
 	if ((ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES) &&



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04  6:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  6:35 [PATCH] nvme: round physical block size down to a power of two Li Youhong
2026-12-04  6:57 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox