Merge branch 'bugfixes' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[sfrench/cifs-2.6.git] / mm / rmap.c
index 23ecd0a892df600665f618aabd1ba2f522bc6387..4bad3267537a8c7826d313afa661a03f51b2df26 100644 (file)
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -182,7 +182,7 @@ int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src)
 {
        struct anon_vma_chain *avc, *pavc;
 
-       list_for_each_entry(pavc, &src->anon_vma_chain, same_vma) {
+       list_for_each_entry_reverse(pavc, &src->anon_vma_chain, same_vma) {
                avc = anon_vma_chain_alloc();
                if (!avc)
                        goto enomem_failure;
@@ -232,6 +232,7 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)
  out_error_free_anon_vma:
        anon_vma_free(anon_vma);
  out_error:
+       unlink_anon_vmas(vma);
        return -ENOMEM;
 }
 
@@ -329,18 +330,6 @@ vma_address(struct page *page, struct vm_area_struct *vma)
                /* page should be within @vma mapping range */
                return -EFAULT;
        }
-       if (unlikely(vma->vm_flags & VM_LOCK_RMAP)) {
-               /*
-                * This VMA is being unlinked or is not yet linked into the
-                * VMA tree.  Do not try to follow this rmap.  This race
-                * condition can result in page_referenced() ignoring a
-                * reference or in try_to_unmap() failing to unmap a page.
-                * The VMA cannot be freed under us because we hold the
-                * anon_vma->lock, which the munmap code takes while
-                * unlinking the anon_vmas from the VMA.
-                */
-               return -EFAULT;
-       }
        return address;
 }
 
@@ -613,9 +602,6 @@ int page_referenced(struct page *page,
        int referenced = 0;
        int we_locked = 0;
 
-       if (TestClearPageReferenced(page))
-               referenced++;
-
        *vm_flags = 0;
        if (page_mapped(page) && page_rmapping(page)) {
                if (!is_locked && (!PageAnon(page) || PageKsm(page))) {
@@ -715,6 +701,30 @@ int page_mkclean(struct page *page)
 }
 EXPORT_SYMBOL_GPL(page_mkclean);
 
+/**
+ * page_move_anon_rmap - move a page to our anon_vma
+ * @page:      the page to move to our anon_vma
+ * @vma:       the vma the page belongs to
+ * @address:   the user virtual address mapped
+ *
+ * When a page belongs exclusively to one process after a COW event,
+ * that page can be moved into the anon_vma that belongs to just that
+ * process, so the rmap code will not search the parent or sibling
+ * processes.
+ */
+void page_move_anon_rmap(struct page *page,
+       struct vm_area_struct *vma, unsigned long address)
+{
+       struct anon_vma *anon_vma = vma->anon_vma;
+
+       VM_BUG_ON(!PageLocked(page));
+       VM_BUG_ON(!anon_vma);
+       VM_BUG_ON(page->index != linear_page_index(vma, address));
+
+       anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
+       page->mapping = (struct address_space *) anon_vma;
+}
+
 /**
  * __page_set_anon_rmap - setup new anonymous rmap
  * @page:      the page to add the mapping to
@@ -724,9 +734,20 @@ EXPORT_SYMBOL_GPL(page_mkclean);
 static void __page_set_anon_rmap(struct page *page,
        struct vm_area_struct *vma, unsigned long address)
 {
-       struct anon_vma *anon_vma = vma->anon_vma;
+       struct anon_vma_chain *avc;
+       struct anon_vma *anon_vma;
+
+       BUG_ON(!vma->anon_vma);
+
+       /*
+        * We must use the _oldest_ possible anon_vma for the page mapping!
+        *
+        * So take the last AVC chain entry in the vma, which is the deepest
+        * ancestor, and use the anon_vma from that.
+        */
+       avc = list_entry(vma->anon_vma_chain.prev, struct anon_vma_chain, same_vma);
+       anon_vma = avc->anon_vma;
 
-       BUG_ON(!anon_vma);
        anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
        page->mapping = (struct address_space *) anon_vma;
        page->index = linear_page_index(vma, address);