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> 2960063497SArun Sharma #include <linux/atomic.h> 303b32123dSGideon Israel Dsouza #include <linux/compiler.h> 3132fcfd40SAl Viro #include <linux/llist.h> 323b32123dSGideon Israel Dsouza 331da177e4SLinus Torvalds #include <asm/uaccess.h> 341da177e4SLinus Torvalds #include <asm/tlbflush.h> 352dca6999SDavid Miller #include <asm/shmparam.h> 361da177e4SLinus Torvalds 3732fcfd40SAl Viro struct vfree_deferred { 3832fcfd40SAl Viro struct llist_head list; 3932fcfd40SAl Viro struct work_struct wq; 4032fcfd40SAl Viro }; 4132fcfd40SAl Viro static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred); 4232fcfd40SAl Viro 4332fcfd40SAl Viro static void __vunmap(const void *, int); 4432fcfd40SAl Viro 4532fcfd40SAl Viro static void free_work(struct work_struct *w) 4632fcfd40SAl Viro { 4732fcfd40SAl Viro struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq); 4832fcfd40SAl Viro struct llist_node *llnode = llist_del_all(&p->list); 4932fcfd40SAl Viro while (llnode) { 5032fcfd40SAl Viro void *p = llnode; 5132fcfd40SAl Viro llnode = llist_next(llnode); 5232fcfd40SAl Viro __vunmap(p, 1); 5332fcfd40SAl Viro } 5432fcfd40SAl Viro } 5532fcfd40SAl Viro 56db64fe02SNick Piggin /*** Page table manipulation functions ***/ 57b221385bSAdrian Bunk 581da177e4SLinus Torvalds static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end) 591da177e4SLinus Torvalds { 601da177e4SLinus Torvalds pte_t *pte; 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds pte = pte_offset_kernel(pmd, addr); 631da177e4SLinus Torvalds do { 641da177e4SLinus Torvalds pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte); 651da177e4SLinus Torvalds WARN_ON(!pte_none(ptent) && !pte_present(ptent)); 661da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds 69db64fe02SNick Piggin static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end) 701da177e4SLinus Torvalds { 711da177e4SLinus Torvalds pmd_t *pmd; 721da177e4SLinus Torvalds unsigned long next; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 751da177e4SLinus Torvalds do { 761da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 771da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 781da177e4SLinus Torvalds continue; 791da177e4SLinus Torvalds vunmap_pte_range(pmd, addr, next); 801da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds 83db64fe02SNick Piggin static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end) 841da177e4SLinus Torvalds { 851da177e4SLinus Torvalds pud_t *pud; 861da177e4SLinus Torvalds unsigned long next; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 891da177e4SLinus Torvalds do { 901da177e4SLinus Torvalds next = pud_addr_end(addr, end); 911da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 921da177e4SLinus Torvalds continue; 931da177e4SLinus Torvalds vunmap_pmd_range(pud, addr, next); 941da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 951da177e4SLinus Torvalds } 961da177e4SLinus Torvalds 97db64fe02SNick Piggin static void vunmap_page_range(unsigned long addr, unsigned long end) 981da177e4SLinus Torvalds { 991da177e4SLinus Torvalds pgd_t *pgd; 1001da177e4SLinus Torvalds unsigned long next; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds BUG_ON(addr >= end); 1031da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 1041da177e4SLinus Torvalds do { 1051da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 1061da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 1071da177e4SLinus Torvalds continue; 1081da177e4SLinus Torvalds vunmap_pud_range(pgd, addr, next); 1091da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds static int vmap_pte_range(pmd_t *pmd, unsigned long addr, 113db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1141da177e4SLinus Torvalds { 1151da177e4SLinus Torvalds pte_t *pte; 1161da177e4SLinus Torvalds 117db64fe02SNick Piggin /* 118db64fe02SNick Piggin * nr is a running index into the array which helps higher level 119db64fe02SNick Piggin * callers keep track of where we're up to. 120db64fe02SNick Piggin */ 121db64fe02SNick Piggin 122872fec16SHugh Dickins pte = pte_alloc_kernel(pmd, addr); 1231da177e4SLinus Torvalds if (!pte) 1241da177e4SLinus Torvalds return -ENOMEM; 1251da177e4SLinus Torvalds do { 126db64fe02SNick Piggin struct page *page = pages[*nr]; 127db64fe02SNick Piggin 128db64fe02SNick Piggin if (WARN_ON(!pte_none(*pte))) 129db64fe02SNick Piggin return -EBUSY; 130db64fe02SNick Piggin if (WARN_ON(!page)) 1311da177e4SLinus Torvalds return -ENOMEM; 1321da177e4SLinus Torvalds set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); 133db64fe02SNick Piggin (*nr)++; 1341da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 1351da177e4SLinus Torvalds return 0; 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 138db64fe02SNick Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr, 139db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1401da177e4SLinus Torvalds { 1411da177e4SLinus Torvalds pmd_t *pmd; 1421da177e4SLinus Torvalds unsigned long next; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds pmd = pmd_alloc(&init_mm, pud, addr); 1451da177e4SLinus Torvalds if (!pmd) 1461da177e4SLinus Torvalds return -ENOMEM; 1471da177e4SLinus Torvalds do { 1481da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 149db64fe02SNick Piggin if (vmap_pte_range(pmd, addr, next, prot, pages, nr)) 1501da177e4SLinus Torvalds return -ENOMEM; 1511da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 1521da177e4SLinus Torvalds return 0; 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 155db64fe02SNick Piggin static int vmap_pud_range(pgd_t *pgd, unsigned long addr, 156db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds pud_t *pud; 1591da177e4SLinus Torvalds unsigned long next; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds pud = pud_alloc(&init_mm, pgd, addr); 1621da177e4SLinus Torvalds if (!pud) 1631da177e4SLinus Torvalds return -ENOMEM; 1641da177e4SLinus Torvalds do { 1651da177e4SLinus Torvalds next = pud_addr_end(addr, end); 166db64fe02SNick Piggin if (vmap_pmd_range(pud, addr, next, prot, pages, nr)) 1671da177e4SLinus Torvalds return -ENOMEM; 1681da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 1691da177e4SLinus Torvalds return 0; 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 172db64fe02SNick Piggin /* 173db64fe02SNick Piggin * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and 174db64fe02SNick Piggin * will have pfns corresponding to the "pages" array. 175db64fe02SNick Piggin * 176db64fe02SNick Piggin * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N] 177db64fe02SNick Piggin */ 1788fc48985STejun Heo static int vmap_page_range_noflush(unsigned long start, unsigned long end, 179db64fe02SNick Piggin pgprot_t prot, struct page **pages) 1801da177e4SLinus Torvalds { 1811da177e4SLinus Torvalds pgd_t *pgd; 1821da177e4SLinus Torvalds unsigned long next; 1832e4e27c7SAdam Lackorzynski unsigned long addr = start; 184db64fe02SNick Piggin int err = 0; 185db64fe02SNick Piggin int nr = 0; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds BUG_ON(addr >= end); 1881da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 1891da177e4SLinus Torvalds do { 1901da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 191db64fe02SNick Piggin err = vmap_pud_range(pgd, addr, next, prot, pages, &nr); 1921da177e4SLinus Torvalds if (err) 193bf88c8c8SFigo.zhang return err; 1941da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 195db64fe02SNick Piggin 196db64fe02SNick Piggin return nr; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1998fc48985STejun Heo static int vmap_page_range(unsigned long start, unsigned long end, 2008fc48985STejun Heo pgprot_t prot, struct page **pages) 2018fc48985STejun Heo { 2028fc48985STejun Heo int ret; 2038fc48985STejun Heo 2048fc48985STejun Heo ret = vmap_page_range_noflush(start, end, prot, pages); 2058fc48985STejun Heo flush_cache_vmap(start, end); 2068fc48985STejun Heo return ret; 2078fc48985STejun Heo } 2088fc48985STejun Heo 20981ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x) 21073bdf0a6SLinus Torvalds { 21173bdf0a6SLinus Torvalds /* 212ab4f2ee1SRussell King * ARM, x86-64 and sparc64 put modules in a special place, 21373bdf0a6SLinus Torvalds * and fall back on vmalloc() if that fails. Others 21473bdf0a6SLinus Torvalds * just put it in the vmalloc space. 21573bdf0a6SLinus Torvalds */ 21673bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) 21773bdf0a6SLinus Torvalds unsigned long addr = (unsigned long)x; 21873bdf0a6SLinus Torvalds if (addr >= MODULES_VADDR && addr < MODULES_END) 21973bdf0a6SLinus Torvalds return 1; 22073bdf0a6SLinus Torvalds #endif 22173bdf0a6SLinus Torvalds return is_vmalloc_addr(x); 22273bdf0a6SLinus Torvalds } 22373bdf0a6SLinus Torvalds 22448667e7aSChristoph Lameter /* 225add688fbSmalc * Walk a vmap address to the struct page it maps. 22648667e7aSChristoph Lameter */ 227add688fbSmalc struct page *vmalloc_to_page(const void *vmalloc_addr) 22848667e7aSChristoph Lameter { 22948667e7aSChristoph Lameter unsigned long addr = (unsigned long) vmalloc_addr; 230add688fbSmalc struct page *page = NULL; 23148667e7aSChristoph Lameter pgd_t *pgd = pgd_offset_k(addr); 23248667e7aSChristoph Lameter 2337aa413deSIngo Molnar /* 2347aa413deSIngo Molnar * XXX we might need to change this if we add VIRTUAL_BUG_ON for 2357aa413deSIngo Molnar * architectures that do not vmalloc module space 2367aa413deSIngo Molnar */ 23773bdf0a6SLinus Torvalds VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); 23859ea7463SJiri Slaby 23948667e7aSChristoph Lameter if (!pgd_none(*pgd)) { 240db64fe02SNick Piggin pud_t *pud = pud_offset(pgd, addr); 24148667e7aSChristoph Lameter if (!pud_none(*pud)) { 242db64fe02SNick Piggin pmd_t *pmd = pmd_offset(pud, addr); 24348667e7aSChristoph Lameter if (!pmd_none(*pmd)) { 244db64fe02SNick Piggin pte_t *ptep, pte; 245db64fe02SNick Piggin 24648667e7aSChristoph Lameter ptep = pte_offset_map(pmd, addr); 24748667e7aSChristoph Lameter pte = *ptep; 24848667e7aSChristoph Lameter if (pte_present(pte)) 249add688fbSmalc page = pte_page(pte); 25048667e7aSChristoph Lameter pte_unmap(ptep); 25148667e7aSChristoph Lameter } 25248667e7aSChristoph Lameter } 25348667e7aSChristoph Lameter } 254add688fbSmalc return page; 255ece86e22SJianyu Zhan } 256ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page); 257ece86e22SJianyu Zhan 258add688fbSmalc /* 259add688fbSmalc * Map a vmalloc()-space virtual address to the physical page frame number. 260add688fbSmalc */ 261add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr) 262add688fbSmalc { 263add688fbSmalc return page_to_pfn(vmalloc_to_page(vmalloc_addr)); 264add688fbSmalc } 265add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn); 266add688fbSmalc 267db64fe02SNick Piggin 268db64fe02SNick Piggin /*** Global kva allocator ***/ 269db64fe02SNick Piggin 270db64fe02SNick Piggin #define VM_LAZY_FREE 0x01 271db64fe02SNick Piggin #define VM_LAZY_FREEING 0x02 272db64fe02SNick Piggin #define VM_VM_AREA 0x04 273db64fe02SNick Piggin 274db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock); 275f1c4069eSJoonsoo Kim /* Export for kexec only */ 276f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list); 27789699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT; 27889699605SNick Piggin 27989699605SNick Piggin /* The vmap cache globals are protected by vmap_area_lock */ 28089699605SNick Piggin static struct rb_node *free_vmap_cache; 28189699605SNick Piggin static unsigned long cached_hole_size; 28289699605SNick Piggin static unsigned long cached_vstart; 28389699605SNick Piggin static unsigned long cached_align; 28489699605SNick Piggin 285ca23e405STejun Heo static unsigned long vmap_area_pcpu_hole; 286db64fe02SNick Piggin 287db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr) 2881da177e4SLinus Torvalds { 289db64fe02SNick Piggin struct rb_node *n = vmap_area_root.rb_node; 290db64fe02SNick Piggin 291db64fe02SNick Piggin while (n) { 292db64fe02SNick Piggin struct vmap_area *va; 293db64fe02SNick Piggin 294db64fe02SNick Piggin va = rb_entry(n, struct vmap_area, rb_node); 295db64fe02SNick Piggin if (addr < va->va_start) 296db64fe02SNick Piggin n = n->rb_left; 297cef2ac3fSHATAYAMA Daisuke else if (addr >= va->va_end) 298db64fe02SNick Piggin n = n->rb_right; 299db64fe02SNick Piggin else 300db64fe02SNick Piggin return va; 301db64fe02SNick Piggin } 302db64fe02SNick Piggin 303db64fe02SNick Piggin return NULL; 304db64fe02SNick Piggin } 305db64fe02SNick Piggin 306db64fe02SNick Piggin static void __insert_vmap_area(struct vmap_area *va) 307db64fe02SNick Piggin { 308db64fe02SNick Piggin struct rb_node **p = &vmap_area_root.rb_node; 309db64fe02SNick Piggin struct rb_node *parent = NULL; 310db64fe02SNick Piggin struct rb_node *tmp; 311db64fe02SNick Piggin 312db64fe02SNick Piggin while (*p) { 313170168d0SNamhyung Kim struct vmap_area *tmp_va; 314db64fe02SNick Piggin 315db64fe02SNick Piggin parent = *p; 316170168d0SNamhyung Kim tmp_va = rb_entry(parent, struct vmap_area, rb_node); 317170168d0SNamhyung Kim if (va->va_start < tmp_va->va_end) 318db64fe02SNick Piggin p = &(*p)->rb_left; 319170168d0SNamhyung Kim else if (va->va_end > tmp_va->va_start) 320db64fe02SNick Piggin p = &(*p)->rb_right; 321db64fe02SNick Piggin else 322db64fe02SNick Piggin BUG(); 323db64fe02SNick Piggin } 324db64fe02SNick Piggin 325db64fe02SNick Piggin rb_link_node(&va->rb_node, parent, p); 326db64fe02SNick Piggin rb_insert_color(&va->rb_node, &vmap_area_root); 327db64fe02SNick Piggin 3284341fa45SJoonsoo Kim /* address-sort this list */ 329db64fe02SNick Piggin tmp = rb_prev(&va->rb_node); 330db64fe02SNick Piggin if (tmp) { 331db64fe02SNick Piggin struct vmap_area *prev; 332db64fe02SNick Piggin prev = rb_entry(tmp, struct vmap_area, rb_node); 333db64fe02SNick Piggin list_add_rcu(&va->list, &prev->list); 334db64fe02SNick Piggin } else 335db64fe02SNick Piggin list_add_rcu(&va->list, &vmap_area_list); 336db64fe02SNick Piggin } 337db64fe02SNick Piggin 338db64fe02SNick Piggin static void purge_vmap_area_lazy(void); 339db64fe02SNick Piggin 340db64fe02SNick Piggin /* 341db64fe02SNick Piggin * Allocate a region of KVA of the specified size and alignment, within the 342db64fe02SNick Piggin * vstart and vend. 343db64fe02SNick Piggin */ 344db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size, 345db64fe02SNick Piggin unsigned long align, 346db64fe02SNick Piggin unsigned long vstart, unsigned long vend, 347db64fe02SNick Piggin int node, gfp_t gfp_mask) 348db64fe02SNick Piggin { 349db64fe02SNick Piggin struct vmap_area *va; 350db64fe02SNick Piggin struct rb_node *n; 3511da177e4SLinus Torvalds unsigned long addr; 352db64fe02SNick Piggin int purged = 0; 35389699605SNick Piggin struct vmap_area *first; 354db64fe02SNick Piggin 3557766970cSNick Piggin BUG_ON(!size); 356db64fe02SNick Piggin BUG_ON(size & ~PAGE_MASK); 35789699605SNick Piggin BUG_ON(!is_power_of_2(align)); 358db64fe02SNick Piggin 359db64fe02SNick Piggin va = kmalloc_node(sizeof(struct vmap_area), 360db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 361db64fe02SNick Piggin if (unlikely(!va)) 362db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 363db64fe02SNick Piggin 3647f88f88fSCatalin Marinas /* 3657f88f88fSCatalin Marinas * Only scan the relevant parts containing pointers to other objects 3667f88f88fSCatalin Marinas * to avoid false negatives. 3677f88f88fSCatalin Marinas */ 3687f88f88fSCatalin Marinas kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK); 3697f88f88fSCatalin Marinas 370db64fe02SNick Piggin retry: 371db64fe02SNick Piggin spin_lock(&vmap_area_lock); 37289699605SNick Piggin /* 37389699605SNick Piggin * Invalidate cache if we have more permissive parameters. 37489699605SNick Piggin * cached_hole_size notes the largest hole noticed _below_ 37589699605SNick Piggin * the vmap_area cached in free_vmap_cache: if size fits 37689699605SNick Piggin * into that hole, we want to scan from vstart to reuse 37789699605SNick Piggin * the hole instead of allocating above free_vmap_cache. 37889699605SNick Piggin * Note that __free_vmap_area may update free_vmap_cache 37989699605SNick Piggin * without updating cached_hole_size or cached_align. 38089699605SNick Piggin */ 38189699605SNick Piggin if (!free_vmap_cache || 38289699605SNick Piggin size < cached_hole_size || 38389699605SNick Piggin vstart < cached_vstart || 38489699605SNick Piggin align < cached_align) { 38589699605SNick Piggin nocache: 38689699605SNick Piggin cached_hole_size = 0; 38789699605SNick Piggin free_vmap_cache = NULL; 38889699605SNick Piggin } 38989699605SNick Piggin /* record if we encounter less permissive parameters */ 39089699605SNick Piggin cached_vstart = vstart; 39189699605SNick Piggin cached_align = align; 39289699605SNick Piggin 39389699605SNick Piggin /* find starting point for our search */ 39489699605SNick Piggin if (free_vmap_cache) { 39589699605SNick Piggin first = rb_entry(free_vmap_cache, struct vmap_area, rb_node); 396248ac0e1SJohannes Weiner addr = ALIGN(first->va_end, align); 39789699605SNick Piggin if (addr < vstart) 39889699605SNick Piggin goto nocache; 399bcb615a8SZhang Yanfei if (addr + size < addr) 4007766970cSNick Piggin goto overflow; 4017766970cSNick Piggin 40289699605SNick Piggin } else { 40389699605SNick Piggin addr = ALIGN(vstart, align); 404bcb615a8SZhang Yanfei if (addr + size < addr) 40589699605SNick Piggin goto overflow; 406db64fe02SNick Piggin 40789699605SNick Piggin n = vmap_area_root.rb_node; 40889699605SNick Piggin first = NULL; 40989699605SNick Piggin 41089699605SNick Piggin while (n) { 411db64fe02SNick Piggin struct vmap_area *tmp; 412db64fe02SNick Piggin tmp = rb_entry(n, struct vmap_area, rb_node); 413db64fe02SNick Piggin if (tmp->va_end >= addr) { 414db64fe02SNick Piggin first = tmp; 41589699605SNick Piggin if (tmp->va_start <= addr) 41689699605SNick Piggin break; 417db64fe02SNick Piggin n = n->rb_left; 41889699605SNick Piggin } else 419db64fe02SNick Piggin n = n->rb_right; 420db64fe02SNick Piggin } 421db64fe02SNick Piggin 422db64fe02SNick Piggin if (!first) 423db64fe02SNick Piggin goto found; 424db64fe02SNick Piggin } 425db64fe02SNick Piggin 42689699605SNick Piggin /* from the starting point, walk areas until a suitable hole is found */ 427248ac0e1SJohannes Weiner while (addr + size > first->va_start && addr + size <= vend) { 42889699605SNick Piggin if (addr + cached_hole_size < first->va_start) 42989699605SNick Piggin cached_hole_size = first->va_start - addr; 430248ac0e1SJohannes Weiner addr = ALIGN(first->va_end, align); 431bcb615a8SZhang Yanfei if (addr + size < addr) 4327766970cSNick Piggin goto overflow; 433db64fe02SNick Piggin 43492ca922fSHong zhi guo if (list_is_last(&first->list, &vmap_area_list)) 435db64fe02SNick Piggin goto found; 43692ca922fSHong zhi guo 43792ca922fSHong zhi guo first = list_entry(first->list.next, 43892ca922fSHong zhi guo struct vmap_area, list); 439db64fe02SNick Piggin } 44089699605SNick Piggin 441db64fe02SNick Piggin found: 44289699605SNick Piggin if (addr + size > vend) 44389699605SNick Piggin goto overflow; 44489699605SNick Piggin 44589699605SNick Piggin va->va_start = addr; 44689699605SNick Piggin va->va_end = addr + size; 44789699605SNick Piggin va->flags = 0; 44889699605SNick Piggin __insert_vmap_area(va); 44989699605SNick Piggin free_vmap_cache = &va->rb_node; 45089699605SNick Piggin spin_unlock(&vmap_area_lock); 45189699605SNick Piggin 45289699605SNick Piggin BUG_ON(va->va_start & (align-1)); 45389699605SNick Piggin BUG_ON(va->va_start < vstart); 45489699605SNick Piggin BUG_ON(va->va_end > vend); 45589699605SNick Piggin 45689699605SNick Piggin return va; 45789699605SNick Piggin 4587766970cSNick Piggin overflow: 459db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 460db64fe02SNick Piggin if (!purged) { 461db64fe02SNick Piggin purge_vmap_area_lazy(); 462db64fe02SNick Piggin purged = 1; 463db64fe02SNick Piggin goto retry; 464db64fe02SNick Piggin } 465db64fe02SNick Piggin if (printk_ratelimit()) 466c1279c4eSGlauber Costa printk(KERN_WARNING 467c1279c4eSGlauber Costa "vmap allocation for size %lu failed: " 468c1279c4eSGlauber Costa "use vmalloc=<size> to increase size.\n", size); 4692498ce42SRalph Wuerthner kfree(va); 470db64fe02SNick Piggin return ERR_PTR(-EBUSY); 471db64fe02SNick Piggin } 472db64fe02SNick Piggin 473db64fe02SNick Piggin static void __free_vmap_area(struct vmap_area *va) 474db64fe02SNick Piggin { 475db64fe02SNick Piggin BUG_ON(RB_EMPTY_NODE(&va->rb_node)); 47689699605SNick Piggin 47789699605SNick Piggin if (free_vmap_cache) { 47889699605SNick Piggin if (va->va_end < cached_vstart) { 47989699605SNick Piggin free_vmap_cache = NULL; 48089699605SNick Piggin } else { 48189699605SNick Piggin struct vmap_area *cache; 48289699605SNick Piggin cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node); 48389699605SNick Piggin if (va->va_start <= cache->va_start) { 48489699605SNick Piggin free_vmap_cache = rb_prev(&va->rb_node); 48589699605SNick Piggin /* 48689699605SNick Piggin * We don't try to update cached_hole_size or 48789699605SNick Piggin * cached_align, but it won't go very wrong. 48889699605SNick Piggin */ 48989699605SNick Piggin } 49089699605SNick Piggin } 49189699605SNick Piggin } 492db64fe02SNick Piggin rb_erase(&va->rb_node, &vmap_area_root); 493db64fe02SNick Piggin RB_CLEAR_NODE(&va->rb_node); 494db64fe02SNick Piggin list_del_rcu(&va->list); 495db64fe02SNick Piggin 496ca23e405STejun Heo /* 497ca23e405STejun Heo * Track the highest possible candidate for pcpu area 498ca23e405STejun Heo * allocation. Areas outside of vmalloc area can be returned 499ca23e405STejun Heo * here too, consider only end addresses which fall inside 500ca23e405STejun Heo * vmalloc area proper. 501ca23e405STejun Heo */ 502ca23e405STejun Heo if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END) 503ca23e405STejun Heo vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end); 504ca23e405STejun Heo 50514769de9SLai Jiangshan kfree_rcu(va, rcu_head); 506db64fe02SNick Piggin } 507db64fe02SNick Piggin 508db64fe02SNick Piggin /* 509db64fe02SNick Piggin * Free a region of KVA allocated by alloc_vmap_area 510db64fe02SNick Piggin */ 511db64fe02SNick Piggin static void free_vmap_area(struct vmap_area *va) 512db64fe02SNick Piggin { 513db64fe02SNick Piggin spin_lock(&vmap_area_lock); 514db64fe02SNick Piggin __free_vmap_area(va); 515db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 516db64fe02SNick Piggin } 517db64fe02SNick Piggin 518db64fe02SNick Piggin /* 519db64fe02SNick Piggin * Clear the pagetable entries of a given vmap_area 520db64fe02SNick Piggin */ 521db64fe02SNick Piggin static void unmap_vmap_area(struct vmap_area *va) 522db64fe02SNick Piggin { 523db64fe02SNick Piggin vunmap_page_range(va->va_start, va->va_end); 524db64fe02SNick Piggin } 525db64fe02SNick Piggin 526cd52858cSNick Piggin static void vmap_debug_free_range(unsigned long start, unsigned long end) 527cd52858cSNick Piggin { 528cd52858cSNick Piggin /* 529cd52858cSNick Piggin * Unmap page tables and force a TLB flush immediately if 530cd52858cSNick Piggin * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free 531cd52858cSNick Piggin * bugs similarly to those in linear kernel virtual address 532cd52858cSNick Piggin * space after a page has been freed. 533cd52858cSNick Piggin * 534cd52858cSNick Piggin * All the lazy freeing logic is still retained, in order to 535cd52858cSNick Piggin * minimise intrusiveness of this debugging feature. 536cd52858cSNick Piggin * 537cd52858cSNick Piggin * This is going to be *slow* (linear kernel virtual address 538cd52858cSNick Piggin * debugging doesn't do a broadcast TLB flush so it is a lot 539cd52858cSNick Piggin * faster). 540cd52858cSNick Piggin */ 541cd52858cSNick Piggin #ifdef CONFIG_DEBUG_PAGEALLOC 542cd52858cSNick Piggin vunmap_page_range(start, end); 543cd52858cSNick Piggin flush_tlb_kernel_range(start, end); 544cd52858cSNick Piggin #endif 545cd52858cSNick Piggin } 546cd52858cSNick Piggin 547db64fe02SNick Piggin /* 548db64fe02SNick Piggin * lazy_max_pages is the maximum amount of virtual address space we gather up 549db64fe02SNick Piggin * before attempting to purge with a TLB flush. 550db64fe02SNick Piggin * 551db64fe02SNick Piggin * There is a tradeoff here: a larger number will cover more kernel page tables 552db64fe02SNick Piggin * and take slightly longer to purge, but it will linearly reduce the number of 553db64fe02SNick Piggin * global TLB flushes that must be performed. It would seem natural to scale 554db64fe02SNick Piggin * this number up linearly with the number of CPUs (because vmapping activity 555db64fe02SNick Piggin * could also scale linearly with the number of CPUs), however it is likely 556db64fe02SNick Piggin * that in practice, workloads might be constrained in other ways that mean 557db64fe02SNick Piggin * vmap activity will not scale linearly with CPUs. Also, I want to be 558db64fe02SNick Piggin * conservative and not introduce a big latency on huge systems, so go with 559db64fe02SNick Piggin * a less aggressive log scale. It will still be an improvement over the old 560db64fe02SNick Piggin * code, and it will be simple to change the scale factor if we find that it 561db64fe02SNick Piggin * becomes a problem on bigger systems. 562db64fe02SNick Piggin */ 563db64fe02SNick Piggin static unsigned long lazy_max_pages(void) 564db64fe02SNick Piggin { 565db64fe02SNick Piggin unsigned int log; 566db64fe02SNick Piggin 567db64fe02SNick Piggin log = fls(num_online_cpus()); 568db64fe02SNick Piggin 569db64fe02SNick Piggin return log * (32UL * 1024 * 1024 / PAGE_SIZE); 570db64fe02SNick Piggin } 571db64fe02SNick Piggin 572db64fe02SNick Piggin static atomic_t vmap_lazy_nr = ATOMIC_INIT(0); 573db64fe02SNick Piggin 57402b709dfSNick Piggin /* for per-CPU blocks */ 57502b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void); 57602b709dfSNick Piggin 577db64fe02SNick Piggin /* 5783ee48b6aSCliff Wickman * called before a call to iounmap() if the caller wants vm_area_struct's 5793ee48b6aSCliff Wickman * immediately freed. 5803ee48b6aSCliff Wickman */ 5813ee48b6aSCliff Wickman void set_iounmap_nonlazy(void) 5823ee48b6aSCliff Wickman { 5833ee48b6aSCliff Wickman atomic_set(&vmap_lazy_nr, lazy_max_pages()+1); 5843ee48b6aSCliff Wickman } 5853ee48b6aSCliff Wickman 5863ee48b6aSCliff Wickman /* 587db64fe02SNick Piggin * Purges all lazily-freed vmap areas. 588db64fe02SNick Piggin * 589db64fe02SNick Piggin * If sync is 0 then don't purge if there is already a purge in progress. 590db64fe02SNick Piggin * If force_flush is 1, then flush kernel TLBs between *start and *end even 591db64fe02SNick Piggin * if we found no lazy vmap areas to unmap (callers can use this to optimise 592db64fe02SNick Piggin * their own TLB flushing). 593db64fe02SNick Piggin * Returns with *start = min(*start, lowest purged address) 594db64fe02SNick Piggin * *end = max(*end, highest purged address) 595db64fe02SNick Piggin */ 596db64fe02SNick Piggin static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end, 597db64fe02SNick Piggin int sync, int force_flush) 598db64fe02SNick Piggin { 59946666d8aSAndrew Morton static DEFINE_SPINLOCK(purge_lock); 600db64fe02SNick Piggin LIST_HEAD(valist); 601db64fe02SNick Piggin struct vmap_area *va; 602cbb76676SVegard Nossum struct vmap_area *n_va; 603db64fe02SNick Piggin int nr = 0; 604db64fe02SNick Piggin 605db64fe02SNick Piggin /* 606db64fe02SNick Piggin * If sync is 0 but force_flush is 1, we'll go sync anyway but callers 607db64fe02SNick Piggin * should not expect such behaviour. This just simplifies locking for 608db64fe02SNick Piggin * the case that isn't actually used at the moment anyway. 609db64fe02SNick Piggin */ 610db64fe02SNick Piggin if (!sync && !force_flush) { 61146666d8aSAndrew Morton if (!spin_trylock(&purge_lock)) 612db64fe02SNick Piggin return; 613db64fe02SNick Piggin } else 61446666d8aSAndrew Morton spin_lock(&purge_lock); 615db64fe02SNick Piggin 61602b709dfSNick Piggin if (sync) 61702b709dfSNick Piggin purge_fragmented_blocks_allcpus(); 61802b709dfSNick Piggin 619db64fe02SNick Piggin rcu_read_lock(); 620db64fe02SNick Piggin list_for_each_entry_rcu(va, &vmap_area_list, list) { 621db64fe02SNick Piggin if (va->flags & VM_LAZY_FREE) { 622db64fe02SNick Piggin if (va->va_start < *start) 623db64fe02SNick Piggin *start = va->va_start; 624db64fe02SNick Piggin if (va->va_end > *end) 625db64fe02SNick Piggin *end = va->va_end; 626db64fe02SNick Piggin nr += (va->va_end - va->va_start) >> PAGE_SHIFT; 627db64fe02SNick Piggin list_add_tail(&va->purge_list, &valist); 628db64fe02SNick Piggin va->flags |= VM_LAZY_FREEING; 629db64fe02SNick Piggin va->flags &= ~VM_LAZY_FREE; 630db64fe02SNick Piggin } 631db64fe02SNick Piggin } 632db64fe02SNick Piggin rcu_read_unlock(); 633db64fe02SNick Piggin 63488f50044SYongseok Koh if (nr) 635db64fe02SNick Piggin atomic_sub(nr, &vmap_lazy_nr); 636db64fe02SNick Piggin 637db64fe02SNick Piggin if (nr || force_flush) 638db64fe02SNick Piggin flush_tlb_kernel_range(*start, *end); 639db64fe02SNick Piggin 640db64fe02SNick Piggin if (nr) { 641db64fe02SNick Piggin spin_lock(&vmap_area_lock); 642cbb76676SVegard Nossum list_for_each_entry_safe(va, n_va, &valist, purge_list) 643db64fe02SNick Piggin __free_vmap_area(va); 644db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 645db64fe02SNick Piggin } 64646666d8aSAndrew Morton spin_unlock(&purge_lock); 647db64fe02SNick Piggin } 648db64fe02SNick Piggin 649db64fe02SNick Piggin /* 650496850e5SNick Piggin * Kick off a purge of the outstanding lazy areas. Don't bother if somebody 651496850e5SNick Piggin * is already purging. 652496850e5SNick Piggin */ 653496850e5SNick Piggin static void try_purge_vmap_area_lazy(void) 654496850e5SNick Piggin { 655496850e5SNick Piggin unsigned long start = ULONG_MAX, end = 0; 656496850e5SNick Piggin 657496850e5SNick Piggin __purge_vmap_area_lazy(&start, &end, 0, 0); 658496850e5SNick Piggin } 659496850e5SNick Piggin 660496850e5SNick Piggin /* 661db64fe02SNick Piggin * Kick off a purge of the outstanding lazy areas. 662db64fe02SNick Piggin */ 663db64fe02SNick Piggin static void purge_vmap_area_lazy(void) 664db64fe02SNick Piggin { 665db64fe02SNick Piggin unsigned long start = ULONG_MAX, end = 0; 666db64fe02SNick Piggin 667496850e5SNick Piggin __purge_vmap_area_lazy(&start, &end, 1, 0); 668db64fe02SNick Piggin } 669db64fe02SNick Piggin 670db64fe02SNick Piggin /* 67164141da5SJeremy Fitzhardinge * Free a vmap area, caller ensuring that the area has been unmapped 67264141da5SJeremy Fitzhardinge * and flush_cache_vunmap had been called for the correct range 67364141da5SJeremy Fitzhardinge * previously. 674db64fe02SNick Piggin */ 67564141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va) 676db64fe02SNick Piggin { 677db64fe02SNick Piggin va->flags |= VM_LAZY_FREE; 678db64fe02SNick Piggin atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr); 679db64fe02SNick Piggin if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages())) 680496850e5SNick Piggin try_purge_vmap_area_lazy(); 681db64fe02SNick Piggin } 682db64fe02SNick Piggin 683b29acbdcSNick Piggin /* 68464141da5SJeremy Fitzhardinge * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been 68564141da5SJeremy Fitzhardinge * called for the correct range previously. 68664141da5SJeremy Fitzhardinge */ 68764141da5SJeremy Fitzhardinge static void free_unmap_vmap_area_noflush(struct vmap_area *va) 68864141da5SJeremy Fitzhardinge { 68964141da5SJeremy Fitzhardinge unmap_vmap_area(va); 69064141da5SJeremy Fitzhardinge free_vmap_area_noflush(va); 69164141da5SJeremy Fitzhardinge } 69264141da5SJeremy Fitzhardinge 69364141da5SJeremy Fitzhardinge /* 694b29acbdcSNick Piggin * Free and unmap a vmap area 695b29acbdcSNick Piggin */ 696b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va) 697b29acbdcSNick Piggin { 698b29acbdcSNick Piggin flush_cache_vunmap(va->va_start, va->va_end); 699b29acbdcSNick Piggin free_unmap_vmap_area_noflush(va); 700b29acbdcSNick Piggin } 701b29acbdcSNick Piggin 702db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr) 703db64fe02SNick Piggin { 704db64fe02SNick Piggin struct vmap_area *va; 705db64fe02SNick Piggin 706db64fe02SNick Piggin spin_lock(&vmap_area_lock); 707db64fe02SNick Piggin va = __find_vmap_area(addr); 708db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 709db64fe02SNick Piggin 710db64fe02SNick Piggin return va; 711db64fe02SNick Piggin } 712db64fe02SNick Piggin 713db64fe02SNick Piggin static void free_unmap_vmap_area_addr(unsigned long addr) 714db64fe02SNick Piggin { 715db64fe02SNick Piggin struct vmap_area *va; 716db64fe02SNick Piggin 717db64fe02SNick Piggin va = find_vmap_area(addr); 718db64fe02SNick Piggin BUG_ON(!va); 719db64fe02SNick Piggin free_unmap_vmap_area(va); 720db64fe02SNick Piggin } 721db64fe02SNick Piggin 722db64fe02SNick Piggin 723db64fe02SNick Piggin /*** Per cpu kva allocator ***/ 724db64fe02SNick Piggin 725db64fe02SNick Piggin /* 726db64fe02SNick Piggin * vmap space is limited especially on 32 bit architectures. Ensure there is 727db64fe02SNick Piggin * room for at least 16 percpu vmap blocks per CPU. 728db64fe02SNick Piggin */ 729db64fe02SNick Piggin /* 730db64fe02SNick Piggin * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able 731db64fe02SNick Piggin * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess 732db64fe02SNick Piggin * instead (we just need a rough idea) 733db64fe02SNick Piggin */ 734db64fe02SNick Piggin #if BITS_PER_LONG == 32 735db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024) 736db64fe02SNick Piggin #else 737db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024*1024) 738db64fe02SNick Piggin #endif 739db64fe02SNick Piggin 740db64fe02SNick Piggin #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) 741db64fe02SNick Piggin #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ 742db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ 743db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) 744db64fe02SNick Piggin #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ 745db64fe02SNick Piggin #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ 746f982f915SClemens Ladisch #define VMAP_BBMAP_BITS \ 747f982f915SClemens Ladisch VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ 748db64fe02SNick Piggin VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ 749f982f915SClemens Ladisch VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16)) 750db64fe02SNick Piggin 751db64fe02SNick Piggin #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) 752db64fe02SNick Piggin 7539b463334SJeremy Fitzhardinge static bool vmap_initialized __read_mostly = false; 7549b463334SJeremy Fitzhardinge 755db64fe02SNick Piggin struct vmap_block_queue { 756db64fe02SNick Piggin spinlock_t lock; 757db64fe02SNick Piggin struct list_head free; 758db64fe02SNick Piggin }; 759db64fe02SNick Piggin 760db64fe02SNick Piggin struct vmap_block { 761db64fe02SNick Piggin spinlock_t lock; 762db64fe02SNick Piggin struct vmap_area *va; 763db64fe02SNick Piggin unsigned long free, dirty; 764db64fe02SNick Piggin DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS); 765db64fe02SNick Piggin struct list_head free_list; 766db64fe02SNick Piggin struct rcu_head rcu_head; 76702b709dfSNick Piggin struct list_head purge; 768db64fe02SNick Piggin }; 769db64fe02SNick Piggin 770db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ 771db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); 772db64fe02SNick Piggin 773db64fe02SNick Piggin /* 774db64fe02SNick Piggin * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block 775db64fe02SNick Piggin * in the free path. Could get rid of this if we change the API to return a 776db64fe02SNick Piggin * "cookie" from alloc, to be passed to free. But no big deal yet. 777db64fe02SNick Piggin */ 778db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_block_tree_lock); 779db64fe02SNick Piggin static RADIX_TREE(vmap_block_tree, GFP_ATOMIC); 780db64fe02SNick Piggin 781db64fe02SNick Piggin /* 782db64fe02SNick Piggin * We should probably have a fallback mechanism to allocate virtual memory 783db64fe02SNick Piggin * out of partially filled vmap blocks. However vmap block sizing should be 784db64fe02SNick Piggin * fairly reasonable according to the vmalloc size, so it shouldn't be a 785db64fe02SNick Piggin * big problem. 786db64fe02SNick Piggin */ 787db64fe02SNick Piggin 788db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr) 789db64fe02SNick Piggin { 790db64fe02SNick Piggin addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); 791db64fe02SNick Piggin addr /= VMAP_BLOCK_SIZE; 792db64fe02SNick Piggin return addr; 793db64fe02SNick Piggin } 794db64fe02SNick Piggin 795db64fe02SNick Piggin static struct vmap_block *new_vmap_block(gfp_t gfp_mask) 796db64fe02SNick Piggin { 797db64fe02SNick Piggin struct vmap_block_queue *vbq; 798db64fe02SNick Piggin struct vmap_block *vb; 799db64fe02SNick Piggin struct vmap_area *va; 800db64fe02SNick Piggin unsigned long vb_idx; 801db64fe02SNick Piggin int node, err; 802db64fe02SNick Piggin 803db64fe02SNick Piggin node = numa_node_id(); 804db64fe02SNick Piggin 805db64fe02SNick Piggin vb = kmalloc_node(sizeof(struct vmap_block), 806db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 807db64fe02SNick Piggin if (unlikely(!vb)) 808db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 809db64fe02SNick Piggin 810db64fe02SNick Piggin va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, 811db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, 812db64fe02SNick Piggin node, gfp_mask); 813ddf9c6d4STobias Klauser if (IS_ERR(va)) { 814db64fe02SNick Piggin kfree(vb); 815e7d86340SJulia Lawall return ERR_CAST(va); 816db64fe02SNick Piggin } 817db64fe02SNick Piggin 818db64fe02SNick Piggin err = radix_tree_preload(gfp_mask); 819db64fe02SNick Piggin if (unlikely(err)) { 820db64fe02SNick Piggin kfree(vb); 821db64fe02SNick Piggin free_vmap_area(va); 822db64fe02SNick Piggin return ERR_PTR(err); 823db64fe02SNick Piggin } 824db64fe02SNick Piggin 825db64fe02SNick Piggin spin_lock_init(&vb->lock); 826db64fe02SNick Piggin vb->va = va; 827db64fe02SNick Piggin vb->free = VMAP_BBMAP_BITS; 828db64fe02SNick Piggin vb->dirty = 0; 829db64fe02SNick Piggin bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS); 830db64fe02SNick Piggin INIT_LIST_HEAD(&vb->free_list); 831db64fe02SNick Piggin 832db64fe02SNick Piggin vb_idx = addr_to_vb_idx(va->va_start); 833db64fe02SNick Piggin spin_lock(&vmap_block_tree_lock); 834db64fe02SNick Piggin err = radix_tree_insert(&vmap_block_tree, vb_idx, vb); 835db64fe02SNick Piggin spin_unlock(&vmap_block_tree_lock); 836db64fe02SNick Piggin BUG_ON(err); 837db64fe02SNick Piggin radix_tree_preload_end(); 838db64fe02SNick Piggin 839db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 840db64fe02SNick Piggin spin_lock(&vbq->lock); 841de560423SNick Piggin list_add_rcu(&vb->free_list, &vbq->free); 842db64fe02SNick Piggin spin_unlock(&vbq->lock); 8433f04ba85STejun Heo put_cpu_var(vmap_block_queue); 844db64fe02SNick Piggin 845db64fe02SNick Piggin return vb; 846db64fe02SNick Piggin } 847db64fe02SNick Piggin 848db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb) 849db64fe02SNick Piggin { 850db64fe02SNick Piggin struct vmap_block *tmp; 851db64fe02SNick Piggin unsigned long vb_idx; 852db64fe02SNick Piggin 853db64fe02SNick Piggin vb_idx = addr_to_vb_idx(vb->va->va_start); 854db64fe02SNick Piggin spin_lock(&vmap_block_tree_lock); 855db64fe02SNick Piggin tmp = radix_tree_delete(&vmap_block_tree, vb_idx); 856db64fe02SNick Piggin spin_unlock(&vmap_block_tree_lock); 857db64fe02SNick Piggin BUG_ON(tmp != vb); 858db64fe02SNick Piggin 85964141da5SJeremy Fitzhardinge free_vmap_area_noflush(vb->va); 86022a3c7d1SLai Jiangshan kfree_rcu(vb, rcu_head); 861db64fe02SNick Piggin } 862db64fe02SNick Piggin 86302b709dfSNick Piggin static void purge_fragmented_blocks(int cpu) 86402b709dfSNick Piggin { 86502b709dfSNick Piggin LIST_HEAD(purge); 86602b709dfSNick Piggin struct vmap_block *vb; 86702b709dfSNick Piggin struct vmap_block *n_vb; 86802b709dfSNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 86902b709dfSNick Piggin 87002b709dfSNick Piggin rcu_read_lock(); 87102b709dfSNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 87202b709dfSNick Piggin 87302b709dfSNick Piggin if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS)) 87402b709dfSNick Piggin continue; 87502b709dfSNick Piggin 87602b709dfSNick Piggin spin_lock(&vb->lock); 87702b709dfSNick Piggin if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) { 87802b709dfSNick Piggin vb->free = 0; /* prevent further allocs after releasing lock */ 87902b709dfSNick Piggin vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */ 88002b709dfSNick Piggin bitmap_fill(vb->dirty_map, VMAP_BBMAP_BITS); 88102b709dfSNick Piggin spin_lock(&vbq->lock); 88202b709dfSNick Piggin list_del_rcu(&vb->free_list); 88302b709dfSNick Piggin spin_unlock(&vbq->lock); 88402b709dfSNick Piggin spin_unlock(&vb->lock); 88502b709dfSNick Piggin list_add_tail(&vb->purge, &purge); 88602b709dfSNick Piggin } else 88702b709dfSNick Piggin spin_unlock(&vb->lock); 88802b709dfSNick Piggin } 88902b709dfSNick Piggin rcu_read_unlock(); 89002b709dfSNick Piggin 89102b709dfSNick Piggin list_for_each_entry_safe(vb, n_vb, &purge, purge) { 89202b709dfSNick Piggin list_del(&vb->purge); 89302b709dfSNick Piggin free_vmap_block(vb); 89402b709dfSNick Piggin } 89502b709dfSNick Piggin } 89602b709dfSNick Piggin 89702b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void) 89802b709dfSNick Piggin { 89902b709dfSNick Piggin int cpu; 90002b709dfSNick Piggin 90102b709dfSNick Piggin for_each_possible_cpu(cpu) 90202b709dfSNick Piggin purge_fragmented_blocks(cpu); 90302b709dfSNick Piggin } 90402b709dfSNick Piggin 905db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask) 906db64fe02SNick Piggin { 907db64fe02SNick Piggin struct vmap_block_queue *vbq; 908db64fe02SNick Piggin struct vmap_block *vb; 909db64fe02SNick Piggin unsigned long addr = 0; 910db64fe02SNick Piggin unsigned int order; 911db64fe02SNick Piggin 912db64fe02SNick Piggin BUG_ON(size & ~PAGE_MASK); 913db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 914aa91c4d8SJan Kara if (WARN_ON(size == 0)) { 915aa91c4d8SJan Kara /* 916aa91c4d8SJan Kara * Allocating 0 bytes isn't what caller wants since 917aa91c4d8SJan Kara * get_order(0) returns funny result. Just warn and terminate 918aa91c4d8SJan Kara * early. 919aa91c4d8SJan Kara */ 920aa91c4d8SJan Kara return NULL; 921aa91c4d8SJan Kara } 922db64fe02SNick Piggin order = get_order(size); 923db64fe02SNick Piggin 924db64fe02SNick Piggin again: 925db64fe02SNick Piggin rcu_read_lock(); 926db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 927db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 928db64fe02SNick Piggin int i; 929db64fe02SNick Piggin 930db64fe02SNick Piggin spin_lock(&vb->lock); 93102b709dfSNick Piggin if (vb->free < 1UL << order) 93202b709dfSNick Piggin goto next; 93302b709dfSNick Piggin 9343fcd76e8SZhang Yanfei i = VMAP_BBMAP_BITS - vb->free; 935db64fe02SNick Piggin addr = vb->va->va_start + (i << PAGE_SHIFT); 936db64fe02SNick Piggin BUG_ON(addr_to_vb_idx(addr) != 937db64fe02SNick Piggin addr_to_vb_idx(vb->va->va_start)); 938db64fe02SNick Piggin vb->free -= 1UL << order; 939db64fe02SNick Piggin if (vb->free == 0) { 940db64fe02SNick Piggin spin_lock(&vbq->lock); 941de560423SNick Piggin list_del_rcu(&vb->free_list); 942db64fe02SNick Piggin spin_unlock(&vbq->lock); 943db64fe02SNick Piggin } 944db64fe02SNick Piggin spin_unlock(&vb->lock); 945db64fe02SNick Piggin break; 94602b709dfSNick Piggin next: 947db64fe02SNick Piggin spin_unlock(&vb->lock); 948db64fe02SNick Piggin } 94902b709dfSNick Piggin 9503f04ba85STejun Heo put_cpu_var(vmap_block_queue); 951db64fe02SNick Piggin rcu_read_unlock(); 952db64fe02SNick Piggin 953db64fe02SNick Piggin if (!addr) { 954db64fe02SNick Piggin vb = new_vmap_block(gfp_mask); 955db64fe02SNick Piggin if (IS_ERR(vb)) 956db64fe02SNick Piggin return vb; 957db64fe02SNick Piggin goto again; 958db64fe02SNick Piggin } 959db64fe02SNick Piggin 960db64fe02SNick Piggin return (void *)addr; 961db64fe02SNick Piggin } 962db64fe02SNick Piggin 963db64fe02SNick Piggin static void vb_free(const void *addr, unsigned long size) 964db64fe02SNick Piggin { 965db64fe02SNick Piggin unsigned long offset; 966db64fe02SNick Piggin unsigned long vb_idx; 967db64fe02SNick Piggin unsigned int order; 968db64fe02SNick Piggin struct vmap_block *vb; 969db64fe02SNick Piggin 970db64fe02SNick Piggin BUG_ON(size & ~PAGE_MASK); 971db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 972b29acbdcSNick Piggin 973b29acbdcSNick Piggin flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size); 974b29acbdcSNick Piggin 975db64fe02SNick Piggin order = get_order(size); 976db64fe02SNick Piggin 977db64fe02SNick Piggin offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1); 978db64fe02SNick Piggin 979db64fe02SNick Piggin vb_idx = addr_to_vb_idx((unsigned long)addr); 980db64fe02SNick Piggin rcu_read_lock(); 981db64fe02SNick Piggin vb = radix_tree_lookup(&vmap_block_tree, vb_idx); 982db64fe02SNick Piggin rcu_read_unlock(); 983db64fe02SNick Piggin BUG_ON(!vb); 984db64fe02SNick Piggin 98564141da5SJeremy Fitzhardinge vunmap_page_range((unsigned long)addr, (unsigned long)addr + size); 98664141da5SJeremy Fitzhardinge 987db64fe02SNick Piggin spin_lock(&vb->lock); 988de560423SNick Piggin BUG_ON(bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order)); 989d086817dSMinChan Kim 990db64fe02SNick Piggin vb->dirty += 1UL << order; 991db64fe02SNick Piggin if (vb->dirty == VMAP_BBMAP_BITS) { 992de560423SNick Piggin BUG_ON(vb->free); 993db64fe02SNick Piggin spin_unlock(&vb->lock); 994db64fe02SNick Piggin free_vmap_block(vb); 995db64fe02SNick Piggin } else 996db64fe02SNick Piggin spin_unlock(&vb->lock); 997db64fe02SNick Piggin } 998db64fe02SNick Piggin 999db64fe02SNick Piggin /** 1000db64fe02SNick Piggin * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer 1001db64fe02SNick Piggin * 1002db64fe02SNick Piggin * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily 1003db64fe02SNick Piggin * to amortize TLB flushing overheads. What this means is that any page you 1004db64fe02SNick Piggin * have now, may, in a former life, have been mapped into kernel virtual 1005db64fe02SNick Piggin * address by the vmap layer and so there might be some CPUs with TLB entries 1006db64fe02SNick Piggin * still referencing that page (additional to the regular 1:1 kernel mapping). 1007db64fe02SNick Piggin * 1008db64fe02SNick Piggin * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can 1009db64fe02SNick Piggin * be sure that none of the pages we have control over will have any aliases 1010db64fe02SNick Piggin * from the vmap layer. 1011db64fe02SNick Piggin */ 1012db64fe02SNick Piggin void vm_unmap_aliases(void) 1013db64fe02SNick Piggin { 1014db64fe02SNick Piggin unsigned long start = ULONG_MAX, end = 0; 1015db64fe02SNick Piggin int cpu; 1016db64fe02SNick Piggin int flush = 0; 1017db64fe02SNick Piggin 10189b463334SJeremy Fitzhardinge if (unlikely(!vmap_initialized)) 10199b463334SJeremy Fitzhardinge return; 10209b463334SJeremy Fitzhardinge 1021db64fe02SNick Piggin for_each_possible_cpu(cpu) { 1022db64fe02SNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 1023db64fe02SNick Piggin struct vmap_block *vb; 1024db64fe02SNick Piggin 1025db64fe02SNick Piggin rcu_read_lock(); 1026db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 1027b136be5eSJoonsoo Kim int i, j; 1028db64fe02SNick Piggin 1029db64fe02SNick Piggin spin_lock(&vb->lock); 1030db64fe02SNick Piggin i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS); 1031b136be5eSJoonsoo Kim if (i < VMAP_BBMAP_BITS) { 1032db64fe02SNick Piggin unsigned long s, e; 1033b136be5eSJoonsoo Kim 1034b136be5eSJoonsoo Kim j = find_last_bit(vb->dirty_map, 1035b136be5eSJoonsoo Kim VMAP_BBMAP_BITS); 1036b136be5eSJoonsoo Kim j = j + 1; /* need exclusive index */ 1037db64fe02SNick Piggin 1038db64fe02SNick Piggin s = vb->va->va_start + (i << PAGE_SHIFT); 1039db64fe02SNick Piggin e = vb->va->va_start + (j << PAGE_SHIFT); 1040db64fe02SNick Piggin flush = 1; 1041db64fe02SNick Piggin 1042db64fe02SNick Piggin if (s < start) 1043db64fe02SNick Piggin start = s; 1044db64fe02SNick Piggin if (e > end) 1045db64fe02SNick Piggin end = e; 1046db64fe02SNick Piggin } 1047db64fe02SNick Piggin spin_unlock(&vb->lock); 1048db64fe02SNick Piggin } 1049db64fe02SNick Piggin rcu_read_unlock(); 1050db64fe02SNick Piggin } 1051db64fe02SNick Piggin 1052db64fe02SNick Piggin __purge_vmap_area_lazy(&start, &end, 1, flush); 1053db64fe02SNick Piggin } 1054db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases); 1055db64fe02SNick Piggin 1056db64fe02SNick Piggin /** 1057db64fe02SNick Piggin * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram 1058db64fe02SNick Piggin * @mem: the pointer returned by vm_map_ram 1059db64fe02SNick Piggin * @count: the count passed to that vm_map_ram call (cannot unmap partial) 1060db64fe02SNick Piggin */ 1061db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count) 1062db64fe02SNick Piggin { 1063db64fe02SNick Piggin unsigned long size = count << PAGE_SHIFT; 1064db64fe02SNick Piggin unsigned long addr = (unsigned long)mem; 1065db64fe02SNick Piggin 1066db64fe02SNick Piggin BUG_ON(!addr); 1067db64fe02SNick Piggin BUG_ON(addr < VMALLOC_START); 1068db64fe02SNick Piggin BUG_ON(addr > VMALLOC_END); 1069db64fe02SNick Piggin BUG_ON(addr & (PAGE_SIZE-1)); 1070db64fe02SNick Piggin 1071db64fe02SNick Piggin debug_check_no_locks_freed(mem, size); 1072cd52858cSNick Piggin vmap_debug_free_range(addr, addr+size); 1073db64fe02SNick Piggin 1074db64fe02SNick Piggin if (likely(count <= VMAP_MAX_ALLOC)) 1075db64fe02SNick Piggin vb_free(mem, size); 1076db64fe02SNick Piggin else 1077db64fe02SNick Piggin free_unmap_vmap_area_addr(addr); 1078db64fe02SNick Piggin } 1079db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram); 1080db64fe02SNick Piggin 1081db64fe02SNick Piggin /** 1082db64fe02SNick Piggin * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) 1083db64fe02SNick Piggin * @pages: an array of pointers to the pages to be mapped 1084db64fe02SNick Piggin * @count: number of pages 1085db64fe02SNick Piggin * @node: prefer to allocate data structures on this node 1086db64fe02SNick Piggin * @prot: memory protection to use. PAGE_KERNEL for regular RAM 1087e99c97adSRandy Dunlap * 108836437638SGioh Kim * If you use this function for less than VMAP_MAX_ALLOC pages, it could be 108936437638SGioh Kim * faster than vmap so it's good. But if you mix long-life and short-life 109036437638SGioh Kim * objects with vm_map_ram(), it could consume lots of address space through 109136437638SGioh Kim * fragmentation (especially on a 32bit machine). You could see failures in 109236437638SGioh Kim * the end. Please use this function for short-lived objects. 109336437638SGioh Kim * 1094e99c97adSRandy Dunlap * Returns: a pointer to the address that has been mapped, or %NULL on failure 1095db64fe02SNick Piggin */ 1096db64fe02SNick Piggin void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) 1097db64fe02SNick Piggin { 1098db64fe02SNick Piggin unsigned long size = count << PAGE_SHIFT; 1099db64fe02SNick Piggin unsigned long addr; 1100db64fe02SNick Piggin void *mem; 1101db64fe02SNick Piggin 1102db64fe02SNick Piggin if (likely(count <= VMAP_MAX_ALLOC)) { 1103db64fe02SNick Piggin mem = vb_alloc(size, GFP_KERNEL); 1104db64fe02SNick Piggin if (IS_ERR(mem)) 1105db64fe02SNick Piggin return NULL; 1106db64fe02SNick Piggin addr = (unsigned long)mem; 1107db64fe02SNick Piggin } else { 1108db64fe02SNick Piggin struct vmap_area *va; 1109db64fe02SNick Piggin va = alloc_vmap_area(size, PAGE_SIZE, 1110db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); 1111db64fe02SNick Piggin if (IS_ERR(va)) 1112db64fe02SNick Piggin return NULL; 1113db64fe02SNick Piggin 1114db64fe02SNick Piggin addr = va->va_start; 1115db64fe02SNick Piggin mem = (void *)addr; 1116db64fe02SNick Piggin } 1117db64fe02SNick Piggin if (vmap_page_range(addr, addr + size, prot, pages) < 0) { 1118db64fe02SNick Piggin vm_unmap_ram(mem, count); 1119db64fe02SNick Piggin return NULL; 1120db64fe02SNick Piggin } 1121db64fe02SNick Piggin return mem; 1122db64fe02SNick Piggin } 1123db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram); 1124db64fe02SNick Piggin 11254341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata; 1126f0aa6617STejun Heo /** 1127be9b7335SNicolas Pitre * vm_area_add_early - add vmap area early during boot 1128be9b7335SNicolas Pitre * @vm: vm_struct to add 1129be9b7335SNicolas Pitre * 1130be9b7335SNicolas Pitre * This function is used to add fixed kernel vm area to vmlist before 1131be9b7335SNicolas Pitre * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags 1132be9b7335SNicolas Pitre * should contain proper values and the other fields should be zero. 1133be9b7335SNicolas Pitre * 1134be9b7335SNicolas Pitre * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 1135be9b7335SNicolas Pitre */ 1136be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm) 1137be9b7335SNicolas Pitre { 1138be9b7335SNicolas Pitre struct vm_struct *tmp, **p; 1139be9b7335SNicolas Pitre 1140be9b7335SNicolas Pitre BUG_ON(vmap_initialized); 1141be9b7335SNicolas Pitre for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { 1142be9b7335SNicolas Pitre if (tmp->addr >= vm->addr) { 1143be9b7335SNicolas Pitre BUG_ON(tmp->addr < vm->addr + vm->size); 1144be9b7335SNicolas Pitre break; 1145be9b7335SNicolas Pitre } else 1146be9b7335SNicolas Pitre BUG_ON(tmp->addr + tmp->size > vm->addr); 1147be9b7335SNicolas Pitre } 1148be9b7335SNicolas Pitre vm->next = *p; 1149be9b7335SNicolas Pitre *p = vm; 1150be9b7335SNicolas Pitre } 1151be9b7335SNicolas Pitre 1152be9b7335SNicolas Pitre /** 1153f0aa6617STejun Heo * vm_area_register_early - register vmap area early during boot 1154f0aa6617STejun Heo * @vm: vm_struct to register 1155c0c0a293STejun Heo * @align: requested alignment 1156f0aa6617STejun Heo * 1157f0aa6617STejun Heo * This function is used to register kernel vm area before 1158f0aa6617STejun Heo * vmalloc_init() is called. @vm->size and @vm->flags should contain 1159f0aa6617STejun Heo * proper values on entry and other fields should be zero. On return, 1160f0aa6617STejun Heo * vm->addr contains the allocated address. 1161f0aa6617STejun Heo * 1162f0aa6617STejun Heo * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 1163f0aa6617STejun Heo */ 1164c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align) 1165f0aa6617STejun Heo { 1166f0aa6617STejun Heo static size_t vm_init_off __initdata; 1167c0c0a293STejun Heo unsigned long addr; 1168f0aa6617STejun Heo 1169c0c0a293STejun Heo addr = ALIGN(VMALLOC_START + vm_init_off, align); 1170c0c0a293STejun Heo vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START; 1171c0c0a293STejun Heo 1172c0c0a293STejun Heo vm->addr = (void *)addr; 1173f0aa6617STejun Heo 1174be9b7335SNicolas Pitre vm_area_add_early(vm); 1175f0aa6617STejun Heo } 1176f0aa6617STejun Heo 1177db64fe02SNick Piggin void __init vmalloc_init(void) 1178db64fe02SNick Piggin { 1179822c18f2SIvan Kokshaysky struct vmap_area *va; 1180822c18f2SIvan Kokshaysky struct vm_struct *tmp; 1181db64fe02SNick Piggin int i; 1182db64fe02SNick Piggin 1183db64fe02SNick Piggin for_each_possible_cpu(i) { 1184db64fe02SNick Piggin struct vmap_block_queue *vbq; 118532fcfd40SAl Viro struct vfree_deferred *p; 1186db64fe02SNick Piggin 1187db64fe02SNick Piggin vbq = &per_cpu(vmap_block_queue, i); 1188db64fe02SNick Piggin spin_lock_init(&vbq->lock); 1189db64fe02SNick Piggin INIT_LIST_HEAD(&vbq->free); 119032fcfd40SAl Viro p = &per_cpu(vfree_deferred, i); 119132fcfd40SAl Viro init_llist_head(&p->list); 119232fcfd40SAl Viro INIT_WORK(&p->wq, free_work); 1193db64fe02SNick Piggin } 11949b463334SJeremy Fitzhardinge 1195822c18f2SIvan Kokshaysky /* Import existing vmlist entries. */ 1196822c18f2SIvan Kokshaysky for (tmp = vmlist; tmp; tmp = tmp->next) { 119743ebdac4SPekka Enberg va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT); 1198dbda591dSKyongHo va->flags = VM_VM_AREA; 1199822c18f2SIvan Kokshaysky va->va_start = (unsigned long)tmp->addr; 1200822c18f2SIvan Kokshaysky va->va_end = va->va_start + tmp->size; 1201dbda591dSKyongHo va->vm = tmp; 1202822c18f2SIvan Kokshaysky __insert_vmap_area(va); 1203822c18f2SIvan Kokshaysky } 1204ca23e405STejun Heo 1205ca23e405STejun Heo vmap_area_pcpu_hole = VMALLOC_END; 1206ca23e405STejun Heo 12079b463334SJeremy Fitzhardinge vmap_initialized = true; 1208db64fe02SNick Piggin } 1209db64fe02SNick Piggin 12108fc48985STejun Heo /** 12118fc48985STejun Heo * map_kernel_range_noflush - map kernel VM area with the specified pages 12128fc48985STejun Heo * @addr: start of the VM area to map 12138fc48985STejun Heo * @size: size of the VM area to map 12148fc48985STejun Heo * @prot: page protection flags to use 12158fc48985STejun Heo * @pages: pages to map 12168fc48985STejun Heo * 12178fc48985STejun Heo * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size 12188fc48985STejun Heo * specify should have been allocated using get_vm_area() and its 12198fc48985STejun Heo * friends. 12208fc48985STejun Heo * 12218fc48985STejun Heo * NOTE: 12228fc48985STejun Heo * This function does NOT do any cache flushing. The caller is 12238fc48985STejun Heo * responsible for calling flush_cache_vmap() on to-be-mapped areas 12248fc48985STejun Heo * before calling this function. 12258fc48985STejun Heo * 12268fc48985STejun Heo * RETURNS: 12278fc48985STejun Heo * The number of pages mapped on success, -errno on failure. 12288fc48985STejun Heo */ 12298fc48985STejun Heo int map_kernel_range_noflush(unsigned long addr, unsigned long size, 12308fc48985STejun Heo pgprot_t prot, struct page **pages) 12318fc48985STejun Heo { 12328fc48985STejun Heo return vmap_page_range_noflush(addr, addr + size, prot, pages); 12338fc48985STejun Heo } 12348fc48985STejun Heo 12358fc48985STejun Heo /** 12368fc48985STejun Heo * unmap_kernel_range_noflush - unmap kernel VM area 12378fc48985STejun Heo * @addr: start of the VM area to unmap 12388fc48985STejun Heo * @size: size of the VM area to unmap 12398fc48985STejun Heo * 12408fc48985STejun Heo * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size 12418fc48985STejun Heo * specify should have been allocated using get_vm_area() and its 12428fc48985STejun Heo * friends. 12438fc48985STejun Heo * 12448fc48985STejun Heo * NOTE: 12458fc48985STejun Heo * This function does NOT do any cache flushing. The caller is 12468fc48985STejun Heo * responsible for calling flush_cache_vunmap() on to-be-mapped areas 12478fc48985STejun Heo * before calling this function and flush_tlb_kernel_range() after. 12488fc48985STejun Heo */ 12498fc48985STejun Heo void unmap_kernel_range_noflush(unsigned long addr, unsigned long size) 12508fc48985STejun Heo { 12518fc48985STejun Heo vunmap_page_range(addr, addr + size); 12528fc48985STejun Heo } 125381e88fdcSHuang Ying EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush); 12548fc48985STejun Heo 12558fc48985STejun Heo /** 12568fc48985STejun Heo * unmap_kernel_range - unmap kernel VM area and flush cache and TLB 12578fc48985STejun Heo * @addr: start of the VM area to unmap 12588fc48985STejun Heo * @size: size of the VM area to unmap 12598fc48985STejun Heo * 12608fc48985STejun Heo * Similar to unmap_kernel_range_noflush() but flushes vcache before 12618fc48985STejun Heo * the unmapping and tlb after. 12628fc48985STejun Heo */ 1263db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size) 1264db64fe02SNick Piggin { 1265db64fe02SNick Piggin unsigned long end = addr + size; 1266f6fcba70STejun Heo 1267f6fcba70STejun Heo flush_cache_vunmap(addr, end); 1268db64fe02SNick Piggin vunmap_page_range(addr, end); 1269db64fe02SNick Piggin flush_tlb_kernel_range(addr, end); 1270db64fe02SNick Piggin } 127193ef6d6cSMinchan Kim EXPORT_SYMBOL_GPL(unmap_kernel_range); 1272db64fe02SNick Piggin 1273f6f8ed47SWANG Chao int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages) 1274db64fe02SNick Piggin { 1275db64fe02SNick Piggin unsigned long addr = (unsigned long)area->addr; 1276762216abSWanpeng Li unsigned long end = addr + get_vm_area_size(area); 1277db64fe02SNick Piggin int err; 1278db64fe02SNick Piggin 1279f6f8ed47SWANG Chao err = vmap_page_range(addr, end, prot, pages); 1280db64fe02SNick Piggin 1281f6f8ed47SWANG Chao return err > 0 ? 0 : err; 1282db64fe02SNick Piggin } 1283db64fe02SNick Piggin EXPORT_SYMBOL_GPL(map_vm_area); 1284db64fe02SNick Piggin 1285f5252e00SMitsuo Hayasaka static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, 12865e6cafc8SMarek Szyprowski unsigned long flags, const void *caller) 1287cf88c790STejun Heo { 1288c69480adSJoonsoo Kim spin_lock(&vmap_area_lock); 1289cf88c790STejun Heo vm->flags = flags; 1290cf88c790STejun Heo vm->addr = (void *)va->va_start; 1291cf88c790STejun Heo vm->size = va->va_end - va->va_start; 1292cf88c790STejun Heo vm->caller = caller; 1293db1aecafSMinchan Kim va->vm = vm; 1294cf88c790STejun Heo va->flags |= VM_VM_AREA; 1295c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 1296f5252e00SMitsuo Hayasaka } 1297cf88c790STejun Heo 129820fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm) 1299f5252e00SMitsuo Hayasaka { 1300d4033afdSJoonsoo Kim /* 130120fc02b4SZhang Yanfei * Before removing VM_UNINITIALIZED, 1302d4033afdSJoonsoo Kim * we should make sure that vm has proper values. 1303d4033afdSJoonsoo Kim * Pair with smp_rmb() in show_numa_info(). 1304d4033afdSJoonsoo Kim */ 1305d4033afdSJoonsoo Kim smp_wmb(); 130620fc02b4SZhang Yanfei vm->flags &= ~VM_UNINITIALIZED; 1307cf88c790STejun Heo } 1308cf88c790STejun Heo 1309db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size, 13102dca6999SDavid Miller unsigned long align, unsigned long flags, unsigned long start, 13115e6cafc8SMarek Szyprowski unsigned long end, int node, gfp_t gfp_mask, const void *caller) 1312db64fe02SNick Piggin { 13130006526dSKautuk Consul struct vmap_area *va; 1314db64fe02SNick Piggin struct vm_struct *area; 13151da177e4SLinus Torvalds 131652fd24caSGiridhar Pemmasani BUG_ON(in_interrupt()); 13170f2d4a8eSZhang Yanfei if (flags & VM_IOREMAP) 13180f2d4a8eSZhang Yanfei align = 1ul << clamp(fls(size), PAGE_SHIFT, IOREMAP_MAX_ORDER); 1319db64fe02SNick Piggin 13201da177e4SLinus Torvalds size = PAGE_ALIGN(size); 132131be8309SOGAWA Hirofumi if (unlikely(!size)) 132231be8309SOGAWA Hirofumi return NULL; 13231da177e4SLinus Torvalds 1324cf88c790STejun Heo area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); 13251da177e4SLinus Torvalds if (unlikely(!area)) 13261da177e4SLinus Torvalds return NULL; 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds /* 13291da177e4SLinus Torvalds * We always allocate a guard page. 13301da177e4SLinus Torvalds */ 13311da177e4SLinus Torvalds size += PAGE_SIZE; 13321da177e4SLinus Torvalds 1333db64fe02SNick Piggin va = alloc_vmap_area(size, align, start, end, node, gfp_mask); 1334db64fe02SNick Piggin if (IS_ERR(va)) { 1335db64fe02SNick Piggin kfree(area); 1336db64fe02SNick Piggin return NULL; 13371da177e4SLinus Torvalds } 13381da177e4SLinus Torvalds 1339f5252e00SMitsuo Hayasaka setup_vmalloc_vm(area, va, flags, caller); 1340f5252e00SMitsuo Hayasaka 13411da177e4SLinus Torvalds return area; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 1344930fc45aSChristoph Lameter struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags, 1345930fc45aSChristoph Lameter unsigned long start, unsigned long end) 1346930fc45aSChristoph Lameter { 134700ef2d2fSDavid Rientjes return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE, 134800ef2d2fSDavid Rientjes GFP_KERNEL, __builtin_return_address(0)); 1349930fc45aSChristoph Lameter } 13505992b6daSRusty Russell EXPORT_SYMBOL_GPL(__get_vm_area); 1351930fc45aSChristoph Lameter 1352c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, 1353c2968612SBenjamin Herrenschmidt unsigned long start, unsigned long end, 13545e6cafc8SMarek Szyprowski const void *caller) 1355c2968612SBenjamin Herrenschmidt { 135600ef2d2fSDavid Rientjes return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE, 135700ef2d2fSDavid Rientjes GFP_KERNEL, caller); 1358c2968612SBenjamin Herrenschmidt } 1359c2968612SBenjamin Herrenschmidt 13601da177e4SLinus Torvalds /** 1361183ff22bSSimon Arlott * get_vm_area - reserve a contiguous kernel virtual area 13621da177e4SLinus Torvalds * @size: size of the area 13631da177e4SLinus Torvalds * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC 13641da177e4SLinus Torvalds * 13651da177e4SLinus Torvalds * Search an area of @size in the kernel virtual mapping area, 13661da177e4SLinus Torvalds * and reserved it for out purposes. Returns the area descriptor 13671da177e4SLinus Torvalds * on success or %NULL on failure. 13681da177e4SLinus Torvalds */ 13691da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) 13701da177e4SLinus Torvalds { 13712dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 137200ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, 137300ef2d2fSDavid Rientjes __builtin_return_address(0)); 137423016969SChristoph Lameter } 137523016969SChristoph Lameter 137623016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, 13775e6cafc8SMarek Szyprowski const void *caller) 137823016969SChristoph Lameter { 13792dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 138000ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, caller); 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds 1383e9da6e99SMarek Szyprowski /** 1384e9da6e99SMarek Szyprowski * find_vm_area - find a continuous kernel virtual area 1385e9da6e99SMarek Szyprowski * @addr: base address 1386e9da6e99SMarek Szyprowski * 1387e9da6e99SMarek Szyprowski * Search for the kernel VM area starting at @addr, and return it. 1388e9da6e99SMarek Szyprowski * It is up to the caller to do all required locking to keep the returned 1389e9da6e99SMarek Szyprowski * pointer valid. 1390e9da6e99SMarek Szyprowski */ 1391e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr) 139283342314SNick Piggin { 1393db64fe02SNick Piggin struct vmap_area *va; 139483342314SNick Piggin 1395db64fe02SNick Piggin va = find_vmap_area((unsigned long)addr); 1396db64fe02SNick Piggin if (va && va->flags & VM_VM_AREA) 1397db1aecafSMinchan Kim return va->vm; 139883342314SNick Piggin 13997856dfebSAndi Kleen return NULL; 14007856dfebSAndi Kleen } 14017856dfebSAndi Kleen 14021da177e4SLinus Torvalds /** 1403183ff22bSSimon Arlott * remove_vm_area - find and remove a continuous kernel virtual area 14041da177e4SLinus Torvalds * @addr: base address 14051da177e4SLinus Torvalds * 14061da177e4SLinus Torvalds * Search for the kernel VM area starting at @addr, and remove it. 14071da177e4SLinus Torvalds * This function returns the found VM area, but using it is NOT safe 14087856dfebSAndi Kleen * on SMP machines, except for its size or flags. 14091da177e4SLinus Torvalds */ 1410b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr) 14111da177e4SLinus Torvalds { 1412db64fe02SNick Piggin struct vmap_area *va; 1413db64fe02SNick Piggin 1414db64fe02SNick Piggin va = find_vmap_area((unsigned long)addr); 1415db64fe02SNick Piggin if (va && va->flags & VM_VM_AREA) { 1416db1aecafSMinchan Kim struct vm_struct *vm = va->vm; 1417f5252e00SMitsuo Hayasaka 1418c69480adSJoonsoo Kim spin_lock(&vmap_area_lock); 1419c69480adSJoonsoo Kim va->vm = NULL; 1420c69480adSJoonsoo Kim va->flags &= ~VM_VM_AREA; 1421c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 1422c69480adSJoonsoo Kim 1423dd32c279SKAMEZAWA Hiroyuki vmap_debug_free_range(va->va_start, va->va_end); 1424dd32c279SKAMEZAWA Hiroyuki free_unmap_vmap_area(va); 1425dd32c279SKAMEZAWA Hiroyuki vm->size -= PAGE_SIZE; 1426dd32c279SKAMEZAWA Hiroyuki 1427db64fe02SNick Piggin return vm; 1428db64fe02SNick Piggin } 1429db64fe02SNick Piggin return NULL; 14301da177e4SLinus Torvalds } 14311da177e4SLinus Torvalds 1432b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages) 14331da177e4SLinus Torvalds { 14341da177e4SLinus Torvalds struct vm_struct *area; 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds if (!addr) 14371da177e4SLinus Torvalds return; 14381da177e4SLinus Torvalds 1439e69e9d4aSHATAYAMA Daisuke if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n", 1440ab15d9b4SDan Carpenter addr)) 14411da177e4SLinus Torvalds return; 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds area = remove_vm_area(addr); 14441da177e4SLinus Torvalds if (unlikely(!area)) { 14454c8573e2SArjan van de Ven WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", 14461da177e4SLinus Torvalds addr); 14471da177e4SLinus Torvalds return; 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 14509a11b49aSIngo Molnar debug_check_no_locks_freed(addr, area->size); 14513ac7fe5aSThomas Gleixner debug_check_no_obj_freed(addr, area->size); 14529a11b49aSIngo Molnar 14531da177e4SLinus Torvalds if (deallocate_pages) { 14541da177e4SLinus Torvalds int i; 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds for (i = 0; i < area->nr_pages; i++) { 1457bf53d6f8SChristoph Lameter struct page *page = area->pages[i]; 1458bf53d6f8SChristoph Lameter 1459bf53d6f8SChristoph Lameter BUG_ON(!page); 1460bf53d6f8SChristoph Lameter __free_page(page); 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds 14638757d5faSJan Kiszka if (area->flags & VM_VPAGES) 14641da177e4SLinus Torvalds vfree(area->pages); 14651da177e4SLinus Torvalds else 14661da177e4SLinus Torvalds kfree(area->pages); 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds kfree(area); 14701da177e4SLinus Torvalds return; 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds /** 14741da177e4SLinus Torvalds * vfree - release memory allocated by vmalloc() 14751da177e4SLinus Torvalds * @addr: memory base address 14761da177e4SLinus Torvalds * 1477183ff22bSSimon Arlott * Free the virtually continuous memory area starting at @addr, as 147880e93effSPekka Enberg * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is 147980e93effSPekka Enberg * NULL, no operation is performed. 14801da177e4SLinus Torvalds * 148132fcfd40SAl Viro * Must not be called in NMI context (strictly speaking, only if we don't 148232fcfd40SAl Viro * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling 148332fcfd40SAl Viro * conventions for vfree() arch-depenedent would be a really bad idea) 148432fcfd40SAl Viro * 1485c9fcee51SAndrew Morton * NOTE: assumes that the object at *addr has a size >= sizeof(llist_node) 14861da177e4SLinus Torvalds */ 1487b3bdda02SChristoph Lameter void vfree(const void *addr) 14881da177e4SLinus Torvalds { 148932fcfd40SAl Viro BUG_ON(in_nmi()); 149089219d37SCatalin Marinas 149189219d37SCatalin Marinas kmemleak_free(addr); 149289219d37SCatalin Marinas 149332fcfd40SAl Viro if (!addr) 149432fcfd40SAl Viro return; 149532fcfd40SAl Viro if (unlikely(in_interrupt())) { 14967c8e0181SChristoph Lameter struct vfree_deferred *p = this_cpu_ptr(&vfree_deferred); 149759d3132fSOleg Nesterov if (llist_add((struct llist_node *)addr, &p->list)) 149832fcfd40SAl Viro schedule_work(&p->wq); 149932fcfd40SAl Viro } else 15001da177e4SLinus Torvalds __vunmap(addr, 1); 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds EXPORT_SYMBOL(vfree); 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds /** 15051da177e4SLinus Torvalds * vunmap - release virtual mapping obtained by vmap() 15061da177e4SLinus Torvalds * @addr: memory base address 15071da177e4SLinus Torvalds * 15081da177e4SLinus Torvalds * Free the virtually contiguous memory area starting at @addr, 15091da177e4SLinus Torvalds * which was created from the page array passed to vmap(). 15101da177e4SLinus Torvalds * 151180e93effSPekka Enberg * Must not be called in interrupt context. 15121da177e4SLinus Torvalds */ 1513b3bdda02SChristoph Lameter void vunmap(const void *addr) 15141da177e4SLinus Torvalds { 15151da177e4SLinus Torvalds BUG_ON(in_interrupt()); 151634754b69SPeter Zijlstra might_sleep(); 151732fcfd40SAl Viro if (addr) 15181da177e4SLinus Torvalds __vunmap(addr, 0); 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap); 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds /** 15231da177e4SLinus Torvalds * vmap - map an array of pages into virtually contiguous space 15241da177e4SLinus Torvalds * @pages: array of page pointers 15251da177e4SLinus Torvalds * @count: number of pages to map 15261da177e4SLinus Torvalds * @flags: vm_area->flags 15271da177e4SLinus Torvalds * @prot: page protection for the mapping 15281da177e4SLinus Torvalds * 15291da177e4SLinus Torvalds * Maps @count pages from @pages into contiguous kernel virtual 15301da177e4SLinus Torvalds * space. 15311da177e4SLinus Torvalds */ 15321da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, 15331da177e4SLinus Torvalds unsigned long flags, pgprot_t prot) 15341da177e4SLinus Torvalds { 15351da177e4SLinus Torvalds struct vm_struct *area; 15361da177e4SLinus Torvalds 153734754b69SPeter Zijlstra might_sleep(); 153834754b69SPeter Zijlstra 15394481374cSJan Beulich if (count > totalram_pages) 15401da177e4SLinus Torvalds return NULL; 15411da177e4SLinus Torvalds 154223016969SChristoph Lameter area = get_vm_area_caller((count << PAGE_SHIFT), flags, 154323016969SChristoph Lameter __builtin_return_address(0)); 15441da177e4SLinus Torvalds if (!area) 15451da177e4SLinus Torvalds return NULL; 154623016969SChristoph Lameter 1547f6f8ed47SWANG Chao if (map_vm_area(area, prot, pages)) { 15481da177e4SLinus Torvalds vunmap(area->addr); 15491da177e4SLinus Torvalds return NULL; 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds return area->addr; 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds EXPORT_SYMBOL(vmap); 15551da177e4SLinus Torvalds 15562dca6999SDavid Miller static void *__vmalloc_node(unsigned long size, unsigned long align, 15572dca6999SDavid Miller gfp_t gfp_mask, pgprot_t prot, 15585e6cafc8SMarek Szyprowski int node, const void *caller); 1559e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, 15603722e13cSWanpeng Li pgprot_t prot, int node) 15611da177e4SLinus Torvalds { 156222943ab1SDave Hansen const int order = 0; 15631da177e4SLinus Torvalds struct page **pages; 15641da177e4SLinus Torvalds unsigned int nr_pages, array_size, i; 1565930f036bSDavid Rientjes const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; 1566930f036bSDavid Rientjes const gfp_t alloc_mask = gfp_mask | __GFP_NOWARN; 15671da177e4SLinus Torvalds 1568762216abSWanpeng Li nr_pages = get_vm_area_size(area) >> PAGE_SHIFT; 15691da177e4SLinus Torvalds array_size = (nr_pages * sizeof(struct page *)); 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds area->nr_pages = nr_pages; 15721da177e4SLinus Torvalds /* Please note that the recursion is strictly bounded. */ 15738757d5faSJan Kiszka if (array_size > PAGE_SIZE) { 1574976d6dfbSJan Beulich pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM, 15753722e13cSWanpeng Li PAGE_KERNEL, node, area->caller); 15768757d5faSJan Kiszka area->flags |= VM_VPAGES; 1577286e1ea3SAndrew Morton } else { 1578976d6dfbSJan Beulich pages = kmalloc_node(array_size, nested_gfp, node); 1579286e1ea3SAndrew Morton } 15801da177e4SLinus Torvalds area->pages = pages; 15811da177e4SLinus Torvalds if (!area->pages) { 15821da177e4SLinus Torvalds remove_vm_area(area->addr); 15831da177e4SLinus Torvalds kfree(area); 15841da177e4SLinus Torvalds return NULL; 15851da177e4SLinus Torvalds } 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds for (i = 0; i < area->nr_pages; i++) { 1588bf53d6f8SChristoph Lameter struct page *page; 1589bf53d6f8SChristoph Lameter 15904b90951cSJianguo Wu if (node == NUMA_NO_NODE) 1591930f036bSDavid Rientjes page = alloc_page(alloc_mask); 1592930fc45aSChristoph Lameter else 1593930f036bSDavid Rientjes page = alloc_pages_node(node, alloc_mask, order); 1594bf53d6f8SChristoph Lameter 1595bf53d6f8SChristoph Lameter if (unlikely(!page)) { 15961da177e4SLinus Torvalds /* Successfully allocated i pages, free them in __vunmap() */ 15971da177e4SLinus Torvalds area->nr_pages = i; 15981da177e4SLinus Torvalds goto fail; 15991da177e4SLinus Torvalds } 1600bf53d6f8SChristoph Lameter area->pages[i] = page; 1601660654f9SEric Dumazet if (gfp_mask & __GFP_WAIT) 1602660654f9SEric Dumazet cond_resched(); 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds 1605f6f8ed47SWANG Chao if (map_vm_area(area, prot, pages)) 16061da177e4SLinus Torvalds goto fail; 16071da177e4SLinus Torvalds return area->addr; 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds fail: 16103ee9a4f0SJoe Perches warn_alloc_failed(gfp_mask, order, 16113ee9a4f0SJoe Perches "vmalloc: allocation failure, allocated %ld of %ld bytes\n", 161222943ab1SDave Hansen (area->nr_pages*PAGE_SIZE), area->size); 16131da177e4SLinus Torvalds vfree(area->addr); 16141da177e4SLinus Torvalds return NULL; 16151da177e4SLinus Torvalds } 16161da177e4SLinus Torvalds 1617d0a21265SDavid Rientjes /** 1618d0a21265SDavid Rientjes * __vmalloc_node_range - allocate virtually contiguous memory 1619d0a21265SDavid Rientjes * @size: allocation size 1620d0a21265SDavid Rientjes * @align: desired alignment 1621d0a21265SDavid Rientjes * @start: vm area range start 1622d0a21265SDavid Rientjes * @end: vm area range end 1623d0a21265SDavid Rientjes * @gfp_mask: flags for the page level allocator 1624d0a21265SDavid Rientjes * @prot: protection mask for the allocated pages 162500ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 1626d0a21265SDavid Rientjes * @caller: caller's return address 1627d0a21265SDavid Rientjes * 1628d0a21265SDavid Rientjes * Allocate enough pages to cover @size from the page level 1629d0a21265SDavid Rientjes * allocator with @gfp_mask flags. Map them into contiguous 1630d0a21265SDavid Rientjes * kernel virtual space, using a pagetable protection of @prot. 1631d0a21265SDavid Rientjes */ 1632d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align, 1633d0a21265SDavid Rientjes unsigned long start, unsigned long end, gfp_t gfp_mask, 16345e6cafc8SMarek Szyprowski pgprot_t prot, int node, const void *caller) 1635930fc45aSChristoph Lameter { 1636d0a21265SDavid Rientjes struct vm_struct *area; 1637d0a21265SDavid Rientjes void *addr; 1638d0a21265SDavid Rientjes unsigned long real_size = size; 1639d0a21265SDavid Rientjes 1640d0a21265SDavid Rientjes size = PAGE_ALIGN(size); 1641d0a21265SDavid Rientjes if (!size || (size >> PAGE_SHIFT) > totalram_pages) 1642de7d2b56SJoe Perches goto fail; 1643d0a21265SDavid Rientjes 164420fc02b4SZhang Yanfei area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED, 1645f5252e00SMitsuo Hayasaka start, end, node, gfp_mask, caller); 1646d0a21265SDavid Rientjes if (!area) 1647de7d2b56SJoe Perches goto fail; 1648d0a21265SDavid Rientjes 16493722e13cSWanpeng Li addr = __vmalloc_area_node(area, gfp_mask, prot, node); 16501368edf0SMel Gorman if (!addr) 1651b82225f3SWanpeng Li return NULL; 165289219d37SCatalin Marinas 165389219d37SCatalin Marinas /* 165420fc02b4SZhang Yanfei * In this function, newly allocated vm_struct has VM_UNINITIALIZED 165520fc02b4SZhang Yanfei * flag. It means that vm_struct is not fully initialized. 16564341fa45SJoonsoo Kim * Now, it is fully initialized, so remove this flag here. 1657f5252e00SMitsuo Hayasaka */ 165820fc02b4SZhang Yanfei clear_vm_uninitialized_flag(area); 1659f5252e00SMitsuo Hayasaka 1660f5252e00SMitsuo Hayasaka /* 16617f88f88fSCatalin Marinas * A ref_count = 2 is needed because vm_struct allocated in 16627f88f88fSCatalin Marinas * __get_vm_area_node() contains a reference to the virtual address of 16637f88f88fSCatalin Marinas * the vmalloc'ed block. 166489219d37SCatalin Marinas */ 16657f88f88fSCatalin Marinas kmemleak_alloc(addr, real_size, 2, gfp_mask); 166689219d37SCatalin Marinas 166789219d37SCatalin Marinas return addr; 1668de7d2b56SJoe Perches 1669de7d2b56SJoe Perches fail: 1670de7d2b56SJoe Perches warn_alloc_failed(gfp_mask, 0, 1671de7d2b56SJoe Perches "vmalloc: allocation failure: %lu bytes\n", 1672de7d2b56SJoe Perches real_size); 1673de7d2b56SJoe Perches return NULL; 1674930fc45aSChristoph Lameter } 1675930fc45aSChristoph Lameter 16761da177e4SLinus Torvalds /** 1677930fc45aSChristoph Lameter * __vmalloc_node - allocate virtually contiguous memory 16781da177e4SLinus Torvalds * @size: allocation size 16792dca6999SDavid Miller * @align: desired alignment 16801da177e4SLinus Torvalds * @gfp_mask: flags for the page level allocator 16811da177e4SLinus Torvalds * @prot: protection mask for the allocated pages 168200ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 1683c85d194bSRandy Dunlap * @caller: caller's return address 16841da177e4SLinus Torvalds * 16851da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 16861da177e4SLinus Torvalds * allocator with @gfp_mask flags. Map them into contiguous 16871da177e4SLinus Torvalds * kernel virtual space, using a pagetable protection of @prot. 16881da177e4SLinus Torvalds */ 16892dca6999SDavid Miller static void *__vmalloc_node(unsigned long size, unsigned long align, 16902dca6999SDavid Miller gfp_t gfp_mask, pgprot_t prot, 16915e6cafc8SMarek Szyprowski int node, const void *caller) 16921da177e4SLinus Torvalds { 1693d0a21265SDavid Rientjes return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, 1694d0a21265SDavid Rientjes gfp_mask, prot, node, caller); 16951da177e4SLinus Torvalds } 16961da177e4SLinus Torvalds 1697930fc45aSChristoph Lameter void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) 1698930fc45aSChristoph Lameter { 169900ef2d2fSDavid Rientjes return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE, 170023016969SChristoph Lameter __builtin_return_address(0)); 1701930fc45aSChristoph Lameter } 17021da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc); 17031da177e4SLinus Torvalds 1704e1ca7788SDave Young static inline void *__vmalloc_node_flags(unsigned long size, 1705e1ca7788SDave Young int node, gfp_t flags) 1706e1ca7788SDave Young { 1707e1ca7788SDave Young return __vmalloc_node(size, 1, flags, PAGE_KERNEL, 1708e1ca7788SDave Young node, __builtin_return_address(0)); 1709e1ca7788SDave Young } 1710e1ca7788SDave Young 17111da177e4SLinus Torvalds /** 17121da177e4SLinus Torvalds * vmalloc - allocate virtually contiguous memory 17131da177e4SLinus Torvalds * @size: allocation size 17141da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 17151da177e4SLinus Torvalds * allocator and map them into contiguous kernel virtual space. 17161da177e4SLinus Torvalds * 1717c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 17181da177e4SLinus Torvalds * use __vmalloc() instead. 17191da177e4SLinus Torvalds */ 17201da177e4SLinus Torvalds void *vmalloc(unsigned long size) 17211da177e4SLinus Torvalds { 172200ef2d2fSDavid Rientjes return __vmalloc_node_flags(size, NUMA_NO_NODE, 172300ef2d2fSDavid Rientjes GFP_KERNEL | __GFP_HIGHMEM); 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc); 17261da177e4SLinus Torvalds 1727930fc45aSChristoph Lameter /** 1728e1ca7788SDave Young * vzalloc - allocate virtually contiguous memory with zero fill 1729e1ca7788SDave Young * @size: allocation size 1730e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 1731e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 1732e1ca7788SDave Young * The memory allocated is set to zero. 1733e1ca7788SDave Young * 1734e1ca7788SDave Young * For tight control over page level allocator and protection flags 1735e1ca7788SDave Young * use __vmalloc() instead. 1736e1ca7788SDave Young */ 1737e1ca7788SDave Young void *vzalloc(unsigned long size) 1738e1ca7788SDave Young { 173900ef2d2fSDavid Rientjes return __vmalloc_node_flags(size, NUMA_NO_NODE, 1740e1ca7788SDave Young GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO); 1741e1ca7788SDave Young } 1742e1ca7788SDave Young EXPORT_SYMBOL(vzalloc); 1743e1ca7788SDave Young 1744e1ca7788SDave Young /** 1745ead04089SRolf Eike Beer * vmalloc_user - allocate zeroed virtually contiguous memory for userspace 174683342314SNick Piggin * @size: allocation size 1747ead04089SRolf Eike Beer * 1748ead04089SRolf Eike Beer * The resulting memory area is zeroed so it can be mapped to userspace 1749ead04089SRolf Eike Beer * without leaking data. 175083342314SNick Piggin */ 175183342314SNick Piggin void *vmalloc_user(unsigned long size) 175283342314SNick Piggin { 175383342314SNick Piggin struct vm_struct *area; 175483342314SNick Piggin void *ret; 175583342314SNick Piggin 17562dca6999SDavid Miller ret = __vmalloc_node(size, SHMLBA, 17572dca6999SDavid Miller GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, 175800ef2d2fSDavid Rientjes PAGE_KERNEL, NUMA_NO_NODE, 175900ef2d2fSDavid Rientjes __builtin_return_address(0)); 17602b4ac44eSEric Dumazet if (ret) { 1761db64fe02SNick Piggin area = find_vm_area(ret); 176283342314SNick Piggin area->flags |= VM_USERMAP; 17632b4ac44eSEric Dumazet } 176483342314SNick Piggin return ret; 176583342314SNick Piggin } 176683342314SNick Piggin EXPORT_SYMBOL(vmalloc_user); 176783342314SNick Piggin 176883342314SNick Piggin /** 1769930fc45aSChristoph Lameter * vmalloc_node - allocate memory on a specific node 1770930fc45aSChristoph Lameter * @size: allocation size 1771d44e0780SRandy Dunlap * @node: numa node 1772930fc45aSChristoph Lameter * 1773930fc45aSChristoph Lameter * Allocate enough pages to cover @size from the page level 1774930fc45aSChristoph Lameter * allocator and map them into contiguous kernel virtual space. 1775930fc45aSChristoph Lameter * 1776c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 1777930fc45aSChristoph Lameter * use __vmalloc() instead. 1778930fc45aSChristoph Lameter */ 1779930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node) 1780930fc45aSChristoph Lameter { 17812dca6999SDavid Miller return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL, 178223016969SChristoph Lameter node, __builtin_return_address(0)); 1783930fc45aSChristoph Lameter } 1784930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node); 1785930fc45aSChristoph Lameter 1786e1ca7788SDave Young /** 1787e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill 1788e1ca7788SDave Young * @size: allocation size 1789e1ca7788SDave Young * @node: numa node 1790e1ca7788SDave Young * 1791e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 1792e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 1793e1ca7788SDave Young * The memory allocated is set to zero. 1794e1ca7788SDave Young * 1795e1ca7788SDave Young * For tight control over page level allocator and protection flags 1796e1ca7788SDave Young * use __vmalloc_node() instead. 1797e1ca7788SDave Young */ 1798e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node) 1799e1ca7788SDave Young { 1800e1ca7788SDave Young return __vmalloc_node_flags(size, node, 1801e1ca7788SDave Young GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO); 1802e1ca7788SDave Young } 1803e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node); 1804e1ca7788SDave Young 18054dc3b16bSPavel Pisa #ifndef PAGE_KERNEL_EXEC 18064dc3b16bSPavel Pisa # define PAGE_KERNEL_EXEC PAGE_KERNEL 18074dc3b16bSPavel Pisa #endif 18084dc3b16bSPavel Pisa 18091da177e4SLinus Torvalds /** 18101da177e4SLinus Torvalds * vmalloc_exec - allocate virtually contiguous, executable memory 18111da177e4SLinus Torvalds * @size: allocation size 18121da177e4SLinus Torvalds * 18131da177e4SLinus Torvalds * Kernel-internal function to allocate enough pages to cover @size 18141da177e4SLinus Torvalds * the page level allocator and map them into contiguous and 18151da177e4SLinus Torvalds * executable kernel virtual space. 18161da177e4SLinus Torvalds * 1817c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 18181da177e4SLinus Torvalds * use __vmalloc() instead. 18191da177e4SLinus Torvalds */ 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds void *vmalloc_exec(unsigned long size) 18221da177e4SLinus Torvalds { 18232dca6999SDavid Miller return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC, 182400ef2d2fSDavid Rientjes NUMA_NO_NODE, __builtin_return_address(0)); 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds 18270d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) 18287ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL 18290d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) 18307ac674f5SBenjamin Herrenschmidt #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL 18310d08e0d3SAndi Kleen #else 18320d08e0d3SAndi Kleen #define GFP_VMALLOC32 GFP_KERNEL 18330d08e0d3SAndi Kleen #endif 18340d08e0d3SAndi Kleen 18351da177e4SLinus Torvalds /** 18361da177e4SLinus Torvalds * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 18371da177e4SLinus Torvalds * @size: allocation size 18381da177e4SLinus Torvalds * 18391da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 18401da177e4SLinus Torvalds * page level allocator and map them into contiguous kernel virtual space. 18411da177e4SLinus Torvalds */ 18421da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 18431da177e4SLinus Torvalds { 18442dca6999SDavid Miller return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL, 184500ef2d2fSDavid Rientjes NUMA_NO_NODE, __builtin_return_address(0)); 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32); 18481da177e4SLinus Torvalds 184983342314SNick Piggin /** 1850ead04089SRolf Eike Beer * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 185183342314SNick Piggin * @size: allocation size 1852ead04089SRolf Eike Beer * 1853ead04089SRolf Eike Beer * The resulting memory area is 32bit addressable and zeroed so it can be 1854ead04089SRolf Eike Beer * mapped to userspace without leaking data. 185583342314SNick Piggin */ 185683342314SNick Piggin void *vmalloc_32_user(unsigned long size) 185783342314SNick Piggin { 185883342314SNick Piggin struct vm_struct *area; 185983342314SNick Piggin void *ret; 186083342314SNick Piggin 18612dca6999SDavid Miller ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, 186200ef2d2fSDavid Rientjes NUMA_NO_NODE, __builtin_return_address(0)); 18632b4ac44eSEric Dumazet if (ret) { 1864db64fe02SNick Piggin area = find_vm_area(ret); 186583342314SNick Piggin area->flags |= VM_USERMAP; 18662b4ac44eSEric Dumazet } 186783342314SNick Piggin return ret; 186883342314SNick Piggin } 186983342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user); 187083342314SNick Piggin 1871d0107eb0SKAMEZAWA Hiroyuki /* 1872d0107eb0SKAMEZAWA Hiroyuki * small helper routine , copy contents to buf from addr. 1873d0107eb0SKAMEZAWA Hiroyuki * If the page is not present, fill zero. 1874d0107eb0SKAMEZAWA Hiroyuki */ 1875d0107eb0SKAMEZAWA Hiroyuki 1876d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count) 1877d0107eb0SKAMEZAWA Hiroyuki { 1878d0107eb0SKAMEZAWA Hiroyuki struct page *p; 1879d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 1880d0107eb0SKAMEZAWA Hiroyuki 1881d0107eb0SKAMEZAWA Hiroyuki while (count) { 1882d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 1883d0107eb0SKAMEZAWA Hiroyuki 1884d0107eb0SKAMEZAWA Hiroyuki offset = (unsigned long)addr & ~PAGE_MASK; 1885d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 1886d0107eb0SKAMEZAWA Hiroyuki if (length > count) 1887d0107eb0SKAMEZAWA Hiroyuki length = count; 1888d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 1889d0107eb0SKAMEZAWA Hiroyuki /* 1890d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 1891d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 1892d0107eb0SKAMEZAWA Hiroyuki * overhead of vmalloc()/vfree() calles for this _debug_ 1893d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 1894d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 1895d0107eb0SKAMEZAWA Hiroyuki */ 1896d0107eb0SKAMEZAWA Hiroyuki if (p) { 1897d0107eb0SKAMEZAWA Hiroyuki /* 1898d0107eb0SKAMEZAWA Hiroyuki * we can expect USER0 is not used (see vread/vwrite's 1899d0107eb0SKAMEZAWA Hiroyuki * function description) 1900d0107eb0SKAMEZAWA Hiroyuki */ 19019b04c5feSCong Wang void *map = kmap_atomic(p); 1902d0107eb0SKAMEZAWA Hiroyuki memcpy(buf, map + offset, length); 19039b04c5feSCong Wang kunmap_atomic(map); 1904d0107eb0SKAMEZAWA Hiroyuki } else 1905d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, length); 1906d0107eb0SKAMEZAWA Hiroyuki 1907d0107eb0SKAMEZAWA Hiroyuki addr += length; 1908d0107eb0SKAMEZAWA Hiroyuki buf += length; 1909d0107eb0SKAMEZAWA Hiroyuki copied += length; 1910d0107eb0SKAMEZAWA Hiroyuki count -= length; 1911d0107eb0SKAMEZAWA Hiroyuki } 1912d0107eb0SKAMEZAWA Hiroyuki return copied; 1913d0107eb0SKAMEZAWA Hiroyuki } 1914d0107eb0SKAMEZAWA Hiroyuki 1915d0107eb0SKAMEZAWA Hiroyuki static int aligned_vwrite(char *buf, char *addr, unsigned long count) 1916d0107eb0SKAMEZAWA Hiroyuki { 1917d0107eb0SKAMEZAWA Hiroyuki struct page *p; 1918d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 1919d0107eb0SKAMEZAWA Hiroyuki 1920d0107eb0SKAMEZAWA Hiroyuki while (count) { 1921d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 1922d0107eb0SKAMEZAWA Hiroyuki 1923d0107eb0SKAMEZAWA Hiroyuki offset = (unsigned long)addr & ~PAGE_MASK; 1924d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 1925d0107eb0SKAMEZAWA Hiroyuki if (length > count) 1926d0107eb0SKAMEZAWA Hiroyuki length = count; 1927d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 1928d0107eb0SKAMEZAWA Hiroyuki /* 1929d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 1930d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 1931d0107eb0SKAMEZAWA Hiroyuki * overhead of vmalloc()/vfree() calles for this _debug_ 1932d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 1933d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 1934d0107eb0SKAMEZAWA Hiroyuki */ 1935d0107eb0SKAMEZAWA Hiroyuki if (p) { 1936d0107eb0SKAMEZAWA Hiroyuki /* 1937d0107eb0SKAMEZAWA Hiroyuki * we can expect USER0 is not used (see vread/vwrite's 1938d0107eb0SKAMEZAWA Hiroyuki * function description) 1939d0107eb0SKAMEZAWA Hiroyuki */ 19409b04c5feSCong Wang void *map = kmap_atomic(p); 1941d0107eb0SKAMEZAWA Hiroyuki memcpy(map + offset, buf, length); 19429b04c5feSCong Wang kunmap_atomic(map); 1943d0107eb0SKAMEZAWA Hiroyuki } 1944d0107eb0SKAMEZAWA Hiroyuki addr += length; 1945d0107eb0SKAMEZAWA Hiroyuki buf += length; 1946d0107eb0SKAMEZAWA Hiroyuki copied += length; 1947d0107eb0SKAMEZAWA Hiroyuki count -= length; 1948d0107eb0SKAMEZAWA Hiroyuki } 1949d0107eb0SKAMEZAWA Hiroyuki return copied; 1950d0107eb0SKAMEZAWA Hiroyuki } 1951d0107eb0SKAMEZAWA Hiroyuki 1952d0107eb0SKAMEZAWA Hiroyuki /** 1953d0107eb0SKAMEZAWA Hiroyuki * vread() - read vmalloc area in a safe way. 1954d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for reading data 1955d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 1956d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 1957d0107eb0SKAMEZAWA Hiroyuki * 1958d0107eb0SKAMEZAWA Hiroyuki * Returns # of bytes which addr and buf should be increased. 1959d0107eb0SKAMEZAWA Hiroyuki * (same number to @count). Returns 0 if [addr...addr+count) doesn't 1960d0107eb0SKAMEZAWA Hiroyuki * includes any intersect with alive vmalloc area. 1961d0107eb0SKAMEZAWA Hiroyuki * 1962d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 1963d0107eb0SKAMEZAWA Hiroyuki * copy data from that area to a given buffer. If the given memory range 1964d0107eb0SKAMEZAWA Hiroyuki * of [addr...addr+count) includes some valid address, data is copied to 1965d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, they'll be zero-filled. 1966d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 1967d0107eb0SKAMEZAWA Hiroyuki * 1968d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 1969a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 1970d0107eb0SKAMEZAWA Hiroyuki * 1971d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vread() is never necessary because the caller 1972d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 1973d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 1974d0107eb0SKAMEZAWA Hiroyuki * any informaion, as /dev/kmem. 1975d0107eb0SKAMEZAWA Hiroyuki * 1976d0107eb0SKAMEZAWA Hiroyuki */ 1977d0107eb0SKAMEZAWA Hiroyuki 19781da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 19791da177e4SLinus Torvalds { 1980e81ce85fSJoonsoo Kim struct vmap_area *va; 1981e81ce85fSJoonsoo Kim struct vm_struct *vm; 19821da177e4SLinus Torvalds char *vaddr, *buf_start = buf; 1983d0107eb0SKAMEZAWA Hiroyuki unsigned long buflen = count; 19841da177e4SLinus Torvalds unsigned long n; 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds /* Don't allow overflow */ 19871da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 19881da177e4SLinus Torvalds count = -(unsigned long) addr; 19891da177e4SLinus Torvalds 1990e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 1991e81ce85fSJoonsoo Kim list_for_each_entry(va, &vmap_area_list, list) { 1992e81ce85fSJoonsoo Kim if (!count) 1993e81ce85fSJoonsoo Kim break; 1994e81ce85fSJoonsoo Kim 1995e81ce85fSJoonsoo Kim if (!(va->flags & VM_VM_AREA)) 1996e81ce85fSJoonsoo Kim continue; 1997e81ce85fSJoonsoo Kim 1998e81ce85fSJoonsoo Kim vm = va->vm; 1999e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 2000762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 20011da177e4SLinus Torvalds continue; 20021da177e4SLinus Torvalds while (addr < vaddr) { 20031da177e4SLinus Torvalds if (count == 0) 20041da177e4SLinus Torvalds goto finished; 20051da177e4SLinus Torvalds *buf = '\0'; 20061da177e4SLinus Torvalds buf++; 20071da177e4SLinus Torvalds addr++; 20081da177e4SLinus Torvalds count--; 20091da177e4SLinus Torvalds } 2010762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 2011d0107eb0SKAMEZAWA Hiroyuki if (n > count) 2012d0107eb0SKAMEZAWA Hiroyuki n = count; 2013e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) 2014d0107eb0SKAMEZAWA Hiroyuki aligned_vread(buf, addr, n); 2015d0107eb0SKAMEZAWA Hiroyuki else /* IOREMAP area is treated as memory hole */ 2016d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, n); 2017d0107eb0SKAMEZAWA Hiroyuki buf += n; 2018d0107eb0SKAMEZAWA Hiroyuki addr += n; 2019d0107eb0SKAMEZAWA Hiroyuki count -= n; 20201da177e4SLinus Torvalds } 20211da177e4SLinus Torvalds finished: 2022e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 2023d0107eb0SKAMEZAWA Hiroyuki 2024d0107eb0SKAMEZAWA Hiroyuki if (buf == buf_start) 2025d0107eb0SKAMEZAWA Hiroyuki return 0; 2026d0107eb0SKAMEZAWA Hiroyuki /* zero-fill memory holes */ 2027d0107eb0SKAMEZAWA Hiroyuki if (buf != buf_start + buflen) 2028d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, buflen - (buf - buf_start)); 2029d0107eb0SKAMEZAWA Hiroyuki 2030d0107eb0SKAMEZAWA Hiroyuki return buflen; 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds 2033d0107eb0SKAMEZAWA Hiroyuki /** 2034d0107eb0SKAMEZAWA Hiroyuki * vwrite() - write vmalloc area in a safe way. 2035d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for source data 2036d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 2037d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 2038d0107eb0SKAMEZAWA Hiroyuki * 2039d0107eb0SKAMEZAWA Hiroyuki * Returns # of bytes which addr and buf should be incresed. 2040d0107eb0SKAMEZAWA Hiroyuki * (same number to @count). 2041d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersect with valid 2042d0107eb0SKAMEZAWA Hiroyuki * vmalloc area, returns 0. 2043d0107eb0SKAMEZAWA Hiroyuki * 2044d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 2045d0107eb0SKAMEZAWA Hiroyuki * copy data from a buffer to the given addr. If specified range of 2046d0107eb0SKAMEZAWA Hiroyuki * [addr...addr+count) includes some valid address, data is copied from 2047d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, no copy to hole. 2048d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 2049d0107eb0SKAMEZAWA Hiroyuki * 2050d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 2051a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 2052d0107eb0SKAMEZAWA Hiroyuki * 2053d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vwrite() is never necessary because the caller 2054d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 2055d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 2056d0107eb0SKAMEZAWA Hiroyuki * any informaion, as /dev/kmem. 2057d0107eb0SKAMEZAWA Hiroyuki */ 2058d0107eb0SKAMEZAWA Hiroyuki 20591da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count) 20601da177e4SLinus Torvalds { 2061e81ce85fSJoonsoo Kim struct vmap_area *va; 2062e81ce85fSJoonsoo Kim struct vm_struct *vm; 2063d0107eb0SKAMEZAWA Hiroyuki char *vaddr; 2064d0107eb0SKAMEZAWA Hiroyuki unsigned long n, buflen; 2065d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds /* Don't allow overflow */ 20681da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 20691da177e4SLinus Torvalds count = -(unsigned long) addr; 2070d0107eb0SKAMEZAWA Hiroyuki buflen = count; 20711da177e4SLinus Torvalds 2072e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 2073e81ce85fSJoonsoo Kim list_for_each_entry(va, &vmap_area_list, list) { 2074e81ce85fSJoonsoo Kim if (!count) 2075e81ce85fSJoonsoo Kim break; 2076e81ce85fSJoonsoo Kim 2077e81ce85fSJoonsoo Kim if (!(va->flags & VM_VM_AREA)) 2078e81ce85fSJoonsoo Kim continue; 2079e81ce85fSJoonsoo Kim 2080e81ce85fSJoonsoo Kim vm = va->vm; 2081e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 2082762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 20831da177e4SLinus Torvalds continue; 20841da177e4SLinus Torvalds while (addr < vaddr) { 20851da177e4SLinus Torvalds if (count == 0) 20861da177e4SLinus Torvalds goto finished; 20871da177e4SLinus Torvalds buf++; 20881da177e4SLinus Torvalds addr++; 20891da177e4SLinus Torvalds count--; 20901da177e4SLinus Torvalds } 2091762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 2092d0107eb0SKAMEZAWA Hiroyuki if (n > count) 2093d0107eb0SKAMEZAWA Hiroyuki n = count; 2094e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) { 2095d0107eb0SKAMEZAWA Hiroyuki aligned_vwrite(buf, addr, n); 2096d0107eb0SKAMEZAWA Hiroyuki copied++; 2097d0107eb0SKAMEZAWA Hiroyuki } 2098d0107eb0SKAMEZAWA Hiroyuki buf += n; 2099d0107eb0SKAMEZAWA Hiroyuki addr += n; 2100d0107eb0SKAMEZAWA Hiroyuki count -= n; 21011da177e4SLinus Torvalds } 21021da177e4SLinus Torvalds finished: 2103e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 2104d0107eb0SKAMEZAWA Hiroyuki if (!copied) 2105d0107eb0SKAMEZAWA Hiroyuki return 0; 2106d0107eb0SKAMEZAWA Hiroyuki return buflen; 21071da177e4SLinus Torvalds } 210883342314SNick Piggin 210983342314SNick Piggin /** 2110e69e9d4aSHATAYAMA Daisuke * remap_vmalloc_range_partial - map vmalloc pages to userspace 2111e69e9d4aSHATAYAMA Daisuke * @vma: vma to cover 2112e69e9d4aSHATAYAMA Daisuke * @uaddr: target user address to start at 2113e69e9d4aSHATAYAMA Daisuke * @kaddr: virtual address of vmalloc kernel memory 2114e69e9d4aSHATAYAMA Daisuke * @size: size of map area 2115e69e9d4aSHATAYAMA Daisuke * 2116e69e9d4aSHATAYAMA Daisuke * Returns: 0 for success, -Exxx on failure 2117e69e9d4aSHATAYAMA Daisuke * 2118e69e9d4aSHATAYAMA Daisuke * This function checks that @kaddr is a valid vmalloc'ed area, 2119e69e9d4aSHATAYAMA Daisuke * and that it is big enough to cover the range starting at 2120e69e9d4aSHATAYAMA Daisuke * @uaddr in @vma. Will return failure if that criteria isn't 2121e69e9d4aSHATAYAMA Daisuke * met. 2122e69e9d4aSHATAYAMA Daisuke * 2123e69e9d4aSHATAYAMA Daisuke * Similar to remap_pfn_range() (see mm/memory.c) 2124e69e9d4aSHATAYAMA Daisuke */ 2125e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr, 2126e69e9d4aSHATAYAMA Daisuke void *kaddr, unsigned long size) 2127e69e9d4aSHATAYAMA Daisuke { 2128e69e9d4aSHATAYAMA Daisuke struct vm_struct *area; 2129e69e9d4aSHATAYAMA Daisuke 2130e69e9d4aSHATAYAMA Daisuke size = PAGE_ALIGN(size); 2131e69e9d4aSHATAYAMA Daisuke 2132e69e9d4aSHATAYAMA Daisuke if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr)) 2133e69e9d4aSHATAYAMA Daisuke return -EINVAL; 2134e69e9d4aSHATAYAMA Daisuke 2135e69e9d4aSHATAYAMA Daisuke area = find_vm_area(kaddr); 2136e69e9d4aSHATAYAMA Daisuke if (!area) 2137e69e9d4aSHATAYAMA Daisuke return -EINVAL; 2138e69e9d4aSHATAYAMA Daisuke 2139e69e9d4aSHATAYAMA Daisuke if (!(area->flags & VM_USERMAP)) 2140e69e9d4aSHATAYAMA Daisuke return -EINVAL; 2141e69e9d4aSHATAYAMA Daisuke 2142e69e9d4aSHATAYAMA Daisuke if (kaddr + size > area->addr + area->size) 2143e69e9d4aSHATAYAMA Daisuke return -EINVAL; 2144e69e9d4aSHATAYAMA Daisuke 2145e69e9d4aSHATAYAMA Daisuke do { 2146e69e9d4aSHATAYAMA Daisuke struct page *page = vmalloc_to_page(kaddr); 2147e69e9d4aSHATAYAMA Daisuke int ret; 2148e69e9d4aSHATAYAMA Daisuke 2149e69e9d4aSHATAYAMA Daisuke ret = vm_insert_page(vma, uaddr, page); 2150e69e9d4aSHATAYAMA Daisuke if (ret) 2151e69e9d4aSHATAYAMA Daisuke return ret; 2152e69e9d4aSHATAYAMA Daisuke 2153e69e9d4aSHATAYAMA Daisuke uaddr += PAGE_SIZE; 2154e69e9d4aSHATAYAMA Daisuke kaddr += PAGE_SIZE; 2155e69e9d4aSHATAYAMA Daisuke size -= PAGE_SIZE; 2156e69e9d4aSHATAYAMA Daisuke } while (size > 0); 2157e69e9d4aSHATAYAMA Daisuke 2158e69e9d4aSHATAYAMA Daisuke vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 2159e69e9d4aSHATAYAMA Daisuke 2160e69e9d4aSHATAYAMA Daisuke return 0; 2161e69e9d4aSHATAYAMA Daisuke } 2162e69e9d4aSHATAYAMA Daisuke EXPORT_SYMBOL(remap_vmalloc_range_partial); 2163e69e9d4aSHATAYAMA Daisuke 2164e69e9d4aSHATAYAMA Daisuke /** 216583342314SNick Piggin * remap_vmalloc_range - map vmalloc pages to userspace 216683342314SNick Piggin * @vma: vma to cover (map full range of vma) 216783342314SNick Piggin * @addr: vmalloc memory 216883342314SNick Piggin * @pgoff: number of pages into addr before first page to map 21697682486bSRandy Dunlap * 21707682486bSRandy Dunlap * Returns: 0 for success, -Exxx on failure 217183342314SNick Piggin * 217283342314SNick Piggin * This function checks that addr is a valid vmalloc'ed area, and 217383342314SNick Piggin * that it is big enough to cover the vma. Will return failure if 217483342314SNick Piggin * that criteria isn't met. 217583342314SNick Piggin * 217672fd4a35SRobert P. J. Day * Similar to remap_pfn_range() (see mm/memory.c) 217783342314SNick Piggin */ 217883342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 217983342314SNick Piggin unsigned long pgoff) 218083342314SNick Piggin { 2181e69e9d4aSHATAYAMA Daisuke return remap_vmalloc_range_partial(vma, vma->vm_start, 2182e69e9d4aSHATAYAMA Daisuke addr + (pgoff << PAGE_SHIFT), 2183e69e9d4aSHATAYAMA Daisuke vma->vm_end - vma->vm_start); 218483342314SNick Piggin } 218583342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range); 218683342314SNick Piggin 21871eeb66a1SChristoph Hellwig /* 21881eeb66a1SChristoph Hellwig * Implement a stub for vmalloc_sync_all() if the architecture chose not to 21891eeb66a1SChristoph Hellwig * have one. 21901eeb66a1SChristoph Hellwig */ 21913b32123dSGideon Israel Dsouza void __weak vmalloc_sync_all(void) 21921eeb66a1SChristoph Hellwig { 21931eeb66a1SChristoph Hellwig } 21945f4352fbSJeremy Fitzhardinge 21955f4352fbSJeremy Fitzhardinge 21962f569afdSMartin Schwidefsky static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data) 21975f4352fbSJeremy Fitzhardinge { 2198cd12909cSDavid Vrabel pte_t ***p = data; 2199cd12909cSDavid Vrabel 2200cd12909cSDavid Vrabel if (p) { 2201cd12909cSDavid Vrabel *(*p) = pte; 2202cd12909cSDavid Vrabel (*p)++; 2203cd12909cSDavid Vrabel } 22045f4352fbSJeremy Fitzhardinge return 0; 22055f4352fbSJeremy Fitzhardinge } 22065f4352fbSJeremy Fitzhardinge 22075f4352fbSJeremy Fitzhardinge /** 22085f4352fbSJeremy Fitzhardinge * alloc_vm_area - allocate a range of kernel address space 22095f4352fbSJeremy Fitzhardinge * @size: size of the area 2210cd12909cSDavid Vrabel * @ptes: returns the PTEs for the address space 22117682486bSRandy Dunlap * 22127682486bSRandy Dunlap * Returns: NULL on failure, vm_struct on success 22135f4352fbSJeremy Fitzhardinge * 22145f4352fbSJeremy Fitzhardinge * This function reserves a range of kernel address space, and 22155f4352fbSJeremy Fitzhardinge * allocates pagetables to map that range. No actual mappings 2216cd12909cSDavid Vrabel * are created. 2217cd12909cSDavid Vrabel * 2218cd12909cSDavid Vrabel * If @ptes is non-NULL, pointers to the PTEs (in init_mm) 2219cd12909cSDavid Vrabel * allocated for the VM area are returned. 22205f4352fbSJeremy Fitzhardinge */ 2221cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes) 22225f4352fbSJeremy Fitzhardinge { 22235f4352fbSJeremy Fitzhardinge struct vm_struct *area; 22245f4352fbSJeremy Fitzhardinge 222523016969SChristoph Lameter area = get_vm_area_caller(size, VM_IOREMAP, 222623016969SChristoph Lameter __builtin_return_address(0)); 22275f4352fbSJeremy Fitzhardinge if (area == NULL) 22285f4352fbSJeremy Fitzhardinge return NULL; 22295f4352fbSJeremy Fitzhardinge 22305f4352fbSJeremy Fitzhardinge /* 22315f4352fbSJeremy Fitzhardinge * This ensures that page tables are constructed for this region 22325f4352fbSJeremy Fitzhardinge * of kernel virtual address space and mapped into init_mm. 22335f4352fbSJeremy Fitzhardinge */ 22345f4352fbSJeremy Fitzhardinge if (apply_to_page_range(&init_mm, (unsigned long)area->addr, 2235cd12909cSDavid Vrabel size, f, ptes ? &ptes : NULL)) { 22365f4352fbSJeremy Fitzhardinge free_vm_area(area); 22375f4352fbSJeremy Fitzhardinge return NULL; 22385f4352fbSJeremy Fitzhardinge } 22395f4352fbSJeremy Fitzhardinge 22405f4352fbSJeremy Fitzhardinge return area; 22415f4352fbSJeremy Fitzhardinge } 22425f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(alloc_vm_area); 22435f4352fbSJeremy Fitzhardinge 22445f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area) 22455f4352fbSJeremy Fitzhardinge { 22465f4352fbSJeremy Fitzhardinge struct vm_struct *ret; 22475f4352fbSJeremy Fitzhardinge ret = remove_vm_area(area->addr); 22485f4352fbSJeremy Fitzhardinge BUG_ON(ret != area); 22495f4352fbSJeremy Fitzhardinge kfree(area); 22505f4352fbSJeremy Fitzhardinge } 22515f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area); 2252a10aa579SChristoph Lameter 22534f8b02b4STejun Heo #ifdef CONFIG_SMP 2254ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n) 2255ca23e405STejun Heo { 2256ca23e405STejun Heo return n ? rb_entry(n, struct vmap_area, rb_node) : NULL; 2257ca23e405STejun Heo } 2258ca23e405STejun Heo 2259ca23e405STejun Heo /** 2260ca23e405STejun Heo * pvm_find_next_prev - find the next and prev vmap_area surrounding @end 2261ca23e405STejun Heo * @end: target address 2262ca23e405STejun Heo * @pnext: out arg for the next vmap_area 2263ca23e405STejun Heo * @pprev: out arg for the previous vmap_area 2264ca23e405STejun Heo * 2265ca23e405STejun Heo * Returns: %true if either or both of next and prev are found, 2266ca23e405STejun Heo * %false if no vmap_area exists 2267ca23e405STejun Heo * 2268ca23e405STejun Heo * Find vmap_areas end addresses of which enclose @end. ie. if not 2269ca23e405STejun Heo * NULL, *pnext->va_end > @end and *pprev->va_end <= @end. 2270ca23e405STejun Heo */ 2271ca23e405STejun Heo static bool pvm_find_next_prev(unsigned long end, 2272ca23e405STejun Heo struct vmap_area **pnext, 2273ca23e405STejun Heo struct vmap_area **pprev) 2274ca23e405STejun Heo { 2275ca23e405STejun Heo struct rb_node *n = vmap_area_root.rb_node; 2276ca23e405STejun Heo struct vmap_area *va = NULL; 2277ca23e405STejun Heo 2278ca23e405STejun Heo while (n) { 2279ca23e405STejun Heo va = rb_entry(n, struct vmap_area, rb_node); 2280ca23e405STejun Heo if (end < va->va_end) 2281ca23e405STejun Heo n = n->rb_left; 2282ca23e405STejun Heo else if (end > va->va_end) 2283ca23e405STejun Heo n = n->rb_right; 2284ca23e405STejun Heo else 2285ca23e405STejun Heo break; 2286ca23e405STejun Heo } 2287ca23e405STejun Heo 2288ca23e405STejun Heo if (!va) 2289ca23e405STejun Heo return false; 2290ca23e405STejun Heo 2291ca23e405STejun Heo if (va->va_end > end) { 2292ca23e405STejun Heo *pnext = va; 2293ca23e405STejun Heo *pprev = node_to_va(rb_prev(&(*pnext)->rb_node)); 2294ca23e405STejun Heo } else { 2295ca23e405STejun Heo *pprev = va; 2296ca23e405STejun Heo *pnext = node_to_va(rb_next(&(*pprev)->rb_node)); 2297ca23e405STejun Heo } 2298ca23e405STejun Heo return true; 2299ca23e405STejun Heo } 2300ca23e405STejun Heo 2301ca23e405STejun Heo /** 2302ca23e405STejun Heo * pvm_determine_end - find the highest aligned address between two vmap_areas 2303ca23e405STejun Heo * @pnext: in/out arg for the next vmap_area 2304ca23e405STejun Heo * @pprev: in/out arg for the previous vmap_area 2305ca23e405STejun Heo * @align: alignment 2306ca23e405STejun Heo * 2307ca23e405STejun Heo * Returns: determined end address 2308ca23e405STejun Heo * 2309ca23e405STejun Heo * Find the highest aligned address between *@pnext and *@pprev below 2310ca23e405STejun Heo * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned 2311ca23e405STejun Heo * down address is between the end addresses of the two vmap_areas. 2312ca23e405STejun Heo * 2313ca23e405STejun Heo * Please note that the address returned by this function may fall 2314ca23e405STejun Heo * inside *@pnext vmap_area. The caller is responsible for checking 2315ca23e405STejun Heo * that. 2316ca23e405STejun Heo */ 2317ca23e405STejun Heo static unsigned long pvm_determine_end(struct vmap_area **pnext, 2318ca23e405STejun Heo struct vmap_area **pprev, 2319ca23e405STejun Heo unsigned long align) 2320ca23e405STejun Heo { 2321ca23e405STejun Heo const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 2322ca23e405STejun Heo unsigned long addr; 2323ca23e405STejun Heo 2324ca23e405STejun Heo if (*pnext) 2325ca23e405STejun Heo addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end); 2326ca23e405STejun Heo else 2327ca23e405STejun Heo addr = vmalloc_end; 2328ca23e405STejun Heo 2329ca23e405STejun Heo while (*pprev && (*pprev)->va_end > addr) { 2330ca23e405STejun Heo *pnext = *pprev; 2331ca23e405STejun Heo *pprev = node_to_va(rb_prev(&(*pnext)->rb_node)); 2332ca23e405STejun Heo } 2333ca23e405STejun Heo 2334ca23e405STejun Heo return addr; 2335ca23e405STejun Heo } 2336ca23e405STejun Heo 2337ca23e405STejun Heo /** 2338ca23e405STejun Heo * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator 2339ca23e405STejun Heo * @offsets: array containing offset of each area 2340ca23e405STejun Heo * @sizes: array containing size of each area 2341ca23e405STejun Heo * @nr_vms: the number of areas to allocate 2342ca23e405STejun Heo * @align: alignment, all entries in @offsets and @sizes must be aligned to this 2343ca23e405STejun Heo * 2344ca23e405STejun Heo * Returns: kmalloc'd vm_struct pointer array pointing to allocated 2345ca23e405STejun Heo * vm_structs on success, %NULL on failure 2346ca23e405STejun Heo * 2347ca23e405STejun Heo * Percpu allocator wants to use congruent vm areas so that it can 2348ca23e405STejun Heo * maintain the offsets among percpu areas. This function allocates 2349ec3f64fcSDavid Rientjes * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to 2350ec3f64fcSDavid Rientjes * be scattered pretty far, distance between two areas easily going up 2351ec3f64fcSDavid Rientjes * to gigabytes. To avoid interacting with regular vmallocs, these 2352ec3f64fcSDavid Rientjes * areas are allocated from top. 2353ca23e405STejun Heo * 2354ca23e405STejun Heo * Despite its complicated look, this allocator is rather simple. It 2355ca23e405STejun Heo * does everything top-down and scans areas from the end looking for 2356ca23e405STejun Heo * matching slot. While scanning, if any of the areas overlaps with 2357ca23e405STejun Heo * existing vmap_area, the base address is pulled down to fit the 2358ca23e405STejun Heo * area. Scanning is repeated till all the areas fit and then all 2359ca23e405STejun Heo * necessary data structres are inserted and the result is returned. 2360ca23e405STejun Heo */ 2361ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, 2362ca23e405STejun Heo const size_t *sizes, int nr_vms, 2363ec3f64fcSDavid Rientjes size_t align) 2364ca23e405STejun Heo { 2365ca23e405STejun Heo const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); 2366ca23e405STejun Heo const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 2367ca23e405STejun Heo struct vmap_area **vas, *prev, *next; 2368ca23e405STejun Heo struct vm_struct **vms; 2369ca23e405STejun Heo int area, area2, last_area, term_area; 2370ca23e405STejun Heo unsigned long base, start, end, last_end; 2371ca23e405STejun Heo bool purged = false; 2372ca23e405STejun Heo 2373ca23e405STejun Heo /* verify parameters and allocate data structures */ 2374ca23e405STejun Heo BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align)); 2375ca23e405STejun Heo for (last_area = 0, area = 0; area < nr_vms; area++) { 2376ca23e405STejun Heo start = offsets[area]; 2377ca23e405STejun Heo end = start + sizes[area]; 2378ca23e405STejun Heo 2379ca23e405STejun Heo /* is everything aligned properly? */ 2380ca23e405STejun Heo BUG_ON(!IS_ALIGNED(offsets[area], align)); 2381ca23e405STejun Heo BUG_ON(!IS_ALIGNED(sizes[area], align)); 2382ca23e405STejun Heo 2383ca23e405STejun Heo /* detect the area with the highest address */ 2384ca23e405STejun Heo if (start > offsets[last_area]) 2385ca23e405STejun Heo last_area = area; 2386ca23e405STejun Heo 2387ca23e405STejun Heo for (area2 = 0; area2 < nr_vms; area2++) { 2388ca23e405STejun Heo unsigned long start2 = offsets[area2]; 2389ca23e405STejun Heo unsigned long end2 = start2 + sizes[area2]; 2390ca23e405STejun Heo 2391ca23e405STejun Heo if (area2 == area) 2392ca23e405STejun Heo continue; 2393ca23e405STejun Heo 2394ca23e405STejun Heo BUG_ON(start2 >= start && start2 < end); 2395ca23e405STejun Heo BUG_ON(end2 <= end && end2 > start); 2396ca23e405STejun Heo } 2397ca23e405STejun Heo } 2398ca23e405STejun Heo last_end = offsets[last_area] + sizes[last_area]; 2399ca23e405STejun Heo 2400ca23e405STejun Heo if (vmalloc_end - vmalloc_start < last_end) { 2401ca23e405STejun Heo WARN_ON(true); 2402ca23e405STejun Heo return NULL; 2403ca23e405STejun Heo } 2404ca23e405STejun Heo 24054d67d860SThomas Meyer vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL); 24064d67d860SThomas Meyer vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL); 2407ca23e405STejun Heo if (!vas || !vms) 2408f1db7afdSKautuk Consul goto err_free2; 2409ca23e405STejun Heo 2410ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 2411ec3f64fcSDavid Rientjes vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL); 2412ec3f64fcSDavid Rientjes vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL); 2413ca23e405STejun Heo if (!vas[area] || !vms[area]) 2414ca23e405STejun Heo goto err_free; 2415ca23e405STejun Heo } 2416ca23e405STejun Heo retry: 2417ca23e405STejun Heo spin_lock(&vmap_area_lock); 2418ca23e405STejun Heo 2419ca23e405STejun Heo /* start scanning - we scan from the top, begin with the last area */ 2420ca23e405STejun Heo area = term_area = last_area; 2421ca23e405STejun Heo start = offsets[area]; 2422ca23e405STejun Heo end = start + sizes[area]; 2423ca23e405STejun Heo 2424ca23e405STejun Heo if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) { 2425ca23e405STejun Heo base = vmalloc_end - last_end; 2426ca23e405STejun Heo goto found; 2427ca23e405STejun Heo } 2428ca23e405STejun Heo base = pvm_determine_end(&next, &prev, align) - end; 2429ca23e405STejun Heo 2430ca23e405STejun Heo while (true) { 2431ca23e405STejun Heo BUG_ON(next && next->va_end <= base + end); 2432ca23e405STejun Heo BUG_ON(prev && prev->va_end > base + end); 2433ca23e405STejun Heo 2434ca23e405STejun Heo /* 2435ca23e405STejun Heo * base might have underflowed, add last_end before 2436ca23e405STejun Heo * comparing. 2437ca23e405STejun Heo */ 2438ca23e405STejun Heo if (base + last_end < vmalloc_start + last_end) { 2439ca23e405STejun Heo spin_unlock(&vmap_area_lock); 2440ca23e405STejun Heo if (!purged) { 2441ca23e405STejun Heo purge_vmap_area_lazy(); 2442ca23e405STejun Heo purged = true; 2443ca23e405STejun Heo goto retry; 2444ca23e405STejun Heo } 2445ca23e405STejun Heo goto err_free; 2446ca23e405STejun Heo } 2447ca23e405STejun Heo 2448ca23e405STejun Heo /* 2449ca23e405STejun Heo * If next overlaps, move base downwards so that it's 2450ca23e405STejun Heo * right below next and then recheck. 2451ca23e405STejun Heo */ 2452ca23e405STejun Heo if (next && next->va_start < base + end) { 2453ca23e405STejun Heo base = pvm_determine_end(&next, &prev, align) - end; 2454ca23e405STejun Heo term_area = area; 2455ca23e405STejun Heo continue; 2456ca23e405STejun Heo } 2457ca23e405STejun Heo 2458ca23e405STejun Heo /* 2459ca23e405STejun Heo * If prev overlaps, shift down next and prev and move 2460ca23e405STejun Heo * base so that it's right below new next and then 2461ca23e405STejun Heo * recheck. 2462ca23e405STejun Heo */ 2463ca23e405STejun Heo if (prev && prev->va_end > base + start) { 2464ca23e405STejun Heo next = prev; 2465ca23e405STejun Heo prev = node_to_va(rb_prev(&next->rb_node)); 2466ca23e405STejun Heo base = pvm_determine_end(&next, &prev, align) - end; 2467ca23e405STejun Heo term_area = area; 2468ca23e405STejun Heo continue; 2469ca23e405STejun Heo } 2470ca23e405STejun Heo 2471ca23e405STejun Heo /* 2472ca23e405STejun Heo * This area fits, move on to the previous one. If 2473ca23e405STejun Heo * the previous one is the terminal one, we're done. 2474ca23e405STejun Heo */ 2475ca23e405STejun Heo area = (area + nr_vms - 1) % nr_vms; 2476ca23e405STejun Heo if (area == term_area) 2477ca23e405STejun Heo break; 2478ca23e405STejun Heo start = offsets[area]; 2479ca23e405STejun Heo end = start + sizes[area]; 2480ca23e405STejun Heo pvm_find_next_prev(base + end, &next, &prev); 2481ca23e405STejun Heo } 2482ca23e405STejun Heo found: 2483ca23e405STejun Heo /* we've found a fitting base, insert all va's */ 2484ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 2485ca23e405STejun Heo struct vmap_area *va = vas[area]; 2486ca23e405STejun Heo 2487ca23e405STejun Heo va->va_start = base + offsets[area]; 2488ca23e405STejun Heo va->va_end = va->va_start + sizes[area]; 2489ca23e405STejun Heo __insert_vmap_area(va); 2490ca23e405STejun Heo } 2491ca23e405STejun Heo 2492ca23e405STejun Heo vmap_area_pcpu_hole = base + offsets[last_area]; 2493ca23e405STejun Heo 2494ca23e405STejun Heo spin_unlock(&vmap_area_lock); 2495ca23e405STejun Heo 2496ca23e405STejun Heo /* insert all vm's */ 2497ca23e405STejun Heo for (area = 0; area < nr_vms; area++) 24983645cb4aSZhang Yanfei setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC, 2499ca23e405STejun Heo pcpu_get_vm_areas); 2500ca23e405STejun Heo 2501ca23e405STejun Heo kfree(vas); 2502ca23e405STejun Heo return vms; 2503ca23e405STejun Heo 2504ca23e405STejun Heo err_free: 2505ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 2506ca23e405STejun Heo kfree(vas[area]); 2507ca23e405STejun Heo kfree(vms[area]); 2508ca23e405STejun Heo } 2509f1db7afdSKautuk Consul err_free2: 2510ca23e405STejun Heo kfree(vas); 2511ca23e405STejun Heo kfree(vms); 2512ca23e405STejun Heo return NULL; 2513ca23e405STejun Heo } 2514ca23e405STejun Heo 2515ca23e405STejun Heo /** 2516ca23e405STejun Heo * pcpu_free_vm_areas - free vmalloc areas for percpu allocator 2517ca23e405STejun Heo * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() 2518ca23e405STejun Heo * @nr_vms: the number of allocated areas 2519ca23e405STejun Heo * 2520ca23e405STejun Heo * Free vm_structs and the array allocated by pcpu_get_vm_areas(). 2521ca23e405STejun Heo */ 2522ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) 2523ca23e405STejun Heo { 2524ca23e405STejun Heo int i; 2525ca23e405STejun Heo 2526ca23e405STejun Heo for (i = 0; i < nr_vms; i++) 2527ca23e405STejun Heo free_vm_area(vms[i]); 2528ca23e405STejun Heo kfree(vms); 2529ca23e405STejun Heo } 25304f8b02b4STejun Heo #endif /* CONFIG_SMP */ 2531a10aa579SChristoph Lameter 2532a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS 2533a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos) 2534d4033afdSJoonsoo Kim __acquires(&vmap_area_lock) 2535a10aa579SChristoph Lameter { 2536a10aa579SChristoph Lameter loff_t n = *pos; 2537d4033afdSJoonsoo Kim struct vmap_area *va; 2538a10aa579SChristoph Lameter 2539d4033afdSJoonsoo Kim spin_lock(&vmap_area_lock); 2540d4033afdSJoonsoo Kim va = list_entry((&vmap_area_list)->next, typeof(*va), list); 2541d4033afdSJoonsoo Kim while (n > 0 && &va->list != &vmap_area_list) { 2542a10aa579SChristoph Lameter n--; 2543d4033afdSJoonsoo Kim va = list_entry(va->list.next, typeof(*va), list); 2544a10aa579SChristoph Lameter } 2545d4033afdSJoonsoo Kim if (!n && &va->list != &vmap_area_list) 2546d4033afdSJoonsoo Kim return va; 2547a10aa579SChristoph Lameter 2548a10aa579SChristoph Lameter return NULL; 2549a10aa579SChristoph Lameter 2550a10aa579SChristoph Lameter } 2551a10aa579SChristoph Lameter 2552a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos) 2553a10aa579SChristoph Lameter { 2554d4033afdSJoonsoo Kim struct vmap_area *va = p, *next; 2555a10aa579SChristoph Lameter 2556a10aa579SChristoph Lameter ++*pos; 2557d4033afdSJoonsoo Kim next = list_entry(va->list.next, typeof(*va), list); 2558d4033afdSJoonsoo Kim if (&next->list != &vmap_area_list) 2559d4033afdSJoonsoo Kim return next; 2560d4033afdSJoonsoo Kim 2561d4033afdSJoonsoo Kim return NULL; 2562a10aa579SChristoph Lameter } 2563a10aa579SChristoph Lameter 2564a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p) 2565d4033afdSJoonsoo Kim __releases(&vmap_area_lock) 2566a10aa579SChristoph Lameter { 2567d4033afdSJoonsoo Kim spin_unlock(&vmap_area_lock); 2568a10aa579SChristoph Lameter } 2569a10aa579SChristoph Lameter 2570a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v) 2571a47a126aSEric Dumazet { 2572e5adfffcSKirill A. Shutemov if (IS_ENABLED(CONFIG_NUMA)) { 2573a47a126aSEric Dumazet unsigned int nr, *counters = m->private; 2574a47a126aSEric Dumazet 2575a47a126aSEric Dumazet if (!counters) 2576a47a126aSEric Dumazet return; 2577a47a126aSEric Dumazet 2578af12346cSWanpeng Li /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ 2579af12346cSWanpeng Li smp_rmb(); 2580af12346cSWanpeng Li if (v->flags & VM_UNINITIALIZED) 2581af12346cSWanpeng Li return; 2582af12346cSWanpeng Li 2583a47a126aSEric Dumazet memset(counters, 0, nr_node_ids * sizeof(unsigned int)); 2584a47a126aSEric Dumazet 2585a47a126aSEric Dumazet for (nr = 0; nr < v->nr_pages; nr++) 2586a47a126aSEric Dumazet counters[page_to_nid(v->pages[nr])]++; 2587a47a126aSEric Dumazet 2588a47a126aSEric Dumazet for_each_node_state(nr, N_HIGH_MEMORY) 2589a47a126aSEric Dumazet if (counters[nr]) 2590a47a126aSEric Dumazet seq_printf(m, " N%u=%u", nr, counters[nr]); 2591a47a126aSEric Dumazet } 2592a47a126aSEric Dumazet } 2593a47a126aSEric Dumazet 2594a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p) 2595a10aa579SChristoph Lameter { 2596d4033afdSJoonsoo Kim struct vmap_area *va = p; 2597d4033afdSJoonsoo Kim struct vm_struct *v; 2598d4033afdSJoonsoo Kim 2599c2ce8c14SWanpeng Li /* 2600c2ce8c14SWanpeng Li * s_show can encounter race with remove_vm_area, !VM_VM_AREA on 2601c2ce8c14SWanpeng Li * behalf of vmap area is being tear down or vm_map_ram allocation. 2602c2ce8c14SWanpeng Li */ 2603c2ce8c14SWanpeng Li if (!(va->flags & VM_VM_AREA)) 2604d4033afdSJoonsoo Kim return 0; 2605d4033afdSJoonsoo Kim 2606d4033afdSJoonsoo Kim v = va->vm; 2607a10aa579SChristoph Lameter 260845ec1690SKees Cook seq_printf(m, "0x%pK-0x%pK %7ld", 2609a10aa579SChristoph Lameter v->addr, v->addr + v->size, v->size); 2610a10aa579SChristoph Lameter 261162c70bceSJoe Perches if (v->caller) 261262c70bceSJoe Perches seq_printf(m, " %pS", v->caller); 261323016969SChristoph Lameter 2614a10aa579SChristoph Lameter if (v->nr_pages) 2615a10aa579SChristoph Lameter seq_printf(m, " pages=%d", v->nr_pages); 2616a10aa579SChristoph Lameter 2617a10aa579SChristoph Lameter if (v->phys_addr) 2618ffa71f33SKenji Kaneshige seq_printf(m, " phys=%llx", (unsigned long long)v->phys_addr); 2619a10aa579SChristoph Lameter 2620a10aa579SChristoph Lameter if (v->flags & VM_IOREMAP) 2621f4527c90SFabian Frederick seq_puts(m, " ioremap"); 2622a10aa579SChristoph Lameter 2623a10aa579SChristoph Lameter if (v->flags & VM_ALLOC) 2624f4527c90SFabian Frederick seq_puts(m, " vmalloc"); 2625a10aa579SChristoph Lameter 2626a10aa579SChristoph Lameter if (v->flags & VM_MAP) 2627f4527c90SFabian Frederick seq_puts(m, " vmap"); 2628a10aa579SChristoph Lameter 2629a10aa579SChristoph Lameter if (v->flags & VM_USERMAP) 2630f4527c90SFabian Frederick seq_puts(m, " user"); 2631a10aa579SChristoph Lameter 2632a10aa579SChristoph Lameter if (v->flags & VM_VPAGES) 2633f4527c90SFabian Frederick seq_puts(m, " vpages"); 2634a10aa579SChristoph Lameter 2635a47a126aSEric Dumazet show_numa_info(m, v); 2636a10aa579SChristoph Lameter seq_putc(m, '\n'); 2637a10aa579SChristoph Lameter return 0; 2638a10aa579SChristoph Lameter } 2639a10aa579SChristoph Lameter 26405f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = { 2641a10aa579SChristoph Lameter .start = s_start, 2642a10aa579SChristoph Lameter .next = s_next, 2643a10aa579SChristoph Lameter .stop = s_stop, 2644a10aa579SChristoph Lameter .show = s_show, 2645a10aa579SChristoph Lameter }; 26465f6a6a9cSAlexey Dobriyan 26475f6a6a9cSAlexey Dobriyan static int vmalloc_open(struct inode *inode, struct file *file) 26485f6a6a9cSAlexey Dobriyan { 2649*703394c1SRob Jones if (IS_ENABLED(CONFIG_NUMA)) 2650*703394c1SRob Jones return seq_open_private(file, &vmalloc_op, 2651*703394c1SRob Jones nr_node_ids * sizeof(unsigned int)); 2652*703394c1SRob Jones else 2653*703394c1SRob Jones return seq_open(file, &vmalloc_op); 26545f6a6a9cSAlexey Dobriyan } 26555f6a6a9cSAlexey Dobriyan 26565f6a6a9cSAlexey Dobriyan static const struct file_operations proc_vmalloc_operations = { 26575f6a6a9cSAlexey Dobriyan .open = vmalloc_open, 26585f6a6a9cSAlexey Dobriyan .read = seq_read, 26595f6a6a9cSAlexey Dobriyan .llseek = seq_lseek, 26605f6a6a9cSAlexey Dobriyan .release = seq_release_private, 26615f6a6a9cSAlexey Dobriyan }; 26625f6a6a9cSAlexey Dobriyan 26635f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void) 26645f6a6a9cSAlexey Dobriyan { 26655f6a6a9cSAlexey Dobriyan proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations); 26665f6a6a9cSAlexey Dobriyan return 0; 26675f6a6a9cSAlexey Dobriyan } 26685f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init); 2669db3808c1SJoonsoo Kim 2670db3808c1SJoonsoo Kim void get_vmalloc_info(struct vmalloc_info *vmi) 2671db3808c1SJoonsoo Kim { 2672f98782ddSJoonsoo Kim struct vmap_area *va; 2673db3808c1SJoonsoo Kim unsigned long free_area_size; 2674db3808c1SJoonsoo Kim unsigned long prev_end; 2675db3808c1SJoonsoo Kim 2676db3808c1SJoonsoo Kim vmi->used = 0; 2677db3808c1SJoonsoo Kim vmi->largest_chunk = 0; 2678db3808c1SJoonsoo Kim 2679db3808c1SJoonsoo Kim prev_end = VMALLOC_START; 2680db3808c1SJoonsoo Kim 2681474750abSJoonsoo Kim rcu_read_lock(); 2682db3808c1SJoonsoo Kim 2683f98782ddSJoonsoo Kim if (list_empty(&vmap_area_list)) { 2684f98782ddSJoonsoo Kim vmi->largest_chunk = VMALLOC_TOTAL; 2685f98782ddSJoonsoo Kim goto out; 2686f98782ddSJoonsoo Kim } 2687f98782ddSJoonsoo Kim 2688474750abSJoonsoo Kim list_for_each_entry_rcu(va, &vmap_area_list, list) { 2689f98782ddSJoonsoo Kim unsigned long addr = va->va_start; 2690db3808c1SJoonsoo Kim 2691db3808c1SJoonsoo Kim /* 2692f98782ddSJoonsoo Kim * Some archs keep another range for modules in vmalloc space 2693db3808c1SJoonsoo Kim */ 2694db3808c1SJoonsoo Kim if (addr < VMALLOC_START) 2695db3808c1SJoonsoo Kim continue; 2696db3808c1SJoonsoo Kim if (addr >= VMALLOC_END) 2697db3808c1SJoonsoo Kim break; 2698db3808c1SJoonsoo Kim 2699f98782ddSJoonsoo Kim if (va->flags & (VM_LAZY_FREE | VM_LAZY_FREEING)) 2700f98782ddSJoonsoo Kim continue; 2701f98782ddSJoonsoo Kim 2702f98782ddSJoonsoo Kim vmi->used += (va->va_end - va->va_start); 2703db3808c1SJoonsoo Kim 2704db3808c1SJoonsoo Kim free_area_size = addr - prev_end; 2705db3808c1SJoonsoo Kim if (vmi->largest_chunk < free_area_size) 2706db3808c1SJoonsoo Kim vmi->largest_chunk = free_area_size; 2707db3808c1SJoonsoo Kim 2708f98782ddSJoonsoo Kim prev_end = va->va_end; 2709db3808c1SJoonsoo Kim } 2710db3808c1SJoonsoo Kim 2711db3808c1SJoonsoo Kim if (VMALLOC_END - prev_end > vmi->largest_chunk) 2712db3808c1SJoonsoo Kim vmi->largest_chunk = VMALLOC_END - prev_end; 2713db3808c1SJoonsoo Kim 2714f98782ddSJoonsoo Kim out: 2715474750abSJoonsoo Kim rcu_read_unlock(); 2716db3808c1SJoonsoo Kim } 2717a10aa579SChristoph Lameter #endif 2718a10aa579SChristoph Lameter 2719