xref: /openbmc/linux/mm/vmalloc.c (revision c2febafc67734a62196c1b9dfba926412d4077ba)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/vmalloc.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1993  Linus Torvalds
51da177e4SLinus Torvalds  *  Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
61da177e4SLinus Torvalds  *  SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000
71da177e4SLinus Torvalds  *  Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002
8930fc45aSChristoph Lameter  *  Numa awareness, Christoph Lameter, SGI, June 2005
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>
213ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
2223016969SChristoph Lameter #include <linux/kallsyms.h>
23db64fe02SNick Piggin #include <linux/list.h>
244da56b99SChris Wilson #include <linux/notifier.h>
25db64fe02SNick Piggin #include <linux/rbtree.h>
26db64fe02SNick Piggin #include <linux/radix-tree.h>
27db64fe02SNick Piggin #include <linux/rcupdate.h>
28f0aa6617STejun Heo #include <linux/pfn.h>
2989219d37SCatalin Marinas #include <linux/kmemleak.h>
3060063497SArun Sharma #include <linux/atomic.h>
313b32123dSGideon Israel Dsouza #include <linux/compiler.h>
3232fcfd40SAl Viro #include <linux/llist.h>
330f616be1SToshi Kani #include <linux/bitops.h>
343b32123dSGideon Israel Dsouza 
357c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
361da177e4SLinus Torvalds #include <asm/tlbflush.h>
372dca6999SDavid Miller #include <asm/shmparam.h>
381da177e4SLinus Torvalds 
39dd56b046SMel Gorman #include "internal.h"
40dd56b046SMel Gorman 
4132fcfd40SAl Viro struct vfree_deferred {
4232fcfd40SAl Viro 	struct llist_head list;
4332fcfd40SAl Viro 	struct work_struct wq;
4432fcfd40SAl Viro };
4532fcfd40SAl Viro static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
4632fcfd40SAl Viro 
4732fcfd40SAl Viro static void __vunmap(const void *, int);
4832fcfd40SAl Viro 
4932fcfd40SAl Viro static void free_work(struct work_struct *w)
5032fcfd40SAl Viro {
5132fcfd40SAl Viro 	struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
5232fcfd40SAl Viro 	struct llist_node *llnode = llist_del_all(&p->list);
5332fcfd40SAl Viro 	while (llnode) {
5432fcfd40SAl Viro 		void *p = llnode;
5532fcfd40SAl Viro 		llnode = llist_next(llnode);
5632fcfd40SAl Viro 		__vunmap(p, 1);
5732fcfd40SAl Viro 	}
5832fcfd40SAl Viro }
5932fcfd40SAl Viro 
60db64fe02SNick Piggin /*** Page table manipulation functions ***/
61b221385bSAdrian Bunk 
621da177e4SLinus Torvalds static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
631da177e4SLinus Torvalds {
641da177e4SLinus Torvalds 	pte_t *pte;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	pte = pte_offset_kernel(pmd, addr);
671da177e4SLinus Torvalds 	do {
681da177e4SLinus Torvalds 		pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
691da177e4SLinus Torvalds 		WARN_ON(!pte_none(ptent) && !pte_present(ptent));
701da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
711da177e4SLinus Torvalds }
721da177e4SLinus Torvalds 
73db64fe02SNick Piggin static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	pmd_t *pmd;
761da177e4SLinus Torvalds 	unsigned long next;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
791da177e4SLinus Torvalds 	do {
801da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
81b9820d8fSToshi Kani 		if (pmd_clear_huge(pmd))
82b9820d8fSToshi Kani 			continue;
831da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
841da177e4SLinus Torvalds 			continue;
851da177e4SLinus Torvalds 		vunmap_pte_range(pmd, addr, next);
861da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
89*c2febafcSKirill A. Shutemov static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	pud_t *pud;
921da177e4SLinus Torvalds 	unsigned long next;
931da177e4SLinus Torvalds 
94*c2febafcSKirill A. Shutemov 	pud = pud_offset(p4d, addr);
951da177e4SLinus Torvalds 	do {
961da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
97b9820d8fSToshi Kani 		if (pud_clear_huge(pud))
98b9820d8fSToshi Kani 			continue;
991da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
1001da177e4SLinus Torvalds 			continue;
1011da177e4SLinus Torvalds 		vunmap_pmd_range(pud, addr, next);
1021da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
105*c2febafcSKirill A. Shutemov static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end)
106*c2febafcSKirill A. Shutemov {
107*c2febafcSKirill A. Shutemov 	p4d_t *p4d;
108*c2febafcSKirill A. Shutemov 	unsigned long next;
109*c2febafcSKirill A. Shutemov 
110*c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
111*c2febafcSKirill A. Shutemov 	do {
112*c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
113*c2febafcSKirill A. Shutemov 		if (p4d_clear_huge(p4d))
114*c2febafcSKirill A. Shutemov 			continue;
115*c2febafcSKirill A. Shutemov 		if (p4d_none_or_clear_bad(p4d))
116*c2febafcSKirill A. Shutemov 			continue;
117*c2febafcSKirill A. Shutemov 		vunmap_pud_range(p4d, addr, next);
118*c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
119*c2febafcSKirill A. Shutemov }
120*c2febafcSKirill A. Shutemov 
121db64fe02SNick Piggin static void vunmap_page_range(unsigned long addr, unsigned long end)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	pgd_t *pgd;
1241da177e4SLinus Torvalds 	unsigned long next;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1271da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
1281da177e4SLinus Torvalds 	do {
1291da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
1301da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
1311da177e4SLinus Torvalds 			continue;
132*c2febafcSKirill A. Shutemov 		vunmap_p4d_range(pgd, addr, next);
1331da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
137db64fe02SNick Piggin 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	pte_t *pte;
1401da177e4SLinus Torvalds 
141db64fe02SNick Piggin 	/*
142db64fe02SNick Piggin 	 * nr is a running index into the array which helps higher level
143db64fe02SNick Piggin 	 * callers keep track of where we're up to.
144db64fe02SNick Piggin 	 */
145db64fe02SNick Piggin 
146872fec16SHugh Dickins 	pte = pte_alloc_kernel(pmd, addr);
1471da177e4SLinus Torvalds 	if (!pte)
1481da177e4SLinus Torvalds 		return -ENOMEM;
1491da177e4SLinus Torvalds 	do {
150db64fe02SNick Piggin 		struct page *page = pages[*nr];
151db64fe02SNick Piggin 
152db64fe02SNick Piggin 		if (WARN_ON(!pte_none(*pte)))
153db64fe02SNick Piggin 			return -EBUSY;
154db64fe02SNick Piggin 		if (WARN_ON(!page))
1551da177e4SLinus Torvalds 			return -ENOMEM;
1561da177e4SLinus Torvalds 		set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
157db64fe02SNick Piggin 		(*nr)++;
1581da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1591da177e4SLinus Torvalds 	return 0;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
162db64fe02SNick Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr,
163db64fe02SNick Piggin 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds 	pmd_t *pmd;
1661da177e4SLinus Torvalds 	unsigned long next;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	pmd = pmd_alloc(&init_mm, pud, addr);
1691da177e4SLinus Torvalds 	if (!pmd)
1701da177e4SLinus Torvalds 		return -ENOMEM;
1711da177e4SLinus Torvalds 	do {
1721da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
173db64fe02SNick Piggin 		if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
1741da177e4SLinus Torvalds 			return -ENOMEM;
1751da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
1761da177e4SLinus Torvalds 	return 0;
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
179*c2febafcSKirill A. Shutemov static int vmap_pud_range(p4d_t *p4d, unsigned long addr,
180db64fe02SNick Piggin 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	pud_t *pud;
1831da177e4SLinus Torvalds 	unsigned long next;
1841da177e4SLinus Torvalds 
185*c2febafcSKirill A. Shutemov 	pud = pud_alloc(&init_mm, p4d, addr);
1861da177e4SLinus Torvalds 	if (!pud)
1871da177e4SLinus Torvalds 		return -ENOMEM;
1881da177e4SLinus Torvalds 	do {
1891da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
190db64fe02SNick Piggin 		if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
1911da177e4SLinus Torvalds 			return -ENOMEM;
1921da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
1931da177e4SLinus Torvalds 	return 0;
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
196*c2febafcSKirill A. Shutemov static int vmap_p4d_range(pgd_t *pgd, unsigned long addr,
197*c2febafcSKirill A. Shutemov 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
198*c2febafcSKirill A. Shutemov {
199*c2febafcSKirill A. Shutemov 	p4d_t *p4d;
200*c2febafcSKirill A. Shutemov 	unsigned long next;
201*c2febafcSKirill A. Shutemov 
202*c2febafcSKirill A. Shutemov 	p4d = p4d_alloc(&init_mm, pgd, addr);
203*c2febafcSKirill A. Shutemov 	if (!p4d)
204*c2febafcSKirill A. Shutemov 		return -ENOMEM;
205*c2febafcSKirill A. Shutemov 	do {
206*c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
207*c2febafcSKirill A. Shutemov 		if (vmap_pud_range(p4d, addr, next, prot, pages, nr))
208*c2febafcSKirill A. Shutemov 			return -ENOMEM;
209*c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
210*c2febafcSKirill A. Shutemov 	return 0;
211*c2febafcSKirill A. Shutemov }
212*c2febafcSKirill A. Shutemov 
213db64fe02SNick Piggin /*
214db64fe02SNick Piggin  * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
215db64fe02SNick Piggin  * will have pfns corresponding to the "pages" array.
216db64fe02SNick Piggin  *
217db64fe02SNick Piggin  * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
218db64fe02SNick Piggin  */
2198fc48985STejun Heo static int vmap_page_range_noflush(unsigned long start, unsigned long end,
220db64fe02SNick Piggin 				   pgprot_t prot, struct page **pages)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	pgd_t *pgd;
2231da177e4SLinus Torvalds 	unsigned long next;
2242e4e27c7SAdam Lackorzynski 	unsigned long addr = start;
225db64fe02SNick Piggin 	int err = 0;
226db64fe02SNick Piggin 	int nr = 0;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	BUG_ON(addr >= end);
2291da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
2301da177e4SLinus Torvalds 	do {
2311da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
232*c2febafcSKirill A. Shutemov 		err = vmap_p4d_range(pgd, addr, next, prot, pages, &nr);
2331da177e4SLinus Torvalds 		if (err)
234bf88c8c8SFigo.zhang 			return err;
2351da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
236db64fe02SNick Piggin 
237db64fe02SNick Piggin 	return nr;
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
2408fc48985STejun Heo static int vmap_page_range(unsigned long start, unsigned long end,
2418fc48985STejun Heo 			   pgprot_t prot, struct page **pages)
2428fc48985STejun Heo {
2438fc48985STejun Heo 	int ret;
2448fc48985STejun Heo 
2458fc48985STejun Heo 	ret = vmap_page_range_noflush(start, end, prot, pages);
2468fc48985STejun Heo 	flush_cache_vmap(start, end);
2478fc48985STejun Heo 	return ret;
2488fc48985STejun Heo }
2498fc48985STejun Heo 
25081ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x)
25173bdf0a6SLinus Torvalds {
25273bdf0a6SLinus Torvalds 	/*
253ab4f2ee1SRussell King 	 * ARM, x86-64 and sparc64 put modules in a special place,
25473bdf0a6SLinus Torvalds 	 * and fall back on vmalloc() if that fails. Others
25573bdf0a6SLinus Torvalds 	 * just put it in the vmalloc space.
25673bdf0a6SLinus Torvalds 	 */
25773bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
25873bdf0a6SLinus Torvalds 	unsigned long addr = (unsigned long)x;
25973bdf0a6SLinus Torvalds 	if (addr >= MODULES_VADDR && addr < MODULES_END)
26073bdf0a6SLinus Torvalds 		return 1;
26173bdf0a6SLinus Torvalds #endif
26273bdf0a6SLinus Torvalds 	return is_vmalloc_addr(x);
26373bdf0a6SLinus Torvalds }
26473bdf0a6SLinus Torvalds 
26548667e7aSChristoph Lameter /*
266add688fbSmalc  * Walk a vmap address to the struct page it maps.
26748667e7aSChristoph Lameter  */
268add688fbSmalc struct page *vmalloc_to_page(const void *vmalloc_addr)
26948667e7aSChristoph Lameter {
27048667e7aSChristoph Lameter 	unsigned long addr = (unsigned long) vmalloc_addr;
271add688fbSmalc 	struct page *page = NULL;
27248667e7aSChristoph Lameter 	pgd_t *pgd = pgd_offset_k(addr);
273*c2febafcSKirill A. Shutemov 	p4d_t *p4d;
274*c2febafcSKirill A. Shutemov 	pud_t *pud;
275*c2febafcSKirill A. Shutemov 	pmd_t *pmd;
276*c2febafcSKirill A. Shutemov 	pte_t *ptep, pte;
27748667e7aSChristoph Lameter 
2787aa413deSIngo Molnar 	/*
2797aa413deSIngo Molnar 	 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
2807aa413deSIngo Molnar 	 * architectures that do not vmalloc module space
2817aa413deSIngo Molnar 	 */
28273bdf0a6SLinus Torvalds 	VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
28359ea7463SJiri Slaby 
284*c2febafcSKirill A. Shutemov 	if (pgd_none(*pgd))
285*c2febafcSKirill A. Shutemov 		return NULL;
286*c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
287*c2febafcSKirill A. Shutemov 	if (p4d_none(*p4d))
288*c2febafcSKirill A. Shutemov 		return NULL;
289*c2febafcSKirill A. Shutemov 	pud = pud_offset(p4d, addr);
290*c2febafcSKirill A. Shutemov 	if (pud_none(*pud))
291*c2febafcSKirill A. Shutemov 		return NULL;
292*c2febafcSKirill A. Shutemov 	pmd = pmd_offset(pud, addr);
293*c2febafcSKirill A. Shutemov 	if (pmd_none(*pmd))
294*c2febafcSKirill A. Shutemov 		return NULL;
295db64fe02SNick Piggin 
29648667e7aSChristoph Lameter 	ptep = pte_offset_map(pmd, addr);
29748667e7aSChristoph Lameter 	pte = *ptep;
29848667e7aSChristoph Lameter 	if (pte_present(pte))
299add688fbSmalc 		page = pte_page(pte);
30048667e7aSChristoph Lameter 	pte_unmap(ptep);
301add688fbSmalc 	return page;
302ece86e22SJianyu Zhan }
303ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page);
304ece86e22SJianyu Zhan 
305add688fbSmalc /*
306add688fbSmalc  * Map a vmalloc()-space virtual address to the physical page frame number.
307add688fbSmalc  */
308add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
309add688fbSmalc {
310add688fbSmalc 	return page_to_pfn(vmalloc_to_page(vmalloc_addr));
311add688fbSmalc }
312add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn);
313add688fbSmalc 
314db64fe02SNick Piggin 
315db64fe02SNick Piggin /*** Global kva allocator ***/
316db64fe02SNick Piggin 
317db64fe02SNick Piggin #define VM_VM_AREA	0x04
318db64fe02SNick Piggin 
319db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock);
320f1c4069eSJoonsoo Kim /* Export for kexec only */
321f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
32280c4bd7aSChris Wilson static LLIST_HEAD(vmap_purge_list);
32389699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT;
32489699605SNick Piggin 
32589699605SNick Piggin /* The vmap cache globals are protected by vmap_area_lock */
32689699605SNick Piggin static struct rb_node *free_vmap_cache;
32789699605SNick Piggin static unsigned long cached_hole_size;
32889699605SNick Piggin static unsigned long cached_vstart;
32989699605SNick Piggin static unsigned long cached_align;
33089699605SNick Piggin 
331ca23e405STejun Heo static unsigned long vmap_area_pcpu_hole;
332db64fe02SNick Piggin 
333db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr)
3341da177e4SLinus Torvalds {
335db64fe02SNick Piggin 	struct rb_node *n = vmap_area_root.rb_node;
336db64fe02SNick Piggin 
337db64fe02SNick Piggin 	while (n) {
338db64fe02SNick Piggin 		struct vmap_area *va;
339db64fe02SNick Piggin 
340db64fe02SNick Piggin 		va = rb_entry(n, struct vmap_area, rb_node);
341db64fe02SNick Piggin 		if (addr < va->va_start)
342db64fe02SNick Piggin 			n = n->rb_left;
343cef2ac3fSHATAYAMA Daisuke 		else if (addr >= va->va_end)
344db64fe02SNick Piggin 			n = n->rb_right;
345db64fe02SNick Piggin 		else
346db64fe02SNick Piggin 			return va;
347db64fe02SNick Piggin 	}
348db64fe02SNick Piggin 
349db64fe02SNick Piggin 	return NULL;
350db64fe02SNick Piggin }
351db64fe02SNick Piggin 
352db64fe02SNick Piggin static void __insert_vmap_area(struct vmap_area *va)
353db64fe02SNick Piggin {
354db64fe02SNick Piggin 	struct rb_node **p = &vmap_area_root.rb_node;
355db64fe02SNick Piggin 	struct rb_node *parent = NULL;
356db64fe02SNick Piggin 	struct rb_node *tmp;
357db64fe02SNick Piggin 
358db64fe02SNick Piggin 	while (*p) {
359170168d0SNamhyung Kim 		struct vmap_area *tmp_va;
360db64fe02SNick Piggin 
361db64fe02SNick Piggin 		parent = *p;
362170168d0SNamhyung Kim 		tmp_va = rb_entry(parent, struct vmap_area, rb_node);
363170168d0SNamhyung Kim 		if (va->va_start < tmp_va->va_end)
364db64fe02SNick Piggin 			p = &(*p)->rb_left;
365170168d0SNamhyung Kim 		else if (va->va_end > tmp_va->va_start)
366db64fe02SNick Piggin 			p = &(*p)->rb_right;
367db64fe02SNick Piggin 		else
368db64fe02SNick Piggin 			BUG();
369db64fe02SNick Piggin 	}
370db64fe02SNick Piggin 
371db64fe02SNick Piggin 	rb_link_node(&va->rb_node, parent, p);
372db64fe02SNick Piggin 	rb_insert_color(&va->rb_node, &vmap_area_root);
373db64fe02SNick Piggin 
3744341fa45SJoonsoo Kim 	/* address-sort this list */
375db64fe02SNick Piggin 	tmp = rb_prev(&va->rb_node);
376db64fe02SNick Piggin 	if (tmp) {
377db64fe02SNick Piggin 		struct vmap_area *prev;
378db64fe02SNick Piggin 		prev = rb_entry(tmp, struct vmap_area, rb_node);
379db64fe02SNick Piggin 		list_add_rcu(&va->list, &prev->list);
380db64fe02SNick Piggin 	} else
381db64fe02SNick Piggin 		list_add_rcu(&va->list, &vmap_area_list);
382db64fe02SNick Piggin }
383db64fe02SNick Piggin 
384db64fe02SNick Piggin static void purge_vmap_area_lazy(void);
385db64fe02SNick Piggin 
3864da56b99SChris Wilson static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
3874da56b99SChris Wilson 
388db64fe02SNick Piggin /*
389db64fe02SNick Piggin  * Allocate a region of KVA of the specified size and alignment, within the
390db64fe02SNick Piggin  * vstart and vend.
391db64fe02SNick Piggin  */
392db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size,
393db64fe02SNick Piggin 				unsigned long align,
394db64fe02SNick Piggin 				unsigned long vstart, unsigned long vend,
395db64fe02SNick Piggin 				int node, gfp_t gfp_mask)
396db64fe02SNick Piggin {
397db64fe02SNick Piggin 	struct vmap_area *va;
398db64fe02SNick Piggin 	struct rb_node *n;
3991da177e4SLinus Torvalds 	unsigned long addr;
400db64fe02SNick Piggin 	int purged = 0;
40189699605SNick Piggin 	struct vmap_area *first;
402db64fe02SNick Piggin 
4037766970cSNick Piggin 	BUG_ON(!size);
404891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
40589699605SNick Piggin 	BUG_ON(!is_power_of_2(align));
406db64fe02SNick Piggin 
4075803ed29SChristoph Hellwig 	might_sleep();
4084da56b99SChris Wilson 
409db64fe02SNick Piggin 	va = kmalloc_node(sizeof(struct vmap_area),
410db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
411db64fe02SNick Piggin 	if (unlikely(!va))
412db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
413db64fe02SNick Piggin 
4147f88f88fSCatalin Marinas 	/*
4157f88f88fSCatalin Marinas 	 * Only scan the relevant parts containing pointers to other objects
4167f88f88fSCatalin Marinas 	 * to avoid false negatives.
4177f88f88fSCatalin Marinas 	 */
4187f88f88fSCatalin Marinas 	kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK);
4197f88f88fSCatalin Marinas 
420db64fe02SNick Piggin retry:
421db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
42289699605SNick Piggin 	/*
42389699605SNick Piggin 	 * Invalidate cache if we have more permissive parameters.
42489699605SNick Piggin 	 * cached_hole_size notes the largest hole noticed _below_
42589699605SNick Piggin 	 * the vmap_area cached in free_vmap_cache: if size fits
42689699605SNick Piggin 	 * into that hole, we want to scan from vstart to reuse
42789699605SNick Piggin 	 * the hole instead of allocating above free_vmap_cache.
42889699605SNick Piggin 	 * Note that __free_vmap_area may update free_vmap_cache
42989699605SNick Piggin 	 * without updating cached_hole_size or cached_align.
43089699605SNick Piggin 	 */
43189699605SNick Piggin 	if (!free_vmap_cache ||
43289699605SNick Piggin 			size < cached_hole_size ||
43389699605SNick Piggin 			vstart < cached_vstart ||
43489699605SNick Piggin 			align < cached_align) {
43589699605SNick Piggin nocache:
43689699605SNick Piggin 		cached_hole_size = 0;
43789699605SNick Piggin 		free_vmap_cache = NULL;
43889699605SNick Piggin 	}
43989699605SNick Piggin 	/* record if we encounter less permissive parameters */
44089699605SNick Piggin 	cached_vstart = vstart;
44189699605SNick Piggin 	cached_align = align;
44289699605SNick Piggin 
44389699605SNick Piggin 	/* find starting point for our search */
44489699605SNick Piggin 	if (free_vmap_cache) {
44589699605SNick Piggin 		first = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
446248ac0e1SJohannes Weiner 		addr = ALIGN(first->va_end, align);
44789699605SNick Piggin 		if (addr < vstart)
44889699605SNick Piggin 			goto nocache;
449bcb615a8SZhang Yanfei 		if (addr + size < addr)
4507766970cSNick Piggin 			goto overflow;
4517766970cSNick Piggin 
45289699605SNick Piggin 	} else {
45389699605SNick Piggin 		addr = ALIGN(vstart, align);
454bcb615a8SZhang Yanfei 		if (addr + size < addr)
45589699605SNick Piggin 			goto overflow;
456db64fe02SNick Piggin 
45789699605SNick Piggin 		n = vmap_area_root.rb_node;
45889699605SNick Piggin 		first = NULL;
45989699605SNick Piggin 
46089699605SNick Piggin 		while (n) {
461db64fe02SNick Piggin 			struct vmap_area *tmp;
462db64fe02SNick Piggin 			tmp = rb_entry(n, struct vmap_area, rb_node);
463db64fe02SNick Piggin 			if (tmp->va_end >= addr) {
464db64fe02SNick Piggin 				first = tmp;
46589699605SNick Piggin 				if (tmp->va_start <= addr)
46689699605SNick Piggin 					break;
467db64fe02SNick Piggin 				n = n->rb_left;
46889699605SNick Piggin 			} else
469db64fe02SNick Piggin 				n = n->rb_right;
470db64fe02SNick Piggin 		}
471db64fe02SNick Piggin 
472db64fe02SNick Piggin 		if (!first)
473db64fe02SNick Piggin 			goto found;
474db64fe02SNick Piggin 	}
475db64fe02SNick Piggin 
47689699605SNick Piggin 	/* from the starting point, walk areas until a suitable hole is found */
477248ac0e1SJohannes Weiner 	while (addr + size > first->va_start && addr + size <= vend) {
47889699605SNick Piggin 		if (addr + cached_hole_size < first->va_start)
47989699605SNick Piggin 			cached_hole_size = first->va_start - addr;
480248ac0e1SJohannes Weiner 		addr = ALIGN(first->va_end, align);
481bcb615a8SZhang Yanfei 		if (addr + size < addr)
4827766970cSNick Piggin 			goto overflow;
483db64fe02SNick Piggin 
48492ca922fSHong zhi guo 		if (list_is_last(&first->list, &vmap_area_list))
485db64fe02SNick Piggin 			goto found;
48692ca922fSHong zhi guo 
4876219c2a2SGeliang Tang 		first = list_next_entry(first, list);
488db64fe02SNick Piggin 	}
48989699605SNick Piggin 
490db64fe02SNick Piggin found:
49189699605SNick Piggin 	if (addr + size > vend)
49289699605SNick Piggin 		goto overflow;
49389699605SNick Piggin 
49489699605SNick Piggin 	va->va_start = addr;
49589699605SNick Piggin 	va->va_end = addr + size;
49689699605SNick Piggin 	va->flags = 0;
49789699605SNick Piggin 	__insert_vmap_area(va);
49889699605SNick Piggin 	free_vmap_cache = &va->rb_node;
49989699605SNick Piggin 	spin_unlock(&vmap_area_lock);
50089699605SNick Piggin 
50161e16557SWang Xiaoqiang 	BUG_ON(!IS_ALIGNED(va->va_start, align));
50289699605SNick Piggin 	BUG_ON(va->va_start < vstart);
50389699605SNick Piggin 	BUG_ON(va->va_end > vend);
50489699605SNick Piggin 
50589699605SNick Piggin 	return va;
50689699605SNick Piggin 
5077766970cSNick Piggin overflow:
508db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
509db64fe02SNick Piggin 	if (!purged) {
510db64fe02SNick Piggin 		purge_vmap_area_lazy();
511db64fe02SNick Piggin 		purged = 1;
512db64fe02SNick Piggin 		goto retry;
513db64fe02SNick Piggin 	}
5144da56b99SChris Wilson 
5154da56b99SChris Wilson 	if (gfpflags_allow_blocking(gfp_mask)) {
5164da56b99SChris Wilson 		unsigned long freed = 0;
5174da56b99SChris Wilson 		blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
5184da56b99SChris Wilson 		if (freed > 0) {
5194da56b99SChris Wilson 			purged = 0;
5204da56b99SChris Wilson 			goto retry;
5214da56b99SChris Wilson 		}
5224da56b99SChris Wilson 	}
5234da56b99SChris Wilson 
524db64fe02SNick Piggin 	if (printk_ratelimit())
525756a025fSJoe Perches 		pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
526756a025fSJoe Perches 			size);
5272498ce42SRalph Wuerthner 	kfree(va);
528db64fe02SNick Piggin 	return ERR_PTR(-EBUSY);
529db64fe02SNick Piggin }
530db64fe02SNick Piggin 
5314da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb)
5324da56b99SChris Wilson {
5334da56b99SChris Wilson 	return blocking_notifier_chain_register(&vmap_notify_list, nb);
5344da56b99SChris Wilson }
5354da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
5364da56b99SChris Wilson 
5374da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb)
5384da56b99SChris Wilson {
5394da56b99SChris Wilson 	return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
5404da56b99SChris Wilson }
5414da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
5424da56b99SChris Wilson 
543db64fe02SNick Piggin static void __free_vmap_area(struct vmap_area *va)
544db64fe02SNick Piggin {
545db64fe02SNick Piggin 	BUG_ON(RB_EMPTY_NODE(&va->rb_node));
54689699605SNick Piggin 
54789699605SNick Piggin 	if (free_vmap_cache) {
54889699605SNick Piggin 		if (va->va_end < cached_vstart) {
54989699605SNick Piggin 			free_vmap_cache = NULL;
55089699605SNick Piggin 		} else {
55189699605SNick Piggin 			struct vmap_area *cache;
55289699605SNick Piggin 			cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
55389699605SNick Piggin 			if (va->va_start <= cache->va_start) {
55489699605SNick Piggin 				free_vmap_cache = rb_prev(&va->rb_node);
55589699605SNick Piggin 				/*
55689699605SNick Piggin 				 * We don't try to update cached_hole_size or
55789699605SNick Piggin 				 * cached_align, but it won't go very wrong.
55889699605SNick Piggin 				 */
55989699605SNick Piggin 			}
56089699605SNick Piggin 		}
56189699605SNick Piggin 	}
562db64fe02SNick Piggin 	rb_erase(&va->rb_node, &vmap_area_root);
563db64fe02SNick Piggin 	RB_CLEAR_NODE(&va->rb_node);
564db64fe02SNick Piggin 	list_del_rcu(&va->list);
565db64fe02SNick Piggin 
566ca23e405STejun Heo 	/*
567ca23e405STejun Heo 	 * Track the highest possible candidate for pcpu area
568ca23e405STejun Heo 	 * allocation.  Areas outside of vmalloc area can be returned
569ca23e405STejun Heo 	 * here too, consider only end addresses which fall inside
570ca23e405STejun Heo 	 * vmalloc area proper.
571ca23e405STejun Heo 	 */
572ca23e405STejun Heo 	if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
573ca23e405STejun Heo 		vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
574ca23e405STejun Heo 
57514769de9SLai Jiangshan 	kfree_rcu(va, rcu_head);
576db64fe02SNick Piggin }
577db64fe02SNick Piggin 
578db64fe02SNick Piggin /*
579db64fe02SNick Piggin  * Free a region of KVA allocated by alloc_vmap_area
580db64fe02SNick Piggin  */
581db64fe02SNick Piggin static void free_vmap_area(struct vmap_area *va)
582db64fe02SNick Piggin {
583db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
584db64fe02SNick Piggin 	__free_vmap_area(va);
585db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
586db64fe02SNick Piggin }
587db64fe02SNick Piggin 
588db64fe02SNick Piggin /*
589db64fe02SNick Piggin  * Clear the pagetable entries of a given vmap_area
590db64fe02SNick Piggin  */
591db64fe02SNick Piggin static void unmap_vmap_area(struct vmap_area *va)
592db64fe02SNick Piggin {
593db64fe02SNick Piggin 	vunmap_page_range(va->va_start, va->va_end);
594db64fe02SNick Piggin }
595db64fe02SNick Piggin 
596cd52858cSNick Piggin static void vmap_debug_free_range(unsigned long start, unsigned long end)
597cd52858cSNick Piggin {
598cd52858cSNick Piggin 	/*
599f48d97f3SJoonsoo Kim 	 * Unmap page tables and force a TLB flush immediately if pagealloc
600f48d97f3SJoonsoo Kim 	 * debugging is enabled.  This catches use after free bugs similarly to
601f48d97f3SJoonsoo Kim 	 * those in linear kernel virtual address space after a page has been
602f48d97f3SJoonsoo Kim 	 * freed.
603cd52858cSNick Piggin 	 *
604f48d97f3SJoonsoo Kim 	 * All the lazy freeing logic is still retained, in order to minimise
605f48d97f3SJoonsoo Kim 	 * intrusiveness of this debugging feature.
606cd52858cSNick Piggin 	 *
607f48d97f3SJoonsoo Kim 	 * This is going to be *slow* (linear kernel virtual address debugging
608f48d97f3SJoonsoo Kim 	 * doesn't do a broadcast TLB flush so it is a lot faster).
609cd52858cSNick Piggin 	 */
610f48d97f3SJoonsoo Kim 	if (debug_pagealloc_enabled()) {
611cd52858cSNick Piggin 		vunmap_page_range(start, end);
612cd52858cSNick Piggin 		flush_tlb_kernel_range(start, end);
613f48d97f3SJoonsoo Kim 	}
614cd52858cSNick Piggin }
615cd52858cSNick Piggin 
616db64fe02SNick Piggin /*
617db64fe02SNick Piggin  * lazy_max_pages is the maximum amount of virtual address space we gather up
618db64fe02SNick Piggin  * before attempting to purge with a TLB flush.
619db64fe02SNick Piggin  *
620db64fe02SNick Piggin  * There is a tradeoff here: a larger number will cover more kernel page tables
621db64fe02SNick Piggin  * and take slightly longer to purge, but it will linearly reduce the number of
622db64fe02SNick Piggin  * global TLB flushes that must be performed. It would seem natural to scale
623db64fe02SNick Piggin  * this number up linearly with the number of CPUs (because vmapping activity
624db64fe02SNick Piggin  * could also scale linearly with the number of CPUs), however it is likely
625db64fe02SNick Piggin  * that in practice, workloads might be constrained in other ways that mean
626db64fe02SNick Piggin  * vmap activity will not scale linearly with CPUs. Also, I want to be
627db64fe02SNick Piggin  * conservative and not introduce a big latency on huge systems, so go with
628db64fe02SNick Piggin  * a less aggressive log scale. It will still be an improvement over the old
629db64fe02SNick Piggin  * code, and it will be simple to change the scale factor if we find that it
630db64fe02SNick Piggin  * becomes a problem on bigger systems.
631db64fe02SNick Piggin  */
632db64fe02SNick Piggin static unsigned long lazy_max_pages(void)
633db64fe02SNick Piggin {
634db64fe02SNick Piggin 	unsigned int log;
635db64fe02SNick Piggin 
636db64fe02SNick Piggin 	log = fls(num_online_cpus());
637db64fe02SNick Piggin 
638db64fe02SNick Piggin 	return log * (32UL * 1024 * 1024 / PAGE_SIZE);
639db64fe02SNick Piggin }
640db64fe02SNick Piggin 
641db64fe02SNick Piggin static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
642db64fe02SNick Piggin 
6430574ecd1SChristoph Hellwig /*
6440574ecd1SChristoph Hellwig  * Serialize vmap purging.  There is no actual criticial section protected
6450574ecd1SChristoph Hellwig  * by this look, but we want to avoid concurrent calls for performance
6460574ecd1SChristoph Hellwig  * reasons and to make the pcpu_get_vm_areas more deterministic.
6470574ecd1SChristoph Hellwig  */
648f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock);
6490574ecd1SChristoph Hellwig 
65002b709dfSNick Piggin /* for per-CPU blocks */
65102b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void);
65202b709dfSNick Piggin 
653db64fe02SNick Piggin /*
6543ee48b6aSCliff Wickman  * called before a call to iounmap() if the caller wants vm_area_struct's
6553ee48b6aSCliff Wickman  * immediately freed.
6563ee48b6aSCliff Wickman  */
6573ee48b6aSCliff Wickman void set_iounmap_nonlazy(void)
6583ee48b6aSCliff Wickman {
6593ee48b6aSCliff Wickman 	atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
6603ee48b6aSCliff Wickman }
6613ee48b6aSCliff Wickman 
6623ee48b6aSCliff Wickman /*
663db64fe02SNick Piggin  * Purges all lazily-freed vmap areas.
664db64fe02SNick Piggin  */
6650574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
666db64fe02SNick Piggin {
66780c4bd7aSChris Wilson 	struct llist_node *valist;
668db64fe02SNick Piggin 	struct vmap_area *va;
669cbb76676SVegard Nossum 	struct vmap_area *n_va;
670763b218dSJoel Fernandes 	bool do_free = false;
671db64fe02SNick Piggin 
6720574ecd1SChristoph Hellwig 	lockdep_assert_held(&vmap_purge_lock);
67302b709dfSNick Piggin 
67480c4bd7aSChris Wilson 	valist = llist_del_all(&vmap_purge_list);
67580c4bd7aSChris Wilson 	llist_for_each_entry(va, valist, purge_list) {
6760574ecd1SChristoph Hellwig 		if (va->va_start < start)
6770574ecd1SChristoph Hellwig 			start = va->va_start;
6780574ecd1SChristoph Hellwig 		if (va->va_end > end)
6790574ecd1SChristoph Hellwig 			end = va->va_end;
680763b218dSJoel Fernandes 		do_free = true;
681db64fe02SNick Piggin 	}
682db64fe02SNick Piggin 
683763b218dSJoel Fernandes 	if (!do_free)
6840574ecd1SChristoph Hellwig 		return false;
6850574ecd1SChristoph Hellwig 
6860574ecd1SChristoph Hellwig 	flush_tlb_kernel_range(start, end);
687db64fe02SNick Piggin 
688db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
689763b218dSJoel Fernandes 	llist_for_each_entry_safe(va, n_va, valist, purge_list) {
690763b218dSJoel Fernandes 		int nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
691763b218dSJoel Fernandes 
692db64fe02SNick Piggin 		__free_vmap_area(va);
693763b218dSJoel Fernandes 		atomic_sub(nr, &vmap_lazy_nr);
694763b218dSJoel Fernandes 		cond_resched_lock(&vmap_area_lock);
695763b218dSJoel Fernandes 	}
696db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
6970574ecd1SChristoph Hellwig 	return true;
698db64fe02SNick Piggin }
699db64fe02SNick Piggin 
700db64fe02SNick Piggin /*
701496850e5SNick Piggin  * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
702496850e5SNick Piggin  * is already purging.
703496850e5SNick Piggin  */
704496850e5SNick Piggin static void try_purge_vmap_area_lazy(void)
705496850e5SNick Piggin {
706f9e09977SChristoph Hellwig 	if (mutex_trylock(&vmap_purge_lock)) {
7070574ecd1SChristoph Hellwig 		__purge_vmap_area_lazy(ULONG_MAX, 0);
708f9e09977SChristoph Hellwig 		mutex_unlock(&vmap_purge_lock);
7090574ecd1SChristoph Hellwig 	}
710496850e5SNick Piggin }
711496850e5SNick Piggin 
712496850e5SNick Piggin /*
713db64fe02SNick Piggin  * Kick off a purge of the outstanding lazy areas.
714db64fe02SNick Piggin  */
715db64fe02SNick Piggin static void purge_vmap_area_lazy(void)
716db64fe02SNick Piggin {
717f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
7180574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
7190574ecd1SChristoph Hellwig 	__purge_vmap_area_lazy(ULONG_MAX, 0);
720f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
721db64fe02SNick Piggin }
722db64fe02SNick Piggin 
723db64fe02SNick Piggin /*
72464141da5SJeremy Fitzhardinge  * Free a vmap area, caller ensuring that the area has been unmapped
72564141da5SJeremy Fitzhardinge  * and flush_cache_vunmap had been called for the correct range
72664141da5SJeremy Fitzhardinge  * previously.
727db64fe02SNick Piggin  */
72864141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va)
729db64fe02SNick Piggin {
73080c4bd7aSChris Wilson 	int nr_lazy;
73180c4bd7aSChris Wilson 
73280c4bd7aSChris Wilson 	nr_lazy = atomic_add_return((va->va_end - va->va_start) >> PAGE_SHIFT,
73380c4bd7aSChris Wilson 				    &vmap_lazy_nr);
73480c4bd7aSChris Wilson 
73580c4bd7aSChris Wilson 	/* After this point, we may free va at any time */
73680c4bd7aSChris Wilson 	llist_add(&va->purge_list, &vmap_purge_list);
73780c4bd7aSChris Wilson 
73880c4bd7aSChris Wilson 	if (unlikely(nr_lazy > lazy_max_pages()))
739496850e5SNick Piggin 		try_purge_vmap_area_lazy();
740db64fe02SNick Piggin }
741db64fe02SNick Piggin 
742b29acbdcSNick Piggin /*
743b29acbdcSNick Piggin  * Free and unmap a vmap area
744b29acbdcSNick Piggin  */
745b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va)
746b29acbdcSNick Piggin {
747b29acbdcSNick Piggin 	flush_cache_vunmap(va->va_start, va->va_end);
748c8eef01eSChristoph Hellwig 	unmap_vmap_area(va);
749c8eef01eSChristoph Hellwig 	free_vmap_area_noflush(va);
750b29acbdcSNick Piggin }
751b29acbdcSNick Piggin 
752db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr)
753db64fe02SNick Piggin {
754db64fe02SNick Piggin 	struct vmap_area *va;
755db64fe02SNick Piggin 
756db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
757db64fe02SNick Piggin 	va = __find_vmap_area(addr);
758db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
759db64fe02SNick Piggin 
760db64fe02SNick Piggin 	return va;
761db64fe02SNick Piggin }
762db64fe02SNick Piggin 
763db64fe02SNick Piggin /*** Per cpu kva allocator ***/
764db64fe02SNick Piggin 
765db64fe02SNick Piggin /*
766db64fe02SNick Piggin  * vmap space is limited especially on 32 bit architectures. Ensure there is
767db64fe02SNick Piggin  * room for at least 16 percpu vmap blocks per CPU.
768db64fe02SNick Piggin  */
769db64fe02SNick Piggin /*
770db64fe02SNick Piggin  * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
771db64fe02SNick Piggin  * to #define VMALLOC_SPACE		(VMALLOC_END-VMALLOC_START). Guess
772db64fe02SNick Piggin  * instead (we just need a rough idea)
773db64fe02SNick Piggin  */
774db64fe02SNick Piggin #if BITS_PER_LONG == 32
775db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024)
776db64fe02SNick Piggin #else
777db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024*1024)
778db64fe02SNick Piggin #endif
779db64fe02SNick Piggin 
780db64fe02SNick Piggin #define VMALLOC_PAGES		(VMALLOC_SPACE / PAGE_SIZE)
781db64fe02SNick Piggin #define VMAP_MAX_ALLOC		BITS_PER_LONG	/* 256K with 4K pages */
782db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX	1024	/* 4MB with 4K pages */
783db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN	(VMAP_MAX_ALLOC*2)
784db64fe02SNick Piggin #define VMAP_MIN(x, y)		((x) < (y) ? (x) : (y)) /* can't use min() */
785db64fe02SNick Piggin #define VMAP_MAX(x, y)		((x) > (y) ? (x) : (y)) /* can't use max() */
786f982f915SClemens Ladisch #define VMAP_BBMAP_BITS		\
787f982f915SClemens Ladisch 		VMAP_MIN(VMAP_BBMAP_BITS_MAX,	\
788db64fe02SNick Piggin 		VMAP_MAX(VMAP_BBMAP_BITS_MIN,	\
789f982f915SClemens Ladisch 			VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
790db64fe02SNick Piggin 
791db64fe02SNick Piggin #define VMAP_BLOCK_SIZE		(VMAP_BBMAP_BITS * PAGE_SIZE)
792db64fe02SNick Piggin 
7939b463334SJeremy Fitzhardinge static bool vmap_initialized __read_mostly = false;
7949b463334SJeremy Fitzhardinge 
795db64fe02SNick Piggin struct vmap_block_queue {
796db64fe02SNick Piggin 	spinlock_t lock;
797db64fe02SNick Piggin 	struct list_head free;
798db64fe02SNick Piggin };
799db64fe02SNick Piggin 
800db64fe02SNick Piggin struct vmap_block {
801db64fe02SNick Piggin 	spinlock_t lock;
802db64fe02SNick Piggin 	struct vmap_area *va;
803db64fe02SNick Piggin 	unsigned long free, dirty;
8047d61bfe8SRoman Pen 	unsigned long dirty_min, dirty_max; /*< dirty range */
805db64fe02SNick Piggin 	struct list_head free_list;
806db64fe02SNick Piggin 	struct rcu_head rcu_head;
80702b709dfSNick Piggin 	struct list_head purge;
808db64fe02SNick Piggin };
809db64fe02SNick Piggin 
810db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
811db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
812db64fe02SNick Piggin 
813db64fe02SNick Piggin /*
814db64fe02SNick Piggin  * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
815db64fe02SNick Piggin  * in the free path. Could get rid of this if we change the API to return a
816db64fe02SNick Piggin  * "cookie" from alloc, to be passed to free. But no big deal yet.
817db64fe02SNick Piggin  */
818db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_block_tree_lock);
819db64fe02SNick Piggin static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
820db64fe02SNick Piggin 
821db64fe02SNick Piggin /*
822db64fe02SNick Piggin  * We should probably have a fallback mechanism to allocate virtual memory
823db64fe02SNick Piggin  * out of partially filled vmap blocks. However vmap block sizing should be
824db64fe02SNick Piggin  * fairly reasonable according to the vmalloc size, so it shouldn't be a
825db64fe02SNick Piggin  * big problem.
826db64fe02SNick Piggin  */
827db64fe02SNick Piggin 
828db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr)
829db64fe02SNick Piggin {
830db64fe02SNick Piggin 	addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
831db64fe02SNick Piggin 	addr /= VMAP_BLOCK_SIZE;
832db64fe02SNick Piggin 	return addr;
833db64fe02SNick Piggin }
834db64fe02SNick Piggin 
835cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
836cf725ce2SRoman Pen {
837cf725ce2SRoman Pen 	unsigned long addr;
838cf725ce2SRoman Pen 
839cf725ce2SRoman Pen 	addr = va_start + (pages_off << PAGE_SHIFT);
840cf725ce2SRoman Pen 	BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
841cf725ce2SRoman Pen 	return (void *)addr;
842cf725ce2SRoman Pen }
843cf725ce2SRoman Pen 
844cf725ce2SRoman Pen /**
845cf725ce2SRoman Pen  * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
846cf725ce2SRoman Pen  *                  block. Of course pages number can't exceed VMAP_BBMAP_BITS
847cf725ce2SRoman Pen  * @order:    how many 2^order pages should be occupied in newly allocated block
848cf725ce2SRoman Pen  * @gfp_mask: flags for the page level allocator
849cf725ce2SRoman Pen  *
850cf725ce2SRoman Pen  * Returns: virtual address in a newly allocated block or ERR_PTR(-errno)
851cf725ce2SRoman Pen  */
852cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
853db64fe02SNick Piggin {
854db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
855db64fe02SNick Piggin 	struct vmap_block *vb;
856db64fe02SNick Piggin 	struct vmap_area *va;
857db64fe02SNick Piggin 	unsigned long vb_idx;
858db64fe02SNick Piggin 	int node, err;
859cf725ce2SRoman Pen 	void *vaddr;
860db64fe02SNick Piggin 
861db64fe02SNick Piggin 	node = numa_node_id();
862db64fe02SNick Piggin 
863db64fe02SNick Piggin 	vb = kmalloc_node(sizeof(struct vmap_block),
864db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
865db64fe02SNick Piggin 	if (unlikely(!vb))
866db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
867db64fe02SNick Piggin 
868db64fe02SNick Piggin 	va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
869db64fe02SNick Piggin 					VMALLOC_START, VMALLOC_END,
870db64fe02SNick Piggin 					node, gfp_mask);
871ddf9c6d4STobias Klauser 	if (IS_ERR(va)) {
872db64fe02SNick Piggin 		kfree(vb);
873e7d86340SJulia Lawall 		return ERR_CAST(va);
874db64fe02SNick Piggin 	}
875db64fe02SNick Piggin 
876db64fe02SNick Piggin 	err = radix_tree_preload(gfp_mask);
877db64fe02SNick Piggin 	if (unlikely(err)) {
878db64fe02SNick Piggin 		kfree(vb);
879db64fe02SNick Piggin 		free_vmap_area(va);
880db64fe02SNick Piggin 		return ERR_PTR(err);
881db64fe02SNick Piggin 	}
882db64fe02SNick Piggin 
883cf725ce2SRoman Pen 	vaddr = vmap_block_vaddr(va->va_start, 0);
884db64fe02SNick Piggin 	spin_lock_init(&vb->lock);
885db64fe02SNick Piggin 	vb->va = va;
886cf725ce2SRoman Pen 	/* At least something should be left free */
887cf725ce2SRoman Pen 	BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
888cf725ce2SRoman Pen 	vb->free = VMAP_BBMAP_BITS - (1UL << order);
889db64fe02SNick Piggin 	vb->dirty = 0;
8907d61bfe8SRoman Pen 	vb->dirty_min = VMAP_BBMAP_BITS;
8917d61bfe8SRoman Pen 	vb->dirty_max = 0;
892db64fe02SNick Piggin 	INIT_LIST_HEAD(&vb->free_list);
893db64fe02SNick Piggin 
894db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(va->va_start);
895db64fe02SNick Piggin 	spin_lock(&vmap_block_tree_lock);
896db64fe02SNick Piggin 	err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
897db64fe02SNick Piggin 	spin_unlock(&vmap_block_tree_lock);
898db64fe02SNick Piggin 	BUG_ON(err);
899db64fe02SNick Piggin 	radix_tree_preload_end();
900db64fe02SNick Piggin 
901db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
902db64fe02SNick Piggin 	spin_lock(&vbq->lock);
90368ac546fSRoman Pen 	list_add_tail_rcu(&vb->free_list, &vbq->free);
904db64fe02SNick Piggin 	spin_unlock(&vbq->lock);
9053f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
906db64fe02SNick Piggin 
907cf725ce2SRoman Pen 	return vaddr;
908db64fe02SNick Piggin }
909db64fe02SNick Piggin 
910db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb)
911db64fe02SNick Piggin {
912db64fe02SNick Piggin 	struct vmap_block *tmp;
913db64fe02SNick Piggin 	unsigned long vb_idx;
914db64fe02SNick Piggin 
915db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(vb->va->va_start);
916db64fe02SNick Piggin 	spin_lock(&vmap_block_tree_lock);
917db64fe02SNick Piggin 	tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
918db64fe02SNick Piggin 	spin_unlock(&vmap_block_tree_lock);
919db64fe02SNick Piggin 	BUG_ON(tmp != vb);
920db64fe02SNick Piggin 
92164141da5SJeremy Fitzhardinge 	free_vmap_area_noflush(vb->va);
92222a3c7d1SLai Jiangshan 	kfree_rcu(vb, rcu_head);
923db64fe02SNick Piggin }
924db64fe02SNick Piggin 
92502b709dfSNick Piggin static void purge_fragmented_blocks(int cpu)
92602b709dfSNick Piggin {
92702b709dfSNick Piggin 	LIST_HEAD(purge);
92802b709dfSNick Piggin 	struct vmap_block *vb;
92902b709dfSNick Piggin 	struct vmap_block *n_vb;
93002b709dfSNick Piggin 	struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
93102b709dfSNick Piggin 
93202b709dfSNick Piggin 	rcu_read_lock();
93302b709dfSNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
93402b709dfSNick Piggin 
93502b709dfSNick Piggin 		if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
93602b709dfSNick Piggin 			continue;
93702b709dfSNick Piggin 
93802b709dfSNick Piggin 		spin_lock(&vb->lock);
93902b709dfSNick Piggin 		if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
94002b709dfSNick Piggin 			vb->free = 0; /* prevent further allocs after releasing lock */
94102b709dfSNick Piggin 			vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
9427d61bfe8SRoman Pen 			vb->dirty_min = 0;
9437d61bfe8SRoman Pen 			vb->dirty_max = VMAP_BBMAP_BITS;
94402b709dfSNick Piggin 			spin_lock(&vbq->lock);
94502b709dfSNick Piggin 			list_del_rcu(&vb->free_list);
94602b709dfSNick Piggin 			spin_unlock(&vbq->lock);
94702b709dfSNick Piggin 			spin_unlock(&vb->lock);
94802b709dfSNick Piggin 			list_add_tail(&vb->purge, &purge);
94902b709dfSNick Piggin 		} else
95002b709dfSNick Piggin 			spin_unlock(&vb->lock);
95102b709dfSNick Piggin 	}
95202b709dfSNick Piggin 	rcu_read_unlock();
95302b709dfSNick Piggin 
95402b709dfSNick Piggin 	list_for_each_entry_safe(vb, n_vb, &purge, purge) {
95502b709dfSNick Piggin 		list_del(&vb->purge);
95602b709dfSNick Piggin 		free_vmap_block(vb);
95702b709dfSNick Piggin 	}
95802b709dfSNick Piggin }
95902b709dfSNick Piggin 
96002b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void)
96102b709dfSNick Piggin {
96202b709dfSNick Piggin 	int cpu;
96302b709dfSNick Piggin 
96402b709dfSNick Piggin 	for_each_possible_cpu(cpu)
96502b709dfSNick Piggin 		purge_fragmented_blocks(cpu);
96602b709dfSNick Piggin }
96702b709dfSNick Piggin 
968db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
969db64fe02SNick Piggin {
970db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
971db64fe02SNick Piggin 	struct vmap_block *vb;
972cf725ce2SRoman Pen 	void *vaddr = NULL;
973db64fe02SNick Piggin 	unsigned int order;
974db64fe02SNick Piggin 
975891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
976db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
977aa91c4d8SJan Kara 	if (WARN_ON(size == 0)) {
978aa91c4d8SJan Kara 		/*
979aa91c4d8SJan Kara 		 * Allocating 0 bytes isn't what caller wants since
980aa91c4d8SJan Kara 		 * get_order(0) returns funny result. Just warn and terminate
981aa91c4d8SJan Kara 		 * early.
982aa91c4d8SJan Kara 		 */
983aa91c4d8SJan Kara 		return NULL;
984aa91c4d8SJan Kara 	}
985db64fe02SNick Piggin 	order = get_order(size);
986db64fe02SNick Piggin 
987db64fe02SNick Piggin 	rcu_read_lock();
988db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
989db64fe02SNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
990cf725ce2SRoman Pen 		unsigned long pages_off;
991db64fe02SNick Piggin 
992db64fe02SNick Piggin 		spin_lock(&vb->lock);
993cf725ce2SRoman Pen 		if (vb->free < (1UL << order)) {
994cf725ce2SRoman Pen 			spin_unlock(&vb->lock);
995cf725ce2SRoman Pen 			continue;
996cf725ce2SRoman Pen 		}
99702b709dfSNick Piggin 
998cf725ce2SRoman Pen 		pages_off = VMAP_BBMAP_BITS - vb->free;
999cf725ce2SRoman Pen 		vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
1000db64fe02SNick Piggin 		vb->free -= 1UL << order;
1001db64fe02SNick Piggin 		if (vb->free == 0) {
1002db64fe02SNick Piggin 			spin_lock(&vbq->lock);
1003de560423SNick Piggin 			list_del_rcu(&vb->free_list);
1004db64fe02SNick Piggin 			spin_unlock(&vbq->lock);
1005db64fe02SNick Piggin 		}
1006cf725ce2SRoman Pen 
1007db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1008db64fe02SNick Piggin 		break;
1009db64fe02SNick Piggin 	}
101002b709dfSNick Piggin 
10113f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
1012db64fe02SNick Piggin 	rcu_read_unlock();
1013db64fe02SNick Piggin 
1014cf725ce2SRoman Pen 	/* Allocate new block if nothing was found */
1015cf725ce2SRoman Pen 	if (!vaddr)
1016cf725ce2SRoman Pen 		vaddr = new_vmap_block(order, gfp_mask);
1017db64fe02SNick Piggin 
1018cf725ce2SRoman Pen 	return vaddr;
1019db64fe02SNick Piggin }
1020db64fe02SNick Piggin 
1021db64fe02SNick Piggin static void vb_free(const void *addr, unsigned long size)
1022db64fe02SNick Piggin {
1023db64fe02SNick Piggin 	unsigned long offset;
1024db64fe02SNick Piggin 	unsigned long vb_idx;
1025db64fe02SNick Piggin 	unsigned int order;
1026db64fe02SNick Piggin 	struct vmap_block *vb;
1027db64fe02SNick Piggin 
1028891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
1029db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
1030b29acbdcSNick Piggin 
1031b29acbdcSNick Piggin 	flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
1032b29acbdcSNick Piggin 
1033db64fe02SNick Piggin 	order = get_order(size);
1034db64fe02SNick Piggin 
1035db64fe02SNick Piggin 	offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
10367d61bfe8SRoman Pen 	offset >>= PAGE_SHIFT;
1037db64fe02SNick Piggin 
1038db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx((unsigned long)addr);
1039db64fe02SNick Piggin 	rcu_read_lock();
1040db64fe02SNick Piggin 	vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
1041db64fe02SNick Piggin 	rcu_read_unlock();
1042db64fe02SNick Piggin 	BUG_ON(!vb);
1043db64fe02SNick Piggin 
104464141da5SJeremy Fitzhardinge 	vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
104564141da5SJeremy Fitzhardinge 
1046db64fe02SNick Piggin 	spin_lock(&vb->lock);
10477d61bfe8SRoman Pen 
10487d61bfe8SRoman Pen 	/* Expand dirty range */
10497d61bfe8SRoman Pen 	vb->dirty_min = min(vb->dirty_min, offset);
10507d61bfe8SRoman Pen 	vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
1051d086817dSMinChan Kim 
1052db64fe02SNick Piggin 	vb->dirty += 1UL << order;
1053db64fe02SNick Piggin 	if (vb->dirty == VMAP_BBMAP_BITS) {
1054de560423SNick Piggin 		BUG_ON(vb->free);
1055db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1056db64fe02SNick Piggin 		free_vmap_block(vb);
1057db64fe02SNick Piggin 	} else
1058db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1059db64fe02SNick Piggin }
1060db64fe02SNick Piggin 
1061db64fe02SNick Piggin /**
1062db64fe02SNick Piggin  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1063db64fe02SNick Piggin  *
1064db64fe02SNick Piggin  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1065db64fe02SNick Piggin  * to amortize TLB flushing overheads. What this means is that any page you
1066db64fe02SNick Piggin  * have now, may, in a former life, have been mapped into kernel virtual
1067db64fe02SNick Piggin  * address by the vmap layer and so there might be some CPUs with TLB entries
1068db64fe02SNick Piggin  * still referencing that page (additional to the regular 1:1 kernel mapping).
1069db64fe02SNick Piggin  *
1070db64fe02SNick Piggin  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1071db64fe02SNick Piggin  * be sure that none of the pages we have control over will have any aliases
1072db64fe02SNick Piggin  * from the vmap layer.
1073db64fe02SNick Piggin  */
1074db64fe02SNick Piggin void vm_unmap_aliases(void)
1075db64fe02SNick Piggin {
1076db64fe02SNick Piggin 	unsigned long start = ULONG_MAX, end = 0;
1077db64fe02SNick Piggin 	int cpu;
1078db64fe02SNick Piggin 	int flush = 0;
1079db64fe02SNick Piggin 
10809b463334SJeremy Fitzhardinge 	if (unlikely(!vmap_initialized))
10819b463334SJeremy Fitzhardinge 		return;
10829b463334SJeremy Fitzhardinge 
10835803ed29SChristoph Hellwig 	might_sleep();
10845803ed29SChristoph Hellwig 
1085db64fe02SNick Piggin 	for_each_possible_cpu(cpu) {
1086db64fe02SNick Piggin 		struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1087db64fe02SNick Piggin 		struct vmap_block *vb;
1088db64fe02SNick Piggin 
1089db64fe02SNick Piggin 		rcu_read_lock();
1090db64fe02SNick Piggin 		list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1091db64fe02SNick Piggin 			spin_lock(&vb->lock);
10927d61bfe8SRoman Pen 			if (vb->dirty) {
10937d61bfe8SRoman Pen 				unsigned long va_start = vb->va->va_start;
1094db64fe02SNick Piggin 				unsigned long s, e;
1095b136be5eSJoonsoo Kim 
10967d61bfe8SRoman Pen 				s = va_start + (vb->dirty_min << PAGE_SHIFT);
10977d61bfe8SRoman Pen 				e = va_start + (vb->dirty_max << PAGE_SHIFT);
1098db64fe02SNick Piggin 
10997d61bfe8SRoman Pen 				start = min(s, start);
11007d61bfe8SRoman Pen 				end   = max(e, end);
11017d61bfe8SRoman Pen 
1102db64fe02SNick Piggin 				flush = 1;
1103db64fe02SNick Piggin 			}
1104db64fe02SNick Piggin 			spin_unlock(&vb->lock);
1105db64fe02SNick Piggin 		}
1106db64fe02SNick Piggin 		rcu_read_unlock();
1107db64fe02SNick Piggin 	}
1108db64fe02SNick Piggin 
1109f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
11100574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
11110574ecd1SChristoph Hellwig 	if (!__purge_vmap_area_lazy(start, end) && flush)
11120574ecd1SChristoph Hellwig 		flush_tlb_kernel_range(start, end);
1113f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
1114db64fe02SNick Piggin }
1115db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1116db64fe02SNick Piggin 
1117db64fe02SNick Piggin /**
1118db64fe02SNick Piggin  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1119db64fe02SNick Piggin  * @mem: the pointer returned by vm_map_ram
1120db64fe02SNick Piggin  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1121db64fe02SNick Piggin  */
1122db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count)
1123db64fe02SNick Piggin {
112465ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
1125db64fe02SNick Piggin 	unsigned long addr = (unsigned long)mem;
11269c3acf60SChristoph Hellwig 	struct vmap_area *va;
1127db64fe02SNick Piggin 
11285803ed29SChristoph Hellwig 	might_sleep();
1129db64fe02SNick Piggin 	BUG_ON(!addr);
1130db64fe02SNick Piggin 	BUG_ON(addr < VMALLOC_START);
1131db64fe02SNick Piggin 	BUG_ON(addr > VMALLOC_END);
1132a1c0b1a0SShawn Lin 	BUG_ON(!PAGE_ALIGNED(addr));
1133db64fe02SNick Piggin 
1134db64fe02SNick Piggin 	debug_check_no_locks_freed(mem, size);
1135cd52858cSNick Piggin 	vmap_debug_free_range(addr, addr+size);
1136db64fe02SNick Piggin 
11379c3acf60SChristoph Hellwig 	if (likely(count <= VMAP_MAX_ALLOC)) {
1138db64fe02SNick Piggin 		vb_free(mem, size);
11399c3acf60SChristoph Hellwig 		return;
11409c3acf60SChristoph Hellwig 	}
11419c3acf60SChristoph Hellwig 
11429c3acf60SChristoph Hellwig 	va = find_vmap_area(addr);
11439c3acf60SChristoph Hellwig 	BUG_ON(!va);
11449c3acf60SChristoph Hellwig 	free_unmap_vmap_area(va);
1145db64fe02SNick Piggin }
1146db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram);
1147db64fe02SNick Piggin 
1148db64fe02SNick Piggin /**
1149db64fe02SNick Piggin  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1150db64fe02SNick Piggin  * @pages: an array of pointers to the pages to be mapped
1151db64fe02SNick Piggin  * @count: number of pages
1152db64fe02SNick Piggin  * @node: prefer to allocate data structures on this node
1153db64fe02SNick Piggin  * @prot: memory protection to use. PAGE_KERNEL for regular RAM
1154e99c97adSRandy Dunlap  *
115536437638SGioh Kim  * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
115636437638SGioh Kim  * faster than vmap so it's good.  But if you mix long-life and short-life
115736437638SGioh Kim  * objects with vm_map_ram(), it could consume lots of address space through
115836437638SGioh Kim  * fragmentation (especially on a 32bit machine).  You could see failures in
115936437638SGioh Kim  * the end.  Please use this function for short-lived objects.
116036437638SGioh Kim  *
1161e99c97adSRandy Dunlap  * Returns: a pointer to the address that has been mapped, or %NULL on failure
1162db64fe02SNick Piggin  */
1163db64fe02SNick Piggin void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1164db64fe02SNick Piggin {
116565ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
1166db64fe02SNick Piggin 	unsigned long addr;
1167db64fe02SNick Piggin 	void *mem;
1168db64fe02SNick Piggin 
1169db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC)) {
1170db64fe02SNick Piggin 		mem = vb_alloc(size, GFP_KERNEL);
1171db64fe02SNick Piggin 		if (IS_ERR(mem))
1172db64fe02SNick Piggin 			return NULL;
1173db64fe02SNick Piggin 		addr = (unsigned long)mem;
1174db64fe02SNick Piggin 	} else {
1175db64fe02SNick Piggin 		struct vmap_area *va;
1176db64fe02SNick Piggin 		va = alloc_vmap_area(size, PAGE_SIZE,
1177db64fe02SNick Piggin 				VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1178db64fe02SNick Piggin 		if (IS_ERR(va))
1179db64fe02SNick Piggin 			return NULL;
1180db64fe02SNick Piggin 
1181db64fe02SNick Piggin 		addr = va->va_start;
1182db64fe02SNick Piggin 		mem = (void *)addr;
1183db64fe02SNick Piggin 	}
1184db64fe02SNick Piggin 	if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1185db64fe02SNick Piggin 		vm_unmap_ram(mem, count);
1186db64fe02SNick Piggin 		return NULL;
1187db64fe02SNick Piggin 	}
1188db64fe02SNick Piggin 	return mem;
1189db64fe02SNick Piggin }
1190db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram);
1191db64fe02SNick Piggin 
11924341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata;
1193f0aa6617STejun Heo /**
1194be9b7335SNicolas Pitre  * vm_area_add_early - add vmap area early during boot
1195be9b7335SNicolas Pitre  * @vm: vm_struct to add
1196be9b7335SNicolas Pitre  *
1197be9b7335SNicolas Pitre  * This function is used to add fixed kernel vm area to vmlist before
1198be9b7335SNicolas Pitre  * vmalloc_init() is called.  @vm->addr, @vm->size, and @vm->flags
1199be9b7335SNicolas Pitre  * should contain proper values and the other fields should be zero.
1200be9b7335SNicolas Pitre  *
1201be9b7335SNicolas Pitre  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1202be9b7335SNicolas Pitre  */
1203be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm)
1204be9b7335SNicolas Pitre {
1205be9b7335SNicolas Pitre 	struct vm_struct *tmp, **p;
1206be9b7335SNicolas Pitre 
1207be9b7335SNicolas Pitre 	BUG_ON(vmap_initialized);
1208be9b7335SNicolas Pitre 	for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1209be9b7335SNicolas Pitre 		if (tmp->addr >= vm->addr) {
1210be9b7335SNicolas Pitre 			BUG_ON(tmp->addr < vm->addr + vm->size);
1211be9b7335SNicolas Pitre 			break;
1212be9b7335SNicolas Pitre 		} else
1213be9b7335SNicolas Pitre 			BUG_ON(tmp->addr + tmp->size > vm->addr);
1214be9b7335SNicolas Pitre 	}
1215be9b7335SNicolas Pitre 	vm->next = *p;
1216be9b7335SNicolas Pitre 	*p = vm;
1217be9b7335SNicolas Pitre }
1218be9b7335SNicolas Pitre 
1219be9b7335SNicolas Pitre /**
1220f0aa6617STejun Heo  * vm_area_register_early - register vmap area early during boot
1221f0aa6617STejun Heo  * @vm: vm_struct to register
1222c0c0a293STejun Heo  * @align: requested alignment
1223f0aa6617STejun Heo  *
1224f0aa6617STejun Heo  * This function is used to register kernel vm area before
1225f0aa6617STejun Heo  * vmalloc_init() is called.  @vm->size and @vm->flags should contain
1226f0aa6617STejun Heo  * proper values on entry and other fields should be zero.  On return,
1227f0aa6617STejun Heo  * vm->addr contains the allocated address.
1228f0aa6617STejun Heo  *
1229f0aa6617STejun Heo  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1230f0aa6617STejun Heo  */
1231c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1232f0aa6617STejun Heo {
1233f0aa6617STejun Heo 	static size_t vm_init_off __initdata;
1234c0c0a293STejun Heo 	unsigned long addr;
1235f0aa6617STejun Heo 
1236c0c0a293STejun Heo 	addr = ALIGN(VMALLOC_START + vm_init_off, align);
1237c0c0a293STejun Heo 	vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1238c0c0a293STejun Heo 
1239c0c0a293STejun Heo 	vm->addr = (void *)addr;
1240f0aa6617STejun Heo 
1241be9b7335SNicolas Pitre 	vm_area_add_early(vm);
1242f0aa6617STejun Heo }
1243f0aa6617STejun Heo 
1244db64fe02SNick Piggin void __init vmalloc_init(void)
1245db64fe02SNick Piggin {
1246822c18f2SIvan Kokshaysky 	struct vmap_area *va;
1247822c18f2SIvan Kokshaysky 	struct vm_struct *tmp;
1248db64fe02SNick Piggin 	int i;
1249db64fe02SNick Piggin 
1250db64fe02SNick Piggin 	for_each_possible_cpu(i) {
1251db64fe02SNick Piggin 		struct vmap_block_queue *vbq;
125232fcfd40SAl Viro 		struct vfree_deferred *p;
1253db64fe02SNick Piggin 
1254db64fe02SNick Piggin 		vbq = &per_cpu(vmap_block_queue, i);
1255db64fe02SNick Piggin 		spin_lock_init(&vbq->lock);
1256db64fe02SNick Piggin 		INIT_LIST_HEAD(&vbq->free);
125732fcfd40SAl Viro 		p = &per_cpu(vfree_deferred, i);
125832fcfd40SAl Viro 		init_llist_head(&p->list);
125932fcfd40SAl Viro 		INIT_WORK(&p->wq, free_work);
1260db64fe02SNick Piggin 	}
12619b463334SJeremy Fitzhardinge 
1262822c18f2SIvan Kokshaysky 	/* Import existing vmlist entries. */
1263822c18f2SIvan Kokshaysky 	for (tmp = vmlist; tmp; tmp = tmp->next) {
126443ebdac4SPekka Enberg 		va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
1265dbda591dSKyongHo 		va->flags = VM_VM_AREA;
1266822c18f2SIvan Kokshaysky 		va->va_start = (unsigned long)tmp->addr;
1267822c18f2SIvan Kokshaysky 		va->va_end = va->va_start + tmp->size;
1268dbda591dSKyongHo 		va->vm = tmp;
1269822c18f2SIvan Kokshaysky 		__insert_vmap_area(va);
1270822c18f2SIvan Kokshaysky 	}
1271ca23e405STejun Heo 
1272ca23e405STejun Heo 	vmap_area_pcpu_hole = VMALLOC_END;
1273ca23e405STejun Heo 
12749b463334SJeremy Fitzhardinge 	vmap_initialized = true;
1275db64fe02SNick Piggin }
1276db64fe02SNick Piggin 
12778fc48985STejun Heo /**
12788fc48985STejun Heo  * map_kernel_range_noflush - map kernel VM area with the specified pages
12798fc48985STejun Heo  * @addr: start of the VM area to map
12808fc48985STejun Heo  * @size: size of the VM area to map
12818fc48985STejun Heo  * @prot: page protection flags to use
12828fc48985STejun Heo  * @pages: pages to map
12838fc48985STejun Heo  *
12848fc48985STejun Heo  * Map PFN_UP(@size) pages at @addr.  The VM area @addr and @size
12858fc48985STejun Heo  * specify should have been allocated using get_vm_area() and its
12868fc48985STejun Heo  * friends.
12878fc48985STejun Heo  *
12888fc48985STejun Heo  * NOTE:
12898fc48985STejun Heo  * This function does NOT do any cache flushing.  The caller is
12908fc48985STejun Heo  * responsible for calling flush_cache_vmap() on to-be-mapped areas
12918fc48985STejun Heo  * before calling this function.
12928fc48985STejun Heo  *
12938fc48985STejun Heo  * RETURNS:
12948fc48985STejun Heo  * The number of pages mapped on success, -errno on failure.
12958fc48985STejun Heo  */
12968fc48985STejun Heo int map_kernel_range_noflush(unsigned long addr, unsigned long size,
12978fc48985STejun Heo 			     pgprot_t prot, struct page **pages)
12988fc48985STejun Heo {
12998fc48985STejun Heo 	return vmap_page_range_noflush(addr, addr + size, prot, pages);
13008fc48985STejun Heo }
13018fc48985STejun Heo 
13028fc48985STejun Heo /**
13038fc48985STejun Heo  * unmap_kernel_range_noflush - unmap kernel VM area
13048fc48985STejun Heo  * @addr: start of the VM area to unmap
13058fc48985STejun Heo  * @size: size of the VM area to unmap
13068fc48985STejun Heo  *
13078fc48985STejun Heo  * Unmap PFN_UP(@size) pages at @addr.  The VM area @addr and @size
13088fc48985STejun Heo  * specify should have been allocated using get_vm_area() and its
13098fc48985STejun Heo  * friends.
13108fc48985STejun Heo  *
13118fc48985STejun Heo  * NOTE:
13128fc48985STejun Heo  * This function does NOT do any cache flushing.  The caller is
13138fc48985STejun Heo  * responsible for calling flush_cache_vunmap() on to-be-mapped areas
13148fc48985STejun Heo  * before calling this function and flush_tlb_kernel_range() after.
13158fc48985STejun Heo  */
13168fc48985STejun Heo void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
13178fc48985STejun Heo {
13188fc48985STejun Heo 	vunmap_page_range(addr, addr + size);
13198fc48985STejun Heo }
132081e88fdcSHuang Ying EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
13218fc48985STejun Heo 
13228fc48985STejun Heo /**
13238fc48985STejun Heo  * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
13248fc48985STejun Heo  * @addr: start of the VM area to unmap
13258fc48985STejun Heo  * @size: size of the VM area to unmap
13268fc48985STejun Heo  *
13278fc48985STejun Heo  * Similar to unmap_kernel_range_noflush() but flushes vcache before
13288fc48985STejun Heo  * the unmapping and tlb after.
13298fc48985STejun Heo  */
1330db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size)
1331db64fe02SNick Piggin {
1332db64fe02SNick Piggin 	unsigned long end = addr + size;
1333f6fcba70STejun Heo 
1334f6fcba70STejun Heo 	flush_cache_vunmap(addr, end);
1335db64fe02SNick Piggin 	vunmap_page_range(addr, end);
1336db64fe02SNick Piggin 	flush_tlb_kernel_range(addr, end);
1337db64fe02SNick Piggin }
133893ef6d6cSMinchan Kim EXPORT_SYMBOL_GPL(unmap_kernel_range);
1339db64fe02SNick Piggin 
1340f6f8ed47SWANG Chao int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages)
1341db64fe02SNick Piggin {
1342db64fe02SNick Piggin 	unsigned long addr = (unsigned long)area->addr;
1343762216abSWanpeng Li 	unsigned long end = addr + get_vm_area_size(area);
1344db64fe02SNick Piggin 	int err;
1345db64fe02SNick Piggin 
1346f6f8ed47SWANG Chao 	err = vmap_page_range(addr, end, prot, pages);
1347db64fe02SNick Piggin 
1348f6f8ed47SWANG Chao 	return err > 0 ? 0 : err;
1349db64fe02SNick Piggin }
1350db64fe02SNick Piggin EXPORT_SYMBOL_GPL(map_vm_area);
1351db64fe02SNick Piggin 
1352f5252e00SMitsuo Hayasaka static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
13535e6cafc8SMarek Szyprowski 			      unsigned long flags, const void *caller)
1354cf88c790STejun Heo {
1355c69480adSJoonsoo Kim 	spin_lock(&vmap_area_lock);
1356cf88c790STejun Heo 	vm->flags = flags;
1357cf88c790STejun Heo 	vm->addr = (void *)va->va_start;
1358cf88c790STejun Heo 	vm->size = va->va_end - va->va_start;
1359cf88c790STejun Heo 	vm->caller = caller;
1360db1aecafSMinchan Kim 	va->vm = vm;
1361cf88c790STejun Heo 	va->flags |= VM_VM_AREA;
1362c69480adSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
1363f5252e00SMitsuo Hayasaka }
1364cf88c790STejun Heo 
136520fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm)
1366f5252e00SMitsuo Hayasaka {
1367d4033afdSJoonsoo Kim 	/*
136820fc02b4SZhang Yanfei 	 * Before removing VM_UNINITIALIZED,
1369d4033afdSJoonsoo Kim 	 * we should make sure that vm has proper values.
1370d4033afdSJoonsoo Kim 	 * Pair with smp_rmb() in show_numa_info().
1371d4033afdSJoonsoo Kim 	 */
1372d4033afdSJoonsoo Kim 	smp_wmb();
137320fc02b4SZhang Yanfei 	vm->flags &= ~VM_UNINITIALIZED;
1374cf88c790STejun Heo }
1375cf88c790STejun Heo 
1376db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size,
13772dca6999SDavid Miller 		unsigned long align, unsigned long flags, unsigned long start,
13785e6cafc8SMarek Szyprowski 		unsigned long end, int node, gfp_t gfp_mask, const void *caller)
1379db64fe02SNick Piggin {
13800006526dSKautuk Consul 	struct vmap_area *va;
1381db64fe02SNick Piggin 	struct vm_struct *area;
13821da177e4SLinus Torvalds 
138352fd24caSGiridhar Pemmasani 	BUG_ON(in_interrupt());
13841da177e4SLinus Torvalds 	size = PAGE_ALIGN(size);
138531be8309SOGAWA Hirofumi 	if (unlikely(!size))
138631be8309SOGAWA Hirofumi 		return NULL;
13871da177e4SLinus Torvalds 
1388252e5c6eSzijun_hu 	if (flags & VM_IOREMAP)
1389252e5c6eSzijun_hu 		align = 1ul << clamp_t(int, get_count_order_long(size),
1390252e5c6eSzijun_hu 				       PAGE_SHIFT, IOREMAP_MAX_ORDER);
1391252e5c6eSzijun_hu 
1392cf88c790STejun Heo 	area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
13931da177e4SLinus Torvalds 	if (unlikely(!area))
13941da177e4SLinus Torvalds 		return NULL;
13951da177e4SLinus Torvalds 
139671394fe5SAndrey Ryabinin 	if (!(flags & VM_NO_GUARD))
13971da177e4SLinus Torvalds 		size += PAGE_SIZE;
13981da177e4SLinus Torvalds 
1399db64fe02SNick Piggin 	va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1400db64fe02SNick Piggin 	if (IS_ERR(va)) {
1401db64fe02SNick Piggin 		kfree(area);
1402db64fe02SNick Piggin 		return NULL;
14031da177e4SLinus Torvalds 	}
14041da177e4SLinus Torvalds 
1405f5252e00SMitsuo Hayasaka 	setup_vmalloc_vm(area, va, flags, caller);
1406f5252e00SMitsuo Hayasaka 
14071da177e4SLinus Torvalds 	return area;
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
1410930fc45aSChristoph Lameter struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1411930fc45aSChristoph Lameter 				unsigned long start, unsigned long end)
1412930fc45aSChristoph Lameter {
141300ef2d2fSDavid Rientjes 	return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
141400ef2d2fSDavid Rientjes 				  GFP_KERNEL, __builtin_return_address(0));
1415930fc45aSChristoph Lameter }
14165992b6daSRusty Russell EXPORT_SYMBOL_GPL(__get_vm_area);
1417930fc45aSChristoph Lameter 
1418c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1419c2968612SBenjamin Herrenschmidt 				       unsigned long start, unsigned long end,
14205e6cafc8SMarek Szyprowski 				       const void *caller)
1421c2968612SBenjamin Herrenschmidt {
142200ef2d2fSDavid Rientjes 	return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
142300ef2d2fSDavid Rientjes 				  GFP_KERNEL, caller);
1424c2968612SBenjamin Herrenschmidt }
1425c2968612SBenjamin Herrenschmidt 
14261da177e4SLinus Torvalds /**
1427183ff22bSSimon Arlott  *	get_vm_area  -  reserve a contiguous kernel virtual area
14281da177e4SLinus Torvalds  *	@size:		size of the area
14291da177e4SLinus Torvalds  *	@flags:		%VM_IOREMAP for I/O mappings or VM_ALLOC
14301da177e4SLinus Torvalds  *
14311da177e4SLinus Torvalds  *	Search an area of @size in the kernel virtual mapping area,
14321da177e4SLinus Torvalds  *	and reserved it for out purposes.  Returns the area descriptor
14331da177e4SLinus Torvalds  *	on success or %NULL on failure.
14341da177e4SLinus Torvalds  */
14351da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
14361da177e4SLinus Torvalds {
14372dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
143800ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL,
143900ef2d2fSDavid Rientjes 				  __builtin_return_address(0));
144023016969SChristoph Lameter }
144123016969SChristoph Lameter 
144223016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
14435e6cafc8SMarek Szyprowski 				const void *caller)
144423016969SChristoph Lameter {
14452dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
144600ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL, caller);
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds 
1449e9da6e99SMarek Szyprowski /**
1450e9da6e99SMarek Szyprowski  *	find_vm_area  -  find a continuous kernel virtual area
1451e9da6e99SMarek Szyprowski  *	@addr:		base address
1452e9da6e99SMarek Szyprowski  *
1453e9da6e99SMarek Szyprowski  *	Search for the kernel VM area starting at @addr, and return it.
1454e9da6e99SMarek Szyprowski  *	It is up to the caller to do all required locking to keep the returned
1455e9da6e99SMarek Szyprowski  *	pointer valid.
1456e9da6e99SMarek Szyprowski  */
1457e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr)
145883342314SNick Piggin {
1459db64fe02SNick Piggin 	struct vmap_area *va;
146083342314SNick Piggin 
1461db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
1462db64fe02SNick Piggin 	if (va && va->flags & VM_VM_AREA)
1463db1aecafSMinchan Kim 		return va->vm;
146483342314SNick Piggin 
14657856dfebSAndi Kleen 	return NULL;
14667856dfebSAndi Kleen }
14677856dfebSAndi Kleen 
14681da177e4SLinus Torvalds /**
1469183ff22bSSimon Arlott  *	remove_vm_area  -  find and remove a continuous kernel virtual area
14701da177e4SLinus Torvalds  *	@addr:		base address
14711da177e4SLinus Torvalds  *
14721da177e4SLinus Torvalds  *	Search for the kernel VM area starting at @addr, and remove it.
14731da177e4SLinus Torvalds  *	This function returns the found VM area, but using it is NOT safe
14747856dfebSAndi Kleen  *	on SMP machines, except for its size or flags.
14751da177e4SLinus Torvalds  */
1476b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr)
14771da177e4SLinus Torvalds {
1478db64fe02SNick Piggin 	struct vmap_area *va;
1479db64fe02SNick Piggin 
14805803ed29SChristoph Hellwig 	might_sleep();
14815803ed29SChristoph Hellwig 
1482db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
1483db64fe02SNick Piggin 	if (va && va->flags & VM_VM_AREA) {
1484db1aecafSMinchan Kim 		struct vm_struct *vm = va->vm;
1485f5252e00SMitsuo Hayasaka 
1486c69480adSJoonsoo Kim 		spin_lock(&vmap_area_lock);
1487c69480adSJoonsoo Kim 		va->vm = NULL;
1488c69480adSJoonsoo Kim 		va->flags &= ~VM_VM_AREA;
1489c69480adSJoonsoo Kim 		spin_unlock(&vmap_area_lock);
1490c69480adSJoonsoo Kim 
1491dd32c279SKAMEZAWA Hiroyuki 		vmap_debug_free_range(va->va_start, va->va_end);
1492a5af5aa8SAndrey Ryabinin 		kasan_free_shadow(vm);
1493dd32c279SKAMEZAWA Hiroyuki 		free_unmap_vmap_area(va);
1494dd32c279SKAMEZAWA Hiroyuki 
1495db64fe02SNick Piggin 		return vm;
1496db64fe02SNick Piggin 	}
1497db64fe02SNick Piggin 	return NULL;
14981da177e4SLinus Torvalds }
14991da177e4SLinus Torvalds 
1500b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages)
15011da177e4SLinus Torvalds {
15021da177e4SLinus Torvalds 	struct vm_struct *area;
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds 	if (!addr)
15051da177e4SLinus Torvalds 		return;
15061da177e4SLinus Torvalds 
1507e69e9d4aSHATAYAMA Daisuke 	if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
1508ab15d9b4SDan Carpenter 			addr))
15091da177e4SLinus Torvalds 		return;
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	area = remove_vm_area(addr);
15121da177e4SLinus Torvalds 	if (unlikely(!area)) {
15134c8573e2SArjan van de Ven 		WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
15141da177e4SLinus Torvalds 				addr);
15151da177e4SLinus Torvalds 		return;
15161da177e4SLinus Torvalds 	}
15171da177e4SLinus Torvalds 
15187511c3edSJerome Marchand 	debug_check_no_locks_freed(addr, get_vm_area_size(area));
15197511c3edSJerome Marchand 	debug_check_no_obj_freed(addr, get_vm_area_size(area));
15209a11b49aSIngo Molnar 
15211da177e4SLinus Torvalds 	if (deallocate_pages) {
15221da177e4SLinus Torvalds 		int i;
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 		for (i = 0; i < area->nr_pages; i++) {
1525bf53d6f8SChristoph Lameter 			struct page *page = area->pages[i];
1526bf53d6f8SChristoph Lameter 
1527bf53d6f8SChristoph Lameter 			BUG_ON(!page);
15284949148aSVladimir Davydov 			__free_pages(page, 0);
15291da177e4SLinus Torvalds 		}
15301da177e4SLinus Torvalds 
1531244d63eeSDavid Rientjes 		kvfree(area->pages);
15321da177e4SLinus Torvalds 	}
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 	kfree(area);
15351da177e4SLinus Torvalds 	return;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
1538bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr)
1539bf22e37aSAndrey Ryabinin {
1540bf22e37aSAndrey Ryabinin 	/*
1541bf22e37aSAndrey Ryabinin 	 * Use raw_cpu_ptr() because this can be called from preemptible
1542bf22e37aSAndrey Ryabinin 	 * context. Preemption is absolutely fine here, because the llist_add()
1543bf22e37aSAndrey Ryabinin 	 * implementation is lockless, so it works even if we are adding to
1544bf22e37aSAndrey Ryabinin 	 * nother cpu's list.  schedule_work() should be fine with this too.
1545bf22e37aSAndrey Ryabinin 	 */
1546bf22e37aSAndrey Ryabinin 	struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
1547bf22e37aSAndrey Ryabinin 
1548bf22e37aSAndrey Ryabinin 	if (llist_add((struct llist_node *)addr, &p->list))
1549bf22e37aSAndrey Ryabinin 		schedule_work(&p->wq);
1550bf22e37aSAndrey Ryabinin }
1551bf22e37aSAndrey Ryabinin 
1552bf22e37aSAndrey Ryabinin /**
1553bf22e37aSAndrey Ryabinin  *	vfree_atomic  -  release memory allocated by vmalloc()
1554bf22e37aSAndrey Ryabinin  *	@addr:		memory base address
1555bf22e37aSAndrey Ryabinin  *
1556bf22e37aSAndrey Ryabinin  *	This one is just like vfree() but can be called in any atomic context
1557bf22e37aSAndrey Ryabinin  *	except NMIs.
1558bf22e37aSAndrey Ryabinin  */
1559bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr)
1560bf22e37aSAndrey Ryabinin {
1561bf22e37aSAndrey Ryabinin 	BUG_ON(in_nmi());
1562bf22e37aSAndrey Ryabinin 
1563bf22e37aSAndrey Ryabinin 	kmemleak_free(addr);
1564bf22e37aSAndrey Ryabinin 
1565bf22e37aSAndrey Ryabinin 	if (!addr)
1566bf22e37aSAndrey Ryabinin 		return;
1567bf22e37aSAndrey Ryabinin 	__vfree_deferred(addr);
1568bf22e37aSAndrey Ryabinin }
1569bf22e37aSAndrey Ryabinin 
15701da177e4SLinus Torvalds /**
15711da177e4SLinus Torvalds  *	vfree  -  release memory allocated by vmalloc()
15721da177e4SLinus Torvalds  *	@addr:		memory base address
15731da177e4SLinus Torvalds  *
1574183ff22bSSimon Arlott  *	Free the virtually continuous memory area starting at @addr, as
157580e93effSPekka Enberg  *	obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
157680e93effSPekka Enberg  *	NULL, no operation is performed.
15771da177e4SLinus Torvalds  *
157832fcfd40SAl Viro  *	Must not be called in NMI context (strictly speaking, only if we don't
157932fcfd40SAl Viro  *	have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
158032fcfd40SAl Viro  *	conventions for vfree() arch-depenedent would be a really bad idea)
158132fcfd40SAl Viro  *
1582c9fcee51SAndrew Morton  *	NOTE: assumes that the object at *addr has a size >= sizeof(llist_node)
15831da177e4SLinus Torvalds  */
1584b3bdda02SChristoph Lameter void vfree(const void *addr)
15851da177e4SLinus Torvalds {
158632fcfd40SAl Viro 	BUG_ON(in_nmi());
158789219d37SCatalin Marinas 
158889219d37SCatalin Marinas 	kmemleak_free(addr);
158989219d37SCatalin Marinas 
159032fcfd40SAl Viro 	if (!addr)
159132fcfd40SAl Viro 		return;
1592bf22e37aSAndrey Ryabinin 	if (unlikely(in_interrupt()))
1593bf22e37aSAndrey Ryabinin 		__vfree_deferred(addr);
1594bf22e37aSAndrey Ryabinin 	else
15951da177e4SLinus Torvalds 		__vunmap(addr, 1);
15961da177e4SLinus Torvalds }
15971da177e4SLinus Torvalds EXPORT_SYMBOL(vfree);
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds /**
16001da177e4SLinus Torvalds  *	vunmap  -  release virtual mapping obtained by vmap()
16011da177e4SLinus Torvalds  *	@addr:		memory base address
16021da177e4SLinus Torvalds  *
16031da177e4SLinus Torvalds  *	Free the virtually contiguous memory area starting at @addr,
16041da177e4SLinus Torvalds  *	which was created from the page array passed to vmap().
16051da177e4SLinus Torvalds  *
160680e93effSPekka Enberg  *	Must not be called in interrupt context.
16071da177e4SLinus Torvalds  */
1608b3bdda02SChristoph Lameter void vunmap(const void *addr)
16091da177e4SLinus Torvalds {
16101da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
161134754b69SPeter Zijlstra 	might_sleep();
161232fcfd40SAl Viro 	if (addr)
16131da177e4SLinus Torvalds 		__vunmap(addr, 0);
16141da177e4SLinus Torvalds }
16151da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap);
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds /**
16181da177e4SLinus Torvalds  *	vmap  -  map an array of pages into virtually contiguous space
16191da177e4SLinus Torvalds  *	@pages:		array of page pointers
16201da177e4SLinus Torvalds  *	@count:		number of pages to map
16211da177e4SLinus Torvalds  *	@flags:		vm_area->flags
16221da177e4SLinus Torvalds  *	@prot:		page protection for the mapping
16231da177e4SLinus Torvalds  *
16241da177e4SLinus Torvalds  *	Maps @count pages from @pages into contiguous kernel virtual
16251da177e4SLinus Torvalds  *	space.
16261da177e4SLinus Torvalds  */
16271da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count,
16281da177e4SLinus Torvalds 		unsigned long flags, pgprot_t prot)
16291da177e4SLinus Torvalds {
16301da177e4SLinus Torvalds 	struct vm_struct *area;
163165ee03c4SGuillermo Julián Moreno 	unsigned long size;		/* In bytes */
16321da177e4SLinus Torvalds 
163334754b69SPeter Zijlstra 	might_sleep();
163434754b69SPeter Zijlstra 
16354481374cSJan Beulich 	if (count > totalram_pages)
16361da177e4SLinus Torvalds 		return NULL;
16371da177e4SLinus Torvalds 
163865ee03c4SGuillermo Julián Moreno 	size = (unsigned long)count << PAGE_SHIFT;
163965ee03c4SGuillermo Julián Moreno 	area = get_vm_area_caller(size, flags, __builtin_return_address(0));
16401da177e4SLinus Torvalds 	if (!area)
16411da177e4SLinus Torvalds 		return NULL;
164223016969SChristoph Lameter 
1643f6f8ed47SWANG Chao 	if (map_vm_area(area, prot, pages)) {
16441da177e4SLinus Torvalds 		vunmap(area->addr);
16451da177e4SLinus Torvalds 		return NULL;
16461da177e4SLinus Torvalds 	}
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	return area->addr;
16491da177e4SLinus Torvalds }
16501da177e4SLinus Torvalds EXPORT_SYMBOL(vmap);
16511da177e4SLinus Torvalds 
16522dca6999SDavid Miller static void *__vmalloc_node(unsigned long size, unsigned long align,
16532dca6999SDavid Miller 			    gfp_t gfp_mask, pgprot_t prot,
16545e6cafc8SMarek Szyprowski 			    int node, const void *caller);
1655e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
16563722e13cSWanpeng Li 				 pgprot_t prot, int node)
16571da177e4SLinus Torvalds {
16581da177e4SLinus Torvalds 	struct page **pages;
16591da177e4SLinus Torvalds 	unsigned int nr_pages, array_size, i;
1660930f036bSDavid Rientjes 	const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
1661930f036bSDavid Rientjes 	const gfp_t alloc_mask = gfp_mask | __GFP_NOWARN;
16621da177e4SLinus Torvalds 
1663762216abSWanpeng Li 	nr_pages = get_vm_area_size(area) >> PAGE_SHIFT;
16641da177e4SLinus Torvalds 	array_size = (nr_pages * sizeof(struct page *));
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 	area->nr_pages = nr_pages;
16671da177e4SLinus Torvalds 	/* Please note that the recursion is strictly bounded. */
16688757d5faSJan Kiszka 	if (array_size > PAGE_SIZE) {
1669976d6dfbSJan Beulich 		pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,
16703722e13cSWanpeng Li 				PAGE_KERNEL, node, area->caller);
1671286e1ea3SAndrew Morton 	} else {
1672976d6dfbSJan Beulich 		pages = kmalloc_node(array_size, nested_gfp, node);
1673286e1ea3SAndrew Morton 	}
16741da177e4SLinus Torvalds 	area->pages = pages;
16751da177e4SLinus Torvalds 	if (!area->pages) {
16761da177e4SLinus Torvalds 		remove_vm_area(area->addr);
16771da177e4SLinus Torvalds 		kfree(area);
16781da177e4SLinus Torvalds 		return NULL;
16791da177e4SLinus Torvalds 	}
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 	for (i = 0; i < area->nr_pages; i++) {
1682bf53d6f8SChristoph Lameter 		struct page *page;
1683bf53d6f8SChristoph Lameter 
16845d17a73aSMichal Hocko 		if (fatal_signal_pending(current)) {
16855d17a73aSMichal Hocko 			area->nr_pages = i;
16865d17a73aSMichal Hocko 			goto fail;
16875d17a73aSMichal Hocko 		}
16885d17a73aSMichal Hocko 
16894b90951cSJianguo Wu 		if (node == NUMA_NO_NODE)
16907877cdccSMichal Hocko 			page = alloc_page(alloc_mask);
1691930fc45aSChristoph Lameter 		else
16927877cdccSMichal Hocko 			page = alloc_pages_node(node, alloc_mask, 0);
1693bf53d6f8SChristoph Lameter 
1694bf53d6f8SChristoph Lameter 		if (unlikely(!page)) {
16951da177e4SLinus Torvalds 			/* Successfully allocated i pages, free them in __vunmap() */
16961da177e4SLinus Torvalds 			area->nr_pages = i;
16971da177e4SLinus Torvalds 			goto fail;
16981da177e4SLinus Torvalds 		}
1699bf53d6f8SChristoph Lameter 		area->pages[i] = page;
1700d0164adcSMel Gorman 		if (gfpflags_allow_blocking(gfp_mask))
1701660654f9SEric Dumazet 			cond_resched();
17021da177e4SLinus Torvalds 	}
17031da177e4SLinus Torvalds 
1704f6f8ed47SWANG Chao 	if (map_vm_area(area, prot, pages))
17051da177e4SLinus Torvalds 		goto fail;
17061da177e4SLinus Torvalds 	return area->addr;
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds fail:
1709a8e99259SMichal Hocko 	warn_alloc(gfp_mask, NULL,
17107877cdccSMichal Hocko 			  "vmalloc: allocation failure, allocated %ld of %ld bytes",
171122943ab1SDave Hansen 			  (area->nr_pages*PAGE_SIZE), area->size);
17121da177e4SLinus Torvalds 	vfree(area->addr);
17131da177e4SLinus Torvalds 	return NULL;
17141da177e4SLinus Torvalds }
17151da177e4SLinus Torvalds 
1716d0a21265SDavid Rientjes /**
1717d0a21265SDavid Rientjes  *	__vmalloc_node_range  -  allocate virtually contiguous memory
1718d0a21265SDavid Rientjes  *	@size:		allocation size
1719d0a21265SDavid Rientjes  *	@align:		desired alignment
1720d0a21265SDavid Rientjes  *	@start:		vm area range start
1721d0a21265SDavid Rientjes  *	@end:		vm area range end
1722d0a21265SDavid Rientjes  *	@gfp_mask:	flags for the page level allocator
1723d0a21265SDavid Rientjes  *	@prot:		protection mask for the allocated pages
1724cb9e3c29SAndrey Ryabinin  *	@vm_flags:	additional vm area flags (e.g. %VM_NO_GUARD)
172500ef2d2fSDavid Rientjes  *	@node:		node to use for allocation or NUMA_NO_NODE
1726d0a21265SDavid Rientjes  *	@caller:	caller's return address
1727d0a21265SDavid Rientjes  *
1728d0a21265SDavid Rientjes  *	Allocate enough pages to cover @size from the page level
1729d0a21265SDavid Rientjes  *	allocator with @gfp_mask flags.  Map them into contiguous
1730d0a21265SDavid Rientjes  *	kernel virtual space, using a pagetable protection of @prot.
1731d0a21265SDavid Rientjes  */
1732d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align,
1733d0a21265SDavid Rientjes 			unsigned long start, unsigned long end, gfp_t gfp_mask,
1734cb9e3c29SAndrey Ryabinin 			pgprot_t prot, unsigned long vm_flags, int node,
1735cb9e3c29SAndrey Ryabinin 			const void *caller)
1736930fc45aSChristoph Lameter {
1737d0a21265SDavid Rientjes 	struct vm_struct *area;
1738d0a21265SDavid Rientjes 	void *addr;
1739d0a21265SDavid Rientjes 	unsigned long real_size = size;
1740d0a21265SDavid Rientjes 
1741d0a21265SDavid Rientjes 	size = PAGE_ALIGN(size);
1742d0a21265SDavid Rientjes 	if (!size || (size >> PAGE_SHIFT) > totalram_pages)
1743de7d2b56SJoe Perches 		goto fail;
1744d0a21265SDavid Rientjes 
1745cb9e3c29SAndrey Ryabinin 	area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED |
1746cb9e3c29SAndrey Ryabinin 				vm_flags, start, end, node, gfp_mask, caller);
1747d0a21265SDavid Rientjes 	if (!area)
1748de7d2b56SJoe Perches 		goto fail;
1749d0a21265SDavid Rientjes 
17503722e13cSWanpeng Li 	addr = __vmalloc_area_node(area, gfp_mask, prot, node);
17511368edf0SMel Gorman 	if (!addr)
1752b82225f3SWanpeng Li 		return NULL;
175389219d37SCatalin Marinas 
175489219d37SCatalin Marinas 	/*
175520fc02b4SZhang Yanfei 	 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
175620fc02b4SZhang Yanfei 	 * flag. It means that vm_struct is not fully initialized.
17574341fa45SJoonsoo Kim 	 * Now, it is fully initialized, so remove this flag here.
1758f5252e00SMitsuo Hayasaka 	 */
175920fc02b4SZhang Yanfei 	clear_vm_uninitialized_flag(area);
1760f5252e00SMitsuo Hayasaka 
1761f5252e00SMitsuo Hayasaka 	/*
17627f88f88fSCatalin Marinas 	 * A ref_count = 2 is needed because vm_struct allocated in
17637f88f88fSCatalin Marinas 	 * __get_vm_area_node() contains a reference to the virtual address of
17647f88f88fSCatalin Marinas 	 * the vmalloc'ed block.
176589219d37SCatalin Marinas 	 */
17667f88f88fSCatalin Marinas 	kmemleak_alloc(addr, real_size, 2, gfp_mask);
176789219d37SCatalin Marinas 
176889219d37SCatalin Marinas 	return addr;
1769de7d2b56SJoe Perches 
1770de7d2b56SJoe Perches fail:
1771a8e99259SMichal Hocko 	warn_alloc(gfp_mask, NULL,
17727877cdccSMichal Hocko 			  "vmalloc: allocation failure: %lu bytes", real_size);
1773de7d2b56SJoe Perches 	return NULL;
1774930fc45aSChristoph Lameter }
1775930fc45aSChristoph Lameter 
17761da177e4SLinus Torvalds /**
1777930fc45aSChristoph Lameter  *	__vmalloc_node  -  allocate virtually contiguous memory
17781da177e4SLinus Torvalds  *	@size:		allocation size
17792dca6999SDavid Miller  *	@align:		desired alignment
17801da177e4SLinus Torvalds  *	@gfp_mask:	flags for the page level allocator
17811da177e4SLinus Torvalds  *	@prot:		protection mask for the allocated pages
178200ef2d2fSDavid Rientjes  *	@node:		node to use for allocation or NUMA_NO_NODE
1783c85d194bSRandy Dunlap  *	@caller:	caller's return address
17841da177e4SLinus Torvalds  *
17851da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
17861da177e4SLinus Torvalds  *	allocator with @gfp_mask flags.  Map them into contiguous
17871da177e4SLinus Torvalds  *	kernel virtual space, using a pagetable protection of @prot.
17881da177e4SLinus Torvalds  */
17892dca6999SDavid Miller static void *__vmalloc_node(unsigned long size, unsigned long align,
17902dca6999SDavid Miller 			    gfp_t gfp_mask, pgprot_t prot,
17915e6cafc8SMarek Szyprowski 			    int node, const void *caller)
17921da177e4SLinus Torvalds {
1793d0a21265SDavid Rientjes 	return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
1794cb9e3c29SAndrey Ryabinin 				gfp_mask, prot, 0, node, caller);
17951da177e4SLinus Torvalds }
17961da177e4SLinus Torvalds 
1797930fc45aSChristoph Lameter void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1798930fc45aSChristoph Lameter {
179900ef2d2fSDavid Rientjes 	return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE,
180023016969SChristoph Lameter 				__builtin_return_address(0));
1801930fc45aSChristoph Lameter }
18021da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc);
18031da177e4SLinus Torvalds 
1804e1ca7788SDave Young static inline void *__vmalloc_node_flags(unsigned long size,
1805e1ca7788SDave Young 					int node, gfp_t flags)
1806e1ca7788SDave Young {
1807e1ca7788SDave Young 	return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
1808e1ca7788SDave Young 					node, __builtin_return_address(0));
1809e1ca7788SDave Young }
1810e1ca7788SDave Young 
18111da177e4SLinus Torvalds /**
18121da177e4SLinus Torvalds  *	vmalloc  -  allocate virtually contiguous memory
18131da177e4SLinus Torvalds  *	@size:		allocation size
18141da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
18151da177e4SLinus Torvalds  *	allocator and map them into contiguous kernel virtual space.
18161da177e4SLinus Torvalds  *
1817c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
18181da177e4SLinus Torvalds  *	use __vmalloc() instead.
18191da177e4SLinus Torvalds  */
18201da177e4SLinus Torvalds void *vmalloc(unsigned long size)
18211da177e4SLinus Torvalds {
182200ef2d2fSDavid Rientjes 	return __vmalloc_node_flags(size, NUMA_NO_NODE,
182300ef2d2fSDavid Rientjes 				    GFP_KERNEL | __GFP_HIGHMEM);
18241da177e4SLinus Torvalds }
18251da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc);
18261da177e4SLinus Torvalds 
1827930fc45aSChristoph Lameter /**
1828e1ca7788SDave Young  *	vzalloc - allocate virtually contiguous memory with zero fill
1829e1ca7788SDave Young  *	@size:	allocation size
1830e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
1831e1ca7788SDave Young  *	allocator and map them into contiguous kernel virtual space.
1832e1ca7788SDave Young  *	The memory allocated is set to zero.
1833e1ca7788SDave Young  *
1834e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
1835e1ca7788SDave Young  *	use __vmalloc() instead.
1836e1ca7788SDave Young  */
1837e1ca7788SDave Young void *vzalloc(unsigned long size)
1838e1ca7788SDave Young {
183900ef2d2fSDavid Rientjes 	return __vmalloc_node_flags(size, NUMA_NO_NODE,
1840e1ca7788SDave Young 				GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1841e1ca7788SDave Young }
1842e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
1843e1ca7788SDave Young 
1844e1ca7788SDave Young /**
1845ead04089SRolf Eike Beer  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
184683342314SNick Piggin  * @size: allocation size
1847ead04089SRolf Eike Beer  *
1848ead04089SRolf Eike Beer  * The resulting memory area is zeroed so it can be mapped to userspace
1849ead04089SRolf Eike Beer  * without leaking data.
185083342314SNick Piggin  */
185183342314SNick Piggin void *vmalloc_user(unsigned long size)
185283342314SNick Piggin {
185383342314SNick Piggin 	struct vm_struct *area;
185483342314SNick Piggin 	void *ret;
185583342314SNick Piggin 
18562dca6999SDavid Miller 	ret = __vmalloc_node(size, SHMLBA,
18572dca6999SDavid Miller 			     GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
185800ef2d2fSDavid Rientjes 			     PAGE_KERNEL, NUMA_NO_NODE,
185900ef2d2fSDavid Rientjes 			     __builtin_return_address(0));
18602b4ac44eSEric Dumazet 	if (ret) {
1861db64fe02SNick Piggin 		area = find_vm_area(ret);
186283342314SNick Piggin 		area->flags |= VM_USERMAP;
18632b4ac44eSEric Dumazet 	}
186483342314SNick Piggin 	return ret;
186583342314SNick Piggin }
186683342314SNick Piggin EXPORT_SYMBOL(vmalloc_user);
186783342314SNick Piggin 
186883342314SNick Piggin /**
1869930fc45aSChristoph Lameter  *	vmalloc_node  -  allocate memory on a specific node
1870930fc45aSChristoph Lameter  *	@size:		allocation size
1871d44e0780SRandy Dunlap  *	@node:		numa node
1872930fc45aSChristoph Lameter  *
1873930fc45aSChristoph Lameter  *	Allocate enough pages to cover @size from the page level
1874930fc45aSChristoph Lameter  *	allocator and map them into contiguous kernel virtual space.
1875930fc45aSChristoph Lameter  *
1876c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
1877930fc45aSChristoph Lameter  *	use __vmalloc() instead.
1878930fc45aSChristoph Lameter  */
1879930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node)
1880930fc45aSChristoph Lameter {
18812dca6999SDavid Miller 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
188223016969SChristoph Lameter 					node, __builtin_return_address(0));
1883930fc45aSChristoph Lameter }
1884930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node);
1885930fc45aSChristoph Lameter 
1886e1ca7788SDave Young /**
1887e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
1888e1ca7788SDave Young  * @size:	allocation size
1889e1ca7788SDave Young  * @node:	numa node
1890e1ca7788SDave Young  *
1891e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
1892e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
1893e1ca7788SDave Young  * The memory allocated is set to zero.
1894e1ca7788SDave Young  *
1895e1ca7788SDave Young  * For tight control over page level allocator and protection flags
1896e1ca7788SDave Young  * use __vmalloc_node() instead.
1897e1ca7788SDave Young  */
1898e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
1899e1ca7788SDave Young {
1900e1ca7788SDave Young 	return __vmalloc_node_flags(size, node,
1901e1ca7788SDave Young 			 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1902e1ca7788SDave Young }
1903e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
1904e1ca7788SDave Young 
19054dc3b16bSPavel Pisa #ifndef PAGE_KERNEL_EXEC
19064dc3b16bSPavel Pisa # define PAGE_KERNEL_EXEC PAGE_KERNEL
19074dc3b16bSPavel Pisa #endif
19084dc3b16bSPavel Pisa 
19091da177e4SLinus Torvalds /**
19101da177e4SLinus Torvalds  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
19111da177e4SLinus Torvalds  *	@size:		allocation size
19121da177e4SLinus Torvalds  *
19131da177e4SLinus Torvalds  *	Kernel-internal function to allocate enough pages to cover @size
19141da177e4SLinus Torvalds  *	the page level allocator and map them into contiguous and
19151da177e4SLinus Torvalds  *	executable kernel virtual space.
19161da177e4SLinus Torvalds  *
1917c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
19181da177e4SLinus Torvalds  *	use __vmalloc() instead.
19191da177e4SLinus Torvalds  */
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds void *vmalloc_exec(unsigned long size)
19221da177e4SLinus Torvalds {
19232dca6999SDavid Miller 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
192400ef2d2fSDavid Rientjes 			      NUMA_NO_NODE, __builtin_return_address(0));
19251da177e4SLinus Torvalds }
19261da177e4SLinus Torvalds 
19270d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
19287ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
19290d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
19307ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
19310d08e0d3SAndi Kleen #else
19320d08e0d3SAndi Kleen #define GFP_VMALLOC32 GFP_KERNEL
19330d08e0d3SAndi Kleen #endif
19340d08e0d3SAndi Kleen 
19351da177e4SLinus Torvalds /**
19361da177e4SLinus Torvalds  *	vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
19371da177e4SLinus Torvalds  *	@size:		allocation size
19381da177e4SLinus Torvalds  *
19391da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
19401da177e4SLinus Torvalds  *	page level allocator and map them into contiguous kernel virtual space.
19411da177e4SLinus Torvalds  */
19421da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
19431da177e4SLinus Torvalds {
19442dca6999SDavid Miller 	return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
194500ef2d2fSDavid Rientjes 			      NUMA_NO_NODE, __builtin_return_address(0));
19461da177e4SLinus Torvalds }
19471da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32);
19481da177e4SLinus Torvalds 
194983342314SNick Piggin /**
1950ead04089SRolf Eike Beer  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
195183342314SNick Piggin  *	@size:		allocation size
1952ead04089SRolf Eike Beer  *
1953ead04089SRolf Eike Beer  * The resulting memory area is 32bit addressable and zeroed so it can be
1954ead04089SRolf Eike Beer  * mapped to userspace without leaking data.
195583342314SNick Piggin  */
195683342314SNick Piggin void *vmalloc_32_user(unsigned long size)
195783342314SNick Piggin {
195883342314SNick Piggin 	struct vm_struct *area;
195983342314SNick Piggin 	void *ret;
196083342314SNick Piggin 
19612dca6999SDavid Miller 	ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
196200ef2d2fSDavid Rientjes 			     NUMA_NO_NODE, __builtin_return_address(0));
19632b4ac44eSEric Dumazet 	if (ret) {
1964db64fe02SNick Piggin 		area = find_vm_area(ret);
196583342314SNick Piggin 		area->flags |= VM_USERMAP;
19662b4ac44eSEric Dumazet 	}
196783342314SNick Piggin 	return ret;
196883342314SNick Piggin }
196983342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user);
197083342314SNick Piggin 
1971d0107eb0SKAMEZAWA Hiroyuki /*
1972d0107eb0SKAMEZAWA Hiroyuki  * small helper routine , copy contents to buf from addr.
1973d0107eb0SKAMEZAWA Hiroyuki  * If the page is not present, fill zero.
1974d0107eb0SKAMEZAWA Hiroyuki  */
1975d0107eb0SKAMEZAWA Hiroyuki 
1976d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count)
1977d0107eb0SKAMEZAWA Hiroyuki {
1978d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
1979d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
1980d0107eb0SKAMEZAWA Hiroyuki 
1981d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
1982d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
1983d0107eb0SKAMEZAWA Hiroyuki 
1984891c49abSAlexander Kuleshov 		offset = offset_in_page(addr);
1985d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
1986d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
1987d0107eb0SKAMEZAWA Hiroyuki 			length = count;
1988d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
1989d0107eb0SKAMEZAWA Hiroyuki 		/*
1990d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
1991d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
1992d0107eb0SKAMEZAWA Hiroyuki 		 * overhead of vmalloc()/vfree() calles for this _debug_
1993d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
1994d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
1995d0107eb0SKAMEZAWA Hiroyuki 		 */
1996d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
1997d0107eb0SKAMEZAWA Hiroyuki 			/*
1998d0107eb0SKAMEZAWA Hiroyuki 			 * we can expect USER0 is not used (see vread/vwrite's
1999d0107eb0SKAMEZAWA Hiroyuki 			 * function description)
2000d0107eb0SKAMEZAWA Hiroyuki 			 */
20019b04c5feSCong Wang 			void *map = kmap_atomic(p);
2002d0107eb0SKAMEZAWA Hiroyuki 			memcpy(buf, map + offset, length);
20039b04c5feSCong Wang 			kunmap_atomic(map);
2004d0107eb0SKAMEZAWA Hiroyuki 		} else
2005d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, length);
2006d0107eb0SKAMEZAWA Hiroyuki 
2007d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
2008d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
2009d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
2010d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
2011d0107eb0SKAMEZAWA Hiroyuki 	}
2012d0107eb0SKAMEZAWA Hiroyuki 	return copied;
2013d0107eb0SKAMEZAWA Hiroyuki }
2014d0107eb0SKAMEZAWA Hiroyuki 
2015d0107eb0SKAMEZAWA Hiroyuki static int aligned_vwrite(char *buf, char *addr, unsigned long count)
2016d0107eb0SKAMEZAWA Hiroyuki {
2017d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
2018d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
2019d0107eb0SKAMEZAWA Hiroyuki 
2020d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
2021d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
2022d0107eb0SKAMEZAWA Hiroyuki 
2023891c49abSAlexander Kuleshov 		offset = offset_in_page(addr);
2024d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
2025d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
2026d0107eb0SKAMEZAWA Hiroyuki 			length = count;
2027d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
2028d0107eb0SKAMEZAWA Hiroyuki 		/*
2029d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
2030d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
2031d0107eb0SKAMEZAWA Hiroyuki 		 * overhead of vmalloc()/vfree() calles for this _debug_
2032d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
2033d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
2034d0107eb0SKAMEZAWA Hiroyuki 		 */
2035d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
2036d0107eb0SKAMEZAWA Hiroyuki 			/*
2037d0107eb0SKAMEZAWA Hiroyuki 			 * we can expect USER0 is not used (see vread/vwrite's
2038d0107eb0SKAMEZAWA Hiroyuki 			 * function description)
2039d0107eb0SKAMEZAWA Hiroyuki 			 */
20409b04c5feSCong Wang 			void *map = kmap_atomic(p);
2041d0107eb0SKAMEZAWA Hiroyuki 			memcpy(map + offset, buf, length);
20429b04c5feSCong Wang 			kunmap_atomic(map);
2043d0107eb0SKAMEZAWA Hiroyuki 		}
2044d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
2045d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
2046d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
2047d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
2048d0107eb0SKAMEZAWA Hiroyuki 	}
2049d0107eb0SKAMEZAWA Hiroyuki 	return copied;
2050d0107eb0SKAMEZAWA Hiroyuki }
2051d0107eb0SKAMEZAWA Hiroyuki 
2052d0107eb0SKAMEZAWA Hiroyuki /**
2053d0107eb0SKAMEZAWA Hiroyuki  *	vread() -  read vmalloc area in a safe way.
2054d0107eb0SKAMEZAWA Hiroyuki  *	@buf:		buffer for reading data
2055d0107eb0SKAMEZAWA Hiroyuki  *	@addr:		vm address.
2056d0107eb0SKAMEZAWA Hiroyuki  *	@count:		number of bytes to be read.
2057d0107eb0SKAMEZAWA Hiroyuki  *
2058d0107eb0SKAMEZAWA Hiroyuki  *	Returns # of bytes which addr and buf should be increased.
2059d0107eb0SKAMEZAWA Hiroyuki  *	(same number to @count). Returns 0 if [addr...addr+count) doesn't
2060d0107eb0SKAMEZAWA Hiroyuki  *	includes any intersect with alive vmalloc area.
2061d0107eb0SKAMEZAWA Hiroyuki  *
2062d0107eb0SKAMEZAWA Hiroyuki  *	This function checks that addr is a valid vmalloc'ed area, and
2063d0107eb0SKAMEZAWA Hiroyuki  *	copy data from that area to a given buffer. If the given memory range
2064d0107eb0SKAMEZAWA Hiroyuki  *	of [addr...addr+count) includes some valid address, data is copied to
2065d0107eb0SKAMEZAWA Hiroyuki  *	proper area of @buf. If there are memory holes, they'll be zero-filled.
2066d0107eb0SKAMEZAWA Hiroyuki  *	IOREMAP area is treated as memory hole and no copy is done.
2067d0107eb0SKAMEZAWA Hiroyuki  *
2068d0107eb0SKAMEZAWA Hiroyuki  *	If [addr...addr+count) doesn't includes any intersects with alive
2069a8e5202dSCong Wang  *	vm_struct area, returns 0. @buf should be kernel's buffer.
2070d0107eb0SKAMEZAWA Hiroyuki  *
2071d0107eb0SKAMEZAWA Hiroyuki  *	Note: In usual ops, vread() is never necessary because the caller
2072d0107eb0SKAMEZAWA Hiroyuki  *	should know vmalloc() area is valid and can use memcpy().
2073d0107eb0SKAMEZAWA Hiroyuki  *	This is for routines which have to access vmalloc area without
2074d0107eb0SKAMEZAWA Hiroyuki  *	any informaion, as /dev/kmem.
2075d0107eb0SKAMEZAWA Hiroyuki  *
2076d0107eb0SKAMEZAWA Hiroyuki  */
2077d0107eb0SKAMEZAWA Hiroyuki 
20781da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
20791da177e4SLinus Torvalds {
2080e81ce85fSJoonsoo Kim 	struct vmap_area *va;
2081e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
20821da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
2083d0107eb0SKAMEZAWA Hiroyuki 	unsigned long buflen = count;
20841da177e4SLinus Torvalds 	unsigned long n;
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds 	/* Don't allow overflow */
20871da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
20881da177e4SLinus Torvalds 		count = -(unsigned long) addr;
20891da177e4SLinus Torvalds 
2090e81ce85fSJoonsoo Kim 	spin_lock(&vmap_area_lock);
2091e81ce85fSJoonsoo Kim 	list_for_each_entry(va, &vmap_area_list, list) {
2092e81ce85fSJoonsoo Kim 		if (!count)
2093e81ce85fSJoonsoo Kim 			break;
2094e81ce85fSJoonsoo Kim 
2095e81ce85fSJoonsoo Kim 		if (!(va->flags & VM_VM_AREA))
2096e81ce85fSJoonsoo Kim 			continue;
2097e81ce85fSJoonsoo Kim 
2098e81ce85fSJoonsoo Kim 		vm = va->vm;
2099e81ce85fSJoonsoo Kim 		vaddr = (char *) vm->addr;
2100762216abSWanpeng Li 		if (addr >= vaddr + get_vm_area_size(vm))
21011da177e4SLinus Torvalds 			continue;
21021da177e4SLinus Torvalds 		while (addr < vaddr) {
21031da177e4SLinus Torvalds 			if (count == 0)
21041da177e4SLinus Torvalds 				goto finished;
21051da177e4SLinus Torvalds 			*buf = '\0';
21061da177e4SLinus Torvalds 			buf++;
21071da177e4SLinus Torvalds 			addr++;
21081da177e4SLinus Torvalds 			count--;
21091da177e4SLinus Torvalds 		}
2110762216abSWanpeng Li 		n = vaddr + get_vm_area_size(vm) - addr;
2111d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
2112d0107eb0SKAMEZAWA Hiroyuki 			n = count;
2113e81ce85fSJoonsoo Kim 		if (!(vm->flags & VM_IOREMAP))
2114d0107eb0SKAMEZAWA Hiroyuki 			aligned_vread(buf, addr, n);
2115d0107eb0SKAMEZAWA Hiroyuki 		else /* IOREMAP area is treated as memory hole */
2116d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, n);
2117d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
2118d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
2119d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
21201da177e4SLinus Torvalds 	}
21211da177e4SLinus Torvalds finished:
2122e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2123d0107eb0SKAMEZAWA Hiroyuki 
2124d0107eb0SKAMEZAWA Hiroyuki 	if (buf == buf_start)
2125d0107eb0SKAMEZAWA Hiroyuki 		return 0;
2126d0107eb0SKAMEZAWA Hiroyuki 	/* zero-fill memory holes */
2127d0107eb0SKAMEZAWA Hiroyuki 	if (buf != buf_start + buflen)
2128d0107eb0SKAMEZAWA Hiroyuki 		memset(buf, 0, buflen - (buf - buf_start));
2129d0107eb0SKAMEZAWA Hiroyuki 
2130d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
21311da177e4SLinus Torvalds }
21321da177e4SLinus Torvalds 
2133d0107eb0SKAMEZAWA Hiroyuki /**
2134d0107eb0SKAMEZAWA Hiroyuki  *	vwrite() -  write vmalloc area in a safe way.
2135d0107eb0SKAMEZAWA Hiroyuki  *	@buf:		buffer for source data
2136d0107eb0SKAMEZAWA Hiroyuki  *	@addr:		vm address.
2137d0107eb0SKAMEZAWA Hiroyuki  *	@count:		number of bytes to be read.
2138d0107eb0SKAMEZAWA Hiroyuki  *
2139d0107eb0SKAMEZAWA Hiroyuki  *	Returns # of bytes which addr and buf should be incresed.
2140d0107eb0SKAMEZAWA Hiroyuki  *	(same number to @count).
2141d0107eb0SKAMEZAWA Hiroyuki  *	If [addr...addr+count) doesn't includes any intersect with valid
2142d0107eb0SKAMEZAWA Hiroyuki  *	vmalloc area, returns 0.
2143d0107eb0SKAMEZAWA Hiroyuki  *
2144d0107eb0SKAMEZAWA Hiroyuki  *	This function checks that addr is a valid vmalloc'ed area, and
2145d0107eb0SKAMEZAWA Hiroyuki  *	copy data from a buffer to the given addr. If specified range of
2146d0107eb0SKAMEZAWA Hiroyuki  *	[addr...addr+count) includes some valid address, data is copied from
2147d0107eb0SKAMEZAWA Hiroyuki  *	proper area of @buf. If there are memory holes, no copy to hole.
2148d0107eb0SKAMEZAWA Hiroyuki  *	IOREMAP area is treated as memory hole and no copy is done.
2149d0107eb0SKAMEZAWA Hiroyuki  *
2150d0107eb0SKAMEZAWA Hiroyuki  *	If [addr...addr+count) doesn't includes any intersects with alive
2151a8e5202dSCong Wang  *	vm_struct area, returns 0. @buf should be kernel's buffer.
2152d0107eb0SKAMEZAWA Hiroyuki  *
2153d0107eb0SKAMEZAWA Hiroyuki  *	Note: In usual ops, vwrite() is never necessary because the caller
2154d0107eb0SKAMEZAWA Hiroyuki  *	should know vmalloc() area is valid and can use memcpy().
2155d0107eb0SKAMEZAWA Hiroyuki  *	This is for routines which have to access vmalloc area without
2156d0107eb0SKAMEZAWA Hiroyuki  *	any informaion, as /dev/kmem.
2157d0107eb0SKAMEZAWA Hiroyuki  */
2158d0107eb0SKAMEZAWA Hiroyuki 
21591da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
21601da177e4SLinus Torvalds {
2161e81ce85fSJoonsoo Kim 	struct vmap_area *va;
2162e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
2163d0107eb0SKAMEZAWA Hiroyuki 	char *vaddr;
2164d0107eb0SKAMEZAWA Hiroyuki 	unsigned long n, buflen;
2165d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 	/* Don't allow overflow */
21681da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
21691da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2170d0107eb0SKAMEZAWA Hiroyuki 	buflen = count;
21711da177e4SLinus Torvalds 
2172e81ce85fSJoonsoo Kim 	spin_lock(&vmap_area_lock);
2173e81ce85fSJoonsoo Kim 	list_for_each_entry(va, &vmap_area_list, list) {
2174e81ce85fSJoonsoo Kim 		if (!count)
2175e81ce85fSJoonsoo Kim 			break;
2176e81ce85fSJoonsoo Kim 
2177e81ce85fSJoonsoo Kim 		if (!(va->flags & VM_VM_AREA))
2178e81ce85fSJoonsoo Kim 			continue;
2179e81ce85fSJoonsoo Kim 
2180e81ce85fSJoonsoo Kim 		vm = va->vm;
2181e81ce85fSJoonsoo Kim 		vaddr = (char *) vm->addr;
2182762216abSWanpeng Li 		if (addr >= vaddr + get_vm_area_size(vm))
21831da177e4SLinus Torvalds 			continue;
21841da177e4SLinus Torvalds 		while (addr < vaddr) {
21851da177e4SLinus Torvalds 			if (count == 0)
21861da177e4SLinus Torvalds 				goto finished;
21871da177e4SLinus Torvalds 			buf++;
21881da177e4SLinus Torvalds 			addr++;
21891da177e4SLinus Torvalds 			count--;
21901da177e4SLinus Torvalds 		}
2191762216abSWanpeng Li 		n = vaddr + get_vm_area_size(vm) - addr;
2192d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
2193d0107eb0SKAMEZAWA Hiroyuki 			n = count;
2194e81ce85fSJoonsoo Kim 		if (!(vm->flags & VM_IOREMAP)) {
2195d0107eb0SKAMEZAWA Hiroyuki 			aligned_vwrite(buf, addr, n);
2196d0107eb0SKAMEZAWA Hiroyuki 			copied++;
2197d0107eb0SKAMEZAWA Hiroyuki 		}
2198d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
2199d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
2200d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
22011da177e4SLinus Torvalds 	}
22021da177e4SLinus Torvalds finished:
2203e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2204d0107eb0SKAMEZAWA Hiroyuki 	if (!copied)
2205d0107eb0SKAMEZAWA Hiroyuki 		return 0;
2206d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
22071da177e4SLinus Torvalds }
220883342314SNick Piggin 
220983342314SNick Piggin /**
2210e69e9d4aSHATAYAMA Daisuke  *	remap_vmalloc_range_partial  -  map vmalloc pages to userspace
2211e69e9d4aSHATAYAMA Daisuke  *	@vma:		vma to cover
2212e69e9d4aSHATAYAMA Daisuke  *	@uaddr:		target user address to start at
2213e69e9d4aSHATAYAMA Daisuke  *	@kaddr:		virtual address of vmalloc kernel memory
2214e69e9d4aSHATAYAMA Daisuke  *	@size:		size of map area
2215e69e9d4aSHATAYAMA Daisuke  *
2216e69e9d4aSHATAYAMA Daisuke  *	Returns:	0 for success, -Exxx on failure
2217e69e9d4aSHATAYAMA Daisuke  *
2218e69e9d4aSHATAYAMA Daisuke  *	This function checks that @kaddr is a valid vmalloc'ed area,
2219e69e9d4aSHATAYAMA Daisuke  *	and that it is big enough to cover the range starting at
2220e69e9d4aSHATAYAMA Daisuke  *	@uaddr in @vma. Will return failure if that criteria isn't
2221e69e9d4aSHATAYAMA Daisuke  *	met.
2222e69e9d4aSHATAYAMA Daisuke  *
2223e69e9d4aSHATAYAMA Daisuke  *	Similar to remap_pfn_range() (see mm/memory.c)
2224e69e9d4aSHATAYAMA Daisuke  */
2225e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
2226e69e9d4aSHATAYAMA Daisuke 				void *kaddr, unsigned long size)
2227e69e9d4aSHATAYAMA Daisuke {
2228e69e9d4aSHATAYAMA Daisuke 	struct vm_struct *area;
2229e69e9d4aSHATAYAMA Daisuke 
2230e69e9d4aSHATAYAMA Daisuke 	size = PAGE_ALIGN(size);
2231e69e9d4aSHATAYAMA Daisuke 
2232e69e9d4aSHATAYAMA Daisuke 	if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
2233e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
2234e69e9d4aSHATAYAMA Daisuke 
2235e69e9d4aSHATAYAMA Daisuke 	area = find_vm_area(kaddr);
2236e69e9d4aSHATAYAMA Daisuke 	if (!area)
2237e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
2238e69e9d4aSHATAYAMA Daisuke 
2239e69e9d4aSHATAYAMA Daisuke 	if (!(area->flags & VM_USERMAP))
2240e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
2241e69e9d4aSHATAYAMA Daisuke 
2242e69e9d4aSHATAYAMA Daisuke 	if (kaddr + size > area->addr + area->size)
2243e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
2244e69e9d4aSHATAYAMA Daisuke 
2245e69e9d4aSHATAYAMA Daisuke 	do {
2246e69e9d4aSHATAYAMA Daisuke 		struct page *page = vmalloc_to_page(kaddr);
2247e69e9d4aSHATAYAMA Daisuke 		int ret;
2248e69e9d4aSHATAYAMA Daisuke 
2249e69e9d4aSHATAYAMA Daisuke 		ret = vm_insert_page(vma, uaddr, page);
2250e69e9d4aSHATAYAMA Daisuke 		if (ret)
2251e69e9d4aSHATAYAMA Daisuke 			return ret;
2252e69e9d4aSHATAYAMA Daisuke 
2253e69e9d4aSHATAYAMA Daisuke 		uaddr += PAGE_SIZE;
2254e69e9d4aSHATAYAMA Daisuke 		kaddr += PAGE_SIZE;
2255e69e9d4aSHATAYAMA Daisuke 		size -= PAGE_SIZE;
2256e69e9d4aSHATAYAMA Daisuke 	} while (size > 0);
2257e69e9d4aSHATAYAMA Daisuke 
2258e69e9d4aSHATAYAMA Daisuke 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
2259e69e9d4aSHATAYAMA Daisuke 
2260e69e9d4aSHATAYAMA Daisuke 	return 0;
2261e69e9d4aSHATAYAMA Daisuke }
2262e69e9d4aSHATAYAMA Daisuke EXPORT_SYMBOL(remap_vmalloc_range_partial);
2263e69e9d4aSHATAYAMA Daisuke 
2264e69e9d4aSHATAYAMA Daisuke /**
226583342314SNick Piggin  *	remap_vmalloc_range  -  map vmalloc pages to userspace
226683342314SNick Piggin  *	@vma:		vma to cover (map full range of vma)
226783342314SNick Piggin  *	@addr:		vmalloc memory
226883342314SNick Piggin  *	@pgoff:		number of pages into addr before first page to map
22697682486bSRandy Dunlap  *
22707682486bSRandy Dunlap  *	Returns:	0 for success, -Exxx on failure
227183342314SNick Piggin  *
227283342314SNick Piggin  *	This function checks that addr is a valid vmalloc'ed area, and
227383342314SNick Piggin  *	that it is big enough to cover the vma. Will return failure if
227483342314SNick Piggin  *	that criteria isn't met.
227583342314SNick Piggin  *
227672fd4a35SRobert P. J. Day  *	Similar to remap_pfn_range() (see mm/memory.c)
227783342314SNick Piggin  */
227883342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
227983342314SNick Piggin 						unsigned long pgoff)
228083342314SNick Piggin {
2281e69e9d4aSHATAYAMA Daisuke 	return remap_vmalloc_range_partial(vma, vma->vm_start,
2282e69e9d4aSHATAYAMA Daisuke 					   addr + (pgoff << PAGE_SHIFT),
2283e69e9d4aSHATAYAMA Daisuke 					   vma->vm_end - vma->vm_start);
228483342314SNick Piggin }
228583342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range);
228683342314SNick Piggin 
22871eeb66a1SChristoph Hellwig /*
22881eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
22891eeb66a1SChristoph Hellwig  * have one.
22901eeb66a1SChristoph Hellwig  */
22913b32123dSGideon Israel Dsouza void __weak vmalloc_sync_all(void)
22921eeb66a1SChristoph Hellwig {
22931eeb66a1SChristoph Hellwig }
22945f4352fbSJeremy Fitzhardinge 
22955f4352fbSJeremy Fitzhardinge 
22962f569afdSMartin Schwidefsky static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
22975f4352fbSJeremy Fitzhardinge {
2298cd12909cSDavid Vrabel 	pte_t ***p = data;
2299cd12909cSDavid Vrabel 
2300cd12909cSDavid Vrabel 	if (p) {
2301cd12909cSDavid Vrabel 		*(*p) = pte;
2302cd12909cSDavid Vrabel 		(*p)++;
2303cd12909cSDavid Vrabel 	}
23045f4352fbSJeremy Fitzhardinge 	return 0;
23055f4352fbSJeremy Fitzhardinge }
23065f4352fbSJeremy Fitzhardinge 
23075f4352fbSJeremy Fitzhardinge /**
23085f4352fbSJeremy Fitzhardinge  *	alloc_vm_area - allocate a range of kernel address space
23095f4352fbSJeremy Fitzhardinge  *	@size:		size of the area
2310cd12909cSDavid Vrabel  *	@ptes:		returns the PTEs for the address space
23117682486bSRandy Dunlap  *
23127682486bSRandy Dunlap  *	Returns:	NULL on failure, vm_struct on success
23135f4352fbSJeremy Fitzhardinge  *
23145f4352fbSJeremy Fitzhardinge  *	This function reserves a range of kernel address space, and
23155f4352fbSJeremy Fitzhardinge  *	allocates pagetables to map that range.  No actual mappings
2316cd12909cSDavid Vrabel  *	are created.
2317cd12909cSDavid Vrabel  *
2318cd12909cSDavid Vrabel  *	If @ptes is non-NULL, pointers to the PTEs (in init_mm)
2319cd12909cSDavid Vrabel  *	allocated for the VM area are returned.
23205f4352fbSJeremy Fitzhardinge  */
2321cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
23225f4352fbSJeremy Fitzhardinge {
23235f4352fbSJeremy Fitzhardinge 	struct vm_struct *area;
23245f4352fbSJeremy Fitzhardinge 
232523016969SChristoph Lameter 	area = get_vm_area_caller(size, VM_IOREMAP,
232623016969SChristoph Lameter 				__builtin_return_address(0));
23275f4352fbSJeremy Fitzhardinge 	if (area == NULL)
23285f4352fbSJeremy Fitzhardinge 		return NULL;
23295f4352fbSJeremy Fitzhardinge 
23305f4352fbSJeremy Fitzhardinge 	/*
23315f4352fbSJeremy Fitzhardinge 	 * This ensures that page tables are constructed for this region
23325f4352fbSJeremy Fitzhardinge 	 * of kernel virtual address space and mapped into init_mm.
23335f4352fbSJeremy Fitzhardinge 	 */
23345f4352fbSJeremy Fitzhardinge 	if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
2335cd12909cSDavid Vrabel 				size, f, ptes ? &ptes : NULL)) {
23365f4352fbSJeremy Fitzhardinge 		free_vm_area(area);
23375f4352fbSJeremy Fitzhardinge 		return NULL;
23385f4352fbSJeremy Fitzhardinge 	}
23395f4352fbSJeremy Fitzhardinge 
23405f4352fbSJeremy Fitzhardinge 	return area;
23415f4352fbSJeremy Fitzhardinge }
23425f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(alloc_vm_area);
23435f4352fbSJeremy Fitzhardinge 
23445f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area)
23455f4352fbSJeremy Fitzhardinge {
23465f4352fbSJeremy Fitzhardinge 	struct vm_struct *ret;
23475f4352fbSJeremy Fitzhardinge 	ret = remove_vm_area(area->addr);
23485f4352fbSJeremy Fitzhardinge 	BUG_ON(ret != area);
23495f4352fbSJeremy Fitzhardinge 	kfree(area);
23505f4352fbSJeremy Fitzhardinge }
23515f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area);
2352a10aa579SChristoph Lameter 
23534f8b02b4STejun Heo #ifdef CONFIG_SMP
2354ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n)
2355ca23e405STejun Heo {
23564583e773SGeliang Tang 	return rb_entry_safe(n, struct vmap_area, rb_node);
2357ca23e405STejun Heo }
2358ca23e405STejun Heo 
2359ca23e405STejun Heo /**
2360ca23e405STejun Heo  * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
2361ca23e405STejun Heo  * @end: target address
2362ca23e405STejun Heo  * @pnext: out arg for the next vmap_area
2363ca23e405STejun Heo  * @pprev: out arg for the previous vmap_area
2364ca23e405STejun Heo  *
2365ca23e405STejun Heo  * Returns: %true if either or both of next and prev are found,
2366ca23e405STejun Heo  *	    %false if no vmap_area exists
2367ca23e405STejun Heo  *
2368ca23e405STejun Heo  * Find vmap_areas end addresses of which enclose @end.  ie. if not
2369ca23e405STejun Heo  * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
2370ca23e405STejun Heo  */
2371ca23e405STejun Heo static bool pvm_find_next_prev(unsigned long end,
2372ca23e405STejun Heo 			       struct vmap_area **pnext,
2373ca23e405STejun Heo 			       struct vmap_area **pprev)
2374ca23e405STejun Heo {
2375ca23e405STejun Heo 	struct rb_node *n = vmap_area_root.rb_node;
2376ca23e405STejun Heo 	struct vmap_area *va = NULL;
2377ca23e405STejun Heo 
2378ca23e405STejun Heo 	while (n) {
2379ca23e405STejun Heo 		va = rb_entry(n, struct vmap_area, rb_node);
2380ca23e405STejun Heo 		if (end < va->va_end)
2381ca23e405STejun Heo 			n = n->rb_left;
2382ca23e405STejun Heo 		else if (end > va->va_end)
2383ca23e405STejun Heo 			n = n->rb_right;
2384ca23e405STejun Heo 		else
2385ca23e405STejun Heo 			break;
2386ca23e405STejun Heo 	}
2387ca23e405STejun Heo 
2388ca23e405STejun Heo 	if (!va)
2389ca23e405STejun Heo 		return false;
2390ca23e405STejun Heo 
2391ca23e405STejun Heo 	if (va->va_end > end) {
2392ca23e405STejun Heo 		*pnext = va;
2393ca23e405STejun Heo 		*pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2394ca23e405STejun Heo 	} else {
2395ca23e405STejun Heo 		*pprev = va;
2396ca23e405STejun Heo 		*pnext = node_to_va(rb_next(&(*pprev)->rb_node));
2397ca23e405STejun Heo 	}
2398ca23e405STejun Heo 	return true;
2399ca23e405STejun Heo }
2400ca23e405STejun Heo 
2401ca23e405STejun Heo /**
2402ca23e405STejun Heo  * pvm_determine_end - find the highest aligned address between two vmap_areas
2403ca23e405STejun Heo  * @pnext: in/out arg for the next vmap_area
2404ca23e405STejun Heo  * @pprev: in/out arg for the previous vmap_area
2405ca23e405STejun Heo  * @align: alignment
2406ca23e405STejun Heo  *
2407ca23e405STejun Heo  * Returns: determined end address
2408ca23e405STejun Heo  *
2409ca23e405STejun Heo  * Find the highest aligned address between *@pnext and *@pprev below
2410ca23e405STejun Heo  * VMALLOC_END.  *@pnext and *@pprev are adjusted so that the aligned
2411ca23e405STejun Heo  * down address is between the end addresses of the two vmap_areas.
2412ca23e405STejun Heo  *
2413ca23e405STejun Heo  * Please note that the address returned by this function may fall
2414ca23e405STejun Heo  * inside *@pnext vmap_area.  The caller is responsible for checking
2415ca23e405STejun Heo  * that.
2416ca23e405STejun Heo  */
2417ca23e405STejun Heo static unsigned long pvm_determine_end(struct vmap_area **pnext,
2418ca23e405STejun Heo 				       struct vmap_area **pprev,
2419ca23e405STejun Heo 				       unsigned long align)
2420ca23e405STejun Heo {
2421ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2422ca23e405STejun Heo 	unsigned long addr;
2423ca23e405STejun Heo 
2424ca23e405STejun Heo 	if (*pnext)
2425ca23e405STejun Heo 		addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
2426ca23e405STejun Heo 	else
2427ca23e405STejun Heo 		addr = vmalloc_end;
2428ca23e405STejun Heo 
2429ca23e405STejun Heo 	while (*pprev && (*pprev)->va_end > addr) {
2430ca23e405STejun Heo 		*pnext = *pprev;
2431ca23e405STejun Heo 		*pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2432ca23e405STejun Heo 	}
2433ca23e405STejun Heo 
2434ca23e405STejun Heo 	return addr;
2435ca23e405STejun Heo }
2436ca23e405STejun Heo 
2437ca23e405STejun Heo /**
2438ca23e405STejun Heo  * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
2439ca23e405STejun Heo  * @offsets: array containing offset of each area
2440ca23e405STejun Heo  * @sizes: array containing size of each area
2441ca23e405STejun Heo  * @nr_vms: the number of areas to allocate
2442ca23e405STejun Heo  * @align: alignment, all entries in @offsets and @sizes must be aligned to this
2443ca23e405STejun Heo  *
2444ca23e405STejun Heo  * Returns: kmalloc'd vm_struct pointer array pointing to allocated
2445ca23e405STejun Heo  *	    vm_structs on success, %NULL on failure
2446ca23e405STejun Heo  *
2447ca23e405STejun Heo  * Percpu allocator wants to use congruent vm areas so that it can
2448ca23e405STejun Heo  * maintain the offsets among percpu areas.  This function allocates
2449ec3f64fcSDavid Rientjes  * congruent vmalloc areas for it with GFP_KERNEL.  These areas tend to
2450ec3f64fcSDavid Rientjes  * be scattered pretty far, distance between two areas easily going up
2451ec3f64fcSDavid Rientjes  * to gigabytes.  To avoid interacting with regular vmallocs, these
2452ec3f64fcSDavid Rientjes  * areas are allocated from top.
2453ca23e405STejun Heo  *
2454ca23e405STejun Heo  * Despite its complicated look, this allocator is rather simple.  It
2455ca23e405STejun Heo  * does everything top-down and scans areas from the end looking for
2456ca23e405STejun Heo  * matching slot.  While scanning, if any of the areas overlaps with
2457ca23e405STejun Heo  * existing vmap_area, the base address is pulled down to fit the
2458ca23e405STejun Heo  * area.  Scanning is repeated till all the areas fit and then all
2459ca23e405STejun Heo  * necessary data structres are inserted and the result is returned.
2460ca23e405STejun Heo  */
2461ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
2462ca23e405STejun Heo 				     const size_t *sizes, int nr_vms,
2463ec3f64fcSDavid Rientjes 				     size_t align)
2464ca23e405STejun Heo {
2465ca23e405STejun Heo 	const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
2466ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2467ca23e405STejun Heo 	struct vmap_area **vas, *prev, *next;
2468ca23e405STejun Heo 	struct vm_struct **vms;
2469ca23e405STejun Heo 	int area, area2, last_area, term_area;
2470ca23e405STejun Heo 	unsigned long base, start, end, last_end;
2471ca23e405STejun Heo 	bool purged = false;
2472ca23e405STejun Heo 
2473ca23e405STejun Heo 	/* verify parameters and allocate data structures */
2474891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(align) || !is_power_of_2(align));
2475ca23e405STejun Heo 	for (last_area = 0, area = 0; area < nr_vms; area++) {
2476ca23e405STejun Heo 		start = offsets[area];
2477ca23e405STejun Heo 		end = start + sizes[area];
2478ca23e405STejun Heo 
2479ca23e405STejun Heo 		/* is everything aligned properly? */
2480ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(offsets[area], align));
2481ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(sizes[area], align));
2482ca23e405STejun Heo 
2483ca23e405STejun Heo 		/* detect the area with the highest address */
2484ca23e405STejun Heo 		if (start > offsets[last_area])
2485ca23e405STejun Heo 			last_area = area;
2486ca23e405STejun Heo 
2487ca23e405STejun Heo 		for (area2 = 0; area2 < nr_vms; area2++) {
2488ca23e405STejun Heo 			unsigned long start2 = offsets[area2];
2489ca23e405STejun Heo 			unsigned long end2 = start2 + sizes[area2];
2490ca23e405STejun Heo 
2491ca23e405STejun Heo 			if (area2 == area)
2492ca23e405STejun Heo 				continue;
2493ca23e405STejun Heo 
2494ca23e405STejun Heo 			BUG_ON(start2 >= start && start2 < end);
2495ca23e405STejun Heo 			BUG_ON(end2 <= end && end2 > start);
2496ca23e405STejun Heo 		}
2497ca23e405STejun Heo 	}
2498ca23e405STejun Heo 	last_end = offsets[last_area] + sizes[last_area];
2499ca23e405STejun Heo 
2500ca23e405STejun Heo 	if (vmalloc_end - vmalloc_start < last_end) {
2501ca23e405STejun Heo 		WARN_ON(true);
2502ca23e405STejun Heo 		return NULL;
2503ca23e405STejun Heo 	}
2504ca23e405STejun Heo 
25054d67d860SThomas Meyer 	vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
25064d67d860SThomas Meyer 	vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
2507ca23e405STejun Heo 	if (!vas || !vms)
2508f1db7afdSKautuk Consul 		goto err_free2;
2509ca23e405STejun Heo 
2510ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
2511ec3f64fcSDavid Rientjes 		vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL);
2512ec3f64fcSDavid Rientjes 		vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
2513ca23e405STejun Heo 		if (!vas[area] || !vms[area])
2514ca23e405STejun Heo 			goto err_free;
2515ca23e405STejun Heo 	}
2516ca23e405STejun Heo retry:
2517ca23e405STejun Heo 	spin_lock(&vmap_area_lock);
2518ca23e405STejun Heo 
2519ca23e405STejun Heo 	/* start scanning - we scan from the top, begin with the last area */
2520ca23e405STejun Heo 	area = term_area = last_area;
2521ca23e405STejun Heo 	start = offsets[area];
2522ca23e405STejun Heo 	end = start + sizes[area];
2523ca23e405STejun Heo 
2524ca23e405STejun Heo 	if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2525ca23e405STejun Heo 		base = vmalloc_end - last_end;
2526ca23e405STejun Heo 		goto found;
2527ca23e405STejun Heo 	}
2528ca23e405STejun Heo 	base = pvm_determine_end(&next, &prev, align) - end;
2529ca23e405STejun Heo 
2530ca23e405STejun Heo 	while (true) {
2531ca23e405STejun Heo 		BUG_ON(next && next->va_end <= base + end);
2532ca23e405STejun Heo 		BUG_ON(prev && prev->va_end > base + end);
2533ca23e405STejun Heo 
2534ca23e405STejun Heo 		/*
2535ca23e405STejun Heo 		 * base might have underflowed, add last_end before
2536ca23e405STejun Heo 		 * comparing.
2537ca23e405STejun Heo 		 */
2538ca23e405STejun Heo 		if (base + last_end < vmalloc_start + last_end) {
2539ca23e405STejun Heo 			spin_unlock(&vmap_area_lock);
2540ca23e405STejun Heo 			if (!purged) {
2541ca23e405STejun Heo 				purge_vmap_area_lazy();
2542ca23e405STejun Heo 				purged = true;
2543ca23e405STejun Heo 				goto retry;
2544ca23e405STejun Heo 			}
2545ca23e405STejun Heo 			goto err_free;
2546ca23e405STejun Heo 		}
2547ca23e405STejun Heo 
2548ca23e405STejun Heo 		/*
2549ca23e405STejun Heo 		 * If next overlaps, move base downwards so that it's
2550ca23e405STejun Heo 		 * right below next and then recheck.
2551ca23e405STejun Heo 		 */
2552ca23e405STejun Heo 		if (next && next->va_start < base + end) {
2553ca23e405STejun Heo 			base = pvm_determine_end(&next, &prev, align) - end;
2554ca23e405STejun Heo 			term_area = area;
2555ca23e405STejun Heo 			continue;
2556ca23e405STejun Heo 		}
2557ca23e405STejun Heo 
2558ca23e405STejun Heo 		/*
2559ca23e405STejun Heo 		 * If prev overlaps, shift down next and prev and move
2560ca23e405STejun Heo 		 * base so that it's right below new next and then
2561ca23e405STejun Heo 		 * recheck.
2562ca23e405STejun Heo 		 */
2563ca23e405STejun Heo 		if (prev && prev->va_end > base + start)  {
2564ca23e405STejun Heo 			next = prev;
2565ca23e405STejun Heo 			prev = node_to_va(rb_prev(&next->rb_node));
2566ca23e405STejun Heo 			base = pvm_determine_end(&next, &prev, align) - end;
2567ca23e405STejun Heo 			term_area = area;
2568ca23e405STejun Heo 			continue;
2569ca23e405STejun Heo 		}
2570ca23e405STejun Heo 
2571ca23e405STejun Heo 		/*
2572ca23e405STejun Heo 		 * This area fits, move on to the previous one.  If
2573ca23e405STejun Heo 		 * the previous one is the terminal one, we're done.
2574ca23e405STejun Heo 		 */
2575ca23e405STejun Heo 		area = (area + nr_vms - 1) % nr_vms;
2576ca23e405STejun Heo 		if (area == term_area)
2577ca23e405STejun Heo 			break;
2578ca23e405STejun Heo 		start = offsets[area];
2579ca23e405STejun Heo 		end = start + sizes[area];
2580ca23e405STejun Heo 		pvm_find_next_prev(base + end, &next, &prev);
2581ca23e405STejun Heo 	}
2582ca23e405STejun Heo found:
2583ca23e405STejun Heo 	/* we've found a fitting base, insert all va's */
2584ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
2585ca23e405STejun Heo 		struct vmap_area *va = vas[area];
2586ca23e405STejun Heo 
2587ca23e405STejun Heo 		va->va_start = base + offsets[area];
2588ca23e405STejun Heo 		va->va_end = va->va_start + sizes[area];
2589ca23e405STejun Heo 		__insert_vmap_area(va);
2590ca23e405STejun Heo 	}
2591ca23e405STejun Heo 
2592ca23e405STejun Heo 	vmap_area_pcpu_hole = base + offsets[last_area];
2593ca23e405STejun Heo 
2594ca23e405STejun Heo 	spin_unlock(&vmap_area_lock);
2595ca23e405STejun Heo 
2596ca23e405STejun Heo 	/* insert all vm's */
2597ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++)
25983645cb4aSZhang Yanfei 		setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2599ca23e405STejun Heo 				 pcpu_get_vm_areas);
2600ca23e405STejun Heo 
2601ca23e405STejun Heo 	kfree(vas);
2602ca23e405STejun Heo 	return vms;
2603ca23e405STejun Heo 
2604ca23e405STejun Heo err_free:
2605ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
2606ca23e405STejun Heo 		kfree(vas[area]);
2607ca23e405STejun Heo 		kfree(vms[area]);
2608ca23e405STejun Heo 	}
2609f1db7afdSKautuk Consul err_free2:
2610ca23e405STejun Heo 	kfree(vas);
2611ca23e405STejun Heo 	kfree(vms);
2612ca23e405STejun Heo 	return NULL;
2613ca23e405STejun Heo }
2614ca23e405STejun Heo 
2615ca23e405STejun Heo /**
2616ca23e405STejun Heo  * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2617ca23e405STejun Heo  * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2618ca23e405STejun Heo  * @nr_vms: the number of allocated areas
2619ca23e405STejun Heo  *
2620ca23e405STejun Heo  * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2621ca23e405STejun Heo  */
2622ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2623ca23e405STejun Heo {
2624ca23e405STejun Heo 	int i;
2625ca23e405STejun Heo 
2626ca23e405STejun Heo 	for (i = 0; i < nr_vms; i++)
2627ca23e405STejun Heo 		free_vm_area(vms[i]);
2628ca23e405STejun Heo 	kfree(vms);
2629ca23e405STejun Heo }
26304f8b02b4STejun Heo #endif	/* CONFIG_SMP */
2631a10aa579SChristoph Lameter 
2632a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS
2633a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos)
2634d4033afdSJoonsoo Kim 	__acquires(&vmap_area_lock)
2635a10aa579SChristoph Lameter {
2636d4033afdSJoonsoo Kim 	spin_lock(&vmap_area_lock);
26373f500069Szijun_hu 	return seq_list_start(&vmap_area_list, *pos);
2638a10aa579SChristoph Lameter }
2639a10aa579SChristoph Lameter 
2640a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2641a10aa579SChristoph Lameter {
26423f500069Szijun_hu 	return seq_list_next(p, &vmap_area_list, pos);
2643a10aa579SChristoph Lameter }
2644a10aa579SChristoph Lameter 
2645a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p)
2646d4033afdSJoonsoo Kim 	__releases(&vmap_area_lock)
2647a10aa579SChristoph Lameter {
2648d4033afdSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2649a10aa579SChristoph Lameter }
2650a10aa579SChristoph Lameter 
2651a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2652a47a126aSEric Dumazet {
2653e5adfffcSKirill A. Shutemov 	if (IS_ENABLED(CONFIG_NUMA)) {
2654a47a126aSEric Dumazet 		unsigned int nr, *counters = m->private;
2655a47a126aSEric Dumazet 
2656a47a126aSEric Dumazet 		if (!counters)
2657a47a126aSEric Dumazet 			return;
2658a47a126aSEric Dumazet 
2659af12346cSWanpeng Li 		if (v->flags & VM_UNINITIALIZED)
2660af12346cSWanpeng Li 			return;
26617e5b528bSDmitry Vyukov 		/* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
26627e5b528bSDmitry Vyukov 		smp_rmb();
2663af12346cSWanpeng Li 
2664a47a126aSEric Dumazet 		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2665a47a126aSEric Dumazet 
2666a47a126aSEric Dumazet 		for (nr = 0; nr < v->nr_pages; nr++)
2667a47a126aSEric Dumazet 			counters[page_to_nid(v->pages[nr])]++;
2668a47a126aSEric Dumazet 
2669a47a126aSEric Dumazet 		for_each_node_state(nr, N_HIGH_MEMORY)
2670a47a126aSEric Dumazet 			if (counters[nr])
2671a47a126aSEric Dumazet 				seq_printf(m, " N%u=%u", nr, counters[nr]);
2672a47a126aSEric Dumazet 	}
2673a47a126aSEric Dumazet }
2674a47a126aSEric Dumazet 
2675a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p)
2676a10aa579SChristoph Lameter {
26773f500069Szijun_hu 	struct vmap_area *va;
2678d4033afdSJoonsoo Kim 	struct vm_struct *v;
2679d4033afdSJoonsoo Kim 
26803f500069Szijun_hu 	va = list_entry(p, struct vmap_area, list);
26813f500069Szijun_hu 
2682c2ce8c14SWanpeng Li 	/*
2683c2ce8c14SWanpeng Li 	 * s_show can encounter race with remove_vm_area, !VM_VM_AREA on
2684c2ce8c14SWanpeng Li 	 * behalf of vmap area is being tear down or vm_map_ram allocation.
2685c2ce8c14SWanpeng Li 	 */
2686c2ce8c14SWanpeng Li 	if (!(va->flags & VM_VM_AREA))
2687d4033afdSJoonsoo Kim 		return 0;
2688d4033afdSJoonsoo Kim 
2689d4033afdSJoonsoo Kim 	v = va->vm;
2690a10aa579SChristoph Lameter 
269145ec1690SKees Cook 	seq_printf(m, "0x%pK-0x%pK %7ld",
2692a10aa579SChristoph Lameter 		v->addr, v->addr + v->size, v->size);
2693a10aa579SChristoph Lameter 
269462c70bceSJoe Perches 	if (v->caller)
269562c70bceSJoe Perches 		seq_printf(m, " %pS", v->caller);
269623016969SChristoph Lameter 
2697a10aa579SChristoph Lameter 	if (v->nr_pages)
2698a10aa579SChristoph Lameter 		seq_printf(m, " pages=%d", v->nr_pages);
2699a10aa579SChristoph Lameter 
2700a10aa579SChristoph Lameter 	if (v->phys_addr)
2701199eaa05SMiles Chen 		seq_printf(m, " phys=%pa", &v->phys_addr);
2702a10aa579SChristoph Lameter 
2703a10aa579SChristoph Lameter 	if (v->flags & VM_IOREMAP)
2704f4527c90SFabian Frederick 		seq_puts(m, " ioremap");
2705a10aa579SChristoph Lameter 
2706a10aa579SChristoph Lameter 	if (v->flags & VM_ALLOC)
2707f4527c90SFabian Frederick 		seq_puts(m, " vmalloc");
2708a10aa579SChristoph Lameter 
2709a10aa579SChristoph Lameter 	if (v->flags & VM_MAP)
2710f4527c90SFabian Frederick 		seq_puts(m, " vmap");
2711a10aa579SChristoph Lameter 
2712a10aa579SChristoph Lameter 	if (v->flags & VM_USERMAP)
2713f4527c90SFabian Frederick 		seq_puts(m, " user");
2714a10aa579SChristoph Lameter 
2715244d63eeSDavid Rientjes 	if (is_vmalloc_addr(v->pages))
2716f4527c90SFabian Frederick 		seq_puts(m, " vpages");
2717a10aa579SChristoph Lameter 
2718a47a126aSEric Dumazet 	show_numa_info(m, v);
2719a10aa579SChristoph Lameter 	seq_putc(m, '\n');
2720a10aa579SChristoph Lameter 	return 0;
2721a10aa579SChristoph Lameter }
2722a10aa579SChristoph Lameter 
27235f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = {
2724a10aa579SChristoph Lameter 	.start = s_start,
2725a10aa579SChristoph Lameter 	.next = s_next,
2726a10aa579SChristoph Lameter 	.stop = s_stop,
2727a10aa579SChristoph Lameter 	.show = s_show,
2728a10aa579SChristoph Lameter };
27295f6a6a9cSAlexey Dobriyan 
27305f6a6a9cSAlexey Dobriyan static int vmalloc_open(struct inode *inode, struct file *file)
27315f6a6a9cSAlexey Dobriyan {
2732703394c1SRob Jones 	if (IS_ENABLED(CONFIG_NUMA))
2733703394c1SRob Jones 		return seq_open_private(file, &vmalloc_op,
2734703394c1SRob Jones 					nr_node_ids * sizeof(unsigned int));
2735703394c1SRob Jones 	else
2736703394c1SRob Jones 		return seq_open(file, &vmalloc_op);
27375f6a6a9cSAlexey Dobriyan }
27385f6a6a9cSAlexey Dobriyan 
27395f6a6a9cSAlexey Dobriyan static const struct file_operations proc_vmalloc_operations = {
27405f6a6a9cSAlexey Dobriyan 	.open		= vmalloc_open,
27415f6a6a9cSAlexey Dobriyan 	.read		= seq_read,
27425f6a6a9cSAlexey Dobriyan 	.llseek		= seq_lseek,
27435f6a6a9cSAlexey Dobriyan 	.release	= seq_release_private,
27445f6a6a9cSAlexey Dobriyan };
27455f6a6a9cSAlexey Dobriyan 
27465f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void)
27475f6a6a9cSAlexey Dobriyan {
27485f6a6a9cSAlexey Dobriyan 	proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
27495f6a6a9cSAlexey Dobriyan 	return 0;
27505f6a6a9cSAlexey Dobriyan }
27515f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init);
2752db3808c1SJoonsoo Kim 
2753a10aa579SChristoph Lameter #endif
2754a10aa579SChristoph Lameter 
2755