Skip to contentSkip to conversation

mm, memcg: avoid oom if cgroup is not populated

PATCH Superseded Yafang Shao 3 participants 8 messages Nov 26, 2019 – Nov 27, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm, memcg: avoid oom if cgroup is not populated

There's one case that the processes in a memcg are all exit (due to OOM group or some other reasons), but the file page caches are still exist. These file page caches may be protected by memory.min so can't be reclaimed. If we can't success to restart the processes in this memcg or don't want to make this memcg offline, then we want to drop the file page caches.
The advantage of droping this file caches is it can avoid the reclaimer (either kswapd or direct) scanning and reclaiming pages from all memcgs exist in this system, because currently the reclaimer will fairly reclaim pages from all memcgs if the system is under memory pressure. The possible method to drop these file page caches is setting the hard limit of this memcg to 0. Unfortunately this may invoke the OOM killer and generates lots of misleading outputs, that should not happen. One misleading output is "Out of memory and no killable processes...", while really there is no tasks rather than no killable tasks. Furthermore, the OOM output is not expected by the admin if he or she only wants to drop the cahes and knows there're no processes running in this memcg.

If memcg is not populated, we should not invoke the OOM killer.

Fixes b6e6edcf ("mm: memcontrol: reclaim and OOM kill when shrinking memory.max below usage")
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/memcontrol.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
mm/memcontrol.c +6 -2
index 1c4c08b..4e08905 100644
@@ -6139,9 +6139,13 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
 			continue;
 		}
 
-		memcg_memory_event(memcg, MEMCG_OOM);
-		if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
+		if (cgroup_is_populated(memcg->css.cgroup)) {
+			memcg_memory_event(memcg, MEMCG_OOM);
+			if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
+				break;
+		} else  {
 			break;
+		}
 	}
 
 	memcg_wb_domain_size_changed(memcg);
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 5 lines · Yafang Shao

There's one case that the processes in a memcg are all exit (due to OOM group or some other reasons), but the file page caches are still exist. These file page caches may be protected by memory.min so can't be reclaimed. If we can't success to restart the processes in this memcg or don't want to make this memcg offline, then we want to drop the file page

Quoted 8 lines

caches.
The advantage of droping this file caches is it can avoid the reclaimer (either kswapd or direct) scanning and reclaiming pages from all memcgs exist in this system, because currently the reclaimer will fairly reclaim pages from all memcgs if the system is under memory pressure. The possible method to drop these file page caches is setting the hard limit of this memcg to 0. Unfortunately this may invoke the OOM killer and generates lots of misleading outputs, that should not happen.

I disagree that the output is misleading. Quite contrary, it provides a useful lead on the unreclaimable memory.

Quoted 2 lines

One misleading output is "Out of memory and no killable processes...", while really there is no tasks rather than no killable tasks.

Again, this is nothing misleading. No task is a trivial subset of no killable task. I do not see why we should treat one differently than the other.

Quoted 3 lines

Furthermore,
the OOM output is not expected by the admin if he or she only wants to drop the cahes and knows there're no processes running in this memcg.

But this is not what hard limit reduced to 0 really does. No matter whether there is some task or not. It simply reclaims _all_ the memory as explained in other email.

Quoted 1 line

If memcg is not populated, we should not invoke the OOM killer.

I have already explained why I believe this is not correct in other email and this description doesn't provide any real justification. It is merely your intepretation of what should happen and I believe you haven't thought through it really.

Quoted 30 lines
Fixes b6e6edcf ("mm: memcontrol: reclaim and OOM kill when shrinking memory.max below usage")
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/memcontrol.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
mm/memcontrol.c +6 -2
index 1c4c08b..4e08905 100644
@@ -6139,9 +6139,13 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
 			continue;
 		}
 
-		memcg_memory_event(memcg, MEMCG_OOM);
-		if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
+		if (cgroup_is_populated(memcg->css.cgroup)) {
+			memcg_memory_event(memcg, MEMCG_OOM);
+			if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
+				break;
+		} else  {
 			break;
+		}
 	}
 
 	memcg_wb_domain_size_changed(memcg);
Signature · 1 line
1.8.3.1
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 16 lines · Michal Hocko
Quoted 5 lines · Yafang Shao

There's one case that the processes in a memcg are all exit (due to OOM group or some other reasons), but the file page caches are still exist. These file page caches may be protected by memory.min so can't be reclaimed. If we can't success to restart the processes in this memcg or don't want to make this memcg offline, then we want to drop the file page

Quoted 8 lines

caches.
The advantage of droping this file caches is it can avoid the reclaimer (either kswapd or direct) scanning and reclaiming pages from all memcgs exist in this system, because currently the reclaimer will fairly reclaim pages from all memcgs if the system is under memory pressure. The possible method to drop these file page caches is setting the hard limit of this memcg to 0. Unfortunately this may invoke the OOM killer and generates lots of misleading outputs, that should not happen.

Quoted 3 lines

I disagree that the output is misleading. Quite contrary, it provides a useful lead on the unreclaimable memory.

We can show the unreclaimable memory independently, rather than print the full oom output.
OOM killer is used to kill process, why do we invoke it when there's no process ?
What's the advantage of doing it ?

Quoted 7 lines
Quoted 2 lines

One misleading output is "Out of memory and no killable processes...", while really there is no tasks rather than no killable tasks.

Again, this is nothing misleading. No task is a trivial subset of no killable task. I do not see why we should treat one differently than the other.

No killable tasks means there's task and the OOM killer may be invoked. While no tasks means the OOM killer is useless.

Quoted 8 lines
Quoted 3 lines

Furthermore,
the OOM output is not expected by the admin if he or she only wants to drop the cahes and knows there're no processes running in this memcg.

But this is not what hard limit reduced to 0 really does. No matter whether there is some task or not. It simply reclaims _all_ the memory as explained in other email.

Are there any way to reclaim page cache only ?
No.

I know it will relcaim all the memory.
If you really think this expression is a prolem, but does it improtant that we should distingush between caches (both page caches and kmem) and _all_ memory, especially when there's no processes ?

Quoted 41 lines
Quoted 1 line

If memcg is not populated, we should not invoke the OOM killer.

I have already explained why I believe this is not correct in other email and this description doesn't provide any real justification. It is merely your intepretation of what should happen and I believe you haven't thought through it really.

Quoted 29 lines
Fixes b6e6edcf ("mm: memcontrol: reclaim and OOM kill when shrinking memory.max below usage")
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/memcontrol.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
mm/memcontrol.c +6 -3
index 1c4c08b..4e08905 100644
@@ -6139,9 +6139,13 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
                      continue;
              }
 
-             memcg_memory_event(memcg, MEMCG_OOM);
-             if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
+             if (cgroup_is_populated(memcg->css.cgroup)) {
+                     memcg_memory_event(memcg, MEMCG_OOM);
+                     if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
+                             break;
+             } else  {
                      break;
+             }
      }
 
      memcg_wb_domain_size_changed(memcg);
--
Quoted 1 line

1.8.3.1

--
Michal Hocko
SUSE Labs

maintainer
#3 lore raw
Quoted 21 lines · Yafang Shao
Quoted 16 lines · Michal Hocko
Quoted 5 lines · Yafang Shao

There's one case that the processes in a memcg are all exit (due to OOM group or some other reasons), but the file page caches are still exist. These file page caches may be protected by memory.min so can't be reclaimed. If we can't success to restart the processes in this memcg or don't want to make this memcg offline, then we want to drop the file page

Quoted 8 lines

caches.
The advantage of droping this file caches is it can avoid the reclaimer (either kswapd or direct) scanning and reclaiming pages from all memcgs exist in this system, because currently the reclaimer will fairly reclaim pages from all memcgs if the system is under memory pressure. The possible method to drop these file page caches is setting the hard limit of this memcg to 0. Unfortunately this may invoke the OOM killer and generates lots of misleading outputs, that should not happen.

Quoted 3 lines

I disagree that the output is misleading. Quite contrary, it provides a useful lead on the unreclaimable memory.

Quoted 5 lines

We can show the unreclaimable memory independently, rather than print the full oom output.
OOM killer is used to kill process, why do we invoke it when there's no process ?
What's the advantage of doing it ?

Consistency.

Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

One misleading output is "Out of memory and no killable processes...", while really there is no tasks rather than no killable tasks.

Again, this is nothing misleading. No task is a trivial subset of no killable task. I do not see why we should treat one differently than the other.

No killable tasks means there's task and the OOM killer may be invoked. While no tasks means the OOM killer is useless.

I disagree.

Quoted 11 lines
Quoted 8 lines
Quoted 3 lines

Furthermore,
the OOM output is not expected by the admin if he or she only wants to drop the cahes and knows there're no processes running in this memcg.

But this is not what hard limit reduced to 0 really does. No matter whether there is some task or not. It simply reclaims _all_ the memory as explained in other email.

Are there any way to reclaim page cache only ?
No.

Correct. And in absence of a solid usecase then I do not see a reason to add this. We have a global knob to achieve this and it has turned out to be abused and just used incorrectly most of the time.

Quoted 4 lines

I know it will relcaim all the memory.
If you really think this expression is a prolem, but does it improtant that we should distingush between caches (both page caches and kmem) and _all_ memory, especially when there's no processes ?

I do not think we should distinguish different memory types and treat them differently when applying hard limit.

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
Quoted 23 lines · Michal Hocko
Quoted 21 lines · Yafang Shao
Quoted 19 lines · Michal Hocko
Quoted 5 lines · Yafang Shao

There's one case that the processes in a memcg are all exit (due to OOM group or some other reasons), but the file page caches are still exist. These file page caches may be protected by memory.min so can't be reclaimed. If we can't success to restart the processes in this memcg or don't want to make this memcg offline, then we want to drop the file page

Quoted 8 lines

caches.
The advantage of droping this file caches is it can avoid the reclaimer (either kswapd or direct) scanning and reclaiming pages from all memcgs exist in this system, because currently the reclaimer will fairly reclaim pages from all memcgs if the system is under memory pressure. The possible method to drop these file page caches is setting the hard limit of this memcg to 0. Unfortunately this may invoke the OOM killer and generates lots of misleading outputs, that should not happen.

I disagree that the output is misleading. Quite contrary, it provides a useful lead on the unreclaimable memory.

Quoted 8 lines
Quoted 5 lines

We can show the unreclaimable memory independently, rather than print the full oom output.
OOM killer is used to kill process, why do we invoke it when there's no process ?
What's the advantage of doing it ?

Consistency.

If there are tasks, we invoke the OOM killer to try to kill the tasks. If there're no tasks, we just try to free the reclaimable pages.

Why do you think this is NOT Consistency?

Regarding the output, why should we distinguish the system OOM and memcg OOM, and why do you think this is Consistency ?

Quoted 29 lines
Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

One misleading output is "Out of memory and no killable processes...", while really there is no tasks rather than no killable tasks.

Again, this is nothing misleading. No task is a trivial subset of no killable task. I do not see why we should treat one differently than the other.

No killable tasks means there's task and the OOM killer may be invoked. While no tasks means the OOM killer is useless.

I disagree.

Quoted 11 lines
Quoted 8 lines
Quoted 3 lines

Furthermore,
the OOM output is not expected by the admin if he or she only wants to drop the cahes and knows there're no processes running in this memcg.

But this is not what hard limit reduced to 0 really does. No matter whether there is some task or not. It simply reclaims _all_ the memory as explained in other email.

Are there any way to reclaim page cache only ?
No.

Correct. And in absence of a solid usecase then I do not see a reason to add this. We have a global knob to achieve this and it has turned out to be abused and just used incorrectly most of the time.

Quoted 8 lines
Quoted 4 lines

I know it will relcaim all the memory.
If you really think this expression is a prolem, but does it improtant that we should distingush between caches (both page caches and kmem) and _all_ memory, especially when there's no processes ?

I do not think we should distinguish different memory types and treat them differently when applying hard limit.
--

It doesn't matter with different memory types.
It really matters with if there is no such memory that we don't need to waster our time to handle it.

Thanks
Yafang

maintainer
#5 lore raw
Quoted 30 lines · Yafang Shao
Quoted 29 lines · Michal Hocko
Quoted 26 lines · Yafang Shao
Quoted 16 lines · Michal Hocko
Quoted 5 lines · Yafang Shao

There's one case that the processes in a memcg are all exit (due to OOM group or some other reasons), but the file page caches are still exist. These file page caches may be protected by memory.min so can't be reclaimed. If we can't success to restart the processes in this memcg or don't want to make this memcg offline, then we want to drop the file page

Quoted 8 lines

caches.
The advantage of droping this file caches is it can avoid the reclaimer (either kswapd or direct) scanning and reclaiming pages from all memcgs exist in this system, because currently the reclaimer will fairly reclaim pages from all memcgs if the system is under memory pressure. The possible method to drop these file page caches is setting the hard limit of this memcg to 0. Unfortunately this may invoke the OOM killer and generates lots of misleading outputs, that should not happen.

Quoted 3 lines

I disagree that the output is misleading. Quite contrary, it provides a useful lead on the unreclaimable memory.

We can show the unreclaimable memory independently, rather than print the full oom output.
OOM killer is used to kill process, why do we invoke it when there's no process ?
What's the advantage of doing it ?

Quoted 5 lines
Quoted 2 lines

Consistency.

If there are tasks, we invoke the OOM killer to try to kill the tasks. If there're no tasks, we just try to free the reclaimable pages.

The fact that the oom killer has been invoked implies there is _no_ reclaimable memory. Full stop.

Anyway I am getting a bit tired to repeat myself so I am not continuing in this discussion. I have provided my feedback to your patch, please try to think harder about it. If you have real life usecases which cannot work properly with the existing functionality and APIs then bring them up.

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#6 lore raw
In reply to Yafang Shao
Quoted 5 lines · Yafang Shao

There's one case that the processes in a memcg are all exit (due to OOM group or some other reasons), but the file page caches are still exist. These file page caches may be protected by memory.min so can't be reclaimed. If we can't success to restart the processes in this memcg or don't want to make this memcg offline, then we want to drop the file page

Quoted 8 lines

caches.
The advantage of droping this file caches is it can avoid the reclaimer (either kswapd or direct) scanning and reclaiming pages from all memcgs exist in this system, because currently the reclaimer will fairly reclaim pages from all memcgs if the system is under memory pressure. The possible method to drop these file page caches is setting the hard limit of this memcg to 0. Unfortunately this may invoke the OOM killer and generates lots of misleading outputs, that should not happen.

You can set memory.high instead...?

#7 lore raw
Quoted 16 lines · Johannes Weiner
Quoted 5 lines · Yafang Shao

There's one case that the processes in a memcg are all exit (due to OOM group or some other reasons), but the file page caches are still exist. These file page caches may be protected by memory.min so can't be reclaimed. If we can't success to restart the processes in this memcg or don't want to make this memcg offline, then we want to drop the file page

Quoted 8 lines

caches.
The advantage of droping this file caches is it can avoid the reclaimer (either kswapd or direct) scanning and reclaiming pages from all memcgs exist in this system, because currently the reclaimer will fairly reclaim pages from all memcgs if the system is under memory pressure. The possible method to drop these file page caches is setting the hard limit of this memcg to 0. Unfortunately this may invoke the OOM killer and generates lots of misleading outputs, that should not happen.

Quoted 1 line

You can set memory.high instead...?

Well, I will take a look at memory.high and analye whether it is reliable.

Thanks
Yafang