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 --- Changes in v2: - Include linux/stacktrace.h file. - Sashiko's review spotted that lo->lo_state is already Lo_unbound by the time fput() is called. Therefore, don't check lo->lo_state value, and check from only loop_handle_cmd() path. Since syzbot is reporting that kworker is finding inflight I/O, this change should still be able to catch [1] and [2] above. - I would like to introduce a kernel config option like CONFIG_DEBUG_AID_FOR_SYZBOT for gating debug code used by syzbot. But that will be a different discussion. - Not using ratelimit for reporting, for there should be only few if any when loop_handle_cmd() finds one. Not checking from loop_queue_rq() path should reduce the amount of output a lot. drivers/block/loop.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 758c20678bf6..11fbe7691367 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; +#ifdef CONFIG_KCOV + unsigned long stack_entries[30]; + int stack_nr; + pid_t pid; + char comm[TASK_COMM_LEN]; +#endif }; #define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ) @@ -1879,6 +1886,12 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); struct loop_device *lo = rq->q->queuedata; +#ifdef CONFIG_KCOV + 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 + blk_mq_start_request(rq); if (data_race(READ_ONCE(lo->lo_state)) != Lo_bound) @@ -1925,6 +1938,14 @@ static void loop_handle_cmd(struct loop_cmd *cmd) int ret = 0; struct mem_cgroup *old_memcg = NULL; +#ifdef CONFIG_KCOV + if (unlikely(disk_openers(lo->lo_disk) == 0)) { + pr_err("BUG: loop%d has no users, but backend found I/O issued by %s/%u.\n", + lo->lo_number, cmd->comm, cmd->pid); + 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; -- 2.56.0