cgroup, blkcg: prevent dirty inodes to pin dying memory cgroups
Subsystems (5)
PATCH v4 1/1 cgroup, blkcg: prevent dirty inodes to pin dying memory cgroups
When an inode is getting dirty for the first time it's associated with a wb structure (see __inode_attach_wb()). It can later be switched to another wb (if e.g. some other cgroup is writing a lot of data to the same inode), but otherwise stays attached to the original wb until being reclaimed.
The problem is that the wb structure holds a reference to the original memory and blkcg cgroups. So if an inode has been dirty once and later is actively used in read-only mode, it has a good chance to pin down the original memory and blkcg cgroups. This is often the case with services bringing data for other services, e.g. updating some rpm packages.
In the real life it becomes a problem due to a large size of the memcg structure, which can easily be 1000x larger than an inode. Also a really large number of dying cgroups can raise different scalability issues, e.g. making the memory reclaim costly and less effective.
To solve the problem inodes should be eventually detached from the corresponding writeback structure. It's inefficient to do it after every writeback completion. Instead it can be done whenever the original memory cgroup is offlined and writeback structure is getting killed. Scanning over a (potentially long) list of inodes and detach them from the writeback structure can take quite some time. To avoid scanning all inodes, attached inodes are kept on a new list (b_attached). To make it less noticeable to a user, the scanning is performed from a work context.
Big thanks to Jan Kara and Dennis Zhou for their ideas and contribution to the previous iterations of this patch.
--- fs/fs-writeback.c | 84 ++++++++++++++++++++++++-------- include/linux/backing-dev-defs.h | 4 ++ include/linux/backing-dev.h | 17 ------- include/linux/writeback.h | 1 + mm/backing-dev.c | 68 ++++++++++++++++++++++++-- 5 files changed, 132 insertions(+), 42 deletions(-)
fs/fs-writeback.c +63 -21
@@ -109,10 +109,10 @@ static void wb_io_lists_depopulated(struct bdi_writeback *wb)* inode_io_list_move_locked - move an inode onto a bdi_writeback IO list * @inode: inode to be moved * @wb: target bdi_writeback -* @head: one of @wb->b_{dirty|io|more_io|dirty_time} +* @head: one of @wb->b_{dirty|io|more_io|dirty_time|attached} * * Move @inode->i_io_list to @list of @wb and set %WB_has_dirty_io. -* Returns %true if @inode is the first occupant of the !dirty_time IO +* Returns %true if @inode is the first occupant of the dirty, io or more_io * lists; otherwise, %false. */ static bool inode_io_list_move_locked(struct inode *inode,
@@ -123,12 +123,17 @@ static bool inode_io_list_move_locked(struct inode *inode,list_move (&inode->i_io_list, head); -/* dirty_time doesn't count as dirty_io until expiration */ -if (head != &wb->b_dirty_time) -return wb_io_lists_populated (wb); +if (head == &wb->b_dirty_time || head == &wb->b_attached) { +/* +* dirty_time doesn't count as dirty_io until expiration, +* attached list keeps a list of clean inodes, which are +* attached to wb. +*/ +wb_io_lists_depopulated (wb); +return false; + } -wb_io_lists_depopulated (wb); -return false; +return wb_io_lists_populated (wb); }/**
@@ -545,6 +550,37 @@ static void inode_switch_wbs(struct inode *inode, int new_wb_id)kfree (isw); } +/** +* cleanup_offline_wb - detach attached clean inodes +* @wb: target wb +* +* Clear the ->i_wb pointer of the attached inodes and drop +* the corresponding wb reference. Skip inodes which are dirty, +* freeing, switching or in the active writeback process. +* +*/ +void cleanup_offline_wb (struct bdi_writeback *wb) +{ +struct inode *inode, *tmp; + +spin_lock (&wb->list_lock); +list_for_each_entry_safe (inode, tmp, &wb->b_attached, i_io_list) { +if (!spin_trylock (&inode->i_lock)) +continue ; +xa_lock_irq (&inode->i_mapping->i_pages); +if (!(inode->i_state & + (I_FREEING |I_CLEAR |I_SYNC |I_DIRTY |I_WB_SWITCH ))) { +WARN_ON_ONCE (inode->i_wb != wb); + inode->i_wb =NULL ; +wb_put (wb); +list_del_init (&inode->i_io_list); + } +xa_unlock_irq (&inode->i_mapping->i_pages); +spin_unlock (&inode->i_lock); + } +spin_unlock (&wb->list_lock); +} +/** * wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it * @wbc: writeback_control of interest
@@ -779,19 +815,18 @@ EXPORT_SYMBOL_GPL(wbc_account_cgroup_owner);*/ int inode_congested (struct inode *inode,int cong_bits) { -/* -* Once set, ->i_wb never becomes NULL while the inode is alive. -* Start transaction iff ->i_wb is visible. -*/ -if (inode &&inode_to_wb_is_valid (inode)) { +if (inode) {struct bdi_writeback *wb;struct wb_lock_cookie lock_cookie = {};bool congested; wb =unlocked_inode_to_wb_begin (inode, &lock_cookie); - congested =wb_congested (wb, cong_bits); +if (wb) { + congested =wb_congested (wb, cong_bits); +unlocked_inode_to_wb_end (inode, &lock_cookie); +return congested; + }unlocked_inode_to_wb_end (inode, &lock_cookie); -return congested; }return wb_congested (&inode_to_bdi (inode)->wb, cong_bits);
@@ -1436,8 +1471,13 @@ static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,inode_io_list_move_locked (inode, wb, &wb->b_dirty_time); inode->i_state &= ~I_SYNC_QUEUED ; }else { -/* The inode is clean. Remove from writeback lists. */ -inode_io_list_del_locked (inode, wb); +/* +* The inode is clean. Remove from writeback lists and +* move it to the attached list, because the inode is +* still attached to wb. +*/ +inode_io_list_move_locked (inode, wb, &wb->b_attached); + inode->i_state &= ~I_SYNC_QUEUED ; } }
@@ -1584,12 +1624,14 @@ static int writeback_single_inode(struct inode *inode, wb =inode_to_wb_and_lock_list (inode);spin_lock (&inode->i_lock);/* -* If the inode is now fully clean, then it can be safely removed from -* its writeback list (if any). Otherwise the flusher threads are -* responsible for the writeback lists. +* If inode is clean, remove it from writeback lists and put into +* the attached list. Otherwise don't touch it. See comment above +* for explanation. */ -if (!(inode->i_state &I_DIRTY_ALL )) -inode_io_list_del_locked (inode, wb); +if (!(inode->i_state &I_DIRTY_ALL )) { +inode_io_list_move_locked (inode, wb, &wb->b_attached); + inode->i_state &= ~I_SYNC_QUEUED ; + }spin_unlock (&wb->list_lock);inode_sync_complete (inode); out:
include/linux/backing-dev-defs.h +4 -0
@@ -114,6 +114,9 @@ struct bdi_writeback {struct list_head b_io;/* parked for writeback */ struct list_head b_more_io;/* parked for more writeback */ struct list_head b_dirty_time;/* time stamps are dirty */ +struct list_head b_attached;/* clean inodes pinning this wb +* though inode->i_wb +*/ spinlock_t list_lock;/* protects the b_* lists */ struct percpu_counter stat[NR_WB_STAT_ITEMS ];
@@ -154,6 +157,7 @@ struct bdi_writeback {struct cgroup_subsys_state *blkcg_css;/* and blkcg */ struct list_head memcg_node;/* anchored at memcg->cgwb_list */ struct list_head blkcg_node;/* anchored at blkcg->cgwb_list */ +struct list_head offline_node;union {struct work_struct release_work;
include/linux/backing-dev.h +0 -17
@@ -257,18 +257,6 @@ wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)return wb; } -/** -* inode_to_wb_is_valid - test whether an inode has a wb associated -* @inode: inode of interest -* -* Returns %true if @inode has a wb associated. May be called without any -* locking. -*/ -static inline bool inode_to_wb_is_valid (struct inode *inode) -{ -return inode->i_wb; -} -/** * inode_to_wb - determine the wb of an inode * @inode: inode of interest
@@ -356,11 +344,6 @@ wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)return &bdi->wb; } -static inline bool inode_to_wb_is_valid (struct inode *inode) -{ -return true; -} -static inline struct bdi_writeback *inode_to_wb (struct inode *inode) {return &inode_to_bdi (inode)->wb;
include/linux/writeback.h +1 -0
@@ -221,6 +221,7 @@ void wbc_account_cgroup_owner(struct writeback_control *wbc, struct page *page,int cgroup_writeback_by_id (u64 bdi_id,int memcg_id,unsigned long nr_pages,enum wb_reason reason,struct wb_completion *done);void cgroup_writeback_umount (void ); +void cleanup_offline_wb (struct bdi_writeback *wb);/** * inode_attach_wb - associate an inode with its wb
mm/backing-dev.c +64 -4
134 lines not on this page · raw message
Signature · 1 line
2.30.2
Jan Kara
maintainerQuoted 28 lines · Roman Gushchin
When an inode is getting dirty for the first time it's associated with a wb structure (see
__inode_attach_wb()). It can later be switched to another wb (if e.g. some other cgroup is writing a lot of data to the same inode), but otherwise stays attached to the original wb until being reclaimed.The problem is that the wb structure holds a reference to the original memory and blkcg cgroups. So if an inode has been dirty once and later is actively used in read-only mode, it has a good chance to pin down the original memory and blkcg cgroups. This is often the case with services bringing data for other services, e.g. updating some rpm packages.
In the real life it becomes a problem due to a large size of the memcg structure, which can easily be 1000x larger than an inode. Also a really large number of dying cgroups can raise different scalability issues, e.g. making the memory reclaim costly and less effective.
To solve the problem inodes should be eventually detached from the corresponding writeback structure. It's inefficient to do it after every writeback completion. Instead it can be done whenever the original memory cgroup is offlined and writeback structure is getting killed. Scanning over a (potentially long) list of inodes and detach them from the writeback structure can take quite some time. To avoid scanning all inodes, attached inodes are kept on a new list (
b_attached). To make it less noticeable to a user, the scanning is performed from a work context.
Quoted 4 lines
Big thanks to Jan Kara and Dennis Zhou for their ideas and contribution to the previous iterations of this patch.
Signed-off-by Roman Gushchin <[email protected]>
Thanks for the patch! On a general note maybe it would be better to split this patch into two - the first one which introduces b_attached list and its handling and the second one which uses it to detach inodes from bdi_writeback structures. Some more comments below.
Quoted 28 lines
fs/fs-writeback.c +10
-5@@ -123,12 +123,17 @@ static bool inode_io_list_move_locked(struct inode *inode,list_move (&inode->i_io_list, head); -/* dirty_time doesn't count as dirty_io until expiration */ -if (head != &wb->b_dirty_time) -return wb_io_lists_populated (wb); +if (head == &wb->b_dirty_time || head == &wb->b_attached) { +/* +* dirty_time doesn't count as dirty_io until expiration, +* attached list keeps a list of clean inodes, which are +* attached to wb. +*/ +wb_io_lists_depopulated (wb); +return false; + } -wb_io_lists_depopulated (wb); -return false; +return wb_io_lists_populated (wb); }/**
fs/fs-writeback.c static void inode_switch_wbs(struct inode *inode, int new_wb_id) · line 551
Quoted 2 lines
fs/fs-writeback.c +0
-0@@ -545,6 +550,37 @@ static void inode_switch_wbs(struct inode *inode, int new_wb_id)kfree (isw); }
I suppose the list_empty(&inode->i_io_list) case in inode_switch_wbs_work_fn() is impossible with your changes now? Can you perhaps add a WARN_ON_ONCE there for this? Also I don't think we want to move clean inodes to dirty list so perhaps we need to be more careful about the selection of target writeback list in that function?
Quoted 10 lines
fs/fs-writeback.c +10
-0+/** +* cleanup_offline_wb - detach attached clean inodes +* @wb: target wb +* +* Clear the ->i_wb pointer of the attached inodes and drop +* the corresponding wb reference. Skip inodes which are dirty, +* freeing, switching or in the active writeback process. +* +*/ +void cleanup_offline_wb (struct bdi_writeback *wb)
fs/fs-writeback.c static void inode_switch_wbs(struct inode *inode, int new_wb_id) · line 572
Quoted 10 lines
fs/fs-writeback.c +10
-0+{ +struct inode *inode, *tmp; + +spin_lock (&wb->list_lock); +list_for_each_entry_safe (inode, tmp, &wb->b_attached, i_io_list) { +if (!spin_trylock (&inode->i_lock)) +continue ; +xa_lock_irq (&inode->i_mapping->i_pages); +if (!(inode->i_state & + (I_FREEING |I_CLEAR |I_SYNC |I_DIRTY |I_WB_SWITCH ))) {
Use I_DIRTY_ALL here instead of I_DIRTY? We don't want to touch I_DIRTY_TIME inodes either I'd say... Also I think you don't want to touch I_WILL_FREE inodes either.
fs/fs-writeback.c static void inode_switch_wbs(struct inode *inode, int new_wb_id) · line 576
Quoted 4 lines
fs/fs-writeback.c +4
-0+WARN_ON_ONCE (inode->i_wb != wb); + inode->i_wb =NULL ; +wb_put (wb); +list_del_init (&inode->i_io_list);
So I was thinking about this and I'm still a bit nervous that setting i_wb to NULL is going to cause subtle crashes somewhere. Granted you are very careful when not to touch the inode but still, even stuff like inode_to_bdi() is not safe to call with inode->i_wb is NULL. So I'm afraid that some place in the writeback code will be looking at i_wb without having any of those bits set and boom. E.g. inode_to_wb() call in test_clear_page_writeback() - what protects that one?
I forgot what possibilities did we already discuss in the past but cannot we just switch inode->i_wb to inode_to_bdi(inode)->wb (i.e., root cgroup writeback structure)? That would be certainly safer...
Quoted 7 lines
fs/fs-writeback.c +7
-0+ } +xa_unlock_irq (&inode->i_mapping->i_pages); +spin_unlock (&inode->i_lock); + } +spin_unlock (&wb->list_lock); +} +
...
Quoted 12 lines
fs/fs-writeback.c +4
-0@@ -386,6 +395,10 @@ static void cgwb_release_workfn(struct work_struct *work)mutex_lock (&wb->bdi->cgwb_release_mutex);wb_shutdown (wb); +spin_lock_irq (&cgwb_lock); +list_del (&wb->offline_node); +spin_unlock_irq (&cgwb_lock); +css_put (wb->memcg_css);css_put (wb->blkcg_css);mutex_unlock (&wb->bdi->cgwb_release_mutex);
fs/fs-writeback.c static void cgwb_kill(struct bdi_writeback *wb) · line 431
Quoted 6 lines
fs/fs-writeback.c +1
-0@@ -413,6 +426,7 @@ static void cgwb_kill(struct bdi_writeback *wb)WARN_ON (!radix_tree_delete (&wb->bdi->cgwb_tree, wb->memcg_css->id));list_del (&wb->memcg_node);list_del (&wb->blkcg_node); +list_add (&wb->offline_node, &offline_cgwbs);percpu_ref_kill (&wb->refcnt); }
I think you need to be a bit more careful with the wb->offline_node. cgwb_create() can end up destroying half-created bdi_writeback structure on error and then you'd see cgwb_release_workfn() called without cgwb_kill() called and you'd likely crash or corrupt memory.
Quoted 35 lines
fs/fs-writeback.c +30
-0@@ -633,6 +647,48 @@ static void cgwb_bdi_unregister(struct backing_dev_info *bdi)mutex_unlock (&bdi->cgwb_release_mutex); } +/** +* cleanup_offline_cgwbs - try to release dying cgwbs +* +* Try to release dying cgwbs by switching attached inodes to the wb +* belonging to the root memory cgroup. Processed wbs are placed at the +* end of the list to guarantee the forward progress. +* +* Should be called with the acquired cgwb_lock lock, which might +* be released and re-acquired in the process. +*/ +static void cleanup_offline_cgwbs_workfn (struct work_struct *work) +{ +struct bdi_writeback *wb; +LIST_HEAD (processed); + +spin_lock_irq (&cgwb_lock); + +while (!list_empty (&offline_cgwbs)) { + wb = list_first_entry(&offline_cgwbs, struct bdi_writeback, + offline_node); + +list_move (&wb->offline_node, &processed); + +if (wb_has_dirty_io (wb)) +continue ; + +if (!percpu_ref_tryget (&wb->refcnt)) +continue ; + +spin_unlock_irq (&cgwb_lock);
fs/fs-writeback.c static void cgwb_bdi_unregister(struct backing_dev_info *bdi) · line 689
Quoted 10 lines
fs/fs-writeback.c +10
-0+cleanup_offline_wb (wb); +spin_lock_irq (&cgwb_lock); + +wb_put (wb); + } + +if (!list_empty (&processed)) +list_splice_tail (&processed, &offline_cgwbs); + +spin_unlock_irq (&cgwb_lock);
Shouldn't we reschedule this work with some delay if offline_cgwbs is non-empty? Otherwise we can end up with non-empty &offline_cgwbs and no cleaning scheduled...
Honza
Signature · 2 lines
Jan Kara <jack-IBi9RG/[email protected]> SUSE Labs, CR
Quoted 32 lines · Jan Kara
Quoted 31 lines · Roman Gushchin
When an inode is getting dirty for the first time it's associated with a wb structure (see
__inode_attach_wb()). It can later be switched to another wb (if e.g. some other cgroup is writing a lot of data to the same inode), but otherwise stays attached to the original wb until being reclaimed.The problem is that the wb structure holds a reference to the original memory and blkcg cgroups. So if an inode has been dirty once and later is actively used in read-only mode, it has a good chance to pin down the original memory and blkcg cgroups. This is often the case with services bringing data for other services, e.g. updating some rpm packages.
In the real life it becomes a problem due to a large size of the memcg structure, which can easily be 1000x larger than an inode. Also a really large number of dying cgroups can raise different scalability issues, e.g. making the memory reclaim costly and less effective.
To solve the problem inodes should be eventually detached from the corresponding writeback structure. It's inefficient to do it after every writeback completion. Instead it can be done whenever the original memory cgroup is offlined and writeback structure is getting killed. Scanning over a (potentially long) list of inodes and detach them from the writeback structure can take quite some time. To avoid scanning all inodes, attached inodes are kept on a new list (
b_attached). To make it less noticeable to a user, the scanning is performed from a work context.Big thanks to Jan Kara and Dennis Zhou for their ideas and contribution to the previous iterations of this patch.
Quoted 6 lines
Quoted 1 line
Signed-off-by Roman Gushchin <[email protected]>Thanks for the patch! On a general note maybe it would be better to split this patch into two - the first one which introduces
b_attachedlist and its handling and the second one which uses it to detach inodes frombdi_writebackstructures. Some more comments below.
Good idea, will do in the next version. And thank you for taking a look!
Quoted 27 lines
Quoted 27 lines
fs/fs-writeback.c +10
-5@@ -123,12 +123,17 @@ static bool inode_io_list_move_locked(struct inode *inode,list_move (&inode->i_io_list, head); -/* dirty_time doesn't count as dirty_io until expiration */ -if (head != &wb->b_dirty_time) -return wb_io_lists_populated (wb); +if (head == &wb->b_dirty_time || head == &wb->b_attached) { +/* +* dirty_time doesn't count as dirty_io until expiration, +* attached list keeps a list of clean inodes, which are +* attached to wb. +*/ +wb_io_lists_depopulated (wb); +return false; + } -wb_io_lists_depopulated (wb); -return false; +return wb_io_lists_populated (wb); }
Quoted 10 lines
fs/fs-writeback.c static void inode_switch_wbs(struct inode *inode, int new_wb_id) · line 551
Quoted 4 lines
fs/fs-writeback.c +0
-0/** @@ -545,6 +550,37 @@ static void inode_switch_wbs(struct inode *inode, int new_wb_id)kfree (isw); }I suppose the
list_empty(&inode->i_io_list)case ininode_switch_wbs_work_fn()is impossible with your changes now? Can you perhaps add aWARN_ON_ONCEthere for this? Also I don't think we want to move clean inodes to dirty list so perhaps we need to be more careful about the selection of target writeback list in that function?
Good point, will add in the next version and double check the clean inode case.
Quoted 17 lines
Quoted 17 lines
fs/fs-writeback.c +16
-0+/** +* cleanup_offline_wb - detach attached clean inodes +* @wb: target wb +* +* Clear the ->i_wb pointer of the attached inodes and drop +* the corresponding wb reference. Skip inodes which are dirty, +* freeing, switching or in the active writeback process. +* +*/ +void cleanup_offline_wb (struct bdi_writeback *wb) +{ +struct inode *inode, *tmp; + +spin_lock (&wb->list_lock); +list_for_each_entry_safe (inode, tmp, &wb->b_attached, i_io_list) { +if (!spin_trylock (&inode->i_lock))
Quoted 8 lines
fs/fs-writeback.c static void inode_switch_wbs(struct inode *inode, int new_wb_id) · line 572
Quoted 4 lines
fs/fs-writeback.c +4
-0+continue ; +xa_lock_irq (&inode->i_mapping->i_pages); +if (!(inode->i_state & + (I_FREEING |I_CLEAR |I_SYNC |I_DIRTY |I_WB_SWITCH ))) {Use
I_DIRTY_ALLhere instead ofI_DIRTY? We don't want to touchI_DIRTY_TIMEinodes either I'd say... Also I think you don't want to touchI_WILL_FREEinodes either.
You're right, will fix it.
Quoted 13 lines
fs/fs-writeback.c static void inode_switch_wbs(struct inode *inode, int new_wb_id) · line 576
Quoted 5 lines
fs/fs-writeback.c +4
-0+WARN_ON_ONCE (inode->i_wb != wb); + inode->i_wb =NULL ; +wb_put (wb); +list_del_init (&inode->i_io_list);So I was thinking about this and I'm still a bit nervous that setting
i_wbto NULL is going to cause subtle crashes somewhere. Granted you are very careful when not to touch the inode but still, even stuff likeinode_to_bdi()is not safe to call withinode->i_wbis NULL. So I'm afraid that some place in the writeback code will be looking ati_wbwithout having any of those bits set and boom. E.g.inode_to_wb()call intest_clear_page_writeback()- what protects that one?
I assume that if the page is dirty/under the writeback, the inode must be dirty too, so we can't zero inode->i_wb.
Quoted 4 lines
I forgot what possibilities did we already discuss in the past but cannot we just switch
inode->i_wbto inode_to_bdi(inode)->wb (i.e., root cgroup writeback structure)? That would be certainly safer...
I am/was nervous too. I had several BUG_ON()'s in all such places and ran the test kernel for about a day on my dev desktop (even updated to Fedora 34 lol), and haven't seen any panics. And certainly I can give it a comprehensive test in a more production environment.
Re switching to the root wb: it's certainly a possibility too, however zeroing has an advantage: the next potential writeback will be accounted to the right cgroup without a need in an additional switch. I'd try to go with zeroing if it's possible, and keep the switching to the root wb as plab B.
Quoted 24 lines
Quoted 8 lines
fs/fs-writeback.c +7
-0+ } +xa_unlock_irq (&inode->i_mapping->i_pages); +spin_unlock (&inode->i_lock); + } +spin_unlock (&wb->list_lock); +} +...
Quoted 15 lines
fs/fs-writeback.c +4
-0@@ -386,6 +395,10 @@ static void cgwb_release_workfn(struct work_struct *work)mutex_lock (&wb->bdi->cgwb_release_mutex);wb_shutdown (wb); +spin_lock_irq (&cgwb_lock); +list_del (&wb->offline_node); +spin_unlock_irq (&cgwb_lock); +css_put (wb->memcg_css);css_put (wb->blkcg_css);mutex_unlock (&wb->bdi->cgwb_release_mutex);@@ -413,6 +426,7 @@ static void cgwb_kill(struct bdi_writeback *wb)WARN_ON (!radix_tree_delete (&wb->bdi->cgwb_tree, wb->memcg_css->id));list_del (&wb->memcg_node);list_del (&wb->blkcg_node);
Quoted 8 lines
fs/fs-writeback.c static void cgwb_kill(struct bdi_writeback *wb) · line 431
Quoted 3 lines
fs/fs-writeback.c +1
-0+list_add (&wb->offline_node, &offline_cgwbs);percpu_ref_kill (&wb->refcnt); }I think you need to be a bit more careful with the
wb->offline_node.cgwb_create()can end up destroying half-createdbdi_writebackstructure on error and then you'd seecgwb_release_workfn()called withoutcgwb_kill()called and you'd likely crash or corrupt memory.
Good point, will check it.
Quoted 42 lines
Quoted 42 lines
fs/fs-writeback.c +36
-0@@ -633,6 +647,48 @@ static void cgwb_bdi_unregister(struct backing_dev_info *bdi)mutex_unlock (&bdi->cgwb_release_mutex); } +/** +* cleanup_offline_cgwbs - try to release dying cgwbs +* +* Try to release dying cgwbs by switching attached inodes to the wb +* belonging to the root memory cgroup. Processed wbs are placed at the +* end of the list to guarantee the forward progress. +* +* Should be called with the acquired cgwb_lock lock, which might +* be released and re-acquired in the process. +*/ +static void cleanup_offline_cgwbs_workfn (struct work_struct *work) +{ +struct bdi_writeback *wb; +LIST_HEAD (processed); + +spin_lock_irq (&cgwb_lock); + +while (!list_empty (&offline_cgwbs)) { + wb = list_first_entry(&offline_cgwbs, struct bdi_writeback, + offline_node); + +list_move (&wb->offline_node, &processed); + +if (wb_has_dirty_io (wb)) +continue ; + +if (!percpu_ref_tryget (&wb->refcnt)) +continue ; + +spin_unlock_irq (&cgwb_lock); +cleanup_offline_wb (wb); +spin_lock_irq (&cgwb_lock); + +wb_put (wb); + } +
Quoted 8 lines
fs/fs-writeback.c static void cgwb_bdi_unregister(struct backing_dev_info *bdi) · line 689
Quoted 4 lines
fs/fs-writeback.c +4
-0+if (!list_empty (&processed)) +list_splice_tail (&processed, &offline_cgwbs); + +spin_unlock_irq (&cgwb_lock);Shouldn't we reschedule this work with some delay if
offline_cgwbsis non-empty? Otherwise we can end up with non-empty&offline_cgwbsand no cleaning scheduled...
I'm not sure. In general it's not a big problem to have few outstanding wb structures, we just need to make sure we don't pile them. I'm scheduling the cleanup from the memcg offlining path, so if new cgroups are regularly created and destroyed, some pressure will be applied.
To reschedule based on time we need to come up with some heuristic how to calculate the required delay and I don't have any specific ideas. If you do, I'm totally fine to incorporate them.
Thanks!
Jan Kara
maintainerQuoted 15 lines · Roman Gushchin
Quoted 13 lines · Jan Kara
fs/fs-writeback.c static void inode_switch_wbs(struct inode *inode, int new_wb_id) · line 576
Quoted 4 lines · Roman Gushchin
fs/fs-writeback.c +4
-0+WARN_ON_ONCE (inode->i_wb != wb); + inode->i_wb =NULL ; +wb_put (wb); +list_del_init (&inode->i_io_list);So I was thinking about this and I'm still a bit nervous that setting
i_wbto NULL is going to cause subtle crashes somewhere. Granted you are very careful when not to touch the inode but still, even stuff likeinode_to_bdi()is not safe to call withinode->i_wbis NULL. So I'm afraid that some place in the writeback code will be looking ati_wbwithout having any of those bits set and boom. E.g.inode_to_wb()call intest_clear_page_writeback()- what protects that one?
Quoted 2 lines
I assume that if the page is dirty/under the writeback, the inode must be dirty too, so we can't zero
inode->i_wb.
If page is under writeback, the inode can be already clean. You could check
!mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)
but see how fragile it is... For every place using inode_to_wb() you have to come up with a test excluding it...
Quoted 8 lines
Quoted 3 lines
I forgot what possibilities did we already discuss in the past but cannot we just switch
inode->i_wbto inode_to_bdi(inode)->wb (i.e., root cgroup writeback structure)? That would be certainly safer...I am/was nervous too. I had several BUG_ON()'s in all such places and ran the test kernel for about a day on my dev desktop (even updated to Fedora 34 lol), and haven't seen any panics. And certainly I can give it a comprehensive test in a more production environment.
I appreciate the testing but it is also about how likely this is to break sometime in future because someone unaware of this corner-case will add new inode_to_wb() call not excluded by one of your conditions.
Quoted 5 lines
Re switching to the root wb: it's certainly a possibility too, however zeroing has an advantage: the next potential writeback will be accounted to the right cgroup without a need in an additional switch. I'd try to go with zeroing if it's possible, and keep the switching to the root wb as plab B.
Yes, there will be the cost of an additional switch. But inodes attached to dying cgroups shouldn't be the fast path anyway so does it matter?
Quoted 49 lines
Quoted 40 lines
Quoted 40 lines
mm/backing-dev.c +36
-0@@ -633,6 +647,48 @@ static void cgwb_bdi_unregister(struct backing_dev_info *bdi)mutex_unlock (&bdi->cgwb_release_mutex); } +/** +* cleanup_offline_cgwbs - try to release dying cgwbs +* +* Try to release dying cgwbs by switching attached inodes to the wb +* belonging to the root memory cgroup. Processed wbs are placed at the +* end of the list to guarantee the forward progress. +* +* Should be called with the acquired cgwb_lock lock, which might +* be released and re-acquired in the process. +*/ +static void cleanup_offline_cgwbs_workfn (struct work_struct *work) +{ +struct bdi_writeback *wb; +LIST_HEAD (processed); + +spin_lock_irq (&cgwb_lock); + +while (!list_empty (&offline_cgwbs)) { + wb = list_first_entry(&offline_cgwbs, struct bdi_writeback, + offline_node); + +list_move (&wb->offline_node, &processed); + +if (wb_has_dirty_io (wb)) +continue ; + +if (!percpu_ref_tryget (&wb->refcnt)) +continue ; + +spin_unlock_irq (&cgwb_lock); +cleanup_offline_wb (wb); +spin_lock_irq (&cgwb_lock); + +wb_put (wb); + } +Quoted 8 lines
mm/backing-dev.c static void cgwb_bdi_unregister(struct backing_dev_info *bdi) · line 689
Quoted 4 lines
mm/backing-dev.c +4
-0+if (!list_empty (&processed)) +list_splice_tail (&processed, &offline_cgwbs); + +spin_unlock_irq (&cgwb_lock);Shouldn't we reschedule this work with some delay if
offline_cgwbsis non-empty? Otherwise we can end up with non-empty&offline_cgwbsand no cleaning scheduled...
Quoted 8 lines
I'm not sure. In general it's not a big problem to have few outstanding wb structures, we just need to make sure we don't pile them. I'm scheduling the cleanup from the memcg offlining path, so if new cgroups are regularly created and destroyed, some pressure will be applied.
To reschedule based on time we need to come up with some heuristic how to calculate the required delay and I don't have any specific ideas. If you do, I'm totally fine to incorporate them.
Well, I'd pick e.g. dirty_expire_interval (30s by default) as a time after which we try again. Because after that time writeback has likely already happened. But I don't insist here. If you think leaving inodes attached to dead cgroups for potentially long time in some cases isn't a problem, then we can leave this as is. If we find it's a problem in the future, we can always add the time-based retry.
Honza
Signature · 2 lines
Jan Kara <jack-IBi9RG/[email protected]> SUSE Labs, CR
Quoted 28 lines · Jan Kara
Quoted 15 lines · Roman Gushchin
Quoted 13 lines · Jan Kara
fs/fs-writeback.c static void inode_switch_wbs(struct inode *inode, int new_wb_id) · line 576
Quoted 4 lines · Roman Gushchin
fs/fs-writeback.c +4
-0+WARN_ON_ONCE (inode->i_wb != wb); + inode->i_wb =NULL ; +wb_put (wb); +list_del_init (&inode->i_io_list);So I was thinking about this and I'm still a bit nervous that setting
i_wbto NULL is going to cause subtle crashes somewhere. Granted you are very careful when not to touch the inode but still, even stuff likeinode_to_bdi()is not safe to call withinode->i_wbis NULL. So I'm afraid that some place in the writeback code will be looking ati_wbwithout having any of those bits set and boom. E.g.inode_to_wb()call intest_clear_page_writeback()- what protects that one?Quoted 2 lines
I assume that if the page is dirty/under the writeback, the inode must be dirty too, so we can't zero
inode->i_wb.If page is under writeback, the inode can be already clean. You could check
!mapping_tagged(mapping,PAGECACHE_TAG_WRITEBACK)
but see how fragile it is... For every place usinginode_to_wb()you have to come up with a test excluding it...Quoted 4 lines
Quoted 3 lines
I forgot what possibilities did we already discuss in the past but cannot we just switch
inode->i_wbto inode_to_bdi(inode)->wb (i.e., root cgroup writeback structure)? That would be certainly safer...
Quoted 8 lines
Quoted 4 lines
I am/was nervous too. I had several BUG_ON()'s in all such places and ran the test kernel for about a day on my dev desktop (even updated to Fedora 34 lol), and haven't seen any panics. And certainly I can give it a comprehensive test in a more production environment.
I appreciate the testing but it is also about how likely this is to break sometime in future because someone unaware of this corner-case will add new
inode_to_wb()call not excluded by one of your conditions.
Ok, you convinced me, will change in the next version.
Quoted 68 lines
Quoted 6 lines
Re switching to the root wb: it's certainly a possibility too, however zeroing has an advantage: the next potential writeback will be accounted to the right cgroup without a need in an additional switch. I'd try to go with zeroing if it's possible, and keep the switching to the root wb as plab B.
Yes, there will be the cost of an additional switch. But inodes attached to dying cgroups shouldn't be the fast path anyway so does it matter?
Quoted 49 lines
Quoted 40 lines
Quoted 40 lines
mm/backing-dev.c +36
-0@@ -633,6 +647,48 @@ static void cgwb_bdi_unregister(struct backing_dev_info *bdi)mutex_unlock (&bdi->cgwb_release_mutex); } +/** +* cleanup_offline_cgwbs - try to release dying cgwbs +* +* Try to release dying cgwbs by switching attached inodes to the wb +* belonging to the root memory cgroup. Processed wbs are placed at the +* end of the list to guarantee the forward progress. +* +* Should be called with the acquired cgwb_lock lock, which might +* be released and re-acquired in the process. +*/ +static void cleanup_offline_cgwbs_workfn (struct work_struct *work) +{ +struct bdi_writeback *wb; +LIST_HEAD (processed); + +spin_lock_irq (&cgwb_lock); + +while (!list_empty (&offline_cgwbs)) { + wb = list_first_entry(&offline_cgwbs, struct bdi_writeback, + offline_node); + +list_move (&wb->offline_node, &processed); + +if (wb_has_dirty_io (wb)) +continue ; + +if (!percpu_ref_tryget (&wb->refcnt)) +continue ; + +spin_unlock_irq (&cgwb_lock); +cleanup_offline_wb (wb); +spin_lock_irq (&cgwb_lock); + +wb_put (wb); + } +Quoted 8 lines
mm/backing-dev.c static void cgwb_bdi_unregister(struct backing_dev_info *bdi) · line 689
Quoted 4 lines
mm/backing-dev.c +4
-0+if (!list_empty (&processed)) +list_splice_tail (&processed, &offline_cgwbs); + +spin_unlock_irq (&cgwb_lock);Shouldn't we reschedule this work with some delay if
offline_cgwbsis non-empty? Otherwise we can end up with non-empty&offline_cgwbsand no cleaning scheduled...Quoted 8 lines
I'm not sure. In general it's not a big problem to have few outstanding wb structures, we just need to make sure we don't pile them. I'm scheduling the cleanup from the memcg offlining path, so if new cgroups are regularly created and destroyed, some pressure will be applied.
To reschedule based on time we need to come up with some heuristic how to calculate the required delay and I don't have any specific ideas. If you do, I'm totally fine to incorporate them.
Quoted 6 lines
Well, I'd pick e.g.
dirty_expire_interval(30s by default) as a time after which we try again. Because after that time writeback has likely already happened. But I don't insist here. If you think leaving inodes attached to dead cgroups for potentially long time in some cases isn't a problem, then we can leave this as is. If we find it's a problem in the future, we can always add the time-based retry.
Yes, I agree, we can add it later.
Thank you!
Roman