linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Jann Horn <jannh@google.com>
To: Suren Baghdasaryan <surenb@google.com>
Cc: akpm@linux-foundation.org, viro@zeniv.linux.org.uk,
	brauner@kernel.org,  shuah@kernel.org, aarcange@redhat.com,
	lokeshgidra@google.com,  peterx@redhat.com, david@redhat.com,
	hughd@google.com, mhocko@suse.com,  axelrasmussen@google.com,
	rppt@kernel.org, willy@infradead.org,  Liam.Howlett@oracle.com,
	zhangpeng362@huawei.com, bgeffon@google.com,
	 kaleshsingh@google.com, ngeoffray@google.com, jdduke@google.com,
	 linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
	 linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	 kernel-team@android.com
Subject: Re: [PATCH v2 2/3] userfaultfd: UFFDIO_REMAP uABI
Date: Wed, 27 Sep 2023 22:04:06 +0200	[thread overview]
Message-ID: <CAG48ez2WNOMwPo4OMVUHbS4mirwbqHUY5qUaaZ9DTkXdkzrjiQ@mail.gmail.com> (raw)
In-Reply-To: <CAJuCfpGb5Amo9Sk0yyruJt9NKaYe9-y+5jmU442NSf3+VT5-dA@mail.gmail.com>

On Wed, Sep 27, 2023 at 8:08 PM Suren Baghdasaryan <surenb@google.com> wrote:
> On Wed, Sep 27, 2023 at 5:47 AM Jann Horn <jannh@google.com> wrote:
> > On Sat, Sep 23, 2023 at 3:31 AM Suren Baghdasaryan <surenb@google.com> wrote:
> > > From: Andrea Arcangeli <aarcange@redhat.com>
> > >
> > > This implements the uABI of UFFDIO_REMAP.
> > >
> > > Notably one mode bitflag is also forwarded (and in turn known) by the
> > > lowlevel remap_pages method.
> > >
> > > Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
> > > Signed-off-by: Suren Baghdasaryan <surenb@google.com>
[...]
> > > +                       /*
> > > +                        * folio_referenced walks the anon_vma chain
> > > +                        * without the folio lock. Serialize against it with
> > > +                        * the anon_vma lock, the folio lock is not enough.
> > > +                        */
> > > +                       src_anon_vma = folio_get_anon_vma(src_folio);
> > > +                       if (!src_anon_vma) {
> > > +                               /* page was unmapped from under us */
> > > +                               err = -EAGAIN;
> > > +                               goto out;
> > > +                       }
> > > +                       if (!anon_vma_trylock_write(src_anon_vma)) {
> > > +                               pte_unmap(&orig_src_pte);
> > > +                               pte_unmap(&orig_dst_pte);
> > > +                               src_pte = dst_pte = NULL;
> > > +                               /* now we can block and wait */
> > > +                               anon_vma_lock_write(src_anon_vma);
> > > +                               goto retry;
> > > +                       }
> > > +               }
> >
> > So at this point we have:
> >
> >  - the current src_pte
> >  - some referenced+locked src_folio that used to be mapped exclusively
> > at src_addr
> >  - (the anon_vma associated with the src_folio)
> >
> > > +               err = remap_anon_pte(dst_mm, src_mm,  dst_vma, src_vma,
> > > +                                    dst_addr, src_addr, dst_pte, src_pte,
> > > +                                    orig_dst_pte, orig_src_pte,
> > > +                                    dst_ptl, src_ptl, src_folio);
> >
> > And then this will, without touching folio mapcounts/refcounts, delete
> > the current PTE at src_addr, and create a PTE at dst_addr pointing to
> > the old src_folio, leading to incorrect refcounts/mapcounts?
>
> I assume this still points to the missing previous_src_pte check
> discussed in the previous comments. Is that correct or is there yet
> another issue?

This is still referring to the missing previous_src_pte check.

> >
> > > +       } else {
> > [...]
> > > +       }
> > > +
> > > +out:
> > > +       if (src_anon_vma) {
> > > +               anon_vma_unlock_write(src_anon_vma);
> > > +               put_anon_vma(src_anon_vma);
> > > +       }
> > > +       if (src_folio) {
> > > +               folio_unlock(src_folio);
> > > +               folio_put(src_folio);
> > > +       }
> > > +       if (dst_pte)
> > > +               pte_unmap(dst_pte);
> > > +       if (src_pte)
> > > +               pte_unmap(src_pte);
> > > +       mmu_notifier_invalidate_range_end(&range);
> > > +
> > > +       return err;
> > > +}
> > [...]
> > > +ssize_t remap_pages(struct mm_struct *dst_mm, struct mm_struct *src_mm,
> > > +                   unsigned long dst_start, unsigned long src_start,
> > > +                   unsigned long len, __u64 mode)
> > > +{
> > > +       struct vm_area_struct *src_vma, *dst_vma;
> > > +       unsigned long src_addr, dst_addr;
> > > +       pmd_t *src_pmd, *dst_pmd;
> > > +       long err = -EINVAL;
> > > +       ssize_t moved = 0;
> > > +
> > > +       /*
> > > +        * Sanitize the command parameters:
> > > +        */
> > > +       BUG_ON(src_start & ~PAGE_MASK);
> > > +       BUG_ON(dst_start & ~PAGE_MASK);
> > > +       BUG_ON(len & ~PAGE_MASK);
> > > +
> > > +       /* Does the address range wrap, or is the span zero-sized? */
> > > +       BUG_ON(src_start + len <= src_start);
> > > +       BUG_ON(dst_start + len <= dst_start);
> > > +
> > > +       /*
> > > +        * Because these are read sempahores there's no risk of lock
> > > +        * inversion.
> > > +        */
> > > +       mmap_read_lock(dst_mm);
> > > +       if (dst_mm != src_mm)
> > > +               mmap_read_lock(src_mm);
> > > +
> > > +       /*
> > > +        * Make sure the vma is not shared, that the src and dst remap
> > > +        * ranges are both valid and fully within a single existing
> > > +        * vma.
> > > +        */
> > > +       src_vma = find_vma(src_mm, src_start);
> > > +       if (!src_vma || (src_vma->vm_flags & VM_SHARED))
> > > +               goto out;
> > > +       if (src_start < src_vma->vm_start ||
> > > +           src_start + len > src_vma->vm_end)
> > > +               goto out;
> > > +
> > > +       dst_vma = find_vma(dst_mm, dst_start);
> > > +       if (!dst_vma || (dst_vma->vm_flags & VM_SHARED))
> > > +               goto out;
> > > +       if (dst_start < dst_vma->vm_start ||
> > > +           dst_start + len > dst_vma->vm_end)
> > > +               goto out;
> > > +
> > > +       err = validate_remap_areas(src_vma, dst_vma);
> > > +       if (err)
> > > +               goto out;
> > > +
> > > +       for (src_addr = src_start, dst_addr = dst_start;
> > > +            src_addr < src_start + len;) {
> > > +               spinlock_t *ptl;
> > > +               pmd_t dst_pmdval;
> > > +               unsigned long step_size;
> > > +
> > > +               BUG_ON(dst_addr >= dst_start + len);
> > > +               /*
> > > +                * Below works because anonymous area would not have a
> > > +                * transparent huge PUD. If file-backed support is added,
> > > +                * that case would need to be handled here.
> > > +                */
> > > +               src_pmd = mm_find_pmd(src_mm, src_addr);
> > > +               if (unlikely(!src_pmd)) {
> > > +                       if (!(mode & UFFDIO_REMAP_MODE_ALLOW_SRC_HOLES)) {
> > > +                               err = -ENOENT;
> > > +                               break;
> > > +                       }
> > > +                       src_pmd = mm_alloc_pmd(src_mm, src_addr);
> > > +                       if (unlikely(!src_pmd)) {
> > > +                               err = -ENOMEM;
> > > +                               break;
> > > +                       }
> > > +               }
> > > +               dst_pmd = mm_alloc_pmd(dst_mm, dst_addr);
> > > +               if (unlikely(!dst_pmd)) {
> > > +                       err = -ENOMEM;
> > > +                       break;
> > > +               }
> > > +
> > > +               dst_pmdval = pmdp_get_lockless(dst_pmd);
> > > +               /*
> > > +                * If the dst_pmd is mapped as THP don't override it and just
> > > +                * be strict. If dst_pmd changes into TPH after this check, the
> > > +                * remap_pages_huge_pmd() will detect the change and retry
> > > +                * while remap_pages_pte() will detect the change and fail.
> > > +                */
> > > +               if (unlikely(pmd_trans_huge(dst_pmdval))) {
> > > +                       err = -EEXIST;
> > > +                       break;
> > > +               }
> > > +
> > > +               ptl = pmd_trans_huge_lock(src_pmd, src_vma);
> > > +               if (ptl && !pmd_trans_huge(*src_pmd)) {
> > > +                       spin_unlock(ptl);
> > > +                       ptl = NULL;
> > > +               }
> >
> > This still looks wrong - we do still have to split_huge_pmd()
> > somewhere so that remap_pages_pte() works.
>
> Hmm, I guess this extra check is not even needed...

Hm, and instead we'd bail at the pte_offset_map_nolock() in
remap_pages_pte()? I guess that's unusual but works...

(It would be a thing to look out for if anyone tried to backport this,
since the checks in pte_offset_map_nolock() were only introduced in
6.5, but idk if anyone's doing that)


  reply	other threads:[~2023-09-27 20:04 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-23  1:31 [PATCH v2 0/3] userfaultfd remap option Suren Baghdasaryan
2023-09-23  1:31 ` [PATCH v2 1/3] userfaultfd: UFFDIO_REMAP: rmap preparation Suren Baghdasaryan
2023-09-28 16:23   ` Peter Xu
2023-09-28 20:03     ` Suren Baghdasaryan
2023-10-02 14:42   ` David Hildenbrand
2023-10-02 15:23     ` Peter Xu
2023-10-02 17:30       ` David Hildenbrand
2023-10-03 17:56         ` Suren Baghdasaryan
2023-09-23  1:31 ` [PATCH v2 2/3] userfaultfd: UFFDIO_REMAP uABI Suren Baghdasaryan
2023-09-27 10:06   ` potential new userfaultfd vs khugepaged conflict [was: Re: [PATCH v2 2/3] userfaultfd: UFFDIO_REMAP uABI] Jann Horn
2023-09-27 17:12     ` Suren Baghdasaryan
2023-09-28 15:29       ` Jann Horn
2023-09-27 12:47   ` [PATCH v2 2/3] userfaultfd: UFFDIO_REMAP uABI Jann Horn
2023-09-27 13:29     ` David Hildenbrand
2023-09-27 18:25       ` Suren Baghdasaryan
2023-09-28 16:28         ` Peter Xu
2023-09-28 17:15         ` David Hildenbrand
2023-09-28 18:32           ` Suren Baghdasaryan
2023-09-28 20:11             ` Suren Baghdasaryan
2023-09-28 19:00           ` Peter Xu
2023-10-02  7:49             ` David Hildenbrand
2023-09-28 16:24       ` Peter Xu
2023-09-28 17:05         ` David Hildenbrand
2023-09-28 17:21           ` Peter Xu
2023-09-28 17:51             ` David Hildenbrand
2023-09-28 18:34               ` Peter Xu
2023-09-28 19:47                 ` Suren Baghdasaryan
2023-10-02  8:00                 ` David Hildenbrand
2023-10-02 15:21                   ` Peter Xu
2023-10-02 15:46                     ` Lokesh Gidra
2023-10-02 15:55                       ` Lokesh Gidra
2023-10-02 17:43                         ` David Hildenbrand
2023-10-02 19:33                           ` Lokesh Gidra
2023-10-03 20:04                             ` Suren Baghdasaryan
2023-10-03 20:21                               ` Peter Xu
2023-10-03 21:08                                 ` David Hildenbrand
2023-10-03 21:20                                   ` Peter Xu
2023-10-03 22:26                                     ` Suren Baghdasaryan
2023-10-03 23:39                                       ` Lokesh Gidra
2023-10-06 12:30                                         ` David Hildenbrand
2023-10-06 15:02                                           ` Suren Baghdasaryan
2023-10-03 21:04                               ` David Hildenbrand
2023-10-02 17:33                     ` David Hildenbrand
2023-10-02 17:36                       ` David Hildenbrand
2023-09-27 18:07     ` Suren Baghdasaryan
2023-09-27 20:04       ` Jann Horn [this message]
2023-09-27 20:42         ` Suren Baghdasaryan
2023-09-27 21:08           ` Suren Baghdasaryan
2023-09-27 22:48             ` Jann Horn
2023-09-28 15:36               ` Suren Baghdasaryan
2023-09-28 17:09   ` Peter Xu
2023-09-28 18:23     ` Suren Baghdasaryan
2023-09-28 18:43   ` Peter Xu
2023-09-28 19:50     ` Suren Baghdasaryan
2023-09-23  1:31 ` [PATCH v2 3/3] selftests/mm: add UFFDIO_REMAP ioctl test Suren Baghdasaryan

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=CAG48ez2WNOMwPo4OMVUHbS4mirwbqHUY5qUaaZ9DTkXdkzrjiQ@mail.gmail.com \
    --to=jannh@google.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=aarcange@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=axelrasmussen@google.com \
    --cc=bgeffon@google.com \
    --cc=brauner@kernel.org \
    --cc=david@redhat.com \
    --cc=hughd@google.com \
    --cc=jdduke@google.com \
    --cc=kaleshsingh@google.com \
    --cc=kernel-team@android.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lokeshgidra@google.com \
    --cc=mhocko@suse.com \
    --cc=ngeoffray@google.com \
    --cc=peterx@redhat.com \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --cc=surenb@google.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    --cc=zhangpeng362@huawei.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