Offline memory cgroups forbids creation new bdi_writebacks. Each try wastes cpu cycles and increases contention around cgwb_lock. For example each O_DIRECT read calls filemap_write_and_wait_range() if inode has cached pages which tries to switch from dying writeback. This patch switches inode writeback to closest online parent cgroup. Fixes: e8a7abf5a5bd ("writeback: disassociate inodes from dying bdi_writebacks") Signed-off-by: Konstantin Khlebnikov <khlebnikov@xxxxxxxxxxxxxx> --- fs/fs-writeback.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c index 542b02d170f8..3af44591a106 100644 --- a/fs/fs-writeback.c +++ b/fs/fs-writeback.c @@ -505,7 +505,7 @@ static void inode_switch_wbs(struct inode *inode, int new_wb_id) /* find and pin the new wb */ rcu_read_lock(); memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys); - if (memcg_css) + if (memcg_css && (memcg_css->flags & CSS_ONLINE)) isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); rcu_read_unlock(); if (!isw->new_wb) @@ -579,9 +579,16 @@ void wbc_attach_and_unlock_inode(struct writeback_control *wbc, /* * A dying wb indicates that the memcg-blkcg mapping has changed * and a new wb is already serving the memcg. Switch immediately. + * If memory cgroup is offline switch to closest online parent. */ - if (unlikely(wb_dying(wbc->wb))) - inode_switch_wbs(inode, wbc->wb_id); + if (unlikely(wb_dying(wbc->wb))) { + struct cgroup_subsys_state *memcg_css = wbc->wb->memcg_css; + + while (!(memcg_css->flags & CSS_ONLINE)) + memcg_css = memcg_css->parent; + + inode_switch_wbs(inode, memcg_css->id); + } } EXPORT_SYMBOL_GPL(wbc_attach_and_unlock_inode);