Skip to content

Commit c268e99

Browse files
Daisuke Nishimuratorvalds
authored andcommitted
memcg: fix hierarchical reclaim
If root_mem has no children, last_scaned_child is set to root_mem itself. But after some children added to root_mem, mem_cgroup_get_next_node can mem_cgroup_put the root_mem although root_mem has not been mem_cgroup_get. This patch fixes this behavior by: - Set last_scanned_child to NULL if root_mem has no children or DFS search has returned to root_mem itself(root_mem is not a "child" of root_mem). Make mem_cgroup_get_first_node return root_mem in this case. There are no mem_cgroup_get/put for root_mem. - Rename mem_cgroup_get_next_node to __mem_cgroup_get_next_node, and mem_cgroup_get_first_node to mem_cgroup_get_next_node. Make mem_cgroup_hierarchical_reclaim call only new mem_cgroup_get_next_node. Signed-off-by: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp> Cc: Balbir Singh <balbir@linux.vnet.ibm.com> Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Pavel Emelyanov <xemul@openvz.org> Cc: Li Zefan <lizf@cn.fujitsu.com> Cc: Paul Menage <menage@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 40d5813 commit c268e99

1 file changed

Lines changed: 36 additions & 32 deletions

File tree

‎mm/memcontrol.c‎

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
633633
* called with hierarchy_mutex held
634634
*/
635635
static struct mem_cgroup *
636-
mem_cgroup_get_next_node(struct mem_cgroup *curr, struct mem_cgroup *root_mem)
636+
__mem_cgroup_get_next_node(struct mem_cgroup *curr, struct mem_cgroup *root_mem)
637637
{
638638
struct cgroup *cgroup, *curr_cgroup, *root_cgroup;
639639

@@ -644,31 +644,26 @@ mem_cgroup_get_next_node(struct mem_cgroup *curr, struct mem_cgroup *root_mem)
644644
/*
645645
* Walk down to children
646646
*/
647-
mem_cgroup_put(curr);
648647
cgroup = list_entry(curr_cgroup->children.next,
649648
struct cgroup, sibling);
650649
curr = mem_cgroup_from_cont(cgroup);
651-
mem_cgroup_get(curr);
652650
goto done;
653651
}
654652

655653
visit_parent:
656654
if (curr_cgroup == root_cgroup) {
657-
mem_cgroup_put(curr);
658-
curr = root_mem;
659-
mem_cgroup_get(curr);
655+
/* caller handles NULL case */
656+
curr = NULL;
660657
goto done;
661658
}
662659

663660
/*
664661
* Goto next sibling
665662
*/
666663
if (curr_cgroup->sibling.next != &curr_cgroup->parent->children) {
667-
mem_cgroup_put(curr);
668664
cgroup = list_entry(curr_cgroup->sibling.next, struct cgroup,
669665
sibling);
670666
curr = mem_cgroup_from_cont(cgroup);
671-
mem_cgroup_get(curr);
672667
goto done;
673668
}
674669

@@ -679,7 +674,6 @@ mem_cgroup_get_next_node(struct mem_cgroup *curr, struct mem_cgroup *root_mem)
679674
goto visit_parent;
680675

681676
done:
682-
root_mem->last_scanned_child = curr;
683677
return curr;
684678
}
685679

@@ -689,40 +683,46 @@ mem_cgroup_get_next_node(struct mem_cgroup *curr, struct mem_cgroup *root_mem)
689683
* that to reclaim free pages from.
690684
*/
691685
static struct mem_cgroup *
692-
mem_cgroup_get_first_node(struct mem_cgroup *root_mem)
686+
mem_cgroup_get_next_node(struct mem_cgroup *root_mem)
693687
{
694688
struct cgroup *cgroup;
695-
struct mem_cgroup *ret;
689+
struct mem_cgroup *orig, *next;
696690
bool obsolete;
697691

698-
obsolete = mem_cgroup_is_obsolete(root_mem->last_scanned_child);
699-
700692
/*
701693
* Scan all children under the mem_cgroup mem
702694
*/
703695
mutex_lock(&mem_cgroup_subsys.hierarchy_mutex);
696+
697+
orig = root_mem->last_scanned_child;
698+
obsolete = mem_cgroup_is_obsolete(orig);
699+
704700
if (list_empty(&root_mem->css.cgroup->children)) {
705-
ret = root_mem;
701+
/*
702+
* root_mem might have children before and last_scanned_child
703+
* may point to one of them. We put it later.
704+
*/
705+
if (orig)
706+
VM_BUG_ON(!obsolete);
707+
next = NULL;
706708
goto done;
707709
}
708710

709-
if (!root_mem->last_scanned_child || obsolete) {
710-
711-
if (obsolete && root_mem->last_scanned_child)
712-
mem_cgroup_put(root_mem->last_scanned_child);
713-
711+
if (!orig || obsolete) {
714712
cgroup = list_first_entry(&root_mem->css.cgroup->children,
715713
struct cgroup, sibling);
716-
ret = mem_cgroup_from_cont(cgroup);
717-
mem_cgroup_get(ret);
714+
next = mem_cgroup_from_cont(cgroup);
718715
} else
719-
ret = mem_cgroup_get_next_node(root_mem->last_scanned_child,
720-
root_mem);
716+
next = __mem_cgroup_get_next_node(orig, root_mem);
721717

722718
done:
723-
root_mem->last_scanned_child = ret;
719+
if (next)
720+
mem_cgroup_get(next);
721+
root_mem->last_scanned_child = next;
722+
if (orig)
723+
mem_cgroup_put(orig);
724724
mutex_unlock(&mem_cgroup_subsys.hierarchy_mutex);
725-
return ret;
725+
return (next) ? next : root_mem;
726726
}
727727

728728
static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
@@ -780,21 +780,18 @@ static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
780780
if (!root_mem->use_hierarchy)
781781
return ret;
782782

783-
next_mem = mem_cgroup_get_first_node(root_mem);
783+
next_mem = mem_cgroup_get_next_node(root_mem);
784784

785785
while (next_mem != root_mem) {
786786
if (mem_cgroup_is_obsolete(next_mem)) {
787-
mem_cgroup_put(next_mem);
788-
next_mem = mem_cgroup_get_first_node(root_mem);
787+
next_mem = mem_cgroup_get_next_node(root_mem);
789788
continue;
790789
}
791790
ret = try_to_free_mem_cgroup_pages(next_mem, gfp_mask, noswap,
792791
get_swappiness(next_mem));
793792
if (mem_cgroup_check_under_limit(root_mem))
794793
return 0;
795-
mutex_lock(&mem_cgroup_subsys.hierarchy_mutex);
796-
next_mem = mem_cgroup_get_next_node(next_mem, root_mem);
797-
mutex_unlock(&mem_cgroup_subsys.hierarchy_mutex);
794+
next_mem = mem_cgroup_get_next_node(root_mem);
798795
}
799796
return ret;
800797
}
@@ -2254,7 +2251,14 @@ static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
22542251
static void mem_cgroup_destroy(struct cgroup_subsys *ss,
22552252
struct cgroup *cont)
22562253
{
2257-
mem_cgroup_put(mem_cgroup_from_cont(cont));
2254+
struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2255+
struct mem_cgroup *last_scanned_child = mem->last_scanned_child;
2256+
2257+
if (last_scanned_child) {
2258+
VM_BUG_ON(!mem_cgroup_is_obsolete(last_scanned_child));
2259+
mem_cgroup_put(last_scanned_child);
2260+
}
2261+
mem_cgroup_put(mem);
22582262
}
22592263

22602264
static int mem_cgroup_populate(struct cgroup_subsys *ss,

0 commit comments

Comments
 (0)