mm/core, x86/mm/pkeys: Add execute-only protection keys support
[cascardo/linux.git] / mm / mprotect.c
index 8eb7bb4..fa37c4c 100644 (file)
@@ -24,6 +24,7 @@
 #include <linux/migrate.h>
 #include <linux/perf_event.h>
 #include <linux/ksm.h>
+#include <linux/pkeys.h>
 #include <asm/uaccess.h>
 #include <asm/pgtable.h>
 #include <asm/cacheflush.h>
@@ -160,9 +161,11 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
                }
 
                if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
-                       if (next - addr != HPAGE_PMD_SIZE)
+                       if (next - addr != HPAGE_PMD_SIZE) {
                                split_huge_pmd(vma, pmd, addr);
-                       else {
+                               if (pmd_none(*pmd))
+                                       continue;
+                       } else {
                                int nr_ptes = change_huge_pmd(vma, pmd, addr,
                                                newprot, prot_numa);
 
@@ -352,7 +355,7 @@ fail:
 SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
                unsigned long, prot)
 {
-       unsigned long vm_flags, nstart, end, tmp, reqprot;
+       unsigned long nstart, end, tmp, reqprot;
        struct vm_area_struct *vma, *prev;
        int error = -EINVAL;
        const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
@@ -378,8 +381,6 @@ SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
        if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
                prot |= PROT_EXEC;
 
-       vm_flags = calc_vm_prot_bits(prot);
-
        down_write(&current->mm->mmap_sem);
 
        vma = find_vma(current->mm, start);
@@ -409,10 +410,11 @@ SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
 
        for (nstart = start ; ; ) {
                unsigned long newflags;
+               int pkey = arch_override_mprotect_pkey(vma, prot, -1);
 
                /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
 
-               newflags = vm_flags;
+               newflags = calc_vm_prot_bits(prot, pkey);
                newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
 
                /* newflags >> 4 shift VM_MAY% in place of VM_% */