提交 eddebbb8 编写于 作者: M Muchun Song 提交者: Zheng Zengkai

writeback: fix obtain a reference to a freeing memcg css

stable inclusion
from stable-5.10.50
commit 6939c39a4106199877f93c96d931cff45c0d1dad
bugzilla: 174522 https://gitee.com/openeuler/kernel/issues/I4DNFY

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=6939c39a4106199877f93c96d931cff45c0d1dad

--------------------------------

[ Upstream commit 8b0ed844 ]

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: 682aa8e1 ("writeback: implement unlocked_inode_to_wb transaction and use it for stat updates")
Link: https://lore.kernel.org/r/20210402091145.80635-1-songmuchun@bytedance.comSigned-off-by: NMuchun Song <songmuchun@bytedance.com>
Acked-by: NMichal Hocko <mhocko@suse.com>
Acked-by: NTejun Heo <tj@kernel.org>
Signed-off-by: NJan Kara <jack@suse.cz>
Signed-off-by: NSasha Levin <sashal@kernel.org>
Signed-off-by: NChen Jun <chenjun102@huawei.com>
Acked-by: NWeilong Chen <chenweilong@huawei.com>
Signed-off-by: NChen Jun <chenjun102@huawei.com>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
上级 9603035c
...@@ -510,9 +510,14 @@ static void inode_switch_wbs(struct inode *inode, int new_wb_id) ...@@ -510,9 +510,14 @@ static void inode_switch_wbs(struct inode *inode, int new_wb_id)
/* find and pin the new wb */ /* find and pin the new wb */
rcu_read_lock(); rcu_read_lock();
memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys); memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys);
if (memcg_css) if (memcg_css && !css_tryget(memcg_css))
isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); memcg_css = NULL;
rcu_read_unlock(); rcu_read_unlock();
if (!memcg_css)
goto out_free;
isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
css_put(memcg_css);
if (!isw->new_wb) if (!isw->new_wb)
goto out_free; goto out_free;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册