mm: memcg/slab: Fix return child memcg objcg for root memcg
Subsystems (8)
PATCH v2 1/1 mm: memcg/slab: Fix return child memcg objcg for root memcg
Consider the following memcg hierarchy.
root
/ \
A BIf we get the objcg of memcg A failed, the get_obj_cgroup_from_current can return the wrong objcg for the root memcg.
---
changelog in v2:
1. Do not use a comparison with the root_mem_cgroup
mm/memcontrol.c | 1 + 1 file changed, 1 insertion(+)
mm/memcontrol.c +1 -0
@@ -2961,6 +2961,7 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) objcg =rcu_dereference (memcg->objcg);if (objcg &&obj_cgroup_tryget (objcg))break ; + objcg =NULL ; }rcu_read_unlock ();
Signature · 1 line
2.20.1
PATCH v2 1/1 mm: memcg/slab: Fix use after free in obj_cgroup_charge
The rcu_read_lock/unlock only can guarantee that the memcg will not be freed, but it cannot guarantee the success of css_get to memcg.
If the whole process of a cgroup offlining is completed between reading a objcg->memcg pointer and bumping the css reference on another CPU, and there are exactly 0 external references to this memory cgroup (how we get to the obj_cgroup_charge() then?), css_get() can change the ref counter from 0 back to 1.
---
changelog in v2:
1. Add unlikely and update the commit log suggested by Roman.
mm/memcontrol.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
mm/memcontrol.c +3 -1
@@ -3221,8 +3221,10 @@ int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size)* independently later. */ rcu_read_lock (); +retry: memcg =obj_cgroup_memcg (objcg); -css_get (&memcg->css); +if (unlikely (!css_tryget (&memcg->css))) + goto retry;rcu_read_unlock (); nr_pages = size >>PAGE_SHIFT ;
Signature · 1 line
2.20.1
Re: [PATCH v2] mm: memcg/slab: Fix use after free in obj_cgroup_charge
Quoted 11 lines · Muchun Song
The rcu_read_lock/unlock only can guarantee that the memcg will not be freed, but it cannot guarantee the success of
css_getto memcg.If the whole process of a cgroup offlining is completed between reading a
objcg->memcgpointer and bumping the css reference on another CPU, and there are exactly 0 external references to this memory cgroup (how we get to theobj_cgroup_charge()then?),css_get()can change the ref counter from 0 back to 1.
Quoted 3 lines
Fixes bf4f059954dc ("mm: memcg/slab: obj_cgroup API")Signed-off-by Muchun Song <[email protected]>Acked-by Roman Gushchin <[email protected]>
Re: [PATCH v2] mm: memcg/slab: Fix use after free in obj_cgroup_charge
In reply to Muchun SongQuoted 11 lines · Muchun Song
The rcu_read_lock/unlock only can guarantee that the memcg will not be freed, but it cannot guarantee the success of
css_getto memcg.If the whole process of a cgroup offlining is completed between reading a
objcg->memcgpointer and bumping the css reference on another CPU, and there are exactly 0 external references to this memory cgroup (how we get to theobj_cgroup_charge()then?),css_get()can change the ref counter from 0 back to 1.
Quoted 3 lines
Fixes bf4f059954dc ("mm: memcg/slab: obj_cgroup API")Signed-off-by Muchun Song <[email protected]>Acked-by Roman Gushchin <[email protected]>
Hi Andrew,
Maybe you forgot to add this to the queue for the merge window?
Thanks.
Quoted 26 lines
---
changelog in v2:
1. Add unlikely and update the commit log suggested by Roman.mm/memcontrol.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)mm/memcontrol.c +3
-2@@ -3221,8 +3221,10 @@ int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size)* independently later. */ rcu_read_lock (); +retry: memcg =obj_cgroup_memcg (objcg); -css_get (&memcg->css); +if (unlikely (!css_tryget (&memcg->css))) + goto retry;rcu_read_unlock (); nr_pages = size >>PAGE_SHIFT ; --2.20.1
PATCH v2 1/1 mm: memcg/slab: Rename *_lruvec_slab_state to *_lruvec_kmem_state
In reply to Muchun SongThe *_lruvec_slab_state is also suitable for pages allocated from buddy, not just for the slab objects. But the function name seems to tell us that only slab object is applicable. So we can rename the keyword of slab to kmem.
---
changelog in v2:
1. Remove VM_BUG_ON suggested by Roman.
include/linux/memcontrol.h | 18 +++++++++--------- kernel/fork.c | 2 +- mm/memcontrol.c | 2 +- mm/workingset.c | 8 ++++---- 4 files changed, 15 insertions(+), 15 deletions(-)
include/linux/memcontrol.h +9 -9
@@ -793,15 +793,15 @@ void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, int val);void __mod_lruvec_state (struct lruvec *lruvec,enum node_stat_item idx,int val); -void __mod_lruvec_slab_state (void *p,enum node_stat_item idx,int val); +void __mod_lruvec_kmem_state (void *p,enum node_stat_item idx,int val); -static inline void mod_lruvec_slab_state (void *p,enum node_stat_item idx, +static inline void mod_lruvec_kmem_state (void *p,enum node_stat_item idx,int val) {unsigned long flags;local_irq_save (flags); -__mod_lruvec_slab_state (p, idx, val); +__mod_lruvec_kmem_state (p, idx, val);local_irq_restore (flags); }
@@ -1227,7 +1227,7 @@ static inline void mod_lruvec_page_state(struct page *page,mod_node_page_state (page_pgdat (page), idx, val); } -static inline void __mod_lruvec_slab_state (void *p,enum node_stat_item idx, +static inline void __mod_lruvec_kmem_state (void *p,enum node_stat_item idx,int val) {struct page *page =virt_to_head_page (p);
@@ -1235,7 +1235,7 @@ static inline void __mod_lruvec_slab_state(void *p, enum node_stat_item idx,__mod_node_page_state (page_pgdat (page), idx, val); } -static inline void mod_lruvec_slab_state (void *p,enum node_stat_item idx, +static inline void mod_lruvec_kmem_state (void *p,enum node_stat_item idx,int val) {struct page *page =virt_to_head_page (p);
@@ -1330,14 +1330,14 @@ static inline void __dec_lruvec_page_state(struct page *page,__mod_lruvec_page_state (page, idx,-1 ); } -static inline void __inc_lruvec_slab_state (void *p,enum node_stat_item idx) +static inline void __inc_lruvec_kmem_state (void *p,enum node_stat_item idx) { -__mod_lruvec_slab_state (p, idx,1 ); +__mod_lruvec_kmem_state (p, idx,1 ); } -static inline void __dec_lruvec_slab_state (void *p,enum node_stat_item idx) +static inline void __dec_lruvec_kmem_state (void *p,enum node_stat_item idx) { -__mod_lruvec_slab_state (p, idx,-1 ); +__mod_lruvec_kmem_state (p, idx,-1 ); }/* idx can be of type enum memcg_stat_item or node_stat_item */
kernel/fork.c +1 -1
@@ -384,7 +384,7 @@ static void account_kernel_stack(struct task_struct *tsk, int account)mod_lruvec_page_state (vm->pages[0 ],NR_KERNEL_STACK_KB , account * (THREAD_SIZE /1024 )); else -mod_lruvec_slab_state (stack ,NR_KERNEL_STACK_KB , +mod_lruvec_kmem_state (stack ,NR_KERNEL_STACK_KB , account * (THREAD_SIZE / 1024)); }
mm/memcontrol.c +1 -1
@@ -866,7 +866,7 @@ void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,__mod_memcg_lruvec_state (lruvec, idx, val); } -void __mod_lruvec_slab_state (void *p,enum node_stat_item idx,int val) +void __mod_lruvec_kmem_state (void *p,enum node_stat_item idx,int val) {pg_data_t *pgdat =page_pgdat (virt_to_page (p));struct mem_cgroup *memcg;
mm/workingset.c +4 -4
@@ -445,12 +445,12 @@ void workingset_update_node(struct xa_node *node)if (node->count && node->count == node->nr_values) {if (list_empty (&node->private_list)) {list_lru_add (&shadow_nodes, &node->private_list); -__inc_lruvec_slab_state (node,WORKINGSET_NODES ); +__inc_lruvec_kmem_state (node,WORKINGSET_NODES ); } }else {if (!list_empty (&node->private_list)) {list_lru_del (&shadow_nodes, &node->private_list); -__dec_lruvec_slab_state (node,WORKINGSET_NODES ); +__dec_lruvec_kmem_state (node,WORKINGSET_NODES ); } } }
@@ -541,7 +541,7 @@ static enum lru_status shadow_lru_isolate(struct list_head *item, }list_lru_isolate (lru, item); -__dec_lruvec_slab_state (node,WORKINGSET_NODES ); +__dec_lruvec_kmem_state (node,WORKINGSET_NODES );spin_unlock (lru_lock);
@@ -564,7 +564,7 @@ static enum lru_status shadow_lru_isolate(struct list_head *item,* shadow entries we were tracking ... */ xas_store (&xas,NULL ); -__inc_lruvec_slab_state (node,WORKINGSET_NODERECLAIM ); +__inc_lruvec_kmem_state (node,WORKINGSET_NODERECLAIM ); out_invalid:xa_unlock_irq (&mapping->i_pages);
Signature · 1 line
2.20.1
Re: [PATCH v2] mm: memcg/slab: Rename *_lruvec_slab_state to *_lruvec_kmem_state
Quoted 8 lines · Muchun Song
The
*_lruvec_slab_stateis also suitable for pages allocated from buddy, not just for the slab objects. But the function name seems to tell us that only slab object is applicable. So we can rename the keyword of slab to kmem.Signed-off-by Muchun Song <[email protected]>Acked-by Roman Gushchin <[email protected]>
PATCH v2 1/1 mm: memcontrol: Simplify the mem_cgroup_page_lruvec
In reply to Muchun SongWe can reuse the code of mem_cgroup_lruvec() to simplify the code of the mem_cgroup_page_lruvec().
---
changelog in v2:
1. Move mem_cgroup_node_lruvec to memcontrol.c to avoid abuse.
include/linux/memcontrol.h | 41 ++++------------------------- mm/memcontrol.c | 53 ++++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 52 deletions(-)
include/linux/memcontrol.h +5 -36
@@ -445,48 +445,17 @@ void mem_cgroup_uncharge_list(struct list_head *page_list);void mem_cgroup_migrate (struct page *oldpage,struct page *newpage); -static struct mem_cgroup_per_node * +static inline struct mem_cgroup_per_node *mem_cgroup_nodeinfo (struct mem_cgroup *memcg,int nid) {return memcg->nodeinfo[nid]; } -/** -* mem_cgroup_lruvec - get the lru list vector for a memcg & node -* @memcg: memcg of the wanted lruvec -* -* Returns the lru list vector holding pages for a given @memcg & -* @node combination. This can be the node lruvec, if the memory -* controller is disabled. -*/ -static inline struct lruvec *mem_cgroup_lruvec (struct mem_cgroup *memcg, -struct pglist_data *pgdat) -{ -struct mem_cgroup_per_node *mz; -struct lruvec *lruvec; - -if (mem_cgroup_disabled ()) { - lruvec = &pgdat->__lruvec; - goto out; - } - -if (!memcg) - memcg = root_mem_cgroup; - - mz =mem_cgroup_nodeinfo (memcg, pgdat->node_id); - lruvec = &mz->lruvec; -out: -/* -* Since a node can be onlined after the mem_cgroup was created, -* we have to be prepared to initialize lruvec->pgdat here; -* and if offlined then reonlined, we need to reinitialize it. -*/ -if (unlikely (lruvec->pgdat != pgdat)) - lruvec->pgdat = pgdat; -return lruvec; -} +struct lruvec *mem_cgroup_lruvec (struct mem_cgroup *memcg, +struct pglist_data *pgdat); -struct lruvec *mem_cgroup_page_lruvec (struct page *,struct pglist_data *); +struct lruvec *mem_cgroup_page_lruvec (struct page *page, +struct pglist_data *pgdat);struct mem_cgroup *mem_cgroup_from_task (struct task_struct *p);
mm/memcontrol.c +37 -16
81 lines not on this page · raw message
Signature · 1 line
2.20.1
Michal Hocko
reviewerRe: [PATCH v2] mm: memcontrol: Simplify the mem_cgroup_page_lruvec
On Wed 28-10-20 11:50:13, Muchun Song wrote:
[...]
mm/memcontrol.c int mem_cgroup_scan_tasks(struct mem_cgroup *memcg, · line 1341
Quoted 4 lines
mm/memcontrol.c +3
-1-struct lruvec *mem_cgroup_page_lruvec (struct page *page,struct pglist_data *pgdat) +static struct lruvec * +__mem_cgroup_node_lruvec (struct mem_cgroup *memcg,struct pglist_data *pgdat, +int nid)
I thought I have made it clear that this is not a good approach. Please do not repost new version without that being addressed. If there are any questions then feel free to ask for details.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH v2] mm: memcontrol: Simplify the mem_cgroup_page_lruvec
Quoted 11 lines · Michal Hocko
On Wed 28-10-20 11:50:13, Muchun Song wrote:
[...]mm/memcontrol.c int mem_cgroup_scan_tasks(struct mem_cgroup *memcg, · line 1341
Quoted 4 lines
mm/memcontrol.c +3
-1-struct lruvec *mem_cgroup_page_lruvec (struct page *page,struct pglist_data *pgdat) +static struct lruvec * +__mem_cgroup_node_lruvec (struct mem_cgroup *memcg,struct pglist_data *pgdat, +int nid)I thought I have made it clear that this is not a good approach. Please do not repost new version without that being addressed. If there are any questions then feel free to ask for details.
You can get nid from pgdat (pgdat->node_id) and also pgdat from nid (NODE_DATA(nid)), so, __mem_cgroup_node_lruvec() only need one of them as parameter.
Michal Hocko
reviewerRe: [PATCH v2] mm: memcontrol: Simplify the mem_cgroup_page_lruvec
Quoted 13 lines · Shakeel Butt
Quoted 11 lines · Michal Hocko
On Wed 28-10-20 11:50:13, Muchun Song wrote:
[...]mm/memcontrol.c int mem_cgroup_scan_tasks(struct mem_cgroup *memcg, · line 1341
Quoted 4 lines
mm/memcontrol.c +3
-1-struct lruvec *mem_cgroup_page_lruvec (struct page *page,struct pglist_data *pgdat) +static struct lruvec * +__mem_cgroup_node_lruvec (struct mem_cgroup *memcg,struct pglist_data *pgdat, +int nid)I thought I have made it clear that this is not a good approach. Please do not repost new version without that being addressed. If there are any questions then feel free to ask for details.
Quoted 3 lines
You can get nid from pgdat (
pgdat->node_id) and also pgdat from nid (NODE_DATA(nid)), so,__mem_cgroup_node_lruvec()only need one of them as parameter.
Exactly what I've said in the previous version review. I suspect that the issue is that mem_cgroup_page_nodeinfo (based on page's node_id) and the given pgdat can mismatch in the existing code but that shouldn't be a real problem because the mismatch can only happen for lruvec->pgdat == NULL unless I am missing something.
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 11 lines · Muchun Song
Consider the following memcg hierarchy.
root / \ A BIf we get the objcg of memcg A failed, the
get_obj_cgroup_from_currentcan return the wrong objcg for the root memcg.Fixes bf4f059954dc ("mm: memcg/slab: obj_cgroup API")Signed-off-by Muchun Song <[email protected]>
Thanks!
Quoted 8 lines · Muchun Song
Consider the following memcg hierarchy.
root / \ A BIf we get the objcg of memcg A failed,
Please fix the above statement.
Quoted 17 lines
the
get_obj_cgroup_from_current
can return the wrong objcg for the root memcg.Fixes bf4f059954dc ("mm: memcg/slab: obj_cgroup API")Signed-off-by Muchun Song <[email protected]>---
changelog in v2:
1. Do not use a comparison with theroot_mem_cgroupmm/memcontrol.c | 1 + 1 file changed, 1 insertion(+)mm/memcontrol.c +0
-0
mm/memcontrol.c __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) · line 2964
Quoted 4 lines
mm/memcontrol.c +1
-0@@ -2961,6 +2961,7 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) objcg =rcu_dereference (memcg->objcg);if (objcg &&obj_cgroup_tryget (objcg))break ; + objcg =NULL ;
Roman, in your cleanup, are you planning to have objcg for root memcg as well?
Quoted 6 lines
mm/memcontrol.c +0
-1}rcu_read_unlock (); --2.20.1
Quoted 5 lines · Shakeel Butt
Quoted 3 lines · Muchun Song
Consider the following memcg hierarchy.
Quoted 7 lines
Quoted 5 lines
root / \ A BIf we get the objcg of memcg A failed,
Please fix the above statement.
Sorry, could you be more specific, I don't quite understand. Thanks.
Quoted 31 lines
Quoted 18 lines
the
get_obj_cgroup_from_current
can return the wrong objcg for the root memcg.Fixes bf4f059954dc ("mm: memcg/slab: obj_cgroup API")Signed-off-by Muchun Song <[email protected]>---
changelog in v2:
1. Do not use a comparison with theroot_mem_cgroupmm/memcontrol.c | 1 + 1 file changed, 1 insertion(+)mm/memcontrol.c +0
-0mm/memcontrol.c __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) · line 2964
Quoted 4 lines
mm/memcontrol.c +1
-0@@ -2961,6 +2961,7 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) objcg =rcu_dereference (memcg->objcg);if (objcg &&obj_cgroup_tryget (objcg))break ; + objcg =NULL ;Roman, in your cleanup, are you planning to have objcg for root memcg as well?
Quoted 6 lines
mm/memcontrol.c +0
-1}rcu_read_unlock (); --2.20.1
Quoted 11 lines · Muchun Song
Quoted 9 lines · Shakeel Butt
Quoted 7 lines · Muchun Song
Consider the following memcg hierarchy.
root / \ A B
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
If we get the objcg of memcg A failed,
Please fix the above statement.
Sorry, could you be more specific, I don't quite understand.
Fix the grammar. Something like "If we failed to get the reference on objcg of memcg A..."
Quoted 13 lines · Shakeel Butt
Quoted 11 lines · Muchun Song
Quoted 9 lines · Shakeel Butt
Quoted 7 lines · Muchun Song
Consider the following memcg hierarchy.
root / \ A B
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
If we get the objcg of memcg A failed,
Please fix the above statement.
Sorry, could you be more specific, I don't quite understand.
Fix the grammar. Something like "If we failed to get the reference on objcg of memcg A..."
Got it. Sorry for my poor English, Thanks.
Signature · 2 lines
Yours, Muchun
Quoted 24 lines · Shakeel Butt
Quoted 8 lines · Muchun Song
Consider the following memcg hierarchy.
root / \ A BIf we get the objcg of memcg A failed,
Please fix the above statement.
Quoted 12 lines
the
get_obj_cgroup_from_current
can return the wrong objcg for the root memcg.Fixes bf4f059954dc ("mm: memcg/slab: obj_cgroup API")Signed-off-by Muchun Song <[email protected]>---
changelog in v2:
1. Do not use a comparison with theroot_mem_cgroupmm/memcontrol.c | 1 + 1 file changed, 1 insertion(+)
Quoted 11 lines
mm/memcontrol.c __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) · line 2964
Quoted 9 lines
mm/memcontrol.c +1
-0@@ -2961,6 +2961,7 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) objcg =rcu_dereference (memcg->objcg);if (objcg &&obj_cgroup_tryget (objcg))break ; + objcg =NULL ;Roman, in your cleanup, are you planning to have objcg for root memcg as well?
Yes. I'll just change the for loop to include the root_mem_cgroup.
Thanks!
Quoted 38 lines · Roman Gushchin
Quoted 24 lines · Shakeel Butt
Quoted 8 lines · Muchun Song
Consider the following memcg hierarchy.
root / \ A BIf we get the objcg of memcg A failed,
Please fix the above statement.
Quoted 12 lines
the
get_obj_cgroup_from_current
can return the wrong objcg for the root memcg.Fixes bf4f059954dc ("mm: memcg/slab: obj_cgroup API")Signed-off-by Muchun Song <[email protected]>---
changelog in v2:
1. Do not use a comparison with theroot_mem_cgroupmm/memcontrol.c | 1 + 1 file changed, 1 insertion(+)Quoted 11 lines
mm/memcontrol.c __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) · line 2964
Quoted 9 lines
mm/memcontrol.c +1
-0@@ -2961,6 +2961,7 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) objcg =rcu_dereference (memcg->objcg);if (objcg &&obj_cgroup_tryget (objcg))break ; + objcg =NULL ;Roman, in your cleanup, are you planning to have objcg for root memcg as well?
Quoted 2 lines
Yes. I'll just change the for loop to include the
root_mem_cgroup.
Then do we really need this patch since it's not tagged for stable?
Quoted 39 lines · Shakeel Butt
Quoted 38 lines · Roman Gushchin
Quoted 35 lines · Shakeel Butt
Quoted 8 lines · Muchun Song
Consider the following memcg hierarchy.
root / \ A BIf we get the objcg of memcg A failed,
Please fix the above statement.
Quoted 17 lines
the
get_obj_cgroup_from_current
can return the wrong objcg for the root memcg.Fixes bf4f059954dc ("mm: memcg/slab: obj_cgroup API")Signed-off-by Muchun Song <[email protected]>---
changelog in v2:
1. Do not use a comparison with theroot_mem_cgroupmm/memcontrol.c | 1 + 1 file changed, 1 insertion(+)mm/memcontrol.c +0
-0mm/memcontrol.c __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) · line 2964
Quoted 4 lines
mm/memcontrol.c +1
-0@@ -2961,6 +2961,7 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) objcg =rcu_dereference (memcg->objcg);if (objcg &&obj_cgroup_tryget (objcg))break ; + objcg =NULL ;Roman, in your cleanup, are you planning to have objcg for root memcg as well?
Quoted 4 lines
Quoted 2 lines
Yes. I'll just change the for loop to include the
root_mem_cgroup.Then do we really need this patch since it's not tagged for stable?
A good question.
Honestly, I really doubt that obj_cgroup_tryget() can fail even with the current code. But, formally speaking, it's possible. So there is likely no practical difference if we'll apply this patch or not, even without the root handling cleanup.
But you're right, with the root handling cleanup, it will make even less of a difference.
Thanks!