[PATCH v2 07/22] nvmet: add context annotation for nvmet_ns::size

sashiko-bot at kernel.org sashiko-bot at kernel.org
Tue Nov 17 23:50:07 PST 2026


Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] nvmet: double READ_ONCE() in nvmet_file_check_lba_range() can
  underflow
--

commit d3bcca86a53dabf9c0c2d63509a67010aa5e3ea3
Author: Nilay Shroff <nilay at linux.ibm.com>

nvmet: add context annotation for nvmet_ns::size

This commit annotates nvmet_ns::size with __guarded_by(&subsys->lock),
introduces nvmet_ns_size() using data_race(READ_ONCE()) for lockless reads,
and pairs those reads with WRITE_ONCE() on updates.

> diff --git a/drivers/nvme/target/io-cmd-file.c b/drivers/nvme/target/io-cmd-file.c
> index e1e02f5f5efb1..c87de131037d4 100644
> --- a/drivers/nvme/target/io-cmd-file.c
> +++ b/drivers/nvme/target/io-cmd-file.c
[ ... ]
> @@ -123,7 +123,8 @@ static bool nvmet_file_check_lba_range(struct nvmet_req *req, u64 slba,
>  	u64 pos64;
>  
>  	if (check_shl_overflow(slba, nvmet_ns_blksize_shift(req->ns), &pos64) ||
> -	    pos64 > req->ns->size || len > req->ns->size - pos64)
> +	    pos64 > nvmet_ns_size(req->ns) ||
> +	    len > nvmet_ns_size(req->ns) - pos64)
>  		return false;
>  
>  	*pos = pos64;

[Severity: Medium]
Can calling nvmet_ns_size(req->ns) twice here underflow nvmet_ns_size(req->ns)
- pos64 and bypass the bounds check?

Because nvmet_ns_size() uses READ_ONCE(), the two calls perform two separate
loads of req->ns->size.  If a concurrent nvmet_file_ns_revalidate() shrinks
req->ns->size below pos64 between the two loads, the first check passes against
the old size while the unsigned 64-bit subtraction in the second check wraps
around near U64_MAX:

CPU0                                  CPU1
nvmet_file_check_lba_range()
  pos64 > nvmet_ns_size(req->ns)
    /* size1 >= pos64: false */
                                      nvmet_file_ns_revalidate()
                                        WRITE_ONCE(ns->size, size2)
                                        /* size2 < pos64 */
  len > nvmet_ns_size(req->ns) - pos64
    /* (u64)size2 - pos64 underflows */

An out-of-range write request then passes nvmet_file_check_lba_range() and can
extend the backing file past the namespace capacity instead of failing with
NVME_SC_CAP_EXCEEDED.  Reading nvmet_ns_size(req->ns) once into a local
variable and using it for both comparisons avoids this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118073505.288910-1-nilay@linux.ibm.com?part=7



More information about the Linux-nvme mailing list