Skip to contentSkip to conversation

mm: memcg softlimit reclaim rework

PATCH Superseded Ying Han 3 participants 4 messages Jul 30, 2012 – Jul 31, 2012

Subsystems (5)

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Ying Han

author
#0 lore raw

PATCH v7 1/2 mm: memcg softlimit reclaim rework

This patch reverts all the existing softlimit reclaim implementations and instead integrates the softlimit reclaim into existing global reclaim logic.

The new softlimit reclaim includes the following changes:

1. add function mem_cgroup_over_soft_limit()

Add the filter function mem_cgroup_over_soft_limit() under the common function shrink_zone(). The later one is being called both from per-memcg reclaim as well as global reclaim.

Today the softlimit takes effect only under global memory pressure. The memcgs get free run above their softlimit until there is a global memory contention. This patch doesn't change the semantics.

Under the global reclaim, we try to skip reclaiming from a memcg under its softlimit. To prevent reclaim from trying too hard on hitting memcgs (above softlimit) w/ only hard-to-reclaim pages, the reclaim priority is used to skip the softlimit check. This is a trade-off of system performance and resource isolation.

2. memcg set soft_limit_in_bytes to 0 by default
This idea is based on discussion with Michal and Johannes from LSF.

a) If soft_limit are all set to MAX, it wastes first three priority iterations without scanning anything.

b) By default every memcg is eligible for softlimit reclaim, and we can also set the value to MAX for special memcg which is immune to soft limit reclaim.

There is a behavior change after this patch: (N == DEF_PRIORITY - 2)

        A: usage > softlimit        B: usage <= softlimit        U: softlimit unset
old:    reclaim at each priority    reclaim when priority < N    reclaim when priority < N
new:    reclaim at each priority    reclaim when priority < N    reclaim at each priority

Note: I can leave the counter->soft_limit uninitialized, at least all the caller of res_counter_init() have the memcg as pre-zeroed structure. However, I might be better not rely on that.

3. forbid setting soft limit on root cgroup

Setting a soft limit in the root cgroup does not make sense, as soft limits are enforced hierarchically and the root cgroup is the hierarchical parent of every other cgroup. It would not provide the discrimination between groups that soft limits are usually used for.

With the current implementation of soft limits, it would only make global reclaim more aggressive compared to target reclaim, but we absolutely don't want anyone to rely on this behaviour.

4. "hierarchical" softlimit reclaim

This is consistant to how softlimit was previously implemented, where the pressure is put for the whole hiearchy as long as the "root" of the hierarchy over its softlimit.

What's the trusted and untrusted setups ?

case 1 : Administrator is the only one setting up the limits and also he expects gurantees of memory under each cgroup's softlimit:

Considering the following:

root (soft: unlimited, use_hierarchy = 1)
-- A (soft: unlimited, usage 22G)

      -- A1 (soft: 10G, usage 17G)
      -- A2 (soft: 6G, usage 5G)

-- B (soft: 16G, usage 10G)

So we have A1 above its softlimit and none of its ancestor does, then global reclaim will only pick A1 to reclaim first.

case 2: Untrusted enviroment where cgroups changes its softlimit or adminstrator could make mistakes. In that case, we still want to attack the mis-configured child if its parent is above softlimit.

Considering the following:

root (soft: unlimited, use_hierarchy = 1)
-- A (soft: 16G, usage 22G)

      -- A1 (soft: 10G, usage 17G)
      -- A2 (soft: 1000G, usage 5G)

-- B (soft: 16G, usage 10G)

Here A2 would set its softlimit way higher than its parent, but the current logic makes sure to still attack it when A exceeds its softlimit.

v7..v6:
1. rebase to mmotm-2012-07-25-16-41

v5..v6:
1. updated w/ kosaki's latest patch.
2. squashed the "set softlimit to 0" patch.
3. applied patch from johannes which disallow root to change its softlimit.

v4..v5:
1. rebase the patchset on memcg-dev tree
2. apply KOSAKI's patch on do_try_to_free_pages()

---
 include/linux/memcontrol.h |   20 +--
 include/linux/swap.h       |    4 -
 kernel/res_counter.c       |    2 +-
 mm/memcontrol.c            |  461 +++-----------------------------------------
 mm/vmscan.c                |   82 ++-------
 5 files changed, 57 insertions(+), 512 deletions(-)
include/linux/memcontrol.h +8 -12
index 8d9489f..65538f9 100644
@@ -122,6 +122,8 @@ extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
 extern void mem_cgroup_replace_page_cache(struct page *oldpage,
 					struct page *newpage);
 
+extern bool mem_cgroup_over_soft_limit(struct mem_cgroup *memcg);
+
 #ifdef CONFIG_MEMCG_SWAP
 extern int do_swap_account;
 #endif
@@ -177,10 +179,6 @@ static inline void mem_cgroup_dec_page_stat(struct page *page,
 	mem_cgroup_update_page_stat(page, idx, -1);
 }
 
-unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
-						gfp_t gfp_mask,
-						unsigned long *total_scanned);
-
 void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 void mem_cgroup_split_huge_fixup(struct page *head);
@@ -354,14 +352,6 @@ static inline void mem_cgroup_dec_page_stat(struct page *page,
 {
 }
 
-static inline
-unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
-					    gfp_t gfp_mask,
-					    unsigned long *total_scanned)
-{
-	return 0;
-}
-
 static inline void mem_cgroup_split_huge_fixup(struct page *head)
 {
 }
@@ -374,6 +364,12 @@ static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
 				struct page *newpage)
 {
 }
+
+static inline
+bool mem_cgroup_over_soft_limit(struct mem_cgroup *memcg)
+{
+	return true;
+}
 #endif /* CONFIG_MEMCG */
 
 #if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
include/linux/swap.h +0 -4
index 388e706..efcce0d 100644
@@ -259,10 +259,6 @@ extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
 extern int __isolate_lru_page(struct page *page, isolate_mode_t mode);
 extern unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem,
 						  gfp_t gfp_mask, bool noswap);
-extern unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *mem,
-						gfp_t gfp_mask, bool noswap,
-						struct zone *zone,
-						unsigned long *nr_scanned);
 extern unsigned long shrink_all_memory(unsigned long nr_pages);
 extern int vm_swappiness;
 extern int remove_mapping(struct address_space *mapping, struct page *page);
kernel/res_counter.c +1 -1
index ad581aa..6db7e68 100644
@@ -18,7 +18,7 @@ void res_counter_init(struct res_counter *counter, struct res_counter *parent)
 {
 	spin_lock_init(&counter->lock);
 	counter->limit = RESOURCE_MAX;
-	counter->soft_limit = RESOURCE_MAX;
+	counter->soft_limit = 0;
 	counter->parent = parent;
 }
 
mm/memcontrol.c +32 -429

591 lines not on this page · raw message

mm/vmscan.c +16 -66

118 lines not on this page · raw message

Signature · 7 lines
1.7.7.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw

mm/memcontrol.c struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) · line 738

Quoted 7 lines · Ying Han
mm/memcontrol.c +7 -0
+	for (; memcg; memcg = parent_mem_cgroup(memcg)) {
+		/* This is global reclaim, stop at root cgroup */
+		if (mem_cgroup_is_root(memcg))
+			break;
+		if (res_counter_soft_limit_excess(&memcg->res))
+			return true;
+	}

I like the simplification of mem_cgroup_over_soft_limit.

Quoted 12 lines

+++ b/mm/vmscan.c

mm/vmscan.c +6 -1
@@ -1866,7 +1866,22 @@ static void shrink_zone(struct zone *zone, struct scan_control *sc)
  	do {
  		struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
 
-		shrink_lruvec(lruvec, sc);
+		/*
+		 * Reclaim from mem_cgroup if any of these conditions are met:
+		 * - this is a targetted reclaim ( not global reclaim)
+		 * - reclaim priority is less than  DEF_PRIORITY - 2
+		 * - mem_cgroup or its ancestor ( not including root cgroup)
+		 * exceeds its soft limit

mm/vmscan.c static void shrink_zone(struct zone *zone, struct scan_control *sc) · line 1884

Quoted 10 lines
mm/vmscan.c +10 -0
+		 *
+		 * Note: The priority check is a balance of how hard to
+		 * preserve the pages under softlimit. If the memcgs of the
+		 * zone having trouble to reclaim pages above their softlimit,
+		 * we have to reclaim under softlimit instead of burning more
+		 * cpu cycles.
+		 */
+		if (!global_reclaim(sc) || sc->priority<  DEF_PRIORITY - 2 ||
+				mem_cgroup_over_soft_limit(memcg))
+			shrink_lruvec(lruvec, sc);

This seems like a bad idea.

Once priority hits DEF_PRIORITY-2, we also end up doing various other things, like sleeping in the main reclaim loop to wait for IO to finish. If we use DEF_PRIORITY-1 as the threshold to skip reclaim when no group is over its soft limit, we may end up sleeping for nothing, and slowing things down terribly.

Let me try to whip up a quick prototype of the LRU weighing idea I described at LSF/MM in San Francisco...

Signature · 7 lines
All rights reversed

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw
In reply to Ying Han

Sprry for my long silence in the last rounds. I was following discussions but didn't get to step in.

Quoted 4 lines · Ying Han

This patch reverts all the existing softlimit reclaim implementations and instead integrates the softlimit reclaim into existing global reclaim logic.

The new softlimit reclaim includes the following changes:

The patch seems to be doing too many things but I do not want to get into "split it this way or that way" now. It is probably better to have it like this for now and take care about these details later.

[...]

Quoted 10 lines

3. forbid setting soft limit on root cgroup

Setting a soft limit in the root cgroup does not make sense, as soft limits are enforced hierarchically and the root cgroup is the hierarchical parent of every other cgroup. It would not provide the discrimination between groups that soft limits are usually used for.

With the current implementation of soft limits, it would only make global reclaim more aggressive compared to target reclaim, but we absolutely don't want anyone to rely on this behaviour.

Hmm, maybe this one can go in sooner without the rest.

[...]

Quoted 18 lines
mm/vmscan.c +9 -1
index 3e0d0cd..59e633c 100644
@@ -1866,7 +1866,22 @@ static void shrink_zone(struct zone *zone, struct scan_control *sc)
 	do {
 		struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
 
-		shrink_lruvec(lruvec, sc);
+		/*
+		 * Reclaim from mem_cgroup if any of these conditions are met:
+		 * - this is a targetted reclaim ( not global reclaim)
+		 * - reclaim priority is less than  DEF_PRIORITY - 2
+		 * - mem_cgroup or its ancestor ( not including root cgroup)
+		 * exceeds its soft limit
+		 *
+		 * Note: The priority check is a balance of how hard to
+		 * preserve the pages under softlimit. If the memcgs of the

mm/vmscan.c static void shrink_zone(struct zone *zone, struct scan_control *sc) · line 1887

Quoted 10 lines
mm/vmscan.c +7 -0
+		 * zone having trouble to reclaim pages above their softlimit,
+		 * we have to reclaim under softlimit instead of burning more
+		 * cpu cycles.
+		 */
+		if (!global_reclaim(sc) || sc->priority < DEF_PRIORITY - 2 ||
+				mem_cgroup_over_soft_limit(memcg))
+			shrink_lruvec(lruvec, sc);
 
 		/*
 		 * Limit reclaim has historically picked one memcg and

[...]

Looks quite straightforward. I have to think about it some more but I like it for starter. Do you have any test results from the overcommitted system?

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author
#3 lore raw
Quoted 2 lines · Michal Hocko

Sprry for my long silence in the last rounds. I was following discussions but didn't get to step in.

Thank you for stepping in this round :)

Quoted 10 lines · Ying Han
Quoted 4 lines

This patch reverts all the existing softlimit reclaim implementations and instead integrates the softlimit reclaim into existing global reclaim logic.

The new softlimit reclaim includes the following changes:

The patch seems to be doing too many things but I do not want to get into "split it this way or that way" now. It is probably better to have it like this for now and take care about these details later.

It was split and then being suggested to put together.

Quoted 9 lines

[...]

Quoted 7 lines

3. forbid setting soft limit on root cgroup

Setting a soft limit in the root cgroup does not make sense, as soft limits are enforced hierarchically and the root cgroup is the hierarchical parent of every other cgroup. It would not provide the discrimination between groups that soft limits are usually used for.

Quoted 5 lines
Quoted 3 lines

With the current implementation of soft limits, it would only make global reclaim more aggressive compared to target reclaim, but we absolutely don't want anyone to rely on this behaviour.

Hmm, maybe this one can go in sooner without the rest.

Well, I have no problem to split this out if that works for ppl.

Quoted 28 lines

[...]

Quoted 26 lines
mm/vmscan.c +16 -1
index 3e0d0cd..59e633c 100644
@@ -1866,7 +1866,22 @@ static void shrink_zone(struct zone *zone, struct scan_control *sc)
      do {
              struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
 
-             shrink_lruvec(lruvec, sc);
+             /*
+              * Reclaim from mem_cgroup if any of these conditions are met:
+              * - this is a targetted reclaim ( not global reclaim)
+              * - reclaim priority is less than  DEF_PRIORITY - 2
+              * - mem_cgroup or its ancestor ( not including root cgroup)
+              * exceeds its soft limit
+              *
+              * Note: The priority check is a balance of how hard to
+              * preserve the pages under softlimit. If the memcgs of the
+              * zone having trouble to reclaim pages above their softlimit,
+              * we have to reclaim under softlimit instead of burning more
+              * cpu cycles.
+              */
+             if (!global_reclaim(sc) || sc->priority < DEF_PRIORITY - 2 ||
+                             mem_cgroup_over_soft_limit(memcg))
+                     shrink_lruvec(lruvec, sc);
 
Quoted 7 lines

mm/vmscan.c static void shrink_zone(struct zone *zone, struct scan_control *sc) · line 1887

Quoted 2 lines
mm/vmscan.c +0 -0
              /*
               * Limit reclaim has historically picked one memcg and

[...]

Looks quite straightforward. I have to think about it some more but I like it for starter. Do you have any test results from the overcommitted system?

I ran some tests while over-commiting the soft limit and triggering global reclaim. Let me post the result here.

--Ying

Quoted 3 lines

--
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>