Re: [RFC PATCH 2/2] memcg: do not report racy no-eligible OOM tasks
From: Tetsuo Handa
Date: Mon Oct 22 2018 - 21:01:34 EST
Michal Hocko wrote:
> On Mon 22-10-18 20:45:17, Tetsuo Handa wrote:
> > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> > > index e79cb59552d9..a9dfed29967b 100644
> > > --- a/mm/memcontrol.c
> > > +++ b/mm/memcontrol.c
> > > @@ -1380,10 +1380,22 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
> > > .gfp_mask = gfp_mask,
> > > .order = order,
> > > };
> > > - bool ret;
> > > + bool ret = true;
> > >
> > > mutex_lock(&oom_lock);
> > > +
> > > + /*
> > > + * multi-threaded tasks might race with oom_reaper and gain
> > > + * MMF_OOM_SKIP before reaching out_of_memory which can lead
> > > + * to out_of_memory failure if the task is the last one in
> > > + * memcg which would be a false possitive failure reported
> > > + */
> > > + if (tsk_is_oom_victim(current))
> > > + goto unlock;
> > > +
> >
> > This is not wrong but is strange. We can use mutex_lock_killable(&oom_lock)
> > so that any killed threads no longer wait for oom_lock.
>
> tsk_is_oom_victim is stronger because it doesn't depend on
> fatal_signal_pending which might be cleared throughout the exit process.
>
I still want to propose this. No need to be memcg OOM specific.
mm/memcontrol.c | 3 ++-
mm/oom_kill.c | 10 ++++++++++
2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index e79cb59..2c1e1ac 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1382,7 +1382,8 @@ static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
};
bool ret;
- mutex_lock(&oom_lock);
+ if (mutex_lock_killable(&oom_lock))
+ return true;
ret = out_of_memory(&oc);
mutex_unlock(&oom_lock);
return ret;
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index f10aa53..e453bad 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -1055,6 +1055,16 @@ bool out_of_memory(struct oom_control *oc)
unsigned long freed = 0;
enum oom_constraint constraint = CONSTRAINT_NONE;
+ /*
+ * It is possible that multi-threaded OOM victims get
+ * task_will_free_mem(current) == false when the OOM reaper quickly
+ * set MMF_OOM_SKIP. But since we know that tsk_is_oom_victim() == true
+ * tasks won't loop forever (unleess it is a __GFP_NOFAIL allocation
+ * request), we don't need to select next OOM victim.
+ */
+ if (tsk_is_oom_victim(current) && !(oc->gfp_mask & __GFP_NOFAIL))
+ return true;
+
if (oom_killer_disabled)
return false;
--
1.8.3.1