linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Peter Xu <peterx@redhat.com>
Cc: Linux-MM <linux-mm@kvack.org>,
	 Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	 Marty Mcfadden <mcfadden8@llnl.gov>,
	"Maya B . Gokhale" <gokhale2@llnl.gov>,
	 Andrea Arcangeli <aarcange@redhat.com>,
	Jann Horn <jannh@google.com>, Christoph Hellwig <hch@lst.de>,
	 Oleg Nesterov <oleg@redhat.com>,
	Kirill Shutemov <kirill@shutemov.name>, Jan Kara <jack@suse.cz>
Subject: Re: [PATCH v3] mm/gup: Allow real explicit breaking of COW
Date: Tue, 11 Aug 2020 12:24:20 -0700	[thread overview]
Message-ID: <CAHk-=whQM=m5td5tfbuxh1f_Gxjsa74XV962BYkjrbeDMAhBpA@mail.gmail.com> (raw)
In-Reply-To: <20200811183950.10603-1-peterx@redhat.com>

On Tue, Aug 11, 2020 at 11:40 AM Peter Xu <peterx@redhat.com> wrote:
>
> index 206f52b36ffb..c88f773d03af 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -1296,7 +1296,17 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd)
>         if (reuse_swap_page(page, NULL)) {
>                 pmd_t entry;
>                 entry = pmd_mkyoung(orig_pmd);
> -               entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
> +               entry = pmd_mkdirty(entry);
> +               if (pmd_uffd_wp(orig_pmd))
> +                       /*
> +                        * This can happen when an uffd-wp protected page is
> +                        * copied due to enfornced COW.  When it happens, we
> +                        * need to keep the uffd-wp bit even after COW, and
> +                        * make sure write bit is kept cleared.
> +                        */
> +                       entry = pmd_mkuffd_wp(pmd_wrprotect(entry));
> +               else
> +                       entry = maybe_pmd_mkwrite(entry, vma);
>                 if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1))
>                         update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
>                 unlock_page(page);
> diff --git a/mm/memory.c b/mm/memory.c
> index c39a13b09602..b27b555a9df8 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -2706,7 +2706,17 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
>                 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
>                 entry = mk_pte(new_page, vma->vm_page_prot);
>                 entry = pte_sw_mkyoung(entry);
> -               entry = maybe_mkwrite(pte_mkdirty(entry), vma);
> +               entry = pte_mkdirty(entry);
> +               if (pte_uffd_wp(vmf->orig_pte))
> +                       /*
> +                        * This can happen when an uffd-wp protected page is
> +                        * copied due to enfornced COW.  When it happens, we
> +                        * need to keep the uffd-wp bit even after COW, and
> +                        * make sure write bit is kept cleared.
> +                        */
> +                       entry = pte_mkuffd_wp(pte_wrprotect(entry));
> +               else
> +                       entry = maybe_mkwrite(entry, vma);
>                 /*
>                  * Clear the pte entry and flush it first, before updating the
>                  * pte with the new entry. This will avoid a race condition

I think this needs to be cleaned up some way. I realize it's not an
exact duplicate (pmd vs pte), but this code is illegible.

Maybe just making it a helper inline function (well, two separate
ones) with the comment above the function would resolve my "this is
very ugly" concerns.


> @@ -2900,7 +2910,13 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf)
>  {
>         struct vm_area_struct *vma = vmf->vma;
>
> -       if (userfaultfd_pte_wp(vma, *vmf->pte)) {
> +       /*
> +        * Userfaultfd-wp only cares about real writes.  E.g., enforced COW for
> +        * read does not count.  When that happens, we will do the COW with the
> +        * UFFD_WP bit inherited from the original PTE/PMD.
> +        */
> +       if ((vmf->flags & FAULT_FLAG_WRITE) &&
> +           userfaultfd_pte_wp(vma, *vmf->pte)) {
>                 pte_unmap_unlock(vmf->pte, vmf->ptl);
>                 return handle_userfault(vmf, VM_UFFD_WP);
>         }
> @@ -4117,7 +4133,14 @@ static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf)
>  static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd)
>  {
>         if (vma_is_anonymous(vmf->vma)) {
> -               if (userfaultfd_huge_pmd_wp(vmf->vma, orig_pmd))
> +               /*
> +                * Userfaultfd-wp only cares about real writes.  E.g., enforced
> +                * COW for read does not count.  When that happens, we will do
> +                * the COW with the UFFD_WP bit inherited from the original
> +                * PTE/PMD.
> +                */
> +               if ((vmf->flags & FAULT_FLAG_WRITE) &&
> +                   userfaultfd_huge_pmd_wp(vmf->vma, orig_pmd))
>                         return handle_userfault(vmf, VM_UFFD_WP);

Here again the comment placement could be improved. Particularly in
the do_wp_page() case, we have a big and somewhat complex function,
and this duplicated boiler-plate makes me worry.

Making it a helper function with a comment above would again I think
make it more legible.

And I think Jann is on the money wrt the follow_page_pte() issue.

I think you broke COW break there entirely.

That was one of the reasons I did just that "make it use FOLL_WRITE"
originally, because it meant that we couldn't have any subtle places
we'd missed.

Now I wonder if there's any other case of FOLL_WRITE that is missing.

            Linus


  parent reply	other threads:[~2020-08-11 19:24 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-11 18:39 [PATCH v3] mm/gup: Allow real explicit breaking of COW Peter Xu
2020-08-11 19:07 ` Jann Horn
2020-08-11 20:02   ` Peter Xu
2020-08-11 20:22     ` Jann Horn
2020-08-11 21:23       ` Peter Xu
2020-08-11 19:24 ` Linus Torvalds [this message]
2020-08-11 20:06   ` Linus Torvalds
2020-08-11 20:46     ` Linus Torvalds
2020-08-11 21:42       ` Peter Xu
2020-08-11 23:10         ` Linus Torvalds
2020-08-20 21:54           ` Peter Xu
2020-08-20 22:01             ` Linus Torvalds
2020-08-21  2:34               ` Peter Xu
2020-08-21 10:13               ` Jan Kara
2020-08-21 12:27                 ` Linus Torvalds
2020-08-21 15:47                   ` Jan Kara
2020-08-21 17:00                     ` Linus Torvalds
2020-08-21 18:08                       ` Peter Xu
2020-08-21 18:23                         ` Linus Torvalds
2020-08-21 19:05                           ` Linus Torvalds
2020-08-21 19:06                             ` Linus Torvalds
2020-08-21 19:31                           ` Peter Xu
2020-08-21 19:42                             ` Linus Torvalds

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='CAHk-=whQM=m5td5tfbuxh1f_Gxjsa74XV962BYkjrbeDMAhBpA@mail.gmail.com' \
    --to=torvalds@linux-foundation.org \
    --cc=aarcange@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=gokhale2@llnl.gov \
    --cc=hch@lst.de \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=kirill@shutemov.name \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mcfadden8@llnl.gov \
    --cc=oleg@redhat.com \
    --cc=peterx@redhat.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).