blob: 7ef6ae964e8f02327b14b69507ecc1357e69a8b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mm/mprotect.c
3 *
4 * (C) Copyright 1994 Linus Torvalds
5 * (C) Copyright 2002 Christoph Hellwig
6 *
Alan Cox046c6882009-01-05 14:06:29 +00007 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
9 */
10
11#include <linux/mm.h>
12#include <linux/hugetlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/shm.h>
14#include <linux/mman.h>
15#include <linux/fs.h>
16#include <linux/highmem.h>
17#include <linux/security.h>
18#include <linux/mempolicy.h>
19#include <linux/personality.h>
20#include <linux/syscalls.h>
Christoph Lameter06972122006-06-23 02:03:35 -070021#include <linux/swap.h>
22#include <linux/swapops.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070023#include <linux/mmu_notifier.h>
KOSAKI Motohiro64cdd542009-01-06 14:39:16 -080024#include <linux/migrate.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020025#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/uaccess.h>
27#include <asm/pgtable.h>
28#include <asm/cacheflush.h>
29#include <asm/tlbflush.h>
30
Venki Pallipadi1c12c4c2008-05-14 16:05:51 -070031#ifndef pgprot_modify
32static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
33{
34 return newprot;
35}
36#endif
37
Mel Gorman4b10e7d2012-10-25 14:16:32 +020038static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
Peter Zijlstrac1e60982006-09-25 23:30:59 -070039 unsigned long addr, unsigned long end, pgprot_t newprot,
Mel Gorman4b10e7d2012-10-25 14:16:32 +020040 int dirty_accountable, int prot_numa)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Mel Gorman4b10e7d2012-10-25 14:16:32 +020042 struct mm_struct *mm = vma->vm_mm;
Christoph Lameter06972122006-06-23 02:03:35 -070043 pte_t *pte, oldpte;
Hugh Dickins705e87c2005-10-29 18:16:27 -070044 spinlock_t *ptl;
Peter Zijlstra7da4d642012-11-19 03:14:23 +010045 unsigned long pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Hugh Dickins705e87c2005-10-29 18:16:27 -070047 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
Zachary Amsden6606c3e2006-09-30 23:29:33 -070048 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 do {
Christoph Lameter06972122006-06-23 02:03:35 -070050 oldpte = *pte;
51 if (pte_present(oldpte)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 pte_t ptent;
Mel Gorman4b10e7d2012-10-25 14:16:32 +020053 bool updated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jeremy Fitzhardinge1ea07042008-06-16 04:30:00 -070055 ptent = ptep_modify_prot_start(mm, addr, pte);
Mel Gorman4b10e7d2012-10-25 14:16:32 +020056 if (!prot_numa) {
57 ptent = pte_modify(ptent, newprot);
58 updated = true;
59 } else {
60 struct page *page;
61
62 page = vm_normal_page(vma, addr, oldpte);
63 if (page) {
64 /* only check non-shared pages */
65 if (!pte_numa(oldpte) &&
66 page_mapcount(page) == 1) {
67 ptent = pte_mknuma(ptent);
68 updated = true;
69 }
70 }
71 }
Jeremy Fitzhardinge1ea07042008-06-16 04:30:00 -070072
Peter Zijlstrac1e60982006-09-25 23:30:59 -070073 /*
74 * Avoid taking write faults for pages we know to be
75 * dirty.
76 */
Mel Gorman4b10e7d2012-10-25 14:16:32 +020077 if (dirty_accountable && pte_dirty(ptent)) {
Peter Zijlstrac1e60982006-09-25 23:30:59 -070078 ptent = pte_mkwrite(ptent);
Mel Gorman4b10e7d2012-10-25 14:16:32 +020079 updated = true;
80 }
81
82 if (updated)
83 pages++;
Jeremy Fitzhardinge1ea07042008-06-16 04:30:00 -070084
85 ptep_modify_prot_commit(mm, addr, pte, ptent);
Konstantin Khlebnikovce1744f2012-03-21 16:33:59 -070086 } else if (IS_ENABLED(CONFIG_MIGRATION) && !pte_file(oldpte)) {
Christoph Lameter06972122006-06-23 02:03:35 -070087 swp_entry_t entry = pte_to_swp_entry(oldpte);
88
89 if (is_write_migration_entry(entry)) {
90 /*
91 * A protection check is difficult so
92 * just be safe and disable write
93 */
94 make_migration_entry_read(&entry);
95 set_pte_at(mm, addr, pte,
96 swp_entry_to_pte(entry));
97 }
Peter Zijlstra7da4d642012-11-19 03:14:23 +010098 pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100 } while (pte++, addr += PAGE_SIZE, addr != end);
Zachary Amsden6606c3e2006-09-30 23:29:33 -0700101 arch_leave_lazy_mmu_mode();
Hugh Dickins705e87c2005-10-29 18:16:27 -0700102 pte_unmap_unlock(pte - 1, ptl);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100103
104 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200107#ifdef CONFIG_NUMA_BALANCING
108static inline void change_pmd_protnuma(struct mm_struct *mm, unsigned long addr,
109 pmd_t *pmd)
110{
111 spin_lock(&mm->page_table_lock);
112 set_pmd_at(mm, addr & PMD_MASK, pmd, pmd_mknuma(*pmd));
113 spin_unlock(&mm->page_table_lock);
114}
115#else
116static inline void change_pmd_protnuma(struct mm_struct *mm, unsigned long addr,
117 pmd_t *pmd)
118{
119 BUG();
120}
121#endif /* CONFIG_NUMA_BALANCING */
122
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100123static inline unsigned long change_pmd_range(struct vm_area_struct *vma, pud_t *pud,
Peter Zijlstrac1e60982006-09-25 23:30:59 -0700124 unsigned long addr, unsigned long end, pgprot_t newprot,
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200125 int dirty_accountable, int prot_numa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 pmd_t *pmd;
128 unsigned long next;
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100129 unsigned long pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 pmd = pmd_offset(pud, addr);
132 do {
133 next = pmd_addr_end(addr, end);
Johannes Weinercd7548a2011-01-13 15:47:04 -0800134 if (pmd_trans_huge(*pmd)) {
135 if (next - addr != HPAGE_PMD_SIZE)
136 split_huge_page_pmd(vma->vm_mm, pmd);
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200137 else if (change_huge_pmd(vma, pmd, addr, newprot, prot_numa)) {
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100138 pages += HPAGE_PMD_NR;
Johannes Weinercd7548a2011-01-13 15:47:04 -0800139 continue;
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100140 }
Johannes Weinercd7548a2011-01-13 15:47:04 -0800141 /* fall through */
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (pmd_none_or_clear_bad(pmd))
144 continue;
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200145 pages += change_pte_range(vma, pmd, addr, next, newprot,
146 dirty_accountable, prot_numa);
147
148 if (prot_numa)
149 change_pmd_protnuma(vma->vm_mm, addr, pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 } while (pmd++, addr = next, addr != end);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100151
152 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100155static inline unsigned long change_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
Peter Zijlstrac1e60982006-09-25 23:30:59 -0700156 unsigned long addr, unsigned long end, pgprot_t newprot,
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200157 int dirty_accountable, int prot_numa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 pud_t *pud;
160 unsigned long next;
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100161 unsigned long pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 pud = pud_offset(pgd, addr);
164 do {
165 next = pud_addr_end(addr, end);
166 if (pud_none_or_clear_bad(pud))
167 continue;
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100168 pages += change_pmd_range(vma, pud, addr, next, newprot,
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200169 dirty_accountable, prot_numa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 } while (pud++, addr = next, addr != end);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100171
172 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100175static unsigned long change_protection_range(struct vm_area_struct *vma,
Peter Zijlstrac1e60982006-09-25 23:30:59 -0700176 unsigned long addr, unsigned long end, pgprot_t newprot,
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200177 int dirty_accountable, int prot_numa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 struct mm_struct *mm = vma->vm_mm;
180 pgd_t *pgd;
181 unsigned long next;
182 unsigned long start = addr;
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100183 unsigned long pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 BUG_ON(addr >= end);
186 pgd = pgd_offset(mm, addr);
187 flush_cache_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 do {
189 next = pgd_addr_end(addr, end);
190 if (pgd_none_or_clear_bad(pgd))
191 continue;
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100192 pages += change_pud_range(vma, pgd, addr, next, newprot,
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200193 dirty_accountable, prot_numa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 } while (pgd++, addr = next, addr != end);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100195
Ingo Molnar1233d582012-11-19 03:14:24 +0100196 /* Only flush the TLB if we actually modified any entries: */
197 if (pages)
198 flush_tlb_range(vma, start, end);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100199
200 return pages;
201}
202
203unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
204 unsigned long end, pgprot_t newprot,
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200205 int dirty_accountable, int prot_numa)
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100206{
207 struct mm_struct *mm = vma->vm_mm;
208 unsigned long pages;
209
210 mmu_notifier_invalidate_range_start(mm, start, end);
211 if (is_vm_hugetlb_page(vma))
212 pages = hugetlb_change_protection(vma, start, end, newprot);
213 else
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200214 pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100215 mmu_notifier_invalidate_range_end(mm, start, end);
216
217 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700220int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
222 unsigned long start, unsigned long end, unsigned long newflags)
223{
224 struct mm_struct *mm = vma->vm_mm;
225 unsigned long oldflags = vma->vm_flags;
226 long nrpages = (end - start) >> PAGE_SHIFT;
227 unsigned long charged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 pgoff_t pgoff;
229 int error;
Peter Zijlstrac1e60982006-09-25 23:30:59 -0700230 int dirty_accountable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (newflags == oldflags) {
233 *pprev = vma;
234 return 0;
235 }
236
237 /*
238 * If we make a private mapping writable we increase our commit;
239 * but (without finer accounting) cannot reduce our commit if we
Mel Gorman5a6fe122009-02-10 14:02:27 +0000240 * make it unwritable again. hugetlb mapping were accounted for
241 * even if read-only so there is no need to account for them here
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243 if (newflags & VM_WRITE) {
Mel Gorman5a6fe122009-02-10 14:02:27 +0000244 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
Andy Whitcroftcdfd4322008-07-23 21:27:28 -0700245 VM_SHARED|VM_NORESERVE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 charged = nrpages;
Al Viro191c5422012-02-13 03:58:52 +0000247 if (security_vm_enough_memory_mm(mm, charged))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return -ENOMEM;
249 newflags |= VM_ACCOUNT;
250 }
251 }
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /*
254 * First try to merge with previous and/or next vma.
255 */
256 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
257 *pprev = vma_merge(mm, *pprev, start, end, newflags,
258 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
259 if (*pprev) {
260 vma = *pprev;
261 goto success;
262 }
263
264 *pprev = vma;
265
266 if (start != vma->vm_start) {
267 error = split_vma(mm, vma, start, 1);
268 if (error)
269 goto fail;
270 }
271
272 if (end != vma->vm_end) {
273 error = split_vma(mm, vma, end, 0);
274 if (error)
275 goto fail;
276 }
277
278success:
279 /*
280 * vm_flags and vm_page_prot are protected by the mmap_sem
281 * held in write mode.
282 */
283 vma->vm_flags = newflags;
Venki Pallipadi1c12c4c2008-05-14 16:05:51 -0700284 vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
285 vm_get_page_prot(newflags));
286
Peter Zijlstrac1e60982006-09-25 23:30:59 -0700287 if (vma_wants_writenotify(vma)) {
Hugh Dickins1ddd4392007-10-22 20:45:12 -0700288 vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
Peter Zijlstrac1e60982006-09-25 23:30:59 -0700289 dirty_accountable = 1;
290 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700291
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200292 change_protection(vma, start, end, vma->vm_page_prot, dirty_accountable, 0);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100293
Hugh Dickinsab50b8e2005-10-29 18:15:56 -0700294 vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
295 vm_stat_account(mm, newflags, vma->vm_file, nrpages);
Pekka Enberg63bfd732010-11-08 21:29:07 +0200296 perf_event_mmap(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return 0;
298
299fail:
300 vm_unacct_memory(charged);
301 return error;
302}
303
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100304SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
305 unsigned long, prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 unsigned long vm_flags, nstart, end, tmp, reqprot;
308 struct vm_area_struct *vma, *prev;
309 int error = -EINVAL;
310 const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
311 prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
312 if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
313 return -EINVAL;
314
315 if (start & ~PAGE_MASK)
316 return -EINVAL;
317 if (!len)
318 return 0;
319 len = PAGE_ALIGN(len);
320 end = start + len;
321 if (end <= start)
322 return -ENOMEM;
Dave Kleikampb845f312008-07-08 00:28:51 +1000323 if (!arch_validate_prot(prot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return -EINVAL;
325
326 reqprot = prot;
327 /*
328 * Does the application expect PROT_READ to imply PROT_EXEC:
329 */
Hua Zhongb344e052006-06-23 02:03:23 -0700330 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 prot |= PROT_EXEC;
332
333 vm_flags = calc_vm_prot_bits(prot);
334
335 down_write(&current->mm->mmap_sem);
336
Linus Torvalds097d5912012-03-06 18:23:36 -0800337 vma = find_vma(current->mm, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 error = -ENOMEM;
339 if (!vma)
340 goto out;
Linus Torvalds097d5912012-03-06 18:23:36 -0800341 prev = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (unlikely(grows & PROT_GROWSDOWN)) {
343 if (vma->vm_start >= end)
344 goto out;
345 start = vma->vm_start;
346 error = -EINVAL;
347 if (!(vma->vm_flags & VM_GROWSDOWN))
348 goto out;
349 }
350 else {
351 if (vma->vm_start > start)
352 goto out;
353 if (unlikely(grows & PROT_GROWSUP)) {
354 end = vma->vm_end;
355 error = -EINVAL;
356 if (!(vma->vm_flags & VM_GROWSUP))
357 goto out;
358 }
359 }
360 if (start > vma->vm_start)
361 prev = vma;
362
363 for (nstart = start ; ; ) {
364 unsigned long newflags;
365
366 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
369
Paolo 'Blaisorblade' Giarrusso7e2cff42005-09-21 09:55:39 -0700370 /* newflags >> 4 shift VM_MAY% in place of VM_% */
371 if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 error = -EACCES;
373 goto out;
374 }
375
376 error = security_file_mprotect(vma, reqprot, prot);
377 if (error)
378 goto out;
379
380 tmp = vma->vm_end;
381 if (tmp > end)
382 tmp = end;
383 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
384 if (error)
385 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 nstart = tmp;
387
388 if (nstart < prev->vm_end)
389 nstart = prev->vm_end;
390 if (nstart >= end)
391 goto out;
392
393 vma = prev->vm_next;
394 if (!vma || vma->vm_start != nstart) {
395 error = -ENOMEM;
396 goto out;
397 }
398 }
399out:
400 up_write(&current->mm->mmap_sem);
401 return error;
402}