Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[pandora-kernel.git] / mm / mprotect.c
index 4c14d42..638edab 100644 (file)
@@ -19,7 +19,8 @@
 #include <linux/mempolicy.h>
 #include <linux/personality.h>
 #include <linux/syscalls.h>
-
+#include <linux/swap.h>
+#include <linux/swapops.h>
 #include <asm/uaccess.h>
 #include <asm/pgtable.h>
 #include <asm/cacheflush.h>
 static void change_pte_range(struct mm_struct *mm, pmd_t *pmd,
                unsigned long addr, unsigned long end, pgprot_t newprot)
 {
-       pte_t *pte;
+       pte_t *pte, oldpte;
        spinlock_t *ptl;
 
        pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
        do {
-               if (pte_present(*pte)) {
+               oldpte = *pte;
+               if (pte_present(oldpte)) {
                        pte_t ptent;
 
                        /* Avoid an SMP race with hardware updated dirty/clean
@@ -43,7 +45,22 @@ static void change_pte_range(struct mm_struct *mm, pmd_t *pmd,
                        ptent = pte_modify(ptep_get_and_clear(mm, addr, pte), newprot);
                        set_pte_at(mm, addr, pte, ptent);
                        lazy_mmu_prot_update(ptent);
+#ifdef CONFIG_MIGRATION
+               } else if (!pte_file(oldpte)) {
+                       swp_entry_t entry = pte_to_swp_entry(oldpte);
+
+                       if (is_write_migration_entry(entry)) {
+                               /*
+                                * A protection check is difficult so
+                                * just be safe and disable write
+                                */
+                               make_migration_entry_read(&entry);
+                               set_pte_at(mm, addr, pte,
+                                       swp_entry_to_pte(entry));
+                       }
+#endif
                }
+
        } while (pte++, addr += PAGE_SIZE, addr != end);
        pte_unmap_unlock(pte - 1, ptl);
 }
@@ -106,6 +123,7 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
        unsigned long oldflags = vma->vm_flags;
        long nrpages = (end - start) >> PAGE_SHIFT;
        unsigned long charged = 0;
+       unsigned int mask;
        pgprot_t newprot;
        pgoff_t pgoff;
        int error;
@@ -132,8 +150,6 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
                }
        }
 
-       newprot = protection_map[newflags & 0xf];
-
        /*
         * First try to merge with previous and/or next vma.
         */
@@ -160,6 +176,14 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
        }
 
 success:
+       /* Don't make the VMA automatically writable if it's shared, but the
+        * backer wishes to know when pages are first written to */
+       mask = VM_READ|VM_WRITE|VM_EXEC|VM_SHARED;
+       if (vma->vm_ops && vma->vm_ops->page_mkwrite)
+               mask &= ~VM_SHARED;
+
+       newprot = protection_map[newflags & mask];
+
        /*
         * vm_flags and vm_page_prot are protected by the mmap_sem
         * held in write mode.
@@ -205,8 +229,7 @@ sys_mprotect(unsigned long start, size_t len, unsigned long prot)
        /*
         * Does the application expect PROT_READ to imply PROT_EXEC:
         */
-       if (unlikely((prot & PROT_READ) &&
-                       (current->personality & READ_IMPLIES_EXEC)))
+       if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
                prot |= PROT_EXEC;
 
        vm_flags = calc_vm_prot_bits(prot);