xref: /openbmc/linux/mm/vmalloc.c (revision 06c8994626d1b7d8c26dfd06992d67703a274054)
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>
344e5aa1f4SShakeel Butt #include <linux/memcontrol.h>
3532fcfd40SAl Viro #include <linux/llist.h>
360f616be1SToshi Kani #include <linux/bitops.h>
3768ad4a33SUladzislau Rezki (Sony) #include <linux/rbtree_augmented.h>
38bdebd6a2SJann Horn #include <linux/overflow.h>
39c0eb315aSNicholas Piggin #include <linux/pgtable.h>
407c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
41f7ee1f13SChristophe Leroy #include <linux/hugetlb.h>
42451769ebSMichal Hocko #include <linux/sched/mm.h>
431da177e4SLinus Torvalds #include <asm/tlbflush.h>
442dca6999SDavid Miller #include <asm/shmparam.h>
451da177e4SLinus Torvalds 
46cf243da6SUladzislau Rezki (Sony) #define CREATE_TRACE_POINTS
47cf243da6SUladzislau Rezki (Sony) #include <trace/events/vmalloc.h>
48cf243da6SUladzislau Rezki (Sony) 
49dd56b046SMel Gorman #include "internal.h"
502a681cfaSJoerg Roedel #include "pgalloc-track.h"
51dd56b046SMel Gorman 
5282a70ce0SChristoph Hellwig #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
5382a70ce0SChristoph Hellwig static unsigned int __ro_after_init ioremap_max_page_shift = BITS_PER_LONG - 1;
5482a70ce0SChristoph Hellwig 
5582a70ce0SChristoph Hellwig static int __init set_nohugeiomap(char *str)
5682a70ce0SChristoph Hellwig {
5782a70ce0SChristoph Hellwig 	ioremap_max_page_shift = PAGE_SHIFT;
5882a70ce0SChristoph Hellwig 	return 0;
5982a70ce0SChristoph Hellwig }
6082a70ce0SChristoph Hellwig early_param("nohugeiomap", set_nohugeiomap);
6182a70ce0SChristoph Hellwig #else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
6282a70ce0SChristoph Hellwig static const unsigned int ioremap_max_page_shift = PAGE_SHIFT;
6382a70ce0SChristoph Hellwig #endif	/* CONFIG_HAVE_ARCH_HUGE_VMAP */
6482a70ce0SChristoph Hellwig 
65121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
66121e6f32SNicholas Piggin static bool __ro_after_init vmap_allow_huge = true;
67121e6f32SNicholas Piggin 
68121e6f32SNicholas Piggin static int __init set_nohugevmalloc(char *str)
69121e6f32SNicholas Piggin {
70121e6f32SNicholas Piggin 	vmap_allow_huge = false;
71121e6f32SNicholas Piggin 	return 0;
72121e6f32SNicholas Piggin }
73121e6f32SNicholas Piggin early_param("nohugevmalloc", set_nohugevmalloc);
74121e6f32SNicholas Piggin #else /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
75121e6f32SNicholas Piggin static const bool vmap_allow_huge = false;
76121e6f32SNicholas Piggin #endif	/* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
77121e6f32SNicholas Piggin 
78186525bdSIngo Molnar bool is_vmalloc_addr(const void *x)
79186525bdSIngo Molnar {
804aff1dc4SAndrey Konovalov 	unsigned long addr = (unsigned long)kasan_reset_tag(x);
81186525bdSIngo Molnar 
82186525bdSIngo Molnar 	return addr >= VMALLOC_START && addr < VMALLOC_END;
83186525bdSIngo Molnar }
84186525bdSIngo Molnar EXPORT_SYMBOL(is_vmalloc_addr);
85186525bdSIngo Molnar 
8632fcfd40SAl Viro struct vfree_deferred {
8732fcfd40SAl Viro 	struct llist_head list;
8832fcfd40SAl Viro 	struct work_struct wq;
8932fcfd40SAl Viro };
9032fcfd40SAl Viro static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
9132fcfd40SAl Viro 
92db64fe02SNick Piggin /*** Page table manipulation functions ***/
935e9e3d77SNicholas Piggin static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
945e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
95f7ee1f13SChristophe Leroy 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
965e9e3d77SNicholas Piggin {
975e9e3d77SNicholas Piggin 	pte_t *pte;
985e9e3d77SNicholas Piggin 	u64 pfn;
99f7ee1f13SChristophe Leroy 	unsigned long size = PAGE_SIZE;
1005e9e3d77SNicholas Piggin 
1015e9e3d77SNicholas Piggin 	pfn = phys_addr >> PAGE_SHIFT;
1025e9e3d77SNicholas Piggin 	pte = pte_alloc_kernel_track(pmd, addr, mask);
1035e9e3d77SNicholas Piggin 	if (!pte)
1045e9e3d77SNicholas Piggin 		return -ENOMEM;
1055e9e3d77SNicholas Piggin 	do {
1065e9e3d77SNicholas Piggin 		BUG_ON(!pte_none(*pte));
107f7ee1f13SChristophe Leroy 
108f7ee1f13SChristophe Leroy #ifdef CONFIG_HUGETLB_PAGE
109f7ee1f13SChristophe Leroy 		size = arch_vmap_pte_range_map_size(addr, end, pfn, max_page_shift);
110f7ee1f13SChristophe Leroy 		if (size != PAGE_SIZE) {
111f7ee1f13SChristophe Leroy 			pte_t entry = pfn_pte(pfn, prot);
112f7ee1f13SChristophe Leroy 
113f7ee1f13SChristophe Leroy 			entry = arch_make_huge_pte(entry, ilog2(size), 0);
114f7ee1f13SChristophe Leroy 			set_huge_pte_at(&init_mm, addr, pte, entry);
115f7ee1f13SChristophe Leroy 			pfn += PFN_DOWN(size);
116f7ee1f13SChristophe Leroy 			continue;
117f7ee1f13SChristophe Leroy 		}
118f7ee1f13SChristophe Leroy #endif
1195e9e3d77SNicholas Piggin 		set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
1205e9e3d77SNicholas Piggin 		pfn++;
121f7ee1f13SChristophe Leroy 	} while (pte += PFN_DOWN(size), addr += size, addr != end);
1225e9e3d77SNicholas Piggin 	*mask |= PGTBL_PTE_MODIFIED;
1235e9e3d77SNicholas Piggin 	return 0;
1245e9e3d77SNicholas Piggin }
1255e9e3d77SNicholas Piggin 
1265e9e3d77SNicholas Piggin static int vmap_try_huge_pmd(pmd_t *pmd, unsigned long addr, unsigned long end,
1275e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
1285e9e3d77SNicholas Piggin 			unsigned int max_page_shift)
1295e9e3d77SNicholas Piggin {
1305e9e3d77SNicholas Piggin 	if (max_page_shift < PMD_SHIFT)
1315e9e3d77SNicholas Piggin 		return 0;
1325e9e3d77SNicholas Piggin 
1335e9e3d77SNicholas Piggin 	if (!arch_vmap_pmd_supported(prot))
1345e9e3d77SNicholas Piggin 		return 0;
1355e9e3d77SNicholas Piggin 
1365e9e3d77SNicholas Piggin 	if ((end - addr) != PMD_SIZE)
1375e9e3d77SNicholas Piggin 		return 0;
1385e9e3d77SNicholas Piggin 
1395e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(addr, PMD_SIZE))
1405e9e3d77SNicholas Piggin 		return 0;
1415e9e3d77SNicholas Piggin 
1425e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(phys_addr, PMD_SIZE))
1435e9e3d77SNicholas Piggin 		return 0;
1445e9e3d77SNicholas Piggin 
1455e9e3d77SNicholas Piggin 	if (pmd_present(*pmd) && !pmd_free_pte_page(pmd, addr))
1465e9e3d77SNicholas Piggin 		return 0;
1475e9e3d77SNicholas Piggin 
1485e9e3d77SNicholas Piggin 	return pmd_set_huge(pmd, phys_addr, prot);
1495e9e3d77SNicholas Piggin }
1505e9e3d77SNicholas Piggin 
1515e9e3d77SNicholas Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
1525e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
1535e9e3d77SNicholas Piggin 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
1545e9e3d77SNicholas Piggin {
1555e9e3d77SNicholas Piggin 	pmd_t *pmd;
1565e9e3d77SNicholas Piggin 	unsigned long next;
1575e9e3d77SNicholas Piggin 
1585e9e3d77SNicholas Piggin 	pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
1595e9e3d77SNicholas Piggin 	if (!pmd)
1605e9e3d77SNicholas Piggin 		return -ENOMEM;
1615e9e3d77SNicholas Piggin 	do {
1625e9e3d77SNicholas Piggin 		next = pmd_addr_end(addr, end);
1635e9e3d77SNicholas Piggin 
1645e9e3d77SNicholas Piggin 		if (vmap_try_huge_pmd(pmd, addr, next, phys_addr, prot,
1655e9e3d77SNicholas Piggin 					max_page_shift)) {
1665e9e3d77SNicholas Piggin 			*mask |= PGTBL_PMD_MODIFIED;
1675e9e3d77SNicholas Piggin 			continue;
1685e9e3d77SNicholas Piggin 		}
1695e9e3d77SNicholas Piggin 
170f7ee1f13SChristophe Leroy 		if (vmap_pte_range(pmd, addr, next, phys_addr, prot, max_page_shift, mask))
1715e9e3d77SNicholas Piggin 			return -ENOMEM;
1725e9e3d77SNicholas Piggin 	} while (pmd++, phys_addr += (next - addr), addr = next, addr != end);
1735e9e3d77SNicholas Piggin 	return 0;
1745e9e3d77SNicholas Piggin }
1755e9e3d77SNicholas Piggin 
1765e9e3d77SNicholas Piggin static int vmap_try_huge_pud(pud_t *pud, unsigned long addr, unsigned long end,
1775e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
1785e9e3d77SNicholas Piggin 			unsigned int max_page_shift)
1795e9e3d77SNicholas Piggin {
1805e9e3d77SNicholas Piggin 	if (max_page_shift < PUD_SHIFT)
1815e9e3d77SNicholas Piggin 		return 0;
1825e9e3d77SNicholas Piggin 
1835e9e3d77SNicholas Piggin 	if (!arch_vmap_pud_supported(prot))
1845e9e3d77SNicholas Piggin 		return 0;
1855e9e3d77SNicholas Piggin 
1865e9e3d77SNicholas Piggin 	if ((end - addr) != PUD_SIZE)
1875e9e3d77SNicholas Piggin 		return 0;
1885e9e3d77SNicholas Piggin 
1895e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(addr, PUD_SIZE))
1905e9e3d77SNicholas Piggin 		return 0;
1915e9e3d77SNicholas Piggin 
1925e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(phys_addr, PUD_SIZE))
1935e9e3d77SNicholas Piggin 		return 0;
1945e9e3d77SNicholas Piggin 
1955e9e3d77SNicholas Piggin 	if (pud_present(*pud) && !pud_free_pmd_page(pud, addr))
1965e9e3d77SNicholas Piggin 		return 0;
1975e9e3d77SNicholas Piggin 
1985e9e3d77SNicholas Piggin 	return pud_set_huge(pud, phys_addr, prot);
1995e9e3d77SNicholas Piggin }
2005e9e3d77SNicholas Piggin 
2015e9e3d77SNicholas Piggin static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
2025e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
2035e9e3d77SNicholas Piggin 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
2045e9e3d77SNicholas Piggin {
2055e9e3d77SNicholas Piggin 	pud_t *pud;
2065e9e3d77SNicholas Piggin 	unsigned long next;
2075e9e3d77SNicholas Piggin 
2085e9e3d77SNicholas Piggin 	pud = pud_alloc_track(&init_mm, p4d, addr, mask);
2095e9e3d77SNicholas Piggin 	if (!pud)
2105e9e3d77SNicholas Piggin 		return -ENOMEM;
2115e9e3d77SNicholas Piggin 	do {
2125e9e3d77SNicholas Piggin 		next = pud_addr_end(addr, end);
2135e9e3d77SNicholas Piggin 
2145e9e3d77SNicholas Piggin 		if (vmap_try_huge_pud(pud, addr, next, phys_addr, prot,
2155e9e3d77SNicholas Piggin 					max_page_shift)) {
2165e9e3d77SNicholas Piggin 			*mask |= PGTBL_PUD_MODIFIED;
2175e9e3d77SNicholas Piggin 			continue;
2185e9e3d77SNicholas Piggin 		}
2195e9e3d77SNicholas Piggin 
2205e9e3d77SNicholas Piggin 		if (vmap_pmd_range(pud, addr, next, phys_addr, prot,
2215e9e3d77SNicholas Piggin 					max_page_shift, mask))
2225e9e3d77SNicholas Piggin 			return -ENOMEM;
2235e9e3d77SNicholas Piggin 	} while (pud++, phys_addr += (next - addr), addr = next, addr != end);
2245e9e3d77SNicholas Piggin 	return 0;
2255e9e3d77SNicholas Piggin }
2265e9e3d77SNicholas Piggin 
2275e9e3d77SNicholas Piggin static int vmap_try_huge_p4d(p4d_t *p4d, unsigned long addr, unsigned long end,
2285e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
2295e9e3d77SNicholas Piggin 			unsigned int max_page_shift)
2305e9e3d77SNicholas Piggin {
2315e9e3d77SNicholas Piggin 	if (max_page_shift < P4D_SHIFT)
2325e9e3d77SNicholas Piggin 		return 0;
2335e9e3d77SNicholas Piggin 
2345e9e3d77SNicholas Piggin 	if (!arch_vmap_p4d_supported(prot))
2355e9e3d77SNicholas Piggin 		return 0;
2365e9e3d77SNicholas Piggin 
2375e9e3d77SNicholas Piggin 	if ((end - addr) != P4D_SIZE)
2385e9e3d77SNicholas Piggin 		return 0;
2395e9e3d77SNicholas Piggin 
2405e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(addr, P4D_SIZE))
2415e9e3d77SNicholas Piggin 		return 0;
2425e9e3d77SNicholas Piggin 
2435e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(phys_addr, P4D_SIZE))
2445e9e3d77SNicholas Piggin 		return 0;
2455e9e3d77SNicholas Piggin 
2465e9e3d77SNicholas Piggin 	if (p4d_present(*p4d) && !p4d_free_pud_page(p4d, addr))
2475e9e3d77SNicholas Piggin 		return 0;
2485e9e3d77SNicholas Piggin 
2495e9e3d77SNicholas Piggin 	return p4d_set_huge(p4d, phys_addr, prot);
2505e9e3d77SNicholas Piggin }
2515e9e3d77SNicholas Piggin 
2525e9e3d77SNicholas Piggin static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
2535e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
2545e9e3d77SNicholas Piggin 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
2555e9e3d77SNicholas Piggin {
2565e9e3d77SNicholas Piggin 	p4d_t *p4d;
2575e9e3d77SNicholas Piggin 	unsigned long next;
2585e9e3d77SNicholas Piggin 
2595e9e3d77SNicholas Piggin 	p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
2605e9e3d77SNicholas Piggin 	if (!p4d)
2615e9e3d77SNicholas Piggin 		return -ENOMEM;
2625e9e3d77SNicholas Piggin 	do {
2635e9e3d77SNicholas Piggin 		next = p4d_addr_end(addr, end);
2645e9e3d77SNicholas Piggin 
2655e9e3d77SNicholas Piggin 		if (vmap_try_huge_p4d(p4d, addr, next, phys_addr, prot,
2665e9e3d77SNicholas Piggin 					max_page_shift)) {
2675e9e3d77SNicholas Piggin 			*mask |= PGTBL_P4D_MODIFIED;
2685e9e3d77SNicholas Piggin 			continue;
2695e9e3d77SNicholas Piggin 		}
2705e9e3d77SNicholas Piggin 
2715e9e3d77SNicholas Piggin 		if (vmap_pud_range(p4d, addr, next, phys_addr, prot,
2725e9e3d77SNicholas Piggin 					max_page_shift, mask))
2735e9e3d77SNicholas Piggin 			return -ENOMEM;
2745e9e3d77SNicholas Piggin 	} while (p4d++, phys_addr += (next - addr), addr = next, addr != end);
2755e9e3d77SNicholas Piggin 	return 0;
2765e9e3d77SNicholas Piggin }
2775e9e3d77SNicholas Piggin 
2785d87510dSNicholas Piggin static int vmap_range_noflush(unsigned long addr, unsigned long end,
2795e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
2805e9e3d77SNicholas Piggin 			unsigned int max_page_shift)
2815e9e3d77SNicholas Piggin {
2825e9e3d77SNicholas Piggin 	pgd_t *pgd;
2835e9e3d77SNicholas Piggin 	unsigned long start;
2845e9e3d77SNicholas Piggin 	unsigned long next;
2855e9e3d77SNicholas Piggin 	int err;
2865e9e3d77SNicholas Piggin 	pgtbl_mod_mask mask = 0;
2875e9e3d77SNicholas Piggin 
2885e9e3d77SNicholas Piggin 	might_sleep();
2895e9e3d77SNicholas Piggin 	BUG_ON(addr >= end);
2905e9e3d77SNicholas Piggin 
2915e9e3d77SNicholas Piggin 	start = addr;
2925e9e3d77SNicholas Piggin 	pgd = pgd_offset_k(addr);
2935e9e3d77SNicholas Piggin 	do {
2945e9e3d77SNicholas Piggin 		next = pgd_addr_end(addr, end);
2955e9e3d77SNicholas Piggin 		err = vmap_p4d_range(pgd, addr, next, phys_addr, prot,
2965e9e3d77SNicholas Piggin 					max_page_shift, &mask);
2975e9e3d77SNicholas Piggin 		if (err)
2985e9e3d77SNicholas Piggin 			break;
2995e9e3d77SNicholas Piggin 	} while (pgd++, phys_addr += (next - addr), addr = next, addr != end);
3005e9e3d77SNicholas Piggin 
3015e9e3d77SNicholas Piggin 	if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
3025e9e3d77SNicholas Piggin 		arch_sync_kernel_mappings(start, end);
3035e9e3d77SNicholas Piggin 
3045e9e3d77SNicholas Piggin 	return err;
3055e9e3d77SNicholas Piggin }
306b221385bSAdrian Bunk 
30782a70ce0SChristoph Hellwig int ioremap_page_range(unsigned long addr, unsigned long end,
30882a70ce0SChristoph Hellwig 		phys_addr_t phys_addr, pgprot_t prot)
3095d87510dSNicholas Piggin {
3105d87510dSNicholas Piggin 	int err;
3115d87510dSNicholas Piggin 
3128491502fSChristoph Hellwig 	err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot),
31382a70ce0SChristoph Hellwig 				 ioremap_max_page_shift);
3145d87510dSNicholas Piggin 	flush_cache_vmap(addr, end);
315b073d7f8SAlexander Potapenko 	if (!err)
316b073d7f8SAlexander Potapenko 		kmsan_ioremap_page_range(addr, end, phys_addr, prot,
317b073d7f8SAlexander Potapenko 					 ioremap_max_page_shift);
3185d87510dSNicholas Piggin 	return err;
3195d87510dSNicholas Piggin }
3205d87510dSNicholas Piggin 
3212ba3e694SJoerg Roedel static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
3222ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds 	pte_t *pte;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	pte = pte_offset_kernel(pmd, addr);
3271da177e4SLinus Torvalds 	do {
3281da177e4SLinus Torvalds 		pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
3291da177e4SLinus Torvalds 		WARN_ON(!pte_none(ptent) && !pte_present(ptent));
3301da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
3312ba3e694SJoerg Roedel 	*mask |= PGTBL_PTE_MODIFIED;
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
3342ba3e694SJoerg Roedel static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
3352ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
3361da177e4SLinus Torvalds {
3371da177e4SLinus Torvalds 	pmd_t *pmd;
3381da177e4SLinus Torvalds 	unsigned long next;
3392ba3e694SJoerg Roedel 	int cleared;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
3421da177e4SLinus Torvalds 	do {
3431da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
3442ba3e694SJoerg Roedel 
3452ba3e694SJoerg Roedel 		cleared = pmd_clear_huge(pmd);
3462ba3e694SJoerg Roedel 		if (cleared || pmd_bad(*pmd))
3472ba3e694SJoerg Roedel 			*mask |= PGTBL_PMD_MODIFIED;
3482ba3e694SJoerg Roedel 
3492ba3e694SJoerg Roedel 		if (cleared)
350b9820d8fSToshi Kani 			continue;
3511da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
3521da177e4SLinus Torvalds 			continue;
3532ba3e694SJoerg Roedel 		vunmap_pte_range(pmd, addr, next, mask);
354e47110e9SAneesh Kumar K.V 
355e47110e9SAneesh Kumar K.V 		cond_resched();
3561da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds 
3592ba3e694SJoerg Roedel static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
3602ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	pud_t *pud;
3631da177e4SLinus Torvalds 	unsigned long next;
3642ba3e694SJoerg Roedel 	int cleared;
3651da177e4SLinus Torvalds 
366c2febafcSKirill A. Shutemov 	pud = pud_offset(p4d, addr);
3671da177e4SLinus Torvalds 	do {
3681da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
3692ba3e694SJoerg Roedel 
3702ba3e694SJoerg Roedel 		cleared = pud_clear_huge(pud);
3712ba3e694SJoerg Roedel 		if (cleared || pud_bad(*pud))
3722ba3e694SJoerg Roedel 			*mask |= PGTBL_PUD_MODIFIED;
3732ba3e694SJoerg Roedel 
3742ba3e694SJoerg Roedel 		if (cleared)
375b9820d8fSToshi Kani 			continue;
3761da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
3771da177e4SLinus Torvalds 			continue;
3782ba3e694SJoerg Roedel 		vunmap_pmd_range(pud, addr, next, mask);
3791da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3822ba3e694SJoerg Roedel static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
3832ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
384c2febafcSKirill A. Shutemov {
385c2febafcSKirill A. Shutemov 	p4d_t *p4d;
386c2febafcSKirill A. Shutemov 	unsigned long next;
387c2febafcSKirill A. Shutemov 
388c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
389c2febafcSKirill A. Shutemov 	do {
390c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
3912ba3e694SJoerg Roedel 
392c8db8c26SLi kunyu 		p4d_clear_huge(p4d);
393c8db8c26SLi kunyu 		if (p4d_bad(*p4d))
3942ba3e694SJoerg Roedel 			*mask |= PGTBL_P4D_MODIFIED;
3952ba3e694SJoerg Roedel 
396c2febafcSKirill A. Shutemov 		if (p4d_none_or_clear_bad(p4d))
397c2febafcSKirill A. Shutemov 			continue;
3982ba3e694SJoerg Roedel 		vunmap_pud_range(p4d, addr, next, mask);
399c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
400c2febafcSKirill A. Shutemov }
401c2febafcSKirill A. Shutemov 
4024ad0ae8cSNicholas Piggin /*
4034ad0ae8cSNicholas Piggin  * vunmap_range_noflush is similar to vunmap_range, but does not
4044ad0ae8cSNicholas Piggin  * flush caches or TLBs.
405b521c43fSChristoph Hellwig  *
4064ad0ae8cSNicholas Piggin  * The caller is responsible for calling flush_cache_vmap() before calling
4074ad0ae8cSNicholas Piggin  * this function, and flush_tlb_kernel_range after it has returned
4084ad0ae8cSNicholas Piggin  * successfully (and before the addresses are expected to cause a page fault
4094ad0ae8cSNicholas Piggin  * or be re-mapped for something else, if TLB flushes are being delayed or
4104ad0ae8cSNicholas Piggin  * coalesced).
411b521c43fSChristoph Hellwig  *
4124ad0ae8cSNicholas Piggin  * This is an internal function only. Do not use outside mm/.
413b521c43fSChristoph Hellwig  */
414b073d7f8SAlexander Potapenko void __vunmap_range_noflush(unsigned long start, unsigned long end)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	unsigned long next;
417b521c43fSChristoph Hellwig 	pgd_t *pgd;
4182ba3e694SJoerg Roedel 	unsigned long addr = start;
4192ba3e694SJoerg Roedel 	pgtbl_mod_mask mask = 0;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	BUG_ON(addr >= end);
4221da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
4231da177e4SLinus Torvalds 	do {
4241da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
4252ba3e694SJoerg Roedel 		if (pgd_bad(*pgd))
4262ba3e694SJoerg Roedel 			mask |= PGTBL_PGD_MODIFIED;
4271da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
4281da177e4SLinus Torvalds 			continue;
4292ba3e694SJoerg Roedel 		vunmap_p4d_range(pgd, addr, next, &mask);
4301da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
4312ba3e694SJoerg Roedel 
4322ba3e694SJoerg Roedel 	if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
4332ba3e694SJoerg Roedel 		arch_sync_kernel_mappings(start, end);
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
436b073d7f8SAlexander Potapenko void vunmap_range_noflush(unsigned long start, unsigned long end)
437b073d7f8SAlexander Potapenko {
438b073d7f8SAlexander Potapenko 	kmsan_vunmap_range_noflush(start, end);
439b073d7f8SAlexander Potapenko 	__vunmap_range_noflush(start, end);
440b073d7f8SAlexander Potapenko }
441b073d7f8SAlexander Potapenko 
4424ad0ae8cSNicholas Piggin /**
4434ad0ae8cSNicholas Piggin  * vunmap_range - unmap kernel virtual addresses
4444ad0ae8cSNicholas Piggin  * @addr: start of the VM area to unmap
4454ad0ae8cSNicholas Piggin  * @end: end of the VM area to unmap (non-inclusive)
4464ad0ae8cSNicholas Piggin  *
4474ad0ae8cSNicholas Piggin  * Clears any present PTEs in the virtual address range, flushes TLBs and
4484ad0ae8cSNicholas Piggin  * caches. Any subsequent access to the address before it has been re-mapped
4494ad0ae8cSNicholas Piggin  * is a kernel bug.
4504ad0ae8cSNicholas Piggin  */
4514ad0ae8cSNicholas Piggin void vunmap_range(unsigned long addr, unsigned long end)
4524ad0ae8cSNicholas Piggin {
4534ad0ae8cSNicholas Piggin 	flush_cache_vunmap(addr, end);
4544ad0ae8cSNicholas Piggin 	vunmap_range_noflush(addr, end);
4554ad0ae8cSNicholas Piggin 	flush_tlb_kernel_range(addr, end);
4564ad0ae8cSNicholas Piggin }
4574ad0ae8cSNicholas Piggin 
4580a264884SNicholas Piggin static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr,
4592ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
4602ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	pte_t *pte;
4631da177e4SLinus Torvalds 
464db64fe02SNick Piggin 	/*
465db64fe02SNick Piggin 	 * nr is a running index into the array which helps higher level
466db64fe02SNick Piggin 	 * callers keep track of where we're up to.
467db64fe02SNick Piggin 	 */
468db64fe02SNick Piggin 
4692ba3e694SJoerg Roedel 	pte = pte_alloc_kernel_track(pmd, addr, mask);
4701da177e4SLinus Torvalds 	if (!pte)
4711da177e4SLinus Torvalds 		return -ENOMEM;
4721da177e4SLinus Torvalds 	do {
473db64fe02SNick Piggin 		struct page *page = pages[*nr];
474db64fe02SNick Piggin 
475db64fe02SNick Piggin 		if (WARN_ON(!pte_none(*pte)))
476db64fe02SNick Piggin 			return -EBUSY;
477db64fe02SNick Piggin 		if (WARN_ON(!page))
4781da177e4SLinus Torvalds 			return -ENOMEM;
4794fcdcc12SYury Norov 		if (WARN_ON(!pfn_valid(page_to_pfn(page))))
4804fcdcc12SYury Norov 			return -EINVAL;
4814fcdcc12SYury Norov 
4821da177e4SLinus Torvalds 		set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
483db64fe02SNick Piggin 		(*nr)++;
4841da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
4852ba3e694SJoerg Roedel 	*mask |= PGTBL_PTE_MODIFIED;
4861da177e4SLinus Torvalds 	return 0;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
4890a264884SNicholas Piggin static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,
4902ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
4912ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
4921da177e4SLinus Torvalds {
4931da177e4SLinus Torvalds 	pmd_t *pmd;
4941da177e4SLinus Torvalds 	unsigned long next;
4951da177e4SLinus Torvalds 
4962ba3e694SJoerg Roedel 	pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
4971da177e4SLinus Torvalds 	if (!pmd)
4981da177e4SLinus Torvalds 		return -ENOMEM;
4991da177e4SLinus Torvalds 	do {
5001da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
5010a264884SNicholas Piggin 		if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask))
5021da177e4SLinus Torvalds 			return -ENOMEM;
5031da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
5041da177e4SLinus Torvalds 	return 0;
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5070a264884SNicholas Piggin static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
5082ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
5092ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
5101da177e4SLinus Torvalds {
5111da177e4SLinus Torvalds 	pud_t *pud;
5121da177e4SLinus Torvalds 	unsigned long next;
5131da177e4SLinus Torvalds 
5142ba3e694SJoerg Roedel 	pud = pud_alloc_track(&init_mm, p4d, addr, mask);
5151da177e4SLinus Torvalds 	if (!pud)
5161da177e4SLinus Torvalds 		return -ENOMEM;
5171da177e4SLinus Torvalds 	do {
5181da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
5190a264884SNicholas Piggin 		if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask))
5201da177e4SLinus Torvalds 			return -ENOMEM;
5211da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
5221da177e4SLinus Torvalds 	return 0;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5250a264884SNicholas Piggin static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
5262ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
5272ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
528c2febafcSKirill A. Shutemov {
529c2febafcSKirill A. Shutemov 	p4d_t *p4d;
530c2febafcSKirill A. Shutemov 	unsigned long next;
531c2febafcSKirill A. Shutemov 
5322ba3e694SJoerg Roedel 	p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
533c2febafcSKirill A. Shutemov 	if (!p4d)
534c2febafcSKirill A. Shutemov 		return -ENOMEM;
535c2febafcSKirill A. Shutemov 	do {
536c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
5370a264884SNicholas Piggin 		if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask))
538c2febafcSKirill A. Shutemov 			return -ENOMEM;
539c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
540c2febafcSKirill A. Shutemov 	return 0;
541c2febafcSKirill A. Shutemov }
542c2febafcSKirill A. Shutemov 
543121e6f32SNicholas Piggin static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
544121e6f32SNicholas Piggin 		pgprot_t prot, struct page **pages)
545121e6f32SNicholas Piggin {
546121e6f32SNicholas Piggin 	unsigned long start = addr;
547121e6f32SNicholas Piggin 	pgd_t *pgd;
548121e6f32SNicholas Piggin 	unsigned long next;
549121e6f32SNicholas Piggin 	int err = 0;
550121e6f32SNicholas Piggin 	int nr = 0;
551121e6f32SNicholas Piggin 	pgtbl_mod_mask mask = 0;
552121e6f32SNicholas Piggin 
553121e6f32SNicholas Piggin 	BUG_ON(addr >= end);
554121e6f32SNicholas Piggin 	pgd = pgd_offset_k(addr);
555121e6f32SNicholas Piggin 	do {
556121e6f32SNicholas Piggin 		next = pgd_addr_end(addr, end);
557121e6f32SNicholas Piggin 		if (pgd_bad(*pgd))
558121e6f32SNicholas Piggin 			mask |= PGTBL_PGD_MODIFIED;
559121e6f32SNicholas Piggin 		err = vmap_pages_p4d_range(pgd, addr, next, prot, pages, &nr, &mask);
560121e6f32SNicholas Piggin 		if (err)
561121e6f32SNicholas Piggin 			return err;
562121e6f32SNicholas Piggin 	} while (pgd++, addr = next, addr != end);
563121e6f32SNicholas Piggin 
564121e6f32SNicholas Piggin 	if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
565121e6f32SNicholas Piggin 		arch_sync_kernel_mappings(start, end);
566121e6f32SNicholas Piggin 
567121e6f32SNicholas Piggin 	return 0;
568121e6f32SNicholas Piggin }
569121e6f32SNicholas Piggin 
570b67177ecSNicholas Piggin /*
571b67177ecSNicholas Piggin  * vmap_pages_range_noflush is similar to vmap_pages_range, but does not
572b67177ecSNicholas Piggin  * flush caches.
573b67177ecSNicholas Piggin  *
574b67177ecSNicholas Piggin  * The caller is responsible for calling flush_cache_vmap() after this
575b67177ecSNicholas Piggin  * function returns successfully and before the addresses are accessed.
576b67177ecSNicholas Piggin  *
577b67177ecSNicholas Piggin  * This is an internal function only. Do not use outside mm/.
578b67177ecSNicholas Piggin  */
579b073d7f8SAlexander Potapenko int __vmap_pages_range_noflush(unsigned long addr, unsigned long end,
580121e6f32SNicholas Piggin 		pgprot_t prot, struct page **pages, unsigned int page_shift)
581121e6f32SNicholas Piggin {
582121e6f32SNicholas Piggin 	unsigned int i, nr = (end - addr) >> PAGE_SHIFT;
583121e6f32SNicholas Piggin 
584121e6f32SNicholas Piggin 	WARN_ON(page_shift < PAGE_SHIFT);
585121e6f32SNicholas Piggin 
586121e6f32SNicholas Piggin 	if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC) ||
587121e6f32SNicholas Piggin 			page_shift == PAGE_SHIFT)
588121e6f32SNicholas Piggin 		return vmap_small_pages_range_noflush(addr, end, prot, pages);
589121e6f32SNicholas Piggin 
590121e6f32SNicholas Piggin 	for (i = 0; i < nr; i += 1U << (page_shift - PAGE_SHIFT)) {
591121e6f32SNicholas Piggin 		int err;
592121e6f32SNicholas Piggin 
593121e6f32SNicholas Piggin 		err = vmap_range_noflush(addr, addr + (1UL << page_shift),
59408262ac5SMatthew Wilcox 					page_to_phys(pages[i]), prot,
595121e6f32SNicholas Piggin 					page_shift);
596121e6f32SNicholas Piggin 		if (err)
597121e6f32SNicholas Piggin 			return err;
598121e6f32SNicholas Piggin 
599121e6f32SNicholas Piggin 		addr += 1UL << page_shift;
600121e6f32SNicholas Piggin 	}
601121e6f32SNicholas Piggin 
602121e6f32SNicholas Piggin 	return 0;
603121e6f32SNicholas Piggin }
604121e6f32SNicholas Piggin 
605b073d7f8SAlexander Potapenko int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
606b073d7f8SAlexander Potapenko 		pgprot_t prot, struct page **pages, unsigned int page_shift)
607b073d7f8SAlexander Potapenko {
608b073d7f8SAlexander Potapenko 	kmsan_vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
609b073d7f8SAlexander Potapenko 	return __vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
610b073d7f8SAlexander Potapenko }
611b073d7f8SAlexander Potapenko 
612b67177ecSNicholas Piggin /**
613b67177ecSNicholas Piggin  * vmap_pages_range - map pages to a kernel virtual address
614b67177ecSNicholas Piggin  * @addr: start of the VM area to map
615b67177ecSNicholas Piggin  * @end: end of the VM area to map (non-inclusive)
616b67177ecSNicholas Piggin  * @prot: page protection flags to use
617b67177ecSNicholas Piggin  * @pages: pages to map (always PAGE_SIZE pages)
618b67177ecSNicholas Piggin  * @page_shift: maximum shift that the pages may be mapped with, @pages must
619b67177ecSNicholas Piggin  * be aligned and contiguous up to at least this shift.
620b67177ecSNicholas Piggin  *
621b67177ecSNicholas Piggin  * RETURNS:
622b67177ecSNicholas Piggin  * 0 on success, -errno on failure.
623b67177ecSNicholas Piggin  */
624121e6f32SNicholas Piggin static int vmap_pages_range(unsigned long addr, unsigned long end,
625121e6f32SNicholas Piggin 		pgprot_t prot, struct page **pages, unsigned int page_shift)
626121e6f32SNicholas Piggin {
627121e6f32SNicholas Piggin 	int err;
628121e6f32SNicholas Piggin 
629121e6f32SNicholas Piggin 	err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
630121e6f32SNicholas Piggin 	flush_cache_vmap(addr, end);
631121e6f32SNicholas Piggin 	return err;
632121e6f32SNicholas Piggin }
633121e6f32SNicholas Piggin 
63481ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x)
63573bdf0a6SLinus Torvalds {
63673bdf0a6SLinus Torvalds 	/*
637ab4f2ee1SRussell King 	 * ARM, x86-64 and sparc64 put modules in a special place,
63873bdf0a6SLinus Torvalds 	 * and fall back on vmalloc() if that fails. Others
63973bdf0a6SLinus Torvalds 	 * just put it in the vmalloc space.
64073bdf0a6SLinus Torvalds 	 */
64173bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
6424aff1dc4SAndrey Konovalov 	unsigned long addr = (unsigned long)kasan_reset_tag(x);
64373bdf0a6SLinus Torvalds 	if (addr >= MODULES_VADDR && addr < MODULES_END)
64473bdf0a6SLinus Torvalds 		return 1;
64573bdf0a6SLinus Torvalds #endif
64673bdf0a6SLinus Torvalds 	return is_vmalloc_addr(x);
64773bdf0a6SLinus Torvalds }
64873bdf0a6SLinus Torvalds 
64948667e7aSChristoph Lameter /*
650c0eb315aSNicholas Piggin  * Walk a vmap address to the struct page it maps. Huge vmap mappings will
651c0eb315aSNicholas Piggin  * return the tail page that corresponds to the base page address, which
652c0eb315aSNicholas Piggin  * matches small vmap mappings.
65348667e7aSChristoph Lameter  */
654add688fbSmalc struct page *vmalloc_to_page(const void *vmalloc_addr)
65548667e7aSChristoph Lameter {
65648667e7aSChristoph Lameter 	unsigned long addr = (unsigned long) vmalloc_addr;
657add688fbSmalc 	struct page *page = NULL;
65848667e7aSChristoph Lameter 	pgd_t *pgd = pgd_offset_k(addr);
659c2febafcSKirill A. Shutemov 	p4d_t *p4d;
660c2febafcSKirill A. Shutemov 	pud_t *pud;
661c2febafcSKirill A. Shutemov 	pmd_t *pmd;
662c2febafcSKirill A. Shutemov 	pte_t *ptep, pte;
66348667e7aSChristoph Lameter 
6647aa413deSIngo Molnar 	/*
6657aa413deSIngo Molnar 	 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
6667aa413deSIngo Molnar 	 * architectures that do not vmalloc module space
6677aa413deSIngo Molnar 	 */
66873bdf0a6SLinus Torvalds 	VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
66959ea7463SJiri Slaby 
670c2febafcSKirill A. Shutemov 	if (pgd_none(*pgd))
671c2febafcSKirill A. Shutemov 		return NULL;
672c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(pgd_leaf(*pgd)))
673c0eb315aSNicholas Piggin 		return NULL; /* XXX: no allowance for huge pgd */
674c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(pgd_bad(*pgd)))
675c0eb315aSNicholas Piggin 		return NULL;
676c0eb315aSNicholas Piggin 
677c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
678c2febafcSKirill A. Shutemov 	if (p4d_none(*p4d))
679c2febafcSKirill A. Shutemov 		return NULL;
680c0eb315aSNicholas Piggin 	if (p4d_leaf(*p4d))
681c0eb315aSNicholas Piggin 		return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT);
682c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(p4d_bad(*p4d)))
683c2febafcSKirill A. Shutemov 		return NULL;
684c0eb315aSNicholas Piggin 
685c0eb315aSNicholas Piggin 	pud = pud_offset(p4d, addr);
686c0eb315aSNicholas Piggin 	if (pud_none(*pud))
687c0eb315aSNicholas Piggin 		return NULL;
688c0eb315aSNicholas Piggin 	if (pud_leaf(*pud))
689c0eb315aSNicholas Piggin 		return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
690c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(pud_bad(*pud)))
691c0eb315aSNicholas Piggin 		return NULL;
692c0eb315aSNicholas Piggin 
693c2febafcSKirill A. Shutemov 	pmd = pmd_offset(pud, addr);
694c0eb315aSNicholas Piggin 	if (pmd_none(*pmd))
695c0eb315aSNicholas Piggin 		return NULL;
696c0eb315aSNicholas Piggin 	if (pmd_leaf(*pmd))
697c0eb315aSNicholas Piggin 		return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
698c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(pmd_bad(*pmd)))
699c2febafcSKirill A. Shutemov 		return NULL;
700db64fe02SNick Piggin 
70148667e7aSChristoph Lameter 	ptep = pte_offset_map(pmd, addr);
70248667e7aSChristoph Lameter 	pte = *ptep;
70348667e7aSChristoph Lameter 	if (pte_present(pte))
704add688fbSmalc 		page = pte_page(pte);
70548667e7aSChristoph Lameter 	pte_unmap(ptep);
706c0eb315aSNicholas Piggin 
707add688fbSmalc 	return page;
708ece86e22SJianyu Zhan }
709ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page);
710ece86e22SJianyu Zhan 
711add688fbSmalc /*
712add688fbSmalc  * Map a vmalloc()-space virtual address to the physical page frame number.
713add688fbSmalc  */
714add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
715add688fbSmalc {
716add688fbSmalc 	return page_to_pfn(vmalloc_to_page(vmalloc_addr));
717add688fbSmalc }
718add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn);
719add688fbSmalc 
720db64fe02SNick Piggin 
721db64fe02SNick Piggin /*** Global kva allocator ***/
722db64fe02SNick Piggin 
723bb850f4dSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_PROPAGATE_CHECK 0
724a6cf4e0fSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0
725bb850f4dSUladzislau Rezki (Sony) 
726db64fe02SNick Piggin 
727db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock);
728e36176beSUladzislau Rezki (Sony) static DEFINE_SPINLOCK(free_vmap_area_lock);
729f1c4069eSJoonsoo Kim /* Export for kexec only */
730f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
73189699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT;
73268ad4a33SUladzislau Rezki (Sony) static bool vmap_initialized __read_mostly;
73389699605SNick Piggin 
73496e2db45SUladzislau Rezki (Sony) static struct rb_root purge_vmap_area_root = RB_ROOT;
73596e2db45SUladzislau Rezki (Sony) static LIST_HEAD(purge_vmap_area_list);
73696e2db45SUladzislau Rezki (Sony) static DEFINE_SPINLOCK(purge_vmap_area_lock);
73796e2db45SUladzislau Rezki (Sony) 
73868ad4a33SUladzislau Rezki (Sony) /*
73968ad4a33SUladzislau Rezki (Sony)  * This kmem_cache is used for vmap_area objects. Instead of
74068ad4a33SUladzislau Rezki (Sony)  * allocating from slab we reuse an object from this cache to
74168ad4a33SUladzislau Rezki (Sony)  * make things faster. Especially in "no edge" splitting of
74268ad4a33SUladzislau Rezki (Sony)  * free block.
74368ad4a33SUladzislau Rezki (Sony)  */
74468ad4a33SUladzislau Rezki (Sony) static struct kmem_cache *vmap_area_cachep;
74589699605SNick Piggin 
74668ad4a33SUladzislau Rezki (Sony) /*
74768ad4a33SUladzislau Rezki (Sony)  * This linked list is used in pair with free_vmap_area_root.
74868ad4a33SUladzislau Rezki (Sony)  * It gives O(1) access to prev/next to perform fast coalescing.
74968ad4a33SUladzislau Rezki (Sony)  */
75068ad4a33SUladzislau Rezki (Sony) static LIST_HEAD(free_vmap_area_list);
75168ad4a33SUladzislau Rezki (Sony) 
75268ad4a33SUladzislau Rezki (Sony) /*
75368ad4a33SUladzislau Rezki (Sony)  * This augment red-black tree represents the free vmap space.
75468ad4a33SUladzislau Rezki (Sony)  * All vmap_area objects in this tree are sorted by va->va_start
75568ad4a33SUladzislau Rezki (Sony)  * address. It is used for allocation and merging when a vmap
75668ad4a33SUladzislau Rezki (Sony)  * object is released.
75768ad4a33SUladzislau Rezki (Sony)  *
75868ad4a33SUladzislau Rezki (Sony)  * Each vmap_area node contains a maximum available free block
75968ad4a33SUladzislau Rezki (Sony)  * of its sub-tree, right or left. Therefore it is possible to
76068ad4a33SUladzislau Rezki (Sony)  * find a lowest match of free area.
76168ad4a33SUladzislau Rezki (Sony)  */
76268ad4a33SUladzislau Rezki (Sony) static struct rb_root free_vmap_area_root = RB_ROOT;
76368ad4a33SUladzislau Rezki (Sony) 
76482dd23e8SUladzislau Rezki (Sony) /*
76582dd23e8SUladzislau Rezki (Sony)  * Preload a CPU with one object for "no edge" split case. The
76682dd23e8SUladzislau Rezki (Sony)  * aim is to get rid of allocations from the atomic context, thus
76782dd23e8SUladzislau Rezki (Sony)  * to use more permissive allocation masks.
76882dd23e8SUladzislau Rezki (Sony)  */
76982dd23e8SUladzislau Rezki (Sony) static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node);
77082dd23e8SUladzislau Rezki (Sony) 
77168ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
77268ad4a33SUladzislau Rezki (Sony) va_size(struct vmap_area *va)
77368ad4a33SUladzislau Rezki (Sony) {
77468ad4a33SUladzislau Rezki (Sony) 	return (va->va_end - va->va_start);
77568ad4a33SUladzislau Rezki (Sony) }
77668ad4a33SUladzislau Rezki (Sony) 
77768ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
77868ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(struct rb_node *node)
77968ad4a33SUladzislau Rezki (Sony) {
78068ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
78168ad4a33SUladzislau Rezki (Sony) 
78268ad4a33SUladzislau Rezki (Sony) 	va = rb_entry_safe(node, struct vmap_area, rb_node);
78368ad4a33SUladzislau Rezki (Sony) 	return va ? va->subtree_max_size : 0;
78468ad4a33SUladzislau Rezki (Sony) }
78568ad4a33SUladzislau Rezki (Sony) 
786315cc066SMichel Lespinasse RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb,
787315cc066SMichel Lespinasse 	struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size)
78868ad4a33SUladzislau Rezki (Sony) 
78968ad4a33SUladzislau Rezki (Sony) static void purge_vmap_area_lazy(void);
79068ad4a33SUladzislau Rezki (Sony) static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
791690467c8SUladzislau Rezki (Sony) static void drain_vmap_area_work(struct work_struct *work);
792690467c8SUladzislau Rezki (Sony) static DECLARE_WORK(drain_vmap_work, drain_vmap_area_work);
793db64fe02SNick Piggin 
79497105f0aSRoman Gushchin static atomic_long_t nr_vmalloc_pages;
79597105f0aSRoman Gushchin 
79697105f0aSRoman Gushchin unsigned long vmalloc_nr_pages(void)
79797105f0aSRoman Gushchin {
79897105f0aSRoman Gushchin 	return atomic_long_read(&nr_vmalloc_pages);
79997105f0aSRoman Gushchin }
80097105f0aSRoman Gushchin 
801153090f2SBaoquan He /* Look up the first VA which satisfies addr < va_end, NULL if none. */
802f181234aSChen Wandun static struct vmap_area *find_vmap_area_exceed_addr(unsigned long addr)
803f181234aSChen Wandun {
804f181234aSChen Wandun 	struct vmap_area *va = NULL;
805f181234aSChen Wandun 	struct rb_node *n = vmap_area_root.rb_node;
806f181234aSChen Wandun 
8074aff1dc4SAndrey Konovalov 	addr = (unsigned long)kasan_reset_tag((void *)addr);
8084aff1dc4SAndrey Konovalov 
809f181234aSChen Wandun 	while (n) {
810f181234aSChen Wandun 		struct vmap_area *tmp;
811f181234aSChen Wandun 
812f181234aSChen Wandun 		tmp = rb_entry(n, struct vmap_area, rb_node);
813f181234aSChen Wandun 		if (tmp->va_end > addr) {
814f181234aSChen Wandun 			va = tmp;
815f181234aSChen Wandun 			if (tmp->va_start <= addr)
816f181234aSChen Wandun 				break;
817f181234aSChen Wandun 
818f181234aSChen Wandun 			n = n->rb_left;
819f181234aSChen Wandun 		} else
820f181234aSChen Wandun 			n = n->rb_right;
821f181234aSChen Wandun 	}
822f181234aSChen Wandun 
823f181234aSChen Wandun 	return va;
824f181234aSChen Wandun }
825f181234aSChen Wandun 
826899c6efeSUladzislau Rezki (Sony) static struct vmap_area *__find_vmap_area(unsigned long addr, struct rb_root *root)
8271da177e4SLinus Torvalds {
828899c6efeSUladzislau Rezki (Sony) 	struct rb_node *n = root->rb_node;
829db64fe02SNick Piggin 
8304aff1dc4SAndrey Konovalov 	addr = (unsigned long)kasan_reset_tag((void *)addr);
8314aff1dc4SAndrey Konovalov 
832db64fe02SNick Piggin 	while (n) {
833db64fe02SNick Piggin 		struct vmap_area *va;
834db64fe02SNick Piggin 
835db64fe02SNick Piggin 		va = rb_entry(n, struct vmap_area, rb_node);
836db64fe02SNick Piggin 		if (addr < va->va_start)
837db64fe02SNick Piggin 			n = n->rb_left;
838cef2ac3fSHATAYAMA Daisuke 		else if (addr >= va->va_end)
839db64fe02SNick Piggin 			n = n->rb_right;
840db64fe02SNick Piggin 		else
841db64fe02SNick Piggin 			return va;
842db64fe02SNick Piggin 	}
843db64fe02SNick Piggin 
844db64fe02SNick Piggin 	return NULL;
845db64fe02SNick Piggin }
846db64fe02SNick Piggin 
84768ad4a33SUladzislau Rezki (Sony) /*
84868ad4a33SUladzislau Rezki (Sony)  * This function returns back addresses of parent node
84968ad4a33SUladzislau Rezki (Sony)  * and its left or right link for further processing.
8509c801f61SUladzislau Rezki (Sony)  *
8519c801f61SUladzislau Rezki (Sony)  * Otherwise NULL is returned. In that case all further
8529c801f61SUladzislau Rezki (Sony)  * steps regarding inserting of conflicting overlap range
8539c801f61SUladzislau Rezki (Sony)  * have to be declined and actually considered as a bug.
85468ad4a33SUladzislau Rezki (Sony)  */
85568ad4a33SUladzislau Rezki (Sony) static __always_inline struct rb_node **
85668ad4a33SUladzislau Rezki (Sony) find_va_links(struct vmap_area *va,
85768ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct rb_node *from,
85868ad4a33SUladzislau Rezki (Sony) 	struct rb_node **parent)
859db64fe02SNick Piggin {
860170168d0SNamhyung Kim 	struct vmap_area *tmp_va;
86168ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
862db64fe02SNick Piggin 
86368ad4a33SUladzislau Rezki (Sony) 	if (root) {
86468ad4a33SUladzislau Rezki (Sony) 		link = &root->rb_node;
86568ad4a33SUladzislau Rezki (Sony) 		if (unlikely(!*link)) {
86668ad4a33SUladzislau Rezki (Sony) 			*parent = NULL;
86768ad4a33SUladzislau Rezki (Sony) 			return link;
86868ad4a33SUladzislau Rezki (Sony) 		}
86968ad4a33SUladzislau Rezki (Sony) 	} else {
87068ad4a33SUladzislau Rezki (Sony) 		link = &from;
87168ad4a33SUladzislau Rezki (Sony) 	}
87268ad4a33SUladzislau Rezki (Sony) 
87368ad4a33SUladzislau Rezki (Sony) 	/*
87468ad4a33SUladzislau Rezki (Sony) 	 * Go to the bottom of the tree. When we hit the last point
87568ad4a33SUladzislau Rezki (Sony) 	 * we end up with parent rb_node and correct direction, i name
87668ad4a33SUladzislau Rezki (Sony) 	 * it link, where the new va->rb_node will be attached to.
87768ad4a33SUladzislau Rezki (Sony) 	 */
87868ad4a33SUladzislau Rezki (Sony) 	do {
87968ad4a33SUladzislau Rezki (Sony) 		tmp_va = rb_entry(*link, struct vmap_area, rb_node);
88068ad4a33SUladzislau Rezki (Sony) 
88168ad4a33SUladzislau Rezki (Sony) 		/*
88268ad4a33SUladzislau Rezki (Sony) 		 * During the traversal we also do some sanity check.
88368ad4a33SUladzislau Rezki (Sony) 		 * Trigger the BUG() if there are sides(left/right)
88468ad4a33SUladzislau Rezki (Sony) 		 * or full overlaps.
88568ad4a33SUladzislau Rezki (Sony) 		 */
886753df96bSBaoquan He 		if (va->va_end <= tmp_va->va_start)
88768ad4a33SUladzislau Rezki (Sony) 			link = &(*link)->rb_left;
888753df96bSBaoquan He 		else if (va->va_start >= tmp_va->va_end)
88968ad4a33SUladzislau Rezki (Sony) 			link = &(*link)->rb_right;
8909c801f61SUladzislau Rezki (Sony) 		else {
8919c801f61SUladzislau Rezki (Sony) 			WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n",
8929c801f61SUladzislau Rezki (Sony) 				va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end);
8939c801f61SUladzislau Rezki (Sony) 
8949c801f61SUladzislau Rezki (Sony) 			return NULL;
8959c801f61SUladzislau Rezki (Sony) 		}
89668ad4a33SUladzislau Rezki (Sony) 	} while (*link);
89768ad4a33SUladzislau Rezki (Sony) 
89868ad4a33SUladzislau Rezki (Sony) 	*parent = &tmp_va->rb_node;
89968ad4a33SUladzislau Rezki (Sony) 	return link;
900db64fe02SNick Piggin }
901db64fe02SNick Piggin 
90268ad4a33SUladzislau Rezki (Sony) static __always_inline struct list_head *
90368ad4a33SUladzislau Rezki (Sony) get_va_next_sibling(struct rb_node *parent, struct rb_node **link)
90468ad4a33SUladzislau Rezki (Sony) {
90568ad4a33SUladzislau Rezki (Sony) 	struct list_head *list;
906db64fe02SNick Piggin 
90768ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!parent))
90868ad4a33SUladzislau Rezki (Sony) 		/*
90968ad4a33SUladzislau Rezki (Sony) 		 * The red-black tree where we try to find VA neighbors
91068ad4a33SUladzislau Rezki (Sony) 		 * before merging or inserting is empty, i.e. it means
91168ad4a33SUladzislau Rezki (Sony) 		 * there is no free vmap space. Normally it does not
91268ad4a33SUladzislau Rezki (Sony) 		 * happen but we handle this case anyway.
91368ad4a33SUladzislau Rezki (Sony) 		 */
91468ad4a33SUladzislau Rezki (Sony) 		return NULL;
91568ad4a33SUladzislau Rezki (Sony) 
91668ad4a33SUladzislau Rezki (Sony) 	list = &rb_entry(parent, struct vmap_area, rb_node)->list;
91768ad4a33SUladzislau Rezki (Sony) 	return (&parent->rb_right == link ? list->next : list);
918db64fe02SNick Piggin }
919db64fe02SNick Piggin 
92068ad4a33SUladzislau Rezki (Sony) static __always_inline void
9218eb510dbSUladzislau Rezki (Sony) __link_va(struct vmap_area *va, struct rb_root *root,
9228eb510dbSUladzislau Rezki (Sony) 	struct rb_node *parent, struct rb_node **link,
9238eb510dbSUladzislau Rezki (Sony) 	struct list_head *head, bool augment)
92468ad4a33SUladzislau Rezki (Sony) {
92568ad4a33SUladzislau Rezki (Sony) 	/*
92668ad4a33SUladzislau Rezki (Sony) 	 * VA is still not in the list, but we can
92768ad4a33SUladzislau Rezki (Sony) 	 * identify its future previous list_head node.
92868ad4a33SUladzislau Rezki (Sony) 	 */
92968ad4a33SUladzislau Rezki (Sony) 	if (likely(parent)) {
93068ad4a33SUladzislau Rezki (Sony) 		head = &rb_entry(parent, struct vmap_area, rb_node)->list;
93168ad4a33SUladzislau Rezki (Sony) 		if (&parent->rb_right != link)
93268ad4a33SUladzislau Rezki (Sony) 			head = head->prev;
93368ad4a33SUladzislau Rezki (Sony) 	}
934db64fe02SNick Piggin 
93568ad4a33SUladzislau Rezki (Sony) 	/* Insert to the rb-tree */
93668ad4a33SUladzislau Rezki (Sony) 	rb_link_node(&va->rb_node, parent, link);
9378eb510dbSUladzislau Rezki (Sony) 	if (augment) {
93868ad4a33SUladzislau Rezki (Sony) 		/*
93968ad4a33SUladzislau Rezki (Sony) 		 * Some explanation here. Just perform simple insertion
94068ad4a33SUladzislau Rezki (Sony) 		 * to the tree. We do not set va->subtree_max_size to
94168ad4a33SUladzislau Rezki (Sony) 		 * its current size before calling rb_insert_augmented().
942153090f2SBaoquan He 		 * It is because we populate the tree from the bottom
94368ad4a33SUladzislau Rezki (Sony) 		 * to parent levels when the node _is_ in the tree.
94468ad4a33SUladzislau Rezki (Sony) 		 *
94568ad4a33SUladzislau Rezki (Sony) 		 * Therefore we set subtree_max_size to zero after insertion,
94668ad4a33SUladzislau Rezki (Sony) 		 * to let __augment_tree_propagate_from() puts everything to
94768ad4a33SUladzislau Rezki (Sony) 		 * the correct order later on.
94868ad4a33SUladzislau Rezki (Sony) 		 */
94968ad4a33SUladzislau Rezki (Sony) 		rb_insert_augmented(&va->rb_node,
95068ad4a33SUladzislau Rezki (Sony) 			root, &free_vmap_area_rb_augment_cb);
95168ad4a33SUladzislau Rezki (Sony) 		va->subtree_max_size = 0;
95268ad4a33SUladzislau Rezki (Sony) 	} else {
95368ad4a33SUladzislau Rezki (Sony) 		rb_insert_color(&va->rb_node, root);
95468ad4a33SUladzislau Rezki (Sony) 	}
95568ad4a33SUladzislau Rezki (Sony) 
95668ad4a33SUladzislau Rezki (Sony) 	/* Address-sort this list */
95768ad4a33SUladzislau Rezki (Sony) 	list_add(&va->list, head);
95868ad4a33SUladzislau Rezki (Sony) }
95968ad4a33SUladzislau Rezki (Sony) 
96068ad4a33SUladzislau Rezki (Sony) static __always_inline void
9618eb510dbSUladzislau Rezki (Sony) link_va(struct vmap_area *va, struct rb_root *root,
9628eb510dbSUladzislau Rezki (Sony) 	struct rb_node *parent, struct rb_node **link,
9638eb510dbSUladzislau Rezki (Sony) 	struct list_head *head)
9648eb510dbSUladzislau Rezki (Sony) {
9658eb510dbSUladzislau Rezki (Sony) 	__link_va(va, root, parent, link, head, false);
9668eb510dbSUladzislau Rezki (Sony) }
9678eb510dbSUladzislau Rezki (Sony) 
9688eb510dbSUladzislau Rezki (Sony) static __always_inline void
9698eb510dbSUladzislau Rezki (Sony) link_va_augment(struct vmap_area *va, struct rb_root *root,
9708eb510dbSUladzislau Rezki (Sony) 	struct rb_node *parent, struct rb_node **link,
9718eb510dbSUladzislau Rezki (Sony) 	struct list_head *head)
9728eb510dbSUladzislau Rezki (Sony) {
9738eb510dbSUladzislau Rezki (Sony) 	__link_va(va, root, parent, link, head, true);
9748eb510dbSUladzislau Rezki (Sony) }
9758eb510dbSUladzislau Rezki (Sony) 
9768eb510dbSUladzislau Rezki (Sony) static __always_inline void
9778eb510dbSUladzislau Rezki (Sony) __unlink_va(struct vmap_area *va, struct rb_root *root, bool augment)
97868ad4a33SUladzislau Rezki (Sony) {
979460e42d1SUladzislau Rezki (Sony) 	if (WARN_ON(RB_EMPTY_NODE(&va->rb_node)))
980460e42d1SUladzislau Rezki (Sony) 		return;
981460e42d1SUladzislau Rezki (Sony) 
9828eb510dbSUladzislau Rezki (Sony) 	if (augment)
98368ad4a33SUladzislau Rezki (Sony) 		rb_erase_augmented(&va->rb_node,
98468ad4a33SUladzislau Rezki (Sony) 			root, &free_vmap_area_rb_augment_cb);
98568ad4a33SUladzislau Rezki (Sony) 	else
98668ad4a33SUladzislau Rezki (Sony) 		rb_erase(&va->rb_node, root);
98768ad4a33SUladzislau Rezki (Sony) 
9885d7a7c54SUladzislau Rezki (Sony) 	list_del_init(&va->list);
98968ad4a33SUladzislau Rezki (Sony) 	RB_CLEAR_NODE(&va->rb_node);
99068ad4a33SUladzislau Rezki (Sony) }
99168ad4a33SUladzislau Rezki (Sony) 
9928eb510dbSUladzislau Rezki (Sony) static __always_inline void
9938eb510dbSUladzislau Rezki (Sony) unlink_va(struct vmap_area *va, struct rb_root *root)
9948eb510dbSUladzislau Rezki (Sony) {
9958eb510dbSUladzislau Rezki (Sony) 	__unlink_va(va, root, false);
9968eb510dbSUladzislau Rezki (Sony) }
9978eb510dbSUladzislau Rezki (Sony) 
9988eb510dbSUladzislau Rezki (Sony) static __always_inline void
9998eb510dbSUladzislau Rezki (Sony) unlink_va_augment(struct vmap_area *va, struct rb_root *root)
10008eb510dbSUladzislau Rezki (Sony) {
10018eb510dbSUladzislau Rezki (Sony) 	__unlink_va(va, root, true);
10028eb510dbSUladzislau Rezki (Sony) }
10038eb510dbSUladzislau Rezki (Sony) 
1004bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK
1005c3385e84SJiapeng Chong /*
1006c3385e84SJiapeng Chong  * Gets called when remove the node and rotate.
1007c3385e84SJiapeng Chong  */
1008c3385e84SJiapeng Chong static __always_inline unsigned long
1009c3385e84SJiapeng Chong compute_subtree_max_size(struct vmap_area *va)
1010c3385e84SJiapeng Chong {
1011c3385e84SJiapeng Chong 	return max3(va_size(va),
1012c3385e84SJiapeng Chong 		get_subtree_max_size(va->rb_node.rb_left),
1013c3385e84SJiapeng Chong 		get_subtree_max_size(va->rb_node.rb_right));
1014c3385e84SJiapeng Chong }
1015c3385e84SJiapeng Chong 
1016bb850f4dSUladzislau Rezki (Sony) static void
1017da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(void)
1018bb850f4dSUladzislau Rezki (Sony) {
1019bb850f4dSUladzislau Rezki (Sony) 	struct vmap_area *va;
1020da27c9edSUladzislau Rezki (Sony) 	unsigned long computed_size;
1021bb850f4dSUladzislau Rezki (Sony) 
1022da27c9edSUladzislau Rezki (Sony) 	list_for_each_entry(va, &free_vmap_area_list, list) {
1023da27c9edSUladzislau Rezki (Sony) 		computed_size = compute_subtree_max_size(va);
1024da27c9edSUladzislau Rezki (Sony) 		if (computed_size != va->subtree_max_size)
1025bb850f4dSUladzislau Rezki (Sony) 			pr_emerg("tree is corrupted: %lu, %lu\n",
1026bb850f4dSUladzislau Rezki (Sony) 				va_size(va), va->subtree_max_size);
1027bb850f4dSUladzislau Rezki (Sony) 	}
1028bb850f4dSUladzislau Rezki (Sony) }
1029bb850f4dSUladzislau Rezki (Sony) #endif
1030bb850f4dSUladzislau Rezki (Sony) 
103168ad4a33SUladzislau Rezki (Sony) /*
103268ad4a33SUladzislau Rezki (Sony)  * This function populates subtree_max_size from bottom to upper
103368ad4a33SUladzislau Rezki (Sony)  * levels starting from VA point. The propagation must be done
103468ad4a33SUladzislau Rezki (Sony)  * when VA size is modified by changing its va_start/va_end. Or
103568ad4a33SUladzislau Rezki (Sony)  * in case of newly inserting of VA to the tree.
103668ad4a33SUladzislau Rezki (Sony)  *
103768ad4a33SUladzislau Rezki (Sony)  * It means that __augment_tree_propagate_from() must be called:
103868ad4a33SUladzislau Rezki (Sony)  * - After VA has been inserted to the tree(free path);
103968ad4a33SUladzislau Rezki (Sony)  * - After VA has been shrunk(allocation path);
104068ad4a33SUladzislau Rezki (Sony)  * - After VA has been increased(merging path).
104168ad4a33SUladzislau Rezki (Sony)  *
104268ad4a33SUladzislau Rezki (Sony)  * Please note that, it does not mean that upper parent nodes
104368ad4a33SUladzislau Rezki (Sony)  * and their subtree_max_size are recalculated all the time up
104468ad4a33SUladzislau Rezki (Sony)  * to the root node.
104568ad4a33SUladzislau Rezki (Sony)  *
104668ad4a33SUladzislau Rezki (Sony)  *       4--8
104768ad4a33SUladzislau Rezki (Sony)  *        /\
104868ad4a33SUladzislau Rezki (Sony)  *       /  \
104968ad4a33SUladzislau Rezki (Sony)  *      /    \
105068ad4a33SUladzislau Rezki (Sony)  *    2--2  8--8
105168ad4a33SUladzislau Rezki (Sony)  *
105268ad4a33SUladzislau Rezki (Sony)  * For example if we modify the node 4, shrinking it to 2, then
105368ad4a33SUladzislau Rezki (Sony)  * no any modification is required. If we shrink the node 2 to 1
105468ad4a33SUladzislau Rezki (Sony)  * its subtree_max_size is updated only, and set to 1. If we shrink
105568ad4a33SUladzislau Rezki (Sony)  * the node 8 to 6, then its subtree_max_size is set to 6 and parent
105668ad4a33SUladzislau Rezki (Sony)  * node becomes 4--6.
105768ad4a33SUladzislau Rezki (Sony)  */
105868ad4a33SUladzislau Rezki (Sony) static __always_inline void
105968ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(struct vmap_area *va)
106068ad4a33SUladzislau Rezki (Sony) {
106168ad4a33SUladzislau Rezki (Sony) 	/*
106215ae144fSUladzislau Rezki (Sony) 	 * Populate the tree from bottom towards the root until
106315ae144fSUladzislau Rezki (Sony) 	 * the calculated maximum available size of checked node
106415ae144fSUladzislau Rezki (Sony) 	 * is equal to its current one.
106568ad4a33SUladzislau Rezki (Sony) 	 */
106615ae144fSUladzislau Rezki (Sony) 	free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL);
1067bb850f4dSUladzislau Rezki (Sony) 
1068bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK
1069da27c9edSUladzislau Rezki (Sony) 	augment_tree_propagate_check();
1070bb850f4dSUladzislau Rezki (Sony) #endif
107168ad4a33SUladzislau Rezki (Sony) }
107268ad4a33SUladzislau Rezki (Sony) 
107368ad4a33SUladzislau Rezki (Sony) static void
107468ad4a33SUladzislau Rezki (Sony) insert_vmap_area(struct vmap_area *va,
107568ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
107668ad4a33SUladzislau Rezki (Sony) {
107768ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
107868ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
107968ad4a33SUladzislau Rezki (Sony) 
108068ad4a33SUladzislau Rezki (Sony) 	link = find_va_links(va, root, NULL, &parent);
10819c801f61SUladzislau Rezki (Sony) 	if (link)
108268ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
108368ad4a33SUladzislau Rezki (Sony) }
108468ad4a33SUladzislau Rezki (Sony) 
108568ad4a33SUladzislau Rezki (Sony) static void
108668ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(struct vmap_area *va,
108768ad4a33SUladzislau Rezki (Sony) 	struct rb_node *from, struct rb_root *root,
108868ad4a33SUladzislau Rezki (Sony) 	struct list_head *head)
108968ad4a33SUladzislau Rezki (Sony) {
109068ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
109168ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
109268ad4a33SUladzislau Rezki (Sony) 
109368ad4a33SUladzislau Rezki (Sony) 	if (from)
109468ad4a33SUladzislau Rezki (Sony) 		link = find_va_links(va, NULL, from, &parent);
109568ad4a33SUladzislau Rezki (Sony) 	else
109668ad4a33SUladzislau Rezki (Sony) 		link = find_va_links(va, root, NULL, &parent);
109768ad4a33SUladzislau Rezki (Sony) 
10989c801f61SUladzislau Rezki (Sony) 	if (link) {
10998eb510dbSUladzislau Rezki (Sony) 		link_va_augment(va, root, parent, link, head);
110068ad4a33SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
110168ad4a33SUladzislau Rezki (Sony) 	}
11029c801f61SUladzislau Rezki (Sony) }
110368ad4a33SUladzislau Rezki (Sony) 
110468ad4a33SUladzislau Rezki (Sony) /*
110568ad4a33SUladzislau Rezki (Sony)  * Merge de-allocated chunk of VA memory with previous
110668ad4a33SUladzislau Rezki (Sony)  * and next free blocks. If coalesce is not done a new
110768ad4a33SUladzislau Rezki (Sony)  * free area is inserted. If VA has been merged, it is
110868ad4a33SUladzislau Rezki (Sony)  * freed.
11099c801f61SUladzislau Rezki (Sony)  *
11109c801f61SUladzislau Rezki (Sony)  * Please note, it can return NULL in case of overlap
11119c801f61SUladzislau Rezki (Sony)  * ranges, followed by WARN() report. Despite it is a
11129c801f61SUladzislau Rezki (Sony)  * buggy behaviour, a system can be alive and keep
11139c801f61SUladzislau Rezki (Sony)  * ongoing.
111468ad4a33SUladzislau Rezki (Sony)  */
11153c5c3cfbSDaniel Axtens static __always_inline struct vmap_area *
11168eb510dbSUladzislau Rezki (Sony) __merge_or_add_vmap_area(struct vmap_area *va,
11178eb510dbSUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head, bool augment)
111868ad4a33SUladzislau Rezki (Sony) {
111968ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *sibling;
112068ad4a33SUladzislau Rezki (Sony) 	struct list_head *next;
112168ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
112268ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
112368ad4a33SUladzislau Rezki (Sony) 	bool merged = false;
112468ad4a33SUladzislau Rezki (Sony) 
112568ad4a33SUladzislau Rezki (Sony) 	/*
112668ad4a33SUladzislau Rezki (Sony) 	 * Find a place in the tree where VA potentially will be
112768ad4a33SUladzislau Rezki (Sony) 	 * inserted, unless it is merged with its sibling/siblings.
112868ad4a33SUladzislau Rezki (Sony) 	 */
112968ad4a33SUladzislau Rezki (Sony) 	link = find_va_links(va, root, NULL, &parent);
11309c801f61SUladzislau Rezki (Sony) 	if (!link)
11319c801f61SUladzislau Rezki (Sony) 		return NULL;
113268ad4a33SUladzislau Rezki (Sony) 
113368ad4a33SUladzislau Rezki (Sony) 	/*
113468ad4a33SUladzislau Rezki (Sony) 	 * Get next node of VA to check if merging can be done.
113568ad4a33SUladzislau Rezki (Sony) 	 */
113668ad4a33SUladzislau Rezki (Sony) 	next = get_va_next_sibling(parent, link);
113768ad4a33SUladzislau Rezki (Sony) 	if (unlikely(next == NULL))
113868ad4a33SUladzislau Rezki (Sony) 		goto insert;
113968ad4a33SUladzislau Rezki (Sony) 
114068ad4a33SUladzislau Rezki (Sony) 	/*
114168ad4a33SUladzislau Rezki (Sony) 	 * start            end
114268ad4a33SUladzislau Rezki (Sony) 	 * |                |
114368ad4a33SUladzislau Rezki (Sony) 	 * |<------VA------>|<-----Next----->|
114468ad4a33SUladzislau Rezki (Sony) 	 *                  |                |
114568ad4a33SUladzislau Rezki (Sony) 	 *                  start            end
114668ad4a33SUladzislau Rezki (Sony) 	 */
114768ad4a33SUladzislau Rezki (Sony) 	if (next != head) {
114868ad4a33SUladzislau Rezki (Sony) 		sibling = list_entry(next, struct vmap_area, list);
114968ad4a33SUladzislau Rezki (Sony) 		if (sibling->va_start == va->va_end) {
115068ad4a33SUladzislau Rezki (Sony) 			sibling->va_start = va->va_start;
115168ad4a33SUladzislau Rezki (Sony) 
115268ad4a33SUladzislau Rezki (Sony) 			/* Free vmap_area object. */
115368ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, va);
115468ad4a33SUladzislau Rezki (Sony) 
115568ad4a33SUladzislau Rezki (Sony) 			/* Point to the new merged area. */
115668ad4a33SUladzislau Rezki (Sony) 			va = sibling;
115768ad4a33SUladzislau Rezki (Sony) 			merged = true;
115868ad4a33SUladzislau Rezki (Sony) 		}
115968ad4a33SUladzislau Rezki (Sony) 	}
116068ad4a33SUladzislau Rezki (Sony) 
116168ad4a33SUladzislau Rezki (Sony) 	/*
116268ad4a33SUladzislau Rezki (Sony) 	 * start            end
116368ad4a33SUladzislau Rezki (Sony) 	 * |                |
116468ad4a33SUladzislau Rezki (Sony) 	 * |<-----Prev----->|<------VA------>|
116568ad4a33SUladzislau Rezki (Sony) 	 *                  |                |
116668ad4a33SUladzislau Rezki (Sony) 	 *                  start            end
116768ad4a33SUladzislau Rezki (Sony) 	 */
116868ad4a33SUladzislau Rezki (Sony) 	if (next->prev != head) {
116968ad4a33SUladzislau Rezki (Sony) 		sibling = list_entry(next->prev, struct vmap_area, list);
117068ad4a33SUladzislau Rezki (Sony) 		if (sibling->va_end == va->va_start) {
11715dd78640SUladzislau Rezki (Sony) 			/*
11725dd78640SUladzislau Rezki (Sony) 			 * If both neighbors are coalesced, it is important
11735dd78640SUladzislau Rezki (Sony) 			 * to unlink the "next" node first, followed by merging
11745dd78640SUladzislau Rezki (Sony) 			 * with "previous" one. Otherwise the tree might not be
11755dd78640SUladzislau Rezki (Sony) 			 * fully populated if a sibling's augmented value is
11765dd78640SUladzislau Rezki (Sony) 			 * "normalized" because of rotation operations.
11775dd78640SUladzislau Rezki (Sony) 			 */
117854f63d9dSUladzislau Rezki (Sony) 			if (merged)
11798eb510dbSUladzislau Rezki (Sony) 				__unlink_va(va, root, augment);
118068ad4a33SUladzislau Rezki (Sony) 
11815dd78640SUladzislau Rezki (Sony) 			sibling->va_end = va->va_end;
11825dd78640SUladzislau Rezki (Sony) 
118368ad4a33SUladzislau Rezki (Sony) 			/* Free vmap_area object. */
118468ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, va);
11853c5c3cfbSDaniel Axtens 
11863c5c3cfbSDaniel Axtens 			/* Point to the new merged area. */
11873c5c3cfbSDaniel Axtens 			va = sibling;
11883c5c3cfbSDaniel Axtens 			merged = true;
118968ad4a33SUladzislau Rezki (Sony) 		}
119068ad4a33SUladzislau Rezki (Sony) 	}
119168ad4a33SUladzislau Rezki (Sony) 
119268ad4a33SUladzislau Rezki (Sony) insert:
11935dd78640SUladzislau Rezki (Sony) 	if (!merged)
11948eb510dbSUladzislau Rezki (Sony) 		__link_va(va, root, parent, link, head, augment);
11953c5c3cfbSDaniel Axtens 
119696e2db45SUladzislau Rezki (Sony) 	return va;
119796e2db45SUladzislau Rezki (Sony) }
119896e2db45SUladzislau Rezki (Sony) 
119996e2db45SUladzislau Rezki (Sony) static __always_inline struct vmap_area *
12008eb510dbSUladzislau Rezki (Sony) merge_or_add_vmap_area(struct vmap_area *va,
12018eb510dbSUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
12028eb510dbSUladzislau Rezki (Sony) {
12038eb510dbSUladzislau Rezki (Sony) 	return __merge_or_add_vmap_area(va, root, head, false);
12048eb510dbSUladzislau Rezki (Sony) }
12058eb510dbSUladzislau Rezki (Sony) 
12068eb510dbSUladzislau Rezki (Sony) static __always_inline struct vmap_area *
120796e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area_augment(struct vmap_area *va,
120896e2db45SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
120996e2db45SUladzislau Rezki (Sony) {
12108eb510dbSUladzislau Rezki (Sony) 	va = __merge_or_add_vmap_area(va, root, head, true);
121196e2db45SUladzislau Rezki (Sony) 	if (va)
12125dd78640SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
121396e2db45SUladzislau Rezki (Sony) 
12143c5c3cfbSDaniel Axtens 	return va;
121568ad4a33SUladzislau Rezki (Sony) }
121668ad4a33SUladzislau Rezki (Sony) 
121768ad4a33SUladzislau Rezki (Sony) static __always_inline bool
121868ad4a33SUladzislau Rezki (Sony) is_within_this_va(struct vmap_area *va, unsigned long size,
121968ad4a33SUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
122068ad4a33SUladzislau Rezki (Sony) {
122168ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr;
122268ad4a33SUladzislau Rezki (Sony) 
122368ad4a33SUladzislau Rezki (Sony) 	if (va->va_start > vstart)
122468ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(va->va_start, align);
122568ad4a33SUladzislau Rezki (Sony) 	else
122668ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(vstart, align);
122768ad4a33SUladzislau Rezki (Sony) 
122868ad4a33SUladzislau Rezki (Sony) 	/* Can be overflowed due to big size or alignment. */
122968ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr + size < nva_start_addr ||
123068ad4a33SUladzislau Rezki (Sony) 			nva_start_addr < vstart)
123168ad4a33SUladzislau Rezki (Sony) 		return false;
123268ad4a33SUladzislau Rezki (Sony) 
123368ad4a33SUladzislau Rezki (Sony) 	return (nva_start_addr + size <= va->va_end);
123468ad4a33SUladzislau Rezki (Sony) }
123568ad4a33SUladzislau Rezki (Sony) 
123668ad4a33SUladzislau Rezki (Sony) /*
123768ad4a33SUladzislau Rezki (Sony)  * Find the first free block(lowest start address) in the tree,
123868ad4a33SUladzislau Rezki (Sony)  * that will accomplish the request corresponding to passing
12399333fe98SUladzislau Rezki  * parameters. Please note, with an alignment bigger than PAGE_SIZE,
12409333fe98SUladzislau Rezki  * a search length is adjusted to account for worst case alignment
12419333fe98SUladzislau Rezki  * overhead.
124268ad4a33SUladzislau Rezki (Sony)  */
124368ad4a33SUladzislau Rezki (Sony) static __always_inline struct vmap_area *
1244f9863be4SUladzislau Rezki (Sony) find_vmap_lowest_match(struct rb_root *root, unsigned long size,
1245f9863be4SUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart, bool adjust_search_size)
124668ad4a33SUladzislau Rezki (Sony) {
124768ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
124868ad4a33SUladzislau Rezki (Sony) 	struct rb_node *node;
12499333fe98SUladzislau Rezki 	unsigned long length;
125068ad4a33SUladzislau Rezki (Sony) 
125168ad4a33SUladzislau Rezki (Sony) 	/* Start from the root. */
1252f9863be4SUladzislau Rezki (Sony) 	node = root->rb_node;
125368ad4a33SUladzislau Rezki (Sony) 
12549333fe98SUladzislau Rezki 	/* Adjust the search size for alignment overhead. */
12559333fe98SUladzislau Rezki 	length = adjust_search_size ? size + align - 1 : size;
12569333fe98SUladzislau Rezki 
125768ad4a33SUladzislau Rezki (Sony) 	while (node) {
125868ad4a33SUladzislau Rezki (Sony) 		va = rb_entry(node, struct vmap_area, rb_node);
125968ad4a33SUladzislau Rezki (Sony) 
12609333fe98SUladzislau Rezki 		if (get_subtree_max_size(node->rb_left) >= length &&
126168ad4a33SUladzislau Rezki (Sony) 				vstart < va->va_start) {
126268ad4a33SUladzislau Rezki (Sony) 			node = node->rb_left;
126368ad4a33SUladzislau Rezki (Sony) 		} else {
126468ad4a33SUladzislau Rezki (Sony) 			if (is_within_this_va(va, size, align, vstart))
126568ad4a33SUladzislau Rezki (Sony) 				return va;
126668ad4a33SUladzislau Rezki (Sony) 
126768ad4a33SUladzislau Rezki (Sony) 			/*
126868ad4a33SUladzislau Rezki (Sony) 			 * Does not make sense to go deeper towards the right
126968ad4a33SUladzislau Rezki (Sony) 			 * sub-tree if it does not have a free block that is
12709333fe98SUladzislau Rezki 			 * equal or bigger to the requested search length.
127168ad4a33SUladzislau Rezki (Sony) 			 */
12729333fe98SUladzislau Rezki 			if (get_subtree_max_size(node->rb_right) >= length) {
127368ad4a33SUladzislau Rezki (Sony) 				node = node->rb_right;
127468ad4a33SUladzislau Rezki (Sony) 				continue;
127568ad4a33SUladzislau Rezki (Sony) 			}
127668ad4a33SUladzislau Rezki (Sony) 
127768ad4a33SUladzislau Rezki (Sony) 			/*
12783806b041SAndrew Morton 			 * OK. We roll back and find the first right sub-tree,
127968ad4a33SUladzislau Rezki (Sony) 			 * that will satisfy the search criteria. It can happen
12809f531973SUladzislau Rezki (Sony) 			 * due to "vstart" restriction or an alignment overhead
12819f531973SUladzislau Rezki (Sony) 			 * that is bigger then PAGE_SIZE.
128268ad4a33SUladzislau Rezki (Sony) 			 */
128368ad4a33SUladzislau Rezki (Sony) 			while ((node = rb_parent(node))) {
128468ad4a33SUladzislau Rezki (Sony) 				va = rb_entry(node, struct vmap_area, rb_node);
128568ad4a33SUladzislau Rezki (Sony) 				if (is_within_this_va(va, size, align, vstart))
128668ad4a33SUladzislau Rezki (Sony) 					return va;
128768ad4a33SUladzislau Rezki (Sony) 
12889333fe98SUladzislau Rezki 				if (get_subtree_max_size(node->rb_right) >= length &&
128968ad4a33SUladzislau Rezki (Sony) 						vstart <= va->va_start) {
12909f531973SUladzislau Rezki (Sony) 					/*
12919f531973SUladzislau Rezki (Sony) 					 * Shift the vstart forward. Please note, we update it with
12929f531973SUladzislau Rezki (Sony) 					 * parent's start address adding "1" because we do not want
12939f531973SUladzislau Rezki (Sony) 					 * to enter same sub-tree after it has already been checked
12949f531973SUladzislau Rezki (Sony) 					 * and no suitable free block found there.
12959f531973SUladzislau Rezki (Sony) 					 */
12969f531973SUladzislau Rezki (Sony) 					vstart = va->va_start + 1;
129768ad4a33SUladzislau Rezki (Sony) 					node = node->rb_right;
129868ad4a33SUladzislau Rezki (Sony) 					break;
129968ad4a33SUladzislau Rezki (Sony) 				}
130068ad4a33SUladzislau Rezki (Sony) 			}
130168ad4a33SUladzislau Rezki (Sony) 		}
130268ad4a33SUladzislau Rezki (Sony) 	}
130368ad4a33SUladzislau Rezki (Sony) 
130468ad4a33SUladzislau Rezki (Sony) 	return NULL;
130568ad4a33SUladzislau Rezki (Sony) }
130668ad4a33SUladzislau Rezki (Sony) 
1307a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1308a6cf4e0fSUladzislau Rezki (Sony) #include <linux/random.h>
1309a6cf4e0fSUladzislau Rezki (Sony) 
1310a6cf4e0fSUladzislau Rezki (Sony) static struct vmap_area *
1311bd1264c3SSong Liu find_vmap_lowest_linear_match(struct list_head *head, unsigned long size,
1312a6cf4e0fSUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
1313a6cf4e0fSUladzislau Rezki (Sony) {
1314a6cf4e0fSUladzislau Rezki (Sony) 	struct vmap_area *va;
1315a6cf4e0fSUladzislau Rezki (Sony) 
1316bd1264c3SSong Liu 	list_for_each_entry(va, head, list) {
1317a6cf4e0fSUladzislau Rezki (Sony) 		if (!is_within_this_va(va, size, align, vstart))
1318a6cf4e0fSUladzislau Rezki (Sony) 			continue;
1319a6cf4e0fSUladzislau Rezki (Sony) 
1320a6cf4e0fSUladzislau Rezki (Sony) 		return va;
1321a6cf4e0fSUladzislau Rezki (Sony) 	}
1322a6cf4e0fSUladzislau Rezki (Sony) 
1323a6cf4e0fSUladzislau Rezki (Sony) 	return NULL;
1324a6cf4e0fSUladzislau Rezki (Sony) }
1325a6cf4e0fSUladzislau Rezki (Sony) 
1326a6cf4e0fSUladzislau Rezki (Sony) static void
1327bd1264c3SSong Liu find_vmap_lowest_match_check(struct rb_root *root, struct list_head *head,
1328bd1264c3SSong Liu 			     unsigned long size, unsigned long align)
1329a6cf4e0fSUladzislau Rezki (Sony) {
1330a6cf4e0fSUladzislau Rezki (Sony) 	struct vmap_area *va_1, *va_2;
1331a6cf4e0fSUladzislau Rezki (Sony) 	unsigned long vstart;
1332a6cf4e0fSUladzislau Rezki (Sony) 	unsigned int rnd;
1333a6cf4e0fSUladzislau Rezki (Sony) 
1334a6cf4e0fSUladzislau Rezki (Sony) 	get_random_bytes(&rnd, sizeof(rnd));
1335a6cf4e0fSUladzislau Rezki (Sony) 	vstart = VMALLOC_START + rnd;
1336a6cf4e0fSUladzislau Rezki (Sony) 
1337bd1264c3SSong Liu 	va_1 = find_vmap_lowest_match(root, size, align, vstart, false);
1338bd1264c3SSong Liu 	va_2 = find_vmap_lowest_linear_match(head, size, align, vstart);
1339a6cf4e0fSUladzislau Rezki (Sony) 
1340a6cf4e0fSUladzislau Rezki (Sony) 	if (va_1 != va_2)
1341a6cf4e0fSUladzislau Rezki (Sony) 		pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n",
1342a6cf4e0fSUladzislau Rezki (Sony) 			va_1, va_2, vstart);
1343a6cf4e0fSUladzislau Rezki (Sony) }
1344a6cf4e0fSUladzislau Rezki (Sony) #endif
1345a6cf4e0fSUladzislau Rezki (Sony) 
134668ad4a33SUladzislau Rezki (Sony) enum fit_type {
134768ad4a33SUladzislau Rezki (Sony) 	NOTHING_FIT = 0,
134868ad4a33SUladzislau Rezki (Sony) 	FL_FIT_TYPE = 1,	/* full fit */
134968ad4a33SUladzislau Rezki (Sony) 	LE_FIT_TYPE = 2,	/* left edge fit */
135068ad4a33SUladzislau Rezki (Sony) 	RE_FIT_TYPE = 3,	/* right edge fit */
135168ad4a33SUladzislau Rezki (Sony) 	NE_FIT_TYPE = 4		/* no edge fit */
135268ad4a33SUladzislau Rezki (Sony) };
135368ad4a33SUladzislau Rezki (Sony) 
135468ad4a33SUladzislau Rezki (Sony) static __always_inline enum fit_type
135568ad4a33SUladzislau Rezki (Sony) classify_va_fit_type(struct vmap_area *va,
135668ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr, unsigned long size)
135768ad4a33SUladzislau Rezki (Sony) {
135868ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
135968ad4a33SUladzislau Rezki (Sony) 
136068ad4a33SUladzislau Rezki (Sony) 	/* Check if it is within VA. */
136168ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr < va->va_start ||
136268ad4a33SUladzislau Rezki (Sony) 			nva_start_addr + size > va->va_end)
136368ad4a33SUladzislau Rezki (Sony) 		return NOTHING_FIT;
136468ad4a33SUladzislau Rezki (Sony) 
136568ad4a33SUladzislau Rezki (Sony) 	/* Now classify. */
136668ad4a33SUladzislau Rezki (Sony) 	if (va->va_start == nva_start_addr) {
136768ad4a33SUladzislau Rezki (Sony) 		if (va->va_end == nva_start_addr + size)
136868ad4a33SUladzislau Rezki (Sony) 			type = FL_FIT_TYPE;
136968ad4a33SUladzislau Rezki (Sony) 		else
137068ad4a33SUladzislau Rezki (Sony) 			type = LE_FIT_TYPE;
137168ad4a33SUladzislau Rezki (Sony) 	} else if (va->va_end == nva_start_addr + size) {
137268ad4a33SUladzislau Rezki (Sony) 		type = RE_FIT_TYPE;
137368ad4a33SUladzislau Rezki (Sony) 	} else {
137468ad4a33SUladzislau Rezki (Sony) 		type = NE_FIT_TYPE;
137568ad4a33SUladzislau Rezki (Sony) 	}
137668ad4a33SUladzislau Rezki (Sony) 
137768ad4a33SUladzislau Rezki (Sony) 	return type;
137868ad4a33SUladzislau Rezki (Sony) }
137968ad4a33SUladzislau Rezki (Sony) 
138068ad4a33SUladzislau Rezki (Sony) static __always_inline int
1381f9863be4SUladzislau Rezki (Sony) adjust_va_to_fit_type(struct rb_root *root, struct list_head *head,
1382f9863be4SUladzislau Rezki (Sony) 		      struct vmap_area *va, unsigned long nva_start_addr,
1383f9863be4SUladzislau Rezki (Sony) 		      unsigned long size)
138468ad4a33SUladzislau Rezki (Sony) {
13852c929233SArnd Bergmann 	struct vmap_area *lva = NULL;
13861b23ff80SBaoquan He 	enum fit_type type = classify_va_fit_type(va, nva_start_addr, size);
138768ad4a33SUladzislau Rezki (Sony) 
138868ad4a33SUladzislau Rezki (Sony) 	if (type == FL_FIT_TYPE) {
138968ad4a33SUladzislau Rezki (Sony) 		/*
139068ad4a33SUladzislau Rezki (Sony) 		 * No need to split VA, it fully fits.
139168ad4a33SUladzislau Rezki (Sony) 		 *
139268ad4a33SUladzislau Rezki (Sony) 		 * |               |
139368ad4a33SUladzislau Rezki (Sony) 		 * V      NVA      V
139468ad4a33SUladzislau Rezki (Sony) 		 * |---------------|
139568ad4a33SUladzislau Rezki (Sony) 		 */
1396f9863be4SUladzislau Rezki (Sony) 		unlink_va_augment(va, root);
139768ad4a33SUladzislau Rezki (Sony) 		kmem_cache_free(vmap_area_cachep, va);
139868ad4a33SUladzislau Rezki (Sony) 	} else if (type == LE_FIT_TYPE) {
139968ad4a33SUladzislau Rezki (Sony) 		/*
140068ad4a33SUladzislau Rezki (Sony) 		 * Split left edge of fit VA.
140168ad4a33SUladzislau Rezki (Sony) 		 *
140268ad4a33SUladzislau Rezki (Sony) 		 * |       |
140368ad4a33SUladzislau Rezki (Sony) 		 * V  NVA  V   R
140468ad4a33SUladzislau Rezki (Sony) 		 * |-------|-------|
140568ad4a33SUladzislau Rezki (Sony) 		 */
140668ad4a33SUladzislau Rezki (Sony) 		va->va_start += size;
140768ad4a33SUladzislau Rezki (Sony) 	} else if (type == RE_FIT_TYPE) {
140868ad4a33SUladzislau Rezki (Sony) 		/*
140968ad4a33SUladzislau Rezki (Sony) 		 * Split right edge of fit VA.
141068ad4a33SUladzislau Rezki (Sony) 		 *
141168ad4a33SUladzislau Rezki (Sony) 		 *         |       |
141268ad4a33SUladzislau Rezki (Sony) 		 *     L   V  NVA  V
141368ad4a33SUladzislau Rezki (Sony) 		 * |-------|-------|
141468ad4a33SUladzislau Rezki (Sony) 		 */
141568ad4a33SUladzislau Rezki (Sony) 		va->va_end = nva_start_addr;
141668ad4a33SUladzislau Rezki (Sony) 	} else if (type == NE_FIT_TYPE) {
141768ad4a33SUladzislau Rezki (Sony) 		/*
141868ad4a33SUladzislau Rezki (Sony) 		 * Split no edge of fit VA.
141968ad4a33SUladzislau Rezki (Sony) 		 *
142068ad4a33SUladzislau Rezki (Sony) 		 *     |       |
142168ad4a33SUladzislau Rezki (Sony) 		 *   L V  NVA  V R
142268ad4a33SUladzislau Rezki (Sony) 		 * |---|-------|---|
142368ad4a33SUladzislau Rezki (Sony) 		 */
142482dd23e8SUladzislau Rezki (Sony) 		lva = __this_cpu_xchg(ne_fit_preload_node, NULL);
142582dd23e8SUladzislau Rezki (Sony) 		if (unlikely(!lva)) {
142682dd23e8SUladzislau Rezki (Sony) 			/*
142782dd23e8SUladzislau Rezki (Sony) 			 * For percpu allocator we do not do any pre-allocation
142882dd23e8SUladzislau Rezki (Sony) 			 * and leave it as it is. The reason is it most likely
142982dd23e8SUladzislau Rezki (Sony) 			 * never ends up with NE_FIT_TYPE splitting. In case of
143082dd23e8SUladzislau Rezki (Sony) 			 * percpu allocations offsets and sizes are aligned to
143182dd23e8SUladzislau Rezki (Sony) 			 * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE
143282dd23e8SUladzislau Rezki (Sony) 			 * are its main fitting cases.
143382dd23e8SUladzislau Rezki (Sony) 			 *
143482dd23e8SUladzislau Rezki (Sony) 			 * There are a few exceptions though, as an example it is
143582dd23e8SUladzislau Rezki (Sony) 			 * a first allocation (early boot up) when we have "one"
143682dd23e8SUladzislau Rezki (Sony) 			 * big free space that has to be split.
1437060650a2SUladzislau Rezki (Sony) 			 *
1438060650a2SUladzislau Rezki (Sony) 			 * Also we can hit this path in case of regular "vmap"
1439060650a2SUladzislau Rezki (Sony) 			 * allocations, if "this" current CPU was not preloaded.
1440060650a2SUladzislau Rezki (Sony) 			 * See the comment in alloc_vmap_area() why. If so, then
1441060650a2SUladzislau Rezki (Sony) 			 * GFP_NOWAIT is used instead to get an extra object for
1442060650a2SUladzislau Rezki (Sony) 			 * split purpose. That is rare and most time does not
1443060650a2SUladzislau Rezki (Sony) 			 * occur.
1444060650a2SUladzislau Rezki (Sony) 			 *
1445060650a2SUladzislau Rezki (Sony) 			 * What happens if an allocation gets failed. Basically,
1446060650a2SUladzislau Rezki (Sony) 			 * an "overflow" path is triggered to purge lazily freed
1447060650a2SUladzislau Rezki (Sony) 			 * areas to free some memory, then, the "retry" path is
1448060650a2SUladzislau Rezki (Sony) 			 * triggered to repeat one more time. See more details
1449060650a2SUladzislau Rezki (Sony) 			 * in alloc_vmap_area() function.
145082dd23e8SUladzislau Rezki (Sony) 			 */
145168ad4a33SUladzislau Rezki (Sony) 			lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
145282dd23e8SUladzislau Rezki (Sony) 			if (!lva)
145368ad4a33SUladzislau Rezki (Sony) 				return -1;
145482dd23e8SUladzislau Rezki (Sony) 		}
145568ad4a33SUladzislau Rezki (Sony) 
145668ad4a33SUladzislau Rezki (Sony) 		/*
145768ad4a33SUladzislau Rezki (Sony) 		 * Build the remainder.
145868ad4a33SUladzislau Rezki (Sony) 		 */
145968ad4a33SUladzislau Rezki (Sony) 		lva->va_start = va->va_start;
146068ad4a33SUladzislau Rezki (Sony) 		lva->va_end = nva_start_addr;
146168ad4a33SUladzislau Rezki (Sony) 
146268ad4a33SUladzislau Rezki (Sony) 		/*
146368ad4a33SUladzislau Rezki (Sony) 		 * Shrink this VA to remaining size.
146468ad4a33SUladzislau Rezki (Sony) 		 */
146568ad4a33SUladzislau Rezki (Sony) 		va->va_start = nva_start_addr + size;
146668ad4a33SUladzislau Rezki (Sony) 	} else {
146768ad4a33SUladzislau Rezki (Sony) 		return -1;
146868ad4a33SUladzislau Rezki (Sony) 	}
146968ad4a33SUladzislau Rezki (Sony) 
147068ad4a33SUladzislau Rezki (Sony) 	if (type != FL_FIT_TYPE) {
147168ad4a33SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
147268ad4a33SUladzislau Rezki (Sony) 
14732c929233SArnd Bergmann 		if (lva)	/* type == NE_FIT_TYPE */
1474f9863be4SUladzislau Rezki (Sony) 			insert_vmap_area_augment(lva, &va->rb_node, root, head);
147568ad4a33SUladzislau Rezki (Sony) 	}
147668ad4a33SUladzislau Rezki (Sony) 
147768ad4a33SUladzislau Rezki (Sony) 	return 0;
147868ad4a33SUladzislau Rezki (Sony) }
147968ad4a33SUladzislau Rezki (Sony) 
148068ad4a33SUladzislau Rezki (Sony) /*
148168ad4a33SUladzislau Rezki (Sony)  * Returns a start address of the newly allocated area, if success.
148268ad4a33SUladzislau Rezki (Sony)  * Otherwise a vend is returned that indicates failure.
148368ad4a33SUladzislau Rezki (Sony)  */
148468ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
1485f9863be4SUladzislau Rezki (Sony) __alloc_vmap_area(struct rb_root *root, struct list_head *head,
1486f9863be4SUladzislau Rezki (Sony) 	unsigned long size, unsigned long align,
1487cacca6baSUladzislau Rezki (Sony) 	unsigned long vstart, unsigned long vend)
148868ad4a33SUladzislau Rezki (Sony) {
14899333fe98SUladzislau Rezki 	bool adjust_search_size = true;
149068ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr;
149168ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
149268ad4a33SUladzislau Rezki (Sony) 	int ret;
149368ad4a33SUladzislau Rezki (Sony) 
14949333fe98SUladzislau Rezki 	/*
14959333fe98SUladzislau Rezki 	 * Do not adjust when:
14969333fe98SUladzislau Rezki 	 *   a) align <= PAGE_SIZE, because it does not make any sense.
14979333fe98SUladzislau Rezki 	 *      All blocks(their start addresses) are at least PAGE_SIZE
14989333fe98SUladzislau Rezki 	 *      aligned anyway;
14999333fe98SUladzislau Rezki 	 *   b) a short range where a requested size corresponds to exactly
15009333fe98SUladzislau Rezki 	 *      specified [vstart:vend] interval and an alignment > PAGE_SIZE.
15019333fe98SUladzislau Rezki 	 *      With adjusted search length an allocation would not succeed.
15029333fe98SUladzislau Rezki 	 */
15039333fe98SUladzislau Rezki 	if (align <= PAGE_SIZE || (align > PAGE_SIZE && (vend - vstart) == size))
15049333fe98SUladzislau Rezki 		adjust_search_size = false;
15059333fe98SUladzislau Rezki 
1506f9863be4SUladzislau Rezki (Sony) 	va = find_vmap_lowest_match(root, size, align, vstart, adjust_search_size);
150768ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!va))
150868ad4a33SUladzislau Rezki (Sony) 		return vend;
150968ad4a33SUladzislau Rezki (Sony) 
151068ad4a33SUladzislau Rezki (Sony) 	if (va->va_start > vstart)
151168ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(va->va_start, align);
151268ad4a33SUladzislau Rezki (Sony) 	else
151368ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(vstart, align);
151468ad4a33SUladzislau Rezki (Sony) 
151568ad4a33SUladzislau Rezki (Sony) 	/* Check the "vend" restriction. */
151668ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr + size > vend)
151768ad4a33SUladzislau Rezki (Sony) 		return vend;
151868ad4a33SUladzislau Rezki (Sony) 
151968ad4a33SUladzislau Rezki (Sony) 	/* Update the free vmap_area. */
1520f9863be4SUladzislau Rezki (Sony) 	ret = adjust_va_to_fit_type(root, head, va, nva_start_addr, size);
15211b23ff80SBaoquan He 	if (WARN_ON_ONCE(ret))
152268ad4a33SUladzislau Rezki (Sony) 		return vend;
152368ad4a33SUladzislau Rezki (Sony) 
1524a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1525bd1264c3SSong Liu 	find_vmap_lowest_match_check(root, head, size, align);
1526a6cf4e0fSUladzislau Rezki (Sony) #endif
1527a6cf4e0fSUladzislau Rezki (Sony) 
152868ad4a33SUladzislau Rezki (Sony) 	return nva_start_addr;
152968ad4a33SUladzislau Rezki (Sony) }
15304da56b99SChris Wilson 
1531db64fe02SNick Piggin /*
1532d98c9e83SAndrey Ryabinin  * Free a region of KVA allocated by alloc_vmap_area
1533d98c9e83SAndrey Ryabinin  */
1534d98c9e83SAndrey Ryabinin static void free_vmap_area(struct vmap_area *va)
1535d98c9e83SAndrey Ryabinin {
1536d98c9e83SAndrey Ryabinin 	/*
1537d98c9e83SAndrey Ryabinin 	 * Remove from the busy tree/list.
1538d98c9e83SAndrey Ryabinin 	 */
1539d98c9e83SAndrey Ryabinin 	spin_lock(&vmap_area_lock);
1540d98c9e83SAndrey Ryabinin 	unlink_va(va, &vmap_area_root);
1541d98c9e83SAndrey Ryabinin 	spin_unlock(&vmap_area_lock);
1542d98c9e83SAndrey Ryabinin 
1543d98c9e83SAndrey Ryabinin 	/*
1544d98c9e83SAndrey Ryabinin 	 * Insert/Merge it back to the free tree/list.
1545d98c9e83SAndrey Ryabinin 	 */
1546d98c9e83SAndrey Ryabinin 	spin_lock(&free_vmap_area_lock);
154796e2db45SUladzislau Rezki (Sony) 	merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list);
1548d98c9e83SAndrey Ryabinin 	spin_unlock(&free_vmap_area_lock);
1549d98c9e83SAndrey Ryabinin }
1550d98c9e83SAndrey Ryabinin 
1551187f8cc4SUladzislau Rezki (Sony) static inline void
1552187f8cc4SUladzislau Rezki (Sony) preload_this_cpu_lock(spinlock_t *lock, gfp_t gfp_mask, int node)
1553187f8cc4SUladzislau Rezki (Sony) {
1554187f8cc4SUladzislau Rezki (Sony) 	struct vmap_area *va = NULL;
1555187f8cc4SUladzislau Rezki (Sony) 
1556187f8cc4SUladzislau Rezki (Sony) 	/*
1557187f8cc4SUladzislau Rezki (Sony) 	 * Preload this CPU with one extra vmap_area object. It is used
1558187f8cc4SUladzislau Rezki (Sony) 	 * when fit type of free area is NE_FIT_TYPE. It guarantees that
1559187f8cc4SUladzislau Rezki (Sony) 	 * a CPU that does an allocation is preloaded.
1560187f8cc4SUladzislau Rezki (Sony) 	 *
1561187f8cc4SUladzislau Rezki (Sony) 	 * We do it in non-atomic context, thus it allows us to use more
1562187f8cc4SUladzislau Rezki (Sony) 	 * permissive allocation masks to be more stable under low memory
1563187f8cc4SUladzislau Rezki (Sony) 	 * condition and high memory pressure.
1564187f8cc4SUladzislau Rezki (Sony) 	 */
1565187f8cc4SUladzislau Rezki (Sony) 	if (!this_cpu_read(ne_fit_preload_node))
1566187f8cc4SUladzislau Rezki (Sony) 		va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
1567187f8cc4SUladzislau Rezki (Sony) 
1568187f8cc4SUladzislau Rezki (Sony) 	spin_lock(lock);
1569187f8cc4SUladzislau Rezki (Sony) 
1570187f8cc4SUladzislau Rezki (Sony) 	if (va && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, va))
1571187f8cc4SUladzislau Rezki (Sony) 		kmem_cache_free(vmap_area_cachep, va);
1572187f8cc4SUladzislau Rezki (Sony) }
1573187f8cc4SUladzislau Rezki (Sony) 
1574d98c9e83SAndrey Ryabinin /*
1575db64fe02SNick Piggin  * Allocate a region of KVA of the specified size and alignment, within the
1576db64fe02SNick Piggin  * vstart and vend.
1577db64fe02SNick Piggin  */
1578db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size,
1579db64fe02SNick Piggin 				unsigned long align,
1580db64fe02SNick Piggin 				unsigned long vstart, unsigned long vend,
1581869176a0SBaoquan He 				int node, gfp_t gfp_mask,
1582869176a0SBaoquan He 				unsigned long va_flags)
1583db64fe02SNick Piggin {
1584187f8cc4SUladzislau Rezki (Sony) 	struct vmap_area *va;
158512e376a6SUladzislau Rezki (Sony) 	unsigned long freed;
15861da177e4SLinus Torvalds 	unsigned long addr;
1587db64fe02SNick Piggin 	int purged = 0;
1588d98c9e83SAndrey Ryabinin 	int ret;
1589db64fe02SNick Piggin 
15907e4a32c0SHyunmin Lee 	if (unlikely(!size || offset_in_page(size) || !is_power_of_2(align)))
15917e4a32c0SHyunmin Lee 		return ERR_PTR(-EINVAL);
1592db64fe02SNick Piggin 
159368ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!vmap_initialized))
159468ad4a33SUladzislau Rezki (Sony) 		return ERR_PTR(-EBUSY);
159568ad4a33SUladzislau Rezki (Sony) 
15965803ed29SChristoph Hellwig 	might_sleep();
1597f07116d7SUladzislau Rezki (Sony) 	gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
15984da56b99SChris Wilson 
1599f07116d7SUladzislau Rezki (Sony) 	va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
1600db64fe02SNick Piggin 	if (unlikely(!va))
1601db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
1602db64fe02SNick Piggin 
16037f88f88fSCatalin Marinas 	/*
16047f88f88fSCatalin Marinas 	 * Only scan the relevant parts containing pointers to other objects
16057f88f88fSCatalin Marinas 	 * to avoid false negatives.
16067f88f88fSCatalin Marinas 	 */
1607f07116d7SUladzislau Rezki (Sony) 	kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask);
16087f88f88fSCatalin Marinas 
1609db64fe02SNick Piggin retry:
1610187f8cc4SUladzislau Rezki (Sony) 	preload_this_cpu_lock(&free_vmap_area_lock, gfp_mask, node);
1611f9863be4SUladzislau Rezki (Sony) 	addr = __alloc_vmap_area(&free_vmap_area_root, &free_vmap_area_list,
1612f9863be4SUladzislau Rezki (Sony) 		size, align, vstart, vend);
1613187f8cc4SUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
161468ad4a33SUladzislau Rezki (Sony) 
1615cf243da6SUladzislau Rezki (Sony) 	trace_alloc_vmap_area(addr, size, align, vstart, vend, addr == vend);
1616cf243da6SUladzislau Rezki (Sony) 
161789699605SNick Piggin 	/*
161868ad4a33SUladzislau Rezki (Sony) 	 * If an allocation fails, the "vend" address is
161968ad4a33SUladzislau Rezki (Sony) 	 * returned. Therefore trigger the overflow path.
162089699605SNick Piggin 	 */
162168ad4a33SUladzislau Rezki (Sony) 	if (unlikely(addr == vend))
162289699605SNick Piggin 		goto overflow;
162389699605SNick Piggin 
162489699605SNick Piggin 	va->va_start = addr;
162589699605SNick Piggin 	va->va_end = addr + size;
1626688fcbfcSPengfei Li 	va->vm = NULL;
1627869176a0SBaoquan He 	va->flags = va_flags;
162868ad4a33SUladzislau Rezki (Sony) 
1629e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
1630e36176beSUladzislau Rezki (Sony) 	insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
163189699605SNick Piggin 	spin_unlock(&vmap_area_lock);
163289699605SNick Piggin 
163361e16557SWang Xiaoqiang 	BUG_ON(!IS_ALIGNED(va->va_start, align));
163489699605SNick Piggin 	BUG_ON(va->va_start < vstart);
163589699605SNick Piggin 	BUG_ON(va->va_end > vend);
163689699605SNick Piggin 
1637d98c9e83SAndrey Ryabinin 	ret = kasan_populate_vmalloc(addr, size);
1638d98c9e83SAndrey Ryabinin 	if (ret) {
1639d98c9e83SAndrey Ryabinin 		free_vmap_area(va);
1640d98c9e83SAndrey Ryabinin 		return ERR_PTR(ret);
1641d98c9e83SAndrey Ryabinin 	}
1642d98c9e83SAndrey Ryabinin 
164389699605SNick Piggin 	return va;
164489699605SNick Piggin 
16457766970cSNick Piggin overflow:
1646db64fe02SNick Piggin 	if (!purged) {
1647db64fe02SNick Piggin 		purge_vmap_area_lazy();
1648db64fe02SNick Piggin 		purged = 1;
1649db64fe02SNick Piggin 		goto retry;
1650db64fe02SNick Piggin 	}
16514da56b99SChris Wilson 
165212e376a6SUladzislau Rezki (Sony) 	freed = 0;
16534da56b99SChris Wilson 	blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
165412e376a6SUladzislau Rezki (Sony) 
16554da56b99SChris Wilson 	if (freed > 0) {
16564da56b99SChris Wilson 		purged = 0;
16574da56b99SChris Wilson 		goto retry;
16584da56b99SChris Wilson 	}
16594da56b99SChris Wilson 
166003497d76SFlorian Fainelli 	if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
1661756a025fSJoe Perches 		pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1662756a025fSJoe Perches 			size);
166368ad4a33SUladzislau Rezki (Sony) 
166468ad4a33SUladzislau Rezki (Sony) 	kmem_cache_free(vmap_area_cachep, va);
1665db64fe02SNick Piggin 	return ERR_PTR(-EBUSY);
1666db64fe02SNick Piggin }
1667db64fe02SNick Piggin 
16684da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb)
16694da56b99SChris Wilson {
16704da56b99SChris Wilson 	return blocking_notifier_chain_register(&vmap_notify_list, nb);
16714da56b99SChris Wilson }
16724da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
16734da56b99SChris Wilson 
16744da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb)
16754da56b99SChris Wilson {
16764da56b99SChris Wilson 	return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
16774da56b99SChris Wilson }
16784da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
16794da56b99SChris Wilson 
1680db64fe02SNick Piggin /*
1681db64fe02SNick Piggin  * lazy_max_pages is the maximum amount of virtual address space we gather up
1682db64fe02SNick Piggin  * before attempting to purge with a TLB flush.
1683db64fe02SNick Piggin  *
1684db64fe02SNick Piggin  * There is a tradeoff here: a larger number will cover more kernel page tables
1685db64fe02SNick Piggin  * and take slightly longer to purge, but it will linearly reduce the number of
1686db64fe02SNick Piggin  * global TLB flushes that must be performed. It would seem natural to scale
1687db64fe02SNick Piggin  * this number up linearly with the number of CPUs (because vmapping activity
1688db64fe02SNick Piggin  * could also scale linearly with the number of CPUs), however it is likely
1689db64fe02SNick Piggin  * that in practice, workloads might be constrained in other ways that mean
1690db64fe02SNick Piggin  * vmap activity will not scale linearly with CPUs. Also, I want to be
1691db64fe02SNick Piggin  * conservative and not introduce a big latency on huge systems, so go with
1692db64fe02SNick Piggin  * a less aggressive log scale. It will still be an improvement over the old
1693db64fe02SNick Piggin  * code, and it will be simple to change the scale factor if we find that it
1694db64fe02SNick Piggin  * becomes a problem on bigger systems.
1695db64fe02SNick Piggin  */
1696db64fe02SNick Piggin static unsigned long lazy_max_pages(void)
1697db64fe02SNick Piggin {
1698db64fe02SNick Piggin 	unsigned int log;
1699db64fe02SNick Piggin 
1700db64fe02SNick Piggin 	log = fls(num_online_cpus());
1701db64fe02SNick Piggin 
1702db64fe02SNick Piggin 	return log * (32UL * 1024 * 1024 / PAGE_SIZE);
1703db64fe02SNick Piggin }
1704db64fe02SNick Piggin 
17054d36e6f8SUladzislau Rezki (Sony) static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0);
1706db64fe02SNick Piggin 
17070574ecd1SChristoph Hellwig /*
1708f0953a1bSIngo Molnar  * Serialize vmap purging.  There is no actual critical section protected
1709153090f2SBaoquan He  * by this lock, but we want to avoid concurrent calls for performance
17100574ecd1SChristoph Hellwig  * reasons and to make the pcpu_get_vm_areas more deterministic.
17110574ecd1SChristoph Hellwig  */
1712f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock);
17130574ecd1SChristoph Hellwig 
171402b709dfSNick Piggin /* for per-CPU blocks */
171502b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void);
171602b709dfSNick Piggin 
17173ee48b6aSCliff Wickman /*
1718db64fe02SNick Piggin  * Purges all lazily-freed vmap areas.
1719db64fe02SNick Piggin  */
17200574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
1721db64fe02SNick Piggin {
17224d36e6f8SUladzislau Rezki (Sony) 	unsigned long resched_threshold;
17236030fd5fSUladzislau Rezki (Sony) 	unsigned int num_purged_areas = 0;
1724baa468a6SBaoquan He 	struct list_head local_purge_list;
172596e2db45SUladzislau Rezki (Sony) 	struct vmap_area *va, *n_va;
1726db64fe02SNick Piggin 
17270574ecd1SChristoph Hellwig 	lockdep_assert_held(&vmap_purge_lock);
172802b709dfSNick Piggin 
172996e2db45SUladzislau Rezki (Sony) 	spin_lock(&purge_vmap_area_lock);
173096e2db45SUladzislau Rezki (Sony) 	purge_vmap_area_root = RB_ROOT;
1731baa468a6SBaoquan He 	list_replace_init(&purge_vmap_area_list, &local_purge_list);
173296e2db45SUladzislau Rezki (Sony) 	spin_unlock(&purge_vmap_area_lock);
173396e2db45SUladzislau Rezki (Sony) 
1734baa468a6SBaoquan He 	if (unlikely(list_empty(&local_purge_list)))
17356030fd5fSUladzislau Rezki (Sony) 		goto out;
173668571be9SUladzislau Rezki (Sony) 
173796e2db45SUladzislau Rezki (Sony) 	start = min(start,
1738baa468a6SBaoquan He 		list_first_entry(&local_purge_list,
173996e2db45SUladzislau Rezki (Sony) 			struct vmap_area, list)->va_start);
174096e2db45SUladzislau Rezki (Sony) 
174196e2db45SUladzislau Rezki (Sony) 	end = max(end,
1742baa468a6SBaoquan He 		list_last_entry(&local_purge_list,
174396e2db45SUladzislau Rezki (Sony) 			struct vmap_area, list)->va_end);
1744db64fe02SNick Piggin 
17450574ecd1SChristoph Hellwig 	flush_tlb_kernel_range(start, end);
17464d36e6f8SUladzislau Rezki (Sony) 	resched_threshold = lazy_max_pages() << 1;
1747db64fe02SNick Piggin 
1748e36176beSUladzislau Rezki (Sony) 	spin_lock(&free_vmap_area_lock);
1749baa468a6SBaoquan He 	list_for_each_entry_safe(va, n_va, &local_purge_list, list) {
17504d36e6f8SUladzislau Rezki (Sony) 		unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
17513c5c3cfbSDaniel Axtens 		unsigned long orig_start = va->va_start;
17523c5c3cfbSDaniel Axtens 		unsigned long orig_end = va->va_end;
1753763b218dSJoel Fernandes 
1754dd3b8353SUladzislau Rezki (Sony) 		/*
1755dd3b8353SUladzislau Rezki (Sony) 		 * Finally insert or merge lazily-freed area. It is
1756dd3b8353SUladzislau Rezki (Sony) 		 * detached and there is no need to "unlink" it from
1757dd3b8353SUladzislau Rezki (Sony) 		 * anything.
1758dd3b8353SUladzislau Rezki (Sony) 		 */
175996e2db45SUladzislau Rezki (Sony) 		va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root,
17603c5c3cfbSDaniel Axtens 				&free_vmap_area_list);
17613c5c3cfbSDaniel Axtens 
17629c801f61SUladzislau Rezki (Sony) 		if (!va)
17639c801f61SUladzislau Rezki (Sony) 			continue;
17649c801f61SUladzislau Rezki (Sony) 
17653c5c3cfbSDaniel Axtens 		if (is_vmalloc_or_module_addr((void *)orig_start))
17663c5c3cfbSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
17673c5c3cfbSDaniel Axtens 					      va->va_start, va->va_end);
1768dd3b8353SUladzislau Rezki (Sony) 
17694d36e6f8SUladzislau Rezki (Sony) 		atomic_long_sub(nr, &vmap_lazy_nr);
17706030fd5fSUladzislau Rezki (Sony) 		num_purged_areas++;
177168571be9SUladzislau Rezki (Sony) 
17724d36e6f8SUladzislau Rezki (Sony) 		if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
1773e36176beSUladzislau Rezki (Sony) 			cond_resched_lock(&free_vmap_area_lock);
1774763b218dSJoel Fernandes 	}
1775e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
17766030fd5fSUladzislau Rezki (Sony) 
17776030fd5fSUladzislau Rezki (Sony) out:
17786030fd5fSUladzislau Rezki (Sony) 	trace_purge_vmap_area_lazy(start, end, num_purged_areas);
17796030fd5fSUladzislau Rezki (Sony) 	return num_purged_areas > 0;
1780db64fe02SNick Piggin }
1781db64fe02SNick Piggin 
1782db64fe02SNick Piggin /*
1783db64fe02SNick Piggin  * Kick off a purge of the outstanding lazy areas.
1784db64fe02SNick Piggin  */
1785db64fe02SNick Piggin static void purge_vmap_area_lazy(void)
1786db64fe02SNick Piggin {
1787f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
17880574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
17890574ecd1SChristoph Hellwig 	__purge_vmap_area_lazy(ULONG_MAX, 0);
1790f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
1791db64fe02SNick Piggin }
1792db64fe02SNick Piggin 
1793690467c8SUladzislau Rezki (Sony) static void drain_vmap_area_work(struct work_struct *work)
1794690467c8SUladzislau Rezki (Sony) {
1795690467c8SUladzislau Rezki (Sony) 	unsigned long nr_lazy;
1796690467c8SUladzislau Rezki (Sony) 
1797690467c8SUladzislau Rezki (Sony) 	do {
1798690467c8SUladzislau Rezki (Sony) 		mutex_lock(&vmap_purge_lock);
1799690467c8SUladzislau Rezki (Sony) 		__purge_vmap_area_lazy(ULONG_MAX, 0);
1800690467c8SUladzislau Rezki (Sony) 		mutex_unlock(&vmap_purge_lock);
1801690467c8SUladzislau Rezki (Sony) 
1802690467c8SUladzislau Rezki (Sony) 		/* Recheck if further work is required. */
1803690467c8SUladzislau Rezki (Sony) 		nr_lazy = atomic_long_read(&vmap_lazy_nr);
1804690467c8SUladzislau Rezki (Sony) 	} while (nr_lazy > lazy_max_pages());
1805690467c8SUladzislau Rezki (Sony) }
1806690467c8SUladzislau Rezki (Sony) 
1807db64fe02SNick Piggin /*
1808edd89818SUladzislau Rezki (Sony)  * Free a vmap area, caller ensuring that the area has been unmapped,
1809edd89818SUladzislau Rezki (Sony)  * unlinked and flush_cache_vunmap had been called for the correct
1810edd89818SUladzislau Rezki (Sony)  * range previously.
1811db64fe02SNick Piggin  */
181264141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va)
1813db64fe02SNick Piggin {
18148c4196feSUladzislau Rezki (Sony) 	unsigned long nr_lazy_max = lazy_max_pages();
18158c4196feSUladzislau Rezki (Sony) 	unsigned long va_start = va->va_start;
18164d36e6f8SUladzislau Rezki (Sony) 	unsigned long nr_lazy;
181780c4bd7aSChris Wilson 
1818edd89818SUladzislau Rezki (Sony) 	if (WARN_ON_ONCE(!list_empty(&va->list)))
1819edd89818SUladzislau Rezki (Sony) 		return;
1820dd3b8353SUladzislau Rezki (Sony) 
18214d36e6f8SUladzislau Rezki (Sony) 	nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
18224d36e6f8SUladzislau Rezki (Sony) 				PAGE_SHIFT, &vmap_lazy_nr);
182380c4bd7aSChris Wilson 
182496e2db45SUladzislau Rezki (Sony) 	/*
182596e2db45SUladzislau Rezki (Sony) 	 * Merge or place it to the purge tree/list.
182696e2db45SUladzislau Rezki (Sony) 	 */
182796e2db45SUladzislau Rezki (Sony) 	spin_lock(&purge_vmap_area_lock);
182896e2db45SUladzislau Rezki (Sony) 	merge_or_add_vmap_area(va,
182996e2db45SUladzislau Rezki (Sony) 		&purge_vmap_area_root, &purge_vmap_area_list);
183096e2db45SUladzislau Rezki (Sony) 	spin_unlock(&purge_vmap_area_lock);
183180c4bd7aSChris Wilson 
18328c4196feSUladzislau Rezki (Sony) 	trace_free_vmap_area_noflush(va_start, nr_lazy, nr_lazy_max);
18338c4196feSUladzislau Rezki (Sony) 
183496e2db45SUladzislau Rezki (Sony) 	/* After this point, we may free va at any time */
18358c4196feSUladzislau Rezki (Sony) 	if (unlikely(nr_lazy > nr_lazy_max))
1836690467c8SUladzislau Rezki (Sony) 		schedule_work(&drain_vmap_work);
1837db64fe02SNick Piggin }
1838db64fe02SNick Piggin 
1839b29acbdcSNick Piggin /*
1840b29acbdcSNick Piggin  * Free and unmap a vmap area
1841b29acbdcSNick Piggin  */
1842b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va)
1843b29acbdcSNick Piggin {
1844b29acbdcSNick Piggin 	flush_cache_vunmap(va->va_start, va->va_end);
18454ad0ae8cSNicholas Piggin 	vunmap_range_noflush(va->va_start, va->va_end);
18468e57f8acSVlastimil Babka 	if (debug_pagealloc_enabled_static())
184782a2e924SChintan Pandya 		flush_tlb_kernel_range(va->va_start, va->va_end);
184882a2e924SChintan Pandya 
1849c8eef01eSChristoph Hellwig 	free_vmap_area_noflush(va);
1850b29acbdcSNick Piggin }
1851b29acbdcSNick Piggin 
1852993d0b28SMatthew Wilcox (Oracle) struct vmap_area *find_vmap_area(unsigned long addr)
1853db64fe02SNick Piggin {
1854db64fe02SNick Piggin 	struct vmap_area *va;
1855db64fe02SNick Piggin 
1856db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
1857899c6efeSUladzislau Rezki (Sony) 	va = __find_vmap_area(addr, &vmap_area_root);
1858db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
1859db64fe02SNick Piggin 
1860db64fe02SNick Piggin 	return va;
1861db64fe02SNick Piggin }
1862db64fe02SNick Piggin 
1863edd89818SUladzislau Rezki (Sony) static struct vmap_area *find_unlink_vmap_area(unsigned long addr)
1864edd89818SUladzislau Rezki (Sony) {
1865edd89818SUladzislau Rezki (Sony) 	struct vmap_area *va;
1866edd89818SUladzislau Rezki (Sony) 
1867edd89818SUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
1868edd89818SUladzislau Rezki (Sony) 	va = __find_vmap_area(addr, &vmap_area_root);
1869edd89818SUladzislau Rezki (Sony) 	if (va)
1870edd89818SUladzislau Rezki (Sony) 		unlink_va(va, &vmap_area_root);
1871edd89818SUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
1872edd89818SUladzislau Rezki (Sony) 
1873edd89818SUladzislau Rezki (Sony) 	return va;
1874edd89818SUladzislau Rezki (Sony) }
1875edd89818SUladzislau Rezki (Sony) 
1876db64fe02SNick Piggin /*** Per cpu kva allocator ***/
1877db64fe02SNick Piggin 
1878db64fe02SNick Piggin /*
1879db64fe02SNick Piggin  * vmap space is limited especially on 32 bit architectures. Ensure there is
1880db64fe02SNick Piggin  * room for at least 16 percpu vmap blocks per CPU.
1881db64fe02SNick Piggin  */
1882db64fe02SNick Piggin /*
1883db64fe02SNick Piggin  * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
1884db64fe02SNick Piggin  * to #define VMALLOC_SPACE		(VMALLOC_END-VMALLOC_START). Guess
1885db64fe02SNick Piggin  * instead (we just need a rough idea)
1886db64fe02SNick Piggin  */
1887db64fe02SNick Piggin #if BITS_PER_LONG == 32
1888db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024)
1889db64fe02SNick Piggin #else
1890db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024*1024)
1891db64fe02SNick Piggin #endif
1892db64fe02SNick Piggin 
1893db64fe02SNick Piggin #define VMALLOC_PAGES		(VMALLOC_SPACE / PAGE_SIZE)
1894db64fe02SNick Piggin #define VMAP_MAX_ALLOC		BITS_PER_LONG	/* 256K with 4K pages */
1895db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX	1024	/* 4MB with 4K pages */
1896db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN	(VMAP_MAX_ALLOC*2)
1897db64fe02SNick Piggin #define VMAP_MIN(x, y)		((x) < (y) ? (x) : (y)) /* can't use min() */
1898db64fe02SNick Piggin #define VMAP_MAX(x, y)		((x) > (y) ? (x) : (y)) /* can't use max() */
1899f982f915SClemens Ladisch #define VMAP_BBMAP_BITS		\
1900f982f915SClemens Ladisch 		VMAP_MIN(VMAP_BBMAP_BITS_MAX,	\
1901db64fe02SNick Piggin 		VMAP_MAX(VMAP_BBMAP_BITS_MIN,	\
1902f982f915SClemens Ladisch 			VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
1903db64fe02SNick Piggin 
1904db64fe02SNick Piggin #define VMAP_BLOCK_SIZE		(VMAP_BBMAP_BITS * PAGE_SIZE)
1905db64fe02SNick Piggin 
1906869176a0SBaoquan He #define VMAP_RAM		0x1 /* indicates vm_map_ram area*/
1907869176a0SBaoquan He #define VMAP_BLOCK		0x2 /* mark out the vmap_block sub-type*/
1908869176a0SBaoquan He #define VMAP_FLAGS_MASK		0x3
1909869176a0SBaoquan He 
1910db64fe02SNick Piggin struct vmap_block_queue {
1911db64fe02SNick Piggin 	spinlock_t lock;
1912db64fe02SNick Piggin 	struct list_head free;
1913db64fe02SNick Piggin };
1914db64fe02SNick Piggin 
1915db64fe02SNick Piggin struct vmap_block {
1916db64fe02SNick Piggin 	spinlock_t lock;
1917db64fe02SNick Piggin 	struct vmap_area *va;
1918db64fe02SNick Piggin 	unsigned long free, dirty;
1919d76f9954SBaoquan He 	DECLARE_BITMAP(used_map, VMAP_BBMAP_BITS);
19207d61bfe8SRoman Pen 	unsigned long dirty_min, dirty_max; /*< dirty range */
1921db64fe02SNick Piggin 	struct list_head free_list;
1922db64fe02SNick Piggin 	struct rcu_head rcu_head;
192302b709dfSNick Piggin 	struct list_head purge;
1924db64fe02SNick Piggin };
1925db64fe02SNick Piggin 
1926db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1927db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1928db64fe02SNick Piggin 
1929db64fe02SNick Piggin /*
19300f14599cSMatthew Wilcox (Oracle)  * XArray of vmap blocks, indexed by address, to quickly find a vmap block
1931db64fe02SNick Piggin  * in the free path. Could get rid of this if we change the API to return a
1932db64fe02SNick Piggin  * "cookie" from alloc, to be passed to free. But no big deal yet.
1933db64fe02SNick Piggin  */
19340f14599cSMatthew Wilcox (Oracle) static DEFINE_XARRAY(vmap_blocks);
1935db64fe02SNick Piggin 
1936db64fe02SNick Piggin /*
1937db64fe02SNick Piggin  * We should probably have a fallback mechanism to allocate virtual memory
1938db64fe02SNick Piggin  * out of partially filled vmap blocks. However vmap block sizing should be
1939db64fe02SNick Piggin  * fairly reasonable according to the vmalloc size, so it shouldn't be a
1940db64fe02SNick Piggin  * big problem.
1941db64fe02SNick Piggin  */
1942db64fe02SNick Piggin 
1943db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr)
1944db64fe02SNick Piggin {
1945db64fe02SNick Piggin 	addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
1946db64fe02SNick Piggin 	addr /= VMAP_BLOCK_SIZE;
1947db64fe02SNick Piggin 	return addr;
1948db64fe02SNick Piggin }
1949db64fe02SNick Piggin 
1950cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
1951cf725ce2SRoman Pen {
1952cf725ce2SRoman Pen 	unsigned long addr;
1953cf725ce2SRoman Pen 
1954cf725ce2SRoman Pen 	addr = va_start + (pages_off << PAGE_SHIFT);
1955cf725ce2SRoman Pen 	BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
1956cf725ce2SRoman Pen 	return (void *)addr;
1957cf725ce2SRoman Pen }
1958cf725ce2SRoman Pen 
1959cf725ce2SRoman Pen /**
1960cf725ce2SRoman Pen  * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
1961cf725ce2SRoman Pen  *                  block. Of course pages number can't exceed VMAP_BBMAP_BITS
1962cf725ce2SRoman Pen  * @order:    how many 2^order pages should be occupied in newly allocated block
1963cf725ce2SRoman Pen  * @gfp_mask: flags for the page level allocator
1964cf725ce2SRoman Pen  *
1965a862f68aSMike Rapoport  * Return: virtual address in a newly allocated block or ERR_PTR(-errno)
1966cf725ce2SRoman Pen  */
1967cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
1968db64fe02SNick Piggin {
1969db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
1970db64fe02SNick Piggin 	struct vmap_block *vb;
1971db64fe02SNick Piggin 	struct vmap_area *va;
1972db64fe02SNick Piggin 	unsigned long vb_idx;
1973db64fe02SNick Piggin 	int node, err;
1974cf725ce2SRoman Pen 	void *vaddr;
1975db64fe02SNick Piggin 
1976db64fe02SNick Piggin 	node = numa_node_id();
1977db64fe02SNick Piggin 
1978db64fe02SNick Piggin 	vb = kmalloc_node(sizeof(struct vmap_block),
1979db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
1980db64fe02SNick Piggin 	if (unlikely(!vb))
1981db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
1982db64fe02SNick Piggin 
1983db64fe02SNick Piggin 	va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
1984db64fe02SNick Piggin 					VMALLOC_START, VMALLOC_END,
1985869176a0SBaoquan He 					node, gfp_mask,
1986869176a0SBaoquan He 					VMAP_RAM|VMAP_BLOCK);
1987ddf9c6d4STobias Klauser 	if (IS_ERR(va)) {
1988db64fe02SNick Piggin 		kfree(vb);
1989e7d86340SJulia Lawall 		return ERR_CAST(va);
1990db64fe02SNick Piggin 	}
1991db64fe02SNick Piggin 
1992cf725ce2SRoman Pen 	vaddr = vmap_block_vaddr(va->va_start, 0);
1993db64fe02SNick Piggin 	spin_lock_init(&vb->lock);
1994db64fe02SNick Piggin 	vb->va = va;
1995cf725ce2SRoman Pen 	/* At least something should be left free */
1996cf725ce2SRoman Pen 	BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
1997d76f9954SBaoquan He 	bitmap_zero(vb->used_map, VMAP_BBMAP_BITS);
1998cf725ce2SRoman Pen 	vb->free = VMAP_BBMAP_BITS - (1UL << order);
1999db64fe02SNick Piggin 	vb->dirty = 0;
20007d61bfe8SRoman Pen 	vb->dirty_min = VMAP_BBMAP_BITS;
20017d61bfe8SRoman Pen 	vb->dirty_max = 0;
2002d76f9954SBaoquan He 	bitmap_set(vb->used_map, 0, (1UL << order));
2003db64fe02SNick Piggin 	INIT_LIST_HEAD(&vb->free_list);
2004db64fe02SNick Piggin 
2005db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(va->va_start);
20060f14599cSMatthew Wilcox (Oracle) 	err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask);
20070f14599cSMatthew Wilcox (Oracle) 	if (err) {
20080f14599cSMatthew Wilcox (Oracle) 		kfree(vb);
20090f14599cSMatthew Wilcox (Oracle) 		free_vmap_area(va);
20100f14599cSMatthew Wilcox (Oracle) 		return ERR_PTR(err);
20110f14599cSMatthew Wilcox (Oracle) 	}
2012db64fe02SNick Piggin 
20133f804920SSebastian Andrzej Siewior 	vbq = raw_cpu_ptr(&vmap_block_queue);
2014db64fe02SNick Piggin 	spin_lock(&vbq->lock);
201568ac546fSRoman Pen 	list_add_tail_rcu(&vb->free_list, &vbq->free);
2016db64fe02SNick Piggin 	spin_unlock(&vbq->lock);
2017db64fe02SNick Piggin 
2018cf725ce2SRoman Pen 	return vaddr;
2019db64fe02SNick Piggin }
2020db64fe02SNick Piggin 
2021db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb)
2022db64fe02SNick Piggin {
2023db64fe02SNick Piggin 	struct vmap_block *tmp;
2024db64fe02SNick Piggin 
20250f14599cSMatthew Wilcox (Oracle) 	tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start));
2026db64fe02SNick Piggin 	BUG_ON(tmp != vb);
2027db64fe02SNick Piggin 
2028edd89818SUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
2029edd89818SUladzislau Rezki (Sony) 	unlink_va(vb->va, &vmap_area_root);
2030edd89818SUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
2031edd89818SUladzislau Rezki (Sony) 
203264141da5SJeremy Fitzhardinge 	free_vmap_area_noflush(vb->va);
203322a3c7d1SLai Jiangshan 	kfree_rcu(vb, rcu_head);
2034db64fe02SNick Piggin }
2035db64fe02SNick Piggin 
203602b709dfSNick Piggin static void purge_fragmented_blocks(int cpu)
203702b709dfSNick Piggin {
203802b709dfSNick Piggin 	LIST_HEAD(purge);
203902b709dfSNick Piggin 	struct vmap_block *vb;
204002b709dfSNick Piggin 	struct vmap_block *n_vb;
204102b709dfSNick Piggin 	struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
204202b709dfSNick Piggin 
204302b709dfSNick Piggin 	rcu_read_lock();
204402b709dfSNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
204502b709dfSNick Piggin 
204602b709dfSNick Piggin 		if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
204702b709dfSNick Piggin 			continue;
204802b709dfSNick Piggin 
204902b709dfSNick Piggin 		spin_lock(&vb->lock);
205002b709dfSNick Piggin 		if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
205102b709dfSNick Piggin 			vb->free = 0; /* prevent further allocs after releasing lock */
205202b709dfSNick Piggin 			vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
20537d61bfe8SRoman Pen 			vb->dirty_min = 0;
20547d61bfe8SRoman Pen 			vb->dirty_max = VMAP_BBMAP_BITS;
205502b709dfSNick Piggin 			spin_lock(&vbq->lock);
205602b709dfSNick Piggin 			list_del_rcu(&vb->free_list);
205702b709dfSNick Piggin 			spin_unlock(&vbq->lock);
205802b709dfSNick Piggin 			spin_unlock(&vb->lock);
205902b709dfSNick Piggin 			list_add_tail(&vb->purge, &purge);
206002b709dfSNick Piggin 		} else
206102b709dfSNick Piggin 			spin_unlock(&vb->lock);
206202b709dfSNick Piggin 	}
206302b709dfSNick Piggin 	rcu_read_unlock();
206402b709dfSNick Piggin 
206502b709dfSNick Piggin 	list_for_each_entry_safe(vb, n_vb, &purge, purge) {
206602b709dfSNick Piggin 		list_del(&vb->purge);
206702b709dfSNick Piggin 		free_vmap_block(vb);
206802b709dfSNick Piggin 	}
206902b709dfSNick Piggin }
207002b709dfSNick Piggin 
207102b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void)
207202b709dfSNick Piggin {
207302b709dfSNick Piggin 	int cpu;
207402b709dfSNick Piggin 
207502b709dfSNick Piggin 	for_each_possible_cpu(cpu)
207602b709dfSNick Piggin 		purge_fragmented_blocks(cpu);
207702b709dfSNick Piggin }
207802b709dfSNick Piggin 
2079db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
2080db64fe02SNick Piggin {
2081db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
2082db64fe02SNick Piggin 	struct vmap_block *vb;
2083cf725ce2SRoman Pen 	void *vaddr = NULL;
2084db64fe02SNick Piggin 	unsigned int order;
2085db64fe02SNick Piggin 
2086891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
2087db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
2088aa91c4d8SJan Kara 	if (WARN_ON(size == 0)) {
2089aa91c4d8SJan Kara 		/*
2090aa91c4d8SJan Kara 		 * Allocating 0 bytes isn't what caller wants since
2091aa91c4d8SJan Kara 		 * get_order(0) returns funny result. Just warn and terminate
2092aa91c4d8SJan Kara 		 * early.
2093aa91c4d8SJan Kara 		 */
2094aa91c4d8SJan Kara 		return NULL;
2095aa91c4d8SJan Kara 	}
2096db64fe02SNick Piggin 	order = get_order(size);
2097db64fe02SNick Piggin 
2098db64fe02SNick Piggin 	rcu_read_lock();
20993f804920SSebastian Andrzej Siewior 	vbq = raw_cpu_ptr(&vmap_block_queue);
2100db64fe02SNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
2101cf725ce2SRoman Pen 		unsigned long pages_off;
2102db64fe02SNick Piggin 
2103db64fe02SNick Piggin 		spin_lock(&vb->lock);
2104cf725ce2SRoman Pen 		if (vb->free < (1UL << order)) {
2105cf725ce2SRoman Pen 			spin_unlock(&vb->lock);
2106cf725ce2SRoman Pen 			continue;
2107cf725ce2SRoman Pen 		}
210802b709dfSNick Piggin 
2109cf725ce2SRoman Pen 		pages_off = VMAP_BBMAP_BITS - vb->free;
2110cf725ce2SRoman Pen 		vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
2111db64fe02SNick Piggin 		vb->free -= 1UL << order;
2112d76f9954SBaoquan He 		bitmap_set(vb->used_map, pages_off, (1UL << order));
2113db64fe02SNick Piggin 		if (vb->free == 0) {
2114db64fe02SNick Piggin 			spin_lock(&vbq->lock);
2115de560423SNick Piggin 			list_del_rcu(&vb->free_list);
2116db64fe02SNick Piggin 			spin_unlock(&vbq->lock);
2117db64fe02SNick Piggin 		}
2118cf725ce2SRoman Pen 
2119db64fe02SNick Piggin 		spin_unlock(&vb->lock);
2120db64fe02SNick Piggin 		break;
2121db64fe02SNick Piggin 	}
212202b709dfSNick Piggin 
2123db64fe02SNick Piggin 	rcu_read_unlock();
2124db64fe02SNick Piggin 
2125cf725ce2SRoman Pen 	/* Allocate new block if nothing was found */
2126cf725ce2SRoman Pen 	if (!vaddr)
2127cf725ce2SRoman Pen 		vaddr = new_vmap_block(order, gfp_mask);
2128db64fe02SNick Piggin 
2129cf725ce2SRoman Pen 	return vaddr;
2130db64fe02SNick Piggin }
2131db64fe02SNick Piggin 
213278a0e8c4SChristoph Hellwig static void vb_free(unsigned long addr, unsigned long size)
2133db64fe02SNick Piggin {
2134db64fe02SNick Piggin 	unsigned long offset;
2135db64fe02SNick Piggin 	unsigned int order;
2136db64fe02SNick Piggin 	struct vmap_block *vb;
2137db64fe02SNick Piggin 
2138891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
2139db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
2140b29acbdcSNick Piggin 
214178a0e8c4SChristoph Hellwig 	flush_cache_vunmap(addr, addr + size);
2142b29acbdcSNick Piggin 
2143db64fe02SNick Piggin 	order = get_order(size);
214478a0e8c4SChristoph Hellwig 	offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT;
21450f14599cSMatthew Wilcox (Oracle) 	vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr));
2146d76f9954SBaoquan He 	spin_lock(&vb->lock);
2147d76f9954SBaoquan He 	bitmap_clear(vb->used_map, offset, (1UL << order));
2148d76f9954SBaoquan He 	spin_unlock(&vb->lock);
2149db64fe02SNick Piggin 
21504ad0ae8cSNicholas Piggin 	vunmap_range_noflush(addr, addr + size);
215164141da5SJeremy Fitzhardinge 
21528e57f8acSVlastimil Babka 	if (debug_pagealloc_enabled_static())
215378a0e8c4SChristoph Hellwig 		flush_tlb_kernel_range(addr, addr + size);
215482a2e924SChintan Pandya 
2155db64fe02SNick Piggin 	spin_lock(&vb->lock);
21567d61bfe8SRoman Pen 
21577d61bfe8SRoman Pen 	/* Expand dirty range */
21587d61bfe8SRoman Pen 	vb->dirty_min = min(vb->dirty_min, offset);
21597d61bfe8SRoman Pen 	vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
2160d086817dSMinChan Kim 
2161db64fe02SNick Piggin 	vb->dirty += 1UL << order;
2162db64fe02SNick Piggin 	if (vb->dirty == VMAP_BBMAP_BITS) {
2163de560423SNick Piggin 		BUG_ON(vb->free);
2164db64fe02SNick Piggin 		spin_unlock(&vb->lock);
2165db64fe02SNick Piggin 		free_vmap_block(vb);
2166db64fe02SNick Piggin 	} else
2167db64fe02SNick Piggin 		spin_unlock(&vb->lock);
2168db64fe02SNick Piggin }
2169db64fe02SNick Piggin 
2170868b104dSRick Edgecombe static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
2171db64fe02SNick Piggin {
2172db64fe02SNick Piggin 	int cpu;
2173db64fe02SNick Piggin 
21749b463334SJeremy Fitzhardinge 	if (unlikely(!vmap_initialized))
21759b463334SJeremy Fitzhardinge 		return;
21769b463334SJeremy Fitzhardinge 
21775803ed29SChristoph Hellwig 	might_sleep();
21785803ed29SChristoph Hellwig 
2179db64fe02SNick Piggin 	for_each_possible_cpu(cpu) {
2180db64fe02SNick Piggin 		struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
2181db64fe02SNick Piggin 		struct vmap_block *vb;
2182db64fe02SNick Piggin 
2183db64fe02SNick Piggin 		rcu_read_lock();
2184db64fe02SNick Piggin 		list_for_each_entry_rcu(vb, &vbq->free, free_list) {
2185db64fe02SNick Piggin 			spin_lock(&vb->lock);
2186ad216c03SVijayanand Jitta 			if (vb->dirty && vb->dirty != VMAP_BBMAP_BITS) {
21877d61bfe8SRoman Pen 				unsigned long va_start = vb->va->va_start;
2188db64fe02SNick Piggin 				unsigned long s, e;
2189b136be5eSJoonsoo Kim 
21907d61bfe8SRoman Pen 				s = va_start + (vb->dirty_min << PAGE_SHIFT);
21917d61bfe8SRoman Pen 				e = va_start + (vb->dirty_max << PAGE_SHIFT);
2192db64fe02SNick Piggin 
21937d61bfe8SRoman Pen 				start = min(s, start);
21947d61bfe8SRoman Pen 				end   = max(e, end);
21957d61bfe8SRoman Pen 
2196db64fe02SNick Piggin 				flush = 1;
2197db64fe02SNick Piggin 			}
2198db64fe02SNick Piggin 			spin_unlock(&vb->lock);
2199db64fe02SNick Piggin 		}
2200db64fe02SNick Piggin 		rcu_read_unlock();
2201db64fe02SNick Piggin 	}
2202db64fe02SNick Piggin 
2203f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
22040574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
22050574ecd1SChristoph Hellwig 	if (!__purge_vmap_area_lazy(start, end) && flush)
22060574ecd1SChristoph Hellwig 		flush_tlb_kernel_range(start, end);
2207f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
2208db64fe02SNick Piggin }
2209868b104dSRick Edgecombe 
2210868b104dSRick Edgecombe /**
2211868b104dSRick Edgecombe  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
2212868b104dSRick Edgecombe  *
2213868b104dSRick Edgecombe  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
2214868b104dSRick Edgecombe  * to amortize TLB flushing overheads. What this means is that any page you
2215868b104dSRick Edgecombe  * have now, may, in a former life, have been mapped into kernel virtual
2216868b104dSRick Edgecombe  * address by the vmap layer and so there might be some CPUs with TLB entries
2217868b104dSRick Edgecombe  * still referencing that page (additional to the regular 1:1 kernel mapping).
2218868b104dSRick Edgecombe  *
2219868b104dSRick Edgecombe  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
2220868b104dSRick Edgecombe  * be sure that none of the pages we have control over will have any aliases
2221868b104dSRick Edgecombe  * from the vmap layer.
2222868b104dSRick Edgecombe  */
2223868b104dSRick Edgecombe void vm_unmap_aliases(void)
2224868b104dSRick Edgecombe {
2225868b104dSRick Edgecombe 	unsigned long start = ULONG_MAX, end = 0;
2226868b104dSRick Edgecombe 	int flush = 0;
2227868b104dSRick Edgecombe 
2228868b104dSRick Edgecombe 	_vm_unmap_aliases(start, end, flush);
2229868b104dSRick Edgecombe }
2230db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases);
2231db64fe02SNick Piggin 
2232db64fe02SNick Piggin /**
2233db64fe02SNick Piggin  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
2234db64fe02SNick Piggin  * @mem: the pointer returned by vm_map_ram
2235db64fe02SNick Piggin  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
2236db64fe02SNick Piggin  */
2237db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count)
2238db64fe02SNick Piggin {
223965ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
22404aff1dc4SAndrey Konovalov 	unsigned long addr = (unsigned long)kasan_reset_tag(mem);
22419c3acf60SChristoph Hellwig 	struct vmap_area *va;
2242db64fe02SNick Piggin 
22435803ed29SChristoph Hellwig 	might_sleep();
2244db64fe02SNick Piggin 	BUG_ON(!addr);
2245db64fe02SNick Piggin 	BUG_ON(addr < VMALLOC_START);
2246db64fe02SNick Piggin 	BUG_ON(addr > VMALLOC_END);
2247a1c0b1a0SShawn Lin 	BUG_ON(!PAGE_ALIGNED(addr));
2248db64fe02SNick Piggin 
2249d98c9e83SAndrey Ryabinin 	kasan_poison_vmalloc(mem, size);
2250d98c9e83SAndrey Ryabinin 
22519c3acf60SChristoph Hellwig 	if (likely(count <= VMAP_MAX_ALLOC)) {
225205e3ff95SChintan Pandya 		debug_check_no_locks_freed(mem, size);
225378a0e8c4SChristoph Hellwig 		vb_free(addr, size);
22549c3acf60SChristoph Hellwig 		return;
22559c3acf60SChristoph Hellwig 	}
22569c3acf60SChristoph Hellwig 
2257edd89818SUladzislau Rezki (Sony) 	va = find_unlink_vmap_area(addr);
225814687619SUladzislau Rezki (Sony) 	if (WARN_ON_ONCE(!va))
225914687619SUladzislau Rezki (Sony) 		return;
226014687619SUladzislau Rezki (Sony) 
226105e3ff95SChintan Pandya 	debug_check_no_locks_freed((void *)va->va_start,
226205e3ff95SChintan Pandya 				    (va->va_end - va->va_start));
22639c3acf60SChristoph Hellwig 	free_unmap_vmap_area(va);
2264db64fe02SNick Piggin }
2265db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram);
2266db64fe02SNick Piggin 
2267db64fe02SNick Piggin /**
2268db64fe02SNick Piggin  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
2269db64fe02SNick Piggin  * @pages: an array of pointers to the pages to be mapped
2270db64fe02SNick Piggin  * @count: number of pages
2271db64fe02SNick Piggin  * @node: prefer to allocate data structures on this node
2272e99c97adSRandy Dunlap  *
227336437638SGioh Kim  * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
227436437638SGioh Kim  * faster than vmap so it's good.  But if you mix long-life and short-life
227536437638SGioh Kim  * objects with vm_map_ram(), it could consume lots of address space through
227636437638SGioh Kim  * fragmentation (especially on a 32bit machine).  You could see failures in
227736437638SGioh Kim  * the end.  Please use this function for short-lived objects.
227836437638SGioh Kim  *
2279e99c97adSRandy Dunlap  * Returns: a pointer to the address that has been mapped, or %NULL on failure
2280db64fe02SNick Piggin  */
2281d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node)
2282db64fe02SNick Piggin {
228365ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
2284db64fe02SNick Piggin 	unsigned long addr;
2285db64fe02SNick Piggin 	void *mem;
2286db64fe02SNick Piggin 
2287db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC)) {
2288db64fe02SNick Piggin 		mem = vb_alloc(size, GFP_KERNEL);
2289db64fe02SNick Piggin 		if (IS_ERR(mem))
2290db64fe02SNick Piggin 			return NULL;
2291db64fe02SNick Piggin 		addr = (unsigned long)mem;
2292db64fe02SNick Piggin 	} else {
2293db64fe02SNick Piggin 		struct vmap_area *va;
2294db64fe02SNick Piggin 		va = alloc_vmap_area(size, PAGE_SIZE,
2295869176a0SBaoquan He 				VMALLOC_START, VMALLOC_END,
2296869176a0SBaoquan He 				node, GFP_KERNEL, VMAP_RAM);
2297db64fe02SNick Piggin 		if (IS_ERR(va))
2298db64fe02SNick Piggin 			return NULL;
2299db64fe02SNick Piggin 
2300db64fe02SNick Piggin 		addr = va->va_start;
2301db64fe02SNick Piggin 		mem = (void *)addr;
2302db64fe02SNick Piggin 	}
2303d98c9e83SAndrey Ryabinin 
2304b67177ecSNicholas Piggin 	if (vmap_pages_range(addr, addr + size, PAGE_KERNEL,
2305b67177ecSNicholas Piggin 				pages, PAGE_SHIFT) < 0) {
2306db64fe02SNick Piggin 		vm_unmap_ram(mem, count);
2307db64fe02SNick Piggin 		return NULL;
2308db64fe02SNick Piggin 	}
2309b67177ecSNicholas Piggin 
231023689e91SAndrey Konovalov 	/*
231123689e91SAndrey Konovalov 	 * Mark the pages as accessible, now that they are mapped.
231223689e91SAndrey Konovalov 	 * With hardware tag-based KASAN, marking is skipped for
231323689e91SAndrey Konovalov 	 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
231423689e91SAndrey Konovalov 	 */
2315f6e39794SAndrey Konovalov 	mem = kasan_unpoison_vmalloc(mem, size, KASAN_VMALLOC_PROT_NORMAL);
231619f1c3acSAndrey Konovalov 
2317db64fe02SNick Piggin 	return mem;
2318db64fe02SNick Piggin }
2319db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram);
2320db64fe02SNick Piggin 
23214341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata;
232292eac168SMike Rapoport 
2323121e6f32SNicholas Piggin static inline unsigned int vm_area_page_order(struct vm_struct *vm)
2324121e6f32SNicholas Piggin {
2325121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
2326121e6f32SNicholas Piggin 	return vm->page_order;
2327121e6f32SNicholas Piggin #else
2328121e6f32SNicholas Piggin 	return 0;
2329121e6f32SNicholas Piggin #endif
2330121e6f32SNicholas Piggin }
2331121e6f32SNicholas Piggin 
2332121e6f32SNicholas Piggin static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int order)
2333121e6f32SNicholas Piggin {
2334121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
2335121e6f32SNicholas Piggin 	vm->page_order = order;
2336121e6f32SNicholas Piggin #else
2337121e6f32SNicholas Piggin 	BUG_ON(order != 0);
2338121e6f32SNicholas Piggin #endif
2339121e6f32SNicholas Piggin }
2340121e6f32SNicholas Piggin 
2341f0aa6617STejun Heo /**
2342be9b7335SNicolas Pitre  * vm_area_add_early - add vmap area early during boot
2343be9b7335SNicolas Pitre  * @vm: vm_struct to add
2344be9b7335SNicolas Pitre  *
2345be9b7335SNicolas Pitre  * This function is used to add fixed kernel vm area to vmlist before
2346be9b7335SNicolas Pitre  * vmalloc_init() is called.  @vm->addr, @vm->size, and @vm->flags
2347be9b7335SNicolas Pitre  * should contain proper values and the other fields should be zero.
2348be9b7335SNicolas Pitre  *
2349be9b7335SNicolas Pitre  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
2350be9b7335SNicolas Pitre  */
2351be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm)
2352be9b7335SNicolas Pitre {
2353be9b7335SNicolas Pitre 	struct vm_struct *tmp, **p;
2354be9b7335SNicolas Pitre 
2355be9b7335SNicolas Pitre 	BUG_ON(vmap_initialized);
2356be9b7335SNicolas Pitre 	for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
2357be9b7335SNicolas Pitre 		if (tmp->addr >= vm->addr) {
2358be9b7335SNicolas Pitre 			BUG_ON(tmp->addr < vm->addr + vm->size);
2359be9b7335SNicolas Pitre 			break;
2360be9b7335SNicolas Pitre 		} else
2361be9b7335SNicolas Pitre 			BUG_ON(tmp->addr + tmp->size > vm->addr);
2362be9b7335SNicolas Pitre 	}
2363be9b7335SNicolas Pitre 	vm->next = *p;
2364be9b7335SNicolas Pitre 	*p = vm;
2365be9b7335SNicolas Pitre }
2366be9b7335SNicolas Pitre 
2367be9b7335SNicolas Pitre /**
2368f0aa6617STejun Heo  * vm_area_register_early - register vmap area early during boot
2369f0aa6617STejun Heo  * @vm: vm_struct to register
2370c0c0a293STejun Heo  * @align: requested alignment
2371f0aa6617STejun Heo  *
2372f0aa6617STejun Heo  * This function is used to register kernel vm area before
2373f0aa6617STejun Heo  * vmalloc_init() is called.  @vm->size and @vm->flags should contain
2374f0aa6617STejun Heo  * proper values on entry and other fields should be zero.  On return,
2375f0aa6617STejun Heo  * vm->addr contains the allocated address.
2376f0aa6617STejun Heo  *
2377f0aa6617STejun Heo  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
2378f0aa6617STejun Heo  */
2379c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align)
2380f0aa6617STejun Heo {
23810eb68437SKefeng Wang 	unsigned long addr = ALIGN(VMALLOC_START, align);
23820eb68437SKefeng Wang 	struct vm_struct *cur, **p;
2383f0aa6617STejun Heo 
23840eb68437SKefeng Wang 	BUG_ON(vmap_initialized);
2385c0c0a293STejun Heo 
23860eb68437SKefeng Wang 	for (p = &vmlist; (cur = *p) != NULL; p = &cur->next) {
23870eb68437SKefeng Wang 		if ((unsigned long)cur->addr - addr >= vm->size)
23880eb68437SKefeng Wang 			break;
23890eb68437SKefeng Wang 		addr = ALIGN((unsigned long)cur->addr + cur->size, align);
23900eb68437SKefeng Wang 	}
23910eb68437SKefeng Wang 
23920eb68437SKefeng Wang 	BUG_ON(addr > VMALLOC_END - vm->size);
2393c0c0a293STejun Heo 	vm->addr = (void *)addr;
23940eb68437SKefeng Wang 	vm->next = *p;
23950eb68437SKefeng Wang 	*p = vm;
23963252b1d8SKefeng Wang 	kasan_populate_early_vm_area_shadow(vm->addr, vm->size);
2397f0aa6617STejun Heo }
2398f0aa6617STejun Heo 
239968ad4a33SUladzislau Rezki (Sony) static void vmap_init_free_space(void)
240068ad4a33SUladzislau Rezki (Sony) {
240168ad4a33SUladzislau Rezki (Sony) 	unsigned long vmap_start = 1;
240268ad4a33SUladzislau Rezki (Sony) 	const unsigned long vmap_end = ULONG_MAX;
240368ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *busy, *free;
240468ad4a33SUladzislau Rezki (Sony) 
240568ad4a33SUladzislau Rezki (Sony) 	/*
240668ad4a33SUladzislau Rezki (Sony) 	 *     B     F     B     B     B     F
240768ad4a33SUladzislau Rezki (Sony) 	 * -|-----|.....|-----|-----|-----|.....|-
240868ad4a33SUladzislau Rezki (Sony) 	 *  |           The KVA space           |
240968ad4a33SUladzislau Rezki (Sony) 	 *  |<--------------------------------->|
241068ad4a33SUladzislau Rezki (Sony) 	 */
241168ad4a33SUladzislau Rezki (Sony) 	list_for_each_entry(busy, &vmap_area_list, list) {
241268ad4a33SUladzislau Rezki (Sony) 		if (busy->va_start - vmap_start > 0) {
241368ad4a33SUladzislau Rezki (Sony) 			free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
241468ad4a33SUladzislau Rezki (Sony) 			if (!WARN_ON_ONCE(!free)) {
241568ad4a33SUladzislau Rezki (Sony) 				free->va_start = vmap_start;
241668ad4a33SUladzislau Rezki (Sony) 				free->va_end = busy->va_start;
241768ad4a33SUladzislau Rezki (Sony) 
241868ad4a33SUladzislau Rezki (Sony) 				insert_vmap_area_augment(free, NULL,
241968ad4a33SUladzislau Rezki (Sony) 					&free_vmap_area_root,
242068ad4a33SUladzislau Rezki (Sony) 						&free_vmap_area_list);
242168ad4a33SUladzislau Rezki (Sony) 			}
242268ad4a33SUladzislau Rezki (Sony) 		}
242368ad4a33SUladzislau Rezki (Sony) 
242468ad4a33SUladzislau Rezki (Sony) 		vmap_start = busy->va_end;
242568ad4a33SUladzislau Rezki (Sony) 	}
242668ad4a33SUladzislau Rezki (Sony) 
242768ad4a33SUladzislau Rezki (Sony) 	if (vmap_end - vmap_start > 0) {
242868ad4a33SUladzislau Rezki (Sony) 		free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
242968ad4a33SUladzislau Rezki (Sony) 		if (!WARN_ON_ONCE(!free)) {
243068ad4a33SUladzislau Rezki (Sony) 			free->va_start = vmap_start;
243168ad4a33SUladzislau Rezki (Sony) 			free->va_end = vmap_end;
243268ad4a33SUladzislau Rezki (Sony) 
243368ad4a33SUladzislau Rezki (Sony) 			insert_vmap_area_augment(free, NULL,
243468ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_root,
243568ad4a33SUladzislau Rezki (Sony) 					&free_vmap_area_list);
243668ad4a33SUladzislau Rezki (Sony) 		}
243768ad4a33SUladzislau Rezki (Sony) 	}
243868ad4a33SUladzislau Rezki (Sony) }
243968ad4a33SUladzislau Rezki (Sony) 
2440e36176beSUladzislau Rezki (Sony) static inline void setup_vmalloc_vm_locked(struct vm_struct *vm,
2441e36176beSUladzislau Rezki (Sony) 	struct vmap_area *va, unsigned long flags, const void *caller)
2442cf88c790STejun Heo {
2443cf88c790STejun Heo 	vm->flags = flags;
2444cf88c790STejun Heo 	vm->addr = (void *)va->va_start;
2445cf88c790STejun Heo 	vm->size = va->va_end - va->va_start;
2446cf88c790STejun Heo 	vm->caller = caller;
2447db1aecafSMinchan Kim 	va->vm = vm;
2448e36176beSUladzislau Rezki (Sony) }
2449e36176beSUladzislau Rezki (Sony) 
2450e36176beSUladzislau Rezki (Sony) static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
2451e36176beSUladzislau Rezki (Sony) 			      unsigned long flags, const void *caller)
2452e36176beSUladzislau Rezki (Sony) {
2453e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
2454e36176beSUladzislau Rezki (Sony) 	setup_vmalloc_vm_locked(vm, va, flags, caller);
2455c69480adSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2456f5252e00SMitsuo Hayasaka }
2457cf88c790STejun Heo 
245820fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm)
2459f5252e00SMitsuo Hayasaka {
2460d4033afdSJoonsoo Kim 	/*
246120fc02b4SZhang Yanfei 	 * Before removing VM_UNINITIALIZED,
2462d4033afdSJoonsoo Kim 	 * we should make sure that vm has proper values.
2463d4033afdSJoonsoo Kim 	 * Pair with smp_rmb() in show_numa_info().
2464d4033afdSJoonsoo Kim 	 */
2465d4033afdSJoonsoo Kim 	smp_wmb();
246620fc02b4SZhang Yanfei 	vm->flags &= ~VM_UNINITIALIZED;
2467cf88c790STejun Heo }
2468cf88c790STejun Heo 
2469db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size,
24707ca3027bSDaniel Axtens 		unsigned long align, unsigned long shift, unsigned long flags,
24717ca3027bSDaniel Axtens 		unsigned long start, unsigned long end, int node,
24727ca3027bSDaniel Axtens 		gfp_t gfp_mask, const void *caller)
2473db64fe02SNick Piggin {
24740006526dSKautuk Consul 	struct vmap_area *va;
2475db64fe02SNick Piggin 	struct vm_struct *area;
2476d98c9e83SAndrey Ryabinin 	unsigned long requested_size = size;
24771da177e4SLinus Torvalds 
247852fd24caSGiridhar Pemmasani 	BUG_ON(in_interrupt());
24797ca3027bSDaniel Axtens 	size = ALIGN(size, 1ul << shift);
248031be8309SOGAWA Hirofumi 	if (unlikely(!size))
248131be8309SOGAWA Hirofumi 		return NULL;
24821da177e4SLinus Torvalds 
2483252e5c6eSzijun_hu 	if (flags & VM_IOREMAP)
2484252e5c6eSzijun_hu 		align = 1ul << clamp_t(int, get_count_order_long(size),
2485252e5c6eSzijun_hu 				       PAGE_SHIFT, IOREMAP_MAX_ORDER);
2486252e5c6eSzijun_hu 
2487cf88c790STejun Heo 	area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
24881da177e4SLinus Torvalds 	if (unlikely(!area))
24891da177e4SLinus Torvalds 		return NULL;
24901da177e4SLinus Torvalds 
249171394fe5SAndrey Ryabinin 	if (!(flags & VM_NO_GUARD))
24921da177e4SLinus Torvalds 		size += PAGE_SIZE;
24931da177e4SLinus Torvalds 
2494869176a0SBaoquan He 	va = alloc_vmap_area(size, align, start, end, node, gfp_mask, 0);
2495db64fe02SNick Piggin 	if (IS_ERR(va)) {
2496db64fe02SNick Piggin 		kfree(area);
2497db64fe02SNick Piggin 		return NULL;
24981da177e4SLinus Torvalds 	}
24991da177e4SLinus Torvalds 
2500d98c9e83SAndrey Ryabinin 	setup_vmalloc_vm(area, va, flags, caller);
25013c5c3cfbSDaniel Axtens 
250219f1c3acSAndrey Konovalov 	/*
250319f1c3acSAndrey Konovalov 	 * Mark pages for non-VM_ALLOC mappings as accessible. Do it now as a
250419f1c3acSAndrey Konovalov 	 * best-effort approach, as they can be mapped outside of vmalloc code.
250519f1c3acSAndrey Konovalov 	 * For VM_ALLOC mappings, the pages are marked as accessible after
250619f1c3acSAndrey Konovalov 	 * getting mapped in __vmalloc_node_range().
250723689e91SAndrey Konovalov 	 * With hardware tag-based KASAN, marking is skipped for
250823689e91SAndrey Konovalov 	 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
250919f1c3acSAndrey Konovalov 	 */
251019f1c3acSAndrey Konovalov 	if (!(flags & VM_ALLOC))
251123689e91SAndrey Konovalov 		area->addr = kasan_unpoison_vmalloc(area->addr, requested_size,
2512f6e39794SAndrey Konovalov 						    KASAN_VMALLOC_PROT_NORMAL);
25131d96320fSAndrey Konovalov 
25141da177e4SLinus Torvalds 	return area;
25151da177e4SLinus Torvalds }
25161da177e4SLinus Torvalds 
2517c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
2518c2968612SBenjamin Herrenschmidt 				       unsigned long start, unsigned long end,
25195e6cafc8SMarek Szyprowski 				       const void *caller)
2520c2968612SBenjamin Herrenschmidt {
25217ca3027bSDaniel Axtens 	return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, start, end,
25227ca3027bSDaniel Axtens 				  NUMA_NO_NODE, GFP_KERNEL, caller);
2523c2968612SBenjamin Herrenschmidt }
2524c2968612SBenjamin Herrenschmidt 
25251da177e4SLinus Torvalds /**
2526183ff22bSSimon Arlott  * get_vm_area - reserve a contiguous kernel virtual area
25271da177e4SLinus Torvalds  * @size:	 size of the area
25281da177e4SLinus Torvalds  * @flags:	 %VM_IOREMAP for I/O mappings or VM_ALLOC
25291da177e4SLinus Torvalds  *
25301da177e4SLinus Torvalds  * Search an area of @size in the kernel virtual mapping area,
25311da177e4SLinus Torvalds  * and reserved it for out purposes.  Returns the area descriptor
25321da177e4SLinus Torvalds  * on success or %NULL on failure.
2533a862f68aSMike Rapoport  *
2534a862f68aSMike Rapoport  * Return: the area descriptor on success or %NULL on failure.
25351da177e4SLinus Torvalds  */
25361da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
25371da177e4SLinus Torvalds {
25387ca3027bSDaniel Axtens 	return __get_vm_area_node(size, 1, PAGE_SHIFT, flags,
25397ca3027bSDaniel Axtens 				  VMALLOC_START, VMALLOC_END,
254000ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL,
254100ef2d2fSDavid Rientjes 				  __builtin_return_address(0));
254223016969SChristoph Lameter }
254323016969SChristoph Lameter 
254423016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
25455e6cafc8SMarek Szyprowski 				const void *caller)
254623016969SChristoph Lameter {
25477ca3027bSDaniel Axtens 	return __get_vm_area_node(size, 1, PAGE_SHIFT, flags,
25487ca3027bSDaniel Axtens 				  VMALLOC_START, VMALLOC_END,
254900ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL, caller);
25501da177e4SLinus Torvalds }
25511da177e4SLinus Torvalds 
2552e9da6e99SMarek Szyprowski /**
2553e9da6e99SMarek Szyprowski  * find_vm_area - find a continuous kernel virtual area
2554e9da6e99SMarek Szyprowski  * @addr:	  base address
2555e9da6e99SMarek Szyprowski  *
2556e9da6e99SMarek Szyprowski  * Search for the kernel VM area starting at @addr, and return it.
2557e9da6e99SMarek Szyprowski  * It is up to the caller to do all required locking to keep the returned
2558e9da6e99SMarek Szyprowski  * pointer valid.
2559a862f68aSMike Rapoport  *
256074640617SHui Su  * Return: the area descriptor on success or %NULL on failure.
2561e9da6e99SMarek Szyprowski  */
2562e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr)
256383342314SNick Piggin {
2564db64fe02SNick Piggin 	struct vmap_area *va;
256583342314SNick Piggin 
2566db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
2567688fcbfcSPengfei Li 	if (!va)
25687856dfebSAndi Kleen 		return NULL;
2569688fcbfcSPengfei Li 
2570688fcbfcSPengfei Li 	return va->vm;
25717856dfebSAndi Kleen }
25727856dfebSAndi Kleen 
25731da177e4SLinus Torvalds /**
2574183ff22bSSimon Arlott  * remove_vm_area - find and remove a continuous kernel virtual area
25751da177e4SLinus Torvalds  * @addr:	    base address
25761da177e4SLinus Torvalds  *
25771da177e4SLinus Torvalds  * Search for the kernel VM area starting at @addr, and remove it.
25781da177e4SLinus Torvalds  * This function returns the found VM area, but using it is NOT safe
25797856dfebSAndi Kleen  * on SMP machines, except for its size or flags.
2580a862f68aSMike Rapoport  *
258174640617SHui Su  * Return: the area descriptor on success or %NULL on failure.
25821da177e4SLinus Torvalds  */
2583b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr)
25841da177e4SLinus Torvalds {
258575c59ce7SChristoph Hellwig 	struct vmap_area *va;
258675c59ce7SChristoph Hellwig 	struct vm_struct *vm;
258775c59ce7SChristoph Hellwig 
25885803ed29SChristoph Hellwig 	might_sleep();
25895803ed29SChristoph Hellwig 
259017d3ef43SChristoph Hellwig 	if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
259117d3ef43SChristoph Hellwig 			addr))
259217d3ef43SChristoph Hellwig 		return NULL;
259317d3ef43SChristoph Hellwig 
259475c59ce7SChristoph Hellwig 	va = find_unlink_vmap_area((unsigned long)addr);
259575c59ce7SChristoph Hellwig 	if (!va || !va->vm)
259675c59ce7SChristoph Hellwig 		return NULL;
259775c59ce7SChristoph Hellwig 	vm = va->vm;
259817d3ef43SChristoph Hellwig 
259917d3ef43SChristoph Hellwig 	debug_check_no_locks_freed(vm->addr, get_vm_area_size(vm));
260017d3ef43SChristoph Hellwig 	debug_check_no_obj_freed(vm->addr, get_vm_area_size(vm));
260175c59ce7SChristoph Hellwig 	kasan_free_module_shadow(vm);
260217d3ef43SChristoph Hellwig 	kasan_poison_vmalloc(vm->addr, get_vm_area_size(vm));
260317d3ef43SChristoph Hellwig 
260475c59ce7SChristoph Hellwig 	free_unmap_vmap_area(va);
260575c59ce7SChristoph Hellwig 	return vm;
26061da177e4SLinus Torvalds }
26071da177e4SLinus Torvalds 
2608868b104dSRick Edgecombe static inline void set_area_direct_map(const struct vm_struct *area,
2609868b104dSRick Edgecombe 				       int (*set_direct_map)(struct page *page))
2610868b104dSRick Edgecombe {
2611868b104dSRick Edgecombe 	int i;
2612868b104dSRick Edgecombe 
2613121e6f32SNicholas Piggin 	/* HUGE_VMALLOC passes small pages to set_direct_map */
2614868b104dSRick Edgecombe 	for (i = 0; i < area->nr_pages; i++)
2615868b104dSRick Edgecombe 		if (page_address(area->pages[i]))
2616868b104dSRick Edgecombe 			set_direct_map(area->pages[i]);
2617868b104dSRick Edgecombe }
2618868b104dSRick Edgecombe 
26199e5fa0aeSChristoph Hellwig /*
26209e5fa0aeSChristoph Hellwig  * Flush the vm mapping and reset the direct map.
26219e5fa0aeSChristoph Hellwig  */
26229e5fa0aeSChristoph Hellwig static void vm_reset_perms(struct vm_struct *area)
2623868b104dSRick Edgecombe {
2624868b104dSRick Edgecombe 	unsigned long start = ULONG_MAX, end = 0;
2625121e6f32SNicholas Piggin 	unsigned int page_order = vm_area_page_order(area);
262631e67340SRick Edgecombe 	int flush_dmap = 0;
2627868b104dSRick Edgecombe 	int i;
2628868b104dSRick Edgecombe 
2629868b104dSRick Edgecombe 	/*
26309e5fa0aeSChristoph Hellwig 	 * Find the start and end range of the direct mappings to make sure that
2631868b104dSRick Edgecombe 	 * the vm_unmap_aliases() flush includes the direct map.
2632868b104dSRick Edgecombe 	 */
2633121e6f32SNicholas Piggin 	for (i = 0; i < area->nr_pages; i += 1U << page_order) {
26348e41f872SRick Edgecombe 		unsigned long addr = (unsigned long)page_address(area->pages[i]);
26359e5fa0aeSChristoph Hellwig 
26368e41f872SRick Edgecombe 		if (addr) {
2637121e6f32SNicholas Piggin 			unsigned long page_size;
2638121e6f32SNicholas Piggin 
2639121e6f32SNicholas Piggin 			page_size = PAGE_SIZE << page_order;
2640868b104dSRick Edgecombe 			start = min(addr, start);
2641121e6f32SNicholas Piggin 			end = max(addr + page_size, end);
264231e67340SRick Edgecombe 			flush_dmap = 1;
2643868b104dSRick Edgecombe 		}
2644868b104dSRick Edgecombe 	}
2645868b104dSRick Edgecombe 
2646868b104dSRick Edgecombe 	/*
2647868b104dSRick Edgecombe 	 * Set direct map to something invalid so that it won't be cached if
2648868b104dSRick Edgecombe 	 * there are any accesses after the TLB flush, then flush the TLB and
2649868b104dSRick Edgecombe 	 * reset the direct map permissions to the default.
2650868b104dSRick Edgecombe 	 */
2651868b104dSRick Edgecombe 	set_area_direct_map(area, set_direct_map_invalid_noflush);
265231e67340SRick Edgecombe 	_vm_unmap_aliases(start, end, flush_dmap);
2653868b104dSRick Edgecombe 	set_area_direct_map(area, set_direct_map_default_noflush);
2654868b104dSRick Edgecombe }
2655868b104dSRick Edgecombe 
2656208162f4SChristoph Hellwig static void delayed_vfree_work(struct work_struct *w)
2657208162f4SChristoph Hellwig {
2658208162f4SChristoph Hellwig 	struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
2659208162f4SChristoph Hellwig 	struct llist_node *t, *llnode;
2660208162f4SChristoph Hellwig 
2661208162f4SChristoph Hellwig 	llist_for_each_safe(llnode, t, llist_del_all(&p->list))
26625d3d31d6SChristoph Hellwig 		vfree(llnode);
2663208162f4SChristoph Hellwig }
2664208162f4SChristoph Hellwig 
2665bf22e37aSAndrey Ryabinin /**
2666bf22e37aSAndrey Ryabinin  * vfree_atomic - release memory allocated by vmalloc()
2667bf22e37aSAndrey Ryabinin  * @addr:	  memory base address
2668bf22e37aSAndrey Ryabinin  *
2669bf22e37aSAndrey Ryabinin  * This one is just like vfree() but can be called in any atomic context
2670bf22e37aSAndrey Ryabinin  * except NMIs.
2671bf22e37aSAndrey Ryabinin  */
2672bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr)
2673bf22e37aSAndrey Ryabinin {
267401e2e839SChristoph Hellwig 	struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
2675bf22e37aSAndrey Ryabinin 
267601e2e839SChristoph Hellwig 	BUG_ON(in_nmi());
2677bf22e37aSAndrey Ryabinin 	kmemleak_free(addr);
2678bf22e37aSAndrey Ryabinin 
267901e2e839SChristoph Hellwig 	/*
268001e2e839SChristoph Hellwig 	 * Use raw_cpu_ptr() because this can be called from preemptible
268101e2e839SChristoph Hellwig 	 * context. Preemption is absolutely fine here, because the llist_add()
268201e2e839SChristoph Hellwig 	 * implementation is lockless, so it works even if we are adding to
268301e2e839SChristoph Hellwig 	 * another cpu's list. schedule_work() should be fine with this too.
268401e2e839SChristoph Hellwig 	 */
268501e2e839SChristoph Hellwig 	if (addr && llist_add((struct llist_node *)addr, &p->list))
268601e2e839SChristoph Hellwig 		schedule_work(&p->wq);
2687bf22e37aSAndrey Ryabinin }
2688bf22e37aSAndrey Ryabinin 
26891da177e4SLinus Torvalds /**
2690fa307474SMatthew Wilcox (Oracle)  * vfree - Release memory allocated by vmalloc()
2691fa307474SMatthew Wilcox (Oracle)  * @addr:  Memory base address
26921da177e4SLinus Torvalds  *
2693fa307474SMatthew Wilcox (Oracle)  * Free the virtually continuous memory area starting at @addr, as obtained
2694fa307474SMatthew Wilcox (Oracle)  * from one of the vmalloc() family of APIs.  This will usually also free the
2695fa307474SMatthew Wilcox (Oracle)  * physical memory underlying the virtual allocation, but that memory is
2696fa307474SMatthew Wilcox (Oracle)  * reference counted, so it will not be freed until the last user goes away.
26971da177e4SLinus Torvalds  *
2698fa307474SMatthew Wilcox (Oracle)  * If @addr is NULL, no operation is performed.
269932fcfd40SAl Viro  *
2700fa307474SMatthew Wilcox (Oracle)  * Context:
27013ca4ea3aSAndrey Ryabinin  * May sleep if called *not* from interrupt context.
2702fa307474SMatthew Wilcox (Oracle)  * Must not be called in NMI context (strictly speaking, it could be
2703fa307474SMatthew Wilcox (Oracle)  * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
2704f0953a1bSIngo Molnar  * conventions for vfree() arch-dependent would be a really bad idea).
27051da177e4SLinus Torvalds  */
2706b3bdda02SChristoph Lameter void vfree(const void *addr)
27071da177e4SLinus Torvalds {
270879311c1fSChristoph Hellwig 	struct vm_struct *vm;
270979311c1fSChristoph Hellwig 	int i;
271079311c1fSChristoph Hellwig 
271101e2e839SChristoph Hellwig 	if (unlikely(in_interrupt())) {
271201e2e839SChristoph Hellwig 		vfree_atomic(addr);
271332fcfd40SAl Viro 		return;
271401e2e839SChristoph Hellwig 	}
271501e2e839SChristoph Hellwig 
271601e2e839SChristoph Hellwig 	BUG_ON(in_nmi());
271701e2e839SChristoph Hellwig 	kmemleak_free(addr);
271801e2e839SChristoph Hellwig 	might_sleep();
271901e2e839SChristoph Hellwig 
272079311c1fSChristoph Hellwig 	if (!addr)
272179311c1fSChristoph Hellwig 		return;
272279311c1fSChristoph Hellwig 
272379311c1fSChristoph Hellwig 	vm = remove_vm_area(addr);
272479311c1fSChristoph Hellwig 	if (unlikely(!vm)) {
272579311c1fSChristoph Hellwig 		WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
272679311c1fSChristoph Hellwig 				addr);
272779311c1fSChristoph Hellwig 		return;
272879311c1fSChristoph Hellwig 	}
272979311c1fSChristoph Hellwig 
27309e5fa0aeSChristoph Hellwig 	if (unlikely(vm->flags & VM_FLUSH_RESET_PERMS))
27319e5fa0aeSChristoph Hellwig 		vm_reset_perms(vm);
273279311c1fSChristoph Hellwig 	for (i = 0; i < vm->nr_pages; i++) {
273379311c1fSChristoph Hellwig 		struct page *page = vm->pages[i];
273479311c1fSChristoph Hellwig 
273579311c1fSChristoph Hellwig 		BUG_ON(!page);
273679311c1fSChristoph Hellwig 		mod_memcg_page_state(page, MEMCG_VMALLOC, -1);
273779311c1fSChristoph Hellwig 		/*
273879311c1fSChristoph Hellwig 		 * High-order allocs for huge vmallocs are split, so
273979311c1fSChristoph Hellwig 		 * can be freed as an array of order-0 allocations
274079311c1fSChristoph Hellwig 		 */
274179311c1fSChristoph Hellwig 		__free_pages(page, 0);
274279311c1fSChristoph Hellwig 		cond_resched();
274379311c1fSChristoph Hellwig 	}
274479311c1fSChristoph Hellwig 	atomic_long_sub(vm->nr_pages, &nr_vmalloc_pages);
274579311c1fSChristoph Hellwig 	kvfree(vm->pages);
274679311c1fSChristoph Hellwig 	kfree(vm);
27471da177e4SLinus Torvalds }
27481da177e4SLinus Torvalds EXPORT_SYMBOL(vfree);
27491da177e4SLinus Torvalds 
27501da177e4SLinus Torvalds /**
27511da177e4SLinus Torvalds  * vunmap - release virtual mapping obtained by vmap()
27521da177e4SLinus Torvalds  * @addr:   memory base address
27531da177e4SLinus Torvalds  *
27541da177e4SLinus Torvalds  * Free the virtually contiguous memory area starting at @addr,
27551da177e4SLinus Torvalds  * which was created from the page array passed to vmap().
27561da177e4SLinus Torvalds  *
275780e93effSPekka Enberg  * Must not be called in interrupt context.
27581da177e4SLinus Torvalds  */
2759b3bdda02SChristoph Lameter void vunmap(const void *addr)
27601da177e4SLinus Torvalds {
276179311c1fSChristoph Hellwig 	struct vm_struct *vm;
276279311c1fSChristoph Hellwig 
27631da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
276434754b69SPeter Zijlstra 	might_sleep();
276579311c1fSChristoph Hellwig 
276679311c1fSChristoph Hellwig 	if (!addr)
276779311c1fSChristoph Hellwig 		return;
276879311c1fSChristoph Hellwig 	vm = remove_vm_area(addr);
276979311c1fSChristoph Hellwig 	if (unlikely(!vm)) {
277079311c1fSChristoph Hellwig 		WARN(1, KERN_ERR "Trying to vunmap() nonexistent vm area (%p)\n",
277179311c1fSChristoph Hellwig 				addr);
277279311c1fSChristoph Hellwig 		return;
277379311c1fSChristoph Hellwig 	}
277479311c1fSChristoph Hellwig 	kfree(vm);
27751da177e4SLinus Torvalds }
27761da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap);
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds /**
27791da177e4SLinus Torvalds  * vmap - map an array of pages into virtually contiguous space
27801da177e4SLinus Torvalds  * @pages: array of page pointers
27811da177e4SLinus Torvalds  * @count: number of pages to map
27821da177e4SLinus Torvalds  * @flags: vm_area->flags
27831da177e4SLinus Torvalds  * @prot: page protection for the mapping
27841da177e4SLinus Torvalds  *
2785b944afc9SChristoph Hellwig  * Maps @count pages from @pages into contiguous kernel virtual space.
2786b944afc9SChristoph Hellwig  * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself
2787b944afc9SChristoph Hellwig  * (which must be kmalloc or vmalloc memory) and one reference per pages in it
2788b944afc9SChristoph Hellwig  * are transferred from the caller to vmap(), and will be freed / dropped when
2789b944afc9SChristoph Hellwig  * vfree() is called on the return value.
2790a862f68aSMike Rapoport  *
2791a862f68aSMike Rapoport  * Return: the address of the area or %NULL on failure
27921da177e4SLinus Torvalds  */
27931da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count,
27941da177e4SLinus Torvalds 	   unsigned long flags, pgprot_t prot)
27951da177e4SLinus Torvalds {
27961da177e4SLinus Torvalds 	struct vm_struct *area;
2797b67177ecSNicholas Piggin 	unsigned long addr;
279865ee03c4SGuillermo Julián Moreno 	unsigned long size;		/* In bytes */
27991da177e4SLinus Torvalds 
280034754b69SPeter Zijlstra 	might_sleep();
280134754b69SPeter Zijlstra 
280237f3605eSChristoph Hellwig 	if (WARN_ON_ONCE(flags & VM_FLUSH_RESET_PERMS))
280337f3605eSChristoph Hellwig 		return NULL;
280437f3605eSChristoph Hellwig 
2805bd1a8fb2SPeter Zijlstra 	/*
2806bd1a8fb2SPeter Zijlstra 	 * Your top guard is someone else's bottom guard. Not having a top
2807bd1a8fb2SPeter Zijlstra 	 * guard compromises someone else's mappings too.
2808bd1a8fb2SPeter Zijlstra 	 */
2809bd1a8fb2SPeter Zijlstra 	if (WARN_ON_ONCE(flags & VM_NO_GUARD))
2810bd1a8fb2SPeter Zijlstra 		flags &= ~VM_NO_GUARD;
2811bd1a8fb2SPeter Zijlstra 
2812ca79b0c2SArun KS 	if (count > totalram_pages())
28131da177e4SLinus Torvalds 		return NULL;
28141da177e4SLinus Torvalds 
281565ee03c4SGuillermo Julián Moreno 	size = (unsigned long)count << PAGE_SHIFT;
281665ee03c4SGuillermo Julián Moreno 	area = get_vm_area_caller(size, flags, __builtin_return_address(0));
28171da177e4SLinus Torvalds 	if (!area)
28181da177e4SLinus Torvalds 		return NULL;
281923016969SChristoph Lameter 
2820b67177ecSNicholas Piggin 	addr = (unsigned long)area->addr;
2821b67177ecSNicholas Piggin 	if (vmap_pages_range(addr, addr + size, pgprot_nx(prot),
2822b67177ecSNicholas Piggin 				pages, PAGE_SHIFT) < 0) {
28231da177e4SLinus Torvalds 		vunmap(area->addr);
28241da177e4SLinus Torvalds 		return NULL;
28251da177e4SLinus Torvalds 	}
28261da177e4SLinus Torvalds 
2827c22ee528SMiaohe Lin 	if (flags & VM_MAP_PUT_PAGES) {
2828b944afc9SChristoph Hellwig 		area->pages = pages;
2829c22ee528SMiaohe Lin 		area->nr_pages = count;
2830c22ee528SMiaohe Lin 	}
28311da177e4SLinus Torvalds 	return area->addr;
28321da177e4SLinus Torvalds }
28331da177e4SLinus Torvalds EXPORT_SYMBOL(vmap);
28341da177e4SLinus Torvalds 
28353e9a9e25SChristoph Hellwig #ifdef CONFIG_VMAP_PFN
28363e9a9e25SChristoph Hellwig struct vmap_pfn_data {
28373e9a9e25SChristoph Hellwig 	unsigned long	*pfns;
28383e9a9e25SChristoph Hellwig 	pgprot_t	prot;
28393e9a9e25SChristoph Hellwig 	unsigned int	idx;
28403e9a9e25SChristoph Hellwig };
28413e9a9e25SChristoph Hellwig 
28423e9a9e25SChristoph Hellwig static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private)
28433e9a9e25SChristoph Hellwig {
28443e9a9e25SChristoph Hellwig 	struct vmap_pfn_data *data = private;
28453e9a9e25SChristoph Hellwig 
28463e9a9e25SChristoph Hellwig 	if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx])))
28473e9a9e25SChristoph Hellwig 		return -EINVAL;
28483e9a9e25SChristoph Hellwig 	*pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot));
28493e9a9e25SChristoph Hellwig 	return 0;
28503e9a9e25SChristoph Hellwig }
28513e9a9e25SChristoph Hellwig 
28523e9a9e25SChristoph Hellwig /**
28533e9a9e25SChristoph Hellwig  * vmap_pfn - map an array of PFNs into virtually contiguous space
28543e9a9e25SChristoph Hellwig  * @pfns: array of PFNs
28553e9a9e25SChristoph Hellwig  * @count: number of pages to map
28563e9a9e25SChristoph Hellwig  * @prot: page protection for the mapping
28573e9a9e25SChristoph Hellwig  *
28583e9a9e25SChristoph Hellwig  * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns
28593e9a9e25SChristoph Hellwig  * the start address of the mapping.
28603e9a9e25SChristoph Hellwig  */
28613e9a9e25SChristoph Hellwig void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
28623e9a9e25SChristoph Hellwig {
28633e9a9e25SChristoph Hellwig 	struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) };
28643e9a9e25SChristoph Hellwig 	struct vm_struct *area;
28653e9a9e25SChristoph Hellwig 
28663e9a9e25SChristoph Hellwig 	area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP,
28673e9a9e25SChristoph Hellwig 			__builtin_return_address(0));
28683e9a9e25SChristoph Hellwig 	if (!area)
28693e9a9e25SChristoph Hellwig 		return NULL;
28703e9a9e25SChristoph Hellwig 	if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
28713e9a9e25SChristoph Hellwig 			count * PAGE_SIZE, vmap_pfn_apply, &data)) {
28723e9a9e25SChristoph Hellwig 		free_vm_area(area);
28733e9a9e25SChristoph Hellwig 		return NULL;
28743e9a9e25SChristoph Hellwig 	}
28753e9a9e25SChristoph Hellwig 	return area->addr;
28763e9a9e25SChristoph Hellwig }
28773e9a9e25SChristoph Hellwig EXPORT_SYMBOL_GPL(vmap_pfn);
28783e9a9e25SChristoph Hellwig #endif /* CONFIG_VMAP_PFN */
28793e9a9e25SChristoph Hellwig 
288012b9f873SUladzislau Rezki static inline unsigned int
288112b9f873SUladzislau Rezki vm_area_alloc_pages(gfp_t gfp, int nid,
2882343ab817SUladzislau Rezki (Sony) 		unsigned int order, unsigned int nr_pages, struct page **pages)
288312b9f873SUladzislau Rezki {
288412b9f873SUladzislau Rezki 	unsigned int nr_allocated = 0;
2885ffb29b1cSChen Wandun 	struct page *page;
2886ffb29b1cSChen Wandun 	int i;
288712b9f873SUladzislau Rezki 
288812b9f873SUladzislau Rezki 	/*
288912b9f873SUladzislau Rezki 	 * For order-0 pages we make use of bulk allocator, if
289012b9f873SUladzislau Rezki 	 * the page array is partly or not at all populated due
289112b9f873SUladzislau Rezki 	 * to fails, fallback to a single page allocator that is
289212b9f873SUladzislau Rezki 	 * more permissive.
289312b9f873SUladzislau Rezki 	 */
2894c00b6b96SChen Wandun 	if (!order) {
28959376130cSMichal Hocko 		gfp_t bulk_gfp = gfp & ~__GFP_NOFAIL;
28969376130cSMichal Hocko 
2897343ab817SUladzislau Rezki (Sony) 		while (nr_allocated < nr_pages) {
2898343ab817SUladzislau Rezki (Sony) 			unsigned int nr, nr_pages_request;
2899343ab817SUladzislau Rezki (Sony) 
2900343ab817SUladzislau Rezki (Sony) 			/*
2901343ab817SUladzislau Rezki (Sony) 			 * A maximum allowed request is hard-coded and is 100
2902343ab817SUladzislau Rezki (Sony) 			 * pages per call. That is done in order to prevent a
2903343ab817SUladzislau Rezki (Sony) 			 * long preemption off scenario in the bulk-allocator
2904343ab817SUladzislau Rezki (Sony) 			 * so the range is [1:100].
2905343ab817SUladzislau Rezki (Sony) 			 */
2906343ab817SUladzislau Rezki (Sony) 			nr_pages_request = min(100U, nr_pages - nr_allocated);
2907343ab817SUladzislau Rezki (Sony) 
2908c00b6b96SChen Wandun 			/* memory allocation should consider mempolicy, we can't
2909c00b6b96SChen Wandun 			 * wrongly use nearest node when nid == NUMA_NO_NODE,
2910c00b6b96SChen Wandun 			 * otherwise memory may be allocated in only one node,
291198af39d5SYixuan Cao 			 * but mempolicy wants to alloc memory by interleaving.
2912c00b6b96SChen Wandun 			 */
2913c00b6b96SChen Wandun 			if (IS_ENABLED(CONFIG_NUMA) && nid == NUMA_NO_NODE)
29149376130cSMichal Hocko 				nr = alloc_pages_bulk_array_mempolicy(bulk_gfp,
2915c00b6b96SChen Wandun 							nr_pages_request,
2916c00b6b96SChen Wandun 							pages + nr_allocated);
2917c00b6b96SChen Wandun 
2918c00b6b96SChen Wandun 			else
29199376130cSMichal Hocko 				nr = alloc_pages_bulk_array_node(bulk_gfp, nid,
2920c00b6b96SChen Wandun 							nr_pages_request,
2921c00b6b96SChen Wandun 							pages + nr_allocated);
2922343ab817SUladzislau Rezki (Sony) 
2923343ab817SUladzislau Rezki (Sony) 			nr_allocated += nr;
2924343ab817SUladzislau Rezki (Sony) 			cond_resched();
2925343ab817SUladzislau Rezki (Sony) 
2926343ab817SUladzislau Rezki (Sony) 			/*
2927343ab817SUladzislau Rezki (Sony) 			 * If zero or pages were obtained partly,
2928343ab817SUladzislau Rezki (Sony) 			 * fallback to a single page allocator.
2929343ab817SUladzislau Rezki (Sony) 			 */
2930343ab817SUladzislau Rezki (Sony) 			if (nr != nr_pages_request)
2931343ab817SUladzislau Rezki (Sony) 				break;
2932343ab817SUladzislau Rezki (Sony) 		}
29333b8000aeSNicholas Piggin 	}
293412b9f873SUladzislau Rezki 
293512b9f873SUladzislau Rezki 	/* High-order pages or fallback path if "bulk" fails. */
293612b9f873SUladzislau Rezki 
2937ffb29b1cSChen Wandun 	while (nr_allocated < nr_pages) {
2938dd544141SVasily Averin 		if (fatal_signal_pending(current))
2939dd544141SVasily Averin 			break;
2940dd544141SVasily Averin 
2941ffb29b1cSChen Wandun 		if (nid == NUMA_NO_NODE)
2942ffb29b1cSChen Wandun 			page = alloc_pages(gfp, order);
2943ffb29b1cSChen Wandun 		else
294412b9f873SUladzislau Rezki 			page = alloc_pages_node(nid, gfp, order);
294512b9f873SUladzislau Rezki 		if (unlikely(!page))
294612b9f873SUladzislau Rezki 			break;
29473b8000aeSNicholas Piggin 		/*
29483b8000aeSNicholas Piggin 		 * Higher order allocations must be able to be treated as
29493b8000aeSNicholas Piggin 		 * indepdenent small pages by callers (as they can with
29503b8000aeSNicholas Piggin 		 * small-page vmallocs). Some drivers do their own refcounting
29513b8000aeSNicholas Piggin 		 * on vmalloc_to_page() pages, some use page->mapping,
29523b8000aeSNicholas Piggin 		 * page->lru, etc.
29533b8000aeSNicholas Piggin 		 */
29543b8000aeSNicholas Piggin 		if (order)
29553b8000aeSNicholas Piggin 			split_page(page, order);
295612b9f873SUladzislau Rezki 
295712b9f873SUladzislau Rezki 		/*
295812b9f873SUladzislau Rezki 		 * Careful, we allocate and map page-order pages, but
295912b9f873SUladzislau Rezki 		 * tracking is done per PAGE_SIZE page so as to keep the
296012b9f873SUladzislau Rezki 		 * vm_struct APIs independent of the physical/mapped size.
296112b9f873SUladzislau Rezki 		 */
296212b9f873SUladzislau Rezki 		for (i = 0; i < (1U << order); i++)
296312b9f873SUladzislau Rezki 			pages[nr_allocated + i] = page + i;
296412b9f873SUladzislau Rezki 
296512b9f873SUladzislau Rezki 		cond_resched();
296612b9f873SUladzislau Rezki 		nr_allocated += 1U << order;
296712b9f873SUladzislau Rezki 	}
296812b9f873SUladzislau Rezki 
296912b9f873SUladzislau Rezki 	return nr_allocated;
297012b9f873SUladzislau Rezki }
297112b9f873SUladzislau Rezki 
2972e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
2973121e6f32SNicholas Piggin 				 pgprot_t prot, unsigned int page_shift,
2974121e6f32SNicholas Piggin 				 int node)
29751da177e4SLinus Torvalds {
2976930f036bSDavid Rientjes 	const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
29779376130cSMichal Hocko 	bool nofail = gfp_mask & __GFP_NOFAIL;
2978121e6f32SNicholas Piggin 	unsigned long addr = (unsigned long)area->addr;
2979121e6f32SNicholas Piggin 	unsigned long size = get_vm_area_size(area);
298034fe6537SAndrew Morton 	unsigned long array_size;
2981121e6f32SNicholas Piggin 	unsigned int nr_small_pages = size >> PAGE_SHIFT;
2982121e6f32SNicholas Piggin 	unsigned int page_order;
2983451769ebSMichal Hocko 	unsigned int flags;
2984451769ebSMichal Hocko 	int ret;
29851da177e4SLinus Torvalds 
2986121e6f32SNicholas Piggin 	array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
298780b1d8fdSLorenzo Stoakes 
2988f255935bSChristoph Hellwig 	if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
2989f255935bSChristoph Hellwig 		gfp_mask |= __GFP_HIGHMEM;
29901da177e4SLinus Torvalds 
29911da177e4SLinus Torvalds 	/* Please note that the recursion is strictly bounded. */
29928757d5faSJan Kiszka 	if (array_size > PAGE_SIZE) {
29935c1f4e69SUladzislau Rezki (Sony) 		area->pages = __vmalloc_node(array_size, 1, nested_gfp, node,
2994f255935bSChristoph Hellwig 					area->caller);
2995286e1ea3SAndrew Morton 	} else {
29965c1f4e69SUladzislau Rezki (Sony) 		area->pages = kmalloc_node(array_size, nested_gfp, node);
2997286e1ea3SAndrew Morton 	}
29987ea36242SAustin Kim 
29995c1f4e69SUladzislau Rezki (Sony) 	if (!area->pages) {
3000c3d77172SUladzislau Rezki (Sony) 		warn_alloc(gfp_mask, NULL,
3001f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, failed to allocated page array size %lu",
3002d70bec8cSNicholas Piggin 			nr_small_pages * PAGE_SIZE, array_size);
3003cd61413bSUladzislau Rezki (Sony) 		free_vm_area(area);
30041da177e4SLinus Torvalds 		return NULL;
30051da177e4SLinus Torvalds 	}
30061da177e4SLinus Torvalds 
3007121e6f32SNicholas Piggin 	set_vm_area_page_order(area, page_shift - PAGE_SHIFT);
3008121e6f32SNicholas Piggin 	page_order = vm_area_page_order(area);
3009121e6f32SNicholas Piggin 
3010c3d77172SUladzislau Rezki (Sony) 	area->nr_pages = vm_area_alloc_pages(gfp_mask | __GFP_NOWARN,
3011c3d77172SUladzislau Rezki (Sony) 		node, page_order, nr_small_pages, area->pages);
30125c1f4e69SUladzislau Rezki (Sony) 
301397105f0aSRoman Gushchin 	atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
30144e5aa1f4SShakeel Butt 	if (gfp_mask & __GFP_ACCOUNT) {
30153b8000aeSNicholas Piggin 		int i;
30164e5aa1f4SShakeel Butt 
30173b8000aeSNicholas Piggin 		for (i = 0; i < area->nr_pages; i++)
30183b8000aeSNicholas Piggin 			mod_memcg_page_state(area->pages[i], MEMCG_VMALLOC, 1);
30194e5aa1f4SShakeel Butt 	}
30205c1f4e69SUladzislau Rezki (Sony) 
30215c1f4e69SUladzislau Rezki (Sony) 	/*
30225c1f4e69SUladzislau Rezki (Sony) 	 * If not enough pages were obtained to accomplish an
3023f41f036bSChristoph Hellwig 	 * allocation request, free them via vfree() if any.
30245c1f4e69SUladzislau Rezki (Sony) 	 */
30255c1f4e69SUladzislau Rezki (Sony) 	if (area->nr_pages != nr_small_pages) {
3026c3d77172SUladzislau Rezki (Sony) 		warn_alloc(gfp_mask, NULL,
3027f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, page order %u, failed to allocate pages",
3028d70bec8cSNicholas Piggin 			area->nr_pages * PAGE_SIZE, page_order);
30291da177e4SLinus Torvalds 		goto fail;
30301da177e4SLinus Torvalds 	}
3031121e6f32SNicholas Piggin 
3032451769ebSMichal Hocko 	/*
3033451769ebSMichal Hocko 	 * page tables allocations ignore external gfp mask, enforce it
3034451769ebSMichal Hocko 	 * by the scope API
3035451769ebSMichal Hocko 	 */
3036451769ebSMichal Hocko 	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
3037451769ebSMichal Hocko 		flags = memalloc_nofs_save();
3038451769ebSMichal Hocko 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
3039451769ebSMichal Hocko 		flags = memalloc_noio_save();
3040451769ebSMichal Hocko 
30419376130cSMichal Hocko 	do {
3042451769ebSMichal Hocko 		ret = vmap_pages_range(addr, addr + size, prot, area->pages,
3043451769ebSMichal Hocko 			page_shift);
30449376130cSMichal Hocko 		if (nofail && (ret < 0))
30459376130cSMichal Hocko 			schedule_timeout_uninterruptible(1);
30469376130cSMichal Hocko 	} while (nofail && (ret < 0));
3047451769ebSMichal Hocko 
3048451769ebSMichal Hocko 	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
3049451769ebSMichal Hocko 		memalloc_nofs_restore(flags);
3050451769ebSMichal Hocko 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
3051451769ebSMichal Hocko 		memalloc_noio_restore(flags);
3052451769ebSMichal Hocko 
3053451769ebSMichal Hocko 	if (ret < 0) {
3054c3d77172SUladzislau Rezki (Sony) 		warn_alloc(gfp_mask, NULL,
3055f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, failed to map pages",
3056d70bec8cSNicholas Piggin 			area->nr_pages * PAGE_SIZE);
30571da177e4SLinus Torvalds 		goto fail;
3058d70bec8cSNicholas Piggin 	}
3059ed1f324cSChristoph Hellwig 
30601da177e4SLinus Torvalds 	return area->addr;
30611da177e4SLinus Torvalds 
30621da177e4SLinus Torvalds fail:
3063f41f036bSChristoph Hellwig 	vfree(area->addr);
30641da177e4SLinus Torvalds 	return NULL;
30651da177e4SLinus Torvalds }
30661da177e4SLinus Torvalds 
3067d0a21265SDavid Rientjes /**
3068d0a21265SDavid Rientjes  * __vmalloc_node_range - allocate virtually contiguous memory
3069d0a21265SDavid Rientjes  * @size:		  allocation size
3070d0a21265SDavid Rientjes  * @align:		  desired alignment
3071d0a21265SDavid Rientjes  * @start:		  vm area range start
3072d0a21265SDavid Rientjes  * @end:		  vm area range end
3073d0a21265SDavid Rientjes  * @gfp_mask:		  flags for the page level allocator
3074d0a21265SDavid Rientjes  * @prot:		  protection mask for the allocated pages
3075cb9e3c29SAndrey Ryabinin  * @vm_flags:		  additional vm area flags (e.g. %VM_NO_GUARD)
307600ef2d2fSDavid Rientjes  * @node:		  node to use for allocation or NUMA_NO_NODE
3077d0a21265SDavid Rientjes  * @caller:		  caller's return address
3078d0a21265SDavid Rientjes  *
3079d0a21265SDavid Rientjes  * Allocate enough pages to cover @size from the page level
3080b7d90e7aSMichal Hocko  * allocator with @gfp_mask flags. Please note that the full set of gfp
308130d3f011SMichal Hocko  * flags are not supported. GFP_KERNEL, GFP_NOFS and GFP_NOIO are all
308230d3f011SMichal Hocko  * supported.
308330d3f011SMichal Hocko  * Zone modifiers are not supported. From the reclaim modifiers
308430d3f011SMichal Hocko  * __GFP_DIRECT_RECLAIM is required (aka GFP_NOWAIT is not supported)
308530d3f011SMichal Hocko  * and only __GFP_NOFAIL is supported (i.e. __GFP_NORETRY and
308630d3f011SMichal Hocko  * __GFP_RETRY_MAYFAIL are not supported).
308730d3f011SMichal Hocko  *
308830d3f011SMichal Hocko  * __GFP_NOWARN can be used to suppress failures messages.
3089b7d90e7aSMichal Hocko  *
3090b7d90e7aSMichal Hocko  * Map them into contiguous kernel virtual space, using a pagetable
3091b7d90e7aSMichal Hocko  * protection of @prot.
3092a862f68aSMike Rapoport  *
3093a862f68aSMike Rapoport  * Return: the address of the area or %NULL on failure
3094d0a21265SDavid Rientjes  */
3095d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align,
3096d0a21265SDavid Rientjes 			unsigned long start, unsigned long end, gfp_t gfp_mask,
3097cb9e3c29SAndrey Ryabinin 			pgprot_t prot, unsigned long vm_flags, int node,
3098cb9e3c29SAndrey Ryabinin 			const void *caller)
3099930fc45aSChristoph Lameter {
3100d0a21265SDavid Rientjes 	struct vm_struct *area;
310119f1c3acSAndrey Konovalov 	void *ret;
3102f6e39794SAndrey Konovalov 	kasan_vmalloc_flags_t kasan_flags = KASAN_VMALLOC_NONE;
3103d0a21265SDavid Rientjes 	unsigned long real_size = size;
3104121e6f32SNicholas Piggin 	unsigned long real_align = align;
3105121e6f32SNicholas Piggin 	unsigned int shift = PAGE_SHIFT;
3106d0a21265SDavid Rientjes 
3107d70bec8cSNicholas Piggin 	if (WARN_ON_ONCE(!size))
3108d70bec8cSNicholas Piggin 		return NULL;
3109d70bec8cSNicholas Piggin 
3110d70bec8cSNicholas Piggin 	if ((size >> PAGE_SHIFT) > totalram_pages()) {
3111d70bec8cSNicholas Piggin 		warn_alloc(gfp_mask, NULL,
3112f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, exceeds total pages",
3113f4bdfeafSUladzislau Rezki (Sony) 			real_size);
3114d70bec8cSNicholas Piggin 		return NULL;
3115121e6f32SNicholas Piggin 	}
3116d0a21265SDavid Rientjes 
3117559089e0SSong Liu 	if (vmap_allow_huge && (vm_flags & VM_ALLOW_HUGE_VMAP)) {
3118121e6f32SNicholas Piggin 		unsigned long size_per_node;
3119121e6f32SNicholas Piggin 
3120121e6f32SNicholas Piggin 		/*
3121121e6f32SNicholas Piggin 		 * Try huge pages. Only try for PAGE_KERNEL allocations,
3122121e6f32SNicholas Piggin 		 * others like modules don't yet expect huge pages in
3123121e6f32SNicholas Piggin 		 * their allocations due to apply_to_page_range not
3124121e6f32SNicholas Piggin 		 * supporting them.
3125121e6f32SNicholas Piggin 		 */
3126121e6f32SNicholas Piggin 
3127121e6f32SNicholas Piggin 		size_per_node = size;
3128121e6f32SNicholas Piggin 		if (node == NUMA_NO_NODE)
3129121e6f32SNicholas Piggin 			size_per_node /= num_online_nodes();
31303382bbeeSChristophe Leroy 		if (arch_vmap_pmd_supported(prot) && size_per_node >= PMD_SIZE)
3131121e6f32SNicholas Piggin 			shift = PMD_SHIFT;
31323382bbeeSChristophe Leroy 		else
31333382bbeeSChristophe Leroy 			shift = arch_vmap_pte_supported_shift(size_per_node);
31343382bbeeSChristophe Leroy 
3135121e6f32SNicholas Piggin 		align = max(real_align, 1UL << shift);
3136121e6f32SNicholas Piggin 		size = ALIGN(real_size, 1UL << shift);
3137121e6f32SNicholas Piggin 	}
3138121e6f32SNicholas Piggin 
3139121e6f32SNicholas Piggin again:
31407ca3027bSDaniel Axtens 	area = __get_vm_area_node(real_size, align, shift, VM_ALLOC |
31417ca3027bSDaniel Axtens 				  VM_UNINITIALIZED | vm_flags, start, end, node,
31427ca3027bSDaniel Axtens 				  gfp_mask, caller);
3143d70bec8cSNicholas Piggin 	if (!area) {
31449376130cSMichal Hocko 		bool nofail = gfp_mask & __GFP_NOFAIL;
3145d70bec8cSNicholas Piggin 		warn_alloc(gfp_mask, NULL,
31469376130cSMichal Hocko 			"vmalloc error: size %lu, vm_struct allocation failed%s",
31479376130cSMichal Hocko 			real_size, (nofail) ? ". Retrying." : "");
31489376130cSMichal Hocko 		if (nofail) {
31499376130cSMichal Hocko 			schedule_timeout_uninterruptible(1);
31509376130cSMichal Hocko 			goto again;
31519376130cSMichal Hocko 		}
3152de7d2b56SJoe Perches 		goto fail;
3153d70bec8cSNicholas Piggin 	}
3154d0a21265SDavid Rientjes 
3155f6e39794SAndrey Konovalov 	/*
3156f6e39794SAndrey Konovalov 	 * Prepare arguments for __vmalloc_area_node() and
3157f6e39794SAndrey Konovalov 	 * kasan_unpoison_vmalloc().
3158f6e39794SAndrey Konovalov 	 */
3159f6e39794SAndrey Konovalov 	if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) {
3160f6e39794SAndrey Konovalov 		if (kasan_hw_tags_enabled()) {
316101d92c7fSAndrey Konovalov 			/*
316201d92c7fSAndrey Konovalov 			 * Modify protection bits to allow tagging.
3163f6e39794SAndrey Konovalov 			 * This must be done before mapping.
316401d92c7fSAndrey Konovalov 			 */
316501d92c7fSAndrey Konovalov 			prot = arch_vmap_pgprot_tagged(prot);
316601d92c7fSAndrey Konovalov 
316723689e91SAndrey Konovalov 			/*
3168f6e39794SAndrey Konovalov 			 * Skip page_alloc poisoning and zeroing for physical
3169f6e39794SAndrey Konovalov 			 * pages backing VM_ALLOC mapping. Memory is instead
3170f6e39794SAndrey Konovalov 			 * poisoned and zeroed by kasan_unpoison_vmalloc().
317123689e91SAndrey Konovalov 			 */
317223689e91SAndrey Konovalov 			gfp_mask |= __GFP_SKIP_KASAN_UNPOISON | __GFP_SKIP_ZERO;
317323689e91SAndrey Konovalov 		}
317423689e91SAndrey Konovalov 
3175f6e39794SAndrey Konovalov 		/* Take note that the mapping is PAGE_KERNEL. */
3176f6e39794SAndrey Konovalov 		kasan_flags |= KASAN_VMALLOC_PROT_NORMAL;
3177f6e39794SAndrey Konovalov 	}
3178f6e39794SAndrey Konovalov 
317901d92c7fSAndrey Konovalov 	/* Allocate physical pages and map them into vmalloc space. */
318019f1c3acSAndrey Konovalov 	ret = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
318119f1c3acSAndrey Konovalov 	if (!ret)
3182121e6f32SNicholas Piggin 		goto fail;
318389219d37SCatalin Marinas 
318423689e91SAndrey Konovalov 	/*
318523689e91SAndrey Konovalov 	 * Mark the pages as accessible, now that they are mapped.
31866c2f761dSAndrey Konovalov 	 * The condition for setting KASAN_VMALLOC_INIT should complement the
31876c2f761dSAndrey Konovalov 	 * one in post_alloc_hook() with regards to the __GFP_SKIP_ZERO check
31886c2f761dSAndrey Konovalov 	 * to make sure that memory is initialized under the same conditions.
3189f6e39794SAndrey Konovalov 	 * Tag-based KASAN modes only assign tags to normal non-executable
3190f6e39794SAndrey Konovalov 	 * allocations, see __kasan_unpoison_vmalloc().
319123689e91SAndrey Konovalov 	 */
3192f6e39794SAndrey Konovalov 	kasan_flags |= KASAN_VMALLOC_VM_ALLOC;
31936c2f761dSAndrey Konovalov 	if (!want_init_on_free() && want_init_on_alloc(gfp_mask) &&
31946c2f761dSAndrey Konovalov 	    (gfp_mask & __GFP_SKIP_ZERO))
319523689e91SAndrey Konovalov 		kasan_flags |= KASAN_VMALLOC_INIT;
3196f6e39794SAndrey Konovalov 	/* KASAN_VMALLOC_PROT_NORMAL already set if required. */
319723689e91SAndrey Konovalov 	area->addr = kasan_unpoison_vmalloc(area->addr, real_size, kasan_flags);
319819f1c3acSAndrey Konovalov 
319989219d37SCatalin Marinas 	/*
320020fc02b4SZhang Yanfei 	 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
320120fc02b4SZhang Yanfei 	 * flag. It means that vm_struct is not fully initialized.
32024341fa45SJoonsoo Kim 	 * Now, it is fully initialized, so remove this flag here.
3203f5252e00SMitsuo Hayasaka 	 */
320420fc02b4SZhang Yanfei 	clear_vm_uninitialized_flag(area);
3205f5252e00SMitsuo Hayasaka 
32067ca3027bSDaniel Axtens 	size = PAGE_ALIGN(size);
320760115fa5SKefeng Wang 	if (!(vm_flags & VM_DEFER_KMEMLEAK))
320894f4a161SCatalin Marinas 		kmemleak_vmalloc(area, size, gfp_mask);
320989219d37SCatalin Marinas 
321019f1c3acSAndrey Konovalov 	return area->addr;
3211de7d2b56SJoe Perches 
3212de7d2b56SJoe Perches fail:
3213121e6f32SNicholas Piggin 	if (shift > PAGE_SHIFT) {
3214121e6f32SNicholas Piggin 		shift = PAGE_SHIFT;
3215121e6f32SNicholas Piggin 		align = real_align;
3216121e6f32SNicholas Piggin 		size = real_size;
3217121e6f32SNicholas Piggin 		goto again;
3218121e6f32SNicholas Piggin 	}
3219121e6f32SNicholas Piggin 
3220de7d2b56SJoe Perches 	return NULL;
3221930fc45aSChristoph Lameter }
3222930fc45aSChristoph Lameter 
32231da177e4SLinus Torvalds /**
3224930fc45aSChristoph Lameter  * __vmalloc_node - allocate virtually contiguous memory
32251da177e4SLinus Torvalds  * @size:	    allocation size
32262dca6999SDavid Miller  * @align:	    desired alignment
32271da177e4SLinus Torvalds  * @gfp_mask:	    flags for the page level allocator
322800ef2d2fSDavid Rientjes  * @node:	    node to use for allocation or NUMA_NO_NODE
3229c85d194bSRandy Dunlap  * @caller:	    caller's return address
32301da177e4SLinus Torvalds  *
3231f38fcb9cSChristoph Hellwig  * Allocate enough pages to cover @size from the page level allocator with
3232f38fcb9cSChristoph Hellwig  * @gfp_mask flags.  Map them into contiguous kernel virtual space.
3233a7c3e901SMichal Hocko  *
3234dcda9b04SMichal Hocko  * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
3235a7c3e901SMichal Hocko  * and __GFP_NOFAIL are not supported
3236a7c3e901SMichal Hocko  *
3237a7c3e901SMichal Hocko  * Any use of gfp flags outside of GFP_KERNEL should be consulted
3238a7c3e901SMichal Hocko  * with mm people.
3239a862f68aSMike Rapoport  *
3240a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
32411da177e4SLinus Torvalds  */
32422b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align,
3243f38fcb9cSChristoph Hellwig 			    gfp_t gfp_mask, int node, const void *caller)
32441da177e4SLinus Torvalds {
3245d0a21265SDavid Rientjes 	return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
3246f38fcb9cSChristoph Hellwig 				gfp_mask, PAGE_KERNEL, 0, node, caller);
32471da177e4SLinus Torvalds }
3248c3f896dcSChristoph Hellwig /*
3249c3f896dcSChristoph Hellwig  * This is only for performance analysis of vmalloc and stress purpose.
3250c3f896dcSChristoph Hellwig  * It is required by vmalloc test module, therefore do not use it other
3251c3f896dcSChristoph Hellwig  * than that.
3252c3f896dcSChristoph Hellwig  */
3253c3f896dcSChristoph Hellwig #ifdef CONFIG_TEST_VMALLOC_MODULE
3254c3f896dcSChristoph Hellwig EXPORT_SYMBOL_GPL(__vmalloc_node);
3255c3f896dcSChristoph Hellwig #endif
32561da177e4SLinus Torvalds 
325788dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask)
3258930fc45aSChristoph Lameter {
3259f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE,
326023016969SChristoph Lameter 				__builtin_return_address(0));
3261930fc45aSChristoph Lameter }
32621da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc);
32631da177e4SLinus Torvalds 
32641da177e4SLinus Torvalds /**
32651da177e4SLinus Torvalds  * vmalloc - allocate virtually contiguous memory
32661da177e4SLinus Torvalds  * @size:    allocation size
326792eac168SMike Rapoport  *
32681da177e4SLinus Torvalds  * Allocate enough pages to cover @size from the page level
32691da177e4SLinus Torvalds  * allocator and map them into contiguous kernel virtual space.
32701da177e4SLinus Torvalds  *
3271c1c8897fSMichael Opdenacker  * For tight control over page level allocator and protection flags
32721da177e4SLinus Torvalds  * use __vmalloc() instead.
3273a862f68aSMike Rapoport  *
3274a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
32751da177e4SLinus Torvalds  */
32761da177e4SLinus Torvalds void *vmalloc(unsigned long size)
32771da177e4SLinus Torvalds {
32784d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE,
32794d39d728SChristoph Hellwig 				__builtin_return_address(0));
32801da177e4SLinus Torvalds }
32811da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc);
32821da177e4SLinus Torvalds 
3283930fc45aSChristoph Lameter /**
3284559089e0SSong Liu  * vmalloc_huge - allocate virtually contiguous memory, allow huge pages
328515a64f5aSClaudio Imbrenda  * @size:      allocation size
3286559089e0SSong Liu  * @gfp_mask:  flags for the page level allocator
328715a64f5aSClaudio Imbrenda  *
3288559089e0SSong Liu  * Allocate enough pages to cover @size from the page level
328915a64f5aSClaudio Imbrenda  * allocator and map them into contiguous kernel virtual space.
3290559089e0SSong Liu  * If @size is greater than or equal to PMD_SIZE, allow using
3291559089e0SSong Liu  * huge pages for the memory
329215a64f5aSClaudio Imbrenda  *
329315a64f5aSClaudio Imbrenda  * Return: pointer to the allocated memory or %NULL on error
329415a64f5aSClaudio Imbrenda  */
3295559089e0SSong Liu void *vmalloc_huge(unsigned long size, gfp_t gfp_mask)
329615a64f5aSClaudio Imbrenda {
329715a64f5aSClaudio Imbrenda 	return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
3298559089e0SSong Liu 				    gfp_mask, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP,
329915a64f5aSClaudio Imbrenda 				    NUMA_NO_NODE, __builtin_return_address(0));
330015a64f5aSClaudio Imbrenda }
3301559089e0SSong Liu EXPORT_SYMBOL_GPL(vmalloc_huge);
330215a64f5aSClaudio Imbrenda 
330315a64f5aSClaudio Imbrenda /**
3304e1ca7788SDave Young  * vzalloc - allocate virtually contiguous memory with zero fill
3305e1ca7788SDave Young  * @size:    allocation size
330692eac168SMike Rapoport  *
3307e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
3308e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
3309e1ca7788SDave Young  * The memory allocated is set to zero.
3310e1ca7788SDave Young  *
3311e1ca7788SDave Young  * For tight control over page level allocator and protection flags
3312e1ca7788SDave Young  * use __vmalloc() instead.
3313a862f68aSMike Rapoport  *
3314a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
3315e1ca7788SDave Young  */
3316e1ca7788SDave Young void *vzalloc(unsigned long size)
3317e1ca7788SDave Young {
33184d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE,
33194d39d728SChristoph Hellwig 				__builtin_return_address(0));
3320e1ca7788SDave Young }
3321e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
3322e1ca7788SDave Young 
3323e1ca7788SDave Young /**
3324ead04089SRolf Eike Beer  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
332583342314SNick Piggin  * @size: allocation size
3326ead04089SRolf Eike Beer  *
3327ead04089SRolf Eike Beer  * The resulting memory area is zeroed so it can be mapped to userspace
3328ead04089SRolf Eike Beer  * without leaking data.
3329a862f68aSMike Rapoport  *
3330a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
333183342314SNick Piggin  */
333283342314SNick Piggin void *vmalloc_user(unsigned long size)
333383342314SNick Piggin {
3334bc84c535SRoman Penyaev 	return __vmalloc_node_range(size, SHMLBA,  VMALLOC_START, VMALLOC_END,
3335bc84c535SRoman Penyaev 				    GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL,
3336bc84c535SRoman Penyaev 				    VM_USERMAP, NUMA_NO_NODE,
333700ef2d2fSDavid Rientjes 				    __builtin_return_address(0));
333883342314SNick Piggin }
333983342314SNick Piggin EXPORT_SYMBOL(vmalloc_user);
334083342314SNick Piggin 
334183342314SNick Piggin /**
3342930fc45aSChristoph Lameter  * vmalloc_node - allocate memory on a specific node
3343930fc45aSChristoph Lameter  * @size:	  allocation size
3344d44e0780SRandy Dunlap  * @node:	  numa node
3345930fc45aSChristoph Lameter  *
3346930fc45aSChristoph Lameter  * Allocate enough pages to cover @size from the page level
3347930fc45aSChristoph Lameter  * allocator and map them into contiguous kernel virtual space.
3348930fc45aSChristoph Lameter  *
3349c1c8897fSMichael Opdenacker  * For tight control over page level allocator and protection flags
3350930fc45aSChristoph Lameter  * use __vmalloc() instead.
3351a862f68aSMike Rapoport  *
3352a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
3353930fc45aSChristoph Lameter  */
3354930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node)
3355930fc45aSChristoph Lameter {
3356f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL, node,
3357f38fcb9cSChristoph Hellwig 			__builtin_return_address(0));
3358930fc45aSChristoph Lameter }
3359930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node);
3360930fc45aSChristoph Lameter 
3361e1ca7788SDave Young /**
3362e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
3363e1ca7788SDave Young  * @size:	allocation size
3364e1ca7788SDave Young  * @node:	numa node
3365e1ca7788SDave Young  *
3366e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
3367e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
3368e1ca7788SDave Young  * The memory allocated is set to zero.
3369e1ca7788SDave Young  *
3370a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
3371e1ca7788SDave Young  */
3372e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
3373e1ca7788SDave Young {
33744d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node,
33754d39d728SChristoph Hellwig 				__builtin_return_address(0));
3376e1ca7788SDave Young }
3377e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
3378e1ca7788SDave Young 
33790d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
3380698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
33810d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
3382698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
33830d08e0d3SAndi Kleen #else
3384698d0831SMichal Hocko /*
3385698d0831SMichal Hocko  * 64b systems should always have either DMA or DMA32 zones. For others
3386698d0831SMichal Hocko  * GFP_DMA32 should do the right thing and use the normal zone.
3387698d0831SMichal Hocko  */
338868d68ff6SZhiyuan Dai #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
33890d08e0d3SAndi Kleen #endif
33900d08e0d3SAndi Kleen 
33911da177e4SLinus Torvalds /**
33921da177e4SLinus Torvalds  * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
33931da177e4SLinus Torvalds  * @size:	allocation size
33941da177e4SLinus Torvalds  *
33951da177e4SLinus Torvalds  * Allocate enough 32bit PA addressable pages to cover @size from the
33961da177e4SLinus Torvalds  * page level allocator and map them into contiguous kernel virtual space.
3397a862f68aSMike Rapoport  *
3398a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
33991da177e4SLinus Torvalds  */
34001da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
34011da177e4SLinus Torvalds {
3402f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE,
3403f38fcb9cSChristoph Hellwig 			__builtin_return_address(0));
34041da177e4SLinus Torvalds }
34051da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32);
34061da177e4SLinus Torvalds 
340783342314SNick Piggin /**
3408ead04089SRolf Eike Beer  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
340983342314SNick Piggin  * @size:	     allocation size
3410ead04089SRolf Eike Beer  *
3411ead04089SRolf Eike Beer  * The resulting memory area is 32bit addressable and zeroed so it can be
3412ead04089SRolf Eike Beer  * mapped to userspace without leaking data.
3413a862f68aSMike Rapoport  *
3414a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
341583342314SNick Piggin  */
341683342314SNick Piggin void *vmalloc_32_user(unsigned long size)
341783342314SNick Piggin {
3418bc84c535SRoman Penyaev 	return __vmalloc_node_range(size, SHMLBA,  VMALLOC_START, VMALLOC_END,
3419bc84c535SRoman Penyaev 				    GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
3420bc84c535SRoman Penyaev 				    VM_USERMAP, NUMA_NO_NODE,
34215a82ac71SRoman Penyaev 				    __builtin_return_address(0));
342283342314SNick Piggin }
342383342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user);
342483342314SNick Piggin 
3425d0107eb0SKAMEZAWA Hiroyuki /*
3426d0107eb0SKAMEZAWA Hiroyuki  * small helper routine , copy contents to buf from addr.
3427d0107eb0SKAMEZAWA Hiroyuki  * If the page is not present, fill zero.
3428d0107eb0SKAMEZAWA Hiroyuki  */
3429d0107eb0SKAMEZAWA Hiroyuki 
3430d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count)
3431d0107eb0SKAMEZAWA Hiroyuki {
3432d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
3433d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
3434d0107eb0SKAMEZAWA Hiroyuki 
3435d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
3436d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
3437d0107eb0SKAMEZAWA Hiroyuki 
3438891c49abSAlexander Kuleshov 		offset = offset_in_page(addr);
3439d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
3440d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
3441d0107eb0SKAMEZAWA Hiroyuki 			length = count;
3442d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
3443d0107eb0SKAMEZAWA Hiroyuki 		/*
3444d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
3445d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
3446f0953a1bSIngo Molnar 		 * overhead of vmalloc()/vfree() calls for this _debug_
3447d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
3448d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
3449d0107eb0SKAMEZAWA Hiroyuki 		 */
3450d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
3451f7c8ce44SDavid Hildenbrand 			/* We can expect USER0 is not used -- see vread() */
34529b04c5feSCong Wang 			void *map = kmap_atomic(p);
3453d0107eb0SKAMEZAWA Hiroyuki 			memcpy(buf, map + offset, length);
34549b04c5feSCong Wang 			kunmap_atomic(map);
3455d0107eb0SKAMEZAWA Hiroyuki 		} else
3456d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, length);
3457d0107eb0SKAMEZAWA Hiroyuki 
3458d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
3459d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
3460d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
3461d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
3462d0107eb0SKAMEZAWA Hiroyuki 	}
3463d0107eb0SKAMEZAWA Hiroyuki 	return copied;
3464d0107eb0SKAMEZAWA Hiroyuki }
3465d0107eb0SKAMEZAWA Hiroyuki 
3466*06c89946SBaoquan He static void vmap_ram_vread(char *buf, char *addr, int count, unsigned long flags)
3467*06c89946SBaoquan He {
3468*06c89946SBaoquan He 	char *start;
3469*06c89946SBaoquan He 	struct vmap_block *vb;
3470*06c89946SBaoquan He 	unsigned long offset;
3471*06c89946SBaoquan He 	unsigned int rs, re, n;
3472*06c89946SBaoquan He 
3473*06c89946SBaoquan He 	/*
3474*06c89946SBaoquan He 	 * If it's area created by vm_map_ram() interface directly, but
3475*06c89946SBaoquan He 	 * not further subdividing and delegating management to vmap_block,
3476*06c89946SBaoquan He 	 * handle it here.
3477*06c89946SBaoquan He 	 */
3478*06c89946SBaoquan He 	if (!(flags & VMAP_BLOCK)) {
3479*06c89946SBaoquan He 		aligned_vread(buf, addr, count);
3480*06c89946SBaoquan He 		return;
3481*06c89946SBaoquan He 	}
3482*06c89946SBaoquan He 
3483*06c89946SBaoquan He 	/*
3484*06c89946SBaoquan He 	 * Area is split into regions and tracked with vmap_block, read out
3485*06c89946SBaoquan He 	 * each region and zero fill the hole between regions.
3486*06c89946SBaoquan He 	 */
3487*06c89946SBaoquan He 	vb = xa_load(&vmap_blocks, addr_to_vb_idx((unsigned long)addr));
3488*06c89946SBaoquan He 	if (!vb)
3489*06c89946SBaoquan He 		goto finished;
3490*06c89946SBaoquan He 
3491*06c89946SBaoquan He 	spin_lock(&vb->lock);
3492*06c89946SBaoquan He 	if (bitmap_empty(vb->used_map, VMAP_BBMAP_BITS)) {
3493*06c89946SBaoquan He 		spin_unlock(&vb->lock);
3494*06c89946SBaoquan He 		goto finished;
3495*06c89946SBaoquan He 	}
3496*06c89946SBaoquan He 	for_each_set_bitrange(rs, re, vb->used_map, VMAP_BBMAP_BITS) {
3497*06c89946SBaoquan He 		if (!count)
3498*06c89946SBaoquan He 			break;
3499*06c89946SBaoquan He 		start = vmap_block_vaddr(vb->va->va_start, rs);
3500*06c89946SBaoquan He 		while (addr < start) {
3501*06c89946SBaoquan He 			if (count == 0)
3502*06c89946SBaoquan He 				goto unlock;
3503*06c89946SBaoquan He 			*buf = '\0';
3504*06c89946SBaoquan He 			buf++;
3505*06c89946SBaoquan He 			addr++;
3506*06c89946SBaoquan He 			count--;
3507*06c89946SBaoquan He 		}
3508*06c89946SBaoquan He 		/*it could start reading from the middle of used region*/
3509*06c89946SBaoquan He 		offset = offset_in_page(addr);
3510*06c89946SBaoquan He 		n = ((re - rs + 1) << PAGE_SHIFT) - offset;
3511*06c89946SBaoquan He 		if (n > count)
3512*06c89946SBaoquan He 			n = count;
3513*06c89946SBaoquan He 		aligned_vread(buf, start+offset, n);
3514*06c89946SBaoquan He 
3515*06c89946SBaoquan He 		buf += n;
3516*06c89946SBaoquan He 		addr += n;
3517*06c89946SBaoquan He 		count -= n;
3518*06c89946SBaoquan He 	}
3519*06c89946SBaoquan He unlock:
3520*06c89946SBaoquan He 	spin_unlock(&vb->lock);
3521*06c89946SBaoquan He 
3522*06c89946SBaoquan He finished:
3523*06c89946SBaoquan He 	/* zero-fill the left dirty or free regions */
3524*06c89946SBaoquan He 	if (count)
3525*06c89946SBaoquan He 		memset(buf, 0, count);
3526*06c89946SBaoquan He }
3527*06c89946SBaoquan He 
3528d0107eb0SKAMEZAWA Hiroyuki /**
3529d0107eb0SKAMEZAWA Hiroyuki  * vread() - read vmalloc area in a safe way.
3530d0107eb0SKAMEZAWA Hiroyuki  * @buf:     buffer for reading data
3531d0107eb0SKAMEZAWA Hiroyuki  * @addr:    vm address.
3532d0107eb0SKAMEZAWA Hiroyuki  * @count:   number of bytes to be read.
3533d0107eb0SKAMEZAWA Hiroyuki  *
3534d0107eb0SKAMEZAWA Hiroyuki  * This function checks that addr is a valid vmalloc'ed area, and
3535d0107eb0SKAMEZAWA Hiroyuki  * copy data from that area to a given buffer. If the given memory range
3536d0107eb0SKAMEZAWA Hiroyuki  * of [addr...addr+count) includes some valid address, data is copied to
3537d0107eb0SKAMEZAWA Hiroyuki  * proper area of @buf. If there are memory holes, they'll be zero-filled.
3538d0107eb0SKAMEZAWA Hiroyuki  * IOREMAP area is treated as memory hole and no copy is done.
3539d0107eb0SKAMEZAWA Hiroyuki  *
3540d0107eb0SKAMEZAWA Hiroyuki  * If [addr...addr+count) doesn't includes any intersects with alive
3541a8e5202dSCong Wang  * vm_struct area, returns 0. @buf should be kernel's buffer.
3542d0107eb0SKAMEZAWA Hiroyuki  *
3543d0107eb0SKAMEZAWA Hiroyuki  * Note: In usual ops, vread() is never necessary because the caller
3544d0107eb0SKAMEZAWA Hiroyuki  * should know vmalloc() area is valid and can use memcpy().
3545d0107eb0SKAMEZAWA Hiroyuki  * This is for routines which have to access vmalloc area without
3546bbcd53c9SDavid Hildenbrand  * any information, as /proc/kcore.
3547a862f68aSMike Rapoport  *
3548a862f68aSMike Rapoport  * Return: number of bytes for which addr and buf should be increased
3549a862f68aSMike Rapoport  * (same number as @count) or %0 if [addr...addr+count) doesn't
3550a862f68aSMike Rapoport  * include any intersection with valid vmalloc area
3551d0107eb0SKAMEZAWA Hiroyuki  */
35521da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
35531da177e4SLinus Torvalds {
3554e81ce85fSJoonsoo Kim 	struct vmap_area *va;
3555e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
35561da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
3557d0107eb0SKAMEZAWA Hiroyuki 	unsigned long buflen = count;
3558*06c89946SBaoquan He 	unsigned long n, size, flags;
35591da177e4SLinus Torvalds 
35604aff1dc4SAndrey Konovalov 	addr = kasan_reset_tag(addr);
35614aff1dc4SAndrey Konovalov 
35621da177e4SLinus Torvalds 	/* Don't allow overflow */
35631da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
35641da177e4SLinus Torvalds 		count = -(unsigned long) addr;
35651da177e4SLinus Torvalds 
3566e81ce85fSJoonsoo Kim 	spin_lock(&vmap_area_lock);
3567f181234aSChen Wandun 	va = find_vmap_area_exceed_addr((unsigned long)addr);
3568f608788cSSerapheim Dimitropoulos 	if (!va)
3569f608788cSSerapheim Dimitropoulos 		goto finished;
3570f181234aSChen Wandun 
3571f181234aSChen Wandun 	/* no intersects with alive vmap_area */
3572f181234aSChen Wandun 	if ((unsigned long)addr + count <= va->va_start)
3573f181234aSChen Wandun 		goto finished;
3574f181234aSChen Wandun 
3575f608788cSSerapheim Dimitropoulos 	list_for_each_entry_from(va, &vmap_area_list, list) {
3576e81ce85fSJoonsoo Kim 		if (!count)
3577e81ce85fSJoonsoo Kim 			break;
3578e81ce85fSJoonsoo Kim 
3579*06c89946SBaoquan He 		vm = va->vm;
3580*06c89946SBaoquan He 		flags = va->flags & VMAP_FLAGS_MASK;
3581*06c89946SBaoquan He 		/*
3582*06c89946SBaoquan He 		 * VMAP_BLOCK indicates a sub-type of vm_map_ram area, need
3583*06c89946SBaoquan He 		 * be set together with VMAP_RAM.
3584*06c89946SBaoquan He 		 */
3585*06c89946SBaoquan He 		WARN_ON(flags == VMAP_BLOCK);
3586*06c89946SBaoquan He 
3587*06c89946SBaoquan He 		if (!vm && !flags)
3588e81ce85fSJoonsoo Kim 			continue;
3589e81ce85fSJoonsoo Kim 
3590*06c89946SBaoquan He 		vaddr = (char *) va->va_start;
3591*06c89946SBaoquan He 		size = vm ? get_vm_area_size(vm) : va_size(va);
3592*06c89946SBaoquan He 
3593*06c89946SBaoquan He 		if (addr >= vaddr + size)
35941da177e4SLinus Torvalds 			continue;
35951da177e4SLinus Torvalds 		while (addr < vaddr) {
35961da177e4SLinus Torvalds 			if (count == 0)
35971da177e4SLinus Torvalds 				goto finished;
35981da177e4SLinus Torvalds 			*buf = '\0';
35991da177e4SLinus Torvalds 			buf++;
36001da177e4SLinus Torvalds 			addr++;
36011da177e4SLinus Torvalds 			count--;
36021da177e4SLinus Torvalds 		}
3603*06c89946SBaoquan He 		n = vaddr + size - addr;
3604d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
3605d0107eb0SKAMEZAWA Hiroyuki 			n = count;
3606*06c89946SBaoquan He 
3607*06c89946SBaoquan He 		if (flags & VMAP_RAM)
3608*06c89946SBaoquan He 			vmap_ram_vread(buf, addr, n, flags);
3609*06c89946SBaoquan He 		else if (!(vm->flags & VM_IOREMAP))
3610d0107eb0SKAMEZAWA Hiroyuki 			aligned_vread(buf, addr, n);
3611d0107eb0SKAMEZAWA Hiroyuki 		else /* IOREMAP area is treated as memory hole */
3612d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, n);
3613d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
3614d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
3615d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
36161da177e4SLinus Torvalds 	}
36171da177e4SLinus Torvalds finished:
3618e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
3619d0107eb0SKAMEZAWA Hiroyuki 
3620d0107eb0SKAMEZAWA Hiroyuki 	if (buf == buf_start)
3621d0107eb0SKAMEZAWA Hiroyuki 		return 0;
3622d0107eb0SKAMEZAWA Hiroyuki 	/* zero-fill memory holes */
3623d0107eb0SKAMEZAWA Hiroyuki 	if (buf != buf_start + buflen)
3624d0107eb0SKAMEZAWA Hiroyuki 		memset(buf, 0, buflen - (buf - buf_start));
3625d0107eb0SKAMEZAWA Hiroyuki 
3626d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
36271da177e4SLinus Torvalds }
36281da177e4SLinus Torvalds 
3629d0107eb0SKAMEZAWA Hiroyuki /**
3630e69e9d4aSHATAYAMA Daisuke  * remap_vmalloc_range_partial - map vmalloc pages to userspace
3631e69e9d4aSHATAYAMA Daisuke  * @vma:		vma to cover
3632e69e9d4aSHATAYAMA Daisuke  * @uaddr:		target user address to start at
3633e69e9d4aSHATAYAMA Daisuke  * @kaddr:		virtual address of vmalloc kernel memory
3634bdebd6a2SJann Horn  * @pgoff:		offset from @kaddr to start at
3635e69e9d4aSHATAYAMA Daisuke  * @size:		size of map area
3636e69e9d4aSHATAYAMA Daisuke  *
3637e69e9d4aSHATAYAMA Daisuke  * Returns:	0 for success, -Exxx on failure
3638e69e9d4aSHATAYAMA Daisuke  *
3639e69e9d4aSHATAYAMA Daisuke  * This function checks that @kaddr is a valid vmalloc'ed area,
3640e69e9d4aSHATAYAMA Daisuke  * and that it is big enough to cover the range starting at
3641e69e9d4aSHATAYAMA Daisuke  * @uaddr in @vma. Will return failure if that criteria isn't
3642e69e9d4aSHATAYAMA Daisuke  * met.
3643e69e9d4aSHATAYAMA Daisuke  *
3644e69e9d4aSHATAYAMA Daisuke  * Similar to remap_pfn_range() (see mm/memory.c)
3645e69e9d4aSHATAYAMA Daisuke  */
3646e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
3647bdebd6a2SJann Horn 				void *kaddr, unsigned long pgoff,
3648bdebd6a2SJann Horn 				unsigned long size)
3649e69e9d4aSHATAYAMA Daisuke {
3650e69e9d4aSHATAYAMA Daisuke 	struct vm_struct *area;
3651bdebd6a2SJann Horn 	unsigned long off;
3652bdebd6a2SJann Horn 	unsigned long end_index;
3653bdebd6a2SJann Horn 
3654bdebd6a2SJann Horn 	if (check_shl_overflow(pgoff, PAGE_SHIFT, &off))
3655bdebd6a2SJann Horn 		return -EINVAL;
3656e69e9d4aSHATAYAMA Daisuke 
3657e69e9d4aSHATAYAMA Daisuke 	size = PAGE_ALIGN(size);
3658e69e9d4aSHATAYAMA Daisuke 
3659e69e9d4aSHATAYAMA Daisuke 	if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
3660e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3661e69e9d4aSHATAYAMA Daisuke 
3662e69e9d4aSHATAYAMA Daisuke 	area = find_vm_area(kaddr);
3663e69e9d4aSHATAYAMA Daisuke 	if (!area)
3664e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3665e69e9d4aSHATAYAMA Daisuke 
3666fe9041c2SChristoph Hellwig 	if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT)))
3667e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3668e69e9d4aSHATAYAMA Daisuke 
3669bdebd6a2SJann Horn 	if (check_add_overflow(size, off, &end_index) ||
3670bdebd6a2SJann Horn 	    end_index > get_vm_area_size(area))
3671e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3672bdebd6a2SJann Horn 	kaddr += off;
3673e69e9d4aSHATAYAMA Daisuke 
3674e69e9d4aSHATAYAMA Daisuke 	do {
3675e69e9d4aSHATAYAMA Daisuke 		struct page *page = vmalloc_to_page(kaddr);
3676e69e9d4aSHATAYAMA Daisuke 		int ret;
3677e69e9d4aSHATAYAMA Daisuke 
3678e69e9d4aSHATAYAMA Daisuke 		ret = vm_insert_page(vma, uaddr, page);
3679e69e9d4aSHATAYAMA Daisuke 		if (ret)
3680e69e9d4aSHATAYAMA Daisuke 			return ret;
3681e69e9d4aSHATAYAMA Daisuke 
3682e69e9d4aSHATAYAMA Daisuke 		uaddr += PAGE_SIZE;
3683e69e9d4aSHATAYAMA Daisuke 		kaddr += PAGE_SIZE;
3684e69e9d4aSHATAYAMA Daisuke 		size -= PAGE_SIZE;
3685e69e9d4aSHATAYAMA Daisuke 	} while (size > 0);
3686e69e9d4aSHATAYAMA Daisuke 
36871c71222eSSuren Baghdasaryan 	vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
3688e69e9d4aSHATAYAMA Daisuke 
3689e69e9d4aSHATAYAMA Daisuke 	return 0;
3690e69e9d4aSHATAYAMA Daisuke }
3691e69e9d4aSHATAYAMA Daisuke 
3692e69e9d4aSHATAYAMA Daisuke /**
369383342314SNick Piggin  * remap_vmalloc_range - map vmalloc pages to userspace
369483342314SNick Piggin  * @vma:		vma to cover (map full range of vma)
369583342314SNick Piggin  * @addr:		vmalloc memory
369683342314SNick Piggin  * @pgoff:		number of pages into addr before first page to map
36977682486bSRandy Dunlap  *
36987682486bSRandy Dunlap  * Returns:	0 for success, -Exxx on failure
369983342314SNick Piggin  *
370083342314SNick Piggin  * This function checks that addr is a valid vmalloc'ed area, and
370183342314SNick Piggin  * that it is big enough to cover the vma. Will return failure if
370283342314SNick Piggin  * that criteria isn't met.
370383342314SNick Piggin  *
370472fd4a35SRobert P. J. Day  * Similar to remap_pfn_range() (see mm/memory.c)
370583342314SNick Piggin  */
370683342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
370783342314SNick Piggin 						unsigned long pgoff)
370883342314SNick Piggin {
3709e69e9d4aSHATAYAMA Daisuke 	return remap_vmalloc_range_partial(vma, vma->vm_start,
3710bdebd6a2SJann Horn 					   addr, pgoff,
3711e69e9d4aSHATAYAMA Daisuke 					   vma->vm_end - vma->vm_start);
371283342314SNick Piggin }
371383342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range);
371483342314SNick Piggin 
37155f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area)
37165f4352fbSJeremy Fitzhardinge {
37175f4352fbSJeremy Fitzhardinge 	struct vm_struct *ret;
37185f4352fbSJeremy Fitzhardinge 	ret = remove_vm_area(area->addr);
37195f4352fbSJeremy Fitzhardinge 	BUG_ON(ret != area);
37205f4352fbSJeremy Fitzhardinge 	kfree(area);
37215f4352fbSJeremy Fitzhardinge }
37225f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area);
3723a10aa579SChristoph Lameter 
37244f8b02b4STejun Heo #ifdef CONFIG_SMP
3725ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n)
3726ca23e405STejun Heo {
37274583e773SGeliang Tang 	return rb_entry_safe(n, struct vmap_area, rb_node);
3728ca23e405STejun Heo }
3729ca23e405STejun Heo 
3730ca23e405STejun Heo /**
373168ad4a33SUladzislau Rezki (Sony)  * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
373268ad4a33SUladzislau Rezki (Sony)  * @addr: target address
3733ca23e405STejun Heo  *
373468ad4a33SUladzislau Rezki (Sony)  * Returns: vmap_area if it is found. If there is no such area
373568ad4a33SUladzislau Rezki (Sony)  *   the first highest(reverse order) vmap_area is returned
373668ad4a33SUladzislau Rezki (Sony)  *   i.e. va->va_start < addr && va->va_end < addr or NULL
373768ad4a33SUladzislau Rezki (Sony)  *   if there are no any areas before @addr.
3738ca23e405STejun Heo  */
373968ad4a33SUladzislau Rezki (Sony) static struct vmap_area *
374068ad4a33SUladzislau Rezki (Sony) pvm_find_va_enclose_addr(unsigned long addr)
3741ca23e405STejun Heo {
374268ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va, *tmp;
374368ad4a33SUladzislau Rezki (Sony) 	struct rb_node *n;
374468ad4a33SUladzislau Rezki (Sony) 
374568ad4a33SUladzislau Rezki (Sony) 	n = free_vmap_area_root.rb_node;
374668ad4a33SUladzislau Rezki (Sony) 	va = NULL;
3747ca23e405STejun Heo 
3748ca23e405STejun Heo 	while (n) {
374968ad4a33SUladzislau Rezki (Sony) 		tmp = rb_entry(n, struct vmap_area, rb_node);
375068ad4a33SUladzislau Rezki (Sony) 		if (tmp->va_start <= addr) {
375168ad4a33SUladzislau Rezki (Sony) 			va = tmp;
375268ad4a33SUladzislau Rezki (Sony) 			if (tmp->va_end >= addr)
3753ca23e405STejun Heo 				break;
3754ca23e405STejun Heo 
375568ad4a33SUladzislau Rezki (Sony) 			n = n->rb_right;
3756ca23e405STejun Heo 		} else {
375768ad4a33SUladzislau Rezki (Sony) 			n = n->rb_left;
3758ca23e405STejun Heo 		}
375968ad4a33SUladzislau Rezki (Sony) 	}
376068ad4a33SUladzislau Rezki (Sony) 
376168ad4a33SUladzislau Rezki (Sony) 	return va;
3762ca23e405STejun Heo }
3763ca23e405STejun Heo 
3764ca23e405STejun Heo /**
376568ad4a33SUladzislau Rezki (Sony)  * pvm_determine_end_from_reverse - find the highest aligned address
376668ad4a33SUladzislau Rezki (Sony)  * of free block below VMALLOC_END
376768ad4a33SUladzislau Rezki (Sony)  * @va:
376868ad4a33SUladzislau Rezki (Sony)  *   in - the VA we start the search(reverse order);
376968ad4a33SUladzislau Rezki (Sony)  *   out - the VA with the highest aligned end address.
3770799fa85dSAlex Shi  * @align: alignment for required highest address
3771ca23e405STejun Heo  *
377268ad4a33SUladzislau Rezki (Sony)  * Returns: determined end address within vmap_area
3773ca23e405STejun Heo  */
377468ad4a33SUladzislau Rezki (Sony) static unsigned long
377568ad4a33SUladzislau Rezki (Sony) pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
3776ca23e405STejun Heo {
377768ad4a33SUladzislau Rezki (Sony) 	unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
3778ca23e405STejun Heo 	unsigned long addr;
3779ca23e405STejun Heo 
378068ad4a33SUladzislau Rezki (Sony) 	if (likely(*va)) {
378168ad4a33SUladzislau Rezki (Sony) 		list_for_each_entry_from_reverse((*va),
378268ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_list, list) {
378368ad4a33SUladzislau Rezki (Sony) 			addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
378468ad4a33SUladzislau Rezki (Sony) 			if ((*va)->va_start < addr)
378568ad4a33SUladzislau Rezki (Sony) 				return addr;
378668ad4a33SUladzislau Rezki (Sony) 		}
3787ca23e405STejun Heo 	}
3788ca23e405STejun Heo 
378968ad4a33SUladzislau Rezki (Sony) 	return 0;
3790ca23e405STejun Heo }
3791ca23e405STejun Heo 
3792ca23e405STejun Heo /**
3793ca23e405STejun Heo  * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3794ca23e405STejun Heo  * @offsets: array containing offset of each area
3795ca23e405STejun Heo  * @sizes: array containing size of each area
3796ca23e405STejun Heo  * @nr_vms: the number of areas to allocate
3797ca23e405STejun Heo  * @align: alignment, all entries in @offsets and @sizes must be aligned to this
3798ca23e405STejun Heo  *
3799ca23e405STejun Heo  * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3800ca23e405STejun Heo  *	    vm_structs on success, %NULL on failure
3801ca23e405STejun Heo  *
3802ca23e405STejun Heo  * Percpu allocator wants to use congruent vm areas so that it can
3803ca23e405STejun Heo  * maintain the offsets among percpu areas.  This function allocates
3804ec3f64fcSDavid Rientjes  * congruent vmalloc areas for it with GFP_KERNEL.  These areas tend to
3805ec3f64fcSDavid Rientjes  * be scattered pretty far, distance between two areas easily going up
3806ec3f64fcSDavid Rientjes  * to gigabytes.  To avoid interacting with regular vmallocs, these
3807ec3f64fcSDavid Rientjes  * areas are allocated from top.
3808ca23e405STejun Heo  *
3809ca23e405STejun Heo  * Despite its complicated look, this allocator is rather simple. It
381068ad4a33SUladzislau Rezki (Sony)  * does everything top-down and scans free blocks from the end looking
381168ad4a33SUladzislau Rezki (Sony)  * for matching base. While scanning, if any of the areas do not fit the
381268ad4a33SUladzislau Rezki (Sony)  * base address is pulled down to fit the area. Scanning is repeated till
381368ad4a33SUladzislau Rezki (Sony)  * all the areas fit and then all necessary data structures are inserted
381468ad4a33SUladzislau Rezki (Sony)  * and the result is returned.
3815ca23e405STejun Heo  */
3816ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3817ca23e405STejun Heo 				     const size_t *sizes, int nr_vms,
3818ec3f64fcSDavid Rientjes 				     size_t align)
3819ca23e405STejun Heo {
3820ca23e405STejun Heo 	const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3821ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
382268ad4a33SUladzislau Rezki (Sony) 	struct vmap_area **vas, *va;
3823ca23e405STejun Heo 	struct vm_struct **vms;
3824ca23e405STejun Heo 	int area, area2, last_area, term_area;
3825253a496dSDaniel Axtens 	unsigned long base, start, size, end, last_end, orig_start, orig_end;
3826ca23e405STejun Heo 	bool purged = false;
3827ca23e405STejun Heo 
3828ca23e405STejun Heo 	/* verify parameters and allocate data structures */
3829891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(align) || !is_power_of_2(align));
3830ca23e405STejun Heo 	for (last_area = 0, area = 0; area < nr_vms; area++) {
3831ca23e405STejun Heo 		start = offsets[area];
3832ca23e405STejun Heo 		end = start + sizes[area];
3833ca23e405STejun Heo 
3834ca23e405STejun Heo 		/* is everything aligned properly? */
3835ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(offsets[area], align));
3836ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(sizes[area], align));
3837ca23e405STejun Heo 
3838ca23e405STejun Heo 		/* detect the area with the highest address */
3839ca23e405STejun Heo 		if (start > offsets[last_area])
3840ca23e405STejun Heo 			last_area = area;
3841ca23e405STejun Heo 
3842c568da28SWei Yang 		for (area2 = area + 1; area2 < nr_vms; area2++) {
3843ca23e405STejun Heo 			unsigned long start2 = offsets[area2];
3844ca23e405STejun Heo 			unsigned long end2 = start2 + sizes[area2];
3845ca23e405STejun Heo 
3846c568da28SWei Yang 			BUG_ON(start2 < end && start < end2);
3847ca23e405STejun Heo 		}
3848ca23e405STejun Heo 	}
3849ca23e405STejun Heo 	last_end = offsets[last_area] + sizes[last_area];
3850ca23e405STejun Heo 
3851ca23e405STejun Heo 	if (vmalloc_end - vmalloc_start < last_end) {
3852ca23e405STejun Heo 		WARN_ON(true);
3853ca23e405STejun Heo 		return NULL;
3854ca23e405STejun Heo 	}
3855ca23e405STejun Heo 
38564d67d860SThomas Meyer 	vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
38574d67d860SThomas Meyer 	vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
3858ca23e405STejun Heo 	if (!vas || !vms)
3859f1db7afdSKautuk Consul 		goto err_free2;
3860ca23e405STejun Heo 
3861ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
386268ad4a33SUladzislau Rezki (Sony) 		vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
3863ec3f64fcSDavid Rientjes 		vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
3864ca23e405STejun Heo 		if (!vas[area] || !vms[area])
3865ca23e405STejun Heo 			goto err_free;
3866ca23e405STejun Heo 	}
3867ca23e405STejun Heo retry:
3868e36176beSUladzislau Rezki (Sony) 	spin_lock(&free_vmap_area_lock);
3869ca23e405STejun Heo 
3870ca23e405STejun Heo 	/* start scanning - we scan from the top, begin with the last area */
3871ca23e405STejun Heo 	area = term_area = last_area;
3872ca23e405STejun Heo 	start = offsets[area];
3873ca23e405STejun Heo 	end = start + sizes[area];
3874ca23e405STejun Heo 
387568ad4a33SUladzislau Rezki (Sony) 	va = pvm_find_va_enclose_addr(vmalloc_end);
387668ad4a33SUladzislau Rezki (Sony) 	base = pvm_determine_end_from_reverse(&va, align) - end;
3877ca23e405STejun Heo 
3878ca23e405STejun Heo 	while (true) {
3879ca23e405STejun Heo 		/*
3880ca23e405STejun Heo 		 * base might have underflowed, add last_end before
3881ca23e405STejun Heo 		 * comparing.
3882ca23e405STejun Heo 		 */
388368ad4a33SUladzislau Rezki (Sony) 		if (base + last_end < vmalloc_start + last_end)
388468ad4a33SUladzislau Rezki (Sony) 			goto overflow;
3885ca23e405STejun Heo 
3886ca23e405STejun Heo 		/*
388768ad4a33SUladzislau Rezki (Sony) 		 * Fitting base has not been found.
3888ca23e405STejun Heo 		 */
388968ad4a33SUladzislau Rezki (Sony) 		if (va == NULL)
389068ad4a33SUladzislau Rezki (Sony) 			goto overflow;
3891ca23e405STejun Heo 
3892ca23e405STejun Heo 		/*
3893d8cc323dSQiujun Huang 		 * If required width exceeds current VA block, move
38945336e52cSKuppuswamy Sathyanarayanan 		 * base downwards and then recheck.
38955336e52cSKuppuswamy Sathyanarayanan 		 */
38965336e52cSKuppuswamy Sathyanarayanan 		if (base + end > va->va_end) {
38975336e52cSKuppuswamy Sathyanarayanan 			base = pvm_determine_end_from_reverse(&va, align) - end;
38985336e52cSKuppuswamy Sathyanarayanan 			term_area = area;
38995336e52cSKuppuswamy Sathyanarayanan 			continue;
39005336e52cSKuppuswamy Sathyanarayanan 		}
39015336e52cSKuppuswamy Sathyanarayanan 
39025336e52cSKuppuswamy Sathyanarayanan 		/*
390368ad4a33SUladzislau Rezki (Sony) 		 * If this VA does not fit, move base downwards and recheck.
3904ca23e405STejun Heo 		 */
39055336e52cSKuppuswamy Sathyanarayanan 		if (base + start < va->va_start) {
390668ad4a33SUladzislau Rezki (Sony) 			va = node_to_va(rb_prev(&va->rb_node));
390768ad4a33SUladzislau Rezki (Sony) 			base = pvm_determine_end_from_reverse(&va, align) - end;
3908ca23e405STejun Heo 			term_area = area;
3909ca23e405STejun Heo 			continue;
3910ca23e405STejun Heo 		}
3911ca23e405STejun Heo 
3912ca23e405STejun Heo 		/*
3913ca23e405STejun Heo 		 * This area fits, move on to the previous one.  If
3914ca23e405STejun Heo 		 * the previous one is the terminal one, we're done.
3915ca23e405STejun Heo 		 */
3916ca23e405STejun Heo 		area = (area + nr_vms - 1) % nr_vms;
3917ca23e405STejun Heo 		if (area == term_area)
3918ca23e405STejun Heo 			break;
391968ad4a33SUladzislau Rezki (Sony) 
3920ca23e405STejun Heo 		start = offsets[area];
3921ca23e405STejun Heo 		end = start + sizes[area];
392268ad4a33SUladzislau Rezki (Sony) 		va = pvm_find_va_enclose_addr(base + end);
3923ca23e405STejun Heo 	}
392468ad4a33SUladzislau Rezki (Sony) 
3925ca23e405STejun Heo 	/* we've found a fitting base, insert all va's */
3926ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
392768ad4a33SUladzislau Rezki (Sony) 		int ret;
3928ca23e405STejun Heo 
392968ad4a33SUladzislau Rezki (Sony) 		start = base + offsets[area];
393068ad4a33SUladzislau Rezki (Sony) 		size = sizes[area];
393168ad4a33SUladzislau Rezki (Sony) 
393268ad4a33SUladzislau Rezki (Sony) 		va = pvm_find_va_enclose_addr(start);
393368ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(va == NULL))
393468ad4a33SUladzislau Rezki (Sony) 			/* It is a BUG(), but trigger recovery instead. */
393568ad4a33SUladzislau Rezki (Sony) 			goto recovery;
393668ad4a33SUladzislau Rezki (Sony) 
3937f9863be4SUladzislau Rezki (Sony) 		ret = adjust_va_to_fit_type(&free_vmap_area_root,
3938f9863be4SUladzislau Rezki (Sony) 					    &free_vmap_area_list,
3939f9863be4SUladzislau Rezki (Sony) 					    va, start, size);
39401b23ff80SBaoquan He 		if (WARN_ON_ONCE(unlikely(ret)))
394168ad4a33SUladzislau Rezki (Sony) 			/* It is a BUG(), but trigger recovery instead. */
394268ad4a33SUladzislau Rezki (Sony) 			goto recovery;
394368ad4a33SUladzislau Rezki (Sony) 
394468ad4a33SUladzislau Rezki (Sony) 		/* Allocated area. */
394568ad4a33SUladzislau Rezki (Sony) 		va = vas[area];
394668ad4a33SUladzislau Rezki (Sony) 		va->va_start = start;
394768ad4a33SUladzislau Rezki (Sony) 		va->va_end = start + size;
3948ca23e405STejun Heo 	}
3949ca23e405STejun Heo 
3950e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
3951ca23e405STejun Heo 
3952253a496dSDaniel Axtens 	/* populate the kasan shadow space */
3953253a496dSDaniel Axtens 	for (area = 0; area < nr_vms; area++) {
3954253a496dSDaniel Axtens 		if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area]))
3955253a496dSDaniel Axtens 			goto err_free_shadow;
3956253a496dSDaniel Axtens 	}
3957253a496dSDaniel Axtens 
3958ca23e405STejun Heo 	/* insert all vm's */
3959e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
3960e36176beSUladzislau Rezki (Sony) 	for (area = 0; area < nr_vms; area++) {
3961e36176beSUladzislau Rezki (Sony) 		insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list);
3962e36176beSUladzislau Rezki (Sony) 
3963e36176beSUladzislau Rezki (Sony) 		setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC,
3964ca23e405STejun Heo 				 pcpu_get_vm_areas);
3965e36176beSUladzislau Rezki (Sony) 	}
3966e36176beSUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
3967ca23e405STejun Heo 
396819f1c3acSAndrey Konovalov 	/*
396919f1c3acSAndrey Konovalov 	 * Mark allocated areas as accessible. Do it now as a best-effort
397019f1c3acSAndrey Konovalov 	 * approach, as they can be mapped outside of vmalloc code.
397123689e91SAndrey Konovalov 	 * With hardware tag-based KASAN, marking is skipped for
397223689e91SAndrey Konovalov 	 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
397319f1c3acSAndrey Konovalov 	 */
39741d96320fSAndrey Konovalov 	for (area = 0; area < nr_vms; area++)
39751d96320fSAndrey Konovalov 		vms[area]->addr = kasan_unpoison_vmalloc(vms[area]->addr,
3976f6e39794SAndrey Konovalov 				vms[area]->size, KASAN_VMALLOC_PROT_NORMAL);
39771d96320fSAndrey Konovalov 
3978ca23e405STejun Heo 	kfree(vas);
3979ca23e405STejun Heo 	return vms;
3980ca23e405STejun Heo 
398168ad4a33SUladzislau Rezki (Sony) recovery:
3982e36176beSUladzislau Rezki (Sony) 	/*
3983e36176beSUladzislau Rezki (Sony) 	 * Remove previously allocated areas. There is no
3984e36176beSUladzislau Rezki (Sony) 	 * need in removing these areas from the busy tree,
3985e36176beSUladzislau Rezki (Sony) 	 * because they are inserted only on the final step
3986e36176beSUladzislau Rezki (Sony) 	 * and when pcpu_get_vm_areas() is success.
3987e36176beSUladzislau Rezki (Sony) 	 */
398868ad4a33SUladzislau Rezki (Sony) 	while (area--) {
3989253a496dSDaniel Axtens 		orig_start = vas[area]->va_start;
3990253a496dSDaniel Axtens 		orig_end = vas[area]->va_end;
399196e2db45SUladzislau Rezki (Sony) 		va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
39923c5c3cfbSDaniel Axtens 				&free_vmap_area_list);
39939c801f61SUladzislau Rezki (Sony) 		if (va)
3994253a496dSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
3995253a496dSDaniel Axtens 				va->va_start, va->va_end);
399668ad4a33SUladzislau Rezki (Sony) 		vas[area] = NULL;
399768ad4a33SUladzislau Rezki (Sony) 	}
399868ad4a33SUladzislau Rezki (Sony) 
399968ad4a33SUladzislau Rezki (Sony) overflow:
4000e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
400168ad4a33SUladzislau Rezki (Sony) 	if (!purged) {
400268ad4a33SUladzislau Rezki (Sony) 		purge_vmap_area_lazy();
400368ad4a33SUladzislau Rezki (Sony) 		purged = true;
400468ad4a33SUladzislau Rezki (Sony) 
400568ad4a33SUladzislau Rezki (Sony) 		/* Before "retry", check if we recover. */
400668ad4a33SUladzislau Rezki (Sony) 		for (area = 0; area < nr_vms; area++) {
400768ad4a33SUladzislau Rezki (Sony) 			if (vas[area])
400868ad4a33SUladzislau Rezki (Sony) 				continue;
400968ad4a33SUladzislau Rezki (Sony) 
401068ad4a33SUladzislau Rezki (Sony) 			vas[area] = kmem_cache_zalloc(
401168ad4a33SUladzislau Rezki (Sony) 				vmap_area_cachep, GFP_KERNEL);
401268ad4a33SUladzislau Rezki (Sony) 			if (!vas[area])
401368ad4a33SUladzislau Rezki (Sony) 				goto err_free;
401468ad4a33SUladzislau Rezki (Sony) 		}
401568ad4a33SUladzislau Rezki (Sony) 
401668ad4a33SUladzislau Rezki (Sony) 		goto retry;
401768ad4a33SUladzislau Rezki (Sony) 	}
401868ad4a33SUladzislau Rezki (Sony) 
4019ca23e405STejun Heo err_free:
4020ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
402168ad4a33SUladzislau Rezki (Sony) 		if (vas[area])
402268ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, vas[area]);
402368ad4a33SUladzislau Rezki (Sony) 
4024ca23e405STejun Heo 		kfree(vms[area]);
4025ca23e405STejun Heo 	}
4026f1db7afdSKautuk Consul err_free2:
4027ca23e405STejun Heo 	kfree(vas);
4028ca23e405STejun Heo 	kfree(vms);
4029ca23e405STejun Heo 	return NULL;
4030253a496dSDaniel Axtens 
4031253a496dSDaniel Axtens err_free_shadow:
4032253a496dSDaniel Axtens 	spin_lock(&free_vmap_area_lock);
4033253a496dSDaniel Axtens 	/*
4034253a496dSDaniel Axtens 	 * We release all the vmalloc shadows, even the ones for regions that
4035253a496dSDaniel Axtens 	 * hadn't been successfully added. This relies on kasan_release_vmalloc
4036253a496dSDaniel Axtens 	 * being able to tolerate this case.
4037253a496dSDaniel Axtens 	 */
4038253a496dSDaniel Axtens 	for (area = 0; area < nr_vms; area++) {
4039253a496dSDaniel Axtens 		orig_start = vas[area]->va_start;
4040253a496dSDaniel Axtens 		orig_end = vas[area]->va_end;
404196e2db45SUladzislau Rezki (Sony) 		va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
4042253a496dSDaniel Axtens 				&free_vmap_area_list);
40439c801f61SUladzislau Rezki (Sony) 		if (va)
4044253a496dSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
4045253a496dSDaniel Axtens 				va->va_start, va->va_end);
4046253a496dSDaniel Axtens 		vas[area] = NULL;
4047253a496dSDaniel Axtens 		kfree(vms[area]);
4048253a496dSDaniel Axtens 	}
4049253a496dSDaniel Axtens 	spin_unlock(&free_vmap_area_lock);
4050253a496dSDaniel Axtens 	kfree(vas);
4051253a496dSDaniel Axtens 	kfree(vms);
4052253a496dSDaniel Axtens 	return NULL;
4053ca23e405STejun Heo }
4054ca23e405STejun Heo 
4055ca23e405STejun Heo /**
4056ca23e405STejun Heo  * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
4057ca23e405STejun Heo  * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
4058ca23e405STejun Heo  * @nr_vms: the number of allocated areas
4059ca23e405STejun Heo  *
4060ca23e405STejun Heo  * Free vm_structs and the array allocated by pcpu_get_vm_areas().
4061ca23e405STejun Heo  */
4062ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
4063ca23e405STejun Heo {
4064ca23e405STejun Heo 	int i;
4065ca23e405STejun Heo 
4066ca23e405STejun Heo 	for (i = 0; i < nr_vms; i++)
4067ca23e405STejun Heo 		free_vm_area(vms[i]);
4068ca23e405STejun Heo 	kfree(vms);
4069ca23e405STejun Heo }
40704f8b02b4STejun Heo #endif	/* CONFIG_SMP */
4071a10aa579SChristoph Lameter 
40725bb1bb35SPaul E. McKenney #ifdef CONFIG_PRINTK
407398f18083SPaul E. McKenney bool vmalloc_dump_obj(void *object)
407498f18083SPaul E. McKenney {
407598f18083SPaul E. McKenney 	struct vm_struct *vm;
407698f18083SPaul E. McKenney 	void *objp = (void *)PAGE_ALIGN((unsigned long)object);
407798f18083SPaul E. McKenney 
407898f18083SPaul E. McKenney 	vm = find_vm_area(objp);
407998f18083SPaul E. McKenney 	if (!vm)
408098f18083SPaul E. McKenney 		return false;
4081bd34dcd4SPaul E. McKenney 	pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
4082bd34dcd4SPaul E. McKenney 		vm->nr_pages, (unsigned long)vm->addr, vm->caller);
408398f18083SPaul E. McKenney 	return true;
408498f18083SPaul E. McKenney }
40855bb1bb35SPaul E. McKenney #endif
408698f18083SPaul E. McKenney 
4087a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS
4088a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos)
4089e36176beSUladzislau Rezki (Sony) 	__acquires(&vmap_purge_lock)
4090d4033afdSJoonsoo Kim 	__acquires(&vmap_area_lock)
4091a10aa579SChristoph Lameter {
4092e36176beSUladzislau Rezki (Sony) 	mutex_lock(&vmap_purge_lock);
4093d4033afdSJoonsoo Kim 	spin_lock(&vmap_area_lock);
4094e36176beSUladzislau Rezki (Sony) 
40953f500069Szijun_hu 	return seq_list_start(&vmap_area_list, *pos);
4096a10aa579SChristoph Lameter }
4097a10aa579SChristoph Lameter 
4098a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4099a10aa579SChristoph Lameter {
41003f500069Szijun_hu 	return seq_list_next(p, &vmap_area_list, pos);
4101a10aa579SChristoph Lameter }
4102a10aa579SChristoph Lameter 
4103a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p)
4104d4033afdSJoonsoo Kim 	__releases(&vmap_area_lock)
41050a7dd4e9SWaiman Long 	__releases(&vmap_purge_lock)
4106a10aa579SChristoph Lameter {
4107d4033afdSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
41080a7dd4e9SWaiman Long 	mutex_unlock(&vmap_purge_lock);
4109a10aa579SChristoph Lameter }
4110a10aa579SChristoph Lameter 
4111a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v)
4112a47a126aSEric Dumazet {
4113e5adfffcSKirill A. Shutemov 	if (IS_ENABLED(CONFIG_NUMA)) {
4114a47a126aSEric Dumazet 		unsigned int nr, *counters = m->private;
411551e50b3aSEric Dumazet 		unsigned int step = 1U << vm_area_page_order(v);
4116a47a126aSEric Dumazet 
4117a47a126aSEric Dumazet 		if (!counters)
4118a47a126aSEric Dumazet 			return;
4119a47a126aSEric Dumazet 
4120af12346cSWanpeng Li 		if (v->flags & VM_UNINITIALIZED)
4121af12346cSWanpeng Li 			return;
41227e5b528bSDmitry Vyukov 		/* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
41237e5b528bSDmitry Vyukov 		smp_rmb();
4124af12346cSWanpeng Li 
4125a47a126aSEric Dumazet 		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
4126a47a126aSEric Dumazet 
412751e50b3aSEric Dumazet 		for (nr = 0; nr < v->nr_pages; nr += step)
412851e50b3aSEric Dumazet 			counters[page_to_nid(v->pages[nr])] += step;
4129a47a126aSEric Dumazet 		for_each_node_state(nr, N_HIGH_MEMORY)
4130a47a126aSEric Dumazet 			if (counters[nr])
4131a47a126aSEric Dumazet 				seq_printf(m, " N%u=%u", nr, counters[nr]);
4132a47a126aSEric Dumazet 	}
4133a47a126aSEric Dumazet }
4134a47a126aSEric Dumazet 
4135dd3b8353SUladzislau Rezki (Sony) static void show_purge_info(struct seq_file *m)
4136dd3b8353SUladzislau Rezki (Sony) {
4137dd3b8353SUladzislau Rezki (Sony) 	struct vmap_area *va;
4138dd3b8353SUladzislau Rezki (Sony) 
413996e2db45SUladzislau Rezki (Sony) 	spin_lock(&purge_vmap_area_lock);
414096e2db45SUladzislau Rezki (Sony) 	list_for_each_entry(va, &purge_vmap_area_list, list) {
4141dd3b8353SUladzislau Rezki (Sony) 		seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n",
4142dd3b8353SUladzislau Rezki (Sony) 			(void *)va->va_start, (void *)va->va_end,
4143dd3b8353SUladzislau Rezki (Sony) 			va->va_end - va->va_start);
4144dd3b8353SUladzislau Rezki (Sony) 	}
414596e2db45SUladzislau Rezki (Sony) 	spin_unlock(&purge_vmap_area_lock);
4146dd3b8353SUladzislau Rezki (Sony) }
4147dd3b8353SUladzislau Rezki (Sony) 
4148a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p)
4149a10aa579SChristoph Lameter {
41503f500069Szijun_hu 	struct vmap_area *va;
4151d4033afdSJoonsoo Kim 	struct vm_struct *v;
4152d4033afdSJoonsoo Kim 
41533f500069Szijun_hu 	va = list_entry(p, struct vmap_area, list);
41543f500069Szijun_hu 
4155c2ce8c14SWanpeng Li 	/*
4156688fcbfcSPengfei Li 	 * s_show can encounter race with remove_vm_area, !vm on behalf
4157688fcbfcSPengfei Li 	 * of vmap area is being tear down or vm_map_ram allocation.
4158c2ce8c14SWanpeng Li 	 */
4159688fcbfcSPengfei Li 	if (!va->vm) {
4160dd3b8353SUladzislau Rezki (Sony) 		seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n",
416178c72746SYisheng Xie 			(void *)va->va_start, (void *)va->va_end,
4162dd3b8353SUladzislau Rezki (Sony) 			va->va_end - va->va_start);
416378c72746SYisheng Xie 
41647cc7913eSEric Dumazet 		goto final;
416578c72746SYisheng Xie 	}
4166d4033afdSJoonsoo Kim 
4167d4033afdSJoonsoo Kim 	v = va->vm;
4168a10aa579SChristoph Lameter 
416945ec1690SKees Cook 	seq_printf(m, "0x%pK-0x%pK %7ld",
4170a10aa579SChristoph Lameter 		v->addr, v->addr + v->size, v->size);
4171a10aa579SChristoph Lameter 
417262c70bceSJoe Perches 	if (v->caller)
417362c70bceSJoe Perches 		seq_printf(m, " %pS", v->caller);
417423016969SChristoph Lameter 
4175a10aa579SChristoph Lameter 	if (v->nr_pages)
4176a10aa579SChristoph Lameter 		seq_printf(m, " pages=%d", v->nr_pages);
4177a10aa579SChristoph Lameter 
4178a10aa579SChristoph Lameter 	if (v->phys_addr)
4179199eaa05SMiles Chen 		seq_printf(m, " phys=%pa", &v->phys_addr);
4180a10aa579SChristoph Lameter 
4181a10aa579SChristoph Lameter 	if (v->flags & VM_IOREMAP)
4182f4527c90SFabian Frederick 		seq_puts(m, " ioremap");
4183a10aa579SChristoph Lameter 
4184a10aa579SChristoph Lameter 	if (v->flags & VM_ALLOC)
4185f4527c90SFabian Frederick 		seq_puts(m, " vmalloc");
4186a10aa579SChristoph Lameter 
4187a10aa579SChristoph Lameter 	if (v->flags & VM_MAP)
4188f4527c90SFabian Frederick 		seq_puts(m, " vmap");
4189a10aa579SChristoph Lameter 
4190a10aa579SChristoph Lameter 	if (v->flags & VM_USERMAP)
4191f4527c90SFabian Frederick 		seq_puts(m, " user");
4192a10aa579SChristoph Lameter 
4193fe9041c2SChristoph Hellwig 	if (v->flags & VM_DMA_COHERENT)
4194fe9041c2SChristoph Hellwig 		seq_puts(m, " dma-coherent");
4195fe9041c2SChristoph Hellwig 
4196244d63eeSDavid Rientjes 	if (is_vmalloc_addr(v->pages))
4197f4527c90SFabian Frederick 		seq_puts(m, " vpages");
4198a10aa579SChristoph Lameter 
4199a47a126aSEric Dumazet 	show_numa_info(m, v);
4200a10aa579SChristoph Lameter 	seq_putc(m, '\n');
4201dd3b8353SUladzislau Rezki (Sony) 
4202dd3b8353SUladzislau Rezki (Sony) 	/*
420396e2db45SUladzislau Rezki (Sony) 	 * As a final step, dump "unpurged" areas.
4204dd3b8353SUladzislau Rezki (Sony) 	 */
42057cc7913eSEric Dumazet final:
4206dd3b8353SUladzislau Rezki (Sony) 	if (list_is_last(&va->list, &vmap_area_list))
4207dd3b8353SUladzislau Rezki (Sony) 		show_purge_info(m);
4208dd3b8353SUladzislau Rezki (Sony) 
4209a10aa579SChristoph Lameter 	return 0;
4210a10aa579SChristoph Lameter }
4211a10aa579SChristoph Lameter 
42125f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = {
4213a10aa579SChristoph Lameter 	.start = s_start,
4214a10aa579SChristoph Lameter 	.next = s_next,
4215a10aa579SChristoph Lameter 	.stop = s_stop,
4216a10aa579SChristoph Lameter 	.show = s_show,
4217a10aa579SChristoph Lameter };
42185f6a6a9cSAlexey Dobriyan 
42195f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void)
42205f6a6a9cSAlexey Dobriyan {
4221fddda2b7SChristoph Hellwig 	if (IS_ENABLED(CONFIG_NUMA))
42220825a6f9SJoe Perches 		proc_create_seq_private("vmallocinfo", 0400, NULL,
422344414d82SChristoph Hellwig 				&vmalloc_op,
422444414d82SChristoph Hellwig 				nr_node_ids * sizeof(unsigned int), NULL);
4225fddda2b7SChristoph Hellwig 	else
42260825a6f9SJoe Perches 		proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
42275f6a6a9cSAlexey Dobriyan 	return 0;
42285f6a6a9cSAlexey Dobriyan }
42295f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init);
4230db3808c1SJoonsoo Kim 
4231a10aa579SChristoph Lameter #endif
4232208162f4SChristoph Hellwig 
4233208162f4SChristoph Hellwig void __init vmalloc_init(void)
4234208162f4SChristoph Hellwig {
4235208162f4SChristoph Hellwig 	struct vmap_area *va;
4236208162f4SChristoph Hellwig 	struct vm_struct *tmp;
4237208162f4SChristoph Hellwig 	int i;
4238208162f4SChristoph Hellwig 
4239208162f4SChristoph Hellwig 	/*
4240208162f4SChristoph Hellwig 	 * Create the cache for vmap_area objects.
4241208162f4SChristoph Hellwig 	 */
4242208162f4SChristoph Hellwig 	vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
4243208162f4SChristoph Hellwig 
4244208162f4SChristoph Hellwig 	for_each_possible_cpu(i) {
4245208162f4SChristoph Hellwig 		struct vmap_block_queue *vbq;
4246208162f4SChristoph Hellwig 		struct vfree_deferred *p;
4247208162f4SChristoph Hellwig 
4248208162f4SChristoph Hellwig 		vbq = &per_cpu(vmap_block_queue, i);
4249208162f4SChristoph Hellwig 		spin_lock_init(&vbq->lock);
4250208162f4SChristoph Hellwig 		INIT_LIST_HEAD(&vbq->free);
4251208162f4SChristoph Hellwig 		p = &per_cpu(vfree_deferred, i);
4252208162f4SChristoph Hellwig 		init_llist_head(&p->list);
4253208162f4SChristoph Hellwig 		INIT_WORK(&p->wq, delayed_vfree_work);
4254208162f4SChristoph Hellwig 	}
4255208162f4SChristoph Hellwig 
4256208162f4SChristoph Hellwig 	/* Import existing vmlist entries. */
4257208162f4SChristoph Hellwig 	for (tmp = vmlist; tmp; tmp = tmp->next) {
4258208162f4SChristoph Hellwig 		va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
4259208162f4SChristoph Hellwig 		if (WARN_ON_ONCE(!va))
4260208162f4SChristoph Hellwig 			continue;
4261208162f4SChristoph Hellwig 
4262208162f4SChristoph Hellwig 		va->va_start = (unsigned long)tmp->addr;
4263208162f4SChristoph Hellwig 		va->va_end = va->va_start + tmp->size;
4264208162f4SChristoph Hellwig 		va->vm = tmp;
4265208162f4SChristoph Hellwig 		insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
4266208162f4SChristoph Hellwig 	}
4267208162f4SChristoph Hellwig 
4268208162f4SChristoph Hellwig 	/*
4269208162f4SChristoph Hellwig 	 * Now we can initialize a free vmap space.
4270208162f4SChristoph Hellwig 	 */
4271208162f4SChristoph Hellwig 	vmap_init_free_space();
4272208162f4SChristoph Hellwig 	vmap_initialized = true;
4273208162f4SChristoph Hellwig }
4274