Merge tag 'v3.10.68' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / mm / vmalloc.c
CommitLineData
1da177e4
LT
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
930fc45a 8 * Numa awareness, Christoph Lameter, SGI, June 2005
1da177e4
LT
9 */
10
db64fe02 11#include <linux/vmalloc.h>
1da177e4
LT
12#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/highmem.h>
d43c36dc 15#include <linux/sched.h>
1da177e4
LT
16#include <linux/slab.h>
17#include <linux/spinlock.h>
18#include <linux/interrupt.h>
5f6a6a9c 19#include <linux/proc_fs.h>
a10aa579 20#include <linux/seq_file.h>
3ac7fe5a 21#include <linux/debugobjects.h>
23016969 22#include <linux/kallsyms.h>
db64fe02
NP
23#include <linux/list.h>
24#include <linux/rbtree.h>
25#include <linux/radix-tree.h>
26#include <linux/rcupdate.h>
f0aa6617 27#include <linux/pfn.h>
89219d37 28#include <linux/kmemleak.h>
60063497 29#include <linux/atomic.h>
32fcfd40 30#include <linux/llist.h>
1da177e4
LT
31#include <asm/uaccess.h>
32#include <asm/tlbflush.h>
2dca6999 33#include <asm/shmparam.h>
1da177e4 34
32fcfd40
AV
35struct vfree_deferred {
36 struct llist_head list;
37 struct work_struct wq;
38};
39static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
40
41static void __vunmap(const void *, int);
42
43static void free_work(struct work_struct *w)
44{
45 struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
46 struct llist_node *llnode = llist_del_all(&p->list);
47 while (llnode) {
48 void *p = llnode;
49 llnode = llist_next(llnode);
50 __vunmap(p, 1);
51 }
52}
53
db64fe02 54/*** Page table manipulation functions ***/
b221385b 55
1da177e4
LT
56static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
57{
58 pte_t *pte;
59
60 pte = pte_offset_kernel(pmd, addr);
61 do {
62 pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
63 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
64 } while (pte++, addr += PAGE_SIZE, addr != end);
65}
66
db64fe02 67static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
1da177e4
LT
68{
69 pmd_t *pmd;
70 unsigned long next;
71
72 pmd = pmd_offset(pud, addr);
73 do {
74 next = pmd_addr_end(addr, end);
75 if (pmd_none_or_clear_bad(pmd))
76 continue;
77 vunmap_pte_range(pmd, addr, next);
78 } while (pmd++, addr = next, addr != end);
79}
80
db64fe02 81static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
1da177e4
LT
82{
83 pud_t *pud;
84 unsigned long next;
85
86 pud = pud_offset(pgd, addr);
87 do {
88 next = pud_addr_end(addr, end);
89 if (pud_none_or_clear_bad(pud))
90 continue;
91 vunmap_pmd_range(pud, addr, next);
92 } while (pud++, addr = next, addr != end);
93}
94
db64fe02 95static void vunmap_page_range(unsigned long addr, unsigned long end)
1da177e4
LT
96{
97 pgd_t *pgd;
98 unsigned long next;
1da177e4
LT
99
100 BUG_ON(addr >= end);
101 pgd = pgd_offset_k(addr);
1da177e4
LT
102 do {
103 next = pgd_addr_end(addr, end);
104 if (pgd_none_or_clear_bad(pgd))
105 continue;
106 vunmap_pud_range(pgd, addr, next);
107 } while (pgd++, addr = next, addr != end);
1da177e4
LT
108}
109
110static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
db64fe02 111 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1da177e4
LT
112{
113 pte_t *pte;
114
db64fe02
NP
115 /*
116 * nr is a running index into the array which helps higher level
117 * callers keep track of where we're up to.
118 */
119
872fec16 120 pte = pte_alloc_kernel(pmd, addr);
1da177e4
LT
121 if (!pte)
122 return -ENOMEM;
123 do {
db64fe02
NP
124 struct page *page = pages[*nr];
125
126 if (WARN_ON(!pte_none(*pte)))
127 return -EBUSY;
128 if (WARN_ON(!page))
1da177e4
LT
129 return -ENOMEM;
130 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
db64fe02 131 (*nr)++;
1da177e4
LT
132 } while (pte++, addr += PAGE_SIZE, addr != end);
133 return 0;
134}
135
db64fe02
NP
136static int vmap_pmd_range(pud_t *pud, unsigned long addr,
137 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1da177e4
LT
138{
139 pmd_t *pmd;
140 unsigned long next;
141
142 pmd = pmd_alloc(&init_mm, pud, addr);
143 if (!pmd)
144 return -ENOMEM;
145 do {
146 next = pmd_addr_end(addr, end);
db64fe02 147 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
1da177e4
LT
148 return -ENOMEM;
149 } while (pmd++, addr = next, addr != end);
150 return 0;
151}
152
db64fe02
NP
153static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
154 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
1da177e4
LT
155{
156 pud_t *pud;
157 unsigned long next;
158
159 pud = pud_alloc(&init_mm, pgd, addr);
160 if (!pud)
161 return -ENOMEM;
162 do {
163 next = pud_addr_end(addr, end);
db64fe02 164 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
1da177e4
LT
165 return -ENOMEM;
166 } while (pud++, addr = next, addr != end);
167 return 0;
168}
169
db64fe02
NP
170/*
171 * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
172 * will have pfns corresponding to the "pages" array.
173 *
174 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
175 */
8fc48985
TH
176static int vmap_page_range_noflush(unsigned long start, unsigned long end,
177 pgprot_t prot, struct page **pages)
1da177e4
LT
178{
179 pgd_t *pgd;
180 unsigned long next;
2e4e27c7 181 unsigned long addr = start;
db64fe02
NP
182 int err = 0;
183 int nr = 0;
1da177e4
LT
184
185 BUG_ON(addr >= end);
186 pgd = pgd_offset_k(addr);
1da177e4
LT
187 do {
188 next = pgd_addr_end(addr, end);
db64fe02 189 err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
1da177e4 190 if (err)
bf88c8c8 191 return err;
1da177e4 192 } while (pgd++, addr = next, addr != end);
db64fe02 193
db64fe02 194 return nr;
1da177e4
LT
195}
196
8fc48985
TH
197static int vmap_page_range(unsigned long start, unsigned long end,
198 pgprot_t prot, struct page **pages)
199{
200 int ret;
201
202 ret = vmap_page_range_noflush(start, end, prot, pages);
203 flush_cache_vmap(start, end);
204 return ret;
205}
206
81ac3ad9 207int is_vmalloc_or_module_addr(const void *x)
73bdf0a6
LT
208{
209 /*
ab4f2ee1 210 * ARM, x86-64 and sparc64 put modules in a special place,
73bdf0a6
LT
211 * and fall back on vmalloc() if that fails. Others
212 * just put it in the vmalloc space.
213 */
214#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
215 unsigned long addr = (unsigned long)x;
216 if (addr >= MODULES_VADDR && addr < MODULES_END)
217 return 1;
218#endif
219 return is_vmalloc_addr(x);
220}
221
48667e7a 222/*
db64fe02 223 * Walk a vmap address to the struct page it maps.
48667e7a 224 */
b3bdda02 225struct page *vmalloc_to_page(const void *vmalloc_addr)
48667e7a
CL
226{
227 unsigned long addr = (unsigned long) vmalloc_addr;
228 struct page *page = NULL;
229 pgd_t *pgd = pgd_offset_k(addr);
48667e7a 230
7aa413de
IM
231 /*
232 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
233 * architectures that do not vmalloc module space
234 */
73bdf0a6 235 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
59ea7463 236
48667e7a 237 if (!pgd_none(*pgd)) {
db64fe02 238 pud_t *pud = pud_offset(pgd, addr);
48667e7a 239 if (!pud_none(*pud)) {
db64fe02 240 pmd_t *pmd = pmd_offset(pud, addr);
48667e7a 241 if (!pmd_none(*pmd)) {
db64fe02
NP
242 pte_t *ptep, pte;
243
48667e7a
CL
244 ptep = pte_offset_map(pmd, addr);
245 pte = *ptep;
246 if (pte_present(pte))
247 page = pte_page(pte);
248 pte_unmap(ptep);
249 }
250 }
251 }
252 return page;
253}
254EXPORT_SYMBOL(vmalloc_to_page);
255
256/*
257 * Map a vmalloc()-space virtual address to the physical page frame number.
258 */
b3bdda02 259unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
48667e7a
CL
260{
261 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
262}
263EXPORT_SYMBOL(vmalloc_to_pfn);
264
db64fe02
NP
265
266/*** Global kva allocator ***/
267
268#define VM_LAZY_FREE 0x01
269#define VM_LAZY_FREEING 0x02
270#define VM_VM_AREA 0x04
271
db64fe02 272static DEFINE_SPINLOCK(vmap_area_lock);
f1c4069e
JK
273/* Export for kexec only */
274LIST_HEAD(vmap_area_list);
89699605
NP
275static struct rb_root vmap_area_root = RB_ROOT;
276
277/* The vmap cache globals are protected by vmap_area_lock */
278static struct rb_node *free_vmap_cache;
279static unsigned long cached_hole_size;
280static unsigned long cached_vstart;
281static unsigned long cached_align;
282
ca23e405 283static unsigned long vmap_area_pcpu_hole;
db64fe02
NP
284
285static struct vmap_area *__find_vmap_area(unsigned long addr)
1da177e4 286{
db64fe02
NP
287 struct rb_node *n = vmap_area_root.rb_node;
288
289 while (n) {
290 struct vmap_area *va;
291
292 va = rb_entry(n, struct vmap_area, rb_node);
293 if (addr < va->va_start)
294 n = n->rb_left;
295 else if (addr > va->va_start)
296 n = n->rb_right;
297 else
298 return va;
299 }
300
301 return NULL;
302}
303
304static void __insert_vmap_area(struct vmap_area *va)
305{
306 struct rb_node **p = &vmap_area_root.rb_node;
307 struct rb_node *parent = NULL;
308 struct rb_node *tmp;
309
310 while (*p) {
170168d0 311 struct vmap_area *tmp_va;
db64fe02
NP
312
313 parent = *p;
170168d0
NK
314 tmp_va = rb_entry(parent, struct vmap_area, rb_node);
315 if (va->va_start < tmp_va->va_end)
db64fe02 316 p = &(*p)->rb_left;
170168d0 317 else if (va->va_end > tmp_va->va_start)
db64fe02
NP
318 p = &(*p)->rb_right;
319 else
320 BUG();
321 }
322
323 rb_link_node(&va->rb_node, parent, p);
324 rb_insert_color(&va->rb_node, &vmap_area_root);
325
4341fa45 326 /* address-sort this list */
db64fe02
NP
327 tmp = rb_prev(&va->rb_node);
328 if (tmp) {
329 struct vmap_area *prev;
330 prev = rb_entry(tmp, struct vmap_area, rb_node);
331 list_add_rcu(&va->list, &prev->list);
332 } else
333 list_add_rcu(&va->list, &vmap_area_list);
334}
335
336static void purge_vmap_area_lazy(void);
337
338/*
339 * Allocate a region of KVA of the specified size and alignment, within the
340 * vstart and vend.
341 */
342static struct vmap_area *alloc_vmap_area(unsigned long size,
343 unsigned long align,
344 unsigned long vstart, unsigned long vend,
345 int node, gfp_t gfp_mask)
346{
347 struct vmap_area *va;
348 struct rb_node *n;
1da177e4 349 unsigned long addr;
db64fe02 350 int purged = 0;
89699605 351 struct vmap_area *first;
db64fe02 352
7766970c 353 BUG_ON(!size);
db64fe02 354 BUG_ON(size & ~PAGE_MASK);
89699605 355 BUG_ON(!is_power_of_2(align));
db64fe02 356
db64fe02
NP
357 va = kmalloc_node(sizeof(struct vmap_area),
358 gfp_mask & GFP_RECLAIM_MASK, node);
359 if (unlikely(!va))
360 return ERR_PTR(-ENOMEM);
361
362retry:
363 spin_lock(&vmap_area_lock);
89699605
NP
364 /*
365 * Invalidate cache if we have more permissive parameters.
366 * cached_hole_size notes the largest hole noticed _below_
367 * the vmap_area cached in free_vmap_cache: if size fits
368 * into that hole, we want to scan from vstart to reuse
369 * the hole instead of allocating above free_vmap_cache.
370 * Note that __free_vmap_area may update free_vmap_cache
371 * without updating cached_hole_size or cached_align.
372 */
373 if (!free_vmap_cache ||
374 size < cached_hole_size ||
375 vstart < cached_vstart ||
376 align < cached_align) {
377nocache:
378 cached_hole_size = 0;
379 free_vmap_cache = NULL;
380 }
381 /* record if we encounter less permissive parameters */
382 cached_vstart = vstart;
383 cached_align = align;
384
385 /* find starting point for our search */
386 if (free_vmap_cache) {
387 first = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
248ac0e1 388 addr = ALIGN(first->va_end, align);
89699605
NP
389 if (addr < vstart)
390 goto nocache;
7bff7acc 391 if (addr + size < addr)
89699605
NP
392 goto overflow;
393
394 } else {
395 addr = ALIGN(vstart, align);
7bff7acc 396 if (addr + size < addr)
89699605
NP
397 goto overflow;
398
399 n = vmap_area_root.rb_node;
400 first = NULL;
401
402 while (n) {
db64fe02
NP
403 struct vmap_area *tmp;
404 tmp = rb_entry(n, struct vmap_area, rb_node);
405 if (tmp->va_end >= addr) {
db64fe02 406 first = tmp;
89699605
NP
407 if (tmp->va_start <= addr)
408 break;
409 n = n->rb_left;
410 } else
db64fe02 411 n = n->rb_right;
89699605 412 }
db64fe02
NP
413
414 if (!first)
415 goto found;
db64fe02 416 }
89699605
NP
417
418 /* from the starting point, walk areas until a suitable hole is found */
248ac0e1 419 while (addr + size > first->va_start && addr + size <= vend) {
89699605
NP
420 if (addr + cached_hole_size < first->va_start)
421 cached_hole_size = first->va_start - addr;
248ac0e1 422 addr = ALIGN(first->va_end, align);
7bff7acc 423 if (addr + size < addr)
89699605
NP
424 goto overflow;
425
6fa3eb70 426 /*
92ca922f 427 if (list_is_last(&first->list, &vmap_area_list))
89699605 428 goto found;
92ca922f
H
429
430 first = list_entry(first->list.next,
431 struct vmap_area, list);
6fa3eb70
S
432 */
433 n = rb_next(&first->rb_node);
434 if (n)
435 first = rb_entry(n, struct vmap_area, rb_node);
436 else
437 goto found;
db64fe02
NP
438 }
439
89699605
NP
440found:
441 if (addr + size > vend)
442 goto overflow;
db64fe02
NP
443
444 va->va_start = addr;
445 va->va_end = addr + size;
446 va->flags = 0;
447 __insert_vmap_area(va);
89699605 448 free_vmap_cache = &va->rb_node;
db64fe02
NP
449 spin_unlock(&vmap_area_lock);
450
89699605
NP
451 BUG_ON(va->va_start & (align-1));
452 BUG_ON(va->va_start < vstart);
453 BUG_ON(va->va_end > vend);
454
db64fe02 455 return va;
89699605
NP
456
457overflow:
458 spin_unlock(&vmap_area_lock);
459 if (!purged) {
460 purge_vmap_area_lazy();
461 purged = 1;
462 goto retry;
463 }
464 if (printk_ratelimit())
465 printk(KERN_WARNING
466 "vmap allocation for size %lu failed: "
467 "use vmalloc=<size> to increase size.\n", size);
468 kfree(va);
469 return ERR_PTR(-EBUSY);
db64fe02
NP
470}
471
db64fe02
NP
472static void __free_vmap_area(struct vmap_area *va)
473{
474 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
89699605
NP
475
476 if (free_vmap_cache) {
477 if (va->va_end < cached_vstart) {
478 free_vmap_cache = NULL;
479 } else {
480 struct vmap_area *cache;
481 cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
482 if (va->va_start <= cache->va_start) {
483 free_vmap_cache = rb_prev(&va->rb_node);
484 /*
485 * We don't try to update cached_hole_size or
486 * cached_align, but it won't go very wrong.
487 */
488 }
489 }
490 }
db64fe02
NP
491 rb_erase(&va->rb_node, &vmap_area_root);
492 RB_CLEAR_NODE(&va->rb_node);
493 list_del_rcu(&va->list);
494
ca23e405
TH
495 /*
496 * Track the highest possible candidate for pcpu area
497 * allocation. Areas outside of vmalloc area can be returned
498 * here too, consider only end addresses which fall inside
499 * vmalloc area proper.
500 */
501 if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
502 vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
503
14769de9 504 kfree_rcu(va, rcu_head);
db64fe02
NP
505}
506
507/*
508 * Free a region of KVA allocated by alloc_vmap_area
509 */
510static void free_vmap_area(struct vmap_area *va)
511{
512 spin_lock(&vmap_area_lock);
513 __free_vmap_area(va);
514 spin_unlock(&vmap_area_lock);
515}
516
517/*
518 * Clear the pagetable entries of a given vmap_area
519 */
520static void unmap_vmap_area(struct vmap_area *va)
521{
522 vunmap_page_range(va->va_start, va->va_end);
523}
524
cd52858c
NP
525static void vmap_debug_free_range(unsigned long start, unsigned long end)
526{
527 /*
528 * Unmap page tables and force a TLB flush immediately if
529 * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
530 * bugs similarly to those in linear kernel virtual address
531 * space after a page has been freed.
532 *
533 * All the lazy freeing logic is still retained, in order to
534 * minimise intrusiveness of this debugging feature.
535 *
536 * This is going to be *slow* (linear kernel virtual address
537 * debugging doesn't do a broadcast TLB flush so it is a lot
538 * faster).
539 */
540#ifdef CONFIG_DEBUG_PAGEALLOC
541 vunmap_page_range(start, end);
542 flush_tlb_kernel_range(start, end);
543#endif
544}
545
db64fe02
NP
546/*
547 * lazy_max_pages is the maximum amount of virtual address space we gather up
548 * before attempting to purge with a TLB flush.
549 *
550 * There is a tradeoff here: a larger number will cover more kernel page tables
551 * and take slightly longer to purge, but it will linearly reduce the number of
552 * global TLB flushes that must be performed. It would seem natural to scale
553 * this number up linearly with the number of CPUs (because vmapping activity
554 * could also scale linearly with the number of CPUs), however it is likely
555 * that in practice, workloads might be constrained in other ways that mean
556 * vmap activity will not scale linearly with CPUs. Also, I want to be
557 * conservative and not introduce a big latency on huge systems, so go with
558 * a less aggressive log scale. It will still be an improvement over the old
559 * code, and it will be simple to change the scale factor if we find that it
560 * becomes a problem on bigger systems.
561 */
562static unsigned long lazy_max_pages(void)
563{
564 unsigned int log;
565
566 log = fls(num_online_cpus());
567
568 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
569}
570
571static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
572
02b709df
NP
573/* for per-CPU blocks */
574static void purge_fragmented_blocks_allcpus(void);
575
3ee48b6a
CW
576/*
577 * called before a call to iounmap() if the caller wants vm_area_struct's
578 * immediately freed.
579 */
580void set_iounmap_nonlazy(void)
581{
582 atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
583}
584
db64fe02
NP
585/*
586 * Purges all lazily-freed vmap areas.
587 *
588 * If sync is 0 then don't purge if there is already a purge in progress.
589 * If force_flush is 1, then flush kernel TLBs between *start and *end even
590 * if we found no lazy vmap areas to unmap (callers can use this to optimise
591 * their own TLB flushing).
592 * Returns with *start = min(*start, lowest purged address)
593 * *end = max(*end, highest purged address)
594 */
595static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
596 int sync, int force_flush)
597{
46666d8a 598 static DEFINE_SPINLOCK(purge_lock);
db64fe02
NP
599 LIST_HEAD(valist);
600 struct vmap_area *va;
cbb76676 601 struct vmap_area *n_va;
db64fe02
NP
602 int nr = 0;
603
604 /*
605 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
606 * should not expect such behaviour. This just simplifies locking for
607 * the case that isn't actually used at the moment anyway.
608 */
609 if (!sync && !force_flush) {
46666d8a 610 if (!spin_trylock(&purge_lock))
db64fe02
NP
611 return;
612 } else
46666d8a 613 spin_lock(&purge_lock);
db64fe02 614
02b709df
NP
615 if (sync)
616 purge_fragmented_blocks_allcpus();
617
db64fe02
NP
618 rcu_read_lock();
619 list_for_each_entry_rcu(va, &vmap_area_list, list) {
620 if (va->flags & VM_LAZY_FREE) {
621 if (va->va_start < *start)
622 *start = va->va_start;
623 if (va->va_end > *end)
624 *end = va->va_end;
625 nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
db64fe02
NP
626 list_add_tail(&va->purge_list, &valist);
627 va->flags |= VM_LAZY_FREEING;
628 va->flags &= ~VM_LAZY_FREE;
629 }
630 }
631 rcu_read_unlock();
632
88f50044 633 if (nr)
db64fe02 634 atomic_sub(nr, &vmap_lazy_nr);
db64fe02
NP
635
636 if (nr || force_flush)
637 flush_tlb_kernel_range(*start, *end);
638
639 if (nr) {
640 spin_lock(&vmap_area_lock);
cbb76676 641 list_for_each_entry_safe(va, n_va, &valist, purge_list)
db64fe02
NP
642 __free_vmap_area(va);
643 spin_unlock(&vmap_area_lock);
644 }
46666d8a 645 spin_unlock(&purge_lock);
db64fe02
NP
646}
647
496850e5
NP
648/*
649 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
650 * is already purging.
651 */
652static void try_purge_vmap_area_lazy(void)
653{
654 unsigned long start = ULONG_MAX, end = 0;
655
656 __purge_vmap_area_lazy(&start, &end, 0, 0);
657}
658
db64fe02
NP
659/*
660 * Kick off a purge of the outstanding lazy areas.
661 */
662static void purge_vmap_area_lazy(void)
663{
664 unsigned long start = ULONG_MAX, end = 0;
665
496850e5 666 __purge_vmap_area_lazy(&start, &end, 1, 0);
db64fe02
NP
667}
668
669/*
64141da5
JF
670 * Free a vmap area, caller ensuring that the area has been unmapped
671 * and flush_cache_vunmap had been called for the correct range
672 * previously.
db64fe02 673 */
64141da5 674static void free_vmap_area_noflush(struct vmap_area *va)
db64fe02
NP
675{
676 va->flags |= VM_LAZY_FREE;
677 atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
678 if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
496850e5 679 try_purge_vmap_area_lazy();
db64fe02
NP
680}
681
64141da5
JF
682/*
683 * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
684 * called for the correct range previously.
685 */
686static void free_unmap_vmap_area_noflush(struct vmap_area *va)
687{
688 unmap_vmap_area(va);
689 free_vmap_area_noflush(va);
690}
691
b29acbdc
NP
692/*
693 * Free and unmap a vmap area
694 */
695static void free_unmap_vmap_area(struct vmap_area *va)
696{
697 flush_cache_vunmap(va->va_start, va->va_end);
698 free_unmap_vmap_area_noflush(va);
699}
700
db64fe02
NP
701static struct vmap_area *find_vmap_area(unsigned long addr)
702{
703 struct vmap_area *va;
704
705 spin_lock(&vmap_area_lock);
706 va = __find_vmap_area(addr);
707 spin_unlock(&vmap_area_lock);
708
709 return va;
710}
711
712static void free_unmap_vmap_area_addr(unsigned long addr)
713{
714 struct vmap_area *va;
715
716 va = find_vmap_area(addr);
717 BUG_ON(!va);
718 free_unmap_vmap_area(va);
719}
720
721
722/*** Per cpu kva allocator ***/
723
724/*
725 * vmap space is limited especially on 32 bit architectures. Ensure there is
726 * room for at least 16 percpu vmap blocks per CPU.
727 */
728/*
729 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
730 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
731 * instead (we just need a rough idea)
732 */
733#if BITS_PER_LONG == 32
734#define VMALLOC_SPACE (128UL*1024*1024)
735#else
736#define VMALLOC_SPACE (128UL*1024*1024*1024)
737#endif
738
739#define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
740#define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
741#define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
742#define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
743#define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
744#define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
f982f915
CL
745#define VMAP_BBMAP_BITS \
746 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
747 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
748 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
db64fe02
NP
749
750#define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
751
9b463334
JF
752static bool vmap_initialized __read_mostly = false;
753
db64fe02
NP
754struct vmap_block_queue {
755 spinlock_t lock;
756 struct list_head free;
db64fe02
NP
757};
758
759struct vmap_block {
760 spinlock_t lock;
761 struct vmap_area *va;
762 struct vmap_block_queue *vbq;
763 unsigned long free, dirty;
764 DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
765 DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
de560423
NP
766 struct list_head free_list;
767 struct rcu_head rcu_head;
02b709df 768 struct list_head purge;
db64fe02
NP
769};
770
771/* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
772static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
773
774/*
775 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
776 * in the free path. Could get rid of this if we change the API to return a
777 * "cookie" from alloc, to be passed to free. But no big deal yet.
778 */
779static DEFINE_SPINLOCK(vmap_block_tree_lock);
780static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
781
782/*
783 * We should probably have a fallback mechanism to allocate virtual memory
784 * out of partially filled vmap blocks. However vmap block sizing should be
785 * fairly reasonable according to the vmalloc size, so it shouldn't be a
786 * big problem.
787 */
788
789static unsigned long addr_to_vb_idx(unsigned long addr)
790{
791 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
792 addr /= VMAP_BLOCK_SIZE;
793 return addr;
794}
795
796static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
797{
798 struct vmap_block_queue *vbq;
799 struct vmap_block *vb;
800 struct vmap_area *va;
801 unsigned long vb_idx;
802 int node, err;
803
804 node = numa_node_id();
805
806 vb = kmalloc_node(sizeof(struct vmap_block),
807 gfp_mask & GFP_RECLAIM_MASK, node);
808 if (unlikely(!vb))
809 return ERR_PTR(-ENOMEM);
810
811 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
812 VMALLOC_START, VMALLOC_END,
813 node, gfp_mask);
ddf9c6d4 814 if (IS_ERR(va)) {
db64fe02 815 kfree(vb);
e7d86340 816 return ERR_CAST(va);
db64fe02
NP
817 }
818
819 err = radix_tree_preload(gfp_mask);
820 if (unlikely(err)) {
821 kfree(vb);
822 free_vmap_area(va);
823 return ERR_PTR(err);
824 }
825
826 spin_lock_init(&vb->lock);
827 vb->va = va;
828 vb->free = VMAP_BBMAP_BITS;
829 vb->dirty = 0;
830 bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
831 bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
832 INIT_LIST_HEAD(&vb->free_list);
db64fe02
NP
833
834 vb_idx = addr_to_vb_idx(va->va_start);
835 spin_lock(&vmap_block_tree_lock);
836 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
837 spin_unlock(&vmap_block_tree_lock);
838 BUG_ON(err);
839 radix_tree_preload_end();
840
841 vbq = &get_cpu_var(vmap_block_queue);
842 vb->vbq = vbq;
843 spin_lock(&vbq->lock);
de560423 844 list_add_rcu(&vb->free_list, &vbq->free);
db64fe02 845 spin_unlock(&vbq->lock);
3f04ba85 846 put_cpu_var(vmap_block_queue);
db64fe02
NP
847
848 return vb;
849}
850
db64fe02
NP
851static void free_vmap_block(struct vmap_block *vb)
852{
853 struct vmap_block *tmp;
854 unsigned long vb_idx;
855
db64fe02
NP
856 vb_idx = addr_to_vb_idx(vb->va->va_start);
857 spin_lock(&vmap_block_tree_lock);
858 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
859 spin_unlock(&vmap_block_tree_lock);
860 BUG_ON(tmp != vb);
861
64141da5 862 free_vmap_area_noflush(vb->va);
22a3c7d1 863 kfree_rcu(vb, rcu_head);
db64fe02
NP
864}
865
02b709df
NP
866static void purge_fragmented_blocks(int cpu)
867{
868 LIST_HEAD(purge);
869 struct vmap_block *vb;
870 struct vmap_block *n_vb;
871 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
872
873 rcu_read_lock();
874 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
875
876 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
877 continue;
878
879 spin_lock(&vb->lock);
880 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
881 vb->free = 0; /* prevent further allocs after releasing lock */
882 vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
883 bitmap_fill(vb->alloc_map, VMAP_BBMAP_BITS);
884 bitmap_fill(vb->dirty_map, VMAP_BBMAP_BITS);
885 spin_lock(&vbq->lock);
886 list_del_rcu(&vb->free_list);
887 spin_unlock(&vbq->lock);
888 spin_unlock(&vb->lock);
889 list_add_tail(&vb->purge, &purge);
890 } else
891 spin_unlock(&vb->lock);
892 }
893 rcu_read_unlock();
894
895 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
896 list_del(&vb->purge);
897 free_vmap_block(vb);
898 }
899}
900
901static void purge_fragmented_blocks_thiscpu(void)
902{
903 purge_fragmented_blocks(smp_processor_id());
904}
905
906static void purge_fragmented_blocks_allcpus(void)
907{
908 int cpu;
909
910 for_each_possible_cpu(cpu)
911 purge_fragmented_blocks(cpu);
912}
913
db64fe02
NP
914static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
915{
916 struct vmap_block_queue *vbq;
917 struct vmap_block *vb;
918 unsigned long addr = 0;
919 unsigned int order;
02b709df 920 int purge = 0;
db64fe02
NP
921
922 BUG_ON(size & ~PAGE_MASK);
923 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
aa91c4d8
JK
924 if (WARN_ON(size == 0)) {
925 /*
926 * Allocating 0 bytes isn't what caller wants since
927 * get_order(0) returns funny result. Just warn and terminate
928 * early.
929 */
930 return NULL;
931 }
db64fe02
NP
932 order = get_order(size);
933
934again:
935 rcu_read_lock();
936 vbq = &get_cpu_var(vmap_block_queue);
937 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
938 int i;
939
940 spin_lock(&vb->lock);
6fa3eb70
S
941 if (vb->free < 1UL << order) {
942 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
943 /* free left too small, handle as fragmented scenario */
944 purge = 1;
945 }
02b709df 946 goto next;
6fa3eb70 947 }
02b709df 948
db64fe02
NP
949 i = bitmap_find_free_region(vb->alloc_map,
950 VMAP_BBMAP_BITS, order);
951
02b709df
NP
952 if (i < 0) {
953 if (vb->free + vb->dirty == VMAP_BBMAP_BITS) {
954 /* fragmented and no outstanding allocations */
955 BUG_ON(vb->dirty != VMAP_BBMAP_BITS);
956 purge = 1;
db64fe02 957 }
02b709df 958 goto next;
db64fe02 959 }
02b709df
NP
960 addr = vb->va->va_start + (i << PAGE_SHIFT);
961 BUG_ON(addr_to_vb_idx(addr) !=
962 addr_to_vb_idx(vb->va->va_start));
963 vb->free -= 1UL << order;
964 if (vb->free == 0) {
965 spin_lock(&vbq->lock);
966 list_del_rcu(&vb->free_list);
967 spin_unlock(&vbq->lock);
968 }
969 spin_unlock(&vb->lock);
970 break;
971next:
db64fe02
NP
972 spin_unlock(&vb->lock);
973 }
02b709df
NP
974
975 if (purge)
976 purge_fragmented_blocks_thiscpu();
977
3f04ba85 978 put_cpu_var(vmap_block_queue);
db64fe02
NP
979 rcu_read_unlock();
980
981 if (!addr) {
982 vb = new_vmap_block(gfp_mask);
983 if (IS_ERR(vb))
984 return vb;
985 goto again;
986 }
987
988 return (void *)addr;
989}
990
991static void vb_free(const void *addr, unsigned long size)
992{
993 unsigned long offset;
994 unsigned long vb_idx;
995 unsigned int order;
996 struct vmap_block *vb;
997
998 BUG_ON(size & ~PAGE_MASK);
999 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
b29acbdc
NP
1000
1001 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
1002
db64fe02
NP
1003 order = get_order(size);
1004
1005 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
1006
1007 vb_idx = addr_to_vb_idx((unsigned long)addr);
1008 rcu_read_lock();
1009 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
1010 rcu_read_unlock();
1011 BUG_ON(!vb);
1012
64141da5
JF
1013 vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
1014
db64fe02 1015 spin_lock(&vb->lock);
de560423 1016 BUG_ON(bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order));
d086817d 1017
db64fe02
NP
1018 vb->dirty += 1UL << order;
1019 if (vb->dirty == VMAP_BBMAP_BITS) {
de560423 1020 BUG_ON(vb->free);
db64fe02
NP
1021 spin_unlock(&vb->lock);
1022 free_vmap_block(vb);
1023 } else
1024 spin_unlock(&vb->lock);
1025}
1026
1027/**
1028 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1029 *
1030 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1031 * to amortize TLB flushing overheads. What this means is that any page you
1032 * have now, may, in a former life, have been mapped into kernel virtual
1033 * address by the vmap layer and so there might be some CPUs with TLB entries
1034 * still referencing that page (additional to the regular 1:1 kernel mapping).
1035 *
1036 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1037 * be sure that none of the pages we have control over will have any aliases
1038 * from the vmap layer.
1039 */
1040void vm_unmap_aliases(void)
1041{
1042 unsigned long start = ULONG_MAX, end = 0;
1043 int cpu;
1044 int flush = 0;
1045
9b463334
JF
1046 if (unlikely(!vmap_initialized))
1047 return;
1048
db64fe02
NP
1049 for_each_possible_cpu(cpu) {
1050 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1051 struct vmap_block *vb;
1052
1053 rcu_read_lock();
1054 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1055 int i;
1056
1057 spin_lock(&vb->lock);
1058 i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
1059 while (i < VMAP_BBMAP_BITS) {
1060 unsigned long s, e;
1061 int j;
1062 j = find_next_zero_bit(vb->dirty_map,
1063 VMAP_BBMAP_BITS, i);
1064
1065 s = vb->va->va_start + (i << PAGE_SHIFT);
1066 e = vb->va->va_start + (j << PAGE_SHIFT);
db64fe02
NP
1067 flush = 1;
1068
1069 if (s < start)
1070 start = s;
1071 if (e > end)
1072 end = e;
1073
1074 i = j;
1075 i = find_next_bit(vb->dirty_map,
1076 VMAP_BBMAP_BITS, i);
1077 }
1078 spin_unlock(&vb->lock);
1079 }
1080 rcu_read_unlock();
1081 }
1082
1083 __purge_vmap_area_lazy(&start, &end, 1, flush);
1084}
1085EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1086
1087/**
1088 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1089 * @mem: the pointer returned by vm_map_ram
1090 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1091 */
1092void vm_unmap_ram(const void *mem, unsigned int count)
1093{
1094 unsigned long size = count << PAGE_SHIFT;
1095 unsigned long addr = (unsigned long)mem;
1096
1097 BUG_ON(!addr);
1098 BUG_ON(addr < VMALLOC_START);
1099 BUG_ON(addr > VMALLOC_END);
1100 BUG_ON(addr & (PAGE_SIZE-1));
1101
1102 debug_check_no_locks_freed(mem, size);
cd52858c 1103 vmap_debug_free_range(addr, addr+size);
db64fe02
NP
1104
1105 if (likely(count <= VMAP_MAX_ALLOC))
1106 vb_free(mem, size);
1107 else
1108 free_unmap_vmap_area_addr(addr);
1109}
1110EXPORT_SYMBOL(vm_unmap_ram);
1111
1112/**
1113 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1114 * @pages: an array of pointers to the pages to be mapped
1115 * @count: number of pages
1116 * @node: prefer to allocate data structures on this node
1117 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
e99c97ad
RD
1118 *
1119 * Returns: a pointer to the address that has been mapped, or %NULL on failure
db64fe02
NP
1120 */
1121void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1122{
1123 unsigned long size = count << PAGE_SHIFT;
1124 unsigned long addr;
1125 void *mem;
1126
1127 if (likely(count <= VMAP_MAX_ALLOC)) {
1128 mem = vb_alloc(size, GFP_KERNEL);
1129 if (IS_ERR(mem))
1130 return NULL;
1131 addr = (unsigned long)mem;
1132 } else {
1133 struct vmap_area *va;
1134 va = alloc_vmap_area(size, PAGE_SIZE,
1135 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1136 if (IS_ERR(va))
1137 return NULL;
1138
1139 addr = va->va_start;
1140 mem = (void *)addr;
1141 }
1142 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1143 vm_unmap_ram(mem, count);
1144 return NULL;
1145 }
1146 return mem;
1147}
1148EXPORT_SYMBOL(vm_map_ram);
1149
4341fa45 1150static struct vm_struct *vmlist __initdata;
be9b7335
NP
1151/**
1152 * vm_area_add_early - add vmap area early during boot
1153 * @vm: vm_struct to add
1154 *
1155 * This function is used to add fixed kernel vm area to vmlist before
1156 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
1157 * should contain proper values and the other fields should be zero.
1158 *
1159 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1160 */
1161void __init vm_area_add_early(struct vm_struct *vm)
1162{
1163 struct vm_struct *tmp, **p;
1164
1165 BUG_ON(vmap_initialized);
1166 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1167 if (tmp->addr >= vm->addr) {
1168 BUG_ON(tmp->addr < vm->addr + vm->size);
1169 break;
1170 } else
1171 BUG_ON(tmp->addr + tmp->size > vm->addr);
1172 }
1173 vm->next = *p;
1174 *p = vm;
1175}
1176
f0aa6617
TH
1177/**
1178 * vm_area_register_early - register vmap area early during boot
1179 * @vm: vm_struct to register
c0c0a293 1180 * @align: requested alignment
f0aa6617
TH
1181 *
1182 * This function is used to register kernel vm area before
1183 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1184 * proper values on entry and other fields should be zero. On return,
1185 * vm->addr contains the allocated address.
1186 *
1187 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1188 */
c0c0a293 1189void __init vm_area_register_early(struct vm_struct *vm, size_t align)
f0aa6617
TH
1190{
1191 static size_t vm_init_off __initdata;
c0c0a293
TH
1192 unsigned long addr;
1193
1194 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1195 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
f0aa6617 1196
c0c0a293 1197 vm->addr = (void *)addr;
f0aa6617 1198
be9b7335 1199 vm_area_add_early(vm);
f0aa6617
TH
1200}
1201
db64fe02
NP
1202void __init vmalloc_init(void)
1203{
822c18f2
IK
1204 struct vmap_area *va;
1205 struct vm_struct *tmp;
db64fe02
NP
1206 int i;
1207
1208 for_each_possible_cpu(i) {
1209 struct vmap_block_queue *vbq;
32fcfd40 1210 struct vfree_deferred *p;
db64fe02
NP
1211
1212 vbq = &per_cpu(vmap_block_queue, i);
1213 spin_lock_init(&vbq->lock);
1214 INIT_LIST_HEAD(&vbq->free);
32fcfd40
AV
1215 p = &per_cpu(vfree_deferred, i);
1216 init_llist_head(&p->list);
1217 INIT_WORK(&p->wq, free_work);
db64fe02 1218 }
9b463334 1219
822c18f2
IK
1220 /* Import existing vmlist entries. */
1221 for (tmp = vmlist; tmp; tmp = tmp->next) {
43ebdac4 1222 va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
dbda591d 1223 va->flags = VM_VM_AREA;
822c18f2
IK
1224 va->va_start = (unsigned long)tmp->addr;
1225 va->va_end = va->va_start + tmp->size;
dbda591d 1226 va->vm = tmp;
822c18f2
IK
1227 __insert_vmap_area(va);
1228 }
ca23e405
TH
1229
1230 vmap_area_pcpu_hole = VMALLOC_END;
1231
9b463334 1232 vmap_initialized = true;
db64fe02
NP
1233}
1234
8fc48985
TH
1235/**
1236 * map_kernel_range_noflush - map kernel VM area with the specified pages
1237 * @addr: start of the VM area to map
1238 * @size: size of the VM area to map
1239 * @prot: page protection flags to use
1240 * @pages: pages to map
1241 *
1242 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1243 * specify should have been allocated using get_vm_area() and its
1244 * friends.
1245 *
1246 * NOTE:
1247 * This function does NOT do any cache flushing. The caller is
1248 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1249 * before calling this function.
1250 *
1251 * RETURNS:
1252 * The number of pages mapped on success, -errno on failure.
1253 */
1254int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1255 pgprot_t prot, struct page **pages)
1256{
1257 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1258}
1259
1260/**
1261 * unmap_kernel_range_noflush - unmap kernel VM area
1262 * @addr: start of the VM area to unmap
1263 * @size: size of the VM area to unmap
1264 *
1265 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1266 * specify should have been allocated using get_vm_area() and its
1267 * friends.
1268 *
1269 * NOTE:
1270 * This function does NOT do any cache flushing. The caller is
1271 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1272 * before calling this function and flush_tlb_kernel_range() after.
1273 */
1274void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1275{
1276 vunmap_page_range(addr, addr + size);
1277}
81e88fdc 1278EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
8fc48985
TH
1279
1280/**
1281 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1282 * @addr: start of the VM area to unmap
1283 * @size: size of the VM area to unmap
1284 *
1285 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1286 * the unmapping and tlb after.
1287 */
db64fe02
NP
1288void unmap_kernel_range(unsigned long addr, unsigned long size)
1289{
1290 unsigned long end = addr + size;
f6fcba70
TH
1291
1292 flush_cache_vunmap(addr, end);
db64fe02
NP
1293 vunmap_page_range(addr, end);
1294 flush_tlb_kernel_range(addr, end);
1295}
1296
1297int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
1298{
1299 unsigned long addr = (unsigned long)area->addr;
1300 unsigned long end = addr + area->size - PAGE_SIZE;
1301 int err;
1302
1303 err = vmap_page_range(addr, end, prot, *pages);
1304 if (err > 0) {
1305 *pages += err;
1306 err = 0;
1307 }
1308
1309 return err;
1310}
1311EXPORT_SYMBOL_GPL(map_vm_area);
1312
f5252e00 1313static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
5e6cafc8 1314 unsigned long flags, const void *caller)
cf88c790 1315{
c69480ad 1316 spin_lock(&vmap_area_lock);
cf88c790
TH
1317 vm->flags = flags;
1318 vm->addr = (void *)va->va_start;
1319 vm->size = va->va_end - va->va_start;
1320 vm->caller = caller;
db1aecaf 1321 va->vm = vm;
cf88c790 1322 va->flags |= VM_VM_AREA;
c69480ad 1323 spin_unlock(&vmap_area_lock);
f5252e00 1324}
cf88c790 1325
4341fa45 1326static void clear_vm_unlist(struct vm_struct *vm)
f5252e00 1327{
d4033afd
JK
1328 /*
1329 * Before removing VM_UNLIST,
1330 * we should make sure that vm has proper values.
1331 * Pair with smp_rmb() in show_numa_info().
1332 */
1333 smp_wmb();
f5252e00 1334 vm->flags &= ~VM_UNLIST;
cf88c790
TH
1335}
1336
f5252e00 1337static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
5e6cafc8 1338 unsigned long flags, const void *caller)
f5252e00
MH
1339{
1340 setup_vmalloc_vm(vm, va, flags, caller);
4341fa45 1341 clear_vm_unlist(vm);
f5252e00
MH
1342}
1343
db64fe02 1344static struct vm_struct *__get_vm_area_node(unsigned long size,
2dca6999 1345 unsigned long align, unsigned long flags, unsigned long start,
5e6cafc8 1346 unsigned long end, int node, gfp_t gfp_mask, const void *caller)
db64fe02 1347{
0006526d 1348 struct vmap_area *va;
db64fe02 1349 struct vm_struct *area;
1da177e4 1350
52fd24ca 1351 BUG_ON(in_interrupt());
1da177e4
LT
1352 if (flags & VM_IOREMAP) {
1353 int bit = fls(size);
1354
1355 if (bit > IOREMAP_MAX_ORDER)
1356 bit = IOREMAP_MAX_ORDER;
1357 else if (bit < PAGE_SHIFT)
1358 bit = PAGE_SHIFT;
1359
1360 align = 1ul << bit;
1361 }
db64fe02 1362
1da177e4 1363 size = PAGE_ALIGN(size);
31be8309
OH
1364 if (unlikely(!size))
1365 return NULL;
1da177e4 1366
cf88c790 1367 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
1da177e4
LT
1368 if (unlikely(!area))
1369 return NULL;
1370
1da177e4
LT
1371 /*
1372 * We always allocate a guard page.
1373 */
1374 size += PAGE_SIZE;
1375
db64fe02
NP
1376 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1377 if (IS_ERR(va)) {
1378 kfree(area);
1379 return NULL;
1da177e4 1380 }
1da177e4 1381
f5252e00
MH
1382 /*
1383 * When this function is called from __vmalloc_node_range,
4341fa45
JK
1384 * we add VM_UNLIST flag to avoid accessing uninitialized
1385 * members of vm_struct such as pages and nr_pages fields.
1386 * They will be set later.
f5252e00
MH
1387 */
1388 if (flags & VM_UNLIST)
1389 setup_vmalloc_vm(area, va, flags, caller);
1390 else
1391 insert_vmalloc_vm(area, va, flags, caller);
1392
1da177e4 1393 return area;
1da177e4
LT
1394}
1395
930fc45a
CL
1396struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1397 unsigned long start, unsigned long end)
1398{
00ef2d2f
DR
1399 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
1400 GFP_KERNEL, __builtin_return_address(0));
930fc45a 1401}
5992b6da 1402EXPORT_SYMBOL_GPL(__get_vm_area);
930fc45a 1403
c2968612
BH
1404struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1405 unsigned long start, unsigned long end,
5e6cafc8 1406 const void *caller)
c2968612 1407{
00ef2d2f
DR
1408 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
1409 GFP_KERNEL, caller);
c2968612
BH
1410}
1411
1da177e4 1412/**
183ff22b 1413 * get_vm_area - reserve a contiguous kernel virtual area
1da177e4
LT
1414 * @size: size of the area
1415 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
1416 *
1417 * Search an area of @size in the kernel virtual mapping area,
1418 * and reserved it for out purposes. Returns the area descriptor
1419 * on success or %NULL on failure.
1420 */
1421struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
1422{
2dca6999 1423 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
00ef2d2f
DR
1424 NUMA_NO_NODE, GFP_KERNEL,
1425 __builtin_return_address(0));
23016969
CL
1426}
1427
1428struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
5e6cafc8 1429 const void *caller)
23016969 1430{
2dca6999 1431 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
00ef2d2f 1432 NUMA_NO_NODE, GFP_KERNEL, caller);
1da177e4
LT
1433}
1434
e9da6e99
MS
1435/**
1436 * find_vm_area - find a continuous kernel virtual area
1437 * @addr: base address
1438 *
1439 * Search for the kernel VM area starting at @addr, and return it.
1440 * It is up to the caller to do all required locking to keep the returned
1441 * pointer valid.
1442 */
1443struct vm_struct *find_vm_area(const void *addr)
83342314 1444{
db64fe02 1445 struct vmap_area *va;
83342314 1446
db64fe02
NP
1447 va = find_vmap_area((unsigned long)addr);
1448 if (va && va->flags & VM_VM_AREA)
db1aecaf 1449 return va->vm;
1da177e4 1450
1da177e4 1451 return NULL;
1da177e4
LT
1452}
1453
7856dfeb 1454/**
183ff22b 1455 * remove_vm_area - find and remove a continuous kernel virtual area
7856dfeb
AK
1456 * @addr: base address
1457 *
1458 * Search for the kernel VM area starting at @addr, and remove it.
1459 * This function returns the found VM area, but using it is NOT safe
1460 * on SMP machines, except for its size or flags.
1461 */
b3bdda02 1462struct vm_struct *remove_vm_area(const void *addr)
7856dfeb 1463{
db64fe02
NP
1464 struct vmap_area *va;
1465
1466 va = find_vmap_area((unsigned long)addr);
1467 if (va && va->flags & VM_VM_AREA) {
db1aecaf 1468 struct vm_struct *vm = va->vm;
f5252e00 1469
c69480ad
JK
1470 spin_lock(&vmap_area_lock);
1471 va->vm = NULL;
1472 va->flags &= ~VM_VM_AREA;
1473 spin_unlock(&vmap_area_lock);
1474
dd32c279
KH
1475 vmap_debug_free_range(va->va_start, va->va_end);
1476 free_unmap_vmap_area(va);
1477 vm->size -= PAGE_SIZE;
1478
db64fe02
NP
1479 return vm;
1480 }
1481 return NULL;
7856dfeb
AK
1482}
1483
b3bdda02 1484static void __vunmap(const void *addr, int deallocate_pages)
1da177e4
LT
1485{
1486 struct vm_struct *area;
1487
1488 if (!addr)
1489 return;
1490
1491 if ((PAGE_SIZE-1) & (unsigned long)addr) {
4c8573e2 1492 WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
1da177e4
LT
1493 return;
1494 }
1495
1496 area = remove_vm_area(addr);
1497 if (unlikely(!area)) {
4c8573e2 1498 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
1da177e4 1499 addr);
1da177e4
LT
1500 return;
1501 }
1502
9a11b49a 1503 debug_check_no_locks_freed(addr, area->size);
3ac7fe5a 1504 debug_check_no_obj_freed(addr, area->size);
9a11b49a 1505
1da177e4
LT
1506 if (deallocate_pages) {
1507 int i;
1508
1509 for (i = 0; i < area->nr_pages; i++) {
bf53d6f8
CL
1510 struct page *page = area->pages[i];
1511
1512 BUG_ON(!page);
1513 __free_page(page);
1da177e4
LT
1514 }
1515
8757d5fa 1516 if (area->flags & VM_VPAGES)
1da177e4
LT
1517 vfree(area->pages);
1518 else
1519 kfree(area->pages);
1520 }
1521
1522 kfree(area);
1523 return;
1524}
32fcfd40 1525
1da177e4
LT
1526/**
1527 * vfree - release memory allocated by vmalloc()
1da177e4
LT
1528 * @addr: memory base address
1529 *
183ff22b 1530 * Free the virtually continuous memory area starting at @addr, as
80e93eff
PE
1531 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1532 * NULL, no operation is performed.
1da177e4 1533 *
32fcfd40
AV
1534 * Must not be called in NMI context (strictly speaking, only if we don't
1535 * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
1536 * conventions for vfree() arch-depenedent would be a really bad idea)
c9fcee51
AM
1537 *
1538 * NOTE: assumes that the object at *addr has a size >= sizeof(llist_node)
32fcfd40 1539 *
1da177e4 1540 */
b3bdda02 1541void vfree(const void *addr)
1da177e4 1542{
32fcfd40 1543 BUG_ON(in_nmi());
89219d37
CM
1544
1545 kmemleak_free(addr);
1546
32fcfd40
AV
1547 if (!addr)
1548 return;
1549 if (unlikely(in_interrupt())) {
1550 struct vfree_deferred *p = &__get_cpu_var(vfree_deferred);
1551 llist_add((struct llist_node *)addr, &p->list);
1552 schedule_work(&p->wq);
1553 } else
1554 __vunmap(addr, 1);
1da177e4 1555}
1da177e4
LT
1556EXPORT_SYMBOL(vfree);
1557
1558/**
1559 * vunmap - release virtual mapping obtained by vmap()
1da177e4
LT
1560 * @addr: memory base address
1561 *
1562 * Free the virtually contiguous memory area starting at @addr,
1563 * which was created from the page array passed to vmap().
1564 *
80e93eff 1565 * Must not be called in interrupt context.
1da177e4 1566 */
b3bdda02 1567void vunmap(const void *addr)
1da177e4
LT
1568{
1569 BUG_ON(in_interrupt());
34754b69 1570 might_sleep();
32fcfd40
AV
1571 if (addr)
1572 __vunmap(addr, 0);
1da177e4 1573}
1da177e4
LT
1574EXPORT_SYMBOL(vunmap);
1575
1576/**
1577 * vmap - map an array of pages into virtually contiguous space
1da177e4
LT
1578 * @pages: array of page pointers
1579 * @count: number of pages to map
1580 * @flags: vm_area->flags
1581 * @prot: page protection for the mapping
1582 *
1583 * Maps @count pages from @pages into contiguous kernel virtual
1584 * space.
1585 */
1586void *vmap(struct page **pages, unsigned int count,
1587 unsigned long flags, pgprot_t prot)
1588{
1589 struct vm_struct *area;
1590
34754b69
PZ
1591 might_sleep();
1592
4481374c 1593 if (count > totalram_pages)
1da177e4
LT
1594 return NULL;
1595
23016969
CL
1596 area = get_vm_area_caller((count << PAGE_SHIFT), flags,
1597 __builtin_return_address(0));
1da177e4
LT
1598 if (!area)
1599 return NULL;
23016969 1600
1da177e4
LT
1601 if (map_vm_area(area, prot, &pages)) {
1602 vunmap(area->addr);
1603 return NULL;
1604 }
1605
1606 return area->addr;
1607}
1da177e4
LT
1608EXPORT_SYMBOL(vmap);
1609
2dca6999
DM
1610static void *__vmalloc_node(unsigned long size, unsigned long align,
1611 gfp_t gfp_mask, pgprot_t prot,
5e6cafc8 1612 int node, const void *caller);
e31d9eb5 1613static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
5e6cafc8 1614 pgprot_t prot, int node, const void *caller)
1da177e4 1615{
22943ab1 1616 const int order = 0;
1da177e4
LT
1617 struct page **pages;
1618 unsigned int nr_pages, array_size, i;
976d6dfb 1619 gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
1da177e4
LT
1620
1621 nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
1622 array_size = (nr_pages * sizeof(struct page *));
1623
1624 area->nr_pages = nr_pages;
1625 /* Please note that the recursion is strictly bounded. */
8757d5fa 1626 if (array_size > PAGE_SIZE) {
976d6dfb 1627 pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,
23016969 1628 PAGE_KERNEL, node, caller);
8757d5fa 1629 area->flags |= VM_VPAGES;
286e1ea3 1630 } else {
976d6dfb 1631 pages = kmalloc_node(array_size, nested_gfp, node);
286e1ea3 1632 }
1da177e4 1633 area->pages = pages;
23016969 1634 area->caller = caller;
1da177e4
LT
1635 if (!area->pages) {
1636 remove_vm_area(area->addr);
1637 kfree(area);
1638 return NULL;
1639 }
1da177e4
LT
1640
1641 for (i = 0; i < area->nr_pages; i++) {
bf53d6f8 1642 struct page *page;
22943ab1 1643 gfp_t tmp_mask = gfp_mask | __GFP_NOWARN;
bf53d6f8 1644
930fc45a 1645 if (node < 0)
22943ab1 1646 page = alloc_page(tmp_mask);
930fc45a 1647 else
22943ab1 1648 page = alloc_pages_node(node, tmp_mask, order);
bf53d6f8
CL
1649
1650 if (unlikely(!page)) {
1da177e4
LT
1651 /* Successfully allocated i pages, free them in __vunmap() */
1652 area->nr_pages = i;
1653 goto fail;
1654 }
bf53d6f8 1655 area->pages[i] = page;
1da177e4
LT
1656 }
1657
1658 if (map_vm_area(area, prot, &pages))
1659 goto fail;
1660 return area->addr;
1661
1662fail:
3ee9a4f0
JP
1663 warn_alloc_failed(gfp_mask, order,
1664 "vmalloc: allocation failure, allocated %ld of %ld bytes\n",
22943ab1 1665 (area->nr_pages*PAGE_SIZE), area->size);
1da177e4
LT
1666 vfree(area->addr);
1667 return NULL;
1668}
1669
1670/**
d0a21265 1671 * __vmalloc_node_range - allocate virtually contiguous memory
1da177e4 1672 * @size: allocation size
2dca6999 1673 * @align: desired alignment
d0a21265
DR
1674 * @start: vm area range start
1675 * @end: vm area range end
1da177e4
LT
1676 * @gfp_mask: flags for the page level allocator
1677 * @prot: protection mask for the allocated pages
00ef2d2f 1678 * @node: node to use for allocation or NUMA_NO_NODE
c85d194b 1679 * @caller: caller's return address
1da177e4
LT
1680 *
1681 * Allocate enough pages to cover @size from the page level
1682 * allocator with @gfp_mask flags. Map them into contiguous
1683 * kernel virtual space, using a pagetable protection of @prot.
1684 */
d0a21265
DR
1685void *__vmalloc_node_range(unsigned long size, unsigned long align,
1686 unsigned long start, unsigned long end, gfp_t gfp_mask,
5e6cafc8 1687 pgprot_t prot, int node, const void *caller)
1da177e4
LT
1688{
1689 struct vm_struct *area;
89219d37
CM
1690 void *addr;
1691 unsigned long real_size = size;
1da177e4
LT
1692
1693 size = PAGE_ALIGN(size);
4481374c 1694 if (!size || (size >> PAGE_SHIFT) > totalram_pages)
de7d2b56 1695 goto fail;
1da177e4 1696
f5252e00
MH
1697 area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNLIST,
1698 start, end, node, gfp_mask, caller);
1da177e4 1699 if (!area)
de7d2b56 1700 goto fail;
1da177e4 1701
89219d37 1702 addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
1368edf0
MG
1703 if (!addr)
1704 return NULL;
89219d37 1705
f5252e00 1706 /*
4341fa45
JK
1707 * In this function, newly allocated vm_struct has VM_UNLIST flag.
1708 * It means that vm_struct is not fully initialized.
1709 * Now, it is fully initialized, so remove this flag here.
f5252e00 1710 */
4341fa45 1711 clear_vm_unlist(area);
f5252e00 1712
89219d37
CM
1713 /*
1714 * A ref_count = 3 is needed because the vm_struct and vmap_area
1715 * structures allocated in the __get_vm_area_node() function contain
1716 * references to the virtual address of the vmalloc'ed block.
1717 */
1718 kmemleak_alloc(addr, real_size, 3, gfp_mask);
1719
1720 return addr;
de7d2b56
JP
1721
1722fail:
1723 warn_alloc_failed(gfp_mask, 0,
1724 "vmalloc: allocation failure: %lu bytes\n",
1725 real_size);
1726 return NULL;
1da177e4
LT
1727}
1728
d0a21265
DR
1729/**
1730 * __vmalloc_node - allocate virtually contiguous memory
1731 * @size: allocation size
1732 * @align: desired alignment
1733 * @gfp_mask: flags for the page level allocator
1734 * @prot: protection mask for the allocated pages
00ef2d2f 1735 * @node: node to use for allocation or NUMA_NO_NODE
d0a21265
DR
1736 * @caller: caller's return address
1737 *
1738 * Allocate enough pages to cover @size from the page level
1739 * allocator with @gfp_mask flags. Map them into contiguous
1740 * kernel virtual space, using a pagetable protection of @prot.
1741 */
1742static void *__vmalloc_node(unsigned long size, unsigned long align,
1743 gfp_t gfp_mask, pgprot_t prot,
5e6cafc8 1744 int node, const void *caller)
d0a21265
DR
1745{
1746 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
1747 gfp_mask, prot, node, caller);
1748}
1749
930fc45a
CL
1750void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1751{
00ef2d2f 1752 return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE,
23016969 1753 __builtin_return_address(0));
930fc45a 1754}
1da177e4
LT
1755EXPORT_SYMBOL(__vmalloc);
1756
e1ca7788
DY
1757static inline void *__vmalloc_node_flags(unsigned long size,
1758 int node, gfp_t flags)
1759{
1760 return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
1761 node, __builtin_return_address(0));
1762}
1763
1da177e4
LT
1764/**
1765 * vmalloc - allocate virtually contiguous memory
1da177e4 1766 * @size: allocation size
1da177e4
LT
1767 * Allocate enough pages to cover @size from the page level
1768 * allocator and map them into contiguous kernel virtual space.
1769 *
c1c8897f 1770 * For tight control over page level allocator and protection flags
1da177e4
LT
1771 * use __vmalloc() instead.
1772 */
1773void *vmalloc(unsigned long size)
1774{
00ef2d2f
DR
1775 return __vmalloc_node_flags(size, NUMA_NO_NODE,
1776 GFP_KERNEL | __GFP_HIGHMEM);
1da177e4 1777}
1da177e4
LT
1778EXPORT_SYMBOL(vmalloc);
1779
e1ca7788
DY
1780/**
1781 * vzalloc - allocate virtually contiguous memory with zero fill
1782 * @size: allocation size
1783 * Allocate enough pages to cover @size from the page level
1784 * allocator and map them into contiguous kernel virtual space.
1785 * The memory allocated is set to zero.
1786 *
1787 * For tight control over page level allocator and protection flags
1788 * use __vmalloc() instead.
1789 */
1790void *vzalloc(unsigned long size)
1791{
00ef2d2f 1792 return __vmalloc_node_flags(size, NUMA_NO_NODE,
e1ca7788
DY
1793 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1794}
1795EXPORT_SYMBOL(vzalloc);
1796
83342314 1797/**
ead04089
REB
1798 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1799 * @size: allocation size
83342314 1800 *
ead04089
REB
1801 * The resulting memory area is zeroed so it can be mapped to userspace
1802 * without leaking data.
83342314
NP
1803 */
1804void *vmalloc_user(unsigned long size)
1805{
1806 struct vm_struct *area;
1807 void *ret;
1808
2dca6999
DM
1809 ret = __vmalloc_node(size, SHMLBA,
1810 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
00ef2d2f
DR
1811 PAGE_KERNEL, NUMA_NO_NODE,
1812 __builtin_return_address(0));
2b4ac44e 1813 if (ret) {
db64fe02 1814 area = find_vm_area(ret);
2b4ac44e 1815 area->flags |= VM_USERMAP;
2b4ac44e 1816 }
83342314
NP
1817 return ret;
1818}
1819EXPORT_SYMBOL(vmalloc_user);
1820
930fc45a
CL
1821/**
1822 * vmalloc_node - allocate memory on a specific node
930fc45a 1823 * @size: allocation size
d44e0780 1824 * @node: numa node
930fc45a
CL
1825 *
1826 * Allocate enough pages to cover @size from the page level
1827 * allocator and map them into contiguous kernel virtual space.
1828 *
c1c8897f 1829 * For tight control over page level allocator and protection flags
930fc45a
CL
1830 * use __vmalloc() instead.
1831 */
1832void *vmalloc_node(unsigned long size, int node)
1833{
2dca6999 1834 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
23016969 1835 node, __builtin_return_address(0));
930fc45a
CL
1836}
1837EXPORT_SYMBOL(vmalloc_node);
1838
e1ca7788
DY
1839/**
1840 * vzalloc_node - allocate memory on a specific node with zero fill
1841 * @size: allocation size
1842 * @node: numa node
1843 *
1844 * Allocate enough pages to cover @size from the page level
1845 * allocator and map them into contiguous kernel virtual space.
1846 * The memory allocated is set to zero.
1847 *
1848 * For tight control over page level allocator and protection flags
1849 * use __vmalloc_node() instead.
1850 */
1851void *vzalloc_node(unsigned long size, int node)
1852{
1853 return __vmalloc_node_flags(size, node,
1854 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1855}
1856EXPORT_SYMBOL(vzalloc_node);
1857
4dc3b16b
PP
1858#ifndef PAGE_KERNEL_EXEC
1859# define PAGE_KERNEL_EXEC PAGE_KERNEL
1860#endif
1861
1da177e4
LT
1862/**
1863 * vmalloc_exec - allocate virtually contiguous, executable memory
1da177e4
LT
1864 * @size: allocation size
1865 *
1866 * Kernel-internal function to allocate enough pages to cover @size
1867 * the page level allocator and map them into contiguous and
1868 * executable kernel virtual space.
1869 *
c1c8897f 1870 * For tight control over page level allocator and protection flags
1da177e4
LT
1871 * use __vmalloc() instead.
1872 */
1873
1da177e4
LT
1874void *vmalloc_exec(unsigned long size)
1875{
2dca6999 1876 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
00ef2d2f 1877 NUMA_NO_NODE, __builtin_return_address(0));
1da177e4
LT
1878}
1879
0d08e0d3 1880#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
7ac674f5 1881#define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
0d08e0d3 1882#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
7ac674f5 1883#define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
0d08e0d3
AK
1884#else
1885#define GFP_VMALLOC32 GFP_KERNEL
1886#endif
1887
1da177e4
LT
1888/**
1889 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
1da177e4
LT
1890 * @size: allocation size
1891 *
1892 * Allocate enough 32bit PA addressable pages to cover @size from the
1893 * page level allocator and map them into contiguous kernel virtual space.
1894 */
1895void *vmalloc_32(unsigned long size)
1896{
2dca6999 1897 return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
00ef2d2f 1898 NUMA_NO_NODE, __builtin_return_address(0));
1da177e4 1899}
1da177e4
LT
1900EXPORT_SYMBOL(vmalloc_32);
1901
83342314 1902/**
ead04089 1903 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
83342314 1904 * @size: allocation size
ead04089
REB
1905 *
1906 * The resulting memory area is 32bit addressable and zeroed so it can be
1907 * mapped to userspace without leaking data.
83342314
NP
1908 */
1909void *vmalloc_32_user(unsigned long size)
1910{
1911 struct vm_struct *area;
1912 void *ret;
1913
2dca6999 1914 ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
00ef2d2f 1915 NUMA_NO_NODE, __builtin_return_address(0));
2b4ac44e 1916 if (ret) {
db64fe02 1917 area = find_vm_area(ret);
2b4ac44e 1918 area->flags |= VM_USERMAP;
2b4ac44e 1919 }
83342314
NP
1920 return ret;
1921}
1922EXPORT_SYMBOL(vmalloc_32_user);
1923
d0107eb0
KH
1924/*
1925 * small helper routine , copy contents to buf from addr.
1926 * If the page is not present, fill zero.
1927 */
1928
1929static int aligned_vread(char *buf, char *addr, unsigned long count)
1930{
1931 struct page *p;
1932 int copied = 0;
1933
1934 while (count) {
1935 unsigned long offset, length;
1936
1937 offset = (unsigned long)addr & ~PAGE_MASK;
1938 length = PAGE_SIZE - offset;
1939 if (length > count)
1940 length = count;
1941 p = vmalloc_to_page(addr);
1942 /*
1943 * To do safe access to this _mapped_ area, we need
1944 * lock. But adding lock here means that we need to add
1945 * overhead of vmalloc()/vfree() calles for this _debug_
1946 * interface, rarely used. Instead of that, we'll use
1947 * kmap() and get small overhead in this access function.
1948 */
1949 if (p) {
1950 /*
1951 * we can expect USER0 is not used (see vread/vwrite's
1952 * function description)
1953 */
9b04c5fe 1954 void *map = kmap_atomic(p);
d0107eb0 1955 memcpy(buf, map + offset, length);
9b04c5fe 1956 kunmap_atomic(map);
d0107eb0
KH
1957 } else
1958 memset(buf, 0, length);
1959
1960 addr += length;
1961 buf += length;
1962 copied += length;
1963 count -= length;
1964 }
1965 return copied;
1966}
1967
1968static int aligned_vwrite(char *buf, char *addr, unsigned long count)
1969{
1970 struct page *p;
1971 int copied = 0;
1972
1973 while (count) {
1974 unsigned long offset, length;
1975
1976 offset = (unsigned long)addr & ~PAGE_MASK;
1977 length = PAGE_SIZE - offset;
1978 if (length > count)
1979 length = count;
1980 p = vmalloc_to_page(addr);
1981 /*
1982 * To do safe access to this _mapped_ area, we need
1983 * lock. But adding lock here means that we need to add
1984 * overhead of vmalloc()/vfree() calles for this _debug_
1985 * interface, rarely used. Instead of that, we'll use
1986 * kmap() and get small overhead in this access function.
1987 */
1988 if (p) {
1989 /*
1990 * we can expect USER0 is not used (see vread/vwrite's
1991 * function description)
1992 */
9b04c5fe 1993 void *map = kmap_atomic(p);
d0107eb0 1994 memcpy(map + offset, buf, length);
9b04c5fe 1995 kunmap_atomic(map);
d0107eb0
KH
1996 }
1997 addr += length;
1998 buf += length;
1999 copied += length;
2000 count -= length;
2001 }
2002 return copied;
2003}
2004
2005/**
2006 * vread() - read vmalloc area in a safe way.
2007 * @buf: buffer for reading data
2008 * @addr: vm address.
2009 * @count: number of bytes to be read.
2010 *
2011 * Returns # of bytes which addr and buf should be increased.
2012 * (same number to @count). Returns 0 if [addr...addr+count) doesn't
2013 * includes any intersect with alive vmalloc area.
2014 *
2015 * This function checks that addr is a valid vmalloc'ed area, and
2016 * copy data from that area to a given buffer. If the given memory range
2017 * of [addr...addr+count) includes some valid address, data is copied to
2018 * proper area of @buf. If there are memory holes, they'll be zero-filled.
2019 * IOREMAP area is treated as memory hole and no copy is done.
2020 *
2021 * If [addr...addr+count) doesn't includes any intersects with alive
a8e5202d 2022 * vm_struct area, returns 0. @buf should be kernel's buffer.
d0107eb0
KH
2023 *
2024 * Note: In usual ops, vread() is never necessary because the caller
2025 * should know vmalloc() area is valid and can use memcpy().
2026 * This is for routines which have to access vmalloc area without
2027 * any informaion, as /dev/kmem.
2028 *
2029 */
2030
1da177e4
LT
2031long vread(char *buf, char *addr, unsigned long count)
2032{
e81ce85f
JK
2033 struct vmap_area *va;
2034 struct vm_struct *vm;
1da177e4 2035 char *vaddr, *buf_start = buf;
d0107eb0 2036 unsigned long buflen = count;
1da177e4
LT
2037 unsigned long n;
2038
2039 /* Don't allow overflow */
2040 if ((unsigned long) addr + count < count)
2041 count = -(unsigned long) addr;
2042
e81ce85f
JK
2043 spin_lock(&vmap_area_lock);
2044 list_for_each_entry(va, &vmap_area_list, list) {
2045 if (!count)
2046 break;
2047
2048 if (!(va->flags & VM_VM_AREA))
2049 continue;
2050
2051 vm = va->vm;
2052 vaddr = (char *) vm->addr;
2053 if (addr >= vaddr + vm->size - PAGE_SIZE)
1da177e4
LT
2054 continue;
2055 while (addr < vaddr) {
2056 if (count == 0)
2057 goto finished;
2058 *buf = '\0';
2059 buf++;
2060 addr++;
2061 count--;
2062 }
e81ce85f 2063 n = vaddr + vm->size - PAGE_SIZE - addr;
d0107eb0
KH
2064 if (n > count)
2065 n = count;
e81ce85f 2066 if (!(vm->flags & VM_IOREMAP))
d0107eb0
KH
2067 aligned_vread(buf, addr, n);
2068 else /* IOREMAP area is treated as memory hole */
2069 memset(buf, 0, n);
2070 buf += n;
2071 addr += n;
2072 count -= n;
1da177e4
LT
2073 }
2074finished:
e81ce85f 2075 spin_unlock(&vmap_area_lock);
d0107eb0
KH
2076
2077 if (buf == buf_start)
2078 return 0;
2079 /* zero-fill memory holes */
2080 if (buf != buf_start + buflen)
2081 memset(buf, 0, buflen - (buf - buf_start));
2082
2083 return buflen;
1da177e4
LT
2084}
2085
d0107eb0
KH
2086/**
2087 * vwrite() - write vmalloc area in a safe way.
2088 * @buf: buffer for source data
2089 * @addr: vm address.
2090 * @count: number of bytes to be read.
2091 *
2092 * Returns # of bytes which addr and buf should be incresed.
2093 * (same number to @count).
2094 * If [addr...addr+count) doesn't includes any intersect with valid
2095 * vmalloc area, returns 0.
2096 *
2097 * This function checks that addr is a valid vmalloc'ed area, and
2098 * copy data from a buffer to the given addr. If specified range of
2099 * [addr...addr+count) includes some valid address, data is copied from
2100 * proper area of @buf. If there are memory holes, no copy to hole.
2101 * IOREMAP area is treated as memory hole and no copy is done.
2102 *
2103 * If [addr...addr+count) doesn't includes any intersects with alive
a8e5202d 2104 * vm_struct area, returns 0. @buf should be kernel's buffer.
d0107eb0
KH
2105 *
2106 * Note: In usual ops, vwrite() is never necessary because the caller
2107 * should know vmalloc() area is valid and can use memcpy().
2108 * This is for routines which have to access vmalloc area without
2109 * any informaion, as /dev/kmem.
d0107eb0
KH
2110 */
2111
1da177e4
LT
2112long vwrite(char *buf, char *addr, unsigned long count)
2113{
e81ce85f
JK
2114 struct vmap_area *va;
2115 struct vm_struct *vm;
d0107eb0
KH
2116 char *vaddr;
2117 unsigned long n, buflen;
2118 int copied = 0;
1da177e4
LT
2119
2120 /* Don't allow overflow */
2121 if ((unsigned long) addr + count < count)
2122 count = -(unsigned long) addr;
d0107eb0 2123 buflen = count;
1da177e4 2124
e81ce85f
JK
2125 spin_lock(&vmap_area_lock);
2126 list_for_each_entry(va, &vmap_area_list, list) {
2127 if (!count)
2128 break;
2129
2130 if (!(va->flags & VM_VM_AREA))
2131 continue;
2132
2133 vm = va->vm;
2134 vaddr = (char *) vm->addr;
2135 if (addr >= vaddr + vm->size - PAGE_SIZE)
1da177e4
LT
2136 continue;
2137 while (addr < vaddr) {
2138 if (count == 0)
2139 goto finished;
2140 buf++;
2141 addr++;
2142 count--;
2143 }
e81ce85f 2144 n = vaddr + vm->size - PAGE_SIZE - addr;
d0107eb0
KH
2145 if (n > count)
2146 n = count;
e81ce85f 2147 if (!(vm->flags & VM_IOREMAP)) {
d0107eb0
KH
2148 aligned_vwrite(buf, addr, n);
2149 copied++;
2150 }
2151 buf += n;
2152 addr += n;
2153 count -= n;
1da177e4
LT
2154 }
2155finished:
e81ce85f 2156 spin_unlock(&vmap_area_lock);
d0107eb0
KH
2157 if (!copied)
2158 return 0;
2159 return buflen;
1da177e4 2160}
83342314
NP
2161
2162/**
2163 * remap_vmalloc_range - map vmalloc pages to userspace
83342314
NP
2164 * @vma: vma to cover (map full range of vma)
2165 * @addr: vmalloc memory
2166 * @pgoff: number of pages into addr before first page to map
7682486b
RD
2167 *
2168 * Returns: 0 for success, -Exxx on failure
83342314
NP
2169 *
2170 * This function checks that addr is a valid vmalloc'ed area, and
2171 * that it is big enough to cover the vma. Will return failure if
2172 * that criteria isn't met.
2173 *
72fd4a35 2174 * Similar to remap_pfn_range() (see mm/memory.c)
83342314
NP
2175 */
2176int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
2177 unsigned long pgoff)
2178{
2179 struct vm_struct *area;
2180 unsigned long uaddr = vma->vm_start;
2181 unsigned long usize = vma->vm_end - vma->vm_start;
83342314
NP
2182
2183 if ((PAGE_SIZE-1) & (unsigned long)addr)
2184 return -EINVAL;
2185
db64fe02 2186 area = find_vm_area(addr);
83342314 2187 if (!area)
db64fe02 2188 return -EINVAL;
83342314
NP
2189
2190 if (!(area->flags & VM_USERMAP))
db64fe02 2191 return -EINVAL;
83342314
NP
2192
2193 if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
db64fe02 2194 return -EINVAL;
83342314
NP
2195
2196 addr += pgoff << PAGE_SHIFT;
2197 do {
2198 struct page *page = vmalloc_to_page(addr);
db64fe02
NP
2199 int ret;
2200
83342314
NP
2201 ret = vm_insert_page(vma, uaddr, page);
2202 if (ret)
2203 return ret;
2204
2205 uaddr += PAGE_SIZE;
2206 addr += PAGE_SIZE;
2207 usize -= PAGE_SIZE;
2208 } while (usize > 0);
2209
314e51b9 2210 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
83342314 2211
db64fe02 2212 return 0;
83342314
NP
2213}
2214EXPORT_SYMBOL(remap_vmalloc_range);
2215
1eeb66a1
CH
2216/*
2217 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
2218 * have one.
2219 */
2220void __attribute__((weak)) vmalloc_sync_all(void)
2221{
2222}
5f4352fb
JF
2223
2224
2f569afd 2225static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
5f4352fb 2226{
cd12909c
DV
2227 pte_t ***p = data;
2228
2229 if (p) {
2230 *(*p) = pte;
2231 (*p)++;
2232 }
5f4352fb
JF
2233 return 0;
2234}
2235
2236/**
2237 * alloc_vm_area - allocate a range of kernel address space
2238 * @size: size of the area
cd12909c 2239 * @ptes: returns the PTEs for the address space
7682486b
RD
2240 *
2241 * Returns: NULL on failure, vm_struct on success
5f4352fb
JF
2242 *
2243 * This function reserves a range of kernel address space, and
2244 * allocates pagetables to map that range. No actual mappings
cd12909c
DV
2245 * are created.
2246 *
2247 * If @ptes is non-NULL, pointers to the PTEs (in init_mm)
2248 * allocated for the VM area are returned.
5f4352fb 2249 */
cd12909c 2250struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
5f4352fb
JF
2251{
2252 struct vm_struct *area;
2253
23016969
CL
2254 area = get_vm_area_caller(size, VM_IOREMAP,
2255 __builtin_return_address(0));
5f4352fb
JF
2256 if (area == NULL)
2257 return NULL;
2258
2259 /*
2260 * This ensures that page tables are constructed for this region
2261 * of kernel virtual address space and mapped into init_mm.
2262 */
2263 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
cd12909c 2264 size, f, ptes ? &ptes : NULL)) {
5f4352fb
JF
2265 free_vm_area(area);
2266 return NULL;
2267 }
2268
5f4352fb
JF
2269 return area;
2270}
2271EXPORT_SYMBOL_GPL(alloc_vm_area);
2272
2273void free_vm_area(struct vm_struct *area)
2274{
2275 struct vm_struct *ret;
2276 ret = remove_vm_area(area->addr);
2277 BUG_ON(ret != area);
2278 kfree(area);
2279}
2280EXPORT_SYMBOL_GPL(free_vm_area);
a10aa579 2281
4f8b02b4 2282#ifdef CONFIG_SMP
ca23e405
TH
2283static struct vmap_area *node_to_va(struct rb_node *n)
2284{
2285 return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
2286}
2287
2288/**
2289 * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
2290 * @end: target address
2291 * @pnext: out arg for the next vmap_area
2292 * @pprev: out arg for the previous vmap_area
2293 *
2294 * Returns: %true if either or both of next and prev are found,
2295 * %false if no vmap_area exists
2296 *
2297 * Find vmap_areas end addresses of which enclose @end. ie. if not
2298 * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
2299 */
2300static bool pvm_find_next_prev(unsigned long end,
2301 struct vmap_area **pnext,
2302 struct vmap_area **pprev)
2303{
2304 struct rb_node *n = vmap_area_root.rb_node;
2305 struct vmap_area *va = NULL;
2306
2307 while (n) {
2308 va = rb_entry(n, struct vmap_area, rb_node);
2309 if (end < va->va_end)
2310 n = n->rb_left;
2311 else if (end > va->va_end)
2312 n = n->rb_right;
2313 else
2314 break;
2315 }
2316
2317 if (!va)
2318 return false;
2319
2320 if (va->va_end > end) {
2321 *pnext = va;
2322 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2323 } else {
2324 *pprev = va;
2325 *pnext = node_to_va(rb_next(&(*pprev)->rb_node));
2326 }
2327 return true;
2328}
2329
2330/**
2331 * pvm_determine_end - find the highest aligned address between two vmap_areas
2332 * @pnext: in/out arg for the next vmap_area
2333 * @pprev: in/out arg for the previous vmap_area
2334 * @align: alignment
2335 *
2336 * Returns: determined end address
2337 *
2338 * Find the highest aligned address between *@pnext and *@pprev below
2339 * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned
2340 * down address is between the end addresses of the two vmap_areas.
2341 *
2342 * Please note that the address returned by this function may fall
2343 * inside *@pnext vmap_area. The caller is responsible for checking
2344 * that.
2345 */
2346static unsigned long pvm_determine_end(struct vmap_area **pnext,
2347 struct vmap_area **pprev,
2348 unsigned long align)
2349{
2350 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2351 unsigned long addr;
2352
2353 if (*pnext)
2354 addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
2355 else
2356 addr = vmalloc_end;
2357
2358 while (*pprev && (*pprev)->va_end > addr) {
2359 *pnext = *pprev;
2360 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2361 }
2362
2363 return addr;
2364}
2365
2366/**
2367 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
2368 * @offsets: array containing offset of each area
2369 * @sizes: array containing size of each area
2370 * @nr_vms: the number of areas to allocate
2371 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
ca23e405
TH
2372 *
2373 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
2374 * vm_structs on success, %NULL on failure
2375 *
2376 * Percpu allocator wants to use congruent vm areas so that it can
2377 * maintain the offsets among percpu areas. This function allocates
ec3f64fc
DR
2378 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
2379 * be scattered pretty far, distance between two areas easily going up
2380 * to gigabytes. To avoid interacting with regular vmallocs, these
2381 * areas are allocated from top.
ca23e405
TH
2382 *
2383 * Despite its complicated look, this allocator is rather simple. It
2384 * does everything top-down and scans areas from the end looking for
2385 * matching slot. While scanning, if any of the areas overlaps with
2386 * existing vmap_area, the base address is pulled down to fit the
2387 * area. Scanning is repeated till all the areas fit and then all
2388 * necessary data structres are inserted and the result is returned.
2389 */
2390struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
2391 const size_t *sizes, int nr_vms,
ec3f64fc 2392 size_t align)
ca23e405
TH
2393{
2394 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
2395 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2396 struct vmap_area **vas, *prev, *next;
2397 struct vm_struct **vms;
2398 int area, area2, last_area, term_area;
2399 unsigned long base, start, end, last_end;
2400 bool purged = false;
2401
ca23e405
TH
2402 /* verify parameters and allocate data structures */
2403 BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align));
2404 for (last_area = 0, area = 0; area < nr_vms; area++) {
2405 start = offsets[area];
2406 end = start + sizes[area];
2407
2408 /* is everything aligned properly? */
2409 BUG_ON(!IS_ALIGNED(offsets[area], align));
2410 BUG_ON(!IS_ALIGNED(sizes[area], align));
2411
2412 /* detect the area with the highest address */
2413 if (start > offsets[last_area])
2414 last_area = area;
2415
2416 for (area2 = 0; area2 < nr_vms; area2++) {
2417 unsigned long start2 = offsets[area2];
2418 unsigned long end2 = start2 + sizes[area2];
2419
2420 if (area2 == area)
2421 continue;
2422
2423 BUG_ON(start2 >= start && start2 < end);
2424 BUG_ON(end2 <= end && end2 > start);
2425 }
2426 }
2427 last_end = offsets[last_area] + sizes[last_area];
2428
2429 if (vmalloc_end - vmalloc_start < last_end) {
2430 WARN_ON(true);
2431 return NULL;
2432 }
2433
4d67d860
TM
2434 vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
2435 vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
ca23e405 2436 if (!vas || !vms)
f1db7afd 2437 goto err_free2;
ca23e405
TH
2438
2439 for (area = 0; area < nr_vms; area++) {
ec3f64fc
DR
2440 vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL);
2441 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
ca23e405
TH
2442 if (!vas[area] || !vms[area])
2443 goto err_free;
2444 }
2445retry:
2446 spin_lock(&vmap_area_lock);
2447
2448 /* start scanning - we scan from the top, begin with the last area */
2449 area = term_area = last_area;
2450 start = offsets[area];
2451 end = start + sizes[area];
2452
2453 if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2454 base = vmalloc_end - last_end;
2455 goto found;
2456 }
2457 base = pvm_determine_end(&next, &prev, align) - end;
2458
2459 while (true) {
2460 BUG_ON(next && next->va_end <= base + end);
2461 BUG_ON(prev && prev->va_end > base + end);
2462
2463 /*
2464 * base might have underflowed, add last_end before
2465 * comparing.
2466 */
2467 if (base + last_end < vmalloc_start + last_end) {
2468 spin_unlock(&vmap_area_lock);
2469 if (!purged) {
2470 purge_vmap_area_lazy();
2471 purged = true;
2472 goto retry;
2473 }
2474 goto err_free;
2475 }
2476
2477 /*
2478 * If next overlaps, move base downwards so that it's
2479 * right below next and then recheck.
2480 */
2481 if (next && next->va_start < base + end) {
2482 base = pvm_determine_end(&next, &prev, align) - end;
2483 term_area = area;
2484 continue;
2485 }
2486
2487 /*
2488 * If prev overlaps, shift down next and prev and move
2489 * base so that it's right below new next and then
2490 * recheck.
2491 */
2492 if (prev && prev->va_end > base + start) {
2493 next = prev;
2494 prev = node_to_va(rb_prev(&next->rb_node));
2495 base = pvm_determine_end(&next, &prev, align) - end;
2496 term_area = area;
2497 continue;
2498 }
2499
2500 /*
2501 * This area fits, move on to the previous one. If
2502 * the previous one is the terminal one, we're done.
2503 */
2504 area = (area + nr_vms - 1) % nr_vms;
2505 if (area == term_area)
2506 break;
2507 start = offsets[area];
2508 end = start + sizes[area];
2509 pvm_find_next_prev(base + end, &next, &prev);
2510 }
2511found:
2512 /* we've found a fitting base, insert all va's */
2513 for (area = 0; area < nr_vms; area++) {
2514 struct vmap_area *va = vas[area];
2515
2516 va->va_start = base + offsets[area];
2517 va->va_end = va->va_start + sizes[area];
2518 __insert_vmap_area(va);
2519 }
2520
2521 vmap_area_pcpu_hole = base + offsets[last_area];
2522
2523 spin_unlock(&vmap_area_lock);
2524
2525 /* insert all vm's */
2526 for (area = 0; area < nr_vms; area++)
2527 insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2528 pcpu_get_vm_areas);
2529
2530 kfree(vas);
2531 return vms;
2532
2533err_free:
2534 for (area = 0; area < nr_vms; area++) {
f1db7afd
KC
2535 kfree(vas[area]);
2536 kfree(vms[area]);
ca23e405 2537 }
f1db7afd 2538err_free2:
ca23e405
TH
2539 kfree(vas);
2540 kfree(vms);
2541 return NULL;
2542}
2543
2544/**
2545 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2546 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2547 * @nr_vms: the number of allocated areas
2548 *
2549 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2550 */
2551void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2552{
2553 int i;
2554
2555 for (i = 0; i < nr_vms; i++)
2556 free_vm_area(vms[i]);
2557 kfree(vms);
2558}
4f8b02b4 2559#endif /* CONFIG_SMP */
a10aa579
CL
2560
2561#ifdef CONFIG_PROC_FS
2562static void *s_start(struct seq_file *m, loff_t *pos)
d4033afd 2563 __acquires(&vmap_area_lock)
a10aa579
CL
2564{
2565 loff_t n = *pos;
d4033afd 2566 struct vmap_area *va;
a10aa579 2567
d4033afd
JK
2568 spin_lock(&vmap_area_lock);
2569 va = list_entry((&vmap_area_list)->next, typeof(*va), list);
2570 while (n > 0 && &va->list != &vmap_area_list) {
a10aa579 2571 n--;
d4033afd 2572 va = list_entry(va->list.next, typeof(*va), list);
a10aa579 2573 }
d4033afd
JK
2574 if (!n && &va->list != &vmap_area_list)
2575 return va;
a10aa579
CL
2576
2577 return NULL;
2578
2579}
2580
2581static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2582{
d4033afd 2583 struct vmap_area *va = p, *next;
a10aa579
CL
2584
2585 ++*pos;
d4033afd
JK
2586 next = list_entry(va->list.next, typeof(*va), list);
2587 if (&next->list != &vmap_area_list)
2588 return next;
2589
2590 return NULL;
a10aa579
CL
2591}
2592
2593static void s_stop(struct seq_file *m, void *p)
d4033afd 2594 __releases(&vmap_area_lock)
a10aa579 2595{
d4033afd 2596 spin_unlock(&vmap_area_lock);
a10aa579
CL
2597}
2598
a47a126a
ED
2599static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2600{
e5adfffc 2601 if (IS_ENABLED(CONFIG_NUMA)) {
a47a126a
ED
2602 unsigned int nr, *counters = m->private;
2603
2604 if (!counters)
2605 return;
2606
4341fa45 2607 /* Pair with smp_wmb() in clear_vm_unlist() */
d4033afd
JK
2608 smp_rmb();
2609 if (v->flags & VM_UNLIST)
2610 return;
2611
a47a126a
ED
2612 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2613
2614 for (nr = 0; nr < v->nr_pages; nr++)
2615 counters[page_to_nid(v->pages[nr])]++;
2616
2617 for_each_node_state(nr, N_HIGH_MEMORY)
2618 if (counters[nr])
2619 seq_printf(m, " N%u=%u", nr, counters[nr]);
2620 }
2621}
2622
a10aa579
CL
2623static int s_show(struct seq_file *m, void *p)
2624{
d4033afd
JK
2625 struct vmap_area *va = p;
2626 struct vm_struct *v;
2627
2628 if (va->flags & (VM_LAZY_FREE | VM_LAZY_FREEING))
2629 return 0;
2630
2631 if (!(va->flags & VM_VM_AREA)) {
2632 seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n",
2633 (void *)va->va_start, (void *)va->va_end,
2634 va->va_end - va->va_start);
2635 return 0;
2636 }
2637
2638 v = va->vm;
a10aa579 2639
45ec1690 2640 seq_printf(m, "0x%pK-0x%pK %7ld",
a10aa579
CL
2641 v->addr, v->addr + v->size, v->size);
2642
62c70bce
JP
2643 if (v->caller)
2644 seq_printf(m, " %pS", v->caller);
23016969 2645
a10aa579
CL
2646 if (v->nr_pages)
2647 seq_printf(m, " pages=%d", v->nr_pages);
2648
2649 if (v->phys_addr)
ffa71f33 2650 seq_printf(m, " phys=%llx", (unsigned long long)v->phys_addr);
a10aa579
CL
2651
2652 if (v->flags & VM_IOREMAP)
2653 seq_printf(m, " ioremap");
2654
2655 if (v->flags & VM_ALLOC)
2656 seq_printf(m, " vmalloc");
2657
2658 if (v->flags & VM_MAP)
2659 seq_printf(m, " vmap");
2660
2661 if (v->flags & VM_USERMAP)
2662 seq_printf(m, " user");
2663
2664 if (v->flags & VM_VPAGES)
2665 seq_printf(m, " vpages");
2666
a47a126a 2667 show_numa_info(m, v);
a10aa579
CL
2668 seq_putc(m, '\n');
2669 return 0;
2670}
2671
5f6a6a9c 2672static const struct seq_operations vmalloc_op = {
a10aa579
CL
2673 .start = s_start,
2674 .next = s_next,
2675 .stop = s_stop,
2676 .show = s_show,
2677};
5f6a6a9c
AD
2678
2679static int vmalloc_open(struct inode *inode, struct file *file)
2680{
2681 unsigned int *ptr = NULL;
2682 int ret;
2683
e5adfffc 2684 if (IS_ENABLED(CONFIG_NUMA)) {
5f6a6a9c 2685 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
51980ac9
KV
2686 if (ptr == NULL)
2687 return -ENOMEM;
2688 }
5f6a6a9c
AD
2689 ret = seq_open(file, &vmalloc_op);
2690 if (!ret) {
2691 struct seq_file *m = file->private_data;
2692 m->private = ptr;
2693 } else
2694 kfree(ptr);
2695 return ret;
2696}
2697
2698static const struct file_operations proc_vmalloc_operations = {
2699 .open = vmalloc_open,
2700 .read = seq_read,
2701 .llseek = seq_lseek,
2702 .release = seq_release_private,
2703};
2704
2705static int __init proc_vmalloc_init(void)
2706{
2707 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
2708 return 0;
2709}
2710module_init(proc_vmalloc_init);
db3808c1
JK
2711
2712void get_vmalloc_info(struct vmalloc_info *vmi)
2713{
f98782dd 2714 struct vmap_area *va;
db3808c1
JK
2715 unsigned long free_area_size;
2716 unsigned long prev_end;
2717
2718 vmi->used = 0;
f98782dd 2719 vmi->largest_chunk = 0;
db3808c1 2720
f98782dd 2721 prev_end = VMALLOC_START;
db3808c1 2722
f98782dd 2723 spin_lock(&vmap_area_lock);
db3808c1 2724
f98782dd
JK
2725 if (list_empty(&vmap_area_list)) {
2726 vmi->largest_chunk = VMALLOC_TOTAL;
2727 goto out;
2728 }
db3808c1 2729
f98782dd
JK
2730 list_for_each_entry(va, &vmap_area_list, list) {
2731 unsigned long addr = va->va_start;
db3808c1 2732
f98782dd
JK
2733 /*
2734 * Some archs keep another range for modules in vmalloc space
2735 */
2736 if (addr < VMALLOC_START)
2737 continue;
2738 if (addr >= VMALLOC_END)
2739 break;
db3808c1 2740
f98782dd
JK
2741 if (va->flags & (VM_LAZY_FREE | VM_LAZY_FREEING))
2742 continue;
db3808c1 2743
f98782dd 2744 vmi->used += (va->va_end - va->va_start);
db3808c1 2745
f98782dd
JK
2746 free_area_size = addr - prev_end;
2747 if (vmi->largest_chunk < free_area_size)
2748 vmi->largest_chunk = free_area_size;
db3808c1 2749
f98782dd 2750 prev_end = va->va_end;
db3808c1 2751 }
f98782dd
JK
2752
2753 if (VMALLOC_END - prev_end > vmi->largest_chunk)
2754 vmi->largest_chunk = VMALLOC_END - prev_end;
2755
2756out:
2757 spin_unlock(&vmap_area_lock);
db3808c1 2758}
a10aa579
CL
2759#endif
2760