All of lore.kernel.org
 help / color / mirror / Atom feed
* Patch "mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp" has been added to the 4.4-stable tree
@ 2017-05-23 14:46 gregkh
  0 siblings, 0 replies; 10+ messages in thread
From: gregkh @ 2017-05-23 14:46 UTC (permalink / raw)
  To: keno, akpm, amit.pundir, gregkh, gthelen, hughd, keescook,
	kirill.shutemov, luto, mhocko, npiggin, oleg, stable, torvalds,
	w
  Cc: stable, stable-commits


This is a note to let you know that I've just added the patch titled

    mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp

to the 4.4-stable tree which can be found at:
    http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     mm-huge_memory.c-respect-foll_force-foll_cow-for-thp.patch
and it can be found in the queue-4.4 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <stable@vger.kernel.org> know about it.


>From 8310d48b125d19fcd9521d83b8293e63eb1646aa Mon Sep 17 00:00:00 2001
From: Keno Fischer <keno@juliacomputing.com>
Date: Tue, 24 Jan 2017 15:17:48 -0800
Subject: mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp

From: Keno Fischer <keno@juliacomputing.com>

commit 8310d48b125d19fcd9521d83b8293e63eb1646aa upstream.

In commit 19be0eaffa3a ("mm: remove gup_flags FOLL_WRITE games from
__get_user_pages()"), the mm code was changed from unsetting FOLL_WRITE
after a COW was resolved to setting the (newly introduced) FOLL_COW
instead.  Simultaneously, the check in gup.c was updated to still allow
writes with FOLL_FORCE set if FOLL_COW had also been set.

However, a similar check in huge_memory.c was forgotten.  As a result,
remote memory writes to ro regions of memory backed by transparent huge
pages cause an infinite loop in the kernel (handle_mm_fault sets
FOLL_COW and returns 0 causing a retry, but follow_trans_huge_pmd bails
out immidiately because `(flags & FOLL_WRITE) && !pmd_write(*pmd)` is
true.

While in this state the process is stil SIGKILLable, but little else
works (e.g.  no ptrace attach, no other signals).  This is easily
reproduced with the following code (assuming thp are set to always):

    #include <assert.h>
    #include <fcntl.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/mman.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>

    #define TEST_SIZE 5 * 1024 * 1024

    int main(void) {
      int status;
      pid_t child;
      int fd = open("/proc/self/mem", O_RDWR);
      void *addr = mmap(NULL, TEST_SIZE, PROT_READ,
                        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
      assert(addr != MAP_FAILED);
      pid_t parent_pid = getpid();
      if ((child = fork()) == 0) {
        void *addr2 = mmap(NULL, TEST_SIZE, PROT_READ | PROT_WRITE,
                           MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
        assert(addr2 != MAP_FAILED);
        memset(addr2, 'a', TEST_SIZE);
        pwrite(fd, addr2, TEST_SIZE, (uintptr_t)addr);
        return 0;
      }
      assert(child == waitpid(child, &status, 0));
      assert(WIFEXITED(status) && WEXITSTATUS(status) == 0);
      return 0;
    }

Fix this by updating follow_trans_huge_pmd in huge_memory.c analogously
to the update in gup.c in the original commit.  The same pattern exists
in follow_devmap_pmd.  However, we should not be able to reach that
check with FOLL_COW set, so add WARN_ONCE to make sure we notice if we
ever do.

[akpm@linux-foundation.org: coding-style fixes]
Link: http://lkml.kernel.org/r/20170106015025.GA38411@juliacomputing.com
Signed-off-by: Keno Fischer <keno@juliacomputing.com>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Willy Tarreau <w@1wt.eu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
[AmitP: Minor refactoring of upstream changes for linux-3.18.y,
        where follow_devmap_pmd() doesn't exist.]
Signed-off-by: Amit Pundir <amit.pundir@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 mm/huge_memory.c |   12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1269,6 +1269,16 @@ out_unlock:
 	return ret;
 }
 
+/*
+ * FOLL_FORCE can write to even unwritable pmd's, but only
+ * after we've gone through a COW cycle and they are dirty.
+ */
+static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags)
+{
+	return pmd_write(pmd) ||
+	       ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pmd_dirty(pmd));
+}
+
 struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
 				   unsigned long addr,
 				   pmd_t *pmd,
@@ -1279,7 +1289,7 @@ struct page *follow_trans_huge_pmd(struc
 
 	assert_spin_locked(pmd_lockptr(mm, pmd));
 
-	if (flags & FOLL_WRITE && !pmd_write(*pmd))
+	if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags))
 		goto out;
 
 	/* Avoid dumping huge zero page */


Patches currently in stable-queue which might be from keno@juliacomputing.com are

queue-4.4/mm-huge_memory.c-respect-foll_force-foll_cow-for-thp.patch

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

* Re: Patch "mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp" has been added to the 4.4-stable tree
  2017-04-10 16:04   ` Greg KH
  2017-04-10 16:13     ` Michal Hocko
@ 2017-04-10 18:10     ` Ben Hutchings
  1 sibling, 0 replies; 10+ messages in thread
From: Ben Hutchings @ 2017-04-10 18:10 UTC (permalink / raw)
  To: Greg KH
  Cc: keno, akpm, gthelen, hughd, keescook, kirill.shutemov, luto,
	mhocko, npiggin, oleg, torvalds, w, stable, stable-commits

[-- Attachment #1: Type: text/plain, Size: 2118 bytes --]

On Mon, 2017-04-10 at 18:04 +0200, Greg KH wrote:
> On Mon, Apr 10, 2017 at 04:48:17PM +0100, Ben Hutchings wrote:
> > > > On Mon, 2017-04-10 at 17:09 +0200, gregkh@linuxfoundation.org wrote:
> > > This is a note to let you know that I've just added the patch titled
> > > 
> > >     mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> > > 
> > > to the 4.4-stable tree which can be found at:
> > >     http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
> > > 
> > > The filename of the patch is:
> > >      mm-huge_memory.c-respect-foll_force-foll_cow-for-thp.patch
> > > and it can be found in the queue-4.4 subdirectory.
> > > 
> > > If you, or anyone else, feels it should not be added to the stable tree,
> > > please let <stable@vger.kernel.org> know about it.
> > > 
> > > 
> > > From 303681d5d538d81b5e23754515202b5b9febd2e9 Mon Sep 17 00:00:00 2001
> > > From: Keno Fischer <keno@juliacomputing.com>
> > > Date: Tue, 24 Jan 2017 15:17:48 -0800
> > > Subject: mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> > > 
> > > From: Keno Fischer <keno@juliacomputing.com>
> > > 
> > > commit 8310d48b125d19fcd9521d83b8293e63eb1646aa upstream.
> > 
> > [...]
> > >  - Drop change to follow_devmap_pmd()
> > >  - pmd_dirty() is not available; check the page flags as in older
> > >    backports of can_follow_write_pte()
> > >  - Adjust context]
> > 
> > [...]
> > 
> > Aside from the fact that I got this backport wrong, I don't think you
> > need the main change here.  In 4.4, all arches that define
> > CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE also define pmd_dirty().
> 
> Hm, ok, but Keno said people are still hitting this issue on 4.4-stable.
> 
> So, what should I do?  I'll go drop this from my queue for now, but as I
> don't know what to do to test this, I'm at a loss...

I don't mean the fix isn't needed; I mean that I don't think you should
use this backport.  You should only need to drop the change to
follow_devmap_pmd().

Ben.

-- 
Ben Hutchings
73.46% of all statistics are made up.


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: Patch "mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp" has been added to the 4.4-stable tree
  2017-04-10 16:13     ` Michal Hocko
@ 2017-04-10 16:21       ` Greg KH
  0 siblings, 0 replies; 10+ messages in thread
From: Greg KH @ 2017-04-10 16:21 UTC (permalink / raw)
  To: Michal Hocko
  Cc: Ben Hutchings, keno, akpm, gthelen, hughd, keescook,
	kirill.shutemov, luto, npiggin, oleg, torvalds, w, stable,
	stable-commits

On Mon, Apr 10, 2017 at 06:13:59PM +0200, Michal Hocko wrote:
> On Mon 10-04-17 18:04:51, Greg KH wrote:
> > On Mon, Apr 10, 2017 at 04:48:17PM +0100, Ben Hutchings wrote:
> > > On Mon, 2017-04-10 at 17:09 +0200, gregkh@linuxfoundation.org wrote:
> > > > This is a note to let you know that I've just added the patch titled
> > > > 
> > > > ����mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> > > > 
> > > > to the 4.4-stable tree which can be found at:
> > > > ����http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
> > > > 
> > > > The filename of the patch is:
> > > > �����mm-huge_memory.c-respect-foll_force-foll_cow-for-thp.patch
> > > > and it can be found in the queue-4.4 subdirectory.
> > > > 
> > > > If you, or anyone else, feels it should not be added to the stable tree,
> > > > please let <stable@vger.kernel.org> know about it.
> > > > 
> > > > 
> > > > From 303681d5d538d81b5e23754515202b5b9febd2e9 Mon Sep 17 00:00:00 2001
> > > > From: Keno Fischer <keno@juliacomputing.com>
> > > > Date: Tue, 24 Jan 2017 15:17:48 -0800
> > > > Subject: mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> > > > 
> > > > From: Keno Fischer <keno@juliacomputing.com>
> > > > 
> > > > commit 8310d48b125d19fcd9521d83b8293e63eb1646aa upstream.
> > > [...]
> > > > �- Drop change to follow_devmap_pmd()
> > > > �- pmd_dirty() is not available; check the page flags as in older
> > > > ���backports of can_follow_write_pte()
> > > > �- Adjust context]
> > > [...]
> > > 
> > > Aside from the fact that I got this backport wrong, I don't think you
> > > need the main change here.  In 4.4, all arches that define
> > > CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE also define pmd_dirty().
> > 
> > Hm, ok, but Keno said people are still hitting this issue on 4.4-stable.
> > 
> > So, what should I do?  I'll go drop this from my queue for now, but as I
> > don't know what to do to test this, I'm at a loss...
> 
> The changelog of the said patch contains the reproducer...

Hah, I should have seen that :)

I'll wait a bit for this one, if I get some time later this week, I'll
work on the fixed up backport.  Unless Keno wants to provide it?
(hint...)

thanks,

greg k-h

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

* Re: Patch "mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp" has been added to the 4.4-stable tree
  2017-04-10 16:04   ` Greg KH
@ 2017-04-10 16:13     ` Michal Hocko
  2017-04-10 16:21       ` Greg KH
  2017-04-10 18:10     ` Ben Hutchings
  1 sibling, 1 reply; 10+ messages in thread
From: Michal Hocko @ 2017-04-10 16:13 UTC (permalink / raw)
  To: Greg KH
  Cc: Ben Hutchings, keno, akpm, gthelen, hughd, keescook,
	kirill.shutemov, luto, npiggin, oleg, torvalds, w, stable,
	stable-commits

On Mon 10-04-17 18:04:51, Greg KH wrote:
> On Mon, Apr 10, 2017 at 04:48:17PM +0100, Ben Hutchings wrote:
> > On Mon, 2017-04-10 at 17:09 +0200, gregkh@linuxfoundation.org wrote:
> > > This is a note to let you know that I've just added the patch titled
> > > 
> > > ����mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> > > 
> > > to the 4.4-stable tree which can be found at:
> > > ����http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
> > > 
> > > The filename of the patch is:
> > > �����mm-huge_memory.c-respect-foll_force-foll_cow-for-thp.patch
> > > and it can be found in the queue-4.4 subdirectory.
> > > 
> > > If you, or anyone else, feels it should not be added to the stable tree,
> > > please let <stable@vger.kernel.org> know about it.
> > > 
> > > 
> > > From 303681d5d538d81b5e23754515202b5b9febd2e9 Mon Sep 17 00:00:00 2001
> > > From: Keno Fischer <keno@juliacomputing.com>
> > > Date: Tue, 24 Jan 2017 15:17:48 -0800
> > > Subject: mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> > > 
> > > From: Keno Fischer <keno@juliacomputing.com>
> > > 
> > > commit 8310d48b125d19fcd9521d83b8293e63eb1646aa upstream.
> > [...]
> > > �- Drop change to follow_devmap_pmd()
> > > �- pmd_dirty() is not available; check the page flags as in older
> > > ���backports of can_follow_write_pte()
> > > �- Adjust context]
> > [...]
> > 
> > Aside from the fact that I got this backport wrong, I don't think you
> > need the main change here.  In 4.4, all arches that define
> > CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE also define pmd_dirty().
> 
> Hm, ok, but Keno said people are still hitting this issue on 4.4-stable.
> 
> So, what should I do?  I'll go drop this from my queue for now, but as I
> don't know what to do to test this, I'm at a loss...

The changelog of the said patch contains the reproducer...
-- 
Michal Hocko
SUSE Labs

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

* Re: Patch "mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp" has been added to the 4.4-stable tree
  2017-04-10 16:05   ` Greg KH
@ 2017-04-10 16:13     ` Michal Hocko
  0 siblings, 0 replies; 10+ messages in thread
From: Michal Hocko @ 2017-04-10 16:13 UTC (permalink / raw)
  To: Greg KH
  Cc: keno, akpm, ben, gthelen, hughd, keescook, kirill.shutemov, luto,
	npiggin, oleg, torvalds, w, stable, stable-commits

On Mon 10-04-17 18:05:16, Greg KH wrote:
> On Mon, Apr 10, 2017 at 05:34:17PM +0200, Michal Hocko wrote:
> > On Mon 10-04-17 17:09:17, Greg KH wrote:
> > [...]
> > > >From 303681d5d538d81b5e23754515202b5b9febd2e9 Mon Sep 17 00:00:00 2001
> > > From: Keno Fischer <keno@juliacomputing.com>
> > > Date: Tue, 24 Jan 2017 15:17:48 -0800
> > > Subject: mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> > > 
> > > From: Keno Fischer <keno@juliacomputing.com>
> > > 
> > > commit 8310d48b125d19fcd9521d83b8293e63eb1646aa upstream.
> > 
> > This backport is wrong. See
> > http://lkml.kernel.org/r/20170328131154.GH18241@dhcp22.suse.cz
> 
> No such Message-ID known.

https://www.spinics.net/lists/stable/msg165118.html
-- 
Michal Hocko
SUSE Labs

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

* Re: Patch "mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp" has been added to the 4.4-stable tree
  2017-04-10 15:34 ` Michal Hocko
@ 2017-04-10 16:05   ` Greg KH
  2017-04-10 16:13     ` Michal Hocko
  0 siblings, 1 reply; 10+ messages in thread
From: Greg KH @ 2017-04-10 16:05 UTC (permalink / raw)
  To: Michal Hocko
  Cc: keno, akpm, ben, gthelen, hughd, keescook, kirill.shutemov, luto,
	npiggin, oleg, torvalds, w, stable, stable-commits

On Mon, Apr 10, 2017 at 05:34:17PM +0200, Michal Hocko wrote:
> On Mon 10-04-17 17:09:17, Greg KH wrote:
> [...]
> > >From 303681d5d538d81b5e23754515202b5b9febd2e9 Mon Sep 17 00:00:00 2001
> > From: Keno Fischer <keno@juliacomputing.com>
> > Date: Tue, 24 Jan 2017 15:17:48 -0800
> > Subject: mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> > 
> > From: Keno Fischer <keno@juliacomputing.com>
> > 
> > commit 8310d48b125d19fcd9521d83b8293e63eb1646aa upstream.
> 
> This backport is wrong. See
> http://lkml.kernel.org/r/20170328131154.GH18241@dhcp22.suse.cz

No such Message-ID known.

:(

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

* Re: Patch "mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp" has been added to the 4.4-stable tree
  2017-04-10 15:48 ` Ben Hutchings
@ 2017-04-10 16:04   ` Greg KH
  2017-04-10 16:13     ` Michal Hocko
  2017-04-10 18:10     ` Ben Hutchings
  0 siblings, 2 replies; 10+ messages in thread
From: Greg KH @ 2017-04-10 16:04 UTC (permalink / raw)
  To: Ben Hutchings
  Cc: keno, akpm, gthelen, hughd, keescook, kirill.shutemov, luto,
	mhocko, npiggin, oleg, torvalds, w, stable, stable-commits

On Mon, Apr 10, 2017 at 04:48:17PM +0100, Ben Hutchings wrote:
> On Mon, 2017-04-10 at 17:09 +0200, gregkh@linuxfoundation.org wrote:
> > This is a note to let you know that I've just added the patch titled
> > 
> > ����mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> > 
> > to the 4.4-stable tree which can be found at:
> > ����http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
> > 
> > The filename of the patch is:
> > �����mm-huge_memory.c-respect-foll_force-foll_cow-for-thp.patch
> > and it can be found in the queue-4.4 subdirectory.
> > 
> > If you, or anyone else, feels it should not be added to the stable tree,
> > please let <stable@vger.kernel.org> know about it.
> > 
> > 
> > From 303681d5d538d81b5e23754515202b5b9febd2e9 Mon Sep 17 00:00:00 2001
> > From: Keno Fischer <keno@juliacomputing.com>
> > Date: Tue, 24 Jan 2017 15:17:48 -0800
> > Subject: mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> > 
> > From: Keno Fischer <keno@juliacomputing.com>
> > 
> > commit 8310d48b125d19fcd9521d83b8293e63eb1646aa upstream.
> [...]
> > �- Drop change to follow_devmap_pmd()
> > �- pmd_dirty() is not available; check the page flags as in older
> > ���backports of can_follow_write_pte()
> > �- Adjust context]
> [...]
> 
> Aside from the fact that I got this backport wrong, I don't think you
> need the main change here.  In 4.4, all arches that define
> CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE also define pmd_dirty().

Hm, ok, but Keno said people are still hitting this issue on 4.4-stable.

So, what should I do?  I'll go drop this from my queue for now, but as I
don't know what to do to test this, I'm at a loss...

thanks,

greg k-h

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

* Re: Patch "mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp" has been added to the 4.4-stable tree
  2017-04-10 15:09 gregkh
  2017-04-10 15:34 ` Michal Hocko
@ 2017-04-10 15:48 ` Ben Hutchings
  2017-04-10 16:04   ` Greg KH
  1 sibling, 1 reply; 10+ messages in thread
From: Ben Hutchings @ 2017-04-10 15:48 UTC (permalink / raw)
  To: gregkh, keno, akpm, gthelen, hughd, keescook, kirill.shutemov,
	luto, mhocko, npiggin, oleg, torvalds, w
  Cc: stable, stable-commits

[-- Attachment #1: Type: text/plain, Size: 1466 bytes --]

On Mon, 2017-04-10 at 17:09 +0200, gregkh@linuxfoundation.org wrote:
> This is a note to let you know that I've just added the patch titled
> 
>     mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> 
> to the 4.4-stable tree which can be found at:
>     http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
> 
> The filename of the patch is:
>      mm-huge_memory.c-respect-foll_force-foll_cow-for-thp.patch
> and it can be found in the queue-4.4 subdirectory.
> 
> If you, or anyone else, feels it should not be added to the stable tree,
> please let <stable@vger.kernel.org> know about it.
> 
> 
> From 303681d5d538d81b5e23754515202b5b9febd2e9 Mon Sep 17 00:00:00 2001
> From: Keno Fischer <keno@juliacomputing.com>
> Date: Tue, 24 Jan 2017 15:17:48 -0800
> Subject: mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> 
> From: Keno Fischer <keno@juliacomputing.com>
> 
> commit 8310d48b125d19fcd9521d83b8293e63eb1646aa upstream.
[...]
>  - Drop change to follow_devmap_pmd()
>  - pmd_dirty() is not available; check the page flags as in older
>    backports of can_follow_write_pte()
>  - Adjust context]
[...]

Aside from the fact that I got this backport wrong, I don't think you
need the main change here.  In 4.4, all arches that define
CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE also define pmd_dirty().

Ben.

-- 
Ben Hutchings
73.46% of all statistics are made up.

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: Patch "mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp" has been added to the 4.4-stable tree
  2017-04-10 15:09 gregkh
@ 2017-04-10 15:34 ` Michal Hocko
  2017-04-10 16:05   ` Greg KH
  2017-04-10 15:48 ` Ben Hutchings
  1 sibling, 1 reply; 10+ messages in thread
From: Michal Hocko @ 2017-04-10 15:34 UTC (permalink / raw)
  To: gregkh
  Cc: keno, akpm, ben, gthelen, hughd, keescook, kirill.shutemov, luto,
	npiggin, oleg, torvalds, w, stable, stable-commits

On Mon 10-04-17 17:09:17, Greg KH wrote:
[...]
> >From 303681d5d538d81b5e23754515202b5b9febd2e9 Mon Sep 17 00:00:00 2001
> From: Keno Fischer <keno@juliacomputing.com>
> Date: Tue, 24 Jan 2017 15:17:48 -0800
> Subject: mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp
> 
> From: Keno Fischer <keno@juliacomputing.com>
> 
> commit 8310d48b125d19fcd9521d83b8293e63eb1646aa upstream.

This backport is wrong. See
http://lkml.kernel.org/r/20170328131154.GH18241@dhcp22.suse.cz

> 
> In commit 19be0eaffa3a ("mm: remove gup_flags FOLL_WRITE games from
> __get_user_pages()"), the mm code was changed from unsetting FOLL_WRITE
> after a COW was resolved to setting the (newly introduced) FOLL_COW
> instead.  Simultaneously, the check in gup.c was updated to still allow
> writes with FOLL_FORCE set if FOLL_COW had also been set.
> 
> However, a similar check in huge_memory.c was forgotten.  As a result,
> remote memory writes to ro regions of memory backed by transparent huge
> pages cause an infinite loop in the kernel (handle_mm_fault sets
> FOLL_COW and returns 0 causing a retry, but follow_trans_huge_pmd bails
> out immidiately because `(flags & FOLL_WRITE) && !pmd_write(*pmd)` is
> true.
> 
> While in this state the process is stil SIGKILLable, but little else
> works (e.g.  no ptrace attach, no other signals).  This is easily
> reproduced with the following code (assuming thp are set to always):
> 
>     #include <assert.h>
>     #include <fcntl.h>
>     #include <stdint.h>
>     #include <stdio.h>
>     #include <string.h>
>     #include <sys/mman.h>
>     #include <sys/stat.h>
>     #include <sys/types.h>
>     #include <sys/wait.h>
>     #include <unistd.h>
> 
>     #define TEST_SIZE 5 * 1024 * 1024
> 
>     int main(void) {
>       int status;
>       pid_t child;
>       int fd = open("/proc/self/mem", O_RDWR);
>       void *addr = mmap(NULL, TEST_SIZE, PROT_READ,
>                         MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
>       assert(addr != MAP_FAILED);
>       pid_t parent_pid = getpid();
>       if ((child = fork()) == 0) {
>         void *addr2 = mmap(NULL, TEST_SIZE, PROT_READ | PROT_WRITE,
>                            MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
>         assert(addr2 != MAP_FAILED);
>         memset(addr2, 'a', TEST_SIZE);
>         pwrite(fd, addr2, TEST_SIZE, (uintptr_t)addr);
>         return 0;
>       }
>       assert(child == waitpid(child, &status, 0));
>       assert(WIFEXITED(status) && WEXITSTATUS(status) == 0);
>       return 0;
>     }
> 
> Fix this by updating follow_trans_huge_pmd in huge_memory.c analogously
> to the update in gup.c in the original commit.  The same pattern exists
> in follow_devmap_pmd.  However, we should not be able to reach that
> check with FOLL_COW set, so add WARN_ONCE to make sure we notice if we
> ever do.
> 
> [akpm@linux-foundation.org: coding-style fixes]
> Link: http://lkml.kernel.org/r/20170106015025.GA38411@juliacomputing.com
> Signed-off-by: Keno Fischer <keno@juliacomputing.com>
> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> Cc: Greg Thelen <gthelen@google.com>
> Cc: Nicholas Piggin <npiggin@gmail.com>
> Cc: Willy Tarreau <w@1wt.eu>
> Cc: Oleg Nesterov <oleg@redhat.com>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: Andy Lutomirski <luto@kernel.org>
> Cc: Michal Hocko <mhocko@suse.com>
> Cc: Hugh Dickins <hughd@google.com>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
> [bwh: Backported to 3.16:
>  - Drop change to follow_devmap_pmd()
>  - pmd_dirty() is not available; check the page flags as in older
>    backports of can_follow_write_pte()
>  - Adjust context]
> Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> 
> ---
>  mm/huge_memory.c |   19 ++++++++++++++++---
>  1 file changed, 16 insertions(+), 3 deletions(-)
> 
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -1269,6 +1269,18 @@ out_unlock:
>  	return ret;
>  }
>  
> +/*
> + * FOLL_FORCE can write to even unwritable pmd's, but only
> + * after we've gone through a COW cycle and they are dirty.
> + */
> +static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page,
> +					unsigned int flags)
> +{
> +	return pmd_write(pmd) ||
> +		((flags & FOLL_FORCE) && (flags & FOLL_COW) &&
> +		 page && PageAnon(page));
> +}
> +
>  struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
>  				   unsigned long addr,
>  				   pmd_t *pmd,
> @@ -1279,9 +1291,6 @@ struct page *follow_trans_huge_pmd(struc
>  
>  	assert_spin_locked(pmd_lockptr(mm, pmd));
>  
> -	if (flags & FOLL_WRITE && !pmd_write(*pmd))
> -		goto out;
> -
>  	/* Avoid dumping huge zero page */
>  	if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
>  		return ERR_PTR(-EFAULT);
> @@ -1292,6 +1301,10 @@ struct page *follow_trans_huge_pmd(struc
>  
>  	page = pmd_page(*pmd);
>  	VM_BUG_ON_PAGE(!PageHead(page), page);
> +
> +	if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, page, flags))
> +		goto out;
> +
>  	if (flags & FOLL_TOUCH) {
>  		pmd_t _pmd;
>  		/*
> 
> 
> Patches currently in stable-queue which might be from keno@juliacomputing.com are
> 
> queue-4.4/mm-huge_memory.c-respect-foll_force-foll_cow-for-thp.patch

-- 
Michal Hocko
SUSE Labs

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

* Patch "mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp" has been added to the 4.4-stable tree
@ 2017-04-10 15:09 gregkh
  2017-04-10 15:34 ` Michal Hocko
  2017-04-10 15:48 ` Ben Hutchings
  0 siblings, 2 replies; 10+ messages in thread
From: gregkh @ 2017-04-10 15:09 UTC (permalink / raw)
  To: keno, akpm, ben, gregkh, gthelen, hughd, keescook,
	kirill.shutemov, luto, mhocko, npiggin, oleg, torvalds, w
  Cc: stable, stable-commits


This is a note to let you know that I've just added the patch titled

    mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp

to the 4.4-stable tree which can be found at:
    http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     mm-huge_memory.c-respect-foll_force-foll_cow-for-thp.patch
and it can be found in the queue-4.4 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <stable@vger.kernel.org> know about it.


>From 303681d5d538d81b5e23754515202b5b9febd2e9 Mon Sep 17 00:00:00 2001
From: Keno Fischer <keno@juliacomputing.com>
Date: Tue, 24 Jan 2017 15:17:48 -0800
Subject: mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp

From: Keno Fischer <keno@juliacomputing.com>

commit 8310d48b125d19fcd9521d83b8293e63eb1646aa upstream.

In commit 19be0eaffa3a ("mm: remove gup_flags FOLL_WRITE games from
__get_user_pages()"), the mm code was changed from unsetting FOLL_WRITE
after a COW was resolved to setting the (newly introduced) FOLL_COW
instead.  Simultaneously, the check in gup.c was updated to still allow
writes with FOLL_FORCE set if FOLL_COW had also been set.

However, a similar check in huge_memory.c was forgotten.  As a result,
remote memory writes to ro regions of memory backed by transparent huge
pages cause an infinite loop in the kernel (handle_mm_fault sets
FOLL_COW and returns 0 causing a retry, but follow_trans_huge_pmd bails
out immidiately because `(flags & FOLL_WRITE) && !pmd_write(*pmd)` is
true.

While in this state the process is stil SIGKILLable, but little else
works (e.g.  no ptrace attach, no other signals).  This is easily
reproduced with the following code (assuming thp are set to always):

    #include <assert.h>
    #include <fcntl.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/mman.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>

    #define TEST_SIZE 5 * 1024 * 1024

    int main(void) {
      int status;
      pid_t child;
      int fd = open("/proc/self/mem", O_RDWR);
      void *addr = mmap(NULL, TEST_SIZE, PROT_READ,
                        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
      assert(addr != MAP_FAILED);
      pid_t parent_pid = getpid();
      if ((child = fork()) == 0) {
        void *addr2 = mmap(NULL, TEST_SIZE, PROT_READ | PROT_WRITE,
                           MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
        assert(addr2 != MAP_FAILED);
        memset(addr2, 'a', TEST_SIZE);
        pwrite(fd, addr2, TEST_SIZE, (uintptr_t)addr);
        return 0;
      }
      assert(child == waitpid(child, &status, 0));
      assert(WIFEXITED(status) && WEXITSTATUS(status) == 0);
      return 0;
    }

Fix this by updating follow_trans_huge_pmd in huge_memory.c analogously
to the update in gup.c in the original commit.  The same pattern exists
in follow_devmap_pmd.  However, we should not be able to reach that
check with FOLL_COW set, so add WARN_ONCE to make sure we notice if we
ever do.

[akpm@linux-foundation.org: coding-style fixes]
Link: http://lkml.kernel.org/r/20170106015025.GA38411@juliacomputing.com
Signed-off-by: Keno Fischer <keno@juliacomputing.com>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Willy Tarreau <w@1wt.eu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
[bwh: Backported to 3.16:
 - Drop change to follow_devmap_pmd()
 - pmd_dirty() is not available; check the page flags as in older
   backports of can_follow_write_pte()
 - Adjust context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 mm/huge_memory.c |   19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1269,6 +1269,18 @@ out_unlock:
 	return ret;
 }
 
+/*
+ * FOLL_FORCE can write to even unwritable pmd's, but only
+ * after we've gone through a COW cycle and they are dirty.
+ */
+static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page,
+					unsigned int flags)
+{
+	return pmd_write(pmd) ||
+		((flags & FOLL_FORCE) && (flags & FOLL_COW) &&
+		 page && PageAnon(page));
+}
+
 struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
 				   unsigned long addr,
 				   pmd_t *pmd,
@@ -1279,9 +1291,6 @@ struct page *follow_trans_huge_pmd(struc
 
 	assert_spin_locked(pmd_lockptr(mm, pmd));
 
-	if (flags & FOLL_WRITE && !pmd_write(*pmd))
-		goto out;
-
 	/* Avoid dumping huge zero page */
 	if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
 		return ERR_PTR(-EFAULT);
@@ -1292,6 +1301,10 @@ struct page *follow_trans_huge_pmd(struc
 
 	page = pmd_page(*pmd);
 	VM_BUG_ON_PAGE(!PageHead(page), page);
+
+	if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, page, flags))
+		goto out;
+
 	if (flags & FOLL_TOUCH) {
 		pmd_t _pmd;
 		/*


Patches currently in stable-queue which might be from keno@juliacomputing.com are

queue-4.4/mm-huge_memory.c-respect-foll_force-foll_cow-for-thp.patch

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

end of thread, other threads:[~2017-05-23 14:49 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-23 14:46 Patch "mm/huge_memory.c: respect FOLL_FORCE/FOLL_COW for thp" has been added to the 4.4-stable tree gregkh
  -- strict thread matches above, loose matches on Subject: below --
2017-04-10 15:09 gregkh
2017-04-10 15:34 ` Michal Hocko
2017-04-10 16:05   ` Greg KH
2017-04-10 16:13     ` Michal Hocko
2017-04-10 15:48 ` Ben Hutchings
2017-04-10 16:04   ` Greg KH
2017-04-10 16:13     ` Michal Hocko
2017-04-10 16:21       ` Greg KH
2017-04-10 18:10     ` Ben Hutchings

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.