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> 15d43c36dcSAlexey Dobriyan #include <linux/sched.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> 24db64fe02SNick Piggin #include <linux/rbtree.h> 25db64fe02SNick Piggin #include <linux/radix-tree.h> 26db64fe02SNick Piggin #include <linux/rcupdate.h> 27f0aa6617STejun Heo #include <linux/pfn.h> 2889219d37SCatalin Marinas #include <linux/kmemleak.h> 29db64fe02SNick Piggin #include <asm/atomic.h> 301da177e4SLinus Torvalds #include <asm/uaccess.h> 311da177e4SLinus Torvalds #include <asm/tlbflush.h> 322dca6999SDavid Miller #include <asm/shmparam.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds 35db64fe02SNick Piggin /*** Page table manipulation functions ***/ 36b221385bSAdrian Bunk 371da177e4SLinus Torvalds static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end) 381da177e4SLinus Torvalds { 391da177e4SLinus Torvalds pte_t *pte; 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds pte = pte_offset_kernel(pmd, addr); 421da177e4SLinus Torvalds do { 431da177e4SLinus Torvalds pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte); 441da177e4SLinus Torvalds WARN_ON(!pte_none(ptent) && !pte_present(ptent)); 451da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 461da177e4SLinus Torvalds } 471da177e4SLinus Torvalds 48db64fe02SNick Piggin static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end) 491da177e4SLinus Torvalds { 501da177e4SLinus Torvalds pmd_t *pmd; 511da177e4SLinus Torvalds unsigned long next; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 541da177e4SLinus Torvalds do { 551da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 561da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 571da177e4SLinus Torvalds continue; 581da177e4SLinus Torvalds vunmap_pte_range(pmd, addr, next); 591da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds 62db64fe02SNick Piggin static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end) 631da177e4SLinus Torvalds { 641da177e4SLinus Torvalds pud_t *pud; 651da177e4SLinus Torvalds unsigned long next; 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 681da177e4SLinus Torvalds do { 691da177e4SLinus Torvalds next = pud_addr_end(addr, end); 701da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 711da177e4SLinus Torvalds continue; 721da177e4SLinus Torvalds vunmap_pmd_range(pud, addr, next); 731da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 76db64fe02SNick Piggin static void vunmap_page_range(unsigned long addr, unsigned long end) 771da177e4SLinus Torvalds { 781da177e4SLinus Torvalds pgd_t *pgd; 791da177e4SLinus Torvalds unsigned long next; 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds BUG_ON(addr >= end); 821da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 831da177e4SLinus Torvalds do { 841da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 851da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 861da177e4SLinus Torvalds continue; 871da177e4SLinus Torvalds vunmap_pud_range(pgd, addr, next); 881da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds static int vmap_pte_range(pmd_t *pmd, unsigned long addr, 92db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds pte_t *pte; 951da177e4SLinus Torvalds 96db64fe02SNick Piggin /* 97db64fe02SNick Piggin * nr is a running index into the array which helps higher level 98db64fe02SNick Piggin * callers keep track of where we're up to. 99db64fe02SNick Piggin */ 100db64fe02SNick Piggin 101872fec16SHugh Dickins pte = pte_alloc_kernel(pmd, addr); 1021da177e4SLinus Torvalds if (!pte) 1031da177e4SLinus Torvalds return -ENOMEM; 1041da177e4SLinus Torvalds do { 105db64fe02SNick Piggin struct page *page = pages[*nr]; 106db64fe02SNick Piggin 107db64fe02SNick Piggin if (WARN_ON(!pte_none(*pte))) 108db64fe02SNick Piggin return -EBUSY; 109db64fe02SNick Piggin if (WARN_ON(!page)) 1101da177e4SLinus Torvalds return -ENOMEM; 1111da177e4SLinus Torvalds set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); 112db64fe02SNick Piggin (*nr)++; 1131da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 1141da177e4SLinus Torvalds return 0; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 117db64fe02SNick Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr, 118db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds pmd_t *pmd; 1211da177e4SLinus Torvalds unsigned long next; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds pmd = pmd_alloc(&init_mm, pud, addr); 1241da177e4SLinus Torvalds if (!pmd) 1251da177e4SLinus Torvalds return -ENOMEM; 1261da177e4SLinus Torvalds do { 1271da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 128db64fe02SNick Piggin if (vmap_pte_range(pmd, addr, next, prot, pages, nr)) 1291da177e4SLinus Torvalds return -ENOMEM; 1301da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 1311da177e4SLinus Torvalds return 0; 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 134db64fe02SNick Piggin static int vmap_pud_range(pgd_t *pgd, unsigned long addr, 135db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds pud_t *pud; 1381da177e4SLinus Torvalds unsigned long next; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds pud = pud_alloc(&init_mm, pgd, addr); 1411da177e4SLinus Torvalds if (!pud) 1421da177e4SLinus Torvalds return -ENOMEM; 1431da177e4SLinus Torvalds do { 1441da177e4SLinus Torvalds next = pud_addr_end(addr, end); 145db64fe02SNick Piggin if (vmap_pmd_range(pud, addr, next, prot, pages, nr)) 1461da177e4SLinus Torvalds return -ENOMEM; 1471da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 1481da177e4SLinus Torvalds return 0; 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 151db64fe02SNick Piggin /* 152db64fe02SNick Piggin * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and 153db64fe02SNick Piggin * will have pfns corresponding to the "pages" array. 154db64fe02SNick Piggin * 155db64fe02SNick Piggin * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N] 156db64fe02SNick Piggin */ 1578fc48985STejun Heo static int vmap_page_range_noflush(unsigned long start, unsigned long end, 158db64fe02SNick Piggin pgprot_t prot, struct page **pages) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds pgd_t *pgd; 1611da177e4SLinus Torvalds unsigned long next; 1622e4e27c7SAdam Lackorzynski unsigned long addr = start; 163db64fe02SNick Piggin int err = 0; 164db64fe02SNick Piggin int nr = 0; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds BUG_ON(addr >= end); 1671da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 1681da177e4SLinus Torvalds do { 1691da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 170db64fe02SNick Piggin err = vmap_pud_range(pgd, addr, next, prot, pages, &nr); 1711da177e4SLinus Torvalds if (err) 172bf88c8c8SFigo.zhang return err; 1731da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 174db64fe02SNick Piggin 175db64fe02SNick Piggin return nr; 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1788fc48985STejun Heo static int vmap_page_range(unsigned long start, unsigned long end, 1798fc48985STejun Heo pgprot_t prot, struct page **pages) 1808fc48985STejun Heo { 1818fc48985STejun Heo int ret; 1828fc48985STejun Heo 1838fc48985STejun Heo ret = vmap_page_range_noflush(start, end, prot, pages); 1848fc48985STejun Heo flush_cache_vmap(start, end); 1858fc48985STejun Heo return ret; 1868fc48985STejun Heo } 1878fc48985STejun Heo 18881ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x) 18973bdf0a6SLinus Torvalds { 19073bdf0a6SLinus Torvalds /* 191ab4f2ee1SRussell King * ARM, x86-64 and sparc64 put modules in a special place, 19273bdf0a6SLinus Torvalds * and fall back on vmalloc() if that fails. Others 19373bdf0a6SLinus Torvalds * just put it in the vmalloc space. 19473bdf0a6SLinus Torvalds */ 19573bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) 19673bdf0a6SLinus Torvalds unsigned long addr = (unsigned long)x; 19773bdf0a6SLinus Torvalds if (addr >= MODULES_VADDR && addr < MODULES_END) 19873bdf0a6SLinus Torvalds return 1; 19973bdf0a6SLinus Torvalds #endif 20073bdf0a6SLinus Torvalds return is_vmalloc_addr(x); 20173bdf0a6SLinus Torvalds } 20273bdf0a6SLinus Torvalds 20348667e7aSChristoph Lameter /* 204db64fe02SNick Piggin * Walk a vmap address to the struct page it maps. 20548667e7aSChristoph Lameter */ 206b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *vmalloc_addr) 20748667e7aSChristoph Lameter { 20848667e7aSChristoph Lameter unsigned long addr = (unsigned long) vmalloc_addr; 20948667e7aSChristoph Lameter struct page *page = NULL; 21048667e7aSChristoph Lameter pgd_t *pgd = pgd_offset_k(addr); 21148667e7aSChristoph Lameter 2127aa413deSIngo Molnar /* 2137aa413deSIngo Molnar * XXX we might need to change this if we add VIRTUAL_BUG_ON for 2147aa413deSIngo Molnar * architectures that do not vmalloc module space 2157aa413deSIngo Molnar */ 21673bdf0a6SLinus Torvalds VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); 21759ea7463SJiri Slaby 21848667e7aSChristoph Lameter if (!pgd_none(*pgd)) { 219db64fe02SNick Piggin pud_t *pud = pud_offset(pgd, addr); 22048667e7aSChristoph Lameter if (!pud_none(*pud)) { 221db64fe02SNick Piggin pmd_t *pmd = pmd_offset(pud, addr); 22248667e7aSChristoph Lameter if (!pmd_none(*pmd)) { 223db64fe02SNick Piggin pte_t *ptep, pte; 224db64fe02SNick Piggin 22548667e7aSChristoph Lameter ptep = pte_offset_map(pmd, addr); 22648667e7aSChristoph Lameter pte = *ptep; 22748667e7aSChristoph Lameter if (pte_present(pte)) 22848667e7aSChristoph Lameter page = pte_page(pte); 22948667e7aSChristoph Lameter pte_unmap(ptep); 23048667e7aSChristoph Lameter } 23148667e7aSChristoph Lameter } 23248667e7aSChristoph Lameter } 23348667e7aSChristoph Lameter return page; 23448667e7aSChristoph Lameter } 23548667e7aSChristoph Lameter EXPORT_SYMBOL(vmalloc_to_page); 23648667e7aSChristoph Lameter 23748667e7aSChristoph Lameter /* 23848667e7aSChristoph Lameter * Map a vmalloc()-space virtual address to the physical page frame number. 23948667e7aSChristoph Lameter */ 240b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *vmalloc_addr) 24148667e7aSChristoph Lameter { 24248667e7aSChristoph Lameter return page_to_pfn(vmalloc_to_page(vmalloc_addr)); 24348667e7aSChristoph Lameter } 24448667e7aSChristoph Lameter EXPORT_SYMBOL(vmalloc_to_pfn); 24548667e7aSChristoph Lameter 246db64fe02SNick Piggin 247db64fe02SNick Piggin /*** Global kva allocator ***/ 248db64fe02SNick Piggin 249db64fe02SNick Piggin #define VM_LAZY_FREE 0x01 250db64fe02SNick Piggin #define VM_LAZY_FREEING 0x02 251db64fe02SNick Piggin #define VM_VM_AREA 0x04 252db64fe02SNick Piggin 253db64fe02SNick Piggin struct vmap_area { 254db64fe02SNick Piggin unsigned long va_start; 255db64fe02SNick Piggin unsigned long va_end; 256db64fe02SNick Piggin unsigned long flags; 257db64fe02SNick Piggin struct rb_node rb_node; /* address sorted rbtree */ 258db64fe02SNick Piggin struct list_head list; /* address sorted list */ 259db64fe02SNick Piggin struct list_head purge_list; /* "lazy purge" list */ 260db64fe02SNick Piggin void *private; 261db64fe02SNick Piggin struct rcu_head rcu_head; 262db64fe02SNick Piggin }; 263db64fe02SNick Piggin 264db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock); 265db64fe02SNick Piggin static struct rb_root vmap_area_root = RB_ROOT; 266db64fe02SNick Piggin static LIST_HEAD(vmap_area_list); 267ca23e405STejun Heo static unsigned long vmap_area_pcpu_hole; 268db64fe02SNick Piggin 269db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr) 2701da177e4SLinus Torvalds { 271db64fe02SNick Piggin struct rb_node *n = vmap_area_root.rb_node; 272db64fe02SNick Piggin 273db64fe02SNick Piggin while (n) { 274db64fe02SNick Piggin struct vmap_area *va; 275db64fe02SNick Piggin 276db64fe02SNick Piggin va = rb_entry(n, struct vmap_area, rb_node); 277db64fe02SNick Piggin if (addr < va->va_start) 278db64fe02SNick Piggin n = n->rb_left; 279db64fe02SNick Piggin else if (addr > va->va_start) 280db64fe02SNick Piggin n = n->rb_right; 281db64fe02SNick Piggin else 282db64fe02SNick Piggin return va; 283db64fe02SNick Piggin } 284db64fe02SNick Piggin 285db64fe02SNick Piggin return NULL; 286db64fe02SNick Piggin } 287db64fe02SNick Piggin 288db64fe02SNick Piggin static void __insert_vmap_area(struct vmap_area *va) 289db64fe02SNick Piggin { 290db64fe02SNick Piggin struct rb_node **p = &vmap_area_root.rb_node; 291db64fe02SNick Piggin struct rb_node *parent = NULL; 292db64fe02SNick Piggin struct rb_node *tmp; 293db64fe02SNick Piggin 294db64fe02SNick Piggin while (*p) { 295db64fe02SNick Piggin struct vmap_area *tmp; 296db64fe02SNick Piggin 297db64fe02SNick Piggin parent = *p; 298db64fe02SNick Piggin tmp = rb_entry(parent, struct vmap_area, rb_node); 299db64fe02SNick Piggin if (va->va_start < tmp->va_end) 300db64fe02SNick Piggin p = &(*p)->rb_left; 301db64fe02SNick Piggin else if (va->va_end > tmp->va_start) 302db64fe02SNick Piggin p = &(*p)->rb_right; 303db64fe02SNick Piggin else 304db64fe02SNick Piggin BUG(); 305db64fe02SNick Piggin } 306db64fe02SNick Piggin 307db64fe02SNick Piggin rb_link_node(&va->rb_node, parent, p); 308db64fe02SNick Piggin rb_insert_color(&va->rb_node, &vmap_area_root); 309db64fe02SNick Piggin 310db64fe02SNick Piggin /* address-sort this list so it is usable like the vmlist */ 311db64fe02SNick Piggin tmp = rb_prev(&va->rb_node); 312db64fe02SNick Piggin if (tmp) { 313db64fe02SNick Piggin struct vmap_area *prev; 314db64fe02SNick Piggin prev = rb_entry(tmp, struct vmap_area, rb_node); 315db64fe02SNick Piggin list_add_rcu(&va->list, &prev->list); 316db64fe02SNick Piggin } else 317db64fe02SNick Piggin list_add_rcu(&va->list, &vmap_area_list); 318db64fe02SNick Piggin } 319db64fe02SNick Piggin 320db64fe02SNick Piggin static void purge_vmap_area_lazy(void); 321db64fe02SNick Piggin 322db64fe02SNick Piggin /* 323db64fe02SNick Piggin * Allocate a region of KVA of the specified size and alignment, within the 324db64fe02SNick Piggin * vstart and vend. 325db64fe02SNick Piggin */ 326db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size, 327db64fe02SNick Piggin unsigned long align, 328db64fe02SNick Piggin unsigned long vstart, unsigned long vend, 329db64fe02SNick Piggin int node, gfp_t gfp_mask) 330db64fe02SNick Piggin { 331db64fe02SNick Piggin struct vmap_area *va; 332db64fe02SNick Piggin struct rb_node *n; 3331da177e4SLinus Torvalds unsigned long addr; 334db64fe02SNick Piggin int purged = 0; 335db64fe02SNick Piggin 3367766970cSNick Piggin BUG_ON(!size); 337db64fe02SNick Piggin BUG_ON(size & ~PAGE_MASK); 338db64fe02SNick Piggin 339db64fe02SNick Piggin va = kmalloc_node(sizeof(struct vmap_area), 340db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 341db64fe02SNick Piggin if (unlikely(!va)) 342db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 343db64fe02SNick Piggin 344db64fe02SNick Piggin retry: 3450ae15132SGlauber Costa addr = ALIGN(vstart, align); 3460ae15132SGlauber Costa 347db64fe02SNick Piggin spin_lock(&vmap_area_lock); 3487766970cSNick Piggin if (addr + size - 1 < addr) 3497766970cSNick Piggin goto overflow; 3507766970cSNick Piggin 351db64fe02SNick Piggin /* XXX: could have a last_hole cache */ 352db64fe02SNick Piggin n = vmap_area_root.rb_node; 353db64fe02SNick Piggin if (n) { 354db64fe02SNick Piggin struct vmap_area *first = NULL; 355db64fe02SNick Piggin 356db64fe02SNick Piggin do { 357db64fe02SNick Piggin struct vmap_area *tmp; 358db64fe02SNick Piggin tmp = rb_entry(n, struct vmap_area, rb_node); 359db64fe02SNick Piggin if (tmp->va_end >= addr) { 360db64fe02SNick Piggin if (!first && tmp->va_start < addr + size) 361db64fe02SNick Piggin first = tmp; 362db64fe02SNick Piggin n = n->rb_left; 363db64fe02SNick Piggin } else { 364db64fe02SNick Piggin first = tmp; 365db64fe02SNick Piggin n = n->rb_right; 366db64fe02SNick Piggin } 367db64fe02SNick Piggin } while (n); 368db64fe02SNick Piggin 369db64fe02SNick Piggin if (!first) 370db64fe02SNick Piggin goto found; 371db64fe02SNick Piggin 372db64fe02SNick Piggin if (first->va_end < addr) { 373db64fe02SNick Piggin n = rb_next(&first->rb_node); 374db64fe02SNick Piggin if (n) 375db64fe02SNick Piggin first = rb_entry(n, struct vmap_area, rb_node); 376db64fe02SNick Piggin else 377db64fe02SNick Piggin goto found; 378db64fe02SNick Piggin } 379db64fe02SNick Piggin 380f011c2daSNick Piggin while (addr + size > first->va_start && addr + size <= vend) { 381db64fe02SNick Piggin addr = ALIGN(first->va_end + PAGE_SIZE, align); 3827766970cSNick Piggin if (addr + size - 1 < addr) 3837766970cSNick Piggin goto overflow; 384db64fe02SNick Piggin 385db64fe02SNick Piggin n = rb_next(&first->rb_node); 386db64fe02SNick Piggin if (n) 387db64fe02SNick Piggin first = rb_entry(n, struct vmap_area, rb_node); 388db64fe02SNick Piggin else 389db64fe02SNick Piggin goto found; 390db64fe02SNick Piggin } 391db64fe02SNick Piggin } 392db64fe02SNick Piggin found: 393db64fe02SNick Piggin if (addr + size > vend) { 3947766970cSNick Piggin overflow: 395db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 396db64fe02SNick Piggin if (!purged) { 397db64fe02SNick Piggin purge_vmap_area_lazy(); 398db64fe02SNick Piggin purged = 1; 399db64fe02SNick Piggin goto retry; 400db64fe02SNick Piggin } 401db64fe02SNick Piggin if (printk_ratelimit()) 402c1279c4eSGlauber Costa printk(KERN_WARNING 403c1279c4eSGlauber Costa "vmap allocation for size %lu failed: " 404c1279c4eSGlauber Costa "use vmalloc=<size> to increase size.\n", size); 4052498ce42SRalph Wuerthner kfree(va); 406db64fe02SNick Piggin return ERR_PTR(-EBUSY); 407db64fe02SNick Piggin } 408db64fe02SNick Piggin 409db64fe02SNick Piggin BUG_ON(addr & (align-1)); 410db64fe02SNick Piggin 411db64fe02SNick Piggin va->va_start = addr; 412db64fe02SNick Piggin va->va_end = addr + size; 413db64fe02SNick Piggin va->flags = 0; 414db64fe02SNick Piggin __insert_vmap_area(va); 415db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 416db64fe02SNick Piggin 417db64fe02SNick Piggin return va; 418db64fe02SNick Piggin } 419db64fe02SNick Piggin 420db64fe02SNick Piggin static void rcu_free_va(struct rcu_head *head) 421db64fe02SNick Piggin { 422db64fe02SNick Piggin struct vmap_area *va = container_of(head, struct vmap_area, rcu_head); 423db64fe02SNick Piggin 424db64fe02SNick Piggin kfree(va); 425db64fe02SNick Piggin } 426db64fe02SNick Piggin 427db64fe02SNick Piggin static void __free_vmap_area(struct vmap_area *va) 428db64fe02SNick Piggin { 429db64fe02SNick Piggin BUG_ON(RB_EMPTY_NODE(&va->rb_node)); 430db64fe02SNick Piggin rb_erase(&va->rb_node, &vmap_area_root); 431db64fe02SNick Piggin RB_CLEAR_NODE(&va->rb_node); 432db64fe02SNick Piggin list_del_rcu(&va->list); 433db64fe02SNick Piggin 434ca23e405STejun Heo /* 435ca23e405STejun Heo * Track the highest possible candidate for pcpu area 436ca23e405STejun Heo * allocation. Areas outside of vmalloc area can be returned 437ca23e405STejun Heo * here too, consider only end addresses which fall inside 438ca23e405STejun Heo * vmalloc area proper. 439ca23e405STejun Heo */ 440ca23e405STejun Heo if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END) 441ca23e405STejun Heo vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end); 442ca23e405STejun Heo 443db64fe02SNick Piggin call_rcu(&va->rcu_head, rcu_free_va); 444db64fe02SNick Piggin } 445db64fe02SNick Piggin 446db64fe02SNick Piggin /* 447db64fe02SNick Piggin * Free a region of KVA allocated by alloc_vmap_area 448db64fe02SNick Piggin */ 449db64fe02SNick Piggin static void free_vmap_area(struct vmap_area *va) 450db64fe02SNick Piggin { 451db64fe02SNick Piggin spin_lock(&vmap_area_lock); 452db64fe02SNick Piggin __free_vmap_area(va); 453db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 454db64fe02SNick Piggin } 455db64fe02SNick Piggin 456db64fe02SNick Piggin /* 457db64fe02SNick Piggin * Clear the pagetable entries of a given vmap_area 458db64fe02SNick Piggin */ 459db64fe02SNick Piggin static void unmap_vmap_area(struct vmap_area *va) 460db64fe02SNick Piggin { 461db64fe02SNick Piggin vunmap_page_range(va->va_start, va->va_end); 462db64fe02SNick Piggin } 463db64fe02SNick Piggin 464cd52858cSNick Piggin static void vmap_debug_free_range(unsigned long start, unsigned long end) 465cd52858cSNick Piggin { 466cd52858cSNick Piggin /* 467cd52858cSNick Piggin * Unmap page tables and force a TLB flush immediately if 468cd52858cSNick Piggin * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free 469cd52858cSNick Piggin * bugs similarly to those in linear kernel virtual address 470cd52858cSNick Piggin * space after a page has been freed. 471cd52858cSNick Piggin * 472cd52858cSNick Piggin * All the lazy freeing logic is still retained, in order to 473cd52858cSNick Piggin * minimise intrusiveness of this debugging feature. 474cd52858cSNick Piggin * 475cd52858cSNick Piggin * This is going to be *slow* (linear kernel virtual address 476cd52858cSNick Piggin * debugging doesn't do a broadcast TLB flush so it is a lot 477cd52858cSNick Piggin * faster). 478cd52858cSNick Piggin */ 479cd52858cSNick Piggin #ifdef CONFIG_DEBUG_PAGEALLOC 480cd52858cSNick Piggin vunmap_page_range(start, end); 481cd52858cSNick Piggin flush_tlb_kernel_range(start, end); 482cd52858cSNick Piggin #endif 483cd52858cSNick Piggin } 484cd52858cSNick Piggin 485db64fe02SNick Piggin /* 486db64fe02SNick Piggin * lazy_max_pages is the maximum amount of virtual address space we gather up 487db64fe02SNick Piggin * before attempting to purge with a TLB flush. 488db64fe02SNick Piggin * 489db64fe02SNick Piggin * There is a tradeoff here: a larger number will cover more kernel page tables 490db64fe02SNick Piggin * and take slightly longer to purge, but it will linearly reduce the number of 491db64fe02SNick Piggin * global TLB flushes that must be performed. It would seem natural to scale 492db64fe02SNick Piggin * this number up linearly with the number of CPUs (because vmapping activity 493db64fe02SNick Piggin * could also scale linearly with the number of CPUs), however it is likely 494db64fe02SNick Piggin * that in practice, workloads might be constrained in other ways that mean 495db64fe02SNick Piggin * vmap activity will not scale linearly with CPUs. Also, I want to be 496db64fe02SNick Piggin * conservative and not introduce a big latency on huge systems, so go with 497db64fe02SNick Piggin * a less aggressive log scale. It will still be an improvement over the old 498db64fe02SNick Piggin * code, and it will be simple to change the scale factor if we find that it 499db64fe02SNick Piggin * becomes a problem on bigger systems. 500db64fe02SNick Piggin */ 501db64fe02SNick Piggin static unsigned long lazy_max_pages(void) 502db64fe02SNick Piggin { 503db64fe02SNick Piggin unsigned int log; 504db64fe02SNick Piggin 505db64fe02SNick Piggin log = fls(num_online_cpus()); 506db64fe02SNick Piggin 507db64fe02SNick Piggin return log * (32UL * 1024 * 1024 / PAGE_SIZE); 508db64fe02SNick Piggin } 509db64fe02SNick Piggin 510db64fe02SNick Piggin static atomic_t vmap_lazy_nr = ATOMIC_INIT(0); 511db64fe02SNick Piggin 512db64fe02SNick Piggin /* 513db64fe02SNick Piggin * Purges all lazily-freed vmap areas. 514db64fe02SNick Piggin * 515db64fe02SNick Piggin * If sync is 0 then don't purge if there is already a purge in progress. 516db64fe02SNick Piggin * If force_flush is 1, then flush kernel TLBs between *start and *end even 517db64fe02SNick Piggin * if we found no lazy vmap areas to unmap (callers can use this to optimise 518db64fe02SNick Piggin * their own TLB flushing). 519db64fe02SNick Piggin * Returns with *start = min(*start, lowest purged address) 520db64fe02SNick Piggin * *end = max(*end, highest purged address) 521db64fe02SNick Piggin */ 522db64fe02SNick Piggin static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end, 523db64fe02SNick Piggin int sync, int force_flush) 524db64fe02SNick Piggin { 52546666d8aSAndrew Morton static DEFINE_SPINLOCK(purge_lock); 526db64fe02SNick Piggin LIST_HEAD(valist); 527db64fe02SNick Piggin struct vmap_area *va; 528cbb76676SVegard Nossum struct vmap_area *n_va; 529db64fe02SNick Piggin int nr = 0; 530db64fe02SNick Piggin 531db64fe02SNick Piggin /* 532db64fe02SNick Piggin * If sync is 0 but force_flush is 1, we'll go sync anyway but callers 533db64fe02SNick Piggin * should not expect such behaviour. This just simplifies locking for 534db64fe02SNick Piggin * the case that isn't actually used at the moment anyway. 535db64fe02SNick Piggin */ 536db64fe02SNick Piggin if (!sync && !force_flush) { 53746666d8aSAndrew Morton if (!spin_trylock(&purge_lock)) 538db64fe02SNick Piggin return; 539db64fe02SNick Piggin } else 54046666d8aSAndrew Morton spin_lock(&purge_lock); 541db64fe02SNick Piggin 542db64fe02SNick Piggin rcu_read_lock(); 543db64fe02SNick Piggin list_for_each_entry_rcu(va, &vmap_area_list, list) { 544db64fe02SNick Piggin if (va->flags & VM_LAZY_FREE) { 545db64fe02SNick Piggin if (va->va_start < *start) 546db64fe02SNick Piggin *start = va->va_start; 547db64fe02SNick Piggin if (va->va_end > *end) 548db64fe02SNick Piggin *end = va->va_end; 549db64fe02SNick Piggin nr += (va->va_end - va->va_start) >> PAGE_SHIFT; 550db64fe02SNick Piggin unmap_vmap_area(va); 551db64fe02SNick Piggin list_add_tail(&va->purge_list, &valist); 552db64fe02SNick Piggin va->flags |= VM_LAZY_FREEING; 553db64fe02SNick Piggin va->flags &= ~VM_LAZY_FREE; 554db64fe02SNick Piggin } 555db64fe02SNick Piggin } 556db64fe02SNick Piggin rcu_read_unlock(); 557db64fe02SNick Piggin 55888f50044SYongseok Koh if (nr) 559db64fe02SNick Piggin atomic_sub(nr, &vmap_lazy_nr); 560db64fe02SNick Piggin 561db64fe02SNick Piggin if (nr || force_flush) 562db64fe02SNick Piggin flush_tlb_kernel_range(*start, *end); 563db64fe02SNick Piggin 564db64fe02SNick Piggin if (nr) { 565db64fe02SNick Piggin spin_lock(&vmap_area_lock); 566cbb76676SVegard Nossum list_for_each_entry_safe(va, n_va, &valist, purge_list) 567db64fe02SNick Piggin __free_vmap_area(va); 568db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 569db64fe02SNick Piggin } 57046666d8aSAndrew Morton spin_unlock(&purge_lock); 571db64fe02SNick Piggin } 572db64fe02SNick Piggin 573db64fe02SNick Piggin /* 574496850e5SNick Piggin * Kick off a purge of the outstanding lazy areas. Don't bother if somebody 575496850e5SNick Piggin * is already purging. 576496850e5SNick Piggin */ 577496850e5SNick Piggin static void try_purge_vmap_area_lazy(void) 578496850e5SNick Piggin { 579496850e5SNick Piggin unsigned long start = ULONG_MAX, end = 0; 580496850e5SNick Piggin 581496850e5SNick Piggin __purge_vmap_area_lazy(&start, &end, 0, 0); 582496850e5SNick Piggin } 583496850e5SNick Piggin 584496850e5SNick Piggin /* 585db64fe02SNick Piggin * Kick off a purge of the outstanding lazy areas. 586db64fe02SNick Piggin */ 587db64fe02SNick Piggin static void purge_vmap_area_lazy(void) 588db64fe02SNick Piggin { 589db64fe02SNick Piggin unsigned long start = ULONG_MAX, end = 0; 590db64fe02SNick Piggin 591496850e5SNick Piggin __purge_vmap_area_lazy(&start, &end, 1, 0); 592db64fe02SNick Piggin } 593db64fe02SNick Piggin 594db64fe02SNick Piggin /* 595b29acbdcSNick Piggin * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been 596b29acbdcSNick Piggin * called for the correct range previously. 597db64fe02SNick Piggin */ 598b29acbdcSNick Piggin static void free_unmap_vmap_area_noflush(struct vmap_area *va) 599db64fe02SNick Piggin { 600db64fe02SNick Piggin va->flags |= VM_LAZY_FREE; 601db64fe02SNick Piggin atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr); 602db64fe02SNick Piggin if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages())) 603496850e5SNick Piggin try_purge_vmap_area_lazy(); 604db64fe02SNick Piggin } 605db64fe02SNick Piggin 606b29acbdcSNick Piggin /* 607b29acbdcSNick Piggin * Free and unmap a vmap area 608b29acbdcSNick Piggin */ 609b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va) 610b29acbdcSNick Piggin { 611b29acbdcSNick Piggin flush_cache_vunmap(va->va_start, va->va_end); 612b29acbdcSNick Piggin free_unmap_vmap_area_noflush(va); 613b29acbdcSNick Piggin } 614b29acbdcSNick Piggin 615db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr) 616db64fe02SNick Piggin { 617db64fe02SNick Piggin struct vmap_area *va; 618db64fe02SNick Piggin 619db64fe02SNick Piggin spin_lock(&vmap_area_lock); 620db64fe02SNick Piggin va = __find_vmap_area(addr); 621db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 622db64fe02SNick Piggin 623db64fe02SNick Piggin return va; 624db64fe02SNick Piggin } 625db64fe02SNick Piggin 626db64fe02SNick Piggin static void free_unmap_vmap_area_addr(unsigned long addr) 627db64fe02SNick Piggin { 628db64fe02SNick Piggin struct vmap_area *va; 629db64fe02SNick Piggin 630db64fe02SNick Piggin va = find_vmap_area(addr); 631db64fe02SNick Piggin BUG_ON(!va); 632db64fe02SNick Piggin free_unmap_vmap_area(va); 633db64fe02SNick Piggin } 634db64fe02SNick Piggin 635db64fe02SNick Piggin 636db64fe02SNick Piggin /*** Per cpu kva allocator ***/ 637db64fe02SNick Piggin 638db64fe02SNick Piggin /* 639db64fe02SNick Piggin * vmap space is limited especially on 32 bit architectures. Ensure there is 640db64fe02SNick Piggin * room for at least 16 percpu vmap blocks per CPU. 641db64fe02SNick Piggin */ 642db64fe02SNick Piggin /* 643db64fe02SNick Piggin * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able 644db64fe02SNick Piggin * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess 645db64fe02SNick Piggin * instead (we just need a rough idea) 646db64fe02SNick Piggin */ 647db64fe02SNick Piggin #if BITS_PER_LONG == 32 648db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024) 649db64fe02SNick Piggin #else 650db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024*1024) 651db64fe02SNick Piggin #endif 652db64fe02SNick Piggin 653db64fe02SNick Piggin #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) 654db64fe02SNick Piggin #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ 655db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ 656db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) 657db64fe02SNick Piggin #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ 658db64fe02SNick Piggin #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ 659db64fe02SNick Piggin #define VMAP_BBMAP_BITS VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ 660db64fe02SNick Piggin VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ 661db64fe02SNick Piggin VMALLOC_PAGES / NR_CPUS / 16)) 662db64fe02SNick Piggin 663db64fe02SNick Piggin #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) 664db64fe02SNick Piggin 6659b463334SJeremy Fitzhardinge static bool vmap_initialized __read_mostly = false; 6669b463334SJeremy Fitzhardinge 667db64fe02SNick Piggin struct vmap_block_queue { 668db64fe02SNick Piggin spinlock_t lock; 669db64fe02SNick Piggin struct list_head free; 670db64fe02SNick Piggin }; 671db64fe02SNick Piggin 672db64fe02SNick Piggin struct vmap_block { 673db64fe02SNick Piggin spinlock_t lock; 674db64fe02SNick Piggin struct vmap_area *va; 675db64fe02SNick Piggin struct vmap_block_queue *vbq; 676db64fe02SNick Piggin unsigned long free, dirty; 677db64fe02SNick Piggin DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS); 678db64fe02SNick Piggin DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS); 679db64fe02SNick Piggin struct list_head free_list; 680db64fe02SNick Piggin struct rcu_head rcu_head; 681db64fe02SNick Piggin }; 682db64fe02SNick Piggin 683db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ 684db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); 685db64fe02SNick Piggin 686db64fe02SNick Piggin /* 687db64fe02SNick Piggin * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block 688db64fe02SNick Piggin * in the free path. Could get rid of this if we change the API to return a 689db64fe02SNick Piggin * "cookie" from alloc, to be passed to free. But no big deal yet. 690db64fe02SNick Piggin */ 691db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_block_tree_lock); 692db64fe02SNick Piggin static RADIX_TREE(vmap_block_tree, GFP_ATOMIC); 693db64fe02SNick Piggin 694db64fe02SNick Piggin /* 695db64fe02SNick Piggin * We should probably have a fallback mechanism to allocate virtual memory 696db64fe02SNick Piggin * out of partially filled vmap blocks. However vmap block sizing should be 697db64fe02SNick Piggin * fairly reasonable according to the vmalloc size, so it shouldn't be a 698db64fe02SNick Piggin * big problem. 699db64fe02SNick Piggin */ 700db64fe02SNick Piggin 701db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr) 702db64fe02SNick Piggin { 703db64fe02SNick Piggin addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); 704db64fe02SNick Piggin addr /= VMAP_BLOCK_SIZE; 705db64fe02SNick Piggin return addr; 706db64fe02SNick Piggin } 707db64fe02SNick Piggin 708db64fe02SNick Piggin static struct vmap_block *new_vmap_block(gfp_t gfp_mask) 709db64fe02SNick Piggin { 710db64fe02SNick Piggin struct vmap_block_queue *vbq; 711db64fe02SNick Piggin struct vmap_block *vb; 712db64fe02SNick Piggin struct vmap_area *va; 713db64fe02SNick Piggin unsigned long vb_idx; 714db64fe02SNick Piggin int node, err; 715db64fe02SNick Piggin 716db64fe02SNick Piggin node = numa_node_id(); 717db64fe02SNick Piggin 718db64fe02SNick Piggin vb = kmalloc_node(sizeof(struct vmap_block), 719db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 720db64fe02SNick Piggin if (unlikely(!vb)) 721db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 722db64fe02SNick Piggin 723db64fe02SNick Piggin va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, 724db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, 725db64fe02SNick Piggin node, gfp_mask); 726db64fe02SNick Piggin if (unlikely(IS_ERR(va))) { 727db64fe02SNick Piggin kfree(vb); 728db64fe02SNick Piggin return ERR_PTR(PTR_ERR(va)); 729db64fe02SNick Piggin } 730db64fe02SNick Piggin 731db64fe02SNick Piggin err = radix_tree_preload(gfp_mask); 732db64fe02SNick Piggin if (unlikely(err)) { 733db64fe02SNick Piggin kfree(vb); 734db64fe02SNick Piggin free_vmap_area(va); 735db64fe02SNick Piggin return ERR_PTR(err); 736db64fe02SNick Piggin } 737db64fe02SNick Piggin 738db64fe02SNick Piggin spin_lock_init(&vb->lock); 739db64fe02SNick Piggin vb->va = va; 740db64fe02SNick Piggin vb->free = VMAP_BBMAP_BITS; 741db64fe02SNick Piggin vb->dirty = 0; 742db64fe02SNick Piggin bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS); 743db64fe02SNick Piggin bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS); 744db64fe02SNick Piggin INIT_LIST_HEAD(&vb->free_list); 745db64fe02SNick Piggin 746db64fe02SNick Piggin vb_idx = addr_to_vb_idx(va->va_start); 747db64fe02SNick Piggin spin_lock(&vmap_block_tree_lock); 748db64fe02SNick Piggin err = radix_tree_insert(&vmap_block_tree, vb_idx, vb); 749db64fe02SNick Piggin spin_unlock(&vmap_block_tree_lock); 750db64fe02SNick Piggin BUG_ON(err); 751db64fe02SNick Piggin radix_tree_preload_end(); 752db64fe02SNick Piggin 753db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 754db64fe02SNick Piggin vb->vbq = vbq; 755db64fe02SNick Piggin spin_lock(&vbq->lock); 756*de560423SNick Piggin list_add_rcu(&vb->free_list, &vbq->free); 757db64fe02SNick Piggin spin_unlock(&vbq->lock); 7583f04ba85STejun Heo put_cpu_var(vmap_block_queue); 759db64fe02SNick Piggin 760db64fe02SNick Piggin return vb; 761db64fe02SNick Piggin } 762db64fe02SNick Piggin 763db64fe02SNick Piggin static void rcu_free_vb(struct rcu_head *head) 764db64fe02SNick Piggin { 765db64fe02SNick Piggin struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head); 766db64fe02SNick Piggin 767db64fe02SNick Piggin kfree(vb); 768db64fe02SNick Piggin } 769db64fe02SNick Piggin 770db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb) 771db64fe02SNick Piggin { 772db64fe02SNick Piggin struct vmap_block *tmp; 773db64fe02SNick Piggin unsigned long vb_idx; 774db64fe02SNick Piggin 775db64fe02SNick Piggin vb_idx = addr_to_vb_idx(vb->va->va_start); 776db64fe02SNick Piggin spin_lock(&vmap_block_tree_lock); 777db64fe02SNick Piggin tmp = radix_tree_delete(&vmap_block_tree, vb_idx); 778db64fe02SNick Piggin spin_unlock(&vmap_block_tree_lock); 779db64fe02SNick Piggin BUG_ON(tmp != vb); 780db64fe02SNick Piggin 781b29acbdcSNick Piggin free_unmap_vmap_area_noflush(vb->va); 782db64fe02SNick Piggin call_rcu(&vb->rcu_head, rcu_free_vb); 783db64fe02SNick Piggin } 784db64fe02SNick Piggin 785db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask) 786db64fe02SNick Piggin { 787db64fe02SNick Piggin struct vmap_block_queue *vbq; 788db64fe02SNick Piggin struct vmap_block *vb; 789db64fe02SNick Piggin unsigned long addr = 0; 790db64fe02SNick Piggin unsigned int order; 791db64fe02SNick Piggin 792db64fe02SNick Piggin BUG_ON(size & ~PAGE_MASK); 793db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 794db64fe02SNick Piggin order = get_order(size); 795db64fe02SNick Piggin 796db64fe02SNick Piggin again: 797db64fe02SNick Piggin rcu_read_lock(); 798db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 799db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 800db64fe02SNick Piggin int i; 801db64fe02SNick Piggin 802db64fe02SNick Piggin spin_lock(&vb->lock); 803db64fe02SNick Piggin i = bitmap_find_free_region(vb->alloc_map, 804db64fe02SNick Piggin VMAP_BBMAP_BITS, order); 805db64fe02SNick Piggin 806db64fe02SNick Piggin if (i >= 0) { 807db64fe02SNick Piggin addr = vb->va->va_start + (i << PAGE_SHIFT); 808db64fe02SNick Piggin BUG_ON(addr_to_vb_idx(addr) != 809db64fe02SNick Piggin addr_to_vb_idx(vb->va->va_start)); 810db64fe02SNick Piggin vb->free -= 1UL << order; 811db64fe02SNick Piggin if (vb->free == 0) { 812db64fe02SNick Piggin spin_lock(&vbq->lock); 813*de560423SNick Piggin list_del_rcu(&vb->free_list); 814db64fe02SNick Piggin spin_unlock(&vbq->lock); 815db64fe02SNick Piggin } 816db64fe02SNick Piggin spin_unlock(&vb->lock); 817db64fe02SNick Piggin break; 818db64fe02SNick Piggin } 819db64fe02SNick Piggin spin_unlock(&vb->lock); 820db64fe02SNick Piggin } 8213f04ba85STejun Heo put_cpu_var(vmap_block_queue); 822db64fe02SNick Piggin rcu_read_unlock(); 823db64fe02SNick Piggin 824db64fe02SNick Piggin if (!addr) { 825db64fe02SNick Piggin vb = new_vmap_block(gfp_mask); 826db64fe02SNick Piggin if (IS_ERR(vb)) 827db64fe02SNick Piggin return vb; 828db64fe02SNick Piggin goto again; 829db64fe02SNick Piggin } 830db64fe02SNick Piggin 831db64fe02SNick Piggin return (void *)addr; 832db64fe02SNick Piggin } 833db64fe02SNick Piggin 834db64fe02SNick Piggin static void vb_free(const void *addr, unsigned long size) 835db64fe02SNick Piggin { 836db64fe02SNick Piggin unsigned long offset; 837db64fe02SNick Piggin unsigned long vb_idx; 838db64fe02SNick Piggin unsigned int order; 839db64fe02SNick Piggin struct vmap_block *vb; 840db64fe02SNick Piggin 841db64fe02SNick Piggin BUG_ON(size & ~PAGE_MASK); 842db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 843b29acbdcSNick Piggin 844b29acbdcSNick Piggin flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size); 845b29acbdcSNick Piggin 846db64fe02SNick Piggin order = get_order(size); 847db64fe02SNick Piggin 848db64fe02SNick Piggin offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1); 849db64fe02SNick Piggin 850db64fe02SNick Piggin vb_idx = addr_to_vb_idx((unsigned long)addr); 851db64fe02SNick Piggin rcu_read_lock(); 852db64fe02SNick Piggin vb = radix_tree_lookup(&vmap_block_tree, vb_idx); 853db64fe02SNick Piggin rcu_read_unlock(); 854db64fe02SNick Piggin BUG_ON(!vb); 855db64fe02SNick Piggin 856db64fe02SNick Piggin spin_lock(&vb->lock); 857*de560423SNick Piggin BUG_ON(bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order)); 858d086817dSMinChan Kim 859db64fe02SNick Piggin vb->dirty += 1UL << order; 860db64fe02SNick Piggin if (vb->dirty == VMAP_BBMAP_BITS) { 861*de560423SNick Piggin BUG_ON(vb->free); 862db64fe02SNick Piggin spin_unlock(&vb->lock); 863db64fe02SNick Piggin free_vmap_block(vb); 864db64fe02SNick Piggin } else 865db64fe02SNick Piggin spin_unlock(&vb->lock); 866db64fe02SNick Piggin } 867db64fe02SNick Piggin 868db64fe02SNick Piggin /** 869db64fe02SNick Piggin * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer 870db64fe02SNick Piggin * 871db64fe02SNick Piggin * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily 872db64fe02SNick Piggin * to amortize TLB flushing overheads. What this means is that any page you 873db64fe02SNick Piggin * have now, may, in a former life, have been mapped into kernel virtual 874db64fe02SNick Piggin * address by the vmap layer and so there might be some CPUs with TLB entries 875db64fe02SNick Piggin * still referencing that page (additional to the regular 1:1 kernel mapping). 876db64fe02SNick Piggin * 877db64fe02SNick Piggin * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can 878db64fe02SNick Piggin * be sure that none of the pages we have control over will have any aliases 879db64fe02SNick Piggin * from the vmap layer. 880db64fe02SNick Piggin */ 881db64fe02SNick Piggin void vm_unmap_aliases(void) 882db64fe02SNick Piggin { 883db64fe02SNick Piggin unsigned long start = ULONG_MAX, end = 0; 884db64fe02SNick Piggin int cpu; 885db64fe02SNick Piggin int flush = 0; 886db64fe02SNick Piggin 8879b463334SJeremy Fitzhardinge if (unlikely(!vmap_initialized)) 8889b463334SJeremy Fitzhardinge return; 8899b463334SJeremy Fitzhardinge 890db64fe02SNick Piggin for_each_possible_cpu(cpu) { 891db64fe02SNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 892db64fe02SNick Piggin struct vmap_block *vb; 893db64fe02SNick Piggin 894db64fe02SNick Piggin rcu_read_lock(); 895db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 896db64fe02SNick Piggin int i; 897db64fe02SNick Piggin 898db64fe02SNick Piggin spin_lock(&vb->lock); 899db64fe02SNick Piggin i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS); 900db64fe02SNick Piggin while (i < VMAP_BBMAP_BITS) { 901db64fe02SNick Piggin unsigned long s, e; 902db64fe02SNick Piggin int j; 903db64fe02SNick Piggin j = find_next_zero_bit(vb->dirty_map, 904db64fe02SNick Piggin VMAP_BBMAP_BITS, i); 905db64fe02SNick Piggin 906db64fe02SNick Piggin s = vb->va->va_start + (i << PAGE_SHIFT); 907db64fe02SNick Piggin e = vb->va->va_start + (j << PAGE_SHIFT); 908db64fe02SNick Piggin vunmap_page_range(s, e); 909db64fe02SNick Piggin flush = 1; 910db64fe02SNick Piggin 911db64fe02SNick Piggin if (s < start) 912db64fe02SNick Piggin start = s; 913db64fe02SNick Piggin if (e > end) 914db64fe02SNick Piggin end = e; 915db64fe02SNick Piggin 916db64fe02SNick Piggin i = j; 917db64fe02SNick Piggin i = find_next_bit(vb->dirty_map, 918db64fe02SNick Piggin VMAP_BBMAP_BITS, i); 919db64fe02SNick Piggin } 920db64fe02SNick Piggin spin_unlock(&vb->lock); 921db64fe02SNick Piggin } 922db64fe02SNick Piggin rcu_read_unlock(); 923db64fe02SNick Piggin } 924db64fe02SNick Piggin 925db64fe02SNick Piggin __purge_vmap_area_lazy(&start, &end, 1, flush); 926db64fe02SNick Piggin } 927db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases); 928db64fe02SNick Piggin 929db64fe02SNick Piggin /** 930db64fe02SNick Piggin * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram 931db64fe02SNick Piggin * @mem: the pointer returned by vm_map_ram 932db64fe02SNick Piggin * @count: the count passed to that vm_map_ram call (cannot unmap partial) 933db64fe02SNick Piggin */ 934db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count) 935db64fe02SNick Piggin { 936db64fe02SNick Piggin unsigned long size = count << PAGE_SHIFT; 937db64fe02SNick Piggin unsigned long addr = (unsigned long)mem; 938db64fe02SNick Piggin 939db64fe02SNick Piggin BUG_ON(!addr); 940db64fe02SNick Piggin BUG_ON(addr < VMALLOC_START); 941db64fe02SNick Piggin BUG_ON(addr > VMALLOC_END); 942db64fe02SNick Piggin BUG_ON(addr & (PAGE_SIZE-1)); 943db64fe02SNick Piggin 944db64fe02SNick Piggin debug_check_no_locks_freed(mem, size); 945cd52858cSNick Piggin vmap_debug_free_range(addr, addr+size); 946db64fe02SNick Piggin 947db64fe02SNick Piggin if (likely(count <= VMAP_MAX_ALLOC)) 948db64fe02SNick Piggin vb_free(mem, size); 949db64fe02SNick Piggin else 950db64fe02SNick Piggin free_unmap_vmap_area_addr(addr); 951db64fe02SNick Piggin } 952db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram); 953db64fe02SNick Piggin 954db64fe02SNick Piggin /** 955db64fe02SNick Piggin * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) 956db64fe02SNick Piggin * @pages: an array of pointers to the pages to be mapped 957db64fe02SNick Piggin * @count: number of pages 958db64fe02SNick Piggin * @node: prefer to allocate data structures on this node 959db64fe02SNick Piggin * @prot: memory protection to use. PAGE_KERNEL for regular RAM 960e99c97adSRandy Dunlap * 961e99c97adSRandy Dunlap * Returns: a pointer to the address that has been mapped, or %NULL on failure 962db64fe02SNick Piggin */ 963db64fe02SNick Piggin void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) 964db64fe02SNick Piggin { 965db64fe02SNick Piggin unsigned long size = count << PAGE_SHIFT; 966db64fe02SNick Piggin unsigned long addr; 967db64fe02SNick Piggin void *mem; 968db64fe02SNick Piggin 969db64fe02SNick Piggin if (likely(count <= VMAP_MAX_ALLOC)) { 970db64fe02SNick Piggin mem = vb_alloc(size, GFP_KERNEL); 971db64fe02SNick Piggin if (IS_ERR(mem)) 972db64fe02SNick Piggin return NULL; 973db64fe02SNick Piggin addr = (unsigned long)mem; 974db64fe02SNick Piggin } else { 975db64fe02SNick Piggin struct vmap_area *va; 976db64fe02SNick Piggin va = alloc_vmap_area(size, PAGE_SIZE, 977db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); 978db64fe02SNick Piggin if (IS_ERR(va)) 979db64fe02SNick Piggin return NULL; 980db64fe02SNick Piggin 981db64fe02SNick Piggin addr = va->va_start; 982db64fe02SNick Piggin mem = (void *)addr; 983db64fe02SNick Piggin } 984db64fe02SNick Piggin if (vmap_page_range(addr, addr + size, prot, pages) < 0) { 985db64fe02SNick Piggin vm_unmap_ram(mem, count); 986db64fe02SNick Piggin return NULL; 987db64fe02SNick Piggin } 988db64fe02SNick Piggin return mem; 989db64fe02SNick Piggin } 990db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram); 991db64fe02SNick Piggin 992f0aa6617STejun Heo /** 993f0aa6617STejun Heo * vm_area_register_early - register vmap area early during boot 994f0aa6617STejun Heo * @vm: vm_struct to register 995c0c0a293STejun Heo * @align: requested alignment 996f0aa6617STejun Heo * 997f0aa6617STejun Heo * This function is used to register kernel vm area before 998f0aa6617STejun Heo * vmalloc_init() is called. @vm->size and @vm->flags should contain 999f0aa6617STejun Heo * proper values on entry and other fields should be zero. On return, 1000f0aa6617STejun Heo * vm->addr contains the allocated address. 1001f0aa6617STejun Heo * 1002f0aa6617STejun Heo * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 1003f0aa6617STejun Heo */ 1004c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align) 1005f0aa6617STejun Heo { 1006f0aa6617STejun Heo static size_t vm_init_off __initdata; 1007c0c0a293STejun Heo unsigned long addr; 1008f0aa6617STejun Heo 1009c0c0a293STejun Heo addr = ALIGN(VMALLOC_START + vm_init_off, align); 1010c0c0a293STejun Heo vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START; 1011c0c0a293STejun Heo 1012c0c0a293STejun Heo vm->addr = (void *)addr; 1013f0aa6617STejun Heo 1014f0aa6617STejun Heo vm->next = vmlist; 1015f0aa6617STejun Heo vmlist = vm; 1016f0aa6617STejun Heo } 1017f0aa6617STejun Heo 1018db64fe02SNick Piggin void __init vmalloc_init(void) 1019db64fe02SNick Piggin { 1020822c18f2SIvan Kokshaysky struct vmap_area *va; 1021822c18f2SIvan Kokshaysky struct vm_struct *tmp; 1022db64fe02SNick Piggin int i; 1023db64fe02SNick Piggin 1024db64fe02SNick Piggin for_each_possible_cpu(i) { 1025db64fe02SNick Piggin struct vmap_block_queue *vbq; 1026db64fe02SNick Piggin 1027db64fe02SNick Piggin vbq = &per_cpu(vmap_block_queue, i); 1028db64fe02SNick Piggin spin_lock_init(&vbq->lock); 1029db64fe02SNick Piggin INIT_LIST_HEAD(&vbq->free); 1030db64fe02SNick Piggin } 10319b463334SJeremy Fitzhardinge 1032822c18f2SIvan Kokshaysky /* Import existing vmlist entries. */ 1033822c18f2SIvan Kokshaysky for (tmp = vmlist; tmp; tmp = tmp->next) { 103443ebdac4SPekka Enberg va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT); 1035822c18f2SIvan Kokshaysky va->flags = tmp->flags | VM_VM_AREA; 1036822c18f2SIvan Kokshaysky va->va_start = (unsigned long)tmp->addr; 1037822c18f2SIvan Kokshaysky va->va_end = va->va_start + tmp->size; 1038822c18f2SIvan Kokshaysky __insert_vmap_area(va); 1039822c18f2SIvan Kokshaysky } 1040ca23e405STejun Heo 1041ca23e405STejun Heo vmap_area_pcpu_hole = VMALLOC_END; 1042ca23e405STejun Heo 10439b463334SJeremy Fitzhardinge vmap_initialized = true; 1044db64fe02SNick Piggin } 1045db64fe02SNick Piggin 10468fc48985STejun Heo /** 10478fc48985STejun Heo * map_kernel_range_noflush - map kernel VM area with the specified pages 10488fc48985STejun Heo * @addr: start of the VM area to map 10498fc48985STejun Heo * @size: size of the VM area to map 10508fc48985STejun Heo * @prot: page protection flags to use 10518fc48985STejun Heo * @pages: pages to map 10528fc48985STejun Heo * 10538fc48985STejun Heo * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size 10548fc48985STejun Heo * specify should have been allocated using get_vm_area() and its 10558fc48985STejun Heo * friends. 10568fc48985STejun Heo * 10578fc48985STejun Heo * NOTE: 10588fc48985STejun Heo * This function does NOT do any cache flushing. The caller is 10598fc48985STejun Heo * responsible for calling flush_cache_vmap() on to-be-mapped areas 10608fc48985STejun Heo * before calling this function. 10618fc48985STejun Heo * 10628fc48985STejun Heo * RETURNS: 10638fc48985STejun Heo * The number of pages mapped on success, -errno on failure. 10648fc48985STejun Heo */ 10658fc48985STejun Heo int map_kernel_range_noflush(unsigned long addr, unsigned long size, 10668fc48985STejun Heo pgprot_t prot, struct page **pages) 10678fc48985STejun Heo { 10688fc48985STejun Heo return vmap_page_range_noflush(addr, addr + size, prot, pages); 10698fc48985STejun Heo } 10708fc48985STejun Heo 10718fc48985STejun Heo /** 10728fc48985STejun Heo * unmap_kernel_range_noflush - unmap kernel VM area 10738fc48985STejun Heo * @addr: start of the VM area to unmap 10748fc48985STejun Heo * @size: size of the VM area to unmap 10758fc48985STejun Heo * 10768fc48985STejun Heo * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size 10778fc48985STejun Heo * specify should have been allocated using get_vm_area() and its 10788fc48985STejun Heo * friends. 10798fc48985STejun Heo * 10808fc48985STejun Heo * NOTE: 10818fc48985STejun Heo * This function does NOT do any cache flushing. The caller is 10828fc48985STejun Heo * responsible for calling flush_cache_vunmap() on to-be-mapped areas 10838fc48985STejun Heo * before calling this function and flush_tlb_kernel_range() after. 10848fc48985STejun Heo */ 10858fc48985STejun Heo void unmap_kernel_range_noflush(unsigned long addr, unsigned long size) 10868fc48985STejun Heo { 10878fc48985STejun Heo vunmap_page_range(addr, addr + size); 10888fc48985STejun Heo } 10898fc48985STejun Heo 10908fc48985STejun Heo /** 10918fc48985STejun Heo * unmap_kernel_range - unmap kernel VM area and flush cache and TLB 10928fc48985STejun Heo * @addr: start of the VM area to unmap 10938fc48985STejun Heo * @size: size of the VM area to unmap 10948fc48985STejun Heo * 10958fc48985STejun Heo * Similar to unmap_kernel_range_noflush() but flushes vcache before 10968fc48985STejun Heo * the unmapping and tlb after. 10978fc48985STejun Heo */ 1098db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size) 1099db64fe02SNick Piggin { 1100db64fe02SNick Piggin unsigned long end = addr + size; 1101f6fcba70STejun Heo 1102f6fcba70STejun Heo flush_cache_vunmap(addr, end); 1103db64fe02SNick Piggin vunmap_page_range(addr, end); 1104db64fe02SNick Piggin flush_tlb_kernel_range(addr, end); 1105db64fe02SNick Piggin } 1106db64fe02SNick Piggin 1107db64fe02SNick Piggin int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages) 1108db64fe02SNick Piggin { 1109db64fe02SNick Piggin unsigned long addr = (unsigned long)area->addr; 1110db64fe02SNick Piggin unsigned long end = addr + area->size - PAGE_SIZE; 1111db64fe02SNick Piggin int err; 1112db64fe02SNick Piggin 1113db64fe02SNick Piggin err = vmap_page_range(addr, end, prot, *pages); 1114db64fe02SNick Piggin if (err > 0) { 1115db64fe02SNick Piggin *pages += err; 1116db64fe02SNick Piggin err = 0; 1117db64fe02SNick Piggin } 1118db64fe02SNick Piggin 1119db64fe02SNick Piggin return err; 1120db64fe02SNick Piggin } 1121db64fe02SNick Piggin EXPORT_SYMBOL_GPL(map_vm_area); 1122db64fe02SNick Piggin 1123db64fe02SNick Piggin /*** Old vmalloc interfaces ***/ 1124db64fe02SNick Piggin DEFINE_RWLOCK(vmlist_lock); 1125db64fe02SNick Piggin struct vm_struct *vmlist; 1126db64fe02SNick Piggin 1127cf88c790STejun Heo static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, 1128cf88c790STejun Heo unsigned long flags, void *caller) 1129cf88c790STejun Heo { 1130cf88c790STejun Heo struct vm_struct *tmp, **p; 1131cf88c790STejun Heo 1132cf88c790STejun Heo vm->flags = flags; 1133cf88c790STejun Heo vm->addr = (void *)va->va_start; 1134cf88c790STejun Heo vm->size = va->va_end - va->va_start; 1135cf88c790STejun Heo vm->caller = caller; 1136cf88c790STejun Heo va->private = vm; 1137cf88c790STejun Heo va->flags |= VM_VM_AREA; 1138cf88c790STejun Heo 1139cf88c790STejun Heo write_lock(&vmlist_lock); 1140cf88c790STejun Heo for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { 1141cf88c790STejun Heo if (tmp->addr >= vm->addr) 1142cf88c790STejun Heo break; 1143cf88c790STejun Heo } 1144cf88c790STejun Heo vm->next = *p; 1145cf88c790STejun Heo *p = vm; 1146cf88c790STejun Heo write_unlock(&vmlist_lock); 1147cf88c790STejun Heo } 1148cf88c790STejun Heo 1149db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size, 11502dca6999SDavid Miller unsigned long align, unsigned long flags, unsigned long start, 11512dca6999SDavid Miller unsigned long end, int node, gfp_t gfp_mask, void *caller) 1152db64fe02SNick Piggin { 1153db64fe02SNick Piggin static struct vmap_area *va; 1154db64fe02SNick Piggin struct vm_struct *area; 11551da177e4SLinus Torvalds 115652fd24caSGiridhar Pemmasani BUG_ON(in_interrupt()); 11571da177e4SLinus Torvalds if (flags & VM_IOREMAP) { 11581da177e4SLinus Torvalds int bit = fls(size); 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds if (bit > IOREMAP_MAX_ORDER) 11611da177e4SLinus Torvalds bit = IOREMAP_MAX_ORDER; 11621da177e4SLinus Torvalds else if (bit < PAGE_SHIFT) 11631da177e4SLinus Torvalds bit = PAGE_SHIFT; 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds align = 1ul << bit; 11661da177e4SLinus Torvalds } 1167db64fe02SNick Piggin 11681da177e4SLinus Torvalds size = PAGE_ALIGN(size); 116931be8309SOGAWA Hirofumi if (unlikely(!size)) 117031be8309SOGAWA Hirofumi return NULL; 11711da177e4SLinus Torvalds 1172cf88c790STejun Heo area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); 11731da177e4SLinus Torvalds if (unlikely(!area)) 11741da177e4SLinus Torvalds return NULL; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds /* 11771da177e4SLinus Torvalds * We always allocate a guard page. 11781da177e4SLinus Torvalds */ 11791da177e4SLinus Torvalds size += PAGE_SIZE; 11801da177e4SLinus Torvalds 1181db64fe02SNick Piggin va = alloc_vmap_area(size, align, start, end, node, gfp_mask); 1182db64fe02SNick Piggin if (IS_ERR(va)) { 1183db64fe02SNick Piggin kfree(area); 1184db64fe02SNick Piggin return NULL; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 1187cf88c790STejun Heo insert_vmalloc_vm(area, va, flags, caller); 11881da177e4SLinus Torvalds return area; 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds 1191930fc45aSChristoph Lameter struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags, 1192930fc45aSChristoph Lameter unsigned long start, unsigned long end) 1193930fc45aSChristoph Lameter { 11942dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, start, end, -1, GFP_KERNEL, 119523016969SChristoph Lameter __builtin_return_address(0)); 1196930fc45aSChristoph Lameter } 11975992b6daSRusty Russell EXPORT_SYMBOL_GPL(__get_vm_area); 1198930fc45aSChristoph Lameter 1199c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, 1200c2968612SBenjamin Herrenschmidt unsigned long start, unsigned long end, 1201c2968612SBenjamin Herrenschmidt void *caller) 1202c2968612SBenjamin Herrenschmidt { 12032dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, start, end, -1, GFP_KERNEL, 1204c2968612SBenjamin Herrenschmidt caller); 1205c2968612SBenjamin Herrenschmidt } 1206c2968612SBenjamin Herrenschmidt 12071da177e4SLinus Torvalds /** 1208183ff22bSSimon Arlott * get_vm_area - reserve a contiguous kernel virtual area 12091da177e4SLinus Torvalds * @size: size of the area 12101da177e4SLinus Torvalds * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC 12111da177e4SLinus Torvalds * 12121da177e4SLinus Torvalds * Search an area of @size in the kernel virtual mapping area, 12131da177e4SLinus Torvalds * and reserved it for out purposes. Returns the area descriptor 12141da177e4SLinus Torvalds * on success or %NULL on failure. 12151da177e4SLinus Torvalds */ 12161da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) 12171da177e4SLinus Torvalds { 12182dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 121923016969SChristoph Lameter -1, GFP_KERNEL, __builtin_return_address(0)); 122023016969SChristoph Lameter } 122123016969SChristoph Lameter 122223016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, 122323016969SChristoph Lameter void *caller) 122423016969SChristoph Lameter { 12252dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 122623016969SChristoph Lameter -1, GFP_KERNEL, caller); 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 122952fd24caSGiridhar Pemmasani struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags, 123052fd24caSGiridhar Pemmasani int node, gfp_t gfp_mask) 1231930fc45aSChristoph Lameter { 12322dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 12332dca6999SDavid Miller node, gfp_mask, __builtin_return_address(0)); 1234930fc45aSChristoph Lameter } 1235930fc45aSChristoph Lameter 1236db64fe02SNick Piggin static struct vm_struct *find_vm_area(const void *addr) 123783342314SNick Piggin { 1238db64fe02SNick Piggin struct vmap_area *va; 123983342314SNick Piggin 1240db64fe02SNick Piggin va = find_vmap_area((unsigned long)addr); 1241db64fe02SNick Piggin if (va && va->flags & VM_VM_AREA) 1242db64fe02SNick Piggin return va->private; 124383342314SNick Piggin 12447856dfebSAndi Kleen return NULL; 12457856dfebSAndi Kleen } 12467856dfebSAndi Kleen 12471da177e4SLinus Torvalds /** 1248183ff22bSSimon Arlott * remove_vm_area - find and remove a continuous kernel virtual area 12491da177e4SLinus Torvalds * @addr: base address 12501da177e4SLinus Torvalds * 12511da177e4SLinus Torvalds * Search for the kernel VM area starting at @addr, and remove it. 12521da177e4SLinus Torvalds * This function returns the found VM area, but using it is NOT safe 12537856dfebSAndi Kleen * on SMP machines, except for its size or flags. 12541da177e4SLinus Torvalds */ 1255b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr) 12561da177e4SLinus Torvalds { 1257db64fe02SNick Piggin struct vmap_area *va; 1258db64fe02SNick Piggin 1259db64fe02SNick Piggin va = find_vmap_area((unsigned long)addr); 1260db64fe02SNick Piggin if (va && va->flags & VM_VM_AREA) { 1261db64fe02SNick Piggin struct vm_struct *vm = va->private; 1262db64fe02SNick Piggin struct vm_struct *tmp, **p; 1263dd32c279SKAMEZAWA Hiroyuki /* 1264dd32c279SKAMEZAWA Hiroyuki * remove from list and disallow access to this vm_struct 1265dd32c279SKAMEZAWA Hiroyuki * before unmap. (address range confliction is maintained by 1266dd32c279SKAMEZAWA Hiroyuki * vmap.) 1267dd32c279SKAMEZAWA Hiroyuki */ 12681da177e4SLinus Torvalds write_lock(&vmlist_lock); 1269db64fe02SNick Piggin for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next) 1270db64fe02SNick Piggin ; 1271db64fe02SNick Piggin *p = tmp->next; 12721da177e4SLinus Torvalds write_unlock(&vmlist_lock); 1273db64fe02SNick Piggin 1274dd32c279SKAMEZAWA Hiroyuki vmap_debug_free_range(va->va_start, va->va_end); 1275dd32c279SKAMEZAWA Hiroyuki free_unmap_vmap_area(va); 1276dd32c279SKAMEZAWA Hiroyuki vm->size -= PAGE_SIZE; 1277dd32c279SKAMEZAWA Hiroyuki 1278db64fe02SNick Piggin return vm; 1279db64fe02SNick Piggin } 1280db64fe02SNick Piggin return NULL; 12811da177e4SLinus Torvalds } 12821da177e4SLinus Torvalds 1283b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages) 12841da177e4SLinus Torvalds { 12851da177e4SLinus Torvalds struct vm_struct *area; 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds if (!addr) 12881da177e4SLinus Torvalds return; 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds if ((PAGE_SIZE-1) & (unsigned long)addr) { 12914c8573e2SArjan van de Ven WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr); 12921da177e4SLinus Torvalds return; 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds area = remove_vm_area(addr); 12961da177e4SLinus Torvalds if (unlikely(!area)) { 12974c8573e2SArjan van de Ven WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", 12981da177e4SLinus Torvalds addr); 12991da177e4SLinus Torvalds return; 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds 13029a11b49aSIngo Molnar debug_check_no_locks_freed(addr, area->size); 13033ac7fe5aSThomas Gleixner debug_check_no_obj_freed(addr, area->size); 13049a11b49aSIngo Molnar 13051da177e4SLinus Torvalds if (deallocate_pages) { 13061da177e4SLinus Torvalds int i; 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds for (i = 0; i < area->nr_pages; i++) { 1309bf53d6f8SChristoph Lameter struct page *page = area->pages[i]; 1310bf53d6f8SChristoph Lameter 1311bf53d6f8SChristoph Lameter BUG_ON(!page); 1312bf53d6f8SChristoph Lameter __free_page(page); 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds 13158757d5faSJan Kiszka if (area->flags & VM_VPAGES) 13161da177e4SLinus Torvalds vfree(area->pages); 13171da177e4SLinus Torvalds else 13181da177e4SLinus Torvalds kfree(area->pages); 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds kfree(area); 13221da177e4SLinus Torvalds return; 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds /** 13261da177e4SLinus Torvalds * vfree - release memory allocated by vmalloc() 13271da177e4SLinus Torvalds * @addr: memory base address 13281da177e4SLinus Torvalds * 1329183ff22bSSimon Arlott * Free the virtually continuous memory area starting at @addr, as 133080e93effSPekka Enberg * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is 133180e93effSPekka Enberg * NULL, no operation is performed. 13321da177e4SLinus Torvalds * 133380e93effSPekka Enberg * Must not be called in interrupt context. 13341da177e4SLinus Torvalds */ 1335b3bdda02SChristoph Lameter void vfree(const void *addr) 13361da177e4SLinus Torvalds { 13371da177e4SLinus Torvalds BUG_ON(in_interrupt()); 133889219d37SCatalin Marinas 133989219d37SCatalin Marinas kmemleak_free(addr); 134089219d37SCatalin Marinas 13411da177e4SLinus Torvalds __vunmap(addr, 1); 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds EXPORT_SYMBOL(vfree); 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds /** 13461da177e4SLinus Torvalds * vunmap - release virtual mapping obtained by vmap() 13471da177e4SLinus Torvalds * @addr: memory base address 13481da177e4SLinus Torvalds * 13491da177e4SLinus Torvalds * Free the virtually contiguous memory area starting at @addr, 13501da177e4SLinus Torvalds * which was created from the page array passed to vmap(). 13511da177e4SLinus Torvalds * 135280e93effSPekka Enberg * Must not be called in interrupt context. 13531da177e4SLinus Torvalds */ 1354b3bdda02SChristoph Lameter void vunmap(const void *addr) 13551da177e4SLinus Torvalds { 13561da177e4SLinus Torvalds BUG_ON(in_interrupt()); 135734754b69SPeter Zijlstra might_sleep(); 13581da177e4SLinus Torvalds __vunmap(addr, 0); 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap); 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds /** 13631da177e4SLinus Torvalds * vmap - map an array of pages into virtually contiguous space 13641da177e4SLinus Torvalds * @pages: array of page pointers 13651da177e4SLinus Torvalds * @count: number of pages to map 13661da177e4SLinus Torvalds * @flags: vm_area->flags 13671da177e4SLinus Torvalds * @prot: page protection for the mapping 13681da177e4SLinus Torvalds * 13691da177e4SLinus Torvalds * Maps @count pages from @pages into contiguous kernel virtual 13701da177e4SLinus Torvalds * space. 13711da177e4SLinus Torvalds */ 13721da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, 13731da177e4SLinus Torvalds unsigned long flags, pgprot_t prot) 13741da177e4SLinus Torvalds { 13751da177e4SLinus Torvalds struct vm_struct *area; 13761da177e4SLinus Torvalds 137734754b69SPeter Zijlstra might_sleep(); 137834754b69SPeter Zijlstra 13794481374cSJan Beulich if (count > totalram_pages) 13801da177e4SLinus Torvalds return NULL; 13811da177e4SLinus Torvalds 138223016969SChristoph Lameter area = get_vm_area_caller((count << PAGE_SHIFT), flags, 138323016969SChristoph Lameter __builtin_return_address(0)); 13841da177e4SLinus Torvalds if (!area) 13851da177e4SLinus Torvalds return NULL; 138623016969SChristoph Lameter 13871da177e4SLinus Torvalds if (map_vm_area(area, prot, &pages)) { 13881da177e4SLinus Torvalds vunmap(area->addr); 13891da177e4SLinus Torvalds return NULL; 13901da177e4SLinus Torvalds } 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds return area->addr; 13931da177e4SLinus Torvalds } 13941da177e4SLinus Torvalds EXPORT_SYMBOL(vmap); 13951da177e4SLinus Torvalds 13962dca6999SDavid Miller static void *__vmalloc_node(unsigned long size, unsigned long align, 13972dca6999SDavid Miller gfp_t gfp_mask, pgprot_t prot, 1398db64fe02SNick Piggin int node, void *caller); 1399e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, 140023016969SChristoph Lameter pgprot_t prot, int node, void *caller) 14011da177e4SLinus Torvalds { 14021da177e4SLinus Torvalds struct page **pages; 14031da177e4SLinus Torvalds unsigned int nr_pages, array_size, i; 1404976d6dfbSJan Beulich gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT; 14071da177e4SLinus Torvalds array_size = (nr_pages * sizeof(struct page *)); 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds area->nr_pages = nr_pages; 14101da177e4SLinus Torvalds /* Please note that the recursion is strictly bounded. */ 14118757d5faSJan Kiszka if (array_size > PAGE_SIZE) { 1412976d6dfbSJan Beulich pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM, 141323016969SChristoph Lameter PAGE_KERNEL, node, caller); 14148757d5faSJan Kiszka area->flags |= VM_VPAGES; 1415286e1ea3SAndrew Morton } else { 1416976d6dfbSJan Beulich pages = kmalloc_node(array_size, nested_gfp, node); 1417286e1ea3SAndrew Morton } 14181da177e4SLinus Torvalds area->pages = pages; 141923016969SChristoph Lameter area->caller = caller; 14201da177e4SLinus Torvalds if (!area->pages) { 14211da177e4SLinus Torvalds remove_vm_area(area->addr); 14221da177e4SLinus Torvalds kfree(area); 14231da177e4SLinus Torvalds return NULL; 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds for (i = 0; i < area->nr_pages; i++) { 1427bf53d6f8SChristoph Lameter struct page *page; 1428bf53d6f8SChristoph Lameter 1429930fc45aSChristoph Lameter if (node < 0) 1430bf53d6f8SChristoph Lameter page = alloc_page(gfp_mask); 1431930fc45aSChristoph Lameter else 1432bf53d6f8SChristoph Lameter page = alloc_pages_node(node, gfp_mask, 0); 1433bf53d6f8SChristoph Lameter 1434bf53d6f8SChristoph Lameter if (unlikely(!page)) { 14351da177e4SLinus Torvalds /* Successfully allocated i pages, free them in __vunmap() */ 14361da177e4SLinus Torvalds area->nr_pages = i; 14371da177e4SLinus Torvalds goto fail; 14381da177e4SLinus Torvalds } 1439bf53d6f8SChristoph Lameter area->pages[i] = page; 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds if (map_vm_area(area, prot, &pages)) 14431da177e4SLinus Torvalds goto fail; 14441da177e4SLinus Torvalds return area->addr; 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds fail: 14471da177e4SLinus Torvalds vfree(area->addr); 14481da177e4SLinus Torvalds return NULL; 14491da177e4SLinus Torvalds } 14501da177e4SLinus Torvalds 1451930fc45aSChristoph Lameter void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot) 1452930fc45aSChristoph Lameter { 145389219d37SCatalin Marinas void *addr = __vmalloc_area_node(area, gfp_mask, prot, -1, 145423016969SChristoph Lameter __builtin_return_address(0)); 145589219d37SCatalin Marinas 145689219d37SCatalin Marinas /* 145789219d37SCatalin Marinas * A ref_count = 3 is needed because the vm_struct and vmap_area 145889219d37SCatalin Marinas * structures allocated in the __get_vm_area_node() function contain 145989219d37SCatalin Marinas * references to the virtual address of the vmalloc'ed block. 146089219d37SCatalin Marinas */ 146189219d37SCatalin Marinas kmemleak_alloc(addr, area->size - PAGE_SIZE, 3, gfp_mask); 146289219d37SCatalin Marinas 146389219d37SCatalin Marinas return addr; 1464930fc45aSChristoph Lameter } 1465930fc45aSChristoph Lameter 14661da177e4SLinus Torvalds /** 1467930fc45aSChristoph Lameter * __vmalloc_node - allocate virtually contiguous memory 14681da177e4SLinus Torvalds * @size: allocation size 14692dca6999SDavid Miller * @align: desired alignment 14701da177e4SLinus Torvalds * @gfp_mask: flags for the page level allocator 14711da177e4SLinus Torvalds * @prot: protection mask for the allocated pages 1472d44e0780SRandy Dunlap * @node: node to use for allocation or -1 1473c85d194bSRandy Dunlap * @caller: caller's return address 14741da177e4SLinus Torvalds * 14751da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 14761da177e4SLinus Torvalds * allocator with @gfp_mask flags. Map them into contiguous 14771da177e4SLinus Torvalds * kernel virtual space, using a pagetable protection of @prot. 14781da177e4SLinus Torvalds */ 14792dca6999SDavid Miller static void *__vmalloc_node(unsigned long size, unsigned long align, 14802dca6999SDavid Miller gfp_t gfp_mask, pgprot_t prot, 148123016969SChristoph Lameter int node, void *caller) 14821da177e4SLinus Torvalds { 14831da177e4SLinus Torvalds struct vm_struct *area; 148489219d37SCatalin Marinas void *addr; 148589219d37SCatalin Marinas unsigned long real_size = size; 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds size = PAGE_ALIGN(size); 14884481374cSJan Beulich if (!size || (size >> PAGE_SHIFT) > totalram_pages) 14891da177e4SLinus Torvalds return NULL; 14901da177e4SLinus Torvalds 14912dca6999SDavid Miller area = __get_vm_area_node(size, align, VM_ALLOC, VMALLOC_START, 14922dca6999SDavid Miller VMALLOC_END, node, gfp_mask, caller); 149323016969SChristoph Lameter 14941da177e4SLinus Torvalds if (!area) 14951da177e4SLinus Torvalds return NULL; 14961da177e4SLinus Torvalds 149789219d37SCatalin Marinas addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller); 149889219d37SCatalin Marinas 149989219d37SCatalin Marinas /* 150089219d37SCatalin Marinas * A ref_count = 3 is needed because the vm_struct and vmap_area 150189219d37SCatalin Marinas * structures allocated in the __get_vm_area_node() function contain 150289219d37SCatalin Marinas * references to the virtual address of the vmalloc'ed block. 150389219d37SCatalin Marinas */ 150489219d37SCatalin Marinas kmemleak_alloc(addr, real_size, 3, gfp_mask); 150589219d37SCatalin Marinas 150689219d37SCatalin Marinas return addr; 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds 1509930fc45aSChristoph Lameter void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) 1510930fc45aSChristoph Lameter { 15112dca6999SDavid Miller return __vmalloc_node(size, 1, gfp_mask, prot, -1, 151223016969SChristoph Lameter __builtin_return_address(0)); 1513930fc45aSChristoph Lameter } 15141da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc); 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds /** 15171da177e4SLinus Torvalds * vmalloc - allocate virtually contiguous memory 15181da177e4SLinus Torvalds * @size: allocation size 15191da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 15201da177e4SLinus Torvalds * allocator and map them into contiguous kernel virtual space. 15211da177e4SLinus Torvalds * 1522c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 15231da177e4SLinus Torvalds * use __vmalloc() instead. 15241da177e4SLinus Torvalds */ 15251da177e4SLinus Torvalds void *vmalloc(unsigned long size) 15261da177e4SLinus Torvalds { 15272dca6999SDavid Miller return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL, 152823016969SChristoph Lameter -1, __builtin_return_address(0)); 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc); 15311da177e4SLinus Torvalds 1532930fc45aSChristoph Lameter /** 1533ead04089SRolf Eike Beer * vmalloc_user - allocate zeroed virtually contiguous memory for userspace 153483342314SNick Piggin * @size: allocation size 1535ead04089SRolf Eike Beer * 1536ead04089SRolf Eike Beer * The resulting memory area is zeroed so it can be mapped to userspace 1537ead04089SRolf Eike Beer * without leaking data. 153883342314SNick Piggin */ 153983342314SNick Piggin void *vmalloc_user(unsigned long size) 154083342314SNick Piggin { 154183342314SNick Piggin struct vm_struct *area; 154283342314SNick Piggin void *ret; 154383342314SNick Piggin 15442dca6999SDavid Miller ret = __vmalloc_node(size, SHMLBA, 15452dca6999SDavid Miller GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, 154684877848SGlauber Costa PAGE_KERNEL, -1, __builtin_return_address(0)); 15472b4ac44eSEric Dumazet if (ret) { 1548db64fe02SNick Piggin area = find_vm_area(ret); 154983342314SNick Piggin area->flags |= VM_USERMAP; 15502b4ac44eSEric Dumazet } 155183342314SNick Piggin return ret; 155283342314SNick Piggin } 155383342314SNick Piggin EXPORT_SYMBOL(vmalloc_user); 155483342314SNick Piggin 155583342314SNick Piggin /** 1556930fc45aSChristoph Lameter * vmalloc_node - allocate memory on a specific node 1557930fc45aSChristoph Lameter * @size: allocation size 1558d44e0780SRandy Dunlap * @node: numa node 1559930fc45aSChristoph Lameter * 1560930fc45aSChristoph Lameter * Allocate enough pages to cover @size from the page level 1561930fc45aSChristoph Lameter * allocator and map them into contiguous kernel virtual space. 1562930fc45aSChristoph Lameter * 1563c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 1564930fc45aSChristoph Lameter * use __vmalloc() instead. 1565930fc45aSChristoph Lameter */ 1566930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node) 1567930fc45aSChristoph Lameter { 15682dca6999SDavid Miller return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL, 156923016969SChristoph Lameter node, __builtin_return_address(0)); 1570930fc45aSChristoph Lameter } 1571930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node); 1572930fc45aSChristoph Lameter 15734dc3b16bSPavel Pisa #ifndef PAGE_KERNEL_EXEC 15744dc3b16bSPavel Pisa # define PAGE_KERNEL_EXEC PAGE_KERNEL 15754dc3b16bSPavel Pisa #endif 15764dc3b16bSPavel Pisa 15771da177e4SLinus Torvalds /** 15781da177e4SLinus Torvalds * vmalloc_exec - allocate virtually contiguous, executable memory 15791da177e4SLinus Torvalds * @size: allocation size 15801da177e4SLinus Torvalds * 15811da177e4SLinus Torvalds * Kernel-internal function to allocate enough pages to cover @size 15821da177e4SLinus Torvalds * the page level allocator and map them into contiguous and 15831da177e4SLinus Torvalds * executable kernel virtual space. 15841da177e4SLinus Torvalds * 1585c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 15861da177e4SLinus Torvalds * use __vmalloc() instead. 15871da177e4SLinus Torvalds */ 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds void *vmalloc_exec(unsigned long size) 15901da177e4SLinus Torvalds { 15912dca6999SDavid Miller return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC, 159284877848SGlauber Costa -1, __builtin_return_address(0)); 15931da177e4SLinus Torvalds } 15941da177e4SLinus Torvalds 15950d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) 15967ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL 15970d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) 15987ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL 15990d08e0d3SAndi Kleen #else 16000d08e0d3SAndi Kleen #define GFP_VMALLOC32 GFP_KERNEL 16010d08e0d3SAndi Kleen #endif 16020d08e0d3SAndi Kleen 16031da177e4SLinus Torvalds /** 16041da177e4SLinus Torvalds * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 16051da177e4SLinus Torvalds * @size: allocation size 16061da177e4SLinus Torvalds * 16071da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 16081da177e4SLinus Torvalds * page level allocator and map them into contiguous kernel virtual space. 16091da177e4SLinus Torvalds */ 16101da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 16111da177e4SLinus Torvalds { 16122dca6999SDavid Miller return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL, 161384877848SGlauber Costa -1, __builtin_return_address(0)); 16141da177e4SLinus Torvalds } 16151da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32); 16161da177e4SLinus Torvalds 161783342314SNick Piggin /** 1618ead04089SRolf Eike Beer * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 161983342314SNick Piggin * @size: allocation size 1620ead04089SRolf Eike Beer * 1621ead04089SRolf Eike Beer * The resulting memory area is 32bit addressable and zeroed so it can be 1622ead04089SRolf Eike Beer * mapped to userspace without leaking data. 162383342314SNick Piggin */ 162483342314SNick Piggin void *vmalloc_32_user(unsigned long size) 162583342314SNick Piggin { 162683342314SNick Piggin struct vm_struct *area; 162783342314SNick Piggin void *ret; 162883342314SNick Piggin 16292dca6999SDavid Miller ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, 163084877848SGlauber Costa -1, __builtin_return_address(0)); 16312b4ac44eSEric Dumazet if (ret) { 1632db64fe02SNick Piggin area = find_vm_area(ret); 163383342314SNick Piggin area->flags |= VM_USERMAP; 16342b4ac44eSEric Dumazet } 163583342314SNick Piggin return ret; 163683342314SNick Piggin } 163783342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user); 163883342314SNick Piggin 1639d0107eb0SKAMEZAWA Hiroyuki /* 1640d0107eb0SKAMEZAWA Hiroyuki * small helper routine , copy contents to buf from addr. 1641d0107eb0SKAMEZAWA Hiroyuki * If the page is not present, fill zero. 1642d0107eb0SKAMEZAWA Hiroyuki */ 1643d0107eb0SKAMEZAWA Hiroyuki 1644d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count) 1645d0107eb0SKAMEZAWA Hiroyuki { 1646d0107eb0SKAMEZAWA Hiroyuki struct page *p; 1647d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 1648d0107eb0SKAMEZAWA Hiroyuki 1649d0107eb0SKAMEZAWA Hiroyuki while (count) { 1650d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 1651d0107eb0SKAMEZAWA Hiroyuki 1652d0107eb0SKAMEZAWA Hiroyuki offset = (unsigned long)addr & ~PAGE_MASK; 1653d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 1654d0107eb0SKAMEZAWA Hiroyuki if (length > count) 1655d0107eb0SKAMEZAWA Hiroyuki length = count; 1656d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 1657d0107eb0SKAMEZAWA Hiroyuki /* 1658d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 1659d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 1660d0107eb0SKAMEZAWA Hiroyuki * overhead of vmalloc()/vfree() calles for this _debug_ 1661d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 1662d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 1663d0107eb0SKAMEZAWA Hiroyuki */ 1664d0107eb0SKAMEZAWA Hiroyuki if (p) { 1665d0107eb0SKAMEZAWA Hiroyuki /* 1666d0107eb0SKAMEZAWA Hiroyuki * we can expect USER0 is not used (see vread/vwrite's 1667d0107eb0SKAMEZAWA Hiroyuki * function description) 1668d0107eb0SKAMEZAWA Hiroyuki */ 1669d0107eb0SKAMEZAWA Hiroyuki void *map = kmap_atomic(p, KM_USER0); 1670d0107eb0SKAMEZAWA Hiroyuki memcpy(buf, map + offset, length); 1671d0107eb0SKAMEZAWA Hiroyuki kunmap_atomic(map, KM_USER0); 1672d0107eb0SKAMEZAWA Hiroyuki } else 1673d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, length); 1674d0107eb0SKAMEZAWA Hiroyuki 1675d0107eb0SKAMEZAWA Hiroyuki addr += length; 1676d0107eb0SKAMEZAWA Hiroyuki buf += length; 1677d0107eb0SKAMEZAWA Hiroyuki copied += length; 1678d0107eb0SKAMEZAWA Hiroyuki count -= length; 1679d0107eb0SKAMEZAWA Hiroyuki } 1680d0107eb0SKAMEZAWA Hiroyuki return copied; 1681d0107eb0SKAMEZAWA Hiroyuki } 1682d0107eb0SKAMEZAWA Hiroyuki 1683d0107eb0SKAMEZAWA Hiroyuki static int aligned_vwrite(char *buf, char *addr, unsigned long count) 1684d0107eb0SKAMEZAWA Hiroyuki { 1685d0107eb0SKAMEZAWA Hiroyuki struct page *p; 1686d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 1687d0107eb0SKAMEZAWA Hiroyuki 1688d0107eb0SKAMEZAWA Hiroyuki while (count) { 1689d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 1690d0107eb0SKAMEZAWA Hiroyuki 1691d0107eb0SKAMEZAWA Hiroyuki offset = (unsigned long)addr & ~PAGE_MASK; 1692d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 1693d0107eb0SKAMEZAWA Hiroyuki if (length > count) 1694d0107eb0SKAMEZAWA Hiroyuki length = count; 1695d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 1696d0107eb0SKAMEZAWA Hiroyuki /* 1697d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 1698d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 1699d0107eb0SKAMEZAWA Hiroyuki * overhead of vmalloc()/vfree() calles for this _debug_ 1700d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 1701d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 1702d0107eb0SKAMEZAWA Hiroyuki */ 1703d0107eb0SKAMEZAWA Hiroyuki if (p) { 1704d0107eb0SKAMEZAWA Hiroyuki /* 1705d0107eb0SKAMEZAWA Hiroyuki * we can expect USER0 is not used (see vread/vwrite's 1706d0107eb0SKAMEZAWA Hiroyuki * function description) 1707d0107eb0SKAMEZAWA Hiroyuki */ 1708d0107eb0SKAMEZAWA Hiroyuki void *map = kmap_atomic(p, KM_USER0); 1709d0107eb0SKAMEZAWA Hiroyuki memcpy(map + offset, buf, length); 1710d0107eb0SKAMEZAWA Hiroyuki kunmap_atomic(map, KM_USER0); 1711d0107eb0SKAMEZAWA Hiroyuki } 1712d0107eb0SKAMEZAWA Hiroyuki addr += length; 1713d0107eb0SKAMEZAWA Hiroyuki buf += length; 1714d0107eb0SKAMEZAWA Hiroyuki copied += length; 1715d0107eb0SKAMEZAWA Hiroyuki count -= length; 1716d0107eb0SKAMEZAWA Hiroyuki } 1717d0107eb0SKAMEZAWA Hiroyuki return copied; 1718d0107eb0SKAMEZAWA Hiroyuki } 1719d0107eb0SKAMEZAWA Hiroyuki 1720d0107eb0SKAMEZAWA Hiroyuki /** 1721d0107eb0SKAMEZAWA Hiroyuki * vread() - read vmalloc area in a safe way. 1722d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for reading data 1723d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 1724d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 1725d0107eb0SKAMEZAWA Hiroyuki * 1726d0107eb0SKAMEZAWA Hiroyuki * Returns # of bytes which addr and buf should be increased. 1727d0107eb0SKAMEZAWA Hiroyuki * (same number to @count). Returns 0 if [addr...addr+count) doesn't 1728d0107eb0SKAMEZAWA Hiroyuki * includes any intersect with alive vmalloc area. 1729d0107eb0SKAMEZAWA Hiroyuki * 1730d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 1731d0107eb0SKAMEZAWA Hiroyuki * copy data from that area to a given buffer. If the given memory range 1732d0107eb0SKAMEZAWA Hiroyuki * of [addr...addr+count) includes some valid address, data is copied to 1733d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, they'll be zero-filled. 1734d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 1735d0107eb0SKAMEZAWA Hiroyuki * 1736d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 1737d0107eb0SKAMEZAWA Hiroyuki * vm_struct area, returns 0. 1738d0107eb0SKAMEZAWA Hiroyuki * @buf should be kernel's buffer. Because this function uses KM_USER0, 1739d0107eb0SKAMEZAWA Hiroyuki * the caller should guarantee KM_USER0 is not used. 1740d0107eb0SKAMEZAWA Hiroyuki * 1741d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vread() is never necessary because the caller 1742d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 1743d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 1744d0107eb0SKAMEZAWA Hiroyuki * any informaion, as /dev/kmem. 1745d0107eb0SKAMEZAWA Hiroyuki * 1746d0107eb0SKAMEZAWA Hiroyuki */ 1747d0107eb0SKAMEZAWA Hiroyuki 17481da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 17491da177e4SLinus Torvalds { 17501da177e4SLinus Torvalds struct vm_struct *tmp; 17511da177e4SLinus Torvalds char *vaddr, *buf_start = buf; 1752d0107eb0SKAMEZAWA Hiroyuki unsigned long buflen = count; 17531da177e4SLinus Torvalds unsigned long n; 17541da177e4SLinus Torvalds 17551da177e4SLinus Torvalds /* Don't allow overflow */ 17561da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 17571da177e4SLinus Torvalds count = -(unsigned long) addr; 17581da177e4SLinus Torvalds 17591da177e4SLinus Torvalds read_lock(&vmlist_lock); 1760d0107eb0SKAMEZAWA Hiroyuki for (tmp = vmlist; count && tmp; tmp = tmp->next) { 17611da177e4SLinus Torvalds vaddr = (char *) tmp->addr; 17621da177e4SLinus Torvalds if (addr >= vaddr + tmp->size - PAGE_SIZE) 17631da177e4SLinus Torvalds continue; 17641da177e4SLinus Torvalds while (addr < vaddr) { 17651da177e4SLinus Torvalds if (count == 0) 17661da177e4SLinus Torvalds goto finished; 17671da177e4SLinus Torvalds *buf = '\0'; 17681da177e4SLinus Torvalds buf++; 17691da177e4SLinus Torvalds addr++; 17701da177e4SLinus Torvalds count--; 17711da177e4SLinus Torvalds } 17721da177e4SLinus Torvalds n = vaddr + tmp->size - PAGE_SIZE - addr; 1773d0107eb0SKAMEZAWA Hiroyuki if (n > count) 1774d0107eb0SKAMEZAWA Hiroyuki n = count; 1775d0107eb0SKAMEZAWA Hiroyuki if (!(tmp->flags & VM_IOREMAP)) 1776d0107eb0SKAMEZAWA Hiroyuki aligned_vread(buf, addr, n); 1777d0107eb0SKAMEZAWA Hiroyuki else /* IOREMAP area is treated as memory hole */ 1778d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, n); 1779d0107eb0SKAMEZAWA Hiroyuki buf += n; 1780d0107eb0SKAMEZAWA Hiroyuki addr += n; 1781d0107eb0SKAMEZAWA Hiroyuki count -= n; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds finished: 17841da177e4SLinus Torvalds read_unlock(&vmlist_lock); 1785d0107eb0SKAMEZAWA Hiroyuki 1786d0107eb0SKAMEZAWA Hiroyuki if (buf == buf_start) 1787d0107eb0SKAMEZAWA Hiroyuki return 0; 1788d0107eb0SKAMEZAWA Hiroyuki /* zero-fill memory holes */ 1789d0107eb0SKAMEZAWA Hiroyuki if (buf != buf_start + buflen) 1790d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, buflen - (buf - buf_start)); 1791d0107eb0SKAMEZAWA Hiroyuki 1792d0107eb0SKAMEZAWA Hiroyuki return buflen; 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds 1795d0107eb0SKAMEZAWA Hiroyuki /** 1796d0107eb0SKAMEZAWA Hiroyuki * vwrite() - write vmalloc area in a safe way. 1797d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for source data 1798d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 1799d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 1800d0107eb0SKAMEZAWA Hiroyuki * 1801d0107eb0SKAMEZAWA Hiroyuki * Returns # of bytes which addr and buf should be incresed. 1802d0107eb0SKAMEZAWA Hiroyuki * (same number to @count). 1803d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersect with valid 1804d0107eb0SKAMEZAWA Hiroyuki * vmalloc area, returns 0. 1805d0107eb0SKAMEZAWA Hiroyuki * 1806d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 1807d0107eb0SKAMEZAWA Hiroyuki * copy data from a buffer to the given addr. If specified range of 1808d0107eb0SKAMEZAWA Hiroyuki * [addr...addr+count) includes some valid address, data is copied from 1809d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, no copy to hole. 1810d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 1811d0107eb0SKAMEZAWA Hiroyuki * 1812d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 1813d0107eb0SKAMEZAWA Hiroyuki * vm_struct area, returns 0. 1814d0107eb0SKAMEZAWA Hiroyuki * @buf should be kernel's buffer. Because this function uses KM_USER0, 1815d0107eb0SKAMEZAWA Hiroyuki * the caller should guarantee KM_USER0 is not used. 1816d0107eb0SKAMEZAWA Hiroyuki * 1817d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vwrite() is never necessary because the caller 1818d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 1819d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 1820d0107eb0SKAMEZAWA Hiroyuki * any informaion, as /dev/kmem. 1821d0107eb0SKAMEZAWA Hiroyuki * 1822d0107eb0SKAMEZAWA Hiroyuki * The caller should guarantee KM_USER1 is not used. 1823d0107eb0SKAMEZAWA Hiroyuki */ 1824d0107eb0SKAMEZAWA Hiroyuki 18251da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count) 18261da177e4SLinus Torvalds { 18271da177e4SLinus Torvalds struct vm_struct *tmp; 1828d0107eb0SKAMEZAWA Hiroyuki char *vaddr; 1829d0107eb0SKAMEZAWA Hiroyuki unsigned long n, buflen; 1830d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds /* Don't allow overflow */ 18331da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 18341da177e4SLinus Torvalds count = -(unsigned long) addr; 1835d0107eb0SKAMEZAWA Hiroyuki buflen = count; 18361da177e4SLinus Torvalds 18371da177e4SLinus Torvalds read_lock(&vmlist_lock); 1838d0107eb0SKAMEZAWA Hiroyuki for (tmp = vmlist; count && tmp; tmp = tmp->next) { 18391da177e4SLinus Torvalds vaddr = (char *) tmp->addr; 18401da177e4SLinus Torvalds if (addr >= vaddr + tmp->size - PAGE_SIZE) 18411da177e4SLinus Torvalds continue; 18421da177e4SLinus Torvalds while (addr < vaddr) { 18431da177e4SLinus Torvalds if (count == 0) 18441da177e4SLinus Torvalds goto finished; 18451da177e4SLinus Torvalds buf++; 18461da177e4SLinus Torvalds addr++; 18471da177e4SLinus Torvalds count--; 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds n = vaddr + tmp->size - PAGE_SIZE - addr; 1850d0107eb0SKAMEZAWA Hiroyuki if (n > count) 1851d0107eb0SKAMEZAWA Hiroyuki n = count; 1852d0107eb0SKAMEZAWA Hiroyuki if (!(tmp->flags & VM_IOREMAP)) { 1853d0107eb0SKAMEZAWA Hiroyuki aligned_vwrite(buf, addr, n); 1854d0107eb0SKAMEZAWA Hiroyuki copied++; 1855d0107eb0SKAMEZAWA Hiroyuki } 1856d0107eb0SKAMEZAWA Hiroyuki buf += n; 1857d0107eb0SKAMEZAWA Hiroyuki addr += n; 1858d0107eb0SKAMEZAWA Hiroyuki count -= n; 18591da177e4SLinus Torvalds } 18601da177e4SLinus Torvalds finished: 18611da177e4SLinus Torvalds read_unlock(&vmlist_lock); 1862d0107eb0SKAMEZAWA Hiroyuki if (!copied) 1863d0107eb0SKAMEZAWA Hiroyuki return 0; 1864d0107eb0SKAMEZAWA Hiroyuki return buflen; 18651da177e4SLinus Torvalds } 186683342314SNick Piggin 186783342314SNick Piggin /** 186883342314SNick Piggin * remap_vmalloc_range - map vmalloc pages to userspace 186983342314SNick Piggin * @vma: vma to cover (map full range of vma) 187083342314SNick Piggin * @addr: vmalloc memory 187183342314SNick Piggin * @pgoff: number of pages into addr before first page to map 18727682486bSRandy Dunlap * 18737682486bSRandy Dunlap * Returns: 0 for success, -Exxx on failure 187483342314SNick Piggin * 187583342314SNick Piggin * This function checks that addr is a valid vmalloc'ed area, and 187683342314SNick Piggin * that it is big enough to cover the vma. Will return failure if 187783342314SNick Piggin * that criteria isn't met. 187883342314SNick Piggin * 187972fd4a35SRobert P. J. Day * Similar to remap_pfn_range() (see mm/memory.c) 188083342314SNick Piggin */ 188183342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 188283342314SNick Piggin unsigned long pgoff) 188383342314SNick Piggin { 188483342314SNick Piggin struct vm_struct *area; 188583342314SNick Piggin unsigned long uaddr = vma->vm_start; 188683342314SNick Piggin unsigned long usize = vma->vm_end - vma->vm_start; 188783342314SNick Piggin 188883342314SNick Piggin if ((PAGE_SIZE-1) & (unsigned long)addr) 188983342314SNick Piggin return -EINVAL; 189083342314SNick Piggin 1891db64fe02SNick Piggin area = find_vm_area(addr); 189283342314SNick Piggin if (!area) 1893db64fe02SNick Piggin return -EINVAL; 189483342314SNick Piggin 189583342314SNick Piggin if (!(area->flags & VM_USERMAP)) 1896db64fe02SNick Piggin return -EINVAL; 189783342314SNick Piggin 189883342314SNick Piggin if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE) 1899db64fe02SNick Piggin return -EINVAL; 190083342314SNick Piggin 190183342314SNick Piggin addr += pgoff << PAGE_SHIFT; 190283342314SNick Piggin do { 190383342314SNick Piggin struct page *page = vmalloc_to_page(addr); 1904db64fe02SNick Piggin int ret; 1905db64fe02SNick Piggin 190683342314SNick Piggin ret = vm_insert_page(vma, uaddr, page); 190783342314SNick Piggin if (ret) 190883342314SNick Piggin return ret; 190983342314SNick Piggin 191083342314SNick Piggin uaddr += PAGE_SIZE; 191183342314SNick Piggin addr += PAGE_SIZE; 191283342314SNick Piggin usize -= PAGE_SIZE; 191383342314SNick Piggin } while (usize > 0); 191483342314SNick Piggin 191583342314SNick Piggin /* Prevent "things" like memory migration? VM_flags need a cleanup... */ 191683342314SNick Piggin vma->vm_flags |= VM_RESERVED; 191783342314SNick Piggin 1918db64fe02SNick Piggin return 0; 191983342314SNick Piggin } 192083342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range); 192183342314SNick Piggin 19221eeb66a1SChristoph Hellwig /* 19231eeb66a1SChristoph Hellwig * Implement a stub for vmalloc_sync_all() if the architecture chose not to 19241eeb66a1SChristoph Hellwig * have one. 19251eeb66a1SChristoph Hellwig */ 19261eeb66a1SChristoph Hellwig void __attribute__((weak)) vmalloc_sync_all(void) 19271eeb66a1SChristoph Hellwig { 19281eeb66a1SChristoph Hellwig } 19295f4352fbSJeremy Fitzhardinge 19305f4352fbSJeremy Fitzhardinge 19312f569afdSMartin Schwidefsky static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data) 19325f4352fbSJeremy Fitzhardinge { 19335f4352fbSJeremy Fitzhardinge /* apply_to_page_range() does all the hard work. */ 19345f4352fbSJeremy Fitzhardinge return 0; 19355f4352fbSJeremy Fitzhardinge } 19365f4352fbSJeremy Fitzhardinge 19375f4352fbSJeremy Fitzhardinge /** 19385f4352fbSJeremy Fitzhardinge * alloc_vm_area - allocate a range of kernel address space 19395f4352fbSJeremy Fitzhardinge * @size: size of the area 19407682486bSRandy Dunlap * 19417682486bSRandy Dunlap * Returns: NULL on failure, vm_struct on success 19425f4352fbSJeremy Fitzhardinge * 19435f4352fbSJeremy Fitzhardinge * This function reserves a range of kernel address space, and 19445f4352fbSJeremy Fitzhardinge * allocates pagetables to map that range. No actual mappings 19455f4352fbSJeremy Fitzhardinge * are created. If the kernel address space is not shared 19465f4352fbSJeremy Fitzhardinge * between processes, it syncs the pagetable across all 19475f4352fbSJeremy Fitzhardinge * processes. 19485f4352fbSJeremy Fitzhardinge */ 19495f4352fbSJeremy Fitzhardinge struct vm_struct *alloc_vm_area(size_t size) 19505f4352fbSJeremy Fitzhardinge { 19515f4352fbSJeremy Fitzhardinge struct vm_struct *area; 19525f4352fbSJeremy Fitzhardinge 195323016969SChristoph Lameter area = get_vm_area_caller(size, VM_IOREMAP, 195423016969SChristoph Lameter __builtin_return_address(0)); 19555f4352fbSJeremy Fitzhardinge if (area == NULL) 19565f4352fbSJeremy Fitzhardinge return NULL; 19575f4352fbSJeremy Fitzhardinge 19585f4352fbSJeremy Fitzhardinge /* 19595f4352fbSJeremy Fitzhardinge * This ensures that page tables are constructed for this region 19605f4352fbSJeremy Fitzhardinge * of kernel virtual address space and mapped into init_mm. 19615f4352fbSJeremy Fitzhardinge */ 19625f4352fbSJeremy Fitzhardinge if (apply_to_page_range(&init_mm, (unsigned long)area->addr, 19635f4352fbSJeremy Fitzhardinge area->size, f, NULL)) { 19645f4352fbSJeremy Fitzhardinge free_vm_area(area); 19655f4352fbSJeremy Fitzhardinge return NULL; 19665f4352fbSJeremy Fitzhardinge } 19675f4352fbSJeremy Fitzhardinge 19685f4352fbSJeremy Fitzhardinge /* Make sure the pagetables are constructed in process kernel 19695f4352fbSJeremy Fitzhardinge mappings */ 19705f4352fbSJeremy Fitzhardinge vmalloc_sync_all(); 19715f4352fbSJeremy Fitzhardinge 19725f4352fbSJeremy Fitzhardinge return area; 19735f4352fbSJeremy Fitzhardinge } 19745f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(alloc_vm_area); 19755f4352fbSJeremy Fitzhardinge 19765f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area) 19775f4352fbSJeremy Fitzhardinge { 19785f4352fbSJeremy Fitzhardinge struct vm_struct *ret; 19795f4352fbSJeremy Fitzhardinge ret = remove_vm_area(area->addr); 19805f4352fbSJeremy Fitzhardinge BUG_ON(ret != area); 19815f4352fbSJeremy Fitzhardinge kfree(area); 19825f4352fbSJeremy Fitzhardinge } 19835f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area); 1984a10aa579SChristoph Lameter 1985ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n) 1986ca23e405STejun Heo { 1987ca23e405STejun Heo return n ? rb_entry(n, struct vmap_area, rb_node) : NULL; 1988ca23e405STejun Heo } 1989ca23e405STejun Heo 1990ca23e405STejun Heo /** 1991ca23e405STejun Heo * pvm_find_next_prev - find the next and prev vmap_area surrounding @end 1992ca23e405STejun Heo * @end: target address 1993ca23e405STejun Heo * @pnext: out arg for the next vmap_area 1994ca23e405STejun Heo * @pprev: out arg for the previous vmap_area 1995ca23e405STejun Heo * 1996ca23e405STejun Heo * Returns: %true if either or both of next and prev are found, 1997ca23e405STejun Heo * %false if no vmap_area exists 1998ca23e405STejun Heo * 1999ca23e405STejun Heo * Find vmap_areas end addresses of which enclose @end. ie. if not 2000ca23e405STejun Heo * NULL, *pnext->va_end > @end and *pprev->va_end <= @end. 2001ca23e405STejun Heo */ 2002ca23e405STejun Heo static bool pvm_find_next_prev(unsigned long end, 2003ca23e405STejun Heo struct vmap_area **pnext, 2004ca23e405STejun Heo struct vmap_area **pprev) 2005ca23e405STejun Heo { 2006ca23e405STejun Heo struct rb_node *n = vmap_area_root.rb_node; 2007ca23e405STejun Heo struct vmap_area *va = NULL; 2008ca23e405STejun Heo 2009ca23e405STejun Heo while (n) { 2010ca23e405STejun Heo va = rb_entry(n, struct vmap_area, rb_node); 2011ca23e405STejun Heo if (end < va->va_end) 2012ca23e405STejun Heo n = n->rb_left; 2013ca23e405STejun Heo else if (end > va->va_end) 2014ca23e405STejun Heo n = n->rb_right; 2015ca23e405STejun Heo else 2016ca23e405STejun Heo break; 2017ca23e405STejun Heo } 2018ca23e405STejun Heo 2019ca23e405STejun Heo if (!va) 2020ca23e405STejun Heo return false; 2021ca23e405STejun Heo 2022ca23e405STejun Heo if (va->va_end > end) { 2023ca23e405STejun Heo *pnext = va; 2024ca23e405STejun Heo *pprev = node_to_va(rb_prev(&(*pnext)->rb_node)); 2025ca23e405STejun Heo } else { 2026ca23e405STejun Heo *pprev = va; 2027ca23e405STejun Heo *pnext = node_to_va(rb_next(&(*pprev)->rb_node)); 2028ca23e405STejun Heo } 2029ca23e405STejun Heo return true; 2030ca23e405STejun Heo } 2031ca23e405STejun Heo 2032ca23e405STejun Heo /** 2033ca23e405STejun Heo * pvm_determine_end - find the highest aligned address between two vmap_areas 2034ca23e405STejun Heo * @pnext: in/out arg for the next vmap_area 2035ca23e405STejun Heo * @pprev: in/out arg for the previous vmap_area 2036ca23e405STejun Heo * @align: alignment 2037ca23e405STejun Heo * 2038ca23e405STejun Heo * Returns: determined end address 2039ca23e405STejun Heo * 2040ca23e405STejun Heo * Find the highest aligned address between *@pnext and *@pprev below 2041ca23e405STejun Heo * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned 2042ca23e405STejun Heo * down address is between the end addresses of the two vmap_areas. 2043ca23e405STejun Heo * 2044ca23e405STejun Heo * Please note that the address returned by this function may fall 2045ca23e405STejun Heo * inside *@pnext vmap_area. The caller is responsible for checking 2046ca23e405STejun Heo * that. 2047ca23e405STejun Heo */ 2048ca23e405STejun Heo static unsigned long pvm_determine_end(struct vmap_area **pnext, 2049ca23e405STejun Heo struct vmap_area **pprev, 2050ca23e405STejun Heo unsigned long align) 2051ca23e405STejun Heo { 2052ca23e405STejun Heo const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 2053ca23e405STejun Heo unsigned long addr; 2054ca23e405STejun Heo 2055ca23e405STejun Heo if (*pnext) 2056ca23e405STejun Heo addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end); 2057ca23e405STejun Heo else 2058ca23e405STejun Heo addr = vmalloc_end; 2059ca23e405STejun Heo 2060ca23e405STejun Heo while (*pprev && (*pprev)->va_end > addr) { 2061ca23e405STejun Heo *pnext = *pprev; 2062ca23e405STejun Heo *pprev = node_to_va(rb_prev(&(*pnext)->rb_node)); 2063ca23e405STejun Heo } 2064ca23e405STejun Heo 2065ca23e405STejun Heo return addr; 2066ca23e405STejun Heo } 2067ca23e405STejun Heo 2068ca23e405STejun Heo /** 2069ca23e405STejun Heo * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator 2070ca23e405STejun Heo * @offsets: array containing offset of each area 2071ca23e405STejun Heo * @sizes: array containing size of each area 2072ca23e405STejun Heo * @nr_vms: the number of areas to allocate 2073ca23e405STejun Heo * @align: alignment, all entries in @offsets and @sizes must be aligned to this 2074ca23e405STejun Heo * @gfp_mask: allocation mask 2075ca23e405STejun Heo * 2076ca23e405STejun Heo * Returns: kmalloc'd vm_struct pointer array pointing to allocated 2077ca23e405STejun Heo * vm_structs on success, %NULL on failure 2078ca23e405STejun Heo * 2079ca23e405STejun Heo * Percpu allocator wants to use congruent vm areas so that it can 2080ca23e405STejun Heo * maintain the offsets among percpu areas. This function allocates 2081ca23e405STejun Heo * congruent vmalloc areas for it. These areas tend to be scattered 2082ca23e405STejun Heo * pretty far, distance between two areas easily going up to 2083ca23e405STejun Heo * gigabytes. To avoid interacting with regular vmallocs, these areas 2084ca23e405STejun Heo * are allocated from top. 2085ca23e405STejun Heo * 2086ca23e405STejun Heo * Despite its complicated look, this allocator is rather simple. It 2087ca23e405STejun Heo * does everything top-down and scans areas from the end looking for 2088ca23e405STejun Heo * matching slot. While scanning, if any of the areas overlaps with 2089ca23e405STejun Heo * existing vmap_area, the base address is pulled down to fit the 2090ca23e405STejun Heo * area. Scanning is repeated till all the areas fit and then all 2091ca23e405STejun Heo * necessary data structres are inserted and the result is returned. 2092ca23e405STejun Heo */ 2093ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, 2094ca23e405STejun Heo const size_t *sizes, int nr_vms, 2095ca23e405STejun Heo size_t align, gfp_t gfp_mask) 2096ca23e405STejun Heo { 2097ca23e405STejun Heo const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); 2098ca23e405STejun Heo const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 2099ca23e405STejun Heo struct vmap_area **vas, *prev, *next; 2100ca23e405STejun Heo struct vm_struct **vms; 2101ca23e405STejun Heo int area, area2, last_area, term_area; 2102ca23e405STejun Heo unsigned long base, start, end, last_end; 2103ca23e405STejun Heo bool purged = false; 2104ca23e405STejun Heo 2105ca23e405STejun Heo gfp_mask &= GFP_RECLAIM_MASK; 2106ca23e405STejun Heo 2107ca23e405STejun Heo /* verify parameters and allocate data structures */ 2108ca23e405STejun Heo BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align)); 2109ca23e405STejun Heo for (last_area = 0, area = 0; area < nr_vms; area++) { 2110ca23e405STejun Heo start = offsets[area]; 2111ca23e405STejun Heo end = start + sizes[area]; 2112ca23e405STejun Heo 2113ca23e405STejun Heo /* is everything aligned properly? */ 2114ca23e405STejun Heo BUG_ON(!IS_ALIGNED(offsets[area], align)); 2115ca23e405STejun Heo BUG_ON(!IS_ALIGNED(sizes[area], align)); 2116ca23e405STejun Heo 2117ca23e405STejun Heo /* detect the area with the highest address */ 2118ca23e405STejun Heo if (start > offsets[last_area]) 2119ca23e405STejun Heo last_area = area; 2120ca23e405STejun Heo 2121ca23e405STejun Heo for (area2 = 0; area2 < nr_vms; area2++) { 2122ca23e405STejun Heo unsigned long start2 = offsets[area2]; 2123ca23e405STejun Heo unsigned long end2 = start2 + sizes[area2]; 2124ca23e405STejun Heo 2125ca23e405STejun Heo if (area2 == area) 2126ca23e405STejun Heo continue; 2127ca23e405STejun Heo 2128ca23e405STejun Heo BUG_ON(start2 >= start && start2 < end); 2129ca23e405STejun Heo BUG_ON(end2 <= end && end2 > start); 2130ca23e405STejun Heo } 2131ca23e405STejun Heo } 2132ca23e405STejun Heo last_end = offsets[last_area] + sizes[last_area]; 2133ca23e405STejun Heo 2134ca23e405STejun Heo if (vmalloc_end - vmalloc_start < last_end) { 2135ca23e405STejun Heo WARN_ON(true); 2136ca23e405STejun Heo return NULL; 2137ca23e405STejun Heo } 2138ca23e405STejun Heo 2139ca23e405STejun Heo vms = kzalloc(sizeof(vms[0]) * nr_vms, gfp_mask); 2140ca23e405STejun Heo vas = kzalloc(sizeof(vas[0]) * nr_vms, gfp_mask); 2141ca23e405STejun Heo if (!vas || !vms) 2142ca23e405STejun Heo goto err_free; 2143ca23e405STejun Heo 2144ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 2145ca23e405STejun Heo vas[area] = kzalloc(sizeof(struct vmap_area), gfp_mask); 2146ca23e405STejun Heo vms[area] = kzalloc(sizeof(struct vm_struct), gfp_mask); 2147ca23e405STejun Heo if (!vas[area] || !vms[area]) 2148ca23e405STejun Heo goto err_free; 2149ca23e405STejun Heo } 2150ca23e405STejun Heo retry: 2151ca23e405STejun Heo spin_lock(&vmap_area_lock); 2152ca23e405STejun Heo 2153ca23e405STejun Heo /* start scanning - we scan from the top, begin with the last area */ 2154ca23e405STejun Heo area = term_area = last_area; 2155ca23e405STejun Heo start = offsets[area]; 2156ca23e405STejun Heo end = start + sizes[area]; 2157ca23e405STejun Heo 2158ca23e405STejun Heo if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) { 2159ca23e405STejun Heo base = vmalloc_end - last_end; 2160ca23e405STejun Heo goto found; 2161ca23e405STejun Heo } 2162ca23e405STejun Heo base = pvm_determine_end(&next, &prev, align) - end; 2163ca23e405STejun Heo 2164ca23e405STejun Heo while (true) { 2165ca23e405STejun Heo BUG_ON(next && next->va_end <= base + end); 2166ca23e405STejun Heo BUG_ON(prev && prev->va_end > base + end); 2167ca23e405STejun Heo 2168ca23e405STejun Heo /* 2169ca23e405STejun Heo * base might have underflowed, add last_end before 2170ca23e405STejun Heo * comparing. 2171ca23e405STejun Heo */ 2172ca23e405STejun Heo if (base + last_end < vmalloc_start + last_end) { 2173ca23e405STejun Heo spin_unlock(&vmap_area_lock); 2174ca23e405STejun Heo if (!purged) { 2175ca23e405STejun Heo purge_vmap_area_lazy(); 2176ca23e405STejun Heo purged = true; 2177ca23e405STejun Heo goto retry; 2178ca23e405STejun Heo } 2179ca23e405STejun Heo goto err_free; 2180ca23e405STejun Heo } 2181ca23e405STejun Heo 2182ca23e405STejun Heo /* 2183ca23e405STejun Heo * If next overlaps, move base downwards so that it's 2184ca23e405STejun Heo * right below next and then recheck. 2185ca23e405STejun Heo */ 2186ca23e405STejun Heo if (next && next->va_start < base + end) { 2187ca23e405STejun Heo base = pvm_determine_end(&next, &prev, align) - end; 2188ca23e405STejun Heo term_area = area; 2189ca23e405STejun Heo continue; 2190ca23e405STejun Heo } 2191ca23e405STejun Heo 2192ca23e405STejun Heo /* 2193ca23e405STejun Heo * If prev overlaps, shift down next and prev and move 2194ca23e405STejun Heo * base so that it's right below new next and then 2195ca23e405STejun Heo * recheck. 2196ca23e405STejun Heo */ 2197ca23e405STejun Heo if (prev && prev->va_end > base + start) { 2198ca23e405STejun Heo next = prev; 2199ca23e405STejun Heo prev = node_to_va(rb_prev(&next->rb_node)); 2200ca23e405STejun Heo base = pvm_determine_end(&next, &prev, align) - end; 2201ca23e405STejun Heo term_area = area; 2202ca23e405STejun Heo continue; 2203ca23e405STejun Heo } 2204ca23e405STejun Heo 2205ca23e405STejun Heo /* 2206ca23e405STejun Heo * This area fits, move on to the previous one. If 2207ca23e405STejun Heo * the previous one is the terminal one, we're done. 2208ca23e405STejun Heo */ 2209ca23e405STejun Heo area = (area + nr_vms - 1) % nr_vms; 2210ca23e405STejun Heo if (area == term_area) 2211ca23e405STejun Heo break; 2212ca23e405STejun Heo start = offsets[area]; 2213ca23e405STejun Heo end = start + sizes[area]; 2214ca23e405STejun Heo pvm_find_next_prev(base + end, &next, &prev); 2215ca23e405STejun Heo } 2216ca23e405STejun Heo found: 2217ca23e405STejun Heo /* we've found a fitting base, insert all va's */ 2218ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 2219ca23e405STejun Heo struct vmap_area *va = vas[area]; 2220ca23e405STejun Heo 2221ca23e405STejun Heo va->va_start = base + offsets[area]; 2222ca23e405STejun Heo va->va_end = va->va_start + sizes[area]; 2223ca23e405STejun Heo __insert_vmap_area(va); 2224ca23e405STejun Heo } 2225ca23e405STejun Heo 2226ca23e405STejun Heo vmap_area_pcpu_hole = base + offsets[last_area]; 2227ca23e405STejun Heo 2228ca23e405STejun Heo spin_unlock(&vmap_area_lock); 2229ca23e405STejun Heo 2230ca23e405STejun Heo /* insert all vm's */ 2231ca23e405STejun Heo for (area = 0; area < nr_vms; area++) 2232ca23e405STejun Heo insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC, 2233ca23e405STejun Heo pcpu_get_vm_areas); 2234ca23e405STejun Heo 2235ca23e405STejun Heo kfree(vas); 2236ca23e405STejun Heo return vms; 2237ca23e405STejun Heo 2238ca23e405STejun Heo err_free: 2239ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 2240ca23e405STejun Heo if (vas) 2241ca23e405STejun Heo kfree(vas[area]); 2242ca23e405STejun Heo if (vms) 2243ca23e405STejun Heo kfree(vms[area]); 2244ca23e405STejun Heo } 2245ca23e405STejun Heo kfree(vas); 2246ca23e405STejun Heo kfree(vms); 2247ca23e405STejun Heo return NULL; 2248ca23e405STejun Heo } 2249ca23e405STejun Heo 2250ca23e405STejun Heo /** 2251ca23e405STejun Heo * pcpu_free_vm_areas - free vmalloc areas for percpu allocator 2252ca23e405STejun Heo * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() 2253ca23e405STejun Heo * @nr_vms: the number of allocated areas 2254ca23e405STejun Heo * 2255ca23e405STejun Heo * Free vm_structs and the array allocated by pcpu_get_vm_areas(). 2256ca23e405STejun Heo */ 2257ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) 2258ca23e405STejun Heo { 2259ca23e405STejun Heo int i; 2260ca23e405STejun Heo 2261ca23e405STejun Heo for (i = 0; i < nr_vms; i++) 2262ca23e405STejun Heo free_vm_area(vms[i]); 2263ca23e405STejun Heo kfree(vms); 2264ca23e405STejun Heo } 2265a10aa579SChristoph Lameter 2266a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS 2267a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos) 2268a10aa579SChristoph Lameter { 2269a10aa579SChristoph Lameter loff_t n = *pos; 2270a10aa579SChristoph Lameter struct vm_struct *v; 2271a10aa579SChristoph Lameter 2272a10aa579SChristoph Lameter read_lock(&vmlist_lock); 2273a10aa579SChristoph Lameter v = vmlist; 2274a10aa579SChristoph Lameter while (n > 0 && v) { 2275a10aa579SChristoph Lameter n--; 2276a10aa579SChristoph Lameter v = v->next; 2277a10aa579SChristoph Lameter } 2278a10aa579SChristoph Lameter if (!n) 2279a10aa579SChristoph Lameter return v; 2280a10aa579SChristoph Lameter 2281a10aa579SChristoph Lameter return NULL; 2282a10aa579SChristoph Lameter 2283a10aa579SChristoph Lameter } 2284a10aa579SChristoph Lameter 2285a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos) 2286a10aa579SChristoph Lameter { 2287a10aa579SChristoph Lameter struct vm_struct *v = p; 2288a10aa579SChristoph Lameter 2289a10aa579SChristoph Lameter ++*pos; 2290a10aa579SChristoph Lameter return v->next; 2291a10aa579SChristoph Lameter } 2292a10aa579SChristoph Lameter 2293a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p) 2294a10aa579SChristoph Lameter { 2295a10aa579SChristoph Lameter read_unlock(&vmlist_lock); 2296a10aa579SChristoph Lameter } 2297a10aa579SChristoph Lameter 2298a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v) 2299a47a126aSEric Dumazet { 2300a47a126aSEric Dumazet if (NUMA_BUILD) { 2301a47a126aSEric Dumazet unsigned int nr, *counters = m->private; 2302a47a126aSEric Dumazet 2303a47a126aSEric Dumazet if (!counters) 2304a47a126aSEric Dumazet return; 2305a47a126aSEric Dumazet 2306a47a126aSEric Dumazet memset(counters, 0, nr_node_ids * sizeof(unsigned int)); 2307a47a126aSEric Dumazet 2308a47a126aSEric Dumazet for (nr = 0; nr < v->nr_pages; nr++) 2309a47a126aSEric Dumazet counters[page_to_nid(v->pages[nr])]++; 2310a47a126aSEric Dumazet 2311a47a126aSEric Dumazet for_each_node_state(nr, N_HIGH_MEMORY) 2312a47a126aSEric Dumazet if (counters[nr]) 2313a47a126aSEric Dumazet seq_printf(m, " N%u=%u", nr, counters[nr]); 2314a47a126aSEric Dumazet } 2315a47a126aSEric Dumazet } 2316a47a126aSEric Dumazet 2317a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p) 2318a10aa579SChristoph Lameter { 2319a10aa579SChristoph Lameter struct vm_struct *v = p; 2320a10aa579SChristoph Lameter 2321a10aa579SChristoph Lameter seq_printf(m, "0x%p-0x%p %7ld", 2322a10aa579SChristoph Lameter v->addr, v->addr + v->size, v->size); 2323a10aa579SChristoph Lameter 232423016969SChristoph Lameter if (v->caller) { 23259c246247SHugh Dickins char buff[KSYM_SYMBOL_LEN]; 232623016969SChristoph Lameter 232723016969SChristoph Lameter seq_putc(m, ' '); 232823016969SChristoph Lameter sprint_symbol(buff, (unsigned long)v->caller); 232923016969SChristoph Lameter seq_puts(m, buff); 233023016969SChristoph Lameter } 233123016969SChristoph Lameter 2332a10aa579SChristoph Lameter if (v->nr_pages) 2333a10aa579SChristoph Lameter seq_printf(m, " pages=%d", v->nr_pages); 2334a10aa579SChristoph Lameter 2335a10aa579SChristoph Lameter if (v->phys_addr) 2336a10aa579SChristoph Lameter seq_printf(m, " phys=%lx", v->phys_addr); 2337a10aa579SChristoph Lameter 2338a10aa579SChristoph Lameter if (v->flags & VM_IOREMAP) 2339a10aa579SChristoph Lameter seq_printf(m, " ioremap"); 2340a10aa579SChristoph Lameter 2341a10aa579SChristoph Lameter if (v->flags & VM_ALLOC) 2342a10aa579SChristoph Lameter seq_printf(m, " vmalloc"); 2343a10aa579SChristoph Lameter 2344a10aa579SChristoph Lameter if (v->flags & VM_MAP) 2345a10aa579SChristoph Lameter seq_printf(m, " vmap"); 2346a10aa579SChristoph Lameter 2347a10aa579SChristoph Lameter if (v->flags & VM_USERMAP) 2348a10aa579SChristoph Lameter seq_printf(m, " user"); 2349a10aa579SChristoph Lameter 2350a10aa579SChristoph Lameter if (v->flags & VM_VPAGES) 2351a10aa579SChristoph Lameter seq_printf(m, " vpages"); 2352a10aa579SChristoph Lameter 2353a47a126aSEric Dumazet show_numa_info(m, v); 2354a10aa579SChristoph Lameter seq_putc(m, '\n'); 2355a10aa579SChristoph Lameter return 0; 2356a10aa579SChristoph Lameter } 2357a10aa579SChristoph Lameter 23585f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = { 2359a10aa579SChristoph Lameter .start = s_start, 2360a10aa579SChristoph Lameter .next = s_next, 2361a10aa579SChristoph Lameter .stop = s_stop, 2362a10aa579SChristoph Lameter .show = s_show, 2363a10aa579SChristoph Lameter }; 23645f6a6a9cSAlexey Dobriyan 23655f6a6a9cSAlexey Dobriyan static int vmalloc_open(struct inode *inode, struct file *file) 23665f6a6a9cSAlexey Dobriyan { 23675f6a6a9cSAlexey Dobriyan unsigned int *ptr = NULL; 23685f6a6a9cSAlexey Dobriyan int ret; 23695f6a6a9cSAlexey Dobriyan 23705f6a6a9cSAlexey Dobriyan if (NUMA_BUILD) 23715f6a6a9cSAlexey Dobriyan ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL); 23725f6a6a9cSAlexey Dobriyan ret = seq_open(file, &vmalloc_op); 23735f6a6a9cSAlexey Dobriyan if (!ret) { 23745f6a6a9cSAlexey Dobriyan struct seq_file *m = file->private_data; 23755f6a6a9cSAlexey Dobriyan m->private = ptr; 23765f6a6a9cSAlexey Dobriyan } else 23775f6a6a9cSAlexey Dobriyan kfree(ptr); 23785f6a6a9cSAlexey Dobriyan return ret; 23795f6a6a9cSAlexey Dobriyan } 23805f6a6a9cSAlexey Dobriyan 23815f6a6a9cSAlexey Dobriyan static const struct file_operations proc_vmalloc_operations = { 23825f6a6a9cSAlexey Dobriyan .open = vmalloc_open, 23835f6a6a9cSAlexey Dobriyan .read = seq_read, 23845f6a6a9cSAlexey Dobriyan .llseek = seq_lseek, 23855f6a6a9cSAlexey Dobriyan .release = seq_release_private, 23865f6a6a9cSAlexey Dobriyan }; 23875f6a6a9cSAlexey Dobriyan 23885f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void) 23895f6a6a9cSAlexey Dobriyan { 23905f6a6a9cSAlexey Dobriyan proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations); 23915f6a6a9cSAlexey Dobriyan return 0; 23925f6a6a9cSAlexey Dobriyan } 23935f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init); 2394a10aa579SChristoph Lameter #endif 2395a10aa579SChristoph Lameter 2396