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