public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Tejun Heo <tj@kernel.org>
To: Jan Kara <jack@suse.cz>
Cc: Christian Brauner <brauner@kernel.org>,
	linux-fsdevel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] writeback: Fix use after free in inode_switch_wbs_work_fn()
Date: Mon, 13 Apr 2026 06:30:43 -1000	[thread overview]
Message-ID: <ad0aM1nN4j2I9Zh_@slm.duckdns.org> (raw)
In-Reply-To: <20260413093618.17244-2-jack@suse.cz>

On Mon, Apr 13, 2026 at 11:36:19AM +0200, Jan Kara wrote:
> 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>

Oh man, that's too subtle.

Acked-by: Tejun Heo <tj@kernel.org>

Thanks.

-- 
tejun

  reply	other threads:[~2026-04-13 16:30 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-13  9:36 [PATCH] writeback: Fix use after free in inode_switch_wbs_work_fn() Jan Kara
2026-04-13 16:30 ` Tejun Heo [this message]
2026-04-13 17:26   ` Jan Kara
2026-04-14 10:02 ` Christian Brauner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ad0aM1nN4j2I9Zh_@slm.duckdns.org \
    --to=tj@kernel.org \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox