drivers: cpuquiet: Fix hotplug stats
[linux-3.10.git] / mm / mprotect.c
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 <linux/tegra_profiler.h>
27 #include <asm/uaccess.h>
28 #include <asm/pgtable.h>
29 #include <asm/cacheflush.h>
30 #include <asm/tlbflush.h>
31
32 #ifndef pgprot_modify
33 static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
34 {
35         return newprot;
36 }
37 #endif
38
39 static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
40                 unsigned long addr, unsigned long end, pgprot_t newprot,
41                 int dirty_accountable, int prot_numa, bool *ret_all_same_node)
42 {
43         struct mm_struct *mm = vma->vm_mm;
44         pte_t *pte, oldpte;
45         spinlock_t *ptl;
46         unsigned long pages = 0;
47         bool all_same_node = true;
48         int last_nid = -1;
49
50         pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
51         arch_enter_lazy_mmu_mode();
52         do {
53                 oldpte = *pte;
54                 if (pte_present(oldpte)) {
55                         pte_t ptent;
56                         bool updated = false;
57
58                         ptent = ptep_modify_prot_start(mm, addr, pte);
59                         if (!prot_numa) {
60                                 pte_t old_ptent = ptent;
61                                 ptent = pte_modify(ptent, newprot);
62                                 if (ptent != old_ptent)
63                                         updated = true;
64                         } else {
65                                 struct page *page;
66
67                                 page = vm_normal_page(vma, addr, oldpte);
68                                 if (page) {
69                                         int this_nid = page_to_nid(page);
70                                         if (last_nid == -1)
71                                                 last_nid = this_nid;
72                                         if (last_nid != this_nid)
73                                                 all_same_node = false;
74
75                                         /* only check non-shared pages */
76                                         if (!pte_numa(oldpte) &&
77                                             page_mapcount(page) == 1) {
78                                                 ptent = pte_mknuma(ptent);
79                                                 updated = true;
80                                         }
81                                 }
82                         }
83
84                         /*
85                          * Avoid taking write faults for pages we know to be
86                          * dirty.
87                          */
88                         if (dirty_accountable && pte_dirty(ptent)) {
89                                 ptent = pte_mkwrite(ptent);
90                                 updated = true;
91                         }
92
93                         if (updated)
94                                 pages++;
95                         ptep_modify_prot_commit(mm, addr, pte, ptent);
96                 } else if (IS_ENABLED(CONFIG_MIGRATION) && !pte_file(oldpte)) {
97                         swp_entry_t entry = pte_to_swp_entry(oldpte);
98
99                         if (is_write_migration_entry(entry)) {
100                                 /*
101                                  * A protection check is difficult so
102                                  * just be safe and disable write
103                                  */
104                                 make_migration_entry_read(&entry);
105                                 set_pte_at(mm, addr, pte,
106                                         swp_entry_to_pte(entry));
107                         }
108                         pages++;
109                 }
110         } while (pte++, addr += PAGE_SIZE, addr != end);
111         arch_leave_lazy_mmu_mode();
112         pte_unmap_unlock(pte - 1, ptl);
113
114         *ret_all_same_node = all_same_node;
115         return pages;
116 }
117
118 #ifdef CONFIG_NUMA_BALANCING
119 static inline void change_pmd_protnuma(struct mm_struct *mm, unsigned long addr,
120                                        pmd_t *pmd)
121 {
122         spin_lock(&mm->page_table_lock);
123         set_pmd_at(mm, addr & PMD_MASK, pmd, pmd_mknuma(*pmd));
124         spin_unlock(&mm->page_table_lock);
125 }
126 #else
127 static inline void change_pmd_protnuma(struct mm_struct *mm, unsigned long addr,
128                                        pmd_t *pmd)
129 {
130         BUG();
131 }
132 #endif /* CONFIG_NUMA_BALANCING */
133
134 static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
135                 pud_t *pud, unsigned long addr, unsigned long end,
136                 pgprot_t newprot, int dirty_accountable, int prot_numa)
137 {
138         pmd_t *pmd;
139         unsigned long next;
140         unsigned long pages = 0;
141         unsigned long nr_huge_updates = 0;
142         bool all_same_node;
143
144         pmd = pmd_offset(pud, addr);
145         do {
146                 next = pmd_addr_end(addr, end);
147                 if (pmd_trans_huge(*pmd)) {
148                         if (next - addr != HPAGE_PMD_SIZE)
149                                 split_huge_page_pmd(vma, addr, pmd);
150                         else if (change_huge_pmd(vma, pmd, addr, newprot,
151                                                  prot_numa)) {
152                                 pages += HPAGE_PMD_NR;
153                                 nr_huge_updates++;
154                                 continue;
155                         }
156                         /* fall through */
157                 }
158                 if (pmd_none_or_clear_bad(pmd))
159                         continue;
160                 pages += change_pte_range(vma, pmd, addr, next, newprot,
161                                  dirty_accountable, prot_numa, &all_same_node);
162
163                 /*
164                  * If we are changing protections for NUMA hinting faults then
165                  * set pmd_numa if the examined pages were all on the same
166                  * node. This allows a regular PMD to be handled as one fault
167                  * and effectively batches the taking of the PTL
168                  */
169                 if (prot_numa && all_same_node)
170                         change_pmd_protnuma(vma->vm_mm, addr, pmd);
171         } while (pmd++, addr = next, addr != end);
172
173         if (nr_huge_updates)
174                 count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
175
176         return pages;
177 }
178
179 static inline unsigned long change_pud_range(struct vm_area_struct *vma,
180                 pgd_t *pgd, unsigned long addr, unsigned long end,
181                 pgprot_t newprot, int dirty_accountable, int prot_numa)
182 {
183         pud_t *pud;
184         unsigned long next;
185         unsigned long pages = 0;
186
187         pud = pud_offset(pgd, addr);
188         do {
189                 next = pud_addr_end(addr, end);
190                 if (pud_none_or_clear_bad(pud))
191                         continue;
192                 pages += change_pmd_range(vma, pud, addr, next, newprot,
193                                  dirty_accountable, prot_numa);
194         } while (pud++, addr = next, addr != end);
195
196         return pages;
197 }
198
199 static unsigned long change_protection_range(struct vm_area_struct *vma,
200                 unsigned long addr, unsigned long end, pgprot_t newprot,
201                 int dirty_accountable, int prot_numa)
202 {
203         struct mm_struct *mm = vma->vm_mm;
204         pgd_t *pgd;
205         unsigned long next;
206         unsigned long start = addr;
207         unsigned long pages = 0;
208
209         BUG_ON(addr >= end);
210         pgd = pgd_offset(mm, addr);
211         flush_cache_range(vma, addr, end);
212         set_tlb_flush_pending(mm);
213         do {
214                 next = pgd_addr_end(addr, end);
215                 if (pgd_none_or_clear_bad(pgd))
216                         continue;
217                 pages += change_pud_range(vma, pgd, addr, next, newprot,
218                                  dirty_accountable, prot_numa);
219         } while (pgd++, addr = next, addr != end);
220
221         /* Only flush the TLB if we actually modified any entries: */
222         if (pages)
223                 flush_tlb_range(vma, start, end);
224         clear_tlb_flush_pending(mm);
225
226         return pages;
227 }
228
229 unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
230                        unsigned long end, pgprot_t newprot,
231                        int dirty_accountable, int prot_numa)
232 {
233         struct mm_struct *mm = vma->vm_mm;
234         unsigned long pages;
235
236         mmu_notifier_invalidate_range_start(mm, start, end);
237         if (is_vm_hugetlb_page(vma))
238                 pages = hugetlb_change_protection(vma, start, end, newprot);
239         else
240                 pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
241         mmu_notifier_invalidate_range_end(mm, start, end);
242
243         return pages;
244 }
245
246 int
247 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
248         unsigned long start, unsigned long end, unsigned long newflags)
249 {
250         struct mm_struct *mm = vma->vm_mm;
251         unsigned long oldflags = vma->vm_flags;
252         long nrpages = (end - start) >> PAGE_SHIFT;
253         unsigned long charged = 0;
254         pgoff_t pgoff;
255         int error;
256         int dirty_accountable = 0;
257
258         if (newflags == oldflags) {
259                 *pprev = vma;
260                 return 0;
261         }
262
263         /*
264          * If we make a private mapping writable we increase our commit;
265          * but (without finer accounting) cannot reduce our commit if we
266          * make it unwritable again. hugetlb mapping were accounted for
267          * even if read-only so there is no need to account for them here
268          */
269         if (newflags & VM_WRITE) {
270                 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
271                                                 VM_SHARED|VM_NORESERVE))) {
272                         charged = nrpages;
273                         if (security_vm_enough_memory_mm(mm, charged))
274                                 return -ENOMEM;
275                         newflags |= VM_ACCOUNT;
276                 }
277         }
278
279         /*
280          * First try to merge with previous and/or next vma.
281          */
282         pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
283         *pprev = vma_merge(mm, *pprev, start, end, newflags,
284                         vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
285                         vma_get_anon_name(vma));
286         if (*pprev) {
287                 vma = *pprev;
288                 goto success;
289         }
290
291         *pprev = vma;
292
293         if (start != vma->vm_start) {
294                 error = split_vma(mm, vma, start, 1);
295                 if (error)
296                         goto fail;
297         }
298
299         if (end != vma->vm_end) {
300                 error = split_vma(mm, vma, end, 0);
301                 if (error)
302                         goto fail;
303         }
304
305 success:
306         /*
307          * vm_flags and vm_page_prot are protected by the mmap_sem
308          * held in write mode.
309          */
310         vma->vm_flags = newflags;
311         vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
312                                           vm_get_page_prot(newflags));
313
314         if (vma_wants_writenotify(vma)) {
315                 vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
316                 dirty_accountable = 1;
317         }
318
319         change_protection(vma, start, end, vma->vm_page_prot,
320                           dirty_accountable, 0);
321
322         vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
323         vm_stat_account(mm, newflags, vma->vm_file, nrpages);
324         perf_event_mmap(vma);
325         quadd_event_mmap(vma);
326         return 0;
327
328 fail:
329         vm_unacct_memory(charged);
330         return error;
331 }
332
333 SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
334                 unsigned long, prot)
335 {
336         unsigned long vm_flags, nstart, end, tmp, reqprot;
337         struct vm_area_struct *vma, *prev;
338         int error = -EINVAL;
339         const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
340         prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
341         if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
342                 return -EINVAL;
343
344         if (start & ~PAGE_MASK)
345                 return -EINVAL;
346         if (!len)
347                 return 0;
348         len = PAGE_ALIGN(len);
349         end = start + len;
350         if (end <= start)
351                 return -ENOMEM;
352         if (!arch_validate_prot(prot))
353                 return -EINVAL;
354
355         reqprot = prot;
356         /*
357          * Does the application expect PROT_READ to imply PROT_EXEC:
358          */
359         if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
360                 prot |= PROT_EXEC;
361
362         vm_flags = calc_vm_prot_bits(prot);
363
364         down_write(&current->mm->mmap_sem);
365
366         vma = find_vma(current->mm, start);
367         error = -ENOMEM;
368         if (!vma)
369                 goto out;
370         prev = vma->vm_prev;
371         if (unlikely(grows & PROT_GROWSDOWN)) {
372                 if (vma->vm_start >= end)
373                         goto out;
374                 start = vma->vm_start;
375                 error = -EINVAL;
376                 if (!(vma->vm_flags & VM_GROWSDOWN))
377                         goto out;
378         } else {
379                 if (vma->vm_start > start)
380                         goto out;
381                 if (unlikely(grows & PROT_GROWSUP)) {
382                         end = vma->vm_end;
383                         error = -EINVAL;
384                         if (!(vma->vm_flags & VM_GROWSUP))
385                                 goto out;
386                 }
387         }
388         if (start > vma->vm_start)
389                 prev = vma;
390
391         for (nstart = start ; ; ) {
392                 unsigned long newflags;
393
394                 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
395
396                 newflags = vm_flags;
397                 newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
398
399                 /* newflags >> 4 shift VM_MAY% in place of VM_% */
400                 if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
401                         error = -EACCES;
402                         goto out;
403                 }
404
405                 error = security_file_mprotect(vma, reqprot, prot);
406                 if (error)
407                         goto out;
408
409                 tmp = vma->vm_end;
410                 if (tmp > end)
411                         tmp = end;
412                 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
413                 if (error)
414                         goto out;
415                 nstart = tmp;
416
417                 if (nstart < prev->vm_end)
418                         nstart = prev->vm_end;
419                 if (nstart >= end)
420                         goto out;
421
422                 vma = prev->vm_next;
423                 if (!vma || vma->vm_start != nstart) {
424                         error = -ENOMEM;
425                         goto out;
426                 }
427         }
428 out:
429         up_write(&current->mm->mmap_sem);
430         return error;
431 }