xref: /openbmc/linux/mm/vmalloc.c (revision 9376130c390a76fac2788a5d6e1a149017b4ab50)
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 
46dd56b046SMel Gorman #include "internal.h"
472a681cfaSJoerg Roedel #include "pgalloc-track.h"
48dd56b046SMel Gorman 
4982a70ce0SChristoph Hellwig #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
5082a70ce0SChristoph Hellwig static unsigned int __ro_after_init ioremap_max_page_shift = BITS_PER_LONG - 1;
5182a70ce0SChristoph Hellwig 
5282a70ce0SChristoph Hellwig static int __init set_nohugeiomap(char *str)
5382a70ce0SChristoph Hellwig {
5482a70ce0SChristoph Hellwig 	ioremap_max_page_shift = PAGE_SHIFT;
5582a70ce0SChristoph Hellwig 	return 0;
5682a70ce0SChristoph Hellwig }
5782a70ce0SChristoph Hellwig early_param("nohugeiomap", set_nohugeiomap);
5882a70ce0SChristoph Hellwig #else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
5982a70ce0SChristoph Hellwig static const unsigned int ioremap_max_page_shift = PAGE_SHIFT;
6082a70ce0SChristoph Hellwig #endif	/* CONFIG_HAVE_ARCH_HUGE_VMAP */
6182a70ce0SChristoph Hellwig 
62121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
63121e6f32SNicholas Piggin static bool __ro_after_init vmap_allow_huge = true;
64121e6f32SNicholas Piggin 
65121e6f32SNicholas Piggin static int __init set_nohugevmalloc(char *str)
66121e6f32SNicholas Piggin {
67121e6f32SNicholas Piggin 	vmap_allow_huge = false;
68121e6f32SNicholas Piggin 	return 0;
69121e6f32SNicholas Piggin }
70121e6f32SNicholas Piggin early_param("nohugevmalloc", set_nohugevmalloc);
71121e6f32SNicholas Piggin #else /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
72121e6f32SNicholas Piggin static const bool vmap_allow_huge = false;
73121e6f32SNicholas Piggin #endif	/* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
74121e6f32SNicholas Piggin 
75186525bdSIngo Molnar bool is_vmalloc_addr(const void *x)
76186525bdSIngo Molnar {
77186525bdSIngo Molnar 	unsigned long addr = (unsigned long)x;
78186525bdSIngo Molnar 
79186525bdSIngo Molnar 	return addr >= VMALLOC_START && addr < VMALLOC_END;
80186525bdSIngo Molnar }
81186525bdSIngo Molnar EXPORT_SYMBOL(is_vmalloc_addr);
82186525bdSIngo Molnar 
8332fcfd40SAl Viro struct vfree_deferred {
8432fcfd40SAl Viro 	struct llist_head list;
8532fcfd40SAl Viro 	struct work_struct wq;
8632fcfd40SAl Viro };
8732fcfd40SAl Viro static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
8832fcfd40SAl Viro 
8932fcfd40SAl Viro static void __vunmap(const void *, int);
9032fcfd40SAl Viro 
9132fcfd40SAl Viro static void free_work(struct work_struct *w)
9232fcfd40SAl Viro {
9332fcfd40SAl Viro 	struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
94894e58c1SByungchul Park 	struct llist_node *t, *llnode;
95894e58c1SByungchul Park 
96894e58c1SByungchul Park 	llist_for_each_safe(llnode, t, llist_del_all(&p->list))
97894e58c1SByungchul Park 		__vunmap((void *)llnode, 1);
9832fcfd40SAl Viro }
9932fcfd40SAl Viro 
100db64fe02SNick Piggin /*** Page table manipulation functions ***/
1015e9e3d77SNicholas Piggin static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
1025e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
103f7ee1f13SChristophe Leroy 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
1045e9e3d77SNicholas Piggin {
1055e9e3d77SNicholas Piggin 	pte_t *pte;
1065e9e3d77SNicholas Piggin 	u64 pfn;
107f7ee1f13SChristophe Leroy 	unsigned long size = PAGE_SIZE;
1085e9e3d77SNicholas Piggin 
1095e9e3d77SNicholas Piggin 	pfn = phys_addr >> PAGE_SHIFT;
1105e9e3d77SNicholas Piggin 	pte = pte_alloc_kernel_track(pmd, addr, mask);
1115e9e3d77SNicholas Piggin 	if (!pte)
1125e9e3d77SNicholas Piggin 		return -ENOMEM;
1135e9e3d77SNicholas Piggin 	do {
1145e9e3d77SNicholas Piggin 		BUG_ON(!pte_none(*pte));
115f7ee1f13SChristophe Leroy 
116f7ee1f13SChristophe Leroy #ifdef CONFIG_HUGETLB_PAGE
117f7ee1f13SChristophe Leroy 		size = arch_vmap_pte_range_map_size(addr, end, pfn, max_page_shift);
118f7ee1f13SChristophe Leroy 		if (size != PAGE_SIZE) {
119f7ee1f13SChristophe Leroy 			pte_t entry = pfn_pte(pfn, prot);
120f7ee1f13SChristophe Leroy 
121f7ee1f13SChristophe Leroy 			entry = pte_mkhuge(entry);
122f7ee1f13SChristophe Leroy 			entry = arch_make_huge_pte(entry, ilog2(size), 0);
123f7ee1f13SChristophe Leroy 			set_huge_pte_at(&init_mm, addr, pte, entry);
124f7ee1f13SChristophe Leroy 			pfn += PFN_DOWN(size);
125f7ee1f13SChristophe Leroy 			continue;
126f7ee1f13SChristophe Leroy 		}
127f7ee1f13SChristophe Leroy #endif
1285e9e3d77SNicholas Piggin 		set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
1295e9e3d77SNicholas Piggin 		pfn++;
130f7ee1f13SChristophe Leroy 	} while (pte += PFN_DOWN(size), addr += size, addr != end);
1315e9e3d77SNicholas Piggin 	*mask |= PGTBL_PTE_MODIFIED;
1325e9e3d77SNicholas Piggin 	return 0;
1335e9e3d77SNicholas Piggin }
1345e9e3d77SNicholas Piggin 
1355e9e3d77SNicholas Piggin static int vmap_try_huge_pmd(pmd_t *pmd, unsigned long addr, unsigned long end,
1365e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
1375e9e3d77SNicholas Piggin 			unsigned int max_page_shift)
1385e9e3d77SNicholas Piggin {
1395e9e3d77SNicholas Piggin 	if (max_page_shift < PMD_SHIFT)
1405e9e3d77SNicholas Piggin 		return 0;
1415e9e3d77SNicholas Piggin 
1425e9e3d77SNicholas Piggin 	if (!arch_vmap_pmd_supported(prot))
1435e9e3d77SNicholas Piggin 		return 0;
1445e9e3d77SNicholas Piggin 
1455e9e3d77SNicholas Piggin 	if ((end - addr) != PMD_SIZE)
1465e9e3d77SNicholas Piggin 		return 0;
1475e9e3d77SNicholas Piggin 
1485e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(addr, PMD_SIZE))
1495e9e3d77SNicholas Piggin 		return 0;
1505e9e3d77SNicholas Piggin 
1515e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(phys_addr, PMD_SIZE))
1525e9e3d77SNicholas Piggin 		return 0;
1535e9e3d77SNicholas Piggin 
1545e9e3d77SNicholas Piggin 	if (pmd_present(*pmd) && !pmd_free_pte_page(pmd, addr))
1555e9e3d77SNicholas Piggin 		return 0;
1565e9e3d77SNicholas Piggin 
1575e9e3d77SNicholas Piggin 	return pmd_set_huge(pmd, phys_addr, prot);
1585e9e3d77SNicholas Piggin }
1595e9e3d77SNicholas Piggin 
1605e9e3d77SNicholas Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
1615e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
1625e9e3d77SNicholas Piggin 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
1635e9e3d77SNicholas Piggin {
1645e9e3d77SNicholas Piggin 	pmd_t *pmd;
1655e9e3d77SNicholas Piggin 	unsigned long next;
1665e9e3d77SNicholas Piggin 
1675e9e3d77SNicholas Piggin 	pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
1685e9e3d77SNicholas Piggin 	if (!pmd)
1695e9e3d77SNicholas Piggin 		return -ENOMEM;
1705e9e3d77SNicholas Piggin 	do {
1715e9e3d77SNicholas Piggin 		next = pmd_addr_end(addr, end);
1725e9e3d77SNicholas Piggin 
1735e9e3d77SNicholas Piggin 		if (vmap_try_huge_pmd(pmd, addr, next, phys_addr, prot,
1745e9e3d77SNicholas Piggin 					max_page_shift)) {
1755e9e3d77SNicholas Piggin 			*mask |= PGTBL_PMD_MODIFIED;
1765e9e3d77SNicholas Piggin 			continue;
1775e9e3d77SNicholas Piggin 		}
1785e9e3d77SNicholas Piggin 
179f7ee1f13SChristophe Leroy 		if (vmap_pte_range(pmd, addr, next, phys_addr, prot, max_page_shift, mask))
1805e9e3d77SNicholas Piggin 			return -ENOMEM;
1815e9e3d77SNicholas Piggin 	} while (pmd++, phys_addr += (next - addr), addr = next, addr != end);
1825e9e3d77SNicholas Piggin 	return 0;
1835e9e3d77SNicholas Piggin }
1845e9e3d77SNicholas Piggin 
1855e9e3d77SNicholas Piggin static int vmap_try_huge_pud(pud_t *pud, unsigned long addr, unsigned long end,
1865e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
1875e9e3d77SNicholas Piggin 			unsigned int max_page_shift)
1885e9e3d77SNicholas Piggin {
1895e9e3d77SNicholas Piggin 	if (max_page_shift < PUD_SHIFT)
1905e9e3d77SNicholas Piggin 		return 0;
1915e9e3d77SNicholas Piggin 
1925e9e3d77SNicholas Piggin 	if (!arch_vmap_pud_supported(prot))
1935e9e3d77SNicholas Piggin 		return 0;
1945e9e3d77SNicholas Piggin 
1955e9e3d77SNicholas Piggin 	if ((end - addr) != PUD_SIZE)
1965e9e3d77SNicholas Piggin 		return 0;
1975e9e3d77SNicholas Piggin 
1985e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(addr, PUD_SIZE))
1995e9e3d77SNicholas Piggin 		return 0;
2005e9e3d77SNicholas Piggin 
2015e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(phys_addr, PUD_SIZE))
2025e9e3d77SNicholas Piggin 		return 0;
2035e9e3d77SNicholas Piggin 
2045e9e3d77SNicholas Piggin 	if (pud_present(*pud) && !pud_free_pmd_page(pud, addr))
2055e9e3d77SNicholas Piggin 		return 0;
2065e9e3d77SNicholas Piggin 
2075e9e3d77SNicholas Piggin 	return pud_set_huge(pud, phys_addr, prot);
2085e9e3d77SNicholas Piggin }
2095e9e3d77SNicholas Piggin 
2105e9e3d77SNicholas Piggin static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
2115e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
2125e9e3d77SNicholas Piggin 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
2135e9e3d77SNicholas Piggin {
2145e9e3d77SNicholas Piggin 	pud_t *pud;
2155e9e3d77SNicholas Piggin 	unsigned long next;
2165e9e3d77SNicholas Piggin 
2175e9e3d77SNicholas Piggin 	pud = pud_alloc_track(&init_mm, p4d, addr, mask);
2185e9e3d77SNicholas Piggin 	if (!pud)
2195e9e3d77SNicholas Piggin 		return -ENOMEM;
2205e9e3d77SNicholas Piggin 	do {
2215e9e3d77SNicholas Piggin 		next = pud_addr_end(addr, end);
2225e9e3d77SNicholas Piggin 
2235e9e3d77SNicholas Piggin 		if (vmap_try_huge_pud(pud, addr, next, phys_addr, prot,
2245e9e3d77SNicholas Piggin 					max_page_shift)) {
2255e9e3d77SNicholas Piggin 			*mask |= PGTBL_PUD_MODIFIED;
2265e9e3d77SNicholas Piggin 			continue;
2275e9e3d77SNicholas Piggin 		}
2285e9e3d77SNicholas Piggin 
2295e9e3d77SNicholas Piggin 		if (vmap_pmd_range(pud, addr, next, phys_addr, prot,
2305e9e3d77SNicholas Piggin 					max_page_shift, mask))
2315e9e3d77SNicholas Piggin 			return -ENOMEM;
2325e9e3d77SNicholas Piggin 	} while (pud++, phys_addr += (next - addr), addr = next, addr != end);
2335e9e3d77SNicholas Piggin 	return 0;
2345e9e3d77SNicholas Piggin }
2355e9e3d77SNicholas Piggin 
2365e9e3d77SNicholas Piggin static int vmap_try_huge_p4d(p4d_t *p4d, unsigned long addr, unsigned long end,
2375e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
2385e9e3d77SNicholas Piggin 			unsigned int max_page_shift)
2395e9e3d77SNicholas Piggin {
2405e9e3d77SNicholas Piggin 	if (max_page_shift < P4D_SHIFT)
2415e9e3d77SNicholas Piggin 		return 0;
2425e9e3d77SNicholas Piggin 
2435e9e3d77SNicholas Piggin 	if (!arch_vmap_p4d_supported(prot))
2445e9e3d77SNicholas Piggin 		return 0;
2455e9e3d77SNicholas Piggin 
2465e9e3d77SNicholas Piggin 	if ((end - addr) != P4D_SIZE)
2475e9e3d77SNicholas Piggin 		return 0;
2485e9e3d77SNicholas Piggin 
2495e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(addr, P4D_SIZE))
2505e9e3d77SNicholas Piggin 		return 0;
2515e9e3d77SNicholas Piggin 
2525e9e3d77SNicholas Piggin 	if (!IS_ALIGNED(phys_addr, P4D_SIZE))
2535e9e3d77SNicholas Piggin 		return 0;
2545e9e3d77SNicholas Piggin 
2555e9e3d77SNicholas Piggin 	if (p4d_present(*p4d) && !p4d_free_pud_page(p4d, addr))
2565e9e3d77SNicholas Piggin 		return 0;
2575e9e3d77SNicholas Piggin 
2585e9e3d77SNicholas Piggin 	return p4d_set_huge(p4d, phys_addr, prot);
2595e9e3d77SNicholas Piggin }
2605e9e3d77SNicholas Piggin 
2615e9e3d77SNicholas Piggin static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
2625e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
2635e9e3d77SNicholas Piggin 			unsigned int max_page_shift, pgtbl_mod_mask *mask)
2645e9e3d77SNicholas Piggin {
2655e9e3d77SNicholas Piggin 	p4d_t *p4d;
2665e9e3d77SNicholas Piggin 	unsigned long next;
2675e9e3d77SNicholas Piggin 
2685e9e3d77SNicholas Piggin 	p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
2695e9e3d77SNicholas Piggin 	if (!p4d)
2705e9e3d77SNicholas Piggin 		return -ENOMEM;
2715e9e3d77SNicholas Piggin 	do {
2725e9e3d77SNicholas Piggin 		next = p4d_addr_end(addr, end);
2735e9e3d77SNicholas Piggin 
2745e9e3d77SNicholas Piggin 		if (vmap_try_huge_p4d(p4d, addr, next, phys_addr, prot,
2755e9e3d77SNicholas Piggin 					max_page_shift)) {
2765e9e3d77SNicholas Piggin 			*mask |= PGTBL_P4D_MODIFIED;
2775e9e3d77SNicholas Piggin 			continue;
2785e9e3d77SNicholas Piggin 		}
2795e9e3d77SNicholas Piggin 
2805e9e3d77SNicholas Piggin 		if (vmap_pud_range(p4d, addr, next, phys_addr, prot,
2815e9e3d77SNicholas Piggin 					max_page_shift, mask))
2825e9e3d77SNicholas Piggin 			return -ENOMEM;
2835e9e3d77SNicholas Piggin 	} while (p4d++, phys_addr += (next - addr), addr = next, addr != end);
2845e9e3d77SNicholas Piggin 	return 0;
2855e9e3d77SNicholas Piggin }
2865e9e3d77SNicholas Piggin 
2875d87510dSNicholas Piggin static int vmap_range_noflush(unsigned long addr, unsigned long end,
2885e9e3d77SNicholas Piggin 			phys_addr_t phys_addr, pgprot_t prot,
2895e9e3d77SNicholas Piggin 			unsigned int max_page_shift)
2905e9e3d77SNicholas Piggin {
2915e9e3d77SNicholas Piggin 	pgd_t *pgd;
2925e9e3d77SNicholas Piggin 	unsigned long start;
2935e9e3d77SNicholas Piggin 	unsigned long next;
2945e9e3d77SNicholas Piggin 	int err;
2955e9e3d77SNicholas Piggin 	pgtbl_mod_mask mask = 0;
2965e9e3d77SNicholas Piggin 
2975e9e3d77SNicholas Piggin 	might_sleep();
2985e9e3d77SNicholas Piggin 	BUG_ON(addr >= end);
2995e9e3d77SNicholas Piggin 
3005e9e3d77SNicholas Piggin 	start = addr;
3015e9e3d77SNicholas Piggin 	pgd = pgd_offset_k(addr);
3025e9e3d77SNicholas Piggin 	do {
3035e9e3d77SNicholas Piggin 		next = pgd_addr_end(addr, end);
3045e9e3d77SNicholas Piggin 		err = vmap_p4d_range(pgd, addr, next, phys_addr, prot,
3055e9e3d77SNicholas Piggin 					max_page_shift, &mask);
3065e9e3d77SNicholas Piggin 		if (err)
3075e9e3d77SNicholas Piggin 			break;
3085e9e3d77SNicholas Piggin 	} while (pgd++, phys_addr += (next - addr), addr = next, addr != end);
3095e9e3d77SNicholas Piggin 
3105e9e3d77SNicholas Piggin 	if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
3115e9e3d77SNicholas Piggin 		arch_sync_kernel_mappings(start, end);
3125e9e3d77SNicholas Piggin 
3135e9e3d77SNicholas Piggin 	return err;
3145e9e3d77SNicholas Piggin }
315b221385bSAdrian Bunk 
31682a70ce0SChristoph Hellwig int ioremap_page_range(unsigned long addr, unsigned long end,
31782a70ce0SChristoph Hellwig 		phys_addr_t phys_addr, pgprot_t prot)
3185d87510dSNicholas Piggin {
3195d87510dSNicholas Piggin 	int err;
3205d87510dSNicholas Piggin 
3218491502fSChristoph Hellwig 	err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot),
32282a70ce0SChristoph Hellwig 				 ioremap_max_page_shift);
3235d87510dSNicholas Piggin 	flush_cache_vmap(addr, end);
3245d87510dSNicholas Piggin 	return err;
3255d87510dSNicholas Piggin }
3265d87510dSNicholas Piggin 
3272ba3e694SJoerg Roedel static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
3282ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
3291da177e4SLinus Torvalds {
3301da177e4SLinus Torvalds 	pte_t *pte;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	pte = pte_offset_kernel(pmd, addr);
3331da177e4SLinus Torvalds 	do {
3341da177e4SLinus Torvalds 		pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
3351da177e4SLinus Torvalds 		WARN_ON(!pte_none(ptent) && !pte_present(ptent));
3361da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
3372ba3e694SJoerg Roedel 	*mask |= PGTBL_PTE_MODIFIED;
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3402ba3e694SJoerg Roedel static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
3412ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	pmd_t *pmd;
3441da177e4SLinus Torvalds 	unsigned long next;
3452ba3e694SJoerg Roedel 	int cleared;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
3481da177e4SLinus Torvalds 	do {
3491da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
3502ba3e694SJoerg Roedel 
3512ba3e694SJoerg Roedel 		cleared = pmd_clear_huge(pmd);
3522ba3e694SJoerg Roedel 		if (cleared || pmd_bad(*pmd))
3532ba3e694SJoerg Roedel 			*mask |= PGTBL_PMD_MODIFIED;
3542ba3e694SJoerg Roedel 
3552ba3e694SJoerg Roedel 		if (cleared)
356b9820d8fSToshi Kani 			continue;
3571da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
3581da177e4SLinus Torvalds 			continue;
3592ba3e694SJoerg Roedel 		vunmap_pte_range(pmd, addr, next, mask);
360e47110e9SAneesh Kumar K.V 
361e47110e9SAneesh Kumar K.V 		cond_resched();
3621da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
3652ba3e694SJoerg Roedel static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
3662ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	pud_t *pud;
3691da177e4SLinus Torvalds 	unsigned long next;
3702ba3e694SJoerg Roedel 	int cleared;
3711da177e4SLinus Torvalds 
372c2febafcSKirill A. Shutemov 	pud = pud_offset(p4d, addr);
3731da177e4SLinus Torvalds 	do {
3741da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
3752ba3e694SJoerg Roedel 
3762ba3e694SJoerg Roedel 		cleared = pud_clear_huge(pud);
3772ba3e694SJoerg Roedel 		if (cleared || pud_bad(*pud))
3782ba3e694SJoerg Roedel 			*mask |= PGTBL_PUD_MODIFIED;
3792ba3e694SJoerg Roedel 
3802ba3e694SJoerg Roedel 		if (cleared)
381b9820d8fSToshi Kani 			continue;
3821da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
3831da177e4SLinus Torvalds 			continue;
3842ba3e694SJoerg Roedel 		vunmap_pmd_range(pud, addr, next, mask);
3851da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3882ba3e694SJoerg Roedel static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
3892ba3e694SJoerg Roedel 			     pgtbl_mod_mask *mask)
390c2febafcSKirill A. Shutemov {
391c2febafcSKirill A. Shutemov 	p4d_t *p4d;
392c2febafcSKirill A. Shutemov 	unsigned long next;
3932ba3e694SJoerg Roedel 	int cleared;
394c2febafcSKirill A. Shutemov 
395c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
396c2febafcSKirill A. Shutemov 	do {
397c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
3982ba3e694SJoerg Roedel 
3992ba3e694SJoerg Roedel 		cleared = p4d_clear_huge(p4d);
4002ba3e694SJoerg Roedel 		if (cleared || p4d_bad(*p4d))
4012ba3e694SJoerg Roedel 			*mask |= PGTBL_P4D_MODIFIED;
4022ba3e694SJoerg Roedel 
4032ba3e694SJoerg Roedel 		if (cleared)
404c2febafcSKirill A. Shutemov 			continue;
405c2febafcSKirill A. Shutemov 		if (p4d_none_or_clear_bad(p4d))
406c2febafcSKirill A. Shutemov 			continue;
4072ba3e694SJoerg Roedel 		vunmap_pud_range(p4d, addr, next, mask);
408c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
409c2febafcSKirill A. Shutemov }
410c2febafcSKirill A. Shutemov 
4114ad0ae8cSNicholas Piggin /*
4124ad0ae8cSNicholas Piggin  * vunmap_range_noflush is similar to vunmap_range, but does not
4134ad0ae8cSNicholas Piggin  * flush caches or TLBs.
414b521c43fSChristoph Hellwig  *
4154ad0ae8cSNicholas Piggin  * The caller is responsible for calling flush_cache_vmap() before calling
4164ad0ae8cSNicholas Piggin  * this function, and flush_tlb_kernel_range after it has returned
4174ad0ae8cSNicholas Piggin  * successfully (and before the addresses are expected to cause a page fault
4184ad0ae8cSNicholas Piggin  * or be re-mapped for something else, if TLB flushes are being delayed or
4194ad0ae8cSNicholas Piggin  * coalesced).
420b521c43fSChristoph Hellwig  *
4214ad0ae8cSNicholas Piggin  * This is an internal function only. Do not use outside mm/.
422b521c43fSChristoph Hellwig  */
4234ad0ae8cSNicholas Piggin void vunmap_range_noflush(unsigned long start, unsigned long end)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	unsigned long next;
426b521c43fSChristoph Hellwig 	pgd_t *pgd;
4272ba3e694SJoerg Roedel 	unsigned long addr = start;
4282ba3e694SJoerg Roedel 	pgtbl_mod_mask mask = 0;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	BUG_ON(addr >= end);
4311da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
4321da177e4SLinus Torvalds 	do {
4331da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
4342ba3e694SJoerg Roedel 		if (pgd_bad(*pgd))
4352ba3e694SJoerg Roedel 			mask |= PGTBL_PGD_MODIFIED;
4361da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
4371da177e4SLinus Torvalds 			continue;
4382ba3e694SJoerg Roedel 		vunmap_p4d_range(pgd, addr, next, &mask);
4391da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
4402ba3e694SJoerg Roedel 
4412ba3e694SJoerg Roedel 	if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
4422ba3e694SJoerg Roedel 		arch_sync_kernel_mappings(start, end);
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
4454ad0ae8cSNicholas Piggin /**
4464ad0ae8cSNicholas Piggin  * vunmap_range - unmap kernel virtual addresses
4474ad0ae8cSNicholas Piggin  * @addr: start of the VM area to unmap
4484ad0ae8cSNicholas Piggin  * @end: end of the VM area to unmap (non-inclusive)
4494ad0ae8cSNicholas Piggin  *
4504ad0ae8cSNicholas Piggin  * Clears any present PTEs in the virtual address range, flushes TLBs and
4514ad0ae8cSNicholas Piggin  * caches. Any subsequent access to the address before it has been re-mapped
4524ad0ae8cSNicholas Piggin  * is a kernel bug.
4534ad0ae8cSNicholas Piggin  */
4544ad0ae8cSNicholas Piggin void vunmap_range(unsigned long addr, unsigned long end)
4554ad0ae8cSNicholas Piggin {
4564ad0ae8cSNicholas Piggin 	flush_cache_vunmap(addr, end);
4574ad0ae8cSNicholas Piggin 	vunmap_range_noflush(addr, end);
4584ad0ae8cSNicholas Piggin 	flush_tlb_kernel_range(addr, end);
4594ad0ae8cSNicholas Piggin }
4604ad0ae8cSNicholas Piggin 
4610a264884SNicholas Piggin static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr,
4622ba3e694SJoerg Roedel 		unsigned long end, pgprot_t prot, struct page **pages, int *nr,
4632ba3e694SJoerg Roedel 		pgtbl_mod_mask *mask)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	pte_t *pte;
4661da177e4SLinus Torvalds 
467db64fe02SNick Piggin 	/*
468db64fe02SNick Piggin 	 * nr is a running index into the array which helps higher level
469db64fe02SNick Piggin 	 * callers keep track of where we're up to.
470db64fe02SNick Piggin 	 */
471db64fe02SNick Piggin 
4722ba3e694SJoerg Roedel 	pte = pte_alloc_kernel_track(pmd, addr, mask);
4731da177e4SLinus Torvalds 	if (!pte)
4741da177e4SLinus Torvalds 		return -ENOMEM;
4751da177e4SLinus Torvalds 	do {
476db64fe02SNick Piggin 		struct page *page = pages[*nr];
477db64fe02SNick Piggin 
478db64fe02SNick Piggin 		if (WARN_ON(!pte_none(*pte)))
479db64fe02SNick Piggin 			return -EBUSY;
480db64fe02SNick Piggin 		if (WARN_ON(!page))
4811da177e4SLinus Torvalds 			return -ENOMEM;
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  */
579b67177ecSNicholas Piggin 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),
594121e6f32SNicholas Piggin 					__pa(page_address(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 
605b67177ecSNicholas Piggin /**
606b67177ecSNicholas Piggin  * vmap_pages_range - map pages to a kernel virtual address
607b67177ecSNicholas Piggin  * @addr: start of the VM area to map
608b67177ecSNicholas Piggin  * @end: end of the VM area to map (non-inclusive)
609b67177ecSNicholas Piggin  * @prot: page protection flags to use
610b67177ecSNicholas Piggin  * @pages: pages to map (always PAGE_SIZE pages)
611b67177ecSNicholas Piggin  * @page_shift: maximum shift that the pages may be mapped with, @pages must
612b67177ecSNicholas Piggin  * be aligned and contiguous up to at least this shift.
613b67177ecSNicholas Piggin  *
614b67177ecSNicholas Piggin  * RETURNS:
615b67177ecSNicholas Piggin  * 0 on success, -errno on failure.
616b67177ecSNicholas Piggin  */
617121e6f32SNicholas Piggin static int vmap_pages_range(unsigned long addr, unsigned long end,
618121e6f32SNicholas Piggin 		pgprot_t prot, struct page **pages, unsigned int page_shift)
619121e6f32SNicholas Piggin {
620121e6f32SNicholas Piggin 	int err;
621121e6f32SNicholas Piggin 
622121e6f32SNicholas Piggin 	err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
623121e6f32SNicholas Piggin 	flush_cache_vmap(addr, end);
624121e6f32SNicholas Piggin 	return err;
625121e6f32SNicholas Piggin }
626121e6f32SNicholas Piggin 
62781ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x)
62873bdf0a6SLinus Torvalds {
62973bdf0a6SLinus Torvalds 	/*
630ab4f2ee1SRussell King 	 * ARM, x86-64 and sparc64 put modules in a special place,
63173bdf0a6SLinus Torvalds 	 * and fall back on vmalloc() if that fails. Others
63273bdf0a6SLinus Torvalds 	 * just put it in the vmalloc space.
63373bdf0a6SLinus Torvalds 	 */
63473bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
63573bdf0a6SLinus Torvalds 	unsigned long addr = (unsigned long)x;
63673bdf0a6SLinus Torvalds 	if (addr >= MODULES_VADDR && addr < MODULES_END)
63773bdf0a6SLinus Torvalds 		return 1;
63873bdf0a6SLinus Torvalds #endif
63973bdf0a6SLinus Torvalds 	return is_vmalloc_addr(x);
64073bdf0a6SLinus Torvalds }
64173bdf0a6SLinus Torvalds 
64248667e7aSChristoph Lameter /*
643c0eb315aSNicholas Piggin  * Walk a vmap address to the struct page it maps. Huge vmap mappings will
644c0eb315aSNicholas Piggin  * return the tail page that corresponds to the base page address, which
645c0eb315aSNicholas Piggin  * matches small vmap mappings.
64648667e7aSChristoph Lameter  */
647add688fbSmalc struct page *vmalloc_to_page(const void *vmalloc_addr)
64848667e7aSChristoph Lameter {
64948667e7aSChristoph Lameter 	unsigned long addr = (unsigned long) vmalloc_addr;
650add688fbSmalc 	struct page *page = NULL;
65148667e7aSChristoph Lameter 	pgd_t *pgd = pgd_offset_k(addr);
652c2febafcSKirill A. Shutemov 	p4d_t *p4d;
653c2febafcSKirill A. Shutemov 	pud_t *pud;
654c2febafcSKirill A. Shutemov 	pmd_t *pmd;
655c2febafcSKirill A. Shutemov 	pte_t *ptep, pte;
65648667e7aSChristoph Lameter 
6577aa413deSIngo Molnar 	/*
6587aa413deSIngo Molnar 	 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
6597aa413deSIngo Molnar 	 * architectures that do not vmalloc module space
6607aa413deSIngo Molnar 	 */
66173bdf0a6SLinus Torvalds 	VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
66259ea7463SJiri Slaby 
663c2febafcSKirill A. Shutemov 	if (pgd_none(*pgd))
664c2febafcSKirill A. Shutemov 		return NULL;
665c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(pgd_leaf(*pgd)))
666c0eb315aSNicholas Piggin 		return NULL; /* XXX: no allowance for huge pgd */
667c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(pgd_bad(*pgd)))
668c0eb315aSNicholas Piggin 		return NULL;
669c0eb315aSNicholas Piggin 
670c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
671c2febafcSKirill A. Shutemov 	if (p4d_none(*p4d))
672c2febafcSKirill A. Shutemov 		return NULL;
673c0eb315aSNicholas Piggin 	if (p4d_leaf(*p4d))
674c0eb315aSNicholas Piggin 		return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT);
675c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(p4d_bad(*p4d)))
676c2febafcSKirill A. Shutemov 		return NULL;
677c0eb315aSNicholas Piggin 
678c0eb315aSNicholas Piggin 	pud = pud_offset(p4d, addr);
679c0eb315aSNicholas Piggin 	if (pud_none(*pud))
680c0eb315aSNicholas Piggin 		return NULL;
681c0eb315aSNicholas Piggin 	if (pud_leaf(*pud))
682c0eb315aSNicholas Piggin 		return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
683c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(pud_bad(*pud)))
684c0eb315aSNicholas Piggin 		return NULL;
685c0eb315aSNicholas Piggin 
686c2febafcSKirill A. Shutemov 	pmd = pmd_offset(pud, addr);
687c0eb315aSNicholas Piggin 	if (pmd_none(*pmd))
688c0eb315aSNicholas Piggin 		return NULL;
689c0eb315aSNicholas Piggin 	if (pmd_leaf(*pmd))
690c0eb315aSNicholas Piggin 		return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
691c0eb315aSNicholas Piggin 	if (WARN_ON_ONCE(pmd_bad(*pmd)))
692c2febafcSKirill A. Shutemov 		return NULL;
693db64fe02SNick Piggin 
69448667e7aSChristoph Lameter 	ptep = pte_offset_map(pmd, addr);
69548667e7aSChristoph Lameter 	pte = *ptep;
69648667e7aSChristoph Lameter 	if (pte_present(pte))
697add688fbSmalc 		page = pte_page(pte);
69848667e7aSChristoph Lameter 	pte_unmap(ptep);
699c0eb315aSNicholas Piggin 
700add688fbSmalc 	return page;
701ece86e22SJianyu Zhan }
702ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page);
703ece86e22SJianyu Zhan 
704add688fbSmalc /*
705add688fbSmalc  * Map a vmalloc()-space virtual address to the physical page frame number.
706add688fbSmalc  */
707add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
708add688fbSmalc {
709add688fbSmalc 	return page_to_pfn(vmalloc_to_page(vmalloc_addr));
710add688fbSmalc }
711add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn);
712add688fbSmalc 
713db64fe02SNick Piggin 
714db64fe02SNick Piggin /*** Global kva allocator ***/
715db64fe02SNick Piggin 
716bb850f4dSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_PROPAGATE_CHECK 0
717a6cf4e0fSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0
718bb850f4dSUladzislau Rezki (Sony) 
719db64fe02SNick Piggin 
720db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock);
721e36176beSUladzislau Rezki (Sony) static DEFINE_SPINLOCK(free_vmap_area_lock);
722f1c4069eSJoonsoo Kim /* Export for kexec only */
723f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
72489699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT;
72568ad4a33SUladzislau Rezki (Sony) static bool vmap_initialized __read_mostly;
72689699605SNick Piggin 
72796e2db45SUladzislau Rezki (Sony) static struct rb_root purge_vmap_area_root = RB_ROOT;
72896e2db45SUladzislau Rezki (Sony) static LIST_HEAD(purge_vmap_area_list);
72996e2db45SUladzislau Rezki (Sony) static DEFINE_SPINLOCK(purge_vmap_area_lock);
73096e2db45SUladzislau Rezki (Sony) 
73168ad4a33SUladzislau Rezki (Sony) /*
73268ad4a33SUladzislau Rezki (Sony)  * This kmem_cache is used for vmap_area objects. Instead of
73368ad4a33SUladzislau Rezki (Sony)  * allocating from slab we reuse an object from this cache to
73468ad4a33SUladzislau Rezki (Sony)  * make things faster. Especially in "no edge" splitting of
73568ad4a33SUladzislau Rezki (Sony)  * free block.
73668ad4a33SUladzislau Rezki (Sony)  */
73768ad4a33SUladzislau Rezki (Sony) static struct kmem_cache *vmap_area_cachep;
73889699605SNick Piggin 
73968ad4a33SUladzislau Rezki (Sony) /*
74068ad4a33SUladzislau Rezki (Sony)  * This linked list is used in pair with free_vmap_area_root.
74168ad4a33SUladzislau Rezki (Sony)  * It gives O(1) access to prev/next to perform fast coalescing.
74268ad4a33SUladzislau Rezki (Sony)  */
74368ad4a33SUladzislau Rezki (Sony) static LIST_HEAD(free_vmap_area_list);
74468ad4a33SUladzislau Rezki (Sony) 
74568ad4a33SUladzislau Rezki (Sony) /*
74668ad4a33SUladzislau Rezki (Sony)  * This augment red-black tree represents the free vmap space.
74768ad4a33SUladzislau Rezki (Sony)  * All vmap_area objects in this tree are sorted by va->va_start
74868ad4a33SUladzislau Rezki (Sony)  * address. It is used for allocation and merging when a vmap
74968ad4a33SUladzislau Rezki (Sony)  * object is released.
75068ad4a33SUladzislau Rezki (Sony)  *
75168ad4a33SUladzislau Rezki (Sony)  * Each vmap_area node contains a maximum available free block
75268ad4a33SUladzislau Rezki (Sony)  * of its sub-tree, right or left. Therefore it is possible to
75368ad4a33SUladzislau Rezki (Sony)  * find a lowest match of free area.
75468ad4a33SUladzislau Rezki (Sony)  */
75568ad4a33SUladzislau Rezki (Sony) static struct rb_root free_vmap_area_root = RB_ROOT;
75668ad4a33SUladzislau Rezki (Sony) 
75782dd23e8SUladzislau Rezki (Sony) /*
75882dd23e8SUladzislau Rezki (Sony)  * Preload a CPU with one object for "no edge" split case. The
75982dd23e8SUladzislau Rezki (Sony)  * aim is to get rid of allocations from the atomic context, thus
76082dd23e8SUladzislau Rezki (Sony)  * to use more permissive allocation masks.
76182dd23e8SUladzislau Rezki (Sony)  */
76282dd23e8SUladzislau Rezki (Sony) static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node);
76382dd23e8SUladzislau Rezki (Sony) 
76468ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
76568ad4a33SUladzislau Rezki (Sony) va_size(struct vmap_area *va)
76668ad4a33SUladzislau Rezki (Sony) {
76768ad4a33SUladzislau Rezki (Sony) 	return (va->va_end - va->va_start);
76868ad4a33SUladzislau Rezki (Sony) }
76968ad4a33SUladzislau Rezki (Sony) 
77068ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
77168ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(struct rb_node *node)
77268ad4a33SUladzislau Rezki (Sony) {
77368ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
77468ad4a33SUladzislau Rezki (Sony) 
77568ad4a33SUladzislau Rezki (Sony) 	va = rb_entry_safe(node, struct vmap_area, rb_node);
77668ad4a33SUladzislau Rezki (Sony) 	return va ? va->subtree_max_size : 0;
77768ad4a33SUladzislau Rezki (Sony) }
77868ad4a33SUladzislau Rezki (Sony) 
77968ad4a33SUladzislau Rezki (Sony) /*
78068ad4a33SUladzislau Rezki (Sony)  * Gets called when remove the node and rotate.
78168ad4a33SUladzislau Rezki (Sony)  */
78268ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
78368ad4a33SUladzislau Rezki (Sony) compute_subtree_max_size(struct vmap_area *va)
78468ad4a33SUladzislau Rezki (Sony) {
78568ad4a33SUladzislau Rezki (Sony) 	return max3(va_size(va),
78668ad4a33SUladzislau Rezki (Sony) 		get_subtree_max_size(va->rb_node.rb_left),
78768ad4a33SUladzislau Rezki (Sony) 		get_subtree_max_size(va->rb_node.rb_right));
78868ad4a33SUladzislau Rezki (Sony) }
78968ad4a33SUladzislau Rezki (Sony) 
790315cc066SMichel Lespinasse RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb,
791315cc066SMichel Lespinasse 	struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size)
79268ad4a33SUladzislau Rezki (Sony) 
79368ad4a33SUladzislau Rezki (Sony) static void purge_vmap_area_lazy(void);
79468ad4a33SUladzislau Rezki (Sony) static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
79568ad4a33SUladzislau Rezki (Sony) static unsigned long lazy_max_pages(void);
796db64fe02SNick Piggin 
79797105f0aSRoman Gushchin static atomic_long_t nr_vmalloc_pages;
79897105f0aSRoman Gushchin 
79997105f0aSRoman Gushchin unsigned long vmalloc_nr_pages(void)
80097105f0aSRoman Gushchin {
80197105f0aSRoman Gushchin 	return atomic_long_read(&nr_vmalloc_pages);
80297105f0aSRoman Gushchin }
80397105f0aSRoman Gushchin 
804f181234aSChen Wandun static struct vmap_area *find_vmap_area_exceed_addr(unsigned long addr)
805f181234aSChen Wandun {
806f181234aSChen Wandun 	struct vmap_area *va = NULL;
807f181234aSChen Wandun 	struct rb_node *n = vmap_area_root.rb_node;
808f181234aSChen Wandun 
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 
826db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr)
8271da177e4SLinus Torvalds {
828db64fe02SNick Piggin 	struct rb_node *n = vmap_area_root.rb_node;
829db64fe02SNick Piggin 
830db64fe02SNick Piggin 	while (n) {
831db64fe02SNick Piggin 		struct vmap_area *va;
832db64fe02SNick Piggin 
833db64fe02SNick Piggin 		va = rb_entry(n, struct vmap_area, rb_node);
834db64fe02SNick Piggin 		if (addr < va->va_start)
835db64fe02SNick Piggin 			n = n->rb_left;
836cef2ac3fSHATAYAMA Daisuke 		else if (addr >= va->va_end)
837db64fe02SNick Piggin 			n = n->rb_right;
838db64fe02SNick Piggin 		else
839db64fe02SNick Piggin 			return va;
840db64fe02SNick Piggin 	}
841db64fe02SNick Piggin 
842db64fe02SNick Piggin 	return NULL;
843db64fe02SNick Piggin }
844db64fe02SNick Piggin 
84568ad4a33SUladzislau Rezki (Sony) /*
84668ad4a33SUladzislau Rezki (Sony)  * This function returns back addresses of parent node
84768ad4a33SUladzislau Rezki (Sony)  * and its left or right link for further processing.
8489c801f61SUladzislau Rezki (Sony)  *
8499c801f61SUladzislau Rezki (Sony)  * Otherwise NULL is returned. In that case all further
8509c801f61SUladzislau Rezki (Sony)  * steps regarding inserting of conflicting overlap range
8519c801f61SUladzislau Rezki (Sony)  * have to be declined and actually considered as a bug.
85268ad4a33SUladzislau Rezki (Sony)  */
85368ad4a33SUladzislau Rezki (Sony) static __always_inline struct rb_node **
85468ad4a33SUladzislau Rezki (Sony) find_va_links(struct vmap_area *va,
85568ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct rb_node *from,
85668ad4a33SUladzislau Rezki (Sony) 	struct rb_node **parent)
857db64fe02SNick Piggin {
858170168d0SNamhyung Kim 	struct vmap_area *tmp_va;
85968ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
860db64fe02SNick Piggin 
86168ad4a33SUladzislau Rezki (Sony) 	if (root) {
86268ad4a33SUladzislau Rezki (Sony) 		link = &root->rb_node;
86368ad4a33SUladzislau Rezki (Sony) 		if (unlikely(!*link)) {
86468ad4a33SUladzislau Rezki (Sony) 			*parent = NULL;
86568ad4a33SUladzislau Rezki (Sony) 			return link;
86668ad4a33SUladzislau Rezki (Sony) 		}
86768ad4a33SUladzislau Rezki (Sony) 	} else {
86868ad4a33SUladzislau Rezki (Sony) 		link = &from;
86968ad4a33SUladzislau Rezki (Sony) 	}
87068ad4a33SUladzislau Rezki (Sony) 
87168ad4a33SUladzislau Rezki (Sony) 	/*
87268ad4a33SUladzislau Rezki (Sony) 	 * Go to the bottom of the tree. When we hit the last point
87368ad4a33SUladzislau Rezki (Sony) 	 * we end up with parent rb_node and correct direction, i name
87468ad4a33SUladzislau Rezki (Sony) 	 * it link, where the new va->rb_node will be attached to.
87568ad4a33SUladzislau Rezki (Sony) 	 */
87668ad4a33SUladzislau Rezki (Sony) 	do {
87768ad4a33SUladzislau Rezki (Sony) 		tmp_va = rb_entry(*link, struct vmap_area, rb_node);
87868ad4a33SUladzislau Rezki (Sony) 
87968ad4a33SUladzislau Rezki (Sony) 		/*
88068ad4a33SUladzislau Rezki (Sony) 		 * During the traversal we also do some sanity check.
88168ad4a33SUladzislau Rezki (Sony) 		 * Trigger the BUG() if there are sides(left/right)
88268ad4a33SUladzislau Rezki (Sony) 		 * or full overlaps.
88368ad4a33SUladzislau Rezki (Sony) 		 */
88468ad4a33SUladzislau Rezki (Sony) 		if (va->va_start < tmp_va->va_end &&
88568ad4a33SUladzislau Rezki (Sony) 				va->va_end <= tmp_va->va_start)
88668ad4a33SUladzislau Rezki (Sony) 			link = &(*link)->rb_left;
88768ad4a33SUladzislau Rezki (Sony) 		else if (va->va_end > tmp_va->va_start &&
88868ad4a33SUladzislau Rezki (Sony) 				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
92168ad4a33SUladzislau Rezki (Sony) link_va(struct vmap_area *va, struct rb_root *root,
92268ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent, struct rb_node **link, struct list_head *head)
92368ad4a33SUladzislau Rezki (Sony) {
92468ad4a33SUladzislau Rezki (Sony) 	/*
92568ad4a33SUladzislau Rezki (Sony) 	 * VA is still not in the list, but we can
92668ad4a33SUladzislau Rezki (Sony) 	 * identify its future previous list_head node.
92768ad4a33SUladzislau Rezki (Sony) 	 */
92868ad4a33SUladzislau Rezki (Sony) 	if (likely(parent)) {
92968ad4a33SUladzislau Rezki (Sony) 		head = &rb_entry(parent, struct vmap_area, rb_node)->list;
93068ad4a33SUladzislau Rezki (Sony) 		if (&parent->rb_right != link)
93168ad4a33SUladzislau Rezki (Sony) 			head = head->prev;
93268ad4a33SUladzislau Rezki (Sony) 	}
933db64fe02SNick Piggin 
93468ad4a33SUladzislau Rezki (Sony) 	/* Insert to the rb-tree */
93568ad4a33SUladzislau Rezki (Sony) 	rb_link_node(&va->rb_node, parent, link);
93668ad4a33SUladzislau Rezki (Sony) 	if (root == &free_vmap_area_root) {
93768ad4a33SUladzislau Rezki (Sony) 		/*
93868ad4a33SUladzislau Rezki (Sony) 		 * Some explanation here. Just perform simple insertion
93968ad4a33SUladzislau Rezki (Sony) 		 * to the tree. We do not set va->subtree_max_size to
94068ad4a33SUladzislau Rezki (Sony) 		 * its current size before calling rb_insert_augmented().
94168ad4a33SUladzislau Rezki (Sony) 		 * It is because of we populate the tree from the bottom
94268ad4a33SUladzislau Rezki (Sony) 		 * to parent levels when the node _is_ in the tree.
94368ad4a33SUladzislau Rezki (Sony) 		 *
94468ad4a33SUladzislau Rezki (Sony) 		 * Therefore we set subtree_max_size to zero after insertion,
94568ad4a33SUladzislau Rezki (Sony) 		 * to let __augment_tree_propagate_from() puts everything to
94668ad4a33SUladzislau Rezki (Sony) 		 * the correct order later on.
94768ad4a33SUladzislau Rezki (Sony) 		 */
94868ad4a33SUladzislau Rezki (Sony) 		rb_insert_augmented(&va->rb_node,
94968ad4a33SUladzislau Rezki (Sony) 			root, &free_vmap_area_rb_augment_cb);
95068ad4a33SUladzislau Rezki (Sony) 		va->subtree_max_size = 0;
95168ad4a33SUladzislau Rezki (Sony) 	} else {
95268ad4a33SUladzislau Rezki (Sony) 		rb_insert_color(&va->rb_node, root);
95368ad4a33SUladzislau Rezki (Sony) 	}
95468ad4a33SUladzislau Rezki (Sony) 
95568ad4a33SUladzislau Rezki (Sony) 	/* Address-sort this list */
95668ad4a33SUladzislau Rezki (Sony) 	list_add(&va->list, head);
95768ad4a33SUladzislau Rezki (Sony) }
95868ad4a33SUladzislau Rezki (Sony) 
95968ad4a33SUladzislau Rezki (Sony) static __always_inline void
96068ad4a33SUladzislau Rezki (Sony) unlink_va(struct vmap_area *va, struct rb_root *root)
96168ad4a33SUladzislau Rezki (Sony) {
962460e42d1SUladzislau Rezki (Sony) 	if (WARN_ON(RB_EMPTY_NODE(&va->rb_node)))
963460e42d1SUladzislau Rezki (Sony) 		return;
964460e42d1SUladzislau Rezki (Sony) 
96568ad4a33SUladzislau Rezki (Sony) 	if (root == &free_vmap_area_root)
96668ad4a33SUladzislau Rezki (Sony) 		rb_erase_augmented(&va->rb_node,
96768ad4a33SUladzislau Rezki (Sony) 			root, &free_vmap_area_rb_augment_cb);
96868ad4a33SUladzislau Rezki (Sony) 	else
96968ad4a33SUladzislau Rezki (Sony) 		rb_erase(&va->rb_node, root);
97068ad4a33SUladzislau Rezki (Sony) 
97168ad4a33SUladzislau Rezki (Sony) 	list_del(&va->list);
97268ad4a33SUladzislau Rezki (Sony) 	RB_CLEAR_NODE(&va->rb_node);
97368ad4a33SUladzislau Rezki (Sony) }
97468ad4a33SUladzislau Rezki (Sony) 
975bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK
976bb850f4dSUladzislau Rezki (Sony) static void
977da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(void)
978bb850f4dSUladzislau Rezki (Sony) {
979bb850f4dSUladzislau Rezki (Sony) 	struct vmap_area *va;
980da27c9edSUladzislau Rezki (Sony) 	unsigned long computed_size;
981bb850f4dSUladzislau Rezki (Sony) 
982da27c9edSUladzislau Rezki (Sony) 	list_for_each_entry(va, &free_vmap_area_list, list) {
983da27c9edSUladzislau Rezki (Sony) 		computed_size = compute_subtree_max_size(va);
984da27c9edSUladzislau Rezki (Sony) 		if (computed_size != va->subtree_max_size)
985bb850f4dSUladzislau Rezki (Sony) 			pr_emerg("tree is corrupted: %lu, %lu\n",
986bb850f4dSUladzislau Rezki (Sony) 				va_size(va), va->subtree_max_size);
987bb850f4dSUladzislau Rezki (Sony) 	}
988bb850f4dSUladzislau Rezki (Sony) }
989bb850f4dSUladzislau Rezki (Sony) #endif
990bb850f4dSUladzislau Rezki (Sony) 
99168ad4a33SUladzislau Rezki (Sony) /*
99268ad4a33SUladzislau Rezki (Sony)  * This function populates subtree_max_size from bottom to upper
99368ad4a33SUladzislau Rezki (Sony)  * levels starting from VA point. The propagation must be done
99468ad4a33SUladzislau Rezki (Sony)  * when VA size is modified by changing its va_start/va_end. Or
99568ad4a33SUladzislau Rezki (Sony)  * in case of newly inserting of VA to the tree.
99668ad4a33SUladzislau Rezki (Sony)  *
99768ad4a33SUladzislau Rezki (Sony)  * It means that __augment_tree_propagate_from() must be called:
99868ad4a33SUladzislau Rezki (Sony)  * - After VA has been inserted to the tree(free path);
99968ad4a33SUladzislau Rezki (Sony)  * - After VA has been shrunk(allocation path);
100068ad4a33SUladzislau Rezki (Sony)  * - After VA has been increased(merging path).
100168ad4a33SUladzislau Rezki (Sony)  *
100268ad4a33SUladzislau Rezki (Sony)  * Please note that, it does not mean that upper parent nodes
100368ad4a33SUladzislau Rezki (Sony)  * and their subtree_max_size are recalculated all the time up
100468ad4a33SUladzislau Rezki (Sony)  * to the root node.
100568ad4a33SUladzislau Rezki (Sony)  *
100668ad4a33SUladzislau Rezki (Sony)  *       4--8
100768ad4a33SUladzislau Rezki (Sony)  *        /\
100868ad4a33SUladzislau Rezki (Sony)  *       /  \
100968ad4a33SUladzislau Rezki (Sony)  *      /    \
101068ad4a33SUladzislau Rezki (Sony)  *    2--2  8--8
101168ad4a33SUladzislau Rezki (Sony)  *
101268ad4a33SUladzislau Rezki (Sony)  * For example if we modify the node 4, shrinking it to 2, then
101368ad4a33SUladzislau Rezki (Sony)  * no any modification is required. If we shrink the node 2 to 1
101468ad4a33SUladzislau Rezki (Sony)  * its subtree_max_size is updated only, and set to 1. If we shrink
101568ad4a33SUladzislau Rezki (Sony)  * the node 8 to 6, then its subtree_max_size is set to 6 and parent
101668ad4a33SUladzislau Rezki (Sony)  * node becomes 4--6.
101768ad4a33SUladzislau Rezki (Sony)  */
101868ad4a33SUladzislau Rezki (Sony) static __always_inline void
101968ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(struct vmap_area *va)
102068ad4a33SUladzislau Rezki (Sony) {
102168ad4a33SUladzislau Rezki (Sony) 	/*
102215ae144fSUladzislau Rezki (Sony) 	 * Populate the tree from bottom towards the root until
102315ae144fSUladzislau Rezki (Sony) 	 * the calculated maximum available size of checked node
102415ae144fSUladzislau Rezki (Sony) 	 * is equal to its current one.
102568ad4a33SUladzislau Rezki (Sony) 	 */
102615ae144fSUladzislau Rezki (Sony) 	free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL);
1027bb850f4dSUladzislau Rezki (Sony) 
1028bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK
1029da27c9edSUladzislau Rezki (Sony) 	augment_tree_propagate_check();
1030bb850f4dSUladzislau Rezki (Sony) #endif
103168ad4a33SUladzislau Rezki (Sony) }
103268ad4a33SUladzislau Rezki (Sony) 
103368ad4a33SUladzislau Rezki (Sony) static void
103468ad4a33SUladzislau Rezki (Sony) insert_vmap_area(struct vmap_area *va,
103568ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
103668ad4a33SUladzislau Rezki (Sony) {
103768ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
103868ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
103968ad4a33SUladzislau Rezki (Sony) 
104068ad4a33SUladzislau Rezki (Sony) 	link = find_va_links(va, root, NULL, &parent);
10419c801f61SUladzislau Rezki (Sony) 	if (link)
104268ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
104368ad4a33SUladzislau Rezki (Sony) }
104468ad4a33SUladzislau Rezki (Sony) 
104568ad4a33SUladzislau Rezki (Sony) static void
104668ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(struct vmap_area *va,
104768ad4a33SUladzislau Rezki (Sony) 	struct rb_node *from, struct rb_root *root,
104868ad4a33SUladzislau Rezki (Sony) 	struct list_head *head)
104968ad4a33SUladzislau Rezki (Sony) {
105068ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
105168ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
105268ad4a33SUladzislau Rezki (Sony) 
105368ad4a33SUladzislau Rezki (Sony) 	if (from)
105468ad4a33SUladzislau Rezki (Sony) 		link = find_va_links(va, NULL, from, &parent);
105568ad4a33SUladzislau Rezki (Sony) 	else
105668ad4a33SUladzislau Rezki (Sony) 		link = find_va_links(va, root, NULL, &parent);
105768ad4a33SUladzislau Rezki (Sony) 
10589c801f61SUladzislau Rezki (Sony) 	if (link) {
105968ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
106068ad4a33SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
106168ad4a33SUladzislau Rezki (Sony) 	}
10629c801f61SUladzislau Rezki (Sony) }
106368ad4a33SUladzislau Rezki (Sony) 
106468ad4a33SUladzislau Rezki (Sony) /*
106568ad4a33SUladzislau Rezki (Sony)  * Merge de-allocated chunk of VA memory with previous
106668ad4a33SUladzislau Rezki (Sony)  * and next free blocks. If coalesce is not done a new
106768ad4a33SUladzislau Rezki (Sony)  * free area is inserted. If VA has been merged, it is
106868ad4a33SUladzislau Rezki (Sony)  * freed.
10699c801f61SUladzislau Rezki (Sony)  *
10709c801f61SUladzislau Rezki (Sony)  * Please note, it can return NULL in case of overlap
10719c801f61SUladzislau Rezki (Sony)  * ranges, followed by WARN() report. Despite it is a
10729c801f61SUladzislau Rezki (Sony)  * buggy behaviour, a system can be alive and keep
10739c801f61SUladzislau Rezki (Sony)  * ongoing.
107468ad4a33SUladzislau Rezki (Sony)  */
10753c5c3cfbSDaniel Axtens static __always_inline struct vmap_area *
107668ad4a33SUladzislau Rezki (Sony) merge_or_add_vmap_area(struct vmap_area *va,
107768ad4a33SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
107868ad4a33SUladzislau Rezki (Sony) {
107968ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *sibling;
108068ad4a33SUladzislau Rezki (Sony) 	struct list_head *next;
108168ad4a33SUladzislau Rezki (Sony) 	struct rb_node **link;
108268ad4a33SUladzislau Rezki (Sony) 	struct rb_node *parent;
108368ad4a33SUladzislau Rezki (Sony) 	bool merged = false;
108468ad4a33SUladzislau Rezki (Sony) 
108568ad4a33SUladzislau Rezki (Sony) 	/*
108668ad4a33SUladzislau Rezki (Sony) 	 * Find a place in the tree where VA potentially will be
108768ad4a33SUladzislau Rezki (Sony) 	 * inserted, unless it is merged with its sibling/siblings.
108868ad4a33SUladzislau Rezki (Sony) 	 */
108968ad4a33SUladzislau Rezki (Sony) 	link = find_va_links(va, root, NULL, &parent);
10909c801f61SUladzislau Rezki (Sony) 	if (!link)
10919c801f61SUladzislau Rezki (Sony) 		return NULL;
109268ad4a33SUladzislau Rezki (Sony) 
109368ad4a33SUladzislau Rezki (Sony) 	/*
109468ad4a33SUladzislau Rezki (Sony) 	 * Get next node of VA to check if merging can be done.
109568ad4a33SUladzislau Rezki (Sony) 	 */
109668ad4a33SUladzislau Rezki (Sony) 	next = get_va_next_sibling(parent, link);
109768ad4a33SUladzislau Rezki (Sony) 	if (unlikely(next == NULL))
109868ad4a33SUladzislau Rezki (Sony) 		goto insert;
109968ad4a33SUladzislau Rezki (Sony) 
110068ad4a33SUladzislau Rezki (Sony) 	/*
110168ad4a33SUladzislau Rezki (Sony) 	 * start            end
110268ad4a33SUladzislau Rezki (Sony) 	 * |                |
110368ad4a33SUladzislau Rezki (Sony) 	 * |<------VA------>|<-----Next----->|
110468ad4a33SUladzislau Rezki (Sony) 	 *                  |                |
110568ad4a33SUladzislau Rezki (Sony) 	 *                  start            end
110668ad4a33SUladzislau Rezki (Sony) 	 */
110768ad4a33SUladzislau Rezki (Sony) 	if (next != head) {
110868ad4a33SUladzislau Rezki (Sony) 		sibling = list_entry(next, struct vmap_area, list);
110968ad4a33SUladzislau Rezki (Sony) 		if (sibling->va_start == va->va_end) {
111068ad4a33SUladzislau Rezki (Sony) 			sibling->va_start = va->va_start;
111168ad4a33SUladzislau Rezki (Sony) 
111268ad4a33SUladzislau Rezki (Sony) 			/* Free vmap_area object. */
111368ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, va);
111468ad4a33SUladzislau Rezki (Sony) 
111568ad4a33SUladzislau Rezki (Sony) 			/* Point to the new merged area. */
111668ad4a33SUladzislau Rezki (Sony) 			va = sibling;
111768ad4a33SUladzislau Rezki (Sony) 			merged = true;
111868ad4a33SUladzislau Rezki (Sony) 		}
111968ad4a33SUladzislau Rezki (Sony) 	}
112068ad4a33SUladzislau Rezki (Sony) 
112168ad4a33SUladzislau Rezki (Sony) 	/*
112268ad4a33SUladzislau Rezki (Sony) 	 * start            end
112368ad4a33SUladzislau Rezki (Sony) 	 * |                |
112468ad4a33SUladzislau Rezki (Sony) 	 * |<-----Prev----->|<------VA------>|
112568ad4a33SUladzislau Rezki (Sony) 	 *                  |                |
112668ad4a33SUladzislau Rezki (Sony) 	 *                  start            end
112768ad4a33SUladzislau Rezki (Sony) 	 */
112868ad4a33SUladzislau Rezki (Sony) 	if (next->prev != head) {
112968ad4a33SUladzislau Rezki (Sony) 		sibling = list_entry(next->prev, struct vmap_area, list);
113068ad4a33SUladzislau Rezki (Sony) 		if (sibling->va_end == va->va_start) {
11315dd78640SUladzislau Rezki (Sony) 			/*
11325dd78640SUladzislau Rezki (Sony) 			 * If both neighbors are coalesced, it is important
11335dd78640SUladzislau Rezki (Sony) 			 * to unlink the "next" node first, followed by merging
11345dd78640SUladzislau Rezki (Sony) 			 * with "previous" one. Otherwise the tree might not be
11355dd78640SUladzislau Rezki (Sony) 			 * fully populated if a sibling's augmented value is
11365dd78640SUladzislau Rezki (Sony) 			 * "normalized" because of rotation operations.
11375dd78640SUladzislau Rezki (Sony) 			 */
113854f63d9dSUladzislau Rezki (Sony) 			if (merged)
113968ad4a33SUladzislau Rezki (Sony) 				unlink_va(va, root);
114068ad4a33SUladzislau Rezki (Sony) 
11415dd78640SUladzislau Rezki (Sony) 			sibling->va_end = va->va_end;
11425dd78640SUladzislau Rezki (Sony) 
114368ad4a33SUladzislau Rezki (Sony) 			/* Free vmap_area object. */
114468ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, va);
11453c5c3cfbSDaniel Axtens 
11463c5c3cfbSDaniel Axtens 			/* Point to the new merged area. */
11473c5c3cfbSDaniel Axtens 			va = sibling;
11483c5c3cfbSDaniel Axtens 			merged = true;
114968ad4a33SUladzislau Rezki (Sony) 		}
115068ad4a33SUladzislau Rezki (Sony) 	}
115168ad4a33SUladzislau Rezki (Sony) 
115268ad4a33SUladzislau Rezki (Sony) insert:
11535dd78640SUladzislau Rezki (Sony) 	if (!merged)
115468ad4a33SUladzislau Rezki (Sony) 		link_va(va, root, parent, link, head);
11553c5c3cfbSDaniel Axtens 
115696e2db45SUladzislau Rezki (Sony) 	return va;
115796e2db45SUladzislau Rezki (Sony) }
115896e2db45SUladzislau Rezki (Sony) 
115996e2db45SUladzislau Rezki (Sony) static __always_inline struct vmap_area *
116096e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area_augment(struct vmap_area *va,
116196e2db45SUladzislau Rezki (Sony) 	struct rb_root *root, struct list_head *head)
116296e2db45SUladzislau Rezki (Sony) {
116396e2db45SUladzislau Rezki (Sony) 	va = merge_or_add_vmap_area(va, root, head);
116496e2db45SUladzislau Rezki (Sony) 	if (va)
11655dd78640SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
116696e2db45SUladzislau Rezki (Sony) 
11673c5c3cfbSDaniel Axtens 	return va;
116868ad4a33SUladzislau Rezki (Sony) }
116968ad4a33SUladzislau Rezki (Sony) 
117068ad4a33SUladzislau Rezki (Sony) static __always_inline bool
117168ad4a33SUladzislau Rezki (Sony) is_within_this_va(struct vmap_area *va, unsigned long size,
117268ad4a33SUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
117368ad4a33SUladzislau Rezki (Sony) {
117468ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr;
117568ad4a33SUladzislau Rezki (Sony) 
117668ad4a33SUladzislau Rezki (Sony) 	if (va->va_start > vstart)
117768ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(va->va_start, align);
117868ad4a33SUladzislau Rezki (Sony) 	else
117968ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(vstart, align);
118068ad4a33SUladzislau Rezki (Sony) 
118168ad4a33SUladzislau Rezki (Sony) 	/* Can be overflowed due to big size or alignment. */
118268ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr + size < nva_start_addr ||
118368ad4a33SUladzislau Rezki (Sony) 			nva_start_addr < vstart)
118468ad4a33SUladzislau Rezki (Sony) 		return false;
118568ad4a33SUladzislau Rezki (Sony) 
118668ad4a33SUladzislau Rezki (Sony) 	return (nva_start_addr + size <= va->va_end);
118768ad4a33SUladzislau Rezki (Sony) }
118868ad4a33SUladzislau Rezki (Sony) 
118968ad4a33SUladzislau Rezki (Sony) /*
119068ad4a33SUladzislau Rezki (Sony)  * Find the first free block(lowest start address) in the tree,
119168ad4a33SUladzislau Rezki (Sony)  * that will accomplish the request corresponding to passing
119268ad4a33SUladzislau Rezki (Sony)  * parameters.
119368ad4a33SUladzislau Rezki (Sony)  */
119468ad4a33SUladzislau Rezki (Sony) static __always_inline struct vmap_area *
119568ad4a33SUladzislau Rezki (Sony) find_vmap_lowest_match(unsigned long size,
119668ad4a33SUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
119768ad4a33SUladzislau Rezki (Sony) {
119868ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
119968ad4a33SUladzislau Rezki (Sony) 	struct rb_node *node;
120068ad4a33SUladzislau Rezki (Sony) 
120168ad4a33SUladzislau Rezki (Sony) 	/* Start from the root. */
120268ad4a33SUladzislau Rezki (Sony) 	node = free_vmap_area_root.rb_node;
120368ad4a33SUladzislau Rezki (Sony) 
120468ad4a33SUladzislau Rezki (Sony) 	while (node) {
120568ad4a33SUladzislau Rezki (Sony) 		va = rb_entry(node, struct vmap_area, rb_node);
120668ad4a33SUladzislau Rezki (Sony) 
12079f531973SUladzislau Rezki (Sony) 		if (get_subtree_max_size(node->rb_left) >= size &&
120868ad4a33SUladzislau Rezki (Sony) 				vstart < va->va_start) {
120968ad4a33SUladzislau Rezki (Sony) 			node = node->rb_left;
121068ad4a33SUladzislau Rezki (Sony) 		} else {
121168ad4a33SUladzislau Rezki (Sony) 			if (is_within_this_va(va, size, align, vstart))
121268ad4a33SUladzislau Rezki (Sony) 				return va;
121368ad4a33SUladzislau Rezki (Sony) 
121468ad4a33SUladzislau Rezki (Sony) 			/*
121568ad4a33SUladzislau Rezki (Sony) 			 * Does not make sense to go deeper towards the right
121668ad4a33SUladzislau Rezki (Sony) 			 * sub-tree if it does not have a free block that is
12179f531973SUladzislau Rezki (Sony) 			 * equal or bigger to the requested search size.
121868ad4a33SUladzislau Rezki (Sony) 			 */
12199f531973SUladzislau Rezki (Sony) 			if (get_subtree_max_size(node->rb_right) >= size) {
122068ad4a33SUladzislau Rezki (Sony) 				node = node->rb_right;
122168ad4a33SUladzislau Rezki (Sony) 				continue;
122268ad4a33SUladzislau Rezki (Sony) 			}
122368ad4a33SUladzislau Rezki (Sony) 
122468ad4a33SUladzislau Rezki (Sony) 			/*
12253806b041SAndrew Morton 			 * OK. We roll back and find the first right sub-tree,
122668ad4a33SUladzislau Rezki (Sony) 			 * that will satisfy the search criteria. It can happen
12279f531973SUladzislau Rezki (Sony) 			 * due to "vstart" restriction or an alignment overhead
12289f531973SUladzislau Rezki (Sony) 			 * that is bigger then PAGE_SIZE.
122968ad4a33SUladzislau Rezki (Sony) 			 */
123068ad4a33SUladzislau Rezki (Sony) 			while ((node = rb_parent(node))) {
123168ad4a33SUladzislau Rezki (Sony) 				va = rb_entry(node, struct vmap_area, rb_node);
123268ad4a33SUladzislau Rezki (Sony) 				if (is_within_this_va(va, size, align, vstart))
123368ad4a33SUladzislau Rezki (Sony) 					return va;
123468ad4a33SUladzislau Rezki (Sony) 
12359f531973SUladzislau Rezki (Sony) 				if (get_subtree_max_size(node->rb_right) >= size &&
123668ad4a33SUladzislau Rezki (Sony) 						vstart <= va->va_start) {
12379f531973SUladzislau Rezki (Sony) 					/*
12389f531973SUladzislau Rezki (Sony) 					 * Shift the vstart forward. Please note, we update it with
12399f531973SUladzislau Rezki (Sony) 					 * parent's start address adding "1" because we do not want
12409f531973SUladzislau Rezki (Sony) 					 * to enter same sub-tree after it has already been checked
12419f531973SUladzislau Rezki (Sony) 					 * and no suitable free block found there.
12429f531973SUladzislau Rezki (Sony) 					 */
12439f531973SUladzislau Rezki (Sony) 					vstart = va->va_start + 1;
124468ad4a33SUladzislau Rezki (Sony) 					node = node->rb_right;
124568ad4a33SUladzislau Rezki (Sony) 					break;
124668ad4a33SUladzislau Rezki (Sony) 				}
124768ad4a33SUladzislau Rezki (Sony) 			}
124868ad4a33SUladzislau Rezki (Sony) 		}
124968ad4a33SUladzislau Rezki (Sony) 	}
125068ad4a33SUladzislau Rezki (Sony) 
125168ad4a33SUladzislau Rezki (Sony) 	return NULL;
125268ad4a33SUladzislau Rezki (Sony) }
125368ad4a33SUladzislau Rezki (Sony) 
1254a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1255a6cf4e0fSUladzislau Rezki (Sony) #include <linux/random.h>
1256a6cf4e0fSUladzislau Rezki (Sony) 
1257a6cf4e0fSUladzislau Rezki (Sony) static struct vmap_area *
1258a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_linear_match(unsigned long size,
1259a6cf4e0fSUladzislau Rezki (Sony) 	unsigned long align, unsigned long vstart)
1260a6cf4e0fSUladzislau Rezki (Sony) {
1261a6cf4e0fSUladzislau Rezki (Sony) 	struct vmap_area *va;
1262a6cf4e0fSUladzislau Rezki (Sony) 
1263a6cf4e0fSUladzislau Rezki (Sony) 	list_for_each_entry(va, &free_vmap_area_list, list) {
1264a6cf4e0fSUladzislau Rezki (Sony) 		if (!is_within_this_va(va, size, align, vstart))
1265a6cf4e0fSUladzislau Rezki (Sony) 			continue;
1266a6cf4e0fSUladzislau Rezki (Sony) 
1267a6cf4e0fSUladzislau Rezki (Sony) 		return va;
1268a6cf4e0fSUladzislau Rezki (Sony) 	}
1269a6cf4e0fSUladzislau Rezki (Sony) 
1270a6cf4e0fSUladzislau Rezki (Sony) 	return NULL;
1271a6cf4e0fSUladzislau Rezki (Sony) }
1272a6cf4e0fSUladzislau Rezki (Sony) 
1273a6cf4e0fSUladzislau Rezki (Sony) static void
1274066fed59SUladzislau Rezki (Sony) find_vmap_lowest_match_check(unsigned long size, unsigned long align)
1275a6cf4e0fSUladzislau Rezki (Sony) {
1276a6cf4e0fSUladzislau Rezki (Sony) 	struct vmap_area *va_1, *va_2;
1277a6cf4e0fSUladzislau Rezki (Sony) 	unsigned long vstart;
1278a6cf4e0fSUladzislau Rezki (Sony) 	unsigned int rnd;
1279a6cf4e0fSUladzislau Rezki (Sony) 
1280a6cf4e0fSUladzislau Rezki (Sony) 	get_random_bytes(&rnd, sizeof(rnd));
1281a6cf4e0fSUladzislau Rezki (Sony) 	vstart = VMALLOC_START + rnd;
1282a6cf4e0fSUladzislau Rezki (Sony) 
1283066fed59SUladzislau Rezki (Sony) 	va_1 = find_vmap_lowest_match(size, align, vstart);
1284066fed59SUladzislau Rezki (Sony) 	va_2 = find_vmap_lowest_linear_match(size, align, vstart);
1285a6cf4e0fSUladzislau Rezki (Sony) 
1286a6cf4e0fSUladzislau Rezki (Sony) 	if (va_1 != va_2)
1287a6cf4e0fSUladzislau Rezki (Sony) 		pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n",
1288a6cf4e0fSUladzislau Rezki (Sony) 			va_1, va_2, vstart);
1289a6cf4e0fSUladzislau Rezki (Sony) }
1290a6cf4e0fSUladzislau Rezki (Sony) #endif
1291a6cf4e0fSUladzislau Rezki (Sony) 
129268ad4a33SUladzislau Rezki (Sony) enum fit_type {
129368ad4a33SUladzislau Rezki (Sony) 	NOTHING_FIT = 0,
129468ad4a33SUladzislau Rezki (Sony) 	FL_FIT_TYPE = 1,	/* full fit */
129568ad4a33SUladzislau Rezki (Sony) 	LE_FIT_TYPE = 2,	/* left edge fit */
129668ad4a33SUladzislau Rezki (Sony) 	RE_FIT_TYPE = 3,	/* right edge fit */
129768ad4a33SUladzislau Rezki (Sony) 	NE_FIT_TYPE = 4		/* no edge fit */
129868ad4a33SUladzislau Rezki (Sony) };
129968ad4a33SUladzislau Rezki (Sony) 
130068ad4a33SUladzislau Rezki (Sony) static __always_inline enum fit_type
130168ad4a33SUladzislau Rezki (Sony) classify_va_fit_type(struct vmap_area *va,
130268ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr, unsigned long size)
130368ad4a33SUladzislau Rezki (Sony) {
130468ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
130568ad4a33SUladzislau Rezki (Sony) 
130668ad4a33SUladzislau Rezki (Sony) 	/* Check if it is within VA. */
130768ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr < va->va_start ||
130868ad4a33SUladzislau Rezki (Sony) 			nva_start_addr + size > va->va_end)
130968ad4a33SUladzislau Rezki (Sony) 		return NOTHING_FIT;
131068ad4a33SUladzislau Rezki (Sony) 
131168ad4a33SUladzislau Rezki (Sony) 	/* Now classify. */
131268ad4a33SUladzislau Rezki (Sony) 	if (va->va_start == nva_start_addr) {
131368ad4a33SUladzislau Rezki (Sony) 		if (va->va_end == nva_start_addr + size)
131468ad4a33SUladzislau Rezki (Sony) 			type = FL_FIT_TYPE;
131568ad4a33SUladzislau Rezki (Sony) 		else
131668ad4a33SUladzislau Rezki (Sony) 			type = LE_FIT_TYPE;
131768ad4a33SUladzislau Rezki (Sony) 	} else if (va->va_end == nva_start_addr + size) {
131868ad4a33SUladzislau Rezki (Sony) 		type = RE_FIT_TYPE;
131968ad4a33SUladzislau Rezki (Sony) 	} else {
132068ad4a33SUladzislau Rezki (Sony) 		type = NE_FIT_TYPE;
132168ad4a33SUladzislau Rezki (Sony) 	}
132268ad4a33SUladzislau Rezki (Sony) 
132368ad4a33SUladzislau Rezki (Sony) 	return type;
132468ad4a33SUladzislau Rezki (Sony) }
132568ad4a33SUladzislau Rezki (Sony) 
132668ad4a33SUladzislau Rezki (Sony) static __always_inline int
132768ad4a33SUladzislau Rezki (Sony) adjust_va_to_fit_type(struct vmap_area *va,
132868ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr, unsigned long size,
132968ad4a33SUladzislau Rezki (Sony) 	enum fit_type type)
133068ad4a33SUladzislau Rezki (Sony) {
13312c929233SArnd Bergmann 	struct vmap_area *lva = NULL;
133268ad4a33SUladzislau Rezki (Sony) 
133368ad4a33SUladzislau Rezki (Sony) 	if (type == FL_FIT_TYPE) {
133468ad4a33SUladzislau Rezki (Sony) 		/*
133568ad4a33SUladzislau Rezki (Sony) 		 * No need to split VA, it fully fits.
133668ad4a33SUladzislau Rezki (Sony) 		 *
133768ad4a33SUladzislau Rezki (Sony) 		 * |               |
133868ad4a33SUladzislau Rezki (Sony) 		 * V      NVA      V
133968ad4a33SUladzislau Rezki (Sony) 		 * |---------------|
134068ad4a33SUladzislau Rezki (Sony) 		 */
134168ad4a33SUladzislau Rezki (Sony) 		unlink_va(va, &free_vmap_area_root);
134268ad4a33SUladzislau Rezki (Sony) 		kmem_cache_free(vmap_area_cachep, va);
134368ad4a33SUladzislau Rezki (Sony) 	} else if (type == LE_FIT_TYPE) {
134468ad4a33SUladzislau Rezki (Sony) 		/*
134568ad4a33SUladzislau Rezki (Sony) 		 * Split left edge of fit VA.
134668ad4a33SUladzislau Rezki (Sony) 		 *
134768ad4a33SUladzislau Rezki (Sony) 		 * |       |
134868ad4a33SUladzislau Rezki (Sony) 		 * V  NVA  V   R
134968ad4a33SUladzislau Rezki (Sony) 		 * |-------|-------|
135068ad4a33SUladzislau Rezki (Sony) 		 */
135168ad4a33SUladzislau Rezki (Sony) 		va->va_start += size;
135268ad4a33SUladzislau Rezki (Sony) 	} else if (type == RE_FIT_TYPE) {
135368ad4a33SUladzislau Rezki (Sony) 		/*
135468ad4a33SUladzislau Rezki (Sony) 		 * Split right edge of fit VA.
135568ad4a33SUladzislau Rezki (Sony) 		 *
135668ad4a33SUladzislau Rezki (Sony) 		 *         |       |
135768ad4a33SUladzislau Rezki (Sony) 		 *     L   V  NVA  V
135868ad4a33SUladzislau Rezki (Sony) 		 * |-------|-------|
135968ad4a33SUladzislau Rezki (Sony) 		 */
136068ad4a33SUladzislau Rezki (Sony) 		va->va_end = nva_start_addr;
136168ad4a33SUladzislau Rezki (Sony) 	} else if (type == NE_FIT_TYPE) {
136268ad4a33SUladzislau Rezki (Sony) 		/*
136368ad4a33SUladzislau Rezki (Sony) 		 * Split no edge of fit VA.
136468ad4a33SUladzislau Rezki (Sony) 		 *
136568ad4a33SUladzislau Rezki (Sony) 		 *     |       |
136668ad4a33SUladzislau Rezki (Sony) 		 *   L V  NVA  V R
136768ad4a33SUladzislau Rezki (Sony) 		 * |---|-------|---|
136868ad4a33SUladzislau Rezki (Sony) 		 */
136982dd23e8SUladzislau Rezki (Sony) 		lva = __this_cpu_xchg(ne_fit_preload_node, NULL);
137082dd23e8SUladzislau Rezki (Sony) 		if (unlikely(!lva)) {
137182dd23e8SUladzislau Rezki (Sony) 			/*
137282dd23e8SUladzislau Rezki (Sony) 			 * For percpu allocator we do not do any pre-allocation
137382dd23e8SUladzislau Rezki (Sony) 			 * and leave it as it is. The reason is it most likely
137482dd23e8SUladzislau Rezki (Sony) 			 * never ends up with NE_FIT_TYPE splitting. In case of
137582dd23e8SUladzislau Rezki (Sony) 			 * percpu allocations offsets and sizes are aligned to
137682dd23e8SUladzislau Rezki (Sony) 			 * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE
137782dd23e8SUladzislau Rezki (Sony) 			 * are its main fitting cases.
137882dd23e8SUladzislau Rezki (Sony) 			 *
137982dd23e8SUladzislau Rezki (Sony) 			 * There are a few exceptions though, as an example it is
138082dd23e8SUladzislau Rezki (Sony) 			 * a first allocation (early boot up) when we have "one"
138182dd23e8SUladzislau Rezki (Sony) 			 * big free space that has to be split.
1382060650a2SUladzislau Rezki (Sony) 			 *
1383060650a2SUladzislau Rezki (Sony) 			 * Also we can hit this path in case of regular "vmap"
1384060650a2SUladzislau Rezki (Sony) 			 * allocations, if "this" current CPU was not preloaded.
1385060650a2SUladzislau Rezki (Sony) 			 * See the comment in alloc_vmap_area() why. If so, then
1386060650a2SUladzislau Rezki (Sony) 			 * GFP_NOWAIT is used instead to get an extra object for
1387060650a2SUladzislau Rezki (Sony) 			 * split purpose. That is rare and most time does not
1388060650a2SUladzislau Rezki (Sony) 			 * occur.
1389060650a2SUladzislau Rezki (Sony) 			 *
1390060650a2SUladzislau Rezki (Sony) 			 * What happens if an allocation gets failed. Basically,
1391060650a2SUladzislau Rezki (Sony) 			 * an "overflow" path is triggered to purge lazily freed
1392060650a2SUladzislau Rezki (Sony) 			 * areas to free some memory, then, the "retry" path is
1393060650a2SUladzislau Rezki (Sony) 			 * triggered to repeat one more time. See more details
1394060650a2SUladzislau Rezki (Sony) 			 * in alloc_vmap_area() function.
139582dd23e8SUladzislau Rezki (Sony) 			 */
139668ad4a33SUladzislau Rezki (Sony) 			lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
139782dd23e8SUladzislau Rezki (Sony) 			if (!lva)
139868ad4a33SUladzislau Rezki (Sony) 				return -1;
139982dd23e8SUladzislau Rezki (Sony) 		}
140068ad4a33SUladzislau Rezki (Sony) 
140168ad4a33SUladzislau Rezki (Sony) 		/*
140268ad4a33SUladzislau Rezki (Sony) 		 * Build the remainder.
140368ad4a33SUladzislau Rezki (Sony) 		 */
140468ad4a33SUladzislau Rezki (Sony) 		lva->va_start = va->va_start;
140568ad4a33SUladzislau Rezki (Sony) 		lva->va_end = nva_start_addr;
140668ad4a33SUladzislau Rezki (Sony) 
140768ad4a33SUladzislau Rezki (Sony) 		/*
140868ad4a33SUladzislau Rezki (Sony) 		 * Shrink this VA to remaining size.
140968ad4a33SUladzislau Rezki (Sony) 		 */
141068ad4a33SUladzislau Rezki (Sony) 		va->va_start = nva_start_addr + size;
141168ad4a33SUladzislau Rezki (Sony) 	} else {
141268ad4a33SUladzislau Rezki (Sony) 		return -1;
141368ad4a33SUladzislau Rezki (Sony) 	}
141468ad4a33SUladzislau Rezki (Sony) 
141568ad4a33SUladzislau Rezki (Sony) 	if (type != FL_FIT_TYPE) {
141668ad4a33SUladzislau Rezki (Sony) 		augment_tree_propagate_from(va);
141768ad4a33SUladzislau Rezki (Sony) 
14182c929233SArnd Bergmann 		if (lva)	/* type == NE_FIT_TYPE */
141968ad4a33SUladzislau Rezki (Sony) 			insert_vmap_area_augment(lva, &va->rb_node,
142068ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_root, &free_vmap_area_list);
142168ad4a33SUladzislau Rezki (Sony) 	}
142268ad4a33SUladzislau Rezki (Sony) 
142368ad4a33SUladzislau Rezki (Sony) 	return 0;
142468ad4a33SUladzislau Rezki (Sony) }
142568ad4a33SUladzislau Rezki (Sony) 
142668ad4a33SUladzislau Rezki (Sony) /*
142768ad4a33SUladzislau Rezki (Sony)  * Returns a start address of the newly allocated area, if success.
142868ad4a33SUladzislau Rezki (Sony)  * Otherwise a vend is returned that indicates failure.
142968ad4a33SUladzislau Rezki (Sony)  */
143068ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long
143168ad4a33SUladzislau Rezki (Sony) __alloc_vmap_area(unsigned long size, unsigned long align,
1432cacca6baSUladzislau Rezki (Sony) 	unsigned long vstart, unsigned long vend)
143368ad4a33SUladzislau Rezki (Sony) {
143468ad4a33SUladzislau Rezki (Sony) 	unsigned long nva_start_addr;
143568ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va;
143668ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
143768ad4a33SUladzislau Rezki (Sony) 	int ret;
143868ad4a33SUladzislau Rezki (Sony) 
143968ad4a33SUladzislau Rezki (Sony) 	va = find_vmap_lowest_match(size, align, vstart);
144068ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!va))
144168ad4a33SUladzislau Rezki (Sony) 		return vend;
144268ad4a33SUladzislau Rezki (Sony) 
144368ad4a33SUladzislau Rezki (Sony) 	if (va->va_start > vstart)
144468ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(va->va_start, align);
144568ad4a33SUladzislau Rezki (Sony) 	else
144668ad4a33SUladzislau Rezki (Sony) 		nva_start_addr = ALIGN(vstart, align);
144768ad4a33SUladzislau Rezki (Sony) 
144868ad4a33SUladzislau Rezki (Sony) 	/* Check the "vend" restriction. */
144968ad4a33SUladzislau Rezki (Sony) 	if (nva_start_addr + size > vend)
145068ad4a33SUladzislau Rezki (Sony) 		return vend;
145168ad4a33SUladzislau Rezki (Sony) 
145268ad4a33SUladzislau Rezki (Sony) 	/* Classify what we have found. */
145368ad4a33SUladzislau Rezki (Sony) 	type = classify_va_fit_type(va, nva_start_addr, size);
145468ad4a33SUladzislau Rezki (Sony) 	if (WARN_ON_ONCE(type == NOTHING_FIT))
145568ad4a33SUladzislau Rezki (Sony) 		return vend;
145668ad4a33SUladzislau Rezki (Sony) 
145768ad4a33SUladzislau Rezki (Sony) 	/* Update the free vmap_area. */
145868ad4a33SUladzislau Rezki (Sony) 	ret = adjust_va_to_fit_type(va, nva_start_addr, size, type);
145968ad4a33SUladzislau Rezki (Sony) 	if (ret)
146068ad4a33SUladzislau Rezki (Sony) 		return vend;
146168ad4a33SUladzislau Rezki (Sony) 
1462a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1463066fed59SUladzislau Rezki (Sony) 	find_vmap_lowest_match_check(size, align);
1464a6cf4e0fSUladzislau Rezki (Sony) #endif
1465a6cf4e0fSUladzislau Rezki (Sony) 
146668ad4a33SUladzislau Rezki (Sony) 	return nva_start_addr;
146768ad4a33SUladzislau Rezki (Sony) }
14684da56b99SChris Wilson 
1469db64fe02SNick Piggin /*
1470d98c9e83SAndrey Ryabinin  * Free a region of KVA allocated by alloc_vmap_area
1471d98c9e83SAndrey Ryabinin  */
1472d98c9e83SAndrey Ryabinin static void free_vmap_area(struct vmap_area *va)
1473d98c9e83SAndrey Ryabinin {
1474d98c9e83SAndrey Ryabinin 	/*
1475d98c9e83SAndrey Ryabinin 	 * Remove from the busy tree/list.
1476d98c9e83SAndrey Ryabinin 	 */
1477d98c9e83SAndrey Ryabinin 	spin_lock(&vmap_area_lock);
1478d98c9e83SAndrey Ryabinin 	unlink_va(va, &vmap_area_root);
1479d98c9e83SAndrey Ryabinin 	spin_unlock(&vmap_area_lock);
1480d98c9e83SAndrey Ryabinin 
1481d98c9e83SAndrey Ryabinin 	/*
1482d98c9e83SAndrey Ryabinin 	 * Insert/Merge it back to the free tree/list.
1483d98c9e83SAndrey Ryabinin 	 */
1484d98c9e83SAndrey Ryabinin 	spin_lock(&free_vmap_area_lock);
148596e2db45SUladzislau Rezki (Sony) 	merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list);
1486d98c9e83SAndrey Ryabinin 	spin_unlock(&free_vmap_area_lock);
1487d98c9e83SAndrey Ryabinin }
1488d98c9e83SAndrey Ryabinin 
1489187f8cc4SUladzislau Rezki (Sony) static inline void
1490187f8cc4SUladzislau Rezki (Sony) preload_this_cpu_lock(spinlock_t *lock, gfp_t gfp_mask, int node)
1491187f8cc4SUladzislau Rezki (Sony) {
1492187f8cc4SUladzislau Rezki (Sony) 	struct vmap_area *va = NULL;
1493187f8cc4SUladzislau Rezki (Sony) 
1494187f8cc4SUladzislau Rezki (Sony) 	/*
1495187f8cc4SUladzislau Rezki (Sony) 	 * Preload this CPU with one extra vmap_area object. It is used
1496187f8cc4SUladzislau Rezki (Sony) 	 * when fit type of free area is NE_FIT_TYPE. It guarantees that
1497187f8cc4SUladzislau Rezki (Sony) 	 * a CPU that does an allocation is preloaded.
1498187f8cc4SUladzislau Rezki (Sony) 	 *
1499187f8cc4SUladzislau Rezki (Sony) 	 * We do it in non-atomic context, thus it allows us to use more
1500187f8cc4SUladzislau Rezki (Sony) 	 * permissive allocation masks to be more stable under low memory
1501187f8cc4SUladzislau Rezki (Sony) 	 * condition and high memory pressure.
1502187f8cc4SUladzislau Rezki (Sony) 	 */
1503187f8cc4SUladzislau Rezki (Sony) 	if (!this_cpu_read(ne_fit_preload_node))
1504187f8cc4SUladzislau Rezki (Sony) 		va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
1505187f8cc4SUladzislau Rezki (Sony) 
1506187f8cc4SUladzislau Rezki (Sony) 	spin_lock(lock);
1507187f8cc4SUladzislau Rezki (Sony) 
1508187f8cc4SUladzislau Rezki (Sony) 	if (va && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, va))
1509187f8cc4SUladzislau Rezki (Sony) 		kmem_cache_free(vmap_area_cachep, va);
1510187f8cc4SUladzislau Rezki (Sony) }
1511187f8cc4SUladzislau Rezki (Sony) 
1512d98c9e83SAndrey Ryabinin /*
1513db64fe02SNick Piggin  * Allocate a region of KVA of the specified size and alignment, within the
1514db64fe02SNick Piggin  * vstart and vend.
1515db64fe02SNick Piggin  */
1516db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size,
1517db64fe02SNick Piggin 				unsigned long align,
1518db64fe02SNick Piggin 				unsigned long vstart, unsigned long vend,
1519db64fe02SNick Piggin 				int node, gfp_t gfp_mask)
1520db64fe02SNick Piggin {
1521187f8cc4SUladzislau Rezki (Sony) 	struct vmap_area *va;
152212e376a6SUladzislau Rezki (Sony) 	unsigned long freed;
15231da177e4SLinus Torvalds 	unsigned long addr;
1524db64fe02SNick Piggin 	int purged = 0;
1525d98c9e83SAndrey Ryabinin 	int ret;
1526db64fe02SNick Piggin 
15277766970cSNick Piggin 	BUG_ON(!size);
1528891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
152989699605SNick Piggin 	BUG_ON(!is_power_of_2(align));
1530db64fe02SNick Piggin 
153168ad4a33SUladzislau Rezki (Sony) 	if (unlikely(!vmap_initialized))
153268ad4a33SUladzislau Rezki (Sony) 		return ERR_PTR(-EBUSY);
153368ad4a33SUladzislau Rezki (Sony) 
15345803ed29SChristoph Hellwig 	might_sleep();
1535f07116d7SUladzislau Rezki (Sony) 	gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
15364da56b99SChris Wilson 
1537f07116d7SUladzislau Rezki (Sony) 	va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
1538db64fe02SNick Piggin 	if (unlikely(!va))
1539db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
1540db64fe02SNick Piggin 
15417f88f88fSCatalin Marinas 	/*
15427f88f88fSCatalin Marinas 	 * Only scan the relevant parts containing pointers to other objects
15437f88f88fSCatalin Marinas 	 * to avoid false negatives.
15447f88f88fSCatalin Marinas 	 */
1545f07116d7SUladzislau Rezki (Sony) 	kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask);
15467f88f88fSCatalin Marinas 
1547db64fe02SNick Piggin retry:
1548187f8cc4SUladzislau Rezki (Sony) 	preload_this_cpu_lock(&free_vmap_area_lock, gfp_mask, node);
1549187f8cc4SUladzislau Rezki (Sony) 	addr = __alloc_vmap_area(size, align, vstart, vend);
1550187f8cc4SUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
155168ad4a33SUladzislau Rezki (Sony) 
155289699605SNick Piggin 	/*
155368ad4a33SUladzislau Rezki (Sony) 	 * If an allocation fails, the "vend" address is
155468ad4a33SUladzislau Rezki (Sony) 	 * returned. Therefore trigger the overflow path.
155589699605SNick Piggin 	 */
155668ad4a33SUladzislau Rezki (Sony) 	if (unlikely(addr == vend))
155789699605SNick Piggin 		goto overflow;
155889699605SNick Piggin 
155989699605SNick Piggin 	va->va_start = addr;
156089699605SNick Piggin 	va->va_end = addr + size;
1561688fcbfcSPengfei Li 	va->vm = NULL;
156268ad4a33SUladzislau Rezki (Sony) 
1563e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
1564e36176beSUladzislau Rezki (Sony) 	insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
156589699605SNick Piggin 	spin_unlock(&vmap_area_lock);
156689699605SNick Piggin 
156761e16557SWang Xiaoqiang 	BUG_ON(!IS_ALIGNED(va->va_start, align));
156889699605SNick Piggin 	BUG_ON(va->va_start < vstart);
156989699605SNick Piggin 	BUG_ON(va->va_end > vend);
157089699605SNick Piggin 
1571d98c9e83SAndrey Ryabinin 	ret = kasan_populate_vmalloc(addr, size);
1572d98c9e83SAndrey Ryabinin 	if (ret) {
1573d98c9e83SAndrey Ryabinin 		free_vmap_area(va);
1574d98c9e83SAndrey Ryabinin 		return ERR_PTR(ret);
1575d98c9e83SAndrey Ryabinin 	}
1576d98c9e83SAndrey Ryabinin 
157789699605SNick Piggin 	return va;
157889699605SNick Piggin 
15797766970cSNick Piggin overflow:
1580db64fe02SNick Piggin 	if (!purged) {
1581db64fe02SNick Piggin 		purge_vmap_area_lazy();
1582db64fe02SNick Piggin 		purged = 1;
1583db64fe02SNick Piggin 		goto retry;
1584db64fe02SNick Piggin 	}
15854da56b99SChris Wilson 
158612e376a6SUladzislau Rezki (Sony) 	freed = 0;
15874da56b99SChris Wilson 	blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
158812e376a6SUladzislau Rezki (Sony) 
15894da56b99SChris Wilson 	if (freed > 0) {
15904da56b99SChris Wilson 		purged = 0;
15914da56b99SChris Wilson 		goto retry;
15924da56b99SChris Wilson 	}
15934da56b99SChris Wilson 
159403497d76SFlorian Fainelli 	if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
1595756a025fSJoe Perches 		pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1596756a025fSJoe Perches 			size);
159768ad4a33SUladzislau Rezki (Sony) 
159868ad4a33SUladzislau Rezki (Sony) 	kmem_cache_free(vmap_area_cachep, va);
1599db64fe02SNick Piggin 	return ERR_PTR(-EBUSY);
1600db64fe02SNick Piggin }
1601db64fe02SNick Piggin 
16024da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb)
16034da56b99SChris Wilson {
16044da56b99SChris Wilson 	return blocking_notifier_chain_register(&vmap_notify_list, nb);
16054da56b99SChris Wilson }
16064da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
16074da56b99SChris Wilson 
16084da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb)
16094da56b99SChris Wilson {
16104da56b99SChris Wilson 	return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
16114da56b99SChris Wilson }
16124da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
16134da56b99SChris Wilson 
1614db64fe02SNick Piggin /*
1615db64fe02SNick Piggin  * lazy_max_pages is the maximum amount of virtual address space we gather up
1616db64fe02SNick Piggin  * before attempting to purge with a TLB flush.
1617db64fe02SNick Piggin  *
1618db64fe02SNick Piggin  * There is a tradeoff here: a larger number will cover more kernel page tables
1619db64fe02SNick Piggin  * and take slightly longer to purge, but it will linearly reduce the number of
1620db64fe02SNick Piggin  * global TLB flushes that must be performed. It would seem natural to scale
1621db64fe02SNick Piggin  * this number up linearly with the number of CPUs (because vmapping activity
1622db64fe02SNick Piggin  * could also scale linearly with the number of CPUs), however it is likely
1623db64fe02SNick Piggin  * that in practice, workloads might be constrained in other ways that mean
1624db64fe02SNick Piggin  * vmap activity will not scale linearly with CPUs. Also, I want to be
1625db64fe02SNick Piggin  * conservative and not introduce a big latency on huge systems, so go with
1626db64fe02SNick Piggin  * a less aggressive log scale. It will still be an improvement over the old
1627db64fe02SNick Piggin  * code, and it will be simple to change the scale factor if we find that it
1628db64fe02SNick Piggin  * becomes a problem on bigger systems.
1629db64fe02SNick Piggin  */
1630db64fe02SNick Piggin static unsigned long lazy_max_pages(void)
1631db64fe02SNick Piggin {
1632db64fe02SNick Piggin 	unsigned int log;
1633db64fe02SNick Piggin 
1634db64fe02SNick Piggin 	log = fls(num_online_cpus());
1635db64fe02SNick Piggin 
1636db64fe02SNick Piggin 	return log * (32UL * 1024 * 1024 / PAGE_SIZE);
1637db64fe02SNick Piggin }
1638db64fe02SNick Piggin 
16394d36e6f8SUladzislau Rezki (Sony) static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0);
1640db64fe02SNick Piggin 
16410574ecd1SChristoph Hellwig /*
1642f0953a1bSIngo Molnar  * Serialize vmap purging.  There is no actual critical section protected
16430574ecd1SChristoph Hellwig  * by this look, but we want to avoid concurrent calls for performance
16440574ecd1SChristoph Hellwig  * reasons and to make the pcpu_get_vm_areas more deterministic.
16450574ecd1SChristoph Hellwig  */
1646f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock);
16470574ecd1SChristoph Hellwig 
164802b709dfSNick Piggin /* for per-CPU blocks */
164902b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void);
165002b709dfSNick Piggin 
16515da96bddSMel Gorman #ifdef CONFIG_X86_64
1652db64fe02SNick Piggin /*
16533ee48b6aSCliff Wickman  * called before a call to iounmap() if the caller wants vm_area_struct's
16543ee48b6aSCliff Wickman  * immediately freed.
16553ee48b6aSCliff Wickman  */
16563ee48b6aSCliff Wickman void set_iounmap_nonlazy(void)
16573ee48b6aSCliff Wickman {
16584d36e6f8SUladzislau Rezki (Sony) 	atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1);
16593ee48b6aSCliff Wickman }
16605da96bddSMel Gorman #endif /* CONFIG_X86_64 */
16613ee48b6aSCliff Wickman 
16623ee48b6aSCliff Wickman /*
1663db64fe02SNick Piggin  * Purges all lazily-freed vmap areas.
1664db64fe02SNick Piggin  */
16650574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
1666db64fe02SNick Piggin {
16674d36e6f8SUladzislau Rezki (Sony) 	unsigned long resched_threshold;
166896e2db45SUladzislau Rezki (Sony) 	struct list_head local_pure_list;
166996e2db45SUladzislau Rezki (Sony) 	struct vmap_area *va, *n_va;
1670db64fe02SNick Piggin 
16710574ecd1SChristoph Hellwig 	lockdep_assert_held(&vmap_purge_lock);
167202b709dfSNick Piggin 
167396e2db45SUladzislau Rezki (Sony) 	spin_lock(&purge_vmap_area_lock);
167496e2db45SUladzislau Rezki (Sony) 	purge_vmap_area_root = RB_ROOT;
167596e2db45SUladzislau Rezki (Sony) 	list_replace_init(&purge_vmap_area_list, &local_pure_list);
167696e2db45SUladzislau Rezki (Sony) 	spin_unlock(&purge_vmap_area_lock);
167796e2db45SUladzislau Rezki (Sony) 
167896e2db45SUladzislau Rezki (Sony) 	if (unlikely(list_empty(&local_pure_list)))
167968571be9SUladzislau Rezki (Sony) 		return false;
168068571be9SUladzislau Rezki (Sony) 
168196e2db45SUladzislau Rezki (Sony) 	start = min(start,
168296e2db45SUladzislau Rezki (Sony) 		list_first_entry(&local_pure_list,
168396e2db45SUladzislau Rezki (Sony) 			struct vmap_area, list)->va_start);
168496e2db45SUladzislau Rezki (Sony) 
168596e2db45SUladzislau Rezki (Sony) 	end = max(end,
168696e2db45SUladzislau Rezki (Sony) 		list_last_entry(&local_pure_list,
168796e2db45SUladzislau Rezki (Sony) 			struct vmap_area, list)->va_end);
1688db64fe02SNick Piggin 
16890574ecd1SChristoph Hellwig 	flush_tlb_kernel_range(start, end);
16904d36e6f8SUladzislau Rezki (Sony) 	resched_threshold = lazy_max_pages() << 1;
1691db64fe02SNick Piggin 
1692e36176beSUladzislau Rezki (Sony) 	spin_lock(&free_vmap_area_lock);
169396e2db45SUladzislau Rezki (Sony) 	list_for_each_entry_safe(va, n_va, &local_pure_list, list) {
16944d36e6f8SUladzislau Rezki (Sony) 		unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
16953c5c3cfbSDaniel Axtens 		unsigned long orig_start = va->va_start;
16963c5c3cfbSDaniel Axtens 		unsigned long orig_end = va->va_end;
1697763b218dSJoel Fernandes 
1698dd3b8353SUladzislau Rezki (Sony) 		/*
1699dd3b8353SUladzislau Rezki (Sony) 		 * Finally insert or merge lazily-freed area. It is
1700dd3b8353SUladzislau Rezki (Sony) 		 * detached and there is no need to "unlink" it from
1701dd3b8353SUladzislau Rezki (Sony) 		 * anything.
1702dd3b8353SUladzislau Rezki (Sony) 		 */
170396e2db45SUladzislau Rezki (Sony) 		va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root,
17043c5c3cfbSDaniel Axtens 				&free_vmap_area_list);
17053c5c3cfbSDaniel Axtens 
17069c801f61SUladzislau Rezki (Sony) 		if (!va)
17079c801f61SUladzislau Rezki (Sony) 			continue;
17089c801f61SUladzislau Rezki (Sony) 
17093c5c3cfbSDaniel Axtens 		if (is_vmalloc_or_module_addr((void *)orig_start))
17103c5c3cfbSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
17113c5c3cfbSDaniel Axtens 					      va->va_start, va->va_end);
1712dd3b8353SUladzislau Rezki (Sony) 
17134d36e6f8SUladzislau Rezki (Sony) 		atomic_long_sub(nr, &vmap_lazy_nr);
171468571be9SUladzislau Rezki (Sony) 
17154d36e6f8SUladzislau Rezki (Sony) 		if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
1716e36176beSUladzislau Rezki (Sony) 			cond_resched_lock(&free_vmap_area_lock);
1717763b218dSJoel Fernandes 	}
1718e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
17190574ecd1SChristoph Hellwig 	return true;
1720db64fe02SNick Piggin }
1721db64fe02SNick Piggin 
1722db64fe02SNick Piggin /*
1723496850e5SNick Piggin  * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
1724496850e5SNick Piggin  * is already purging.
1725496850e5SNick Piggin  */
1726496850e5SNick Piggin static void try_purge_vmap_area_lazy(void)
1727496850e5SNick Piggin {
1728f9e09977SChristoph Hellwig 	if (mutex_trylock(&vmap_purge_lock)) {
17290574ecd1SChristoph Hellwig 		__purge_vmap_area_lazy(ULONG_MAX, 0);
1730f9e09977SChristoph Hellwig 		mutex_unlock(&vmap_purge_lock);
17310574ecd1SChristoph Hellwig 	}
1732496850e5SNick Piggin }
1733496850e5SNick Piggin 
1734496850e5SNick Piggin /*
1735db64fe02SNick Piggin  * Kick off a purge of the outstanding lazy areas.
1736db64fe02SNick Piggin  */
1737db64fe02SNick Piggin static void purge_vmap_area_lazy(void)
1738db64fe02SNick Piggin {
1739f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
17400574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
17410574ecd1SChristoph Hellwig 	__purge_vmap_area_lazy(ULONG_MAX, 0);
1742f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
1743db64fe02SNick Piggin }
1744db64fe02SNick Piggin 
1745db64fe02SNick Piggin /*
174664141da5SJeremy Fitzhardinge  * Free a vmap area, caller ensuring that the area has been unmapped
174764141da5SJeremy Fitzhardinge  * and flush_cache_vunmap had been called for the correct range
174864141da5SJeremy Fitzhardinge  * previously.
1749db64fe02SNick Piggin  */
175064141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va)
1751db64fe02SNick Piggin {
17524d36e6f8SUladzislau Rezki (Sony) 	unsigned long nr_lazy;
175380c4bd7aSChris Wilson 
1754dd3b8353SUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
1755dd3b8353SUladzislau Rezki (Sony) 	unlink_va(va, &vmap_area_root);
1756dd3b8353SUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
1757dd3b8353SUladzislau Rezki (Sony) 
17584d36e6f8SUladzislau Rezki (Sony) 	nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
17594d36e6f8SUladzislau Rezki (Sony) 				PAGE_SHIFT, &vmap_lazy_nr);
176080c4bd7aSChris Wilson 
176196e2db45SUladzislau Rezki (Sony) 	/*
176296e2db45SUladzislau Rezki (Sony) 	 * Merge or place it to the purge tree/list.
176396e2db45SUladzislau Rezki (Sony) 	 */
176496e2db45SUladzislau Rezki (Sony) 	spin_lock(&purge_vmap_area_lock);
176596e2db45SUladzislau Rezki (Sony) 	merge_or_add_vmap_area(va,
176696e2db45SUladzislau Rezki (Sony) 		&purge_vmap_area_root, &purge_vmap_area_list);
176796e2db45SUladzislau Rezki (Sony) 	spin_unlock(&purge_vmap_area_lock);
176880c4bd7aSChris Wilson 
176996e2db45SUladzislau Rezki (Sony) 	/* After this point, we may free va at any time */
177080c4bd7aSChris Wilson 	if (unlikely(nr_lazy > lazy_max_pages()))
1771496850e5SNick Piggin 		try_purge_vmap_area_lazy();
1772db64fe02SNick Piggin }
1773db64fe02SNick Piggin 
1774b29acbdcSNick Piggin /*
1775b29acbdcSNick Piggin  * Free and unmap a vmap area
1776b29acbdcSNick Piggin  */
1777b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va)
1778b29acbdcSNick Piggin {
1779b29acbdcSNick Piggin 	flush_cache_vunmap(va->va_start, va->va_end);
17804ad0ae8cSNicholas Piggin 	vunmap_range_noflush(va->va_start, va->va_end);
17818e57f8acSVlastimil Babka 	if (debug_pagealloc_enabled_static())
178282a2e924SChintan Pandya 		flush_tlb_kernel_range(va->va_start, va->va_end);
178382a2e924SChintan Pandya 
1784c8eef01eSChristoph Hellwig 	free_vmap_area_noflush(va);
1785b29acbdcSNick Piggin }
1786b29acbdcSNick Piggin 
1787db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr)
1788db64fe02SNick Piggin {
1789db64fe02SNick Piggin 	struct vmap_area *va;
1790db64fe02SNick Piggin 
1791db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
1792db64fe02SNick Piggin 	va = __find_vmap_area(addr);
1793db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
1794db64fe02SNick Piggin 
1795db64fe02SNick Piggin 	return va;
1796db64fe02SNick Piggin }
1797db64fe02SNick Piggin 
1798db64fe02SNick Piggin /*** Per cpu kva allocator ***/
1799db64fe02SNick Piggin 
1800db64fe02SNick Piggin /*
1801db64fe02SNick Piggin  * vmap space is limited especially on 32 bit architectures. Ensure there is
1802db64fe02SNick Piggin  * room for at least 16 percpu vmap blocks per CPU.
1803db64fe02SNick Piggin  */
1804db64fe02SNick Piggin /*
1805db64fe02SNick Piggin  * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
1806db64fe02SNick Piggin  * to #define VMALLOC_SPACE		(VMALLOC_END-VMALLOC_START). Guess
1807db64fe02SNick Piggin  * instead (we just need a rough idea)
1808db64fe02SNick Piggin  */
1809db64fe02SNick Piggin #if BITS_PER_LONG == 32
1810db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024)
1811db64fe02SNick Piggin #else
1812db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024*1024)
1813db64fe02SNick Piggin #endif
1814db64fe02SNick Piggin 
1815db64fe02SNick Piggin #define VMALLOC_PAGES		(VMALLOC_SPACE / PAGE_SIZE)
1816db64fe02SNick Piggin #define VMAP_MAX_ALLOC		BITS_PER_LONG	/* 256K with 4K pages */
1817db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX	1024	/* 4MB with 4K pages */
1818db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN	(VMAP_MAX_ALLOC*2)
1819db64fe02SNick Piggin #define VMAP_MIN(x, y)		((x) < (y) ? (x) : (y)) /* can't use min() */
1820db64fe02SNick Piggin #define VMAP_MAX(x, y)		((x) > (y) ? (x) : (y)) /* can't use max() */
1821f982f915SClemens Ladisch #define VMAP_BBMAP_BITS		\
1822f982f915SClemens Ladisch 		VMAP_MIN(VMAP_BBMAP_BITS_MAX,	\
1823db64fe02SNick Piggin 		VMAP_MAX(VMAP_BBMAP_BITS_MIN,	\
1824f982f915SClemens Ladisch 			VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
1825db64fe02SNick Piggin 
1826db64fe02SNick Piggin #define VMAP_BLOCK_SIZE		(VMAP_BBMAP_BITS * PAGE_SIZE)
1827db64fe02SNick Piggin 
1828db64fe02SNick Piggin struct vmap_block_queue {
1829db64fe02SNick Piggin 	spinlock_t lock;
1830db64fe02SNick Piggin 	struct list_head free;
1831db64fe02SNick Piggin };
1832db64fe02SNick Piggin 
1833db64fe02SNick Piggin struct vmap_block {
1834db64fe02SNick Piggin 	spinlock_t lock;
1835db64fe02SNick Piggin 	struct vmap_area *va;
1836db64fe02SNick Piggin 	unsigned long free, dirty;
18377d61bfe8SRoman Pen 	unsigned long dirty_min, dirty_max; /*< dirty range */
1838db64fe02SNick Piggin 	struct list_head free_list;
1839db64fe02SNick Piggin 	struct rcu_head rcu_head;
184002b709dfSNick Piggin 	struct list_head purge;
1841db64fe02SNick Piggin };
1842db64fe02SNick Piggin 
1843db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1844db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1845db64fe02SNick Piggin 
1846db64fe02SNick Piggin /*
18470f14599cSMatthew Wilcox (Oracle)  * XArray of vmap blocks, indexed by address, to quickly find a vmap block
1848db64fe02SNick Piggin  * in the free path. Could get rid of this if we change the API to return a
1849db64fe02SNick Piggin  * "cookie" from alloc, to be passed to free. But no big deal yet.
1850db64fe02SNick Piggin  */
18510f14599cSMatthew Wilcox (Oracle) static DEFINE_XARRAY(vmap_blocks);
1852db64fe02SNick Piggin 
1853db64fe02SNick Piggin /*
1854db64fe02SNick Piggin  * We should probably have a fallback mechanism to allocate virtual memory
1855db64fe02SNick Piggin  * out of partially filled vmap blocks. However vmap block sizing should be
1856db64fe02SNick Piggin  * fairly reasonable according to the vmalloc size, so it shouldn't be a
1857db64fe02SNick Piggin  * big problem.
1858db64fe02SNick Piggin  */
1859db64fe02SNick Piggin 
1860db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr)
1861db64fe02SNick Piggin {
1862db64fe02SNick Piggin 	addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
1863db64fe02SNick Piggin 	addr /= VMAP_BLOCK_SIZE;
1864db64fe02SNick Piggin 	return addr;
1865db64fe02SNick Piggin }
1866db64fe02SNick Piggin 
1867cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
1868cf725ce2SRoman Pen {
1869cf725ce2SRoman Pen 	unsigned long addr;
1870cf725ce2SRoman Pen 
1871cf725ce2SRoman Pen 	addr = va_start + (pages_off << PAGE_SHIFT);
1872cf725ce2SRoman Pen 	BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
1873cf725ce2SRoman Pen 	return (void *)addr;
1874cf725ce2SRoman Pen }
1875cf725ce2SRoman Pen 
1876cf725ce2SRoman Pen /**
1877cf725ce2SRoman Pen  * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
1878cf725ce2SRoman Pen  *                  block. Of course pages number can't exceed VMAP_BBMAP_BITS
1879cf725ce2SRoman Pen  * @order:    how many 2^order pages should be occupied in newly allocated block
1880cf725ce2SRoman Pen  * @gfp_mask: flags for the page level allocator
1881cf725ce2SRoman Pen  *
1882a862f68aSMike Rapoport  * Return: virtual address in a newly allocated block or ERR_PTR(-errno)
1883cf725ce2SRoman Pen  */
1884cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
1885db64fe02SNick Piggin {
1886db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
1887db64fe02SNick Piggin 	struct vmap_block *vb;
1888db64fe02SNick Piggin 	struct vmap_area *va;
1889db64fe02SNick Piggin 	unsigned long vb_idx;
1890db64fe02SNick Piggin 	int node, err;
1891cf725ce2SRoman Pen 	void *vaddr;
1892db64fe02SNick Piggin 
1893db64fe02SNick Piggin 	node = numa_node_id();
1894db64fe02SNick Piggin 
1895db64fe02SNick Piggin 	vb = kmalloc_node(sizeof(struct vmap_block),
1896db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
1897db64fe02SNick Piggin 	if (unlikely(!vb))
1898db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
1899db64fe02SNick Piggin 
1900db64fe02SNick Piggin 	va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
1901db64fe02SNick Piggin 					VMALLOC_START, VMALLOC_END,
1902db64fe02SNick Piggin 					node, gfp_mask);
1903ddf9c6d4STobias Klauser 	if (IS_ERR(va)) {
1904db64fe02SNick Piggin 		kfree(vb);
1905e7d86340SJulia Lawall 		return ERR_CAST(va);
1906db64fe02SNick Piggin 	}
1907db64fe02SNick Piggin 
1908cf725ce2SRoman Pen 	vaddr = vmap_block_vaddr(va->va_start, 0);
1909db64fe02SNick Piggin 	spin_lock_init(&vb->lock);
1910db64fe02SNick Piggin 	vb->va = va;
1911cf725ce2SRoman Pen 	/* At least something should be left free */
1912cf725ce2SRoman Pen 	BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
1913cf725ce2SRoman Pen 	vb->free = VMAP_BBMAP_BITS - (1UL << order);
1914db64fe02SNick Piggin 	vb->dirty = 0;
19157d61bfe8SRoman Pen 	vb->dirty_min = VMAP_BBMAP_BITS;
19167d61bfe8SRoman Pen 	vb->dirty_max = 0;
1917db64fe02SNick Piggin 	INIT_LIST_HEAD(&vb->free_list);
1918db64fe02SNick Piggin 
1919db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(va->va_start);
19200f14599cSMatthew Wilcox (Oracle) 	err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask);
19210f14599cSMatthew Wilcox (Oracle) 	if (err) {
19220f14599cSMatthew Wilcox (Oracle) 		kfree(vb);
19230f14599cSMatthew Wilcox (Oracle) 		free_vmap_area(va);
19240f14599cSMatthew Wilcox (Oracle) 		return ERR_PTR(err);
19250f14599cSMatthew Wilcox (Oracle) 	}
1926db64fe02SNick Piggin 
1927db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
1928db64fe02SNick Piggin 	spin_lock(&vbq->lock);
192968ac546fSRoman Pen 	list_add_tail_rcu(&vb->free_list, &vbq->free);
1930db64fe02SNick Piggin 	spin_unlock(&vbq->lock);
19313f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
1932db64fe02SNick Piggin 
1933cf725ce2SRoman Pen 	return vaddr;
1934db64fe02SNick Piggin }
1935db64fe02SNick Piggin 
1936db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb)
1937db64fe02SNick Piggin {
1938db64fe02SNick Piggin 	struct vmap_block *tmp;
1939db64fe02SNick Piggin 
19400f14599cSMatthew Wilcox (Oracle) 	tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start));
1941db64fe02SNick Piggin 	BUG_ON(tmp != vb);
1942db64fe02SNick Piggin 
194364141da5SJeremy Fitzhardinge 	free_vmap_area_noflush(vb->va);
194422a3c7d1SLai Jiangshan 	kfree_rcu(vb, rcu_head);
1945db64fe02SNick Piggin }
1946db64fe02SNick Piggin 
194702b709dfSNick Piggin static void purge_fragmented_blocks(int cpu)
194802b709dfSNick Piggin {
194902b709dfSNick Piggin 	LIST_HEAD(purge);
195002b709dfSNick Piggin 	struct vmap_block *vb;
195102b709dfSNick Piggin 	struct vmap_block *n_vb;
195202b709dfSNick Piggin 	struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
195302b709dfSNick Piggin 
195402b709dfSNick Piggin 	rcu_read_lock();
195502b709dfSNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
195602b709dfSNick Piggin 
195702b709dfSNick Piggin 		if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
195802b709dfSNick Piggin 			continue;
195902b709dfSNick Piggin 
196002b709dfSNick Piggin 		spin_lock(&vb->lock);
196102b709dfSNick Piggin 		if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
196202b709dfSNick Piggin 			vb->free = 0; /* prevent further allocs after releasing lock */
196302b709dfSNick Piggin 			vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
19647d61bfe8SRoman Pen 			vb->dirty_min = 0;
19657d61bfe8SRoman Pen 			vb->dirty_max = VMAP_BBMAP_BITS;
196602b709dfSNick Piggin 			spin_lock(&vbq->lock);
196702b709dfSNick Piggin 			list_del_rcu(&vb->free_list);
196802b709dfSNick Piggin 			spin_unlock(&vbq->lock);
196902b709dfSNick Piggin 			spin_unlock(&vb->lock);
197002b709dfSNick Piggin 			list_add_tail(&vb->purge, &purge);
197102b709dfSNick Piggin 		} else
197202b709dfSNick Piggin 			spin_unlock(&vb->lock);
197302b709dfSNick Piggin 	}
197402b709dfSNick Piggin 	rcu_read_unlock();
197502b709dfSNick Piggin 
197602b709dfSNick Piggin 	list_for_each_entry_safe(vb, n_vb, &purge, purge) {
197702b709dfSNick Piggin 		list_del(&vb->purge);
197802b709dfSNick Piggin 		free_vmap_block(vb);
197902b709dfSNick Piggin 	}
198002b709dfSNick Piggin }
198102b709dfSNick Piggin 
198202b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void)
198302b709dfSNick Piggin {
198402b709dfSNick Piggin 	int cpu;
198502b709dfSNick Piggin 
198602b709dfSNick Piggin 	for_each_possible_cpu(cpu)
198702b709dfSNick Piggin 		purge_fragmented_blocks(cpu);
198802b709dfSNick Piggin }
198902b709dfSNick Piggin 
1990db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
1991db64fe02SNick Piggin {
1992db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
1993db64fe02SNick Piggin 	struct vmap_block *vb;
1994cf725ce2SRoman Pen 	void *vaddr = NULL;
1995db64fe02SNick Piggin 	unsigned int order;
1996db64fe02SNick Piggin 
1997891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
1998db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
1999aa91c4d8SJan Kara 	if (WARN_ON(size == 0)) {
2000aa91c4d8SJan Kara 		/*
2001aa91c4d8SJan Kara 		 * Allocating 0 bytes isn't what caller wants since
2002aa91c4d8SJan Kara 		 * get_order(0) returns funny result. Just warn and terminate
2003aa91c4d8SJan Kara 		 * early.
2004aa91c4d8SJan Kara 		 */
2005aa91c4d8SJan Kara 		return NULL;
2006aa91c4d8SJan Kara 	}
2007db64fe02SNick Piggin 	order = get_order(size);
2008db64fe02SNick Piggin 
2009db64fe02SNick Piggin 	rcu_read_lock();
2010db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
2011db64fe02SNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
2012cf725ce2SRoman Pen 		unsigned long pages_off;
2013db64fe02SNick Piggin 
2014db64fe02SNick Piggin 		spin_lock(&vb->lock);
2015cf725ce2SRoman Pen 		if (vb->free < (1UL << order)) {
2016cf725ce2SRoman Pen 			spin_unlock(&vb->lock);
2017cf725ce2SRoman Pen 			continue;
2018cf725ce2SRoman Pen 		}
201902b709dfSNick Piggin 
2020cf725ce2SRoman Pen 		pages_off = VMAP_BBMAP_BITS - vb->free;
2021cf725ce2SRoman Pen 		vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
2022db64fe02SNick Piggin 		vb->free -= 1UL << order;
2023db64fe02SNick Piggin 		if (vb->free == 0) {
2024db64fe02SNick Piggin 			spin_lock(&vbq->lock);
2025de560423SNick Piggin 			list_del_rcu(&vb->free_list);
2026db64fe02SNick Piggin 			spin_unlock(&vbq->lock);
2027db64fe02SNick Piggin 		}
2028cf725ce2SRoman Pen 
2029db64fe02SNick Piggin 		spin_unlock(&vb->lock);
2030db64fe02SNick Piggin 		break;
2031db64fe02SNick Piggin 	}
203202b709dfSNick Piggin 
20333f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
2034db64fe02SNick Piggin 	rcu_read_unlock();
2035db64fe02SNick Piggin 
2036cf725ce2SRoman Pen 	/* Allocate new block if nothing was found */
2037cf725ce2SRoman Pen 	if (!vaddr)
2038cf725ce2SRoman Pen 		vaddr = new_vmap_block(order, gfp_mask);
2039db64fe02SNick Piggin 
2040cf725ce2SRoman Pen 	return vaddr;
2041db64fe02SNick Piggin }
2042db64fe02SNick Piggin 
204378a0e8c4SChristoph Hellwig static void vb_free(unsigned long addr, unsigned long size)
2044db64fe02SNick Piggin {
2045db64fe02SNick Piggin 	unsigned long offset;
2046db64fe02SNick Piggin 	unsigned int order;
2047db64fe02SNick Piggin 	struct vmap_block *vb;
2048db64fe02SNick Piggin 
2049891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
2050db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
2051b29acbdcSNick Piggin 
205278a0e8c4SChristoph Hellwig 	flush_cache_vunmap(addr, addr + size);
2053b29acbdcSNick Piggin 
2054db64fe02SNick Piggin 	order = get_order(size);
205578a0e8c4SChristoph Hellwig 	offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT;
20560f14599cSMatthew Wilcox (Oracle) 	vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr));
2057db64fe02SNick Piggin 
20584ad0ae8cSNicholas Piggin 	vunmap_range_noflush(addr, addr + size);
205964141da5SJeremy Fitzhardinge 
20608e57f8acSVlastimil Babka 	if (debug_pagealloc_enabled_static())
206178a0e8c4SChristoph Hellwig 		flush_tlb_kernel_range(addr, addr + size);
206282a2e924SChintan Pandya 
2063db64fe02SNick Piggin 	spin_lock(&vb->lock);
20647d61bfe8SRoman Pen 
20657d61bfe8SRoman Pen 	/* Expand dirty range */
20667d61bfe8SRoman Pen 	vb->dirty_min = min(vb->dirty_min, offset);
20677d61bfe8SRoman Pen 	vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
2068d086817dSMinChan Kim 
2069db64fe02SNick Piggin 	vb->dirty += 1UL << order;
2070db64fe02SNick Piggin 	if (vb->dirty == VMAP_BBMAP_BITS) {
2071de560423SNick Piggin 		BUG_ON(vb->free);
2072db64fe02SNick Piggin 		spin_unlock(&vb->lock);
2073db64fe02SNick Piggin 		free_vmap_block(vb);
2074db64fe02SNick Piggin 	} else
2075db64fe02SNick Piggin 		spin_unlock(&vb->lock);
2076db64fe02SNick Piggin }
2077db64fe02SNick Piggin 
2078868b104dSRick Edgecombe static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
2079db64fe02SNick Piggin {
2080db64fe02SNick Piggin 	int cpu;
2081db64fe02SNick Piggin 
20829b463334SJeremy Fitzhardinge 	if (unlikely(!vmap_initialized))
20839b463334SJeremy Fitzhardinge 		return;
20849b463334SJeremy Fitzhardinge 
20855803ed29SChristoph Hellwig 	might_sleep();
20865803ed29SChristoph Hellwig 
2087db64fe02SNick Piggin 	for_each_possible_cpu(cpu) {
2088db64fe02SNick Piggin 		struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
2089db64fe02SNick Piggin 		struct vmap_block *vb;
2090db64fe02SNick Piggin 
2091db64fe02SNick Piggin 		rcu_read_lock();
2092db64fe02SNick Piggin 		list_for_each_entry_rcu(vb, &vbq->free, free_list) {
2093db64fe02SNick Piggin 			spin_lock(&vb->lock);
2094ad216c03SVijayanand Jitta 			if (vb->dirty && vb->dirty != VMAP_BBMAP_BITS) {
20957d61bfe8SRoman Pen 				unsigned long va_start = vb->va->va_start;
2096db64fe02SNick Piggin 				unsigned long s, e;
2097b136be5eSJoonsoo Kim 
20987d61bfe8SRoman Pen 				s = va_start + (vb->dirty_min << PAGE_SHIFT);
20997d61bfe8SRoman Pen 				e = va_start + (vb->dirty_max << PAGE_SHIFT);
2100db64fe02SNick Piggin 
21017d61bfe8SRoman Pen 				start = min(s, start);
21027d61bfe8SRoman Pen 				end   = max(e, end);
21037d61bfe8SRoman Pen 
2104db64fe02SNick Piggin 				flush = 1;
2105db64fe02SNick Piggin 			}
2106db64fe02SNick Piggin 			spin_unlock(&vb->lock);
2107db64fe02SNick Piggin 		}
2108db64fe02SNick Piggin 		rcu_read_unlock();
2109db64fe02SNick Piggin 	}
2110db64fe02SNick Piggin 
2111f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
21120574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
21130574ecd1SChristoph Hellwig 	if (!__purge_vmap_area_lazy(start, end) && flush)
21140574ecd1SChristoph Hellwig 		flush_tlb_kernel_range(start, end);
2115f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
2116db64fe02SNick Piggin }
2117868b104dSRick Edgecombe 
2118868b104dSRick Edgecombe /**
2119868b104dSRick Edgecombe  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
2120868b104dSRick Edgecombe  *
2121868b104dSRick Edgecombe  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
2122868b104dSRick Edgecombe  * to amortize TLB flushing overheads. What this means is that any page you
2123868b104dSRick Edgecombe  * have now, may, in a former life, have been mapped into kernel virtual
2124868b104dSRick Edgecombe  * address by the vmap layer and so there might be some CPUs with TLB entries
2125868b104dSRick Edgecombe  * still referencing that page (additional to the regular 1:1 kernel mapping).
2126868b104dSRick Edgecombe  *
2127868b104dSRick Edgecombe  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
2128868b104dSRick Edgecombe  * be sure that none of the pages we have control over will have any aliases
2129868b104dSRick Edgecombe  * from the vmap layer.
2130868b104dSRick Edgecombe  */
2131868b104dSRick Edgecombe void vm_unmap_aliases(void)
2132868b104dSRick Edgecombe {
2133868b104dSRick Edgecombe 	unsigned long start = ULONG_MAX, end = 0;
2134868b104dSRick Edgecombe 	int flush = 0;
2135868b104dSRick Edgecombe 
2136868b104dSRick Edgecombe 	_vm_unmap_aliases(start, end, flush);
2137868b104dSRick Edgecombe }
2138db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases);
2139db64fe02SNick Piggin 
2140db64fe02SNick Piggin /**
2141db64fe02SNick Piggin  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
2142db64fe02SNick Piggin  * @mem: the pointer returned by vm_map_ram
2143db64fe02SNick Piggin  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
2144db64fe02SNick Piggin  */
2145db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count)
2146db64fe02SNick Piggin {
214765ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
2148db64fe02SNick Piggin 	unsigned long addr = (unsigned long)mem;
21499c3acf60SChristoph Hellwig 	struct vmap_area *va;
2150db64fe02SNick Piggin 
21515803ed29SChristoph Hellwig 	might_sleep();
2152db64fe02SNick Piggin 	BUG_ON(!addr);
2153db64fe02SNick Piggin 	BUG_ON(addr < VMALLOC_START);
2154db64fe02SNick Piggin 	BUG_ON(addr > VMALLOC_END);
2155a1c0b1a0SShawn Lin 	BUG_ON(!PAGE_ALIGNED(addr));
2156db64fe02SNick Piggin 
2157d98c9e83SAndrey Ryabinin 	kasan_poison_vmalloc(mem, size);
2158d98c9e83SAndrey Ryabinin 
21599c3acf60SChristoph Hellwig 	if (likely(count <= VMAP_MAX_ALLOC)) {
216005e3ff95SChintan Pandya 		debug_check_no_locks_freed(mem, size);
216178a0e8c4SChristoph Hellwig 		vb_free(addr, size);
21629c3acf60SChristoph Hellwig 		return;
21639c3acf60SChristoph Hellwig 	}
21649c3acf60SChristoph Hellwig 
21659c3acf60SChristoph Hellwig 	va = find_vmap_area(addr);
21669c3acf60SChristoph Hellwig 	BUG_ON(!va);
216705e3ff95SChintan Pandya 	debug_check_no_locks_freed((void *)va->va_start,
216805e3ff95SChintan Pandya 				    (va->va_end - va->va_start));
21699c3acf60SChristoph Hellwig 	free_unmap_vmap_area(va);
2170db64fe02SNick Piggin }
2171db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram);
2172db64fe02SNick Piggin 
2173db64fe02SNick Piggin /**
2174db64fe02SNick Piggin  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
2175db64fe02SNick Piggin  * @pages: an array of pointers to the pages to be mapped
2176db64fe02SNick Piggin  * @count: number of pages
2177db64fe02SNick Piggin  * @node: prefer to allocate data structures on this node
2178e99c97adSRandy Dunlap  *
217936437638SGioh Kim  * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
218036437638SGioh Kim  * faster than vmap so it's good.  But if you mix long-life and short-life
218136437638SGioh Kim  * objects with vm_map_ram(), it could consume lots of address space through
218236437638SGioh Kim  * fragmentation (especially on a 32bit machine).  You could see failures in
218336437638SGioh Kim  * the end.  Please use this function for short-lived objects.
218436437638SGioh Kim  *
2185e99c97adSRandy Dunlap  * Returns: a pointer to the address that has been mapped, or %NULL on failure
2186db64fe02SNick Piggin  */
2187d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node)
2188db64fe02SNick Piggin {
218965ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
2190db64fe02SNick Piggin 	unsigned long addr;
2191db64fe02SNick Piggin 	void *mem;
2192db64fe02SNick Piggin 
2193db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC)) {
2194db64fe02SNick Piggin 		mem = vb_alloc(size, GFP_KERNEL);
2195db64fe02SNick Piggin 		if (IS_ERR(mem))
2196db64fe02SNick Piggin 			return NULL;
2197db64fe02SNick Piggin 		addr = (unsigned long)mem;
2198db64fe02SNick Piggin 	} else {
2199db64fe02SNick Piggin 		struct vmap_area *va;
2200db64fe02SNick Piggin 		va = alloc_vmap_area(size, PAGE_SIZE,
2201db64fe02SNick Piggin 				VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
2202db64fe02SNick Piggin 		if (IS_ERR(va))
2203db64fe02SNick Piggin 			return NULL;
2204db64fe02SNick Piggin 
2205db64fe02SNick Piggin 		addr = va->va_start;
2206db64fe02SNick Piggin 		mem = (void *)addr;
2207db64fe02SNick Piggin 	}
2208d98c9e83SAndrey Ryabinin 
2209d98c9e83SAndrey Ryabinin 	kasan_unpoison_vmalloc(mem, size);
2210d98c9e83SAndrey Ryabinin 
2211b67177ecSNicholas Piggin 	if (vmap_pages_range(addr, addr + size, PAGE_KERNEL,
2212b67177ecSNicholas Piggin 				pages, PAGE_SHIFT) < 0) {
2213db64fe02SNick Piggin 		vm_unmap_ram(mem, count);
2214db64fe02SNick Piggin 		return NULL;
2215db64fe02SNick Piggin 	}
2216b67177ecSNicholas Piggin 
2217db64fe02SNick Piggin 	return mem;
2218db64fe02SNick Piggin }
2219db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram);
2220db64fe02SNick Piggin 
22214341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata;
222292eac168SMike Rapoport 
2223121e6f32SNicholas Piggin static inline unsigned int vm_area_page_order(struct vm_struct *vm)
2224121e6f32SNicholas Piggin {
2225121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
2226121e6f32SNicholas Piggin 	return vm->page_order;
2227121e6f32SNicholas Piggin #else
2228121e6f32SNicholas Piggin 	return 0;
2229121e6f32SNicholas Piggin #endif
2230121e6f32SNicholas Piggin }
2231121e6f32SNicholas Piggin 
2232121e6f32SNicholas Piggin static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int order)
2233121e6f32SNicholas Piggin {
2234121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
2235121e6f32SNicholas Piggin 	vm->page_order = order;
2236121e6f32SNicholas Piggin #else
2237121e6f32SNicholas Piggin 	BUG_ON(order != 0);
2238121e6f32SNicholas Piggin #endif
2239121e6f32SNicholas Piggin }
2240121e6f32SNicholas Piggin 
2241f0aa6617STejun Heo /**
2242be9b7335SNicolas Pitre  * vm_area_add_early - add vmap area early during boot
2243be9b7335SNicolas Pitre  * @vm: vm_struct to add
2244be9b7335SNicolas Pitre  *
2245be9b7335SNicolas Pitre  * This function is used to add fixed kernel vm area to vmlist before
2246be9b7335SNicolas Pitre  * vmalloc_init() is called.  @vm->addr, @vm->size, and @vm->flags
2247be9b7335SNicolas Pitre  * should contain proper values and the other fields should be zero.
2248be9b7335SNicolas Pitre  *
2249be9b7335SNicolas Pitre  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
2250be9b7335SNicolas Pitre  */
2251be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm)
2252be9b7335SNicolas Pitre {
2253be9b7335SNicolas Pitre 	struct vm_struct *tmp, **p;
2254be9b7335SNicolas Pitre 
2255be9b7335SNicolas Pitre 	BUG_ON(vmap_initialized);
2256be9b7335SNicolas Pitre 	for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
2257be9b7335SNicolas Pitre 		if (tmp->addr >= vm->addr) {
2258be9b7335SNicolas Pitre 			BUG_ON(tmp->addr < vm->addr + vm->size);
2259be9b7335SNicolas Pitre 			break;
2260be9b7335SNicolas Pitre 		} else
2261be9b7335SNicolas Pitre 			BUG_ON(tmp->addr + tmp->size > vm->addr);
2262be9b7335SNicolas Pitre 	}
2263be9b7335SNicolas Pitre 	vm->next = *p;
2264be9b7335SNicolas Pitre 	*p = vm;
2265be9b7335SNicolas Pitre }
2266be9b7335SNicolas Pitre 
2267be9b7335SNicolas Pitre /**
2268f0aa6617STejun Heo  * vm_area_register_early - register vmap area early during boot
2269f0aa6617STejun Heo  * @vm: vm_struct to register
2270c0c0a293STejun Heo  * @align: requested alignment
2271f0aa6617STejun Heo  *
2272f0aa6617STejun Heo  * This function is used to register kernel vm area before
2273f0aa6617STejun Heo  * vmalloc_init() is called.  @vm->size and @vm->flags should contain
2274f0aa6617STejun Heo  * proper values on entry and other fields should be zero.  On return,
2275f0aa6617STejun Heo  * vm->addr contains the allocated address.
2276f0aa6617STejun Heo  *
2277f0aa6617STejun Heo  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
2278f0aa6617STejun Heo  */
2279c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align)
2280f0aa6617STejun Heo {
22810eb68437SKefeng Wang 	unsigned long addr = ALIGN(VMALLOC_START, align);
22820eb68437SKefeng Wang 	struct vm_struct *cur, **p;
2283f0aa6617STejun Heo 
22840eb68437SKefeng Wang 	BUG_ON(vmap_initialized);
2285c0c0a293STejun Heo 
22860eb68437SKefeng Wang 	for (p = &vmlist; (cur = *p) != NULL; p = &cur->next) {
22870eb68437SKefeng Wang 		if ((unsigned long)cur->addr - addr >= vm->size)
22880eb68437SKefeng Wang 			break;
22890eb68437SKefeng Wang 		addr = ALIGN((unsigned long)cur->addr + cur->size, align);
22900eb68437SKefeng Wang 	}
22910eb68437SKefeng Wang 
22920eb68437SKefeng Wang 	BUG_ON(addr > VMALLOC_END - vm->size);
2293c0c0a293STejun Heo 	vm->addr = (void *)addr;
22940eb68437SKefeng Wang 	vm->next = *p;
22950eb68437SKefeng Wang 	*p = vm;
22963252b1d8SKefeng Wang 	kasan_populate_early_vm_area_shadow(vm->addr, vm->size);
2297f0aa6617STejun Heo }
2298f0aa6617STejun Heo 
229968ad4a33SUladzislau Rezki (Sony) static void vmap_init_free_space(void)
230068ad4a33SUladzislau Rezki (Sony) {
230168ad4a33SUladzislau Rezki (Sony) 	unsigned long vmap_start = 1;
230268ad4a33SUladzislau Rezki (Sony) 	const unsigned long vmap_end = ULONG_MAX;
230368ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *busy, *free;
230468ad4a33SUladzislau Rezki (Sony) 
230568ad4a33SUladzislau Rezki (Sony) 	/*
230668ad4a33SUladzislau Rezki (Sony) 	 *     B     F     B     B     B     F
230768ad4a33SUladzislau Rezki (Sony) 	 * -|-----|.....|-----|-----|-----|.....|-
230868ad4a33SUladzislau Rezki (Sony) 	 *  |           The KVA space           |
230968ad4a33SUladzislau Rezki (Sony) 	 *  |<--------------------------------->|
231068ad4a33SUladzislau Rezki (Sony) 	 */
231168ad4a33SUladzislau Rezki (Sony) 	list_for_each_entry(busy, &vmap_area_list, list) {
231268ad4a33SUladzislau Rezki (Sony) 		if (busy->va_start - vmap_start > 0) {
231368ad4a33SUladzislau Rezki (Sony) 			free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
231468ad4a33SUladzislau Rezki (Sony) 			if (!WARN_ON_ONCE(!free)) {
231568ad4a33SUladzislau Rezki (Sony) 				free->va_start = vmap_start;
231668ad4a33SUladzislau Rezki (Sony) 				free->va_end = busy->va_start;
231768ad4a33SUladzislau Rezki (Sony) 
231868ad4a33SUladzislau Rezki (Sony) 				insert_vmap_area_augment(free, NULL,
231968ad4a33SUladzislau Rezki (Sony) 					&free_vmap_area_root,
232068ad4a33SUladzislau Rezki (Sony) 						&free_vmap_area_list);
232168ad4a33SUladzislau Rezki (Sony) 			}
232268ad4a33SUladzislau Rezki (Sony) 		}
232368ad4a33SUladzislau Rezki (Sony) 
232468ad4a33SUladzislau Rezki (Sony) 		vmap_start = busy->va_end;
232568ad4a33SUladzislau Rezki (Sony) 	}
232668ad4a33SUladzislau Rezki (Sony) 
232768ad4a33SUladzislau Rezki (Sony) 	if (vmap_end - vmap_start > 0) {
232868ad4a33SUladzislau Rezki (Sony) 		free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
232968ad4a33SUladzislau Rezki (Sony) 		if (!WARN_ON_ONCE(!free)) {
233068ad4a33SUladzislau Rezki (Sony) 			free->va_start = vmap_start;
233168ad4a33SUladzislau Rezki (Sony) 			free->va_end = vmap_end;
233268ad4a33SUladzislau Rezki (Sony) 
233368ad4a33SUladzislau Rezki (Sony) 			insert_vmap_area_augment(free, NULL,
233468ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_root,
233568ad4a33SUladzislau Rezki (Sony) 					&free_vmap_area_list);
233668ad4a33SUladzislau Rezki (Sony) 		}
233768ad4a33SUladzislau Rezki (Sony) 	}
233868ad4a33SUladzislau Rezki (Sony) }
233968ad4a33SUladzislau Rezki (Sony) 
2340db64fe02SNick Piggin void __init vmalloc_init(void)
2341db64fe02SNick Piggin {
2342822c18f2SIvan Kokshaysky 	struct vmap_area *va;
2343822c18f2SIvan Kokshaysky 	struct vm_struct *tmp;
2344db64fe02SNick Piggin 	int i;
2345db64fe02SNick Piggin 
234668ad4a33SUladzislau Rezki (Sony) 	/*
234768ad4a33SUladzislau Rezki (Sony) 	 * Create the cache for vmap_area objects.
234868ad4a33SUladzislau Rezki (Sony) 	 */
234968ad4a33SUladzislau Rezki (Sony) 	vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
235068ad4a33SUladzislau Rezki (Sony) 
2351db64fe02SNick Piggin 	for_each_possible_cpu(i) {
2352db64fe02SNick Piggin 		struct vmap_block_queue *vbq;
235332fcfd40SAl Viro 		struct vfree_deferred *p;
2354db64fe02SNick Piggin 
2355db64fe02SNick Piggin 		vbq = &per_cpu(vmap_block_queue, i);
2356db64fe02SNick Piggin 		spin_lock_init(&vbq->lock);
2357db64fe02SNick Piggin 		INIT_LIST_HEAD(&vbq->free);
235832fcfd40SAl Viro 		p = &per_cpu(vfree_deferred, i);
235932fcfd40SAl Viro 		init_llist_head(&p->list);
236032fcfd40SAl Viro 		INIT_WORK(&p->wq, free_work);
2361db64fe02SNick Piggin 	}
23629b463334SJeremy Fitzhardinge 
2363822c18f2SIvan Kokshaysky 	/* Import existing vmlist entries. */
2364822c18f2SIvan Kokshaysky 	for (tmp = vmlist; tmp; tmp = tmp->next) {
236568ad4a33SUladzislau Rezki (Sony) 		va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
236668ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(!va))
236768ad4a33SUladzislau Rezki (Sony) 			continue;
236868ad4a33SUladzislau Rezki (Sony) 
2369822c18f2SIvan Kokshaysky 		va->va_start = (unsigned long)tmp->addr;
2370822c18f2SIvan Kokshaysky 		va->va_end = va->va_start + tmp->size;
2371dbda591dSKyongHo 		va->vm = tmp;
237268ad4a33SUladzislau Rezki (Sony) 		insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
2373822c18f2SIvan Kokshaysky 	}
2374ca23e405STejun Heo 
237568ad4a33SUladzislau Rezki (Sony) 	/*
237668ad4a33SUladzislau Rezki (Sony) 	 * Now we can initialize a free vmap space.
237768ad4a33SUladzislau Rezki (Sony) 	 */
237868ad4a33SUladzislau Rezki (Sony) 	vmap_init_free_space();
23799b463334SJeremy Fitzhardinge 	vmap_initialized = true;
2380db64fe02SNick Piggin }
2381db64fe02SNick Piggin 
2382e36176beSUladzislau Rezki (Sony) static inline void setup_vmalloc_vm_locked(struct vm_struct *vm,
2383e36176beSUladzislau Rezki (Sony) 	struct vmap_area *va, unsigned long flags, const void *caller)
2384cf88c790STejun Heo {
2385cf88c790STejun Heo 	vm->flags = flags;
2386cf88c790STejun Heo 	vm->addr = (void *)va->va_start;
2387cf88c790STejun Heo 	vm->size = va->va_end - va->va_start;
2388cf88c790STejun Heo 	vm->caller = caller;
2389db1aecafSMinchan Kim 	va->vm = vm;
2390e36176beSUladzislau Rezki (Sony) }
2391e36176beSUladzislau Rezki (Sony) 
2392e36176beSUladzislau Rezki (Sony) static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
2393e36176beSUladzislau Rezki (Sony) 			      unsigned long flags, const void *caller)
2394e36176beSUladzislau Rezki (Sony) {
2395e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
2396e36176beSUladzislau Rezki (Sony) 	setup_vmalloc_vm_locked(vm, va, flags, caller);
2397c69480adSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2398f5252e00SMitsuo Hayasaka }
2399cf88c790STejun Heo 
240020fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm)
2401f5252e00SMitsuo Hayasaka {
2402d4033afdSJoonsoo Kim 	/*
240320fc02b4SZhang Yanfei 	 * Before removing VM_UNINITIALIZED,
2404d4033afdSJoonsoo Kim 	 * we should make sure that vm has proper values.
2405d4033afdSJoonsoo Kim 	 * Pair with smp_rmb() in show_numa_info().
2406d4033afdSJoonsoo Kim 	 */
2407d4033afdSJoonsoo Kim 	smp_wmb();
240820fc02b4SZhang Yanfei 	vm->flags &= ~VM_UNINITIALIZED;
2409cf88c790STejun Heo }
2410cf88c790STejun Heo 
2411db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size,
24127ca3027bSDaniel Axtens 		unsigned long align, unsigned long shift, unsigned long flags,
24137ca3027bSDaniel Axtens 		unsigned long start, unsigned long end, int node,
24147ca3027bSDaniel Axtens 		gfp_t gfp_mask, const void *caller)
2415db64fe02SNick Piggin {
24160006526dSKautuk Consul 	struct vmap_area *va;
2417db64fe02SNick Piggin 	struct vm_struct *area;
2418d98c9e83SAndrey Ryabinin 	unsigned long requested_size = size;
24191da177e4SLinus Torvalds 
242052fd24caSGiridhar Pemmasani 	BUG_ON(in_interrupt());
24217ca3027bSDaniel Axtens 	size = ALIGN(size, 1ul << shift);
242231be8309SOGAWA Hirofumi 	if (unlikely(!size))
242331be8309SOGAWA Hirofumi 		return NULL;
24241da177e4SLinus Torvalds 
2425252e5c6eSzijun_hu 	if (flags & VM_IOREMAP)
2426252e5c6eSzijun_hu 		align = 1ul << clamp_t(int, get_count_order_long(size),
2427252e5c6eSzijun_hu 				       PAGE_SHIFT, IOREMAP_MAX_ORDER);
2428252e5c6eSzijun_hu 
2429cf88c790STejun Heo 	area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
24301da177e4SLinus Torvalds 	if (unlikely(!area))
24311da177e4SLinus Torvalds 		return NULL;
24321da177e4SLinus Torvalds 
243371394fe5SAndrey Ryabinin 	if (!(flags & VM_NO_GUARD))
24341da177e4SLinus Torvalds 		size += PAGE_SIZE;
24351da177e4SLinus Torvalds 
2436db64fe02SNick Piggin 	va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
2437db64fe02SNick Piggin 	if (IS_ERR(va)) {
2438db64fe02SNick Piggin 		kfree(area);
2439db64fe02SNick Piggin 		return NULL;
24401da177e4SLinus Torvalds 	}
24411da177e4SLinus Torvalds 
2442d98c9e83SAndrey Ryabinin 	kasan_unpoison_vmalloc((void *)va->va_start, requested_size);
2443f5252e00SMitsuo Hayasaka 
2444d98c9e83SAndrey Ryabinin 	setup_vmalloc_vm(area, va, flags, caller);
24453c5c3cfbSDaniel Axtens 
24461da177e4SLinus Torvalds 	return area;
24471da177e4SLinus Torvalds }
24481da177e4SLinus Torvalds 
2449c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
2450c2968612SBenjamin Herrenschmidt 				       unsigned long start, unsigned long end,
24515e6cafc8SMarek Szyprowski 				       const void *caller)
2452c2968612SBenjamin Herrenschmidt {
24537ca3027bSDaniel Axtens 	return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, start, end,
24547ca3027bSDaniel Axtens 				  NUMA_NO_NODE, GFP_KERNEL, caller);
2455c2968612SBenjamin Herrenschmidt }
2456c2968612SBenjamin Herrenschmidt 
24571da177e4SLinus Torvalds /**
2458183ff22bSSimon Arlott  * get_vm_area - reserve a contiguous kernel virtual area
24591da177e4SLinus Torvalds  * @size:	 size of the area
24601da177e4SLinus Torvalds  * @flags:	 %VM_IOREMAP for I/O mappings or VM_ALLOC
24611da177e4SLinus Torvalds  *
24621da177e4SLinus Torvalds  * Search an area of @size in the kernel virtual mapping area,
24631da177e4SLinus Torvalds  * and reserved it for out purposes.  Returns the area descriptor
24641da177e4SLinus Torvalds  * on success or %NULL on failure.
2465a862f68aSMike Rapoport  *
2466a862f68aSMike Rapoport  * Return: the area descriptor on success or %NULL on failure.
24671da177e4SLinus Torvalds  */
24681da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
24691da177e4SLinus Torvalds {
24707ca3027bSDaniel Axtens 	return __get_vm_area_node(size, 1, PAGE_SHIFT, flags,
24717ca3027bSDaniel Axtens 				  VMALLOC_START, VMALLOC_END,
247200ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL,
247300ef2d2fSDavid Rientjes 				  __builtin_return_address(0));
247423016969SChristoph Lameter }
247523016969SChristoph Lameter 
247623016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
24775e6cafc8SMarek Szyprowski 				const void *caller)
247823016969SChristoph Lameter {
24797ca3027bSDaniel Axtens 	return __get_vm_area_node(size, 1, PAGE_SHIFT, flags,
24807ca3027bSDaniel Axtens 				  VMALLOC_START, VMALLOC_END,
248100ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL, caller);
24821da177e4SLinus Torvalds }
24831da177e4SLinus Torvalds 
2484e9da6e99SMarek Szyprowski /**
2485e9da6e99SMarek Szyprowski  * find_vm_area - find a continuous kernel virtual area
2486e9da6e99SMarek Szyprowski  * @addr:	  base address
2487e9da6e99SMarek Szyprowski  *
2488e9da6e99SMarek Szyprowski  * Search for the kernel VM area starting at @addr, and return it.
2489e9da6e99SMarek Szyprowski  * It is up to the caller to do all required locking to keep the returned
2490e9da6e99SMarek Szyprowski  * pointer valid.
2491a862f68aSMike Rapoport  *
249274640617SHui Su  * Return: the area descriptor on success or %NULL on failure.
2493e9da6e99SMarek Szyprowski  */
2494e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr)
249583342314SNick Piggin {
2496db64fe02SNick Piggin 	struct vmap_area *va;
249783342314SNick Piggin 
2498db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
2499688fcbfcSPengfei Li 	if (!va)
25007856dfebSAndi Kleen 		return NULL;
2501688fcbfcSPengfei Li 
2502688fcbfcSPengfei Li 	return va->vm;
25037856dfebSAndi Kleen }
25047856dfebSAndi Kleen 
25051da177e4SLinus Torvalds /**
2506183ff22bSSimon Arlott  * remove_vm_area - find and remove a continuous kernel virtual area
25071da177e4SLinus Torvalds  * @addr:	    base address
25081da177e4SLinus Torvalds  *
25091da177e4SLinus Torvalds  * Search for the kernel VM area starting at @addr, and remove it.
25101da177e4SLinus Torvalds  * This function returns the found VM area, but using it is NOT safe
25117856dfebSAndi Kleen  * on SMP machines, except for its size or flags.
2512a862f68aSMike Rapoport  *
251374640617SHui Su  * Return: the area descriptor on success or %NULL on failure.
25141da177e4SLinus Torvalds  */
2515b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr)
25161da177e4SLinus Torvalds {
2517db64fe02SNick Piggin 	struct vmap_area *va;
2518db64fe02SNick Piggin 
25195803ed29SChristoph Hellwig 	might_sleep();
25205803ed29SChristoph Hellwig 
2521dd3b8353SUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
2522dd3b8353SUladzislau Rezki (Sony) 	va = __find_vmap_area((unsigned long)addr);
2523688fcbfcSPengfei Li 	if (va && va->vm) {
2524db1aecafSMinchan Kim 		struct vm_struct *vm = va->vm;
2525f5252e00SMitsuo Hayasaka 
2526c69480adSJoonsoo Kim 		va->vm = NULL;
2527c69480adSJoonsoo Kim 		spin_unlock(&vmap_area_lock);
2528c69480adSJoonsoo Kim 
2529a5af5aa8SAndrey Ryabinin 		kasan_free_shadow(vm);
2530dd32c279SKAMEZAWA Hiroyuki 		free_unmap_vmap_area(va);
2531dd32c279SKAMEZAWA Hiroyuki 
2532db64fe02SNick Piggin 		return vm;
2533db64fe02SNick Piggin 	}
2534dd3b8353SUladzislau Rezki (Sony) 
2535dd3b8353SUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
2536db64fe02SNick Piggin 	return NULL;
25371da177e4SLinus Torvalds }
25381da177e4SLinus Torvalds 
2539868b104dSRick Edgecombe static inline void set_area_direct_map(const struct vm_struct *area,
2540868b104dSRick Edgecombe 				       int (*set_direct_map)(struct page *page))
2541868b104dSRick Edgecombe {
2542868b104dSRick Edgecombe 	int i;
2543868b104dSRick Edgecombe 
2544121e6f32SNicholas Piggin 	/* HUGE_VMALLOC passes small pages to set_direct_map */
2545868b104dSRick Edgecombe 	for (i = 0; i < area->nr_pages; i++)
2546868b104dSRick Edgecombe 		if (page_address(area->pages[i]))
2547868b104dSRick Edgecombe 			set_direct_map(area->pages[i]);
2548868b104dSRick Edgecombe }
2549868b104dSRick Edgecombe 
2550868b104dSRick Edgecombe /* Handle removing and resetting vm mappings related to the vm_struct. */
2551868b104dSRick Edgecombe static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages)
2552868b104dSRick Edgecombe {
2553868b104dSRick Edgecombe 	unsigned long start = ULONG_MAX, end = 0;
2554121e6f32SNicholas Piggin 	unsigned int page_order = vm_area_page_order(area);
2555868b104dSRick Edgecombe 	int flush_reset = area->flags & VM_FLUSH_RESET_PERMS;
255631e67340SRick Edgecombe 	int flush_dmap = 0;
2557868b104dSRick Edgecombe 	int i;
2558868b104dSRick Edgecombe 
2559868b104dSRick Edgecombe 	remove_vm_area(area->addr);
2560868b104dSRick Edgecombe 
2561868b104dSRick Edgecombe 	/* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */
2562868b104dSRick Edgecombe 	if (!flush_reset)
2563868b104dSRick Edgecombe 		return;
2564868b104dSRick Edgecombe 
2565868b104dSRick Edgecombe 	/*
2566868b104dSRick Edgecombe 	 * If not deallocating pages, just do the flush of the VM area and
2567868b104dSRick Edgecombe 	 * return.
2568868b104dSRick Edgecombe 	 */
2569868b104dSRick Edgecombe 	if (!deallocate_pages) {
2570868b104dSRick Edgecombe 		vm_unmap_aliases();
2571868b104dSRick Edgecombe 		return;
2572868b104dSRick Edgecombe 	}
2573868b104dSRick Edgecombe 
2574868b104dSRick Edgecombe 	/*
2575868b104dSRick Edgecombe 	 * If execution gets here, flush the vm mapping and reset the direct
2576868b104dSRick Edgecombe 	 * map. Find the start and end range of the direct mappings to make sure
2577868b104dSRick Edgecombe 	 * the vm_unmap_aliases() flush includes the direct map.
2578868b104dSRick Edgecombe 	 */
2579121e6f32SNicholas Piggin 	for (i = 0; i < area->nr_pages; i += 1U << page_order) {
25808e41f872SRick Edgecombe 		unsigned long addr = (unsigned long)page_address(area->pages[i]);
25818e41f872SRick Edgecombe 		if (addr) {
2582121e6f32SNicholas Piggin 			unsigned long page_size;
2583121e6f32SNicholas Piggin 
2584121e6f32SNicholas Piggin 			page_size = PAGE_SIZE << page_order;
2585868b104dSRick Edgecombe 			start = min(addr, start);
2586121e6f32SNicholas Piggin 			end = max(addr + page_size, end);
258731e67340SRick Edgecombe 			flush_dmap = 1;
2588868b104dSRick Edgecombe 		}
2589868b104dSRick Edgecombe 	}
2590868b104dSRick Edgecombe 
2591868b104dSRick Edgecombe 	/*
2592868b104dSRick Edgecombe 	 * Set direct map to something invalid so that it won't be cached if
2593868b104dSRick Edgecombe 	 * there are any accesses after the TLB flush, then flush the TLB and
2594868b104dSRick Edgecombe 	 * reset the direct map permissions to the default.
2595868b104dSRick Edgecombe 	 */
2596868b104dSRick Edgecombe 	set_area_direct_map(area, set_direct_map_invalid_noflush);
259731e67340SRick Edgecombe 	_vm_unmap_aliases(start, end, flush_dmap);
2598868b104dSRick Edgecombe 	set_area_direct_map(area, set_direct_map_default_noflush);
2599868b104dSRick Edgecombe }
2600868b104dSRick Edgecombe 
2601b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages)
26021da177e4SLinus Torvalds {
26031da177e4SLinus Torvalds 	struct vm_struct *area;
26041da177e4SLinus Torvalds 
26051da177e4SLinus Torvalds 	if (!addr)
26061da177e4SLinus Torvalds 		return;
26071da177e4SLinus Torvalds 
2608e69e9d4aSHATAYAMA Daisuke 	if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
2609ab15d9b4SDan Carpenter 			addr))
26101da177e4SLinus Torvalds 		return;
26111da177e4SLinus Torvalds 
26126ade2032SLiviu Dudau 	area = find_vm_area(addr);
26131da177e4SLinus Torvalds 	if (unlikely(!area)) {
26144c8573e2SArjan van de Ven 		WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
26151da177e4SLinus Torvalds 				addr);
26161da177e4SLinus Torvalds 		return;
26171da177e4SLinus Torvalds 	}
26181da177e4SLinus Torvalds 
261905e3ff95SChintan Pandya 	debug_check_no_locks_freed(area->addr, get_vm_area_size(area));
262005e3ff95SChintan Pandya 	debug_check_no_obj_freed(area->addr, get_vm_area_size(area));
26219a11b49aSIngo Molnar 
2622c041098cSVincenzo Frascino 	kasan_poison_vmalloc(area->addr, get_vm_area_size(area));
26233c5c3cfbSDaniel Axtens 
2624868b104dSRick Edgecombe 	vm_remove_mappings(area, deallocate_pages);
2625868b104dSRick Edgecombe 
26261da177e4SLinus Torvalds 	if (deallocate_pages) {
2627121e6f32SNicholas Piggin 		unsigned int page_order = vm_area_page_order(area);
26284e5aa1f4SShakeel Butt 		int i, step = 1U << page_order;
26291da177e4SLinus Torvalds 
26304e5aa1f4SShakeel Butt 		for (i = 0; i < area->nr_pages; i += step) {
2631bf53d6f8SChristoph Lameter 			struct page *page = area->pages[i];
2632bf53d6f8SChristoph Lameter 
2633bf53d6f8SChristoph Lameter 			BUG_ON(!page);
26344e5aa1f4SShakeel Butt 			mod_memcg_page_state(page, MEMCG_VMALLOC, -step);
2635121e6f32SNicholas Piggin 			__free_pages(page, page_order);
2636a850e932SRafael Aquini 			cond_resched();
26371da177e4SLinus Torvalds 		}
263897105f0aSRoman Gushchin 		atomic_long_sub(area->nr_pages, &nr_vmalloc_pages);
26391da177e4SLinus Torvalds 
2640244d63eeSDavid Rientjes 		kvfree(area->pages);
26411da177e4SLinus Torvalds 	}
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds 	kfree(area);
26441da177e4SLinus Torvalds }
26451da177e4SLinus Torvalds 
2646bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr)
2647bf22e37aSAndrey Ryabinin {
2648bf22e37aSAndrey Ryabinin 	/*
2649bf22e37aSAndrey Ryabinin 	 * Use raw_cpu_ptr() because this can be called from preemptible
2650bf22e37aSAndrey Ryabinin 	 * context. Preemption is absolutely fine here, because the llist_add()
2651bf22e37aSAndrey Ryabinin 	 * implementation is lockless, so it works even if we are adding to
265273221d88SJeongtae Park 	 * another cpu's list. schedule_work() should be fine with this too.
2653bf22e37aSAndrey Ryabinin 	 */
2654bf22e37aSAndrey Ryabinin 	struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
2655bf22e37aSAndrey Ryabinin 
2656bf22e37aSAndrey Ryabinin 	if (llist_add((struct llist_node *)addr, &p->list))
2657bf22e37aSAndrey Ryabinin 		schedule_work(&p->wq);
2658bf22e37aSAndrey Ryabinin }
2659bf22e37aSAndrey Ryabinin 
2660bf22e37aSAndrey Ryabinin /**
2661bf22e37aSAndrey Ryabinin  * vfree_atomic - release memory allocated by vmalloc()
2662bf22e37aSAndrey Ryabinin  * @addr:	  memory base address
2663bf22e37aSAndrey Ryabinin  *
2664bf22e37aSAndrey Ryabinin  * This one is just like vfree() but can be called in any atomic context
2665bf22e37aSAndrey Ryabinin  * except NMIs.
2666bf22e37aSAndrey Ryabinin  */
2667bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr)
2668bf22e37aSAndrey Ryabinin {
2669bf22e37aSAndrey Ryabinin 	BUG_ON(in_nmi());
2670bf22e37aSAndrey Ryabinin 
2671bf22e37aSAndrey Ryabinin 	kmemleak_free(addr);
2672bf22e37aSAndrey Ryabinin 
2673bf22e37aSAndrey Ryabinin 	if (!addr)
2674bf22e37aSAndrey Ryabinin 		return;
2675bf22e37aSAndrey Ryabinin 	__vfree_deferred(addr);
2676bf22e37aSAndrey Ryabinin }
2677bf22e37aSAndrey Ryabinin 
2678c67dc624SRoman Penyaev static void __vfree(const void *addr)
2679c67dc624SRoman Penyaev {
2680c67dc624SRoman Penyaev 	if (unlikely(in_interrupt()))
2681c67dc624SRoman Penyaev 		__vfree_deferred(addr);
2682c67dc624SRoman Penyaev 	else
2683c67dc624SRoman Penyaev 		__vunmap(addr, 1);
2684c67dc624SRoman Penyaev }
2685c67dc624SRoman Penyaev 
26861da177e4SLinus Torvalds /**
2687fa307474SMatthew Wilcox (Oracle)  * vfree - Release memory allocated by vmalloc()
2688fa307474SMatthew Wilcox (Oracle)  * @addr:  Memory base address
26891da177e4SLinus Torvalds  *
2690fa307474SMatthew Wilcox (Oracle)  * Free the virtually continuous memory area starting at @addr, as obtained
2691fa307474SMatthew Wilcox (Oracle)  * from one of the vmalloc() family of APIs.  This will usually also free the
2692fa307474SMatthew Wilcox (Oracle)  * physical memory underlying the virtual allocation, but that memory is
2693fa307474SMatthew Wilcox (Oracle)  * reference counted, so it will not be freed until the last user goes away.
26941da177e4SLinus Torvalds  *
2695fa307474SMatthew Wilcox (Oracle)  * If @addr is NULL, no operation is performed.
269632fcfd40SAl Viro  *
2697fa307474SMatthew Wilcox (Oracle)  * Context:
26983ca4ea3aSAndrey Ryabinin  * May sleep if called *not* from interrupt context.
2699fa307474SMatthew Wilcox (Oracle)  * Must not be called in NMI context (strictly speaking, it could be
2700fa307474SMatthew Wilcox (Oracle)  * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
2701f0953a1bSIngo Molnar  * conventions for vfree() arch-dependent would be a really bad idea).
27021da177e4SLinus Torvalds  */
2703b3bdda02SChristoph Lameter void vfree(const void *addr)
27041da177e4SLinus Torvalds {
270532fcfd40SAl Viro 	BUG_ON(in_nmi());
270689219d37SCatalin Marinas 
270789219d37SCatalin Marinas 	kmemleak_free(addr);
270889219d37SCatalin Marinas 
2709a8dda165SAndrey Ryabinin 	might_sleep_if(!in_interrupt());
2710a8dda165SAndrey Ryabinin 
271132fcfd40SAl Viro 	if (!addr)
271232fcfd40SAl Viro 		return;
2713c67dc624SRoman Penyaev 
2714c67dc624SRoman Penyaev 	__vfree(addr);
27151da177e4SLinus Torvalds }
27161da177e4SLinus Torvalds EXPORT_SYMBOL(vfree);
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds /**
27191da177e4SLinus Torvalds  * vunmap - release virtual mapping obtained by vmap()
27201da177e4SLinus Torvalds  * @addr:   memory base address
27211da177e4SLinus Torvalds  *
27221da177e4SLinus Torvalds  * Free the virtually contiguous memory area starting at @addr,
27231da177e4SLinus Torvalds  * which was created from the page array passed to vmap().
27241da177e4SLinus Torvalds  *
272580e93effSPekka Enberg  * Must not be called in interrupt context.
27261da177e4SLinus Torvalds  */
2727b3bdda02SChristoph Lameter void vunmap(const void *addr)
27281da177e4SLinus Torvalds {
27291da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
273034754b69SPeter Zijlstra 	might_sleep();
273132fcfd40SAl Viro 	if (addr)
27321da177e4SLinus Torvalds 		__vunmap(addr, 0);
27331da177e4SLinus Torvalds }
27341da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap);
27351da177e4SLinus Torvalds 
27361da177e4SLinus Torvalds /**
27371da177e4SLinus Torvalds  * vmap - map an array of pages into virtually contiguous space
27381da177e4SLinus Torvalds  * @pages: array of page pointers
27391da177e4SLinus Torvalds  * @count: number of pages to map
27401da177e4SLinus Torvalds  * @flags: vm_area->flags
27411da177e4SLinus Torvalds  * @prot: page protection for the mapping
27421da177e4SLinus Torvalds  *
2743b944afc9SChristoph Hellwig  * Maps @count pages from @pages into contiguous kernel virtual space.
2744b944afc9SChristoph Hellwig  * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself
2745b944afc9SChristoph Hellwig  * (which must be kmalloc or vmalloc memory) and one reference per pages in it
2746b944afc9SChristoph Hellwig  * are transferred from the caller to vmap(), and will be freed / dropped when
2747b944afc9SChristoph Hellwig  * vfree() is called on the return value.
2748a862f68aSMike Rapoport  *
2749a862f68aSMike Rapoport  * Return: the address of the area or %NULL on failure
27501da177e4SLinus Torvalds  */
27511da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count,
27521da177e4SLinus Torvalds 	   unsigned long flags, pgprot_t prot)
27531da177e4SLinus Torvalds {
27541da177e4SLinus Torvalds 	struct vm_struct *area;
2755b67177ecSNicholas Piggin 	unsigned long addr;
275665ee03c4SGuillermo Julián Moreno 	unsigned long size;		/* In bytes */
27571da177e4SLinus Torvalds 
275834754b69SPeter Zijlstra 	might_sleep();
275934754b69SPeter Zijlstra 
2760bd1a8fb2SPeter Zijlstra 	/*
2761bd1a8fb2SPeter Zijlstra 	 * Your top guard is someone else's bottom guard. Not having a top
2762bd1a8fb2SPeter Zijlstra 	 * guard compromises someone else's mappings too.
2763bd1a8fb2SPeter Zijlstra 	 */
2764bd1a8fb2SPeter Zijlstra 	if (WARN_ON_ONCE(flags & VM_NO_GUARD))
2765bd1a8fb2SPeter Zijlstra 		flags &= ~VM_NO_GUARD;
2766bd1a8fb2SPeter Zijlstra 
2767ca79b0c2SArun KS 	if (count > totalram_pages())
27681da177e4SLinus Torvalds 		return NULL;
27691da177e4SLinus Torvalds 
277065ee03c4SGuillermo Julián Moreno 	size = (unsigned long)count << PAGE_SHIFT;
277165ee03c4SGuillermo Julián Moreno 	area = get_vm_area_caller(size, flags, __builtin_return_address(0));
27721da177e4SLinus Torvalds 	if (!area)
27731da177e4SLinus Torvalds 		return NULL;
277423016969SChristoph Lameter 
2775b67177ecSNicholas Piggin 	addr = (unsigned long)area->addr;
2776b67177ecSNicholas Piggin 	if (vmap_pages_range(addr, addr + size, pgprot_nx(prot),
2777b67177ecSNicholas Piggin 				pages, PAGE_SHIFT) < 0) {
27781da177e4SLinus Torvalds 		vunmap(area->addr);
27791da177e4SLinus Torvalds 		return NULL;
27801da177e4SLinus Torvalds 	}
27811da177e4SLinus Torvalds 
2782c22ee528SMiaohe Lin 	if (flags & VM_MAP_PUT_PAGES) {
2783b944afc9SChristoph Hellwig 		area->pages = pages;
2784c22ee528SMiaohe Lin 		area->nr_pages = count;
2785c22ee528SMiaohe Lin 	}
27861da177e4SLinus Torvalds 	return area->addr;
27871da177e4SLinus Torvalds }
27881da177e4SLinus Torvalds EXPORT_SYMBOL(vmap);
27891da177e4SLinus Torvalds 
27903e9a9e25SChristoph Hellwig #ifdef CONFIG_VMAP_PFN
27913e9a9e25SChristoph Hellwig struct vmap_pfn_data {
27923e9a9e25SChristoph Hellwig 	unsigned long	*pfns;
27933e9a9e25SChristoph Hellwig 	pgprot_t	prot;
27943e9a9e25SChristoph Hellwig 	unsigned int	idx;
27953e9a9e25SChristoph Hellwig };
27963e9a9e25SChristoph Hellwig 
27973e9a9e25SChristoph Hellwig static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private)
27983e9a9e25SChristoph Hellwig {
27993e9a9e25SChristoph Hellwig 	struct vmap_pfn_data *data = private;
28003e9a9e25SChristoph Hellwig 
28013e9a9e25SChristoph Hellwig 	if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx])))
28023e9a9e25SChristoph Hellwig 		return -EINVAL;
28033e9a9e25SChristoph Hellwig 	*pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot));
28043e9a9e25SChristoph Hellwig 	return 0;
28053e9a9e25SChristoph Hellwig }
28063e9a9e25SChristoph Hellwig 
28073e9a9e25SChristoph Hellwig /**
28083e9a9e25SChristoph Hellwig  * vmap_pfn - map an array of PFNs into virtually contiguous space
28093e9a9e25SChristoph Hellwig  * @pfns: array of PFNs
28103e9a9e25SChristoph Hellwig  * @count: number of pages to map
28113e9a9e25SChristoph Hellwig  * @prot: page protection for the mapping
28123e9a9e25SChristoph Hellwig  *
28133e9a9e25SChristoph Hellwig  * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns
28143e9a9e25SChristoph Hellwig  * the start address of the mapping.
28153e9a9e25SChristoph Hellwig  */
28163e9a9e25SChristoph Hellwig void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
28173e9a9e25SChristoph Hellwig {
28183e9a9e25SChristoph Hellwig 	struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) };
28193e9a9e25SChristoph Hellwig 	struct vm_struct *area;
28203e9a9e25SChristoph Hellwig 
28213e9a9e25SChristoph Hellwig 	area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP,
28223e9a9e25SChristoph Hellwig 			__builtin_return_address(0));
28233e9a9e25SChristoph Hellwig 	if (!area)
28243e9a9e25SChristoph Hellwig 		return NULL;
28253e9a9e25SChristoph Hellwig 	if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
28263e9a9e25SChristoph Hellwig 			count * PAGE_SIZE, vmap_pfn_apply, &data)) {
28273e9a9e25SChristoph Hellwig 		free_vm_area(area);
28283e9a9e25SChristoph Hellwig 		return NULL;
28293e9a9e25SChristoph Hellwig 	}
28303e9a9e25SChristoph Hellwig 	return area->addr;
28313e9a9e25SChristoph Hellwig }
28323e9a9e25SChristoph Hellwig EXPORT_SYMBOL_GPL(vmap_pfn);
28333e9a9e25SChristoph Hellwig #endif /* CONFIG_VMAP_PFN */
28343e9a9e25SChristoph Hellwig 
283512b9f873SUladzislau Rezki static inline unsigned int
283612b9f873SUladzislau Rezki vm_area_alloc_pages(gfp_t gfp, int nid,
2837343ab817SUladzislau Rezki (Sony) 		unsigned int order, unsigned int nr_pages, struct page **pages)
283812b9f873SUladzislau Rezki {
283912b9f873SUladzislau Rezki 	unsigned int nr_allocated = 0;
2840ffb29b1cSChen Wandun 	struct page *page;
2841ffb29b1cSChen Wandun 	int i;
284212b9f873SUladzislau Rezki 
284312b9f873SUladzislau Rezki 	/*
284412b9f873SUladzislau Rezki 	 * For order-0 pages we make use of bulk allocator, if
284512b9f873SUladzislau Rezki 	 * the page array is partly or not at all populated due
284612b9f873SUladzislau Rezki 	 * to fails, fallback to a single page allocator that is
284712b9f873SUladzislau Rezki 	 * more permissive.
284812b9f873SUladzislau Rezki 	 */
2849c00b6b96SChen Wandun 	if (!order) {
2850*9376130cSMichal Hocko 		gfp_t bulk_gfp = gfp & ~__GFP_NOFAIL;
2851*9376130cSMichal Hocko 
2852343ab817SUladzislau Rezki (Sony) 		while (nr_allocated < nr_pages) {
2853343ab817SUladzislau Rezki (Sony) 			unsigned int nr, nr_pages_request;
2854343ab817SUladzislau Rezki (Sony) 
2855343ab817SUladzislau Rezki (Sony) 			/*
2856343ab817SUladzislau Rezki (Sony) 			 * A maximum allowed request is hard-coded and is 100
2857343ab817SUladzislau Rezki (Sony) 			 * pages per call. That is done in order to prevent a
2858343ab817SUladzislau Rezki (Sony) 			 * long preemption off scenario in the bulk-allocator
2859343ab817SUladzislau Rezki (Sony) 			 * so the range is [1:100].
2860343ab817SUladzislau Rezki (Sony) 			 */
2861343ab817SUladzislau Rezki (Sony) 			nr_pages_request = min(100U, nr_pages - nr_allocated);
2862343ab817SUladzislau Rezki (Sony) 
2863c00b6b96SChen Wandun 			/* memory allocation should consider mempolicy, we can't
2864c00b6b96SChen Wandun 			 * wrongly use nearest node when nid == NUMA_NO_NODE,
2865c00b6b96SChen Wandun 			 * otherwise memory may be allocated in only one node,
2866c00b6b96SChen Wandun 			 * but mempolcy want to alloc memory by interleaving.
2867c00b6b96SChen Wandun 			 */
2868c00b6b96SChen Wandun 			if (IS_ENABLED(CONFIG_NUMA) && nid == NUMA_NO_NODE)
2869*9376130cSMichal Hocko 				nr = alloc_pages_bulk_array_mempolicy(bulk_gfp,
2870c00b6b96SChen Wandun 							nr_pages_request,
2871c00b6b96SChen Wandun 							pages + nr_allocated);
2872c00b6b96SChen Wandun 
2873c00b6b96SChen Wandun 			else
2874*9376130cSMichal Hocko 				nr = alloc_pages_bulk_array_node(bulk_gfp, nid,
2875c00b6b96SChen Wandun 							nr_pages_request,
2876c00b6b96SChen Wandun 							pages + nr_allocated);
2877343ab817SUladzislau Rezki (Sony) 
2878343ab817SUladzislau Rezki (Sony) 			nr_allocated += nr;
2879343ab817SUladzislau Rezki (Sony) 			cond_resched();
2880343ab817SUladzislau Rezki (Sony) 
2881343ab817SUladzislau Rezki (Sony) 			/*
2882343ab817SUladzislau Rezki (Sony) 			 * If zero or pages were obtained partly,
2883343ab817SUladzislau Rezki (Sony) 			 * fallback to a single page allocator.
2884343ab817SUladzislau Rezki (Sony) 			 */
2885343ab817SUladzislau Rezki (Sony) 			if (nr != nr_pages_request)
2886343ab817SUladzislau Rezki (Sony) 				break;
2887343ab817SUladzislau Rezki (Sony) 		}
2888c00b6b96SChen Wandun 	} else
288912b9f873SUladzislau Rezki 		/*
289012b9f873SUladzislau Rezki 		 * Compound pages required for remap_vmalloc_page if
289112b9f873SUladzislau Rezki 		 * high-order pages.
289212b9f873SUladzislau Rezki 		 */
289312b9f873SUladzislau Rezki 		gfp |= __GFP_COMP;
289412b9f873SUladzislau Rezki 
289512b9f873SUladzislau Rezki 	/* High-order pages or fallback path if "bulk" fails. */
289612b9f873SUladzislau Rezki 
2897ffb29b1cSChen Wandun 	while (nr_allocated < nr_pages) {
2898dd544141SVasily Averin 		if (fatal_signal_pending(current))
2899dd544141SVasily Averin 			break;
2900dd544141SVasily Averin 
2901ffb29b1cSChen Wandun 		if (nid == NUMA_NO_NODE)
2902ffb29b1cSChen Wandun 			page = alloc_pages(gfp, order);
2903ffb29b1cSChen Wandun 		else
290412b9f873SUladzislau Rezki 			page = alloc_pages_node(nid, gfp, order);
290512b9f873SUladzislau Rezki 		if (unlikely(!page))
290612b9f873SUladzislau Rezki 			break;
290712b9f873SUladzislau Rezki 
290812b9f873SUladzislau Rezki 		/*
290912b9f873SUladzislau Rezki 		 * Careful, we allocate and map page-order pages, but
291012b9f873SUladzislau Rezki 		 * tracking is done per PAGE_SIZE page so as to keep the
291112b9f873SUladzislau Rezki 		 * vm_struct APIs independent of the physical/mapped size.
291212b9f873SUladzislau Rezki 		 */
291312b9f873SUladzislau Rezki 		for (i = 0; i < (1U << order); i++)
291412b9f873SUladzislau Rezki 			pages[nr_allocated + i] = page + i;
291512b9f873SUladzislau Rezki 
291612b9f873SUladzislau Rezki 		cond_resched();
291712b9f873SUladzislau Rezki 		nr_allocated += 1U << order;
291812b9f873SUladzislau Rezki 	}
291912b9f873SUladzislau Rezki 
292012b9f873SUladzislau Rezki 	return nr_allocated;
292112b9f873SUladzislau Rezki }
292212b9f873SUladzislau Rezki 
2923e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
2924121e6f32SNicholas Piggin 				 pgprot_t prot, unsigned int page_shift,
2925121e6f32SNicholas Piggin 				 int node)
29261da177e4SLinus Torvalds {
2927930f036bSDavid Rientjes 	const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
2928228f778eSVasily Averin 	const gfp_t orig_gfp_mask = gfp_mask;
2929*9376130cSMichal Hocko 	bool nofail = gfp_mask & __GFP_NOFAIL;
2930121e6f32SNicholas Piggin 	unsigned long addr = (unsigned long)area->addr;
2931121e6f32SNicholas Piggin 	unsigned long size = get_vm_area_size(area);
293234fe6537SAndrew Morton 	unsigned long array_size;
2933121e6f32SNicholas Piggin 	unsigned int nr_small_pages = size >> PAGE_SHIFT;
2934121e6f32SNicholas Piggin 	unsigned int page_order;
2935451769ebSMichal Hocko 	unsigned int flags;
2936451769ebSMichal Hocko 	int ret;
29371da177e4SLinus Torvalds 
2938121e6f32SNicholas Piggin 	array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
2939f255935bSChristoph Hellwig 	gfp_mask |= __GFP_NOWARN;
2940f255935bSChristoph Hellwig 	if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
2941f255935bSChristoph Hellwig 		gfp_mask |= __GFP_HIGHMEM;
29421da177e4SLinus Torvalds 
29431da177e4SLinus Torvalds 	/* Please note that the recursion is strictly bounded. */
29448757d5faSJan Kiszka 	if (array_size > PAGE_SIZE) {
29455c1f4e69SUladzislau Rezki (Sony) 		area->pages = __vmalloc_node(array_size, 1, nested_gfp, node,
2946f255935bSChristoph Hellwig 					area->caller);
2947286e1ea3SAndrew Morton 	} else {
29485c1f4e69SUladzislau Rezki (Sony) 		area->pages = kmalloc_node(array_size, nested_gfp, node);
2949286e1ea3SAndrew Morton 	}
29507ea36242SAustin Kim 
29515c1f4e69SUladzislau Rezki (Sony) 	if (!area->pages) {
2952228f778eSVasily Averin 		warn_alloc(orig_gfp_mask, NULL,
2953f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, failed to allocated page array size %lu",
2954d70bec8cSNicholas Piggin 			nr_small_pages * PAGE_SIZE, array_size);
2955cd61413bSUladzislau Rezki (Sony) 		free_vm_area(area);
29561da177e4SLinus Torvalds 		return NULL;
29571da177e4SLinus Torvalds 	}
29581da177e4SLinus Torvalds 
2959121e6f32SNicholas Piggin 	set_vm_area_page_order(area, page_shift - PAGE_SHIFT);
2960121e6f32SNicholas Piggin 	page_order = vm_area_page_order(area);
2961121e6f32SNicholas Piggin 
296212b9f873SUladzislau Rezki 	area->nr_pages = vm_area_alloc_pages(gfp_mask, node,
296312b9f873SUladzislau Rezki 		page_order, nr_small_pages, area->pages);
29645c1f4e69SUladzislau Rezki (Sony) 
296597105f0aSRoman Gushchin 	atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
29664e5aa1f4SShakeel Butt 	if (gfp_mask & __GFP_ACCOUNT) {
29674e5aa1f4SShakeel Butt 		int i, step = 1U << page_order;
29684e5aa1f4SShakeel Butt 
29694e5aa1f4SShakeel Butt 		for (i = 0; i < area->nr_pages; i += step)
29704e5aa1f4SShakeel Butt 			mod_memcg_page_state(area->pages[i], MEMCG_VMALLOC,
29714e5aa1f4SShakeel Butt 					     step);
29724e5aa1f4SShakeel Butt 	}
29735c1f4e69SUladzislau Rezki (Sony) 
29745c1f4e69SUladzislau Rezki (Sony) 	/*
29755c1f4e69SUladzislau Rezki (Sony) 	 * If not enough pages were obtained to accomplish an
29765c1f4e69SUladzislau Rezki (Sony) 	 * allocation request, free them via __vfree() if any.
29775c1f4e69SUladzislau Rezki (Sony) 	 */
29785c1f4e69SUladzislau Rezki (Sony) 	if (area->nr_pages != nr_small_pages) {
2979228f778eSVasily Averin 		warn_alloc(orig_gfp_mask, NULL,
2980f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, page order %u, failed to allocate pages",
2981d70bec8cSNicholas Piggin 			area->nr_pages * PAGE_SIZE, page_order);
29821da177e4SLinus Torvalds 		goto fail;
29831da177e4SLinus Torvalds 	}
2984121e6f32SNicholas Piggin 
2985451769ebSMichal Hocko 	/*
2986451769ebSMichal Hocko 	 * page tables allocations ignore external gfp mask, enforce it
2987451769ebSMichal Hocko 	 * by the scope API
2988451769ebSMichal Hocko 	 */
2989451769ebSMichal Hocko 	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
2990451769ebSMichal Hocko 		flags = memalloc_nofs_save();
2991451769ebSMichal Hocko 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
2992451769ebSMichal Hocko 		flags = memalloc_noio_save();
2993451769ebSMichal Hocko 
2994*9376130cSMichal Hocko 	do {
2995451769ebSMichal Hocko 		ret = vmap_pages_range(addr, addr + size, prot, area->pages,
2996451769ebSMichal Hocko 			page_shift);
2997*9376130cSMichal Hocko 		if (nofail && (ret < 0))
2998*9376130cSMichal Hocko 			schedule_timeout_uninterruptible(1);
2999*9376130cSMichal Hocko 	} while (nofail && (ret < 0));
3000451769ebSMichal Hocko 
3001451769ebSMichal Hocko 	if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
3002451769ebSMichal Hocko 		memalloc_nofs_restore(flags);
3003451769ebSMichal Hocko 	else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
3004451769ebSMichal Hocko 		memalloc_noio_restore(flags);
3005451769ebSMichal Hocko 
3006451769ebSMichal Hocko 	if (ret < 0) {
3007228f778eSVasily Averin 		warn_alloc(orig_gfp_mask, NULL,
3008f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, failed to map pages",
3009d70bec8cSNicholas Piggin 			area->nr_pages * PAGE_SIZE);
30101da177e4SLinus Torvalds 		goto fail;
3011d70bec8cSNicholas Piggin 	}
3012ed1f324cSChristoph Hellwig 
30131da177e4SLinus Torvalds 	return area->addr;
30141da177e4SLinus Torvalds 
30151da177e4SLinus Torvalds fail:
3016c67dc624SRoman Penyaev 	__vfree(area->addr);
30171da177e4SLinus Torvalds 	return NULL;
30181da177e4SLinus Torvalds }
30191da177e4SLinus Torvalds 
3020d0a21265SDavid Rientjes /**
3021d0a21265SDavid Rientjes  * __vmalloc_node_range - allocate virtually contiguous memory
3022d0a21265SDavid Rientjes  * @size:		  allocation size
3023d0a21265SDavid Rientjes  * @align:		  desired alignment
3024d0a21265SDavid Rientjes  * @start:		  vm area range start
3025d0a21265SDavid Rientjes  * @end:		  vm area range end
3026d0a21265SDavid Rientjes  * @gfp_mask:		  flags for the page level allocator
3027d0a21265SDavid Rientjes  * @prot:		  protection mask for the allocated pages
3028cb9e3c29SAndrey Ryabinin  * @vm_flags:		  additional vm area flags (e.g. %VM_NO_GUARD)
302900ef2d2fSDavid Rientjes  * @node:		  node to use for allocation or NUMA_NO_NODE
3030d0a21265SDavid Rientjes  * @caller:		  caller's return address
3031d0a21265SDavid Rientjes  *
3032d0a21265SDavid Rientjes  * Allocate enough pages to cover @size from the page level
3033b7d90e7aSMichal Hocko  * allocator with @gfp_mask flags. Please note that the full set of gfp
3034b7d90e7aSMichal Hocko  * flags are not supported. GFP_KERNEL would be a preferred allocation mode
3035b7d90e7aSMichal Hocko  * but GFP_NOFS and GFP_NOIO are supported as well. Zone modifiers are not
3036b7d90e7aSMichal Hocko  * supported. From the reclaim modifiers__GFP_DIRECT_RECLAIM is required (aka
3037b7d90e7aSMichal Hocko  * GFP_NOWAIT is not supported) and only __GFP_NOFAIL is supported (aka
3038b7d90e7aSMichal Hocko  * __GFP_NORETRY and __GFP_RETRY_MAYFAIL are not supported).
3039b7d90e7aSMichal Hocko  * __GFP_NOWARN can be used to suppress error messages about failures.
3040b7d90e7aSMichal Hocko  *
3041b7d90e7aSMichal Hocko  * Map them into contiguous kernel virtual space, using a pagetable
3042b7d90e7aSMichal Hocko  * protection of @prot.
3043a862f68aSMike Rapoport  *
3044a862f68aSMike Rapoport  * Return: the address of the area or %NULL on failure
3045d0a21265SDavid Rientjes  */
3046d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align,
3047d0a21265SDavid Rientjes 			unsigned long start, unsigned long end, gfp_t gfp_mask,
3048cb9e3c29SAndrey Ryabinin 			pgprot_t prot, unsigned long vm_flags, int node,
3049cb9e3c29SAndrey Ryabinin 			const void *caller)
3050930fc45aSChristoph Lameter {
3051d0a21265SDavid Rientjes 	struct vm_struct *area;
3052d0a21265SDavid Rientjes 	void *addr;
3053d0a21265SDavid Rientjes 	unsigned long real_size = size;
3054121e6f32SNicholas Piggin 	unsigned long real_align = align;
3055121e6f32SNicholas Piggin 	unsigned int shift = PAGE_SHIFT;
3056d0a21265SDavid Rientjes 
3057d70bec8cSNicholas Piggin 	if (WARN_ON_ONCE(!size))
3058d70bec8cSNicholas Piggin 		return NULL;
3059d70bec8cSNicholas Piggin 
3060d70bec8cSNicholas Piggin 	if ((size >> PAGE_SHIFT) > totalram_pages()) {
3061d70bec8cSNicholas Piggin 		warn_alloc(gfp_mask, NULL,
3062f4bdfeafSUladzislau Rezki (Sony) 			"vmalloc error: size %lu, exceeds total pages",
3063f4bdfeafSUladzislau Rezki (Sony) 			real_size);
3064d70bec8cSNicholas Piggin 		return NULL;
3065121e6f32SNicholas Piggin 	}
3066d0a21265SDavid Rientjes 
30673382bbeeSChristophe Leroy 	if (vmap_allow_huge && !(vm_flags & VM_NO_HUGE_VMAP)) {
3068121e6f32SNicholas Piggin 		unsigned long size_per_node;
3069121e6f32SNicholas Piggin 
3070121e6f32SNicholas Piggin 		/*
3071121e6f32SNicholas Piggin 		 * Try huge pages. Only try for PAGE_KERNEL allocations,
3072121e6f32SNicholas Piggin 		 * others like modules don't yet expect huge pages in
3073121e6f32SNicholas Piggin 		 * their allocations due to apply_to_page_range not
3074121e6f32SNicholas Piggin 		 * supporting them.
3075121e6f32SNicholas Piggin 		 */
3076121e6f32SNicholas Piggin 
3077121e6f32SNicholas Piggin 		size_per_node = size;
3078121e6f32SNicholas Piggin 		if (node == NUMA_NO_NODE)
3079121e6f32SNicholas Piggin 			size_per_node /= num_online_nodes();
30803382bbeeSChristophe Leroy 		if (arch_vmap_pmd_supported(prot) && size_per_node >= PMD_SIZE)
3081121e6f32SNicholas Piggin 			shift = PMD_SHIFT;
30823382bbeeSChristophe Leroy 		else
30833382bbeeSChristophe Leroy 			shift = arch_vmap_pte_supported_shift(size_per_node);
30843382bbeeSChristophe Leroy 
3085121e6f32SNicholas Piggin 		align = max(real_align, 1UL << shift);
3086121e6f32SNicholas Piggin 		size = ALIGN(real_size, 1UL << shift);
3087121e6f32SNicholas Piggin 	}
3088121e6f32SNicholas Piggin 
3089121e6f32SNicholas Piggin again:
30907ca3027bSDaniel Axtens 	area = __get_vm_area_node(real_size, align, shift, VM_ALLOC |
30917ca3027bSDaniel Axtens 				  VM_UNINITIALIZED | vm_flags, start, end, node,
30927ca3027bSDaniel Axtens 				  gfp_mask, caller);
3093d70bec8cSNicholas Piggin 	if (!area) {
3094*9376130cSMichal Hocko 		bool nofail = gfp_mask & __GFP_NOFAIL;
3095d70bec8cSNicholas Piggin 		warn_alloc(gfp_mask, NULL,
3096*9376130cSMichal Hocko 			"vmalloc error: size %lu, vm_struct allocation failed%s",
3097*9376130cSMichal Hocko 			real_size, (nofail) ? ". Retrying." : "");
3098*9376130cSMichal Hocko 		if (nofail) {
3099*9376130cSMichal Hocko 			schedule_timeout_uninterruptible(1);
3100*9376130cSMichal Hocko 			goto again;
3101*9376130cSMichal Hocko 		}
3102de7d2b56SJoe Perches 		goto fail;
3103d70bec8cSNicholas Piggin 	}
3104d0a21265SDavid Rientjes 
3105121e6f32SNicholas Piggin 	addr = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
31061368edf0SMel Gorman 	if (!addr)
3107121e6f32SNicholas Piggin 		goto fail;
310889219d37SCatalin Marinas 
310989219d37SCatalin Marinas 	/*
311020fc02b4SZhang Yanfei 	 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
311120fc02b4SZhang Yanfei 	 * flag. It means that vm_struct is not fully initialized.
31124341fa45SJoonsoo Kim 	 * Now, it is fully initialized, so remove this flag here.
3113f5252e00SMitsuo Hayasaka 	 */
311420fc02b4SZhang Yanfei 	clear_vm_uninitialized_flag(area);
3115f5252e00SMitsuo Hayasaka 
31167ca3027bSDaniel Axtens 	size = PAGE_ALIGN(size);
311760115fa5SKefeng Wang 	if (!(vm_flags & VM_DEFER_KMEMLEAK))
311894f4a161SCatalin Marinas 		kmemleak_vmalloc(area, size, gfp_mask);
311989219d37SCatalin Marinas 
312089219d37SCatalin Marinas 	return addr;
3121de7d2b56SJoe Perches 
3122de7d2b56SJoe Perches fail:
3123121e6f32SNicholas Piggin 	if (shift > PAGE_SHIFT) {
3124121e6f32SNicholas Piggin 		shift = PAGE_SHIFT;
3125121e6f32SNicholas Piggin 		align = real_align;
3126121e6f32SNicholas Piggin 		size = real_size;
3127121e6f32SNicholas Piggin 		goto again;
3128121e6f32SNicholas Piggin 	}
3129121e6f32SNicholas Piggin 
3130de7d2b56SJoe Perches 	return NULL;
3131930fc45aSChristoph Lameter }
3132930fc45aSChristoph Lameter 
31331da177e4SLinus Torvalds /**
3134930fc45aSChristoph Lameter  * __vmalloc_node - allocate virtually contiguous memory
31351da177e4SLinus Torvalds  * @size:	    allocation size
31362dca6999SDavid Miller  * @align:	    desired alignment
31371da177e4SLinus Torvalds  * @gfp_mask:	    flags for the page level allocator
313800ef2d2fSDavid Rientjes  * @node:	    node to use for allocation or NUMA_NO_NODE
3139c85d194bSRandy Dunlap  * @caller:	    caller's return address
31401da177e4SLinus Torvalds  *
3141f38fcb9cSChristoph Hellwig  * Allocate enough pages to cover @size from the page level allocator with
3142f38fcb9cSChristoph Hellwig  * @gfp_mask flags.  Map them into contiguous kernel virtual space.
3143a7c3e901SMichal Hocko  *
3144dcda9b04SMichal Hocko  * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
3145a7c3e901SMichal Hocko  * and __GFP_NOFAIL are not supported
3146a7c3e901SMichal Hocko  *
3147a7c3e901SMichal Hocko  * Any use of gfp flags outside of GFP_KERNEL should be consulted
3148a7c3e901SMichal Hocko  * with mm people.
3149a862f68aSMike Rapoport  *
3150a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
31511da177e4SLinus Torvalds  */
31522b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align,
3153f38fcb9cSChristoph Hellwig 			    gfp_t gfp_mask, int node, const void *caller)
31541da177e4SLinus Torvalds {
3155d0a21265SDavid Rientjes 	return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
3156f38fcb9cSChristoph Hellwig 				gfp_mask, PAGE_KERNEL, 0, node, caller);
31571da177e4SLinus Torvalds }
3158c3f896dcSChristoph Hellwig /*
3159c3f896dcSChristoph Hellwig  * This is only for performance analysis of vmalloc and stress purpose.
3160c3f896dcSChristoph Hellwig  * It is required by vmalloc test module, therefore do not use it other
3161c3f896dcSChristoph Hellwig  * than that.
3162c3f896dcSChristoph Hellwig  */
3163c3f896dcSChristoph Hellwig #ifdef CONFIG_TEST_VMALLOC_MODULE
3164c3f896dcSChristoph Hellwig EXPORT_SYMBOL_GPL(__vmalloc_node);
3165c3f896dcSChristoph Hellwig #endif
31661da177e4SLinus Torvalds 
316788dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask)
3168930fc45aSChristoph Lameter {
3169f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE,
317023016969SChristoph Lameter 				__builtin_return_address(0));
3171930fc45aSChristoph Lameter }
31721da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc);
31731da177e4SLinus Torvalds 
31741da177e4SLinus Torvalds /**
31751da177e4SLinus Torvalds  * vmalloc - allocate virtually contiguous memory
31761da177e4SLinus Torvalds  * @size:    allocation size
317792eac168SMike Rapoport  *
31781da177e4SLinus Torvalds  * Allocate enough pages to cover @size from the page level
31791da177e4SLinus Torvalds  * allocator and map them into contiguous kernel virtual space.
31801da177e4SLinus Torvalds  *
3181c1c8897fSMichael Opdenacker  * For tight control over page level allocator and protection flags
31821da177e4SLinus Torvalds  * use __vmalloc() instead.
3183a862f68aSMike Rapoport  *
3184a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
31851da177e4SLinus Torvalds  */
31861da177e4SLinus Torvalds void *vmalloc(unsigned long size)
31871da177e4SLinus Torvalds {
31884d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE,
31894d39d728SChristoph Hellwig 				__builtin_return_address(0));
31901da177e4SLinus Torvalds }
31911da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc);
31921da177e4SLinus Torvalds 
3193930fc45aSChristoph Lameter /**
319415a64f5aSClaudio Imbrenda  * vmalloc_no_huge - allocate virtually contiguous memory using small pages
319515a64f5aSClaudio Imbrenda  * @size:    allocation size
319615a64f5aSClaudio Imbrenda  *
319715a64f5aSClaudio Imbrenda  * Allocate enough non-huge pages to cover @size from the page level
319815a64f5aSClaudio Imbrenda  * allocator and map them into contiguous kernel virtual space.
319915a64f5aSClaudio Imbrenda  *
320015a64f5aSClaudio Imbrenda  * Return: pointer to the allocated memory or %NULL on error
320115a64f5aSClaudio Imbrenda  */
320215a64f5aSClaudio Imbrenda void *vmalloc_no_huge(unsigned long size)
320315a64f5aSClaudio Imbrenda {
320415a64f5aSClaudio Imbrenda 	return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
320515a64f5aSClaudio Imbrenda 				    GFP_KERNEL, PAGE_KERNEL, VM_NO_HUGE_VMAP,
320615a64f5aSClaudio Imbrenda 				    NUMA_NO_NODE, __builtin_return_address(0));
320715a64f5aSClaudio Imbrenda }
320815a64f5aSClaudio Imbrenda EXPORT_SYMBOL(vmalloc_no_huge);
320915a64f5aSClaudio Imbrenda 
321015a64f5aSClaudio Imbrenda /**
3211e1ca7788SDave Young  * vzalloc - allocate virtually contiguous memory with zero fill
3212e1ca7788SDave Young  * @size:    allocation size
321392eac168SMike Rapoport  *
3214e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
3215e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
3216e1ca7788SDave Young  * The memory allocated is set to zero.
3217e1ca7788SDave Young  *
3218e1ca7788SDave Young  * For tight control over page level allocator and protection flags
3219e1ca7788SDave Young  * use __vmalloc() instead.
3220a862f68aSMike Rapoport  *
3221a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
3222e1ca7788SDave Young  */
3223e1ca7788SDave Young void *vzalloc(unsigned long size)
3224e1ca7788SDave Young {
32254d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE,
32264d39d728SChristoph Hellwig 				__builtin_return_address(0));
3227e1ca7788SDave Young }
3228e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
3229e1ca7788SDave Young 
3230e1ca7788SDave Young /**
3231ead04089SRolf Eike Beer  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
323283342314SNick Piggin  * @size: allocation size
3233ead04089SRolf Eike Beer  *
3234ead04089SRolf Eike Beer  * The resulting memory area is zeroed so it can be mapped to userspace
3235ead04089SRolf Eike Beer  * without leaking data.
3236a862f68aSMike Rapoport  *
3237a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
323883342314SNick Piggin  */
323983342314SNick Piggin void *vmalloc_user(unsigned long size)
324083342314SNick Piggin {
3241bc84c535SRoman Penyaev 	return __vmalloc_node_range(size, SHMLBA,  VMALLOC_START, VMALLOC_END,
3242bc84c535SRoman Penyaev 				    GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL,
3243bc84c535SRoman Penyaev 				    VM_USERMAP, NUMA_NO_NODE,
324400ef2d2fSDavid Rientjes 				    __builtin_return_address(0));
324583342314SNick Piggin }
324683342314SNick Piggin EXPORT_SYMBOL(vmalloc_user);
324783342314SNick Piggin 
324883342314SNick Piggin /**
3249930fc45aSChristoph Lameter  * vmalloc_node - allocate memory on a specific node
3250930fc45aSChristoph Lameter  * @size:	  allocation size
3251d44e0780SRandy Dunlap  * @node:	  numa node
3252930fc45aSChristoph Lameter  *
3253930fc45aSChristoph Lameter  * Allocate enough pages to cover @size from the page level
3254930fc45aSChristoph Lameter  * allocator and map them into contiguous kernel virtual space.
3255930fc45aSChristoph Lameter  *
3256c1c8897fSMichael Opdenacker  * For tight control over page level allocator and protection flags
3257930fc45aSChristoph Lameter  * use __vmalloc() instead.
3258a862f68aSMike Rapoport  *
3259a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
3260930fc45aSChristoph Lameter  */
3261930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node)
3262930fc45aSChristoph Lameter {
3263f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL, node,
3264f38fcb9cSChristoph Hellwig 			__builtin_return_address(0));
3265930fc45aSChristoph Lameter }
3266930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node);
3267930fc45aSChristoph Lameter 
3268e1ca7788SDave Young /**
3269e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
3270e1ca7788SDave Young  * @size:	allocation size
3271e1ca7788SDave Young  * @node:	numa node
3272e1ca7788SDave Young  *
3273e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
3274e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
3275e1ca7788SDave Young  * The memory allocated is set to zero.
3276e1ca7788SDave Young  *
3277a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
3278e1ca7788SDave Young  */
3279e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
3280e1ca7788SDave Young {
32814d39d728SChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node,
32824d39d728SChristoph Hellwig 				__builtin_return_address(0));
3283e1ca7788SDave Young }
3284e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
3285e1ca7788SDave Young 
32860d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
3287698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
32880d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
3289698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
32900d08e0d3SAndi Kleen #else
3291698d0831SMichal Hocko /*
3292698d0831SMichal Hocko  * 64b systems should always have either DMA or DMA32 zones. For others
3293698d0831SMichal Hocko  * GFP_DMA32 should do the right thing and use the normal zone.
3294698d0831SMichal Hocko  */
329568d68ff6SZhiyuan Dai #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
32960d08e0d3SAndi Kleen #endif
32970d08e0d3SAndi Kleen 
32981da177e4SLinus Torvalds /**
32991da177e4SLinus Torvalds  * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
33001da177e4SLinus Torvalds  * @size:	allocation size
33011da177e4SLinus Torvalds  *
33021da177e4SLinus Torvalds  * Allocate enough 32bit PA addressable pages to cover @size from the
33031da177e4SLinus Torvalds  * page level allocator and map them into contiguous kernel virtual space.
3304a862f68aSMike Rapoport  *
3305a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
33061da177e4SLinus Torvalds  */
33071da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
33081da177e4SLinus Torvalds {
3309f38fcb9cSChristoph Hellwig 	return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE,
3310f38fcb9cSChristoph Hellwig 			__builtin_return_address(0));
33111da177e4SLinus Torvalds }
33121da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32);
33131da177e4SLinus Torvalds 
331483342314SNick Piggin /**
3315ead04089SRolf Eike Beer  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
331683342314SNick Piggin  * @size:	     allocation size
3317ead04089SRolf Eike Beer  *
3318ead04089SRolf Eike Beer  * The resulting memory area is 32bit addressable and zeroed so it can be
3319ead04089SRolf Eike Beer  * mapped to userspace without leaking data.
3320a862f68aSMike Rapoport  *
3321a862f68aSMike Rapoport  * Return: pointer to the allocated memory or %NULL on error
332283342314SNick Piggin  */
332383342314SNick Piggin void *vmalloc_32_user(unsigned long size)
332483342314SNick Piggin {
3325bc84c535SRoman Penyaev 	return __vmalloc_node_range(size, SHMLBA,  VMALLOC_START, VMALLOC_END,
3326bc84c535SRoman Penyaev 				    GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
3327bc84c535SRoman Penyaev 				    VM_USERMAP, NUMA_NO_NODE,
33285a82ac71SRoman Penyaev 				    __builtin_return_address(0));
332983342314SNick Piggin }
333083342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user);
333183342314SNick Piggin 
3332d0107eb0SKAMEZAWA Hiroyuki /*
3333d0107eb0SKAMEZAWA Hiroyuki  * small helper routine , copy contents to buf from addr.
3334d0107eb0SKAMEZAWA Hiroyuki  * If the page is not present, fill zero.
3335d0107eb0SKAMEZAWA Hiroyuki  */
3336d0107eb0SKAMEZAWA Hiroyuki 
3337d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count)
3338d0107eb0SKAMEZAWA Hiroyuki {
3339d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
3340d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
3341d0107eb0SKAMEZAWA Hiroyuki 
3342d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
3343d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
3344d0107eb0SKAMEZAWA Hiroyuki 
3345891c49abSAlexander Kuleshov 		offset = offset_in_page(addr);
3346d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
3347d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
3348d0107eb0SKAMEZAWA Hiroyuki 			length = count;
3349d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
3350d0107eb0SKAMEZAWA Hiroyuki 		/*
3351d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
3352d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
3353f0953a1bSIngo Molnar 		 * overhead of vmalloc()/vfree() calls for this _debug_
3354d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
3355d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
3356d0107eb0SKAMEZAWA Hiroyuki 		 */
3357d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
3358f7c8ce44SDavid Hildenbrand 			/* We can expect USER0 is not used -- see vread() */
33599b04c5feSCong Wang 			void *map = kmap_atomic(p);
3360d0107eb0SKAMEZAWA Hiroyuki 			memcpy(buf, map + offset, length);
33619b04c5feSCong Wang 			kunmap_atomic(map);
3362d0107eb0SKAMEZAWA Hiroyuki 		} else
3363d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, length);
3364d0107eb0SKAMEZAWA Hiroyuki 
3365d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
3366d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
3367d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
3368d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
3369d0107eb0SKAMEZAWA Hiroyuki 	}
3370d0107eb0SKAMEZAWA Hiroyuki 	return copied;
3371d0107eb0SKAMEZAWA Hiroyuki }
3372d0107eb0SKAMEZAWA Hiroyuki 
3373d0107eb0SKAMEZAWA Hiroyuki /**
3374d0107eb0SKAMEZAWA Hiroyuki  * vread() - read vmalloc area in a safe way.
3375d0107eb0SKAMEZAWA Hiroyuki  * @buf:     buffer for reading data
3376d0107eb0SKAMEZAWA Hiroyuki  * @addr:    vm address.
3377d0107eb0SKAMEZAWA Hiroyuki  * @count:   number of bytes to be read.
3378d0107eb0SKAMEZAWA Hiroyuki  *
3379d0107eb0SKAMEZAWA Hiroyuki  * This function checks that addr is a valid vmalloc'ed area, and
3380d0107eb0SKAMEZAWA Hiroyuki  * copy data from that area to a given buffer. If the given memory range
3381d0107eb0SKAMEZAWA Hiroyuki  * of [addr...addr+count) includes some valid address, data is copied to
3382d0107eb0SKAMEZAWA Hiroyuki  * proper area of @buf. If there are memory holes, they'll be zero-filled.
3383d0107eb0SKAMEZAWA Hiroyuki  * IOREMAP area is treated as memory hole and no copy is done.
3384d0107eb0SKAMEZAWA Hiroyuki  *
3385d0107eb0SKAMEZAWA Hiroyuki  * If [addr...addr+count) doesn't includes any intersects with alive
3386a8e5202dSCong Wang  * vm_struct area, returns 0. @buf should be kernel's buffer.
3387d0107eb0SKAMEZAWA Hiroyuki  *
3388d0107eb0SKAMEZAWA Hiroyuki  * Note: In usual ops, vread() is never necessary because the caller
3389d0107eb0SKAMEZAWA Hiroyuki  * should know vmalloc() area is valid and can use memcpy().
3390d0107eb0SKAMEZAWA Hiroyuki  * This is for routines which have to access vmalloc area without
3391bbcd53c9SDavid Hildenbrand  * any information, as /proc/kcore.
3392a862f68aSMike Rapoport  *
3393a862f68aSMike Rapoport  * Return: number of bytes for which addr and buf should be increased
3394a862f68aSMike Rapoport  * (same number as @count) or %0 if [addr...addr+count) doesn't
3395a862f68aSMike Rapoport  * include any intersection with valid vmalloc area
3396d0107eb0SKAMEZAWA Hiroyuki  */
33971da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
33981da177e4SLinus Torvalds {
3399e81ce85fSJoonsoo Kim 	struct vmap_area *va;
3400e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
34011da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
3402d0107eb0SKAMEZAWA Hiroyuki 	unsigned long buflen = count;
34031da177e4SLinus Torvalds 	unsigned long n;
34041da177e4SLinus Torvalds 
34051da177e4SLinus Torvalds 	/* Don't allow overflow */
34061da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
34071da177e4SLinus Torvalds 		count = -(unsigned long) addr;
34081da177e4SLinus Torvalds 
3409e81ce85fSJoonsoo Kim 	spin_lock(&vmap_area_lock);
3410f181234aSChen Wandun 	va = find_vmap_area_exceed_addr((unsigned long)addr);
3411f608788cSSerapheim Dimitropoulos 	if (!va)
3412f608788cSSerapheim Dimitropoulos 		goto finished;
3413f181234aSChen Wandun 
3414f181234aSChen Wandun 	/* no intersects with alive vmap_area */
3415f181234aSChen Wandun 	if ((unsigned long)addr + count <= va->va_start)
3416f181234aSChen Wandun 		goto finished;
3417f181234aSChen Wandun 
3418f608788cSSerapheim Dimitropoulos 	list_for_each_entry_from(va, &vmap_area_list, list) {
3419e81ce85fSJoonsoo Kim 		if (!count)
3420e81ce85fSJoonsoo Kim 			break;
3421e81ce85fSJoonsoo Kim 
3422688fcbfcSPengfei Li 		if (!va->vm)
3423e81ce85fSJoonsoo Kim 			continue;
3424e81ce85fSJoonsoo Kim 
3425e81ce85fSJoonsoo Kim 		vm = va->vm;
3426e81ce85fSJoonsoo Kim 		vaddr = (char *) vm->addr;
3427762216abSWanpeng Li 		if (addr >= vaddr + get_vm_area_size(vm))
34281da177e4SLinus Torvalds 			continue;
34291da177e4SLinus Torvalds 		while (addr < vaddr) {
34301da177e4SLinus Torvalds 			if (count == 0)
34311da177e4SLinus Torvalds 				goto finished;
34321da177e4SLinus Torvalds 			*buf = '\0';
34331da177e4SLinus Torvalds 			buf++;
34341da177e4SLinus Torvalds 			addr++;
34351da177e4SLinus Torvalds 			count--;
34361da177e4SLinus Torvalds 		}
3437762216abSWanpeng Li 		n = vaddr + get_vm_area_size(vm) - addr;
3438d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
3439d0107eb0SKAMEZAWA Hiroyuki 			n = count;
3440e81ce85fSJoonsoo Kim 		if (!(vm->flags & VM_IOREMAP))
3441d0107eb0SKAMEZAWA Hiroyuki 			aligned_vread(buf, addr, n);
3442d0107eb0SKAMEZAWA Hiroyuki 		else /* IOREMAP area is treated as memory hole */
3443d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, n);
3444d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
3445d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
3446d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
34471da177e4SLinus Torvalds 	}
34481da177e4SLinus Torvalds finished:
3449e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
3450d0107eb0SKAMEZAWA Hiroyuki 
3451d0107eb0SKAMEZAWA Hiroyuki 	if (buf == buf_start)
3452d0107eb0SKAMEZAWA Hiroyuki 		return 0;
3453d0107eb0SKAMEZAWA Hiroyuki 	/* zero-fill memory holes */
3454d0107eb0SKAMEZAWA Hiroyuki 	if (buf != buf_start + buflen)
3455d0107eb0SKAMEZAWA Hiroyuki 		memset(buf, 0, buflen - (buf - buf_start));
3456d0107eb0SKAMEZAWA Hiroyuki 
3457d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
34581da177e4SLinus Torvalds }
34591da177e4SLinus Torvalds 
3460d0107eb0SKAMEZAWA Hiroyuki /**
3461e69e9d4aSHATAYAMA Daisuke  * remap_vmalloc_range_partial - map vmalloc pages to userspace
3462e69e9d4aSHATAYAMA Daisuke  * @vma:		vma to cover
3463e69e9d4aSHATAYAMA Daisuke  * @uaddr:		target user address to start at
3464e69e9d4aSHATAYAMA Daisuke  * @kaddr:		virtual address of vmalloc kernel memory
3465bdebd6a2SJann Horn  * @pgoff:		offset from @kaddr to start at
3466e69e9d4aSHATAYAMA Daisuke  * @size:		size of map area
3467e69e9d4aSHATAYAMA Daisuke  *
3468e69e9d4aSHATAYAMA Daisuke  * Returns:	0 for success, -Exxx on failure
3469e69e9d4aSHATAYAMA Daisuke  *
3470e69e9d4aSHATAYAMA Daisuke  * This function checks that @kaddr is a valid vmalloc'ed area,
3471e69e9d4aSHATAYAMA Daisuke  * and that it is big enough to cover the range starting at
3472e69e9d4aSHATAYAMA Daisuke  * @uaddr in @vma. Will return failure if that criteria isn't
3473e69e9d4aSHATAYAMA Daisuke  * met.
3474e69e9d4aSHATAYAMA Daisuke  *
3475e69e9d4aSHATAYAMA Daisuke  * Similar to remap_pfn_range() (see mm/memory.c)
3476e69e9d4aSHATAYAMA Daisuke  */
3477e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
3478bdebd6a2SJann Horn 				void *kaddr, unsigned long pgoff,
3479bdebd6a2SJann Horn 				unsigned long size)
3480e69e9d4aSHATAYAMA Daisuke {
3481e69e9d4aSHATAYAMA Daisuke 	struct vm_struct *area;
3482bdebd6a2SJann Horn 	unsigned long off;
3483bdebd6a2SJann Horn 	unsigned long end_index;
3484bdebd6a2SJann Horn 
3485bdebd6a2SJann Horn 	if (check_shl_overflow(pgoff, PAGE_SHIFT, &off))
3486bdebd6a2SJann Horn 		return -EINVAL;
3487e69e9d4aSHATAYAMA Daisuke 
3488e69e9d4aSHATAYAMA Daisuke 	size = PAGE_ALIGN(size);
3489e69e9d4aSHATAYAMA Daisuke 
3490e69e9d4aSHATAYAMA Daisuke 	if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
3491e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3492e69e9d4aSHATAYAMA Daisuke 
3493e69e9d4aSHATAYAMA Daisuke 	area = find_vm_area(kaddr);
3494e69e9d4aSHATAYAMA Daisuke 	if (!area)
3495e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3496e69e9d4aSHATAYAMA Daisuke 
3497fe9041c2SChristoph Hellwig 	if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT)))
3498e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3499e69e9d4aSHATAYAMA Daisuke 
3500bdebd6a2SJann Horn 	if (check_add_overflow(size, off, &end_index) ||
3501bdebd6a2SJann Horn 	    end_index > get_vm_area_size(area))
3502e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
3503bdebd6a2SJann Horn 	kaddr += off;
3504e69e9d4aSHATAYAMA Daisuke 
3505e69e9d4aSHATAYAMA Daisuke 	do {
3506e69e9d4aSHATAYAMA Daisuke 		struct page *page = vmalloc_to_page(kaddr);
3507e69e9d4aSHATAYAMA Daisuke 		int ret;
3508e69e9d4aSHATAYAMA Daisuke 
3509e69e9d4aSHATAYAMA Daisuke 		ret = vm_insert_page(vma, uaddr, page);
3510e69e9d4aSHATAYAMA Daisuke 		if (ret)
3511e69e9d4aSHATAYAMA Daisuke 			return ret;
3512e69e9d4aSHATAYAMA Daisuke 
3513e69e9d4aSHATAYAMA Daisuke 		uaddr += PAGE_SIZE;
3514e69e9d4aSHATAYAMA Daisuke 		kaddr += PAGE_SIZE;
3515e69e9d4aSHATAYAMA Daisuke 		size -= PAGE_SIZE;
3516e69e9d4aSHATAYAMA Daisuke 	} while (size > 0);
3517e69e9d4aSHATAYAMA Daisuke 
3518e69e9d4aSHATAYAMA Daisuke 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3519e69e9d4aSHATAYAMA Daisuke 
3520e69e9d4aSHATAYAMA Daisuke 	return 0;
3521e69e9d4aSHATAYAMA Daisuke }
3522e69e9d4aSHATAYAMA Daisuke 
3523e69e9d4aSHATAYAMA Daisuke /**
352483342314SNick Piggin  * remap_vmalloc_range - map vmalloc pages to userspace
352583342314SNick Piggin  * @vma:		vma to cover (map full range of vma)
352683342314SNick Piggin  * @addr:		vmalloc memory
352783342314SNick Piggin  * @pgoff:		number of pages into addr before first page to map
35287682486bSRandy Dunlap  *
35297682486bSRandy Dunlap  * Returns:	0 for success, -Exxx on failure
353083342314SNick Piggin  *
353183342314SNick Piggin  * This function checks that addr is a valid vmalloc'ed area, and
353283342314SNick Piggin  * that it is big enough to cover the vma. Will return failure if
353383342314SNick Piggin  * that criteria isn't met.
353483342314SNick Piggin  *
353572fd4a35SRobert P. J. Day  * Similar to remap_pfn_range() (see mm/memory.c)
353683342314SNick Piggin  */
353783342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
353883342314SNick Piggin 						unsigned long pgoff)
353983342314SNick Piggin {
3540e69e9d4aSHATAYAMA Daisuke 	return remap_vmalloc_range_partial(vma, vma->vm_start,
3541bdebd6a2SJann Horn 					   addr, pgoff,
3542e69e9d4aSHATAYAMA Daisuke 					   vma->vm_end - vma->vm_start);
354383342314SNick Piggin }
354483342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range);
354583342314SNick Piggin 
35465f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area)
35475f4352fbSJeremy Fitzhardinge {
35485f4352fbSJeremy Fitzhardinge 	struct vm_struct *ret;
35495f4352fbSJeremy Fitzhardinge 	ret = remove_vm_area(area->addr);
35505f4352fbSJeremy Fitzhardinge 	BUG_ON(ret != area);
35515f4352fbSJeremy Fitzhardinge 	kfree(area);
35525f4352fbSJeremy Fitzhardinge }
35535f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area);
3554a10aa579SChristoph Lameter 
35554f8b02b4STejun Heo #ifdef CONFIG_SMP
3556ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n)
3557ca23e405STejun Heo {
35584583e773SGeliang Tang 	return rb_entry_safe(n, struct vmap_area, rb_node);
3559ca23e405STejun Heo }
3560ca23e405STejun Heo 
3561ca23e405STejun Heo /**
356268ad4a33SUladzislau Rezki (Sony)  * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
356368ad4a33SUladzislau Rezki (Sony)  * @addr: target address
3564ca23e405STejun Heo  *
356568ad4a33SUladzislau Rezki (Sony)  * Returns: vmap_area if it is found. If there is no such area
356668ad4a33SUladzislau Rezki (Sony)  *   the first highest(reverse order) vmap_area is returned
356768ad4a33SUladzislau Rezki (Sony)  *   i.e. va->va_start < addr && va->va_end < addr or NULL
356868ad4a33SUladzislau Rezki (Sony)  *   if there are no any areas before @addr.
3569ca23e405STejun Heo  */
357068ad4a33SUladzislau Rezki (Sony) static struct vmap_area *
357168ad4a33SUladzislau Rezki (Sony) pvm_find_va_enclose_addr(unsigned long addr)
3572ca23e405STejun Heo {
357368ad4a33SUladzislau Rezki (Sony) 	struct vmap_area *va, *tmp;
357468ad4a33SUladzislau Rezki (Sony) 	struct rb_node *n;
357568ad4a33SUladzislau Rezki (Sony) 
357668ad4a33SUladzislau Rezki (Sony) 	n = free_vmap_area_root.rb_node;
357768ad4a33SUladzislau Rezki (Sony) 	va = NULL;
3578ca23e405STejun Heo 
3579ca23e405STejun Heo 	while (n) {
358068ad4a33SUladzislau Rezki (Sony) 		tmp = rb_entry(n, struct vmap_area, rb_node);
358168ad4a33SUladzislau Rezki (Sony) 		if (tmp->va_start <= addr) {
358268ad4a33SUladzislau Rezki (Sony) 			va = tmp;
358368ad4a33SUladzislau Rezki (Sony) 			if (tmp->va_end >= addr)
3584ca23e405STejun Heo 				break;
3585ca23e405STejun Heo 
358668ad4a33SUladzislau Rezki (Sony) 			n = n->rb_right;
3587ca23e405STejun Heo 		} else {
358868ad4a33SUladzislau Rezki (Sony) 			n = n->rb_left;
3589ca23e405STejun Heo 		}
359068ad4a33SUladzislau Rezki (Sony) 	}
359168ad4a33SUladzislau Rezki (Sony) 
359268ad4a33SUladzislau Rezki (Sony) 	return va;
3593ca23e405STejun Heo }
3594ca23e405STejun Heo 
3595ca23e405STejun Heo /**
359668ad4a33SUladzislau Rezki (Sony)  * pvm_determine_end_from_reverse - find the highest aligned address
359768ad4a33SUladzislau Rezki (Sony)  * of free block below VMALLOC_END
359868ad4a33SUladzislau Rezki (Sony)  * @va:
359968ad4a33SUladzislau Rezki (Sony)  *   in - the VA we start the search(reverse order);
360068ad4a33SUladzislau Rezki (Sony)  *   out - the VA with the highest aligned end address.
3601799fa85dSAlex Shi  * @align: alignment for required highest address
3602ca23e405STejun Heo  *
360368ad4a33SUladzislau Rezki (Sony)  * Returns: determined end address within vmap_area
3604ca23e405STejun Heo  */
360568ad4a33SUladzislau Rezki (Sony) static unsigned long
360668ad4a33SUladzislau Rezki (Sony) pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
3607ca23e405STejun Heo {
360868ad4a33SUladzislau Rezki (Sony) 	unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
3609ca23e405STejun Heo 	unsigned long addr;
3610ca23e405STejun Heo 
361168ad4a33SUladzislau Rezki (Sony) 	if (likely(*va)) {
361268ad4a33SUladzislau Rezki (Sony) 		list_for_each_entry_from_reverse((*va),
361368ad4a33SUladzislau Rezki (Sony) 				&free_vmap_area_list, list) {
361468ad4a33SUladzislau Rezki (Sony) 			addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
361568ad4a33SUladzislau Rezki (Sony) 			if ((*va)->va_start < addr)
361668ad4a33SUladzislau Rezki (Sony) 				return addr;
361768ad4a33SUladzislau Rezki (Sony) 		}
3618ca23e405STejun Heo 	}
3619ca23e405STejun Heo 
362068ad4a33SUladzislau Rezki (Sony) 	return 0;
3621ca23e405STejun Heo }
3622ca23e405STejun Heo 
3623ca23e405STejun Heo /**
3624ca23e405STejun Heo  * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3625ca23e405STejun Heo  * @offsets: array containing offset of each area
3626ca23e405STejun Heo  * @sizes: array containing size of each area
3627ca23e405STejun Heo  * @nr_vms: the number of areas to allocate
3628ca23e405STejun Heo  * @align: alignment, all entries in @offsets and @sizes must be aligned to this
3629ca23e405STejun Heo  *
3630ca23e405STejun Heo  * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3631ca23e405STejun Heo  *	    vm_structs on success, %NULL on failure
3632ca23e405STejun Heo  *
3633ca23e405STejun Heo  * Percpu allocator wants to use congruent vm areas so that it can
3634ca23e405STejun Heo  * maintain the offsets among percpu areas.  This function allocates
3635ec3f64fcSDavid Rientjes  * congruent vmalloc areas for it with GFP_KERNEL.  These areas tend to
3636ec3f64fcSDavid Rientjes  * be scattered pretty far, distance between two areas easily going up
3637ec3f64fcSDavid Rientjes  * to gigabytes.  To avoid interacting with regular vmallocs, these
3638ec3f64fcSDavid Rientjes  * areas are allocated from top.
3639ca23e405STejun Heo  *
3640ca23e405STejun Heo  * Despite its complicated look, this allocator is rather simple. It
364168ad4a33SUladzislau Rezki (Sony)  * does everything top-down and scans free blocks from the end looking
364268ad4a33SUladzislau Rezki (Sony)  * for matching base. While scanning, if any of the areas do not fit the
364368ad4a33SUladzislau Rezki (Sony)  * base address is pulled down to fit the area. Scanning is repeated till
364468ad4a33SUladzislau Rezki (Sony)  * all the areas fit and then all necessary data structures are inserted
364568ad4a33SUladzislau Rezki (Sony)  * and the result is returned.
3646ca23e405STejun Heo  */
3647ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3648ca23e405STejun Heo 				     const size_t *sizes, int nr_vms,
3649ec3f64fcSDavid Rientjes 				     size_t align)
3650ca23e405STejun Heo {
3651ca23e405STejun Heo 	const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3652ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
365368ad4a33SUladzislau Rezki (Sony) 	struct vmap_area **vas, *va;
3654ca23e405STejun Heo 	struct vm_struct **vms;
3655ca23e405STejun Heo 	int area, area2, last_area, term_area;
3656253a496dSDaniel Axtens 	unsigned long base, start, size, end, last_end, orig_start, orig_end;
3657ca23e405STejun Heo 	bool purged = false;
365868ad4a33SUladzislau Rezki (Sony) 	enum fit_type type;
3659ca23e405STejun Heo 
3660ca23e405STejun Heo 	/* verify parameters and allocate data structures */
3661891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(align) || !is_power_of_2(align));
3662ca23e405STejun Heo 	for (last_area = 0, area = 0; area < nr_vms; area++) {
3663ca23e405STejun Heo 		start = offsets[area];
3664ca23e405STejun Heo 		end = start + sizes[area];
3665ca23e405STejun Heo 
3666ca23e405STejun Heo 		/* is everything aligned properly? */
3667ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(offsets[area], align));
3668ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(sizes[area], align));
3669ca23e405STejun Heo 
3670ca23e405STejun Heo 		/* detect the area with the highest address */
3671ca23e405STejun Heo 		if (start > offsets[last_area])
3672ca23e405STejun Heo 			last_area = area;
3673ca23e405STejun Heo 
3674c568da28SWei Yang 		for (area2 = area + 1; area2 < nr_vms; area2++) {
3675ca23e405STejun Heo 			unsigned long start2 = offsets[area2];
3676ca23e405STejun Heo 			unsigned long end2 = start2 + sizes[area2];
3677ca23e405STejun Heo 
3678c568da28SWei Yang 			BUG_ON(start2 < end && start < end2);
3679ca23e405STejun Heo 		}
3680ca23e405STejun Heo 	}
3681ca23e405STejun Heo 	last_end = offsets[last_area] + sizes[last_area];
3682ca23e405STejun Heo 
3683ca23e405STejun Heo 	if (vmalloc_end - vmalloc_start < last_end) {
3684ca23e405STejun Heo 		WARN_ON(true);
3685ca23e405STejun Heo 		return NULL;
3686ca23e405STejun Heo 	}
3687ca23e405STejun Heo 
36884d67d860SThomas Meyer 	vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
36894d67d860SThomas Meyer 	vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
3690ca23e405STejun Heo 	if (!vas || !vms)
3691f1db7afdSKautuk Consul 		goto err_free2;
3692ca23e405STejun Heo 
3693ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
369468ad4a33SUladzislau Rezki (Sony) 		vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
3695ec3f64fcSDavid Rientjes 		vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
3696ca23e405STejun Heo 		if (!vas[area] || !vms[area])
3697ca23e405STejun Heo 			goto err_free;
3698ca23e405STejun Heo 	}
3699ca23e405STejun Heo retry:
3700e36176beSUladzislau Rezki (Sony) 	spin_lock(&free_vmap_area_lock);
3701ca23e405STejun Heo 
3702ca23e405STejun Heo 	/* start scanning - we scan from the top, begin with the last area */
3703ca23e405STejun Heo 	area = term_area = last_area;
3704ca23e405STejun Heo 	start = offsets[area];
3705ca23e405STejun Heo 	end = start + sizes[area];
3706ca23e405STejun Heo 
370768ad4a33SUladzislau Rezki (Sony) 	va = pvm_find_va_enclose_addr(vmalloc_end);
370868ad4a33SUladzislau Rezki (Sony) 	base = pvm_determine_end_from_reverse(&va, align) - end;
3709ca23e405STejun Heo 
3710ca23e405STejun Heo 	while (true) {
3711ca23e405STejun Heo 		/*
3712ca23e405STejun Heo 		 * base might have underflowed, add last_end before
3713ca23e405STejun Heo 		 * comparing.
3714ca23e405STejun Heo 		 */
371568ad4a33SUladzislau Rezki (Sony) 		if (base + last_end < vmalloc_start + last_end)
371668ad4a33SUladzislau Rezki (Sony) 			goto overflow;
3717ca23e405STejun Heo 
3718ca23e405STejun Heo 		/*
371968ad4a33SUladzislau Rezki (Sony) 		 * Fitting base has not been found.
3720ca23e405STejun Heo 		 */
372168ad4a33SUladzislau Rezki (Sony) 		if (va == NULL)
372268ad4a33SUladzislau Rezki (Sony) 			goto overflow;
3723ca23e405STejun Heo 
3724ca23e405STejun Heo 		/*
3725d8cc323dSQiujun Huang 		 * If required width exceeds current VA block, move
37265336e52cSKuppuswamy Sathyanarayanan 		 * base downwards and then recheck.
37275336e52cSKuppuswamy Sathyanarayanan 		 */
37285336e52cSKuppuswamy Sathyanarayanan 		if (base + end > va->va_end) {
37295336e52cSKuppuswamy Sathyanarayanan 			base = pvm_determine_end_from_reverse(&va, align) - end;
37305336e52cSKuppuswamy Sathyanarayanan 			term_area = area;
37315336e52cSKuppuswamy Sathyanarayanan 			continue;
37325336e52cSKuppuswamy Sathyanarayanan 		}
37335336e52cSKuppuswamy Sathyanarayanan 
37345336e52cSKuppuswamy Sathyanarayanan 		/*
373568ad4a33SUladzislau Rezki (Sony) 		 * If this VA does not fit, move base downwards and recheck.
3736ca23e405STejun Heo 		 */
37375336e52cSKuppuswamy Sathyanarayanan 		if (base + start < va->va_start) {
373868ad4a33SUladzislau Rezki (Sony) 			va = node_to_va(rb_prev(&va->rb_node));
373968ad4a33SUladzislau Rezki (Sony) 			base = pvm_determine_end_from_reverse(&va, align) - end;
3740ca23e405STejun Heo 			term_area = area;
3741ca23e405STejun Heo 			continue;
3742ca23e405STejun Heo 		}
3743ca23e405STejun Heo 
3744ca23e405STejun Heo 		/*
3745ca23e405STejun Heo 		 * This area fits, move on to the previous one.  If
3746ca23e405STejun Heo 		 * the previous one is the terminal one, we're done.
3747ca23e405STejun Heo 		 */
3748ca23e405STejun Heo 		area = (area + nr_vms - 1) % nr_vms;
3749ca23e405STejun Heo 		if (area == term_area)
3750ca23e405STejun Heo 			break;
375168ad4a33SUladzislau Rezki (Sony) 
3752ca23e405STejun Heo 		start = offsets[area];
3753ca23e405STejun Heo 		end = start + sizes[area];
375468ad4a33SUladzislau Rezki (Sony) 		va = pvm_find_va_enclose_addr(base + end);
3755ca23e405STejun Heo 	}
375668ad4a33SUladzislau Rezki (Sony) 
3757ca23e405STejun Heo 	/* we've found a fitting base, insert all va's */
3758ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
375968ad4a33SUladzislau Rezki (Sony) 		int ret;
3760ca23e405STejun Heo 
376168ad4a33SUladzislau Rezki (Sony) 		start = base + offsets[area];
376268ad4a33SUladzislau Rezki (Sony) 		size = sizes[area];
376368ad4a33SUladzislau Rezki (Sony) 
376468ad4a33SUladzislau Rezki (Sony) 		va = pvm_find_va_enclose_addr(start);
376568ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(va == NULL))
376668ad4a33SUladzislau Rezki (Sony) 			/* It is a BUG(), but trigger recovery instead. */
376768ad4a33SUladzislau Rezki (Sony) 			goto recovery;
376868ad4a33SUladzislau Rezki (Sony) 
376968ad4a33SUladzislau Rezki (Sony) 		type = classify_va_fit_type(va, start, size);
377068ad4a33SUladzislau Rezki (Sony) 		if (WARN_ON_ONCE(type == NOTHING_FIT))
377168ad4a33SUladzislau Rezki (Sony) 			/* It is a BUG(), but trigger recovery instead. */
377268ad4a33SUladzislau Rezki (Sony) 			goto recovery;
377368ad4a33SUladzislau Rezki (Sony) 
377468ad4a33SUladzislau Rezki (Sony) 		ret = adjust_va_to_fit_type(va, start, size, type);
377568ad4a33SUladzislau Rezki (Sony) 		if (unlikely(ret))
377668ad4a33SUladzislau Rezki (Sony) 			goto recovery;
377768ad4a33SUladzislau Rezki (Sony) 
377868ad4a33SUladzislau Rezki (Sony) 		/* Allocated area. */
377968ad4a33SUladzislau Rezki (Sony) 		va = vas[area];
378068ad4a33SUladzislau Rezki (Sony) 		va->va_start = start;
378168ad4a33SUladzislau Rezki (Sony) 		va->va_end = start + size;
3782ca23e405STejun Heo 	}
3783ca23e405STejun Heo 
3784e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
3785ca23e405STejun Heo 
3786253a496dSDaniel Axtens 	/* populate the kasan shadow space */
3787253a496dSDaniel Axtens 	for (area = 0; area < nr_vms; area++) {
3788253a496dSDaniel Axtens 		if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area]))
3789253a496dSDaniel Axtens 			goto err_free_shadow;
3790253a496dSDaniel Axtens 
3791253a496dSDaniel Axtens 		kasan_unpoison_vmalloc((void *)vas[area]->va_start,
3792253a496dSDaniel Axtens 				       sizes[area]);
3793253a496dSDaniel Axtens 	}
3794253a496dSDaniel Axtens 
3795ca23e405STejun Heo 	/* insert all vm's */
3796e36176beSUladzislau Rezki (Sony) 	spin_lock(&vmap_area_lock);
3797e36176beSUladzislau Rezki (Sony) 	for (area = 0; area < nr_vms; area++) {
3798e36176beSUladzislau Rezki (Sony) 		insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list);
3799e36176beSUladzislau Rezki (Sony) 
3800e36176beSUladzislau Rezki (Sony) 		setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC,
3801ca23e405STejun Heo 				 pcpu_get_vm_areas);
3802e36176beSUladzislau Rezki (Sony) 	}
3803e36176beSUladzislau Rezki (Sony) 	spin_unlock(&vmap_area_lock);
3804ca23e405STejun Heo 
3805ca23e405STejun Heo 	kfree(vas);
3806ca23e405STejun Heo 	return vms;
3807ca23e405STejun Heo 
380868ad4a33SUladzislau Rezki (Sony) recovery:
3809e36176beSUladzislau Rezki (Sony) 	/*
3810e36176beSUladzislau Rezki (Sony) 	 * Remove previously allocated areas. There is no
3811e36176beSUladzislau Rezki (Sony) 	 * need in removing these areas from the busy tree,
3812e36176beSUladzislau Rezki (Sony) 	 * because they are inserted only on the final step
3813e36176beSUladzislau Rezki (Sony) 	 * and when pcpu_get_vm_areas() is success.
3814e36176beSUladzislau Rezki (Sony) 	 */
381568ad4a33SUladzislau Rezki (Sony) 	while (area--) {
3816253a496dSDaniel Axtens 		orig_start = vas[area]->va_start;
3817253a496dSDaniel Axtens 		orig_end = vas[area]->va_end;
381896e2db45SUladzislau Rezki (Sony) 		va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
38193c5c3cfbSDaniel Axtens 				&free_vmap_area_list);
38209c801f61SUladzislau Rezki (Sony) 		if (va)
3821253a496dSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
3822253a496dSDaniel Axtens 				va->va_start, va->va_end);
382368ad4a33SUladzislau Rezki (Sony) 		vas[area] = NULL;
382468ad4a33SUladzislau Rezki (Sony) 	}
382568ad4a33SUladzislau Rezki (Sony) 
382668ad4a33SUladzislau Rezki (Sony) overflow:
3827e36176beSUladzislau Rezki (Sony) 	spin_unlock(&free_vmap_area_lock);
382868ad4a33SUladzislau Rezki (Sony) 	if (!purged) {
382968ad4a33SUladzislau Rezki (Sony) 		purge_vmap_area_lazy();
383068ad4a33SUladzislau Rezki (Sony) 		purged = true;
383168ad4a33SUladzislau Rezki (Sony) 
383268ad4a33SUladzislau Rezki (Sony) 		/* Before "retry", check if we recover. */
383368ad4a33SUladzislau Rezki (Sony) 		for (area = 0; area < nr_vms; area++) {
383468ad4a33SUladzislau Rezki (Sony) 			if (vas[area])
383568ad4a33SUladzislau Rezki (Sony) 				continue;
383668ad4a33SUladzislau Rezki (Sony) 
383768ad4a33SUladzislau Rezki (Sony) 			vas[area] = kmem_cache_zalloc(
383868ad4a33SUladzislau Rezki (Sony) 				vmap_area_cachep, GFP_KERNEL);
383968ad4a33SUladzislau Rezki (Sony) 			if (!vas[area])
384068ad4a33SUladzislau Rezki (Sony) 				goto err_free;
384168ad4a33SUladzislau Rezki (Sony) 		}
384268ad4a33SUladzislau Rezki (Sony) 
384368ad4a33SUladzislau Rezki (Sony) 		goto retry;
384468ad4a33SUladzislau Rezki (Sony) 	}
384568ad4a33SUladzislau Rezki (Sony) 
3846ca23e405STejun Heo err_free:
3847ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
384868ad4a33SUladzislau Rezki (Sony) 		if (vas[area])
384968ad4a33SUladzislau Rezki (Sony) 			kmem_cache_free(vmap_area_cachep, vas[area]);
385068ad4a33SUladzislau Rezki (Sony) 
3851ca23e405STejun Heo 		kfree(vms[area]);
3852ca23e405STejun Heo 	}
3853f1db7afdSKautuk Consul err_free2:
3854ca23e405STejun Heo 	kfree(vas);
3855ca23e405STejun Heo 	kfree(vms);
3856ca23e405STejun Heo 	return NULL;
3857253a496dSDaniel Axtens 
3858253a496dSDaniel Axtens err_free_shadow:
3859253a496dSDaniel Axtens 	spin_lock(&free_vmap_area_lock);
3860253a496dSDaniel Axtens 	/*
3861253a496dSDaniel Axtens 	 * We release all the vmalloc shadows, even the ones for regions that
3862253a496dSDaniel Axtens 	 * hadn't been successfully added. This relies on kasan_release_vmalloc
3863253a496dSDaniel Axtens 	 * being able to tolerate this case.
3864253a496dSDaniel Axtens 	 */
3865253a496dSDaniel Axtens 	for (area = 0; area < nr_vms; area++) {
3866253a496dSDaniel Axtens 		orig_start = vas[area]->va_start;
3867253a496dSDaniel Axtens 		orig_end = vas[area]->va_end;
386896e2db45SUladzislau Rezki (Sony) 		va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
3869253a496dSDaniel Axtens 				&free_vmap_area_list);
38709c801f61SUladzislau Rezki (Sony) 		if (va)
3871253a496dSDaniel Axtens 			kasan_release_vmalloc(orig_start, orig_end,
3872253a496dSDaniel Axtens 				va->va_start, va->va_end);
3873253a496dSDaniel Axtens 		vas[area] = NULL;
3874253a496dSDaniel Axtens 		kfree(vms[area]);
3875253a496dSDaniel Axtens 	}
3876253a496dSDaniel Axtens 	spin_unlock(&free_vmap_area_lock);
3877253a496dSDaniel Axtens 	kfree(vas);
3878253a496dSDaniel Axtens 	kfree(vms);
3879253a496dSDaniel Axtens 	return NULL;
3880ca23e405STejun Heo }
3881ca23e405STejun Heo 
3882ca23e405STejun Heo /**
3883ca23e405STejun Heo  * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
3884ca23e405STejun Heo  * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
3885ca23e405STejun Heo  * @nr_vms: the number of allocated areas
3886ca23e405STejun Heo  *
3887ca23e405STejun Heo  * Free vm_structs and the array allocated by pcpu_get_vm_areas().
3888ca23e405STejun Heo  */
3889ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
3890ca23e405STejun Heo {
3891ca23e405STejun Heo 	int i;
3892ca23e405STejun Heo 
3893ca23e405STejun Heo 	for (i = 0; i < nr_vms; i++)
3894ca23e405STejun Heo 		free_vm_area(vms[i]);
3895ca23e405STejun Heo 	kfree(vms);
3896ca23e405STejun Heo }
38974f8b02b4STejun Heo #endif	/* CONFIG_SMP */
3898a10aa579SChristoph Lameter 
38995bb1bb35SPaul E. McKenney #ifdef CONFIG_PRINTK
390098f18083SPaul E. McKenney bool vmalloc_dump_obj(void *object)
390198f18083SPaul E. McKenney {
390298f18083SPaul E. McKenney 	struct vm_struct *vm;
390398f18083SPaul E. McKenney 	void *objp = (void *)PAGE_ALIGN((unsigned long)object);
390498f18083SPaul E. McKenney 
390598f18083SPaul E. McKenney 	vm = find_vm_area(objp);
390698f18083SPaul E. McKenney 	if (!vm)
390798f18083SPaul E. McKenney 		return false;
3908bd34dcd4SPaul E. McKenney 	pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
3909bd34dcd4SPaul E. McKenney 		vm->nr_pages, (unsigned long)vm->addr, vm->caller);
391098f18083SPaul E. McKenney 	return true;
391198f18083SPaul E. McKenney }
39125bb1bb35SPaul E. McKenney #endif
391398f18083SPaul E. McKenney 
3914a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS
3915a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos)
3916e36176beSUladzislau Rezki (Sony) 	__acquires(&vmap_purge_lock)
3917d4033afdSJoonsoo Kim 	__acquires(&vmap_area_lock)
3918a10aa579SChristoph Lameter {
3919e36176beSUladzislau Rezki (Sony) 	mutex_lock(&vmap_purge_lock);
3920d4033afdSJoonsoo Kim 	spin_lock(&vmap_area_lock);
3921e36176beSUladzislau Rezki (Sony) 
39223f500069Szijun_hu 	return seq_list_start(&vmap_area_list, *pos);
3923a10aa579SChristoph Lameter }
3924a10aa579SChristoph Lameter 
3925a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3926a10aa579SChristoph Lameter {
39273f500069Szijun_hu 	return seq_list_next(p, &vmap_area_list, pos);
3928a10aa579SChristoph Lameter }
3929a10aa579SChristoph Lameter 
3930a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p)
3931d4033afdSJoonsoo Kim 	__releases(&vmap_area_lock)
39320a7dd4e9SWaiman Long 	__releases(&vmap_purge_lock)
3933a10aa579SChristoph Lameter {
3934d4033afdSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
39350a7dd4e9SWaiman Long 	mutex_unlock(&vmap_purge_lock);
3936a10aa579SChristoph Lameter }
3937a10aa579SChristoph Lameter 
3938a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v)
3939a47a126aSEric Dumazet {
3940e5adfffcSKirill A. Shutemov 	if (IS_ENABLED(CONFIG_NUMA)) {
3941a47a126aSEric Dumazet 		unsigned int nr, *counters = m->private;
394251e50b3aSEric Dumazet 		unsigned int step = 1U << vm_area_page_order(v);
3943a47a126aSEric Dumazet 
3944a47a126aSEric Dumazet 		if (!counters)
3945a47a126aSEric Dumazet 			return;
3946a47a126aSEric Dumazet 
3947af12346cSWanpeng Li 		if (v->flags & VM_UNINITIALIZED)
3948af12346cSWanpeng Li 			return;
39497e5b528bSDmitry Vyukov 		/* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
39507e5b528bSDmitry Vyukov 		smp_rmb();
3951af12346cSWanpeng Li 
3952a47a126aSEric Dumazet 		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
3953a47a126aSEric Dumazet 
395451e50b3aSEric Dumazet 		for (nr = 0; nr < v->nr_pages; nr += step)
395551e50b3aSEric Dumazet 			counters[page_to_nid(v->pages[nr])] += step;
3956a47a126aSEric Dumazet 		for_each_node_state(nr, N_HIGH_MEMORY)
3957a47a126aSEric Dumazet 			if (counters[nr])
3958a47a126aSEric Dumazet 				seq_printf(m, " N%u=%u", nr, counters[nr]);
3959a47a126aSEric Dumazet 	}
3960a47a126aSEric Dumazet }
3961a47a126aSEric Dumazet 
3962dd3b8353SUladzislau Rezki (Sony) static void show_purge_info(struct seq_file *m)
3963dd3b8353SUladzislau Rezki (Sony) {
3964dd3b8353SUladzislau Rezki (Sony) 	struct vmap_area *va;
3965dd3b8353SUladzislau Rezki (Sony) 
396696e2db45SUladzislau Rezki (Sony) 	spin_lock(&purge_vmap_area_lock);
396796e2db45SUladzislau Rezki (Sony) 	list_for_each_entry(va, &purge_vmap_area_list, list) {
3968dd3b8353SUladzislau Rezki (Sony) 		seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n",
3969dd3b8353SUladzislau Rezki (Sony) 			(void *)va->va_start, (void *)va->va_end,
3970dd3b8353SUladzislau Rezki (Sony) 			va->va_end - va->va_start);
3971dd3b8353SUladzislau Rezki (Sony) 	}
397296e2db45SUladzislau Rezki (Sony) 	spin_unlock(&purge_vmap_area_lock);
3973dd3b8353SUladzislau Rezki (Sony) }
3974dd3b8353SUladzislau Rezki (Sony) 
3975a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p)
3976a10aa579SChristoph Lameter {
39773f500069Szijun_hu 	struct vmap_area *va;
3978d4033afdSJoonsoo Kim 	struct vm_struct *v;
3979d4033afdSJoonsoo Kim 
39803f500069Szijun_hu 	va = list_entry(p, struct vmap_area, list);
39813f500069Szijun_hu 
3982c2ce8c14SWanpeng Li 	/*
3983688fcbfcSPengfei Li 	 * s_show can encounter race with remove_vm_area, !vm on behalf
3984688fcbfcSPengfei Li 	 * of vmap area is being tear down or vm_map_ram allocation.
3985c2ce8c14SWanpeng Li 	 */
3986688fcbfcSPengfei Li 	if (!va->vm) {
3987dd3b8353SUladzislau Rezki (Sony) 		seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n",
398878c72746SYisheng Xie 			(void *)va->va_start, (void *)va->va_end,
3989dd3b8353SUladzislau Rezki (Sony) 			va->va_end - va->va_start);
399078c72746SYisheng Xie 
39917cc7913eSEric Dumazet 		goto final;
399278c72746SYisheng Xie 	}
3993d4033afdSJoonsoo Kim 
3994d4033afdSJoonsoo Kim 	v = va->vm;
3995a10aa579SChristoph Lameter 
399645ec1690SKees Cook 	seq_printf(m, "0x%pK-0x%pK %7ld",
3997a10aa579SChristoph Lameter 		v->addr, v->addr + v->size, v->size);
3998a10aa579SChristoph Lameter 
399962c70bceSJoe Perches 	if (v->caller)
400062c70bceSJoe Perches 		seq_printf(m, " %pS", v->caller);
400123016969SChristoph Lameter 
4002a10aa579SChristoph Lameter 	if (v->nr_pages)
4003a10aa579SChristoph Lameter 		seq_printf(m, " pages=%d", v->nr_pages);
4004a10aa579SChristoph Lameter 
4005a10aa579SChristoph Lameter 	if (v->phys_addr)
4006199eaa05SMiles Chen 		seq_printf(m, " phys=%pa", &v->phys_addr);
4007a10aa579SChristoph Lameter 
4008a10aa579SChristoph Lameter 	if (v->flags & VM_IOREMAP)
4009f4527c90SFabian Frederick 		seq_puts(m, " ioremap");
4010a10aa579SChristoph Lameter 
4011a10aa579SChristoph Lameter 	if (v->flags & VM_ALLOC)
4012f4527c90SFabian Frederick 		seq_puts(m, " vmalloc");
4013a10aa579SChristoph Lameter 
4014a10aa579SChristoph Lameter 	if (v->flags & VM_MAP)
4015f4527c90SFabian Frederick 		seq_puts(m, " vmap");
4016a10aa579SChristoph Lameter 
4017a10aa579SChristoph Lameter 	if (v->flags & VM_USERMAP)
4018f4527c90SFabian Frederick 		seq_puts(m, " user");
4019a10aa579SChristoph Lameter 
4020fe9041c2SChristoph Hellwig 	if (v->flags & VM_DMA_COHERENT)
4021fe9041c2SChristoph Hellwig 		seq_puts(m, " dma-coherent");
4022fe9041c2SChristoph Hellwig 
4023244d63eeSDavid Rientjes 	if (is_vmalloc_addr(v->pages))
4024f4527c90SFabian Frederick 		seq_puts(m, " vpages");
4025a10aa579SChristoph Lameter 
4026a47a126aSEric Dumazet 	show_numa_info(m, v);
4027a10aa579SChristoph Lameter 	seq_putc(m, '\n');
4028dd3b8353SUladzislau Rezki (Sony) 
4029dd3b8353SUladzislau Rezki (Sony) 	/*
403096e2db45SUladzislau Rezki (Sony) 	 * As a final step, dump "unpurged" areas.
4031dd3b8353SUladzislau Rezki (Sony) 	 */
40327cc7913eSEric Dumazet final:
4033dd3b8353SUladzislau Rezki (Sony) 	if (list_is_last(&va->list, &vmap_area_list))
4034dd3b8353SUladzislau Rezki (Sony) 		show_purge_info(m);
4035dd3b8353SUladzislau Rezki (Sony) 
4036a10aa579SChristoph Lameter 	return 0;
4037a10aa579SChristoph Lameter }
4038a10aa579SChristoph Lameter 
40395f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = {
4040a10aa579SChristoph Lameter 	.start = s_start,
4041a10aa579SChristoph Lameter 	.next = s_next,
4042a10aa579SChristoph Lameter 	.stop = s_stop,
4043a10aa579SChristoph Lameter 	.show = s_show,
4044a10aa579SChristoph Lameter };
40455f6a6a9cSAlexey Dobriyan 
40465f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void)
40475f6a6a9cSAlexey Dobriyan {
4048fddda2b7SChristoph Hellwig 	if (IS_ENABLED(CONFIG_NUMA))
40490825a6f9SJoe Perches 		proc_create_seq_private("vmallocinfo", 0400, NULL,
405044414d82SChristoph Hellwig 				&vmalloc_op,
405144414d82SChristoph Hellwig 				nr_node_ids * sizeof(unsigned int), NULL);
4052fddda2b7SChristoph Hellwig 	else
40530825a6f9SJoe Perches 		proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
40545f6a6a9cSAlexey Dobriyan 	return 0;
40555f6a6a9cSAlexey Dobriyan }
40565f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init);
4057db3808c1SJoonsoo Kim 
4058a10aa579SChristoph Lameter #endif
4059