diff mbox

mm: Check for SIGKILL inside dup_mmap() loop.

Message ID 20180607150546.1c7db21f70221008e14b8bb8@linux-foundation.org (mailing list archive)
State New, archived
Headers show

Commit Message

Andrew Morton June 7, 2018, 10:05 p.m. UTC
Despite all the discussion, we're short on formal review/ack tags on
this one.

Here's what I have:


From: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Subject: mm: check for SIGKILL inside dup_mmap() loop

As a theoretical problem, dup_mmap() of an mm_struct with 60000+ vmas can
loop while potentially allocating memory, with mm->mmap_sem held for write
by current thread.  This is bad if current thread was selected as an OOM
victim, for current thread will continue allocations using memory reserves
while OOM reaper is unable to reclaim memory.

As an actually observable problem, it is not difficult to make OOM reaper
unable to reclaim memory if the OOM victim is blocked at
i_mmap_lock_write() in this loop.  Unfortunately, since nobody can explain
whether it is safe to use killable wait there, let's check for SIGKILL
before trying to allocate memory.  Even without an OOM event, there is no
point with continuing the loop from the beginning if current thread is
killed.

I tested with debug printk().  This patch should be safe because we
already fail if security_vm_enough_memory_mm() or
kmem_cache_alloc(GFP_KERNEL) fails and exit_mmap() handles it.

[  417.030691] ***** Aborting dup_mmap() due to SIGKILL *****
[  417.036129] ***** Aborting dup_mmap() due to SIGKILL *****
[  417.044544] ***** Aborting dup_mmap() due to SIGKILL *****
[  419.116445] ***** Aborting dup_mmap() due to SIGKILL *****
[  419.118401] ***** Aborting exit_mmap() due to NULL mmap *****

[akpm@linux-foundation.org: add comment]
Link: http://lkml.kernel.org/r/201804071938.CDE04681.SOFVQJFtMHOOLF@I-love.SAKURA.ne.jp
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Rik van Riel <riel@redhat.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: David Rientjes <rientjes@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 kernel/fork.c |    8 ++++++++
 1 file changed, 8 insertions(+)

Comments

Matthew Wilcox June 8, 2018, 5:05 p.m. UTC | #1
On Thu, Jun 07, 2018 at 03:05:46PM -0700, Andrew Morton wrote:
> [akpm@linux-foundation.org: add comment]

Can I fix the comment?  ;-)

> @@ -440,6 +440,14 @@ static __latent_entropy int dup_mmap(str
>  			continue;
>  		}
>  		charge = 0;
> +		/*
> +		 * Don't duplicate many vmas if we've been oom-killed (for
> +		 * example)
> +		 */

		/*
		 * No point in continuing if we're just going to die at
		 * the end of the fork.  This may happen due to being OOM.
		 */

> +		if (fatal_signal_pending(current)) {
> +			retval = -EINTR;
> +			goto out;
> +		}

Reviewed-by: Matthew Wilcox <mawilcox@microsoft.com>
diff mbox

Patch

diff -puN kernel/fork.c~mm-check-for-sigkill-inside-dup_mmap-loop kernel/fork.c
--- a/kernel/fork.c~mm-check-for-sigkill-inside-dup_mmap-loop
+++ a/kernel/fork.c
@@ -440,6 +440,14 @@  static __latent_entropy int dup_mmap(str
 			continue;
 		}
 		charge = 0;
+		/*
+		 * Don't duplicate many vmas if we've been oom-killed (for
+		 * example)
+		 */
+		if (fatal_signal_pending(current)) {
+			retval = -EINTR;
+			goto out;
+		}
 		if (mpnt->vm_flags & VM_ACCOUNT) {
 			unsigned long len = vma_pages(mpnt);