[PATCH] ZVC/zone_reclaim: Leave 1% of unmapped pagecache pages for file I/O
[pandora-kernel.git] / mm / mprotect.c
index 17a2b52..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;
@@ -124,15 +142,7 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
         * a MAP_NORESERVE private mapping to writable will now reserve.
         */
        if (newflags & VM_WRITE) {
-               if (oldflags & VM_RESERVED) {
-                       BUG_ON(oldflags & VM_WRITE);
-                       printk(KERN_WARNING "program %s is using MAP_PRIVATE, "
-                               "PROT_WRITE mprotect of VM_RESERVED memory, "
-                               "which is deprecated. Please report this to "
-                               "linux-kernel@vger.kernel.org\n",current->comm);
-                       return -EACCES;
-               }
-               if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_SHARED|VM_HUGETLB))) {
+               if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_SHARED))) {
                        charged = nrpages;
                        if (security_vm_enough_memory(charged))
                                return -ENOMEM;
@@ -140,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.
         */
@@ -168,13 +176,24 @@ 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.
         */
        vma->vm_flags = newflags;
        vma->vm_page_prot = newprot;
-       change_protection(vma, start, end, newprot);
+       if (is_vm_hugetlb_page(vma))
+               hugetlb_change_protection(vma, start, end, newprot);
+       else
+               change_protection(vma, start, end, newprot);
        vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
        vm_stat_account(mm, newflags, vma->vm_file, nrpages);
        return 0;
@@ -210,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);
@@ -248,11 +266,6 @@ sys_mprotect(unsigned long start, size_t len, unsigned long prot)
 
                /* Here we know that  vma->vm_start <= nstart < vma->vm_end. */
 
-               if (is_vm_hugetlb_page(vma)) {
-                       error = -EACCES;
-                       goto out;
-               }
-
                newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
 
                /* newflags >> 4 shift VM_MAY% in place of VM_% */