ia64/linux-2.6.18-xen.hg

view mm/mprotect.c @ 749:2892ca2b9c17

linux/x86: cleanup IO-APIC code

- get 32-bit code in sync with 64-bit wrt ExtINT pin detection being
unnecessary
- eliminate build warnings resulting from c/s 725

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Nov 28 13:31:21 2008 +0000 (2008-11-28)
parents 5f64a1735b3c
children
line source
1 /*
2 * mm/mprotect.c
3 *
4 * (C) Copyright 1994 Linus Torvalds
5 * (C) Copyright 2002 Christoph Hellwig
6 *
7 * Address space accounting code <alan@redhat.com>
8 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
9 */
11 #include <linux/mm.h>
12 #include <linux/hugetlb.h>
13 #include <linux/slab.h>
14 #include <linux/shm.h>
15 #include <linux/mman.h>
16 #include <linux/fs.h>
17 #include <linux/highmem.h>
18 #include <linux/security.h>
19 #include <linux/mempolicy.h>
20 #include <linux/personality.h>
21 #include <linux/syscalls.h>
22 #include <linux/swap.h>
23 #include <linux/swapops.h>
24 #include <asm/uaccess.h>
25 #include <asm/pgtable.h>
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
29 static void change_pte_range(struct mm_struct *mm, pmd_t *pmd,
30 unsigned long addr, unsigned long end, pgprot_t newprot)
31 {
32 pte_t *pte, oldpte;
33 spinlock_t *ptl;
35 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
36 do {
37 oldpte = *pte;
38 if (pte_present(oldpte)) {
39 pte_t ptent;
41 /* Avoid an SMP race with hardware updated dirty/clean
42 * bits by wiping the pte and then setting the new pte
43 * into place.
44 */
45 ptent = pte_modify(ptep_get_and_clear(mm, addr, pte), newprot);
46 set_pte_at(mm, addr, pte, ptent);
47 lazy_mmu_prot_update(ptent);
48 #ifdef CONFIG_MIGRATION
49 } else if (!pte_file(oldpte)) {
50 swp_entry_t entry = pte_to_swp_entry(oldpte);
52 if (is_write_migration_entry(entry)) {
53 /*
54 * A protection check is difficult so
55 * just be safe and disable write
56 */
57 make_migration_entry_read(&entry);
58 set_pte_at(mm, addr, pte,
59 swp_entry_to_pte(entry));
60 }
61 #endif
62 }
64 } while (pte++, addr += PAGE_SIZE, addr != end);
65 pte_unmap_unlock(pte - 1, ptl);
66 }
68 static inline void change_pmd_range(struct mm_struct *mm, pud_t *pud,
69 unsigned long addr, unsigned long end, pgprot_t newprot)
70 {
71 pmd_t *pmd;
72 unsigned long next;
74 pmd = pmd_offset(pud, addr);
75 do {
76 next = pmd_addr_end(addr, end);
77 if (pmd_none_or_clear_bad(pmd))
78 continue;
79 if (arch_change_pte_range(mm, pmd, addr, next, newprot))
80 continue;
81 change_pte_range(mm, pmd, addr, next, newprot);
82 } while (pmd++, addr = next, addr != end);
83 }
85 static inline void change_pud_range(struct mm_struct *mm, pgd_t *pgd,
86 unsigned long addr, unsigned long end, pgprot_t newprot)
87 {
88 pud_t *pud;
89 unsigned long next;
91 pud = pud_offset(pgd, addr);
92 do {
93 next = pud_addr_end(addr, end);
94 if (pud_none_or_clear_bad(pud))
95 continue;
96 change_pmd_range(mm, pud, addr, next, newprot);
97 } while (pud++, addr = next, addr != end);
98 }
100 static void change_protection(struct vm_area_struct *vma,
101 unsigned long addr, unsigned long end, pgprot_t newprot)
102 {
103 struct mm_struct *mm = vma->vm_mm;
104 pgd_t *pgd;
105 unsigned long next;
106 unsigned long start = addr;
108 BUG_ON(addr >= end);
109 pgd = pgd_offset(mm, addr);
110 flush_cache_range(vma, addr, end);
111 do {
112 next = pgd_addr_end(addr, end);
113 if (pgd_none_or_clear_bad(pgd))
114 continue;
115 change_pud_range(mm, pgd, addr, next, newprot);
116 } while (pgd++, addr = next, addr != end);
117 flush_tlb_range(vma, start, end);
118 }
120 static int
121 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
122 unsigned long start, unsigned long end, unsigned long newflags)
123 {
124 struct mm_struct *mm = vma->vm_mm;
125 unsigned long oldflags = vma->vm_flags;
126 long nrpages = (end - start) >> PAGE_SHIFT;
127 unsigned long charged = 0;
128 unsigned int mask;
129 pgprot_t newprot;
130 pgoff_t pgoff;
131 int error;
133 if (newflags == oldflags) {
134 *pprev = vma;
135 return 0;
136 }
138 /*
139 * If we make a private mapping writable we increase our commit;
140 * but (without finer accounting) cannot reduce our commit if we
141 * make it unwritable again.
142 *
143 * FIXME? We haven't defined a VM_NORESERVE flag, so mprotecting
144 * a MAP_NORESERVE private mapping to writable will now reserve.
145 */
146 if (newflags & VM_WRITE) {
147 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_SHARED))) {
148 charged = nrpages;
149 if (security_vm_enough_memory(charged))
150 return -ENOMEM;
151 newflags |= VM_ACCOUNT;
152 }
153 }
155 /*
156 * First try to merge with previous and/or next vma.
157 */
158 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
159 *pprev = vma_merge(mm, *pprev, start, end, newflags,
160 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
161 if (*pprev) {
162 vma = *pprev;
163 goto success;
164 }
166 *pprev = vma;
168 if (start != vma->vm_start) {
169 error = split_vma(mm, vma, start, 1);
170 if (error)
171 goto fail;
172 }
174 if (end != vma->vm_end) {
175 error = split_vma(mm, vma, end, 0);
176 if (error)
177 goto fail;
178 }
180 success:
181 /* Don't make the VMA automatically writable if it's shared, but the
182 * backer wishes to know when pages are first written to */
183 mask = VM_READ|VM_WRITE|VM_EXEC|VM_SHARED;
184 if (vma->vm_ops && vma->vm_ops->page_mkwrite)
185 mask &= ~VM_SHARED;
187 newprot = protection_map[newflags & mask];
189 /*
190 * vm_flags and vm_page_prot are protected by the mmap_sem
191 * held in write mode.
192 */
193 vma->vm_flags = newflags;
194 vma->vm_page_prot = newprot;
195 if (is_vm_hugetlb_page(vma))
196 hugetlb_change_protection(vma, start, end, newprot);
197 else
198 change_protection(vma, start, end, newprot);
199 vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
200 vm_stat_account(mm, newflags, vma->vm_file, nrpages);
201 return 0;
203 fail:
204 vm_unacct_memory(charged);
205 return error;
206 }
208 asmlinkage long
209 sys_mprotect(unsigned long start, size_t len, unsigned long prot)
210 {
211 unsigned long vm_flags, nstart, end, tmp, reqprot;
212 struct vm_area_struct *vma, *prev;
213 int error = -EINVAL;
214 const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
215 prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
216 if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
217 return -EINVAL;
219 if (start & ~PAGE_MASK)
220 return -EINVAL;
221 if (!len)
222 return 0;
223 len = PAGE_ALIGN(len);
224 end = start + len;
225 if (end <= start)
226 return -ENOMEM;
227 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
228 return -EINVAL;
230 reqprot = prot;
231 /*
232 * Does the application expect PROT_READ to imply PROT_EXEC:
233 */
234 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
235 prot |= PROT_EXEC;
237 vm_flags = calc_vm_prot_bits(prot);
239 down_write(&current->mm->mmap_sem);
241 vma = find_vma_prev(current->mm, start, &prev);
242 error = -ENOMEM;
243 if (!vma)
244 goto out;
245 if (unlikely(grows & PROT_GROWSDOWN)) {
246 if (vma->vm_start >= end)
247 goto out;
248 start = vma->vm_start;
249 error = -EINVAL;
250 if (!(vma->vm_flags & VM_GROWSDOWN))
251 goto out;
252 }
253 else {
254 if (vma->vm_start > start)
255 goto out;
256 if (unlikely(grows & PROT_GROWSUP)) {
257 end = vma->vm_end;
258 error = -EINVAL;
259 if (!(vma->vm_flags & VM_GROWSUP))
260 goto out;
261 }
262 }
263 if (start > vma->vm_start)
264 prev = vma;
266 for (nstart = start ; ; ) {
267 unsigned long newflags;
269 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
271 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
273 /* newflags >> 4 shift VM_MAY% in place of VM_% */
274 if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
275 error = -EACCES;
276 goto out;
277 }
279 error = security_file_mprotect(vma, reqprot, prot);
280 if (error)
281 goto out;
283 tmp = vma->vm_end;
284 if (tmp > end)
285 tmp = end;
286 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
287 if (error)
288 goto out;
289 nstart = tmp;
291 if (nstart < prev->vm_end)
292 nstart = prev->vm_end;
293 if (nstart >= end)
294 goto out;
296 vma = prev->vm_next;
297 if (!vma || vma->vm_start != nstart) {
298 error = -ENOMEM;
299 goto out;
300 }
301 }
302 out:
303 up_write(&current->mm->mmap_sem);
304 return error;
305 }