mm: mempolicy: Implement change_prot_numa() in terms of change_protection()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / mm / mprotect.c
... / ...
CommitLineData
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@lxorguk.ukuu.org.uk>
8 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
9 */
10
11#include <linux/mm.h>
12#include <linux/hugetlb.h>
13#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>
21#include <linux/swap.h>
22#include <linux/swapops.h>
23#include <linux/mmu_notifier.h>
24#include <linux/migrate.h>
25#include <linux/perf_event.h>
26#include <asm/uaccess.h>
27#include <asm/pgtable.h>
28#include <asm/cacheflush.h>
29#include <asm/tlbflush.h>
30
31#ifndef pgprot_modify
32static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
33{
34 return newprot;
35}
36#endif
37
38static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
39 unsigned long addr, unsigned long end, pgprot_t newprot,
40 int dirty_accountable, int prot_numa)
41{
42 struct mm_struct *mm = vma->vm_mm;
43 pte_t *pte, oldpte;
44 spinlock_t *ptl;
45 unsigned long pages = 0;
46
47 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
48 arch_enter_lazy_mmu_mode();
49 do {
50 oldpte = *pte;
51 if (pte_present(oldpte)) {
52 pte_t ptent;
53 bool updated = false;
54
55 ptent = ptep_modify_prot_start(mm, addr, pte);
56 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 }
72
73 /*
74 * Avoid taking write faults for pages we know to be
75 * dirty.
76 */
77 if (dirty_accountable && pte_dirty(ptent)) {
78 ptent = pte_mkwrite(ptent);
79 updated = true;
80 }
81
82 if (updated)
83 pages++;
84
85 ptep_modify_prot_commit(mm, addr, pte, ptent);
86 } else if (IS_ENABLED(CONFIG_MIGRATION) && !pte_file(oldpte)) {
87 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 }
98 pages++;
99 }
100 } while (pte++, addr += PAGE_SIZE, addr != end);
101 arch_leave_lazy_mmu_mode();
102 pte_unmap_unlock(pte - 1, ptl);
103
104 return pages;
105}
106
107#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
123static inline unsigned long change_pmd_range(struct vm_area_struct *vma, pud_t *pud,
124 unsigned long addr, unsigned long end, pgprot_t newprot,
125 int dirty_accountable, int prot_numa)
126{
127 pmd_t *pmd;
128 unsigned long next;
129 unsigned long pages = 0;
130
131 pmd = pmd_offset(pud, addr);
132 do {
133 next = pmd_addr_end(addr, end);
134 if (pmd_trans_huge(*pmd)) {
135 if (next - addr != HPAGE_PMD_SIZE)
136 split_huge_page_pmd(vma->vm_mm, pmd);
137 else if (change_huge_pmd(vma, pmd, addr, newprot, prot_numa)) {
138 pages += HPAGE_PMD_NR;
139 continue;
140 }
141 /* fall through */
142 }
143 if (pmd_none_or_clear_bad(pmd))
144 continue;
145 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);
150 } while (pmd++, addr = next, addr != end);
151
152 return pages;
153}
154
155static inline unsigned long change_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
156 unsigned long addr, unsigned long end, pgprot_t newprot,
157 int dirty_accountable, int prot_numa)
158{
159 pud_t *pud;
160 unsigned long next;
161 unsigned long pages = 0;
162
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;
168 pages += change_pmd_range(vma, pud, addr, next, newprot,
169 dirty_accountable, prot_numa);
170 } while (pud++, addr = next, addr != end);
171
172 return pages;
173}
174
175static unsigned long change_protection_range(struct vm_area_struct *vma,
176 unsigned long addr, unsigned long end, pgprot_t newprot,
177 int dirty_accountable, int prot_numa)
178{
179 struct mm_struct *mm = vma->vm_mm;
180 pgd_t *pgd;
181 unsigned long next;
182 unsigned long start = addr;
183 unsigned long pages = 0;
184
185 BUG_ON(addr >= end);
186 pgd = pgd_offset(mm, addr);
187 flush_cache_range(vma, addr, end);
188 do {
189 next = pgd_addr_end(addr, end);
190 if (pgd_none_or_clear_bad(pgd))
191 continue;
192 pages += change_pud_range(vma, pgd, addr, next, newprot,
193 dirty_accountable, prot_numa);
194 } while (pgd++, addr = next, addr != end);
195
196 /* Only flush the TLB if we actually modified any entries: */
197 if (pages)
198 flush_tlb_range(vma, start, end);
199
200 return pages;
201}
202
203unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
204 unsigned long end, pgprot_t newprot,
205 int dirty_accountable, int prot_numa)
206{
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
214 pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
215 mmu_notifier_invalidate_range_end(mm, start, end);
216
217 return pages;
218}
219
220int
221mprotect_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;
228 pgoff_t pgoff;
229 int error;
230 int dirty_accountable = 0;
231
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
240 * make it unwritable again. hugetlb mapping were accounted for
241 * even if read-only so there is no need to account for them here
242 */
243 if (newflags & VM_WRITE) {
244 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
245 VM_SHARED|VM_NORESERVE))) {
246 charged = nrpages;
247 if (security_vm_enough_memory_mm(mm, charged))
248 return -ENOMEM;
249 newflags |= VM_ACCOUNT;
250 }
251 }
252
253 /*
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;
284 vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
285 vm_get_page_prot(newflags));
286
287 if (vma_wants_writenotify(vma)) {
288 vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
289 dirty_accountable = 1;
290 }
291
292 change_protection(vma, start, end, vma->vm_page_prot, dirty_accountable, 0);
293
294 vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
295 vm_stat_account(mm, newflags, vma->vm_file, nrpages);
296 perf_event_mmap(vma);
297 return 0;
298
299fail:
300 vm_unacct_memory(charged);
301 return error;
302}
303
304SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
305 unsigned long, prot)
306{
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;
323 if (!arch_validate_prot(prot))
324 return -EINVAL;
325
326 reqprot = prot;
327 /*
328 * Does the application expect PROT_READ to imply PROT_EXEC:
329 */
330 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
331 prot |= PROT_EXEC;
332
333 vm_flags = calc_vm_prot_bits(prot);
334
335 down_write(&current->mm->mmap_sem);
336
337 vma = find_vma(current->mm, start);
338 error = -ENOMEM;
339 if (!vma)
340 goto out;
341 prev = vma->vm_prev;
342 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
368 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
369
370 /* newflags >> 4 shift VM_MAY% in place of VM_% */
371 if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
372 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;
386 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}