4 * (C) Copyright 1994 Linus Torvalds
5 * (C) Copyright 2002 Christoph Hellwig
7 * Address space accounting code <alan@redhat.com>
8 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
12 #include <linux/hugetlb.h>
13 #include <linux/slab.h>
14 #include <linux/shm.h>
15 #include <linux/mman.h>
17 #include <linux/highmem.h>
18 #include <linux/security.h>
19 #include <linux/mempolicy.h>
21 #include <asm/uaccess.h>
22 #include <asm/pgtable.h>
23 #include <asm/pgalloc.h>
24 #include <asm/cacheflush.h>
25 #include <asm/tlbflush.h>
28 change_pte_range(pmd_t *pmd, unsigned long address,
29 unsigned long size, pgprot_t newprot)
41 pte = pte_offset_map(pmd, address);
47 if (pte_present(*pte)) {
50 /* Avoid an SMP race with hardware updated dirty/clean
51 * bits by wiping the pte and then setting the new pte
54 entry = ptep_get_and_clear(pte);
55 set_pte(pte, pte_modify(entry, newprot));
59 } while (address && (address < end));
64 change_pmd_range(pgd_t *pgd, unsigned long address,
65 unsigned long size, pgprot_t newprot)
77 pmd = pmd_offset(pgd, address);
78 address &= ~PGDIR_MASK;
83 change_pte_range(pmd, address, end - address, newprot);
84 address = (address + PMD_SIZE) & PMD_MASK;
86 } while (address && (address < end));
90 change_protection(struct vm_area_struct *vma, unsigned long start,
91 unsigned long end, pgprot_t newprot)
94 unsigned long beg = start;
96 dir = pgd_offset(current->mm, start);
97 flush_cache_range(vma, beg, end);
100 spin_lock(¤t->mm->page_table_lock);
102 change_pmd_range(dir, start, end - start, newprot);
103 start = (start + PGDIR_SIZE) & PGDIR_MASK;
105 } while (start && (start < end));
106 flush_tlb_range(vma, beg, end);
107 spin_unlock(¤t->mm->page_table_lock);
112 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
113 unsigned long start, unsigned long end, unsigned int newflags)
115 struct mm_struct * mm = vma->vm_mm;
116 unsigned long charged = 0, old_end = vma->vm_end;
118 unsigned int oldflags;
122 if (newflags == vma->vm_flags) {
128 * If we make a private mapping writable we increase our commit;
129 * but (without finer accounting) cannot reduce our commit if we
130 * make it unwritable again.
132 * FIXME? We haven't defined a VM_NORESERVE flag, so mprotecting
133 * a MAP_NORESERVE private mapping to writable will now reserve.
135 if (newflags & VM_WRITE) {
136 if (!(vma->vm_flags & (VM_ACCOUNT|VM_WRITE|VM_SHARED|VM_HUGETLB))) {
137 charged = (end - start) >> PAGE_SHIFT;
138 if (security_vm_enough_memory(charged))
140 newflags |= VM_ACCOUNT;
144 newprot = protection_map[newflags & 0xf];
147 * First try to merge with previous and/or next vma.
149 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
150 *pprev = vma_merge(mm, *pprev, start, end, newflags,
151 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
157 if (start != vma->vm_start) {
158 error = split_vma(mm, vma, start, 1);
163 * Unless it returns an error, this function always sets *pprev to
164 * the first vma for which vma->vm_end >= end.
168 if (end != vma->vm_end) {
169 error = split_vma(mm, vma, end, 0);
176 * vm_flags and vm_page_prot are protected by the mmap_sem
177 * held in write mode.
179 oldflags = vma->vm_flags;
180 vma->vm_flags = newflags;
181 vma->vm_page_prot = newprot;
182 if (oldflags & VM_EXEC)
183 arch_remove_exec_range(current->mm, old_end);
184 change_protection(vma, start, end, newprot);
188 vm_unacct_memory(charged);
193 do_mprotect(struct mm_struct *mm, unsigned long start, size_t len,
196 unsigned long vm_flags, nstart, end, tmp;
197 struct vm_area_struct *vma, *prev;
199 const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
200 prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
201 if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
204 if (start & ~PAGE_MASK)
206 len = PAGE_ALIGN(len);
210 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
215 vm_flags = calc_vm_prot_bits(prot);
217 down_write(&mm->mmap_sem);
219 vma = find_vma_prev(mm, start, &prev);
223 if (unlikely(grows & PROT_GROWSDOWN)) {
224 if (vma->vm_start >= end)
226 start = vma->vm_start;
228 if (!(vma->vm_flags & VM_GROWSDOWN))
232 if (vma->vm_start > start)
234 if (unlikely(grows & PROT_GROWSUP)) {
237 if (!(vma->vm_flags & VM_GROWSUP))
241 if (start > vma->vm_start)
244 for (nstart = start ; ; ) {
245 unsigned int newflags;
247 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
249 if (is_vm_hugetlb_page(vma)) {
254 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
256 if ((newflags & ~(newflags >> 4)) & 0xf) {
261 error = security_file_mprotect(vma, prot);
268 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
273 if (nstart < prev->vm_end)
274 nstart = prev->vm_end;
279 if (!vma || vma->vm_start != nstart) {
285 up_write(&mm->mmap_sem);
289 asmlinkage long sys_mprotect(unsigned long start, size_t len, unsigned long prot)
291 return(do_mprotect(current->mm, start, len, prot));