aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorAndrea Arcangeli <aarcange@redhat.com>2017-09-20 20:06:58 +0200
committerAndrea Arcangeli <aarcange@redhat.com>2023-11-11 22:03:37 -0500
commite80c1814c1da8a268e539ebbabd190cc943a69a5 (patch)
treea39f0c31206188b399cc9552ad81b913cb0b1918
parent373825d3dd551ce5f15d20542ce965794b27c060 (diff)
downloadaa-e80c1814c1da8a268e539ebbabd190cc943a69a5.tar.gz
userfaultfd: UFFDIO_REMAP: rmap preparation
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 lock. However page_referenced() is doing rmap walks without taking the page lock first, so page_lock_anon_vma_read must be updated to re-check that the page->mapping didn't change after we obtained the anon_vma read lock. UFFDIO_REMAP takes the anon_vma lock for writing before altering the page->mapping, so if the page->mapping is still the same after obtaining the anon_vma read lock (without the page lock), the rmap walks can go ahead safely (and UFFDIO_REMAP will wait the rmap walk to complete before proceeding). UFFDIO_REMAP serializes against itself with the page lock. All other places taking the anon_vma lock while holding the mmap_sem for writing, don't need to check if the page->mapping has changed after taking the anon_vma lock, regardless of the page lock, because UFFDIO_REMAP holds the mmap_sem for reading. There's one constraint enforced to allow this simplification: the source pages passed to UFFDIO_REMAP must be mapped only in one vma, but this constraint is an acceptable tradeoff for UFFDIO_REMAP users. The source addresses passed to UFFDIO_REMAP should be set as VM_DONTCOPY with MADV_DONTFORK to avoid any risk of the mapcount of the pages increasing if some thread of the process forks() before UFFDIO_REMAP run. Acked-by: Pavel Emelyanov <xemul@virtuozzo.com> Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
-rw-r--r--mm/rmap.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/mm/rmap.c b/mm/rmap.c
index 7f7320259afbc9..cd737e02619e31 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -535,6 +535,7 @@ struct anon_vma *page_lock_anon_vma_read(struct page *page)
struct anon_vma *root_anon_vma;
unsigned long anon_mapping;
+repeat:
rcu_read_lock();
anon_mapping = (unsigned long)READ_ONCE(page->mapping);
if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
@@ -573,6 +574,18 @@ struct anon_vma *page_lock_anon_vma_read(struct page *page)
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 page was locked.
+ */
+ if (unlikely((unsigned long) READ_ONCE(page->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