xref: /linux/mm/vmalloc.c (revision 040932cdcfca9b0ac55a4f74f194c2e2c8a2527b)
1 /*
2  *  linux/mm/vmalloc.c
3  *
4  *  Copyright (C) 1993  Linus Torvalds
5  *  Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
6  *  SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000
7  *  Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002
8  *  Numa awareness, Christoph Lameter, SGI, June 2005
9  */
10 
11 #include <linux/vmalloc.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/highmem.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/interrupt.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <linux/debugobjects.h>
21 #include <linux/kallsyms.h>
22 #include <linux/list.h>
23 #include <linux/rbtree.h>
24 #include <linux/radix-tree.h>
25 #include <linux/rcupdate.h>
26 #include <linux/pfn.h>
27 #include <linux/kmemleak.h>
28 
29 #include <asm/atomic.h>
30 #include <asm/uaccess.h>
31 #include <asm/tlbflush.h>
32 
33 
34 /*** Page table manipulation functions ***/
35 
36 static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
37 {
38 	pte_t *pte;
39 
40 	pte = pte_offset_kernel(pmd, addr);
41 	do {
42 		pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
43 		WARN_ON(!pte_none(ptent) && !pte_present(ptent));
44 	} while (pte++, addr += PAGE_SIZE, addr != end);
45 }
46 
47 static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
48 {
49 	pmd_t *pmd;
50 	unsigned long next;
51 
52 	pmd = pmd_offset(pud, addr);
53 	do {
54 		next = pmd_addr_end(addr, end);
55 		if (pmd_none_or_clear_bad(pmd))
56 			continue;
57 		vunmap_pte_range(pmd, addr, next);
58 	} while (pmd++, addr = next, addr != end);
59 }
60 
61 static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
62 {
63 	pud_t *pud;
64 	unsigned long next;
65 
66 	pud = pud_offset(pgd, addr);
67 	do {
68 		next = pud_addr_end(addr, end);
69 		if (pud_none_or_clear_bad(pud))
70 			continue;
71 		vunmap_pmd_range(pud, addr, next);
72 	} while (pud++, addr = next, addr != end);
73 }
74 
75 static void vunmap_page_range(unsigned long addr, unsigned long end)
76 {
77 	pgd_t *pgd;
78 	unsigned long next;
79 
80 	BUG_ON(addr >= end);
81 	pgd = pgd_offset_k(addr);
82 	do {
83 		next = pgd_addr_end(addr, end);
84 		if (pgd_none_or_clear_bad(pgd))
85 			continue;
86 		vunmap_pud_range(pgd, addr, next);
87 	} while (pgd++, addr = next, addr != end);
88 }
89 
90 static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
91 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
92 {
93 	pte_t *pte;
94 
95 	/*
96 	 * nr is a running index into the array which helps higher level
97 	 * callers keep track of where we're up to.
98 	 */
99 
100 	pte = pte_alloc_kernel(pmd, addr);
101 	if (!pte)
102 		return -ENOMEM;
103 	do {
104 		struct page *page = pages[*nr];
105 
106 		if (WARN_ON(!pte_none(*pte)))
107 			return -EBUSY;
108 		if (WARN_ON(!page))
109 			return -ENOMEM;
110 		set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
111 		(*nr)++;
112 	} while (pte++, addr += PAGE_SIZE, addr != end);
113 	return 0;
114 }
115 
116 static int vmap_pmd_range(pud_t *pud, unsigned long addr,
117 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
118 {
119 	pmd_t *pmd;
120 	unsigned long next;
121 
122 	pmd = pmd_alloc(&init_mm, pud, addr);
123 	if (!pmd)
124 		return -ENOMEM;
125 	do {
126 		next = pmd_addr_end(addr, end);
127 		if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
128 			return -ENOMEM;
129 	} while (pmd++, addr = next, addr != end);
130 	return 0;
131 }
132 
133 static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
134 		unsigned long end, pgprot_t prot, struct page **pages, int *nr)
135 {
136 	pud_t *pud;
137 	unsigned long next;
138 
139 	pud = pud_alloc(&init_mm, pgd, addr);
140 	if (!pud)
141 		return -ENOMEM;
142 	do {
143 		next = pud_addr_end(addr, end);
144 		if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
145 			return -ENOMEM;
146 	} while (pud++, addr = next, addr != end);
147 	return 0;
148 }
149 
150 /*
151  * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
152  * will have pfns corresponding to the "pages" array.
153  *
154  * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
155  */
156 static int vmap_page_range_noflush(unsigned long start, unsigned long end,
157 				   pgprot_t prot, struct page **pages)
158 {
159 	pgd_t *pgd;
160 	unsigned long next;
161 	unsigned long addr = start;
162 	int err = 0;
163 	int nr = 0;
164 
165 	BUG_ON(addr >= end);
166 	pgd = pgd_offset_k(addr);
167 	do {
168 		next = pgd_addr_end(addr, end);
169 		err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
170 		if (err)
171 			break;
172 	} while (pgd++, addr = next, addr != end);
173 
174 	if (unlikely(err))
175 		return err;
176 	return nr;
177 }
178 
179 static int vmap_page_range(unsigned long start, unsigned long end,
180 			   pgprot_t prot, struct page **pages)
181 {
182 	int ret;
183 
184 	ret = vmap_page_range_noflush(start, end, prot, pages);
185 	flush_cache_vmap(start, end);
186 	return ret;
187 }
188 
189 static inline int is_vmalloc_or_module_addr(const void *x)
190 {
191 	/*
192 	 * ARM, x86-64 and sparc64 put modules in a special place,
193 	 * and fall back on vmalloc() if that fails. Others
194 	 * just put it in the vmalloc space.
195 	 */
196 #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
197 	unsigned long addr = (unsigned long)x;
198 	if (addr >= MODULES_VADDR && addr < MODULES_END)
199 		return 1;
200 #endif
201 	return is_vmalloc_addr(x);
202 }
203 
204 /*
205  * Walk a vmap address to the struct page it maps.
206  */
207 struct page *vmalloc_to_page(const void *vmalloc_addr)
208 {
209 	unsigned long addr = (unsigned long) vmalloc_addr;
210 	struct page *page = NULL;
211 	pgd_t *pgd = pgd_offset_k(addr);
212 
213 	/*
214 	 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
215 	 * architectures that do not vmalloc module space
216 	 */
217 	VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
218 
219 	if (!pgd_none(*pgd)) {
220 		pud_t *pud = pud_offset(pgd, addr);
221 		if (!pud_none(*pud)) {
222 			pmd_t *pmd = pmd_offset(pud, addr);
223 			if (!pmd_none(*pmd)) {
224 				pte_t *ptep, pte;
225 
226 				ptep = pte_offset_map(pmd, addr);
227 				pte = *ptep;
228 				if (pte_present(pte))
229 					page = pte_page(pte);
230 				pte_unmap(ptep);
231 			}
232 		}
233 	}
234 	return page;
235 }
236 EXPORT_SYMBOL(vmalloc_to_page);
237 
238 /*
239  * Map a vmalloc()-space virtual address to the physical page frame number.
240  */
241 unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
242 {
243 	return page_to_pfn(vmalloc_to_page(vmalloc_addr));
244 }
245 EXPORT_SYMBOL(vmalloc_to_pfn);
246 
247 
248 /*** Global kva allocator ***/
249 
250 #define VM_LAZY_FREE	0x01
251 #define VM_LAZY_FREEING	0x02
252 #define VM_VM_AREA	0x04
253 
254 struct vmap_area {
255 	unsigned long va_start;
256 	unsigned long va_end;
257 	unsigned long flags;
258 	struct rb_node rb_node;		/* address sorted rbtree */
259 	struct list_head list;		/* address sorted list */
260 	struct list_head purge_list;	/* "lazy purge" list */
261 	void *private;
262 	struct rcu_head rcu_head;
263 };
264 
265 static DEFINE_SPINLOCK(vmap_area_lock);
266 static struct rb_root vmap_area_root = RB_ROOT;
267 static LIST_HEAD(vmap_area_list);
268 static unsigned long vmap_area_pcpu_hole;
269 
270 static struct vmap_area *__find_vmap_area(unsigned long addr)
271 {
272 	struct rb_node *n = vmap_area_root.rb_node;
273 
274 	while (n) {
275 		struct vmap_area *va;
276 
277 		va = rb_entry(n, struct vmap_area, rb_node);
278 		if (addr < va->va_start)
279 			n = n->rb_left;
280 		else if (addr > va->va_start)
281 			n = n->rb_right;
282 		else
283 			return va;
284 	}
285 
286 	return NULL;
287 }
288 
289 static void __insert_vmap_area(struct vmap_area *va)
290 {
291 	struct rb_node **p = &vmap_area_root.rb_node;
292 	struct rb_node *parent = NULL;
293 	struct rb_node *tmp;
294 
295 	while (*p) {
296 		struct vmap_area *tmp;
297 
298 		parent = *p;
299 		tmp = rb_entry(parent, struct vmap_area, rb_node);
300 		if (va->va_start < tmp->va_end)
301 			p = &(*p)->rb_left;
302 		else if (va->va_end > tmp->va_start)
303 			p = &(*p)->rb_right;
304 		else
305 			BUG();
306 	}
307 
308 	rb_link_node(&va->rb_node, parent, p);
309 	rb_insert_color(&va->rb_node, &vmap_area_root);
310 
311 	/* address-sort this list so it is usable like the vmlist */
312 	tmp = rb_prev(&va->rb_node);
313 	if (tmp) {
314 		struct vmap_area *prev;
315 		prev = rb_entry(tmp, struct vmap_area, rb_node);
316 		list_add_rcu(&va->list, &prev->list);
317 	} else
318 		list_add_rcu(&va->list, &vmap_area_list);
319 }
320 
321 static void purge_vmap_area_lazy(void);
322 
323 /*
324  * Allocate a region of KVA of the specified size and alignment, within the
325  * vstart and vend.
326  */
327 static struct vmap_area *alloc_vmap_area(unsigned long size,
328 				unsigned long align,
329 				unsigned long vstart, unsigned long vend,
330 				int node, gfp_t gfp_mask)
331 {
332 	struct vmap_area *va;
333 	struct rb_node *n;
334 	unsigned long addr;
335 	int purged = 0;
336 
337 	BUG_ON(!size);
338 	BUG_ON(size & ~PAGE_MASK);
339 
340 	va = kmalloc_node(sizeof(struct vmap_area),
341 			gfp_mask & GFP_RECLAIM_MASK, node);
342 	if (unlikely(!va))
343 		return ERR_PTR(-ENOMEM);
344 
345 retry:
346 	addr = ALIGN(vstart, align);
347 
348 	spin_lock(&vmap_area_lock);
349 	if (addr + size - 1 < addr)
350 		goto overflow;
351 
352 	/* XXX: could have a last_hole cache */
353 	n = vmap_area_root.rb_node;
354 	if (n) {
355 		struct vmap_area *first = NULL;
356 
357 		do {
358 			struct vmap_area *tmp;
359 			tmp = rb_entry(n, struct vmap_area, rb_node);
360 			if (tmp->va_end >= addr) {
361 				if (!first && tmp->va_start < addr + size)
362 					first = tmp;
363 				n = n->rb_left;
364 			} else {
365 				first = tmp;
366 				n = n->rb_right;
367 			}
368 		} while (n);
369 
370 		if (!first)
371 			goto found;
372 
373 		if (first->va_end < addr) {
374 			n = rb_next(&first->rb_node);
375 			if (n)
376 				first = rb_entry(n, struct vmap_area, rb_node);
377 			else
378 				goto found;
379 		}
380 
381 		while (addr + size > first->va_start && addr + size <= vend) {
382 			addr = ALIGN(first->va_end + PAGE_SIZE, align);
383 			if (addr + size - 1 < addr)
384 				goto overflow;
385 
386 			n = rb_next(&first->rb_node);
387 			if (n)
388 				first = rb_entry(n, struct vmap_area, rb_node);
389 			else
390 				goto found;
391 		}
392 	}
393 found:
394 	if (addr + size > vend) {
395 overflow:
396 		spin_unlock(&vmap_area_lock);
397 		if (!purged) {
398 			purge_vmap_area_lazy();
399 			purged = 1;
400 			goto retry;
401 		}
402 		if (printk_ratelimit())
403 			printk(KERN_WARNING
404 				"vmap allocation for size %lu failed: "
405 				"use vmalloc=<size> to increase size.\n", size);
406 		kfree(va);
407 		return ERR_PTR(-EBUSY);
408 	}
409 
410 	BUG_ON(addr & (align-1));
411 
412 	va->va_start = addr;
413 	va->va_end = addr + size;
414 	va->flags = 0;
415 	__insert_vmap_area(va);
416 	spin_unlock(&vmap_area_lock);
417 
418 	return va;
419 }
420 
421 static void rcu_free_va(struct rcu_head *head)
422 {
423 	struct vmap_area *va = container_of(head, struct vmap_area, rcu_head);
424 
425 	kfree(va);
426 }
427 
428 static void __free_vmap_area(struct vmap_area *va)
429 {
430 	BUG_ON(RB_EMPTY_NODE(&va->rb_node));
431 	rb_erase(&va->rb_node, &vmap_area_root);
432 	RB_CLEAR_NODE(&va->rb_node);
433 	list_del_rcu(&va->list);
434 
435 	/*
436 	 * Track the highest possible candidate for pcpu area
437 	 * allocation.  Areas outside of vmalloc area can be returned
438 	 * here too, consider only end addresses which fall inside
439 	 * vmalloc area proper.
440 	 */
441 	if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
442 		vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
443 
444 	call_rcu(&va->rcu_head, rcu_free_va);
445 }
446 
447 /*
448  * Free a region of KVA allocated by alloc_vmap_area
449  */
450 static void free_vmap_area(struct vmap_area *va)
451 {
452 	spin_lock(&vmap_area_lock);
453 	__free_vmap_area(va);
454 	spin_unlock(&vmap_area_lock);
455 }
456 
457 /*
458  * Clear the pagetable entries of a given vmap_area
459  */
460 static void unmap_vmap_area(struct vmap_area *va)
461 {
462 	vunmap_page_range(va->va_start, va->va_end);
463 }
464 
465 static void vmap_debug_free_range(unsigned long start, unsigned long end)
466 {
467 	/*
468 	 * Unmap page tables and force a TLB flush immediately if
469 	 * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
470 	 * bugs similarly to those in linear kernel virtual address
471 	 * space after a page has been freed.
472 	 *
473 	 * All the lazy freeing logic is still retained, in order to
474 	 * minimise intrusiveness of this debugging feature.
475 	 *
476 	 * This is going to be *slow* (linear kernel virtual address
477 	 * debugging doesn't do a broadcast TLB flush so it is a lot
478 	 * faster).
479 	 */
480 #ifdef CONFIG_DEBUG_PAGEALLOC
481 	vunmap_page_range(start, end);
482 	flush_tlb_kernel_range(start, end);
483 #endif
484 }
485 
486 /*
487  * lazy_max_pages is the maximum amount of virtual address space we gather up
488  * before attempting to purge with a TLB flush.
489  *
490  * There is a tradeoff here: a larger number will cover more kernel page tables
491  * and take slightly longer to purge, but it will linearly reduce the number of
492  * global TLB flushes that must be performed. It would seem natural to scale
493  * this number up linearly with the number of CPUs (because vmapping activity
494  * could also scale linearly with the number of CPUs), however it is likely
495  * that in practice, workloads might be constrained in other ways that mean
496  * vmap activity will not scale linearly with CPUs. Also, I want to be
497  * conservative and not introduce a big latency on huge systems, so go with
498  * a less aggressive log scale. It will still be an improvement over the old
499  * code, and it will be simple to change the scale factor if we find that it
500  * becomes a problem on bigger systems.
501  */
502 static unsigned long lazy_max_pages(void)
503 {
504 	unsigned int log;
505 
506 	log = fls(num_online_cpus());
507 
508 	return log * (32UL * 1024 * 1024 / PAGE_SIZE);
509 }
510 
511 static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
512 
513 /*
514  * Purges all lazily-freed vmap areas.
515  *
516  * If sync is 0 then don't purge if there is already a purge in progress.
517  * If force_flush is 1, then flush kernel TLBs between *start and *end even
518  * if we found no lazy vmap areas to unmap (callers can use this to optimise
519  * their own TLB flushing).
520  * Returns with *start = min(*start, lowest purged address)
521  *              *end = max(*end, highest purged address)
522  */
523 static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
524 					int sync, int force_flush)
525 {
526 	static DEFINE_SPINLOCK(purge_lock);
527 	LIST_HEAD(valist);
528 	struct vmap_area *va;
529 	struct vmap_area *n_va;
530 	int nr = 0;
531 
532 	/*
533 	 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
534 	 * should not expect such behaviour. This just simplifies locking for
535 	 * the case that isn't actually used at the moment anyway.
536 	 */
537 	if (!sync && !force_flush) {
538 		if (!spin_trylock(&purge_lock))
539 			return;
540 	} else
541 		spin_lock(&purge_lock);
542 
543 	rcu_read_lock();
544 	list_for_each_entry_rcu(va, &vmap_area_list, list) {
545 		if (va->flags & VM_LAZY_FREE) {
546 			if (va->va_start < *start)
547 				*start = va->va_start;
548 			if (va->va_end > *end)
549 				*end = va->va_end;
550 			nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
551 			unmap_vmap_area(va);
552 			list_add_tail(&va->purge_list, &valist);
553 			va->flags |= VM_LAZY_FREEING;
554 			va->flags &= ~VM_LAZY_FREE;
555 		}
556 	}
557 	rcu_read_unlock();
558 
559 	if (nr) {
560 		BUG_ON(nr > atomic_read(&vmap_lazy_nr));
561 		atomic_sub(nr, &vmap_lazy_nr);
562 	}
563 
564 	if (nr || force_flush)
565 		flush_tlb_kernel_range(*start, *end);
566 
567 	if (nr) {
568 		spin_lock(&vmap_area_lock);
569 		list_for_each_entry_safe(va, n_va, &valist, purge_list)
570 			__free_vmap_area(va);
571 		spin_unlock(&vmap_area_lock);
572 	}
573 	spin_unlock(&purge_lock);
574 }
575 
576 /*
577  * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
578  * is already purging.
579  */
580 static void try_purge_vmap_area_lazy(void)
581 {
582 	unsigned long start = ULONG_MAX, end = 0;
583 
584 	__purge_vmap_area_lazy(&start, &end, 0, 0);
585 }
586 
587 /*
588  * Kick off a purge of the outstanding lazy areas.
589  */
590 static void purge_vmap_area_lazy(void)
591 {
592 	unsigned long start = ULONG_MAX, end = 0;
593 
594 	__purge_vmap_area_lazy(&start, &end, 1, 0);
595 }
596 
597 /*
598  * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
599  * called for the correct range previously.
600  */
601 static void free_unmap_vmap_area_noflush(struct vmap_area *va)
602 {
603 	va->flags |= VM_LAZY_FREE;
604 	atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
605 	if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
606 		try_purge_vmap_area_lazy();
607 }
608 
609 /*
610  * Free and unmap a vmap area
611  */
612 static void free_unmap_vmap_area(struct vmap_area *va)
613 {
614 	flush_cache_vunmap(va->va_start, va->va_end);
615 	free_unmap_vmap_area_noflush(va);
616 }
617 
618 static struct vmap_area *find_vmap_area(unsigned long addr)
619 {
620 	struct vmap_area *va;
621 
622 	spin_lock(&vmap_area_lock);
623 	va = __find_vmap_area(addr);
624 	spin_unlock(&vmap_area_lock);
625 
626 	return va;
627 }
628 
629 static void free_unmap_vmap_area_addr(unsigned long addr)
630 {
631 	struct vmap_area *va;
632 
633 	va = find_vmap_area(addr);
634 	BUG_ON(!va);
635 	free_unmap_vmap_area(va);
636 }
637 
638 
639 /*** Per cpu kva allocator ***/
640 
641 /*
642  * vmap space is limited especially on 32 bit architectures. Ensure there is
643  * room for at least 16 percpu vmap blocks per CPU.
644  */
645 /*
646  * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
647  * to #define VMALLOC_SPACE		(VMALLOC_END-VMALLOC_START). Guess
648  * instead (we just need a rough idea)
649  */
650 #if BITS_PER_LONG == 32
651 #define VMALLOC_SPACE		(128UL*1024*1024)
652 #else
653 #define VMALLOC_SPACE		(128UL*1024*1024*1024)
654 #endif
655 
656 #define VMALLOC_PAGES		(VMALLOC_SPACE / PAGE_SIZE)
657 #define VMAP_MAX_ALLOC		BITS_PER_LONG	/* 256K with 4K pages */
658 #define VMAP_BBMAP_BITS_MAX	1024	/* 4MB with 4K pages */
659 #define VMAP_BBMAP_BITS_MIN	(VMAP_MAX_ALLOC*2)
660 #define VMAP_MIN(x, y)		((x) < (y) ? (x) : (y)) /* can't use min() */
661 #define VMAP_MAX(x, y)		((x) > (y) ? (x) : (y)) /* can't use max() */
662 #define VMAP_BBMAP_BITS		VMAP_MIN(VMAP_BBMAP_BITS_MAX,		\
663 					VMAP_MAX(VMAP_BBMAP_BITS_MIN,	\
664 						VMALLOC_PAGES / NR_CPUS / 16))
665 
666 #define VMAP_BLOCK_SIZE		(VMAP_BBMAP_BITS * PAGE_SIZE)
667 
668 static bool vmap_initialized __read_mostly = false;
669 
670 struct vmap_block_queue {
671 	spinlock_t lock;
672 	struct list_head free;
673 	struct list_head dirty;
674 	unsigned int nr_dirty;
675 };
676 
677 struct vmap_block {
678 	spinlock_t lock;
679 	struct vmap_area *va;
680 	struct vmap_block_queue *vbq;
681 	unsigned long free, dirty;
682 	DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
683 	DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
684 	union {
685 		struct list_head free_list;
686 		struct rcu_head rcu_head;
687 	};
688 };
689 
690 /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
691 static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
692 
693 /*
694  * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
695  * in the free path. Could get rid of this if we change the API to return a
696  * "cookie" from alloc, to be passed to free. But no big deal yet.
697  */
698 static DEFINE_SPINLOCK(vmap_block_tree_lock);
699 static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
700 
701 /*
702  * We should probably have a fallback mechanism to allocate virtual memory
703  * out of partially filled vmap blocks. However vmap block sizing should be
704  * fairly reasonable according to the vmalloc size, so it shouldn't be a
705  * big problem.
706  */
707 
708 static unsigned long addr_to_vb_idx(unsigned long addr)
709 {
710 	addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
711 	addr /= VMAP_BLOCK_SIZE;
712 	return addr;
713 }
714 
715 static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
716 {
717 	struct vmap_block_queue *vbq;
718 	struct vmap_block *vb;
719 	struct vmap_area *va;
720 	unsigned long vb_idx;
721 	int node, err;
722 
723 	node = numa_node_id();
724 
725 	vb = kmalloc_node(sizeof(struct vmap_block),
726 			gfp_mask & GFP_RECLAIM_MASK, node);
727 	if (unlikely(!vb))
728 		return ERR_PTR(-ENOMEM);
729 
730 	va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
731 					VMALLOC_START, VMALLOC_END,
732 					node, gfp_mask);
733 	if (unlikely(IS_ERR(va))) {
734 		kfree(vb);
735 		return ERR_PTR(PTR_ERR(va));
736 	}
737 
738 	err = radix_tree_preload(gfp_mask);
739 	if (unlikely(err)) {
740 		kfree(vb);
741 		free_vmap_area(va);
742 		return ERR_PTR(err);
743 	}
744 
745 	spin_lock_init(&vb->lock);
746 	vb->va = va;
747 	vb->free = VMAP_BBMAP_BITS;
748 	vb->dirty = 0;
749 	bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
750 	bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
751 	INIT_LIST_HEAD(&vb->free_list);
752 
753 	vb_idx = addr_to_vb_idx(va->va_start);
754 	spin_lock(&vmap_block_tree_lock);
755 	err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
756 	spin_unlock(&vmap_block_tree_lock);
757 	BUG_ON(err);
758 	radix_tree_preload_end();
759 
760 	vbq = &get_cpu_var(vmap_block_queue);
761 	vb->vbq = vbq;
762 	spin_lock(&vbq->lock);
763 	list_add(&vb->free_list, &vbq->free);
764 	spin_unlock(&vbq->lock);
765 	put_cpu_var(vmap_cpu_blocks);
766 
767 	return vb;
768 }
769 
770 static void rcu_free_vb(struct rcu_head *head)
771 {
772 	struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
773 
774 	kfree(vb);
775 }
776 
777 static void free_vmap_block(struct vmap_block *vb)
778 {
779 	struct vmap_block *tmp;
780 	unsigned long vb_idx;
781 
782 	BUG_ON(!list_empty(&vb->free_list));
783 
784 	vb_idx = addr_to_vb_idx(vb->va->va_start);
785 	spin_lock(&vmap_block_tree_lock);
786 	tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
787 	spin_unlock(&vmap_block_tree_lock);
788 	BUG_ON(tmp != vb);
789 
790 	free_unmap_vmap_area_noflush(vb->va);
791 	call_rcu(&vb->rcu_head, rcu_free_vb);
792 }
793 
794 static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
795 {
796 	struct vmap_block_queue *vbq;
797 	struct vmap_block *vb;
798 	unsigned long addr = 0;
799 	unsigned int order;
800 
801 	BUG_ON(size & ~PAGE_MASK);
802 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
803 	order = get_order(size);
804 
805 again:
806 	rcu_read_lock();
807 	vbq = &get_cpu_var(vmap_block_queue);
808 	list_for_each_entry_rcu(vb, &vbq->free, free_list) {
809 		int i;
810 
811 		spin_lock(&vb->lock);
812 		i = bitmap_find_free_region(vb->alloc_map,
813 						VMAP_BBMAP_BITS, order);
814 
815 		if (i >= 0) {
816 			addr = vb->va->va_start + (i << PAGE_SHIFT);
817 			BUG_ON(addr_to_vb_idx(addr) !=
818 					addr_to_vb_idx(vb->va->va_start));
819 			vb->free -= 1UL << order;
820 			if (vb->free == 0) {
821 				spin_lock(&vbq->lock);
822 				list_del_init(&vb->free_list);
823 				spin_unlock(&vbq->lock);
824 			}
825 			spin_unlock(&vb->lock);
826 			break;
827 		}
828 		spin_unlock(&vb->lock);
829 	}
830 	put_cpu_var(vmap_cpu_blocks);
831 	rcu_read_unlock();
832 
833 	if (!addr) {
834 		vb = new_vmap_block(gfp_mask);
835 		if (IS_ERR(vb))
836 			return vb;
837 		goto again;
838 	}
839 
840 	return (void *)addr;
841 }
842 
843 static void vb_free(const void *addr, unsigned long size)
844 {
845 	unsigned long offset;
846 	unsigned long vb_idx;
847 	unsigned int order;
848 	struct vmap_block *vb;
849 
850 	BUG_ON(size & ~PAGE_MASK);
851 	BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
852 
853 	flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
854 
855 	order = get_order(size);
856 
857 	offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
858 
859 	vb_idx = addr_to_vb_idx((unsigned long)addr);
860 	rcu_read_lock();
861 	vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
862 	rcu_read_unlock();
863 	BUG_ON(!vb);
864 
865 	spin_lock(&vb->lock);
866 	bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
867 
868 	vb->dirty += 1UL << order;
869 	if (vb->dirty == VMAP_BBMAP_BITS) {
870 		BUG_ON(vb->free || !list_empty(&vb->free_list));
871 		spin_unlock(&vb->lock);
872 		free_vmap_block(vb);
873 	} else
874 		spin_unlock(&vb->lock);
875 }
876 
877 /**
878  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
879  *
880  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
881  * to amortize TLB flushing overheads. What this means is that any page you
882  * have now, may, in a former life, have been mapped into kernel virtual
883  * address by the vmap layer and so there might be some CPUs with TLB entries
884  * still referencing that page (additional to the regular 1:1 kernel mapping).
885  *
886  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
887  * be sure that none of the pages we have control over will have any aliases
888  * from the vmap layer.
889  */
890 void vm_unmap_aliases(void)
891 {
892 	unsigned long start = ULONG_MAX, end = 0;
893 	int cpu;
894 	int flush = 0;
895 
896 	if (unlikely(!vmap_initialized))
897 		return;
898 
899 	for_each_possible_cpu(cpu) {
900 		struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
901 		struct vmap_block *vb;
902 
903 		rcu_read_lock();
904 		list_for_each_entry_rcu(vb, &vbq->free, free_list) {
905 			int i;
906 
907 			spin_lock(&vb->lock);
908 			i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
909 			while (i < VMAP_BBMAP_BITS) {
910 				unsigned long s, e;
911 				int j;
912 				j = find_next_zero_bit(vb->dirty_map,
913 					VMAP_BBMAP_BITS, i);
914 
915 				s = vb->va->va_start + (i << PAGE_SHIFT);
916 				e = vb->va->va_start + (j << PAGE_SHIFT);
917 				vunmap_page_range(s, e);
918 				flush = 1;
919 
920 				if (s < start)
921 					start = s;
922 				if (e > end)
923 					end = e;
924 
925 				i = j;
926 				i = find_next_bit(vb->dirty_map,
927 							VMAP_BBMAP_BITS, i);
928 			}
929 			spin_unlock(&vb->lock);
930 		}
931 		rcu_read_unlock();
932 	}
933 
934 	__purge_vmap_area_lazy(&start, &end, 1, flush);
935 }
936 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
937 
938 /**
939  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
940  * @mem: the pointer returned by vm_map_ram
941  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
942  */
943 void vm_unmap_ram(const void *mem, unsigned int count)
944 {
945 	unsigned long size = count << PAGE_SHIFT;
946 	unsigned long addr = (unsigned long)mem;
947 
948 	BUG_ON(!addr);
949 	BUG_ON(addr < VMALLOC_START);
950 	BUG_ON(addr > VMALLOC_END);
951 	BUG_ON(addr & (PAGE_SIZE-1));
952 
953 	debug_check_no_locks_freed(mem, size);
954 	vmap_debug_free_range(addr, addr+size);
955 
956 	if (likely(count <= VMAP_MAX_ALLOC))
957 		vb_free(mem, size);
958 	else
959 		free_unmap_vmap_area_addr(addr);
960 }
961 EXPORT_SYMBOL(vm_unmap_ram);
962 
963 /**
964  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
965  * @pages: an array of pointers to the pages to be mapped
966  * @count: number of pages
967  * @node: prefer to allocate data structures on this node
968  * @prot: memory protection to use. PAGE_KERNEL for regular RAM
969  *
970  * Returns: a pointer to the address that has been mapped, or %NULL on failure
971  */
972 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
973 {
974 	unsigned long size = count << PAGE_SHIFT;
975 	unsigned long addr;
976 	void *mem;
977 
978 	if (likely(count <= VMAP_MAX_ALLOC)) {
979 		mem = vb_alloc(size, GFP_KERNEL);
980 		if (IS_ERR(mem))
981 			return NULL;
982 		addr = (unsigned long)mem;
983 	} else {
984 		struct vmap_area *va;
985 		va = alloc_vmap_area(size, PAGE_SIZE,
986 				VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
987 		if (IS_ERR(va))
988 			return NULL;
989 
990 		addr = va->va_start;
991 		mem = (void *)addr;
992 	}
993 	if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
994 		vm_unmap_ram(mem, count);
995 		return NULL;
996 	}
997 	return mem;
998 }
999 EXPORT_SYMBOL(vm_map_ram);
1000 
1001 /**
1002  * vm_area_register_early - register vmap area early during boot
1003  * @vm: vm_struct to register
1004  * @align: requested alignment
1005  *
1006  * This function is used to register kernel vm area before
1007  * vmalloc_init() is called.  @vm->size and @vm->flags should contain
1008  * proper values on entry and other fields should be zero.  On return,
1009  * vm->addr contains the allocated address.
1010  *
1011  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1012  */
1013 void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1014 {
1015 	static size_t vm_init_off __initdata;
1016 	unsigned long addr;
1017 
1018 	addr = ALIGN(VMALLOC_START + vm_init_off, align);
1019 	vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1020 
1021 	vm->addr = (void *)addr;
1022 
1023 	vm->next = vmlist;
1024 	vmlist = vm;
1025 }
1026 
1027 void __init vmalloc_init(void)
1028 {
1029 	struct vmap_area *va;
1030 	struct vm_struct *tmp;
1031 	int i;
1032 
1033 	for_each_possible_cpu(i) {
1034 		struct vmap_block_queue *vbq;
1035 
1036 		vbq = &per_cpu(vmap_block_queue, i);
1037 		spin_lock_init(&vbq->lock);
1038 		INIT_LIST_HEAD(&vbq->free);
1039 		INIT_LIST_HEAD(&vbq->dirty);
1040 		vbq->nr_dirty = 0;
1041 	}
1042 
1043 	/* Import existing vmlist entries. */
1044 	for (tmp = vmlist; tmp; tmp = tmp->next) {
1045 		va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
1046 		va->flags = tmp->flags | VM_VM_AREA;
1047 		va->va_start = (unsigned long)tmp->addr;
1048 		va->va_end = va->va_start + tmp->size;
1049 		__insert_vmap_area(va);
1050 	}
1051 
1052 	vmap_area_pcpu_hole = VMALLOC_END;
1053 
1054 	vmap_initialized = true;
1055 }
1056 
1057 /**
1058  * map_kernel_range_noflush - map kernel VM area with the specified pages
1059  * @addr: start of the VM area to map
1060  * @size: size of the VM area to map
1061  * @prot: page protection flags to use
1062  * @pages: pages to map
1063  *
1064  * Map PFN_UP(@size) pages at @addr.  The VM area @addr and @size
1065  * specify should have been allocated using get_vm_area() and its
1066  * friends.
1067  *
1068  * NOTE:
1069  * This function does NOT do any cache flushing.  The caller is
1070  * responsible for calling flush_cache_vmap() on to-be-mapped areas
1071  * before calling this function.
1072  *
1073  * RETURNS:
1074  * The number of pages mapped on success, -errno on failure.
1075  */
1076 int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1077 			     pgprot_t prot, struct page **pages)
1078 {
1079 	return vmap_page_range_noflush(addr, addr + size, prot, pages);
1080 }
1081 
1082 /**
1083  * unmap_kernel_range_noflush - unmap kernel VM area
1084  * @addr: start of the VM area to unmap
1085  * @size: size of the VM area to unmap
1086  *
1087  * Unmap PFN_UP(@size) pages at @addr.  The VM area @addr and @size
1088  * specify should have been allocated using get_vm_area() and its
1089  * friends.
1090  *
1091  * NOTE:
1092  * This function does NOT do any cache flushing.  The caller is
1093  * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1094  * before calling this function and flush_tlb_kernel_range() after.
1095  */
1096 void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1097 {
1098 	vunmap_page_range(addr, addr + size);
1099 }
1100 
1101 /**
1102  * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1103  * @addr: start of the VM area to unmap
1104  * @size: size of the VM area to unmap
1105  *
1106  * Similar to unmap_kernel_range_noflush() but flushes vcache before
1107  * the unmapping and tlb after.
1108  */
1109 void unmap_kernel_range(unsigned long addr, unsigned long size)
1110 {
1111 	unsigned long end = addr + size;
1112 
1113 	flush_cache_vunmap(addr, end);
1114 	vunmap_page_range(addr, end);
1115 	flush_tlb_kernel_range(addr, end);
1116 }
1117 
1118 int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
1119 {
1120 	unsigned long addr = (unsigned long)area->addr;
1121 	unsigned long end = addr + area->size - PAGE_SIZE;
1122 	int err;
1123 
1124 	err = vmap_page_range(addr, end, prot, *pages);
1125 	if (err > 0) {
1126 		*pages += err;
1127 		err = 0;
1128 	}
1129 
1130 	return err;
1131 }
1132 EXPORT_SYMBOL_GPL(map_vm_area);
1133 
1134 /*** Old vmalloc interfaces ***/
1135 DEFINE_RWLOCK(vmlist_lock);
1136 struct vm_struct *vmlist;
1137 
1138 static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
1139 			      unsigned long flags, void *caller)
1140 {
1141 	struct vm_struct *tmp, **p;
1142 
1143 	vm->flags = flags;
1144 	vm->addr = (void *)va->va_start;
1145 	vm->size = va->va_end - va->va_start;
1146 	vm->caller = caller;
1147 	va->private = vm;
1148 	va->flags |= VM_VM_AREA;
1149 
1150 	write_lock(&vmlist_lock);
1151 	for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1152 		if (tmp->addr >= vm->addr)
1153 			break;
1154 	}
1155 	vm->next = *p;
1156 	*p = vm;
1157 	write_unlock(&vmlist_lock);
1158 }
1159 
1160 static struct vm_struct *__get_vm_area_node(unsigned long size,
1161 		unsigned long flags, unsigned long start, unsigned long end,
1162 		int node, gfp_t gfp_mask, void *caller)
1163 {
1164 	static struct vmap_area *va;
1165 	struct vm_struct *area;
1166 	unsigned long align = 1;
1167 
1168 	BUG_ON(in_interrupt());
1169 	if (flags & VM_IOREMAP) {
1170 		int bit = fls(size);
1171 
1172 		if (bit > IOREMAP_MAX_ORDER)
1173 			bit = IOREMAP_MAX_ORDER;
1174 		else if (bit < PAGE_SHIFT)
1175 			bit = PAGE_SHIFT;
1176 
1177 		align = 1ul << bit;
1178 	}
1179 
1180 	size = PAGE_ALIGN(size);
1181 	if (unlikely(!size))
1182 		return NULL;
1183 
1184 	area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
1185 	if (unlikely(!area))
1186 		return NULL;
1187 
1188 	/*
1189 	 * We always allocate a guard page.
1190 	 */
1191 	size += PAGE_SIZE;
1192 
1193 	va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1194 	if (IS_ERR(va)) {
1195 		kfree(area);
1196 		return NULL;
1197 	}
1198 
1199 	insert_vmalloc_vm(area, va, flags, caller);
1200 	return area;
1201 }
1202 
1203 struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1204 				unsigned long start, unsigned long end)
1205 {
1206 	return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1207 						__builtin_return_address(0));
1208 }
1209 EXPORT_SYMBOL_GPL(__get_vm_area);
1210 
1211 struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1212 				       unsigned long start, unsigned long end,
1213 				       void *caller)
1214 {
1215 	return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1216 				  caller);
1217 }
1218 
1219 /**
1220  *	get_vm_area  -  reserve a contiguous kernel virtual area
1221  *	@size:		size of the area
1222  *	@flags:		%VM_IOREMAP for I/O mappings or VM_ALLOC
1223  *
1224  *	Search an area of @size in the kernel virtual mapping area,
1225  *	and reserved it for out purposes.  Returns the area descriptor
1226  *	on success or %NULL on failure.
1227  */
1228 struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
1229 {
1230 	return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
1231 				-1, GFP_KERNEL, __builtin_return_address(0));
1232 }
1233 
1234 struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
1235 				void *caller)
1236 {
1237 	return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
1238 						-1, GFP_KERNEL, caller);
1239 }
1240 
1241 struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
1242 				   int node, gfp_t gfp_mask)
1243 {
1244 	return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
1245 				  gfp_mask, __builtin_return_address(0));
1246 }
1247 
1248 static struct vm_struct *find_vm_area(const void *addr)
1249 {
1250 	struct vmap_area *va;
1251 
1252 	va = find_vmap_area((unsigned long)addr);
1253 	if (va && va->flags & VM_VM_AREA)
1254 		return va->private;
1255 
1256 	return NULL;
1257 }
1258 
1259 /**
1260  *	remove_vm_area  -  find and remove a continuous kernel virtual area
1261  *	@addr:		base address
1262  *
1263  *	Search for the kernel VM area starting at @addr, and remove it.
1264  *	This function returns the found VM area, but using it is NOT safe
1265  *	on SMP machines, except for its size or flags.
1266  */
1267 struct vm_struct *remove_vm_area(const void *addr)
1268 {
1269 	struct vmap_area *va;
1270 
1271 	va = find_vmap_area((unsigned long)addr);
1272 	if (va && va->flags & VM_VM_AREA) {
1273 		struct vm_struct *vm = va->private;
1274 		struct vm_struct *tmp, **p;
1275 
1276 		vmap_debug_free_range(va->va_start, va->va_end);
1277 		free_unmap_vmap_area(va);
1278 		vm->size -= PAGE_SIZE;
1279 
1280 		write_lock(&vmlist_lock);
1281 		for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
1282 			;
1283 		*p = tmp->next;
1284 		write_unlock(&vmlist_lock);
1285 
1286 		return vm;
1287 	}
1288 	return NULL;
1289 }
1290 
1291 static void __vunmap(const void *addr, int deallocate_pages)
1292 {
1293 	struct vm_struct *area;
1294 
1295 	if (!addr)
1296 		return;
1297 
1298 	if ((PAGE_SIZE-1) & (unsigned long)addr) {
1299 		WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
1300 		return;
1301 	}
1302 
1303 	area = remove_vm_area(addr);
1304 	if (unlikely(!area)) {
1305 		WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
1306 				addr);
1307 		return;
1308 	}
1309 
1310 	debug_check_no_locks_freed(addr, area->size);
1311 	debug_check_no_obj_freed(addr, area->size);
1312 
1313 	if (deallocate_pages) {
1314 		int i;
1315 
1316 		for (i = 0; i < area->nr_pages; i++) {
1317 			struct page *page = area->pages[i];
1318 
1319 			BUG_ON(!page);
1320 			__free_page(page);
1321 		}
1322 
1323 		if (area->flags & VM_VPAGES)
1324 			vfree(area->pages);
1325 		else
1326 			kfree(area->pages);
1327 	}
1328 
1329 	kfree(area);
1330 	return;
1331 }
1332 
1333 /**
1334  *	vfree  -  release memory allocated by vmalloc()
1335  *	@addr:		memory base address
1336  *
1337  *	Free the virtually continuous memory area starting at @addr, as
1338  *	obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1339  *	NULL, no operation is performed.
1340  *
1341  *	Must not be called in interrupt context.
1342  */
1343 void vfree(const void *addr)
1344 {
1345 	BUG_ON(in_interrupt());
1346 
1347 	kmemleak_free(addr);
1348 
1349 	__vunmap(addr, 1);
1350 }
1351 EXPORT_SYMBOL(vfree);
1352 
1353 /**
1354  *	vunmap  -  release virtual mapping obtained by vmap()
1355  *	@addr:		memory base address
1356  *
1357  *	Free the virtually contiguous memory area starting at @addr,
1358  *	which was created from the page array passed to vmap().
1359  *
1360  *	Must not be called in interrupt context.
1361  */
1362 void vunmap(const void *addr)
1363 {
1364 	BUG_ON(in_interrupt());
1365 	might_sleep();
1366 	__vunmap(addr, 0);
1367 }
1368 EXPORT_SYMBOL(vunmap);
1369 
1370 /**
1371  *	vmap  -  map an array of pages into virtually contiguous space
1372  *	@pages:		array of page pointers
1373  *	@count:		number of pages to map
1374  *	@flags:		vm_area->flags
1375  *	@prot:		page protection for the mapping
1376  *
1377  *	Maps @count pages from @pages into contiguous kernel virtual
1378  *	space.
1379  */
1380 void *vmap(struct page **pages, unsigned int count,
1381 		unsigned long flags, pgprot_t prot)
1382 {
1383 	struct vm_struct *area;
1384 
1385 	might_sleep();
1386 
1387 	if (count > num_physpages)
1388 		return NULL;
1389 
1390 	area = get_vm_area_caller((count << PAGE_SHIFT), flags,
1391 					__builtin_return_address(0));
1392 	if (!area)
1393 		return NULL;
1394 
1395 	if (map_vm_area(area, prot, &pages)) {
1396 		vunmap(area->addr);
1397 		return NULL;
1398 	}
1399 
1400 	return area->addr;
1401 }
1402 EXPORT_SYMBOL(vmap);
1403 
1404 static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1405 			    int node, void *caller);
1406 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
1407 				 pgprot_t prot, int node, void *caller)
1408 {
1409 	struct page **pages;
1410 	unsigned int nr_pages, array_size, i;
1411 
1412 	nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
1413 	array_size = (nr_pages * sizeof(struct page *));
1414 
1415 	area->nr_pages = nr_pages;
1416 	/* Please note that the recursion is strictly bounded. */
1417 	if (array_size > PAGE_SIZE) {
1418 		pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
1419 				PAGE_KERNEL, node, caller);
1420 		area->flags |= VM_VPAGES;
1421 	} else {
1422 		pages = kmalloc_node(array_size,
1423 				(gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
1424 				node);
1425 	}
1426 	area->pages = pages;
1427 	area->caller = caller;
1428 	if (!area->pages) {
1429 		remove_vm_area(area->addr);
1430 		kfree(area);
1431 		return NULL;
1432 	}
1433 
1434 	for (i = 0; i < area->nr_pages; i++) {
1435 		struct page *page;
1436 
1437 		if (node < 0)
1438 			page = alloc_page(gfp_mask);
1439 		else
1440 			page = alloc_pages_node(node, gfp_mask, 0);
1441 
1442 		if (unlikely(!page)) {
1443 			/* Successfully allocated i pages, free them in __vunmap() */
1444 			area->nr_pages = i;
1445 			goto fail;
1446 		}
1447 		area->pages[i] = page;
1448 	}
1449 
1450 	if (map_vm_area(area, prot, &pages))
1451 		goto fail;
1452 	return area->addr;
1453 
1454 fail:
1455 	vfree(area->addr);
1456 	return NULL;
1457 }
1458 
1459 void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
1460 {
1461 	void *addr = __vmalloc_area_node(area, gfp_mask, prot, -1,
1462 					 __builtin_return_address(0));
1463 
1464 	/*
1465 	 * A ref_count = 3 is needed because the vm_struct and vmap_area
1466 	 * structures allocated in the __get_vm_area_node() function contain
1467 	 * references to the virtual address of the vmalloc'ed block.
1468 	 */
1469 	kmemleak_alloc(addr, area->size - PAGE_SIZE, 3, gfp_mask);
1470 
1471 	return addr;
1472 }
1473 
1474 /**
1475  *	__vmalloc_node  -  allocate virtually contiguous memory
1476  *	@size:		allocation size
1477  *	@gfp_mask:	flags for the page level allocator
1478  *	@prot:		protection mask for the allocated pages
1479  *	@node:		node to use for allocation or -1
1480  *	@caller:	caller's return address
1481  *
1482  *	Allocate enough pages to cover @size from the page level
1483  *	allocator with @gfp_mask flags.  Map them into contiguous
1484  *	kernel virtual space, using a pagetable protection of @prot.
1485  */
1486 static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1487 						int node, void *caller)
1488 {
1489 	struct vm_struct *area;
1490 	void *addr;
1491 	unsigned long real_size = size;
1492 
1493 	size = PAGE_ALIGN(size);
1494 	if (!size || (size >> PAGE_SHIFT) > num_physpages)
1495 		return NULL;
1496 
1497 	area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
1498 						node, gfp_mask, caller);
1499 
1500 	if (!area)
1501 		return NULL;
1502 
1503 	addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
1504 
1505 	/*
1506 	 * A ref_count = 3 is needed because the vm_struct and vmap_area
1507 	 * structures allocated in the __get_vm_area_node() function contain
1508 	 * references to the virtual address of the vmalloc'ed block.
1509 	 */
1510 	kmemleak_alloc(addr, real_size, 3, gfp_mask);
1511 
1512 	return addr;
1513 }
1514 
1515 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1516 {
1517 	return __vmalloc_node(size, gfp_mask, prot, -1,
1518 				__builtin_return_address(0));
1519 }
1520 EXPORT_SYMBOL(__vmalloc);
1521 
1522 /**
1523  *	vmalloc  -  allocate virtually contiguous memory
1524  *	@size:		allocation size
1525  *	Allocate enough pages to cover @size from the page level
1526  *	allocator and map them into contiguous kernel virtual space.
1527  *
1528  *	For tight control over page level allocator and protection flags
1529  *	use __vmalloc() instead.
1530  */
1531 void *vmalloc(unsigned long size)
1532 {
1533 	return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1534 					-1, __builtin_return_address(0));
1535 }
1536 EXPORT_SYMBOL(vmalloc);
1537 
1538 /**
1539  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1540  * @size: allocation size
1541  *
1542  * The resulting memory area is zeroed so it can be mapped to userspace
1543  * without leaking data.
1544  */
1545 void *vmalloc_user(unsigned long size)
1546 {
1547 	struct vm_struct *area;
1548 	void *ret;
1549 
1550 	ret = __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
1551 			     PAGE_KERNEL, -1, __builtin_return_address(0));
1552 	if (ret) {
1553 		area = find_vm_area(ret);
1554 		area->flags |= VM_USERMAP;
1555 	}
1556 	return ret;
1557 }
1558 EXPORT_SYMBOL(vmalloc_user);
1559 
1560 /**
1561  *	vmalloc_node  -  allocate memory on a specific node
1562  *	@size:		allocation size
1563  *	@node:		numa node
1564  *
1565  *	Allocate enough pages to cover @size from the page level
1566  *	allocator and map them into contiguous kernel virtual space.
1567  *
1568  *	For tight control over page level allocator and protection flags
1569  *	use __vmalloc() instead.
1570  */
1571 void *vmalloc_node(unsigned long size, int node)
1572 {
1573 	return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1574 					node, __builtin_return_address(0));
1575 }
1576 EXPORT_SYMBOL(vmalloc_node);
1577 
1578 #ifndef PAGE_KERNEL_EXEC
1579 # define PAGE_KERNEL_EXEC PAGE_KERNEL
1580 #endif
1581 
1582 /**
1583  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
1584  *	@size:		allocation size
1585  *
1586  *	Kernel-internal function to allocate enough pages to cover @size
1587  *	the page level allocator and map them into contiguous and
1588  *	executable kernel virtual space.
1589  *
1590  *	For tight control over page level allocator and protection flags
1591  *	use __vmalloc() instead.
1592  */
1593 
1594 void *vmalloc_exec(unsigned long size)
1595 {
1596 	return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
1597 			      -1, __builtin_return_address(0));
1598 }
1599 
1600 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
1601 #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
1602 #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
1603 #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
1604 #else
1605 #define GFP_VMALLOC32 GFP_KERNEL
1606 #endif
1607 
1608 /**
1609  *	vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
1610  *	@size:		allocation size
1611  *
1612  *	Allocate enough 32bit PA addressable pages to cover @size from the
1613  *	page level allocator and map them into contiguous kernel virtual space.
1614  */
1615 void *vmalloc_32(unsigned long size)
1616 {
1617 	return __vmalloc_node(size, GFP_VMALLOC32, PAGE_KERNEL,
1618 			      -1, __builtin_return_address(0));
1619 }
1620 EXPORT_SYMBOL(vmalloc_32);
1621 
1622 /**
1623  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
1624  *	@size:		allocation size
1625  *
1626  * The resulting memory area is 32bit addressable and zeroed so it can be
1627  * mapped to userspace without leaking data.
1628  */
1629 void *vmalloc_32_user(unsigned long size)
1630 {
1631 	struct vm_struct *area;
1632 	void *ret;
1633 
1634 	ret = __vmalloc_node(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
1635 			     -1, __builtin_return_address(0));
1636 	if (ret) {
1637 		area = find_vm_area(ret);
1638 		area->flags |= VM_USERMAP;
1639 	}
1640 	return ret;
1641 }
1642 EXPORT_SYMBOL(vmalloc_32_user);
1643 
1644 long vread(char *buf, char *addr, unsigned long count)
1645 {
1646 	struct vm_struct *tmp;
1647 	char *vaddr, *buf_start = buf;
1648 	unsigned long n;
1649 
1650 	/* Don't allow overflow */
1651 	if ((unsigned long) addr + count < count)
1652 		count = -(unsigned long) addr;
1653 
1654 	read_lock(&vmlist_lock);
1655 	for (tmp = vmlist; tmp; tmp = tmp->next) {
1656 		vaddr = (char *) tmp->addr;
1657 		if (addr >= vaddr + tmp->size - PAGE_SIZE)
1658 			continue;
1659 		while (addr < vaddr) {
1660 			if (count == 0)
1661 				goto finished;
1662 			*buf = '\0';
1663 			buf++;
1664 			addr++;
1665 			count--;
1666 		}
1667 		n = vaddr + tmp->size - PAGE_SIZE - addr;
1668 		do {
1669 			if (count == 0)
1670 				goto finished;
1671 			*buf = *addr;
1672 			buf++;
1673 			addr++;
1674 			count--;
1675 		} while (--n > 0);
1676 	}
1677 finished:
1678 	read_unlock(&vmlist_lock);
1679 	return buf - buf_start;
1680 }
1681 
1682 long vwrite(char *buf, char *addr, unsigned long count)
1683 {
1684 	struct vm_struct *tmp;
1685 	char *vaddr, *buf_start = buf;
1686 	unsigned long n;
1687 
1688 	/* Don't allow overflow */
1689 	if ((unsigned long) addr + count < count)
1690 		count = -(unsigned long) addr;
1691 
1692 	read_lock(&vmlist_lock);
1693 	for (tmp = vmlist; tmp; tmp = tmp->next) {
1694 		vaddr = (char *) tmp->addr;
1695 		if (addr >= vaddr + tmp->size - PAGE_SIZE)
1696 			continue;
1697 		while (addr < vaddr) {
1698 			if (count == 0)
1699 				goto finished;
1700 			buf++;
1701 			addr++;
1702 			count--;
1703 		}
1704 		n = vaddr + tmp->size - PAGE_SIZE - addr;
1705 		do {
1706 			if (count == 0)
1707 				goto finished;
1708 			*addr = *buf;
1709 			buf++;
1710 			addr++;
1711 			count--;
1712 		} while (--n > 0);
1713 	}
1714 finished:
1715 	read_unlock(&vmlist_lock);
1716 	return buf - buf_start;
1717 }
1718 
1719 /**
1720  *	remap_vmalloc_range  -  map vmalloc pages to userspace
1721  *	@vma:		vma to cover (map full range of vma)
1722  *	@addr:		vmalloc memory
1723  *	@pgoff:		number of pages into addr before first page to map
1724  *
1725  *	Returns:	0 for success, -Exxx on failure
1726  *
1727  *	This function checks that addr is a valid vmalloc'ed area, and
1728  *	that it is big enough to cover the vma. Will return failure if
1729  *	that criteria isn't met.
1730  *
1731  *	Similar to remap_pfn_range() (see mm/memory.c)
1732  */
1733 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1734 						unsigned long pgoff)
1735 {
1736 	struct vm_struct *area;
1737 	unsigned long uaddr = vma->vm_start;
1738 	unsigned long usize = vma->vm_end - vma->vm_start;
1739 
1740 	if ((PAGE_SIZE-1) & (unsigned long)addr)
1741 		return -EINVAL;
1742 
1743 	area = find_vm_area(addr);
1744 	if (!area)
1745 		return -EINVAL;
1746 
1747 	if (!(area->flags & VM_USERMAP))
1748 		return -EINVAL;
1749 
1750 	if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
1751 		return -EINVAL;
1752 
1753 	addr += pgoff << PAGE_SHIFT;
1754 	do {
1755 		struct page *page = vmalloc_to_page(addr);
1756 		int ret;
1757 
1758 		ret = vm_insert_page(vma, uaddr, page);
1759 		if (ret)
1760 			return ret;
1761 
1762 		uaddr += PAGE_SIZE;
1763 		addr += PAGE_SIZE;
1764 		usize -= PAGE_SIZE;
1765 	} while (usize > 0);
1766 
1767 	/* Prevent "things" like memory migration? VM_flags need a cleanup... */
1768 	vma->vm_flags |= VM_RESERVED;
1769 
1770 	return 0;
1771 }
1772 EXPORT_SYMBOL(remap_vmalloc_range);
1773 
1774 /*
1775  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
1776  * have one.
1777  */
1778 void  __attribute__((weak)) vmalloc_sync_all(void)
1779 {
1780 }
1781 
1782 
1783 static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
1784 {
1785 	/* apply_to_page_range() does all the hard work. */
1786 	return 0;
1787 }
1788 
1789 /**
1790  *	alloc_vm_area - allocate a range of kernel address space
1791  *	@size:		size of the area
1792  *
1793  *	Returns:	NULL on failure, vm_struct on success
1794  *
1795  *	This function reserves a range of kernel address space, and
1796  *	allocates pagetables to map that range.  No actual mappings
1797  *	are created.  If the kernel address space is not shared
1798  *	between processes, it syncs the pagetable across all
1799  *	processes.
1800  */
1801 struct vm_struct *alloc_vm_area(size_t size)
1802 {
1803 	struct vm_struct *area;
1804 
1805 	area = get_vm_area_caller(size, VM_IOREMAP,
1806 				__builtin_return_address(0));
1807 	if (area == NULL)
1808 		return NULL;
1809 
1810 	/*
1811 	 * This ensures that page tables are constructed for this region
1812 	 * of kernel virtual address space and mapped into init_mm.
1813 	 */
1814 	if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
1815 				area->size, f, NULL)) {
1816 		free_vm_area(area);
1817 		return NULL;
1818 	}
1819 
1820 	/* Make sure the pagetables are constructed in process kernel
1821 	   mappings */
1822 	vmalloc_sync_all();
1823 
1824 	return area;
1825 }
1826 EXPORT_SYMBOL_GPL(alloc_vm_area);
1827 
1828 void free_vm_area(struct vm_struct *area)
1829 {
1830 	struct vm_struct *ret;
1831 	ret = remove_vm_area(area->addr);
1832 	BUG_ON(ret != area);
1833 	kfree(area);
1834 }
1835 EXPORT_SYMBOL_GPL(free_vm_area);
1836 
1837 static struct vmap_area *node_to_va(struct rb_node *n)
1838 {
1839 	return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
1840 }
1841 
1842 /**
1843  * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
1844  * @end: target address
1845  * @pnext: out arg for the next vmap_area
1846  * @pprev: out arg for the previous vmap_area
1847  *
1848  * Returns: %true if either or both of next and prev are found,
1849  *	    %false if no vmap_area exists
1850  *
1851  * Find vmap_areas end addresses of which enclose @end.  ie. if not
1852  * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
1853  */
1854 static bool pvm_find_next_prev(unsigned long end,
1855 			       struct vmap_area **pnext,
1856 			       struct vmap_area **pprev)
1857 {
1858 	struct rb_node *n = vmap_area_root.rb_node;
1859 	struct vmap_area *va = NULL;
1860 
1861 	while (n) {
1862 		va = rb_entry(n, struct vmap_area, rb_node);
1863 		if (end < va->va_end)
1864 			n = n->rb_left;
1865 		else if (end > va->va_end)
1866 			n = n->rb_right;
1867 		else
1868 			break;
1869 	}
1870 
1871 	if (!va)
1872 		return false;
1873 
1874 	if (va->va_end > end) {
1875 		*pnext = va;
1876 		*pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
1877 	} else {
1878 		*pprev = va;
1879 		*pnext = node_to_va(rb_next(&(*pprev)->rb_node));
1880 	}
1881 	return true;
1882 }
1883 
1884 /**
1885  * pvm_determine_end - find the highest aligned address between two vmap_areas
1886  * @pnext: in/out arg for the next vmap_area
1887  * @pprev: in/out arg for the previous vmap_area
1888  * @align: alignment
1889  *
1890  * Returns: determined end address
1891  *
1892  * Find the highest aligned address between *@pnext and *@pprev below
1893  * VMALLOC_END.  *@pnext and *@pprev are adjusted so that the aligned
1894  * down address is between the end addresses of the two vmap_areas.
1895  *
1896  * Please note that the address returned by this function may fall
1897  * inside *@pnext vmap_area.  The caller is responsible for checking
1898  * that.
1899  */
1900 static unsigned long pvm_determine_end(struct vmap_area **pnext,
1901 				       struct vmap_area **pprev,
1902 				       unsigned long align)
1903 {
1904 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
1905 	unsigned long addr;
1906 
1907 	if (*pnext)
1908 		addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
1909 	else
1910 		addr = vmalloc_end;
1911 
1912 	while (*pprev && (*pprev)->va_end > addr) {
1913 		*pnext = *pprev;
1914 		*pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
1915 	}
1916 
1917 	return addr;
1918 }
1919 
1920 /**
1921  * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
1922  * @offsets: array containing offset of each area
1923  * @sizes: array containing size of each area
1924  * @nr_vms: the number of areas to allocate
1925  * @align: alignment, all entries in @offsets and @sizes must be aligned to this
1926  * @gfp_mask: allocation mask
1927  *
1928  * Returns: kmalloc'd vm_struct pointer array pointing to allocated
1929  *	    vm_structs on success, %NULL on failure
1930  *
1931  * Percpu allocator wants to use congruent vm areas so that it can
1932  * maintain the offsets among percpu areas.  This function allocates
1933  * congruent vmalloc areas for it.  These areas tend to be scattered
1934  * pretty far, distance between two areas easily going up to
1935  * gigabytes.  To avoid interacting with regular vmallocs, these areas
1936  * are allocated from top.
1937  *
1938  * Despite its complicated look, this allocator is rather simple.  It
1939  * does everything top-down and scans areas from the end looking for
1940  * matching slot.  While scanning, if any of the areas overlaps with
1941  * existing vmap_area, the base address is pulled down to fit the
1942  * area.  Scanning is repeated till all the areas fit and then all
1943  * necessary data structres are inserted and the result is returned.
1944  */
1945 struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
1946 				     const size_t *sizes, int nr_vms,
1947 				     size_t align, gfp_t gfp_mask)
1948 {
1949 	const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
1950 	const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
1951 	struct vmap_area **vas, *prev, *next;
1952 	struct vm_struct **vms;
1953 	int area, area2, last_area, term_area;
1954 	unsigned long base, start, end, last_end;
1955 	bool purged = false;
1956 
1957 	gfp_mask &= GFP_RECLAIM_MASK;
1958 
1959 	/* verify parameters and allocate data structures */
1960 	BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align));
1961 	for (last_area = 0, area = 0; area < nr_vms; area++) {
1962 		start = offsets[area];
1963 		end = start + sizes[area];
1964 
1965 		/* is everything aligned properly? */
1966 		BUG_ON(!IS_ALIGNED(offsets[area], align));
1967 		BUG_ON(!IS_ALIGNED(sizes[area], align));
1968 
1969 		/* detect the area with the highest address */
1970 		if (start > offsets[last_area])
1971 			last_area = area;
1972 
1973 		for (area2 = 0; area2 < nr_vms; area2++) {
1974 			unsigned long start2 = offsets[area2];
1975 			unsigned long end2 = start2 + sizes[area2];
1976 
1977 			if (area2 == area)
1978 				continue;
1979 
1980 			BUG_ON(start2 >= start && start2 < end);
1981 			BUG_ON(end2 <= end && end2 > start);
1982 		}
1983 	}
1984 	last_end = offsets[last_area] + sizes[last_area];
1985 
1986 	if (vmalloc_end - vmalloc_start < last_end) {
1987 		WARN_ON(true);
1988 		return NULL;
1989 	}
1990 
1991 	vms = kzalloc(sizeof(vms[0]) * nr_vms, gfp_mask);
1992 	vas = kzalloc(sizeof(vas[0]) * nr_vms, gfp_mask);
1993 	if (!vas || !vms)
1994 		goto err_free;
1995 
1996 	for (area = 0; area < nr_vms; area++) {
1997 		vas[area] = kzalloc(sizeof(struct vmap_area), gfp_mask);
1998 		vms[area] = kzalloc(sizeof(struct vm_struct), gfp_mask);
1999 		if (!vas[area] || !vms[area])
2000 			goto err_free;
2001 	}
2002 retry:
2003 	spin_lock(&vmap_area_lock);
2004 
2005 	/* start scanning - we scan from the top, begin with the last area */
2006 	area = term_area = last_area;
2007 	start = offsets[area];
2008 	end = start + sizes[area];
2009 
2010 	if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2011 		base = vmalloc_end - last_end;
2012 		goto found;
2013 	}
2014 	base = pvm_determine_end(&next, &prev, align) - end;
2015 
2016 	while (true) {
2017 		BUG_ON(next && next->va_end <= base + end);
2018 		BUG_ON(prev && prev->va_end > base + end);
2019 
2020 		/*
2021 		 * base might have underflowed, add last_end before
2022 		 * comparing.
2023 		 */
2024 		if (base + last_end < vmalloc_start + last_end) {
2025 			spin_unlock(&vmap_area_lock);
2026 			if (!purged) {
2027 				purge_vmap_area_lazy();
2028 				purged = true;
2029 				goto retry;
2030 			}
2031 			goto err_free;
2032 		}
2033 
2034 		/*
2035 		 * If next overlaps, move base downwards so that it's
2036 		 * right below next and then recheck.
2037 		 */
2038 		if (next && next->va_start < base + end) {
2039 			base = pvm_determine_end(&next, &prev, align) - end;
2040 			term_area = area;
2041 			continue;
2042 		}
2043 
2044 		/*
2045 		 * If prev overlaps, shift down next and prev and move
2046 		 * base so that it's right below new next and then
2047 		 * recheck.
2048 		 */
2049 		if (prev && prev->va_end > base + start)  {
2050 			next = prev;
2051 			prev = node_to_va(rb_prev(&next->rb_node));
2052 			base = pvm_determine_end(&next, &prev, align) - end;
2053 			term_area = area;
2054 			continue;
2055 		}
2056 
2057 		/*
2058 		 * This area fits, move on to the previous one.  If
2059 		 * the previous one is the terminal one, we're done.
2060 		 */
2061 		area = (area + nr_vms - 1) % nr_vms;
2062 		if (area == term_area)
2063 			break;
2064 		start = offsets[area];
2065 		end = start + sizes[area];
2066 		pvm_find_next_prev(base + end, &next, &prev);
2067 	}
2068 found:
2069 	/* we've found a fitting base, insert all va's */
2070 	for (area = 0; area < nr_vms; area++) {
2071 		struct vmap_area *va = vas[area];
2072 
2073 		va->va_start = base + offsets[area];
2074 		va->va_end = va->va_start + sizes[area];
2075 		__insert_vmap_area(va);
2076 	}
2077 
2078 	vmap_area_pcpu_hole = base + offsets[last_area];
2079 
2080 	spin_unlock(&vmap_area_lock);
2081 
2082 	/* insert all vm's */
2083 	for (area = 0; area < nr_vms; area++)
2084 		insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2085 				  pcpu_get_vm_areas);
2086 
2087 	kfree(vas);
2088 	return vms;
2089 
2090 err_free:
2091 	for (area = 0; area < nr_vms; area++) {
2092 		if (vas)
2093 			kfree(vas[area]);
2094 		if (vms)
2095 			kfree(vms[area]);
2096 	}
2097 	kfree(vas);
2098 	kfree(vms);
2099 	return NULL;
2100 }
2101 
2102 /**
2103  * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2104  * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2105  * @nr_vms: the number of allocated areas
2106  *
2107  * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2108  */
2109 void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2110 {
2111 	int i;
2112 
2113 	for (i = 0; i < nr_vms; i++)
2114 		free_vm_area(vms[i]);
2115 	kfree(vms);
2116 }
2117 
2118 #ifdef CONFIG_PROC_FS
2119 static void *s_start(struct seq_file *m, loff_t *pos)
2120 {
2121 	loff_t n = *pos;
2122 	struct vm_struct *v;
2123 
2124 	read_lock(&vmlist_lock);
2125 	v = vmlist;
2126 	while (n > 0 && v) {
2127 		n--;
2128 		v = v->next;
2129 	}
2130 	if (!n)
2131 		return v;
2132 
2133 	return NULL;
2134 
2135 }
2136 
2137 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2138 {
2139 	struct vm_struct *v = p;
2140 
2141 	++*pos;
2142 	return v->next;
2143 }
2144 
2145 static void s_stop(struct seq_file *m, void *p)
2146 {
2147 	read_unlock(&vmlist_lock);
2148 }
2149 
2150 static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2151 {
2152 	if (NUMA_BUILD) {
2153 		unsigned int nr, *counters = m->private;
2154 
2155 		if (!counters)
2156 			return;
2157 
2158 		memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2159 
2160 		for (nr = 0; nr < v->nr_pages; nr++)
2161 			counters[page_to_nid(v->pages[nr])]++;
2162 
2163 		for_each_node_state(nr, N_HIGH_MEMORY)
2164 			if (counters[nr])
2165 				seq_printf(m, " N%u=%u", nr, counters[nr]);
2166 	}
2167 }
2168 
2169 static int s_show(struct seq_file *m, void *p)
2170 {
2171 	struct vm_struct *v = p;
2172 
2173 	seq_printf(m, "0x%p-0x%p %7ld",
2174 		v->addr, v->addr + v->size, v->size);
2175 
2176 	if (v->caller) {
2177 		char buff[KSYM_SYMBOL_LEN];
2178 
2179 		seq_putc(m, ' ');
2180 		sprint_symbol(buff, (unsigned long)v->caller);
2181 		seq_puts(m, buff);
2182 	}
2183 
2184 	if (v->nr_pages)
2185 		seq_printf(m, " pages=%d", v->nr_pages);
2186 
2187 	if (v->phys_addr)
2188 		seq_printf(m, " phys=%lx", v->phys_addr);
2189 
2190 	if (v->flags & VM_IOREMAP)
2191 		seq_printf(m, " ioremap");
2192 
2193 	if (v->flags & VM_ALLOC)
2194 		seq_printf(m, " vmalloc");
2195 
2196 	if (v->flags & VM_MAP)
2197 		seq_printf(m, " vmap");
2198 
2199 	if (v->flags & VM_USERMAP)
2200 		seq_printf(m, " user");
2201 
2202 	if (v->flags & VM_VPAGES)
2203 		seq_printf(m, " vpages");
2204 
2205 	show_numa_info(m, v);
2206 	seq_putc(m, '\n');
2207 	return 0;
2208 }
2209 
2210 static const struct seq_operations vmalloc_op = {
2211 	.start = s_start,
2212 	.next = s_next,
2213 	.stop = s_stop,
2214 	.show = s_show,
2215 };
2216 
2217 static int vmalloc_open(struct inode *inode, struct file *file)
2218 {
2219 	unsigned int *ptr = NULL;
2220 	int ret;
2221 
2222 	if (NUMA_BUILD)
2223 		ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
2224 	ret = seq_open(file, &vmalloc_op);
2225 	if (!ret) {
2226 		struct seq_file *m = file->private_data;
2227 		m->private = ptr;
2228 	} else
2229 		kfree(ptr);
2230 	return ret;
2231 }
2232 
2233 static const struct file_operations proc_vmalloc_operations = {
2234 	.open		= vmalloc_open,
2235 	.read		= seq_read,
2236 	.llseek		= seq_lseek,
2237 	.release	= seq_release_private,
2238 };
2239 
2240 static int __init proc_vmalloc_init(void)
2241 {
2242 	proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
2243 	return 0;
2244 }
2245 module_init(proc_vmalloc_init);
2246 #endif
2247 
2248