xref: /linux/mm/vmalloc.c (revision 89699605fe7cfd8611900346f61cb6cbf179b10a)
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>
29db64fe02SNick Piggin #include <asm/atomic.h>
301da177e4SLinus Torvalds #include <asm/uaccess.h>
311da177e4SLinus Torvalds #include <asm/tlbflush.h>
322dca6999SDavid Miller #include <asm/shmparam.h>
331da177e4SLinus Torvalds 
34db64fe02SNick Piggin /*** Page table manipulation functions ***/
35b221385bSAdrian Bunk 
361da177e4SLinus Torvalds static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	pte_t *pte;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	pte = pte_offset_kernel(pmd, addr);
411da177e4SLinus Torvalds 	do {
421da177e4SLinus Torvalds 		pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
431da177e4SLinus Torvalds 		WARN_ON(!pte_none(ptent) && !pte_present(ptent));
441da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
451da177e4SLinus Torvalds }
461da177e4SLinus Torvalds 
47db64fe02SNick Piggin static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds 	pmd_t *pmd;
501da177e4SLinus Torvalds 	unsigned long next;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
531da177e4SLinus Torvalds 	do {
541da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
551da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
561da177e4SLinus Torvalds 			continue;
571da177e4SLinus Torvalds 		vunmap_pte_range(pmd, addr, next);
581da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
61db64fe02SNick Piggin static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	pud_t *pud;
641da177e4SLinus Torvalds 	unsigned long next;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
671da177e4SLinus Torvalds 	do {
681da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
691da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
701da177e4SLinus Torvalds 			continue;
711da177e4SLinus Torvalds 		vunmap_pmd_range(pud, addr, next);
721da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
75db64fe02SNick Piggin static void vunmap_page_range(unsigned long addr, unsigned long end)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	pgd_t *pgd;
781da177e4SLinus Torvalds 	unsigned long next;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	BUG_ON(addr >= end);
811da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
821da177e4SLinus Torvalds 	do {
831da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
841da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
851da177e4SLinus Torvalds 			continue;
861da177e4SLinus Torvalds 		vunmap_pud_range(pgd, addr, next);
871da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
91db64fe02SNick Piggin 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	pte_t *pte;
941da177e4SLinus Torvalds 
95db64fe02SNick Piggin 	/*
96db64fe02SNick Piggin 	 * nr is a running index into the array which helps higher level
97db64fe02SNick Piggin 	 * callers keep track of where we're up to.
98db64fe02SNick Piggin 	 */
99db64fe02SNick Piggin 
100872fec16SHugh Dickins 	pte = pte_alloc_kernel(pmd, addr);
1011da177e4SLinus Torvalds 	if (!pte)
1021da177e4SLinus Torvalds 		return -ENOMEM;
1031da177e4SLinus Torvalds 	do {
104db64fe02SNick Piggin 		struct page *page = pages[*nr];
105db64fe02SNick Piggin 
106db64fe02SNick Piggin 		if (WARN_ON(!pte_none(*pte)))
107db64fe02SNick Piggin 			return -EBUSY;
108db64fe02SNick Piggin 		if (WARN_ON(!page))
1091da177e4SLinus Torvalds 			return -ENOMEM;
1101da177e4SLinus Torvalds 		set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
111db64fe02SNick Piggin 		(*nr)++;
1121da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1131da177e4SLinus Torvalds 	return 0;
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
116db64fe02SNick Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr,
117db64fe02SNick Piggin 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	pmd_t *pmd;
1201da177e4SLinus Torvalds 	unsigned long next;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	pmd = pmd_alloc(&init_mm, pud, addr);
1231da177e4SLinus Torvalds 	if (!pmd)
1241da177e4SLinus Torvalds 		return -ENOMEM;
1251da177e4SLinus Torvalds 	do {
1261da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
127db64fe02SNick Piggin 		if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
1281da177e4SLinus Torvalds 			return -ENOMEM;
1291da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
1301da177e4SLinus Torvalds 	return 0;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
133db64fe02SNick Piggin static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
134db64fe02SNick Piggin 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	pud_t *pud;
1371da177e4SLinus Torvalds 	unsigned long next;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	pud = pud_alloc(&init_mm, pgd, addr);
1401da177e4SLinus Torvalds 	if (!pud)
1411da177e4SLinus Torvalds 		return -ENOMEM;
1421da177e4SLinus Torvalds 	do {
1431da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
144db64fe02SNick Piggin 		if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
1451da177e4SLinus Torvalds 			return -ENOMEM;
1461da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
1471da177e4SLinus Torvalds 	return 0;
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
150db64fe02SNick Piggin /*
151db64fe02SNick Piggin  * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
152db64fe02SNick Piggin  * will have pfns corresponding to the "pages" array.
153db64fe02SNick Piggin  *
154db64fe02SNick Piggin  * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
155db64fe02SNick Piggin  */
1568fc48985STejun Heo static int vmap_page_range_noflush(unsigned long start, unsigned long end,
157db64fe02SNick Piggin 				   pgprot_t prot, struct page **pages)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	pgd_t *pgd;
1601da177e4SLinus Torvalds 	unsigned long next;
1612e4e27c7SAdam Lackorzynski 	unsigned long addr = start;
162db64fe02SNick Piggin 	int err = 0;
163db64fe02SNick Piggin 	int nr = 0;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1661da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
1671da177e4SLinus Torvalds 	do {
1681da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
169db64fe02SNick Piggin 		err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
1701da177e4SLinus Torvalds 		if (err)
171bf88c8c8SFigo.zhang 			return err;
1721da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
173db64fe02SNick Piggin 
174db64fe02SNick Piggin 	return nr;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1778fc48985STejun Heo static int vmap_page_range(unsigned long start, unsigned long end,
1788fc48985STejun Heo 			   pgprot_t prot, struct page **pages)
1798fc48985STejun Heo {
1808fc48985STejun Heo 	int ret;
1818fc48985STejun Heo 
1828fc48985STejun Heo 	ret = vmap_page_range_noflush(start, end, prot, pages);
1838fc48985STejun Heo 	flush_cache_vmap(start, end);
1848fc48985STejun Heo 	return ret;
1858fc48985STejun Heo }
1868fc48985STejun Heo 
18781ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x)
18873bdf0a6SLinus Torvalds {
18973bdf0a6SLinus Torvalds 	/*
190ab4f2ee1SRussell King 	 * ARM, x86-64 and sparc64 put modules in a special place,
19173bdf0a6SLinus Torvalds 	 * and fall back on vmalloc() if that fails. Others
19273bdf0a6SLinus Torvalds 	 * just put it in the vmalloc space.
19373bdf0a6SLinus Torvalds 	 */
19473bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
19573bdf0a6SLinus Torvalds 	unsigned long addr = (unsigned long)x;
19673bdf0a6SLinus Torvalds 	if (addr >= MODULES_VADDR && addr < MODULES_END)
19773bdf0a6SLinus Torvalds 		return 1;
19873bdf0a6SLinus Torvalds #endif
19973bdf0a6SLinus Torvalds 	return is_vmalloc_addr(x);
20073bdf0a6SLinus Torvalds }
20173bdf0a6SLinus Torvalds 
20248667e7aSChristoph Lameter /*
203db64fe02SNick Piggin  * Walk a vmap address to the struct page it maps.
20448667e7aSChristoph Lameter  */
205b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *vmalloc_addr)
20648667e7aSChristoph Lameter {
20748667e7aSChristoph Lameter 	unsigned long addr = (unsigned long) vmalloc_addr;
20848667e7aSChristoph Lameter 	struct page *page = NULL;
20948667e7aSChristoph Lameter 	pgd_t *pgd = pgd_offset_k(addr);
21048667e7aSChristoph Lameter 
2117aa413deSIngo Molnar 	/*
2127aa413deSIngo Molnar 	 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
2137aa413deSIngo Molnar 	 * architectures that do not vmalloc module space
2147aa413deSIngo Molnar 	 */
21573bdf0a6SLinus Torvalds 	VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
21659ea7463SJiri Slaby 
21748667e7aSChristoph Lameter 	if (!pgd_none(*pgd)) {
218db64fe02SNick Piggin 		pud_t *pud = pud_offset(pgd, addr);
21948667e7aSChristoph Lameter 		if (!pud_none(*pud)) {
220db64fe02SNick Piggin 			pmd_t *pmd = pmd_offset(pud, addr);
22148667e7aSChristoph Lameter 			if (!pmd_none(*pmd)) {
222db64fe02SNick Piggin 				pte_t *ptep, pte;
223db64fe02SNick Piggin 
22448667e7aSChristoph Lameter 				ptep = pte_offset_map(pmd, addr);
22548667e7aSChristoph Lameter 				pte = *ptep;
22648667e7aSChristoph Lameter 				if (pte_present(pte))
22748667e7aSChristoph Lameter 					page = pte_page(pte);
22848667e7aSChristoph Lameter 				pte_unmap(ptep);
22948667e7aSChristoph Lameter 			}
23048667e7aSChristoph Lameter 		}
23148667e7aSChristoph Lameter 	}
23248667e7aSChristoph Lameter 	return page;
23348667e7aSChristoph Lameter }
23448667e7aSChristoph Lameter EXPORT_SYMBOL(vmalloc_to_page);
23548667e7aSChristoph Lameter 
23648667e7aSChristoph Lameter /*
23748667e7aSChristoph Lameter  * Map a vmalloc()-space virtual address to the physical page frame number.
23848667e7aSChristoph Lameter  */
239b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
24048667e7aSChristoph Lameter {
24148667e7aSChristoph Lameter 	return page_to_pfn(vmalloc_to_page(vmalloc_addr));
24248667e7aSChristoph Lameter }
24348667e7aSChristoph Lameter EXPORT_SYMBOL(vmalloc_to_pfn);
24448667e7aSChristoph Lameter 
245db64fe02SNick Piggin 
246db64fe02SNick Piggin /*** Global kva allocator ***/
247db64fe02SNick Piggin 
248db64fe02SNick Piggin #define VM_LAZY_FREE	0x01
249db64fe02SNick Piggin #define VM_LAZY_FREEING	0x02
250db64fe02SNick Piggin #define VM_VM_AREA	0x04
251db64fe02SNick Piggin 
252db64fe02SNick Piggin struct vmap_area {
253db64fe02SNick Piggin 	unsigned long va_start;
254db64fe02SNick Piggin 	unsigned long va_end;
255db64fe02SNick Piggin 	unsigned long flags;
256db64fe02SNick Piggin 	struct rb_node rb_node;		/* address sorted rbtree */
257db64fe02SNick Piggin 	struct list_head list;		/* address sorted list */
258db64fe02SNick Piggin 	struct list_head purge_list;	/* "lazy purge" list */
259db64fe02SNick Piggin 	void *private;
260db64fe02SNick Piggin 	struct rcu_head rcu_head;
261db64fe02SNick Piggin };
262db64fe02SNick Piggin 
263db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock);
264db64fe02SNick Piggin static LIST_HEAD(vmap_area_list);
265*89699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT;
266*89699605SNick Piggin 
267*89699605SNick Piggin /* The vmap cache globals are protected by vmap_area_lock */
268*89699605SNick Piggin static struct rb_node *free_vmap_cache;
269*89699605SNick Piggin static unsigned long cached_hole_size;
270*89699605SNick Piggin static unsigned long cached_vstart;
271*89699605SNick Piggin static unsigned long cached_align;
272*89699605SNick Piggin 
273ca23e405STejun Heo static unsigned long vmap_area_pcpu_hole;
274db64fe02SNick Piggin 
275db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr)
2761da177e4SLinus Torvalds {
277db64fe02SNick Piggin 	struct rb_node *n = vmap_area_root.rb_node;
278db64fe02SNick Piggin 
279db64fe02SNick Piggin 	while (n) {
280db64fe02SNick Piggin 		struct vmap_area *va;
281db64fe02SNick Piggin 
282db64fe02SNick Piggin 		va = rb_entry(n, struct vmap_area, rb_node);
283db64fe02SNick Piggin 		if (addr < va->va_start)
284db64fe02SNick Piggin 			n = n->rb_left;
285db64fe02SNick Piggin 		else if (addr > va->va_start)
286db64fe02SNick Piggin 			n = n->rb_right;
287db64fe02SNick Piggin 		else
288db64fe02SNick Piggin 			return va;
289db64fe02SNick Piggin 	}
290db64fe02SNick Piggin 
291db64fe02SNick Piggin 	return NULL;
292db64fe02SNick Piggin }
293db64fe02SNick Piggin 
294db64fe02SNick Piggin static void __insert_vmap_area(struct vmap_area *va)
295db64fe02SNick Piggin {
296db64fe02SNick Piggin 	struct rb_node **p = &vmap_area_root.rb_node;
297db64fe02SNick Piggin 	struct rb_node *parent = NULL;
298db64fe02SNick Piggin 	struct rb_node *tmp;
299db64fe02SNick Piggin 
300db64fe02SNick Piggin 	while (*p) {
301170168d0SNamhyung Kim 		struct vmap_area *tmp_va;
302db64fe02SNick Piggin 
303db64fe02SNick Piggin 		parent = *p;
304170168d0SNamhyung Kim 		tmp_va = rb_entry(parent, struct vmap_area, rb_node);
305170168d0SNamhyung Kim 		if (va->va_start < tmp_va->va_end)
306db64fe02SNick Piggin 			p = &(*p)->rb_left;
307170168d0SNamhyung Kim 		else if (va->va_end > tmp_va->va_start)
308db64fe02SNick Piggin 			p = &(*p)->rb_right;
309db64fe02SNick Piggin 		else
310db64fe02SNick Piggin 			BUG();
311db64fe02SNick Piggin 	}
312db64fe02SNick Piggin 
313db64fe02SNick Piggin 	rb_link_node(&va->rb_node, parent, p);
314db64fe02SNick Piggin 	rb_insert_color(&va->rb_node, &vmap_area_root);
315db64fe02SNick Piggin 
316db64fe02SNick Piggin 	/* address-sort this list so it is usable like the vmlist */
317db64fe02SNick Piggin 	tmp = rb_prev(&va->rb_node);
318db64fe02SNick Piggin 	if (tmp) {
319db64fe02SNick Piggin 		struct vmap_area *prev;
320db64fe02SNick Piggin 		prev = rb_entry(tmp, struct vmap_area, rb_node);
321db64fe02SNick Piggin 		list_add_rcu(&va->list, &prev->list);
322db64fe02SNick Piggin 	} else
323db64fe02SNick Piggin 		list_add_rcu(&va->list, &vmap_area_list);
324db64fe02SNick Piggin }
325db64fe02SNick Piggin 
326db64fe02SNick Piggin static void purge_vmap_area_lazy(void);
327db64fe02SNick Piggin 
328db64fe02SNick Piggin /*
329db64fe02SNick Piggin  * Allocate a region of KVA of the specified size and alignment, within the
330db64fe02SNick Piggin  * vstart and vend.
331db64fe02SNick Piggin  */
332db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size,
333db64fe02SNick Piggin 				unsigned long align,
334db64fe02SNick Piggin 				unsigned long vstart, unsigned long vend,
335db64fe02SNick Piggin 				int node, gfp_t gfp_mask)
336db64fe02SNick Piggin {
337db64fe02SNick Piggin 	struct vmap_area *va;
338db64fe02SNick Piggin 	struct rb_node *n;
3391da177e4SLinus Torvalds 	unsigned long addr;
340db64fe02SNick Piggin 	int purged = 0;
341*89699605SNick Piggin 	struct vmap_area *first;
342db64fe02SNick Piggin 
3437766970cSNick Piggin 	BUG_ON(!size);
344db64fe02SNick Piggin 	BUG_ON(size & ~PAGE_MASK);
345*89699605SNick Piggin 	BUG_ON(!is_power_of_2(align));
346db64fe02SNick Piggin 
347db64fe02SNick Piggin 	va = kmalloc_node(sizeof(struct vmap_area),
348db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
349db64fe02SNick Piggin 	if (unlikely(!va))
350db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
351db64fe02SNick Piggin 
352db64fe02SNick Piggin retry:
353db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
354*89699605SNick Piggin 	/*
355*89699605SNick Piggin 	 * Invalidate cache if we have more permissive parameters.
356*89699605SNick Piggin 	 * cached_hole_size notes the largest hole noticed _below_
357*89699605SNick Piggin 	 * the vmap_area cached in free_vmap_cache: if size fits
358*89699605SNick Piggin 	 * into that hole, we want to scan from vstart to reuse
359*89699605SNick Piggin 	 * the hole instead of allocating above free_vmap_cache.
360*89699605SNick Piggin 	 * Note that __free_vmap_area may update free_vmap_cache
361*89699605SNick Piggin 	 * without updating cached_hole_size or cached_align.
362*89699605SNick Piggin 	 */
363*89699605SNick Piggin 	if (!free_vmap_cache ||
364*89699605SNick Piggin 			size < cached_hole_size ||
365*89699605SNick Piggin 			vstart < cached_vstart ||
366*89699605SNick Piggin 			align < cached_align) {
367*89699605SNick Piggin nocache:
368*89699605SNick Piggin 		cached_hole_size = 0;
369*89699605SNick Piggin 		free_vmap_cache = NULL;
370*89699605SNick Piggin 	}
371*89699605SNick Piggin 	/* record if we encounter less permissive parameters */
372*89699605SNick Piggin 	cached_vstart = vstart;
373*89699605SNick Piggin 	cached_align = align;
374*89699605SNick Piggin 
375*89699605SNick Piggin 	/* find starting point for our search */
376*89699605SNick Piggin 	if (free_vmap_cache) {
377*89699605SNick Piggin 		first = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
378*89699605SNick Piggin 		addr = ALIGN(first->va_end + PAGE_SIZE, align);
379*89699605SNick Piggin 		if (addr < vstart)
380*89699605SNick Piggin 			goto nocache;
3817766970cSNick Piggin 		if (addr + size - 1 < addr)
3827766970cSNick Piggin 			goto overflow;
3837766970cSNick Piggin 
384*89699605SNick Piggin 	} else {
385*89699605SNick Piggin 		addr = ALIGN(vstart, align);
386*89699605SNick Piggin 		if (addr + size - 1 < addr)
387*89699605SNick Piggin 			goto overflow;
388db64fe02SNick Piggin 
389*89699605SNick Piggin 		n = vmap_area_root.rb_node;
390*89699605SNick Piggin 		first = NULL;
391*89699605SNick Piggin 
392*89699605SNick Piggin 		while (n) {
393db64fe02SNick Piggin 			struct vmap_area *tmp;
394db64fe02SNick Piggin 			tmp = rb_entry(n, struct vmap_area, rb_node);
395db64fe02SNick Piggin 			if (tmp->va_end >= addr) {
396db64fe02SNick Piggin 				first = tmp;
397*89699605SNick Piggin 				if (tmp->va_start <= addr)
398*89699605SNick Piggin 					break;
399db64fe02SNick Piggin 				n = n->rb_left;
400*89699605SNick Piggin 			} else
401db64fe02SNick Piggin 				n = n->rb_right;
402db64fe02SNick Piggin 		}
403db64fe02SNick Piggin 
404db64fe02SNick Piggin 		if (!first)
405db64fe02SNick Piggin 			goto found;
406db64fe02SNick Piggin 	}
407db64fe02SNick Piggin 
408*89699605SNick Piggin 	/* from the starting point, walk areas until a suitable hole is found */
409*89699605SNick Piggin 	while (addr + size >= first->va_start && addr + size <= vend) {
410*89699605SNick Piggin 		if (addr + cached_hole_size < first->va_start)
411*89699605SNick Piggin 			cached_hole_size = first->va_start - addr;
412db64fe02SNick Piggin 		addr = ALIGN(first->va_end + PAGE_SIZE, align);
4137766970cSNick Piggin 		if (addr + size - 1 < addr)
4147766970cSNick Piggin 			goto overflow;
415db64fe02SNick Piggin 
416db64fe02SNick Piggin 		n = rb_next(&first->rb_node);
417db64fe02SNick Piggin 		if (n)
418db64fe02SNick Piggin 			first = rb_entry(n, struct vmap_area, rb_node);
419db64fe02SNick Piggin 		else
420db64fe02SNick Piggin 			goto found;
421db64fe02SNick Piggin 	}
422*89699605SNick Piggin 
423db64fe02SNick Piggin found:
424*89699605SNick Piggin 	if (addr + size > vend)
425*89699605SNick Piggin 		goto overflow;
426*89699605SNick Piggin 
427*89699605SNick Piggin 	va->va_start = addr;
428*89699605SNick Piggin 	va->va_end = addr + size;
429*89699605SNick Piggin 	va->flags = 0;
430*89699605SNick Piggin 	__insert_vmap_area(va);
431*89699605SNick Piggin 	free_vmap_cache = &va->rb_node;
432*89699605SNick Piggin 	spin_unlock(&vmap_area_lock);
433*89699605SNick Piggin 
434*89699605SNick Piggin 	BUG_ON(va->va_start & (align-1));
435*89699605SNick Piggin 	BUG_ON(va->va_start < vstart);
436*89699605SNick Piggin 	BUG_ON(va->va_end > vend);
437*89699605SNick Piggin 
438*89699605SNick Piggin 	return va;
439*89699605SNick Piggin 
4407766970cSNick Piggin overflow:
441db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
442db64fe02SNick Piggin 	if (!purged) {
443db64fe02SNick Piggin 		purge_vmap_area_lazy();
444db64fe02SNick Piggin 		purged = 1;
445db64fe02SNick Piggin 		goto retry;
446db64fe02SNick Piggin 	}
447db64fe02SNick Piggin 	if (printk_ratelimit())
448c1279c4eSGlauber Costa 		printk(KERN_WARNING
449c1279c4eSGlauber Costa 			"vmap allocation for size %lu failed: "
450c1279c4eSGlauber Costa 			"use vmalloc=<size> to increase size.\n", size);
4512498ce42SRalph Wuerthner 	kfree(va);
452db64fe02SNick Piggin 	return ERR_PTR(-EBUSY);
453db64fe02SNick Piggin }
454db64fe02SNick Piggin 
455db64fe02SNick Piggin static void rcu_free_va(struct rcu_head *head)
456db64fe02SNick Piggin {
457db64fe02SNick Piggin 	struct vmap_area *va = container_of(head, struct vmap_area, rcu_head);
458db64fe02SNick Piggin 
459db64fe02SNick Piggin 	kfree(va);
460db64fe02SNick Piggin }
461db64fe02SNick Piggin 
462db64fe02SNick Piggin static void __free_vmap_area(struct vmap_area *va)
463db64fe02SNick Piggin {
464db64fe02SNick Piggin 	BUG_ON(RB_EMPTY_NODE(&va->rb_node));
465*89699605SNick Piggin 
466*89699605SNick Piggin 	if (free_vmap_cache) {
467*89699605SNick Piggin 		if (va->va_end < cached_vstart) {
468*89699605SNick Piggin 			free_vmap_cache = NULL;
469*89699605SNick Piggin 		} else {
470*89699605SNick Piggin 			struct vmap_area *cache;
471*89699605SNick Piggin 			cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
472*89699605SNick Piggin 			if (va->va_start <= cache->va_start) {
473*89699605SNick Piggin 				free_vmap_cache = rb_prev(&va->rb_node);
474*89699605SNick Piggin 				/*
475*89699605SNick Piggin 				 * We don't try to update cached_hole_size or
476*89699605SNick Piggin 				 * cached_align, but it won't go very wrong.
477*89699605SNick Piggin 				 */
478*89699605SNick Piggin 			}
479*89699605SNick Piggin 		}
480*89699605SNick Piggin 	}
481db64fe02SNick Piggin 	rb_erase(&va->rb_node, &vmap_area_root);
482db64fe02SNick Piggin 	RB_CLEAR_NODE(&va->rb_node);
483db64fe02SNick Piggin 	list_del_rcu(&va->list);
484db64fe02SNick Piggin 
485ca23e405STejun Heo 	/*
486ca23e405STejun Heo 	 * Track the highest possible candidate for pcpu area
487ca23e405STejun Heo 	 * allocation.  Areas outside of vmalloc area can be returned
488ca23e405STejun Heo 	 * here too, consider only end addresses which fall inside
489ca23e405STejun Heo 	 * vmalloc area proper.
490ca23e405STejun Heo 	 */
491ca23e405STejun Heo 	if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
492ca23e405STejun Heo 		vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
493ca23e405STejun Heo 
494db64fe02SNick Piggin 	call_rcu(&va->rcu_head, rcu_free_va);
495db64fe02SNick Piggin }
496db64fe02SNick Piggin 
497db64fe02SNick Piggin /*
498db64fe02SNick Piggin  * Free a region of KVA allocated by alloc_vmap_area
499db64fe02SNick Piggin  */
500db64fe02SNick Piggin static void free_vmap_area(struct vmap_area *va)
501db64fe02SNick Piggin {
502db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
503db64fe02SNick Piggin 	__free_vmap_area(va);
504db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
505db64fe02SNick Piggin }
506db64fe02SNick Piggin 
507db64fe02SNick Piggin /*
508db64fe02SNick Piggin  * Clear the pagetable entries of a given vmap_area
509db64fe02SNick Piggin  */
510db64fe02SNick Piggin static void unmap_vmap_area(struct vmap_area *va)
511db64fe02SNick Piggin {
512db64fe02SNick Piggin 	vunmap_page_range(va->va_start, va->va_end);
513db64fe02SNick Piggin }
514db64fe02SNick Piggin 
515cd52858cSNick Piggin static void vmap_debug_free_range(unsigned long start, unsigned long end)
516cd52858cSNick Piggin {
517cd52858cSNick Piggin 	/*
518cd52858cSNick Piggin 	 * Unmap page tables and force a TLB flush immediately if
519cd52858cSNick Piggin 	 * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
520cd52858cSNick Piggin 	 * bugs similarly to those in linear kernel virtual address
521cd52858cSNick Piggin 	 * space after a page has been freed.
522cd52858cSNick Piggin 	 *
523cd52858cSNick Piggin 	 * All the lazy freeing logic is still retained, in order to
524cd52858cSNick Piggin 	 * minimise intrusiveness of this debugging feature.
525cd52858cSNick Piggin 	 *
526cd52858cSNick Piggin 	 * This is going to be *slow* (linear kernel virtual address
527cd52858cSNick Piggin 	 * debugging doesn't do a broadcast TLB flush so it is a lot
528cd52858cSNick Piggin 	 * faster).
529cd52858cSNick Piggin 	 */
530cd52858cSNick Piggin #ifdef CONFIG_DEBUG_PAGEALLOC
531cd52858cSNick Piggin 	vunmap_page_range(start, end);
532cd52858cSNick Piggin 	flush_tlb_kernel_range(start, end);
533cd52858cSNick Piggin #endif
534cd52858cSNick Piggin }
535cd52858cSNick Piggin 
536db64fe02SNick Piggin /*
537db64fe02SNick Piggin  * lazy_max_pages is the maximum amount of virtual address space we gather up
538db64fe02SNick Piggin  * before attempting to purge with a TLB flush.
539db64fe02SNick Piggin  *
540db64fe02SNick Piggin  * There is a tradeoff here: a larger number will cover more kernel page tables
541db64fe02SNick Piggin  * and take slightly longer to purge, but it will linearly reduce the number of
542db64fe02SNick Piggin  * global TLB flushes that must be performed. It would seem natural to scale
543db64fe02SNick Piggin  * this number up linearly with the number of CPUs (because vmapping activity
544db64fe02SNick Piggin  * could also scale linearly with the number of CPUs), however it is likely
545db64fe02SNick Piggin  * that in practice, workloads might be constrained in other ways that mean
546db64fe02SNick Piggin  * vmap activity will not scale linearly with CPUs. Also, I want to be
547db64fe02SNick Piggin  * conservative and not introduce a big latency on huge systems, so go with
548db64fe02SNick Piggin  * a less aggressive log scale. It will still be an improvement over the old
549db64fe02SNick Piggin  * code, and it will be simple to change the scale factor if we find that it
550db64fe02SNick Piggin  * becomes a problem on bigger systems.
551db64fe02SNick Piggin  */
552db64fe02SNick Piggin static unsigned long lazy_max_pages(void)
553db64fe02SNick Piggin {
554db64fe02SNick Piggin 	unsigned int log;
555db64fe02SNick Piggin 
556db64fe02SNick Piggin 	log = fls(num_online_cpus());
557db64fe02SNick Piggin 
558db64fe02SNick Piggin 	return log * (32UL * 1024 * 1024 / PAGE_SIZE);
559db64fe02SNick Piggin }
560db64fe02SNick Piggin 
561db64fe02SNick Piggin static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
562db64fe02SNick Piggin 
56302b709dfSNick Piggin /* for per-CPU blocks */
56402b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void);
56502b709dfSNick Piggin 
566db64fe02SNick Piggin /*
5673ee48b6aSCliff Wickman  * called before a call to iounmap() if the caller wants vm_area_struct's
5683ee48b6aSCliff Wickman  * immediately freed.
5693ee48b6aSCliff Wickman  */
5703ee48b6aSCliff Wickman void set_iounmap_nonlazy(void)
5713ee48b6aSCliff Wickman {
5723ee48b6aSCliff Wickman 	atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
5733ee48b6aSCliff Wickman }
5743ee48b6aSCliff Wickman 
5753ee48b6aSCliff Wickman /*
576db64fe02SNick Piggin  * Purges all lazily-freed vmap areas.
577db64fe02SNick Piggin  *
578db64fe02SNick Piggin  * If sync is 0 then don't purge if there is already a purge in progress.
579db64fe02SNick Piggin  * If force_flush is 1, then flush kernel TLBs between *start and *end even
580db64fe02SNick Piggin  * if we found no lazy vmap areas to unmap (callers can use this to optimise
581db64fe02SNick Piggin  * their own TLB flushing).
582db64fe02SNick Piggin  * Returns with *start = min(*start, lowest purged address)
583db64fe02SNick Piggin  *              *end = max(*end, highest purged address)
584db64fe02SNick Piggin  */
585db64fe02SNick Piggin static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
586db64fe02SNick Piggin 					int sync, int force_flush)
587db64fe02SNick Piggin {
58846666d8aSAndrew Morton 	static DEFINE_SPINLOCK(purge_lock);
589db64fe02SNick Piggin 	LIST_HEAD(valist);
590db64fe02SNick Piggin 	struct vmap_area *va;
591cbb76676SVegard Nossum 	struct vmap_area *n_va;
592db64fe02SNick Piggin 	int nr = 0;
593db64fe02SNick Piggin 
594db64fe02SNick Piggin 	/*
595db64fe02SNick Piggin 	 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
596db64fe02SNick Piggin 	 * should not expect such behaviour. This just simplifies locking for
597db64fe02SNick Piggin 	 * the case that isn't actually used at the moment anyway.
598db64fe02SNick Piggin 	 */
599db64fe02SNick Piggin 	if (!sync && !force_flush) {
60046666d8aSAndrew Morton 		if (!spin_trylock(&purge_lock))
601db64fe02SNick Piggin 			return;
602db64fe02SNick Piggin 	} else
60346666d8aSAndrew Morton 		spin_lock(&purge_lock);
604db64fe02SNick Piggin 
60502b709dfSNick Piggin 	if (sync)
60602b709dfSNick Piggin 		purge_fragmented_blocks_allcpus();
60702b709dfSNick Piggin 
608db64fe02SNick Piggin 	rcu_read_lock();
609db64fe02SNick Piggin 	list_for_each_entry_rcu(va, &vmap_area_list, list) {
610db64fe02SNick Piggin 		if (va->flags & VM_LAZY_FREE) {
611db64fe02SNick Piggin 			if (va->va_start < *start)
612db64fe02SNick Piggin 				*start = va->va_start;
613db64fe02SNick Piggin 			if (va->va_end > *end)
614db64fe02SNick Piggin 				*end = va->va_end;
615db64fe02SNick Piggin 			nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
616db64fe02SNick Piggin 			list_add_tail(&va->purge_list, &valist);
617db64fe02SNick Piggin 			va->flags |= VM_LAZY_FREEING;
618db64fe02SNick Piggin 			va->flags &= ~VM_LAZY_FREE;
619db64fe02SNick Piggin 		}
620db64fe02SNick Piggin 	}
621db64fe02SNick Piggin 	rcu_read_unlock();
622db64fe02SNick Piggin 
62388f50044SYongseok Koh 	if (nr)
624db64fe02SNick Piggin 		atomic_sub(nr, &vmap_lazy_nr);
625db64fe02SNick Piggin 
626db64fe02SNick Piggin 	if (nr || force_flush)
627db64fe02SNick Piggin 		flush_tlb_kernel_range(*start, *end);
628db64fe02SNick Piggin 
629db64fe02SNick Piggin 	if (nr) {
630db64fe02SNick Piggin 		spin_lock(&vmap_area_lock);
631cbb76676SVegard Nossum 		list_for_each_entry_safe(va, n_va, &valist, purge_list)
632db64fe02SNick Piggin 			__free_vmap_area(va);
633db64fe02SNick Piggin 		spin_unlock(&vmap_area_lock);
634db64fe02SNick Piggin 	}
63546666d8aSAndrew Morton 	spin_unlock(&purge_lock);
636db64fe02SNick Piggin }
637db64fe02SNick Piggin 
638db64fe02SNick Piggin /*
639496850e5SNick Piggin  * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
640496850e5SNick Piggin  * is already purging.
641496850e5SNick Piggin  */
642496850e5SNick Piggin static void try_purge_vmap_area_lazy(void)
643496850e5SNick Piggin {
644496850e5SNick Piggin 	unsigned long start = ULONG_MAX, end = 0;
645496850e5SNick Piggin 
646496850e5SNick Piggin 	__purge_vmap_area_lazy(&start, &end, 0, 0);
647496850e5SNick Piggin }
648496850e5SNick Piggin 
649496850e5SNick Piggin /*
650db64fe02SNick Piggin  * Kick off a purge of the outstanding lazy areas.
651db64fe02SNick Piggin  */
652db64fe02SNick Piggin static void purge_vmap_area_lazy(void)
653db64fe02SNick Piggin {
654db64fe02SNick Piggin 	unsigned long start = ULONG_MAX, end = 0;
655db64fe02SNick Piggin 
656496850e5SNick Piggin 	__purge_vmap_area_lazy(&start, &end, 1, 0);
657db64fe02SNick Piggin }
658db64fe02SNick Piggin 
659db64fe02SNick Piggin /*
66064141da5SJeremy Fitzhardinge  * Free a vmap area, caller ensuring that the area has been unmapped
66164141da5SJeremy Fitzhardinge  * and flush_cache_vunmap had been called for the correct range
66264141da5SJeremy Fitzhardinge  * previously.
663db64fe02SNick Piggin  */
66464141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va)
665db64fe02SNick Piggin {
666db64fe02SNick Piggin 	va->flags |= VM_LAZY_FREE;
667db64fe02SNick Piggin 	atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
668db64fe02SNick Piggin 	if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
669496850e5SNick Piggin 		try_purge_vmap_area_lazy();
670db64fe02SNick Piggin }
671db64fe02SNick Piggin 
672b29acbdcSNick Piggin /*
67364141da5SJeremy Fitzhardinge  * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
67464141da5SJeremy Fitzhardinge  * called for the correct range previously.
67564141da5SJeremy Fitzhardinge  */
67664141da5SJeremy Fitzhardinge static void free_unmap_vmap_area_noflush(struct vmap_area *va)
67764141da5SJeremy Fitzhardinge {
67864141da5SJeremy Fitzhardinge 	unmap_vmap_area(va);
67964141da5SJeremy Fitzhardinge 	free_vmap_area_noflush(va);
68064141da5SJeremy Fitzhardinge }
68164141da5SJeremy Fitzhardinge 
68264141da5SJeremy Fitzhardinge /*
683b29acbdcSNick Piggin  * Free and unmap a vmap area
684b29acbdcSNick Piggin  */
685b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va)
686b29acbdcSNick Piggin {
687b29acbdcSNick Piggin 	flush_cache_vunmap(va->va_start, va->va_end);
688b29acbdcSNick Piggin 	free_unmap_vmap_area_noflush(va);
689b29acbdcSNick Piggin }
690b29acbdcSNick Piggin 
691db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr)
692db64fe02SNick Piggin {
693db64fe02SNick Piggin 	struct vmap_area *va;
694db64fe02SNick Piggin 
695db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
696db64fe02SNick Piggin 	va = __find_vmap_area(addr);
697db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
698db64fe02SNick Piggin 
699db64fe02SNick Piggin 	return va;
700db64fe02SNick Piggin }
701db64fe02SNick Piggin 
702db64fe02SNick Piggin static void free_unmap_vmap_area_addr(unsigned long addr)
703db64fe02SNick Piggin {
704db64fe02SNick Piggin 	struct vmap_area *va;
705db64fe02SNick Piggin 
706db64fe02SNick Piggin 	va = find_vmap_area(addr);
707db64fe02SNick Piggin 	BUG_ON(!va);
708db64fe02SNick Piggin 	free_unmap_vmap_area(va);
709db64fe02SNick Piggin }
710db64fe02SNick Piggin 
711db64fe02SNick Piggin 
712db64fe02SNick Piggin /*** Per cpu kva allocator ***/
713db64fe02SNick Piggin 
714db64fe02SNick Piggin /*
715db64fe02SNick Piggin  * vmap space is limited especially on 32 bit architectures. Ensure there is
716db64fe02SNick Piggin  * room for at least 16 percpu vmap blocks per CPU.
717db64fe02SNick Piggin  */
718db64fe02SNick Piggin /*
719db64fe02SNick Piggin  * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
720db64fe02SNick Piggin  * to #define VMALLOC_SPACE		(VMALLOC_END-VMALLOC_START). Guess
721db64fe02SNick Piggin  * instead (we just need a rough idea)
722db64fe02SNick Piggin  */
723db64fe02SNick Piggin #if BITS_PER_LONG == 32
724db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024)
725db64fe02SNick Piggin #else
726db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024*1024)
727db64fe02SNick Piggin #endif
728db64fe02SNick Piggin 
729db64fe02SNick Piggin #define VMALLOC_PAGES		(VMALLOC_SPACE / PAGE_SIZE)
730db64fe02SNick Piggin #define VMAP_MAX_ALLOC		BITS_PER_LONG	/* 256K with 4K pages */
731db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX	1024	/* 4MB with 4K pages */
732db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN	(VMAP_MAX_ALLOC*2)
733db64fe02SNick Piggin #define VMAP_MIN(x, y)		((x) < (y) ? (x) : (y)) /* can't use min() */
734db64fe02SNick Piggin #define VMAP_MAX(x, y)		((x) > (y) ? (x) : (y)) /* can't use max() */
735db64fe02SNick Piggin #define VMAP_BBMAP_BITS		VMAP_MIN(VMAP_BBMAP_BITS_MAX,		\
736db64fe02SNick Piggin 					VMAP_MAX(VMAP_BBMAP_BITS_MIN,	\
737db64fe02SNick Piggin 						VMALLOC_PAGES / NR_CPUS / 16))
738db64fe02SNick Piggin 
739db64fe02SNick Piggin #define VMAP_BLOCK_SIZE		(VMAP_BBMAP_BITS * PAGE_SIZE)
740db64fe02SNick Piggin 
7419b463334SJeremy Fitzhardinge static bool vmap_initialized __read_mostly = false;
7429b463334SJeremy Fitzhardinge 
743db64fe02SNick Piggin struct vmap_block_queue {
744db64fe02SNick Piggin 	spinlock_t lock;
745db64fe02SNick Piggin 	struct list_head free;
746db64fe02SNick Piggin };
747db64fe02SNick Piggin 
748db64fe02SNick Piggin struct vmap_block {
749db64fe02SNick Piggin 	spinlock_t lock;
750db64fe02SNick Piggin 	struct vmap_area *va;
751db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
752db64fe02SNick Piggin 	unsigned long free, dirty;
753db64fe02SNick Piggin 	DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
754db64fe02SNick Piggin 	DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
755db64fe02SNick Piggin 	struct list_head free_list;
756db64fe02SNick Piggin 	struct rcu_head rcu_head;
75702b709dfSNick Piggin 	struct list_head purge;
758db64fe02SNick Piggin };
759db64fe02SNick Piggin 
760db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
761db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
762db64fe02SNick Piggin 
763db64fe02SNick Piggin /*
764db64fe02SNick Piggin  * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
765db64fe02SNick Piggin  * in the free path. Could get rid of this if we change the API to return a
766db64fe02SNick Piggin  * "cookie" from alloc, to be passed to free. But no big deal yet.
767db64fe02SNick Piggin  */
768db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_block_tree_lock);
769db64fe02SNick Piggin static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
770db64fe02SNick Piggin 
771db64fe02SNick Piggin /*
772db64fe02SNick Piggin  * We should probably have a fallback mechanism to allocate virtual memory
773db64fe02SNick Piggin  * out of partially filled vmap blocks. However vmap block sizing should be
774db64fe02SNick Piggin  * fairly reasonable according to the vmalloc size, so it shouldn't be a
775db64fe02SNick Piggin  * big problem.
776db64fe02SNick Piggin  */
777db64fe02SNick Piggin 
778db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr)
779db64fe02SNick Piggin {
780db64fe02SNick Piggin 	addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
781db64fe02SNick Piggin 	addr /= VMAP_BLOCK_SIZE;
782db64fe02SNick Piggin 	return addr;
783db64fe02SNick Piggin }
784db64fe02SNick Piggin 
785db64fe02SNick Piggin static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
786db64fe02SNick Piggin {
787db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
788db64fe02SNick Piggin 	struct vmap_block *vb;
789db64fe02SNick Piggin 	struct vmap_area *va;
790db64fe02SNick Piggin 	unsigned long vb_idx;
791db64fe02SNick Piggin 	int node, err;
792db64fe02SNick Piggin 
793db64fe02SNick Piggin 	node = numa_node_id();
794db64fe02SNick Piggin 
795db64fe02SNick Piggin 	vb = kmalloc_node(sizeof(struct vmap_block),
796db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
797db64fe02SNick Piggin 	if (unlikely(!vb))
798db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
799db64fe02SNick Piggin 
800db64fe02SNick Piggin 	va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
801db64fe02SNick Piggin 					VMALLOC_START, VMALLOC_END,
802db64fe02SNick Piggin 					node, gfp_mask);
803ddf9c6d4STobias Klauser 	if (IS_ERR(va)) {
804db64fe02SNick Piggin 		kfree(vb);
805e7d86340SJulia Lawall 		return ERR_CAST(va);
806db64fe02SNick Piggin 	}
807db64fe02SNick Piggin 
808db64fe02SNick Piggin 	err = radix_tree_preload(gfp_mask);
809db64fe02SNick Piggin 	if (unlikely(err)) {
810db64fe02SNick Piggin 		kfree(vb);
811db64fe02SNick Piggin 		free_vmap_area(va);
812db64fe02SNick Piggin 		return ERR_PTR(err);
813db64fe02SNick Piggin 	}
814db64fe02SNick Piggin 
815db64fe02SNick Piggin 	spin_lock_init(&vb->lock);
816db64fe02SNick Piggin 	vb->va = va;
817db64fe02SNick Piggin 	vb->free = VMAP_BBMAP_BITS;
818db64fe02SNick Piggin 	vb->dirty = 0;
819db64fe02SNick Piggin 	bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
820db64fe02SNick Piggin 	bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
821db64fe02SNick Piggin 	INIT_LIST_HEAD(&vb->free_list);
822db64fe02SNick Piggin 
823db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(va->va_start);
824db64fe02SNick Piggin 	spin_lock(&vmap_block_tree_lock);
825db64fe02SNick Piggin 	err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
826db64fe02SNick Piggin 	spin_unlock(&vmap_block_tree_lock);
827db64fe02SNick Piggin 	BUG_ON(err);
828db64fe02SNick Piggin 	radix_tree_preload_end();
829db64fe02SNick Piggin 
830db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
831db64fe02SNick Piggin 	vb->vbq = vbq;
832db64fe02SNick Piggin 	spin_lock(&vbq->lock);
833de560423SNick Piggin 	list_add_rcu(&vb->free_list, &vbq->free);
834db64fe02SNick Piggin 	spin_unlock(&vbq->lock);
8353f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
836db64fe02SNick Piggin 
837db64fe02SNick Piggin 	return vb;
838db64fe02SNick Piggin }
839db64fe02SNick Piggin 
840db64fe02SNick Piggin static void rcu_free_vb(struct rcu_head *head)
841db64fe02SNick Piggin {
842db64fe02SNick Piggin 	struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
843db64fe02SNick Piggin 
844db64fe02SNick Piggin 	kfree(vb);
845db64fe02SNick Piggin }
846db64fe02SNick Piggin 
847db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb)
848db64fe02SNick Piggin {
849db64fe02SNick Piggin 	struct vmap_block *tmp;
850db64fe02SNick Piggin 	unsigned long vb_idx;
851db64fe02SNick Piggin 
852db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(vb->va->va_start);
853db64fe02SNick Piggin 	spin_lock(&vmap_block_tree_lock);
854db64fe02SNick Piggin 	tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
855db64fe02SNick Piggin 	spin_unlock(&vmap_block_tree_lock);
856db64fe02SNick Piggin 	BUG_ON(tmp != vb);
857db64fe02SNick Piggin 
85864141da5SJeremy Fitzhardinge 	free_vmap_area_noflush(vb->va);
859db64fe02SNick Piggin 	call_rcu(&vb->rcu_head, rcu_free_vb);
860db64fe02SNick Piggin }
861db64fe02SNick Piggin 
86202b709dfSNick Piggin static void purge_fragmented_blocks(int cpu)
86302b709dfSNick Piggin {
86402b709dfSNick Piggin 	LIST_HEAD(purge);
86502b709dfSNick Piggin 	struct vmap_block *vb;
86602b709dfSNick Piggin 	struct vmap_block *n_vb;
86702b709dfSNick Piggin 	struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
86802b709dfSNick Piggin 
86902b709dfSNick Piggin 	rcu_read_lock();
87002b709dfSNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
87102b709dfSNick Piggin 
87202b709dfSNick Piggin 		if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
87302b709dfSNick Piggin 			continue;
87402b709dfSNick Piggin 
87502b709dfSNick Piggin 		spin_lock(&vb->lock);
87602b709dfSNick Piggin 		if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
87702b709dfSNick Piggin 			vb->free = 0; /* prevent further allocs after releasing lock */
87802b709dfSNick Piggin 			vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
87902b709dfSNick Piggin 			bitmap_fill(vb->alloc_map, VMAP_BBMAP_BITS);
88002b709dfSNick Piggin 			bitmap_fill(vb->dirty_map, VMAP_BBMAP_BITS);
88102b709dfSNick Piggin 			spin_lock(&vbq->lock);
88202b709dfSNick Piggin 			list_del_rcu(&vb->free_list);
88302b709dfSNick Piggin 			spin_unlock(&vbq->lock);
88402b709dfSNick Piggin 			spin_unlock(&vb->lock);
88502b709dfSNick Piggin 			list_add_tail(&vb->purge, &purge);
88602b709dfSNick Piggin 		} else
88702b709dfSNick Piggin 			spin_unlock(&vb->lock);
88802b709dfSNick Piggin 	}
88902b709dfSNick Piggin 	rcu_read_unlock();
89002b709dfSNick Piggin 
89102b709dfSNick Piggin 	list_for_each_entry_safe(vb, n_vb, &purge, purge) {
89202b709dfSNick Piggin 		list_del(&vb->purge);
89302b709dfSNick Piggin 		free_vmap_block(vb);
89402b709dfSNick Piggin 	}
89502b709dfSNick Piggin }
89602b709dfSNick Piggin 
89702b709dfSNick Piggin static void purge_fragmented_blocks_thiscpu(void)
89802b709dfSNick Piggin {
89902b709dfSNick Piggin 	purge_fragmented_blocks(smp_processor_id());
90002b709dfSNick Piggin }
90102b709dfSNick Piggin 
90202b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void)
90302b709dfSNick Piggin {
90402b709dfSNick Piggin 	int cpu;
90502b709dfSNick Piggin 
90602b709dfSNick Piggin 	for_each_possible_cpu(cpu)
90702b709dfSNick Piggin 		purge_fragmented_blocks(cpu);
90802b709dfSNick Piggin }
90902b709dfSNick Piggin 
910db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
911db64fe02SNick Piggin {
912db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
913db64fe02SNick Piggin 	struct vmap_block *vb;
914db64fe02SNick Piggin 	unsigned long addr = 0;
915db64fe02SNick Piggin 	unsigned int order;
91602b709dfSNick Piggin 	int purge = 0;
917db64fe02SNick Piggin 
918db64fe02SNick Piggin 	BUG_ON(size & ~PAGE_MASK);
919db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
920db64fe02SNick Piggin 	order = get_order(size);
921db64fe02SNick Piggin 
922db64fe02SNick Piggin again:
923db64fe02SNick Piggin 	rcu_read_lock();
924db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
925db64fe02SNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
926db64fe02SNick Piggin 		int i;
927db64fe02SNick Piggin 
928db64fe02SNick Piggin 		spin_lock(&vb->lock);
92902b709dfSNick Piggin 		if (vb->free < 1UL << order)
93002b709dfSNick Piggin 			goto next;
93102b709dfSNick Piggin 
932db64fe02SNick Piggin 		i = bitmap_find_free_region(vb->alloc_map,
933db64fe02SNick Piggin 						VMAP_BBMAP_BITS, order);
934db64fe02SNick Piggin 
93502b709dfSNick Piggin 		if (i < 0) {
93602b709dfSNick Piggin 			if (vb->free + vb->dirty == VMAP_BBMAP_BITS) {
93702b709dfSNick Piggin 				/* fragmented and no outstanding allocations */
93802b709dfSNick Piggin 				BUG_ON(vb->dirty != VMAP_BBMAP_BITS);
93902b709dfSNick Piggin 				purge = 1;
94002b709dfSNick Piggin 			}
94102b709dfSNick Piggin 			goto next;
94202b709dfSNick Piggin 		}
943db64fe02SNick Piggin 		addr = vb->va->va_start + (i << PAGE_SHIFT);
944db64fe02SNick Piggin 		BUG_ON(addr_to_vb_idx(addr) !=
945db64fe02SNick Piggin 				addr_to_vb_idx(vb->va->va_start));
946db64fe02SNick Piggin 		vb->free -= 1UL << order;
947db64fe02SNick Piggin 		if (vb->free == 0) {
948db64fe02SNick Piggin 			spin_lock(&vbq->lock);
949de560423SNick Piggin 			list_del_rcu(&vb->free_list);
950db64fe02SNick Piggin 			spin_unlock(&vbq->lock);
951db64fe02SNick Piggin 		}
952db64fe02SNick Piggin 		spin_unlock(&vb->lock);
953db64fe02SNick Piggin 		break;
95402b709dfSNick Piggin next:
955db64fe02SNick Piggin 		spin_unlock(&vb->lock);
956db64fe02SNick Piggin 	}
95702b709dfSNick Piggin 
95802b709dfSNick Piggin 	if (purge)
95902b709dfSNick Piggin 		purge_fragmented_blocks_thiscpu();
96002b709dfSNick Piggin 
9613f04ba85STejun Heo 	put_cpu_var(vmap_block_queue);
962db64fe02SNick Piggin 	rcu_read_unlock();
963db64fe02SNick Piggin 
964db64fe02SNick Piggin 	if (!addr) {
965db64fe02SNick Piggin 		vb = new_vmap_block(gfp_mask);
966db64fe02SNick Piggin 		if (IS_ERR(vb))
967db64fe02SNick Piggin 			return vb;
968db64fe02SNick Piggin 		goto again;
969db64fe02SNick Piggin 	}
970db64fe02SNick Piggin 
971db64fe02SNick Piggin 	return (void *)addr;
972db64fe02SNick Piggin }
973db64fe02SNick Piggin 
974db64fe02SNick Piggin static void vb_free(const void *addr, unsigned long size)
975db64fe02SNick Piggin {
976db64fe02SNick Piggin 	unsigned long offset;
977db64fe02SNick Piggin 	unsigned long vb_idx;
978db64fe02SNick Piggin 	unsigned int order;
979db64fe02SNick Piggin 	struct vmap_block *vb;
980db64fe02SNick Piggin 
981db64fe02SNick Piggin 	BUG_ON(size & ~PAGE_MASK);
982db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
983b29acbdcSNick Piggin 
984b29acbdcSNick Piggin 	flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
985b29acbdcSNick Piggin 
986db64fe02SNick Piggin 	order = get_order(size);
987db64fe02SNick Piggin 
988db64fe02SNick Piggin 	offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
989db64fe02SNick Piggin 
990db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx((unsigned long)addr);
991db64fe02SNick Piggin 	rcu_read_lock();
992db64fe02SNick Piggin 	vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
993db64fe02SNick Piggin 	rcu_read_unlock();
994db64fe02SNick Piggin 	BUG_ON(!vb);
995db64fe02SNick Piggin 
99664141da5SJeremy Fitzhardinge 	vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
99764141da5SJeremy Fitzhardinge 
998db64fe02SNick Piggin 	spin_lock(&vb->lock);
999de560423SNick Piggin 	BUG_ON(bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order));
1000d086817dSMinChan Kim 
1001db64fe02SNick Piggin 	vb->dirty += 1UL << order;
1002db64fe02SNick Piggin 	if (vb->dirty == VMAP_BBMAP_BITS) {
1003de560423SNick Piggin 		BUG_ON(vb->free);
1004db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1005db64fe02SNick Piggin 		free_vmap_block(vb);
1006db64fe02SNick Piggin 	} else
1007db64fe02SNick Piggin 		spin_unlock(&vb->lock);
1008db64fe02SNick Piggin }
1009db64fe02SNick Piggin 
1010db64fe02SNick Piggin /**
1011db64fe02SNick Piggin  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1012db64fe02SNick Piggin  *
1013db64fe02SNick Piggin  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1014db64fe02SNick Piggin  * to amortize TLB flushing overheads. What this means is that any page you
1015db64fe02SNick Piggin  * have now, may, in a former life, have been mapped into kernel virtual
1016db64fe02SNick Piggin  * address by the vmap layer and so there might be some CPUs with TLB entries
1017db64fe02SNick Piggin  * still referencing that page (additional to the regular 1:1 kernel mapping).
1018db64fe02SNick Piggin  *
1019db64fe02SNick Piggin  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1020db64fe02SNick Piggin  * be sure that none of the pages we have control over will have any aliases
1021db64fe02SNick Piggin  * from the vmap layer.
1022db64fe02SNick Piggin  */
1023db64fe02SNick Piggin void vm_unmap_aliases(void)
1024db64fe02SNick Piggin {
1025db64fe02SNick Piggin 	unsigned long start = ULONG_MAX, end = 0;
1026db64fe02SNick Piggin 	int cpu;
1027db64fe02SNick Piggin 	int flush = 0;
1028db64fe02SNick Piggin 
10299b463334SJeremy Fitzhardinge 	if (unlikely(!vmap_initialized))
10309b463334SJeremy Fitzhardinge 		return;
10319b463334SJeremy Fitzhardinge 
1032db64fe02SNick Piggin 	for_each_possible_cpu(cpu) {
1033db64fe02SNick Piggin 		struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1034db64fe02SNick Piggin 		struct vmap_block *vb;
1035db64fe02SNick Piggin 
1036db64fe02SNick Piggin 		rcu_read_lock();
1037db64fe02SNick Piggin 		list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1038db64fe02SNick Piggin 			int i;
1039db64fe02SNick Piggin 
1040db64fe02SNick Piggin 			spin_lock(&vb->lock);
1041db64fe02SNick Piggin 			i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
1042db64fe02SNick Piggin 			while (i < VMAP_BBMAP_BITS) {
1043db64fe02SNick Piggin 				unsigned long s, e;
1044db64fe02SNick Piggin 				int j;
1045db64fe02SNick Piggin 				j = find_next_zero_bit(vb->dirty_map,
1046db64fe02SNick Piggin 					VMAP_BBMAP_BITS, i);
1047db64fe02SNick Piggin 
1048db64fe02SNick Piggin 				s = vb->va->va_start + (i << PAGE_SHIFT);
1049db64fe02SNick Piggin 				e = vb->va->va_start + (j << PAGE_SHIFT);
1050db64fe02SNick Piggin 				flush = 1;
1051db64fe02SNick Piggin 
1052db64fe02SNick Piggin 				if (s < start)
1053db64fe02SNick Piggin 					start = s;
1054db64fe02SNick Piggin 				if (e > end)
1055db64fe02SNick Piggin 					end = e;
1056db64fe02SNick Piggin 
1057db64fe02SNick Piggin 				i = j;
1058db64fe02SNick Piggin 				i = find_next_bit(vb->dirty_map,
1059db64fe02SNick Piggin 							VMAP_BBMAP_BITS, i);
1060db64fe02SNick Piggin 			}
1061db64fe02SNick Piggin 			spin_unlock(&vb->lock);
1062db64fe02SNick Piggin 		}
1063db64fe02SNick Piggin 		rcu_read_unlock();
1064db64fe02SNick Piggin 	}
1065db64fe02SNick Piggin 
1066db64fe02SNick Piggin 	__purge_vmap_area_lazy(&start, &end, 1, flush);
1067db64fe02SNick Piggin }
1068db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1069db64fe02SNick Piggin 
1070db64fe02SNick Piggin /**
1071db64fe02SNick Piggin  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1072db64fe02SNick Piggin  * @mem: the pointer returned by vm_map_ram
1073db64fe02SNick Piggin  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1074db64fe02SNick Piggin  */
1075db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count)
1076db64fe02SNick Piggin {
1077db64fe02SNick Piggin 	unsigned long size = count << PAGE_SHIFT;
1078db64fe02SNick Piggin 	unsigned long addr = (unsigned long)mem;
1079db64fe02SNick Piggin 
1080db64fe02SNick Piggin 	BUG_ON(!addr);
1081db64fe02SNick Piggin 	BUG_ON(addr < VMALLOC_START);
1082db64fe02SNick Piggin 	BUG_ON(addr > VMALLOC_END);
1083db64fe02SNick Piggin 	BUG_ON(addr & (PAGE_SIZE-1));
1084db64fe02SNick Piggin 
1085db64fe02SNick Piggin 	debug_check_no_locks_freed(mem, size);
1086cd52858cSNick Piggin 	vmap_debug_free_range(addr, addr+size);
1087db64fe02SNick Piggin 
1088db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC))
1089db64fe02SNick Piggin 		vb_free(mem, size);
1090db64fe02SNick Piggin 	else
1091db64fe02SNick Piggin 		free_unmap_vmap_area_addr(addr);
1092db64fe02SNick Piggin }
1093db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram);
1094db64fe02SNick Piggin 
1095db64fe02SNick Piggin /**
1096db64fe02SNick Piggin  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1097db64fe02SNick Piggin  * @pages: an array of pointers to the pages to be mapped
1098db64fe02SNick Piggin  * @count: number of pages
1099db64fe02SNick Piggin  * @node: prefer to allocate data structures on this node
1100db64fe02SNick Piggin  * @prot: memory protection to use. PAGE_KERNEL for regular RAM
1101e99c97adSRandy Dunlap  *
1102e99c97adSRandy Dunlap  * Returns: a pointer to the address that has been mapped, or %NULL on failure
1103db64fe02SNick Piggin  */
1104db64fe02SNick Piggin void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1105db64fe02SNick Piggin {
1106db64fe02SNick Piggin 	unsigned long size = count << PAGE_SHIFT;
1107db64fe02SNick Piggin 	unsigned long addr;
1108db64fe02SNick Piggin 	void *mem;
1109db64fe02SNick Piggin 
1110db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC)) {
1111db64fe02SNick Piggin 		mem = vb_alloc(size, GFP_KERNEL);
1112db64fe02SNick Piggin 		if (IS_ERR(mem))
1113db64fe02SNick Piggin 			return NULL;
1114db64fe02SNick Piggin 		addr = (unsigned long)mem;
1115db64fe02SNick Piggin 	} else {
1116db64fe02SNick Piggin 		struct vmap_area *va;
1117db64fe02SNick Piggin 		va = alloc_vmap_area(size, PAGE_SIZE,
1118db64fe02SNick Piggin 				VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1119db64fe02SNick Piggin 		if (IS_ERR(va))
1120db64fe02SNick Piggin 			return NULL;
1121db64fe02SNick Piggin 
1122db64fe02SNick Piggin 		addr = va->va_start;
1123db64fe02SNick Piggin 		mem = (void *)addr;
1124db64fe02SNick Piggin 	}
1125db64fe02SNick Piggin 	if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1126db64fe02SNick Piggin 		vm_unmap_ram(mem, count);
1127db64fe02SNick Piggin 		return NULL;
1128db64fe02SNick Piggin 	}
1129db64fe02SNick Piggin 	return mem;
1130db64fe02SNick Piggin }
1131db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram);
1132db64fe02SNick Piggin 
1133f0aa6617STejun Heo /**
1134f0aa6617STejun Heo  * vm_area_register_early - register vmap area early during boot
1135f0aa6617STejun Heo  * @vm: vm_struct to register
1136c0c0a293STejun Heo  * @align: requested alignment
1137f0aa6617STejun Heo  *
1138f0aa6617STejun Heo  * This function is used to register kernel vm area before
1139f0aa6617STejun Heo  * vmalloc_init() is called.  @vm->size and @vm->flags should contain
1140f0aa6617STejun Heo  * proper values on entry and other fields should be zero.  On return,
1141f0aa6617STejun Heo  * vm->addr contains the allocated address.
1142f0aa6617STejun Heo  *
1143f0aa6617STejun Heo  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1144f0aa6617STejun Heo  */
1145c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1146f0aa6617STejun Heo {
1147f0aa6617STejun Heo 	static size_t vm_init_off __initdata;
1148c0c0a293STejun Heo 	unsigned long addr;
1149f0aa6617STejun Heo 
1150c0c0a293STejun Heo 	addr = ALIGN(VMALLOC_START + vm_init_off, align);
1151c0c0a293STejun Heo 	vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1152c0c0a293STejun Heo 
1153c0c0a293STejun Heo 	vm->addr = (void *)addr;
1154f0aa6617STejun Heo 
1155f0aa6617STejun Heo 	vm->next = vmlist;
1156f0aa6617STejun Heo 	vmlist = vm;
1157f0aa6617STejun Heo }
1158f0aa6617STejun Heo 
1159db64fe02SNick Piggin void __init vmalloc_init(void)
1160db64fe02SNick Piggin {
1161822c18f2SIvan Kokshaysky 	struct vmap_area *va;
1162822c18f2SIvan Kokshaysky 	struct vm_struct *tmp;
1163db64fe02SNick Piggin 	int i;
1164db64fe02SNick Piggin 
1165db64fe02SNick Piggin 	for_each_possible_cpu(i) {
1166db64fe02SNick Piggin 		struct vmap_block_queue *vbq;
1167db64fe02SNick Piggin 
1168db64fe02SNick Piggin 		vbq = &per_cpu(vmap_block_queue, i);
1169db64fe02SNick Piggin 		spin_lock_init(&vbq->lock);
1170db64fe02SNick Piggin 		INIT_LIST_HEAD(&vbq->free);
1171db64fe02SNick Piggin 	}
11729b463334SJeremy Fitzhardinge 
1173822c18f2SIvan Kokshaysky 	/* Import existing vmlist entries. */
1174822c18f2SIvan Kokshaysky 	for (tmp = vmlist; tmp; tmp = tmp->next) {
117543ebdac4SPekka Enberg 		va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
1176822c18f2SIvan Kokshaysky 		va->flags = tmp->flags | VM_VM_AREA;
1177822c18f2SIvan Kokshaysky 		va->va_start = (unsigned long)tmp->addr;
1178822c18f2SIvan Kokshaysky 		va->va_end = va->va_start + tmp->size;
1179822c18f2SIvan Kokshaysky 		__insert_vmap_area(va);
1180822c18f2SIvan Kokshaysky 	}
1181ca23e405STejun Heo 
1182ca23e405STejun Heo 	vmap_area_pcpu_hole = VMALLOC_END;
1183ca23e405STejun Heo 
11849b463334SJeremy Fitzhardinge 	vmap_initialized = true;
1185db64fe02SNick Piggin }
1186db64fe02SNick Piggin 
11878fc48985STejun Heo /**
11888fc48985STejun Heo  * map_kernel_range_noflush - map kernel VM area with the specified pages
11898fc48985STejun Heo  * @addr: start of the VM area to map
11908fc48985STejun Heo  * @size: size of the VM area to map
11918fc48985STejun Heo  * @prot: page protection flags to use
11928fc48985STejun Heo  * @pages: pages to map
11938fc48985STejun Heo  *
11948fc48985STejun Heo  * Map PFN_UP(@size) pages at @addr.  The VM area @addr and @size
11958fc48985STejun Heo  * specify should have been allocated using get_vm_area() and its
11968fc48985STejun Heo  * friends.
11978fc48985STejun Heo  *
11988fc48985STejun Heo  * NOTE:
11998fc48985STejun Heo  * This function does NOT do any cache flushing.  The caller is
12008fc48985STejun Heo  * responsible for calling flush_cache_vmap() on to-be-mapped areas
12018fc48985STejun Heo  * before calling this function.
12028fc48985STejun Heo  *
12038fc48985STejun Heo  * RETURNS:
12048fc48985STejun Heo  * The number of pages mapped on success, -errno on failure.
12058fc48985STejun Heo  */
12068fc48985STejun Heo int map_kernel_range_noflush(unsigned long addr, unsigned long size,
12078fc48985STejun Heo 			     pgprot_t prot, struct page **pages)
12088fc48985STejun Heo {
12098fc48985STejun Heo 	return vmap_page_range_noflush(addr, addr + size, prot, pages);
12108fc48985STejun Heo }
12118fc48985STejun Heo 
12128fc48985STejun Heo /**
12138fc48985STejun Heo  * unmap_kernel_range_noflush - unmap kernel VM area
12148fc48985STejun Heo  * @addr: start of the VM area to unmap
12158fc48985STejun Heo  * @size: size of the VM area to unmap
12168fc48985STejun Heo  *
12178fc48985STejun Heo  * Unmap PFN_UP(@size) pages at @addr.  The VM area @addr and @size
12188fc48985STejun Heo  * specify should have been allocated using get_vm_area() and its
12198fc48985STejun Heo  * friends.
12208fc48985STejun Heo  *
12218fc48985STejun Heo  * NOTE:
12228fc48985STejun Heo  * This function does NOT do any cache flushing.  The caller is
12238fc48985STejun Heo  * responsible for calling flush_cache_vunmap() on to-be-mapped areas
12248fc48985STejun Heo  * before calling this function and flush_tlb_kernel_range() after.
12258fc48985STejun Heo  */
12268fc48985STejun Heo void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
12278fc48985STejun Heo {
12288fc48985STejun Heo 	vunmap_page_range(addr, addr + size);
12298fc48985STejun Heo }
123081e88fdcSHuang Ying EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
12318fc48985STejun Heo 
12328fc48985STejun Heo /**
12338fc48985STejun Heo  * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
12348fc48985STejun Heo  * @addr: start of the VM area to unmap
12358fc48985STejun Heo  * @size: size of the VM area to unmap
12368fc48985STejun Heo  *
12378fc48985STejun Heo  * Similar to unmap_kernel_range_noflush() but flushes vcache before
12388fc48985STejun Heo  * the unmapping and tlb after.
12398fc48985STejun Heo  */
1240db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size)
1241db64fe02SNick Piggin {
1242db64fe02SNick Piggin 	unsigned long end = addr + size;
1243f6fcba70STejun Heo 
1244f6fcba70STejun Heo 	flush_cache_vunmap(addr, end);
1245db64fe02SNick Piggin 	vunmap_page_range(addr, end);
1246db64fe02SNick Piggin 	flush_tlb_kernel_range(addr, end);
1247db64fe02SNick Piggin }
1248db64fe02SNick Piggin 
1249db64fe02SNick Piggin int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
1250db64fe02SNick Piggin {
1251db64fe02SNick Piggin 	unsigned long addr = (unsigned long)area->addr;
1252db64fe02SNick Piggin 	unsigned long end = addr + area->size - PAGE_SIZE;
1253db64fe02SNick Piggin 	int err;
1254db64fe02SNick Piggin 
1255db64fe02SNick Piggin 	err = vmap_page_range(addr, end, prot, *pages);
1256db64fe02SNick Piggin 	if (err > 0) {
1257db64fe02SNick Piggin 		*pages += err;
1258db64fe02SNick Piggin 		err = 0;
1259db64fe02SNick Piggin 	}
1260db64fe02SNick Piggin 
1261db64fe02SNick Piggin 	return err;
1262db64fe02SNick Piggin }
1263db64fe02SNick Piggin EXPORT_SYMBOL_GPL(map_vm_area);
1264db64fe02SNick Piggin 
1265db64fe02SNick Piggin /*** Old vmalloc interfaces ***/
1266db64fe02SNick Piggin DEFINE_RWLOCK(vmlist_lock);
1267db64fe02SNick Piggin struct vm_struct *vmlist;
1268db64fe02SNick Piggin 
1269cf88c790STejun Heo static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
1270cf88c790STejun Heo 			      unsigned long flags, void *caller)
1271cf88c790STejun Heo {
1272cf88c790STejun Heo 	struct vm_struct *tmp, **p;
1273cf88c790STejun Heo 
1274cf88c790STejun Heo 	vm->flags = flags;
1275cf88c790STejun Heo 	vm->addr = (void *)va->va_start;
1276cf88c790STejun Heo 	vm->size = va->va_end - va->va_start;
1277cf88c790STejun Heo 	vm->caller = caller;
1278cf88c790STejun Heo 	va->private = vm;
1279cf88c790STejun Heo 	va->flags |= VM_VM_AREA;
1280cf88c790STejun Heo 
1281cf88c790STejun Heo 	write_lock(&vmlist_lock);
1282cf88c790STejun Heo 	for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1283cf88c790STejun Heo 		if (tmp->addr >= vm->addr)
1284cf88c790STejun Heo 			break;
1285cf88c790STejun Heo 	}
1286cf88c790STejun Heo 	vm->next = *p;
1287cf88c790STejun Heo 	*p = vm;
1288cf88c790STejun Heo 	write_unlock(&vmlist_lock);
1289cf88c790STejun Heo }
1290cf88c790STejun Heo 
1291db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size,
12922dca6999SDavid Miller 		unsigned long align, unsigned long flags, unsigned long start,
12932dca6999SDavid Miller 		unsigned long end, int node, gfp_t gfp_mask, void *caller)
1294db64fe02SNick Piggin {
1295db64fe02SNick Piggin 	static struct vmap_area *va;
1296db64fe02SNick Piggin 	struct vm_struct *area;
12971da177e4SLinus Torvalds 
129852fd24caSGiridhar Pemmasani 	BUG_ON(in_interrupt());
12991da177e4SLinus Torvalds 	if (flags & VM_IOREMAP) {
13001da177e4SLinus Torvalds 		int bit = fls(size);
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 		if (bit > IOREMAP_MAX_ORDER)
13031da177e4SLinus Torvalds 			bit = IOREMAP_MAX_ORDER;
13041da177e4SLinus Torvalds 		else if (bit < PAGE_SHIFT)
13051da177e4SLinus Torvalds 			bit = PAGE_SHIFT;
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 		align = 1ul << bit;
13081da177e4SLinus Torvalds 	}
1309db64fe02SNick Piggin 
13101da177e4SLinus Torvalds 	size = PAGE_ALIGN(size);
131131be8309SOGAWA Hirofumi 	if (unlikely(!size))
131231be8309SOGAWA Hirofumi 		return NULL;
13131da177e4SLinus Torvalds 
1314cf88c790STejun Heo 	area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
13151da177e4SLinus Torvalds 	if (unlikely(!area))
13161da177e4SLinus Torvalds 		return NULL;
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	/*
13191da177e4SLinus Torvalds 	 * We always allocate a guard page.
13201da177e4SLinus Torvalds 	 */
13211da177e4SLinus Torvalds 	size += PAGE_SIZE;
13221da177e4SLinus Torvalds 
1323db64fe02SNick Piggin 	va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1324db64fe02SNick Piggin 	if (IS_ERR(va)) {
1325db64fe02SNick Piggin 		kfree(area);
1326db64fe02SNick Piggin 		return NULL;
13271da177e4SLinus Torvalds 	}
13281da177e4SLinus Torvalds 
1329cf88c790STejun Heo 	insert_vmalloc_vm(area, va, flags, caller);
13301da177e4SLinus Torvalds 	return area;
13311da177e4SLinus Torvalds }
13321da177e4SLinus Torvalds 
1333930fc45aSChristoph Lameter struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1334930fc45aSChristoph Lameter 				unsigned long start, unsigned long end)
1335930fc45aSChristoph Lameter {
13362dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, start, end, -1, GFP_KERNEL,
133723016969SChristoph Lameter 						__builtin_return_address(0));
1338930fc45aSChristoph Lameter }
13395992b6daSRusty Russell EXPORT_SYMBOL_GPL(__get_vm_area);
1340930fc45aSChristoph Lameter 
1341c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1342c2968612SBenjamin Herrenschmidt 				       unsigned long start, unsigned long end,
1343c2968612SBenjamin Herrenschmidt 				       void *caller)
1344c2968612SBenjamin Herrenschmidt {
13452dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, start, end, -1, GFP_KERNEL,
1346c2968612SBenjamin Herrenschmidt 				  caller);
1347c2968612SBenjamin Herrenschmidt }
1348c2968612SBenjamin Herrenschmidt 
13491da177e4SLinus Torvalds /**
1350183ff22bSSimon Arlott  *	get_vm_area  -  reserve a contiguous kernel virtual area
13511da177e4SLinus Torvalds  *	@size:		size of the area
13521da177e4SLinus Torvalds  *	@flags:		%VM_IOREMAP for I/O mappings or VM_ALLOC
13531da177e4SLinus Torvalds  *
13541da177e4SLinus Torvalds  *	Search an area of @size in the kernel virtual mapping area,
13551da177e4SLinus Torvalds  *	and reserved it for out purposes.  Returns the area descriptor
13561da177e4SLinus Torvalds  *	on success or %NULL on failure.
13571da177e4SLinus Torvalds  */
13581da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
13591da177e4SLinus Torvalds {
13602dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
136123016969SChristoph Lameter 				-1, GFP_KERNEL, __builtin_return_address(0));
136223016969SChristoph Lameter }
136323016969SChristoph Lameter 
136423016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
136523016969SChristoph Lameter 				void *caller)
136623016969SChristoph Lameter {
13672dca6999SDavid Miller 	return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
136823016969SChristoph Lameter 						-1, GFP_KERNEL, caller);
13691da177e4SLinus Torvalds }
13701da177e4SLinus Torvalds 
1371db64fe02SNick Piggin static struct vm_struct *find_vm_area(const void *addr)
137283342314SNick Piggin {
1373db64fe02SNick Piggin 	struct vmap_area *va;
137483342314SNick Piggin 
1375db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
1376db64fe02SNick Piggin 	if (va && va->flags & VM_VM_AREA)
1377db64fe02SNick Piggin 		return va->private;
137883342314SNick Piggin 
13797856dfebSAndi Kleen 	return NULL;
13807856dfebSAndi Kleen }
13817856dfebSAndi Kleen 
13821da177e4SLinus Torvalds /**
1383183ff22bSSimon Arlott  *	remove_vm_area  -  find and remove a continuous kernel virtual area
13841da177e4SLinus Torvalds  *	@addr:		base address
13851da177e4SLinus Torvalds  *
13861da177e4SLinus Torvalds  *	Search for the kernel VM area starting at @addr, and remove it.
13871da177e4SLinus Torvalds  *	This function returns the found VM area, but using it is NOT safe
13887856dfebSAndi Kleen  *	on SMP machines, except for its size or flags.
13891da177e4SLinus Torvalds  */
1390b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr)
13911da177e4SLinus Torvalds {
1392db64fe02SNick Piggin 	struct vmap_area *va;
1393db64fe02SNick Piggin 
1394db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
1395db64fe02SNick Piggin 	if (va && va->flags & VM_VM_AREA) {
1396db64fe02SNick Piggin 		struct vm_struct *vm = va->private;
1397db64fe02SNick Piggin 		struct vm_struct *tmp, **p;
1398dd32c279SKAMEZAWA Hiroyuki 		/*
1399dd32c279SKAMEZAWA Hiroyuki 		 * remove from list and disallow access to this vm_struct
1400dd32c279SKAMEZAWA Hiroyuki 		 * before unmap. (address range confliction is maintained by
1401dd32c279SKAMEZAWA Hiroyuki 		 * vmap.)
1402dd32c279SKAMEZAWA Hiroyuki 		 */
14031da177e4SLinus Torvalds 		write_lock(&vmlist_lock);
1404db64fe02SNick Piggin 		for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
1405db64fe02SNick Piggin 			;
1406db64fe02SNick Piggin 		*p = tmp->next;
14071da177e4SLinus Torvalds 		write_unlock(&vmlist_lock);
1408db64fe02SNick Piggin 
1409dd32c279SKAMEZAWA Hiroyuki 		vmap_debug_free_range(va->va_start, va->va_end);
1410dd32c279SKAMEZAWA Hiroyuki 		free_unmap_vmap_area(va);
1411dd32c279SKAMEZAWA Hiroyuki 		vm->size -= PAGE_SIZE;
1412dd32c279SKAMEZAWA Hiroyuki 
1413db64fe02SNick Piggin 		return vm;
1414db64fe02SNick Piggin 	}
1415db64fe02SNick Piggin 	return NULL;
14161da177e4SLinus Torvalds }
14171da177e4SLinus Torvalds 
1418b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages)
14191da177e4SLinus Torvalds {
14201da177e4SLinus Torvalds 	struct vm_struct *area;
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	if (!addr)
14231da177e4SLinus Torvalds 		return;
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	if ((PAGE_SIZE-1) & (unsigned long)addr) {
14264c8573e2SArjan van de Ven 		WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
14271da177e4SLinus Torvalds 		return;
14281da177e4SLinus Torvalds 	}
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 	area = remove_vm_area(addr);
14311da177e4SLinus Torvalds 	if (unlikely(!area)) {
14324c8573e2SArjan van de Ven 		WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
14331da177e4SLinus Torvalds 				addr);
14341da177e4SLinus Torvalds 		return;
14351da177e4SLinus Torvalds 	}
14361da177e4SLinus Torvalds 
14379a11b49aSIngo Molnar 	debug_check_no_locks_freed(addr, area->size);
14383ac7fe5aSThomas Gleixner 	debug_check_no_obj_freed(addr, area->size);
14399a11b49aSIngo Molnar 
14401da177e4SLinus Torvalds 	if (deallocate_pages) {
14411da177e4SLinus Torvalds 		int i;
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 		for (i = 0; i < area->nr_pages; i++) {
1444bf53d6f8SChristoph Lameter 			struct page *page = area->pages[i];
1445bf53d6f8SChristoph Lameter 
1446bf53d6f8SChristoph Lameter 			BUG_ON(!page);
1447bf53d6f8SChristoph Lameter 			__free_page(page);
14481da177e4SLinus Torvalds 		}
14491da177e4SLinus Torvalds 
14508757d5faSJan Kiszka 		if (area->flags & VM_VPAGES)
14511da177e4SLinus Torvalds 			vfree(area->pages);
14521da177e4SLinus Torvalds 		else
14531da177e4SLinus Torvalds 			kfree(area->pages);
14541da177e4SLinus Torvalds 	}
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 	kfree(area);
14571da177e4SLinus Torvalds 	return;
14581da177e4SLinus Torvalds }
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds /**
14611da177e4SLinus Torvalds  *	vfree  -  release memory allocated by vmalloc()
14621da177e4SLinus Torvalds  *	@addr:		memory base address
14631da177e4SLinus Torvalds  *
1464183ff22bSSimon Arlott  *	Free the virtually continuous memory area starting at @addr, as
146580e93effSPekka Enberg  *	obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
146680e93effSPekka Enberg  *	NULL, no operation is performed.
14671da177e4SLinus Torvalds  *
146880e93effSPekka Enberg  *	Must not be called in interrupt context.
14691da177e4SLinus Torvalds  */
1470b3bdda02SChristoph Lameter void vfree(const void *addr)
14711da177e4SLinus Torvalds {
14721da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
147389219d37SCatalin Marinas 
147489219d37SCatalin Marinas 	kmemleak_free(addr);
147589219d37SCatalin Marinas 
14761da177e4SLinus Torvalds 	__vunmap(addr, 1);
14771da177e4SLinus Torvalds }
14781da177e4SLinus Torvalds EXPORT_SYMBOL(vfree);
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds /**
14811da177e4SLinus Torvalds  *	vunmap  -  release virtual mapping obtained by vmap()
14821da177e4SLinus Torvalds  *	@addr:		memory base address
14831da177e4SLinus Torvalds  *
14841da177e4SLinus Torvalds  *	Free the virtually contiguous memory area starting at @addr,
14851da177e4SLinus Torvalds  *	which was created from the page array passed to vmap().
14861da177e4SLinus Torvalds  *
148780e93effSPekka Enberg  *	Must not be called in interrupt context.
14881da177e4SLinus Torvalds  */
1489b3bdda02SChristoph Lameter void vunmap(const void *addr)
14901da177e4SLinus Torvalds {
14911da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
149234754b69SPeter Zijlstra 	might_sleep();
14931da177e4SLinus Torvalds 	__vunmap(addr, 0);
14941da177e4SLinus Torvalds }
14951da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap);
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds /**
14981da177e4SLinus Torvalds  *	vmap  -  map an array of pages into virtually contiguous space
14991da177e4SLinus Torvalds  *	@pages:		array of page pointers
15001da177e4SLinus Torvalds  *	@count:		number of pages to map
15011da177e4SLinus Torvalds  *	@flags:		vm_area->flags
15021da177e4SLinus Torvalds  *	@prot:		page protection for the mapping
15031da177e4SLinus Torvalds  *
15041da177e4SLinus Torvalds  *	Maps @count pages from @pages into contiguous kernel virtual
15051da177e4SLinus Torvalds  *	space.
15061da177e4SLinus Torvalds  */
15071da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count,
15081da177e4SLinus Torvalds 		unsigned long flags, pgprot_t prot)
15091da177e4SLinus Torvalds {
15101da177e4SLinus Torvalds 	struct vm_struct *area;
15111da177e4SLinus Torvalds 
151234754b69SPeter Zijlstra 	might_sleep();
151334754b69SPeter Zijlstra 
15144481374cSJan Beulich 	if (count > totalram_pages)
15151da177e4SLinus Torvalds 		return NULL;
15161da177e4SLinus Torvalds 
151723016969SChristoph Lameter 	area = get_vm_area_caller((count << PAGE_SHIFT), flags,
151823016969SChristoph Lameter 					__builtin_return_address(0));
15191da177e4SLinus Torvalds 	if (!area)
15201da177e4SLinus Torvalds 		return NULL;
152123016969SChristoph Lameter 
15221da177e4SLinus Torvalds 	if (map_vm_area(area, prot, &pages)) {
15231da177e4SLinus Torvalds 		vunmap(area->addr);
15241da177e4SLinus Torvalds 		return NULL;
15251da177e4SLinus Torvalds 	}
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	return area->addr;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds EXPORT_SYMBOL(vmap);
15301da177e4SLinus Torvalds 
15312dca6999SDavid Miller static void *__vmalloc_node(unsigned long size, unsigned long align,
15322dca6999SDavid Miller 			    gfp_t gfp_mask, pgprot_t prot,
1533db64fe02SNick Piggin 			    int node, void *caller);
1534e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
153523016969SChristoph Lameter 				 pgprot_t prot, int node, void *caller)
15361da177e4SLinus Torvalds {
15371da177e4SLinus Torvalds 	struct page **pages;
15381da177e4SLinus Torvalds 	unsigned int nr_pages, array_size, i;
1539976d6dfbSJan Beulich 	gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 	nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
15421da177e4SLinus Torvalds 	array_size = (nr_pages * sizeof(struct page *));
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	area->nr_pages = nr_pages;
15451da177e4SLinus Torvalds 	/* Please note that the recursion is strictly bounded. */
15468757d5faSJan Kiszka 	if (array_size > PAGE_SIZE) {
1547976d6dfbSJan Beulich 		pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,
154823016969SChristoph Lameter 				PAGE_KERNEL, node, caller);
15498757d5faSJan Kiszka 		area->flags |= VM_VPAGES;
1550286e1ea3SAndrew Morton 	} else {
1551976d6dfbSJan Beulich 		pages = kmalloc_node(array_size, nested_gfp, node);
1552286e1ea3SAndrew Morton 	}
15531da177e4SLinus Torvalds 	area->pages = pages;
155423016969SChristoph Lameter 	area->caller = caller;
15551da177e4SLinus Torvalds 	if (!area->pages) {
15561da177e4SLinus Torvalds 		remove_vm_area(area->addr);
15571da177e4SLinus Torvalds 		kfree(area);
15581da177e4SLinus Torvalds 		return NULL;
15591da177e4SLinus Torvalds 	}
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	for (i = 0; i < area->nr_pages; i++) {
1562bf53d6f8SChristoph Lameter 		struct page *page;
1563bf53d6f8SChristoph Lameter 
1564930fc45aSChristoph Lameter 		if (node < 0)
1565bf53d6f8SChristoph Lameter 			page = alloc_page(gfp_mask);
1566930fc45aSChristoph Lameter 		else
1567bf53d6f8SChristoph Lameter 			page = alloc_pages_node(node, gfp_mask, 0);
1568bf53d6f8SChristoph Lameter 
1569bf53d6f8SChristoph Lameter 		if (unlikely(!page)) {
15701da177e4SLinus Torvalds 			/* Successfully allocated i pages, free them in __vunmap() */
15711da177e4SLinus Torvalds 			area->nr_pages = i;
15721da177e4SLinus Torvalds 			goto fail;
15731da177e4SLinus Torvalds 		}
1574bf53d6f8SChristoph Lameter 		area->pages[i] = page;
15751da177e4SLinus Torvalds 	}
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	if (map_vm_area(area, prot, &pages))
15781da177e4SLinus Torvalds 		goto fail;
15791da177e4SLinus Torvalds 	return area->addr;
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds fail:
15821da177e4SLinus Torvalds 	vfree(area->addr);
15831da177e4SLinus Torvalds 	return NULL;
15841da177e4SLinus Torvalds }
15851da177e4SLinus Torvalds 
1586d0a21265SDavid Rientjes /**
1587d0a21265SDavid Rientjes  *	__vmalloc_node_range  -  allocate virtually contiguous memory
1588d0a21265SDavid Rientjes  *	@size:		allocation size
1589d0a21265SDavid Rientjes  *	@align:		desired alignment
1590d0a21265SDavid Rientjes  *	@start:		vm area range start
1591d0a21265SDavid Rientjes  *	@end:		vm area range end
1592d0a21265SDavid Rientjes  *	@gfp_mask:	flags for the page level allocator
1593d0a21265SDavid Rientjes  *	@prot:		protection mask for the allocated pages
1594d0a21265SDavid Rientjes  *	@node:		node to use for allocation or -1
1595d0a21265SDavid Rientjes  *	@caller:	caller's return address
1596d0a21265SDavid Rientjes  *
1597d0a21265SDavid Rientjes  *	Allocate enough pages to cover @size from the page level
1598d0a21265SDavid Rientjes  *	allocator with @gfp_mask flags.  Map them into contiguous
1599d0a21265SDavid Rientjes  *	kernel virtual space, using a pagetable protection of @prot.
1600d0a21265SDavid Rientjes  */
1601d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align,
1602d0a21265SDavid Rientjes 			unsigned long start, unsigned long end, gfp_t gfp_mask,
1603d0a21265SDavid Rientjes 			pgprot_t prot, int node, void *caller)
1604930fc45aSChristoph Lameter {
1605d0a21265SDavid Rientjes 	struct vm_struct *area;
1606d0a21265SDavid Rientjes 	void *addr;
1607d0a21265SDavid Rientjes 	unsigned long real_size = size;
1608d0a21265SDavid Rientjes 
1609d0a21265SDavid Rientjes 	size = PAGE_ALIGN(size);
1610d0a21265SDavid Rientjes 	if (!size || (size >> PAGE_SHIFT) > totalram_pages)
1611d0a21265SDavid Rientjes 		return NULL;
1612d0a21265SDavid Rientjes 
1613d0a21265SDavid Rientjes 	area = __get_vm_area_node(size, align, VM_ALLOC, start, end, node,
1614d0a21265SDavid Rientjes 				  gfp_mask, caller);
1615d0a21265SDavid Rientjes 
1616d0a21265SDavid Rientjes 	if (!area)
1617d0a21265SDavid Rientjes 		return NULL;
1618d0a21265SDavid Rientjes 
1619d0a21265SDavid Rientjes 	addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
162089219d37SCatalin Marinas 
162189219d37SCatalin Marinas 	/*
162289219d37SCatalin Marinas 	 * A ref_count = 3 is needed because the vm_struct and vmap_area
162389219d37SCatalin Marinas 	 * structures allocated in the __get_vm_area_node() function contain
162489219d37SCatalin Marinas 	 * references to the virtual address of the vmalloc'ed block.
162589219d37SCatalin Marinas 	 */
1626d0a21265SDavid Rientjes 	kmemleak_alloc(addr, real_size, 3, gfp_mask);
162789219d37SCatalin Marinas 
162889219d37SCatalin Marinas 	return addr;
1629930fc45aSChristoph Lameter }
1630930fc45aSChristoph Lameter 
16311da177e4SLinus Torvalds /**
1632930fc45aSChristoph Lameter  *	__vmalloc_node  -  allocate virtually contiguous memory
16331da177e4SLinus Torvalds  *	@size:		allocation size
16342dca6999SDavid Miller  *	@align:		desired alignment
16351da177e4SLinus Torvalds  *	@gfp_mask:	flags for the page level allocator
16361da177e4SLinus Torvalds  *	@prot:		protection mask for the allocated pages
1637d44e0780SRandy Dunlap  *	@node:		node to use for allocation or -1
1638c85d194bSRandy Dunlap  *	@caller:	caller's return address
16391da177e4SLinus Torvalds  *
16401da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
16411da177e4SLinus Torvalds  *	allocator with @gfp_mask flags.  Map them into contiguous
16421da177e4SLinus Torvalds  *	kernel virtual space, using a pagetable protection of @prot.
16431da177e4SLinus Torvalds  */
16442dca6999SDavid Miller static void *__vmalloc_node(unsigned long size, unsigned long align,
16452dca6999SDavid Miller 			    gfp_t gfp_mask, pgprot_t prot,
164623016969SChristoph Lameter 			    int node, void *caller)
16471da177e4SLinus Torvalds {
1648d0a21265SDavid Rientjes 	return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
1649d0a21265SDavid Rientjes 				gfp_mask, prot, node, caller);
16501da177e4SLinus Torvalds }
16511da177e4SLinus Torvalds 
1652930fc45aSChristoph Lameter void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1653930fc45aSChristoph Lameter {
16542dca6999SDavid Miller 	return __vmalloc_node(size, 1, gfp_mask, prot, -1,
165523016969SChristoph Lameter 				__builtin_return_address(0));
1656930fc45aSChristoph Lameter }
16571da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc);
16581da177e4SLinus Torvalds 
1659e1ca7788SDave Young static inline void *__vmalloc_node_flags(unsigned long size,
1660e1ca7788SDave Young 					int node, gfp_t flags)
1661e1ca7788SDave Young {
1662e1ca7788SDave Young 	return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
1663e1ca7788SDave Young 					node, __builtin_return_address(0));
1664e1ca7788SDave Young }
1665e1ca7788SDave Young 
16661da177e4SLinus Torvalds /**
16671da177e4SLinus Torvalds  *	vmalloc  -  allocate virtually contiguous memory
16681da177e4SLinus Torvalds  *	@size:		allocation size
16691da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
16701da177e4SLinus Torvalds  *	allocator and map them into contiguous kernel virtual space.
16711da177e4SLinus Torvalds  *
1672c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
16731da177e4SLinus Torvalds  *	use __vmalloc() instead.
16741da177e4SLinus Torvalds  */
16751da177e4SLinus Torvalds void *vmalloc(unsigned long size)
16761da177e4SLinus Torvalds {
1677e1ca7788SDave Young 	return __vmalloc_node_flags(size, -1, GFP_KERNEL | __GFP_HIGHMEM);
16781da177e4SLinus Torvalds }
16791da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc);
16801da177e4SLinus Torvalds 
1681930fc45aSChristoph Lameter /**
1682e1ca7788SDave Young  *	vzalloc - allocate virtually contiguous memory with zero fill
1683e1ca7788SDave Young  *	@size:	allocation size
1684e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
1685e1ca7788SDave Young  *	allocator and map them into contiguous kernel virtual space.
1686e1ca7788SDave Young  *	The memory allocated is set to zero.
1687e1ca7788SDave Young  *
1688e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
1689e1ca7788SDave Young  *	use __vmalloc() instead.
1690e1ca7788SDave Young  */
1691e1ca7788SDave Young void *vzalloc(unsigned long size)
1692e1ca7788SDave Young {
1693e1ca7788SDave Young 	return __vmalloc_node_flags(size, -1,
1694e1ca7788SDave Young 				GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1695e1ca7788SDave Young }
1696e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
1697e1ca7788SDave Young 
1698e1ca7788SDave Young /**
1699ead04089SRolf Eike Beer  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
170083342314SNick Piggin  * @size: allocation size
1701ead04089SRolf Eike Beer  *
1702ead04089SRolf Eike Beer  * The resulting memory area is zeroed so it can be mapped to userspace
1703ead04089SRolf Eike Beer  * without leaking data.
170483342314SNick Piggin  */
170583342314SNick Piggin void *vmalloc_user(unsigned long size)
170683342314SNick Piggin {
170783342314SNick Piggin 	struct vm_struct *area;
170883342314SNick Piggin 	void *ret;
170983342314SNick Piggin 
17102dca6999SDavid Miller 	ret = __vmalloc_node(size, SHMLBA,
17112dca6999SDavid Miller 			     GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
171284877848SGlauber Costa 			     PAGE_KERNEL, -1, __builtin_return_address(0));
17132b4ac44eSEric Dumazet 	if (ret) {
1714db64fe02SNick Piggin 		area = find_vm_area(ret);
171583342314SNick Piggin 		area->flags |= VM_USERMAP;
17162b4ac44eSEric Dumazet 	}
171783342314SNick Piggin 	return ret;
171883342314SNick Piggin }
171983342314SNick Piggin EXPORT_SYMBOL(vmalloc_user);
172083342314SNick Piggin 
172183342314SNick Piggin /**
1722930fc45aSChristoph Lameter  *	vmalloc_node  -  allocate memory on a specific node
1723930fc45aSChristoph Lameter  *	@size:		allocation size
1724d44e0780SRandy Dunlap  *	@node:		numa node
1725930fc45aSChristoph Lameter  *
1726930fc45aSChristoph Lameter  *	Allocate enough pages to cover @size from the page level
1727930fc45aSChristoph Lameter  *	allocator and map them into contiguous kernel virtual space.
1728930fc45aSChristoph Lameter  *
1729c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
1730930fc45aSChristoph Lameter  *	use __vmalloc() instead.
1731930fc45aSChristoph Lameter  */
1732930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node)
1733930fc45aSChristoph Lameter {
17342dca6999SDavid Miller 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
173523016969SChristoph Lameter 					node, __builtin_return_address(0));
1736930fc45aSChristoph Lameter }
1737930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node);
1738930fc45aSChristoph Lameter 
1739e1ca7788SDave Young /**
1740e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
1741e1ca7788SDave Young  * @size:	allocation size
1742e1ca7788SDave Young  * @node:	numa node
1743e1ca7788SDave Young  *
1744e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
1745e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
1746e1ca7788SDave Young  * The memory allocated is set to zero.
1747e1ca7788SDave Young  *
1748e1ca7788SDave Young  * For tight control over page level allocator and protection flags
1749e1ca7788SDave Young  * use __vmalloc_node() instead.
1750e1ca7788SDave Young  */
1751e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
1752e1ca7788SDave Young {
1753e1ca7788SDave Young 	return __vmalloc_node_flags(size, node,
1754e1ca7788SDave Young 			 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1755e1ca7788SDave Young }
1756e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
1757e1ca7788SDave Young 
17584dc3b16bSPavel Pisa #ifndef PAGE_KERNEL_EXEC
17594dc3b16bSPavel Pisa # define PAGE_KERNEL_EXEC PAGE_KERNEL
17604dc3b16bSPavel Pisa #endif
17614dc3b16bSPavel Pisa 
17621da177e4SLinus Torvalds /**
17631da177e4SLinus Torvalds  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
17641da177e4SLinus Torvalds  *	@size:		allocation size
17651da177e4SLinus Torvalds  *
17661da177e4SLinus Torvalds  *	Kernel-internal function to allocate enough pages to cover @size
17671da177e4SLinus Torvalds  *	the page level allocator and map them into contiguous and
17681da177e4SLinus Torvalds  *	executable kernel virtual space.
17691da177e4SLinus Torvalds  *
1770c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
17711da177e4SLinus Torvalds  *	use __vmalloc() instead.
17721da177e4SLinus Torvalds  */
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds void *vmalloc_exec(unsigned long size)
17751da177e4SLinus Torvalds {
17762dca6999SDavid Miller 	return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
177784877848SGlauber Costa 			      -1, __builtin_return_address(0));
17781da177e4SLinus Torvalds }
17791da177e4SLinus Torvalds 
17800d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
17817ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
17820d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
17837ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
17840d08e0d3SAndi Kleen #else
17850d08e0d3SAndi Kleen #define GFP_VMALLOC32 GFP_KERNEL
17860d08e0d3SAndi Kleen #endif
17870d08e0d3SAndi Kleen 
17881da177e4SLinus Torvalds /**
17891da177e4SLinus Torvalds  *	vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
17901da177e4SLinus Torvalds  *	@size:		allocation size
17911da177e4SLinus Torvalds  *
17921da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
17931da177e4SLinus Torvalds  *	page level allocator and map them into contiguous kernel virtual space.
17941da177e4SLinus Torvalds  */
17951da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
17961da177e4SLinus Torvalds {
17972dca6999SDavid Miller 	return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
179884877848SGlauber Costa 			      -1, __builtin_return_address(0));
17991da177e4SLinus Torvalds }
18001da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32);
18011da177e4SLinus Torvalds 
180283342314SNick Piggin /**
1803ead04089SRolf Eike Beer  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
180483342314SNick Piggin  *	@size:		allocation size
1805ead04089SRolf Eike Beer  *
1806ead04089SRolf Eike Beer  * The resulting memory area is 32bit addressable and zeroed so it can be
1807ead04089SRolf Eike Beer  * mapped to userspace without leaking data.
180883342314SNick Piggin  */
180983342314SNick Piggin void *vmalloc_32_user(unsigned long size)
181083342314SNick Piggin {
181183342314SNick Piggin 	struct vm_struct *area;
181283342314SNick Piggin 	void *ret;
181383342314SNick Piggin 
18142dca6999SDavid Miller 	ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
181584877848SGlauber Costa 			     -1, __builtin_return_address(0));
18162b4ac44eSEric Dumazet 	if (ret) {
1817db64fe02SNick Piggin 		area = find_vm_area(ret);
181883342314SNick Piggin 		area->flags |= VM_USERMAP;
18192b4ac44eSEric Dumazet 	}
182083342314SNick Piggin 	return ret;
182183342314SNick Piggin }
182283342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user);
182383342314SNick Piggin 
1824d0107eb0SKAMEZAWA Hiroyuki /*
1825d0107eb0SKAMEZAWA Hiroyuki  * small helper routine , copy contents to buf from addr.
1826d0107eb0SKAMEZAWA Hiroyuki  * If the page is not present, fill zero.
1827d0107eb0SKAMEZAWA Hiroyuki  */
1828d0107eb0SKAMEZAWA Hiroyuki 
1829d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count)
1830d0107eb0SKAMEZAWA Hiroyuki {
1831d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
1832d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
1833d0107eb0SKAMEZAWA Hiroyuki 
1834d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
1835d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
1836d0107eb0SKAMEZAWA Hiroyuki 
1837d0107eb0SKAMEZAWA Hiroyuki 		offset = (unsigned long)addr & ~PAGE_MASK;
1838d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
1839d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
1840d0107eb0SKAMEZAWA Hiroyuki 			length = count;
1841d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
1842d0107eb0SKAMEZAWA Hiroyuki 		/*
1843d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
1844d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
1845d0107eb0SKAMEZAWA Hiroyuki 		 * overhead of vmalloc()/vfree() calles for this _debug_
1846d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
1847d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
1848d0107eb0SKAMEZAWA Hiroyuki 		 */
1849d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
1850d0107eb0SKAMEZAWA Hiroyuki 			/*
1851d0107eb0SKAMEZAWA Hiroyuki 			 * we can expect USER0 is not used (see vread/vwrite's
1852d0107eb0SKAMEZAWA Hiroyuki 			 * function description)
1853d0107eb0SKAMEZAWA Hiroyuki 			 */
1854d0107eb0SKAMEZAWA Hiroyuki 			void *map = kmap_atomic(p, KM_USER0);
1855d0107eb0SKAMEZAWA Hiroyuki 			memcpy(buf, map + offset, length);
1856d0107eb0SKAMEZAWA Hiroyuki 			kunmap_atomic(map, KM_USER0);
1857d0107eb0SKAMEZAWA Hiroyuki 		} else
1858d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, length);
1859d0107eb0SKAMEZAWA Hiroyuki 
1860d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
1861d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
1862d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
1863d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
1864d0107eb0SKAMEZAWA Hiroyuki 	}
1865d0107eb0SKAMEZAWA Hiroyuki 	return copied;
1866d0107eb0SKAMEZAWA Hiroyuki }
1867d0107eb0SKAMEZAWA Hiroyuki 
1868d0107eb0SKAMEZAWA Hiroyuki static int aligned_vwrite(char *buf, char *addr, unsigned long count)
1869d0107eb0SKAMEZAWA Hiroyuki {
1870d0107eb0SKAMEZAWA Hiroyuki 	struct page *p;
1871d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
1872d0107eb0SKAMEZAWA Hiroyuki 
1873d0107eb0SKAMEZAWA Hiroyuki 	while (count) {
1874d0107eb0SKAMEZAWA Hiroyuki 		unsigned long offset, length;
1875d0107eb0SKAMEZAWA Hiroyuki 
1876d0107eb0SKAMEZAWA Hiroyuki 		offset = (unsigned long)addr & ~PAGE_MASK;
1877d0107eb0SKAMEZAWA Hiroyuki 		length = PAGE_SIZE - offset;
1878d0107eb0SKAMEZAWA Hiroyuki 		if (length > count)
1879d0107eb0SKAMEZAWA Hiroyuki 			length = count;
1880d0107eb0SKAMEZAWA Hiroyuki 		p = vmalloc_to_page(addr);
1881d0107eb0SKAMEZAWA Hiroyuki 		/*
1882d0107eb0SKAMEZAWA Hiroyuki 		 * To do safe access to this _mapped_ area, we need
1883d0107eb0SKAMEZAWA Hiroyuki 		 * lock. But adding lock here means that we need to add
1884d0107eb0SKAMEZAWA Hiroyuki 		 * overhead of vmalloc()/vfree() calles for this _debug_
1885d0107eb0SKAMEZAWA Hiroyuki 		 * interface, rarely used. Instead of that, we'll use
1886d0107eb0SKAMEZAWA Hiroyuki 		 * kmap() and get small overhead in this access function.
1887d0107eb0SKAMEZAWA Hiroyuki 		 */
1888d0107eb0SKAMEZAWA Hiroyuki 		if (p) {
1889d0107eb0SKAMEZAWA Hiroyuki 			/*
1890d0107eb0SKAMEZAWA Hiroyuki 			 * we can expect USER0 is not used (see vread/vwrite's
1891d0107eb0SKAMEZAWA Hiroyuki 			 * function description)
1892d0107eb0SKAMEZAWA Hiroyuki 			 */
1893d0107eb0SKAMEZAWA Hiroyuki 			void *map = kmap_atomic(p, KM_USER0);
1894d0107eb0SKAMEZAWA Hiroyuki 			memcpy(map + offset, buf, length);
1895d0107eb0SKAMEZAWA Hiroyuki 			kunmap_atomic(map, KM_USER0);
1896d0107eb0SKAMEZAWA Hiroyuki 		}
1897d0107eb0SKAMEZAWA Hiroyuki 		addr += length;
1898d0107eb0SKAMEZAWA Hiroyuki 		buf += length;
1899d0107eb0SKAMEZAWA Hiroyuki 		copied += length;
1900d0107eb0SKAMEZAWA Hiroyuki 		count -= length;
1901d0107eb0SKAMEZAWA Hiroyuki 	}
1902d0107eb0SKAMEZAWA Hiroyuki 	return copied;
1903d0107eb0SKAMEZAWA Hiroyuki }
1904d0107eb0SKAMEZAWA Hiroyuki 
1905d0107eb0SKAMEZAWA Hiroyuki /**
1906d0107eb0SKAMEZAWA Hiroyuki  *	vread() -  read vmalloc area in a safe way.
1907d0107eb0SKAMEZAWA Hiroyuki  *	@buf:		buffer for reading data
1908d0107eb0SKAMEZAWA Hiroyuki  *	@addr:		vm address.
1909d0107eb0SKAMEZAWA Hiroyuki  *	@count:		number of bytes to be read.
1910d0107eb0SKAMEZAWA Hiroyuki  *
1911d0107eb0SKAMEZAWA Hiroyuki  *	Returns # of bytes which addr and buf should be increased.
1912d0107eb0SKAMEZAWA Hiroyuki  *	(same number to @count). Returns 0 if [addr...addr+count) doesn't
1913d0107eb0SKAMEZAWA Hiroyuki  *	includes any intersect with alive vmalloc area.
1914d0107eb0SKAMEZAWA Hiroyuki  *
1915d0107eb0SKAMEZAWA Hiroyuki  *	This function checks that addr is a valid vmalloc'ed area, and
1916d0107eb0SKAMEZAWA Hiroyuki  *	copy data from that area to a given buffer. If the given memory range
1917d0107eb0SKAMEZAWA Hiroyuki  *	of [addr...addr+count) includes some valid address, data is copied to
1918d0107eb0SKAMEZAWA Hiroyuki  *	proper area of @buf. If there are memory holes, they'll be zero-filled.
1919d0107eb0SKAMEZAWA Hiroyuki  *	IOREMAP area is treated as memory hole and no copy is done.
1920d0107eb0SKAMEZAWA Hiroyuki  *
1921d0107eb0SKAMEZAWA Hiroyuki  *	If [addr...addr+count) doesn't includes any intersects with alive
1922d0107eb0SKAMEZAWA Hiroyuki  *	vm_struct area, returns 0.
1923d0107eb0SKAMEZAWA Hiroyuki  *	@buf should be kernel's buffer. Because	this function uses KM_USER0,
1924d0107eb0SKAMEZAWA Hiroyuki  *	the caller should guarantee KM_USER0 is not used.
1925d0107eb0SKAMEZAWA Hiroyuki  *
1926d0107eb0SKAMEZAWA Hiroyuki  *	Note: In usual ops, vread() is never necessary because the caller
1927d0107eb0SKAMEZAWA Hiroyuki  *	should know vmalloc() area is valid and can use memcpy().
1928d0107eb0SKAMEZAWA Hiroyuki  *	This is for routines which have to access vmalloc area without
1929d0107eb0SKAMEZAWA Hiroyuki  *	any informaion, as /dev/kmem.
1930d0107eb0SKAMEZAWA Hiroyuki  *
1931d0107eb0SKAMEZAWA Hiroyuki  */
1932d0107eb0SKAMEZAWA Hiroyuki 
19331da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
19341da177e4SLinus Torvalds {
19351da177e4SLinus Torvalds 	struct vm_struct *tmp;
19361da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
1937d0107eb0SKAMEZAWA Hiroyuki 	unsigned long buflen = count;
19381da177e4SLinus Torvalds 	unsigned long n;
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 	/* Don't allow overflow */
19411da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
19421da177e4SLinus Torvalds 		count = -(unsigned long) addr;
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 	read_lock(&vmlist_lock);
1945d0107eb0SKAMEZAWA Hiroyuki 	for (tmp = vmlist; count && tmp; tmp = tmp->next) {
19461da177e4SLinus Torvalds 		vaddr = (char *) tmp->addr;
19471da177e4SLinus Torvalds 		if (addr >= vaddr + tmp->size - PAGE_SIZE)
19481da177e4SLinus Torvalds 			continue;
19491da177e4SLinus Torvalds 		while (addr < vaddr) {
19501da177e4SLinus Torvalds 			if (count == 0)
19511da177e4SLinus Torvalds 				goto finished;
19521da177e4SLinus Torvalds 			*buf = '\0';
19531da177e4SLinus Torvalds 			buf++;
19541da177e4SLinus Torvalds 			addr++;
19551da177e4SLinus Torvalds 			count--;
19561da177e4SLinus Torvalds 		}
19571da177e4SLinus Torvalds 		n = vaddr + tmp->size - PAGE_SIZE - addr;
1958d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
1959d0107eb0SKAMEZAWA Hiroyuki 			n = count;
1960d0107eb0SKAMEZAWA Hiroyuki 		if (!(tmp->flags & VM_IOREMAP))
1961d0107eb0SKAMEZAWA Hiroyuki 			aligned_vread(buf, addr, n);
1962d0107eb0SKAMEZAWA Hiroyuki 		else /* IOREMAP area is treated as memory hole */
1963d0107eb0SKAMEZAWA Hiroyuki 			memset(buf, 0, n);
1964d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
1965d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
1966d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
19671da177e4SLinus Torvalds 	}
19681da177e4SLinus Torvalds finished:
19691da177e4SLinus Torvalds 	read_unlock(&vmlist_lock);
1970d0107eb0SKAMEZAWA Hiroyuki 
1971d0107eb0SKAMEZAWA Hiroyuki 	if (buf == buf_start)
1972d0107eb0SKAMEZAWA Hiroyuki 		return 0;
1973d0107eb0SKAMEZAWA Hiroyuki 	/* zero-fill memory holes */
1974d0107eb0SKAMEZAWA Hiroyuki 	if (buf != buf_start + buflen)
1975d0107eb0SKAMEZAWA Hiroyuki 		memset(buf, 0, buflen - (buf - buf_start));
1976d0107eb0SKAMEZAWA Hiroyuki 
1977d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
19781da177e4SLinus Torvalds }
19791da177e4SLinus Torvalds 
1980d0107eb0SKAMEZAWA Hiroyuki /**
1981d0107eb0SKAMEZAWA Hiroyuki  *	vwrite() -  write vmalloc area in a safe way.
1982d0107eb0SKAMEZAWA Hiroyuki  *	@buf:		buffer for source data
1983d0107eb0SKAMEZAWA Hiroyuki  *	@addr:		vm address.
1984d0107eb0SKAMEZAWA Hiroyuki  *	@count:		number of bytes to be read.
1985d0107eb0SKAMEZAWA Hiroyuki  *
1986d0107eb0SKAMEZAWA Hiroyuki  *	Returns # of bytes which addr and buf should be incresed.
1987d0107eb0SKAMEZAWA Hiroyuki  *	(same number to @count).
1988d0107eb0SKAMEZAWA Hiroyuki  *	If [addr...addr+count) doesn't includes any intersect with valid
1989d0107eb0SKAMEZAWA Hiroyuki  *	vmalloc area, returns 0.
1990d0107eb0SKAMEZAWA Hiroyuki  *
1991d0107eb0SKAMEZAWA Hiroyuki  *	This function checks that addr is a valid vmalloc'ed area, and
1992d0107eb0SKAMEZAWA Hiroyuki  *	copy data from a buffer to the given addr. If specified range of
1993d0107eb0SKAMEZAWA Hiroyuki  *	[addr...addr+count) includes some valid address, data is copied from
1994d0107eb0SKAMEZAWA Hiroyuki  *	proper area of @buf. If there are memory holes, no copy to hole.
1995d0107eb0SKAMEZAWA Hiroyuki  *	IOREMAP area is treated as memory hole and no copy is done.
1996d0107eb0SKAMEZAWA Hiroyuki  *
1997d0107eb0SKAMEZAWA Hiroyuki  *	If [addr...addr+count) doesn't includes any intersects with alive
1998d0107eb0SKAMEZAWA Hiroyuki  *	vm_struct area, returns 0.
1999d0107eb0SKAMEZAWA Hiroyuki  *	@buf should be kernel's buffer. Because	this function uses KM_USER0,
2000d0107eb0SKAMEZAWA Hiroyuki  *	the caller should guarantee KM_USER0 is not used.
2001d0107eb0SKAMEZAWA Hiroyuki  *
2002d0107eb0SKAMEZAWA Hiroyuki  *	Note: In usual ops, vwrite() is never necessary because the caller
2003d0107eb0SKAMEZAWA Hiroyuki  *	should know vmalloc() area is valid and can use memcpy().
2004d0107eb0SKAMEZAWA Hiroyuki  *	This is for routines which have to access vmalloc area without
2005d0107eb0SKAMEZAWA Hiroyuki  *	any informaion, as /dev/kmem.
2006d0107eb0SKAMEZAWA Hiroyuki  *
2007d0107eb0SKAMEZAWA Hiroyuki  *	The caller should guarantee KM_USER1 is not used.
2008d0107eb0SKAMEZAWA Hiroyuki  */
2009d0107eb0SKAMEZAWA Hiroyuki 
20101da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
20111da177e4SLinus Torvalds {
20121da177e4SLinus Torvalds 	struct vm_struct *tmp;
2013d0107eb0SKAMEZAWA Hiroyuki 	char *vaddr;
2014d0107eb0SKAMEZAWA Hiroyuki 	unsigned long n, buflen;
2015d0107eb0SKAMEZAWA Hiroyuki 	int copied = 0;
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds 	/* Don't allow overflow */
20181da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
20191da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2020d0107eb0SKAMEZAWA Hiroyuki 	buflen = count;
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 	read_lock(&vmlist_lock);
2023d0107eb0SKAMEZAWA Hiroyuki 	for (tmp = vmlist; count && tmp; tmp = tmp->next) {
20241da177e4SLinus Torvalds 		vaddr = (char *) tmp->addr;
20251da177e4SLinus Torvalds 		if (addr >= vaddr + tmp->size - PAGE_SIZE)
20261da177e4SLinus Torvalds 			continue;
20271da177e4SLinus Torvalds 		while (addr < vaddr) {
20281da177e4SLinus Torvalds 			if (count == 0)
20291da177e4SLinus Torvalds 				goto finished;
20301da177e4SLinus Torvalds 			buf++;
20311da177e4SLinus Torvalds 			addr++;
20321da177e4SLinus Torvalds 			count--;
20331da177e4SLinus Torvalds 		}
20341da177e4SLinus Torvalds 		n = vaddr + tmp->size - PAGE_SIZE - addr;
2035d0107eb0SKAMEZAWA Hiroyuki 		if (n > count)
2036d0107eb0SKAMEZAWA Hiroyuki 			n = count;
2037d0107eb0SKAMEZAWA Hiroyuki 		if (!(tmp->flags & VM_IOREMAP)) {
2038d0107eb0SKAMEZAWA Hiroyuki 			aligned_vwrite(buf, addr, n);
2039d0107eb0SKAMEZAWA Hiroyuki 			copied++;
2040d0107eb0SKAMEZAWA Hiroyuki 		}
2041d0107eb0SKAMEZAWA Hiroyuki 		buf += n;
2042d0107eb0SKAMEZAWA Hiroyuki 		addr += n;
2043d0107eb0SKAMEZAWA Hiroyuki 		count -= n;
20441da177e4SLinus Torvalds 	}
20451da177e4SLinus Torvalds finished:
20461da177e4SLinus Torvalds 	read_unlock(&vmlist_lock);
2047d0107eb0SKAMEZAWA Hiroyuki 	if (!copied)
2048d0107eb0SKAMEZAWA Hiroyuki 		return 0;
2049d0107eb0SKAMEZAWA Hiroyuki 	return buflen;
20501da177e4SLinus Torvalds }
205183342314SNick Piggin 
205283342314SNick Piggin /**
205383342314SNick Piggin  *	remap_vmalloc_range  -  map vmalloc pages to userspace
205483342314SNick Piggin  *	@vma:		vma to cover (map full range of vma)
205583342314SNick Piggin  *	@addr:		vmalloc memory
205683342314SNick Piggin  *	@pgoff:		number of pages into addr before first page to map
20577682486bSRandy Dunlap  *
20587682486bSRandy Dunlap  *	Returns:	0 for success, -Exxx on failure
205983342314SNick Piggin  *
206083342314SNick Piggin  *	This function checks that addr is a valid vmalloc'ed area, and
206183342314SNick Piggin  *	that it is big enough to cover the vma. Will return failure if
206283342314SNick Piggin  *	that criteria isn't met.
206383342314SNick Piggin  *
206472fd4a35SRobert P. J. Day  *	Similar to remap_pfn_range() (see mm/memory.c)
206583342314SNick Piggin  */
206683342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
206783342314SNick Piggin 						unsigned long pgoff)
206883342314SNick Piggin {
206983342314SNick Piggin 	struct vm_struct *area;
207083342314SNick Piggin 	unsigned long uaddr = vma->vm_start;
207183342314SNick Piggin 	unsigned long usize = vma->vm_end - vma->vm_start;
207283342314SNick Piggin 
207383342314SNick Piggin 	if ((PAGE_SIZE-1) & (unsigned long)addr)
207483342314SNick Piggin 		return -EINVAL;
207583342314SNick Piggin 
2076db64fe02SNick Piggin 	area = find_vm_area(addr);
207783342314SNick Piggin 	if (!area)
2078db64fe02SNick Piggin 		return -EINVAL;
207983342314SNick Piggin 
208083342314SNick Piggin 	if (!(area->flags & VM_USERMAP))
2081db64fe02SNick Piggin 		return -EINVAL;
208283342314SNick Piggin 
208383342314SNick Piggin 	if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
2084db64fe02SNick Piggin 		return -EINVAL;
208583342314SNick Piggin 
208683342314SNick Piggin 	addr += pgoff << PAGE_SHIFT;
208783342314SNick Piggin 	do {
208883342314SNick Piggin 		struct page *page = vmalloc_to_page(addr);
2089db64fe02SNick Piggin 		int ret;
2090db64fe02SNick Piggin 
209183342314SNick Piggin 		ret = vm_insert_page(vma, uaddr, page);
209283342314SNick Piggin 		if (ret)
209383342314SNick Piggin 			return ret;
209483342314SNick Piggin 
209583342314SNick Piggin 		uaddr += PAGE_SIZE;
209683342314SNick Piggin 		addr += PAGE_SIZE;
209783342314SNick Piggin 		usize -= PAGE_SIZE;
209883342314SNick Piggin 	} while (usize > 0);
209983342314SNick Piggin 
210083342314SNick Piggin 	/* Prevent "things" like memory migration? VM_flags need a cleanup... */
210183342314SNick Piggin 	vma->vm_flags |= VM_RESERVED;
210283342314SNick Piggin 
2103db64fe02SNick Piggin 	return 0;
210483342314SNick Piggin }
210583342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range);
210683342314SNick Piggin 
21071eeb66a1SChristoph Hellwig /*
21081eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
21091eeb66a1SChristoph Hellwig  * have one.
21101eeb66a1SChristoph Hellwig  */
21111eeb66a1SChristoph Hellwig void  __attribute__((weak)) vmalloc_sync_all(void)
21121eeb66a1SChristoph Hellwig {
21131eeb66a1SChristoph Hellwig }
21145f4352fbSJeremy Fitzhardinge 
21155f4352fbSJeremy Fitzhardinge 
21162f569afdSMartin Schwidefsky static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
21175f4352fbSJeremy Fitzhardinge {
21185f4352fbSJeremy Fitzhardinge 	/* apply_to_page_range() does all the hard work. */
21195f4352fbSJeremy Fitzhardinge 	return 0;
21205f4352fbSJeremy Fitzhardinge }
21215f4352fbSJeremy Fitzhardinge 
21225f4352fbSJeremy Fitzhardinge /**
21235f4352fbSJeremy Fitzhardinge  *	alloc_vm_area - allocate a range of kernel address space
21245f4352fbSJeremy Fitzhardinge  *	@size:		size of the area
21257682486bSRandy Dunlap  *
21267682486bSRandy Dunlap  *	Returns:	NULL on failure, vm_struct on success
21275f4352fbSJeremy Fitzhardinge  *
21285f4352fbSJeremy Fitzhardinge  *	This function reserves a range of kernel address space, and
21295f4352fbSJeremy Fitzhardinge  *	allocates pagetables to map that range.  No actual mappings
21305f4352fbSJeremy Fitzhardinge  *	are created.  If the kernel address space is not shared
21315f4352fbSJeremy Fitzhardinge  *	between processes, it syncs the pagetable across all
21325f4352fbSJeremy Fitzhardinge  *	processes.
21335f4352fbSJeremy Fitzhardinge  */
21345f4352fbSJeremy Fitzhardinge struct vm_struct *alloc_vm_area(size_t size)
21355f4352fbSJeremy Fitzhardinge {
21365f4352fbSJeremy Fitzhardinge 	struct vm_struct *area;
21375f4352fbSJeremy Fitzhardinge 
213823016969SChristoph Lameter 	area = get_vm_area_caller(size, VM_IOREMAP,
213923016969SChristoph Lameter 				__builtin_return_address(0));
21405f4352fbSJeremy Fitzhardinge 	if (area == NULL)
21415f4352fbSJeremy Fitzhardinge 		return NULL;
21425f4352fbSJeremy Fitzhardinge 
21435f4352fbSJeremy Fitzhardinge 	/*
21445f4352fbSJeremy Fitzhardinge 	 * This ensures that page tables are constructed for this region
21455f4352fbSJeremy Fitzhardinge 	 * of kernel virtual address space and mapped into init_mm.
21465f4352fbSJeremy Fitzhardinge 	 */
21475f4352fbSJeremy Fitzhardinge 	if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
21485f4352fbSJeremy Fitzhardinge 				area->size, f, NULL)) {
21495f4352fbSJeremy Fitzhardinge 		free_vm_area(area);
21505f4352fbSJeremy Fitzhardinge 		return NULL;
21515f4352fbSJeremy Fitzhardinge 	}
21525f4352fbSJeremy Fitzhardinge 
21535f4352fbSJeremy Fitzhardinge 	/* Make sure the pagetables are constructed in process kernel
21545f4352fbSJeremy Fitzhardinge 	   mappings */
21555f4352fbSJeremy Fitzhardinge 	vmalloc_sync_all();
21565f4352fbSJeremy Fitzhardinge 
21575f4352fbSJeremy Fitzhardinge 	return area;
21585f4352fbSJeremy Fitzhardinge }
21595f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(alloc_vm_area);
21605f4352fbSJeremy Fitzhardinge 
21615f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area)
21625f4352fbSJeremy Fitzhardinge {
21635f4352fbSJeremy Fitzhardinge 	struct vm_struct *ret;
21645f4352fbSJeremy Fitzhardinge 	ret = remove_vm_area(area->addr);
21655f4352fbSJeremy Fitzhardinge 	BUG_ON(ret != area);
21665f4352fbSJeremy Fitzhardinge 	kfree(area);
21675f4352fbSJeremy Fitzhardinge }
21685f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area);
2169a10aa579SChristoph Lameter 
21704f8b02b4STejun Heo #ifdef CONFIG_SMP
2171ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n)
2172ca23e405STejun Heo {
2173ca23e405STejun Heo 	return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
2174ca23e405STejun Heo }
2175ca23e405STejun Heo 
2176ca23e405STejun Heo /**
2177ca23e405STejun Heo  * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
2178ca23e405STejun Heo  * @end: target address
2179ca23e405STejun Heo  * @pnext: out arg for the next vmap_area
2180ca23e405STejun Heo  * @pprev: out arg for the previous vmap_area
2181ca23e405STejun Heo  *
2182ca23e405STejun Heo  * Returns: %true if either or both of next and prev are found,
2183ca23e405STejun Heo  *	    %false if no vmap_area exists
2184ca23e405STejun Heo  *
2185ca23e405STejun Heo  * Find vmap_areas end addresses of which enclose @end.  ie. if not
2186ca23e405STejun Heo  * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
2187ca23e405STejun Heo  */
2188ca23e405STejun Heo static bool pvm_find_next_prev(unsigned long end,
2189ca23e405STejun Heo 			       struct vmap_area **pnext,
2190ca23e405STejun Heo 			       struct vmap_area **pprev)
2191ca23e405STejun Heo {
2192ca23e405STejun Heo 	struct rb_node *n = vmap_area_root.rb_node;
2193ca23e405STejun Heo 	struct vmap_area *va = NULL;
2194ca23e405STejun Heo 
2195ca23e405STejun Heo 	while (n) {
2196ca23e405STejun Heo 		va = rb_entry(n, struct vmap_area, rb_node);
2197ca23e405STejun Heo 		if (end < va->va_end)
2198ca23e405STejun Heo 			n = n->rb_left;
2199ca23e405STejun Heo 		else if (end > va->va_end)
2200ca23e405STejun Heo 			n = n->rb_right;
2201ca23e405STejun Heo 		else
2202ca23e405STejun Heo 			break;
2203ca23e405STejun Heo 	}
2204ca23e405STejun Heo 
2205ca23e405STejun Heo 	if (!va)
2206ca23e405STejun Heo 		return false;
2207ca23e405STejun Heo 
2208ca23e405STejun Heo 	if (va->va_end > end) {
2209ca23e405STejun Heo 		*pnext = va;
2210ca23e405STejun Heo 		*pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2211ca23e405STejun Heo 	} else {
2212ca23e405STejun Heo 		*pprev = va;
2213ca23e405STejun Heo 		*pnext = node_to_va(rb_next(&(*pprev)->rb_node));
2214ca23e405STejun Heo 	}
2215ca23e405STejun Heo 	return true;
2216ca23e405STejun Heo }
2217ca23e405STejun Heo 
2218ca23e405STejun Heo /**
2219ca23e405STejun Heo  * pvm_determine_end - find the highest aligned address between two vmap_areas
2220ca23e405STejun Heo  * @pnext: in/out arg for the next vmap_area
2221ca23e405STejun Heo  * @pprev: in/out arg for the previous vmap_area
2222ca23e405STejun Heo  * @align: alignment
2223ca23e405STejun Heo  *
2224ca23e405STejun Heo  * Returns: determined end address
2225ca23e405STejun Heo  *
2226ca23e405STejun Heo  * Find the highest aligned address between *@pnext and *@pprev below
2227ca23e405STejun Heo  * VMALLOC_END.  *@pnext and *@pprev are adjusted so that the aligned
2228ca23e405STejun Heo  * down address is between the end addresses of the two vmap_areas.
2229ca23e405STejun Heo  *
2230ca23e405STejun Heo  * Please note that the address returned by this function may fall
2231ca23e405STejun Heo  * inside *@pnext vmap_area.  The caller is responsible for checking
2232ca23e405STejun Heo  * that.
2233ca23e405STejun Heo  */
2234ca23e405STejun Heo static unsigned long pvm_determine_end(struct vmap_area **pnext,
2235ca23e405STejun Heo 				       struct vmap_area **pprev,
2236ca23e405STejun Heo 				       unsigned long align)
2237ca23e405STejun Heo {
2238ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2239ca23e405STejun Heo 	unsigned long addr;
2240ca23e405STejun Heo 
2241ca23e405STejun Heo 	if (*pnext)
2242ca23e405STejun Heo 		addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
2243ca23e405STejun Heo 	else
2244ca23e405STejun Heo 		addr = vmalloc_end;
2245ca23e405STejun Heo 
2246ca23e405STejun Heo 	while (*pprev && (*pprev)->va_end > addr) {
2247ca23e405STejun Heo 		*pnext = *pprev;
2248ca23e405STejun Heo 		*pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2249ca23e405STejun Heo 	}
2250ca23e405STejun Heo 
2251ca23e405STejun Heo 	return addr;
2252ca23e405STejun Heo }
2253ca23e405STejun Heo 
2254ca23e405STejun Heo /**
2255ca23e405STejun Heo  * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
2256ca23e405STejun Heo  * @offsets: array containing offset of each area
2257ca23e405STejun Heo  * @sizes: array containing size of each area
2258ca23e405STejun Heo  * @nr_vms: the number of areas to allocate
2259ca23e405STejun Heo  * @align: alignment, all entries in @offsets and @sizes must be aligned to this
2260ca23e405STejun Heo  *
2261ca23e405STejun Heo  * Returns: kmalloc'd vm_struct pointer array pointing to allocated
2262ca23e405STejun Heo  *	    vm_structs on success, %NULL on failure
2263ca23e405STejun Heo  *
2264ca23e405STejun Heo  * Percpu allocator wants to use congruent vm areas so that it can
2265ca23e405STejun Heo  * maintain the offsets among percpu areas.  This function allocates
2266ec3f64fcSDavid Rientjes  * congruent vmalloc areas for it with GFP_KERNEL.  These areas tend to
2267ec3f64fcSDavid Rientjes  * be scattered pretty far, distance between two areas easily going up
2268ec3f64fcSDavid Rientjes  * to gigabytes.  To avoid interacting with regular vmallocs, these
2269ec3f64fcSDavid Rientjes  * areas are allocated from top.
2270ca23e405STejun Heo  *
2271ca23e405STejun Heo  * Despite its complicated look, this allocator is rather simple.  It
2272ca23e405STejun Heo  * does everything top-down and scans areas from the end looking for
2273ca23e405STejun Heo  * matching slot.  While scanning, if any of the areas overlaps with
2274ca23e405STejun Heo  * existing vmap_area, the base address is pulled down to fit the
2275ca23e405STejun Heo  * area.  Scanning is repeated till all the areas fit and then all
2276ca23e405STejun Heo  * necessary data structres are inserted and the result is returned.
2277ca23e405STejun Heo  */
2278ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
2279ca23e405STejun Heo 				     const size_t *sizes, int nr_vms,
2280ec3f64fcSDavid Rientjes 				     size_t align)
2281ca23e405STejun Heo {
2282ca23e405STejun Heo 	const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
2283ca23e405STejun Heo 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2284ca23e405STejun Heo 	struct vmap_area **vas, *prev, *next;
2285ca23e405STejun Heo 	struct vm_struct **vms;
2286ca23e405STejun Heo 	int area, area2, last_area, term_area;
2287ca23e405STejun Heo 	unsigned long base, start, end, last_end;
2288ca23e405STejun Heo 	bool purged = false;
2289ca23e405STejun Heo 
2290ca23e405STejun Heo 	/* verify parameters and allocate data structures */
2291ca23e405STejun Heo 	BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align));
2292ca23e405STejun Heo 	for (last_area = 0, area = 0; area < nr_vms; area++) {
2293ca23e405STejun Heo 		start = offsets[area];
2294ca23e405STejun Heo 		end = start + sizes[area];
2295ca23e405STejun Heo 
2296ca23e405STejun Heo 		/* is everything aligned properly? */
2297ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(offsets[area], align));
2298ca23e405STejun Heo 		BUG_ON(!IS_ALIGNED(sizes[area], align));
2299ca23e405STejun Heo 
2300ca23e405STejun Heo 		/* detect the area with the highest address */
2301ca23e405STejun Heo 		if (start > offsets[last_area])
2302ca23e405STejun Heo 			last_area = area;
2303ca23e405STejun Heo 
2304ca23e405STejun Heo 		for (area2 = 0; area2 < nr_vms; area2++) {
2305ca23e405STejun Heo 			unsigned long start2 = offsets[area2];
2306ca23e405STejun Heo 			unsigned long end2 = start2 + sizes[area2];
2307ca23e405STejun Heo 
2308ca23e405STejun Heo 			if (area2 == area)
2309ca23e405STejun Heo 				continue;
2310ca23e405STejun Heo 
2311ca23e405STejun Heo 			BUG_ON(start2 >= start && start2 < end);
2312ca23e405STejun Heo 			BUG_ON(end2 <= end && end2 > start);
2313ca23e405STejun Heo 		}
2314ca23e405STejun Heo 	}
2315ca23e405STejun Heo 	last_end = offsets[last_area] + sizes[last_area];
2316ca23e405STejun Heo 
2317ca23e405STejun Heo 	if (vmalloc_end - vmalloc_start < last_end) {
2318ca23e405STejun Heo 		WARN_ON(true);
2319ca23e405STejun Heo 		return NULL;
2320ca23e405STejun Heo 	}
2321ca23e405STejun Heo 
2322ec3f64fcSDavid Rientjes 	vms = kzalloc(sizeof(vms[0]) * nr_vms, GFP_KERNEL);
2323ec3f64fcSDavid Rientjes 	vas = kzalloc(sizeof(vas[0]) * nr_vms, GFP_KERNEL);
2324ca23e405STejun Heo 	if (!vas || !vms)
2325ca23e405STejun Heo 		goto err_free;
2326ca23e405STejun Heo 
2327ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
2328ec3f64fcSDavid Rientjes 		vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL);
2329ec3f64fcSDavid Rientjes 		vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
2330ca23e405STejun Heo 		if (!vas[area] || !vms[area])
2331ca23e405STejun Heo 			goto err_free;
2332ca23e405STejun Heo 	}
2333ca23e405STejun Heo retry:
2334ca23e405STejun Heo 	spin_lock(&vmap_area_lock);
2335ca23e405STejun Heo 
2336ca23e405STejun Heo 	/* start scanning - we scan from the top, begin with the last area */
2337ca23e405STejun Heo 	area = term_area = last_area;
2338ca23e405STejun Heo 	start = offsets[area];
2339ca23e405STejun Heo 	end = start + sizes[area];
2340ca23e405STejun Heo 
2341ca23e405STejun Heo 	if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2342ca23e405STejun Heo 		base = vmalloc_end - last_end;
2343ca23e405STejun Heo 		goto found;
2344ca23e405STejun Heo 	}
2345ca23e405STejun Heo 	base = pvm_determine_end(&next, &prev, align) - end;
2346ca23e405STejun Heo 
2347ca23e405STejun Heo 	while (true) {
2348ca23e405STejun Heo 		BUG_ON(next && next->va_end <= base + end);
2349ca23e405STejun Heo 		BUG_ON(prev && prev->va_end > base + end);
2350ca23e405STejun Heo 
2351ca23e405STejun Heo 		/*
2352ca23e405STejun Heo 		 * base might have underflowed, add last_end before
2353ca23e405STejun Heo 		 * comparing.
2354ca23e405STejun Heo 		 */
2355ca23e405STejun Heo 		if (base + last_end < vmalloc_start + last_end) {
2356ca23e405STejun Heo 			spin_unlock(&vmap_area_lock);
2357ca23e405STejun Heo 			if (!purged) {
2358ca23e405STejun Heo 				purge_vmap_area_lazy();
2359ca23e405STejun Heo 				purged = true;
2360ca23e405STejun Heo 				goto retry;
2361ca23e405STejun Heo 			}
2362ca23e405STejun Heo 			goto err_free;
2363ca23e405STejun Heo 		}
2364ca23e405STejun Heo 
2365ca23e405STejun Heo 		/*
2366ca23e405STejun Heo 		 * If next overlaps, move base downwards so that it's
2367ca23e405STejun Heo 		 * right below next and then recheck.
2368ca23e405STejun Heo 		 */
2369ca23e405STejun Heo 		if (next && next->va_start < base + end) {
2370ca23e405STejun Heo 			base = pvm_determine_end(&next, &prev, align) - end;
2371ca23e405STejun Heo 			term_area = area;
2372ca23e405STejun Heo 			continue;
2373ca23e405STejun Heo 		}
2374ca23e405STejun Heo 
2375ca23e405STejun Heo 		/*
2376ca23e405STejun Heo 		 * If prev overlaps, shift down next and prev and move
2377ca23e405STejun Heo 		 * base so that it's right below new next and then
2378ca23e405STejun Heo 		 * recheck.
2379ca23e405STejun Heo 		 */
2380ca23e405STejun Heo 		if (prev && prev->va_end > base + start)  {
2381ca23e405STejun Heo 			next = prev;
2382ca23e405STejun Heo 			prev = node_to_va(rb_prev(&next->rb_node));
2383ca23e405STejun Heo 			base = pvm_determine_end(&next, &prev, align) - end;
2384ca23e405STejun Heo 			term_area = area;
2385ca23e405STejun Heo 			continue;
2386ca23e405STejun Heo 		}
2387ca23e405STejun Heo 
2388ca23e405STejun Heo 		/*
2389ca23e405STejun Heo 		 * This area fits, move on to the previous one.  If
2390ca23e405STejun Heo 		 * the previous one is the terminal one, we're done.
2391ca23e405STejun Heo 		 */
2392ca23e405STejun Heo 		area = (area + nr_vms - 1) % nr_vms;
2393ca23e405STejun Heo 		if (area == term_area)
2394ca23e405STejun Heo 			break;
2395ca23e405STejun Heo 		start = offsets[area];
2396ca23e405STejun Heo 		end = start + sizes[area];
2397ca23e405STejun Heo 		pvm_find_next_prev(base + end, &next, &prev);
2398ca23e405STejun Heo 	}
2399ca23e405STejun Heo found:
2400ca23e405STejun Heo 	/* we've found a fitting base, insert all va's */
2401ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
2402ca23e405STejun Heo 		struct vmap_area *va = vas[area];
2403ca23e405STejun Heo 
2404ca23e405STejun Heo 		va->va_start = base + offsets[area];
2405ca23e405STejun Heo 		va->va_end = va->va_start + sizes[area];
2406ca23e405STejun Heo 		__insert_vmap_area(va);
2407ca23e405STejun Heo 	}
2408ca23e405STejun Heo 
2409ca23e405STejun Heo 	vmap_area_pcpu_hole = base + offsets[last_area];
2410ca23e405STejun Heo 
2411ca23e405STejun Heo 	spin_unlock(&vmap_area_lock);
2412ca23e405STejun Heo 
2413ca23e405STejun Heo 	/* insert all vm's */
2414ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++)
2415ca23e405STejun Heo 		insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2416ca23e405STejun Heo 				  pcpu_get_vm_areas);
2417ca23e405STejun Heo 
2418ca23e405STejun Heo 	kfree(vas);
2419ca23e405STejun Heo 	return vms;
2420ca23e405STejun Heo 
2421ca23e405STejun Heo err_free:
2422ca23e405STejun Heo 	for (area = 0; area < nr_vms; area++) {
2423ca23e405STejun Heo 		if (vas)
2424ca23e405STejun Heo 			kfree(vas[area]);
2425ca23e405STejun Heo 		if (vms)
2426ca23e405STejun Heo 			kfree(vms[area]);
2427ca23e405STejun Heo 	}
2428ca23e405STejun Heo 	kfree(vas);
2429ca23e405STejun Heo 	kfree(vms);
2430ca23e405STejun Heo 	return NULL;
2431ca23e405STejun Heo }
2432ca23e405STejun Heo 
2433ca23e405STejun Heo /**
2434ca23e405STejun Heo  * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2435ca23e405STejun Heo  * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2436ca23e405STejun Heo  * @nr_vms: the number of allocated areas
2437ca23e405STejun Heo  *
2438ca23e405STejun Heo  * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2439ca23e405STejun Heo  */
2440ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2441ca23e405STejun Heo {
2442ca23e405STejun Heo 	int i;
2443ca23e405STejun Heo 
2444ca23e405STejun Heo 	for (i = 0; i < nr_vms; i++)
2445ca23e405STejun Heo 		free_vm_area(vms[i]);
2446ca23e405STejun Heo 	kfree(vms);
2447ca23e405STejun Heo }
24484f8b02b4STejun Heo #endif	/* CONFIG_SMP */
2449a10aa579SChristoph Lameter 
2450a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS
2451a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos)
2452e199b5d1SNamhyung Kim 	__acquires(&vmlist_lock)
2453a10aa579SChristoph Lameter {
2454a10aa579SChristoph Lameter 	loff_t n = *pos;
2455a10aa579SChristoph Lameter 	struct vm_struct *v;
2456a10aa579SChristoph Lameter 
2457a10aa579SChristoph Lameter 	read_lock(&vmlist_lock);
2458a10aa579SChristoph Lameter 	v = vmlist;
2459a10aa579SChristoph Lameter 	while (n > 0 && v) {
2460a10aa579SChristoph Lameter 		n--;
2461a10aa579SChristoph Lameter 		v = v->next;
2462a10aa579SChristoph Lameter 	}
2463a10aa579SChristoph Lameter 	if (!n)
2464a10aa579SChristoph Lameter 		return v;
2465a10aa579SChristoph Lameter 
2466a10aa579SChristoph Lameter 	return NULL;
2467a10aa579SChristoph Lameter 
2468a10aa579SChristoph Lameter }
2469a10aa579SChristoph Lameter 
2470a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2471a10aa579SChristoph Lameter {
2472a10aa579SChristoph Lameter 	struct vm_struct *v = p;
2473a10aa579SChristoph Lameter 
2474a10aa579SChristoph Lameter 	++*pos;
2475a10aa579SChristoph Lameter 	return v->next;
2476a10aa579SChristoph Lameter }
2477a10aa579SChristoph Lameter 
2478a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p)
2479e199b5d1SNamhyung Kim 	__releases(&vmlist_lock)
2480a10aa579SChristoph Lameter {
2481a10aa579SChristoph Lameter 	read_unlock(&vmlist_lock);
2482a10aa579SChristoph Lameter }
2483a10aa579SChristoph Lameter 
2484a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2485a47a126aSEric Dumazet {
2486a47a126aSEric Dumazet 	if (NUMA_BUILD) {
2487a47a126aSEric Dumazet 		unsigned int nr, *counters = m->private;
2488a47a126aSEric Dumazet 
2489a47a126aSEric Dumazet 		if (!counters)
2490a47a126aSEric Dumazet 			return;
2491a47a126aSEric Dumazet 
2492a47a126aSEric Dumazet 		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2493a47a126aSEric Dumazet 
2494a47a126aSEric Dumazet 		for (nr = 0; nr < v->nr_pages; nr++)
2495a47a126aSEric Dumazet 			counters[page_to_nid(v->pages[nr])]++;
2496a47a126aSEric Dumazet 
2497a47a126aSEric Dumazet 		for_each_node_state(nr, N_HIGH_MEMORY)
2498a47a126aSEric Dumazet 			if (counters[nr])
2499a47a126aSEric Dumazet 				seq_printf(m, " N%u=%u", nr, counters[nr]);
2500a47a126aSEric Dumazet 	}
2501a47a126aSEric Dumazet }
2502a47a126aSEric Dumazet 
2503a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p)
2504a10aa579SChristoph Lameter {
2505a10aa579SChristoph Lameter 	struct vm_struct *v = p;
2506a10aa579SChristoph Lameter 
2507a10aa579SChristoph Lameter 	seq_printf(m, "0x%p-0x%p %7ld",
2508a10aa579SChristoph Lameter 		v->addr, v->addr + v->size, v->size);
2509a10aa579SChristoph Lameter 
251062c70bceSJoe Perches 	if (v->caller)
251162c70bceSJoe Perches 		seq_printf(m, " %pS", v->caller);
251223016969SChristoph Lameter 
2513a10aa579SChristoph Lameter 	if (v->nr_pages)
2514a10aa579SChristoph Lameter 		seq_printf(m, " pages=%d", v->nr_pages);
2515a10aa579SChristoph Lameter 
2516a10aa579SChristoph Lameter 	if (v->phys_addr)
2517ffa71f33SKenji Kaneshige 		seq_printf(m, " phys=%llx", (unsigned long long)v->phys_addr);
2518a10aa579SChristoph Lameter 
2519a10aa579SChristoph Lameter 	if (v->flags & VM_IOREMAP)
2520a10aa579SChristoph Lameter 		seq_printf(m, " ioremap");
2521a10aa579SChristoph Lameter 
2522a10aa579SChristoph Lameter 	if (v->flags & VM_ALLOC)
2523a10aa579SChristoph Lameter 		seq_printf(m, " vmalloc");
2524a10aa579SChristoph Lameter 
2525a10aa579SChristoph Lameter 	if (v->flags & VM_MAP)
2526a10aa579SChristoph Lameter 		seq_printf(m, " vmap");
2527a10aa579SChristoph Lameter 
2528a10aa579SChristoph Lameter 	if (v->flags & VM_USERMAP)
2529a10aa579SChristoph Lameter 		seq_printf(m, " user");
2530a10aa579SChristoph Lameter 
2531a10aa579SChristoph Lameter 	if (v->flags & VM_VPAGES)
2532a10aa579SChristoph Lameter 		seq_printf(m, " vpages");
2533a10aa579SChristoph Lameter 
2534a47a126aSEric Dumazet 	show_numa_info(m, v);
2535a10aa579SChristoph Lameter 	seq_putc(m, '\n');
2536a10aa579SChristoph Lameter 	return 0;
2537a10aa579SChristoph Lameter }
2538a10aa579SChristoph Lameter 
25395f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = {
2540a10aa579SChristoph Lameter 	.start = s_start,
2541a10aa579SChristoph Lameter 	.next = s_next,
2542a10aa579SChristoph Lameter 	.stop = s_stop,
2543a10aa579SChristoph Lameter 	.show = s_show,
2544a10aa579SChristoph Lameter };
25455f6a6a9cSAlexey Dobriyan 
25465f6a6a9cSAlexey Dobriyan static int vmalloc_open(struct inode *inode, struct file *file)
25475f6a6a9cSAlexey Dobriyan {
25485f6a6a9cSAlexey Dobriyan 	unsigned int *ptr = NULL;
25495f6a6a9cSAlexey Dobriyan 	int ret;
25505f6a6a9cSAlexey Dobriyan 
255151980ac9SKulikov Vasiliy 	if (NUMA_BUILD) {
25525f6a6a9cSAlexey Dobriyan 		ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
255351980ac9SKulikov Vasiliy 		if (ptr == NULL)
255451980ac9SKulikov Vasiliy 			return -ENOMEM;
255551980ac9SKulikov Vasiliy 	}
25565f6a6a9cSAlexey Dobriyan 	ret = seq_open(file, &vmalloc_op);
25575f6a6a9cSAlexey Dobriyan 	if (!ret) {
25585f6a6a9cSAlexey Dobriyan 		struct seq_file *m = file->private_data;
25595f6a6a9cSAlexey Dobriyan 		m->private = ptr;
25605f6a6a9cSAlexey Dobriyan 	} else
25615f6a6a9cSAlexey Dobriyan 		kfree(ptr);
25625f6a6a9cSAlexey Dobriyan 	return ret;
25635f6a6a9cSAlexey Dobriyan }
25645f6a6a9cSAlexey Dobriyan 
25655f6a6a9cSAlexey Dobriyan static const struct file_operations proc_vmalloc_operations = {
25665f6a6a9cSAlexey Dobriyan 	.open		= vmalloc_open,
25675f6a6a9cSAlexey Dobriyan 	.read		= seq_read,
25685f6a6a9cSAlexey Dobriyan 	.llseek		= seq_lseek,
25695f6a6a9cSAlexey Dobriyan 	.release	= seq_release_private,
25705f6a6a9cSAlexey Dobriyan };
25715f6a6a9cSAlexey Dobriyan 
25725f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void)
25735f6a6a9cSAlexey Dobriyan {
25745f6a6a9cSAlexey Dobriyan 	proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
25755f6a6a9cSAlexey Dobriyan 	return 0;
25765f6a6a9cSAlexey Dobriyan }
25775f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init);
2578a10aa579SChristoph Lameter #endif
2579a10aa579SChristoph Lameter 
2580