Commit 9ff4868e authored by David Rientjes's avatar David Rientjes Committed by Linus Torvalds

mm, oom: allow exiting threads to have access to memory reserves

Exiting threads, those with PF_EXITING set, can pagefault and require
memory before they can make forward progress.  This happens, for instance,
when a process must fault task->robust_list, a userspace structure, before
detaching its memory.

These threads also aren't guaranteed to get access to memory reserves
unless oom killed or killed from userspace.  The oom killer won't grant
memory reserves if other threads are also exiting other than current and
stalling at the same point.  This prevents needlessly killing processes
when others are already exiting.

Instead of special casing all the possible situations between PF_EXITING
getting set and a thread detaching its mm where it may allocate memory,
which probably wouldn't get updated when a change is made to the exit
path, the solution is to give all exiting threads access to memory
reserves if they call the oom killer.  This allows them to quickly
allocate, detach its mm, and free the memory it represents.

Summary of Luigi's bug report:

: He had an oom condition where threads were faulting on task->robust_list
: and repeatedly called the oom killer but it would defer killing a thread
: because it saw other PF_EXITING threads.  This can happen anytime we need
: to allocate memory after setting PF_EXITING and before detaching our mm;
: if there are other threads in the same state then the oom killer won't do
: anything unless one of them happens to be killed from userspace.
:
: So instead of only deferring for PF_EXITING and !task->robust_list, it's
: better to just give them access to memory reserves to prevent a potential
: livelock so that any other faults that may be introduced in the future in
: the exit path don't cause the same problem (and hopefully we don't allow
: too many of those!).
Signed-off-by: default avatarDavid Rientjes <rientjes@google.com>
Acked-by: default avatarMinchan Kim <minchan@kernel.org>
Tested-by: default avatarLuigi Semenzato <semenzato@google.com>
Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 348b4655
...@@ -310,27 +310,14 @@ enum oom_scan_t oom_scan_process_thread(struct task_struct *task, ...@@ -310,27 +310,14 @@ enum oom_scan_t oom_scan_process_thread(struct task_struct *task,
if (!task->mm) if (!task->mm)
return OOM_SCAN_CONTINUE; return OOM_SCAN_CONTINUE;
if (task->flags & PF_EXITING) { if (task->flags & PF_EXITING && !force_kill) {
/* /*
* If task is current and is in the process of releasing memory, * If this task is not being ptraced on exit, then wait for it
* allow the "kill" to set TIF_MEMDIE, which will allow it to * to finish before killing some other task unnecessarily.
* access memory reserves. Otherwise, it may stall forever.
*
* The iteration isn't broken here, however, in case other
* threads are found to have already been oom killed.
*/
if (task == current)
return OOM_SCAN_SELECT;
else if (!force_kill) {
/*
* If this task is not being ptraced on exit, then wait
* for it to finish before killing some other task
* unnecessarily.
*/ */
if (!(task->group_leader->ptrace & PT_TRACE_EXIT)) if (!(task->group_leader->ptrace & PT_TRACE_EXIT))
return OOM_SCAN_ABORT; return OOM_SCAN_ABORT;
} }
}
return OOM_SCAN_OK; return OOM_SCAN_OK;
} }
...@@ -706,11 +693,11 @@ void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, ...@@ -706,11 +693,11 @@ void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,
return; return;
/* /*
* If current has a pending SIGKILL, then automatically select it. The * If current has a pending SIGKILL or is exiting, then automatically
* goal is to allow it to allocate so that it may quickly exit and free * select it. The goal is to allow it to allocate so that it may
* its memory. * quickly exit and free its memory.
*/ */
if (fatal_signal_pending(current)) { if (fatal_signal_pending(current) || current->flags & PF_EXITING) {
set_thread_flag(TIF_MEMDIE); set_thread_flag(TIF_MEMDIE);
return; return;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment