blob: 046e0889e65f5d9881f8aae4a89c4d830ccfb39c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * mm/mprotect.c
4 *
5 * (C) Copyright 1994 Linus Torvalds
6 * (C) Copyright 2002 Christoph Hellwig
7 *
Alan Cox046c6882009-01-05 14:06:29 +00008 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
10 */
11
Christoph Hellwiga5201102019-08-28 16:19:53 +020012#include <linux/pagewalk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/hugetlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Christoph Lameter06972122006-06-23 02:03:35 -070022#include <linux/swap.h>
23#include <linux/swapops.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070024#include <linux/mmu_notifier.h>
KOSAKI Motohiro64cdd542009-01-06 14:39:16 -080025#include <linux/migrate.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020026#include <linux/perf_event.h>
Dave Hansene8c24d32016-07-29 09:30:15 -070027#include <linux/pkeys.h>
Mel Gorman64a9a342014-01-21 15:51:02 -080028#include <linux/ksm.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Mel Gorman09a913a2018-04-10 16:29:20 -070030#include <linux/mm_inline.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/pgtable.h>
32#include <asm/cacheflush.h>
Dave Hansene8c24d32016-07-29 09:30:15 -070033#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/tlbflush.h>
35
Kirill A. Shutemov36f88182015-06-24 16:56:10 -070036#include "internal.h"
37
Mel Gorman4b10e7d2012-10-25 14:16:32 +020038static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
Peter Zijlstrac1e60982006-09-25 23:30:59 -070039 unsigned long addr, unsigned long end, pgprot_t newprot,
Peter Xu58705442020-04-06 20:05:45 -070040 unsigned long cp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Christoph Lameter06972122006-06-23 02:03:35 -070042 pte_t *pte, oldpte;
Hugh Dickins705e87c2005-10-29 18:16:27 -070043 spinlock_t *ptl;
Peter Zijlstra7da4d642012-11-19 03:14:23 +010044 unsigned long pages = 0;
Andi Kleen3e321582016-12-12 16:41:47 -080045 int target_node = NUMA_NO_NODE;
Peter Xu58705442020-04-06 20:05:45 -070046 bool dirty_accountable = cp_flags & MM_CP_DIRTY_ACCT;
47 bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andrea Arcangeli175ad4f2017-02-22 15:44:12 -080049 /*
50 * Can be called with only the mmap_sem for reading by
51 * prot_numa so we must check the pmd isn't constantly
52 * changing from under us from pmd_none to pmd_trans_huge
53 * and/or the other way around.
54 */
55 if (pmd_trans_unstable(pmd))
56 return 0;
57
58 /*
59 * The pmd points to a regular pte so the pmd can't change
60 * from under us even if the mmap_sem is only hold for
61 * reading.
62 */
63 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Mel Gorman1ad9f622014-04-07 15:36:56 -070064
Andi Kleen3e321582016-12-12 16:41:47 -080065 /* Get target node for single threaded private VMAs */
66 if (prot_numa && !(vma->vm_flags & VM_SHARED) &&
67 atomic_read(&vma->vm_mm->mm_users) == 1)
68 target_node = numa_node_id();
69
Mel Gorman3ea27712017-08-02 13:31:52 -070070 flush_tlb_batched_pending(vma->vm_mm);
Zachary Amsden6606c3e2006-09-30 23:29:33 -070071 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 do {
Christoph Lameter06972122006-06-23 02:03:35 -070073 oldpte = *pte;
74 if (pte_present(oldpte)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 pte_t ptent;
Mel Gormanb191f9b2015-03-25 15:55:40 -070076 bool preserve_write = prot_numa && pte_write(oldpte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Mel Gormane944fd62015-02-12 14:58:35 -080078 /*
79 * Avoid trapping faults against the zero or KSM
80 * pages. See similar comment in change_huge_pmd.
81 */
82 if (prot_numa) {
83 struct page *page;
84
Huang Yinga818f532019-11-30 17:57:32 -080085 /* Avoid TLB flush if possible */
86 if (pte_protnone(oldpte))
87 continue;
88
Mel Gormane944fd62015-02-12 14:58:35 -080089 page = vm_normal_page(vma, addr, oldpte);
90 if (!page || PageKsm(page))
91 continue;
Mel Gorman10c10452015-02-12 14:58:44 -080092
Henry Willard859d4ad2018-01-31 16:21:07 -080093 /* Also skip shared copy-on-write pages */
94 if (is_cow_mapping(vma->vm_flags) &&
95 page_mapcount(page) != 1)
96 continue;
97
Mel Gorman09a913a2018-04-10 16:29:20 -070098 /*
99 * While migration can move some dirty pages,
100 * it cannot move them all from MIGRATE_ASYNC
101 * context.
102 */
Huang Ying9de4f222020-04-06 20:04:41 -0700103 if (page_is_file_lru(page) && PageDirty(page))
Mel Gorman09a913a2018-04-10 16:29:20 -0700104 continue;
105
Andi Kleen3e321582016-12-12 16:41:47 -0800106 /*
107 * Don't mess with PTEs if page is already on the node
108 * a single-threaded process is running on.
109 */
110 if (target_node == page_to_nid(page))
111 continue;
Mel Gormane944fd62015-02-12 14:58:35 -0800112 }
113
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -0800114 oldpte = ptep_modify_prot_start(vma, addr, pte);
115 ptent = pte_modify(oldpte, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -0700116 if (preserve_write)
Aneesh Kumar K.V288bc542017-02-24 14:59:16 -0800117 ptent = pte_mk_savedwrite(ptent);
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200118
Mel Gorman8a0516e2015-02-12 14:58:22 -0800119 /* Avoid taking write faults for known dirty pages */
120 if (dirty_accountable && pte_dirty(ptent) &&
121 (pte_soft_dirty(ptent) ||
122 !(vma->vm_flags & VM_SOFTDIRTY))) {
123 ptent = pte_mkwrite(ptent);
Mel Gorman4b10e7d2012-10-25 14:16:32 +0200124 }
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -0800125 ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
Mel Gorman8a0516e2015-02-12 14:58:22 -0800126 pages++;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800127 } else if (IS_ENABLED(CONFIG_MIGRATION)) {
Christoph Lameter06972122006-06-23 02:03:35 -0700128 swp_entry_t entry = pte_to_swp_entry(oldpte);
129
130 if (is_write_migration_entry(entry)) {
Cyrill Gorcunovc3d16e12013-10-16 13:46:51 -0700131 pte_t newpte;
Christoph Lameter06972122006-06-23 02:03:35 -0700132 /*
133 * A protection check is difficult so
134 * just be safe and disable write
135 */
136 make_migration_entry_read(&entry);
Cyrill Gorcunovc3d16e12013-10-16 13:46:51 -0700137 newpte = swp_entry_to_pte(entry);
138 if (pte_swp_soft_dirty(oldpte))
139 newpte = pte_swp_mksoft_dirty(newpte);
Mike Rapoport94393c72019-05-13 17:23:14 -0700140 set_pte_at(vma->vm_mm, addr, pte, newpte);
Mel Gormane920e142013-10-07 11:28:48 +0100141
142 pages++;
Christoph Lameter06972122006-06-23 02:03:35 -0700143 }
Jérôme Glisse5042db42017-09-08 16:11:43 -0700144
145 if (is_write_device_private_entry(entry)) {
146 pte_t newpte;
147
148 /*
149 * We do not preserve soft-dirtiness. See
150 * copy_one_pte() for explanation.
151 */
152 make_device_private_entry_read(&entry);
153 newpte = swp_entry_to_pte(entry);
Mike Rapoport94393c72019-05-13 17:23:14 -0700154 set_pte_at(vma->vm_mm, addr, pte, newpte);
Jérôme Glisse5042db42017-09-08 16:11:43 -0700155
156 pages++;
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159 } while (pte++, addr += PAGE_SIZE, addr != end);
Zachary Amsden6606c3e2006-09-30 23:29:33 -0700160 arch_leave_lazy_mmu_mode();
Hugh Dickins705e87c2005-10-29 18:16:27 -0700161 pte_unmap_unlock(pte - 1, ptl);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100162
163 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Mel Gorman8b272b32020-03-05 22:28:26 -0800166/*
167 * Used when setting automatic NUMA hinting protection where it is
168 * critical that a numa hinting PMD is not confused with a bad PMD.
169 */
170static inline int pmd_none_or_clear_bad_unless_trans_huge(pmd_t *pmd)
171{
172 pmd_t pmdval = pmd_read_atomic(pmd);
173
174 /* See pmd_none_or_trans_huge_or_clear_bad for info on barrier */
175#ifdef CONFIG_TRANSPARENT_HUGEPAGE
176 barrier();
177#endif
178
179 if (pmd_none(pmdval))
180 return 1;
181 if (pmd_trans_huge(pmdval))
182 return 0;
183 if (unlikely(pmd_bad(pmdval))) {
184 pmd_clear_bad(pmd);
185 return 1;
186 }
187
188 return 0;
189}
190
Andrew Morton7d12efa2012-12-18 14:23:17 -0800191static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
192 pud_t *pud, unsigned long addr, unsigned long end,
Peter Xu58705442020-04-06 20:05:45 -0700193 pgprot_t newprot, unsigned long cp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 pmd_t *pmd;
196 unsigned long next;
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100197 unsigned long pages = 0;
Mel Gorman72403b42013-11-12 15:08:32 -0800198 unsigned long nr_huge_updates = 0;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800199 struct mmu_notifier_range range;
200
201 range.start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 pmd = pmd_offset(pud, addr);
204 do {
Mel Gorman25cbbef2013-10-07 11:29:14 +0100205 unsigned long this_pages;
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 next = pmd_addr_end(addr, end);
Mel Gorman8b272b32020-03-05 22:28:26 -0800208
209 /*
210 * Automatic NUMA balancing walks the tables with mmap_sem
211 * held for read. It's possible a parallel update to occur
212 * between pmd_trans_huge() and a pmd_none_or_clear_bad()
213 * check leading to a false positive and clearing.
214 * Hence, it's necessary to atomically read the PMD value
215 * for all the checks.
216 */
217 if (!is_swap_pmd(*pmd) && !pmd_devmap(*pmd) &&
218 pmd_none_or_clear_bad_unless_trans_huge(pmd))
Anshuman Khandual4991c092018-01-04 16:17:52 -0800219 goto next;
Rik van Riela5338092014-04-07 15:36:57 -0700220
221 /* invoke the mmu notifier if the pmd is populated */
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800222 if (!range.start) {
Jérôme Glisse7269f992019-05-13 17:20:53 -0700223 mmu_notifier_range_init(&range,
224 MMU_NOTIFY_PROTECTION_VMA, 0,
225 vma, vma->vm_mm, addr, end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800226 mmu_notifier_invalidate_range_start(&range);
Rik van Riela5338092014-04-07 15:36:57 -0700227 }
228
Zi Yan84c3fc42017-09-08 16:11:01 -0700229 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
Kirill A. Shutemov6b9116a2016-02-11 16:13:03 -0800230 if (next - addr != HPAGE_PMD_SIZE) {
David Rientjesfd607752016-12-12 16:42:20 -0800231 __split_huge_pmd(vma, pmd, addr, false, NULL);
Kirill A. Shutemov6b9116a2016-02-11 16:13:03 -0800232 } else {
Mel Gormanf123d742013-10-07 11:28:49 +0100233 int nr_ptes = change_huge_pmd(vma, pmd, addr,
Peter Xu58705442020-04-06 20:05:45 -0700234 newprot, cp_flags);
Mel Gormanf123d742013-10-07 11:28:49 +0100235
236 if (nr_ptes) {
Mel Gorman72403b42013-11-12 15:08:32 -0800237 if (nr_ptes == HPAGE_PMD_NR) {
238 pages += HPAGE_PMD_NR;
239 nr_huge_updates++;
240 }
Mel Gorman1ad9f622014-04-07 15:36:56 -0700241
242 /* huge pmd was handled */
Anshuman Khandual4991c092018-01-04 16:17:52 -0800243 goto next;
Mel Gormanf123d742013-10-07 11:28:49 +0100244 }
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100245 }
Rik van Riel88a9ab62014-04-07 15:36:55 -0700246 /* fall through, the trans huge pmd just split */
Johannes Weinercd7548a2011-01-13 15:47:04 -0800247 }
Mel Gorman25cbbef2013-10-07 11:29:14 +0100248 this_pages = change_pte_range(vma, pmd, addr, next, newprot,
Peter Xu58705442020-04-06 20:05:45 -0700249 cp_flags);
Mel Gorman25cbbef2013-10-07 11:29:14 +0100250 pages += this_pages;
Anshuman Khandual4991c092018-01-04 16:17:52 -0800251next:
252 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 } while (pmd++, addr = next, addr != end);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100254
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800255 if (range.start)
256 mmu_notifier_invalidate_range_end(&range);
Rik van Riela5338092014-04-07 15:36:57 -0700257
Mel Gorman72403b42013-11-12 15:08:32 -0800258 if (nr_huge_updates)
259 count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100260 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Andrew Morton7d12efa2012-12-18 14:23:17 -0800263static inline unsigned long change_pud_range(struct vm_area_struct *vma,
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300264 p4d_t *p4d, unsigned long addr, unsigned long end,
Peter Xu58705442020-04-06 20:05:45 -0700265 pgprot_t newprot, unsigned long cp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 pud_t *pud;
268 unsigned long next;
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100269 unsigned long pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300271 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 do {
273 next = pud_addr_end(addr, end);
274 if (pud_none_or_clear_bad(pud))
275 continue;
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100276 pages += change_pmd_range(vma, pud, addr, next, newprot,
Peter Xu58705442020-04-06 20:05:45 -0700277 cp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 } while (pud++, addr = next, addr != end);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100279
280 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300283static inline unsigned long change_p4d_range(struct vm_area_struct *vma,
284 pgd_t *pgd, unsigned long addr, unsigned long end,
Peter Xu58705442020-04-06 20:05:45 -0700285 pgprot_t newprot, unsigned long cp_flags)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300286{
287 p4d_t *p4d;
288 unsigned long next;
289 unsigned long pages = 0;
290
291 p4d = p4d_offset(pgd, addr);
292 do {
293 next = p4d_addr_end(addr, end);
294 if (p4d_none_or_clear_bad(p4d))
295 continue;
296 pages += change_pud_range(vma, p4d, addr, next, newprot,
Peter Xu58705442020-04-06 20:05:45 -0700297 cp_flags);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300298 } while (p4d++, addr = next, addr != end);
299
300 return pages;
301}
302
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100303static unsigned long change_protection_range(struct vm_area_struct *vma,
Peter Zijlstrac1e60982006-09-25 23:30:59 -0700304 unsigned long addr, unsigned long end, pgprot_t newprot,
Peter Xu58705442020-04-06 20:05:45 -0700305 unsigned long cp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 struct mm_struct *mm = vma->vm_mm;
308 pgd_t *pgd;
309 unsigned long next;
310 unsigned long start = addr;
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100311 unsigned long pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 BUG_ON(addr >= end);
314 pgd = pgd_offset(mm, addr);
315 flush_cache_range(vma, addr, end);
Nadav Amit16af97d2017-08-10 15:23:56 -0700316 inc_tlb_flush_pending(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 do {
318 next = pgd_addr_end(addr, end);
319 if (pgd_none_or_clear_bad(pgd))
320 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300321 pages += change_p4d_range(vma, pgd, addr, next, newprot,
Peter Xu58705442020-04-06 20:05:45 -0700322 cp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 } while (pgd++, addr = next, addr != end);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100324
Ingo Molnar1233d582012-11-19 03:14:24 +0100325 /* Only flush the TLB if we actually modified any entries: */
326 if (pages)
327 flush_tlb_range(vma, start, end);
Nadav Amit16af97d2017-08-10 15:23:56 -0700328 dec_tlb_flush_pending(mm);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100329
330 return pages;
331}
332
333unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
334 unsigned long end, pgprot_t newprot,
Peter Xu58705442020-04-06 20:05:45 -0700335 unsigned long cp_flags)
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100336{
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100337 unsigned long pages;
338
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100339 if (is_vm_hugetlb_page(vma))
340 pages = hugetlb_change_protection(vma, start, end, newprot);
341 else
Peter Xu58705442020-04-06 20:05:45 -0700342 pages = change_protection_range(vma, start, end, newprot,
343 cp_flags);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100344
345 return pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Andi Kleen42e40892018-06-13 15:48:27 -0700348static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
349 unsigned long next, struct mm_walk *walk)
350{
351 return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
352 0 : -EACCES;
353}
354
355static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
356 unsigned long addr, unsigned long next,
357 struct mm_walk *walk)
358{
359 return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
360 0 : -EACCES;
361}
362
363static int prot_none_test(unsigned long addr, unsigned long next,
364 struct mm_walk *walk)
365{
366 return 0;
367}
368
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200369static const struct mm_walk_ops prot_none_walk_ops = {
370 .pte_entry = prot_none_pte_entry,
371 .hugetlb_entry = prot_none_hugetlb_entry,
372 .test_walk = prot_none_test,
373};
Andi Kleen42e40892018-06-13 15:48:27 -0700374
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700375int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
377 unsigned long start, unsigned long end, unsigned long newflags)
378{
379 struct mm_struct *mm = vma->vm_mm;
380 unsigned long oldflags = vma->vm_flags;
381 long nrpages = (end - start) >> PAGE_SHIFT;
382 unsigned long charged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 pgoff_t pgoff;
384 int error;
Peter Zijlstrac1e60982006-09-25 23:30:59 -0700385 int dirty_accountable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (newflags == oldflags) {
388 *pprev = vma;
389 return 0;
390 }
391
392 /*
Andi Kleen42e40892018-06-13 15:48:27 -0700393 * Do PROT_NONE PFN permission checks here when we can still
394 * bail out without undoing a lot of state. This is a rather
395 * uncommon case, so doesn't need to be very optimized.
396 */
397 if (arch_has_pfn_modify_check() &&
398 (vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
399 (newflags & (VM_READ|VM_WRITE|VM_EXEC)) == 0) {
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200400 pgprot_t new_pgprot = vm_get_page_prot(newflags);
401
402 error = walk_page_range(current->mm, start, end,
403 &prot_none_walk_ops, &new_pgprot);
Andi Kleen42e40892018-06-13 15:48:27 -0700404 if (error)
405 return error;
406 }
407
408 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * If we make a private mapping writable we increase our commit;
410 * but (without finer accounting) cannot reduce our commit if we
Mel Gorman5a6fe122009-02-10 14:02:27 +0000411 * make it unwritable again. hugetlb mapping were accounted for
412 * even if read-only so there is no need to account for them here
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
414 if (newflags & VM_WRITE) {
Konstantin Khlebnikov84638332016-01-14 15:22:07 -0800415 /* Check space limits when area turns into data. */
416 if (!may_expand_vm(mm, newflags, nrpages) &&
417 may_expand_vm(mm, oldflags, nrpages))
418 return -ENOMEM;
Mel Gorman5a6fe122009-02-10 14:02:27 +0000419 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
Andy Whitcroftcdfd4322008-07-23 21:27:28 -0700420 VM_SHARED|VM_NORESERVE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 charged = nrpages;
Al Viro191c5422012-02-13 03:58:52 +0000422 if (security_vm_enough_memory_mm(mm, charged))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return -ENOMEM;
424 newflags |= VM_ACCOUNT;
425 }
426 }
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /*
429 * First try to merge with previous and/or next vma.
430 */
431 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
432 *pprev = vma_merge(mm, *pprev, start, end, newflags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700433 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
434 vma->vm_userfaultfd_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (*pprev) {
436 vma = *pprev;
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700437 VM_WARN_ON((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 goto success;
439 }
440
441 *pprev = vma;
442
443 if (start != vma->vm_start) {
444 error = split_vma(mm, vma, start, 1);
445 if (error)
446 goto fail;
447 }
448
449 if (end != vma->vm_end) {
450 error = split_vma(mm, vma, end, 0);
451 if (error)
452 goto fail;
453 }
454
455success:
456 /*
457 * vm_flags and vm_page_prot are protected by the mmap_sem
458 * held in write mode.
459 */
460 vma->vm_flags = newflags;
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700461 dirty_accountable = vma_wants_writenotify(vma, vma->vm_page_prot);
Peter Feiner64e455072014-10-13 15:55:46 -0700462 vma_set_page_prot(vma);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700463
Andrew Morton7d12efa2012-12-18 14:23:17 -0800464 change_protection(vma, start, end, vma->vm_page_prot,
Peter Xu58705442020-04-06 20:05:45 -0700465 dirty_accountable ? MM_CP_DIRTY_ACCT : 0);
Peter Zijlstra7da4d642012-11-19 03:14:23 +0100466
Kirill A. Shutemov36f88182015-06-24 16:56:10 -0700467 /*
468 * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
469 * fault on access.
470 */
471 if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
472 (newflags & VM_WRITE)) {
473 populate_vma_page_range(vma, start, end, NULL);
474 }
475
Konstantin Khlebnikov84638332016-01-14 15:22:07 -0800476 vm_stat_account(mm, oldflags, -nrpages);
477 vm_stat_account(mm, newflags, nrpages);
Pekka Enberg63bfd732010-11-08 21:29:07 +0200478 perf_event_mmap(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480
481fail:
482 vm_unacct_memory(charged);
483 return error;
484}
485
Dave Hansen7d06d9c2016-07-29 09:30:12 -0700486/*
487 * pkey==-1 when doing a legacy mprotect()
488 */
489static int do_mprotect_pkey(unsigned long start, size_t len,
490 unsigned long prot, int pkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Dave Hansen62b5f7d2016-02-12 13:02:40 -0800492 unsigned long nstart, end, tmp, reqprot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct vm_area_struct *vma, *prev;
494 int error = -EINVAL;
495 const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
Piotr Kwapulinskif1385562016-03-22 14:27:51 -0700496 const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
497 (prot & PROT_READ);
498
Andrey Konovalov057d33892019-09-25 16:48:30 -0700499 start = untagged_addr(start);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
502 if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
503 return -EINVAL;
504
505 if (start & ~PAGE_MASK)
506 return -EINVAL;
507 if (!len)
508 return 0;
509 len = PAGE_ALIGN(len);
510 end = start + len;
511 if (end <= start)
512 return -ENOMEM;
Khalid Aziz9035cf92018-02-21 10:15:49 -0700513 if (!arch_validate_prot(prot, start))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return -EINVAL;
515
516 reqprot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Michal Hockodc0ef0d2016-05-23 16:25:27 -0700518 if (down_write_killable(&current->mm->mmap_sem))
519 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Dave Hansene8c24d32016-07-29 09:30:15 -0700521 /*
522 * If userspace did not allocate the pkey, do not let
523 * them use it here.
524 */
525 error = -EINVAL;
526 if ((pkey != -1) && !mm_pkey_is_allocated(current->mm, pkey))
527 goto out;
528
Linus Torvalds097d5912012-03-06 18:23:36 -0800529 vma = find_vma(current->mm, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 error = -ENOMEM;
531 if (!vma)
532 goto out;
Linus Torvalds097d5912012-03-06 18:23:36 -0800533 prev = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (unlikely(grows & PROT_GROWSDOWN)) {
535 if (vma->vm_start >= end)
536 goto out;
537 start = vma->vm_start;
538 error = -EINVAL;
539 if (!(vma->vm_flags & VM_GROWSDOWN))
540 goto out;
Andrew Morton7d12efa2012-12-18 14:23:17 -0800541 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (vma->vm_start > start)
543 goto out;
544 if (unlikely(grows & PROT_GROWSUP)) {
545 end = vma->vm_end;
546 error = -EINVAL;
547 if (!(vma->vm_flags & VM_GROWSUP))
548 goto out;
549 }
550 }
551 if (start > vma->vm_start)
552 prev = vma;
553
554 for (nstart = start ; ; ) {
Dave Hansena8502b62016-07-29 09:30:13 -0700555 unsigned long mask_off_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 unsigned long newflags;
Dave Hansen7d06d9c2016-07-29 09:30:12 -0700557 int new_vma_pkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Andrew Morton7d12efa2012-12-18 14:23:17 -0800559 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Piotr Kwapulinskif1385562016-03-22 14:27:51 -0700561 /* Does the application expect PROT_READ to imply PROT_EXEC */
562 if (rier && (vma->vm_flags & VM_MAYEXEC))
563 prot |= PROT_EXEC;
564
Dave Hansena8502b62016-07-29 09:30:13 -0700565 /*
566 * Each mprotect() call explicitly passes r/w/x permissions.
567 * If a permission is not passed to mprotect(), it must be
568 * cleared from the VMA.
569 */
570 mask_off_old_flags = VM_READ | VM_WRITE | VM_EXEC |
Khalid Aziz2c2d57b52018-02-21 10:15:50 -0700571 VM_FLAGS_CLEAR;
Dave Hansena8502b62016-07-29 09:30:13 -0700572
Dave Hansen7d06d9c2016-07-29 09:30:12 -0700573 new_vma_pkey = arch_override_mprotect_pkey(vma, prot, pkey);
574 newflags = calc_vm_prot_bits(prot, new_vma_pkey);
Dave Hansena8502b62016-07-29 09:30:13 -0700575 newflags |= (vma->vm_flags & ~mask_off_old_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Paolo 'Blaisorblade' Giarrusso7e2cff42005-09-21 09:55:39 -0700577 /* newflags >> 4 shift VM_MAY% in place of VM_% */
578 if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 error = -EACCES;
580 goto out;
581 }
582
583 error = security_file_mprotect(vma, reqprot, prot);
584 if (error)
585 goto out;
586
587 tmp = vma->vm_end;
588 if (tmp > end)
589 tmp = end;
590 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
591 if (error)
592 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 nstart = tmp;
594
595 if (nstart < prev->vm_end)
596 nstart = prev->vm_end;
597 if (nstart >= end)
598 goto out;
599
600 vma = prev->vm_next;
601 if (!vma || vma->vm_start != nstart) {
602 error = -ENOMEM;
603 goto out;
604 }
Piotr Kwapulinskif1385562016-03-22 14:27:51 -0700605 prot = reqprot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607out:
608 up_write(&current->mm->mmap_sem);
609 return error;
610}
Dave Hansen7d06d9c2016-07-29 09:30:12 -0700611
612SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
613 unsigned long, prot)
614{
615 return do_mprotect_pkey(start, len, prot, -1);
616}
617
Heiko Carstensc7142ae2016-12-12 16:43:09 -0800618#ifdef CONFIG_ARCH_HAS_PKEYS
619
Dave Hansen7d06d9c2016-07-29 09:30:12 -0700620SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
621 unsigned long, prot, int, pkey)
622{
623 return do_mprotect_pkey(start, len, prot, pkey);
624}
Dave Hansene8c24d32016-07-29 09:30:15 -0700625
626SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)
627{
628 int pkey;
629 int ret;
630
631 /* No flags supported yet. */
632 if (flags)
633 return -EINVAL;
634 /* check for unsupported init values */
635 if (init_val & ~PKEY_ACCESS_MASK)
636 return -EINVAL;
637
638 down_write(&current->mm->mmap_sem);
639 pkey = mm_pkey_alloc(current->mm);
640
641 ret = -ENOSPC;
642 if (pkey == -1)
643 goto out;
644
645 ret = arch_set_user_pkey_access(current, pkey, init_val);
646 if (ret) {
647 mm_pkey_free(current->mm, pkey);
648 goto out;
649 }
650 ret = pkey;
651out:
652 up_write(&current->mm->mmap_sem);
653 return ret;
654}
655
656SYSCALL_DEFINE1(pkey_free, int, pkey)
657{
658 int ret;
659
660 down_write(&current->mm->mmap_sem);
661 ret = mm_pkey_free(current->mm, pkey);
662 up_write(&current->mm->mmap_sem);
663
664 /*
665 * We could provie warnings or errors if any VMA still
666 * has the pkey set here.
667 */
668 return ret;
669}
Heiko Carstensc7142ae2016-12-12 16:43:09 -0800670
671#endif /* CONFIG_ARCH_HAS_PKEYS */