Re: [PATCH v2 2/3] userfaultfd: UFFDIO_REMAP uABI

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Sep 27, 2023 at 1:04 PM Jann Horn <jannh@xxxxxxxxxx> wrote:
>
> On Wed, Sep 27, 2023 at 8:08 PM Suren Baghdasaryan <surenb@xxxxxxxxxx> wrote:
> > On Wed, Sep 27, 2023 at 5:47 AM Jann Horn <jannh@xxxxxxxxxx> wrote:
> > > On Sat, Sep 23, 2023 at 3:31 AM Suren Baghdasaryan <surenb@xxxxxxxxxx> wrote:
> > > > From: Andrea Arcangeli <aarcange@xxxxxxxxxx>
> > > >
> > > > 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@xxxxxxxxxx>
> > > > Signed-off-by: Suren Baghdasaryan <surenb@xxxxxxxxxx>
> [...]
> > > > +                       /*
> > > > +                        * 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...

Yes, that's what I was thinking but I agree, that seems fragile. Maybe
just bail out early if (ptl && !pmd_trans_huge())?

>
> (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)





[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux