xref: /openbmc/linux/mm/vmalloc.c (revision cef2ac3f6c8ab532e49cf69d05f540931ad8ee64)
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>
15d43c36dcSAlexey Dobriyan #include <linux/sched.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>
24db64fe02SNick Piggin #include <linux/rbtree.h>
25db64fe02SNick Piggin #include <linux/radix-tree.h>
26db64fe02SNick Piggin #include <linux/rcupdate.h>
27f0aa6617STejun Heo #include <linux/pfn.h>
2889219d37SCatalin Marinas #include <linux/kmemleak.h>
2960063497SArun Sharma #include <linux/atomic.h>
3032fcfd40SAl Viro #include <linux/llist.h>
311da177e4SLinus Torvalds #include <asm/uaccess.h>
321da177e4SLinus Torvalds #include <asm/tlbflush.h>
332dca6999SDavid Miller #include <asm/shmparam.h>
341da177e4SLinus Torvalds 
3532fcfd40SAl Viro struct vfree_deferred {
3632fcfd40SAl Viro 	struct llist_head list;
3732fcfd40SAl Viro 	struct work_struct wq;
3832fcfd40SAl Viro };
3932fcfd40SAl Viro static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
4032fcfd40SAl Viro 
4132fcfd40SAl Viro static void __vunmap(const void *, int);
4232fcfd40SAl Viro 
4332fcfd40SAl Viro static void free_work(struct work_struct *w)
4432fcfd40SAl Viro {
4532fcfd40SAl Viro 	struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
4632fcfd40SAl Viro 	struct llist_node *llnode = llist_del_all(&p->list);
4732fcfd40SAl Viro 	while (llnode) {
4832fcfd40SAl Viro 		void *p = llnode;
4932fcfd40SAl Viro 		llnode = llist_next(llnode);
5032fcfd40SAl Viro 		__vunmap(p, 1);
5132fcfd40SAl Viro 	}
5232fcfd40SAl Viro }
5332fcfd40SAl Viro 
54db64fe02SNick Piggin /*** Page table manipulation functions ***/
55b221385bSAdrian Bunk 
561da177e4SLinus Torvalds static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds 	pte_t *pte;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	pte = pte_offset_kernel(pmd, addr);
611da177e4SLinus Torvalds 	do {
621da177e4SLinus Torvalds 		pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
631da177e4SLinus Torvalds 		WARN_ON(!pte_none(ptent) && !pte_present(ptent));
641da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
651da177e4SLinus Torvalds }
661da177e4SLinus Torvalds 
67db64fe02SNick Piggin static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
681da177e4SLinus Torvalds {
691da177e4SLinus Torvalds 	pmd_t *pmd;
701da177e4SLinus Torvalds 	unsigned long next;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
731da177e4SLinus Torvalds 	do {
741da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
751da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
761da177e4SLinus Torvalds 			continue;
771da177e4SLinus Torvalds 		vunmap_pte_range(pmd, addr, next);
781da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
81db64fe02SNick Piggin static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
821da177e4SLinus Torvalds {
831da177e4SLinus Torvalds 	pud_t *pud;
841da177e4SLinus Torvalds 	unsigned long next;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
871da177e4SLinus Torvalds 	do {
881da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
891da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
901da177e4SLinus Torvalds 			continue;
911da177e4SLinus Torvalds 		vunmap_pmd_range(pud, addr, next);
921da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
95db64fe02SNick Piggin static void vunmap_page_range(unsigned long addr, unsigned long end)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	pgd_t *pgd;
981da177e4SLinus Torvalds 	unsigned long next;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1011da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
1021da177e4SLinus Torvalds 	do {
1031da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
1041da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
1051da177e4SLinus Torvalds 			continue;
1061da177e4SLinus Torvalds 		vunmap_pud_range(pgd, addr, next);
1071da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
111db64fe02SNick Piggin 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	pte_t *pte;
1141da177e4SLinus Torvalds 
115db64fe02SNick Piggin 	/*
116db64fe02SNick Piggin 	 * nr is a running index into the array which helps higher level
117db64fe02SNick Piggin 	 * callers keep track of where we're up to.
118db64fe02SNick Piggin 	 */
119db64fe02SNick Piggin 
120872fec16SHugh Dickins 	pte = pte_alloc_kernel(pmd, addr);
1211da177e4SLinus Torvalds 	if (!pte)
1221da177e4SLinus Torvalds 		return -ENOMEM;
1231da177e4SLinus Torvalds 	do {
124db64fe02SNick Piggin 		struct page *page = pages[*nr];
125db64fe02SNick Piggin 
126db64fe02SNick Piggin 		if (WARN_ON(!pte_none(*pte)))
127db64fe02SNick Piggin 			return -EBUSY;
128db64fe02SNick Piggin 		if (WARN_ON(!page))
1291da177e4SLinus Torvalds 			return -ENOMEM;
1301da177e4SLinus Torvalds 		set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
131db64fe02SNick Piggin 		(*nr)++;
1321da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1331da177e4SLinus Torvalds 	return 0;
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
136db64fe02SNick Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr,
137db64fe02SNick Piggin 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	pmd_t *pmd;
1401da177e4SLinus Torvalds 	unsigned long next;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	pmd = pmd_alloc(&init_mm, pud, addr);
1431da177e4SLinus Torvalds 	if (!pmd)
1441da177e4SLinus Torvalds 		return -ENOMEM;
1451da177e4SLinus Torvalds 	do {
1461da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
147db64fe02SNick Piggin 		if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
1481da177e4SLinus Torvalds 			return -ENOMEM;
1491da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
1501da177e4SLinus Torvalds 	return 0;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
153db64fe02SNick Piggin static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
154db64fe02SNick Piggin 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	pud_t *pud;
1571da177e4SLinus Torvalds 	unsigned long next;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	pud = pud_alloc(&init_mm, pgd, addr);
1601da177e4SLinus Torvalds 	if (!pud)
1611da177e4SLinus Torvalds 		return -ENOMEM;
1621da177e4SLinus Torvalds 	do {
1631da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
164db64fe02SNick Piggin 		if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
1651da177e4SLinus Torvalds 			return -ENOMEM;
1661da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
1671da177e4SLinus Torvalds 	return 0;
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
170db64fe02SNick Piggin /*
171db64fe02SNick Piggin  * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
172db64fe02SNick Piggin  * will have pfns corresponding to the "pages" array.
173db64fe02SNick Piggin  *
174db64fe02SNick Piggin  * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
175db64fe02SNick Piggin  */
1768fc48985STejun Heo static int vmap_page_range_noflush(unsigned long start, unsigned long end,
177db64fe02SNick Piggin 				   pgprot_t prot, struct page **pages)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	pgd_t *pgd;
1801da177e4SLinus Torvalds 	unsigned long next;
1812e4e27c7SAdam Lackorzynski 	unsigned long addr = start;
182db64fe02SNick Piggin 	int err = 0;
183db64fe02SNick Piggin 	int nr = 0;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1861da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
1871da177e4SLinus Torvalds 	do {
1881da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
189db64fe02SNick Piggin 		err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
1901da177e4SLinus Torvalds 		if (err)
191bf88c8c8SFigo.zhang 			return err;
1921da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
193db64fe02SNick Piggin 
194db64fe02SNick Piggin 	return nr;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1978fc48985STejun Heo static int vmap_page_range(unsigned long start, unsigned long end,
1988fc48985STejun Heo 			   pgprot_t prot, struct page **pages)
1998fc48985STejun Heo {
2008fc48985STejun Heo 	int ret;
2018fc48985STejun Heo 
2028fc48985STejun Heo 	ret = vmap_page_range_noflush(start, end, prot, pages);
2038fc48985STejun Heo 	flush_cache_vmap(start, end);
2048fc48985STejun Heo 	return ret;
2058fc48985STejun Heo }
2068fc48985STejun Heo 
20781ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x)
20873bdf0a6SLinus Torvalds {
20973bdf0a6SLinus Torvalds 	/*
210ab4f2ee1SRussell King 	 * ARM, x86-64 and sparc64 put modules in a special place,
21173bdf0a6SLinus Torvalds 	 * and fall back on vmalloc() if that fails. Others
21273bdf0a6SLinus Torvalds 	 * just put it in the vmalloc space.
21373bdf0a6SLinus Torvalds 	 */
21473bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
21573bdf0a6SLinus Torvalds 	unsigned long addr = (unsigned long)x;
21673bdf0a6SLinus Torvalds 	if (addr >= MODULES_VADDR && addr < MODULES_END)
21773bdf0a6SLinus Torvalds 		return 1;
21873bdf0a6SLinus Torvalds #endif
21973bdf0a6SLinus Torvalds 	return is_vmalloc_addr(x);
22073bdf0a6SLinus Torvalds }
22173bdf0a6SLinus Torvalds 
22248667e7aSChristoph Lameter /*
223db64fe02SNick Piggin  * Walk a vmap address to the struct page it maps.
22448667e7aSChristoph Lameter  */
225b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *vmalloc_addr)
22648667e7aSChristoph Lameter {
22748667e7aSChristoph Lameter 	unsigned long addr = (unsigned long) vmalloc_addr;
22848667e7aSChristoph Lameter 	struct page *page = NULL;
22948667e7aSChristoph Lameter 	pgd_t *pgd = pgd_offset_k(addr);
23048667e7aSChristoph Lameter 
2317aa413deSIngo Molnar 	/*
2327aa413deSIngo Molnar 	 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
2337aa413deSIngo Molnar 	 * architectures that do not vmalloc module space
2347aa413deSIngo Molnar 	 */
23573bdf0a6SLinus Torvalds 	VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
23659ea7463SJiri Slaby 
23748667e7aSChristoph Lameter 	if (!pgd_none(*pgd)) {
238db64fe02SNick Piggin 		pud_t *pud = pud_offset(pgd, addr);
23948667e7aSChristoph Lameter 		if (!pud_none(*pud)) {
240db64fe02SNick Piggin 			pmd_t *pmd = pmd_offset(pud, addr);
24148667e7aSChristoph Lameter 			if (!pmd_none(*pmd)) {
242db64fe02SNick Piggin 				pte_t *ptep, pte;
243db64fe02SNick Piggin 
24448667e7aSChristoph Lameter 				ptep = pte_offset_map(pmd, addr);
24548667e7aSChristoph Lameter 				pte = *ptep;
24648667e7aSChristoph Lameter 				if (pte_present(pte))
24748667e7aSChristoph Lameter 					page = pte_page(pte);
24848667e7aSChristoph Lameter 				pte_unmap(ptep);
24948667e7aSChristoph Lameter 			}
25048667e7aSChristoph Lameter 		}
25148667e7aSChristoph Lameter 	}
25248667e7aSChristoph Lameter 	return page;
25348667e7aSChristoph Lameter }
25448667e7aSChristoph Lameter EXPORT_SYMBOL(vmalloc_to_page);
25548667e7aSChristoph Lameter 
25648667e7aSChristoph Lameter /*
25748667e7aSChristoph Lameter  * Map a vmalloc()-space virtual address to the physical page frame number.
25848667e7aSChristoph Lameter  */
259b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
26048667e7aSChristoph Lameter {
26148667e7aSChristoph Lameter 	return page_to_pfn(vmalloc_to_page(vmalloc_addr));
26248667e7aSChristoph Lameter }
26348667e7aSChristoph Lameter EXPORT_SYMBOL(vmalloc_to_pfn);
26448667e7aSChristoph Lameter 
265db64fe02SNick Piggin 
266db64fe02SNick Piggin /*** Global kva allocator ***/
267db64fe02SNick Piggin 
268db64fe02SNick Piggin #define VM_LAZY_FREE	0x01
269db64fe02SNick Piggin #define VM_LAZY_FREEING	0x02
270db64fe02SNick Piggin #define VM_VM_AREA	0x04
271db64fe02SNick Piggin 
272db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock);
273f1c4069eSJoonsoo Kim /* Export for kexec only */
274f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
27589699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT;
27689699605SNick Piggin 
27789699605SNick Piggin /* The vmap cache globals are protected by vmap_area_lock */
27889699605SNick Piggin static struct rb_node *free_vmap_cache;
27989699605SNick Piggin static unsigned long cached_hole_size;
28089699605SNick Piggin static unsigned long cached_vstart;
28189699605SNick Piggin static unsigned long cached_align;
28289699605SNick Piggin 
283ca23e405STejun Heo static unsigned long vmap_area_pcpu_hole;
284db64fe02SNick Piggin 
285db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr)
2861da177e4SLinus Torvalds {
287db64fe02SNick Piggin 	struct rb_node *n = vmap_area_root.rb_node;
288db64fe02SNick Piggin 
289db64fe02SNick Piggin 	while (n) {
290db64fe02SNick Piggin 		struct vmap_area *va;
291db64fe02SNick Piggin 
292db64fe02SNick Piggin 		va = rb_entry(n, struct vmap_area, rb_node);
293db64fe02SNick Piggin 		if (addr < va->va_start)
294db64fe02SNick Piggin 			n = n->rb_left;
295*cef2ac3fSHATAYAMA Daisuke 		else if (addr >= va->va_end)
296db64fe02SNick Piggin 			n = n->rb_right;
297db64fe02SNick Piggin 		else
298db64fe02SNick Piggin 			return va;
299db64fe02SNick Piggin 	}
300db64fe02SNick Piggin 
301db64fe02SNick Piggin 	return NULL;
302db64fe02SNick Piggin }
303db64fe02SNick Piggin 
304db64fe02SNick Piggin static void __insert_vmap_area(struct vmap_area *va)
305db64fe02SNick Piggin {
306db64fe02SNick Piggin 	struct rb_node **p = &vmap_area_root.rb_node;
307db64fe02SNick Piggin 	struct rb_node *parent = NULL;
308db64fe02SNick Piggin 	struct rb_node *tmp;
309db64fe02SNick Piggin 
310db64fe02SNick Piggin 	while (*p) {
311170168d0SNamhyung Kim 		struct vmap_area *tmp_va;
312db64fe02SNick Piggin 
313db64fe02SNick Piggin 		parent = *p;
314170168d0SNamhyung Kim 		tmp_va = rb_entry(parent, struct vmap_area, rb_node);
315170168d0SNamhyung Kim 		if (va->va_start < tmp_va->va_end)
316db64fe02SNick Piggin 			p = &(*p)->rb_left;
317170168d0SNamhyung Kim 		else if (va->va_end > tmp_va->va_start)
318db64fe02SNick Piggin 			p = &(*p)->rb_right;
319db64fe02SNick Piggin 		else
320db64fe02SNick Piggin 			BUG();
321db64fe02SNick Piggin 	}
322db64fe02SNick Piggin 
323db64fe02SNick Piggin 	rb_link_node(&va->rb_node, parent, p);
324db64fe02SNick Piggin 	rb_insert_color(&va->rb_node, &vmap_area_root);
325db64fe02SNick Piggin 
3264341fa45SJoonsoo Kim 	/* address-sort this list */
327db64fe02SNick Piggin 	tmp = rb_prev(&va->rb_node);
328db64fe02SNick Piggin 	if (tmp) {
329db64fe02SNick Piggin 		struct vmap_area *prev;
330db64fe02SNick Piggin 		prev = rb_entry(tmp, struct vmap_area, rb_node);
331db64fe02SNick Piggin 		list_add_rcu(&va->list, &prev->list);
332db64fe02SNick Piggin 	} else
333db64fe02SNick Piggin 		list_add_rcu(&va->list, &vmap_area_list);
334db64fe02SNick Piggin }
335db64fe02SNick Piggin 
336db64fe02SNick Piggin static void purge_vmap_area_lazy(void);
337db64fe02SNick Piggin 
338db64fe02SNick Piggin /*
339db64fe02SNick Piggin  * Allocate a region of KVA of the specified size and alignment, within the
340db64fe02SNick Piggin  * vstart and vend.
341db64fe02SNick Piggin  */
342db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size,
343db64fe02SNick Piggin 				unsigned long align,
344db64fe02SNick Piggin 				unsigned long vstart, unsigned long vend,
345db64fe02SNick Piggin 				int node, gfp_t gfp_mask)
346db64fe02SNick Piggin {
347db64fe02SNick Piggin 	struct vmap_area *va;
348db64fe02SNick Piggin 	struct rb_node *n;
3491da177e4SLinus Torvalds 	unsigned long addr;
350db64fe02SNick Piggin 	int purged = 0;
35189699605SNick Piggin 	struct vmap_area *first;
352db64fe02SNick Piggin 
3537766970cSNick Piggin 	BUG_ON(!size);
354db64fe02SNick Piggin 	BUG_ON(size & ~PAGE_MASK);
35589699605SNick Piggin 	BUG_ON(!is_power_of_2(align));
356db64fe02SNick Piggin 
357db64fe02SNick Piggin 	va = kmalloc_node(sizeof(struct vmap_area),
358db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
359db64fe02SNick Piggin 	if (unlikely(!va))
360db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
361db64fe02SNick Piggin 
362db64fe02SNick Piggin retry:
363db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
36489699605SNick Piggin 	/*
36589699605SNick Piggin 	 * Invalidate cache if we have more permissive parameters.
36689699605SNick Piggin 	 * cached_hole_size notes the largest hole noticed _below_
36789699605SNick Piggin 	 * the vmap_area cached in free_vmap_cache: if size fits
36889699605SNick Piggin 	 * into that hole, we want to scan from vstart to reuse
36989699605SNick Piggin 	 * the hole instead of allocating above free_vmap_cache.
37089699605SNick Piggin 	 * Note that __free_vmap_area may update free_vmap_cache
37189699605SNick Piggin 	 * without updating cached_hole_size or cached_align.
37289699605SNick Piggin 	 */
37389699605SNick Piggin 	if (!free_vmap_cache ||
37489699605SNick Piggin 			size < cached_hole_size ||
37589699605SNick Piggin 			vstart < cached_vstart ||
37689699605SNick Piggin 			align < cached_align) {
37789699605SNick Piggin nocache:
37889699605SNick Piggin 		cached_hole_size = 0;
37989699605SNick Piggin 		free_vmap_cache = NULL;
38089699605SNick Piggin 	}
38189699605SNick Piggin 	/* record if we encounter less permissive parameters */
38289699605SNick Piggin 	cached_vstart = vstart;
38389699605SNick Piggin 	cached_align = align;
38489699605SNick Piggin 
38589699605SNick Piggin 	/* find starting point for our search */
38689699605SNick Piggin 	if (free_vmap_cache) {
38789699605SNick Piggin 		first = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
388248ac0e1SJohannes Weiner 		addr = ALIGN(first->va_end, align);
38989699605SNick Piggin 		if (addr < vstart)
39089699605SNick Piggin 			goto nocache;
3917766970cSNick Piggin 		if (addr + size - 1 < addr)
3927766970cSNick Piggin 			goto overflow;
3937766970cSNick Piggin 
39489699605SNick Piggin 	} else {
39589699605SNick Piggin 		addr = ALIGN(vstart, align);
39689699605SNick Piggin 		if (addr + size - 1 < addr)
39789699605SNick Piggin 			goto overflow;
398db64fe02SNick Piggin 
39989699605SNick Piggin 		n = vmap_area_root.rb_node;
40089699605SNick Piggin 		first = NULL;
40189699605SNick Piggin 
40289699605SNick Piggin 		while (n) {
403db64fe02SNick Piggin 			struct vmap_area *tmp;
404db64fe02SNick Piggin 			tmp = rb_entry(n, struct vmap_area, rb_node);
405db64fe02SNick Piggin 			if (tmp->va_end >= addr) {
406db64fe02SNick Piggin 				first = tmp;
40789699605SNick Piggin 				if (tmp->va_start <= addr)
40889699605SNick Piggin 					break;
409db64fe02SNick Piggin 				n = n->rb_left;
41089699605SNick Piggin 			} else
411db64fe02SNick Piggin 				n = n->rb_right;
412db64fe02SNick Piggin 		}
413db64fe02SNick Piggin 
414db64fe02SNick Piggin 		if (!first)
415db64fe02SNick Piggin 			goto found;
416db64fe02SNick Piggin 	}
417db64fe02SNick Piggin 
41889699605SNick Piggin 	/* from the starting point, walk areas until a suitable hole is found */
419248ac0e1SJohannes Weiner 	while (addr + size > first->va_start && addr + size <= vend) {
42089699605SNick Piggin 		if (addr + cached_hole_size < first->va_start)
42189699605SNick Piggin 			cached_hole_size = first->va_start - addr;
422248ac0e1SJohannes Weiner 		addr = ALIGN(first->va_end, align);
4237766970cSNick Piggin 		if (addr + size - 1 < addr)
4247766970cSNick Piggin 			goto overflow;
425db64fe02SNick Piggin 
42692ca922fSHong zhi guo 		if (list_is_last(&first->list, &vmap_area_list))
427db64fe02SNick Piggin 			goto found;
42892ca922fSHong zhi guo 
42992ca922fSHong zhi guo 		first = list_entry(first->list.next,
43092ca922fSHong zhi guo 				struct vmap_area, list);
431db64fe02SNick Piggin 	}
43289699605SNick Piggin 
433db64fe02SNick Piggin found:
43489699605SNick Piggin 	if (addr + size > vend)
43589699605SNick Piggin 		goto overflow;
43689699605SNick Piggin 
43789699605SNick Piggin 	va->va_start = addr;
43889699605SNick Piggin 	va->va_end = addr + size;
43989699605SNick Piggin 	va->flags = 0;
44089699605SNick Piggin 	__insert_vmap_area(va);
44189699605SNick Piggin 	free_vmap_cache = &va->rb_node;
44289699605SNick Piggin 	spin_unlock(&vmap_area_lock);
44389699605SNick Piggin 
44489699605SNick Piggin 	BUG_ON(va->va_start & (align-1));
44589699605SNick Piggin 	BUG_ON(va->va_start < vstart);
44689699605SNick Piggin 	BUG_ON(va->va_end > vend);
44789699605SNick Piggin 
44889699605SNick Piggin 	return va;
44989699605SNick Piggin 
4507766970cSNick Piggin overflow:
451db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
452db64fe02SNick Piggin 	if (!purged) {
453db64fe02SNick Piggin 		purge_vmap_area_lazy();
454db64fe02SNick Piggin 		purged = 1;
455db64fe02SNick Piggin 		goto retry;
456db64fe02SNick Piggin 	}
457db64fe02SNick Piggin 	if (printk_ratelimit())
458c1279c4eSGlauber Costa 		printk(KERN_WARNING
459c1279c4eSGlauber Costa 			"vmap allocation for size %lu failed: "
460c1279c4eSGlauber Costa 			"use vmalloc=<size> to increase size.\n", size);
4612498ce42SRalph Wuerthner 	kfree(va);
462db64fe02SNick Piggin 	return ERR_PTR(-EBUSY);
463db64fe02SNick Piggin }
464db64fe02SNick Piggin 
465db64fe02SNick Piggin static void __free_vmap_area(struct vmap_area *va)
466db64fe02SNick Piggin {
467db64fe02SNick Piggin 	BUG_ON(RB_EMPTY_NODE(&va->rb_node));
46889699605SNick Piggin 
46989699605SNick Piggin 	if (free_vmap_cache) {
47089699605SNick Piggin 		if (va->va_end < cached_vstart) {
47189699605SNick Piggin 			free_vmap_cache = NULL;
47289699605SNick Piggin 		} else {
47389699605SNick Piggin 			struct vmap_area *cache;
47489699605SNick Piggin 			cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
47589699605SNick Piggin 			if (va->va_start <= cache->va_start) {
47689699605SNick Piggin 				free_vmap_cache = rb_prev(&va->rb_node);
47789699605SNick Piggin 				/*
47889699605SNick Piggin 				 * We don't try to update cached_hole_size or
47989699605SNick Piggin 				 * cached_align, but it won't go very wrong.
48089699605SNick Piggin 				 */
48189699605SNick Piggin 			}
48289699605SNick Piggin 		}
48389699605SNick Piggin 	}
484db64fe02SNick Piggin 	rb_erase(&va->rb_node, &vmap_area_root);
485db64fe02SNick Piggin 	RB_CLEAR_NODE(&va->rb_node);
486db64fe02SNick Piggin 	list_del_rcu(&va->list);
487db64fe02SNick Piggin 
488ca23e405STejun Heo 	/*
489ca23e405STejun Heo 	 * Track the highest possible candidate for pcpu area
490ca23e405STejun Heo 	 * allocation.  Areas outside of vmalloc area can be returned
491ca23e405STejun Heo 	 * here too, consider only end addresses which fall inside
492ca23e405STejun Heo 	 * vmalloc area proper.
493ca23e405STejun Heo 	 */
494ca23e405STejun Heo 	if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
495ca23e405STejun Heo 		vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
496ca23e405STejun Heo 
49714769de9SLai Jiangshan 	kfree_rcu(va, rcu_head);
498db64fe02SNick Piggin }
499db64fe02SNick Piggin 
500db64fe02SNick Piggin /*
501db64fe02SNick Piggin  * Free a region of KVA allocated by alloc_vmap_area
502db64fe02SNick Piggin  */
503db64fe02SNick Piggin static void free_vmap_area(struct vmap_area *va)
504db64fe02SNick Piggin {
505db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
506db64fe02SNick Piggin 	__free_vmap_area(va);
507db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
508db64fe02SNick Piggin }
509db64fe02SNick Piggin 
510db64fe02SNick Piggin /*
511db64fe02SNick Piggin  * Clear the pagetable entries of a given vmap_area
512db64fe02SNick Piggin  */
513db64fe02SNick Piggin static void unmap_vmap_area(struct vmap_area *va)
514db64fe02SNick Piggin {
515db64fe02SNick Piggin 	vunmap_page_range(va->va_start, va->va_end);
516db64fe02SNick Piggin }
517db64fe02SNick Piggin 
518cd52858cSNick Piggin static void vmap_debug_free_range(unsigned long start, unsigned long end)
519cd52858cSNick Piggin {
520cd52858cSNick Piggin 	/*
521cd52858cSNick Piggin 	 * Unmap page tables and force a TLB flush immediately if
522cd52858cSNick Piggin 	 * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
523cd52858cSNick Piggin 	 * bugs similarly to those in linear kernel virtual address
524cd52858cSNick Piggin 	 * space after a page has been freed.
525cd52858cSNick Piggin 	 *
526cd52858cSNick Piggin 	 * All the lazy freeing logic is still retained, in order to
527cd52858cSNick Piggin 	 * minimise intrusiveness of this debugging feature.
528cd52858cSNick Piggin 	 *
529cd52858cSNick Piggin 	 * This is going to be *slow* (linear kernel virtual address
530cd52858cSNick Piggin 	 * debugging doesn't do a broadcast TLB flush so it is a lot
531cd52858cSNick Piggin 	 * faster).
532cd52858cSNick Piggin 	 */
533cd52858cSNick Piggin #ifdef CONFIG_DEBUG_PAGEALLOC
534cd52858cSNick Piggin 	vunmap_page_range(start, end);
535cd52858cSNick Piggin 	flush_tlb_kernel_range(start, end);
536cd52858cSNick Piggin #endif
537cd52858cSNick Piggin }
538cd52858cSNick Piggin 
539db64fe02SNick Piggin /*
540db64fe02SNick Piggin  * lazy_max_pages is the maximum amount of virtual address space we gather up
541db64fe02SNick Piggin  * before attempting to purge with a TLB flush.
542db64fe02SNick Piggin  *
543db64fe02SNick Piggin  * There is a tradeoff here: a larger number will cover more kernel page tables
544db64fe02SNick Piggin  * and take slightly longer to purge, but it will linearly reduce the number of
545db64fe02SNick Piggin  * global TLB flushes that must be performed. It would seem natural to scale
546db64fe02SNick Piggin  * this number up linearly with the number of CPUs (because vmapping activity
547db64fe02SNick Piggin  * could also scale linearly with the number of CPUs), however it is likely
548db64fe02SNick Piggin  * that in practice, workloads might be constrained in other ways that mean
549db64fe02SNick Piggin  * vmap activity will not scale linearly with CPUs. Also, I want to be
550db64fe02SNick Piggin  * conservative and not introduce a big latency on huge systems, so go with
551db64fe02SNick Piggin  * a less aggressive log scale. It will still be an improvement over the old
552db64fe02SNick Piggin  * code, and it will be simple to change the scale factor if we find that it
553db64fe02SNick Piggin  * becomes a problem on bigger systems.
554db64fe02SNick Piggin  */
555db64fe02SNick Piggin static unsigned long lazy_max_pages(void)
556db64fe02SNick Piggin {
557db64fe02SNick Piggin 	unsigned int log;
558db64fe02SNick Piggin 
559db64fe02SNick Piggin 	log = fls(num_online_cpus());
560db64fe02SNick Piggin 
561db64fe02SNick Piggin 	return log * (32UL * 1024 * 1024 / PAGE_SIZE);
562db64fe02SNick Piggin }
563db64fe02SNick Piggin 
564db64fe02SNick Piggin static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
565db64fe02SNick Piggin 
56602b709dfSNick Piggin /* for per-CPU blocks */
56702b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void);
56802b709dfSNick Piggin 
569db64fe02SNick Piggin /*
5703ee48b6aSCliff Wickman  * called before a call to iounmap() if the caller wants vm_area_struct's
5713ee48b6aSCliff Wickman  * immediately freed.
5723ee48b6aSCliff Wickman  */
5733ee48b6aSCliff Wickman void set_iounmap_nonlazy(void)
5743ee48b6aSCliff Wickman {
5753ee48b6aSCliff Wickman 	atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
5763ee48b6aSCliff Wickman }
5773ee48b6aSCliff Wickman 
5783ee48b6aSCliff Wickman /*
579db64fe02SNick Piggin  * Purges all lazily-freed vmap areas.
580db64fe02SNick Piggin  *
581db64fe02SNick Piggin  * If sync is 0 then don't purge if there is already a purge in progress.
582db64fe02SNick Piggin  * If force_flush is 1, then flush kernel TLBs between *start and *end even
583db64fe02SNick Piggin  * if we found no lazy vmap areas to unmap (callers can use this to optimise
584db64fe02SNick Piggin  * their own TLB flushing).
585db64fe02SNick Piggin  * Returns with *start = min(*start, lowest purged address)
586db64fe02SNick Piggin  *              *end = max(*end, highest purged address)
587db64fe02SNick Piggin  */
588db64fe02SNick Piggin static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
589db64fe02SNick Piggin 					int sync, int force_flush)
590db64fe02SNick Piggin {
59146666d8aSAndrew Morton 	static DEFINE_SPINLOCK(purge_lock);
592db64fe02SNick Piggin 	LIST_HEAD(valist);
593db64fe02SNick Piggin 	struct vmap_area *va;
594cbb76676SVegard Nossum 	struct vmap_area *n_va;
595db64fe02SNick Piggin 	int nr = 0;
596db64fe02SNick Piggin 
597db64fe02SNick Piggin 	/*
598db64fe02SNick Piggin 	 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
599db64fe02SNick Piggin 	 * should not expect such behaviour. This just simplifies locking for
600db64fe02SNick Piggin 	 * the case that isn't actually used at the moment anyway.
601db64fe02SNick Piggin 	 */
602db64fe02SNick Piggin 	if (!sync && !force_flush) {
60346666d8aSAndrew Morton 		if (!spin_trylock(&purge_lock))
604db64fe02SNick Piggin 			return;
605db64fe02SNick Piggin 	} else
60646666d8aSAndrew Morton 		spin_lock(&purge_lock);
607db64fe02SNick Piggin 
60802b709dfSNick Piggin 	if (sync)
60902b709dfSNick Piggin 		purge_fragmented_blocks_allcpus();
61002b709dfSNick Piggin 
611db64fe02SNick Piggin 	rcu_read_lock();
612db64fe02SNick Piggin 	list_for_each_entry_rcu(va, &vmap_area_list, list) {
613db64fe02SNick Piggin 		if (va->flags & VM_LAZY_FREE) {
614db64fe02SNick Piggin 			if (va->va_start < *start)
615db64fe02SNick Piggin 				*start = va->va_start;
616db64fe02SNick Piggin 			if (va->va_end > *end)
617db64fe02SNick Piggin 				*end = va->va_end;
618db64fe02SNick Piggin 			nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
619db64fe02SNick Piggin 			list_add_tail(&va->purge_list, &valist);
620db64fe02SNick Piggin 			va->flags |= VM_LAZY_FREEING;
621db64fe02SNick Piggin 			va->flags &= ~VM_LAZY_FREE;
622db64fe02SNick Piggin 		}
623db64fe02SNick Piggin 	}
624db64fe02SNick Piggin 	rcu_read_unlock();
625db64fe02SNick Piggin 
62688f50044SYongseok Koh 	if (nr)
627db64fe02SNick Piggin 		atomic_sub(nr, &vmap_lazy_nr);
628db64fe02SNick Piggin 
629db64fe02SNick Piggin 	if (nr || force_flush)
630db64fe02SNick Piggin 		flush_tlb_kernel_range(*start, *end);
631db64fe02SNick Piggin 
632db64fe02SNick Piggin 	if (nr) {
633db64fe02SNick Piggin 		spin_lock(&vmap_area_lock);
634cbb76676SVegard Nossum 		list_for_each_entry_safe(va, n_va, &valist, purge_list)
635db64fe02SNick Piggin 			__free_vmap_area(va);
636db64fe02SNick Piggin 		spin_unlock(&vmap_area_lock);
637db64fe02SNick Piggin 	}
63846666d8aSAndrew Morton 	spin_unlock(&purge_lock);
639db64fe02SNick Piggin }
640db64fe02SNick Piggin 
641db64fe02SNick Piggin /*
642496850e5SNick Piggin  * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
643496850e5SNick Piggin  * is already purging.
644496850e5SNick Piggin  */
645496850e5SNick Piggin static void try_purge_vmap_area_lazy(void)
646496850e5SNick Piggin {
647496850e5SNick Piggin 	unsigned long start = ULONG_MAX, end = 0;
648496850e5SNick Piggin 
649496850e5SNick Piggin 	__purge_vmap_area_lazy(&start, &end, 0, 0);
650496850e5SNick Piggin }
651496850e5SNick Piggin 
652496850e5SNick Piggin /*
653db64fe02SNick Piggin  * Kick off a purge of the outstanding lazy areas.
654db64fe02SNick Piggin  */
655db64fe02SNick Piggin static void purge_vmap_area_lazy(void)
656db64fe02SNick Piggin {
657db64fe02SNick Piggin 	unsigned long start = ULONG_MAX, end = 0;
658db64fe02SNick Piggin 
659496850e5SNick Piggin 	__purge_vmap_area_lazy(&start, &end, 1, 0);
660db64fe02SNick Piggin }
661db64fe02SNick Piggin 
662db64fe02SNick Piggin /*
66364141da5SJeremy Fitzhardinge  * Free a vmap area, caller ensuring that the area has been unmapped
66464141da5SJeremy Fitzhardinge  * and flush_cache_vunmap had been called for the correct range
66564141da5SJeremy Fitzhardinge  * previously.
666db64fe02SNick Piggin  */
66764141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va)
668db64fe02SNick Piggin {
669db64fe02SNick Piggin 	va->flags |= VM_LAZY_FREE;
670db64fe02SNick Piggin 	atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
671db64fe02SNick Piggin 	if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
672496850e5SNick Piggin 		try_purge_vmap_area_lazy();
673db64fe02SNick Piggin }
674db64fe02SNick Piggin 
675b29acbdcSNick Piggin /*
67664141da5SJeremy Fitzhardinge  * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
67764141da5SJeremy Fitzhardinge  * called for the correct range previously.
67864141da5SJeremy Fitzhardinge  */
67964141da5SJeremy Fitzhardinge static void free_unmap_vmap_area_noflush(struct vmap_area *va)
68064141da5SJeremy Fitzhardinge {
68164141da5SJeremy Fitzhardinge 	unmap_vmap_area(va);
68264141da5SJeremy Fitzhardinge 	free_vmap_area_noflush(va);
68364141da5SJeremy Fitzhardinge }
68464141da5SJeremy Fitzhardinge 
68564141da5SJeremy Fitzhardinge /*
686b29acbdcSNick Piggin  * Free and unmap a vmap area
687b29acbdcSNick Piggin  */
688b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va)
689b29acbdcSNick Piggin {
690b29acbdcSNick Piggin 	flush_cache_vunmap(va->va_start, va->va_end);
691b29acbdcSNick Piggin 	free_unmap_vmap_area_noflush(va);
692b29acbdcSNick Piggin }
693b29acbdcSNick Piggin 
694db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr)
695db64fe02SNick Piggin {
696db64fe02SNick Piggin 	struct vmap_area *va;
697db64fe02SNick Piggin 
698db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
699db64fe02SNick Piggin 	va = __find_vmap_area(addr);
700db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
701db64fe02SNick Piggin 
702db64fe02SNick Piggin 	return va;
703db64fe02SNick Piggin }
704db64fe02SNick Piggin 
705db64fe02SNick Piggin static void free_unmap_vmap_area_addr(unsigned long addr)
706db64fe02SNick Piggin {
707db64fe02SNick Piggin 	struct vmap_area *va;
708db64fe02SNick Piggin 
709db64fe02SNick Piggin 	va = find_vmap_area(addr);
710db64fe02SNick Piggin 	BUG_ON(!va);
711db64fe02SNick Piggin 	free_unmap_vmap_area(va);
712db64fe02SNick Piggin }
713db64fe02SNick Piggin 
714db64fe02SNick Piggin 
715db64fe02SNick Piggin /*** Per cpu kva allocator ***/
716db64fe02SNick Piggin 
717db64fe02SNick Piggin /*
718db64fe02SNick Piggin  * vmap space is limited especially on 32 bit architectures. Ensure there is
719db64fe02SNick Piggin  * room for at least 16 percpu vmap blocks per CPU.
720db64fe02SNick Piggin  */
721db64fe02SNick Piggin /*
722db64fe02SNick Piggin  * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
723db64fe02SNick Piggin  * to #define VMALLOC_SPACE		(VMALLOC_END-VMALLOC_START). Guess
724db64fe02SNick Piggin  * instead (we just need a rough idea)
725db64fe02SNick Piggin  */
726db64fe02SNick Piggin #if BITS_PER_LONG == 32
727db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024)
728db64fe02SNick Piggin #else
729db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024*1024)
730db64fe02SNick Piggin #endif
731db64fe02SNick Piggin 
732db64fe02SNick Piggin #define VMALLOC_PAGES		(VMALLOC_SPACE / PAGE_SIZE)
733db64fe02SNick Piggin #define VMAP_MAX_ALLOC		BITS_PER_LONG	/* 256K with 4K pages */
734db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX	1024	/* 4MB with 4K pages */
735db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN	(VMAP_MAX_ALLOC*2)
736db64fe02SNick Piggin #define VMAP_MIN(x, y)		((x) < (y) ? (x) : (y)) /* can't use min() */
737db64fe02SNick Piggin #define VMAP_MAX(x, y)		((x) > (y) ? (x) : (y)) /* can't use max() */
738f982f915SClemens Ladisch #define VMAP_BBMAP_BITS		\
739f982f915SClemens Ladisch 		VMAP_MIN(VMAP_BBMAP_BITS_MAX,	\
740db64fe02SNick Piggin 		VMAP_MAX(VMAP_BBMAP_BITS_MIN,	\
741f982f915SClemens Ladisch 			VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
742db64fe02SNick Piggin 
743db64fe02SNick Piggin #define VMAP_BLOCK_SIZE		(VMAP_BBMAP_BITS * PAGE_SIZE)
744db64fe02SNick Piggin 
7459b463334SJeremy Fitzhardinge static bool vmap_initialized __read_mostly = false;
7469b463334SJeremy Fitzhardinge 
747db64fe02SNick Piggin struct vmap_block_queue {
748db64fe02SNick Piggin 	spinlock_t lock;
749db64fe02SNick Piggin 	struct list_head free;
750db64fe02SNick Piggin };
751db64fe02SNick Piggin 
752db64fe02SNick Piggin struct vmap_block {
753db64fe02SNick Piggin 	spinlock_t lock;
754db64fe02SNick Piggin 	struct vmap_area *va;
755db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
756db64fe02SNick Piggin 	unsigned long free, dirty;
757db64fe02SNick Piggin 	DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
758db64fe02SNick Piggin 	DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
759db64fe02SNick Piggin 	struct list_head free_list;
760db64fe02SNick Piggin 	struct rcu_head rcu_head;
76102b709dfSNick Piggin 	struct list_head purge;
762db64fe02SNick Piggin };
763db64fe02SNick Piggin 
764db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
765db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
766db64fe02SNick Piggin 
767db64fe02SNick Piggin /*
768db64fe02SNick Piggin  * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
769db64fe02SNick Piggin  * in the free path. Could get rid of this if we change the API to return a
770db64fe02SNick Piggin  * "cookie" from alloc, to be passed to free. But no big deal yet.
771db64fe02SNick Piggin  */
772db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_block_tree_lock);
773db64fe02SNick Piggin static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
774db64fe02SNick Piggin 
775db64fe02SNick Piggin /*
776db64fe02SNick Piggin  * We should probably have a fallback mechanism to allocate virtual memory
777db64fe02SNick Piggin  * out of partially filled vmap blocks. However vmap block sizing should be
778db64fe02SNick Piggin  * fairly reasonable according to the vmalloc size, so it shouldn't be a
779db64fe02SNick Piggin  * big problem.
780db64fe02SNick Piggin  */
781db64fe02SNick Piggin 
782db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr)
783db64fe02SNick Piggin {
784db64fe02SNick Piggin 	addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
785db64fe02SNick Piggin 	addr /= VMAP_BLOCK_SIZE;
786db64fe02SNick Piggin 	return addr;
787db64fe02SNick Piggin }
788db64fe02SNick Piggin 
789db64fe02SNick Piggin static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
790db64fe02SNick Piggin {
791db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
792db64fe02SNick Piggin 	struct vmap_block *vb;
793db64fe02SNick Piggin 	struct vmap_area *va;
794db64fe02SNick Piggin 	unsigned long vb_idx;
795db64fe02SNick Piggin 	int node, err;
796db64fe02SNick Piggin 
797db64fe02SNick Piggin 	node = numa_node_id();
798db64fe02SNick Piggin 
799db64fe02SNick Piggin 	vb = kmalloc_node(sizeof(struct vmap_block),
800db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
801db64fe02SNick Piggin 	if (unlikely(!vb))
802db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
803db64fe02SNick Piggin 
804db64fe02SNick Piggin 	va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
805db64fe02SNick Piggin 					VMALLOC_START, VMALLOC_END,
806db64fe02SNick Piggin 					node, gfp_mask);
807ddf9c6d4STobias Klauser 	if (IS_ERR(va)) {
808db64fe02SNick Piggin 		kfree(vb);
809e7d86340SJulia Lawall 		return ERR_CAST(va);
810db64fe02SNick Piggin 	}
811db64fe02SNick Piggin 
812db64fe02SNick Piggin 	err = radix_tree_preload(gfp_mask);
813db64fe02SNick Piggin 	if (unlikely(err)) {
814db64fe02SNick Piggin 		kfree(vb);
815db64fe02SNick Piggin 		free_vmap_area(va);
816db64fe02SNick Piggin 		return ERR_PTR(err);
817db64fe02SNick Piggin 	}
818db64fe02SNick Piggin 
819db64fe02SNick Piggin 	spin_lock_init(&vb->lock);
820db64fe02SNick Piggin 	vb->va = va;
821db64fe02SNick Piggin 	vb->free = VMAP_BBMAP_BITS;
822db64fe02SNick Piggin 	vb->dirty = 0;
823db64fe02SNick Piggin 	bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
824db64fe02SNick Piggin 	bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
825db64fe02SNick Piggin 	INIT_LIST_HEAD(&vb->free_list);
826db64fe02SNick Piggin 
827db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(va->va_start);
828db64fe02SNick Piggin 	spin_lock(&vmap_block_tree_lock);
829db64fe02SNick Piggin 	err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
830db64fe02SNick Piggin 	spin_unlock(&vmap_block_tree_lock);
831db64fe02SNick Piggin 	BUG_ON(err);
832db64fe02SNick Piggin 	radix_tree_preload_end();
833db64fe02SNick Piggin 
834db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
835db64fe02SNick Piggin 	vb->vbq = vbq;
836db64fe02SNick Piggin 	spin_lock(&vbq->lock);
837de560423SNick Piggin 	list_add_rcu(&vb->free_list, &vbq->free);
838db64fe02SNick Piggin 	spin_unlock(&vbq->lock);
8393f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
840db64fe02SNick Piggin 
841db64fe02SNick Piggin 	return vb;
842db64fe02SNick Piggin }
843db64fe02SNick Piggin 
844db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb)
845db64fe02SNick Piggin {
846db64fe02SNick Piggin 	struct vmap_block *tmp;
847db64fe02SNick Piggin 	unsigned long vb_idx;
848db64fe02SNick Piggin 
849db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(vb->va->va_start);
850db64fe02SNick Piggin 	spin_lock(&vmap_block_tree_lock);
851db64fe02SNick Piggin 	tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
852db64fe02SNick Piggin 	spin_unlock(&vmap_block_tree_lock);
853db64fe02SNick Piggin 	BUG_ON(tmp != vb);
854db64fe02SNick Piggin 
85564141da5SJeremy Fitzhardinge 	free_vmap_area_noflush(vb->va);
85622a3c7d1SLai Jiangshan 	kfree_rcu(vb, rcu_head);
857db64fe02SNick Piggin }
858db64fe02SNick Piggin 
85902b709dfSNick Piggin static void purge_fragmented_blocks(int cpu)
86002b709dfSNick Piggin {
86102b709dfSNick Piggin 	LIST_HEAD(purge);
86202b709dfSNick Piggin 	struct vmap_block *vb;
86302b709dfSNick Piggin 	struct vmap_block *n_vb;
86402b709dfSNick Piggin 	struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
86502b709dfSNick Piggin 
86602b709dfSNick Piggin 	rcu_read_lock();
86702b709dfSNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
86802b709dfSNick Piggin 
86902b709dfSNick Piggin 		if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
87002b709dfSNick Piggin 			continue;
87102b709dfSNick Piggin 
87202b709dfSNick Piggin 		spin_lock(&vb->lock);
87302b709dfSNick Piggin 		if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
87402b709dfSNick Piggin 			vb->free = 0; /* prevent further allocs after releasing lock */
87502b709dfSNick Piggin 			vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
87602b709dfSNick Piggin 			bitmap_fill(vb->alloc_map, VMAP_BBMAP_BITS);
87702b709dfSNick Piggin 			bitmap_fill(vb->dirty_map, VMAP_BBMAP_BITS);
87802b709dfSNick Piggin 			spin_lock(&vbq->lock);
87902b709dfSNick Piggin 			list_del_rcu(&vb->free_list);
88002b709dfSNick Piggin 			spin_unlock(&vbq->lock);
88102b709dfSNick Piggin 			spin_unlock(&vb->lock);
88202b709dfSNick Piggin 			list_add_tail(&vb->purge, &purge);
88302b709dfSNick Piggin 		} else
88402b709dfSNick Piggin 			spin_unlock(&vb->lock);
88502b709dfSNick Piggin 	}
88602b709dfSNick Piggin 	rcu_read_unlock();
88702b709dfSNick Piggin 
88802b709dfSNick Piggin 	list_for_each_entry_safe(vb, n_vb, &purge, purge) {
88902b709dfSNick Piggin 		list_del(&vb->purge);
89002b709dfSNick Piggin 		free_vmap_block(vb);
89102b709dfSNick Piggin 	}
89202b709dfSNick Piggin }
89302b709dfSNick Piggin 
89402b709dfSNick Piggin static void purge_fragmented_blocks_thiscpu(void)
89502b709dfSNick Piggin {
89602b709dfSNick Piggin 	purge_fragmented_blocks(smp_processor_id());
89702b709dfSNick Piggin }
89802b709dfSNick Piggin 
89902b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void)
90002b709dfSNick Piggin {
90102b709dfSNick Piggin 	int cpu;
90202b709dfSNick Piggin 
90302b709dfSNick Piggin 	for_each_possible_cpu(cpu)
90402b709dfSNick Piggin 		purge_fragmented_blocks(cpu);
90502b709dfSNick Piggin }
90602b709dfSNick Piggin 
907db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
908db64fe02SNick Piggin {
909db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
910db64fe02SNick Piggin 	struct vmap_block *vb;
911db64fe02SNick Piggin 	unsigned long addr = 0;
912db64fe02SNick Piggin 	unsigned int order;
91302b709dfSNick Piggin 	int purge = 0;
914db64fe02SNick Piggin 
915db64fe02SNick Piggin 	BUG_ON(size & ~PAGE_MASK);
916db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
917aa91c4d8SJan Kara 	if (WARN_ON(size == 0)) {
918aa91c4d8SJan Kara 		/*
919aa91c4d8SJan Kara 		 * Allocating 0 bytes isn't what caller wants since
920aa91c4d8SJan Kara 		 * get_order(0) returns funny result. Just warn and terminate
921aa91c4d8SJan Kara 		 * early.
922aa91c4d8SJan Kara 		 */
923aa91c4d8SJan Kara 		return NULL;
924aa91c4d8SJan Kara 	}
925db64fe02SNick Piggin 	order = get_order(size);
926db64fe02SNick Piggin 
927db64fe02SNick Piggin again:
928db64fe02SNick Piggin 	rcu_read_lock();
929db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
930db64fe02SNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
931db64fe02SNick Piggin 		int i;
932db64fe02SNick Piggin 
933db64fe02SNick Piggin 		spin_lock(&vb->lock);
93402b709dfSNick Piggin 		if (vb->free < 1UL << order)
93502b709dfSNick Piggin 			goto next;
93602b709dfSNick Piggin 
937db64fe02SNick Piggin 		i = bitmap_find_free_region(vb->alloc_map,
938db64fe02SNick Piggin 						VMAP_BBMAP_BITS, order);
939db64fe02SNick Piggin 
94002b709dfSNick Piggin 		if (i < 0) {
94102b709dfSNick Piggin 			if (vb->free + vb->dirty == VMAP_BBMAP_BITS) {
94202b709dfSNick Piggin 				/* fragmented and no outstanding allocations */
94302b709dfSNick Piggin 				BUG_ON(vb->dirty != VMAP_BBMAP_BITS);
94402b709dfSNick Piggin 				purge = 1;
94502b709dfSNick Piggin 			}
94602b709dfSNick Piggin 			goto next;
94702b709dfSNick Piggin 		}
948db64fe02SNick Piggin 		addr = vb->va->va_start + (i << PAGE_SHIFT);
949db64fe02SNick Piggin 		BUG_ON(addr_to_vb_idx(addr) !=
950db64fe02SNick Piggin 				addr_to_vb_idx(vb->va->va_start));
951db64fe02SNick Piggin 		vb->free -= 1UL << order;
952db64fe02SNick Piggin 		if (vb->free == 0) {
953db64fe02SNick Piggin 			spin_lock(&vbq->lock);
954de560423SNick Piggin 			list_del_rcu(&vb->free_list);
955db64fe02SNick Piggin 			spin_unlock(&vbq->lock);
956db64fe02SNick Piggin 		}
957db64fe02SNick Piggin 		spin_unlock(&vb->lock);
958db64fe02SNick Piggin 		break;
95902b709dfSNick Piggin next:
960db64fe02SNick Piggin 		spin_unlock(&vb->lock);
961db64fe02SNick Piggin 	}
96202b709dfSNick Piggin 
96302b709dfSNick Piggin 	if (purge)
96402b709dfSNick Piggin 		purge_fragmented_blocks_thiscpu();
96502b709dfSNick Piggin 
9663f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
967db64fe02SNick Piggin 	rcu_read_unlock();
968db64fe02SNick Piggin 
969db64fe02SNick Piggin 	if (!addr) {
970db64fe02SNick Piggin 		vb = new_vmap_block(gfp_mask);
971db64fe02SNick Piggin 		if (IS_ERR(vb))
972db64fe02SNick Piggin 			return vb;
973db64fe02SNick Piggin 		goto again;
974db64fe02SNick Piggin 	}
975db64fe02SNick Piggin 
976db64fe02SNick Piggin 	return (void *)addr;
977db64fe02SNick Piggin }
978db64fe02SNick Piggin 
979db64fe02SNick Piggin static void vb_free(const void *addr, unsigned long size)
980db64fe02SNick Piggin {
981db64fe02SNick Piggin 	unsigned long offset;
982db64fe02SNick Piggin 	unsigned long vb_idx;
983db64fe02SNick Piggin 	unsigned int order;
984db64fe02SNick Piggin 	struct vmap_block *vb;
985db64fe02SNick Piggin 
986db64fe02SNick Piggin 	BUG_ON(size & ~PAGE_MASK);
987db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
988b29acbdcSNick Piggin 
989b29acbdcSNick Piggin 	flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
990b29acbdcSNick Piggin 
991db64fe02SNick Piggin 	order = get_order(size);
992db64fe02SNick Piggin 
993db64fe02SNick Piggin 	offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
994db64fe02SNick Piggin 
995db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx((unsigned long)addr);
996db64fe02SNick Piggin 	rcu_read_lock();
997db64fe02SNick Piggin 	vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
998db64fe02SNick Piggin 	rcu_read_unlock();
999db64fe02SNick Piggin 	BUG_ON(!vb);
1000db64fe02SNick Piggin 
100164141da5SJeremy Fitzhardinge 	vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
100264141da5SJeremy Fitzhardinge 
1003db64fe02SNick Piggin 	spin_lock(&vb->lock);
1004de560423SNick Piggin 	BUG_ON(bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order));
1005d086817dSMinChan Kim 
1006db64fe02SNick Piggin 	vb->dirty += 1UL << order;
1007db64fe02SNick Piggin 	if (vb->dirty == VMAP_BBMAP_BITS) {
1008de560423SNick Piggin 		BUG_ON(vb->free);
1009db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1010db64fe02SNick Piggin 		free_vmap_block(vb);
1011db64fe02SNick Piggin 	} else
1012db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1013db64fe02SNick Piggin }
1014db64fe02SNick Piggin 
1015db64fe02SNick Piggin /**
1016db64fe02SNick Piggin  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1017db64fe02SNick Piggin  *
1018db64fe02SNick Piggin  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1019db64fe02SNick Piggin  * to amortize TLB flushing overheads. What this means is that any page you
1020db64fe02SNick Piggin  * have now, may, in a former life, have been mapped into kernel virtual
1021db64fe02SNick Piggin  * address by the vmap layer and so there might be some CPUs with TLB entries
1022db64fe02SNick Piggin  * still referencing that page (additional to the regular 1:1 kernel mapping).
1023db64fe02SNick Piggin  *
1024db64fe02SNick Piggin  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1025db64fe02SNick Piggin  * be sure that none of the pages we have control over will have any aliases
1026db64fe02SNick Piggin  * from the vmap layer.
1027db64fe02SNick Piggin  */
1028db64fe02SNick Piggin void vm_unmap_aliases(void)
1029db64fe02SNick Piggin {
1030db64fe02SNick Piggin 	unsigned long start = ULONG_MAX, end = 0;
1031db64fe02SNick Piggin 	int cpu;
1032db64fe02SNick Piggin 	int flush = 0;
1033db64fe02SNick Piggin 
10349b463334SJeremy Fitzhardinge 	if (unlikely(!vmap_initialized))
10359b463334SJeremy Fitzhardinge 		return;
10369b463334SJeremy Fitzhardinge 
1037db64fe02SNick Piggin 	for_each_possible_cpu(cpu) {
1038db64fe02SNick Piggin 		struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1039db64fe02SNick Piggin 		struct vmap_block *vb;
1040db64fe02SNick Piggin 
1041db64fe02SNick Piggin 		rcu_read_lock();
1042db64fe02SNick Piggin 		list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1043db64fe02SNick Piggin 			int i;
1044db64fe02SNick Piggin 
1045db64fe02SNick Piggin 			spin_lock(&vb->lock);
1046db64fe02SNick Piggin 			i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
1047db64fe02SNick Piggin 			while (i < VMAP_BBMAP_BITS) {
1048db64fe02SNick Piggin 				unsigned long s, e;
1049db64fe02SNick Piggin 				int j;
1050db64fe02SNick Piggin 				j = find_next_zero_bit(vb->dirty_map,
1051db64fe02SNick Piggin 					VMAP_BBMAP_BITS, i);
1052db64fe02SNick Piggin 
1053db64fe02SNick Piggin 				s = vb->va->va_start + (i << PAGE_SHIFT);
1054db64fe02SNick Piggin 				e = vb->va->va_start + (j << PAGE_SHIFT);
1055db64fe02SNick Piggin 				flush = 1;
1056db64fe02SNick Piggin 
1057db64fe02SNick Piggin 				if (s < start)
1058db64fe02SNick Piggin 					start = s;
1059db64fe02SNick Piggin 				if (e > end)
1060db64fe02SNick Piggin 					end = e;
1061db64fe02SNick Piggin 
1062db64fe02SNick Piggin 				i = j;
1063db64fe02SNick Piggin 				i = find_next_bit(vb->dirty_map,
1064db64fe02SNick Piggin 							VMAP_BBMAP_BITS, i);
1065db64fe02SNick Piggin 			}
1066db64fe02SNick Piggin 			spin_unlock(&vb->lock);
1067db64fe02SNick Piggin 		}
1068db64fe02SNick Piggin 		rcu_read_unlock();
1069db64fe02SNick Piggin 	}
1070db64fe02SNick Piggin 
1071db64fe02SNick Piggin 	__purge_vmap_area_lazy(&start, &end, 1, flush);
1072db64fe02SNick Piggin }
1073db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1074db64fe02SNick Piggin 
1075db64fe02SNick Piggin /**
1076db64fe02SNick Piggin  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1077db64fe02SNick Piggin  * @mem: the pointer returned by vm_map_ram
1078db64fe02SNick Piggin  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1079db64fe02SNick Piggin  */
1080db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count)
1081db64fe02SNick Piggin {
1082db64fe02SNick Piggin 	unsigned long size = count << PAGE_SHIFT;
1083db64fe02SNick Piggin 	unsigned long addr = (unsigned long)mem;
1084db64fe02SNick Piggin 
1085db64fe02SNick Piggin 	BUG_ON(!addr);
1086db64fe02SNick Piggin 	BUG_ON(addr < VMALLOC_START);
1087db64fe02SNick Piggin 	BUG_ON(addr > VMALLOC_END);
1088db64fe02SNick Piggin 	BUG_ON(addr & (PAGE_SIZE-1));
1089db64fe02SNick Piggin 
1090db64fe02SNick Piggin 	debug_check_no_locks_freed(mem, size);
1091cd52858cSNick Piggin 	vmap_debug_free_range(addr, addr+size);
1092db64fe02SNick Piggin 
1093db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC))
1094db64fe02SNick Piggin 		vb_free(mem, size);
1095db64fe02SNick Piggin 	else
1096db64fe02SNick Piggin 		free_unmap_vmap_area_addr(addr);
1097db64fe02SNick Piggin }
1098db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram);
1099db64fe02SNick Piggin 
1100db64fe02SNick Piggin /**
1101db64fe02SNick Piggin  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1102db64fe02SNick Piggin  * @pages: an array of pointers to the pages to be mapped
1103db64fe02SNick Piggin  * @count: number of pages
1104db64fe02SNick Piggin  * @node: prefer to allocate data structures on this node
1105db64fe02SNick Piggin  * @prot: memory protection to use. PAGE_KERNEL for regular RAM
1106e99c97adSRandy Dunlap  *
1107e99c97adSRandy Dunlap  * Returns: a pointer to the address that has been mapped, or %NULL on failure
1108db64fe02SNick Piggin  */
1109db64fe02SNick Piggin void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1110db64fe02SNick Piggin {
1111db64fe02SNick Piggin 	unsigned long size = count << PAGE_SHIFT;
1112db64fe02SNick Piggin 	unsigned long addr;
1113db64fe02SNick Piggin 	void *mem;
1114db64fe02SNick Piggin 
1115db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC)) {
1116db64fe02SNick Piggin 		mem = vb_alloc(size, GFP_KERNEL);
1117db64fe02SNick Piggin 		if (IS_ERR(mem))
1118db64fe02SNick Piggin 			return NULL;
1119db64fe02SNick Piggin 		addr = (unsigned long)mem;
1120db64fe02SNick Piggin 	} else {
1121db64fe02SNick Piggin 		struct vmap_area *va;
1122db64fe02SNick Piggin 		va = alloc_vmap_area(size, PAGE_SIZE,
1123db64fe02SNick Piggin 				VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1124db64fe02SNick Piggin 		if (IS_ERR(va))
1125db64fe02SNick Piggin 			return NULL;
1126db64fe02SNick Piggin 
1127db64fe02SNick Piggin 		addr = va->va_start;
1128db64fe02SNick Piggin 		mem = (void *)addr;
1129db64fe02SNick Piggin 	}
1130db64fe02SNick Piggin 	if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1131db64fe02SNick Piggin 		vm_unmap_ram(mem, count);
1132db64fe02SNick Piggin 		return NULL;
1133db64fe02SNick Piggin 	}
1134db64fe02SNick Piggin 	return mem;
1135db64fe02SNick Piggin }
1136db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram);
1137db64fe02SNick Piggin 
11384341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata;
1139f0aa6617STejun Heo /**
1140be9b7335SNicolas Pitre  * vm_area_add_early - add vmap area early during boot
1141be9b7335SNicolas Pitre  * @vm: vm_struct to add
1142be9b7335SNicolas Pitre  *
1143be9b7335SNicolas Pitre  * This function is used to add fixed kernel vm area to vmlist before
1144be9b7335SNicolas Pitre  * vmalloc_init() is called.  @vm->addr, @vm->size, and @vm->flags
1145be9b7335SNicolas Pitre  * should contain proper values and the other fields should be zero.
1146be9b7335SNicolas Pitre  *
1147be9b7335SNicolas Pitre  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1148be9b7335SNicolas Pitre  */
1149be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm)
1150be9b7335SNicolas Pitre {
1151be9b7335SNicolas Pitre 	struct vm_struct *tmp, **p;
1152be9b7335SNicolas Pitre 
1153be9b7335SNicolas Pitre 	BUG_ON(vmap_initialized);
1154be9b7335SNicolas Pitre 	for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1155be9b7335SNicolas Pitre 		if (tmp->addr >= vm->addr) {
1156be9b7335SNicolas Pitre 			BUG_ON(tmp->addr < vm->addr + vm->size);
1157be9b7335SNicolas Pitre 			break;
1158be9b7335SNicolas Pitre 		} else
1159be9b7335SNicolas Pitre 			BUG_ON(tmp->addr + tmp->size > vm->addr);
1160be9b7335SNicolas Pitre 	}
1161be9b7335SNicolas Pitre 	vm->next = *p;
1162be9b7335SNicolas Pitre 	*p = vm;
1163be9b7335SNicolas Pitre }
1164be9b7335SNicolas Pitre 
1165be9b7335SNicolas Pitre /**
1166f0aa6617STejun Heo  * vm_area_register_early - register vmap area early during boot
1167f0aa6617STejun Heo  * @vm: vm_struct to register
1168c0c0a293STejun Heo  * @align: requested alignment
1169f0aa6617STejun Heo  *
1170f0aa6617STejun Heo  * This function is used to register kernel vm area before
1171f0aa6617STejun Heo  * vmalloc_init() is called.  @vm->size and @vm->flags should contain
1172f0aa6617STejun Heo  * proper values on entry and other fields should be zero.  On return,
1173f0aa6617STejun Heo  * vm->addr contains the allocated address.
1174f0aa6617STejun Heo  *
1175f0aa6617STejun Heo  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1176f0aa6617STejun Heo  */
1177c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1178f0aa6617STejun Heo {
1179f0aa6617STejun Heo 	static size_t vm_init_off __initdata;
1180c0c0a293STejun Heo 	unsigned long addr;
1181f0aa6617STejun Heo 
1182c0c0a293STejun Heo 	addr = ALIGN(VMALLOC_START + vm_init_off, align);
1183c0c0a293STejun Heo 	vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1184c0c0a293STejun Heo 
1185c0c0a293STejun Heo 	vm->addr = (void *)addr;
1186f0aa6617STejun Heo 
1187be9b7335SNicolas Pitre 	vm_area_add_early(vm);
1188f0aa6617STejun Heo }
1189f0aa6617STejun Heo 
1190db64fe02SNick Piggin void __init vmalloc_init(void)
1191db64fe02SNick Piggin {
1192822c18f2SIvan Kokshaysky 	struct vmap_area *va;
1193822c18f2SIvan Kokshaysky 	struct vm_struct *tmp;
1194db64fe02SNick Piggin 	int i;
1195db64fe02SNick Piggin 
1196db64fe02SNick Piggin 	for_each_possible_cpu(i) {
1197db64fe02SNick Piggin 		struct vmap_block_queue *vbq;
119832fcfd40SAl Viro 		struct vfree_deferred *p;
1199db64fe02SNick Piggin 
1200db64fe02SNick Piggin 		vbq = &per_cpu(vmap_block_queue, i);
1201db64fe02SNick Piggin 		spin_lock_init(&vbq->lock);
1202db64fe02SNick Piggin 		INIT_LIST_HEAD(&vbq->free);
120332fcfd40SAl Viro 		p = &per_cpu(vfree_deferred, i);
120432fcfd40SAl Viro 		init_llist_head(&p->list);
120532fcfd40SAl Viro 		INIT_WORK(&p->wq, free_work);
1206db64fe02SNick Piggin 	}
12079b463334SJeremy Fitzhardinge 
1208822c18f2SIvan Kokshaysky 	/* Import existing vmlist entries. */
1209822c18f2SIvan Kokshaysky 	for (tmp = vmlist; tmp; tmp = tmp->next) {
121043ebdac4SPekka Enberg 		va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
1211dbda591dSKyongHo 		va->flags = VM_VM_AREA;
1212822c18f2SIvan Kokshaysky 		va->va_start = (unsigned long)tmp->addr;
1213822c18f2SIvan Kokshaysky 		va->va_end = va->va_start + tmp->size;
1214dbda591dSKyongHo 		va->vm = tmp;
1215822c18f2SIvan Kokshaysky 		__insert_vmap_area(va);
1216822c18f2SIvan Kokshaysky 	}
1217ca23e405STejun Heo 
1218ca23e405STejun Heo 	vmap_area_pcpu_hole = VMALLOC_END;
1219ca23e405STejun Heo 
12209b463334SJeremy Fitzhardinge 	vmap_initialized = true;
1221db64fe02SNick Piggin }
1222db64fe02SNick Piggin 
12238fc48985STejun Heo /**
12248fc48985STejun Heo  * map_kernel_range_noflush - map kernel VM area with the specified pages
12258fc48985STejun Heo  * @addr: start of the VM area to map
12268fc48985STejun Heo  * @size: size of the VM area to map
12278fc48985STejun Heo  * @prot: page protection flags to use
12288fc48985STejun Heo  * @pages: pages to map
12298fc48985STejun Heo  *
12308fc48985STejun Heo  * Map PFN_UP(@size) pages at @addr.  The VM area @addr and @size
12318fc48985STejun Heo  * specify should have been allocated using get_vm_area() and its
12328fc48985STejun Heo  * friends.
12338fc48985STejun Heo  *
12348fc48985STejun Heo  * NOTE:
12358fc48985STejun Heo  * This function does NOT do any cache flushing.  The caller is
12368fc48985STejun Heo  * responsible for calling flush_cache_vmap() on to-be-mapped areas
12378fc48985STejun Heo  * before calling this function.
12388fc48985STejun Heo  *
12398fc48985STejun Heo  * RETURNS:
12408fc48985STejun Heo  * The number of pages mapped on success, -errno on failure.
12418fc48985STejun Heo  */
12428fc48985STejun Heo int map_kernel_range_noflush(unsigned long addr, unsigned long size,
12438fc48985STejun Heo 			     pgprot_t prot, struct page **pages)
12448fc48985STejun Heo {
12458fc48985STejun Heo 	return vmap_page_range_noflush(addr, addr + size, prot, pages);
12468fc48985STejun Heo }
12478fc48985STejun Heo 
12488fc48985STejun Heo /**
12498fc48985STejun Heo  * unmap_kernel_range_noflush - unmap kernel VM area
12508fc48985STejun Heo  * @addr: start of the VM area to unmap
12518fc48985STejun Heo  * @size: size of the VM area to unmap
12528fc48985STejun Heo  *
12538fc48985STejun Heo  * Unmap PFN_UP(@size) pages at @addr.  The VM area @addr and @size
12548fc48985STejun Heo  * specify should have been allocated using get_vm_area() and its
12558fc48985STejun Heo  * friends.
12568fc48985STejun Heo  *
12578fc48985STejun Heo  * NOTE:
12588fc48985STejun Heo  * This function does NOT do any cache flushing.  The caller is
12598fc48985STejun Heo  * responsible for calling flush_cache_vunmap() on to-be-mapped areas
12608fc48985STejun Heo  * before calling this function and flush_tlb_kernel_range() after.
12618fc48985STejun Heo  */
12628fc48985STejun Heo void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
12638fc48985STejun Heo {
12648fc48985STejun Heo 	vunmap_page_range(addr, addr + size);
12658fc48985STejun Heo }
126681e88fdcSHuang Ying EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
12678fc48985STejun Heo 
12688fc48985STejun Heo /**
12698fc48985STejun Heo  * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
12708fc48985STejun Heo  * @addr: start of the VM area to unmap
12718fc48985STejun Heo  * @size: size of the VM area to unmap
12728fc48985STejun Heo  *
12738fc48985STejun Heo  * Similar to unmap_kernel_range_noflush() but flushes vcache before
12748fc48985STejun Heo  * the unmapping and tlb after.
12758fc48985STejun Heo  */
1276db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size)
1277db64fe02SNick Piggin {
1278db64fe02SNick Piggin 	unsigned long end = addr + size;
1279f6fcba70STejun Heo 
1280f6fcba70STejun Heo 	flush_cache_vunmap(addr, end);
1281db64fe02SNick Piggin 	vunmap_page_range(addr, end);
1282db64fe02SNick Piggin 	flush_tlb_kernel_range(addr, end);
1283db64fe02SNick Piggin }
1284db64fe02SNick Piggin 
1285db64fe02SNick Piggin int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
1286db64fe02SNick Piggin {
1287db64fe02SNick Piggin 	unsigned long addr = (unsigned long)area->addr;
1288db64fe02SNick Piggin 	unsigned long end = addr + area->size - PAGE_SIZE;
1289db64fe02SNick Piggin 	int err;
1290db64fe02SNick Piggin 
1291db64fe02SNick Piggin 	err = vmap_page_range(addr, end, prot, *pages);
1292db64fe02SNick Piggin 	if (err > 0) {
1293db64fe02SNick Piggin 		*pages += err;
1294db64fe02SNick Piggin 		err = 0;
1295db64fe02SNick Piggin 	}
1296db64fe02SNick Piggin 
1297db64fe02SNick Piggin 	return err;
1298db64fe02SNick Piggin }
1299db64fe02SNick Piggin EXPORT_SYMBOL_GPL(map_vm_area);
1300db64fe02SNick Piggin 
1301f5252e00SMitsuo Hayasaka static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
13025e6cafc8SMarek Szyprowski 			      unsigned long flags, const void *caller)
1303cf88c790STejun Heo {
1304c69480adSJoonsoo Kim 	spin_lock(&vmap_area_lock);
1305cf88c790STejun Heo 	vm->flags = flags;
1306cf88c790STejun Heo 	vm->addr = (void *)va->va_start;
1307cf88c790STejun Heo 	vm->size = va->va_end - va->va_start;
1308cf88c790STejun Heo 	vm->caller = caller;
1309db1aecafSMinchan Kim 	va->vm = vm;
1310cf88c790STejun Heo 	va->flags |= VM_VM_AREA;
1311c69480adSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
1312f5252e00SMitsuo Hayasaka }
1313cf88c790STejun Heo 
13144341fa45SJoonsoo Kim static void clear_vm_unlist(struct vm_struct *vm)
1315f5252e00SMitsuo Hayasaka {
1316d4033afdSJoonsoo Kim 	/*
1317d4033afdSJoonsoo Kim 	 * Before removing VM_UNLIST,
1318d4033afdSJoonsoo Kim 	 * we should make sure that vm has proper values.
1319d4033afdSJoonsoo Kim 	 * Pair with smp_rmb() in show_numa_info().
1320d4033afdSJoonsoo Kim 	 */
1321d4033afdSJoonsoo Kim 	smp_wmb();
1322f5252e00SMitsuo Hayasaka 	vm->flags &= ~VM_UNLIST;
1323cf88c790STejun Heo }
1324cf88c790STejun Heo 
1325f5252e00SMitsuo Hayasaka static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
13265e6cafc8SMarek Szyprowski 			      unsigned long flags, const void *caller)
1327f5252e00SMitsuo Hayasaka {
1328f5252e00SMitsuo Hayasaka 	setup_vmalloc_vm(vm, va, flags, caller);
13294341fa45SJoonsoo Kim 	clear_vm_unlist(vm);
1330f5252e00SMitsuo Hayasaka }
1331f5252e00SMitsuo Hayasaka 
1332db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size,
13332dca6999SDavid Miller 		unsigned long align, unsigned long flags, unsigned long start,
13345e6cafc8SMarek Szyprowski 		unsigned long end, int node, gfp_t gfp_mask, const void *caller)
1335db64fe02SNick Piggin {
13360006526dSKautuk Consul 	struct vmap_area *va;
1337db64fe02SNick Piggin 	struct vm_struct *area;
13381da177e4SLinus Torvalds 
133952fd24caSGiridhar Pemmasani 	BUG_ON(in_interrupt());
13401da177e4SLinus Torvalds 	if (flags & VM_IOREMAP) {
13411da177e4SLinus Torvalds 		int bit = fls(size);
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 		if (bit > IOREMAP_MAX_ORDER)
13441da177e4SLinus Torvalds 			bit = IOREMAP_MAX_ORDER;
13451da177e4SLinus Torvalds 		else if (bit < PAGE_SHIFT)
13461da177e4SLinus Torvalds 			bit = PAGE_SHIFT;
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 		align = 1ul << bit;
13491da177e4SLinus Torvalds 	}
1350db64fe02SNick Piggin 
13511da177e4SLinus Torvalds 	size = PAGE_ALIGN(size);
135231be8309SOGAWA Hirofumi 	if (unlikely(!size))
135331be8309SOGAWA Hirofumi 		return NULL;
13541da177e4SLinus Torvalds 
1355cf88c790STejun Heo 	area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
13561da177e4SLinus Torvalds 	if (unlikely(!area))
13571da177e4SLinus Torvalds 		return NULL;
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	/*
13601da177e4SLinus Torvalds 	 * We always allocate a guard page.
13611da177e4SLinus Torvalds 	 */
13621da177e4SLinus Torvalds 	size += PAGE_SIZE;
13631da177e4SLinus Torvalds 
1364db64fe02SNick Piggin 	va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1365db64fe02SNick Piggin 	if (IS_ERR(va)) {
1366db64fe02SNick Piggin 		kfree(area);
1367db64fe02SNick Piggin 		return NULL;
13681da177e4SLinus Torvalds 	}
13691da177e4SLinus Torvalds 
1370f5252e00SMitsuo Hayasaka 	/*
1371f5252e00SMitsuo Hayasaka 	 * When this function is called from __vmalloc_node_range,
13724341fa45SJoonsoo Kim 	 * we add VM_UNLIST flag to avoid accessing uninitialized
13734341fa45SJoonsoo Kim 	 * members of vm_struct such as pages and nr_pages fields.
13744341fa45SJoonsoo Kim 	 * They will be set later.
1375f5252e00SMitsuo Hayasaka 	 */
1376f5252e00SMitsuo Hayasaka 	if (flags & VM_UNLIST)
1377f5252e00SMitsuo Hayasaka 		setup_vmalloc_vm(area, va, flags, caller);
1378f5252e00SMitsuo Hayasaka 	else
1379cf88c790STejun Heo 		insert_vmalloc_vm(area, va, flags, caller);
1380f5252e00SMitsuo Hayasaka 
13811da177e4SLinus Torvalds 	return area;
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
1384930fc45aSChristoph Lameter struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1385930fc45aSChristoph Lameter 				unsigned long start, unsigned long end)
1386930fc45aSChristoph Lameter {
138700ef2d2fSDavid Rientjes 	return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
138800ef2d2fSDavid Rientjes 				  GFP_KERNEL, __builtin_return_address(0));
1389930fc45aSChristoph Lameter }
13905992b6daSRusty Russell EXPORT_SYMBOL_GPL(__get_vm_area);
1391930fc45aSChristoph Lameter 
1392c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1393c2968612SBenjamin Herrenschmidt 				       unsigned long start, unsigned long end,
13945e6cafc8SMarek Szyprowski 				       const void *caller)
1395c2968612SBenjamin Herrenschmidt {
139600ef2d2fSDavid Rientjes 	return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
139700ef2d2fSDavid Rientjes 				  GFP_KERNEL, caller);
1398c2968612SBenjamin Herrenschmidt }
1399c2968612SBenjamin Herrenschmidt 
14001da177e4SLinus Torvalds /**
1401183ff22bSSimon Arlott  *	get_vm_area  -  reserve a contiguous kernel virtual area
14021da177e4SLinus Torvalds  *	@size:		size of the area
14031da177e4SLinus Torvalds  *	@flags:		%VM_IOREMAP for I/O mappings or VM_ALLOC
14041da177e4SLinus Torvalds  *
14051da177e4SLinus Torvalds  *	Search an area of @size in the kernel virtual mapping area,
14061da177e4SLinus Torvalds  *	and reserved it for out purposes.  Returns the area descriptor
14071da177e4SLinus Torvalds  *	on success or %NULL on failure.
14081da177e4SLinus Torvalds  */
14091da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
14101da177e4SLinus Torvalds {
14112dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
141200ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL,
141300ef2d2fSDavid Rientjes 				  __builtin_return_address(0));
141423016969SChristoph Lameter }
141523016969SChristoph Lameter 
141623016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
14175e6cafc8SMarek Szyprowski 				const void *caller)
141823016969SChristoph Lameter {
14192dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
142000ef2d2fSDavid Rientjes 				  NUMA_NO_NODE, GFP_KERNEL, caller);
14211da177e4SLinus Torvalds }
14221da177e4SLinus Torvalds 
1423e9da6e99SMarek Szyprowski /**
1424e9da6e99SMarek Szyprowski  *	find_vm_area  -  find a continuous kernel virtual area
1425e9da6e99SMarek Szyprowski  *	@addr:		base address
1426e9da6e99SMarek Szyprowski  *
1427e9da6e99SMarek Szyprowski  *	Search for the kernel VM area starting at @addr, and return it.
1428e9da6e99SMarek Szyprowski  *	It is up to the caller to do all required locking to keep the returned
1429e9da6e99SMarek Szyprowski  *	pointer valid.
1430e9da6e99SMarek Szyprowski  */
1431e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr)
143283342314SNick Piggin {
1433db64fe02SNick Piggin 	struct vmap_area *va;
143483342314SNick Piggin 
1435db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
1436db64fe02SNick Piggin 	if (va && va->flags & VM_VM_AREA)
1437db1aecafSMinchan Kim 		return va->vm;
143883342314SNick Piggin 
14397856dfebSAndi Kleen 	return NULL;
14407856dfebSAndi Kleen }
14417856dfebSAndi Kleen 
14421da177e4SLinus Torvalds /**
1443183ff22bSSimon Arlott  *	remove_vm_area  -  find and remove a continuous kernel virtual area
14441da177e4SLinus Torvalds  *	@addr:		base address
14451da177e4SLinus Torvalds  *
14461da177e4SLinus Torvalds  *	Search for the kernel VM area starting at @addr, and remove it.
14471da177e4SLinus Torvalds  *	This function returns the found VM area, but using it is NOT safe
14487856dfebSAndi Kleen  *	on SMP machines, except for its size or flags.
14491da177e4SLinus Torvalds  */
1450b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr)
14511da177e4SLinus Torvalds {
1452db64fe02SNick Piggin 	struct vmap_area *va;
1453db64fe02SNick Piggin 
1454db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
1455db64fe02SNick Piggin 	if (va && va->flags & VM_VM_AREA) {
1456db1aecafSMinchan Kim 		struct vm_struct *vm = va->vm;
1457f5252e00SMitsuo Hayasaka 
1458c69480adSJoonsoo Kim 		spin_lock(&vmap_area_lock);
1459c69480adSJoonsoo Kim 		va->vm = NULL;
1460c69480adSJoonsoo Kim 		va->flags &= ~VM_VM_AREA;
1461c69480adSJoonsoo Kim 		spin_unlock(&vmap_area_lock);
1462c69480adSJoonsoo Kim 
1463dd32c279SKAMEZAWA Hiroyuki 		vmap_debug_free_range(va->va_start, va->va_end);
1464dd32c279SKAMEZAWA Hiroyuki 		free_unmap_vmap_area(va);
1465dd32c279SKAMEZAWA Hiroyuki 		vm->size -= PAGE_SIZE;
1466dd32c279SKAMEZAWA Hiroyuki 
1467db64fe02SNick Piggin 		return vm;
1468db64fe02SNick Piggin 	}
1469db64fe02SNick Piggin 	return NULL;
14701da177e4SLinus Torvalds }
14711da177e4SLinus Torvalds 
1472b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages)
14731da177e4SLinus Torvalds {
14741da177e4SLinus Torvalds 	struct vm_struct *area;
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	if (!addr)
14771da177e4SLinus Torvalds 		return;
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	if ((PAGE_SIZE-1) & (unsigned long)addr) {
14804c8573e2SArjan van de Ven 		WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
14811da177e4SLinus Torvalds 		return;
14821da177e4SLinus Torvalds 	}
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 	area = remove_vm_area(addr);
14851da177e4SLinus Torvalds 	if (unlikely(!area)) {
14864c8573e2SArjan van de Ven 		WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
14871da177e4SLinus Torvalds 				addr);
14881da177e4SLinus Torvalds 		return;
14891da177e4SLinus Torvalds 	}
14901da177e4SLinus Torvalds 
14919a11b49aSIngo Molnar 	debug_check_no_locks_freed(addr, area->size);
14923ac7fe5aSThomas Gleixner 	debug_check_no_obj_freed(addr, area->size);
14939a11b49aSIngo Molnar 
14941da177e4SLinus Torvalds 	if (deallocate_pages) {
14951da177e4SLinus Torvalds 		int i;
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 		for (i = 0; i < area->nr_pages; i++) {
1498bf53d6f8SChristoph Lameter 			struct page *page = area->pages[i];
1499bf53d6f8SChristoph Lameter 
1500bf53d6f8SChristoph Lameter 			BUG_ON(!page);
1501bf53d6f8SChristoph Lameter 			__free_page(page);
15021da177e4SLinus Torvalds 		}
15031da177e4SLinus Torvalds 
15048757d5faSJan Kiszka 		if (area->flags & VM_VPAGES)
15051da177e4SLinus Torvalds 			vfree(area->pages);
15061da177e4SLinus Torvalds 		else
15071da177e4SLinus Torvalds 			kfree(area->pages);
15081da177e4SLinus Torvalds 	}
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	kfree(area);
15111da177e4SLinus Torvalds 	return;
15121da177e4SLinus Torvalds }
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds /**
15151da177e4SLinus Torvalds  *	vfree  -  release memory allocated by vmalloc()
15161da177e4SLinus Torvalds  *	@addr:		memory base address
15171da177e4SLinus Torvalds  *
1518183ff22bSSimon Arlott  *	Free the virtually continuous memory area starting at @addr, as
151980e93effSPekka Enberg  *	obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
152080e93effSPekka Enberg  *	NULL, no operation is performed.
15211da177e4SLinus Torvalds  *
152232fcfd40SAl Viro  *	Must not be called in NMI context (strictly speaking, only if we don't
152332fcfd40SAl Viro  *	have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
152432fcfd40SAl Viro  *	conventions for vfree() arch-depenedent would be a really bad idea)
152532fcfd40SAl Viro  *
1526c9fcee51SAndrew Morton  *	NOTE: assumes that the object at *addr has a size >= sizeof(llist_node)
1527c9fcee51SAndrew Morton  *
15281da177e4SLinus Torvalds  */
1529b3bdda02SChristoph Lameter void vfree(const void *addr)
15301da177e4SLinus Torvalds {
153132fcfd40SAl Viro 	BUG_ON(in_nmi());
153289219d37SCatalin Marinas 
153389219d37SCatalin Marinas 	kmemleak_free(addr);
153489219d37SCatalin Marinas 
153532fcfd40SAl Viro 	if (!addr)
153632fcfd40SAl Viro 		return;
153732fcfd40SAl Viro 	if (unlikely(in_interrupt())) {
153832fcfd40SAl Viro 		struct vfree_deferred *p = &__get_cpu_var(vfree_deferred);
153932fcfd40SAl Viro 		llist_add((struct llist_node *)addr, &p->list);
154032fcfd40SAl Viro 		schedule_work(&p->wq);
154132fcfd40SAl Viro 	} else
15421da177e4SLinus Torvalds 		__vunmap(addr, 1);
15431da177e4SLinus Torvalds }
15441da177e4SLinus Torvalds EXPORT_SYMBOL(vfree);
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds /**
15471da177e4SLinus Torvalds  *	vunmap  -  release virtual mapping obtained by vmap()
15481da177e4SLinus Torvalds  *	@addr:		memory base address
15491da177e4SLinus Torvalds  *
15501da177e4SLinus Torvalds  *	Free the virtually contiguous memory area starting at @addr,
15511da177e4SLinus Torvalds  *	which was created from the page array passed to vmap().
15521da177e4SLinus Torvalds  *
155380e93effSPekka Enberg  *	Must not be called in interrupt context.
15541da177e4SLinus Torvalds  */
1555b3bdda02SChristoph Lameter void vunmap(const void *addr)
15561da177e4SLinus Torvalds {
15571da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
155834754b69SPeter Zijlstra 	might_sleep();
155932fcfd40SAl Viro 	if (addr)
15601da177e4SLinus Torvalds 		__vunmap(addr, 0);
15611da177e4SLinus Torvalds }
15621da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap);
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds /**
15651da177e4SLinus Torvalds  *	vmap  -  map an array of pages into virtually contiguous space
15661da177e4SLinus Torvalds  *	@pages:		array of page pointers
15671da177e4SLinus Torvalds  *	@count:		number of pages to map
15681da177e4SLinus Torvalds  *	@flags:		vm_area->flags
15691da177e4SLinus Torvalds  *	@prot:		page protection for the mapping
15701da177e4SLinus Torvalds  *
15711da177e4SLinus Torvalds  *	Maps @count pages from @pages into contiguous kernel virtual
15721da177e4SLinus Torvalds  *	space.
15731da177e4SLinus Torvalds  */
15741da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count,
15751da177e4SLinus Torvalds 		unsigned long flags, pgprot_t prot)
15761da177e4SLinus Torvalds {
15771da177e4SLinus Torvalds 	struct vm_struct *area;
15781da177e4SLinus Torvalds 
157934754b69SPeter Zijlstra 	might_sleep();
158034754b69SPeter Zijlstra 
15814481374cSJan Beulich 	if (count > totalram_pages)
15821da177e4SLinus Torvalds 		return NULL;
15831da177e4SLinus Torvalds 
158423016969SChristoph Lameter 	area = get_vm_area_caller((count << PAGE_SHIFT), flags,
158523016969SChristoph Lameter 					__builtin_return_address(0));
15861da177e4SLinus Torvalds 	if (!area)
15871da177e4SLinus Torvalds 		return NULL;
158823016969SChristoph Lameter 
15891da177e4SLinus Torvalds 	if (map_vm_area(area, prot, &pages)) {
15901da177e4SLinus Torvalds 		vunmap(area->addr);
15911da177e4SLinus Torvalds 		return NULL;
15921da177e4SLinus Torvalds 	}
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 	return area->addr;
15951da177e4SLinus Torvalds }
15961da177e4SLinus Torvalds EXPORT_SYMBOL(vmap);
15971da177e4SLinus Torvalds 
15982dca6999SDavid Miller static void *__vmalloc_node(unsigned long size, unsigned long align,
15992dca6999SDavid Miller 			    gfp_t gfp_mask, pgprot_t prot,
16005e6cafc8SMarek Szyprowski 			    int node, const void *caller);
1601e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
16025e6cafc8SMarek Szyprowski 				 pgprot_t prot, int node, const void *caller)
16031da177e4SLinus Torvalds {
160422943ab1SDave Hansen 	const int order = 0;
16051da177e4SLinus Torvalds 	struct page **pages;
16061da177e4SLinus Torvalds 	unsigned int nr_pages, array_size, i;
1607976d6dfbSJan Beulich 	gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
16101da177e4SLinus Torvalds 	array_size = (nr_pages * sizeof(struct page *));
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	area->nr_pages = nr_pages;
16131da177e4SLinus Torvalds 	/* Please note that the recursion is strictly bounded. */
16148757d5faSJan Kiszka 	if (array_size > PAGE_SIZE) {
1615976d6dfbSJan Beulich 		pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,
161623016969SChristoph Lameter 				PAGE_KERNEL, node, caller);
16178757d5faSJan Kiszka 		area->flags |= VM_VPAGES;
1618286e1ea3SAndrew Morton 	} else {
1619976d6dfbSJan Beulich 		pages = kmalloc_node(array_size, nested_gfp, node);
1620286e1ea3SAndrew Morton 	}
16211da177e4SLinus Torvalds 	area->pages = pages;
162223016969SChristoph Lameter 	area->caller = caller;
16231da177e4SLinus Torvalds 	if (!area->pages) {
16241da177e4SLinus Torvalds 		remove_vm_area(area->addr);
16251da177e4SLinus Torvalds 		kfree(area);
16261da177e4SLinus Torvalds 		return NULL;
16271da177e4SLinus Torvalds 	}
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	for (i = 0; i < area->nr_pages; i++) {
1630bf53d6f8SChristoph Lameter 		struct page *page;
163122943ab1SDave Hansen 		gfp_t tmp_mask = gfp_mask | __GFP_NOWARN;
1632bf53d6f8SChristoph Lameter 
1633930fc45aSChristoph Lameter 		if (node < 0)
163422943ab1SDave Hansen 			page = alloc_page(tmp_mask);
1635930fc45aSChristoph Lameter 		else
163622943ab1SDave Hansen 			page = alloc_pages_node(node, tmp_mask, order);
1637bf53d6f8SChristoph Lameter 
1638bf53d6f8SChristoph Lameter 		if (unlikely(!page)) {
16391da177e4SLinus Torvalds 			/* Successfully allocated i pages, free them in __vunmap() */
16401da177e4SLinus Torvalds 			area->nr_pages = i;
16411da177e4SLinus Torvalds 			goto fail;
16421da177e4SLinus Torvalds 		}
1643bf53d6f8SChristoph Lameter 		area->pages[i] = page;
16441da177e4SLinus Torvalds 	}
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds 	if (map_vm_area(area, prot, &pages))
16471da177e4SLinus Torvalds 		goto fail;
16481da177e4SLinus Torvalds 	return area->addr;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds fail:
16513ee9a4f0SJoe Perches 	warn_alloc_failed(gfp_mask, order,
16523ee9a4f0SJoe Perches 			  "vmalloc: allocation failure, allocated %ld of %ld bytes\n",
165322943ab1SDave Hansen 			  (area->nr_pages*PAGE_SIZE), area->size);
16541da177e4SLinus Torvalds 	vfree(area->addr);
16551da177e4SLinus Torvalds 	return NULL;
16561da177e4SLinus Torvalds }
16571da177e4SLinus Torvalds 
1658d0a21265SDavid Rientjes /**
1659d0a21265SDavid Rientjes  *	__vmalloc_node_range  -  allocate virtually contiguous memory
1660d0a21265SDavid Rientjes  *	@size:		allocation size
1661d0a21265SDavid Rientjes  *	@align:		desired alignment
1662d0a21265SDavid Rientjes  *	@start:		vm area range start
1663d0a21265SDavid Rientjes  *	@end:		vm area range end
1664d0a21265SDavid Rientjes  *	@gfp_mask:	flags for the page level allocator
1665d0a21265SDavid Rientjes  *	@prot:		protection mask for the allocated pages
166600ef2d2fSDavid Rientjes  *	@node:		node to use for allocation or NUMA_NO_NODE
1667d0a21265SDavid Rientjes  *	@caller:	caller's return address
1668d0a21265SDavid Rientjes  *
1669d0a21265SDavid Rientjes  *	Allocate enough pages to cover @size from the page level
1670d0a21265SDavid Rientjes  *	allocator with @gfp_mask flags.  Map them into contiguous
1671d0a21265SDavid Rientjes  *	kernel virtual space, using a pagetable protection of @prot.
1672d0a21265SDavid Rientjes  */
1673d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align,
1674d0a21265SDavid Rientjes 			unsigned long start, unsigned long end, gfp_t gfp_mask,
16755e6cafc8SMarek Szyprowski 			pgprot_t prot, int node, const void *caller)
1676930fc45aSChristoph Lameter {
1677d0a21265SDavid Rientjes 	struct vm_struct *area;
1678d0a21265SDavid Rientjes 	void *addr;
1679d0a21265SDavid Rientjes 	unsigned long real_size = size;
1680d0a21265SDavid Rientjes 
1681d0a21265SDavid Rientjes 	size = PAGE_ALIGN(size);
1682d0a21265SDavid Rientjes 	if (!size || (size >> PAGE_SHIFT) > totalram_pages)
1683de7d2b56SJoe Perches 		goto fail;
1684d0a21265SDavid Rientjes 
1685f5252e00SMitsuo Hayasaka 	area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNLIST,
1686f5252e00SMitsuo Hayasaka 				  start, end, node, gfp_mask, caller);
1687d0a21265SDavid Rientjes 	if (!area)
1688de7d2b56SJoe Perches 		goto fail;
1689d0a21265SDavid Rientjes 
1690d0a21265SDavid Rientjes 	addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
16911368edf0SMel Gorman 	if (!addr)
16921368edf0SMel Gorman 		return NULL;
169389219d37SCatalin Marinas 
169489219d37SCatalin Marinas 	/*
16954341fa45SJoonsoo Kim 	 * In this function, newly allocated vm_struct has VM_UNLIST flag.
16964341fa45SJoonsoo Kim 	 * It means that vm_struct is not fully initialized.
16974341fa45SJoonsoo Kim 	 * Now, it is fully initialized, so remove this flag here.
1698f5252e00SMitsuo Hayasaka 	 */
16994341fa45SJoonsoo Kim 	clear_vm_unlist(area);
1700f5252e00SMitsuo Hayasaka 
1701f5252e00SMitsuo Hayasaka 	/*
170289219d37SCatalin Marinas 	 * A ref_count = 3 is needed because the vm_struct and vmap_area
170389219d37SCatalin Marinas 	 * structures allocated in the __get_vm_area_node() function contain
170489219d37SCatalin Marinas 	 * references to the virtual address of the vmalloc'ed block.
170589219d37SCatalin Marinas 	 */
1706d0a21265SDavid Rientjes 	kmemleak_alloc(addr, real_size, 3, gfp_mask);
170789219d37SCatalin Marinas 
170889219d37SCatalin Marinas 	return addr;
1709de7d2b56SJoe Perches 
1710de7d2b56SJoe Perches fail:
1711de7d2b56SJoe Perches 	warn_alloc_failed(gfp_mask, 0,
1712de7d2b56SJoe Perches 			  "vmalloc: allocation failure: %lu bytes\n",
1713de7d2b56SJoe Perches 			  real_size);
1714de7d2b56SJoe Perches 	return NULL;
1715930fc45aSChristoph Lameter }
1716930fc45aSChristoph Lameter 
17171da177e4SLinus Torvalds /**
1718930fc45aSChristoph Lameter  *	__vmalloc_node  -  allocate virtually contiguous memory
17191da177e4SLinus Torvalds  *	@size:		allocation size
17202dca6999SDavid Miller  *	@align:		desired alignment
17211da177e4SLinus Torvalds  *	@gfp_mask:	flags for the page level allocator
17221da177e4SLinus Torvalds  *	@prot:		protection mask for the allocated pages
172300ef2d2fSDavid Rientjes  *	@node:		node to use for allocation or NUMA_NO_NODE
1724c85d194bSRandy Dunlap  *	@caller:	caller's return address
17251da177e4SLinus Torvalds  *
17261da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
17271da177e4SLinus Torvalds  *	allocator with @gfp_mask flags.  Map them into contiguous
17281da177e4SLinus Torvalds  *	kernel virtual space, using a pagetable protection of @prot.
17291da177e4SLinus Torvalds  */
17302dca6999SDavid Miller static void *__vmalloc_node(unsigned long size, unsigned long align,
17312dca6999SDavid Miller 			    gfp_t gfp_mask, pgprot_t prot,
17325e6cafc8SMarek Szyprowski 			    int node, const void *caller)
17331da177e4SLinus Torvalds {
1734d0a21265SDavid Rientjes 	return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
1735d0a21265SDavid Rientjes 				gfp_mask, prot, node, caller);
17361da177e4SLinus Torvalds }
17371da177e4SLinus Torvalds 
1738930fc45aSChristoph Lameter void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1739930fc45aSChristoph Lameter {
174000ef2d2fSDavid Rientjes 	return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE,
174123016969SChristoph Lameter 				__builtin_return_address(0));
1742930fc45aSChristoph Lameter }
17431da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc);
17441da177e4SLinus Torvalds 
1745e1ca7788SDave Young static inline void *__vmalloc_node_flags(unsigned long size,
1746e1ca7788SDave Young 					int node, gfp_t flags)
1747e1ca7788SDave Young {
1748e1ca7788SDave Young 	return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
1749e1ca7788SDave Young 					node, __builtin_return_address(0));
1750e1ca7788SDave Young }
1751e1ca7788SDave Young 
17521da177e4SLinus Torvalds /**
17531da177e4SLinus Torvalds  *	vmalloc  -  allocate virtually contiguous memory
17541da177e4SLinus Torvalds  *	@size:		allocation size
17551da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
17561da177e4SLinus Torvalds  *	allocator and map them into contiguous kernel virtual space.
17571da177e4SLinus Torvalds  *
1758c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
17591da177e4SLinus Torvalds  *	use __vmalloc() instead.
17601da177e4SLinus Torvalds  */
17611da177e4SLinus Torvalds void *vmalloc(unsigned long size)
17621da177e4SLinus Torvalds {
176300ef2d2fSDavid Rientjes 	return __vmalloc_node_flags(size, NUMA_NO_NODE,
176400ef2d2fSDavid Rientjes 				    GFP_KERNEL | __GFP_HIGHMEM);
17651da177e4SLinus Torvalds }
17661da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc);
17671da177e4SLinus Torvalds 
1768930fc45aSChristoph Lameter /**
1769e1ca7788SDave Young  *	vzalloc - allocate virtually contiguous memory with zero fill
1770e1ca7788SDave Young  *	@size:	allocation size
1771e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
1772e1ca7788SDave Young  *	allocator and map them into contiguous kernel virtual space.
1773e1ca7788SDave Young  *	The memory allocated is set to zero.
1774e1ca7788SDave Young  *
1775e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
1776e1ca7788SDave Young  *	use __vmalloc() instead.
1777e1ca7788SDave Young  */
1778e1ca7788SDave Young void *vzalloc(unsigned long size)
1779e1ca7788SDave Young {
178000ef2d2fSDavid Rientjes 	return __vmalloc_node_flags(size, NUMA_NO_NODE,
1781e1ca7788SDave Young 				GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1782e1ca7788SDave Young }
1783e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
1784e1ca7788SDave Young 
1785e1ca7788SDave Young /**
1786ead04089SRolf Eike Beer  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
178783342314SNick Piggin  * @size: allocation size
1788ead04089SRolf Eike Beer  *
1789ead04089SRolf Eike Beer  * The resulting memory area is zeroed so it can be mapped to userspace
1790ead04089SRolf Eike Beer  * without leaking data.
179183342314SNick Piggin  */
179283342314SNick Piggin void *vmalloc_user(unsigned long size)
179383342314SNick Piggin {
179483342314SNick Piggin 	struct vm_struct *area;
179583342314SNick Piggin 	void *ret;
179683342314SNick Piggin 
17972dca6999SDavid Miller 	ret = __vmalloc_node(size, SHMLBA,
17982dca6999SDavid Miller 			     GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
179900ef2d2fSDavid Rientjes 			     PAGE_KERNEL, NUMA_NO_NODE,
180000ef2d2fSDavid Rientjes 			     __builtin_return_address(0));
18012b4ac44eSEric Dumazet 	if (ret) {
1802db64fe02SNick Piggin 		area = find_vm_area(ret);
180383342314SNick Piggin 		area->flags |= VM_USERMAP;
18042b4ac44eSEric Dumazet 	}
180583342314SNick Piggin 	return ret;
180683342314SNick Piggin }
180783342314SNick Piggin EXPORT_SYMBOL(vmalloc_user);
180883342314SNick Piggin 
180983342314SNick Piggin /**
1810930fc45aSChristoph Lameter  *	vmalloc_node  -  allocate memory on a specific node
1811930fc45aSChristoph Lameter  *	@size:		allocation size
1812d44e0780SRandy Dunlap  *	@node:		numa node
1813930fc45aSChristoph Lameter  *
1814930fc45aSChristoph Lameter  *	Allocate enough pages to cover @size from the page level
1815930fc45aSChristoph Lameter  *	allocator and map them into contiguous kernel virtual space.
1816930fc45aSChristoph Lameter  *
1817c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
1818930fc45aSChristoph Lameter  *	use __vmalloc() instead.
1819930fc45aSChristoph Lameter  */
1820930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node)
1821930fc45aSChristoph Lameter {
18222dca6999SDavid Miller 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
182323016969SChristoph Lameter 					node, __builtin_return_address(0));
1824930fc45aSChristoph Lameter }
1825930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node);
1826930fc45aSChristoph Lameter 
1827e1ca7788SDave Young /**
1828e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
1829e1ca7788SDave Young  * @size:	allocation size
1830e1ca7788SDave Young  * @node:	numa node
1831e1ca7788SDave Young  *
1832e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
1833e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
1834e1ca7788SDave Young  * The memory allocated is set to zero.
1835e1ca7788SDave Young  *
1836e1ca7788SDave Young  * For tight control over page level allocator and protection flags
1837e1ca7788SDave Young  * use __vmalloc_node() instead.
1838e1ca7788SDave Young  */
1839e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
1840e1ca7788SDave Young {
1841e1ca7788SDave Young 	return __vmalloc_node_flags(size, node,
1842e1ca7788SDave Young 			 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1843e1ca7788SDave Young }
1844e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
1845e1ca7788SDave Young 
18464dc3b16bSPavel Pisa #ifndef PAGE_KERNEL_EXEC
18474dc3b16bSPavel Pisa # define PAGE_KERNEL_EXEC PAGE_KERNEL
18484dc3b16bSPavel Pisa #endif
18494dc3b16bSPavel Pisa 
18501da177e4SLinus Torvalds /**
18511da177e4SLinus Torvalds  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
18521da177e4SLinus Torvalds  *	@size:		allocation size
18531da177e4SLinus Torvalds  *
18541da177e4SLinus Torvalds  *	Kernel-internal function to allocate enough pages to cover @size
18551da177e4SLinus Torvalds  *	the page level allocator and map them into contiguous and
18561da177e4SLinus Torvalds  *	executable kernel virtual space.
18571da177e4SLinus Torvalds  *
1858c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
18591da177e4SLinus Torvalds  *	use __vmalloc() instead.
18601da177e4SLinus Torvalds  */
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds void *vmalloc_exec(unsigned long size)
18631da177e4SLinus Torvalds {
18642dca6999SDavid Miller 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
186500ef2d2fSDavid Rientjes 			      NUMA_NO_NODE, __builtin_return_address(0));
18661da177e4SLinus Torvalds }
18671da177e4SLinus Torvalds 
18680d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
18697ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
18700d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
18717ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
18720d08e0d3SAndi Kleen #else
18730d08e0d3SAndi Kleen #define GFP_VMALLOC32 GFP_KERNEL
18740d08e0d3SAndi Kleen #endif
18750d08e0d3SAndi Kleen 
18761da177e4SLinus Torvalds /**
18771da177e4SLinus Torvalds  *	vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
18781da177e4SLinus Torvalds  *	@size:		allocation size
18791da177e4SLinus Torvalds  *
18801da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
18811da177e4SLinus Torvalds  *	page level allocator and map them into contiguous kernel virtual space.
18821da177e4SLinus Torvalds  */
18831da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
18841da177e4SLinus Torvalds {
18852dca6999SDavid Miller 	return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
188600ef2d2fSDavid Rientjes 			      NUMA_NO_NODE, __builtin_return_address(0));
18871da177e4SLinus Torvalds }
18881da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32);
18891da177e4SLinus Torvalds 
189083342314SNick Piggin /**
1891ead04089SRolf Eike Beer  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
189283342314SNick Piggin  *	@size:		allocation size
1893ead04089SRolf Eike Beer  *
1894ead04089SRolf Eike Beer  * The resulting memory area is 32bit addressable and zeroed so it can be
1895ead04089SRolf Eike Beer  * mapped to userspace without leaking data.
189683342314SNick Piggin  */
189783342314SNick Piggin void *vmalloc_32_user(unsigned long size)
189883342314SNick Piggin {
189983342314SNick Piggin 	struct vm_struct *area;
190083342314SNick Piggin 	void *ret;
190183342314SNick Piggin 
19022dca6999SDavid Miller 	ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
190300ef2d2fSDavid Rientjes 			     NUMA_NO_NODE, __builtin_return_address(0));
19042b4ac44eSEric Dumazet 	if (ret) {
1905db64fe02SNick Piggin 		area = find_vm_area(ret);
190683342314SNick Piggin 		area->flags |= VM_USERMAP;
19072b4ac44eSEric Dumazet 	}
190883342314SNick Piggin 	return ret;
190983342314SNick Piggin }
191083342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user);
191183342314SNick Piggin 
1912d0107eb0SKAMEZAWA Hiroyuki /*
1913d0107eb0SKAMEZAWA Hiroyuki  * small helper routine , copy contents to buf from addr.
1914d0107eb0SKAMEZAWA Hiroyuki  * If the page is not present, fill zero.
1915d0107eb0SKAMEZAWA Hiroyuki  */
1916d0107eb0SKAMEZAWA Hiroyuki 
1917d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count)
1918d0107eb0SKAMEZAWA Hiroyuki {
1919d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
1920d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
1921d0107eb0SKAMEZAWA Hiroyuki 
1922d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
1923d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
1924d0107eb0SKAMEZAWA Hiroyuki 
1925d0107eb0SKAMEZAWA Hiroyuki 		offset = (unsigned long)addr & ~PAGE_MASK;
1926d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
1927d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
1928d0107eb0SKAMEZAWA Hiroyuki 			length = count;
1929d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
1930d0107eb0SKAMEZAWA Hiroyuki 		/*
1931d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
1932d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
1933d0107eb0SKAMEZAWA Hiroyuki 		 * overhead of vmalloc()/vfree() calles for this _debug_
1934d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
1935d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
1936d0107eb0SKAMEZAWA Hiroyuki 		 */
1937d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
1938d0107eb0SKAMEZAWA Hiroyuki 			/*
1939d0107eb0SKAMEZAWA Hiroyuki 			 * we can expect USER0 is not used (see vread/vwrite's
1940d0107eb0SKAMEZAWA Hiroyuki 			 * function description)
1941d0107eb0SKAMEZAWA Hiroyuki 			 */
19429b04c5feSCong Wang 			void *map = kmap_atomic(p);
1943d0107eb0SKAMEZAWA Hiroyuki 			memcpy(buf, map + offset, length);
19449b04c5feSCong Wang 			kunmap_atomic(map);
1945d0107eb0SKAMEZAWA Hiroyuki 		} else
1946d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, length);
1947d0107eb0SKAMEZAWA Hiroyuki 
1948d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
1949d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
1950d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
1951d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
1952d0107eb0SKAMEZAWA Hiroyuki 	}
1953d0107eb0SKAMEZAWA Hiroyuki 	return copied;
1954d0107eb0SKAMEZAWA Hiroyuki }
1955d0107eb0SKAMEZAWA Hiroyuki 
1956d0107eb0SKAMEZAWA Hiroyuki static int aligned_vwrite(char *buf, char *addr, unsigned long count)
1957d0107eb0SKAMEZAWA Hiroyuki {
1958d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
1959d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
1960d0107eb0SKAMEZAWA Hiroyuki 
1961d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
1962d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
1963d0107eb0SKAMEZAWA Hiroyuki 
1964d0107eb0SKAMEZAWA Hiroyuki 		offset = (unsigned long)addr & ~PAGE_MASK;
1965d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
1966d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
1967d0107eb0SKAMEZAWA Hiroyuki 			length = count;
1968d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
1969d0107eb0SKAMEZAWA Hiroyuki 		/*
1970d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
1971d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
1972d0107eb0SKAMEZAWA Hiroyuki 		 * overhead of vmalloc()/vfree() calles for this _debug_
1973d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
1974d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
1975d0107eb0SKAMEZAWA Hiroyuki 		 */
1976d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
1977d0107eb0SKAMEZAWA Hiroyuki 			/*
1978d0107eb0SKAMEZAWA Hiroyuki 			 * we can expect USER0 is not used (see vread/vwrite's
1979d0107eb0SKAMEZAWA Hiroyuki 			 * function description)
1980d0107eb0SKAMEZAWA Hiroyuki 			 */
19819b04c5feSCong Wang 			void *map = kmap_atomic(p);
1982d0107eb0SKAMEZAWA Hiroyuki 			memcpy(map + offset, buf, length);
19839b04c5feSCong Wang 			kunmap_atomic(map);
1984d0107eb0SKAMEZAWA Hiroyuki 		}
1985d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
1986d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
1987d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
1988d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
1989d0107eb0SKAMEZAWA Hiroyuki 	}
1990d0107eb0SKAMEZAWA Hiroyuki 	return copied;
1991d0107eb0SKAMEZAWA Hiroyuki }
1992d0107eb0SKAMEZAWA Hiroyuki 
1993d0107eb0SKAMEZAWA Hiroyuki /**
1994d0107eb0SKAMEZAWA Hiroyuki  *	vread() -  read vmalloc area in a safe way.
1995d0107eb0SKAMEZAWA Hiroyuki  *	@buf:		buffer for reading data
1996d0107eb0SKAMEZAWA Hiroyuki  *	@addr:		vm address.
1997d0107eb0SKAMEZAWA Hiroyuki  *	@count:		number of bytes to be read.
1998d0107eb0SKAMEZAWA Hiroyuki  *
1999d0107eb0SKAMEZAWA Hiroyuki  *	Returns # of bytes which addr and buf should be increased.
2000d0107eb0SKAMEZAWA Hiroyuki  *	(same number to @count). Returns 0 if [addr...addr+count) doesn't
2001d0107eb0SKAMEZAWA Hiroyuki  *	includes any intersect with alive vmalloc area.
2002d0107eb0SKAMEZAWA Hiroyuki  *
2003d0107eb0SKAMEZAWA Hiroyuki  *	This function checks that addr is a valid vmalloc'ed area, and
2004d0107eb0SKAMEZAWA Hiroyuki  *	copy data from that area to a given buffer. If the given memory range
2005d0107eb0SKAMEZAWA Hiroyuki  *	of [addr...addr+count) includes some valid address, data is copied to
2006d0107eb0SKAMEZAWA Hiroyuki  *	proper area of @buf. If there are memory holes, they'll be zero-filled.
2007d0107eb0SKAMEZAWA Hiroyuki  *	IOREMAP area is treated as memory hole and no copy is done.
2008d0107eb0SKAMEZAWA Hiroyuki  *
2009d0107eb0SKAMEZAWA Hiroyuki  *	If [addr...addr+count) doesn't includes any intersects with alive
2010a8e5202dSCong Wang  *	vm_struct area, returns 0. @buf should be kernel's buffer.
2011d0107eb0SKAMEZAWA Hiroyuki  *
2012d0107eb0SKAMEZAWA Hiroyuki  *	Note: In usual ops, vread() is never necessary because the caller
2013d0107eb0SKAMEZAWA Hiroyuki  *	should know vmalloc() area is valid and can use memcpy().
2014d0107eb0SKAMEZAWA Hiroyuki  *	This is for routines which have to access vmalloc area without
2015d0107eb0SKAMEZAWA Hiroyuki  *	any informaion, as /dev/kmem.
2016d0107eb0SKAMEZAWA Hiroyuki  *
2017d0107eb0SKAMEZAWA Hiroyuki  */
2018d0107eb0SKAMEZAWA Hiroyuki 
20191da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
20201da177e4SLinus Torvalds {
2021e81ce85fSJoonsoo Kim 	struct vmap_area *va;
2022e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
20231da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
2024d0107eb0SKAMEZAWA Hiroyuki 	unsigned long buflen = count;
20251da177e4SLinus Torvalds 	unsigned long n;
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 	/* Don't allow overflow */
20281da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
20291da177e4SLinus Torvalds 		count = -(unsigned long) addr;
20301da177e4SLinus Torvalds 
2031e81ce85fSJoonsoo Kim 	spin_lock(&vmap_area_lock);
2032e81ce85fSJoonsoo Kim 	list_for_each_entry(va, &vmap_area_list, list) {
2033e81ce85fSJoonsoo Kim 		if (!count)
2034e81ce85fSJoonsoo Kim 			break;
2035e81ce85fSJoonsoo Kim 
2036e81ce85fSJoonsoo Kim 		if (!(va->flags & VM_VM_AREA))
2037e81ce85fSJoonsoo Kim 			continue;
2038e81ce85fSJoonsoo Kim 
2039e81ce85fSJoonsoo Kim 		vm = va->vm;
2040e81ce85fSJoonsoo Kim 		vaddr = (char *) vm->addr;
2041e81ce85fSJoonsoo Kim 		if (addr >= vaddr + vm->size - PAGE_SIZE)
20421da177e4SLinus Torvalds 			continue;
20431da177e4SLinus Torvalds 		while (addr < vaddr) {
20441da177e4SLinus Torvalds 			if (count == 0)
20451da177e4SLinus Torvalds 				goto finished;
20461da177e4SLinus Torvalds 			*buf = '\0';
20471da177e4SLinus Torvalds 			buf++;
20481da177e4SLinus Torvalds 			addr++;
20491da177e4SLinus Torvalds 			count--;
20501da177e4SLinus Torvalds 		}
2051e81ce85fSJoonsoo Kim 		n = vaddr + vm->size - PAGE_SIZE - addr;
2052d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
2053d0107eb0SKAMEZAWA Hiroyuki 			n = count;
2054e81ce85fSJoonsoo Kim 		if (!(vm->flags & VM_IOREMAP))
2055d0107eb0SKAMEZAWA Hiroyuki 			aligned_vread(buf, addr, n);
2056d0107eb0SKAMEZAWA Hiroyuki 		else /* IOREMAP area is treated as memory hole */
2057d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, n);
2058d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
2059d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
2060d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
20611da177e4SLinus Torvalds 	}
20621da177e4SLinus Torvalds finished:
2063e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2064d0107eb0SKAMEZAWA Hiroyuki 
2065d0107eb0SKAMEZAWA Hiroyuki 	if (buf == buf_start)
2066d0107eb0SKAMEZAWA Hiroyuki 		return 0;
2067d0107eb0SKAMEZAWA Hiroyuki 	/* zero-fill memory holes */
2068d0107eb0SKAMEZAWA Hiroyuki 	if (buf != buf_start + buflen)
2069d0107eb0SKAMEZAWA Hiroyuki 		memset(buf, 0, buflen - (buf - buf_start));
2070d0107eb0SKAMEZAWA Hiroyuki 
2071d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
20721da177e4SLinus Torvalds }
20731da177e4SLinus Torvalds 
2074d0107eb0SKAMEZAWA Hiroyuki /**
2075d0107eb0SKAMEZAWA Hiroyuki  *	vwrite() -  write vmalloc area in a safe way.
2076d0107eb0SKAMEZAWA Hiroyuki  *	@buf:		buffer for source data
2077d0107eb0SKAMEZAWA Hiroyuki  *	@addr:		vm address.
2078d0107eb0SKAMEZAWA Hiroyuki  *	@count:		number of bytes to be read.
2079d0107eb0SKAMEZAWA Hiroyuki  *
2080d0107eb0SKAMEZAWA Hiroyuki  *	Returns # of bytes which addr and buf should be incresed.
2081d0107eb0SKAMEZAWA Hiroyuki  *	(same number to @count).
2082d0107eb0SKAMEZAWA Hiroyuki  *	If [addr...addr+count) doesn't includes any intersect with valid
2083d0107eb0SKAMEZAWA Hiroyuki  *	vmalloc area, returns 0.
2084d0107eb0SKAMEZAWA Hiroyuki  *
2085d0107eb0SKAMEZAWA Hiroyuki  *	This function checks that addr is a valid vmalloc'ed area, and
2086d0107eb0SKAMEZAWA Hiroyuki  *	copy data from a buffer to the given addr. If specified range of
2087d0107eb0SKAMEZAWA Hiroyuki  *	[addr...addr+count) includes some valid address, data is copied from
2088d0107eb0SKAMEZAWA Hiroyuki  *	proper area of @buf. If there are memory holes, no copy to hole.
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, vwrite() 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 
21001da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
21011da177e4SLinus Torvalds {
2102e81ce85fSJoonsoo Kim 	struct vmap_area *va;
2103e81ce85fSJoonsoo Kim 	struct vm_struct *vm;
2104d0107eb0SKAMEZAWA Hiroyuki 	char *vaddr;
2105d0107eb0SKAMEZAWA Hiroyuki 	unsigned long n, buflen;
2106d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 	/* Don't allow overflow */
21091da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
21101da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2111d0107eb0SKAMEZAWA Hiroyuki 	buflen = count;
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;
2123e81ce85fSJoonsoo Kim 		if (addr >= vaddr + vm->size - PAGE_SIZE)
21241da177e4SLinus Torvalds 			continue;
21251da177e4SLinus Torvalds 		while (addr < vaddr) {
21261da177e4SLinus Torvalds 			if (count == 0)
21271da177e4SLinus Torvalds 				goto finished;
21281da177e4SLinus Torvalds 			buf++;
21291da177e4SLinus Torvalds 			addr++;
21301da177e4SLinus Torvalds 			count--;
21311da177e4SLinus Torvalds 		}
2132e81ce85fSJoonsoo Kim 		n = vaddr + vm->size - PAGE_SIZE - addr;
2133d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
2134d0107eb0SKAMEZAWA Hiroyuki 			n = count;
2135e81ce85fSJoonsoo Kim 		if (!(vm->flags & VM_IOREMAP)) {
2136d0107eb0SKAMEZAWA Hiroyuki 			aligned_vwrite(buf, addr, n);
2137d0107eb0SKAMEZAWA Hiroyuki 			copied++;
2138d0107eb0SKAMEZAWA Hiroyuki 		}
2139d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
2140d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
2141d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
21421da177e4SLinus Torvalds 	}
21431da177e4SLinus Torvalds finished:
2144e81ce85fSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2145d0107eb0SKAMEZAWA Hiroyuki 	if (!copied)
2146d0107eb0SKAMEZAWA Hiroyuki 		return 0;
2147d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
21481da177e4SLinus Torvalds }
214983342314SNick Piggin 
215083342314SNick Piggin /**
215183342314SNick Piggin  *	remap_vmalloc_range  -  map vmalloc pages to userspace
215283342314SNick Piggin  *	@vma:		vma to cover (map full range of vma)
215383342314SNick Piggin  *	@addr:		vmalloc memory
215483342314SNick Piggin  *	@pgoff:		number of pages into addr before first page to map
21557682486bSRandy Dunlap  *
21567682486bSRandy Dunlap  *	Returns:	0 for success, -Exxx on failure
215783342314SNick Piggin  *
215883342314SNick Piggin  *	This function checks that addr is a valid vmalloc'ed area, and
215983342314SNick Piggin  *	that it is big enough to cover the vma. Will return failure if
216083342314SNick Piggin  *	that criteria isn't met.
216183342314SNick Piggin  *
216272fd4a35SRobert P. J. Day  *	Similar to remap_pfn_range() (see mm/memory.c)
216383342314SNick Piggin  */
216483342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
216583342314SNick Piggin 						unsigned long pgoff)
216683342314SNick Piggin {
216783342314SNick Piggin 	struct vm_struct *area;
216883342314SNick Piggin 	unsigned long uaddr = vma->vm_start;
216983342314SNick Piggin 	unsigned long usize = vma->vm_end - vma->vm_start;
217083342314SNick Piggin 
217183342314SNick Piggin 	if ((PAGE_SIZE-1) & (unsigned long)addr)
217283342314SNick Piggin 		return -EINVAL;
217383342314SNick Piggin 
2174db64fe02SNick Piggin 	area = find_vm_area(addr);
217583342314SNick Piggin 	if (!area)
2176db64fe02SNick Piggin 		return -EINVAL;
217783342314SNick Piggin 
217883342314SNick Piggin 	if (!(area->flags & VM_USERMAP))
2179db64fe02SNick Piggin 		return -EINVAL;
218083342314SNick Piggin 
218183342314SNick Piggin 	if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
2182db64fe02SNick Piggin 		return -EINVAL;
218383342314SNick Piggin 
218483342314SNick Piggin 	addr += pgoff << PAGE_SHIFT;
218583342314SNick Piggin 	do {
218683342314SNick Piggin 		struct page *page = vmalloc_to_page(addr);
2187db64fe02SNick Piggin 		int ret;
2188db64fe02SNick Piggin 
218983342314SNick Piggin 		ret = vm_insert_page(vma, uaddr, page);
219083342314SNick Piggin 		if (ret)
219183342314SNick Piggin 			return ret;
219283342314SNick Piggin 
219383342314SNick Piggin 		uaddr += PAGE_SIZE;
219483342314SNick Piggin 		addr += PAGE_SIZE;
219583342314SNick Piggin 		usize -= PAGE_SIZE;
219683342314SNick Piggin 	} while (usize > 0);
219783342314SNick Piggin 
2198314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
219983342314SNick Piggin 
2200db64fe02SNick Piggin 	return 0;
220183342314SNick Piggin }
220283342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range);
220383342314SNick Piggin 
22041eeb66a1SChristoph Hellwig /*
22051eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
22061eeb66a1SChristoph Hellwig  * have one.
22071eeb66a1SChristoph Hellwig  */
22081eeb66a1SChristoph Hellwig void  __attribute__((weak)) vmalloc_sync_all(void)
22091eeb66a1SChristoph Hellwig {
22101eeb66a1SChristoph Hellwig }
22115f4352fbSJeremy Fitzhardinge 
22125f4352fbSJeremy Fitzhardinge 
22132f569afdSMartin Schwidefsky static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
22145f4352fbSJeremy Fitzhardinge {
2215cd12909cSDavid Vrabel 	pte_t ***p = data;
2216cd12909cSDavid Vrabel 
2217cd12909cSDavid Vrabel 	if (p) {
2218cd12909cSDavid Vrabel 		*(*p) = pte;
2219cd12909cSDavid Vrabel 		(*p)++;
2220cd12909cSDavid Vrabel 	}
22215f4352fbSJeremy Fitzhardinge 	return 0;
22225f4352fbSJeremy Fitzhardinge }
22235f4352fbSJeremy Fitzhardinge 
22245f4352fbSJeremy Fitzhardinge /**
22255f4352fbSJeremy Fitzhardinge  *	alloc_vm_area - allocate a range of kernel address space
22265f4352fbSJeremy Fitzhardinge  *	@size:		size of the area
2227cd12909cSDavid Vrabel  *	@ptes:		returns the PTEs for the address space
22287682486bSRandy Dunlap  *
22297682486bSRandy Dunlap  *	Returns:	NULL on failure, vm_struct on success
22305f4352fbSJeremy Fitzhardinge  *
22315f4352fbSJeremy Fitzhardinge  *	This function reserves a range of kernel address space, and
22325f4352fbSJeremy Fitzhardinge  *	allocates pagetables to map that range.  No actual mappings
2233cd12909cSDavid Vrabel  *	are created.
2234cd12909cSDavid Vrabel  *
2235cd12909cSDavid Vrabel  *	If @ptes is non-NULL, pointers to the PTEs (in init_mm)
2236cd12909cSDavid Vrabel  *	allocated for the VM area are returned.
22375f4352fbSJeremy Fitzhardinge  */
2238cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
22395f4352fbSJeremy Fitzhardinge {
22405f4352fbSJeremy Fitzhardinge 	struct vm_struct *area;
22415f4352fbSJeremy Fitzhardinge 
224223016969SChristoph Lameter 	area = get_vm_area_caller(size, VM_IOREMAP,
224323016969SChristoph Lameter 				__builtin_return_address(0));
22445f4352fbSJeremy Fitzhardinge 	if (area == NULL)
22455f4352fbSJeremy Fitzhardinge 		return NULL;
22465f4352fbSJeremy Fitzhardinge 
22475f4352fbSJeremy Fitzhardinge 	/*
22485f4352fbSJeremy Fitzhardinge 	 * This ensures that page tables are constructed for this region
22495f4352fbSJeremy Fitzhardinge 	 * of kernel virtual address space and mapped into init_mm.
22505f4352fbSJeremy Fitzhardinge 	 */
22515f4352fbSJeremy Fitzhardinge 	if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
2252cd12909cSDavid Vrabel 				size, f, ptes ? &ptes : NULL)) {
22535f4352fbSJeremy Fitzhardinge 		free_vm_area(area);
22545f4352fbSJeremy Fitzhardinge 		return NULL;
22555f4352fbSJeremy Fitzhardinge 	}
22565f4352fbSJeremy Fitzhardinge 
22575f4352fbSJeremy Fitzhardinge 	return area;
22585f4352fbSJeremy Fitzhardinge }
22595f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(alloc_vm_area);
22605f4352fbSJeremy Fitzhardinge 
22615f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area)
22625f4352fbSJeremy Fitzhardinge {
22635f4352fbSJeremy Fitzhardinge 	struct vm_struct *ret;
22645f4352fbSJeremy Fitzhardinge 	ret = remove_vm_area(area->addr);
22655f4352fbSJeremy Fitzhardinge 	BUG_ON(ret != area);
22665f4352fbSJeremy Fitzhardinge 	kfree(area);
22675f4352fbSJeremy Fitzhardinge }
22685f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area);
2269a10aa579SChristoph Lameter 
22704f8b02b4STejun Heo #ifdef CONFIG_SMP
2271ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n)
2272ca23e405STejun Heo {
2273ca23e405STejun Heo 	return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
2274ca23e405STejun Heo }
2275ca23e405STejun Heo 
2276ca23e405STejun Heo /**
2277ca23e405STejun Heo  * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
2278ca23e405STejun Heo  * @end: target address
2279ca23e405STejun Heo  * @pnext: out arg for the next vmap_area
2280ca23e405STejun Heo  * @pprev: out arg for the previous vmap_area
2281ca23e405STejun Heo  *
2282ca23e405STejun Heo  * Returns: %true if either or both of next and prev are found,
2283ca23e405STejun Heo  *	    %false if no vmap_area exists
2284ca23e405STejun Heo  *
2285ca23e405STejun Heo  * Find vmap_areas end addresses of which enclose @end.  ie. if not
2286ca23e405STejun Heo  * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
2287ca23e405STejun Heo  */
2288ca23e405STejun Heo static bool pvm_find_next_prev(unsigned long end,
2289ca23e405STejun Heo 			       struct vmap_area **pnext,
2290ca23e405STejun Heo 			       struct vmap_area **pprev)
2291ca23e405STejun Heo {
2292ca23e405STejun Heo 	struct rb_node *n = vmap_area_root.rb_node;
2293ca23e405STejun Heo 	struct vmap_area *va = NULL;
2294ca23e405STejun Heo 
2295ca23e405STejun Heo 	while (n) {
2296ca23e405STejun Heo 		va = rb_entry(n, struct vmap_area, rb_node);
2297ca23e405STejun Heo 		if (end < va->va_end)
2298ca23e405STejun Heo 			n = n->rb_left;
2299ca23e405STejun Heo 		else if (end > va->va_end)
2300ca23e405STejun Heo 			n = n->rb_right;
2301ca23e405STejun Heo 		else
2302ca23e405STejun Heo 			break;
2303ca23e405STejun Heo 	}
2304ca23e405STejun Heo 
2305ca23e405STejun Heo 	if (!va)
2306ca23e405STejun Heo 		return false;
2307ca23e405STejun Heo 
2308ca23e405STejun Heo 	if (va->va_end > end) {
2309ca23e405STejun Heo 		*pnext = va;
2310ca23e405STejun Heo 		*pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2311ca23e405STejun Heo 	} else {
2312ca23e405STejun Heo 		*pprev = va;
2313ca23e405STejun Heo 		*pnext = node_to_va(rb_next(&(*pprev)->rb_node));
2314ca23e405STejun Heo 	}
2315ca23e405STejun Heo 	return true;
2316ca23e405STejun Heo }
2317ca23e405STejun Heo 
2318ca23e405STejun Heo /**
2319ca23e405STejun Heo  * pvm_determine_end - find the highest aligned address between two vmap_areas
2320ca23e405STejun Heo  * @pnext: in/out arg for the next vmap_area
2321ca23e405STejun Heo  * @pprev: in/out arg for the previous vmap_area
2322ca23e405STejun Heo  * @align: alignment
2323ca23e405STejun Heo  *
2324ca23e405STejun Heo  * Returns: determined end address
2325ca23e405STejun Heo  *
2326ca23e405STejun Heo  * Find the highest aligned address between *@pnext and *@pprev below
2327ca23e405STejun Heo  * VMALLOC_END.  *@pnext and *@pprev are adjusted so that the aligned
2328ca23e405STejun Heo  * down address is between the end addresses of the two vmap_areas.
2329ca23e405STejun Heo  *
2330ca23e405STejun Heo  * Please note that the address returned by this function may fall
2331ca23e405STejun Heo  * inside *@pnext vmap_area.  The caller is responsible for checking
2332ca23e405STejun Heo  * that.
2333ca23e405STejun Heo  */
2334ca23e405STejun Heo static unsigned long pvm_determine_end(struct vmap_area **pnext,
2335ca23e405STejun Heo 				       struct vmap_area **pprev,
2336ca23e405STejun Heo 				       unsigned long align)
2337ca23e405STejun Heo {
2338ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2339ca23e405STejun Heo 	unsigned long addr;
2340ca23e405STejun Heo 
2341ca23e405STejun Heo 	if (*pnext)
2342ca23e405STejun Heo 		addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
2343ca23e405STejun Heo 	else
2344ca23e405STejun Heo 		addr = vmalloc_end;
2345ca23e405STejun Heo 
2346ca23e405STejun Heo 	while (*pprev && (*pprev)->va_end > addr) {
2347ca23e405STejun Heo 		*pnext = *pprev;
2348ca23e405STejun Heo 		*pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2349ca23e405STejun Heo 	}
2350ca23e405STejun Heo 
2351ca23e405STejun Heo 	return addr;
2352ca23e405STejun Heo }
2353ca23e405STejun Heo 
2354ca23e405STejun Heo /**
2355ca23e405STejun Heo  * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
2356ca23e405STejun Heo  * @offsets: array containing offset of each area
2357ca23e405STejun Heo  * @sizes: array containing size of each area
2358ca23e405STejun Heo  * @nr_vms: the number of areas to allocate
2359ca23e405STejun Heo  * @align: alignment, all entries in @offsets and @sizes must be aligned to this
2360ca23e405STejun Heo  *
2361ca23e405STejun Heo  * Returns: kmalloc'd vm_struct pointer array pointing to allocated
2362ca23e405STejun Heo  *	    vm_structs on success, %NULL on failure
2363ca23e405STejun Heo  *
2364ca23e405STejun Heo  * Percpu allocator wants to use congruent vm areas so that it can
2365ca23e405STejun Heo  * maintain the offsets among percpu areas.  This function allocates
2366ec3f64fcSDavid Rientjes  * congruent vmalloc areas for it with GFP_KERNEL.  These areas tend to
2367ec3f64fcSDavid Rientjes  * be scattered pretty far, distance between two areas easily going up
2368ec3f64fcSDavid Rientjes  * to gigabytes.  To avoid interacting with regular vmallocs, these
2369ec3f64fcSDavid Rientjes  * areas are allocated from top.
2370ca23e405STejun Heo  *
2371ca23e405STejun Heo  * Despite its complicated look, this allocator is rather simple.  It
2372ca23e405STejun Heo  * does everything top-down and scans areas from the end looking for
2373ca23e405STejun Heo  * matching slot.  While scanning, if any of the areas overlaps with
2374ca23e405STejun Heo  * existing vmap_area, the base address is pulled down to fit the
2375ca23e405STejun Heo  * area.  Scanning is repeated till all the areas fit and then all
2376ca23e405STejun Heo  * necessary data structres are inserted and the result is returned.
2377ca23e405STejun Heo  */
2378ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
2379ca23e405STejun Heo 				     const size_t *sizes, int nr_vms,
2380ec3f64fcSDavid Rientjes 				     size_t align)
2381ca23e405STejun Heo {
2382ca23e405STejun Heo 	const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
2383ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2384ca23e405STejun Heo 	struct vmap_area **vas, *prev, *next;
2385ca23e405STejun Heo 	struct vm_struct **vms;
2386ca23e405STejun Heo 	int area, area2, last_area, term_area;
2387ca23e405STejun Heo 	unsigned long base, start, end, last_end;
2388ca23e405STejun Heo 	bool purged = false;
2389ca23e405STejun Heo 
2390ca23e405STejun Heo 	/* verify parameters and allocate data structures */
2391ca23e405STejun Heo 	BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align));
2392ca23e405STejun Heo 	for (last_area = 0, area = 0; area < nr_vms; area++) {
2393ca23e405STejun Heo 		start = offsets[area];
2394ca23e405STejun Heo 		end = start + sizes[area];
2395ca23e405STejun Heo 
2396ca23e405STejun Heo 		/* is everything aligned properly? */
2397ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(offsets[area], align));
2398ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(sizes[area], align));
2399ca23e405STejun Heo 
2400ca23e405STejun Heo 		/* detect the area with the highest address */
2401ca23e405STejun Heo 		if (start > offsets[last_area])
2402ca23e405STejun Heo 			last_area = area;
2403ca23e405STejun Heo 
2404ca23e405STejun Heo 		for (area2 = 0; area2 < nr_vms; area2++) {
2405ca23e405STejun Heo 			unsigned long start2 = offsets[area2];
2406ca23e405STejun Heo 			unsigned long end2 = start2 + sizes[area2];
2407ca23e405STejun Heo 
2408ca23e405STejun Heo 			if (area2 == area)
2409ca23e405STejun Heo 				continue;
2410ca23e405STejun Heo 
2411ca23e405STejun Heo 			BUG_ON(start2 >= start && start2 < end);
2412ca23e405STejun Heo 			BUG_ON(end2 <= end && end2 > start);
2413ca23e405STejun Heo 		}
2414ca23e405STejun Heo 	}
2415ca23e405STejun Heo 	last_end = offsets[last_area] + sizes[last_area];
2416ca23e405STejun Heo 
2417ca23e405STejun Heo 	if (vmalloc_end - vmalloc_start < last_end) {
2418ca23e405STejun Heo 		WARN_ON(true);
2419ca23e405STejun Heo 		return NULL;
2420ca23e405STejun Heo 	}
2421ca23e405STejun Heo 
24224d67d860SThomas Meyer 	vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
24234d67d860SThomas Meyer 	vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
2424ca23e405STejun Heo 	if (!vas || !vms)
2425f1db7afdSKautuk Consul 		goto err_free2;
2426ca23e405STejun Heo 
2427ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
2428ec3f64fcSDavid Rientjes 		vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL);
2429ec3f64fcSDavid Rientjes 		vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
2430ca23e405STejun Heo 		if (!vas[area] || !vms[area])
2431ca23e405STejun Heo 			goto err_free;
2432ca23e405STejun Heo 	}
2433ca23e405STejun Heo retry:
2434ca23e405STejun Heo 	spin_lock(&vmap_area_lock);
2435ca23e405STejun Heo 
2436ca23e405STejun Heo 	/* start scanning - we scan from the top, begin with the last area */
2437ca23e405STejun Heo 	area = term_area = last_area;
2438ca23e405STejun Heo 	start = offsets[area];
2439ca23e405STejun Heo 	end = start + sizes[area];
2440ca23e405STejun Heo 
2441ca23e405STejun Heo 	if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2442ca23e405STejun Heo 		base = vmalloc_end - last_end;
2443ca23e405STejun Heo 		goto found;
2444ca23e405STejun Heo 	}
2445ca23e405STejun Heo 	base = pvm_determine_end(&next, &prev, align) - end;
2446ca23e405STejun Heo 
2447ca23e405STejun Heo 	while (true) {
2448ca23e405STejun Heo 		BUG_ON(next && next->va_end <= base + end);
2449ca23e405STejun Heo 		BUG_ON(prev && prev->va_end > base + end);
2450ca23e405STejun Heo 
2451ca23e405STejun Heo 		/*
2452ca23e405STejun Heo 		 * base might have underflowed, add last_end before
2453ca23e405STejun Heo 		 * comparing.
2454ca23e405STejun Heo 		 */
2455ca23e405STejun Heo 		if (base + last_end < vmalloc_start + last_end) {
2456ca23e405STejun Heo 			spin_unlock(&vmap_area_lock);
2457ca23e405STejun Heo 			if (!purged) {
2458ca23e405STejun Heo 				purge_vmap_area_lazy();
2459ca23e405STejun Heo 				purged = true;
2460ca23e405STejun Heo 				goto retry;
2461ca23e405STejun Heo 			}
2462ca23e405STejun Heo 			goto err_free;
2463ca23e405STejun Heo 		}
2464ca23e405STejun Heo 
2465ca23e405STejun Heo 		/*
2466ca23e405STejun Heo 		 * If next overlaps, move base downwards so that it's
2467ca23e405STejun Heo 		 * right below next and then recheck.
2468ca23e405STejun Heo 		 */
2469ca23e405STejun Heo 		if (next && next->va_start < base + end) {
2470ca23e405STejun Heo 			base = pvm_determine_end(&next, &prev, align) - end;
2471ca23e405STejun Heo 			term_area = area;
2472ca23e405STejun Heo 			continue;
2473ca23e405STejun Heo 		}
2474ca23e405STejun Heo 
2475ca23e405STejun Heo 		/*
2476ca23e405STejun Heo 		 * If prev overlaps, shift down next and prev and move
2477ca23e405STejun Heo 		 * base so that it's right below new next and then
2478ca23e405STejun Heo 		 * recheck.
2479ca23e405STejun Heo 		 */
2480ca23e405STejun Heo 		if (prev && prev->va_end > base + start)  {
2481ca23e405STejun Heo 			next = prev;
2482ca23e405STejun Heo 			prev = node_to_va(rb_prev(&next->rb_node));
2483ca23e405STejun Heo 			base = pvm_determine_end(&next, &prev, align) - end;
2484ca23e405STejun Heo 			term_area = area;
2485ca23e405STejun Heo 			continue;
2486ca23e405STejun Heo 		}
2487ca23e405STejun Heo 
2488ca23e405STejun Heo 		/*
2489ca23e405STejun Heo 		 * This area fits, move on to the previous one.  If
2490ca23e405STejun Heo 		 * the previous one is the terminal one, we're done.
2491ca23e405STejun Heo 		 */
2492ca23e405STejun Heo 		area = (area + nr_vms - 1) % nr_vms;
2493ca23e405STejun Heo 		if (area == term_area)
2494ca23e405STejun Heo 			break;
2495ca23e405STejun Heo 		start = offsets[area];
2496ca23e405STejun Heo 		end = start + sizes[area];
2497ca23e405STejun Heo 		pvm_find_next_prev(base + end, &next, &prev);
2498ca23e405STejun Heo 	}
2499ca23e405STejun Heo found:
2500ca23e405STejun Heo 	/* we've found a fitting base, insert all va's */
2501ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
2502ca23e405STejun Heo 		struct vmap_area *va = vas[area];
2503ca23e405STejun Heo 
2504ca23e405STejun Heo 		va->va_start = base + offsets[area];
2505ca23e405STejun Heo 		va->va_end = va->va_start + sizes[area];
2506ca23e405STejun Heo 		__insert_vmap_area(va);
2507ca23e405STejun Heo 	}
2508ca23e405STejun Heo 
2509ca23e405STejun Heo 	vmap_area_pcpu_hole = base + offsets[last_area];
2510ca23e405STejun Heo 
2511ca23e405STejun Heo 	spin_unlock(&vmap_area_lock);
2512ca23e405STejun Heo 
2513ca23e405STejun Heo 	/* insert all vm's */
2514ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++)
2515ca23e405STejun Heo 		insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2516ca23e405STejun Heo 				  pcpu_get_vm_areas);
2517ca23e405STejun Heo 
2518ca23e405STejun Heo 	kfree(vas);
2519ca23e405STejun Heo 	return vms;
2520ca23e405STejun Heo 
2521ca23e405STejun Heo err_free:
2522ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
2523ca23e405STejun Heo 		kfree(vas[area]);
2524ca23e405STejun Heo 		kfree(vms[area]);
2525ca23e405STejun Heo 	}
2526f1db7afdSKautuk Consul err_free2:
2527ca23e405STejun Heo 	kfree(vas);
2528ca23e405STejun Heo 	kfree(vms);
2529ca23e405STejun Heo 	return NULL;
2530ca23e405STejun Heo }
2531ca23e405STejun Heo 
2532ca23e405STejun Heo /**
2533ca23e405STejun Heo  * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2534ca23e405STejun Heo  * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2535ca23e405STejun Heo  * @nr_vms: the number of allocated areas
2536ca23e405STejun Heo  *
2537ca23e405STejun Heo  * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2538ca23e405STejun Heo  */
2539ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2540ca23e405STejun Heo {
2541ca23e405STejun Heo 	int i;
2542ca23e405STejun Heo 
2543ca23e405STejun Heo 	for (i = 0; i < nr_vms; i++)
2544ca23e405STejun Heo 		free_vm_area(vms[i]);
2545ca23e405STejun Heo 	kfree(vms);
2546ca23e405STejun Heo }
25474f8b02b4STejun Heo #endif	/* CONFIG_SMP */
2548a10aa579SChristoph Lameter 
2549a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS
2550a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos)
2551d4033afdSJoonsoo Kim 	__acquires(&vmap_area_lock)
2552a10aa579SChristoph Lameter {
2553a10aa579SChristoph Lameter 	loff_t n = *pos;
2554d4033afdSJoonsoo Kim 	struct vmap_area *va;
2555a10aa579SChristoph Lameter 
2556d4033afdSJoonsoo Kim 	spin_lock(&vmap_area_lock);
2557d4033afdSJoonsoo Kim 	va = list_entry((&vmap_area_list)->next, typeof(*va), list);
2558d4033afdSJoonsoo Kim 	while (n > 0 && &va->list != &vmap_area_list) {
2559a10aa579SChristoph Lameter 		n--;
2560d4033afdSJoonsoo Kim 		va = list_entry(va->list.next, typeof(*va), list);
2561a10aa579SChristoph Lameter 	}
2562d4033afdSJoonsoo Kim 	if (!n && &va->list != &vmap_area_list)
2563d4033afdSJoonsoo Kim 		return va;
2564a10aa579SChristoph Lameter 
2565a10aa579SChristoph Lameter 	return NULL;
2566a10aa579SChristoph Lameter 
2567a10aa579SChristoph Lameter }
2568a10aa579SChristoph Lameter 
2569a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2570a10aa579SChristoph Lameter {
2571d4033afdSJoonsoo Kim 	struct vmap_area *va = p, *next;
2572a10aa579SChristoph Lameter 
2573a10aa579SChristoph Lameter 	++*pos;
2574d4033afdSJoonsoo Kim 	next = list_entry(va->list.next, typeof(*va), list);
2575d4033afdSJoonsoo Kim 	if (&next->list != &vmap_area_list)
2576d4033afdSJoonsoo Kim 		return next;
2577d4033afdSJoonsoo Kim 
2578d4033afdSJoonsoo Kim 	return NULL;
2579a10aa579SChristoph Lameter }
2580a10aa579SChristoph Lameter 
2581a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p)
2582d4033afdSJoonsoo Kim 	__releases(&vmap_area_lock)
2583a10aa579SChristoph Lameter {
2584d4033afdSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2585a10aa579SChristoph Lameter }
2586a10aa579SChristoph Lameter 
2587a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2588a47a126aSEric Dumazet {
2589e5adfffcSKirill A. Shutemov 	if (IS_ENABLED(CONFIG_NUMA)) {
2590a47a126aSEric Dumazet 		unsigned int nr, *counters = m->private;
2591a47a126aSEric Dumazet 
2592a47a126aSEric Dumazet 		if (!counters)
2593a47a126aSEric Dumazet 			return;
2594a47a126aSEric Dumazet 
25954341fa45SJoonsoo Kim 		/* Pair with smp_wmb() in clear_vm_unlist() */
2596d4033afdSJoonsoo Kim 		smp_rmb();
2597d4033afdSJoonsoo Kim 		if (v->flags & VM_UNLIST)
2598d4033afdSJoonsoo Kim 			return;
2599d4033afdSJoonsoo Kim 
2600a47a126aSEric Dumazet 		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2601a47a126aSEric Dumazet 
2602a47a126aSEric Dumazet 		for (nr = 0; nr < v->nr_pages; nr++)
2603a47a126aSEric Dumazet 			counters[page_to_nid(v->pages[nr])]++;
2604a47a126aSEric Dumazet 
2605a47a126aSEric Dumazet 		for_each_node_state(nr, N_HIGH_MEMORY)
2606a47a126aSEric Dumazet 			if (counters[nr])
2607a47a126aSEric Dumazet 				seq_printf(m, " N%u=%u", nr, counters[nr]);
2608a47a126aSEric Dumazet 	}
2609a47a126aSEric Dumazet }
2610a47a126aSEric Dumazet 
2611a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p)
2612a10aa579SChristoph Lameter {
2613d4033afdSJoonsoo Kim 	struct vmap_area *va = p;
2614d4033afdSJoonsoo Kim 	struct vm_struct *v;
2615d4033afdSJoonsoo Kim 
2616d4033afdSJoonsoo Kim 	if (va->flags & (VM_LAZY_FREE | VM_LAZY_FREEING))
2617d4033afdSJoonsoo Kim 		return 0;
2618d4033afdSJoonsoo Kim 
2619d4033afdSJoonsoo Kim 	if (!(va->flags & VM_VM_AREA)) {
2620d4033afdSJoonsoo Kim 		seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n",
2621d4033afdSJoonsoo Kim 			(void *)va->va_start, (void *)va->va_end,
2622d4033afdSJoonsoo Kim 					va->va_end - va->va_start);
2623d4033afdSJoonsoo Kim 		return 0;
2624d4033afdSJoonsoo Kim 	}
2625d4033afdSJoonsoo Kim 
2626d4033afdSJoonsoo Kim 	v = va->vm;
2627a10aa579SChristoph Lameter 
262845ec1690SKees Cook 	seq_printf(m, "0x%pK-0x%pK %7ld",
2629a10aa579SChristoph Lameter 		v->addr, v->addr + v->size, v->size);
2630a10aa579SChristoph Lameter 
263162c70bceSJoe Perches 	if (v->caller)
263262c70bceSJoe Perches 		seq_printf(m, " %pS", v->caller);
263323016969SChristoph Lameter 
2634a10aa579SChristoph Lameter 	if (v->nr_pages)
2635a10aa579SChristoph Lameter 		seq_printf(m, " pages=%d", v->nr_pages);
2636a10aa579SChristoph Lameter 
2637a10aa579SChristoph Lameter 	if (v->phys_addr)
2638ffa71f33SKenji Kaneshige 		seq_printf(m, " phys=%llx", (unsigned long long)v->phys_addr);
2639a10aa579SChristoph Lameter 
2640a10aa579SChristoph Lameter 	if (v->flags & VM_IOREMAP)
2641a10aa579SChristoph Lameter 		seq_printf(m, " ioremap");
2642a10aa579SChristoph Lameter 
2643a10aa579SChristoph Lameter 	if (v->flags & VM_ALLOC)
2644a10aa579SChristoph Lameter 		seq_printf(m, " vmalloc");
2645a10aa579SChristoph Lameter 
2646a10aa579SChristoph Lameter 	if (v->flags & VM_MAP)
2647a10aa579SChristoph Lameter 		seq_printf(m, " vmap");
2648a10aa579SChristoph Lameter 
2649a10aa579SChristoph Lameter 	if (v->flags & VM_USERMAP)
2650a10aa579SChristoph Lameter 		seq_printf(m, " user");
2651a10aa579SChristoph Lameter 
2652a10aa579SChristoph Lameter 	if (v->flags & VM_VPAGES)
2653a10aa579SChristoph Lameter 		seq_printf(m, " vpages");
2654a10aa579SChristoph Lameter 
2655a47a126aSEric Dumazet 	show_numa_info(m, v);
2656a10aa579SChristoph Lameter 	seq_putc(m, '\n');
2657a10aa579SChristoph Lameter 	return 0;
2658a10aa579SChristoph Lameter }
2659a10aa579SChristoph Lameter 
26605f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = {
2661a10aa579SChristoph Lameter 	.start = s_start,
2662a10aa579SChristoph Lameter 	.next = s_next,
2663a10aa579SChristoph Lameter 	.stop = s_stop,
2664a10aa579SChristoph Lameter 	.show = s_show,
2665a10aa579SChristoph Lameter };
26665f6a6a9cSAlexey Dobriyan 
26675f6a6a9cSAlexey Dobriyan static int vmalloc_open(struct inode *inode, struct file *file)
26685f6a6a9cSAlexey Dobriyan {
26695f6a6a9cSAlexey Dobriyan 	unsigned int *ptr = NULL;
26705f6a6a9cSAlexey Dobriyan 	int ret;
26715f6a6a9cSAlexey Dobriyan 
2672e5adfffcSKirill A. Shutemov 	if (IS_ENABLED(CONFIG_NUMA)) {
26735f6a6a9cSAlexey Dobriyan 		ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
267451980ac9SKulikov Vasiliy 		if (ptr == NULL)
267551980ac9SKulikov Vasiliy 			return -ENOMEM;
267651980ac9SKulikov Vasiliy 	}
26775f6a6a9cSAlexey Dobriyan 	ret = seq_open(file, &vmalloc_op);
26785f6a6a9cSAlexey Dobriyan 	if (!ret) {
26795f6a6a9cSAlexey Dobriyan 		struct seq_file *m = file->private_data;
26805f6a6a9cSAlexey Dobriyan 		m->private = ptr;
26815f6a6a9cSAlexey Dobriyan 	} else
26825f6a6a9cSAlexey Dobriyan 		kfree(ptr);
26835f6a6a9cSAlexey Dobriyan 	return ret;
26845f6a6a9cSAlexey Dobriyan }
26855f6a6a9cSAlexey Dobriyan 
26865f6a6a9cSAlexey Dobriyan static const struct file_operations proc_vmalloc_operations = {
26875f6a6a9cSAlexey Dobriyan 	.open		= vmalloc_open,
26885f6a6a9cSAlexey Dobriyan 	.read		= seq_read,
26895f6a6a9cSAlexey Dobriyan 	.llseek		= seq_lseek,
26905f6a6a9cSAlexey Dobriyan 	.release	= seq_release_private,
26915f6a6a9cSAlexey Dobriyan };
26925f6a6a9cSAlexey Dobriyan 
26935f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void)
26945f6a6a9cSAlexey Dobriyan {
26955f6a6a9cSAlexey Dobriyan 	proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
26965f6a6a9cSAlexey Dobriyan 	return 0;
26975f6a6a9cSAlexey Dobriyan }
26985f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init);
2699db3808c1SJoonsoo Kim 
2700db3808c1SJoonsoo Kim void get_vmalloc_info(struct vmalloc_info *vmi)
2701db3808c1SJoonsoo Kim {
2702f98782ddSJoonsoo Kim 	struct vmap_area *va;
2703db3808c1SJoonsoo Kim 	unsigned long free_area_size;
2704db3808c1SJoonsoo Kim 	unsigned long prev_end;
2705db3808c1SJoonsoo Kim 
2706db3808c1SJoonsoo Kim 	vmi->used = 0;
2707db3808c1SJoonsoo Kim 	vmi->largest_chunk = 0;
2708db3808c1SJoonsoo Kim 
2709db3808c1SJoonsoo Kim 	prev_end = VMALLOC_START;
2710db3808c1SJoonsoo Kim 
2711f98782ddSJoonsoo Kim 	spin_lock(&vmap_area_lock);
2712db3808c1SJoonsoo Kim 
2713f98782ddSJoonsoo Kim 	if (list_empty(&vmap_area_list)) {
2714f98782ddSJoonsoo Kim 		vmi->largest_chunk = VMALLOC_TOTAL;
2715f98782ddSJoonsoo Kim 		goto out;
2716f98782ddSJoonsoo Kim 	}
2717f98782ddSJoonsoo Kim 
2718f98782ddSJoonsoo Kim 	list_for_each_entry(va, &vmap_area_list, list) {
2719f98782ddSJoonsoo Kim 		unsigned long addr = va->va_start;
2720db3808c1SJoonsoo Kim 
2721db3808c1SJoonsoo Kim 		/*
2722f98782ddSJoonsoo Kim 		 * Some archs keep another range for modules in vmalloc space
2723db3808c1SJoonsoo Kim 		 */
2724db3808c1SJoonsoo Kim 		if (addr < VMALLOC_START)
2725db3808c1SJoonsoo Kim 			continue;
2726db3808c1SJoonsoo Kim 		if (addr >= VMALLOC_END)
2727db3808c1SJoonsoo Kim 			break;
2728db3808c1SJoonsoo Kim 
2729f98782ddSJoonsoo Kim 		if (va->flags & (VM_LAZY_FREE | VM_LAZY_FREEING))
2730f98782ddSJoonsoo Kim 			continue;
2731f98782ddSJoonsoo Kim 
2732f98782ddSJoonsoo Kim 		vmi->used += (va->va_end - va->va_start);
2733db3808c1SJoonsoo Kim 
2734db3808c1SJoonsoo Kim 		free_area_size = addr - prev_end;
2735db3808c1SJoonsoo Kim 		if (vmi->largest_chunk < free_area_size)
2736db3808c1SJoonsoo Kim 			vmi->largest_chunk = free_area_size;
2737db3808c1SJoonsoo Kim 
2738f98782ddSJoonsoo Kim 		prev_end = va->va_end;
2739db3808c1SJoonsoo Kim 	}
2740db3808c1SJoonsoo Kim 
2741db3808c1SJoonsoo Kim 	if (VMALLOC_END - prev_end > vmi->largest_chunk)
2742db3808c1SJoonsoo Kim 		vmi->largest_chunk = VMALLOC_END - prev_end;
2743db3808c1SJoonsoo Kim 
2744f98782ddSJoonsoo Kim out:
2745f98782ddSJoonsoo Kim 	spin_unlock(&vmap_area_lock);
2746db3808c1SJoonsoo Kim }
2747a10aa579SChristoph Lameter #endif
2748a10aa579SChristoph Lameter 
2749