linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Andrea Arcangeli <aarcange@redhat.com>
To: Andrew Morton <akpm@linux-foundation.org>, linux-mm@kvack.org
Cc: "Dr. David Alan Gilbert" <dgilbert@redhat.com>,
	Maxime Coquelin <maxime.coquelin@redhat.com>,
	Mike Rapoport <rppt@linux.vnet.ibm.com>,
	Mike Kravetz <mike.kravetz@oracle.com>,
	Alexey Perevalov <a.perevalov@samsung.com>
Subject: [PATCH 4/6] userfaultfd: call userfaultfd_unmap_prep only if __split_vma succeeds
Date: Wed,  2 Aug 2017 18:51:43 +0200	[thread overview]
Message-ID: <20170802165145.22628-5-aarcange@redhat.com> (raw)
In-Reply-To: <20170802165145.22628-1-aarcange@redhat.com>

A __split_vma is not a worthy event to report, and it's definitely not
a unmap so it would be incorrect to report unmap for the whole region
to the userfaultfd manager if a __split_vma fails.

So only call userfaultfd_unmap_prep after the __vma_splitting is over
and do_munmap cannot fail anymore.

Also add unlikely because it's better to optimize for the vast
majority of apps that aren't using userfaultfd in a non cooperative
way. Ideally we should also find a way to eliminate the branch
entirely if CONFIG_USERFAULTFD=n, but it would complicate things so
stick to unlikely for now.

Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
---
 mm/mmap.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/mm/mmap.c b/mm/mmap.c
index f19efcf75418..9b0161f1adf3 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -2639,13 +2639,6 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len,
 	if (vma->vm_start >= end)
 		return 0;
 
-	if (uf) {
-		int error = userfaultfd_unmap_prep(vma, start, end, uf);
-
-		if (error)
-			return error;
-	}
-
 	/*
 	 * If we need to split any vma, do it now to save pain later.
 	 *
@@ -2679,6 +2672,21 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len,
 	}
 	vma = prev ? prev->vm_next : mm->mmap;
 
+	if (unlikely(uf)) {
+		/*
+		 * If userfaultfd_unmap_prep returns an error the vmas
+		 * will remain splitted, but userland will get a
+		 * highly unexpected error anyway. This is no
+		 * different than the case where the first of the two
+		 * __split_vma fails, but we don't undo the first
+		 * split, despite we could. This is unlikely enough
+		 * failure that it's not worth optimizing it for.
+		 */
+		int error = userfaultfd_unmap_prep(vma, start, end, uf);
+		if (error)
+			return error;
+	}
+
 	/*
 	 * unlock any mlock()ed ranges before detaching vmas
 	 */

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

  parent reply	other threads:[~2017-08-02 16:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-02 16:51 [PATCH 0/6] userfaultfd updates for v4.13-rc3 Andrea Arcangeli
2017-08-02 16:51 ` [PATCH 1/6] userfaultfd: hugetlbfs: remove superfluous page unlock in VM_SHARED case Andrea Arcangeli
2017-08-02 17:12   ` Mike Kravetz
2017-08-02 16:51 ` [PATCH 2/6] userfaultfd: selftest: exercise UFFDIO_COPY/ZEROPAGE -EEXIST Andrea Arcangeli
2017-08-02 21:33   ` Mike Kravetz
2017-08-02 16:51 ` [PATCH 3/6] userfaultfd: selftest: explicit failure if the SIGBUS test failed Andrea Arcangeli
2017-08-02 16:51 ` Andrea Arcangeli [this message]
2017-08-02 16:51 ` [PATCH 5/6] userfaultfd: provide pid in userfault msg Andrea Arcangeli
2017-08-02 16:51 ` [PATCH 6/6] userfaultfd: provide pid in userfault msg - add feat union Andrea Arcangeli
2017-08-02 21:29 ` [PATCH 0/6] userfaultfd updates for v4.13-rc3 Andrew Morton
2017-08-02 22:27   ` Andrea Arcangeli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170802165145.22628-5-aarcange@redhat.com \
    --to=aarcange@redhat.com \
    --cc=a.perevalov@samsung.com \
    --cc=akpm@linux-foundation.org \
    --cc=dgilbert@redhat.com \
    --cc=linux-mm@kvack.org \
    --cc=maxime.coquelin@redhat.com \
    --cc=mike.kravetz@oracle.com \
    --cc=rppt@linux.vnet.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).