Skip to contentSkip to conversation

memcg oom: don't try to kill a process if there is no process

PATCH Superseded Yafang Shao 2 participants 6 messages May 2, 2020 – May 4, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover memcg oom: don't try to kill a process if there is no process

Recently Shakeel reported a issue which also confused me serveral months earlier that it still try to kill a process even if there is no process. If a memcg is not populated, it is useless to try to kill a process.

mem_cgroup_out_of_memory() is renamed to mem_cgroup_oom_kill() for better understanding.

Yafang Shao (2):
mm, memcg: better name mem_cgroup_out_of_memory()
mm, memcg: don't try to kill a process if memcg is not populated

 mm/memcontrol.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
Signature · 1 line
2.18.2
#1 lore raw

PATCH 1/2 mm, memcg: better name mem_cgroup_out_of_memory()

Rename mem_cgroup_out_of_memory() to mem_cgroup_oom_kill() to indicate that this function is used to try to kill a process.
With this change it will cooperate better with the oom events.
function memcg event
mem_cgroup_oom() oom
mem_cgroup_oom_kill() oom_kill

Cc Shakeel Butt <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Greg Thelen <[email protected]>
---
 mm/memcontrol.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
mm/memcontrol.c +5 -5
index 5beea03dd58a..985edce98491 100644
@@ -1570,7 +1570,7 @@ unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
 	return page_counter_read(&memcg->memory);
 }
 
-static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
+static bool mem_cgroup_oom_kill(struct mem_cgroup *memcg, gfp_t gfp_mask,
 				     int order)
 {
 	struct oom_control oc = {
@@ -1799,7 +1799,7 @@ static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int
 	 * relying on the oom victim to make a forward progress and we can
 	 * invoke the oom killer here.
 	 *
-	 * Please note that mem_cgroup_out_of_memory might fail to find a
+	 * Please note that mem_cgroup_oom_kill might fail to find a
 	 * victim and then we have to bail out from the charge path.
 	 */
 	if (memcg->oom_kill_disable) {
@@ -1821,7 +1821,7 @@ static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int
 		mem_cgroup_oom_notify(memcg);
 
 	mem_cgroup_unmark_under_oom(memcg);
-	if (mem_cgroup_out_of_memory(memcg, mask, order))
+	if (mem_cgroup_oom_kill(memcg, mask, order))
 		ret = OOM_SUCCESS;
 	else
 		ret = OOM_FAILED;
@@ -1879,7 +1879,7 @@ bool mem_cgroup_oom_synchronize(bool handle)
 	if (locked && !memcg->oom_kill_disable) {
 		mem_cgroup_unmark_under_oom(memcg);
 		finish_wait(&memcg_oom_waitq, &owait.wait);
-		mem_cgroup_out_of_memory(memcg, current->memcg_oom_gfp_mask,
+		mem_cgroup_oom_kill(memcg, current->memcg_oom_gfp_mask,
 					 current->memcg_oom_order);
 	} else {
 		schedule();
@@ -6102,7 +6102,7 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
 		}
 
 		memcg_memory_event(memcg, MEMCG_OOM);
-		if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
+		if (!mem_cgroup_oom_kill(memcg, GFP_KERNEL, 0))
 			break;
 	}
 
Signature · 1 line
2.18.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] mm, memcg: better name mem_cgroup_out_of_memory()

Quoted 8 lines · Yafang Shao

Rename mem_cgroup_out_of_memory() to mem_cgroup_oom_kill() to indicate that this function is used to try to kill a process.
With this change it will cooperate better with the oom events.

        function                memcg event
        mem_cgroup_oom()        oom
        mem_cgroup_oom_kill()   oom_kill
Quoted 6 lines
Cc Shakeel Butt <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Greg Thelen <[email protected]>
Quoted 56 lines
---
 mm/memcontrol.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
mm/memcontrol.c +5 -6
index 5beea03dd58a..985edce98491 100644
@@ -1570,7 +1570,7 @@ unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
        return page_counter_read(&memcg->memory);
 }
 
-static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
+static bool mem_cgroup_oom_kill(struct mem_cgroup *memcg, gfp_t gfp_mask,
                                     int order)
 {
        struct oom_control oc = {
@@ -1799,7 +1799,7 @@ static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int
         * relying on the oom victim to make a forward progress and we can
         * invoke the oom killer here.
         *
-        * Please note that mem_cgroup_out_of_memory might fail to find a
+        * Please note that mem_cgroup_oom_kill might fail to find a
         * victim and then we have to bail out from the charge path.
         */
        if (memcg->oom_kill_disable) {
@@ -1821,7 +1821,7 @@ static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int
                mem_cgroup_oom_notify(memcg);
 
        mem_cgroup_unmark_under_oom(memcg);
-       if (mem_cgroup_out_of_memory(memcg, mask, order))
+       if (mem_cgroup_oom_kill(memcg, mask, order))
                ret = OOM_SUCCESS;
        else
                ret = OOM_FAILED;
@@ -1879,7 +1879,7 @@ bool mem_cgroup_oom_synchronize(bool handle)
        if (locked && !memcg->oom_kill_disable) {
                mem_cgroup_unmark_under_oom(memcg);
                finish_wait(&memcg_oom_waitq, &owait.wait);
-               mem_cgroup_out_of_memory(memcg, current->memcg_oom_gfp_mask,
+               mem_cgroup_oom_kill(memcg, current->memcg_oom_gfp_mask,
                                         current->memcg_oom_order);
        } else {
                schedule();
@@ -6102,7 +6102,7 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
                }
 
                memcg_memory_event(memcg, MEMCG_OOM);
-               if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
+               if (!mem_cgroup_oom_kill(memcg, GFP_KERNEL, 0))
                        break;
        }
 
--

2.18.2

#3 lore raw

PATCH 2/2 mm, memcg: don't try to kill a process if memcg is not populated

In reply to Yafang Shao

Recently Shakeel reported a issue which also confused me serveral months earlier. Bellow is his report -

Lowering memory.max can trigger an oom-kill if the reclaim does not succeed. However if oom-killer does not find a process for killing, it dumps a lot of warnings.

Deleting a memcg does not reclaim memory from it and the memory can linger till there is a memory pressure. One normal way to proactively reclaim such memory is to set memory.max to 0 just before deleting the memcg. However if some of the memcg's memory is pinned by others, this operation can trigger an oom-kill without any process and thus can log a lot un-needed warnings. So, ignore all such warnings from memory.max.

[[email protected]: commit log above]

A better way to avoid this issue is to avoid trying to kill a process if memcg is not populated.
Note that OOM is different with OOM kill. OOM is a status that the system or memcg is out of memory, while OOM kill is a result that a process inside this memcg is killed when this memcg is in OOM status. That is the same reason why there're both MEMCG_OOM event and MEMCG_OOM_KILL event. If we have already known that there's nothing to kill, i.e. the memcg is not populated, then we don't need to have a try.

Cc Shakeel Butt <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Greg Thelen <[email protected]>
---
 mm/memcontrol.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/memcontrol.c +4 -0
index 985edce98491..29afe3df9d98 100644
@@ -6102,6 +6102,10 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
 		}
 
 		memcg_memory_event(memcg, MEMCG_OOM);
+
+		if (!cgroup_is_populated(memcg->css.cgroup))
+			break;
+
 		if (!mem_cgroup_oom_kill(memcg, GFP_KERNEL, 0))
 			break;
 	}
Signature · 1 line
2.18.2
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] mm, memcg: don't try to kill a process if memcg is not populated

Quoted 8 lines · Yafang Shao

Recently Shakeel reported a issue which also confused me serveral months earlier. Bellow is his report -

Lowering memory.max can trigger an oom-kill if the reclaim does not succeed. However if oom-killer does not find a process for killing, it dumps a lot of warnings.

Quoted 6 lines

Deleting a memcg does not reclaim memory from it and the memory can linger till there is a memory pressure. One normal way to proactively reclaim such memory is to set memory.max to 0 just before deleting the memcg. However if some of the memcg's memory is pinned by others, this operation can trigger an oom-kill without any process and thus can log a lot un-needed warnings. So, ignore all such warnings from memory.max.

lot *of* un-needed

Quoted 6 lines

[[email protected]: commit log above]

A better way to avoid this issue is to avoid trying to kill a process if memcg is not populated.
Note that OOM is different with OOM kill.

different *from*

Quoted 6 lines

OOM is a status that the
system or memcg is out of memory, while OOM kill is a result that a process inside this memcg is killed when this memcg is in OOM status. That is the same reason why there're both MEMCG_OOM event and MEMCG_OOM_KILL event. If we have already known that there's nothing to kill, i.e. the memcg is not populated, then we don't need to have a try.

need to try

I think adding the discussion of memory.high is also useful in the commit message. Basically why setting memory.max to 0 is better than setting memory.high to 0 before deletion. The reason is remote charging.

Quoted 7 lines
Cc Shakeel Butt <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Greg Thelen <[email protected]>
Quoted 22 lines
---
 mm/memcontrol.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/memcontrol.c +4 -1
index 985edce98491..29afe3df9d98 100644
@@ -6102,6 +6102,10 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
                }
 
                memcg_memory_event(memcg, MEMCG_OOM);
+
+               if (!cgroup_is_populated(memcg->css.cgroup))
+                       break;
+
                if (!mem_cgroup_oom_kill(memcg, GFP_KERNEL, 0))
                        break;
        }
--

2.18.2

#5 lore raw

Re: [PATCH 2/2] mm, memcg: don't try to kill a process if memcg is not populated

Quoted 10 lines · Shakeel Butt
Quoted 8 lines · Yafang Shao

Recently Shakeel reported a issue which also confused me serveral months earlier. Bellow is his report -

Lowering memory.max can trigger an oom-kill if the reclaim does not succeed. However if oom-killer does not find a process for killing, it dumps a lot of warnings.

Quoted 9 lines
Quoted 6 lines

Deleting a memcg does not reclaim memory from it and the memory can linger till there is a memory pressure. One normal way to proactively reclaim such memory is to set memory.max to 0 just before deleting the memcg. However if some of the memcg's memory is pinned by others, this operation can trigger an oom-kill without any process and thus can log a lot un-needed warnings. So, ignore all such warnings from memory.max.

lot *of* un-needed

Thanks.

Quoted 9 lines
Quoted 6 lines

[[email protected]: commit log above]

A better way to avoid this issue is to avoid trying to kill a process if memcg is not populated.
Note that OOM is different with OOM kill.

different *from*

Thanks

Quoted 9 lines
Quoted 6 lines

OOM is a status that the
system or memcg is out of memory, while OOM kill is a result that a process inside this memcg is killed when this memcg is in OOM status. That is the same reason why there're both MEMCG_OOM event and MEMCG_OOM_KILL event. If we have already known that there's nothing to kill, i.e. the memcg is not populated, then we don't need to have a try.

need to try

Thanks

Quoted 5 lines

I think adding the discussion of memory.high is also useful in the commit message. Basically why setting memory.max to 0 is better than setting memory.high to 0 before deletion. The reason is remote charging.

Sure, will add it.

Quoted 32 lines
Quoted 7 lines
Cc Shakeel Butt <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Greg Thelen <[email protected]>
Quoted 22 lines
---
 mm/memcontrol.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/memcontrol.c +4 -1
index 985edce98491..29afe3df9d98 100644
@@ -6102,6 +6102,10 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
                }
 
                memcg_memory_event(memcg, MEMCG_OOM);
+
+               if (!cgroup_is_populated(memcg->css.cgroup))
+                       break;
+
                if (!mem_cgroup_oom_kill(memcg, GFP_KERNEL, 0))
                        break;
        }
--

2.18.2

Signature · 2 lines
Thanks
Yafang