xref: /openbmc/linux/mm/vmalloc.c (revision 82a70ce0426dd7c4099516175019dccbd18cebf9)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Copyright (C) 1993  Linus Torvalds
41da177e4SLinus Torvalds  *  Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
51da177e4SLinus Torvalds  *  SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000
61da177e4SLinus Torvalds  *  Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002
7930fc45aSChristoph Lameter  *  Numa awareness, Christoph Lameter, SGI, June 2005
8d758ffe6SUladzislau Rezki (Sony)  *  Improving global KVA allocator, Uladzislau Rezki, Sony, May 2019
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
11db64fe02SNick Piggin #include <linux/vmalloc.h>
121da177e4SLinus Torvalds #include <linux/mm.h>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/highmem.h>
15c3edc401SIngo Molnar #include <linux/sched/signal.h>
161da177e4SLinus Torvalds #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/spinlock.h>
181da177e4SLinus Torvalds #include <linux/interrupt.h>
195f6a6a9cSAlexey Dobriyan #include <linux/proc_fs.h>
20a10aa579SChristoph Lameter #include <linux/seq_file.h>
21868b104dSRick Edgecombe #include <linux/set_memory.h>
223ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
2323016969SChristoph Lameter #include <linux/kallsyms.h>
24db64fe02SNick Piggin #include <linux/list.h>
254da56b99SChris Wilson #include <linux/notifier.h>
26db64fe02SNick Piggin #include <linux/rbtree.h>
270f14599cSMatthew Wilcox (Oracle) #include <linux/xarray.h>
285da96bddSMel Gorman #include <linux/io.h>
29db64fe02SNick Piggin #include <linux/rcupdate.h>
30f0aa6617STejun Heo #include <linux/pfn.h>
3189219d37SCatalin Marinas #include <linux/kmemleak.h>
3260063497SArun Sharma #include <linux/atomic.h>
333b32123dSGideon Israel Dsouza #include <linux/compiler.h>
3432fcfd40SAl Viro #include <linux/llist.h>
350f616be1SToshi Kani #include <linux/bitops.h>
3668ad4a33SUladzislau Rezki (Sony) #include <linux/rbtree_augmented.h>
37bdebd6a2SJann Horn #include <linux/overflow.h>
38c0eb315aSNicholas Piggin #include <linux/pgtable.h>
397c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
40f7ee1f13SChristophe Leroy #include <linux/hugetlb.h>
411da177e4SLinus Torvalds #include <asm/tlbflush.h>
422dca6999SDavid Miller #include <asm/shmparam.h>
431da177e4SLinus Torvalds 
44dd56b046SMel Gorman #include "internal.h"
452a681cfaSJoerg Roedel #include "pgalloc-track.h"
46dd56b046SMel Gorman 
47*82a70ce0SChristoph Hellwig #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
48*82a70ce0SChristoph Hellwig static unsigned int __ro_after_init ioremap_max_page_shift = BITS_PER_LONG - 1;
49*82a70ce0SChristoph Hellwig 
50*82a70ce0SChristoph Hellwig static int __init set_nohugeiomap(char *str)
51*82a70ce0SChristoph Hellwig {
52*82a70ce0SChristoph Hellwig 	ioremap_max_page_shift = PAGE_SHIFT;
53*82a70ce0SChristoph Hellwig 	return 0;
54*82a70ce0SChristoph Hellwig }
55*82a70ce0SChristoph Hellwig early_param("nohugeiomap", set_nohugeiomap);
56*82a70ce0SChristoph Hellwig #else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
57*82a70ce0SChristoph Hellwig static const unsigned int ioremap_max_page_shift = PAGE_SHIFT;
58*82a70ce0SChristoph Hellwig #endif	/* CONFIG_HAVE_ARCH_HUGE_VMAP */
59*82a70ce0SChristoph Hellwig 
60121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
61121e6f32SNicholas Piggin static bool __ro_after_init vmap_allow_huge = true;
62121e6f32SNicholas Piggin 
63121e6f32SNicholas Piggin static int __init set_nohugevmalloc(char *str)
64121e6f32SNicholas Piggin {
65121e6f32SNicholas Piggin 	vmap_allow_huge = false;
66121e6f32SNicholas Piggin 	return 0;
67121e6f32SNicholas Piggin }
68121e6f32SNicholas Piggin early_param("nohugevmalloc", set_nohugevmalloc);
69121e6f32SNicholas Piggin #else /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
70121e6f32SNicholas Piggin static const bool vmap_allow_huge = false;
71121e6f32SNicholas Piggin #endif	/* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
72121e6f32SNicholas Piggin 
73186525bdSIngo Molnar bool is_vmalloc_addr(const void *x)
74186525bdSIngo Molnar {
75186525bdSIngo Molnar 	unsigned long addr = (unsigned long)x;
76186525bdSIngo Molnar 
77186525bdSIngo Molnar 	return addr >= VMALLOC_START && addr < VMALLOC_END;
78186525bdSIngo Molnar }
79186525bdSIngo Molnar EXPORT_SYMBOL(is_vmalloc_addr);
80186525bdSIngo Molnar 
8132fcfd40SAl Viro struct vfree_deferred {
8232fcfd40SAl Viro 	struct llist_head list;
8332fcfd40SAl Viro 	struct work_struct wq;
8432fcfd40SAl Viro };
8532fcfd40SAl Viro static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
8632fcfd40SAl Viro 
8732fcfd40SAl Viro static void __vunmap(const void *, int);
8832fcfd40SAl Viro 
8932fcfd40SAl Viro static void free_work(struct work_struct *w)
9032fcfd40SAl Viro {
9132fcfd40SAl Viro 	struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
92894e58c1SByungchul Park 	struct llist_node *t, *llnode;
93894e58c1SByungchul Park 
94894e58c1SByungchul Park 	llist_for_each_safe(llnode, t, llist_del_all(&p->list))
95894e58c1SByungchul Park 		__vunmap((void *)llnode, 1);
9632fcfd40SAl Viro }
9732fcfd40SAl Viro 
98db64fe02SNick Piggin /*** Page table manipulation functions ***/
995e9e3d77SNicholas Piggin static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
1005e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
101f7ee1f13SChristophe Leroy 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
1025e9e3d77SNicholas Piggin {
1035e9e3d77SNicholas Piggin 	pte_t *pte;
1045e9e3d77SNicholas Piggin 	u64 pfn;
105f7ee1f13SChristophe Leroy 	unsigned long size = PAGE_SIZE;
1065e9e3d77SNicholas Piggin 
1075e9e3d77SNicholas Piggin 	pfn = phys_addr >> PAGE_SHIFT;
1085e9e3d77SNicholas Piggin 	pte = pte_alloc_kernel_track(pmd, addr, mask);
1095e9e3d77SNicholas Piggin 	if (!pte)
1105e9e3d77SNicholas Piggin 		return -ENOMEM;
1115e9e3d77SNicholas Piggin 	do {
1125e9e3d77SNicholas Piggin 		BUG_ON(!pte_none(*pte));
113f7ee1f13SChristophe Leroy 
114f7ee1f13SChristophe Leroy #ifdef CONFIG_HUGETLB_PAGE
115f7ee1f13SChristophe Leroy 		size = arch_vmap_pte_range_map_size(addr, end, pfn, max_page_shift);
116f7ee1f13SChristophe Leroy 		if (size != PAGE_SIZE) {
117f7ee1f13SChristophe Leroy 			pte_t entry = pfn_pte(pfn, prot);
118f7ee1f13SChristophe Leroy 
119f7ee1f13SChristophe Leroy 			entry = pte_mkhuge(entry);
120f7ee1f13SChristophe Leroy 			entry = arch_make_huge_pte(entry, ilog2(size), 0);
121f7ee1f13SChristophe Leroy 			set_huge_pte_at(&init_mm, addr, pte, entry);
122f7ee1f13SChristophe Leroy 			pfn += PFN_DOWN(size);
123f7ee1f13SChristophe Leroy 			continue;
124f7ee1f13SChristophe Leroy 		}
125f7ee1f13SChristophe Leroy #endif
1265e9e3d77SNicholas Piggin 		set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
1275e9e3d77SNicholas Piggin 		pfn++;
128f7ee1f13SChristophe Leroy 	} while (pte += PFN_DOWN(size), addr += size, addr != end);
1295e9e3d77SNicholas Piggin 	*mask |= PGTBL_PTE_MODIFIED;
1305e9e3d77SNicholas Piggin 	return 0;
1315e9e3d77SNicholas Piggin }
1325e9e3d77SNicholas Piggin 
1335e9e3d77SNicholas Piggin static int vmap_try_huge_pmd(pmd_t *pmd, unsigned long addr, unsigned long end,
1345e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
1355e9e3d77SNicholas Piggin 			unsigned int max_page_shift)
1365e9e3d77SNicholas Piggin {
1375e9e3d77SNicholas Piggin 	if (max_page_shift < PMD_SHIFT)
1385e9e3d77SNicholas Piggin 		return 0;
1395e9e3d77SNicholas Piggin 
1405e9e3d77SNicholas Piggin 	if (!arch_vmap_pmd_supported(prot))
1415e9e3d77SNicholas Piggin 		return 0;
1425e9e3d77SNicholas Piggin 
1435e9e3d77SNicholas Piggin 	if ((end - addr) != PMD_SIZE)
1445e9e3d77SNicholas Piggin 		return 0;
1455e9e3d77SNicholas Piggin 
1465e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(addr, PMD_SIZE))
1475e9e3d77SNicholas Piggin 		return 0;
1485e9e3d77SNicholas Piggin 
1495e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(phys_addr, PMD_SIZE))
1505e9e3d77SNicholas Piggin 		return 0;
1515e9e3d77SNicholas Piggin 
1525e9e3d77SNicholas Piggin 	if (pmd_present(*pmd) && !pmd_free_pte_page(pmd, addr))
1535e9e3d77SNicholas Piggin 		return 0;
1545e9e3d77SNicholas Piggin 
1555e9e3d77SNicholas Piggin 	return pmd_set_huge(pmd, phys_addr, prot);
1565e9e3d77SNicholas Piggin }
1575e9e3d77SNicholas Piggin 
1585e9e3d77SNicholas Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
1595e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
1605e9e3d77SNicholas Piggin 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
1615e9e3d77SNicholas Piggin {
1625e9e3d77SNicholas Piggin 	pmd_t *pmd;
1635e9e3d77SNicholas Piggin 	unsigned long next;
1645e9e3d77SNicholas Piggin 
1655e9e3d77SNicholas Piggin 	pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
1665e9e3d77SNicholas Piggin 	if (!pmd)
1675e9e3d77SNicholas Piggin 		return -ENOMEM;
1685e9e3d77SNicholas Piggin 	do {
1695e9e3d77SNicholas Piggin 		next = pmd_addr_end(addr, end);
1705e9e3d77SNicholas Piggin 
1715e9e3d77SNicholas Piggin 		if (vmap_try_huge_pmd(pmd, addr, next, phys_addr, prot,
1725e9e3d77SNicholas Piggin 					max_page_shift)) {
1735e9e3d77SNicholas Piggin 			*mask |= PGTBL_PMD_MODIFIED;
1745e9e3d77SNicholas Piggin 			continue;
1755e9e3d77SNicholas Piggin 		}
1765e9e3d77SNicholas Piggin 
177f7ee1f13SChristophe Leroy 		if (vmap_pte_range(pmd, addr, next, phys_addr, prot, max_page_shift, mask))
1785e9e3d77SNicholas Piggin 			return -ENOMEM;
1795e9e3d77SNicholas Piggin 	} while (pmd++, phys_addr += (next - addr), addr = next, addr != end);
1805e9e3d77SNicholas Piggin 	return 0;
1815e9e3d77SNicholas Piggin }
1825e9e3d77SNicholas Piggin 
1835e9e3d77SNicholas Piggin static int vmap_try_huge_pud(pud_t *pud, unsigned long addr, unsigned long end,
1845e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
1855e9e3d77SNicholas Piggin 			unsigned int max_page_shift)
1865e9e3d77SNicholas Piggin {
1875e9e3d77SNicholas Piggin 	if (max_page_shift < PUD_SHIFT)
1885e9e3d77SNicholas Piggin 		return 0;
1895e9e3d77SNicholas Piggin 
1905e9e3d77SNicholas Piggin 	if (!arch_vmap_pud_supported(prot))
1915e9e3d77SNicholas Piggin 		return 0;
1925e9e3d77SNicholas Piggin 
1935e9e3d77SNicholas Piggin 	if ((end - addr) != PUD_SIZE)
1945e9e3d77SNicholas Piggin 		return 0;
1955e9e3d77SNicholas Piggin 
1965e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(addr, PUD_SIZE))
1975e9e3d77SNicholas Piggin 		return 0;
1985e9e3d77SNicholas Piggin 
1995e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(phys_addr, PUD_SIZE))
2005e9e3d77SNicholas Piggin 		return 0;
2015e9e3d77SNicholas Piggin 
2025e9e3d77SNicholas Piggin 	if (pud_present(*pud) && !pud_free_pmd_page(pud, addr))
2035e9e3d77SNicholas Piggin 		return 0;
2045e9e3d77SNicholas Piggin 
2055e9e3d77SNicholas Piggin 	return pud_set_huge(pud, phys_addr, prot);
2065e9e3d77SNicholas Piggin }
2075e9e3d77SNicholas Piggin 
2085e9e3d77SNicholas Piggin static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
2095e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
2105e9e3d77SNicholas Piggin 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
2115e9e3d77SNicholas Piggin {
2125e9e3d77SNicholas Piggin 	pud_t *pud;
2135e9e3d77SNicholas Piggin 	unsigned long next;
2145e9e3d77SNicholas Piggin 
2155e9e3d77SNicholas Piggin 	pud = pud_alloc_track(&init_mm, p4d, addr, mask);
2165e9e3d77SNicholas Piggin 	if (!pud)
2175e9e3d77SNicholas Piggin 		return -ENOMEM;
2185e9e3d77SNicholas Piggin 	do {
2195e9e3d77SNicholas Piggin 		next = pud_addr_end(addr, end);
2205e9e3d77SNicholas Piggin 
2215e9e3d77SNicholas Piggin 		if (vmap_try_huge_pud(pud, addr, next, phys_addr, prot,
2225e9e3d77SNicholas Piggin 					max_page_shift)) {
2235e9e3d77SNicholas Piggin 			*mask |= PGTBL_PUD_MODIFIED;
2245e9e3d77SNicholas Piggin 			continue;
2255e9e3d77SNicholas Piggin 		}
2265e9e3d77SNicholas Piggin 
2275e9e3d77SNicholas Piggin 		if (vmap_pmd_range(pud, addr, next, phys_addr, prot,
2285e9e3d77SNicholas Piggin 					max_page_shift, mask))
2295e9e3d77SNicholas Piggin 			return -ENOMEM;
2305e9e3d77SNicholas Piggin 	} while (pud++, phys_addr += (next - addr), addr = next, addr != end);
2315e9e3d77SNicholas Piggin 	return 0;
2325e9e3d77SNicholas Piggin }
2335e9e3d77SNicholas Piggin 
2345e9e3d77SNicholas Piggin static int vmap_try_huge_p4d(p4d_t *p4d, unsigned long addr, unsigned long end,
2355e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
2365e9e3d77SNicholas Piggin 			unsigned int max_page_shift)
2375e9e3d77SNicholas Piggin {
2385e9e3d77SNicholas Piggin 	if (max_page_shift < P4D_SHIFT)
2395e9e3d77SNicholas Piggin 		return 0;
2405e9e3d77SNicholas Piggin 
2415e9e3d77SNicholas Piggin 	if (!arch_vmap_p4d_supported(prot))
2425e9e3d77SNicholas Piggin 		return 0;
2435e9e3d77SNicholas Piggin 
2445e9e3d77SNicholas Piggin 	if ((end - addr) != P4D_SIZE)
2455e9e3d77SNicholas Piggin 		return 0;
2465e9e3d77SNicholas Piggin 
2475e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(addr, P4D_SIZE))
2485e9e3d77SNicholas Piggin 		return 0;
2495e9e3d77SNicholas Piggin 
2505e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(phys_addr, P4D_SIZE))
2515e9e3d77SNicholas Piggin 		return 0;
2525e9e3d77SNicholas Piggin 
2535e9e3d77SNicholas Piggin 	if (p4d_present(*p4d) && !p4d_free_pud_page(p4d, addr))
2545e9e3d77SNicholas Piggin 		return 0;
2555e9e3d77SNicholas Piggin 
2565e9e3d77SNicholas Piggin 	return p4d_set_huge(p4d, phys_addr, prot);
2575e9e3d77SNicholas Piggin }
2585e9e3d77SNicholas Piggin 
2595e9e3d77SNicholas Piggin static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
2605e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
2615e9e3d77SNicholas Piggin 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
2625e9e3d77SNicholas Piggin {
2635e9e3d77SNicholas Piggin 	p4d_t *p4d;
2645e9e3d77SNicholas Piggin 	unsigned long next;
2655e9e3d77SNicholas Piggin 
2665e9e3d77SNicholas Piggin 	p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
2675e9e3d77SNicholas Piggin 	if (!p4d)
2685e9e3d77SNicholas Piggin 		return -ENOMEM;
2695e9e3d77SNicholas Piggin 	do {
2705e9e3d77SNicholas Piggin 		next = p4d_addr_end(addr, end);
2715e9e3d77SNicholas Piggin 
2725e9e3d77SNicholas Piggin 		if (vmap_try_huge_p4d(p4d, addr, next, phys_addr, prot,
2735e9e3d77SNicholas Piggin 					max_page_shift)) {
2745e9e3d77SNicholas Piggin 			*mask |= PGTBL_P4D_MODIFIED;
2755e9e3d77SNicholas Piggin 			continue;
2765e9e3d77SNicholas Piggin 		}
2775e9e3d77SNicholas Piggin 
2785e9e3d77SNicholas Piggin 		if (vmap_pud_range(p4d, addr, next, phys_addr, prot,
2795e9e3d77SNicholas Piggin 					max_page_shift, mask))
2805e9e3d77SNicholas Piggin 			return -ENOMEM;
2815e9e3d77SNicholas Piggin 	} while (p4d++, phys_addr += (next - addr), addr = next, addr != end);
2825e9e3d77SNicholas Piggin 	return 0;
2835e9e3d77SNicholas Piggin }
2845e9e3d77SNicholas Piggin 
2855d87510dSNicholas Piggin static int vmap_range_noflush(unsigned long addr, unsigned long end,
2865e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
2875e9e3d77SNicholas Piggin 			unsigned int max_page_shift)
2885e9e3d77SNicholas Piggin {
2895e9e3d77SNicholas Piggin 	pgd_t *pgd;
2905e9e3d77SNicholas Piggin 	unsigned long start;
2915e9e3d77SNicholas Piggin 	unsigned long next;
2925e9e3d77SNicholas Piggin 	int err;
2935e9e3d77SNicholas Piggin 	pgtbl_mod_mask mask = 0;
2945e9e3d77SNicholas Piggin 
2955e9e3d77SNicholas Piggin 	might_sleep();
2965e9e3d77SNicholas Piggin 	BUG_ON(addr >= end);
2975e9e3d77SNicholas Piggin 
2985e9e3d77SNicholas Piggin 	start = addr;
2995e9e3d77SNicholas Piggin 	pgd = pgd_offset_k(addr);
3005e9e3d77SNicholas Piggin 	do {
3015e9e3d77SNicholas Piggin 		next = pgd_addr_end(addr, end);
3025e9e3d77SNicholas Piggin 		err = vmap_p4d_range(pgd, addr, next, phys_addr, prot,
3035e9e3d77SNicholas Piggin 					max_page_shift, &mask);
3045e9e3d77SNicholas Piggin 		if (err)
3055e9e3d77SNicholas Piggin 			break;
3065e9e3d77SNicholas Piggin 	} while (pgd++, phys_addr += (next - addr), addr = next, addr != end);
3075e9e3d77SNicholas Piggin 
3085e9e3d77SNicholas Piggin 	if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
3095e9e3d77SNicholas Piggin 		arch_sync_kernel_mappings(start, end);
3105e9e3d77SNicholas Piggin 
3115e9e3d77SNicholas Piggin 	return err;
3125e9e3d77SNicholas Piggin }
313b221385bSAdrian Bunk 
314*82a70ce0SChristoph Hellwig int ioremap_page_range(unsigned long addr, unsigned long end,
315*82a70ce0SChristoph Hellwig 		phys_addr_t phys_addr, pgprot_t prot)
3165d87510dSNicholas Piggin {
3175d87510dSNicholas Piggin 	int err;
3185d87510dSNicholas Piggin 
319*82a70ce0SChristoph Hellwig 	err = vmap_range_noflush(addr, end, phys_addr, prot,
320*82a70ce0SChristoph Hellwig 				 ioremap_max_page_shift);
3215d87510dSNicholas Piggin 	flush_cache_vmap(addr, end);
3225d87510dSNicholas Piggin 	return err;
3235d87510dSNicholas Piggin }
3245d87510dSNicholas Piggin 
3252ba3e694SJoerg Roedel static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
3262ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	pte_t *pte;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	pte = pte_offset_kernel(pmd, addr);
3311da177e4SLinus Torvalds 	do {
3321da177e4SLinus Torvalds 		pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
3331da177e4SLinus Torvalds 		WARN_ON(!pte_none(ptent) && !pte_present(ptent));
3341da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
3352ba3e694SJoerg Roedel 	*mask |= PGTBL_PTE_MODIFIED;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3382ba3e694SJoerg Roedel static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
3392ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	pmd_t *pmd;
3421da177e4SLinus Torvalds 	unsigned long next;
3432ba3e694SJoerg Roedel 	int cleared;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
3461da177e4SLinus Torvalds 	do {
3471da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
3482ba3e694SJoerg Roedel 
3492ba3e694SJoerg Roedel 		cleared = pmd_clear_huge(pmd);
3502ba3e694SJoerg Roedel 		if (cleared || pmd_bad(*pmd))
3512ba3e694SJoerg Roedel 			*mask |= PGTBL_PMD_MODIFIED;
3522ba3e694SJoerg Roedel 
3532ba3e694SJoerg Roedel 		if (cleared)
354b9820d8fSToshi Kani 			continue;
3551da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
3561da177e4SLinus Torvalds 			continue;
3572ba3e694SJoerg Roedel 		vunmap_pte_range(pmd, addr, next, mask);
358e47110e9SAneesh Kumar K.V 
359e47110e9SAneesh Kumar K.V 		cond_resched();
3601da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3632ba3e694SJoerg Roedel static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
3642ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	pud_t *pud;
3671da177e4SLinus Torvalds 	unsigned long next;
3682ba3e694SJoerg Roedel 	int cleared;
3691da177e4SLinus Torvalds 
370c2febafcSKirill A. Shutemov 	pud = pud_offset(p4d, addr);
3711da177e4SLinus Torvalds 	do {
3721da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
3732ba3e694SJoerg Roedel 
3742ba3e694SJoerg Roedel 		cleared = pud_clear_huge(pud);
3752ba3e694SJoerg Roedel 		if (cleared || pud_bad(*pud))
3762ba3e694SJoerg Roedel 			*mask |= PGTBL_PUD_MODIFIED;
3772ba3e694SJoerg Roedel 
3782ba3e694SJoerg Roedel 		if (cleared)
379b9820d8fSToshi Kani 			continue;
3801da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
3811da177e4SLinus Torvalds 			continue;
3822ba3e694SJoerg Roedel 		vunmap_pmd_range(pud, addr, next, mask);
3831da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3862ba3e694SJoerg Roedel static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
3872ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
388c2febafcSKirill A. Shutemov {
389c2febafcSKirill A. Shutemov 	p4d_t *p4d;
390c2febafcSKirill A. Shutemov 	unsigned long next;
3912ba3e694SJoerg Roedel 	int cleared;
392c2febafcSKirill A. Shutemov 
393c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
394c2febafcSKirill A. Shutemov 	do {
395c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
3962ba3e694SJoerg Roedel 
3972ba3e694SJoerg Roedel 		cleared = p4d_clear_huge(p4d);
3982ba3e694SJoerg Roedel 		if (cleared || p4d_bad(*p4d))
3992ba3e694SJoerg Roedel 			*mask |= PGTBL_P4D_MODIFIED;
4002ba3e694SJoerg Roedel 
4012ba3e694SJoerg Roedel 		if (cleared)
402c2febafcSKirill A. Shutemov 			continue;
403c2febafcSKirill A. Shutemov 		if (p4d_none_or_clear_bad(p4d))
404c2febafcSKirill A. Shutemov 			continue;
4052ba3e694SJoerg Roedel 		vunmap_pud_range(p4d, addr, next, mask);
406c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
407c2febafcSKirill A. Shutemov }
408c2febafcSKirill A. Shutemov 
4094ad0ae8cSNicholas Piggin /*
4104ad0ae8cSNicholas Piggin  * vunmap_range_noflush is similar to vunmap_range, but does not
4114ad0ae8cSNicholas Piggin  * flush caches or TLBs.
412b521c43fSChristoph Hellwig  *
4134ad0ae8cSNicholas Piggin  * The caller is responsible for calling flush_cache_vmap() before calling
4144ad0ae8cSNicholas Piggin  * this function, and flush_tlb_kernel_range after it has returned
4154ad0ae8cSNicholas Piggin  * successfully (and before the addresses are expected to cause a page fault
4164ad0ae8cSNicholas Piggin  * or be re-mapped for something else, if TLB flushes are being delayed or
4174ad0ae8cSNicholas Piggin  * coalesced).
418b521c43fSChristoph Hellwig  *
4194ad0ae8cSNicholas Piggin  * This is an internal function only. Do not use outside mm/.
420b521c43fSChristoph Hellwig  */
4214ad0ae8cSNicholas Piggin void vunmap_range_noflush(unsigned long start, unsigned long end)
4221da177e4SLinus Torvalds {
4231da177e4SLinus Torvalds 	unsigned long next;
424b521c43fSChristoph Hellwig 	pgd_t *pgd;
4252ba3e694SJoerg Roedel 	unsigned long addr = start;
4262ba3e694SJoerg Roedel 	pgtbl_mod_mask mask = 0;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	BUG_ON(addr >= end);
4291da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
4301da177e4SLinus Torvalds 	do {
4311da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
4322ba3e694SJoerg Roedel 		if (pgd_bad(*pgd))
4332ba3e694SJoerg Roedel 			mask |= PGTBL_PGD_MODIFIED;
4341da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
4351da177e4SLinus Torvalds 			continue;
4362ba3e694SJoerg Roedel 		vunmap_p4d_range(pgd, addr, next, &mask);
4371da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
4382ba3e694SJoerg Roedel 
4392ba3e694SJoerg Roedel 	if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
4402ba3e694SJoerg Roedel 		arch_sync_kernel_mappings(start, end);
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4434ad0ae8cSNicholas Piggin /**
4444ad0ae8cSNicholas Piggin  * vunmap_range - unmap kernel virtual addresses
4454ad0ae8cSNicholas Piggin  * @addr: start of the VM area to unmap
4464ad0ae8cSNicholas Piggin  * @end: end of the VM area to unmap (non-inclusive)
4474ad0ae8cSNicholas Piggin  *
4484ad0ae8cSNicholas Piggin  * Clears any present PTEs in the virtual address range, flushes TLBs and
4494ad0ae8cSNicholas Piggin  * caches. Any subsequent access to the address before it has been re-mapped
4504ad0ae8cSNicholas Piggin  * is a kernel bug.
4514ad0ae8cSNicholas Piggin  */
4524ad0ae8cSNicholas Piggin void vunmap_range(unsigned long addr, unsigned long end)
4534ad0ae8cSNicholas Piggin {
4544ad0ae8cSNicholas Piggin 	flush_cache_vunmap(addr, end);
4554ad0ae8cSNicholas Piggin 	vunmap_range_noflush(addr, end);
4564ad0ae8cSNicholas Piggin 	flush_tlb_kernel_range(addr, end);
4574ad0ae8cSNicholas Piggin }
4584ad0ae8cSNicholas Piggin 
4590a264884SNicholas Piggin static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr,
4602ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
4612ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	pte_t *pte;
4641da177e4SLinus Torvalds 
465db64fe02SNick Piggin 	/*
466db64fe02SNick Piggin 	 * nr is a running index into the array which helps higher level
467db64fe02SNick Piggin 	 * callers keep track of where we're up to.
468db64fe02SNick Piggin 	 */
469db64fe02SNick Piggin 
4702ba3e694SJoerg Roedel 	pte = pte_alloc_kernel_track(pmd, addr, mask);
4711da177e4SLinus Torvalds 	if (!pte)
4721da177e4SLinus Torvalds 		return -ENOMEM;
4731da177e4SLinus Torvalds 	do {
474db64fe02SNick Piggin 		struct page *page = pages[*nr];
475db64fe02SNick Piggin 
476db64fe02SNick Piggin 		if (WARN_ON(!pte_none(*pte)))
477db64fe02SNick Piggin 			return -EBUSY;
478db64fe02SNick Piggin 		if (WARN_ON(!page))
4791da177e4SLinus Torvalds 			return -ENOMEM;
4801da177e4SLinus Torvalds 		set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
481db64fe02SNick Piggin 		(*nr)++;
4821da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
4832ba3e694SJoerg Roedel 	*mask |= PGTBL_PTE_MODIFIED;
4841da177e4SLinus Torvalds 	return 0;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4870a264884SNicholas Piggin static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,
4882ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
4892ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	pmd_t *pmd;
4921da177e4SLinus Torvalds 	unsigned long next;
4931da177e4SLinus Torvalds 
4942ba3e694SJoerg Roedel 	pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
4951da177e4SLinus Torvalds 	if (!pmd)
4961da177e4SLinus Torvalds 		return -ENOMEM;
4971da177e4SLinus Torvalds 	do {
4981da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
4990a264884SNicholas Piggin 		if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask))
5001da177e4SLinus Torvalds 			return -ENOMEM;
5011da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
5021da177e4SLinus Torvalds 	return 0;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
5050a264884SNicholas Piggin static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
5062ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
5072ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 	pud_t *pud;
5101da177e4SLinus Torvalds 	unsigned long next;
5111da177e4SLinus Torvalds 
5122ba3e694SJoerg Roedel 	pud = pud_alloc_track(&init_mm, p4d, addr, mask);
5131da177e4SLinus Torvalds 	if (!pud)
5141da177e4SLinus Torvalds 		return -ENOMEM;
5151da177e4SLinus Torvalds 	do {
5161da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
5170a264884SNicholas Piggin 		if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask))
5181da177e4SLinus Torvalds 			return -ENOMEM;
5191da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
5201da177e4SLinus Torvalds 	return 0;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5230a264884SNicholas Piggin static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
5242ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
5252ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
526c2febafcSKirill A. Shutemov {
527c2febafcSKirill A. Shutemov 	p4d_t *p4d;
528c2febafcSKirill A. Shutemov 	unsigned long next;
529c2febafcSKirill A. Shutemov 
5302ba3e694SJoerg Roedel 	p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
531c2febafcSKirill A. Shutemov 	if (!p4d)
532c2febafcSKirill A. Shutemov 		return -ENOMEM;
533c2febafcSKirill A. Shutemov 	do {
534c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
5350a264884SNicholas Piggin 		if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask))
536c2febafcSKirill A. Shutemov 			return -ENOMEM;
537c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
538c2febafcSKirill A. Shutemov 	return 0;
539c2febafcSKirill A. Shutemov }
540c2febafcSKirill A. Shutemov 
541121e6f32SNicholas Piggin static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
542121e6f32SNicholas Piggin 		pgprot_t prot, struct page **pages)
543121e6f32SNicholas Piggin {
544121e6f32SNicholas Piggin 	unsigned long start = addr;
545121e6f32SNicholas Piggin 	pgd_t *pgd;
546121e6f32SNicholas Piggin 	unsigned long next;
547121e6f32SNicholas Piggin 	int err = 0;
548121e6f32SNicholas Piggin 	int nr = 0;
549121e6f32SNicholas Piggin 	pgtbl_mod_mask mask = 0;
550121e6f32SNicholas Piggin 
551121e6f32SNicholas Piggin 	BUG_ON(addr >= end);
552121e6f32SNicholas Piggin 	pgd = pgd_offset_k(addr);
553121e6f32SNicholas Piggin 	do {
554121e6f32SNicholas Piggin 		next = pgd_addr_end(addr, end);
555121e6f32SNicholas Piggin 		if (pgd_bad(*pgd))
556121e6f32SNicholas Piggin 			mask |= PGTBL_PGD_MODIFIED;
557121e6f32SNicholas Piggin 		err = vmap_pages_p4d_range(pgd, addr, next, prot, pages, &nr, &mask);
558121e6f32SNicholas Piggin 		if (err)
559121e6f32SNicholas Piggin 			return err;
560121e6f32SNicholas Piggin 	} while (pgd++, addr = next, addr != end);
561121e6f32SNicholas Piggin 
562121e6f32SNicholas Piggin 	if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
563121e6f32SNicholas Piggin 		arch_sync_kernel_mappings(start, end);
564121e6f32SNicholas Piggin 
565121e6f32SNicholas Piggin 	return 0;
566121e6f32SNicholas Piggin }
567121e6f32SNicholas Piggin 
568b67177ecSNicholas Piggin /*
569b67177ecSNicholas Piggin  * vmap_pages_range_noflush is similar to vmap_pages_range, but does not
570b67177ecSNicholas Piggin  * flush caches.
571b67177ecSNicholas Piggin  *
572b67177ecSNicholas Piggin  * The caller is responsible for calling flush_cache_vmap() after this
573b67177ecSNicholas Piggin  * function returns successfully and before the addresses are accessed.
574b67177ecSNicholas Piggin  *
575b67177ecSNicholas Piggin  * This is an internal function only. Do not use outside mm/.
576b67177ecSNicholas Piggin  */
577b67177ecSNicholas Piggin int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
578121e6f32SNicholas Piggin 		pgprot_t prot, struct page **pages, unsigned int page_shift)
579121e6f32SNicholas Piggin {
580121e6f32SNicholas Piggin 	unsigned int i, nr = (end - addr) >> PAGE_SHIFT;
581121e6f32SNicholas Piggin 
582121e6f32SNicholas Piggin 	WARN_ON(page_shift < PAGE_SHIFT);
583121e6f32SNicholas Piggin 
584121e6f32SNicholas Piggin 	if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC) ||
585121e6f32SNicholas Piggin 			page_shift == PAGE_SHIFT)
586121e6f32SNicholas Piggin 		return vmap_small_pages_range_noflush(addr, end, prot, pages);
587121e6f32SNicholas Piggin 
588121e6f32SNicholas Piggin 	for (i = 0; i < nr; i += 1U << (page_shift - PAGE_SHIFT)) {
589121e6f32SNicholas Piggin 		int err;
590121e6f32SNicholas Piggin 
591121e6f32SNicholas Piggin 		err = vmap_range_noflush(addr, addr + (1UL << page_shift),
592121e6f32SNicholas Piggin 					__pa(page_address(pages[i])), prot,
593121e6f32SNicholas Piggin 					page_shift);
594121e6f32SNicholas Piggin 		if (err)
595121e6f32SNicholas Piggin 			return err;
596121e6f32SNicholas Piggin 
597121e6f32SNicholas Piggin 		addr += 1UL << page_shift;
598121e6f32SNicholas Piggin 	}
599121e6f32SNicholas Piggin 
600121e6f32SNicholas Piggin 	return 0;
601121e6f32SNicholas Piggin }
602121e6f32SNicholas Piggin 
603b67177ecSNicholas Piggin /**
604b67177ecSNicholas Piggin  * vmap_pages_range - map pages to a kernel virtual address
605b67177ecSNicholas Piggin  * @addr: start of the VM area to map
606b67177ecSNicholas Piggin  * @end: end of the VM area to map (non-inclusive)
607b67177ecSNicholas Piggin  * @prot: page protection flags to use
608b67177ecSNicholas Piggin  * @pages: pages to map (always PAGE_SIZE pages)
609b67177ecSNicholas Piggin  * @page_shift: maximum shift that the pages may be mapped with, @pages must
610b67177ecSNicholas Piggin  * be aligned and contiguous up to at least this shift.
611b67177ecSNicholas Piggin  *
612b67177ecSNicholas Piggin  * RETURNS:
613b67177ecSNicholas Piggin  * 0 on success, -errno on failure.
614b67177ecSNicholas Piggin  */
615121e6f32SNicholas Piggin static int vmap_pages_range(unsigned long addr, unsigned long end,
616121e6f32SNicholas Piggin 		pgprot_t prot, struct page **pages, unsigned int page_shift)
617121e6f32SNicholas Piggin {
618121e6f32SNicholas Piggin 	int err;
619121e6f32SNicholas Piggin 
620121e6f32SNicholas Piggin 	err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
621121e6f32SNicholas Piggin 	flush_cache_vmap(addr, end);
622121e6f32SNicholas Piggin 	return err;
623121e6f32SNicholas Piggin }
624121e6f32SNicholas Piggin 
62581ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x)
62673bdf0a6SLinus Torvalds {
62773bdf0a6SLinus Torvalds 	/*
628ab4f2ee1SRussell King 	 * ARM, x86-64 and sparc64 put modules in a special place,
62973bdf0a6SLinus Torvalds 	 * and fall back on vmalloc() if that fails. Others
63073bdf0a6SLinus Torvalds 	 * just put it in the vmalloc space.
63173bdf0a6SLinus Torvalds 	 */
63273bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
63373bdf0a6SLinus Torvalds 	unsigned long addr = (unsigned long)x;
63473bdf0a6SLinus Torvalds 	if (addr >= MODULES_VADDR && addr < MODULES_END)
63573bdf0a6SLinus Torvalds 		return 1;
63673bdf0a6SLinus Torvalds #endif
63773bdf0a6SLinus Torvalds 	return is_vmalloc_addr(x);
63873bdf0a6SLinus Torvalds }
63973bdf0a6SLinus Torvalds 
64048667e7aSChristoph Lameter /*
641c0eb315aSNicholas Piggin  * Walk a vmap address to the struct page it maps. Huge vmap mappings will
642c0eb315aSNicholas Piggin  * return the tail page that corresponds to the base page address, which
643c0eb315aSNicholas Piggin  * matches small vmap mappings.
64448667e7aSChristoph Lameter  */
645add688fbSmalc struct page *vmalloc_to_page(const void *vmalloc_addr)
64648667e7aSChristoph Lameter {
64748667e7aSChristoph Lameter 	unsigned long addr = (unsigned long) vmalloc_addr;
648add688fbSmalc 	struct page *page = NULL;
64948667e7aSChristoph Lameter 	pgd_t *pgd = pgd_offset_k(addr);
650c2febafcSKirill A. Shutemov 	p4d_t *p4d;
651c2febafcSKirill A. Shutemov 	pud_t *pud;
652c2febafcSKirill A. Shutemov 	pmd_t *pmd;
653c2febafcSKirill A. Shutemov 	pte_t *ptep, pte;
65448667e7aSChristoph Lameter 
6557aa413deSIngo Molnar 	/*
6567aa413deSIngo Molnar 	 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
6577aa413deSIngo Molnar 	 * architectures that do not vmalloc module space
6587aa413deSIngo Molnar 	 */
65973bdf0a6SLinus Torvalds 	VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
66059ea7463SJiri Slaby 
661c2febafcSKirill A. Shutemov 	if (pgd_none(*pgd))
662c2febafcSKirill A. Shutemov 		return NULL;
663c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(pgd_leaf(*pgd)))
664c0eb315aSNicholas Piggin 		return NULL; /* XXX: no allowance for huge pgd */
665c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(pgd_bad(*pgd)))
666c0eb315aSNicholas Piggin 		return NULL;
667c0eb315aSNicholas Piggin 
668c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
669c2febafcSKirill A. Shutemov 	if (p4d_none(*p4d))
670c2febafcSKirill A. Shutemov 		return NULL;
671c0eb315aSNicholas Piggin 	if (p4d_leaf(*p4d))
672c0eb315aSNicholas Piggin 		return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT);
673c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(p4d_bad(*p4d)))
674c2febafcSKirill A. Shutemov 		return NULL;
675c0eb315aSNicholas Piggin 
676c0eb315aSNicholas Piggin 	pud = pud_offset(p4d, addr);
677c0eb315aSNicholas Piggin 	if (pud_none(*pud))
678c0eb315aSNicholas Piggin 		return NULL;
679c0eb315aSNicholas Piggin 	if (pud_leaf(*pud))
680c0eb315aSNicholas Piggin 		return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
681c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(pud_bad(*pud)))
682c0eb315aSNicholas Piggin 		return NULL;
683c0eb315aSNicholas Piggin 
684c2febafcSKirill A. Shutemov 	pmd = pmd_offset(pud, addr);
685c0eb315aSNicholas Piggin 	if (pmd_none(*pmd))
686c0eb315aSNicholas Piggin 		return NULL;
687c0eb315aSNicholas Piggin 	if (pmd_leaf(*pmd))
688c0eb315aSNicholas Piggin 		return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
689c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(pmd_bad(*pmd)))
690c2febafcSKirill A. Shutemov 		return NULL;
691db64fe02SNick Piggin 
69248667e7aSChristoph Lameter 	ptep = pte_offset_map(pmd, addr);
69348667e7aSChristoph Lameter 	pte = *ptep;
69448667e7aSChristoph Lameter 	if (pte_present(pte))
695add688fbSmalc 		page = pte_page(pte);
69648667e7aSChristoph Lameter 	pte_unmap(ptep);
697c0eb315aSNicholas Piggin 
698add688fbSmalc 	return page;
699ece86e22SJianyu Zhan }
700ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page);
701ece86e22SJianyu Zhan 
702add688fbSmalc /*
703add688fbSmalc  * Map a vmalloc()-space virtual address to the physical page frame number.
704add688fbSmalc  */
705add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
706add688fbSmalc {
707add688fbSmalc 	return page_to_pfn(vmalloc_to_page(vmalloc_addr));
708add688fbSmalc }
709add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn);
710add688fbSmalc 
711db64fe02SNick Piggin 
712db64fe02SNick Piggin /*** Global kva allocator ***/
713db64fe02SNick Piggin 
714bb850f4dSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_PROPAGATE_CHECK 0
715a6cf4e0fSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0
716bb850f4dSUladzislau Rezki (Sony) 
717db64fe02SNick Piggin 
718db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock);
719e36176beSUladzislau Rezki (Sony) static DEFINE_SPINLOCK(free_vmap_area_lock);
720f1c4069eSJoonsoo Kim /* Export for kexec only */
721f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
72289699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT;
72368ad4a33SUladzislau Rezki (Sony) static bool vmap_initialized __read_mostly;
72489699605SNick Piggin 
72596e2db45SUladzislau Rezki (Sony) static struct rb_root purge_vmap_area_root = RB_ROOT;
72696e2db45SUladzislau Rezki (Sony) static LIST_HEAD(purge_vmap_area_list);
72796e2db45SUladzislau Rezki (Sony) static DEFINE_SPINLOCK(purge_vmap_area_lock);
72896e2db45SUladzislau Rezki (Sony) 
72968ad4a33SUladzislau Rezki (Sony) /*
73068ad4a33SUladzislau Rezki (Sony)  * This kmem_cache is used for vmap_area objects. Instead of
73168ad4a33SUladzislau Rezki (Sony)  * allocating from slab we reuse an object from this cache to
73268ad4a33SUladzislau Rezki (Sony)  * make things faster. Especially in "no edge" splitting of
73368ad4a33SUladzislau Rezki (Sony)  * free block.
73468ad4a33SUladzislau Rezki (Sony)  */
73568ad4a33SUladzislau Rezki (Sony) static struct kmem_cache *vmap_area_cachep;
73689699605SNick Piggin 
73768ad4a33SUladzislau Rezki (Sony) /*
73868ad4a33SUladzislau Rezki (Sony)  * This linked list is used in pair with free_vmap_area_root.
73968ad4a33SUladzislau Rezki (Sony)  * It gives O(1) access to prev/next to perform fast coalescing.
74068ad4a33SUladzislau Rezki (Sony)  */
74168ad4a33SUladzislau Rezki (Sony) static LIST_HEAD(free_vmap_area_list);
74268ad4a33SUladzislau Rezki (Sony) 
74368ad4a33SUladzislau Rezki (Sony) /*
74468ad4a33SUladzislau Rezki (Sony)  * This augment red-black tree represents the free vmap space.
74568ad4a33SUladzislau Rezki (Sony)  * All vmap_area objects in this tree are sorted by va->va_start
74668ad4a33SUladzislau Rezki (Sony)  * address. It is used for allocation and merging when a vmap
74768ad4a33SUladzislau Rezki (Sony)  * object is released.
74868ad4a33SUladzislau Rezki (Sony)  *
74968ad4a33SUladzislau Rezki (Sony)  * Each vmap_area node contains a maximum available free block
75068ad4a33SUladzislau Rezki (Sony)  * of its sub-tree, right or left. Therefore it is possible to
75168ad4a33SUladzislau Rezki (Sony)  * find a lowest match of free area.
75268ad4a33SUladzislau Rezki (Sony)  */
75368ad4a33SUladzislau Rezki (Sony) static struct rb_root free_vmap_area_root = RB_ROOT;
75468ad4a33SUladzislau Rezki (Sony) 
75582dd23e8SUladzislau Rezki (Sony) /*
75682dd23e8SUladzislau Rezki (Sony)  * Preload a CPU with one object for "no edge" split case. The
75782dd23e8SUladzislau Rezki (Sony)  * aim is to get rid of allocations from the atomic context, thus
75882dd23e8SUladzislau Rezki (Sony)  * to use more permissive allocation masks.
75982dd23e8SUladzislau Rezki (Sony)  */
76082dd23e8SUladzislau Rezki (Sony) static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node);
76182dd23e8SUladzislau Rezki (Sony) 
76268ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
76368ad4a33SUladzislau Rezki (Sony) va_size(struct vmap_area *va)
76468ad4a33SUladzislau Rezki (Sony) {
76568ad4a33SUladzislau Rezki (Sony) 	return (va->va_end - va->va_start);
76668ad4a33SUladzislau Rezki (Sony) }
76768ad4a33SUladzislau Rezki (Sony) 
76868ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
76968ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(struct rb_node *node)
77068ad4a33SUladzislau Rezki (Sony) {
77168ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
77268ad4a33SUladzislau Rezki (Sony) 
77368ad4a33SUladzislau Rezki (Sony) 	va = rb_entry_safe(node, struct vmap_area, rb_node);
77468ad4a33SUladzislau Rezki (Sony) 	return va ? va->subtree_max_size : 0;
77568ad4a33SUladzislau Rezki (Sony) }
77668ad4a33SUladzislau Rezki (Sony) 
77768ad4a33SUladzislau Rezki (Sony) /*
77868ad4a33SUladzislau Rezki (Sony)  * Gets called when remove the node and rotate.
77968ad4a33SUladzislau Rezki (Sony)  */
78068ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
78168ad4a33SUladzislau Rezki (Sony) compute_subtree_max_size(struct vmap_area *va)
78268ad4a33SUladzislau Rezki (Sony) {
78368ad4a33SUladzislau Rezki (Sony) 	return max3(va_size(va),
78468ad4a33SUladzislau Rezki (Sony) 		get_subtree_max_size(va->rb_node.rb_left),
78568ad4a33SUladzislau Rezki (Sony) 		get_subtree_max_size(va->rb_node.rb_right));
78668ad4a33SUladzislau Rezki (Sony) }
78768ad4a33SUladzislau Rezki (Sony) 
788315cc066SMichel Lespinasse RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb,
789315cc066SMichel Lespinasse 	struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size)
79068ad4a33SUladzislau Rezki (Sony) 
79168ad4a33SUladzislau Rezki (Sony) static void purge_vmap_area_lazy(void);
79268ad4a33SUladzislau Rezki (Sony) static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
79368ad4a33SUladzislau Rezki (Sony) static unsigned long lazy_max_pages(void);
794db64fe02SNick Piggin 
79597105f0aSRoman Gushchin static atomic_long_t nr_vmalloc_pages;
79697105f0aSRoman Gushchin 
79797105f0aSRoman Gushchin unsigned long vmalloc_nr_pages(void)
79897105f0aSRoman Gushchin {
79997105f0aSRoman Gushchin 	return atomic_long_read(&nr_vmalloc_pages);
80097105f0aSRoman Gushchin }
80197105f0aSRoman Gushchin 
802db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr)
8031da177e4SLinus Torvalds {
804db64fe02SNick Piggin 	struct rb_node *n = vmap_area_root.rb_node;
805db64fe02SNick Piggin 
806db64fe02SNick Piggin 	while (n) {
807db64fe02SNick Piggin 		struct vmap_area *va;
808db64fe02SNick Piggin 
809db64fe02SNick Piggin 		va = rb_entry(n, struct vmap_area, rb_node);
810db64fe02SNick Piggin 		if (addr < va->va_start)
811db64fe02SNick Piggin 			n = n->rb_left;
812cef2ac3fSHATAYAMA Daisuke 		else if (addr >= va->va_end)
813db64fe02SNick Piggin 			n = n->rb_right;
814db64fe02SNick Piggin 		else
815db64fe02SNick Piggin 			return va;
816db64fe02SNick Piggin 	}
817db64fe02SNick Piggin 
818db64fe02SNick Piggin 	return NULL;
819db64fe02SNick Piggin }
820db64fe02SNick Piggin 
82168ad4a33SUladzislau Rezki (Sony) /*
82268ad4a33SUladzislau Rezki (Sony)  * This function returns back addresses of parent node
82368ad4a33SUladzislau Rezki (Sony)  * and its left or right link for further processing.
8249c801f61SUladzislau Rezki (Sony)  *
8259c801f61SUladzislau Rezki (Sony)  * Otherwise NULL is returned. In that case all further
8269c801f61SUladzislau Rezki (Sony)  * steps regarding inserting of conflicting overlap range
8279c801f61SUladzislau Rezki (Sony)  * have to be declined and actually considered as a bug.
82868ad4a33SUladzislau Rezki (Sony)  */
82968ad4a33SUladzislau Rezki (Sony) static __always_inline struct rb_node **
83068ad4a33SUladzislau Rezki (Sony) find_va_links(struct vmap_area *va,
83168ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct rb_node *from,
83268ad4a33SUladzislau Rezki (Sony) 	struct rb_node **parent)
833db64fe02SNick Piggin {
834170168d0SNamhyung Kim 	struct vmap_area *tmp_va;
83568ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
836db64fe02SNick Piggin 
83768ad4a33SUladzislau Rezki (Sony) 	if (root) {
83868ad4a33SUladzislau Rezki (Sony) 		link = &root->rb_node;
83968ad4a33SUladzislau Rezki (Sony) 		if (unlikely(!*link)) {
84068ad4a33SUladzislau Rezki (Sony) 			*parent = NULL;
84168ad4a33SUladzislau Rezki (Sony) 			return link;
84268ad4a33SUladzislau Rezki (Sony) 		}
84368ad4a33SUladzislau Rezki (Sony) 	} else {
84468ad4a33SUladzislau Rezki (Sony) 		link = &from;
84568ad4a33SUladzislau Rezki (Sony) 	}
84668ad4a33SUladzislau Rezki (Sony) 
84768ad4a33SUladzislau Rezki (Sony) 	/*
84868ad4a33SUladzislau Rezki (Sony) 	 * Go to the bottom of the tree. When we hit the last point
84968ad4a33SUladzislau Rezki (Sony) 	 * we end up with parent rb_node and correct direction, i name
85068ad4a33SUladzislau Rezki (Sony) 	 * it link, where the new va->rb_node will be attached to.
85168ad4a33SUladzislau Rezki (Sony) 	 */
85268ad4a33SUladzislau Rezki (Sony) 	do {
85368ad4a33SUladzislau Rezki (Sony) 		tmp_va = rb_entry(*link, struct vmap_area, rb_node);
85468ad4a33SUladzislau Rezki (Sony) 
85568ad4a33SUladzislau Rezki (Sony) 		/*
85668ad4a33SUladzislau Rezki (Sony) 		 * During the traversal we also do some sanity check.
85768ad4a33SUladzislau Rezki (Sony) 		 * Trigger the BUG() if there are sides(left/right)
85868ad4a33SUladzislau Rezki (Sony) 		 * or full overlaps.
85968ad4a33SUladzislau Rezki (Sony) 		 */
86068ad4a33SUladzislau Rezki (Sony) 		if (va->va_start < tmp_va->va_end &&
86168ad4a33SUladzislau Rezki (Sony) 				va->va_end <= tmp_va->va_start)
86268ad4a33SUladzislau Rezki (Sony) 			link = &(*link)->rb_left;
86368ad4a33SUladzislau Rezki (Sony) 		else if (va->va_end > tmp_va->va_start &&
86468ad4a33SUladzislau Rezki (Sony) 				va->va_start >= tmp_va->va_end)
86568ad4a33SUladzislau Rezki (Sony) 			link = &(*link)->rb_right;
8669c801f61SUladzislau Rezki (Sony) 		else {
8679c801f61SUladzislau Rezki (Sony) 			WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n",
8689c801f61SUladzislau Rezki (Sony) 				va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end);
8699c801f61SUladzislau Rezki (Sony) 
8709c801f61SUladzislau Rezki (Sony) 			return NULL;
8719c801f61SUladzislau Rezki (Sony) 		}
87268ad4a33SUladzislau Rezki (Sony) 	} while (*link);
87368ad4a33SUladzislau Rezki (Sony) 
87468ad4a33SUladzislau Rezki (Sony) 	*parent = &tmp_va->rb_node;
87568ad4a33SUladzislau Rezki (Sony) 	return link;
876db64fe02SNick Piggin }
877db64fe02SNick Piggin 
87868ad4a33SUladzislau Rezki (Sony) static __always_inline struct list_head *
87968ad4a33SUladzislau Rezki (Sony) get_va_next_sibling(struct rb_node *parent, struct rb_node **link)
88068ad4a33SUladzislau Rezki (Sony) {
88168ad4a33SUladzislau Rezki (Sony) 	struct list_head *list;
882db64fe02SNick Piggin 
88368ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!parent))
88468ad4a33SUladzislau Rezki (Sony) 		/*
88568ad4a33SUladzislau Rezki (Sony) 		 * The red-black tree where we try to find VA neighbors
88668ad4a33SUladzislau Rezki (Sony) 		 * before merging or inserting is empty, i.e. it means
88768ad4a33SUladzislau Rezki (Sony) 		 * there is no free vmap space. Normally it does not
88868ad4a33SUladzislau Rezki (Sony) 		 * happen but we handle this case anyway.
88968ad4a33SUladzislau Rezki (Sony) 		 */
89068ad4a33SUladzislau Rezki (Sony) 		return NULL;
89168ad4a33SUladzislau Rezki (Sony) 
89268ad4a33SUladzislau Rezki (Sony) 	list = &rb_entry(parent, struct vmap_area, rb_node)->list;
89368ad4a33SUladzislau Rezki (Sony) 	return (&parent->rb_right == link ? list->next : list);
894db64fe02SNick Piggin }
895db64fe02SNick Piggin 
89668ad4a33SUladzislau Rezki (Sony) static __always_inline void
89768ad4a33SUladzislau Rezki (Sony) link_va(struct vmap_area *va, struct rb_root *root,
89868ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent, struct rb_node **link, struct list_head *head)
89968ad4a33SUladzislau Rezki (Sony) {
90068ad4a33SUladzislau Rezki (Sony) 	/*
90168ad4a33SUladzislau Rezki (Sony) 	 * VA is still not in the list, but we can
90268ad4a33SUladzislau Rezki (Sony) 	 * identify its future previous list_head node.
90368ad4a33SUladzislau Rezki (Sony) 	 */
90468ad4a33SUladzislau Rezki (Sony) 	if (likely(parent)) {
90568ad4a33SUladzislau Rezki (Sony) 		head = &rb_entry(parent, struct vmap_area, rb_node)->list;
90668ad4a33SUladzislau Rezki (Sony) 		if (&parent->rb_right != link)
90768ad4a33SUladzislau Rezki (Sony) 			head = head->prev;
90868ad4a33SUladzislau Rezki (Sony) 	}
909db64fe02SNick Piggin 
91068ad4a33SUladzislau Rezki (Sony) 	/* Insert to the rb-tree */
91168ad4a33SUladzislau Rezki (Sony) 	rb_link_node(&va->rb_node, parent, link);
91268ad4a33SUladzislau Rezki (Sony) 	if (root == &free_vmap_area_root) {
91368ad4a33SUladzislau Rezki (Sony) 		/*
91468ad4a33SUladzislau Rezki (Sony) 		 * Some explanation here. Just perform simple insertion
91568ad4a33SUladzislau Rezki (Sony) 		 * to the tree. We do not set va->subtree_max_size to
91668ad4a33SUladzislau Rezki (Sony) 		 * its current size before calling rb_insert_augmented().
91768ad4a33SUladzislau Rezki (Sony) 		 * It is because of we populate the tree from the bottom
91868ad4a33SUladzislau Rezki (Sony) 		 * to parent levels when the node _is_ in the tree.
91968ad4a33SUladzislau Rezki (Sony) 		 *
92068ad4a33SUladzislau Rezki (Sony) 		 * Therefore we set subtree_max_size to zero after insertion,
92168ad4a33SUladzislau Rezki (Sony) 		 * to let __augment_tree_propagate_from() puts everything to
92268ad4a33SUladzislau Rezki (Sony) 		 * the correct order later on.
92368ad4a33SUladzislau Rezki (Sony) 		 */
92468ad4a33SUladzislau Rezki (Sony) 		rb_insert_augmented(&va->rb_node,
92568ad4a33SUladzislau Rezki (Sony) 			root, &free_vmap_area_rb_augment_cb);
92668ad4a33SUladzislau Rezki (Sony) 		va->subtree_max_size = 0;
92768ad4a33SUladzislau Rezki (Sony) 	} else {
92868ad4a33SUladzislau Rezki (Sony) 		rb_insert_color(&va->rb_node, root);
92968ad4a33SUladzislau Rezki (Sony) 	}
93068ad4a33SUladzislau Rezki (Sony) 
93168ad4a33SUladzislau Rezki (Sony) 	/* Address-sort this list */
93268ad4a33SUladzislau Rezki (Sony) 	list_add(&va->list, head);
93368ad4a33SUladzislau Rezki (Sony) }
93468ad4a33SUladzislau Rezki (Sony) 
93568ad4a33SUladzislau Rezki (Sony) static __always_inline void
93668ad4a33SUladzislau Rezki (Sony) unlink_va(struct vmap_area *va, struct rb_root *root)
93768ad4a33SUladzislau Rezki (Sony) {
938460e42d1SUladzislau Rezki (Sony) 	if (WARN_ON(RB_EMPTY_NODE(&va->rb_node)))
939460e42d1SUladzislau Rezki (Sony) 		return;
940460e42d1SUladzislau Rezki (Sony) 
94168ad4a33SUladzislau Rezki (Sony) 	if (root == &free_vmap_area_root)
94268ad4a33SUladzislau Rezki (Sony) 		rb_erase_augmented(&va->rb_node,
94368ad4a33SUladzislau Rezki (Sony) 			root, &free_vmap_area_rb_augment_cb);
94468ad4a33SUladzislau Rezki (Sony) 	else
94568ad4a33SUladzislau Rezki (Sony) 		rb_erase(&va->rb_node, root);
94668ad4a33SUladzislau Rezki (Sony) 
94768ad4a33SUladzislau Rezki (Sony) 	list_del(&va->list);
94868ad4a33SUladzislau Rezki (Sony) 	RB_CLEAR_NODE(&va->rb_node);
94968ad4a33SUladzislau Rezki (Sony) }
95068ad4a33SUladzislau Rezki (Sony) 
951bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK
952bb850f4dSUladzislau Rezki (Sony) static void
953da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(void)
954bb850f4dSUladzislau Rezki (Sony) {
955bb850f4dSUladzislau Rezki (Sony) 	struct vmap_area *va;
956da27c9edSUladzislau Rezki (Sony) 	unsigned long computed_size;
957bb850f4dSUladzislau Rezki (Sony) 
958da27c9edSUladzislau Rezki (Sony) 	list_for_each_entry(va, &free_vmap_area_list, list) {
959da27c9edSUladzislau Rezki (Sony) 		computed_size = compute_subtree_max_size(va);
960da27c9edSUladzislau Rezki (Sony) 		if (computed_size != va->subtree_max_size)
961bb850f4dSUladzislau Rezki (Sony) 			pr_emerg("tree is corrupted: %lu, %lu\n",
962bb850f4dSUladzislau Rezki (Sony) 				va_size(va), va->subtree_max_size);
963bb850f4dSUladzislau Rezki (Sony) 	}
964bb850f4dSUladzislau Rezki (Sony) }
965bb850f4dSUladzislau Rezki (Sony) #endif
966bb850f4dSUladzislau Rezki (Sony) 
96768ad4a33SUladzislau Rezki (Sony) /*
96868ad4a33SUladzislau Rezki (Sony)  * This function populates subtree_max_size from bottom to upper
96968ad4a33SUladzislau Rezki (Sony)  * levels starting from VA point. The propagation must be done
97068ad4a33SUladzislau Rezki (Sony)  * when VA size is modified by changing its va_start/va_end. Or
97168ad4a33SUladzislau Rezki (Sony)  * in case of newly inserting of VA to the tree.
97268ad4a33SUladzislau Rezki (Sony)  *
97368ad4a33SUladzislau Rezki (Sony)  * It means that __augment_tree_propagate_from() must be called:
97468ad4a33SUladzislau Rezki (Sony)  * - After VA has been inserted to the tree(free path);
97568ad4a33SUladzislau Rezki (Sony)  * - After VA has been shrunk(allocation path);
97668ad4a33SUladzislau Rezki (Sony)  * - After VA has been increased(merging path).
97768ad4a33SUladzislau Rezki (Sony)  *
97868ad4a33SUladzislau Rezki (Sony)  * Please note that, it does not mean that upper parent nodes
97968ad4a33SUladzislau Rezki (Sony)  * and their subtree_max_size are recalculated all the time up
98068ad4a33SUladzislau Rezki (Sony)  * to the root node.
98168ad4a33SUladzislau Rezki (Sony)  *
98268ad4a33SUladzislau Rezki (Sony)  *       4--8
98368ad4a33SUladzislau Rezki (Sony)  *        /\
98468ad4a33SUladzislau Rezki (Sony)  *       /  \
98568ad4a33SUladzislau Rezki (Sony)  *      /    \
98668ad4a33SUladzislau Rezki (Sony)  *    2--2  8--8
98768ad4a33SUladzislau Rezki (Sony)  *
98868ad4a33SUladzislau Rezki (Sony)  * For example if we modify the node 4, shrinking it to 2, then
98968ad4a33SUladzislau Rezki (Sony)  * no any modification is required. If we shrink the node 2 to 1
99068ad4a33SUladzislau Rezki (Sony)  * its subtree_max_size is updated only, and set to 1. If we shrink
99168ad4a33SUladzislau Rezki (Sony)  * the node 8 to 6, then its subtree_max_size is set to 6 and parent
99268ad4a33SUladzislau Rezki (Sony)  * node becomes 4--6.
99368ad4a33SUladzislau Rezki (Sony)  */
99468ad4a33SUladzislau Rezki (Sony) static __always_inline void
99568ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(struct vmap_area *va)
99668ad4a33SUladzislau Rezki (Sony) {
99768ad4a33SUladzislau Rezki (Sony) 	/*
99815ae144fSUladzislau Rezki (Sony) 	 * Populate the tree from bottom towards the root until
99915ae144fSUladzislau Rezki (Sony) 	 * the calculated maximum available size of checked node
100015ae144fSUladzislau Rezki (Sony) 	 * is equal to its current one.
100168ad4a33SUladzislau Rezki (Sony) 	 */
100215ae144fSUladzislau Rezki (Sony) 	free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL);
1003bb850f4dSUladzislau Rezki (Sony) 
1004bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK
1005da27c9edSUladzislau Rezki (Sony) 	augment_tree_propagate_check();
1006bb850f4dSUladzislau Rezki (Sony) #endif
100768ad4a33SUladzislau Rezki (Sony) }
100868ad4a33SUladzislau Rezki (Sony) 
100968ad4a33SUladzislau Rezki (Sony) static void
101068ad4a33SUladzislau Rezki (Sony) insert_vmap_area(struct vmap_area *va,
101168ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
101268ad4a33SUladzislau Rezki (Sony) {
101368ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
101468ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
101568ad4a33SUladzislau Rezki (Sony) 
101668ad4a33SUladzislau Rezki (Sony) 	link = find_va_links(va, root, NULL, &parent);
10179c801f61SUladzislau Rezki (Sony) 	if (link)
101868ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
101968ad4a33SUladzislau Rezki (Sony) }
102068ad4a33SUladzislau Rezki (Sony) 
102168ad4a33SUladzislau Rezki (Sony) static void
102268ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(struct vmap_area *va,
102368ad4a33SUladzislau Rezki (Sony) 	struct rb_node *from, struct rb_root *root,
102468ad4a33SUladzislau Rezki (Sony) 	struct list_head *head)
102568ad4a33SUladzislau Rezki (Sony) {
102668ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
102768ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
102868ad4a33SUladzislau Rezki (Sony) 
102968ad4a33SUladzislau Rezki (Sony) 	if (from)
103068ad4a33SUladzislau Rezki (Sony) 		link = find_va_links(va, NULL, from, &parent);
103168ad4a33SUladzislau Rezki (Sony) 	else
103268ad4a33SUladzislau Rezki (Sony) 		link = find_va_links(va, root, NULL, &parent);
103368ad4a33SUladzislau Rezki (Sony) 
10349c801f61SUladzislau Rezki (Sony) 	if (link) {
103568ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
103668ad4a33SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
103768ad4a33SUladzislau Rezki (Sony) 	}
10389c801f61SUladzislau Rezki (Sony) }
103968ad4a33SUladzislau Rezki (Sony) 
104068ad4a33SUladzislau Rezki (Sony) /*
104168ad4a33SUladzislau Rezki (Sony)  * Merge de-allocated chunk of VA memory with previous
104268ad4a33SUladzislau Rezki (Sony)  * and next free blocks. If coalesce is not done a new
104368ad4a33SUladzislau Rezki (Sony)  * free area is inserted. If VA has been merged, it is
104468ad4a33SUladzislau Rezki (Sony)  * freed.
10459c801f61SUladzislau Rezki (Sony)  *
10469c801f61SUladzislau Rezki (Sony)  * Please note, it can return NULL in case of overlap
10479c801f61SUladzislau Rezki (Sony)  * ranges, followed by WARN() report. Despite it is a
10489c801f61SUladzislau Rezki (Sony)  * buggy behaviour, a system can be alive and keep
10499c801f61SUladzislau Rezki (Sony)  * ongoing.
105068ad4a33SUladzislau Rezki (Sony)  */
10513c5c3cfbSDaniel Axtens static __always_inline struct vmap_area *
105268ad4a33SUladzislau Rezki (Sony) merge_or_add_vmap_area(struct vmap_area *va,
105368ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
105468ad4a33SUladzislau Rezki (Sony) {
105568ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *sibling;
105668ad4a33SUladzislau Rezki (Sony) 	struct list_head *next;
105768ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
105868ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
105968ad4a33SUladzislau Rezki (Sony) 	bool merged = false;
106068ad4a33SUladzislau Rezki (Sony) 
106168ad4a33SUladzislau Rezki (Sony) 	/*
106268ad4a33SUladzislau Rezki (Sony) 	 * Find a place in the tree where VA potentially will be
106368ad4a33SUladzislau Rezki (Sony) 	 * inserted, unless it is merged with its sibling/siblings.
106468ad4a33SUladzislau Rezki (Sony) 	 */
106568ad4a33SUladzislau Rezki (Sony) 	link = find_va_links(va, root, NULL, &parent);
10669c801f61SUladzislau Rezki (Sony) 	if (!link)
10679c801f61SUladzislau Rezki (Sony) 		return NULL;
106868ad4a33SUladzislau Rezki (Sony) 
106968ad4a33SUladzislau Rezki (Sony) 	/*
107068ad4a33SUladzislau Rezki (Sony) 	 * Get next node of VA to check if merging can be done.
107168ad4a33SUladzislau Rezki (Sony) 	 */
107268ad4a33SUladzislau Rezki (Sony) 	next = get_va_next_sibling(parent, link);
107368ad4a33SUladzislau Rezki (Sony) 	if (unlikely(next == NULL))
107468ad4a33SUladzislau Rezki (Sony) 		goto insert;
107568ad4a33SUladzislau Rezki (Sony) 
107668ad4a33SUladzislau Rezki (Sony) 	/*
107768ad4a33SUladzislau Rezki (Sony) 	 * start            end
107868ad4a33SUladzislau Rezki (Sony) 	 * |                |
107968ad4a33SUladzislau Rezki (Sony) 	 * |<------VA------>|<-----Next----->|
108068ad4a33SUladzislau Rezki (Sony) 	 *                  |                |
108168ad4a33SUladzislau Rezki (Sony) 	 *                  start            end
108268ad4a33SUladzislau Rezki (Sony) 	 */
108368ad4a33SUladzislau Rezki (Sony) 	if (next != head) {
108468ad4a33SUladzislau Rezki (Sony) 		sibling = list_entry(next, struct vmap_area, list);
108568ad4a33SUladzislau Rezki (Sony) 		if (sibling->va_start == va->va_end) {
108668ad4a33SUladzislau Rezki (Sony) 			sibling->va_start = va->va_start;
108768ad4a33SUladzislau Rezki (Sony) 
108868ad4a33SUladzislau Rezki (Sony) 			/* Free vmap_area object. */
108968ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, va);
109068ad4a33SUladzislau Rezki (Sony) 
109168ad4a33SUladzislau Rezki (Sony) 			/* Point to the new merged area. */
109268ad4a33SUladzislau Rezki (Sony) 			va = sibling;
109368ad4a33SUladzislau Rezki (Sony) 			merged = true;
109468ad4a33SUladzislau Rezki (Sony) 		}
109568ad4a33SUladzislau Rezki (Sony) 	}
109668ad4a33SUladzislau Rezki (Sony) 
109768ad4a33SUladzislau Rezki (Sony) 	/*
109868ad4a33SUladzislau Rezki (Sony) 	 * start            end
109968ad4a33SUladzislau Rezki (Sony) 	 * |                |
110068ad4a33SUladzislau Rezki (Sony) 	 * |<-----Prev----->|<------VA------>|
110168ad4a33SUladzislau Rezki (Sony) 	 *                  |                |
110268ad4a33SUladzislau Rezki (Sony) 	 *                  start            end
110368ad4a33SUladzislau Rezki (Sony) 	 */
110468ad4a33SUladzislau Rezki (Sony) 	if (next->prev != head) {
110568ad4a33SUladzislau Rezki (Sony) 		sibling = list_entry(next->prev, struct vmap_area, list);
110668ad4a33SUladzislau Rezki (Sony) 		if (sibling->va_end == va->va_start) {
11075dd78640SUladzislau Rezki (Sony) 			/*
11085dd78640SUladzislau Rezki (Sony) 			 * If both neighbors are coalesced, it is important
11095dd78640SUladzislau Rezki (Sony) 			 * to unlink the "next" node first, followed by merging
11105dd78640SUladzislau Rezki (Sony) 			 * with "previous" one. Otherwise the tree might not be
11115dd78640SUladzislau Rezki (Sony) 			 * fully populated if a sibling's augmented value is
11125dd78640SUladzislau Rezki (Sony) 			 * "normalized" because of rotation operations.
11135dd78640SUladzislau Rezki (Sony) 			 */
111454f63d9dSUladzislau Rezki (Sony) 			if (merged)
111568ad4a33SUladzislau Rezki (Sony) 				unlink_va(va, root);
111668ad4a33SUladzislau Rezki (Sony) 
11175dd78640SUladzislau Rezki (Sony) 			sibling->va_end = va->va_end;
11185dd78640SUladzislau Rezki (Sony) 
111968ad4a33SUladzislau Rezki (Sony) 			/* Free vmap_area object. */
112068ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, va);
11213c5c3cfbSDaniel Axtens 
11223c5c3cfbSDaniel Axtens 			/* Point to the new merged area. */
11233c5c3cfbSDaniel Axtens 			va = sibling;
11243c5c3cfbSDaniel Axtens 			merged = true;
112568ad4a33SUladzislau Rezki (Sony) 		}
112668ad4a33SUladzislau Rezki (Sony) 	}
112768ad4a33SUladzislau Rezki (Sony) 
112868ad4a33SUladzislau Rezki (Sony) insert:
11295dd78640SUladzislau Rezki (Sony) 	if (!merged)
113068ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
11313c5c3cfbSDaniel Axtens 
113296e2db45SUladzislau Rezki (Sony) 	return va;
113396e2db45SUladzislau Rezki (Sony) }
113496e2db45SUladzislau Rezki (Sony) 
113596e2db45SUladzislau Rezki (Sony) static __always_inline struct vmap_area *
113696e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area_augment(struct vmap_area *va,
113796e2db45SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
113896e2db45SUladzislau Rezki (Sony) {
113996e2db45SUladzislau Rezki (Sony) 	va = merge_or_add_vmap_area(va, root, head);
114096e2db45SUladzislau Rezki (Sony) 	if (va)
11415dd78640SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
114296e2db45SUladzislau Rezki (Sony) 
11433c5c3cfbSDaniel Axtens 	return va;
114468ad4a33SUladzislau Rezki (Sony) }
114568ad4a33SUladzislau Rezki (Sony) 
114668ad4a33SUladzislau Rezki (Sony) static __always_inline bool
114768ad4a33SUladzislau Rezki (Sony) is_within_this_va(struct vmap_area *va, unsigned long size,
114868ad4a33SUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
114968ad4a33SUladzislau Rezki (Sony) {
115068ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr;
115168ad4a33SUladzislau Rezki (Sony) 
115268ad4a33SUladzislau Rezki (Sony) 	if (va->va_start > vstart)
115368ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(va->va_start, align);
115468ad4a33SUladzislau Rezki (Sony) 	else
115568ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(vstart, align);
115668ad4a33SUladzislau Rezki (Sony) 
115768ad4a33SUladzislau Rezki (Sony) 	/* Can be overflowed due to big size or alignment. */
115868ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr + size < nva_start_addr ||
115968ad4a33SUladzislau Rezki (Sony) 			nva_start_addr < vstart)
116068ad4a33SUladzislau Rezki (Sony) 		return false;
116168ad4a33SUladzislau Rezki (Sony) 
116268ad4a33SUladzislau Rezki (Sony) 	return (nva_start_addr + size <= va->va_end);
116368ad4a33SUladzislau Rezki (Sony) }
116468ad4a33SUladzislau Rezki (Sony) 
116568ad4a33SUladzislau Rezki (Sony) /*
116668ad4a33SUladzislau Rezki (Sony)  * Find the first free block(lowest start address) in the tree,
116768ad4a33SUladzislau Rezki (Sony)  * that will accomplish the request corresponding to passing
116868ad4a33SUladzislau Rezki (Sony)  * parameters.
116968ad4a33SUladzislau Rezki (Sony)  */
117068ad4a33SUladzislau Rezki (Sony) static __always_inline struct vmap_area *
117168ad4a33SUladzislau Rezki (Sony) find_vmap_lowest_match(unsigned long size,
117268ad4a33SUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
117368ad4a33SUladzislau Rezki (Sony) {
117468ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
117568ad4a33SUladzislau Rezki (Sony) 	struct rb_node *node;
117668ad4a33SUladzislau Rezki (Sony) 	unsigned long length;
117768ad4a33SUladzislau Rezki (Sony) 
117868ad4a33SUladzislau Rezki (Sony) 	/* Start from the root. */
117968ad4a33SUladzislau Rezki (Sony) 	node = free_vmap_area_root.rb_node;
118068ad4a33SUladzislau Rezki (Sony) 
118168ad4a33SUladzislau Rezki (Sony) 	/* Adjust the search size for alignment overhead. */
118268ad4a33SUladzislau Rezki (Sony) 	length = size + align - 1;
118368ad4a33SUladzislau Rezki (Sony) 
118468ad4a33SUladzislau Rezki (Sony) 	while (node) {
118568ad4a33SUladzislau Rezki (Sony) 		va = rb_entry(node, struct vmap_area, rb_node);
118668ad4a33SUladzislau Rezki (Sony) 
118768ad4a33SUladzislau Rezki (Sony) 		if (get_subtree_max_size(node->rb_left) >= length &&
118868ad4a33SUladzislau Rezki (Sony) 				vstart < va->va_start) {
118968ad4a33SUladzislau Rezki (Sony) 			node = node->rb_left;
119068ad4a33SUladzislau Rezki (Sony) 		} else {
119168ad4a33SUladzislau Rezki (Sony) 			if (is_within_this_va(va, size, align, vstart))
119268ad4a33SUladzislau Rezki (Sony) 				return va;
119368ad4a33SUladzislau Rezki (Sony) 
119468ad4a33SUladzislau Rezki (Sony) 			/*
119568ad4a33SUladzislau Rezki (Sony) 			 * Does not make sense to go deeper towards the right
119668ad4a33SUladzislau Rezki (Sony) 			 * sub-tree if it does not have a free block that is
119768ad4a33SUladzislau Rezki (Sony) 			 * equal or bigger to the requested search length.
119868ad4a33SUladzislau Rezki (Sony) 			 */
119968ad4a33SUladzislau Rezki (Sony) 			if (get_subtree_max_size(node->rb_right) >= length) {
120068ad4a33SUladzislau Rezki (Sony) 				node = node->rb_right;
120168ad4a33SUladzislau Rezki (Sony) 				continue;
120268ad4a33SUladzislau Rezki (Sony) 			}
120368ad4a33SUladzislau Rezki (Sony) 
120468ad4a33SUladzislau Rezki (Sony) 			/*
12053806b041SAndrew Morton 			 * OK. We roll back and find the first right sub-tree,
120668ad4a33SUladzislau Rezki (Sony) 			 * that will satisfy the search criteria. It can happen
120768ad4a33SUladzislau Rezki (Sony) 			 * only once due to "vstart" restriction.
120868ad4a33SUladzislau Rezki (Sony) 			 */
120968ad4a33SUladzislau Rezki (Sony) 			while ((node = rb_parent(node))) {
121068ad4a33SUladzislau Rezki (Sony) 				va = rb_entry(node, struct vmap_area, rb_node);
121168ad4a33SUladzislau Rezki (Sony) 				if (is_within_this_va(va, size, align, vstart))
121268ad4a33SUladzislau Rezki (Sony) 					return va;
121368ad4a33SUladzislau Rezki (Sony) 
121468ad4a33SUladzislau Rezki (Sony) 				if (get_subtree_max_size(node->rb_right) >= length &&
121568ad4a33SUladzislau Rezki (Sony) 						vstart <= va->va_start) {
121668ad4a33SUladzislau Rezki (Sony) 					node = node->rb_right;
121768ad4a33SUladzislau Rezki (Sony) 					break;
121868ad4a33SUladzislau Rezki (Sony) 				}
121968ad4a33SUladzislau Rezki (Sony) 			}
122068ad4a33SUladzislau Rezki (Sony) 		}
122168ad4a33SUladzislau Rezki (Sony) 	}
122268ad4a33SUladzislau Rezki (Sony) 
122368ad4a33SUladzislau Rezki (Sony) 	return NULL;
122468ad4a33SUladzislau Rezki (Sony) }
122568ad4a33SUladzislau Rezki (Sony) 
1226a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1227a6cf4e0fSUladzislau Rezki (Sony) #include <linux/random.h>
1228a6cf4e0fSUladzislau Rezki (Sony) 
1229a6cf4e0fSUladzislau Rezki (Sony) static struct vmap_area *
1230a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_linear_match(unsigned long size,
1231a6cf4e0fSUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
1232a6cf4e0fSUladzislau Rezki (Sony) {
1233a6cf4e0fSUladzislau Rezki (Sony) 	struct vmap_area *va;
1234a6cf4e0fSUladzislau Rezki (Sony) 
1235a6cf4e0fSUladzislau Rezki (Sony) 	list_for_each_entry(va, &free_vmap_area_list, list) {
1236a6cf4e0fSUladzislau Rezki (Sony) 		if (!is_within_this_va(va, size, align, vstart))
1237a6cf4e0fSUladzislau Rezki (Sony) 			continue;
1238a6cf4e0fSUladzislau Rezki (Sony) 
1239a6cf4e0fSUladzislau Rezki (Sony) 		return va;
1240a6cf4e0fSUladzislau Rezki (Sony) 	}
1241a6cf4e0fSUladzislau Rezki (Sony) 
1242a6cf4e0fSUladzislau Rezki (Sony) 	return NULL;
1243a6cf4e0fSUladzislau Rezki (Sony) }
1244a6cf4e0fSUladzislau Rezki (Sony) 
1245a6cf4e0fSUladzislau Rezki (Sony) static void
1246a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_match_check(unsigned long size)
1247a6cf4e0fSUladzislau Rezki (Sony) {
1248a6cf4e0fSUladzislau Rezki (Sony) 	struct vmap_area *va_1, *va_2;
1249a6cf4e0fSUladzislau Rezki (Sony) 	unsigned long vstart;
1250a6cf4e0fSUladzislau Rezki (Sony) 	unsigned int rnd;
1251a6cf4e0fSUladzislau Rezki (Sony) 
1252a6cf4e0fSUladzislau Rezki (Sony) 	get_random_bytes(&rnd, sizeof(rnd));
1253a6cf4e0fSUladzislau Rezki (Sony) 	vstart = VMALLOC_START + rnd;
1254a6cf4e0fSUladzislau Rezki (Sony) 
1255a6cf4e0fSUladzislau Rezki (Sony) 	va_1 = find_vmap_lowest_match(size, 1, vstart);
1256a6cf4e0fSUladzislau Rezki (Sony) 	va_2 = find_vmap_lowest_linear_match(size, 1, vstart);
1257a6cf4e0fSUladzislau Rezki (Sony) 
1258a6cf4e0fSUladzislau Rezki (Sony) 	if (va_1 != va_2)
1259a6cf4e0fSUladzislau Rezki (Sony) 		pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n",
1260a6cf4e0fSUladzislau Rezki (Sony) 			va_1, va_2, vstart);
1261a6cf4e0fSUladzislau Rezki (Sony) }
1262a6cf4e0fSUladzislau Rezki (Sony) #endif
1263a6cf4e0fSUladzislau Rezki (Sony) 
126468ad4a33SUladzislau Rezki (Sony) enum fit_type {
126568ad4a33SUladzislau Rezki (Sony) 	NOTHING_FIT = 0,
126668ad4a33SUladzislau Rezki (Sony) 	FL_FIT_TYPE = 1,	/* full fit */
126768ad4a33SUladzislau Rezki (Sony) 	LE_FIT_TYPE = 2,	/* left edge fit */
126868ad4a33SUladzislau Rezki (Sony) 	RE_FIT_TYPE = 3,	/* right edge fit */
126968ad4a33SUladzislau Rezki (Sony) 	NE_FIT_TYPE = 4		/* no edge fit */
127068ad4a33SUladzislau Rezki (Sony) };
127168ad4a33SUladzislau Rezki (Sony) 
127268ad4a33SUladzislau Rezki (Sony) static __always_inline enum fit_type
127368ad4a33SUladzislau Rezki (Sony) classify_va_fit_type(struct vmap_area *va,
127468ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr, unsigned long size)
127568ad4a33SUladzislau Rezki (Sony) {
127668ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
127768ad4a33SUladzislau Rezki (Sony) 
127868ad4a33SUladzislau Rezki (Sony) 	/* Check if it is within VA. */
127968ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr < va->va_start ||
128068ad4a33SUladzislau Rezki (Sony) 			nva_start_addr + size > va->va_end)
128168ad4a33SUladzislau Rezki (Sony) 		return NOTHING_FIT;
128268ad4a33SUladzislau Rezki (Sony) 
128368ad4a33SUladzislau Rezki (Sony) 	/* Now classify. */
128468ad4a33SUladzislau Rezki (Sony) 	if (va->va_start == nva_start_addr) {
128568ad4a33SUladzislau Rezki (Sony) 		if (va->va_end == nva_start_addr + size)
128668ad4a33SUladzislau Rezki (Sony) 			type = FL_FIT_TYPE;
128768ad4a33SUladzislau Rezki (Sony) 		else
128868ad4a33SUladzislau Rezki (Sony) 			type = LE_FIT_TYPE;
128968ad4a33SUladzislau Rezki (Sony) 	} else if (va->va_end == nva_start_addr + size) {
129068ad4a33SUladzislau Rezki (Sony) 		type = RE_FIT_TYPE;
129168ad4a33SUladzislau Rezki (Sony) 	} else {
129268ad4a33SUladzislau Rezki (Sony) 		type = NE_FIT_TYPE;
129368ad4a33SUladzislau Rezki (Sony) 	}
129468ad4a33SUladzislau Rezki (Sony) 
129568ad4a33SUladzislau Rezki (Sony) 	return type;
129668ad4a33SUladzislau Rezki (Sony) }
129768ad4a33SUladzislau Rezki (Sony) 
129868ad4a33SUladzislau Rezki (Sony) static __always_inline int
129968ad4a33SUladzislau Rezki (Sony) adjust_va_to_fit_type(struct vmap_area *va,
130068ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr, unsigned long size,
130168ad4a33SUladzislau Rezki (Sony) 	enum fit_type type)
130268ad4a33SUladzislau Rezki (Sony) {
13032c929233SArnd Bergmann 	struct vmap_area *lva = NULL;
130468ad4a33SUladzislau Rezki (Sony) 
130568ad4a33SUladzislau Rezki (Sony) 	if (type == FL_FIT_TYPE) {
130668ad4a33SUladzislau Rezki (Sony) 		/*
130768ad4a33SUladzislau Rezki (Sony) 		 * No need to split VA, it fully fits.
130868ad4a33SUladzislau Rezki (Sony) 		 *
130968ad4a33SUladzislau Rezki (Sony) 		 * |               |
131068ad4a33SUladzislau Rezki (Sony) 		 * V      NVA      V
131168ad4a33SUladzislau Rezki (Sony) 		 * |---------------|
131268ad4a33SUladzislau Rezki (Sony) 		 */
131368ad4a33SUladzislau Rezki (Sony) 		unlink_va(va, &free_vmap_area_root);
131468ad4a33SUladzislau Rezki (Sony) 		kmem_cache_free(vmap_area_cachep, va);
131568ad4a33SUladzislau Rezki (Sony) 	} else if (type == LE_FIT_TYPE) {
131668ad4a33SUladzislau Rezki (Sony) 		/*
131768ad4a33SUladzislau Rezki (Sony) 		 * Split left edge of fit VA.
131868ad4a33SUladzislau Rezki (Sony) 		 *
131968ad4a33SUladzislau Rezki (Sony) 		 * |       |
132068ad4a33SUladzislau Rezki (Sony) 		 * V  NVA  V   R
132168ad4a33SUladzislau Rezki (Sony) 		 * |-------|-------|
132268ad4a33SUladzislau Rezki (Sony) 		 */
132368ad4a33SUladzislau Rezki (Sony) 		va->va_start += size;
132468ad4a33SUladzislau Rezki (Sony) 	} else if (type == RE_FIT_TYPE) {
132568ad4a33SUladzislau Rezki (Sony) 		/*
132668ad4a33SUladzislau Rezki (Sony) 		 * Split right edge of fit VA.
132768ad4a33SUladzislau Rezki (Sony) 		 *
132868ad4a33SUladzislau Rezki (Sony) 		 *         |       |
132968ad4a33SUladzislau Rezki (Sony) 		 *     L   V  NVA  V
133068ad4a33SUladzislau Rezki (Sony) 		 * |-------|-------|
133168ad4a33SUladzislau Rezki (Sony) 		 */
133268ad4a33SUladzislau Rezki (Sony) 		va->va_end = nva_start_addr;
133368ad4a33SUladzislau Rezki (Sony) 	} else if (type == NE_FIT_TYPE) {
133468ad4a33SUladzislau Rezki (Sony) 		/*
133568ad4a33SUladzislau Rezki (Sony) 		 * Split no edge of fit VA.
133668ad4a33SUladzislau Rezki (Sony) 		 *
133768ad4a33SUladzislau Rezki (Sony) 		 *     |       |
133868ad4a33SUladzislau Rezki (Sony) 		 *   L V  NVA  V R
133968ad4a33SUladzislau Rezki (Sony) 		 * |---|-------|---|
134068ad4a33SUladzislau Rezki (Sony) 		 */
134182dd23e8SUladzislau Rezki (Sony) 		lva = __this_cpu_xchg(ne_fit_preload_node, NULL);
134282dd23e8SUladzislau Rezki (Sony) 		if (unlikely(!lva)) {
134382dd23e8SUladzislau Rezki (Sony) 			/*
134482dd23e8SUladzislau Rezki (Sony) 			 * For percpu allocator we do not do any pre-allocation
134582dd23e8SUladzislau Rezki (Sony) 			 * and leave it as it is. The reason is it most likely
134682dd23e8SUladzislau Rezki (Sony) 			 * never ends up with NE_FIT_TYPE splitting. In case of
134782dd23e8SUladzislau Rezki (Sony) 			 * percpu allocations offsets and sizes are aligned to
134882dd23e8SUladzislau Rezki (Sony) 			 * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE
134982dd23e8SUladzislau Rezki (Sony) 			 * are its main fitting cases.
135082dd23e8SUladzislau Rezki (Sony) 			 *
135182dd23e8SUladzislau Rezki (Sony) 			 * There are a few exceptions though, as an example it is
135282dd23e8SUladzislau Rezki (Sony) 			 * a first allocation (early boot up) when we have "one"
135382dd23e8SUladzislau Rezki (Sony) 			 * big free space that has to be split.
1354060650a2SUladzislau Rezki (Sony) 			 *
1355060650a2SUladzislau Rezki (Sony) 			 * Also we can hit this path in case of regular "vmap"
1356060650a2SUladzislau Rezki (Sony) 			 * allocations, if "this" current CPU was not preloaded.
1357060650a2SUladzislau Rezki (Sony) 			 * See the comment in alloc_vmap_area() why. If so, then
1358060650a2SUladzislau Rezki (Sony) 			 * GFP_NOWAIT is used instead to get an extra object for
1359060650a2SUladzislau Rezki (Sony) 			 * split purpose. That is rare and most time does not
1360060650a2SUladzislau Rezki (Sony) 			 * occur.
1361060650a2SUladzislau Rezki (Sony) 			 *
1362060650a2SUladzislau Rezki (Sony) 			 * What happens if an allocation gets failed. Basically,
1363060650a2SUladzislau Rezki (Sony) 			 * an "overflow" path is triggered to purge lazily freed
1364060650a2SUladzislau Rezki (Sony) 			 * areas to free some memory, then, the "retry" path is
1365060650a2SUladzislau Rezki (Sony) 			 * triggered to repeat one more time. See more details
1366060650a2SUladzislau Rezki (Sony) 			 * in alloc_vmap_area() function.
136782dd23e8SUladzislau Rezki (Sony) 			 */
136868ad4a33SUladzislau Rezki (Sony) 			lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
136982dd23e8SUladzislau Rezki (Sony) 			if (!lva)
137068ad4a33SUladzislau Rezki (Sony) 				return -1;
137182dd23e8SUladzislau Rezki (Sony) 		}
137268ad4a33SUladzislau Rezki (Sony) 
137368ad4a33SUladzislau Rezki (Sony) 		/*
137468ad4a33SUladzislau Rezki (Sony) 		 * Build the remainder.
137568ad4a33SUladzislau Rezki (Sony) 		 */
137668ad4a33SUladzislau Rezki (Sony) 		lva->va_start = va->va_start;
137768ad4a33SUladzislau Rezki (Sony) 		lva->va_end = nva_start_addr;
137868ad4a33SUladzislau Rezki (Sony) 
137968ad4a33SUladzislau Rezki (Sony) 		/*
138068ad4a33SUladzislau Rezki (Sony) 		 * Shrink this VA to remaining size.
138168ad4a33SUladzislau Rezki (Sony) 		 */
138268ad4a33SUladzislau Rezki (Sony) 		va->va_start = nva_start_addr + size;
138368ad4a33SUladzislau Rezki (Sony) 	} else {
138468ad4a33SUladzislau Rezki (Sony) 		return -1;
138568ad4a33SUladzislau Rezki (Sony) 	}
138668ad4a33SUladzislau Rezki (Sony) 
138768ad4a33SUladzislau Rezki (Sony) 	if (type != FL_FIT_TYPE) {
138868ad4a33SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
138968ad4a33SUladzislau Rezki (Sony) 
13902c929233SArnd Bergmann 		if (lva)	/* type == NE_FIT_TYPE */
139168ad4a33SUladzislau Rezki (Sony) 			insert_vmap_area_augment(lva, &va->rb_node,
139268ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_root, &free_vmap_area_list);
139368ad4a33SUladzislau Rezki (Sony) 	}
139468ad4a33SUladzislau Rezki (Sony) 
139568ad4a33SUladzislau Rezki (Sony) 	return 0;
139668ad4a33SUladzislau Rezki (Sony) }
139768ad4a33SUladzislau Rezki (Sony) 
139868ad4a33SUladzislau Rezki (Sony) /*
139968ad4a33SUladzislau Rezki (Sony)  * Returns a start address of the newly allocated area, if success.
140068ad4a33SUladzislau Rezki (Sony)  * Otherwise a vend is returned that indicates failure.
140168ad4a33SUladzislau Rezki (Sony)  */
140268ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
140368ad4a33SUladzislau Rezki (Sony) __alloc_vmap_area(unsigned long size, unsigned long align,
1404cacca6baSUladzislau Rezki (Sony) 	unsigned long vstart, unsigned long vend)
140568ad4a33SUladzislau Rezki (Sony) {
140668ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr;
140768ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
140868ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
140968ad4a33SUladzislau Rezki (Sony) 	int ret;
141068ad4a33SUladzislau Rezki (Sony) 
141168ad4a33SUladzislau Rezki (Sony) 	va = find_vmap_lowest_match(size, align, vstart);
141268ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!va))
141368ad4a33SUladzislau Rezki (Sony) 		return vend;
141468ad4a33SUladzislau Rezki (Sony) 
141568ad4a33SUladzislau Rezki (Sony) 	if (va->va_start > vstart)
141668ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(va->va_start, align);
141768ad4a33SUladzislau Rezki (Sony) 	else
141868ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(vstart, align);
141968ad4a33SUladzislau Rezki (Sony) 
142068ad4a33SUladzislau Rezki (Sony) 	/* Check the "vend" restriction. */
142168ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr + size > vend)
142268ad4a33SUladzislau Rezki (Sony) 		return vend;
142368ad4a33SUladzislau Rezki (Sony) 
142468ad4a33SUladzislau Rezki (Sony) 	/* Classify what we have found. */
142568ad4a33SUladzislau Rezki (Sony) 	type = classify_va_fit_type(va, nva_start_addr, size);
142668ad4a33SUladzislau Rezki (Sony) 	if (WARN_ON_ONCE(type == NOTHING_FIT))
142768ad4a33SUladzislau Rezki (Sony) 		return vend;
142868ad4a33SUladzislau Rezki (Sony) 
142968ad4a33SUladzislau Rezki (Sony) 	/* Update the free vmap_area. */
143068ad4a33SUladzislau Rezki (Sony) 	ret = adjust_va_to_fit_type(va, nva_start_addr, size, type);
143168ad4a33SUladzislau Rezki (Sony) 	if (ret)
143268ad4a33SUladzislau Rezki (Sony) 		return vend;
143368ad4a33SUladzislau Rezki (Sony) 
1434a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1435a6cf4e0fSUladzislau Rezki (Sony) 	find_vmap_lowest_match_check(size);
1436a6cf4e0fSUladzislau Rezki (Sony) #endif
1437a6cf4e0fSUladzislau Rezki (Sony) 
143868ad4a33SUladzislau Rezki (Sony) 	return nva_start_addr;
143968ad4a33SUladzislau Rezki (Sony) }
14404da56b99SChris Wilson 
1441db64fe02SNick Piggin /*
1442d98c9e83SAndrey Ryabinin  * Free a region of KVA allocated by alloc_vmap_area
1443d98c9e83SAndrey Ryabinin  */
1444d98c9e83SAndrey Ryabinin static void free_vmap_area(struct vmap_area *va)
1445d98c9e83SAndrey Ryabinin {
1446d98c9e83SAndrey Ryabinin 	/*
1447d98c9e83SAndrey Ryabinin 	 * Remove from the busy tree/list.
1448d98c9e83SAndrey Ryabinin 	 */
1449d98c9e83SAndrey Ryabinin 	spin_lock(&vmap_area_lock);
1450d98c9e83SAndrey Ryabinin 	unlink_va(va, &vmap_area_root);
1451d98c9e83SAndrey Ryabinin 	spin_unlock(&vmap_area_lock);
1452d98c9e83SAndrey Ryabinin 
1453d98c9e83SAndrey Ryabinin 	/*
1454d98c9e83SAndrey Ryabinin 	 * Insert/Merge it back to the free tree/list.
1455d98c9e83SAndrey Ryabinin 	 */
1456d98c9e83SAndrey Ryabinin 	spin_lock(&free_vmap_area_lock);
145796e2db45SUladzislau Rezki (Sony) 	merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list);
1458d98c9e83SAndrey Ryabinin 	spin_unlock(&free_vmap_area_lock);
1459d98c9e83SAndrey Ryabinin }
1460d98c9e83SAndrey Ryabinin 
1461187f8cc4SUladzislau Rezki (Sony) static inline void
1462187f8cc4SUladzislau Rezki (Sony) preload_this_cpu_lock(spinlock_t *lock, gfp_t gfp_mask, int node)
1463187f8cc4SUladzislau Rezki (Sony) {
1464187f8cc4SUladzislau Rezki (Sony) 	struct vmap_area *va = NULL;
1465187f8cc4SUladzislau Rezki (Sony) 
1466187f8cc4SUladzislau Rezki (Sony) 	/*
1467187f8cc4SUladzislau Rezki (Sony) 	 * Preload this CPU with one extra vmap_area object. It is used
1468187f8cc4SUladzislau Rezki (Sony) 	 * when fit type of free area is NE_FIT_TYPE. It guarantees that
1469187f8cc4SUladzislau Rezki (Sony) 	 * a CPU that does an allocation is preloaded.
1470187f8cc4SUladzislau Rezki (Sony) 	 *
1471187f8cc4SUladzislau Rezki (Sony) 	 * We do it in non-atomic context, thus it allows us to use more
1472187f8cc4SUladzislau Rezki (Sony) 	 * permissive allocation masks to be more stable under low memory
1473187f8cc4SUladzislau Rezki (Sony) 	 * condition and high memory pressure.
1474187f8cc4SUladzislau Rezki (Sony) 	 */
1475187f8cc4SUladzislau Rezki (Sony) 	if (!this_cpu_read(ne_fit_preload_node))
1476187f8cc4SUladzislau Rezki (Sony) 		va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
1477187f8cc4SUladzislau Rezki (Sony) 
1478187f8cc4SUladzislau Rezki (Sony) 	spin_lock(lock);
1479187f8cc4SUladzislau Rezki (Sony) 
1480187f8cc4SUladzislau Rezki (Sony) 	if (va && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, va))
1481187f8cc4SUladzislau Rezki (Sony) 		kmem_cache_free(vmap_area_cachep, va);
1482187f8cc4SUladzislau Rezki (Sony) }
1483187f8cc4SUladzislau Rezki (Sony) 
1484d98c9e83SAndrey Ryabinin /*
1485db64fe02SNick Piggin  * Allocate a region of KVA of the specified size and alignment, within the
1486db64fe02SNick Piggin  * vstart and vend.
1487db64fe02SNick Piggin  */
1488db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size,
1489db64fe02SNick Piggin 				unsigned long align,
1490db64fe02SNick Piggin 				unsigned long vstart, unsigned long vend,
1491db64fe02SNick Piggin 				int node, gfp_t gfp_mask)
1492db64fe02SNick Piggin {
1493187f8cc4SUladzislau Rezki (Sony) 	struct vmap_area *va;
14941da177e4SLinus Torvalds 	unsigned long addr;
1495db64fe02SNick Piggin 	int purged = 0;
1496d98c9e83SAndrey Ryabinin 	int ret;
1497db64fe02SNick Piggin 
14987766970cSNick Piggin 	BUG_ON(!size);
1499891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
150089699605SNick Piggin 	BUG_ON(!is_power_of_2(align));
1501db64fe02SNick Piggin 
150268ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!vmap_initialized))
150368ad4a33SUladzislau Rezki (Sony) 		return ERR_PTR(-EBUSY);
150468ad4a33SUladzislau Rezki (Sony) 
15055803ed29SChristoph Hellwig 	might_sleep();
1506f07116d7SUladzislau Rezki (Sony) 	gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
15074da56b99SChris Wilson 
1508f07116d7SUladzislau Rezki (Sony) 	va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
1509db64fe02SNick Piggin 	if (unlikely(!va))
1510db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
1511db64fe02SNick Piggin 
15127f88f88fSCatalin Marinas 	/*
15137f88f88fSCatalin Marinas 	 * Only scan the relevant parts containing pointers to other objects
15147f88f88fSCatalin Marinas 	 * to avoid false negatives.
15157f88f88fSCatalin Marinas 	 */
1516f07116d7SUladzislau Rezki (Sony) 	kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask);
15177f88f88fSCatalin Marinas 
1518db64fe02SNick Piggin retry:
1519187f8cc4SUladzislau Rezki (Sony) 	preload_this_cpu_lock(&free_vmap_area_lock, gfp_mask, node);
1520187f8cc4SUladzislau Rezki (Sony) 	addr = __alloc_vmap_area(size, align, vstart, vend);
1521187f8cc4SUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
152268ad4a33SUladzislau Rezki (Sony) 
152389699605SNick Piggin 	/*
152468ad4a33SUladzislau Rezki (Sony) 	 * If an allocation fails, the "vend" address is
152568ad4a33SUladzislau Rezki (Sony) 	 * returned. Therefore trigger the overflow path.
152689699605SNick Piggin 	 */
152768ad4a33SUladzislau Rezki (Sony) 	if (unlikely(addr == vend))
152889699605SNick Piggin 		goto overflow;
152989699605SNick Piggin 
153089699605SNick Piggin 	va->va_start = addr;
153189699605SNick Piggin 	va->va_end = addr + size;
1532688fcbfcSPengfei Li 	va->vm = NULL;
153368ad4a33SUladzislau Rezki (Sony) 
1534e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
1535e36176beSUladzislau Rezki (Sony) 	insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
153689699605SNick Piggin 	spin_unlock(&vmap_area_lock);
153789699605SNick Piggin 
153861e16557SWang Xiaoqiang 	BUG_ON(!IS_ALIGNED(va->va_start, align));
153989699605SNick Piggin 	BUG_ON(va->va_start < vstart);
154089699605SNick Piggin 	BUG_ON(va->va_end > vend);
154189699605SNick Piggin 
1542d98c9e83SAndrey Ryabinin 	ret = kasan_populate_vmalloc(addr, size);
1543d98c9e83SAndrey Ryabinin 	if (ret) {
1544d98c9e83SAndrey Ryabinin 		free_vmap_area(va);
1545d98c9e83SAndrey Ryabinin 		return ERR_PTR(ret);
1546d98c9e83SAndrey Ryabinin 	}
1547d98c9e83SAndrey Ryabinin 
154889699605SNick Piggin 	return va;
154989699605SNick Piggin 
15507766970cSNick Piggin overflow:
1551db64fe02SNick Piggin 	if (!purged) {
1552db64fe02SNick Piggin 		purge_vmap_area_lazy();
1553db64fe02SNick Piggin 		purged = 1;
1554db64fe02SNick Piggin 		goto retry;
1555db64fe02SNick Piggin 	}
15564da56b99SChris Wilson 
15574da56b99SChris Wilson 	if (gfpflags_allow_blocking(gfp_mask)) {
15584da56b99SChris Wilson 		unsigned long freed = 0;
15594da56b99SChris Wilson 		blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
15604da56b99SChris Wilson 		if (freed > 0) {
15614da56b99SChris Wilson 			purged = 0;
15624da56b99SChris Wilson 			goto retry;
15634da56b99SChris Wilson 		}
15644da56b99SChris Wilson 	}
15654da56b99SChris Wilson 
156603497d76SFlorian Fainelli 	if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
1567756a025fSJoe Perches 		pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1568756a025fSJoe Perches 			size);
156968ad4a33SUladzislau Rezki (Sony) 
157068ad4a33SUladzislau Rezki (Sony) 	kmem_cache_free(vmap_area_cachep, va);
1571db64fe02SNick Piggin 	return ERR_PTR(-EBUSY);
1572db64fe02SNick Piggin }
1573db64fe02SNick Piggin 
15744da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb)
15754da56b99SChris Wilson {
15764da56b99SChris Wilson 	return blocking_notifier_chain_register(&vmap_notify_list, nb);
15774da56b99SChris Wilson }
15784da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
15794da56b99SChris Wilson 
15804da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb)
15814da56b99SChris Wilson {
15824da56b99SChris Wilson 	return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
15834da56b99SChris Wilson }
15844da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
15854da56b99SChris Wilson 
1586db64fe02SNick Piggin /*
1587db64fe02SNick Piggin  * lazy_max_pages is the maximum amount of virtual address space we gather up
1588db64fe02SNick Piggin  * before attempting to purge with a TLB flush.
1589db64fe02SNick Piggin  *
1590db64fe02SNick Piggin  * There is a tradeoff here: a larger number will cover more kernel page tables
1591db64fe02SNick Piggin  * and take slightly longer to purge, but it will linearly reduce the number of
1592db64fe02SNick Piggin  * global TLB flushes that must be performed. It would seem natural to scale
1593db64fe02SNick Piggin  * this number up linearly with the number of CPUs (because vmapping activity
1594db64fe02SNick Piggin  * could also scale linearly with the number of CPUs), however it is likely
1595db64fe02SNick Piggin  * that in practice, workloads might be constrained in other ways that mean
1596db64fe02SNick Piggin  * vmap activity will not scale linearly with CPUs. Also, I want to be
1597db64fe02SNick Piggin  * conservative and not introduce a big latency on huge systems, so go with
1598db64fe02SNick Piggin  * a less aggressive log scale. It will still be an improvement over the old
1599db64fe02SNick Piggin  * code, and it will be simple to change the scale factor if we find that it
1600db64fe02SNick Piggin  * becomes a problem on bigger systems.
1601db64fe02SNick Piggin  */
1602db64fe02SNick Piggin static unsigned long lazy_max_pages(void)
1603db64fe02SNick Piggin {
1604db64fe02SNick Piggin 	unsigned int log;
1605db64fe02SNick Piggin 
1606db64fe02SNick Piggin 	log = fls(num_online_cpus());
1607db64fe02SNick Piggin 
1608db64fe02SNick Piggin 	return log * (32UL * 1024 * 1024 / PAGE_SIZE);
1609db64fe02SNick Piggin }
1610db64fe02SNick Piggin 
16114d36e6f8SUladzislau Rezki (Sony) static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0);
1612db64fe02SNick Piggin 
16130574ecd1SChristoph Hellwig /*
1614f0953a1bSIngo Molnar  * Serialize vmap purging.  There is no actual critical section protected
16150574ecd1SChristoph Hellwig  * by this look, but we want to avoid concurrent calls for performance
16160574ecd1SChristoph Hellwig  * reasons and to make the pcpu_get_vm_areas more deterministic.
16170574ecd1SChristoph Hellwig  */
1618f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock);
16190574ecd1SChristoph Hellwig 
162002b709dfSNick Piggin /* for per-CPU blocks */
162102b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void);
162202b709dfSNick Piggin 
16235da96bddSMel Gorman #ifdef CONFIG_X86_64
1624db64fe02SNick Piggin /*
16253ee48b6aSCliff Wickman  * called before a call to iounmap() if the caller wants vm_area_struct's
16263ee48b6aSCliff Wickman  * immediately freed.
16273ee48b6aSCliff Wickman  */
16283ee48b6aSCliff Wickman void set_iounmap_nonlazy(void)
16293ee48b6aSCliff Wickman {
16304d36e6f8SUladzislau Rezki (Sony) 	atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1);
16313ee48b6aSCliff Wickman }
16325da96bddSMel Gorman #endif /* CONFIG_X86_64 */
16333ee48b6aSCliff Wickman 
16343ee48b6aSCliff Wickman /*
1635db64fe02SNick Piggin  * Purges all lazily-freed vmap areas.
1636db64fe02SNick Piggin  */
16370574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
1638db64fe02SNick Piggin {
16394d36e6f8SUladzislau Rezki (Sony) 	unsigned long resched_threshold;
164096e2db45SUladzislau Rezki (Sony) 	struct list_head local_pure_list;
164196e2db45SUladzislau Rezki (Sony) 	struct vmap_area *va, *n_va;
1642db64fe02SNick Piggin 
16430574ecd1SChristoph Hellwig 	lockdep_assert_held(&vmap_purge_lock);
164402b709dfSNick Piggin 
164596e2db45SUladzislau Rezki (Sony) 	spin_lock(&purge_vmap_area_lock);
164696e2db45SUladzislau Rezki (Sony) 	purge_vmap_area_root = RB_ROOT;
164796e2db45SUladzislau Rezki (Sony) 	list_replace_init(&purge_vmap_area_list, &local_pure_list);
164896e2db45SUladzislau Rezki (Sony) 	spin_unlock(&purge_vmap_area_lock);
164996e2db45SUladzislau Rezki (Sony) 
165096e2db45SUladzislau Rezki (Sony) 	if (unlikely(list_empty(&local_pure_list)))
165168571be9SUladzislau Rezki (Sony) 		return false;
165268571be9SUladzislau Rezki (Sony) 
165396e2db45SUladzislau Rezki (Sony) 	start = min(start,
165496e2db45SUladzislau Rezki (Sony) 		list_first_entry(&local_pure_list,
165596e2db45SUladzislau Rezki (Sony) 			struct vmap_area, list)->va_start);
165696e2db45SUladzislau Rezki (Sony) 
165796e2db45SUladzislau Rezki (Sony) 	end = max(end,
165896e2db45SUladzislau Rezki (Sony) 		list_last_entry(&local_pure_list,
165996e2db45SUladzislau Rezki (Sony) 			struct vmap_area, list)->va_end);
1660db64fe02SNick Piggin 
16610574ecd1SChristoph Hellwig 	flush_tlb_kernel_range(start, end);
16624d36e6f8SUladzislau Rezki (Sony) 	resched_threshold = lazy_max_pages() << 1;
1663db64fe02SNick Piggin 
1664e36176beSUladzislau Rezki (Sony) 	spin_lock(&free_vmap_area_lock);
166596e2db45SUladzislau Rezki (Sony) 	list_for_each_entry_safe(va, n_va, &local_pure_list, list) {
16664d36e6f8SUladzislau Rezki (Sony) 		unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
16673c5c3cfbSDaniel Axtens 		unsigned long orig_start = va->va_start;
16683c5c3cfbSDaniel Axtens 		unsigned long orig_end = va->va_end;
1669763b218dSJoel Fernandes 
1670dd3b8353SUladzislau Rezki (Sony) 		/*
1671dd3b8353SUladzislau Rezki (Sony) 		 * Finally insert or merge lazily-freed area. It is
1672dd3b8353SUladzislau Rezki (Sony) 		 * detached and there is no need to "unlink" it from
1673dd3b8353SUladzislau Rezki (Sony) 		 * anything.
1674dd3b8353SUladzislau Rezki (Sony) 		 */
167596e2db45SUladzislau Rezki (Sony) 		va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root,
16763c5c3cfbSDaniel Axtens 				&free_vmap_area_list);
16773c5c3cfbSDaniel Axtens 
16789c801f61SUladzislau Rezki (Sony) 		if (!va)
16799c801f61SUladzislau Rezki (Sony) 			continue;
16809c801f61SUladzislau Rezki (Sony) 
16813c5c3cfbSDaniel Axtens 		if (is_vmalloc_or_module_addr((void *)orig_start))
16823c5c3cfbSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
16833c5c3cfbSDaniel Axtens 					      va->va_start, va->va_end);
1684dd3b8353SUladzislau Rezki (Sony) 
16854d36e6f8SUladzislau Rezki (Sony) 		atomic_long_sub(nr, &vmap_lazy_nr);
168668571be9SUladzislau Rezki (Sony) 
16874d36e6f8SUladzislau Rezki (Sony) 		if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
1688e36176beSUladzislau Rezki (Sony) 			cond_resched_lock(&free_vmap_area_lock);
1689763b218dSJoel Fernandes 	}
1690e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
16910574ecd1SChristoph Hellwig 	return true;
1692db64fe02SNick Piggin }
1693db64fe02SNick Piggin 
1694db64fe02SNick Piggin /*
1695496850e5SNick Piggin  * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
1696496850e5SNick Piggin  * is already purging.
1697496850e5SNick Piggin  */
1698496850e5SNick Piggin static void try_purge_vmap_area_lazy(void)
1699496850e5SNick Piggin {
1700f9e09977SChristoph Hellwig 	if (mutex_trylock(&vmap_purge_lock)) {
17010574ecd1SChristoph Hellwig 		__purge_vmap_area_lazy(ULONG_MAX, 0);
1702f9e09977SChristoph Hellwig 		mutex_unlock(&vmap_purge_lock);
17030574ecd1SChristoph Hellwig 	}
1704496850e5SNick Piggin }
1705496850e5SNick Piggin 
1706496850e5SNick Piggin /*
1707db64fe02SNick Piggin  * Kick off a purge of the outstanding lazy areas.
1708db64fe02SNick Piggin  */
1709db64fe02SNick Piggin static void purge_vmap_area_lazy(void)
1710db64fe02SNick Piggin {
1711f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
17120574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
17130574ecd1SChristoph Hellwig 	__purge_vmap_area_lazy(ULONG_MAX, 0);
1714f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
1715db64fe02SNick Piggin }
1716db64fe02SNick Piggin 
1717db64fe02SNick Piggin /*
171864141da5SJeremy Fitzhardinge  * Free a vmap area, caller ensuring that the area has been unmapped
171964141da5SJeremy Fitzhardinge  * and flush_cache_vunmap had been called for the correct range
172064141da5SJeremy Fitzhardinge  * previously.
1721db64fe02SNick Piggin  */
172264141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va)
1723db64fe02SNick Piggin {
17244d36e6f8SUladzislau Rezki (Sony) 	unsigned long nr_lazy;
172580c4bd7aSChris Wilson 
1726dd3b8353SUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
1727dd3b8353SUladzislau Rezki (Sony) 	unlink_va(va, &vmap_area_root);
1728dd3b8353SUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
1729dd3b8353SUladzislau Rezki (Sony) 
17304d36e6f8SUladzislau Rezki (Sony) 	nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
17314d36e6f8SUladzislau Rezki (Sony) 				PAGE_SHIFT, &vmap_lazy_nr);
173280c4bd7aSChris Wilson 
173396e2db45SUladzislau Rezki (Sony) 	/*
173496e2db45SUladzislau Rezki (Sony) 	 * Merge or place it to the purge tree/list.
173596e2db45SUladzislau Rezki (Sony) 	 */
173696e2db45SUladzislau Rezki (Sony) 	spin_lock(&purge_vmap_area_lock);
173796e2db45SUladzislau Rezki (Sony) 	merge_or_add_vmap_area(va,
173896e2db45SUladzislau Rezki (Sony) 		&purge_vmap_area_root, &purge_vmap_area_list);
173996e2db45SUladzislau Rezki (Sony) 	spin_unlock(&purge_vmap_area_lock);
174080c4bd7aSChris Wilson 
174196e2db45SUladzislau Rezki (Sony) 	/* After this point, we may free va at any time */
174280c4bd7aSChris Wilson 	if (unlikely(nr_lazy > lazy_max_pages()))
1743496850e5SNick Piggin 		try_purge_vmap_area_lazy();
1744db64fe02SNick Piggin }
1745db64fe02SNick Piggin 
1746b29acbdcSNick Piggin /*
1747b29acbdcSNick Piggin  * Free and unmap a vmap area
1748b29acbdcSNick Piggin  */
1749b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va)
1750b29acbdcSNick Piggin {
1751b29acbdcSNick Piggin 	flush_cache_vunmap(va->va_start, va->va_end);
17524ad0ae8cSNicholas Piggin 	vunmap_range_noflush(va->va_start, va->va_end);
17538e57f8acSVlastimil Babka 	if (debug_pagealloc_enabled_static())
175482a2e924SChintan Pandya 		flush_tlb_kernel_range(va->va_start, va->va_end);
175582a2e924SChintan Pandya 
1756c8eef01eSChristoph Hellwig 	free_vmap_area_noflush(va);
1757b29acbdcSNick Piggin }
1758b29acbdcSNick Piggin 
1759db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr)
1760db64fe02SNick Piggin {
1761db64fe02SNick Piggin 	struct vmap_area *va;
1762db64fe02SNick Piggin 
1763db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
1764db64fe02SNick Piggin 	va = __find_vmap_area(addr);
1765db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
1766db64fe02SNick Piggin 
1767db64fe02SNick Piggin 	return va;
1768db64fe02SNick Piggin }
1769db64fe02SNick Piggin 
1770db64fe02SNick Piggin /*** Per cpu kva allocator ***/
1771db64fe02SNick Piggin 
1772db64fe02SNick Piggin /*
1773db64fe02SNick Piggin  * vmap space is limited especially on 32 bit architectures. Ensure there is
1774db64fe02SNick Piggin  * room for at least 16 percpu vmap blocks per CPU.
1775db64fe02SNick Piggin  */
1776db64fe02SNick Piggin /*
1777db64fe02SNick Piggin  * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
1778db64fe02SNick Piggin  * to #define VMALLOC_SPACE		(VMALLOC_END-VMALLOC_START). Guess
1779db64fe02SNick Piggin  * instead (we just need a rough idea)
1780db64fe02SNick Piggin  */
1781db64fe02SNick Piggin #if BITS_PER_LONG == 32
1782db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024)
1783db64fe02SNick Piggin #else
1784db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024*1024)
1785db64fe02SNick Piggin #endif
1786db64fe02SNick Piggin 
1787db64fe02SNick Piggin #define VMALLOC_PAGES		(VMALLOC_SPACE / PAGE_SIZE)
1788db64fe02SNick Piggin #define VMAP_MAX_ALLOC		BITS_PER_LONG	/* 256K with 4K pages */
1789db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX	1024	/* 4MB with 4K pages */
1790db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN	(VMAP_MAX_ALLOC*2)
1791db64fe02SNick Piggin #define VMAP_MIN(x, y)		((x) < (y) ? (x) : (y)) /* can't use min() */
1792db64fe02SNick Piggin #define VMAP_MAX(x, y)		((x) > (y) ? (x) : (y)) /* can't use max() */
1793f982f915SClemens Ladisch #define VMAP_BBMAP_BITS		\
1794f982f915SClemens Ladisch 		VMAP_MIN(VMAP_BBMAP_BITS_MAX,	\
1795db64fe02SNick Piggin 		VMAP_MAX(VMAP_BBMAP_BITS_MIN,	\
1796f982f915SClemens Ladisch 			VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
1797db64fe02SNick Piggin 
1798db64fe02SNick Piggin #define VMAP_BLOCK_SIZE		(VMAP_BBMAP_BITS * PAGE_SIZE)
1799db64fe02SNick Piggin 
1800db64fe02SNick Piggin struct vmap_block_queue {
1801db64fe02SNick Piggin 	spinlock_t lock;
1802db64fe02SNick Piggin 	struct list_head free;
1803db64fe02SNick Piggin };
1804db64fe02SNick Piggin 
1805db64fe02SNick Piggin struct vmap_block {
1806db64fe02SNick Piggin 	spinlock_t lock;
1807db64fe02SNick Piggin 	struct vmap_area *va;
1808db64fe02SNick Piggin 	unsigned long free, dirty;
18097d61bfe8SRoman Pen 	unsigned long dirty_min, dirty_max; /*< dirty range */
1810db64fe02SNick Piggin 	struct list_head free_list;
1811db64fe02SNick Piggin 	struct rcu_head rcu_head;
181202b709dfSNick Piggin 	struct list_head purge;
1813db64fe02SNick Piggin };
1814db64fe02SNick Piggin 
1815db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1816db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1817db64fe02SNick Piggin 
1818db64fe02SNick Piggin /*
18190f14599cSMatthew Wilcox (Oracle)  * XArray of vmap blocks, indexed by address, to quickly find a vmap block
1820db64fe02SNick Piggin  * in the free path. Could get rid of this if we change the API to return a
1821db64fe02SNick Piggin  * "cookie" from alloc, to be passed to free. But no big deal yet.
1822db64fe02SNick Piggin  */
18230f14599cSMatthew Wilcox (Oracle) static DEFINE_XARRAY(vmap_blocks);
1824db64fe02SNick Piggin 
1825db64fe02SNick Piggin /*
1826db64fe02SNick Piggin  * We should probably have a fallback mechanism to allocate virtual memory
1827db64fe02SNick Piggin  * out of partially filled vmap blocks. However vmap block sizing should be
1828db64fe02SNick Piggin  * fairly reasonable according to the vmalloc size, so it shouldn't be a
1829db64fe02SNick Piggin  * big problem.
1830db64fe02SNick Piggin  */
1831db64fe02SNick Piggin 
1832db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr)
1833db64fe02SNick Piggin {
1834db64fe02SNick Piggin 	addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
1835db64fe02SNick Piggin 	addr /= VMAP_BLOCK_SIZE;
1836db64fe02SNick Piggin 	return addr;
1837db64fe02SNick Piggin }
1838db64fe02SNick Piggin 
1839cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
1840cf725ce2SRoman Pen {
1841cf725ce2SRoman Pen 	unsigned long addr;
1842cf725ce2SRoman Pen 
1843cf725ce2SRoman Pen 	addr = va_start + (pages_off << PAGE_SHIFT);
1844cf725ce2SRoman Pen 	BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
1845cf725ce2SRoman Pen 	return (void *)addr;
1846cf725ce2SRoman Pen }
1847cf725ce2SRoman Pen 
1848cf725ce2SRoman Pen /**
1849cf725ce2SRoman Pen  * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
1850cf725ce2SRoman Pen  *                  block. Of course pages number can't exceed VMAP_BBMAP_BITS
1851cf725ce2SRoman Pen  * @order:    how many 2^order pages should be occupied in newly allocated block
1852cf725ce2SRoman Pen  * @gfp_mask: flags for the page level allocator
1853cf725ce2SRoman Pen  *
1854a862f68aSMike Rapoport  * Return: virtual address in a newly allocated block or ERR_PTR(-errno)
1855cf725ce2SRoman Pen  */
1856cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
1857db64fe02SNick Piggin {
1858db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
1859db64fe02SNick Piggin 	struct vmap_block *vb;
1860db64fe02SNick Piggin 	struct vmap_area *va;
1861db64fe02SNick Piggin 	unsigned long vb_idx;
1862db64fe02SNick Piggin 	int node, err;
1863cf725ce2SRoman Pen 	void *vaddr;
1864db64fe02SNick Piggin 
1865db64fe02SNick Piggin 	node = numa_node_id();
1866db64fe02SNick Piggin 
1867db64fe02SNick Piggin 	vb = kmalloc_node(sizeof(struct vmap_block),
1868db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
1869db64fe02SNick Piggin 	if (unlikely(!vb))
1870db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
1871db64fe02SNick Piggin 
1872db64fe02SNick Piggin 	va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
1873db64fe02SNick Piggin 					VMALLOC_START, VMALLOC_END,
1874db64fe02SNick Piggin 					node, gfp_mask);
1875ddf9c6d4STobias Klauser 	if (IS_ERR(va)) {
1876db64fe02SNick Piggin 		kfree(vb);
1877e7d86340SJulia Lawall 		return ERR_CAST(va);
1878db64fe02SNick Piggin 	}
1879db64fe02SNick Piggin 
1880cf725ce2SRoman Pen 	vaddr = vmap_block_vaddr(va->va_start, 0);
1881db64fe02SNick Piggin 	spin_lock_init(&vb->lock);
1882db64fe02SNick Piggin 	vb->va = va;
1883cf725ce2SRoman Pen 	/* At least something should be left free */
1884cf725ce2SRoman Pen 	BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
1885cf725ce2SRoman Pen 	vb->free = VMAP_BBMAP_BITS - (1UL << order);
1886db64fe02SNick Piggin 	vb->dirty = 0;
18877d61bfe8SRoman Pen 	vb->dirty_min = VMAP_BBMAP_BITS;
18887d61bfe8SRoman Pen 	vb->dirty_max = 0;
1889db64fe02SNick Piggin 	INIT_LIST_HEAD(&vb->free_list);
1890db64fe02SNick Piggin 
1891db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(va->va_start);
18920f14599cSMatthew Wilcox (Oracle) 	err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask);
18930f14599cSMatthew Wilcox (Oracle) 	if (err) {
18940f14599cSMatthew Wilcox (Oracle) 		kfree(vb);
18950f14599cSMatthew Wilcox (Oracle) 		free_vmap_area(va);
18960f14599cSMatthew Wilcox (Oracle) 		return ERR_PTR(err);
18970f14599cSMatthew Wilcox (Oracle) 	}
1898db64fe02SNick Piggin 
1899db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
1900db64fe02SNick Piggin 	spin_lock(&vbq->lock);
190168ac546fSRoman Pen 	list_add_tail_rcu(&vb->free_list, &vbq->free);
1902db64fe02SNick Piggin 	spin_unlock(&vbq->lock);
19033f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
1904db64fe02SNick Piggin 
1905cf725ce2SRoman Pen 	return vaddr;
1906db64fe02SNick Piggin }
1907db64fe02SNick Piggin 
1908db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb)
1909db64fe02SNick Piggin {
1910db64fe02SNick Piggin 	struct vmap_block *tmp;
1911db64fe02SNick Piggin 
19120f14599cSMatthew Wilcox (Oracle) 	tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start));
1913db64fe02SNick Piggin 	BUG_ON(tmp != vb);
1914db64fe02SNick Piggin 
191564141da5SJeremy Fitzhardinge 	free_vmap_area_noflush(vb->va);
191622a3c7d1SLai Jiangshan 	kfree_rcu(vb, rcu_head);
1917db64fe02SNick Piggin }
1918db64fe02SNick Piggin 
191902b709dfSNick Piggin static void purge_fragmented_blocks(int cpu)
192002b709dfSNick Piggin {
192102b709dfSNick Piggin 	LIST_HEAD(purge);
192202b709dfSNick Piggin 	struct vmap_block *vb;
192302b709dfSNick Piggin 	struct vmap_block *n_vb;
192402b709dfSNick Piggin 	struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
192502b709dfSNick Piggin 
192602b709dfSNick Piggin 	rcu_read_lock();
192702b709dfSNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
192802b709dfSNick Piggin 
192902b709dfSNick Piggin 		if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
193002b709dfSNick Piggin 			continue;
193102b709dfSNick Piggin 
193202b709dfSNick Piggin 		spin_lock(&vb->lock);
193302b709dfSNick Piggin 		if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
193402b709dfSNick Piggin 			vb->free = 0; /* prevent further allocs after releasing lock */
193502b709dfSNick Piggin 			vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
19367d61bfe8SRoman Pen 			vb->dirty_min = 0;
19377d61bfe8SRoman Pen 			vb->dirty_max = VMAP_BBMAP_BITS;
193802b709dfSNick Piggin 			spin_lock(&vbq->lock);
193902b709dfSNick Piggin 			list_del_rcu(&vb->free_list);
194002b709dfSNick Piggin 			spin_unlock(&vbq->lock);
194102b709dfSNick Piggin 			spin_unlock(&vb->lock);
194202b709dfSNick Piggin 			list_add_tail(&vb->purge, &purge);
194302b709dfSNick Piggin 		} else
194402b709dfSNick Piggin 			spin_unlock(&vb->lock);
194502b709dfSNick Piggin 	}
194602b709dfSNick Piggin 	rcu_read_unlock();
194702b709dfSNick Piggin 
194802b709dfSNick Piggin 	list_for_each_entry_safe(vb, n_vb, &purge, purge) {
194902b709dfSNick Piggin 		list_del(&vb->purge);
195002b709dfSNick Piggin 		free_vmap_block(vb);
195102b709dfSNick Piggin 	}
195202b709dfSNick Piggin }
195302b709dfSNick Piggin 
195402b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void)
195502b709dfSNick Piggin {
195602b709dfSNick Piggin 	int cpu;
195702b709dfSNick Piggin 
195802b709dfSNick Piggin 	for_each_possible_cpu(cpu)
195902b709dfSNick Piggin 		purge_fragmented_blocks(cpu);
196002b709dfSNick Piggin }
196102b709dfSNick Piggin 
1962db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
1963db64fe02SNick Piggin {
1964db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
1965db64fe02SNick Piggin 	struct vmap_block *vb;
1966cf725ce2SRoman Pen 	void *vaddr = NULL;
1967db64fe02SNick Piggin 	unsigned int order;
1968db64fe02SNick Piggin 
1969891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
1970db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
1971aa91c4d8SJan Kara 	if (WARN_ON(size == 0)) {
1972aa91c4d8SJan Kara 		/*
1973aa91c4d8SJan Kara 		 * Allocating 0 bytes isn't what caller wants since
1974aa91c4d8SJan Kara 		 * get_order(0) returns funny result. Just warn and terminate
1975aa91c4d8SJan Kara 		 * early.
1976aa91c4d8SJan Kara 		 */
1977aa91c4d8SJan Kara 		return NULL;
1978aa91c4d8SJan Kara 	}
1979db64fe02SNick Piggin 	order = get_order(size);
1980db64fe02SNick Piggin 
1981db64fe02SNick Piggin 	rcu_read_lock();
1982db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
1983db64fe02SNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1984cf725ce2SRoman Pen 		unsigned long pages_off;
1985db64fe02SNick Piggin 
1986db64fe02SNick Piggin 		spin_lock(&vb->lock);
1987cf725ce2SRoman Pen 		if (vb->free < (1UL << order)) {
1988cf725ce2SRoman Pen 			spin_unlock(&vb->lock);
1989cf725ce2SRoman Pen 			continue;
1990cf725ce2SRoman Pen 		}
199102b709dfSNick Piggin 
1992cf725ce2SRoman Pen 		pages_off = VMAP_BBMAP_BITS - vb->free;
1993cf725ce2SRoman Pen 		vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
1994db64fe02SNick Piggin 		vb->free -= 1UL << order;
1995db64fe02SNick Piggin 		if (vb->free == 0) {
1996db64fe02SNick Piggin 			spin_lock(&vbq->lock);
1997de560423SNick Piggin 			list_del_rcu(&vb->free_list);
1998db64fe02SNick Piggin 			spin_unlock(&vbq->lock);
1999db64fe02SNick Piggin 		}
2000cf725ce2SRoman Pen 
2001db64fe02SNick Piggin 		spin_unlock(&vb->lock);
2002db64fe02SNick Piggin 		break;
2003db64fe02SNick Piggin 	}
200402b709dfSNick Piggin 
20053f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
2006db64fe02SNick Piggin 	rcu_read_unlock();
2007db64fe02SNick Piggin 
2008cf725ce2SRoman Pen 	/* Allocate new block if nothing was found */
2009cf725ce2SRoman Pen 	if (!vaddr)
2010cf725ce2SRoman Pen 		vaddr = new_vmap_block(order, gfp_mask);
2011db64fe02SNick Piggin 
2012cf725ce2SRoman Pen 	return vaddr;
2013db64fe02SNick Piggin }
2014db64fe02SNick Piggin 
201578a0e8c4SChristoph Hellwig static void vb_free(unsigned long addr, unsigned long size)
2016db64fe02SNick Piggin {
2017db64fe02SNick Piggin 	unsigned long offset;
2018db64fe02SNick Piggin 	unsigned int order;
2019db64fe02SNick Piggin 	struct vmap_block *vb;
2020db64fe02SNick Piggin 
2021891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
2022db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
2023b29acbdcSNick Piggin 
202478a0e8c4SChristoph Hellwig 	flush_cache_vunmap(addr, addr + size);
2025b29acbdcSNick Piggin 
2026db64fe02SNick Piggin 	order = get_order(size);
202778a0e8c4SChristoph Hellwig 	offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT;
20280f14599cSMatthew Wilcox (Oracle) 	vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr));
2029db64fe02SNick Piggin 
20304ad0ae8cSNicholas Piggin 	vunmap_range_noflush(addr, addr + size);
203164141da5SJeremy Fitzhardinge 
20328e57f8acSVlastimil Babka 	if (debug_pagealloc_enabled_static())
203378a0e8c4SChristoph Hellwig 		flush_tlb_kernel_range(addr, addr + size);
203482a2e924SChintan Pandya 
2035db64fe02SNick Piggin 	spin_lock(&vb->lock);
20367d61bfe8SRoman Pen 
20377d61bfe8SRoman Pen 	/* Expand dirty range */
20387d61bfe8SRoman Pen 	vb->dirty_min = min(vb->dirty_min, offset);
20397d61bfe8SRoman Pen 	vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
2040d086817dSMinChan Kim 
2041db64fe02SNick Piggin 	vb->dirty += 1UL << order;
2042db64fe02SNick Piggin 	if (vb->dirty == VMAP_BBMAP_BITS) {
2043de560423SNick Piggin 		BUG_ON(vb->free);
2044db64fe02SNick Piggin 		spin_unlock(&vb->lock);
2045db64fe02SNick Piggin 		free_vmap_block(vb);
2046db64fe02SNick Piggin 	} else
2047db64fe02SNick Piggin 		spin_unlock(&vb->lock);
2048db64fe02SNick Piggin }
2049db64fe02SNick Piggin 
2050868b104dSRick Edgecombe static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
2051db64fe02SNick Piggin {
2052db64fe02SNick Piggin 	int cpu;
2053db64fe02SNick Piggin 
20549b463334SJeremy Fitzhardinge 	if (unlikely(!vmap_initialized))
20559b463334SJeremy Fitzhardinge 		return;
20569b463334SJeremy Fitzhardinge 
20575803ed29SChristoph Hellwig 	might_sleep();
20585803ed29SChristoph Hellwig 
2059db64fe02SNick Piggin 	for_each_possible_cpu(cpu) {
2060db64fe02SNick Piggin 		struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
2061db64fe02SNick Piggin 		struct vmap_block *vb;
2062db64fe02SNick Piggin 
2063db64fe02SNick Piggin 		rcu_read_lock();
2064db64fe02SNick Piggin 		list_for_each_entry_rcu(vb, &vbq->free, free_list) {
2065db64fe02SNick Piggin 			spin_lock(&vb->lock);
2066ad216c03SVijayanand Jitta 			if (vb->dirty && vb->dirty != VMAP_BBMAP_BITS) {
20677d61bfe8SRoman Pen 				unsigned long va_start = vb->va->va_start;
2068db64fe02SNick Piggin 				unsigned long s, e;
2069b136be5eSJoonsoo Kim 
20707d61bfe8SRoman Pen 				s = va_start + (vb->dirty_min << PAGE_SHIFT);
20717d61bfe8SRoman Pen 				e = va_start + (vb->dirty_max << PAGE_SHIFT);
2072db64fe02SNick Piggin 
20737d61bfe8SRoman Pen 				start = min(s, start);
20747d61bfe8SRoman Pen 				end   = max(e, end);
20757d61bfe8SRoman Pen 
2076db64fe02SNick Piggin 				flush = 1;
2077db64fe02SNick Piggin 			}
2078db64fe02SNick Piggin 			spin_unlock(&vb->lock);
2079db64fe02SNick Piggin 		}
2080db64fe02SNick Piggin 		rcu_read_unlock();
2081db64fe02SNick Piggin 	}
2082db64fe02SNick Piggin 
2083f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
20840574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
20850574ecd1SChristoph Hellwig 	if (!__purge_vmap_area_lazy(start, end) && flush)
20860574ecd1SChristoph Hellwig 		flush_tlb_kernel_range(start, end);
2087f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
2088db64fe02SNick Piggin }
2089868b104dSRick Edgecombe 
2090868b104dSRick Edgecombe /**
2091868b104dSRick Edgecombe  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
2092868b104dSRick Edgecombe  *
2093868b104dSRick Edgecombe  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
2094868b104dSRick Edgecombe  * to amortize TLB flushing overheads. What this means is that any page you
2095868b104dSRick Edgecombe  * have now, may, in a former life, have been mapped into kernel virtual
2096868b104dSRick Edgecombe  * address by the vmap layer and so there might be some CPUs with TLB entries
2097868b104dSRick Edgecombe  * still referencing that page (additional to the regular 1:1 kernel mapping).
2098868b104dSRick Edgecombe  *
2099868b104dSRick Edgecombe  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
2100868b104dSRick Edgecombe  * be sure that none of the pages we have control over will have any aliases
2101868b104dSRick Edgecombe  * from the vmap layer.
2102868b104dSRick Edgecombe  */
2103868b104dSRick Edgecombe void vm_unmap_aliases(void)
2104868b104dSRick Edgecombe {
2105868b104dSRick Edgecombe 	unsigned long start = ULONG_MAX, end = 0;
2106868b104dSRick Edgecombe 	int flush = 0;
2107868b104dSRick Edgecombe 
2108868b104dSRick Edgecombe 	_vm_unmap_aliases(start, end, flush);
2109868b104dSRick Edgecombe }
2110db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases);
2111db64fe02SNick Piggin 
2112db64fe02SNick Piggin /**
2113db64fe02SNick Piggin  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
2114db64fe02SNick Piggin  * @mem: the pointer returned by vm_map_ram
2115db64fe02SNick Piggin  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
2116db64fe02SNick Piggin  */
2117db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count)
2118db64fe02SNick Piggin {
211965ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
2120db64fe02SNick Piggin 	unsigned long addr = (unsigned long)mem;
21219c3acf60SChristoph Hellwig 	struct vmap_area *va;
2122db64fe02SNick Piggin 
21235803ed29SChristoph Hellwig 	might_sleep();
2124db64fe02SNick Piggin 	BUG_ON(!addr);
2125db64fe02SNick Piggin 	BUG_ON(addr < VMALLOC_START);
2126db64fe02SNick Piggin 	BUG_ON(addr > VMALLOC_END);
2127a1c0b1a0SShawn Lin 	BUG_ON(!PAGE_ALIGNED(addr));
2128db64fe02SNick Piggin 
2129d98c9e83SAndrey Ryabinin 	kasan_poison_vmalloc(mem, size);
2130d98c9e83SAndrey Ryabinin 
21319c3acf60SChristoph Hellwig 	if (likely(count <= VMAP_MAX_ALLOC)) {
213205e3ff95SChintan Pandya 		debug_check_no_locks_freed(mem, size);
213378a0e8c4SChristoph Hellwig 		vb_free(addr, size);
21349c3acf60SChristoph Hellwig 		return;
21359c3acf60SChristoph Hellwig 	}
21369c3acf60SChristoph Hellwig 
21379c3acf60SChristoph Hellwig 	va = find_vmap_area(addr);
21389c3acf60SChristoph Hellwig 	BUG_ON(!va);
213905e3ff95SChintan Pandya 	debug_check_no_locks_freed((void *)va->va_start,
214005e3ff95SChintan Pandya 				    (va->va_end - va->va_start));
21419c3acf60SChristoph Hellwig 	free_unmap_vmap_area(va);
2142db64fe02SNick Piggin }
2143db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram);
2144db64fe02SNick Piggin 
2145db64fe02SNick Piggin /**
2146db64fe02SNick Piggin  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
2147db64fe02SNick Piggin  * @pages: an array of pointers to the pages to be mapped
2148db64fe02SNick Piggin  * @count: number of pages
2149db64fe02SNick Piggin  * @node: prefer to allocate data structures on this node
2150e99c97adSRandy Dunlap  *
215136437638SGioh Kim  * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
215236437638SGioh Kim  * faster than vmap so it's good.  But if you mix long-life and short-life
215336437638SGioh Kim  * objects with vm_map_ram(), it could consume lots of address space through
215436437638SGioh Kim  * fragmentation (especially on a 32bit machine).  You could see failures in
215536437638SGioh Kim  * the end.  Please use this function for short-lived objects.
215636437638SGioh Kim  *
2157e99c97adSRandy Dunlap  * Returns: a pointer to the address that has been mapped, or %NULL on failure
2158db64fe02SNick Piggin  */
2159d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node)
2160db64fe02SNick Piggin {
216165ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
2162db64fe02SNick Piggin 	unsigned long addr;
2163db64fe02SNick Piggin 	void *mem;
2164db64fe02SNick Piggin 
2165db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC)) {
2166db64fe02SNick Piggin 		mem = vb_alloc(size, GFP_KERNEL);
2167db64fe02SNick Piggin 		if (IS_ERR(mem))
2168db64fe02SNick Piggin 			return NULL;
2169db64fe02SNick Piggin 		addr = (unsigned long)mem;
2170db64fe02SNick Piggin 	} else {
2171db64fe02SNick Piggin 		struct vmap_area *va;
2172db64fe02SNick Piggin 		va = alloc_vmap_area(size, PAGE_SIZE,
2173db64fe02SNick Piggin 				VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
2174db64fe02SNick Piggin 		if (IS_ERR(va))
2175db64fe02SNick Piggin 			return NULL;
2176db64fe02SNick Piggin 
2177db64fe02SNick Piggin 		addr = va->va_start;
2178db64fe02SNick Piggin 		mem = (void *)addr;
2179db64fe02SNick Piggin 	}
2180d98c9e83SAndrey Ryabinin 
2181d98c9e83SAndrey Ryabinin 	kasan_unpoison_vmalloc(mem, size);
2182d98c9e83SAndrey Ryabinin 
2183b67177ecSNicholas Piggin 	if (vmap_pages_range(addr, addr + size, PAGE_KERNEL,
2184b67177ecSNicholas Piggin 				pages, PAGE_SHIFT) < 0) {
2185db64fe02SNick Piggin 		vm_unmap_ram(mem, count);
2186db64fe02SNick Piggin 		return NULL;
2187db64fe02SNick Piggin 	}
2188b67177ecSNicholas Piggin 
2189db64fe02SNick Piggin 	return mem;
2190db64fe02SNick Piggin }
2191db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram);
2192db64fe02SNick Piggin 
21934341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata;
219492eac168SMike Rapoport 
2195121e6f32SNicholas Piggin static inline unsigned int vm_area_page_order(struct vm_struct *vm)
2196121e6f32SNicholas Piggin {
2197121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
2198121e6f32SNicholas Piggin 	return vm->page_order;
2199121e6f32SNicholas Piggin #else
2200121e6f32SNicholas Piggin 	return 0;
2201121e6f32SNicholas Piggin #endif
2202121e6f32SNicholas Piggin }
2203121e6f32SNicholas Piggin 
2204121e6f32SNicholas Piggin static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int order)
2205121e6f32SNicholas Piggin {
2206121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
2207121e6f32SNicholas Piggin 	vm->page_order = order;
2208121e6f32SNicholas Piggin #else
2209121e6f32SNicholas Piggin 	BUG_ON(order != 0);
2210121e6f32SNicholas Piggin #endif
2211121e6f32SNicholas Piggin }
2212121e6f32SNicholas Piggin 
2213f0aa6617STejun Heo /**
2214be9b7335SNicolas Pitre  * vm_area_add_early - add vmap area early during boot
2215be9b7335SNicolas Pitre  * @vm: vm_struct to add
2216be9b7335SNicolas Pitre  *
2217be9b7335SNicolas Pitre  * This function is used to add fixed kernel vm area to vmlist before
2218be9b7335SNicolas Pitre  * vmalloc_init() is called.  @vm->addr, @vm->size, and @vm->flags
2219be9b7335SNicolas Pitre  * should contain proper values and the other fields should be zero.
2220be9b7335SNicolas Pitre  *
2221be9b7335SNicolas Pitre  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
2222be9b7335SNicolas Pitre  */
2223be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm)
2224be9b7335SNicolas Pitre {
2225be9b7335SNicolas Pitre 	struct vm_struct *tmp, **p;
2226be9b7335SNicolas Pitre 
2227be9b7335SNicolas Pitre 	BUG_ON(vmap_initialized);
2228be9b7335SNicolas Pitre 	for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
2229be9b7335SNicolas Pitre 		if (tmp->addr >= vm->addr) {
2230be9b7335SNicolas Pitre 			BUG_ON(tmp->addr < vm->addr + vm->size);
2231be9b7335SNicolas Pitre 			break;
2232be9b7335SNicolas Pitre 		} else
2233be9b7335SNicolas Pitre 			BUG_ON(tmp->addr + tmp->size > vm->addr);
2234be9b7335SNicolas Pitre 	}
2235be9b7335SNicolas Pitre 	vm->next = *p;
2236be9b7335SNicolas Pitre 	*p = vm;
2237be9b7335SNicolas Pitre }
2238be9b7335SNicolas Pitre 
2239be9b7335SNicolas Pitre /**
2240f0aa6617STejun Heo  * vm_area_register_early - register vmap area early during boot
2241f0aa6617STejun Heo  * @vm: vm_struct to register
2242c0c0a293STejun Heo  * @align: requested alignment
2243f0aa6617STejun Heo  *
2244f0aa6617STejun Heo  * This function is used to register kernel vm area before
2245f0aa6617STejun Heo  * vmalloc_init() is called.  @vm->size and @vm->flags should contain
2246f0aa6617STejun Heo  * proper values on entry and other fields should be zero.  On return,
2247f0aa6617STejun Heo  * vm->addr contains the allocated address.
2248f0aa6617STejun Heo  *
2249f0aa6617STejun Heo  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
2250f0aa6617STejun Heo  */
2251c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align)
2252f0aa6617STejun Heo {
2253f0aa6617STejun Heo 	static size_t vm_init_off __initdata;
2254c0c0a293STejun Heo 	unsigned long addr;
2255f0aa6617STejun Heo 
2256c0c0a293STejun Heo 	addr = ALIGN(VMALLOC_START + vm_init_off, align);
2257c0c0a293STejun Heo 	vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
2258c0c0a293STejun Heo 
2259c0c0a293STejun Heo 	vm->addr = (void *)addr;
2260f0aa6617STejun Heo 
2261be9b7335SNicolas Pitre 	vm_area_add_early(vm);
2262f0aa6617STejun Heo }
2263f0aa6617STejun Heo 
226468ad4a33SUladzislau Rezki (Sony) static void vmap_init_free_space(void)
226568ad4a33SUladzislau Rezki (Sony) {
226668ad4a33SUladzislau Rezki (Sony) 	unsigned long vmap_start = 1;
226768ad4a33SUladzislau Rezki (Sony) 	const unsigned long vmap_end = ULONG_MAX;
226868ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *busy, *free;
226968ad4a33SUladzislau Rezki (Sony) 
227068ad4a33SUladzislau Rezki (Sony) 	/*
227168ad4a33SUladzislau Rezki (Sony) 	 *     B     F     B     B     B     F
227268ad4a33SUladzislau Rezki (Sony) 	 * -|-----|.....|-----|-----|-----|.....|-
227368ad4a33SUladzislau Rezki (Sony) 	 *  |           The KVA space           |
227468ad4a33SUladzislau Rezki (Sony) 	 *  |<--------------------------------->|
227568ad4a33SUladzislau Rezki (Sony) 	 */
227668ad4a33SUladzislau Rezki (Sony) 	list_for_each_entry(busy, &vmap_area_list, list) {
227768ad4a33SUladzislau Rezki (Sony) 		if (busy->va_start - vmap_start > 0) {
227868ad4a33SUladzislau Rezki (Sony) 			free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
227968ad4a33SUladzislau Rezki (Sony) 			if (!WARN_ON_ONCE(!free)) {
228068ad4a33SUladzislau Rezki (Sony) 				free->va_start = vmap_start;
228168ad4a33SUladzislau Rezki (Sony) 				free->va_end = busy->va_start;
228268ad4a33SUladzislau Rezki (Sony) 
228368ad4a33SUladzislau Rezki (Sony) 				insert_vmap_area_augment(free, NULL,
228468ad4a33SUladzislau Rezki (Sony) 					&free_vmap_area_root,
228568ad4a33SUladzislau Rezki (Sony) 						&free_vmap_area_list);
228668ad4a33SUladzislau Rezki (Sony) 			}
228768ad4a33SUladzislau Rezki (Sony) 		}
228868ad4a33SUladzislau Rezki (Sony) 
228968ad4a33SUladzislau Rezki (Sony) 		vmap_start = busy->va_end;
229068ad4a33SUladzislau Rezki (Sony) 	}
229168ad4a33SUladzislau Rezki (Sony) 
229268ad4a33SUladzislau Rezki (Sony) 	if (vmap_end - vmap_start > 0) {
229368ad4a33SUladzislau Rezki (Sony) 		free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
229468ad4a33SUladzislau Rezki (Sony) 		if (!WARN_ON_ONCE(!free)) {
229568ad4a33SUladzislau Rezki (Sony) 			free->va_start = vmap_start;
229668ad4a33SUladzislau Rezki (Sony) 			free->va_end = vmap_end;
229768ad4a33SUladzislau Rezki (Sony) 
229868ad4a33SUladzislau Rezki (Sony) 			insert_vmap_area_augment(free, NULL,
229968ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_root,
230068ad4a33SUladzislau Rezki (Sony) 					&free_vmap_area_list);
230168ad4a33SUladzislau Rezki (Sony) 		}
230268ad4a33SUladzislau Rezki (Sony) 	}
230368ad4a33SUladzislau Rezki (Sony) }
230468ad4a33SUladzislau Rezki (Sony) 
2305db64fe02SNick Piggin void __init vmalloc_init(void)
2306db64fe02SNick Piggin {
2307822c18f2SIvan Kokshaysky 	struct vmap_area *va;
2308822c18f2SIvan Kokshaysky 	struct vm_struct *tmp;
2309db64fe02SNick Piggin 	int i;
2310db64fe02SNick Piggin 
231168ad4a33SUladzislau Rezki (Sony) 	/*
231268ad4a33SUladzislau Rezki (Sony) 	 * Create the cache for vmap_area objects.
231368ad4a33SUladzislau Rezki (Sony) 	 */
231468ad4a33SUladzislau Rezki (Sony) 	vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
231568ad4a33SUladzislau Rezki (Sony) 
2316db64fe02SNick Piggin 	for_each_possible_cpu(i) {
2317db64fe02SNick Piggin 		struct vmap_block_queue *vbq;
231832fcfd40SAl Viro 		struct vfree_deferred *p;
2319db64fe02SNick Piggin 
2320db64fe02SNick Piggin 		vbq = &per_cpu(vmap_block_queue, i);
2321db64fe02SNick Piggin 		spin_lock_init(&vbq->lock);
2322db64fe02SNick Piggin 		INIT_LIST_HEAD(&vbq->free);
232332fcfd40SAl Viro 		p = &per_cpu(vfree_deferred, i);
232432fcfd40SAl Viro 		init_llist_head(&p->list);
232532fcfd40SAl Viro 		INIT_WORK(&p->wq, free_work);
2326db64fe02SNick Piggin 	}
23279b463334SJeremy Fitzhardinge 
2328822c18f2SIvan Kokshaysky 	/* Import existing vmlist entries. */
2329822c18f2SIvan Kokshaysky 	for (tmp = vmlist; tmp; tmp = tmp->next) {
233068ad4a33SUladzislau Rezki (Sony) 		va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
233168ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(!va))
233268ad4a33SUladzislau Rezki (Sony) 			continue;
233368ad4a33SUladzislau Rezki (Sony) 
2334822c18f2SIvan Kokshaysky 		va->va_start = (unsigned long)tmp->addr;
2335822c18f2SIvan Kokshaysky 		va->va_end = va->va_start + tmp->size;
2336dbda591dSKyongHo 		va->vm = tmp;
233768ad4a33SUladzislau Rezki (Sony) 		insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
2338822c18f2SIvan Kokshaysky 	}
2339ca23e405STejun Heo 
234068ad4a33SUladzislau Rezki (Sony) 	/*
234168ad4a33SUladzislau Rezki (Sony) 	 * Now we can initialize a free vmap space.
234268ad4a33SUladzislau Rezki (Sony) 	 */
234368ad4a33SUladzislau Rezki (Sony) 	vmap_init_free_space();
23449b463334SJeremy Fitzhardinge 	vmap_initialized = true;
2345db64fe02SNick Piggin }
2346db64fe02SNick Piggin 
2347e36176beSUladzislau Rezki (Sony) static inline void setup_vmalloc_vm_locked(struct vm_struct *vm,
2348e36176beSUladzislau Rezki (Sony) 	struct vmap_area *va, unsigned long flags, const void *caller)
2349cf88c790STejun Heo {
2350cf88c790STejun Heo 	vm->flags = flags;
2351cf88c790STejun Heo 	vm->addr = (void *)va->va_start;
2352cf88c790STejun Heo 	vm->size = va->va_end - va->va_start;
2353cf88c790STejun Heo 	vm->caller = caller;
2354db1aecafSMinchan Kim 	va->vm = vm;
2355e36176beSUladzislau Rezki (Sony) }
2356e36176beSUladzislau Rezki (Sony) 
2357e36176beSUladzislau Rezki (Sony) static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
2358e36176beSUladzislau Rezki (Sony) 			      unsigned long flags, const void *caller)
2359e36176beSUladzislau Rezki (Sony) {
2360e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
2361e36176beSUladzislau Rezki (Sony) 	setup_vmalloc_vm_locked(vm, va, flags, caller);
2362c69480adSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2363f5252e00SMitsuo Hayasaka }
2364cf88c790STejun Heo 
236520fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm)
2366f5252e00SMitsuo Hayasaka {
2367d4033afdSJoonsoo Kim 	/*
236820fc02b4SZhang Yanfei 	 * Before removing VM_UNINITIALIZED,
2369d4033afdSJoonsoo Kim 	 * we should make sure that vm has proper values.
2370d4033afdSJoonsoo Kim 	 * Pair with smp_rmb() in show_numa_info().
2371d4033afdSJoonsoo Kim 	 */
2372d4033afdSJoonsoo Kim 	smp_wmb();
237320fc02b4SZhang Yanfei 	vm->flags &= ~VM_UNINITIALIZED;
2374cf88c790STejun Heo }
2375cf88c790STejun Heo 
2376db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size,
23777ca3027bSDaniel Axtens 		unsigned long align, unsigned long shift, unsigned long flags,
23787ca3027bSDaniel Axtens 		unsigned long start, unsigned long end, int node,
23797ca3027bSDaniel Axtens 		gfp_t gfp_mask, const void *caller)
2380db64fe02SNick Piggin {
23810006526dSKautuk Consul 	struct vmap_area *va;
2382db64fe02SNick Piggin 	struct vm_struct *area;
2383d98c9e83SAndrey Ryabinin 	unsigned long requested_size = size;
23841da177e4SLinus Torvalds 
238552fd24caSGiridhar Pemmasani 	BUG_ON(in_interrupt());
23867ca3027bSDaniel Axtens 	size = ALIGN(size, 1ul << shift);
238731be8309SOGAWA Hirofumi 	if (unlikely(!size))
238831be8309SOGAWA Hirofumi 		return NULL;
23891da177e4SLinus Torvalds 
2390252e5c6eSzijun_hu 	if (flags & VM_IOREMAP)
2391252e5c6eSzijun_hu 		align = 1ul << clamp_t(int, get_count_order_long(size),
2392252e5c6eSzijun_hu 				       PAGE_SHIFT, IOREMAP_MAX_ORDER);
2393252e5c6eSzijun_hu 
2394cf88c790STejun Heo 	area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
23951da177e4SLinus Torvalds 	if (unlikely(!area))
23961da177e4SLinus Torvalds 		return NULL;
23971da177e4SLinus Torvalds 
239871394fe5SAndrey Ryabinin 	if (!(flags & VM_NO_GUARD))
23991da177e4SLinus Torvalds 		size += PAGE_SIZE;
24001da177e4SLinus Torvalds 
2401db64fe02SNick Piggin 	va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
2402db64fe02SNick Piggin 	if (IS_ERR(va)) {
2403db64fe02SNick Piggin 		kfree(area);
2404db64fe02SNick Piggin 		return NULL;
24051da177e4SLinus Torvalds 	}
24061da177e4SLinus Torvalds 
2407d98c9e83SAndrey Ryabinin 	kasan_unpoison_vmalloc((void *)va->va_start, requested_size);
2408f5252e00SMitsuo Hayasaka 
2409d98c9e83SAndrey Ryabinin 	setup_vmalloc_vm(area, va, flags, caller);
24103c5c3cfbSDaniel Axtens 
24111da177e4SLinus Torvalds 	return area;
24121da177e4SLinus Torvalds }
24131da177e4SLinus Torvalds 
2414c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
2415c2968612SBenjamin Herrenschmidt 				       unsigned long start, unsigned long end,
24165e6cafc8SMarek Szyprowski 				       const void *caller)
2417c2968612SBenjamin Herrenschmidt {
24187ca3027bSDaniel Axtens 	return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, start, end,
24197ca3027bSDaniel Axtens 				  NUMA_NO_NODE, GFP_KERNEL, caller);
2420c2968612SBenjamin Herrenschmidt }
2421c2968612SBenjamin Herrenschmidt 
24221da177e4SLinus Torvalds /**
2423183ff22bSSimon Arlott  * get_vm_area - reserve a contiguous kernel virtual area
24241da177e4SLinus Torvalds  * @size:	 size of the area
24251da177e4SLinus Torvalds  * @flags:	 %VM_IOREMAP for I/O mappings or VM_ALLOC
24261da177e4SLinus Torvalds  *
24271da177e4SLinus Torvalds  * Search an area of @size in the kernel virtual mapping area,
24281da177e4SLinus Torvalds  * and reserved it for out purposes.  Returns the area descriptor
24291da177e4SLinus Torvalds  * on success or %NULL on failure.
2430a862f68aSMike Rapoport  *
2431a862f68aSMike Rapoport  * Return: the area descriptor on success or %NULL on failure.
24321da177e4SLinus Torvalds  */
24331da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
24341da177e4SLinus Torvalds {
24357ca3027bSDaniel Axtens 	return __get_vm_area_node(size, 1, PAGE_SHIFT, flags,
24367ca3027bSDaniel Axtens 				  VMALLOC_START, VMALLOC_END,
243700ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL,
243800ef2d2fSDavid Rientjes 				  __builtin_return_address(0));
243923016969SChristoph Lameter }
244023016969SChristoph Lameter 
244123016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
24425e6cafc8SMarek Szyprowski 				const void *caller)
244323016969SChristoph Lameter {
24447ca3027bSDaniel Axtens 	return __get_vm_area_node(size, 1, PAGE_SHIFT, flags,
24457ca3027bSDaniel Axtens 				  VMALLOC_START, VMALLOC_END,
244600ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL, caller);
24471da177e4SLinus Torvalds }
24481da177e4SLinus Torvalds 
2449e9da6e99SMarek Szyprowski /**
2450e9da6e99SMarek Szyprowski  * find_vm_area - find a continuous kernel virtual area
2451e9da6e99SMarek Szyprowski  * @addr:	  base address
2452e9da6e99SMarek Szyprowski  *
2453e9da6e99SMarek Szyprowski  * Search for the kernel VM area starting at @addr, and return it.
2454e9da6e99SMarek Szyprowski  * It is up to the caller to do all required locking to keep the returned
2455e9da6e99SMarek Szyprowski  * pointer valid.
2456a862f68aSMike Rapoport  *
245774640617SHui Su  * Return: the area descriptor on success or %NULL on failure.
2458e9da6e99SMarek Szyprowski  */
2459e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr)
246083342314SNick Piggin {
2461db64fe02SNick Piggin 	struct vmap_area *va;
246283342314SNick Piggin 
2463db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
2464688fcbfcSPengfei Li 	if (!va)
24657856dfebSAndi Kleen 		return NULL;
2466688fcbfcSPengfei Li 
2467688fcbfcSPengfei Li 	return va->vm;
24687856dfebSAndi Kleen }
24697856dfebSAndi Kleen 
24701da177e4SLinus Torvalds /**
2471183ff22bSSimon Arlott  * remove_vm_area - find and remove a continuous kernel virtual area
24721da177e4SLinus Torvalds  * @addr:	    base address
24731da177e4SLinus Torvalds  *
24741da177e4SLinus Torvalds  * Search for the kernel VM area starting at @addr, and remove it.
24751da177e4SLinus Torvalds  * This function returns the found VM area, but using it is NOT safe
24767856dfebSAndi Kleen  * on SMP machines, except for its size or flags.
2477a862f68aSMike Rapoport  *
247874640617SHui Su  * Return: the area descriptor on success or %NULL on failure.
24791da177e4SLinus Torvalds  */
2480b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr)
24811da177e4SLinus Torvalds {
2482db64fe02SNick Piggin 	struct vmap_area *va;
2483db64fe02SNick Piggin 
24845803ed29SChristoph Hellwig 	might_sleep();
24855803ed29SChristoph Hellwig 
2486dd3b8353SUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
2487dd3b8353SUladzislau Rezki (Sony) 	va = __find_vmap_area((unsigned long)addr);
2488688fcbfcSPengfei Li 	if (va && va->vm) {
2489db1aecafSMinchan Kim 		struct vm_struct *vm = va->vm;
2490f5252e00SMitsuo Hayasaka 
2491c69480adSJoonsoo Kim 		va->vm = NULL;
2492c69480adSJoonsoo Kim 		spin_unlock(&vmap_area_lock);
2493c69480adSJoonsoo Kim 
2494a5af5aa8SAndrey Ryabinin 		kasan_free_shadow(vm);
2495dd32c279SKAMEZAWA Hiroyuki 		free_unmap_vmap_area(va);
2496dd32c279SKAMEZAWA Hiroyuki 
2497db64fe02SNick Piggin 		return vm;
2498db64fe02SNick Piggin 	}
2499dd3b8353SUladzislau Rezki (Sony) 
2500dd3b8353SUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
2501db64fe02SNick Piggin 	return NULL;
25021da177e4SLinus Torvalds }
25031da177e4SLinus Torvalds 
2504868b104dSRick Edgecombe static inline void set_area_direct_map(const struct vm_struct *area,
2505868b104dSRick Edgecombe 				       int (*set_direct_map)(struct page *page))
2506868b104dSRick Edgecombe {
2507868b104dSRick Edgecombe 	int i;
2508868b104dSRick Edgecombe 
2509121e6f32SNicholas Piggin 	/* HUGE_VMALLOC passes small pages to set_direct_map */
2510868b104dSRick Edgecombe 	for (i = 0; i < area->nr_pages; i++)
2511868b104dSRick Edgecombe 		if (page_address(area->pages[i]))
2512868b104dSRick Edgecombe 			set_direct_map(area->pages[i]);
2513868b104dSRick Edgecombe }
2514868b104dSRick Edgecombe 
2515868b104dSRick Edgecombe /* Handle removing and resetting vm mappings related to the vm_struct. */
2516868b104dSRick Edgecombe static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages)
2517868b104dSRick Edgecombe {
2518868b104dSRick Edgecombe 	unsigned long start = ULONG_MAX, end = 0;
2519121e6f32SNicholas Piggin 	unsigned int page_order = vm_area_page_order(area);
2520868b104dSRick Edgecombe 	int flush_reset = area->flags & VM_FLUSH_RESET_PERMS;
252131e67340SRick Edgecombe 	int flush_dmap = 0;
2522868b104dSRick Edgecombe 	int i;
2523868b104dSRick Edgecombe 
2524868b104dSRick Edgecombe 	remove_vm_area(area->addr);
2525868b104dSRick Edgecombe 
2526868b104dSRick Edgecombe 	/* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */
2527868b104dSRick Edgecombe 	if (!flush_reset)
2528868b104dSRick Edgecombe 		return;
2529868b104dSRick Edgecombe 
2530868b104dSRick Edgecombe 	/*
2531868b104dSRick Edgecombe 	 * If not deallocating pages, just do the flush of the VM area and
2532868b104dSRick Edgecombe 	 * return.
2533868b104dSRick Edgecombe 	 */
2534868b104dSRick Edgecombe 	if (!deallocate_pages) {
2535868b104dSRick Edgecombe 		vm_unmap_aliases();
2536868b104dSRick Edgecombe 		return;
2537868b104dSRick Edgecombe 	}
2538868b104dSRick Edgecombe 
2539868b104dSRick Edgecombe 	/*
2540868b104dSRick Edgecombe 	 * If execution gets here, flush the vm mapping and reset the direct
2541868b104dSRick Edgecombe 	 * map. Find the start and end range of the direct mappings to make sure
2542868b104dSRick Edgecombe 	 * the vm_unmap_aliases() flush includes the direct map.
2543868b104dSRick Edgecombe 	 */
2544121e6f32SNicholas Piggin 	for (i = 0; i < area->nr_pages; i += 1U << page_order) {
25458e41f872SRick Edgecombe 		unsigned long addr = (unsigned long)page_address(area->pages[i]);
25468e41f872SRick Edgecombe 		if (addr) {
2547121e6f32SNicholas Piggin 			unsigned long page_size;
2548121e6f32SNicholas Piggin 
2549121e6f32SNicholas Piggin 			page_size = PAGE_SIZE << page_order;
2550868b104dSRick Edgecombe 			start = min(addr, start);
2551121e6f32SNicholas Piggin 			end = max(addr + page_size, end);
255231e67340SRick Edgecombe 			flush_dmap = 1;
2553868b104dSRick Edgecombe 		}
2554868b104dSRick Edgecombe 	}
2555868b104dSRick Edgecombe 
2556868b104dSRick Edgecombe 	/*
2557868b104dSRick Edgecombe 	 * Set direct map to something invalid so that it won't be cached if
2558868b104dSRick Edgecombe 	 * there are any accesses after the TLB flush, then flush the TLB and
2559868b104dSRick Edgecombe 	 * reset the direct map permissions to the default.
2560868b104dSRick Edgecombe 	 */
2561868b104dSRick Edgecombe 	set_area_direct_map(area, set_direct_map_invalid_noflush);
256231e67340SRick Edgecombe 	_vm_unmap_aliases(start, end, flush_dmap);
2563868b104dSRick Edgecombe 	set_area_direct_map(area, set_direct_map_default_noflush);
2564868b104dSRick Edgecombe }
2565868b104dSRick Edgecombe 
2566b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages)
25671da177e4SLinus Torvalds {
25681da177e4SLinus Torvalds 	struct vm_struct *area;
25691da177e4SLinus Torvalds 
25701da177e4SLinus Torvalds 	if (!addr)
25711da177e4SLinus Torvalds 		return;
25721da177e4SLinus Torvalds 
2573e69e9d4aSHATAYAMA Daisuke 	if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
2574ab15d9b4SDan Carpenter 			addr))
25751da177e4SLinus Torvalds 		return;
25761da177e4SLinus Torvalds 
25776ade2032SLiviu Dudau 	area = find_vm_area(addr);
25781da177e4SLinus Torvalds 	if (unlikely(!area)) {
25794c8573e2SArjan van de Ven 		WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
25801da177e4SLinus Torvalds 				addr);
25811da177e4SLinus Torvalds 		return;
25821da177e4SLinus Torvalds 	}
25831da177e4SLinus Torvalds 
258405e3ff95SChintan Pandya 	debug_check_no_locks_freed(area->addr, get_vm_area_size(area));
258505e3ff95SChintan Pandya 	debug_check_no_obj_freed(area->addr, get_vm_area_size(area));
25869a11b49aSIngo Molnar 
2587c041098cSVincenzo Frascino 	kasan_poison_vmalloc(area->addr, get_vm_area_size(area));
25883c5c3cfbSDaniel Axtens 
2589868b104dSRick Edgecombe 	vm_remove_mappings(area, deallocate_pages);
2590868b104dSRick Edgecombe 
25911da177e4SLinus Torvalds 	if (deallocate_pages) {
2592121e6f32SNicholas Piggin 		unsigned int page_order = vm_area_page_order(area);
25931da177e4SLinus Torvalds 		int i;
25941da177e4SLinus Torvalds 
2595121e6f32SNicholas Piggin 		for (i = 0; i < area->nr_pages; i += 1U << page_order) {
2596bf53d6f8SChristoph Lameter 			struct page *page = area->pages[i];
2597bf53d6f8SChristoph Lameter 
2598bf53d6f8SChristoph Lameter 			BUG_ON(!page);
2599121e6f32SNicholas Piggin 			__free_pages(page, page_order);
2600a850e932SRafael Aquini 			cond_resched();
26011da177e4SLinus Torvalds 		}
260297105f0aSRoman Gushchin 		atomic_long_sub(area->nr_pages, &nr_vmalloc_pages);
26031da177e4SLinus Torvalds 
2604244d63eeSDavid Rientjes 		kvfree(area->pages);
26051da177e4SLinus Torvalds 	}
26061da177e4SLinus Torvalds 
26071da177e4SLinus Torvalds 	kfree(area);
26081da177e4SLinus Torvalds }
26091da177e4SLinus Torvalds 
2610bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr)
2611bf22e37aSAndrey Ryabinin {
2612bf22e37aSAndrey Ryabinin 	/*
2613bf22e37aSAndrey Ryabinin 	 * Use raw_cpu_ptr() because this can be called from preemptible
2614bf22e37aSAndrey Ryabinin 	 * context. Preemption is absolutely fine here, because the llist_add()
2615bf22e37aSAndrey Ryabinin 	 * implementation is lockless, so it works even if we are adding to
261673221d88SJeongtae Park 	 * another cpu's list. schedule_work() should be fine with this too.
2617bf22e37aSAndrey Ryabinin 	 */
2618bf22e37aSAndrey Ryabinin 	struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
2619bf22e37aSAndrey Ryabinin 
2620bf22e37aSAndrey Ryabinin 	if (llist_add((struct llist_node *)addr, &p->list))
2621bf22e37aSAndrey Ryabinin 		schedule_work(&p->wq);
2622bf22e37aSAndrey Ryabinin }
2623bf22e37aSAndrey Ryabinin 
2624bf22e37aSAndrey Ryabinin /**
2625bf22e37aSAndrey Ryabinin  * vfree_atomic - release memory allocated by vmalloc()
2626bf22e37aSAndrey Ryabinin  * @addr:	  memory base address
2627bf22e37aSAndrey Ryabinin  *
2628bf22e37aSAndrey Ryabinin  * This one is just like vfree() but can be called in any atomic context
2629bf22e37aSAndrey Ryabinin  * except NMIs.
2630bf22e37aSAndrey Ryabinin  */
2631bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr)
2632bf22e37aSAndrey Ryabinin {
2633bf22e37aSAndrey Ryabinin 	BUG_ON(in_nmi());
2634bf22e37aSAndrey Ryabinin 
2635bf22e37aSAndrey Ryabinin 	kmemleak_free(addr);
2636bf22e37aSAndrey Ryabinin 
2637bf22e37aSAndrey Ryabinin 	if (!addr)
2638bf22e37aSAndrey Ryabinin 		return;
2639bf22e37aSAndrey Ryabinin 	__vfree_deferred(addr);
2640bf22e37aSAndrey Ryabinin }
2641bf22e37aSAndrey Ryabinin 
2642c67dc624SRoman Penyaev static void __vfree(const void *addr)
2643c67dc624SRoman Penyaev {
2644c67dc624SRoman Penyaev 	if (unlikely(in_interrupt()))
2645c67dc624SRoman Penyaev 		__vfree_deferred(addr);
2646c67dc624SRoman Penyaev 	else
2647c67dc624SRoman Penyaev 		__vunmap(addr, 1);
2648c67dc624SRoman Penyaev }
2649c67dc624SRoman Penyaev 
26501da177e4SLinus Torvalds /**
2651fa307474SMatthew Wilcox (Oracle)  * vfree - Release memory allocated by vmalloc()
2652fa307474SMatthew Wilcox (Oracle)  * @addr:  Memory base address
26531da177e4SLinus Torvalds  *
2654fa307474SMatthew Wilcox (Oracle)  * Free the virtually continuous memory area starting at @addr, as obtained
2655fa307474SMatthew Wilcox (Oracle)  * from one of the vmalloc() family of APIs.  This will usually also free the
2656fa307474SMatthew Wilcox (Oracle)  * physical memory underlying the virtual allocation, but that memory is
2657fa307474SMatthew Wilcox (Oracle)  * reference counted, so it will not be freed until the last user goes away.
26581da177e4SLinus Torvalds  *
2659fa307474SMatthew Wilcox (Oracle)  * If @addr is NULL, no operation is performed.
266032fcfd40SAl Viro  *
2661fa307474SMatthew Wilcox (Oracle)  * Context:
26623ca4ea3aSAndrey Ryabinin  * May sleep if called *not* from interrupt context.
2663fa307474SMatthew Wilcox (Oracle)  * Must not be called in NMI context (strictly speaking, it could be
2664fa307474SMatthew Wilcox (Oracle)  * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
2665f0953a1bSIngo Molnar  * conventions for vfree() arch-dependent would be a really bad idea).
26661da177e4SLinus Torvalds  */
2667b3bdda02SChristoph Lameter void vfree(const void *addr)
26681da177e4SLinus Torvalds {
266932fcfd40SAl Viro 	BUG_ON(in_nmi());
267089219d37SCatalin Marinas 
267189219d37SCatalin Marinas 	kmemleak_free(addr);
267289219d37SCatalin Marinas 
2673a8dda165SAndrey Ryabinin 	might_sleep_if(!in_interrupt());
2674a8dda165SAndrey Ryabinin 
267532fcfd40SAl Viro 	if (!addr)
267632fcfd40SAl Viro 		return;
2677c67dc624SRoman Penyaev 
2678c67dc624SRoman Penyaev 	__vfree(addr);
26791da177e4SLinus Torvalds }
26801da177e4SLinus Torvalds EXPORT_SYMBOL(vfree);
26811da177e4SLinus Torvalds 
26821da177e4SLinus Torvalds /**
26831da177e4SLinus Torvalds  * vunmap - release virtual mapping obtained by vmap()
26841da177e4SLinus Torvalds  * @addr:   memory base address
26851da177e4SLinus Torvalds  *
26861da177e4SLinus Torvalds  * Free the virtually contiguous memory area starting at @addr,
26871da177e4SLinus Torvalds  * which was created from the page array passed to vmap().
26881da177e4SLinus Torvalds  *
268980e93effSPekka Enberg  * Must not be called in interrupt context.
26901da177e4SLinus Torvalds  */
2691b3bdda02SChristoph Lameter void vunmap(const void *addr)
26921da177e4SLinus Torvalds {
26931da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
269434754b69SPeter Zijlstra 	might_sleep();
269532fcfd40SAl Viro 	if (addr)
26961da177e4SLinus Torvalds 		__vunmap(addr, 0);
26971da177e4SLinus Torvalds }
26981da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap);
26991da177e4SLinus Torvalds 
27001da177e4SLinus Torvalds /**
27011da177e4SLinus Torvalds  * vmap - map an array of pages into virtually contiguous space
27021da177e4SLinus Torvalds  * @pages: array of page pointers
27031da177e4SLinus Torvalds  * @count: number of pages to map
27041da177e4SLinus Torvalds  * @flags: vm_area->flags
27051da177e4SLinus Torvalds  * @prot: page protection for the mapping
27061da177e4SLinus Torvalds  *
2707b944afc9SChristoph Hellwig  * Maps @count pages from @pages into contiguous kernel virtual space.
2708b944afc9SChristoph Hellwig  * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself
2709b944afc9SChristoph Hellwig  * (which must be kmalloc or vmalloc memory) and one reference per pages in it
2710b944afc9SChristoph Hellwig  * are transferred from the caller to vmap(), and will be freed / dropped when
2711b944afc9SChristoph Hellwig  * vfree() is called on the return value.
2712a862f68aSMike Rapoport  *
2713a862f68aSMike Rapoport  * Return: the address of the area or %NULL on failure
27141da177e4SLinus Torvalds  */
27151da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count,
27161da177e4SLinus Torvalds 	   unsigned long flags, pgprot_t prot)
27171da177e4SLinus Torvalds {
27181da177e4SLinus Torvalds 	struct vm_struct *area;
2719b67177ecSNicholas Piggin 	unsigned long addr;
272065ee03c4SGuillermo Julián Moreno 	unsigned long size;		/* In bytes */
27211da177e4SLinus Torvalds 
272234754b69SPeter Zijlstra 	might_sleep();
272334754b69SPeter Zijlstra 
2724ca79b0c2SArun KS 	if (count > totalram_pages())
27251da177e4SLinus Torvalds 		return NULL;
27261da177e4SLinus Torvalds 
272765ee03c4SGuillermo Julián Moreno 	size = (unsigned long)count << PAGE_SHIFT;
272865ee03c4SGuillermo Julián Moreno 	area = get_vm_area_caller(size, flags, __builtin_return_address(0));
27291da177e4SLinus Torvalds 	if (!area)
27301da177e4SLinus Torvalds 		return NULL;
273123016969SChristoph Lameter 
2732b67177ecSNicholas Piggin 	addr = (unsigned long)area->addr;
2733b67177ecSNicholas Piggin 	if (vmap_pages_range(addr, addr + size, pgprot_nx(prot),
2734b67177ecSNicholas Piggin 				pages, PAGE_SHIFT) < 0) {
27351da177e4SLinus Torvalds 		vunmap(area->addr);
27361da177e4SLinus Torvalds 		return NULL;
27371da177e4SLinus Torvalds 	}
27381da177e4SLinus Torvalds 
2739c22ee528SMiaohe Lin 	if (flags & VM_MAP_PUT_PAGES) {
2740b944afc9SChristoph Hellwig 		area->pages = pages;
2741c22ee528SMiaohe Lin 		area->nr_pages = count;
2742c22ee528SMiaohe Lin 	}
27431da177e4SLinus Torvalds 	return area->addr;
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds EXPORT_SYMBOL(vmap);
27461da177e4SLinus Torvalds 
27473e9a9e25SChristoph Hellwig #ifdef CONFIG_VMAP_PFN
27483e9a9e25SChristoph Hellwig struct vmap_pfn_data {
27493e9a9e25SChristoph Hellwig 	unsigned long	*pfns;
27503e9a9e25SChristoph Hellwig 	pgprot_t	prot;
27513e9a9e25SChristoph Hellwig 	unsigned int	idx;
27523e9a9e25SChristoph Hellwig };
27533e9a9e25SChristoph Hellwig 
27543e9a9e25SChristoph Hellwig static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private)
27553e9a9e25SChristoph Hellwig {
27563e9a9e25SChristoph Hellwig 	struct vmap_pfn_data *data = private;
27573e9a9e25SChristoph Hellwig 
27583e9a9e25SChristoph Hellwig 	if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx])))
27593e9a9e25SChristoph Hellwig 		return -EINVAL;
27603e9a9e25SChristoph Hellwig 	*pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot));
27613e9a9e25SChristoph Hellwig 	return 0;
27623e9a9e25SChristoph Hellwig }
27633e9a9e25SChristoph Hellwig 
27643e9a9e25SChristoph Hellwig /**
27653e9a9e25SChristoph Hellwig  * vmap_pfn - map an array of PFNs into virtually contiguous space
27663e9a9e25SChristoph Hellwig  * @pfns: array of PFNs
27673e9a9e25SChristoph Hellwig  * @count: number of pages to map
27683e9a9e25SChristoph Hellwig  * @prot: page protection for the mapping
27693e9a9e25SChristoph Hellwig  *
27703e9a9e25SChristoph Hellwig  * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns
27713e9a9e25SChristoph Hellwig  * the start address of the mapping.
27723e9a9e25SChristoph Hellwig  */
27733e9a9e25SChristoph Hellwig void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
27743e9a9e25SChristoph Hellwig {
27753e9a9e25SChristoph Hellwig 	struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) };
27763e9a9e25SChristoph Hellwig 	struct vm_struct *area;
27773e9a9e25SChristoph Hellwig 
27783e9a9e25SChristoph Hellwig 	area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP,
27793e9a9e25SChristoph Hellwig 			__builtin_return_address(0));
27803e9a9e25SChristoph Hellwig 	if (!area)
27813e9a9e25SChristoph Hellwig 		return NULL;
27823e9a9e25SChristoph Hellwig 	if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
27833e9a9e25SChristoph Hellwig 			count * PAGE_SIZE, vmap_pfn_apply, &data)) {
27843e9a9e25SChristoph Hellwig 		free_vm_area(area);
27853e9a9e25SChristoph Hellwig 		return NULL;
27863e9a9e25SChristoph Hellwig 	}
27873e9a9e25SChristoph Hellwig 	return area->addr;
27883e9a9e25SChristoph Hellwig }
27893e9a9e25SChristoph Hellwig EXPORT_SYMBOL_GPL(vmap_pfn);
27903e9a9e25SChristoph Hellwig #endif /* CONFIG_VMAP_PFN */
27913e9a9e25SChristoph Hellwig 
279212b9f873SUladzislau Rezki static inline unsigned int
279312b9f873SUladzislau Rezki vm_area_alloc_pages(gfp_t gfp, int nid,
279412b9f873SUladzislau Rezki 		unsigned int order, unsigned long nr_pages, struct page **pages)
279512b9f873SUladzislau Rezki {
279612b9f873SUladzislau Rezki 	unsigned int nr_allocated = 0;
279712b9f873SUladzislau Rezki 
279812b9f873SUladzislau Rezki 	/*
279912b9f873SUladzislau Rezki 	 * For order-0 pages we make use of bulk allocator, if
280012b9f873SUladzislau Rezki 	 * the page array is partly or not at all populated due
280112b9f873SUladzislau Rezki 	 * to fails, fallback to a single page allocator that is
280212b9f873SUladzislau Rezki 	 * more permissive.
280312b9f873SUladzislau Rezki 	 */
280412b9f873SUladzislau Rezki 	if (!order)
280512b9f873SUladzislau Rezki 		nr_allocated = alloc_pages_bulk_array_node(
280612b9f873SUladzislau Rezki 			gfp, nid, nr_pages, pages);
280712b9f873SUladzislau Rezki 	else
280812b9f873SUladzislau Rezki 		/*
280912b9f873SUladzislau Rezki 		 * Compound pages required for remap_vmalloc_page if
281012b9f873SUladzislau Rezki 		 * high-order pages.
281112b9f873SUladzislau Rezki 		 */
281212b9f873SUladzislau Rezki 		gfp |= __GFP_COMP;
281312b9f873SUladzislau Rezki 
281412b9f873SUladzislau Rezki 	/* High-order pages or fallback path if "bulk" fails. */
281512b9f873SUladzislau Rezki 	while (nr_allocated < nr_pages) {
281612b9f873SUladzislau Rezki 		struct page *page;
281712b9f873SUladzislau Rezki 		int i;
281812b9f873SUladzislau Rezki 
281912b9f873SUladzislau Rezki 		page = alloc_pages_node(nid, gfp, order);
282012b9f873SUladzislau Rezki 		if (unlikely(!page))
282112b9f873SUladzislau Rezki 			break;
282212b9f873SUladzislau Rezki 
282312b9f873SUladzislau Rezki 		/*
282412b9f873SUladzislau Rezki 		 * Careful, we allocate and map page-order pages, but
282512b9f873SUladzislau Rezki 		 * tracking is done per PAGE_SIZE page so as to keep the
282612b9f873SUladzislau Rezki 		 * vm_struct APIs independent of the physical/mapped size.
282712b9f873SUladzislau Rezki 		 */
282812b9f873SUladzislau Rezki 		for (i = 0; i < (1U << order); i++)
282912b9f873SUladzislau Rezki 			pages[nr_allocated + i] = page + i;
283012b9f873SUladzislau Rezki 
283112b9f873SUladzislau Rezki 		if (gfpflags_allow_blocking(gfp))
283212b9f873SUladzislau Rezki 			cond_resched();
283312b9f873SUladzislau Rezki 
283412b9f873SUladzislau Rezki 		nr_allocated += 1U << order;
283512b9f873SUladzislau Rezki 	}
283612b9f873SUladzislau Rezki 
283712b9f873SUladzislau Rezki 	return nr_allocated;
283812b9f873SUladzislau Rezki }
283912b9f873SUladzislau Rezki 
2840e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
2841121e6f32SNicholas Piggin 				 pgprot_t prot, unsigned int page_shift,
2842121e6f32SNicholas Piggin 				 int node)
28431da177e4SLinus Torvalds {
2844930f036bSDavid Rientjes 	const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
2845121e6f32SNicholas Piggin 	unsigned long addr = (unsigned long)area->addr;
2846121e6f32SNicholas Piggin 	unsigned long size = get_vm_area_size(area);
284734fe6537SAndrew Morton 	unsigned long array_size;
2848121e6f32SNicholas Piggin 	unsigned int nr_small_pages = size >> PAGE_SHIFT;
2849121e6f32SNicholas Piggin 	unsigned int page_order;
28501da177e4SLinus Torvalds 
2851121e6f32SNicholas Piggin 	array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
2852f255935bSChristoph Hellwig 	gfp_mask |= __GFP_NOWARN;
2853f255935bSChristoph Hellwig 	if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
2854f255935bSChristoph Hellwig 		gfp_mask |= __GFP_HIGHMEM;
28551da177e4SLinus Torvalds 
28561da177e4SLinus Torvalds 	/* Please note that the recursion is strictly bounded. */
28578757d5faSJan Kiszka 	if (array_size > PAGE_SIZE) {
28585c1f4e69SUladzislau Rezki (Sony) 		area->pages = __vmalloc_node(array_size, 1, nested_gfp, node,
2859f255935bSChristoph Hellwig 					area->caller);
2860286e1ea3SAndrew Morton 	} else {
28615c1f4e69SUladzislau Rezki (Sony) 		area->pages = kmalloc_node(array_size, nested_gfp, node);
2862286e1ea3SAndrew Morton 	}
28637ea36242SAustin Kim 
28645c1f4e69SUladzislau Rezki (Sony) 	if (!area->pages) {
2865d70bec8cSNicholas Piggin 		warn_alloc(gfp_mask, NULL,
2866f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, failed to allocated page array size %lu",
2867d70bec8cSNicholas Piggin 			nr_small_pages * PAGE_SIZE, array_size);
2868cd61413bSUladzislau Rezki (Sony) 		free_vm_area(area);
28691da177e4SLinus Torvalds 		return NULL;
28701da177e4SLinus Torvalds 	}
28711da177e4SLinus Torvalds 
2872121e6f32SNicholas Piggin 	set_vm_area_page_order(area, page_shift - PAGE_SHIFT);
2873121e6f32SNicholas Piggin 	page_order = vm_area_page_order(area);
2874121e6f32SNicholas Piggin 
287512b9f873SUladzislau Rezki 	area->nr_pages = vm_area_alloc_pages(gfp_mask, node,
287612b9f873SUladzislau Rezki 		page_order, nr_small_pages, area->pages);
28775c1f4e69SUladzislau Rezki (Sony) 
287897105f0aSRoman Gushchin 	atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
28795c1f4e69SUladzislau Rezki (Sony) 
28805c1f4e69SUladzislau Rezki (Sony) 	/*
28815c1f4e69SUladzislau Rezki (Sony) 	 * If not enough pages were obtained to accomplish an
28825c1f4e69SUladzislau Rezki (Sony) 	 * allocation request, free them via __vfree() if any.
28835c1f4e69SUladzislau Rezki (Sony) 	 */
28845c1f4e69SUladzislau Rezki (Sony) 	if (area->nr_pages != nr_small_pages) {
2885d70bec8cSNicholas Piggin 		warn_alloc(gfp_mask, NULL,
2886f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, page order %u, failed to allocate pages",
2887d70bec8cSNicholas Piggin 			area->nr_pages * PAGE_SIZE, page_order);
28881da177e4SLinus Torvalds 		goto fail;
28891da177e4SLinus Torvalds 	}
2890121e6f32SNicholas Piggin 
289112b9f873SUladzislau Rezki 	if (vmap_pages_range(addr, addr + size, prot, area->pages,
289212b9f873SUladzislau Rezki 			page_shift) < 0) {
2893d70bec8cSNicholas Piggin 		warn_alloc(gfp_mask, NULL,
2894f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, failed to map pages",
2895d70bec8cSNicholas Piggin 			area->nr_pages * PAGE_SIZE);
28961da177e4SLinus Torvalds 		goto fail;
2897d70bec8cSNicholas Piggin 	}
2898ed1f324cSChristoph Hellwig 
28991da177e4SLinus Torvalds 	return area->addr;
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds fail:
2902c67dc624SRoman Penyaev 	__vfree(area->addr);
29031da177e4SLinus Torvalds 	return NULL;
29041da177e4SLinus Torvalds }
29051da177e4SLinus Torvalds 
2906d0a21265SDavid Rientjes /**
2907d0a21265SDavid Rientjes  * __vmalloc_node_range - allocate virtually contiguous memory
2908d0a21265SDavid Rientjes  * @size:		  allocation size
2909d0a21265SDavid Rientjes  * @align:		  desired alignment
2910d0a21265SDavid Rientjes  * @start:		  vm area range start
2911d0a21265SDavid Rientjes  * @end:		  vm area range end
2912d0a21265SDavid Rientjes  * @gfp_mask:		  flags for the page level allocator
2913d0a21265SDavid Rientjes  * @prot:		  protection mask for the allocated pages
2914cb9e3c29SAndrey Ryabinin  * @vm_flags:		  additional vm area flags (e.g. %VM_NO_GUARD)
291500ef2d2fSDavid Rientjes  * @node:		  node to use for allocation or NUMA_NO_NODE
2916d0a21265SDavid Rientjes  * @caller:		  caller's return address
2917d0a21265SDavid Rientjes  *
2918d0a21265SDavid Rientjes  * Allocate enough pages to cover @size from the page level
2919d0a21265SDavid Rientjes  * allocator with @gfp_mask flags.  Map them into contiguous
2920d0a21265SDavid Rientjes  * kernel virtual space, using a pagetable protection of @prot.
2921a862f68aSMike Rapoport  *
2922a862f68aSMike Rapoport  * Return: the address of the area or %NULL on failure
2923d0a21265SDavid Rientjes  */
2924d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align,
2925d0a21265SDavid Rientjes 			unsigned long start, unsigned long end, gfp_t gfp_mask,
2926cb9e3c29SAndrey Ryabinin 			pgprot_t prot, unsigned long vm_flags, int node,
2927cb9e3c29SAndrey Ryabinin 			const void *caller)
2928930fc45aSChristoph Lameter {
2929d0a21265SDavid Rientjes 	struct vm_struct *area;
2930d0a21265SDavid Rientjes 	void *addr;
2931d0a21265SDavid Rientjes 	unsigned long real_size = size;
2932121e6f32SNicholas Piggin 	unsigned long real_align = align;
2933121e6f32SNicholas Piggin 	unsigned int shift = PAGE_SHIFT;
2934d0a21265SDavid Rientjes 
2935d70bec8cSNicholas Piggin 	if (WARN_ON_ONCE(!size))
2936d70bec8cSNicholas Piggin 		return NULL;
2937d70bec8cSNicholas Piggin 
2938d70bec8cSNicholas Piggin 	if ((size >> PAGE_SHIFT) > totalram_pages()) {
2939d70bec8cSNicholas Piggin 		warn_alloc(gfp_mask, NULL,
2940f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, exceeds total pages",
2941f4bdfeafSUladzislau Rezki (Sony) 			real_size);
2942d70bec8cSNicholas Piggin 		return NULL;
2943121e6f32SNicholas Piggin 	}
2944d0a21265SDavid Rientjes 
29453382bbeeSChristophe Leroy 	if (vmap_allow_huge && !(vm_flags & VM_NO_HUGE_VMAP)) {
2946121e6f32SNicholas Piggin 		unsigned long size_per_node;
2947121e6f32SNicholas Piggin 
2948121e6f32SNicholas Piggin 		/*
2949121e6f32SNicholas Piggin 		 * Try huge pages. Only try for PAGE_KERNEL allocations,
2950121e6f32SNicholas Piggin 		 * others like modules don't yet expect huge pages in
2951121e6f32SNicholas Piggin 		 * their allocations due to apply_to_page_range not
2952121e6f32SNicholas Piggin 		 * supporting them.
2953121e6f32SNicholas Piggin 		 */
2954121e6f32SNicholas Piggin 
2955121e6f32SNicholas Piggin 		size_per_node = size;
2956121e6f32SNicholas Piggin 		if (node == NUMA_NO_NODE)
2957121e6f32SNicholas Piggin 			size_per_node /= num_online_nodes();
29583382bbeeSChristophe Leroy 		if (arch_vmap_pmd_supported(prot) && size_per_node >= PMD_SIZE)
2959121e6f32SNicholas Piggin 			shift = PMD_SHIFT;
29603382bbeeSChristophe Leroy 		else
29613382bbeeSChristophe Leroy 			shift = arch_vmap_pte_supported_shift(size_per_node);
29623382bbeeSChristophe Leroy 
2963121e6f32SNicholas Piggin 		align = max(real_align, 1UL << shift);
2964121e6f32SNicholas Piggin 		size = ALIGN(real_size, 1UL << shift);
2965121e6f32SNicholas Piggin 	}
2966121e6f32SNicholas Piggin 
2967121e6f32SNicholas Piggin again:
29687ca3027bSDaniel Axtens 	area = __get_vm_area_node(real_size, align, shift, VM_ALLOC |
29697ca3027bSDaniel Axtens 				  VM_UNINITIALIZED | vm_flags, start, end, node,
29707ca3027bSDaniel Axtens 				  gfp_mask, caller);
2971d70bec8cSNicholas Piggin 	if (!area) {
2972d70bec8cSNicholas Piggin 		warn_alloc(gfp_mask, NULL,
2973f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, vm_struct allocation failed",
2974f4bdfeafSUladzislau Rezki (Sony) 			real_size);
2975de7d2b56SJoe Perches 		goto fail;
2976d70bec8cSNicholas Piggin 	}
2977d0a21265SDavid Rientjes 
2978121e6f32SNicholas Piggin 	addr = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
29791368edf0SMel Gorman 	if (!addr)
2980121e6f32SNicholas Piggin 		goto fail;
298189219d37SCatalin Marinas 
298289219d37SCatalin Marinas 	/*
298320fc02b4SZhang Yanfei 	 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
298420fc02b4SZhang Yanfei 	 * flag. It means that vm_struct is not fully initialized.
29854341fa45SJoonsoo Kim 	 * Now, it is fully initialized, so remove this flag here.
2986f5252e00SMitsuo Hayasaka 	 */
298720fc02b4SZhang Yanfei 	clear_vm_uninitialized_flag(area);
2988f5252e00SMitsuo Hayasaka 
29897ca3027bSDaniel Axtens 	size = PAGE_ALIGN(size);
299094f4a161SCatalin Marinas 	kmemleak_vmalloc(area, size, gfp_mask);
299189219d37SCatalin Marinas 
299289219d37SCatalin Marinas 	return addr;
2993de7d2b56SJoe Perches 
2994de7d2b56SJoe Perches fail:
2995121e6f32SNicholas Piggin 	if (shift > PAGE_SHIFT) {
2996121e6f32SNicholas Piggin 		shift = PAGE_SHIFT;
2997121e6f32SNicholas Piggin 		align = real_align;
2998121e6f32SNicholas Piggin 		size = real_size;
2999121e6f32SNicholas Piggin 		goto again;
3000121e6f32SNicholas Piggin 	}
3001121e6f32SNicholas Piggin 
3002de7d2b56SJoe Perches 	return NULL;
3003930fc45aSChristoph Lameter }
3004930fc45aSChristoph Lameter 
30051da177e4SLinus Torvalds /**
3006930fc45aSChristoph Lameter  * __vmalloc_node - allocate virtually contiguous memory
30071da177e4SLinus Torvalds  * @size:	    allocation size
30082dca6999SDavid Miller  * @align:	    desired alignment
30091da177e4SLinus Torvalds  * @gfp_mask:	    flags for the page level allocator
301000ef2d2fSDavid Rientjes  * @node:	    node to use for allocation or NUMA_NO_NODE
3011c85d194bSRandy Dunlap  * @caller:	    caller's return address
30121da177e4SLinus Torvalds  *
3013f38fcb9cSChristoph Hellwig  * Allocate enough pages to cover @size from the page level allocator with
3014f38fcb9cSChristoph Hellwig  * @gfp_mask flags.  Map them into contiguous kernel virtual space.
3015a7c3e901SMichal Hocko  *
3016dcda9b04SMichal Hocko  * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
3017a7c3e901SMichal Hocko  * and __GFP_NOFAIL are not supported
3018a7c3e901SMichal Hocko  *
3019a7c3e901SMichal Hocko  * Any use of gfp flags outside of GFP_KERNEL should be consulted
3020a7c3e901SMichal Hocko  * with mm people.
3021a862f68aSMike Rapoport  *
3022a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
30231da177e4SLinus Torvalds  */
30242b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align,
3025f38fcb9cSChristoph Hellwig 			    gfp_t gfp_mask, int node, const void *caller)
30261da177e4SLinus Torvalds {
3027d0a21265SDavid Rientjes 	return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
3028f38fcb9cSChristoph Hellwig 				gfp_mask, PAGE_KERNEL, 0, node, caller);
30291da177e4SLinus Torvalds }
3030c3f896dcSChristoph Hellwig /*
3031c3f896dcSChristoph Hellwig  * This is only for performance analysis of vmalloc and stress purpose.
3032c3f896dcSChristoph Hellwig  * It is required by vmalloc test module, therefore do not use it other
3033c3f896dcSChristoph Hellwig  * than that.
3034c3f896dcSChristoph Hellwig  */
3035c3f896dcSChristoph Hellwig #ifdef CONFIG_TEST_VMALLOC_MODULE
3036c3f896dcSChristoph Hellwig EXPORT_SYMBOL_GPL(__vmalloc_node);
3037c3f896dcSChristoph Hellwig #endif
30381da177e4SLinus Torvalds 
303988dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask)
3040930fc45aSChristoph Lameter {
3041f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE,
304223016969SChristoph Lameter 				__builtin_return_address(0));
3043930fc45aSChristoph Lameter }
30441da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc);
30451da177e4SLinus Torvalds 
30461da177e4SLinus Torvalds /**
30471da177e4SLinus Torvalds  * vmalloc - allocate virtually contiguous memory
30481da177e4SLinus Torvalds  * @size:    allocation size
304992eac168SMike Rapoport  *
30501da177e4SLinus Torvalds  * Allocate enough pages to cover @size from the page level
30511da177e4SLinus Torvalds  * allocator and map them into contiguous kernel virtual space.
30521da177e4SLinus Torvalds  *
3053c1c8897fSMichael Opdenacker  * For tight control over page level allocator and protection flags
30541da177e4SLinus Torvalds  * use __vmalloc() instead.
3055a862f68aSMike Rapoport  *
3056a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
30571da177e4SLinus Torvalds  */
30581da177e4SLinus Torvalds void *vmalloc(unsigned long size)
30591da177e4SLinus Torvalds {
30604d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE,
30614d39d728SChristoph Hellwig 				__builtin_return_address(0));
30621da177e4SLinus Torvalds }
30631da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc);
30641da177e4SLinus Torvalds 
3065930fc45aSChristoph Lameter /**
306615a64f5aSClaudio Imbrenda  * vmalloc_no_huge - allocate virtually contiguous memory using small pages
306715a64f5aSClaudio Imbrenda  * @size:    allocation size
306815a64f5aSClaudio Imbrenda  *
306915a64f5aSClaudio Imbrenda  * Allocate enough non-huge pages to cover @size from the page level
307015a64f5aSClaudio Imbrenda  * allocator and map them into contiguous kernel virtual space.
307115a64f5aSClaudio Imbrenda  *
307215a64f5aSClaudio Imbrenda  * Return: pointer to the allocated memory or %NULL on error
307315a64f5aSClaudio Imbrenda  */
307415a64f5aSClaudio Imbrenda void *vmalloc_no_huge(unsigned long size)
307515a64f5aSClaudio Imbrenda {
307615a64f5aSClaudio Imbrenda 	return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
307715a64f5aSClaudio Imbrenda 				    GFP_KERNEL, PAGE_KERNEL, VM_NO_HUGE_VMAP,
307815a64f5aSClaudio Imbrenda 				    NUMA_NO_NODE, __builtin_return_address(0));
307915a64f5aSClaudio Imbrenda }
308015a64f5aSClaudio Imbrenda EXPORT_SYMBOL(vmalloc_no_huge);
308115a64f5aSClaudio Imbrenda 
308215a64f5aSClaudio Imbrenda /**
3083e1ca7788SDave Young  * vzalloc - allocate virtually contiguous memory with zero fill
3084e1ca7788SDave Young  * @size:    allocation size
308592eac168SMike Rapoport  *
3086e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
3087e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
3088e1ca7788SDave Young  * The memory allocated is set to zero.
3089e1ca7788SDave Young  *
3090e1ca7788SDave Young  * For tight control over page level allocator and protection flags
3091e1ca7788SDave Young  * use __vmalloc() instead.
3092a862f68aSMike Rapoport  *
3093a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
3094e1ca7788SDave Young  */
3095e1ca7788SDave Young void *vzalloc(unsigned long size)
3096e1ca7788SDave Young {
30974d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE,
30984d39d728SChristoph Hellwig 				__builtin_return_address(0));
3099e1ca7788SDave Young }
3100e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
3101e1ca7788SDave Young 
3102e1ca7788SDave Young /**
3103ead04089SRolf Eike Beer  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
310483342314SNick Piggin  * @size: allocation size
3105ead04089SRolf Eike Beer  *
3106ead04089SRolf Eike Beer  * The resulting memory area is zeroed so it can be mapped to userspace
3107ead04089SRolf Eike Beer  * without leaking data.
3108a862f68aSMike Rapoport  *
3109a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
311083342314SNick Piggin  */
311183342314SNick Piggin void *vmalloc_user(unsigned long size)
311283342314SNick Piggin {
3113bc84c535SRoman Penyaev 	return __vmalloc_node_range(size, SHMLBA,  VMALLOC_START, VMALLOC_END,
3114bc84c535SRoman Penyaev 				    GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL,
3115bc84c535SRoman Penyaev 				    VM_USERMAP, NUMA_NO_NODE,
311600ef2d2fSDavid Rientjes 				    __builtin_return_address(0));
311783342314SNick Piggin }
311883342314SNick Piggin EXPORT_SYMBOL(vmalloc_user);
311983342314SNick Piggin 
312083342314SNick Piggin /**
3121930fc45aSChristoph Lameter  * vmalloc_node - allocate memory on a specific node
3122930fc45aSChristoph Lameter  * @size:	  allocation size
3123d44e0780SRandy Dunlap  * @node:	  numa node
3124930fc45aSChristoph Lameter  *
3125930fc45aSChristoph Lameter  * Allocate enough pages to cover @size from the page level
3126930fc45aSChristoph Lameter  * allocator and map them into contiguous kernel virtual space.
3127930fc45aSChristoph Lameter  *
3128c1c8897fSMichael Opdenacker  * For tight control over page level allocator and protection flags
3129930fc45aSChristoph Lameter  * use __vmalloc() instead.
3130a862f68aSMike Rapoport  *
3131a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
3132930fc45aSChristoph Lameter  */
3133930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node)
3134930fc45aSChristoph Lameter {
3135f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL, node,
3136f38fcb9cSChristoph Hellwig 			__builtin_return_address(0));
3137930fc45aSChristoph Lameter }
3138930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node);
3139930fc45aSChristoph Lameter 
3140e1ca7788SDave Young /**
3141e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
3142e1ca7788SDave Young  * @size:	allocation size
3143e1ca7788SDave Young  * @node:	numa node
3144e1ca7788SDave Young  *
3145e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
3146e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
3147e1ca7788SDave Young  * The memory allocated is set to zero.
3148e1ca7788SDave Young  *
3149a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
3150e1ca7788SDave Young  */
3151e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
3152e1ca7788SDave Young {
31534d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node,
31544d39d728SChristoph Hellwig 				__builtin_return_address(0));
3155e1ca7788SDave Young }
3156e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
3157e1ca7788SDave Young 
31580d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
3159698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
31600d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
3161698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
31620d08e0d3SAndi Kleen #else
3163698d0831SMichal Hocko /*
3164698d0831SMichal Hocko  * 64b systems should always have either DMA or DMA32 zones. For others
3165698d0831SMichal Hocko  * GFP_DMA32 should do the right thing and use the normal zone.
3166698d0831SMichal Hocko  */
316768d68ff6SZhiyuan Dai #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
31680d08e0d3SAndi Kleen #endif
31690d08e0d3SAndi Kleen 
31701da177e4SLinus Torvalds /**
31711da177e4SLinus Torvalds  * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
31721da177e4SLinus Torvalds  * @size:	allocation size
31731da177e4SLinus Torvalds  *
31741da177e4SLinus Torvalds  * Allocate enough 32bit PA addressable pages to cover @size from the
31751da177e4SLinus Torvalds  * page level allocator and map them into contiguous kernel virtual space.
3176a862f68aSMike Rapoport  *
3177a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
31781da177e4SLinus Torvalds  */
31791da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
31801da177e4SLinus Torvalds {
3181f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE,
3182f38fcb9cSChristoph Hellwig 			__builtin_return_address(0));
31831da177e4SLinus Torvalds }
31841da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32);
31851da177e4SLinus Torvalds 
318683342314SNick Piggin /**
3187ead04089SRolf Eike Beer  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
318883342314SNick Piggin  * @size:	     allocation size
3189ead04089SRolf Eike Beer  *
3190ead04089SRolf Eike Beer  * The resulting memory area is 32bit addressable and zeroed so it can be
3191ead04089SRolf Eike Beer  * mapped to userspace without leaking data.
3192a862f68aSMike Rapoport  *
3193a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
319483342314SNick Piggin  */
319583342314SNick Piggin void *vmalloc_32_user(unsigned long size)
319683342314SNick Piggin {
3197bc84c535SRoman Penyaev 	return __vmalloc_node_range(size, SHMLBA,  VMALLOC_START, VMALLOC_END,
3198bc84c535SRoman Penyaev 				    GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
3199bc84c535SRoman Penyaev 				    VM_USERMAP, NUMA_NO_NODE,
32005a82ac71SRoman Penyaev 				    __builtin_return_address(0));
320183342314SNick Piggin }
320283342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user);
320383342314SNick Piggin 
3204d0107eb0SKAMEZAWA Hiroyuki /*
3205d0107eb0SKAMEZAWA Hiroyuki  * small helper routine , copy contents to buf from addr.
3206d0107eb0SKAMEZAWA Hiroyuki  * If the page is not present, fill zero.
3207d0107eb0SKAMEZAWA Hiroyuki  */
3208d0107eb0SKAMEZAWA Hiroyuki 
3209d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count)
3210d0107eb0SKAMEZAWA Hiroyuki {
3211d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
3212d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
3213d0107eb0SKAMEZAWA Hiroyuki 
3214d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
3215d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
3216d0107eb0SKAMEZAWA Hiroyuki 
3217891c49abSAlexander Kuleshov 		offset = offset_in_page(addr);
3218d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
3219d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
3220d0107eb0SKAMEZAWA Hiroyuki 			length = count;
3221d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
3222d0107eb0SKAMEZAWA Hiroyuki 		/*
3223d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
3224d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
3225f0953a1bSIngo Molnar 		 * overhead of vmalloc()/vfree() calls for this _debug_
3226d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
3227d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
3228d0107eb0SKAMEZAWA Hiroyuki 		 */
3229d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
3230f7c8ce44SDavid Hildenbrand 			/* We can expect USER0 is not used -- see vread() */
32319b04c5feSCong Wang 			void *map = kmap_atomic(p);
3232d0107eb0SKAMEZAWA Hiroyuki 			memcpy(buf, map + offset, length);
32339b04c5feSCong Wang 			kunmap_atomic(map);
3234d0107eb0SKAMEZAWA Hiroyuki 		} else
3235d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, length);
3236d0107eb0SKAMEZAWA Hiroyuki 
3237d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
3238d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
3239d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
3240d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
3241d0107eb0SKAMEZAWA Hiroyuki 	}
3242d0107eb0SKAMEZAWA Hiroyuki 	return copied;
3243d0107eb0SKAMEZAWA Hiroyuki }
3244d0107eb0SKAMEZAWA Hiroyuki 
3245d0107eb0SKAMEZAWA Hiroyuki /**
3246d0107eb0SKAMEZAWA Hiroyuki  * vread() - read vmalloc area in a safe way.
3247d0107eb0SKAMEZAWA Hiroyuki  * @buf:     buffer for reading data
3248d0107eb0SKAMEZAWA Hiroyuki  * @addr:    vm address.
3249d0107eb0SKAMEZAWA Hiroyuki  * @count:   number of bytes to be read.
3250d0107eb0SKAMEZAWA Hiroyuki  *
3251d0107eb0SKAMEZAWA Hiroyuki  * This function checks that addr is a valid vmalloc'ed area, and
3252d0107eb0SKAMEZAWA Hiroyuki  * copy data from that area to a given buffer. If the given memory range
3253d0107eb0SKAMEZAWA Hiroyuki  * of [addr...addr+count) includes some valid address, data is copied to
3254d0107eb0SKAMEZAWA Hiroyuki  * proper area of @buf. If there are memory holes, they'll be zero-filled.
3255d0107eb0SKAMEZAWA Hiroyuki  * IOREMAP area is treated as memory hole and no copy is done.
3256d0107eb0SKAMEZAWA Hiroyuki  *
3257d0107eb0SKAMEZAWA Hiroyuki  * If [addr...addr+count) doesn't includes any intersects with alive
3258a8e5202dSCong Wang  * vm_struct area, returns 0. @buf should be kernel's buffer.
3259d0107eb0SKAMEZAWA Hiroyuki  *
3260d0107eb0SKAMEZAWA Hiroyuki  * Note: In usual ops, vread() is never necessary because the caller
3261d0107eb0SKAMEZAWA Hiroyuki  * should know vmalloc() area is valid and can use memcpy().
3262d0107eb0SKAMEZAWA Hiroyuki  * This is for routines which have to access vmalloc area without
3263bbcd53c9SDavid Hildenbrand  * any information, as /proc/kcore.
3264a862f68aSMike Rapoport  *
3265a862f68aSMike Rapoport  * Return: number of bytes for which addr and buf should be increased
3266a862f68aSMike Rapoport  * (same number as @count) or %0 if [addr...addr+count) doesn't
3267a862f68aSMike Rapoport  * include any intersection with valid vmalloc area
3268d0107eb0SKAMEZAWA Hiroyuki  */
32691da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
32701da177e4SLinus Torvalds {
3271e81ce85fSJoonsoo Kim 	struct vmap_area *va;
3272e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
32731da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
3274d0107eb0SKAMEZAWA Hiroyuki 	unsigned long buflen = count;
32751da177e4SLinus Torvalds 	unsigned long n;
32761da177e4SLinus Torvalds 
32771da177e4SLinus Torvalds 	/* Don't allow overflow */
32781da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
32791da177e4SLinus Torvalds 		count = -(unsigned long) addr;
32801da177e4SLinus Torvalds 
3281e81ce85fSJoonsoo Kim 	spin_lock(&vmap_area_lock);
3282f608788cSSerapheim Dimitropoulos 	va = __find_vmap_area((unsigned long)addr);
3283f608788cSSerapheim Dimitropoulos 	if (!va)
3284f608788cSSerapheim Dimitropoulos 		goto finished;
3285f608788cSSerapheim Dimitropoulos 	list_for_each_entry_from(va, &vmap_area_list, list) {
3286e81ce85fSJoonsoo Kim 		if (!count)
3287e81ce85fSJoonsoo Kim 			break;
3288e81ce85fSJoonsoo Kim 
3289688fcbfcSPengfei Li 		if (!va->vm)
3290e81ce85fSJoonsoo Kim 			continue;
3291e81ce85fSJoonsoo Kim 
3292e81ce85fSJoonsoo Kim 		vm = va->vm;
3293e81ce85fSJoonsoo Kim 		vaddr = (char *) vm->addr;
3294762216abSWanpeng Li 		if (addr >= vaddr + get_vm_area_size(vm))
32951da177e4SLinus Torvalds 			continue;
32961da177e4SLinus Torvalds 		while (addr < vaddr) {
32971da177e4SLinus Torvalds 			if (count == 0)
32981da177e4SLinus Torvalds 				goto finished;
32991da177e4SLinus Torvalds 			*buf = '\0';
33001da177e4SLinus Torvalds 			buf++;
33011da177e4SLinus Torvalds 			addr++;
33021da177e4SLinus Torvalds 			count--;
33031da177e4SLinus Torvalds 		}
3304762216abSWanpeng Li 		n = vaddr + get_vm_area_size(vm) - addr;
3305d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
3306d0107eb0SKAMEZAWA Hiroyuki 			n = count;
3307e81ce85fSJoonsoo Kim 		if (!(vm->flags & VM_IOREMAP))
3308d0107eb0SKAMEZAWA Hiroyuki 			aligned_vread(buf, addr, n);
3309d0107eb0SKAMEZAWA Hiroyuki 		else /* IOREMAP area is treated as memory hole */
3310d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, n);
3311d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
3312d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
3313d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
33141da177e4SLinus Torvalds 	}
33151da177e4SLinus Torvalds finished:
3316e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
3317d0107eb0SKAMEZAWA Hiroyuki 
3318d0107eb0SKAMEZAWA Hiroyuki 	if (buf == buf_start)
3319d0107eb0SKAMEZAWA Hiroyuki 		return 0;
3320d0107eb0SKAMEZAWA Hiroyuki 	/* zero-fill memory holes */
3321d0107eb0SKAMEZAWA Hiroyuki 	if (buf != buf_start + buflen)
3322d0107eb0SKAMEZAWA Hiroyuki 		memset(buf, 0, buflen - (buf - buf_start));
3323d0107eb0SKAMEZAWA Hiroyuki 
3324d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
33251da177e4SLinus Torvalds }
33261da177e4SLinus Torvalds 
3327d0107eb0SKAMEZAWA Hiroyuki /**
3328e69e9d4aSHATAYAMA Daisuke  * remap_vmalloc_range_partial - map vmalloc pages to userspace
3329e69e9d4aSHATAYAMA Daisuke  * @vma:		vma to cover
3330e69e9d4aSHATAYAMA Daisuke  * @uaddr:		target user address to start at
3331e69e9d4aSHATAYAMA Daisuke  * @kaddr:		virtual address of vmalloc kernel memory
3332bdebd6a2SJann Horn  * @pgoff:		offset from @kaddr to start at
3333e69e9d4aSHATAYAMA Daisuke  * @size:		size of map area
3334e69e9d4aSHATAYAMA Daisuke  *
3335e69e9d4aSHATAYAMA Daisuke  * Returns:	0 for success, -Exxx on failure
3336e69e9d4aSHATAYAMA Daisuke  *
3337e69e9d4aSHATAYAMA Daisuke  * This function checks that @kaddr is a valid vmalloc'ed area,
3338e69e9d4aSHATAYAMA Daisuke  * and that it is big enough to cover the range starting at
3339e69e9d4aSHATAYAMA Daisuke  * @uaddr in @vma. Will return failure if that criteria isn't
3340e69e9d4aSHATAYAMA Daisuke  * met.
3341e69e9d4aSHATAYAMA Daisuke  *
3342e69e9d4aSHATAYAMA Daisuke  * Similar to remap_pfn_range() (see mm/memory.c)
3343e69e9d4aSHATAYAMA Daisuke  */
3344e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
3345bdebd6a2SJann Horn 				void *kaddr, unsigned long pgoff,
3346bdebd6a2SJann Horn 				unsigned long size)
3347e69e9d4aSHATAYAMA Daisuke {
3348e69e9d4aSHATAYAMA Daisuke 	struct vm_struct *area;
3349bdebd6a2SJann Horn 	unsigned long off;
3350bdebd6a2SJann Horn 	unsigned long end_index;
3351bdebd6a2SJann Horn 
3352bdebd6a2SJann Horn 	if (check_shl_overflow(pgoff, PAGE_SHIFT, &off))
3353bdebd6a2SJann Horn 		return -EINVAL;
3354e69e9d4aSHATAYAMA Daisuke 
3355e69e9d4aSHATAYAMA Daisuke 	size = PAGE_ALIGN(size);
3356e69e9d4aSHATAYAMA Daisuke 
3357e69e9d4aSHATAYAMA Daisuke 	if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
3358e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3359e69e9d4aSHATAYAMA Daisuke 
3360e69e9d4aSHATAYAMA Daisuke 	area = find_vm_area(kaddr);
3361e69e9d4aSHATAYAMA Daisuke 	if (!area)
3362e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3363e69e9d4aSHATAYAMA Daisuke 
3364fe9041c2SChristoph Hellwig 	if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT)))
3365e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3366e69e9d4aSHATAYAMA Daisuke 
3367bdebd6a2SJann Horn 	if (check_add_overflow(size, off, &end_index) ||
3368bdebd6a2SJann Horn 	    end_index > get_vm_area_size(area))
3369e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3370bdebd6a2SJann Horn 	kaddr += off;
3371e69e9d4aSHATAYAMA Daisuke 
3372e69e9d4aSHATAYAMA Daisuke 	do {
3373e69e9d4aSHATAYAMA Daisuke 		struct page *page = vmalloc_to_page(kaddr);
3374e69e9d4aSHATAYAMA Daisuke 		int ret;
3375e69e9d4aSHATAYAMA Daisuke 
3376e69e9d4aSHATAYAMA Daisuke 		ret = vm_insert_page(vma, uaddr, page);
3377e69e9d4aSHATAYAMA Daisuke 		if (ret)
3378e69e9d4aSHATAYAMA Daisuke 			return ret;
3379e69e9d4aSHATAYAMA Daisuke 
3380e69e9d4aSHATAYAMA Daisuke 		uaddr += PAGE_SIZE;
3381e69e9d4aSHATAYAMA Daisuke 		kaddr += PAGE_SIZE;
3382e69e9d4aSHATAYAMA Daisuke 		size -= PAGE_SIZE;
3383e69e9d4aSHATAYAMA Daisuke 	} while (size > 0);
3384e69e9d4aSHATAYAMA Daisuke 
3385e69e9d4aSHATAYAMA Daisuke 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3386e69e9d4aSHATAYAMA Daisuke 
3387e69e9d4aSHATAYAMA Daisuke 	return 0;
3388e69e9d4aSHATAYAMA Daisuke }
3389e69e9d4aSHATAYAMA Daisuke 
3390e69e9d4aSHATAYAMA Daisuke /**
339183342314SNick Piggin  * remap_vmalloc_range - map vmalloc pages to userspace
339283342314SNick Piggin  * @vma:		vma to cover (map full range of vma)
339383342314SNick Piggin  * @addr:		vmalloc memory
339483342314SNick Piggin  * @pgoff:		number of pages into addr before first page to map
33957682486bSRandy Dunlap  *
33967682486bSRandy Dunlap  * Returns:	0 for success, -Exxx on failure
339783342314SNick Piggin  *
339883342314SNick Piggin  * This function checks that addr is a valid vmalloc'ed area, and
339983342314SNick Piggin  * that it is big enough to cover the vma. Will return failure if
340083342314SNick Piggin  * that criteria isn't met.
340183342314SNick Piggin  *
340272fd4a35SRobert P. J. Day  * Similar to remap_pfn_range() (see mm/memory.c)
340383342314SNick Piggin  */
340483342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
340583342314SNick Piggin 						unsigned long pgoff)
340683342314SNick Piggin {
3407e69e9d4aSHATAYAMA Daisuke 	return remap_vmalloc_range_partial(vma, vma->vm_start,
3408bdebd6a2SJann Horn 					   addr, pgoff,
3409e69e9d4aSHATAYAMA Daisuke 					   vma->vm_end - vma->vm_start);
341083342314SNick Piggin }
341183342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range);
341283342314SNick Piggin 
34135f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area)
34145f4352fbSJeremy Fitzhardinge {
34155f4352fbSJeremy Fitzhardinge 	struct vm_struct *ret;
34165f4352fbSJeremy Fitzhardinge 	ret = remove_vm_area(area->addr);
34175f4352fbSJeremy Fitzhardinge 	BUG_ON(ret != area);
34185f4352fbSJeremy Fitzhardinge 	kfree(area);
34195f4352fbSJeremy Fitzhardinge }
34205f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area);
3421a10aa579SChristoph Lameter 
34224f8b02b4STejun Heo #ifdef CONFIG_SMP
3423ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n)
3424ca23e405STejun Heo {
34254583e773SGeliang Tang 	return rb_entry_safe(n, struct vmap_area, rb_node);
3426ca23e405STejun Heo }
3427ca23e405STejun Heo 
3428ca23e405STejun Heo /**
342968ad4a33SUladzislau Rezki (Sony)  * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
343068ad4a33SUladzislau Rezki (Sony)  * @addr: target address
3431ca23e405STejun Heo  *
343268ad4a33SUladzislau Rezki (Sony)  * Returns: vmap_area if it is found. If there is no such area
343368ad4a33SUladzislau Rezki (Sony)  *   the first highest(reverse order) vmap_area is returned
343468ad4a33SUladzislau Rezki (Sony)  *   i.e. va->va_start < addr && va->va_end < addr or NULL
343568ad4a33SUladzislau Rezki (Sony)  *   if there are no any areas before @addr.
3436ca23e405STejun Heo  */
343768ad4a33SUladzislau Rezki (Sony) static struct vmap_area *
343868ad4a33SUladzislau Rezki (Sony) pvm_find_va_enclose_addr(unsigned long addr)
3439ca23e405STejun Heo {
344068ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va, *tmp;
344168ad4a33SUladzislau Rezki (Sony) 	struct rb_node *n;
344268ad4a33SUladzislau Rezki (Sony) 
344368ad4a33SUladzislau Rezki (Sony) 	n = free_vmap_area_root.rb_node;
344468ad4a33SUladzislau Rezki (Sony) 	va = NULL;
3445ca23e405STejun Heo 
3446ca23e405STejun Heo 	while (n) {
344768ad4a33SUladzislau Rezki (Sony) 		tmp = rb_entry(n, struct vmap_area, rb_node);
344868ad4a33SUladzislau Rezki (Sony) 		if (tmp->va_start <= addr) {
344968ad4a33SUladzislau Rezki (Sony) 			va = tmp;
345068ad4a33SUladzislau Rezki (Sony) 			if (tmp->va_end >= addr)
3451ca23e405STejun Heo 				break;
3452ca23e405STejun Heo 
345368ad4a33SUladzislau Rezki (Sony) 			n = n->rb_right;
3454ca23e405STejun Heo 		} else {
345568ad4a33SUladzislau Rezki (Sony) 			n = n->rb_left;
3456ca23e405STejun Heo 		}
345768ad4a33SUladzislau Rezki (Sony) 	}
345868ad4a33SUladzislau Rezki (Sony) 
345968ad4a33SUladzislau Rezki (Sony) 	return va;
3460ca23e405STejun Heo }
3461ca23e405STejun Heo 
3462ca23e405STejun Heo /**
346368ad4a33SUladzislau Rezki (Sony)  * pvm_determine_end_from_reverse - find the highest aligned address
346468ad4a33SUladzislau Rezki (Sony)  * of free block below VMALLOC_END
346568ad4a33SUladzislau Rezki (Sony)  * @va:
346668ad4a33SUladzislau Rezki (Sony)  *   in - the VA we start the search(reverse order);
346768ad4a33SUladzislau Rezki (Sony)  *   out - the VA with the highest aligned end address.
3468799fa85dSAlex Shi  * @align: alignment for required highest address
3469ca23e405STejun Heo  *
347068ad4a33SUladzislau Rezki (Sony)  * Returns: determined end address within vmap_area
3471ca23e405STejun Heo  */
347268ad4a33SUladzislau Rezki (Sony) static unsigned long
347368ad4a33SUladzislau Rezki (Sony) pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
3474ca23e405STejun Heo {
347568ad4a33SUladzislau Rezki (Sony) 	unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
3476ca23e405STejun Heo 	unsigned long addr;
3477ca23e405STejun Heo 
347868ad4a33SUladzislau Rezki (Sony) 	if (likely(*va)) {
347968ad4a33SUladzislau Rezki (Sony) 		list_for_each_entry_from_reverse((*va),
348068ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_list, list) {
348168ad4a33SUladzislau Rezki (Sony) 			addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
348268ad4a33SUladzislau Rezki (Sony) 			if ((*va)->va_start < addr)
348368ad4a33SUladzislau Rezki (Sony) 				return addr;
348468ad4a33SUladzislau Rezki (Sony) 		}
3485ca23e405STejun Heo 	}
3486ca23e405STejun Heo 
348768ad4a33SUladzislau Rezki (Sony) 	return 0;
3488ca23e405STejun Heo }
3489ca23e405STejun Heo 
3490ca23e405STejun Heo /**
3491ca23e405STejun Heo  * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3492ca23e405STejun Heo  * @offsets: array containing offset of each area
3493ca23e405STejun Heo  * @sizes: array containing size of each area
3494ca23e405STejun Heo  * @nr_vms: the number of areas to allocate
3495ca23e405STejun Heo  * @align: alignment, all entries in @offsets and @sizes must be aligned to this
3496ca23e405STejun Heo  *
3497ca23e405STejun Heo  * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3498ca23e405STejun Heo  *	    vm_structs on success, %NULL on failure
3499ca23e405STejun Heo  *
3500ca23e405STejun Heo  * Percpu allocator wants to use congruent vm areas so that it can
3501ca23e405STejun Heo  * maintain the offsets among percpu areas.  This function allocates
3502ec3f64fcSDavid Rientjes  * congruent vmalloc areas for it with GFP_KERNEL.  These areas tend to
3503ec3f64fcSDavid Rientjes  * be scattered pretty far, distance between two areas easily going up
3504ec3f64fcSDavid Rientjes  * to gigabytes.  To avoid interacting with regular vmallocs, these
3505ec3f64fcSDavid Rientjes  * areas are allocated from top.
3506ca23e405STejun Heo  *
3507ca23e405STejun Heo  * Despite its complicated look, this allocator is rather simple. It
350868ad4a33SUladzislau Rezki (Sony)  * does everything top-down and scans free blocks from the end looking
350968ad4a33SUladzislau Rezki (Sony)  * for matching base. While scanning, if any of the areas do not fit the
351068ad4a33SUladzislau Rezki (Sony)  * base address is pulled down to fit the area. Scanning is repeated till
351168ad4a33SUladzislau Rezki (Sony)  * all the areas fit and then all necessary data structures are inserted
351268ad4a33SUladzislau Rezki (Sony)  * and the result is returned.
3513ca23e405STejun Heo  */
3514ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3515ca23e405STejun Heo 				     const size_t *sizes, int nr_vms,
3516ec3f64fcSDavid Rientjes 				     size_t align)
3517ca23e405STejun Heo {
3518ca23e405STejun Heo 	const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3519ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
352068ad4a33SUladzislau Rezki (Sony) 	struct vmap_area **vas, *va;
3521ca23e405STejun Heo 	struct vm_struct **vms;
3522ca23e405STejun Heo 	int area, area2, last_area, term_area;
3523253a496dSDaniel Axtens 	unsigned long base, start, size, end, last_end, orig_start, orig_end;
3524ca23e405STejun Heo 	bool purged = false;
352568ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
3526ca23e405STejun Heo 
3527ca23e405STejun Heo 	/* verify parameters and allocate data structures */
3528891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(align) || !is_power_of_2(align));
3529ca23e405STejun Heo 	for (last_area = 0, area = 0; area < nr_vms; area++) {
3530ca23e405STejun Heo 		start = offsets[area];
3531ca23e405STejun Heo 		end = start + sizes[area];
3532ca23e405STejun Heo 
3533ca23e405STejun Heo 		/* is everything aligned properly? */
3534ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(offsets[area], align));
3535ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(sizes[area], align));
3536ca23e405STejun Heo 
3537ca23e405STejun Heo 		/* detect the area with the highest address */
3538ca23e405STejun Heo 		if (start > offsets[last_area])
3539ca23e405STejun Heo 			last_area = area;
3540ca23e405STejun Heo 
3541c568da28SWei Yang 		for (area2 = area + 1; area2 < nr_vms; area2++) {
3542ca23e405STejun Heo 			unsigned long start2 = offsets[area2];
3543ca23e405STejun Heo 			unsigned long end2 = start2 + sizes[area2];
3544ca23e405STejun Heo 
3545c568da28SWei Yang 			BUG_ON(start2 < end && start < end2);
3546ca23e405STejun Heo 		}
3547ca23e405STejun Heo 	}
3548ca23e405STejun Heo 	last_end = offsets[last_area] + sizes[last_area];
3549ca23e405STejun Heo 
3550ca23e405STejun Heo 	if (vmalloc_end - vmalloc_start < last_end) {
3551ca23e405STejun Heo 		WARN_ON(true);
3552ca23e405STejun Heo 		return NULL;
3553ca23e405STejun Heo 	}
3554ca23e405STejun Heo 
35554d67d860SThomas Meyer 	vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
35564d67d860SThomas Meyer 	vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
3557ca23e405STejun Heo 	if (!vas || !vms)
3558f1db7afdSKautuk Consul 		goto err_free2;
3559ca23e405STejun Heo 
3560ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
356168ad4a33SUladzislau Rezki (Sony) 		vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
3562ec3f64fcSDavid Rientjes 		vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
3563ca23e405STejun Heo 		if (!vas[area] || !vms[area])
3564ca23e405STejun Heo 			goto err_free;
3565ca23e405STejun Heo 	}
3566ca23e405STejun Heo retry:
3567e36176beSUladzislau Rezki (Sony) 	spin_lock(&free_vmap_area_lock);
3568ca23e405STejun Heo 
3569ca23e405STejun Heo 	/* start scanning - we scan from the top, begin with the last area */
3570ca23e405STejun Heo 	area = term_area = last_area;
3571ca23e405STejun Heo 	start = offsets[area];
3572ca23e405STejun Heo 	end = start + sizes[area];
3573ca23e405STejun Heo 
357468ad4a33SUladzislau Rezki (Sony) 	va = pvm_find_va_enclose_addr(vmalloc_end);
357568ad4a33SUladzislau Rezki (Sony) 	base = pvm_determine_end_from_reverse(&va, align) - end;
3576ca23e405STejun Heo 
3577ca23e405STejun Heo 	while (true) {
3578ca23e405STejun Heo 		/*
3579ca23e405STejun Heo 		 * base might have underflowed, add last_end before
3580ca23e405STejun Heo 		 * comparing.
3581ca23e405STejun Heo 		 */
358268ad4a33SUladzislau Rezki (Sony) 		if (base + last_end < vmalloc_start + last_end)
358368ad4a33SUladzislau Rezki (Sony) 			goto overflow;
3584ca23e405STejun Heo 
3585ca23e405STejun Heo 		/*
358668ad4a33SUladzislau Rezki (Sony) 		 * Fitting base has not been found.
3587ca23e405STejun Heo 		 */
358868ad4a33SUladzislau Rezki (Sony) 		if (va == NULL)
358968ad4a33SUladzislau Rezki (Sony) 			goto overflow;
3590ca23e405STejun Heo 
3591ca23e405STejun Heo 		/*
3592d8cc323dSQiujun Huang 		 * If required width exceeds current VA block, move
35935336e52cSKuppuswamy Sathyanarayanan 		 * base downwards and then recheck.
35945336e52cSKuppuswamy Sathyanarayanan 		 */
35955336e52cSKuppuswamy Sathyanarayanan 		if (base + end > va->va_end) {
35965336e52cSKuppuswamy Sathyanarayanan 			base = pvm_determine_end_from_reverse(&va, align) - end;
35975336e52cSKuppuswamy Sathyanarayanan 			term_area = area;
35985336e52cSKuppuswamy Sathyanarayanan 			continue;
35995336e52cSKuppuswamy Sathyanarayanan 		}
36005336e52cSKuppuswamy Sathyanarayanan 
36015336e52cSKuppuswamy Sathyanarayanan 		/*
360268ad4a33SUladzislau Rezki (Sony) 		 * If this VA does not fit, move base downwards and recheck.
3603ca23e405STejun Heo 		 */
36045336e52cSKuppuswamy Sathyanarayanan 		if (base + start < va->va_start) {
360568ad4a33SUladzislau Rezki (Sony) 			va = node_to_va(rb_prev(&va->rb_node));
360668ad4a33SUladzislau Rezki (Sony) 			base = pvm_determine_end_from_reverse(&va, align) - end;
3607ca23e405STejun Heo 			term_area = area;
3608ca23e405STejun Heo 			continue;
3609ca23e405STejun Heo 		}
3610ca23e405STejun Heo 
3611ca23e405STejun Heo 		/*
3612ca23e405STejun Heo 		 * This area fits, move on to the previous one.  If
3613ca23e405STejun Heo 		 * the previous one is the terminal one, we're done.
3614ca23e405STejun Heo 		 */
3615ca23e405STejun Heo 		area = (area + nr_vms - 1) % nr_vms;
3616ca23e405STejun Heo 		if (area == term_area)
3617ca23e405STejun Heo 			break;
361868ad4a33SUladzislau Rezki (Sony) 
3619ca23e405STejun Heo 		start = offsets[area];
3620ca23e405STejun Heo 		end = start + sizes[area];
362168ad4a33SUladzislau Rezki (Sony) 		va = pvm_find_va_enclose_addr(base + end);
3622ca23e405STejun Heo 	}
362368ad4a33SUladzislau Rezki (Sony) 
3624ca23e405STejun Heo 	/* we've found a fitting base, insert all va's */
3625ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
362668ad4a33SUladzislau Rezki (Sony) 		int ret;
3627ca23e405STejun Heo 
362868ad4a33SUladzislau Rezki (Sony) 		start = base + offsets[area];
362968ad4a33SUladzislau Rezki (Sony) 		size = sizes[area];
363068ad4a33SUladzislau Rezki (Sony) 
363168ad4a33SUladzislau Rezki (Sony) 		va = pvm_find_va_enclose_addr(start);
363268ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(va == NULL))
363368ad4a33SUladzislau Rezki (Sony) 			/* It is a BUG(), but trigger recovery instead. */
363468ad4a33SUladzislau Rezki (Sony) 			goto recovery;
363568ad4a33SUladzislau Rezki (Sony) 
363668ad4a33SUladzislau Rezki (Sony) 		type = classify_va_fit_type(va, start, size);
363768ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(type == NOTHING_FIT))
363868ad4a33SUladzislau Rezki (Sony) 			/* It is a BUG(), but trigger recovery instead. */
363968ad4a33SUladzislau Rezki (Sony) 			goto recovery;
364068ad4a33SUladzislau Rezki (Sony) 
364168ad4a33SUladzislau Rezki (Sony) 		ret = adjust_va_to_fit_type(va, start, size, type);
364268ad4a33SUladzislau Rezki (Sony) 		if (unlikely(ret))
364368ad4a33SUladzislau Rezki (Sony) 			goto recovery;
364468ad4a33SUladzislau Rezki (Sony) 
364568ad4a33SUladzislau Rezki (Sony) 		/* Allocated area. */
364668ad4a33SUladzislau Rezki (Sony) 		va = vas[area];
364768ad4a33SUladzislau Rezki (Sony) 		va->va_start = start;
364868ad4a33SUladzislau Rezki (Sony) 		va->va_end = start + size;
3649ca23e405STejun Heo 	}
3650ca23e405STejun Heo 
3651e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
3652ca23e405STejun Heo 
3653253a496dSDaniel Axtens 	/* populate the kasan shadow space */
3654253a496dSDaniel Axtens 	for (area = 0; area < nr_vms; area++) {
3655253a496dSDaniel Axtens 		if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area]))
3656253a496dSDaniel Axtens 			goto err_free_shadow;
3657253a496dSDaniel Axtens 
3658253a496dSDaniel Axtens 		kasan_unpoison_vmalloc((void *)vas[area]->va_start,
3659253a496dSDaniel Axtens 				       sizes[area]);
3660253a496dSDaniel Axtens 	}
3661253a496dSDaniel Axtens 
3662ca23e405STejun Heo 	/* insert all vm's */
3663e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
3664e36176beSUladzislau Rezki (Sony) 	for (area = 0; area < nr_vms; area++) {
3665e36176beSUladzislau Rezki (Sony) 		insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list);
3666e36176beSUladzislau Rezki (Sony) 
3667e36176beSUladzislau Rezki (Sony) 		setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC,
3668ca23e405STejun Heo 				 pcpu_get_vm_areas);
3669e36176beSUladzislau Rezki (Sony) 	}
3670e36176beSUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
3671ca23e405STejun Heo 
3672ca23e405STejun Heo 	kfree(vas);
3673ca23e405STejun Heo 	return vms;
3674ca23e405STejun Heo 
367568ad4a33SUladzislau Rezki (Sony) recovery:
3676e36176beSUladzislau Rezki (Sony) 	/*
3677e36176beSUladzislau Rezki (Sony) 	 * Remove previously allocated areas. There is no
3678e36176beSUladzislau Rezki (Sony) 	 * need in removing these areas from the busy tree,
3679e36176beSUladzislau Rezki (Sony) 	 * because they are inserted only on the final step
3680e36176beSUladzislau Rezki (Sony) 	 * and when pcpu_get_vm_areas() is success.
3681e36176beSUladzislau Rezki (Sony) 	 */
368268ad4a33SUladzislau Rezki (Sony) 	while (area--) {
3683253a496dSDaniel Axtens 		orig_start = vas[area]->va_start;
3684253a496dSDaniel Axtens 		orig_end = vas[area]->va_end;
368596e2db45SUladzislau Rezki (Sony) 		va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
36863c5c3cfbSDaniel Axtens 				&free_vmap_area_list);
36879c801f61SUladzislau Rezki (Sony) 		if (va)
3688253a496dSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
3689253a496dSDaniel Axtens 				va->va_start, va->va_end);
369068ad4a33SUladzislau Rezki (Sony) 		vas[area] = NULL;
369168ad4a33SUladzislau Rezki (Sony) 	}
369268ad4a33SUladzislau Rezki (Sony) 
369368ad4a33SUladzislau Rezki (Sony) overflow:
3694e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
369568ad4a33SUladzislau Rezki (Sony) 	if (!purged) {
369668ad4a33SUladzislau Rezki (Sony) 		purge_vmap_area_lazy();
369768ad4a33SUladzislau Rezki (Sony) 		purged = true;
369868ad4a33SUladzislau Rezki (Sony) 
369968ad4a33SUladzislau Rezki (Sony) 		/* Before "retry", check if we recover. */
370068ad4a33SUladzislau Rezki (Sony) 		for (area = 0; area < nr_vms; area++) {
370168ad4a33SUladzislau Rezki (Sony) 			if (vas[area])
370268ad4a33SUladzislau Rezki (Sony) 				continue;
370368ad4a33SUladzislau Rezki (Sony) 
370468ad4a33SUladzislau Rezki (Sony) 			vas[area] = kmem_cache_zalloc(
370568ad4a33SUladzislau Rezki (Sony) 				vmap_area_cachep, GFP_KERNEL);
370668ad4a33SUladzislau Rezki (Sony) 			if (!vas[area])
370768ad4a33SUladzislau Rezki (Sony) 				goto err_free;
370868ad4a33SUladzislau Rezki (Sony) 		}
370968ad4a33SUladzislau Rezki (Sony) 
371068ad4a33SUladzislau Rezki (Sony) 		goto retry;
371168ad4a33SUladzislau Rezki (Sony) 	}
371268ad4a33SUladzislau Rezki (Sony) 
3713ca23e405STejun Heo err_free:
3714ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
371568ad4a33SUladzislau Rezki (Sony) 		if (vas[area])
371668ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, vas[area]);
371768ad4a33SUladzislau Rezki (Sony) 
3718ca23e405STejun Heo 		kfree(vms[area]);
3719ca23e405STejun Heo 	}
3720f1db7afdSKautuk Consul err_free2:
3721ca23e405STejun Heo 	kfree(vas);
3722ca23e405STejun Heo 	kfree(vms);
3723ca23e405STejun Heo 	return NULL;
3724253a496dSDaniel Axtens 
3725253a496dSDaniel Axtens err_free_shadow:
3726253a496dSDaniel Axtens 	spin_lock(&free_vmap_area_lock);
3727253a496dSDaniel Axtens 	/*
3728253a496dSDaniel Axtens 	 * We release all the vmalloc shadows, even the ones for regions that
3729253a496dSDaniel Axtens 	 * hadn't been successfully added. This relies on kasan_release_vmalloc
3730253a496dSDaniel Axtens 	 * being able to tolerate this case.
3731253a496dSDaniel Axtens 	 */
3732253a496dSDaniel Axtens 	for (area = 0; area < nr_vms; area++) {
3733253a496dSDaniel Axtens 		orig_start = vas[area]->va_start;
3734253a496dSDaniel Axtens 		orig_end = vas[area]->va_end;
373596e2db45SUladzislau Rezki (Sony) 		va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
3736253a496dSDaniel Axtens 				&free_vmap_area_list);
37379c801f61SUladzislau Rezki (Sony) 		if (va)
3738253a496dSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
3739253a496dSDaniel Axtens 				va->va_start, va->va_end);
3740253a496dSDaniel Axtens 		vas[area] = NULL;
3741253a496dSDaniel Axtens 		kfree(vms[area]);
3742253a496dSDaniel Axtens 	}
3743253a496dSDaniel Axtens 	spin_unlock(&free_vmap_area_lock);
3744253a496dSDaniel Axtens 	kfree(vas);
3745253a496dSDaniel Axtens 	kfree(vms);
3746253a496dSDaniel Axtens 	return NULL;
3747ca23e405STejun Heo }
3748ca23e405STejun Heo 
3749ca23e405STejun Heo /**
3750ca23e405STejun Heo  * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
3751ca23e405STejun Heo  * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
3752ca23e405STejun Heo  * @nr_vms: the number of allocated areas
3753ca23e405STejun Heo  *
3754ca23e405STejun Heo  * Free vm_structs and the array allocated by pcpu_get_vm_areas().
3755ca23e405STejun Heo  */
3756ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
3757ca23e405STejun Heo {
3758ca23e405STejun Heo 	int i;
3759ca23e405STejun Heo 
3760ca23e405STejun Heo 	for (i = 0; i < nr_vms; i++)
3761ca23e405STejun Heo 		free_vm_area(vms[i]);
3762ca23e405STejun Heo 	kfree(vms);
3763ca23e405STejun Heo }
37644f8b02b4STejun Heo #endif	/* CONFIG_SMP */
3765a10aa579SChristoph Lameter 
37665bb1bb35SPaul E. McKenney #ifdef CONFIG_PRINTK
376798f18083SPaul E. McKenney bool vmalloc_dump_obj(void *object)
376898f18083SPaul E. McKenney {
376998f18083SPaul E. McKenney 	struct vm_struct *vm;
377098f18083SPaul E. McKenney 	void *objp = (void *)PAGE_ALIGN((unsigned long)object);
377198f18083SPaul E. McKenney 
377298f18083SPaul E. McKenney 	vm = find_vm_area(objp);
377398f18083SPaul E. McKenney 	if (!vm)
377498f18083SPaul E. McKenney 		return false;
3775bd34dcd4SPaul E. McKenney 	pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
3776bd34dcd4SPaul E. McKenney 		vm->nr_pages, (unsigned long)vm->addr, vm->caller);
377798f18083SPaul E. McKenney 	return true;
377898f18083SPaul E. McKenney }
37795bb1bb35SPaul E. McKenney #endif
378098f18083SPaul E. McKenney 
3781a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS
3782a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos)
3783e36176beSUladzislau Rezki (Sony) 	__acquires(&vmap_purge_lock)
3784d4033afdSJoonsoo Kim 	__acquires(&vmap_area_lock)
3785a10aa579SChristoph Lameter {
3786e36176beSUladzislau Rezki (Sony) 	mutex_lock(&vmap_purge_lock);
3787d4033afdSJoonsoo Kim 	spin_lock(&vmap_area_lock);
3788e36176beSUladzislau Rezki (Sony) 
37893f500069Szijun_hu 	return seq_list_start(&vmap_area_list, *pos);
3790a10aa579SChristoph Lameter }
3791a10aa579SChristoph Lameter 
3792a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3793a10aa579SChristoph Lameter {
37943f500069Szijun_hu 	return seq_list_next(p, &vmap_area_list, pos);
3795a10aa579SChristoph Lameter }
3796a10aa579SChristoph Lameter 
3797a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p)
3798d4033afdSJoonsoo Kim 	__releases(&vmap_area_lock)
37990a7dd4e9SWaiman Long 	__releases(&vmap_purge_lock)
3800a10aa579SChristoph Lameter {
3801d4033afdSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
38020a7dd4e9SWaiman Long 	mutex_unlock(&vmap_purge_lock);
3803a10aa579SChristoph Lameter }
3804a10aa579SChristoph Lameter 
3805a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v)
3806a47a126aSEric Dumazet {
3807e5adfffcSKirill A. Shutemov 	if (IS_ENABLED(CONFIG_NUMA)) {
3808a47a126aSEric Dumazet 		unsigned int nr, *counters = m->private;
3809a47a126aSEric Dumazet 
3810a47a126aSEric Dumazet 		if (!counters)
3811a47a126aSEric Dumazet 			return;
3812a47a126aSEric Dumazet 
3813af12346cSWanpeng Li 		if (v->flags & VM_UNINITIALIZED)
3814af12346cSWanpeng Li 			return;
38157e5b528bSDmitry Vyukov 		/* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
38167e5b528bSDmitry Vyukov 		smp_rmb();
3817af12346cSWanpeng Li 
3818a47a126aSEric Dumazet 		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
3819a47a126aSEric Dumazet 
3820a47a126aSEric Dumazet 		for (nr = 0; nr < v->nr_pages; nr++)
3821a47a126aSEric Dumazet 			counters[page_to_nid(v->pages[nr])]++;
3822a47a126aSEric Dumazet 
3823a47a126aSEric Dumazet 		for_each_node_state(nr, N_HIGH_MEMORY)
3824a47a126aSEric Dumazet 			if (counters[nr])
3825a47a126aSEric Dumazet 				seq_printf(m, " N%u=%u", nr, counters[nr]);
3826a47a126aSEric Dumazet 	}
3827a47a126aSEric Dumazet }
3828a47a126aSEric Dumazet 
3829dd3b8353SUladzislau Rezki (Sony) static void show_purge_info(struct seq_file *m)
3830dd3b8353SUladzislau Rezki (Sony) {
3831dd3b8353SUladzislau Rezki (Sony) 	struct vmap_area *va;
3832dd3b8353SUladzislau Rezki (Sony) 
383396e2db45SUladzislau Rezki (Sony) 	spin_lock(&purge_vmap_area_lock);
383496e2db45SUladzislau Rezki (Sony) 	list_for_each_entry(va, &purge_vmap_area_list, list) {
3835dd3b8353SUladzislau Rezki (Sony) 		seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n",
3836dd3b8353SUladzislau Rezki (Sony) 			(void *)va->va_start, (void *)va->va_end,
3837dd3b8353SUladzislau Rezki (Sony) 			va->va_end - va->va_start);
3838dd3b8353SUladzislau Rezki (Sony) 	}
383996e2db45SUladzislau Rezki (Sony) 	spin_unlock(&purge_vmap_area_lock);
3840dd3b8353SUladzislau Rezki (Sony) }
3841dd3b8353SUladzislau Rezki (Sony) 
3842a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p)
3843a10aa579SChristoph Lameter {
38443f500069Szijun_hu 	struct vmap_area *va;
3845d4033afdSJoonsoo Kim 	struct vm_struct *v;
3846d4033afdSJoonsoo Kim 
38473f500069Szijun_hu 	va = list_entry(p, struct vmap_area, list);
38483f500069Szijun_hu 
3849c2ce8c14SWanpeng Li 	/*
3850688fcbfcSPengfei Li 	 * s_show can encounter race with remove_vm_area, !vm on behalf
3851688fcbfcSPengfei Li 	 * of vmap area is being tear down or vm_map_ram allocation.
3852c2ce8c14SWanpeng Li 	 */
3853688fcbfcSPengfei Li 	if (!va->vm) {
3854dd3b8353SUladzislau Rezki (Sony) 		seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n",
385578c72746SYisheng Xie 			(void *)va->va_start, (void *)va->va_end,
3856dd3b8353SUladzislau Rezki (Sony) 			va->va_end - va->va_start);
385778c72746SYisheng Xie 
3858d4033afdSJoonsoo Kim 		return 0;
385978c72746SYisheng Xie 	}
3860d4033afdSJoonsoo Kim 
3861d4033afdSJoonsoo Kim 	v = va->vm;
3862a10aa579SChristoph Lameter 
386345ec1690SKees Cook 	seq_printf(m, "0x%pK-0x%pK %7ld",
3864a10aa579SChristoph Lameter 		v->addr, v->addr + v->size, v->size);
3865a10aa579SChristoph Lameter 
386662c70bceSJoe Perches 	if (v->caller)
386762c70bceSJoe Perches 		seq_printf(m, " %pS", v->caller);
386823016969SChristoph Lameter 
3869a10aa579SChristoph Lameter 	if (v->nr_pages)
3870a10aa579SChristoph Lameter 		seq_printf(m, " pages=%d", v->nr_pages);
3871a10aa579SChristoph Lameter 
3872a10aa579SChristoph Lameter 	if (v->phys_addr)
3873199eaa05SMiles Chen 		seq_printf(m, " phys=%pa", &v->phys_addr);
3874a10aa579SChristoph Lameter 
3875a10aa579SChristoph Lameter 	if (v->flags & VM_IOREMAP)
3876f4527c90SFabian Frederick 		seq_puts(m, " ioremap");
3877a10aa579SChristoph Lameter 
3878a10aa579SChristoph Lameter 	if (v->flags & VM_ALLOC)
3879f4527c90SFabian Frederick 		seq_puts(m, " vmalloc");
3880a10aa579SChristoph Lameter 
3881a10aa579SChristoph Lameter 	if (v->flags & VM_MAP)
3882f4527c90SFabian Frederick 		seq_puts(m, " vmap");
3883a10aa579SChristoph Lameter 
3884a10aa579SChristoph Lameter 	if (v->flags & VM_USERMAP)
3885f4527c90SFabian Frederick 		seq_puts(m, " user");
3886a10aa579SChristoph Lameter 
3887fe9041c2SChristoph Hellwig 	if (v->flags & VM_DMA_COHERENT)
3888fe9041c2SChristoph Hellwig 		seq_puts(m, " dma-coherent");
3889fe9041c2SChristoph Hellwig 
3890244d63eeSDavid Rientjes 	if (is_vmalloc_addr(v->pages))
3891f4527c90SFabian Frederick 		seq_puts(m, " vpages");
3892a10aa579SChristoph Lameter 
3893a47a126aSEric Dumazet 	show_numa_info(m, v);
3894a10aa579SChristoph Lameter 	seq_putc(m, '\n');
3895dd3b8353SUladzislau Rezki (Sony) 
3896dd3b8353SUladzislau Rezki (Sony) 	/*
389796e2db45SUladzislau Rezki (Sony) 	 * As a final step, dump "unpurged" areas.
3898dd3b8353SUladzislau Rezki (Sony) 	 */
3899dd3b8353SUladzislau Rezki (Sony) 	if (list_is_last(&va->list, &vmap_area_list))
3900dd3b8353SUladzislau Rezki (Sony) 		show_purge_info(m);
3901dd3b8353SUladzislau Rezki (Sony) 
3902a10aa579SChristoph Lameter 	return 0;
3903a10aa579SChristoph Lameter }
3904a10aa579SChristoph Lameter 
39055f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = {
3906a10aa579SChristoph Lameter 	.start = s_start,
3907a10aa579SChristoph Lameter 	.next = s_next,
3908a10aa579SChristoph Lameter 	.stop = s_stop,
3909a10aa579SChristoph Lameter 	.show = s_show,
3910a10aa579SChristoph Lameter };
39115f6a6a9cSAlexey Dobriyan 
39125f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void)
39135f6a6a9cSAlexey Dobriyan {
3914fddda2b7SChristoph Hellwig 	if (IS_ENABLED(CONFIG_NUMA))
39150825a6f9SJoe Perches 		proc_create_seq_private("vmallocinfo", 0400, NULL,
391644414d82SChristoph Hellwig 				&vmalloc_op,
391744414d82SChristoph Hellwig 				nr_node_ids * sizeof(unsigned int), NULL);
3918fddda2b7SChristoph Hellwig 	else
39190825a6f9SJoe Perches 		proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
39205f6a6a9cSAlexey Dobriyan 	return 0;
39215f6a6a9cSAlexey Dobriyan }
39225f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init);
3923db3808c1SJoonsoo Kim 
3924a10aa579SChristoph Lameter #endif
3925