Re: [PATCH v7 3/3] writeback: switch a replaced cgwb's inodes to its successor

From: Tejun Heo

Date: Fri Oct 16 2026 - 18:46:00 EST


On Thu, Oct 15, 2026 at 11:40:49AM +0000, Liz Fong-Jones wrote:
> When a live memcg's blkcg association changes, cgwb_create() replaces
> its killed wb in bdi->cgwb_tree. Foreign flushes for the memcg then
> find the new wb, but the inodes, dirty or not, are still attached to the
> old one, and they only move over as they are written back (see
> wbc_attach_and_unlock_inode()). Until then, other memcgs appending to
> those inodes stall in balance_dirty_pages().
>
> After the takeover, queue a work item that switches all inodes on the
> old wb's lists to the wb foreign flushes now find, like
> cleanup_offline_cgwb() does for b_attached and b_dirty_time, but
> including b_dirty, b_io and b_more_io. The switch carries the dirty and
> writeback page counts, so nothing needs to be written back first.
> cgwb_create() can run with interrupts disabled (folio_account_dirtied()
> -> inode_attach_wb()), so it only queues the work. A switch to the old
> wb that was queued before it was replaced can land after the work ran,
> so inode_switch_wbs_work_fn() queues the work again when the wb it
> switched inodes to is dying and no longer owns its slot. Inodes attached
> to the old wb but on none of its lists, such as one attached as the wb
> is replaced (see __inode_attach_wb()) or a clean one switched over from
> the root wb, still move over only when written back. If the successor is
> already gone, fall back like cleanup_offline_cgwb() does, to the nearest
> live ancestor's wb or the root wb.
>
> Since commit f6988c90671e ("writeback: bound cleanup_offline_cgwb()
> rescans by rotating scanned inodes"), the scan moves the inodes it walks
> to the newest end of each list, so hold wb_switch_rwsem for read across
> it, like the switches themselves. Otherwise a sync that has fixed its
> cutoff but not yet queued inodes can find older dirty inodes moved
> behind a newer one on b_dirty and return without writing them.
>
> Test: a live cgroup appends to 5000 files on XFS at 250MiB/s, io is
> disabled on its parent so its wb is killed, it creates a file (the
> takeover), and a sibling keeps appending to its files under a 4G parent
> limit. Worst lag of the sibling behind schedule over 30s, in three runs:
>
> without this patch 9.1s 4.0s 1.0s
> with this patch 1.7s 1.3s 1.0s
>
> Without this patch, moving the old wb's inodes over took up to 11s,
> first switch to last; with it, all 5001 were switched in a 23-35ms
> window, with up to 98MiB of the old cgroup's pages dirty.
>
> Suggested-by: Tejun Heo <tj@xxxxxxxxxx>
> Assisted-by: Claude:claude-opus-5-5 checkpatch sparse
> Assisted-by: Claude:claude-fable-5-1
> Reviewed-by: Jan Kara <jack@xxxxxxx>
> Signed-off-by: Liz Fong-Jones <lizf@xxxxxxxxxxxx>

Acked-by: Tejun Heo <tj@xxxxxxxxxx>

Thanks.

--
tejun