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 * Try to merge a vma with the previous flag, return 1 if successful or 0 if it
114 mprotect_attempt_merge(struct vm_area_struct *vma, struct vm_area_struct *prev,
115 unsigned long end, int newflags)
117 struct mm_struct * mm;
122 if (prev->vm_end != vma->vm_start)
124 if (!can_vma_merge(prev, newflags))
126 if (vma->vm_file || (vma->vm_flags & VM_SHARED))
130 * If the whole area changes to the protection of the previous one
131 * we can just get rid of it.
133 if (end == vma->vm_end) {
134 spin_lock(&mm->page_table_lock);
136 __vma_unlink(mm, vma, prev);
137 spin_unlock(&mm->page_table_lock);
139 kmem_cache_free(vm_area_cachep, vma);
145 * Otherwise extend it.
147 spin_lock(&mm->page_table_lock);
150 spin_unlock(&mm->page_table_lock);
155 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
156 unsigned long start, unsigned long end, unsigned int newflags)
158 struct mm_struct * mm = vma->vm_mm;
159 unsigned long charged = 0;
163 if (newflags == vma->vm_flags) {
169 * If we make a private mapping writable we increase our commit;
170 * but (without finer accounting) cannot reduce our commit if we
171 * make it unwritable again.
173 * FIXME? We haven't defined a VM_NORESERVE flag, so mprotecting
174 * a MAP_NORESERVE private mapping to writable will now reserve.
176 if (newflags & VM_WRITE) {
177 if (!(vma->vm_flags & (VM_ACCOUNT|VM_WRITE|VM_SHARED))
178 && VM_MAYACCT(vma)) {
179 charged = (end - start) >> PAGE_SHIFT;
180 if (security_vm_enough_memory(charged))
182 newflags |= VM_ACCOUNT;
186 newprot = protection_map[newflags & 0xf];
188 if (start == vma->vm_start) {
190 * Try to merge with the previous vma.
192 if (mprotect_attempt_merge(vma, *pprev, end, newflags)) {
197 error = split_vma(mm, vma, start, 1);
202 * Unless it returns an error, this function always sets *pprev to
203 * the first vma for which vma->vm_end >= end.
207 if (end != vma->vm_end) {
208 error = split_vma(mm, vma, end, 0);
213 spin_lock(&mm->page_table_lock);
214 vma->vm_flags = newflags;
215 vma->vm_page_prot = newprot;
216 spin_unlock(&mm->page_table_lock);
218 change_protection(vma, start, end, newprot);
222 vm_unacct_memory(charged);
227 sys_mprotect(unsigned long start, size_t len, unsigned long prot)
229 unsigned long vm_flags, nstart, end, tmp;
230 struct vm_area_struct * vma, * next, * prev;
232 const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
233 prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
234 if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
237 if (start & ~PAGE_MASK)
239 len = PAGE_ALIGN(len);
243 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
248 vm_flags = calc_vm_prot_bits(prot);
250 down_write(¤t->mm->mmap_sem);
252 vma = find_vma_prev(current->mm, start, &prev);
256 if (unlikely(grows & PROT_GROWSDOWN)) {
257 if (vma->vm_start >= end)
259 start = vma->vm_start;
261 if (!(vma->vm_flags & VM_GROWSDOWN))
265 if (vma->vm_start > start)
267 if (unlikely(grows & PROT_GROWSUP)) {
270 if (!(vma->vm_flags & VM_GROWSUP))
275 for (nstart = start ; ; ) {
276 unsigned int newflags;
279 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
281 if (is_vm_hugetlb_page(vma)) {
286 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
288 if ((newflags & ~(newflags >> 4)) & 0xf) {
293 error = security_file_mprotect(vma, prot);
297 if (vma->vm_end > end) {
298 error = mprotect_fixup(vma, &prev, nstart, end, newflags);
301 if (vma->vm_end == end)
306 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
313 if (!vma || vma->vm_start != nstart) {
319 if (next && prev->vm_end == next->vm_start &&
320 can_vma_merge(next, prev->vm_flags) &&
321 !prev->vm_file && !(prev->vm_flags & VM_SHARED)) {
322 spin_lock(&prev->vm_mm->page_table_lock);
323 prev->vm_end = next->vm_end;
324 __vma_unlink(prev->vm_mm, next, prev);
325 spin_unlock(&prev->vm_mm->page_table_lock);
327 kmem_cache_free(vm_area_cachep, next);
328 prev->vm_mm->map_count--;
331 up_write(¤t->mm->mmap_sem);