Skip to contentSkip to conversation

add res_counter_usage_safe

RFC PATCH Superseded Kamezawa Hiroyuki 5 participants 9 messages Jun 28, 2012 – Jul 4, 2012

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/2 add res_counter_usage_safe

This series is a cleaned up patches discussed in a few days ago, the topic was how to make compaction works well even if there is a memcg under OOM. ==
memcg: add res_counter_usage_safe()

I think usage > limit means a sign of BUG. But, sometimes, res_counter_charge_nofail() is very convenient. tcp_memcg uses it. And I'd like to use it for helping page migration.

This patch adds res_counter_usage_safe() which returns min(usage,limit). By this we can use res_counter_charge_nofail() without breaking user experience.

---
 include/linux/res_counter.h |    2 ++
 kernel/res_counter.c        |   15 +++++++++++++++
 net/ipv4/tcp_memcontrol.c   |    2 +-
 3 files changed, 18 insertions(+), 1 deletions(-)
include/linux/res_counter.h +2 -0
index 7d7fbe2..a6f8cc5 100644
@@ -226,4 +226,6 @@ res_counter_set_soft_limit(struct res_counter *cnt,
 	return 0;
 }
 
+u64 res_counter_usage_safe(struct res_counter *cnt);
+
 #endif
kernel/res_counter.c +15 -0
index ad581aa..e84149b 100644
@@ -171,6 +171,21 @@ u64 res_counter_read_u64(struct res_counter *counter, int member)
 }
 #endif
 
+/*
+ * Returns usage. If usage > limit, limit is returned.
+ * This is useful not to break user experiance if the excess
+ * is temporal.
+ */
+u64 res_counter_usage_safe(struct res_counter *counter)
+{
+	u64 usage, limit;
+
+	limit = res_counter_read_u64(counter, RES_LIMIT);
+	usage = res_counter_read_u64(counter, RES_USAGE);
+
+	return min(usage, limit);
+}
+
 int res_counter_memparse_write_strategy(const char *buf,
 					unsigned long long *res)
 {
net/ipv4/tcp_memcontrol.c +1 -1
index b6f3583..a73dce6 100644
@@ -180,7 +180,7 @@ static u64 tcp_read_usage(struct mem_cgroup *memcg)
 		return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT;
 
 	tcp = tcp_from_cgproto(cg_proto);
-	return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_USAGE);
+	return res_counter_usage_safe(&tcp->tcp_memory_allocated);
 }
 
 static u64 tcp_cgroup_read(struct cgroup *cont, struct cftype *cft)
Signature · 8 lines
1.7.4.1


--
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

RFC PATCH 2/2 memcg : remove -ENOMEM at page migration.

For handling many kinds of races, memcg adds an extra charge to page's memcg at page migration. But this affects the page compaction and make it fail if the memcg is under OOM.

This patch uses res_counter_charge_nofail() in page migration path and remove -ENOMEM. By this, page migration will not fail by the status of memcg.

---
 mm/memcontrol.c |   26 +++++++-------------------
 1 files changed, 7 insertions(+), 19 deletions(-)
mm/memcontrol.c +7 -19
index a2677e0..7424fab 100644
@@ -3168,6 +3168,7 @@ int mem_cgroup_prepare_migration(struct page *page,
 	struct page *newpage, struct mem_cgroup **memcgp, gfp_t gfp_mask)
 {
 	struct mem_cgroup *memcg = NULL;
+	struct res_counter *dummy;
 	struct page_cgroup *pc;
 	enum charge_type ctype;
 	int ret = 0;
@@ -3222,29 +3223,16 @@ int mem_cgroup_prepare_migration(struct page *page,
 	 */
 	if (!memcg)
 		return 0;
-
-	*memcgp = memcg;
-	ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, memcgp, false);
-	css_put(&memcg->css);/* drop extra refcnt */
-	if (ret) {
-		if (PageAnon(page)) {
-			lock_page_cgroup(pc);
-			ClearPageCgroupMigration(pc);
-			unlock_page_cgroup(pc);
-			/*
-			 * The old page may be fully unmapped while we kept it.
-			 */
-			mem_cgroup_uncharge_page(page);
-		}
-		/* we'll need to revisit this error code (we have -EINTR) */
-		return -ENOMEM;
-	}
 	/*
 	 * We charge new page before it's used/mapped. So, even if unlock_page()
 	 * is called before end_migration, we can catch all events on this new
 	 * page. In the case new page is migrated but not remapped, new page's
 	 * mapcount will be finally 0 and we call uncharge in end_migration().
 	 */
+	res_counter_charge_nofail(&memcg->res, PAGE_SIZE, &dummy);
+	if (do_swap_account)
+		res_counter_charge_nofail(&memcg->memsw, PAGE_SIZE, &dummy);
+
 	if (PageAnon(page))
 		ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
 	else if (page_is_file_cache(page))
@@ -3807,9 +3795,9 @@ static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
 
 	if (!mem_cgroup_is_root(memcg)) {
 		if (!swap)
-			return res_counter_read_u64(&memcg->res, RES_USAGE);
+			return res_counter_usage_safe(&memcg->res);
 		else
-			return res_counter_read_u64(&memcg->memsw, RES_USAGE);
+			return res_counter_usage_safe(&memcg->memsw);
 	}
 
 	val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
Signature · 8 lines
1.7.4.1


--
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>
Acked-by on v1 1/2
#4 lore raw
In reply to Kamezawa Hiroyuki
Quoted 9 lines · Kamezawa Hiroyuki

I think usage > limit means a sign of BUG. But, sometimes, res_counter_charge_nofail() is very convenient. tcp_memcg uses it. And I'd like to use it for helping page migration.

This patch adds res_counter_usage_safe() which returns min(usage,limit). By this we can use res_counter_charge_nofail() without breaking user experience.

I totally agree.

It would be very nice to never go over limit, but truth is, sometimes we're forced too - for a limited time. In those circumstances, it is better to actually charge memcg, so the charges won't unbalance and disappear. Every work around proposed so far for those has been to basically add some form of "extra_charge" to the memcg, that would effectively charge to it, but not display it.

The good fix is in the display side.

We should just be careful to always have good justification for no_fail usage. It should be reserved to those situations where we really need it, but that's on us on future reviews.

For the idea:

For the patch itself: I believe we can take the lock once in res_counter_usage_safe, and then read the value and the limit under it.

Calling res_counter_read_u64 two times seems not only wasteful but potentially wrong, since they can change under our nose.

--
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>

#5 lore raw
Quoted 28 lines · Glauber Costa
Quoted 9 lines · Kamezawa Hiroyuki

I think usage > limit means a sign of BUG. But, sometimes, res_counter_charge_nofail() is very convenient. tcp_memcg uses it. And I'd like to use it for helping page migration.

This patch adds res_counter_usage_safe() which returns min(usage,limit). By this we can use res_counter_charge_nofail() without breaking user experience.

I totally agree.

It would be very nice to never go over limit, but truth is, sometimes we're forced too - for a limited time. In those circumstances, it is better to actually charge memcg, so the charges won't unbalance and disappear. Every work around proposed so far for those has been to basically add some form of "extra_charge" to the memcg, that would effectively charge to it, but not display it.

The good fix is in the display side.

We should just be careful to always have good justification for no_fail usage. It should be reserved to those situations where we really need it, but that's on us on future reviews.

For the idea:

Quoted 8 lines

For the patch itself: I believe we can take the lock once in res_counter_usage_safe, and then read the value and the limit under it.

Calling res_counter_read_u64 two times seems not only wasteful but potentially wrong, since they can change under our nose.

Thank you for comments.

I'll update the patch using that way.

Thanks,
-Kame

--
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>

Acked-by on v1 1/2
#6 lore raw
In reply to Kamezawa Hiroyuki
Quoted 16 lines · Kamezawa Hiroyuki
include/linux/res_counter.h +2 -0
index 7d7fbe2..a6f8cc5 100644
@@ -226,4 +226,6 @@ res_counter_set_soft_limit(struct res_counter *cnt,
 	return 0;
 }
 
+u64 res_counter_usage_safe(struct res_counter *cnt);
+
 #endif
kernel/res_counter.c +0 -0
index ad581aa..e84149b 100644

kernel/res_counter.c u64 res_counter_read_u64(struct res_counter *counter, int member) · line 177

Quoted 7 lines
kernel/res_counter.c +4 -0
@@ -171,6 +171,21 @@ u64 res_counter_read_u64(struct res_counter *counter, int member)
 }
 #endif
 
+/*
+ * Returns usage. If usage > limit, limit is returned.
+ * This is useful not to break user experiance if the excess
+ * is temporal.

s/temporal/temporary/

Quoted 19 lines
kernel/res_counter.c +11 -0
+ */
+u64 res_counter_usage_safe(struct res_counter *counter)
+{
+	u64 usage, limit;
+
+	limit = res_counter_read_u64(counter, RES_LIMIT);
+	usage = res_counter_read_u64(counter, RES_USAGE);
+
+	return min(usage, limit);
+}
+
 int res_counter_memparse_write_strategy(const char *buf,
 					unsigned long long *res)
 {
net/ipv4/tcp_memcontrol.c +0 -0
index b6f3583..a73dce6 100644

net/ipv4/tcp_memcontrol.c static u64 tcp_read_usage(struct mem_cgroup *memcg) · line 186

Quoted 8 lines
net/ipv4/tcp_memcontrol.c +1 -1
@@ -180,7 +180,7 @@ static u64 tcp_read_usage(struct mem_cgroup *memcg)
 		return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT;
 
 	tcp = tcp_from_cgproto(cg_proto);
-	return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_USAGE);
+	return res_counter_usage_safe(&tcp->tcp_memory_allocated);
 }
 
 static u64 tcp_cgroup_read(struct cgroup *cont, struct cftype *cft)

--
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>

Acked-by on v1 2/2
#2 lore raw

Re: [RFC][PATCH 2/2] memcg : remove -ENOMEM at page migration.

In reply to Kamezawa Hiroyuki
Quoted 10 lines · Kamezawa Hiroyuki

For handling many kinds of races, memcg adds an extra charge to page's memcg at page migration. But this affects the page compaction and make it fail if the memcg is under OOM.

This patch uses res_counter_charge_nofail() in page migration path and remove -ENOMEM. By this, page migration will not fail by the status of memcg.

This is a very good improvement for page migration under memory compaction and increases the liklihood that it will do useful work for transparent hugepage allocations, thanks!

--
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>

Acked-by on v1 2/2
#3 lore raw

Re: [RFC][PATCH 2/2] memcg : remove -ENOMEM at page migration.

In reply to Kamezawa Hiroyuki
Quoted 7 lines · KAMEZAWA Hiroyuki

For handling many kinds of races, memcg adds an extra charge to page's memcg at page migration. But this affects the page compaction and make it fail if the memcg is under OOM.

This patch uses res_counter_charge_nofail() in page migration path and remove -ENOMEM. By this, page migration will not fail by the status of memcg.

Maybe we could add something like below to the changelog as well. "
Even though res_counter_charge_nofail can silently go over the memcg limit mem_cgroup_usage compensates that and it doesn't tell the real truth to the userspace.
Excessive charges are only temporal and done on a single page per-CPU in the worst case. This sounds tolerable and actually consumes less charges than the current per-cpu memcg_stock.
"

Quoted 2 lines

Thanks!

Quoted 66 lines
---
 mm/memcontrol.c |   26 +++++++-------------------
 1 files changed, 7 insertions(+), 19 deletions(-)
mm/memcontrol.c +7 -19
index a2677e0..7424fab 100644
@@ -3168,6 +3168,7 @@ int mem_cgroup_prepare_migration(struct page *page,
 	struct page *newpage, struct mem_cgroup **memcgp, gfp_t gfp_mask)
 {
 	struct mem_cgroup *memcg = NULL;
+	struct res_counter *dummy;
 	struct page_cgroup *pc;
 	enum charge_type ctype;
 	int ret = 0;
@@ -3222,29 +3223,16 @@ int mem_cgroup_prepare_migration(struct page *page,
 	 */
 	if (!memcg)
 		return 0;
-
-	*memcgp = memcg;
-	ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, memcgp, false);
-	css_put(&memcg->css);/* drop extra refcnt */
-	if (ret) {
-		if (PageAnon(page)) {
-			lock_page_cgroup(pc);
-			ClearPageCgroupMigration(pc);
-			unlock_page_cgroup(pc);
-			/*
-			 * The old page may be fully unmapped while we kept it.
-			 */
-			mem_cgroup_uncharge_page(page);
-		}
-		/* we'll need to revisit this error code (we have -EINTR) */
-		return -ENOMEM;
-	}
 	/*
 	 * We charge new page before it's used/mapped. So, even if unlock_page()
 	 * is called before end_migration, we can catch all events on this new
 	 * page. In the case new page is migrated but not remapped, new page's
 	 * mapcount will be finally 0 and we call uncharge in end_migration().
 	 */
+	res_counter_charge_nofail(&memcg->res, PAGE_SIZE, &dummy);
+	if (do_swap_account)
+		res_counter_charge_nofail(&memcg->memsw, PAGE_SIZE, &dummy);
+
 	if (PageAnon(page))
 		ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
 	else if (page_is_file_cache(page))
@@ -3807,9 +3795,9 @@ static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
 
 	if (!mem_cgroup_is_root(memcg)) {
 		if (!swap)
-			return res_counter_read_u64(&memcg->res, RES_USAGE);
+			return res_counter_usage_safe(&memcg->res);
 		else
-			return res_counter_read_u64(&memcg->memsw, RES_USAGE);
+			return res_counter_usage_safe(&memcg->memsw);
 	}
 
 	val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
Signature · 1 line
1.7.4.1
Signature · 12 lines
Michal Hocko
SUSE Labs
SUSE LINUX s.r.o.
Lihovarska 1060/12
190 00 Praha 9    
Czech Republic

--
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>
Reviewed-by on v1 1/2
#7 lore raw
In reply to Kamezawa Hiroyuki
Quoted 9 lines · KAMEZAWA Hiroyuki

This series is a cleaned up patches discussed in a few days ago, the topic was how to make compaction works well even if there is a memcg under OOM. ==
memcg: add res_counter_usage_safe()

I think usage > limit means a sign of BUG. But, sometimes, res_counter_charge_nofail() is very convenient. tcp_memcg uses it. And I'd like to use it for helping page migration.

Quoted 5 lines

This patch adds res_counter_usage_safe() which returns min(usage,limit). By this we can use res_counter_charge_nofail() without breaking user experience.

Quoted 60 lines
---
 include/linux/res_counter.h |    2 ++
 kernel/res_counter.c        |   15 +++++++++++++++
 net/ipv4/tcp_memcontrol.c   |    2 +-
 3 files changed, 18 insertions(+), 1 deletions(-)
include/linux/res_counter.h +2 -0
index 7d7fbe2..a6f8cc5 100644
@@ -226,4 +226,6 @@ res_counter_set_soft_limit(struct res_counter *cnt,
 	return 0;
 }
 
+u64 res_counter_usage_safe(struct res_counter *cnt);
+
 #endif
kernel/res_counter.c +15 -0
index ad581aa..e84149b 100644
@@ -171,6 +171,21 @@ u64 res_counter_read_u64(struct res_counter *counter, int member)
 }
 #endif
 
+/*
+ * Returns usage. If usage > limit, limit is returned.
+ * This is useful not to break user experiance if the excess
+ * is temporal.
+ */
+u64 res_counter_usage_safe(struct res_counter *counter)
+{
+	u64 usage, limit;
+
+	limit = res_counter_read_u64(counter, RES_LIMIT);
+	usage = res_counter_read_u64(counter, RES_USAGE);
+
+	return min(usage, limit);
+}
+
 int res_counter_memparse_write_strategy(const char *buf,
 					unsigned long long *res)
 {
net/ipv4/tcp_memcontrol.c +1 -1
index b6f3583..a73dce6 100644
@@ -180,7 +180,7 @@ static u64 tcp_read_usage(struct mem_cgroup *memcg)
 		return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT;
 
 	tcp = tcp_from_cgproto(cg_proto);
-	return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_USAGE);
+	return res_counter_usage_safe(&tcp->tcp_memory_allocated);
 }
 
 static u64 tcp_cgroup_read(struct cgroup *cont, struct cftype *cft)
Signature · 1 line
1.7.4.1
Signature · 12 lines
Michal Hocko
SUSE Labs
SUSE LINUX s.r.o.
Lihovarska 1060/12
190 00 Praha 9    
Czech Republic

--
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>
#8 lore raw
In reply to Kamezawa Hiroyuki
Quoted 36 lines · Kamezawa Hiroyuki

This series is a cleaned up patches discussed in a few days ago, the topic was how to make compaction works well even if there is a memcg under OOM. ==
memcg: add res_counter_usage_safe()

I think usage > limit means a sign of BUG. But, sometimes, res_counter_charge_nofail() is very convenient. tcp_memcg uses it. And I'd like to use it for helping page migration.

This patch adds res_counter_usage_safe() which returns min(usage,limit). By this we can use res_counter_charge_nofail() without breaking user experience.

---

include/linux/res_counter.h |    2 ++
kernel/res_counter.c        |   15 +++++++++++++++
net/ipv4/tcp_memcontrol.c   |    2 +-
3 files changed, 18 insertions(+), 1 deletions(-)
include/linux/res_counter.h +2 -0
index 7d7fbe2..a6f8cc5 100644
@@ -226,4 +226,6 @@ res_counter_set_soft_limit(struct res_counter *cnt,
 	return 0;
 }
 
+u64 res_counter_usage_safe(struct res_counter *cnt);
+
 #endif
kernel/res_counter.c +0 -0
index ad581aa..e84149b 100644

kernel/res_counter.c u64 res_counter_read_u64(struct res_counter *counter, int member) · line 176

Quoted 6 lines
kernel/res_counter.c +3 -0
@@ -171,6 +171,21 @@ u64 res_counter_read_u64(struct res_counter *counter, int member)
 }
 #endif
 
+/*
+ * Returns usage. If usage > limit, limit is returned.
+ * This is useful not to break user experiance if the excess
                                      ^^^^^^^^

/experiance/experience

Quoted 37 lines
kernel/res_counter.c +12 -0
+ * is temporal.
+ */
+u64 res_counter_usage_safe(struct res_counter *counter)
+{
+	u64 usage, limit;
+
+	limit = res_counter_read_u64(counter, RES_LIMIT);
+	usage = res_counter_read_u64(counter, RES_USAGE);
+
+	return min(usage, limit);
+}
+
 int res_counter_memparse_write_strategy(const char *buf,
 					unsigned long long *res)
 {
net/ipv4/tcp_memcontrol.c +1 -1
index b6f3583..a73dce6 100644
@@ -180,7 +180,7 @@ static u64 tcp_read_usage(struct mem_cgroup *memcg)
 		return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT;
 
 	tcp = tcp_from_cgproto(cg_proto);
-	return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_USAGE);
+	return res_counter_usage_safe(&tcp->tcp_memory_allocated);
 }
 
 static u64 tcp_cgroup_read(struct cgroup *cont, struct cftype *cft)
Signature · 8 lines
1.7.4.1


--
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>

--
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>