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>
20 #include <asm/uaccess.h>
21 #include <asm/pgalloc.h>
22 #include <asm/pgtable.h>
23 #include <asm/cacheflush.h>
24 #include <asm/tlbflush.h>
27 change_pte_range(pmd_t *pmd, unsigned long address,
28 unsigned long size, pgprot_t newprot)
40 pte = pte_offset_map(pmd, address);
46 if (pte_present(*pte)) {
49 /* Avoid an SMP race with hardware updated dirty/clean
50 * bits by wiping the pte and then setting the new pte
53 entry = ptep_get_and_clear(pte);
54 set_pte(pte, pte_modify(entry, newprot));
58 } while (address && (address < end));
63 change_pmd_range(pgd_t *pgd, unsigned long address,
64 unsigned long size, pgprot_t newprot)
76 pmd = pmd_offset(pgd, address);
77 address &= ~PGDIR_MASK;
82 change_pte_range(pmd, address, end - address, newprot);
83 address = (address + PMD_SIZE) & PMD_MASK;
85 } while (address && (address < end));
89 change_protection(struct vm_area_struct *vma, unsigned long start,
90 unsigned long end, pgprot_t newprot)
93 unsigned long beg = start;
95 dir = pgd_offset(current->mm, start);
96 flush_cache_range(vma, beg, end);
99 spin_lock(¤t->mm->page_table_lock);
101 change_pmd_range(dir, start, end - start, newprot);
102 start = (start + PGDIR_SIZE) & PGDIR_MASK;
104 } while (start && (start < end));
105 flush_tlb_range(vma, beg, end);
106 spin_unlock(¤t->mm->page_table_lock);
110 #if VMA_MERGING_FIXUP
112 * Try to merge a vma with the previous flag, return 1 if successful or 0 if it
116 mprotect_attempt_merge(struct vm_area_struct *vma, struct vm_area_struct *prev,
117 unsigned long end, int newflags)
119 struct mm_struct * mm = vma->vm_mm;
123 if (prev->vm_end != vma->vm_start)
125 if (!can_vma_merge(prev, newflags))
127 if (vma->vm_file || (vma->vm_flags & VM_SHARED))
131 * If the whole area changes to the protection of the previous one
132 * we can just get rid of it.
134 if (end == vma->vm_end) {
135 spin_lock(&mm->page_table_lock);
137 __vma_unlink(mm, vma, prev);
138 spin_unlock(&mm->page_table_lock);
140 kmem_cache_free(vm_area_cachep, vma);
146 * Otherwise extend it.
148 spin_lock(&mm->page_table_lock);
151 spin_unlock(&mm->page_table_lock);
157 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
158 unsigned long start, unsigned long end, unsigned int newflags)
160 struct mm_struct * mm = vma->vm_mm;
161 unsigned long charged = 0;
165 if (newflags == vma->vm_flags) {
171 * If we make a private mapping writable we increase our commit;
172 * but (without finer accounting) cannot reduce our commit if we
173 * make it unwritable again.
175 * FIXME? We haven't defined a VM_NORESERVE flag, so mprotecting
176 * a MAP_NORESERVE private mapping to writable will now reserve.
178 if (newflags & VM_WRITE) {
179 if (!(vma->vm_flags & (VM_ACCOUNT|VM_WRITE|VM_SHARED))) {
180 charged = (end - start) >> PAGE_SHIFT;
181 if (security_vm_enough_memory(charged))
183 newflags |= VM_ACCOUNT;
187 newprot = protection_map[newflags & 0xf];
189 if (start == vma->vm_start) {
190 #if VMA_MERGING_FIXUP
192 * Try to merge with the previous vma.
194 if (mprotect_attempt_merge(vma, *pprev, end, newflags)) {
200 error = split_vma(mm, vma, start, 1);
205 * Unless it returns an error, this function always sets *pprev to
206 * the first vma for which vma->vm_end >= end.
210 if (end != vma->vm_end) {
211 error = split_vma(mm, vma, end, 0);
216 spin_lock(&mm->page_table_lock);
217 vma->vm_flags = newflags;
218 vma->vm_page_prot = newprot;
219 spin_unlock(&mm->page_table_lock);
220 #if VMA_MERGING_FIXUP
223 change_protection(vma, start, end, newprot);
227 vm_unacct_memory(charged);
232 do_mprotect(struct mm_struct *mm, unsigned long start, size_t len,
235 unsigned long vm_flags, nstart, end, tmp;
236 struct vm_area_struct * vma, * next, * prev;
238 const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
239 prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
240 if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
243 if (start & ~PAGE_MASK)
245 len = PAGE_ALIGN(len);
249 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
254 vm_flags = calc_vm_prot_bits(prot);
256 down_write(&mm->mmap_sem);
258 vma = find_vma_prev(mm, start, &prev);
262 if (unlikely(grows & PROT_GROWSDOWN)) {
263 if (vma->vm_start >= end)
265 start = vma->vm_start;
267 if (!(vma->vm_flags & VM_GROWSDOWN))
271 if (vma->vm_start > start)
273 if (unlikely(grows & PROT_GROWSUP)) {
276 if (!(vma->vm_flags & VM_GROWSUP))
281 for (nstart = start ; ; ) {
282 unsigned int newflags;
285 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
287 if (is_vm_hugetlb_page(vma)) {
292 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
294 if ((newflags & ~(newflags >> 4)) & 0xf) {
299 error = security_file_mprotect(vma, prot);
303 if (vma->vm_end > end) {
304 error = mprotect_fixup(vma, &prev, nstart, end, newflags);
307 if (vma->vm_end == end)
312 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
319 if (!vma || vma->vm_start != nstart) {
325 #if VMA_MERGING_FIXUP
326 if (next && prev->vm_end == next->vm_start &&
327 can_vma_merge(next, prev->vm_flags) &&
328 !prev->vm_file && !(prev->vm_flags & VM_SHARED)) {
329 spin_lock(&prev->vm_mm->page_table_lock);
330 prev->vm_end = next->vm_end;
331 __vma_unlink(prev->vm_mm, next, prev);
332 spin_unlock(&prev->vm_mm->page_table_lock);
334 kmem_cache_free(vm_area_cachep, next);
335 prev->vm_mm->map_count--;
339 up_write(&mm->mmap_sem);
343 asmlinkage long sys_mprotect(unsigned long start, size_t len, unsigned long prot)
345 return(do_mprotect(current->mm, start, len, prot));