Skip to content

Commit 8b0ed84

Browse files
Muchun Songjankara
authored andcommitted
writeback: fix obtain a reference to a freeing memcg css
The caller of wb_get_create() should pin the memcg, because wb_get_create() relies on this guarantee. The rcu read lock only can guarantee that the memcg css returned by css_from_id() cannot be released, but the reference of the memcg can be zero. rcu_read_lock() memcg_css = css_from_id() wb_get_create(memcg_css) cgwb_create(memcg_css) // css_get can change the ref counter from 0 back to 1 css_get(memcg_css) rcu_read_unlock() Fix it by holding a reference to the css before calling wb_get_create(). This is not a problem I encountered in the real world. Just the result of a code review. Fixes: 682aa8e ("writeback: implement unlocked_inode_to_wb transaction and use it for stat updates") Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Muchun Song <[email protected]> Acked-by: Michal Hocko <[email protected]> Acked-by: Tejun Heo <[email protected]> Signed-off-by: Jan Kara <[email protected]>
1 parent ce1b06c commit 8b0ed84

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

fs/fs-writeback.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,14 @@ static void inode_switch_wbs(struct inode *inode, int new_wb_id)
508508
/* find and pin the new wb */
509509
rcu_read_lock();
510510
memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys);
511-
if (memcg_css)
512-
isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
511+
if (memcg_css && !css_tryget(memcg_css))
512+
memcg_css = NULL;
513513
rcu_read_unlock();
514+
if (!memcg_css)
515+
goto out_free;
516+
517+
isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
518+
css_put(memcg_css);
514519
if (!isw->new_wb)
515520
goto out_free;
516521

0 commit comments

Comments
 (0)