Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Loic Poulain" <loic.poulain@oss.qualcomm.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v14 2/2] block: implement NVMEM provider
Date: Wed, 02 Dec 2026 14:05:47 +0000	[thread overview]
Message-ID: <sashiko-outbox-245926@kernel.org> (raw)
In-Reply-To: <20261201-block-as-nvmem-v14-2-3a07f4ff246c@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] block: uninitialized i_blkbits and open race in
  blk_nvmem_reg_read()
- [High] block: blk_nvmem_del() called before setting GD_DEAD in
  __del_gendisk()
- [Medium] block: incomplete exclusive claim checks and race in
  bdev_open()
--

commit f9f9b1014c54843ace4a0a5b1f337b992b4d78ce
Author: Loic Poulain <loic.poulain@oss.qualcomm.com>

block: implement NVMEM provider

This commit registers block devices and partitions that describe an NVMEM
layout in device tree as read-only NVMEM providers backed by the block
device page cache, and rejects exclusive claims on them in bdev_open().

> diff --git a/block/bdev.c b/block/bdev.c
> index e0a3dbb06837f..730a42c6161af 100644
> --- a/block/bdev.c
> +++ b/block/bdev.c
> @@ -976,6 +976,13 @@ int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder,
>  	int ret;
>  
>  	if (holder) {
> +		/*
> +		 * A block device providing NVMEM cells is not a filesystem,
> +		 * md or dm member and must not be claimed exclusively.
> +		 */
> +		if (bdev_is_nvmem(bdev))
> +			return -EBUSY;
> +
>  		mode |= BLK_OPEN_EXCL;
>  		ret = bd_prepare_to_claim(bdev, holder, hops);
>  		if (ret)

[Severity: Medium]
Can an exclusive opener still claim the underlying disk, or race with
blk_nvmem_add() to claim the NVMEM block device itself?

bdev_is_nvmem(bdev) only checks bdev->bd_nvmem on the exact block_device
being opened, and blk_nvmem_add() does not mark whole->bd_holder or
increment bd_openers / disk->open_partitions.  When a partition is an
NVMEM provider, an exclusive open of disk->part0 (or vice versa) sees
bdev_is_nvmem() == false and bd_may_claim() == true, claiming the whole
disk, and BLKPG_DEL_PARTITION or BLKRRPART can delete the partition.

In addition, add_disk_final() and add_partition() call bdev_add() and emit
KOBJ_ADD before calling blk_nvmem_add(), which only sets bdev->bd_nvmem
after nvmem_register() finishes probing layout drivers, and bdev_open()
does not recheck bdev_is_nvmem(bdev) under disk->open_mutex:

CPU0                                  CPU1
add_disk_final() / add_partition()
  bdev_add(bdev, ...)
  kobject_uevent(..., KOBJ_ADD)
                                      bdev_open(bdev, ..., holder, ...)
                                        if (bdev_is_nvmem(bdev)) -> false
                                        bd_prepare_to_claim(...)
  blk_nvmem_add(bdev)
    nvmem_register(&config)
    WRITE_ONCE(bdev->bd_nvmem, nvmem)
                                        mutex_lock(&disk->open_mutex)
                                        bd_finish_claiming(...)  <-- claimed

> diff --git a/block/blk-nvmem.c b/block/blk-nvmem.c
> new file mode 100644
> index 0000000000000..8d29fd338f0b0
> --- /dev/null
> +++ b/block/blk-nvmem.c
> @@ -0,0 +1,121 @@
[ ... ]
> +static int blk_nvmem_reg_read(void *priv, unsigned int from,
> +			      void *val, size_t bytes)
> +{
> +	struct block_device *bdev = priv;
> +	struct address_space *mapping = bdev->bd_mapping;
> +	loff_t pos = from;
> +
> +	/* the device may have shrunk since the NVMEM device was registered */
> +	if (pos + bytes > bdev_nr_bytes(bdev))
> +		return -EIO;
> +
> +	while (bytes) {
> +		struct folio *folio;
> +		size_t offset, len;
> +
> +		folio = read_mapping_folio(mapping, pos >> PAGE_SHIFT, NULL);
> +		if (IS_ERR(folio))
> +			return PTR_ERR(folio);

[Severity: High]
Does reading from bdev->bd_mapping without opening bdev use an
uninitialized inode->i_blkbits and race with set_init_blocksize() on the
first open?

bdev_alloc() allocates BD_INODE(bdev) from blockdev_superblock with
i_blkbits = PAGE_SHIFT and minimum folio order 0; set_init_blocksize()
only configures them in blkdev_get_whole() / blkdev_get_part() when
bd_openers transitions from 0 to 1:

block/bdev.c:blkdev_get_part() {
	...
	if (!atomic_read(&part->bd_openers)) {
		disk->open_partitions++;
		set_init_blocksize(part);
	}
	...
}

When blk_nvmem_reg_read() runs before bdev has been opened (such as during
synchronous layout probing in nvmem_register()), i_blkbits is still
PAGE_SHIFT.  If the partition or disk size is not a multiple of PAGE_SIZE,
reading the last page under CONFIG_BUFFER_HEAD=y submits a PAGE_SIZE read
bio past bdev_nr_sectors(bdev) that bio_check_eod() fails with -EIO (while
under !CONFIG_BUFFER_HEAD, iomap_read_folio_iter() sees one block per
folio and marks the folio uptodate without zeroing the post-EOF tail).

Because bd_openers stays 0, those PAGE_SIZE folios and buffer_heads remain
cached in bdev->bd_mapping.  When a non-exclusive opener such as udev or
blkid later opens bdev for the first time, set_init_blocksize() mutates
i_blkbits and mapping->flags without locking or invalidating the mapping,
racing with concurrent blk_nvmem_reg_read() calls and leaving cached
buffer_heads whose b_size no longer matches i_blkbits (triggering
WARN_ON(bh->b_size != blocksize) in block_read_full_folio()).

[ ... ]
> diff --git a/block/genhd.c b/block/genhd.c
> index 69204d44adae1..39adc6f5e44cf 100644
> --- a/block/genhd.c
> +++ b/block/genhd.c
[ ... ]
> @@ -708,6 +710,8 @@ static void __del_gendisk(struct gendisk *disk)
>  
>  	disk_del_events(disk);
>  
> +	blk_nvmem_del(disk->part0);
> +
>  	/*
>  	 * Prevent new openers by unlinked the bdev inode.
>  	 */

[Severity: High]
Can calling blk_nvmem_del(disk->part0) before __blk_mark_disk_dead(disk)
deadlock when the request queue is frozen or runtime-suspended?

As noted in the comment above blk_nvmem_del(), draining NVMEM readers
relies on GD_DEAD already being set so that __bio_queue_enter() errors out
instead of waiting for the queue to unfreeze or resume.  In
__del_gendisk(), however, blk_nvmem_del(disk->part0) runs before
__blk_mark_disk_dead(disk):

CPU0                                  CPU1
__del_gendisk(disk)                   nvmem_reg_read()
                                        srcu_read_lock(&nvmem->srcu)
                                        blk_nvmem_reg_read()
                                          ...
                                            __bio_queue_enter()
                                              wait_event(q->mq_freeze_wq,
                                                ... || GD_DEAD)
  blk_nvmem_del(disk->part0)
    nvmem_unregister()
      synchronize_srcu(&nvmem->srcu)  <-- deadlocks waiting for CPU1
  ...
  __blk_mark_disk_dead(disk)          <-- never reached

This also clears disk->part0->bd_nvmem before bdev_unhash(disk->part0)
runs under disk->open_mutex, leaving a window while disk_live(disk) is
still true where an exclusive bdev_open() on disk->part0 sees
bdev_is_nvmem() == false.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201-block-as-nvmem-v14-0-3a07f4ff246c@oss.qualcomm.com?part=2

      reply	other threads:[~2026-12-02 14:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 14:05 [PATCH v14 0/2] Support for block device NVMEM providers Loic Poulain
2026-12-01 14:05 ` [PATCH v14 1/2] block: partitions: of: Attach partition fwnode to the block device Loic Poulain
2026-12-01 14:05 ` [PATCH v14 2/2] block: implement NVMEM provider Loic Poulain
2026-12-02 14:05   ` 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-245926@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=loic.poulain@oss.qualcomm.com \
    --cc=robh@kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox