Skip to contentSkip to conversation

don't autoload block drivers on stat (and cgroup configuration)

PATCH New Christoph Hellwig 4 participants 8 messages Apr 23, 2025 – Apr 24, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi all,

Christian pointed out that the addition of the block device lookup from stat can cause the legacy driver autoload to trigger from a plain stat, which is a bad idea.

This series fixes that and also stops autoloading from blk-cgroup configuration, which isn't quite as bad but still silly.

Diffstat:

 block/bdev.c           |   17 +++++++----------
 block/blk-cgroup.c     |    2 +-
 block/blk.h            |    3 +++
 block/fops.c           |    2 +-
 include/linux/blkdev.h |    4 ----
 5 files changed, 12 insertions(+), 16 deletions(-)
#1 lore raw

PATCH 1/4 block: move blkdev_{get,put} _no_open prototypes out of blkdev.h

These are only to be used by block internal code. Remove the comment as we grew more users due to reworking block device node opening.

---
 block/blk.h            | 3 +++
 include/linux/blkdev.h | 4 ----
 2 files changed, 3 insertions(+), 4 deletions(-)
block/blk.h +3 -0
index 006e3be433d2..939e5e1aec9c 100644
@@ -94,6 +94,9 @@ static inline void blk_wait_io(struct completion *done)
 		wait_for_completion_io(done);
 }
 
+struct block_device *blkdev_get_no_open(dev_t dev);
+void blkdev_put_no_open(struct block_device *bdev);
+
 #define BIO_INLINE_VECS 4
 struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs,
 		gfp_t gfp_mask);
include/linux/blkdev.h +0 -4
index 2b8875a82ff8..557f10ad9b46 100644
@@ -1693,10 +1693,6 @@ int bd_prepare_to_claim(struct block_device *bdev, void *holder,
 		const struct blk_holder_ops *hops);
 void bd_abort_claiming(struct block_device *bdev, void *holder);
 
-/* just for blk-cgroup, don't use elsewhere */
-struct block_device *blkdev_get_no_open(dev_t dev);
-void blkdev_put_no_open(struct block_device *bdev);
-
 struct block_device *I_BDEV(struct inode *inode);
 struct block_device *file_bdev(struct file *bdev_file);
 bool disk_live(struct gendisk *disk);
Signature · 1 line
2.47.2
#2 lore raw

PATCH 2/4 block: remove the backing_inode variable in bdev_statx

In reply to Christoph Hellwig

backing_inode is only used once, so remove it and update the comment describing the bdev lookup to be a bit more clear.

---
 block/bdev.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
block/bdev.c +4 -7
index 6a34179192c9..1af9ed054f45 100644
@@ -1274,18 +1274,15 @@ void sync_bdevs(bool wait)
  */
 void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask)
 {
-	struct inode *backing_inode;
 	struct block_device *bdev;
 
-	backing_inode = d_backing_inode(path->dentry);
-
 	/*
-	 * Note that backing_inode is the inode of a block device node file,
-	 * not the block device's internal inode.  Therefore it is *not* valid
-	 * to use I_BDEV() here; the block device has to be looked up by i_rdev
+	 * Note that d_backing_inode() returns the block device node inode, not
+	 * the block device's internal inode.  Therefore it is *not* valid to
+	 * use I_BDEV() here; the block device has to be looked up by i_rdev
 	 * instead.
 	 */
-	bdev = blkdev_get_no_open(backing_inode->i_rdev);
+	bdev = blkdev_get_no_open(d_backing_inode(path->dentry)->i_rdev);
 	if (!bdev)
 		return;
 
Signature · 1 line
2.47.2
#3 lore raw

PATCH 3/4 block: don't autoload drivers on stat

In reply to Christoph Hellwig

blkdev_get_no_open can trigger the legacy autoload of block drivers. A simple stat of a block device has not historically done that, so disable this behavior again.

Fixes 9abcfbd235f5 ("block: Add atomic write support for statx")
---
 block/bdev.c       | 8 ++++----
 block/blk-cgroup.c | 2 +-
 block/blk.h        | 2 +-
 block/fops.c       | 2 +-
 4 files changed, 7 insertions(+), 7 deletions(-)
block/bdev.c +4 -4
index 1af9ed054f45..3ebad8e953e1 100644
@@ -777,13 +777,13 @@ static void blkdev_put_part(struct block_device *part)
 	blkdev_put_whole(whole);
 }
 
-struct block_device *blkdev_get_no_open(dev_t dev)
+struct block_device *blkdev_get_no_open(dev_t dev, bool autoload)
 {
 	struct block_device *bdev;
 	struct inode *inode;
 
 	inode = ilookup(blockdev_superblock, dev);
-	if (!inode && IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD)) {
+	if (!inode && autoload && IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD)) {
 		blk_request_module(dev);
 		inode = ilookup(blockdev_superblock, dev);
 		if (inode)
@@ -1005,7 +1005,7 @@ struct file *bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
 	if (ret)
 		return ERR_PTR(ret);
 
-	bdev = blkdev_get_no_open(dev);
+	bdev = blkdev_get_no_open(dev, true);
 	if (!bdev)
 		return ERR_PTR(-ENXIO);
 
@@ -1282,7 +1282,7 @@ void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask)
 	 * use I_BDEV() here; the block device has to be looked up by i_rdev
 	 * instead.
 	 */
-	bdev = blkdev_get_no_open(d_backing_inode(path->dentry)->i_rdev);
+	bdev = blkdev_get_no_open(d_backing_inode(path->dentry)->i_rdev, false);
 	if (!bdev)
 		return;
 
block/blk-cgroup.c +1 -1
index 5905f277057b..e172aeda4183 100644
@@ -797,7 +797,7 @@ int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx)
 		return -EINVAL;
 	input = skip_spaces(input);
 
-	bdev = blkdev_get_no_open(MKDEV(major, minor));
+	bdev = blkdev_get_no_open(MKDEV(major, minor), true);
 	if (!bdev)
 		return -ENODEV;
 	if (bdev_is_partition(bdev)) {
block/blk.h +1 -1
index 939e5e1aec9c..328075787814 100644
@@ -94,7 +94,7 @@ static inline void blk_wait_io(struct completion *done)
 		wait_for_completion_io(done);
 }
 
-struct block_device *blkdev_get_no_open(dev_t dev);
+struct block_device *blkdev_get_no_open(dev_t dev, bool autoload);
 void blkdev_put_no_open(struct block_device *bdev);
 
 #define BIO_INLINE_VECS 4
block/fops.c +1 -1
index be9f1dbea9ce..d23ddb2dc113 100644
@@ -642,7 +642,7 @@ static int blkdev_open(struct inode *inode, struct file *filp)
 	if (ret)
 		return ret;
 
-	bdev = blkdev_get_no_open(inode->i_rdev);
+	bdev = blkdev_get_no_open(inode->i_rdev, true);
 	if (!bdev)
 		return -ENXIO;
 
Signature · 1 line
2.47.2
#4 lore raw

PATCH 4/4 block: don't autoload drivers on blk-cgroup configuration

In reply to Christoph Hellwig

Loading a driver just to configure blk-cgroup doesn't make sense, as that assumes and already existing device.

---
 block/blk-cgroup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-cgroup.c +1 -1
index e172aeda4183..ce93706555c5 100644
@@ -797,7 +797,7 @@ int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx)
 		return -EINVAL;
 	input = skip_spaces(input);
 
-	bdev = blkdev_get_no_open(MKDEV(major, minor), true);
+	bdev = blkdev_get_no_open(MKDEV(major, minor), false);
 	if (!bdev)
 		return -ENODEV;
 	if (bdev_is_partition(bdev)) {
Signature · 1 line
2.47.2
maintainer Acked-by
#5 lore raw
In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig

Hi all,

Christian pointed out that the addition of the block device lookup from stat can cause the legacy driver autoload to trigger from a plain stat, which is a bad idea.

This series fixes that and also stops autoloading from blk-cgroup configuration, which isn't quite as bad but still silly.

Thanks.

Signature · 1 line
tejun
Reviewed-by
#6 lore raw
In reply to Christoph Hellwig
Quoted 3 lines · Christoph Hellwig

Hi all,

Christian pointed out that the addition of the block device lookup

Thank you! And sorry I just sent you a mail complaining about this again. I didn't see this patch series until just now. Thanks for doing this!

Quoted 13 lines

from stat can cause the legacy driver autoload to trigger from a plain stat, which is a bad idea.

This series fixes that and also stops autoloading from blk-cgroup configuration, which isn't quite as bad but still silly.

Diffstat:

 block/bdev.c           |   17 +++++++----------
 block/blk-cgroup.c     |    2 +-
 block/blk.h            |    3 +++
 block/fops.c           |    2 +-
 include/linux/blkdev.h |    4 ----
 5 files changed, 12 insertions(+), 16 deletions(-)
maintainer Applied
#7 lore raw
In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig

Christian pointed out that the addition of the block device lookup from stat can cause the legacy driver autoload to trigger from a plain stat, which is a bad idea.

This series fixes that and also stops autoloading from blk-cgroup configuration, which isn't quite as bad but still silly.

[...]

Applied, thanks!

[1/4] block: move blkdev_{get,put} _no_open prototypes out of blkdev.h

      commit: c63202140d4b411d27380805c4d68eb11407b7f2
[2/4] block: remove the backing_inode variable in bdev_statx
      commit: d13b7090b2510abaa83a25717466decca23e8226
[3/4] block: don't autoload drivers on stat
      commit: 5f33b5226c9d92359e58e91ad0bf0c1791da36a1
[4/4] block: don't autoload drivers on blk-cgroup configuration
      commit: c4d2519c6ad854dc2114e77d693b3cf1baf55330

Best regards,

Signature · 1 line
Jens Axboe