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