syzbot is reporting a NULL pointer dereference [1] and invalid reading of destroyed "struct file" [2] in lo_rw_aio() within the loop driver. Also, syzbot-ci is reporting read attempt on a no-longer bound loop device [3]. These reports are caused by current implementation that a block device's backing storage and associated structures (such as "struct file") are invalidated before all outstanding asynchronous or file-system-driven I/O requests have fully completed. Investigation revealed that filesystems can leak outstanding non-blocking Read/Write requests during error-rollbacks in the mount failure path or during standard unmount sequences: 1. Write Leaks via -EIO: When filemap_fdatawrite_range() returns an error (such as -EIO due to filesystem errors), filemap_write_and_wait_range() skips the subsequent __filemap_fdatawait_range() call. As a result, sync_blockdev() returns prematurely without waiting for the completion of write requests already sitting in flight. 2. Read Leaks via Readahead: Asynchronous metadata readahead requests are entirely ignored by sync_blockdev(), allowing ongoing Read I/O to bypass data synchronization entirely. When these leaked requests later complete or attempt backend processing via loop's kworker (loop_handle_cmd()), they dereference now-invalid or nullified fields. While earlier attempts tried to address this regression inside the loop driver by temporarily releasing disk->open_mutex [4] or proposing a core layer post_release() callback [5], these approaches were rejected because they either introduced invasive driver-specific hooks into the block layer or broke the core mutual exclusion semantics that the block layer relies upon. To fix this comprehensively without introducing deadlocks or altering driver-specific semantics, enforce an explicit queue freeze in the core VFS close path inside bdev_release(), right before disk->open_mutex is acquired: 1. Lock-Free Congestion Clearing: Calling blk_mq_freeze_queue() before acquiring disk->open_mutex avoids any circular locking dependencies (disk->open_mutex => wq_completion => VFS locks => disk->open_mutex), guaranteeing that the lockdep hierarchy remains perfectly undisturbed. 2. Guaranteed Quenching: Freezing the queue ensures that all outstanding asynchronous Direct I/O (cmd->use_aio != 0) and page-cache readahead requests are physically completed and drained before the loop device proceeds to teardown and unbind the backing file via fput(). This architecture-compliant fix should be able to ensure data integrity and lifecycle synchronization across all virtual block devices without muddying block layer locks. Reported-by: syzbot+cd8a9a308e879a4e2c28@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=cd8a9a308e879a4e2c28 [1] Reported-by: syzbot+bc273027d5643e48e5b3@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=bc273027d5643e48e5b3 [2] Link: https://lkml.kernel.org/r/6acbe268.8cdf6287.1eb53.00ed.GAE@google.com [3] Link: https://lkml.kernel.org/r/8dedfc40-9cae-44ff-9960-e0eb1825e963@I-love.SAKURA.ne.jp [4] Link: https://lkml.kernel.org/r/69960302-1535-441a-be4e-d652766d65c2@I-love.SAKURA.ne.jp [5] Assisted-by: Gemini-Pro Signed-off-by: Tetsuo Handa --- Notes on why this cannot and should not be fixed on the filesystem side: (1) In filemap_fdatawrite_range(), BIOs are submitted before -EIO is returned. (2) The comment in filemap_write_and_wait_range() states that we don't wait for submitted BIOs when filemap_fdatawrite_range() returned -EIO. (3) As a result, sync_blockdev() does not wait for block layer's queue to become empty when filemap_fdatawrite_range() returned -EIO. (4) As a result, we need to wait for block layer's queue to become empty (by calling freeze/unfreeze API) between sync_blockdev() and lo_release() if we want to make sure that there is no inflight BIOs when lo_release() is called. (5) Modifying individual filesystems does not help, for sync_blockdev() from bdev_release() might append inflight BIOs. It will introduce several architectural and stability issues if we shift the responsibility to individual filesystems to explicitly freeze/unfreeze the queue: (a) Violation of Layer Separation: Filesystems should remain agnostic to whether the underlying block device is a physical hardware disk or a virtual loop device. Forcing filesystems to handle this layer-specific quirk violates VFS/block layer separation. (b) Risk of Double Freeze / Deadlocks: Filesystem layer should handle only filesystem level freezing, and block layer should handle only block level freezing. (c) Ownership of the Device Lifecycle: Quenching inflight I/O before destroying a device's context is the fundamental responsibility of the layer managing the device's lifecycle. Since bdev_release() is the final gatekeeper before __loop_clr_fd() nullifies the backing file, the block core (before releasing open_mutex) or the loop module (after releasing open_mutex) must enforce this last line of defense. block/bdev.c | 38 +++++++++++++++++++++++++++++++- drivers/block/loop.c | 1 + include/linux/blkdev.h | 1 + rust/kernel/block/mq/gen_disk.rs | 1 + 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/block/bdev.c b/block/bdev.c index a4065e16544d..3d5292fa4432 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -753,7 +753,9 @@ static void bd_end_claim(struct block_device *bdev, void *holder) static void blkdev_flush_mapping(struct block_device *bdev) { WARN_ON_ONCE(bdev->bd_holders); - sync_blockdev(bdev); + /* See bd_early_sync() for this behavior. */ + if (!bdev->bd_disk->fops->sync_queue_before_release) + sync_blockdev(bdev); kill_bdev(bdev); bdev_write_inode(bdev); } @@ -1180,11 +1182,45 @@ static inline void bd_early_sync(struct gendisk *disk, struct block_device *bdev * Therefore, this bdev->bd_openers == 1 + bdev->bd_closers condition * guarantees that any thread who might become the last opener performs * synchronization which should be done before holding open_mutex. + * + * Also, wait for all in-flight BIOs to complete if + * sync_queue_before_release is set, due to reasons listed below. + * + * (1) sync_blockdev() fails to wait for completion of writeback when + * filemap_fdatawrite_range() returned -EIO after some write BIOs + * have been submitted. Also, sync_blockdev() does not wait for + * completion of read BIOs submitted by e.g. filesystem's readahead + * behavior. To wait for completion of already submitted BIOs, we + * need to freeze block layer's queue which we are about to close. + * (2) At least, lo_release() (one of bdev->bd_disk->fops->release() + * targets) depends on that there is no in-flight BIO when + * lo_release() is called. If this dependency is broken, the kernel + * crashes due to lo_rw_aio() trying to process in-flight BIOs + * after blkdev_flush_mapping() and lo_release() are called. + * (3) lo_release() is not allowed to freeze block layer's queue due to + * circular locking dependency which involves open_mutex. Also, + * lo_release() is required to complete fput() from __loop_clr_fd() + * before lo_release() returns to usermode, for existing userspace + * programs depend on such behavior. These constraints make it + * impossible for lo_release() to defer queue freezing and fput() + * to delayed context such as WQ or task work. + * (4) If blkdev_flush_mapping() submits new BIOs, there might be + * in-flight BIOs when bdev->bd_disk->fops->release() is called. + * Since lo_release() cannot accept such possibility, we freeze + * queue here and forbid further submission of BIOs after we + * unfreeze queue and re-acquires open_mutex. + * (5) Whether to wait for all in-flight BIOs to complete is an opt-in + * basis switch, for this behavior is required by loop module which + * cannot freeze queue with open_mutex held due to circular locking + * dependency. */ if (atomic_read(&bdev->bd_openers) == 1 + bdev->bd_closers) { bdev->bd_closers++; mutex_unlock(&disk->open_mutex); sync_blockdev(bdev); + if (disk->fops->sync_queue_before_release) + blk_mq_unfreeze_queue(bdev->bd_queue, + blk_mq_freeze_queue(bdev->bd_queue)); mutex_lock(&disk->open_mutex); bdev->bd_closers--; } diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 758c20678bf6..5104cc9b9253 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1796,6 +1796,7 @@ static const struct block_device_operations lo_fops = { .compat_ioctl = lo_compat_ioctl, #endif .free_disk = lo_free_disk, + .sync_queue_before_release = true, }; /* diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index 4f7905c3412b..dcb08337518a 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1598,6 +1598,7 @@ struct block_device_operations { enum blk_unique_id id_type); struct module *owner; const struct pr_ops *pr_ops; + bool sync_queue_before_release; /* * Special callback for probing GPT entry at a given sector. diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_disk.rs index fc97dd873974..fa554e5b2c18 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -145,6 +145,7 @@ pub fn build( pr_ops: core::ptr::null_mut(), free_disk: None, poll_bio: None, + sync_queue_before_release: false, }; // SAFETY: `gendisk` is a valid pointer as we initialized it above -- 2.56.0