Merge tag 'v3.10.68' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / mm / vmalloc.c
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/sched.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/interrupt.h>
19 #include <linux/proc_fs.h>
20 #include <linux/seq_file.h>
21 #include <linux/debugobjects.h>
22 #include <linux/kallsyms.h>
23 #include <linux/list.h>
24 #include <linux/rbtree.h>
25 #include <linux/radix-tree.h>
26 #include <linux/rcupdate.h>
27 #include <linux/pfn.h>
28 #include <linux/kmemleak.h>
29 #include <linux/atomic.h>
30 #include <linux/llist.h>
31 #include <asm/uaccess.h>
32 #include <asm/tlbflush.h>
33 #include <asm/shmparam.h>
34
35 struct vfree_deferred {
36 struct llist_head list;
37 struct work_struct wq;
38 };
39 static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
40
41 static void __vunmap(const void *, int);
42
43 static 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
54 /*** Page table manipulation functions ***/
55
56 static 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
67 static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
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
81 static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
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
95 static void vunmap_page_range(unsigned long addr, unsigned long end)
96 {
97 pgd_t *pgd;
98 unsigned long next;
99
100 BUG_ON(addr >= end);
101 pgd = pgd_offset_k(addr);
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);
108 }
109
110 static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
111 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
112 {
113 pte_t *pte;
114
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
120 pte = pte_alloc_kernel(pmd, addr);
121 if (!pte)
122 return -ENOMEM;
123 do {
124 struct page *page = pages[*nr];
125
126 if (WARN_ON(!pte_none(*pte)))
127 return -EBUSY;
128 if (WARN_ON(!page))
129 return -ENOMEM;
130 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
131 (*nr)++;
132 } while (pte++, addr += PAGE_SIZE, addr != end);
133 return 0;
134 }
135
136 static int vmap_pmd_range(pud_t *pud, unsigned long addr,
137 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
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);
147 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
148 return -ENOMEM;
149 } while (pmd++, addr = next, addr != end);
150 return 0;
151 }
152
153 static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
154 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
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);
164 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
165 return -ENOMEM;
166 } while (pud++, addr = next, addr != end);
167 return 0;
168 }
169
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 */
176 static int vmap_page_range_noflush(unsigned long start, unsigned long end,
177 pgprot_t prot, struct page **pages)
178 {
179 pgd_t *pgd;
180 unsigned long next;
181 unsigned long addr = start;
182 int err = 0;
183 int nr = 0;
184
185 BUG_ON(addr >= end);
186 pgd = pgd_offset_k(addr);
187 do {
188 next = pgd_addr_end(addr, end);
189 err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
190 if (err)
191 return err;
192 } while (pgd++, addr = next, addr != end);
193
194 return nr;
195 }
196
197 static 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
207 int is_vmalloc_or_module_addr(const void *x)
208 {
209 /*
210 * ARM, x86-64 and sparc64 put modules in a special place,
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
222 /*
223 * Walk a vmap address to the struct page it maps.
224 */
225 struct page *vmalloc_to_page(const void *vmalloc_addr)
226 {
227 unsigned long addr = (unsigned long) vmalloc_addr;
228 struct page *page = NULL;
229 pgd_t *pgd = pgd_offset_k(addr);
230
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 */
235 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
236
237 if (!pgd_none(*pgd)) {
238 pud_t *pud = pud_offset(pgd, addr);
239 if (!pud_none(*pud)) {
240 pmd_t *pmd = pmd_offset(pud, addr);
241 if (!pmd_none(*pmd)) {
242 pte_t *ptep, pte;
243
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 }
254 EXPORT_SYMBOL(vmalloc_to_page);
255
256 /*
257 * Map a vmalloc()-space virtual address to the physical page frame number.
258 */
259 unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
260 {
261 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
262 }
263 EXPORT_SYMBOL(vmalloc_to_pfn);
264
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
272 static DEFINE_SPINLOCK(vmap_area_lock);
273 /* Export for kexec only */
274 LIST_HEAD(vmap_area_list);
275 static struct rb_root vmap_area_root = RB_ROOT;
276
277 /* The vmap cache globals are protected by vmap_area_lock */
278 static struct rb_node *free_vmap_cache;
279 static unsigned long cached_hole_size;
280 static unsigned long cached_vstart;
281 static unsigned long cached_align;
282
283 static unsigned long vmap_area_pcpu_hole;
284
285 static struct vmap_area *__find_vmap_area(unsigned long addr)
286 {
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
304 static 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) {
311 struct vmap_area *tmp_va;
312
313 parent = *p;
314 tmp_va = rb_entry(parent, struct vmap_area, rb_node);
315 if (va->va_start < tmp_va->va_end)
316 p = &(*p)->rb_left;
317 else if (va->va_end > tmp_va->va_start)
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
326 /* address-sort this list */
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
336 static 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 */
342 static 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;
349 unsigned long addr;
350 int purged = 0;
351 struct vmap_area *first;
352
353 BUG_ON(!size);
354 BUG_ON(size & ~PAGE_MASK);
355 BUG_ON(!is_power_of_2(align));
356
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
362 retry:
363 spin_lock(&vmap_area_lock);
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) {
377 nocache:
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);
388 addr = ALIGN(first->va_end, align);
389 if (addr < vstart)
390 goto nocache;
391 if (addr + size < addr)
392 goto overflow;
393
394 } else {
395 addr = ALIGN(vstart, align);
396 if (addr + size < addr)
397 goto overflow;
398
399 n = vmap_area_root.rb_node;
400 first = NULL;
401
402 while (n) {
403 struct vmap_area *tmp;
404 tmp = rb_entry(n, struct vmap_area, rb_node);
405 if (tmp->va_end >= addr) {
406 first = tmp;
407 if (tmp->va_start <= addr)
408 break;
409 n = n->rb_left;
410 } else
411 n = n->rb_right;
412 }
413
414 if (!first)
415 goto found;
416 }
417
418 /* from the starting point, walk areas until a suitable hole is found */
419 while (addr + size > first->va_start && addr + size <= vend) {
420 if (addr + cached_hole_size < first->va_start)
421 cached_hole_size = first->va_start - addr;
422 addr = ALIGN(first->va_end, align);
423 if (addr + size < addr)
424 goto overflow;
425
426 /*
427 if (list_is_last(&first->list, &vmap_area_list))
428 goto found;
429
430 first = list_entry(first->list.next,
431 struct vmap_area, list);
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;
438 }
439
440 found:
441 if (addr + size > vend)
442 goto overflow;
443
444 va->va_start = addr;
445 va->va_end = addr + size;
446 va->flags = 0;
447 __insert_vmap_area(va);
448 free_vmap_cache = &va->rb_node;
449 spin_unlock(&vmap_area_lock);
450
451 BUG_ON(va->va_start & (align-1));
452 BUG_ON(va->va_start < vstart);
453 BUG_ON(va->va_end > vend);
454
455 return va;
456
457 overflow:
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);
470 }
471
472 static void __free_vmap_area(struct vmap_area *va)
473 {
474 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
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 }
491 rb_erase(&va->rb_node, &vmap_area_root);
492 RB_CLEAR_NODE(&va->rb_node);
493 list_del_rcu(&va->list);
494
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
504 kfree_rcu(va, rcu_head);
505 }
506
507 /*
508 * Free a region of KVA allocated by alloc_vmap_area
509 */
510 static 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 */
520 static void unmap_vmap_area(struct vmap_area *va)
521 {
522 vunmap_page_range(va->va_start, va->va_end);
523 }
524
525 static 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
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 */
562 static 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
571 static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
572
573 /* for per-CPU blocks */
574 static void purge_fragmented_blocks_allcpus(void);
575
576 /*
577 * called before a call to iounmap() if the caller wants vm_area_struct's
578 * immediately freed.
579 */
580 void set_iounmap_nonlazy(void)
581 {
582 atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
583 }
584
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 */
595 static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
596 int sync, int force_flush)
597 {
598 static DEFINE_SPINLOCK(purge_lock);
599 LIST_HEAD(valist);
600 struct vmap_area *va;
601 struct vmap_area *n_va;
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) {
610 if (!spin_trylock(&purge_lock))
611 return;
612 } else
613 spin_lock(&purge_lock);
614
615 if (sync)
616 purge_fragmented_blocks_allcpus();
617
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;
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
633 if (nr)
634 atomic_sub(nr, &vmap_lazy_nr);
635
636 if (nr || force_flush)
637 flush_tlb_kernel_range(*start, *end);
638
639 if (nr) {
640 spin_lock(&vmap_area_lock);
641 list_for_each_entry_safe(va, n_va, &valist, purge_list)
642 __free_vmap_area(va);
643 spin_unlock(&vmap_area_lock);
644 }
645 spin_unlock(&purge_lock);
646 }
647
648 /*
649 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
650 * is already purging.
651 */
652 static 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
659 /*
660 * Kick off a purge of the outstanding lazy areas.
661 */
662 static void purge_vmap_area_lazy(void)
663 {
664 unsigned long start = ULONG_MAX, end = 0;
665
666 __purge_vmap_area_lazy(&start, &end, 1, 0);
667 }
668
669 /*
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.
673 */
674 static void free_vmap_area_noflush(struct vmap_area *va)
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()))
679 try_purge_vmap_area_lazy();
680 }
681
682 /*
683 * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
684 * called for the correct range previously.
685 */
686 static void free_unmap_vmap_area_noflush(struct vmap_area *va)
687 {
688 unmap_vmap_area(va);
689 free_vmap_area_noflush(va);
690 }
691
692 /*
693 * Free and unmap a vmap area
694 */
695 static 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
701 static 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
712 static 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() */
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))
749
750 #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
751
752 static bool vmap_initialized __read_mostly = false;
753
754 struct vmap_block_queue {
755 spinlock_t lock;
756 struct list_head free;
757 };
758
759 struct 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);
766 struct list_head free_list;
767 struct rcu_head rcu_head;
768 struct list_head purge;
769 };
770
771 /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
772 static 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 */
779 static DEFINE_SPINLOCK(vmap_block_tree_lock);
780 static 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
789 static 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
796 static 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);
814 if (IS_ERR(va)) {
815 kfree(vb);
816 return ERR_CAST(va);
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);
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);
844 list_add_rcu(&vb->free_list, &vbq->free);
845 spin_unlock(&vbq->lock);
846 put_cpu_var(vmap_block_queue);
847
848 return vb;
849 }
850
851 static void free_vmap_block(struct vmap_block *vb)
852 {
853 struct vmap_block *tmp;
854 unsigned long vb_idx;
855
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
862 free_vmap_area_noflush(vb->va);
863 kfree_rcu(vb, rcu_head);
864 }
865
866 static 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
901 static void purge_fragmented_blocks_thiscpu(void)
902 {
903 purge_fragmented_blocks(smp_processor_id());
904 }
905
906 static void purge_fragmented_blocks_allcpus(void)
907 {
908 int cpu;
909
910 for_each_possible_cpu(cpu)
911 purge_fragmented_blocks(cpu);
912 }
913
914 static 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;
920 int purge = 0;
921
922 BUG_ON(size & ~PAGE_MASK);
923 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
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 }
932 order = get_order(size);
933
934 again:
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);
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 }
946 goto next;
947 }
948
949 i = bitmap_find_free_region(vb->alloc_map,
950 VMAP_BBMAP_BITS, order);
951
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;
957 }
958 goto next;
959 }
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;
971 next:
972 spin_unlock(&vb->lock);
973 }
974
975 if (purge)
976 purge_fragmented_blocks_thiscpu();
977
978 put_cpu_var(vmap_block_queue);
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
991 static 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);
1000
1001 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
1002
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
1013 vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
1014
1015 spin_lock(&vb->lock);
1016 BUG_ON(bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order));
1017
1018 vb->dirty += 1UL << order;
1019 if (vb->dirty == VMAP_BBMAP_BITS) {
1020 BUG_ON(vb->free);
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 */
1040 void vm_unmap_aliases(void)
1041 {
1042 unsigned long start = ULONG_MAX, end = 0;
1043 int cpu;
1044 int flush = 0;
1045
1046 if (unlikely(!vmap_initialized))
1047 return;
1048
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);
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 }
1085 EXPORT_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 */
1092 void 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);
1103 vmap_debug_free_range(addr, addr+size);
1104
1105 if (likely(count <= VMAP_MAX_ALLOC))
1106 vb_free(mem, size);
1107 else
1108 free_unmap_vmap_area_addr(addr);
1109 }
1110 EXPORT_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
1118 *
1119 * Returns: a pointer to the address that has been mapped, or %NULL on failure
1120 */
1121 void *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 }
1148 EXPORT_SYMBOL(vm_map_ram);
1149
1150 static struct vm_struct *vmlist __initdata;
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 */
1161 void __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
1177 /**
1178 * vm_area_register_early - register vmap area early during boot
1179 * @vm: vm_struct to register
1180 * @align: requested alignment
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 */
1189 void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1190 {
1191 static size_t vm_init_off __initdata;
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;
1196
1197 vm->addr = (void *)addr;
1198
1199 vm_area_add_early(vm);
1200 }
1201
1202 void __init vmalloc_init(void)
1203 {
1204 struct vmap_area *va;
1205 struct vm_struct *tmp;
1206 int i;
1207
1208 for_each_possible_cpu(i) {
1209 struct vmap_block_queue *vbq;
1210 struct vfree_deferred *p;
1211
1212 vbq = &per_cpu(vmap_block_queue, i);
1213 spin_lock_init(&vbq->lock);
1214 INIT_LIST_HEAD(&vbq->free);
1215 p = &per_cpu(vfree_deferred, i);
1216 init_llist_head(&p->list);
1217 INIT_WORK(&p->wq, free_work);
1218 }
1219
1220 /* Import existing vmlist entries. */
1221 for (tmp = vmlist; tmp; tmp = tmp->next) {
1222 va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
1223 va->flags = VM_VM_AREA;
1224 va->va_start = (unsigned long)tmp->addr;
1225 va->va_end = va->va_start + tmp->size;
1226 va->vm = tmp;
1227 __insert_vmap_area(va);
1228 }
1229
1230 vmap_area_pcpu_hole = VMALLOC_END;
1231
1232 vmap_initialized = true;
1233 }
1234
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 */
1254 int 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 */
1274 void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1275 {
1276 vunmap_page_range(addr, addr + size);
1277 }
1278 EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
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 */
1288 void unmap_kernel_range(unsigned long addr, unsigned long size)
1289 {
1290 unsigned long end = addr + size;
1291
1292 flush_cache_vunmap(addr, end);
1293 vunmap_page_range(addr, end);
1294 flush_tlb_kernel_range(addr, end);
1295 }
1296
1297 int 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 }
1311 EXPORT_SYMBOL_GPL(map_vm_area);
1312
1313 static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
1314 unsigned long flags, const void *caller)
1315 {
1316 spin_lock(&vmap_area_lock);
1317 vm->flags = flags;
1318 vm->addr = (void *)va->va_start;
1319 vm->size = va->va_end - va->va_start;
1320 vm->caller = caller;
1321 va->vm = vm;
1322 va->flags |= VM_VM_AREA;
1323 spin_unlock(&vmap_area_lock);
1324 }
1325
1326 static void clear_vm_unlist(struct vm_struct *vm)
1327 {
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();
1334 vm->flags &= ~VM_UNLIST;
1335 }
1336
1337 static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
1338 unsigned long flags, const void *caller)
1339 {
1340 setup_vmalloc_vm(vm, va, flags, caller);
1341 clear_vm_unlist(vm);
1342 }
1343
1344 static struct vm_struct *__get_vm_area_node(unsigned long size,
1345 unsigned long align, unsigned long flags, unsigned long start,
1346 unsigned long end, int node, gfp_t gfp_mask, const void *caller)
1347 {
1348 struct vmap_area *va;
1349 struct vm_struct *area;
1350
1351 BUG_ON(in_interrupt());
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 }
1362
1363 size = PAGE_ALIGN(size);
1364 if (unlikely(!size))
1365 return NULL;
1366
1367 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
1368 if (unlikely(!area))
1369 return NULL;
1370
1371 /*
1372 * We always allocate a guard page.
1373 */
1374 size += PAGE_SIZE;
1375
1376 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1377 if (IS_ERR(va)) {
1378 kfree(area);
1379 return NULL;
1380 }
1381
1382 /*
1383 * When this function is called from __vmalloc_node_range,
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.
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
1393 return area;
1394 }
1395
1396 struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1397 unsigned long start, unsigned long end)
1398 {
1399 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
1400 GFP_KERNEL, __builtin_return_address(0));
1401 }
1402 EXPORT_SYMBOL_GPL(__get_vm_area);
1403
1404 struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1405 unsigned long start, unsigned long end,
1406 const void *caller)
1407 {
1408 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
1409 GFP_KERNEL, caller);
1410 }
1411
1412 /**
1413 * get_vm_area - reserve a contiguous kernel virtual area
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 */
1421 struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
1422 {
1423 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
1424 NUMA_NO_NODE, GFP_KERNEL,
1425 __builtin_return_address(0));
1426 }
1427
1428 struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
1429 const void *caller)
1430 {
1431 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
1432 NUMA_NO_NODE, GFP_KERNEL, caller);
1433 }
1434
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 */
1443 struct vm_struct *find_vm_area(const void *addr)
1444 {
1445 struct vmap_area *va;
1446
1447 va = find_vmap_area((unsigned long)addr);
1448 if (va && va->flags & VM_VM_AREA)
1449 return va->vm;
1450
1451 return NULL;
1452 }
1453
1454 /**
1455 * remove_vm_area - find and remove a continuous kernel virtual area
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 */
1462 struct vm_struct *remove_vm_area(const void *addr)
1463 {
1464 struct vmap_area *va;
1465
1466 va = find_vmap_area((unsigned long)addr);
1467 if (va && va->flags & VM_VM_AREA) {
1468 struct vm_struct *vm = va->vm;
1469
1470 spin_lock(&vmap_area_lock);
1471 va->vm = NULL;
1472 va->flags &= ~VM_VM_AREA;
1473 spin_unlock(&vmap_area_lock);
1474
1475 vmap_debug_free_range(va->va_start, va->va_end);
1476 free_unmap_vmap_area(va);
1477 vm->size -= PAGE_SIZE;
1478
1479 return vm;
1480 }
1481 return NULL;
1482 }
1483
1484 static void __vunmap(const void *addr, int deallocate_pages)
1485 {
1486 struct vm_struct *area;
1487
1488 if (!addr)
1489 return;
1490
1491 if ((PAGE_SIZE-1) & (unsigned long)addr) {
1492 WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
1493 return;
1494 }
1495
1496 area = remove_vm_area(addr);
1497 if (unlikely(!area)) {
1498 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
1499 addr);
1500 return;
1501 }
1502
1503 debug_check_no_locks_freed(addr, area->size);
1504 debug_check_no_obj_freed(addr, area->size);
1505
1506 if (deallocate_pages) {
1507 int i;
1508
1509 for (i = 0; i < area->nr_pages; i++) {
1510 struct page *page = area->pages[i];
1511
1512 BUG_ON(!page);
1513 __free_page(page);
1514 }
1515
1516 if (area->flags & VM_VPAGES)
1517 vfree(area->pages);
1518 else
1519 kfree(area->pages);
1520 }
1521
1522 kfree(area);
1523 return;
1524 }
1525
1526 /**
1527 * vfree - release memory allocated by vmalloc()
1528 * @addr: memory base address
1529 *
1530 * Free the virtually continuous memory area starting at @addr, as
1531 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1532 * NULL, no operation is performed.
1533 *
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)
1537 *
1538 * NOTE: assumes that the object at *addr has a size >= sizeof(llist_node)
1539 *
1540 */
1541 void vfree(const void *addr)
1542 {
1543 BUG_ON(in_nmi());
1544
1545 kmemleak_free(addr);
1546
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);
1555 }
1556 EXPORT_SYMBOL(vfree);
1557
1558 /**
1559 * vunmap - release virtual mapping obtained by vmap()
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 *
1565 * Must not be called in interrupt context.
1566 */
1567 void vunmap(const void *addr)
1568 {
1569 BUG_ON(in_interrupt());
1570 might_sleep();
1571 if (addr)
1572 __vunmap(addr, 0);
1573 }
1574 EXPORT_SYMBOL(vunmap);
1575
1576 /**
1577 * vmap - map an array of pages into virtually contiguous space
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 */
1586 void *vmap(struct page **pages, unsigned int count,
1587 unsigned long flags, pgprot_t prot)
1588 {
1589 struct vm_struct *area;
1590
1591 might_sleep();
1592
1593 if (count > totalram_pages)
1594 return NULL;
1595
1596 area = get_vm_area_caller((count << PAGE_SHIFT), flags,
1597 __builtin_return_address(0));
1598 if (!area)
1599 return NULL;
1600
1601 if (map_vm_area(area, prot, &pages)) {
1602 vunmap(area->addr);
1603 return NULL;
1604 }
1605
1606 return area->addr;
1607 }
1608 EXPORT_SYMBOL(vmap);
1609
1610 static void *__vmalloc_node(unsigned long size, unsigned long align,
1611 gfp_t gfp_mask, pgprot_t prot,
1612 int node, const void *caller);
1613 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
1614 pgprot_t prot, int node, const void *caller)
1615 {
1616 const int order = 0;
1617 struct page **pages;
1618 unsigned int nr_pages, array_size, i;
1619 gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
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. */
1626 if (array_size > PAGE_SIZE) {
1627 pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,
1628 PAGE_KERNEL, node, caller);
1629 area->flags |= VM_VPAGES;
1630 } else {
1631 pages = kmalloc_node(array_size, nested_gfp, node);
1632 }
1633 area->pages = pages;
1634 area->caller = caller;
1635 if (!area->pages) {
1636 remove_vm_area(area->addr);
1637 kfree(area);
1638 return NULL;
1639 }
1640
1641 for (i = 0; i < area->nr_pages; i++) {
1642 struct page *page;
1643 gfp_t tmp_mask = gfp_mask | __GFP_NOWARN;
1644
1645 if (node < 0)
1646 page = alloc_page(tmp_mask);
1647 else
1648 page = alloc_pages_node(node, tmp_mask, order);
1649
1650 if (unlikely(!page)) {
1651 /* Successfully allocated i pages, free them in __vunmap() */
1652 area->nr_pages = i;
1653 goto fail;
1654 }
1655 area->pages[i] = page;
1656 }
1657
1658 if (map_vm_area(area, prot, &pages))
1659 goto fail;
1660 return area->addr;
1661
1662 fail:
1663 warn_alloc_failed(gfp_mask, order,
1664 "vmalloc: allocation failure, allocated %ld of %ld bytes\n",
1665 (area->nr_pages*PAGE_SIZE), area->size);
1666 vfree(area->addr);
1667 return NULL;
1668 }
1669
1670 /**
1671 * __vmalloc_node_range - allocate virtually contiguous memory
1672 * @size: allocation size
1673 * @align: desired alignment
1674 * @start: vm area range start
1675 * @end: vm area range end
1676 * @gfp_mask: flags for the page level allocator
1677 * @prot: protection mask for the allocated pages
1678 * @node: node to use for allocation or NUMA_NO_NODE
1679 * @caller: caller's return address
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 */
1685 void *__vmalloc_node_range(unsigned long size, unsigned long align,
1686 unsigned long start, unsigned long end, gfp_t gfp_mask,
1687 pgprot_t prot, int node, const void *caller)
1688 {
1689 struct vm_struct *area;
1690 void *addr;
1691 unsigned long real_size = size;
1692
1693 size = PAGE_ALIGN(size);
1694 if (!size || (size >> PAGE_SHIFT) > totalram_pages)
1695 goto fail;
1696
1697 area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNLIST,
1698 start, end, node, gfp_mask, caller);
1699 if (!area)
1700 goto fail;
1701
1702 addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
1703 if (!addr)
1704 return NULL;
1705
1706 /*
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.
1710 */
1711 clear_vm_unlist(area);
1712
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;
1721
1722 fail:
1723 warn_alloc_failed(gfp_mask, 0,
1724 "vmalloc: allocation failure: %lu bytes\n",
1725 real_size);
1726 return NULL;
1727 }
1728
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
1735 * @node: node to use for allocation or NUMA_NO_NODE
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 */
1742 static void *__vmalloc_node(unsigned long size, unsigned long align,
1743 gfp_t gfp_mask, pgprot_t prot,
1744 int node, const void *caller)
1745 {
1746 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
1747 gfp_mask, prot, node, caller);
1748 }
1749
1750 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1751 {
1752 return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE,
1753 __builtin_return_address(0));
1754 }
1755 EXPORT_SYMBOL(__vmalloc);
1756
1757 static 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
1764 /**
1765 * vmalloc - allocate virtually contiguous memory
1766 * @size: allocation size
1767 * Allocate enough pages to cover @size from the page level
1768 * allocator and map them into contiguous kernel virtual space.
1769 *
1770 * For tight control over page level allocator and protection flags
1771 * use __vmalloc() instead.
1772 */
1773 void *vmalloc(unsigned long size)
1774 {
1775 return __vmalloc_node_flags(size, NUMA_NO_NODE,
1776 GFP_KERNEL | __GFP_HIGHMEM);
1777 }
1778 EXPORT_SYMBOL(vmalloc);
1779
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 */
1790 void *vzalloc(unsigned long size)
1791 {
1792 return __vmalloc_node_flags(size, NUMA_NO_NODE,
1793 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1794 }
1795 EXPORT_SYMBOL(vzalloc);
1796
1797 /**
1798 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1799 * @size: allocation size
1800 *
1801 * The resulting memory area is zeroed so it can be mapped to userspace
1802 * without leaking data.
1803 */
1804 void *vmalloc_user(unsigned long size)
1805 {
1806 struct vm_struct *area;
1807 void *ret;
1808
1809 ret = __vmalloc_node(size, SHMLBA,
1810 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
1811 PAGE_KERNEL, NUMA_NO_NODE,
1812 __builtin_return_address(0));
1813 if (ret) {
1814 area = find_vm_area(ret);
1815 area->flags |= VM_USERMAP;
1816 }
1817 return ret;
1818 }
1819 EXPORT_SYMBOL(vmalloc_user);
1820
1821 /**
1822 * vmalloc_node - allocate memory on a specific node
1823 * @size: allocation size
1824 * @node: numa node
1825 *
1826 * Allocate enough pages to cover @size from the page level
1827 * allocator and map them into contiguous kernel virtual space.
1828 *
1829 * For tight control over page level allocator and protection flags
1830 * use __vmalloc() instead.
1831 */
1832 void *vmalloc_node(unsigned long size, int node)
1833 {
1834 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1835 node, __builtin_return_address(0));
1836 }
1837 EXPORT_SYMBOL(vmalloc_node);
1838
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 */
1851 void *vzalloc_node(unsigned long size, int node)
1852 {
1853 return __vmalloc_node_flags(size, node,
1854 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1855 }
1856 EXPORT_SYMBOL(vzalloc_node);
1857
1858 #ifndef PAGE_KERNEL_EXEC
1859 # define PAGE_KERNEL_EXEC PAGE_KERNEL
1860 #endif
1861
1862 /**
1863 * vmalloc_exec - allocate virtually contiguous, executable memory
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 *
1870 * For tight control over page level allocator and protection flags
1871 * use __vmalloc() instead.
1872 */
1873
1874 void *vmalloc_exec(unsigned long size)
1875 {
1876 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
1877 NUMA_NO_NODE, __builtin_return_address(0));
1878 }
1879
1880 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
1881 #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
1882 #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
1883 #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
1884 #else
1885 #define GFP_VMALLOC32 GFP_KERNEL
1886 #endif
1887
1888 /**
1889 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
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 */
1895 void *vmalloc_32(unsigned long size)
1896 {
1897 return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
1898 NUMA_NO_NODE, __builtin_return_address(0));
1899 }
1900 EXPORT_SYMBOL(vmalloc_32);
1901
1902 /**
1903 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
1904 * @size: allocation size
1905 *
1906 * The resulting memory area is 32bit addressable and zeroed so it can be
1907 * mapped to userspace without leaking data.
1908 */
1909 void *vmalloc_32_user(unsigned long size)
1910 {
1911 struct vm_struct *area;
1912 void *ret;
1913
1914 ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
1915 NUMA_NO_NODE, __builtin_return_address(0));
1916 if (ret) {
1917 area = find_vm_area(ret);
1918 area->flags |= VM_USERMAP;
1919 }
1920 return ret;
1921 }
1922 EXPORT_SYMBOL(vmalloc_32_user);
1923
1924 /*
1925 * small helper routine , copy contents to buf from addr.
1926 * If the page is not present, fill zero.
1927 */
1928
1929 static 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 */
1954 void *map = kmap_atomic(p);
1955 memcpy(buf, map + offset, length);
1956 kunmap_atomic(map);
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
1968 static 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 */
1993 void *map = kmap_atomic(p);
1994 memcpy(map + offset, buf, length);
1995 kunmap_atomic(map);
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
2022 * vm_struct area, returns 0. @buf should be kernel's buffer.
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
2031 long vread(char *buf, char *addr, unsigned long count)
2032 {
2033 struct vmap_area *va;
2034 struct vm_struct *vm;
2035 char *vaddr, *buf_start = buf;
2036 unsigned long buflen = count;
2037 unsigned long n;
2038
2039 /* Don't allow overflow */
2040 if ((unsigned long) addr + count < count)
2041 count = -(unsigned long) addr;
2042
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)
2054 continue;
2055 while (addr < vaddr) {
2056 if (count == 0)
2057 goto finished;
2058 *buf = '\0';
2059 buf++;
2060 addr++;
2061 count--;
2062 }
2063 n = vaddr + vm->size - PAGE_SIZE - addr;
2064 if (n > count)
2065 n = count;
2066 if (!(vm->flags & VM_IOREMAP))
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;
2073 }
2074 finished:
2075 spin_unlock(&vmap_area_lock);
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;
2084 }
2085
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
2104 * vm_struct area, returns 0. @buf should be kernel's buffer.
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.
2110 */
2111
2112 long vwrite(char *buf, char *addr, unsigned long count)
2113 {
2114 struct vmap_area *va;
2115 struct vm_struct *vm;
2116 char *vaddr;
2117 unsigned long n, buflen;
2118 int copied = 0;
2119
2120 /* Don't allow overflow */
2121 if ((unsigned long) addr + count < count)
2122 count = -(unsigned long) addr;
2123 buflen = count;
2124
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)
2136 continue;
2137 while (addr < vaddr) {
2138 if (count == 0)
2139 goto finished;
2140 buf++;
2141 addr++;
2142 count--;
2143 }
2144 n = vaddr + vm->size - PAGE_SIZE - addr;
2145 if (n > count)
2146 n = count;
2147 if (!(vm->flags & VM_IOREMAP)) {
2148 aligned_vwrite(buf, addr, n);
2149 copied++;
2150 }
2151 buf += n;
2152 addr += n;
2153 count -= n;
2154 }
2155 finished:
2156 spin_unlock(&vmap_area_lock);
2157 if (!copied)
2158 return 0;
2159 return buflen;
2160 }
2161
2162 /**
2163 * remap_vmalloc_range - map vmalloc pages to userspace
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
2167 *
2168 * Returns: 0 for success, -Exxx on failure
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 *
2174 * Similar to remap_pfn_range() (see mm/memory.c)
2175 */
2176 int 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;
2182
2183 if ((PAGE_SIZE-1) & (unsigned long)addr)
2184 return -EINVAL;
2185
2186 area = find_vm_area(addr);
2187 if (!area)
2188 return -EINVAL;
2189
2190 if (!(area->flags & VM_USERMAP))
2191 return -EINVAL;
2192
2193 if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
2194 return -EINVAL;
2195
2196 addr += pgoff << PAGE_SHIFT;
2197 do {
2198 struct page *page = vmalloc_to_page(addr);
2199 int ret;
2200
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
2210 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
2211
2212 return 0;
2213 }
2214 EXPORT_SYMBOL(remap_vmalloc_range);
2215
2216 /*
2217 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
2218 * have one.
2219 */
2220 void __attribute__((weak)) vmalloc_sync_all(void)
2221 {
2222 }
2223
2224
2225 static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
2226 {
2227 pte_t ***p = data;
2228
2229 if (p) {
2230 *(*p) = pte;
2231 (*p)++;
2232 }
2233 return 0;
2234 }
2235
2236 /**
2237 * alloc_vm_area - allocate a range of kernel address space
2238 * @size: size of the area
2239 * @ptes: returns the PTEs for the address space
2240 *
2241 * Returns: NULL on failure, vm_struct on success
2242 *
2243 * This function reserves a range of kernel address space, and
2244 * allocates pagetables to map that range. No actual mappings
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.
2249 */
2250 struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
2251 {
2252 struct vm_struct *area;
2253
2254 area = get_vm_area_caller(size, VM_IOREMAP,
2255 __builtin_return_address(0));
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,
2264 size, f, ptes ? &ptes : NULL)) {
2265 free_vm_area(area);
2266 return NULL;
2267 }
2268
2269 return area;
2270 }
2271 EXPORT_SYMBOL_GPL(alloc_vm_area);
2272
2273 void 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 }
2280 EXPORT_SYMBOL_GPL(free_vm_area);
2281
2282 #ifdef CONFIG_SMP
2283 static 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 */
2300 static 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 */
2346 static 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
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
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.
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 */
2390 struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
2391 const size_t *sizes, int nr_vms,
2392 size_t align)
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
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
2434 vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
2435 vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
2436 if (!vas || !vms)
2437 goto err_free2;
2438
2439 for (area = 0; area < nr_vms; area++) {
2440 vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL);
2441 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
2442 if (!vas[area] || !vms[area])
2443 goto err_free;
2444 }
2445 retry:
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 }
2511 found:
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
2533 err_free:
2534 for (area = 0; area < nr_vms; area++) {
2535 kfree(vas[area]);
2536 kfree(vms[area]);
2537 }
2538 err_free2:
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 */
2551 void 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 }
2559 #endif /* CONFIG_SMP */
2560
2561 #ifdef CONFIG_PROC_FS
2562 static void *s_start(struct seq_file *m, loff_t *pos)
2563 __acquires(&vmap_area_lock)
2564 {
2565 loff_t n = *pos;
2566 struct vmap_area *va;
2567
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) {
2571 n--;
2572 va = list_entry(va->list.next, typeof(*va), list);
2573 }
2574 if (!n && &va->list != &vmap_area_list)
2575 return va;
2576
2577 return NULL;
2578
2579 }
2580
2581 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2582 {
2583 struct vmap_area *va = p, *next;
2584
2585 ++*pos;
2586 next = list_entry(va->list.next, typeof(*va), list);
2587 if (&next->list != &vmap_area_list)
2588 return next;
2589
2590 return NULL;
2591 }
2592
2593 static void s_stop(struct seq_file *m, void *p)
2594 __releases(&vmap_area_lock)
2595 {
2596 spin_unlock(&vmap_area_lock);
2597 }
2598
2599 static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2600 {
2601 if (IS_ENABLED(CONFIG_NUMA)) {
2602 unsigned int nr, *counters = m->private;
2603
2604 if (!counters)
2605 return;
2606
2607 /* Pair with smp_wmb() in clear_vm_unlist() */
2608 smp_rmb();
2609 if (v->flags & VM_UNLIST)
2610 return;
2611
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
2623 static int s_show(struct seq_file *m, void *p)
2624 {
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;
2639
2640 seq_printf(m, "0x%pK-0x%pK %7ld",
2641 v->addr, v->addr + v->size, v->size);
2642
2643 if (v->caller)
2644 seq_printf(m, " %pS", v->caller);
2645
2646 if (v->nr_pages)
2647 seq_printf(m, " pages=%d", v->nr_pages);
2648
2649 if (v->phys_addr)
2650 seq_printf(m, " phys=%llx", (unsigned long long)v->phys_addr);
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
2667 show_numa_info(m, v);
2668 seq_putc(m, '\n');
2669 return 0;
2670 }
2671
2672 static const struct seq_operations vmalloc_op = {
2673 .start = s_start,
2674 .next = s_next,
2675 .stop = s_stop,
2676 .show = s_show,
2677 };
2678
2679 static int vmalloc_open(struct inode *inode, struct file *file)
2680 {
2681 unsigned int *ptr = NULL;
2682 int ret;
2683
2684 if (IS_ENABLED(CONFIG_NUMA)) {
2685 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
2686 if (ptr == NULL)
2687 return -ENOMEM;
2688 }
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
2698 static 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
2705 static int __init proc_vmalloc_init(void)
2706 {
2707 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
2708 return 0;
2709 }
2710 module_init(proc_vmalloc_init);
2711
2712 void get_vmalloc_info(struct vmalloc_info *vmi)
2713 {
2714 struct vmap_area *va;
2715 unsigned long free_area_size;
2716 unsigned long prev_end;
2717
2718 vmi->used = 0;
2719 vmi->largest_chunk = 0;
2720
2721 prev_end = VMALLOC_START;
2722
2723 spin_lock(&vmap_area_lock);
2724
2725 if (list_empty(&vmap_area_list)) {
2726 vmi->largest_chunk = VMALLOC_TOTAL;
2727 goto out;
2728 }
2729
2730 list_for_each_entry(va, &vmap_area_list, list) {
2731 unsigned long addr = va->va_start;
2732
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;
2740
2741 if (va->flags & (VM_LAZY_FREE | VM_LAZY_FREEING))
2742 continue;
2743
2744 vmi->used += (va->va_end - va->va_start);
2745
2746 free_area_size = addr - prev_end;
2747 if (vmi->largest_chunk < free_area_size)
2748 vmi->largest_chunk = free_area_size;
2749
2750 prev_end = va->va_end;
2751 }
2752
2753 if (VMALLOC_END - prev_end > vmi->largest_chunk)
2754 vmi->largest_chunk = VMALLOC_END - prev_end;
2755
2756 out:
2757 spin_unlock(&vmap_area_lock);
2758 }
2759 #endif
2760