1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/mm/vmalloc.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1993 Linus Torvalds 61da177e4SLinus Torvalds * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 71da177e4SLinus Torvalds * SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000 81da177e4SLinus Torvalds * Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002 9930fc45aSChristoph Lameter * Numa awareness, Christoph Lameter, SGI, June 2005 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 12db64fe02SNick Piggin #include <linux/vmalloc.h> 131da177e4SLinus Torvalds #include <linux/mm.h> 141da177e4SLinus Torvalds #include <linux/module.h> 151da177e4SLinus Torvalds #include <linux/highmem.h> 16c3edc401SIngo Molnar #include <linux/sched/signal.h> 171da177e4SLinus Torvalds #include <linux/slab.h> 181da177e4SLinus Torvalds #include <linux/spinlock.h> 191da177e4SLinus Torvalds #include <linux/interrupt.h> 205f6a6a9cSAlexey Dobriyan #include <linux/proc_fs.h> 21a10aa579SChristoph Lameter #include <linux/seq_file.h> 22868b104dSRick Edgecombe #include <linux/set_memory.h> 233ac7fe5aSThomas Gleixner #include <linux/debugobjects.h> 2423016969SChristoph Lameter #include <linux/kallsyms.h> 25db64fe02SNick Piggin #include <linux/list.h> 264da56b99SChris Wilson #include <linux/notifier.h> 27db64fe02SNick Piggin #include <linux/rbtree.h> 28db64fe02SNick Piggin #include <linux/radix-tree.h> 29db64fe02SNick Piggin #include <linux/rcupdate.h> 30f0aa6617STejun Heo #include <linux/pfn.h> 3189219d37SCatalin Marinas #include <linux/kmemleak.h> 3260063497SArun Sharma #include <linux/atomic.h> 333b32123dSGideon Israel Dsouza #include <linux/compiler.h> 3432fcfd40SAl Viro #include <linux/llist.h> 350f616be1SToshi Kani #include <linux/bitops.h> 3668ad4a33SUladzislau Rezki (Sony) #include <linux/rbtree_augmented.h> 373b32123dSGideon Israel Dsouza 387c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 391da177e4SLinus Torvalds #include <asm/tlbflush.h> 402dca6999SDavid Miller #include <asm/shmparam.h> 411da177e4SLinus Torvalds 42dd56b046SMel Gorman #include "internal.h" 43dd56b046SMel Gorman 4432fcfd40SAl Viro struct vfree_deferred { 4532fcfd40SAl Viro struct llist_head list; 4632fcfd40SAl Viro struct work_struct wq; 4732fcfd40SAl Viro }; 4832fcfd40SAl Viro static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred); 4932fcfd40SAl Viro 5032fcfd40SAl Viro static void __vunmap(const void *, int); 5132fcfd40SAl Viro 5232fcfd40SAl Viro static void free_work(struct work_struct *w) 5332fcfd40SAl Viro { 5432fcfd40SAl Viro struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq); 55894e58c1SByungchul Park struct llist_node *t, *llnode; 56894e58c1SByungchul Park 57894e58c1SByungchul Park llist_for_each_safe(llnode, t, llist_del_all(&p->list)) 58894e58c1SByungchul Park __vunmap((void *)llnode, 1); 5932fcfd40SAl Viro } 6032fcfd40SAl Viro 61db64fe02SNick Piggin /*** Page table manipulation functions ***/ 62b221385bSAdrian Bunk 631da177e4SLinus Torvalds static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end) 641da177e4SLinus Torvalds { 651da177e4SLinus Torvalds pte_t *pte; 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds pte = pte_offset_kernel(pmd, addr); 681da177e4SLinus Torvalds do { 691da177e4SLinus Torvalds pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte); 701da177e4SLinus Torvalds WARN_ON(!pte_none(ptent) && !pte_present(ptent)); 711da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds 74db64fe02SNick Piggin static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end) 751da177e4SLinus Torvalds { 761da177e4SLinus Torvalds pmd_t *pmd; 771da177e4SLinus Torvalds unsigned long next; 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 801da177e4SLinus Torvalds do { 811da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 82b9820d8fSToshi Kani if (pmd_clear_huge(pmd)) 83b9820d8fSToshi Kani continue; 841da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 851da177e4SLinus Torvalds continue; 861da177e4SLinus Torvalds vunmap_pte_range(pmd, addr, next); 871da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds 90c2febafcSKirill A. Shutemov static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end) 911da177e4SLinus Torvalds { 921da177e4SLinus Torvalds pud_t *pud; 931da177e4SLinus Torvalds unsigned long next; 941da177e4SLinus Torvalds 95c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 961da177e4SLinus Torvalds do { 971da177e4SLinus Torvalds next = pud_addr_end(addr, end); 98b9820d8fSToshi Kani if (pud_clear_huge(pud)) 99b9820d8fSToshi Kani continue; 1001da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 1011da177e4SLinus Torvalds continue; 1021da177e4SLinus Torvalds vunmap_pmd_range(pud, addr, next); 1031da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 106c2febafcSKirill A. Shutemov static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end) 107c2febafcSKirill A. Shutemov { 108c2febafcSKirill A. Shutemov p4d_t *p4d; 109c2febafcSKirill A. Shutemov unsigned long next; 110c2febafcSKirill A. Shutemov 111c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 112c2febafcSKirill A. Shutemov do { 113c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 114c2febafcSKirill A. Shutemov if (p4d_clear_huge(p4d)) 115c2febafcSKirill A. Shutemov continue; 116c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 117c2febafcSKirill A. Shutemov continue; 118c2febafcSKirill A. Shutemov vunmap_pud_range(p4d, addr, next); 119c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 120c2febafcSKirill A. Shutemov } 121c2febafcSKirill A. Shutemov 122db64fe02SNick Piggin static void vunmap_page_range(unsigned long addr, unsigned long end) 1231da177e4SLinus Torvalds { 1241da177e4SLinus Torvalds pgd_t *pgd; 1251da177e4SLinus Torvalds unsigned long next; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds BUG_ON(addr >= end); 1281da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 1291da177e4SLinus Torvalds do { 1301da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 1311da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 1321da177e4SLinus Torvalds continue; 133c2febafcSKirill A. Shutemov vunmap_p4d_range(pgd, addr, next); 1341da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds static int vmap_pte_range(pmd_t *pmd, unsigned long addr, 138db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1391da177e4SLinus Torvalds { 1401da177e4SLinus Torvalds pte_t *pte; 1411da177e4SLinus Torvalds 142db64fe02SNick Piggin /* 143db64fe02SNick Piggin * nr is a running index into the array which helps higher level 144db64fe02SNick Piggin * callers keep track of where we're up to. 145db64fe02SNick Piggin */ 146db64fe02SNick Piggin 147872fec16SHugh Dickins pte = pte_alloc_kernel(pmd, addr); 1481da177e4SLinus Torvalds if (!pte) 1491da177e4SLinus Torvalds return -ENOMEM; 1501da177e4SLinus Torvalds do { 151db64fe02SNick Piggin struct page *page = pages[*nr]; 152db64fe02SNick Piggin 153db64fe02SNick Piggin if (WARN_ON(!pte_none(*pte))) 154db64fe02SNick Piggin return -EBUSY; 155db64fe02SNick Piggin if (WARN_ON(!page)) 1561da177e4SLinus Torvalds return -ENOMEM; 1571da177e4SLinus Torvalds set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); 158db64fe02SNick Piggin (*nr)++; 1591da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 1601da177e4SLinus Torvalds return 0; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 163db64fe02SNick Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr, 164db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1651da177e4SLinus Torvalds { 1661da177e4SLinus Torvalds pmd_t *pmd; 1671da177e4SLinus Torvalds unsigned long next; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds pmd = pmd_alloc(&init_mm, pud, addr); 1701da177e4SLinus Torvalds if (!pmd) 1711da177e4SLinus Torvalds return -ENOMEM; 1721da177e4SLinus Torvalds do { 1731da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 174db64fe02SNick Piggin if (vmap_pte_range(pmd, addr, next, prot, pages, nr)) 1751da177e4SLinus Torvalds return -ENOMEM; 1761da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 1771da177e4SLinus Torvalds return 0; 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 180c2febafcSKirill A. Shutemov static int vmap_pud_range(p4d_t *p4d, unsigned long addr, 181db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1821da177e4SLinus Torvalds { 1831da177e4SLinus Torvalds pud_t *pud; 1841da177e4SLinus Torvalds unsigned long next; 1851da177e4SLinus Torvalds 186c2febafcSKirill A. Shutemov pud = pud_alloc(&init_mm, p4d, addr); 1871da177e4SLinus Torvalds if (!pud) 1881da177e4SLinus Torvalds return -ENOMEM; 1891da177e4SLinus Torvalds do { 1901da177e4SLinus Torvalds next = pud_addr_end(addr, end); 191db64fe02SNick Piggin if (vmap_pmd_range(pud, addr, next, prot, pages, nr)) 1921da177e4SLinus Torvalds return -ENOMEM; 1931da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 1941da177e4SLinus Torvalds return 0; 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 197c2febafcSKirill A. Shutemov static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, 198c2febafcSKirill A. Shutemov unsigned long end, pgprot_t prot, struct page **pages, int *nr) 199c2febafcSKirill A. Shutemov { 200c2febafcSKirill A. Shutemov p4d_t *p4d; 201c2febafcSKirill A. Shutemov unsigned long next; 202c2febafcSKirill A. Shutemov 203c2febafcSKirill A. Shutemov p4d = p4d_alloc(&init_mm, pgd, addr); 204c2febafcSKirill A. Shutemov if (!p4d) 205c2febafcSKirill A. Shutemov return -ENOMEM; 206c2febafcSKirill A. Shutemov do { 207c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 208c2febafcSKirill A. Shutemov if (vmap_pud_range(p4d, addr, next, prot, pages, nr)) 209c2febafcSKirill A. Shutemov return -ENOMEM; 210c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 211c2febafcSKirill A. Shutemov return 0; 212c2febafcSKirill A. Shutemov } 213c2febafcSKirill A. Shutemov 214db64fe02SNick Piggin /* 215db64fe02SNick Piggin * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and 216db64fe02SNick Piggin * will have pfns corresponding to the "pages" array. 217db64fe02SNick Piggin * 218db64fe02SNick Piggin * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N] 219db64fe02SNick Piggin */ 2208fc48985STejun Heo static int vmap_page_range_noflush(unsigned long start, unsigned long end, 221db64fe02SNick Piggin pgprot_t prot, struct page **pages) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds pgd_t *pgd; 2241da177e4SLinus Torvalds unsigned long next; 2252e4e27c7SAdam Lackorzynski unsigned long addr = start; 226db64fe02SNick Piggin int err = 0; 227db64fe02SNick Piggin int nr = 0; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds BUG_ON(addr >= end); 2301da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 2311da177e4SLinus Torvalds do { 2321da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 233c2febafcSKirill A. Shutemov err = vmap_p4d_range(pgd, addr, next, prot, pages, &nr); 2341da177e4SLinus Torvalds if (err) 235bf88c8c8SFigo.zhang return err; 2361da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 237db64fe02SNick Piggin 238db64fe02SNick Piggin return nr; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds 2418fc48985STejun Heo static int vmap_page_range(unsigned long start, unsigned long end, 2428fc48985STejun Heo pgprot_t prot, struct page **pages) 2438fc48985STejun Heo { 2448fc48985STejun Heo int ret; 2458fc48985STejun Heo 2468fc48985STejun Heo ret = vmap_page_range_noflush(start, end, prot, pages); 2478fc48985STejun Heo flush_cache_vmap(start, end); 2488fc48985STejun Heo return ret; 2498fc48985STejun Heo } 2508fc48985STejun Heo 25181ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x) 25273bdf0a6SLinus Torvalds { 25373bdf0a6SLinus Torvalds /* 254ab4f2ee1SRussell King * ARM, x86-64 and sparc64 put modules in a special place, 25573bdf0a6SLinus Torvalds * and fall back on vmalloc() if that fails. Others 25673bdf0a6SLinus Torvalds * just put it in the vmalloc space. 25773bdf0a6SLinus Torvalds */ 25873bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) 25973bdf0a6SLinus Torvalds unsigned long addr = (unsigned long)x; 26073bdf0a6SLinus Torvalds if (addr >= MODULES_VADDR && addr < MODULES_END) 26173bdf0a6SLinus Torvalds return 1; 26273bdf0a6SLinus Torvalds #endif 26373bdf0a6SLinus Torvalds return is_vmalloc_addr(x); 26473bdf0a6SLinus Torvalds } 26573bdf0a6SLinus Torvalds 26648667e7aSChristoph Lameter /* 267add688fbSmalc * Walk a vmap address to the struct page it maps. 26848667e7aSChristoph Lameter */ 269add688fbSmalc struct page *vmalloc_to_page(const void *vmalloc_addr) 27048667e7aSChristoph Lameter { 27148667e7aSChristoph Lameter unsigned long addr = (unsigned long) vmalloc_addr; 272add688fbSmalc struct page *page = NULL; 27348667e7aSChristoph Lameter pgd_t *pgd = pgd_offset_k(addr); 274c2febafcSKirill A. Shutemov p4d_t *p4d; 275c2febafcSKirill A. Shutemov pud_t *pud; 276c2febafcSKirill A. Shutemov pmd_t *pmd; 277c2febafcSKirill A. Shutemov pte_t *ptep, pte; 27848667e7aSChristoph Lameter 2797aa413deSIngo Molnar /* 2807aa413deSIngo Molnar * XXX we might need to change this if we add VIRTUAL_BUG_ON for 2817aa413deSIngo Molnar * architectures that do not vmalloc module space 2827aa413deSIngo Molnar */ 28373bdf0a6SLinus Torvalds VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); 28459ea7463SJiri Slaby 285c2febafcSKirill A. Shutemov if (pgd_none(*pgd)) 286c2febafcSKirill A. Shutemov return NULL; 287c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 288c2febafcSKirill A. Shutemov if (p4d_none(*p4d)) 289c2febafcSKirill A. Shutemov return NULL; 290c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 291029c54b0SArd Biesheuvel 292029c54b0SArd Biesheuvel /* 293029c54b0SArd Biesheuvel * Don't dereference bad PUD or PMD (below) entries. This will also 294029c54b0SArd Biesheuvel * identify huge mappings, which we may encounter on architectures 295029c54b0SArd Biesheuvel * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be 296029c54b0SArd Biesheuvel * identified as vmalloc addresses by is_vmalloc_addr(), but are 297029c54b0SArd Biesheuvel * not [unambiguously] associated with a struct page, so there is 298029c54b0SArd Biesheuvel * no correct value to return for them. 299029c54b0SArd Biesheuvel */ 300029c54b0SArd Biesheuvel WARN_ON_ONCE(pud_bad(*pud)); 301029c54b0SArd Biesheuvel if (pud_none(*pud) || pud_bad(*pud)) 302c2febafcSKirill A. Shutemov return NULL; 303c2febafcSKirill A. Shutemov pmd = pmd_offset(pud, addr); 304029c54b0SArd Biesheuvel WARN_ON_ONCE(pmd_bad(*pmd)); 305029c54b0SArd Biesheuvel if (pmd_none(*pmd) || pmd_bad(*pmd)) 306c2febafcSKirill A. Shutemov return NULL; 307db64fe02SNick Piggin 30848667e7aSChristoph Lameter ptep = pte_offset_map(pmd, addr); 30948667e7aSChristoph Lameter pte = *ptep; 31048667e7aSChristoph Lameter if (pte_present(pte)) 311add688fbSmalc page = pte_page(pte); 31248667e7aSChristoph Lameter pte_unmap(ptep); 313add688fbSmalc return page; 314ece86e22SJianyu Zhan } 315ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page); 316ece86e22SJianyu Zhan 317add688fbSmalc /* 318add688fbSmalc * Map a vmalloc()-space virtual address to the physical page frame number. 319add688fbSmalc */ 320add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr) 321add688fbSmalc { 322add688fbSmalc return page_to_pfn(vmalloc_to_page(vmalloc_addr)); 323add688fbSmalc } 324add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn); 325add688fbSmalc 326db64fe02SNick Piggin 327db64fe02SNick Piggin /*** Global kva allocator ***/ 328db64fe02SNick Piggin 329bb850f4dSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_PROPAGATE_CHECK 0 330a6cf4e0fSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0 331bb850f4dSUladzislau Rezki (Sony) 332db64fe02SNick Piggin 333db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock); 334f1c4069eSJoonsoo Kim /* Export for kexec only */ 335f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list); 33680c4bd7aSChris Wilson static LLIST_HEAD(vmap_purge_list); 33789699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT; 33868ad4a33SUladzislau Rezki (Sony) static bool vmap_initialized __read_mostly; 33989699605SNick Piggin 34068ad4a33SUladzislau Rezki (Sony) /* 34168ad4a33SUladzislau Rezki (Sony) * This kmem_cache is used for vmap_area objects. Instead of 34268ad4a33SUladzislau Rezki (Sony) * allocating from slab we reuse an object from this cache to 34368ad4a33SUladzislau Rezki (Sony) * make things faster. Especially in "no edge" splitting of 34468ad4a33SUladzislau Rezki (Sony) * free block. 34568ad4a33SUladzislau Rezki (Sony) */ 34668ad4a33SUladzislau Rezki (Sony) static struct kmem_cache *vmap_area_cachep; 34789699605SNick Piggin 34868ad4a33SUladzislau Rezki (Sony) /* 34968ad4a33SUladzislau Rezki (Sony) * This linked list is used in pair with free_vmap_area_root. 35068ad4a33SUladzislau Rezki (Sony) * It gives O(1) access to prev/next to perform fast coalescing. 35168ad4a33SUladzislau Rezki (Sony) */ 35268ad4a33SUladzislau Rezki (Sony) static LIST_HEAD(free_vmap_area_list); 35368ad4a33SUladzislau Rezki (Sony) 35468ad4a33SUladzislau Rezki (Sony) /* 35568ad4a33SUladzislau Rezki (Sony) * This augment red-black tree represents the free vmap space. 35668ad4a33SUladzislau Rezki (Sony) * All vmap_area objects in this tree are sorted by va->va_start 35768ad4a33SUladzislau Rezki (Sony) * address. It is used for allocation and merging when a vmap 35868ad4a33SUladzislau Rezki (Sony) * object is released. 35968ad4a33SUladzislau Rezki (Sony) * 36068ad4a33SUladzislau Rezki (Sony) * Each vmap_area node contains a maximum available free block 36168ad4a33SUladzislau Rezki (Sony) * of its sub-tree, right or left. Therefore it is possible to 36268ad4a33SUladzislau Rezki (Sony) * find a lowest match of free area. 36368ad4a33SUladzislau Rezki (Sony) */ 36468ad4a33SUladzislau Rezki (Sony) static struct rb_root free_vmap_area_root = RB_ROOT; 36568ad4a33SUladzislau Rezki (Sony) 36682dd23e8SUladzislau Rezki (Sony) /* 36782dd23e8SUladzislau Rezki (Sony) * Preload a CPU with one object for "no edge" split case. The 36882dd23e8SUladzislau Rezki (Sony) * aim is to get rid of allocations from the atomic context, thus 36982dd23e8SUladzislau Rezki (Sony) * to use more permissive allocation masks. 37082dd23e8SUladzislau Rezki (Sony) */ 37182dd23e8SUladzislau Rezki (Sony) static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node); 37282dd23e8SUladzislau Rezki (Sony) 37368ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 37468ad4a33SUladzislau Rezki (Sony) va_size(struct vmap_area *va) 37568ad4a33SUladzislau Rezki (Sony) { 37668ad4a33SUladzislau Rezki (Sony) return (va->va_end - va->va_start); 37768ad4a33SUladzislau Rezki (Sony) } 37868ad4a33SUladzislau Rezki (Sony) 37968ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 38068ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(struct rb_node *node) 38168ad4a33SUladzislau Rezki (Sony) { 38268ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 38368ad4a33SUladzislau Rezki (Sony) 38468ad4a33SUladzislau Rezki (Sony) va = rb_entry_safe(node, struct vmap_area, rb_node); 38568ad4a33SUladzislau Rezki (Sony) return va ? va->subtree_max_size : 0; 38668ad4a33SUladzislau Rezki (Sony) } 38768ad4a33SUladzislau Rezki (Sony) 38868ad4a33SUladzislau Rezki (Sony) /* 38968ad4a33SUladzislau Rezki (Sony) * Gets called when remove the node and rotate. 39068ad4a33SUladzislau Rezki (Sony) */ 39168ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 39268ad4a33SUladzislau Rezki (Sony) compute_subtree_max_size(struct vmap_area *va) 39368ad4a33SUladzislau Rezki (Sony) { 39468ad4a33SUladzislau Rezki (Sony) return max3(va_size(va), 39568ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(va->rb_node.rb_left), 39668ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(va->rb_node.rb_right)); 39768ad4a33SUladzislau Rezki (Sony) } 39868ad4a33SUladzislau Rezki (Sony) 399*315cc066SMichel Lespinasse RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb, 400*315cc066SMichel Lespinasse struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size) 40168ad4a33SUladzislau Rezki (Sony) 40268ad4a33SUladzislau Rezki (Sony) static void purge_vmap_area_lazy(void); 40368ad4a33SUladzislau Rezki (Sony) static BLOCKING_NOTIFIER_HEAD(vmap_notify_list); 40468ad4a33SUladzislau Rezki (Sony) static unsigned long lazy_max_pages(void); 405db64fe02SNick Piggin 40697105f0aSRoman Gushchin static atomic_long_t nr_vmalloc_pages; 40797105f0aSRoman Gushchin 40897105f0aSRoman Gushchin unsigned long vmalloc_nr_pages(void) 40997105f0aSRoman Gushchin { 41097105f0aSRoman Gushchin return atomic_long_read(&nr_vmalloc_pages); 41197105f0aSRoman Gushchin } 41297105f0aSRoman Gushchin 413db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr) 4141da177e4SLinus Torvalds { 415db64fe02SNick Piggin struct rb_node *n = vmap_area_root.rb_node; 416db64fe02SNick Piggin 417db64fe02SNick Piggin while (n) { 418db64fe02SNick Piggin struct vmap_area *va; 419db64fe02SNick Piggin 420db64fe02SNick Piggin va = rb_entry(n, struct vmap_area, rb_node); 421db64fe02SNick Piggin if (addr < va->va_start) 422db64fe02SNick Piggin n = n->rb_left; 423cef2ac3fSHATAYAMA Daisuke else if (addr >= va->va_end) 424db64fe02SNick Piggin n = n->rb_right; 425db64fe02SNick Piggin else 426db64fe02SNick Piggin return va; 427db64fe02SNick Piggin } 428db64fe02SNick Piggin 429db64fe02SNick Piggin return NULL; 430db64fe02SNick Piggin } 431db64fe02SNick Piggin 43268ad4a33SUladzislau Rezki (Sony) /* 43368ad4a33SUladzislau Rezki (Sony) * This function returns back addresses of parent node 43468ad4a33SUladzislau Rezki (Sony) * and its left or right link for further processing. 43568ad4a33SUladzislau Rezki (Sony) */ 43668ad4a33SUladzislau Rezki (Sony) static __always_inline struct rb_node ** 43768ad4a33SUladzislau Rezki (Sony) find_va_links(struct vmap_area *va, 43868ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct rb_node *from, 43968ad4a33SUladzislau Rezki (Sony) struct rb_node **parent) 440db64fe02SNick Piggin { 441170168d0SNamhyung Kim struct vmap_area *tmp_va; 44268ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 443db64fe02SNick Piggin 44468ad4a33SUladzislau Rezki (Sony) if (root) { 44568ad4a33SUladzislau Rezki (Sony) link = &root->rb_node; 44668ad4a33SUladzislau Rezki (Sony) if (unlikely(!*link)) { 44768ad4a33SUladzislau Rezki (Sony) *parent = NULL; 44868ad4a33SUladzislau Rezki (Sony) return link; 44968ad4a33SUladzislau Rezki (Sony) } 45068ad4a33SUladzislau Rezki (Sony) } else { 45168ad4a33SUladzislau Rezki (Sony) link = &from; 45268ad4a33SUladzislau Rezki (Sony) } 45368ad4a33SUladzislau Rezki (Sony) 45468ad4a33SUladzislau Rezki (Sony) /* 45568ad4a33SUladzislau Rezki (Sony) * Go to the bottom of the tree. When we hit the last point 45668ad4a33SUladzislau Rezki (Sony) * we end up with parent rb_node and correct direction, i name 45768ad4a33SUladzislau Rezki (Sony) * it link, where the new va->rb_node will be attached to. 45868ad4a33SUladzislau Rezki (Sony) */ 45968ad4a33SUladzislau Rezki (Sony) do { 46068ad4a33SUladzislau Rezki (Sony) tmp_va = rb_entry(*link, struct vmap_area, rb_node); 46168ad4a33SUladzislau Rezki (Sony) 46268ad4a33SUladzislau Rezki (Sony) /* 46368ad4a33SUladzislau Rezki (Sony) * During the traversal we also do some sanity check. 46468ad4a33SUladzislau Rezki (Sony) * Trigger the BUG() if there are sides(left/right) 46568ad4a33SUladzislau Rezki (Sony) * or full overlaps. 46668ad4a33SUladzislau Rezki (Sony) */ 46768ad4a33SUladzislau Rezki (Sony) if (va->va_start < tmp_va->va_end && 46868ad4a33SUladzislau Rezki (Sony) va->va_end <= tmp_va->va_start) 46968ad4a33SUladzislau Rezki (Sony) link = &(*link)->rb_left; 47068ad4a33SUladzislau Rezki (Sony) else if (va->va_end > tmp_va->va_start && 47168ad4a33SUladzislau Rezki (Sony) va->va_start >= tmp_va->va_end) 47268ad4a33SUladzislau Rezki (Sony) link = &(*link)->rb_right; 473db64fe02SNick Piggin else 474db64fe02SNick Piggin BUG(); 47568ad4a33SUladzislau Rezki (Sony) } while (*link); 47668ad4a33SUladzislau Rezki (Sony) 47768ad4a33SUladzislau Rezki (Sony) *parent = &tmp_va->rb_node; 47868ad4a33SUladzislau Rezki (Sony) return link; 479db64fe02SNick Piggin } 480db64fe02SNick Piggin 48168ad4a33SUladzislau Rezki (Sony) static __always_inline struct list_head * 48268ad4a33SUladzislau Rezki (Sony) get_va_next_sibling(struct rb_node *parent, struct rb_node **link) 48368ad4a33SUladzislau Rezki (Sony) { 48468ad4a33SUladzislau Rezki (Sony) struct list_head *list; 485db64fe02SNick Piggin 48668ad4a33SUladzislau Rezki (Sony) if (unlikely(!parent)) 48768ad4a33SUladzislau Rezki (Sony) /* 48868ad4a33SUladzislau Rezki (Sony) * The red-black tree where we try to find VA neighbors 48968ad4a33SUladzislau Rezki (Sony) * before merging or inserting is empty, i.e. it means 49068ad4a33SUladzislau Rezki (Sony) * there is no free vmap space. Normally it does not 49168ad4a33SUladzislau Rezki (Sony) * happen but we handle this case anyway. 49268ad4a33SUladzislau Rezki (Sony) */ 49368ad4a33SUladzislau Rezki (Sony) return NULL; 49468ad4a33SUladzislau Rezki (Sony) 49568ad4a33SUladzislau Rezki (Sony) list = &rb_entry(parent, struct vmap_area, rb_node)->list; 49668ad4a33SUladzislau Rezki (Sony) return (&parent->rb_right == link ? list->next : list); 497db64fe02SNick Piggin } 498db64fe02SNick Piggin 49968ad4a33SUladzislau Rezki (Sony) static __always_inline void 50068ad4a33SUladzislau Rezki (Sony) link_va(struct vmap_area *va, struct rb_root *root, 50168ad4a33SUladzislau Rezki (Sony) struct rb_node *parent, struct rb_node **link, struct list_head *head) 50268ad4a33SUladzislau Rezki (Sony) { 50368ad4a33SUladzislau Rezki (Sony) /* 50468ad4a33SUladzislau Rezki (Sony) * VA is still not in the list, but we can 50568ad4a33SUladzislau Rezki (Sony) * identify its future previous list_head node. 50668ad4a33SUladzislau Rezki (Sony) */ 50768ad4a33SUladzislau Rezki (Sony) if (likely(parent)) { 50868ad4a33SUladzislau Rezki (Sony) head = &rb_entry(parent, struct vmap_area, rb_node)->list; 50968ad4a33SUladzislau Rezki (Sony) if (&parent->rb_right != link) 51068ad4a33SUladzislau Rezki (Sony) head = head->prev; 51168ad4a33SUladzislau Rezki (Sony) } 512db64fe02SNick Piggin 51368ad4a33SUladzislau Rezki (Sony) /* Insert to the rb-tree */ 51468ad4a33SUladzislau Rezki (Sony) rb_link_node(&va->rb_node, parent, link); 51568ad4a33SUladzislau Rezki (Sony) if (root == &free_vmap_area_root) { 51668ad4a33SUladzislau Rezki (Sony) /* 51768ad4a33SUladzislau Rezki (Sony) * Some explanation here. Just perform simple insertion 51868ad4a33SUladzislau Rezki (Sony) * to the tree. We do not set va->subtree_max_size to 51968ad4a33SUladzislau Rezki (Sony) * its current size before calling rb_insert_augmented(). 52068ad4a33SUladzislau Rezki (Sony) * It is because of we populate the tree from the bottom 52168ad4a33SUladzislau Rezki (Sony) * to parent levels when the node _is_ in the tree. 52268ad4a33SUladzislau Rezki (Sony) * 52368ad4a33SUladzislau Rezki (Sony) * Therefore we set subtree_max_size to zero after insertion, 52468ad4a33SUladzislau Rezki (Sony) * to let __augment_tree_propagate_from() puts everything to 52568ad4a33SUladzislau Rezki (Sony) * the correct order later on. 52668ad4a33SUladzislau Rezki (Sony) */ 52768ad4a33SUladzislau Rezki (Sony) rb_insert_augmented(&va->rb_node, 52868ad4a33SUladzislau Rezki (Sony) root, &free_vmap_area_rb_augment_cb); 52968ad4a33SUladzislau Rezki (Sony) va->subtree_max_size = 0; 53068ad4a33SUladzislau Rezki (Sony) } else { 53168ad4a33SUladzislau Rezki (Sony) rb_insert_color(&va->rb_node, root); 53268ad4a33SUladzislau Rezki (Sony) } 53368ad4a33SUladzislau Rezki (Sony) 53468ad4a33SUladzislau Rezki (Sony) /* Address-sort this list */ 53568ad4a33SUladzislau Rezki (Sony) list_add(&va->list, head); 53668ad4a33SUladzislau Rezki (Sony) } 53768ad4a33SUladzislau Rezki (Sony) 53868ad4a33SUladzislau Rezki (Sony) static __always_inline void 53968ad4a33SUladzislau Rezki (Sony) unlink_va(struct vmap_area *va, struct rb_root *root) 54068ad4a33SUladzislau Rezki (Sony) { 541460e42d1SUladzislau Rezki (Sony) if (WARN_ON(RB_EMPTY_NODE(&va->rb_node))) 542460e42d1SUladzislau Rezki (Sony) return; 543460e42d1SUladzislau Rezki (Sony) 54468ad4a33SUladzislau Rezki (Sony) if (root == &free_vmap_area_root) 54568ad4a33SUladzislau Rezki (Sony) rb_erase_augmented(&va->rb_node, 54668ad4a33SUladzislau Rezki (Sony) root, &free_vmap_area_rb_augment_cb); 54768ad4a33SUladzislau Rezki (Sony) else 54868ad4a33SUladzislau Rezki (Sony) rb_erase(&va->rb_node, root); 54968ad4a33SUladzislau Rezki (Sony) 55068ad4a33SUladzislau Rezki (Sony) list_del(&va->list); 55168ad4a33SUladzislau Rezki (Sony) RB_CLEAR_NODE(&va->rb_node); 55268ad4a33SUladzislau Rezki (Sony) } 55368ad4a33SUladzislau Rezki (Sony) 554bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK 555bb850f4dSUladzislau Rezki (Sony) static void 556bb850f4dSUladzislau Rezki (Sony) augment_tree_propagate_check(struct rb_node *n) 557bb850f4dSUladzislau Rezki (Sony) { 558bb850f4dSUladzislau Rezki (Sony) struct vmap_area *va; 559bb850f4dSUladzislau Rezki (Sony) struct rb_node *node; 560bb850f4dSUladzislau Rezki (Sony) unsigned long size; 561bb850f4dSUladzislau Rezki (Sony) bool found = false; 562bb850f4dSUladzislau Rezki (Sony) 563bb850f4dSUladzislau Rezki (Sony) if (n == NULL) 564bb850f4dSUladzislau Rezki (Sony) return; 565bb850f4dSUladzislau Rezki (Sony) 566bb850f4dSUladzislau Rezki (Sony) va = rb_entry(n, struct vmap_area, rb_node); 567bb850f4dSUladzislau Rezki (Sony) size = va->subtree_max_size; 568bb850f4dSUladzislau Rezki (Sony) node = n; 569bb850f4dSUladzislau Rezki (Sony) 570bb850f4dSUladzislau Rezki (Sony) while (node) { 571bb850f4dSUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 572bb850f4dSUladzislau Rezki (Sony) 573bb850f4dSUladzislau Rezki (Sony) if (get_subtree_max_size(node->rb_left) == size) { 574bb850f4dSUladzislau Rezki (Sony) node = node->rb_left; 575bb850f4dSUladzislau Rezki (Sony) } else { 576bb850f4dSUladzislau Rezki (Sony) if (va_size(va) == size) { 577bb850f4dSUladzislau Rezki (Sony) found = true; 578bb850f4dSUladzislau Rezki (Sony) break; 579bb850f4dSUladzislau Rezki (Sony) } 580bb850f4dSUladzislau Rezki (Sony) 581bb850f4dSUladzislau Rezki (Sony) node = node->rb_right; 582bb850f4dSUladzislau Rezki (Sony) } 583bb850f4dSUladzislau Rezki (Sony) } 584bb850f4dSUladzislau Rezki (Sony) 585bb850f4dSUladzislau Rezki (Sony) if (!found) { 586bb850f4dSUladzislau Rezki (Sony) va = rb_entry(n, struct vmap_area, rb_node); 587bb850f4dSUladzislau Rezki (Sony) pr_emerg("tree is corrupted: %lu, %lu\n", 588bb850f4dSUladzislau Rezki (Sony) va_size(va), va->subtree_max_size); 589bb850f4dSUladzislau Rezki (Sony) } 590bb850f4dSUladzislau Rezki (Sony) 591bb850f4dSUladzislau Rezki (Sony) augment_tree_propagate_check(n->rb_left); 592bb850f4dSUladzislau Rezki (Sony) augment_tree_propagate_check(n->rb_right); 593bb850f4dSUladzislau Rezki (Sony) } 594bb850f4dSUladzislau Rezki (Sony) #endif 595bb850f4dSUladzislau Rezki (Sony) 59668ad4a33SUladzislau Rezki (Sony) /* 59768ad4a33SUladzislau Rezki (Sony) * This function populates subtree_max_size from bottom to upper 59868ad4a33SUladzislau Rezki (Sony) * levels starting from VA point. The propagation must be done 59968ad4a33SUladzislau Rezki (Sony) * when VA size is modified by changing its va_start/va_end. Or 60068ad4a33SUladzislau Rezki (Sony) * in case of newly inserting of VA to the tree. 60168ad4a33SUladzislau Rezki (Sony) * 60268ad4a33SUladzislau Rezki (Sony) * It means that __augment_tree_propagate_from() must be called: 60368ad4a33SUladzislau Rezki (Sony) * - After VA has been inserted to the tree(free path); 60468ad4a33SUladzislau Rezki (Sony) * - After VA has been shrunk(allocation path); 60568ad4a33SUladzislau Rezki (Sony) * - After VA has been increased(merging path). 60668ad4a33SUladzislau Rezki (Sony) * 60768ad4a33SUladzislau Rezki (Sony) * Please note that, it does not mean that upper parent nodes 60868ad4a33SUladzislau Rezki (Sony) * and their subtree_max_size are recalculated all the time up 60968ad4a33SUladzislau Rezki (Sony) * to the root node. 61068ad4a33SUladzislau Rezki (Sony) * 61168ad4a33SUladzislau Rezki (Sony) * 4--8 61268ad4a33SUladzislau Rezki (Sony) * /\ 61368ad4a33SUladzislau Rezki (Sony) * / \ 61468ad4a33SUladzislau Rezki (Sony) * / \ 61568ad4a33SUladzislau Rezki (Sony) * 2--2 8--8 61668ad4a33SUladzislau Rezki (Sony) * 61768ad4a33SUladzislau Rezki (Sony) * For example if we modify the node 4, shrinking it to 2, then 61868ad4a33SUladzislau Rezki (Sony) * no any modification is required. If we shrink the node 2 to 1 61968ad4a33SUladzislau Rezki (Sony) * its subtree_max_size is updated only, and set to 1. If we shrink 62068ad4a33SUladzislau Rezki (Sony) * the node 8 to 6, then its subtree_max_size is set to 6 and parent 62168ad4a33SUladzislau Rezki (Sony) * node becomes 4--6. 62268ad4a33SUladzislau Rezki (Sony) */ 62368ad4a33SUladzislau Rezki (Sony) static __always_inline void 62468ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(struct vmap_area *va) 62568ad4a33SUladzislau Rezki (Sony) { 62668ad4a33SUladzislau Rezki (Sony) struct rb_node *node = &va->rb_node; 62768ad4a33SUladzislau Rezki (Sony) unsigned long new_va_sub_max_size; 62868ad4a33SUladzislau Rezki (Sony) 62968ad4a33SUladzislau Rezki (Sony) while (node) { 63068ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 63168ad4a33SUladzislau Rezki (Sony) new_va_sub_max_size = compute_subtree_max_size(va); 63268ad4a33SUladzislau Rezki (Sony) 63368ad4a33SUladzislau Rezki (Sony) /* 63468ad4a33SUladzislau Rezki (Sony) * If the newly calculated maximum available size of the 63568ad4a33SUladzislau Rezki (Sony) * subtree is equal to the current one, then it means that 63668ad4a33SUladzislau Rezki (Sony) * the tree is propagated correctly. So we have to stop at 63768ad4a33SUladzislau Rezki (Sony) * this point to save cycles. 63868ad4a33SUladzislau Rezki (Sony) */ 63968ad4a33SUladzislau Rezki (Sony) if (va->subtree_max_size == new_va_sub_max_size) 64068ad4a33SUladzislau Rezki (Sony) break; 64168ad4a33SUladzislau Rezki (Sony) 64268ad4a33SUladzislau Rezki (Sony) va->subtree_max_size = new_va_sub_max_size; 64368ad4a33SUladzislau Rezki (Sony) node = rb_parent(&va->rb_node); 64468ad4a33SUladzislau Rezki (Sony) } 645bb850f4dSUladzislau Rezki (Sony) 646bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK 647bb850f4dSUladzislau Rezki (Sony) augment_tree_propagate_check(free_vmap_area_root.rb_node); 648bb850f4dSUladzislau Rezki (Sony) #endif 64968ad4a33SUladzislau Rezki (Sony) } 65068ad4a33SUladzislau Rezki (Sony) 65168ad4a33SUladzislau Rezki (Sony) static void 65268ad4a33SUladzislau Rezki (Sony) insert_vmap_area(struct vmap_area *va, 65368ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 65468ad4a33SUladzislau Rezki (Sony) { 65568ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 65668ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 65768ad4a33SUladzislau Rezki (Sony) 65868ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 65968ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 66068ad4a33SUladzislau Rezki (Sony) } 66168ad4a33SUladzislau Rezki (Sony) 66268ad4a33SUladzislau Rezki (Sony) static void 66368ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(struct vmap_area *va, 66468ad4a33SUladzislau Rezki (Sony) struct rb_node *from, struct rb_root *root, 66568ad4a33SUladzislau Rezki (Sony) struct list_head *head) 66668ad4a33SUladzislau Rezki (Sony) { 66768ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 66868ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 66968ad4a33SUladzislau Rezki (Sony) 67068ad4a33SUladzislau Rezki (Sony) if (from) 67168ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, NULL, from, &parent); 67268ad4a33SUladzislau Rezki (Sony) else 67368ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 67468ad4a33SUladzislau Rezki (Sony) 67568ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 67668ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 67768ad4a33SUladzislau Rezki (Sony) } 67868ad4a33SUladzislau Rezki (Sony) 67968ad4a33SUladzislau Rezki (Sony) /* 68068ad4a33SUladzislau Rezki (Sony) * Merge de-allocated chunk of VA memory with previous 68168ad4a33SUladzislau Rezki (Sony) * and next free blocks. If coalesce is not done a new 68268ad4a33SUladzislau Rezki (Sony) * free area is inserted. If VA has been merged, it is 68368ad4a33SUladzislau Rezki (Sony) * freed. 68468ad4a33SUladzislau Rezki (Sony) */ 68568ad4a33SUladzislau Rezki (Sony) static __always_inline void 68668ad4a33SUladzislau Rezki (Sony) merge_or_add_vmap_area(struct vmap_area *va, 68768ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 68868ad4a33SUladzislau Rezki (Sony) { 68968ad4a33SUladzislau Rezki (Sony) struct vmap_area *sibling; 69068ad4a33SUladzislau Rezki (Sony) struct list_head *next; 69168ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 69268ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 69368ad4a33SUladzislau Rezki (Sony) bool merged = false; 69468ad4a33SUladzislau Rezki (Sony) 69568ad4a33SUladzislau Rezki (Sony) /* 69668ad4a33SUladzislau Rezki (Sony) * Find a place in the tree where VA potentially will be 69768ad4a33SUladzislau Rezki (Sony) * inserted, unless it is merged with its sibling/siblings. 69868ad4a33SUladzislau Rezki (Sony) */ 69968ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 70068ad4a33SUladzislau Rezki (Sony) 70168ad4a33SUladzislau Rezki (Sony) /* 70268ad4a33SUladzislau Rezki (Sony) * Get next node of VA to check if merging can be done. 70368ad4a33SUladzislau Rezki (Sony) */ 70468ad4a33SUladzislau Rezki (Sony) next = get_va_next_sibling(parent, link); 70568ad4a33SUladzislau Rezki (Sony) if (unlikely(next == NULL)) 70668ad4a33SUladzislau Rezki (Sony) goto insert; 70768ad4a33SUladzislau Rezki (Sony) 70868ad4a33SUladzislau Rezki (Sony) /* 70968ad4a33SUladzislau Rezki (Sony) * start end 71068ad4a33SUladzislau Rezki (Sony) * | | 71168ad4a33SUladzislau Rezki (Sony) * |<------VA------>|<-----Next----->| 71268ad4a33SUladzislau Rezki (Sony) * | | 71368ad4a33SUladzislau Rezki (Sony) * start end 71468ad4a33SUladzislau Rezki (Sony) */ 71568ad4a33SUladzislau Rezki (Sony) if (next != head) { 71668ad4a33SUladzislau Rezki (Sony) sibling = list_entry(next, struct vmap_area, list); 71768ad4a33SUladzislau Rezki (Sony) if (sibling->va_start == va->va_end) { 71868ad4a33SUladzislau Rezki (Sony) sibling->va_start = va->va_start; 71968ad4a33SUladzislau Rezki (Sony) 72068ad4a33SUladzislau Rezki (Sony) /* Check and update the tree if needed. */ 72168ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(sibling); 72268ad4a33SUladzislau Rezki (Sony) 72368ad4a33SUladzislau Rezki (Sony) /* Free vmap_area object. */ 72468ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 72568ad4a33SUladzislau Rezki (Sony) 72668ad4a33SUladzislau Rezki (Sony) /* Point to the new merged area. */ 72768ad4a33SUladzislau Rezki (Sony) va = sibling; 72868ad4a33SUladzislau Rezki (Sony) merged = true; 72968ad4a33SUladzislau Rezki (Sony) } 73068ad4a33SUladzislau Rezki (Sony) } 73168ad4a33SUladzislau Rezki (Sony) 73268ad4a33SUladzislau Rezki (Sony) /* 73368ad4a33SUladzislau Rezki (Sony) * start end 73468ad4a33SUladzislau Rezki (Sony) * | | 73568ad4a33SUladzislau Rezki (Sony) * |<-----Prev----->|<------VA------>| 73668ad4a33SUladzislau Rezki (Sony) * | | 73768ad4a33SUladzislau Rezki (Sony) * start end 73868ad4a33SUladzislau Rezki (Sony) */ 73968ad4a33SUladzislau Rezki (Sony) if (next->prev != head) { 74068ad4a33SUladzislau Rezki (Sony) sibling = list_entry(next->prev, struct vmap_area, list); 74168ad4a33SUladzislau Rezki (Sony) if (sibling->va_end == va->va_start) { 74268ad4a33SUladzislau Rezki (Sony) sibling->va_end = va->va_end; 74368ad4a33SUladzislau Rezki (Sony) 74468ad4a33SUladzislau Rezki (Sony) /* Check and update the tree if needed. */ 74568ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(sibling); 74668ad4a33SUladzislau Rezki (Sony) 74754f63d9dSUladzislau Rezki (Sony) if (merged) 74868ad4a33SUladzislau Rezki (Sony) unlink_va(va, root); 74968ad4a33SUladzislau Rezki (Sony) 75068ad4a33SUladzislau Rezki (Sony) /* Free vmap_area object. */ 75168ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 75268ad4a33SUladzislau Rezki (Sony) return; 75368ad4a33SUladzislau Rezki (Sony) } 75468ad4a33SUladzislau Rezki (Sony) } 75568ad4a33SUladzislau Rezki (Sony) 75668ad4a33SUladzislau Rezki (Sony) insert: 75768ad4a33SUladzislau Rezki (Sony) if (!merged) { 75868ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 75968ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 76068ad4a33SUladzislau Rezki (Sony) } 76168ad4a33SUladzislau Rezki (Sony) } 76268ad4a33SUladzislau Rezki (Sony) 76368ad4a33SUladzislau Rezki (Sony) static __always_inline bool 76468ad4a33SUladzislau Rezki (Sony) is_within_this_va(struct vmap_area *va, unsigned long size, 76568ad4a33SUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 76668ad4a33SUladzislau Rezki (Sony) { 76768ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr; 76868ad4a33SUladzislau Rezki (Sony) 76968ad4a33SUladzislau Rezki (Sony) if (va->va_start > vstart) 77068ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(va->va_start, align); 77168ad4a33SUladzislau Rezki (Sony) else 77268ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(vstart, align); 77368ad4a33SUladzislau Rezki (Sony) 77468ad4a33SUladzislau Rezki (Sony) /* Can be overflowed due to big size or alignment. */ 77568ad4a33SUladzislau Rezki (Sony) if (nva_start_addr + size < nva_start_addr || 77668ad4a33SUladzislau Rezki (Sony) nva_start_addr < vstart) 77768ad4a33SUladzislau Rezki (Sony) return false; 77868ad4a33SUladzislau Rezki (Sony) 77968ad4a33SUladzislau Rezki (Sony) return (nva_start_addr + size <= va->va_end); 78068ad4a33SUladzislau Rezki (Sony) } 78168ad4a33SUladzislau Rezki (Sony) 78268ad4a33SUladzislau Rezki (Sony) /* 78368ad4a33SUladzislau Rezki (Sony) * Find the first free block(lowest start address) in the tree, 78468ad4a33SUladzislau Rezki (Sony) * that will accomplish the request corresponding to passing 78568ad4a33SUladzislau Rezki (Sony) * parameters. 78668ad4a33SUladzislau Rezki (Sony) */ 78768ad4a33SUladzislau Rezki (Sony) static __always_inline struct vmap_area * 78868ad4a33SUladzislau Rezki (Sony) find_vmap_lowest_match(unsigned long size, 78968ad4a33SUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 79068ad4a33SUladzislau Rezki (Sony) { 79168ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 79268ad4a33SUladzislau Rezki (Sony) struct rb_node *node; 79368ad4a33SUladzislau Rezki (Sony) unsigned long length; 79468ad4a33SUladzislau Rezki (Sony) 79568ad4a33SUladzislau Rezki (Sony) /* Start from the root. */ 79668ad4a33SUladzislau Rezki (Sony) node = free_vmap_area_root.rb_node; 79768ad4a33SUladzislau Rezki (Sony) 79868ad4a33SUladzislau Rezki (Sony) /* Adjust the search size for alignment overhead. */ 79968ad4a33SUladzislau Rezki (Sony) length = size + align - 1; 80068ad4a33SUladzislau Rezki (Sony) 80168ad4a33SUladzislau Rezki (Sony) while (node) { 80268ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 80368ad4a33SUladzislau Rezki (Sony) 80468ad4a33SUladzislau Rezki (Sony) if (get_subtree_max_size(node->rb_left) >= length && 80568ad4a33SUladzislau Rezki (Sony) vstart < va->va_start) { 80668ad4a33SUladzislau Rezki (Sony) node = node->rb_left; 80768ad4a33SUladzislau Rezki (Sony) } else { 80868ad4a33SUladzislau Rezki (Sony) if (is_within_this_va(va, size, align, vstart)) 80968ad4a33SUladzislau Rezki (Sony) return va; 81068ad4a33SUladzislau Rezki (Sony) 81168ad4a33SUladzislau Rezki (Sony) /* 81268ad4a33SUladzislau Rezki (Sony) * Does not make sense to go deeper towards the right 81368ad4a33SUladzislau Rezki (Sony) * sub-tree if it does not have a free block that is 81468ad4a33SUladzislau Rezki (Sony) * equal or bigger to the requested search length. 81568ad4a33SUladzislau Rezki (Sony) */ 81668ad4a33SUladzislau Rezki (Sony) if (get_subtree_max_size(node->rb_right) >= length) { 81768ad4a33SUladzislau Rezki (Sony) node = node->rb_right; 81868ad4a33SUladzislau Rezki (Sony) continue; 81968ad4a33SUladzislau Rezki (Sony) } 82068ad4a33SUladzislau Rezki (Sony) 82168ad4a33SUladzislau Rezki (Sony) /* 8223806b041SAndrew Morton * OK. We roll back and find the first right sub-tree, 82368ad4a33SUladzislau Rezki (Sony) * that will satisfy the search criteria. It can happen 82468ad4a33SUladzislau Rezki (Sony) * only once due to "vstart" restriction. 82568ad4a33SUladzislau Rezki (Sony) */ 82668ad4a33SUladzislau Rezki (Sony) while ((node = rb_parent(node))) { 82768ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 82868ad4a33SUladzislau Rezki (Sony) if (is_within_this_va(va, size, align, vstart)) 82968ad4a33SUladzislau Rezki (Sony) return va; 83068ad4a33SUladzislau Rezki (Sony) 83168ad4a33SUladzislau Rezki (Sony) if (get_subtree_max_size(node->rb_right) >= length && 83268ad4a33SUladzislau Rezki (Sony) vstart <= va->va_start) { 83368ad4a33SUladzislau Rezki (Sony) node = node->rb_right; 83468ad4a33SUladzislau Rezki (Sony) break; 83568ad4a33SUladzislau Rezki (Sony) } 83668ad4a33SUladzislau Rezki (Sony) } 83768ad4a33SUladzislau Rezki (Sony) } 83868ad4a33SUladzislau Rezki (Sony) } 83968ad4a33SUladzislau Rezki (Sony) 84068ad4a33SUladzislau Rezki (Sony) return NULL; 84168ad4a33SUladzislau Rezki (Sony) } 84268ad4a33SUladzislau Rezki (Sony) 843a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 844a6cf4e0fSUladzislau Rezki (Sony) #include <linux/random.h> 845a6cf4e0fSUladzislau Rezki (Sony) 846a6cf4e0fSUladzislau Rezki (Sony) static struct vmap_area * 847a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_linear_match(unsigned long size, 848a6cf4e0fSUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 849a6cf4e0fSUladzislau Rezki (Sony) { 850a6cf4e0fSUladzislau Rezki (Sony) struct vmap_area *va; 851a6cf4e0fSUladzislau Rezki (Sony) 852a6cf4e0fSUladzislau Rezki (Sony) list_for_each_entry(va, &free_vmap_area_list, list) { 853a6cf4e0fSUladzislau Rezki (Sony) if (!is_within_this_va(va, size, align, vstart)) 854a6cf4e0fSUladzislau Rezki (Sony) continue; 855a6cf4e0fSUladzislau Rezki (Sony) 856a6cf4e0fSUladzislau Rezki (Sony) return va; 857a6cf4e0fSUladzislau Rezki (Sony) } 858a6cf4e0fSUladzislau Rezki (Sony) 859a6cf4e0fSUladzislau Rezki (Sony) return NULL; 860a6cf4e0fSUladzislau Rezki (Sony) } 861a6cf4e0fSUladzislau Rezki (Sony) 862a6cf4e0fSUladzislau Rezki (Sony) static void 863a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_match_check(unsigned long size) 864a6cf4e0fSUladzislau Rezki (Sony) { 865a6cf4e0fSUladzislau Rezki (Sony) struct vmap_area *va_1, *va_2; 866a6cf4e0fSUladzislau Rezki (Sony) unsigned long vstart; 867a6cf4e0fSUladzislau Rezki (Sony) unsigned int rnd; 868a6cf4e0fSUladzislau Rezki (Sony) 869a6cf4e0fSUladzislau Rezki (Sony) get_random_bytes(&rnd, sizeof(rnd)); 870a6cf4e0fSUladzislau Rezki (Sony) vstart = VMALLOC_START + rnd; 871a6cf4e0fSUladzislau Rezki (Sony) 872a6cf4e0fSUladzislau Rezki (Sony) va_1 = find_vmap_lowest_match(size, 1, vstart); 873a6cf4e0fSUladzislau Rezki (Sony) va_2 = find_vmap_lowest_linear_match(size, 1, vstart); 874a6cf4e0fSUladzislau Rezki (Sony) 875a6cf4e0fSUladzislau Rezki (Sony) if (va_1 != va_2) 876a6cf4e0fSUladzislau Rezki (Sony) pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n", 877a6cf4e0fSUladzislau Rezki (Sony) va_1, va_2, vstart); 878a6cf4e0fSUladzislau Rezki (Sony) } 879a6cf4e0fSUladzislau Rezki (Sony) #endif 880a6cf4e0fSUladzislau Rezki (Sony) 88168ad4a33SUladzislau Rezki (Sony) enum fit_type { 88268ad4a33SUladzislau Rezki (Sony) NOTHING_FIT = 0, 88368ad4a33SUladzislau Rezki (Sony) FL_FIT_TYPE = 1, /* full fit */ 88468ad4a33SUladzislau Rezki (Sony) LE_FIT_TYPE = 2, /* left edge fit */ 88568ad4a33SUladzislau Rezki (Sony) RE_FIT_TYPE = 3, /* right edge fit */ 88668ad4a33SUladzislau Rezki (Sony) NE_FIT_TYPE = 4 /* no edge fit */ 88768ad4a33SUladzislau Rezki (Sony) }; 88868ad4a33SUladzislau Rezki (Sony) 88968ad4a33SUladzislau Rezki (Sony) static __always_inline enum fit_type 89068ad4a33SUladzislau Rezki (Sony) classify_va_fit_type(struct vmap_area *va, 89168ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr, unsigned long size) 89268ad4a33SUladzislau Rezki (Sony) { 89368ad4a33SUladzislau Rezki (Sony) enum fit_type type; 89468ad4a33SUladzislau Rezki (Sony) 89568ad4a33SUladzislau Rezki (Sony) /* Check if it is within VA. */ 89668ad4a33SUladzislau Rezki (Sony) if (nva_start_addr < va->va_start || 89768ad4a33SUladzislau Rezki (Sony) nva_start_addr + size > va->va_end) 89868ad4a33SUladzislau Rezki (Sony) return NOTHING_FIT; 89968ad4a33SUladzislau Rezki (Sony) 90068ad4a33SUladzislau Rezki (Sony) /* Now classify. */ 90168ad4a33SUladzislau Rezki (Sony) if (va->va_start == nva_start_addr) { 90268ad4a33SUladzislau Rezki (Sony) if (va->va_end == nva_start_addr + size) 90368ad4a33SUladzislau Rezki (Sony) type = FL_FIT_TYPE; 90468ad4a33SUladzislau Rezki (Sony) else 90568ad4a33SUladzislau Rezki (Sony) type = LE_FIT_TYPE; 90668ad4a33SUladzislau Rezki (Sony) } else if (va->va_end == nva_start_addr + size) { 90768ad4a33SUladzislau Rezki (Sony) type = RE_FIT_TYPE; 90868ad4a33SUladzislau Rezki (Sony) } else { 90968ad4a33SUladzislau Rezki (Sony) type = NE_FIT_TYPE; 91068ad4a33SUladzislau Rezki (Sony) } 91168ad4a33SUladzislau Rezki (Sony) 91268ad4a33SUladzislau Rezki (Sony) return type; 91368ad4a33SUladzislau Rezki (Sony) } 91468ad4a33SUladzislau Rezki (Sony) 91568ad4a33SUladzislau Rezki (Sony) static __always_inline int 91668ad4a33SUladzislau Rezki (Sony) adjust_va_to_fit_type(struct vmap_area *va, 91768ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr, unsigned long size, 91868ad4a33SUladzislau Rezki (Sony) enum fit_type type) 91968ad4a33SUladzislau Rezki (Sony) { 9202c929233SArnd Bergmann struct vmap_area *lva = NULL; 92168ad4a33SUladzislau Rezki (Sony) 92268ad4a33SUladzislau Rezki (Sony) if (type == FL_FIT_TYPE) { 92368ad4a33SUladzislau Rezki (Sony) /* 92468ad4a33SUladzislau Rezki (Sony) * No need to split VA, it fully fits. 92568ad4a33SUladzislau Rezki (Sony) * 92668ad4a33SUladzislau Rezki (Sony) * | | 92768ad4a33SUladzislau Rezki (Sony) * V NVA V 92868ad4a33SUladzislau Rezki (Sony) * |---------------| 92968ad4a33SUladzislau Rezki (Sony) */ 93068ad4a33SUladzislau Rezki (Sony) unlink_va(va, &free_vmap_area_root); 93168ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 93268ad4a33SUladzislau Rezki (Sony) } else if (type == LE_FIT_TYPE) { 93368ad4a33SUladzislau Rezki (Sony) /* 93468ad4a33SUladzislau Rezki (Sony) * Split left edge of fit VA. 93568ad4a33SUladzislau Rezki (Sony) * 93668ad4a33SUladzislau Rezki (Sony) * | | 93768ad4a33SUladzislau Rezki (Sony) * V NVA V R 93868ad4a33SUladzislau Rezki (Sony) * |-------|-------| 93968ad4a33SUladzislau Rezki (Sony) */ 94068ad4a33SUladzislau Rezki (Sony) va->va_start += size; 94168ad4a33SUladzislau Rezki (Sony) } else if (type == RE_FIT_TYPE) { 94268ad4a33SUladzislau Rezki (Sony) /* 94368ad4a33SUladzislau Rezki (Sony) * Split right edge of fit VA. 94468ad4a33SUladzislau Rezki (Sony) * 94568ad4a33SUladzislau Rezki (Sony) * | | 94668ad4a33SUladzislau Rezki (Sony) * L V NVA V 94768ad4a33SUladzislau Rezki (Sony) * |-------|-------| 94868ad4a33SUladzislau Rezki (Sony) */ 94968ad4a33SUladzislau Rezki (Sony) va->va_end = nva_start_addr; 95068ad4a33SUladzislau Rezki (Sony) } else if (type == NE_FIT_TYPE) { 95168ad4a33SUladzislau Rezki (Sony) /* 95268ad4a33SUladzislau Rezki (Sony) * Split no edge of fit VA. 95368ad4a33SUladzislau Rezki (Sony) * 95468ad4a33SUladzislau Rezki (Sony) * | | 95568ad4a33SUladzislau Rezki (Sony) * L V NVA V R 95668ad4a33SUladzislau Rezki (Sony) * |---|-------|---| 95768ad4a33SUladzislau Rezki (Sony) */ 95882dd23e8SUladzislau Rezki (Sony) lva = __this_cpu_xchg(ne_fit_preload_node, NULL); 95982dd23e8SUladzislau Rezki (Sony) if (unlikely(!lva)) { 96082dd23e8SUladzislau Rezki (Sony) /* 96182dd23e8SUladzislau Rezki (Sony) * For percpu allocator we do not do any pre-allocation 96282dd23e8SUladzislau Rezki (Sony) * and leave it as it is. The reason is it most likely 96382dd23e8SUladzislau Rezki (Sony) * never ends up with NE_FIT_TYPE splitting. In case of 96482dd23e8SUladzislau Rezki (Sony) * percpu allocations offsets and sizes are aligned to 96582dd23e8SUladzislau Rezki (Sony) * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE 96682dd23e8SUladzislau Rezki (Sony) * are its main fitting cases. 96782dd23e8SUladzislau Rezki (Sony) * 96882dd23e8SUladzislau Rezki (Sony) * There are a few exceptions though, as an example it is 96982dd23e8SUladzislau Rezki (Sony) * a first allocation (early boot up) when we have "one" 97082dd23e8SUladzislau Rezki (Sony) * big free space that has to be split. 97182dd23e8SUladzislau Rezki (Sony) */ 97268ad4a33SUladzislau Rezki (Sony) lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT); 97382dd23e8SUladzislau Rezki (Sony) if (!lva) 97468ad4a33SUladzislau Rezki (Sony) return -1; 97582dd23e8SUladzislau Rezki (Sony) } 97668ad4a33SUladzislau Rezki (Sony) 97768ad4a33SUladzislau Rezki (Sony) /* 97868ad4a33SUladzislau Rezki (Sony) * Build the remainder. 97968ad4a33SUladzislau Rezki (Sony) */ 98068ad4a33SUladzislau Rezki (Sony) lva->va_start = va->va_start; 98168ad4a33SUladzislau Rezki (Sony) lva->va_end = nva_start_addr; 98268ad4a33SUladzislau Rezki (Sony) 98368ad4a33SUladzislau Rezki (Sony) /* 98468ad4a33SUladzislau Rezki (Sony) * Shrink this VA to remaining size. 98568ad4a33SUladzislau Rezki (Sony) */ 98668ad4a33SUladzislau Rezki (Sony) va->va_start = nva_start_addr + size; 98768ad4a33SUladzislau Rezki (Sony) } else { 98868ad4a33SUladzislau Rezki (Sony) return -1; 98968ad4a33SUladzislau Rezki (Sony) } 99068ad4a33SUladzislau Rezki (Sony) 99168ad4a33SUladzislau Rezki (Sony) if (type != FL_FIT_TYPE) { 99268ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 99368ad4a33SUladzislau Rezki (Sony) 9942c929233SArnd Bergmann if (lva) /* type == NE_FIT_TYPE */ 99568ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(lva, &va->rb_node, 99668ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, &free_vmap_area_list); 99768ad4a33SUladzislau Rezki (Sony) } 99868ad4a33SUladzislau Rezki (Sony) 99968ad4a33SUladzislau Rezki (Sony) return 0; 100068ad4a33SUladzislau Rezki (Sony) } 100168ad4a33SUladzislau Rezki (Sony) 100268ad4a33SUladzislau Rezki (Sony) /* 100368ad4a33SUladzislau Rezki (Sony) * Returns a start address of the newly allocated area, if success. 100468ad4a33SUladzislau Rezki (Sony) * Otherwise a vend is returned that indicates failure. 100568ad4a33SUladzislau Rezki (Sony) */ 100668ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 100768ad4a33SUladzislau Rezki (Sony) __alloc_vmap_area(unsigned long size, unsigned long align, 1008cacca6baSUladzislau Rezki (Sony) unsigned long vstart, unsigned long vend) 100968ad4a33SUladzislau Rezki (Sony) { 101068ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr; 101168ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 101268ad4a33SUladzislau Rezki (Sony) enum fit_type type; 101368ad4a33SUladzislau Rezki (Sony) int ret; 101468ad4a33SUladzislau Rezki (Sony) 101568ad4a33SUladzislau Rezki (Sony) va = find_vmap_lowest_match(size, align, vstart); 101668ad4a33SUladzislau Rezki (Sony) if (unlikely(!va)) 101768ad4a33SUladzislau Rezki (Sony) return vend; 101868ad4a33SUladzislau Rezki (Sony) 101968ad4a33SUladzislau Rezki (Sony) if (va->va_start > vstart) 102068ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(va->va_start, align); 102168ad4a33SUladzislau Rezki (Sony) else 102268ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(vstart, align); 102368ad4a33SUladzislau Rezki (Sony) 102468ad4a33SUladzislau Rezki (Sony) /* Check the "vend" restriction. */ 102568ad4a33SUladzislau Rezki (Sony) if (nva_start_addr + size > vend) 102668ad4a33SUladzislau Rezki (Sony) return vend; 102768ad4a33SUladzislau Rezki (Sony) 102868ad4a33SUladzislau Rezki (Sony) /* Classify what we have found. */ 102968ad4a33SUladzislau Rezki (Sony) type = classify_va_fit_type(va, nva_start_addr, size); 103068ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(type == NOTHING_FIT)) 103168ad4a33SUladzislau Rezki (Sony) return vend; 103268ad4a33SUladzislau Rezki (Sony) 103368ad4a33SUladzislau Rezki (Sony) /* Update the free vmap_area. */ 103468ad4a33SUladzislau Rezki (Sony) ret = adjust_va_to_fit_type(va, nva_start_addr, size, type); 103568ad4a33SUladzislau Rezki (Sony) if (ret) 103668ad4a33SUladzislau Rezki (Sony) return vend; 103768ad4a33SUladzislau Rezki (Sony) 1038a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 1039a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_match_check(size); 1040a6cf4e0fSUladzislau Rezki (Sony) #endif 1041a6cf4e0fSUladzislau Rezki (Sony) 104268ad4a33SUladzislau Rezki (Sony) return nva_start_addr; 104368ad4a33SUladzislau Rezki (Sony) } 10444da56b99SChris Wilson 1045db64fe02SNick Piggin /* 1046db64fe02SNick Piggin * Allocate a region of KVA of the specified size and alignment, within the 1047db64fe02SNick Piggin * vstart and vend. 1048db64fe02SNick Piggin */ 1049db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size, 1050db64fe02SNick Piggin unsigned long align, 1051db64fe02SNick Piggin unsigned long vstart, unsigned long vend, 1052db64fe02SNick Piggin int node, gfp_t gfp_mask) 1053db64fe02SNick Piggin { 105482dd23e8SUladzislau Rezki (Sony) struct vmap_area *va, *pva; 10551da177e4SLinus Torvalds unsigned long addr; 1056db64fe02SNick Piggin int purged = 0; 1057db64fe02SNick Piggin 10587766970cSNick Piggin BUG_ON(!size); 1059891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 106089699605SNick Piggin BUG_ON(!is_power_of_2(align)); 1061db64fe02SNick Piggin 106268ad4a33SUladzislau Rezki (Sony) if (unlikely(!vmap_initialized)) 106368ad4a33SUladzislau Rezki (Sony) return ERR_PTR(-EBUSY); 106468ad4a33SUladzislau Rezki (Sony) 10655803ed29SChristoph Hellwig might_sleep(); 10664da56b99SChris Wilson 106768ad4a33SUladzislau Rezki (Sony) va = kmem_cache_alloc_node(vmap_area_cachep, 1068db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 1069db64fe02SNick Piggin if (unlikely(!va)) 1070db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 1071db64fe02SNick Piggin 10727f88f88fSCatalin Marinas /* 10737f88f88fSCatalin Marinas * Only scan the relevant parts containing pointers to other objects 10747f88f88fSCatalin Marinas * to avoid false negatives. 10757f88f88fSCatalin Marinas */ 10767f88f88fSCatalin Marinas kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK); 10777f88f88fSCatalin Marinas 1078db64fe02SNick Piggin retry: 107982dd23e8SUladzislau Rezki (Sony) /* 108082dd23e8SUladzislau Rezki (Sony) * Preload this CPU with one extra vmap_area object to ensure 108182dd23e8SUladzislau Rezki (Sony) * that we have it available when fit type of free area is 108282dd23e8SUladzislau Rezki (Sony) * NE_FIT_TYPE. 108382dd23e8SUladzislau Rezki (Sony) * 108482dd23e8SUladzislau Rezki (Sony) * The preload is done in non-atomic context, thus it allows us 108582dd23e8SUladzislau Rezki (Sony) * to use more permissive allocation masks to be more stable under 108682dd23e8SUladzislau Rezki (Sony) * low memory condition and high memory pressure. 108782dd23e8SUladzislau Rezki (Sony) * 108882dd23e8SUladzislau Rezki (Sony) * Even if it fails we do not really care about that. Just proceed 108982dd23e8SUladzislau Rezki (Sony) * as it is. "overflow" path will refill the cache we allocate from. 109082dd23e8SUladzislau Rezki (Sony) */ 109182dd23e8SUladzislau Rezki (Sony) preempt_disable(); 109282dd23e8SUladzislau Rezki (Sony) if (!__this_cpu_read(ne_fit_preload_node)) { 109382dd23e8SUladzislau Rezki (Sony) preempt_enable(); 109482dd23e8SUladzislau Rezki (Sony) pva = kmem_cache_alloc_node(vmap_area_cachep, GFP_KERNEL, node); 109582dd23e8SUladzislau Rezki (Sony) preempt_disable(); 109682dd23e8SUladzislau Rezki (Sony) 109782dd23e8SUladzislau Rezki (Sony) if (__this_cpu_cmpxchg(ne_fit_preload_node, NULL, pva)) { 109882dd23e8SUladzislau Rezki (Sony) if (pva) 109982dd23e8SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, pva); 110082dd23e8SUladzislau Rezki (Sony) } 110182dd23e8SUladzislau Rezki (Sony) } 110282dd23e8SUladzislau Rezki (Sony) 1103db64fe02SNick Piggin spin_lock(&vmap_area_lock); 110482dd23e8SUladzislau Rezki (Sony) preempt_enable(); 110568ad4a33SUladzislau Rezki (Sony) 110689699605SNick Piggin /* 110768ad4a33SUladzislau Rezki (Sony) * If an allocation fails, the "vend" address is 110868ad4a33SUladzislau Rezki (Sony) * returned. Therefore trigger the overflow path. 110989699605SNick Piggin */ 1110cacca6baSUladzislau Rezki (Sony) addr = __alloc_vmap_area(size, align, vstart, vend); 111168ad4a33SUladzislau Rezki (Sony) if (unlikely(addr == vend)) 111289699605SNick Piggin goto overflow; 111389699605SNick Piggin 111489699605SNick Piggin va->va_start = addr; 111589699605SNick Piggin va->va_end = addr + size; 1116688fcbfcSPengfei Li va->vm = NULL; 111768ad4a33SUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 111868ad4a33SUladzislau Rezki (Sony) 111989699605SNick Piggin spin_unlock(&vmap_area_lock); 112089699605SNick Piggin 112161e16557SWang Xiaoqiang BUG_ON(!IS_ALIGNED(va->va_start, align)); 112289699605SNick Piggin BUG_ON(va->va_start < vstart); 112389699605SNick Piggin BUG_ON(va->va_end > vend); 112489699605SNick Piggin 112589699605SNick Piggin return va; 112689699605SNick Piggin 11277766970cSNick Piggin overflow: 1128db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 1129db64fe02SNick Piggin if (!purged) { 1130db64fe02SNick Piggin purge_vmap_area_lazy(); 1131db64fe02SNick Piggin purged = 1; 1132db64fe02SNick Piggin goto retry; 1133db64fe02SNick Piggin } 11344da56b99SChris Wilson 11354da56b99SChris Wilson if (gfpflags_allow_blocking(gfp_mask)) { 11364da56b99SChris Wilson unsigned long freed = 0; 11374da56b99SChris Wilson blocking_notifier_call_chain(&vmap_notify_list, 0, &freed); 11384da56b99SChris Wilson if (freed > 0) { 11394da56b99SChris Wilson purged = 0; 11404da56b99SChris Wilson goto retry; 11414da56b99SChris Wilson } 11424da56b99SChris Wilson } 11434da56b99SChris Wilson 114403497d76SFlorian Fainelli if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) 1145756a025fSJoe Perches pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n", 1146756a025fSJoe Perches size); 114768ad4a33SUladzislau Rezki (Sony) 114868ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 1149db64fe02SNick Piggin return ERR_PTR(-EBUSY); 1150db64fe02SNick Piggin } 1151db64fe02SNick Piggin 11524da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb) 11534da56b99SChris Wilson { 11544da56b99SChris Wilson return blocking_notifier_chain_register(&vmap_notify_list, nb); 11554da56b99SChris Wilson } 11564da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier); 11574da56b99SChris Wilson 11584da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb) 11594da56b99SChris Wilson { 11604da56b99SChris Wilson return blocking_notifier_chain_unregister(&vmap_notify_list, nb); 11614da56b99SChris Wilson } 11624da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier); 11634da56b99SChris Wilson 1164db64fe02SNick Piggin static void __free_vmap_area(struct vmap_area *va) 1165db64fe02SNick Piggin { 116689699605SNick Piggin /* 116768ad4a33SUladzislau Rezki (Sony) * Remove from the busy tree/list. 116889699605SNick Piggin */ 116968ad4a33SUladzislau Rezki (Sony) unlink_va(va, &vmap_area_root); 1170db64fe02SNick Piggin 1171ca23e405STejun Heo /* 117268ad4a33SUladzislau Rezki (Sony) * Merge VA with its neighbors, otherwise just add it. 1173ca23e405STejun Heo */ 117468ad4a33SUladzislau Rezki (Sony) merge_or_add_vmap_area(va, 117568ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, &free_vmap_area_list); 1176db64fe02SNick Piggin } 1177db64fe02SNick Piggin 1178db64fe02SNick Piggin /* 1179db64fe02SNick Piggin * Free a region of KVA allocated by alloc_vmap_area 1180db64fe02SNick Piggin */ 1181db64fe02SNick Piggin static void free_vmap_area(struct vmap_area *va) 1182db64fe02SNick Piggin { 1183db64fe02SNick Piggin spin_lock(&vmap_area_lock); 1184db64fe02SNick Piggin __free_vmap_area(va); 1185db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 1186db64fe02SNick Piggin } 1187db64fe02SNick Piggin 1188db64fe02SNick Piggin /* 1189db64fe02SNick Piggin * Clear the pagetable entries of a given vmap_area 1190db64fe02SNick Piggin */ 1191db64fe02SNick Piggin static void unmap_vmap_area(struct vmap_area *va) 1192db64fe02SNick Piggin { 1193db64fe02SNick Piggin vunmap_page_range(va->va_start, va->va_end); 1194db64fe02SNick Piggin } 1195db64fe02SNick Piggin 1196db64fe02SNick Piggin /* 1197db64fe02SNick Piggin * lazy_max_pages is the maximum amount of virtual address space we gather up 1198db64fe02SNick Piggin * before attempting to purge with a TLB flush. 1199db64fe02SNick Piggin * 1200db64fe02SNick Piggin * There is a tradeoff here: a larger number will cover more kernel page tables 1201db64fe02SNick Piggin * and take slightly longer to purge, but it will linearly reduce the number of 1202db64fe02SNick Piggin * global TLB flushes that must be performed. It would seem natural to scale 1203db64fe02SNick Piggin * this number up linearly with the number of CPUs (because vmapping activity 1204db64fe02SNick Piggin * could also scale linearly with the number of CPUs), however it is likely 1205db64fe02SNick Piggin * that in practice, workloads might be constrained in other ways that mean 1206db64fe02SNick Piggin * vmap activity will not scale linearly with CPUs. Also, I want to be 1207db64fe02SNick Piggin * conservative and not introduce a big latency on huge systems, so go with 1208db64fe02SNick Piggin * a less aggressive log scale. It will still be an improvement over the old 1209db64fe02SNick Piggin * code, and it will be simple to change the scale factor if we find that it 1210db64fe02SNick Piggin * becomes a problem on bigger systems. 1211db64fe02SNick Piggin */ 1212db64fe02SNick Piggin static unsigned long lazy_max_pages(void) 1213db64fe02SNick Piggin { 1214db64fe02SNick Piggin unsigned int log; 1215db64fe02SNick Piggin 1216db64fe02SNick Piggin log = fls(num_online_cpus()); 1217db64fe02SNick Piggin 1218db64fe02SNick Piggin return log * (32UL * 1024 * 1024 / PAGE_SIZE); 1219db64fe02SNick Piggin } 1220db64fe02SNick Piggin 12214d36e6f8SUladzislau Rezki (Sony) static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0); 1222db64fe02SNick Piggin 12230574ecd1SChristoph Hellwig /* 12240574ecd1SChristoph Hellwig * Serialize vmap purging. There is no actual criticial section protected 12250574ecd1SChristoph Hellwig * by this look, but we want to avoid concurrent calls for performance 12260574ecd1SChristoph Hellwig * reasons and to make the pcpu_get_vm_areas more deterministic. 12270574ecd1SChristoph Hellwig */ 1228f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock); 12290574ecd1SChristoph Hellwig 123002b709dfSNick Piggin /* for per-CPU blocks */ 123102b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void); 123202b709dfSNick Piggin 1233db64fe02SNick Piggin /* 12343ee48b6aSCliff Wickman * called before a call to iounmap() if the caller wants vm_area_struct's 12353ee48b6aSCliff Wickman * immediately freed. 12363ee48b6aSCliff Wickman */ 12373ee48b6aSCliff Wickman void set_iounmap_nonlazy(void) 12383ee48b6aSCliff Wickman { 12394d36e6f8SUladzislau Rezki (Sony) atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1); 12403ee48b6aSCliff Wickman } 12413ee48b6aSCliff Wickman 12423ee48b6aSCliff Wickman /* 1243db64fe02SNick Piggin * Purges all lazily-freed vmap areas. 1244db64fe02SNick Piggin */ 12450574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end) 1246db64fe02SNick Piggin { 12474d36e6f8SUladzislau Rezki (Sony) unsigned long resched_threshold; 124880c4bd7aSChris Wilson struct llist_node *valist; 1249db64fe02SNick Piggin struct vmap_area *va; 1250cbb76676SVegard Nossum struct vmap_area *n_va; 1251db64fe02SNick Piggin 12520574ecd1SChristoph Hellwig lockdep_assert_held(&vmap_purge_lock); 125302b709dfSNick Piggin 125480c4bd7aSChris Wilson valist = llist_del_all(&vmap_purge_list); 125568571be9SUladzislau Rezki (Sony) if (unlikely(valist == NULL)) 125668571be9SUladzislau Rezki (Sony) return false; 125768571be9SUladzislau Rezki (Sony) 125868571be9SUladzislau Rezki (Sony) /* 12593f8fd02bSJoerg Roedel * First make sure the mappings are removed from all page-tables 12603f8fd02bSJoerg Roedel * before they are freed. 12613f8fd02bSJoerg Roedel */ 12623f8fd02bSJoerg Roedel vmalloc_sync_all(); 12633f8fd02bSJoerg Roedel 12643f8fd02bSJoerg Roedel /* 126568571be9SUladzislau Rezki (Sony) * TODO: to calculate a flush range without looping. 126668571be9SUladzislau Rezki (Sony) * The list can be up to lazy_max_pages() elements. 126768571be9SUladzislau Rezki (Sony) */ 126880c4bd7aSChris Wilson llist_for_each_entry(va, valist, purge_list) { 12690574ecd1SChristoph Hellwig if (va->va_start < start) 12700574ecd1SChristoph Hellwig start = va->va_start; 12710574ecd1SChristoph Hellwig if (va->va_end > end) 12720574ecd1SChristoph Hellwig end = va->va_end; 1273db64fe02SNick Piggin } 1274db64fe02SNick Piggin 12750574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 12764d36e6f8SUladzislau Rezki (Sony) resched_threshold = lazy_max_pages() << 1; 1277db64fe02SNick Piggin 1278db64fe02SNick Piggin spin_lock(&vmap_area_lock); 1279763b218dSJoel Fernandes llist_for_each_entry_safe(va, n_va, valist, purge_list) { 12804d36e6f8SUladzislau Rezki (Sony) unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT; 1281763b218dSJoel Fernandes 1282dd3b8353SUladzislau Rezki (Sony) /* 1283dd3b8353SUladzislau Rezki (Sony) * Finally insert or merge lazily-freed area. It is 1284dd3b8353SUladzislau Rezki (Sony) * detached and there is no need to "unlink" it from 1285dd3b8353SUladzislau Rezki (Sony) * anything. 1286dd3b8353SUladzislau Rezki (Sony) */ 1287dd3b8353SUladzislau Rezki (Sony) merge_or_add_vmap_area(va, 1288dd3b8353SUladzislau Rezki (Sony) &free_vmap_area_root, &free_vmap_area_list); 1289dd3b8353SUladzislau Rezki (Sony) 12904d36e6f8SUladzislau Rezki (Sony) atomic_long_sub(nr, &vmap_lazy_nr); 129168571be9SUladzislau Rezki (Sony) 12924d36e6f8SUladzislau Rezki (Sony) if (atomic_long_read(&vmap_lazy_nr) < resched_threshold) 1293763b218dSJoel Fernandes cond_resched_lock(&vmap_area_lock); 1294763b218dSJoel Fernandes } 1295db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 12960574ecd1SChristoph Hellwig return true; 1297db64fe02SNick Piggin } 1298db64fe02SNick Piggin 1299db64fe02SNick Piggin /* 1300496850e5SNick Piggin * Kick off a purge of the outstanding lazy areas. Don't bother if somebody 1301496850e5SNick Piggin * is already purging. 1302496850e5SNick Piggin */ 1303496850e5SNick Piggin static void try_purge_vmap_area_lazy(void) 1304496850e5SNick Piggin { 1305f9e09977SChristoph Hellwig if (mutex_trylock(&vmap_purge_lock)) { 13060574ecd1SChristoph Hellwig __purge_vmap_area_lazy(ULONG_MAX, 0); 1307f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 13080574ecd1SChristoph Hellwig } 1309496850e5SNick Piggin } 1310496850e5SNick Piggin 1311496850e5SNick Piggin /* 1312db64fe02SNick Piggin * Kick off a purge of the outstanding lazy areas. 1313db64fe02SNick Piggin */ 1314db64fe02SNick Piggin static void purge_vmap_area_lazy(void) 1315db64fe02SNick Piggin { 1316f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 13170574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 13180574ecd1SChristoph Hellwig __purge_vmap_area_lazy(ULONG_MAX, 0); 1319f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 1320db64fe02SNick Piggin } 1321db64fe02SNick Piggin 1322db64fe02SNick Piggin /* 132364141da5SJeremy Fitzhardinge * Free a vmap area, caller ensuring that the area has been unmapped 132464141da5SJeremy Fitzhardinge * and flush_cache_vunmap had been called for the correct range 132564141da5SJeremy Fitzhardinge * previously. 1326db64fe02SNick Piggin */ 132764141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va) 1328db64fe02SNick Piggin { 13294d36e6f8SUladzislau Rezki (Sony) unsigned long nr_lazy; 133080c4bd7aSChris Wilson 1331dd3b8353SUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 1332dd3b8353SUladzislau Rezki (Sony) unlink_va(va, &vmap_area_root); 1333dd3b8353SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 1334dd3b8353SUladzislau Rezki (Sony) 13354d36e6f8SUladzislau Rezki (Sony) nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >> 13364d36e6f8SUladzislau Rezki (Sony) PAGE_SHIFT, &vmap_lazy_nr); 133780c4bd7aSChris Wilson 133880c4bd7aSChris Wilson /* After this point, we may free va at any time */ 133980c4bd7aSChris Wilson llist_add(&va->purge_list, &vmap_purge_list); 134080c4bd7aSChris Wilson 134180c4bd7aSChris Wilson if (unlikely(nr_lazy > lazy_max_pages())) 1342496850e5SNick Piggin try_purge_vmap_area_lazy(); 1343db64fe02SNick Piggin } 1344db64fe02SNick Piggin 1345b29acbdcSNick Piggin /* 1346b29acbdcSNick Piggin * Free and unmap a vmap area 1347b29acbdcSNick Piggin */ 1348b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va) 1349b29acbdcSNick Piggin { 1350b29acbdcSNick Piggin flush_cache_vunmap(va->va_start, va->va_end); 1351c8eef01eSChristoph Hellwig unmap_vmap_area(va); 135282a2e924SChintan Pandya if (debug_pagealloc_enabled()) 135382a2e924SChintan Pandya flush_tlb_kernel_range(va->va_start, va->va_end); 135482a2e924SChintan Pandya 1355c8eef01eSChristoph Hellwig free_vmap_area_noflush(va); 1356b29acbdcSNick Piggin } 1357b29acbdcSNick Piggin 1358db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr) 1359db64fe02SNick Piggin { 1360db64fe02SNick Piggin struct vmap_area *va; 1361db64fe02SNick Piggin 1362db64fe02SNick Piggin spin_lock(&vmap_area_lock); 1363db64fe02SNick Piggin va = __find_vmap_area(addr); 1364db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 1365db64fe02SNick Piggin 1366db64fe02SNick Piggin return va; 1367db64fe02SNick Piggin } 1368db64fe02SNick Piggin 1369db64fe02SNick Piggin /*** Per cpu kva allocator ***/ 1370db64fe02SNick Piggin 1371db64fe02SNick Piggin /* 1372db64fe02SNick Piggin * vmap space is limited especially on 32 bit architectures. Ensure there is 1373db64fe02SNick Piggin * room for at least 16 percpu vmap blocks per CPU. 1374db64fe02SNick Piggin */ 1375db64fe02SNick Piggin /* 1376db64fe02SNick Piggin * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able 1377db64fe02SNick Piggin * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess 1378db64fe02SNick Piggin * instead (we just need a rough idea) 1379db64fe02SNick Piggin */ 1380db64fe02SNick Piggin #if BITS_PER_LONG == 32 1381db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024) 1382db64fe02SNick Piggin #else 1383db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024*1024) 1384db64fe02SNick Piggin #endif 1385db64fe02SNick Piggin 1386db64fe02SNick Piggin #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) 1387db64fe02SNick Piggin #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ 1388db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ 1389db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) 1390db64fe02SNick Piggin #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ 1391db64fe02SNick Piggin #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ 1392f982f915SClemens Ladisch #define VMAP_BBMAP_BITS \ 1393f982f915SClemens Ladisch VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ 1394db64fe02SNick Piggin VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ 1395f982f915SClemens Ladisch VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16)) 1396db64fe02SNick Piggin 1397db64fe02SNick Piggin #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) 1398db64fe02SNick Piggin 1399db64fe02SNick Piggin struct vmap_block_queue { 1400db64fe02SNick Piggin spinlock_t lock; 1401db64fe02SNick Piggin struct list_head free; 1402db64fe02SNick Piggin }; 1403db64fe02SNick Piggin 1404db64fe02SNick Piggin struct vmap_block { 1405db64fe02SNick Piggin spinlock_t lock; 1406db64fe02SNick Piggin struct vmap_area *va; 1407db64fe02SNick Piggin unsigned long free, dirty; 14087d61bfe8SRoman Pen unsigned long dirty_min, dirty_max; /*< dirty range */ 1409db64fe02SNick Piggin struct list_head free_list; 1410db64fe02SNick Piggin struct rcu_head rcu_head; 141102b709dfSNick Piggin struct list_head purge; 1412db64fe02SNick Piggin }; 1413db64fe02SNick Piggin 1414db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ 1415db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); 1416db64fe02SNick Piggin 1417db64fe02SNick Piggin /* 1418db64fe02SNick Piggin * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block 1419db64fe02SNick Piggin * in the free path. Could get rid of this if we change the API to return a 1420db64fe02SNick Piggin * "cookie" from alloc, to be passed to free. But no big deal yet. 1421db64fe02SNick Piggin */ 1422db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_block_tree_lock); 1423db64fe02SNick Piggin static RADIX_TREE(vmap_block_tree, GFP_ATOMIC); 1424db64fe02SNick Piggin 1425db64fe02SNick Piggin /* 1426db64fe02SNick Piggin * We should probably have a fallback mechanism to allocate virtual memory 1427db64fe02SNick Piggin * out of partially filled vmap blocks. However vmap block sizing should be 1428db64fe02SNick Piggin * fairly reasonable according to the vmalloc size, so it shouldn't be a 1429db64fe02SNick Piggin * big problem. 1430db64fe02SNick Piggin */ 1431db64fe02SNick Piggin 1432db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr) 1433db64fe02SNick Piggin { 1434db64fe02SNick Piggin addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); 1435db64fe02SNick Piggin addr /= VMAP_BLOCK_SIZE; 1436db64fe02SNick Piggin return addr; 1437db64fe02SNick Piggin } 1438db64fe02SNick Piggin 1439cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off) 1440cf725ce2SRoman Pen { 1441cf725ce2SRoman Pen unsigned long addr; 1442cf725ce2SRoman Pen 1443cf725ce2SRoman Pen addr = va_start + (pages_off << PAGE_SHIFT); 1444cf725ce2SRoman Pen BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start)); 1445cf725ce2SRoman Pen return (void *)addr; 1446cf725ce2SRoman Pen } 1447cf725ce2SRoman Pen 1448cf725ce2SRoman Pen /** 1449cf725ce2SRoman Pen * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this 1450cf725ce2SRoman Pen * block. Of course pages number can't exceed VMAP_BBMAP_BITS 1451cf725ce2SRoman Pen * @order: how many 2^order pages should be occupied in newly allocated block 1452cf725ce2SRoman Pen * @gfp_mask: flags for the page level allocator 1453cf725ce2SRoman Pen * 1454a862f68aSMike Rapoport * Return: virtual address in a newly allocated block or ERR_PTR(-errno) 1455cf725ce2SRoman Pen */ 1456cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) 1457db64fe02SNick Piggin { 1458db64fe02SNick Piggin struct vmap_block_queue *vbq; 1459db64fe02SNick Piggin struct vmap_block *vb; 1460db64fe02SNick Piggin struct vmap_area *va; 1461db64fe02SNick Piggin unsigned long vb_idx; 1462db64fe02SNick Piggin int node, err; 1463cf725ce2SRoman Pen void *vaddr; 1464db64fe02SNick Piggin 1465db64fe02SNick Piggin node = numa_node_id(); 1466db64fe02SNick Piggin 1467db64fe02SNick Piggin vb = kmalloc_node(sizeof(struct vmap_block), 1468db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 1469db64fe02SNick Piggin if (unlikely(!vb)) 1470db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 1471db64fe02SNick Piggin 1472db64fe02SNick Piggin va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, 1473db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, 1474db64fe02SNick Piggin node, gfp_mask); 1475ddf9c6d4STobias Klauser if (IS_ERR(va)) { 1476db64fe02SNick Piggin kfree(vb); 1477e7d86340SJulia Lawall return ERR_CAST(va); 1478db64fe02SNick Piggin } 1479db64fe02SNick Piggin 1480db64fe02SNick Piggin err = radix_tree_preload(gfp_mask); 1481db64fe02SNick Piggin if (unlikely(err)) { 1482db64fe02SNick Piggin kfree(vb); 1483db64fe02SNick Piggin free_vmap_area(va); 1484db64fe02SNick Piggin return ERR_PTR(err); 1485db64fe02SNick Piggin } 1486db64fe02SNick Piggin 1487cf725ce2SRoman Pen vaddr = vmap_block_vaddr(va->va_start, 0); 1488db64fe02SNick Piggin spin_lock_init(&vb->lock); 1489db64fe02SNick Piggin vb->va = va; 1490cf725ce2SRoman Pen /* At least something should be left free */ 1491cf725ce2SRoman Pen BUG_ON(VMAP_BBMAP_BITS <= (1UL << order)); 1492cf725ce2SRoman Pen vb->free = VMAP_BBMAP_BITS - (1UL << order); 1493db64fe02SNick Piggin vb->dirty = 0; 14947d61bfe8SRoman Pen vb->dirty_min = VMAP_BBMAP_BITS; 14957d61bfe8SRoman Pen vb->dirty_max = 0; 1496db64fe02SNick Piggin INIT_LIST_HEAD(&vb->free_list); 1497db64fe02SNick Piggin 1498db64fe02SNick Piggin vb_idx = addr_to_vb_idx(va->va_start); 1499db64fe02SNick Piggin spin_lock(&vmap_block_tree_lock); 1500db64fe02SNick Piggin err = radix_tree_insert(&vmap_block_tree, vb_idx, vb); 1501db64fe02SNick Piggin spin_unlock(&vmap_block_tree_lock); 1502db64fe02SNick Piggin BUG_ON(err); 1503db64fe02SNick Piggin radix_tree_preload_end(); 1504db64fe02SNick Piggin 1505db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 1506db64fe02SNick Piggin spin_lock(&vbq->lock); 150768ac546fSRoman Pen list_add_tail_rcu(&vb->free_list, &vbq->free); 1508db64fe02SNick Piggin spin_unlock(&vbq->lock); 15093f04ba85STejun Heo put_cpu_var(vmap_block_queue); 1510db64fe02SNick Piggin 1511cf725ce2SRoman Pen return vaddr; 1512db64fe02SNick Piggin } 1513db64fe02SNick Piggin 1514db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb) 1515db64fe02SNick Piggin { 1516db64fe02SNick Piggin struct vmap_block *tmp; 1517db64fe02SNick Piggin unsigned long vb_idx; 1518db64fe02SNick Piggin 1519db64fe02SNick Piggin vb_idx = addr_to_vb_idx(vb->va->va_start); 1520db64fe02SNick Piggin spin_lock(&vmap_block_tree_lock); 1521db64fe02SNick Piggin tmp = radix_tree_delete(&vmap_block_tree, vb_idx); 1522db64fe02SNick Piggin spin_unlock(&vmap_block_tree_lock); 1523db64fe02SNick Piggin BUG_ON(tmp != vb); 1524db64fe02SNick Piggin 152564141da5SJeremy Fitzhardinge free_vmap_area_noflush(vb->va); 152622a3c7d1SLai Jiangshan kfree_rcu(vb, rcu_head); 1527db64fe02SNick Piggin } 1528db64fe02SNick Piggin 152902b709dfSNick Piggin static void purge_fragmented_blocks(int cpu) 153002b709dfSNick Piggin { 153102b709dfSNick Piggin LIST_HEAD(purge); 153202b709dfSNick Piggin struct vmap_block *vb; 153302b709dfSNick Piggin struct vmap_block *n_vb; 153402b709dfSNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 153502b709dfSNick Piggin 153602b709dfSNick Piggin rcu_read_lock(); 153702b709dfSNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 153802b709dfSNick Piggin 153902b709dfSNick Piggin if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS)) 154002b709dfSNick Piggin continue; 154102b709dfSNick Piggin 154202b709dfSNick Piggin spin_lock(&vb->lock); 154302b709dfSNick Piggin if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) { 154402b709dfSNick Piggin vb->free = 0; /* prevent further allocs after releasing lock */ 154502b709dfSNick Piggin vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */ 15467d61bfe8SRoman Pen vb->dirty_min = 0; 15477d61bfe8SRoman Pen vb->dirty_max = VMAP_BBMAP_BITS; 154802b709dfSNick Piggin spin_lock(&vbq->lock); 154902b709dfSNick Piggin list_del_rcu(&vb->free_list); 155002b709dfSNick Piggin spin_unlock(&vbq->lock); 155102b709dfSNick Piggin spin_unlock(&vb->lock); 155202b709dfSNick Piggin list_add_tail(&vb->purge, &purge); 155302b709dfSNick Piggin } else 155402b709dfSNick Piggin spin_unlock(&vb->lock); 155502b709dfSNick Piggin } 155602b709dfSNick Piggin rcu_read_unlock(); 155702b709dfSNick Piggin 155802b709dfSNick Piggin list_for_each_entry_safe(vb, n_vb, &purge, purge) { 155902b709dfSNick Piggin list_del(&vb->purge); 156002b709dfSNick Piggin free_vmap_block(vb); 156102b709dfSNick Piggin } 156202b709dfSNick Piggin } 156302b709dfSNick Piggin 156402b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void) 156502b709dfSNick Piggin { 156602b709dfSNick Piggin int cpu; 156702b709dfSNick Piggin 156802b709dfSNick Piggin for_each_possible_cpu(cpu) 156902b709dfSNick Piggin purge_fragmented_blocks(cpu); 157002b709dfSNick Piggin } 157102b709dfSNick Piggin 1572db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask) 1573db64fe02SNick Piggin { 1574db64fe02SNick Piggin struct vmap_block_queue *vbq; 1575db64fe02SNick Piggin struct vmap_block *vb; 1576cf725ce2SRoman Pen void *vaddr = NULL; 1577db64fe02SNick Piggin unsigned int order; 1578db64fe02SNick Piggin 1579891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 1580db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 1581aa91c4d8SJan Kara if (WARN_ON(size == 0)) { 1582aa91c4d8SJan Kara /* 1583aa91c4d8SJan Kara * Allocating 0 bytes isn't what caller wants since 1584aa91c4d8SJan Kara * get_order(0) returns funny result. Just warn and terminate 1585aa91c4d8SJan Kara * early. 1586aa91c4d8SJan Kara */ 1587aa91c4d8SJan Kara return NULL; 1588aa91c4d8SJan Kara } 1589db64fe02SNick Piggin order = get_order(size); 1590db64fe02SNick Piggin 1591db64fe02SNick Piggin rcu_read_lock(); 1592db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 1593db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 1594cf725ce2SRoman Pen unsigned long pages_off; 1595db64fe02SNick Piggin 1596db64fe02SNick Piggin spin_lock(&vb->lock); 1597cf725ce2SRoman Pen if (vb->free < (1UL << order)) { 1598cf725ce2SRoman Pen spin_unlock(&vb->lock); 1599cf725ce2SRoman Pen continue; 1600cf725ce2SRoman Pen } 160102b709dfSNick Piggin 1602cf725ce2SRoman Pen pages_off = VMAP_BBMAP_BITS - vb->free; 1603cf725ce2SRoman Pen vaddr = vmap_block_vaddr(vb->va->va_start, pages_off); 1604db64fe02SNick Piggin vb->free -= 1UL << order; 1605db64fe02SNick Piggin if (vb->free == 0) { 1606db64fe02SNick Piggin spin_lock(&vbq->lock); 1607de560423SNick Piggin list_del_rcu(&vb->free_list); 1608db64fe02SNick Piggin spin_unlock(&vbq->lock); 1609db64fe02SNick Piggin } 1610cf725ce2SRoman Pen 1611db64fe02SNick Piggin spin_unlock(&vb->lock); 1612db64fe02SNick Piggin break; 1613db64fe02SNick Piggin } 161402b709dfSNick Piggin 16153f04ba85STejun Heo put_cpu_var(vmap_block_queue); 1616db64fe02SNick Piggin rcu_read_unlock(); 1617db64fe02SNick Piggin 1618cf725ce2SRoman Pen /* Allocate new block if nothing was found */ 1619cf725ce2SRoman Pen if (!vaddr) 1620cf725ce2SRoman Pen vaddr = new_vmap_block(order, gfp_mask); 1621db64fe02SNick Piggin 1622cf725ce2SRoman Pen return vaddr; 1623db64fe02SNick Piggin } 1624db64fe02SNick Piggin 1625db64fe02SNick Piggin static void vb_free(const void *addr, unsigned long size) 1626db64fe02SNick Piggin { 1627db64fe02SNick Piggin unsigned long offset; 1628db64fe02SNick Piggin unsigned long vb_idx; 1629db64fe02SNick Piggin unsigned int order; 1630db64fe02SNick Piggin struct vmap_block *vb; 1631db64fe02SNick Piggin 1632891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 1633db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 1634b29acbdcSNick Piggin 1635b29acbdcSNick Piggin flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size); 1636b29acbdcSNick Piggin 1637db64fe02SNick Piggin order = get_order(size); 1638db64fe02SNick Piggin 1639db64fe02SNick Piggin offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1); 16407d61bfe8SRoman Pen offset >>= PAGE_SHIFT; 1641db64fe02SNick Piggin 1642db64fe02SNick Piggin vb_idx = addr_to_vb_idx((unsigned long)addr); 1643db64fe02SNick Piggin rcu_read_lock(); 1644db64fe02SNick Piggin vb = radix_tree_lookup(&vmap_block_tree, vb_idx); 1645db64fe02SNick Piggin rcu_read_unlock(); 1646db64fe02SNick Piggin BUG_ON(!vb); 1647db64fe02SNick Piggin 164864141da5SJeremy Fitzhardinge vunmap_page_range((unsigned long)addr, (unsigned long)addr + size); 164964141da5SJeremy Fitzhardinge 165082a2e924SChintan Pandya if (debug_pagealloc_enabled()) 165182a2e924SChintan Pandya flush_tlb_kernel_range((unsigned long)addr, 165282a2e924SChintan Pandya (unsigned long)addr + size); 165382a2e924SChintan Pandya 1654db64fe02SNick Piggin spin_lock(&vb->lock); 16557d61bfe8SRoman Pen 16567d61bfe8SRoman Pen /* Expand dirty range */ 16577d61bfe8SRoman Pen vb->dirty_min = min(vb->dirty_min, offset); 16587d61bfe8SRoman Pen vb->dirty_max = max(vb->dirty_max, offset + (1UL << order)); 1659d086817dSMinChan Kim 1660db64fe02SNick Piggin vb->dirty += 1UL << order; 1661db64fe02SNick Piggin if (vb->dirty == VMAP_BBMAP_BITS) { 1662de560423SNick Piggin BUG_ON(vb->free); 1663db64fe02SNick Piggin spin_unlock(&vb->lock); 1664db64fe02SNick Piggin free_vmap_block(vb); 1665db64fe02SNick Piggin } else 1666db64fe02SNick Piggin spin_unlock(&vb->lock); 1667db64fe02SNick Piggin } 1668db64fe02SNick Piggin 1669868b104dSRick Edgecombe static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush) 1670db64fe02SNick Piggin { 1671db64fe02SNick Piggin int cpu; 1672db64fe02SNick Piggin 16739b463334SJeremy Fitzhardinge if (unlikely(!vmap_initialized)) 16749b463334SJeremy Fitzhardinge return; 16759b463334SJeremy Fitzhardinge 16765803ed29SChristoph Hellwig might_sleep(); 16775803ed29SChristoph Hellwig 1678db64fe02SNick Piggin for_each_possible_cpu(cpu) { 1679db64fe02SNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 1680db64fe02SNick Piggin struct vmap_block *vb; 1681db64fe02SNick Piggin 1682db64fe02SNick Piggin rcu_read_lock(); 1683db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 1684db64fe02SNick Piggin spin_lock(&vb->lock); 16857d61bfe8SRoman Pen if (vb->dirty) { 16867d61bfe8SRoman Pen unsigned long va_start = vb->va->va_start; 1687db64fe02SNick Piggin unsigned long s, e; 1688b136be5eSJoonsoo Kim 16897d61bfe8SRoman Pen s = va_start + (vb->dirty_min << PAGE_SHIFT); 16907d61bfe8SRoman Pen e = va_start + (vb->dirty_max << PAGE_SHIFT); 1691db64fe02SNick Piggin 16927d61bfe8SRoman Pen start = min(s, start); 16937d61bfe8SRoman Pen end = max(e, end); 16947d61bfe8SRoman Pen 1695db64fe02SNick Piggin flush = 1; 1696db64fe02SNick Piggin } 1697db64fe02SNick Piggin spin_unlock(&vb->lock); 1698db64fe02SNick Piggin } 1699db64fe02SNick Piggin rcu_read_unlock(); 1700db64fe02SNick Piggin } 1701db64fe02SNick Piggin 1702f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 17030574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 17040574ecd1SChristoph Hellwig if (!__purge_vmap_area_lazy(start, end) && flush) 17050574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 1706f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 1707db64fe02SNick Piggin } 1708868b104dSRick Edgecombe 1709868b104dSRick Edgecombe /** 1710868b104dSRick Edgecombe * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer 1711868b104dSRick Edgecombe * 1712868b104dSRick Edgecombe * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily 1713868b104dSRick Edgecombe * to amortize TLB flushing overheads. What this means is that any page you 1714868b104dSRick Edgecombe * have now, may, in a former life, have been mapped into kernel virtual 1715868b104dSRick Edgecombe * address by the vmap layer and so there might be some CPUs with TLB entries 1716868b104dSRick Edgecombe * still referencing that page (additional to the regular 1:1 kernel mapping). 1717868b104dSRick Edgecombe * 1718868b104dSRick Edgecombe * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can 1719868b104dSRick Edgecombe * be sure that none of the pages we have control over will have any aliases 1720868b104dSRick Edgecombe * from the vmap layer. 1721868b104dSRick Edgecombe */ 1722868b104dSRick Edgecombe void vm_unmap_aliases(void) 1723868b104dSRick Edgecombe { 1724868b104dSRick Edgecombe unsigned long start = ULONG_MAX, end = 0; 1725868b104dSRick Edgecombe int flush = 0; 1726868b104dSRick Edgecombe 1727868b104dSRick Edgecombe _vm_unmap_aliases(start, end, flush); 1728868b104dSRick Edgecombe } 1729db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases); 1730db64fe02SNick Piggin 1731db64fe02SNick Piggin /** 1732db64fe02SNick Piggin * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram 1733db64fe02SNick Piggin * @mem: the pointer returned by vm_map_ram 1734db64fe02SNick Piggin * @count: the count passed to that vm_map_ram call (cannot unmap partial) 1735db64fe02SNick Piggin */ 1736db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count) 1737db64fe02SNick Piggin { 173865ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 1739db64fe02SNick Piggin unsigned long addr = (unsigned long)mem; 17409c3acf60SChristoph Hellwig struct vmap_area *va; 1741db64fe02SNick Piggin 17425803ed29SChristoph Hellwig might_sleep(); 1743db64fe02SNick Piggin BUG_ON(!addr); 1744db64fe02SNick Piggin BUG_ON(addr < VMALLOC_START); 1745db64fe02SNick Piggin BUG_ON(addr > VMALLOC_END); 1746a1c0b1a0SShawn Lin BUG_ON(!PAGE_ALIGNED(addr)); 1747db64fe02SNick Piggin 17489c3acf60SChristoph Hellwig if (likely(count <= VMAP_MAX_ALLOC)) { 174905e3ff95SChintan Pandya debug_check_no_locks_freed(mem, size); 1750db64fe02SNick Piggin vb_free(mem, size); 17519c3acf60SChristoph Hellwig return; 17529c3acf60SChristoph Hellwig } 17539c3acf60SChristoph Hellwig 17549c3acf60SChristoph Hellwig va = find_vmap_area(addr); 17559c3acf60SChristoph Hellwig BUG_ON(!va); 175605e3ff95SChintan Pandya debug_check_no_locks_freed((void *)va->va_start, 175705e3ff95SChintan Pandya (va->va_end - va->va_start)); 17589c3acf60SChristoph Hellwig free_unmap_vmap_area(va); 1759db64fe02SNick Piggin } 1760db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram); 1761db64fe02SNick Piggin 1762db64fe02SNick Piggin /** 1763db64fe02SNick Piggin * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) 1764db64fe02SNick Piggin * @pages: an array of pointers to the pages to be mapped 1765db64fe02SNick Piggin * @count: number of pages 1766db64fe02SNick Piggin * @node: prefer to allocate data structures on this node 1767db64fe02SNick Piggin * @prot: memory protection to use. PAGE_KERNEL for regular RAM 1768e99c97adSRandy Dunlap * 176936437638SGioh Kim * If you use this function for less than VMAP_MAX_ALLOC pages, it could be 177036437638SGioh Kim * faster than vmap so it's good. But if you mix long-life and short-life 177136437638SGioh Kim * objects with vm_map_ram(), it could consume lots of address space through 177236437638SGioh Kim * fragmentation (especially on a 32bit machine). You could see failures in 177336437638SGioh Kim * the end. Please use this function for short-lived objects. 177436437638SGioh Kim * 1775e99c97adSRandy Dunlap * Returns: a pointer to the address that has been mapped, or %NULL on failure 1776db64fe02SNick Piggin */ 1777db64fe02SNick Piggin void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) 1778db64fe02SNick Piggin { 177965ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 1780db64fe02SNick Piggin unsigned long addr; 1781db64fe02SNick Piggin void *mem; 1782db64fe02SNick Piggin 1783db64fe02SNick Piggin if (likely(count <= VMAP_MAX_ALLOC)) { 1784db64fe02SNick Piggin mem = vb_alloc(size, GFP_KERNEL); 1785db64fe02SNick Piggin if (IS_ERR(mem)) 1786db64fe02SNick Piggin return NULL; 1787db64fe02SNick Piggin addr = (unsigned long)mem; 1788db64fe02SNick Piggin } else { 1789db64fe02SNick Piggin struct vmap_area *va; 1790db64fe02SNick Piggin va = alloc_vmap_area(size, PAGE_SIZE, 1791db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); 1792db64fe02SNick Piggin if (IS_ERR(va)) 1793db64fe02SNick Piggin return NULL; 1794db64fe02SNick Piggin 1795db64fe02SNick Piggin addr = va->va_start; 1796db64fe02SNick Piggin mem = (void *)addr; 1797db64fe02SNick Piggin } 1798db64fe02SNick Piggin if (vmap_page_range(addr, addr + size, prot, pages) < 0) { 1799db64fe02SNick Piggin vm_unmap_ram(mem, count); 1800db64fe02SNick Piggin return NULL; 1801db64fe02SNick Piggin } 1802db64fe02SNick Piggin return mem; 1803db64fe02SNick Piggin } 1804db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram); 1805db64fe02SNick Piggin 18064341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata; 180792eac168SMike Rapoport 1808f0aa6617STejun Heo /** 1809be9b7335SNicolas Pitre * vm_area_add_early - add vmap area early during boot 1810be9b7335SNicolas Pitre * @vm: vm_struct to add 1811be9b7335SNicolas Pitre * 1812be9b7335SNicolas Pitre * This function is used to add fixed kernel vm area to vmlist before 1813be9b7335SNicolas Pitre * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags 1814be9b7335SNicolas Pitre * should contain proper values and the other fields should be zero. 1815be9b7335SNicolas Pitre * 1816be9b7335SNicolas Pitre * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 1817be9b7335SNicolas Pitre */ 1818be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm) 1819be9b7335SNicolas Pitre { 1820be9b7335SNicolas Pitre struct vm_struct *tmp, **p; 1821be9b7335SNicolas Pitre 1822be9b7335SNicolas Pitre BUG_ON(vmap_initialized); 1823be9b7335SNicolas Pitre for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { 1824be9b7335SNicolas Pitre if (tmp->addr >= vm->addr) { 1825be9b7335SNicolas Pitre BUG_ON(tmp->addr < vm->addr + vm->size); 1826be9b7335SNicolas Pitre break; 1827be9b7335SNicolas Pitre } else 1828be9b7335SNicolas Pitre BUG_ON(tmp->addr + tmp->size > vm->addr); 1829be9b7335SNicolas Pitre } 1830be9b7335SNicolas Pitre vm->next = *p; 1831be9b7335SNicolas Pitre *p = vm; 1832be9b7335SNicolas Pitre } 1833be9b7335SNicolas Pitre 1834be9b7335SNicolas Pitre /** 1835f0aa6617STejun Heo * vm_area_register_early - register vmap area early during boot 1836f0aa6617STejun Heo * @vm: vm_struct to register 1837c0c0a293STejun Heo * @align: requested alignment 1838f0aa6617STejun Heo * 1839f0aa6617STejun Heo * This function is used to register kernel vm area before 1840f0aa6617STejun Heo * vmalloc_init() is called. @vm->size and @vm->flags should contain 1841f0aa6617STejun Heo * proper values on entry and other fields should be zero. On return, 1842f0aa6617STejun Heo * vm->addr contains the allocated address. 1843f0aa6617STejun Heo * 1844f0aa6617STejun Heo * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 1845f0aa6617STejun Heo */ 1846c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align) 1847f0aa6617STejun Heo { 1848f0aa6617STejun Heo static size_t vm_init_off __initdata; 1849c0c0a293STejun Heo unsigned long addr; 1850f0aa6617STejun Heo 1851c0c0a293STejun Heo addr = ALIGN(VMALLOC_START + vm_init_off, align); 1852c0c0a293STejun Heo vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START; 1853c0c0a293STejun Heo 1854c0c0a293STejun Heo vm->addr = (void *)addr; 1855f0aa6617STejun Heo 1856be9b7335SNicolas Pitre vm_area_add_early(vm); 1857f0aa6617STejun Heo } 1858f0aa6617STejun Heo 185968ad4a33SUladzislau Rezki (Sony) static void vmap_init_free_space(void) 186068ad4a33SUladzislau Rezki (Sony) { 186168ad4a33SUladzislau Rezki (Sony) unsigned long vmap_start = 1; 186268ad4a33SUladzislau Rezki (Sony) const unsigned long vmap_end = ULONG_MAX; 186368ad4a33SUladzislau Rezki (Sony) struct vmap_area *busy, *free; 186468ad4a33SUladzislau Rezki (Sony) 186568ad4a33SUladzislau Rezki (Sony) /* 186668ad4a33SUladzislau Rezki (Sony) * B F B B B F 186768ad4a33SUladzislau Rezki (Sony) * -|-----|.....|-----|-----|-----|.....|- 186868ad4a33SUladzislau Rezki (Sony) * | The KVA space | 186968ad4a33SUladzislau Rezki (Sony) * |<--------------------------------->| 187068ad4a33SUladzislau Rezki (Sony) */ 187168ad4a33SUladzislau Rezki (Sony) list_for_each_entry(busy, &vmap_area_list, list) { 187268ad4a33SUladzislau Rezki (Sony) if (busy->va_start - vmap_start > 0) { 187368ad4a33SUladzislau Rezki (Sony) free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 187468ad4a33SUladzislau Rezki (Sony) if (!WARN_ON_ONCE(!free)) { 187568ad4a33SUladzislau Rezki (Sony) free->va_start = vmap_start; 187668ad4a33SUladzislau Rezki (Sony) free->va_end = busy->va_start; 187768ad4a33SUladzislau Rezki (Sony) 187868ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(free, NULL, 187968ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, 188068ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list); 188168ad4a33SUladzislau Rezki (Sony) } 188268ad4a33SUladzislau Rezki (Sony) } 188368ad4a33SUladzislau Rezki (Sony) 188468ad4a33SUladzislau Rezki (Sony) vmap_start = busy->va_end; 188568ad4a33SUladzislau Rezki (Sony) } 188668ad4a33SUladzislau Rezki (Sony) 188768ad4a33SUladzislau Rezki (Sony) if (vmap_end - vmap_start > 0) { 188868ad4a33SUladzislau Rezki (Sony) free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 188968ad4a33SUladzislau Rezki (Sony) if (!WARN_ON_ONCE(!free)) { 189068ad4a33SUladzislau Rezki (Sony) free->va_start = vmap_start; 189168ad4a33SUladzislau Rezki (Sony) free->va_end = vmap_end; 189268ad4a33SUladzislau Rezki (Sony) 189368ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(free, NULL, 189468ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, 189568ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list); 189668ad4a33SUladzislau Rezki (Sony) } 189768ad4a33SUladzislau Rezki (Sony) } 189868ad4a33SUladzislau Rezki (Sony) } 189968ad4a33SUladzislau Rezki (Sony) 1900db64fe02SNick Piggin void __init vmalloc_init(void) 1901db64fe02SNick Piggin { 1902822c18f2SIvan Kokshaysky struct vmap_area *va; 1903822c18f2SIvan Kokshaysky struct vm_struct *tmp; 1904db64fe02SNick Piggin int i; 1905db64fe02SNick Piggin 190668ad4a33SUladzislau Rezki (Sony) /* 190768ad4a33SUladzislau Rezki (Sony) * Create the cache for vmap_area objects. 190868ad4a33SUladzislau Rezki (Sony) */ 190968ad4a33SUladzislau Rezki (Sony) vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC); 191068ad4a33SUladzislau Rezki (Sony) 1911db64fe02SNick Piggin for_each_possible_cpu(i) { 1912db64fe02SNick Piggin struct vmap_block_queue *vbq; 191332fcfd40SAl Viro struct vfree_deferred *p; 1914db64fe02SNick Piggin 1915db64fe02SNick Piggin vbq = &per_cpu(vmap_block_queue, i); 1916db64fe02SNick Piggin spin_lock_init(&vbq->lock); 1917db64fe02SNick Piggin INIT_LIST_HEAD(&vbq->free); 191832fcfd40SAl Viro p = &per_cpu(vfree_deferred, i); 191932fcfd40SAl Viro init_llist_head(&p->list); 192032fcfd40SAl Viro INIT_WORK(&p->wq, free_work); 1921db64fe02SNick Piggin } 19229b463334SJeremy Fitzhardinge 1923822c18f2SIvan Kokshaysky /* Import existing vmlist entries. */ 1924822c18f2SIvan Kokshaysky for (tmp = vmlist; tmp; tmp = tmp->next) { 192568ad4a33SUladzislau Rezki (Sony) va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 192668ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(!va)) 192768ad4a33SUladzislau Rezki (Sony) continue; 192868ad4a33SUladzislau Rezki (Sony) 1929822c18f2SIvan Kokshaysky va->va_start = (unsigned long)tmp->addr; 1930822c18f2SIvan Kokshaysky va->va_end = va->va_start + tmp->size; 1931dbda591dSKyongHo va->vm = tmp; 193268ad4a33SUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 1933822c18f2SIvan Kokshaysky } 1934ca23e405STejun Heo 193568ad4a33SUladzislau Rezki (Sony) /* 193668ad4a33SUladzislau Rezki (Sony) * Now we can initialize a free vmap space. 193768ad4a33SUladzislau Rezki (Sony) */ 193868ad4a33SUladzislau Rezki (Sony) vmap_init_free_space(); 19399b463334SJeremy Fitzhardinge vmap_initialized = true; 1940db64fe02SNick Piggin } 1941db64fe02SNick Piggin 19428fc48985STejun Heo /** 19438fc48985STejun Heo * map_kernel_range_noflush - map kernel VM area with the specified pages 19448fc48985STejun Heo * @addr: start of the VM area to map 19458fc48985STejun Heo * @size: size of the VM area to map 19468fc48985STejun Heo * @prot: page protection flags to use 19478fc48985STejun Heo * @pages: pages to map 19488fc48985STejun Heo * 19498fc48985STejun Heo * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size 19508fc48985STejun Heo * specify should have been allocated using get_vm_area() and its 19518fc48985STejun Heo * friends. 19528fc48985STejun Heo * 19538fc48985STejun Heo * NOTE: 19548fc48985STejun Heo * This function does NOT do any cache flushing. The caller is 19558fc48985STejun Heo * responsible for calling flush_cache_vmap() on to-be-mapped areas 19568fc48985STejun Heo * before calling this function. 19578fc48985STejun Heo * 19588fc48985STejun Heo * RETURNS: 19598fc48985STejun Heo * The number of pages mapped on success, -errno on failure. 19608fc48985STejun Heo */ 19618fc48985STejun Heo int map_kernel_range_noflush(unsigned long addr, unsigned long size, 19628fc48985STejun Heo pgprot_t prot, struct page **pages) 19638fc48985STejun Heo { 19648fc48985STejun Heo return vmap_page_range_noflush(addr, addr + size, prot, pages); 19658fc48985STejun Heo } 19668fc48985STejun Heo 19678fc48985STejun Heo /** 19688fc48985STejun Heo * unmap_kernel_range_noflush - unmap kernel VM area 19698fc48985STejun Heo * @addr: start of the VM area to unmap 19708fc48985STejun Heo * @size: size of the VM area to unmap 19718fc48985STejun Heo * 19728fc48985STejun Heo * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size 19738fc48985STejun Heo * specify should have been allocated using get_vm_area() and its 19748fc48985STejun Heo * friends. 19758fc48985STejun Heo * 19768fc48985STejun Heo * NOTE: 19778fc48985STejun Heo * This function does NOT do any cache flushing. The caller is 19788fc48985STejun Heo * responsible for calling flush_cache_vunmap() on to-be-mapped areas 19798fc48985STejun Heo * before calling this function and flush_tlb_kernel_range() after. 19808fc48985STejun Heo */ 19818fc48985STejun Heo void unmap_kernel_range_noflush(unsigned long addr, unsigned long size) 19828fc48985STejun Heo { 19838fc48985STejun Heo vunmap_page_range(addr, addr + size); 19848fc48985STejun Heo } 198581e88fdcSHuang Ying EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush); 19868fc48985STejun Heo 19878fc48985STejun Heo /** 19888fc48985STejun Heo * unmap_kernel_range - unmap kernel VM area and flush cache and TLB 19898fc48985STejun Heo * @addr: start of the VM area to unmap 19908fc48985STejun Heo * @size: size of the VM area to unmap 19918fc48985STejun Heo * 19928fc48985STejun Heo * Similar to unmap_kernel_range_noflush() but flushes vcache before 19938fc48985STejun Heo * the unmapping and tlb after. 19948fc48985STejun Heo */ 1995db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size) 1996db64fe02SNick Piggin { 1997db64fe02SNick Piggin unsigned long end = addr + size; 1998f6fcba70STejun Heo 1999f6fcba70STejun Heo flush_cache_vunmap(addr, end); 2000db64fe02SNick Piggin vunmap_page_range(addr, end); 2001db64fe02SNick Piggin flush_tlb_kernel_range(addr, end); 2002db64fe02SNick Piggin } 200393ef6d6cSMinchan Kim EXPORT_SYMBOL_GPL(unmap_kernel_range); 2004db64fe02SNick Piggin 2005f6f8ed47SWANG Chao int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages) 2006db64fe02SNick Piggin { 2007db64fe02SNick Piggin unsigned long addr = (unsigned long)area->addr; 2008762216abSWanpeng Li unsigned long end = addr + get_vm_area_size(area); 2009db64fe02SNick Piggin int err; 2010db64fe02SNick Piggin 2011f6f8ed47SWANG Chao err = vmap_page_range(addr, end, prot, pages); 2012db64fe02SNick Piggin 2013f6f8ed47SWANG Chao return err > 0 ? 0 : err; 2014db64fe02SNick Piggin } 2015db64fe02SNick Piggin EXPORT_SYMBOL_GPL(map_vm_area); 2016db64fe02SNick Piggin 2017f5252e00SMitsuo Hayasaka static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, 20185e6cafc8SMarek Szyprowski unsigned long flags, const void *caller) 2019cf88c790STejun Heo { 2020c69480adSJoonsoo Kim spin_lock(&vmap_area_lock); 2021cf88c790STejun Heo vm->flags = flags; 2022cf88c790STejun Heo vm->addr = (void *)va->va_start; 2023cf88c790STejun Heo vm->size = va->va_end - va->va_start; 2024cf88c790STejun Heo vm->caller = caller; 2025db1aecafSMinchan Kim va->vm = vm; 2026c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 2027f5252e00SMitsuo Hayasaka } 2028cf88c790STejun Heo 202920fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm) 2030f5252e00SMitsuo Hayasaka { 2031d4033afdSJoonsoo Kim /* 203220fc02b4SZhang Yanfei * Before removing VM_UNINITIALIZED, 2033d4033afdSJoonsoo Kim * we should make sure that vm has proper values. 2034d4033afdSJoonsoo Kim * Pair with smp_rmb() in show_numa_info(). 2035d4033afdSJoonsoo Kim */ 2036d4033afdSJoonsoo Kim smp_wmb(); 203720fc02b4SZhang Yanfei vm->flags &= ~VM_UNINITIALIZED; 2038cf88c790STejun Heo } 2039cf88c790STejun Heo 2040db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size, 20412dca6999SDavid Miller unsigned long align, unsigned long flags, unsigned long start, 20425e6cafc8SMarek Szyprowski unsigned long end, int node, gfp_t gfp_mask, const void *caller) 2043db64fe02SNick Piggin { 20440006526dSKautuk Consul struct vmap_area *va; 2045db64fe02SNick Piggin struct vm_struct *area; 20461da177e4SLinus Torvalds 204752fd24caSGiridhar Pemmasani BUG_ON(in_interrupt()); 20481da177e4SLinus Torvalds size = PAGE_ALIGN(size); 204931be8309SOGAWA Hirofumi if (unlikely(!size)) 205031be8309SOGAWA Hirofumi return NULL; 20511da177e4SLinus Torvalds 2052252e5c6eSzijun_hu if (flags & VM_IOREMAP) 2053252e5c6eSzijun_hu align = 1ul << clamp_t(int, get_count_order_long(size), 2054252e5c6eSzijun_hu PAGE_SHIFT, IOREMAP_MAX_ORDER); 2055252e5c6eSzijun_hu 2056cf88c790STejun Heo area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); 20571da177e4SLinus Torvalds if (unlikely(!area)) 20581da177e4SLinus Torvalds return NULL; 20591da177e4SLinus Torvalds 206071394fe5SAndrey Ryabinin if (!(flags & VM_NO_GUARD)) 20611da177e4SLinus Torvalds size += PAGE_SIZE; 20621da177e4SLinus Torvalds 2063db64fe02SNick Piggin va = alloc_vmap_area(size, align, start, end, node, gfp_mask); 2064db64fe02SNick Piggin if (IS_ERR(va)) { 2065db64fe02SNick Piggin kfree(area); 2066db64fe02SNick Piggin return NULL; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds 2069f5252e00SMitsuo Hayasaka setup_vmalloc_vm(area, va, flags, caller); 2070f5252e00SMitsuo Hayasaka 20711da177e4SLinus Torvalds return area; 20721da177e4SLinus Torvalds } 20731da177e4SLinus Torvalds 2074930fc45aSChristoph Lameter struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags, 2075930fc45aSChristoph Lameter unsigned long start, unsigned long end) 2076930fc45aSChristoph Lameter { 207700ef2d2fSDavid Rientjes return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE, 207800ef2d2fSDavid Rientjes GFP_KERNEL, __builtin_return_address(0)); 2079930fc45aSChristoph Lameter } 20805992b6daSRusty Russell EXPORT_SYMBOL_GPL(__get_vm_area); 2081930fc45aSChristoph Lameter 2082c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, 2083c2968612SBenjamin Herrenschmidt unsigned long start, unsigned long end, 20845e6cafc8SMarek Szyprowski const void *caller) 2085c2968612SBenjamin Herrenschmidt { 208600ef2d2fSDavid Rientjes return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE, 208700ef2d2fSDavid Rientjes GFP_KERNEL, caller); 2088c2968612SBenjamin Herrenschmidt } 2089c2968612SBenjamin Herrenschmidt 20901da177e4SLinus Torvalds /** 2091183ff22bSSimon Arlott * get_vm_area - reserve a contiguous kernel virtual area 20921da177e4SLinus Torvalds * @size: size of the area 20931da177e4SLinus Torvalds * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC 20941da177e4SLinus Torvalds * 20951da177e4SLinus Torvalds * Search an area of @size in the kernel virtual mapping area, 20961da177e4SLinus Torvalds * and reserved it for out purposes. Returns the area descriptor 20971da177e4SLinus Torvalds * on success or %NULL on failure. 2098a862f68aSMike Rapoport * 2099a862f68aSMike Rapoport * Return: the area descriptor on success or %NULL on failure. 21001da177e4SLinus Torvalds */ 21011da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) 21021da177e4SLinus Torvalds { 21032dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 210400ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, 210500ef2d2fSDavid Rientjes __builtin_return_address(0)); 210623016969SChristoph Lameter } 210723016969SChristoph Lameter 210823016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, 21095e6cafc8SMarek Szyprowski const void *caller) 211023016969SChristoph Lameter { 21112dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 211200ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, caller); 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 2115e9da6e99SMarek Szyprowski /** 2116e9da6e99SMarek Szyprowski * find_vm_area - find a continuous kernel virtual area 2117e9da6e99SMarek Szyprowski * @addr: base address 2118e9da6e99SMarek Szyprowski * 2119e9da6e99SMarek Szyprowski * Search for the kernel VM area starting at @addr, and return it. 2120e9da6e99SMarek Szyprowski * It is up to the caller to do all required locking to keep the returned 2121e9da6e99SMarek Szyprowski * pointer valid. 2122a862f68aSMike Rapoport * 2123a862f68aSMike Rapoport * Return: pointer to the found area or %NULL on faulure 2124e9da6e99SMarek Szyprowski */ 2125e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr) 212683342314SNick Piggin { 2127db64fe02SNick Piggin struct vmap_area *va; 212883342314SNick Piggin 2129db64fe02SNick Piggin va = find_vmap_area((unsigned long)addr); 2130688fcbfcSPengfei Li if (!va) 21317856dfebSAndi Kleen return NULL; 2132688fcbfcSPengfei Li 2133688fcbfcSPengfei Li return va->vm; 21347856dfebSAndi Kleen } 21357856dfebSAndi Kleen 21361da177e4SLinus Torvalds /** 2137183ff22bSSimon Arlott * remove_vm_area - find and remove a continuous kernel virtual area 21381da177e4SLinus Torvalds * @addr: base address 21391da177e4SLinus Torvalds * 21401da177e4SLinus Torvalds * Search for the kernel VM area starting at @addr, and remove it. 21411da177e4SLinus Torvalds * This function returns the found VM area, but using it is NOT safe 21427856dfebSAndi Kleen * on SMP machines, except for its size or flags. 2143a862f68aSMike Rapoport * 2144a862f68aSMike Rapoport * Return: pointer to the found area or %NULL on faulure 21451da177e4SLinus Torvalds */ 2146b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr) 21471da177e4SLinus Torvalds { 2148db64fe02SNick Piggin struct vmap_area *va; 2149db64fe02SNick Piggin 21505803ed29SChristoph Hellwig might_sleep(); 21515803ed29SChristoph Hellwig 2152dd3b8353SUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 2153dd3b8353SUladzislau Rezki (Sony) va = __find_vmap_area((unsigned long)addr); 2154688fcbfcSPengfei Li if (va && va->vm) { 2155db1aecafSMinchan Kim struct vm_struct *vm = va->vm; 2156f5252e00SMitsuo Hayasaka 2157c69480adSJoonsoo Kim va->vm = NULL; 2158c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 2159c69480adSJoonsoo Kim 2160a5af5aa8SAndrey Ryabinin kasan_free_shadow(vm); 2161dd32c279SKAMEZAWA Hiroyuki free_unmap_vmap_area(va); 2162dd32c279SKAMEZAWA Hiroyuki 2163db64fe02SNick Piggin return vm; 2164db64fe02SNick Piggin } 2165dd3b8353SUladzislau Rezki (Sony) 2166dd3b8353SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 2167db64fe02SNick Piggin return NULL; 21681da177e4SLinus Torvalds } 21691da177e4SLinus Torvalds 2170868b104dSRick Edgecombe static inline void set_area_direct_map(const struct vm_struct *area, 2171868b104dSRick Edgecombe int (*set_direct_map)(struct page *page)) 2172868b104dSRick Edgecombe { 2173868b104dSRick Edgecombe int i; 2174868b104dSRick Edgecombe 2175868b104dSRick Edgecombe for (i = 0; i < area->nr_pages; i++) 2176868b104dSRick Edgecombe if (page_address(area->pages[i])) 2177868b104dSRick Edgecombe set_direct_map(area->pages[i]); 2178868b104dSRick Edgecombe } 2179868b104dSRick Edgecombe 2180868b104dSRick Edgecombe /* Handle removing and resetting vm mappings related to the vm_struct. */ 2181868b104dSRick Edgecombe static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages) 2182868b104dSRick Edgecombe { 2183868b104dSRick Edgecombe unsigned long start = ULONG_MAX, end = 0; 2184868b104dSRick Edgecombe int flush_reset = area->flags & VM_FLUSH_RESET_PERMS; 218531e67340SRick Edgecombe int flush_dmap = 0; 2186868b104dSRick Edgecombe int i; 2187868b104dSRick Edgecombe 2188868b104dSRick Edgecombe remove_vm_area(area->addr); 2189868b104dSRick Edgecombe 2190868b104dSRick Edgecombe /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */ 2191868b104dSRick Edgecombe if (!flush_reset) 2192868b104dSRick Edgecombe return; 2193868b104dSRick Edgecombe 2194868b104dSRick Edgecombe /* 2195868b104dSRick Edgecombe * If not deallocating pages, just do the flush of the VM area and 2196868b104dSRick Edgecombe * return. 2197868b104dSRick Edgecombe */ 2198868b104dSRick Edgecombe if (!deallocate_pages) { 2199868b104dSRick Edgecombe vm_unmap_aliases(); 2200868b104dSRick Edgecombe return; 2201868b104dSRick Edgecombe } 2202868b104dSRick Edgecombe 2203868b104dSRick Edgecombe /* 2204868b104dSRick Edgecombe * If execution gets here, flush the vm mapping and reset the direct 2205868b104dSRick Edgecombe * map. Find the start and end range of the direct mappings to make sure 2206868b104dSRick Edgecombe * the vm_unmap_aliases() flush includes the direct map. 2207868b104dSRick Edgecombe */ 2208868b104dSRick Edgecombe for (i = 0; i < area->nr_pages; i++) { 22098e41f872SRick Edgecombe unsigned long addr = (unsigned long)page_address(area->pages[i]); 22108e41f872SRick Edgecombe if (addr) { 2211868b104dSRick Edgecombe start = min(addr, start); 22128e41f872SRick Edgecombe end = max(addr + PAGE_SIZE, end); 221331e67340SRick Edgecombe flush_dmap = 1; 2214868b104dSRick Edgecombe } 2215868b104dSRick Edgecombe } 2216868b104dSRick Edgecombe 2217868b104dSRick Edgecombe /* 2218868b104dSRick Edgecombe * Set direct map to something invalid so that it won't be cached if 2219868b104dSRick Edgecombe * there are any accesses after the TLB flush, then flush the TLB and 2220868b104dSRick Edgecombe * reset the direct map permissions to the default. 2221868b104dSRick Edgecombe */ 2222868b104dSRick Edgecombe set_area_direct_map(area, set_direct_map_invalid_noflush); 222331e67340SRick Edgecombe _vm_unmap_aliases(start, end, flush_dmap); 2224868b104dSRick Edgecombe set_area_direct_map(area, set_direct_map_default_noflush); 2225868b104dSRick Edgecombe } 2226868b104dSRick Edgecombe 2227b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages) 22281da177e4SLinus Torvalds { 22291da177e4SLinus Torvalds struct vm_struct *area; 22301da177e4SLinus Torvalds 22311da177e4SLinus Torvalds if (!addr) 22321da177e4SLinus Torvalds return; 22331da177e4SLinus Torvalds 2234e69e9d4aSHATAYAMA Daisuke if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n", 2235ab15d9b4SDan Carpenter addr)) 22361da177e4SLinus Torvalds return; 22371da177e4SLinus Torvalds 22386ade2032SLiviu Dudau area = find_vm_area(addr); 22391da177e4SLinus Torvalds if (unlikely(!area)) { 22404c8573e2SArjan van de Ven WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", 22411da177e4SLinus Torvalds addr); 22421da177e4SLinus Torvalds return; 22431da177e4SLinus Torvalds } 22441da177e4SLinus Torvalds 224505e3ff95SChintan Pandya debug_check_no_locks_freed(area->addr, get_vm_area_size(area)); 224605e3ff95SChintan Pandya debug_check_no_obj_freed(area->addr, get_vm_area_size(area)); 22479a11b49aSIngo Molnar 2248868b104dSRick Edgecombe vm_remove_mappings(area, deallocate_pages); 2249868b104dSRick Edgecombe 22501da177e4SLinus Torvalds if (deallocate_pages) { 22511da177e4SLinus Torvalds int i; 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds for (i = 0; i < area->nr_pages; i++) { 2254bf53d6f8SChristoph Lameter struct page *page = area->pages[i]; 2255bf53d6f8SChristoph Lameter 2256bf53d6f8SChristoph Lameter BUG_ON(!page); 22574949148aSVladimir Davydov __free_pages(page, 0); 22581da177e4SLinus Torvalds } 225997105f0aSRoman Gushchin atomic_long_sub(area->nr_pages, &nr_vmalloc_pages); 22601da177e4SLinus Torvalds 2261244d63eeSDavid Rientjes kvfree(area->pages); 22621da177e4SLinus Torvalds } 22631da177e4SLinus Torvalds 22641da177e4SLinus Torvalds kfree(area); 22651da177e4SLinus Torvalds return; 22661da177e4SLinus Torvalds } 22671da177e4SLinus Torvalds 2268bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr) 2269bf22e37aSAndrey Ryabinin { 2270bf22e37aSAndrey Ryabinin /* 2271bf22e37aSAndrey Ryabinin * Use raw_cpu_ptr() because this can be called from preemptible 2272bf22e37aSAndrey Ryabinin * context. Preemption is absolutely fine here, because the llist_add() 2273bf22e37aSAndrey Ryabinin * implementation is lockless, so it works even if we are adding to 2274bf22e37aSAndrey Ryabinin * nother cpu's list. schedule_work() should be fine with this too. 2275bf22e37aSAndrey Ryabinin */ 2276bf22e37aSAndrey Ryabinin struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred); 2277bf22e37aSAndrey Ryabinin 2278bf22e37aSAndrey Ryabinin if (llist_add((struct llist_node *)addr, &p->list)) 2279bf22e37aSAndrey Ryabinin schedule_work(&p->wq); 2280bf22e37aSAndrey Ryabinin } 2281bf22e37aSAndrey Ryabinin 2282bf22e37aSAndrey Ryabinin /** 2283bf22e37aSAndrey Ryabinin * vfree_atomic - release memory allocated by vmalloc() 2284bf22e37aSAndrey Ryabinin * @addr: memory base address 2285bf22e37aSAndrey Ryabinin * 2286bf22e37aSAndrey Ryabinin * This one is just like vfree() but can be called in any atomic context 2287bf22e37aSAndrey Ryabinin * except NMIs. 2288bf22e37aSAndrey Ryabinin */ 2289bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr) 2290bf22e37aSAndrey Ryabinin { 2291bf22e37aSAndrey Ryabinin BUG_ON(in_nmi()); 2292bf22e37aSAndrey Ryabinin 2293bf22e37aSAndrey Ryabinin kmemleak_free(addr); 2294bf22e37aSAndrey Ryabinin 2295bf22e37aSAndrey Ryabinin if (!addr) 2296bf22e37aSAndrey Ryabinin return; 2297bf22e37aSAndrey Ryabinin __vfree_deferred(addr); 2298bf22e37aSAndrey Ryabinin } 2299bf22e37aSAndrey Ryabinin 2300c67dc624SRoman Penyaev static void __vfree(const void *addr) 2301c67dc624SRoman Penyaev { 2302c67dc624SRoman Penyaev if (unlikely(in_interrupt())) 2303c67dc624SRoman Penyaev __vfree_deferred(addr); 2304c67dc624SRoman Penyaev else 2305c67dc624SRoman Penyaev __vunmap(addr, 1); 2306c67dc624SRoman Penyaev } 2307c67dc624SRoman Penyaev 23081da177e4SLinus Torvalds /** 23091da177e4SLinus Torvalds * vfree - release memory allocated by vmalloc() 23101da177e4SLinus Torvalds * @addr: memory base address 23111da177e4SLinus Torvalds * 2312183ff22bSSimon Arlott * Free the virtually continuous memory area starting at @addr, as 231380e93effSPekka Enberg * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is 231480e93effSPekka Enberg * NULL, no operation is performed. 23151da177e4SLinus Torvalds * 231632fcfd40SAl Viro * Must not be called in NMI context (strictly speaking, only if we don't 231732fcfd40SAl Viro * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling 231832fcfd40SAl Viro * conventions for vfree() arch-depenedent would be a really bad idea) 231932fcfd40SAl Viro * 23203ca4ea3aSAndrey Ryabinin * May sleep if called *not* from interrupt context. 23213ca4ea3aSAndrey Ryabinin * 23220e056eb5Smchehab@s-opensource.com * NOTE: assumes that the object at @addr has a size >= sizeof(llist_node) 23231da177e4SLinus Torvalds */ 2324b3bdda02SChristoph Lameter void vfree(const void *addr) 23251da177e4SLinus Torvalds { 232632fcfd40SAl Viro BUG_ON(in_nmi()); 232789219d37SCatalin Marinas 232889219d37SCatalin Marinas kmemleak_free(addr); 232989219d37SCatalin Marinas 2330a8dda165SAndrey Ryabinin might_sleep_if(!in_interrupt()); 2331a8dda165SAndrey Ryabinin 233232fcfd40SAl Viro if (!addr) 233332fcfd40SAl Viro return; 2334c67dc624SRoman Penyaev 2335c67dc624SRoman Penyaev __vfree(addr); 23361da177e4SLinus Torvalds } 23371da177e4SLinus Torvalds EXPORT_SYMBOL(vfree); 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds /** 23401da177e4SLinus Torvalds * vunmap - release virtual mapping obtained by vmap() 23411da177e4SLinus Torvalds * @addr: memory base address 23421da177e4SLinus Torvalds * 23431da177e4SLinus Torvalds * Free the virtually contiguous memory area starting at @addr, 23441da177e4SLinus Torvalds * which was created from the page array passed to vmap(). 23451da177e4SLinus Torvalds * 234680e93effSPekka Enberg * Must not be called in interrupt context. 23471da177e4SLinus Torvalds */ 2348b3bdda02SChristoph Lameter void vunmap(const void *addr) 23491da177e4SLinus Torvalds { 23501da177e4SLinus Torvalds BUG_ON(in_interrupt()); 235134754b69SPeter Zijlstra might_sleep(); 235232fcfd40SAl Viro if (addr) 23531da177e4SLinus Torvalds __vunmap(addr, 0); 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap); 23561da177e4SLinus Torvalds 23571da177e4SLinus Torvalds /** 23581da177e4SLinus Torvalds * vmap - map an array of pages into virtually contiguous space 23591da177e4SLinus Torvalds * @pages: array of page pointers 23601da177e4SLinus Torvalds * @count: number of pages to map 23611da177e4SLinus Torvalds * @flags: vm_area->flags 23621da177e4SLinus Torvalds * @prot: page protection for the mapping 23631da177e4SLinus Torvalds * 23641da177e4SLinus Torvalds * Maps @count pages from @pages into contiguous kernel virtual 23651da177e4SLinus Torvalds * space. 2366a862f68aSMike Rapoport * 2367a862f68aSMike Rapoport * Return: the address of the area or %NULL on failure 23681da177e4SLinus Torvalds */ 23691da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, 23701da177e4SLinus Torvalds unsigned long flags, pgprot_t prot) 23711da177e4SLinus Torvalds { 23721da177e4SLinus Torvalds struct vm_struct *area; 237365ee03c4SGuillermo Julián Moreno unsigned long size; /* In bytes */ 23741da177e4SLinus Torvalds 237534754b69SPeter Zijlstra might_sleep(); 237634754b69SPeter Zijlstra 2377ca79b0c2SArun KS if (count > totalram_pages()) 23781da177e4SLinus Torvalds return NULL; 23791da177e4SLinus Torvalds 238065ee03c4SGuillermo Julián Moreno size = (unsigned long)count << PAGE_SHIFT; 238165ee03c4SGuillermo Julián Moreno area = get_vm_area_caller(size, flags, __builtin_return_address(0)); 23821da177e4SLinus Torvalds if (!area) 23831da177e4SLinus Torvalds return NULL; 238423016969SChristoph Lameter 2385f6f8ed47SWANG Chao if (map_vm_area(area, prot, pages)) { 23861da177e4SLinus Torvalds vunmap(area->addr); 23871da177e4SLinus Torvalds return NULL; 23881da177e4SLinus Torvalds } 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds return area->addr; 23911da177e4SLinus Torvalds } 23921da177e4SLinus Torvalds EXPORT_SYMBOL(vmap); 23931da177e4SLinus Torvalds 23948594a21cSMichal Hocko static void *__vmalloc_node(unsigned long size, unsigned long align, 23958594a21cSMichal Hocko gfp_t gfp_mask, pgprot_t prot, 23968594a21cSMichal Hocko int node, const void *caller); 2397e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, 23983722e13cSWanpeng Li pgprot_t prot, int node) 23991da177e4SLinus Torvalds { 24001da177e4SLinus Torvalds struct page **pages; 24011da177e4SLinus Torvalds unsigned int nr_pages, array_size, i; 2402930f036bSDavid Rientjes const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; 2403704b862fSLaura Abbott const gfp_t alloc_mask = gfp_mask | __GFP_NOWARN; 2404704b862fSLaura Abbott const gfp_t highmem_mask = (gfp_mask & (GFP_DMA | GFP_DMA32)) ? 2405704b862fSLaura Abbott 0 : 2406704b862fSLaura Abbott __GFP_HIGHMEM; 24071da177e4SLinus Torvalds 2408762216abSWanpeng Li nr_pages = get_vm_area_size(area) >> PAGE_SHIFT; 24091da177e4SLinus Torvalds array_size = (nr_pages * sizeof(struct page *)); 24101da177e4SLinus Torvalds 24111da177e4SLinus Torvalds /* Please note that the recursion is strictly bounded. */ 24128757d5faSJan Kiszka if (array_size > PAGE_SIZE) { 2413704b862fSLaura Abbott pages = __vmalloc_node(array_size, 1, nested_gfp|highmem_mask, 24143722e13cSWanpeng Li PAGE_KERNEL, node, area->caller); 2415286e1ea3SAndrew Morton } else { 2416976d6dfbSJan Beulich pages = kmalloc_node(array_size, nested_gfp, node); 2417286e1ea3SAndrew Morton } 24187ea36242SAustin Kim 24197ea36242SAustin Kim if (!pages) { 24201da177e4SLinus Torvalds remove_vm_area(area->addr); 24211da177e4SLinus Torvalds kfree(area); 24221da177e4SLinus Torvalds return NULL; 24231da177e4SLinus Torvalds } 24241da177e4SLinus Torvalds 24257ea36242SAustin Kim area->pages = pages; 24267ea36242SAustin Kim area->nr_pages = nr_pages; 24277ea36242SAustin Kim 24281da177e4SLinus Torvalds for (i = 0; i < area->nr_pages; i++) { 2429bf53d6f8SChristoph Lameter struct page *page; 2430bf53d6f8SChristoph Lameter 24314b90951cSJianguo Wu if (node == NUMA_NO_NODE) 2432704b862fSLaura Abbott page = alloc_page(alloc_mask|highmem_mask); 2433930fc45aSChristoph Lameter else 2434704b862fSLaura Abbott page = alloc_pages_node(node, alloc_mask|highmem_mask, 0); 2435bf53d6f8SChristoph Lameter 2436bf53d6f8SChristoph Lameter if (unlikely(!page)) { 24371da177e4SLinus Torvalds /* Successfully allocated i pages, free them in __vunmap() */ 24381da177e4SLinus Torvalds area->nr_pages = i; 243997105f0aSRoman Gushchin atomic_long_add(area->nr_pages, &nr_vmalloc_pages); 24401da177e4SLinus Torvalds goto fail; 24411da177e4SLinus Torvalds } 2442bf53d6f8SChristoph Lameter area->pages[i] = page; 2443704b862fSLaura Abbott if (gfpflags_allow_blocking(gfp_mask|highmem_mask)) 2444660654f9SEric Dumazet cond_resched(); 24451da177e4SLinus Torvalds } 244697105f0aSRoman Gushchin atomic_long_add(area->nr_pages, &nr_vmalloc_pages); 24471da177e4SLinus Torvalds 2448f6f8ed47SWANG Chao if (map_vm_area(area, prot, pages)) 24491da177e4SLinus Torvalds goto fail; 24501da177e4SLinus Torvalds return area->addr; 24511da177e4SLinus Torvalds 24521da177e4SLinus Torvalds fail: 2453a8e99259SMichal Hocko warn_alloc(gfp_mask, NULL, 24547877cdccSMichal Hocko "vmalloc: allocation failure, allocated %ld of %ld bytes", 245522943ab1SDave Hansen (area->nr_pages*PAGE_SIZE), area->size); 2456c67dc624SRoman Penyaev __vfree(area->addr); 24571da177e4SLinus Torvalds return NULL; 24581da177e4SLinus Torvalds } 24591da177e4SLinus Torvalds 2460d0a21265SDavid Rientjes /** 2461d0a21265SDavid Rientjes * __vmalloc_node_range - allocate virtually contiguous memory 2462d0a21265SDavid Rientjes * @size: allocation size 2463d0a21265SDavid Rientjes * @align: desired alignment 2464d0a21265SDavid Rientjes * @start: vm area range start 2465d0a21265SDavid Rientjes * @end: vm area range end 2466d0a21265SDavid Rientjes * @gfp_mask: flags for the page level allocator 2467d0a21265SDavid Rientjes * @prot: protection mask for the allocated pages 2468cb9e3c29SAndrey Ryabinin * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD) 246900ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 2470d0a21265SDavid Rientjes * @caller: caller's return address 2471d0a21265SDavid Rientjes * 2472d0a21265SDavid Rientjes * Allocate enough pages to cover @size from the page level 2473d0a21265SDavid Rientjes * allocator with @gfp_mask flags. Map them into contiguous 2474d0a21265SDavid Rientjes * kernel virtual space, using a pagetable protection of @prot. 2475a862f68aSMike Rapoport * 2476a862f68aSMike Rapoport * Return: the address of the area or %NULL on failure 2477d0a21265SDavid Rientjes */ 2478d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align, 2479d0a21265SDavid Rientjes unsigned long start, unsigned long end, gfp_t gfp_mask, 2480cb9e3c29SAndrey Ryabinin pgprot_t prot, unsigned long vm_flags, int node, 2481cb9e3c29SAndrey Ryabinin const void *caller) 2482930fc45aSChristoph Lameter { 2483d0a21265SDavid Rientjes struct vm_struct *area; 2484d0a21265SDavid Rientjes void *addr; 2485d0a21265SDavid Rientjes unsigned long real_size = size; 2486d0a21265SDavid Rientjes 2487d0a21265SDavid Rientjes size = PAGE_ALIGN(size); 2488ca79b0c2SArun KS if (!size || (size >> PAGE_SHIFT) > totalram_pages()) 2489de7d2b56SJoe Perches goto fail; 2490d0a21265SDavid Rientjes 2491cb9e3c29SAndrey Ryabinin area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED | 2492cb9e3c29SAndrey Ryabinin vm_flags, start, end, node, gfp_mask, caller); 2493d0a21265SDavid Rientjes if (!area) 2494de7d2b56SJoe Perches goto fail; 2495d0a21265SDavid Rientjes 24963722e13cSWanpeng Li addr = __vmalloc_area_node(area, gfp_mask, prot, node); 24971368edf0SMel Gorman if (!addr) 2498b82225f3SWanpeng Li return NULL; 249989219d37SCatalin Marinas 250089219d37SCatalin Marinas /* 250120fc02b4SZhang Yanfei * In this function, newly allocated vm_struct has VM_UNINITIALIZED 250220fc02b4SZhang Yanfei * flag. It means that vm_struct is not fully initialized. 25034341fa45SJoonsoo Kim * Now, it is fully initialized, so remove this flag here. 2504f5252e00SMitsuo Hayasaka */ 250520fc02b4SZhang Yanfei clear_vm_uninitialized_flag(area); 2506f5252e00SMitsuo Hayasaka 250794f4a161SCatalin Marinas kmemleak_vmalloc(area, size, gfp_mask); 250889219d37SCatalin Marinas 250989219d37SCatalin Marinas return addr; 2510de7d2b56SJoe Perches 2511de7d2b56SJoe Perches fail: 2512a8e99259SMichal Hocko warn_alloc(gfp_mask, NULL, 25137877cdccSMichal Hocko "vmalloc: allocation failure: %lu bytes", real_size); 2514de7d2b56SJoe Perches return NULL; 2515930fc45aSChristoph Lameter } 2516930fc45aSChristoph Lameter 2517153178edSUladzislau Rezki (Sony) /* 2518153178edSUladzislau Rezki (Sony) * This is only for performance analysis of vmalloc and stress purpose. 2519153178edSUladzislau Rezki (Sony) * It is required by vmalloc test module, therefore do not use it other 2520153178edSUladzislau Rezki (Sony) * than that. 2521153178edSUladzislau Rezki (Sony) */ 2522153178edSUladzislau Rezki (Sony) #ifdef CONFIG_TEST_VMALLOC_MODULE 2523153178edSUladzislau Rezki (Sony) EXPORT_SYMBOL_GPL(__vmalloc_node_range); 2524153178edSUladzislau Rezki (Sony) #endif 2525153178edSUladzislau Rezki (Sony) 25261da177e4SLinus Torvalds /** 2527930fc45aSChristoph Lameter * __vmalloc_node - allocate virtually contiguous memory 25281da177e4SLinus Torvalds * @size: allocation size 25292dca6999SDavid Miller * @align: desired alignment 25301da177e4SLinus Torvalds * @gfp_mask: flags for the page level allocator 25311da177e4SLinus Torvalds * @prot: protection mask for the allocated pages 253200ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 2533c85d194bSRandy Dunlap * @caller: caller's return address 25341da177e4SLinus Torvalds * 25351da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 25361da177e4SLinus Torvalds * allocator with @gfp_mask flags. Map them into contiguous 25371da177e4SLinus Torvalds * kernel virtual space, using a pagetable protection of @prot. 2538a7c3e901SMichal Hocko * 2539dcda9b04SMichal Hocko * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL 2540a7c3e901SMichal Hocko * and __GFP_NOFAIL are not supported 2541a7c3e901SMichal Hocko * 2542a7c3e901SMichal Hocko * Any use of gfp flags outside of GFP_KERNEL should be consulted 2543a7c3e901SMichal Hocko * with mm people. 2544a862f68aSMike Rapoport * 2545a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 25461da177e4SLinus Torvalds */ 25478594a21cSMichal Hocko static void *__vmalloc_node(unsigned long size, unsigned long align, 25482dca6999SDavid Miller gfp_t gfp_mask, pgprot_t prot, 25495e6cafc8SMarek Szyprowski int node, const void *caller) 25501da177e4SLinus Torvalds { 2551d0a21265SDavid Rientjes return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, 2552cb9e3c29SAndrey Ryabinin gfp_mask, prot, 0, node, caller); 25531da177e4SLinus Torvalds } 25541da177e4SLinus Torvalds 2555930fc45aSChristoph Lameter void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) 2556930fc45aSChristoph Lameter { 255700ef2d2fSDavid Rientjes return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE, 255823016969SChristoph Lameter __builtin_return_address(0)); 2559930fc45aSChristoph Lameter } 25601da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc); 25611da177e4SLinus Torvalds 25628594a21cSMichal Hocko static inline void *__vmalloc_node_flags(unsigned long size, 25638594a21cSMichal Hocko int node, gfp_t flags) 25648594a21cSMichal Hocko { 25658594a21cSMichal Hocko return __vmalloc_node(size, 1, flags, PAGE_KERNEL, 25668594a21cSMichal Hocko node, __builtin_return_address(0)); 25678594a21cSMichal Hocko } 25688594a21cSMichal Hocko 25698594a21cSMichal Hocko 25708594a21cSMichal Hocko void *__vmalloc_node_flags_caller(unsigned long size, int node, gfp_t flags, 25718594a21cSMichal Hocko void *caller) 25728594a21cSMichal Hocko { 25738594a21cSMichal Hocko return __vmalloc_node(size, 1, flags, PAGE_KERNEL, node, caller); 25748594a21cSMichal Hocko } 25758594a21cSMichal Hocko 25761da177e4SLinus Torvalds /** 25771da177e4SLinus Torvalds * vmalloc - allocate virtually contiguous memory 25781da177e4SLinus Torvalds * @size: allocation size 257992eac168SMike Rapoport * 25801da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 25811da177e4SLinus Torvalds * allocator and map them into contiguous kernel virtual space. 25821da177e4SLinus Torvalds * 2583c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 25841da177e4SLinus Torvalds * use __vmalloc() instead. 2585a862f68aSMike Rapoport * 2586a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 25871da177e4SLinus Torvalds */ 25881da177e4SLinus Torvalds void *vmalloc(unsigned long size) 25891da177e4SLinus Torvalds { 259000ef2d2fSDavid Rientjes return __vmalloc_node_flags(size, NUMA_NO_NODE, 259119809c2dSMichal Hocko GFP_KERNEL); 25921da177e4SLinus Torvalds } 25931da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc); 25941da177e4SLinus Torvalds 2595930fc45aSChristoph Lameter /** 2596e1ca7788SDave Young * vzalloc - allocate virtually contiguous memory with zero fill 2597e1ca7788SDave Young * @size: allocation size 259892eac168SMike Rapoport * 2599e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 2600e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 2601e1ca7788SDave Young * The memory allocated is set to zero. 2602e1ca7788SDave Young * 2603e1ca7788SDave Young * For tight control over page level allocator and protection flags 2604e1ca7788SDave Young * use __vmalloc() instead. 2605a862f68aSMike Rapoport * 2606a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 2607e1ca7788SDave Young */ 2608e1ca7788SDave Young void *vzalloc(unsigned long size) 2609e1ca7788SDave Young { 261000ef2d2fSDavid Rientjes return __vmalloc_node_flags(size, NUMA_NO_NODE, 261119809c2dSMichal Hocko GFP_KERNEL | __GFP_ZERO); 2612e1ca7788SDave Young } 2613e1ca7788SDave Young EXPORT_SYMBOL(vzalloc); 2614e1ca7788SDave Young 2615e1ca7788SDave Young /** 2616ead04089SRolf Eike Beer * vmalloc_user - allocate zeroed virtually contiguous memory for userspace 261783342314SNick Piggin * @size: allocation size 2618ead04089SRolf Eike Beer * 2619ead04089SRolf Eike Beer * The resulting memory area is zeroed so it can be mapped to userspace 2620ead04089SRolf Eike Beer * without leaking data. 2621a862f68aSMike Rapoport * 2622a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 262383342314SNick Piggin */ 262483342314SNick Piggin void *vmalloc_user(unsigned long size) 262583342314SNick Piggin { 2626bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 2627bc84c535SRoman Penyaev GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL, 2628bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 262900ef2d2fSDavid Rientjes __builtin_return_address(0)); 263083342314SNick Piggin } 263183342314SNick Piggin EXPORT_SYMBOL(vmalloc_user); 263283342314SNick Piggin 263383342314SNick Piggin /** 2634930fc45aSChristoph Lameter * vmalloc_node - allocate memory on a specific node 2635930fc45aSChristoph Lameter * @size: allocation size 2636d44e0780SRandy Dunlap * @node: numa node 2637930fc45aSChristoph Lameter * 2638930fc45aSChristoph Lameter * Allocate enough pages to cover @size from the page level 2639930fc45aSChristoph Lameter * allocator and map them into contiguous kernel virtual space. 2640930fc45aSChristoph Lameter * 2641c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 2642930fc45aSChristoph Lameter * use __vmalloc() instead. 2643a862f68aSMike Rapoport * 2644a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 2645930fc45aSChristoph Lameter */ 2646930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node) 2647930fc45aSChristoph Lameter { 264819809c2dSMichal Hocko return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL, 264923016969SChristoph Lameter node, __builtin_return_address(0)); 2650930fc45aSChristoph Lameter } 2651930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node); 2652930fc45aSChristoph Lameter 2653e1ca7788SDave Young /** 2654e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill 2655e1ca7788SDave Young * @size: allocation size 2656e1ca7788SDave Young * @node: numa node 2657e1ca7788SDave Young * 2658e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 2659e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 2660e1ca7788SDave Young * The memory allocated is set to zero. 2661e1ca7788SDave Young * 2662e1ca7788SDave Young * For tight control over page level allocator and protection flags 2663e1ca7788SDave Young * use __vmalloc_node() instead. 2664a862f68aSMike Rapoport * 2665a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 2666e1ca7788SDave Young */ 2667e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node) 2668e1ca7788SDave Young { 2669e1ca7788SDave Young return __vmalloc_node_flags(size, node, 267019809c2dSMichal Hocko GFP_KERNEL | __GFP_ZERO); 2671e1ca7788SDave Young } 2672e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node); 2673e1ca7788SDave Young 26741da177e4SLinus Torvalds /** 26751da177e4SLinus Torvalds * vmalloc_exec - allocate virtually contiguous, executable memory 26761da177e4SLinus Torvalds * @size: allocation size 26771da177e4SLinus Torvalds * 26781da177e4SLinus Torvalds * Kernel-internal function to allocate enough pages to cover @size 26791da177e4SLinus Torvalds * the page level allocator and map them into contiguous and 26801da177e4SLinus Torvalds * executable kernel virtual space. 26811da177e4SLinus Torvalds * 2682c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 26831da177e4SLinus Torvalds * use __vmalloc() instead. 2684a862f68aSMike Rapoport * 2685a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 26861da177e4SLinus Torvalds */ 26871da177e4SLinus Torvalds void *vmalloc_exec(unsigned long size) 26881da177e4SLinus Torvalds { 2689868b104dSRick Edgecombe return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, 2690868b104dSRick Edgecombe GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS, 269100ef2d2fSDavid Rientjes NUMA_NO_NODE, __builtin_return_address(0)); 26921da177e4SLinus Torvalds } 26931da177e4SLinus Torvalds 26940d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) 2695698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) 26960d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) 2697698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL) 26980d08e0d3SAndi Kleen #else 2699698d0831SMichal Hocko /* 2700698d0831SMichal Hocko * 64b systems should always have either DMA or DMA32 zones. For others 2701698d0831SMichal Hocko * GFP_DMA32 should do the right thing and use the normal zone. 2702698d0831SMichal Hocko */ 2703698d0831SMichal Hocko #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL 27040d08e0d3SAndi Kleen #endif 27050d08e0d3SAndi Kleen 27061da177e4SLinus Torvalds /** 27071da177e4SLinus Torvalds * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 27081da177e4SLinus Torvalds * @size: allocation size 27091da177e4SLinus Torvalds * 27101da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 27111da177e4SLinus Torvalds * page level allocator and map them into contiguous kernel virtual space. 2712a862f68aSMike Rapoport * 2713a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 27141da177e4SLinus Torvalds */ 27151da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 27161da177e4SLinus Torvalds { 27172dca6999SDavid Miller return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL, 271800ef2d2fSDavid Rientjes NUMA_NO_NODE, __builtin_return_address(0)); 27191da177e4SLinus Torvalds } 27201da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32); 27211da177e4SLinus Torvalds 272283342314SNick Piggin /** 2723ead04089SRolf Eike Beer * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 272483342314SNick Piggin * @size: allocation size 2725ead04089SRolf Eike Beer * 2726ead04089SRolf Eike Beer * The resulting memory area is 32bit addressable and zeroed so it can be 2727ead04089SRolf Eike Beer * mapped to userspace without leaking data. 2728a862f68aSMike Rapoport * 2729a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 273083342314SNick Piggin */ 273183342314SNick Piggin void *vmalloc_32_user(unsigned long size) 273283342314SNick Piggin { 2733bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 2734bc84c535SRoman Penyaev GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, 2735bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 27365a82ac71SRoman Penyaev __builtin_return_address(0)); 273783342314SNick Piggin } 273883342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user); 273983342314SNick Piggin 2740d0107eb0SKAMEZAWA Hiroyuki /* 2741d0107eb0SKAMEZAWA Hiroyuki * small helper routine , copy contents to buf from addr. 2742d0107eb0SKAMEZAWA Hiroyuki * If the page is not present, fill zero. 2743d0107eb0SKAMEZAWA Hiroyuki */ 2744d0107eb0SKAMEZAWA Hiroyuki 2745d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count) 2746d0107eb0SKAMEZAWA Hiroyuki { 2747d0107eb0SKAMEZAWA Hiroyuki struct page *p; 2748d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 2749d0107eb0SKAMEZAWA Hiroyuki 2750d0107eb0SKAMEZAWA Hiroyuki while (count) { 2751d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 2752d0107eb0SKAMEZAWA Hiroyuki 2753891c49abSAlexander Kuleshov offset = offset_in_page(addr); 2754d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 2755d0107eb0SKAMEZAWA Hiroyuki if (length > count) 2756d0107eb0SKAMEZAWA Hiroyuki length = count; 2757d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 2758d0107eb0SKAMEZAWA Hiroyuki /* 2759d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 2760d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 2761d0107eb0SKAMEZAWA Hiroyuki * overhead of vmalloc()/vfree() calles for this _debug_ 2762d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 2763d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 2764d0107eb0SKAMEZAWA Hiroyuki */ 2765d0107eb0SKAMEZAWA Hiroyuki if (p) { 2766d0107eb0SKAMEZAWA Hiroyuki /* 2767d0107eb0SKAMEZAWA Hiroyuki * we can expect USER0 is not used (see vread/vwrite's 2768d0107eb0SKAMEZAWA Hiroyuki * function description) 2769d0107eb0SKAMEZAWA Hiroyuki */ 27709b04c5feSCong Wang void *map = kmap_atomic(p); 2771d0107eb0SKAMEZAWA Hiroyuki memcpy(buf, map + offset, length); 27729b04c5feSCong Wang kunmap_atomic(map); 2773d0107eb0SKAMEZAWA Hiroyuki } else 2774d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, length); 2775d0107eb0SKAMEZAWA Hiroyuki 2776d0107eb0SKAMEZAWA Hiroyuki addr += length; 2777d0107eb0SKAMEZAWA Hiroyuki buf += length; 2778d0107eb0SKAMEZAWA Hiroyuki copied += length; 2779d0107eb0SKAMEZAWA Hiroyuki count -= length; 2780d0107eb0SKAMEZAWA Hiroyuki } 2781d0107eb0SKAMEZAWA Hiroyuki return copied; 2782d0107eb0SKAMEZAWA Hiroyuki } 2783d0107eb0SKAMEZAWA Hiroyuki 2784d0107eb0SKAMEZAWA Hiroyuki static int aligned_vwrite(char *buf, char *addr, unsigned long count) 2785d0107eb0SKAMEZAWA Hiroyuki { 2786d0107eb0SKAMEZAWA Hiroyuki struct page *p; 2787d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 2788d0107eb0SKAMEZAWA Hiroyuki 2789d0107eb0SKAMEZAWA Hiroyuki while (count) { 2790d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 2791d0107eb0SKAMEZAWA Hiroyuki 2792891c49abSAlexander Kuleshov offset = offset_in_page(addr); 2793d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 2794d0107eb0SKAMEZAWA Hiroyuki if (length > count) 2795d0107eb0SKAMEZAWA Hiroyuki length = count; 2796d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 2797d0107eb0SKAMEZAWA Hiroyuki /* 2798d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 2799d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 2800d0107eb0SKAMEZAWA Hiroyuki * overhead of vmalloc()/vfree() calles for this _debug_ 2801d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 2802d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 2803d0107eb0SKAMEZAWA Hiroyuki */ 2804d0107eb0SKAMEZAWA Hiroyuki if (p) { 2805d0107eb0SKAMEZAWA Hiroyuki /* 2806d0107eb0SKAMEZAWA Hiroyuki * we can expect USER0 is not used (see vread/vwrite's 2807d0107eb0SKAMEZAWA Hiroyuki * function description) 2808d0107eb0SKAMEZAWA Hiroyuki */ 28099b04c5feSCong Wang void *map = kmap_atomic(p); 2810d0107eb0SKAMEZAWA Hiroyuki memcpy(map + offset, buf, length); 28119b04c5feSCong Wang kunmap_atomic(map); 2812d0107eb0SKAMEZAWA Hiroyuki } 2813d0107eb0SKAMEZAWA Hiroyuki addr += length; 2814d0107eb0SKAMEZAWA Hiroyuki buf += length; 2815d0107eb0SKAMEZAWA Hiroyuki copied += length; 2816d0107eb0SKAMEZAWA Hiroyuki count -= length; 2817d0107eb0SKAMEZAWA Hiroyuki } 2818d0107eb0SKAMEZAWA Hiroyuki return copied; 2819d0107eb0SKAMEZAWA Hiroyuki } 2820d0107eb0SKAMEZAWA Hiroyuki 2821d0107eb0SKAMEZAWA Hiroyuki /** 2822d0107eb0SKAMEZAWA Hiroyuki * vread() - read vmalloc area in a safe way. 2823d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for reading data 2824d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 2825d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 2826d0107eb0SKAMEZAWA Hiroyuki * 2827d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 2828d0107eb0SKAMEZAWA Hiroyuki * copy data from that area to a given buffer. If the given memory range 2829d0107eb0SKAMEZAWA Hiroyuki * of [addr...addr+count) includes some valid address, data is copied to 2830d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, they'll be zero-filled. 2831d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 2832d0107eb0SKAMEZAWA Hiroyuki * 2833d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 2834a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 2835d0107eb0SKAMEZAWA Hiroyuki * 2836d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vread() is never necessary because the caller 2837d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 2838d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 2839d9009d67SGeert Uytterhoeven * any information, as /dev/kmem. 2840a862f68aSMike Rapoport * 2841a862f68aSMike Rapoport * Return: number of bytes for which addr and buf should be increased 2842a862f68aSMike Rapoport * (same number as @count) or %0 if [addr...addr+count) doesn't 2843a862f68aSMike Rapoport * include any intersection with valid vmalloc area 2844d0107eb0SKAMEZAWA Hiroyuki */ 28451da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 28461da177e4SLinus Torvalds { 2847e81ce85fSJoonsoo Kim struct vmap_area *va; 2848e81ce85fSJoonsoo Kim struct vm_struct *vm; 28491da177e4SLinus Torvalds char *vaddr, *buf_start = buf; 2850d0107eb0SKAMEZAWA Hiroyuki unsigned long buflen = count; 28511da177e4SLinus Torvalds unsigned long n; 28521da177e4SLinus Torvalds 28531da177e4SLinus Torvalds /* Don't allow overflow */ 28541da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 28551da177e4SLinus Torvalds count = -(unsigned long) addr; 28561da177e4SLinus Torvalds 2857e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 2858e81ce85fSJoonsoo Kim list_for_each_entry(va, &vmap_area_list, list) { 2859e81ce85fSJoonsoo Kim if (!count) 2860e81ce85fSJoonsoo Kim break; 2861e81ce85fSJoonsoo Kim 2862688fcbfcSPengfei Li if (!va->vm) 2863e81ce85fSJoonsoo Kim continue; 2864e81ce85fSJoonsoo Kim 2865e81ce85fSJoonsoo Kim vm = va->vm; 2866e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 2867762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 28681da177e4SLinus Torvalds continue; 28691da177e4SLinus Torvalds while (addr < vaddr) { 28701da177e4SLinus Torvalds if (count == 0) 28711da177e4SLinus Torvalds goto finished; 28721da177e4SLinus Torvalds *buf = '\0'; 28731da177e4SLinus Torvalds buf++; 28741da177e4SLinus Torvalds addr++; 28751da177e4SLinus Torvalds count--; 28761da177e4SLinus Torvalds } 2877762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 2878d0107eb0SKAMEZAWA Hiroyuki if (n > count) 2879d0107eb0SKAMEZAWA Hiroyuki n = count; 2880e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) 2881d0107eb0SKAMEZAWA Hiroyuki aligned_vread(buf, addr, n); 2882d0107eb0SKAMEZAWA Hiroyuki else /* IOREMAP area is treated as memory hole */ 2883d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, n); 2884d0107eb0SKAMEZAWA Hiroyuki buf += n; 2885d0107eb0SKAMEZAWA Hiroyuki addr += n; 2886d0107eb0SKAMEZAWA Hiroyuki count -= n; 28871da177e4SLinus Torvalds } 28881da177e4SLinus Torvalds finished: 2889e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 2890d0107eb0SKAMEZAWA Hiroyuki 2891d0107eb0SKAMEZAWA Hiroyuki if (buf == buf_start) 2892d0107eb0SKAMEZAWA Hiroyuki return 0; 2893d0107eb0SKAMEZAWA Hiroyuki /* zero-fill memory holes */ 2894d0107eb0SKAMEZAWA Hiroyuki if (buf != buf_start + buflen) 2895d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, buflen - (buf - buf_start)); 2896d0107eb0SKAMEZAWA Hiroyuki 2897d0107eb0SKAMEZAWA Hiroyuki return buflen; 28981da177e4SLinus Torvalds } 28991da177e4SLinus Torvalds 2900d0107eb0SKAMEZAWA Hiroyuki /** 2901d0107eb0SKAMEZAWA Hiroyuki * vwrite() - write vmalloc area in a safe way. 2902d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for source data 2903d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 2904d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 2905d0107eb0SKAMEZAWA Hiroyuki * 2906d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 2907d0107eb0SKAMEZAWA Hiroyuki * copy data from a buffer to the given addr. If specified range of 2908d0107eb0SKAMEZAWA Hiroyuki * [addr...addr+count) includes some valid address, data is copied from 2909d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, no copy to hole. 2910d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 2911d0107eb0SKAMEZAWA Hiroyuki * 2912d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 2913a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 2914d0107eb0SKAMEZAWA Hiroyuki * 2915d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vwrite() is never necessary because the caller 2916d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 2917d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 2918d9009d67SGeert Uytterhoeven * any information, as /dev/kmem. 2919a862f68aSMike Rapoport * 2920a862f68aSMike Rapoport * Return: number of bytes for which addr and buf should be 2921a862f68aSMike Rapoport * increased (same number as @count) or %0 if [addr...addr+count) 2922a862f68aSMike Rapoport * doesn't include any intersection with valid vmalloc area 2923d0107eb0SKAMEZAWA Hiroyuki */ 29241da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count) 29251da177e4SLinus Torvalds { 2926e81ce85fSJoonsoo Kim struct vmap_area *va; 2927e81ce85fSJoonsoo Kim struct vm_struct *vm; 2928d0107eb0SKAMEZAWA Hiroyuki char *vaddr; 2929d0107eb0SKAMEZAWA Hiroyuki unsigned long n, buflen; 2930d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 29311da177e4SLinus Torvalds 29321da177e4SLinus Torvalds /* Don't allow overflow */ 29331da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 29341da177e4SLinus Torvalds count = -(unsigned long) addr; 2935d0107eb0SKAMEZAWA Hiroyuki buflen = count; 29361da177e4SLinus Torvalds 2937e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 2938e81ce85fSJoonsoo Kim list_for_each_entry(va, &vmap_area_list, list) { 2939e81ce85fSJoonsoo Kim if (!count) 2940e81ce85fSJoonsoo Kim break; 2941e81ce85fSJoonsoo Kim 2942688fcbfcSPengfei Li if (!va->vm) 2943e81ce85fSJoonsoo Kim continue; 2944e81ce85fSJoonsoo Kim 2945e81ce85fSJoonsoo Kim vm = va->vm; 2946e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 2947762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 29481da177e4SLinus Torvalds continue; 29491da177e4SLinus Torvalds while (addr < vaddr) { 29501da177e4SLinus Torvalds if (count == 0) 29511da177e4SLinus Torvalds goto finished; 29521da177e4SLinus Torvalds buf++; 29531da177e4SLinus Torvalds addr++; 29541da177e4SLinus Torvalds count--; 29551da177e4SLinus Torvalds } 2956762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 2957d0107eb0SKAMEZAWA Hiroyuki if (n > count) 2958d0107eb0SKAMEZAWA Hiroyuki n = count; 2959e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) { 2960d0107eb0SKAMEZAWA Hiroyuki aligned_vwrite(buf, addr, n); 2961d0107eb0SKAMEZAWA Hiroyuki copied++; 2962d0107eb0SKAMEZAWA Hiroyuki } 2963d0107eb0SKAMEZAWA Hiroyuki buf += n; 2964d0107eb0SKAMEZAWA Hiroyuki addr += n; 2965d0107eb0SKAMEZAWA Hiroyuki count -= n; 29661da177e4SLinus Torvalds } 29671da177e4SLinus Torvalds finished: 2968e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 2969d0107eb0SKAMEZAWA Hiroyuki if (!copied) 2970d0107eb0SKAMEZAWA Hiroyuki return 0; 2971d0107eb0SKAMEZAWA Hiroyuki return buflen; 29721da177e4SLinus Torvalds } 297383342314SNick Piggin 297483342314SNick Piggin /** 2975e69e9d4aSHATAYAMA Daisuke * remap_vmalloc_range_partial - map vmalloc pages to userspace 2976e69e9d4aSHATAYAMA Daisuke * @vma: vma to cover 2977e69e9d4aSHATAYAMA Daisuke * @uaddr: target user address to start at 2978e69e9d4aSHATAYAMA Daisuke * @kaddr: virtual address of vmalloc kernel memory 2979e69e9d4aSHATAYAMA Daisuke * @size: size of map area 2980e69e9d4aSHATAYAMA Daisuke * 2981e69e9d4aSHATAYAMA Daisuke * Returns: 0 for success, -Exxx on failure 2982e69e9d4aSHATAYAMA Daisuke * 2983e69e9d4aSHATAYAMA Daisuke * This function checks that @kaddr is a valid vmalloc'ed area, 2984e69e9d4aSHATAYAMA Daisuke * and that it is big enough to cover the range starting at 2985e69e9d4aSHATAYAMA Daisuke * @uaddr in @vma. Will return failure if that criteria isn't 2986e69e9d4aSHATAYAMA Daisuke * met. 2987e69e9d4aSHATAYAMA Daisuke * 2988e69e9d4aSHATAYAMA Daisuke * Similar to remap_pfn_range() (see mm/memory.c) 2989e69e9d4aSHATAYAMA Daisuke */ 2990e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr, 2991e69e9d4aSHATAYAMA Daisuke void *kaddr, unsigned long size) 2992e69e9d4aSHATAYAMA Daisuke { 2993e69e9d4aSHATAYAMA Daisuke struct vm_struct *area; 2994e69e9d4aSHATAYAMA Daisuke 2995e69e9d4aSHATAYAMA Daisuke size = PAGE_ALIGN(size); 2996e69e9d4aSHATAYAMA Daisuke 2997e69e9d4aSHATAYAMA Daisuke if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr)) 2998e69e9d4aSHATAYAMA Daisuke return -EINVAL; 2999e69e9d4aSHATAYAMA Daisuke 3000e69e9d4aSHATAYAMA Daisuke area = find_vm_area(kaddr); 3001e69e9d4aSHATAYAMA Daisuke if (!area) 3002e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3003e69e9d4aSHATAYAMA Daisuke 3004fe9041c2SChristoph Hellwig if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT))) 3005e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3006e69e9d4aSHATAYAMA Daisuke 3007401592d2SRoman Penyaev if (kaddr + size > area->addr + get_vm_area_size(area)) 3008e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3009e69e9d4aSHATAYAMA Daisuke 3010e69e9d4aSHATAYAMA Daisuke do { 3011e69e9d4aSHATAYAMA Daisuke struct page *page = vmalloc_to_page(kaddr); 3012e69e9d4aSHATAYAMA Daisuke int ret; 3013e69e9d4aSHATAYAMA Daisuke 3014e69e9d4aSHATAYAMA Daisuke ret = vm_insert_page(vma, uaddr, page); 3015e69e9d4aSHATAYAMA Daisuke if (ret) 3016e69e9d4aSHATAYAMA Daisuke return ret; 3017e69e9d4aSHATAYAMA Daisuke 3018e69e9d4aSHATAYAMA Daisuke uaddr += PAGE_SIZE; 3019e69e9d4aSHATAYAMA Daisuke kaddr += PAGE_SIZE; 3020e69e9d4aSHATAYAMA Daisuke size -= PAGE_SIZE; 3021e69e9d4aSHATAYAMA Daisuke } while (size > 0); 3022e69e9d4aSHATAYAMA Daisuke 3023e69e9d4aSHATAYAMA Daisuke vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 3024e69e9d4aSHATAYAMA Daisuke 3025e69e9d4aSHATAYAMA Daisuke return 0; 3026e69e9d4aSHATAYAMA Daisuke } 3027e69e9d4aSHATAYAMA Daisuke EXPORT_SYMBOL(remap_vmalloc_range_partial); 3028e69e9d4aSHATAYAMA Daisuke 3029e69e9d4aSHATAYAMA Daisuke /** 303083342314SNick Piggin * remap_vmalloc_range - map vmalloc pages to userspace 303183342314SNick Piggin * @vma: vma to cover (map full range of vma) 303283342314SNick Piggin * @addr: vmalloc memory 303383342314SNick Piggin * @pgoff: number of pages into addr before first page to map 30347682486bSRandy Dunlap * 30357682486bSRandy Dunlap * Returns: 0 for success, -Exxx on failure 303683342314SNick Piggin * 303783342314SNick Piggin * This function checks that addr is a valid vmalloc'ed area, and 303883342314SNick Piggin * that it is big enough to cover the vma. Will return failure if 303983342314SNick Piggin * that criteria isn't met. 304083342314SNick Piggin * 304172fd4a35SRobert P. J. Day * Similar to remap_pfn_range() (see mm/memory.c) 304283342314SNick Piggin */ 304383342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 304483342314SNick Piggin unsigned long pgoff) 304583342314SNick Piggin { 3046e69e9d4aSHATAYAMA Daisuke return remap_vmalloc_range_partial(vma, vma->vm_start, 3047e69e9d4aSHATAYAMA Daisuke addr + (pgoff << PAGE_SHIFT), 3048e69e9d4aSHATAYAMA Daisuke vma->vm_end - vma->vm_start); 304983342314SNick Piggin } 305083342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range); 305183342314SNick Piggin 30521eeb66a1SChristoph Hellwig /* 30531eeb66a1SChristoph Hellwig * Implement a stub for vmalloc_sync_all() if the architecture chose not to 30541eeb66a1SChristoph Hellwig * have one. 30553f8fd02bSJoerg Roedel * 30563f8fd02bSJoerg Roedel * The purpose of this function is to make sure the vmalloc area 30573f8fd02bSJoerg Roedel * mappings are identical in all page-tables in the system. 30581eeb66a1SChristoph Hellwig */ 30593b32123dSGideon Israel Dsouza void __weak vmalloc_sync_all(void) 30601eeb66a1SChristoph Hellwig { 30611eeb66a1SChristoph Hellwig } 30625f4352fbSJeremy Fitzhardinge 30635f4352fbSJeremy Fitzhardinge 30648b1e0f81SAnshuman Khandual static int f(pte_t *pte, unsigned long addr, void *data) 30655f4352fbSJeremy Fitzhardinge { 3066cd12909cSDavid Vrabel pte_t ***p = data; 3067cd12909cSDavid Vrabel 3068cd12909cSDavid Vrabel if (p) { 3069cd12909cSDavid Vrabel *(*p) = pte; 3070cd12909cSDavid Vrabel (*p)++; 3071cd12909cSDavid Vrabel } 30725f4352fbSJeremy Fitzhardinge return 0; 30735f4352fbSJeremy Fitzhardinge } 30745f4352fbSJeremy Fitzhardinge 30755f4352fbSJeremy Fitzhardinge /** 30765f4352fbSJeremy Fitzhardinge * alloc_vm_area - allocate a range of kernel address space 30775f4352fbSJeremy Fitzhardinge * @size: size of the area 3078cd12909cSDavid Vrabel * @ptes: returns the PTEs for the address space 30797682486bSRandy Dunlap * 30807682486bSRandy Dunlap * Returns: NULL on failure, vm_struct on success 30815f4352fbSJeremy Fitzhardinge * 30825f4352fbSJeremy Fitzhardinge * This function reserves a range of kernel address space, and 30835f4352fbSJeremy Fitzhardinge * allocates pagetables to map that range. No actual mappings 3084cd12909cSDavid Vrabel * are created. 3085cd12909cSDavid Vrabel * 3086cd12909cSDavid Vrabel * If @ptes is non-NULL, pointers to the PTEs (in init_mm) 3087cd12909cSDavid Vrabel * allocated for the VM area are returned. 30885f4352fbSJeremy Fitzhardinge */ 3089cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes) 30905f4352fbSJeremy Fitzhardinge { 30915f4352fbSJeremy Fitzhardinge struct vm_struct *area; 30925f4352fbSJeremy Fitzhardinge 309323016969SChristoph Lameter area = get_vm_area_caller(size, VM_IOREMAP, 309423016969SChristoph Lameter __builtin_return_address(0)); 30955f4352fbSJeremy Fitzhardinge if (area == NULL) 30965f4352fbSJeremy Fitzhardinge return NULL; 30975f4352fbSJeremy Fitzhardinge 30985f4352fbSJeremy Fitzhardinge /* 30995f4352fbSJeremy Fitzhardinge * This ensures that page tables are constructed for this region 31005f4352fbSJeremy Fitzhardinge * of kernel virtual address space and mapped into init_mm. 31015f4352fbSJeremy Fitzhardinge */ 31025f4352fbSJeremy Fitzhardinge if (apply_to_page_range(&init_mm, (unsigned long)area->addr, 3103cd12909cSDavid Vrabel size, f, ptes ? &ptes : NULL)) { 31045f4352fbSJeremy Fitzhardinge free_vm_area(area); 31055f4352fbSJeremy Fitzhardinge return NULL; 31065f4352fbSJeremy Fitzhardinge } 31075f4352fbSJeremy Fitzhardinge 31085f4352fbSJeremy Fitzhardinge return area; 31095f4352fbSJeremy Fitzhardinge } 31105f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(alloc_vm_area); 31115f4352fbSJeremy Fitzhardinge 31125f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area) 31135f4352fbSJeremy Fitzhardinge { 31145f4352fbSJeremy Fitzhardinge struct vm_struct *ret; 31155f4352fbSJeremy Fitzhardinge ret = remove_vm_area(area->addr); 31165f4352fbSJeremy Fitzhardinge BUG_ON(ret != area); 31175f4352fbSJeremy Fitzhardinge kfree(area); 31185f4352fbSJeremy Fitzhardinge } 31195f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area); 3120a10aa579SChristoph Lameter 31214f8b02b4STejun Heo #ifdef CONFIG_SMP 3122ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n) 3123ca23e405STejun Heo { 31244583e773SGeliang Tang return rb_entry_safe(n, struct vmap_area, rb_node); 3125ca23e405STejun Heo } 3126ca23e405STejun Heo 3127ca23e405STejun Heo /** 312868ad4a33SUladzislau Rezki (Sony) * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to 312968ad4a33SUladzislau Rezki (Sony) * @addr: target address 3130ca23e405STejun Heo * 313168ad4a33SUladzislau Rezki (Sony) * Returns: vmap_area if it is found. If there is no such area 313268ad4a33SUladzislau Rezki (Sony) * the first highest(reverse order) vmap_area is returned 313368ad4a33SUladzislau Rezki (Sony) * i.e. va->va_start < addr && va->va_end < addr or NULL 313468ad4a33SUladzislau Rezki (Sony) * if there are no any areas before @addr. 3135ca23e405STejun Heo */ 313668ad4a33SUladzislau Rezki (Sony) static struct vmap_area * 313768ad4a33SUladzislau Rezki (Sony) pvm_find_va_enclose_addr(unsigned long addr) 3138ca23e405STejun Heo { 313968ad4a33SUladzislau Rezki (Sony) struct vmap_area *va, *tmp; 314068ad4a33SUladzislau Rezki (Sony) struct rb_node *n; 314168ad4a33SUladzislau Rezki (Sony) 314268ad4a33SUladzislau Rezki (Sony) n = free_vmap_area_root.rb_node; 314368ad4a33SUladzislau Rezki (Sony) va = NULL; 3144ca23e405STejun Heo 3145ca23e405STejun Heo while (n) { 314668ad4a33SUladzislau Rezki (Sony) tmp = rb_entry(n, struct vmap_area, rb_node); 314768ad4a33SUladzislau Rezki (Sony) if (tmp->va_start <= addr) { 314868ad4a33SUladzislau Rezki (Sony) va = tmp; 314968ad4a33SUladzislau Rezki (Sony) if (tmp->va_end >= addr) 3150ca23e405STejun Heo break; 3151ca23e405STejun Heo 315268ad4a33SUladzislau Rezki (Sony) n = n->rb_right; 3153ca23e405STejun Heo } else { 315468ad4a33SUladzislau Rezki (Sony) n = n->rb_left; 3155ca23e405STejun Heo } 315668ad4a33SUladzislau Rezki (Sony) } 315768ad4a33SUladzislau Rezki (Sony) 315868ad4a33SUladzislau Rezki (Sony) return va; 3159ca23e405STejun Heo } 3160ca23e405STejun Heo 3161ca23e405STejun Heo /** 316268ad4a33SUladzislau Rezki (Sony) * pvm_determine_end_from_reverse - find the highest aligned address 316368ad4a33SUladzislau Rezki (Sony) * of free block below VMALLOC_END 316468ad4a33SUladzislau Rezki (Sony) * @va: 316568ad4a33SUladzislau Rezki (Sony) * in - the VA we start the search(reverse order); 316668ad4a33SUladzislau Rezki (Sony) * out - the VA with the highest aligned end address. 3167ca23e405STejun Heo * 316868ad4a33SUladzislau Rezki (Sony) * Returns: determined end address within vmap_area 3169ca23e405STejun Heo */ 317068ad4a33SUladzislau Rezki (Sony) static unsigned long 317168ad4a33SUladzislau Rezki (Sony) pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align) 3172ca23e405STejun Heo { 317368ad4a33SUladzislau Rezki (Sony) unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 3174ca23e405STejun Heo unsigned long addr; 3175ca23e405STejun Heo 317668ad4a33SUladzislau Rezki (Sony) if (likely(*va)) { 317768ad4a33SUladzislau Rezki (Sony) list_for_each_entry_from_reverse((*va), 317868ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list, list) { 317968ad4a33SUladzislau Rezki (Sony) addr = min((*va)->va_end & ~(align - 1), vmalloc_end); 318068ad4a33SUladzislau Rezki (Sony) if ((*va)->va_start < addr) 318168ad4a33SUladzislau Rezki (Sony) return addr; 318268ad4a33SUladzislau Rezki (Sony) } 3183ca23e405STejun Heo } 3184ca23e405STejun Heo 318568ad4a33SUladzislau Rezki (Sony) return 0; 3186ca23e405STejun Heo } 3187ca23e405STejun Heo 3188ca23e405STejun Heo /** 3189ca23e405STejun Heo * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator 3190ca23e405STejun Heo * @offsets: array containing offset of each area 3191ca23e405STejun Heo * @sizes: array containing size of each area 3192ca23e405STejun Heo * @nr_vms: the number of areas to allocate 3193ca23e405STejun Heo * @align: alignment, all entries in @offsets and @sizes must be aligned to this 3194ca23e405STejun Heo * 3195ca23e405STejun Heo * Returns: kmalloc'd vm_struct pointer array pointing to allocated 3196ca23e405STejun Heo * vm_structs on success, %NULL on failure 3197ca23e405STejun Heo * 3198ca23e405STejun Heo * Percpu allocator wants to use congruent vm areas so that it can 3199ca23e405STejun Heo * maintain the offsets among percpu areas. This function allocates 3200ec3f64fcSDavid Rientjes * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to 3201ec3f64fcSDavid Rientjes * be scattered pretty far, distance between two areas easily going up 3202ec3f64fcSDavid Rientjes * to gigabytes. To avoid interacting with regular vmallocs, these 3203ec3f64fcSDavid Rientjes * areas are allocated from top. 3204ca23e405STejun Heo * 3205ca23e405STejun Heo * Despite its complicated look, this allocator is rather simple. It 320668ad4a33SUladzislau Rezki (Sony) * does everything top-down and scans free blocks from the end looking 320768ad4a33SUladzislau Rezki (Sony) * for matching base. While scanning, if any of the areas do not fit the 320868ad4a33SUladzislau Rezki (Sony) * base address is pulled down to fit the area. Scanning is repeated till 320968ad4a33SUladzislau Rezki (Sony) * all the areas fit and then all necessary data structures are inserted 321068ad4a33SUladzislau Rezki (Sony) * and the result is returned. 3211ca23e405STejun Heo */ 3212ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, 3213ca23e405STejun Heo const size_t *sizes, int nr_vms, 3214ec3f64fcSDavid Rientjes size_t align) 3215ca23e405STejun Heo { 3216ca23e405STejun Heo const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); 3217ca23e405STejun Heo const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 321868ad4a33SUladzislau Rezki (Sony) struct vmap_area **vas, *va; 3219ca23e405STejun Heo struct vm_struct **vms; 3220ca23e405STejun Heo int area, area2, last_area, term_area; 322168ad4a33SUladzislau Rezki (Sony) unsigned long base, start, size, end, last_end; 3222ca23e405STejun Heo bool purged = false; 322368ad4a33SUladzislau Rezki (Sony) enum fit_type type; 3224ca23e405STejun Heo 3225ca23e405STejun Heo /* verify parameters and allocate data structures */ 3226891c49abSAlexander Kuleshov BUG_ON(offset_in_page(align) || !is_power_of_2(align)); 3227ca23e405STejun Heo for (last_area = 0, area = 0; area < nr_vms; area++) { 3228ca23e405STejun Heo start = offsets[area]; 3229ca23e405STejun Heo end = start + sizes[area]; 3230ca23e405STejun Heo 3231ca23e405STejun Heo /* is everything aligned properly? */ 3232ca23e405STejun Heo BUG_ON(!IS_ALIGNED(offsets[area], align)); 3233ca23e405STejun Heo BUG_ON(!IS_ALIGNED(sizes[area], align)); 3234ca23e405STejun Heo 3235ca23e405STejun Heo /* detect the area with the highest address */ 3236ca23e405STejun Heo if (start > offsets[last_area]) 3237ca23e405STejun Heo last_area = area; 3238ca23e405STejun Heo 3239c568da28SWei Yang for (area2 = area + 1; area2 < nr_vms; area2++) { 3240ca23e405STejun Heo unsigned long start2 = offsets[area2]; 3241ca23e405STejun Heo unsigned long end2 = start2 + sizes[area2]; 3242ca23e405STejun Heo 3243c568da28SWei Yang BUG_ON(start2 < end && start < end2); 3244ca23e405STejun Heo } 3245ca23e405STejun Heo } 3246ca23e405STejun Heo last_end = offsets[last_area] + sizes[last_area]; 3247ca23e405STejun Heo 3248ca23e405STejun Heo if (vmalloc_end - vmalloc_start < last_end) { 3249ca23e405STejun Heo WARN_ON(true); 3250ca23e405STejun Heo return NULL; 3251ca23e405STejun Heo } 3252ca23e405STejun Heo 32534d67d860SThomas Meyer vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL); 32544d67d860SThomas Meyer vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL); 3255ca23e405STejun Heo if (!vas || !vms) 3256f1db7afdSKautuk Consul goto err_free2; 3257ca23e405STejun Heo 3258ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 325968ad4a33SUladzislau Rezki (Sony) vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL); 3260ec3f64fcSDavid Rientjes vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL); 3261ca23e405STejun Heo if (!vas[area] || !vms[area]) 3262ca23e405STejun Heo goto err_free; 3263ca23e405STejun Heo } 3264ca23e405STejun Heo retry: 3265ca23e405STejun Heo spin_lock(&vmap_area_lock); 3266ca23e405STejun Heo 3267ca23e405STejun Heo /* start scanning - we scan from the top, begin with the last area */ 3268ca23e405STejun Heo area = term_area = last_area; 3269ca23e405STejun Heo start = offsets[area]; 3270ca23e405STejun Heo end = start + sizes[area]; 3271ca23e405STejun Heo 327268ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(vmalloc_end); 327368ad4a33SUladzislau Rezki (Sony) base = pvm_determine_end_from_reverse(&va, align) - end; 3274ca23e405STejun Heo 3275ca23e405STejun Heo while (true) { 3276ca23e405STejun Heo /* 3277ca23e405STejun Heo * base might have underflowed, add last_end before 3278ca23e405STejun Heo * comparing. 3279ca23e405STejun Heo */ 328068ad4a33SUladzislau Rezki (Sony) if (base + last_end < vmalloc_start + last_end) 328168ad4a33SUladzislau Rezki (Sony) goto overflow; 3282ca23e405STejun Heo 3283ca23e405STejun Heo /* 328468ad4a33SUladzislau Rezki (Sony) * Fitting base has not been found. 3285ca23e405STejun Heo */ 328668ad4a33SUladzislau Rezki (Sony) if (va == NULL) 328768ad4a33SUladzislau Rezki (Sony) goto overflow; 3288ca23e405STejun Heo 3289ca23e405STejun Heo /* 32905336e52cSKuppuswamy Sathyanarayanan * If required width exeeds current VA block, move 32915336e52cSKuppuswamy Sathyanarayanan * base downwards and then recheck. 32925336e52cSKuppuswamy Sathyanarayanan */ 32935336e52cSKuppuswamy Sathyanarayanan if (base + end > va->va_end) { 32945336e52cSKuppuswamy Sathyanarayanan base = pvm_determine_end_from_reverse(&va, align) - end; 32955336e52cSKuppuswamy Sathyanarayanan term_area = area; 32965336e52cSKuppuswamy Sathyanarayanan continue; 32975336e52cSKuppuswamy Sathyanarayanan } 32985336e52cSKuppuswamy Sathyanarayanan 32995336e52cSKuppuswamy Sathyanarayanan /* 330068ad4a33SUladzislau Rezki (Sony) * If this VA does not fit, move base downwards and recheck. 3301ca23e405STejun Heo */ 33025336e52cSKuppuswamy Sathyanarayanan if (base + start < va->va_start) { 330368ad4a33SUladzislau Rezki (Sony) va = node_to_va(rb_prev(&va->rb_node)); 330468ad4a33SUladzislau Rezki (Sony) base = pvm_determine_end_from_reverse(&va, align) - end; 3305ca23e405STejun Heo term_area = area; 3306ca23e405STejun Heo continue; 3307ca23e405STejun Heo } 3308ca23e405STejun Heo 3309ca23e405STejun Heo /* 3310ca23e405STejun Heo * This area fits, move on to the previous one. If 3311ca23e405STejun Heo * the previous one is the terminal one, we're done. 3312ca23e405STejun Heo */ 3313ca23e405STejun Heo area = (area + nr_vms - 1) % nr_vms; 3314ca23e405STejun Heo if (area == term_area) 3315ca23e405STejun Heo break; 331668ad4a33SUladzislau Rezki (Sony) 3317ca23e405STejun Heo start = offsets[area]; 3318ca23e405STejun Heo end = start + sizes[area]; 331968ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(base + end); 3320ca23e405STejun Heo } 332168ad4a33SUladzislau Rezki (Sony) 3322ca23e405STejun Heo /* we've found a fitting base, insert all va's */ 3323ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 332468ad4a33SUladzislau Rezki (Sony) int ret; 3325ca23e405STejun Heo 332668ad4a33SUladzislau Rezki (Sony) start = base + offsets[area]; 332768ad4a33SUladzislau Rezki (Sony) size = sizes[area]; 332868ad4a33SUladzislau Rezki (Sony) 332968ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(start); 333068ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(va == NULL)) 333168ad4a33SUladzislau Rezki (Sony) /* It is a BUG(), but trigger recovery instead. */ 333268ad4a33SUladzislau Rezki (Sony) goto recovery; 333368ad4a33SUladzislau Rezki (Sony) 333468ad4a33SUladzislau Rezki (Sony) type = classify_va_fit_type(va, start, size); 333568ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(type == NOTHING_FIT)) 333668ad4a33SUladzislau Rezki (Sony) /* It is a BUG(), but trigger recovery instead. */ 333768ad4a33SUladzislau Rezki (Sony) goto recovery; 333868ad4a33SUladzislau Rezki (Sony) 333968ad4a33SUladzislau Rezki (Sony) ret = adjust_va_to_fit_type(va, start, size, type); 334068ad4a33SUladzislau Rezki (Sony) if (unlikely(ret)) 334168ad4a33SUladzislau Rezki (Sony) goto recovery; 334268ad4a33SUladzislau Rezki (Sony) 334368ad4a33SUladzislau Rezki (Sony) /* Allocated area. */ 334468ad4a33SUladzislau Rezki (Sony) va = vas[area]; 334568ad4a33SUladzislau Rezki (Sony) va->va_start = start; 334668ad4a33SUladzislau Rezki (Sony) va->va_end = start + size; 334768ad4a33SUladzislau Rezki (Sony) 334868ad4a33SUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 3349ca23e405STejun Heo } 3350ca23e405STejun Heo 3351ca23e405STejun Heo spin_unlock(&vmap_area_lock); 3352ca23e405STejun Heo 3353ca23e405STejun Heo /* insert all vm's */ 3354ca23e405STejun Heo for (area = 0; area < nr_vms; area++) 33553645cb4aSZhang Yanfei setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC, 3356ca23e405STejun Heo pcpu_get_vm_areas); 3357ca23e405STejun Heo 3358ca23e405STejun Heo kfree(vas); 3359ca23e405STejun Heo return vms; 3360ca23e405STejun Heo 336168ad4a33SUladzislau Rezki (Sony) recovery: 336268ad4a33SUladzislau Rezki (Sony) /* Remove previously inserted areas. */ 336368ad4a33SUladzislau Rezki (Sony) while (area--) { 336468ad4a33SUladzislau Rezki (Sony) __free_vmap_area(vas[area]); 336568ad4a33SUladzislau Rezki (Sony) vas[area] = NULL; 336668ad4a33SUladzislau Rezki (Sony) } 336768ad4a33SUladzislau Rezki (Sony) 336868ad4a33SUladzislau Rezki (Sony) overflow: 336968ad4a33SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 337068ad4a33SUladzislau Rezki (Sony) if (!purged) { 337168ad4a33SUladzislau Rezki (Sony) purge_vmap_area_lazy(); 337268ad4a33SUladzislau Rezki (Sony) purged = true; 337368ad4a33SUladzislau Rezki (Sony) 337468ad4a33SUladzislau Rezki (Sony) /* Before "retry", check if we recover. */ 337568ad4a33SUladzislau Rezki (Sony) for (area = 0; area < nr_vms; area++) { 337668ad4a33SUladzislau Rezki (Sony) if (vas[area]) 337768ad4a33SUladzislau Rezki (Sony) continue; 337868ad4a33SUladzislau Rezki (Sony) 337968ad4a33SUladzislau Rezki (Sony) vas[area] = kmem_cache_zalloc( 338068ad4a33SUladzislau Rezki (Sony) vmap_area_cachep, GFP_KERNEL); 338168ad4a33SUladzislau Rezki (Sony) if (!vas[area]) 338268ad4a33SUladzislau Rezki (Sony) goto err_free; 338368ad4a33SUladzislau Rezki (Sony) } 338468ad4a33SUladzislau Rezki (Sony) 338568ad4a33SUladzislau Rezki (Sony) goto retry; 338668ad4a33SUladzislau Rezki (Sony) } 338768ad4a33SUladzislau Rezki (Sony) 3388ca23e405STejun Heo err_free: 3389ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 339068ad4a33SUladzislau Rezki (Sony) if (vas[area]) 339168ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, vas[area]); 339268ad4a33SUladzislau Rezki (Sony) 3393ca23e405STejun Heo kfree(vms[area]); 3394ca23e405STejun Heo } 3395f1db7afdSKautuk Consul err_free2: 3396ca23e405STejun Heo kfree(vas); 3397ca23e405STejun Heo kfree(vms); 3398ca23e405STejun Heo return NULL; 3399ca23e405STejun Heo } 3400ca23e405STejun Heo 3401ca23e405STejun Heo /** 3402ca23e405STejun Heo * pcpu_free_vm_areas - free vmalloc areas for percpu allocator 3403ca23e405STejun Heo * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() 3404ca23e405STejun Heo * @nr_vms: the number of allocated areas 3405ca23e405STejun Heo * 3406ca23e405STejun Heo * Free vm_structs and the array allocated by pcpu_get_vm_areas(). 3407ca23e405STejun Heo */ 3408ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) 3409ca23e405STejun Heo { 3410ca23e405STejun Heo int i; 3411ca23e405STejun Heo 3412ca23e405STejun Heo for (i = 0; i < nr_vms; i++) 3413ca23e405STejun Heo free_vm_area(vms[i]); 3414ca23e405STejun Heo kfree(vms); 3415ca23e405STejun Heo } 34164f8b02b4STejun Heo #endif /* CONFIG_SMP */ 3417a10aa579SChristoph Lameter 3418a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS 3419a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos) 3420d4033afdSJoonsoo Kim __acquires(&vmap_area_lock) 3421a10aa579SChristoph Lameter { 3422d4033afdSJoonsoo Kim spin_lock(&vmap_area_lock); 34233f500069Szijun_hu return seq_list_start(&vmap_area_list, *pos); 3424a10aa579SChristoph Lameter } 3425a10aa579SChristoph Lameter 3426a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos) 3427a10aa579SChristoph Lameter { 34283f500069Szijun_hu return seq_list_next(p, &vmap_area_list, pos); 3429a10aa579SChristoph Lameter } 3430a10aa579SChristoph Lameter 3431a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p) 3432d4033afdSJoonsoo Kim __releases(&vmap_area_lock) 3433a10aa579SChristoph Lameter { 3434d4033afdSJoonsoo Kim spin_unlock(&vmap_area_lock); 3435a10aa579SChristoph Lameter } 3436a10aa579SChristoph Lameter 3437a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v) 3438a47a126aSEric Dumazet { 3439e5adfffcSKirill A. Shutemov if (IS_ENABLED(CONFIG_NUMA)) { 3440a47a126aSEric Dumazet unsigned int nr, *counters = m->private; 3441a47a126aSEric Dumazet 3442a47a126aSEric Dumazet if (!counters) 3443a47a126aSEric Dumazet return; 3444a47a126aSEric Dumazet 3445af12346cSWanpeng Li if (v->flags & VM_UNINITIALIZED) 3446af12346cSWanpeng Li return; 34477e5b528bSDmitry Vyukov /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ 34487e5b528bSDmitry Vyukov smp_rmb(); 3449af12346cSWanpeng Li 3450a47a126aSEric Dumazet memset(counters, 0, nr_node_ids * sizeof(unsigned int)); 3451a47a126aSEric Dumazet 3452a47a126aSEric Dumazet for (nr = 0; nr < v->nr_pages; nr++) 3453a47a126aSEric Dumazet counters[page_to_nid(v->pages[nr])]++; 3454a47a126aSEric Dumazet 3455a47a126aSEric Dumazet for_each_node_state(nr, N_HIGH_MEMORY) 3456a47a126aSEric Dumazet if (counters[nr]) 3457a47a126aSEric Dumazet seq_printf(m, " N%u=%u", nr, counters[nr]); 3458a47a126aSEric Dumazet } 3459a47a126aSEric Dumazet } 3460a47a126aSEric Dumazet 3461dd3b8353SUladzislau Rezki (Sony) static void show_purge_info(struct seq_file *m) 3462dd3b8353SUladzislau Rezki (Sony) { 3463dd3b8353SUladzislau Rezki (Sony) struct llist_node *head; 3464dd3b8353SUladzislau Rezki (Sony) struct vmap_area *va; 3465dd3b8353SUladzislau Rezki (Sony) 3466dd3b8353SUladzislau Rezki (Sony) head = READ_ONCE(vmap_purge_list.first); 3467dd3b8353SUladzislau Rezki (Sony) if (head == NULL) 3468dd3b8353SUladzislau Rezki (Sony) return; 3469dd3b8353SUladzislau Rezki (Sony) 3470dd3b8353SUladzislau Rezki (Sony) llist_for_each_entry(va, head, purge_list) { 3471dd3b8353SUladzislau Rezki (Sony) seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n", 3472dd3b8353SUladzislau Rezki (Sony) (void *)va->va_start, (void *)va->va_end, 3473dd3b8353SUladzislau Rezki (Sony) va->va_end - va->va_start); 3474dd3b8353SUladzislau Rezki (Sony) } 3475dd3b8353SUladzislau Rezki (Sony) } 3476dd3b8353SUladzislau Rezki (Sony) 3477a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p) 3478a10aa579SChristoph Lameter { 34793f500069Szijun_hu struct vmap_area *va; 3480d4033afdSJoonsoo Kim struct vm_struct *v; 3481d4033afdSJoonsoo Kim 34823f500069Szijun_hu va = list_entry(p, struct vmap_area, list); 34833f500069Szijun_hu 3484c2ce8c14SWanpeng Li /* 3485688fcbfcSPengfei Li * s_show can encounter race with remove_vm_area, !vm on behalf 3486688fcbfcSPengfei Li * of vmap area is being tear down or vm_map_ram allocation. 3487c2ce8c14SWanpeng Li */ 3488688fcbfcSPengfei Li if (!va->vm) { 3489dd3b8353SUladzislau Rezki (Sony) seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n", 349078c72746SYisheng Xie (void *)va->va_start, (void *)va->va_end, 3491dd3b8353SUladzislau Rezki (Sony) va->va_end - va->va_start); 349278c72746SYisheng Xie 3493d4033afdSJoonsoo Kim return 0; 349478c72746SYisheng Xie } 3495d4033afdSJoonsoo Kim 3496d4033afdSJoonsoo Kim v = va->vm; 3497a10aa579SChristoph Lameter 349845ec1690SKees Cook seq_printf(m, "0x%pK-0x%pK %7ld", 3499a10aa579SChristoph Lameter v->addr, v->addr + v->size, v->size); 3500a10aa579SChristoph Lameter 350162c70bceSJoe Perches if (v->caller) 350262c70bceSJoe Perches seq_printf(m, " %pS", v->caller); 350323016969SChristoph Lameter 3504a10aa579SChristoph Lameter if (v->nr_pages) 3505a10aa579SChristoph Lameter seq_printf(m, " pages=%d", v->nr_pages); 3506a10aa579SChristoph Lameter 3507a10aa579SChristoph Lameter if (v->phys_addr) 3508199eaa05SMiles Chen seq_printf(m, " phys=%pa", &v->phys_addr); 3509a10aa579SChristoph Lameter 3510a10aa579SChristoph Lameter if (v->flags & VM_IOREMAP) 3511f4527c90SFabian Frederick seq_puts(m, " ioremap"); 3512a10aa579SChristoph Lameter 3513a10aa579SChristoph Lameter if (v->flags & VM_ALLOC) 3514f4527c90SFabian Frederick seq_puts(m, " vmalloc"); 3515a10aa579SChristoph Lameter 3516a10aa579SChristoph Lameter if (v->flags & VM_MAP) 3517f4527c90SFabian Frederick seq_puts(m, " vmap"); 3518a10aa579SChristoph Lameter 3519a10aa579SChristoph Lameter if (v->flags & VM_USERMAP) 3520f4527c90SFabian Frederick seq_puts(m, " user"); 3521a10aa579SChristoph Lameter 3522fe9041c2SChristoph Hellwig if (v->flags & VM_DMA_COHERENT) 3523fe9041c2SChristoph Hellwig seq_puts(m, " dma-coherent"); 3524fe9041c2SChristoph Hellwig 3525244d63eeSDavid Rientjes if (is_vmalloc_addr(v->pages)) 3526f4527c90SFabian Frederick seq_puts(m, " vpages"); 3527a10aa579SChristoph Lameter 3528a47a126aSEric Dumazet show_numa_info(m, v); 3529a10aa579SChristoph Lameter seq_putc(m, '\n'); 3530dd3b8353SUladzislau Rezki (Sony) 3531dd3b8353SUladzislau Rezki (Sony) /* 3532dd3b8353SUladzislau Rezki (Sony) * As a final step, dump "unpurged" areas. Note, 3533dd3b8353SUladzislau Rezki (Sony) * that entire "/proc/vmallocinfo" output will not 3534dd3b8353SUladzislau Rezki (Sony) * be address sorted, because the purge list is not 3535dd3b8353SUladzislau Rezki (Sony) * sorted. 3536dd3b8353SUladzislau Rezki (Sony) */ 3537dd3b8353SUladzislau Rezki (Sony) if (list_is_last(&va->list, &vmap_area_list)) 3538dd3b8353SUladzislau Rezki (Sony) show_purge_info(m); 3539dd3b8353SUladzislau Rezki (Sony) 3540a10aa579SChristoph Lameter return 0; 3541a10aa579SChristoph Lameter } 3542a10aa579SChristoph Lameter 35435f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = { 3544a10aa579SChristoph Lameter .start = s_start, 3545a10aa579SChristoph Lameter .next = s_next, 3546a10aa579SChristoph Lameter .stop = s_stop, 3547a10aa579SChristoph Lameter .show = s_show, 3548a10aa579SChristoph Lameter }; 35495f6a6a9cSAlexey Dobriyan 35505f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void) 35515f6a6a9cSAlexey Dobriyan { 3552fddda2b7SChristoph Hellwig if (IS_ENABLED(CONFIG_NUMA)) 35530825a6f9SJoe Perches proc_create_seq_private("vmallocinfo", 0400, NULL, 355444414d82SChristoph Hellwig &vmalloc_op, 355544414d82SChristoph Hellwig nr_node_ids * sizeof(unsigned int), NULL); 3556fddda2b7SChristoph Hellwig else 35570825a6f9SJoe Perches proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op); 35585f6a6a9cSAlexey Dobriyan return 0; 35595f6a6a9cSAlexey Dobriyan } 35605f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init); 3561db3808c1SJoonsoo Kim 3562a10aa579SChristoph Lameter #endif 3563