Skip to contentSkip to conversation

memcg: robust enforcement of memory.high

PATCH Superseded Shakeel Butt 2 participants 15 messages Feb 10, 2022 – Feb 11, 2022

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover memcg: robust enforcement of memory.high

Due to the semantics of memory.high enforcement i.e. throttle the workload without oom-kill, we are trying to use it for right sizing the workloads in our production environment. However we observed the mechanism fails for some specific applications which does bug chunck of allocations in a single syscall. The reason behind this failure is due to the limitation of the memory.high enforcement's current implementation. This patch series solves this issue by enforcing the memory.high synchronously and making it more robust.

Shakeel Butt (4):
memcg: refactor mem_cgroup_oom
memcg: unify force charging conditions
selftests: memcg: test high limit for single entry allocation
memcg: synchronously enforce memory.high

 include/linux/page_counter.h                  |  10 +
 mm/memcontrol.c                               | 175 ++++++++++--------
 mm/page_counter.c                             |  59 ++++--
 tools/testing/selftests/cgroup/cgroup_util.c  |  15 +-
 tools/testing/selftests/cgroup/cgroup_util.h  |   1 +
 .../selftests/cgroup/test_memcontrol.c        |  78 ++++++++
 6 files changed, 240 insertions(+), 98 deletions(-)
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
#1 lore raw

PATCH 1/4 memcg: refactor mem_cgroup_oom

The function mem_cgroup_oom returns enum which has four possible values but the caller does not care about such values and only care if the return value is OOM_SUCCESS or not. So, remove the enum altogether and make mem_cgroup_oom returns a simple bool.

---
 mm/memcontrol.c | 40 +++++++++++++---------------------------
 1 file changed, 13 insertions(+), 27 deletions(-)
mm/memcontrol.c +13 -27
index a0e9d9f12cf5..c40c27822802 100644
@@ -1795,20 +1795,12 @@ static void memcg_oom_recover(struct mem_cgroup *memcg)
 		__wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
 }
 
-enum oom_status {
-	OOM_SUCCESS,
-	OOM_FAILED,
-	OOM_ASYNC,
-	OOM_SKIPPED
-};
-
-static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
+static bool mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
 {
-	enum oom_status ret;
-	bool locked;
+	bool locked, ret = false;
 
 	if (order > PAGE_ALLOC_COSTLY_ORDER)
-		return OOM_SKIPPED;
+		return ret;
 
 	memcg_memory_event(memcg, MEMCG_OOM);
 
@@ -1831,14 +1823,13 @@ static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int
 	 * victim and then we have to bail out from the charge path.
 	 */
 	if (memcg->oom_kill_disable) {
-		if (!current->in_user_fault)
-			return OOM_SKIPPED;
-		css_get(&memcg->css);
-		current->memcg_in_oom = memcg;
-		current->memcg_oom_gfp_mask = mask;
-		current->memcg_oom_order = order;
-
-		return OOM_ASYNC;
+		if (current->in_user_fault) {
+			css_get(&memcg->css);
+			current->memcg_in_oom = memcg;
+			current->memcg_oom_gfp_mask = mask;
+			current->memcg_oom_order = order;
+		}
+		return ret;
 	}
 
 	mem_cgroup_mark_under_oom(memcg);
@@ -1849,10 +1840,7 @@ static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int
 		mem_cgroup_oom_notify(memcg);
 
 	mem_cgroup_unmark_under_oom(memcg);
-	if (mem_cgroup_out_of_memory(memcg, mask, order))
-		ret = OOM_SUCCESS;
-	else
-		ret = OOM_FAILED;
+	ret = mem_cgroup_out_of_memory(memcg, mask, order);
 
 	if (locked)
 		mem_cgroup_oom_unlock(memcg);
@@ -2545,7 +2533,6 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	int nr_retries = MAX_RECLAIM_RETRIES;
 	struct mem_cgroup *mem_over_limit;
 	struct page_counter *counter;
-	enum oom_status oom_status;
 	unsigned long nr_reclaimed;
 	bool passed_oom = false;
 	bool may_swap = true;
@@ -2648,9 +2635,8 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	 * a forward progress or bypass the charge if the oom killer
 	 * couldn't make any progress.
 	 */
-	oom_status = mem_cgroup_oom(mem_over_limit, gfp_mask,
-		       get_order(nr_pages * PAGE_SIZE));
-	if (oom_status == OOM_SUCCESS) {
+	if (mem_cgroup_oom(mem_over_limit, gfp_mask,
+			   get_order(nr_pages * PAGE_SIZE))) {
 		passed_oom = true;
 		nr_retries = MAX_RECLAIM_RETRIES;
 		goto retry;
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] memcg: refactor mem_cgroup_oom

Quoted 6 lines · Shakeel Butt

The function mem_cgroup_oom returns enum which has four possible values but the caller does not care about such values and only care if the return value is OOM_SUCCESS or not. So, remove the enum altogether and make mem_cgroup_oom returns a simple bool.

Nice!

Quoted 9 lines
---
 mm/memcontrol.c | 40 +++++++++++++---------------------------
 1 file changed, 13 insertions(+), 27 deletions(-)
mm/memcontrol.c +0 -0
index a0e9d9f12cf5..c40c27822802 100644

mm/memcontrol.c static void memcg_oom_recover(struct mem_cgroup *memcg) · line 1804

Quoted 10 lines
mm/memcontrol.c +0 -7
@@ -1795,20 +1795,12 @@ static void memcg_oom_recover(struct mem_cgroup *memcg)
 		__wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
 }
 
-enum oom_status {
-	OOM_SUCCESS,
-	OOM_FAILED,
-	OOM_ASYNC,
-	OOM_SKIPPED
-};
-

The only thing, I'd add a small comment on the return value here. E.g. "returns true if one or more tasks have been successfully killed" or something like this.

Thanks!

Quoted 70 lines
mm/memcontrol.c +13 -20
-static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
+static bool mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
 {
-	enum oom_status ret;
-	bool locked;
+	bool locked, ret = false;
 
 	if (order > PAGE_ALLOC_COSTLY_ORDER)
-		return OOM_SKIPPED;
+		return ret;
 
 	memcg_memory_event(memcg, MEMCG_OOM);
 
@@ -1831,14 +1823,13 @@ static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int
 	 * victim and then we have to bail out from the charge path.
 	 */
 	if (memcg->oom_kill_disable) {
-		if (!current->in_user_fault)
-			return OOM_SKIPPED;
-		css_get(&memcg->css);
-		current->memcg_in_oom = memcg;
-		current->memcg_oom_gfp_mask = mask;
-		current->memcg_oom_order = order;
-
-		return OOM_ASYNC;
+		if (current->in_user_fault) {
+			css_get(&memcg->css);
+			current->memcg_in_oom = memcg;
+			current->memcg_oom_gfp_mask = mask;
+			current->memcg_oom_order = order;
+		}
+		return ret;
 	}
 
 	mem_cgroup_mark_under_oom(memcg);
@@ -1849,10 +1840,7 @@ static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int
 		mem_cgroup_oom_notify(memcg);
 
 	mem_cgroup_unmark_under_oom(memcg);
-	if (mem_cgroup_out_of_memory(memcg, mask, order))
-		ret = OOM_SUCCESS;
-	else
-		ret = OOM_FAILED;
+	ret = mem_cgroup_out_of_memory(memcg, mask, order);
 
 	if (locked)
 		mem_cgroup_oom_unlock(memcg);
@@ -2545,7 +2533,6 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	int nr_retries = MAX_RECLAIM_RETRIES;
 	struct mem_cgroup *mem_over_limit;
 	struct page_counter *counter;
-	enum oom_status oom_status;
 	unsigned long nr_reclaimed;
 	bool passed_oom = false;
 	bool may_swap = true;
@@ -2648,9 +2635,8 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	 * a forward progress or bypass the charge if the oom killer
 	 * couldn't make any progress.
 	 */
-	oom_status = mem_cgroup_oom(mem_over_limit, gfp_mask,
-		       get_order(nr_pages * PAGE_SIZE));
-	if (oom_status == OOM_SUCCESS) {
+	if (mem_cgroup_oom(mem_over_limit, gfp_mask,
+			   get_order(nr_pages * PAGE_SIZE))) {
 		passed_oom = true;
 		nr_retries = MAX_RECLAIM_RETRIES;
 		goto retry;
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
#3 lore raw

Re: [PATCH 1/4] memcg: refactor mem_cgroup_oom

Quoted 7 lines · Roman Gushchin
Quoted 5 lines · Shakeel Butt

The function mem_cgroup_oom returns enum which has four possible values but the caller does not care about such values and only care if the return value is OOM_SUCCESS or not. So, remove the enum altogether and make mem_cgroup_oom returns a simple bool.

Quoted 5 lines
Quoted 1 line

Nice!

Thanks.

Quoted 1 line

[...]

Quoted 5 lines

The only thing, I'd add a small comment on the return value here. E.g. "returns true if one or more tasks have been successfully killed" or something like this.

Will do in the next version.

#4 lore raw

PATCH 2/4 memcg: unify force charging conditions

In reply to Shakeel Butt

Currently the kernel force charges the allocations which have __GFP_HIGH flag without triggering the memory reclaim. __GFP_HIGH indicates that the caller is high priority and since commit 869712fd3de5 ("mm:
memcontrol: fix network errors from failing __GFP_ATOMIC charges") the kernel let such allocations do force charging. Please note that __GFP_ATOMIC has been replaced by __GFP_HIGH.

__GFP_HIGH does not tell if the caller can block or can trigger reclaim. There are separate checks to determine that. So, there is no need to skip reclaim for __GFP_HIGH allocations. So, handle __GFP_HIGH together with __GFP_NOFAIL which also does force charging.

Please note that this is a noop change as there are no __GFP_HIGH allocators in kernel which also have __GFP_ACCOUNT (or SLAB_ACCOUNT) and does not allow reclaim for now. The reason for this patch is to simplify the reasoning of the following patches.

---
 mm/memcontrol.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
mm/memcontrol.c +7 -10
index c40c27822802..ae73a40818b0 100644
@@ -2560,15 +2560,6 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		goto retry;
 	}
 
-	/*
-	 * Memcg doesn't have a dedicated reserve for atomic
-	 * allocations. But like the global atomic pool, we need to
-	 * put the burden of reclaim on regular allocation requests
-	 * and let these go through as privileged allocations.
-	 */
-	if (gfp_mask & __GFP_HIGH)
-		goto force;
-
 	/*
 	 * Prevent unbounded recursion when reclaim operations need to
 	 * allocate memory. This might exceed the limits temporarily,
@@ -2642,7 +2633,13 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		goto retry;
 	}
 nomem:
-	if (!(gfp_mask & __GFP_NOFAIL))
+	/*
+	 * Memcg doesn't have a dedicated reserve for atomic
+	 * allocations. But like the global atomic pool, we need to
+	 * put the burden of reclaim on regular allocation requests
+	 * and let these go through as privileged allocations.
+	 */
+	if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH)))
 		return -ENOMEM;
 force:
 	/*
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
#5 lore raw

Re: [PATCH 2/4] memcg: unify force charging conditions

Quoted 11 lines · Shakeel Butt

Currently the kernel force charges the allocations which have __GFP_HIGH flag without triggering the memory reclaim. __GFP_HIGH indicates that the caller is high priority and since commit 869712fd3de5 ("mm:
memcontrol: fix network errors from failing __GFP_ATOMIC charges") the kernel let such allocations do force charging. Please note that __GFP_ATOMIC has been replaced by __GFP_HIGH.

__GFP_HIGH does not tell if the caller can block or can trigger reclaim. There are separate checks to determine that. So, there is no need to skip reclaim for __GFP_HIGH allocations. So, handle __GFP_HIGH together with __GFP_NOFAIL which also does force charging.

This sounds very reasonable. But shouldn't we check if __GFP_DIRECT_RECLAIM is set and bail out otherwise?

Thanks!

Quoted 49 lines

Please note that this is a noop change as there are no __GFP_HIGH allocators in kernel which also have __GFP_ACCOUNT (or SLAB_ACCOUNT) and does not allow reclaim for now. The reason for this patch is to simplify the reasoning of the following patches.

---
 mm/memcontrol.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
mm/memcontrol.c +7 -10
index c40c27822802..ae73a40818b0 100644
@@ -2560,15 +2560,6 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		goto retry;
 	}
 
-	/*
-	 * Memcg doesn't have a dedicated reserve for atomic
-	 * allocations. But like the global atomic pool, we need to
-	 * put the burden of reclaim on regular allocation requests
-	 * and let these go through as privileged allocations.
-	 */
-	if (gfp_mask & __GFP_HIGH)
-		goto force;
-
 	/*
 	 * Prevent unbounded recursion when reclaim operations need to
 	 * allocate memory. This might exceed the limits temporarily,
@@ -2642,7 +2633,13 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		goto retry;
 	}
 nomem:
-	if (!(gfp_mask & __GFP_NOFAIL))
+	/*
+	 * Memcg doesn't have a dedicated reserve for atomic
+	 * allocations. But like the global atomic pool, we need to
+	 * put the burden of reclaim on regular allocation requests
+	 * and let these go through as privileged allocations.
+	 */
+	if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH)))
 		return -ENOMEM;
 force:
 	/*
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
#6 lore raw

Re: [PATCH 2/4] memcg: unify force charging conditions

Quoted 9 lines · Roman Gushchin
Quoted 7 lines · Shakeel Butt

Currently the kernel force charges the allocations which have __GFP_HIGH flag without triggering the memory reclaim. __GFP_HIGH indicates that the caller is high priority and since commit 869712fd3de5 ("mm:
memcontrol: fix network errors from failing __GFP_ATOMIC charges") the kernel let such allocations do force charging. Please note that __GFP_ATOMIC has been replaced by __GFP_HIGH.

Quoted 8 lines
Quoted 4 lines

__GFP_HIGH does not tell if the caller can block or can trigger reclaim. There are separate checks to determine that. So, there is no need to skip reclaim for __GFP_HIGH allocations. So, handle __GFP_HIGH together with __GFP_NOFAIL which also does force charging.

This sounds very reasonable. But shouldn't we check if __GFP_DIRECT_RECLAIM is set and bail out otherwise?

We already have a gfpflags_allow_blocking() check which checks for __GFP_DIRECT_RECLAIM.

Reviewed-by
#7 lore raw

Re: [PATCH 2/4] memcg: unify force charging conditions

Quoted 15 lines · Shakeel Butt
Quoted 14 lines · Roman Gushchin
Quoted 11 lines · Shakeel Butt

Currently the kernel force charges the allocations which have __GFP_HIGH flag without triggering the memory reclaim. __GFP_HIGH indicates that the caller is high priority and since commit 869712fd3de5 ("mm:
memcontrol: fix network errors from failing __GFP_ATOMIC charges") the kernel let such allocations do force charging. Please note that __GFP_ATOMIC has been replaced by __GFP_HIGH.

__GFP_HIGH does not tell if the caller can block or can trigger reclaim. There are separate checks to determine that. So, there is no need to skip reclaim for __GFP_HIGH allocations. So, handle __GFP_HIGH together with __GFP_NOFAIL which also does force charging.

Quoted 6 lines
Quoted 3 lines

This sounds very reasonable. But shouldn't we check if __GFP_DIRECT_RECLAIM is set and bail out otherwise?

We already have a gfpflags_allow_blocking() check which checks for __GFP_DIRECT_RECLAIM.

Indeed. Please, feel free to add

Thank you!

#8 lore raw

PATCH 3/4 selftests: memcg: test high limit for single entry allocation

In reply to Shakeel Butt

Test the enforcement of memory.high limit for large amount of memory allocation within a single kernel entry. There are valid use-cases where the application can trigger large amount of memory allocation within a single syscall e.g. mlock() or mmap(MAP_POPULATE). Make sure memory.high limit enforcement works for such use-cases.

---
 tools/testing/selftests/cgroup/cgroup_util.c  | 15 +++-
 tools/testing/selftests/cgroup/cgroup_util.h  |  1 +
 .../selftests/cgroup/test_memcontrol.c        | 78 +++++++++++++++++++
 3 files changed, 91 insertions(+), 3 deletions(-)
tools/testing/selftests/cgroup/cgroup_util.c +12 -3
index 0cf7e90c0052..dbaa7aabbb4a 100644
@@ -583,7 +583,7 @@ int clone_into_cgroup_run_wait(const char *cgroup)
 	return 0;
 }
 
-int cg_prepare_for_wait(const char *cgroup)
+static int __prepare_for_wait(const char *cgroup, const char *filename)
 {
 	int fd, ret = -1;
 
@@ -591,8 +591,7 @@ int cg_prepare_for_wait(const char *cgroup)
 	if (fd == -1)
 		return fd;
 
-	ret = inotify_add_watch(fd, cg_control(cgroup, "cgroup.events"),
-				IN_MODIFY);
+	ret = inotify_add_watch(fd, cg_control(cgroup, filename), IN_MODIFY);
 	if (ret == -1) {
 		close(fd);
 		fd = -1;
@@ -601,6 +600,16 @@ int cg_prepare_for_wait(const char *cgroup)
 	return fd;
 }
 
+int cg_prepare_for_wait(const char *cgroup)
+{
+	return __prepare_for_wait(cgroup, "cgroup.events");
+}
+
+int memcg_prepare_for_wait(const char *cgroup)
+{
+	return __prepare_for_wait(cgroup, "memory.events");
+}
+
 int cg_wait_for(int fd)
 {
 	int ret = -1;
tools/testing/selftests/cgroup/cgroup_util.h +1 -0
index 4f66d10626d2..628738532ac9 100644
@@ -55,4 +55,5 @@ extern int clone_reap(pid_t pid, int options);
 extern int clone_into_cgroup_run_wait(const char *cgroup);
 extern int dirfd_open_opath(const char *dir);
 extern int cg_prepare_for_wait(const char *cgroup);
+extern int memcg_prepare_for_wait(const char *cgroup);
 extern int cg_wait_for(int fd);
tools/testing/selftests/cgroup/test_memcontrol.c +78 -0

96 lines not on this page · raw message

Signature · 1 line
2.35.1.265.g69c8d7142f-goog
#9 lore raw

PATCH 4/4 memcg: synchronously enforce memory.high

In reply to Shakeel Butt

The high limit is used to throttle the workload without invoking the oom-killer. Recently we tried to use the high limit to right size our internal workloads. More specifically dynamically adjusting the limits of the workload without letting the workload get oom-killed. However due to the limitation of the implementation of high limit enforcement, we observed the mechanism fails for some real workloads.

The high limit is enforced on return-to-userspace i.e. the kernel let the usage goes over the limit and when the execution returns to userspace, the high reclaim is triggered and the process can get throttled as well. However this mechanism fails for workloads which do large allocations in a single kernel entry e.g. applications that mlock() a large chunk of memory in a single syscall. Such applications bypass the high limit and can trigger the oom-killer.

To make high limit enforcement more robust, this patch make the limit enforcement synchronous. However there are couple of open questions to enforce high limit synchronously. What should be the behavior of __GFP_NORETRY allocaion on hitting high limit? Similar question arise for allocations which do not allow blocking. This patch took the approach to keep the previous behavior i.e. let such allocations not throttle synchronously but rely on the return-to-userspace mechanism to throttle processes triggering such allocations.

This patch does not remove the return-to-userspace high limit enforcement due to the reason mentioned in the previous para. Also the allocations where the memory usage is below high limit but the swap usage is above swap's high limit, such allocators are throttled in the return-to-userspace.

---
 include/linux/page_counter.h |  10 +++
 mm/memcontrol.c              | 124 ++++++++++++++++++++++-------------
 mm/page_counter.c            |  59 +++++++++++++----
 3 files changed, 132 insertions(+), 61 deletions(-)
include/linux/page_counter.h +10 -0
index 679591301994..08413a5c73f9 100644
@@ -60,6 +60,16 @@ void page_counter_charge(struct page_counter *counter, unsigned long nr_pages);
 bool page_counter_try_charge(struct page_counter *counter,
 			     unsigned long nr_pages,
 			     struct page_counter **fail);
+
+enum charge_status {
+	CHG_SUCCESS,
+	CHG_FAILED_HIGH,
+	CHG_FAILED_MAX
+};
+enum charge_status page_counter_try_charge_high(struct page_counter *counter,
+						unsigned long nr_pages,
+						struct page_counter **fail);
+
 void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages);
 void page_counter_set_min(struct page_counter *counter, unsigned long nr_pages);
 void page_counter_set_low(struct page_counter *counter, unsigned long nr_pages);
mm/memcontrol.c +77 -47

208 lines not on this page · raw message

mm/page_counter.c +45 -14

83 lines not on this page · raw message

Signature · 1 line
2.35.1.265.g69c8d7142f-goog
#10 lore raw

Re: [PATCH 4/4] memcg: synchronously enforce memory.high

Quoted 24 lines · Shakeel Butt

The high limit is used to throttle the workload without invoking the oom-killer. Recently we tried to use the high limit to right size our internal workloads. More specifically dynamically adjusting the limits of the workload without letting the workload get oom-killed. However due to the limitation of the implementation of high limit enforcement, we observed the mechanism fails for some real workloads.

The high limit is enforced on return-to-userspace i.e. the kernel let the usage goes over the limit and when the execution returns to userspace, the high reclaim is triggered and the process can get throttled as well. However this mechanism fails for workloads which do large allocations in a single kernel entry e.g. applications that mlock() a large chunk of memory in a single syscall. Such applications bypass the high limit and can trigger the oom-killer.

To make high limit enforcement more robust, this patch make the limit enforcement synchronous. However there are couple of open questions to enforce high limit synchronously. What should be the behavior of __GFP_NORETRY allocaion on hitting high limit? Similar question arise for allocations which do not allow blocking. This patch took the approach to keep the previous behavior i.e. let such allocations not throttle synchronously but rely on the return-to-userspace mechanism to throttle processes triggering such allocations.

Quoted 5 lines

This patch does not remove the return-to-userspace high limit enforcement due to the reason mentioned in the previous para. Also the allocations where the memory usage is below high limit but the swap usage is above swap's high limit, such allocators are throttled in the return-to-userspace.

Has this approach been extensively tested in the production?

Injecting sleeps at return-to-userspace moment is safe in terms of priority
inversions: a slowed down task will unlikely affect the rest of the system.

It way less predictable for a random allocation in the kernel mode, what if the task is already holding a system-wide resource?

Someone might argue that it's not better than a system-wide memory shortage and the same allocation might go into a direct reclaim anyway, but with the way how memory.high is used it will happen way more often.

Thanks!

#11 lore raw

Re: [PATCH 4/4] memcg: synchronously enforce memory.high

Quoted 1 line · Roman Gushchin

[...]

Quoted 6 lines

Has this approach been extensively tested in the production?

Injecting sleeps at return-to-userspace moment is safe in terms of priority
inversions: a slowed down task will unlikely affect the rest of the system.

Quoted 7 lines

It way less predictable for a random allocation in the kernel mode, what if the task is already holding a system-wide resource?

Someone might argue that it's not better than a system-wide memory shortage and the same allocation might go into a direct reclaim anyway, but with the way how memory.high is used it will happen way more often.

Thanks for the review.

This patchset is tested in the test environment for now and I do plan to test this in production but that is a slow process and will take some time.

Let me answer the main concern you have raised i.e. the safety of throttling a task synchronously in the charge code path. Please note that synchronous memory reclaim and oom-killing can already cause the priority inversion issues you have mentioned. The way we usually tackle such issues are through userspace controllers. For example oomd is the userspace solution for catering such issues related to oom-killing. Here we have a similar userspace daemon monitoring the workload and deciding if it should let the workload grow or kill it.

Now should we keep the current high limit enforcement implementation and let it be ineffective for some real workloads or should we make the enforcement more robust and let the userspace tackle some corner case priority inversion issues. I think we should follow the second option as we already have precedence of doing the same for reclaim and oom-killing.

thanks,
Shakeel

#12 lore raw

Re: [PATCH 4/4] memcg: synchronously enforce memory.high

Quoted 32 lines · Shakeel Butt
Quoted 1 line · Roman Gushchin

[...]

Quoted 6 lines

Has this approach been extensively tested in the production?

Injecting sleeps at return-to-userspace moment is safe in terms of priority
inversions: a slowed down task will unlikely affect the rest of the system.

Quoted 7 lines

It way less predictable for a random allocation in the kernel mode, what if the task is already holding a system-wide resource?

Someone might argue that it's not better than a system-wide memory shortage and the same allocation might go into a direct reclaim anyway, but with the way how memory.high is used it will happen way more often.

Thanks for the review.

This patchset is tested in the test environment for now and I do plan to test this in production but that is a slow process and will take some time.

Let me answer the main concern you have raised i.e. the safety of throttling a task synchronously in the charge code path. Please note that synchronous memory reclaim and oom-killing can already cause the priority inversion issues you have mentioned. The way we usually tackle such issues are through userspace controllers. For example oomd is the userspace solution for catering such issues related to oom-killing. Here we have a similar userspace daemon monitoring the workload and deciding if it should let the workload grow or kill it.

Quoted 6 lines

Now should we keep the current high limit enforcement implementation and let it be ineffective for some real workloads or should we make the enforcement more robust and let the userspace tackle some corner case priority inversion issues. I think we should follow the second option as we already have precedence of doing the same for reclaim and oom-killing.

Well, in a theory it sounds good and I have no intention to oppose the idea. However in practice we might easily get quite serious problems. So I think we should be extra careful here. In the end we don't want to pull and then revert this patch.

The difference between the system-wide direct reclaim and this case is that usually kswapd is doing a good job of refilling the empty buffer, so we don't usually work in the circumstances of the global memory shortage. And when we do, often it's not working out quite well, this is why oomd and other similar solutions are required.

Another option is to use your approach only for special cases (e.g. huge allocations) and keep the existing approach for most other allocations.

Thanks!

#13 lore raw

Re: [PATCH 4/4] memcg: synchronously enforce memory.high

Quoted 46 lines · Roman Gushchin
Quoted 32 lines · Shakeel Butt
Quoted 1 line · Roman Gushchin

[...]

Quoted 6 lines

Has this approach been extensively tested in the production?

Injecting sleeps at return-to-userspace moment is safe in terms of priority
inversions: a slowed down task will unlikely affect the rest of the system.

Quoted 7 lines

It way less predictable for a random allocation in the kernel mode, what if the task is already holding a system-wide resource?

Someone might argue that it's not better than a system-wide memory shortage and the same allocation might go into a direct reclaim anyway, but with the way how memory.high is used it will happen way more often.

Thanks for the review.

This patchset is tested in the test environment for now and I do plan to test this in production but that is a slow process and will take some time.

Let me answer the main concern you have raised i.e. the safety of throttling a task synchronously in the charge code path. Please note that synchronous memory reclaim and oom-killing can already cause the priority inversion issues you have mentioned. The way we usually tackle such issues are through userspace controllers. For example oomd is the userspace solution for catering such issues related to oom-killing. Here we have a similar userspace daemon monitoring the workload and deciding if it should let the workload grow or kill it.

Quoted 6 lines

Now should we keep the current high limit enforcement implementation and let it be ineffective for some real workloads or should we make the enforcement more robust and let the userspace tackle some corner case priority inversion issues. I think we should follow the second option as we already have precedence of doing the same for reclaim and oom-killing.

Well, in a theory it sounds good and I have no intention to oppose the idea. However in practice we might easily get quite serious problems. So I think we should be extra careful here. In the end we don't want to pull and then revert this patch.

Quoted 9 lines

The difference between the system-wide direct reclaim and this case is that usually kswapd is doing a good job of refilling the empty buffer, so we don't usually work in the circumstances of the global memory shortage. And when we do, often it's not working out quite well, this is why oomd and other similar solutions are required.
.
Another option is to use your approach only for special cases (e.g. huge allocations) and keep the existing approach for most other allocations.

These are not necessarily huge allocations and can be a large number of small allocations. However I think we can make this idea work by checking current->memcg_nr_pages_over_high. If order(current->memcg_nr_pages_over_high) is, let's say, larger than PAGE_ALLOC_COSTLY_ORDER, then throttle synchronously. WDYT?

#14 lore raw

Re: [PATCH 4/4] memcg: synchronously enforce memory.high

Quoted 57 lines · Shakeel Butt
Quoted 46 lines · Roman Gushchin
Quoted 32 lines · Shakeel Butt
Quoted 1 line · Roman Gushchin

[...]

Quoted 6 lines

Has this approach been extensively tested in the production?

Injecting sleeps at return-to-userspace moment is safe in terms of priority
inversions: a slowed down task will unlikely affect the rest of the system.

Quoted 7 lines

It way less predictable for a random allocation in the kernel mode, what if the task is already holding a system-wide resource?

Someone might argue that it's not better than a system-wide memory shortage and the same allocation might go into a direct reclaim anyway, but with the way how memory.high is used it will happen way more often.

Thanks for the review.

This patchset is tested in the test environment for now and I do plan to test this in production but that is a slow process and will take some time.

Let me answer the main concern you have raised i.e. the safety of throttling a task synchronously in the charge code path. Please note that synchronous memory reclaim and oom-killing can already cause the priority inversion issues you have mentioned. The way we usually tackle such issues are through userspace controllers. For example oomd is the userspace solution for catering such issues related to oom-killing. Here we have a similar userspace daemon monitoring the workload and deciding if it should let the workload grow or kill it.

Quoted 6 lines

Now should we keep the current high limit enforcement implementation and let it be ineffective for some real workloads or should we make the enforcement more robust and let the userspace tackle some corner case priority inversion issues. I think we should follow the second option as we already have precedence of doing the same for reclaim and oom-killing.

Well, in a theory it sounds good and I have no intention to oppose the idea. However in practice we might easily get quite serious problems. So I think we should be extra careful here. In the end we don't want to pull and then revert this patch.

Quoted 9 lines

The difference between the system-wide direct reclaim and this case is that usually kswapd is doing a good job of refilling the empty buffer, so we don't usually work in the circumstances of the global memory shortage. And when we do, often it's not working out quite well, this is why oomd and other similar solutions are required.
.
Another option is to use your approach only for special cases (e.g. huge allocations) and keep the existing approach for most other allocations.

Quoted 5 lines

These are not necessarily huge allocations and can be a large number of small allocations. However I think we can make this idea work by checking current->memcg_nr_pages_over_high. If order(current->memcg_nr_pages_over_high) is, let's say, larger than PAGE_ALLOC_COSTLY_ORDER, then throttle synchronously. WDYT?

Yes, I really like this idea: the majority of allocations will be handled in a proven way, however we'll have coverage for large outliers as well.

Not sure about PAGE_ALLOC_COSTLY_ORDER though, I'd probably chose a larger constant, but we can discuss it separately.

Thanks!