1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 1993 Linus Torvalds
4 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
6 * Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002
7 * Numa awareness, Christoph Lameter, SGI, June 2005
8 * Improving global KVA allocator, Uladzislau Rezki, Sony, May 2019
11 #include <linux/vmalloc.h>
13 #include <linux/module.h>
14 #include <linux/highmem.h>
15 #include <linux/sched/signal.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/set_memory.h>
22 #include <linux/debugobjects.h>
23 #include <linux/kallsyms.h>
24 #include <linux/list.h>
25 #include <linux/notifier.h>
26 #include <linux/rbtree.h>
27 #include <linux/xarray.h>
29 #include <linux/rcupdate.h>
30 #include <linux/pfn.h>
31 #include <linux/kmemleak.h>
32 #include <linux/atomic.h>
33 #include <linux/compiler.h>
34 #include <linux/memcontrol.h>
35 #include <linux/llist.h>
36 #include <linux/bitops.h>
37 #include <linux/rbtree_augmented.h>
38 #include <linux/overflow.h>
39 #include <linux/pgtable.h>
40 #include <linux/uaccess.h>
41 #include <linux/hugetlb.h>
42 #include <linux/sched/mm.h>
43 #include <asm/tlbflush.h>
44 #include <asm/shmparam.h>
47 #include "pgalloc-track.h"
49 #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
50 static unsigned int __ro_after_init ioremap_max_page_shift = BITS_PER_LONG - 1;
52 static int __init set_nohugeiomap(char *str)
54 ioremap_max_page_shift = PAGE_SHIFT;
57 early_param("nohugeiomap", set_nohugeiomap);
58 #else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
59 static const unsigned int ioremap_max_page_shift = PAGE_SHIFT;
60 #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
62 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
63 static bool __ro_after_init vmap_allow_huge = true;
65 static int __init set_nohugevmalloc(char *str)
67 vmap_allow_huge = false;
70 early_param("nohugevmalloc", set_nohugevmalloc);
71 #else /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
72 static const bool vmap_allow_huge = false;
73 #endif /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */
75 bool is_vmalloc_addr(const void *x)
77 unsigned long addr = (unsigned long)kasan_reset_tag(x);
79 return addr >= VMALLOC_START && addr < VMALLOC_END;
81 EXPORT_SYMBOL(is_vmalloc_addr);
83 struct vfree_deferred {
84 struct llist_head list;
85 struct work_struct wq;
87 static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
89 static void __vunmap(const void *, int);
91 static void free_work(struct work_struct *w)
93 struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
94 struct llist_node *t, *llnode;
96 llist_for_each_safe(llnode, t, llist_del_all(&p->list))
97 __vunmap((void *)llnode, 1);
100 /*** Page table manipulation functions ***/
101 static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
102 phys_addr_t phys_addr, pgprot_t prot,
103 unsigned int max_page_shift, pgtbl_mod_mask *mask)
107 unsigned long size = PAGE_SIZE;
109 pfn = phys_addr >> PAGE_SHIFT;
110 pte = pte_alloc_kernel_track(pmd, addr, mask);
114 BUG_ON(!pte_none(*pte));
116 #ifdef CONFIG_HUGETLB_PAGE
117 size = arch_vmap_pte_range_map_size(addr, end, pfn, max_page_shift);
118 if (size != PAGE_SIZE) {
119 pte_t entry = pfn_pte(pfn, prot);
121 entry = arch_make_huge_pte(entry, ilog2(size), 0);
122 set_huge_pte_at(&init_mm, addr, pte, entry);
123 pfn += PFN_DOWN(size);
127 set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
129 } while (pte += PFN_DOWN(size), addr += size, addr != end);
130 *mask |= PGTBL_PTE_MODIFIED;
134 static int vmap_try_huge_pmd(pmd_t *pmd, unsigned long addr, unsigned long end,
135 phys_addr_t phys_addr, pgprot_t prot,
136 unsigned int max_page_shift)
138 if (max_page_shift < PMD_SHIFT)
141 if (!arch_vmap_pmd_supported(prot))
144 if ((end - addr) != PMD_SIZE)
147 if (!IS_ALIGNED(addr, PMD_SIZE))
150 if (!IS_ALIGNED(phys_addr, PMD_SIZE))
153 if (pmd_present(*pmd) && !pmd_free_pte_page(pmd, addr))
156 return pmd_set_huge(pmd, phys_addr, prot);
159 static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
160 phys_addr_t phys_addr, pgprot_t prot,
161 unsigned int max_page_shift, pgtbl_mod_mask *mask)
166 pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
170 next = pmd_addr_end(addr, end);
172 if (vmap_try_huge_pmd(pmd, addr, next, phys_addr, prot,
174 *mask |= PGTBL_PMD_MODIFIED;
178 if (vmap_pte_range(pmd, addr, next, phys_addr, prot, max_page_shift, mask))
180 } while (pmd++, phys_addr += (next - addr), addr = next, addr != end);
184 static int vmap_try_huge_pud(pud_t *pud, unsigned long addr, unsigned long end,
185 phys_addr_t phys_addr, pgprot_t prot,
186 unsigned int max_page_shift)
188 if (max_page_shift < PUD_SHIFT)
191 if (!arch_vmap_pud_supported(prot))
194 if ((end - addr) != PUD_SIZE)
197 if (!IS_ALIGNED(addr, PUD_SIZE))
200 if (!IS_ALIGNED(phys_addr, PUD_SIZE))
203 if (pud_present(*pud) && !pud_free_pmd_page(pud, addr))
206 return pud_set_huge(pud, phys_addr, prot);
209 static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
210 phys_addr_t phys_addr, pgprot_t prot,
211 unsigned int max_page_shift, pgtbl_mod_mask *mask)
216 pud = pud_alloc_track(&init_mm, p4d, addr, mask);
220 next = pud_addr_end(addr, end);
222 if (vmap_try_huge_pud(pud, addr, next, phys_addr, prot,
224 *mask |= PGTBL_PUD_MODIFIED;
228 if (vmap_pmd_range(pud, addr, next, phys_addr, prot,
229 max_page_shift, mask))
231 } while (pud++, phys_addr += (next - addr), addr = next, addr != end);
235 static int vmap_try_huge_p4d(p4d_t *p4d, unsigned long addr, unsigned long end,
236 phys_addr_t phys_addr, pgprot_t prot,
237 unsigned int max_page_shift)
239 if (max_page_shift < P4D_SHIFT)
242 if (!arch_vmap_p4d_supported(prot))
245 if ((end - addr) != P4D_SIZE)
248 if (!IS_ALIGNED(addr, P4D_SIZE))
251 if (!IS_ALIGNED(phys_addr, P4D_SIZE))
254 if (p4d_present(*p4d) && !p4d_free_pud_page(p4d, addr))
257 return p4d_set_huge(p4d, phys_addr, prot);
260 static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
261 phys_addr_t phys_addr, pgprot_t prot,
262 unsigned int max_page_shift, pgtbl_mod_mask *mask)
267 p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
271 next = p4d_addr_end(addr, end);
273 if (vmap_try_huge_p4d(p4d, addr, next, phys_addr, prot,
275 *mask |= PGTBL_P4D_MODIFIED;
279 if (vmap_pud_range(p4d, addr, next, phys_addr, prot,
280 max_page_shift, mask))
282 } while (p4d++, phys_addr += (next - addr), addr = next, addr != end);
286 static int vmap_range_noflush(unsigned long addr, unsigned long end,
287 phys_addr_t phys_addr, pgprot_t prot,
288 unsigned int max_page_shift)
294 pgtbl_mod_mask mask = 0;
300 pgd = pgd_offset_k(addr);
302 next = pgd_addr_end(addr, end);
303 err = vmap_p4d_range(pgd, addr, next, phys_addr, prot,
304 max_page_shift, &mask);
307 } while (pgd++, phys_addr += (next - addr), addr = next, addr != end);
309 if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
310 arch_sync_kernel_mappings(start, end);
315 int ioremap_page_range(unsigned long addr, unsigned long end,
316 phys_addr_t phys_addr, pgprot_t prot)
320 err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot),
321 ioremap_max_page_shift);
322 flush_cache_vmap(addr, end);
326 static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
327 pgtbl_mod_mask *mask)
331 pte = pte_offset_kernel(pmd, addr);
333 pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
334 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
335 } while (pte++, addr += PAGE_SIZE, addr != end);
336 *mask |= PGTBL_PTE_MODIFIED;
339 static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
340 pgtbl_mod_mask *mask)
346 pmd = pmd_offset(pud, addr);
348 next = pmd_addr_end(addr, end);
350 cleared = pmd_clear_huge(pmd);
351 if (cleared || pmd_bad(*pmd))
352 *mask |= PGTBL_PMD_MODIFIED;
356 if (pmd_none_or_clear_bad(pmd))
358 vunmap_pte_range(pmd, addr, next, mask);
361 } while (pmd++, addr = next, addr != end);
364 static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
365 pgtbl_mod_mask *mask)
371 pud = pud_offset(p4d, addr);
373 next = pud_addr_end(addr, end);
375 cleared = pud_clear_huge(pud);
376 if (cleared || pud_bad(*pud))
377 *mask |= PGTBL_PUD_MODIFIED;
381 if (pud_none_or_clear_bad(pud))
383 vunmap_pmd_range(pud, addr, next, mask);
384 } while (pud++, addr = next, addr != end);
387 static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
388 pgtbl_mod_mask *mask)
393 p4d = p4d_offset(pgd, addr);
395 next = p4d_addr_end(addr, end);
399 *mask |= PGTBL_P4D_MODIFIED;
401 if (p4d_none_or_clear_bad(p4d))
403 vunmap_pud_range(p4d, addr, next, mask);
404 } while (p4d++, addr = next, addr != end);
408 * vunmap_range_noflush is similar to vunmap_range, but does not
409 * flush caches or TLBs.
411 * The caller is responsible for calling flush_cache_vmap() before calling
412 * this function, and flush_tlb_kernel_range after it has returned
413 * successfully (and before the addresses are expected to cause a page fault
414 * or be re-mapped for something else, if TLB flushes are being delayed or
417 * This is an internal function only. Do not use outside mm/.
419 void vunmap_range_noflush(unsigned long start, unsigned long end)
423 unsigned long addr = start;
424 pgtbl_mod_mask mask = 0;
427 pgd = pgd_offset_k(addr);
429 next = pgd_addr_end(addr, end);
431 mask |= PGTBL_PGD_MODIFIED;
432 if (pgd_none_or_clear_bad(pgd))
434 vunmap_p4d_range(pgd, addr, next, &mask);
435 } while (pgd++, addr = next, addr != end);
437 if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
438 arch_sync_kernel_mappings(start, end);
442 * vunmap_range - unmap kernel virtual addresses
443 * @addr: start of the VM area to unmap
444 * @end: end of the VM area to unmap (non-inclusive)
446 * Clears any present PTEs in the virtual address range, flushes TLBs and
447 * caches. Any subsequent access to the address before it has been re-mapped
450 void vunmap_range(unsigned long addr, unsigned long end)
452 flush_cache_vunmap(addr, end);
453 vunmap_range_noflush(addr, end);
454 flush_tlb_kernel_range(addr, end);
457 static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr,
458 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
459 pgtbl_mod_mask *mask)
464 * nr is a running index into the array which helps higher level
465 * callers keep track of where we're up to.
468 pte = pte_alloc_kernel_track(pmd, addr, mask);
472 struct page *page = pages[*nr];
474 if (WARN_ON(!pte_none(*pte)))
478 if (WARN_ON(!pfn_valid(page_to_pfn(page))))
481 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
483 } while (pte++, addr += PAGE_SIZE, addr != end);
484 *mask |= PGTBL_PTE_MODIFIED;
488 static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,
489 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
490 pgtbl_mod_mask *mask)
495 pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
499 next = pmd_addr_end(addr, end);
500 if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask))
502 } while (pmd++, addr = next, addr != end);
506 static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
507 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
508 pgtbl_mod_mask *mask)
513 pud = pud_alloc_track(&init_mm, p4d, addr, mask);
517 next = pud_addr_end(addr, end);
518 if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask))
520 } while (pud++, addr = next, addr != end);
524 static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
525 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
526 pgtbl_mod_mask *mask)
531 p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
535 next = p4d_addr_end(addr, end);
536 if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask))
538 } while (p4d++, addr = next, addr != end);
542 static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
543 pgprot_t prot, struct page **pages)
545 unsigned long start = addr;
550 pgtbl_mod_mask mask = 0;
553 pgd = pgd_offset_k(addr);
555 next = pgd_addr_end(addr, end);
557 mask |= PGTBL_PGD_MODIFIED;
558 err = vmap_pages_p4d_range(pgd, addr, next, prot, pages, &nr, &mask);
561 } while (pgd++, addr = next, addr != end);
563 if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
564 arch_sync_kernel_mappings(start, end);
570 * vmap_pages_range_noflush is similar to vmap_pages_range, but does not
573 * The caller is responsible for calling flush_cache_vmap() after this
574 * function returns successfully and before the addresses are accessed.
576 * This is an internal function only. Do not use outside mm/.
578 int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
579 pgprot_t prot, struct page **pages, unsigned int page_shift)
581 unsigned int i, nr = (end - addr) >> PAGE_SHIFT;
583 WARN_ON(page_shift < PAGE_SHIFT);
585 if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC) ||
586 page_shift == PAGE_SHIFT)
587 return vmap_small_pages_range_noflush(addr, end, prot, pages);
589 for (i = 0; i < nr; i += 1U << (page_shift - PAGE_SHIFT)) {
592 err = vmap_range_noflush(addr, addr + (1UL << page_shift),
593 page_to_phys(pages[i]), prot,
598 addr += 1UL << page_shift;
605 * vmap_pages_range - map pages to a kernel virtual address
606 * @addr: start of the VM area to map
607 * @end: end of the VM area to map (non-inclusive)
608 * @prot: page protection flags to use
609 * @pages: pages to map (always PAGE_SIZE pages)
610 * @page_shift: maximum shift that the pages may be mapped with, @pages must
611 * be aligned and contiguous up to at least this shift.
614 * 0 on success, -errno on failure.
616 static int vmap_pages_range(unsigned long addr, unsigned long end,
617 pgprot_t prot, struct page **pages, unsigned int page_shift)
621 err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
622 flush_cache_vmap(addr, end);
626 int is_vmalloc_or_module_addr(const void *x)
629 * ARM, x86-64 and sparc64 put modules in a special place,
630 * and fall back on vmalloc() if that fails. Others
631 * just put it in the vmalloc space.
633 #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
634 unsigned long addr = (unsigned long)kasan_reset_tag(x);
635 if (addr >= MODULES_VADDR && addr < MODULES_END)
638 return is_vmalloc_addr(x);
642 * Walk a vmap address to the struct page it maps. Huge vmap mappings will
643 * return the tail page that corresponds to the base page address, which
644 * matches small vmap mappings.
646 struct page *vmalloc_to_page(const void *vmalloc_addr)
648 unsigned long addr = (unsigned long) vmalloc_addr;
649 struct page *page = NULL;
650 pgd_t *pgd = pgd_offset_k(addr);
657 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
658 * architectures that do not vmalloc module space
660 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
664 if (WARN_ON_ONCE(pgd_leaf(*pgd)))
665 return NULL; /* XXX: no allowance for huge pgd */
666 if (WARN_ON_ONCE(pgd_bad(*pgd)))
669 p4d = p4d_offset(pgd, addr);
673 return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT);
674 if (WARN_ON_ONCE(p4d_bad(*p4d)))
677 pud = pud_offset(p4d, addr);
681 return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
682 if (WARN_ON_ONCE(pud_bad(*pud)))
685 pmd = pmd_offset(pud, addr);
689 return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
690 if (WARN_ON_ONCE(pmd_bad(*pmd)))
693 ptep = pte_offset_map(pmd, addr);
695 if (pte_present(pte))
696 page = pte_page(pte);
701 EXPORT_SYMBOL(vmalloc_to_page);
704 * Map a vmalloc()-space virtual address to the physical page frame number.
706 unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
708 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
710 EXPORT_SYMBOL(vmalloc_to_pfn);
713 /*** Global kva allocator ***/
715 #define DEBUG_AUGMENT_PROPAGATE_CHECK 0
716 #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0
719 static DEFINE_SPINLOCK(vmap_area_lock);
720 static DEFINE_SPINLOCK(free_vmap_area_lock);
721 /* Export for kexec only */
722 LIST_HEAD(vmap_area_list);
723 static struct rb_root vmap_area_root = RB_ROOT;
724 static bool vmap_initialized __read_mostly;
726 static struct rb_root purge_vmap_area_root = RB_ROOT;
727 static LIST_HEAD(purge_vmap_area_list);
728 static DEFINE_SPINLOCK(purge_vmap_area_lock);
731 * This kmem_cache is used for vmap_area objects. Instead of
732 * allocating from slab we reuse an object from this cache to
733 * make things faster. Especially in "no edge" splitting of
736 static struct kmem_cache *vmap_area_cachep;
739 * This linked list is used in pair with free_vmap_area_root.
740 * It gives O(1) access to prev/next to perform fast coalescing.
742 static LIST_HEAD(free_vmap_area_list);
745 * This augment red-black tree represents the free vmap space.
746 * All vmap_area objects in this tree are sorted by va->va_start
747 * address. It is used for allocation and merging when a vmap
748 * object is released.
750 * Each vmap_area node contains a maximum available free block
751 * of its sub-tree, right or left. Therefore it is possible to
752 * find a lowest match of free area.
754 static struct rb_root free_vmap_area_root = RB_ROOT;
757 * Preload a CPU with one object for "no edge" split case. The
758 * aim is to get rid of allocations from the atomic context, thus
759 * to use more permissive allocation masks.
761 static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node);
763 static __always_inline unsigned long
764 va_size(struct vmap_area *va)
766 return (va->va_end - va->va_start);
769 static __always_inline unsigned long
770 get_subtree_max_size(struct rb_node *node)
772 struct vmap_area *va;
774 va = rb_entry_safe(node, struct vmap_area, rb_node);
775 return va ? va->subtree_max_size : 0;
778 RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb,
779 struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size)
781 static void purge_vmap_area_lazy(void);
782 static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
783 static void drain_vmap_area_work(struct work_struct *work);
784 static DECLARE_WORK(drain_vmap_work, drain_vmap_area_work);
786 static atomic_long_t nr_vmalloc_pages;
788 unsigned long vmalloc_nr_pages(void)
790 return atomic_long_read(&nr_vmalloc_pages);
793 /* Look up the first VA which satisfies addr < va_end, NULL if none. */
794 static struct vmap_area *find_vmap_area_exceed_addr(unsigned long addr)
796 struct vmap_area *va = NULL;
797 struct rb_node *n = vmap_area_root.rb_node;
799 addr = (unsigned long)kasan_reset_tag((void *)addr);
802 struct vmap_area *tmp;
804 tmp = rb_entry(n, struct vmap_area, rb_node);
805 if (tmp->va_end > addr) {
807 if (tmp->va_start <= addr)
818 static struct vmap_area *__find_vmap_area(unsigned long addr, struct rb_root *root)
820 struct rb_node *n = root->rb_node;
822 addr = (unsigned long)kasan_reset_tag((void *)addr);
825 struct vmap_area *va;
827 va = rb_entry(n, struct vmap_area, rb_node);
828 if (addr < va->va_start)
830 else if (addr >= va->va_end)
840 * This function returns back addresses of parent node
841 * and its left or right link for further processing.
843 * Otherwise NULL is returned. In that case all further
844 * steps regarding inserting of conflicting overlap range
845 * have to be declined and actually considered as a bug.
847 static __always_inline struct rb_node **
848 find_va_links(struct vmap_area *va,
849 struct rb_root *root, struct rb_node *from,
850 struct rb_node **parent)
852 struct vmap_area *tmp_va;
853 struct rb_node **link;
856 link = &root->rb_node;
857 if (unlikely(!*link)) {
866 * Go to the bottom of the tree. When we hit the last point
867 * we end up with parent rb_node and correct direction, i name
868 * it link, where the new va->rb_node will be attached to.
871 tmp_va = rb_entry(*link, struct vmap_area, rb_node);
874 * During the traversal we also do some sanity check.
875 * Trigger the BUG() if there are sides(left/right)
878 if (va->va_end <= tmp_va->va_start)
879 link = &(*link)->rb_left;
880 else if (va->va_start >= tmp_va->va_end)
881 link = &(*link)->rb_right;
883 WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n",
884 va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end);
890 *parent = &tmp_va->rb_node;
894 static __always_inline struct list_head *
895 get_va_next_sibling(struct rb_node *parent, struct rb_node **link)
897 struct list_head *list;
899 if (unlikely(!parent))
901 * The red-black tree where we try to find VA neighbors
902 * before merging or inserting is empty, i.e. it means
903 * there is no free vmap space. Normally it does not
904 * happen but we handle this case anyway.
908 list = &rb_entry(parent, struct vmap_area, rb_node)->list;
909 return (&parent->rb_right == link ? list->next : list);
912 static __always_inline void
913 __link_va(struct vmap_area *va, struct rb_root *root,
914 struct rb_node *parent, struct rb_node **link,
915 struct list_head *head, bool augment)
918 * VA is still not in the list, but we can
919 * identify its future previous list_head node.
921 if (likely(parent)) {
922 head = &rb_entry(parent, struct vmap_area, rb_node)->list;
923 if (&parent->rb_right != link)
927 /* Insert to the rb-tree */
928 rb_link_node(&va->rb_node, parent, link);
931 * Some explanation here. Just perform simple insertion
932 * to the tree. We do not set va->subtree_max_size to
933 * its current size before calling rb_insert_augmented().
934 * It is because we populate the tree from the bottom
935 * to parent levels when the node _is_ in the tree.
937 * Therefore we set subtree_max_size to zero after insertion,
938 * to let __augment_tree_propagate_from() puts everything to
939 * the correct order later on.
941 rb_insert_augmented(&va->rb_node,
942 root, &free_vmap_area_rb_augment_cb);
943 va->subtree_max_size = 0;
945 rb_insert_color(&va->rb_node, root);
948 /* Address-sort this list */
949 list_add(&va->list, head);
952 static __always_inline void
953 link_va(struct vmap_area *va, struct rb_root *root,
954 struct rb_node *parent, struct rb_node **link,
955 struct list_head *head)
957 __link_va(va, root, parent, link, head, false);
960 static __always_inline void
961 link_va_augment(struct vmap_area *va, struct rb_root *root,
962 struct rb_node *parent, struct rb_node **link,
963 struct list_head *head)
965 __link_va(va, root, parent, link, head, true);
968 static __always_inline void
969 __unlink_va(struct vmap_area *va, struct rb_root *root, bool augment)
971 if (WARN_ON(RB_EMPTY_NODE(&va->rb_node)))
975 rb_erase_augmented(&va->rb_node,
976 root, &free_vmap_area_rb_augment_cb);
978 rb_erase(&va->rb_node, root);
980 list_del_init(&va->list);
981 RB_CLEAR_NODE(&va->rb_node);
984 static __always_inline void
985 unlink_va(struct vmap_area *va, struct rb_root *root)
987 __unlink_va(va, root, false);
990 static __always_inline void
991 unlink_va_augment(struct vmap_area *va, struct rb_root *root)
993 __unlink_va(va, root, true);
996 #if DEBUG_AUGMENT_PROPAGATE_CHECK
998 * Gets called when remove the node and rotate.
1000 static __always_inline unsigned long
1001 compute_subtree_max_size(struct vmap_area *va)
1003 return max3(va_size(va),
1004 get_subtree_max_size(va->rb_node.rb_left),
1005 get_subtree_max_size(va->rb_node.rb_right));
1009 augment_tree_propagate_check(void)
1011 struct vmap_area *va;
1012 unsigned long computed_size;
1014 list_for_each_entry(va, &free_vmap_area_list, list) {
1015 computed_size = compute_subtree_max_size(va);
1016 if (computed_size != va->subtree_max_size)
1017 pr_emerg("tree is corrupted: %lu, %lu\n",
1018 va_size(va), va->subtree_max_size);
1024 * This function populates subtree_max_size from bottom to upper
1025 * levels starting from VA point. The propagation must be done
1026 * when VA size is modified by changing its va_start/va_end. Or
1027 * in case of newly inserting of VA to the tree.
1029 * It means that __augment_tree_propagate_from() must be called:
1030 * - After VA has been inserted to the tree(free path);
1031 * - After VA has been shrunk(allocation path);
1032 * - After VA has been increased(merging path).
1034 * Please note that, it does not mean that upper parent nodes
1035 * and their subtree_max_size are recalculated all the time up
1044 * For example if we modify the node 4, shrinking it to 2, then
1045 * no any modification is required. If we shrink the node 2 to 1
1046 * its subtree_max_size is updated only, and set to 1. If we shrink
1047 * the node 8 to 6, then its subtree_max_size is set to 6 and parent
1048 * node becomes 4--6.
1050 static __always_inline void
1051 augment_tree_propagate_from(struct vmap_area *va)
1054 * Populate the tree from bottom towards the root until
1055 * the calculated maximum available size of checked node
1056 * is equal to its current one.
1058 free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL);
1060 #if DEBUG_AUGMENT_PROPAGATE_CHECK
1061 augment_tree_propagate_check();
1066 insert_vmap_area(struct vmap_area *va,
1067 struct rb_root *root, struct list_head *head)
1069 struct rb_node **link;
1070 struct rb_node *parent;
1072 link = find_va_links(va, root, NULL, &parent);
1074 link_va(va, root, parent, link, head);
1078 insert_vmap_area_augment(struct vmap_area *va,
1079 struct rb_node *from, struct rb_root *root,
1080 struct list_head *head)
1082 struct rb_node **link;
1083 struct rb_node *parent;
1086 link = find_va_links(va, NULL, from, &parent);
1088 link = find_va_links(va, root, NULL, &parent);
1091 link_va_augment(va, root, parent, link, head);
1092 augment_tree_propagate_from(va);
1097 * Merge de-allocated chunk of VA memory with previous
1098 * and next free blocks. If coalesce is not done a new
1099 * free area is inserted. If VA has been merged, it is
1102 * Please note, it can return NULL in case of overlap
1103 * ranges, followed by WARN() report. Despite it is a
1104 * buggy behaviour, a system can be alive and keep
1107 static __always_inline struct vmap_area *
1108 __merge_or_add_vmap_area(struct vmap_area *va,
1109 struct rb_root *root, struct list_head *head, bool augment)
1111 struct vmap_area *sibling;
1112 struct list_head *next;
1113 struct rb_node **link;
1114 struct rb_node *parent;
1115 bool merged = false;
1118 * Find a place in the tree where VA potentially will be
1119 * inserted, unless it is merged with its sibling/siblings.
1121 link = find_va_links(va, root, NULL, &parent);
1126 * Get next node of VA to check if merging can be done.
1128 next = get_va_next_sibling(parent, link);
1129 if (unlikely(next == NULL))
1135 * |<------VA------>|<-----Next----->|
1140 sibling = list_entry(next, struct vmap_area, list);
1141 if (sibling->va_start == va->va_end) {
1142 sibling->va_start = va->va_start;
1144 /* Free vmap_area object. */
1145 kmem_cache_free(vmap_area_cachep, va);
1147 /* Point to the new merged area. */
1156 * |<-----Prev----->|<------VA------>|
1160 if (next->prev != head) {
1161 sibling = list_entry(next->prev, struct vmap_area, list);
1162 if (sibling->va_end == va->va_start) {
1164 * If both neighbors are coalesced, it is important
1165 * to unlink the "next" node first, followed by merging
1166 * with "previous" one. Otherwise the tree might not be
1167 * fully populated if a sibling's augmented value is
1168 * "normalized" because of rotation operations.
1171 __unlink_va(va, root, augment);
1173 sibling->va_end = va->va_end;
1175 /* Free vmap_area object. */
1176 kmem_cache_free(vmap_area_cachep, va);
1178 /* Point to the new merged area. */
1186 __link_va(va, root, parent, link, head, augment);
1191 static __always_inline struct vmap_area *
1192 merge_or_add_vmap_area(struct vmap_area *va,
1193 struct rb_root *root, struct list_head *head)
1195 return __merge_or_add_vmap_area(va, root, head, false);
1198 static __always_inline struct vmap_area *
1199 merge_or_add_vmap_area_augment(struct vmap_area *va,
1200 struct rb_root *root, struct list_head *head)
1202 va = __merge_or_add_vmap_area(va, root, head, true);
1204 augment_tree_propagate_from(va);
1209 static __always_inline bool
1210 is_within_this_va(struct vmap_area *va, unsigned long size,
1211 unsigned long align, unsigned long vstart)
1213 unsigned long nva_start_addr;
1215 if (va->va_start > vstart)
1216 nva_start_addr = ALIGN(va->va_start, align);
1218 nva_start_addr = ALIGN(vstart, align);
1220 /* Can be overflowed due to big size or alignment. */
1221 if (nva_start_addr + size < nva_start_addr ||
1222 nva_start_addr < vstart)
1225 return (nva_start_addr + size <= va->va_end);
1229 * Find the first free block(lowest start address) in the tree,
1230 * that will accomplish the request corresponding to passing
1231 * parameters. Please note, with an alignment bigger than PAGE_SIZE,
1232 * a search length is adjusted to account for worst case alignment
1235 static __always_inline struct vmap_area *
1236 find_vmap_lowest_match(struct rb_root *root, unsigned long size,
1237 unsigned long align, unsigned long vstart, bool adjust_search_size)
1239 struct vmap_area *va;
1240 struct rb_node *node;
1241 unsigned long length;
1243 /* Start from the root. */
1244 node = root->rb_node;
1246 /* Adjust the search size for alignment overhead. */
1247 length = adjust_search_size ? size + align - 1 : size;
1250 va = rb_entry(node, struct vmap_area, rb_node);
1252 if (get_subtree_max_size(node->rb_left) >= length &&
1253 vstart < va->va_start) {
1254 node = node->rb_left;
1256 if (is_within_this_va(va, size, align, vstart))
1260 * Does not make sense to go deeper towards the right
1261 * sub-tree if it does not have a free block that is
1262 * equal or bigger to the requested search length.
1264 if (get_subtree_max_size(node->rb_right) >= length) {
1265 node = node->rb_right;
1270 * OK. We roll back and find the first right sub-tree,
1271 * that will satisfy the search criteria. It can happen
1272 * due to "vstart" restriction or an alignment overhead
1273 * that is bigger then PAGE_SIZE.
1275 while ((node = rb_parent(node))) {
1276 va = rb_entry(node, struct vmap_area, rb_node);
1277 if (is_within_this_va(va, size, align, vstart))
1280 if (get_subtree_max_size(node->rb_right) >= length &&
1281 vstart <= va->va_start) {
1283 * Shift the vstart forward. Please note, we update it with
1284 * parent's start address adding "1" because we do not want
1285 * to enter same sub-tree after it has already been checked
1286 * and no suitable free block found there.
1288 vstart = va->va_start + 1;
1289 node = node->rb_right;
1299 #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1300 #include <linux/random.h>
1302 static struct vmap_area *
1303 find_vmap_lowest_linear_match(struct list_head *head, unsigned long size,
1304 unsigned long align, unsigned long vstart)
1306 struct vmap_area *va;
1308 list_for_each_entry(va, head, list) {
1309 if (!is_within_this_va(va, size, align, vstart))
1319 find_vmap_lowest_match_check(struct rb_root *root, struct list_head *head,
1320 unsigned long size, unsigned long align)
1322 struct vmap_area *va_1, *va_2;
1323 unsigned long vstart;
1326 get_random_bytes(&rnd, sizeof(rnd));
1327 vstart = VMALLOC_START + rnd;
1329 va_1 = find_vmap_lowest_match(root, size, align, vstart, false);
1330 va_2 = find_vmap_lowest_linear_match(head, size, align, vstart);
1333 pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n",
1334 va_1, va_2, vstart);
1340 FL_FIT_TYPE = 1, /* full fit */
1341 LE_FIT_TYPE = 2, /* left edge fit */
1342 RE_FIT_TYPE = 3, /* right edge fit */
1343 NE_FIT_TYPE = 4 /* no edge fit */
1346 static __always_inline enum fit_type
1347 classify_va_fit_type(struct vmap_area *va,
1348 unsigned long nva_start_addr, unsigned long size)
1352 /* Check if it is within VA. */
1353 if (nva_start_addr < va->va_start ||
1354 nva_start_addr + size > va->va_end)
1358 if (va->va_start == nva_start_addr) {
1359 if (va->va_end == nva_start_addr + size)
1363 } else if (va->va_end == nva_start_addr + size) {
1372 static __always_inline int
1373 adjust_va_to_fit_type(struct rb_root *root, struct list_head *head,
1374 struct vmap_area *va, unsigned long nva_start_addr,
1377 struct vmap_area *lva = NULL;
1378 enum fit_type type = classify_va_fit_type(va, nva_start_addr, size);
1380 if (type == FL_FIT_TYPE) {
1382 * No need to split VA, it fully fits.
1388 unlink_va_augment(va, root);
1389 kmem_cache_free(vmap_area_cachep, va);
1390 } else if (type == LE_FIT_TYPE) {
1392 * Split left edge of fit VA.
1398 va->va_start += size;
1399 } else if (type == RE_FIT_TYPE) {
1401 * Split right edge of fit VA.
1407 va->va_end = nva_start_addr;
1408 } else if (type == NE_FIT_TYPE) {
1410 * Split no edge of fit VA.
1416 lva = __this_cpu_xchg(ne_fit_preload_node, NULL);
1417 if (unlikely(!lva)) {
1419 * For percpu allocator we do not do any pre-allocation
1420 * and leave it as it is. The reason is it most likely
1421 * never ends up with NE_FIT_TYPE splitting. In case of
1422 * percpu allocations offsets and sizes are aligned to
1423 * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE
1424 * are its main fitting cases.
1426 * There are a few exceptions though, as an example it is
1427 * a first allocation (early boot up) when we have "one"
1428 * big free space that has to be split.
1430 * Also we can hit this path in case of regular "vmap"
1431 * allocations, if "this" current CPU was not preloaded.
1432 * See the comment in alloc_vmap_area() why. If so, then
1433 * GFP_NOWAIT is used instead to get an extra object for
1434 * split purpose. That is rare and most time does not
1437 * What happens if an allocation gets failed. Basically,
1438 * an "overflow" path is triggered to purge lazily freed
1439 * areas to free some memory, then, the "retry" path is
1440 * triggered to repeat one more time. See more details
1441 * in alloc_vmap_area() function.
1443 lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
1449 * Build the remainder.
1451 lva->va_start = va->va_start;
1452 lva->va_end = nva_start_addr;
1455 * Shrink this VA to remaining size.
1457 va->va_start = nva_start_addr + size;
1462 if (type != FL_FIT_TYPE) {
1463 augment_tree_propagate_from(va);
1465 if (lva) /* type == NE_FIT_TYPE */
1466 insert_vmap_area_augment(lva, &va->rb_node, root, head);
1473 * Returns a start address of the newly allocated area, if success.
1474 * Otherwise a vend is returned that indicates failure.
1476 static __always_inline unsigned long
1477 __alloc_vmap_area(struct rb_root *root, struct list_head *head,
1478 unsigned long size, unsigned long align,
1479 unsigned long vstart, unsigned long vend)
1481 bool adjust_search_size = true;
1482 unsigned long nva_start_addr;
1483 struct vmap_area *va;
1487 * Do not adjust when:
1488 * a) align <= PAGE_SIZE, because it does not make any sense.
1489 * All blocks(their start addresses) are at least PAGE_SIZE
1491 * b) a short range where a requested size corresponds to exactly
1492 * specified [vstart:vend] interval and an alignment > PAGE_SIZE.
1493 * With adjusted search length an allocation would not succeed.
1495 if (align <= PAGE_SIZE || (align > PAGE_SIZE && (vend - vstart) == size))
1496 adjust_search_size = false;
1498 va = find_vmap_lowest_match(root, size, align, vstart, adjust_search_size);
1502 if (va->va_start > vstart)
1503 nva_start_addr = ALIGN(va->va_start, align);
1505 nva_start_addr = ALIGN(vstart, align);
1507 /* Check the "vend" restriction. */
1508 if (nva_start_addr + size > vend)
1511 /* Update the free vmap_area. */
1512 ret = adjust_va_to_fit_type(root, head, va, nva_start_addr, size);
1513 if (WARN_ON_ONCE(ret))
1516 #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1517 find_vmap_lowest_match_check(root, head, size, align);
1520 return nva_start_addr;
1524 * Free a region of KVA allocated by alloc_vmap_area
1526 static void free_vmap_area(struct vmap_area *va)
1529 * Remove from the busy tree/list.
1531 spin_lock(&vmap_area_lock);
1532 unlink_va(va, &vmap_area_root);
1533 spin_unlock(&vmap_area_lock);
1536 * Insert/Merge it back to the free tree/list.
1538 spin_lock(&free_vmap_area_lock);
1539 merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list);
1540 spin_unlock(&free_vmap_area_lock);
1544 preload_this_cpu_lock(spinlock_t *lock, gfp_t gfp_mask, int node)
1546 struct vmap_area *va = NULL;
1549 * Preload this CPU with one extra vmap_area object. It is used
1550 * when fit type of free area is NE_FIT_TYPE. It guarantees that
1551 * a CPU that does an allocation is preloaded.
1553 * We do it in non-atomic context, thus it allows us to use more
1554 * permissive allocation masks to be more stable under low memory
1555 * condition and high memory pressure.
1557 if (!this_cpu_read(ne_fit_preload_node))
1558 va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
1562 if (va && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, va))
1563 kmem_cache_free(vmap_area_cachep, va);
1567 * Allocate a region of KVA of the specified size and alignment, within the
1570 static struct vmap_area *alloc_vmap_area(unsigned long size,
1571 unsigned long align,
1572 unsigned long vstart, unsigned long vend,
1573 int node, gfp_t gfp_mask)
1575 struct vmap_area *va;
1576 unsigned long freed;
1582 BUG_ON(offset_in_page(size));
1583 BUG_ON(!is_power_of_2(align));
1585 if (unlikely(!vmap_initialized))
1586 return ERR_PTR(-EBUSY);
1589 gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
1591 va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
1593 return ERR_PTR(-ENOMEM);
1596 * Only scan the relevant parts containing pointers to other objects
1597 * to avoid false negatives.
1599 kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask);
1602 preload_this_cpu_lock(&free_vmap_area_lock, gfp_mask, node);
1603 addr = __alloc_vmap_area(&free_vmap_area_root, &free_vmap_area_list,
1604 size, align, vstart, vend);
1605 spin_unlock(&free_vmap_area_lock);
1608 * If an allocation fails, the "vend" address is
1609 * returned. Therefore trigger the overflow path.
1611 if (unlikely(addr == vend))
1614 va->va_start = addr;
1615 va->va_end = addr + size;
1618 spin_lock(&vmap_area_lock);
1619 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
1620 spin_unlock(&vmap_area_lock);
1622 BUG_ON(!IS_ALIGNED(va->va_start, align));
1623 BUG_ON(va->va_start < vstart);
1624 BUG_ON(va->va_end > vend);
1626 ret = kasan_populate_vmalloc(addr, size);
1629 return ERR_PTR(ret);
1636 purge_vmap_area_lazy();
1642 blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
1649 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
1650 pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1653 kmem_cache_free(vmap_area_cachep, va);
1654 return ERR_PTR(-EBUSY);
1657 int register_vmap_purge_notifier(struct notifier_block *nb)
1659 return blocking_notifier_chain_register(&vmap_notify_list, nb);
1661 EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
1663 int unregister_vmap_purge_notifier(struct notifier_block *nb)
1665 return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
1667 EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
1670 * lazy_max_pages is the maximum amount of virtual address space we gather up
1671 * before attempting to purge with a TLB flush.
1673 * There is a tradeoff here: a larger number will cover more kernel page tables
1674 * and take slightly longer to purge, but it will linearly reduce the number of
1675 * global TLB flushes that must be performed. It would seem natural to scale
1676 * this number up linearly with the number of CPUs (because vmapping activity
1677 * could also scale linearly with the number of CPUs), however it is likely
1678 * that in practice, workloads might be constrained in other ways that mean
1679 * vmap activity will not scale linearly with CPUs. Also, I want to be
1680 * conservative and not introduce a big latency on huge systems, so go with
1681 * a less aggressive log scale. It will still be an improvement over the old
1682 * code, and it will be simple to change the scale factor if we find that it
1683 * becomes a problem on bigger systems.
1685 static unsigned long lazy_max_pages(void)
1689 log = fls(num_online_cpus());
1691 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
1694 static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0);
1697 * Serialize vmap purging. There is no actual critical section protected
1698 * by this lock, but we want to avoid concurrent calls for performance
1699 * reasons and to make the pcpu_get_vm_areas more deterministic.
1701 static DEFINE_MUTEX(vmap_purge_lock);
1703 /* for per-CPU blocks */
1704 static void purge_fragmented_blocks_allcpus(void);
1707 * Purges all lazily-freed vmap areas.
1709 static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
1711 unsigned long resched_threshold;
1712 struct list_head local_purge_list;
1713 struct vmap_area *va, *n_va;
1715 lockdep_assert_held(&vmap_purge_lock);
1717 spin_lock(&purge_vmap_area_lock);
1718 purge_vmap_area_root = RB_ROOT;
1719 list_replace_init(&purge_vmap_area_list, &local_purge_list);
1720 spin_unlock(&purge_vmap_area_lock);
1722 if (unlikely(list_empty(&local_purge_list)))
1726 list_first_entry(&local_purge_list,
1727 struct vmap_area, list)->va_start);
1730 list_last_entry(&local_purge_list,
1731 struct vmap_area, list)->va_end);
1733 flush_tlb_kernel_range(start, end);
1734 resched_threshold = lazy_max_pages() << 1;
1736 spin_lock(&free_vmap_area_lock);
1737 list_for_each_entry_safe(va, n_va, &local_purge_list, list) {
1738 unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
1739 unsigned long orig_start = va->va_start;
1740 unsigned long orig_end = va->va_end;
1743 * Finally insert or merge lazily-freed area. It is
1744 * detached and there is no need to "unlink" it from
1747 va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root,
1748 &free_vmap_area_list);
1753 if (is_vmalloc_or_module_addr((void *)orig_start))
1754 kasan_release_vmalloc(orig_start, orig_end,
1755 va->va_start, va->va_end);
1757 atomic_long_sub(nr, &vmap_lazy_nr);
1759 if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
1760 cond_resched_lock(&free_vmap_area_lock);
1762 spin_unlock(&free_vmap_area_lock);
1767 * Kick off a purge of the outstanding lazy areas.
1769 static void purge_vmap_area_lazy(void)
1771 mutex_lock(&vmap_purge_lock);
1772 purge_fragmented_blocks_allcpus();
1773 __purge_vmap_area_lazy(ULONG_MAX, 0);
1774 mutex_unlock(&vmap_purge_lock);
1777 static void drain_vmap_area_work(struct work_struct *work)
1779 unsigned long nr_lazy;
1782 mutex_lock(&vmap_purge_lock);
1783 __purge_vmap_area_lazy(ULONG_MAX, 0);
1784 mutex_unlock(&vmap_purge_lock);
1786 /* Recheck if further work is required. */
1787 nr_lazy = atomic_long_read(&vmap_lazy_nr);
1788 } while (nr_lazy > lazy_max_pages());
1792 * Free a vmap area, caller ensuring that the area has been unmapped
1793 * and flush_cache_vunmap had been called for the correct range
1796 static void free_vmap_area_noflush(struct vmap_area *va)
1798 unsigned long nr_lazy;
1800 spin_lock(&vmap_area_lock);
1801 unlink_va(va, &vmap_area_root);
1802 spin_unlock(&vmap_area_lock);
1804 nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
1805 PAGE_SHIFT, &vmap_lazy_nr);
1808 * Merge or place it to the purge tree/list.
1810 spin_lock(&purge_vmap_area_lock);
1811 merge_or_add_vmap_area(va,
1812 &purge_vmap_area_root, &purge_vmap_area_list);
1813 spin_unlock(&purge_vmap_area_lock);
1815 /* After this point, we may free va at any time */
1816 if (unlikely(nr_lazy > lazy_max_pages()))
1817 schedule_work(&drain_vmap_work);
1821 * Free and unmap a vmap area
1823 static void free_unmap_vmap_area(struct vmap_area *va)
1825 flush_cache_vunmap(va->va_start, va->va_end);
1826 vunmap_range_noflush(va->va_start, va->va_end);
1827 if (debug_pagealloc_enabled_static())
1828 flush_tlb_kernel_range(va->va_start, va->va_end);
1830 free_vmap_area_noflush(va);
1833 struct vmap_area *find_vmap_area(unsigned long addr)
1835 struct vmap_area *va;
1837 spin_lock(&vmap_area_lock);
1838 va = __find_vmap_area(addr, &vmap_area_root);
1839 spin_unlock(&vmap_area_lock);
1844 /*** Per cpu kva allocator ***/
1847 * vmap space is limited especially on 32 bit architectures. Ensure there is
1848 * room for at least 16 percpu vmap blocks per CPU.
1851 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
1852 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
1853 * instead (we just need a rough idea)
1855 #if BITS_PER_LONG == 32
1856 #define VMALLOC_SPACE (128UL*1024*1024)
1858 #define VMALLOC_SPACE (128UL*1024*1024*1024)
1861 #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
1862 #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
1863 #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
1864 #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
1865 #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
1866 #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
1867 #define VMAP_BBMAP_BITS \
1868 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
1869 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
1870 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
1872 #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
1874 struct vmap_block_queue {
1876 struct list_head free;
1881 struct vmap_area *va;
1882 unsigned long free, dirty;
1883 unsigned long dirty_min, dirty_max; /*< dirty range */
1884 struct list_head free_list;
1885 struct rcu_head rcu_head;
1886 struct list_head purge;
1889 /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1890 static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1893 * XArray of vmap blocks, indexed by address, to quickly find a vmap block
1894 * in the free path. Could get rid of this if we change the API to return a
1895 * "cookie" from alloc, to be passed to free. But no big deal yet.
1897 static DEFINE_XARRAY(vmap_blocks);
1900 * We should probably have a fallback mechanism to allocate virtual memory
1901 * out of partially filled vmap blocks. However vmap block sizing should be
1902 * fairly reasonable according to the vmalloc size, so it shouldn't be a
1906 static unsigned long addr_to_vb_idx(unsigned long addr)
1908 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
1909 addr /= VMAP_BLOCK_SIZE;
1913 static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
1917 addr = va_start + (pages_off << PAGE_SHIFT);
1918 BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
1919 return (void *)addr;
1923 * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
1924 * block. Of course pages number can't exceed VMAP_BBMAP_BITS
1925 * @order: how many 2^order pages should be occupied in newly allocated block
1926 * @gfp_mask: flags for the page level allocator
1928 * Return: virtual address in a newly allocated block or ERR_PTR(-errno)
1930 static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
1932 struct vmap_block_queue *vbq;
1933 struct vmap_block *vb;
1934 struct vmap_area *va;
1935 unsigned long vb_idx;
1939 node = numa_node_id();
1941 vb = kmalloc_node(sizeof(struct vmap_block),
1942 gfp_mask & GFP_RECLAIM_MASK, node);
1944 return ERR_PTR(-ENOMEM);
1946 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
1947 VMALLOC_START, VMALLOC_END,
1951 return ERR_CAST(va);
1954 vaddr = vmap_block_vaddr(va->va_start, 0);
1955 spin_lock_init(&vb->lock);
1957 /* At least something should be left free */
1958 BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
1959 vb->free = VMAP_BBMAP_BITS - (1UL << order);
1961 vb->dirty_min = VMAP_BBMAP_BITS;
1963 INIT_LIST_HEAD(&vb->free_list);
1965 vb_idx = addr_to_vb_idx(va->va_start);
1966 err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask);
1970 return ERR_PTR(err);
1973 vbq = raw_cpu_ptr(&vmap_block_queue);
1974 spin_lock(&vbq->lock);
1975 list_add_tail_rcu(&vb->free_list, &vbq->free);
1976 spin_unlock(&vbq->lock);
1981 static void free_vmap_block(struct vmap_block *vb)
1983 struct vmap_block *tmp;
1985 tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start));
1988 free_vmap_area_noflush(vb->va);
1989 kfree_rcu(vb, rcu_head);
1992 static void purge_fragmented_blocks(int cpu)
1995 struct vmap_block *vb;
1996 struct vmap_block *n_vb;
1997 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
2000 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
2002 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
2005 spin_lock(&vb->lock);
2006 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
2007 vb->free = 0; /* prevent further allocs after releasing lock */
2008 vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
2010 vb->dirty_max = VMAP_BBMAP_BITS;
2011 spin_lock(&vbq->lock);
2012 list_del_rcu(&vb->free_list);
2013 spin_unlock(&vbq->lock);
2014 spin_unlock(&vb->lock);
2015 list_add_tail(&vb->purge, &purge);
2017 spin_unlock(&vb->lock);
2021 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
2022 list_del(&vb->purge);
2023 free_vmap_block(vb);
2027 static void purge_fragmented_blocks_allcpus(void)
2031 for_each_possible_cpu(cpu)
2032 purge_fragmented_blocks(cpu);
2035 static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
2037 struct vmap_block_queue *vbq;
2038 struct vmap_block *vb;
2042 BUG_ON(offset_in_page(size));
2043 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
2044 if (WARN_ON(size == 0)) {
2046 * Allocating 0 bytes isn't what caller wants since
2047 * get_order(0) returns funny result. Just warn and terminate
2052 order = get_order(size);
2055 vbq = raw_cpu_ptr(&vmap_block_queue);
2056 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
2057 unsigned long pages_off;
2059 spin_lock(&vb->lock);
2060 if (vb->free < (1UL << order)) {
2061 spin_unlock(&vb->lock);
2065 pages_off = VMAP_BBMAP_BITS - vb->free;
2066 vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
2067 vb->free -= 1UL << order;
2068 if (vb->free == 0) {
2069 spin_lock(&vbq->lock);
2070 list_del_rcu(&vb->free_list);
2071 spin_unlock(&vbq->lock);
2074 spin_unlock(&vb->lock);
2080 /* Allocate new block if nothing was found */
2082 vaddr = new_vmap_block(order, gfp_mask);
2087 static void vb_free(unsigned long addr, unsigned long size)
2089 unsigned long offset;
2091 struct vmap_block *vb;
2093 BUG_ON(offset_in_page(size));
2094 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
2096 flush_cache_vunmap(addr, addr + size);
2098 order = get_order(size);
2099 offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT;
2100 vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr));
2102 vunmap_range_noflush(addr, addr + size);
2104 if (debug_pagealloc_enabled_static())
2105 flush_tlb_kernel_range(addr, addr + size);
2107 spin_lock(&vb->lock);
2109 /* Expand dirty range */
2110 vb->dirty_min = min(vb->dirty_min, offset);
2111 vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
2113 vb->dirty += 1UL << order;
2114 if (vb->dirty == VMAP_BBMAP_BITS) {
2116 spin_unlock(&vb->lock);
2117 free_vmap_block(vb);
2119 spin_unlock(&vb->lock);
2122 static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
2126 if (unlikely(!vmap_initialized))
2131 for_each_possible_cpu(cpu) {
2132 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
2133 struct vmap_block *vb;
2136 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
2137 spin_lock(&vb->lock);
2138 if (vb->dirty && vb->dirty != VMAP_BBMAP_BITS) {
2139 unsigned long va_start = vb->va->va_start;
2142 s = va_start + (vb->dirty_min << PAGE_SHIFT);
2143 e = va_start + (vb->dirty_max << PAGE_SHIFT);
2145 start = min(s, start);
2150 spin_unlock(&vb->lock);
2155 mutex_lock(&vmap_purge_lock);
2156 purge_fragmented_blocks_allcpus();
2157 if (!__purge_vmap_area_lazy(start, end) && flush)
2158 flush_tlb_kernel_range(start, end);
2159 mutex_unlock(&vmap_purge_lock);
2163 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
2165 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
2166 * to amortize TLB flushing overheads. What this means is that any page you
2167 * have now, may, in a former life, have been mapped into kernel virtual
2168 * address by the vmap layer and so there might be some CPUs with TLB entries
2169 * still referencing that page (additional to the regular 1:1 kernel mapping).
2171 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
2172 * be sure that none of the pages we have control over will have any aliases
2173 * from the vmap layer.
2175 void vm_unmap_aliases(void)
2177 unsigned long start = ULONG_MAX, end = 0;
2180 _vm_unmap_aliases(start, end, flush);
2182 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
2185 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
2186 * @mem: the pointer returned by vm_map_ram
2187 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
2189 void vm_unmap_ram(const void *mem, unsigned int count)
2191 unsigned long size = (unsigned long)count << PAGE_SHIFT;
2192 unsigned long addr = (unsigned long)kasan_reset_tag(mem);
2193 struct vmap_area *va;
2197 BUG_ON(addr < VMALLOC_START);
2198 BUG_ON(addr > VMALLOC_END);
2199 BUG_ON(!PAGE_ALIGNED(addr));
2201 kasan_poison_vmalloc(mem, size);
2203 if (likely(count <= VMAP_MAX_ALLOC)) {
2204 debug_check_no_locks_freed(mem, size);
2205 vb_free(addr, size);
2209 va = find_vmap_area(addr);
2211 debug_check_no_locks_freed((void *)va->va_start,
2212 (va->va_end - va->va_start));
2213 free_unmap_vmap_area(va);
2215 EXPORT_SYMBOL(vm_unmap_ram);
2218 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
2219 * @pages: an array of pointers to the pages to be mapped
2220 * @count: number of pages
2221 * @node: prefer to allocate data structures on this node
2223 * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
2224 * faster than vmap so it's good. But if you mix long-life and short-life
2225 * objects with vm_map_ram(), it could consume lots of address space through
2226 * fragmentation (especially on a 32bit machine). You could see failures in
2227 * the end. Please use this function for short-lived objects.
2229 * Returns: a pointer to the address that has been mapped, or %NULL on failure
2231 void *vm_map_ram(struct page **pages, unsigned int count, int node)
2233 unsigned long size = (unsigned long)count << PAGE_SHIFT;
2237 if (likely(count <= VMAP_MAX_ALLOC)) {
2238 mem = vb_alloc(size, GFP_KERNEL);
2241 addr = (unsigned long)mem;
2243 struct vmap_area *va;
2244 va = alloc_vmap_area(size, PAGE_SIZE,
2245 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
2249 addr = va->va_start;
2253 if (vmap_pages_range(addr, addr + size, PAGE_KERNEL,
2254 pages, PAGE_SHIFT) < 0) {
2255 vm_unmap_ram(mem, count);
2260 * Mark the pages as accessible, now that they are mapped.
2261 * With hardware tag-based KASAN, marking is skipped for
2262 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
2264 mem = kasan_unpoison_vmalloc(mem, size, KASAN_VMALLOC_PROT_NORMAL);
2268 EXPORT_SYMBOL(vm_map_ram);
2270 static struct vm_struct *vmlist __initdata;
2272 static inline unsigned int vm_area_page_order(struct vm_struct *vm)
2274 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
2275 return vm->page_order;
2281 static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int order)
2283 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
2284 vm->page_order = order;
2291 * vm_area_add_early - add vmap area early during boot
2292 * @vm: vm_struct to add
2294 * This function is used to add fixed kernel vm area to vmlist before
2295 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
2296 * should contain proper values and the other fields should be zero.
2298 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
2300 void __init vm_area_add_early(struct vm_struct *vm)
2302 struct vm_struct *tmp, **p;
2304 BUG_ON(vmap_initialized);
2305 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
2306 if (tmp->addr >= vm->addr) {
2307 BUG_ON(tmp->addr < vm->addr + vm->size);
2310 BUG_ON(tmp->addr + tmp->size > vm->addr);
2317 * vm_area_register_early - register vmap area early during boot
2318 * @vm: vm_struct to register
2319 * @align: requested alignment
2321 * This function is used to register kernel vm area before
2322 * vmalloc_init() is called. @vm->size and @vm->flags should contain
2323 * proper values on entry and other fields should be zero. On return,
2324 * vm->addr contains the allocated address.
2326 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
2328 void __init vm_area_register_early(struct vm_struct *vm, size_t align)
2330 unsigned long addr = ALIGN(VMALLOC_START, align);
2331 struct vm_struct *cur, **p;
2333 BUG_ON(vmap_initialized);
2335 for (p = &vmlist; (cur = *p) != NULL; p = &cur->next) {
2336 if ((unsigned long)cur->addr - addr >= vm->size)
2338 addr = ALIGN((unsigned long)cur->addr + cur->size, align);
2341 BUG_ON(addr > VMALLOC_END - vm->size);
2342 vm->addr = (void *)addr;
2345 kasan_populate_early_vm_area_shadow(vm->addr, vm->size);
2348 static void vmap_init_free_space(void)
2350 unsigned long vmap_start = 1;
2351 const unsigned long vmap_end = ULONG_MAX;
2352 struct vmap_area *busy, *free;
2356 * -|-----|.....|-----|-----|-----|.....|-
2358 * |<--------------------------------->|
2360 list_for_each_entry(busy, &vmap_area_list, list) {
2361 if (busy->va_start - vmap_start > 0) {
2362 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
2363 if (!WARN_ON_ONCE(!free)) {
2364 free->va_start = vmap_start;
2365 free->va_end = busy->va_start;
2367 insert_vmap_area_augment(free, NULL,
2368 &free_vmap_area_root,
2369 &free_vmap_area_list);
2373 vmap_start = busy->va_end;
2376 if (vmap_end - vmap_start > 0) {
2377 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
2378 if (!WARN_ON_ONCE(!free)) {
2379 free->va_start = vmap_start;
2380 free->va_end = vmap_end;
2382 insert_vmap_area_augment(free, NULL,
2383 &free_vmap_area_root,
2384 &free_vmap_area_list);
2389 void __init vmalloc_init(void)
2391 struct vmap_area *va;
2392 struct vm_struct *tmp;
2396 * Create the cache for vmap_area objects.
2398 vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
2400 for_each_possible_cpu(i) {
2401 struct vmap_block_queue *vbq;
2402 struct vfree_deferred *p;
2404 vbq = &per_cpu(vmap_block_queue, i);
2405 spin_lock_init(&vbq->lock);
2406 INIT_LIST_HEAD(&vbq->free);
2407 p = &per_cpu(vfree_deferred, i);
2408 init_llist_head(&p->list);
2409 INIT_WORK(&p->wq, free_work);
2412 /* Import existing vmlist entries. */
2413 for (tmp = vmlist; tmp; tmp = tmp->next) {
2414 va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
2415 if (WARN_ON_ONCE(!va))
2418 va->va_start = (unsigned long)tmp->addr;
2419 va->va_end = va->va_start + tmp->size;
2421 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
2425 * Now we can initialize a free vmap space.
2427 vmap_init_free_space();
2428 vmap_initialized = true;
2431 static inline void setup_vmalloc_vm_locked(struct vm_struct *vm,
2432 struct vmap_area *va, unsigned long flags, const void *caller)
2435 vm->addr = (void *)va->va_start;
2436 vm->size = va->va_end - va->va_start;
2437 vm->caller = caller;
2441 static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
2442 unsigned long flags, const void *caller)
2444 spin_lock(&vmap_area_lock);
2445 setup_vmalloc_vm_locked(vm, va, flags, caller);
2446 spin_unlock(&vmap_area_lock);
2449 static void clear_vm_uninitialized_flag(struct vm_struct *vm)
2452 * Before removing VM_UNINITIALIZED,
2453 * we should make sure that vm has proper values.
2454 * Pair with smp_rmb() in show_numa_info().
2457 vm->flags &= ~VM_UNINITIALIZED;
2460 static struct vm_struct *__get_vm_area_node(unsigned long size,
2461 unsigned long align, unsigned long shift, unsigned long flags,
2462 unsigned long start, unsigned long end, int node,
2463 gfp_t gfp_mask, const void *caller)
2465 struct vmap_area *va;
2466 struct vm_struct *area;
2467 unsigned long requested_size = size;
2469 BUG_ON(in_interrupt());
2470 size = ALIGN(size, 1ul << shift);
2471 if (unlikely(!size))
2474 if (flags & VM_IOREMAP)
2475 align = 1ul << clamp_t(int, get_count_order_long(size),
2476 PAGE_SHIFT, IOREMAP_MAX_ORDER);
2478 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
2479 if (unlikely(!area))
2482 if (!(flags & VM_NO_GUARD))
2485 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
2491 setup_vmalloc_vm(area, va, flags, caller);
2494 * Mark pages for non-VM_ALLOC mappings as accessible. Do it now as a
2495 * best-effort approach, as they can be mapped outside of vmalloc code.
2496 * For VM_ALLOC mappings, the pages are marked as accessible after
2497 * getting mapped in __vmalloc_node_range().
2498 * With hardware tag-based KASAN, marking is skipped for
2499 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
2501 if (!(flags & VM_ALLOC))
2502 area->addr = kasan_unpoison_vmalloc(area->addr, requested_size,
2503 KASAN_VMALLOC_PROT_NORMAL);
2508 struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
2509 unsigned long start, unsigned long end,
2512 return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, start, end,
2513 NUMA_NO_NODE, GFP_KERNEL, caller);
2517 * get_vm_area - reserve a contiguous kernel virtual area
2518 * @size: size of the area
2519 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
2521 * Search an area of @size in the kernel virtual mapping area,
2522 * and reserved it for out purposes. Returns the area descriptor
2523 * on success or %NULL on failure.
2525 * Return: the area descriptor on success or %NULL on failure.
2527 struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
2529 return __get_vm_area_node(size, 1, PAGE_SHIFT, flags,
2530 VMALLOC_START, VMALLOC_END,
2531 NUMA_NO_NODE, GFP_KERNEL,
2532 __builtin_return_address(0));
2535 struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
2538 return __get_vm_area_node(size, 1, PAGE_SHIFT, flags,
2539 VMALLOC_START, VMALLOC_END,
2540 NUMA_NO_NODE, GFP_KERNEL, caller);
2544 * find_vm_area - find a continuous kernel virtual area
2545 * @addr: base address
2547 * Search for the kernel VM area starting at @addr, and return it.
2548 * It is up to the caller to do all required locking to keep the returned
2551 * Return: the area descriptor on success or %NULL on failure.
2553 struct vm_struct *find_vm_area(const void *addr)
2555 struct vmap_area *va;
2557 va = find_vmap_area((unsigned long)addr);
2565 * remove_vm_area - find and remove a continuous kernel virtual area
2566 * @addr: base address
2568 * Search for the kernel VM area starting at @addr, and remove it.
2569 * This function returns the found VM area, but using it is NOT safe
2570 * on SMP machines, except for its size or flags.
2572 * Return: the area descriptor on success or %NULL on failure.
2574 struct vm_struct *remove_vm_area(const void *addr)
2576 struct vmap_area *va;
2580 spin_lock(&vmap_area_lock);
2581 va = __find_vmap_area((unsigned long)addr, &vmap_area_root);
2583 struct vm_struct *vm = va->vm;
2586 spin_unlock(&vmap_area_lock);
2588 kasan_free_module_shadow(vm);
2589 free_unmap_vmap_area(va);
2594 spin_unlock(&vmap_area_lock);
2598 static inline void set_area_direct_map(const struct vm_struct *area,
2599 int (*set_direct_map)(struct page *page))
2603 /* HUGE_VMALLOC passes small pages to set_direct_map */
2604 for (i = 0; i < area->nr_pages; i++)
2605 if (page_address(area->pages[i]))
2606 set_direct_map(area->pages[i]);
2609 /* Handle removing and resetting vm mappings related to the vm_struct. */
2610 static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages)
2612 unsigned long start = ULONG_MAX, end = 0;
2613 unsigned int page_order = vm_area_page_order(area);
2614 int flush_reset = area->flags & VM_FLUSH_RESET_PERMS;
2618 remove_vm_area(area->addr);
2620 /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */
2625 * If not deallocating pages, just do the flush of the VM area and
2628 if (!deallocate_pages) {
2634 * If execution gets here, flush the vm mapping and reset the direct
2635 * map. Find the start and end range of the direct mappings to make sure
2636 * the vm_unmap_aliases() flush includes the direct map.
2638 for (i = 0; i < area->nr_pages; i += 1U << page_order) {
2639 unsigned long addr = (unsigned long)page_address(area->pages[i]);
2641 unsigned long page_size;
2643 page_size = PAGE_SIZE << page_order;
2644 start = min(addr, start);
2645 end = max(addr + page_size, end);
2651 * Set direct map to something invalid so that it won't be cached if
2652 * there are any accesses after the TLB flush, then flush the TLB and
2653 * reset the direct map permissions to the default.
2655 set_area_direct_map(area, set_direct_map_invalid_noflush);
2656 _vm_unmap_aliases(start, end, flush_dmap);
2657 set_area_direct_map(area, set_direct_map_default_noflush);
2660 static void __vunmap(const void *addr, int deallocate_pages)
2662 struct vm_struct *area;
2667 if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
2671 area = find_vm_area(addr);
2672 if (unlikely(!area)) {
2673 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
2678 debug_check_no_locks_freed(area->addr, get_vm_area_size(area));
2679 debug_check_no_obj_freed(area->addr, get_vm_area_size(area));
2681 kasan_poison_vmalloc(area->addr, get_vm_area_size(area));
2683 vm_remove_mappings(area, deallocate_pages);
2685 if (deallocate_pages) {
2688 for (i = 0; i < area->nr_pages; i++) {
2689 struct page *page = area->pages[i];
2692 mod_memcg_page_state(page, MEMCG_VMALLOC, -1);
2694 * High-order allocs for huge vmallocs are split, so
2695 * can be freed as an array of order-0 allocations
2697 __free_pages(page, 0);
2700 atomic_long_sub(area->nr_pages, &nr_vmalloc_pages);
2702 kvfree(area->pages);
2708 static inline void __vfree_deferred(const void *addr)
2711 * Use raw_cpu_ptr() because this can be called from preemptible
2712 * context. Preemption is absolutely fine here, because the llist_add()
2713 * implementation is lockless, so it works even if we are adding to
2714 * another cpu's list. schedule_work() should be fine with this too.
2716 struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
2718 if (llist_add((struct llist_node *)addr, &p->list))
2719 schedule_work(&p->wq);
2723 * vfree_atomic - release memory allocated by vmalloc()
2724 * @addr: memory base address
2726 * This one is just like vfree() but can be called in any atomic context
2729 void vfree_atomic(const void *addr)
2733 kmemleak_free(addr);
2737 __vfree_deferred(addr);
2740 static void __vfree(const void *addr)
2742 if (unlikely(in_interrupt()))
2743 __vfree_deferred(addr);
2749 * vfree - Release memory allocated by vmalloc()
2750 * @addr: Memory base address
2752 * Free the virtually continuous memory area starting at @addr, as obtained
2753 * from one of the vmalloc() family of APIs. This will usually also free the
2754 * physical memory underlying the virtual allocation, but that memory is
2755 * reference counted, so it will not be freed until the last user goes away.
2757 * If @addr is NULL, no operation is performed.
2760 * May sleep if called *not* from interrupt context.
2761 * Must not be called in NMI context (strictly speaking, it could be
2762 * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
2763 * conventions for vfree() arch-dependent would be a really bad idea).
2765 void vfree(const void *addr)
2769 kmemleak_free(addr);
2771 might_sleep_if(!in_interrupt());
2778 EXPORT_SYMBOL(vfree);
2781 * vunmap - release virtual mapping obtained by vmap()
2782 * @addr: memory base address
2784 * Free the virtually contiguous memory area starting at @addr,
2785 * which was created from the page array passed to vmap().
2787 * Must not be called in interrupt context.
2789 void vunmap(const void *addr)
2791 BUG_ON(in_interrupt());
2796 EXPORT_SYMBOL(vunmap);
2799 * vmap - map an array of pages into virtually contiguous space
2800 * @pages: array of page pointers
2801 * @count: number of pages to map
2802 * @flags: vm_area->flags
2803 * @prot: page protection for the mapping
2805 * Maps @count pages from @pages into contiguous kernel virtual space.
2806 * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself
2807 * (which must be kmalloc or vmalloc memory) and one reference per pages in it
2808 * are transferred from the caller to vmap(), and will be freed / dropped when
2809 * vfree() is called on the return value.
2811 * Return: the address of the area or %NULL on failure
2813 void *vmap(struct page **pages, unsigned int count,
2814 unsigned long flags, pgprot_t prot)
2816 struct vm_struct *area;
2818 unsigned long size; /* In bytes */
2823 * Your top guard is someone else's bottom guard. Not having a top
2824 * guard compromises someone else's mappings too.
2826 if (WARN_ON_ONCE(flags & VM_NO_GUARD))
2827 flags &= ~VM_NO_GUARD;
2829 if (count > totalram_pages())
2832 size = (unsigned long)count << PAGE_SHIFT;
2833 area = get_vm_area_caller(size, flags, __builtin_return_address(0));
2837 addr = (unsigned long)area->addr;
2838 if (vmap_pages_range(addr, addr + size, pgprot_nx(prot),
2839 pages, PAGE_SHIFT) < 0) {
2844 if (flags & VM_MAP_PUT_PAGES) {
2845 area->pages = pages;
2846 area->nr_pages = count;
2850 EXPORT_SYMBOL(vmap);
2852 #ifdef CONFIG_VMAP_PFN
2853 struct vmap_pfn_data {
2854 unsigned long *pfns;
2859 static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private)
2861 struct vmap_pfn_data *data = private;
2863 if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx])))
2865 *pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot));
2870 * vmap_pfn - map an array of PFNs into virtually contiguous space
2871 * @pfns: array of PFNs
2872 * @count: number of pages to map
2873 * @prot: page protection for the mapping
2875 * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns
2876 * the start address of the mapping.
2878 void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
2880 struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) };
2881 struct vm_struct *area;
2883 area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP,
2884 __builtin_return_address(0));
2887 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
2888 count * PAGE_SIZE, vmap_pfn_apply, &data)) {
2894 EXPORT_SYMBOL_GPL(vmap_pfn);
2895 #endif /* CONFIG_VMAP_PFN */
2897 static inline unsigned int
2898 vm_area_alloc_pages(gfp_t gfp, int nid,
2899 unsigned int order, unsigned int nr_pages, struct page **pages)
2901 unsigned int nr_allocated = 0;
2906 * For order-0 pages we make use of bulk allocator, if
2907 * the page array is partly or not at all populated due
2908 * to fails, fallback to a single page allocator that is
2912 gfp_t bulk_gfp = gfp & ~__GFP_NOFAIL;
2914 while (nr_allocated < nr_pages) {
2915 unsigned int nr, nr_pages_request;
2918 * A maximum allowed request is hard-coded and is 100
2919 * pages per call. That is done in order to prevent a
2920 * long preemption off scenario in the bulk-allocator
2921 * so the range is [1:100].
2923 nr_pages_request = min(100U, nr_pages - nr_allocated);
2925 /* memory allocation should consider mempolicy, we can't
2926 * wrongly use nearest node when nid == NUMA_NO_NODE,
2927 * otherwise memory may be allocated in only one node,
2928 * but mempolicy wants to alloc memory by interleaving.
2930 if (IS_ENABLED(CONFIG_NUMA) && nid == NUMA_NO_NODE)
2931 nr = alloc_pages_bulk_array_mempolicy(bulk_gfp,
2933 pages + nr_allocated);
2936 nr = alloc_pages_bulk_array_node(bulk_gfp, nid,
2938 pages + nr_allocated);
2944 * If zero or pages were obtained partly,
2945 * fallback to a single page allocator.
2947 if (nr != nr_pages_request)
2952 /* High-order pages or fallback path if "bulk" fails. */
2954 while (nr_allocated < nr_pages) {
2955 if (fatal_signal_pending(current))
2958 if (nid == NUMA_NO_NODE)
2959 page = alloc_pages(gfp, order);
2961 page = alloc_pages_node(nid, gfp, order);
2962 if (unlikely(!page))
2965 * Higher order allocations must be able to be treated as
2966 * indepdenent small pages by callers (as they can with
2967 * small-page vmallocs). Some drivers do their own refcounting
2968 * on vmalloc_to_page() pages, some use page->mapping,
2972 split_page(page, order);
2975 * Careful, we allocate and map page-order pages, but
2976 * tracking is done per PAGE_SIZE page so as to keep the
2977 * vm_struct APIs independent of the physical/mapped size.
2979 for (i = 0; i < (1U << order); i++)
2980 pages[nr_allocated + i] = page + i;
2983 nr_allocated += 1U << order;
2986 return nr_allocated;
2989 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
2990 pgprot_t prot, unsigned int page_shift,
2993 const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
2994 bool nofail = gfp_mask & __GFP_NOFAIL;
2995 unsigned long addr = (unsigned long)area->addr;
2996 unsigned long size = get_vm_area_size(area);
2997 unsigned long array_size;
2998 unsigned int nr_small_pages = size >> PAGE_SHIFT;
2999 unsigned int page_order;
3003 array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
3004 gfp_mask |= __GFP_NOWARN;
3005 if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
3006 gfp_mask |= __GFP_HIGHMEM;
3008 /* Please note that the recursion is strictly bounded. */
3009 if (array_size > PAGE_SIZE) {
3010 area->pages = __vmalloc_node(array_size, 1, nested_gfp, node,
3013 area->pages = kmalloc_node(array_size, nested_gfp, node);
3017 warn_alloc(gfp_mask, NULL,
3018 "vmalloc error: size %lu, failed to allocated page array size %lu",
3019 nr_small_pages * PAGE_SIZE, array_size);
3024 set_vm_area_page_order(area, page_shift - PAGE_SHIFT);
3025 page_order = vm_area_page_order(area);
3027 area->nr_pages = vm_area_alloc_pages(gfp_mask | __GFP_NOWARN,
3028 node, page_order, nr_small_pages, area->pages);
3030 atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
3031 if (gfp_mask & __GFP_ACCOUNT) {
3034 for (i = 0; i < area->nr_pages; i++)
3035 mod_memcg_page_state(area->pages[i], MEMCG_VMALLOC, 1);
3039 * If not enough pages were obtained to accomplish an
3040 * allocation request, free them via __vfree() if any.
3042 if (area->nr_pages != nr_small_pages) {
3043 warn_alloc(gfp_mask, NULL,
3044 "vmalloc error: size %lu, page order %u, failed to allocate pages",
3045 area->nr_pages * PAGE_SIZE, page_order);
3050 * page tables allocations ignore external gfp mask, enforce it
3053 if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
3054 flags = memalloc_nofs_save();
3055 else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
3056 flags = memalloc_noio_save();
3059 ret = vmap_pages_range(addr, addr + size, prot, area->pages,
3061 if (nofail && (ret < 0))
3062 schedule_timeout_uninterruptible(1);
3063 } while (nofail && (ret < 0));
3065 if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
3066 memalloc_nofs_restore(flags);
3067 else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
3068 memalloc_noio_restore(flags);
3071 warn_alloc(gfp_mask, NULL,
3072 "vmalloc error: size %lu, failed to map pages",
3073 area->nr_pages * PAGE_SIZE);
3080 __vfree(area->addr);
3085 * __vmalloc_node_range - allocate virtually contiguous memory
3086 * @size: allocation size
3087 * @align: desired alignment
3088 * @start: vm area range start
3089 * @end: vm area range end
3090 * @gfp_mask: flags for the page level allocator
3091 * @prot: protection mask for the allocated pages
3092 * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD)
3093 * @node: node to use for allocation or NUMA_NO_NODE
3094 * @caller: caller's return address
3096 * Allocate enough pages to cover @size from the page level
3097 * allocator with @gfp_mask flags. Please note that the full set of gfp
3098 * flags are not supported. GFP_KERNEL, GFP_NOFS and GFP_NOIO are all
3100 * Zone modifiers are not supported. From the reclaim modifiers
3101 * __GFP_DIRECT_RECLAIM is required (aka GFP_NOWAIT is not supported)
3102 * and only __GFP_NOFAIL is supported (i.e. __GFP_NORETRY and
3103 * __GFP_RETRY_MAYFAIL are not supported).
3105 * __GFP_NOWARN can be used to suppress failures messages.
3107 * Map them into contiguous kernel virtual space, using a pagetable
3108 * protection of @prot.
3110 * Return: the address of the area or %NULL on failure
3112 void *__vmalloc_node_range(unsigned long size, unsigned long align,
3113 unsigned long start, unsigned long end, gfp_t gfp_mask,
3114 pgprot_t prot, unsigned long vm_flags, int node,
3117 struct vm_struct *area;
3119 kasan_vmalloc_flags_t kasan_flags = KASAN_VMALLOC_NONE;
3120 unsigned long real_size = size;
3121 unsigned long real_align = align;
3122 unsigned int shift = PAGE_SHIFT;
3124 if (WARN_ON_ONCE(!size))
3127 if ((size >> PAGE_SHIFT) > totalram_pages()) {
3128 warn_alloc(gfp_mask, NULL,
3129 "vmalloc error: size %lu, exceeds total pages",
3134 if (vmap_allow_huge && (vm_flags & VM_ALLOW_HUGE_VMAP)) {
3135 unsigned long size_per_node;
3138 * Try huge pages. Only try for PAGE_KERNEL allocations,
3139 * others like modules don't yet expect huge pages in
3140 * their allocations due to apply_to_page_range not
3144 size_per_node = size;
3145 if (node == NUMA_NO_NODE)
3146 size_per_node /= num_online_nodes();
3147 if (arch_vmap_pmd_supported(prot) && size_per_node >= PMD_SIZE)
3150 shift = arch_vmap_pte_supported_shift(size_per_node);
3152 align = max(real_align, 1UL << shift);
3153 size = ALIGN(real_size, 1UL << shift);
3157 area = __get_vm_area_node(real_size, align, shift, VM_ALLOC |
3158 VM_UNINITIALIZED | vm_flags, start, end, node,
3161 bool nofail = gfp_mask & __GFP_NOFAIL;
3162 warn_alloc(gfp_mask, NULL,
3163 "vmalloc error: size %lu, vm_struct allocation failed%s",
3164 real_size, (nofail) ? ". Retrying." : "");
3166 schedule_timeout_uninterruptible(1);
3173 * Prepare arguments for __vmalloc_area_node() and
3174 * kasan_unpoison_vmalloc().
3176 if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) {
3177 if (kasan_hw_tags_enabled()) {
3179 * Modify protection bits to allow tagging.
3180 * This must be done before mapping.
3182 prot = arch_vmap_pgprot_tagged(prot);
3185 * Skip page_alloc poisoning and zeroing for physical
3186 * pages backing VM_ALLOC mapping. Memory is instead
3187 * poisoned and zeroed by kasan_unpoison_vmalloc().
3189 gfp_mask |= __GFP_SKIP_KASAN_UNPOISON | __GFP_SKIP_ZERO;
3192 /* Take note that the mapping is PAGE_KERNEL. */
3193 kasan_flags |= KASAN_VMALLOC_PROT_NORMAL;
3196 /* Allocate physical pages and map them into vmalloc space. */
3197 ret = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
3202 * Mark the pages as accessible, now that they are mapped.
3203 * The condition for setting KASAN_VMALLOC_INIT should complement the
3204 * one in post_alloc_hook() with regards to the __GFP_SKIP_ZERO check
3205 * to make sure that memory is initialized under the same conditions.
3206 * Tag-based KASAN modes only assign tags to normal non-executable
3207 * allocations, see __kasan_unpoison_vmalloc().
3209 kasan_flags |= KASAN_VMALLOC_VM_ALLOC;
3210 if (!want_init_on_free() && want_init_on_alloc(gfp_mask) &&
3211 (gfp_mask & __GFP_SKIP_ZERO))
3212 kasan_flags |= KASAN_VMALLOC_INIT;
3213 /* KASAN_VMALLOC_PROT_NORMAL already set if required. */
3214 area->addr = kasan_unpoison_vmalloc(area->addr, real_size, kasan_flags);
3217 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
3218 * flag. It means that vm_struct is not fully initialized.
3219 * Now, it is fully initialized, so remove this flag here.
3221 clear_vm_uninitialized_flag(area);
3223 size = PAGE_ALIGN(size);
3224 if (!(vm_flags & VM_DEFER_KMEMLEAK))
3225 kmemleak_vmalloc(area, size, gfp_mask);
3230 if (shift > PAGE_SHIFT) {
3241 * __vmalloc_node - allocate virtually contiguous memory
3242 * @size: allocation size
3243 * @align: desired alignment
3244 * @gfp_mask: flags for the page level allocator
3245 * @node: node to use for allocation or NUMA_NO_NODE
3246 * @caller: caller's return address
3248 * Allocate enough pages to cover @size from the page level allocator with
3249 * @gfp_mask flags. Map them into contiguous kernel virtual space.
3251 * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
3252 * and __GFP_NOFAIL are not supported
3254 * Any use of gfp flags outside of GFP_KERNEL should be consulted
3257 * Return: pointer to the allocated memory or %NULL on error
3259 void *__vmalloc_node(unsigned long size, unsigned long align,
3260 gfp_t gfp_mask, int node, const void *caller)
3262 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
3263 gfp_mask, PAGE_KERNEL, 0, node, caller);
3266 * This is only for performance analysis of vmalloc and stress purpose.
3267 * It is required by vmalloc test module, therefore do not use it other
3270 #ifdef CONFIG_TEST_VMALLOC_MODULE
3271 EXPORT_SYMBOL_GPL(__vmalloc_node);
3274 void *__vmalloc(unsigned long size, gfp_t gfp_mask)
3276 return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE,
3277 __builtin_return_address(0));
3279 EXPORT_SYMBOL(__vmalloc);
3282 * vmalloc - allocate virtually contiguous memory
3283 * @size: allocation size
3285 * Allocate enough pages to cover @size from the page level
3286 * allocator and map them into contiguous kernel virtual space.
3288 * For tight control over page level allocator and protection flags
3289 * use __vmalloc() instead.
3291 * Return: pointer to the allocated memory or %NULL on error
3293 void *vmalloc(unsigned long size)
3295 return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE,
3296 __builtin_return_address(0));
3298 EXPORT_SYMBOL(vmalloc);
3301 * vmalloc_huge - allocate virtually contiguous memory, allow huge pages
3302 * @size: allocation size
3303 * @gfp_mask: flags for the page level allocator
3305 * Allocate enough pages to cover @size from the page level
3306 * allocator and map them into contiguous kernel virtual space.
3307 * If @size is greater than or equal to PMD_SIZE, allow using
3308 * huge pages for the memory
3310 * Return: pointer to the allocated memory or %NULL on error
3312 void *vmalloc_huge(unsigned long size, gfp_t gfp_mask)
3314 return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
3315 gfp_mask, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP,
3316 NUMA_NO_NODE, __builtin_return_address(0));
3318 EXPORT_SYMBOL_GPL(vmalloc_huge);
3321 * vzalloc - allocate virtually contiguous memory with zero fill
3322 * @size: allocation size
3324 * Allocate enough pages to cover @size from the page level
3325 * allocator and map them into contiguous kernel virtual space.
3326 * The memory allocated is set to zero.
3328 * For tight control over page level allocator and protection flags
3329 * use __vmalloc() instead.
3331 * Return: pointer to the allocated memory or %NULL on error
3333 void *vzalloc(unsigned long size)
3335 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE,
3336 __builtin_return_address(0));
3338 EXPORT_SYMBOL(vzalloc);
3341 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
3342 * @size: allocation size
3344 * The resulting memory area is zeroed so it can be mapped to userspace
3345 * without leaking data.
3347 * Return: pointer to the allocated memory or %NULL on error
3349 void *vmalloc_user(unsigned long size)
3351 return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END,
3352 GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL,
3353 VM_USERMAP, NUMA_NO_NODE,
3354 __builtin_return_address(0));
3356 EXPORT_SYMBOL(vmalloc_user);
3359 * vmalloc_node - allocate memory on a specific node
3360 * @size: allocation size
3363 * Allocate enough pages to cover @size from the page level
3364 * allocator and map them into contiguous kernel virtual space.
3366 * For tight control over page level allocator and protection flags
3367 * use __vmalloc() instead.
3369 * Return: pointer to the allocated memory or %NULL on error
3371 void *vmalloc_node(unsigned long size, int node)
3373 return __vmalloc_node(size, 1, GFP_KERNEL, node,
3374 __builtin_return_address(0));
3376 EXPORT_SYMBOL(vmalloc_node);
3379 * vzalloc_node - allocate memory on a specific node with zero fill
3380 * @size: allocation size
3383 * Allocate enough pages to cover @size from the page level
3384 * allocator and map them into contiguous kernel virtual space.
3385 * The memory allocated is set to zero.
3387 * Return: pointer to the allocated memory or %NULL on error
3389 void *vzalloc_node(unsigned long size, int node)
3391 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node,
3392 __builtin_return_address(0));
3394 EXPORT_SYMBOL(vzalloc_node);
3396 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
3397 #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
3398 #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
3399 #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
3402 * 64b systems should always have either DMA or DMA32 zones. For others
3403 * GFP_DMA32 should do the right thing and use the normal zone.
3405 #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
3409 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
3410 * @size: allocation size
3412 * Allocate enough 32bit PA addressable pages to cover @size from the
3413 * page level allocator and map them into contiguous kernel virtual space.
3415 * Return: pointer to the allocated memory or %NULL on error
3417 void *vmalloc_32(unsigned long size)
3419 return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE,
3420 __builtin_return_address(0));
3422 EXPORT_SYMBOL(vmalloc_32);
3425 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
3426 * @size: allocation size
3428 * The resulting memory area is 32bit addressable and zeroed so it can be
3429 * mapped to userspace without leaking data.
3431 * Return: pointer to the allocated memory or %NULL on error
3433 void *vmalloc_32_user(unsigned long size)
3435 return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END,
3436 GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
3437 VM_USERMAP, NUMA_NO_NODE,
3438 __builtin_return_address(0));
3440 EXPORT_SYMBOL(vmalloc_32_user);
3443 * small helper routine , copy contents to buf from addr.
3444 * If the page is not present, fill zero.
3447 static int aligned_vread(char *buf, char *addr, unsigned long count)
3453 unsigned long offset, length;
3455 offset = offset_in_page(addr);
3456 length = PAGE_SIZE - offset;
3459 p = vmalloc_to_page(addr);
3461 * To do safe access to this _mapped_ area, we need
3462 * lock. But adding lock here means that we need to add
3463 * overhead of vmalloc()/vfree() calls for this _debug_
3464 * interface, rarely used. Instead of that, we'll use
3465 * kmap() and get small overhead in this access function.
3468 /* We can expect USER0 is not used -- see vread() */
3469 void *map = kmap_atomic(p);
3470 memcpy(buf, map + offset, length);
3473 memset(buf, 0, length);
3484 * vread() - read vmalloc area in a safe way.
3485 * @buf: buffer for reading data
3486 * @addr: vm address.
3487 * @count: number of bytes to be read.
3489 * This function checks that addr is a valid vmalloc'ed area, and
3490 * copy data from that area to a given buffer. If the given memory range
3491 * of [addr...addr+count) includes some valid address, data is copied to
3492 * proper area of @buf. If there are memory holes, they'll be zero-filled.
3493 * IOREMAP area is treated as memory hole and no copy is done.
3495 * If [addr...addr+count) doesn't includes any intersects with alive
3496 * vm_struct area, returns 0. @buf should be kernel's buffer.
3498 * Note: In usual ops, vread() is never necessary because the caller
3499 * should know vmalloc() area is valid and can use memcpy().
3500 * This is for routines which have to access vmalloc area without
3501 * any information, as /proc/kcore.
3503 * Return: number of bytes for which addr and buf should be increased
3504 * (same number as @count) or %0 if [addr...addr+count) doesn't
3505 * include any intersection with valid vmalloc area
3507 long vread(char *buf, char *addr, unsigned long count)
3509 struct vmap_area *va;
3510 struct vm_struct *vm;
3511 char *vaddr, *buf_start = buf;
3512 unsigned long buflen = count;
3515 addr = kasan_reset_tag(addr);
3517 /* Don't allow overflow */
3518 if ((unsigned long) addr + count < count)
3519 count = -(unsigned long) addr;
3521 spin_lock(&vmap_area_lock);
3522 va = find_vmap_area_exceed_addr((unsigned long)addr);
3526 /* no intersects with alive vmap_area */
3527 if ((unsigned long)addr + count <= va->va_start)
3530 list_for_each_entry_from(va, &vmap_area_list, list) {
3538 vaddr = (char *) vm->addr;
3539 if (addr >= vaddr + get_vm_area_size(vm))
3541 while (addr < vaddr) {
3549 n = vaddr + get_vm_area_size(vm) - addr;
3552 if (!(vm->flags & VM_IOREMAP))
3553 aligned_vread(buf, addr, n);
3554 else /* IOREMAP area is treated as memory hole */
3561 spin_unlock(&vmap_area_lock);
3563 if (buf == buf_start)
3565 /* zero-fill memory holes */
3566 if (buf != buf_start + buflen)
3567 memset(buf, 0, buflen - (buf - buf_start));
3573 * remap_vmalloc_range_partial - map vmalloc pages to userspace
3574 * @vma: vma to cover
3575 * @uaddr: target user address to start at
3576 * @kaddr: virtual address of vmalloc kernel memory
3577 * @pgoff: offset from @kaddr to start at
3578 * @size: size of map area
3580 * Returns: 0 for success, -Exxx on failure
3582 * This function checks that @kaddr is a valid vmalloc'ed area,
3583 * and that it is big enough to cover the range starting at
3584 * @uaddr in @vma. Will return failure if that criteria isn't
3587 * Similar to remap_pfn_range() (see mm/memory.c)
3589 int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
3590 void *kaddr, unsigned long pgoff,
3593 struct vm_struct *area;
3595 unsigned long end_index;
3597 if (check_shl_overflow(pgoff, PAGE_SHIFT, &off))
3600 size = PAGE_ALIGN(size);
3602 if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
3605 area = find_vm_area(kaddr);
3609 if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT)))
3612 if (check_add_overflow(size, off, &end_index) ||
3613 end_index > get_vm_area_size(area))
3618 struct page *page = vmalloc_to_page(kaddr);
3621 ret = vm_insert_page(vma, uaddr, page);
3630 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3636 * remap_vmalloc_range - map vmalloc pages to userspace
3637 * @vma: vma to cover (map full range of vma)
3638 * @addr: vmalloc memory
3639 * @pgoff: number of pages into addr before first page to map
3641 * Returns: 0 for success, -Exxx on failure
3643 * This function checks that addr is a valid vmalloc'ed area, and
3644 * that it is big enough to cover the vma. Will return failure if
3645 * that criteria isn't met.
3647 * Similar to remap_pfn_range() (see mm/memory.c)
3649 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
3650 unsigned long pgoff)
3652 return remap_vmalloc_range_partial(vma, vma->vm_start,
3654 vma->vm_end - vma->vm_start);
3656 EXPORT_SYMBOL(remap_vmalloc_range);
3658 void free_vm_area(struct vm_struct *area)
3660 struct vm_struct *ret;
3661 ret = remove_vm_area(area->addr);
3662 BUG_ON(ret != area);
3665 EXPORT_SYMBOL_GPL(free_vm_area);
3668 static struct vmap_area *node_to_va(struct rb_node *n)
3670 return rb_entry_safe(n, struct vmap_area, rb_node);
3674 * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
3675 * @addr: target address
3677 * Returns: vmap_area if it is found. If there is no such area
3678 * the first highest(reverse order) vmap_area is returned
3679 * i.e. va->va_start < addr && va->va_end < addr or NULL
3680 * if there are no any areas before @addr.
3682 static struct vmap_area *
3683 pvm_find_va_enclose_addr(unsigned long addr)
3685 struct vmap_area *va, *tmp;
3688 n = free_vmap_area_root.rb_node;
3692 tmp = rb_entry(n, struct vmap_area, rb_node);
3693 if (tmp->va_start <= addr) {
3695 if (tmp->va_end >= addr)
3708 * pvm_determine_end_from_reverse - find the highest aligned address
3709 * of free block below VMALLOC_END
3711 * in - the VA we start the search(reverse order);
3712 * out - the VA with the highest aligned end address.
3713 * @align: alignment for required highest address
3715 * Returns: determined end address within vmap_area
3717 static unsigned long
3718 pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
3720 unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
3724 list_for_each_entry_from_reverse((*va),
3725 &free_vmap_area_list, list) {
3726 addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
3727 if ((*va)->va_start < addr)
3736 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3737 * @offsets: array containing offset of each area
3738 * @sizes: array containing size of each area
3739 * @nr_vms: the number of areas to allocate
3740 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
3742 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3743 * vm_structs on success, %NULL on failure
3745 * Percpu allocator wants to use congruent vm areas so that it can
3746 * maintain the offsets among percpu areas. This function allocates
3747 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
3748 * be scattered pretty far, distance between two areas easily going up
3749 * to gigabytes. To avoid interacting with regular vmallocs, these
3750 * areas are allocated from top.
3752 * Despite its complicated look, this allocator is rather simple. It
3753 * does everything top-down and scans free blocks from the end looking
3754 * for matching base. While scanning, if any of the areas do not fit the
3755 * base address is pulled down to fit the area. Scanning is repeated till
3756 * all the areas fit and then all necessary data structures are inserted
3757 * and the result is returned.
3759 struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3760 const size_t *sizes, int nr_vms,
3763 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3764 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
3765 struct vmap_area **vas, *va;
3766 struct vm_struct **vms;
3767 int area, area2, last_area, term_area;
3768 unsigned long base, start, size, end, last_end, orig_start, orig_end;
3769 bool purged = false;
3771 /* verify parameters and allocate data structures */
3772 BUG_ON(offset_in_page(align) || !is_power_of_2(align));
3773 for (last_area = 0, area = 0; area < nr_vms; area++) {
3774 start = offsets[area];
3775 end = start + sizes[area];
3777 /* is everything aligned properly? */
3778 BUG_ON(!IS_ALIGNED(offsets[area], align));
3779 BUG_ON(!IS_ALIGNED(sizes[area], align));
3781 /* detect the area with the highest address */
3782 if (start > offsets[last_area])
3785 for (area2 = area + 1; area2 < nr_vms; area2++) {
3786 unsigned long start2 = offsets[area2];
3787 unsigned long end2 = start2 + sizes[area2];
3789 BUG_ON(start2 < end && start < end2);
3792 last_end = offsets[last_area] + sizes[last_area];
3794 if (vmalloc_end - vmalloc_start < last_end) {
3799 vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
3800 vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
3804 for (area = 0; area < nr_vms; area++) {
3805 vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
3806 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
3807 if (!vas[area] || !vms[area])
3811 spin_lock(&free_vmap_area_lock);
3813 /* start scanning - we scan from the top, begin with the last area */
3814 area = term_area = last_area;
3815 start = offsets[area];
3816 end = start + sizes[area];
3818 va = pvm_find_va_enclose_addr(vmalloc_end);
3819 base = pvm_determine_end_from_reverse(&va, align) - end;
3823 * base might have underflowed, add last_end before
3826 if (base + last_end < vmalloc_start + last_end)
3830 * Fitting base has not been found.
3836 * If required width exceeds current VA block, move
3837 * base downwards and then recheck.
3839 if (base + end > va->va_end) {
3840 base = pvm_determine_end_from_reverse(&va, align) - end;
3846 * If this VA does not fit, move base downwards and recheck.
3848 if (base + start < va->va_start) {
3849 va = node_to_va(rb_prev(&va->rb_node));
3850 base = pvm_determine_end_from_reverse(&va, align) - end;
3856 * This area fits, move on to the previous one. If
3857 * the previous one is the terminal one, we're done.
3859 area = (area + nr_vms - 1) % nr_vms;
3860 if (area == term_area)
3863 start = offsets[area];
3864 end = start + sizes[area];
3865 va = pvm_find_va_enclose_addr(base + end);
3868 /* we've found a fitting base, insert all va's */
3869 for (area = 0; area < nr_vms; area++) {
3872 start = base + offsets[area];
3875 va = pvm_find_va_enclose_addr(start);
3876 if (WARN_ON_ONCE(va == NULL))
3877 /* It is a BUG(), but trigger recovery instead. */
3880 ret = adjust_va_to_fit_type(&free_vmap_area_root,
3881 &free_vmap_area_list,
3883 if (WARN_ON_ONCE(unlikely(ret)))
3884 /* It is a BUG(), but trigger recovery instead. */
3887 /* Allocated area. */
3889 va->va_start = start;
3890 va->va_end = start + size;
3893 spin_unlock(&free_vmap_area_lock);
3895 /* populate the kasan shadow space */
3896 for (area = 0; area < nr_vms; area++) {
3897 if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area]))
3898 goto err_free_shadow;
3901 /* insert all vm's */
3902 spin_lock(&vmap_area_lock);
3903 for (area = 0; area < nr_vms; area++) {
3904 insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list);
3906 setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC,
3909 spin_unlock(&vmap_area_lock);
3912 * Mark allocated areas as accessible. Do it now as a best-effort
3913 * approach, as they can be mapped outside of vmalloc code.
3914 * With hardware tag-based KASAN, marking is skipped for
3915 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
3917 for (area = 0; area < nr_vms; area++)
3918 vms[area]->addr = kasan_unpoison_vmalloc(vms[area]->addr,
3919 vms[area]->size, KASAN_VMALLOC_PROT_NORMAL);
3926 * Remove previously allocated areas. There is no
3927 * need in removing these areas from the busy tree,
3928 * because they are inserted only on the final step
3929 * and when pcpu_get_vm_areas() is success.
3932 orig_start = vas[area]->va_start;
3933 orig_end = vas[area]->va_end;
3934 va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
3935 &free_vmap_area_list);
3937 kasan_release_vmalloc(orig_start, orig_end,
3938 va->va_start, va->va_end);
3943 spin_unlock(&free_vmap_area_lock);
3945 purge_vmap_area_lazy();
3948 /* Before "retry", check if we recover. */
3949 for (area = 0; area < nr_vms; area++) {
3953 vas[area] = kmem_cache_zalloc(
3954 vmap_area_cachep, GFP_KERNEL);
3963 for (area = 0; area < nr_vms; area++) {
3965 kmem_cache_free(vmap_area_cachep, vas[area]);
3975 spin_lock(&free_vmap_area_lock);
3977 * We release all the vmalloc shadows, even the ones for regions that
3978 * hadn't been successfully added. This relies on kasan_release_vmalloc
3979 * being able to tolerate this case.
3981 for (area = 0; area < nr_vms; area++) {
3982 orig_start = vas[area]->va_start;
3983 orig_end = vas[area]->va_end;
3984 va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
3985 &free_vmap_area_list);
3987 kasan_release_vmalloc(orig_start, orig_end,
3988 va->va_start, va->va_end);
3992 spin_unlock(&free_vmap_area_lock);
3999 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
4000 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
4001 * @nr_vms: the number of allocated areas
4003 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
4005 void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
4009 for (i = 0; i < nr_vms; i++)
4010 free_vm_area(vms[i]);
4013 #endif /* CONFIG_SMP */
4015 #ifdef CONFIG_PRINTK
4016 bool vmalloc_dump_obj(void *object)
4018 struct vm_struct *vm;
4019 void *objp = (void *)PAGE_ALIGN((unsigned long)object);
4021 vm = find_vm_area(objp);
4024 pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
4025 vm->nr_pages, (unsigned long)vm->addr, vm->caller);
4030 #ifdef CONFIG_PROC_FS
4031 static void *s_start(struct seq_file *m, loff_t *pos)
4032 __acquires(&vmap_purge_lock)
4033 __acquires(&vmap_area_lock)
4035 mutex_lock(&vmap_purge_lock);
4036 spin_lock(&vmap_area_lock);
4038 return seq_list_start(&vmap_area_list, *pos);
4041 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4043 return seq_list_next(p, &vmap_area_list, pos);
4046 static void s_stop(struct seq_file *m, void *p)
4047 __releases(&vmap_area_lock)
4048 __releases(&vmap_purge_lock)
4050 spin_unlock(&vmap_area_lock);
4051 mutex_unlock(&vmap_purge_lock);
4054 static void show_numa_info(struct seq_file *m, struct vm_struct *v)
4056 if (IS_ENABLED(CONFIG_NUMA)) {
4057 unsigned int nr, *counters = m->private;
4058 unsigned int step = 1U << vm_area_page_order(v);
4063 if (v->flags & VM_UNINITIALIZED)
4065 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
4068 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
4070 for (nr = 0; nr < v->nr_pages; nr += step)
4071 counters[page_to_nid(v->pages[nr])] += step;
4072 for_each_node_state(nr, N_HIGH_MEMORY)
4074 seq_printf(m, " N%u=%u", nr, counters[nr]);
4078 static void show_purge_info(struct seq_file *m)
4080 struct vmap_area *va;
4082 spin_lock(&purge_vmap_area_lock);
4083 list_for_each_entry(va, &purge_vmap_area_list, list) {
4084 seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n",
4085 (void *)va->va_start, (void *)va->va_end,
4086 va->va_end - va->va_start);
4088 spin_unlock(&purge_vmap_area_lock);
4091 static int s_show(struct seq_file *m, void *p)
4093 struct vmap_area *va;
4094 struct vm_struct *v;
4096 va = list_entry(p, struct vmap_area, list);
4099 * s_show can encounter race with remove_vm_area, !vm on behalf
4100 * of vmap area is being tear down or vm_map_ram allocation.
4103 seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n",
4104 (void *)va->va_start, (void *)va->va_end,
4105 va->va_end - va->va_start);
4112 seq_printf(m, "0x%pK-0x%pK %7ld",
4113 v->addr, v->addr + v->size, v->size);
4116 seq_printf(m, " %pS", v->caller);
4119 seq_printf(m, " pages=%d", v->nr_pages);
4122 seq_printf(m, " phys=%pa", &v->phys_addr);
4124 if (v->flags & VM_IOREMAP)
4125 seq_puts(m, " ioremap");
4127 if (v->flags & VM_ALLOC)
4128 seq_puts(m, " vmalloc");
4130 if (v->flags & VM_MAP)
4131 seq_puts(m, " vmap");
4133 if (v->flags & VM_USERMAP)
4134 seq_puts(m, " user");
4136 if (v->flags & VM_DMA_COHERENT)
4137 seq_puts(m, " dma-coherent");
4139 if (is_vmalloc_addr(v->pages))
4140 seq_puts(m, " vpages");
4142 show_numa_info(m, v);
4146 * As a final step, dump "unpurged" areas.
4149 if (list_is_last(&va->list, &vmap_area_list))
4155 static const struct seq_operations vmalloc_op = {
4162 static int __init proc_vmalloc_init(void)
4164 if (IS_ENABLED(CONFIG_NUMA))
4165 proc_create_seq_private("vmallocinfo", 0400, NULL,
4167 nr_node_ids * sizeof(unsigned int), NULL);
4169 proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
4172 module_init(proc_vmalloc_init);