]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
writeback: Fix use after free in inode_switch_wbs_work_fn()
authorJan Kara <jack@suse.cz>
Mon, 13 Apr 2026 09:36:19 +0000 (11:36 +0200)
committerChristian Brauner <brauner@kernel.org>
Thu, 23 Apr 2026 22:34:58 +0000 (00:34 +0200)
inode_switch_wbs_work_fn() has a loop like:

  wb_get(new_wb);
  while (1) {
    list = llist_del_all(&new_wb->switch_wbs_ctxs);
    /* Nothing to do? */
    if (!list)
      break;
    ... process the items ...
  }

Now adding of items to the list looks like:

wb_queue_isw()
  if (llist_add(&isw->list, &wb->switch_wbs_ctxs))
    queue_work(isw_wq, &wb->switch_work);

Because inode_switch_wbs_work_fn() loops when processing isw items, it
can happen that wb->switch_work is pending while wb->switch_wbs_ctxs is
empty. This is a problem because in that case wb can get freed (no isw
items -> no wb reference) while the work is still pending causing
use-after-free issues.

We cannot just fix this by cancelling work when freeing wb because that
could still trigger problematic 0 -> 1 transitions on wb refcount due to
wb_get() in inode_switch_wbs_work_fn(). It could be all handled with
more careful code but that seems unnecessarily complex so let's avoid
that until it is proven that the looping actually brings practical
benefit. Just remove the loop from inode_switch_wbs_work_fn() instead.
That way when wb_queue_isw() queues work, we are guaranteed we have
added the first item to wb->switch_wbs_ctxs and nobody is going to
remove it (and drop the wb reference it holds) until the queued work
runs.

Fixes: e1b849cfa6b6 ("writeback: Avoid contention on wb->list_lock when switching inodes")
CC: stable@vger.kernel.org
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/20260413093618.17244-2-jack@suse.cz
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/fs-writeback.c

index 3c75ee025bdaf97985ca7f2090e4e0b5bb05205f..d63baa1b6fecf7e353d85d4365873783c6e92d01 100644 (file)
@@ -570,28 +570,30 @@ void inode_switch_wbs_work_fn(struct work_struct *work)
        struct inode_switch_wbs_context *isw, *next_isw;
        struct llist_node *list;
 
+       list = llist_del_all(&new_wb->switch_wbs_ctxs);
        /*
-        * Grab out reference to wb so that it cannot get freed under us
+        * Nothing to do? That would be a problem as references held by isw
+        * items protect wb from freeing...
+        */
+       if (WARN_ON_ONCE(!list))
+               return;
+
+       /*
+        * Grab our reference to wb so that it cannot get freed under us
         * after we process all the isw items.
         */
        wb_get(new_wb);
-       while (1) {
-               list = llist_del_all(&new_wb->switch_wbs_ctxs);
-               /* Nothing to do? */
-               if (!list)
-                       break;
-               /*
-                * In addition to synchronizing among switchers, I_WB_SWITCH
-                * tells the RCU protected stat update paths to grab the i_page
-                * lock so that stat transfer can synchronize against them.
-                * Let's continue after I_WB_SWITCH is guaranteed to be
-                * visible.
-                */
-               synchronize_rcu();
+       /*
+        * In addition to synchronizing among switchers, I_WB_SWITCH
+        * tells the RCU protected stat update paths to grab the i_page
+        * lock so that stat transfer can synchronize against them.
+        * Let's continue after I_WB_SWITCH is guaranteed to be
+        * visible.
+        */
+       synchronize_rcu();
 
-               llist_for_each_entry_safe(isw, next_isw, list, list)
-                       process_inode_switch_wbs(new_wb, isw);
-       }
+       llist_for_each_entry_safe(isw, next_isw, list, list)
+               process_inode_switch_wbs(new_wb, isw);
        wb_put(new_wb);
 }