From 7313a2abbf8af0a3e76eba01336b0a032e582050 Mon Sep 17 00:00:00 2001 From: Fupan Li Date: Fri, 10 Mar 2023 15:34:46 +0800 Subject: [PATCH] anolis: notify the victim's memcg's oom event when system OOM occur ANBZ: #8462 When a system OOM occur, and select a victim to kill, it's better to notify the victim's memcg's OOM event, thus, the container's cgroup OOM event watcher would be notified and mark the container's exit reason as "OOM". Signed-off-by: Fupan Li Acked-by: Xu Yu --- include/linux/oom.h | 1 + mm/memcontrol.c | 3 +-- mm/oom_kill.c | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/linux/oom.h b/include/linux/oom.h index 53ab32996788..d554ec3b30f4 100644 --- a/include/linux/oom.h +++ b/include/linux/oom.h @@ -109,6 +109,7 @@ extern unsigned long oom_badness(struct task_struct *p, extern bool out_of_memory(struct oom_control *oc); extern void exit_oom_victim(void); +extern void mem_cgroup_oom_notify(struct mem_cgroup *memcg); extern int register_oom_notifier(struct notifier_block *nb); extern int unregister_oom_notifier(struct notifier_block *nb); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 8ee61f4b3706..3384c2c7a01f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -186,7 +186,6 @@ struct mem_cgroup_event { }; static void mem_cgroup_threshold(struct mem_cgroup *memcg); -static void mem_cgroup_oom_notify(struct mem_cgroup *memcg); /* Stuffs for move charges at task migration. */ /* @@ -5069,7 +5068,7 @@ static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg) return 0; } -static void mem_cgroup_oom_notify(struct mem_cgroup *memcg) +void mem_cgroup_oom_notify(struct mem_cgroup *memcg) { struct mem_cgroup *iter; diff --git a/mm/oom_kill.c b/mm/oom_kill.c index e96d52601770..8d504bac2809 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -970,6 +970,11 @@ static void oom_kill_process(struct oom_control *oc, const char *message) struct task_struct *child; struct task_struct *t; struct mem_cgroup *oom_group; + +#ifdef CONFIG_MEMCG + struct mem_cgroup *memcg; +#endif + unsigned int victim_points = 0; static DEFINE_RATELIMIT_STATE(oom_global_rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST); @@ -1045,6 +1050,16 @@ static void oom_kill_process(struct oom_control *oc, const char *message) put_task_struct(p); read_unlock(&tasklist_lock); +#ifdef CONFIG_MEMCG + rcu_read_lock(); + memcg = mem_cgroup_from_task(victim); + if (memcg != NULL && memcg != root_mem_cgroup && !is_memcg_oom(oc)) { + css_get(&memcg->css); + mem_cgroup_oom_notify(memcg); + css_put(&memcg->css); + } + rcu_read_unlock(); +#endif /* * Do we need to kill the entire memory cgroup? * Or even one of the ancestor memory cgroups? -- Gitee