11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/vmalloc.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1993 Linus Torvalds 51da177e4SLinus Torvalds * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 61da177e4SLinus Torvalds * SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000 71da177e4SLinus Torvalds * Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002 8930fc45aSChristoph Lameter * Numa awareness, Christoph Lameter, SGI, June 2005 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 11db64fe02SNick Piggin #include <linux/vmalloc.h> 121da177e4SLinus Torvalds #include <linux/mm.h> 131da177e4SLinus Torvalds #include <linux/module.h> 141da177e4SLinus Torvalds #include <linux/highmem.h> 15c3edc401SIngo Molnar #include <linux/sched/signal.h> 161da177e4SLinus Torvalds #include <linux/slab.h> 171da177e4SLinus Torvalds #include <linux/spinlock.h> 181da177e4SLinus Torvalds #include <linux/interrupt.h> 195f6a6a9cSAlexey Dobriyan #include <linux/proc_fs.h> 20a10aa579SChristoph Lameter #include <linux/seq_file.h> 213ac7fe5aSThomas Gleixner #include <linux/debugobjects.h> 2223016969SChristoph Lameter #include <linux/kallsyms.h> 23db64fe02SNick Piggin #include <linux/list.h> 244da56b99SChris Wilson #include <linux/notifier.h> 25db64fe02SNick Piggin #include <linux/rbtree.h> 26db64fe02SNick Piggin #include <linux/radix-tree.h> 27db64fe02SNick Piggin #include <linux/rcupdate.h> 28f0aa6617STejun Heo #include <linux/pfn.h> 2989219d37SCatalin Marinas #include <linux/kmemleak.h> 3060063497SArun Sharma #include <linux/atomic.h> 313b32123dSGideon Israel Dsouza #include <linux/compiler.h> 3232fcfd40SAl Viro #include <linux/llist.h> 330f616be1SToshi Kani #include <linux/bitops.h> 343b32123dSGideon Israel Dsouza 357c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 361da177e4SLinus Torvalds #include <asm/tlbflush.h> 372dca6999SDavid Miller #include <asm/shmparam.h> 381da177e4SLinus Torvalds 39dd56b046SMel Gorman #include "internal.h" 40dd56b046SMel Gorman 4132fcfd40SAl Viro struct vfree_deferred { 4232fcfd40SAl Viro struct llist_head list; 4332fcfd40SAl Viro struct work_struct wq; 4432fcfd40SAl Viro }; 4532fcfd40SAl Viro static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred); 4632fcfd40SAl Viro 4732fcfd40SAl Viro static void __vunmap(const void *, int); 4832fcfd40SAl Viro 4932fcfd40SAl Viro static void free_work(struct work_struct *w) 5032fcfd40SAl Viro { 5132fcfd40SAl Viro struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq); 52894e58c1SByungchul Park struct llist_node *t, *llnode; 53894e58c1SByungchul Park 54894e58c1SByungchul Park llist_for_each_safe(llnode, t, llist_del_all(&p->list)) 55894e58c1SByungchul Park __vunmap((void *)llnode, 1); 5632fcfd40SAl Viro } 5732fcfd40SAl Viro 58db64fe02SNick Piggin /*** Page table manipulation functions ***/ 59b221385bSAdrian Bunk 601da177e4SLinus Torvalds static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end) 611da177e4SLinus Torvalds { 621da177e4SLinus Torvalds pte_t *pte; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds pte = pte_offset_kernel(pmd, addr); 651da177e4SLinus Torvalds do { 661da177e4SLinus Torvalds pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte); 671da177e4SLinus Torvalds WARN_ON(!pte_none(ptent) && !pte_present(ptent)); 681da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 691da177e4SLinus Torvalds } 701da177e4SLinus Torvalds 71db64fe02SNick Piggin static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end) 721da177e4SLinus Torvalds { 731da177e4SLinus Torvalds pmd_t *pmd; 741da177e4SLinus Torvalds unsigned long next; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 771da177e4SLinus Torvalds do { 781da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 79b9820d8fSToshi Kani if (pmd_clear_huge(pmd)) 80b9820d8fSToshi Kani continue; 811da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 821da177e4SLinus Torvalds continue; 831da177e4SLinus Torvalds vunmap_pte_range(pmd, addr, next); 841da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 87c2febafcSKirill A. Shutemov static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds pud_t *pud; 901da177e4SLinus Torvalds unsigned long next; 911da177e4SLinus Torvalds 92c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 931da177e4SLinus Torvalds do { 941da177e4SLinus Torvalds next = pud_addr_end(addr, end); 95b9820d8fSToshi Kani if (pud_clear_huge(pud)) 96b9820d8fSToshi Kani continue; 971da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 981da177e4SLinus Torvalds continue; 991da177e4SLinus Torvalds vunmap_pmd_range(pud, addr, next); 1001da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 103c2febafcSKirill A. Shutemov static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end) 104c2febafcSKirill A. Shutemov { 105c2febafcSKirill A. Shutemov p4d_t *p4d; 106c2febafcSKirill A. Shutemov unsigned long next; 107c2febafcSKirill A. Shutemov 108c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 109c2febafcSKirill A. Shutemov do { 110c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 111c2febafcSKirill A. Shutemov if (p4d_clear_huge(p4d)) 112c2febafcSKirill A. Shutemov continue; 113c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 114c2febafcSKirill A. Shutemov continue; 115c2febafcSKirill A. Shutemov vunmap_pud_range(p4d, addr, next); 116c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 117c2febafcSKirill A. Shutemov } 118c2febafcSKirill A. Shutemov 119db64fe02SNick Piggin static void vunmap_page_range(unsigned long addr, unsigned long end) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds pgd_t *pgd; 1221da177e4SLinus Torvalds unsigned long next; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds BUG_ON(addr >= end); 1251da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 1261da177e4SLinus Torvalds do { 1271da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 1281da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 1291da177e4SLinus Torvalds continue; 130c2febafcSKirill A. Shutemov vunmap_p4d_range(pgd, addr, next); 1311da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds static int vmap_pte_range(pmd_t *pmd, unsigned long addr, 135db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds pte_t *pte; 1381da177e4SLinus Torvalds 139db64fe02SNick Piggin /* 140db64fe02SNick Piggin * nr is a running index into the array which helps higher level 141db64fe02SNick Piggin * callers keep track of where we're up to. 142db64fe02SNick Piggin */ 143db64fe02SNick Piggin 144872fec16SHugh Dickins pte = pte_alloc_kernel(pmd, addr); 1451da177e4SLinus Torvalds if (!pte) 1461da177e4SLinus Torvalds return -ENOMEM; 1471da177e4SLinus Torvalds do { 148db64fe02SNick Piggin struct page *page = pages[*nr]; 149db64fe02SNick Piggin 150db64fe02SNick Piggin if (WARN_ON(!pte_none(*pte))) 151db64fe02SNick Piggin return -EBUSY; 152db64fe02SNick Piggin if (WARN_ON(!page)) 1531da177e4SLinus Torvalds return -ENOMEM; 1541da177e4SLinus Torvalds set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); 155db64fe02SNick Piggin (*nr)++; 1561da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 1571da177e4SLinus Torvalds return 0; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 160db64fe02SNick Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr, 161db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds pmd_t *pmd; 1641da177e4SLinus Torvalds unsigned long next; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds pmd = pmd_alloc(&init_mm, pud, addr); 1671da177e4SLinus Torvalds if (!pmd) 1681da177e4SLinus Torvalds return -ENOMEM; 1691da177e4SLinus Torvalds do { 1701da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 171db64fe02SNick Piggin if (vmap_pte_range(pmd, addr, next, prot, pages, nr)) 1721da177e4SLinus Torvalds return -ENOMEM; 1731da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 1741da177e4SLinus Torvalds return 0; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 177c2febafcSKirill A. Shutemov static int vmap_pud_range(p4d_t *p4d, unsigned long addr, 178db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds pud_t *pud; 1811da177e4SLinus Torvalds unsigned long next; 1821da177e4SLinus Torvalds 183c2febafcSKirill A. Shutemov pud = pud_alloc(&init_mm, p4d, addr); 1841da177e4SLinus Torvalds if (!pud) 1851da177e4SLinus Torvalds return -ENOMEM; 1861da177e4SLinus Torvalds do { 1871da177e4SLinus Torvalds next = pud_addr_end(addr, end); 188db64fe02SNick Piggin if (vmap_pmd_range(pud, addr, next, prot, pages, nr)) 1891da177e4SLinus Torvalds return -ENOMEM; 1901da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 1911da177e4SLinus Torvalds return 0; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 194c2febafcSKirill A. Shutemov static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, 195c2febafcSKirill A. Shutemov unsigned long end, pgprot_t prot, struct page **pages, int *nr) 196c2febafcSKirill A. Shutemov { 197c2febafcSKirill A. Shutemov p4d_t *p4d; 198c2febafcSKirill A. Shutemov unsigned long next; 199c2febafcSKirill A. Shutemov 200c2febafcSKirill A. Shutemov p4d = p4d_alloc(&init_mm, pgd, addr); 201c2febafcSKirill A. Shutemov if (!p4d) 202c2febafcSKirill A. Shutemov return -ENOMEM; 203c2febafcSKirill A. Shutemov do { 204c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 205c2febafcSKirill A. Shutemov if (vmap_pud_range(p4d, addr, next, prot, pages, nr)) 206c2febafcSKirill A. Shutemov return -ENOMEM; 207c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 208c2febafcSKirill A. Shutemov return 0; 209c2febafcSKirill A. Shutemov } 210c2febafcSKirill A. Shutemov 211db64fe02SNick Piggin /* 212db64fe02SNick Piggin * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and 213db64fe02SNick Piggin * will have pfns corresponding to the "pages" array. 214db64fe02SNick Piggin * 215db64fe02SNick Piggin * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N] 216db64fe02SNick Piggin */ 2178fc48985STejun Heo static int vmap_page_range_noflush(unsigned long start, unsigned long end, 218db64fe02SNick Piggin pgprot_t prot, struct page **pages) 2191da177e4SLinus Torvalds { 2201da177e4SLinus Torvalds pgd_t *pgd; 2211da177e4SLinus Torvalds unsigned long next; 2222e4e27c7SAdam Lackorzynski unsigned long addr = start; 223db64fe02SNick Piggin int err = 0; 224db64fe02SNick Piggin int nr = 0; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds BUG_ON(addr >= end); 2271da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 2281da177e4SLinus Torvalds do { 2291da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 230c2febafcSKirill A. Shutemov err = vmap_p4d_range(pgd, addr, next, prot, pages, &nr); 2311da177e4SLinus Torvalds if (err) 232bf88c8c8SFigo.zhang return err; 2331da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 234db64fe02SNick Piggin 235db64fe02SNick Piggin return nr; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2388fc48985STejun Heo static int vmap_page_range(unsigned long start, unsigned long end, 2398fc48985STejun Heo pgprot_t prot, struct page **pages) 2408fc48985STejun Heo { 2418fc48985STejun Heo int ret; 2428fc48985STejun Heo 2438fc48985STejun Heo ret = vmap_page_range_noflush(start, end, prot, pages); 2448fc48985STejun Heo flush_cache_vmap(start, end); 2458fc48985STejun Heo return ret; 2468fc48985STejun Heo } 2478fc48985STejun Heo 24881ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x) 24973bdf0a6SLinus Torvalds { 25073bdf0a6SLinus Torvalds /* 251ab4f2ee1SRussell King * ARM, x86-64 and sparc64 put modules in a special place, 25273bdf0a6SLinus Torvalds * and fall back on vmalloc() if that fails. Others 25373bdf0a6SLinus Torvalds * just put it in the vmalloc space. 25473bdf0a6SLinus Torvalds */ 25573bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) 25673bdf0a6SLinus Torvalds unsigned long addr = (unsigned long)x; 25773bdf0a6SLinus Torvalds if (addr >= MODULES_VADDR && addr < MODULES_END) 25873bdf0a6SLinus Torvalds return 1; 25973bdf0a6SLinus Torvalds #endif 26073bdf0a6SLinus Torvalds return is_vmalloc_addr(x); 26173bdf0a6SLinus Torvalds } 26273bdf0a6SLinus Torvalds 26348667e7aSChristoph Lameter /* 264add688fbSmalc * Walk a vmap address to the struct page it maps. 26548667e7aSChristoph Lameter */ 266add688fbSmalc struct page *vmalloc_to_page(const void *vmalloc_addr) 26748667e7aSChristoph Lameter { 26848667e7aSChristoph Lameter unsigned long addr = (unsigned long) vmalloc_addr; 269add688fbSmalc struct page *page = NULL; 27048667e7aSChristoph Lameter pgd_t *pgd = pgd_offset_k(addr); 271c2febafcSKirill A. Shutemov p4d_t *p4d; 272c2febafcSKirill A. Shutemov pud_t *pud; 273c2febafcSKirill A. Shutemov pmd_t *pmd; 274c2febafcSKirill A. Shutemov pte_t *ptep, pte; 27548667e7aSChristoph Lameter 2767aa413deSIngo Molnar /* 2777aa413deSIngo Molnar * XXX we might need to change this if we add VIRTUAL_BUG_ON for 2787aa413deSIngo Molnar * architectures that do not vmalloc module space 2797aa413deSIngo Molnar */ 28073bdf0a6SLinus Torvalds VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); 28159ea7463SJiri Slaby 282c2febafcSKirill A. Shutemov if (pgd_none(*pgd)) 283c2febafcSKirill A. Shutemov return NULL; 284c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 285c2febafcSKirill A. Shutemov if (p4d_none(*p4d)) 286c2febafcSKirill A. Shutemov return NULL; 287c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 288029c54b0SArd Biesheuvel 289029c54b0SArd Biesheuvel /* 290029c54b0SArd Biesheuvel * Don't dereference bad PUD or PMD (below) entries. This will also 291029c54b0SArd Biesheuvel * identify huge mappings, which we may encounter on architectures 292029c54b0SArd Biesheuvel * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be 293029c54b0SArd Biesheuvel * identified as vmalloc addresses by is_vmalloc_addr(), but are 294029c54b0SArd Biesheuvel * not [unambiguously] associated with a struct page, so there is 295029c54b0SArd Biesheuvel * no correct value to return for them. 296029c54b0SArd Biesheuvel */ 297029c54b0SArd Biesheuvel WARN_ON_ONCE(pud_bad(*pud)); 298029c54b0SArd Biesheuvel if (pud_none(*pud) || pud_bad(*pud)) 299c2febafcSKirill A. Shutemov return NULL; 300c2febafcSKirill A. Shutemov pmd = pmd_offset(pud, addr); 301029c54b0SArd Biesheuvel WARN_ON_ONCE(pmd_bad(*pmd)); 302029c54b0SArd Biesheuvel if (pmd_none(*pmd) || pmd_bad(*pmd)) 303c2febafcSKirill A. Shutemov return NULL; 304db64fe02SNick Piggin 30548667e7aSChristoph Lameter ptep = pte_offset_map(pmd, addr); 30648667e7aSChristoph Lameter pte = *ptep; 30748667e7aSChristoph Lameter if (pte_present(pte)) 308add688fbSmalc page = pte_page(pte); 30948667e7aSChristoph Lameter pte_unmap(ptep); 310add688fbSmalc return page; 311ece86e22SJianyu Zhan } 312ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page); 313ece86e22SJianyu Zhan 314add688fbSmalc /* 315add688fbSmalc * Map a vmalloc()-space virtual address to the physical page frame number. 316add688fbSmalc */ 317add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr) 318add688fbSmalc { 319add688fbSmalc return page_to_pfn(vmalloc_to_page(vmalloc_addr)); 320add688fbSmalc } 321add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn); 322add688fbSmalc 323db64fe02SNick Piggin 324db64fe02SNick Piggin /*** Global kva allocator ***/ 325db64fe02SNick Piggin 32678c72746SYisheng Xie #define VM_LAZY_FREE 0x02 327db64fe02SNick Piggin #define VM_VM_AREA 0x04 328db64fe02SNick Piggin 329db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock); 330f1c4069eSJoonsoo Kim /* Export for kexec only */ 331f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list); 33280c4bd7aSChris Wilson static LLIST_HEAD(vmap_purge_list); 33389699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT; 33489699605SNick Piggin 33589699605SNick Piggin /* The vmap cache globals are protected by vmap_area_lock */ 33689699605SNick Piggin static struct rb_node *free_vmap_cache; 33789699605SNick Piggin static unsigned long cached_hole_size; 33889699605SNick Piggin static unsigned long cached_vstart; 33989699605SNick Piggin static unsigned long cached_align; 34089699605SNick Piggin 341ca23e405STejun Heo static unsigned long vmap_area_pcpu_hole; 342db64fe02SNick Piggin 343db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr) 3441da177e4SLinus Torvalds { 345db64fe02SNick Piggin struct rb_node *n = vmap_area_root.rb_node; 346db64fe02SNick Piggin 347db64fe02SNick Piggin while (n) { 348db64fe02SNick Piggin struct vmap_area *va; 349db64fe02SNick Piggin 350db64fe02SNick Piggin va = rb_entry(n, struct vmap_area, rb_node); 351db64fe02SNick Piggin if (addr < va->va_start) 352db64fe02SNick Piggin n = n->rb_left; 353cef2ac3fSHATAYAMA Daisuke else if (addr >= va->va_end) 354db64fe02SNick Piggin n = n->rb_right; 355db64fe02SNick Piggin else 356db64fe02SNick Piggin return va; 357db64fe02SNick Piggin } 358db64fe02SNick Piggin 359db64fe02SNick Piggin return NULL; 360db64fe02SNick Piggin } 361db64fe02SNick Piggin 362db64fe02SNick Piggin static void __insert_vmap_area(struct vmap_area *va) 363db64fe02SNick Piggin { 364db64fe02SNick Piggin struct rb_node **p = &vmap_area_root.rb_node; 365db64fe02SNick Piggin struct rb_node *parent = NULL; 366db64fe02SNick Piggin struct rb_node *tmp; 367db64fe02SNick Piggin 368db64fe02SNick Piggin while (*p) { 369170168d0SNamhyung Kim struct vmap_area *tmp_va; 370db64fe02SNick Piggin 371db64fe02SNick Piggin parent = *p; 372170168d0SNamhyung Kim tmp_va = rb_entry(parent, struct vmap_area, rb_node); 373170168d0SNamhyung Kim if (va->va_start < tmp_va->va_end) 374db64fe02SNick Piggin p = &(*p)->rb_left; 375170168d0SNamhyung Kim else if (va->va_end > tmp_va->va_start) 376db64fe02SNick Piggin p = &(*p)->rb_right; 377db64fe02SNick Piggin else 378db64fe02SNick Piggin BUG(); 379db64fe02SNick Piggin } 380db64fe02SNick Piggin 381db64fe02SNick Piggin rb_link_node(&va->rb_node, parent, p); 382db64fe02SNick Piggin rb_insert_color(&va->rb_node, &vmap_area_root); 383db64fe02SNick Piggin 3844341fa45SJoonsoo Kim /* address-sort this list */ 385db64fe02SNick Piggin tmp = rb_prev(&va->rb_node); 386db64fe02SNick Piggin if (tmp) { 387db64fe02SNick Piggin struct vmap_area *prev; 388db64fe02SNick Piggin prev = rb_entry(tmp, struct vmap_area, rb_node); 389db64fe02SNick Piggin list_add_rcu(&va->list, &prev->list); 390db64fe02SNick Piggin } else 391db64fe02SNick Piggin list_add_rcu(&va->list, &vmap_area_list); 392db64fe02SNick Piggin } 393db64fe02SNick Piggin 394db64fe02SNick Piggin static void purge_vmap_area_lazy(void); 395db64fe02SNick Piggin 3964da56b99SChris Wilson static BLOCKING_NOTIFIER_HEAD(vmap_notify_list); 3974da56b99SChris Wilson 398db64fe02SNick Piggin /* 399db64fe02SNick Piggin * Allocate a region of KVA of the specified size and alignment, within the 400db64fe02SNick Piggin * vstart and vend. 401db64fe02SNick Piggin */ 402db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size, 403db64fe02SNick Piggin unsigned long align, 404db64fe02SNick Piggin unsigned long vstart, unsigned long vend, 405db64fe02SNick Piggin int node, gfp_t gfp_mask) 406db64fe02SNick Piggin { 407db64fe02SNick Piggin struct vmap_area *va; 408db64fe02SNick Piggin struct rb_node *n; 4091da177e4SLinus Torvalds unsigned long addr; 410db64fe02SNick Piggin int purged = 0; 41189699605SNick Piggin struct vmap_area *first; 412db64fe02SNick Piggin 4137766970cSNick Piggin BUG_ON(!size); 414891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 41589699605SNick Piggin BUG_ON(!is_power_of_2(align)); 416db64fe02SNick Piggin 4175803ed29SChristoph Hellwig might_sleep(); 4184da56b99SChris Wilson 419db64fe02SNick Piggin va = kmalloc_node(sizeof(struct vmap_area), 420db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 421db64fe02SNick Piggin if (unlikely(!va)) 422db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 423db64fe02SNick Piggin 4247f88f88fSCatalin Marinas /* 4257f88f88fSCatalin Marinas * Only scan the relevant parts containing pointers to other objects 4267f88f88fSCatalin Marinas * to avoid false negatives. 4277f88f88fSCatalin Marinas */ 4287f88f88fSCatalin Marinas kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK); 4297f88f88fSCatalin Marinas 430db64fe02SNick Piggin retry: 431db64fe02SNick Piggin spin_lock(&vmap_area_lock); 43289699605SNick Piggin /* 43389699605SNick Piggin * Invalidate cache if we have more permissive parameters. 43489699605SNick Piggin * cached_hole_size notes the largest hole noticed _below_ 43589699605SNick Piggin * the vmap_area cached in free_vmap_cache: if size fits 43689699605SNick Piggin * into that hole, we want to scan from vstart to reuse 43789699605SNick Piggin * the hole instead of allocating above free_vmap_cache. 43889699605SNick Piggin * Note that __free_vmap_area may update free_vmap_cache 43989699605SNick Piggin * without updating cached_hole_size or cached_align. 44089699605SNick Piggin */ 44189699605SNick Piggin if (!free_vmap_cache || 44289699605SNick Piggin size < cached_hole_size || 44389699605SNick Piggin vstart < cached_vstart || 44489699605SNick Piggin align < cached_align) { 44589699605SNick Piggin nocache: 44689699605SNick Piggin cached_hole_size = 0; 44789699605SNick Piggin free_vmap_cache = NULL; 44889699605SNick Piggin } 44989699605SNick Piggin /* record if we encounter less permissive parameters */ 45089699605SNick Piggin cached_vstart = vstart; 45189699605SNick Piggin cached_align = align; 45289699605SNick Piggin 45389699605SNick Piggin /* find starting point for our search */ 45489699605SNick Piggin if (free_vmap_cache) { 45589699605SNick Piggin first = rb_entry(free_vmap_cache, struct vmap_area, rb_node); 456248ac0e1SJohannes Weiner addr = ALIGN(first->va_end, align); 45789699605SNick Piggin if (addr < vstart) 45889699605SNick Piggin goto nocache; 459bcb615a8SZhang Yanfei if (addr + size < addr) 4607766970cSNick Piggin goto overflow; 4617766970cSNick Piggin 46289699605SNick Piggin } else { 46389699605SNick Piggin addr = ALIGN(vstart, align); 464bcb615a8SZhang Yanfei if (addr + size < addr) 46589699605SNick Piggin goto overflow; 466db64fe02SNick Piggin 46789699605SNick Piggin n = vmap_area_root.rb_node; 46889699605SNick Piggin first = NULL; 46989699605SNick Piggin 47089699605SNick Piggin while (n) { 471db64fe02SNick Piggin struct vmap_area *tmp; 472db64fe02SNick Piggin tmp = rb_entry(n, struct vmap_area, rb_node); 473db64fe02SNick Piggin if (tmp->va_end >= addr) { 474db64fe02SNick Piggin first = tmp; 47589699605SNick Piggin if (tmp->va_start <= addr) 47689699605SNick Piggin break; 477db64fe02SNick Piggin n = n->rb_left; 47889699605SNick Piggin } else 479db64fe02SNick Piggin n = n->rb_right; 480db64fe02SNick Piggin } 481db64fe02SNick Piggin 482db64fe02SNick Piggin if (!first) 483db64fe02SNick Piggin goto found; 484db64fe02SNick Piggin } 485db64fe02SNick Piggin 48689699605SNick Piggin /* from the starting point, walk areas until a suitable hole is found */ 487248ac0e1SJohannes Weiner while (addr + size > first->va_start && addr + size <= vend) { 48889699605SNick Piggin if (addr + cached_hole_size < first->va_start) 48989699605SNick Piggin cached_hole_size = first->va_start - addr; 490248ac0e1SJohannes Weiner addr = ALIGN(first->va_end, align); 491bcb615a8SZhang Yanfei if (addr + size < addr) 4927766970cSNick Piggin goto overflow; 493db64fe02SNick Piggin 49492ca922fSHong zhi guo if (list_is_last(&first->list, &vmap_area_list)) 495db64fe02SNick Piggin goto found; 49692ca922fSHong zhi guo 4976219c2a2SGeliang Tang first = list_next_entry(first, list); 498db64fe02SNick Piggin } 49989699605SNick Piggin 500db64fe02SNick Piggin found: 501afd07389SUladzislau Rezki (Sony) /* 502afd07389SUladzislau Rezki (Sony) * Check also calculated address against the vstart, 503afd07389SUladzislau Rezki (Sony) * because it can be 0 because of big align request. 504afd07389SUladzislau Rezki (Sony) */ 505afd07389SUladzislau Rezki (Sony) if (addr + size > vend || addr < vstart) 50689699605SNick Piggin goto overflow; 50789699605SNick Piggin 50889699605SNick Piggin va->va_start = addr; 50989699605SNick Piggin va->va_end = addr + size; 51089699605SNick Piggin va->flags = 0; 51189699605SNick Piggin __insert_vmap_area(va); 51289699605SNick Piggin free_vmap_cache = &va->rb_node; 51389699605SNick Piggin spin_unlock(&vmap_area_lock); 51489699605SNick Piggin 51561e16557SWang Xiaoqiang BUG_ON(!IS_ALIGNED(va->va_start, align)); 51689699605SNick Piggin BUG_ON(va->va_start < vstart); 51789699605SNick Piggin BUG_ON(va->va_end > vend); 51889699605SNick Piggin 51989699605SNick Piggin return va; 52089699605SNick Piggin 5217766970cSNick Piggin overflow: 522db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 523db64fe02SNick Piggin if (!purged) { 524db64fe02SNick Piggin purge_vmap_area_lazy(); 525db64fe02SNick Piggin purged = 1; 526db64fe02SNick Piggin goto retry; 527db64fe02SNick Piggin } 5284da56b99SChris Wilson 5294da56b99SChris Wilson if (gfpflags_allow_blocking(gfp_mask)) { 5304da56b99SChris Wilson unsigned long freed = 0; 5314da56b99SChris Wilson blocking_notifier_call_chain(&vmap_notify_list, 0, &freed); 5324da56b99SChris Wilson if (freed > 0) { 5334da56b99SChris Wilson purged = 0; 5344da56b99SChris Wilson goto retry; 5354da56b99SChris Wilson } 5364da56b99SChris Wilson } 5374da56b99SChris Wilson 53803497d76SFlorian Fainelli if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) 539756a025fSJoe Perches pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n", 540756a025fSJoe Perches size); 5412498ce42SRalph Wuerthner kfree(va); 542db64fe02SNick Piggin return ERR_PTR(-EBUSY); 543db64fe02SNick Piggin } 544db64fe02SNick Piggin 5454da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb) 5464da56b99SChris Wilson { 5474da56b99SChris Wilson return blocking_notifier_chain_register(&vmap_notify_list, nb); 5484da56b99SChris Wilson } 5494da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier); 5504da56b99SChris Wilson 5514da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb) 5524da56b99SChris Wilson { 5534da56b99SChris Wilson return blocking_notifier_chain_unregister(&vmap_notify_list, nb); 5544da56b99SChris Wilson } 5554da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier); 5564da56b99SChris Wilson 557db64fe02SNick Piggin static void __free_vmap_area(struct vmap_area *va) 558db64fe02SNick Piggin { 559db64fe02SNick Piggin BUG_ON(RB_EMPTY_NODE(&va->rb_node)); 56089699605SNick Piggin 56189699605SNick Piggin if (free_vmap_cache) { 56289699605SNick Piggin if (va->va_end < cached_vstart) { 56389699605SNick Piggin free_vmap_cache = NULL; 56489699605SNick Piggin } else { 56589699605SNick Piggin struct vmap_area *cache; 56689699605SNick Piggin cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node); 56789699605SNick Piggin if (va->va_start <= cache->va_start) { 56889699605SNick Piggin free_vmap_cache = rb_prev(&va->rb_node); 56989699605SNick Piggin /* 57089699605SNick Piggin * We don't try to update cached_hole_size or 57189699605SNick Piggin * cached_align, but it won't go very wrong. 57289699605SNick Piggin */ 57389699605SNick Piggin } 57489699605SNick Piggin } 57589699605SNick Piggin } 576db64fe02SNick Piggin rb_erase(&va->rb_node, &vmap_area_root); 577db64fe02SNick Piggin RB_CLEAR_NODE(&va->rb_node); 578db64fe02SNick Piggin list_del_rcu(&va->list); 579db64fe02SNick Piggin 580ca23e405STejun Heo /* 581ca23e405STejun Heo * Track the highest possible candidate for pcpu area 582ca23e405STejun Heo * allocation. Areas outside of vmalloc area can be returned 583ca23e405STejun Heo * here too, consider only end addresses which fall inside 584ca23e405STejun Heo * vmalloc area proper. 585ca23e405STejun Heo */ 586ca23e405STejun Heo if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END) 587ca23e405STejun Heo vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end); 588ca23e405STejun Heo 58914769de9SLai Jiangshan kfree_rcu(va, rcu_head); 590db64fe02SNick Piggin } 591db64fe02SNick Piggin 592db64fe02SNick Piggin /* 593db64fe02SNick Piggin * Free a region of KVA allocated by alloc_vmap_area 594db64fe02SNick Piggin */ 595db64fe02SNick Piggin static void free_vmap_area(struct vmap_area *va) 596db64fe02SNick Piggin { 597db64fe02SNick Piggin spin_lock(&vmap_area_lock); 598db64fe02SNick Piggin __free_vmap_area(va); 599db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 600db64fe02SNick Piggin } 601db64fe02SNick Piggin 602db64fe02SNick Piggin /* 603db64fe02SNick Piggin * Clear the pagetable entries of a given vmap_area 604db64fe02SNick Piggin */ 605db64fe02SNick Piggin static void unmap_vmap_area(struct vmap_area *va) 606db64fe02SNick Piggin { 607db64fe02SNick Piggin vunmap_page_range(va->va_start, va->va_end); 608db64fe02SNick Piggin } 609db64fe02SNick Piggin 610db64fe02SNick Piggin /* 611db64fe02SNick Piggin * lazy_max_pages is the maximum amount of virtual address space we gather up 612db64fe02SNick Piggin * before attempting to purge with a TLB flush. 613db64fe02SNick Piggin * 614db64fe02SNick Piggin * There is a tradeoff here: a larger number will cover more kernel page tables 615db64fe02SNick Piggin * and take slightly longer to purge, but it will linearly reduce the number of 616db64fe02SNick Piggin * global TLB flushes that must be performed. It would seem natural to scale 617db64fe02SNick Piggin * this number up linearly with the number of CPUs (because vmapping activity 618db64fe02SNick Piggin * could also scale linearly with the number of CPUs), however it is likely 619db64fe02SNick Piggin * that in practice, workloads might be constrained in other ways that mean 620db64fe02SNick Piggin * vmap activity will not scale linearly with CPUs. Also, I want to be 621db64fe02SNick Piggin * conservative and not introduce a big latency on huge systems, so go with 622db64fe02SNick Piggin * a less aggressive log scale. It will still be an improvement over the old 623db64fe02SNick Piggin * code, and it will be simple to change the scale factor if we find that it 624db64fe02SNick Piggin * becomes a problem on bigger systems. 625db64fe02SNick Piggin */ 626db64fe02SNick Piggin static unsigned long lazy_max_pages(void) 627db64fe02SNick Piggin { 628db64fe02SNick Piggin unsigned int log; 629db64fe02SNick Piggin 630db64fe02SNick Piggin log = fls(num_online_cpus()); 631db64fe02SNick Piggin 632db64fe02SNick Piggin return log * (32UL * 1024 * 1024 / PAGE_SIZE); 633db64fe02SNick Piggin } 634db64fe02SNick Piggin 635db64fe02SNick Piggin static atomic_t vmap_lazy_nr = ATOMIC_INIT(0); 636db64fe02SNick Piggin 6370574ecd1SChristoph Hellwig /* 6380574ecd1SChristoph Hellwig * Serialize vmap purging. There is no actual criticial section protected 6390574ecd1SChristoph Hellwig * by this look, but we want to avoid concurrent calls for performance 6400574ecd1SChristoph Hellwig * reasons and to make the pcpu_get_vm_areas more deterministic. 6410574ecd1SChristoph Hellwig */ 642f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock); 6430574ecd1SChristoph Hellwig 64402b709dfSNick Piggin /* for per-CPU blocks */ 64502b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void); 64602b709dfSNick Piggin 647db64fe02SNick Piggin /* 6483ee48b6aSCliff Wickman * called before a call to iounmap() if the caller wants vm_area_struct's 6493ee48b6aSCliff Wickman * immediately freed. 6503ee48b6aSCliff Wickman */ 6513ee48b6aSCliff Wickman void set_iounmap_nonlazy(void) 6523ee48b6aSCliff Wickman { 6533ee48b6aSCliff Wickman atomic_set(&vmap_lazy_nr, lazy_max_pages()+1); 6543ee48b6aSCliff Wickman } 6553ee48b6aSCliff Wickman 6563ee48b6aSCliff Wickman /* 657db64fe02SNick Piggin * Purges all lazily-freed vmap areas. 658db64fe02SNick Piggin */ 6590574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end) 660db64fe02SNick Piggin { 66180c4bd7aSChris Wilson struct llist_node *valist; 662db64fe02SNick Piggin struct vmap_area *va; 663cbb76676SVegard Nossum struct vmap_area *n_va; 664763b218dSJoel Fernandes bool do_free = false; 665db64fe02SNick Piggin 6660574ecd1SChristoph Hellwig lockdep_assert_held(&vmap_purge_lock); 66702b709dfSNick Piggin 66880c4bd7aSChris Wilson valist = llist_del_all(&vmap_purge_list); 66980c4bd7aSChris Wilson llist_for_each_entry(va, valist, purge_list) { 6700574ecd1SChristoph Hellwig if (va->va_start < start) 6710574ecd1SChristoph Hellwig start = va->va_start; 6720574ecd1SChristoph Hellwig if (va->va_end > end) 6730574ecd1SChristoph Hellwig end = va->va_end; 674763b218dSJoel Fernandes do_free = true; 675db64fe02SNick Piggin } 676db64fe02SNick Piggin 677763b218dSJoel Fernandes if (!do_free) 6780574ecd1SChristoph Hellwig return false; 6790574ecd1SChristoph Hellwig 6800574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 681db64fe02SNick Piggin 682db64fe02SNick Piggin spin_lock(&vmap_area_lock); 683763b218dSJoel Fernandes llist_for_each_entry_safe(va, n_va, valist, purge_list) { 684763b218dSJoel Fernandes int nr = (va->va_end - va->va_start) >> PAGE_SHIFT; 685763b218dSJoel Fernandes 686db64fe02SNick Piggin __free_vmap_area(va); 687763b218dSJoel Fernandes atomic_sub(nr, &vmap_lazy_nr); 688763b218dSJoel Fernandes cond_resched_lock(&vmap_area_lock); 689763b218dSJoel Fernandes } 690db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 6910574ecd1SChristoph Hellwig return true; 692db64fe02SNick Piggin } 693db64fe02SNick Piggin 694db64fe02SNick Piggin /* 695496850e5SNick Piggin * Kick off a purge of the outstanding lazy areas. Don't bother if somebody 696496850e5SNick Piggin * is already purging. 697496850e5SNick Piggin */ 698496850e5SNick Piggin static void try_purge_vmap_area_lazy(void) 699496850e5SNick Piggin { 700f9e09977SChristoph Hellwig if (mutex_trylock(&vmap_purge_lock)) { 7010574ecd1SChristoph Hellwig __purge_vmap_area_lazy(ULONG_MAX, 0); 702f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 7030574ecd1SChristoph Hellwig } 704496850e5SNick Piggin } 705496850e5SNick Piggin 706496850e5SNick Piggin /* 707db64fe02SNick Piggin * Kick off a purge of the outstanding lazy areas. 708db64fe02SNick Piggin */ 709db64fe02SNick Piggin static void purge_vmap_area_lazy(void) 710db64fe02SNick Piggin { 711f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 7120574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 7130574ecd1SChristoph Hellwig __purge_vmap_area_lazy(ULONG_MAX, 0); 714f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 715db64fe02SNick Piggin } 716db64fe02SNick Piggin 717db64fe02SNick Piggin /* 71864141da5SJeremy Fitzhardinge * Free a vmap area, caller ensuring that the area has been unmapped 71964141da5SJeremy Fitzhardinge * and flush_cache_vunmap had been called for the correct range 72064141da5SJeremy Fitzhardinge * previously. 721db64fe02SNick Piggin */ 72264141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va) 723db64fe02SNick Piggin { 72480c4bd7aSChris Wilson int nr_lazy; 72580c4bd7aSChris Wilson 72680c4bd7aSChris Wilson nr_lazy = atomic_add_return((va->va_end - va->va_start) >> PAGE_SHIFT, 72780c4bd7aSChris Wilson &vmap_lazy_nr); 72880c4bd7aSChris Wilson 72980c4bd7aSChris Wilson /* After this point, we may free va at any time */ 73080c4bd7aSChris Wilson llist_add(&va->purge_list, &vmap_purge_list); 73180c4bd7aSChris Wilson 73280c4bd7aSChris Wilson if (unlikely(nr_lazy > lazy_max_pages())) 733496850e5SNick Piggin try_purge_vmap_area_lazy(); 734db64fe02SNick Piggin } 735db64fe02SNick Piggin 736b29acbdcSNick Piggin /* 737b29acbdcSNick Piggin * Free and unmap a vmap area 738b29acbdcSNick Piggin */ 739b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va) 740b29acbdcSNick Piggin { 741b29acbdcSNick Piggin flush_cache_vunmap(va->va_start, va->va_end); 742c8eef01eSChristoph Hellwig unmap_vmap_area(va); 74382a2e924SChintan Pandya if (debug_pagealloc_enabled()) 74482a2e924SChintan Pandya flush_tlb_kernel_range(va->va_start, va->va_end); 74582a2e924SChintan Pandya 746c8eef01eSChristoph Hellwig free_vmap_area_noflush(va); 747b29acbdcSNick Piggin } 748b29acbdcSNick Piggin 749db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr) 750db64fe02SNick Piggin { 751db64fe02SNick Piggin struct vmap_area *va; 752db64fe02SNick Piggin 753db64fe02SNick Piggin spin_lock(&vmap_area_lock); 754db64fe02SNick Piggin va = __find_vmap_area(addr); 755db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 756db64fe02SNick Piggin 757db64fe02SNick Piggin return va; 758db64fe02SNick Piggin } 759db64fe02SNick Piggin 760db64fe02SNick Piggin /*** Per cpu kva allocator ***/ 761db64fe02SNick Piggin 762db64fe02SNick Piggin /* 763db64fe02SNick Piggin * vmap space is limited especially on 32 bit architectures. Ensure there is 764db64fe02SNick Piggin * room for at least 16 percpu vmap blocks per CPU. 765db64fe02SNick Piggin */ 766db64fe02SNick Piggin /* 767db64fe02SNick Piggin * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able 768db64fe02SNick Piggin * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess 769db64fe02SNick Piggin * instead (we just need a rough idea) 770db64fe02SNick Piggin */ 771db64fe02SNick Piggin #if BITS_PER_LONG == 32 772db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024) 773db64fe02SNick Piggin #else 774db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024*1024) 775db64fe02SNick Piggin #endif 776db64fe02SNick Piggin 777db64fe02SNick Piggin #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) 778db64fe02SNick Piggin #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ 779db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ 780db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) 781db64fe02SNick Piggin #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ 782db64fe02SNick Piggin #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ 783f982f915SClemens Ladisch #define VMAP_BBMAP_BITS \ 784f982f915SClemens Ladisch VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ 785db64fe02SNick Piggin VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ 786f982f915SClemens Ladisch VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16)) 787db64fe02SNick Piggin 788db64fe02SNick Piggin #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) 789db64fe02SNick Piggin 7909b463334SJeremy Fitzhardinge static bool vmap_initialized __read_mostly = false; 7919b463334SJeremy Fitzhardinge 792db64fe02SNick Piggin struct vmap_block_queue { 793db64fe02SNick Piggin spinlock_t lock; 794db64fe02SNick Piggin struct list_head free; 795db64fe02SNick Piggin }; 796db64fe02SNick Piggin 797db64fe02SNick Piggin struct vmap_block { 798db64fe02SNick Piggin spinlock_t lock; 799db64fe02SNick Piggin struct vmap_area *va; 800db64fe02SNick Piggin unsigned long free, dirty; 8017d61bfe8SRoman Pen unsigned long dirty_min, dirty_max; /*< dirty range */ 802db64fe02SNick Piggin struct list_head free_list; 803db64fe02SNick Piggin struct rcu_head rcu_head; 80402b709dfSNick Piggin struct list_head purge; 805db64fe02SNick Piggin }; 806db64fe02SNick Piggin 807db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ 808db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); 809db64fe02SNick Piggin 810db64fe02SNick Piggin /* 811db64fe02SNick Piggin * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block 812db64fe02SNick Piggin * in the free path. Could get rid of this if we change the API to return a 813db64fe02SNick Piggin * "cookie" from alloc, to be passed to free. But no big deal yet. 814db64fe02SNick Piggin */ 815db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_block_tree_lock); 816db64fe02SNick Piggin static RADIX_TREE(vmap_block_tree, GFP_ATOMIC); 817db64fe02SNick Piggin 818db64fe02SNick Piggin /* 819db64fe02SNick Piggin * We should probably have a fallback mechanism to allocate virtual memory 820db64fe02SNick Piggin * out of partially filled vmap blocks. However vmap block sizing should be 821db64fe02SNick Piggin * fairly reasonable according to the vmalloc size, so it shouldn't be a 822db64fe02SNick Piggin * big problem. 823db64fe02SNick Piggin */ 824db64fe02SNick Piggin 825db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr) 826db64fe02SNick Piggin { 827db64fe02SNick Piggin addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); 828db64fe02SNick Piggin addr /= VMAP_BLOCK_SIZE; 829db64fe02SNick Piggin return addr; 830db64fe02SNick Piggin } 831db64fe02SNick Piggin 832cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off) 833cf725ce2SRoman Pen { 834cf725ce2SRoman Pen unsigned long addr; 835cf725ce2SRoman Pen 836cf725ce2SRoman Pen addr = va_start + (pages_off << PAGE_SHIFT); 837cf725ce2SRoman Pen BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start)); 838cf725ce2SRoman Pen return (void *)addr; 839cf725ce2SRoman Pen } 840cf725ce2SRoman Pen 841cf725ce2SRoman Pen /** 842cf725ce2SRoman Pen * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this 843cf725ce2SRoman Pen * block. Of course pages number can't exceed VMAP_BBMAP_BITS 844cf725ce2SRoman Pen * @order: how many 2^order pages should be occupied in newly allocated block 845cf725ce2SRoman Pen * @gfp_mask: flags for the page level allocator 846cf725ce2SRoman Pen * 847cf725ce2SRoman Pen * Returns: virtual address in a newly allocated block or ERR_PTR(-errno) 848cf725ce2SRoman Pen */ 849cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) 850db64fe02SNick Piggin { 851db64fe02SNick Piggin struct vmap_block_queue *vbq; 852db64fe02SNick Piggin struct vmap_block *vb; 853db64fe02SNick Piggin struct vmap_area *va; 854db64fe02SNick Piggin unsigned long vb_idx; 855db64fe02SNick Piggin int node, err; 856cf725ce2SRoman Pen void *vaddr; 857db64fe02SNick Piggin 858db64fe02SNick Piggin node = numa_node_id(); 859db64fe02SNick Piggin 860db64fe02SNick Piggin vb = kmalloc_node(sizeof(struct vmap_block), 861db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 862db64fe02SNick Piggin if (unlikely(!vb)) 863db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 864db64fe02SNick Piggin 865db64fe02SNick Piggin va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, 866db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, 867db64fe02SNick Piggin node, gfp_mask); 868ddf9c6d4STobias Klauser if (IS_ERR(va)) { 869db64fe02SNick Piggin kfree(vb); 870e7d86340SJulia Lawall return ERR_CAST(va); 871db64fe02SNick Piggin } 872db64fe02SNick Piggin 873db64fe02SNick Piggin err = radix_tree_preload(gfp_mask); 874db64fe02SNick Piggin if (unlikely(err)) { 875db64fe02SNick Piggin kfree(vb); 876db64fe02SNick Piggin free_vmap_area(va); 877db64fe02SNick Piggin return ERR_PTR(err); 878db64fe02SNick Piggin } 879db64fe02SNick Piggin 880cf725ce2SRoman Pen vaddr = vmap_block_vaddr(va->va_start, 0); 881db64fe02SNick Piggin spin_lock_init(&vb->lock); 882db64fe02SNick Piggin vb->va = va; 883cf725ce2SRoman Pen /* At least something should be left free */ 884cf725ce2SRoman Pen BUG_ON(VMAP_BBMAP_BITS <= (1UL << order)); 885cf725ce2SRoman Pen vb->free = VMAP_BBMAP_BITS - (1UL << order); 886db64fe02SNick Piggin vb->dirty = 0; 8877d61bfe8SRoman Pen vb->dirty_min = VMAP_BBMAP_BITS; 8887d61bfe8SRoman Pen vb->dirty_max = 0; 889db64fe02SNick Piggin INIT_LIST_HEAD(&vb->free_list); 890db64fe02SNick Piggin 891db64fe02SNick Piggin vb_idx = addr_to_vb_idx(va->va_start); 892db64fe02SNick Piggin spin_lock(&vmap_block_tree_lock); 893db64fe02SNick Piggin err = radix_tree_insert(&vmap_block_tree, vb_idx, vb); 894db64fe02SNick Piggin spin_unlock(&vmap_block_tree_lock); 895db64fe02SNick Piggin BUG_ON(err); 896db64fe02SNick Piggin radix_tree_preload_end(); 897db64fe02SNick Piggin 898db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 899db64fe02SNick Piggin spin_lock(&vbq->lock); 90068ac546fSRoman Pen list_add_tail_rcu(&vb->free_list, &vbq->free); 901db64fe02SNick Piggin spin_unlock(&vbq->lock); 9023f04ba85STejun Heo put_cpu_var(vmap_block_queue); 903db64fe02SNick Piggin 904cf725ce2SRoman Pen return vaddr; 905db64fe02SNick Piggin } 906db64fe02SNick Piggin 907db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb) 908db64fe02SNick Piggin { 909db64fe02SNick Piggin struct vmap_block *tmp; 910db64fe02SNick Piggin unsigned long vb_idx; 911db64fe02SNick Piggin 912db64fe02SNick Piggin vb_idx = addr_to_vb_idx(vb->va->va_start); 913db64fe02SNick Piggin spin_lock(&vmap_block_tree_lock); 914db64fe02SNick Piggin tmp = radix_tree_delete(&vmap_block_tree, vb_idx); 915db64fe02SNick Piggin spin_unlock(&vmap_block_tree_lock); 916db64fe02SNick Piggin BUG_ON(tmp != vb); 917db64fe02SNick Piggin 91864141da5SJeremy Fitzhardinge free_vmap_area_noflush(vb->va); 91922a3c7d1SLai Jiangshan kfree_rcu(vb, rcu_head); 920db64fe02SNick Piggin } 921db64fe02SNick Piggin 92202b709dfSNick Piggin static void purge_fragmented_blocks(int cpu) 92302b709dfSNick Piggin { 92402b709dfSNick Piggin LIST_HEAD(purge); 92502b709dfSNick Piggin struct vmap_block *vb; 92602b709dfSNick Piggin struct vmap_block *n_vb; 92702b709dfSNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 92802b709dfSNick Piggin 92902b709dfSNick Piggin rcu_read_lock(); 93002b709dfSNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 93102b709dfSNick Piggin 93202b709dfSNick Piggin if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS)) 93302b709dfSNick Piggin continue; 93402b709dfSNick Piggin 93502b709dfSNick Piggin spin_lock(&vb->lock); 93602b709dfSNick Piggin if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) { 93702b709dfSNick Piggin vb->free = 0; /* prevent further allocs after releasing lock */ 93802b709dfSNick Piggin vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */ 9397d61bfe8SRoman Pen vb->dirty_min = 0; 9407d61bfe8SRoman Pen vb->dirty_max = VMAP_BBMAP_BITS; 94102b709dfSNick Piggin spin_lock(&vbq->lock); 94202b709dfSNick Piggin list_del_rcu(&vb->free_list); 94302b709dfSNick Piggin spin_unlock(&vbq->lock); 94402b709dfSNick Piggin spin_unlock(&vb->lock); 94502b709dfSNick Piggin list_add_tail(&vb->purge, &purge); 94602b709dfSNick Piggin } else 94702b709dfSNick Piggin spin_unlock(&vb->lock); 94802b709dfSNick Piggin } 94902b709dfSNick Piggin rcu_read_unlock(); 95002b709dfSNick Piggin 95102b709dfSNick Piggin list_for_each_entry_safe(vb, n_vb, &purge, purge) { 95202b709dfSNick Piggin list_del(&vb->purge); 95302b709dfSNick Piggin free_vmap_block(vb); 95402b709dfSNick Piggin } 95502b709dfSNick Piggin } 95602b709dfSNick Piggin 95702b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void) 95802b709dfSNick Piggin { 95902b709dfSNick Piggin int cpu; 96002b709dfSNick Piggin 96102b709dfSNick Piggin for_each_possible_cpu(cpu) 96202b709dfSNick Piggin purge_fragmented_blocks(cpu); 96302b709dfSNick Piggin } 96402b709dfSNick Piggin 965db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask) 966db64fe02SNick Piggin { 967db64fe02SNick Piggin struct vmap_block_queue *vbq; 968db64fe02SNick Piggin struct vmap_block *vb; 969cf725ce2SRoman Pen void *vaddr = NULL; 970db64fe02SNick Piggin unsigned int order; 971db64fe02SNick Piggin 972891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 973db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 974aa91c4d8SJan Kara if (WARN_ON(size == 0)) { 975aa91c4d8SJan Kara /* 976aa91c4d8SJan Kara * Allocating 0 bytes isn't what caller wants since 977aa91c4d8SJan Kara * get_order(0) returns funny result. Just warn and terminate 978aa91c4d8SJan Kara * early. 979aa91c4d8SJan Kara */ 980aa91c4d8SJan Kara return NULL; 981aa91c4d8SJan Kara } 982db64fe02SNick Piggin order = get_order(size); 983db64fe02SNick Piggin 984db64fe02SNick Piggin rcu_read_lock(); 985db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 986db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 987cf725ce2SRoman Pen unsigned long pages_off; 988db64fe02SNick Piggin 989db64fe02SNick Piggin spin_lock(&vb->lock); 990cf725ce2SRoman Pen if (vb->free < (1UL << order)) { 991cf725ce2SRoman Pen spin_unlock(&vb->lock); 992cf725ce2SRoman Pen continue; 993cf725ce2SRoman Pen } 99402b709dfSNick Piggin 995cf725ce2SRoman Pen pages_off = VMAP_BBMAP_BITS - vb->free; 996cf725ce2SRoman Pen vaddr = vmap_block_vaddr(vb->va->va_start, pages_off); 997db64fe02SNick Piggin vb->free -= 1UL << order; 998db64fe02SNick Piggin if (vb->free == 0) { 999db64fe02SNick Piggin spin_lock(&vbq->lock); 1000de560423SNick Piggin list_del_rcu(&vb->free_list); 1001db64fe02SNick Piggin spin_unlock(&vbq->lock); 1002db64fe02SNick Piggin } 1003cf725ce2SRoman Pen 1004db64fe02SNick Piggin spin_unlock(&vb->lock); 1005db64fe02SNick Piggin break; 1006db64fe02SNick Piggin } 100702b709dfSNick Piggin 10083f04ba85STejun Heo put_cpu_var(vmap_block_queue); 1009db64fe02SNick Piggin rcu_read_unlock(); 1010db64fe02SNick Piggin 1011cf725ce2SRoman Pen /* Allocate new block if nothing was found */ 1012cf725ce2SRoman Pen if (!vaddr) 1013cf725ce2SRoman Pen vaddr = new_vmap_block(order, gfp_mask); 1014db64fe02SNick Piggin 1015cf725ce2SRoman Pen return vaddr; 1016db64fe02SNick Piggin } 1017db64fe02SNick Piggin 1018db64fe02SNick Piggin static void vb_free(const void *addr, unsigned long size) 1019db64fe02SNick Piggin { 1020db64fe02SNick Piggin unsigned long offset; 1021db64fe02SNick Piggin unsigned long vb_idx; 1022db64fe02SNick Piggin unsigned int order; 1023db64fe02SNick Piggin struct vmap_block *vb; 1024db64fe02SNick Piggin 1025891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 1026db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 1027b29acbdcSNick Piggin 1028b29acbdcSNick Piggin flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size); 1029b29acbdcSNick Piggin 1030db64fe02SNick Piggin order = get_order(size); 1031db64fe02SNick Piggin 1032db64fe02SNick Piggin offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1); 10337d61bfe8SRoman Pen offset >>= PAGE_SHIFT; 1034db64fe02SNick Piggin 1035db64fe02SNick Piggin vb_idx = addr_to_vb_idx((unsigned long)addr); 1036db64fe02SNick Piggin rcu_read_lock(); 1037db64fe02SNick Piggin vb = radix_tree_lookup(&vmap_block_tree, vb_idx); 1038db64fe02SNick Piggin rcu_read_unlock(); 1039db64fe02SNick Piggin BUG_ON(!vb); 1040db64fe02SNick Piggin 104164141da5SJeremy Fitzhardinge vunmap_page_range((unsigned long)addr, (unsigned long)addr + size); 104264141da5SJeremy Fitzhardinge 104382a2e924SChintan Pandya if (debug_pagealloc_enabled()) 104482a2e924SChintan Pandya flush_tlb_kernel_range((unsigned long)addr, 104582a2e924SChintan Pandya (unsigned long)addr + size); 104682a2e924SChintan Pandya 1047db64fe02SNick Piggin spin_lock(&vb->lock); 10487d61bfe8SRoman Pen 10497d61bfe8SRoman Pen /* Expand dirty range */ 10507d61bfe8SRoman Pen vb->dirty_min = min(vb->dirty_min, offset); 10517d61bfe8SRoman Pen vb->dirty_max = max(vb->dirty_max, offset + (1UL << order)); 1052d086817dSMinChan Kim 1053db64fe02SNick Piggin vb->dirty += 1UL << order; 1054db64fe02SNick Piggin if (vb->dirty == VMAP_BBMAP_BITS) { 1055de560423SNick Piggin BUG_ON(vb->free); 1056db64fe02SNick Piggin spin_unlock(&vb->lock); 1057db64fe02SNick Piggin free_vmap_block(vb); 1058db64fe02SNick Piggin } else 1059db64fe02SNick Piggin spin_unlock(&vb->lock); 1060db64fe02SNick Piggin } 1061db64fe02SNick Piggin 1062db64fe02SNick Piggin /** 1063db64fe02SNick Piggin * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer 1064db64fe02SNick Piggin * 1065db64fe02SNick Piggin * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily 1066db64fe02SNick Piggin * to amortize TLB flushing overheads. What this means is that any page you 1067db64fe02SNick Piggin * have now, may, in a former life, have been mapped into kernel virtual 1068db64fe02SNick Piggin * address by the vmap layer and so there might be some CPUs with TLB entries 1069db64fe02SNick Piggin * still referencing that page (additional to the regular 1:1 kernel mapping). 1070db64fe02SNick Piggin * 1071db64fe02SNick Piggin * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can 1072db64fe02SNick Piggin * be sure that none of the pages we have control over will have any aliases 1073db64fe02SNick Piggin * from the vmap layer. 1074db64fe02SNick Piggin */ 1075db64fe02SNick Piggin void vm_unmap_aliases(void) 1076db64fe02SNick Piggin { 1077db64fe02SNick Piggin unsigned long start = ULONG_MAX, end = 0; 1078db64fe02SNick Piggin int cpu; 1079db64fe02SNick Piggin int flush = 0; 1080db64fe02SNick Piggin 10819b463334SJeremy Fitzhardinge if (unlikely(!vmap_initialized)) 10829b463334SJeremy Fitzhardinge return; 10839b463334SJeremy Fitzhardinge 10845803ed29SChristoph Hellwig might_sleep(); 10855803ed29SChristoph Hellwig 1086db64fe02SNick Piggin for_each_possible_cpu(cpu) { 1087db64fe02SNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 1088db64fe02SNick Piggin struct vmap_block *vb; 1089db64fe02SNick Piggin 1090db64fe02SNick Piggin rcu_read_lock(); 1091db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 1092db64fe02SNick Piggin spin_lock(&vb->lock); 10937d61bfe8SRoman Pen if (vb->dirty) { 10947d61bfe8SRoman Pen unsigned long va_start = vb->va->va_start; 1095db64fe02SNick Piggin unsigned long s, e; 1096b136be5eSJoonsoo Kim 10977d61bfe8SRoman Pen s = va_start + (vb->dirty_min << PAGE_SHIFT); 10987d61bfe8SRoman Pen e = va_start + (vb->dirty_max << PAGE_SHIFT); 1099db64fe02SNick Piggin 11007d61bfe8SRoman Pen start = min(s, start); 11017d61bfe8SRoman Pen end = max(e, end); 11027d61bfe8SRoman Pen 1103db64fe02SNick Piggin flush = 1; 1104db64fe02SNick Piggin } 1105db64fe02SNick Piggin spin_unlock(&vb->lock); 1106db64fe02SNick Piggin } 1107db64fe02SNick Piggin rcu_read_unlock(); 1108db64fe02SNick Piggin } 1109db64fe02SNick Piggin 1110f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 11110574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 11120574ecd1SChristoph Hellwig if (!__purge_vmap_area_lazy(start, end) && flush) 11130574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 1114f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 1115db64fe02SNick Piggin } 1116db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases); 1117db64fe02SNick Piggin 1118db64fe02SNick Piggin /** 1119db64fe02SNick Piggin * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram 1120db64fe02SNick Piggin * @mem: the pointer returned by vm_map_ram 1121db64fe02SNick Piggin * @count: the count passed to that vm_map_ram call (cannot unmap partial) 1122db64fe02SNick Piggin */ 1123db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count) 1124db64fe02SNick Piggin { 112565ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 1126db64fe02SNick Piggin unsigned long addr = (unsigned long)mem; 11279c3acf60SChristoph Hellwig struct vmap_area *va; 1128db64fe02SNick Piggin 11295803ed29SChristoph Hellwig might_sleep(); 1130db64fe02SNick Piggin BUG_ON(!addr); 1131db64fe02SNick Piggin BUG_ON(addr < VMALLOC_START); 1132db64fe02SNick Piggin BUG_ON(addr > VMALLOC_END); 1133a1c0b1a0SShawn Lin BUG_ON(!PAGE_ALIGNED(addr)); 1134db64fe02SNick Piggin 11359c3acf60SChristoph Hellwig if (likely(count <= VMAP_MAX_ALLOC)) { 113605e3ff95SChintan Pandya debug_check_no_locks_freed(mem, size); 1137db64fe02SNick Piggin vb_free(mem, size); 11389c3acf60SChristoph Hellwig return; 11399c3acf60SChristoph Hellwig } 11409c3acf60SChristoph Hellwig 11419c3acf60SChristoph Hellwig va = find_vmap_area(addr); 11429c3acf60SChristoph Hellwig BUG_ON(!va); 114305e3ff95SChintan Pandya debug_check_no_locks_freed((void *)va->va_start, 114405e3ff95SChintan Pandya (va->va_end - va->va_start)); 11459c3acf60SChristoph Hellwig free_unmap_vmap_area(va); 1146db64fe02SNick Piggin } 1147db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram); 1148db64fe02SNick Piggin 1149db64fe02SNick Piggin /** 1150db64fe02SNick Piggin * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) 1151db64fe02SNick Piggin * @pages: an array of pointers to the pages to be mapped 1152db64fe02SNick Piggin * @count: number of pages 1153db64fe02SNick Piggin * @node: prefer to allocate data structures on this node 1154db64fe02SNick Piggin * @prot: memory protection to use. PAGE_KERNEL for regular RAM 1155e99c97adSRandy Dunlap * 115636437638SGioh Kim * If you use this function for less than VMAP_MAX_ALLOC pages, it could be 115736437638SGioh Kim * faster than vmap so it's good. But if you mix long-life and short-life 115836437638SGioh Kim * objects with vm_map_ram(), it could consume lots of address space through 115936437638SGioh Kim * fragmentation (especially on a 32bit machine). You could see failures in 116036437638SGioh Kim * the end. Please use this function for short-lived objects. 116136437638SGioh Kim * 1162e99c97adSRandy Dunlap * Returns: a pointer to the address that has been mapped, or %NULL on failure 1163db64fe02SNick Piggin */ 1164db64fe02SNick Piggin void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) 1165db64fe02SNick Piggin { 116665ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 1167db64fe02SNick Piggin unsigned long addr; 1168db64fe02SNick Piggin void *mem; 1169db64fe02SNick Piggin 1170db64fe02SNick Piggin if (likely(count <= VMAP_MAX_ALLOC)) { 1171db64fe02SNick Piggin mem = vb_alloc(size, GFP_KERNEL); 1172db64fe02SNick Piggin if (IS_ERR(mem)) 1173db64fe02SNick Piggin return NULL; 1174db64fe02SNick Piggin addr = (unsigned long)mem; 1175db64fe02SNick Piggin } else { 1176db64fe02SNick Piggin struct vmap_area *va; 1177db64fe02SNick Piggin va = alloc_vmap_area(size, PAGE_SIZE, 1178db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); 1179db64fe02SNick Piggin if (IS_ERR(va)) 1180db64fe02SNick Piggin return NULL; 1181db64fe02SNick Piggin 1182db64fe02SNick Piggin addr = va->va_start; 1183db64fe02SNick Piggin mem = (void *)addr; 1184db64fe02SNick Piggin } 1185db64fe02SNick Piggin if (vmap_page_range(addr, addr + size, prot, pages) < 0) { 1186db64fe02SNick Piggin vm_unmap_ram(mem, count); 1187db64fe02SNick Piggin return NULL; 1188db64fe02SNick Piggin } 1189db64fe02SNick Piggin return mem; 1190db64fe02SNick Piggin } 1191db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram); 1192db64fe02SNick Piggin 11934341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata; 1194*92eac168SMike Rapoport 1195f0aa6617STejun Heo /** 1196be9b7335SNicolas Pitre * vm_area_add_early - add vmap area early during boot 1197be9b7335SNicolas Pitre * @vm: vm_struct to add 1198be9b7335SNicolas Pitre * 1199be9b7335SNicolas Pitre * This function is used to add fixed kernel vm area to vmlist before 1200be9b7335SNicolas Pitre * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags 1201be9b7335SNicolas Pitre * should contain proper values and the other fields should be zero. 1202be9b7335SNicolas Pitre * 1203be9b7335SNicolas Pitre * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 1204be9b7335SNicolas Pitre */ 1205be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm) 1206be9b7335SNicolas Pitre { 1207be9b7335SNicolas Pitre struct vm_struct *tmp, **p; 1208be9b7335SNicolas Pitre 1209be9b7335SNicolas Pitre BUG_ON(vmap_initialized); 1210be9b7335SNicolas Pitre for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { 1211be9b7335SNicolas Pitre if (tmp->addr >= vm->addr) { 1212be9b7335SNicolas Pitre BUG_ON(tmp->addr < vm->addr + vm->size); 1213be9b7335SNicolas Pitre break; 1214be9b7335SNicolas Pitre } else 1215be9b7335SNicolas Pitre BUG_ON(tmp->addr + tmp->size > vm->addr); 1216be9b7335SNicolas Pitre } 1217be9b7335SNicolas Pitre vm->next = *p; 1218be9b7335SNicolas Pitre *p = vm; 1219be9b7335SNicolas Pitre } 1220be9b7335SNicolas Pitre 1221be9b7335SNicolas Pitre /** 1222f0aa6617STejun Heo * vm_area_register_early - register vmap area early during boot 1223f0aa6617STejun Heo * @vm: vm_struct to register 1224c0c0a293STejun Heo * @align: requested alignment 1225f0aa6617STejun Heo * 1226f0aa6617STejun Heo * This function is used to register kernel vm area before 1227f0aa6617STejun Heo * vmalloc_init() is called. @vm->size and @vm->flags should contain 1228f0aa6617STejun Heo * proper values on entry and other fields should be zero. On return, 1229f0aa6617STejun Heo * vm->addr contains the allocated address. 1230f0aa6617STejun Heo * 1231f0aa6617STejun Heo * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 1232f0aa6617STejun Heo */ 1233c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align) 1234f0aa6617STejun Heo { 1235f0aa6617STejun Heo static size_t vm_init_off __initdata; 1236c0c0a293STejun Heo unsigned long addr; 1237f0aa6617STejun Heo 1238c0c0a293STejun Heo addr = ALIGN(VMALLOC_START + vm_init_off, align); 1239c0c0a293STejun Heo vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START; 1240c0c0a293STejun Heo 1241c0c0a293STejun Heo vm->addr = (void *)addr; 1242f0aa6617STejun Heo 1243be9b7335SNicolas Pitre vm_area_add_early(vm); 1244f0aa6617STejun Heo } 1245f0aa6617STejun Heo 1246db64fe02SNick Piggin void __init vmalloc_init(void) 1247db64fe02SNick Piggin { 1248822c18f2SIvan Kokshaysky struct vmap_area *va; 1249822c18f2SIvan Kokshaysky struct vm_struct *tmp; 1250db64fe02SNick Piggin int i; 1251db64fe02SNick Piggin 1252db64fe02SNick Piggin for_each_possible_cpu(i) { 1253db64fe02SNick Piggin struct vmap_block_queue *vbq; 125432fcfd40SAl Viro struct vfree_deferred *p; 1255db64fe02SNick Piggin 1256db64fe02SNick Piggin vbq = &per_cpu(vmap_block_queue, i); 1257db64fe02SNick Piggin spin_lock_init(&vbq->lock); 1258db64fe02SNick Piggin INIT_LIST_HEAD(&vbq->free); 125932fcfd40SAl Viro p = &per_cpu(vfree_deferred, i); 126032fcfd40SAl Viro init_llist_head(&p->list); 126132fcfd40SAl Viro INIT_WORK(&p->wq, free_work); 1262db64fe02SNick Piggin } 12639b463334SJeremy Fitzhardinge 1264822c18f2SIvan Kokshaysky /* Import existing vmlist entries. */ 1265822c18f2SIvan Kokshaysky for (tmp = vmlist; tmp; tmp = tmp->next) { 126643ebdac4SPekka Enberg va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT); 1267dbda591dSKyongHo va->flags = VM_VM_AREA; 1268822c18f2SIvan Kokshaysky va->va_start = (unsigned long)tmp->addr; 1269822c18f2SIvan Kokshaysky va->va_end = va->va_start + tmp->size; 1270dbda591dSKyongHo va->vm = tmp; 1271822c18f2SIvan Kokshaysky __insert_vmap_area(va); 1272822c18f2SIvan Kokshaysky } 1273ca23e405STejun Heo 1274ca23e405STejun Heo vmap_area_pcpu_hole = VMALLOC_END; 1275ca23e405STejun Heo 12769b463334SJeremy Fitzhardinge vmap_initialized = true; 1277db64fe02SNick Piggin } 1278db64fe02SNick Piggin 12798fc48985STejun Heo /** 12808fc48985STejun Heo * map_kernel_range_noflush - map kernel VM area with the specified pages 12818fc48985STejun Heo * @addr: start of the VM area to map 12828fc48985STejun Heo * @size: size of the VM area to map 12838fc48985STejun Heo * @prot: page protection flags to use 12848fc48985STejun Heo * @pages: pages to map 12858fc48985STejun Heo * 12868fc48985STejun Heo * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size 12878fc48985STejun Heo * specify should have been allocated using get_vm_area() and its 12888fc48985STejun Heo * friends. 12898fc48985STejun Heo * 12908fc48985STejun Heo * NOTE: 12918fc48985STejun Heo * This function does NOT do any cache flushing. The caller is 12928fc48985STejun Heo * responsible for calling flush_cache_vmap() on to-be-mapped areas 12938fc48985STejun Heo * before calling this function. 12948fc48985STejun Heo * 12958fc48985STejun Heo * RETURNS: 12968fc48985STejun Heo * The number of pages mapped on success, -errno on failure. 12978fc48985STejun Heo */ 12988fc48985STejun Heo int map_kernel_range_noflush(unsigned long addr, unsigned long size, 12998fc48985STejun Heo pgprot_t prot, struct page **pages) 13008fc48985STejun Heo { 13018fc48985STejun Heo return vmap_page_range_noflush(addr, addr + size, prot, pages); 13028fc48985STejun Heo } 13038fc48985STejun Heo 13048fc48985STejun Heo /** 13058fc48985STejun Heo * unmap_kernel_range_noflush - unmap kernel VM area 13068fc48985STejun Heo * @addr: start of the VM area to unmap 13078fc48985STejun Heo * @size: size of the VM area to unmap 13088fc48985STejun Heo * 13098fc48985STejun Heo * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size 13108fc48985STejun Heo * specify should have been allocated using get_vm_area() and its 13118fc48985STejun Heo * friends. 13128fc48985STejun Heo * 13138fc48985STejun Heo * NOTE: 13148fc48985STejun Heo * This function does NOT do any cache flushing. The caller is 13158fc48985STejun Heo * responsible for calling flush_cache_vunmap() on to-be-mapped areas 13168fc48985STejun Heo * before calling this function and flush_tlb_kernel_range() after. 13178fc48985STejun Heo */ 13188fc48985STejun Heo void unmap_kernel_range_noflush(unsigned long addr, unsigned long size) 13198fc48985STejun Heo { 13208fc48985STejun Heo vunmap_page_range(addr, addr + size); 13218fc48985STejun Heo } 132281e88fdcSHuang Ying EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush); 13238fc48985STejun Heo 13248fc48985STejun Heo /** 13258fc48985STejun Heo * unmap_kernel_range - unmap kernel VM area and flush cache and TLB 13268fc48985STejun Heo * @addr: start of the VM area to unmap 13278fc48985STejun Heo * @size: size of the VM area to unmap 13288fc48985STejun Heo * 13298fc48985STejun Heo * Similar to unmap_kernel_range_noflush() but flushes vcache before 13308fc48985STejun Heo * the unmapping and tlb after. 13318fc48985STejun Heo */ 1332db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size) 1333db64fe02SNick Piggin { 1334db64fe02SNick Piggin unsigned long end = addr + size; 1335f6fcba70STejun Heo 1336f6fcba70STejun Heo flush_cache_vunmap(addr, end); 1337db64fe02SNick Piggin vunmap_page_range(addr, end); 1338db64fe02SNick Piggin flush_tlb_kernel_range(addr, end); 1339db64fe02SNick Piggin } 134093ef6d6cSMinchan Kim EXPORT_SYMBOL_GPL(unmap_kernel_range); 1341db64fe02SNick Piggin 1342f6f8ed47SWANG Chao int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages) 1343db64fe02SNick Piggin { 1344db64fe02SNick Piggin unsigned long addr = (unsigned long)area->addr; 1345762216abSWanpeng Li unsigned long end = addr + get_vm_area_size(area); 1346db64fe02SNick Piggin int err; 1347db64fe02SNick Piggin 1348f6f8ed47SWANG Chao err = vmap_page_range(addr, end, prot, pages); 1349db64fe02SNick Piggin 1350f6f8ed47SWANG Chao return err > 0 ? 0 : err; 1351db64fe02SNick Piggin } 1352db64fe02SNick Piggin EXPORT_SYMBOL_GPL(map_vm_area); 1353db64fe02SNick Piggin 1354f5252e00SMitsuo Hayasaka static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, 13555e6cafc8SMarek Szyprowski unsigned long flags, const void *caller) 1356cf88c790STejun Heo { 1357c69480adSJoonsoo Kim spin_lock(&vmap_area_lock); 1358cf88c790STejun Heo vm->flags = flags; 1359cf88c790STejun Heo vm->addr = (void *)va->va_start; 1360cf88c790STejun Heo vm->size = va->va_end - va->va_start; 1361cf88c790STejun Heo vm->caller = caller; 1362db1aecafSMinchan Kim va->vm = vm; 1363cf88c790STejun Heo va->flags |= VM_VM_AREA; 1364c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 1365f5252e00SMitsuo Hayasaka } 1366cf88c790STejun Heo 136720fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm) 1368f5252e00SMitsuo Hayasaka { 1369d4033afdSJoonsoo Kim /* 137020fc02b4SZhang Yanfei * Before removing VM_UNINITIALIZED, 1371d4033afdSJoonsoo Kim * we should make sure that vm has proper values. 1372d4033afdSJoonsoo Kim * Pair with smp_rmb() in show_numa_info(). 1373d4033afdSJoonsoo Kim */ 1374d4033afdSJoonsoo Kim smp_wmb(); 137520fc02b4SZhang Yanfei vm->flags &= ~VM_UNINITIALIZED; 1376cf88c790STejun Heo } 1377cf88c790STejun Heo 1378db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size, 13792dca6999SDavid Miller unsigned long align, unsigned long flags, unsigned long start, 13805e6cafc8SMarek Szyprowski unsigned long end, int node, gfp_t gfp_mask, const void *caller) 1381db64fe02SNick Piggin { 13820006526dSKautuk Consul struct vmap_area *va; 1383db64fe02SNick Piggin struct vm_struct *area; 13841da177e4SLinus Torvalds 138552fd24caSGiridhar Pemmasani BUG_ON(in_interrupt()); 13861da177e4SLinus Torvalds size = PAGE_ALIGN(size); 138731be8309SOGAWA Hirofumi if (unlikely(!size)) 138831be8309SOGAWA Hirofumi return NULL; 13891da177e4SLinus Torvalds 1390252e5c6eSzijun_hu if (flags & VM_IOREMAP) 1391252e5c6eSzijun_hu align = 1ul << clamp_t(int, get_count_order_long(size), 1392252e5c6eSzijun_hu PAGE_SHIFT, IOREMAP_MAX_ORDER); 1393252e5c6eSzijun_hu 1394cf88c790STejun Heo area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); 13951da177e4SLinus Torvalds if (unlikely(!area)) 13961da177e4SLinus Torvalds return NULL; 13971da177e4SLinus Torvalds 139871394fe5SAndrey Ryabinin if (!(flags & VM_NO_GUARD)) 13991da177e4SLinus Torvalds size += PAGE_SIZE; 14001da177e4SLinus Torvalds 1401db64fe02SNick Piggin va = alloc_vmap_area(size, align, start, end, node, gfp_mask); 1402db64fe02SNick Piggin if (IS_ERR(va)) { 1403db64fe02SNick Piggin kfree(area); 1404db64fe02SNick Piggin return NULL; 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 1407f5252e00SMitsuo Hayasaka setup_vmalloc_vm(area, va, flags, caller); 1408f5252e00SMitsuo Hayasaka 14091da177e4SLinus Torvalds return area; 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds 1412930fc45aSChristoph Lameter struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags, 1413930fc45aSChristoph Lameter unsigned long start, unsigned long end) 1414930fc45aSChristoph Lameter { 141500ef2d2fSDavid Rientjes return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE, 141600ef2d2fSDavid Rientjes GFP_KERNEL, __builtin_return_address(0)); 1417930fc45aSChristoph Lameter } 14185992b6daSRusty Russell EXPORT_SYMBOL_GPL(__get_vm_area); 1419930fc45aSChristoph Lameter 1420c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, 1421c2968612SBenjamin Herrenschmidt unsigned long start, unsigned long end, 14225e6cafc8SMarek Szyprowski const void *caller) 1423c2968612SBenjamin Herrenschmidt { 142400ef2d2fSDavid Rientjes return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE, 142500ef2d2fSDavid Rientjes GFP_KERNEL, caller); 1426c2968612SBenjamin Herrenschmidt } 1427c2968612SBenjamin Herrenschmidt 14281da177e4SLinus Torvalds /** 1429183ff22bSSimon Arlott * get_vm_area - reserve a contiguous kernel virtual area 14301da177e4SLinus Torvalds * @size: size of the area 14311da177e4SLinus Torvalds * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC 14321da177e4SLinus Torvalds * 14331da177e4SLinus Torvalds * Search an area of @size in the kernel virtual mapping area, 14341da177e4SLinus Torvalds * and reserved it for out purposes. Returns the area descriptor 14351da177e4SLinus Torvalds * on success or %NULL on failure. 14361da177e4SLinus Torvalds */ 14371da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) 14381da177e4SLinus Torvalds { 14392dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 144000ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, 144100ef2d2fSDavid Rientjes __builtin_return_address(0)); 144223016969SChristoph Lameter } 144323016969SChristoph Lameter 144423016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, 14455e6cafc8SMarek Szyprowski const void *caller) 144623016969SChristoph Lameter { 14472dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 144800ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, caller); 14491da177e4SLinus Torvalds } 14501da177e4SLinus Torvalds 1451e9da6e99SMarek Szyprowski /** 1452e9da6e99SMarek Szyprowski * find_vm_area - find a continuous kernel virtual area 1453e9da6e99SMarek Szyprowski * @addr: base address 1454e9da6e99SMarek Szyprowski * 1455e9da6e99SMarek Szyprowski * Search for the kernel VM area starting at @addr, and return it. 1456e9da6e99SMarek Szyprowski * It is up to the caller to do all required locking to keep the returned 1457e9da6e99SMarek Szyprowski * pointer valid. 1458e9da6e99SMarek Szyprowski */ 1459e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr) 146083342314SNick Piggin { 1461db64fe02SNick Piggin struct vmap_area *va; 146283342314SNick Piggin 1463db64fe02SNick Piggin va = find_vmap_area((unsigned long)addr); 1464db64fe02SNick Piggin if (va && va->flags & VM_VM_AREA) 1465db1aecafSMinchan Kim return va->vm; 146683342314SNick Piggin 14677856dfebSAndi Kleen return NULL; 14687856dfebSAndi Kleen } 14697856dfebSAndi Kleen 14701da177e4SLinus Torvalds /** 1471183ff22bSSimon Arlott * remove_vm_area - find and remove a continuous kernel virtual area 14721da177e4SLinus Torvalds * @addr: base address 14731da177e4SLinus Torvalds * 14741da177e4SLinus Torvalds * Search for the kernel VM area starting at @addr, and remove it. 14751da177e4SLinus Torvalds * This function returns the found VM area, but using it is NOT safe 14767856dfebSAndi Kleen * on SMP machines, except for its size or flags. 14771da177e4SLinus Torvalds */ 1478b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr) 14791da177e4SLinus Torvalds { 1480db64fe02SNick Piggin struct vmap_area *va; 1481db64fe02SNick Piggin 14825803ed29SChristoph Hellwig might_sleep(); 14835803ed29SChristoph Hellwig 1484db64fe02SNick Piggin va = find_vmap_area((unsigned long)addr); 1485db64fe02SNick Piggin if (va && va->flags & VM_VM_AREA) { 1486db1aecafSMinchan Kim struct vm_struct *vm = va->vm; 1487f5252e00SMitsuo Hayasaka 1488c69480adSJoonsoo Kim spin_lock(&vmap_area_lock); 1489c69480adSJoonsoo Kim va->vm = NULL; 1490c69480adSJoonsoo Kim va->flags &= ~VM_VM_AREA; 149178c72746SYisheng Xie va->flags |= VM_LAZY_FREE; 1492c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 1493c69480adSJoonsoo Kim 1494a5af5aa8SAndrey Ryabinin kasan_free_shadow(vm); 1495dd32c279SKAMEZAWA Hiroyuki free_unmap_vmap_area(va); 1496dd32c279SKAMEZAWA Hiroyuki 1497db64fe02SNick Piggin return vm; 1498db64fe02SNick Piggin } 1499db64fe02SNick Piggin return NULL; 15001da177e4SLinus Torvalds } 15011da177e4SLinus Torvalds 1502b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages) 15031da177e4SLinus Torvalds { 15041da177e4SLinus Torvalds struct vm_struct *area; 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds if (!addr) 15071da177e4SLinus Torvalds return; 15081da177e4SLinus Torvalds 1509e69e9d4aSHATAYAMA Daisuke if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n", 1510ab15d9b4SDan Carpenter addr)) 15111da177e4SLinus Torvalds return; 15121da177e4SLinus Torvalds 15136ade2032SLiviu Dudau area = find_vm_area(addr); 15141da177e4SLinus Torvalds if (unlikely(!area)) { 15154c8573e2SArjan van de Ven WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", 15161da177e4SLinus Torvalds addr); 15171da177e4SLinus Torvalds return; 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds 152005e3ff95SChintan Pandya debug_check_no_locks_freed(area->addr, get_vm_area_size(area)); 152105e3ff95SChintan Pandya debug_check_no_obj_freed(area->addr, get_vm_area_size(area)); 15229a11b49aSIngo Molnar 1523f3c01d2fSChintan Pandya remove_vm_area(addr); 15241da177e4SLinus Torvalds if (deallocate_pages) { 15251da177e4SLinus Torvalds int i; 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds for (i = 0; i < area->nr_pages; i++) { 1528bf53d6f8SChristoph Lameter struct page *page = area->pages[i]; 1529bf53d6f8SChristoph Lameter 1530bf53d6f8SChristoph Lameter BUG_ON(!page); 15314949148aSVladimir Davydov __free_pages(page, 0); 15321da177e4SLinus Torvalds } 15331da177e4SLinus Torvalds 1534244d63eeSDavid Rientjes kvfree(area->pages); 15351da177e4SLinus Torvalds } 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds kfree(area); 15381da177e4SLinus Torvalds return; 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds 1541bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr) 1542bf22e37aSAndrey Ryabinin { 1543bf22e37aSAndrey Ryabinin /* 1544bf22e37aSAndrey Ryabinin * Use raw_cpu_ptr() because this can be called from preemptible 1545bf22e37aSAndrey Ryabinin * context. Preemption is absolutely fine here, because the llist_add() 1546bf22e37aSAndrey Ryabinin * implementation is lockless, so it works even if we are adding to 1547bf22e37aSAndrey Ryabinin * nother cpu's list. schedule_work() should be fine with this too. 1548bf22e37aSAndrey Ryabinin */ 1549bf22e37aSAndrey Ryabinin struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred); 1550bf22e37aSAndrey Ryabinin 1551bf22e37aSAndrey Ryabinin if (llist_add((struct llist_node *)addr, &p->list)) 1552bf22e37aSAndrey Ryabinin schedule_work(&p->wq); 1553bf22e37aSAndrey Ryabinin } 1554bf22e37aSAndrey Ryabinin 1555bf22e37aSAndrey Ryabinin /** 1556bf22e37aSAndrey Ryabinin * vfree_atomic - release memory allocated by vmalloc() 1557bf22e37aSAndrey Ryabinin * @addr: memory base address 1558bf22e37aSAndrey Ryabinin * 1559bf22e37aSAndrey Ryabinin * This one is just like vfree() but can be called in any atomic context 1560bf22e37aSAndrey Ryabinin * except NMIs. 1561bf22e37aSAndrey Ryabinin */ 1562bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr) 1563bf22e37aSAndrey Ryabinin { 1564bf22e37aSAndrey Ryabinin BUG_ON(in_nmi()); 1565bf22e37aSAndrey Ryabinin 1566bf22e37aSAndrey Ryabinin kmemleak_free(addr); 1567bf22e37aSAndrey Ryabinin 1568bf22e37aSAndrey Ryabinin if (!addr) 1569bf22e37aSAndrey Ryabinin return; 1570bf22e37aSAndrey Ryabinin __vfree_deferred(addr); 1571bf22e37aSAndrey Ryabinin } 1572bf22e37aSAndrey Ryabinin 1573c67dc624SRoman Penyaev static void __vfree(const void *addr) 1574c67dc624SRoman Penyaev { 1575c67dc624SRoman Penyaev if (unlikely(in_interrupt())) 1576c67dc624SRoman Penyaev __vfree_deferred(addr); 1577c67dc624SRoman Penyaev else 1578c67dc624SRoman Penyaev __vunmap(addr, 1); 1579c67dc624SRoman Penyaev } 1580c67dc624SRoman Penyaev 15811da177e4SLinus Torvalds /** 15821da177e4SLinus Torvalds * vfree - release memory allocated by vmalloc() 15831da177e4SLinus Torvalds * @addr: memory base address 15841da177e4SLinus Torvalds * 1585183ff22bSSimon Arlott * Free the virtually continuous memory area starting at @addr, as 158680e93effSPekka Enberg * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is 158780e93effSPekka Enberg * NULL, no operation is performed. 15881da177e4SLinus Torvalds * 158932fcfd40SAl Viro * Must not be called in NMI context (strictly speaking, only if we don't 159032fcfd40SAl Viro * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling 159132fcfd40SAl Viro * conventions for vfree() arch-depenedent would be a really bad idea) 159232fcfd40SAl Viro * 15933ca4ea3aSAndrey Ryabinin * May sleep if called *not* from interrupt context. 15943ca4ea3aSAndrey Ryabinin * 15950e056eb5Smchehab@s-opensource.com * NOTE: assumes that the object at @addr has a size >= sizeof(llist_node) 15961da177e4SLinus Torvalds */ 1597b3bdda02SChristoph Lameter void vfree(const void *addr) 15981da177e4SLinus Torvalds { 159932fcfd40SAl Viro BUG_ON(in_nmi()); 160089219d37SCatalin Marinas 160189219d37SCatalin Marinas kmemleak_free(addr); 160289219d37SCatalin Marinas 1603a8dda165SAndrey Ryabinin might_sleep_if(!in_interrupt()); 1604a8dda165SAndrey Ryabinin 160532fcfd40SAl Viro if (!addr) 160632fcfd40SAl Viro return; 1607c67dc624SRoman Penyaev 1608c67dc624SRoman Penyaev __vfree(addr); 16091da177e4SLinus Torvalds } 16101da177e4SLinus Torvalds EXPORT_SYMBOL(vfree); 16111da177e4SLinus Torvalds 16121da177e4SLinus Torvalds /** 16131da177e4SLinus Torvalds * vunmap - release virtual mapping obtained by vmap() 16141da177e4SLinus Torvalds * @addr: memory base address 16151da177e4SLinus Torvalds * 16161da177e4SLinus Torvalds * Free the virtually contiguous memory area starting at @addr, 16171da177e4SLinus Torvalds * which was created from the page array passed to vmap(). 16181da177e4SLinus Torvalds * 161980e93effSPekka Enberg * Must not be called in interrupt context. 16201da177e4SLinus Torvalds */ 1621b3bdda02SChristoph Lameter void vunmap(const void *addr) 16221da177e4SLinus Torvalds { 16231da177e4SLinus Torvalds BUG_ON(in_interrupt()); 162434754b69SPeter Zijlstra might_sleep(); 162532fcfd40SAl Viro if (addr) 16261da177e4SLinus Torvalds __vunmap(addr, 0); 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap); 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds /** 16311da177e4SLinus Torvalds * vmap - map an array of pages into virtually contiguous space 16321da177e4SLinus Torvalds * @pages: array of page pointers 16331da177e4SLinus Torvalds * @count: number of pages to map 16341da177e4SLinus Torvalds * @flags: vm_area->flags 16351da177e4SLinus Torvalds * @prot: page protection for the mapping 16361da177e4SLinus Torvalds * 16371da177e4SLinus Torvalds * Maps @count pages from @pages into contiguous kernel virtual 16381da177e4SLinus Torvalds * space. 16391da177e4SLinus Torvalds */ 16401da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, 16411da177e4SLinus Torvalds unsigned long flags, pgprot_t prot) 16421da177e4SLinus Torvalds { 16431da177e4SLinus Torvalds struct vm_struct *area; 164465ee03c4SGuillermo Julián Moreno unsigned long size; /* In bytes */ 16451da177e4SLinus Torvalds 164634754b69SPeter Zijlstra might_sleep(); 164734754b69SPeter Zijlstra 1648ca79b0c2SArun KS if (count > totalram_pages()) 16491da177e4SLinus Torvalds return NULL; 16501da177e4SLinus Torvalds 165165ee03c4SGuillermo Julián Moreno size = (unsigned long)count << PAGE_SHIFT; 165265ee03c4SGuillermo Julián Moreno area = get_vm_area_caller(size, flags, __builtin_return_address(0)); 16531da177e4SLinus Torvalds if (!area) 16541da177e4SLinus Torvalds return NULL; 165523016969SChristoph Lameter 1656f6f8ed47SWANG Chao if (map_vm_area(area, prot, pages)) { 16571da177e4SLinus Torvalds vunmap(area->addr); 16581da177e4SLinus Torvalds return NULL; 16591da177e4SLinus Torvalds } 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds return area->addr; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds EXPORT_SYMBOL(vmap); 16641da177e4SLinus Torvalds 16658594a21cSMichal Hocko static void *__vmalloc_node(unsigned long size, unsigned long align, 16668594a21cSMichal Hocko gfp_t gfp_mask, pgprot_t prot, 16678594a21cSMichal Hocko int node, const void *caller); 1668e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, 16693722e13cSWanpeng Li pgprot_t prot, int node) 16701da177e4SLinus Torvalds { 16711da177e4SLinus Torvalds struct page **pages; 16721da177e4SLinus Torvalds unsigned int nr_pages, array_size, i; 1673930f036bSDavid Rientjes const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; 1674704b862fSLaura Abbott const gfp_t alloc_mask = gfp_mask | __GFP_NOWARN; 1675704b862fSLaura Abbott const gfp_t highmem_mask = (gfp_mask & (GFP_DMA | GFP_DMA32)) ? 1676704b862fSLaura Abbott 0 : 1677704b862fSLaura Abbott __GFP_HIGHMEM; 16781da177e4SLinus Torvalds 1679762216abSWanpeng Li nr_pages = get_vm_area_size(area) >> PAGE_SHIFT; 16801da177e4SLinus Torvalds array_size = (nr_pages * sizeof(struct page *)); 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds area->nr_pages = nr_pages; 16831da177e4SLinus Torvalds /* Please note that the recursion is strictly bounded. */ 16848757d5faSJan Kiszka if (array_size > PAGE_SIZE) { 1685704b862fSLaura Abbott pages = __vmalloc_node(array_size, 1, nested_gfp|highmem_mask, 16863722e13cSWanpeng Li PAGE_KERNEL, node, area->caller); 1687286e1ea3SAndrew Morton } else { 1688976d6dfbSJan Beulich pages = kmalloc_node(array_size, nested_gfp, node); 1689286e1ea3SAndrew Morton } 16901da177e4SLinus Torvalds area->pages = pages; 16911da177e4SLinus Torvalds if (!area->pages) { 16921da177e4SLinus Torvalds remove_vm_area(area->addr); 16931da177e4SLinus Torvalds kfree(area); 16941da177e4SLinus Torvalds return NULL; 16951da177e4SLinus Torvalds } 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds for (i = 0; i < area->nr_pages; i++) { 1698bf53d6f8SChristoph Lameter struct page *page; 1699bf53d6f8SChristoph Lameter 17004b90951cSJianguo Wu if (node == NUMA_NO_NODE) 1701704b862fSLaura Abbott page = alloc_page(alloc_mask|highmem_mask); 1702930fc45aSChristoph Lameter else 1703704b862fSLaura Abbott page = alloc_pages_node(node, alloc_mask|highmem_mask, 0); 1704bf53d6f8SChristoph Lameter 1705bf53d6f8SChristoph Lameter if (unlikely(!page)) { 17061da177e4SLinus Torvalds /* Successfully allocated i pages, free them in __vunmap() */ 17071da177e4SLinus Torvalds area->nr_pages = i; 17081da177e4SLinus Torvalds goto fail; 17091da177e4SLinus Torvalds } 1710bf53d6f8SChristoph Lameter area->pages[i] = page; 1711704b862fSLaura Abbott if (gfpflags_allow_blocking(gfp_mask|highmem_mask)) 1712660654f9SEric Dumazet cond_resched(); 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds 1715f6f8ed47SWANG Chao if (map_vm_area(area, prot, pages)) 17161da177e4SLinus Torvalds goto fail; 17171da177e4SLinus Torvalds return area->addr; 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds fail: 1720a8e99259SMichal Hocko warn_alloc(gfp_mask, NULL, 17217877cdccSMichal Hocko "vmalloc: allocation failure, allocated %ld of %ld bytes", 172222943ab1SDave Hansen (area->nr_pages*PAGE_SIZE), area->size); 1723c67dc624SRoman Penyaev __vfree(area->addr); 17241da177e4SLinus Torvalds return NULL; 17251da177e4SLinus Torvalds } 17261da177e4SLinus Torvalds 1727d0a21265SDavid Rientjes /** 1728d0a21265SDavid Rientjes * __vmalloc_node_range - allocate virtually contiguous memory 1729d0a21265SDavid Rientjes * @size: allocation size 1730d0a21265SDavid Rientjes * @align: desired alignment 1731d0a21265SDavid Rientjes * @start: vm area range start 1732d0a21265SDavid Rientjes * @end: vm area range end 1733d0a21265SDavid Rientjes * @gfp_mask: flags for the page level allocator 1734d0a21265SDavid Rientjes * @prot: protection mask for the allocated pages 1735cb9e3c29SAndrey Ryabinin * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD) 173600ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 1737d0a21265SDavid Rientjes * @caller: caller's return address 1738d0a21265SDavid Rientjes * 1739d0a21265SDavid Rientjes * Allocate enough pages to cover @size from the page level 1740d0a21265SDavid Rientjes * allocator with @gfp_mask flags. Map them into contiguous 1741d0a21265SDavid Rientjes * kernel virtual space, using a pagetable protection of @prot. 1742d0a21265SDavid Rientjes */ 1743d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align, 1744d0a21265SDavid Rientjes unsigned long start, unsigned long end, gfp_t gfp_mask, 1745cb9e3c29SAndrey Ryabinin pgprot_t prot, unsigned long vm_flags, int node, 1746cb9e3c29SAndrey Ryabinin const void *caller) 1747930fc45aSChristoph Lameter { 1748d0a21265SDavid Rientjes struct vm_struct *area; 1749d0a21265SDavid Rientjes void *addr; 1750d0a21265SDavid Rientjes unsigned long real_size = size; 1751d0a21265SDavid Rientjes 1752d0a21265SDavid Rientjes size = PAGE_ALIGN(size); 1753ca79b0c2SArun KS if (!size || (size >> PAGE_SHIFT) > totalram_pages()) 1754de7d2b56SJoe Perches goto fail; 1755d0a21265SDavid Rientjes 1756cb9e3c29SAndrey Ryabinin area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED | 1757cb9e3c29SAndrey Ryabinin vm_flags, start, end, node, gfp_mask, caller); 1758d0a21265SDavid Rientjes if (!area) 1759de7d2b56SJoe Perches goto fail; 1760d0a21265SDavid Rientjes 17613722e13cSWanpeng Li addr = __vmalloc_area_node(area, gfp_mask, prot, node); 17621368edf0SMel Gorman if (!addr) 1763b82225f3SWanpeng Li return NULL; 176489219d37SCatalin Marinas 176589219d37SCatalin Marinas /* 176620fc02b4SZhang Yanfei * In this function, newly allocated vm_struct has VM_UNINITIALIZED 176720fc02b4SZhang Yanfei * flag. It means that vm_struct is not fully initialized. 17684341fa45SJoonsoo Kim * Now, it is fully initialized, so remove this flag here. 1769f5252e00SMitsuo Hayasaka */ 177020fc02b4SZhang Yanfei clear_vm_uninitialized_flag(area); 1771f5252e00SMitsuo Hayasaka 177294f4a161SCatalin Marinas kmemleak_vmalloc(area, size, gfp_mask); 177389219d37SCatalin Marinas 177489219d37SCatalin Marinas return addr; 1775de7d2b56SJoe Perches 1776de7d2b56SJoe Perches fail: 1777a8e99259SMichal Hocko warn_alloc(gfp_mask, NULL, 17787877cdccSMichal Hocko "vmalloc: allocation failure: %lu bytes", real_size); 1779de7d2b56SJoe Perches return NULL; 1780930fc45aSChristoph Lameter } 1781930fc45aSChristoph Lameter 1782153178edSUladzislau Rezki (Sony) /* 1783153178edSUladzislau Rezki (Sony) * This is only for performance analysis of vmalloc and stress purpose. 1784153178edSUladzislau Rezki (Sony) * It is required by vmalloc test module, therefore do not use it other 1785153178edSUladzislau Rezki (Sony) * than that. 1786153178edSUladzislau Rezki (Sony) */ 1787153178edSUladzislau Rezki (Sony) #ifdef CONFIG_TEST_VMALLOC_MODULE 1788153178edSUladzislau Rezki (Sony) EXPORT_SYMBOL_GPL(__vmalloc_node_range); 1789153178edSUladzislau Rezki (Sony) #endif 1790153178edSUladzislau Rezki (Sony) 17911da177e4SLinus Torvalds /** 1792930fc45aSChristoph Lameter * __vmalloc_node - allocate virtually contiguous memory 17931da177e4SLinus Torvalds * @size: allocation size 17942dca6999SDavid Miller * @align: desired alignment 17951da177e4SLinus Torvalds * @gfp_mask: flags for the page level allocator 17961da177e4SLinus Torvalds * @prot: protection mask for the allocated pages 179700ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 1798c85d194bSRandy Dunlap * @caller: caller's return address 17991da177e4SLinus Torvalds * 18001da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 18011da177e4SLinus Torvalds * allocator with @gfp_mask flags. Map them into contiguous 18021da177e4SLinus Torvalds * kernel virtual space, using a pagetable protection of @prot. 1803a7c3e901SMichal Hocko * 1804dcda9b04SMichal Hocko * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL 1805a7c3e901SMichal Hocko * and __GFP_NOFAIL are not supported 1806a7c3e901SMichal Hocko * 1807a7c3e901SMichal Hocko * Any use of gfp flags outside of GFP_KERNEL should be consulted 1808a7c3e901SMichal Hocko * with mm people. 18091da177e4SLinus Torvalds */ 18108594a21cSMichal Hocko static void *__vmalloc_node(unsigned long size, unsigned long align, 18112dca6999SDavid Miller gfp_t gfp_mask, pgprot_t prot, 18125e6cafc8SMarek Szyprowski int node, const void *caller) 18131da177e4SLinus Torvalds { 1814d0a21265SDavid Rientjes return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, 1815cb9e3c29SAndrey Ryabinin gfp_mask, prot, 0, node, caller); 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds 1818930fc45aSChristoph Lameter void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) 1819930fc45aSChristoph Lameter { 182000ef2d2fSDavid Rientjes return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE, 182123016969SChristoph Lameter __builtin_return_address(0)); 1822930fc45aSChristoph Lameter } 18231da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc); 18241da177e4SLinus Torvalds 18258594a21cSMichal Hocko static inline void *__vmalloc_node_flags(unsigned long size, 18268594a21cSMichal Hocko int node, gfp_t flags) 18278594a21cSMichal Hocko { 18288594a21cSMichal Hocko return __vmalloc_node(size, 1, flags, PAGE_KERNEL, 18298594a21cSMichal Hocko node, __builtin_return_address(0)); 18308594a21cSMichal Hocko } 18318594a21cSMichal Hocko 18328594a21cSMichal Hocko 18338594a21cSMichal Hocko void *__vmalloc_node_flags_caller(unsigned long size, int node, gfp_t flags, 18348594a21cSMichal Hocko void *caller) 18358594a21cSMichal Hocko { 18368594a21cSMichal Hocko return __vmalloc_node(size, 1, flags, PAGE_KERNEL, node, caller); 18378594a21cSMichal Hocko } 18388594a21cSMichal Hocko 18391da177e4SLinus Torvalds /** 18401da177e4SLinus Torvalds * vmalloc - allocate virtually contiguous memory 18411da177e4SLinus Torvalds * @size: allocation size 1842*92eac168SMike Rapoport * 18431da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 18441da177e4SLinus Torvalds * allocator and map them into contiguous kernel virtual space. 18451da177e4SLinus Torvalds * 1846c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 18471da177e4SLinus Torvalds * use __vmalloc() instead. 18481da177e4SLinus Torvalds */ 18491da177e4SLinus Torvalds void *vmalloc(unsigned long size) 18501da177e4SLinus Torvalds { 185100ef2d2fSDavid Rientjes return __vmalloc_node_flags(size, NUMA_NO_NODE, 185219809c2dSMichal Hocko GFP_KERNEL); 18531da177e4SLinus Torvalds } 18541da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc); 18551da177e4SLinus Torvalds 1856930fc45aSChristoph Lameter /** 1857e1ca7788SDave Young * vzalloc - allocate virtually contiguous memory with zero fill 1858e1ca7788SDave Young * @size: allocation size 1859*92eac168SMike Rapoport * 1860e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 1861e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 1862e1ca7788SDave Young * The memory allocated is set to zero. 1863e1ca7788SDave Young * 1864e1ca7788SDave Young * For tight control over page level allocator and protection flags 1865e1ca7788SDave Young * use __vmalloc() instead. 1866e1ca7788SDave Young */ 1867e1ca7788SDave Young void *vzalloc(unsigned long size) 1868e1ca7788SDave Young { 186900ef2d2fSDavid Rientjes return __vmalloc_node_flags(size, NUMA_NO_NODE, 187019809c2dSMichal Hocko GFP_KERNEL | __GFP_ZERO); 1871e1ca7788SDave Young } 1872e1ca7788SDave Young EXPORT_SYMBOL(vzalloc); 1873e1ca7788SDave Young 1874e1ca7788SDave Young /** 1875ead04089SRolf Eike Beer * vmalloc_user - allocate zeroed virtually contiguous memory for userspace 187683342314SNick Piggin * @size: allocation size 1877ead04089SRolf Eike Beer * 1878ead04089SRolf Eike Beer * The resulting memory area is zeroed so it can be mapped to userspace 1879ead04089SRolf Eike Beer * without leaking data. 188083342314SNick Piggin */ 188183342314SNick Piggin void *vmalloc_user(unsigned long size) 188283342314SNick Piggin { 1883bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 1884bc84c535SRoman Penyaev GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL, 1885bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 188600ef2d2fSDavid Rientjes __builtin_return_address(0)); 188783342314SNick Piggin } 188883342314SNick Piggin EXPORT_SYMBOL(vmalloc_user); 188983342314SNick Piggin 189083342314SNick Piggin /** 1891930fc45aSChristoph Lameter * vmalloc_node - allocate memory on a specific node 1892930fc45aSChristoph Lameter * @size: allocation size 1893d44e0780SRandy Dunlap * @node: numa node 1894930fc45aSChristoph Lameter * 1895930fc45aSChristoph Lameter * Allocate enough pages to cover @size from the page level 1896930fc45aSChristoph Lameter * allocator and map them into contiguous kernel virtual space. 1897930fc45aSChristoph Lameter * 1898c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 1899930fc45aSChristoph Lameter * use __vmalloc() instead. 1900930fc45aSChristoph Lameter */ 1901930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node) 1902930fc45aSChristoph Lameter { 190319809c2dSMichal Hocko return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL, 190423016969SChristoph Lameter node, __builtin_return_address(0)); 1905930fc45aSChristoph Lameter } 1906930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node); 1907930fc45aSChristoph Lameter 1908e1ca7788SDave Young /** 1909e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill 1910e1ca7788SDave Young * @size: allocation size 1911e1ca7788SDave Young * @node: numa node 1912e1ca7788SDave Young * 1913e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 1914e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 1915e1ca7788SDave Young * The memory allocated is set to zero. 1916e1ca7788SDave Young * 1917e1ca7788SDave Young * For tight control over page level allocator and protection flags 1918e1ca7788SDave Young * use __vmalloc_node() instead. 1919e1ca7788SDave Young */ 1920e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node) 1921e1ca7788SDave Young { 1922e1ca7788SDave Young return __vmalloc_node_flags(size, node, 192319809c2dSMichal Hocko GFP_KERNEL | __GFP_ZERO); 1924e1ca7788SDave Young } 1925e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node); 1926e1ca7788SDave Young 19271da177e4SLinus Torvalds /** 19281da177e4SLinus Torvalds * vmalloc_exec - allocate virtually contiguous, executable memory 19291da177e4SLinus Torvalds * @size: allocation size 19301da177e4SLinus Torvalds * 19311da177e4SLinus Torvalds * Kernel-internal function to allocate enough pages to cover @size 19321da177e4SLinus Torvalds * the page level allocator and map them into contiguous and 19331da177e4SLinus Torvalds * executable kernel virtual space. 19341da177e4SLinus Torvalds * 1935c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 19361da177e4SLinus Torvalds * use __vmalloc() instead. 19371da177e4SLinus Torvalds */ 19381da177e4SLinus Torvalds void *vmalloc_exec(unsigned long size) 19391da177e4SLinus Torvalds { 194019809c2dSMichal Hocko return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL_EXEC, 194100ef2d2fSDavid Rientjes NUMA_NO_NODE, __builtin_return_address(0)); 19421da177e4SLinus Torvalds } 19431da177e4SLinus Torvalds 19440d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) 1945698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) 19460d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) 1947698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL) 19480d08e0d3SAndi Kleen #else 1949698d0831SMichal Hocko /* 1950698d0831SMichal Hocko * 64b systems should always have either DMA or DMA32 zones. For others 1951698d0831SMichal Hocko * GFP_DMA32 should do the right thing and use the normal zone. 1952698d0831SMichal Hocko */ 1953698d0831SMichal Hocko #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL 19540d08e0d3SAndi Kleen #endif 19550d08e0d3SAndi Kleen 19561da177e4SLinus Torvalds /** 19571da177e4SLinus Torvalds * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 19581da177e4SLinus Torvalds * @size: allocation size 19591da177e4SLinus Torvalds * 19601da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 19611da177e4SLinus Torvalds * page level allocator and map them into contiguous kernel virtual space. 19621da177e4SLinus Torvalds */ 19631da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 19641da177e4SLinus Torvalds { 19652dca6999SDavid Miller return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL, 196600ef2d2fSDavid Rientjes NUMA_NO_NODE, __builtin_return_address(0)); 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32); 19691da177e4SLinus Torvalds 197083342314SNick Piggin /** 1971ead04089SRolf Eike Beer * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 197283342314SNick Piggin * @size: allocation size 1973ead04089SRolf Eike Beer * 1974ead04089SRolf Eike Beer * The resulting memory area is 32bit addressable and zeroed so it can be 1975ead04089SRolf Eike Beer * mapped to userspace without leaking data. 197683342314SNick Piggin */ 197783342314SNick Piggin void *vmalloc_32_user(unsigned long size) 197883342314SNick Piggin { 1979bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 1980bc84c535SRoman Penyaev GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, 1981bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 19825a82ac71SRoman Penyaev __builtin_return_address(0)); 198383342314SNick Piggin } 198483342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user); 198583342314SNick Piggin 1986d0107eb0SKAMEZAWA Hiroyuki /* 1987d0107eb0SKAMEZAWA Hiroyuki * small helper routine , copy contents to buf from addr. 1988d0107eb0SKAMEZAWA Hiroyuki * If the page is not present, fill zero. 1989d0107eb0SKAMEZAWA Hiroyuki */ 1990d0107eb0SKAMEZAWA Hiroyuki 1991d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count) 1992d0107eb0SKAMEZAWA Hiroyuki { 1993d0107eb0SKAMEZAWA Hiroyuki struct page *p; 1994d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 1995d0107eb0SKAMEZAWA Hiroyuki 1996d0107eb0SKAMEZAWA Hiroyuki while (count) { 1997d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 1998d0107eb0SKAMEZAWA Hiroyuki 1999891c49abSAlexander Kuleshov offset = offset_in_page(addr); 2000d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 2001d0107eb0SKAMEZAWA Hiroyuki if (length > count) 2002d0107eb0SKAMEZAWA Hiroyuki length = count; 2003d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 2004d0107eb0SKAMEZAWA Hiroyuki /* 2005d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 2006d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 2007d0107eb0SKAMEZAWA Hiroyuki * overhead of vmalloc()/vfree() calles for this _debug_ 2008d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 2009d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 2010d0107eb0SKAMEZAWA Hiroyuki */ 2011d0107eb0SKAMEZAWA Hiroyuki if (p) { 2012d0107eb0SKAMEZAWA Hiroyuki /* 2013d0107eb0SKAMEZAWA Hiroyuki * we can expect USER0 is not used (see vread/vwrite's 2014d0107eb0SKAMEZAWA Hiroyuki * function description) 2015d0107eb0SKAMEZAWA Hiroyuki */ 20169b04c5feSCong Wang void *map = kmap_atomic(p); 2017d0107eb0SKAMEZAWA Hiroyuki memcpy(buf, map + offset, length); 20189b04c5feSCong Wang kunmap_atomic(map); 2019d0107eb0SKAMEZAWA Hiroyuki } else 2020d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, length); 2021d0107eb0SKAMEZAWA Hiroyuki 2022d0107eb0SKAMEZAWA Hiroyuki addr += length; 2023d0107eb0SKAMEZAWA Hiroyuki buf += length; 2024d0107eb0SKAMEZAWA Hiroyuki copied += length; 2025d0107eb0SKAMEZAWA Hiroyuki count -= length; 2026d0107eb0SKAMEZAWA Hiroyuki } 2027d0107eb0SKAMEZAWA Hiroyuki return copied; 2028d0107eb0SKAMEZAWA Hiroyuki } 2029d0107eb0SKAMEZAWA Hiroyuki 2030d0107eb0SKAMEZAWA Hiroyuki static int aligned_vwrite(char *buf, char *addr, unsigned long count) 2031d0107eb0SKAMEZAWA Hiroyuki { 2032d0107eb0SKAMEZAWA Hiroyuki struct page *p; 2033d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 2034d0107eb0SKAMEZAWA Hiroyuki 2035d0107eb0SKAMEZAWA Hiroyuki while (count) { 2036d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 2037d0107eb0SKAMEZAWA Hiroyuki 2038891c49abSAlexander Kuleshov offset = offset_in_page(addr); 2039d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 2040d0107eb0SKAMEZAWA Hiroyuki if (length > count) 2041d0107eb0SKAMEZAWA Hiroyuki length = count; 2042d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 2043d0107eb0SKAMEZAWA Hiroyuki /* 2044d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 2045d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 2046d0107eb0SKAMEZAWA Hiroyuki * overhead of vmalloc()/vfree() calles for this _debug_ 2047d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 2048d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 2049d0107eb0SKAMEZAWA Hiroyuki */ 2050d0107eb0SKAMEZAWA Hiroyuki if (p) { 2051d0107eb0SKAMEZAWA Hiroyuki /* 2052d0107eb0SKAMEZAWA Hiroyuki * we can expect USER0 is not used (see vread/vwrite's 2053d0107eb0SKAMEZAWA Hiroyuki * function description) 2054d0107eb0SKAMEZAWA Hiroyuki */ 20559b04c5feSCong Wang void *map = kmap_atomic(p); 2056d0107eb0SKAMEZAWA Hiroyuki memcpy(map + offset, buf, length); 20579b04c5feSCong Wang kunmap_atomic(map); 2058d0107eb0SKAMEZAWA Hiroyuki } 2059d0107eb0SKAMEZAWA Hiroyuki addr += length; 2060d0107eb0SKAMEZAWA Hiroyuki buf += length; 2061d0107eb0SKAMEZAWA Hiroyuki copied += length; 2062d0107eb0SKAMEZAWA Hiroyuki count -= length; 2063d0107eb0SKAMEZAWA Hiroyuki } 2064d0107eb0SKAMEZAWA Hiroyuki return copied; 2065d0107eb0SKAMEZAWA Hiroyuki } 2066d0107eb0SKAMEZAWA Hiroyuki 2067d0107eb0SKAMEZAWA Hiroyuki /** 2068d0107eb0SKAMEZAWA Hiroyuki * vread() - read vmalloc area in a safe way. 2069d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for reading data 2070d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 2071d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 2072d0107eb0SKAMEZAWA Hiroyuki * 2073d0107eb0SKAMEZAWA Hiroyuki * Returns # of bytes which addr and buf should be increased. 2074d0107eb0SKAMEZAWA Hiroyuki * (same number to @count). Returns 0 if [addr...addr+count) doesn't 2075d0107eb0SKAMEZAWA Hiroyuki * includes any intersect with alive vmalloc area. 2076d0107eb0SKAMEZAWA Hiroyuki * 2077d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 2078d0107eb0SKAMEZAWA Hiroyuki * copy data from that area to a given buffer. If the given memory range 2079d0107eb0SKAMEZAWA Hiroyuki * of [addr...addr+count) includes some valid address, data is copied to 2080d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, they'll be zero-filled. 2081d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 2082d0107eb0SKAMEZAWA Hiroyuki * 2083d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 2084a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 2085d0107eb0SKAMEZAWA Hiroyuki * 2086d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vread() is never necessary because the caller 2087d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 2088d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 2089d0107eb0SKAMEZAWA Hiroyuki * any informaion, as /dev/kmem. 2090d0107eb0SKAMEZAWA Hiroyuki */ 20911da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 20921da177e4SLinus Torvalds { 2093e81ce85fSJoonsoo Kim struct vmap_area *va; 2094e81ce85fSJoonsoo Kim struct vm_struct *vm; 20951da177e4SLinus Torvalds char *vaddr, *buf_start = buf; 2096d0107eb0SKAMEZAWA Hiroyuki unsigned long buflen = count; 20971da177e4SLinus Torvalds unsigned long n; 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds /* Don't allow overflow */ 21001da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 21011da177e4SLinus Torvalds count = -(unsigned long) addr; 21021da177e4SLinus Torvalds 2103e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 2104e81ce85fSJoonsoo Kim list_for_each_entry(va, &vmap_area_list, list) { 2105e81ce85fSJoonsoo Kim if (!count) 2106e81ce85fSJoonsoo Kim break; 2107e81ce85fSJoonsoo Kim 2108e81ce85fSJoonsoo Kim if (!(va->flags & VM_VM_AREA)) 2109e81ce85fSJoonsoo Kim continue; 2110e81ce85fSJoonsoo Kim 2111e81ce85fSJoonsoo Kim vm = va->vm; 2112e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 2113762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 21141da177e4SLinus Torvalds continue; 21151da177e4SLinus Torvalds while (addr < vaddr) { 21161da177e4SLinus Torvalds if (count == 0) 21171da177e4SLinus Torvalds goto finished; 21181da177e4SLinus Torvalds *buf = '\0'; 21191da177e4SLinus Torvalds buf++; 21201da177e4SLinus Torvalds addr++; 21211da177e4SLinus Torvalds count--; 21221da177e4SLinus Torvalds } 2123762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 2124d0107eb0SKAMEZAWA Hiroyuki if (n > count) 2125d0107eb0SKAMEZAWA Hiroyuki n = count; 2126e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) 2127d0107eb0SKAMEZAWA Hiroyuki aligned_vread(buf, addr, n); 2128d0107eb0SKAMEZAWA Hiroyuki else /* IOREMAP area is treated as memory hole */ 2129d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, n); 2130d0107eb0SKAMEZAWA Hiroyuki buf += n; 2131d0107eb0SKAMEZAWA Hiroyuki addr += n; 2132d0107eb0SKAMEZAWA Hiroyuki count -= n; 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds finished: 2135e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 2136d0107eb0SKAMEZAWA Hiroyuki 2137d0107eb0SKAMEZAWA Hiroyuki if (buf == buf_start) 2138d0107eb0SKAMEZAWA Hiroyuki return 0; 2139d0107eb0SKAMEZAWA Hiroyuki /* zero-fill memory holes */ 2140d0107eb0SKAMEZAWA Hiroyuki if (buf != buf_start + buflen) 2141d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, buflen - (buf - buf_start)); 2142d0107eb0SKAMEZAWA Hiroyuki 2143d0107eb0SKAMEZAWA Hiroyuki return buflen; 21441da177e4SLinus Torvalds } 21451da177e4SLinus Torvalds 2146d0107eb0SKAMEZAWA Hiroyuki /** 2147d0107eb0SKAMEZAWA Hiroyuki * vwrite() - write vmalloc area in a safe way. 2148d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for source data 2149d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 2150d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 2151d0107eb0SKAMEZAWA Hiroyuki * 2152d0107eb0SKAMEZAWA Hiroyuki * Returns # of bytes which addr and buf should be incresed. 2153d0107eb0SKAMEZAWA Hiroyuki * (same number to @count). 2154d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersect with valid 2155d0107eb0SKAMEZAWA Hiroyuki * vmalloc area, returns 0. 2156d0107eb0SKAMEZAWA Hiroyuki * 2157d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 2158d0107eb0SKAMEZAWA Hiroyuki * copy data from a buffer to the given addr. If specified range of 2159d0107eb0SKAMEZAWA Hiroyuki * [addr...addr+count) includes some valid address, data is copied from 2160d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, no copy to hole. 2161d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 2162d0107eb0SKAMEZAWA Hiroyuki * 2163d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 2164a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 2165d0107eb0SKAMEZAWA Hiroyuki * 2166d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vwrite() is never necessary because the caller 2167d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 2168d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 2169d0107eb0SKAMEZAWA Hiroyuki * any informaion, as /dev/kmem. 2170d0107eb0SKAMEZAWA Hiroyuki */ 21711da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count) 21721da177e4SLinus Torvalds { 2173e81ce85fSJoonsoo Kim struct vmap_area *va; 2174e81ce85fSJoonsoo Kim struct vm_struct *vm; 2175d0107eb0SKAMEZAWA Hiroyuki char *vaddr; 2176d0107eb0SKAMEZAWA Hiroyuki unsigned long n, buflen; 2177d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 21781da177e4SLinus Torvalds 21791da177e4SLinus Torvalds /* Don't allow overflow */ 21801da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 21811da177e4SLinus Torvalds count = -(unsigned long) addr; 2182d0107eb0SKAMEZAWA Hiroyuki buflen = count; 21831da177e4SLinus Torvalds 2184e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 2185e81ce85fSJoonsoo Kim list_for_each_entry(va, &vmap_area_list, list) { 2186e81ce85fSJoonsoo Kim if (!count) 2187e81ce85fSJoonsoo Kim break; 2188e81ce85fSJoonsoo Kim 2189e81ce85fSJoonsoo Kim if (!(va->flags & VM_VM_AREA)) 2190e81ce85fSJoonsoo Kim continue; 2191e81ce85fSJoonsoo Kim 2192e81ce85fSJoonsoo Kim vm = va->vm; 2193e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 2194762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 21951da177e4SLinus Torvalds continue; 21961da177e4SLinus Torvalds while (addr < vaddr) { 21971da177e4SLinus Torvalds if (count == 0) 21981da177e4SLinus Torvalds goto finished; 21991da177e4SLinus Torvalds buf++; 22001da177e4SLinus Torvalds addr++; 22011da177e4SLinus Torvalds count--; 22021da177e4SLinus Torvalds } 2203762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 2204d0107eb0SKAMEZAWA Hiroyuki if (n > count) 2205d0107eb0SKAMEZAWA Hiroyuki n = count; 2206e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) { 2207d0107eb0SKAMEZAWA Hiroyuki aligned_vwrite(buf, addr, n); 2208d0107eb0SKAMEZAWA Hiroyuki copied++; 2209d0107eb0SKAMEZAWA Hiroyuki } 2210d0107eb0SKAMEZAWA Hiroyuki buf += n; 2211d0107eb0SKAMEZAWA Hiroyuki addr += n; 2212d0107eb0SKAMEZAWA Hiroyuki count -= n; 22131da177e4SLinus Torvalds } 22141da177e4SLinus Torvalds finished: 2215e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 2216d0107eb0SKAMEZAWA Hiroyuki if (!copied) 2217d0107eb0SKAMEZAWA Hiroyuki return 0; 2218d0107eb0SKAMEZAWA Hiroyuki return buflen; 22191da177e4SLinus Torvalds } 222083342314SNick Piggin 222183342314SNick Piggin /** 2222e69e9d4aSHATAYAMA Daisuke * remap_vmalloc_range_partial - map vmalloc pages to userspace 2223e69e9d4aSHATAYAMA Daisuke * @vma: vma to cover 2224e69e9d4aSHATAYAMA Daisuke * @uaddr: target user address to start at 2225e69e9d4aSHATAYAMA Daisuke * @kaddr: virtual address of vmalloc kernel memory 2226e69e9d4aSHATAYAMA Daisuke * @size: size of map area 2227e69e9d4aSHATAYAMA Daisuke * 2228e69e9d4aSHATAYAMA Daisuke * Returns: 0 for success, -Exxx on failure 2229e69e9d4aSHATAYAMA Daisuke * 2230e69e9d4aSHATAYAMA Daisuke * This function checks that @kaddr is a valid vmalloc'ed area, 2231e69e9d4aSHATAYAMA Daisuke * and that it is big enough to cover the range starting at 2232e69e9d4aSHATAYAMA Daisuke * @uaddr in @vma. Will return failure if that criteria isn't 2233e69e9d4aSHATAYAMA Daisuke * met. 2234e69e9d4aSHATAYAMA Daisuke * 2235e69e9d4aSHATAYAMA Daisuke * Similar to remap_pfn_range() (see mm/memory.c) 2236e69e9d4aSHATAYAMA Daisuke */ 2237e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr, 2238e69e9d4aSHATAYAMA Daisuke void *kaddr, unsigned long size) 2239e69e9d4aSHATAYAMA Daisuke { 2240e69e9d4aSHATAYAMA Daisuke struct vm_struct *area; 2241e69e9d4aSHATAYAMA Daisuke 2242e69e9d4aSHATAYAMA Daisuke size = PAGE_ALIGN(size); 2243e69e9d4aSHATAYAMA Daisuke 2244e69e9d4aSHATAYAMA Daisuke if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr)) 2245e69e9d4aSHATAYAMA Daisuke return -EINVAL; 2246e69e9d4aSHATAYAMA Daisuke 2247e69e9d4aSHATAYAMA Daisuke area = find_vm_area(kaddr); 2248e69e9d4aSHATAYAMA Daisuke if (!area) 2249e69e9d4aSHATAYAMA Daisuke return -EINVAL; 2250e69e9d4aSHATAYAMA Daisuke 2251e69e9d4aSHATAYAMA Daisuke if (!(area->flags & VM_USERMAP)) 2252e69e9d4aSHATAYAMA Daisuke return -EINVAL; 2253e69e9d4aSHATAYAMA Daisuke 2254401592d2SRoman Penyaev if (kaddr + size > area->addr + get_vm_area_size(area)) 2255e69e9d4aSHATAYAMA Daisuke return -EINVAL; 2256e69e9d4aSHATAYAMA Daisuke 2257e69e9d4aSHATAYAMA Daisuke do { 2258e69e9d4aSHATAYAMA Daisuke struct page *page = vmalloc_to_page(kaddr); 2259e69e9d4aSHATAYAMA Daisuke int ret; 2260e69e9d4aSHATAYAMA Daisuke 2261e69e9d4aSHATAYAMA Daisuke ret = vm_insert_page(vma, uaddr, page); 2262e69e9d4aSHATAYAMA Daisuke if (ret) 2263e69e9d4aSHATAYAMA Daisuke return ret; 2264e69e9d4aSHATAYAMA Daisuke 2265e69e9d4aSHATAYAMA Daisuke uaddr += PAGE_SIZE; 2266e69e9d4aSHATAYAMA Daisuke kaddr += PAGE_SIZE; 2267e69e9d4aSHATAYAMA Daisuke size -= PAGE_SIZE; 2268e69e9d4aSHATAYAMA Daisuke } while (size > 0); 2269e69e9d4aSHATAYAMA Daisuke 2270e69e9d4aSHATAYAMA Daisuke vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 2271e69e9d4aSHATAYAMA Daisuke 2272e69e9d4aSHATAYAMA Daisuke return 0; 2273e69e9d4aSHATAYAMA Daisuke } 2274e69e9d4aSHATAYAMA Daisuke EXPORT_SYMBOL(remap_vmalloc_range_partial); 2275e69e9d4aSHATAYAMA Daisuke 2276e69e9d4aSHATAYAMA Daisuke /** 227783342314SNick Piggin * remap_vmalloc_range - map vmalloc pages to userspace 227883342314SNick Piggin * @vma: vma to cover (map full range of vma) 227983342314SNick Piggin * @addr: vmalloc memory 228083342314SNick Piggin * @pgoff: number of pages into addr before first page to map 22817682486bSRandy Dunlap * 22827682486bSRandy Dunlap * Returns: 0 for success, -Exxx on failure 228383342314SNick Piggin * 228483342314SNick Piggin * This function checks that addr is a valid vmalloc'ed area, and 228583342314SNick Piggin * that it is big enough to cover the vma. Will return failure if 228683342314SNick Piggin * that criteria isn't met. 228783342314SNick Piggin * 228872fd4a35SRobert P. J. Day * Similar to remap_pfn_range() (see mm/memory.c) 228983342314SNick Piggin */ 229083342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 229183342314SNick Piggin unsigned long pgoff) 229283342314SNick Piggin { 2293e69e9d4aSHATAYAMA Daisuke return remap_vmalloc_range_partial(vma, vma->vm_start, 2294e69e9d4aSHATAYAMA Daisuke addr + (pgoff << PAGE_SHIFT), 2295e69e9d4aSHATAYAMA Daisuke vma->vm_end - vma->vm_start); 229683342314SNick Piggin } 229783342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range); 229883342314SNick Piggin 22991eeb66a1SChristoph Hellwig /* 23001eeb66a1SChristoph Hellwig * Implement a stub for vmalloc_sync_all() if the architecture chose not to 23011eeb66a1SChristoph Hellwig * have one. 23021eeb66a1SChristoph Hellwig */ 23033b32123dSGideon Israel Dsouza void __weak vmalloc_sync_all(void) 23041eeb66a1SChristoph Hellwig { 23051eeb66a1SChristoph Hellwig } 23065f4352fbSJeremy Fitzhardinge 23075f4352fbSJeremy Fitzhardinge 23082f569afdSMartin Schwidefsky static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data) 23095f4352fbSJeremy Fitzhardinge { 2310cd12909cSDavid Vrabel pte_t ***p = data; 2311cd12909cSDavid Vrabel 2312cd12909cSDavid Vrabel if (p) { 2313cd12909cSDavid Vrabel *(*p) = pte; 2314cd12909cSDavid Vrabel (*p)++; 2315cd12909cSDavid Vrabel } 23165f4352fbSJeremy Fitzhardinge return 0; 23175f4352fbSJeremy Fitzhardinge } 23185f4352fbSJeremy Fitzhardinge 23195f4352fbSJeremy Fitzhardinge /** 23205f4352fbSJeremy Fitzhardinge * alloc_vm_area - allocate a range of kernel address space 23215f4352fbSJeremy Fitzhardinge * @size: size of the area 2322cd12909cSDavid Vrabel * @ptes: returns the PTEs for the address space 23237682486bSRandy Dunlap * 23247682486bSRandy Dunlap * Returns: NULL on failure, vm_struct on success 23255f4352fbSJeremy Fitzhardinge * 23265f4352fbSJeremy Fitzhardinge * This function reserves a range of kernel address space, and 23275f4352fbSJeremy Fitzhardinge * allocates pagetables to map that range. No actual mappings 2328cd12909cSDavid Vrabel * are created. 2329cd12909cSDavid Vrabel * 2330cd12909cSDavid Vrabel * If @ptes is non-NULL, pointers to the PTEs (in init_mm) 2331cd12909cSDavid Vrabel * allocated for the VM area are returned. 23325f4352fbSJeremy Fitzhardinge */ 2333cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes) 23345f4352fbSJeremy Fitzhardinge { 23355f4352fbSJeremy Fitzhardinge struct vm_struct *area; 23365f4352fbSJeremy Fitzhardinge 233723016969SChristoph Lameter area = get_vm_area_caller(size, VM_IOREMAP, 233823016969SChristoph Lameter __builtin_return_address(0)); 23395f4352fbSJeremy Fitzhardinge if (area == NULL) 23405f4352fbSJeremy Fitzhardinge return NULL; 23415f4352fbSJeremy Fitzhardinge 23425f4352fbSJeremy Fitzhardinge /* 23435f4352fbSJeremy Fitzhardinge * This ensures that page tables are constructed for this region 23445f4352fbSJeremy Fitzhardinge * of kernel virtual address space and mapped into init_mm. 23455f4352fbSJeremy Fitzhardinge */ 23465f4352fbSJeremy Fitzhardinge if (apply_to_page_range(&init_mm, (unsigned long)area->addr, 2347cd12909cSDavid Vrabel size, f, ptes ? &ptes : NULL)) { 23485f4352fbSJeremy Fitzhardinge free_vm_area(area); 23495f4352fbSJeremy Fitzhardinge return NULL; 23505f4352fbSJeremy Fitzhardinge } 23515f4352fbSJeremy Fitzhardinge 23525f4352fbSJeremy Fitzhardinge return area; 23535f4352fbSJeremy Fitzhardinge } 23545f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(alloc_vm_area); 23555f4352fbSJeremy Fitzhardinge 23565f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area) 23575f4352fbSJeremy Fitzhardinge { 23585f4352fbSJeremy Fitzhardinge struct vm_struct *ret; 23595f4352fbSJeremy Fitzhardinge ret = remove_vm_area(area->addr); 23605f4352fbSJeremy Fitzhardinge BUG_ON(ret != area); 23615f4352fbSJeremy Fitzhardinge kfree(area); 23625f4352fbSJeremy Fitzhardinge } 23635f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area); 2364a10aa579SChristoph Lameter 23654f8b02b4STejun Heo #ifdef CONFIG_SMP 2366ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n) 2367ca23e405STejun Heo { 23684583e773SGeliang Tang return rb_entry_safe(n, struct vmap_area, rb_node); 2369ca23e405STejun Heo } 2370ca23e405STejun Heo 2371ca23e405STejun Heo /** 2372ca23e405STejun Heo * pvm_find_next_prev - find the next and prev vmap_area surrounding @end 2373ca23e405STejun Heo * @end: target address 2374ca23e405STejun Heo * @pnext: out arg for the next vmap_area 2375ca23e405STejun Heo * @pprev: out arg for the previous vmap_area 2376ca23e405STejun Heo * 2377ca23e405STejun Heo * Returns: %true if either or both of next and prev are found, 2378ca23e405STejun Heo * %false if no vmap_area exists 2379ca23e405STejun Heo * 2380ca23e405STejun Heo * Find vmap_areas end addresses of which enclose @end. ie. if not 2381ca23e405STejun Heo * NULL, *pnext->va_end > @end and *pprev->va_end <= @end. 2382ca23e405STejun Heo */ 2383ca23e405STejun Heo static bool pvm_find_next_prev(unsigned long end, 2384ca23e405STejun Heo struct vmap_area **pnext, 2385ca23e405STejun Heo struct vmap_area **pprev) 2386ca23e405STejun Heo { 2387ca23e405STejun Heo struct rb_node *n = vmap_area_root.rb_node; 2388ca23e405STejun Heo struct vmap_area *va = NULL; 2389ca23e405STejun Heo 2390ca23e405STejun Heo while (n) { 2391ca23e405STejun Heo va = rb_entry(n, struct vmap_area, rb_node); 2392ca23e405STejun Heo if (end < va->va_end) 2393ca23e405STejun Heo n = n->rb_left; 2394ca23e405STejun Heo else if (end > va->va_end) 2395ca23e405STejun Heo n = n->rb_right; 2396ca23e405STejun Heo else 2397ca23e405STejun Heo break; 2398ca23e405STejun Heo } 2399ca23e405STejun Heo 2400ca23e405STejun Heo if (!va) 2401ca23e405STejun Heo return false; 2402ca23e405STejun Heo 2403ca23e405STejun Heo if (va->va_end > end) { 2404ca23e405STejun Heo *pnext = va; 2405ca23e405STejun Heo *pprev = node_to_va(rb_prev(&(*pnext)->rb_node)); 2406ca23e405STejun Heo } else { 2407ca23e405STejun Heo *pprev = va; 2408ca23e405STejun Heo *pnext = node_to_va(rb_next(&(*pprev)->rb_node)); 2409ca23e405STejun Heo } 2410ca23e405STejun Heo return true; 2411ca23e405STejun Heo } 2412ca23e405STejun Heo 2413ca23e405STejun Heo /** 2414ca23e405STejun Heo * pvm_determine_end - find the highest aligned address between two vmap_areas 2415ca23e405STejun Heo * @pnext: in/out arg for the next vmap_area 2416ca23e405STejun Heo * @pprev: in/out arg for the previous vmap_area 2417ca23e405STejun Heo * @align: alignment 2418ca23e405STejun Heo * 2419ca23e405STejun Heo * Returns: determined end address 2420ca23e405STejun Heo * 2421ca23e405STejun Heo * Find the highest aligned address between *@pnext and *@pprev below 2422ca23e405STejun Heo * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned 2423ca23e405STejun Heo * down address is between the end addresses of the two vmap_areas. 2424ca23e405STejun Heo * 2425ca23e405STejun Heo * Please note that the address returned by this function may fall 2426ca23e405STejun Heo * inside *@pnext vmap_area. The caller is responsible for checking 2427ca23e405STejun Heo * that. 2428ca23e405STejun Heo */ 2429ca23e405STejun Heo static unsigned long pvm_determine_end(struct vmap_area **pnext, 2430ca23e405STejun Heo struct vmap_area **pprev, 2431ca23e405STejun Heo unsigned long align) 2432ca23e405STejun Heo { 2433ca23e405STejun Heo const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 2434ca23e405STejun Heo unsigned long addr; 2435ca23e405STejun Heo 2436ca23e405STejun Heo if (*pnext) 2437ca23e405STejun Heo addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end); 2438ca23e405STejun Heo else 2439ca23e405STejun Heo addr = vmalloc_end; 2440ca23e405STejun Heo 2441ca23e405STejun Heo while (*pprev && (*pprev)->va_end > addr) { 2442ca23e405STejun Heo *pnext = *pprev; 2443ca23e405STejun Heo *pprev = node_to_va(rb_prev(&(*pnext)->rb_node)); 2444ca23e405STejun Heo } 2445ca23e405STejun Heo 2446ca23e405STejun Heo return addr; 2447ca23e405STejun Heo } 2448ca23e405STejun Heo 2449ca23e405STejun Heo /** 2450ca23e405STejun Heo * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator 2451ca23e405STejun Heo * @offsets: array containing offset of each area 2452ca23e405STejun Heo * @sizes: array containing size of each area 2453ca23e405STejun Heo * @nr_vms: the number of areas to allocate 2454ca23e405STejun Heo * @align: alignment, all entries in @offsets and @sizes must be aligned to this 2455ca23e405STejun Heo * 2456ca23e405STejun Heo * Returns: kmalloc'd vm_struct pointer array pointing to allocated 2457ca23e405STejun Heo * vm_structs on success, %NULL on failure 2458ca23e405STejun Heo * 2459ca23e405STejun Heo * Percpu allocator wants to use congruent vm areas so that it can 2460ca23e405STejun Heo * maintain the offsets among percpu areas. This function allocates 2461ec3f64fcSDavid Rientjes * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to 2462ec3f64fcSDavid Rientjes * be scattered pretty far, distance between two areas easily going up 2463ec3f64fcSDavid Rientjes * to gigabytes. To avoid interacting with regular vmallocs, these 2464ec3f64fcSDavid Rientjes * areas are allocated from top. 2465ca23e405STejun Heo * 2466ca23e405STejun Heo * Despite its complicated look, this allocator is rather simple. It 2467ca23e405STejun Heo * does everything top-down and scans areas from the end looking for 2468ca23e405STejun Heo * matching slot. While scanning, if any of the areas overlaps with 2469ca23e405STejun Heo * existing vmap_area, the base address is pulled down to fit the 2470ca23e405STejun Heo * area. Scanning is repeated till all the areas fit and then all 2471c568da28SWei Yang * necessary data structures are inserted and the result is returned. 2472ca23e405STejun Heo */ 2473ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, 2474ca23e405STejun Heo const size_t *sizes, int nr_vms, 2475ec3f64fcSDavid Rientjes size_t align) 2476ca23e405STejun Heo { 2477ca23e405STejun Heo const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); 2478ca23e405STejun Heo const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 2479ca23e405STejun Heo struct vmap_area **vas, *prev, *next; 2480ca23e405STejun Heo struct vm_struct **vms; 2481ca23e405STejun Heo int area, area2, last_area, term_area; 2482ca23e405STejun Heo unsigned long base, start, end, last_end; 2483ca23e405STejun Heo bool purged = false; 2484ca23e405STejun Heo 2485ca23e405STejun Heo /* verify parameters and allocate data structures */ 2486891c49abSAlexander Kuleshov BUG_ON(offset_in_page(align) || !is_power_of_2(align)); 2487ca23e405STejun Heo for (last_area = 0, area = 0; area < nr_vms; area++) { 2488ca23e405STejun Heo start = offsets[area]; 2489ca23e405STejun Heo end = start + sizes[area]; 2490ca23e405STejun Heo 2491ca23e405STejun Heo /* is everything aligned properly? */ 2492ca23e405STejun Heo BUG_ON(!IS_ALIGNED(offsets[area], align)); 2493ca23e405STejun Heo BUG_ON(!IS_ALIGNED(sizes[area], align)); 2494ca23e405STejun Heo 2495ca23e405STejun Heo /* detect the area with the highest address */ 2496ca23e405STejun Heo if (start > offsets[last_area]) 2497ca23e405STejun Heo last_area = area; 2498ca23e405STejun Heo 2499c568da28SWei Yang for (area2 = area + 1; area2 < nr_vms; area2++) { 2500ca23e405STejun Heo unsigned long start2 = offsets[area2]; 2501ca23e405STejun Heo unsigned long end2 = start2 + sizes[area2]; 2502ca23e405STejun Heo 2503c568da28SWei Yang BUG_ON(start2 < end && start < end2); 2504ca23e405STejun Heo } 2505ca23e405STejun Heo } 2506ca23e405STejun Heo last_end = offsets[last_area] + sizes[last_area]; 2507ca23e405STejun Heo 2508ca23e405STejun Heo if (vmalloc_end - vmalloc_start < last_end) { 2509ca23e405STejun Heo WARN_ON(true); 2510ca23e405STejun Heo return NULL; 2511ca23e405STejun Heo } 2512ca23e405STejun Heo 25134d67d860SThomas Meyer vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL); 25144d67d860SThomas Meyer vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL); 2515ca23e405STejun Heo if (!vas || !vms) 2516f1db7afdSKautuk Consul goto err_free2; 2517ca23e405STejun Heo 2518ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 2519ec3f64fcSDavid Rientjes vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL); 2520ec3f64fcSDavid Rientjes vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL); 2521ca23e405STejun Heo if (!vas[area] || !vms[area]) 2522ca23e405STejun Heo goto err_free; 2523ca23e405STejun Heo } 2524ca23e405STejun Heo retry: 2525ca23e405STejun Heo spin_lock(&vmap_area_lock); 2526ca23e405STejun Heo 2527ca23e405STejun Heo /* start scanning - we scan from the top, begin with the last area */ 2528ca23e405STejun Heo area = term_area = last_area; 2529ca23e405STejun Heo start = offsets[area]; 2530ca23e405STejun Heo end = start + sizes[area]; 2531ca23e405STejun Heo 2532ca23e405STejun Heo if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) { 2533ca23e405STejun Heo base = vmalloc_end - last_end; 2534ca23e405STejun Heo goto found; 2535ca23e405STejun Heo } 2536ca23e405STejun Heo base = pvm_determine_end(&next, &prev, align) - end; 2537ca23e405STejun Heo 2538ca23e405STejun Heo while (true) { 2539ca23e405STejun Heo BUG_ON(next && next->va_end <= base + end); 2540ca23e405STejun Heo BUG_ON(prev && prev->va_end > base + end); 2541ca23e405STejun Heo 2542ca23e405STejun Heo /* 2543ca23e405STejun Heo * base might have underflowed, add last_end before 2544ca23e405STejun Heo * comparing. 2545ca23e405STejun Heo */ 2546ca23e405STejun Heo if (base + last_end < vmalloc_start + last_end) { 2547ca23e405STejun Heo spin_unlock(&vmap_area_lock); 2548ca23e405STejun Heo if (!purged) { 2549ca23e405STejun Heo purge_vmap_area_lazy(); 2550ca23e405STejun Heo purged = true; 2551ca23e405STejun Heo goto retry; 2552ca23e405STejun Heo } 2553ca23e405STejun Heo goto err_free; 2554ca23e405STejun Heo } 2555ca23e405STejun Heo 2556ca23e405STejun Heo /* 2557ca23e405STejun Heo * If next overlaps, move base downwards so that it's 2558ca23e405STejun Heo * right below next and then recheck. 2559ca23e405STejun Heo */ 2560ca23e405STejun Heo if (next && next->va_start < base + end) { 2561ca23e405STejun Heo base = pvm_determine_end(&next, &prev, align) - end; 2562ca23e405STejun Heo term_area = area; 2563ca23e405STejun Heo continue; 2564ca23e405STejun Heo } 2565ca23e405STejun Heo 2566ca23e405STejun Heo /* 2567ca23e405STejun Heo * If prev overlaps, shift down next and prev and move 2568ca23e405STejun Heo * base so that it's right below new next and then 2569ca23e405STejun Heo * recheck. 2570ca23e405STejun Heo */ 2571ca23e405STejun Heo if (prev && prev->va_end > base + start) { 2572ca23e405STejun Heo next = prev; 2573ca23e405STejun Heo prev = node_to_va(rb_prev(&next->rb_node)); 2574ca23e405STejun Heo base = pvm_determine_end(&next, &prev, align) - end; 2575ca23e405STejun Heo term_area = area; 2576ca23e405STejun Heo continue; 2577ca23e405STejun Heo } 2578ca23e405STejun Heo 2579ca23e405STejun Heo /* 2580ca23e405STejun Heo * This area fits, move on to the previous one. If 2581ca23e405STejun Heo * the previous one is the terminal one, we're done. 2582ca23e405STejun Heo */ 2583ca23e405STejun Heo area = (area + nr_vms - 1) % nr_vms; 2584ca23e405STejun Heo if (area == term_area) 2585ca23e405STejun Heo break; 2586ca23e405STejun Heo start = offsets[area]; 2587ca23e405STejun Heo end = start + sizes[area]; 2588ca23e405STejun Heo pvm_find_next_prev(base + end, &next, &prev); 2589ca23e405STejun Heo } 2590ca23e405STejun Heo found: 2591ca23e405STejun Heo /* we've found a fitting base, insert all va's */ 2592ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 2593ca23e405STejun Heo struct vmap_area *va = vas[area]; 2594ca23e405STejun Heo 2595ca23e405STejun Heo va->va_start = base + offsets[area]; 2596ca23e405STejun Heo va->va_end = va->va_start + sizes[area]; 2597ca23e405STejun Heo __insert_vmap_area(va); 2598ca23e405STejun Heo } 2599ca23e405STejun Heo 2600ca23e405STejun Heo vmap_area_pcpu_hole = base + offsets[last_area]; 2601ca23e405STejun Heo 2602ca23e405STejun Heo spin_unlock(&vmap_area_lock); 2603ca23e405STejun Heo 2604ca23e405STejun Heo /* insert all vm's */ 2605ca23e405STejun Heo for (area = 0; area < nr_vms; area++) 26063645cb4aSZhang Yanfei setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC, 2607ca23e405STejun Heo pcpu_get_vm_areas); 2608ca23e405STejun Heo 2609ca23e405STejun Heo kfree(vas); 2610ca23e405STejun Heo return vms; 2611ca23e405STejun Heo 2612ca23e405STejun Heo err_free: 2613ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 2614ca23e405STejun Heo kfree(vas[area]); 2615ca23e405STejun Heo kfree(vms[area]); 2616ca23e405STejun Heo } 2617f1db7afdSKautuk Consul err_free2: 2618ca23e405STejun Heo kfree(vas); 2619ca23e405STejun Heo kfree(vms); 2620ca23e405STejun Heo return NULL; 2621ca23e405STejun Heo } 2622ca23e405STejun Heo 2623ca23e405STejun Heo /** 2624ca23e405STejun Heo * pcpu_free_vm_areas - free vmalloc areas for percpu allocator 2625ca23e405STejun Heo * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() 2626ca23e405STejun Heo * @nr_vms: the number of allocated areas 2627ca23e405STejun Heo * 2628ca23e405STejun Heo * Free vm_structs and the array allocated by pcpu_get_vm_areas(). 2629ca23e405STejun Heo */ 2630ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) 2631ca23e405STejun Heo { 2632ca23e405STejun Heo int i; 2633ca23e405STejun Heo 2634ca23e405STejun Heo for (i = 0; i < nr_vms; i++) 2635ca23e405STejun Heo free_vm_area(vms[i]); 2636ca23e405STejun Heo kfree(vms); 2637ca23e405STejun Heo } 26384f8b02b4STejun Heo #endif /* CONFIG_SMP */ 2639a10aa579SChristoph Lameter 2640a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS 2641a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos) 2642d4033afdSJoonsoo Kim __acquires(&vmap_area_lock) 2643a10aa579SChristoph Lameter { 2644d4033afdSJoonsoo Kim spin_lock(&vmap_area_lock); 26453f500069Szijun_hu return seq_list_start(&vmap_area_list, *pos); 2646a10aa579SChristoph Lameter } 2647a10aa579SChristoph Lameter 2648a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos) 2649a10aa579SChristoph Lameter { 26503f500069Szijun_hu return seq_list_next(p, &vmap_area_list, pos); 2651a10aa579SChristoph Lameter } 2652a10aa579SChristoph Lameter 2653a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p) 2654d4033afdSJoonsoo Kim __releases(&vmap_area_lock) 2655a10aa579SChristoph Lameter { 2656d4033afdSJoonsoo Kim spin_unlock(&vmap_area_lock); 2657a10aa579SChristoph Lameter } 2658a10aa579SChristoph Lameter 2659a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v) 2660a47a126aSEric Dumazet { 2661e5adfffcSKirill A. Shutemov if (IS_ENABLED(CONFIG_NUMA)) { 2662a47a126aSEric Dumazet unsigned int nr, *counters = m->private; 2663a47a126aSEric Dumazet 2664a47a126aSEric Dumazet if (!counters) 2665a47a126aSEric Dumazet return; 2666a47a126aSEric Dumazet 2667af12346cSWanpeng Li if (v->flags & VM_UNINITIALIZED) 2668af12346cSWanpeng Li return; 26697e5b528bSDmitry Vyukov /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ 26707e5b528bSDmitry Vyukov smp_rmb(); 2671af12346cSWanpeng Li 2672a47a126aSEric Dumazet memset(counters, 0, nr_node_ids * sizeof(unsigned int)); 2673a47a126aSEric Dumazet 2674a47a126aSEric Dumazet for (nr = 0; nr < v->nr_pages; nr++) 2675a47a126aSEric Dumazet counters[page_to_nid(v->pages[nr])]++; 2676a47a126aSEric Dumazet 2677a47a126aSEric Dumazet for_each_node_state(nr, N_HIGH_MEMORY) 2678a47a126aSEric Dumazet if (counters[nr]) 2679a47a126aSEric Dumazet seq_printf(m, " N%u=%u", nr, counters[nr]); 2680a47a126aSEric Dumazet } 2681a47a126aSEric Dumazet } 2682a47a126aSEric Dumazet 2683a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p) 2684a10aa579SChristoph Lameter { 26853f500069Szijun_hu struct vmap_area *va; 2686d4033afdSJoonsoo Kim struct vm_struct *v; 2687d4033afdSJoonsoo Kim 26883f500069Szijun_hu va = list_entry(p, struct vmap_area, list); 26893f500069Szijun_hu 2690c2ce8c14SWanpeng Li /* 2691c2ce8c14SWanpeng Li * s_show can encounter race with remove_vm_area, !VM_VM_AREA on 2692c2ce8c14SWanpeng Li * behalf of vmap area is being tear down or vm_map_ram allocation. 2693c2ce8c14SWanpeng Li */ 269478c72746SYisheng Xie if (!(va->flags & VM_VM_AREA)) { 269578c72746SYisheng Xie seq_printf(m, "0x%pK-0x%pK %7ld %s\n", 269678c72746SYisheng Xie (void *)va->va_start, (void *)va->va_end, 269778c72746SYisheng Xie va->va_end - va->va_start, 269878c72746SYisheng Xie va->flags & VM_LAZY_FREE ? "unpurged vm_area" : "vm_map_ram"); 269978c72746SYisheng Xie 2700d4033afdSJoonsoo Kim return 0; 270178c72746SYisheng Xie } 2702d4033afdSJoonsoo Kim 2703d4033afdSJoonsoo Kim v = va->vm; 2704a10aa579SChristoph Lameter 270545ec1690SKees Cook seq_printf(m, "0x%pK-0x%pK %7ld", 2706a10aa579SChristoph Lameter v->addr, v->addr + v->size, v->size); 2707a10aa579SChristoph Lameter 270862c70bceSJoe Perches if (v->caller) 270962c70bceSJoe Perches seq_printf(m, " %pS", v->caller); 271023016969SChristoph Lameter 2711a10aa579SChristoph Lameter if (v->nr_pages) 2712a10aa579SChristoph Lameter seq_printf(m, " pages=%d", v->nr_pages); 2713a10aa579SChristoph Lameter 2714a10aa579SChristoph Lameter if (v->phys_addr) 2715199eaa05SMiles Chen seq_printf(m, " phys=%pa", &v->phys_addr); 2716a10aa579SChristoph Lameter 2717a10aa579SChristoph Lameter if (v->flags & VM_IOREMAP) 2718f4527c90SFabian Frederick seq_puts(m, " ioremap"); 2719a10aa579SChristoph Lameter 2720a10aa579SChristoph Lameter if (v->flags & VM_ALLOC) 2721f4527c90SFabian Frederick seq_puts(m, " vmalloc"); 2722a10aa579SChristoph Lameter 2723a10aa579SChristoph Lameter if (v->flags & VM_MAP) 2724f4527c90SFabian Frederick seq_puts(m, " vmap"); 2725a10aa579SChristoph Lameter 2726a10aa579SChristoph Lameter if (v->flags & VM_USERMAP) 2727f4527c90SFabian Frederick seq_puts(m, " user"); 2728a10aa579SChristoph Lameter 2729244d63eeSDavid Rientjes if (is_vmalloc_addr(v->pages)) 2730f4527c90SFabian Frederick seq_puts(m, " vpages"); 2731a10aa579SChristoph Lameter 2732a47a126aSEric Dumazet show_numa_info(m, v); 2733a10aa579SChristoph Lameter seq_putc(m, '\n'); 2734a10aa579SChristoph Lameter return 0; 2735a10aa579SChristoph Lameter } 2736a10aa579SChristoph Lameter 27375f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = { 2738a10aa579SChristoph Lameter .start = s_start, 2739a10aa579SChristoph Lameter .next = s_next, 2740a10aa579SChristoph Lameter .stop = s_stop, 2741a10aa579SChristoph Lameter .show = s_show, 2742a10aa579SChristoph Lameter }; 27435f6a6a9cSAlexey Dobriyan 27445f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void) 27455f6a6a9cSAlexey Dobriyan { 2746fddda2b7SChristoph Hellwig if (IS_ENABLED(CONFIG_NUMA)) 27470825a6f9SJoe Perches proc_create_seq_private("vmallocinfo", 0400, NULL, 274844414d82SChristoph Hellwig &vmalloc_op, 274944414d82SChristoph Hellwig nr_node_ids * sizeof(unsigned int), NULL); 2750fddda2b7SChristoph Hellwig else 27510825a6f9SJoe Perches proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op); 27525f6a6a9cSAlexey Dobriyan return 0; 27535f6a6a9cSAlexey Dobriyan } 27545f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init); 2755db3808c1SJoonsoo Kim 2756a10aa579SChristoph Lameter #endif 2757