xref: /linux/mm/vmalloc.c (revision f011c2dae6cffc50ef67d9bd937b488ba5db8913)
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>
151da177e4SLinus Torvalds #include <linux/slab.h>
161da177e4SLinus Torvalds #include <linux/spinlock.h>
171da177e4SLinus Torvalds #include <linux/interrupt.h>
185f6a6a9cSAlexey Dobriyan #include <linux/proc_fs.h>
19a10aa579SChristoph Lameter #include <linux/seq_file.h>
203ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
2123016969SChristoph Lameter #include <linux/kallsyms.h>
22db64fe02SNick Piggin #include <linux/list.h>
23db64fe02SNick Piggin #include <linux/rbtree.h>
24db64fe02SNick Piggin #include <linux/radix-tree.h>
25db64fe02SNick Piggin #include <linux/rcupdate.h>
261da177e4SLinus Torvalds 
27db64fe02SNick Piggin #include <asm/atomic.h>
281da177e4SLinus Torvalds #include <asm/uaccess.h>
291da177e4SLinus Torvalds #include <asm/tlbflush.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds 
32db64fe02SNick Piggin /*** Page table manipulation functions ***/
33b221385bSAdrian Bunk 
341da177e4SLinus Torvalds static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
351da177e4SLinus Torvalds {
361da177e4SLinus Torvalds 	pte_t *pte;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 	pte = pte_offset_kernel(pmd, addr);
391da177e4SLinus Torvalds 	do {
401da177e4SLinus Torvalds 		pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
411da177e4SLinus Torvalds 		WARN_ON(!pte_none(ptent) && !pte_present(ptent));
421da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
431da177e4SLinus Torvalds }
441da177e4SLinus Torvalds 
45db64fe02SNick Piggin static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds 	pmd_t *pmd;
481da177e4SLinus Torvalds 	unsigned long next;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
511da177e4SLinus Torvalds 	do {
521da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
531da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
541da177e4SLinus Torvalds 			continue;
551da177e4SLinus Torvalds 		vunmap_pte_range(pmd, addr, next);
561da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
59db64fe02SNick Piggin static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds 	pud_t *pud;
621da177e4SLinus Torvalds 	unsigned long next;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
651da177e4SLinus Torvalds 	do {
661da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
671da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
681da177e4SLinus Torvalds 			continue;
691da177e4SLinus Torvalds 		vunmap_pmd_range(pud, addr, next);
701da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
711da177e4SLinus Torvalds }
721da177e4SLinus Torvalds 
73db64fe02SNick Piggin static void vunmap_page_range(unsigned long addr, unsigned long end)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	pgd_t *pgd;
761da177e4SLinus Torvalds 	unsigned long next;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	BUG_ON(addr >= end);
791da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
801da177e4SLinus Torvalds 	flush_cache_vunmap(addr, end);
811da177e4SLinus Torvalds 	do {
821da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
831da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
841da177e4SLinus Torvalds 			continue;
851da177e4SLinus Torvalds 		vunmap_pud_range(pgd, addr, next);
861da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
90db64fe02SNick Piggin 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	pte_t *pte;
931da177e4SLinus Torvalds 
94db64fe02SNick Piggin 	/*
95db64fe02SNick Piggin 	 * nr is a running index into the array which helps higher level
96db64fe02SNick Piggin 	 * callers keep track of where we're up to.
97db64fe02SNick Piggin 	 */
98db64fe02SNick Piggin 
99872fec16SHugh Dickins 	pte = pte_alloc_kernel(pmd, addr);
1001da177e4SLinus Torvalds 	if (!pte)
1011da177e4SLinus Torvalds 		return -ENOMEM;
1021da177e4SLinus Torvalds 	do {
103db64fe02SNick Piggin 		struct page *page = pages[*nr];
104db64fe02SNick Piggin 
105db64fe02SNick Piggin 		if (WARN_ON(!pte_none(*pte)))
106db64fe02SNick Piggin 			return -EBUSY;
107db64fe02SNick Piggin 		if (WARN_ON(!page))
1081da177e4SLinus Torvalds 			return -ENOMEM;
1091da177e4SLinus Torvalds 		set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
110db64fe02SNick Piggin 		(*nr)++;
1111da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1121da177e4SLinus Torvalds 	return 0;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
115db64fe02SNick Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr,
116db64fe02SNick Piggin 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	pmd_t *pmd;
1191da177e4SLinus Torvalds 	unsigned long next;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	pmd = pmd_alloc(&init_mm, pud, addr);
1221da177e4SLinus Torvalds 	if (!pmd)
1231da177e4SLinus Torvalds 		return -ENOMEM;
1241da177e4SLinus Torvalds 	do {
1251da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
126db64fe02SNick Piggin 		if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
1271da177e4SLinus Torvalds 			return -ENOMEM;
1281da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
1291da177e4SLinus Torvalds 	return 0;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
132db64fe02SNick Piggin static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
133db64fe02SNick Piggin 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	pud_t *pud;
1361da177e4SLinus Torvalds 	unsigned long next;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	pud = pud_alloc(&init_mm, pgd, addr);
1391da177e4SLinus Torvalds 	if (!pud)
1401da177e4SLinus Torvalds 		return -ENOMEM;
1411da177e4SLinus Torvalds 	do {
1421da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
143db64fe02SNick Piggin 		if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
1441da177e4SLinus Torvalds 			return -ENOMEM;
1451da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
1461da177e4SLinus Torvalds 	return 0;
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
149db64fe02SNick Piggin /*
150db64fe02SNick Piggin  * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
151db64fe02SNick Piggin  * will have pfns corresponding to the "pages" array.
152db64fe02SNick Piggin  *
153db64fe02SNick Piggin  * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
154db64fe02SNick Piggin  */
155db64fe02SNick Piggin static int vmap_page_range(unsigned long addr, unsigned long end,
156db64fe02SNick Piggin 				pgprot_t prot, struct page **pages)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	pgd_t *pgd;
1591da177e4SLinus Torvalds 	unsigned long next;
160db64fe02SNick Piggin 	int err = 0;
161db64fe02SNick Piggin 	int nr = 0;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1641da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
1651da177e4SLinus Torvalds 	do {
1661da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
167db64fe02SNick Piggin 		err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
1681da177e4SLinus Torvalds 		if (err)
1691da177e4SLinus Torvalds 			break;
1701da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
171db64fe02SNick Piggin 	flush_cache_vmap(addr, end);
172db64fe02SNick Piggin 
173db64fe02SNick Piggin 	if (unlikely(err))
1741da177e4SLinus Torvalds 		return err;
175db64fe02SNick Piggin 	return nr;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
17873bdf0a6SLinus Torvalds static inline int is_vmalloc_or_module_addr(const void *x)
17973bdf0a6SLinus Torvalds {
18073bdf0a6SLinus Torvalds 	/*
181ab4f2ee1SRussell King 	 * ARM, x86-64 and sparc64 put modules in a special place,
18273bdf0a6SLinus Torvalds 	 * and fall back on vmalloc() if that fails. Others
18373bdf0a6SLinus Torvalds 	 * just put it in the vmalloc space.
18473bdf0a6SLinus Torvalds 	 */
18573bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
18673bdf0a6SLinus Torvalds 	unsigned long addr = (unsigned long)x;
18773bdf0a6SLinus Torvalds 	if (addr >= MODULES_VADDR && addr < MODULES_END)
18873bdf0a6SLinus Torvalds 		return 1;
18973bdf0a6SLinus Torvalds #endif
19073bdf0a6SLinus Torvalds 	return is_vmalloc_addr(x);
19173bdf0a6SLinus Torvalds }
19273bdf0a6SLinus Torvalds 
19348667e7aSChristoph Lameter /*
194db64fe02SNick Piggin  * Walk a vmap address to the struct page it maps.
19548667e7aSChristoph Lameter  */
196b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *vmalloc_addr)
19748667e7aSChristoph Lameter {
19848667e7aSChristoph Lameter 	unsigned long addr = (unsigned long) vmalloc_addr;
19948667e7aSChristoph Lameter 	struct page *page = NULL;
20048667e7aSChristoph Lameter 	pgd_t *pgd = pgd_offset_k(addr);
20148667e7aSChristoph Lameter 
2027aa413deSIngo Molnar 	/*
2037aa413deSIngo Molnar 	 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
2047aa413deSIngo Molnar 	 * architectures that do not vmalloc module space
2057aa413deSIngo Molnar 	 */
20673bdf0a6SLinus Torvalds 	VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
20759ea7463SJiri Slaby 
20848667e7aSChristoph Lameter 	if (!pgd_none(*pgd)) {
209db64fe02SNick Piggin 		pud_t *pud = pud_offset(pgd, addr);
21048667e7aSChristoph Lameter 		if (!pud_none(*pud)) {
211db64fe02SNick Piggin 			pmd_t *pmd = pmd_offset(pud, addr);
21248667e7aSChristoph Lameter 			if (!pmd_none(*pmd)) {
213db64fe02SNick Piggin 				pte_t *ptep, pte;
214db64fe02SNick Piggin 
21548667e7aSChristoph Lameter 				ptep = pte_offset_map(pmd, addr);
21648667e7aSChristoph Lameter 				pte = *ptep;
21748667e7aSChristoph Lameter 				if (pte_present(pte))
21848667e7aSChristoph Lameter 					page = pte_page(pte);
21948667e7aSChristoph Lameter 				pte_unmap(ptep);
22048667e7aSChristoph Lameter 			}
22148667e7aSChristoph Lameter 		}
22248667e7aSChristoph Lameter 	}
22348667e7aSChristoph Lameter 	return page;
22448667e7aSChristoph Lameter }
22548667e7aSChristoph Lameter EXPORT_SYMBOL(vmalloc_to_page);
22648667e7aSChristoph Lameter 
22748667e7aSChristoph Lameter /*
22848667e7aSChristoph Lameter  * Map a vmalloc()-space virtual address to the physical page frame number.
22948667e7aSChristoph Lameter  */
230b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
23148667e7aSChristoph Lameter {
23248667e7aSChristoph Lameter 	return page_to_pfn(vmalloc_to_page(vmalloc_addr));
23348667e7aSChristoph Lameter }
23448667e7aSChristoph Lameter EXPORT_SYMBOL(vmalloc_to_pfn);
23548667e7aSChristoph Lameter 
236db64fe02SNick Piggin 
237db64fe02SNick Piggin /*** Global kva allocator ***/
238db64fe02SNick Piggin 
239db64fe02SNick Piggin #define VM_LAZY_FREE	0x01
240db64fe02SNick Piggin #define VM_LAZY_FREEING	0x02
241db64fe02SNick Piggin #define VM_VM_AREA	0x04
242db64fe02SNick Piggin 
243db64fe02SNick Piggin struct vmap_area {
244db64fe02SNick Piggin 	unsigned long va_start;
245db64fe02SNick Piggin 	unsigned long va_end;
246db64fe02SNick Piggin 	unsigned long flags;
247db64fe02SNick Piggin 	struct rb_node rb_node;		/* address sorted rbtree */
248db64fe02SNick Piggin 	struct list_head list;		/* address sorted list */
249db64fe02SNick Piggin 	struct list_head purge_list;	/* "lazy purge" list */
250db64fe02SNick Piggin 	void *private;
251db64fe02SNick Piggin 	struct rcu_head rcu_head;
252db64fe02SNick Piggin };
253db64fe02SNick Piggin 
254db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock);
255db64fe02SNick Piggin static struct rb_root vmap_area_root = RB_ROOT;
256db64fe02SNick Piggin static LIST_HEAD(vmap_area_list);
257db64fe02SNick Piggin 
258db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr)
2591da177e4SLinus Torvalds {
260db64fe02SNick Piggin 	struct rb_node *n = vmap_area_root.rb_node;
261db64fe02SNick Piggin 
262db64fe02SNick Piggin 	while (n) {
263db64fe02SNick Piggin 		struct vmap_area *va;
264db64fe02SNick Piggin 
265db64fe02SNick Piggin 		va = rb_entry(n, struct vmap_area, rb_node);
266db64fe02SNick Piggin 		if (addr < va->va_start)
267db64fe02SNick Piggin 			n = n->rb_left;
268db64fe02SNick Piggin 		else if (addr > va->va_start)
269db64fe02SNick Piggin 			n = n->rb_right;
270db64fe02SNick Piggin 		else
271db64fe02SNick Piggin 			return va;
272db64fe02SNick Piggin 	}
273db64fe02SNick Piggin 
274db64fe02SNick Piggin 	return NULL;
275db64fe02SNick Piggin }
276db64fe02SNick Piggin 
277db64fe02SNick Piggin static void __insert_vmap_area(struct vmap_area *va)
278db64fe02SNick Piggin {
279db64fe02SNick Piggin 	struct rb_node **p = &vmap_area_root.rb_node;
280db64fe02SNick Piggin 	struct rb_node *parent = NULL;
281db64fe02SNick Piggin 	struct rb_node *tmp;
282db64fe02SNick Piggin 
283db64fe02SNick Piggin 	while (*p) {
284db64fe02SNick Piggin 		struct vmap_area *tmp;
285db64fe02SNick Piggin 
286db64fe02SNick Piggin 		parent = *p;
287db64fe02SNick Piggin 		tmp = rb_entry(parent, struct vmap_area, rb_node);
288db64fe02SNick Piggin 		if (va->va_start < tmp->va_end)
289db64fe02SNick Piggin 			p = &(*p)->rb_left;
290db64fe02SNick Piggin 		else if (va->va_end > tmp->va_start)
291db64fe02SNick Piggin 			p = &(*p)->rb_right;
292db64fe02SNick Piggin 		else
293db64fe02SNick Piggin 			BUG();
294db64fe02SNick Piggin 	}
295db64fe02SNick Piggin 
296db64fe02SNick Piggin 	rb_link_node(&va->rb_node, parent, p);
297db64fe02SNick Piggin 	rb_insert_color(&va->rb_node, &vmap_area_root);
298db64fe02SNick Piggin 
299db64fe02SNick Piggin 	/* address-sort this list so it is usable like the vmlist */
300db64fe02SNick Piggin 	tmp = rb_prev(&va->rb_node);
301db64fe02SNick Piggin 	if (tmp) {
302db64fe02SNick Piggin 		struct vmap_area *prev;
303db64fe02SNick Piggin 		prev = rb_entry(tmp, struct vmap_area, rb_node);
304db64fe02SNick Piggin 		list_add_rcu(&va->list, &prev->list);
305db64fe02SNick Piggin 	} else
306db64fe02SNick Piggin 		list_add_rcu(&va->list, &vmap_area_list);
307db64fe02SNick Piggin }
308db64fe02SNick Piggin 
309db64fe02SNick Piggin static void purge_vmap_area_lazy(void);
310db64fe02SNick Piggin 
311db64fe02SNick Piggin /*
312db64fe02SNick Piggin  * Allocate a region of KVA of the specified size and alignment, within the
313db64fe02SNick Piggin  * vstart and vend.
314db64fe02SNick Piggin  */
315db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size,
316db64fe02SNick Piggin 				unsigned long align,
317db64fe02SNick Piggin 				unsigned long vstart, unsigned long vend,
318db64fe02SNick Piggin 				int node, gfp_t gfp_mask)
319db64fe02SNick Piggin {
320db64fe02SNick Piggin 	struct vmap_area *va;
321db64fe02SNick Piggin 	struct rb_node *n;
3221da177e4SLinus Torvalds 	unsigned long addr;
323db64fe02SNick Piggin 	int purged = 0;
324db64fe02SNick Piggin 
325db64fe02SNick Piggin 	BUG_ON(size & ~PAGE_MASK);
326db64fe02SNick Piggin 
327db64fe02SNick Piggin 	addr = ALIGN(vstart, align);
328db64fe02SNick Piggin 
329db64fe02SNick Piggin 	va = kmalloc_node(sizeof(struct vmap_area),
330db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
331db64fe02SNick Piggin 	if (unlikely(!va))
332db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
333db64fe02SNick Piggin 
334db64fe02SNick Piggin retry:
335db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
336db64fe02SNick Piggin 	/* XXX: could have a last_hole cache */
337db64fe02SNick Piggin 	n = vmap_area_root.rb_node;
338db64fe02SNick Piggin 	if (n) {
339db64fe02SNick Piggin 		struct vmap_area *first = NULL;
340db64fe02SNick Piggin 
341db64fe02SNick Piggin 		do {
342db64fe02SNick Piggin 			struct vmap_area *tmp;
343db64fe02SNick Piggin 			tmp = rb_entry(n, struct vmap_area, rb_node);
344db64fe02SNick Piggin 			if (tmp->va_end >= addr) {
345db64fe02SNick Piggin 				if (!first && tmp->va_start < addr + size)
346db64fe02SNick Piggin 					first = tmp;
347db64fe02SNick Piggin 				n = n->rb_left;
348db64fe02SNick Piggin 			} else {
349db64fe02SNick Piggin 				first = tmp;
350db64fe02SNick Piggin 				n = n->rb_right;
351db64fe02SNick Piggin 			}
352db64fe02SNick Piggin 		} while (n);
353db64fe02SNick Piggin 
354db64fe02SNick Piggin 		if (!first)
355db64fe02SNick Piggin 			goto found;
356db64fe02SNick Piggin 
357db64fe02SNick Piggin 		if (first->va_end < addr) {
358db64fe02SNick Piggin 			n = rb_next(&first->rb_node);
359db64fe02SNick Piggin 			if (n)
360db64fe02SNick Piggin 				first = rb_entry(n, struct vmap_area, rb_node);
361db64fe02SNick Piggin 			else
362db64fe02SNick Piggin 				goto found;
363db64fe02SNick Piggin 		}
364db64fe02SNick Piggin 
365*f011c2daSNick Piggin 		while (addr + size > first->va_start && addr + size <= vend) {
366db64fe02SNick Piggin 			addr = ALIGN(first->va_end + PAGE_SIZE, align);
367db64fe02SNick Piggin 
368db64fe02SNick Piggin 			n = rb_next(&first->rb_node);
369db64fe02SNick Piggin 			if (n)
370db64fe02SNick Piggin 				first = rb_entry(n, struct vmap_area, rb_node);
371db64fe02SNick Piggin 			else
372db64fe02SNick Piggin 				goto found;
373db64fe02SNick Piggin 		}
374db64fe02SNick Piggin 	}
375db64fe02SNick Piggin found:
376db64fe02SNick Piggin 	if (addr + size > vend) {
377db64fe02SNick Piggin 		spin_unlock(&vmap_area_lock);
378db64fe02SNick Piggin 		if (!purged) {
379db64fe02SNick Piggin 			purge_vmap_area_lazy();
380db64fe02SNick Piggin 			purged = 1;
381db64fe02SNick Piggin 			goto retry;
382db64fe02SNick Piggin 		}
383db64fe02SNick Piggin 		if (printk_ratelimit())
384db64fe02SNick Piggin 			printk(KERN_WARNING "vmap allocation failed: "
385db64fe02SNick Piggin 				 "use vmalloc=<size> to increase size.\n");
386db64fe02SNick Piggin 		return ERR_PTR(-EBUSY);
387db64fe02SNick Piggin 	}
388db64fe02SNick Piggin 
389db64fe02SNick Piggin 	BUG_ON(addr & (align-1));
390db64fe02SNick Piggin 
391db64fe02SNick Piggin 	va->va_start = addr;
392db64fe02SNick Piggin 	va->va_end = addr + size;
393db64fe02SNick Piggin 	va->flags = 0;
394db64fe02SNick Piggin 	__insert_vmap_area(va);
395db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
396db64fe02SNick Piggin 
397db64fe02SNick Piggin 	return va;
398db64fe02SNick Piggin }
399db64fe02SNick Piggin 
400db64fe02SNick Piggin static void rcu_free_va(struct rcu_head *head)
401db64fe02SNick Piggin {
402db64fe02SNick Piggin 	struct vmap_area *va = container_of(head, struct vmap_area, rcu_head);
403db64fe02SNick Piggin 
404db64fe02SNick Piggin 	kfree(va);
405db64fe02SNick Piggin }
406db64fe02SNick Piggin 
407db64fe02SNick Piggin static void __free_vmap_area(struct vmap_area *va)
408db64fe02SNick Piggin {
409db64fe02SNick Piggin 	BUG_ON(RB_EMPTY_NODE(&va->rb_node));
410db64fe02SNick Piggin 	rb_erase(&va->rb_node, &vmap_area_root);
411db64fe02SNick Piggin 	RB_CLEAR_NODE(&va->rb_node);
412db64fe02SNick Piggin 	list_del_rcu(&va->list);
413db64fe02SNick Piggin 
414db64fe02SNick Piggin 	call_rcu(&va->rcu_head, rcu_free_va);
415db64fe02SNick Piggin }
416db64fe02SNick Piggin 
417db64fe02SNick Piggin /*
418db64fe02SNick Piggin  * Free a region of KVA allocated by alloc_vmap_area
419db64fe02SNick Piggin  */
420db64fe02SNick Piggin static void free_vmap_area(struct vmap_area *va)
421db64fe02SNick Piggin {
422db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
423db64fe02SNick Piggin 	__free_vmap_area(va);
424db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
425db64fe02SNick Piggin }
426db64fe02SNick Piggin 
427db64fe02SNick Piggin /*
428db64fe02SNick Piggin  * Clear the pagetable entries of a given vmap_area
429db64fe02SNick Piggin  */
430db64fe02SNick Piggin static void unmap_vmap_area(struct vmap_area *va)
431db64fe02SNick Piggin {
432db64fe02SNick Piggin 	vunmap_page_range(va->va_start, va->va_end);
433db64fe02SNick Piggin }
434db64fe02SNick Piggin 
435db64fe02SNick Piggin /*
436db64fe02SNick Piggin  * lazy_max_pages is the maximum amount of virtual address space we gather up
437db64fe02SNick Piggin  * before attempting to purge with a TLB flush.
438db64fe02SNick Piggin  *
439db64fe02SNick Piggin  * There is a tradeoff here: a larger number will cover more kernel page tables
440db64fe02SNick Piggin  * and take slightly longer to purge, but it will linearly reduce the number of
441db64fe02SNick Piggin  * global TLB flushes that must be performed. It would seem natural to scale
442db64fe02SNick Piggin  * this number up linearly with the number of CPUs (because vmapping activity
443db64fe02SNick Piggin  * could also scale linearly with the number of CPUs), however it is likely
444db64fe02SNick Piggin  * that in practice, workloads might be constrained in other ways that mean
445db64fe02SNick Piggin  * vmap activity will not scale linearly with CPUs. Also, I want to be
446db64fe02SNick Piggin  * conservative and not introduce a big latency on huge systems, so go with
447db64fe02SNick Piggin  * a less aggressive log scale. It will still be an improvement over the old
448db64fe02SNick Piggin  * code, and it will be simple to change the scale factor if we find that it
449db64fe02SNick Piggin  * becomes a problem on bigger systems.
450db64fe02SNick Piggin  */
451db64fe02SNick Piggin static unsigned long lazy_max_pages(void)
452db64fe02SNick Piggin {
453db64fe02SNick Piggin 	unsigned int log;
454db64fe02SNick Piggin 
455db64fe02SNick Piggin 	log = fls(num_online_cpus());
456db64fe02SNick Piggin 
457db64fe02SNick Piggin 	return log * (32UL * 1024 * 1024 / PAGE_SIZE);
458db64fe02SNick Piggin }
459db64fe02SNick Piggin 
460db64fe02SNick Piggin static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
461db64fe02SNick Piggin 
462db64fe02SNick Piggin /*
463db64fe02SNick Piggin  * Purges all lazily-freed vmap areas.
464db64fe02SNick Piggin  *
465db64fe02SNick Piggin  * If sync is 0 then don't purge if there is already a purge in progress.
466db64fe02SNick Piggin  * If force_flush is 1, then flush kernel TLBs between *start and *end even
467db64fe02SNick Piggin  * if we found no lazy vmap areas to unmap (callers can use this to optimise
468db64fe02SNick Piggin  * their own TLB flushing).
469db64fe02SNick Piggin  * Returns with *start = min(*start, lowest purged address)
470db64fe02SNick Piggin  *              *end = max(*end, highest purged address)
471db64fe02SNick Piggin  */
472db64fe02SNick Piggin static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
473db64fe02SNick Piggin 					int sync, int force_flush)
474db64fe02SNick Piggin {
475db64fe02SNick Piggin 	static DEFINE_SPINLOCK(purge_lock);
476db64fe02SNick Piggin 	LIST_HEAD(valist);
477db64fe02SNick Piggin 	struct vmap_area *va;
478db64fe02SNick Piggin 	int nr = 0;
479db64fe02SNick Piggin 
480db64fe02SNick Piggin 	/*
481db64fe02SNick Piggin 	 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
482db64fe02SNick Piggin 	 * should not expect such behaviour. This just simplifies locking for
483db64fe02SNick Piggin 	 * the case that isn't actually used at the moment anyway.
484db64fe02SNick Piggin 	 */
485db64fe02SNick Piggin 	if (!sync && !force_flush) {
486db64fe02SNick Piggin 		if (!spin_trylock(&purge_lock))
487db64fe02SNick Piggin 			return;
488db64fe02SNick Piggin 	} else
489db64fe02SNick Piggin 		spin_lock(&purge_lock);
490db64fe02SNick Piggin 
491db64fe02SNick Piggin 	rcu_read_lock();
492db64fe02SNick Piggin 	list_for_each_entry_rcu(va, &vmap_area_list, list) {
493db64fe02SNick Piggin 		if (va->flags & VM_LAZY_FREE) {
494db64fe02SNick Piggin 			if (va->va_start < *start)
495db64fe02SNick Piggin 				*start = va->va_start;
496db64fe02SNick Piggin 			if (va->va_end > *end)
497db64fe02SNick Piggin 				*end = va->va_end;
498db64fe02SNick Piggin 			nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
499db64fe02SNick Piggin 			unmap_vmap_area(va);
500db64fe02SNick Piggin 			list_add_tail(&va->purge_list, &valist);
501db64fe02SNick Piggin 			va->flags |= VM_LAZY_FREEING;
502db64fe02SNick Piggin 			va->flags &= ~VM_LAZY_FREE;
503db64fe02SNick Piggin 		}
504db64fe02SNick Piggin 	}
505db64fe02SNick Piggin 	rcu_read_unlock();
506db64fe02SNick Piggin 
507db64fe02SNick Piggin 	if (nr) {
508db64fe02SNick Piggin 		BUG_ON(nr > atomic_read(&vmap_lazy_nr));
509db64fe02SNick Piggin 		atomic_sub(nr, &vmap_lazy_nr);
510db64fe02SNick Piggin 	}
511db64fe02SNick Piggin 
512db64fe02SNick Piggin 	if (nr || force_flush)
513db64fe02SNick Piggin 		flush_tlb_kernel_range(*start, *end);
514db64fe02SNick Piggin 
515db64fe02SNick Piggin 	if (nr) {
516db64fe02SNick Piggin 		spin_lock(&vmap_area_lock);
517db64fe02SNick Piggin 		list_for_each_entry(va, &valist, purge_list)
518db64fe02SNick Piggin 			__free_vmap_area(va);
519db64fe02SNick Piggin 		spin_unlock(&vmap_area_lock);
520db64fe02SNick Piggin 	}
521db64fe02SNick Piggin 	spin_unlock(&purge_lock);
522db64fe02SNick Piggin }
523db64fe02SNick Piggin 
524db64fe02SNick Piggin /*
525db64fe02SNick Piggin  * Kick off a purge of the outstanding lazy areas.
526db64fe02SNick Piggin  */
527db64fe02SNick Piggin static void purge_vmap_area_lazy(void)
528db64fe02SNick Piggin {
529db64fe02SNick Piggin 	unsigned long start = ULONG_MAX, end = 0;
530db64fe02SNick Piggin 
531db64fe02SNick Piggin 	__purge_vmap_area_lazy(&start, &end, 0, 0);
532db64fe02SNick Piggin }
533db64fe02SNick Piggin 
534db64fe02SNick Piggin /*
535db64fe02SNick Piggin  * Free and unmap a vmap area
536db64fe02SNick Piggin  */
537db64fe02SNick Piggin static void free_unmap_vmap_area(struct vmap_area *va)
538db64fe02SNick Piggin {
539db64fe02SNick Piggin 	va->flags |= VM_LAZY_FREE;
540db64fe02SNick Piggin 	atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
541db64fe02SNick Piggin 	if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
542db64fe02SNick Piggin 		purge_vmap_area_lazy();
543db64fe02SNick Piggin }
544db64fe02SNick Piggin 
545db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr)
546db64fe02SNick Piggin {
547db64fe02SNick Piggin 	struct vmap_area *va;
548db64fe02SNick Piggin 
549db64fe02SNick Piggin 	spin_lock(&vmap_area_lock);
550db64fe02SNick Piggin 	va = __find_vmap_area(addr);
551db64fe02SNick Piggin 	spin_unlock(&vmap_area_lock);
552db64fe02SNick Piggin 
553db64fe02SNick Piggin 	return va;
554db64fe02SNick Piggin }
555db64fe02SNick Piggin 
556db64fe02SNick Piggin static void free_unmap_vmap_area_addr(unsigned long addr)
557db64fe02SNick Piggin {
558db64fe02SNick Piggin 	struct vmap_area *va;
559db64fe02SNick Piggin 
560db64fe02SNick Piggin 	va = find_vmap_area(addr);
561db64fe02SNick Piggin 	BUG_ON(!va);
562db64fe02SNick Piggin 	free_unmap_vmap_area(va);
563db64fe02SNick Piggin }
564db64fe02SNick Piggin 
565db64fe02SNick Piggin 
566db64fe02SNick Piggin /*** Per cpu kva allocator ***/
567db64fe02SNick Piggin 
568db64fe02SNick Piggin /*
569db64fe02SNick Piggin  * vmap space is limited especially on 32 bit architectures. Ensure there is
570db64fe02SNick Piggin  * room for at least 16 percpu vmap blocks per CPU.
571db64fe02SNick Piggin  */
572db64fe02SNick Piggin /*
573db64fe02SNick Piggin  * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
574db64fe02SNick Piggin  * to #define VMALLOC_SPACE		(VMALLOC_END-VMALLOC_START). Guess
575db64fe02SNick Piggin  * instead (we just need a rough idea)
576db64fe02SNick Piggin  */
577db64fe02SNick Piggin #if BITS_PER_LONG == 32
578db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024)
579db64fe02SNick Piggin #else
580db64fe02SNick Piggin #define VMALLOC_SPACE		(128UL*1024*1024*1024)
581db64fe02SNick Piggin #endif
582db64fe02SNick Piggin 
583db64fe02SNick Piggin #define VMALLOC_PAGES		(VMALLOC_SPACE / PAGE_SIZE)
584db64fe02SNick Piggin #define VMAP_MAX_ALLOC		BITS_PER_LONG	/* 256K with 4K pages */
585db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX	1024	/* 4MB with 4K pages */
586db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN	(VMAP_MAX_ALLOC*2)
587db64fe02SNick Piggin #define VMAP_MIN(x, y)		((x) < (y) ? (x) : (y)) /* can't use min() */
588db64fe02SNick Piggin #define VMAP_MAX(x, y)		((x) > (y) ? (x) : (y)) /* can't use max() */
589db64fe02SNick Piggin #define VMAP_BBMAP_BITS		VMAP_MIN(VMAP_BBMAP_BITS_MAX,		\
590db64fe02SNick Piggin 					VMAP_MAX(VMAP_BBMAP_BITS_MIN,	\
591db64fe02SNick Piggin 						VMALLOC_PAGES / NR_CPUS / 16))
592db64fe02SNick Piggin 
593db64fe02SNick Piggin #define VMAP_BLOCK_SIZE		(VMAP_BBMAP_BITS * PAGE_SIZE)
594db64fe02SNick Piggin 
5959b463334SJeremy Fitzhardinge static bool vmap_initialized __read_mostly = false;
5969b463334SJeremy Fitzhardinge 
597db64fe02SNick Piggin struct vmap_block_queue {
598db64fe02SNick Piggin 	spinlock_t lock;
599db64fe02SNick Piggin 	struct list_head free;
600db64fe02SNick Piggin 	struct list_head dirty;
601db64fe02SNick Piggin 	unsigned int nr_dirty;
602db64fe02SNick Piggin };
603db64fe02SNick Piggin 
604db64fe02SNick Piggin struct vmap_block {
605db64fe02SNick Piggin 	spinlock_t lock;
606db64fe02SNick Piggin 	struct vmap_area *va;
607db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
608db64fe02SNick Piggin 	unsigned long free, dirty;
609db64fe02SNick Piggin 	DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
610db64fe02SNick Piggin 	DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
611db64fe02SNick Piggin 	union {
612db64fe02SNick Piggin 		struct {
613db64fe02SNick Piggin 			struct list_head free_list;
614db64fe02SNick Piggin 			struct list_head dirty_list;
615db64fe02SNick Piggin 		};
616db64fe02SNick Piggin 		struct rcu_head rcu_head;
617db64fe02SNick Piggin 	};
618db64fe02SNick Piggin };
619db64fe02SNick Piggin 
620db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
621db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
622db64fe02SNick Piggin 
623db64fe02SNick Piggin /*
624db64fe02SNick Piggin  * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
625db64fe02SNick Piggin  * in the free path. Could get rid of this if we change the API to return a
626db64fe02SNick Piggin  * "cookie" from alloc, to be passed to free. But no big deal yet.
627db64fe02SNick Piggin  */
628db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_block_tree_lock);
629db64fe02SNick Piggin static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
630db64fe02SNick Piggin 
631db64fe02SNick Piggin /*
632db64fe02SNick Piggin  * We should probably have a fallback mechanism to allocate virtual memory
633db64fe02SNick Piggin  * out of partially filled vmap blocks. However vmap block sizing should be
634db64fe02SNick Piggin  * fairly reasonable according to the vmalloc size, so it shouldn't be a
635db64fe02SNick Piggin  * big problem.
636db64fe02SNick Piggin  */
637db64fe02SNick Piggin 
638db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr)
639db64fe02SNick Piggin {
640db64fe02SNick Piggin 	addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
641db64fe02SNick Piggin 	addr /= VMAP_BLOCK_SIZE;
642db64fe02SNick Piggin 	return addr;
643db64fe02SNick Piggin }
644db64fe02SNick Piggin 
645db64fe02SNick Piggin static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
646db64fe02SNick Piggin {
647db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
648db64fe02SNick Piggin 	struct vmap_block *vb;
649db64fe02SNick Piggin 	struct vmap_area *va;
650db64fe02SNick Piggin 	unsigned long vb_idx;
651db64fe02SNick Piggin 	int node, err;
652db64fe02SNick Piggin 
653db64fe02SNick Piggin 	node = numa_node_id();
654db64fe02SNick Piggin 
655db64fe02SNick Piggin 	vb = kmalloc_node(sizeof(struct vmap_block),
656db64fe02SNick Piggin 			gfp_mask & GFP_RECLAIM_MASK, node);
657db64fe02SNick Piggin 	if (unlikely(!vb))
658db64fe02SNick Piggin 		return ERR_PTR(-ENOMEM);
659db64fe02SNick Piggin 
660db64fe02SNick Piggin 	va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
661db64fe02SNick Piggin 					VMALLOC_START, VMALLOC_END,
662db64fe02SNick Piggin 					node, gfp_mask);
663db64fe02SNick Piggin 	if (unlikely(IS_ERR(va))) {
664db64fe02SNick Piggin 		kfree(vb);
665db64fe02SNick Piggin 		return ERR_PTR(PTR_ERR(va));
666db64fe02SNick Piggin 	}
667db64fe02SNick Piggin 
668db64fe02SNick Piggin 	err = radix_tree_preload(gfp_mask);
669db64fe02SNick Piggin 	if (unlikely(err)) {
670db64fe02SNick Piggin 		kfree(vb);
671db64fe02SNick Piggin 		free_vmap_area(va);
672db64fe02SNick Piggin 		return ERR_PTR(err);
673db64fe02SNick Piggin 	}
674db64fe02SNick Piggin 
675db64fe02SNick Piggin 	spin_lock_init(&vb->lock);
676db64fe02SNick Piggin 	vb->va = va;
677db64fe02SNick Piggin 	vb->free = VMAP_BBMAP_BITS;
678db64fe02SNick Piggin 	vb->dirty = 0;
679db64fe02SNick Piggin 	bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
680db64fe02SNick Piggin 	bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
681db64fe02SNick Piggin 	INIT_LIST_HEAD(&vb->free_list);
682db64fe02SNick Piggin 	INIT_LIST_HEAD(&vb->dirty_list);
683db64fe02SNick Piggin 
684db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(va->va_start);
685db64fe02SNick Piggin 	spin_lock(&vmap_block_tree_lock);
686db64fe02SNick Piggin 	err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
687db64fe02SNick Piggin 	spin_unlock(&vmap_block_tree_lock);
688db64fe02SNick Piggin 	BUG_ON(err);
689db64fe02SNick Piggin 	radix_tree_preload_end();
690db64fe02SNick Piggin 
691db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
692db64fe02SNick Piggin 	vb->vbq = vbq;
693db64fe02SNick Piggin 	spin_lock(&vbq->lock);
694db64fe02SNick Piggin 	list_add(&vb->free_list, &vbq->free);
695db64fe02SNick Piggin 	spin_unlock(&vbq->lock);
696db64fe02SNick Piggin 	put_cpu_var(vmap_cpu_blocks);
697db64fe02SNick Piggin 
698db64fe02SNick Piggin 	return vb;
699db64fe02SNick Piggin }
700db64fe02SNick Piggin 
701db64fe02SNick Piggin static void rcu_free_vb(struct rcu_head *head)
702db64fe02SNick Piggin {
703db64fe02SNick Piggin 	struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
704db64fe02SNick Piggin 
705db64fe02SNick Piggin 	kfree(vb);
706db64fe02SNick Piggin }
707db64fe02SNick Piggin 
708db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb)
709db64fe02SNick Piggin {
710db64fe02SNick Piggin 	struct vmap_block *tmp;
711db64fe02SNick Piggin 	unsigned long vb_idx;
712db64fe02SNick Piggin 
713db64fe02SNick Piggin 	spin_lock(&vb->vbq->lock);
714db64fe02SNick Piggin 	if (!list_empty(&vb->free_list))
715db64fe02SNick Piggin 		list_del(&vb->free_list);
716db64fe02SNick Piggin 	if (!list_empty(&vb->dirty_list))
717db64fe02SNick Piggin 		list_del(&vb->dirty_list);
718db64fe02SNick Piggin 	spin_unlock(&vb->vbq->lock);
719db64fe02SNick Piggin 
720db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx(vb->va->va_start);
721db64fe02SNick Piggin 	spin_lock(&vmap_block_tree_lock);
722db64fe02SNick Piggin 	tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
723db64fe02SNick Piggin 	spin_unlock(&vmap_block_tree_lock);
724db64fe02SNick Piggin 	BUG_ON(tmp != vb);
725db64fe02SNick Piggin 
726db64fe02SNick Piggin 	free_unmap_vmap_area(vb->va);
727db64fe02SNick Piggin 	call_rcu(&vb->rcu_head, rcu_free_vb);
728db64fe02SNick Piggin }
729db64fe02SNick Piggin 
730db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
731db64fe02SNick Piggin {
732db64fe02SNick Piggin 	struct vmap_block_queue *vbq;
733db64fe02SNick Piggin 	struct vmap_block *vb;
734db64fe02SNick Piggin 	unsigned long addr = 0;
735db64fe02SNick Piggin 	unsigned int order;
736db64fe02SNick Piggin 
737db64fe02SNick Piggin 	BUG_ON(size & ~PAGE_MASK);
738db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
739db64fe02SNick Piggin 	order = get_order(size);
740db64fe02SNick Piggin 
741db64fe02SNick Piggin again:
742db64fe02SNick Piggin 	rcu_read_lock();
743db64fe02SNick Piggin 	vbq = &get_cpu_var(vmap_block_queue);
744db64fe02SNick Piggin 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
745db64fe02SNick Piggin 		int i;
746db64fe02SNick Piggin 
747db64fe02SNick Piggin 		spin_lock(&vb->lock);
748db64fe02SNick Piggin 		i = bitmap_find_free_region(vb->alloc_map,
749db64fe02SNick Piggin 						VMAP_BBMAP_BITS, order);
750db64fe02SNick Piggin 
751db64fe02SNick Piggin 		if (i >= 0) {
752db64fe02SNick Piggin 			addr = vb->va->va_start + (i << PAGE_SHIFT);
753db64fe02SNick Piggin 			BUG_ON(addr_to_vb_idx(addr) !=
754db64fe02SNick Piggin 					addr_to_vb_idx(vb->va->va_start));
755db64fe02SNick Piggin 			vb->free -= 1UL << order;
756db64fe02SNick Piggin 			if (vb->free == 0) {
757db64fe02SNick Piggin 				spin_lock(&vbq->lock);
758db64fe02SNick Piggin 				list_del_init(&vb->free_list);
759db64fe02SNick Piggin 				spin_unlock(&vbq->lock);
760db64fe02SNick Piggin 			}
761db64fe02SNick Piggin 			spin_unlock(&vb->lock);
762db64fe02SNick Piggin 			break;
763db64fe02SNick Piggin 		}
764db64fe02SNick Piggin 		spin_unlock(&vb->lock);
765db64fe02SNick Piggin 	}
766db64fe02SNick Piggin 	put_cpu_var(vmap_cpu_blocks);
767db64fe02SNick Piggin 	rcu_read_unlock();
768db64fe02SNick Piggin 
769db64fe02SNick Piggin 	if (!addr) {
770db64fe02SNick Piggin 		vb = new_vmap_block(gfp_mask);
771db64fe02SNick Piggin 		if (IS_ERR(vb))
772db64fe02SNick Piggin 			return vb;
773db64fe02SNick Piggin 		goto again;
774db64fe02SNick Piggin 	}
775db64fe02SNick Piggin 
776db64fe02SNick Piggin 	return (void *)addr;
777db64fe02SNick Piggin }
778db64fe02SNick Piggin 
779db64fe02SNick Piggin static void vb_free(const void *addr, unsigned long size)
780db64fe02SNick Piggin {
781db64fe02SNick Piggin 	unsigned long offset;
782db64fe02SNick Piggin 	unsigned long vb_idx;
783db64fe02SNick Piggin 	unsigned int order;
784db64fe02SNick Piggin 	struct vmap_block *vb;
785db64fe02SNick Piggin 
786db64fe02SNick Piggin 	BUG_ON(size & ~PAGE_MASK);
787db64fe02SNick Piggin 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
788db64fe02SNick Piggin 	order = get_order(size);
789db64fe02SNick Piggin 
790db64fe02SNick Piggin 	offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
791db64fe02SNick Piggin 
792db64fe02SNick Piggin 	vb_idx = addr_to_vb_idx((unsigned long)addr);
793db64fe02SNick Piggin 	rcu_read_lock();
794db64fe02SNick Piggin 	vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
795db64fe02SNick Piggin 	rcu_read_unlock();
796db64fe02SNick Piggin 	BUG_ON(!vb);
797db64fe02SNick Piggin 
798db64fe02SNick Piggin 	spin_lock(&vb->lock);
799db64fe02SNick Piggin 	bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
800db64fe02SNick Piggin 	if (!vb->dirty) {
801db64fe02SNick Piggin 		spin_lock(&vb->vbq->lock);
802db64fe02SNick Piggin 		list_add(&vb->dirty_list, &vb->vbq->dirty);
803db64fe02SNick Piggin 		spin_unlock(&vb->vbq->lock);
804db64fe02SNick Piggin 	}
805db64fe02SNick Piggin 	vb->dirty += 1UL << order;
806db64fe02SNick Piggin 	if (vb->dirty == VMAP_BBMAP_BITS) {
807db64fe02SNick Piggin 		BUG_ON(vb->free || !list_empty(&vb->free_list));
808db64fe02SNick Piggin 		spin_unlock(&vb->lock);
809db64fe02SNick Piggin 		free_vmap_block(vb);
810db64fe02SNick Piggin 	} else
811db64fe02SNick Piggin 		spin_unlock(&vb->lock);
812db64fe02SNick Piggin }
813db64fe02SNick Piggin 
814db64fe02SNick Piggin /**
815db64fe02SNick Piggin  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
816db64fe02SNick Piggin  *
817db64fe02SNick Piggin  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
818db64fe02SNick Piggin  * to amortize TLB flushing overheads. What this means is that any page you
819db64fe02SNick Piggin  * have now, may, in a former life, have been mapped into kernel virtual
820db64fe02SNick Piggin  * address by the vmap layer and so there might be some CPUs with TLB entries
821db64fe02SNick Piggin  * still referencing that page (additional to the regular 1:1 kernel mapping).
822db64fe02SNick Piggin  *
823db64fe02SNick Piggin  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
824db64fe02SNick Piggin  * be sure that none of the pages we have control over will have any aliases
825db64fe02SNick Piggin  * from the vmap layer.
826db64fe02SNick Piggin  */
827db64fe02SNick Piggin void vm_unmap_aliases(void)
828db64fe02SNick Piggin {
829db64fe02SNick Piggin 	unsigned long start = ULONG_MAX, end = 0;
830db64fe02SNick Piggin 	int cpu;
831db64fe02SNick Piggin 	int flush = 0;
832db64fe02SNick Piggin 
8339b463334SJeremy Fitzhardinge 	if (unlikely(!vmap_initialized))
8349b463334SJeremy Fitzhardinge 		return;
8359b463334SJeremy Fitzhardinge 
836db64fe02SNick Piggin 	for_each_possible_cpu(cpu) {
837db64fe02SNick Piggin 		struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
838db64fe02SNick Piggin 		struct vmap_block *vb;
839db64fe02SNick Piggin 
840db64fe02SNick Piggin 		rcu_read_lock();
841db64fe02SNick Piggin 		list_for_each_entry_rcu(vb, &vbq->free, free_list) {
842db64fe02SNick Piggin 			int i;
843db64fe02SNick Piggin 
844db64fe02SNick Piggin 			spin_lock(&vb->lock);
845db64fe02SNick Piggin 			i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
846db64fe02SNick Piggin 			while (i < VMAP_BBMAP_BITS) {
847db64fe02SNick Piggin 				unsigned long s, e;
848db64fe02SNick Piggin 				int j;
849db64fe02SNick Piggin 				j = find_next_zero_bit(vb->dirty_map,
850db64fe02SNick Piggin 					VMAP_BBMAP_BITS, i);
851db64fe02SNick Piggin 
852db64fe02SNick Piggin 				s = vb->va->va_start + (i << PAGE_SHIFT);
853db64fe02SNick Piggin 				e = vb->va->va_start + (j << PAGE_SHIFT);
854db64fe02SNick Piggin 				vunmap_page_range(s, e);
855db64fe02SNick Piggin 				flush = 1;
856db64fe02SNick Piggin 
857db64fe02SNick Piggin 				if (s < start)
858db64fe02SNick Piggin 					start = s;
859db64fe02SNick Piggin 				if (e > end)
860db64fe02SNick Piggin 					end = e;
861db64fe02SNick Piggin 
862db64fe02SNick Piggin 				i = j;
863db64fe02SNick Piggin 				i = find_next_bit(vb->dirty_map,
864db64fe02SNick Piggin 							VMAP_BBMAP_BITS, i);
865db64fe02SNick Piggin 			}
866db64fe02SNick Piggin 			spin_unlock(&vb->lock);
867db64fe02SNick Piggin 		}
868db64fe02SNick Piggin 		rcu_read_unlock();
869db64fe02SNick Piggin 	}
870db64fe02SNick Piggin 
871db64fe02SNick Piggin 	__purge_vmap_area_lazy(&start, &end, 1, flush);
872db64fe02SNick Piggin }
873db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases);
874db64fe02SNick Piggin 
875db64fe02SNick Piggin /**
876db64fe02SNick Piggin  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
877db64fe02SNick Piggin  * @mem: the pointer returned by vm_map_ram
878db64fe02SNick Piggin  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
879db64fe02SNick Piggin  */
880db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count)
881db64fe02SNick Piggin {
882db64fe02SNick Piggin 	unsigned long size = count << PAGE_SHIFT;
883db64fe02SNick Piggin 	unsigned long addr = (unsigned long)mem;
884db64fe02SNick Piggin 
885db64fe02SNick Piggin 	BUG_ON(!addr);
886db64fe02SNick Piggin 	BUG_ON(addr < VMALLOC_START);
887db64fe02SNick Piggin 	BUG_ON(addr > VMALLOC_END);
888db64fe02SNick Piggin 	BUG_ON(addr & (PAGE_SIZE-1));
889db64fe02SNick Piggin 
890db64fe02SNick Piggin 	debug_check_no_locks_freed(mem, size);
891db64fe02SNick Piggin 
892db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC))
893db64fe02SNick Piggin 		vb_free(mem, size);
894db64fe02SNick Piggin 	else
895db64fe02SNick Piggin 		free_unmap_vmap_area_addr(addr);
896db64fe02SNick Piggin }
897db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram);
898db64fe02SNick Piggin 
899db64fe02SNick Piggin /**
900db64fe02SNick Piggin  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
901db64fe02SNick Piggin  * @pages: an array of pointers to the pages to be mapped
902db64fe02SNick Piggin  * @count: number of pages
903db64fe02SNick Piggin  * @node: prefer to allocate data structures on this node
904db64fe02SNick Piggin  * @prot: memory protection to use. PAGE_KERNEL for regular RAM
905e99c97adSRandy Dunlap  *
906e99c97adSRandy Dunlap  * Returns: a pointer to the address that has been mapped, or %NULL on failure
907db64fe02SNick Piggin  */
908db64fe02SNick Piggin void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
909db64fe02SNick Piggin {
910db64fe02SNick Piggin 	unsigned long size = count << PAGE_SHIFT;
911db64fe02SNick Piggin 	unsigned long addr;
912db64fe02SNick Piggin 	void *mem;
913db64fe02SNick Piggin 
914db64fe02SNick Piggin 	if (likely(count <= VMAP_MAX_ALLOC)) {
915db64fe02SNick Piggin 		mem = vb_alloc(size, GFP_KERNEL);
916db64fe02SNick Piggin 		if (IS_ERR(mem))
917db64fe02SNick Piggin 			return NULL;
918db64fe02SNick Piggin 		addr = (unsigned long)mem;
919db64fe02SNick Piggin 	} else {
920db64fe02SNick Piggin 		struct vmap_area *va;
921db64fe02SNick Piggin 		va = alloc_vmap_area(size, PAGE_SIZE,
922db64fe02SNick Piggin 				VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
923db64fe02SNick Piggin 		if (IS_ERR(va))
924db64fe02SNick Piggin 			return NULL;
925db64fe02SNick Piggin 
926db64fe02SNick Piggin 		addr = va->va_start;
927db64fe02SNick Piggin 		mem = (void *)addr;
928db64fe02SNick Piggin 	}
929db64fe02SNick Piggin 	if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
930db64fe02SNick Piggin 		vm_unmap_ram(mem, count);
931db64fe02SNick Piggin 		return NULL;
932db64fe02SNick Piggin 	}
933db64fe02SNick Piggin 	return mem;
934db64fe02SNick Piggin }
935db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram);
936db64fe02SNick Piggin 
937db64fe02SNick Piggin void __init vmalloc_init(void)
938db64fe02SNick Piggin {
939db64fe02SNick Piggin 	int i;
940db64fe02SNick Piggin 
941db64fe02SNick Piggin 	for_each_possible_cpu(i) {
942db64fe02SNick Piggin 		struct vmap_block_queue *vbq;
943db64fe02SNick Piggin 
944db64fe02SNick Piggin 		vbq = &per_cpu(vmap_block_queue, i);
945db64fe02SNick Piggin 		spin_lock_init(&vbq->lock);
946db64fe02SNick Piggin 		INIT_LIST_HEAD(&vbq->free);
947db64fe02SNick Piggin 		INIT_LIST_HEAD(&vbq->dirty);
948db64fe02SNick Piggin 		vbq->nr_dirty = 0;
949db64fe02SNick Piggin 	}
9509b463334SJeremy Fitzhardinge 
9519b463334SJeremy Fitzhardinge 	vmap_initialized = true;
952db64fe02SNick Piggin }
953db64fe02SNick Piggin 
954db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size)
955db64fe02SNick Piggin {
956db64fe02SNick Piggin 	unsigned long end = addr + size;
957db64fe02SNick Piggin 	vunmap_page_range(addr, end);
958db64fe02SNick Piggin 	flush_tlb_kernel_range(addr, end);
959db64fe02SNick Piggin }
960db64fe02SNick Piggin 
961db64fe02SNick Piggin int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
962db64fe02SNick Piggin {
963db64fe02SNick Piggin 	unsigned long addr = (unsigned long)area->addr;
964db64fe02SNick Piggin 	unsigned long end = addr + area->size - PAGE_SIZE;
965db64fe02SNick Piggin 	int err;
966db64fe02SNick Piggin 
967db64fe02SNick Piggin 	err = vmap_page_range(addr, end, prot, *pages);
968db64fe02SNick Piggin 	if (err > 0) {
969db64fe02SNick Piggin 		*pages += err;
970db64fe02SNick Piggin 		err = 0;
971db64fe02SNick Piggin 	}
972db64fe02SNick Piggin 
973db64fe02SNick Piggin 	return err;
974db64fe02SNick Piggin }
975db64fe02SNick Piggin EXPORT_SYMBOL_GPL(map_vm_area);
976db64fe02SNick Piggin 
977db64fe02SNick Piggin /*** Old vmalloc interfaces ***/
978db64fe02SNick Piggin DEFINE_RWLOCK(vmlist_lock);
979db64fe02SNick Piggin struct vm_struct *vmlist;
980db64fe02SNick Piggin 
981db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size,
982db64fe02SNick Piggin 		unsigned long flags, unsigned long start, unsigned long end,
983db64fe02SNick Piggin 		int node, gfp_t gfp_mask, void *caller)
984db64fe02SNick Piggin {
985db64fe02SNick Piggin 	static struct vmap_area *va;
986db64fe02SNick Piggin 	struct vm_struct *area;
987db64fe02SNick Piggin 	struct vm_struct *tmp, **p;
988db64fe02SNick Piggin 	unsigned long align = 1;
9891da177e4SLinus Torvalds 
99052fd24caSGiridhar Pemmasani 	BUG_ON(in_interrupt());
9911da177e4SLinus Torvalds 	if (flags & VM_IOREMAP) {
9921da177e4SLinus Torvalds 		int bit = fls(size);
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 		if (bit > IOREMAP_MAX_ORDER)
9951da177e4SLinus Torvalds 			bit = IOREMAP_MAX_ORDER;
9961da177e4SLinus Torvalds 		else if (bit < PAGE_SHIFT)
9971da177e4SLinus Torvalds 			bit = PAGE_SHIFT;
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 		align = 1ul << bit;
10001da177e4SLinus Torvalds 	}
1001db64fe02SNick Piggin 
10021da177e4SLinus Torvalds 	size = PAGE_ALIGN(size);
100331be8309SOGAWA Hirofumi 	if (unlikely(!size))
100431be8309SOGAWA Hirofumi 		return NULL;
10051da177e4SLinus Torvalds 
10066cb06229SChristoph Lameter 	area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
10071da177e4SLinus Torvalds 	if (unlikely(!area))
10081da177e4SLinus Torvalds 		return NULL;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	/*
10111da177e4SLinus Torvalds 	 * We always allocate a guard page.
10121da177e4SLinus Torvalds 	 */
10131da177e4SLinus Torvalds 	size += PAGE_SIZE;
10141da177e4SLinus Torvalds 
1015db64fe02SNick Piggin 	va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1016db64fe02SNick Piggin 	if (IS_ERR(va)) {
1017db64fe02SNick Piggin 		kfree(area);
1018db64fe02SNick Piggin 		return NULL;
10191da177e4SLinus Torvalds 	}
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	area->flags = flags;
1022db64fe02SNick Piggin 	area->addr = (void *)va->va_start;
10231da177e4SLinus Torvalds 	area->size = size;
10241da177e4SLinus Torvalds 	area->pages = NULL;
10251da177e4SLinus Torvalds 	area->nr_pages = 0;
10261da177e4SLinus Torvalds 	area->phys_addr = 0;
102723016969SChristoph Lameter 	area->caller = caller;
1028db64fe02SNick Piggin 	va->private = area;
1029db64fe02SNick Piggin 	va->flags |= VM_VM_AREA;
1030db64fe02SNick Piggin 
1031db64fe02SNick Piggin 	write_lock(&vmlist_lock);
1032db64fe02SNick Piggin 	for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1033db64fe02SNick Piggin 		if (tmp->addr >= area->addr)
1034db64fe02SNick Piggin 			break;
1035db64fe02SNick Piggin 	}
1036db64fe02SNick Piggin 	area->next = *p;
1037db64fe02SNick Piggin 	*p = area;
10381da177e4SLinus Torvalds 	write_unlock(&vmlist_lock);
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	return area;
10411da177e4SLinus Torvalds }
10421da177e4SLinus Torvalds 
1043930fc45aSChristoph Lameter struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1044930fc45aSChristoph Lameter 				unsigned long start, unsigned long end)
1045930fc45aSChristoph Lameter {
104623016969SChristoph Lameter 	return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
104723016969SChristoph Lameter 						__builtin_return_address(0));
1048930fc45aSChristoph Lameter }
10495992b6daSRusty Russell EXPORT_SYMBOL_GPL(__get_vm_area);
1050930fc45aSChristoph Lameter 
10511da177e4SLinus Torvalds /**
1052183ff22bSSimon Arlott  *	get_vm_area  -  reserve a contiguous kernel virtual area
10531da177e4SLinus Torvalds  *	@size:		size of the area
10541da177e4SLinus Torvalds  *	@flags:		%VM_IOREMAP for I/O mappings or VM_ALLOC
10551da177e4SLinus Torvalds  *
10561da177e4SLinus Torvalds  *	Search an area of @size in the kernel virtual mapping area,
10571da177e4SLinus Torvalds  *	and reserved it for out purposes.  Returns the area descriptor
10581da177e4SLinus Torvalds  *	on success or %NULL on failure.
10591da177e4SLinus Torvalds  */
10601da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
10611da177e4SLinus Torvalds {
106223016969SChristoph Lameter 	return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
106323016969SChristoph Lameter 				-1, GFP_KERNEL, __builtin_return_address(0));
106423016969SChristoph Lameter }
106523016969SChristoph Lameter 
106623016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
106723016969SChristoph Lameter 				void *caller)
106823016969SChristoph Lameter {
106923016969SChristoph Lameter 	return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
107023016969SChristoph Lameter 						-1, GFP_KERNEL, caller);
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
107352fd24caSGiridhar Pemmasani struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
107452fd24caSGiridhar Pemmasani 				   int node, gfp_t gfp_mask)
1075930fc45aSChristoph Lameter {
107652fd24caSGiridhar Pemmasani 	return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
107723016969SChristoph Lameter 				  gfp_mask, __builtin_return_address(0));
1078930fc45aSChristoph Lameter }
1079930fc45aSChristoph Lameter 
1080db64fe02SNick Piggin static struct vm_struct *find_vm_area(const void *addr)
108183342314SNick Piggin {
1082db64fe02SNick Piggin 	struct vmap_area *va;
108383342314SNick Piggin 
1084db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
1085db64fe02SNick Piggin 	if (va && va->flags & VM_VM_AREA)
1086db64fe02SNick Piggin 		return va->private;
108783342314SNick Piggin 
10887856dfebSAndi Kleen 	return NULL;
10897856dfebSAndi Kleen }
10907856dfebSAndi Kleen 
10911da177e4SLinus Torvalds /**
1092183ff22bSSimon Arlott  *	remove_vm_area  -  find and remove a continuous kernel virtual area
10931da177e4SLinus Torvalds  *	@addr:		base address
10941da177e4SLinus Torvalds  *
10951da177e4SLinus Torvalds  *	Search for the kernel VM area starting at @addr, and remove it.
10961da177e4SLinus Torvalds  *	This function returns the found VM area, but using it is NOT safe
10977856dfebSAndi Kleen  *	on SMP machines, except for its size or flags.
10981da177e4SLinus Torvalds  */
1099b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr)
11001da177e4SLinus Torvalds {
1101db64fe02SNick Piggin 	struct vmap_area *va;
1102db64fe02SNick Piggin 
1103db64fe02SNick Piggin 	va = find_vmap_area((unsigned long)addr);
1104db64fe02SNick Piggin 	if (va && va->flags & VM_VM_AREA) {
1105db64fe02SNick Piggin 		struct vm_struct *vm = va->private;
1106db64fe02SNick Piggin 		struct vm_struct *tmp, **p;
1107db64fe02SNick Piggin 		free_unmap_vmap_area(va);
1108db64fe02SNick Piggin 		vm->size -= PAGE_SIZE;
1109db64fe02SNick Piggin 
11101da177e4SLinus Torvalds 		write_lock(&vmlist_lock);
1111db64fe02SNick Piggin 		for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
1112db64fe02SNick Piggin 			;
1113db64fe02SNick Piggin 		*p = tmp->next;
11141da177e4SLinus Torvalds 		write_unlock(&vmlist_lock);
1115db64fe02SNick Piggin 
1116db64fe02SNick Piggin 		return vm;
1117db64fe02SNick Piggin 	}
1118db64fe02SNick Piggin 	return NULL;
11191da177e4SLinus Torvalds }
11201da177e4SLinus Torvalds 
1121b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds 	struct vm_struct *area;
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	if (!addr)
11261da177e4SLinus Torvalds 		return;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	if ((PAGE_SIZE-1) & (unsigned long)addr) {
11294c8573e2SArjan van de Ven 		WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
11301da177e4SLinus Torvalds 		return;
11311da177e4SLinus Torvalds 	}
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	area = remove_vm_area(addr);
11341da177e4SLinus Torvalds 	if (unlikely(!area)) {
11354c8573e2SArjan van de Ven 		WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
11361da177e4SLinus Torvalds 				addr);
11371da177e4SLinus Torvalds 		return;
11381da177e4SLinus Torvalds 	}
11391da177e4SLinus Torvalds 
11409a11b49aSIngo Molnar 	debug_check_no_locks_freed(addr, area->size);
11413ac7fe5aSThomas Gleixner 	debug_check_no_obj_freed(addr, area->size);
11429a11b49aSIngo Molnar 
11431da177e4SLinus Torvalds 	if (deallocate_pages) {
11441da177e4SLinus Torvalds 		int i;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 		for (i = 0; i < area->nr_pages; i++) {
1147bf53d6f8SChristoph Lameter 			struct page *page = area->pages[i];
1148bf53d6f8SChristoph Lameter 
1149bf53d6f8SChristoph Lameter 			BUG_ON(!page);
1150bf53d6f8SChristoph Lameter 			__free_page(page);
11511da177e4SLinus Torvalds 		}
11521da177e4SLinus Torvalds 
11538757d5faSJan Kiszka 		if (area->flags & VM_VPAGES)
11541da177e4SLinus Torvalds 			vfree(area->pages);
11551da177e4SLinus Torvalds 		else
11561da177e4SLinus Torvalds 			kfree(area->pages);
11571da177e4SLinus Torvalds 	}
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	kfree(area);
11601da177e4SLinus Torvalds 	return;
11611da177e4SLinus Torvalds }
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds /**
11641da177e4SLinus Torvalds  *	vfree  -  release memory allocated by vmalloc()
11651da177e4SLinus Torvalds  *	@addr:		memory base address
11661da177e4SLinus Torvalds  *
1167183ff22bSSimon Arlott  *	Free the virtually continuous memory area starting at @addr, as
116880e93effSPekka Enberg  *	obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
116980e93effSPekka Enberg  *	NULL, no operation is performed.
11701da177e4SLinus Torvalds  *
117180e93effSPekka Enberg  *	Must not be called in interrupt context.
11721da177e4SLinus Torvalds  */
1173b3bdda02SChristoph Lameter void vfree(const void *addr)
11741da177e4SLinus Torvalds {
11751da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
11761da177e4SLinus Torvalds 	__vunmap(addr, 1);
11771da177e4SLinus Torvalds }
11781da177e4SLinus Torvalds EXPORT_SYMBOL(vfree);
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds /**
11811da177e4SLinus Torvalds  *	vunmap  -  release virtual mapping obtained by vmap()
11821da177e4SLinus Torvalds  *	@addr:		memory base address
11831da177e4SLinus Torvalds  *
11841da177e4SLinus Torvalds  *	Free the virtually contiguous memory area starting at @addr,
11851da177e4SLinus Torvalds  *	which was created from the page array passed to vmap().
11861da177e4SLinus Torvalds  *
118780e93effSPekka Enberg  *	Must not be called in interrupt context.
11881da177e4SLinus Torvalds  */
1189b3bdda02SChristoph Lameter void vunmap(const void *addr)
11901da177e4SLinus Torvalds {
11911da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
11921da177e4SLinus Torvalds 	__vunmap(addr, 0);
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap);
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds /**
11971da177e4SLinus Torvalds  *	vmap  -  map an array of pages into virtually contiguous space
11981da177e4SLinus Torvalds  *	@pages:		array of page pointers
11991da177e4SLinus Torvalds  *	@count:		number of pages to map
12001da177e4SLinus Torvalds  *	@flags:		vm_area->flags
12011da177e4SLinus Torvalds  *	@prot:		page protection for the mapping
12021da177e4SLinus Torvalds  *
12031da177e4SLinus Torvalds  *	Maps @count pages from @pages into contiguous kernel virtual
12041da177e4SLinus Torvalds  *	space.
12051da177e4SLinus Torvalds  */
12061da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count,
12071da177e4SLinus Torvalds 		unsigned long flags, pgprot_t prot)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	struct vm_struct *area;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	if (count > num_physpages)
12121da177e4SLinus Torvalds 		return NULL;
12131da177e4SLinus Torvalds 
121423016969SChristoph Lameter 	area = get_vm_area_caller((count << PAGE_SHIFT), flags,
121523016969SChristoph Lameter 					__builtin_return_address(0));
12161da177e4SLinus Torvalds 	if (!area)
12171da177e4SLinus Torvalds 		return NULL;
121823016969SChristoph Lameter 
12191da177e4SLinus Torvalds 	if (map_vm_area(area, prot, &pages)) {
12201da177e4SLinus Torvalds 		vunmap(area->addr);
12211da177e4SLinus Torvalds 		return NULL;
12221da177e4SLinus Torvalds 	}
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	return area->addr;
12251da177e4SLinus Torvalds }
12261da177e4SLinus Torvalds EXPORT_SYMBOL(vmap);
12271da177e4SLinus Torvalds 
1228db64fe02SNick Piggin static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1229db64fe02SNick Piggin 			    int node, void *caller);
1230e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
123123016969SChristoph Lameter 				 pgprot_t prot, int node, void *caller)
12321da177e4SLinus Torvalds {
12331da177e4SLinus Torvalds 	struct page **pages;
12341da177e4SLinus Torvalds 	unsigned int nr_pages, array_size, i;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
12371da177e4SLinus Torvalds 	array_size = (nr_pages * sizeof(struct page *));
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	area->nr_pages = nr_pages;
12401da177e4SLinus Torvalds 	/* Please note that the recursion is strictly bounded. */
12418757d5faSJan Kiszka 	if (array_size > PAGE_SIZE) {
124294f6030cSChristoph Lameter 		pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
124323016969SChristoph Lameter 				PAGE_KERNEL, node, caller);
12448757d5faSJan Kiszka 		area->flags |= VM_VPAGES;
1245286e1ea3SAndrew Morton 	} else {
1246286e1ea3SAndrew Morton 		pages = kmalloc_node(array_size,
12476cb06229SChristoph Lameter 				(gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
1248286e1ea3SAndrew Morton 				node);
1249286e1ea3SAndrew Morton 	}
12501da177e4SLinus Torvalds 	area->pages = pages;
125123016969SChristoph Lameter 	area->caller = caller;
12521da177e4SLinus Torvalds 	if (!area->pages) {
12531da177e4SLinus Torvalds 		remove_vm_area(area->addr);
12541da177e4SLinus Torvalds 		kfree(area);
12551da177e4SLinus Torvalds 		return NULL;
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	for (i = 0; i < area->nr_pages; i++) {
1259bf53d6f8SChristoph Lameter 		struct page *page;
1260bf53d6f8SChristoph Lameter 
1261930fc45aSChristoph Lameter 		if (node < 0)
1262bf53d6f8SChristoph Lameter 			page = alloc_page(gfp_mask);
1263930fc45aSChristoph Lameter 		else
1264bf53d6f8SChristoph Lameter 			page = alloc_pages_node(node, gfp_mask, 0);
1265bf53d6f8SChristoph Lameter 
1266bf53d6f8SChristoph Lameter 		if (unlikely(!page)) {
12671da177e4SLinus Torvalds 			/* Successfully allocated i pages, free them in __vunmap() */
12681da177e4SLinus Torvalds 			area->nr_pages = i;
12691da177e4SLinus Torvalds 			goto fail;
12701da177e4SLinus Torvalds 		}
1271bf53d6f8SChristoph Lameter 		area->pages[i] = page;
12721da177e4SLinus Torvalds 	}
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	if (map_vm_area(area, prot, &pages))
12751da177e4SLinus Torvalds 		goto fail;
12761da177e4SLinus Torvalds 	return area->addr;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds fail:
12791da177e4SLinus Torvalds 	vfree(area->addr);
12801da177e4SLinus Torvalds 	return NULL;
12811da177e4SLinus Torvalds }
12821da177e4SLinus Torvalds 
1283930fc45aSChristoph Lameter void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
1284930fc45aSChristoph Lameter {
128523016969SChristoph Lameter 	return __vmalloc_area_node(area, gfp_mask, prot, -1,
128623016969SChristoph Lameter 					__builtin_return_address(0));
1287930fc45aSChristoph Lameter }
1288930fc45aSChristoph Lameter 
12891da177e4SLinus Torvalds /**
1290930fc45aSChristoph Lameter  *	__vmalloc_node  -  allocate virtually contiguous memory
12911da177e4SLinus Torvalds  *	@size:		allocation size
12921da177e4SLinus Torvalds  *	@gfp_mask:	flags for the page level allocator
12931da177e4SLinus Torvalds  *	@prot:		protection mask for the allocated pages
1294d44e0780SRandy Dunlap  *	@node:		node to use for allocation or -1
1295c85d194bSRandy Dunlap  *	@caller:	caller's return address
12961da177e4SLinus Torvalds  *
12971da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
12981da177e4SLinus Torvalds  *	allocator with @gfp_mask flags.  Map them into contiguous
12991da177e4SLinus Torvalds  *	kernel virtual space, using a pagetable protection of @prot.
13001da177e4SLinus Torvalds  */
1301b221385bSAdrian Bunk static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
130223016969SChristoph Lameter 						int node, void *caller)
13031da177e4SLinus Torvalds {
13041da177e4SLinus Torvalds 	struct vm_struct *area;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	size = PAGE_ALIGN(size);
13071da177e4SLinus Torvalds 	if (!size || (size >> PAGE_SHIFT) > num_physpages)
13081da177e4SLinus Torvalds 		return NULL;
13091da177e4SLinus Torvalds 
131023016969SChristoph Lameter 	area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
131123016969SChristoph Lameter 						node, gfp_mask, caller);
131223016969SChristoph Lameter 
13131da177e4SLinus Torvalds 	if (!area)
13141da177e4SLinus Torvalds 		return NULL;
13151da177e4SLinus Torvalds 
131623016969SChristoph Lameter 	return __vmalloc_area_node(area, gfp_mask, prot, node, caller);
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
1319930fc45aSChristoph Lameter void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1320930fc45aSChristoph Lameter {
132123016969SChristoph Lameter 	return __vmalloc_node(size, gfp_mask, prot, -1,
132223016969SChristoph Lameter 				__builtin_return_address(0));
1323930fc45aSChristoph Lameter }
13241da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc);
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds /**
13271da177e4SLinus Torvalds  *	vmalloc  -  allocate virtually contiguous memory
13281da177e4SLinus Torvalds  *	@size:		allocation size
13291da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
13301da177e4SLinus Torvalds  *	allocator and map them into contiguous kernel virtual space.
13311da177e4SLinus Torvalds  *
1332c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
13331da177e4SLinus Torvalds  *	use __vmalloc() instead.
13341da177e4SLinus Torvalds  */
13351da177e4SLinus Torvalds void *vmalloc(unsigned long size)
13361da177e4SLinus Torvalds {
133723016969SChristoph Lameter 	return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
133823016969SChristoph Lameter 					-1, __builtin_return_address(0));
13391da177e4SLinus Torvalds }
13401da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc);
13411da177e4SLinus Torvalds 
1342930fc45aSChristoph Lameter /**
1343ead04089SRolf Eike Beer  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
134483342314SNick Piggin  * @size: allocation size
1345ead04089SRolf Eike Beer  *
1346ead04089SRolf Eike Beer  * The resulting memory area is zeroed so it can be mapped to userspace
1347ead04089SRolf Eike Beer  * without leaking data.
134883342314SNick Piggin  */
134983342314SNick Piggin void *vmalloc_user(unsigned long size)
135083342314SNick Piggin {
135183342314SNick Piggin 	struct vm_struct *area;
135283342314SNick Piggin 	void *ret;
135383342314SNick Piggin 
135483342314SNick Piggin 	ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
13552b4ac44eSEric Dumazet 	if (ret) {
1356db64fe02SNick Piggin 		area = find_vm_area(ret);
135783342314SNick Piggin 		area->flags |= VM_USERMAP;
13582b4ac44eSEric Dumazet 	}
135983342314SNick Piggin 	return ret;
136083342314SNick Piggin }
136183342314SNick Piggin EXPORT_SYMBOL(vmalloc_user);
136283342314SNick Piggin 
136383342314SNick Piggin /**
1364930fc45aSChristoph Lameter  *	vmalloc_node  -  allocate memory on a specific node
1365930fc45aSChristoph Lameter  *	@size:		allocation size
1366d44e0780SRandy Dunlap  *	@node:		numa node
1367930fc45aSChristoph Lameter  *
1368930fc45aSChristoph Lameter  *	Allocate enough pages to cover @size from the page level
1369930fc45aSChristoph Lameter  *	allocator and map them into contiguous kernel virtual space.
1370930fc45aSChristoph Lameter  *
1371c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
1372930fc45aSChristoph Lameter  *	use __vmalloc() instead.
1373930fc45aSChristoph Lameter  */
1374930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node)
1375930fc45aSChristoph Lameter {
137623016969SChristoph Lameter 	return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
137723016969SChristoph Lameter 					node, __builtin_return_address(0));
1378930fc45aSChristoph Lameter }
1379930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node);
1380930fc45aSChristoph Lameter 
13814dc3b16bSPavel Pisa #ifndef PAGE_KERNEL_EXEC
13824dc3b16bSPavel Pisa # define PAGE_KERNEL_EXEC PAGE_KERNEL
13834dc3b16bSPavel Pisa #endif
13844dc3b16bSPavel Pisa 
13851da177e4SLinus Torvalds /**
13861da177e4SLinus Torvalds  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
13871da177e4SLinus Torvalds  *	@size:		allocation size
13881da177e4SLinus Torvalds  *
13891da177e4SLinus Torvalds  *	Kernel-internal function to allocate enough pages to cover @size
13901da177e4SLinus Torvalds  *	the page level allocator and map them into contiguous and
13911da177e4SLinus Torvalds  *	executable kernel virtual space.
13921da177e4SLinus Torvalds  *
1393c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
13941da177e4SLinus Torvalds  *	use __vmalloc() instead.
13951da177e4SLinus Torvalds  */
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds void *vmalloc_exec(unsigned long size)
13981da177e4SLinus Torvalds {
13991da177e4SLinus Torvalds 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
14020d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
14037ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
14040d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
14057ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
14060d08e0d3SAndi Kleen #else
14070d08e0d3SAndi Kleen #define GFP_VMALLOC32 GFP_KERNEL
14080d08e0d3SAndi Kleen #endif
14090d08e0d3SAndi Kleen 
14101da177e4SLinus Torvalds /**
14111da177e4SLinus Torvalds  *	vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
14121da177e4SLinus Torvalds  *	@size:		allocation size
14131da177e4SLinus Torvalds  *
14141da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
14151da177e4SLinus Torvalds  *	page level allocator and map them into contiguous kernel virtual space.
14161da177e4SLinus Torvalds  */
14171da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
14181da177e4SLinus Torvalds {
14190d08e0d3SAndi Kleen 	return __vmalloc(size, GFP_VMALLOC32, PAGE_KERNEL);
14201da177e4SLinus Torvalds }
14211da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32);
14221da177e4SLinus Torvalds 
142383342314SNick Piggin /**
1424ead04089SRolf Eike Beer  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
142583342314SNick Piggin  *	@size:		allocation size
1426ead04089SRolf Eike Beer  *
1427ead04089SRolf Eike Beer  * The resulting memory area is 32bit addressable and zeroed so it can be
1428ead04089SRolf Eike Beer  * mapped to userspace without leaking data.
142983342314SNick Piggin  */
143083342314SNick Piggin void *vmalloc_32_user(unsigned long size)
143183342314SNick Piggin {
143283342314SNick Piggin 	struct vm_struct *area;
143383342314SNick Piggin 	void *ret;
143483342314SNick Piggin 
14350d08e0d3SAndi Kleen 	ret = __vmalloc(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL);
14362b4ac44eSEric Dumazet 	if (ret) {
1437db64fe02SNick Piggin 		area = find_vm_area(ret);
143883342314SNick Piggin 		area->flags |= VM_USERMAP;
14392b4ac44eSEric Dumazet 	}
144083342314SNick Piggin 	return ret;
144183342314SNick Piggin }
144283342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user);
144383342314SNick Piggin 
14441da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
14451da177e4SLinus Torvalds {
14461da177e4SLinus Torvalds 	struct vm_struct *tmp;
14471da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
14481da177e4SLinus Torvalds 	unsigned long n;
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 	/* Don't allow overflow */
14511da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
14521da177e4SLinus Torvalds 		count = -(unsigned long) addr;
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	read_lock(&vmlist_lock);
14551da177e4SLinus Torvalds 	for (tmp = vmlist; tmp; tmp = tmp->next) {
14561da177e4SLinus Torvalds 		vaddr = (char *) tmp->addr;
14571da177e4SLinus Torvalds 		if (addr >= vaddr + tmp->size - PAGE_SIZE)
14581da177e4SLinus Torvalds 			continue;
14591da177e4SLinus Torvalds 		while (addr < vaddr) {
14601da177e4SLinus Torvalds 			if (count == 0)
14611da177e4SLinus Torvalds 				goto finished;
14621da177e4SLinus Torvalds 			*buf = '\0';
14631da177e4SLinus Torvalds 			buf++;
14641da177e4SLinus Torvalds 			addr++;
14651da177e4SLinus Torvalds 			count--;
14661da177e4SLinus Torvalds 		}
14671da177e4SLinus Torvalds 		n = vaddr + tmp->size - PAGE_SIZE - addr;
14681da177e4SLinus Torvalds 		do {
14691da177e4SLinus Torvalds 			if (count == 0)
14701da177e4SLinus Torvalds 				goto finished;
14711da177e4SLinus Torvalds 			*buf = *addr;
14721da177e4SLinus Torvalds 			buf++;
14731da177e4SLinus Torvalds 			addr++;
14741da177e4SLinus Torvalds 			count--;
14751da177e4SLinus Torvalds 		} while (--n > 0);
14761da177e4SLinus Torvalds 	}
14771da177e4SLinus Torvalds finished:
14781da177e4SLinus Torvalds 	read_unlock(&vmlist_lock);
14791da177e4SLinus Torvalds 	return buf - buf_start;
14801da177e4SLinus Torvalds }
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
14831da177e4SLinus Torvalds {
14841da177e4SLinus Torvalds 	struct vm_struct *tmp;
14851da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
14861da177e4SLinus Torvalds 	unsigned long n;
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds 	/* Don't allow overflow */
14891da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
14901da177e4SLinus Torvalds 		count = -(unsigned long) addr;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	read_lock(&vmlist_lock);
14931da177e4SLinus Torvalds 	for (tmp = vmlist; tmp; tmp = tmp->next) {
14941da177e4SLinus Torvalds 		vaddr = (char *) tmp->addr;
14951da177e4SLinus Torvalds 		if (addr >= vaddr + tmp->size - PAGE_SIZE)
14961da177e4SLinus Torvalds 			continue;
14971da177e4SLinus Torvalds 		while (addr < vaddr) {
14981da177e4SLinus Torvalds 			if (count == 0)
14991da177e4SLinus Torvalds 				goto finished;
15001da177e4SLinus Torvalds 			buf++;
15011da177e4SLinus Torvalds 			addr++;
15021da177e4SLinus Torvalds 			count--;
15031da177e4SLinus Torvalds 		}
15041da177e4SLinus Torvalds 		n = vaddr + tmp->size - PAGE_SIZE - addr;
15051da177e4SLinus Torvalds 		do {
15061da177e4SLinus Torvalds 			if (count == 0)
15071da177e4SLinus Torvalds 				goto finished;
15081da177e4SLinus Torvalds 			*addr = *buf;
15091da177e4SLinus Torvalds 			buf++;
15101da177e4SLinus Torvalds 			addr++;
15111da177e4SLinus Torvalds 			count--;
15121da177e4SLinus Torvalds 		} while (--n > 0);
15131da177e4SLinus Torvalds 	}
15141da177e4SLinus Torvalds finished:
15151da177e4SLinus Torvalds 	read_unlock(&vmlist_lock);
15161da177e4SLinus Torvalds 	return buf - buf_start;
15171da177e4SLinus Torvalds }
151883342314SNick Piggin 
151983342314SNick Piggin /**
152083342314SNick Piggin  *	remap_vmalloc_range  -  map vmalloc pages to userspace
152183342314SNick Piggin  *	@vma:		vma to cover (map full range of vma)
152283342314SNick Piggin  *	@addr:		vmalloc memory
152383342314SNick Piggin  *	@pgoff:		number of pages into addr before first page to map
15247682486bSRandy Dunlap  *
15257682486bSRandy Dunlap  *	Returns:	0 for success, -Exxx on failure
152683342314SNick Piggin  *
152783342314SNick Piggin  *	This function checks that addr is a valid vmalloc'ed area, and
152883342314SNick Piggin  *	that it is big enough to cover the vma. Will return failure if
152983342314SNick Piggin  *	that criteria isn't met.
153083342314SNick Piggin  *
153172fd4a35SRobert P. J. Day  *	Similar to remap_pfn_range() (see mm/memory.c)
153283342314SNick Piggin  */
153383342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
153483342314SNick Piggin 						unsigned long pgoff)
153583342314SNick Piggin {
153683342314SNick Piggin 	struct vm_struct *area;
153783342314SNick Piggin 	unsigned long uaddr = vma->vm_start;
153883342314SNick Piggin 	unsigned long usize = vma->vm_end - vma->vm_start;
153983342314SNick Piggin 
154083342314SNick Piggin 	if ((PAGE_SIZE-1) & (unsigned long)addr)
154183342314SNick Piggin 		return -EINVAL;
154283342314SNick Piggin 
1543db64fe02SNick Piggin 	area = find_vm_area(addr);
154483342314SNick Piggin 	if (!area)
1545db64fe02SNick Piggin 		return -EINVAL;
154683342314SNick Piggin 
154783342314SNick Piggin 	if (!(area->flags & VM_USERMAP))
1548db64fe02SNick Piggin 		return -EINVAL;
154983342314SNick Piggin 
155083342314SNick Piggin 	if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
1551db64fe02SNick Piggin 		return -EINVAL;
155283342314SNick Piggin 
155383342314SNick Piggin 	addr += pgoff << PAGE_SHIFT;
155483342314SNick Piggin 	do {
155583342314SNick Piggin 		struct page *page = vmalloc_to_page(addr);
1556db64fe02SNick Piggin 		int ret;
1557db64fe02SNick Piggin 
155883342314SNick Piggin 		ret = vm_insert_page(vma, uaddr, page);
155983342314SNick Piggin 		if (ret)
156083342314SNick Piggin 			return ret;
156183342314SNick Piggin 
156283342314SNick Piggin 		uaddr += PAGE_SIZE;
156383342314SNick Piggin 		addr += PAGE_SIZE;
156483342314SNick Piggin 		usize -= PAGE_SIZE;
156583342314SNick Piggin 	} while (usize > 0);
156683342314SNick Piggin 
156783342314SNick Piggin 	/* Prevent "things" like memory migration? VM_flags need a cleanup... */
156883342314SNick Piggin 	vma->vm_flags |= VM_RESERVED;
156983342314SNick Piggin 
1570db64fe02SNick Piggin 	return 0;
157183342314SNick Piggin }
157283342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range);
157383342314SNick Piggin 
15741eeb66a1SChristoph Hellwig /*
15751eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
15761eeb66a1SChristoph Hellwig  * have one.
15771eeb66a1SChristoph Hellwig  */
15781eeb66a1SChristoph Hellwig void  __attribute__((weak)) vmalloc_sync_all(void)
15791eeb66a1SChristoph Hellwig {
15801eeb66a1SChristoph Hellwig }
15815f4352fbSJeremy Fitzhardinge 
15825f4352fbSJeremy Fitzhardinge 
15832f569afdSMartin Schwidefsky static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
15845f4352fbSJeremy Fitzhardinge {
15855f4352fbSJeremy Fitzhardinge 	/* apply_to_page_range() does all the hard work. */
15865f4352fbSJeremy Fitzhardinge 	return 0;
15875f4352fbSJeremy Fitzhardinge }
15885f4352fbSJeremy Fitzhardinge 
15895f4352fbSJeremy Fitzhardinge /**
15905f4352fbSJeremy Fitzhardinge  *	alloc_vm_area - allocate a range of kernel address space
15915f4352fbSJeremy Fitzhardinge  *	@size:		size of the area
15927682486bSRandy Dunlap  *
15937682486bSRandy Dunlap  *	Returns:	NULL on failure, vm_struct on success
15945f4352fbSJeremy Fitzhardinge  *
15955f4352fbSJeremy Fitzhardinge  *	This function reserves a range of kernel address space, and
15965f4352fbSJeremy Fitzhardinge  *	allocates pagetables to map that range.  No actual mappings
15975f4352fbSJeremy Fitzhardinge  *	are created.  If the kernel address space is not shared
15985f4352fbSJeremy Fitzhardinge  *	between processes, it syncs the pagetable across all
15995f4352fbSJeremy Fitzhardinge  *	processes.
16005f4352fbSJeremy Fitzhardinge  */
16015f4352fbSJeremy Fitzhardinge struct vm_struct *alloc_vm_area(size_t size)
16025f4352fbSJeremy Fitzhardinge {
16035f4352fbSJeremy Fitzhardinge 	struct vm_struct *area;
16045f4352fbSJeremy Fitzhardinge 
160523016969SChristoph Lameter 	area = get_vm_area_caller(size, VM_IOREMAP,
160623016969SChristoph Lameter 				__builtin_return_address(0));
16075f4352fbSJeremy Fitzhardinge 	if (area == NULL)
16085f4352fbSJeremy Fitzhardinge 		return NULL;
16095f4352fbSJeremy Fitzhardinge 
16105f4352fbSJeremy Fitzhardinge 	/*
16115f4352fbSJeremy Fitzhardinge 	 * This ensures that page tables are constructed for this region
16125f4352fbSJeremy Fitzhardinge 	 * of kernel virtual address space and mapped into init_mm.
16135f4352fbSJeremy Fitzhardinge 	 */
16145f4352fbSJeremy Fitzhardinge 	if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
16155f4352fbSJeremy Fitzhardinge 				area->size, f, NULL)) {
16165f4352fbSJeremy Fitzhardinge 		free_vm_area(area);
16175f4352fbSJeremy Fitzhardinge 		return NULL;
16185f4352fbSJeremy Fitzhardinge 	}
16195f4352fbSJeremy Fitzhardinge 
16205f4352fbSJeremy Fitzhardinge 	/* Make sure the pagetables are constructed in process kernel
16215f4352fbSJeremy Fitzhardinge 	   mappings */
16225f4352fbSJeremy Fitzhardinge 	vmalloc_sync_all();
16235f4352fbSJeremy Fitzhardinge 
16245f4352fbSJeremy Fitzhardinge 	return area;
16255f4352fbSJeremy Fitzhardinge }
16265f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(alloc_vm_area);
16275f4352fbSJeremy Fitzhardinge 
16285f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area)
16295f4352fbSJeremy Fitzhardinge {
16305f4352fbSJeremy Fitzhardinge 	struct vm_struct *ret;
16315f4352fbSJeremy Fitzhardinge 	ret = remove_vm_area(area->addr);
16325f4352fbSJeremy Fitzhardinge 	BUG_ON(ret != area);
16335f4352fbSJeremy Fitzhardinge 	kfree(area);
16345f4352fbSJeremy Fitzhardinge }
16355f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area);
1636a10aa579SChristoph Lameter 
1637a10aa579SChristoph Lameter 
1638a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS
1639a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos)
1640a10aa579SChristoph Lameter {
1641a10aa579SChristoph Lameter 	loff_t n = *pos;
1642a10aa579SChristoph Lameter 	struct vm_struct *v;
1643a10aa579SChristoph Lameter 
1644a10aa579SChristoph Lameter 	read_lock(&vmlist_lock);
1645a10aa579SChristoph Lameter 	v = vmlist;
1646a10aa579SChristoph Lameter 	while (n > 0 && v) {
1647a10aa579SChristoph Lameter 		n--;
1648a10aa579SChristoph Lameter 		v = v->next;
1649a10aa579SChristoph Lameter 	}
1650a10aa579SChristoph Lameter 	if (!n)
1651a10aa579SChristoph Lameter 		return v;
1652a10aa579SChristoph Lameter 
1653a10aa579SChristoph Lameter 	return NULL;
1654a10aa579SChristoph Lameter 
1655a10aa579SChristoph Lameter }
1656a10aa579SChristoph Lameter 
1657a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos)
1658a10aa579SChristoph Lameter {
1659a10aa579SChristoph Lameter 	struct vm_struct *v = p;
1660a10aa579SChristoph Lameter 
1661a10aa579SChristoph Lameter 	++*pos;
1662a10aa579SChristoph Lameter 	return v->next;
1663a10aa579SChristoph Lameter }
1664a10aa579SChristoph Lameter 
1665a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p)
1666a10aa579SChristoph Lameter {
1667a10aa579SChristoph Lameter 	read_unlock(&vmlist_lock);
1668a10aa579SChristoph Lameter }
1669a10aa579SChristoph Lameter 
1670a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v)
1671a47a126aSEric Dumazet {
1672a47a126aSEric Dumazet 	if (NUMA_BUILD) {
1673a47a126aSEric Dumazet 		unsigned int nr, *counters = m->private;
1674a47a126aSEric Dumazet 
1675a47a126aSEric Dumazet 		if (!counters)
1676a47a126aSEric Dumazet 			return;
1677a47a126aSEric Dumazet 
1678a47a126aSEric Dumazet 		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
1679a47a126aSEric Dumazet 
1680a47a126aSEric Dumazet 		for (nr = 0; nr < v->nr_pages; nr++)
1681a47a126aSEric Dumazet 			counters[page_to_nid(v->pages[nr])]++;
1682a47a126aSEric Dumazet 
1683a47a126aSEric Dumazet 		for_each_node_state(nr, N_HIGH_MEMORY)
1684a47a126aSEric Dumazet 			if (counters[nr])
1685a47a126aSEric Dumazet 				seq_printf(m, " N%u=%u", nr, counters[nr]);
1686a47a126aSEric Dumazet 	}
1687a47a126aSEric Dumazet }
1688a47a126aSEric Dumazet 
1689a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p)
1690a10aa579SChristoph Lameter {
1691a10aa579SChristoph Lameter 	struct vm_struct *v = p;
1692a10aa579SChristoph Lameter 
1693a10aa579SChristoph Lameter 	seq_printf(m, "0x%p-0x%p %7ld",
1694a10aa579SChristoph Lameter 		v->addr, v->addr + v->size, v->size);
1695a10aa579SChristoph Lameter 
169623016969SChristoph Lameter 	if (v->caller) {
169723016969SChristoph Lameter 		char buff[2 * KSYM_NAME_LEN];
169823016969SChristoph Lameter 
169923016969SChristoph Lameter 		seq_putc(m, ' ');
170023016969SChristoph Lameter 		sprint_symbol(buff, (unsigned long)v->caller);
170123016969SChristoph Lameter 		seq_puts(m, buff);
170223016969SChristoph Lameter 	}
170323016969SChristoph Lameter 
1704a10aa579SChristoph Lameter 	if (v->nr_pages)
1705a10aa579SChristoph Lameter 		seq_printf(m, " pages=%d", v->nr_pages);
1706a10aa579SChristoph Lameter 
1707a10aa579SChristoph Lameter 	if (v->phys_addr)
1708a10aa579SChristoph Lameter 		seq_printf(m, " phys=%lx", v->phys_addr);
1709a10aa579SChristoph Lameter 
1710a10aa579SChristoph Lameter 	if (v->flags & VM_IOREMAP)
1711a10aa579SChristoph Lameter 		seq_printf(m, " ioremap");
1712a10aa579SChristoph Lameter 
1713a10aa579SChristoph Lameter 	if (v->flags & VM_ALLOC)
1714a10aa579SChristoph Lameter 		seq_printf(m, " vmalloc");
1715a10aa579SChristoph Lameter 
1716a10aa579SChristoph Lameter 	if (v->flags & VM_MAP)
1717a10aa579SChristoph Lameter 		seq_printf(m, " vmap");
1718a10aa579SChristoph Lameter 
1719a10aa579SChristoph Lameter 	if (v->flags & VM_USERMAP)
1720a10aa579SChristoph Lameter 		seq_printf(m, " user");
1721a10aa579SChristoph Lameter 
1722a10aa579SChristoph Lameter 	if (v->flags & VM_VPAGES)
1723a10aa579SChristoph Lameter 		seq_printf(m, " vpages");
1724a10aa579SChristoph Lameter 
1725a47a126aSEric Dumazet 	show_numa_info(m, v);
1726a10aa579SChristoph Lameter 	seq_putc(m, '\n');
1727a10aa579SChristoph Lameter 	return 0;
1728a10aa579SChristoph Lameter }
1729a10aa579SChristoph Lameter 
17305f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = {
1731a10aa579SChristoph Lameter 	.start = s_start,
1732a10aa579SChristoph Lameter 	.next = s_next,
1733a10aa579SChristoph Lameter 	.stop = s_stop,
1734a10aa579SChristoph Lameter 	.show = s_show,
1735a10aa579SChristoph Lameter };
17365f6a6a9cSAlexey Dobriyan 
17375f6a6a9cSAlexey Dobriyan static int vmalloc_open(struct inode *inode, struct file *file)
17385f6a6a9cSAlexey Dobriyan {
17395f6a6a9cSAlexey Dobriyan 	unsigned int *ptr = NULL;
17405f6a6a9cSAlexey Dobriyan 	int ret;
17415f6a6a9cSAlexey Dobriyan 
17425f6a6a9cSAlexey Dobriyan 	if (NUMA_BUILD)
17435f6a6a9cSAlexey Dobriyan 		ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
17445f6a6a9cSAlexey Dobriyan 	ret = seq_open(file, &vmalloc_op);
17455f6a6a9cSAlexey Dobriyan 	if (!ret) {
17465f6a6a9cSAlexey Dobriyan 		struct seq_file *m = file->private_data;
17475f6a6a9cSAlexey Dobriyan 		m->private = ptr;
17485f6a6a9cSAlexey Dobriyan 	} else
17495f6a6a9cSAlexey Dobriyan 		kfree(ptr);
17505f6a6a9cSAlexey Dobriyan 	return ret;
17515f6a6a9cSAlexey Dobriyan }
17525f6a6a9cSAlexey Dobriyan 
17535f6a6a9cSAlexey Dobriyan static const struct file_operations proc_vmalloc_operations = {
17545f6a6a9cSAlexey Dobriyan 	.open		= vmalloc_open,
17555f6a6a9cSAlexey Dobriyan 	.read		= seq_read,
17565f6a6a9cSAlexey Dobriyan 	.llseek		= seq_lseek,
17575f6a6a9cSAlexey Dobriyan 	.release	= seq_release_private,
17585f6a6a9cSAlexey Dobriyan };
17595f6a6a9cSAlexey Dobriyan 
17605f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void)
17615f6a6a9cSAlexey Dobriyan {
17625f6a6a9cSAlexey Dobriyan 	proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
17635f6a6a9cSAlexey Dobriyan 	return 0;
17645f6a6a9cSAlexey Dobriyan }
17655f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init);
1766a10aa579SChristoph Lameter #endif
1767a10aa579SChristoph Lameter 
1768