bdev_file_open_by_path() checks whether the device is read-only after bdev_file_open_by_dev() has already claimed it for the caller's holder. On this error path, fput() can defer ->release() until after the caller gets -EACCES. A different holder can still get -EBUSY, and the block layer can still call ->mark_dead() after the caller has freed its holder state. The caller receives no file with which to release the claim. Use bdev_fput() to drop the claim acquired by this open before returning -EACCES. The final file release can still be deferred. Fixes: f3a608827d1f ("bdev: open block device as files") Signed-off-by: Gleb Chesnokov --- Tested in QEMU with two back-to-back opens of a read-only RAM disk from the same task. The read/write open returned -EACCES on both kernels. The read open with a different holder returned -EBUSY before the fix and succeeded with it. OpenAI Codex assisted with testing. block/bdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/bdev.c b/block/bdev.c index f20f91b3176e..add1a69f2f4a 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -1127,7 +1127,7 @@ struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode, file = bdev_file_open_by_dev(dev, mode, holder, hops); if (!IS_ERR(file) && (mode & BLK_OPEN_WRITE)) { if (bdev_read_only(file_bdev(file))) { - fput(file); + bdev_fput(file); file = ERR_PTR(-EACCES); } } base-commit: 25faec9ce61c01fb45abf812a222c5b43c3cd5c5 -- 2.56.1