From 3421db93ed76ea57311e543c1454ebee1ac9c438 Mon Sep 17 00:00:00 2001 From: Tetsuo Handa Date: Sun, 11 Oct 2026 22:26:18 +0900 Subject: [PATCH] loop: Detect write requests that failed to wait for completion. This is a debug patch which will be reverted after we observed what is happening. This patch does not attempt to fix the problem itself. syzbot is reporting NULL pointer dereference in lo_rw_aio() [1][2]. An analysis by the Gemini AI collaborator [3] considers that this problem is caused by a timing shift primarily exposed by commit 65565ca5f99b ("block: unify the synchronous bi_end_io callbacks"), along with helper refactorings like commit 92c3737a2473 ("block: add a bio_submit_or_kill helper"). But due to difficulty of reproducing this race, discussion about what is happening and how to fix this problem is stalling. Also, we haven't identified how many filesystems are subjected to this problem. Therefore, we need to proceed to debug printk() approach, for syzbot cannot find a reproducer for this problem. Another analysis by the Gemini AI collaborator found possibility of sync_blockdev() not waiting for write requests due to skipping __filemap_fdatawait_range() when filemap_fdatawrite_range() returned -EIO. Step 1: A test process is created and calls unshare(). Step 2: That process extracts a crafted JFS image to a memfd storage. Step 3: That process binds that image to a loop device. Step 4: That process mounts that loop device as read-write. Step 5: That process issues some buffered write requests. Step 6: That process is killed before writeback for Step 5 starts. Step 7: Automatic unmount operation starts due to teardown of a mount namespace. Step 8: jfs_umount() calls filemap_write_and_wait() (directly or via sync_blockdev()). Step 9: filemap_fdatawrite_range() returns -EIO due to filesystem errors after several submit_bio() have been already issued. Step 10: jfs_umount() completes without calling __filemap_fdatawait_range() which should wait for completion of I/O requests issued by Step 5 due to Step 9. Step 11: fput() is called from __loop_clr_fd() from lo_release(), and destruction of "struct file" starts after returning from lo_release(). Step 12: The in-flight I/O request issued by Step 9 is finally processed by the loop backend kworker. However, since the backing "struct file" (and its associated structures) is already under destruction, the kworker attempts to dereference now-invalid fields, leading to WARN_ON_ONCE(). In order to check whether the steps described above is actually happening, add debug code to the loop module for reporting I/O requests that are processed after __loop_clr_fd() started. Note that this debug code is enabled only if CONFIG_KCOV=y, for this check is a waste of computation resources for almost all users. Link: https://syzkaller.appspot.com/bug?extid=cd8a9a308e879a4e2c28 [1] Link: https://syzkaller.appspot.com/bug?extid=bc273027d5643e48e5b3 [2] Link: https://lkml.kernel.org/r/fbb3edda-f108-4e5b-acf2-266f043f8125@I-love.SAKURA.ne.jp [3] Assisted-by: Gemini-Pro Signed-off-by: Tetsuo Handa --- drivers/block/loop.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index a3325ceab484..974e7f182570 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -36,6 +36,7 @@ #include #include #include +#include /* Possible states of device */ enum { @@ -86,6 +87,12 @@ struct loop_cmd { struct bio_vec *bvec; struct cgroup_subsys_state *blkcg_css; struct cgroup_subsys_state *memcg_css; +#if defined(CONFIG_KCOV) && defined(CONFIG_STACKTRACE) + unsigned long stack_entries[30]; + int stack_nr; + pid_t pid; + char comm[TASK_COMM_LEN]; +#endif }; #define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ) @@ -1808,6 +1815,13 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, #endif } #endif + +#if defined(CONFIG_KCOV) && defined(CONFIG_STACKTRACE) + cmd->stack_nr = stack_trace_save(cmd->stack_entries, ARRAY_SIZE(cmd->stack_entries), 0); + cmd->pid = current->pid; + get_task_comm(cmd->comm, current); +#endif + loop_queue_work(lo, cmd); return BLK_STS_OK; @@ -1823,6 +1837,21 @@ static void loop_handle_cmd(struct loop_cmd *cmd) int ret = 0; struct mem_cgroup *old_memcg = NULL; +#if defined(CONFIG_KCOV) && defined(CONFIG_STACKTRACE) + /* + * This request was queued after checking lo->lo_state == Lo_bound + * in loop_queue_rq(). The state should remain unchanged unless + * __loop_clr_fd() by error starts before blk_mq_complete_request() + * is called. + */ + if (unlikely(data_race(READ_ONCE(lo->lo_state)) != Lo_bound && + disk_openers(lo->lo_disk) == 0)) { + pr_err("BUG: found I/O issued by %s/%u but loop%d is no longer bound.\n", + cmd->comm, cmd->pid, lo->lo_number); + printk("Call Trace:\n"); + stack_trace_print(cmd->stack_entries, cmd->stack_nr, 4); + } +#endif if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { ret = -EIO; goto failed;