From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796220348; b=5sxjn82uXTkqSdtaHaQww7+sKIhmrK4iJ3vcbJs8fysHp4zB5SXV10QM2/DMwUWq/FbG 2G2gKY94zNqTO9OeO6R+pEDdd0QtXYfvOofszHVyVD8eWclJlE8wTdnGMfR78IHeTCYZR NmzGKLJX5/uBR209INHwntgONZ8hij4qtANMqec6kd8ZPLuHxi8HMMrWQaXb5XbN5v3qV HYj+/ZssNkfCqWRYEkVIvtpCc85gbac8wyUoE9fPGWkN3dgy4BxHig0u4TlXaA6UhM7A2 JIkua/+fbozAHLJarqKT9wz6OHJaoZd6U+Oqqzej93jrdQaPewgLcIM2eLC4PuWDq+Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796220348; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=TydDmmKVJ8OJFAM/gYGKz3EEjvTuBCZONkBy2G2mW/E=; b=SpjUoDQ0TplWFM2xQangw76wKI5BzPfY+WwspwtYI6AKgliEGM8hIWR3McqNtXthscr+ 7mdqKYGEjZi5gZpeujUKkocZwuh5GJWTP0/Kai59dH1hjDZh8O0S/3BYnAGT9kmI22dWO OeOngy2OyMK8NlHKu+6UmhiES/XSikKJFeQRRbYgN6amSqLNK/BYxVNXK4DoAWjk1WzJk t45HOfsLTnb5beMeDw5i1FvTgmOmDz0Uf2n9K8aO8aToFqsKAIqAzNQIHaYsZEZmpH96M K11SaD5X03XzzPH2Kq6vCao2m0KwiI9ZzXZOxraoiL9uJ/uENjcK4u/BTkWXCD9E8Pg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=MMVsh8tu; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A500606A329 for ; Wed, 2 Dec 2026 14:05:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=MMVsh8tu Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDEE01F00893; Wed, 2 Dec 2026 14:05:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796220347; bh=TydDmmKVJ8OJFAM/gYGKz3EEjvTuBCZONkBy2G2mW/E=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=MMVsh8tuAsCBl5Dfi7xF4aPjTExMEYJHk5D4+sY/IbICT+bpluK7qBl8HYMUHJiRm VvBU/5+umVmyoQyxsraY3WXFN+eiJZMEPGJttCDQg0I869yZ8lSkJIX1oARCQVlVwr AZcZ1ocVaNwA12luLK/J+0fBGIiVS4iMU+nnpAUyRIoxfUhKQlZaNJhhKJYUVyesmK 4faaF8nIsWR5DUkk/7YoTa8S7xeIPcPZyqyub4XYGMWDkNPj3eKF4iLq+RSW8lWEI/ Kr2cDa1BC6LGPMBJZVxktsE51SynO1WWIFcV4sMrRbAfZD0ntBxECfIF9T1RV6cCtt PUKNKUmumQIaQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v14 2/2] block: implement NVMEM provider Reply-To: sashiko-reviews@lists.linux.dev To: "Loic Poulain" Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org In-Reply-To: <20261201-block-as-nvmem-v14-2-3a07f4ff246c@oss.qualcomm.com> References: <20261201-block-as-nvmem-v14-0-3a07f4ff246c@oss.qualcomm.com> <20261201-block-as-nvmem-v14-2-3a07f4ff246c@oss.qualcomm.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 14:05:47 +0000 X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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; > =20 > 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 |=3D BLK_OPEN_EXCL; > ret =3D 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() =3D=3D false and bd_may_claim() =3D=3D true, claiming the w= hole 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 =3D priv; > + struct address_space *mapping =3D bdev->bd_mapping; > + loff_t pos =3D 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 =3D 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 =3D 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=3Dy 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 !=3D 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) > =20 > disk_del_events(disk); > =20 > + 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() =3D=3D false. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201-block-as-n= vmem-v14-0-3a07f4ff246c@oss.qualcomm.com?part=3D2