Message ID | 20230914152620.2743033-2-surenb@google.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | userfaultfd remap option | expand |
On Thu, Sep 14, 2023 at 08:26:11AM -0700, Suren Baghdasaryan wrote: > From: Andrea Arcangeli <aarcange@redhat.com> > > As far as the rmap code is concerned, UFFDIO_REMAP only alters the > page->mapping and page->index. It does it while holding the page folio->mapping and folio->index. It does it while holding the folio No other objections ...
On Thu, Sep 14, 2023 at 10:56 AM Matthew Wilcox <willy@infradead.org> wrote: > > On Thu, Sep 14, 2023 at 08:26:11AM -0700, Suren Baghdasaryan wrote: > > From: Andrea Arcangeli <aarcange@redhat.com> > > > > As far as the rmap code is concerned, UFFDIO_REMAP only alters the > > page->mapping and page->index. It does it while holding the page > > folio->mapping and folio->index. It does it while holding the folio Ack. > > No other objections ... Thanks for the review!
diff --git a/mm/rmap.c b/mm/rmap.c index ec7f8e6c9e48..c1ebbd23fa61 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -542,6 +542,7 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio, struct anon_vma *root_anon_vma; unsigned long anon_mapping; +repeat: rcu_read_lock(); anon_mapping = (unsigned long)READ_ONCE(folio->mapping); if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) @@ -586,6 +587,18 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio, rcu_read_unlock(); anon_vma_lock_read(anon_vma); + /* + * Check if UFFDIO_REMAP changed the anon_vma. This is needed + * because we don't assume the folio was locked. + */ + if (unlikely((unsigned long) READ_ONCE(folio->mapping) != + anon_mapping)) { + anon_vma_unlock_read(anon_vma); + put_anon_vma(anon_vma); + anon_vma = NULL; + goto repeat; + } + if (atomic_dec_and_test(&anon_vma->refcount)) { /* * Oops, we held the last refcount, release the lock