Commit d10c8095 authored by Tejun Heo's avatar Tejun Heo Committed by Jens Axboe

writeback: implement foreign cgroup inode bdi_writeback switching

As concurrent write sharing of an inode is expected to be very rare
and memcg only tracks page ownership on first-use basis severely
confining the usefulness of such sharing, cgroup writeback tracks
ownership per-inode.  While the support for concurrent write sharing
of an inode is deemed unnecessary, an inode being written to by
different cgroups at different points in time is a lot more common,
and, more importantly, charging only by first-use can too readily lead
to grossly incorrect behaviors (single foreign page can lead to
gigabytes of writeback to be incorrectly attributed).

To resolve this issue, cgroup writeback detects the majority dirtier
of an inode and transfers the ownership to it.  The previous patches
implemented the foreign condition detection mechanism and laid the
groundwork.  This patch implements the actual switching.

With the previously implemented [unlocked_]inode_to_wb_and_list_lock()
and wb stat transaction, grabbing wb->list_lock, inode->i_lock and
mapping->tree_lock gives us full exclusion against all wb operations
on the target inode.  inode_switch_wb_work_fn() grabs all the locks
and transfers the inode atomically along with its RECLAIMABLE and
Signed-off-by: default avatarTejun Heo <>
Cc: Jens Axboe <>
Cc: Jan Kara <>
Cc: Wu Fengguang <>
Cc: Greg Thelen <>
Signed-off-by: default avatarJens Axboe <>
parent aaa2cacf
......@@ -322,30 +322,112 @@ static void inode_switch_wbs_work_fn(struct work_struct *work)
struct inode_switch_wbs_context *isw =
container_of(work, struct inode_switch_wbs_context, work);
struct inode *inode = isw->inode;
struct address_space *mapping = inode->i_mapping;
struct bdi_writeback *old_wb = inode->i_wb;
struct bdi_writeback *new_wb = isw->new_wb;
struct radix_tree_iter iter;
bool switched = false;
void **slot;
* By the time control reaches here, RCU grace period has passed
* since I_WB_SWITCH assertion and all wb stat update transactions
* between unlocked_inode_to_wb_begin/end() are guaranteed to be
* synchronizing against mapping->tree_lock.
* Grabbing old_wb->list_lock, inode->i_lock and mapping->tree_lock
* gives us exclusion against all wb related operations on @inode
* including IO list manipulations and stat updates.
if (old_wb < new_wb) {
spin_lock_nested(&new_wb->list_lock, SINGLE_DEPTH_NESTING);
} else {
spin_lock_nested(&old_wb->list_lock, SINGLE_DEPTH_NESTING);
* Once I_FREEING is visible under i_lock, the eviction path owns
* the inode and we shouldn't modify ->i_wb_list.
if (unlikely(inode->i_state & I_FREEING))
goto skip_switch;
* Count and transfer stats. Note that PAGECACHE_TAG_DIRTY points
* to possibly dirty pages while PAGECACHE_TAG_WRITEBACK points to
* pages actually under underwriteback.
radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter, 0,
struct page *page = radix_tree_deref_slot_protected(slot,
if (likely(page) && PageDirty(page)) {
__dec_wb_stat(old_wb, WB_RECLAIMABLE);
__inc_wb_stat(new_wb, WB_RECLAIMABLE);
radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter, 0,
struct page *page = radix_tree_deref_slot_protected(slot,
if (likely(page)) {
__dec_wb_stat(old_wb, WB_WRITEBACK);
__inc_wb_stat(new_wb, WB_WRITEBACK);
* Transfer to @new_wb's IO list if necessary. The specific list
* @inode was on is ignored and the inode is put on ->b_dirty which
* is always correct including from ->b_dirty_time. The transfer
* preserves @inode->dirtied_when ordering.
if (!list_empty(&inode->i_wb_list)) {
struct inode *pos;
inode_wb_list_del_locked(inode, old_wb);
inode->i_wb = new_wb;
list_for_each_entry(pos, &new_wb->b_dirty, i_wb_list)
if (time_after_eq(inode->dirtied_when,
inode_wb_list_move_locked(inode, new_wb, pos->i_wb_list.prev);
} else {
inode->i_wb = new_wb;
/* ->i_wb_frn updates may race wbc_detach_inode() but doesn't matter */
inode->i_wb_frn_winner = 0;
inode->i_wb_frn_avg_time = 0;
inode->i_wb_frn_history = 0;
switched = true;
* Paired with load_acquire in unlocked_inode_to_wb_begin() and
* ensures that the new wb is visible if they see !I_WB_SWITCH.
smp_store_release(&inode->i_state, inode->i_state & ~I_WB_SWITCH);
if (switched) {
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment