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

* Re: [PATCH] nvme: round physical block size down to a power of two
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04  6:57 UTC (permalink / raw)
  To: Li Youhong; +Cc: linux-nvme

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] nvme: rounddown_pow_of_two(0) UB on u32 overflow in
  nvme_update_disk_info()
- [Medium] nvme: nvme_update_disk_info() rounds physical_block_size to
  non-divisor
--

commit 206ca12cfb02e4dd20fd71b056be00d31099fd6f
Author: Li Youhong <liyouhong@kylinos.cn>

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

This commit rounds lim->physical_block_size down to the nearest power of
two in nvme_update_disk_info() when the value derived from NPWG and the
atomic write unit is not already a power of two.

> diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
> index f43582e5f6c4a..6b505971a02bd 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);

[Severity: Medium]
Can lim->physical_block_size be 0 here and invoke undefined behavior in
rounddown_pow_of_two(0)?

With CONFIG_TRANSPARENT_HUGEPAGE, blk_validate_block_size() accepts bs
up to SZ_64K.  If optperf is set and id->npwg is 0xffff, the u32
multiplication bs * (1 + le16_to_cpu(id->npwg)) overflows to 0.

Because is_power_of_2(0) is false, this calls rounddown_pow_of_two(0)
(1UL << (fls_long(0) - 1)), tripping CONFIG_UBSAN_SHIFT checks where
blk_validate_limits() previously clamped 0 to lim->logical_block_size.

Using check_mul_overflow() for phys_bs, as already done for
discard_granularity in this function, and checking for non-zero before
rounding would avoid the undefined shift.

>  	lim->io_min = phys_bs;
>  	lim->io_opt = io_opt;

[Severity: Medium]
Does rounddown_pow_of_two() guarantee that physical_block_size does not
divide io_min whenever phys_bs is not a power of two?

For any non-power-of-two phys_bs <= atomic_bs (such as 1536), rounding
down yields a power of two p with p < phys_bs < 2 * p (1024), so io_min
is never a multiple of physical_block_size.

With NVMe multipath, nvme_update_ns_info() -> queue_limits_stack_bdev()
-> blk_stack_limits() then overwrites io_min with physical_block_size,
defeating NPWG advertisement, and sets BLK_FLAG_MISALIGNED so
alignment_offset reports -1 on a healthy namespace.

Even on single-path disks, blk_validate_limits() rounds lim->io_opt down
to a multiple of lim->physical_block_size, truncating NOWS values that
are multiples of NPWG (e.g. 1536 to 1024, below io_min).

Using the largest power-of-two divisor of phys_bs capped by the atomic
write limit (e.g. min(phys_bs & -phys_bs,
rounddown_pow_of_two(atomic_bs))) would give a power-of-two
physical_block_size that divides io_min without false misalignment.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204063559.1044548-1-dayou5941@163.com?part=1


^ permalink raw reply	[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