cgroup-aware OOM killer
Subsystems (5)
PATCH v3 1/6 mm, oom: use oom_victims counter to synchronize oom victim selection
In reply to Roman GushchinOom killer should avoid unnecessary kills. To prevent them, during the tasks list traverse we check for task which was previously selected as oom victims. If there is such a task, new victim is not selected.
This approach is sub-optimal (we're doing costly iteration over the task list every time) and will not work for the cgroup-aware oom killer.
We already have oom_victims counter, which can be effectively used for the task.
If there are victims in flight, don't do anything; if the counter falls to 0, there are no more oom victims left.
So, it's a good time to start looking for a new victim.
--- mm/oom_kill.c | 7 +++++++ 1 file changed, 7 insertions(+)
mm/oom_kill.c +7 -0
@@ -992,6 +992,13 @@ bool out_of_memory(struct oom_control *oc)if (oom_killer_disabled)return false; +/* +* If there are oom victims in flight, we don't need to select +* a new victim. +*/ +if (atomic_read (&oom_victims) >0 ) +return true; +if (!is_memcg_oom (oc)) {blocking_notifier_call_chain (&oom_notify_list,0 , &freed);if (freed >0 )
Signature · 7 lines
2.7.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v3 2/6 mm, oom: cgroup-aware OOM killer
In reply to Roman GushchinTraditionally, the OOM killer is operating on a process level. Under oom conditions, it finds a process with the highest oom score and kills it.
This behavior doesn't suit well the system with many running containers. There are two main issues:
1) There is no fairness between containers. A small container with few large processes will be chosen over a large one with huge number of small processes.
2) Containers often do not expect that some random process inside will be killed. In many cases much more safer behavior is to kill all tasks in the container. Traditionally, this was implemented in userspace, but doing it in the kernel has some advantages, especially in a case of a system-wide OOM.
3) Per-process oom_score_adj affects global OOM, so it's a breache in the isolation.
To address these issues, cgroup-aware OOM killer is introduced.
Under OOM conditions, it tries to find the biggest memory consumer, and free memory by killing corresponding task(s). The difference the "traditional" OOM killer is that it can treat memory cgroups as memory consumers as well as single processes.
By default, it will look for the biggest leaf cgroup, and kill the largest task inside.
But a user can change this behavior by enabling the per-cgroup oom_kill_all_tasks option. If set, it causes the OOM killer treat the whole cgroup as an indivisible memory consumer. In case if it's selected as on OOM victim, all belonging tasks will be killed.
Tasks in the root cgroup are treated as independent memory consumers, and are compared with other memory consumers (e.g. leaf cgroups). The root cgroup doesn't support the oom_kill_all_tasks feature.
--- include/linux/memcontrol.h | 20 ++++++ include/linux/oom.h | 3 + mm/memcontrol.c | 155 ++++++++++++++++++++++++++++++++++++++++++ mm/oom_kill.c | 164 +++++++++++++++++++++++++++++---------------- 4 files changed, 285 insertions(+), 57 deletions(-)
include/linux/memcontrol.h +20 -0
@@ -35,6 +35,7 @@ struct mem_cgroup;struct page ;struct mm_struct ;struct kmem_cache ; +struct oom_control ;/* Cgroup-specific page state, on top of universal node page state */ enum memcg_stat_item {
@@ -199,6 +200,9 @@ struct mem_cgroup {/* OOM-Killer disable */ int oom_kill_disable; +/* kill all tasks in the subtree in case of OOM */ +bool oom_kill_all_tasks; +/* handle for "memory.events" */ struct cgroup_file events_file;
@@ -342,6 +346,11 @@ struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){ return css ? container_of(css, struct mem_cgroup, css) : NULL; } +static inline void mem_cgroup_put (struct mem_cgroup *memcg) +{ +css_put (&memcg->css); +} +#define mem_cgroup_from_counter (counter, member) \ container_of(counter, struct mem_cgroup, member)
@@ -480,6 +489,8 @@ static inline bool task_in_memcg_oom(struct task_struct *p)bool mem_cgroup_oom_synchronize (bool wait); +bool mem_cgroup_select_oom_victim (struct oom_control *oc); +#ifdef CONFIG_MEMCG_SWAP extern int do_swap_account;#endif
@@ -739,6 +750,10 @@ static inline bool task_in_mem_cgroup(struct task_struct *task,return true; } +static inline void mem_cgroup_put (struct mem_cgroup *memcg) +{ +} +static inline struct mem_cgroup * mem_cgroup_iter(struct mem_cgroup *root, struct mem_cgroup *prev,
@@ -926,6 +941,11 @@ static inlinevoid count_memcg_event_mm (struct mm_struct *mm,enum vm_event_item idx) { } + +static inline bool mem_cgroup_select_oom_victim (struct oom_control *oc) +{ +return false; +}#endif /* CONFIG_MEMCG */ static inline void __inc_memcg_state(struct mem_cgroup *memcg,
include/linux/oom.h +3 -0
@@ -39,6 +39,7 @@ struct oom_control {unsigned long totalpages;struct task_struct *chosen;unsigned long chosen_points; +struct mem_cgroup *chosen_memcg; };extern struct mutex oom_lock;
@@ -79,6 +80,8 @@ extern void oom_killer_enable(void);extern struct task_struct *find_lock_task_mm (struct task_struct *p); +extern int oom_evaluate_task (struct task_struct *task,void *arg); +/* sysctls */ extern int sysctl_oom_dump_tasks;extern int sysctl_oom_kill_allocating_task;
mm/memcontrol.c +155 -0
173 lines not on this page · raw message
mm/oom_kill.c +107 -57
196 lines not on this page · raw message
Signature · 7 lines
2.7.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v3 3/6 mm, oom: cgroup-aware OOM killer debug info
In reply to Roman GushchinDump the cgroup oom badness score, as well as the name of chosen victim cgroup.
Here how it looks like in dmesg:
[ 18.824495] Choosing a victim memcg because of the system-wide OOM [ 18.826911] Cgroup /A1: 200805 [ 18.827996] Cgroup /A2: 273072 [ 18.828937] Cgroup /A2/B3: 51 [ 18.829795] Cgroup /A2/B4: 272969 [ 18.830800] Cgroup /A2/B5: 52 [ 18.831890] Chosen cgroup /A2/B4: 272969
--- mm/memcontrol.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
mm/memcontrol.c +19 -1
@@ -2669,7 +2669,15 @@ bool mem_cgroup_select_oom_victim(struct oom_control *oc)if (!cgroup_subsys_on_dfl (memory_cgrp_subsys))return false; + +pr_info ("Choosing a victim memcg because of the %s" , + oc->memcg ? +"memory limit reached of cgroup " : +"system-wide OOM\n" );if (oc->memcg) { +pr_cont_cgroup_path (oc->memcg->css.cgroup); +pr_cont ("\n" ); + chosen_memcg = oc->memcg; parent = oc->memcg; }
@@ -2683,6 +2691,10 @@ bool mem_cgroup_select_oom_victim(struct oom_control *oc) points =mem_cgroup_oom_badness (iter, oc->nodemask); +pr_info ("Cgroup " ); +pr_cont_cgroup_path (iter->css.cgroup); +pr_cont (": %ld\n" , points); +if (points > chosen_memcg_points) { chosen_memcg = iter; chosen_memcg_points = points;
@@ -2731,6 +2743,10 @@ bool mem_cgroup_select_oom_victim(struct oom_control *oc) oc->chosen_memcg = chosen_memcg; } +pr_info ("Chosen cgroup " ); +pr_cont_cgroup_path (chosen_memcg->css.cgroup); +pr_cont (": %ld\n" , oc->chosen_points); +/* * Even if we have to kill all tasks in the cgroup, * we need to select the biggest task to start with.
@@ -2739,7 +2755,9 @@ bool mem_cgroup_select_oom_victim(struct oom_control *oc)*/ oc->chosen_points =0 ;mem_cgroup_scan_tasks (chosen_memcg, oom_evaluate_task, oc); - } + }else if (oc->chosen) +pr_info ("Chosen task %s (%d) in root cgroup: %ld\n" , + oc->chosen->comm, oc->chosen->pid, oc->chosen_points);rcu_read_unlock ();
Signature · 7 lines
2.7.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v3 4/6 mm, oom: introduce oom_score_adj for memory cgroups
In reply to Roman GushchinIntroduce a per-memory-cgroup oom_score_adj setting. A read-write single value file which exits on non-root cgroups. The default is "0".
It will have a similar meaning to a per-process value, available via /proc/<pid>/oom_score_adj. Should be in a range [-1000, 1000].
--- include/linux/memcontrol.h | 3 +++ mm/memcontrol.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+)
include/linux/memcontrol.h +3 -0
@@ -203,6 +203,9 @@ struct mem_cgroup {/* kill all tasks in the subtree in case of OOM */ bool oom_kill_all_tasks; +/* OOM kill score adjustment */ +short oom_score_adj; +/* handle for "memory.events" */ struct cgroup_file events_file;
mm/memcontrol.c +36 -0
@@ -5333,6 +5333,36 @@ static ssize_t memory_oom_kill_all_tasks_write(struct kernfs_open_file *of,return nbytes; } +static int memory_oom_score_adj_show (struct seq_file *m,void *v) +{ +struct mem_cgroup *memcg =mem_cgroup_from_css (seq_css (m)); +short oom_score_adj = memcg->oom_score_adj; + +seq_printf (m,"%d\n" , oom_score_adj); + +return 0 ; +} + +static ssize_t memory_oom_score_adj_write (struct kernfs_open_file *of, +char *buf,size_t nbytes,loff_t off) +{ +struct mem_cgroup *memcg =mem_cgroup_from_css (of_css (of)); +int oom_score_adj; +int err; + + err =kstrtoint (strstrip (buf),0 , &oom_score_adj); +if (err) +return err; + +if (oom_score_adj <OOM_SCORE_ADJ_MIN || + oom_score_adj >OOM_SCORE_ADJ_MAX ) +return -EINVAL ; + + memcg->oom_score_adj = (short )oom_score_adj; + +return nbytes; +} +static int memory_events_show (struct seq_file *m,void *v) {struct mem_cgroup *memcg =mem_cgroup_from_css (seq_css (m));
@@ -5459,6 +5489,12 @@ static struct cftype memory_files[] = { .write = memory_oom_kill_all_tasks_write, }, { + .name = "oom_score_adj", + .flags = CFTYPE_NOT_ON_ROOT, + .seq_show = memory_oom_score_adj_show, + .write = memory_oom_score_adj_write, + }, + { .name = "events", .flags = CFTYPE_NOT_ON_ROOT, .file_offset = offsetof(struct mem_cgroup, events_file),
Signature · 7 lines
2.7.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v3 5/6 mm, oom: don't mark all oom victims tasks with TIF_MEMDIE
In reply to Roman GushchinWe want to limit the number of tasks which are having an access to the memory reserves. To ensure the progress it's enough to have one such process at the time.
If we need to kill the whole cgroup, let's give an access to the memory reserves only to the first process in the list, which is (usually) the biggest process.
This will give us good chances that all other processes will be able to quit without an access to the memory reserves.
Otherwise, to keep going forward, let's grant the access to the memory reserves for tasks, which can't be reaped by the oom_reaper. As it will be done from the oom reaper thread, which handles the oom reaper queue consequently, there is no high risk to have too many such processes at the same time.
To implement this solution, we need to stop using TIF_MEMDIE flag as an universal marker for oom victims tasks. It's not a big issue, as we have oom_mm pointer/tsk_is_oom_victim(), which are just better.
--- kernel/exit.c | 2 +- mm/oom_kill.c | 31 ++++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 10 deletions(-)
kernel/exit.c +1 -1
@@ -554,7 +554,7 @@ static void exit_mm(void)task_unlock (current);mm_update_next_owner (mm);mmput (mm); -if (test_thread_flag (TIF_MEMDIE )) +if (tsk_is_oom_victim (current))exit_oom_victim (); }
mm/oom_kill.c +22 -9
@@ -556,8 +556,18 @@ static void oom_reap_task(struct task_struct *tsk)struct mm_struct *mm = tsk->signal->oom_mm;/* Retry the down_read_trylock(mmap_sem) a few times */ -while (attempts++ <MAX_OOM_REAP_RETRIES && !__oom_reap_task_mm (tsk, mm)) +while (attempts++ <MAX_OOM_REAP_RETRIES && + !__oom_reap_task_mm (tsk, mm)) { + +/* +* If the task has no access to the memory reserves, +* grant it to help the task to exit. +*/ +if (!test_tsk_thread_flag (tsk,TIF_MEMDIE )) +set_tsk_thread_flag (tsk,TIF_MEMDIE ); +schedule_timeout_idle (HZ /10 ); + }if (attempts <=MAX_OOM_REAP_RETRIES ) goto done;
@@ -647,16 +657,13 @@ static inline void wake_oom_reaper(struct task_struct *tsk)*/ static void mark_oom_victim (struct task_struct *tsk) { -struct mm_struct *mm = tsk->mm; -WARN_ON (oom_killer_disabled); -/* OOM killer might race with memcg OOM */ -if (test_and_set_tsk_thread_flag (tsk,TIF_MEMDIE )) -return ;/* oom_mm is bound to the signal struct life time. */ -if (!cmpxchg (&tsk->signal->oom_mm,NULL , mm)) -mmgrab (tsk->signal->oom_mm); +if (cmpxchg (&tsk->signal->oom_mm,NULL , tsk->mm) !=NULL ) +return ; + +mmgrab (tsk->signal->oom_mm);/* * Make sure that the task is woken up from uninterruptible sleep
@@ -665,7 +672,13 @@ static void mark_oom_victim(struct task_struct *tsk)* that TIF_MEMDIE tasks should be ignored. */ __thaw_task (tsk); -atomic_inc (&oom_victims); + +/* +* If there are no oom victims in flight, +* give the task an access to the memory reserves. +*/ +if (atomic_inc_return (&oom_victims) ==1 ) +set_tsk_thread_flag (tsk,TIF_MEMDIE ); }/**
Signature · 7 lines
2.7.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v3 6/6 mm,oom,docs: describe the cgroup-aware OOM killer
In reply to Roman GushchinUpdate cgroups v2 docs.
--- Documentation/cgroup-v2.txt | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
Documentation/cgroup-v2.txt +44 -0
@@ -44,6 +44,7 @@ CONTENTS 5-2-1. Memory Interface Files 5-2-2. Usage Guidelines 5-2-3. Memory Ownership + 5-2-4. Cgroup-aware OOM Killer 5-3. IO 5-3-1. IO Interface Files 5-3-2. Writeback
@@ -799,6 +800,26 @@ PAGE_SIZE multiple when read back. high limit is used and monitored properly, this limit's utility is limited to providing the final safety net. + memory.oom_kill_all_tasks + + A read-write single value file which exits on non-root + cgroups. The default is "0". + + Defines whether the OOM killer should treat the cgroup + as a single entity during the victim selection. + + If set, it will cause the OOM killer to kill all belonging + tasks, both in case of a system-wide or cgroup-wide OOM. + + memory.oom_score_adj + + A read-write single value file which exits on non-root + cgroups. The default is "0". + + OOM killer score adjustment, which has as similar meaning + to a per-process value, available via /proc/<pid>/oom_score_adj. + Should be in a range [-1000, 1000]. + memory.events A read-only flat-keyed file which exists on non-root cgroups.
@@ -1028,6 +1049,29 @@ POSIX_FADV_DONTNEED to relinquish the ownership of memory areas belonging to the affected files to ensure correct memory ownership. +5-2-4. Cgroup-aware OOM Killer + +Cgroup v2 memory controller implements a cgroup-aware OOM killer. +It means that it treats memory cgroups as first class OOM entities. + +Under OOM conditions the memory controller tries to make the best +choise of a victim, hierarchically looking for the largest memory +consumer. By default, it will look for the biggest task in the +biggest leaf cgroup. + +But a user can change this behavior by enabling the per-cgroup +oom_kill_all_tasks option. If set, it causes the OOM killer treat +the whole cgroup as an indivisible memory consumer. In case if it's +selected as on OOM victim, all belonging tasks will be killed. + +Tasks in the root cgroup are treated as independent memory consumers, +and are compared with other memory consumers (e.g. leaf cgroups). +The root cgroup doesn't support the oom_kill_all_tasks feature. + +This affects both system- and cgroup-wide OOMs. For a cgroup-wide OOM +the memory controller considers only cgroups belonging to the sub-tree +of the OOM'ing cgroup. + 5-3. IO The "io" controller regulates the distribution of IO resources. This
Signature · 7 lines
2.7.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
v3 cover cgroup-aware OOM killer
This patchset makes the OOM killer cgroup-aware.
Patch 1 causes out_of_memory() look at the oom_victim counter
to decide if a new victim is required.
Patch 2 is main patch which implements cgroup-aware OOM killer.
Patch 3 adds some debug output, which can be refined later.
Patch 4 introduces per-cgroup oom_score_adj knob.
Patch 5 fixes a problem with too many processes receiving an
access to the memory reserves.
Patch 6 is docs update.
v1:
https://lkml.org/lkml/2017/5/18/969
v2:
- Reworked victim selection based on feedback
from Michal Hocko, Vladimir Davydov and Johannes Weiner
- "Kill all tasks" is now an opt-in option, by default only one process will be killed
- Added per-cgroup oom_score_adj
- Refined oom score calculations, suggested by Vladimir Davydov
- Converted to a patchset
v3:
- Fixed swap accounting
- Switched to use oom_victims counter to prevent unnecessary kills
- TIF_MEMDIE is set only when necessary
- Moved all oom victim killing code into oom_kill.c
- Merged commits 1-4 into 6
- Separated oom_score_adj logic into a separate commit 4
- Separated debug output into a separate commit 3
Roman Gushchin (6):
mm, oom: use oom_victims counter to synchronize oom victim selection mm, oom: cgroup-aware OOM killer
mm, oom: cgroup-aware OOM killer debug info
mm, oom: introduce oom_score_adj for memory cgroups
mm, oom: don't mark all oom victims tasks with TIF_MEMDIE mm,oom,docs: describe the cgroup-aware OOM killer
Documentation/cgroup-v2.txt | 44 ++++++++++ include/linux/memcontrol.h | 23 +++++ include/linux/oom.h | 3 + kernel/exit.c | 2 +- mm/memcontrol.c | 209 ++++++++++++++++++++++++++++++++++++++++++++ mm/oom_kill.c | 202 ++++++++++++++++++++++++++++-------------- 6 files changed, 416 insertions(+), 67 deletions(-)
Signature · 7 lines
2.7.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [v3 1/6] mm, oom: use oom_victims counter to synchronize oom victim selection
In reply to a message not in the archiveQuoted 36 lines · Tetsuo Handa
Quoted 6 lines · Roman Gushchin
mm/oom_kill.c +0
-0@@ -992,6 +992,13 @@ bool out_of_memory(struct oom_control *oc)if (oom_killer_disabled)return false;mm/oom_kill.c bool out_of_memory(struct oom_control *oc) · line 1004
Quoted 10 lines
mm/oom_kill.c +7
-0+/* +* If there are oom victims in flight, we don't need to select +* a new victim. +*/ +if (atomic_read (&oom_victims) >0 ) +return true; +if (!is_memcg_oom (oc)) {blocking_notifier_call_chain (&oom_notify_list,0 , &freed);if (freed >0 )Above in this patch and below in patch 5 are wrong.
Quoted 16 lines
mm/oom_kill.c +7
-1@@ -665,7 +672,13 @@ static void mark_oom_victim(struct task_struct *tsk)* that TIF_MEMDIE tasks should be ignored. */ __thaw_task (tsk); -atomic_inc (&oom_victims); + +/* +* If there are no oom victims in flight, +* give the task an access to the memory reserves. +*/ +if (atomic_inc_return (&oom_victims) ==1 ) +set_tsk_thread_flag (tsk,TIF_MEMDIE ); }
Quoted 9 lines
mm/oom_kill.c static void mark_oom_victim(struct task_struct *tsk) · line 684
Quoted 1 line
mm/oom_kill.c +0
-0/** The OOM reaper is not available for
CONFIG_MMU=nkernels, and timeout based giveup is not permitted, but a multithreaded process might be selected as an OOM victim. Not settingTIF_MEMDIEto all threads sharing an OOM victim's mm increases possibility of preventing some OOM victim thread from terminating (e.g. one of them cannot leave__alloc_pages_slowpath()withmmap_semheld for write due to waiting for theTIF_MEMDIEthread to callexit_oom_victim()when theTIF_MEMDIEthread is waiting for the thread withmmap_semheld for write).
I agree, that CONFIG_MMU=n is a special case, and the proposed approach can't be used directly. But can you, please, why do you find the first chunk wrong? Basically, it's exactly what we do now: we increment oom_victims for every oom victim, and every process decrements this counter during it's exit path. If there is at least one existing victim, we will select it again, so it's just an optimization. Am I missing something? Why should we start new victim selection if there processes that will likely quit and release memory soon?
Thanks!
Re: [v3 1/6] mm, oom: use oom_victims counter to synchronize oom victim selection
Quoted 47 lines · Roman Gushchin
Quoted 36 lines · Tetsuo Handa
Quoted 6 lines · Roman Gushchin
mm/oom_kill.c +0
-0@@ -992,6 +992,13 @@ bool out_of_memory(struct oom_control *oc)if (oom_killer_disabled)return false;mm/oom_kill.c bool out_of_memory(struct oom_control *oc) · line 1004
Quoted 10 lines
mm/oom_kill.c +7
-0+/* +* If there are oom victims in flight, we don't need to select +* a new victim. +*/ +if (atomic_read (&oom_victims) >0 ) +return true; +if (!is_memcg_oom (oc)) {blocking_notifier_call_chain (&oom_notify_list,0 , &freed);if (freed >0 )Above in this patch and below in patch 5 are wrong.
Quoted 16 lines
mm/oom_kill.c +7
-1@@ -665,7 +672,13 @@ static void mark_oom_victim(struct task_struct *tsk)* that TIF_MEMDIE tasks should be ignored. */ __thaw_task (tsk); -atomic_inc (&oom_victims); + +/* +* If there are no oom victims in flight, +* give the task an access to the memory reserves. +*/ +if (atomic_inc_return (&oom_victims) ==1 ) +set_tsk_thread_flag (tsk,TIF_MEMDIE ); }Quoted 9 lines
mm/oom_kill.c static void mark_oom_victim(struct task_struct *tsk) · line 684
Quoted 1 line
mm/oom_kill.c +0
-0/** The OOM reaper is not available for
CONFIG_MMU=nkernels, and timeout based giveup is not permitted, but a multithreaded process might be selected as an OOM victim. Not settingTIF_MEMDIEto all threads sharing an OOM victim's mm increases possibility of preventing some OOM victim thread from terminating (e.g. one of them cannot leave__alloc_pages_slowpath()withmmap_semheld for write due to waiting for theTIF_MEMDIEthread to callexit_oom_victim()when theTIF_MEMDIEthread is waiting for the thread withmmap_semheld for write).
Quoted 2 lines
I agree, that
CONFIG_MMU=nis a special case, and the proposed approach can't be used directly. But can you, please, why do you find the first chunk wrong?
Since you are checking oom_victims before checking task_will_free_mem(current), only one thread can get TIF_MEMDIE. This is where a multithreaded OOM victim without the OOM reaper can get stuck forever.
Quoted 5 lines
Basically, it's exactly what we do now: we increment
oom_victimsfor every oom victim, and every process decrements this counter during it's exit path. If there is at least one existing victim, we will select it again, so it's just an optimization. Am I missing something? Why should we start new victim selection if there processes that will likely quit and release memory soon?
If you check oom_victims like
http://lkml.kernel.org/r/[email protected] does, all threads in a multithreaded OOM victim will have a chance to get TIF_MEMDIE.
By the way, below in patch 5 is also wrong.
You are not permitted to set TIF_MEMDIE if oom_killer_disabled == true.
mm/oom_kill.c +11 -1
@@ -556,8 +556,18 @@ static void oom_reap_task(struct task_struct *tsk)struct mm_struct *mm = tsk->signal->oom_mm;/* Retry the down_read_trylock(mmap_sem) a few times */ -while (attempts++ <MAX_OOM_REAP_RETRIES && !__oom_reap_task_mm (tsk, mm)) +while (attempts++ <MAX_OOM_REAP_RETRIES && + !__oom_reap_task_mm (tsk, mm)) { + +/* +* If the task has no access to the memory reserves, +* grant it to help the task to exit. +*/ +if (!test_tsk_thread_flag (tsk,TIF_MEMDIE )) +set_tsk_thread_flag (tsk,TIF_MEMDIE ); +schedule_timeout_idle (HZ /10 ); + }if (attempts <=MAX_OOM_REAP_RETRIES ) goto done;
Re: [v3 1/6] mm, oom: use oom_victims counter to synchronize oom victim selection
Quoted 28 lines · Tetsuo Handa
Quoted 27 lines · Roman Gushchin
Quoted 25 lines · Tetsuo Handa
Quoted 6 lines · Roman Gushchin
mm/oom_kill.c +0
-0@@ -992,6 +992,13 @@ bool out_of_memory(struct oom_control *oc)if (oom_killer_disabled)return false;mm/oom_kill.c bool out_of_memory(struct oom_control *oc) · line 1004
Quoted 10 lines
mm/oom_kill.c +7
-0+/* +* If there are oom victims in flight, we don't need to select +* a new victim. +*/ +if (atomic_read (&oom_victims) >0 ) +return true; +if (!is_memcg_oom (oc)) {blocking_notifier_call_chain (&oom_notify_list,0 , &freed);if (freed >0 )The OOM reaper is not available for
CONFIG_MMU=nkernels, and timeout based giveup is not permitted, but a multithreaded process might be selected as an OOM victim. Not settingTIF_MEMDIEto all threads sharing an OOM victim's mm increases possibility of preventing some OOM victim thread from terminating (e.g. one of them cannot leave__alloc_pages_slowpath()withmmap_semheld for write due to waiting for theTIF_MEMDIEthread to callexit_oom_victim()when theTIF_MEMDIEthread is waiting for the thread withmmap_semheld for write).
Quoted 6 lines
Quoted 2 lines
I agree, that
CONFIG_MMU=nis a special case, and the proposed approach can't be used directly. But can you, please, why do you find the first chunk wrong?Since you are checking
oom_victimsbefore checkingtask_will_free_mem(current), only one thread can getTIF_MEMDIE. This is where a multithreaded OOM victim without the OOM reaper can get stuck forever.
Oops, I misinterpreted. This is where a multithreaded OOM victim with or without the OOM reaper can get stuck forever. Think about a process with two threads is selected by the OOM killer and only one of these two threads can get TIF_MEMDIE.
Thread-1 Thread-2 The OOM killer The OOM reaper
Calls down_write(¤t->mm->mmap_sem).
Enters __alloc_pages_slowpath().
Enters __alloc_pages_slowpath().
Takes oom_lock.
Calls out_of_memory().
Selects Thread-1 as an OOM victim.
Gets SIGKILL. Gets SIGKILL.
Gets TIF_MEMDIE.
Releases oom_lock.
Leaves __alloc_pages_slowpath() because Thread-1 has TIF_MEMDIE.
Takes oom_lock.
Will do nothing because down_read_trylock() fails. Releases oom_lock.
Gives up and sets MMF_OOM_SKIP after one second.
Takes oom_lock.
Calls out_of_memory().
Will not check MMF_OOM_SKIP because Thread-1 still has TIF_MEMDIE. // <= get stuck waiting for Thread-1. Releases oom_lock.
Will not leave __alloc_pages_slowpath() because Thread-2 does not have TIF_MEMDIE. Will not call up_write(¤t->mm->mmap_sem).
Reaches do_exit().
Calls down_read(¤t->mm->mmap_sem) in exit_mm() in do_exit(). // <= get stuck waiting for Thread-2. Will not call up_read(¤t->mm->mmap_sem) in exit_mm() in do_exit().
Will not clear TIF_MEMDIE in exit_oom_victim() in exit_mm() in do_exit().
Michal Hocko
maintainerRe: [v3 5/6] mm, oom: don't mark all oom victims tasks with TIF_MEMDIE
In reply to Roman GushchinQuoted 9 lines · Roman Gushchin
We want to limit the number of tasks which are having an access to the memory reserves. To ensure the progress it's enough to have one such process at the time.
If we need to kill the whole cgroup, let's give an access to the memory reserves only to the first process in the list, which is (usually) the biggest process.
This will give us good chances that all other processes will be able to quit without an access to the memory reserves.
I don't like this to be honest. Is there any reason to go the reduced memory reserves access to oom victims I was suggesting earlier [1]?
[1] http://lkml.kernel.org/r/http://lkml.kernel.org/r/[email protected]
Signature · 8 lines
Michal Hocko SUSE Labs -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Michal Hocko
maintainerRe: [v3 1/6] mm, oom: use oom_victims counter to synchronize oom victim selection
In reply to Roman GushchinQuoted 10 lines · Roman Gushchin
Oom killer should avoid unnecessary kills. To prevent them, during the tasks list traverse we check for task which was previously selected as oom victims. If there is such a task, new victim is not selected.
This approach is sub-optimal (we're doing costly iteration over the task list every time) and will not work for the cgroup-aware oom killer.
We already have
oom_victimscounter, which can be effectively used for the task.
A global counter will not work properly, I am afraid. a) you should consider the oom domain and do not block oom on unrelated domains and b) you have no guarantee that the oom victim will terminate reasonably. That is why we have MMF_OOM_SKIP check in oom_evaluate_task.
I think you should have something similar for your memcg victim selection. If you see a memcg in the oom hierarchy with oom victims which are alive and not MMF_OOM_SKIP, you should abort the scanning.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [v3 5/6] mm, oom: don't mark all oom victims tasks with TIF_MEMDIE
In reply to Michal HockoQuoted 11 lines · Michal Hocko
Quoted 9 lines · Roman Gushchin
We want to limit the number of tasks which are having an access to the memory reserves. To ensure the progress it's enough to have one such process at the time.
If we need to kill the whole cgroup, let's give an access to the memory reserves only to the first process in the list, which is (usually) the biggest process.
This will give us good chances that all other processes will be able to quit without an access to the memory reserves.
Quoted 4 lines
I don't like this to be honest. Is there any reason to go the reduced memory reserves access to oom victims I was suggesting earlier [1]?
[1] http://lkml.kernel.org/r/http://lkml.kernel.org/r/[email protected]
I've nothing against your approach. What's the state of this patchset? Do you plan to bring it upstream?
Roman
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [v3 1/6] mm, oom: use oom_victims counter to synchronize oom victim selection
In reply to Tetsuo HandaQuoted 58 lines · Tetsuo Handa
Quoted 28 lines · Tetsuo Handa
Quoted 27 lines · Roman Gushchin
Quoted 25 lines · Tetsuo Handa
Quoted 6 lines · Roman Gushchin
mm/oom_kill.c +0
-0@@ -992,6 +992,13 @@ bool out_of_memory(struct oom_control *oc)if (oom_killer_disabled)return false;mm/oom_kill.c bool out_of_memory(struct oom_control *oc) · line 1004
Quoted 10 lines
mm/oom_kill.c +7
-0+/* +* If there are oom victims in flight, we don't need to select +* a new victim. +*/ +if (atomic_read (&oom_victims) >0 ) +return true; +if (!is_memcg_oom (oc)) {blocking_notifier_call_chain (&oom_notify_list,0 , &freed);if (freed >0 )The OOM reaper is not available for
CONFIG_MMU=nkernels, and timeout based giveup is not permitted, but a multithreaded process might be selected as an OOM victim. Not settingTIF_MEMDIEto all threads sharing an OOM victim's mm increases possibility of preventing some OOM victim thread from terminating (e.g. one of them cannot leave__alloc_pages_slowpath()withmmap_semheld for write due to waiting for theTIF_MEMDIEthread to callexit_oom_victim()when theTIF_MEMDIEthread is waiting for the thread withmmap_semheld for write).Quoted 6 lines
Quoted 2 lines
I agree, that
CONFIG_MMU=nis a special case, and the proposed approach can't be used directly. But can you, please, why do you find the first chunk wrong?Since you are checking
oom_victimsbefore checkingtask_will_free_mem(current), only one thread can getTIF_MEMDIE. This is where a multithreaded OOM victim without the OOM reaper can get stuck forever.Oops, I misinterpreted. This is where a multithreaded OOM victim with or without the OOM reaper can get stuck forever. Think about a process with two threads is selected by the OOM killer and only one of these two threads can get
TIF_MEMDIE.Thread-1 Thread-2 The OOM killer The OOM reaper
Calls down_write(¤t->mm->mmap_sem).Enters
__alloc_pages_slowpath().
Enters__alloc_pages_slowpath().
Takesoom_lock.
Callsout_of_memory().
Selects Thread-1 as an OOM victim.
Gets SIGKILL. Gets SIGKILL.
GetsTIF_MEMDIE.
Releasesoom_lock.
Leaves__alloc_pages_slowpath()because Thread-1 hasTIF_MEMDIE.
Takesoom_lock.
Will do nothing becausedown_read_trylock()fails. Releasesoom_lock.
Gives up and setsMMF_OOM_SKIPafter one second.
Takesoom_lock.
Callsout_of_memory().
Quoted 8 lines
Will not check
MMF_OOM_SKIPbecause Thread-1 still hasTIF_MEMDIE. // <= get stuck waiting for Thread-1. Releasesoom_lock.
Will not leave__alloc_pages_slowpath()because Thread-2 does not haveTIF_MEMDIE. Will not callup_write(¤t->mm->mmap_sem).
Reachesdo_exit().
Callsdown_read(¤t->mm->mmap_sem)inexit_mm()indo_exit(). // <= get stuck waiting for Thread-2. Will not callup_read(¤t->mm->mmap_sem)inexit_mm()indo_exit().
Will not clearTIF_MEMDIEinexit_oom_victim()inexit_mm()indo_exit().
That's interesting... Does it mean, that we have to give an access to the reserves to all threads to guarantee the forward progress?
What do you think about Michal's approach? He posted a link in the thread.
Thank you!
Roman
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [v3 1/6] mm, oom: use oom_victims counter to synchronize oom victim selection
Quoted 33 lines · Roman Gushchin
Quoted 23 lines · Tetsuo Handa
Tetsuo Handa wrote:
Oops, I misinterpreted. This is where a multithreaded OOM victim with or without the OOM reaper can get stuck forever. Think about a process with two threads is selected by the OOM killer and only one of these two threads can getTIF_MEMDIE.Thread-1 Thread-2 The OOM killer The OOM reaper
Calls down_write(¤t->mm->mmap_sem).Enters
__alloc_pages_slowpath().
Enters__alloc_pages_slowpath().
Takesoom_lock.
Callsout_of_memory().
Selects Thread-1 as an OOM victim.
Gets SIGKILL. Gets SIGKILL.
GetsTIF_MEMDIE.
Releasesoom_lock.
Leaves__alloc_pages_slowpath()because Thread-1 hasTIF_MEMDIE.
Takesoom_lock.
Will do nothing becausedown_read_trylock()fails. Releasesoom_lock.
Gives up and setsMMF_OOM_SKIPafter one second.
Takesoom_lock.
Callsout_of_memory().Quoted 8 lines
Will not check
MMF_OOM_SKIPbecause Thread-1 still hasTIF_MEMDIE. // <= get stuck waiting for Thread-1. Releasesoom_lock.
Will not leave__alloc_pages_slowpath()because Thread-2 does not haveTIF_MEMDIE. Will not callup_write(¤t->mm->mmap_sem).
Reachesdo_exit().
Callsdown_read(¤t->mm->mmap_sem)inexit_mm()indo_exit(). // <= get stuck waiting for Thread-2. Will not callup_read(¤t->mm->mmap_sem)inexit_mm()indo_exit().
Will not clearTIF_MEMDIEinexit_oom_victim()inexit_mm()indo_exit().
Quoted 2 lines
That's interesting... Does it mean, that we have to give an access to the reserves to all threads to guarantee the forward progress?
Yes, for we don't have __GFP_KILLABLE flag.
Quoted 2 lines
What do you think about Michal's approach? He posted a link in the thread.
Please read that thread.
Michal Hocko
maintainerRe: [v3 5/6] mm, oom: don't mark all oom victims tasks with TIF_MEMDIE
In reply to Roman GushchinQuoted 12 lines · Roman Gushchin
Quoted 11 lines · Michal Hocko
Quoted 9 lines · Roman Gushchin
We want to limit the number of tasks which are having an access to the memory reserves. To ensure the progress it's enough to have one such process at the time.
If we need to kill the whole cgroup, let's give an access to the memory reserves only to the first process in the list, which is (usually) the biggest process.
This will give us good chances that all other processes will be able to quit without an access to the memory reserves.
Quoted 7 lines
Quoted 4 lines
I don't like this to be honest. Is there any reason to go the reduced memory reserves access to oom victims I was suggesting earlier [1]?
[1] http://lkml.kernel.org/r/http://lkml.kernel.org/r/[email protected]
I've nothing against your approach. What's the state of this patchset? Do you plan to bring it upstream?
Just the specific patch I have linked should be sufficient for what you need here. The patchset had some issues which I didn't have time to fix and as such the need for the above patch was not a high priority as well.
Signature · 8 lines
Michal Hocko SUSE Labs -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
David Rientjes
reviewerRe: [v3 2/6] mm, oom: cgroup-aware OOM killer
In reply to Roman GushchinQuoted 11 lines · Roman Gushchin
Traditionally, the OOM killer is operating on a process level. Under oom conditions, it finds a process with the highest oom score and kills it.
This behavior doesn't suit well the system with many running containers. There are two main issues:
1) There is no fairness between containers. A small container with few large processes will be chosen over a large one with huge number of small processes.
Yes, the original motivation was to limit killing to a single process, if possible. To do that, we kill the process with the largest rss to free the most memory and rely on the user to configure /proc/pid/oom_score_adj if something else should be prioritized.
With containerization and overcommit of system memory, we concur that killing the single largest process isn't always preferable and neglects the priority of its memcg. Your motivation seems to be to provide fairness between one memcg with a large process and one memcg with a large number of small processes; I'm curious if you are concerned about the priority of a memcg hierarchy (how important that "job" is) or whether you are strictly concerned with "largeness" of memcgs relative to each other.
Quoted 6 lines
2) Containers often do not expect that some random process inside will be killed. In many cases much more safer behavior is to kill all tasks in the container. Traditionally, this was implemented in userspace, but doing it in the kernel has some advantages, especially in a case of a system-wide OOM.
We agree.
Quoted 3 lines
3) Per-process
oom_score_adjaffects global OOM, so it's a breache in the isolation.
This should only be a consequence of overcommiting memcgs at the top level so the system oom killer is actually ever invoked, otherwise per-process oom_score_adj works well for memcg oom killing.
Quoted 7 lines
To address these issues, cgroup-aware OOM killer is introduced.
Under OOM conditions, it tries to find the biggest memory consumer, and free memory by killing corresponding task(s). The difference the "traditional" OOM killer is that it can treat memory cgroups as memory consumers as well as single processes.
Quoted 8 lines
By default, it will look for the biggest leaf cgroup, and kill the largest task inside.
But a user can change this behavior by enabling the per-cgroup
oom_kill_all_tasksoption. If set, it causes the OOM killer treat the whole cgroup as an indivisible memory consumer. In case if it's selected as on OOM victim, all belonging tasks will be killed.
These are two different things, right? We can adjust how the system oom killer chooses victims when memcg hierarchies overcommit the system to not strictly prefer the single process with the largest rss without killing everything attached to the memcg.
Separately: do you not intend to support memcg priorities at all, but rather strictly consider the "largeness" of a memcg versus other memcgs?
In our methodology, each memcg is assigned a priority value and the iteration of the hierarchy simply compares and visits the memcg with the lowest priority at each level and then selects the largest process to kill. This could also support a "kill-all" knob.
struct mem_cgroup *memcg = root_mem_cgroup; struct mem_cgroup *low_memcg; unsigned long low_priority;
next:
low_memcg = NULL;
low_priority = ULONG_MAX;
for_each_child_of_memcg(memcg) {
unsigned long prio = memcg_oom_priority(memcg);
if (prio < low_priority) {
low_memcg = memcg;
low_priority = prio;
}
}
if (low_memcg)
goto next;
oom_kill_process_from_memcg(memcg);So this is a priority based model that is different than your aggregate usage model but I think it allows userspace to define a more powerful policy. We certainly may want to kill from a memcg with a single large process, or we may want to kill from a memcg with several small processes, it depends on the importance of that job.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [v3 2/6] mm, oom: cgroup-aware OOM killer
Quoted 19 lines · David Rientjes
Quoted 11 lines · Roman Gushchin
Traditionally, the OOM killer is operating on a process level. Under oom conditions, it finds a process with the highest oom score and kills it.
This behavior doesn't suit well the system with many running containers. There are two main issues:
1) There is no fairness between containers. A small container with few large processes will be chosen over a large one with huge number of small processes.
Yes, the original motivation was to limit killing to a single process, if possible. To do that, we kill the process with the largest rss to free the most memory and rely on the user to configure
/proc/pid/oom_score_adjif something else should be prioritized.
Quoted 7 lines
With containerization and overcommit of system memory, we concur that killing the single largest process isn't always preferable and neglects the priority of its memcg. Your motivation seems to be to provide fairness between one memcg with a large process and one memcg with a large number of small processes; I'm curious if you are concerned about the priority of a memcg hierarchy (how important that "job" is) or whether you are strictly concerned with "largeness" of memcgs relative to each other.
I'm pretty sure we should provide some way to prioritize some cgroups over other (in terms of oom killer preferences), but I'm not 100% sure yet, what's the best way to do it. I've suggested something similar to the existing oom_score_adj for tasks, mostly to folow the existing design.
One of the questions to answer in priority-based model is how to compare tasks in the root cgroup with cgroups?
Quoted 10 lines
Quoted 9 lines
...
By default, it will look for the biggest leaf cgroup, and kill the largest task inside.But a user can change this behavior by enabling the per-cgroup
oom_kill_all_tasksoption. If set, it causes the OOM killer treat the whole cgroup as an indivisible memory consumer. In case if it's selected as on OOM victim, all belonging tasks will be killed.
Quoted 4 lines
These are two different things, right? We can adjust how the system oom killer chooses victims when memcg hierarchies overcommit the system to not strictly prefer the single process with the largest rss without killing everything attached to the memcg.
They are different, and I thought about providing two independent knobs. But after all I haven't found enough real life examples, where it can be useful. Can you provide something here?
Also, they are different only for non-leaf cgroups; leaf cgroups are always treated as indivisible memory consumers during victim selection.
I assume, that containerized systems will always set oom_kill_all_tasks for top-level container memory cgroups. By default it's turned off to provide backward compatibility with current behavior and avoid excessive kills and support oom_score_adj==-1000 (I've added this to v4, will post soon).
Quoted 2 lines
Separately: do you not intend to support memcg priorities at all, but rather strictly consider the "largeness" of a memcg versus other memcgs?
Answered upper.
Quoted 24 lines
In our methodology, each memcg is assigned a priority value and the iteration of the hierarchy simply compares and visits the memcg with the lowest priority at each level and then selects the largest process to kill. This could also support a "kill-all" knob.
struct mem_cgroup *memcg = root_mem_cgroup; struct mem_cgroup *low_memcg; unsigned long low_priority;next:
low_memcg = NULL; low_priority = ULONG_MAX; for_each_child_of_memcg(memcg) { unsigned long prio = memcg_oom_priority(memcg); if (prio < low_priority) { low_memcg = memcg; low_priority = prio; } } if (low_memcg) goto next; oom_kill_process_from_memcg(memcg);
Quoted 5 lines
So this is a priority based model that is different than your aggregate usage model but I think it allows userspace to define a more powerful policy. We certainly may want to kill from a memcg with a single large process, or we may want to kill from a memcg with several small processes, it depends on the importance of that job.
I believe, that both models have some advantages.
Priority-based model is more powerful, but requires support from the userspace to set up these priorities (and, probably, adjust them dynamically). Size-based model is limited, but provides reasonable behavior without any additional configuration.
I will agree here with Michal Hocko, that bpf like mechanism can be used here to provide an option for writing some custom oom policies. After we will have necessary infrastructure to iterate over cgroup tree, select a cgroup with largest oom score, and kill a biggest task/all the tasks there, we can add an ability to customize the cgroup (and maybe tasks) evaluation.
Thanks!
Roman
David Rientjes
reviewerRe: [v3 2/6] mm, oom: cgroup-aware OOM killer
Quoted 13 lines · Roman Gushchin
Quoted 5 lines
Yes, the original motivation was to limit killing to a single process, if possible. To do that, we kill the process with the largest rss to free the most memory and rely on the user to configure
/proc/pid/oom_score_adjif something else should be prioritized.Quoted 7 lines
With containerization and overcommit of system memory, we concur that killing the single largest process isn't always preferable and neglects the priority of its memcg. Your motivation seems to be to provide fairness between one memcg with a large process and one memcg with a large number of small processes; I'm curious if you are concerned about the priority of a memcg hierarchy (how important that "job" is) or whether you are strictly concerned with "largeness" of memcgs relative to each other.
Quoted 8 lines
I'm pretty sure we should provide some way to prioritize some cgroups over other (in terms of oom killer preferences), but I'm not 100% sure yet, what's the best way to do it. I've suggested something similar to the existing
oom_score_adjfor tasks, mostly to folow the existing design.One of the questions to answer in priority-based model is how to compare tasks in the root cgroup with cgroups?
We do this with an alternate scoring mechanism, that is purely priority based and tiebreaks based on largest rss. An additional tunable is added for each process, under /proc/pid, and also to the memcg hierarchy, and is enabled via a system-wide sysctl. I way to mesh the two scoring mechanisms together would be helpful, but for our purposes we don't use oom_score_adj at all, other than converting OOM_SCORE_ADJ_MIN to still be oom disabled when written by third party apps.
For memcg oom conditions, iteration of the hierarchy begins at the oom memcg. For system oom conditions, this is the root memcg.
All processes attached to the oom memcg have their priority based value and this is compared to all child memcg's priority value at that level. If a process has the lowest priority, it is killed and we're done; we could implement a "kill all" mechanism for this memcg that is checked before the process is killed.
If a memcg has the lowest priority compared to attached processes, it is iterated as well, and so on throughout the memcg hierarchy until we find the lowest priority process in the lowest priority leaf memcg. This way, we can fully control which process is killed for both system and memcg oom conditions. I can easily post patches for this, we have used it for years.
Quoted 9 lines
Quoted 4 lines
These are two different things, right? We can adjust how the system oom killer chooses victims when memcg hierarchies overcommit the system to not strictly prefer the single process with the largest rss without killing everything attached to the memcg.
They are different, and I thought about providing two independent knobs. But after all I haven't found enough real life examples, where it can be useful. Can you provide something here?
Yes, we have users who we chown their memcg hierarchy to and have full control over setting up their hierarchy however we want. Our "Activity Manager", using Documentation/cgroup-v1/memory.txt terminology, only is aware of the top level memcg that was chown'd to the user. That user runs a series of batch jobs that are submitted to it and each job is represented as a subcontainer to enforce strict limits on the amount of memory that job can use. When it becomes oom, we have found that it is preferable to oom kill the entire batch job rather than leave it in an inconsistent state, so enabling such a knob here would be helpful.
Other top-level jobs are fine with individual processes being oom killed. It can be a low priority process for which they have full control over defining the priority through the new per-process and per-memcg value described above. Easy example is scraping logs periodically or other best-effort tasks like cleanup. They can happily be oom killed and rescheduled without taking down the entire first-class job.
Quoted 9 lines
Also, they are different only for non-leaf cgroups; leaf cgroups are always treated as indivisible memory consumers during victim selection.
I assume, that containerized systems will always set
oom_kill_all_tasksfor top-level container memory cgroups. By default it's turned off to provide backward compatibility with current behavior and avoid excessive kills and support oom_score_adj==-1000 (I've added this to v4, will post soon).
We certainly would not be enabling it for top-level memcgs, there would be no way that we could because we have best-effort processes, but we would like to enable it for small batch jobs that are run on behalf of a user in their own subcontainer. We have had this usecase for ~3 years and solely because of the problem that you pointed out earlier: it is often much more reliable for the kernel to do oom killing of multiple processes rather than userspace.
Quoted 30 lines
Quoted 24 lines
In our methodology, each memcg is assigned a priority value and the iteration of the hierarchy simply compares and visits the memcg with the lowest priority at each level and then selects the largest process to kill. This could also support a "kill-all" knob.
struct mem_cgroup *memcg = root_mem_cgroup; struct mem_cgroup *low_memcg; unsigned long low_priority;next:
low_memcg = NULL; low_priority = ULONG_MAX; for_each_child_of_memcg(memcg) { unsigned long prio = memcg_oom_priority(memcg); if (prio < low_priority) { low_memcg = memcg; low_priority = prio; } } if (low_memcg) goto next; oom_kill_process_from_memcg(memcg);Quoted 5 lines
So this is a priority based model that is different than your aggregate usage model but I think it allows userspace to define a more powerful policy. We certainly may want to kill from a memcg with a single large process, or we may want to kill from a memcg with several small processes, it depends on the importance of that job.
Quoted 3 lines
I believe, that both models have some advantages.
Priority-based model is more powerful, but requires support from the userspace to set up these priorities (and, probably, adjust them dynamically).
It's a no-op if nobody sets up priorities or the system-wide sysctl is disabled. Presumably, as in our model, the Activity Manager sets the sysctl and is responsible for configuring the priorities if present. All memcgs at the sibling level or subcontainer level remain the default if not defined by the chown'd user, so this falls back to an rss model for backwards compatibility.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [v3 2/6] mm, oom: cgroup-aware OOM killer
Quoted 127 lines · David Rientjes
Quoted 13 lines · Roman Gushchin
Quoted 5 lines
Yes, the original motivation was to limit killing to a single process, if possible. To do that, we kill the process with the largest rss to free the most memory and rely on the user to configure
/proc/pid/oom_score_adjif something else should be prioritized.Quoted 7 lines
With containerization and overcommit of system memory, we concur that killing the single largest process isn't always preferable and neglects the priority of its memcg. Your motivation seems to be to provide fairness between one memcg with a large process and one memcg with a large number of small processes; I'm curious if you are concerned about the priority of a memcg hierarchy (how important that "job" is) or whether you are strictly concerned with "largeness" of memcgs relative to each other.
Quoted 8 lines
I'm pretty sure we should provide some way to prioritize some cgroups over other (in terms of oom killer preferences), but I'm not 100% sure yet, what's the best way to do it. I've suggested something similar to the existing
oom_score_adjfor tasks, mostly to folow the existing design.One of the questions to answer in priority-based model is how to compare tasks in the root cgroup with cgroups?
We do this with an alternate scoring mechanism, that is purely priority based and tiebreaks based on largest rss. An additional tunable is added for each process, under
/proc/pid, and also to the memcg hierarchy, and is enabled via a system-wide sysctl. I way to mesh the two scoring mechanisms together would be helpful, but for our purposes we don't useoom_score_adjat all, other than convertingOOM_SCORE_ADJ_MINto still be oom disabled when written by third party apps.For memcg oom conditions, iteration of the hierarchy begins at the oom memcg. For system oom conditions, this is the root memcg.
All processes attached to the oom memcg have their priority based value and this is compared to all child memcg's priority value at that level. If a process has the lowest priority, it is killed and we're done; we could implement a "kill all" mechanism for this memcg that is checked before the process is killed.
If a memcg has the lowest priority compared to attached processes, it is iterated as well, and so on throughout the memcg hierarchy until we find the lowest priority process in the lowest priority leaf memcg. This way, we can fully control which process is killed for both system and memcg oom conditions. I can easily post patches for this, we have used it for years.
Quoted 9 lines
Quoted 4 lines
These are two different things, right? We can adjust how the system oom killer chooses victims when memcg hierarchies overcommit the system to not strictly prefer the single process with the largest rss without killing everything attached to the memcg.
They are different, and I thought about providing two independent knobs. But after all I haven't found enough real life examples, where it can be useful. Can you provide something here?
Yes, we have users who we chown their memcg hierarchy to and have full control over setting up their hierarchy however we want. Our "Activity Manager", using
Documentation/cgroup-v1/memory.txtterminology, only is aware of the top level memcg that was chown'd to the user. That user runs a series of batch jobs that are submitted to it and each job is represented as a subcontainer to enforce strict limits on the amount of memory that job can use. When it becomes oom, we have found that it is preferable to oom kill the entire batch job rather than leave it in an inconsistent state, so enabling such a knob here would be helpful.Other top-level jobs are fine with individual processes being oom killed. It can be a low priority process for which they have full control over defining the priority through the new per-process and per-memcg value described above. Easy example is scraping logs periodically or other best-effort tasks like cleanup. They can happily be oom killed and rescheduled without taking down the entire first-class job.
Quoted 9 lines
Also, they are different only for non-leaf cgroups; leaf cgroups are always treated as indivisible memory consumers during victim selection.
I assume, that containerized systems will always set
oom_kill_all_tasksfor top-level container memory cgroups. By default it's turned off to provide backward compatibility with current behavior and avoid excessive kills and support oom_score_adj==-1000 (I've added this to v4, will post soon).We certainly would not be enabling it for top-level memcgs, there would be no way that we could because we have best-effort processes, but we would like to enable it for small batch jobs that are run on behalf of a user in their own subcontainer. We have had this usecase for ~3 years and solely because of the problem that you pointed out earlier: it is often much more reliable for the kernel to do oom killing of multiple processes rather than userspace.
Quoted 30 lines
Quoted 24 lines
In our methodology, each memcg is assigned a priority value and the iteration of the hierarchy simply compares and visits the memcg with the lowest priority at each level and then selects the largest process to kill. This could also support a "kill-all" knob.
struct mem_cgroup *memcg = root_mem_cgroup; struct mem_cgroup *low_memcg; unsigned long low_priority;next:
low_memcg = NULL; low_priority = ULONG_MAX; for_each_child_of_memcg(memcg) { unsigned long prio = memcg_oom_priority(memcg); if (prio < low_priority) { low_memcg = memcg; low_priority = prio; } } if (low_memcg) goto next; oom_kill_process_from_memcg(memcg);Quoted 5 lines
So this is a priority based model that is different than your aggregate usage model but I think it allows userspace to define a more powerful policy. We certainly may want to kill from a memcg with a single large process, or we may want to kill from a memcg with several small processes, it depends on the importance of that job.
Quoted 3 lines
I believe, that both models have some advantages.
Priority-based model is more powerful, but requires support from the userspace to set up these priorities (and, probably, adjust them dynamically).
Quoted 6 lines
It's a no-op if nobody sets up priorities or the system-wide sysctl is disabled. Presumably, as in our model, the Activity Manager sets the sysctl and is responsible for configuring the priorities if present. All memcgs at the sibling level or subcontainer level remain the default if not defined by the chown'd user, so this falls back to an rss model for backwards compatibility.
Hm, this is interesting...
What I'm thinking about, is that we can introduce the following model:
each memory cgroup has an integer oom priority value, 0 be default. Root cgroup priority is always 0, other cgroups can have both positive or negative priorities.
During OOM victim selection we compare cgroups on each hierarchy level based on priority and size, if there are several cgroups with equal priority. Per-task oom_score_adj will affect task selection inside a cgroup if oom_kill_all_tasks is not set. -1000 special value will also completely protect a task from being killed, if only oom_kill_all_tasks is not set.
I wonder, if it will work for you?
Thanks!
Roman
David Rientjes
reviewerRe: [v3 2/6] mm, oom: cgroup-aware OOM killer
Quoted 7 lines · Roman Gushchin
Quoted 6 lines
It's a no-op if nobody sets up priorities or the system-wide sysctl is disabled. Presumably, as in our model, the Activity Manager sets the sysctl and is responsible for configuring the priorities if present. All memcgs at the sibling level or subcontainer level remain the default if not defined by the chown'd user, so this falls back to an rss model for backwards compatibility.
Quoted 7 lines
Hm, this is interesting...
What I'm thinking about, is that we can introduce the following model:
each memory cgroup has an integer oom priority value, 0 be default. Root cgroup priority is always 0, other cgroups can have both positive or negative priorities.
For our purposes we use a range of [0, 10000] for the per-process oom priority; 10000 implies the process is not oom killable, 5000 is the default. We use a range of [0, 9999] for the per-memcg oom priority since memcgs cannot disable themselves from oom killing (although they could oom disable all attached processes). We can obviously remap our priorities to whatever we decide here, but I think we should give ourselves more room and provide 10000 priorities at the minimum (we have 5000 true priorities plus overlimit bias). I'm not sure that negative priorities make sense in this model, is there a strong reason to prefer [-5000, 5000] over [0, 10000]?
And, yes, the root memcg remains a constant oom priority and is never actually checked.
Quoted 6 lines
During OOM victim selection we compare cgroups on each hierarchy level based on priority and size, if there are several cgroups with equal priority. Per-task
oom_score_adjwill affect task selection inside a cgroup ifoom_kill_all_tasksis not set. -1000 special value will also completely protect a task from being killed, if onlyoom_kill_all_tasksis not set.
If there are several cgroups of equal priority, we prefer the one that was created the most recently just to avoid losing work that has been done for a long period of time. But the key in this proposal is that we _always_ continue to iterate the memcg hierarchy until we find a process attached to a memcg with the lowest priority relative to sibling cgroups, if any.
To adapt your model to this proposal, memory.oom_kill_all_tasks would only be effective if there are no descendant memcgs. In that case, iteration stops anyway and in my model we kill the process with the lowest per-process priority. This could trivially check memory.oom_kill_all_tasks and kill everything, and I'm happy to support that feature since we have had a need for it in the past as well.
We should talk about when this priority-based scoring becomes effective. We enable it by default in our kernel, but it could be guarded with a VM sysctl if necessary to enact a system-wide policy.