Skip to content

Commit c22d70a

Browse files
rgushchintorvalds
authored andcommitted
writeback, cgroup: release dying cgwbs by switching attached inodes
Asynchronously try to release dying cgwbs by switching attached inodes to the nearest living ancestor wb. It helps to get rid of per-cgroup writeback structures themselves and of pinned memory and block cgroups, which are significantly larger structures (mostly due to large per-cpu statistics data). This prevents memory waste and helps to avoid different scalability problems caused by large piles of dying cgroups. Reuse the existing mechanism of inode switching used for foreign inode detection. To speed things up batch up to 115 inode switching in a single operation (the maximum number is selected so that the resulting struct inode_switch_wbs_context can fit into 1024 bytes). Because every switching consists of two steps divided by an RCU grace period, it would be too slow without batching. Please note that the whole batch counts as a single operation (when increasing/decreasing isw_nr_in_flight). This allows to keep umounting working (flush the switching queue), however prevents cleanups from consuming the whole switching quota and effectively blocking the frn switching. A cgwb cleanup operation can fail due to different reasons (e.g. not enough memory, the cgwb has an in-flight/pending io, an attached inode in a wrong state, etc). In this case the next scheduled cleanup will make a new attempt. An attempt is made each time a new cgwb is offlined (in other words a memcg and/or a blkcg is deleted by a user). In the future an additional attempt scheduled by a timer can be implemented. [[email protected]: replace open-coded "115" with arithmetic] Link: https://lkml.kernel.org/r/YMEcSBcq/[email protected] [[email protected]: add smp_mb() to inode_prepare_wbs_switch()] Link: https://lkml.kernel.org/r/[email protected] [[email protected]: fix documentation] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Roman Gushchin <[email protected]> Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Acked-by: Tejun Heo <[email protected]> Acked-by: Dennis Zhou <[email protected]> Reviewed-by: Jan Kara <[email protected]> Cc: Alexander Viro <[email protected]> Cc: Dave Chinner <[email protected]> Cc: Jan Kara <[email protected]> Cc: Jens Axboe <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent f5fbe6b commit c22d70a

File tree

4 files changed

+165
-12
lines changed

4 files changed

+165
-12
lines changed

fs/fs-writeback.c

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ void wb_wait_for_completion(struct wb_completion *done)
225225
/* one round can affect upto 5 slots */
226226
#define WB_FRN_MAX_IN_FLIGHT 1024 /* don't queue too many concurrently */
227227

228+
/*
229+
* Maximum inodes per isw. A specific value has been chosen to make
230+
* struct inode_switch_wbs_context fit into 1024 bytes kmalloc.
231+
*/
232+
#define WB_MAX_INODES_PER_ISW ((1024UL - sizeof(struct inode_switch_wbs_context)) \
233+
/ sizeof(struct inode *))
234+
228235
static atomic_t isw_nr_in_flight = ATOMIC_INIT(0);
229236
static struct workqueue_struct *isw_wq;
230237

@@ -503,6 +510,32 @@ static void inode_switch_wbs_work_fn(struct work_struct *work)
503510
atomic_dec(&isw_nr_in_flight);
504511
}
505512

513+
static bool inode_prepare_wbs_switch(struct inode *inode,
514+
struct bdi_writeback *new_wb)
515+
{
516+
/*
517+
* Paired with smp_mb() in cgroup_writeback_umount().
518+
* isw_nr_in_flight must be increased before checking SB_ACTIVE and
519+
* grabbing an inode, otherwise isw_nr_in_flight can be observed as 0
520+
* in cgroup_writeback_umount() and the isw_wq will be not flushed.
521+
*/
522+
smp_mb();
523+
524+
/* while holding I_WB_SWITCH, no one else can update the association */
525+
spin_lock(&inode->i_lock);
526+
if (!(inode->i_sb->s_flags & SB_ACTIVE) ||
527+
inode->i_state & (I_WB_SWITCH | I_FREEING | I_WILL_FREE) ||
528+
inode_to_wb(inode) == new_wb) {
529+
spin_unlock(&inode->i_lock);
530+
return false;
531+
}
532+
inode->i_state |= I_WB_SWITCH;
533+
__iget(inode);
534+
spin_unlock(&inode->i_lock);
535+
536+
return true;
537+
}
538+
506539
/**
507540
* inode_switch_wbs - change the wb association of an inode
508541
* @inode: target inode
@@ -540,17 +573,8 @@ static void inode_switch_wbs(struct inode *inode, int new_wb_id)
540573
if (!isw->new_wb)
541574
goto out_free;
542575

543-
/* while holding I_WB_SWITCH, no one else can update the association */
544-
spin_lock(&inode->i_lock);
545-
if (!(inode->i_sb->s_flags & SB_ACTIVE) ||
546-
inode->i_state & (I_WB_SWITCH | I_FREEING | I_WILL_FREE) ||
547-
inode_to_wb(inode) == isw->new_wb) {
548-
spin_unlock(&inode->i_lock);
576+
if (!inode_prepare_wbs_switch(inode, isw->new_wb))
549577
goto out_free;
550-
}
551-
inode->i_state |= I_WB_SWITCH;
552-
__iget(inode);
553-
spin_unlock(&inode->i_lock);
554578

555579
isw->inodes[0] = inode;
556580

@@ -571,6 +595,73 @@ static void inode_switch_wbs(struct inode *inode, int new_wb_id)
571595
kfree(isw);
572596
}
573597

598+
/**
599+
* cleanup_offline_cgwb - detach associated inodes
600+
* @wb: target wb
601+
*
602+
* Switch all inodes attached to @wb to a nearest living ancestor's wb in order
603+
* to eventually release the dying @wb. Returns %true if not all inodes were
604+
* switched and the function has to be restarted.
605+
*/
606+
bool cleanup_offline_cgwb(struct bdi_writeback *wb)
607+
{
608+
struct cgroup_subsys_state *memcg_css;
609+
struct inode_switch_wbs_context *isw;
610+
struct inode *inode;
611+
int nr;
612+
bool restart = false;
613+
614+
isw = kzalloc(sizeof(*isw) + WB_MAX_INODES_PER_ISW *
615+
sizeof(struct inode *), GFP_KERNEL);
616+
if (!isw)
617+
return restart;
618+
619+
atomic_inc(&isw_nr_in_flight);
620+
621+
for (memcg_css = wb->memcg_css->parent; memcg_css;
622+
memcg_css = memcg_css->parent) {
623+
isw->new_wb = wb_get_create(wb->bdi, memcg_css, GFP_KERNEL);
624+
if (isw->new_wb)
625+
break;
626+
}
627+
if (unlikely(!isw->new_wb))
628+
isw->new_wb = &wb->bdi->wb; /* wb_get() is noop for bdi's wb */
629+
630+
nr = 0;
631+
spin_lock(&wb->list_lock);
632+
list_for_each_entry(inode, &wb->b_attached, i_io_list) {
633+
if (!inode_prepare_wbs_switch(inode, isw->new_wb))
634+
continue;
635+
636+
isw->inodes[nr++] = inode;
637+
638+
if (nr >= WB_MAX_INODES_PER_ISW - 1) {
639+
restart = true;
640+
break;
641+
}
642+
}
643+
spin_unlock(&wb->list_lock);
644+
645+
/* no attached inodes? bail out */
646+
if (nr == 0) {
647+
atomic_dec(&isw_nr_in_flight);
648+
wb_put(isw->new_wb);
649+
kfree(isw);
650+
return restart;
651+
}
652+
653+
/*
654+
* In addition to synchronizing among switchers, I_WB_SWITCH tells
655+
* the RCU protected stat update paths to grab the i_page
656+
* lock so that stat transfer can synchronize against them.
657+
* Let's continue after I_WB_SWITCH is guaranteed to be visible.
658+
*/
659+
INIT_RCU_WORK(&isw->work, inode_switch_wbs_work_fn);
660+
queue_rcu_work(isw_wq, &isw->work);
661+
662+
return restart;
663+
}
664+
574665
/**
575666
* wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it
576667
* @wbc: writeback_control of interest

include/linux/backing-dev-defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ struct bdi_writeback {
155155
struct list_head memcg_node; /* anchored at memcg->cgwb_list */
156156
struct list_head blkcg_node; /* anchored at blkcg->cgwb_list */
157157
struct list_head b_attached; /* attached inodes, protected by list_lock */
158+
struct list_head offline_node; /* anchored at offline_cgwbs */
158159

159160
union {
160161
struct work_struct release_work;

include/linux/writeback.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ void wbc_account_cgroup_owner(struct writeback_control *wbc, struct page *page,
221221
int cgroup_writeback_by_id(u64 bdi_id, int memcg_id, unsigned long nr_pages,
222222
enum wb_reason reason, struct wb_completion *done);
223223
void cgroup_writeback_umount(void);
224+
bool cleanup_offline_cgwb(struct bdi_writeback *wb);
224225

225226
/**
226227
* inode_attach_wb - associate an inode with its wb

mm/backing-dev.c

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,16 @@ static void wb_exit(struct bdi_writeback *wb)
371371
#include <linux/memcontrol.h>
372372

373373
/*
374-
* cgwb_lock protects bdi->cgwb_tree, blkcg->cgwb_list, and memcg->cgwb_list.
375-
* bdi->cgwb_tree is also RCU protected.
374+
* cgwb_lock protects bdi->cgwb_tree, blkcg->cgwb_list, offline_cgwbs and
375+
* memcg->cgwb_list. bdi->cgwb_tree is also RCU protected.
376376
*/
377377
static DEFINE_SPINLOCK(cgwb_lock);
378378
static struct workqueue_struct *cgwb_release_wq;
379379

380+
static LIST_HEAD(offline_cgwbs);
381+
static void cleanup_offline_cgwbs_workfn(struct work_struct *work);
382+
static DECLARE_WORK(cleanup_offline_cgwbs_work, cleanup_offline_cgwbs_workfn);
383+
380384
static void cgwb_release_workfn(struct work_struct *work)
381385
{
382386
struct bdi_writeback *wb = container_of(work, struct bdi_writeback,
@@ -395,6 +399,11 @@ static void cgwb_release_workfn(struct work_struct *work)
395399

396400
fprop_local_destroy_percpu(&wb->memcg_completions);
397401
percpu_ref_exit(&wb->refcnt);
402+
403+
spin_lock_irq(&cgwb_lock);
404+
list_del(&wb->offline_node);
405+
spin_unlock_irq(&cgwb_lock);
406+
398407
wb_exit(wb);
399408
WARN_ON_ONCE(!list_empty(&wb->b_attached));
400409
kfree_rcu(wb, rcu);
@@ -414,6 +423,7 @@ static void cgwb_kill(struct bdi_writeback *wb)
414423
WARN_ON(!radix_tree_delete(&wb->bdi->cgwb_tree, wb->memcg_css->id));
415424
list_del(&wb->memcg_node);
416425
list_del(&wb->blkcg_node);
426+
list_add(&wb->offline_node, &offline_cgwbs);
417427
percpu_ref_kill(&wb->refcnt);
418428
}
419429

@@ -635,6 +645,54 @@ static void cgwb_bdi_unregister(struct backing_dev_info *bdi)
635645
mutex_unlock(&bdi->cgwb_release_mutex);
636646
}
637647

648+
/*
649+
* cleanup_offline_cgwbs_workfn - try to release dying cgwbs
650+
*
651+
* Try to release dying cgwbs by switching attached inodes to the nearest
652+
* living ancestor's writeback. Processed wbs are placed at the end
653+
* of the list to guarantee the forward progress.
654+
*/
655+
static void cleanup_offline_cgwbs_workfn(struct work_struct *work)
656+
{
657+
struct bdi_writeback *wb;
658+
LIST_HEAD(processed);
659+
660+
spin_lock_irq(&cgwb_lock);
661+
662+
while (!list_empty(&offline_cgwbs)) {
663+
wb = list_first_entry(&offline_cgwbs, struct bdi_writeback,
664+
offline_node);
665+
list_move(&wb->offline_node, &processed);
666+
667+
/*
668+
* If wb is dirty, cleaning up the writeback by switching
669+
* attached inodes will result in an effective removal of any
670+
* bandwidth restrictions, which isn't the goal. Instead,
671+
* it can be postponed until the next time, when all io
672+
* will be likely completed. If in the meantime some inodes
673+
* will get re-dirtied, they should be eventually switched to
674+
* a new cgwb.
675+
*/
676+
if (wb_has_dirty_io(wb))
677+
continue;
678+
679+
if (!wb_tryget(wb))
680+
continue;
681+
682+
spin_unlock_irq(&cgwb_lock);
683+
while (cleanup_offline_cgwb(wb))
684+
cond_resched();
685+
spin_lock_irq(&cgwb_lock);
686+
687+
wb_put(wb);
688+
}
689+
690+
if (!list_empty(&processed))
691+
list_splice_tail(&processed, &offline_cgwbs);
692+
693+
spin_unlock_irq(&cgwb_lock);
694+
}
695+
638696
/**
639697
* wb_memcg_offline - kill all wb's associated with a memcg being offlined
640698
* @memcg: memcg being offlined
@@ -651,6 +709,8 @@ void wb_memcg_offline(struct mem_cgroup *memcg)
651709
cgwb_kill(wb);
652710
memcg_cgwb_list->next = NULL; /* prevent new wb's */
653711
spin_unlock_irq(&cgwb_lock);
712+
713+
queue_work(system_unbound_wq, &cleanup_offline_cgwbs_work);
654714
}
655715

656716
/**

0 commit comments

Comments
 (0)