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