xref: /openbmc/linux/mm/mprotect.c (revision 4dd845b5a3e57ad07f26ef808707b064696fe34b)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  mm/mprotect.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  (C) Copyright 1994 Linus Torvalds
61da177e4SLinus Torvalds  *  (C) Copyright 2002 Christoph Hellwig
71da177e4SLinus Torvalds  *
8046c6884SAlan Cox  *  Address space accounting code	<alan@lxorguk.ukuu.org.uk>
91da177e4SLinus Torvalds  *  (C) Copyright 2002 Red Hat Inc, All Rights Reserved
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
12a520110eSChristoph Hellwig #include <linux/pagewalk.h>
131da177e4SLinus Torvalds #include <linux/hugetlb.h>
141da177e4SLinus Torvalds #include <linux/shm.h>
151da177e4SLinus Torvalds #include <linux/mman.h>
161da177e4SLinus Torvalds #include <linux/fs.h>
171da177e4SLinus Torvalds #include <linux/highmem.h>
181da177e4SLinus Torvalds #include <linux/security.h>
191da177e4SLinus Torvalds #include <linux/mempolicy.h>
201da177e4SLinus Torvalds #include <linux/personality.h>
211da177e4SLinus Torvalds #include <linux/syscalls.h>
220697212aSChristoph Lameter #include <linux/swap.h>
230697212aSChristoph Lameter #include <linux/swapops.h>
24cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
2564cdd548SKOSAKI Motohiro #include <linux/migrate.h>
26cdd6c482SIngo Molnar #include <linux/perf_event.h>
27e8c24d3aSDave Hansen #include <linux/pkeys.h>
2864a9a34eSMel Gorman #include <linux/ksm.h>
297c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
3009a913a7SMel Gorman #include <linux/mm_inline.h>
31ca5999fdSMike Rapoport #include <linux/pgtable.h>
321da177e4SLinus Torvalds #include <asm/cacheflush.h>
33e8c24d3aSDave Hansen #include <asm/mmu_context.h>
341da177e4SLinus Torvalds #include <asm/tlbflush.h>
351da177e4SLinus Torvalds 
3636f88188SKirill A. Shutemov #include "internal.h"
3736f88188SKirill A. Shutemov 
384b10e7d5SMel Gorman static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
39c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
4058705444SPeter Xu 		unsigned long cp_flags)
411da177e4SLinus Torvalds {
420697212aSChristoph Lameter 	pte_t *pte, oldpte;
43705e87c0SHugh Dickins 	spinlock_t *ptl;
447da4d641SPeter Zijlstra 	unsigned long pages = 0;
453e321587SAndi Kleen 	int target_node = NUMA_NO_NODE;
4658705444SPeter Xu 	bool dirty_accountable = cp_flags & MM_CP_DIRTY_ACCT;
4758705444SPeter Xu 	bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
48292924b2SPeter Xu 	bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
49292924b2SPeter Xu 	bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
501da177e4SLinus Torvalds 
51175ad4f1SAndrea Arcangeli 	/*
52c1e8d7c6SMichel Lespinasse 	 * Can be called with only the mmap_lock for reading by
53175ad4f1SAndrea Arcangeli 	 * prot_numa so we must check the pmd isn't constantly
54175ad4f1SAndrea Arcangeli 	 * changing from under us from pmd_none to pmd_trans_huge
55175ad4f1SAndrea Arcangeli 	 * and/or the other way around.
56175ad4f1SAndrea Arcangeli 	 */
57175ad4f1SAndrea Arcangeli 	if (pmd_trans_unstable(pmd))
58175ad4f1SAndrea Arcangeli 		return 0;
59175ad4f1SAndrea Arcangeli 
60175ad4f1SAndrea Arcangeli 	/*
61175ad4f1SAndrea Arcangeli 	 * The pmd points to a regular pte so the pmd can't change
62c1e8d7c6SMichel Lespinasse 	 * from under us even if the mmap_lock is only hold for
63175ad4f1SAndrea Arcangeli 	 * reading.
64175ad4f1SAndrea Arcangeli 	 */
65175ad4f1SAndrea Arcangeli 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
661ad9f620SMel Gorman 
673e321587SAndi Kleen 	/* Get target node for single threaded private VMAs */
683e321587SAndi Kleen 	if (prot_numa && !(vma->vm_flags & VM_SHARED) &&
693e321587SAndi Kleen 	    atomic_read(&vma->vm_mm->mm_users) == 1)
703e321587SAndi Kleen 		target_node = numa_node_id();
713e321587SAndi Kleen 
723ea27719SMel Gorman 	flush_tlb_batched_pending(vma->vm_mm);
736606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
741da177e4SLinus Torvalds 	do {
750697212aSChristoph Lameter 		oldpte = *pte;
760697212aSChristoph Lameter 		if (pte_present(oldpte)) {
771da177e4SLinus Torvalds 			pte_t ptent;
78b191f9b1SMel Gorman 			bool preserve_write = prot_numa && pte_write(oldpte);
791da177e4SLinus Torvalds 
80e944fd67SMel Gorman 			/*
81e944fd67SMel Gorman 			 * Avoid trapping faults against the zero or KSM
82e944fd67SMel Gorman 			 * pages. See similar comment in change_huge_pmd.
83e944fd67SMel Gorman 			 */
84e944fd67SMel Gorman 			if (prot_numa) {
85e944fd67SMel Gorman 				struct page *page;
86e944fd67SMel Gorman 
87a818f536SHuang Ying 				/* Avoid TLB flush if possible */
88a818f536SHuang Ying 				if (pte_protnone(oldpte))
89a818f536SHuang Ying 					continue;
90a818f536SHuang Ying 
91e944fd67SMel Gorman 				page = vm_normal_page(vma, addr, oldpte);
92e944fd67SMel Gorman 				if (!page || PageKsm(page))
93e944fd67SMel Gorman 					continue;
9410c1045fSMel Gorman 
95859d4adcSHenry Willard 				/* Also skip shared copy-on-write pages */
96859d4adcSHenry Willard 				if (is_cow_mapping(vma->vm_flags) &&
97859d4adcSHenry Willard 				    page_mapcount(page) != 1)
98859d4adcSHenry Willard 					continue;
99859d4adcSHenry Willard 
10009a913a7SMel Gorman 				/*
10109a913a7SMel Gorman 				 * While migration can move some dirty pages,
10209a913a7SMel Gorman 				 * it cannot move them all from MIGRATE_ASYNC
10309a913a7SMel Gorman 				 * context.
10409a913a7SMel Gorman 				 */
1059de4f22aSHuang Ying 				if (page_is_file_lru(page) && PageDirty(page))
10609a913a7SMel Gorman 					continue;
10709a913a7SMel Gorman 
1083e321587SAndi Kleen 				/*
1093e321587SAndi Kleen 				 * Don't mess with PTEs if page is already on the node
1103e321587SAndi Kleen 				 * a single-threaded process is running on.
1113e321587SAndi Kleen 				 */
1123e321587SAndi Kleen 				if (target_node == page_to_nid(page))
1133e321587SAndi Kleen 					continue;
114e944fd67SMel Gorman 			}
115e944fd67SMel Gorman 
11604a86453SAneesh Kumar K.V 			oldpte = ptep_modify_prot_start(vma, addr, pte);
11704a86453SAneesh Kumar K.V 			ptent = pte_modify(oldpte, newprot);
118b191f9b1SMel Gorman 			if (preserve_write)
119288bc549SAneesh Kumar K.V 				ptent = pte_mk_savedwrite(ptent);
1208a0516edSMel Gorman 
121292924b2SPeter Xu 			if (uffd_wp) {
122292924b2SPeter Xu 				ptent = pte_wrprotect(ptent);
123292924b2SPeter Xu 				ptent = pte_mkuffd_wp(ptent);
124292924b2SPeter Xu 			} else if (uffd_wp_resolve) {
125292924b2SPeter Xu 				/*
126292924b2SPeter Xu 				 * Leave the write bit to be handled
127292924b2SPeter Xu 				 * by PF interrupt handler, then
128292924b2SPeter Xu 				 * things like COW could be properly
129292924b2SPeter Xu 				 * handled.
130292924b2SPeter Xu 				 */
131292924b2SPeter Xu 				ptent = pte_clear_uffd_wp(ptent);
132292924b2SPeter Xu 			}
133292924b2SPeter Xu 
1348a0516edSMel Gorman 			/* Avoid taking write faults for known dirty pages */
13564e45507SPeter Feiner 			if (dirty_accountable && pte_dirty(ptent) &&
13664e45507SPeter Feiner 					(pte_soft_dirty(ptent) ||
1378a0516edSMel Gorman 					 !(vma->vm_flags & VM_SOFTDIRTY))) {
1389d85d586SAneesh Kumar K.V 				ptent = pte_mkwrite(ptent);
1398a0516edSMel Gorman 			}
14004a86453SAneesh Kumar K.V 			ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
1414b10e7d5SMel Gorman 			pages++;
142f45ec5ffSPeter Xu 		} else if (is_swap_pte(oldpte)) {
1430697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(oldpte);
144f45ec5ffSPeter Xu 			pte_t newpte;
1450697212aSChristoph Lameter 
146*4dd845b5SAlistair Popple 			if (is_writable_migration_entry(entry)) {
1470697212aSChristoph Lameter 				/*
1480697212aSChristoph Lameter 				 * A protection check is difficult so
1490697212aSChristoph Lameter 				 * just be safe and disable write
1500697212aSChristoph Lameter 				 */
151*4dd845b5SAlistair Popple 				entry = make_readable_migration_entry(
152*4dd845b5SAlistair Popple 							swp_offset(entry));
153c3d16e16SCyrill Gorcunov 				newpte = swp_entry_to_pte(entry);
154c3d16e16SCyrill Gorcunov 				if (pte_swp_soft_dirty(oldpte))
155c3d16e16SCyrill Gorcunov 					newpte = pte_swp_mksoft_dirty(newpte);
156f45ec5ffSPeter Xu 				if (pte_swp_uffd_wp(oldpte))
157f45ec5ffSPeter Xu 					newpte = pte_swp_mkuffd_wp(newpte);
158*4dd845b5SAlistair Popple 			} else if (is_writable_device_private_entry(entry)) {
1595042db43SJérôme Glisse 				/*
1605042db43SJérôme Glisse 				 * We do not preserve soft-dirtiness. See
1615042db43SJérôme Glisse 				 * copy_one_pte() for explanation.
1625042db43SJérôme Glisse 				 */
163*4dd845b5SAlistair Popple 				entry = make_readable_device_private_entry(
164*4dd845b5SAlistair Popple 							swp_offset(entry));
1655042db43SJérôme Glisse 				newpte = swp_entry_to_pte(entry);
166f45ec5ffSPeter Xu 				if (pte_swp_uffd_wp(oldpte))
167f45ec5ffSPeter Xu 					newpte = pte_swp_mkuffd_wp(newpte);
168f45ec5ffSPeter Xu 			} else {
169f45ec5ffSPeter Xu 				newpte = oldpte;
170f45ec5ffSPeter Xu 			}
1715042db43SJérôme Glisse 
172f45ec5ffSPeter Xu 			if (uffd_wp)
173f45ec5ffSPeter Xu 				newpte = pte_swp_mkuffd_wp(newpte);
174f45ec5ffSPeter Xu 			else if (uffd_wp_resolve)
175f45ec5ffSPeter Xu 				newpte = pte_swp_clear_uffd_wp(newpte);
176f45ec5ffSPeter Xu 
177f45ec5ffSPeter Xu 			if (!pte_same(oldpte, newpte)) {
178f45ec5ffSPeter Xu 				set_pte_at(vma->vm_mm, addr, pte, newpte);
1795042db43SJérôme Glisse 				pages++;
1805042db43SJérôme Glisse 			}
181e920e14cSMel Gorman 		}
1821da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1836606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
184705e87c0SHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
1857da4d641SPeter Zijlstra 
1867da4d641SPeter Zijlstra 	return pages;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1898b272b3cSMel Gorman /*
1908b272b3cSMel Gorman  * Used when setting automatic NUMA hinting protection where it is
1918b272b3cSMel Gorman  * critical that a numa hinting PMD is not confused with a bad PMD.
1928b272b3cSMel Gorman  */
1938b272b3cSMel Gorman static inline int pmd_none_or_clear_bad_unless_trans_huge(pmd_t *pmd)
1948b272b3cSMel Gorman {
1958b272b3cSMel Gorman 	pmd_t pmdval = pmd_read_atomic(pmd);
1968b272b3cSMel Gorman 
1978b272b3cSMel Gorman 	/* See pmd_none_or_trans_huge_or_clear_bad for info on barrier */
1988b272b3cSMel Gorman #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1998b272b3cSMel Gorman 	barrier();
2008b272b3cSMel Gorman #endif
2018b272b3cSMel Gorman 
2028b272b3cSMel Gorman 	if (pmd_none(pmdval))
2038b272b3cSMel Gorman 		return 1;
2048b272b3cSMel Gorman 	if (pmd_trans_huge(pmdval))
2058b272b3cSMel Gorman 		return 0;
2068b272b3cSMel Gorman 	if (unlikely(pmd_bad(pmdval))) {
2078b272b3cSMel Gorman 		pmd_clear_bad(pmd);
2088b272b3cSMel Gorman 		return 1;
2098b272b3cSMel Gorman 	}
2108b272b3cSMel Gorman 
2118b272b3cSMel Gorman 	return 0;
2128b272b3cSMel Gorman }
2138b272b3cSMel Gorman 
2147d12efaeSAndrew Morton static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
2157d12efaeSAndrew Morton 		pud_t *pud, unsigned long addr, unsigned long end,
21658705444SPeter Xu 		pgprot_t newprot, unsigned long cp_flags)
2171da177e4SLinus Torvalds {
2181da177e4SLinus Torvalds 	pmd_t *pmd;
2191da177e4SLinus Torvalds 	unsigned long next;
2207da4d641SPeter Zijlstra 	unsigned long pages = 0;
22172403b4aSMel Gorman 	unsigned long nr_huge_updates = 0;
222ac46d4f3SJérôme Glisse 	struct mmu_notifier_range range;
223ac46d4f3SJérôme Glisse 
224ac46d4f3SJérôme Glisse 	range.start = 0;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
2271da177e4SLinus Torvalds 	do {
22825cbbef1SMel Gorman 		unsigned long this_pages;
22925cbbef1SMel Gorman 
2301da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
2318b272b3cSMel Gorman 
2328b272b3cSMel Gorman 		/*
233c1e8d7c6SMichel Lespinasse 		 * Automatic NUMA balancing walks the tables with mmap_lock
2348b272b3cSMel Gorman 		 * held for read. It's possible a parallel update to occur
2358b272b3cSMel Gorman 		 * between pmd_trans_huge() and a pmd_none_or_clear_bad()
2368b272b3cSMel Gorman 		 * check leading to a false positive and clearing.
2378b272b3cSMel Gorman 		 * Hence, it's necessary to atomically read the PMD value
2388b272b3cSMel Gorman 		 * for all the checks.
2398b272b3cSMel Gorman 		 */
2408b272b3cSMel Gorman 		if (!is_swap_pmd(*pmd) && !pmd_devmap(*pmd) &&
2418b272b3cSMel Gorman 		     pmd_none_or_clear_bad_unless_trans_huge(pmd))
2424991c09cSAnshuman Khandual 			goto next;
243a5338093SRik van Riel 
244a5338093SRik van Riel 		/* invoke the mmu notifier if the pmd is populated */
245ac46d4f3SJérôme Glisse 		if (!range.start) {
2467269f999SJérôme Glisse 			mmu_notifier_range_init(&range,
2477269f999SJérôme Glisse 				MMU_NOTIFY_PROTECTION_VMA, 0,
2486f4f13e8SJérôme Glisse 				vma, vma->vm_mm, addr, end);
249ac46d4f3SJérôme Glisse 			mmu_notifier_invalidate_range_start(&range);
250a5338093SRik van Riel 		}
251a5338093SRik van Riel 
25284c3fc4eSZi Yan 		if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
2536b9116a6SKirill A. Shutemov 			if (next - addr != HPAGE_PMD_SIZE) {
254fd60775aSDavid Rientjes 				__split_huge_pmd(vma, pmd, addr, false, NULL);
2556b9116a6SKirill A. Shutemov 			} else {
256f123d74aSMel Gorman 				int nr_ptes = change_huge_pmd(vma, pmd, addr,
25758705444SPeter Xu 							      newprot, cp_flags);
258f123d74aSMel Gorman 
259f123d74aSMel Gorman 				if (nr_ptes) {
26072403b4aSMel Gorman 					if (nr_ptes == HPAGE_PMD_NR) {
26172403b4aSMel Gorman 						pages += HPAGE_PMD_NR;
26272403b4aSMel Gorman 						nr_huge_updates++;
26372403b4aSMel Gorman 					}
2641ad9f620SMel Gorman 
2651ad9f620SMel Gorman 					/* huge pmd was handled */
2664991c09cSAnshuman Khandual 					goto next;
2677da4d641SPeter Zijlstra 				}
268f123d74aSMel Gorman 			}
26988a9ab6eSRik van Riel 			/* fall through, the trans huge pmd just split */
270cd7548abSJohannes Weiner 		}
27125cbbef1SMel Gorman 		this_pages = change_pte_range(vma, pmd, addr, next, newprot,
27258705444SPeter Xu 					      cp_flags);
27325cbbef1SMel Gorman 		pages += this_pages;
2744991c09cSAnshuman Khandual next:
2754991c09cSAnshuman Khandual 		cond_resched();
2761da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
2777da4d641SPeter Zijlstra 
278ac46d4f3SJérôme Glisse 	if (range.start)
279ac46d4f3SJérôme Glisse 		mmu_notifier_invalidate_range_end(&range);
280a5338093SRik van Riel 
28172403b4aSMel Gorman 	if (nr_huge_updates)
28272403b4aSMel Gorman 		count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
2837da4d641SPeter Zijlstra 	return pages;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2867d12efaeSAndrew Morton static inline unsigned long change_pud_range(struct vm_area_struct *vma,
287c2febafcSKirill A. Shutemov 		p4d_t *p4d, unsigned long addr, unsigned long end,
28858705444SPeter Xu 		pgprot_t newprot, unsigned long cp_flags)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	pud_t *pud;
2911da177e4SLinus Torvalds 	unsigned long next;
2927da4d641SPeter Zijlstra 	unsigned long pages = 0;
2931da177e4SLinus Torvalds 
294c2febafcSKirill A. Shutemov 	pud = pud_offset(p4d, addr);
2951da177e4SLinus Torvalds 	do {
2961da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
2971da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
2981da177e4SLinus Torvalds 			continue;
2997da4d641SPeter Zijlstra 		pages += change_pmd_range(vma, pud, addr, next, newprot,
30058705444SPeter Xu 					  cp_flags);
3011da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
3027da4d641SPeter Zijlstra 
3037da4d641SPeter Zijlstra 	return pages;
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
306c2febafcSKirill A. Shutemov static inline unsigned long change_p4d_range(struct vm_area_struct *vma,
307c2febafcSKirill A. Shutemov 		pgd_t *pgd, unsigned long addr, unsigned long end,
30858705444SPeter Xu 		pgprot_t newprot, unsigned long cp_flags)
309c2febafcSKirill A. Shutemov {
310c2febafcSKirill A. Shutemov 	p4d_t *p4d;
311c2febafcSKirill A. Shutemov 	unsigned long next;
312c2febafcSKirill A. Shutemov 	unsigned long pages = 0;
313c2febafcSKirill A. Shutemov 
314c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
315c2febafcSKirill A. Shutemov 	do {
316c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
317c2febafcSKirill A. Shutemov 		if (p4d_none_or_clear_bad(p4d))
318c2febafcSKirill A. Shutemov 			continue;
319c2febafcSKirill A. Shutemov 		pages += change_pud_range(vma, p4d, addr, next, newprot,
32058705444SPeter Xu 					  cp_flags);
321c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
322c2febafcSKirill A. Shutemov 
323c2febafcSKirill A. Shutemov 	return pages;
324c2febafcSKirill A. Shutemov }
325c2febafcSKirill A. Shutemov 
3267da4d641SPeter Zijlstra static unsigned long change_protection_range(struct vm_area_struct *vma,
327c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
32858705444SPeter Xu 		unsigned long cp_flags)
3291da177e4SLinus Torvalds {
3301da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
3311da177e4SLinus Torvalds 	pgd_t *pgd;
3321da177e4SLinus Torvalds 	unsigned long next;
3331da177e4SLinus Torvalds 	unsigned long start = addr;
3347da4d641SPeter Zijlstra 	unsigned long pages = 0;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	BUG_ON(addr >= end);
3371da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
3381da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
33916af97dcSNadav Amit 	inc_tlb_flush_pending(mm);
3401da177e4SLinus Torvalds 	do {
3411da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
3421da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
3431da177e4SLinus Torvalds 			continue;
344c2febafcSKirill A. Shutemov 		pages += change_p4d_range(vma, pgd, addr, next, newprot,
34558705444SPeter Xu 					  cp_flags);
3461da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
3477da4d641SPeter Zijlstra 
3481233d588SIngo Molnar 	/* Only flush the TLB if we actually modified any entries: */
3491233d588SIngo Molnar 	if (pages)
3501da177e4SLinus Torvalds 		flush_tlb_range(vma, start, end);
35116af97dcSNadav Amit 	dec_tlb_flush_pending(mm);
3527da4d641SPeter Zijlstra 
3537da4d641SPeter Zijlstra 	return pages;
3547da4d641SPeter Zijlstra }
3557da4d641SPeter Zijlstra 
3567da4d641SPeter Zijlstra unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
3577da4d641SPeter Zijlstra 		       unsigned long end, pgprot_t newprot,
35858705444SPeter Xu 		       unsigned long cp_flags)
3597da4d641SPeter Zijlstra {
3607da4d641SPeter Zijlstra 	unsigned long pages;
3617da4d641SPeter Zijlstra 
362292924b2SPeter Xu 	BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
363292924b2SPeter Xu 
3647da4d641SPeter Zijlstra 	if (is_vm_hugetlb_page(vma))
3657da4d641SPeter Zijlstra 		pages = hugetlb_change_protection(vma, start, end, newprot);
3667da4d641SPeter Zijlstra 	else
36758705444SPeter Xu 		pages = change_protection_range(vma, start, end, newprot,
36858705444SPeter Xu 						cp_flags);
3697da4d641SPeter Zijlstra 
3707da4d641SPeter Zijlstra 	return pages;
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
37342e4089cSAndi Kleen static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
37442e4089cSAndi Kleen 			       unsigned long next, struct mm_walk *walk)
37542e4089cSAndi Kleen {
37642e4089cSAndi Kleen 	return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
37742e4089cSAndi Kleen 		0 : -EACCES;
37842e4089cSAndi Kleen }
37942e4089cSAndi Kleen 
38042e4089cSAndi Kleen static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
38142e4089cSAndi Kleen 				   unsigned long addr, unsigned long next,
38242e4089cSAndi Kleen 				   struct mm_walk *walk)
38342e4089cSAndi Kleen {
38442e4089cSAndi Kleen 	return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
38542e4089cSAndi Kleen 		0 : -EACCES;
38642e4089cSAndi Kleen }
38742e4089cSAndi Kleen 
38842e4089cSAndi Kleen static int prot_none_test(unsigned long addr, unsigned long next,
38942e4089cSAndi Kleen 			  struct mm_walk *walk)
39042e4089cSAndi Kleen {
39142e4089cSAndi Kleen 	return 0;
39242e4089cSAndi Kleen }
39342e4089cSAndi Kleen 
3947b86ac33SChristoph Hellwig static const struct mm_walk_ops prot_none_walk_ops = {
39542e4089cSAndi Kleen 	.pte_entry		= prot_none_pte_entry,
39642e4089cSAndi Kleen 	.hugetlb_entry		= prot_none_hugetlb_entry,
39742e4089cSAndi Kleen 	.test_walk		= prot_none_test,
39842e4089cSAndi Kleen };
39942e4089cSAndi Kleen 
400b6a2fea3SOllie Wild int
4011da177e4SLinus Torvalds mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
4021da177e4SLinus Torvalds 	unsigned long start, unsigned long end, unsigned long newflags)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
4051da177e4SLinus Torvalds 	unsigned long oldflags = vma->vm_flags;
4061da177e4SLinus Torvalds 	long nrpages = (end - start) >> PAGE_SHIFT;
4071da177e4SLinus Torvalds 	unsigned long charged = 0;
4081da177e4SLinus Torvalds 	pgoff_t pgoff;
4091da177e4SLinus Torvalds 	int error;
410c1e6098bSPeter Zijlstra 	int dirty_accountable = 0;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	if (newflags == oldflags) {
4131da177e4SLinus Torvalds 		*pprev = vma;
4141da177e4SLinus Torvalds 		return 0;
4151da177e4SLinus Torvalds 	}
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	/*
41842e4089cSAndi Kleen 	 * Do PROT_NONE PFN permission checks here when we can still
41942e4089cSAndi Kleen 	 * bail out without undoing a lot of state. This is a rather
42042e4089cSAndi Kleen 	 * uncommon case, so doesn't need to be very optimized.
42142e4089cSAndi Kleen 	 */
42242e4089cSAndi Kleen 	if (arch_has_pfn_modify_check() &&
42342e4089cSAndi Kleen 	    (vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
4246cb4d9a2SAnshuman Khandual 	    (newflags & VM_ACCESS_FLAGS) == 0) {
4257b86ac33SChristoph Hellwig 		pgprot_t new_pgprot = vm_get_page_prot(newflags);
4267b86ac33SChristoph Hellwig 
4277b86ac33SChristoph Hellwig 		error = walk_page_range(current->mm, start, end,
4287b86ac33SChristoph Hellwig 				&prot_none_walk_ops, &new_pgprot);
42942e4089cSAndi Kleen 		if (error)
43042e4089cSAndi Kleen 			return error;
43142e4089cSAndi Kleen 	}
43242e4089cSAndi Kleen 
43342e4089cSAndi Kleen 	/*
4341da177e4SLinus Torvalds 	 * If we make a private mapping writable we increase our commit;
4351da177e4SLinus Torvalds 	 * but (without finer accounting) cannot reduce our commit if we
4365a6fe125SMel Gorman 	 * make it unwritable again. hugetlb mapping were accounted for
4375a6fe125SMel Gorman 	 * even if read-only so there is no need to account for them here
4381da177e4SLinus Torvalds 	 */
4391da177e4SLinus Torvalds 	if (newflags & VM_WRITE) {
44084638335SKonstantin Khlebnikov 		/* Check space limits when area turns into data. */
44184638335SKonstantin Khlebnikov 		if (!may_expand_vm(mm, newflags, nrpages) &&
44284638335SKonstantin Khlebnikov 				may_expand_vm(mm, oldflags, nrpages))
44384638335SKonstantin Khlebnikov 			return -ENOMEM;
4445a6fe125SMel Gorman 		if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
445cdfd4325SAndy Whitcroft 						VM_SHARED|VM_NORESERVE))) {
4461da177e4SLinus Torvalds 			charged = nrpages;
447191c5424SAl Viro 			if (security_vm_enough_memory_mm(mm, charged))
4481da177e4SLinus Torvalds 				return -ENOMEM;
4491da177e4SLinus Torvalds 			newflags |= VM_ACCOUNT;
4501da177e4SLinus Torvalds 		}
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	/*
4541da177e4SLinus Torvalds 	 * First try to merge with previous and/or next vma.
4551da177e4SLinus Torvalds 	 */
4561da177e4SLinus Torvalds 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
4571da177e4SLinus Torvalds 	*pprev = vma_merge(mm, *pprev, start, end, newflags,
45819a809afSAndrea Arcangeli 			   vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
45919a809afSAndrea Arcangeli 			   vma->vm_userfaultfd_ctx);
4601da177e4SLinus Torvalds 	if (*pprev) {
4611da177e4SLinus Torvalds 		vma = *pprev;
462e86f15eeSAndrea Arcangeli 		VM_WARN_ON((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY);
4631da177e4SLinus Torvalds 		goto success;
4641da177e4SLinus Torvalds 	}
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	*pprev = vma;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	if (start != vma->vm_start) {
4691da177e4SLinus Torvalds 		error = split_vma(mm, vma, start, 1);
4701da177e4SLinus Torvalds 		if (error)
4711da177e4SLinus Torvalds 			goto fail;
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	if (end != vma->vm_end) {
4751da177e4SLinus Torvalds 		error = split_vma(mm, vma, end, 0);
4761da177e4SLinus Torvalds 		if (error)
4771da177e4SLinus Torvalds 			goto fail;
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds success:
4811da177e4SLinus Torvalds 	/*
482c1e8d7c6SMichel Lespinasse 	 * vm_flags and vm_page_prot are protected by the mmap_lock
4831da177e4SLinus Torvalds 	 * held in write mode.
4841da177e4SLinus Torvalds 	 */
4851da177e4SLinus Torvalds 	vma->vm_flags = newflags;
4866d2329f8SAndrea Arcangeli 	dirty_accountable = vma_wants_writenotify(vma, vma->vm_page_prot);
48764e45507SPeter Feiner 	vma_set_page_prot(vma);
488d08b3851SPeter Zijlstra 
4897d12efaeSAndrew Morton 	change_protection(vma, start, end, vma->vm_page_prot,
49058705444SPeter Xu 			  dirty_accountable ? MM_CP_DIRTY_ACCT : 0);
4917da4d641SPeter Zijlstra 
49236f88188SKirill A. Shutemov 	/*
49336f88188SKirill A. Shutemov 	 * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
49436f88188SKirill A. Shutemov 	 * fault on access.
49536f88188SKirill A. Shutemov 	 */
49636f88188SKirill A. Shutemov 	if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
49736f88188SKirill A. Shutemov 			(newflags & VM_WRITE)) {
49836f88188SKirill A. Shutemov 		populate_vma_page_range(vma, start, end, NULL);
49936f88188SKirill A. Shutemov 	}
50036f88188SKirill A. Shutemov 
50184638335SKonstantin Khlebnikov 	vm_stat_account(mm, oldflags, -nrpages);
50284638335SKonstantin Khlebnikov 	vm_stat_account(mm, newflags, nrpages);
50363bfd738SPekka Enberg 	perf_event_mmap(vma);
5041da177e4SLinus Torvalds 	return 0;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds fail:
5071da177e4SLinus Torvalds 	vm_unacct_memory(charged);
5081da177e4SLinus Torvalds 	return error;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
5117d06d9c9SDave Hansen /*
5127d06d9c9SDave Hansen  * pkey==-1 when doing a legacy mprotect()
5137d06d9c9SDave Hansen  */
5147d06d9c9SDave Hansen static int do_mprotect_pkey(unsigned long start, size_t len,
5157d06d9c9SDave Hansen 		unsigned long prot, int pkey)
5161da177e4SLinus Torvalds {
51762b5f7d0SDave Hansen 	unsigned long nstart, end, tmp, reqprot;
5181da177e4SLinus Torvalds 	struct vm_area_struct *vma, *prev;
5191da177e4SLinus Torvalds 	int error = -EINVAL;
5201da177e4SLinus Torvalds 	const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
521f138556dSPiotr Kwapulinski 	const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
522f138556dSPiotr Kwapulinski 				(prot & PROT_READ);
523f138556dSPiotr Kwapulinski 
524057d3389SAndrey Konovalov 	start = untagged_addr(start);
525057d3389SAndrey Konovalov 
5261da177e4SLinus Torvalds 	prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
5271da177e4SLinus Torvalds 	if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
5281da177e4SLinus Torvalds 		return -EINVAL;
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	if (start & ~PAGE_MASK)
5311da177e4SLinus Torvalds 		return -EINVAL;
5321da177e4SLinus Torvalds 	if (!len)
5331da177e4SLinus Torvalds 		return 0;
5341da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
5351da177e4SLinus Torvalds 	end = start + len;
5361da177e4SLinus Torvalds 	if (end <= start)
5371da177e4SLinus Torvalds 		return -ENOMEM;
5389035cf9aSKhalid Aziz 	if (!arch_validate_prot(prot, start))
5391da177e4SLinus Torvalds 		return -EINVAL;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	reqprot = prot;
5421da177e4SLinus Torvalds 
543d8ed45c5SMichel Lespinasse 	if (mmap_write_lock_killable(current->mm))
544dc0ef0dfSMichal Hocko 		return -EINTR;
5451da177e4SLinus Torvalds 
546e8c24d3aSDave Hansen 	/*
547e8c24d3aSDave Hansen 	 * If userspace did not allocate the pkey, do not let
548e8c24d3aSDave Hansen 	 * them use it here.
549e8c24d3aSDave Hansen 	 */
550e8c24d3aSDave Hansen 	error = -EINVAL;
551e8c24d3aSDave Hansen 	if ((pkey != -1) && !mm_pkey_is_allocated(current->mm, pkey))
552e8c24d3aSDave Hansen 		goto out;
553e8c24d3aSDave Hansen 
554097d5910SLinus Torvalds 	vma = find_vma(current->mm, start);
5551da177e4SLinus Torvalds 	error = -ENOMEM;
5561da177e4SLinus Torvalds 	if (!vma)
5571da177e4SLinus Torvalds 		goto out;
558097d5910SLinus Torvalds 	prev = vma->vm_prev;
5591da177e4SLinus Torvalds 	if (unlikely(grows & PROT_GROWSDOWN)) {
5601da177e4SLinus Torvalds 		if (vma->vm_start >= end)
5611da177e4SLinus Torvalds 			goto out;
5621da177e4SLinus Torvalds 		start = vma->vm_start;
5631da177e4SLinus Torvalds 		error = -EINVAL;
5641da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_GROWSDOWN))
5651da177e4SLinus Torvalds 			goto out;
5667d12efaeSAndrew Morton 	} else {
5671da177e4SLinus Torvalds 		if (vma->vm_start > start)
5681da177e4SLinus Torvalds 			goto out;
5691da177e4SLinus Torvalds 		if (unlikely(grows & PROT_GROWSUP)) {
5701da177e4SLinus Torvalds 			end = vma->vm_end;
5711da177e4SLinus Torvalds 			error = -EINVAL;
5721da177e4SLinus Torvalds 			if (!(vma->vm_flags & VM_GROWSUP))
5731da177e4SLinus Torvalds 				goto out;
5741da177e4SLinus Torvalds 		}
5751da177e4SLinus Torvalds 	}
5761da177e4SLinus Torvalds 	if (start > vma->vm_start)
5771da177e4SLinus Torvalds 		prev = vma;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	for (nstart = start ; ; ) {
580a8502b67SDave Hansen 		unsigned long mask_off_old_flags;
5811da177e4SLinus Torvalds 		unsigned long newflags;
5827d06d9c9SDave Hansen 		int new_vma_pkey;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 		/* Here we know that vma->vm_start <= nstart < vma->vm_end. */
5851da177e4SLinus Torvalds 
586f138556dSPiotr Kwapulinski 		/* Does the application expect PROT_READ to imply PROT_EXEC */
587f138556dSPiotr Kwapulinski 		if (rier && (vma->vm_flags & VM_MAYEXEC))
588f138556dSPiotr Kwapulinski 			prot |= PROT_EXEC;
589f138556dSPiotr Kwapulinski 
590a8502b67SDave Hansen 		/*
591a8502b67SDave Hansen 		 * Each mprotect() call explicitly passes r/w/x permissions.
592a8502b67SDave Hansen 		 * If a permission is not passed to mprotect(), it must be
593a8502b67SDave Hansen 		 * cleared from the VMA.
594a8502b67SDave Hansen 		 */
595a8502b67SDave Hansen 		mask_off_old_flags = VM_READ | VM_WRITE | VM_EXEC |
5962c2d57b5SKhalid Aziz 					VM_FLAGS_CLEAR;
597a8502b67SDave Hansen 
5987d06d9c9SDave Hansen 		new_vma_pkey = arch_override_mprotect_pkey(vma, prot, pkey);
5997d06d9c9SDave Hansen 		newflags = calc_vm_prot_bits(prot, new_vma_pkey);
600a8502b67SDave Hansen 		newflags |= (vma->vm_flags & ~mask_off_old_flags);
6011da177e4SLinus Torvalds 
6027e2cff42SPaolo 'Blaisorblade' Giarrusso 		/* newflags >> 4 shift VM_MAY% in place of VM_% */
6036cb4d9a2SAnshuman Khandual 		if ((newflags & ~(newflags >> 4)) & VM_ACCESS_FLAGS) {
6041da177e4SLinus Torvalds 			error = -EACCES;
6051da177e4SLinus Torvalds 			goto out;
6061da177e4SLinus Torvalds 		}
6071da177e4SLinus Torvalds 
608c462ac28SCatalin Marinas 		/* Allow architectures to sanity-check the new flags */
609c462ac28SCatalin Marinas 		if (!arch_validate_flags(newflags)) {
610c462ac28SCatalin Marinas 			error = -EINVAL;
611c462ac28SCatalin Marinas 			goto out;
612c462ac28SCatalin Marinas 		}
613c462ac28SCatalin Marinas 
6141da177e4SLinus Torvalds 		error = security_file_mprotect(vma, reqprot, prot);
6151da177e4SLinus Torvalds 		if (error)
6161da177e4SLinus Torvalds 			goto out;
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 		tmp = vma->vm_end;
6191da177e4SLinus Torvalds 		if (tmp > end)
6201da177e4SLinus Torvalds 			tmp = end;
62195bb7c42SSean Christopherson 
622dbf53f75STianjia Zhang 		if (vma->vm_ops && vma->vm_ops->mprotect) {
62395bb7c42SSean Christopherson 			error = vma->vm_ops->mprotect(vma, nstart, tmp, newflags);
62495bb7c42SSean Christopherson 			if (error)
62595bb7c42SSean Christopherson 				goto out;
626dbf53f75STianjia Zhang 		}
62795bb7c42SSean Christopherson 
6281da177e4SLinus Torvalds 		error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
6291da177e4SLinus Torvalds 		if (error)
6301da177e4SLinus Torvalds 			goto out;
63195bb7c42SSean Christopherson 
6321da177e4SLinus Torvalds 		nstart = tmp;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 		if (nstart < prev->vm_end)
6351da177e4SLinus Torvalds 			nstart = prev->vm_end;
6361da177e4SLinus Torvalds 		if (nstart >= end)
6371da177e4SLinus Torvalds 			goto out;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 		vma = prev->vm_next;
6401da177e4SLinus Torvalds 		if (!vma || vma->vm_start != nstart) {
6411da177e4SLinus Torvalds 			error = -ENOMEM;
6421da177e4SLinus Torvalds 			goto out;
6431da177e4SLinus Torvalds 		}
644f138556dSPiotr Kwapulinski 		prot = reqprot;
6451da177e4SLinus Torvalds 	}
6461da177e4SLinus Torvalds out:
647d8ed45c5SMichel Lespinasse 	mmap_write_unlock(current->mm);
6481da177e4SLinus Torvalds 	return error;
6491da177e4SLinus Torvalds }
6507d06d9c9SDave Hansen 
6517d06d9c9SDave Hansen SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
6527d06d9c9SDave Hansen 		unsigned long, prot)
6537d06d9c9SDave Hansen {
6547d06d9c9SDave Hansen 	return do_mprotect_pkey(start, len, prot, -1);
6557d06d9c9SDave Hansen }
6567d06d9c9SDave Hansen 
657c7142aeaSHeiko Carstens #ifdef CONFIG_ARCH_HAS_PKEYS
658c7142aeaSHeiko Carstens 
6597d06d9c9SDave Hansen SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
6607d06d9c9SDave Hansen 		unsigned long, prot, int, pkey)
6617d06d9c9SDave Hansen {
6627d06d9c9SDave Hansen 	return do_mprotect_pkey(start, len, prot, pkey);
6637d06d9c9SDave Hansen }
664e8c24d3aSDave Hansen 
665e8c24d3aSDave Hansen SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)
666e8c24d3aSDave Hansen {
667e8c24d3aSDave Hansen 	int pkey;
668e8c24d3aSDave Hansen 	int ret;
669e8c24d3aSDave Hansen 
670e8c24d3aSDave Hansen 	/* No flags supported yet. */
671e8c24d3aSDave Hansen 	if (flags)
672e8c24d3aSDave Hansen 		return -EINVAL;
673e8c24d3aSDave Hansen 	/* check for unsupported init values */
674e8c24d3aSDave Hansen 	if (init_val & ~PKEY_ACCESS_MASK)
675e8c24d3aSDave Hansen 		return -EINVAL;
676e8c24d3aSDave Hansen 
677d8ed45c5SMichel Lespinasse 	mmap_write_lock(current->mm);
678e8c24d3aSDave Hansen 	pkey = mm_pkey_alloc(current->mm);
679e8c24d3aSDave Hansen 
680e8c24d3aSDave Hansen 	ret = -ENOSPC;
681e8c24d3aSDave Hansen 	if (pkey == -1)
682e8c24d3aSDave Hansen 		goto out;
683e8c24d3aSDave Hansen 
684e8c24d3aSDave Hansen 	ret = arch_set_user_pkey_access(current, pkey, init_val);
685e8c24d3aSDave Hansen 	if (ret) {
686e8c24d3aSDave Hansen 		mm_pkey_free(current->mm, pkey);
687e8c24d3aSDave Hansen 		goto out;
688e8c24d3aSDave Hansen 	}
689e8c24d3aSDave Hansen 	ret = pkey;
690e8c24d3aSDave Hansen out:
691d8ed45c5SMichel Lespinasse 	mmap_write_unlock(current->mm);
692e8c24d3aSDave Hansen 	return ret;
693e8c24d3aSDave Hansen }
694e8c24d3aSDave Hansen 
695e8c24d3aSDave Hansen SYSCALL_DEFINE1(pkey_free, int, pkey)
696e8c24d3aSDave Hansen {
697e8c24d3aSDave Hansen 	int ret;
698e8c24d3aSDave Hansen 
699d8ed45c5SMichel Lespinasse 	mmap_write_lock(current->mm);
700e8c24d3aSDave Hansen 	ret = mm_pkey_free(current->mm, pkey);
701d8ed45c5SMichel Lespinasse 	mmap_write_unlock(current->mm);
702e8c24d3aSDave Hansen 
703e8c24d3aSDave Hansen 	/*
704f0953a1bSIngo Molnar 	 * We could provide warnings or errors if any VMA still
705e8c24d3aSDave Hansen 	 * has the pkey set here.
706e8c24d3aSDave Hansen 	 */
707e8c24d3aSDave Hansen 	return ret;
708e8c24d3aSDave Hansen }
709c7142aeaSHeiko Carstens 
710c7142aeaSHeiko Carstens #endif /* CONFIG_ARCH_HAS_PKEYS */
711