[PATCH] nvme: round physical block size down to a power of two

From: Li Youhong

Date: Fri Dec 04 2026 - 01:37:23 EST


From: Li Youhong <liyouhong@xxxxxxxxxx>

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@xxxxxxxxxx>
---
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) &&