All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] mm, memcg: skip killing processes under memcg protection at first scan
@ 2019-08-19  1:18 Yafang Shao
  2019-08-19 21:12 ` Roman Gushchin
  2019-08-20 21:39 ` Roman Gushchin
  0 siblings, 2 replies; 23+ messages in thread
From: Yafang Shao @ 2019-08-19  1:18 UTC (permalink / raw)
  To: akpm
  Cc: linux-mm, Yafang Shao, Roman Gushchin, Randy Dunlap,
	Johannes Weiner, Michal Hocko, Vladimir Davydov, Tetsuo Handa,
	Souptick Joarder, Yafang Shao

In the current memory.min design, the system is going to do OOM instead
of reclaiming the reclaimable pages protected by memory.min if the
system is lack of free memory. While under this condition, the OOM
killer may kill the processes in the memcg protected by memory.min.
This behavior is very weird.
In order to make it more reasonable, I make some changes in the OOM
killer. In this patch, the OOM killer will do two-round scan. It will
skip the processes under memcg protection at the first scan, and if it
can't kill any processes it will rescan all the processes.

Regarding the overhead this change may takes, I don't think it will be a
problem because this only happens under system  memory pressure and
the OOM killer can't find any proper victims which are not under memcg
protection.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Cc: Roman Gushchin <guro@fb.com>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Cc: Souptick Joarder <jrdr.linux@gmail.com>
Cc: Yafang Shao <shaoyafang@didiglobal.com>
---
 include/linux/memcontrol.h |  6 ++++++
 mm/memcontrol.c            | 16 ++++++++++++++++
 mm/oom_kill.c              | 23 +++++++++++++++++++++--
 3 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 44c4146..534fe92 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -337,6 +337,7 @@ static inline bool mem_cgroup_disabled(void)
 
 enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root,
 						struct mem_cgroup *memcg);
+bool task_under_memcg_protection(struct task_struct *p);
 
 int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
 			  gfp_t gfp_mask, struct mem_cgroup **memcgp,
@@ -813,6 +814,11 @@ static inline enum mem_cgroup_protection mem_cgroup_protected(
 	return MEMCG_PROT_NONE;
 }
 
+static inline bool task_under_memcg_protection(struct task_struct *p)
+{
+	return false;
+}
+
 static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
 					gfp_t gfp_mask,
 					struct mem_cgroup **memcgp,
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index cdbb7a8..8df3d88 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6030,6 +6030,22 @@ enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root,
 		return MEMCG_PROT_NONE;
 }
 
+bool task_under_memcg_protection(struct task_struct *p)
+{
+	struct mem_cgroup *memcg;
+	bool protected;
+
+	rcu_read_lock();
+	memcg = mem_cgroup_from_task(p);
+	if (memcg != root_mem_cgroup && memcg->memory.min)
+		protected = true;
+	else
+		protected = false;
+	rcu_read_unlock();
+
+	return protected;
+}
+
 /**
  * mem_cgroup_try_charge - try charging a page
  * @page: page to charge
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index eda2e2a..e9bdad3 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -367,12 +367,31 @@ static void select_bad_process(struct oom_control *oc)
 	if (is_memcg_oom(oc))
 		mem_cgroup_scan_tasks(oc->memcg, oom_evaluate_task, oc);
 	else {
+		bool memcg_check = false;
+		bool memcg_skip = false;
 		struct task_struct *p;
+		int selected = 0;
 
 		rcu_read_lock();
-		for_each_process(p)
-			if (oom_evaluate_task(p, oc))
+retry:
+		for_each_process(p) {
+			if (!memcg_check && task_under_memcg_protection(p)) {
+				memcg_skip = true;
+				continue;
+			}
+			selected = oom_evaluate_task(p, oc);
+			if (selected)
 				break;
+		}
+
+		if (!selected) {
+			if (memcg_skip) {
+				if (!oc->chosen || oc->chosen == (void *)-1UL) {
+					memcg_check = true;
+					goto retry;
+				}
+			}
+		}
 		rcu_read_unlock();
 	}
 }
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2019-08-21  8:46 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-19  1:18 [PATCH v2] mm, memcg: skip killing processes under memcg protection at first scan Yafang Shao
2019-08-19 21:12 ` Roman Gushchin
2019-08-20  1:16   ` Yafang Shao
2019-08-20  1:39     ` Roman Gushchin
2019-08-20  2:01       ` Yafang Shao
2019-08-20  2:40         ` Yafang Shao
2019-08-20  6:40     ` Michal Hocko
2019-08-20  7:15       ` Yafang Shao
2019-08-20  7:27         ` Michal Hocko
2019-08-20  7:49           ` Yafang Shao
2019-08-20  8:34             ` Michal Hocko
2019-08-20  8:55               ` Yafang Shao
2019-08-20  9:17                 ` Michal Hocko
2019-08-20  9:26                   ` Yafang Shao
2019-08-20 10:40                     ` Michal Hocko
2019-08-20 21:39 ` Roman Gushchin
2019-08-21  1:00   ` Yafang Shao
2019-08-21  6:44     ` Michal Hocko
2019-08-21  7:26       ` Yafang Shao
2019-08-21  8:05         ` Michal Hocko
2019-08-21  8:15           ` Yafang Shao
2019-08-21  8:34             ` Michal Hocko
2019-08-21  8:46               ` Yafang Shao

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.