Skip to contentSkip to conversation

memcg: Enable fine-grained per process memory control

RFC PATCH In review Waiman Long 9 participants 44 messages Aug 17, 2020 – Sep 9, 2020

Subsystems (10)
lore
Share & tools
lore
#0 lore raw

cover memcg: Enable fine-grained per process memory control

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing. When it reaches "memory.max" for memory cgroup v2 or when the system is running out of free memory, the OOM killer will be invoked to kill some tasks to free up additional memory. However, one has little control of which tasks are going to be killed by an OOM killer. Killing tasks that hold some important resources without freeing them first can create other system problems down the road.

Users who do not want the OOM killer to be invoked to kill random tasks in an out-of-memory situation can use the memory control facility provided by this new patchset via prctl(2) to better manage the mitigation action that needs to be performed to various tasks when the specified memory limit is exceeded with memory cgroup v2 being used.

The currently supported mitigation actions include the followings:

1) Return ENOMEM for some syscalls that allocate or handle memory
2) Slow down the process for memory reclaim to catch up
3) Send a specific signal to the task
4) Kill the task

The users that want better memory control for their applicatons can either modify their applications to call the prctl(2) syscall directly with the new memory control command code or write the desired action to the newly provided memctl procfs files of their applications provided that those applications run in a non-root v2 memory cgroup.

Waiman Long (8):
memcg: Enable fine-grained control of over memory.high action memcg, mm: Return ENOMEM or delay if memcg_over_limit
memcg: Allow the use of task RSS memory as over-high action trigger fs/proc: Support a new procfs memctl file
memcg: Allow direct per-task memory limit checking
memcg: Introduce additional memory control slowdown if needed
memcg: Enable logging of memory control mitigation action
memcg: Add over-high action prctl() documentation

 Documentation/userspace-api/index.rst      |   1 +
 Documentation/userspace-api/memcontrol.rst | 174 ++++++++++++++++
 fs/proc/base.c                             | 109 ++++++++++
 include/linux/memcontrol.h                 |   4 +
 include/linux/sched.h                      |  24 +++
 include/uapi/linux/prctl.h                 |  48 +++++
 kernel/fork.c                              |   1 +
 kernel/sys.c                               |  16 ++
 mm/memcontrol.c                            | 227 +++++++++++++++++++++
 mm/mlock.c                                 |   6 +
 mm/mmap.c                                  |  12 ++
 mm/mprotect.c                              |   3 +
 12 files changed, 625 insertions(+)
 create mode 100644 Documentation/userspace-api/memcontrol.rst
Signature · 1 line
2.18.1
#1 lore raw

RFC PATCH 1/8 memcg: Enable fine-grained control of over memory.high action

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate especially when rotating disks are used for swapping or writing dirty pages. In this case, the physical memory consumption will keep on increasing. When it reaches "memory.max" or the system is really running out of memory, the OOM killer will be invoked to kill some tasks to free up additional memory. However, one has little control of which tasks are going to be killed by an OOM killer.

Users who do not want the OOM killer to be invoked to kill random tasks in an out-of-memory situation will require a better way to manage memory and deal with applications that are out of control in term of physical memory consumption rate.

A new set of prctl(2) commands are added to provide a facility to allow users to manage the physical memory consumption of each of their applications and control the mitigation actions that should be taken when those applications consume more physical memory than what they are supposed to use.

The new prctl(2) commands are PR_SET_MEMCONTROL and PR_GET_MEMCONTROL to set the memory control parameters and retrieve those parameters respectively. The four possible mitigation actions for a task that exceeds their designated memory limit are:

1) Return ENOMEM for some syscalls that allocate or handle memory
2) Slow down the process for memory reclaim to catch up
3) Send a specific signal to the task
4) Kill the task

The parameters that can be specified in the new PR_SET_MEMCONTROL commands are:

arg2 - the mitigation action (bits 0-7), signal number (bits 8-15)

	and flags (bits 16-31).

arg3 - the additional memory limit (in bytes) that will be added to
memory.high as the real limit that will trigger the mitigation action.

The PR_MEMFLAG_SIGCONT flag is used to specify continuous signal delivery instead of a one-shot signal.

---
 include/linux/memcontrol.h |   4 ++
 include/linux/sched.h      |   7 +++
 include/uapi/linux/prctl.h |  37 ++++++++++++
 kernel/sys.c               |  16 ++++++
 mm/memcontrol.c            | 114 +++++++++++++++++++++++++++++++++++++
 5 files changed, 178 insertions(+)
include/linux/memcontrol.h +4 -0
index d0b036123c6a..40e6ceb8209b 100644
@@ -445,6 +445,10 @@ void mem_cgroup_uncharge_list(struct list_head *page_list);
 
 void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
 
+long mem_cgroup_over_high_get(struct task_struct *task, unsigned long item);
+long mem_cgroup_over_high_set(struct task_struct *task, unsigned long action,
+			      unsigned long limit);
+
 static struct mem_cgroup_per_node *
 mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
 {
include/linux/sched.h +7 -0
index 93ecd930efd3..c79d606d27ab 100644
@@ -1265,6 +1265,13 @@ struct task_struct {
 	/* Number of pages to reclaim on returning to userland: */
 	unsigned int			memcg_nr_pages_over_high;
 
+	/* Memory over-high action, flags, signal and limit */
+	unsigned char			memcg_over_high_action;
+	unsigned char			memcg_over_high_signal;
+	unsigned short			memcg_over_high_flags;
+	unsigned int			memcg_over_high_climit;
+	unsigned int			memcg_over_limit;
+
 	/* Used by memcontrol for targeted memcg charge: */
 	struct mem_cgroup		*active_memcg;
 #endif
include/uapi/linux/prctl.h +37 -0
index 07b4f8131e36..87970ae7b32c 100644
@@ -238,4 +238,41 @@ struct prctl_mm_map {
 #define PR_SET_IO_FLUSHER		57
 #define PR_GET_IO_FLUSHER		58
 
+/* Per task fine-grained memory cgroup control */
+#define PR_GET_MEMCONTROL		59
+#define PR_SET_MEMCONTROL		60
+
+/*
+ * PR_SET_MEMCONTROL:
+ * 2 parameters are passed:
+ *  - Action word
+ *  - Memory cgroup additional memory limit
+ *
+ * The action word consists of 3 bit fields:
+ *  - Bits  0-7 : over-memory-limit action code
+ *  - Bits  8-15: signal number
+ *  - Bits 16-32: action flags
+ */
+
+/* Control values for PR_SET_MEMCONTROL over limit action */
+# define PR_MEMACT_NONE			0
+# define PR_MEMACT_ENOMEM		1	/* Deny memory request */
+# define PR_MEMACT_SLOWDOWN		2	/* Slow down the process */
+# define PR_MEMACT_SIGNAL		3	/* Send signal */
+# define PR_MEMACT_KILL			4	/* Kill the process */
+# define PR_MEMACT_MAX			PR_MEMACT_KILL
+
+/* Flags for PR_SET_MEMCONTROL */
+# define PR_MEMFLAG_SIGCONT		(1UL << 0) /* Continuous signal delivery */
+# define PR_MEMFLAG_MASK		PR_MEMFLAG_SIGCONT
+
+/* Action word masks */
+# define PR_MEMACT_MASK			0xff
+# define PR_MEMACT_SIG_SHIFT		8
+# define PR_MEMACT_FLG_SHIFT		16
+
+/* Return specified value for PR_GET_MEMCONTROL */
+# define PR_MEMGET_ACTION		0
+# define PR_MEMGET_CLIMIT		1
+
 #endif /* _LINUX_PRCTL_H */
kernel/sys.c +16 -0
index ca11af9d815d..644b86235d7f 100644
@@ -64,6 +64,10 @@
 
 #include <linux/nospec.h>
 
+#ifdef CONFIG_MEMCG
+#include <linux/memcontrol.h>
+#endif
+
 #include <linux/kmsg_dump.h>
 /* Move somewhere else to avoid recompiling? */
 #include <generated/utsrelease.h>
@@ -2530,6 +2534,18 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 
 		error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER;
 		break;
+#ifdef CONFIG_MEMCG
+	case PR_GET_MEMCONTROL:
+		if (arg3 || arg4 || arg5)
+			return -EINVAL;
+		error = mem_cgroup_over_high_get(me, arg2);
+		break;
+	case PR_SET_MEMCONTROL:
+		if (arg4 || arg5)
+			return -EINVAL;
+		error = mem_cgroup_over_high_set(me, arg2, arg3);
+		break;
+#endif
 	default:
 		error = -EINVAL;
 		break;
mm/memcontrol.c +114 -0

144 lines not on this page · raw message

Signature · 1 line
2.18.1
#2 lore raw

Re: [RFC PATCH 1/8] memcg: Enable fine-grained control of over memory.high action

Astractly, I think this really overcomplicates the API a lot. If these are truly generally useful (and I think that remains to be demonstrated), they should be additions to the existing API, rather than a sidestep with prctl.

I also worry about some other more concrete things:

1. Doesn't this allow unprivileged applications to potentially bypass memory.high constraints set by a system administrator?
2. What's the purpose of PR_MEMACT_KILL, compared to memory.max?
3. Why add this entirely separate signal delivery path when we already have eventfd/poll/inotify support, which makes a lot more sense for modern applications?

#3 lore raw

Re: [RFC PATCH 1/8] memcg: Enable fine-grained control of over memory.high action

Quoted 1 line · Chris Down

Astractly, I think this really overcomplicates the API a lot. If these are truly generally useful (and I think that remains to be demonstrated), they should be additions to the existing API, rather than a sidestep with prctl.

This patchset is derived from customer requests. With existing API, I suppose you mean the memory cgroup API. Right? The reason to use prctl() is that there are users out there who want some kind of per-process control instead of for a whole group of processes unless the users try to create one cgroup per process which is not very efficient.

Quoted 4 lines

I also worry about some other more concrete things:

1. Doesn't this allow unprivileged applications to potentially bypass    memory.high constraints set by a system administrator?

The memory.high constraint is for triggering memory reclaim. The new mitigation actions introduced by this patchset will only be applied if memory reclaim alone fails to limit the physical memory consumption. The current memory cgroup memory reclaim code will not be affected by this patchset.

Quoted 1 line

2. What's the purpose of PR_MEMACT_KILL, compared to memory.max?

A user can use this to specify which processes are less important and can be sacrificed first instead of the other more important ones in case they are really in a OOM situation. IOW, users can specify the order where OOM kills can happen.

Quoted 1 line

3. Why add this entirely separate signal delivery path when we already have eventfd/poll/inotify support, which makes a lot more sense for modern    applications?

Good question, I will look further into this to see if it can be applicable in this case.

Cheers,
Longman

#4 lore raw

Re: [RFC PATCH 1/8] memcg: Enable fine-grained control of over memory.high action

Quoted 3 lines · Waiman Long
Quoted 1 line · Chris Down

Astractly, I think this really overcomplicates the API a lot. If these are truly generally useful (and I think that remains to be demonstrated), they should be additions to the existing API, rather than a sidestep with prctl.

This patchset is derived from customer requests. With existing API, I suppose you mean the memory cgroup API. Right? The reason to use prctl() is that there are users out there who want some kind of per-process control instead of for a whole group of processes unless the users try to create one cgroup per process which is not very efficient.

If using one cgroup per process is inefficient, then that's what needs to be fixed. Making the API extremely complex to reason about for every user isn't a good compromise when we're talking about an already niche use case.

Quoted 4 lines
Quoted 3 lines

I also worry about some other more concrete things:

1. Doesn't this allow unprivileged applications to potentially bypass    memory.high constraints set by a system administrator?

The memory.high constraint is for triggering memory reclaim. The new mitigation actions introduced by this patchset will only be applied if memory reclaim alone fails to limit the physical memory consumption. The current memory cgroup memory reclaim code will not be affected by this patchset.

memory.high isn't only for triggering memory reclaim, it's also about active throttling when the application fails to come under. Fundamentally it's supposed to indicate the point at which we expect the application to either cooperate or get forcibly descheduled -- take a look at where we call schedule_timeout_killable.

I really struggle to think about how all of those things should interact in this patchset.

Quoted 2 lines
Quoted 1 line

2. What's the purpose of PR_MEMACT_KILL, compared to memory.max?

A user can use this to specify which processes are less important and can be sacrificed first instead of the other more important ones in case they are really in a OOM situation. IOW, users can specify the order where OOM kills can happen.

You can already do that with something like oomd, which has way more flexibility than this. Why codify this in the kernel instead of in a userspace agent?

#5 lore raw

Re: [RFC PATCH 1/8] memcg: Enable fine-grained control of over memory.high action

In reply to Waiman Long
Quoted 8 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Quoted 4 lines

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate especially when rotating disks are used for swapping or writing dirty pages. In this case, the physical memory consumption will keep on increasing.

Isn't this the real underlying issue? Why not make the guarantees of memory.high more strict instead of adding more interfaces and complexity?

By the way, have you observed this issue on real workloads or some test cases? It would be good to get a repro with simple test cases.

#6 lore raw

Re: [RFC PATCH 1/8] memcg: Enable fine-grained control of over memory.high action

Quoted 8 lines · Shakeel Butt
Quoted 4 lines

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate especially when rotating disks are used for swapping or writing dirty pages. In this case, the physical memory consumption will keep on increasing.

Isn't this the real underlying issue? Why not make the guarantees of memory.high more strict instead of adding more interfaces and complexity?

Oh, thanks Shakeel for bringing this up. I missed this in the original changelog and I'm surprised that it's mentioned, since we do have protections against that.

Waiman, we already added artificial throttling if memory reclaim is not sufficiently achieved in 0e4b01df8659 ("mm, memcg: throttle allocators when failing reclaim over memory.high"), which has been present since v5.4. This should significantly inhibit physical memory consumption from increasing. What problems are you having with that? :-)

#7 lore raw

Re: [RFC PATCH 1/8] memcg: Enable fine-grained control of over memory.high action

Quoted 6 lines · Chris Down
Quoted 5 lines · Shakeel Butt
Quoted 4 lines

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate especially when rotating disks are used for swapping or writing dirty pages. In this case, the physical memory consumption will keep on increasing.

Quoted 8 lines
Quoted 3 lines

Isn't this the real underlying issue? Why not make the guarantees of memory.high more strict instead of adding more interfaces and complexity?

Oh, thanks Shakeel for bringing this up. I missed this in the original changelog and I'm surprised that it's mentioned, since we do have protections against that.

Waiman, we already added artificial throttling if memory reclaim is not sufficiently achieved in 0e4b01df8659 ("mm, memcg: throttle allocators when failing reclaim over memory.high"), which has been present since v5.4. This should significantly inhibit physical memory consumption from increasing. What problems are you having with that? :-)

Oh, I think I overlooked your patch. You are right. There are already throttling in place. So I need to re-examine my patch to see if it is still necessary or reduce the scope of the patch.

Thanks,
Longman

#8 lore raw

Re: [RFC PATCH 1/8] memcg: Enable fine-grained control of over memory.high action

In reply to Shakeel Butt
Quoted 12 lines · Shakeel Butt
Quoted 11 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate especially when rotating disks are used for swapping or writing dirty pages. In this case, the physical memory consumption will keep on increasing.

Quoted 7 lines

Isn't this the real underlying issue? Why not make the guarantees of memory.high more strict instead of adding more interfaces and complexity?

By the way, have you observed this issue on real workloads or some test cases? It would be good to get a repro with simple test cases.

As said before, this is from a customer request. I will need to re-examine the existing features to see if they can satisfy the customer need.

Cheers,
Longman

#9 lore raw

RFC PATCH 2/8 memcg, mm: Return ENOMEM or delay if memcg_over_limit

In reply to Waiman Long

The brk(), mmap(), mlock(), mlockall() and mprotect() syscalls are modified to check the memcg_over_limit flag and return ENOMEM when it is set and memory control action is PR_MEMACT_ENOMEM.

In case the action is PR_MEMACT_SLOWDOWN, an artificial delay of 20ms will be added to slow down the memory allocation syscalls.

---
 include/linux/sched.h | 16 ++++++++++++++++
 kernel/fork.c         |  1 +
 mm/memcontrol.c       | 25 +++++++++++++++++++++++--
 mm/mlock.c            |  6 ++++++
 mm/mmap.c             | 12 ++++++++++++
 mm/mprotect.c         |  3 +++
 6 files changed, 61 insertions(+), 2 deletions(-)
include/linux/sched.h +16 -0
index c79d606d27ab..9ec1bd072334 100644
@@ -1477,6 +1477,22 @@ static inline char task_state_to_char(struct task_struct *tsk)
 	return task_index_to_char(task_state_index(tsk));
 }
 
+#ifdef CONFIG_MEMCG
+extern bool mem_cgroup_check_over_limit(void);
+
+static inline bool mem_over_memcg_limit(void)
+{
+	if (READ_ONCE(current->memcg_over_limit))
+		return mem_cgroup_check_over_limit();
+	return false;
+}
+#else
+static inline bool mem_over_memcg_limit(void)
+{
+	return false;
+}
+#endif
+
 /**
  * is_global_init - check if a task structure is init. Since init
  * is free to have sub-threads we need to check tgid.
kernel/fork.c +1 -0
index 4d32190861bd..61f9a9e5f857 100644
@@ -940,6 +940,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
 
 #ifdef CONFIG_MEMCG
 	tsk->active_memcg = NULL;
+	tsk->memcg_over_limit = false;
 #endif
 	return tsk;
 
mm/memcontrol.c +23 -2
index 1106dac024ac..5cad7bb26d13 100644
@@ -2646,7 +2646,9 @@ static bool __mem_cgroup_over_high_action(struct mem_cgroup *memcg, u8 action)
 	if (!mm)
 		return true;	/* No more check is needed */
 
-	current->memcg_over_limit = false;
+	if (READ_ONCE(current->memcg_over_limit))
+		WRITE_ONCE(current->memcg_over_limit, false);
+
 	if ((action == PR_MEMACT_SIGNAL) && !signal)
 		goto out;
 
@@ -2660,7 +2662,11 @@ static bool __mem_cgroup_over_high_action(struct mem_cgroup *memcg, u8 action)
 		WRITE_ONCE(current->memcg_over_limit, true);
 		break;
 	case PR_MEMACT_SLOWDOWN:
-		/* Slow down by yielding the cpu */
+		/*
+		 * Slow down by yielding the cpu & adding delay to
+		 * memory allocation syscalls.
+		 */
+		WRITE_ONCE(current->memcg_over_limit, true);
 		set_tsk_need_resched(current);
 		set_preempt_need_resched();
 		break;
@@ -2694,6 +2700,21 @@ static inline bool mem_cgroup_over_high_action(struct mem_cgroup *memcg)
 	return __mem_cgroup_over_high_action(memcg, action);
 }
 
+/*
+ * Called from memory allocation syscalls.
+ * Return true if ENOMEM should be returned, false otherwise.
+ */
+bool mem_cgroup_check_over_limit(void)
+{
+	u8 action = READ_ONCE(current->memcg_over_high_action);
+
+	if (action == PR_MEMACT_ENOMEM)
+		return true;
+	if (action == PR_MEMACT_SLOWDOWN)
+		msleep(20);	/* Artificial delay of 20ms */
+	return false;
+}
+
 static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		      unsigned int nr_pages)
 {
mm/mlock.c +6 -0
index 93ca2bf30b4f..130d4b3fa0f5 100644
@@ -678,6 +678,9 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla
 	if (!can_do_mlock())
 		return -EPERM;
 
+	if (mem_over_memcg_limit())
+		return -ENOMEM;
+
 	len = PAGE_ALIGN(len + (offset_in_page(start)));
 	start &= PAGE_MASK;
 
@@ -807,6 +810,9 @@ SYSCALL_DEFINE1(mlockall, int, flags)
 	if (!can_do_mlock())
 		return -EPERM;
 
+	if (mem_over_memcg_limit())
+		return -ENOMEM;
+
 	lock_limit = rlimit(RLIMIT_MEMLOCK);
 	lock_limit >>= PAGE_SHIFT;
 
mm/mmap.c +12 -0
index 40248d84ad5f..873ccf2560a6 100644
@@ -198,6 +198,10 @@ SYSCALL_DEFINE1(brk, unsigned long, brk)
 	bool downgraded = false;
 	LIST_HEAD(uf);
 
+	/* Too much memory used? */
+	if (mem_over_memcg_limit())
+		return -ENOMEM;
+
 	if (mmap_write_lock_killable(mm))
 		return -EINTR;
 
@@ -1407,6 +1411,10 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
 	if (mm->map_count > sysctl_max_map_count)
 		return -ENOMEM;
 
+	/* Too much memory used? */
+	if (mem_over_memcg_limit())
+		return -ENOMEM;
+
 	/* Obtain the address to map to. we verify (or select) it and ensure
 	 * that it represents a valid section of the address space.
 	 */
@@ -1557,6 +1565,10 @@ unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
 	struct file *file = NULL;
 	unsigned long retval;
 
+	/* Too much memory used? */
+	if (mem_over_memcg_limit())
+		return -ENOMEM;
+
 	if (!(flags & MAP_ANONYMOUS)) {
 		audit_mmap_fd(fd, flags);
 		file = fget(fd);
mm/mprotect.c +3 -0
index ce8b8a5eacbb..b2c0f50bb0a0 100644
@@ -519,6 +519,9 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
 	const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
 				(prot & PROT_READ);
 
+	if (mem_over_memcg_limit())
+		return -ENOMEM;
+
 	start = untagged_addr(start);
 
 	prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
Signature · 1 line
2.18.1
#10 lore raw

RFC PATCH 3/8 memcg: Allow the use of task RSS memory as over-high action trigger

In reply to Waiman Long

The total memory consumption of a task as tracked by memory cgroup includes different types of memory like page caches, anonymous memory, share memory and kernel memory.

In a memory cgroup with a multiple running tasks, using total memory consumption of all the tasks within the cgroup as action trigger may not be fair to tasks that don't contribute to excessive memory usage.

Page cache memory can typically be shared between multiple tasks. It is also not easy to pin kernel memory usage to a specific task. That leaves a task's anonymous (RSS) memory usage as best proxy for a task's contribution to total memory consumption within the memory cgroup.

So a new set of PR_MEMFLAG_RSS_* flags are added to enable the checking of a task's real RSS memory footprint as a trigger to over-high action provided that the total memory consumption of the cgroup has exceeded memory.high + the additional memcg memory limit.

---
 include/linux/memcontrol.h |  2 +-
 include/linux/sched.h      |  3 ++-
 include/uapi/linux/prctl.h | 14 +++++++++++---
 kernel/sys.c               |  4 ++--
 mm/memcontrol.c            | 32 ++++++++++++++++++++++++++++++--
 5 files changed, 46 insertions(+), 9 deletions(-)
include/linux/memcontrol.h +1 -1
index 40e6ceb8209b..562958cf79d8 100644
@@ -447,7 +447,7 @@ void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
 
 long mem_cgroup_over_high_get(struct task_struct *task, unsigned long item);
 long mem_cgroup_over_high_set(struct task_struct *task, unsigned long action,
-			      unsigned long limit);
+			      unsigned long limit, unsigned long limit2);
 
 static struct mem_cgroup_per_node *
 mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
include/linux/sched.h +2 -1
index 9ec1bd072334..a1e9ac8b9b16 100644
@@ -1265,11 +1265,12 @@ struct task_struct {
 	/* Number of pages to reclaim on returning to userland: */
 	unsigned int			memcg_nr_pages_over_high;
 
-	/* Memory over-high action, flags, signal and limit */
+	/* Memory over-high action, flags, signal and limits */
 	unsigned char			memcg_over_high_action;
 	unsigned char			memcg_over_high_signal;
 	unsigned short			memcg_over_high_flags;
 	unsigned int			memcg_over_high_climit;
+	unsigned int			memcg_over_high_plimit;
 	unsigned int			memcg_over_limit;
 
 	/* Used by memcontrol for targeted memcg charge: */
include/uapi/linux/prctl.h +11 -3
index 87970ae7b32c..ef8d84c94b4a 100644
@@ -244,9 +244,10 @@ struct prctl_mm_map {
 
 /*
  * PR_SET_MEMCONTROL:
- * 2 parameters are passed:
+ * 3 parameters are passed:
  *  - Action word
  *  - Memory cgroup additional memory limit
+ *  - Flag specific memory limit
  *
  * The action word consists of 3 bit fields:
  *  - Bits  0-7 : over-memory-limit action code
@@ -263,8 +264,14 @@ struct prctl_mm_map {
 # define PR_MEMACT_MAX			PR_MEMACT_KILL
 
 /* Flags for PR_SET_MEMCONTROL */
-# define PR_MEMFLAG_SIGCONT		(1UL << 0) /* Continuous signal delivery */
-# define PR_MEMFLAG_MASK		PR_MEMFLAG_SIGCONT
+# define PR_MEMFLAG_SIGCONT		(1UL <<  0) /* Continuous signal delivery */
+# define PR_MEMFLAG_RSS_ANON		(1UL <<  8) /* Check anonymous pages */
+# define PR_MEMFLAG_RSS_FILE		(1UL <<  9) /* Check file pages */
+# define PR_MEMFLAG_RSS_SHMEM		(1UL << 10) /* Check shmem pages */
+# define PR_MEMFLAG_RSS			(PR_MEMFLAG_RSS_ANON |\
+					 PR_MEMFLAG_RSS_FILE |\
+					 PR_MEMFLAG_RSS_SHMEM)
+# define PR_MEMFLAG_MASK		(PR_MEMFLAG_SIGCONT | PR_MEMFLAG_RSS)
 
 /* Action word masks */
 # define PR_MEMACT_MASK			0xff
@@ -274,5 +281,6 @@ struct prctl_mm_map {
 /* Return specified value for PR_GET_MEMCONTROL */
 # define PR_MEMGET_ACTION		0
 # define PR_MEMGET_CLIMIT		1
+# define PR_MEMGET_PLIMIT		2
 
 #endif /* _LINUX_PRCTL_H */
kernel/sys.c +2 -2
index 644b86235d7f..272f82227c2d 100644
@@ -2541,9 +2541,9 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 		error = mem_cgroup_over_high_get(me, arg2);
 		break;
 	case PR_SET_MEMCONTROL:
-		if (arg4 || arg5)
+		if (arg5)
 			return -EINVAL;
-		error = mem_cgroup_over_high_set(me, arg2, arg3);
+		error = mem_cgroup_over_high_set(me, arg2, arg3, arg4);
 		break;
 #endif
 	default:
mm/memcontrol.c +30 -2
index 5cad7bb26d13..aa76bae7f408 100644
@@ -2629,6 +2629,12 @@ void mem_cgroup_handle_over_high(void)
 	css_put(&memcg->css);
 }
 
+static inline unsigned long
+get_rss_counter(struct mm_struct *mm, int mm_bit, u16 flags, int rss_bit)
+{
+	return (flags & rss_bit) ? get_mm_counter(mm, mm_bit) : 0;
+}
+
 /*
  * Task specific action when over the high limit.
  * Return true if an action has been taken or further check is not needed,
@@ -2656,6 +2662,22 @@ static bool __mem_cgroup_over_high_action(struct mem_cgroup *memcg, u8 action)
 	if (mem <= memcg->memory.high + limit)
 		goto out;
 
+	/*
+	 * Check RSS memory if any of the PR_MEMFLAG_RSS flags is set.
+	 */
+	if (flags & PR_MEMFLAG_RSS) {
+		mem = get_rss_counter(mm, MM_ANONPAGES, flags,
+				      PR_MEMFLAG_RSS_ANON) +
+		      get_rss_counter(mm, MM_FILEPAGES, flags,
+				      PR_MEMFLAG_RSS_FILE) +
+		      get_rss_counter(mm, MM_SHMEMPAGES, flags,
+				      PR_MEMFLAG_RSS_SHMEM);
+
+		limit = READ_ONCE(current->memcg_over_high_plimit);
+		if (mem <= limit)
+			goto out;
+	}
+
 	ret = true;
 	switch (action) {
 	case PR_MEMACT_ENOMEM:
@@ -7063,12 +7085,15 @@ long mem_cgroup_over_high_get(struct task_struct *task, unsigned long item)
 
 	case PR_MEMGET_CLIMIT:
 		return (long)task->memcg_over_high_climit * PAGE_SIZE;
+
+	case PR_MEMGET_PLIMIT:
+		return (long)task->memcg_over_high_plimit * PAGE_SIZE;
 	}
 	return -EINVAL;
 }
 
 long mem_cgroup_over_high_set(struct task_struct *task, unsigned long action,
-			      unsigned long limit)
+			      unsigned long limit, unsigned long limit2)
 {
 	unsigned char  cmd   = action & PR_MEMACT_MASK;
 	unsigned char  sig   = (action >> PR_MEMACT_SIG_SHIFT) & PR_MEMACT_MASK;
@@ -7084,12 +7109,15 @@ long mem_cgroup_over_high_set(struct task_struct *task, unsigned long action,
 
 	if (cmd == PR_MEMACT_NONE) {
 		WRITE_ONCE(task->memcg_over_high_climit, 0);
+		WRITE_ONCE(task->memcg_over_high_plimit, 0);
 	} else {
 		/*
 		 * Convert limits to # of pages
 		 */
-		limit = DIV_ROUND_UP(limit, PAGE_SIZE);
+		limit  = DIV_ROUND_UP(limit, PAGE_SIZE);
+		limit2 = DIV_ROUND_UP(limit2, PAGE_SIZE);
 		WRITE_ONCE(task->memcg_over_high_climit, limit);
+		WRITE_ONCE(task->memcg_over_high_plimit, limit2);
 	}
 	return 0;
 }
Signature · 1 line
2.18.1
#11 lore raw

RFC PATCH 4/8 fs/proc: Support a new procfs memctl file

In reply to Waiman Long

To allow system administrators to view and modify the over-high action settings of a running application, a new /proc/<pid>/memctl file is now added to show the over-high action parameters as well as allowing their modification.

---
 fs/proc/base.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 109 insertions(+)
fs/proc/base.c +109 -0
index 617db4e0faa0..3c9349ad1e37 100644
@@ -88,6 +88,8 @@
 #include <linux/user_namespace.h>
 #include <linux/fs_struct.h>
 #include <linux/slab.h>
+#include <linux/prctl.h>
+#include <linux/ctype.h>
 #include <linux/sched/autogroup.h>
 #include <linux/sched/mm.h>
 #include <linux/sched/coredump.h>
@@ -3145,6 +3147,107 @@ static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
 }
 #endif /* CONFIG_STACKLEAK_METRICS */
 
+#ifdef CONFIG_MEMCG
+/*
+ * Memory cgroup control parameters
+ * <over_high_action> <limit1> <limit2>
+ */
+static ssize_t proc_memctl_read(struct file *file, char __user *buf,
+				 size_t count, loff_t *ppos)
+{
+	struct task_struct *task = get_proc_task(file_inode(file));
+	unsigned long action, limit1, limit2;
+	char buffer[80];
+	ssize_t len;
+
+	if (!task)
+		return -ESRCH;
+
+	action = task->memcg_over_high_action |
+		(task->memcg_over_high_signal << PR_MEMACT_SIG_SHIFT) |
+		(task->memcg_over_high_flags  << PR_MEMACT_FLG_SHIFT);
+	limit1 = (unsigned long)task->memcg_over_high_climit  * PAGE_SIZE;
+	limit2 = (unsigned long)task->memcg_over_high_plimit * PAGE_SIZE;
+
+	put_task_struct(task);
+	len = snprintf(buffer, sizeof(buffer), "%ld %ld %ld\n",
+		       action, limit1, limit2);
+	return simple_read_from_buffer(buf, count, ppos, buffer, len);
+}
+
+static ssize_t proc_memctl_write(struct file *file, const char __user *buf,
+				  size_t count, loff_t *offs)
+{
+	struct task_struct *task = get_proc_task(file_inode(file));
+	unsigned long vals[3];
+	char buffer[80];
+	char *ptr, *next;
+	int i, err;
+	unsigned int action, signal, flags;
+
+	if (!task)
+		return -ESRCH;
+	if (count  > sizeof(buffer) - 1)
+		count = sizeof(buffer) - 1;
+	if (copy_from_user(buffer, buf, count)) {
+		err = -EFAULT;
+		goto out;
+	}
+	buffer[count] = '\0';
+	next = buffer;
+
+	/*
+	 * Expect to find 3 numbers
+	 */
+	for (i = 0, ptr = buffer; i < 3; i++) {
+		ptr = skip_spaces(next);
+		if (!*ptr) {
+			err = -EINVAL;
+			goto out;
+		}
+
+		/* Skip non-space characters for next */
+		for (next = ptr; *next && !isspace(*next); next++)
+			;
+		if (isspace(*next))
+			*next++ = '\0';
+
+		err = kstrtoul(ptr, 0, &vals[i]);
+		if (err)
+			break;
+	}
+	action = vals[0] & PR_MEMACT_MASK;
+	signal = (vals[0] >> PR_MEMACT_SIG_SHIFT) & PR_MEMACT_MASK;
+	flags  = vals[0] >> PR_MEMACT_FLG_SHIFT;
+
+	/* Round up limits to number of pages */
+	vals[1] = DIV_ROUND_UP(vals[1], PAGE_SIZE);
+	vals[2] = DIV_ROUND_UP(vals[2], PAGE_SIZE);
+
+	/* Check input values */
+	if ((action > PR_MEMACT_MAX) || (signal >= _NSIG) ||
+	    (flags & ~PR_MEMFLAG_MASK)) {
+		err = -EINVAL;
+		goto out;
+	}
+
+	WRITE_ONCE(task->memcg_over_high_action, action);
+	WRITE_ONCE(task->memcg_over_high_signal, signal);
+	WRITE_ONCE(task->memcg_over_high_flags,  flags);
+	WRITE_ONCE(task->memcg_over_high_climit, vals[1]);
+	WRITE_ONCE(task->memcg_over_high_plimit, vals[2]);
+out:
+	put_task_struct(task);
+	return err < 0 ? err : count;
+}
+
+const struct file_operations proc_memctl_operations = {
+	.read   = proc_memctl_read,
+	.write  = proc_memctl_write,
+	.llseek	= generic_file_llseek,
+};
+#endif /* CONFIG_MEMCG */
+
 /*
  * Thread groups
  */
@@ -3258,6 +3361,9 @@ static const struct pid_entry tgid_base_stuff[] = {
 #ifdef CONFIG_PROC_PID_ARCH_STATUS
 	ONE("arch_status", S_IRUGO, proc_pid_arch_status),
 #endif
+#ifdef CONFIG_MEMCG
+	REG("memctl", 0644, proc_memctl_operations),
+#endif
 };
 
 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
@@ -3587,6 +3693,9 @@ static const struct pid_entry tid_base_stuff[] = {
 #ifdef CONFIG_PROC_PID_ARCH_STATUS
 	ONE("arch_status", S_IRUGO, proc_pid_arch_status),
 #endif
+#ifdef CONFIG_MEMCG
+	REG("memctl", 0644, proc_memctl_operations),
+#endif
 };
 
 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Signature · 1 line
2.18.1
#12 lore raw

Re: [RFC PATCH 4/8] fs/proc: Support a new procfs memctl file

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Waiman,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on hnaz-linux-mm/master]
[also build test WARNING on linus/master v5.9-rc1 next-20200817] [cannot apply to tip/sched/core]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Waiman-Long/memcg-Enable-fine-grained-per-process-memory-control/20200817-221201
base:   https://github.com/hnaz/linux-mm master
config: x86_64-randconfig-s022-20200817 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.2-180-g49f7e13a-dirty
        # save the attached .config to linux build tree
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate

sparse warnings: (new ones prefixed by >>)

fs/proc/base.c:2231:25: sparse: sparse: cast to restricted fmode_t fs/proc/base.c:2288:42: sparse: sparse: cast from restricted fmode_t fs/proc/base.c:2385:48: sparse: sparse: cast from restricted fmode_t

>> fs/proc/base.c:3244:30: sparse: sparse: symbol 'proc_memctl_operations' was not declared. Should it be static?

fs/proc/base.c: note: in included file (through include/linux/rcuwait.h, include/linux/percpu-rwsem.h, include/linux/fs.h, ...):
include/linux/sched/signal.h:695:37: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
include/linux/sched/signal.h:695:37: sparse: expected struct spinlock [usertype] *lock
include/linux/sched/signal.h:695:37: sparse: got struct spinlock [noderef] __rcu *
fs/proc/base.c:1104:36: sparse: sparse: context imbalance in '__set_oom_adj' - unexpected unlock

Please review and possibly fold the followup patch.

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#13 lore raw

[RFC PATCH] fs/proc: proc_memctl_operations can be static

In reply to Waiman Long
Signed-off-by: kernel test robot <[email protected]>
---
 base.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/proc/base.c +1 -1
index 28a1afeb67a9c..463a92cf8a95d 100644
@@ -3241,7 +3241,7 @@ static ssize_t proc_memctl_write(struct file *file, const char __user *buf,
 	return err < 0 ? err : count;
 }
 
-const struct file_operations proc_memctl_operations = {
+static const struct file_operations proc_memctl_operations = {
 	.read   = proc_memctl_read,
 	.write  = proc_memctl_write,
 	.llseek	= generic_file_llseek,
#14 lore raw

RFC PATCH 5/8 memcg: Allow direct per-task memory limit checking

In reply to Waiman Long

Up to now, the PR_SET_MEMCONTROL prctl(2) call enables user-specified action only if the total memory consumption in the memory cgroup exceeds memory.high by the additional memory threshold specified.

There are cases where a user may want direct memory consumption control for certain applications even if the total cgroup memory consumption has not exceeded the limit yet. One way of doing that is to create one memory cgroup per application. However, if an application call other helper applications, these helper applications will fall into the same cgroup breaking the one application per cgroup rule.

Another alternative is to enable user to enable direct per-task memory limit checking which is what this patch is about. That is for special use cases and is not recommended for general use as memory reclaim may not be triggered even if the per-task memory limit has been exceeded.

---
 include/uapi/linux/prctl.h |  4 ++-
 mm/memcontrol.c            | 52 +++++++++++++++++++++++++++-----------
 2 files changed, 40 insertions(+), 16 deletions(-)
include/uapi/linux/prctl.h +3 -1
index ef8d84c94b4a..7ba40e10737d 100644
@@ -265,13 +265,15 @@ struct prctl_mm_map {
 
 /* Flags for PR_SET_MEMCONTROL */
 # define PR_MEMFLAG_SIGCONT		(1UL <<  0) /* Continuous signal delivery */
+# define PR_MEMFLAG_DIRECT		(1UL <<  1) /* Direct memory limit */
 # define PR_MEMFLAG_RSS_ANON		(1UL <<  8) /* Check anonymous pages */
 # define PR_MEMFLAG_RSS_FILE		(1UL <<  9) /* Check file pages */
 # define PR_MEMFLAG_RSS_SHMEM		(1UL << 10) /* Check shmem pages */
 # define PR_MEMFLAG_RSS			(PR_MEMFLAG_RSS_ANON |\
 					 PR_MEMFLAG_RSS_FILE |\
 					 PR_MEMFLAG_RSS_SHMEM)
-# define PR_MEMFLAG_MASK		(PR_MEMFLAG_SIGCONT | PR_MEMFLAG_RSS)
+# define PR_MEMFLAG_MASK		(PR_MEMFLAG_SIGCONT | PR_MEMFLAG_RSS |\
+					 PR_MEMFLAG_DIRECT)
 
 /* Action word masks */
 # define PR_MEMACT_MASK			0xff
mm/memcontrol.c +37 -15

98 lines not on this page · raw message

Signature · 1 line
2.18.1
#15 lore raw

RFC PATCH 6/8 memcg: Introduce additional memory control slowdown if needed

In reply to Waiman Long

For fast cpus on slow disks, yielding the cpus repeatedly with PR_MEMACT_SLOWDOWN may not be able to slow down memory allocation enough for memory reclaim to catch up. In case a large memory block is mmap'ed and the pages are faulted in one-by-one, the syscall delays won't be activated during this process.

To be safe, an additional variable delay of 20-5000 us will be added to __mem_cgroup_over_high_action() if the excess memory used is more than 1/256 of the memory limit.

---
 mm/memcontrol.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
mm/memcontrol.c +16 -6
index 6488f8a10d66..bddf3e659469 100644
@@ -2643,11 +2643,10 @@ get_rss_counter(struct mm_struct *mm, int mm_bit, u16 flags, int rss_bit)
 static bool __mem_cgroup_over_high_action(struct mem_cgroup *memcg, u8 action,
 					  u16 flags)
 {
-	unsigned long mem = 0;
+	unsigned long mem = 0, limit = 0, excess = 0;
 	bool ret = false;
 	struct mm_struct *mm = get_task_mm(current);
 	u8  signal = READ_ONCE(current->memcg_over_high_signal);
-	u32 limit;
 
 	if (!mm)
 		return true;	/* No more check is needed */
@@ -2657,9 +2656,10 @@ static bool __mem_cgroup_over_high_action(struct mem_cgroup *memcg, u8 action,
 
 	if (memcg) {
 		mem = page_counter_read(&memcg->memory);
-		limit = READ_ONCE(current->memcg_over_high_climit);
-		if (mem <= memcg->memory.high + limit)
+		limit = READ_ONCE(current->memcg_over_high_climit) + memcg->memory.high;
+		if (mem <= limit)
 			goto out;
+		excess = mem - limit;
 	}
 
 	/*
@@ -2676,6 +2676,7 @@ static bool __mem_cgroup_over_high_action(struct mem_cgroup *memcg, u8 action,
 		limit = READ_ONCE(current->memcg_over_high_plimit);
 		if (mem <= limit)
 			goto out;
+		excess = mem - limit;
 	}
 
 	ret = true;
@@ -2685,10 +2686,19 @@ static bool __mem_cgroup_over_high_action(struct mem_cgroup *memcg, u8 action,
 		break;
 	case PR_MEMACT_SLOWDOWN:
 		/*
-		 * Slow down by yielding the cpu & adding delay to
-		 * memory allocation syscalls.
+		 * Slow down by yielding the cpu & adding delay to memory
+		 * allocation syscalls.
+		 *
+		 * An additional 20-5000 us of delay is added in case the
+		 * excess memory is more than 1/256 of the limit.
 		 */
 		WRITE_ONCE(current->memcg_over_limit, true);
+		limit >>= 8;
+		if (limit && (excess > limit)) {
+			int delay = min(5000UL, excess/limit * 20UL);
+
+			udelay(delay);
+		}
 		set_tsk_need_resched(current);
 		set_preempt_need_resched();
 		break;
Signature · 1 line
2.18.1
#16 lore raw

RFC PATCH 7/8 memcg: Enable logging of memory control mitigation action

In reply to Waiman Long

Some of the migitation actions of PR_SET_MEMCONTROL give no visible signal that some actions are being done inside the kernel. To make it more visble, a new PR_MEMFLAG_LOG flag is added to enable the logging of the migitation action done in the kernel ring buffer.

The logging is done once when the mitigation action starts through the setting of an internal PR_MEMFLAG_LOGGED flag. This flag will be cleared when it is detected that the memory limit no longer exceeds memory.high.

---
 include/uapi/linux/prctl.h |  1 +
 mm/memcontrol.c            | 34 +++++++++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)
include/uapi/linux/prctl.h +1 -0
index 7ba40e10737d..faa7a51fc52a 100644
@@ -266,6 +266,7 @@ struct prctl_mm_map {
 /* Flags for PR_SET_MEMCONTROL */
 # define PR_MEMFLAG_SIGCONT		(1UL <<  0) /* Continuous signal delivery */
 # define PR_MEMFLAG_DIRECT		(1UL <<  1) /* Direct memory limit */
+# define PR_MEMFLAG_LOG			(1UL <<  2) /* Log action done */
 # define PR_MEMFLAG_RSS_ANON		(1UL <<  8) /* Check anonymous pages */
 # define PR_MEMFLAG_RSS_FILE		(1UL <<  9) /* Check file pages */
 # define PR_MEMFLAG_RSS_SHMEM		(1UL << 10) /* Check shmem pages */
mm/memcontrol.c +33 -1
index bddf3e659469..5bda2dd755fc 100644
@@ -2640,6 +2640,7 @@ get_rss_counter(struct mm_struct *mm, int mm_bit, u16 flags, int rss_bit)
  * Return true if an action has been taken or further check is not needed,
  * false otherwise.
  */
+#define PR_MEMFLAG_LOGGED	(1UL << 7)	/* A log message printed */
 static bool __mem_cgroup_over_high_action(struct mem_cgroup *memcg, u8 action,
 					  u16 flags)
 {
@@ -2714,6 +2715,32 @@ static bool __mem_cgroup_over_high_action(struct mem_cgroup *memcg, u8 action,
 		break;
 	}
 
+	if ((flags & (PR_MEMFLAG_LOG|PR_MEMFLAG_LOGGED)) == PR_MEMFLAG_LOG) {
+		char name[80];
+		static const char * const acts[] = {
+			[PR_MEMACT_ENOMEM]   = "Action: return ENOMEM on some syscalls",
+			[PR_MEMACT_SLOWDOWN] = "Action: slow down process",
+			[PR_MEMACT_SIGNAL]   = "Action: send signal",
+			[PR_MEMACT_KILL]     = "Action: kill the process",
+		};
+
+		name[0] = '\0';
+		if (memcg)
+			cgroup_name(memcg->css.cgroup, name, sizeof(name));
+		else
+			strcpy(name, "N/A");
+
+		/*
+		 * Use printk_deferred() to minimize delay in the memory
+		 * allocation path.
+		 */
+		printk_deferred(KERN_INFO
+			"Cgroup: %s, Comm: %s, Pid: %d, Mem: %ld pages, %s\n",
+			name, current->comm, current->pid, mem, acts[action]);
+		WRITE_ONCE(current->memcg_over_high_flags,
+			   flags | PR_MEMFLAG_LOGGED);
+	}
+
 out:
 	mmput(mm);
 	/*
@@ -2740,8 +2767,13 @@ static inline bool mem_cgroup_over_high_action(struct mem_cgroup *memcg,
 
 	if (flags & PR_MEMFLAG_DIRECT)
 		memcg = NULL;	/* Direct per-task memory limit checking */
-	else if (!mem_high)
+	else if (!mem_high) {
+		/* Clear the PR_MEMFLAG_LOGGED flag, if set */
+		if (flags & PR_MEMFLAG_LOGGED)
+			WRITE_ONCE(current->memcg_over_high_flags,
+				   flags & ~PR_MEMFLAG_LOGGED);
 		return false;
+	}
 
 	return __mem_cgroup_over_high_action(memcg, action, flags);
 }
Signature · 1 line
2.18.1
#17 lore raw

RFC PATCH 8/8 memcg: Add over-high action prctl() documentation

In reply to Waiman Long

A new memcontrol.rst documentation file is added to document the new prctl(2) interface for setting the over-high mitigation action parameters and retrieving them.

---
 Documentation/userspace-api/index.rst      |   1 +
 Documentation/userspace-api/memcontrol.rst | 174 +++++++++++++++++++++
 2 files changed, 175 insertions(+)
 create mode 100644 Documentation/userspace-api/memcontrol.rst
Documentation/userspace-api/index.rst +1 -0
index 69fc5167e648..1c0fc7a7f4ec 100644
@@ -23,6 +23,7 @@ place where this information is gathered.
    accelerators/ocxl
    ioctl/index
    media/index
+   memcontrol
 
 .. only::  subproject and html
 
Documentation/userspace-api/memcontrol.rst new file +174 -0

174 lines not on this page · raw message

Signature · 1 line
2.18.1
reviewer
#18 lore raw
In reply to Waiman Long
Quoted 30 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing. When it reaches "memory.max" for memory cgroup v2 or when the system is running out of free memory, the OOM killer will be invoked to kill some tasks to free up additional memory. However, one has little control of which tasks are going to be killed by an OOM killer. Killing tasks that hold some important resources without freeing them first can create other system problems down the road.

Users who do not want the OOM killer to be invoked to kill random tasks in an out-of-memory situation can use the memory control facility provided by this new patchset via prctl(2) to better manage the mitigation action that needs to be performed to various tasks when the specified memory limit is exceeded with memory cgroup v2 being used.

The currently supported mitigation actions include the followings:

1) Return ENOMEM for some syscalls that allocate or handle memory
2) Slow down the process for memory reclaim to catch up
3) Send a specific signal to the task
4) Kill the task

Quoted 5 lines

The users that want better memory control for their applicatons can either modify their applications to call the prctl(2) syscall directly with the new memory control command code or write the desired action to the newly provided memctl procfs files of their applications provided that those applications run in a non-root v2 memory cgroup.

prctl is fundamentally about per-process control while cgroup (not only memcg) is about group of processes interface. How do those two interact together? In other words what is the semantic when different processes have a different views on the same underlying memcg event?

Also the above description doesn't really describe any usecase which struggles with the existing interface. We already do allow slow down and along with PSI also provide user space control over close to OOM situation.

Signature · 2 lines
Michal Hocko
SUSE Labs
#19 lore raw
Quoted 36 lines · Michal Hocko
Quoted 30 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing. When it reaches "memory.max" for memory cgroup v2 or when the system is running out of free memory, the OOM killer will be invoked to kill some tasks to free up additional memory. However, one has little control of which tasks are going to be killed by an OOM killer. Killing tasks that hold some important resources without freeing them first can create other system problems down the road.

Users who do not want the OOM killer to be invoked to kill random tasks in an out-of-memory situation can use the memory control facility provided by this new patchset via prctl(2) to better manage the mitigation action that needs to be performed to various tasks when the specified memory limit is exceeded with memory cgroup v2 being used.

The currently supported mitigation actions include the followings:

1) Return ENOMEM for some syscalls that allocate or handle memory
2) Slow down the process for memory reclaim to catch up
3) Send a specific signal to the task
4) Kill the task

Quoted 5 lines

The users that want better memory control for their applicatons can either modify their applications to call the prctl(2) syscall directly with the new memory control command code or write the desired action to the newly provided memctl procfs files of their applications provided that those applications run in a non-root v2 memory cgroup.

Quoted 4 lines

prctl is fundamentally about per-process control while cgroup (not only memcg) is about group of processes interface. How do those two interact together? In other words what is the semantic when different processes have a different views on the same underlying memcg event?

As said in a previous mail, this patchset is derived from a customer request and per-process control is exactly what the customer wants. That is why prctl() is used. This patchset is intended to supplement the existing memory cgroup features. Processes in a memory cgroup that don't use this new API will behave exactly like before. Only processes that opt to use this new API will have additional mitigation actions applied on them in case the additional limits are reached.

Quoted 6 lines

Also the above description doesn't really describe any usecase which struggles with the existing interface. We already do allow slow down and along with PSI also provide user space control over close to OOM situation.

The customer that request it was using Solaris. Solaris does allow per-process memory control and they have tools that rely on this capability. This patchset will help them migrate off Solaris easier. I will look closer into how PSI can help here.

Thanks,
Longman

reviewer
#20 lore raw
Quoted 41 lines · Waiman Long
Quoted 36 lines · Michal Hocko
Quoted 30 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing. When it reaches "memory.max" for memory cgroup v2 or when the system is running out of free memory, the OOM killer will be invoked to kill some tasks to free up additional memory. However, one has little control of which tasks are going to be killed by an OOM killer. Killing tasks that hold some important resources without freeing them first can create other system problems down the road.

Users who do not want the OOM killer to be invoked to kill random tasks in an out-of-memory situation can use the memory control facility provided by this new patchset via prctl(2) to better manage the mitigation action that needs to be performed to various tasks when the specified memory limit is exceeded with memory cgroup v2 being used.

The currently supported mitigation actions include the followings:

1) Return ENOMEM for some syscalls that allocate or handle memory
2) Slow down the process for memory reclaim to catch up
3) Send a specific signal to the task
4) Kill the task

Quoted 5 lines

The users that want better memory control for their applicatons can either modify their applications to call the prctl(2) syscall directly with the new memory control command code or write the desired action to the newly provided memctl procfs files of their applications provided that those applications run in a non-root v2 memory cgroup.

Quoted 4 lines

prctl is fundamentally about per-process control while cgroup (not only memcg) is about group of processes interface. How do those two interact together? In other words what is the semantic when different processes have a different views on the same underlying memcg event?

Quoted 7 lines

As said in a previous mail, this patchset is derived from a customer request and per-process control is exactly what the customer wants. That is why prctl() is used. This patchset is intended to supplement the existing memory cgroup features. Processes in a memory cgroup that don't use this new API will behave exactly like before. Only processes that opt to use this new API will have additional mitigation actions applied on them in case the additional limits are reached.

Please keep in mind that you are proposing a new user API that we will have to maintain for ever. That requires that the interface is consistent and well defined. As I've said the fundamental problem with this interface is that you are trying to hammer a process centric interface into a framework that is fundamentally process group oriented. Maybe there is a sensible way to do that without all sorts of weird corner cases but I haven't seen any of that explained here.

Really just try to describe a semantic when two different tasks in the same memcg have a different opinion on the same event. One wants ENOMEM and other a specific signal to be delivered. Right now the behavior will be timing specific because who hits the oom path is non-deterministic from the userspace POV. Let's say that you can somehow handle that, now how are you going implement ENOMEM for any context other than current task? I am pretty sure the more specific questions you will have the more this will get awkward.

Signature · 2 lines
Michal Hocko
SUSE Labs
#21 lore raw
Quoted 57 lines · Michal Hocko
Quoted 41 lines · Waiman Long
Quoted 36 lines · Michal Hocko
Quoted 30 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing. When it reaches "memory.max" for memory cgroup v2 or when the system is running out of free memory, the OOM killer will be invoked to kill some tasks to free up additional memory. However, one has little control of which tasks are going to be killed by an OOM killer. Killing tasks that hold some important resources without freeing them first can create other system problems down the road.

Users who do not want the OOM killer to be invoked to kill random tasks in an out-of-memory situation can use the memory control facility provided by this new patchset via prctl(2) to better manage the mitigation action that needs to be performed to various tasks when the specified memory limit is exceeded with memory cgroup v2 being used.

The currently supported mitigation actions include the followings:

1) Return ENOMEM for some syscalls that allocate or handle memory
2) Slow down the process for memory reclaim to catch up
3) Send a specific signal to the task
4) Kill the task

Quoted 5 lines

The users that want better memory control for their applicatons can either modify their applications to call the prctl(2) syscall directly with the new memory control command code or write the desired action to the newly provided memctl procfs files of their applications provided that those applications run in a non-root v2 memory cgroup.

Quoted 4 lines

prctl is fundamentally about per-process control while cgroup (not only memcg) is about group of processes interface. How do those two interact together? In other words what is the semantic when different processes have a different views on the same underlying memcg event?

Quoted 7 lines

As said in a previous mail, this patchset is derived from a customer request and per-process control is exactly what the customer wants. That is why prctl() is used. This patchset is intended to supplement the existing memory cgroup features. Processes in a memory cgroup that don't use this new API will behave exactly like before. Only processes that opt to use this new API will have additional mitigation actions applied on them in case the additional limits are reached.

Please keep in mind that you are proposing a new user API that we will have to maintain for ever. That requires that the interface is consistent and well defined. As I've said the fundamental problem with this interface is that you are trying to hammer a process centric interface into a framework that is fundamentally process group oriented. Maybe there is a sensible way to do that without all sorts of weird corner cases but I haven't seen any of that explained here.

Quoted 8 lines

Really just try to describe a semantic when two different tasks in the same memcg have a different opinion on the same event. One wants ENOMEM and other a specific signal to be delivered. Right now the behavior will be timing specific because who hits the oom path is non-deterministic from the userspace POV. Let's say that you can somehow handle that, now how are you going implement ENOMEM for any context other than current task? I am pretty sure the more specific questions you will have the more this will get awkward.

The basic idea of triggering a user-specified memory-over-high mitigation is when the actual memory usage exceed a threshold which is supposed to be between "high" and "max". The additional limit that is passed in is for setting this additional threshold. We want to avoid OOM at all cost.

The ENOMEM error may not be suitable for all applications as some of them may not be able to handle ENOMEM gracefully. That is for applications that are designed to handle that.

Cheers,
Longman

#22 lore raw
In reply to Waiman Long
Quoted 10 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing.

Then slow down the allocator? That's what we do for dirty pages too, we slow down the dirtier when we run against the limits.

reviewer
#23 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Quoted 6 lines
Quoted 3 lines

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing.

Then slow down the allocator? That's what we do for dirty pages too, we slow down the dirtier when we run against the limits.

This is what we actually do. Have a look at mem_cgroup_handle_over_high.

Signature · 2 lines
Michal Hocko
SUSE Labs
#24 lore raw
Quoted 9 lines · Michal Hocko
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing.

Then slow down the allocator? That's what we do for dirty pages too, we slow down the dirtier when we run against the limits.

This is what we actually do. Have a look at mem_cgroup_handle_over_high.

But then how can it run-away like Waiman suggested?

/me goes look... and finds MEMCG_MAX_HIGH_DELAY_JIFFIES.

That's a fail... :-(

reviewer
#25 lore raw
Quoted 14 lines · Peter Zijlstra
Quoted 13 lines · Michal Hocko
Quoted 12 lines · Peter Zijlstra
Quoted 10 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Then slow down the allocator? That's what we do for dirty pages too, we slow down the dirtier when we run against the limits.

This is what we actually do. Have a look at mem_cgroup_handle_over_high.

But then how can it run-away like Waiman suggested?

As Chris mentioned in other reply. This functionality is quite new.

Quoted 1 line

/me goes look... and finds MEMCG_MAX_HIGH_DELAY_JIFFIES.

We can certainly tune a different backoff delays but I suspect this is not the problem here.

Quoted 1 line

That's a fail... :-(

Signature · 2 lines
Michal Hocko
SUSE Labs
#26 lore raw
Quoted 8 lines · Michal Hocko
Quoted 1 line

But then how can it run-away like Waiman suggested?

As Chris mentioned in other reply. This functionality is quite new.

Quoted 1 line

/me goes look... and finds MEMCG_MAX_HIGH_DELAY_JIFFIES.

We can certainly tune a different backoff delays but I suspect this is not the problem here.

Tuning? That thing needs throwing out, it's fundamentally buggered. Why didn't anybody look at how the I/O drtying thing works first?

What you need is a feeback loop against the rate of freeing pages, and when you near the saturation point, the allocation rate should exactly match the freeing rate.

But this thing has nothing what so ever like that.

reviewer
#27 lore raw
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Michal Hocko
Quoted 1 line

But then how can it run-away like Waiman suggested?

As Chris mentioned in other reply. This functionality is quite new.

Quoted 1 line

/me goes look... and finds MEMCG_MAX_HIGH_DELAY_JIFFIES.

We can certainly tune a different backoff delays but I suspect this is not the problem here.

Quoted 8 lines

Tuning? That thing needs throwing out, it's fundamentally buggered. Why didn't anybody look at how the I/O drtying thing works first?

What you need is a feeback loop against the rate of freeing pages, and when you near the saturation point, the allocation rate should exactly match the freeing rate.

But this thing has nothing what so ever like that.

Existing usecases seem to be doing fine with the existing implementation. If we find out that this is insufficient then we can work on that but I believe this is tangent to this email thread. There are no indications that the current implementation doesn't throttle enough. The proposal also aims at much richer interface to define the oom behavior.

Signature · 2 lines
Michal Hocko
SUSE Labs
#28 lore raw
Quoted 2 lines · Michal Hocko

The proposal also aims at much richer interface to define the oom behavior.

Oh yeah, I'm not defending any of that prctl() nonsense.

Just saying that from a math / control theory point of view, the current thing is a abhorrent failure.

maintainer
#29 lore raw
In reply to [email protected]
Quoted 3 lines · On Tue, Aug 18, 2020 at 12:18:44PM +0200…

What you need is a feeback loop against the rate of freeing pages, and when you near the saturation point, the allocation rate should exactly match the freeing rate.

IO throttling solves a slightly different problem.

IO occurs in parallel to the workload's execution stream, and you're trying to take the workload from dirtying at CPU speed to rate match to the independent IO stream.

With memory allocations, though, freeing happens from inside the execution stream of the workload. If you throttle allocations, you're most likely throttling the freeing rate as well. And you'll slow down reclaim scanning by the same amount as the page references, so it's not making reclaim more successful either. The alloc/use/free (im)balance is an inherent property of the workload, regardless of the speed you're executing it at.

So the goal here is different. We're not trying to pace the workload into some form of sustainability. Rather, it's for OOM handling. When we detect the workload's alloc/use/free pattern is unsustainable given available memory, we slow it down just enough to allow userspace to implement OOM policy and job priorities (on containerized hosts these tend to be too complex to express in the kernel's oom scoring system).

The exponential curve makes it look like we're trying to do some type of feedback system, but it's really only to let minor infractions pass and throttle unsustainable expansion ruthlessly. Drop-behind reclaim can be a bit bumpy because we batch on the allocation side as well as on the reclaim side, hence the fuzz factor there.

maintainer
#30 lore raw
Quoted 5 lines · Johannes Weiner
Quoted 3 lines · On Tue, Aug 18, 2020 at 12:18:44PM +0200…

What you need is a feeback loop against the rate of freeing pages, and when you near the saturation point, the allocation rate should exactly match the freeing rate.

Quoted 8 lines

IO throttling solves a slightly different problem.

IO occurs in parallel to the workload's execution stream, and you're trying to take the workload from dirtying at CPU speed to rate match to the independent IO stream.

With memory allocations, though, freeing happens from inside the execution stream of the workload. If you throttle allocations, you're

For a single task, but even then you're making the argument that we need to allocate memory to free memory, and we all know where that gets us.

But we're actually talking about a cgroup here, which is a collection of tasks all doing things in parallel.

Quoted 5 lines

most likely throttling the freeing rate as well. And you'll slow down reclaim scanning by the same amount as the page references, so it's not making reclaim more successful either. The alloc/use/free (im)balance is an inherent property of the workload, regardless of the speed you're executing it at.

Arguably seeing the rate drop to near 0 is a very good point to consider running cgroup-OOM.

maintainer
#31 lore raw
Quoted 12 lines · Peter Zijlstra
Quoted 11 lines · Johannes Weiner
Quoted 3 lines · On Tue, Aug 18, 2020 at 12:18:44PM +0200, [email protected] wrote:

What you need is a feeback loop against the rate of freeing pages, and when you near the saturation point, the allocation rate should exactly match the freeing rate.

IO throttling solves a slightly different problem.

IO occurs in parallel to the workload's execution stream, and you're trying to take the workload from dirtying at CPU speed to rate match to the independent IO stream.

Quoted 8 lines
Quoted 2 lines

With memory allocations, though, freeing happens from inside the execution stream of the workload. If you throttle allocations, you're

For a single task, but even then you're making the argument that we need to allocate memory to free memory, and we all know where that gets us.

But we're actually talking about a cgroup here, which is a collection of tasks all doing things in parallel.

Right, but sharing a memory cgroup means sharing an LRU list, and that transfers memory pressure and allocation burden between otherwise independent tasks - if nothing else through cache misses on the executables and libraries. I doubt that one task can go through several comprehensive reclaim cycles on a shared LRU without completely annihilating the latency or throughput targets of everybody else in the group in most real world applications.

Quoted 8 lines
Quoted 5 lines

most likely throttling the freeing rate as well. And you'll slow down reclaim scanning by the same amount as the page references, so it's not making reclaim more successful either. The alloc/use/free (im)balance is an inherent property of the workload, regardless of the speed you're executing it at.

Arguably seeing the rate drop to near 0 is a very good point to consider running cgroup-OOM.

Agreed. In the past, that's actually what we did: In cgroup1, you could disable the kernel OOM killer, and when reclaim failed at the limit, the allocating task would be put on a waitqueue until woken up by a freeing event. Conceptually this is clean & straight-forward.

However,

1. Putting allocation contexts with unknown locks to indefinite sleep caused deadlocks, for obvious reasons. Userspace OOM killing tends to take a lot of task-specific locks when scanning through /proc files for kill candidates, and can easily get stuck.

Using bounded over indefinite waits is simply acknowledging that the deadlock potential when connecting arbitrary task stacks in the system through free->alloc ordering is equally difficult to plan out as alloc->free ordering.

The non-cgroup OOM killer actually has the same deadlock potential, where the allocating/killing task can hold resources that the OOM victim requires to exit. The OOM reaper hides it, the static emergency reserves hide it - but to truly solve this problem, you would have to have full knowledge of memory & lock ordering dependencies of those tasks. And then can still end up with scenarios where the only answer is panic().

2. I don't recall ever seeing situations in cgroup1 where the precise matching of allocation rate to freeing rate has allowed cgroups to run sustainably after reclaim has failed. The practical benefit of a complicated feedback loop over something crude & robust once we're in an OOM situation is not apparent to me.

[ That's different from the IO-throttling *while still doing
reclaim* that Dave brought up. *That* justifies the same effort we put into dirty throttling. I'm only talking about the situation where reclaim has already failed and we need to facilitate userspace OOM handling. ]

So that was the motivation for the bounded sleeps. They do not guarantee containment, but they provide a reasonable amount of time for the userspace OOM handler to intervene, without deadlocking.

That all being said, the semantics of the new 'high' limit in cgroup2 have allowed us to move reclaim/limit enforcement out of the allocation context and into the userspace return path.

See the call to mem_cgroup_handle_over_high() from tracehook_notify_resume(), and the comments in try_charge() around set_notify_resume().

This already solves the free->alloc ordering problem by allowing the allocation to exceed the limit temporarily until at least all locks are dropped, we know we can sleep etc., before performing enforcement.

That means we may not need the timed sleeps anymore for that purpose, and could bring back directed waits for freeing-events again.

What do you think? Any hazards around indefinite sleeps in that resume path? It's called before __rseq_handle_notify_resume and the arch-specific resume callback (which appears to be a no-op currently).

Chris, Michal, what are your thoughts? It would certainly be simpler conceptually on the memcg side.

#32 lore raw
Quoted 15 lines · Johannes Weiner

That all being said, the semantics of the new 'high' limit in cgroup2 have allowed us to move reclaim/limit enforcement out of the allocation context and into the userspace return path.

See the call to mem_cgroup_handle_over_high() from tracehook_notify_resume(), and the comments in try_charge() around set_notify_resume().

This already solves the free->alloc ordering problem by allowing the allocation to exceed the limit temporarily until at least all locks are dropped, we know we can sleep etc., before performing enforcement.

That means we may not need the timed sleeps anymore for that purpose, and could bring back directed waits for freeing-events again.

Quoted 6 lines

What do you think? Any hazards around indefinite sleeps in that resume path? It's called before __rseq_handle_notify_resume and the arch-specific resume callback (which appears to be a no-op currently).

Chris, Michal, what are your thoughts? It would certainly be simpler conceptually on the memcg side.

I'm not against that, although I personally don't feel very strongly about it either way, since the current behaviour clearly works in practice.

reviewer
#33 lore raw
In reply to Johannes Weiner

[Sorry, this slipped through cracks]

Quoted 1 line · Johannes Weiner

On Fri, Aug 21, 2020 at 09:37:16PM +0200, Peter Zijlstra wrote:

[...]

Quoted 20 lines
Quoted 2 lines

Arguably seeing the rate drop to near 0 is a very good point to consider running cgroup-OOM.

Agreed. In the past, that's actually what we did: In cgroup1, you could disable the kernel OOM killer, and when reclaim failed at the limit, the allocating task would be put on a waitqueue until woken up by a freeing event. Conceptually this is clean & straight-forward.

However,

1. Putting allocation contexts with unknown locks to indefinite sleep caused deadlocks, for obvious reasons. Userspace OOM killing tends to take a lot of task-specific locks when scanning through /proc files for kill candidates, and can easily get stuck.

Using bounded over indefinite waits is simply acknowledging that the deadlock potential when connecting arbitrary task stacks in the system through free->alloc ordering is equally difficult to plan out as alloc->free ordering.

Quoted 7 lines

The non-cgroup OOM killer actually has the same deadlock potential, where the allocating/killing task can hold resources that the OOM victim requires to exit. The OOM reaper hides it, the static emergency reserves hide it - but to truly solve this problem, you would have to have full knowledge of memory & lock ordering dependencies of those tasks. And then can still end up with scenarios where the only answer is panic().

Yes. Even killing all eligible tasks is not guaranteed to help the situation because a) resources might be not bound to a process life time (e.g. tmpfs) or ineligible task might be holding resources that block others to do the proper cleanup. OOM reaper is here to make sure we reclaim some of the address space of the victim and we go over all eligible tasks rather than getting stuck at the first victim for ever.

Quoted 5 lines

2. I don't recall ever seeing situations in cgroup1 where the precise matching of allocation rate to freeing rate has allowed cgroups to run sustainably after reclaim has failed. The practical benefit of a complicated feedback loop over something crude & robust once we're in an OOM situation is not apparent to me.

Yes, this is usually go OOM and kill something. Running on a very edge of the (memcg) oom doesn't tend to be sustainable and I am not sure it makes sense to optimize for.

Quoted 9 lines

[ That's different from the IO-throttling *while still doing
reclaim* that Dave brought up. *That* justifies the same effort we put into dirty throttling. I'm only talking about the situation where reclaim has already failed and we need to facilitate userspace OOM handling. ]

So that was the motivation for the bounded sleeps. They do not guarantee containment, but they provide a reasonable amount of time for the userspace OOM handler to intervene, without deadlocking.

Yes, memory.high is mostly a best effort containment. We do have the hard limit to put a stop on runaways or if you are watching for PSI then the high limit throttling would give you enough idea to take an action from the userspace.

Quoted 15 lines

That all being said, the semantics of the new 'high' limit in cgroup2 have allowed us to move reclaim/limit enforcement out of the allocation context and into the userspace return path.

See the call to mem_cgroup_handle_over_high() from tracehook_notify_resume(), and the comments in try_charge() around set_notify_resume().

This already solves the free->alloc ordering problem by allowing the allocation to exceed the limit temporarily until at least all locks are dropped, we know we can sleep etc., before performing enforcement.

That means we may not need the timed sleeps anymore for that purpose, and could bring back directed waits for freeing-events again.

Quoted 6 lines

What do you think? Any hazards around indefinite sleeps in that resume path? It's called before __rseq_handle_notify_resume and the arch-specific resume callback (which appears to be a no-op currently).

Chris, Michal, what are your thoughts? It would certainly be simpler conceptually on the memcg side.

I would need a more specific description. But as I've already said. It doesn't seem that we are in a need to fix any practical problem here. High limit implementation has changed quite a lot recently. I would rather see it settled for a while and see how it behaves in wider variety of workloads before changing the implementation again.

Signature · 2 lines
Michal Hocko
SUSE Labs
#34 lore raw
In reply to [email protected]
Quoted 1 line · [email protected] writes:

But then how can it run-away like Waiman suggested?

Probably because he's not running with that commit at all. We and others use this to prevent runaway allocation on a huge range of production and desktop use cases and it works just fine.

Quoted 3 lines

/me goes look... and finds MEMCG_MAX_HIGH_DELAY_JIFFIES.

That's a fail... :-(

I'd ask that you understand a bit more about the tradeoffs and intentions of the patch before rushing in to declare its failure, considering it works just fine :-)

Clamping the maximal time allows the application to take some action to remediate the situation, while still being slowed down significantly. 2 seconds per allocation batch is still absolutely plenty for any use case I've come across. If you have evidence it isn't, then present that instead of vague notions of "wrongness".

#35 lore raw
Quoted 9 lines · Chris Down

I'd ask that you understand a bit more about the tradeoffs and intentions of the patch before rushing in to declare its failure, considering it works just fine :-)

Clamping the maximal time allows the application to take some action to remediate the situation, while still being slowed down significantly. 2 seconds per allocation batch is still absolutely plenty for any use case I've come across. If you have evidence it isn't, then present that instead of vague notions of "wrongness".

There is no feedback from the freeing rate, therefore it cannot be correct in maintaining a maximum amount of pages.

0.5 pages / sec is still non-zero, and if the free rate is 0, you'll crawl across whatever limit was set without any bounds. This is math 101.

It's true that I haven't been paying attention to mm in a while, but I was one of the original authors of the I/O dirty balancing, I do think I understand how these things work.

#36 lore raw
Quoted 6 lines · [email protected] writes:
Quoted 4 lines · Chris Down

I'd ask that you understand a bit more about the tradeoffs and intentions of the patch before rushing in to declare its failure, considering it works just fine :-)

Quoted 8 lines
Quoted 5 lines

Clamping the maximal time allows the application to take some action to remediate the situation, while still being slowed down significantly. 2 seconds per allocation batch is still absolutely plenty for any use case I've come across. If you have evidence it isn't, then present that instead of vague notions of "wrongness".

There is no feedback from the freeing rate, therefore it cannot be correct in maintaining a maximum amount of pages.

memory.high is not about maintaining a maximum amount of pages. It's strictly best-effort, and the ramifications of a breach are typically fundamentally different than for dirty throttling.

Quoted 7 lines

0.5 pages / sec is still non-zero, and if the free rate is 0, you'll crawl across whatever limit was set without any bounds. This is math 101.

It's true that I haven't been paying attention to mm in a while, but I was one of the original authors of the I/O dirty balancing, I do think I understand how these things work.

You're suggesting we replace a well understood, easy to reason about model with something non-trivially more complex, all on the back of you suggesting that the current approach is "wrong" without any evidence or quantification.

Peter, we're not going to throw out perfectly function memcg code simply because of your say so, especially when you've not asked for information or context about the tradeoffs involved, or presented any evidence that something perverse is actually happening.

Prescribing a specific solution modelled on some other code path here without producing evidence or measurements specific to the nuances of this particular endpoint is not a recipe for success.

#37 lore raw
In reply to Chris Down
Quoted 5 lines · Chris Down
Quoted 1 line · [email protected] writes:

But then how can it run-away like Waiman suggested?

Probably because he's not running with that commit at all. We and others use this to prevent runaway allocation on a huge range of production and desktop use cases and it works just fine.

Quoted 8 lines
Quoted 3 lines

/me goes look... and finds MEMCG_MAX_HIGH_DELAY_JIFFIES.

That's a fail... :-(

I'd ask that you understand a bit more about the tradeoffs and intentions of the patch before rushing in to declare its failure, considering it works just fine :-)

Clamping the maximal time allows the application to take some action to remediate the situation, while still being slowed down significantly. 2 seconds per allocation batch is still absolutely plenty for any use case I've come across. If you have evidence it isn't, then present that instead of vague notions of "wrongness".

Sorry for the late reply.

I ran some test on the latest kernel and and it seems to work as expected. I was running the test on an older kernel that doesn't have this patch and I was not aware of it before hand.

Sorry for the confusion.

Cheers,
Longman

#38 lore raw
In reply to [email protected]
Quoted 8 lines · [email protected] writes:
Quoted 7 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Quoted 6 lines
Quoted 3 lines

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing.

Then slow down the allocator? That's what we do for dirty pages too, we slow down the dirtier when we run against the limits.

We already do that since v5.4. I'm wondering whether Waiman's customer is just running with a too-old kernel without 0e4b01df865 ("mm, memcg: throttle allocators when failing reclaim over memory.high") backported.

#39 lore raw
Quoted 13 lines · Chris Down
Quoted 12 lines · [email protected] writes:
Quoted 10 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing.

Quoted 6 lines
Quoted 2 lines

Then slow down the allocator? That's what we do for dirty pages too, we slow down the dirtier when we run against the limits.

We already do that since v5.4. I'm wondering whether Waiman's customer is just running with a too-old kernel without 0e4b01df865 ("mm, memcg: throttle allocators when failing reclaim over memory.high") backported.

That commit is fundamentally broken, it doesn't guarantee anything.

Please go read how the dirty throttling works (unless people wrecked that since..).

#40 lore raw
Quoted 17 lines · On Tue, Aug 18, 2020 at 12:04:44PM +0200…
Quoted 16 lines · Chris Down
Quoted 14 lines · [email protected] writes:
Quoted 10 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing.

Then slow down the allocator? That's what we do for dirty pages too, we slow down the dirtier when we run against the limits.

Quoted 8 lines
Quoted 3 lines

We already do that since v5.4. I'm wondering whether Waiman's customer is just running with a too-old kernel without 0e4b01df865 ("mm, memcg: throttle allocators when failing reclaim over memory.high") backported.

That commit is fundamentally broken, it doesn't guarantee anything.

Please go read how the dirty throttling works (unless people wrecked that since..).

Of course they did.

https://lore.kernel.org/linux-mm/ce7975cd-6353-3f29-b52c-7a81b1d07caa-tSWWG44O7X1aa/[email protected]/

#41 lore raw
Quoted 22 lines · Matthew Wilcox
Quoted 21 lines · On Tue, Aug 18, 2020 at 12:04:44PM +0200, [email protected] wrote:
Quoted 19 lines · Chris Down
Quoted 8 lines · [email protected] writes:
Quoted 7 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Quoted 6 lines
Quoted 3 lines

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing.

Then slow down the allocator? That's what we do for dirty pages too, we slow down the dirtier when we run against the limits.

We already do that since v5.4. I'm wondering whether Waiman's customer is just running with a too-old kernel without 0e4b01df865 ("mm, memcg: throttle allocators when failing reclaim over memory.high") backported.

Quoted 8 lines
Quoted 4 lines

That commit is fundamentally broken, it doesn't guarantee anything.

Please go read how the dirty throttling works (unless people wrecked that since..).

Of course they did.

https://lore.kernel.org/linux-mm/[email protected]/

Different thing. That's memory reclaim throttling, not dirty page throttling. balance_dirty_pages() still works just fine as it does not look at device congestion. page cleaning rate is accounted in test_clear_page_writeback(), page dirtying rate is accounted directly in balance_dirty_pages(). That feedback loop has not been broken...

And I compeltely agree with Peter here - the control theory we applied to the dirty throttling problem is still 100% valid and so the algorithm still just works all these years later. I've only been saying that allocation should use the same feedback model for reclaim throttling since ~2011...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#42 lore raw
In reply to Chris Down
Quoted 9 lines · Chris Down
Quoted 8 lines · [email protected] writes:
Quoted 7 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing.

Then slow down the allocator? That's what we do for dirty pages too, we slow down the dirtier when we run against the limits.

We already do that since v5.4. I'm wondering whether Waiman's customer is just running with a too-old kernel without 0e4b01df865 ("mm, memcg: throttle allocators when failing reclaim over memory.high") backported.

The fact is that we don't have that in RHEL8 yet and cgroup v2 is still not the default at the moment.

I am planning to backport the throttling patches to RHEL and hopefully can switch to use cgroup v2 soon.

Cheers,
Longman

#43 lore raw
In reply to [email protected]
Quoted 8 lines · On 8/18/20 5:14 AM, [email protected] wrote:
Quoted 7 lines · Waiman Long

Memory controller can be used to control and limit the amount of physical memory used by a task. When a limit is set in "memory.high" in a v2 non-root memory cgroup, the memory controller will try to reclaim memory if the limit has been exceeded. Normally, that will be enough to keep the physical memory consumption of tasks in the memory cgroup to be around or below the "memory.high" limit.

Quoted 6 lines
Quoted 3 lines

Sometimes, memory reclaim may not be able to recover memory in a rate that can catch up to the physical memory allocation rate. In this case, the physical memory consumption will keep on increasing.

Then slow down the allocator? That's what we do for dirty pages too, we slow down the dirtier when we run against the limits.

I missed that there are already allocator throttling done in upstream code. So I will need to reexamine if this patch is necessary or not.

Thanks,
Longman