mm/demotion: demote pages according to allocation fallback order
[linux-2.6-block.git] / mm / mprotect.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * mm/mprotect.c
4 *
5 * (C) Copyright 1994 Linus Torvalds
6 * (C) Copyright 2002 Christoph Hellwig
7 *
046c6884 8 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
9 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
10 */
11
a520110e 12#include <linux/pagewalk.h>
1da177e4 13#include <linux/hugetlb.h>
1da177e4
LT
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>
0697212a
CL
22#include <linux/swap.h>
23#include <linux/swapops.h>
cddb8a5c 24#include <linux/mmu_notifier.h>
64cdd548 25#include <linux/migrate.h>
cdd6c482 26#include <linux/perf_event.h>
e8c24d3a 27#include <linux/pkeys.h>
64a9a34e 28#include <linux/ksm.h>
7c0f6ba6 29#include <linux/uaccess.h>
09a913a7 30#include <linux/mm_inline.h>
ca5999fd 31#include <linux/pgtable.h>
a1a3a2fc 32#include <linux/sched/sysctl.h>
fe2567eb 33#include <linux/userfaultfd_k.h>
1da177e4 34#include <asm/cacheflush.h>
e8c24d3a 35#include <asm/mmu_context.h>
1da177e4 36#include <asm/tlbflush.h>
4a18419f 37#include <asm/tlb.h>
1da177e4 38
36f88188
KS
39#include "internal.h"
40
64fe24a3
DH
41static inline bool can_change_pte_writable(struct vm_area_struct *vma,
42 unsigned long addr, pte_t pte)
43{
44 struct page *page;
45
46 VM_BUG_ON(!(vma->vm_flags & VM_WRITE) || pte_write(pte));
47
48 if (pte_protnone(pte) || !pte_dirty(pte))
49 return false;
50
51 /* Do we need write faults for softdirty tracking? */
76aefad6 52 if (vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte))
64fe24a3
DH
53 return false;
54
55 /* Do we need write faults for uffd-wp tracking? */
56 if (userfaultfd_pte_wp(vma, pte))
57 return false;
58
59 if (!(vma->vm_flags & VM_SHARED)) {
60 /*
61 * We can only special-case on exclusive anonymous pages,
62 * because we know that our write-fault handler similarly would
63 * map them writable without any additional checks while holding
64 * the PT lock.
65 */
66 page = vm_normal_page(vma, addr, pte);
67 if (!page || !PageAnon(page) || !PageAnonExclusive(page))
68 return false;
69 }
70
71 return true;
72}
73
4a18419f
NA
74static unsigned long change_pte_range(struct mmu_gather *tlb,
75 struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
76 unsigned long end, pgprot_t newprot, unsigned long cp_flags)
1da177e4 77{
0697212a 78 pte_t *pte, oldpte;
705e87c0 79 spinlock_t *ptl;
7da4d641 80 unsigned long pages = 0;
3e321587 81 int target_node = NUMA_NO_NODE;
58705444 82 bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
292924b2
PX
83 bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
84 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
1da177e4 85
4a18419f
NA
86 tlb_change_page_size(tlb, PAGE_SIZE);
87
175ad4f1 88 /*
c1e8d7c6 89 * Can be called with only the mmap_lock for reading by
175ad4f1
AA
90 * prot_numa so we must check the pmd isn't constantly
91 * changing from under us from pmd_none to pmd_trans_huge
92 * and/or the other way around.
93 */
94 if (pmd_trans_unstable(pmd))
95 return 0;
96
97 /*
98 * The pmd points to a regular pte so the pmd can't change
c1e8d7c6 99 * from under us even if the mmap_lock is only hold for
175ad4f1
AA
100 * reading.
101 */
102 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1ad9f620 103
3e321587
AK
104 /* Get target node for single threaded private VMAs */
105 if (prot_numa && !(vma->vm_flags & VM_SHARED) &&
106 atomic_read(&vma->vm_mm->mm_users) == 1)
107 target_node = numa_node_id();
108
3ea27719 109 flush_tlb_batched_pending(vma->vm_mm);
6606c3e0 110 arch_enter_lazy_mmu_mode();
1da177e4 111 do {
0697212a
CL
112 oldpte = *pte;
113 if (pte_present(oldpte)) {
1da177e4 114 pte_t ptent;
b191f9b1 115 bool preserve_write = prot_numa && pte_write(oldpte);
1da177e4 116
e944fd67
MG
117 /*
118 * Avoid trapping faults against the zero or KSM
119 * pages. See similar comment in change_huge_pmd.
120 */
121 if (prot_numa) {
122 struct page *page;
a1a3a2fc 123 int nid;
33024536 124 bool toptier;
e944fd67 125
a818f536
HY
126 /* Avoid TLB flush if possible */
127 if (pte_protnone(oldpte))
128 continue;
129
e944fd67 130 page = vm_normal_page(vma, addr, oldpte);
3218f871 131 if (!page || is_zone_device_page(page) || PageKsm(page))
e944fd67 132 continue;
10c1045f 133
859d4adc
HW
134 /* Also skip shared copy-on-write pages */
135 if (is_cow_mapping(vma->vm_flags) &&
80d47f5d 136 page_count(page) != 1)
859d4adc
HW
137 continue;
138
09a913a7
MG
139 /*
140 * While migration can move some dirty pages,
141 * it cannot move them all from MIGRATE_ASYNC
142 * context.
143 */
9de4f22a 144 if (page_is_file_lru(page) && PageDirty(page))
09a913a7
MG
145 continue;
146
3e321587
AK
147 /*
148 * Don't mess with PTEs if page is already on the node
149 * a single-threaded process is running on.
150 */
a1a3a2fc
HY
151 nid = page_to_nid(page);
152 if (target_node == nid)
153 continue;
33024536 154 toptier = node_is_toptier(nid);
a1a3a2fc
HY
155
156 /*
157 * Skip scanning top tier node if normal numa
158 * balancing is disabled
159 */
160 if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
33024536 161 toptier)
3e321587 162 continue;
33024536
HY
163 if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING &&
164 !toptier)
165 xchg_page_access_time(page,
166 jiffies_to_msecs(jiffies));
e944fd67
MG
167 }
168
04a86453
AK
169 oldpte = ptep_modify_prot_start(vma, addr, pte);
170 ptent = pte_modify(oldpte, newprot);
b191f9b1 171 if (preserve_write)
288bc549 172 ptent = pte_mk_savedwrite(ptent);
4b10e7d5 173
292924b2
PX
174 if (uffd_wp) {
175 ptent = pte_wrprotect(ptent);
176 ptent = pte_mkuffd_wp(ptent);
177 } else if (uffd_wp_resolve) {
292924b2
PX
178 ptent = pte_clear_uffd_wp(ptent);
179 }
180
64fe24a3
DH
181 /*
182 * In some writable, shared mappings, we might want
183 * to catch actual write access -- see
184 * vma_wants_writenotify().
185 *
186 * In all writable, private mappings, we have to
187 * properly handle COW.
188 *
189 * In both cases, we can sometimes still change PTEs
190 * writable and avoid the write-fault handler, for
191 * example, if a PTE is already dirty and no other
192 * COW or special handling is required.
193 */
194 if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
195 !pte_write(ptent) &&
196 can_change_pte_writable(vma, addr, ptent))
8a0516ed 197 ptent = pte_mkwrite(ptent);
64fe24a3 198
04a86453 199 ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
c9fe6656
NA
200 if (pte_needs_flush(oldpte, ptent))
201 tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
8a0516ed 202 pages++;
f45ec5ff 203 } else if (is_swap_pte(oldpte)) {
0697212a 204 swp_entry_t entry = pte_to_swp_entry(oldpte);
f45ec5ff 205 pte_t newpte;
0697212a 206
4dd845b5 207 if (is_writable_migration_entry(entry)) {
3d2f78f0
PX
208 struct page *page = pfn_swap_entry_to_page(entry);
209
0697212a
CL
210 /*
211 * A protection check is difficult so
212 * just be safe and disable write
213 */
6c287605
DH
214 if (PageAnon(page))
215 entry = make_readable_exclusive_migration_entry(
216 swp_offset(entry));
217 else
218 entry = make_readable_migration_entry(swp_offset(entry));
c3d16e16
CG
219 newpte = swp_entry_to_pte(entry);
220 if (pte_swp_soft_dirty(oldpte))
221 newpte = pte_swp_mksoft_dirty(newpte);
f45ec5ff
PX
222 if (pte_swp_uffd_wp(oldpte))
223 newpte = pte_swp_mkuffd_wp(newpte);
4dd845b5 224 } else if (is_writable_device_private_entry(entry)) {
5042db43
JG
225 /*
226 * We do not preserve soft-dirtiness. See
227 * copy_one_pte() for explanation.
228 */
4dd845b5
AP
229 entry = make_readable_device_private_entry(
230 swp_offset(entry));
5042db43 231 newpte = swp_entry_to_pte(entry);
f45ec5ff
PX
232 if (pte_swp_uffd_wp(oldpte))
233 newpte = pte_swp_mkuffd_wp(newpte);
b756a3b5
AP
234 } else if (is_writable_device_exclusive_entry(entry)) {
235 entry = make_readable_device_exclusive_entry(
236 swp_offset(entry));
237 newpte = swp_entry_to_pte(entry);
238 if (pte_swp_soft_dirty(oldpte))
239 newpte = pte_swp_mksoft_dirty(newpte);
240 if (pte_swp_uffd_wp(oldpte))
241 newpte = pte_swp_mkuffd_wp(newpte);
fe2567eb
PX
242 } else if (pte_marker_entry_uffd_wp(entry)) {
243 /*
244 * If this is uffd-wp pte marker and we'd like
245 * to unprotect it, drop it; the next page
246 * fault will trigger without uffd trapping.
247 */
248 if (uffd_wp_resolve) {
249 pte_clear(vma->vm_mm, addr, pte);
250 pages++;
251 }
5c041f5d 252 continue;
f45ec5ff
PX
253 } else {
254 newpte = oldpte;
255 }
5042db43 256
f45ec5ff
PX
257 if (uffd_wp)
258 newpte = pte_swp_mkuffd_wp(newpte);
259 else if (uffd_wp_resolve)
260 newpte = pte_swp_clear_uffd_wp(newpte);
261
262 if (!pte_same(oldpte, newpte)) {
263 set_pte_at(vma->vm_mm, addr, pte, newpte);
5042db43
JG
264 pages++;
265 }
fe2567eb
PX
266 } else {
267 /* It must be an none page, or what else?.. */
268 WARN_ON_ONCE(!pte_none(oldpte));
269 if (unlikely(uffd_wp && !vma_is_anonymous(vma))) {
270 /*
271 * For file-backed mem, we need to be able to
272 * wr-protect a none pte, because even if the
273 * pte is none, the page/swap cache could
274 * exist. Doing that by install a marker.
275 */
276 set_pte_at(vma->vm_mm, addr, pte,
277 make_pte_marker(PTE_MARKER_UFFD_WP));
278 pages++;
279 }
1da177e4
LT
280 }
281 } while (pte++, addr += PAGE_SIZE, addr != end);
6606c3e0 282 arch_leave_lazy_mmu_mode();
705e87c0 283 pte_unmap_unlock(pte - 1, ptl);
7da4d641
PZ
284
285 return pages;
1da177e4
LT
286}
287
8b272b3c
MG
288/*
289 * Used when setting automatic NUMA hinting protection where it is
290 * critical that a numa hinting PMD is not confused with a bad PMD.
291 */
292static inline int pmd_none_or_clear_bad_unless_trans_huge(pmd_t *pmd)
293{
294 pmd_t pmdval = pmd_read_atomic(pmd);
295
296 /* See pmd_none_or_trans_huge_or_clear_bad for info on barrier */
297#ifdef CONFIG_TRANSPARENT_HUGEPAGE
298 barrier();
299#endif
300
301 if (pmd_none(pmdval))
302 return 1;
303 if (pmd_trans_huge(pmdval))
304 return 0;
305 if (unlikely(pmd_bad(pmdval))) {
306 pmd_clear_bad(pmd);
307 return 1;
308 }
309
310 return 0;
311}
312
fe2567eb
PX
313/* Return true if we're uffd wr-protecting file-backed memory, or false */
314static inline bool
315uffd_wp_protect_file(struct vm_area_struct *vma, unsigned long cp_flags)
316{
317 return (cp_flags & MM_CP_UFFD_WP) && !vma_is_anonymous(vma);
318}
319
320/*
321 * If wr-protecting the range for file-backed, populate pgtable for the case
322 * when pgtable is empty but page cache exists. When {pte|pmd|...}_alloc()
323 * failed it means no memory, we don't have a better option but stop.
324 */
325#define change_pmd_prepare(vma, pmd, cp_flags) \
326 do { \
327 if (unlikely(uffd_wp_protect_file(vma, cp_flags))) { \
328 if (WARN_ON_ONCE(pte_alloc(vma->vm_mm, pmd))) \
329 break; \
330 } \
331 } while (0)
332/*
333 * This is the general pud/p4d/pgd version of change_pmd_prepare(). We need to
334 * have separate change_pmd_prepare() because pte_alloc() returns 0 on success,
335 * while {pmd|pud|p4d}_alloc() returns the valid pointer on success.
336 */
337#define change_prepare(vma, high, low, addr, cp_flags) \
338 do { \
339 if (unlikely(uffd_wp_protect_file(vma, cp_flags))) { \
340 low##_t *p = low##_alloc(vma->vm_mm, high, addr); \
341 if (WARN_ON_ONCE(p == NULL)) \
342 break; \
343 } \
344 } while (0)
345
4a18419f
NA
346static inline unsigned long change_pmd_range(struct mmu_gather *tlb,
347 struct vm_area_struct *vma, pud_t *pud, unsigned long addr,
348 unsigned long end, pgprot_t newprot, unsigned long cp_flags)
1da177e4
LT
349{
350 pmd_t *pmd;
351 unsigned long next;
7da4d641 352 unsigned long pages = 0;
72403b4a 353 unsigned long nr_huge_updates = 0;
ac46d4f3
JG
354 struct mmu_notifier_range range;
355
356 range.start = 0;
1da177e4
LT
357
358 pmd = pmd_offset(pud, addr);
359 do {
25cbbef1
MG
360 unsigned long this_pages;
361
1da177e4 362 next = pmd_addr_end(addr, end);
8b272b3c 363
fe2567eb 364 change_pmd_prepare(vma, pmd, cp_flags);
8b272b3c 365 /*
c1e8d7c6 366 * Automatic NUMA balancing walks the tables with mmap_lock
8b272b3c
MG
367 * held for read. It's possible a parallel update to occur
368 * between pmd_trans_huge() and a pmd_none_or_clear_bad()
369 * check leading to a false positive and clearing.
370 * Hence, it's necessary to atomically read the PMD value
371 * for all the checks.
372 */
373 if (!is_swap_pmd(*pmd) && !pmd_devmap(*pmd) &&
374 pmd_none_or_clear_bad_unless_trans_huge(pmd))
4991c09c 375 goto next;
a5338093
RR
376
377 /* invoke the mmu notifier if the pmd is populated */
ac46d4f3 378 if (!range.start) {
7269f999
JG
379 mmu_notifier_range_init(&range,
380 MMU_NOTIFY_PROTECTION_VMA, 0,
381 vma, vma->vm_mm, addr, end);
ac46d4f3 382 mmu_notifier_invalidate_range_start(&range);
a5338093
RR
383 }
384
84c3fc4e 385 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
019c2d8b
PX
386 if ((next - addr != HPAGE_PMD_SIZE) ||
387 uffd_wp_protect_file(vma, cp_flags)) {
fd60775a 388 __split_huge_pmd(vma, pmd, addr, false, NULL);
019c2d8b
PX
389 /*
390 * For file-backed, the pmd could have been
391 * cleared; make sure pmd populated if
392 * necessary, then fall-through to pte level.
393 */
394 change_pmd_prepare(vma, pmd, cp_flags);
6b9116a6 395 } else {
4a18419f
NA
396 /*
397 * change_huge_pmd() does not defer TLB flushes,
398 * so no need to propagate the tlb argument.
399 */
400 int nr_ptes = change_huge_pmd(tlb, vma, pmd,
401 addr, newprot, cp_flags);
f123d74a
MG
402
403 if (nr_ptes) {
72403b4a
MG
404 if (nr_ptes == HPAGE_PMD_NR) {
405 pages += HPAGE_PMD_NR;
406 nr_huge_updates++;
407 }
1ad9f620
MG
408
409 /* huge pmd was handled */
4991c09c 410 goto next;
f123d74a 411 }
7da4d641 412 }
88a9ab6e 413 /* fall through, the trans huge pmd just split */
cd7548ab 414 }
4a18419f
NA
415 this_pages = change_pte_range(tlb, vma, pmd, addr, next,
416 newprot, cp_flags);
25cbbef1 417 pages += this_pages;
4991c09c
AK
418next:
419 cond_resched();
1da177e4 420 } while (pmd++, addr = next, addr != end);
7da4d641 421
ac46d4f3
JG
422 if (range.start)
423 mmu_notifier_invalidate_range_end(&range);
a5338093 424
72403b4a
MG
425 if (nr_huge_updates)
426 count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
7da4d641 427 return pages;
1da177e4
LT
428}
429
4a18419f
NA
430static inline unsigned long change_pud_range(struct mmu_gather *tlb,
431 struct vm_area_struct *vma, p4d_t *p4d, unsigned long addr,
432 unsigned long end, pgprot_t newprot, unsigned long cp_flags)
1da177e4
LT
433{
434 pud_t *pud;
435 unsigned long next;
7da4d641 436 unsigned long pages = 0;
1da177e4 437
c2febafc 438 pud = pud_offset(p4d, addr);
1da177e4
LT
439 do {
440 next = pud_addr_end(addr, end);
fe2567eb 441 change_prepare(vma, pud, pmd, addr, cp_flags);
1da177e4
LT
442 if (pud_none_or_clear_bad(pud))
443 continue;
4a18419f 444 pages += change_pmd_range(tlb, vma, pud, addr, next, newprot,
58705444 445 cp_flags);
1da177e4 446 } while (pud++, addr = next, addr != end);
7da4d641
PZ
447
448 return pages;
1da177e4
LT
449}
450
4a18419f
NA
451static inline unsigned long change_p4d_range(struct mmu_gather *tlb,
452 struct vm_area_struct *vma, pgd_t *pgd, unsigned long addr,
453 unsigned long end, pgprot_t newprot, unsigned long cp_flags)
c2febafc
KS
454{
455 p4d_t *p4d;
456 unsigned long next;
457 unsigned long pages = 0;
458
459 p4d = p4d_offset(pgd, addr);
460 do {
461 next = p4d_addr_end(addr, end);
fe2567eb 462 change_prepare(vma, p4d, pud, addr, cp_flags);
c2febafc
KS
463 if (p4d_none_or_clear_bad(p4d))
464 continue;
4a18419f 465 pages += change_pud_range(tlb, vma, p4d, addr, next, newprot,
58705444 466 cp_flags);
c2febafc
KS
467 } while (p4d++, addr = next, addr != end);
468
469 return pages;
470}
471
4a18419f
NA
472static unsigned long change_protection_range(struct mmu_gather *tlb,
473 struct vm_area_struct *vma, unsigned long addr,
474 unsigned long end, pgprot_t newprot, unsigned long cp_flags)
1da177e4
LT
475{
476 struct mm_struct *mm = vma->vm_mm;
477 pgd_t *pgd;
478 unsigned long next;
7da4d641 479 unsigned long pages = 0;
1da177e4
LT
480
481 BUG_ON(addr >= end);
482 pgd = pgd_offset(mm, addr);
4a18419f 483 tlb_start_vma(tlb, vma);
1da177e4
LT
484 do {
485 next = pgd_addr_end(addr, end);
fe2567eb 486 change_prepare(vma, pgd, p4d, addr, cp_flags);
1da177e4
LT
487 if (pgd_none_or_clear_bad(pgd))
488 continue;
4a18419f 489 pages += change_p4d_range(tlb, vma, pgd, addr, next, newprot,
58705444 490 cp_flags);
1da177e4 491 } while (pgd++, addr = next, addr != end);
7da4d641 492
4a18419f 493 tlb_end_vma(tlb, vma);
7da4d641
PZ
494
495 return pages;
496}
497
4a18419f
NA
498unsigned long change_protection(struct mmu_gather *tlb,
499 struct vm_area_struct *vma, unsigned long start,
7da4d641 500 unsigned long end, pgprot_t newprot,
58705444 501 unsigned long cp_flags)
7da4d641 502{
7da4d641
PZ
503 unsigned long pages;
504
292924b2
PX
505 BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
506
7da4d641 507 if (is_vm_hugetlb_page(vma))
5a90d5a1
PX
508 pages = hugetlb_change_protection(vma, start, end, newprot,
509 cp_flags);
7da4d641 510 else
4a18419f 511 pages = change_protection_range(tlb, vma, start, end, newprot,
58705444 512 cp_flags);
7da4d641
PZ
513
514 return pages;
1da177e4
LT
515}
516
42e4089c
AK
517static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
518 unsigned long next, struct mm_walk *walk)
519{
520 return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
521 0 : -EACCES;
522}
523
524static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
525 unsigned long addr, unsigned long next,
526 struct mm_walk *walk)
527{
528 return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
529 0 : -EACCES;
530}
531
532static int prot_none_test(unsigned long addr, unsigned long next,
533 struct mm_walk *walk)
534{
535 return 0;
536}
537
7b86ac33
CH
538static const struct mm_walk_ops prot_none_walk_ops = {
539 .pte_entry = prot_none_pte_entry,
540 .hugetlb_entry = prot_none_hugetlb_entry,
541 .test_walk = prot_none_test,
542};
42e4089c 543
b6a2fea3 544int
4a18419f
NA
545mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma,
546 struct vm_area_struct **pprev, unsigned long start,
547 unsigned long end, unsigned long newflags)
1da177e4
LT
548{
549 struct mm_struct *mm = vma->vm_mm;
550 unsigned long oldflags = vma->vm_flags;
551 long nrpages = (end - start) >> PAGE_SHIFT;
552 unsigned long charged = 0;
64fe24a3 553 bool try_change_writable;
1da177e4
LT
554 pgoff_t pgoff;
555 int error;
556
557 if (newflags == oldflags) {
558 *pprev = vma;
559 return 0;
560 }
561
42e4089c
AK
562 /*
563 * Do PROT_NONE PFN permission checks here when we can still
564 * bail out without undoing a lot of state. This is a rather
565 * uncommon case, so doesn't need to be very optimized.
566 */
567 if (arch_has_pfn_modify_check() &&
568 (vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
6cb4d9a2 569 (newflags & VM_ACCESS_FLAGS) == 0) {
7b86ac33
CH
570 pgprot_t new_pgprot = vm_get_page_prot(newflags);
571
572 error = walk_page_range(current->mm, start, end,
573 &prot_none_walk_ops, &new_pgprot);
42e4089c
AK
574 if (error)
575 return error;
576 }
577
1da177e4
LT
578 /*
579 * If we make a private mapping writable we increase our commit;
580 * but (without finer accounting) cannot reduce our commit if we
5a6fe125
MG
581 * make it unwritable again. hugetlb mapping were accounted for
582 * even if read-only so there is no need to account for them here
1da177e4
LT
583 */
584 if (newflags & VM_WRITE) {
84638335
KK
585 /* Check space limits when area turns into data. */
586 if (!may_expand_vm(mm, newflags, nrpages) &&
587 may_expand_vm(mm, oldflags, nrpages))
588 return -ENOMEM;
5a6fe125 589 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
cdfd4325 590 VM_SHARED|VM_NORESERVE))) {
1da177e4 591 charged = nrpages;
191c5424 592 if (security_vm_enough_memory_mm(mm, charged))
1da177e4
LT
593 return -ENOMEM;
594 newflags |= VM_ACCOUNT;
595 }
596 }
597
1da177e4
LT
598 /*
599 * First try to merge with previous and/or next vma.
600 */
601 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
602 *pprev = vma_merge(mm, *pprev, start, end, newflags,
19a809af 603 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
5c26f6ac 604 vma->vm_userfaultfd_ctx, anon_vma_name(vma));
1da177e4
LT
605 if (*pprev) {
606 vma = *pprev;
e86f15ee 607 VM_WARN_ON((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY);
1da177e4
LT
608 goto success;
609 }
610
611 *pprev = vma;
612
613 if (start != vma->vm_start) {
614 error = split_vma(mm, vma, start, 1);
615 if (error)
616 goto fail;
617 }
618
619 if (end != vma->vm_end) {
620 error = split_vma(mm, vma, end, 0);
621 if (error)
622 goto fail;
623 }
624
625success:
626 /*
c1e8d7c6 627 * vm_flags and vm_page_prot are protected by the mmap_lock
1da177e4
LT
628 * held in write mode.
629 */
630 vma->vm_flags = newflags;
64fe24a3
DH
631 /*
632 * We want to check manually if we can change individual PTEs writable
633 * if we can't do that automatically for all PTEs in a mapping. For
634 * private mappings, that's always the case when we have write
635 * permissions as we properly have to handle COW.
636 */
637 if (vma->vm_flags & VM_SHARED)
638 try_change_writable = vma_wants_writenotify(vma, vma->vm_page_prot);
639 else
640 try_change_writable = !!(vma->vm_flags & VM_WRITE);
64e45507 641 vma_set_page_prot(vma);
d08b3851 642
4a18419f 643 change_protection(tlb, vma, start, end, vma->vm_page_prot,
64fe24a3 644 try_change_writable ? MM_CP_TRY_CHANGE_WRITABLE : 0);
7da4d641 645
36f88188
KS
646 /*
647 * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
648 * fault on access.
649 */
650 if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
651 (newflags & VM_WRITE)) {
652 populate_vma_page_range(vma, start, end, NULL);
653 }
654
84638335
KK
655 vm_stat_account(mm, oldflags, -nrpages);
656 vm_stat_account(mm, newflags, nrpages);
63bfd738 657 perf_event_mmap(vma);
1da177e4
LT
658 return 0;
659
660fail:
661 vm_unacct_memory(charged);
662 return error;
663}
664
7d06d9c9
DH
665/*
666 * pkey==-1 when doing a legacy mprotect()
667 */
668static int do_mprotect_pkey(unsigned long start, size_t len,
669 unsigned long prot, int pkey)
1da177e4 670{
62b5f7d0 671 unsigned long nstart, end, tmp, reqprot;
1da177e4 672 struct vm_area_struct *vma, *prev;
48725bbc 673 int error;
1da177e4 674 const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
f138556d
PK
675 const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
676 (prot & PROT_READ);
4a18419f 677 struct mmu_gather tlb;
f138556d 678
057d3389
AK
679 start = untagged_addr(start);
680
1da177e4
LT
681 prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
682 if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
683 return -EINVAL;
684
685 if (start & ~PAGE_MASK)
686 return -EINVAL;
687 if (!len)
688 return 0;
689 len = PAGE_ALIGN(len);
690 end = start + len;
691 if (end <= start)
692 return -ENOMEM;
9035cf9a 693 if (!arch_validate_prot(prot, start))
1da177e4
LT
694 return -EINVAL;
695
696 reqprot = prot;
1da177e4 697
d8ed45c5 698 if (mmap_write_lock_killable(current->mm))
dc0ef0df 699 return -EINTR;
1da177e4 700
e8c24d3a
DH
701 /*
702 * If userspace did not allocate the pkey, do not let
703 * them use it here.
704 */
705 error = -EINVAL;
706 if ((pkey != -1) && !mm_pkey_is_allocated(current->mm, pkey))
707 goto out;
708
097d5910 709 vma = find_vma(current->mm, start);
1da177e4
LT
710 error = -ENOMEM;
711 if (!vma)
712 goto out;
6af5fa0d 713
1da177e4
LT
714 if (unlikely(grows & PROT_GROWSDOWN)) {
715 if (vma->vm_start >= end)
716 goto out;
717 start = vma->vm_start;
718 error = -EINVAL;
719 if (!(vma->vm_flags & VM_GROWSDOWN))
720 goto out;
7d12efae 721 } else {
1da177e4
LT
722 if (vma->vm_start > start)
723 goto out;
724 if (unlikely(grows & PROT_GROWSUP)) {
725 end = vma->vm_end;
726 error = -EINVAL;
727 if (!(vma->vm_flags & VM_GROWSUP))
728 goto out;
729 }
730 }
6af5fa0d 731
1da177e4
LT
732 if (start > vma->vm_start)
733 prev = vma;
6af5fa0d
LS
734 else
735 prev = vma->vm_prev;
1da177e4 736
4a18419f 737 tlb_gather_mmu(&tlb, current->mm);
1da177e4 738 for (nstart = start ; ; ) {
a8502b67 739 unsigned long mask_off_old_flags;
1da177e4 740 unsigned long newflags;
7d06d9c9 741 int new_vma_pkey;
1da177e4 742
7d12efae 743 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
1da177e4 744
f138556d
PK
745 /* Does the application expect PROT_READ to imply PROT_EXEC */
746 if (rier && (vma->vm_flags & VM_MAYEXEC))
747 prot |= PROT_EXEC;
748
a8502b67
DH
749 /*
750 * Each mprotect() call explicitly passes r/w/x permissions.
751 * If a permission is not passed to mprotect(), it must be
752 * cleared from the VMA.
753 */
754 mask_off_old_flags = VM_READ | VM_WRITE | VM_EXEC |
2c2d57b5 755 VM_FLAGS_CLEAR;
a8502b67 756
7d06d9c9
DH
757 new_vma_pkey = arch_override_mprotect_pkey(vma, prot, pkey);
758 newflags = calc_vm_prot_bits(prot, new_vma_pkey);
a8502b67 759 newflags |= (vma->vm_flags & ~mask_off_old_flags);
1da177e4 760
7e2cff42 761 /* newflags >> 4 shift VM_MAY% in place of VM_% */
6cb4d9a2 762 if ((newflags & ~(newflags >> 4)) & VM_ACCESS_FLAGS) {
1da177e4 763 error = -EACCES;
4a18419f 764 break;
1da177e4
LT
765 }
766
c462ac28
CM
767 /* Allow architectures to sanity-check the new flags */
768 if (!arch_validate_flags(newflags)) {
769 error = -EINVAL;
4a18419f 770 break;
c462ac28
CM
771 }
772
1da177e4
LT
773 error = security_file_mprotect(vma, reqprot, prot);
774 if (error)
4a18419f 775 break;
1da177e4
LT
776
777 tmp = vma->vm_end;
778 if (tmp > end)
779 tmp = end;
95bb7c42 780
dbf53f75 781 if (vma->vm_ops && vma->vm_ops->mprotect) {
95bb7c42 782 error = vma->vm_ops->mprotect(vma, nstart, tmp, newflags);
dbf53f75 783 if (error)
4a18419f 784 break;
dbf53f75 785 }
95bb7c42 786
4a18419f 787 error = mprotect_fixup(&tlb, vma, &prev, nstart, tmp, newflags);
1da177e4 788 if (error)
4a18419f 789 break;
95bb7c42 790
1da177e4
LT
791 nstart = tmp;
792
793 if (nstart < prev->vm_end)
794 nstart = prev->vm_end;
795 if (nstart >= end)
4a18419f 796 break;
1da177e4
LT
797
798 vma = prev->vm_next;
799 if (!vma || vma->vm_start != nstart) {
800 error = -ENOMEM;
4a18419f 801 break;
1da177e4 802 }
f138556d 803 prot = reqprot;
1da177e4 804 }
4a18419f 805 tlb_finish_mmu(&tlb);
1da177e4 806out:
d8ed45c5 807 mmap_write_unlock(current->mm);
1da177e4
LT
808 return error;
809}
7d06d9c9
DH
810
811SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
812 unsigned long, prot)
813{
814 return do_mprotect_pkey(start, len, prot, -1);
815}
816
c7142aea
HC
817#ifdef CONFIG_ARCH_HAS_PKEYS
818
7d06d9c9
DH
819SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
820 unsigned long, prot, int, pkey)
821{
822 return do_mprotect_pkey(start, len, prot, pkey);
823}
e8c24d3a
DH
824
825SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)
826{
827 int pkey;
828 int ret;
829
830 /* No flags supported yet. */
831 if (flags)
832 return -EINVAL;
833 /* check for unsupported init values */
834 if (init_val & ~PKEY_ACCESS_MASK)
835 return -EINVAL;
836
d8ed45c5 837 mmap_write_lock(current->mm);
e8c24d3a
DH
838 pkey = mm_pkey_alloc(current->mm);
839
840 ret = -ENOSPC;
841 if (pkey == -1)
842 goto out;
843
844 ret = arch_set_user_pkey_access(current, pkey, init_val);
845 if (ret) {
846 mm_pkey_free(current->mm, pkey);
847 goto out;
848 }
849 ret = pkey;
850out:
d8ed45c5 851 mmap_write_unlock(current->mm);
e8c24d3a
DH
852 return ret;
853}
854
855SYSCALL_DEFINE1(pkey_free, int, pkey)
856{
857 int ret;
858
d8ed45c5 859 mmap_write_lock(current->mm);
e8c24d3a 860 ret = mm_pkey_free(current->mm, pkey);
d8ed45c5 861 mmap_write_unlock(current->mm);
e8c24d3a
DH
862
863 /*
f0953a1b 864 * We could provide warnings or errors if any VMA still
e8c24d3a
DH
865 * has the pkey set here.
866 */
867 return ret;
868}
c7142aea
HC
869
870#endif /* CONFIG_ARCH_HAS_PKEYS */