xref: /openbmc/linux/mm/vmalloc.c (revision 78c72746f56b212ecf768a7e67cee3b7cf89238c)
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 
89c2febafcSKirill 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 
94c2febafcSKirill 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 
105c2febafcSKirill A. Shutemov static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end)
106c2febafcSKirill A. Shutemov {
107c2febafcSKirill A. Shutemov 	p4d_t *p4d;
108c2febafcSKirill A. Shutemov 	unsigned long next;
109c2febafcSKirill A. Shutemov 
110c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
111c2febafcSKirill A. Shutemov 	do {
112c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
113c2febafcSKirill A. Shutemov 		if (p4d_clear_huge(p4d))
114c2febafcSKirill A. Shutemov 			continue;
115c2febafcSKirill A. Shutemov 		if (p4d_none_or_clear_bad(p4d))
116c2febafcSKirill A. Shutemov 			continue;
117c2febafcSKirill A. Shutemov 		vunmap_pud_range(p4d, addr, next);
118c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
119c2febafcSKirill A. Shutemov }
120c2febafcSKirill 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;
132c2febafcSKirill 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 
179c2febafcSKirill 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 
185c2febafcSKirill 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 
196c2febafcSKirill A. Shutemov static int vmap_p4d_range(pgd_t *pgd, unsigned long addr,
197c2febafcSKirill A. Shutemov 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
198c2febafcSKirill A. Shutemov {
199c2febafcSKirill A. Shutemov 	p4d_t *p4d;
200c2febafcSKirill A. Shutemov 	unsigned long next;
201c2febafcSKirill A. Shutemov 
202c2febafcSKirill A. Shutemov 	p4d = p4d_alloc(&init_mm, pgd, addr);
203c2febafcSKirill A. Shutemov 	if (!p4d)
204c2febafcSKirill A. Shutemov 		return -ENOMEM;
205c2febafcSKirill A. Shutemov 	do {
206c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
207c2febafcSKirill A. Shutemov 		if (vmap_pud_range(p4d, addr, next, prot, pages, nr))
208c2febafcSKirill A. Shutemov 			return -ENOMEM;
209c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
210c2febafcSKirill A. Shutemov 	return 0;
211c2febafcSKirill A. Shutemov }
212c2febafcSKirill 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);
232c2febafcSKirill 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);
273c2febafcSKirill A. Shutemov 	p4d_t *p4d;
274c2febafcSKirill A. Shutemov 	pud_t *pud;
275c2febafcSKirill A. Shutemov 	pmd_t *pmd;
276c2febafcSKirill 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 
284c2febafcSKirill A. Shutemov 	if (pgd_none(*pgd))
285c2febafcSKirill A. Shutemov 		return NULL;
286c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
287c2febafcSKirill A. Shutemov 	if (p4d_none(*p4d))
288c2febafcSKirill A. Shutemov 		return NULL;
289c2febafcSKirill A. Shutemov 	pud = pud_offset(p4d, addr);
290029c54b0SArd Biesheuvel 
291029c54b0SArd Biesheuvel 	/*
292029c54b0SArd Biesheuvel 	 * Don't dereference bad PUD or PMD (below) entries. This will also
293029c54b0SArd Biesheuvel 	 * identify huge mappings, which we may encounter on architectures
294029c54b0SArd Biesheuvel 	 * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be
295029c54b0SArd Biesheuvel 	 * identified as vmalloc addresses by is_vmalloc_addr(), but are
296029c54b0SArd Biesheuvel 	 * not [unambiguously] associated with a struct page, so there is
297029c54b0SArd Biesheuvel 	 * no correct value to return for them.
298029c54b0SArd Biesheuvel 	 */
299029c54b0SArd Biesheuvel 	WARN_ON_ONCE(pud_bad(*pud));
300029c54b0SArd Biesheuvel 	if (pud_none(*pud) || pud_bad(*pud))
301c2febafcSKirill A. Shutemov 		return NULL;
302c2febafcSKirill A. Shutemov 	pmd = pmd_offset(pud, addr);
303029c54b0SArd Biesheuvel 	WARN_ON_ONCE(pmd_bad(*pmd));
304029c54b0SArd Biesheuvel 	if (pmd_none(*pmd) || pmd_bad(*pmd))
305c2febafcSKirill A. Shutemov 		return NULL;
306db64fe02SNick Piggin 
30748667e7aSChristoph Lameter 	ptep = pte_offset_map(pmd, addr);
30848667e7aSChristoph Lameter 	pte = *ptep;
30948667e7aSChristoph Lameter 	if (pte_present(pte))
310add688fbSmalc 		page = pte_page(pte);
31148667e7aSChristoph Lameter 	pte_unmap(ptep);
312add688fbSmalc 	return page;
313ece86e22SJianyu Zhan }
314ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page);
315ece86e22SJianyu Zhan 
316add688fbSmalc /*
317add688fbSmalc  * Map a vmalloc()-space virtual address to the physical page frame number.
318add688fbSmalc  */
319add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
320add688fbSmalc {
321add688fbSmalc 	return page_to_pfn(vmalloc_to_page(vmalloc_addr));
322add688fbSmalc }
323add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn);
324add688fbSmalc 
325db64fe02SNick Piggin 
326db64fe02SNick Piggin /*** Global kva allocator ***/
327db64fe02SNick Piggin 
328*78c72746SYisheng Xie #define VM_LAZY_FREE	0x02
329db64fe02SNick Piggin #define VM_VM_AREA	0x04
330db64fe02SNick Piggin 
331db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock);
332f1c4069eSJoonsoo Kim /* Export for kexec only */
333f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
33480c4bd7aSChris Wilson static LLIST_HEAD(vmap_purge_list);
33589699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT;
33689699605SNick Piggin 
33789699605SNick Piggin /* The vmap cache globals are protected by vmap_area_lock */
33889699605SNick Piggin static struct rb_node *free_vmap_cache;
33989699605SNick Piggin static unsigned long cached_hole_size;
34089699605SNick Piggin static unsigned long cached_vstart;
34189699605SNick Piggin static unsigned long cached_align;
34289699605SNick Piggin 
343ca23e405STejun Heo static unsigned long vmap_area_pcpu_hole;
344db64fe02SNick Piggin 
345db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr)
3461da177e4SLinus Torvalds {
347db64fe02SNick Piggin 	struct rb_node *n = vmap_area_root.rb_node;
348db64fe02SNick Piggin 
349db64fe02SNick Piggin 	while (n) {
350db64fe02SNick Piggin 		struct vmap_area *va;
351db64fe02SNick Piggin 
352db64fe02SNick Piggin 		va = rb_entry(n, struct vmap_area, rb_node);
353db64fe02SNick Piggin 		if (addr < va->va_start)
354db64fe02SNick Piggin 			n = n->rb_left;
355cef2ac3fSHATAYAMA Daisuke 		else if (addr >= va->va_end)
356db64fe02SNick Piggin 			n = n->rb_right;
357db64fe02SNick Piggin 		else
358db64fe02SNick Piggin 			return va;
359db64fe02SNick Piggin 	}
360db64fe02SNick Piggin 
361db64fe02SNick Piggin 	return NULL;
362db64fe02SNick Piggin }
363db64fe02SNick Piggin 
364db64fe02SNick Piggin static void __insert_vmap_area(struct vmap_area *va)
365db64fe02SNick Piggin {
366db64fe02SNick Piggin 	struct rb_node **p = &vmap_area_root.rb_node;
367db64fe02SNick Piggin 	struct rb_node *parent = NULL;
368db64fe02SNick Piggin 	struct rb_node *tmp;
369db64fe02SNick Piggin 
370db64fe02SNick Piggin 	while (*p) {
371170168d0SNamhyung Kim 		struct vmap_area *tmp_va;
372db64fe02SNick Piggin 
373db64fe02SNick Piggin 		parent = *p;
374170168d0SNamhyung Kim 		tmp_va = rb_entry(parent, struct vmap_area, rb_node);
375170168d0SNamhyung Kim 		if (va->va_start < tmp_va->va_end)
376db64fe02SNick Piggin 			p = &(*p)->rb_left;
377170168d0SNamhyung Kim 		else if (va->va_end > tmp_va->va_start)
378db64fe02SNick Piggin 			p = &(*p)->rb_right;
379db64fe02SNick Piggin 		else
380db64fe02SNick Piggin 			BUG();
381db64fe02SNick Piggin 	}
382db64fe02SNick Piggin 
383db64fe02SNick Piggin 	rb_link_node(&va->rb_node, parent, p);
384db64fe02SNick Piggin 	rb_insert_color(&va->rb_node, &vmap_area_root);
385db64fe02SNick Piggin 
3864341fa45SJoonsoo Kim 	/* address-sort this list */
387db64fe02SNick Piggin 	tmp = rb_prev(&va->rb_node);
388db64fe02SNick Piggin 	if (tmp) {
389db64fe02SNick Piggin 		struct vmap_area *prev;
390db64fe02SNick Piggin 		prev = rb_entry(tmp, struct vmap_area, rb_node);
391db64fe02SNick Piggin 		list_add_rcu(&va->list, &prev->list);
392db64fe02SNick Piggin 	} else
393db64fe02SNick Piggin 		list_add_rcu(&va->list, &vmap_area_list);
394db64fe02SNick Piggin }
395db64fe02SNick Piggin 
396db64fe02SNick Piggin static void purge_vmap_area_lazy(void);
397db64fe02SNick Piggin 
3984da56b99SChris Wilson static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
3994da56b99SChris Wilson 
400db64fe02SNick Piggin /*
401db64fe02SNick Piggin  * Allocate a region of KVA of the specified size and alignment, within the
402db64fe02SNick Piggin  * vstart and vend.
403db64fe02SNick Piggin  */
404db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size,
405db64fe02SNick Piggin 				unsigned long align,
406db64fe02SNick Piggin 				unsigned long vstart, unsigned long vend,
407db64fe02SNick Piggin 				int node, gfp_t gfp_mask)
408db64fe02SNick Piggin {
409db64fe02SNick Piggin 	struct vmap_area *va;
410db64fe02SNick Piggin 	struct rb_node *n;
4111da177e4SLinus Torvalds 	unsigned long addr;
412db64fe02SNick Piggin 	int purged = 0;
41389699605SNick Piggin 	struct vmap_area *first;
414db64fe02SNick Piggin 
4157766970cSNick Piggin 	BUG_ON(!size);
416891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
41789699605SNick Piggin 	BUG_ON(!is_power_of_2(align));
418db64fe02SNick Piggin 
4195803ed29SChristoph Hellwig 	might_sleep();
4204da56b99SChris Wilson 
421db64fe02SNick Piggin 	va = kmalloc_node(sizeof(struct vmap_area),
422db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
423db64fe02SNick Piggin 	if (unlikely(!va))
424db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
425db64fe02SNick Piggin 
4267f88f88fSCatalin Marinas 	/*
4277f88f88fSCatalin Marinas 	 * Only scan the relevant parts containing pointers to other objects
4287f88f88fSCatalin Marinas 	 * to avoid false negatives.
4297f88f88fSCatalin Marinas 	 */
4307f88f88fSCatalin Marinas 	kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK);
4317f88f88fSCatalin Marinas 
432db64fe02SNick Piggin retry:
433db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
43489699605SNick Piggin 	/*
43589699605SNick Piggin 	 * Invalidate cache if we have more permissive parameters.
43689699605SNick Piggin 	 * cached_hole_size notes the largest hole noticed _below_
43789699605SNick Piggin 	 * the vmap_area cached in free_vmap_cache: if size fits
43889699605SNick Piggin 	 * into that hole, we want to scan from vstart to reuse
43989699605SNick Piggin 	 * the hole instead of allocating above free_vmap_cache.
44089699605SNick Piggin 	 * Note that __free_vmap_area may update free_vmap_cache
44189699605SNick Piggin 	 * without updating cached_hole_size or cached_align.
44289699605SNick Piggin 	 */
44389699605SNick Piggin 	if (!free_vmap_cache ||
44489699605SNick Piggin 			size < cached_hole_size ||
44589699605SNick Piggin 			vstart < cached_vstart ||
44689699605SNick Piggin 			align < cached_align) {
44789699605SNick Piggin nocache:
44889699605SNick Piggin 		cached_hole_size = 0;
44989699605SNick Piggin 		free_vmap_cache = NULL;
45089699605SNick Piggin 	}
45189699605SNick Piggin 	/* record if we encounter less permissive parameters */
45289699605SNick Piggin 	cached_vstart = vstart;
45389699605SNick Piggin 	cached_align = align;
45489699605SNick Piggin 
45589699605SNick Piggin 	/* find starting point for our search */
45689699605SNick Piggin 	if (free_vmap_cache) {
45789699605SNick Piggin 		first = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
458248ac0e1SJohannes Weiner 		addr = ALIGN(first->va_end, align);
45989699605SNick Piggin 		if (addr < vstart)
46089699605SNick Piggin 			goto nocache;
461bcb615a8SZhang Yanfei 		if (addr + size < addr)
4627766970cSNick Piggin 			goto overflow;
4637766970cSNick Piggin 
46489699605SNick Piggin 	} else {
46589699605SNick Piggin 		addr = ALIGN(vstart, align);
466bcb615a8SZhang Yanfei 		if (addr + size < addr)
46789699605SNick Piggin 			goto overflow;
468db64fe02SNick Piggin 
46989699605SNick Piggin 		n = vmap_area_root.rb_node;
47089699605SNick Piggin 		first = NULL;
47189699605SNick Piggin 
47289699605SNick Piggin 		while (n) {
473db64fe02SNick Piggin 			struct vmap_area *tmp;
474db64fe02SNick Piggin 			tmp = rb_entry(n, struct vmap_area, rb_node);
475db64fe02SNick Piggin 			if (tmp->va_end >= addr) {
476db64fe02SNick Piggin 				first = tmp;
47789699605SNick Piggin 				if (tmp->va_start <= addr)
47889699605SNick Piggin 					break;
479db64fe02SNick Piggin 				n = n->rb_left;
48089699605SNick Piggin 			} else
481db64fe02SNick Piggin 				n = n->rb_right;
482db64fe02SNick Piggin 		}
483db64fe02SNick Piggin 
484db64fe02SNick Piggin 		if (!first)
485db64fe02SNick Piggin 			goto found;
486db64fe02SNick Piggin 	}
487db64fe02SNick Piggin 
48889699605SNick Piggin 	/* from the starting point, walk areas until a suitable hole is found */
489248ac0e1SJohannes Weiner 	while (addr + size > first->va_start && addr + size <= vend) {
49089699605SNick Piggin 		if (addr + cached_hole_size < first->va_start)
49189699605SNick Piggin 			cached_hole_size = first->va_start - addr;
492248ac0e1SJohannes Weiner 		addr = ALIGN(first->va_end, align);
493bcb615a8SZhang Yanfei 		if (addr + size < addr)
4947766970cSNick Piggin 			goto overflow;
495db64fe02SNick Piggin 
49692ca922fSHong zhi guo 		if (list_is_last(&first->list, &vmap_area_list))
497db64fe02SNick Piggin 			goto found;
49892ca922fSHong zhi guo 
4996219c2a2SGeliang Tang 		first = list_next_entry(first, list);
500db64fe02SNick Piggin 	}
50189699605SNick Piggin 
502db64fe02SNick Piggin found:
50389699605SNick Piggin 	if (addr + size > vend)
50489699605SNick Piggin 		goto overflow;
50589699605SNick Piggin 
50689699605SNick Piggin 	va->va_start = addr;
50789699605SNick Piggin 	va->va_end = addr + size;
50889699605SNick Piggin 	va->flags = 0;
50989699605SNick Piggin 	__insert_vmap_area(va);
51089699605SNick Piggin 	free_vmap_cache = &va->rb_node;
51189699605SNick Piggin 	spin_unlock(&vmap_area_lock);
51289699605SNick Piggin 
51361e16557SWang Xiaoqiang 	BUG_ON(!IS_ALIGNED(va->va_start, align));
51489699605SNick Piggin 	BUG_ON(va->va_start < vstart);
51589699605SNick Piggin 	BUG_ON(va->va_end > vend);
51689699605SNick Piggin 
51789699605SNick Piggin 	return va;
51889699605SNick Piggin 
5197766970cSNick Piggin overflow:
520db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
521db64fe02SNick Piggin 	if (!purged) {
522db64fe02SNick Piggin 		purge_vmap_area_lazy();
523db64fe02SNick Piggin 		purged = 1;
524db64fe02SNick Piggin 		goto retry;
525db64fe02SNick Piggin 	}
5264da56b99SChris Wilson 
5274da56b99SChris Wilson 	if (gfpflags_allow_blocking(gfp_mask)) {
5284da56b99SChris Wilson 		unsigned long freed = 0;
5294da56b99SChris Wilson 		blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
5304da56b99SChris Wilson 		if (freed > 0) {
5314da56b99SChris Wilson 			purged = 0;
5324da56b99SChris Wilson 			goto retry;
5334da56b99SChris Wilson 		}
5344da56b99SChris Wilson 	}
5354da56b99SChris Wilson 
53603497d76SFlorian Fainelli 	if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
537756a025fSJoe Perches 		pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
538756a025fSJoe Perches 			size);
5392498ce42SRalph Wuerthner 	kfree(va);
540db64fe02SNick Piggin 	return ERR_PTR(-EBUSY);
541db64fe02SNick Piggin }
542db64fe02SNick Piggin 
5434da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb)
5444da56b99SChris Wilson {
5454da56b99SChris Wilson 	return blocking_notifier_chain_register(&vmap_notify_list, nb);
5464da56b99SChris Wilson }
5474da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
5484da56b99SChris Wilson 
5494da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb)
5504da56b99SChris Wilson {
5514da56b99SChris Wilson 	return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
5524da56b99SChris Wilson }
5534da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
5544da56b99SChris Wilson 
555db64fe02SNick Piggin static void __free_vmap_area(struct vmap_area *va)
556db64fe02SNick Piggin {
557db64fe02SNick Piggin 	BUG_ON(RB_EMPTY_NODE(&va->rb_node));
55889699605SNick Piggin 
55989699605SNick Piggin 	if (free_vmap_cache) {
56089699605SNick Piggin 		if (va->va_end < cached_vstart) {
56189699605SNick Piggin 			free_vmap_cache = NULL;
56289699605SNick Piggin 		} else {
56389699605SNick Piggin 			struct vmap_area *cache;
56489699605SNick Piggin 			cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
56589699605SNick Piggin 			if (va->va_start <= cache->va_start) {
56689699605SNick Piggin 				free_vmap_cache = rb_prev(&va->rb_node);
56789699605SNick Piggin 				/*
56889699605SNick Piggin 				 * We don't try to update cached_hole_size or
56989699605SNick Piggin 				 * cached_align, but it won't go very wrong.
57089699605SNick Piggin 				 */
57189699605SNick Piggin 			}
57289699605SNick Piggin 		}
57389699605SNick Piggin 	}
574db64fe02SNick Piggin 	rb_erase(&va->rb_node, &vmap_area_root);
575db64fe02SNick Piggin 	RB_CLEAR_NODE(&va->rb_node);
576db64fe02SNick Piggin 	list_del_rcu(&va->list);
577db64fe02SNick Piggin 
578ca23e405STejun Heo 	/*
579ca23e405STejun Heo 	 * Track the highest possible candidate for pcpu area
580ca23e405STejun Heo 	 * allocation.  Areas outside of vmalloc area can be returned
581ca23e405STejun Heo 	 * here too, consider only end addresses which fall inside
582ca23e405STejun Heo 	 * vmalloc area proper.
583ca23e405STejun Heo 	 */
584ca23e405STejun Heo 	if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
585ca23e405STejun Heo 		vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
586ca23e405STejun Heo 
58714769de9SLai Jiangshan 	kfree_rcu(va, rcu_head);
588db64fe02SNick Piggin }
589db64fe02SNick Piggin 
590db64fe02SNick Piggin /*
591db64fe02SNick Piggin  * Free a region of KVA allocated by alloc_vmap_area
592db64fe02SNick Piggin  */
593db64fe02SNick Piggin static void free_vmap_area(struct vmap_area *va)
594db64fe02SNick Piggin {
595db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
596db64fe02SNick Piggin 	__free_vmap_area(va);
597db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
598db64fe02SNick Piggin }
599db64fe02SNick Piggin 
600db64fe02SNick Piggin /*
601db64fe02SNick Piggin  * Clear the pagetable entries of a given vmap_area
602db64fe02SNick Piggin  */
603db64fe02SNick Piggin static void unmap_vmap_area(struct vmap_area *va)
604db64fe02SNick Piggin {
605db64fe02SNick Piggin 	vunmap_page_range(va->va_start, va->va_end);
606db64fe02SNick Piggin }
607db64fe02SNick Piggin 
608cd52858cSNick Piggin static void vmap_debug_free_range(unsigned long start, unsigned long end)
609cd52858cSNick Piggin {
610cd52858cSNick Piggin 	/*
611f48d97f3SJoonsoo Kim 	 * Unmap page tables and force a TLB flush immediately if pagealloc
612f48d97f3SJoonsoo Kim 	 * debugging is enabled.  This catches use after free bugs similarly to
613f48d97f3SJoonsoo Kim 	 * those in linear kernel virtual address space after a page has been
614f48d97f3SJoonsoo Kim 	 * freed.
615cd52858cSNick Piggin 	 *
616f48d97f3SJoonsoo Kim 	 * All the lazy freeing logic is still retained, in order to minimise
617f48d97f3SJoonsoo Kim 	 * intrusiveness of this debugging feature.
618cd52858cSNick Piggin 	 *
619f48d97f3SJoonsoo Kim 	 * This is going to be *slow* (linear kernel virtual address debugging
620f48d97f3SJoonsoo Kim 	 * doesn't do a broadcast TLB flush so it is a lot faster).
621cd52858cSNick Piggin 	 */
622f48d97f3SJoonsoo Kim 	if (debug_pagealloc_enabled()) {
623cd52858cSNick Piggin 		vunmap_page_range(start, end);
624cd52858cSNick Piggin 		flush_tlb_kernel_range(start, end);
625f48d97f3SJoonsoo Kim 	}
626cd52858cSNick Piggin }
627cd52858cSNick Piggin 
628db64fe02SNick Piggin /*
629db64fe02SNick Piggin  * lazy_max_pages is the maximum amount of virtual address space we gather up
630db64fe02SNick Piggin  * before attempting to purge with a TLB flush.
631db64fe02SNick Piggin  *
632db64fe02SNick Piggin  * There is a tradeoff here: a larger number will cover more kernel page tables
633db64fe02SNick Piggin  * and take slightly longer to purge, but it will linearly reduce the number of
634db64fe02SNick Piggin  * global TLB flushes that must be performed. It would seem natural to scale
635db64fe02SNick Piggin  * this number up linearly with the number of CPUs (because vmapping activity
636db64fe02SNick Piggin  * could also scale linearly with the number of CPUs), however it is likely
637db64fe02SNick Piggin  * that in practice, workloads might be constrained in other ways that mean
638db64fe02SNick Piggin  * vmap activity will not scale linearly with CPUs. Also, I want to be
639db64fe02SNick Piggin  * conservative and not introduce a big latency on huge systems, so go with
640db64fe02SNick Piggin  * a less aggressive log scale. It will still be an improvement over the old
641db64fe02SNick Piggin  * code, and it will be simple to change the scale factor if we find that it
642db64fe02SNick Piggin  * becomes a problem on bigger systems.
643db64fe02SNick Piggin  */
644db64fe02SNick Piggin static unsigned long lazy_max_pages(void)
645db64fe02SNick Piggin {
646db64fe02SNick Piggin 	unsigned int log;
647db64fe02SNick Piggin 
648db64fe02SNick Piggin 	log = fls(num_online_cpus());
649db64fe02SNick Piggin 
650db64fe02SNick Piggin 	return log * (32UL * 1024 * 1024 / PAGE_SIZE);
651db64fe02SNick Piggin }
652db64fe02SNick Piggin 
653db64fe02SNick Piggin static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
654db64fe02SNick Piggin 
6550574ecd1SChristoph Hellwig /*
6560574ecd1SChristoph Hellwig  * Serialize vmap purging.  There is no actual criticial section protected
6570574ecd1SChristoph Hellwig  * by this look, but we want to avoid concurrent calls for performance
6580574ecd1SChristoph Hellwig  * reasons and to make the pcpu_get_vm_areas more deterministic.
6590574ecd1SChristoph Hellwig  */
660f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock);
6610574ecd1SChristoph Hellwig 
66202b709dfSNick Piggin /* for per-CPU blocks */
66302b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void);
66402b709dfSNick Piggin 
665db64fe02SNick Piggin /*
6663ee48b6aSCliff Wickman  * called before a call to iounmap() if the caller wants vm_area_struct's
6673ee48b6aSCliff Wickman  * immediately freed.
6683ee48b6aSCliff Wickman  */
6693ee48b6aSCliff Wickman void set_iounmap_nonlazy(void)
6703ee48b6aSCliff Wickman {
6713ee48b6aSCliff Wickman 	atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
6723ee48b6aSCliff Wickman }
6733ee48b6aSCliff Wickman 
6743ee48b6aSCliff Wickman /*
675db64fe02SNick Piggin  * Purges all lazily-freed vmap areas.
676db64fe02SNick Piggin  */
6770574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
678db64fe02SNick Piggin {
67980c4bd7aSChris Wilson 	struct llist_node *valist;
680db64fe02SNick Piggin 	struct vmap_area *va;
681cbb76676SVegard Nossum 	struct vmap_area *n_va;
682763b218dSJoel Fernandes 	bool do_free = false;
683db64fe02SNick Piggin 
6840574ecd1SChristoph Hellwig 	lockdep_assert_held(&vmap_purge_lock);
68502b709dfSNick Piggin 
68680c4bd7aSChris Wilson 	valist = llist_del_all(&vmap_purge_list);
68780c4bd7aSChris Wilson 	llist_for_each_entry(va, valist, purge_list) {
6880574ecd1SChristoph Hellwig 		if (va->va_start < start)
6890574ecd1SChristoph Hellwig 			start = va->va_start;
6900574ecd1SChristoph Hellwig 		if (va->va_end > end)
6910574ecd1SChristoph Hellwig 			end = va->va_end;
692763b218dSJoel Fernandes 		do_free = true;
693db64fe02SNick Piggin 	}
694db64fe02SNick Piggin 
695763b218dSJoel Fernandes 	if (!do_free)
6960574ecd1SChristoph Hellwig 		return false;
6970574ecd1SChristoph Hellwig 
6980574ecd1SChristoph Hellwig 	flush_tlb_kernel_range(start, end);
699db64fe02SNick Piggin 
700db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
701763b218dSJoel Fernandes 	llist_for_each_entry_safe(va, n_va, valist, purge_list) {
702763b218dSJoel Fernandes 		int nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
703763b218dSJoel Fernandes 
704db64fe02SNick Piggin 		__free_vmap_area(va);
705763b218dSJoel Fernandes 		atomic_sub(nr, &vmap_lazy_nr);
706763b218dSJoel Fernandes 		cond_resched_lock(&vmap_area_lock);
707763b218dSJoel Fernandes 	}
708db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
7090574ecd1SChristoph Hellwig 	return true;
710db64fe02SNick Piggin }
711db64fe02SNick Piggin 
712db64fe02SNick Piggin /*
713496850e5SNick Piggin  * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
714496850e5SNick Piggin  * is already purging.
715496850e5SNick Piggin  */
716496850e5SNick Piggin static void try_purge_vmap_area_lazy(void)
717496850e5SNick Piggin {
718f9e09977SChristoph Hellwig 	if (mutex_trylock(&vmap_purge_lock)) {
7190574ecd1SChristoph Hellwig 		__purge_vmap_area_lazy(ULONG_MAX, 0);
720f9e09977SChristoph Hellwig 		mutex_unlock(&vmap_purge_lock);
7210574ecd1SChristoph Hellwig 	}
722496850e5SNick Piggin }
723496850e5SNick Piggin 
724496850e5SNick Piggin /*
725db64fe02SNick Piggin  * Kick off a purge of the outstanding lazy areas.
726db64fe02SNick Piggin  */
727db64fe02SNick Piggin static void purge_vmap_area_lazy(void)
728db64fe02SNick Piggin {
729f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
7300574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
7310574ecd1SChristoph Hellwig 	__purge_vmap_area_lazy(ULONG_MAX, 0);
732f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
733db64fe02SNick Piggin }
734db64fe02SNick Piggin 
735db64fe02SNick Piggin /*
73664141da5SJeremy Fitzhardinge  * Free a vmap area, caller ensuring that the area has been unmapped
73764141da5SJeremy Fitzhardinge  * and flush_cache_vunmap had been called for the correct range
73864141da5SJeremy Fitzhardinge  * previously.
739db64fe02SNick Piggin  */
74064141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va)
741db64fe02SNick Piggin {
74280c4bd7aSChris Wilson 	int nr_lazy;
74380c4bd7aSChris Wilson 
74480c4bd7aSChris Wilson 	nr_lazy = atomic_add_return((va->va_end - va->va_start) >> PAGE_SHIFT,
74580c4bd7aSChris Wilson 				    &vmap_lazy_nr);
74680c4bd7aSChris Wilson 
74780c4bd7aSChris Wilson 	/* After this point, we may free va at any time */
74880c4bd7aSChris Wilson 	llist_add(&va->purge_list, &vmap_purge_list);
74980c4bd7aSChris Wilson 
75080c4bd7aSChris Wilson 	if (unlikely(nr_lazy > lazy_max_pages()))
751496850e5SNick Piggin 		try_purge_vmap_area_lazy();
752db64fe02SNick Piggin }
753db64fe02SNick Piggin 
754b29acbdcSNick Piggin /*
755b29acbdcSNick Piggin  * Free and unmap a vmap area
756b29acbdcSNick Piggin  */
757b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va)
758b29acbdcSNick Piggin {
759b29acbdcSNick Piggin 	flush_cache_vunmap(va->va_start, va->va_end);
760c8eef01eSChristoph Hellwig 	unmap_vmap_area(va);
761c8eef01eSChristoph Hellwig 	free_vmap_area_noflush(va);
762b29acbdcSNick Piggin }
763b29acbdcSNick Piggin 
764db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr)
765db64fe02SNick Piggin {
766db64fe02SNick Piggin 	struct vmap_area *va;
767db64fe02SNick Piggin 
768db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
769db64fe02SNick Piggin 	va = __find_vmap_area(addr);
770db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
771db64fe02SNick Piggin 
772db64fe02SNick Piggin 	return va;
773db64fe02SNick Piggin }
774db64fe02SNick Piggin 
775db64fe02SNick Piggin /*** Per cpu kva allocator ***/
776db64fe02SNick Piggin 
777db64fe02SNick Piggin /*
778db64fe02SNick Piggin  * vmap space is limited especially on 32 bit architectures. Ensure there is
779db64fe02SNick Piggin  * room for at least 16 percpu vmap blocks per CPU.
780db64fe02SNick Piggin  */
781db64fe02SNick Piggin /*
782db64fe02SNick Piggin  * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
783db64fe02SNick Piggin  * to #define VMALLOC_SPACE		(VMALLOC_END-VMALLOC_START). Guess
784db64fe02SNick Piggin  * instead (we just need a rough idea)
785db64fe02SNick Piggin  */
786db64fe02SNick Piggin #if BITS_PER_LONG == 32
787db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024)
788db64fe02SNick Piggin #else
789db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024*1024)
790db64fe02SNick Piggin #endif
791db64fe02SNick Piggin 
792db64fe02SNick Piggin #define VMALLOC_PAGES		(VMALLOC_SPACE / PAGE_SIZE)
793db64fe02SNick Piggin #define VMAP_MAX_ALLOC		BITS_PER_LONG	/* 256K with 4K pages */
794db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX	1024	/* 4MB with 4K pages */
795db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN	(VMAP_MAX_ALLOC*2)
796db64fe02SNick Piggin #define VMAP_MIN(x, y)		((x) < (y) ? (x) : (y)) /* can't use min() */
797db64fe02SNick Piggin #define VMAP_MAX(x, y)		((x) > (y) ? (x) : (y)) /* can't use max() */
798f982f915SClemens Ladisch #define VMAP_BBMAP_BITS		\
799f982f915SClemens Ladisch 		VMAP_MIN(VMAP_BBMAP_BITS_MAX,	\
800db64fe02SNick Piggin 		VMAP_MAX(VMAP_BBMAP_BITS_MIN,	\
801f982f915SClemens Ladisch 			VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
802db64fe02SNick Piggin 
803db64fe02SNick Piggin #define VMAP_BLOCK_SIZE		(VMAP_BBMAP_BITS * PAGE_SIZE)
804db64fe02SNick Piggin 
8059b463334SJeremy Fitzhardinge static bool vmap_initialized __read_mostly = false;
8069b463334SJeremy Fitzhardinge 
807db64fe02SNick Piggin struct vmap_block_queue {
808db64fe02SNick Piggin 	spinlock_t lock;
809db64fe02SNick Piggin 	struct list_head free;
810db64fe02SNick Piggin };
811db64fe02SNick Piggin 
812db64fe02SNick Piggin struct vmap_block {
813db64fe02SNick Piggin 	spinlock_t lock;
814db64fe02SNick Piggin 	struct vmap_area *va;
815db64fe02SNick Piggin 	unsigned long free, dirty;
8167d61bfe8SRoman Pen 	unsigned long dirty_min, dirty_max; /*< dirty range */
817db64fe02SNick Piggin 	struct list_head free_list;
818db64fe02SNick Piggin 	struct rcu_head rcu_head;
81902b709dfSNick Piggin 	struct list_head purge;
820db64fe02SNick Piggin };
821db64fe02SNick Piggin 
822db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
823db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
824db64fe02SNick Piggin 
825db64fe02SNick Piggin /*
826db64fe02SNick Piggin  * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
827db64fe02SNick Piggin  * in the free path. Could get rid of this if we change the API to return a
828db64fe02SNick Piggin  * "cookie" from alloc, to be passed to free. But no big deal yet.
829db64fe02SNick Piggin  */
830db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_block_tree_lock);
831db64fe02SNick Piggin static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
832db64fe02SNick Piggin 
833db64fe02SNick Piggin /*
834db64fe02SNick Piggin  * We should probably have a fallback mechanism to allocate virtual memory
835db64fe02SNick Piggin  * out of partially filled vmap blocks. However vmap block sizing should be
836db64fe02SNick Piggin  * fairly reasonable according to the vmalloc size, so it shouldn't be a
837db64fe02SNick Piggin  * big problem.
838db64fe02SNick Piggin  */
839db64fe02SNick Piggin 
840db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr)
841db64fe02SNick Piggin {
842db64fe02SNick Piggin 	addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
843db64fe02SNick Piggin 	addr /= VMAP_BLOCK_SIZE;
844db64fe02SNick Piggin 	return addr;
845db64fe02SNick Piggin }
846db64fe02SNick Piggin 
847cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
848cf725ce2SRoman Pen {
849cf725ce2SRoman Pen 	unsigned long addr;
850cf725ce2SRoman Pen 
851cf725ce2SRoman Pen 	addr = va_start + (pages_off << PAGE_SHIFT);
852cf725ce2SRoman Pen 	BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
853cf725ce2SRoman Pen 	return (void *)addr;
854cf725ce2SRoman Pen }
855cf725ce2SRoman Pen 
856cf725ce2SRoman Pen /**
857cf725ce2SRoman Pen  * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
858cf725ce2SRoman Pen  *                  block. Of course pages number can't exceed VMAP_BBMAP_BITS
859cf725ce2SRoman Pen  * @order:    how many 2^order pages should be occupied in newly allocated block
860cf725ce2SRoman Pen  * @gfp_mask: flags for the page level allocator
861cf725ce2SRoman Pen  *
862cf725ce2SRoman Pen  * Returns: virtual address in a newly allocated block or ERR_PTR(-errno)
863cf725ce2SRoman Pen  */
864cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
865db64fe02SNick Piggin {
866db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
867db64fe02SNick Piggin 	struct vmap_block *vb;
868db64fe02SNick Piggin 	struct vmap_area *va;
869db64fe02SNick Piggin 	unsigned long vb_idx;
870db64fe02SNick Piggin 	int node, err;
871cf725ce2SRoman Pen 	void *vaddr;
872db64fe02SNick Piggin 
873db64fe02SNick Piggin 	node = numa_node_id();
874db64fe02SNick Piggin 
875db64fe02SNick Piggin 	vb = kmalloc_node(sizeof(struct vmap_block),
876db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
877db64fe02SNick Piggin 	if (unlikely(!vb))
878db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
879db64fe02SNick Piggin 
880db64fe02SNick Piggin 	va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
881db64fe02SNick Piggin 					VMALLOC_START, VMALLOC_END,
882db64fe02SNick Piggin 					node, gfp_mask);
883ddf9c6d4STobias Klauser 	if (IS_ERR(va)) {
884db64fe02SNick Piggin 		kfree(vb);
885e7d86340SJulia Lawall 		return ERR_CAST(va);
886db64fe02SNick Piggin 	}
887db64fe02SNick Piggin 
888db64fe02SNick Piggin 	err = radix_tree_preload(gfp_mask);
889db64fe02SNick Piggin 	if (unlikely(err)) {
890db64fe02SNick Piggin 		kfree(vb);
891db64fe02SNick Piggin 		free_vmap_area(va);
892db64fe02SNick Piggin 		return ERR_PTR(err);
893db64fe02SNick Piggin 	}
894db64fe02SNick Piggin 
895cf725ce2SRoman Pen 	vaddr = vmap_block_vaddr(va->va_start, 0);
896db64fe02SNick Piggin 	spin_lock_init(&vb->lock);
897db64fe02SNick Piggin 	vb->va = va;
898cf725ce2SRoman Pen 	/* At least something should be left free */
899cf725ce2SRoman Pen 	BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
900cf725ce2SRoman Pen 	vb->free = VMAP_BBMAP_BITS - (1UL << order);
901db64fe02SNick Piggin 	vb->dirty = 0;
9027d61bfe8SRoman Pen 	vb->dirty_min = VMAP_BBMAP_BITS;
9037d61bfe8SRoman Pen 	vb->dirty_max = 0;
904db64fe02SNick Piggin 	INIT_LIST_HEAD(&vb->free_list);
905db64fe02SNick Piggin 
906db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(va->va_start);
907db64fe02SNick Piggin 	spin_lock(&vmap_block_tree_lock);
908db64fe02SNick Piggin 	err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
909db64fe02SNick Piggin 	spin_unlock(&vmap_block_tree_lock);
910db64fe02SNick Piggin 	BUG_ON(err);
911db64fe02SNick Piggin 	radix_tree_preload_end();
912db64fe02SNick Piggin 
913db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
914db64fe02SNick Piggin 	spin_lock(&vbq->lock);
91568ac546fSRoman Pen 	list_add_tail_rcu(&vb->free_list, &vbq->free);
916db64fe02SNick Piggin 	spin_unlock(&vbq->lock);
9173f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
918db64fe02SNick Piggin 
919cf725ce2SRoman Pen 	return vaddr;
920db64fe02SNick Piggin }
921db64fe02SNick Piggin 
922db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb)
923db64fe02SNick Piggin {
924db64fe02SNick Piggin 	struct vmap_block *tmp;
925db64fe02SNick Piggin 	unsigned long vb_idx;
926db64fe02SNick Piggin 
927db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(vb->va->va_start);
928db64fe02SNick Piggin 	spin_lock(&vmap_block_tree_lock);
929db64fe02SNick Piggin 	tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
930db64fe02SNick Piggin 	spin_unlock(&vmap_block_tree_lock);
931db64fe02SNick Piggin 	BUG_ON(tmp != vb);
932db64fe02SNick Piggin 
93364141da5SJeremy Fitzhardinge 	free_vmap_area_noflush(vb->va);
93422a3c7d1SLai Jiangshan 	kfree_rcu(vb, rcu_head);
935db64fe02SNick Piggin }
936db64fe02SNick Piggin 
93702b709dfSNick Piggin static void purge_fragmented_blocks(int cpu)
93802b709dfSNick Piggin {
93902b709dfSNick Piggin 	LIST_HEAD(purge);
94002b709dfSNick Piggin 	struct vmap_block *vb;
94102b709dfSNick Piggin 	struct vmap_block *n_vb;
94202b709dfSNick Piggin 	struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
94302b709dfSNick Piggin 
94402b709dfSNick Piggin 	rcu_read_lock();
94502b709dfSNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
94602b709dfSNick Piggin 
94702b709dfSNick Piggin 		if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
94802b709dfSNick Piggin 			continue;
94902b709dfSNick Piggin 
95002b709dfSNick Piggin 		spin_lock(&vb->lock);
95102b709dfSNick Piggin 		if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
95202b709dfSNick Piggin 			vb->free = 0; /* prevent further allocs after releasing lock */
95302b709dfSNick Piggin 			vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
9547d61bfe8SRoman Pen 			vb->dirty_min = 0;
9557d61bfe8SRoman Pen 			vb->dirty_max = VMAP_BBMAP_BITS;
95602b709dfSNick Piggin 			spin_lock(&vbq->lock);
95702b709dfSNick Piggin 			list_del_rcu(&vb->free_list);
95802b709dfSNick Piggin 			spin_unlock(&vbq->lock);
95902b709dfSNick Piggin 			spin_unlock(&vb->lock);
96002b709dfSNick Piggin 			list_add_tail(&vb->purge, &purge);
96102b709dfSNick Piggin 		} else
96202b709dfSNick Piggin 			spin_unlock(&vb->lock);
96302b709dfSNick Piggin 	}
96402b709dfSNick Piggin 	rcu_read_unlock();
96502b709dfSNick Piggin 
96602b709dfSNick Piggin 	list_for_each_entry_safe(vb, n_vb, &purge, purge) {
96702b709dfSNick Piggin 		list_del(&vb->purge);
96802b709dfSNick Piggin 		free_vmap_block(vb);
96902b709dfSNick Piggin 	}
97002b709dfSNick Piggin }
97102b709dfSNick Piggin 
97202b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void)
97302b709dfSNick Piggin {
97402b709dfSNick Piggin 	int cpu;
97502b709dfSNick Piggin 
97602b709dfSNick Piggin 	for_each_possible_cpu(cpu)
97702b709dfSNick Piggin 		purge_fragmented_blocks(cpu);
97802b709dfSNick Piggin }
97902b709dfSNick Piggin 
980db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
981db64fe02SNick Piggin {
982db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
983db64fe02SNick Piggin 	struct vmap_block *vb;
984cf725ce2SRoman Pen 	void *vaddr = NULL;
985db64fe02SNick Piggin 	unsigned int order;
986db64fe02SNick Piggin 
987891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
988db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
989aa91c4d8SJan Kara 	if (WARN_ON(size == 0)) {
990aa91c4d8SJan Kara 		/*
991aa91c4d8SJan Kara 		 * Allocating 0 bytes isn't what caller wants since
992aa91c4d8SJan Kara 		 * get_order(0) returns funny result. Just warn and terminate
993aa91c4d8SJan Kara 		 * early.
994aa91c4d8SJan Kara 		 */
995aa91c4d8SJan Kara 		return NULL;
996aa91c4d8SJan Kara 	}
997db64fe02SNick Piggin 	order = get_order(size);
998db64fe02SNick Piggin 
999db64fe02SNick Piggin 	rcu_read_lock();
1000db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
1001db64fe02SNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1002cf725ce2SRoman Pen 		unsigned long pages_off;
1003db64fe02SNick Piggin 
1004db64fe02SNick Piggin 		spin_lock(&vb->lock);
1005cf725ce2SRoman Pen 		if (vb->free < (1UL << order)) {
1006cf725ce2SRoman Pen 			spin_unlock(&vb->lock);
1007cf725ce2SRoman Pen 			continue;
1008cf725ce2SRoman Pen 		}
100902b709dfSNick Piggin 
1010cf725ce2SRoman Pen 		pages_off = VMAP_BBMAP_BITS - vb->free;
1011cf725ce2SRoman Pen 		vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
1012db64fe02SNick Piggin 		vb->free -= 1UL << order;
1013db64fe02SNick Piggin 		if (vb->free == 0) {
1014db64fe02SNick Piggin 			spin_lock(&vbq->lock);
1015de560423SNick Piggin 			list_del_rcu(&vb->free_list);
1016db64fe02SNick Piggin 			spin_unlock(&vbq->lock);
1017db64fe02SNick Piggin 		}
1018cf725ce2SRoman Pen 
1019db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1020db64fe02SNick Piggin 		break;
1021db64fe02SNick Piggin 	}
102202b709dfSNick Piggin 
10233f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
1024db64fe02SNick Piggin 	rcu_read_unlock();
1025db64fe02SNick Piggin 
1026cf725ce2SRoman Pen 	/* Allocate new block if nothing was found */
1027cf725ce2SRoman Pen 	if (!vaddr)
1028cf725ce2SRoman Pen 		vaddr = new_vmap_block(order, gfp_mask);
1029db64fe02SNick Piggin 
1030cf725ce2SRoman Pen 	return vaddr;
1031db64fe02SNick Piggin }
1032db64fe02SNick Piggin 
1033db64fe02SNick Piggin static void vb_free(const void *addr, unsigned long size)
1034db64fe02SNick Piggin {
1035db64fe02SNick Piggin 	unsigned long offset;
1036db64fe02SNick Piggin 	unsigned long vb_idx;
1037db64fe02SNick Piggin 	unsigned int order;
1038db64fe02SNick Piggin 	struct vmap_block *vb;
1039db64fe02SNick Piggin 
1040891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(size));
1041db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
1042b29acbdcSNick Piggin 
1043b29acbdcSNick Piggin 	flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
1044b29acbdcSNick Piggin 
1045db64fe02SNick Piggin 	order = get_order(size);
1046db64fe02SNick Piggin 
1047db64fe02SNick Piggin 	offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
10487d61bfe8SRoman Pen 	offset >>= PAGE_SHIFT;
1049db64fe02SNick Piggin 
1050db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx((unsigned long)addr);
1051db64fe02SNick Piggin 	rcu_read_lock();
1052db64fe02SNick Piggin 	vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
1053db64fe02SNick Piggin 	rcu_read_unlock();
1054db64fe02SNick Piggin 	BUG_ON(!vb);
1055db64fe02SNick Piggin 
105664141da5SJeremy Fitzhardinge 	vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
105764141da5SJeremy Fitzhardinge 
1058db64fe02SNick Piggin 	spin_lock(&vb->lock);
10597d61bfe8SRoman Pen 
10607d61bfe8SRoman Pen 	/* Expand dirty range */
10617d61bfe8SRoman Pen 	vb->dirty_min = min(vb->dirty_min, offset);
10627d61bfe8SRoman Pen 	vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
1063d086817dSMinChan Kim 
1064db64fe02SNick Piggin 	vb->dirty += 1UL << order;
1065db64fe02SNick Piggin 	if (vb->dirty == VMAP_BBMAP_BITS) {
1066de560423SNick Piggin 		BUG_ON(vb->free);
1067db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1068db64fe02SNick Piggin 		free_vmap_block(vb);
1069db64fe02SNick Piggin 	} else
1070db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1071db64fe02SNick Piggin }
1072db64fe02SNick Piggin 
1073db64fe02SNick Piggin /**
1074db64fe02SNick Piggin  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1075db64fe02SNick Piggin  *
1076db64fe02SNick Piggin  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1077db64fe02SNick Piggin  * to amortize TLB flushing overheads. What this means is that any page you
1078db64fe02SNick Piggin  * have now, may, in a former life, have been mapped into kernel virtual
1079db64fe02SNick Piggin  * address by the vmap layer and so there might be some CPUs with TLB entries
1080db64fe02SNick Piggin  * still referencing that page (additional to the regular 1:1 kernel mapping).
1081db64fe02SNick Piggin  *
1082db64fe02SNick Piggin  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1083db64fe02SNick Piggin  * be sure that none of the pages we have control over will have any aliases
1084db64fe02SNick Piggin  * from the vmap layer.
1085db64fe02SNick Piggin  */
1086db64fe02SNick Piggin void vm_unmap_aliases(void)
1087db64fe02SNick Piggin {
1088db64fe02SNick Piggin 	unsigned long start = ULONG_MAX, end = 0;
1089db64fe02SNick Piggin 	int cpu;
1090db64fe02SNick Piggin 	int flush = 0;
1091db64fe02SNick Piggin 
10929b463334SJeremy Fitzhardinge 	if (unlikely(!vmap_initialized))
10939b463334SJeremy Fitzhardinge 		return;
10949b463334SJeremy Fitzhardinge 
10955803ed29SChristoph Hellwig 	might_sleep();
10965803ed29SChristoph Hellwig 
1097db64fe02SNick Piggin 	for_each_possible_cpu(cpu) {
1098db64fe02SNick Piggin 		struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1099db64fe02SNick Piggin 		struct vmap_block *vb;
1100db64fe02SNick Piggin 
1101db64fe02SNick Piggin 		rcu_read_lock();
1102db64fe02SNick Piggin 		list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1103db64fe02SNick Piggin 			spin_lock(&vb->lock);
11047d61bfe8SRoman Pen 			if (vb->dirty) {
11057d61bfe8SRoman Pen 				unsigned long va_start = vb->va->va_start;
1106db64fe02SNick Piggin 				unsigned long s, e;
1107b136be5eSJoonsoo Kim 
11087d61bfe8SRoman Pen 				s = va_start + (vb->dirty_min << PAGE_SHIFT);
11097d61bfe8SRoman Pen 				e = va_start + (vb->dirty_max << PAGE_SHIFT);
1110db64fe02SNick Piggin 
11117d61bfe8SRoman Pen 				start = min(s, start);
11127d61bfe8SRoman Pen 				end   = max(e, end);
11137d61bfe8SRoman Pen 
1114db64fe02SNick Piggin 				flush = 1;
1115db64fe02SNick Piggin 			}
1116db64fe02SNick Piggin 			spin_unlock(&vb->lock);
1117db64fe02SNick Piggin 		}
1118db64fe02SNick Piggin 		rcu_read_unlock();
1119db64fe02SNick Piggin 	}
1120db64fe02SNick Piggin 
1121f9e09977SChristoph Hellwig 	mutex_lock(&vmap_purge_lock);
11220574ecd1SChristoph Hellwig 	purge_fragmented_blocks_allcpus();
11230574ecd1SChristoph Hellwig 	if (!__purge_vmap_area_lazy(start, end) && flush)
11240574ecd1SChristoph Hellwig 		flush_tlb_kernel_range(start, end);
1125f9e09977SChristoph Hellwig 	mutex_unlock(&vmap_purge_lock);
1126db64fe02SNick Piggin }
1127db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1128db64fe02SNick Piggin 
1129db64fe02SNick Piggin /**
1130db64fe02SNick Piggin  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1131db64fe02SNick Piggin  * @mem: the pointer returned by vm_map_ram
1132db64fe02SNick Piggin  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1133db64fe02SNick Piggin  */
1134db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count)
1135db64fe02SNick Piggin {
113665ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
1137db64fe02SNick Piggin 	unsigned long addr = (unsigned long)mem;
11389c3acf60SChristoph Hellwig 	struct vmap_area *va;
1139db64fe02SNick Piggin 
11405803ed29SChristoph Hellwig 	might_sleep();
1141db64fe02SNick Piggin 	BUG_ON(!addr);
1142db64fe02SNick Piggin 	BUG_ON(addr < VMALLOC_START);
1143db64fe02SNick Piggin 	BUG_ON(addr > VMALLOC_END);
1144a1c0b1a0SShawn Lin 	BUG_ON(!PAGE_ALIGNED(addr));
1145db64fe02SNick Piggin 
1146db64fe02SNick Piggin 	debug_check_no_locks_freed(mem, size);
1147cd52858cSNick Piggin 	vmap_debug_free_range(addr, addr+size);
1148db64fe02SNick Piggin 
11499c3acf60SChristoph Hellwig 	if (likely(count <= VMAP_MAX_ALLOC)) {
1150db64fe02SNick Piggin 		vb_free(mem, size);
11519c3acf60SChristoph Hellwig 		return;
11529c3acf60SChristoph Hellwig 	}
11539c3acf60SChristoph Hellwig 
11549c3acf60SChristoph Hellwig 	va = find_vmap_area(addr);
11559c3acf60SChristoph Hellwig 	BUG_ON(!va);
11569c3acf60SChristoph Hellwig 	free_unmap_vmap_area(va);
1157db64fe02SNick Piggin }
1158db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram);
1159db64fe02SNick Piggin 
1160db64fe02SNick Piggin /**
1161db64fe02SNick Piggin  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1162db64fe02SNick Piggin  * @pages: an array of pointers to the pages to be mapped
1163db64fe02SNick Piggin  * @count: number of pages
1164db64fe02SNick Piggin  * @node: prefer to allocate data structures on this node
1165db64fe02SNick Piggin  * @prot: memory protection to use. PAGE_KERNEL for regular RAM
1166e99c97adSRandy Dunlap  *
116736437638SGioh Kim  * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
116836437638SGioh Kim  * faster than vmap so it's good.  But if you mix long-life and short-life
116936437638SGioh Kim  * objects with vm_map_ram(), it could consume lots of address space through
117036437638SGioh Kim  * fragmentation (especially on a 32bit machine).  You could see failures in
117136437638SGioh Kim  * the end.  Please use this function for short-lived objects.
117236437638SGioh Kim  *
1173e99c97adSRandy Dunlap  * Returns: a pointer to the address that has been mapped, or %NULL on failure
1174db64fe02SNick Piggin  */
1175db64fe02SNick Piggin void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1176db64fe02SNick Piggin {
117765ee03c4SGuillermo Julián Moreno 	unsigned long size = (unsigned long)count << PAGE_SHIFT;
1178db64fe02SNick Piggin 	unsigned long addr;
1179db64fe02SNick Piggin 	void *mem;
1180db64fe02SNick Piggin 
1181db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC)) {
1182db64fe02SNick Piggin 		mem = vb_alloc(size, GFP_KERNEL);
1183db64fe02SNick Piggin 		if (IS_ERR(mem))
1184db64fe02SNick Piggin 			return NULL;
1185db64fe02SNick Piggin 		addr = (unsigned long)mem;
1186db64fe02SNick Piggin 	} else {
1187db64fe02SNick Piggin 		struct vmap_area *va;
1188db64fe02SNick Piggin 		va = alloc_vmap_area(size, PAGE_SIZE,
1189db64fe02SNick Piggin 				VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1190db64fe02SNick Piggin 		if (IS_ERR(va))
1191db64fe02SNick Piggin 			return NULL;
1192db64fe02SNick Piggin 
1193db64fe02SNick Piggin 		addr = va->va_start;
1194db64fe02SNick Piggin 		mem = (void *)addr;
1195db64fe02SNick Piggin 	}
1196db64fe02SNick Piggin 	if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1197db64fe02SNick Piggin 		vm_unmap_ram(mem, count);
1198db64fe02SNick Piggin 		return NULL;
1199db64fe02SNick Piggin 	}
1200db64fe02SNick Piggin 	return mem;
1201db64fe02SNick Piggin }
1202db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram);
1203db64fe02SNick Piggin 
12044341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata;
1205f0aa6617STejun Heo /**
1206be9b7335SNicolas Pitre  * vm_area_add_early - add vmap area early during boot
1207be9b7335SNicolas Pitre  * @vm: vm_struct to add
1208be9b7335SNicolas Pitre  *
1209be9b7335SNicolas Pitre  * This function is used to add fixed kernel vm area to vmlist before
1210be9b7335SNicolas Pitre  * vmalloc_init() is called.  @vm->addr, @vm->size, and @vm->flags
1211be9b7335SNicolas Pitre  * should contain proper values and the other fields should be zero.
1212be9b7335SNicolas Pitre  *
1213be9b7335SNicolas Pitre  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1214be9b7335SNicolas Pitre  */
1215be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm)
1216be9b7335SNicolas Pitre {
1217be9b7335SNicolas Pitre 	struct vm_struct *tmp, **p;
1218be9b7335SNicolas Pitre 
1219be9b7335SNicolas Pitre 	BUG_ON(vmap_initialized);
1220be9b7335SNicolas Pitre 	for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1221be9b7335SNicolas Pitre 		if (tmp->addr >= vm->addr) {
1222be9b7335SNicolas Pitre 			BUG_ON(tmp->addr < vm->addr + vm->size);
1223be9b7335SNicolas Pitre 			break;
1224be9b7335SNicolas Pitre 		} else
1225be9b7335SNicolas Pitre 			BUG_ON(tmp->addr + tmp->size > vm->addr);
1226be9b7335SNicolas Pitre 	}
1227be9b7335SNicolas Pitre 	vm->next = *p;
1228be9b7335SNicolas Pitre 	*p = vm;
1229be9b7335SNicolas Pitre }
1230be9b7335SNicolas Pitre 
1231be9b7335SNicolas Pitre /**
1232f0aa6617STejun Heo  * vm_area_register_early - register vmap area early during boot
1233f0aa6617STejun Heo  * @vm: vm_struct to register
1234c0c0a293STejun Heo  * @align: requested alignment
1235f0aa6617STejun Heo  *
1236f0aa6617STejun Heo  * This function is used to register kernel vm area before
1237f0aa6617STejun Heo  * vmalloc_init() is called.  @vm->size and @vm->flags should contain
1238f0aa6617STejun Heo  * proper values on entry and other fields should be zero.  On return,
1239f0aa6617STejun Heo  * vm->addr contains the allocated address.
1240f0aa6617STejun Heo  *
1241f0aa6617STejun Heo  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1242f0aa6617STejun Heo  */
1243c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1244f0aa6617STejun Heo {
1245f0aa6617STejun Heo 	static size_t vm_init_off __initdata;
1246c0c0a293STejun Heo 	unsigned long addr;
1247f0aa6617STejun Heo 
1248c0c0a293STejun Heo 	addr = ALIGN(VMALLOC_START + vm_init_off, align);
1249c0c0a293STejun Heo 	vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1250c0c0a293STejun Heo 
1251c0c0a293STejun Heo 	vm->addr = (void *)addr;
1252f0aa6617STejun Heo 
1253be9b7335SNicolas Pitre 	vm_area_add_early(vm);
1254f0aa6617STejun Heo }
1255f0aa6617STejun Heo 
1256db64fe02SNick Piggin void __init vmalloc_init(void)
1257db64fe02SNick Piggin {
1258822c18f2SIvan Kokshaysky 	struct vmap_area *va;
1259822c18f2SIvan Kokshaysky 	struct vm_struct *tmp;
1260db64fe02SNick Piggin 	int i;
1261db64fe02SNick Piggin 
1262db64fe02SNick Piggin 	for_each_possible_cpu(i) {
1263db64fe02SNick Piggin 		struct vmap_block_queue *vbq;
126432fcfd40SAl Viro 		struct vfree_deferred *p;
1265db64fe02SNick Piggin 
1266db64fe02SNick Piggin 		vbq = &per_cpu(vmap_block_queue, i);
1267db64fe02SNick Piggin 		spin_lock_init(&vbq->lock);
1268db64fe02SNick Piggin 		INIT_LIST_HEAD(&vbq->free);
126932fcfd40SAl Viro 		p = &per_cpu(vfree_deferred, i);
127032fcfd40SAl Viro 		init_llist_head(&p->list);
127132fcfd40SAl Viro 		INIT_WORK(&p->wq, free_work);
1272db64fe02SNick Piggin 	}
12739b463334SJeremy Fitzhardinge 
1274822c18f2SIvan Kokshaysky 	/* Import existing vmlist entries. */
1275822c18f2SIvan Kokshaysky 	for (tmp = vmlist; tmp; tmp = tmp->next) {
127643ebdac4SPekka Enberg 		va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
1277dbda591dSKyongHo 		va->flags = VM_VM_AREA;
1278822c18f2SIvan Kokshaysky 		va->va_start = (unsigned long)tmp->addr;
1279822c18f2SIvan Kokshaysky 		va->va_end = va->va_start + tmp->size;
1280dbda591dSKyongHo 		va->vm = tmp;
1281822c18f2SIvan Kokshaysky 		__insert_vmap_area(va);
1282822c18f2SIvan Kokshaysky 	}
1283ca23e405STejun Heo 
1284ca23e405STejun Heo 	vmap_area_pcpu_hole = VMALLOC_END;
1285ca23e405STejun Heo 
12869b463334SJeremy Fitzhardinge 	vmap_initialized = true;
1287db64fe02SNick Piggin }
1288db64fe02SNick Piggin 
12898fc48985STejun Heo /**
12908fc48985STejun Heo  * map_kernel_range_noflush - map kernel VM area with the specified pages
12918fc48985STejun Heo  * @addr: start of the VM area to map
12928fc48985STejun Heo  * @size: size of the VM area to map
12938fc48985STejun Heo  * @prot: page protection flags to use
12948fc48985STejun Heo  * @pages: pages to map
12958fc48985STejun Heo  *
12968fc48985STejun Heo  * Map PFN_UP(@size) pages at @addr.  The VM area @addr and @size
12978fc48985STejun Heo  * specify should have been allocated using get_vm_area() and its
12988fc48985STejun Heo  * friends.
12998fc48985STejun Heo  *
13008fc48985STejun Heo  * NOTE:
13018fc48985STejun Heo  * This function does NOT do any cache flushing.  The caller is
13028fc48985STejun Heo  * responsible for calling flush_cache_vmap() on to-be-mapped areas
13038fc48985STejun Heo  * before calling this function.
13048fc48985STejun Heo  *
13058fc48985STejun Heo  * RETURNS:
13068fc48985STejun Heo  * The number of pages mapped on success, -errno on failure.
13078fc48985STejun Heo  */
13088fc48985STejun Heo int map_kernel_range_noflush(unsigned long addr, unsigned long size,
13098fc48985STejun Heo 			     pgprot_t prot, struct page **pages)
13108fc48985STejun Heo {
13118fc48985STejun Heo 	return vmap_page_range_noflush(addr, addr + size, prot, pages);
13128fc48985STejun Heo }
13138fc48985STejun Heo 
13148fc48985STejun Heo /**
13158fc48985STejun Heo  * unmap_kernel_range_noflush - unmap kernel VM area
13168fc48985STejun Heo  * @addr: start of the VM area to unmap
13178fc48985STejun Heo  * @size: size of the VM area to unmap
13188fc48985STejun Heo  *
13198fc48985STejun Heo  * Unmap PFN_UP(@size) pages at @addr.  The VM area @addr and @size
13208fc48985STejun Heo  * specify should have been allocated using get_vm_area() and its
13218fc48985STejun Heo  * friends.
13228fc48985STejun Heo  *
13238fc48985STejun Heo  * NOTE:
13248fc48985STejun Heo  * This function does NOT do any cache flushing.  The caller is
13258fc48985STejun Heo  * responsible for calling flush_cache_vunmap() on to-be-mapped areas
13268fc48985STejun Heo  * before calling this function and flush_tlb_kernel_range() after.
13278fc48985STejun Heo  */
13288fc48985STejun Heo void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
13298fc48985STejun Heo {
13308fc48985STejun Heo 	vunmap_page_range(addr, addr + size);
13318fc48985STejun Heo }
133281e88fdcSHuang Ying EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
13338fc48985STejun Heo 
13348fc48985STejun Heo /**
13358fc48985STejun Heo  * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
13368fc48985STejun Heo  * @addr: start of the VM area to unmap
13378fc48985STejun Heo  * @size: size of the VM area to unmap
13388fc48985STejun Heo  *
13398fc48985STejun Heo  * Similar to unmap_kernel_range_noflush() but flushes vcache before
13408fc48985STejun Heo  * the unmapping and tlb after.
13418fc48985STejun Heo  */
1342db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size)
1343db64fe02SNick Piggin {
1344db64fe02SNick Piggin 	unsigned long end = addr + size;
1345f6fcba70STejun Heo 
1346f6fcba70STejun Heo 	flush_cache_vunmap(addr, end);
1347db64fe02SNick Piggin 	vunmap_page_range(addr, end);
1348db64fe02SNick Piggin 	flush_tlb_kernel_range(addr, end);
1349db64fe02SNick Piggin }
135093ef6d6cSMinchan Kim EXPORT_SYMBOL_GPL(unmap_kernel_range);
1351db64fe02SNick Piggin 
1352f6f8ed47SWANG Chao int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages)
1353db64fe02SNick Piggin {
1354db64fe02SNick Piggin 	unsigned long addr = (unsigned long)area->addr;
1355762216abSWanpeng Li 	unsigned long end = addr + get_vm_area_size(area);
1356db64fe02SNick Piggin 	int err;
1357db64fe02SNick Piggin 
1358f6f8ed47SWANG Chao 	err = vmap_page_range(addr, end, prot, pages);
1359db64fe02SNick Piggin 
1360f6f8ed47SWANG Chao 	return err > 0 ? 0 : err;
1361db64fe02SNick Piggin }
1362db64fe02SNick Piggin EXPORT_SYMBOL_GPL(map_vm_area);
1363db64fe02SNick Piggin 
1364f5252e00SMitsuo Hayasaka static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
13655e6cafc8SMarek Szyprowski 			      unsigned long flags, const void *caller)
1366cf88c790STejun Heo {
1367c69480adSJoonsoo Kim 	spin_lock(&vmap_area_lock);
1368cf88c790STejun Heo 	vm->flags = flags;
1369cf88c790STejun Heo 	vm->addr = (void *)va->va_start;
1370cf88c790STejun Heo 	vm->size = va->va_end - va->va_start;
1371cf88c790STejun Heo 	vm->caller = caller;
1372db1aecafSMinchan Kim 	va->vm = vm;
1373cf88c790STejun Heo 	va->flags |= VM_VM_AREA;
1374c69480adSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
1375f5252e00SMitsuo Hayasaka }
1376cf88c790STejun Heo 
137720fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm)
1378f5252e00SMitsuo Hayasaka {
1379d4033afdSJoonsoo Kim 	/*
138020fc02b4SZhang Yanfei 	 * Before removing VM_UNINITIALIZED,
1381d4033afdSJoonsoo Kim 	 * we should make sure that vm has proper values.
1382d4033afdSJoonsoo Kim 	 * Pair with smp_rmb() in show_numa_info().
1383d4033afdSJoonsoo Kim 	 */
1384d4033afdSJoonsoo Kim 	smp_wmb();
138520fc02b4SZhang Yanfei 	vm->flags &= ~VM_UNINITIALIZED;
1386cf88c790STejun Heo }
1387cf88c790STejun Heo 
1388db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size,
13892dca6999SDavid Miller 		unsigned long align, unsigned long flags, unsigned long start,
13905e6cafc8SMarek Szyprowski 		unsigned long end, int node, gfp_t gfp_mask, const void *caller)
1391db64fe02SNick Piggin {
13920006526dSKautuk Consul 	struct vmap_area *va;
1393db64fe02SNick Piggin 	struct vm_struct *area;
13941da177e4SLinus Torvalds 
139552fd24caSGiridhar Pemmasani 	BUG_ON(in_interrupt());
13961da177e4SLinus Torvalds 	size = PAGE_ALIGN(size);
139731be8309SOGAWA Hirofumi 	if (unlikely(!size))
139831be8309SOGAWA Hirofumi 		return NULL;
13991da177e4SLinus Torvalds 
1400252e5c6eSzijun_hu 	if (flags & VM_IOREMAP)
1401252e5c6eSzijun_hu 		align = 1ul << clamp_t(int, get_count_order_long(size),
1402252e5c6eSzijun_hu 				       PAGE_SHIFT, IOREMAP_MAX_ORDER);
1403252e5c6eSzijun_hu 
1404cf88c790STejun Heo 	area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
14051da177e4SLinus Torvalds 	if (unlikely(!area))
14061da177e4SLinus Torvalds 		return NULL;
14071da177e4SLinus Torvalds 
140871394fe5SAndrey Ryabinin 	if (!(flags & VM_NO_GUARD))
14091da177e4SLinus Torvalds 		size += PAGE_SIZE;
14101da177e4SLinus Torvalds 
1411db64fe02SNick Piggin 	va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1412db64fe02SNick Piggin 	if (IS_ERR(va)) {
1413db64fe02SNick Piggin 		kfree(area);
1414db64fe02SNick Piggin 		return NULL;
14151da177e4SLinus Torvalds 	}
14161da177e4SLinus Torvalds 
1417f5252e00SMitsuo Hayasaka 	setup_vmalloc_vm(area, va, flags, caller);
1418f5252e00SMitsuo Hayasaka 
14191da177e4SLinus Torvalds 	return area;
14201da177e4SLinus Torvalds }
14211da177e4SLinus Torvalds 
1422930fc45aSChristoph Lameter struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1423930fc45aSChristoph Lameter 				unsigned long start, unsigned long end)
1424930fc45aSChristoph Lameter {
142500ef2d2fSDavid Rientjes 	return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
142600ef2d2fSDavid Rientjes 				  GFP_KERNEL, __builtin_return_address(0));
1427930fc45aSChristoph Lameter }
14285992b6daSRusty Russell EXPORT_SYMBOL_GPL(__get_vm_area);
1429930fc45aSChristoph Lameter 
1430c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1431c2968612SBenjamin Herrenschmidt 				       unsigned long start, unsigned long end,
14325e6cafc8SMarek Szyprowski 				       const void *caller)
1433c2968612SBenjamin Herrenschmidt {
143400ef2d2fSDavid Rientjes 	return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
143500ef2d2fSDavid Rientjes 				  GFP_KERNEL, caller);
1436c2968612SBenjamin Herrenschmidt }
1437c2968612SBenjamin Herrenschmidt 
14381da177e4SLinus Torvalds /**
1439183ff22bSSimon Arlott  *	get_vm_area  -  reserve a contiguous kernel virtual area
14401da177e4SLinus Torvalds  *	@size:		size of the area
14411da177e4SLinus Torvalds  *	@flags:		%VM_IOREMAP for I/O mappings or VM_ALLOC
14421da177e4SLinus Torvalds  *
14431da177e4SLinus Torvalds  *	Search an area of @size in the kernel virtual mapping area,
14441da177e4SLinus Torvalds  *	and reserved it for out purposes.  Returns the area descriptor
14451da177e4SLinus Torvalds  *	on success or %NULL on failure.
14461da177e4SLinus Torvalds  */
14471da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
14481da177e4SLinus Torvalds {
14492dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
145000ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL,
145100ef2d2fSDavid Rientjes 				  __builtin_return_address(0));
145223016969SChristoph Lameter }
145323016969SChristoph Lameter 
145423016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
14555e6cafc8SMarek Szyprowski 				const void *caller)
145623016969SChristoph Lameter {
14572dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
145800ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL, caller);
14591da177e4SLinus Torvalds }
14601da177e4SLinus Torvalds 
1461e9da6e99SMarek Szyprowski /**
1462e9da6e99SMarek Szyprowski  *	find_vm_area  -  find a continuous kernel virtual area
1463e9da6e99SMarek Szyprowski  *	@addr:		base address
1464e9da6e99SMarek Szyprowski  *
1465e9da6e99SMarek Szyprowski  *	Search for the kernel VM area starting at @addr, and return it.
1466e9da6e99SMarek Szyprowski  *	It is up to the caller to do all required locking to keep the returned
1467e9da6e99SMarek Szyprowski  *	pointer valid.
1468e9da6e99SMarek Szyprowski  */
1469e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr)
147083342314SNick Piggin {
1471db64fe02SNick Piggin 	struct vmap_area *va;
147283342314SNick Piggin 
1473db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
1474db64fe02SNick Piggin 	if (va && va->flags & VM_VM_AREA)
1475db1aecafSMinchan Kim 		return va->vm;
147683342314SNick Piggin 
14777856dfebSAndi Kleen 	return NULL;
14787856dfebSAndi Kleen }
14797856dfebSAndi Kleen 
14801da177e4SLinus Torvalds /**
1481183ff22bSSimon Arlott  *	remove_vm_area  -  find and remove a continuous kernel virtual area
14821da177e4SLinus Torvalds  *	@addr:		base address
14831da177e4SLinus Torvalds  *
14841da177e4SLinus Torvalds  *	Search for the kernel VM area starting at @addr, and remove it.
14851da177e4SLinus Torvalds  *	This function returns the found VM area, but using it is NOT safe
14867856dfebSAndi Kleen  *	on SMP machines, except for its size or flags.
14871da177e4SLinus Torvalds  */
1488b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr)
14891da177e4SLinus Torvalds {
1490db64fe02SNick Piggin 	struct vmap_area *va;
1491db64fe02SNick Piggin 
14925803ed29SChristoph Hellwig 	might_sleep();
14935803ed29SChristoph Hellwig 
1494db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
1495db64fe02SNick Piggin 	if (va && va->flags & VM_VM_AREA) {
1496db1aecafSMinchan Kim 		struct vm_struct *vm = va->vm;
1497f5252e00SMitsuo Hayasaka 
1498c69480adSJoonsoo Kim 		spin_lock(&vmap_area_lock);
1499c69480adSJoonsoo Kim 		va->vm = NULL;
1500c69480adSJoonsoo Kim 		va->flags &= ~VM_VM_AREA;
1501*78c72746SYisheng Xie 		va->flags |= VM_LAZY_FREE;
1502c69480adSJoonsoo Kim 		spin_unlock(&vmap_area_lock);
1503c69480adSJoonsoo Kim 
1504dd32c279SKAMEZAWA Hiroyuki 		vmap_debug_free_range(va->va_start, va->va_end);
1505a5af5aa8SAndrey Ryabinin 		kasan_free_shadow(vm);
1506dd32c279SKAMEZAWA Hiroyuki 		free_unmap_vmap_area(va);
1507dd32c279SKAMEZAWA Hiroyuki 
1508db64fe02SNick Piggin 		return vm;
1509db64fe02SNick Piggin 	}
1510db64fe02SNick Piggin 	return NULL;
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds 
1513b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages)
15141da177e4SLinus Torvalds {
15151da177e4SLinus Torvalds 	struct vm_struct *area;
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	if (!addr)
15181da177e4SLinus Torvalds 		return;
15191da177e4SLinus Torvalds 
1520e69e9d4aSHATAYAMA Daisuke 	if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
1521ab15d9b4SDan Carpenter 			addr))
15221da177e4SLinus Torvalds 		return;
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 	area = remove_vm_area(addr);
15251da177e4SLinus Torvalds 	if (unlikely(!area)) {
15264c8573e2SArjan van de Ven 		WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
15271da177e4SLinus Torvalds 				addr);
15281da177e4SLinus Torvalds 		return;
15291da177e4SLinus Torvalds 	}
15301da177e4SLinus Torvalds 
15317511c3edSJerome Marchand 	debug_check_no_locks_freed(addr, get_vm_area_size(area));
15327511c3edSJerome Marchand 	debug_check_no_obj_freed(addr, get_vm_area_size(area));
15339a11b49aSIngo Molnar 
15341da177e4SLinus Torvalds 	if (deallocate_pages) {
15351da177e4SLinus Torvalds 		int i;
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 		for (i = 0; i < area->nr_pages; i++) {
1538bf53d6f8SChristoph Lameter 			struct page *page = area->pages[i];
1539bf53d6f8SChristoph Lameter 
1540bf53d6f8SChristoph Lameter 			BUG_ON(!page);
15414949148aSVladimir Davydov 			__free_pages(page, 0);
15421da177e4SLinus Torvalds 		}
15431da177e4SLinus Torvalds 
1544244d63eeSDavid Rientjes 		kvfree(area->pages);
15451da177e4SLinus Torvalds 	}
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	kfree(area);
15481da177e4SLinus Torvalds 	return;
15491da177e4SLinus Torvalds }
15501da177e4SLinus Torvalds 
1551bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr)
1552bf22e37aSAndrey Ryabinin {
1553bf22e37aSAndrey Ryabinin 	/*
1554bf22e37aSAndrey Ryabinin 	 * Use raw_cpu_ptr() because this can be called from preemptible
1555bf22e37aSAndrey Ryabinin 	 * context. Preemption is absolutely fine here, because the llist_add()
1556bf22e37aSAndrey Ryabinin 	 * implementation is lockless, so it works even if we are adding to
1557bf22e37aSAndrey Ryabinin 	 * nother cpu's list.  schedule_work() should be fine with this too.
1558bf22e37aSAndrey Ryabinin 	 */
1559bf22e37aSAndrey Ryabinin 	struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
1560bf22e37aSAndrey Ryabinin 
1561bf22e37aSAndrey Ryabinin 	if (llist_add((struct llist_node *)addr, &p->list))
1562bf22e37aSAndrey Ryabinin 		schedule_work(&p->wq);
1563bf22e37aSAndrey Ryabinin }
1564bf22e37aSAndrey Ryabinin 
1565bf22e37aSAndrey Ryabinin /**
1566bf22e37aSAndrey Ryabinin  *	vfree_atomic  -  release memory allocated by vmalloc()
1567bf22e37aSAndrey Ryabinin  *	@addr:		memory base address
1568bf22e37aSAndrey Ryabinin  *
1569bf22e37aSAndrey Ryabinin  *	This one is just like vfree() but can be called in any atomic context
1570bf22e37aSAndrey Ryabinin  *	except NMIs.
1571bf22e37aSAndrey Ryabinin  */
1572bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr)
1573bf22e37aSAndrey Ryabinin {
1574bf22e37aSAndrey Ryabinin 	BUG_ON(in_nmi());
1575bf22e37aSAndrey Ryabinin 
1576bf22e37aSAndrey Ryabinin 	kmemleak_free(addr);
1577bf22e37aSAndrey Ryabinin 
1578bf22e37aSAndrey Ryabinin 	if (!addr)
1579bf22e37aSAndrey Ryabinin 		return;
1580bf22e37aSAndrey Ryabinin 	__vfree_deferred(addr);
1581bf22e37aSAndrey Ryabinin }
1582bf22e37aSAndrey Ryabinin 
15831da177e4SLinus Torvalds /**
15841da177e4SLinus Torvalds  *	vfree  -  release memory allocated by vmalloc()
15851da177e4SLinus Torvalds  *	@addr:		memory base address
15861da177e4SLinus Torvalds  *
1587183ff22bSSimon Arlott  *	Free the virtually continuous memory area starting at @addr, as
158880e93effSPekka Enberg  *	obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
158980e93effSPekka Enberg  *	NULL, no operation is performed.
15901da177e4SLinus Torvalds  *
159132fcfd40SAl Viro  *	Must not be called in NMI context (strictly speaking, only if we don't
159232fcfd40SAl Viro  *	have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
159332fcfd40SAl Viro  *	conventions for vfree() arch-depenedent would be a really bad idea)
159432fcfd40SAl Viro  *
15950e056eb5Smchehab@s-opensource.com  *	NOTE: assumes that the object at @addr has a size >= sizeof(llist_node)
15961da177e4SLinus Torvalds  */
1597b3bdda02SChristoph Lameter void vfree(const void *addr)
15981da177e4SLinus Torvalds {
159932fcfd40SAl Viro 	BUG_ON(in_nmi());
160089219d37SCatalin Marinas 
160189219d37SCatalin Marinas 	kmemleak_free(addr);
160289219d37SCatalin Marinas 
160332fcfd40SAl Viro 	if (!addr)
160432fcfd40SAl Viro 		return;
1605bf22e37aSAndrey Ryabinin 	if (unlikely(in_interrupt()))
1606bf22e37aSAndrey Ryabinin 		__vfree_deferred(addr);
1607bf22e37aSAndrey Ryabinin 	else
16081da177e4SLinus Torvalds 		__vunmap(addr, 1);
16091da177e4SLinus Torvalds }
16101da177e4SLinus Torvalds EXPORT_SYMBOL(vfree);
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds /**
16131da177e4SLinus Torvalds  *	vunmap  -  release virtual mapping obtained by vmap()
16141da177e4SLinus Torvalds  *	@addr:		memory base address
16151da177e4SLinus Torvalds  *
16161da177e4SLinus Torvalds  *	Free the virtually contiguous memory area starting at @addr,
16171da177e4SLinus Torvalds  *	which was created from the page array passed to vmap().
16181da177e4SLinus Torvalds  *
161980e93effSPekka Enberg  *	Must not be called in interrupt context.
16201da177e4SLinus Torvalds  */
1621b3bdda02SChristoph Lameter void vunmap(const void *addr)
16221da177e4SLinus Torvalds {
16231da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
162434754b69SPeter Zijlstra 	might_sleep();
162532fcfd40SAl Viro 	if (addr)
16261da177e4SLinus Torvalds 		__vunmap(addr, 0);
16271da177e4SLinus Torvalds }
16281da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap);
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds /**
16311da177e4SLinus Torvalds  *	vmap  -  map an array of pages into virtually contiguous space
16321da177e4SLinus Torvalds  *	@pages:		array of page pointers
16331da177e4SLinus Torvalds  *	@count:		number of pages to map
16341da177e4SLinus Torvalds  *	@flags:		vm_area->flags
16351da177e4SLinus Torvalds  *	@prot:		page protection for the mapping
16361da177e4SLinus Torvalds  *
16371da177e4SLinus Torvalds  *	Maps @count pages from @pages into contiguous kernel virtual
16381da177e4SLinus Torvalds  *	space.
16391da177e4SLinus Torvalds  */
16401da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count,
16411da177e4SLinus Torvalds 		unsigned long flags, pgprot_t prot)
16421da177e4SLinus Torvalds {
16431da177e4SLinus Torvalds 	struct vm_struct *area;
164465ee03c4SGuillermo Julián Moreno 	unsigned long size;		/* In bytes */
16451da177e4SLinus Torvalds 
164634754b69SPeter Zijlstra 	might_sleep();
164734754b69SPeter Zijlstra 
16484481374cSJan Beulich 	if (count > totalram_pages)
16491da177e4SLinus Torvalds 		return NULL;
16501da177e4SLinus Torvalds 
165165ee03c4SGuillermo Julián Moreno 	size = (unsigned long)count << PAGE_SHIFT;
165265ee03c4SGuillermo Julián Moreno 	area = get_vm_area_caller(size, flags, __builtin_return_address(0));
16531da177e4SLinus Torvalds 	if (!area)
16541da177e4SLinus Torvalds 		return NULL;
165523016969SChristoph Lameter 
1656f6f8ed47SWANG Chao 	if (map_vm_area(area, prot, pages)) {
16571da177e4SLinus Torvalds 		vunmap(area->addr);
16581da177e4SLinus Torvalds 		return NULL;
16591da177e4SLinus Torvalds 	}
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	return area->addr;
16621da177e4SLinus Torvalds }
16631da177e4SLinus Torvalds EXPORT_SYMBOL(vmap);
16641da177e4SLinus Torvalds 
16658594a21cSMichal Hocko static void *__vmalloc_node(unsigned long size, unsigned long align,
16668594a21cSMichal Hocko 			    gfp_t gfp_mask, pgprot_t prot,
16678594a21cSMichal Hocko 			    int node, const void *caller);
1668e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
16693722e13cSWanpeng Li 				 pgprot_t prot, int node)
16701da177e4SLinus Torvalds {
16711da177e4SLinus Torvalds 	struct page **pages;
16721da177e4SLinus Torvalds 	unsigned int nr_pages, array_size, i;
1673930f036bSDavid Rientjes 	const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
167419809c2dSMichal Hocko 	const gfp_t alloc_mask = gfp_mask | __GFP_HIGHMEM | __GFP_NOWARN;
16751da177e4SLinus Torvalds 
1676762216abSWanpeng Li 	nr_pages = get_vm_area_size(area) >> PAGE_SHIFT;
16771da177e4SLinus Torvalds 	array_size = (nr_pages * sizeof(struct page *));
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	area->nr_pages = nr_pages;
16801da177e4SLinus Torvalds 	/* Please note that the recursion is strictly bounded. */
16818757d5faSJan Kiszka 	if (array_size > PAGE_SIZE) {
1682976d6dfbSJan Beulich 		pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,
16833722e13cSWanpeng Li 				PAGE_KERNEL, node, area->caller);
1684286e1ea3SAndrew Morton 	} else {
1685976d6dfbSJan Beulich 		pages = kmalloc_node(array_size, nested_gfp, node);
1686286e1ea3SAndrew Morton 	}
16871da177e4SLinus Torvalds 	area->pages = pages;
16881da177e4SLinus Torvalds 	if (!area->pages) {
16891da177e4SLinus Torvalds 		remove_vm_area(area->addr);
16901da177e4SLinus Torvalds 		kfree(area);
16911da177e4SLinus Torvalds 		return NULL;
16921da177e4SLinus Torvalds 	}
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	for (i = 0; i < area->nr_pages; i++) {
1695bf53d6f8SChristoph Lameter 		struct page *page;
1696bf53d6f8SChristoph Lameter 
16975d17a73aSMichal Hocko 		if (fatal_signal_pending(current)) {
16985d17a73aSMichal Hocko 			area->nr_pages = i;
1699171012f5SDmitry Vyukov 			goto fail_no_warn;
17005d17a73aSMichal Hocko 		}
17015d17a73aSMichal Hocko 
17024b90951cSJianguo Wu 		if (node == NUMA_NO_NODE)
17037877cdccSMichal Hocko 			page = alloc_page(alloc_mask);
1704930fc45aSChristoph Lameter 		else
17057877cdccSMichal Hocko 			page = alloc_pages_node(node, alloc_mask, 0);
1706bf53d6f8SChristoph Lameter 
1707bf53d6f8SChristoph Lameter 		if (unlikely(!page)) {
17081da177e4SLinus Torvalds 			/* Successfully allocated i pages, free them in __vunmap() */
17091da177e4SLinus Torvalds 			area->nr_pages = i;
17101da177e4SLinus Torvalds 			goto fail;
17111da177e4SLinus Torvalds 		}
1712bf53d6f8SChristoph Lameter 		area->pages[i] = page;
1713d0164adcSMel Gorman 		if (gfpflags_allow_blocking(gfp_mask))
1714660654f9SEric Dumazet 			cond_resched();
17151da177e4SLinus Torvalds 	}
17161da177e4SLinus Torvalds 
1717f6f8ed47SWANG Chao 	if (map_vm_area(area, prot, pages))
17181da177e4SLinus Torvalds 		goto fail;
17191da177e4SLinus Torvalds 	return area->addr;
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds fail:
1722a8e99259SMichal Hocko 	warn_alloc(gfp_mask, NULL,
17237877cdccSMichal Hocko 			  "vmalloc: allocation failure, allocated %ld of %ld bytes",
172422943ab1SDave Hansen 			  (area->nr_pages*PAGE_SIZE), area->size);
1725171012f5SDmitry Vyukov fail_no_warn:
17261da177e4SLinus Torvalds 	vfree(area->addr);
17271da177e4SLinus Torvalds 	return NULL;
17281da177e4SLinus Torvalds }
17291da177e4SLinus Torvalds 
1730d0a21265SDavid Rientjes /**
1731d0a21265SDavid Rientjes  *	__vmalloc_node_range  -  allocate virtually contiguous memory
1732d0a21265SDavid Rientjes  *	@size:		allocation size
1733d0a21265SDavid Rientjes  *	@align:		desired alignment
1734d0a21265SDavid Rientjes  *	@start:		vm area range start
1735d0a21265SDavid Rientjes  *	@end:		vm area range end
1736d0a21265SDavid Rientjes  *	@gfp_mask:	flags for the page level allocator
1737d0a21265SDavid Rientjes  *	@prot:		protection mask for the allocated pages
1738cb9e3c29SAndrey Ryabinin  *	@vm_flags:	additional vm area flags (e.g. %VM_NO_GUARD)
173900ef2d2fSDavid Rientjes  *	@node:		node to use for allocation or NUMA_NO_NODE
1740d0a21265SDavid Rientjes  *	@caller:	caller's return address
1741d0a21265SDavid Rientjes  *
1742d0a21265SDavid Rientjes  *	Allocate enough pages to cover @size from the page level
1743d0a21265SDavid Rientjes  *	allocator with @gfp_mask flags.  Map them into contiguous
1744d0a21265SDavid Rientjes  *	kernel virtual space, using a pagetable protection of @prot.
1745d0a21265SDavid Rientjes  */
1746d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align,
1747d0a21265SDavid Rientjes 			unsigned long start, unsigned long end, gfp_t gfp_mask,
1748cb9e3c29SAndrey Ryabinin 			pgprot_t prot, unsigned long vm_flags, int node,
1749cb9e3c29SAndrey Ryabinin 			const void *caller)
1750930fc45aSChristoph Lameter {
1751d0a21265SDavid Rientjes 	struct vm_struct *area;
1752d0a21265SDavid Rientjes 	void *addr;
1753d0a21265SDavid Rientjes 	unsigned long real_size = size;
1754d0a21265SDavid Rientjes 
1755d0a21265SDavid Rientjes 	size = PAGE_ALIGN(size);
1756d0a21265SDavid Rientjes 	if (!size || (size >> PAGE_SHIFT) > totalram_pages)
1757de7d2b56SJoe Perches 		goto fail;
1758d0a21265SDavid Rientjes 
1759cb9e3c29SAndrey Ryabinin 	area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED |
1760cb9e3c29SAndrey Ryabinin 				vm_flags, start, end, node, gfp_mask, caller);
1761d0a21265SDavid Rientjes 	if (!area)
1762de7d2b56SJoe Perches 		goto fail;
1763d0a21265SDavid Rientjes 
17643722e13cSWanpeng Li 	addr = __vmalloc_area_node(area, gfp_mask, prot, node);
17651368edf0SMel Gorman 	if (!addr)
1766b82225f3SWanpeng Li 		return NULL;
176789219d37SCatalin Marinas 
176889219d37SCatalin Marinas 	/*
176920fc02b4SZhang Yanfei 	 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
177020fc02b4SZhang Yanfei 	 * flag. It means that vm_struct is not fully initialized.
17714341fa45SJoonsoo Kim 	 * Now, it is fully initialized, so remove this flag here.
1772f5252e00SMitsuo Hayasaka 	 */
177320fc02b4SZhang Yanfei 	clear_vm_uninitialized_flag(area);
1774f5252e00SMitsuo Hayasaka 
177594f4a161SCatalin Marinas 	kmemleak_vmalloc(area, size, gfp_mask);
177689219d37SCatalin Marinas 
177789219d37SCatalin Marinas 	return addr;
1778de7d2b56SJoe Perches 
1779de7d2b56SJoe Perches fail:
1780a8e99259SMichal Hocko 	warn_alloc(gfp_mask, NULL,
17817877cdccSMichal Hocko 			  "vmalloc: allocation failure: %lu bytes", real_size);
1782de7d2b56SJoe Perches 	return NULL;
1783930fc45aSChristoph Lameter }
1784930fc45aSChristoph Lameter 
17851da177e4SLinus Torvalds /**
1786930fc45aSChristoph Lameter  *	__vmalloc_node  -  allocate virtually contiguous memory
17871da177e4SLinus Torvalds  *	@size:		allocation size
17882dca6999SDavid Miller  *	@align:		desired alignment
17891da177e4SLinus Torvalds  *	@gfp_mask:	flags for the page level allocator
17901da177e4SLinus Torvalds  *	@prot:		protection mask for the allocated pages
179100ef2d2fSDavid Rientjes  *	@node:		node to use for allocation or NUMA_NO_NODE
1792c85d194bSRandy Dunlap  *	@caller:	caller's return address
17931da177e4SLinus Torvalds  *
17941da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
17951da177e4SLinus Torvalds  *	allocator with @gfp_mask flags.  Map them into contiguous
17961da177e4SLinus Torvalds  *	kernel virtual space, using a pagetable protection of @prot.
1797a7c3e901SMichal Hocko  *
1798a7c3e901SMichal Hocko  *	Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_REPEAT
1799a7c3e901SMichal Hocko  *	and __GFP_NOFAIL are not supported
1800a7c3e901SMichal Hocko  *
1801a7c3e901SMichal Hocko  *	Any use of gfp flags outside of GFP_KERNEL should be consulted
1802a7c3e901SMichal Hocko  *	with mm people.
1803a7c3e901SMichal Hocko  *
18041da177e4SLinus Torvalds  */
18058594a21cSMichal Hocko static void *__vmalloc_node(unsigned long size, unsigned long align,
18062dca6999SDavid Miller 			    gfp_t gfp_mask, pgprot_t prot,
18075e6cafc8SMarek Szyprowski 			    int node, const void *caller)
18081da177e4SLinus Torvalds {
1809d0a21265SDavid Rientjes 	return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
1810cb9e3c29SAndrey Ryabinin 				gfp_mask, prot, 0, node, caller);
18111da177e4SLinus Torvalds }
18121da177e4SLinus Torvalds 
1813930fc45aSChristoph Lameter void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1814930fc45aSChristoph Lameter {
181500ef2d2fSDavid Rientjes 	return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE,
181623016969SChristoph Lameter 				__builtin_return_address(0));
1817930fc45aSChristoph Lameter }
18181da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc);
18191da177e4SLinus Torvalds 
18208594a21cSMichal Hocko static inline void *__vmalloc_node_flags(unsigned long size,
18218594a21cSMichal Hocko 					int node, gfp_t flags)
18228594a21cSMichal Hocko {
18238594a21cSMichal Hocko 	return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
18248594a21cSMichal Hocko 					node, __builtin_return_address(0));
18258594a21cSMichal Hocko }
18268594a21cSMichal Hocko 
18278594a21cSMichal Hocko 
18288594a21cSMichal Hocko void *__vmalloc_node_flags_caller(unsigned long size, int node, gfp_t flags,
18298594a21cSMichal Hocko 				  void *caller)
18308594a21cSMichal Hocko {
18318594a21cSMichal Hocko 	return __vmalloc_node(size, 1, flags, PAGE_KERNEL, node, caller);
18328594a21cSMichal Hocko }
18338594a21cSMichal Hocko 
18341da177e4SLinus Torvalds /**
18351da177e4SLinus Torvalds  *	vmalloc  -  allocate virtually contiguous memory
18361da177e4SLinus Torvalds  *	@size:		allocation size
18371da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
18381da177e4SLinus Torvalds  *	allocator and map them into contiguous kernel virtual space.
18391da177e4SLinus Torvalds  *
1840c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
18411da177e4SLinus Torvalds  *	use __vmalloc() instead.
18421da177e4SLinus Torvalds  */
18431da177e4SLinus Torvalds void *vmalloc(unsigned long size)
18441da177e4SLinus Torvalds {
184500ef2d2fSDavid Rientjes 	return __vmalloc_node_flags(size, NUMA_NO_NODE,
184619809c2dSMichal Hocko 				    GFP_KERNEL);
18471da177e4SLinus Torvalds }
18481da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc);
18491da177e4SLinus Torvalds 
1850930fc45aSChristoph Lameter /**
1851e1ca7788SDave Young  *	vzalloc - allocate virtually contiguous memory with zero fill
1852e1ca7788SDave Young  *	@size:	allocation size
1853e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
1854e1ca7788SDave Young  *	allocator and map them into contiguous kernel virtual space.
1855e1ca7788SDave Young  *	The memory allocated is set to zero.
1856e1ca7788SDave Young  *
1857e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
1858e1ca7788SDave Young  *	use __vmalloc() instead.
1859e1ca7788SDave Young  */
1860e1ca7788SDave Young void *vzalloc(unsigned long size)
1861e1ca7788SDave Young {
186200ef2d2fSDavid Rientjes 	return __vmalloc_node_flags(size, NUMA_NO_NODE,
186319809c2dSMichal Hocko 				GFP_KERNEL | __GFP_ZERO);
1864e1ca7788SDave Young }
1865e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
1866e1ca7788SDave Young 
1867e1ca7788SDave Young /**
1868ead04089SRolf Eike Beer  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
186983342314SNick Piggin  * @size: allocation size
1870ead04089SRolf Eike Beer  *
1871ead04089SRolf Eike Beer  * The resulting memory area is zeroed so it can be mapped to userspace
1872ead04089SRolf Eike Beer  * without leaking data.
187383342314SNick Piggin  */
187483342314SNick Piggin void *vmalloc_user(unsigned long size)
187583342314SNick Piggin {
187683342314SNick Piggin 	struct vm_struct *area;
187783342314SNick Piggin 	void *ret;
187883342314SNick Piggin 
18792dca6999SDavid Miller 	ret = __vmalloc_node(size, SHMLBA,
188019809c2dSMichal Hocko 			     GFP_KERNEL | __GFP_ZERO,
188100ef2d2fSDavid Rientjes 			     PAGE_KERNEL, NUMA_NO_NODE,
188200ef2d2fSDavid Rientjes 			     __builtin_return_address(0));
18832b4ac44eSEric Dumazet 	if (ret) {
1884db64fe02SNick Piggin 		area = find_vm_area(ret);
188583342314SNick Piggin 		area->flags |= VM_USERMAP;
18862b4ac44eSEric Dumazet 	}
188783342314SNick Piggin 	return ret;
188883342314SNick Piggin }
188983342314SNick Piggin EXPORT_SYMBOL(vmalloc_user);
189083342314SNick Piggin 
189183342314SNick Piggin /**
1892930fc45aSChristoph Lameter  *	vmalloc_node  -  allocate memory on a specific node
1893930fc45aSChristoph Lameter  *	@size:		allocation size
1894d44e0780SRandy Dunlap  *	@node:		numa node
1895930fc45aSChristoph Lameter  *
1896930fc45aSChristoph Lameter  *	Allocate enough pages to cover @size from the page level
1897930fc45aSChristoph Lameter  *	allocator and map them into contiguous kernel virtual space.
1898930fc45aSChristoph Lameter  *
1899c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
1900930fc45aSChristoph Lameter  *	use __vmalloc() instead.
1901930fc45aSChristoph Lameter  */
1902930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node)
1903930fc45aSChristoph Lameter {
190419809c2dSMichal Hocko 	return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL,
190523016969SChristoph Lameter 					node, __builtin_return_address(0));
1906930fc45aSChristoph Lameter }
1907930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node);
1908930fc45aSChristoph Lameter 
1909e1ca7788SDave Young /**
1910e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
1911e1ca7788SDave Young  * @size:	allocation size
1912e1ca7788SDave Young  * @node:	numa node
1913e1ca7788SDave Young  *
1914e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
1915e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
1916e1ca7788SDave Young  * The memory allocated is set to zero.
1917e1ca7788SDave Young  *
1918e1ca7788SDave Young  * For tight control over page level allocator and protection flags
1919e1ca7788SDave Young  * use __vmalloc_node() instead.
1920e1ca7788SDave Young  */
1921e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
1922e1ca7788SDave Young {
1923e1ca7788SDave Young 	return __vmalloc_node_flags(size, node,
192419809c2dSMichal Hocko 			 GFP_KERNEL | __GFP_ZERO);
1925e1ca7788SDave Young }
1926e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
1927e1ca7788SDave Young 
19284dc3b16bSPavel Pisa #ifndef PAGE_KERNEL_EXEC
19294dc3b16bSPavel Pisa # define PAGE_KERNEL_EXEC PAGE_KERNEL
19304dc3b16bSPavel Pisa #endif
19314dc3b16bSPavel Pisa 
19321da177e4SLinus Torvalds /**
19331da177e4SLinus Torvalds  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
19341da177e4SLinus Torvalds  *	@size:		allocation size
19351da177e4SLinus Torvalds  *
19361da177e4SLinus Torvalds  *	Kernel-internal function to allocate enough pages to cover @size
19371da177e4SLinus Torvalds  *	the page level allocator and map them into contiguous and
19381da177e4SLinus Torvalds  *	executable kernel virtual space.
19391da177e4SLinus Torvalds  *
1940c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
19411da177e4SLinus Torvalds  *	use __vmalloc() instead.
19421da177e4SLinus Torvalds  */
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds void *vmalloc_exec(unsigned long size)
19451da177e4SLinus Torvalds {
194619809c2dSMichal Hocko 	return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL_EXEC,
194700ef2d2fSDavid Rientjes 			      NUMA_NO_NODE, __builtin_return_address(0));
19481da177e4SLinus Torvalds }
19491da177e4SLinus Torvalds 
19500d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
19517ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
19520d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
19537ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
19540d08e0d3SAndi Kleen #else
19550d08e0d3SAndi Kleen #define GFP_VMALLOC32 GFP_KERNEL
19560d08e0d3SAndi Kleen #endif
19570d08e0d3SAndi Kleen 
19581da177e4SLinus Torvalds /**
19591da177e4SLinus Torvalds  *	vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
19601da177e4SLinus Torvalds  *	@size:		allocation size
19611da177e4SLinus Torvalds  *
19621da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
19631da177e4SLinus Torvalds  *	page level allocator and map them into contiguous kernel virtual space.
19641da177e4SLinus Torvalds  */
19651da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
19661da177e4SLinus Torvalds {
19672dca6999SDavid Miller 	return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
196800ef2d2fSDavid Rientjes 			      NUMA_NO_NODE, __builtin_return_address(0));
19691da177e4SLinus Torvalds }
19701da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32);
19711da177e4SLinus Torvalds 
197283342314SNick Piggin /**
1973ead04089SRolf Eike Beer  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
197483342314SNick Piggin  *	@size:		allocation size
1975ead04089SRolf Eike Beer  *
1976ead04089SRolf Eike Beer  * The resulting memory area is 32bit addressable and zeroed so it can be
1977ead04089SRolf Eike Beer  * mapped to userspace without leaking data.
197883342314SNick Piggin  */
197983342314SNick Piggin void *vmalloc_32_user(unsigned long size)
198083342314SNick Piggin {
198183342314SNick Piggin 	struct vm_struct *area;
198283342314SNick Piggin 	void *ret;
198383342314SNick Piggin 
19842dca6999SDavid Miller 	ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
198500ef2d2fSDavid Rientjes 			     NUMA_NO_NODE, __builtin_return_address(0));
19862b4ac44eSEric Dumazet 	if (ret) {
1987db64fe02SNick Piggin 		area = find_vm_area(ret);
198883342314SNick Piggin 		area->flags |= VM_USERMAP;
19892b4ac44eSEric Dumazet 	}
199083342314SNick Piggin 	return ret;
199183342314SNick Piggin }
199283342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user);
199383342314SNick Piggin 
1994d0107eb0SKAMEZAWA Hiroyuki /*
1995d0107eb0SKAMEZAWA Hiroyuki  * small helper routine , copy contents to buf from addr.
1996d0107eb0SKAMEZAWA Hiroyuki  * If the page is not present, fill zero.
1997d0107eb0SKAMEZAWA Hiroyuki  */
1998d0107eb0SKAMEZAWA Hiroyuki 
1999d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count)
2000d0107eb0SKAMEZAWA Hiroyuki {
2001d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
2002d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
2003d0107eb0SKAMEZAWA Hiroyuki 
2004d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
2005d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
2006d0107eb0SKAMEZAWA Hiroyuki 
2007891c49abSAlexander Kuleshov 		offset = offset_in_page(addr);
2008d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
2009d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
2010d0107eb0SKAMEZAWA Hiroyuki 			length = count;
2011d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
2012d0107eb0SKAMEZAWA Hiroyuki 		/*
2013d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
2014d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
2015d0107eb0SKAMEZAWA Hiroyuki 		 * overhead of vmalloc()/vfree() calles for this _debug_
2016d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
2017d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
2018d0107eb0SKAMEZAWA Hiroyuki 		 */
2019d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
2020d0107eb0SKAMEZAWA Hiroyuki 			/*
2021d0107eb0SKAMEZAWA Hiroyuki 			 * we can expect USER0 is not used (see vread/vwrite's
2022d0107eb0SKAMEZAWA Hiroyuki 			 * function description)
2023d0107eb0SKAMEZAWA Hiroyuki 			 */
20249b04c5feSCong Wang 			void *map = kmap_atomic(p);
2025d0107eb0SKAMEZAWA Hiroyuki 			memcpy(buf, map + offset, length);
20269b04c5feSCong Wang 			kunmap_atomic(map);
2027d0107eb0SKAMEZAWA Hiroyuki 		} else
2028d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, length);
2029d0107eb0SKAMEZAWA Hiroyuki 
2030d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
2031d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
2032d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
2033d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
2034d0107eb0SKAMEZAWA Hiroyuki 	}
2035d0107eb0SKAMEZAWA Hiroyuki 	return copied;
2036d0107eb0SKAMEZAWA Hiroyuki }
2037d0107eb0SKAMEZAWA Hiroyuki 
2038d0107eb0SKAMEZAWA Hiroyuki static int aligned_vwrite(char *buf, char *addr, unsigned long count)
2039d0107eb0SKAMEZAWA Hiroyuki {
2040d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
2041d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
2042d0107eb0SKAMEZAWA Hiroyuki 
2043d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
2044d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
2045d0107eb0SKAMEZAWA Hiroyuki 
2046891c49abSAlexander Kuleshov 		offset = offset_in_page(addr);
2047d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
2048d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
2049d0107eb0SKAMEZAWA Hiroyuki 			length = count;
2050d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
2051d0107eb0SKAMEZAWA Hiroyuki 		/*
2052d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
2053d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
2054d0107eb0SKAMEZAWA Hiroyuki 		 * overhead of vmalloc()/vfree() calles for this _debug_
2055d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
2056d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
2057d0107eb0SKAMEZAWA Hiroyuki 		 */
2058d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
2059d0107eb0SKAMEZAWA Hiroyuki 			/*
2060d0107eb0SKAMEZAWA Hiroyuki 			 * we can expect USER0 is not used (see vread/vwrite's
2061d0107eb0SKAMEZAWA Hiroyuki 			 * function description)
2062d0107eb0SKAMEZAWA Hiroyuki 			 */
20639b04c5feSCong Wang 			void *map = kmap_atomic(p);
2064d0107eb0SKAMEZAWA Hiroyuki 			memcpy(map + offset, buf, length);
20659b04c5feSCong Wang 			kunmap_atomic(map);
2066d0107eb0SKAMEZAWA Hiroyuki 		}
2067d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
2068d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
2069d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
2070d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
2071d0107eb0SKAMEZAWA Hiroyuki 	}
2072d0107eb0SKAMEZAWA Hiroyuki 	return copied;
2073d0107eb0SKAMEZAWA Hiroyuki }
2074d0107eb0SKAMEZAWA Hiroyuki 
2075d0107eb0SKAMEZAWA Hiroyuki /**
2076d0107eb0SKAMEZAWA Hiroyuki  *	vread() -  read vmalloc area in a safe way.
2077d0107eb0SKAMEZAWA Hiroyuki  *	@buf:		buffer for reading data
2078d0107eb0SKAMEZAWA Hiroyuki  *	@addr:		vm address.
2079d0107eb0SKAMEZAWA Hiroyuki  *	@count:		number of bytes to be read.
2080d0107eb0SKAMEZAWA Hiroyuki  *
2081d0107eb0SKAMEZAWA Hiroyuki  *	Returns # of bytes which addr and buf should be increased.
2082d0107eb0SKAMEZAWA Hiroyuki  *	(same number to @count). Returns 0 if [addr...addr+count) doesn't
2083d0107eb0SKAMEZAWA Hiroyuki  *	includes any intersect with alive vmalloc area.
2084d0107eb0SKAMEZAWA Hiroyuki  *
2085d0107eb0SKAMEZAWA Hiroyuki  *	This function checks that addr is a valid vmalloc'ed area, and
2086d0107eb0SKAMEZAWA Hiroyuki  *	copy data from that area to a given buffer. If the given memory range
2087d0107eb0SKAMEZAWA Hiroyuki  *	of [addr...addr+count) includes some valid address, data is copied to
2088d0107eb0SKAMEZAWA Hiroyuki  *	proper area of @buf. If there are memory holes, they'll be zero-filled.
2089d0107eb0SKAMEZAWA Hiroyuki  *	IOREMAP area is treated as memory hole and no copy is done.
2090d0107eb0SKAMEZAWA Hiroyuki  *
2091d0107eb0SKAMEZAWA Hiroyuki  *	If [addr...addr+count) doesn't includes any intersects with alive
2092a8e5202dSCong Wang  *	vm_struct area, returns 0. @buf should be kernel's buffer.
2093d0107eb0SKAMEZAWA Hiroyuki  *
2094d0107eb0SKAMEZAWA Hiroyuki  *	Note: In usual ops, vread() is never necessary because the caller
2095d0107eb0SKAMEZAWA Hiroyuki  *	should know vmalloc() area is valid and can use memcpy().
2096d0107eb0SKAMEZAWA Hiroyuki  *	This is for routines which have to access vmalloc area without
2097d0107eb0SKAMEZAWA Hiroyuki  *	any informaion, as /dev/kmem.
2098d0107eb0SKAMEZAWA Hiroyuki  *
2099d0107eb0SKAMEZAWA Hiroyuki  */
2100d0107eb0SKAMEZAWA Hiroyuki 
21011da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
21021da177e4SLinus Torvalds {
2103e81ce85fSJoonsoo Kim 	struct vmap_area *va;
2104e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
21051da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
2106d0107eb0SKAMEZAWA Hiroyuki 	unsigned long buflen = count;
21071da177e4SLinus Torvalds 	unsigned long n;
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 	/* Don't allow overflow */
21101da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
21111da177e4SLinus Torvalds 		count = -(unsigned long) addr;
21121da177e4SLinus Torvalds 
2113e81ce85fSJoonsoo Kim 	spin_lock(&vmap_area_lock);
2114e81ce85fSJoonsoo Kim 	list_for_each_entry(va, &vmap_area_list, list) {
2115e81ce85fSJoonsoo Kim 		if (!count)
2116e81ce85fSJoonsoo Kim 			break;
2117e81ce85fSJoonsoo Kim 
2118e81ce85fSJoonsoo Kim 		if (!(va->flags & VM_VM_AREA))
2119e81ce85fSJoonsoo Kim 			continue;
2120e81ce85fSJoonsoo Kim 
2121e81ce85fSJoonsoo Kim 		vm = va->vm;
2122e81ce85fSJoonsoo Kim 		vaddr = (char *) vm->addr;
2123762216abSWanpeng Li 		if (addr >= vaddr + get_vm_area_size(vm))
21241da177e4SLinus Torvalds 			continue;
21251da177e4SLinus Torvalds 		while (addr < vaddr) {
21261da177e4SLinus Torvalds 			if (count == 0)
21271da177e4SLinus Torvalds 				goto finished;
21281da177e4SLinus Torvalds 			*buf = '\0';
21291da177e4SLinus Torvalds 			buf++;
21301da177e4SLinus Torvalds 			addr++;
21311da177e4SLinus Torvalds 			count--;
21321da177e4SLinus Torvalds 		}
2133762216abSWanpeng Li 		n = vaddr + get_vm_area_size(vm) - addr;
2134d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
2135d0107eb0SKAMEZAWA Hiroyuki 			n = count;
2136e81ce85fSJoonsoo Kim 		if (!(vm->flags & VM_IOREMAP))
2137d0107eb0SKAMEZAWA Hiroyuki 			aligned_vread(buf, addr, n);
2138d0107eb0SKAMEZAWA Hiroyuki 		else /* IOREMAP area is treated as memory hole */
2139d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, n);
2140d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
2141d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
2142d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
21431da177e4SLinus Torvalds 	}
21441da177e4SLinus Torvalds finished:
2145e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2146d0107eb0SKAMEZAWA Hiroyuki 
2147d0107eb0SKAMEZAWA Hiroyuki 	if (buf == buf_start)
2148d0107eb0SKAMEZAWA Hiroyuki 		return 0;
2149d0107eb0SKAMEZAWA Hiroyuki 	/* zero-fill memory holes */
2150d0107eb0SKAMEZAWA Hiroyuki 	if (buf != buf_start + buflen)
2151d0107eb0SKAMEZAWA Hiroyuki 		memset(buf, 0, buflen - (buf - buf_start));
2152d0107eb0SKAMEZAWA Hiroyuki 
2153d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
21541da177e4SLinus Torvalds }
21551da177e4SLinus Torvalds 
2156d0107eb0SKAMEZAWA Hiroyuki /**
2157d0107eb0SKAMEZAWA Hiroyuki  *	vwrite() -  write vmalloc area in a safe way.
2158d0107eb0SKAMEZAWA Hiroyuki  *	@buf:		buffer for source data
2159d0107eb0SKAMEZAWA Hiroyuki  *	@addr:		vm address.
2160d0107eb0SKAMEZAWA Hiroyuki  *	@count:		number of bytes to be read.
2161d0107eb0SKAMEZAWA Hiroyuki  *
2162d0107eb0SKAMEZAWA Hiroyuki  *	Returns # of bytes which addr and buf should be incresed.
2163d0107eb0SKAMEZAWA Hiroyuki  *	(same number to @count).
2164d0107eb0SKAMEZAWA Hiroyuki  *	If [addr...addr+count) doesn't includes any intersect with valid
2165d0107eb0SKAMEZAWA Hiroyuki  *	vmalloc area, returns 0.
2166d0107eb0SKAMEZAWA Hiroyuki  *
2167d0107eb0SKAMEZAWA Hiroyuki  *	This function checks that addr is a valid vmalloc'ed area, and
2168d0107eb0SKAMEZAWA Hiroyuki  *	copy data from a buffer to the given addr. If specified range of
2169d0107eb0SKAMEZAWA Hiroyuki  *	[addr...addr+count) includes some valid address, data is copied from
2170d0107eb0SKAMEZAWA Hiroyuki  *	proper area of @buf. If there are memory holes, no copy to hole.
2171d0107eb0SKAMEZAWA Hiroyuki  *	IOREMAP area is treated as memory hole and no copy is done.
2172d0107eb0SKAMEZAWA Hiroyuki  *
2173d0107eb0SKAMEZAWA Hiroyuki  *	If [addr...addr+count) doesn't includes any intersects with alive
2174a8e5202dSCong Wang  *	vm_struct area, returns 0. @buf should be kernel's buffer.
2175d0107eb0SKAMEZAWA Hiroyuki  *
2176d0107eb0SKAMEZAWA Hiroyuki  *	Note: In usual ops, vwrite() is never necessary because the caller
2177d0107eb0SKAMEZAWA Hiroyuki  *	should know vmalloc() area is valid and can use memcpy().
2178d0107eb0SKAMEZAWA Hiroyuki  *	This is for routines which have to access vmalloc area without
2179d0107eb0SKAMEZAWA Hiroyuki  *	any informaion, as /dev/kmem.
2180d0107eb0SKAMEZAWA Hiroyuki  */
2181d0107eb0SKAMEZAWA Hiroyuki 
21821da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
21831da177e4SLinus Torvalds {
2184e81ce85fSJoonsoo Kim 	struct vmap_area *va;
2185e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
2186d0107eb0SKAMEZAWA Hiroyuki 	char *vaddr;
2187d0107eb0SKAMEZAWA Hiroyuki 	unsigned long n, buflen;
2188d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
21891da177e4SLinus Torvalds 
21901da177e4SLinus Torvalds 	/* Don't allow overflow */
21911da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
21921da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2193d0107eb0SKAMEZAWA Hiroyuki 	buflen = count;
21941da177e4SLinus Torvalds 
2195e81ce85fSJoonsoo Kim 	spin_lock(&vmap_area_lock);
2196e81ce85fSJoonsoo Kim 	list_for_each_entry(va, &vmap_area_list, list) {
2197e81ce85fSJoonsoo Kim 		if (!count)
2198e81ce85fSJoonsoo Kim 			break;
2199e81ce85fSJoonsoo Kim 
2200e81ce85fSJoonsoo Kim 		if (!(va->flags & VM_VM_AREA))
2201e81ce85fSJoonsoo Kim 			continue;
2202e81ce85fSJoonsoo Kim 
2203e81ce85fSJoonsoo Kim 		vm = va->vm;
2204e81ce85fSJoonsoo Kim 		vaddr = (char *) vm->addr;
2205762216abSWanpeng Li 		if (addr >= vaddr + get_vm_area_size(vm))
22061da177e4SLinus Torvalds 			continue;
22071da177e4SLinus Torvalds 		while (addr < vaddr) {
22081da177e4SLinus Torvalds 			if (count == 0)
22091da177e4SLinus Torvalds 				goto finished;
22101da177e4SLinus Torvalds 			buf++;
22111da177e4SLinus Torvalds 			addr++;
22121da177e4SLinus Torvalds 			count--;
22131da177e4SLinus Torvalds 		}
2214762216abSWanpeng Li 		n = vaddr + get_vm_area_size(vm) - addr;
2215d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
2216d0107eb0SKAMEZAWA Hiroyuki 			n = count;
2217e81ce85fSJoonsoo Kim 		if (!(vm->flags & VM_IOREMAP)) {
2218d0107eb0SKAMEZAWA Hiroyuki 			aligned_vwrite(buf, addr, n);
2219d0107eb0SKAMEZAWA Hiroyuki 			copied++;
2220d0107eb0SKAMEZAWA Hiroyuki 		}
2221d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
2222d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
2223d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
22241da177e4SLinus Torvalds 	}
22251da177e4SLinus Torvalds finished:
2226e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2227d0107eb0SKAMEZAWA Hiroyuki 	if (!copied)
2228d0107eb0SKAMEZAWA Hiroyuki 		return 0;
2229d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
22301da177e4SLinus Torvalds }
223183342314SNick Piggin 
223283342314SNick Piggin /**
2233e69e9d4aSHATAYAMA Daisuke  *	remap_vmalloc_range_partial  -  map vmalloc pages to userspace
2234e69e9d4aSHATAYAMA Daisuke  *	@vma:		vma to cover
2235e69e9d4aSHATAYAMA Daisuke  *	@uaddr:		target user address to start at
2236e69e9d4aSHATAYAMA Daisuke  *	@kaddr:		virtual address of vmalloc kernel memory
2237e69e9d4aSHATAYAMA Daisuke  *	@size:		size of map area
2238e69e9d4aSHATAYAMA Daisuke  *
2239e69e9d4aSHATAYAMA Daisuke  *	Returns:	0 for success, -Exxx on failure
2240e69e9d4aSHATAYAMA Daisuke  *
2241e69e9d4aSHATAYAMA Daisuke  *	This function checks that @kaddr is a valid vmalloc'ed area,
2242e69e9d4aSHATAYAMA Daisuke  *	and that it is big enough to cover the range starting at
2243e69e9d4aSHATAYAMA Daisuke  *	@uaddr in @vma. Will return failure if that criteria isn't
2244e69e9d4aSHATAYAMA Daisuke  *	met.
2245e69e9d4aSHATAYAMA Daisuke  *
2246e69e9d4aSHATAYAMA Daisuke  *	Similar to remap_pfn_range() (see mm/memory.c)
2247e69e9d4aSHATAYAMA Daisuke  */
2248e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
2249e69e9d4aSHATAYAMA Daisuke 				void *kaddr, unsigned long size)
2250e69e9d4aSHATAYAMA Daisuke {
2251e69e9d4aSHATAYAMA Daisuke 	struct vm_struct *area;
2252e69e9d4aSHATAYAMA Daisuke 
2253e69e9d4aSHATAYAMA Daisuke 	size = PAGE_ALIGN(size);
2254e69e9d4aSHATAYAMA Daisuke 
2255e69e9d4aSHATAYAMA Daisuke 	if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
2256e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
2257e69e9d4aSHATAYAMA Daisuke 
2258e69e9d4aSHATAYAMA Daisuke 	area = find_vm_area(kaddr);
2259e69e9d4aSHATAYAMA Daisuke 	if (!area)
2260e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
2261e69e9d4aSHATAYAMA Daisuke 
2262e69e9d4aSHATAYAMA Daisuke 	if (!(area->flags & VM_USERMAP))
2263e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
2264e69e9d4aSHATAYAMA Daisuke 
2265e69e9d4aSHATAYAMA Daisuke 	if (kaddr + size > area->addr + area->size)
2266e69e9d4aSHATAYAMA Daisuke 		return -EINVAL;
2267e69e9d4aSHATAYAMA Daisuke 
2268e69e9d4aSHATAYAMA Daisuke 	do {
2269e69e9d4aSHATAYAMA Daisuke 		struct page *page = vmalloc_to_page(kaddr);
2270e69e9d4aSHATAYAMA Daisuke 		int ret;
2271e69e9d4aSHATAYAMA Daisuke 
2272e69e9d4aSHATAYAMA Daisuke 		ret = vm_insert_page(vma, uaddr, page);
2273e69e9d4aSHATAYAMA Daisuke 		if (ret)
2274e69e9d4aSHATAYAMA Daisuke 			return ret;
2275e69e9d4aSHATAYAMA Daisuke 
2276e69e9d4aSHATAYAMA Daisuke 		uaddr += PAGE_SIZE;
2277e69e9d4aSHATAYAMA Daisuke 		kaddr += PAGE_SIZE;
2278e69e9d4aSHATAYAMA Daisuke 		size -= PAGE_SIZE;
2279e69e9d4aSHATAYAMA Daisuke 	} while (size > 0);
2280e69e9d4aSHATAYAMA Daisuke 
2281e69e9d4aSHATAYAMA Daisuke 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
2282e69e9d4aSHATAYAMA Daisuke 
2283e69e9d4aSHATAYAMA Daisuke 	return 0;
2284e69e9d4aSHATAYAMA Daisuke }
2285e69e9d4aSHATAYAMA Daisuke EXPORT_SYMBOL(remap_vmalloc_range_partial);
2286e69e9d4aSHATAYAMA Daisuke 
2287e69e9d4aSHATAYAMA Daisuke /**
228883342314SNick Piggin  *	remap_vmalloc_range  -  map vmalloc pages to userspace
228983342314SNick Piggin  *	@vma:		vma to cover (map full range of vma)
229083342314SNick Piggin  *	@addr:		vmalloc memory
229183342314SNick Piggin  *	@pgoff:		number of pages into addr before first page to map
22927682486bSRandy Dunlap  *
22937682486bSRandy Dunlap  *	Returns:	0 for success, -Exxx on failure
229483342314SNick Piggin  *
229583342314SNick Piggin  *	This function checks that addr is a valid vmalloc'ed area, and
229683342314SNick Piggin  *	that it is big enough to cover the vma. Will return failure if
229783342314SNick Piggin  *	that criteria isn't met.
229883342314SNick Piggin  *
229972fd4a35SRobert P. J. Day  *	Similar to remap_pfn_range() (see mm/memory.c)
230083342314SNick Piggin  */
230183342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
230283342314SNick Piggin 						unsigned long pgoff)
230383342314SNick Piggin {
2304e69e9d4aSHATAYAMA Daisuke 	return remap_vmalloc_range_partial(vma, vma->vm_start,
2305e69e9d4aSHATAYAMA Daisuke 					   addr + (pgoff << PAGE_SHIFT),
2306e69e9d4aSHATAYAMA Daisuke 					   vma->vm_end - vma->vm_start);
230783342314SNick Piggin }
230883342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range);
230983342314SNick Piggin 
23101eeb66a1SChristoph Hellwig /*
23111eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
23121eeb66a1SChristoph Hellwig  * have one.
23131eeb66a1SChristoph Hellwig  */
23143b32123dSGideon Israel Dsouza void __weak vmalloc_sync_all(void)
23151eeb66a1SChristoph Hellwig {
23161eeb66a1SChristoph Hellwig }
23175f4352fbSJeremy Fitzhardinge 
23185f4352fbSJeremy Fitzhardinge 
23192f569afdSMartin Schwidefsky static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
23205f4352fbSJeremy Fitzhardinge {
2321cd12909cSDavid Vrabel 	pte_t ***p = data;
2322cd12909cSDavid Vrabel 
2323cd12909cSDavid Vrabel 	if (p) {
2324cd12909cSDavid Vrabel 		*(*p) = pte;
2325cd12909cSDavid Vrabel 		(*p)++;
2326cd12909cSDavid Vrabel 	}
23275f4352fbSJeremy Fitzhardinge 	return 0;
23285f4352fbSJeremy Fitzhardinge }
23295f4352fbSJeremy Fitzhardinge 
23305f4352fbSJeremy Fitzhardinge /**
23315f4352fbSJeremy Fitzhardinge  *	alloc_vm_area - allocate a range of kernel address space
23325f4352fbSJeremy Fitzhardinge  *	@size:		size of the area
2333cd12909cSDavid Vrabel  *	@ptes:		returns the PTEs for the address space
23347682486bSRandy Dunlap  *
23357682486bSRandy Dunlap  *	Returns:	NULL on failure, vm_struct on success
23365f4352fbSJeremy Fitzhardinge  *
23375f4352fbSJeremy Fitzhardinge  *	This function reserves a range of kernel address space, and
23385f4352fbSJeremy Fitzhardinge  *	allocates pagetables to map that range.  No actual mappings
2339cd12909cSDavid Vrabel  *	are created.
2340cd12909cSDavid Vrabel  *
2341cd12909cSDavid Vrabel  *	If @ptes is non-NULL, pointers to the PTEs (in init_mm)
2342cd12909cSDavid Vrabel  *	allocated for the VM area are returned.
23435f4352fbSJeremy Fitzhardinge  */
2344cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
23455f4352fbSJeremy Fitzhardinge {
23465f4352fbSJeremy Fitzhardinge 	struct vm_struct *area;
23475f4352fbSJeremy Fitzhardinge 
234823016969SChristoph Lameter 	area = get_vm_area_caller(size, VM_IOREMAP,
234923016969SChristoph Lameter 				__builtin_return_address(0));
23505f4352fbSJeremy Fitzhardinge 	if (area == NULL)
23515f4352fbSJeremy Fitzhardinge 		return NULL;
23525f4352fbSJeremy Fitzhardinge 
23535f4352fbSJeremy Fitzhardinge 	/*
23545f4352fbSJeremy Fitzhardinge 	 * This ensures that page tables are constructed for this region
23555f4352fbSJeremy Fitzhardinge 	 * of kernel virtual address space and mapped into init_mm.
23565f4352fbSJeremy Fitzhardinge 	 */
23575f4352fbSJeremy Fitzhardinge 	if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
2358cd12909cSDavid Vrabel 				size, f, ptes ? &ptes : NULL)) {
23595f4352fbSJeremy Fitzhardinge 		free_vm_area(area);
23605f4352fbSJeremy Fitzhardinge 		return NULL;
23615f4352fbSJeremy Fitzhardinge 	}
23625f4352fbSJeremy Fitzhardinge 
23635f4352fbSJeremy Fitzhardinge 	return area;
23645f4352fbSJeremy Fitzhardinge }
23655f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(alloc_vm_area);
23665f4352fbSJeremy Fitzhardinge 
23675f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area)
23685f4352fbSJeremy Fitzhardinge {
23695f4352fbSJeremy Fitzhardinge 	struct vm_struct *ret;
23705f4352fbSJeremy Fitzhardinge 	ret = remove_vm_area(area->addr);
23715f4352fbSJeremy Fitzhardinge 	BUG_ON(ret != area);
23725f4352fbSJeremy Fitzhardinge 	kfree(area);
23735f4352fbSJeremy Fitzhardinge }
23745f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area);
2375a10aa579SChristoph Lameter 
23764f8b02b4STejun Heo #ifdef CONFIG_SMP
2377ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n)
2378ca23e405STejun Heo {
23794583e773SGeliang Tang 	return rb_entry_safe(n, struct vmap_area, rb_node);
2380ca23e405STejun Heo }
2381ca23e405STejun Heo 
2382ca23e405STejun Heo /**
2383ca23e405STejun Heo  * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
2384ca23e405STejun Heo  * @end: target address
2385ca23e405STejun Heo  * @pnext: out arg for the next vmap_area
2386ca23e405STejun Heo  * @pprev: out arg for the previous vmap_area
2387ca23e405STejun Heo  *
2388ca23e405STejun Heo  * Returns: %true if either or both of next and prev are found,
2389ca23e405STejun Heo  *	    %false if no vmap_area exists
2390ca23e405STejun Heo  *
2391ca23e405STejun Heo  * Find vmap_areas end addresses of which enclose @end.  ie. if not
2392ca23e405STejun Heo  * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
2393ca23e405STejun Heo  */
2394ca23e405STejun Heo static bool pvm_find_next_prev(unsigned long end,
2395ca23e405STejun Heo 			       struct vmap_area **pnext,
2396ca23e405STejun Heo 			       struct vmap_area **pprev)
2397ca23e405STejun Heo {
2398ca23e405STejun Heo 	struct rb_node *n = vmap_area_root.rb_node;
2399ca23e405STejun Heo 	struct vmap_area *va = NULL;
2400ca23e405STejun Heo 
2401ca23e405STejun Heo 	while (n) {
2402ca23e405STejun Heo 		va = rb_entry(n, struct vmap_area, rb_node);
2403ca23e405STejun Heo 		if (end < va->va_end)
2404ca23e405STejun Heo 			n = n->rb_left;
2405ca23e405STejun Heo 		else if (end > va->va_end)
2406ca23e405STejun Heo 			n = n->rb_right;
2407ca23e405STejun Heo 		else
2408ca23e405STejun Heo 			break;
2409ca23e405STejun Heo 	}
2410ca23e405STejun Heo 
2411ca23e405STejun Heo 	if (!va)
2412ca23e405STejun Heo 		return false;
2413ca23e405STejun Heo 
2414ca23e405STejun Heo 	if (va->va_end > end) {
2415ca23e405STejun Heo 		*pnext = va;
2416ca23e405STejun Heo 		*pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2417ca23e405STejun Heo 	} else {
2418ca23e405STejun Heo 		*pprev = va;
2419ca23e405STejun Heo 		*pnext = node_to_va(rb_next(&(*pprev)->rb_node));
2420ca23e405STejun Heo 	}
2421ca23e405STejun Heo 	return true;
2422ca23e405STejun Heo }
2423ca23e405STejun Heo 
2424ca23e405STejun Heo /**
2425ca23e405STejun Heo  * pvm_determine_end - find the highest aligned address between two vmap_areas
2426ca23e405STejun Heo  * @pnext: in/out arg for the next vmap_area
2427ca23e405STejun Heo  * @pprev: in/out arg for the previous vmap_area
2428ca23e405STejun Heo  * @align: alignment
2429ca23e405STejun Heo  *
2430ca23e405STejun Heo  * Returns: determined end address
2431ca23e405STejun Heo  *
2432ca23e405STejun Heo  * Find the highest aligned address between *@pnext and *@pprev below
2433ca23e405STejun Heo  * VMALLOC_END.  *@pnext and *@pprev are adjusted so that the aligned
2434ca23e405STejun Heo  * down address is between the end addresses of the two vmap_areas.
2435ca23e405STejun Heo  *
2436ca23e405STejun Heo  * Please note that the address returned by this function may fall
2437ca23e405STejun Heo  * inside *@pnext vmap_area.  The caller is responsible for checking
2438ca23e405STejun Heo  * that.
2439ca23e405STejun Heo  */
2440ca23e405STejun Heo static unsigned long pvm_determine_end(struct vmap_area **pnext,
2441ca23e405STejun Heo 				       struct vmap_area **pprev,
2442ca23e405STejun Heo 				       unsigned long align)
2443ca23e405STejun Heo {
2444ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2445ca23e405STejun Heo 	unsigned long addr;
2446ca23e405STejun Heo 
2447ca23e405STejun Heo 	if (*pnext)
2448ca23e405STejun Heo 		addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
2449ca23e405STejun Heo 	else
2450ca23e405STejun Heo 		addr = vmalloc_end;
2451ca23e405STejun Heo 
2452ca23e405STejun Heo 	while (*pprev && (*pprev)->va_end > addr) {
2453ca23e405STejun Heo 		*pnext = *pprev;
2454ca23e405STejun Heo 		*pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2455ca23e405STejun Heo 	}
2456ca23e405STejun Heo 
2457ca23e405STejun Heo 	return addr;
2458ca23e405STejun Heo }
2459ca23e405STejun Heo 
2460ca23e405STejun Heo /**
2461ca23e405STejun Heo  * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
2462ca23e405STejun Heo  * @offsets: array containing offset of each area
2463ca23e405STejun Heo  * @sizes: array containing size of each area
2464ca23e405STejun Heo  * @nr_vms: the number of areas to allocate
2465ca23e405STejun Heo  * @align: alignment, all entries in @offsets and @sizes must be aligned to this
2466ca23e405STejun Heo  *
2467ca23e405STejun Heo  * Returns: kmalloc'd vm_struct pointer array pointing to allocated
2468ca23e405STejun Heo  *	    vm_structs on success, %NULL on failure
2469ca23e405STejun Heo  *
2470ca23e405STejun Heo  * Percpu allocator wants to use congruent vm areas so that it can
2471ca23e405STejun Heo  * maintain the offsets among percpu areas.  This function allocates
2472ec3f64fcSDavid Rientjes  * congruent vmalloc areas for it with GFP_KERNEL.  These areas tend to
2473ec3f64fcSDavid Rientjes  * be scattered pretty far, distance between two areas easily going up
2474ec3f64fcSDavid Rientjes  * to gigabytes.  To avoid interacting with regular vmallocs, these
2475ec3f64fcSDavid Rientjes  * areas are allocated from top.
2476ca23e405STejun Heo  *
2477ca23e405STejun Heo  * Despite its complicated look, this allocator is rather simple.  It
2478ca23e405STejun Heo  * does everything top-down and scans areas from the end looking for
2479ca23e405STejun Heo  * matching slot.  While scanning, if any of the areas overlaps with
2480ca23e405STejun Heo  * existing vmap_area, the base address is pulled down to fit the
2481ca23e405STejun Heo  * area.  Scanning is repeated till all the areas fit and then all
2482ca23e405STejun Heo  * necessary data structres are inserted and the result is returned.
2483ca23e405STejun Heo  */
2484ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
2485ca23e405STejun Heo 				     const size_t *sizes, int nr_vms,
2486ec3f64fcSDavid Rientjes 				     size_t align)
2487ca23e405STejun Heo {
2488ca23e405STejun Heo 	const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
2489ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2490ca23e405STejun Heo 	struct vmap_area **vas, *prev, *next;
2491ca23e405STejun Heo 	struct vm_struct **vms;
2492ca23e405STejun Heo 	int area, area2, last_area, term_area;
2493ca23e405STejun Heo 	unsigned long base, start, end, last_end;
2494ca23e405STejun Heo 	bool purged = false;
2495ca23e405STejun Heo 
2496ca23e405STejun Heo 	/* verify parameters and allocate data structures */
2497891c49abSAlexander Kuleshov 	BUG_ON(offset_in_page(align) || !is_power_of_2(align));
2498ca23e405STejun Heo 	for (last_area = 0, area = 0; area < nr_vms; area++) {
2499ca23e405STejun Heo 		start = offsets[area];
2500ca23e405STejun Heo 		end = start + sizes[area];
2501ca23e405STejun Heo 
2502ca23e405STejun Heo 		/* is everything aligned properly? */
2503ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(offsets[area], align));
2504ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(sizes[area], align));
2505ca23e405STejun Heo 
2506ca23e405STejun Heo 		/* detect the area with the highest address */
2507ca23e405STejun Heo 		if (start > offsets[last_area])
2508ca23e405STejun Heo 			last_area = area;
2509ca23e405STejun Heo 
2510ca23e405STejun Heo 		for (area2 = 0; area2 < nr_vms; area2++) {
2511ca23e405STejun Heo 			unsigned long start2 = offsets[area2];
2512ca23e405STejun Heo 			unsigned long end2 = start2 + sizes[area2];
2513ca23e405STejun Heo 
2514ca23e405STejun Heo 			if (area2 == area)
2515ca23e405STejun Heo 				continue;
2516ca23e405STejun Heo 
2517ca23e405STejun Heo 			BUG_ON(start2 >= start && start2 < end);
2518ca23e405STejun Heo 			BUG_ON(end2 <= end && end2 > start);
2519ca23e405STejun Heo 		}
2520ca23e405STejun Heo 	}
2521ca23e405STejun Heo 	last_end = offsets[last_area] + sizes[last_area];
2522ca23e405STejun Heo 
2523ca23e405STejun Heo 	if (vmalloc_end - vmalloc_start < last_end) {
2524ca23e405STejun Heo 		WARN_ON(true);
2525ca23e405STejun Heo 		return NULL;
2526ca23e405STejun Heo 	}
2527ca23e405STejun Heo 
25284d67d860SThomas Meyer 	vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
25294d67d860SThomas Meyer 	vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
2530ca23e405STejun Heo 	if (!vas || !vms)
2531f1db7afdSKautuk Consul 		goto err_free2;
2532ca23e405STejun Heo 
2533ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
2534ec3f64fcSDavid Rientjes 		vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL);
2535ec3f64fcSDavid Rientjes 		vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
2536ca23e405STejun Heo 		if (!vas[area] || !vms[area])
2537ca23e405STejun Heo 			goto err_free;
2538ca23e405STejun Heo 	}
2539ca23e405STejun Heo retry:
2540ca23e405STejun Heo 	spin_lock(&vmap_area_lock);
2541ca23e405STejun Heo 
2542ca23e405STejun Heo 	/* start scanning - we scan from the top, begin with the last area */
2543ca23e405STejun Heo 	area = term_area = last_area;
2544ca23e405STejun Heo 	start = offsets[area];
2545ca23e405STejun Heo 	end = start + sizes[area];
2546ca23e405STejun Heo 
2547ca23e405STejun Heo 	if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2548ca23e405STejun Heo 		base = vmalloc_end - last_end;
2549ca23e405STejun Heo 		goto found;
2550ca23e405STejun Heo 	}
2551ca23e405STejun Heo 	base = pvm_determine_end(&next, &prev, align) - end;
2552ca23e405STejun Heo 
2553ca23e405STejun Heo 	while (true) {
2554ca23e405STejun Heo 		BUG_ON(next && next->va_end <= base + end);
2555ca23e405STejun Heo 		BUG_ON(prev && prev->va_end > base + end);
2556ca23e405STejun Heo 
2557ca23e405STejun Heo 		/*
2558ca23e405STejun Heo 		 * base might have underflowed, add last_end before
2559ca23e405STejun Heo 		 * comparing.
2560ca23e405STejun Heo 		 */
2561ca23e405STejun Heo 		if (base + last_end < vmalloc_start + last_end) {
2562ca23e405STejun Heo 			spin_unlock(&vmap_area_lock);
2563ca23e405STejun Heo 			if (!purged) {
2564ca23e405STejun Heo 				purge_vmap_area_lazy();
2565ca23e405STejun Heo 				purged = true;
2566ca23e405STejun Heo 				goto retry;
2567ca23e405STejun Heo 			}
2568ca23e405STejun Heo 			goto err_free;
2569ca23e405STejun Heo 		}
2570ca23e405STejun Heo 
2571ca23e405STejun Heo 		/*
2572ca23e405STejun Heo 		 * If next overlaps, move base downwards so that it's
2573ca23e405STejun Heo 		 * right below next and then recheck.
2574ca23e405STejun Heo 		 */
2575ca23e405STejun Heo 		if (next && next->va_start < base + end) {
2576ca23e405STejun Heo 			base = pvm_determine_end(&next, &prev, align) - end;
2577ca23e405STejun Heo 			term_area = area;
2578ca23e405STejun Heo 			continue;
2579ca23e405STejun Heo 		}
2580ca23e405STejun Heo 
2581ca23e405STejun Heo 		/*
2582ca23e405STejun Heo 		 * If prev overlaps, shift down next and prev and move
2583ca23e405STejun Heo 		 * base so that it's right below new next and then
2584ca23e405STejun Heo 		 * recheck.
2585ca23e405STejun Heo 		 */
2586ca23e405STejun Heo 		if (prev && prev->va_end > base + start)  {
2587ca23e405STejun Heo 			next = prev;
2588ca23e405STejun Heo 			prev = node_to_va(rb_prev(&next->rb_node));
2589ca23e405STejun Heo 			base = pvm_determine_end(&next, &prev, align) - end;
2590ca23e405STejun Heo 			term_area = area;
2591ca23e405STejun Heo 			continue;
2592ca23e405STejun Heo 		}
2593ca23e405STejun Heo 
2594ca23e405STejun Heo 		/*
2595ca23e405STejun Heo 		 * This area fits, move on to the previous one.  If
2596ca23e405STejun Heo 		 * the previous one is the terminal one, we're done.
2597ca23e405STejun Heo 		 */
2598ca23e405STejun Heo 		area = (area + nr_vms - 1) % nr_vms;
2599ca23e405STejun Heo 		if (area == term_area)
2600ca23e405STejun Heo 			break;
2601ca23e405STejun Heo 		start = offsets[area];
2602ca23e405STejun Heo 		end = start + sizes[area];
2603ca23e405STejun Heo 		pvm_find_next_prev(base + end, &next, &prev);
2604ca23e405STejun Heo 	}
2605ca23e405STejun Heo found:
2606ca23e405STejun Heo 	/* we've found a fitting base, insert all va's */
2607ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
2608ca23e405STejun Heo 		struct vmap_area *va = vas[area];
2609ca23e405STejun Heo 
2610ca23e405STejun Heo 		va->va_start = base + offsets[area];
2611ca23e405STejun Heo 		va->va_end = va->va_start + sizes[area];
2612ca23e405STejun Heo 		__insert_vmap_area(va);
2613ca23e405STejun Heo 	}
2614ca23e405STejun Heo 
2615ca23e405STejun Heo 	vmap_area_pcpu_hole = base + offsets[last_area];
2616ca23e405STejun Heo 
2617ca23e405STejun Heo 	spin_unlock(&vmap_area_lock);
2618ca23e405STejun Heo 
2619ca23e405STejun Heo 	/* insert all vm's */
2620ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++)
26213645cb4aSZhang Yanfei 		setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2622ca23e405STejun Heo 				 pcpu_get_vm_areas);
2623ca23e405STejun Heo 
2624ca23e405STejun Heo 	kfree(vas);
2625ca23e405STejun Heo 	return vms;
2626ca23e405STejun Heo 
2627ca23e405STejun Heo err_free:
2628ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
2629ca23e405STejun Heo 		kfree(vas[area]);
2630ca23e405STejun Heo 		kfree(vms[area]);
2631ca23e405STejun Heo 	}
2632f1db7afdSKautuk Consul err_free2:
2633ca23e405STejun Heo 	kfree(vas);
2634ca23e405STejun Heo 	kfree(vms);
2635ca23e405STejun Heo 	return NULL;
2636ca23e405STejun Heo }
2637ca23e405STejun Heo 
2638ca23e405STejun Heo /**
2639ca23e405STejun Heo  * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2640ca23e405STejun Heo  * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2641ca23e405STejun Heo  * @nr_vms: the number of allocated areas
2642ca23e405STejun Heo  *
2643ca23e405STejun Heo  * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2644ca23e405STejun Heo  */
2645ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2646ca23e405STejun Heo {
2647ca23e405STejun Heo 	int i;
2648ca23e405STejun Heo 
2649ca23e405STejun Heo 	for (i = 0; i < nr_vms; i++)
2650ca23e405STejun Heo 		free_vm_area(vms[i]);
2651ca23e405STejun Heo 	kfree(vms);
2652ca23e405STejun Heo }
26534f8b02b4STejun Heo #endif	/* CONFIG_SMP */
2654a10aa579SChristoph Lameter 
2655a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS
2656a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos)
2657d4033afdSJoonsoo Kim 	__acquires(&vmap_area_lock)
2658a10aa579SChristoph Lameter {
2659d4033afdSJoonsoo Kim 	spin_lock(&vmap_area_lock);
26603f500069Szijun_hu 	return seq_list_start(&vmap_area_list, *pos);
2661a10aa579SChristoph Lameter }
2662a10aa579SChristoph Lameter 
2663a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2664a10aa579SChristoph Lameter {
26653f500069Szijun_hu 	return seq_list_next(p, &vmap_area_list, pos);
2666a10aa579SChristoph Lameter }
2667a10aa579SChristoph Lameter 
2668a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p)
2669d4033afdSJoonsoo Kim 	__releases(&vmap_area_lock)
2670a10aa579SChristoph Lameter {
2671d4033afdSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2672a10aa579SChristoph Lameter }
2673a10aa579SChristoph Lameter 
2674a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2675a47a126aSEric Dumazet {
2676e5adfffcSKirill A. Shutemov 	if (IS_ENABLED(CONFIG_NUMA)) {
2677a47a126aSEric Dumazet 		unsigned int nr, *counters = m->private;
2678a47a126aSEric Dumazet 
2679a47a126aSEric Dumazet 		if (!counters)
2680a47a126aSEric Dumazet 			return;
2681a47a126aSEric Dumazet 
2682af12346cSWanpeng Li 		if (v->flags & VM_UNINITIALIZED)
2683af12346cSWanpeng Li 			return;
26847e5b528bSDmitry Vyukov 		/* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
26857e5b528bSDmitry Vyukov 		smp_rmb();
2686af12346cSWanpeng Li 
2687a47a126aSEric Dumazet 		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2688a47a126aSEric Dumazet 
2689a47a126aSEric Dumazet 		for (nr = 0; nr < v->nr_pages; nr++)
2690a47a126aSEric Dumazet 			counters[page_to_nid(v->pages[nr])]++;
2691a47a126aSEric Dumazet 
2692a47a126aSEric Dumazet 		for_each_node_state(nr, N_HIGH_MEMORY)
2693a47a126aSEric Dumazet 			if (counters[nr])
2694a47a126aSEric Dumazet 				seq_printf(m, " N%u=%u", nr, counters[nr]);
2695a47a126aSEric Dumazet 	}
2696a47a126aSEric Dumazet }
2697a47a126aSEric Dumazet 
2698a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p)
2699a10aa579SChristoph Lameter {
27003f500069Szijun_hu 	struct vmap_area *va;
2701d4033afdSJoonsoo Kim 	struct vm_struct *v;
2702d4033afdSJoonsoo Kim 
27033f500069Szijun_hu 	va = list_entry(p, struct vmap_area, list);
27043f500069Szijun_hu 
2705c2ce8c14SWanpeng Li 	/*
2706c2ce8c14SWanpeng Li 	 * s_show can encounter race with remove_vm_area, !VM_VM_AREA on
2707c2ce8c14SWanpeng Li 	 * behalf of vmap area is being tear down or vm_map_ram allocation.
2708c2ce8c14SWanpeng Li 	 */
2709*78c72746SYisheng Xie 	if (!(va->flags & VM_VM_AREA)) {
2710*78c72746SYisheng Xie 		seq_printf(m, "0x%pK-0x%pK %7ld %s\n",
2711*78c72746SYisheng Xie 			(void *)va->va_start, (void *)va->va_end,
2712*78c72746SYisheng Xie 			va->va_end - va->va_start,
2713*78c72746SYisheng Xie 			va->flags & VM_LAZY_FREE ? "unpurged vm_area" : "vm_map_ram");
2714*78c72746SYisheng Xie 
2715d4033afdSJoonsoo Kim 		return 0;
2716*78c72746SYisheng Xie 	}
2717d4033afdSJoonsoo Kim 
2718d4033afdSJoonsoo Kim 	v = va->vm;
2719a10aa579SChristoph Lameter 
272045ec1690SKees Cook 	seq_printf(m, "0x%pK-0x%pK %7ld",
2721a10aa579SChristoph Lameter 		v->addr, v->addr + v->size, v->size);
2722a10aa579SChristoph Lameter 
272362c70bceSJoe Perches 	if (v->caller)
272462c70bceSJoe Perches 		seq_printf(m, " %pS", v->caller);
272523016969SChristoph Lameter 
2726a10aa579SChristoph Lameter 	if (v->nr_pages)
2727a10aa579SChristoph Lameter 		seq_printf(m, " pages=%d", v->nr_pages);
2728a10aa579SChristoph Lameter 
2729a10aa579SChristoph Lameter 	if (v->phys_addr)
2730199eaa05SMiles Chen 		seq_printf(m, " phys=%pa", &v->phys_addr);
2731a10aa579SChristoph Lameter 
2732a10aa579SChristoph Lameter 	if (v->flags & VM_IOREMAP)
2733f4527c90SFabian Frederick 		seq_puts(m, " ioremap");
2734a10aa579SChristoph Lameter 
2735a10aa579SChristoph Lameter 	if (v->flags & VM_ALLOC)
2736f4527c90SFabian Frederick 		seq_puts(m, " vmalloc");
2737a10aa579SChristoph Lameter 
2738a10aa579SChristoph Lameter 	if (v->flags & VM_MAP)
2739f4527c90SFabian Frederick 		seq_puts(m, " vmap");
2740a10aa579SChristoph Lameter 
2741a10aa579SChristoph Lameter 	if (v->flags & VM_USERMAP)
2742f4527c90SFabian Frederick 		seq_puts(m, " user");
2743a10aa579SChristoph Lameter 
2744244d63eeSDavid Rientjes 	if (is_vmalloc_addr(v->pages))
2745f4527c90SFabian Frederick 		seq_puts(m, " vpages");
2746a10aa579SChristoph Lameter 
2747a47a126aSEric Dumazet 	show_numa_info(m, v);
2748a10aa579SChristoph Lameter 	seq_putc(m, '\n');
2749a10aa579SChristoph Lameter 	return 0;
2750a10aa579SChristoph Lameter }
2751a10aa579SChristoph Lameter 
27525f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = {
2753a10aa579SChristoph Lameter 	.start = s_start,
2754a10aa579SChristoph Lameter 	.next = s_next,
2755a10aa579SChristoph Lameter 	.stop = s_stop,
2756a10aa579SChristoph Lameter 	.show = s_show,
2757a10aa579SChristoph Lameter };
27585f6a6a9cSAlexey Dobriyan 
27595f6a6a9cSAlexey Dobriyan static int vmalloc_open(struct inode *inode, struct file *file)
27605f6a6a9cSAlexey Dobriyan {
2761703394c1SRob Jones 	if (IS_ENABLED(CONFIG_NUMA))
2762703394c1SRob Jones 		return seq_open_private(file, &vmalloc_op,
2763703394c1SRob Jones 					nr_node_ids * sizeof(unsigned int));
2764703394c1SRob Jones 	else
2765703394c1SRob Jones 		return seq_open(file, &vmalloc_op);
27665f6a6a9cSAlexey Dobriyan }
27675f6a6a9cSAlexey Dobriyan 
27685f6a6a9cSAlexey Dobriyan static const struct file_operations proc_vmalloc_operations = {
27695f6a6a9cSAlexey Dobriyan 	.open		= vmalloc_open,
27705f6a6a9cSAlexey Dobriyan 	.read		= seq_read,
27715f6a6a9cSAlexey Dobriyan 	.llseek		= seq_lseek,
27725f6a6a9cSAlexey Dobriyan 	.release	= seq_release_private,
27735f6a6a9cSAlexey Dobriyan };
27745f6a6a9cSAlexey Dobriyan 
27755f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void)
27765f6a6a9cSAlexey Dobriyan {
27775f6a6a9cSAlexey Dobriyan 	proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
27785f6a6a9cSAlexey Dobriyan 	return 0;
27795f6a6a9cSAlexey Dobriyan }
27805f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init);
2781db3808c1SJoonsoo Kim 
2782a10aa579SChristoph Lameter #endif
2783a10aa579SChristoph Lameter 
2784