xref: /openbmc/linux/mm/mprotect.c (revision c3d16e16522fe3fe8759735850a0676da18f4b1d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  mm/mprotect.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  (C) Copyright 1994 Linus Torvalds
51da177e4SLinus Torvalds  *  (C) Copyright 2002 Christoph Hellwig
61da177e4SLinus Torvalds  *
7046c6884SAlan Cox  *  Address space accounting code	<alan@lxorguk.ukuu.org.uk>
81da177e4SLinus Torvalds  *  (C) Copyright 2002 Red Hat Inc, All Rights Reserved
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/hugetlb.h>
131da177e4SLinus Torvalds #include <linux/shm.h>
141da177e4SLinus Torvalds #include <linux/mman.h>
151da177e4SLinus Torvalds #include <linux/fs.h>
161da177e4SLinus Torvalds #include <linux/highmem.h>
171da177e4SLinus Torvalds #include <linux/security.h>
181da177e4SLinus Torvalds #include <linux/mempolicy.h>
191da177e4SLinus Torvalds #include <linux/personality.h>
201da177e4SLinus Torvalds #include <linux/syscalls.h>
210697212aSChristoph Lameter #include <linux/swap.h>
220697212aSChristoph Lameter #include <linux/swapops.h>
23cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
2464cdd548SKOSAKI Motohiro #include <linux/migrate.h>
25cdd6c482SIngo Molnar #include <linux/perf_event.h>
261da177e4SLinus Torvalds #include <asm/uaccess.h>
271da177e4SLinus Torvalds #include <asm/pgtable.h>
281da177e4SLinus Torvalds #include <asm/cacheflush.h>
291da177e4SLinus Torvalds #include <asm/tlbflush.h>
301da177e4SLinus Torvalds 
311c12c4cfSVenki Pallipadi #ifndef pgprot_modify
321c12c4cfSVenki Pallipadi static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
331c12c4cfSVenki Pallipadi {
341c12c4cfSVenki Pallipadi 	return newprot;
351c12c4cfSVenki Pallipadi }
361c12c4cfSVenki Pallipadi #endif
371c12c4cfSVenki Pallipadi 
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,
409532fec1SMel Gorman 		int dirty_accountable, int prot_numa, bool *ret_all_same_node)
411da177e4SLinus Torvalds {
424b10e7d5SMel Gorman 	struct mm_struct *mm = vma->vm_mm;
430697212aSChristoph Lameter 	pte_t *pte, oldpte;
44705e87c0SHugh Dickins 	spinlock_t *ptl;
457da4d641SPeter Zijlstra 	unsigned long pages = 0;
469532fec1SMel Gorman 	bool all_same_node = true;
479532fec1SMel Gorman 	int last_nid = -1;
481da177e4SLinus Torvalds 
49705e87c0SHugh Dickins 	pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
506606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
511da177e4SLinus Torvalds 	do {
520697212aSChristoph Lameter 		oldpte = *pte;
530697212aSChristoph Lameter 		if (pte_present(oldpte)) {
541da177e4SLinus Torvalds 			pte_t ptent;
554b10e7d5SMel Gorman 			bool updated = false;
561da177e4SLinus Torvalds 
571ea0704eSJeremy Fitzhardinge 			ptent = ptep_modify_prot_start(mm, addr, pte);
584b10e7d5SMel Gorman 			if (!prot_numa) {
59c1e6098bSPeter Zijlstra 				ptent = pte_modify(ptent, newprot);
604b10e7d5SMel Gorman 				updated = true;
614b10e7d5SMel Gorman 			} else {
624b10e7d5SMel Gorman 				struct page *page;
634b10e7d5SMel Gorman 
644b10e7d5SMel Gorman 				page = vm_normal_page(vma, addr, oldpte);
654b10e7d5SMel Gorman 				if (page) {
669532fec1SMel Gorman 					int this_nid = page_to_nid(page);
679532fec1SMel Gorman 					if (last_nid == -1)
689532fec1SMel Gorman 						last_nid = this_nid;
699532fec1SMel Gorman 					if (last_nid != this_nid)
709532fec1SMel Gorman 						all_same_node = false;
719532fec1SMel Gorman 
724b10e7d5SMel Gorman 					/* only check non-shared pages */
734b10e7d5SMel Gorman 					if (!pte_numa(oldpte) &&
744b10e7d5SMel Gorman 					    page_mapcount(page) == 1) {
754b10e7d5SMel Gorman 						ptent = pte_mknuma(ptent);
764b10e7d5SMel Gorman 						updated = true;
774b10e7d5SMel Gorman 					}
784b10e7d5SMel Gorman 				}
794b10e7d5SMel Gorman 			}
801ea0704eSJeremy Fitzhardinge 
81c1e6098bSPeter Zijlstra 			/*
82c1e6098bSPeter Zijlstra 			 * Avoid taking write faults for pages we know to be
83c1e6098bSPeter Zijlstra 			 * dirty.
84c1e6098bSPeter Zijlstra 			 */
854b10e7d5SMel Gorman 			if (dirty_accountable && pte_dirty(ptent)) {
86c1e6098bSPeter Zijlstra 				ptent = pte_mkwrite(ptent);
874b10e7d5SMel Gorman 				updated = true;
884b10e7d5SMel Gorman 			}
891ea0704eSJeremy Fitzhardinge 
904b10e7d5SMel Gorman 			if (updated)
914b10e7d5SMel Gorman 				pages++;
921ea0704eSJeremy Fitzhardinge 			ptep_modify_prot_commit(mm, addr, pte, ptent);
93ce1744f4SKonstantin Khlebnikov 		} else if (IS_ENABLED(CONFIG_MIGRATION) && !pte_file(oldpte)) {
940697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(oldpte);
950697212aSChristoph Lameter 
960697212aSChristoph Lameter 			if (is_write_migration_entry(entry)) {
97*c3d16e16SCyrill Gorcunov 				pte_t newpte;
980697212aSChristoph Lameter 				/*
990697212aSChristoph Lameter 				 * A protection check is difficult so
1000697212aSChristoph Lameter 				 * just be safe and disable write
1010697212aSChristoph Lameter 				 */
1020697212aSChristoph Lameter 				make_migration_entry_read(&entry);
103*c3d16e16SCyrill Gorcunov 				newpte = swp_entry_to_pte(entry);
104*c3d16e16SCyrill Gorcunov 				if (pte_swp_soft_dirty(oldpte))
105*c3d16e16SCyrill Gorcunov 					newpte = pte_swp_mksoft_dirty(newpte);
106*c3d16e16SCyrill Gorcunov 				set_pte_at(mm, addr, pte, newpte);
1071da177e4SLinus Torvalds 			}
1087da4d641SPeter Zijlstra 			pages++;
1090697212aSChristoph Lameter 		}
1101da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1116606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
112705e87c0SHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
1137da4d641SPeter Zijlstra 
1149532fec1SMel Gorman 	*ret_all_same_node = all_same_node;
1157da4d641SPeter Zijlstra 	return pages;
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
1184b10e7d5SMel Gorman #ifdef CONFIG_NUMA_BALANCING
1194b10e7d5SMel Gorman static inline void change_pmd_protnuma(struct mm_struct *mm, unsigned long addr,
1204b10e7d5SMel Gorman 				       pmd_t *pmd)
1214b10e7d5SMel Gorman {
1224b10e7d5SMel Gorman 	spin_lock(&mm->page_table_lock);
1234b10e7d5SMel Gorman 	set_pmd_at(mm, addr & PMD_MASK, pmd, pmd_mknuma(*pmd));
1244b10e7d5SMel Gorman 	spin_unlock(&mm->page_table_lock);
1254b10e7d5SMel Gorman }
1264b10e7d5SMel Gorman #else
1274b10e7d5SMel Gorman static inline void change_pmd_protnuma(struct mm_struct *mm, unsigned long addr,
1284b10e7d5SMel Gorman 				       pmd_t *pmd)
1294b10e7d5SMel Gorman {
1304b10e7d5SMel Gorman 	BUG();
1314b10e7d5SMel Gorman }
1324b10e7d5SMel Gorman #endif /* CONFIG_NUMA_BALANCING */
1334b10e7d5SMel Gorman 
1347d12efaeSAndrew Morton static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
1357d12efaeSAndrew Morton 		pud_t *pud, unsigned long addr, unsigned long end,
1367d12efaeSAndrew Morton 		pgprot_t newprot, int dirty_accountable, int prot_numa)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	pmd_t *pmd;
1391da177e4SLinus Torvalds 	unsigned long next;
1407da4d641SPeter Zijlstra 	unsigned long pages = 0;
1419532fec1SMel Gorman 	bool all_same_node;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
1441da177e4SLinus Torvalds 	do {
1451da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
146cd7548abSJohannes Weiner 		if (pmd_trans_huge(*pmd)) {
147cd7548abSJohannes Weiner 			if (next - addr != HPAGE_PMD_SIZE)
148e180377fSKirill A. Shutemov 				split_huge_page_pmd(vma, addr, pmd);
1497d12efaeSAndrew Morton 			else if (change_huge_pmd(vma, pmd, addr, newprot,
1507d12efaeSAndrew Morton 						 prot_numa)) {
1517da4d641SPeter Zijlstra 				pages += HPAGE_PMD_NR;
152cd7548abSJohannes Weiner 				continue;
1537da4d641SPeter Zijlstra 			}
154cd7548abSJohannes Weiner 			/* fall through */
155cd7548abSJohannes Weiner 		}
1561da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
1571da177e4SLinus Torvalds 			continue;
1584b10e7d5SMel Gorman 		pages += change_pte_range(vma, pmd, addr, next, newprot,
1599532fec1SMel Gorman 				 dirty_accountable, prot_numa, &all_same_node);
1604b10e7d5SMel Gorman 
1619532fec1SMel Gorman 		/*
1629532fec1SMel Gorman 		 * If we are changing protections for NUMA hinting faults then
1639532fec1SMel Gorman 		 * set pmd_numa if the examined pages were all on the same
1649532fec1SMel Gorman 		 * node. This allows a regular PMD to be handled as one fault
1659532fec1SMel Gorman 		 * and effectively batches the taking of the PTL
1669532fec1SMel Gorman 		 */
1679532fec1SMel Gorman 		if (prot_numa && all_same_node)
1684b10e7d5SMel Gorman 			change_pmd_protnuma(vma->vm_mm, addr, pmd);
1691da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
1707da4d641SPeter Zijlstra 
1717da4d641SPeter Zijlstra 	return pages;
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1747d12efaeSAndrew Morton static inline unsigned long change_pud_range(struct vm_area_struct *vma,
1757d12efaeSAndrew Morton 		pgd_t *pgd, unsigned long addr, unsigned long end,
1767d12efaeSAndrew Morton 		pgprot_t newprot, int dirty_accountable, int prot_numa)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	pud_t *pud;
1791da177e4SLinus Torvalds 	unsigned long next;
1807da4d641SPeter Zijlstra 	unsigned long pages = 0;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
1831da177e4SLinus Torvalds 	do {
1841da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
1851da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
1861da177e4SLinus Torvalds 			continue;
1877da4d641SPeter Zijlstra 		pages += change_pmd_range(vma, pud, addr, next, newprot,
1884b10e7d5SMel Gorman 				 dirty_accountable, prot_numa);
1891da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
1907da4d641SPeter Zijlstra 
1917da4d641SPeter Zijlstra 	return pages;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1947da4d641SPeter Zijlstra static unsigned long change_protection_range(struct vm_area_struct *vma,
195c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
1964b10e7d5SMel Gorman 		int dirty_accountable, int prot_numa)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
1991da177e4SLinus Torvalds 	pgd_t *pgd;
2001da177e4SLinus Torvalds 	unsigned long next;
2011da177e4SLinus Torvalds 	unsigned long start = addr;
2027da4d641SPeter Zijlstra 	unsigned long pages = 0;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	BUG_ON(addr >= end);
2051da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
2061da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
2071da177e4SLinus Torvalds 	do {
2081da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
2091da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
2101da177e4SLinus Torvalds 			continue;
2117da4d641SPeter Zijlstra 		pages += change_pud_range(vma, pgd, addr, next, newprot,
2124b10e7d5SMel Gorman 				 dirty_accountable, prot_numa);
2131da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
2147da4d641SPeter Zijlstra 
2151233d588SIngo Molnar 	/* Only flush the TLB if we actually modified any entries: */
2161233d588SIngo Molnar 	if (pages)
2171da177e4SLinus Torvalds 		flush_tlb_range(vma, start, end);
2187da4d641SPeter Zijlstra 
2197da4d641SPeter Zijlstra 	return pages;
2207da4d641SPeter Zijlstra }
2217da4d641SPeter Zijlstra 
2227da4d641SPeter Zijlstra unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
2237da4d641SPeter Zijlstra 		       unsigned long end, pgprot_t newprot,
2244b10e7d5SMel Gorman 		       int dirty_accountable, int prot_numa)
2257da4d641SPeter Zijlstra {
2267da4d641SPeter Zijlstra 	struct mm_struct *mm = vma->vm_mm;
2277da4d641SPeter Zijlstra 	unsigned long pages;
2287da4d641SPeter Zijlstra 
2297da4d641SPeter Zijlstra 	mmu_notifier_invalidate_range_start(mm, start, end);
2307da4d641SPeter Zijlstra 	if (is_vm_hugetlb_page(vma))
2317da4d641SPeter Zijlstra 		pages = hugetlb_change_protection(vma, start, end, newprot);
2327da4d641SPeter Zijlstra 	else
2334b10e7d5SMel Gorman 		pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
2347da4d641SPeter Zijlstra 	mmu_notifier_invalidate_range_end(mm, start, end);
2357da4d641SPeter Zijlstra 
2367da4d641SPeter Zijlstra 	return pages;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
239b6a2fea3SOllie Wild int
2401da177e4SLinus Torvalds mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
2411da177e4SLinus Torvalds 	unsigned long start, unsigned long end, unsigned long newflags)
2421da177e4SLinus Torvalds {
2431da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
2441da177e4SLinus Torvalds 	unsigned long oldflags = vma->vm_flags;
2451da177e4SLinus Torvalds 	long nrpages = (end - start) >> PAGE_SHIFT;
2461da177e4SLinus Torvalds 	unsigned long charged = 0;
2471da177e4SLinus Torvalds 	pgoff_t pgoff;
2481da177e4SLinus Torvalds 	int error;
249c1e6098bSPeter Zijlstra 	int dirty_accountable = 0;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	if (newflags == oldflags) {
2521da177e4SLinus Torvalds 		*pprev = vma;
2531da177e4SLinus Torvalds 		return 0;
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	/*
2571da177e4SLinus Torvalds 	 * If we make a private mapping writable we increase our commit;
2581da177e4SLinus Torvalds 	 * but (without finer accounting) cannot reduce our commit if we
2595a6fe125SMel Gorman 	 * make it unwritable again. hugetlb mapping were accounted for
2605a6fe125SMel Gorman 	 * even if read-only so there is no need to account for them here
2611da177e4SLinus Torvalds 	 */
2621da177e4SLinus Torvalds 	if (newflags & VM_WRITE) {
2635a6fe125SMel Gorman 		if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
264cdfd4325SAndy Whitcroft 						VM_SHARED|VM_NORESERVE))) {
2651da177e4SLinus Torvalds 			charged = nrpages;
266191c5424SAl Viro 			if (security_vm_enough_memory_mm(mm, charged))
2671da177e4SLinus Torvalds 				return -ENOMEM;
2681da177e4SLinus Torvalds 			newflags |= VM_ACCOUNT;
2691da177e4SLinus Torvalds 		}
2701da177e4SLinus Torvalds 	}
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	/*
2731da177e4SLinus Torvalds 	 * First try to merge with previous and/or next vma.
2741da177e4SLinus Torvalds 	 */
2751da177e4SLinus Torvalds 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
2761da177e4SLinus Torvalds 	*pprev = vma_merge(mm, *pprev, start, end, newflags,
2771da177e4SLinus Torvalds 			vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2781da177e4SLinus Torvalds 	if (*pprev) {
2791da177e4SLinus Torvalds 		vma = *pprev;
2801da177e4SLinus Torvalds 		goto success;
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	*pprev = vma;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	if (start != vma->vm_start) {
2861da177e4SLinus Torvalds 		error = split_vma(mm, vma, start, 1);
2871da177e4SLinus Torvalds 		if (error)
2881da177e4SLinus Torvalds 			goto fail;
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	if (end != vma->vm_end) {
2921da177e4SLinus Torvalds 		error = split_vma(mm, vma, end, 0);
2931da177e4SLinus Torvalds 		if (error)
2941da177e4SLinus Torvalds 			goto fail;
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds success:
2981da177e4SLinus Torvalds 	/*
2991da177e4SLinus Torvalds 	 * vm_flags and vm_page_prot are protected by the mmap_sem
3001da177e4SLinus Torvalds 	 * held in write mode.
3011da177e4SLinus Torvalds 	 */
3021da177e4SLinus Torvalds 	vma->vm_flags = newflags;
3031c12c4cfSVenki Pallipadi 	vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
3041c12c4cfSVenki Pallipadi 					  vm_get_page_prot(newflags));
3051c12c4cfSVenki Pallipadi 
306c1e6098bSPeter Zijlstra 	if (vma_wants_writenotify(vma)) {
3071ddd439eSHugh Dickins 		vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
308c1e6098bSPeter Zijlstra 		dirty_accountable = 1;
309c1e6098bSPeter Zijlstra 	}
310d08b3851SPeter Zijlstra 
3117d12efaeSAndrew Morton 	change_protection(vma, start, end, vma->vm_page_prot,
3127d12efaeSAndrew Morton 			  dirty_accountable, 0);
3137da4d641SPeter Zijlstra 
314ab50b8edSHugh Dickins 	vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
315ab50b8edSHugh Dickins 	vm_stat_account(mm, newflags, vma->vm_file, nrpages);
31663bfd738SPekka Enberg 	perf_event_mmap(vma);
3171da177e4SLinus Torvalds 	return 0;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds fail:
3201da177e4SLinus Torvalds 	vm_unacct_memory(charged);
3211da177e4SLinus Torvalds 	return error;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3246a6160a7SHeiko Carstens SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
3256a6160a7SHeiko Carstens 		unsigned long, prot)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	unsigned long vm_flags, nstart, end, tmp, reqprot;
3281da177e4SLinus Torvalds 	struct vm_area_struct *vma, *prev;
3291da177e4SLinus Torvalds 	int error = -EINVAL;
3301da177e4SLinus Torvalds 	const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
3311da177e4SLinus Torvalds 	prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
3321da177e4SLinus Torvalds 	if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
3331da177e4SLinus Torvalds 		return -EINVAL;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	if (start & ~PAGE_MASK)
3361da177e4SLinus Torvalds 		return -EINVAL;
3371da177e4SLinus Torvalds 	if (!len)
3381da177e4SLinus Torvalds 		return 0;
3391da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
3401da177e4SLinus Torvalds 	end = start + len;
3411da177e4SLinus Torvalds 	if (end <= start)
3421da177e4SLinus Torvalds 		return -ENOMEM;
343b845f313SDave Kleikamp 	if (!arch_validate_prot(prot))
3441da177e4SLinus Torvalds 		return -EINVAL;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	reqprot = prot;
3471da177e4SLinus Torvalds 	/*
3481da177e4SLinus Torvalds 	 * Does the application expect PROT_READ to imply PROT_EXEC:
3491da177e4SLinus Torvalds 	 */
350b344e05cSHua Zhong 	if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
3511da177e4SLinus Torvalds 		prot |= PROT_EXEC;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot);
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	down_write(&current->mm->mmap_sem);
3561da177e4SLinus Torvalds 
357097d5910SLinus Torvalds 	vma = find_vma(current->mm, start);
3581da177e4SLinus Torvalds 	error = -ENOMEM;
3591da177e4SLinus Torvalds 	if (!vma)
3601da177e4SLinus Torvalds 		goto out;
361097d5910SLinus Torvalds 	prev = vma->vm_prev;
3621da177e4SLinus Torvalds 	if (unlikely(grows & PROT_GROWSDOWN)) {
3631da177e4SLinus Torvalds 		if (vma->vm_start >= end)
3641da177e4SLinus Torvalds 			goto out;
3651da177e4SLinus Torvalds 		start = vma->vm_start;
3661da177e4SLinus Torvalds 		error = -EINVAL;
3671da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_GROWSDOWN))
3681da177e4SLinus Torvalds 			goto out;
3697d12efaeSAndrew Morton 	} else {
3701da177e4SLinus Torvalds 		if (vma->vm_start > start)
3711da177e4SLinus Torvalds 			goto out;
3721da177e4SLinus Torvalds 		if (unlikely(grows & PROT_GROWSUP)) {
3731da177e4SLinus Torvalds 			end = vma->vm_end;
3741da177e4SLinus Torvalds 			error = -EINVAL;
3751da177e4SLinus Torvalds 			if (!(vma->vm_flags & VM_GROWSUP))
3761da177e4SLinus Torvalds 				goto out;
3771da177e4SLinus Torvalds 		}
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 	if (start > vma->vm_start)
3801da177e4SLinus Torvalds 		prev = vma;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	for (nstart = start ; ; ) {
3831da177e4SLinus Torvalds 		unsigned long newflags;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 		/* Here we know that vma->vm_start <= nstart < vma->vm_end. */
3861da177e4SLinus Torvalds 
3877d12efaeSAndrew Morton 		newflags = vm_flags;
3887d12efaeSAndrew Morton 		newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
3891da177e4SLinus Torvalds 
3907e2cff42SPaolo 'Blaisorblade' Giarrusso 		/* newflags >> 4 shift VM_MAY% in place of VM_% */
3917e2cff42SPaolo 'Blaisorblade' Giarrusso 		if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
3921da177e4SLinus Torvalds 			error = -EACCES;
3931da177e4SLinus Torvalds 			goto out;
3941da177e4SLinus Torvalds 		}
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 		error = security_file_mprotect(vma, reqprot, prot);
3971da177e4SLinus Torvalds 		if (error)
3981da177e4SLinus Torvalds 			goto out;
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 		tmp = vma->vm_end;
4011da177e4SLinus Torvalds 		if (tmp > end)
4021da177e4SLinus Torvalds 			tmp = end;
4031da177e4SLinus Torvalds 		error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
4041da177e4SLinus Torvalds 		if (error)
4051da177e4SLinus Torvalds 			goto out;
4061da177e4SLinus Torvalds 		nstart = tmp;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 		if (nstart < prev->vm_end)
4091da177e4SLinus Torvalds 			nstart = prev->vm_end;
4101da177e4SLinus Torvalds 		if (nstart >= end)
4111da177e4SLinus Torvalds 			goto out;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 		vma = prev->vm_next;
4141da177e4SLinus Torvalds 		if (!vma || vma->vm_start != nstart) {
4151da177e4SLinus Torvalds 			error = -ENOMEM;
4161da177e4SLinus Torvalds 			goto out;
4171da177e4SLinus Torvalds 		}
4181da177e4SLinus Torvalds 	}
4191da177e4SLinus Torvalds out:
4201da177e4SLinus Torvalds 	up_write(&current->mm->mmap_sem);
4211da177e4SLinus Torvalds 	return error;
4221da177e4SLinus Torvalds }
423