From: sashiko-bot@kernel.org
To: "Li Youhong" <dayou5941@163.com>
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH] nvme: round physical block size down to a power of two
Date: Fri, 04 Dec 2026 06:57:03 +0000 [thread overview]
Message-ID: <sashiko-outbox-249265@kernel.org> (raw)
In-Reply-To: <20261204063559.1044548-1-dayou5941@163.com>
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
prev parent reply other threads:[~2026-12-04 6:57 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
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=sashiko-outbox-249265@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dayou5941@163.com \
--cc=linux-nvme@lists.infradead.org \
--cc=sashiko-reviews@lists.linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.