From 6eedc2b4058e256edcb2e004e0db3a154425452e Mon Sep 17 00:00:00 2001 From: Tetsuo Handa Date: Mon, 12 Oct 2026 17:26:05 +0900 Subject: [PATCH] block: Wait for inflight requests in bdev_release() 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 --- block/bdev.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/block/bdev.c b/block/bdev.c index f20f91b3176e..0567acd757e4 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -1170,6 +1170,19 @@ void bdev_release(struct file *bdev_file) if (atomic_read(&bdev->bd_openers) == 1) sync_blockdev(bdev); + /* + * Always wait for all inflight read/write requests to complete. + * + * We can't omit this wait when it does not look like we're the last + * one, for it is possible that two processes concurrently call + * bdev_release() and see atomic_read(&bdev->bd_openers) > 1 and then + * sequentially call blkdev_put_whole() with disk->open_mutex held. + * + * Also, we can't wait with disk->open_mutex held because there is + * possibility of deadlock. + */ + blk_mq_unfreeze_queue(bdev->bd_queue, blk_mq_freeze_queue(bdev->bd_queue)); + mutex_lock(&disk->open_mutex); bdev_yield_write_access(bdev_file);