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> 37bdebd6a2SJann Horn #include <linux/overflow.h> 383b32123dSGideon Israel Dsouza 397c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 401da177e4SLinus Torvalds #include <asm/tlbflush.h> 412dca6999SDavid Miller #include <asm/shmparam.h> 421da177e4SLinus Torvalds 43dd56b046SMel Gorman #include "internal.h" 44dd56b046SMel Gorman 45186525bdSIngo Molnar bool is_vmalloc_addr(const void *x) 46186525bdSIngo Molnar { 47186525bdSIngo Molnar unsigned long addr = (unsigned long)x; 48186525bdSIngo Molnar 49186525bdSIngo Molnar return addr >= VMALLOC_START && addr < VMALLOC_END; 50186525bdSIngo Molnar } 51186525bdSIngo Molnar EXPORT_SYMBOL(is_vmalloc_addr); 52186525bdSIngo Molnar 5332fcfd40SAl Viro struct vfree_deferred { 5432fcfd40SAl Viro struct llist_head list; 5532fcfd40SAl Viro struct work_struct wq; 5632fcfd40SAl Viro }; 5732fcfd40SAl Viro static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred); 5832fcfd40SAl Viro 5932fcfd40SAl Viro static void __vunmap(const void *, int); 6032fcfd40SAl Viro 6132fcfd40SAl Viro static void free_work(struct work_struct *w) 6232fcfd40SAl Viro { 6332fcfd40SAl Viro struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq); 64894e58c1SByungchul Park struct llist_node *t, *llnode; 65894e58c1SByungchul Park 66894e58c1SByungchul Park llist_for_each_safe(llnode, t, llist_del_all(&p->list)) 67894e58c1SByungchul Park __vunmap((void *)llnode, 1); 6832fcfd40SAl Viro } 6932fcfd40SAl Viro 70db64fe02SNick Piggin /*** Page table manipulation functions ***/ 71b221385bSAdrian Bunk 721da177e4SLinus Torvalds static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end) 731da177e4SLinus Torvalds { 741da177e4SLinus Torvalds pte_t *pte; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds pte = pte_offset_kernel(pmd, addr); 771da177e4SLinus Torvalds do { 781da177e4SLinus Torvalds pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte); 791da177e4SLinus Torvalds WARN_ON(!pte_none(ptent) && !pte_present(ptent)); 801da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds 83db64fe02SNick Piggin static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end) 841da177e4SLinus Torvalds { 851da177e4SLinus Torvalds pmd_t *pmd; 861da177e4SLinus Torvalds unsigned long next; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 891da177e4SLinus Torvalds do { 901da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 91b9820d8fSToshi Kani if (pmd_clear_huge(pmd)) 92b9820d8fSToshi Kani continue; 931da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 941da177e4SLinus Torvalds continue; 951da177e4SLinus Torvalds vunmap_pte_range(pmd, addr, next); 961da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds 99c2febafcSKirill A. Shutemov static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end) 1001da177e4SLinus Torvalds { 1011da177e4SLinus Torvalds pud_t *pud; 1021da177e4SLinus Torvalds unsigned long next; 1031da177e4SLinus Torvalds 104c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 1051da177e4SLinus Torvalds do { 1061da177e4SLinus Torvalds next = pud_addr_end(addr, end); 107b9820d8fSToshi Kani if (pud_clear_huge(pud)) 108b9820d8fSToshi Kani continue; 1091da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 1101da177e4SLinus Torvalds continue; 1111da177e4SLinus Torvalds vunmap_pmd_range(pud, addr, next); 1121da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 115c2febafcSKirill A. Shutemov static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end) 116c2febafcSKirill A. Shutemov { 117c2febafcSKirill A. Shutemov p4d_t *p4d; 118c2febafcSKirill A. Shutemov unsigned long next; 119c2febafcSKirill A. Shutemov 120c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 121c2febafcSKirill A. Shutemov do { 122c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 123c2febafcSKirill A. Shutemov if (p4d_clear_huge(p4d)) 124c2febafcSKirill A. Shutemov continue; 125c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 126c2febafcSKirill A. Shutemov continue; 127c2febafcSKirill A. Shutemov vunmap_pud_range(p4d, addr, next); 128c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 129c2febafcSKirill A. Shutemov } 130c2febafcSKirill A. Shutemov 131b521c43fSChristoph Hellwig /** 132b521c43fSChristoph Hellwig * unmap_kernel_range_noflush - unmap kernel VM area 133b521c43fSChristoph Hellwig * @addr: start of the VM area to unmap 134b521c43fSChristoph Hellwig * @size: size of the VM area to unmap 135b521c43fSChristoph Hellwig * 136b521c43fSChristoph Hellwig * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size specify 137b521c43fSChristoph Hellwig * should have been allocated using get_vm_area() and its friends. 138b521c43fSChristoph Hellwig * 139b521c43fSChristoph Hellwig * NOTE: 140b521c43fSChristoph Hellwig * This function does NOT do any cache flushing. The caller is responsible 141b521c43fSChristoph Hellwig * for calling flush_cache_vunmap() on to-be-mapped areas before calling this 142b521c43fSChristoph Hellwig * function and flush_tlb_kernel_range() after. 143b521c43fSChristoph Hellwig */ 144b521c43fSChristoph Hellwig void unmap_kernel_range_noflush(unsigned long addr, unsigned long size) 1451da177e4SLinus Torvalds { 146b521c43fSChristoph Hellwig unsigned long end = addr + size; 1471da177e4SLinus Torvalds unsigned long next; 148b521c43fSChristoph Hellwig pgd_t *pgd; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds BUG_ON(addr >= end); 1511da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 1521da177e4SLinus Torvalds do { 1531da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 1541da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 1551da177e4SLinus Torvalds continue; 156c2febafcSKirill A. Shutemov vunmap_p4d_range(pgd, addr, next); 1571da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds static int vmap_pte_range(pmd_t *pmd, unsigned long addr, 161db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds pte_t *pte; 1641da177e4SLinus Torvalds 165db64fe02SNick Piggin /* 166db64fe02SNick Piggin * nr is a running index into the array which helps higher level 167db64fe02SNick Piggin * callers keep track of where we're up to. 168db64fe02SNick Piggin */ 169db64fe02SNick Piggin 170872fec16SHugh Dickins pte = pte_alloc_kernel(pmd, addr); 1711da177e4SLinus Torvalds if (!pte) 1721da177e4SLinus Torvalds return -ENOMEM; 1731da177e4SLinus Torvalds do { 174db64fe02SNick Piggin struct page *page = pages[*nr]; 175db64fe02SNick Piggin 176db64fe02SNick Piggin if (WARN_ON(!pte_none(*pte))) 177db64fe02SNick Piggin return -EBUSY; 178db64fe02SNick Piggin if (WARN_ON(!page)) 1791da177e4SLinus Torvalds return -ENOMEM; 1801da177e4SLinus Torvalds set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); 181db64fe02SNick Piggin (*nr)++; 1821da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 1831da177e4SLinus Torvalds return 0; 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 186db64fe02SNick Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr, 187db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 1881da177e4SLinus Torvalds { 1891da177e4SLinus Torvalds pmd_t *pmd; 1901da177e4SLinus Torvalds unsigned long next; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds pmd = pmd_alloc(&init_mm, pud, addr); 1931da177e4SLinus Torvalds if (!pmd) 1941da177e4SLinus Torvalds return -ENOMEM; 1951da177e4SLinus Torvalds do { 1961da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 197db64fe02SNick Piggin if (vmap_pte_range(pmd, addr, next, prot, pages, nr)) 1981da177e4SLinus Torvalds return -ENOMEM; 1991da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 2001da177e4SLinus Torvalds return 0; 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 203c2febafcSKirill A. Shutemov static int vmap_pud_range(p4d_t *p4d, unsigned long addr, 204db64fe02SNick Piggin unsigned long end, pgprot_t prot, struct page **pages, int *nr) 2051da177e4SLinus Torvalds { 2061da177e4SLinus Torvalds pud_t *pud; 2071da177e4SLinus Torvalds unsigned long next; 2081da177e4SLinus Torvalds 209c2febafcSKirill A. Shutemov pud = pud_alloc(&init_mm, p4d, addr); 2101da177e4SLinus Torvalds if (!pud) 2111da177e4SLinus Torvalds return -ENOMEM; 2121da177e4SLinus Torvalds do { 2131da177e4SLinus Torvalds next = pud_addr_end(addr, end); 214db64fe02SNick Piggin if (vmap_pmd_range(pud, addr, next, prot, pages, nr)) 2151da177e4SLinus Torvalds return -ENOMEM; 2161da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 2171da177e4SLinus Torvalds return 0; 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 220c2febafcSKirill A. Shutemov static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, 221c2febafcSKirill A. Shutemov unsigned long end, pgprot_t prot, struct page **pages, int *nr) 222c2febafcSKirill A. Shutemov { 223c2febafcSKirill A. Shutemov p4d_t *p4d; 224c2febafcSKirill A. Shutemov unsigned long next; 225c2febafcSKirill A. Shutemov 226c2febafcSKirill A. Shutemov p4d = p4d_alloc(&init_mm, pgd, addr); 227c2febafcSKirill A. Shutemov if (!p4d) 228c2febafcSKirill A. Shutemov return -ENOMEM; 229c2febafcSKirill A. Shutemov do { 230c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 231c2febafcSKirill A. Shutemov if (vmap_pud_range(p4d, addr, next, prot, pages, nr)) 232c2febafcSKirill A. Shutemov return -ENOMEM; 233c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 234c2febafcSKirill A. Shutemov return 0; 235c2febafcSKirill A. Shutemov } 236c2febafcSKirill A. Shutemov 237b521c43fSChristoph Hellwig /** 238b521c43fSChristoph Hellwig * map_kernel_range_noflush - map kernel VM area with the specified pages 239b521c43fSChristoph Hellwig * @addr: start of the VM area to map 240b521c43fSChristoph Hellwig * @size: size of the VM area to map 241b521c43fSChristoph Hellwig * @prot: page protection flags to use 242b521c43fSChristoph Hellwig * @pages: pages to map 243db64fe02SNick Piggin * 244b521c43fSChristoph Hellwig * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size specify should 245b521c43fSChristoph Hellwig * have been allocated using get_vm_area() and its friends. 246b521c43fSChristoph Hellwig * 247b521c43fSChristoph Hellwig * NOTE: 248b521c43fSChristoph Hellwig * This function does NOT do any cache flushing. The caller is responsible for 249b521c43fSChristoph Hellwig * calling flush_cache_vmap() on to-be-mapped areas before calling this 250b521c43fSChristoph Hellwig * function. 251b521c43fSChristoph Hellwig * 252b521c43fSChristoph Hellwig * RETURNS: 25360bb4465SChristoph Hellwig * 0 on success, -errno on failure. 254db64fe02SNick Piggin */ 255b521c43fSChristoph Hellwig int map_kernel_range_noflush(unsigned long addr, unsigned long size, 256db64fe02SNick Piggin pgprot_t prot, struct page **pages) 2571da177e4SLinus Torvalds { 258b521c43fSChristoph Hellwig unsigned long end = addr + size; 2591da177e4SLinus Torvalds unsigned long next; 260b521c43fSChristoph Hellwig pgd_t *pgd; 261db64fe02SNick Piggin int err = 0; 262db64fe02SNick Piggin int nr = 0; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds BUG_ON(addr >= end); 2651da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 2661da177e4SLinus Torvalds do { 2671da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 268c2febafcSKirill A. Shutemov err = vmap_p4d_range(pgd, addr, next, prot, pages, &nr); 2691da177e4SLinus Torvalds if (err) 270bf88c8c8SFigo.zhang return err; 2711da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 272db64fe02SNick Piggin 27360bb4465SChristoph Hellwig return 0; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 276ed1f324cSChristoph Hellwig int map_kernel_range(unsigned long start, unsigned long size, pgprot_t prot, 277ed1f324cSChristoph Hellwig struct page **pages) 2788fc48985STejun Heo { 2798fc48985STejun Heo int ret; 2808fc48985STejun Heo 281a29adb62SChristoph Hellwig ret = map_kernel_range_noflush(start, size, prot, pages); 282a29adb62SChristoph Hellwig flush_cache_vmap(start, start + size); 2838fc48985STejun Heo return ret; 2848fc48985STejun Heo } 2858fc48985STejun Heo 28681ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x) 28773bdf0a6SLinus Torvalds { 28873bdf0a6SLinus Torvalds /* 289ab4f2ee1SRussell King * ARM, x86-64 and sparc64 put modules in a special place, 29073bdf0a6SLinus Torvalds * and fall back on vmalloc() if that fails. Others 29173bdf0a6SLinus Torvalds * just put it in the vmalloc space. 29273bdf0a6SLinus Torvalds */ 29373bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) 29473bdf0a6SLinus Torvalds unsigned long addr = (unsigned long)x; 29573bdf0a6SLinus Torvalds if (addr >= MODULES_VADDR && addr < MODULES_END) 29673bdf0a6SLinus Torvalds return 1; 29773bdf0a6SLinus Torvalds #endif 29873bdf0a6SLinus Torvalds return is_vmalloc_addr(x); 29973bdf0a6SLinus Torvalds } 30073bdf0a6SLinus Torvalds 30148667e7aSChristoph Lameter /* 302add688fbSmalc * Walk a vmap address to the struct page it maps. 30348667e7aSChristoph Lameter */ 304add688fbSmalc struct page *vmalloc_to_page(const void *vmalloc_addr) 30548667e7aSChristoph Lameter { 30648667e7aSChristoph Lameter unsigned long addr = (unsigned long) vmalloc_addr; 307add688fbSmalc struct page *page = NULL; 30848667e7aSChristoph Lameter pgd_t *pgd = pgd_offset_k(addr); 309c2febafcSKirill A. Shutemov p4d_t *p4d; 310c2febafcSKirill A. Shutemov pud_t *pud; 311c2febafcSKirill A. Shutemov pmd_t *pmd; 312c2febafcSKirill A. Shutemov pte_t *ptep, pte; 31348667e7aSChristoph Lameter 3147aa413deSIngo Molnar /* 3157aa413deSIngo Molnar * XXX we might need to change this if we add VIRTUAL_BUG_ON for 3167aa413deSIngo Molnar * architectures that do not vmalloc module space 3177aa413deSIngo Molnar */ 31873bdf0a6SLinus Torvalds VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); 31959ea7463SJiri Slaby 320c2febafcSKirill A. Shutemov if (pgd_none(*pgd)) 321c2febafcSKirill A. Shutemov return NULL; 322c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 323c2febafcSKirill A. Shutemov if (p4d_none(*p4d)) 324c2febafcSKirill A. Shutemov return NULL; 325c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 326029c54b0SArd Biesheuvel 327029c54b0SArd Biesheuvel /* 328029c54b0SArd Biesheuvel * Don't dereference bad PUD or PMD (below) entries. This will also 329029c54b0SArd Biesheuvel * identify huge mappings, which we may encounter on architectures 330029c54b0SArd Biesheuvel * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be 331029c54b0SArd Biesheuvel * identified as vmalloc addresses by is_vmalloc_addr(), but are 332029c54b0SArd Biesheuvel * not [unambiguously] associated with a struct page, so there is 333029c54b0SArd Biesheuvel * no correct value to return for them. 334029c54b0SArd Biesheuvel */ 335029c54b0SArd Biesheuvel WARN_ON_ONCE(pud_bad(*pud)); 336029c54b0SArd Biesheuvel if (pud_none(*pud) || pud_bad(*pud)) 337c2febafcSKirill A. Shutemov return NULL; 338c2febafcSKirill A. Shutemov pmd = pmd_offset(pud, addr); 339029c54b0SArd Biesheuvel WARN_ON_ONCE(pmd_bad(*pmd)); 340029c54b0SArd Biesheuvel if (pmd_none(*pmd) || pmd_bad(*pmd)) 341c2febafcSKirill A. Shutemov return NULL; 342db64fe02SNick Piggin 34348667e7aSChristoph Lameter ptep = pte_offset_map(pmd, addr); 34448667e7aSChristoph Lameter pte = *ptep; 34548667e7aSChristoph Lameter if (pte_present(pte)) 346add688fbSmalc page = pte_page(pte); 34748667e7aSChristoph Lameter pte_unmap(ptep); 348add688fbSmalc return page; 349ece86e22SJianyu Zhan } 350ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page); 351ece86e22SJianyu Zhan 352add688fbSmalc /* 353add688fbSmalc * Map a vmalloc()-space virtual address to the physical page frame number. 354add688fbSmalc */ 355add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr) 356add688fbSmalc { 357add688fbSmalc return page_to_pfn(vmalloc_to_page(vmalloc_addr)); 358add688fbSmalc } 359add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn); 360add688fbSmalc 361db64fe02SNick Piggin 362db64fe02SNick Piggin /*** Global kva allocator ***/ 363db64fe02SNick Piggin 364bb850f4dSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_PROPAGATE_CHECK 0 365a6cf4e0fSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0 366bb850f4dSUladzislau Rezki (Sony) 367db64fe02SNick Piggin 368db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock); 369e36176beSUladzislau Rezki (Sony) static DEFINE_SPINLOCK(free_vmap_area_lock); 370f1c4069eSJoonsoo Kim /* Export for kexec only */ 371f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list); 37280c4bd7aSChris Wilson static LLIST_HEAD(vmap_purge_list); 37389699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT; 37468ad4a33SUladzislau Rezki (Sony) static bool vmap_initialized __read_mostly; 37589699605SNick Piggin 37668ad4a33SUladzislau Rezki (Sony) /* 37768ad4a33SUladzislau Rezki (Sony) * This kmem_cache is used for vmap_area objects. Instead of 37868ad4a33SUladzislau Rezki (Sony) * allocating from slab we reuse an object from this cache to 37968ad4a33SUladzislau Rezki (Sony) * make things faster. Especially in "no edge" splitting of 38068ad4a33SUladzislau Rezki (Sony) * free block. 38168ad4a33SUladzislau Rezki (Sony) */ 38268ad4a33SUladzislau Rezki (Sony) static struct kmem_cache *vmap_area_cachep; 38389699605SNick Piggin 38468ad4a33SUladzislau Rezki (Sony) /* 38568ad4a33SUladzislau Rezki (Sony) * This linked list is used in pair with free_vmap_area_root. 38668ad4a33SUladzislau Rezki (Sony) * It gives O(1) access to prev/next to perform fast coalescing. 38768ad4a33SUladzislau Rezki (Sony) */ 38868ad4a33SUladzislau Rezki (Sony) static LIST_HEAD(free_vmap_area_list); 38968ad4a33SUladzislau Rezki (Sony) 39068ad4a33SUladzislau Rezki (Sony) /* 39168ad4a33SUladzislau Rezki (Sony) * This augment red-black tree represents the free vmap space. 39268ad4a33SUladzislau Rezki (Sony) * All vmap_area objects in this tree are sorted by va->va_start 39368ad4a33SUladzislau Rezki (Sony) * address. It is used for allocation and merging when a vmap 39468ad4a33SUladzislau Rezki (Sony) * object is released. 39568ad4a33SUladzislau Rezki (Sony) * 39668ad4a33SUladzislau Rezki (Sony) * Each vmap_area node contains a maximum available free block 39768ad4a33SUladzislau Rezki (Sony) * of its sub-tree, right or left. Therefore it is possible to 39868ad4a33SUladzislau Rezki (Sony) * find a lowest match of free area. 39968ad4a33SUladzislau Rezki (Sony) */ 40068ad4a33SUladzislau Rezki (Sony) static struct rb_root free_vmap_area_root = RB_ROOT; 40168ad4a33SUladzislau Rezki (Sony) 40282dd23e8SUladzislau Rezki (Sony) /* 40382dd23e8SUladzislau Rezki (Sony) * Preload a CPU with one object for "no edge" split case. The 40482dd23e8SUladzislau Rezki (Sony) * aim is to get rid of allocations from the atomic context, thus 40582dd23e8SUladzislau Rezki (Sony) * to use more permissive allocation masks. 40682dd23e8SUladzislau Rezki (Sony) */ 40782dd23e8SUladzislau Rezki (Sony) static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node); 40882dd23e8SUladzislau Rezki (Sony) 40968ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 41068ad4a33SUladzislau Rezki (Sony) va_size(struct vmap_area *va) 41168ad4a33SUladzislau Rezki (Sony) { 41268ad4a33SUladzislau Rezki (Sony) return (va->va_end - va->va_start); 41368ad4a33SUladzislau Rezki (Sony) } 41468ad4a33SUladzislau Rezki (Sony) 41568ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 41668ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(struct rb_node *node) 41768ad4a33SUladzislau Rezki (Sony) { 41868ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 41968ad4a33SUladzislau Rezki (Sony) 42068ad4a33SUladzislau Rezki (Sony) va = rb_entry_safe(node, struct vmap_area, rb_node); 42168ad4a33SUladzislau Rezki (Sony) return va ? va->subtree_max_size : 0; 42268ad4a33SUladzislau Rezki (Sony) } 42368ad4a33SUladzislau Rezki (Sony) 42468ad4a33SUladzislau Rezki (Sony) /* 42568ad4a33SUladzislau Rezki (Sony) * Gets called when remove the node and rotate. 42668ad4a33SUladzislau Rezki (Sony) */ 42768ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 42868ad4a33SUladzislau Rezki (Sony) compute_subtree_max_size(struct vmap_area *va) 42968ad4a33SUladzislau Rezki (Sony) { 43068ad4a33SUladzislau Rezki (Sony) return max3(va_size(va), 43168ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(va->rb_node.rb_left), 43268ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(va->rb_node.rb_right)); 43368ad4a33SUladzislau Rezki (Sony) } 43468ad4a33SUladzislau Rezki (Sony) 435315cc066SMichel Lespinasse RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb, 436315cc066SMichel Lespinasse struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size) 43768ad4a33SUladzislau Rezki (Sony) 43868ad4a33SUladzislau Rezki (Sony) static void purge_vmap_area_lazy(void); 43968ad4a33SUladzislau Rezki (Sony) static BLOCKING_NOTIFIER_HEAD(vmap_notify_list); 44068ad4a33SUladzislau Rezki (Sony) static unsigned long lazy_max_pages(void); 441db64fe02SNick Piggin 44297105f0aSRoman Gushchin static atomic_long_t nr_vmalloc_pages; 44397105f0aSRoman Gushchin 44497105f0aSRoman Gushchin unsigned long vmalloc_nr_pages(void) 44597105f0aSRoman Gushchin { 44697105f0aSRoman Gushchin return atomic_long_read(&nr_vmalloc_pages); 44797105f0aSRoman Gushchin } 44897105f0aSRoman Gushchin 449db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr) 4501da177e4SLinus Torvalds { 451db64fe02SNick Piggin struct rb_node *n = vmap_area_root.rb_node; 452db64fe02SNick Piggin 453db64fe02SNick Piggin while (n) { 454db64fe02SNick Piggin struct vmap_area *va; 455db64fe02SNick Piggin 456db64fe02SNick Piggin va = rb_entry(n, struct vmap_area, rb_node); 457db64fe02SNick Piggin if (addr < va->va_start) 458db64fe02SNick Piggin n = n->rb_left; 459cef2ac3fSHATAYAMA Daisuke else if (addr >= va->va_end) 460db64fe02SNick Piggin n = n->rb_right; 461db64fe02SNick Piggin else 462db64fe02SNick Piggin return va; 463db64fe02SNick Piggin } 464db64fe02SNick Piggin 465db64fe02SNick Piggin return NULL; 466db64fe02SNick Piggin } 467db64fe02SNick Piggin 46868ad4a33SUladzislau Rezki (Sony) /* 46968ad4a33SUladzislau Rezki (Sony) * This function returns back addresses of parent node 47068ad4a33SUladzislau Rezki (Sony) * and its left or right link for further processing. 47168ad4a33SUladzislau Rezki (Sony) */ 47268ad4a33SUladzislau Rezki (Sony) static __always_inline struct rb_node ** 47368ad4a33SUladzislau Rezki (Sony) find_va_links(struct vmap_area *va, 47468ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct rb_node *from, 47568ad4a33SUladzislau Rezki (Sony) struct rb_node **parent) 476db64fe02SNick Piggin { 477170168d0SNamhyung Kim struct vmap_area *tmp_va; 47868ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 479db64fe02SNick Piggin 48068ad4a33SUladzislau Rezki (Sony) if (root) { 48168ad4a33SUladzislau Rezki (Sony) link = &root->rb_node; 48268ad4a33SUladzislau Rezki (Sony) if (unlikely(!*link)) { 48368ad4a33SUladzislau Rezki (Sony) *parent = NULL; 48468ad4a33SUladzislau Rezki (Sony) return link; 48568ad4a33SUladzislau Rezki (Sony) } 48668ad4a33SUladzislau Rezki (Sony) } else { 48768ad4a33SUladzislau Rezki (Sony) link = &from; 48868ad4a33SUladzislau Rezki (Sony) } 48968ad4a33SUladzislau Rezki (Sony) 49068ad4a33SUladzislau Rezki (Sony) /* 49168ad4a33SUladzislau Rezki (Sony) * Go to the bottom of the tree. When we hit the last point 49268ad4a33SUladzislau Rezki (Sony) * we end up with parent rb_node and correct direction, i name 49368ad4a33SUladzislau Rezki (Sony) * it link, where the new va->rb_node will be attached to. 49468ad4a33SUladzislau Rezki (Sony) */ 49568ad4a33SUladzislau Rezki (Sony) do { 49668ad4a33SUladzislau Rezki (Sony) tmp_va = rb_entry(*link, struct vmap_area, rb_node); 49768ad4a33SUladzislau Rezki (Sony) 49868ad4a33SUladzislau Rezki (Sony) /* 49968ad4a33SUladzislau Rezki (Sony) * During the traversal we also do some sanity check. 50068ad4a33SUladzislau Rezki (Sony) * Trigger the BUG() if there are sides(left/right) 50168ad4a33SUladzislau Rezki (Sony) * or full overlaps. 50268ad4a33SUladzislau Rezki (Sony) */ 50368ad4a33SUladzislau Rezki (Sony) if (va->va_start < tmp_va->va_end && 50468ad4a33SUladzislau Rezki (Sony) va->va_end <= tmp_va->va_start) 50568ad4a33SUladzislau Rezki (Sony) link = &(*link)->rb_left; 50668ad4a33SUladzislau Rezki (Sony) else if (va->va_end > tmp_va->va_start && 50768ad4a33SUladzislau Rezki (Sony) va->va_start >= tmp_va->va_end) 50868ad4a33SUladzislau Rezki (Sony) link = &(*link)->rb_right; 509db64fe02SNick Piggin else 510db64fe02SNick Piggin BUG(); 51168ad4a33SUladzislau Rezki (Sony) } while (*link); 51268ad4a33SUladzislau Rezki (Sony) 51368ad4a33SUladzislau Rezki (Sony) *parent = &tmp_va->rb_node; 51468ad4a33SUladzislau Rezki (Sony) return link; 515db64fe02SNick Piggin } 516db64fe02SNick Piggin 51768ad4a33SUladzislau Rezki (Sony) static __always_inline struct list_head * 51868ad4a33SUladzislau Rezki (Sony) get_va_next_sibling(struct rb_node *parent, struct rb_node **link) 51968ad4a33SUladzislau Rezki (Sony) { 52068ad4a33SUladzislau Rezki (Sony) struct list_head *list; 521db64fe02SNick Piggin 52268ad4a33SUladzislau Rezki (Sony) if (unlikely(!parent)) 52368ad4a33SUladzislau Rezki (Sony) /* 52468ad4a33SUladzislau Rezki (Sony) * The red-black tree where we try to find VA neighbors 52568ad4a33SUladzislau Rezki (Sony) * before merging or inserting is empty, i.e. it means 52668ad4a33SUladzislau Rezki (Sony) * there is no free vmap space. Normally it does not 52768ad4a33SUladzislau Rezki (Sony) * happen but we handle this case anyway. 52868ad4a33SUladzislau Rezki (Sony) */ 52968ad4a33SUladzislau Rezki (Sony) return NULL; 53068ad4a33SUladzislau Rezki (Sony) 53168ad4a33SUladzislau Rezki (Sony) list = &rb_entry(parent, struct vmap_area, rb_node)->list; 53268ad4a33SUladzislau Rezki (Sony) return (&parent->rb_right == link ? list->next : list); 533db64fe02SNick Piggin } 534db64fe02SNick Piggin 53568ad4a33SUladzislau Rezki (Sony) static __always_inline void 53668ad4a33SUladzislau Rezki (Sony) link_va(struct vmap_area *va, struct rb_root *root, 53768ad4a33SUladzislau Rezki (Sony) struct rb_node *parent, struct rb_node **link, struct list_head *head) 53868ad4a33SUladzislau Rezki (Sony) { 53968ad4a33SUladzislau Rezki (Sony) /* 54068ad4a33SUladzislau Rezki (Sony) * VA is still not in the list, but we can 54168ad4a33SUladzislau Rezki (Sony) * identify its future previous list_head node. 54268ad4a33SUladzislau Rezki (Sony) */ 54368ad4a33SUladzislau Rezki (Sony) if (likely(parent)) { 54468ad4a33SUladzislau Rezki (Sony) head = &rb_entry(parent, struct vmap_area, rb_node)->list; 54568ad4a33SUladzislau Rezki (Sony) if (&parent->rb_right != link) 54668ad4a33SUladzislau Rezki (Sony) head = head->prev; 54768ad4a33SUladzislau Rezki (Sony) } 548db64fe02SNick Piggin 54968ad4a33SUladzislau Rezki (Sony) /* Insert to the rb-tree */ 55068ad4a33SUladzislau Rezki (Sony) rb_link_node(&va->rb_node, parent, link); 55168ad4a33SUladzislau Rezki (Sony) if (root == &free_vmap_area_root) { 55268ad4a33SUladzislau Rezki (Sony) /* 55368ad4a33SUladzislau Rezki (Sony) * Some explanation here. Just perform simple insertion 55468ad4a33SUladzislau Rezki (Sony) * to the tree. We do not set va->subtree_max_size to 55568ad4a33SUladzislau Rezki (Sony) * its current size before calling rb_insert_augmented(). 55668ad4a33SUladzislau Rezki (Sony) * It is because of we populate the tree from the bottom 55768ad4a33SUladzislau Rezki (Sony) * to parent levels when the node _is_ in the tree. 55868ad4a33SUladzislau Rezki (Sony) * 55968ad4a33SUladzislau Rezki (Sony) * Therefore we set subtree_max_size to zero after insertion, 56068ad4a33SUladzislau Rezki (Sony) * to let __augment_tree_propagate_from() puts everything to 56168ad4a33SUladzislau Rezki (Sony) * the correct order later on. 56268ad4a33SUladzislau Rezki (Sony) */ 56368ad4a33SUladzislau Rezki (Sony) rb_insert_augmented(&va->rb_node, 56468ad4a33SUladzislau Rezki (Sony) root, &free_vmap_area_rb_augment_cb); 56568ad4a33SUladzislau Rezki (Sony) va->subtree_max_size = 0; 56668ad4a33SUladzislau Rezki (Sony) } else { 56768ad4a33SUladzislau Rezki (Sony) rb_insert_color(&va->rb_node, root); 56868ad4a33SUladzislau Rezki (Sony) } 56968ad4a33SUladzislau Rezki (Sony) 57068ad4a33SUladzislau Rezki (Sony) /* Address-sort this list */ 57168ad4a33SUladzislau Rezki (Sony) list_add(&va->list, head); 57268ad4a33SUladzislau Rezki (Sony) } 57368ad4a33SUladzislau Rezki (Sony) 57468ad4a33SUladzislau Rezki (Sony) static __always_inline void 57568ad4a33SUladzislau Rezki (Sony) unlink_va(struct vmap_area *va, struct rb_root *root) 57668ad4a33SUladzislau Rezki (Sony) { 577460e42d1SUladzislau Rezki (Sony) if (WARN_ON(RB_EMPTY_NODE(&va->rb_node))) 578460e42d1SUladzislau Rezki (Sony) return; 579460e42d1SUladzislau Rezki (Sony) 58068ad4a33SUladzislau Rezki (Sony) if (root == &free_vmap_area_root) 58168ad4a33SUladzislau Rezki (Sony) rb_erase_augmented(&va->rb_node, 58268ad4a33SUladzislau Rezki (Sony) root, &free_vmap_area_rb_augment_cb); 58368ad4a33SUladzislau Rezki (Sony) else 58468ad4a33SUladzislau Rezki (Sony) rb_erase(&va->rb_node, root); 58568ad4a33SUladzislau Rezki (Sony) 58668ad4a33SUladzislau Rezki (Sony) list_del(&va->list); 58768ad4a33SUladzislau Rezki (Sony) RB_CLEAR_NODE(&va->rb_node); 58868ad4a33SUladzislau Rezki (Sony) } 58968ad4a33SUladzislau Rezki (Sony) 590bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK 591bb850f4dSUladzislau Rezki (Sony) static void 592bb850f4dSUladzislau Rezki (Sony) augment_tree_propagate_check(struct rb_node *n) 593bb850f4dSUladzislau Rezki (Sony) { 594bb850f4dSUladzislau Rezki (Sony) struct vmap_area *va; 595bb850f4dSUladzislau Rezki (Sony) struct rb_node *node; 596bb850f4dSUladzislau Rezki (Sony) unsigned long size; 597bb850f4dSUladzislau Rezki (Sony) bool found = false; 598bb850f4dSUladzislau Rezki (Sony) 599bb850f4dSUladzislau Rezki (Sony) if (n == NULL) 600bb850f4dSUladzislau Rezki (Sony) return; 601bb850f4dSUladzislau Rezki (Sony) 602bb850f4dSUladzislau Rezki (Sony) va = rb_entry(n, struct vmap_area, rb_node); 603bb850f4dSUladzislau Rezki (Sony) size = va->subtree_max_size; 604bb850f4dSUladzislau Rezki (Sony) node = n; 605bb850f4dSUladzislau Rezki (Sony) 606bb850f4dSUladzislau Rezki (Sony) while (node) { 607bb850f4dSUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 608bb850f4dSUladzislau Rezki (Sony) 609bb850f4dSUladzislau Rezki (Sony) if (get_subtree_max_size(node->rb_left) == size) { 610bb850f4dSUladzislau Rezki (Sony) node = node->rb_left; 611bb850f4dSUladzislau Rezki (Sony) } else { 612bb850f4dSUladzislau Rezki (Sony) if (va_size(va) == size) { 613bb850f4dSUladzislau Rezki (Sony) found = true; 614bb850f4dSUladzislau Rezki (Sony) break; 615bb850f4dSUladzislau Rezki (Sony) } 616bb850f4dSUladzislau Rezki (Sony) 617bb850f4dSUladzislau Rezki (Sony) node = node->rb_right; 618bb850f4dSUladzislau Rezki (Sony) } 619bb850f4dSUladzislau Rezki (Sony) } 620bb850f4dSUladzislau Rezki (Sony) 621bb850f4dSUladzislau Rezki (Sony) if (!found) { 622bb850f4dSUladzislau Rezki (Sony) va = rb_entry(n, struct vmap_area, rb_node); 623bb850f4dSUladzislau Rezki (Sony) pr_emerg("tree is corrupted: %lu, %lu\n", 624bb850f4dSUladzislau Rezki (Sony) va_size(va), va->subtree_max_size); 625bb850f4dSUladzislau Rezki (Sony) } 626bb850f4dSUladzislau Rezki (Sony) 627bb850f4dSUladzislau Rezki (Sony) augment_tree_propagate_check(n->rb_left); 628bb850f4dSUladzislau Rezki (Sony) augment_tree_propagate_check(n->rb_right); 629bb850f4dSUladzislau Rezki (Sony) } 630bb850f4dSUladzislau Rezki (Sony) #endif 631bb850f4dSUladzislau Rezki (Sony) 63268ad4a33SUladzislau Rezki (Sony) /* 63368ad4a33SUladzislau Rezki (Sony) * This function populates subtree_max_size from bottom to upper 63468ad4a33SUladzislau Rezki (Sony) * levels starting from VA point. The propagation must be done 63568ad4a33SUladzislau Rezki (Sony) * when VA size is modified by changing its va_start/va_end. Or 63668ad4a33SUladzislau Rezki (Sony) * in case of newly inserting of VA to the tree. 63768ad4a33SUladzislau Rezki (Sony) * 63868ad4a33SUladzislau Rezki (Sony) * It means that __augment_tree_propagate_from() must be called: 63968ad4a33SUladzislau Rezki (Sony) * - After VA has been inserted to the tree(free path); 64068ad4a33SUladzislau Rezki (Sony) * - After VA has been shrunk(allocation path); 64168ad4a33SUladzislau Rezki (Sony) * - After VA has been increased(merging path). 64268ad4a33SUladzislau Rezki (Sony) * 64368ad4a33SUladzislau Rezki (Sony) * Please note that, it does not mean that upper parent nodes 64468ad4a33SUladzislau Rezki (Sony) * and their subtree_max_size are recalculated all the time up 64568ad4a33SUladzislau Rezki (Sony) * to the root node. 64668ad4a33SUladzislau Rezki (Sony) * 64768ad4a33SUladzislau Rezki (Sony) * 4--8 64868ad4a33SUladzislau Rezki (Sony) * /\ 64968ad4a33SUladzislau Rezki (Sony) * / \ 65068ad4a33SUladzislau Rezki (Sony) * / \ 65168ad4a33SUladzislau Rezki (Sony) * 2--2 8--8 65268ad4a33SUladzislau Rezki (Sony) * 65368ad4a33SUladzislau Rezki (Sony) * For example if we modify the node 4, shrinking it to 2, then 65468ad4a33SUladzislau Rezki (Sony) * no any modification is required. If we shrink the node 2 to 1 65568ad4a33SUladzislau Rezki (Sony) * its subtree_max_size is updated only, and set to 1. If we shrink 65668ad4a33SUladzislau Rezki (Sony) * the node 8 to 6, then its subtree_max_size is set to 6 and parent 65768ad4a33SUladzislau Rezki (Sony) * node becomes 4--6. 65868ad4a33SUladzislau Rezki (Sony) */ 65968ad4a33SUladzislau Rezki (Sony) static __always_inline void 66068ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(struct vmap_area *va) 66168ad4a33SUladzislau Rezki (Sony) { 66268ad4a33SUladzislau Rezki (Sony) struct rb_node *node = &va->rb_node; 66368ad4a33SUladzislau Rezki (Sony) unsigned long new_va_sub_max_size; 66468ad4a33SUladzislau Rezki (Sony) 66568ad4a33SUladzislau Rezki (Sony) while (node) { 66668ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 66768ad4a33SUladzislau Rezki (Sony) new_va_sub_max_size = compute_subtree_max_size(va); 66868ad4a33SUladzislau Rezki (Sony) 66968ad4a33SUladzislau Rezki (Sony) /* 67068ad4a33SUladzislau Rezki (Sony) * If the newly calculated maximum available size of the 67168ad4a33SUladzislau Rezki (Sony) * subtree is equal to the current one, then it means that 67268ad4a33SUladzislau Rezki (Sony) * the tree is propagated correctly. So we have to stop at 67368ad4a33SUladzislau Rezki (Sony) * this point to save cycles. 67468ad4a33SUladzislau Rezki (Sony) */ 67568ad4a33SUladzislau Rezki (Sony) if (va->subtree_max_size == new_va_sub_max_size) 67668ad4a33SUladzislau Rezki (Sony) break; 67768ad4a33SUladzislau Rezki (Sony) 67868ad4a33SUladzislau Rezki (Sony) va->subtree_max_size = new_va_sub_max_size; 67968ad4a33SUladzislau Rezki (Sony) node = rb_parent(&va->rb_node); 68068ad4a33SUladzislau Rezki (Sony) } 681bb850f4dSUladzislau Rezki (Sony) 682bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK 683bb850f4dSUladzislau Rezki (Sony) augment_tree_propagate_check(free_vmap_area_root.rb_node); 684bb850f4dSUladzislau Rezki (Sony) #endif 68568ad4a33SUladzislau Rezki (Sony) } 68668ad4a33SUladzislau Rezki (Sony) 68768ad4a33SUladzislau Rezki (Sony) static void 68868ad4a33SUladzislau Rezki (Sony) insert_vmap_area(struct vmap_area *va, 68968ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 69068ad4a33SUladzislau Rezki (Sony) { 69168ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 69268ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 69368ad4a33SUladzislau Rezki (Sony) 69468ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 69568ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 69668ad4a33SUladzislau Rezki (Sony) } 69768ad4a33SUladzislau Rezki (Sony) 69868ad4a33SUladzislau Rezki (Sony) static void 69968ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(struct vmap_area *va, 70068ad4a33SUladzislau Rezki (Sony) struct rb_node *from, struct rb_root *root, 70168ad4a33SUladzislau Rezki (Sony) struct list_head *head) 70268ad4a33SUladzislau Rezki (Sony) { 70368ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 70468ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 70568ad4a33SUladzislau Rezki (Sony) 70668ad4a33SUladzislau Rezki (Sony) if (from) 70768ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, NULL, from, &parent); 70868ad4a33SUladzislau Rezki (Sony) else 70968ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 71068ad4a33SUladzislau Rezki (Sony) 71168ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 71268ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 71368ad4a33SUladzislau Rezki (Sony) } 71468ad4a33SUladzislau Rezki (Sony) 71568ad4a33SUladzislau Rezki (Sony) /* 71668ad4a33SUladzislau Rezki (Sony) * Merge de-allocated chunk of VA memory with previous 71768ad4a33SUladzislau Rezki (Sony) * and next free blocks. If coalesce is not done a new 71868ad4a33SUladzislau Rezki (Sony) * free area is inserted. If VA has been merged, it is 71968ad4a33SUladzislau Rezki (Sony) * freed. 72068ad4a33SUladzislau Rezki (Sony) */ 7213c5c3cfbSDaniel Axtens static __always_inline struct vmap_area * 72268ad4a33SUladzislau Rezki (Sony) merge_or_add_vmap_area(struct vmap_area *va, 72368ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 72468ad4a33SUladzislau Rezki (Sony) { 72568ad4a33SUladzislau Rezki (Sony) struct vmap_area *sibling; 72668ad4a33SUladzislau Rezki (Sony) struct list_head *next; 72768ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 72868ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 72968ad4a33SUladzislau Rezki (Sony) bool merged = false; 73068ad4a33SUladzislau Rezki (Sony) 73168ad4a33SUladzislau Rezki (Sony) /* 73268ad4a33SUladzislau Rezki (Sony) * Find a place in the tree where VA potentially will be 73368ad4a33SUladzislau Rezki (Sony) * inserted, unless it is merged with its sibling/siblings. 73468ad4a33SUladzislau Rezki (Sony) */ 73568ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 73668ad4a33SUladzislau Rezki (Sony) 73768ad4a33SUladzislau Rezki (Sony) /* 73868ad4a33SUladzislau Rezki (Sony) * Get next node of VA to check if merging can be done. 73968ad4a33SUladzislau Rezki (Sony) */ 74068ad4a33SUladzislau Rezki (Sony) next = get_va_next_sibling(parent, link); 74168ad4a33SUladzislau Rezki (Sony) if (unlikely(next == NULL)) 74268ad4a33SUladzislau Rezki (Sony) goto insert; 74368ad4a33SUladzislau Rezki (Sony) 74468ad4a33SUladzislau Rezki (Sony) /* 74568ad4a33SUladzislau Rezki (Sony) * start end 74668ad4a33SUladzislau Rezki (Sony) * | | 74768ad4a33SUladzislau Rezki (Sony) * |<------VA------>|<-----Next----->| 74868ad4a33SUladzislau Rezki (Sony) * | | 74968ad4a33SUladzislau Rezki (Sony) * start end 75068ad4a33SUladzislau Rezki (Sony) */ 75168ad4a33SUladzislau Rezki (Sony) if (next != head) { 75268ad4a33SUladzislau Rezki (Sony) sibling = list_entry(next, struct vmap_area, list); 75368ad4a33SUladzislau Rezki (Sony) if (sibling->va_start == va->va_end) { 75468ad4a33SUladzislau Rezki (Sony) sibling->va_start = va->va_start; 75568ad4a33SUladzislau Rezki (Sony) 75668ad4a33SUladzislau Rezki (Sony) /* Check and update the tree if needed. */ 75768ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(sibling); 75868ad4a33SUladzislau Rezki (Sony) 75968ad4a33SUladzislau Rezki (Sony) /* Free vmap_area object. */ 76068ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 76168ad4a33SUladzislau Rezki (Sony) 76268ad4a33SUladzislau Rezki (Sony) /* Point to the new merged area. */ 76368ad4a33SUladzislau Rezki (Sony) va = sibling; 76468ad4a33SUladzislau Rezki (Sony) merged = true; 76568ad4a33SUladzislau Rezki (Sony) } 76668ad4a33SUladzislau Rezki (Sony) } 76768ad4a33SUladzislau Rezki (Sony) 76868ad4a33SUladzislau Rezki (Sony) /* 76968ad4a33SUladzislau Rezki (Sony) * start end 77068ad4a33SUladzislau Rezki (Sony) * | | 77168ad4a33SUladzislau Rezki (Sony) * |<-----Prev----->|<------VA------>| 77268ad4a33SUladzislau Rezki (Sony) * | | 77368ad4a33SUladzislau Rezki (Sony) * start end 77468ad4a33SUladzislau Rezki (Sony) */ 77568ad4a33SUladzislau Rezki (Sony) if (next->prev != head) { 77668ad4a33SUladzislau Rezki (Sony) sibling = list_entry(next->prev, struct vmap_area, list); 77768ad4a33SUladzislau Rezki (Sony) if (sibling->va_end == va->va_start) { 77868ad4a33SUladzislau Rezki (Sony) sibling->va_end = va->va_end; 77968ad4a33SUladzislau Rezki (Sony) 78068ad4a33SUladzislau Rezki (Sony) /* Check and update the tree if needed. */ 78168ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(sibling); 78268ad4a33SUladzislau Rezki (Sony) 78354f63d9dSUladzislau Rezki (Sony) if (merged) 78468ad4a33SUladzislau Rezki (Sony) unlink_va(va, root); 78568ad4a33SUladzislau Rezki (Sony) 78668ad4a33SUladzislau Rezki (Sony) /* Free vmap_area object. */ 78768ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 7883c5c3cfbSDaniel Axtens 7893c5c3cfbSDaniel Axtens /* Point to the new merged area. */ 7903c5c3cfbSDaniel Axtens va = sibling; 7913c5c3cfbSDaniel Axtens merged = true; 79268ad4a33SUladzislau Rezki (Sony) } 79368ad4a33SUladzislau Rezki (Sony) } 79468ad4a33SUladzislau Rezki (Sony) 79568ad4a33SUladzislau Rezki (Sony) insert: 79668ad4a33SUladzislau Rezki (Sony) if (!merged) { 79768ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 79868ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 79968ad4a33SUladzislau Rezki (Sony) } 8003c5c3cfbSDaniel Axtens 8013c5c3cfbSDaniel Axtens return va; 80268ad4a33SUladzislau Rezki (Sony) } 80368ad4a33SUladzislau Rezki (Sony) 80468ad4a33SUladzislau Rezki (Sony) static __always_inline bool 80568ad4a33SUladzislau Rezki (Sony) is_within_this_va(struct vmap_area *va, unsigned long size, 80668ad4a33SUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 80768ad4a33SUladzislau Rezki (Sony) { 80868ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr; 80968ad4a33SUladzislau Rezki (Sony) 81068ad4a33SUladzislau Rezki (Sony) if (va->va_start > vstart) 81168ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(va->va_start, align); 81268ad4a33SUladzislau Rezki (Sony) else 81368ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(vstart, align); 81468ad4a33SUladzislau Rezki (Sony) 81568ad4a33SUladzislau Rezki (Sony) /* Can be overflowed due to big size or alignment. */ 81668ad4a33SUladzislau Rezki (Sony) if (nva_start_addr + size < nva_start_addr || 81768ad4a33SUladzislau Rezki (Sony) nva_start_addr < vstart) 81868ad4a33SUladzislau Rezki (Sony) return false; 81968ad4a33SUladzislau Rezki (Sony) 82068ad4a33SUladzislau Rezki (Sony) return (nva_start_addr + size <= va->va_end); 82168ad4a33SUladzislau Rezki (Sony) } 82268ad4a33SUladzislau Rezki (Sony) 82368ad4a33SUladzislau Rezki (Sony) /* 82468ad4a33SUladzislau Rezki (Sony) * Find the first free block(lowest start address) in the tree, 82568ad4a33SUladzislau Rezki (Sony) * that will accomplish the request corresponding to passing 82668ad4a33SUladzislau Rezki (Sony) * parameters. 82768ad4a33SUladzislau Rezki (Sony) */ 82868ad4a33SUladzislau Rezki (Sony) static __always_inline struct vmap_area * 82968ad4a33SUladzislau Rezki (Sony) find_vmap_lowest_match(unsigned long size, 83068ad4a33SUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 83168ad4a33SUladzislau Rezki (Sony) { 83268ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 83368ad4a33SUladzislau Rezki (Sony) struct rb_node *node; 83468ad4a33SUladzislau Rezki (Sony) unsigned long length; 83568ad4a33SUladzislau Rezki (Sony) 83668ad4a33SUladzislau Rezki (Sony) /* Start from the root. */ 83768ad4a33SUladzislau Rezki (Sony) node = free_vmap_area_root.rb_node; 83868ad4a33SUladzislau Rezki (Sony) 83968ad4a33SUladzislau Rezki (Sony) /* Adjust the search size for alignment overhead. */ 84068ad4a33SUladzislau Rezki (Sony) length = size + align - 1; 84168ad4a33SUladzislau Rezki (Sony) 84268ad4a33SUladzislau Rezki (Sony) while (node) { 84368ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 84468ad4a33SUladzislau Rezki (Sony) 84568ad4a33SUladzislau Rezki (Sony) if (get_subtree_max_size(node->rb_left) >= length && 84668ad4a33SUladzislau Rezki (Sony) vstart < va->va_start) { 84768ad4a33SUladzislau Rezki (Sony) node = node->rb_left; 84868ad4a33SUladzislau Rezki (Sony) } else { 84968ad4a33SUladzislau Rezki (Sony) if (is_within_this_va(va, size, align, vstart)) 85068ad4a33SUladzislau Rezki (Sony) return va; 85168ad4a33SUladzislau Rezki (Sony) 85268ad4a33SUladzislau Rezki (Sony) /* 85368ad4a33SUladzislau Rezki (Sony) * Does not make sense to go deeper towards the right 85468ad4a33SUladzislau Rezki (Sony) * sub-tree if it does not have a free block that is 85568ad4a33SUladzislau Rezki (Sony) * equal or bigger to the requested search length. 85668ad4a33SUladzislau Rezki (Sony) */ 85768ad4a33SUladzislau Rezki (Sony) if (get_subtree_max_size(node->rb_right) >= length) { 85868ad4a33SUladzislau Rezki (Sony) node = node->rb_right; 85968ad4a33SUladzislau Rezki (Sony) continue; 86068ad4a33SUladzislau Rezki (Sony) } 86168ad4a33SUladzislau Rezki (Sony) 86268ad4a33SUladzislau Rezki (Sony) /* 8633806b041SAndrew Morton * OK. We roll back and find the first right sub-tree, 86468ad4a33SUladzislau Rezki (Sony) * that will satisfy the search criteria. It can happen 86568ad4a33SUladzislau Rezki (Sony) * only once due to "vstart" restriction. 86668ad4a33SUladzislau Rezki (Sony) */ 86768ad4a33SUladzislau Rezki (Sony) while ((node = rb_parent(node))) { 86868ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 86968ad4a33SUladzislau Rezki (Sony) if (is_within_this_va(va, size, align, vstart)) 87068ad4a33SUladzislau Rezki (Sony) return va; 87168ad4a33SUladzislau Rezki (Sony) 87268ad4a33SUladzislau Rezki (Sony) if (get_subtree_max_size(node->rb_right) >= length && 87368ad4a33SUladzislau Rezki (Sony) vstart <= va->va_start) { 87468ad4a33SUladzislau Rezki (Sony) node = node->rb_right; 87568ad4a33SUladzislau Rezki (Sony) break; 87668ad4a33SUladzislau Rezki (Sony) } 87768ad4a33SUladzislau Rezki (Sony) } 87868ad4a33SUladzislau Rezki (Sony) } 87968ad4a33SUladzislau Rezki (Sony) } 88068ad4a33SUladzislau Rezki (Sony) 88168ad4a33SUladzislau Rezki (Sony) return NULL; 88268ad4a33SUladzislau Rezki (Sony) } 88368ad4a33SUladzislau Rezki (Sony) 884a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 885a6cf4e0fSUladzislau Rezki (Sony) #include <linux/random.h> 886a6cf4e0fSUladzislau Rezki (Sony) 887a6cf4e0fSUladzislau Rezki (Sony) static struct vmap_area * 888a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_linear_match(unsigned long size, 889a6cf4e0fSUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 890a6cf4e0fSUladzislau Rezki (Sony) { 891a6cf4e0fSUladzislau Rezki (Sony) struct vmap_area *va; 892a6cf4e0fSUladzislau Rezki (Sony) 893a6cf4e0fSUladzislau Rezki (Sony) list_for_each_entry(va, &free_vmap_area_list, list) { 894a6cf4e0fSUladzislau Rezki (Sony) if (!is_within_this_va(va, size, align, vstart)) 895a6cf4e0fSUladzislau Rezki (Sony) continue; 896a6cf4e0fSUladzislau Rezki (Sony) 897a6cf4e0fSUladzislau Rezki (Sony) return va; 898a6cf4e0fSUladzislau Rezki (Sony) } 899a6cf4e0fSUladzislau Rezki (Sony) 900a6cf4e0fSUladzislau Rezki (Sony) return NULL; 901a6cf4e0fSUladzislau Rezki (Sony) } 902a6cf4e0fSUladzislau Rezki (Sony) 903a6cf4e0fSUladzislau Rezki (Sony) static void 904a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_match_check(unsigned long size) 905a6cf4e0fSUladzislau Rezki (Sony) { 906a6cf4e0fSUladzislau Rezki (Sony) struct vmap_area *va_1, *va_2; 907a6cf4e0fSUladzislau Rezki (Sony) unsigned long vstart; 908a6cf4e0fSUladzislau Rezki (Sony) unsigned int rnd; 909a6cf4e0fSUladzislau Rezki (Sony) 910a6cf4e0fSUladzislau Rezki (Sony) get_random_bytes(&rnd, sizeof(rnd)); 911a6cf4e0fSUladzislau Rezki (Sony) vstart = VMALLOC_START + rnd; 912a6cf4e0fSUladzislau Rezki (Sony) 913a6cf4e0fSUladzislau Rezki (Sony) va_1 = find_vmap_lowest_match(size, 1, vstart); 914a6cf4e0fSUladzislau Rezki (Sony) va_2 = find_vmap_lowest_linear_match(size, 1, vstart); 915a6cf4e0fSUladzislau Rezki (Sony) 916a6cf4e0fSUladzislau Rezki (Sony) if (va_1 != va_2) 917a6cf4e0fSUladzislau Rezki (Sony) pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n", 918a6cf4e0fSUladzislau Rezki (Sony) va_1, va_2, vstart); 919a6cf4e0fSUladzislau Rezki (Sony) } 920a6cf4e0fSUladzislau Rezki (Sony) #endif 921a6cf4e0fSUladzislau Rezki (Sony) 92268ad4a33SUladzislau Rezki (Sony) enum fit_type { 92368ad4a33SUladzislau Rezki (Sony) NOTHING_FIT = 0, 92468ad4a33SUladzislau Rezki (Sony) FL_FIT_TYPE = 1, /* full fit */ 92568ad4a33SUladzislau Rezki (Sony) LE_FIT_TYPE = 2, /* left edge fit */ 92668ad4a33SUladzislau Rezki (Sony) RE_FIT_TYPE = 3, /* right edge fit */ 92768ad4a33SUladzislau Rezki (Sony) NE_FIT_TYPE = 4 /* no edge fit */ 92868ad4a33SUladzislau Rezki (Sony) }; 92968ad4a33SUladzislau Rezki (Sony) 93068ad4a33SUladzislau Rezki (Sony) static __always_inline enum fit_type 93168ad4a33SUladzislau Rezki (Sony) classify_va_fit_type(struct vmap_area *va, 93268ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr, unsigned long size) 93368ad4a33SUladzislau Rezki (Sony) { 93468ad4a33SUladzislau Rezki (Sony) enum fit_type type; 93568ad4a33SUladzislau Rezki (Sony) 93668ad4a33SUladzislau Rezki (Sony) /* Check if it is within VA. */ 93768ad4a33SUladzislau Rezki (Sony) if (nva_start_addr < va->va_start || 93868ad4a33SUladzislau Rezki (Sony) nva_start_addr + size > va->va_end) 93968ad4a33SUladzislau Rezki (Sony) return NOTHING_FIT; 94068ad4a33SUladzislau Rezki (Sony) 94168ad4a33SUladzislau Rezki (Sony) /* Now classify. */ 94268ad4a33SUladzislau Rezki (Sony) if (va->va_start == nva_start_addr) { 94368ad4a33SUladzislau Rezki (Sony) if (va->va_end == nva_start_addr + size) 94468ad4a33SUladzislau Rezki (Sony) type = FL_FIT_TYPE; 94568ad4a33SUladzislau Rezki (Sony) else 94668ad4a33SUladzislau Rezki (Sony) type = LE_FIT_TYPE; 94768ad4a33SUladzislau Rezki (Sony) } else if (va->va_end == nva_start_addr + size) { 94868ad4a33SUladzislau Rezki (Sony) type = RE_FIT_TYPE; 94968ad4a33SUladzislau Rezki (Sony) } else { 95068ad4a33SUladzislau Rezki (Sony) type = NE_FIT_TYPE; 95168ad4a33SUladzislau Rezki (Sony) } 95268ad4a33SUladzislau Rezki (Sony) 95368ad4a33SUladzislau Rezki (Sony) return type; 95468ad4a33SUladzislau Rezki (Sony) } 95568ad4a33SUladzislau Rezki (Sony) 95668ad4a33SUladzislau Rezki (Sony) static __always_inline int 95768ad4a33SUladzislau Rezki (Sony) adjust_va_to_fit_type(struct vmap_area *va, 95868ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr, unsigned long size, 95968ad4a33SUladzislau Rezki (Sony) enum fit_type type) 96068ad4a33SUladzislau Rezki (Sony) { 9612c929233SArnd Bergmann struct vmap_area *lva = NULL; 96268ad4a33SUladzislau Rezki (Sony) 96368ad4a33SUladzislau Rezki (Sony) if (type == FL_FIT_TYPE) { 96468ad4a33SUladzislau Rezki (Sony) /* 96568ad4a33SUladzislau Rezki (Sony) * No need to split VA, it fully fits. 96668ad4a33SUladzislau Rezki (Sony) * 96768ad4a33SUladzislau Rezki (Sony) * | | 96868ad4a33SUladzislau Rezki (Sony) * V NVA V 96968ad4a33SUladzislau Rezki (Sony) * |---------------| 97068ad4a33SUladzislau Rezki (Sony) */ 97168ad4a33SUladzislau Rezki (Sony) unlink_va(va, &free_vmap_area_root); 97268ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 97368ad4a33SUladzislau Rezki (Sony) } else if (type == LE_FIT_TYPE) { 97468ad4a33SUladzislau Rezki (Sony) /* 97568ad4a33SUladzislau Rezki (Sony) * Split left edge of fit VA. 97668ad4a33SUladzislau Rezki (Sony) * 97768ad4a33SUladzislau Rezki (Sony) * | | 97868ad4a33SUladzislau Rezki (Sony) * V NVA V R 97968ad4a33SUladzislau Rezki (Sony) * |-------|-------| 98068ad4a33SUladzislau Rezki (Sony) */ 98168ad4a33SUladzislau Rezki (Sony) va->va_start += size; 98268ad4a33SUladzislau Rezki (Sony) } else if (type == RE_FIT_TYPE) { 98368ad4a33SUladzislau Rezki (Sony) /* 98468ad4a33SUladzislau Rezki (Sony) * Split right edge of fit VA. 98568ad4a33SUladzislau Rezki (Sony) * 98668ad4a33SUladzislau Rezki (Sony) * | | 98768ad4a33SUladzislau Rezki (Sony) * L V NVA V 98868ad4a33SUladzislau Rezki (Sony) * |-------|-------| 98968ad4a33SUladzislau Rezki (Sony) */ 99068ad4a33SUladzislau Rezki (Sony) va->va_end = nva_start_addr; 99168ad4a33SUladzislau Rezki (Sony) } else if (type == NE_FIT_TYPE) { 99268ad4a33SUladzislau Rezki (Sony) /* 99368ad4a33SUladzislau Rezki (Sony) * Split no edge of fit VA. 99468ad4a33SUladzislau Rezki (Sony) * 99568ad4a33SUladzislau Rezki (Sony) * | | 99668ad4a33SUladzislau Rezki (Sony) * L V NVA V R 99768ad4a33SUladzislau Rezki (Sony) * |---|-------|---| 99868ad4a33SUladzislau Rezki (Sony) */ 99982dd23e8SUladzislau Rezki (Sony) lva = __this_cpu_xchg(ne_fit_preload_node, NULL); 100082dd23e8SUladzislau Rezki (Sony) if (unlikely(!lva)) { 100182dd23e8SUladzislau Rezki (Sony) /* 100282dd23e8SUladzislau Rezki (Sony) * For percpu allocator we do not do any pre-allocation 100382dd23e8SUladzislau Rezki (Sony) * and leave it as it is. The reason is it most likely 100482dd23e8SUladzislau Rezki (Sony) * never ends up with NE_FIT_TYPE splitting. In case of 100582dd23e8SUladzislau Rezki (Sony) * percpu allocations offsets and sizes are aligned to 100682dd23e8SUladzislau Rezki (Sony) * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE 100782dd23e8SUladzislau Rezki (Sony) * are its main fitting cases. 100882dd23e8SUladzislau Rezki (Sony) * 100982dd23e8SUladzislau Rezki (Sony) * There are a few exceptions though, as an example it is 101082dd23e8SUladzislau Rezki (Sony) * a first allocation (early boot up) when we have "one" 101182dd23e8SUladzislau Rezki (Sony) * big free space that has to be split. 1012060650a2SUladzislau Rezki (Sony) * 1013060650a2SUladzislau Rezki (Sony) * Also we can hit this path in case of regular "vmap" 1014060650a2SUladzislau Rezki (Sony) * allocations, if "this" current CPU was not preloaded. 1015060650a2SUladzislau Rezki (Sony) * See the comment in alloc_vmap_area() why. If so, then 1016060650a2SUladzislau Rezki (Sony) * GFP_NOWAIT is used instead to get an extra object for 1017060650a2SUladzislau Rezki (Sony) * split purpose. That is rare and most time does not 1018060650a2SUladzislau Rezki (Sony) * occur. 1019060650a2SUladzislau Rezki (Sony) * 1020060650a2SUladzislau Rezki (Sony) * What happens if an allocation gets failed. Basically, 1021060650a2SUladzislau Rezki (Sony) * an "overflow" path is triggered to purge lazily freed 1022060650a2SUladzislau Rezki (Sony) * areas to free some memory, then, the "retry" path is 1023060650a2SUladzislau Rezki (Sony) * triggered to repeat one more time. See more details 1024060650a2SUladzislau Rezki (Sony) * in alloc_vmap_area() function. 102582dd23e8SUladzislau Rezki (Sony) */ 102668ad4a33SUladzislau Rezki (Sony) lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT); 102782dd23e8SUladzislau Rezki (Sony) if (!lva) 102868ad4a33SUladzislau Rezki (Sony) return -1; 102982dd23e8SUladzislau Rezki (Sony) } 103068ad4a33SUladzislau Rezki (Sony) 103168ad4a33SUladzislau Rezki (Sony) /* 103268ad4a33SUladzislau Rezki (Sony) * Build the remainder. 103368ad4a33SUladzislau Rezki (Sony) */ 103468ad4a33SUladzislau Rezki (Sony) lva->va_start = va->va_start; 103568ad4a33SUladzislau Rezki (Sony) lva->va_end = nva_start_addr; 103668ad4a33SUladzislau Rezki (Sony) 103768ad4a33SUladzislau Rezki (Sony) /* 103868ad4a33SUladzislau Rezki (Sony) * Shrink this VA to remaining size. 103968ad4a33SUladzislau Rezki (Sony) */ 104068ad4a33SUladzislau Rezki (Sony) va->va_start = nva_start_addr + size; 104168ad4a33SUladzislau Rezki (Sony) } else { 104268ad4a33SUladzislau Rezki (Sony) return -1; 104368ad4a33SUladzislau Rezki (Sony) } 104468ad4a33SUladzislau Rezki (Sony) 104568ad4a33SUladzislau Rezki (Sony) if (type != FL_FIT_TYPE) { 104668ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 104768ad4a33SUladzislau Rezki (Sony) 10482c929233SArnd Bergmann if (lva) /* type == NE_FIT_TYPE */ 104968ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(lva, &va->rb_node, 105068ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, &free_vmap_area_list); 105168ad4a33SUladzislau Rezki (Sony) } 105268ad4a33SUladzislau Rezki (Sony) 105368ad4a33SUladzislau Rezki (Sony) return 0; 105468ad4a33SUladzislau Rezki (Sony) } 105568ad4a33SUladzislau Rezki (Sony) 105668ad4a33SUladzislau Rezki (Sony) /* 105768ad4a33SUladzislau Rezki (Sony) * Returns a start address of the newly allocated area, if success. 105868ad4a33SUladzislau Rezki (Sony) * Otherwise a vend is returned that indicates failure. 105968ad4a33SUladzislau Rezki (Sony) */ 106068ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 106168ad4a33SUladzislau Rezki (Sony) __alloc_vmap_area(unsigned long size, unsigned long align, 1062cacca6baSUladzislau Rezki (Sony) unsigned long vstart, unsigned long vend) 106368ad4a33SUladzislau Rezki (Sony) { 106468ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr; 106568ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 106668ad4a33SUladzislau Rezki (Sony) enum fit_type type; 106768ad4a33SUladzislau Rezki (Sony) int ret; 106868ad4a33SUladzislau Rezki (Sony) 106968ad4a33SUladzislau Rezki (Sony) va = find_vmap_lowest_match(size, align, vstart); 107068ad4a33SUladzislau Rezki (Sony) if (unlikely(!va)) 107168ad4a33SUladzislau Rezki (Sony) return vend; 107268ad4a33SUladzislau Rezki (Sony) 107368ad4a33SUladzislau Rezki (Sony) if (va->va_start > vstart) 107468ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(va->va_start, align); 107568ad4a33SUladzislau Rezki (Sony) else 107668ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(vstart, align); 107768ad4a33SUladzislau Rezki (Sony) 107868ad4a33SUladzislau Rezki (Sony) /* Check the "vend" restriction. */ 107968ad4a33SUladzislau Rezki (Sony) if (nva_start_addr + size > vend) 108068ad4a33SUladzislau Rezki (Sony) return vend; 108168ad4a33SUladzislau Rezki (Sony) 108268ad4a33SUladzislau Rezki (Sony) /* Classify what we have found. */ 108368ad4a33SUladzislau Rezki (Sony) type = classify_va_fit_type(va, nva_start_addr, size); 108468ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(type == NOTHING_FIT)) 108568ad4a33SUladzislau Rezki (Sony) return vend; 108668ad4a33SUladzislau Rezki (Sony) 108768ad4a33SUladzislau Rezki (Sony) /* Update the free vmap_area. */ 108868ad4a33SUladzislau Rezki (Sony) ret = adjust_va_to_fit_type(va, nva_start_addr, size, type); 108968ad4a33SUladzislau Rezki (Sony) if (ret) 109068ad4a33SUladzislau Rezki (Sony) return vend; 109168ad4a33SUladzislau Rezki (Sony) 1092a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 1093a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_match_check(size); 1094a6cf4e0fSUladzislau Rezki (Sony) #endif 1095a6cf4e0fSUladzislau Rezki (Sony) 109668ad4a33SUladzislau Rezki (Sony) return nva_start_addr; 109768ad4a33SUladzislau Rezki (Sony) } 10984da56b99SChris Wilson 1099db64fe02SNick Piggin /* 1100d98c9e83SAndrey Ryabinin * Free a region of KVA allocated by alloc_vmap_area 1101d98c9e83SAndrey Ryabinin */ 1102d98c9e83SAndrey Ryabinin static void free_vmap_area(struct vmap_area *va) 1103d98c9e83SAndrey Ryabinin { 1104d98c9e83SAndrey Ryabinin /* 1105d98c9e83SAndrey Ryabinin * Remove from the busy tree/list. 1106d98c9e83SAndrey Ryabinin */ 1107d98c9e83SAndrey Ryabinin spin_lock(&vmap_area_lock); 1108d98c9e83SAndrey Ryabinin unlink_va(va, &vmap_area_root); 1109d98c9e83SAndrey Ryabinin spin_unlock(&vmap_area_lock); 1110d98c9e83SAndrey Ryabinin 1111d98c9e83SAndrey Ryabinin /* 1112d98c9e83SAndrey Ryabinin * Insert/Merge it back to the free tree/list. 1113d98c9e83SAndrey Ryabinin */ 1114d98c9e83SAndrey Ryabinin spin_lock(&free_vmap_area_lock); 1115d98c9e83SAndrey Ryabinin merge_or_add_vmap_area(va, &free_vmap_area_root, &free_vmap_area_list); 1116d98c9e83SAndrey Ryabinin spin_unlock(&free_vmap_area_lock); 1117d98c9e83SAndrey Ryabinin } 1118d98c9e83SAndrey Ryabinin 1119d98c9e83SAndrey Ryabinin /* 1120db64fe02SNick Piggin * Allocate a region of KVA of the specified size and alignment, within the 1121db64fe02SNick Piggin * vstart and vend. 1122db64fe02SNick Piggin */ 1123db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size, 1124db64fe02SNick Piggin unsigned long align, 1125db64fe02SNick Piggin unsigned long vstart, unsigned long vend, 1126db64fe02SNick Piggin int node, gfp_t gfp_mask) 1127db64fe02SNick Piggin { 112882dd23e8SUladzislau Rezki (Sony) struct vmap_area *va, *pva; 11291da177e4SLinus Torvalds unsigned long addr; 1130db64fe02SNick Piggin int purged = 0; 1131d98c9e83SAndrey Ryabinin int ret; 1132db64fe02SNick Piggin 11337766970cSNick Piggin BUG_ON(!size); 1134891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 113589699605SNick Piggin BUG_ON(!is_power_of_2(align)); 1136db64fe02SNick Piggin 113768ad4a33SUladzislau Rezki (Sony) if (unlikely(!vmap_initialized)) 113868ad4a33SUladzislau Rezki (Sony) return ERR_PTR(-EBUSY); 113968ad4a33SUladzislau Rezki (Sony) 11405803ed29SChristoph Hellwig might_sleep(); 1141f07116d7SUladzislau Rezki (Sony) gfp_mask = gfp_mask & GFP_RECLAIM_MASK; 11424da56b99SChris Wilson 1143f07116d7SUladzislau Rezki (Sony) va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); 1144db64fe02SNick Piggin if (unlikely(!va)) 1145db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 1146db64fe02SNick Piggin 11477f88f88fSCatalin Marinas /* 11487f88f88fSCatalin Marinas * Only scan the relevant parts containing pointers to other objects 11497f88f88fSCatalin Marinas * to avoid false negatives. 11507f88f88fSCatalin Marinas */ 1151f07116d7SUladzislau Rezki (Sony) kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask); 11527f88f88fSCatalin Marinas 1153db64fe02SNick Piggin retry: 115482dd23e8SUladzislau Rezki (Sony) /* 115581f1ba58SUladzislau Rezki (Sony) * Preload this CPU with one extra vmap_area object. It is used 115681f1ba58SUladzislau Rezki (Sony) * when fit type of free area is NE_FIT_TYPE. Please note, it 115781f1ba58SUladzislau Rezki (Sony) * does not guarantee that an allocation occurs on a CPU that 115881f1ba58SUladzislau Rezki (Sony) * is preloaded, instead we minimize the case when it is not. 115981f1ba58SUladzislau Rezki (Sony) * It can happen because of cpu migration, because there is a 116081f1ba58SUladzislau Rezki (Sony) * race until the below spinlock is taken. 116182dd23e8SUladzislau Rezki (Sony) * 116282dd23e8SUladzislau Rezki (Sony) * The preload is done in non-atomic context, thus it allows us 116382dd23e8SUladzislau Rezki (Sony) * to use more permissive allocation masks to be more stable under 116481f1ba58SUladzislau Rezki (Sony) * low memory condition and high memory pressure. In rare case, 116581f1ba58SUladzislau Rezki (Sony) * if not preloaded, GFP_NOWAIT is used. 116682dd23e8SUladzislau Rezki (Sony) * 116781f1ba58SUladzislau Rezki (Sony) * Set "pva" to NULL here, because of "retry" path. 116882dd23e8SUladzislau Rezki (Sony) */ 116981f1ba58SUladzislau Rezki (Sony) pva = NULL; 117082dd23e8SUladzislau Rezki (Sony) 117181f1ba58SUladzislau Rezki (Sony) if (!this_cpu_read(ne_fit_preload_node)) 117281f1ba58SUladzislau Rezki (Sony) /* 117381f1ba58SUladzislau Rezki (Sony) * Even if it fails we do not really care about that. 117481f1ba58SUladzislau Rezki (Sony) * Just proceed as it is. If needed "overflow" path 117581f1ba58SUladzislau Rezki (Sony) * will refill the cache we allocate from. 117681f1ba58SUladzislau Rezki (Sony) */ 1177f07116d7SUladzislau Rezki (Sony) pva = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); 117882dd23e8SUladzislau Rezki (Sony) 1179e36176beSUladzislau Rezki (Sony) spin_lock(&free_vmap_area_lock); 118081f1ba58SUladzislau Rezki (Sony) 118181f1ba58SUladzislau Rezki (Sony) if (pva && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, pva)) 118281f1ba58SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, pva); 118368ad4a33SUladzislau Rezki (Sony) 118489699605SNick Piggin /* 118568ad4a33SUladzislau Rezki (Sony) * If an allocation fails, the "vend" address is 118668ad4a33SUladzislau Rezki (Sony) * returned. Therefore trigger the overflow path. 118789699605SNick Piggin */ 1188cacca6baSUladzislau Rezki (Sony) addr = __alloc_vmap_area(size, align, vstart, vend); 1189e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 1190e36176beSUladzislau Rezki (Sony) 119168ad4a33SUladzislau Rezki (Sony) if (unlikely(addr == vend)) 119289699605SNick Piggin goto overflow; 119389699605SNick Piggin 119489699605SNick Piggin va->va_start = addr; 119589699605SNick Piggin va->va_end = addr + size; 1196688fcbfcSPengfei Li va->vm = NULL; 119768ad4a33SUladzislau Rezki (Sony) 1198d98c9e83SAndrey Ryabinin 1199e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 1200e36176beSUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 120189699605SNick Piggin spin_unlock(&vmap_area_lock); 120289699605SNick Piggin 120361e16557SWang Xiaoqiang BUG_ON(!IS_ALIGNED(va->va_start, align)); 120489699605SNick Piggin BUG_ON(va->va_start < vstart); 120589699605SNick Piggin BUG_ON(va->va_end > vend); 120689699605SNick Piggin 1207d98c9e83SAndrey Ryabinin ret = kasan_populate_vmalloc(addr, size); 1208d98c9e83SAndrey Ryabinin if (ret) { 1209d98c9e83SAndrey Ryabinin free_vmap_area(va); 1210d98c9e83SAndrey Ryabinin return ERR_PTR(ret); 1211d98c9e83SAndrey Ryabinin } 1212d98c9e83SAndrey Ryabinin 121389699605SNick Piggin return va; 121489699605SNick Piggin 12157766970cSNick Piggin overflow: 1216db64fe02SNick Piggin if (!purged) { 1217db64fe02SNick Piggin purge_vmap_area_lazy(); 1218db64fe02SNick Piggin purged = 1; 1219db64fe02SNick Piggin goto retry; 1220db64fe02SNick Piggin } 12214da56b99SChris Wilson 12224da56b99SChris Wilson if (gfpflags_allow_blocking(gfp_mask)) { 12234da56b99SChris Wilson unsigned long freed = 0; 12244da56b99SChris Wilson blocking_notifier_call_chain(&vmap_notify_list, 0, &freed); 12254da56b99SChris Wilson if (freed > 0) { 12264da56b99SChris Wilson purged = 0; 12274da56b99SChris Wilson goto retry; 12284da56b99SChris Wilson } 12294da56b99SChris Wilson } 12304da56b99SChris Wilson 123103497d76SFlorian Fainelli if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) 1232756a025fSJoe Perches pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n", 1233756a025fSJoe Perches size); 123468ad4a33SUladzislau Rezki (Sony) 123568ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 1236db64fe02SNick Piggin return ERR_PTR(-EBUSY); 1237db64fe02SNick Piggin } 1238db64fe02SNick Piggin 12394da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb) 12404da56b99SChris Wilson { 12414da56b99SChris Wilson return blocking_notifier_chain_register(&vmap_notify_list, nb); 12424da56b99SChris Wilson } 12434da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier); 12444da56b99SChris Wilson 12454da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb) 12464da56b99SChris Wilson { 12474da56b99SChris Wilson return blocking_notifier_chain_unregister(&vmap_notify_list, nb); 12484da56b99SChris Wilson } 12494da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier); 12504da56b99SChris Wilson 1251db64fe02SNick Piggin /* 1252db64fe02SNick Piggin * lazy_max_pages is the maximum amount of virtual address space we gather up 1253db64fe02SNick Piggin * before attempting to purge with a TLB flush. 1254db64fe02SNick Piggin * 1255db64fe02SNick Piggin * There is a tradeoff here: a larger number will cover more kernel page tables 1256db64fe02SNick Piggin * and take slightly longer to purge, but it will linearly reduce the number of 1257db64fe02SNick Piggin * global TLB flushes that must be performed. It would seem natural to scale 1258db64fe02SNick Piggin * this number up linearly with the number of CPUs (because vmapping activity 1259db64fe02SNick Piggin * could also scale linearly with the number of CPUs), however it is likely 1260db64fe02SNick Piggin * that in practice, workloads might be constrained in other ways that mean 1261db64fe02SNick Piggin * vmap activity will not scale linearly with CPUs. Also, I want to be 1262db64fe02SNick Piggin * conservative and not introduce a big latency on huge systems, so go with 1263db64fe02SNick Piggin * a less aggressive log scale. It will still be an improvement over the old 1264db64fe02SNick Piggin * code, and it will be simple to change the scale factor if we find that it 1265db64fe02SNick Piggin * becomes a problem on bigger systems. 1266db64fe02SNick Piggin */ 1267db64fe02SNick Piggin static unsigned long lazy_max_pages(void) 1268db64fe02SNick Piggin { 1269db64fe02SNick Piggin unsigned int log; 1270db64fe02SNick Piggin 1271db64fe02SNick Piggin log = fls(num_online_cpus()); 1272db64fe02SNick Piggin 1273db64fe02SNick Piggin return log * (32UL * 1024 * 1024 / PAGE_SIZE); 1274db64fe02SNick Piggin } 1275db64fe02SNick Piggin 12764d36e6f8SUladzislau Rezki (Sony) static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0); 1277db64fe02SNick Piggin 12780574ecd1SChristoph Hellwig /* 12790574ecd1SChristoph Hellwig * Serialize vmap purging. There is no actual criticial section protected 12800574ecd1SChristoph Hellwig * by this look, but we want to avoid concurrent calls for performance 12810574ecd1SChristoph Hellwig * reasons and to make the pcpu_get_vm_areas more deterministic. 12820574ecd1SChristoph Hellwig */ 1283f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock); 12840574ecd1SChristoph Hellwig 128502b709dfSNick Piggin /* for per-CPU blocks */ 128602b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void); 128702b709dfSNick Piggin 1288db64fe02SNick Piggin /* 12893ee48b6aSCliff Wickman * called before a call to iounmap() if the caller wants vm_area_struct's 12903ee48b6aSCliff Wickman * immediately freed. 12913ee48b6aSCliff Wickman */ 12923ee48b6aSCliff Wickman void set_iounmap_nonlazy(void) 12933ee48b6aSCliff Wickman { 12944d36e6f8SUladzislau Rezki (Sony) atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1); 12953ee48b6aSCliff Wickman } 12963ee48b6aSCliff Wickman 12973ee48b6aSCliff Wickman /* 1298db64fe02SNick Piggin * Purges all lazily-freed vmap areas. 1299db64fe02SNick Piggin */ 13000574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end) 1301db64fe02SNick Piggin { 13024d36e6f8SUladzislau Rezki (Sony) unsigned long resched_threshold; 130380c4bd7aSChris Wilson struct llist_node *valist; 1304db64fe02SNick Piggin struct vmap_area *va; 1305cbb76676SVegard Nossum struct vmap_area *n_va; 1306db64fe02SNick Piggin 13070574ecd1SChristoph Hellwig lockdep_assert_held(&vmap_purge_lock); 130802b709dfSNick Piggin 130980c4bd7aSChris Wilson valist = llist_del_all(&vmap_purge_list); 131068571be9SUladzislau Rezki (Sony) if (unlikely(valist == NULL)) 131168571be9SUladzislau Rezki (Sony) return false; 131268571be9SUladzislau Rezki (Sony) 131368571be9SUladzislau Rezki (Sony) /* 13143f8fd02bSJoerg Roedel * First make sure the mappings are removed from all page-tables 13153f8fd02bSJoerg Roedel * before they are freed. 13163f8fd02bSJoerg Roedel */ 1317763802b5SJoerg Roedel vmalloc_sync_unmappings(); 13183f8fd02bSJoerg Roedel 13193f8fd02bSJoerg Roedel /* 132068571be9SUladzislau Rezki (Sony) * TODO: to calculate a flush range without looping. 132168571be9SUladzislau Rezki (Sony) * The list can be up to lazy_max_pages() elements. 132268571be9SUladzislau Rezki (Sony) */ 132380c4bd7aSChris Wilson llist_for_each_entry(va, valist, purge_list) { 13240574ecd1SChristoph Hellwig if (va->va_start < start) 13250574ecd1SChristoph Hellwig start = va->va_start; 13260574ecd1SChristoph Hellwig if (va->va_end > end) 13270574ecd1SChristoph Hellwig end = va->va_end; 1328db64fe02SNick Piggin } 1329db64fe02SNick Piggin 13300574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 13314d36e6f8SUladzislau Rezki (Sony) resched_threshold = lazy_max_pages() << 1; 1332db64fe02SNick Piggin 1333e36176beSUladzislau Rezki (Sony) spin_lock(&free_vmap_area_lock); 1334763b218dSJoel Fernandes llist_for_each_entry_safe(va, n_va, valist, purge_list) { 13354d36e6f8SUladzislau Rezki (Sony) unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT; 13363c5c3cfbSDaniel Axtens unsigned long orig_start = va->va_start; 13373c5c3cfbSDaniel Axtens unsigned long orig_end = va->va_end; 1338763b218dSJoel Fernandes 1339dd3b8353SUladzislau Rezki (Sony) /* 1340dd3b8353SUladzislau Rezki (Sony) * Finally insert or merge lazily-freed area. It is 1341dd3b8353SUladzislau Rezki (Sony) * detached and there is no need to "unlink" it from 1342dd3b8353SUladzislau Rezki (Sony) * anything. 1343dd3b8353SUladzislau Rezki (Sony) */ 13443c5c3cfbSDaniel Axtens va = merge_or_add_vmap_area(va, &free_vmap_area_root, 13453c5c3cfbSDaniel Axtens &free_vmap_area_list); 13463c5c3cfbSDaniel Axtens 13473c5c3cfbSDaniel Axtens if (is_vmalloc_or_module_addr((void *)orig_start)) 13483c5c3cfbSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 13493c5c3cfbSDaniel Axtens va->va_start, va->va_end); 1350dd3b8353SUladzislau Rezki (Sony) 13514d36e6f8SUladzislau Rezki (Sony) atomic_long_sub(nr, &vmap_lazy_nr); 135268571be9SUladzislau Rezki (Sony) 13534d36e6f8SUladzislau Rezki (Sony) if (atomic_long_read(&vmap_lazy_nr) < resched_threshold) 1354e36176beSUladzislau Rezki (Sony) cond_resched_lock(&free_vmap_area_lock); 1355763b218dSJoel Fernandes } 1356e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 13570574ecd1SChristoph Hellwig return true; 1358db64fe02SNick Piggin } 1359db64fe02SNick Piggin 1360db64fe02SNick Piggin /* 1361496850e5SNick Piggin * Kick off a purge of the outstanding lazy areas. Don't bother if somebody 1362496850e5SNick Piggin * is already purging. 1363496850e5SNick Piggin */ 1364496850e5SNick Piggin static void try_purge_vmap_area_lazy(void) 1365496850e5SNick Piggin { 1366f9e09977SChristoph Hellwig if (mutex_trylock(&vmap_purge_lock)) { 13670574ecd1SChristoph Hellwig __purge_vmap_area_lazy(ULONG_MAX, 0); 1368f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 13690574ecd1SChristoph Hellwig } 1370496850e5SNick Piggin } 1371496850e5SNick Piggin 1372496850e5SNick Piggin /* 1373db64fe02SNick Piggin * Kick off a purge of the outstanding lazy areas. 1374db64fe02SNick Piggin */ 1375db64fe02SNick Piggin static void purge_vmap_area_lazy(void) 1376db64fe02SNick Piggin { 1377f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 13780574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 13790574ecd1SChristoph Hellwig __purge_vmap_area_lazy(ULONG_MAX, 0); 1380f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 1381db64fe02SNick Piggin } 1382db64fe02SNick Piggin 1383db64fe02SNick Piggin /* 138464141da5SJeremy Fitzhardinge * Free a vmap area, caller ensuring that the area has been unmapped 138564141da5SJeremy Fitzhardinge * and flush_cache_vunmap had been called for the correct range 138664141da5SJeremy Fitzhardinge * previously. 1387db64fe02SNick Piggin */ 138864141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va) 1389db64fe02SNick Piggin { 13904d36e6f8SUladzislau Rezki (Sony) unsigned long nr_lazy; 139180c4bd7aSChris Wilson 1392dd3b8353SUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 1393dd3b8353SUladzislau Rezki (Sony) unlink_va(va, &vmap_area_root); 1394dd3b8353SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 1395dd3b8353SUladzislau Rezki (Sony) 13964d36e6f8SUladzislau Rezki (Sony) nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >> 13974d36e6f8SUladzislau Rezki (Sony) PAGE_SHIFT, &vmap_lazy_nr); 139880c4bd7aSChris Wilson 139980c4bd7aSChris Wilson /* After this point, we may free va at any time */ 140080c4bd7aSChris Wilson llist_add(&va->purge_list, &vmap_purge_list); 140180c4bd7aSChris Wilson 140280c4bd7aSChris Wilson if (unlikely(nr_lazy > lazy_max_pages())) 1403496850e5SNick Piggin try_purge_vmap_area_lazy(); 1404db64fe02SNick Piggin } 1405db64fe02SNick Piggin 1406b29acbdcSNick Piggin /* 1407b29acbdcSNick Piggin * Free and unmap a vmap area 1408b29acbdcSNick Piggin */ 1409b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va) 1410b29acbdcSNick Piggin { 1411b29acbdcSNick Piggin flush_cache_vunmap(va->va_start, va->va_end); 1412855e57a1SChristoph Hellwig unmap_kernel_range_noflush(va->va_start, va->va_end - va->va_start); 14138e57f8acSVlastimil Babka if (debug_pagealloc_enabled_static()) 141482a2e924SChintan Pandya flush_tlb_kernel_range(va->va_start, va->va_end); 141582a2e924SChintan Pandya 1416c8eef01eSChristoph Hellwig free_vmap_area_noflush(va); 1417b29acbdcSNick Piggin } 1418b29acbdcSNick Piggin 1419db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr) 1420db64fe02SNick Piggin { 1421db64fe02SNick Piggin struct vmap_area *va; 1422db64fe02SNick Piggin 1423db64fe02SNick Piggin spin_lock(&vmap_area_lock); 1424db64fe02SNick Piggin va = __find_vmap_area(addr); 1425db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 1426db64fe02SNick Piggin 1427db64fe02SNick Piggin return va; 1428db64fe02SNick Piggin } 1429db64fe02SNick Piggin 1430db64fe02SNick Piggin /*** Per cpu kva allocator ***/ 1431db64fe02SNick Piggin 1432db64fe02SNick Piggin /* 1433db64fe02SNick Piggin * vmap space is limited especially on 32 bit architectures. Ensure there is 1434db64fe02SNick Piggin * room for at least 16 percpu vmap blocks per CPU. 1435db64fe02SNick Piggin */ 1436db64fe02SNick Piggin /* 1437db64fe02SNick Piggin * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able 1438db64fe02SNick Piggin * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess 1439db64fe02SNick Piggin * instead (we just need a rough idea) 1440db64fe02SNick Piggin */ 1441db64fe02SNick Piggin #if BITS_PER_LONG == 32 1442db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024) 1443db64fe02SNick Piggin #else 1444db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024*1024) 1445db64fe02SNick Piggin #endif 1446db64fe02SNick Piggin 1447db64fe02SNick Piggin #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) 1448db64fe02SNick Piggin #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ 1449db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ 1450db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) 1451db64fe02SNick Piggin #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ 1452db64fe02SNick Piggin #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ 1453f982f915SClemens Ladisch #define VMAP_BBMAP_BITS \ 1454f982f915SClemens Ladisch VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ 1455db64fe02SNick Piggin VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ 1456f982f915SClemens Ladisch VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16)) 1457db64fe02SNick Piggin 1458db64fe02SNick Piggin #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) 1459db64fe02SNick Piggin 1460db64fe02SNick Piggin struct vmap_block_queue { 1461db64fe02SNick Piggin spinlock_t lock; 1462db64fe02SNick Piggin struct list_head free; 1463db64fe02SNick Piggin }; 1464db64fe02SNick Piggin 1465db64fe02SNick Piggin struct vmap_block { 1466db64fe02SNick Piggin spinlock_t lock; 1467db64fe02SNick Piggin struct vmap_area *va; 1468db64fe02SNick Piggin unsigned long free, dirty; 14697d61bfe8SRoman Pen unsigned long dirty_min, dirty_max; /*< dirty range */ 1470db64fe02SNick Piggin struct list_head free_list; 1471db64fe02SNick Piggin struct rcu_head rcu_head; 147202b709dfSNick Piggin struct list_head purge; 1473db64fe02SNick Piggin }; 1474db64fe02SNick Piggin 1475db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ 1476db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); 1477db64fe02SNick Piggin 1478db64fe02SNick Piggin /* 1479db64fe02SNick Piggin * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block 1480db64fe02SNick Piggin * in the free path. Could get rid of this if we change the API to return a 1481db64fe02SNick Piggin * "cookie" from alloc, to be passed to free. But no big deal yet. 1482db64fe02SNick Piggin */ 1483db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_block_tree_lock); 1484db64fe02SNick Piggin static RADIX_TREE(vmap_block_tree, GFP_ATOMIC); 1485db64fe02SNick Piggin 1486db64fe02SNick Piggin /* 1487db64fe02SNick Piggin * We should probably have a fallback mechanism to allocate virtual memory 1488db64fe02SNick Piggin * out of partially filled vmap blocks. However vmap block sizing should be 1489db64fe02SNick Piggin * fairly reasonable according to the vmalloc size, so it shouldn't be a 1490db64fe02SNick Piggin * big problem. 1491db64fe02SNick Piggin */ 1492db64fe02SNick Piggin 1493db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr) 1494db64fe02SNick Piggin { 1495db64fe02SNick Piggin addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); 1496db64fe02SNick Piggin addr /= VMAP_BLOCK_SIZE; 1497db64fe02SNick Piggin return addr; 1498db64fe02SNick Piggin } 1499db64fe02SNick Piggin 1500cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off) 1501cf725ce2SRoman Pen { 1502cf725ce2SRoman Pen unsigned long addr; 1503cf725ce2SRoman Pen 1504cf725ce2SRoman Pen addr = va_start + (pages_off << PAGE_SHIFT); 1505cf725ce2SRoman Pen BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start)); 1506cf725ce2SRoman Pen return (void *)addr; 1507cf725ce2SRoman Pen } 1508cf725ce2SRoman Pen 1509cf725ce2SRoman Pen /** 1510cf725ce2SRoman Pen * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this 1511cf725ce2SRoman Pen * block. Of course pages number can't exceed VMAP_BBMAP_BITS 1512cf725ce2SRoman Pen * @order: how many 2^order pages should be occupied in newly allocated block 1513cf725ce2SRoman Pen * @gfp_mask: flags for the page level allocator 1514cf725ce2SRoman Pen * 1515a862f68aSMike Rapoport * Return: virtual address in a newly allocated block or ERR_PTR(-errno) 1516cf725ce2SRoman Pen */ 1517cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) 1518db64fe02SNick Piggin { 1519db64fe02SNick Piggin struct vmap_block_queue *vbq; 1520db64fe02SNick Piggin struct vmap_block *vb; 1521db64fe02SNick Piggin struct vmap_area *va; 1522db64fe02SNick Piggin unsigned long vb_idx; 1523db64fe02SNick Piggin int node, err; 1524cf725ce2SRoman Pen void *vaddr; 1525db64fe02SNick Piggin 1526db64fe02SNick Piggin node = numa_node_id(); 1527db64fe02SNick Piggin 1528db64fe02SNick Piggin vb = kmalloc_node(sizeof(struct vmap_block), 1529db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 1530db64fe02SNick Piggin if (unlikely(!vb)) 1531db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 1532db64fe02SNick Piggin 1533db64fe02SNick Piggin va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, 1534db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, 1535db64fe02SNick Piggin node, gfp_mask); 1536ddf9c6d4STobias Klauser if (IS_ERR(va)) { 1537db64fe02SNick Piggin kfree(vb); 1538e7d86340SJulia Lawall return ERR_CAST(va); 1539db64fe02SNick Piggin } 1540db64fe02SNick Piggin 1541db64fe02SNick Piggin err = radix_tree_preload(gfp_mask); 1542db64fe02SNick Piggin if (unlikely(err)) { 1543db64fe02SNick Piggin kfree(vb); 1544db64fe02SNick Piggin free_vmap_area(va); 1545db64fe02SNick Piggin return ERR_PTR(err); 1546db64fe02SNick Piggin } 1547db64fe02SNick Piggin 1548cf725ce2SRoman Pen vaddr = vmap_block_vaddr(va->va_start, 0); 1549db64fe02SNick Piggin spin_lock_init(&vb->lock); 1550db64fe02SNick Piggin vb->va = va; 1551cf725ce2SRoman Pen /* At least something should be left free */ 1552cf725ce2SRoman Pen BUG_ON(VMAP_BBMAP_BITS <= (1UL << order)); 1553cf725ce2SRoman Pen vb->free = VMAP_BBMAP_BITS - (1UL << order); 1554db64fe02SNick Piggin vb->dirty = 0; 15557d61bfe8SRoman Pen vb->dirty_min = VMAP_BBMAP_BITS; 15567d61bfe8SRoman Pen vb->dirty_max = 0; 1557db64fe02SNick Piggin INIT_LIST_HEAD(&vb->free_list); 1558db64fe02SNick Piggin 1559db64fe02SNick Piggin vb_idx = addr_to_vb_idx(va->va_start); 1560db64fe02SNick Piggin spin_lock(&vmap_block_tree_lock); 1561db64fe02SNick Piggin err = radix_tree_insert(&vmap_block_tree, vb_idx, vb); 1562db64fe02SNick Piggin spin_unlock(&vmap_block_tree_lock); 1563db64fe02SNick Piggin BUG_ON(err); 1564db64fe02SNick Piggin radix_tree_preload_end(); 1565db64fe02SNick Piggin 1566db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 1567db64fe02SNick Piggin spin_lock(&vbq->lock); 156868ac546fSRoman Pen list_add_tail_rcu(&vb->free_list, &vbq->free); 1569db64fe02SNick Piggin spin_unlock(&vbq->lock); 15703f04ba85STejun Heo put_cpu_var(vmap_block_queue); 1571db64fe02SNick Piggin 1572cf725ce2SRoman Pen return vaddr; 1573db64fe02SNick Piggin } 1574db64fe02SNick Piggin 1575db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb) 1576db64fe02SNick Piggin { 1577db64fe02SNick Piggin struct vmap_block *tmp; 1578db64fe02SNick Piggin unsigned long vb_idx; 1579db64fe02SNick Piggin 1580db64fe02SNick Piggin vb_idx = addr_to_vb_idx(vb->va->va_start); 1581db64fe02SNick Piggin spin_lock(&vmap_block_tree_lock); 1582db64fe02SNick Piggin tmp = radix_tree_delete(&vmap_block_tree, vb_idx); 1583db64fe02SNick Piggin spin_unlock(&vmap_block_tree_lock); 1584db64fe02SNick Piggin BUG_ON(tmp != vb); 1585db64fe02SNick Piggin 158664141da5SJeremy Fitzhardinge free_vmap_area_noflush(vb->va); 158722a3c7d1SLai Jiangshan kfree_rcu(vb, rcu_head); 1588db64fe02SNick Piggin } 1589db64fe02SNick Piggin 159002b709dfSNick Piggin static void purge_fragmented_blocks(int cpu) 159102b709dfSNick Piggin { 159202b709dfSNick Piggin LIST_HEAD(purge); 159302b709dfSNick Piggin struct vmap_block *vb; 159402b709dfSNick Piggin struct vmap_block *n_vb; 159502b709dfSNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 159602b709dfSNick Piggin 159702b709dfSNick Piggin rcu_read_lock(); 159802b709dfSNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 159902b709dfSNick Piggin 160002b709dfSNick Piggin if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS)) 160102b709dfSNick Piggin continue; 160202b709dfSNick Piggin 160302b709dfSNick Piggin spin_lock(&vb->lock); 160402b709dfSNick Piggin if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) { 160502b709dfSNick Piggin vb->free = 0; /* prevent further allocs after releasing lock */ 160602b709dfSNick Piggin vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */ 16077d61bfe8SRoman Pen vb->dirty_min = 0; 16087d61bfe8SRoman Pen vb->dirty_max = VMAP_BBMAP_BITS; 160902b709dfSNick Piggin spin_lock(&vbq->lock); 161002b709dfSNick Piggin list_del_rcu(&vb->free_list); 161102b709dfSNick Piggin spin_unlock(&vbq->lock); 161202b709dfSNick Piggin spin_unlock(&vb->lock); 161302b709dfSNick Piggin list_add_tail(&vb->purge, &purge); 161402b709dfSNick Piggin } else 161502b709dfSNick Piggin spin_unlock(&vb->lock); 161602b709dfSNick Piggin } 161702b709dfSNick Piggin rcu_read_unlock(); 161802b709dfSNick Piggin 161902b709dfSNick Piggin list_for_each_entry_safe(vb, n_vb, &purge, purge) { 162002b709dfSNick Piggin list_del(&vb->purge); 162102b709dfSNick Piggin free_vmap_block(vb); 162202b709dfSNick Piggin } 162302b709dfSNick Piggin } 162402b709dfSNick Piggin 162502b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void) 162602b709dfSNick Piggin { 162702b709dfSNick Piggin int cpu; 162802b709dfSNick Piggin 162902b709dfSNick Piggin for_each_possible_cpu(cpu) 163002b709dfSNick Piggin purge_fragmented_blocks(cpu); 163102b709dfSNick Piggin } 163202b709dfSNick Piggin 1633db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask) 1634db64fe02SNick Piggin { 1635db64fe02SNick Piggin struct vmap_block_queue *vbq; 1636db64fe02SNick Piggin struct vmap_block *vb; 1637cf725ce2SRoman Pen void *vaddr = NULL; 1638db64fe02SNick Piggin unsigned int order; 1639db64fe02SNick Piggin 1640891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 1641db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 1642aa91c4d8SJan Kara if (WARN_ON(size == 0)) { 1643aa91c4d8SJan Kara /* 1644aa91c4d8SJan Kara * Allocating 0 bytes isn't what caller wants since 1645aa91c4d8SJan Kara * get_order(0) returns funny result. Just warn and terminate 1646aa91c4d8SJan Kara * early. 1647aa91c4d8SJan Kara */ 1648aa91c4d8SJan Kara return NULL; 1649aa91c4d8SJan Kara } 1650db64fe02SNick Piggin order = get_order(size); 1651db64fe02SNick Piggin 1652db64fe02SNick Piggin rcu_read_lock(); 1653db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 1654db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 1655cf725ce2SRoman Pen unsigned long pages_off; 1656db64fe02SNick Piggin 1657db64fe02SNick Piggin spin_lock(&vb->lock); 1658cf725ce2SRoman Pen if (vb->free < (1UL << order)) { 1659cf725ce2SRoman Pen spin_unlock(&vb->lock); 1660cf725ce2SRoman Pen continue; 1661cf725ce2SRoman Pen } 166202b709dfSNick Piggin 1663cf725ce2SRoman Pen pages_off = VMAP_BBMAP_BITS - vb->free; 1664cf725ce2SRoman Pen vaddr = vmap_block_vaddr(vb->va->va_start, pages_off); 1665db64fe02SNick Piggin vb->free -= 1UL << order; 1666db64fe02SNick Piggin if (vb->free == 0) { 1667db64fe02SNick Piggin spin_lock(&vbq->lock); 1668de560423SNick Piggin list_del_rcu(&vb->free_list); 1669db64fe02SNick Piggin spin_unlock(&vbq->lock); 1670db64fe02SNick Piggin } 1671cf725ce2SRoman Pen 1672db64fe02SNick Piggin spin_unlock(&vb->lock); 1673db64fe02SNick Piggin break; 1674db64fe02SNick Piggin } 167502b709dfSNick Piggin 16763f04ba85STejun Heo put_cpu_var(vmap_block_queue); 1677db64fe02SNick Piggin rcu_read_unlock(); 1678db64fe02SNick Piggin 1679cf725ce2SRoman Pen /* Allocate new block if nothing was found */ 1680cf725ce2SRoman Pen if (!vaddr) 1681cf725ce2SRoman Pen vaddr = new_vmap_block(order, gfp_mask); 1682db64fe02SNick Piggin 1683cf725ce2SRoman Pen return vaddr; 1684db64fe02SNick Piggin } 1685db64fe02SNick Piggin 168678a0e8c4SChristoph Hellwig static void vb_free(unsigned long addr, unsigned long size) 1687db64fe02SNick Piggin { 1688db64fe02SNick Piggin unsigned long offset; 1689db64fe02SNick Piggin unsigned long vb_idx; 1690db64fe02SNick Piggin unsigned int order; 1691db64fe02SNick Piggin struct vmap_block *vb; 1692db64fe02SNick Piggin 1693891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 1694db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 1695b29acbdcSNick Piggin 169678a0e8c4SChristoph Hellwig flush_cache_vunmap(addr, addr + size); 1697b29acbdcSNick Piggin 1698db64fe02SNick Piggin order = get_order(size); 1699db64fe02SNick Piggin 170078a0e8c4SChristoph Hellwig offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT; 1701db64fe02SNick Piggin 170278a0e8c4SChristoph Hellwig vb_idx = addr_to_vb_idx(addr); 1703db64fe02SNick Piggin rcu_read_lock(); 1704db64fe02SNick Piggin vb = radix_tree_lookup(&vmap_block_tree, vb_idx); 1705db64fe02SNick Piggin rcu_read_unlock(); 1706db64fe02SNick Piggin BUG_ON(!vb); 1707db64fe02SNick Piggin 1708b521c43fSChristoph Hellwig unmap_kernel_range_noflush(addr, size); 170964141da5SJeremy Fitzhardinge 17108e57f8acSVlastimil Babka if (debug_pagealloc_enabled_static()) 171178a0e8c4SChristoph Hellwig flush_tlb_kernel_range(addr, addr + size); 171282a2e924SChintan Pandya 1713db64fe02SNick Piggin spin_lock(&vb->lock); 17147d61bfe8SRoman Pen 17157d61bfe8SRoman Pen /* Expand dirty range */ 17167d61bfe8SRoman Pen vb->dirty_min = min(vb->dirty_min, offset); 17177d61bfe8SRoman Pen vb->dirty_max = max(vb->dirty_max, offset + (1UL << order)); 1718d086817dSMinChan Kim 1719db64fe02SNick Piggin vb->dirty += 1UL << order; 1720db64fe02SNick Piggin if (vb->dirty == VMAP_BBMAP_BITS) { 1721de560423SNick Piggin BUG_ON(vb->free); 1722db64fe02SNick Piggin spin_unlock(&vb->lock); 1723db64fe02SNick Piggin free_vmap_block(vb); 1724db64fe02SNick Piggin } else 1725db64fe02SNick Piggin spin_unlock(&vb->lock); 1726db64fe02SNick Piggin } 1727db64fe02SNick Piggin 1728868b104dSRick Edgecombe static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush) 1729db64fe02SNick Piggin { 1730db64fe02SNick Piggin int cpu; 1731db64fe02SNick Piggin 17329b463334SJeremy Fitzhardinge if (unlikely(!vmap_initialized)) 17339b463334SJeremy Fitzhardinge return; 17349b463334SJeremy Fitzhardinge 17355803ed29SChristoph Hellwig might_sleep(); 17365803ed29SChristoph Hellwig 1737db64fe02SNick Piggin for_each_possible_cpu(cpu) { 1738db64fe02SNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 1739db64fe02SNick Piggin struct vmap_block *vb; 1740db64fe02SNick Piggin 1741db64fe02SNick Piggin rcu_read_lock(); 1742db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 1743db64fe02SNick Piggin spin_lock(&vb->lock); 17447d61bfe8SRoman Pen if (vb->dirty) { 17457d61bfe8SRoman Pen unsigned long va_start = vb->va->va_start; 1746db64fe02SNick Piggin unsigned long s, e; 1747b136be5eSJoonsoo Kim 17487d61bfe8SRoman Pen s = va_start + (vb->dirty_min << PAGE_SHIFT); 17497d61bfe8SRoman Pen e = va_start + (vb->dirty_max << PAGE_SHIFT); 1750db64fe02SNick Piggin 17517d61bfe8SRoman Pen start = min(s, start); 17527d61bfe8SRoman Pen end = max(e, end); 17537d61bfe8SRoman Pen 1754db64fe02SNick Piggin flush = 1; 1755db64fe02SNick Piggin } 1756db64fe02SNick Piggin spin_unlock(&vb->lock); 1757db64fe02SNick Piggin } 1758db64fe02SNick Piggin rcu_read_unlock(); 1759db64fe02SNick Piggin } 1760db64fe02SNick Piggin 1761f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 17620574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 17630574ecd1SChristoph Hellwig if (!__purge_vmap_area_lazy(start, end) && flush) 17640574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 1765f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 1766db64fe02SNick Piggin } 1767868b104dSRick Edgecombe 1768868b104dSRick Edgecombe /** 1769868b104dSRick Edgecombe * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer 1770868b104dSRick Edgecombe * 1771868b104dSRick Edgecombe * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily 1772868b104dSRick Edgecombe * to amortize TLB flushing overheads. What this means is that any page you 1773868b104dSRick Edgecombe * have now, may, in a former life, have been mapped into kernel virtual 1774868b104dSRick Edgecombe * address by the vmap layer and so there might be some CPUs with TLB entries 1775868b104dSRick Edgecombe * still referencing that page (additional to the regular 1:1 kernel mapping). 1776868b104dSRick Edgecombe * 1777868b104dSRick Edgecombe * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can 1778868b104dSRick Edgecombe * be sure that none of the pages we have control over will have any aliases 1779868b104dSRick Edgecombe * from the vmap layer. 1780868b104dSRick Edgecombe */ 1781868b104dSRick Edgecombe void vm_unmap_aliases(void) 1782868b104dSRick Edgecombe { 1783868b104dSRick Edgecombe unsigned long start = ULONG_MAX, end = 0; 1784868b104dSRick Edgecombe int flush = 0; 1785868b104dSRick Edgecombe 1786868b104dSRick Edgecombe _vm_unmap_aliases(start, end, flush); 1787868b104dSRick Edgecombe } 1788db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases); 1789db64fe02SNick Piggin 1790db64fe02SNick Piggin /** 1791db64fe02SNick Piggin * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram 1792db64fe02SNick Piggin * @mem: the pointer returned by vm_map_ram 1793db64fe02SNick Piggin * @count: the count passed to that vm_map_ram call (cannot unmap partial) 1794db64fe02SNick Piggin */ 1795db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count) 1796db64fe02SNick Piggin { 179765ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 1798db64fe02SNick Piggin unsigned long addr = (unsigned long)mem; 17999c3acf60SChristoph Hellwig struct vmap_area *va; 1800db64fe02SNick Piggin 18015803ed29SChristoph Hellwig might_sleep(); 1802db64fe02SNick Piggin BUG_ON(!addr); 1803db64fe02SNick Piggin BUG_ON(addr < VMALLOC_START); 1804db64fe02SNick Piggin BUG_ON(addr > VMALLOC_END); 1805a1c0b1a0SShawn Lin BUG_ON(!PAGE_ALIGNED(addr)); 1806db64fe02SNick Piggin 1807d98c9e83SAndrey Ryabinin kasan_poison_vmalloc(mem, size); 1808d98c9e83SAndrey Ryabinin 18099c3acf60SChristoph Hellwig if (likely(count <= VMAP_MAX_ALLOC)) { 181005e3ff95SChintan Pandya debug_check_no_locks_freed(mem, size); 181178a0e8c4SChristoph Hellwig vb_free(addr, size); 18129c3acf60SChristoph Hellwig return; 18139c3acf60SChristoph Hellwig } 18149c3acf60SChristoph Hellwig 18159c3acf60SChristoph Hellwig va = find_vmap_area(addr); 18169c3acf60SChristoph Hellwig BUG_ON(!va); 181705e3ff95SChintan Pandya debug_check_no_locks_freed((void *)va->va_start, 181805e3ff95SChintan Pandya (va->va_end - va->va_start)); 18199c3acf60SChristoph Hellwig free_unmap_vmap_area(va); 1820db64fe02SNick Piggin } 1821db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram); 1822db64fe02SNick Piggin 1823db64fe02SNick Piggin /** 1824db64fe02SNick Piggin * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) 1825db64fe02SNick Piggin * @pages: an array of pointers to the pages to be mapped 1826db64fe02SNick Piggin * @count: number of pages 1827db64fe02SNick Piggin * @node: prefer to allocate data structures on this node 1828db64fe02SNick Piggin * @prot: memory protection to use. PAGE_KERNEL for regular RAM 1829e99c97adSRandy Dunlap * 183036437638SGioh Kim * If you use this function for less than VMAP_MAX_ALLOC pages, it could be 183136437638SGioh Kim * faster than vmap so it's good. But if you mix long-life and short-life 183236437638SGioh Kim * objects with vm_map_ram(), it could consume lots of address space through 183336437638SGioh Kim * fragmentation (especially on a 32bit machine). You could see failures in 183436437638SGioh Kim * the end. Please use this function for short-lived objects. 183536437638SGioh Kim * 1836e99c97adSRandy Dunlap * Returns: a pointer to the address that has been mapped, or %NULL on failure 1837db64fe02SNick Piggin */ 1838d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node) 1839db64fe02SNick Piggin { 184065ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 1841db64fe02SNick Piggin unsigned long addr; 1842db64fe02SNick Piggin void *mem; 1843db64fe02SNick Piggin 1844db64fe02SNick Piggin if (likely(count <= VMAP_MAX_ALLOC)) { 1845db64fe02SNick Piggin mem = vb_alloc(size, GFP_KERNEL); 1846db64fe02SNick Piggin if (IS_ERR(mem)) 1847db64fe02SNick Piggin return NULL; 1848db64fe02SNick Piggin addr = (unsigned long)mem; 1849db64fe02SNick Piggin } else { 1850db64fe02SNick Piggin struct vmap_area *va; 1851db64fe02SNick Piggin va = alloc_vmap_area(size, PAGE_SIZE, 1852db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); 1853db64fe02SNick Piggin if (IS_ERR(va)) 1854db64fe02SNick Piggin return NULL; 1855db64fe02SNick Piggin 1856db64fe02SNick Piggin addr = va->va_start; 1857db64fe02SNick Piggin mem = (void *)addr; 1858db64fe02SNick Piggin } 1859d98c9e83SAndrey Ryabinin 1860d98c9e83SAndrey Ryabinin kasan_unpoison_vmalloc(mem, size); 1861d98c9e83SAndrey Ryabinin 1862d4efd79aSChristoph Hellwig if (map_kernel_range(addr, size, PAGE_KERNEL, pages) < 0) { 1863db64fe02SNick Piggin vm_unmap_ram(mem, count); 1864db64fe02SNick Piggin return NULL; 1865db64fe02SNick Piggin } 1866db64fe02SNick Piggin return mem; 1867db64fe02SNick Piggin } 1868db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram); 1869db64fe02SNick Piggin 18704341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata; 187192eac168SMike Rapoport 1872f0aa6617STejun Heo /** 1873be9b7335SNicolas Pitre * vm_area_add_early - add vmap area early during boot 1874be9b7335SNicolas Pitre * @vm: vm_struct to add 1875be9b7335SNicolas Pitre * 1876be9b7335SNicolas Pitre * This function is used to add fixed kernel vm area to vmlist before 1877be9b7335SNicolas Pitre * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags 1878be9b7335SNicolas Pitre * should contain proper values and the other fields should be zero. 1879be9b7335SNicolas Pitre * 1880be9b7335SNicolas Pitre * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 1881be9b7335SNicolas Pitre */ 1882be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm) 1883be9b7335SNicolas Pitre { 1884be9b7335SNicolas Pitre struct vm_struct *tmp, **p; 1885be9b7335SNicolas Pitre 1886be9b7335SNicolas Pitre BUG_ON(vmap_initialized); 1887be9b7335SNicolas Pitre for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { 1888be9b7335SNicolas Pitre if (tmp->addr >= vm->addr) { 1889be9b7335SNicolas Pitre BUG_ON(tmp->addr < vm->addr + vm->size); 1890be9b7335SNicolas Pitre break; 1891be9b7335SNicolas Pitre } else 1892be9b7335SNicolas Pitre BUG_ON(tmp->addr + tmp->size > vm->addr); 1893be9b7335SNicolas Pitre } 1894be9b7335SNicolas Pitre vm->next = *p; 1895be9b7335SNicolas Pitre *p = vm; 1896be9b7335SNicolas Pitre } 1897be9b7335SNicolas Pitre 1898be9b7335SNicolas Pitre /** 1899f0aa6617STejun Heo * vm_area_register_early - register vmap area early during boot 1900f0aa6617STejun Heo * @vm: vm_struct to register 1901c0c0a293STejun Heo * @align: requested alignment 1902f0aa6617STejun Heo * 1903f0aa6617STejun Heo * This function is used to register kernel vm area before 1904f0aa6617STejun Heo * vmalloc_init() is called. @vm->size and @vm->flags should contain 1905f0aa6617STejun Heo * proper values on entry and other fields should be zero. On return, 1906f0aa6617STejun Heo * vm->addr contains the allocated address. 1907f0aa6617STejun Heo * 1908f0aa6617STejun Heo * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 1909f0aa6617STejun Heo */ 1910c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align) 1911f0aa6617STejun Heo { 1912f0aa6617STejun Heo static size_t vm_init_off __initdata; 1913c0c0a293STejun Heo unsigned long addr; 1914f0aa6617STejun Heo 1915c0c0a293STejun Heo addr = ALIGN(VMALLOC_START + vm_init_off, align); 1916c0c0a293STejun Heo vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START; 1917c0c0a293STejun Heo 1918c0c0a293STejun Heo vm->addr = (void *)addr; 1919f0aa6617STejun Heo 1920be9b7335SNicolas Pitre vm_area_add_early(vm); 1921f0aa6617STejun Heo } 1922f0aa6617STejun Heo 192368ad4a33SUladzislau Rezki (Sony) static void vmap_init_free_space(void) 192468ad4a33SUladzislau Rezki (Sony) { 192568ad4a33SUladzislau Rezki (Sony) unsigned long vmap_start = 1; 192668ad4a33SUladzislau Rezki (Sony) const unsigned long vmap_end = ULONG_MAX; 192768ad4a33SUladzislau Rezki (Sony) struct vmap_area *busy, *free; 192868ad4a33SUladzislau Rezki (Sony) 192968ad4a33SUladzislau Rezki (Sony) /* 193068ad4a33SUladzislau Rezki (Sony) * B F B B B F 193168ad4a33SUladzislau Rezki (Sony) * -|-----|.....|-----|-----|-----|.....|- 193268ad4a33SUladzislau Rezki (Sony) * | The KVA space | 193368ad4a33SUladzislau Rezki (Sony) * |<--------------------------------->| 193468ad4a33SUladzislau Rezki (Sony) */ 193568ad4a33SUladzislau Rezki (Sony) list_for_each_entry(busy, &vmap_area_list, list) { 193668ad4a33SUladzislau Rezki (Sony) if (busy->va_start - vmap_start > 0) { 193768ad4a33SUladzislau Rezki (Sony) free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 193868ad4a33SUladzislau Rezki (Sony) if (!WARN_ON_ONCE(!free)) { 193968ad4a33SUladzislau Rezki (Sony) free->va_start = vmap_start; 194068ad4a33SUladzislau Rezki (Sony) free->va_end = busy->va_start; 194168ad4a33SUladzislau Rezki (Sony) 194268ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(free, NULL, 194368ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, 194468ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list); 194568ad4a33SUladzislau Rezki (Sony) } 194668ad4a33SUladzislau Rezki (Sony) } 194768ad4a33SUladzislau Rezki (Sony) 194868ad4a33SUladzislau Rezki (Sony) vmap_start = busy->va_end; 194968ad4a33SUladzislau Rezki (Sony) } 195068ad4a33SUladzislau Rezki (Sony) 195168ad4a33SUladzislau Rezki (Sony) if (vmap_end - vmap_start > 0) { 195268ad4a33SUladzislau Rezki (Sony) free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 195368ad4a33SUladzislau Rezki (Sony) if (!WARN_ON_ONCE(!free)) { 195468ad4a33SUladzislau Rezki (Sony) free->va_start = vmap_start; 195568ad4a33SUladzislau Rezki (Sony) free->va_end = vmap_end; 195668ad4a33SUladzislau Rezki (Sony) 195768ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(free, NULL, 195868ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, 195968ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list); 196068ad4a33SUladzislau Rezki (Sony) } 196168ad4a33SUladzislau Rezki (Sony) } 196268ad4a33SUladzislau Rezki (Sony) } 196368ad4a33SUladzislau Rezki (Sony) 1964db64fe02SNick Piggin void __init vmalloc_init(void) 1965db64fe02SNick Piggin { 1966822c18f2SIvan Kokshaysky struct vmap_area *va; 1967822c18f2SIvan Kokshaysky struct vm_struct *tmp; 1968db64fe02SNick Piggin int i; 1969db64fe02SNick Piggin 197068ad4a33SUladzislau Rezki (Sony) /* 197168ad4a33SUladzislau Rezki (Sony) * Create the cache for vmap_area objects. 197268ad4a33SUladzislau Rezki (Sony) */ 197368ad4a33SUladzislau Rezki (Sony) vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC); 197468ad4a33SUladzislau Rezki (Sony) 1975db64fe02SNick Piggin for_each_possible_cpu(i) { 1976db64fe02SNick Piggin struct vmap_block_queue *vbq; 197732fcfd40SAl Viro struct vfree_deferred *p; 1978db64fe02SNick Piggin 1979db64fe02SNick Piggin vbq = &per_cpu(vmap_block_queue, i); 1980db64fe02SNick Piggin spin_lock_init(&vbq->lock); 1981db64fe02SNick Piggin INIT_LIST_HEAD(&vbq->free); 198232fcfd40SAl Viro p = &per_cpu(vfree_deferred, i); 198332fcfd40SAl Viro init_llist_head(&p->list); 198432fcfd40SAl Viro INIT_WORK(&p->wq, free_work); 1985db64fe02SNick Piggin } 19869b463334SJeremy Fitzhardinge 1987822c18f2SIvan Kokshaysky /* Import existing vmlist entries. */ 1988822c18f2SIvan Kokshaysky for (tmp = vmlist; tmp; tmp = tmp->next) { 198968ad4a33SUladzislau Rezki (Sony) va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 199068ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(!va)) 199168ad4a33SUladzislau Rezki (Sony) continue; 199268ad4a33SUladzislau Rezki (Sony) 1993822c18f2SIvan Kokshaysky va->va_start = (unsigned long)tmp->addr; 1994822c18f2SIvan Kokshaysky va->va_end = va->va_start + tmp->size; 1995dbda591dSKyongHo va->vm = tmp; 199668ad4a33SUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 1997822c18f2SIvan Kokshaysky } 1998ca23e405STejun Heo 199968ad4a33SUladzislau Rezki (Sony) /* 200068ad4a33SUladzislau Rezki (Sony) * Now we can initialize a free vmap space. 200168ad4a33SUladzislau Rezki (Sony) */ 200268ad4a33SUladzislau Rezki (Sony) vmap_init_free_space(); 20039b463334SJeremy Fitzhardinge vmap_initialized = true; 2004db64fe02SNick Piggin } 2005db64fe02SNick Piggin 20068fc48985STejun Heo /** 20078fc48985STejun Heo * unmap_kernel_range - unmap kernel VM area and flush cache and TLB 20088fc48985STejun Heo * @addr: start of the VM area to unmap 20098fc48985STejun Heo * @size: size of the VM area to unmap 20108fc48985STejun Heo * 20118fc48985STejun Heo * Similar to unmap_kernel_range_noflush() but flushes vcache before 20128fc48985STejun Heo * the unmapping and tlb after. 20138fc48985STejun Heo */ 2014db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size) 2015db64fe02SNick Piggin { 2016db64fe02SNick Piggin unsigned long end = addr + size; 2017f6fcba70STejun Heo 2018f6fcba70STejun Heo flush_cache_vunmap(addr, end); 2019b521c43fSChristoph Hellwig unmap_kernel_range_noflush(addr, size); 2020db64fe02SNick Piggin flush_tlb_kernel_range(addr, end); 2021db64fe02SNick Piggin } 2022db64fe02SNick Piggin 2023e36176beSUladzislau Rezki (Sony) static inline void setup_vmalloc_vm_locked(struct vm_struct *vm, 2024e36176beSUladzislau Rezki (Sony) struct vmap_area *va, unsigned long flags, const void *caller) 2025cf88c790STejun Heo { 2026cf88c790STejun Heo vm->flags = flags; 2027cf88c790STejun Heo vm->addr = (void *)va->va_start; 2028cf88c790STejun Heo vm->size = va->va_end - va->va_start; 2029cf88c790STejun Heo vm->caller = caller; 2030db1aecafSMinchan Kim va->vm = vm; 2031e36176beSUladzislau Rezki (Sony) } 2032e36176beSUladzislau Rezki (Sony) 2033e36176beSUladzislau Rezki (Sony) static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, 2034e36176beSUladzislau Rezki (Sony) unsigned long flags, const void *caller) 2035e36176beSUladzislau Rezki (Sony) { 2036e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 2037e36176beSUladzislau Rezki (Sony) setup_vmalloc_vm_locked(vm, va, flags, caller); 2038c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 2039f5252e00SMitsuo Hayasaka } 2040cf88c790STejun Heo 204120fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm) 2042f5252e00SMitsuo Hayasaka { 2043d4033afdSJoonsoo Kim /* 204420fc02b4SZhang Yanfei * Before removing VM_UNINITIALIZED, 2045d4033afdSJoonsoo Kim * we should make sure that vm has proper values. 2046d4033afdSJoonsoo Kim * Pair with smp_rmb() in show_numa_info(). 2047d4033afdSJoonsoo Kim */ 2048d4033afdSJoonsoo Kim smp_wmb(); 204920fc02b4SZhang Yanfei vm->flags &= ~VM_UNINITIALIZED; 2050cf88c790STejun Heo } 2051cf88c790STejun Heo 2052db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size, 20532dca6999SDavid Miller unsigned long align, unsigned long flags, unsigned long start, 20545e6cafc8SMarek Szyprowski unsigned long end, int node, gfp_t gfp_mask, const void *caller) 2055db64fe02SNick Piggin { 20560006526dSKautuk Consul struct vmap_area *va; 2057db64fe02SNick Piggin struct vm_struct *area; 2058d98c9e83SAndrey Ryabinin unsigned long requested_size = size; 20591da177e4SLinus Torvalds 206052fd24caSGiridhar Pemmasani BUG_ON(in_interrupt()); 20611da177e4SLinus Torvalds size = PAGE_ALIGN(size); 206231be8309SOGAWA Hirofumi if (unlikely(!size)) 206331be8309SOGAWA Hirofumi return NULL; 20641da177e4SLinus Torvalds 2065252e5c6eSzijun_hu if (flags & VM_IOREMAP) 2066252e5c6eSzijun_hu align = 1ul << clamp_t(int, get_count_order_long(size), 2067252e5c6eSzijun_hu PAGE_SHIFT, IOREMAP_MAX_ORDER); 2068252e5c6eSzijun_hu 2069cf88c790STejun Heo area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); 20701da177e4SLinus Torvalds if (unlikely(!area)) 20711da177e4SLinus Torvalds return NULL; 20721da177e4SLinus Torvalds 207371394fe5SAndrey Ryabinin if (!(flags & VM_NO_GUARD)) 20741da177e4SLinus Torvalds size += PAGE_SIZE; 20751da177e4SLinus Torvalds 2076db64fe02SNick Piggin va = alloc_vmap_area(size, align, start, end, node, gfp_mask); 2077db64fe02SNick Piggin if (IS_ERR(va)) { 2078db64fe02SNick Piggin kfree(area); 2079db64fe02SNick Piggin return NULL; 20801da177e4SLinus Torvalds } 20811da177e4SLinus Torvalds 2082d98c9e83SAndrey Ryabinin kasan_unpoison_vmalloc((void *)va->va_start, requested_size); 2083f5252e00SMitsuo Hayasaka 2084d98c9e83SAndrey Ryabinin setup_vmalloc_vm(area, va, flags, caller); 20853c5c3cfbSDaniel Axtens 20861da177e4SLinus Torvalds return area; 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds 2089c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, 2090c2968612SBenjamin Herrenschmidt unsigned long start, unsigned long end, 20915e6cafc8SMarek Szyprowski const void *caller) 2092c2968612SBenjamin Herrenschmidt { 209300ef2d2fSDavid Rientjes return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE, 209400ef2d2fSDavid Rientjes GFP_KERNEL, caller); 2095c2968612SBenjamin Herrenschmidt } 2096c2968612SBenjamin Herrenschmidt 20971da177e4SLinus Torvalds /** 2098183ff22bSSimon Arlott * get_vm_area - reserve a contiguous kernel virtual area 20991da177e4SLinus Torvalds * @size: size of the area 21001da177e4SLinus Torvalds * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC 21011da177e4SLinus Torvalds * 21021da177e4SLinus Torvalds * Search an area of @size in the kernel virtual mapping area, 21031da177e4SLinus Torvalds * and reserved it for out purposes. Returns the area descriptor 21041da177e4SLinus Torvalds * on success or %NULL on failure. 2105a862f68aSMike Rapoport * 2106a862f68aSMike Rapoport * Return: the area descriptor on success or %NULL on failure. 21071da177e4SLinus Torvalds */ 21081da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) 21091da177e4SLinus Torvalds { 21102dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 211100ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, 211200ef2d2fSDavid Rientjes __builtin_return_address(0)); 211323016969SChristoph Lameter } 211423016969SChristoph Lameter 211523016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, 21165e6cafc8SMarek Szyprowski const void *caller) 211723016969SChristoph Lameter { 21182dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 211900ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, caller); 21201da177e4SLinus Torvalds } 21211da177e4SLinus Torvalds 2122e9da6e99SMarek Szyprowski /** 2123e9da6e99SMarek Szyprowski * find_vm_area - find a continuous kernel virtual area 2124e9da6e99SMarek Szyprowski * @addr: base address 2125e9da6e99SMarek Szyprowski * 2126e9da6e99SMarek Szyprowski * Search for the kernel VM area starting at @addr, and return it. 2127e9da6e99SMarek Szyprowski * It is up to the caller to do all required locking to keep the returned 2128e9da6e99SMarek Szyprowski * pointer valid. 2129a862f68aSMike Rapoport * 2130a862f68aSMike Rapoport * Return: pointer to the found area or %NULL on faulure 2131e9da6e99SMarek Szyprowski */ 2132e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr) 213383342314SNick Piggin { 2134db64fe02SNick Piggin struct vmap_area *va; 213583342314SNick Piggin 2136db64fe02SNick Piggin va = find_vmap_area((unsigned long)addr); 2137688fcbfcSPengfei Li if (!va) 21387856dfebSAndi Kleen return NULL; 2139688fcbfcSPengfei Li 2140688fcbfcSPengfei Li return va->vm; 21417856dfebSAndi Kleen } 21427856dfebSAndi Kleen 21431da177e4SLinus Torvalds /** 2144183ff22bSSimon Arlott * remove_vm_area - find and remove a continuous kernel virtual area 21451da177e4SLinus Torvalds * @addr: base address 21461da177e4SLinus Torvalds * 21471da177e4SLinus Torvalds * Search for the kernel VM area starting at @addr, and remove it. 21481da177e4SLinus Torvalds * This function returns the found VM area, but using it is NOT safe 21497856dfebSAndi Kleen * on SMP machines, except for its size or flags. 2150a862f68aSMike Rapoport * 2151a862f68aSMike Rapoport * Return: pointer to the found area or %NULL on faulure 21521da177e4SLinus Torvalds */ 2153b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr) 21541da177e4SLinus Torvalds { 2155db64fe02SNick Piggin struct vmap_area *va; 2156db64fe02SNick Piggin 21575803ed29SChristoph Hellwig might_sleep(); 21585803ed29SChristoph Hellwig 2159dd3b8353SUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 2160dd3b8353SUladzislau Rezki (Sony) va = __find_vmap_area((unsigned long)addr); 2161688fcbfcSPengfei Li if (va && va->vm) { 2162db1aecafSMinchan Kim struct vm_struct *vm = va->vm; 2163f5252e00SMitsuo Hayasaka 2164c69480adSJoonsoo Kim va->vm = NULL; 2165c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 2166c69480adSJoonsoo Kim 2167a5af5aa8SAndrey Ryabinin kasan_free_shadow(vm); 2168dd32c279SKAMEZAWA Hiroyuki free_unmap_vmap_area(va); 2169dd32c279SKAMEZAWA Hiroyuki 2170db64fe02SNick Piggin return vm; 2171db64fe02SNick Piggin } 2172dd3b8353SUladzislau Rezki (Sony) 2173dd3b8353SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 2174db64fe02SNick Piggin return NULL; 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds 2177868b104dSRick Edgecombe static inline void set_area_direct_map(const struct vm_struct *area, 2178868b104dSRick Edgecombe int (*set_direct_map)(struct page *page)) 2179868b104dSRick Edgecombe { 2180868b104dSRick Edgecombe int i; 2181868b104dSRick Edgecombe 2182868b104dSRick Edgecombe for (i = 0; i < area->nr_pages; i++) 2183868b104dSRick Edgecombe if (page_address(area->pages[i])) 2184868b104dSRick Edgecombe set_direct_map(area->pages[i]); 2185868b104dSRick Edgecombe } 2186868b104dSRick Edgecombe 2187868b104dSRick Edgecombe /* Handle removing and resetting vm mappings related to the vm_struct. */ 2188868b104dSRick Edgecombe static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages) 2189868b104dSRick Edgecombe { 2190868b104dSRick Edgecombe unsigned long start = ULONG_MAX, end = 0; 2191868b104dSRick Edgecombe int flush_reset = area->flags & VM_FLUSH_RESET_PERMS; 219231e67340SRick Edgecombe int flush_dmap = 0; 2193868b104dSRick Edgecombe int i; 2194868b104dSRick Edgecombe 2195868b104dSRick Edgecombe remove_vm_area(area->addr); 2196868b104dSRick Edgecombe 2197868b104dSRick Edgecombe /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */ 2198868b104dSRick Edgecombe if (!flush_reset) 2199868b104dSRick Edgecombe return; 2200868b104dSRick Edgecombe 2201868b104dSRick Edgecombe /* 2202868b104dSRick Edgecombe * If not deallocating pages, just do the flush of the VM area and 2203868b104dSRick Edgecombe * return. 2204868b104dSRick Edgecombe */ 2205868b104dSRick Edgecombe if (!deallocate_pages) { 2206868b104dSRick Edgecombe vm_unmap_aliases(); 2207868b104dSRick Edgecombe return; 2208868b104dSRick Edgecombe } 2209868b104dSRick Edgecombe 2210868b104dSRick Edgecombe /* 2211868b104dSRick Edgecombe * If execution gets here, flush the vm mapping and reset the direct 2212868b104dSRick Edgecombe * map. Find the start and end range of the direct mappings to make sure 2213868b104dSRick Edgecombe * the vm_unmap_aliases() flush includes the direct map. 2214868b104dSRick Edgecombe */ 2215868b104dSRick Edgecombe for (i = 0; i < area->nr_pages; i++) { 22168e41f872SRick Edgecombe unsigned long addr = (unsigned long)page_address(area->pages[i]); 22178e41f872SRick Edgecombe if (addr) { 2218868b104dSRick Edgecombe start = min(addr, start); 22198e41f872SRick Edgecombe end = max(addr + PAGE_SIZE, end); 222031e67340SRick Edgecombe flush_dmap = 1; 2221868b104dSRick Edgecombe } 2222868b104dSRick Edgecombe } 2223868b104dSRick Edgecombe 2224868b104dSRick Edgecombe /* 2225868b104dSRick Edgecombe * Set direct map to something invalid so that it won't be cached if 2226868b104dSRick Edgecombe * there are any accesses after the TLB flush, then flush the TLB and 2227868b104dSRick Edgecombe * reset the direct map permissions to the default. 2228868b104dSRick Edgecombe */ 2229868b104dSRick Edgecombe set_area_direct_map(area, set_direct_map_invalid_noflush); 223031e67340SRick Edgecombe _vm_unmap_aliases(start, end, flush_dmap); 2231868b104dSRick Edgecombe set_area_direct_map(area, set_direct_map_default_noflush); 2232868b104dSRick Edgecombe } 2233868b104dSRick Edgecombe 2234b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages) 22351da177e4SLinus Torvalds { 22361da177e4SLinus Torvalds struct vm_struct *area; 22371da177e4SLinus Torvalds 22381da177e4SLinus Torvalds if (!addr) 22391da177e4SLinus Torvalds return; 22401da177e4SLinus Torvalds 2241e69e9d4aSHATAYAMA Daisuke if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n", 2242ab15d9b4SDan Carpenter addr)) 22431da177e4SLinus Torvalds return; 22441da177e4SLinus Torvalds 22456ade2032SLiviu Dudau area = find_vm_area(addr); 22461da177e4SLinus Torvalds if (unlikely(!area)) { 22474c8573e2SArjan van de Ven WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", 22481da177e4SLinus Torvalds addr); 22491da177e4SLinus Torvalds return; 22501da177e4SLinus Torvalds } 22511da177e4SLinus Torvalds 225205e3ff95SChintan Pandya debug_check_no_locks_freed(area->addr, get_vm_area_size(area)); 225305e3ff95SChintan Pandya debug_check_no_obj_freed(area->addr, get_vm_area_size(area)); 22549a11b49aSIngo Molnar 22553c5c3cfbSDaniel Axtens kasan_poison_vmalloc(area->addr, area->size); 22563c5c3cfbSDaniel Axtens 2257868b104dSRick Edgecombe vm_remove_mappings(area, deallocate_pages); 2258868b104dSRick Edgecombe 22591da177e4SLinus Torvalds if (deallocate_pages) { 22601da177e4SLinus Torvalds int i; 22611da177e4SLinus Torvalds 22621da177e4SLinus Torvalds for (i = 0; i < area->nr_pages; i++) { 2263bf53d6f8SChristoph Lameter struct page *page = area->pages[i]; 2264bf53d6f8SChristoph Lameter 2265bf53d6f8SChristoph Lameter BUG_ON(!page); 22664949148aSVladimir Davydov __free_pages(page, 0); 22671da177e4SLinus Torvalds } 226897105f0aSRoman Gushchin atomic_long_sub(area->nr_pages, &nr_vmalloc_pages); 22691da177e4SLinus Torvalds 2270244d63eeSDavid Rientjes kvfree(area->pages); 22711da177e4SLinus Torvalds } 22721da177e4SLinus Torvalds 22731da177e4SLinus Torvalds kfree(area); 22741da177e4SLinus Torvalds return; 22751da177e4SLinus Torvalds } 22761da177e4SLinus Torvalds 2277bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr) 2278bf22e37aSAndrey Ryabinin { 2279bf22e37aSAndrey Ryabinin /* 2280bf22e37aSAndrey Ryabinin * Use raw_cpu_ptr() because this can be called from preemptible 2281bf22e37aSAndrey Ryabinin * context. Preemption is absolutely fine here, because the llist_add() 2282bf22e37aSAndrey Ryabinin * implementation is lockless, so it works even if we are adding to 2283bf22e37aSAndrey Ryabinin * nother cpu's list. schedule_work() should be fine with this too. 2284bf22e37aSAndrey Ryabinin */ 2285bf22e37aSAndrey Ryabinin struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred); 2286bf22e37aSAndrey Ryabinin 2287bf22e37aSAndrey Ryabinin if (llist_add((struct llist_node *)addr, &p->list)) 2288bf22e37aSAndrey Ryabinin schedule_work(&p->wq); 2289bf22e37aSAndrey Ryabinin } 2290bf22e37aSAndrey Ryabinin 2291bf22e37aSAndrey Ryabinin /** 2292bf22e37aSAndrey Ryabinin * vfree_atomic - release memory allocated by vmalloc() 2293bf22e37aSAndrey Ryabinin * @addr: memory base address 2294bf22e37aSAndrey Ryabinin * 2295bf22e37aSAndrey Ryabinin * This one is just like vfree() but can be called in any atomic context 2296bf22e37aSAndrey Ryabinin * except NMIs. 2297bf22e37aSAndrey Ryabinin */ 2298bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr) 2299bf22e37aSAndrey Ryabinin { 2300bf22e37aSAndrey Ryabinin BUG_ON(in_nmi()); 2301bf22e37aSAndrey Ryabinin 2302bf22e37aSAndrey Ryabinin kmemleak_free(addr); 2303bf22e37aSAndrey Ryabinin 2304bf22e37aSAndrey Ryabinin if (!addr) 2305bf22e37aSAndrey Ryabinin return; 2306bf22e37aSAndrey Ryabinin __vfree_deferred(addr); 2307bf22e37aSAndrey Ryabinin } 2308bf22e37aSAndrey Ryabinin 2309c67dc624SRoman Penyaev static void __vfree(const void *addr) 2310c67dc624SRoman Penyaev { 2311c67dc624SRoman Penyaev if (unlikely(in_interrupt())) 2312c67dc624SRoman Penyaev __vfree_deferred(addr); 2313c67dc624SRoman Penyaev else 2314c67dc624SRoman Penyaev __vunmap(addr, 1); 2315c67dc624SRoman Penyaev } 2316c67dc624SRoman Penyaev 23171da177e4SLinus Torvalds /** 23181da177e4SLinus Torvalds * vfree - release memory allocated by vmalloc() 23191da177e4SLinus Torvalds * @addr: memory base address 23201da177e4SLinus Torvalds * 2321183ff22bSSimon Arlott * Free the virtually continuous memory area starting at @addr, as 232280e93effSPekka Enberg * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is 232380e93effSPekka Enberg * NULL, no operation is performed. 23241da177e4SLinus Torvalds * 232532fcfd40SAl Viro * Must not be called in NMI context (strictly speaking, only if we don't 232632fcfd40SAl Viro * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling 232732fcfd40SAl Viro * conventions for vfree() arch-depenedent would be a really bad idea) 232832fcfd40SAl Viro * 23293ca4ea3aSAndrey Ryabinin * May sleep if called *not* from interrupt context. 23303ca4ea3aSAndrey Ryabinin * 23310e056eb5Smchehab@s-opensource.com * NOTE: assumes that the object at @addr has a size >= sizeof(llist_node) 23321da177e4SLinus Torvalds */ 2333b3bdda02SChristoph Lameter void vfree(const void *addr) 23341da177e4SLinus Torvalds { 233532fcfd40SAl Viro BUG_ON(in_nmi()); 233689219d37SCatalin Marinas 233789219d37SCatalin Marinas kmemleak_free(addr); 233889219d37SCatalin Marinas 2339a8dda165SAndrey Ryabinin might_sleep_if(!in_interrupt()); 2340a8dda165SAndrey Ryabinin 234132fcfd40SAl Viro if (!addr) 234232fcfd40SAl Viro return; 2343c67dc624SRoman Penyaev 2344c67dc624SRoman Penyaev __vfree(addr); 23451da177e4SLinus Torvalds } 23461da177e4SLinus Torvalds EXPORT_SYMBOL(vfree); 23471da177e4SLinus Torvalds 23481da177e4SLinus Torvalds /** 23491da177e4SLinus Torvalds * vunmap - release virtual mapping obtained by vmap() 23501da177e4SLinus Torvalds * @addr: memory base address 23511da177e4SLinus Torvalds * 23521da177e4SLinus Torvalds * Free the virtually contiguous memory area starting at @addr, 23531da177e4SLinus Torvalds * which was created from the page array passed to vmap(). 23541da177e4SLinus Torvalds * 235580e93effSPekka Enberg * Must not be called in interrupt context. 23561da177e4SLinus Torvalds */ 2357b3bdda02SChristoph Lameter void vunmap(const void *addr) 23581da177e4SLinus Torvalds { 23591da177e4SLinus Torvalds BUG_ON(in_interrupt()); 236034754b69SPeter Zijlstra might_sleep(); 236132fcfd40SAl Viro if (addr) 23621da177e4SLinus Torvalds __vunmap(addr, 0); 23631da177e4SLinus Torvalds } 23641da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap); 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds /** 23671da177e4SLinus Torvalds * vmap - map an array of pages into virtually contiguous space 23681da177e4SLinus Torvalds * @pages: array of page pointers 23691da177e4SLinus Torvalds * @count: number of pages to map 23701da177e4SLinus Torvalds * @flags: vm_area->flags 23711da177e4SLinus Torvalds * @prot: page protection for the mapping 23721da177e4SLinus Torvalds * 23731da177e4SLinus Torvalds * Maps @count pages from @pages into contiguous kernel virtual 23741da177e4SLinus Torvalds * space. 2375a862f68aSMike Rapoport * 2376a862f68aSMike Rapoport * Return: the address of the area or %NULL on failure 23771da177e4SLinus Torvalds */ 23781da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, 23791da177e4SLinus Torvalds unsigned long flags, pgprot_t prot) 23801da177e4SLinus Torvalds { 23811da177e4SLinus Torvalds struct vm_struct *area; 238265ee03c4SGuillermo Julián Moreno unsigned long size; /* In bytes */ 23831da177e4SLinus Torvalds 238434754b69SPeter Zijlstra might_sleep(); 238534754b69SPeter Zijlstra 2386ca79b0c2SArun KS if (count > totalram_pages()) 23871da177e4SLinus Torvalds return NULL; 23881da177e4SLinus Torvalds 238965ee03c4SGuillermo Julián Moreno size = (unsigned long)count << PAGE_SHIFT; 239065ee03c4SGuillermo Julián Moreno area = get_vm_area_caller(size, flags, __builtin_return_address(0)); 23911da177e4SLinus Torvalds if (!area) 23921da177e4SLinus Torvalds return NULL; 239323016969SChristoph Lameter 2394cca98e9fSChristoph Hellwig if (map_kernel_range((unsigned long)area->addr, size, pgprot_nx(prot), 2395ed1f324cSChristoph Hellwig pages) < 0) { 23961da177e4SLinus Torvalds vunmap(area->addr); 23971da177e4SLinus Torvalds return NULL; 23981da177e4SLinus Torvalds } 23991da177e4SLinus Torvalds 24001da177e4SLinus Torvalds return area->addr; 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds EXPORT_SYMBOL(vmap); 24031da177e4SLinus Torvalds 2404e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, 24053722e13cSWanpeng Li pgprot_t prot, int node) 24061da177e4SLinus Torvalds { 24071da177e4SLinus Torvalds struct page **pages; 24081da177e4SLinus Torvalds unsigned int nr_pages, array_size, i; 2409930f036bSDavid Rientjes const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; 2410704b862fSLaura Abbott const gfp_t alloc_mask = gfp_mask | __GFP_NOWARN; 2411704b862fSLaura Abbott const gfp_t highmem_mask = (gfp_mask & (GFP_DMA | GFP_DMA32)) ? 2412704b862fSLaura Abbott 0 : 2413704b862fSLaura Abbott __GFP_HIGHMEM; 24141da177e4SLinus Torvalds 2415762216abSWanpeng Li nr_pages = get_vm_area_size(area) >> PAGE_SHIFT; 24161da177e4SLinus Torvalds array_size = (nr_pages * sizeof(struct page *)); 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds /* Please note that the recursion is strictly bounded. */ 24198757d5faSJan Kiszka if (array_size > PAGE_SIZE) { 2420704b862fSLaura Abbott pages = __vmalloc_node(array_size, 1, nested_gfp|highmem_mask, 2421f38fcb9cSChristoph Hellwig node, area->caller); 2422286e1ea3SAndrew Morton } else { 2423976d6dfbSJan Beulich pages = kmalloc_node(array_size, nested_gfp, node); 2424286e1ea3SAndrew Morton } 24257ea36242SAustin Kim 24267ea36242SAustin Kim if (!pages) { 24271da177e4SLinus Torvalds remove_vm_area(area->addr); 24281da177e4SLinus Torvalds kfree(area); 24291da177e4SLinus Torvalds return NULL; 24301da177e4SLinus Torvalds } 24311da177e4SLinus Torvalds 24327ea36242SAustin Kim area->pages = pages; 24337ea36242SAustin Kim area->nr_pages = nr_pages; 24347ea36242SAustin Kim 24351da177e4SLinus Torvalds for (i = 0; i < area->nr_pages; i++) { 2436bf53d6f8SChristoph Lameter struct page *page; 2437bf53d6f8SChristoph Lameter 24384b90951cSJianguo Wu if (node == NUMA_NO_NODE) 2439704b862fSLaura Abbott page = alloc_page(alloc_mask|highmem_mask); 2440930fc45aSChristoph Lameter else 2441704b862fSLaura Abbott page = alloc_pages_node(node, alloc_mask|highmem_mask, 0); 2442bf53d6f8SChristoph Lameter 2443bf53d6f8SChristoph Lameter if (unlikely(!page)) { 24441da177e4SLinus Torvalds /* Successfully allocated i pages, free them in __vunmap() */ 24451da177e4SLinus Torvalds area->nr_pages = i; 244697105f0aSRoman Gushchin atomic_long_add(area->nr_pages, &nr_vmalloc_pages); 24471da177e4SLinus Torvalds goto fail; 24481da177e4SLinus Torvalds } 2449bf53d6f8SChristoph Lameter area->pages[i] = page; 2450dcf61ff0SLiu Xiang if (gfpflags_allow_blocking(gfp_mask)) 2451660654f9SEric Dumazet cond_resched(); 24521da177e4SLinus Torvalds } 245397105f0aSRoman Gushchin atomic_long_add(area->nr_pages, &nr_vmalloc_pages); 24541da177e4SLinus Torvalds 2455ed1f324cSChristoph Hellwig if (map_kernel_range((unsigned long)area->addr, get_vm_area_size(area), 2456ed1f324cSChristoph Hellwig prot, pages) < 0) 24571da177e4SLinus Torvalds goto fail; 2458ed1f324cSChristoph Hellwig 24591da177e4SLinus Torvalds return area->addr; 24601da177e4SLinus Torvalds 24611da177e4SLinus Torvalds fail: 2462a8e99259SMichal Hocko warn_alloc(gfp_mask, NULL, 24637877cdccSMichal Hocko "vmalloc: allocation failure, allocated %ld of %ld bytes", 246422943ab1SDave Hansen (area->nr_pages*PAGE_SIZE), area->size); 2465c67dc624SRoman Penyaev __vfree(area->addr); 24661da177e4SLinus Torvalds return NULL; 24671da177e4SLinus Torvalds } 24681da177e4SLinus Torvalds 2469d0a21265SDavid Rientjes /** 2470d0a21265SDavid Rientjes * __vmalloc_node_range - allocate virtually contiguous memory 2471d0a21265SDavid Rientjes * @size: allocation size 2472d0a21265SDavid Rientjes * @align: desired alignment 2473d0a21265SDavid Rientjes * @start: vm area range start 2474d0a21265SDavid Rientjes * @end: vm area range end 2475d0a21265SDavid Rientjes * @gfp_mask: flags for the page level allocator 2476d0a21265SDavid Rientjes * @prot: protection mask for the allocated pages 2477cb9e3c29SAndrey Ryabinin * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD) 247800ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 2479d0a21265SDavid Rientjes * @caller: caller's return address 2480d0a21265SDavid Rientjes * 2481d0a21265SDavid Rientjes * Allocate enough pages to cover @size from the page level 2482d0a21265SDavid Rientjes * allocator with @gfp_mask flags. Map them into contiguous 2483d0a21265SDavid Rientjes * kernel virtual space, using a pagetable protection of @prot. 2484a862f68aSMike Rapoport * 2485a862f68aSMike Rapoport * Return: the address of the area or %NULL on failure 2486d0a21265SDavid Rientjes */ 2487d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align, 2488d0a21265SDavid Rientjes unsigned long start, unsigned long end, gfp_t gfp_mask, 2489cb9e3c29SAndrey Ryabinin pgprot_t prot, unsigned long vm_flags, int node, 2490cb9e3c29SAndrey Ryabinin const void *caller) 2491930fc45aSChristoph Lameter { 2492d0a21265SDavid Rientjes struct vm_struct *area; 2493d0a21265SDavid Rientjes void *addr; 2494d0a21265SDavid Rientjes unsigned long real_size = size; 2495d0a21265SDavid Rientjes 2496d0a21265SDavid Rientjes size = PAGE_ALIGN(size); 2497ca79b0c2SArun KS if (!size || (size >> PAGE_SHIFT) > totalram_pages()) 2498de7d2b56SJoe Perches goto fail; 2499d0a21265SDavid Rientjes 2500d98c9e83SAndrey Ryabinin area = __get_vm_area_node(real_size, align, VM_ALLOC | VM_UNINITIALIZED | 2501cb9e3c29SAndrey Ryabinin vm_flags, start, end, node, gfp_mask, caller); 2502d0a21265SDavid Rientjes if (!area) 2503de7d2b56SJoe Perches goto fail; 2504d0a21265SDavid Rientjes 25053722e13cSWanpeng Li addr = __vmalloc_area_node(area, gfp_mask, prot, node); 25061368edf0SMel Gorman if (!addr) 2507b82225f3SWanpeng Li return NULL; 250889219d37SCatalin Marinas 250989219d37SCatalin Marinas /* 251020fc02b4SZhang Yanfei * In this function, newly allocated vm_struct has VM_UNINITIALIZED 251120fc02b4SZhang Yanfei * flag. It means that vm_struct is not fully initialized. 25124341fa45SJoonsoo Kim * Now, it is fully initialized, so remove this flag here. 2513f5252e00SMitsuo Hayasaka */ 251420fc02b4SZhang Yanfei clear_vm_uninitialized_flag(area); 2515f5252e00SMitsuo Hayasaka 251694f4a161SCatalin Marinas kmemleak_vmalloc(area, size, gfp_mask); 251789219d37SCatalin Marinas 251889219d37SCatalin Marinas return addr; 2519de7d2b56SJoe Perches 2520de7d2b56SJoe Perches fail: 2521a8e99259SMichal Hocko warn_alloc(gfp_mask, NULL, 25227877cdccSMichal Hocko "vmalloc: allocation failure: %lu bytes", real_size); 2523de7d2b56SJoe Perches return NULL; 2524930fc45aSChristoph Lameter } 2525930fc45aSChristoph Lameter 2526153178edSUladzislau Rezki (Sony) /* 2527153178edSUladzislau Rezki (Sony) * This is only for performance analysis of vmalloc and stress purpose. 2528153178edSUladzislau Rezki (Sony) * It is required by vmalloc test module, therefore do not use it other 2529153178edSUladzislau Rezki (Sony) * than that. 2530153178edSUladzislau Rezki (Sony) */ 2531153178edSUladzislau Rezki (Sony) #ifdef CONFIG_TEST_VMALLOC_MODULE 2532153178edSUladzislau Rezki (Sony) EXPORT_SYMBOL_GPL(__vmalloc_node_range); 2533153178edSUladzislau Rezki (Sony) #endif 2534153178edSUladzislau Rezki (Sony) 25351da177e4SLinus Torvalds /** 2536930fc45aSChristoph Lameter * __vmalloc_node - allocate virtually contiguous memory 25371da177e4SLinus Torvalds * @size: allocation size 25382dca6999SDavid Miller * @align: desired alignment 25391da177e4SLinus Torvalds * @gfp_mask: flags for the page level allocator 254000ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 2541c85d194bSRandy Dunlap * @caller: caller's return address 25421da177e4SLinus Torvalds * 2543f38fcb9cSChristoph Hellwig * Allocate enough pages to cover @size from the page level allocator with 2544f38fcb9cSChristoph Hellwig * @gfp_mask flags. Map them into contiguous kernel virtual space. 2545a7c3e901SMichal Hocko * 2546dcda9b04SMichal Hocko * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL 2547a7c3e901SMichal Hocko * and __GFP_NOFAIL are not supported 2548a7c3e901SMichal Hocko * 2549a7c3e901SMichal Hocko * Any use of gfp flags outside of GFP_KERNEL should be consulted 2550a7c3e901SMichal Hocko * with mm people. 2551a862f68aSMike Rapoport * 2552a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 25531da177e4SLinus Torvalds */ 2554*2b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align, 2555f38fcb9cSChristoph Hellwig gfp_t gfp_mask, int node, const void *caller) 25561da177e4SLinus Torvalds { 2557d0a21265SDavid Rientjes return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, 2558f38fcb9cSChristoph Hellwig gfp_mask, PAGE_KERNEL, 0, node, caller); 25591da177e4SLinus Torvalds } 25601da177e4SLinus Torvalds 256188dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask) 2562930fc45aSChristoph Lameter { 2563f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE, 256423016969SChristoph Lameter __builtin_return_address(0)); 2565930fc45aSChristoph Lameter } 25661da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc); 25671da177e4SLinus Torvalds 25681da177e4SLinus Torvalds /** 25691da177e4SLinus Torvalds * vmalloc - allocate virtually contiguous memory 25701da177e4SLinus Torvalds * @size: allocation size 257192eac168SMike Rapoport * 25721da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 25731da177e4SLinus Torvalds * allocator and map them into contiguous kernel virtual space. 25741da177e4SLinus Torvalds * 2575c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 25761da177e4SLinus Torvalds * use __vmalloc() instead. 2577a862f68aSMike Rapoport * 2578a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 25791da177e4SLinus Torvalds */ 25801da177e4SLinus Torvalds void *vmalloc(unsigned long size) 25811da177e4SLinus Torvalds { 25824d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE, 25834d39d728SChristoph Hellwig __builtin_return_address(0)); 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc); 25861da177e4SLinus Torvalds 2587930fc45aSChristoph Lameter /** 2588e1ca7788SDave Young * vzalloc - allocate virtually contiguous memory with zero fill 2589e1ca7788SDave Young * @size: allocation size 259092eac168SMike Rapoport * 2591e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 2592e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 2593e1ca7788SDave Young * The memory allocated is set to zero. 2594e1ca7788SDave Young * 2595e1ca7788SDave Young * For tight control over page level allocator and protection flags 2596e1ca7788SDave Young * use __vmalloc() instead. 2597a862f68aSMike Rapoport * 2598a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 2599e1ca7788SDave Young */ 2600e1ca7788SDave Young void *vzalloc(unsigned long size) 2601e1ca7788SDave Young { 26024d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE, 26034d39d728SChristoph Hellwig __builtin_return_address(0)); 2604e1ca7788SDave Young } 2605e1ca7788SDave Young EXPORT_SYMBOL(vzalloc); 2606e1ca7788SDave Young 2607e1ca7788SDave Young /** 2608ead04089SRolf Eike Beer * vmalloc_user - allocate zeroed virtually contiguous memory for userspace 260983342314SNick Piggin * @size: allocation size 2610ead04089SRolf Eike Beer * 2611ead04089SRolf Eike Beer * The resulting memory area is zeroed so it can be mapped to userspace 2612ead04089SRolf Eike Beer * without leaking data. 2613a862f68aSMike Rapoport * 2614a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 261583342314SNick Piggin */ 261683342314SNick Piggin void *vmalloc_user(unsigned long size) 261783342314SNick Piggin { 2618bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 2619bc84c535SRoman Penyaev GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL, 2620bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 262100ef2d2fSDavid Rientjes __builtin_return_address(0)); 262283342314SNick Piggin } 262383342314SNick Piggin EXPORT_SYMBOL(vmalloc_user); 262483342314SNick Piggin 262583342314SNick Piggin /** 2626930fc45aSChristoph Lameter * vmalloc_node - allocate memory on a specific node 2627930fc45aSChristoph Lameter * @size: allocation size 2628d44e0780SRandy Dunlap * @node: numa node 2629930fc45aSChristoph Lameter * 2630930fc45aSChristoph Lameter * Allocate enough pages to cover @size from the page level 2631930fc45aSChristoph Lameter * allocator and map them into contiguous kernel virtual space. 2632930fc45aSChristoph Lameter * 2633c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 2634930fc45aSChristoph Lameter * use __vmalloc() instead. 2635a862f68aSMike Rapoport * 2636a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 2637930fc45aSChristoph Lameter */ 2638930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node) 2639930fc45aSChristoph Lameter { 2640f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL, node, 2641f38fcb9cSChristoph Hellwig __builtin_return_address(0)); 2642930fc45aSChristoph Lameter } 2643930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node); 2644930fc45aSChristoph Lameter 2645e1ca7788SDave Young /** 2646e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill 2647e1ca7788SDave Young * @size: allocation size 2648e1ca7788SDave Young * @node: numa node 2649e1ca7788SDave Young * 2650e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 2651e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 2652e1ca7788SDave Young * The memory allocated is set to zero. 2653e1ca7788SDave Young * 2654a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 2655e1ca7788SDave Young */ 2656e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node) 2657e1ca7788SDave Young { 26584d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node, 26594d39d728SChristoph Hellwig __builtin_return_address(0)); 2660e1ca7788SDave Young } 2661e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node); 2662e1ca7788SDave Young 26631da177e4SLinus Torvalds /** 2664fc970227SAndrii Nakryiko * vmalloc_user_node_flags - allocate memory for userspace on a specific node 2665fc970227SAndrii Nakryiko * @size: allocation size 2666fc970227SAndrii Nakryiko * @node: numa node 2667fc970227SAndrii Nakryiko * @flags: flags for the page level allocator 2668fc970227SAndrii Nakryiko * 2669fc970227SAndrii Nakryiko * The resulting memory area is zeroed so it can be mapped to userspace 2670fc970227SAndrii Nakryiko * without leaking data. 2671fc970227SAndrii Nakryiko * 2672fc970227SAndrii Nakryiko * Return: pointer to the allocated memory or %NULL on error 2673fc970227SAndrii Nakryiko */ 2674fc970227SAndrii Nakryiko void *vmalloc_user_node_flags(unsigned long size, int node, gfp_t flags) 2675fc970227SAndrii Nakryiko { 2676fc970227SAndrii Nakryiko return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 2677fc970227SAndrii Nakryiko flags | __GFP_ZERO, PAGE_KERNEL, 2678fc970227SAndrii Nakryiko VM_USERMAP, node, 2679fc970227SAndrii Nakryiko __builtin_return_address(0)); 2680fc970227SAndrii Nakryiko } 2681fc970227SAndrii Nakryiko EXPORT_SYMBOL(vmalloc_user_node_flags); 2682fc970227SAndrii Nakryiko 2683fc970227SAndrii Nakryiko /** 26841da177e4SLinus Torvalds * vmalloc_exec - allocate virtually contiguous, executable memory 26851da177e4SLinus Torvalds * @size: allocation size 26861da177e4SLinus Torvalds * 26871da177e4SLinus Torvalds * Kernel-internal function to allocate enough pages to cover @size 26881da177e4SLinus Torvalds * the page level allocator and map them into contiguous and 26891da177e4SLinus Torvalds * executable kernel virtual space. 26901da177e4SLinus Torvalds * 2691c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 26921da177e4SLinus Torvalds * use __vmalloc() instead. 2693a862f68aSMike Rapoport * 2694a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 26951da177e4SLinus Torvalds */ 26961da177e4SLinus Torvalds void *vmalloc_exec(unsigned long size) 26971da177e4SLinus Torvalds { 2698868b104dSRick Edgecombe return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, 2699868b104dSRick Edgecombe GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS, 270000ef2d2fSDavid Rientjes NUMA_NO_NODE, __builtin_return_address(0)); 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds 27030d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) 2704698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) 27050d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) 2706698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL) 27070d08e0d3SAndi Kleen #else 2708698d0831SMichal Hocko /* 2709698d0831SMichal Hocko * 64b systems should always have either DMA or DMA32 zones. For others 2710698d0831SMichal Hocko * GFP_DMA32 should do the right thing and use the normal zone. 2711698d0831SMichal Hocko */ 2712698d0831SMichal Hocko #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL 27130d08e0d3SAndi Kleen #endif 27140d08e0d3SAndi Kleen 27151da177e4SLinus Torvalds /** 27161da177e4SLinus Torvalds * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 27171da177e4SLinus Torvalds * @size: allocation size 27181da177e4SLinus Torvalds * 27191da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 27201da177e4SLinus Torvalds * page level allocator and map them into contiguous kernel virtual space. 2721a862f68aSMike Rapoport * 2722a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 27231da177e4SLinus Torvalds */ 27241da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 27251da177e4SLinus Torvalds { 2726f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE, 2727f38fcb9cSChristoph Hellwig __builtin_return_address(0)); 27281da177e4SLinus Torvalds } 27291da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32); 27301da177e4SLinus Torvalds 273183342314SNick Piggin /** 2732ead04089SRolf Eike Beer * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 273383342314SNick Piggin * @size: allocation size 2734ead04089SRolf Eike Beer * 2735ead04089SRolf Eike Beer * The resulting memory area is 32bit addressable and zeroed so it can be 2736ead04089SRolf Eike Beer * mapped to userspace without leaking data. 2737a862f68aSMike Rapoport * 2738a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 273983342314SNick Piggin */ 274083342314SNick Piggin void *vmalloc_32_user(unsigned long size) 274183342314SNick Piggin { 2742bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 2743bc84c535SRoman Penyaev GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, 2744bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 27455a82ac71SRoman Penyaev __builtin_return_address(0)); 274683342314SNick Piggin } 274783342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user); 274883342314SNick Piggin 2749d0107eb0SKAMEZAWA Hiroyuki /* 2750d0107eb0SKAMEZAWA Hiroyuki * small helper routine , copy contents to buf from addr. 2751d0107eb0SKAMEZAWA Hiroyuki * If the page is not present, fill zero. 2752d0107eb0SKAMEZAWA Hiroyuki */ 2753d0107eb0SKAMEZAWA Hiroyuki 2754d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count) 2755d0107eb0SKAMEZAWA Hiroyuki { 2756d0107eb0SKAMEZAWA Hiroyuki struct page *p; 2757d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 2758d0107eb0SKAMEZAWA Hiroyuki 2759d0107eb0SKAMEZAWA Hiroyuki while (count) { 2760d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 2761d0107eb0SKAMEZAWA Hiroyuki 2762891c49abSAlexander Kuleshov offset = offset_in_page(addr); 2763d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 2764d0107eb0SKAMEZAWA Hiroyuki if (length > count) 2765d0107eb0SKAMEZAWA Hiroyuki length = count; 2766d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 2767d0107eb0SKAMEZAWA Hiroyuki /* 2768d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 2769d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 2770d0107eb0SKAMEZAWA Hiroyuki * overhead of vmalloc()/vfree() calles for this _debug_ 2771d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 2772d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 2773d0107eb0SKAMEZAWA Hiroyuki */ 2774d0107eb0SKAMEZAWA Hiroyuki if (p) { 2775d0107eb0SKAMEZAWA Hiroyuki /* 2776d0107eb0SKAMEZAWA Hiroyuki * we can expect USER0 is not used (see vread/vwrite's 2777d0107eb0SKAMEZAWA Hiroyuki * function description) 2778d0107eb0SKAMEZAWA Hiroyuki */ 27799b04c5feSCong Wang void *map = kmap_atomic(p); 2780d0107eb0SKAMEZAWA Hiroyuki memcpy(buf, map + offset, length); 27819b04c5feSCong Wang kunmap_atomic(map); 2782d0107eb0SKAMEZAWA Hiroyuki } else 2783d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, length); 2784d0107eb0SKAMEZAWA Hiroyuki 2785d0107eb0SKAMEZAWA Hiroyuki addr += length; 2786d0107eb0SKAMEZAWA Hiroyuki buf += length; 2787d0107eb0SKAMEZAWA Hiroyuki copied += length; 2788d0107eb0SKAMEZAWA Hiroyuki count -= length; 2789d0107eb0SKAMEZAWA Hiroyuki } 2790d0107eb0SKAMEZAWA Hiroyuki return copied; 2791d0107eb0SKAMEZAWA Hiroyuki } 2792d0107eb0SKAMEZAWA Hiroyuki 2793d0107eb0SKAMEZAWA Hiroyuki static int aligned_vwrite(char *buf, char *addr, unsigned long count) 2794d0107eb0SKAMEZAWA Hiroyuki { 2795d0107eb0SKAMEZAWA Hiroyuki struct page *p; 2796d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 2797d0107eb0SKAMEZAWA Hiroyuki 2798d0107eb0SKAMEZAWA Hiroyuki while (count) { 2799d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 2800d0107eb0SKAMEZAWA Hiroyuki 2801891c49abSAlexander Kuleshov offset = offset_in_page(addr); 2802d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 2803d0107eb0SKAMEZAWA Hiroyuki if (length > count) 2804d0107eb0SKAMEZAWA Hiroyuki length = count; 2805d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 2806d0107eb0SKAMEZAWA Hiroyuki /* 2807d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 2808d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 2809d0107eb0SKAMEZAWA Hiroyuki * overhead of vmalloc()/vfree() calles for this _debug_ 2810d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 2811d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 2812d0107eb0SKAMEZAWA Hiroyuki */ 2813d0107eb0SKAMEZAWA Hiroyuki if (p) { 2814d0107eb0SKAMEZAWA Hiroyuki /* 2815d0107eb0SKAMEZAWA Hiroyuki * we can expect USER0 is not used (see vread/vwrite's 2816d0107eb0SKAMEZAWA Hiroyuki * function description) 2817d0107eb0SKAMEZAWA Hiroyuki */ 28189b04c5feSCong Wang void *map = kmap_atomic(p); 2819d0107eb0SKAMEZAWA Hiroyuki memcpy(map + offset, buf, length); 28209b04c5feSCong Wang kunmap_atomic(map); 2821d0107eb0SKAMEZAWA Hiroyuki } 2822d0107eb0SKAMEZAWA Hiroyuki addr += length; 2823d0107eb0SKAMEZAWA Hiroyuki buf += length; 2824d0107eb0SKAMEZAWA Hiroyuki copied += length; 2825d0107eb0SKAMEZAWA Hiroyuki count -= length; 2826d0107eb0SKAMEZAWA Hiroyuki } 2827d0107eb0SKAMEZAWA Hiroyuki return copied; 2828d0107eb0SKAMEZAWA Hiroyuki } 2829d0107eb0SKAMEZAWA Hiroyuki 2830d0107eb0SKAMEZAWA Hiroyuki /** 2831d0107eb0SKAMEZAWA Hiroyuki * vread() - read vmalloc area in a safe way. 2832d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for reading data 2833d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 2834d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 2835d0107eb0SKAMEZAWA Hiroyuki * 2836d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 2837d0107eb0SKAMEZAWA Hiroyuki * copy data from that area to a given buffer. If the given memory range 2838d0107eb0SKAMEZAWA Hiroyuki * of [addr...addr+count) includes some valid address, data is copied to 2839d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, they'll be zero-filled. 2840d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 2841d0107eb0SKAMEZAWA Hiroyuki * 2842d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 2843a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 2844d0107eb0SKAMEZAWA Hiroyuki * 2845d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vread() is never necessary because the caller 2846d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 2847d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 2848d9009d67SGeert Uytterhoeven * any information, as /dev/kmem. 2849a862f68aSMike Rapoport * 2850a862f68aSMike Rapoport * Return: number of bytes for which addr and buf should be increased 2851a862f68aSMike Rapoport * (same number as @count) or %0 if [addr...addr+count) doesn't 2852a862f68aSMike Rapoport * include any intersection with valid vmalloc area 2853d0107eb0SKAMEZAWA Hiroyuki */ 28541da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 28551da177e4SLinus Torvalds { 2856e81ce85fSJoonsoo Kim struct vmap_area *va; 2857e81ce85fSJoonsoo Kim struct vm_struct *vm; 28581da177e4SLinus Torvalds char *vaddr, *buf_start = buf; 2859d0107eb0SKAMEZAWA Hiroyuki unsigned long buflen = count; 28601da177e4SLinus Torvalds unsigned long n; 28611da177e4SLinus Torvalds 28621da177e4SLinus Torvalds /* Don't allow overflow */ 28631da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 28641da177e4SLinus Torvalds count = -(unsigned long) addr; 28651da177e4SLinus Torvalds 2866e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 2867e81ce85fSJoonsoo Kim list_for_each_entry(va, &vmap_area_list, list) { 2868e81ce85fSJoonsoo Kim if (!count) 2869e81ce85fSJoonsoo Kim break; 2870e81ce85fSJoonsoo Kim 2871688fcbfcSPengfei Li if (!va->vm) 2872e81ce85fSJoonsoo Kim continue; 2873e81ce85fSJoonsoo Kim 2874e81ce85fSJoonsoo Kim vm = va->vm; 2875e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 2876762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 28771da177e4SLinus Torvalds continue; 28781da177e4SLinus Torvalds while (addr < vaddr) { 28791da177e4SLinus Torvalds if (count == 0) 28801da177e4SLinus Torvalds goto finished; 28811da177e4SLinus Torvalds *buf = '\0'; 28821da177e4SLinus Torvalds buf++; 28831da177e4SLinus Torvalds addr++; 28841da177e4SLinus Torvalds count--; 28851da177e4SLinus Torvalds } 2886762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 2887d0107eb0SKAMEZAWA Hiroyuki if (n > count) 2888d0107eb0SKAMEZAWA Hiroyuki n = count; 2889e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) 2890d0107eb0SKAMEZAWA Hiroyuki aligned_vread(buf, addr, n); 2891d0107eb0SKAMEZAWA Hiroyuki else /* IOREMAP area is treated as memory hole */ 2892d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, n); 2893d0107eb0SKAMEZAWA Hiroyuki buf += n; 2894d0107eb0SKAMEZAWA Hiroyuki addr += n; 2895d0107eb0SKAMEZAWA Hiroyuki count -= n; 28961da177e4SLinus Torvalds } 28971da177e4SLinus Torvalds finished: 2898e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 2899d0107eb0SKAMEZAWA Hiroyuki 2900d0107eb0SKAMEZAWA Hiroyuki if (buf == buf_start) 2901d0107eb0SKAMEZAWA Hiroyuki return 0; 2902d0107eb0SKAMEZAWA Hiroyuki /* zero-fill memory holes */ 2903d0107eb0SKAMEZAWA Hiroyuki if (buf != buf_start + buflen) 2904d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, buflen - (buf - buf_start)); 2905d0107eb0SKAMEZAWA Hiroyuki 2906d0107eb0SKAMEZAWA Hiroyuki return buflen; 29071da177e4SLinus Torvalds } 29081da177e4SLinus Torvalds 2909d0107eb0SKAMEZAWA Hiroyuki /** 2910d0107eb0SKAMEZAWA Hiroyuki * vwrite() - write vmalloc area in a safe way. 2911d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for source data 2912d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 2913d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 2914d0107eb0SKAMEZAWA Hiroyuki * 2915d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 2916d0107eb0SKAMEZAWA Hiroyuki * copy data from a buffer to the given addr. If specified range of 2917d0107eb0SKAMEZAWA Hiroyuki * [addr...addr+count) includes some valid address, data is copied from 2918d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, no copy to hole. 2919d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 2920d0107eb0SKAMEZAWA Hiroyuki * 2921d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 2922a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 2923d0107eb0SKAMEZAWA Hiroyuki * 2924d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vwrite() is never necessary because the caller 2925d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 2926d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 2927d9009d67SGeert Uytterhoeven * any information, as /dev/kmem. 2928a862f68aSMike Rapoport * 2929a862f68aSMike Rapoport * Return: number of bytes for which addr and buf should be 2930a862f68aSMike Rapoport * increased (same number as @count) or %0 if [addr...addr+count) 2931a862f68aSMike Rapoport * doesn't include any intersection with valid vmalloc area 2932d0107eb0SKAMEZAWA Hiroyuki */ 29331da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count) 29341da177e4SLinus Torvalds { 2935e81ce85fSJoonsoo Kim struct vmap_area *va; 2936e81ce85fSJoonsoo Kim struct vm_struct *vm; 2937d0107eb0SKAMEZAWA Hiroyuki char *vaddr; 2938d0107eb0SKAMEZAWA Hiroyuki unsigned long n, buflen; 2939d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 29401da177e4SLinus Torvalds 29411da177e4SLinus Torvalds /* Don't allow overflow */ 29421da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 29431da177e4SLinus Torvalds count = -(unsigned long) addr; 2944d0107eb0SKAMEZAWA Hiroyuki buflen = count; 29451da177e4SLinus Torvalds 2946e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 2947e81ce85fSJoonsoo Kim list_for_each_entry(va, &vmap_area_list, list) { 2948e81ce85fSJoonsoo Kim if (!count) 2949e81ce85fSJoonsoo Kim break; 2950e81ce85fSJoonsoo Kim 2951688fcbfcSPengfei Li if (!va->vm) 2952e81ce85fSJoonsoo Kim continue; 2953e81ce85fSJoonsoo Kim 2954e81ce85fSJoonsoo Kim vm = va->vm; 2955e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 2956762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 29571da177e4SLinus Torvalds continue; 29581da177e4SLinus Torvalds while (addr < vaddr) { 29591da177e4SLinus Torvalds if (count == 0) 29601da177e4SLinus Torvalds goto finished; 29611da177e4SLinus Torvalds buf++; 29621da177e4SLinus Torvalds addr++; 29631da177e4SLinus Torvalds count--; 29641da177e4SLinus Torvalds } 2965762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 2966d0107eb0SKAMEZAWA Hiroyuki if (n > count) 2967d0107eb0SKAMEZAWA Hiroyuki n = count; 2968e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) { 2969d0107eb0SKAMEZAWA Hiroyuki aligned_vwrite(buf, addr, n); 2970d0107eb0SKAMEZAWA Hiroyuki copied++; 2971d0107eb0SKAMEZAWA Hiroyuki } 2972d0107eb0SKAMEZAWA Hiroyuki buf += n; 2973d0107eb0SKAMEZAWA Hiroyuki addr += n; 2974d0107eb0SKAMEZAWA Hiroyuki count -= n; 29751da177e4SLinus Torvalds } 29761da177e4SLinus Torvalds finished: 2977e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 2978d0107eb0SKAMEZAWA Hiroyuki if (!copied) 2979d0107eb0SKAMEZAWA Hiroyuki return 0; 2980d0107eb0SKAMEZAWA Hiroyuki return buflen; 29811da177e4SLinus Torvalds } 298283342314SNick Piggin 298383342314SNick Piggin /** 2984e69e9d4aSHATAYAMA Daisuke * remap_vmalloc_range_partial - map vmalloc pages to userspace 2985e69e9d4aSHATAYAMA Daisuke * @vma: vma to cover 2986e69e9d4aSHATAYAMA Daisuke * @uaddr: target user address to start at 2987e69e9d4aSHATAYAMA Daisuke * @kaddr: virtual address of vmalloc kernel memory 2988bdebd6a2SJann Horn * @pgoff: offset from @kaddr to start at 2989e69e9d4aSHATAYAMA Daisuke * @size: size of map area 2990e69e9d4aSHATAYAMA Daisuke * 2991e69e9d4aSHATAYAMA Daisuke * Returns: 0 for success, -Exxx on failure 2992e69e9d4aSHATAYAMA Daisuke * 2993e69e9d4aSHATAYAMA Daisuke * This function checks that @kaddr is a valid vmalloc'ed area, 2994e69e9d4aSHATAYAMA Daisuke * and that it is big enough to cover the range starting at 2995e69e9d4aSHATAYAMA Daisuke * @uaddr in @vma. Will return failure if that criteria isn't 2996e69e9d4aSHATAYAMA Daisuke * met. 2997e69e9d4aSHATAYAMA Daisuke * 2998e69e9d4aSHATAYAMA Daisuke * Similar to remap_pfn_range() (see mm/memory.c) 2999e69e9d4aSHATAYAMA Daisuke */ 3000e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr, 3001bdebd6a2SJann Horn void *kaddr, unsigned long pgoff, 3002bdebd6a2SJann Horn unsigned long size) 3003e69e9d4aSHATAYAMA Daisuke { 3004e69e9d4aSHATAYAMA Daisuke struct vm_struct *area; 3005bdebd6a2SJann Horn unsigned long off; 3006bdebd6a2SJann Horn unsigned long end_index; 3007bdebd6a2SJann Horn 3008bdebd6a2SJann Horn if (check_shl_overflow(pgoff, PAGE_SHIFT, &off)) 3009bdebd6a2SJann Horn return -EINVAL; 3010e69e9d4aSHATAYAMA Daisuke 3011e69e9d4aSHATAYAMA Daisuke size = PAGE_ALIGN(size); 3012e69e9d4aSHATAYAMA Daisuke 3013e69e9d4aSHATAYAMA Daisuke if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr)) 3014e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3015e69e9d4aSHATAYAMA Daisuke 3016e69e9d4aSHATAYAMA Daisuke area = find_vm_area(kaddr); 3017e69e9d4aSHATAYAMA Daisuke if (!area) 3018e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3019e69e9d4aSHATAYAMA Daisuke 3020fe9041c2SChristoph Hellwig if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT))) 3021e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3022e69e9d4aSHATAYAMA Daisuke 3023bdebd6a2SJann Horn if (check_add_overflow(size, off, &end_index) || 3024bdebd6a2SJann Horn end_index > get_vm_area_size(area)) 3025e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3026bdebd6a2SJann Horn kaddr += off; 3027e69e9d4aSHATAYAMA Daisuke 3028e69e9d4aSHATAYAMA Daisuke do { 3029e69e9d4aSHATAYAMA Daisuke struct page *page = vmalloc_to_page(kaddr); 3030e69e9d4aSHATAYAMA Daisuke int ret; 3031e69e9d4aSHATAYAMA Daisuke 3032e69e9d4aSHATAYAMA Daisuke ret = vm_insert_page(vma, uaddr, page); 3033e69e9d4aSHATAYAMA Daisuke if (ret) 3034e69e9d4aSHATAYAMA Daisuke return ret; 3035e69e9d4aSHATAYAMA Daisuke 3036e69e9d4aSHATAYAMA Daisuke uaddr += PAGE_SIZE; 3037e69e9d4aSHATAYAMA Daisuke kaddr += PAGE_SIZE; 3038e69e9d4aSHATAYAMA Daisuke size -= PAGE_SIZE; 3039e69e9d4aSHATAYAMA Daisuke } while (size > 0); 3040e69e9d4aSHATAYAMA Daisuke 3041e69e9d4aSHATAYAMA Daisuke vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 3042e69e9d4aSHATAYAMA Daisuke 3043e69e9d4aSHATAYAMA Daisuke return 0; 3044e69e9d4aSHATAYAMA Daisuke } 3045e69e9d4aSHATAYAMA Daisuke EXPORT_SYMBOL(remap_vmalloc_range_partial); 3046e69e9d4aSHATAYAMA Daisuke 3047e69e9d4aSHATAYAMA Daisuke /** 304883342314SNick Piggin * remap_vmalloc_range - map vmalloc pages to userspace 304983342314SNick Piggin * @vma: vma to cover (map full range of vma) 305083342314SNick Piggin * @addr: vmalloc memory 305183342314SNick Piggin * @pgoff: number of pages into addr before first page to map 30527682486bSRandy Dunlap * 30537682486bSRandy Dunlap * Returns: 0 for success, -Exxx on failure 305483342314SNick Piggin * 305583342314SNick Piggin * This function checks that addr is a valid vmalloc'ed area, and 305683342314SNick Piggin * that it is big enough to cover the vma. Will return failure if 305783342314SNick Piggin * that criteria isn't met. 305883342314SNick Piggin * 305972fd4a35SRobert P. J. Day * Similar to remap_pfn_range() (see mm/memory.c) 306083342314SNick Piggin */ 306183342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 306283342314SNick Piggin unsigned long pgoff) 306383342314SNick Piggin { 3064e69e9d4aSHATAYAMA Daisuke return remap_vmalloc_range_partial(vma, vma->vm_start, 3065bdebd6a2SJann Horn addr, pgoff, 3066e69e9d4aSHATAYAMA Daisuke vma->vm_end - vma->vm_start); 306783342314SNick Piggin } 306883342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range); 306983342314SNick Piggin 30701eeb66a1SChristoph Hellwig /* 3071763802b5SJoerg Roedel * Implement stubs for vmalloc_sync_[un]mappings () if the architecture chose 3072763802b5SJoerg Roedel * not to have one. 30733f8fd02bSJoerg Roedel * 30743f8fd02bSJoerg Roedel * The purpose of this function is to make sure the vmalloc area 30753f8fd02bSJoerg Roedel * mappings are identical in all page-tables in the system. 30761eeb66a1SChristoph Hellwig */ 3077763802b5SJoerg Roedel void __weak vmalloc_sync_mappings(void) 30781eeb66a1SChristoph Hellwig { 30791eeb66a1SChristoph Hellwig } 30805f4352fbSJeremy Fitzhardinge 3081763802b5SJoerg Roedel void __weak vmalloc_sync_unmappings(void) 3082763802b5SJoerg Roedel { 3083763802b5SJoerg Roedel } 30845f4352fbSJeremy Fitzhardinge 30858b1e0f81SAnshuman Khandual static int f(pte_t *pte, unsigned long addr, void *data) 30865f4352fbSJeremy Fitzhardinge { 3087cd12909cSDavid Vrabel pte_t ***p = data; 3088cd12909cSDavid Vrabel 3089cd12909cSDavid Vrabel if (p) { 3090cd12909cSDavid Vrabel *(*p) = pte; 3091cd12909cSDavid Vrabel (*p)++; 3092cd12909cSDavid Vrabel } 30935f4352fbSJeremy Fitzhardinge return 0; 30945f4352fbSJeremy Fitzhardinge } 30955f4352fbSJeremy Fitzhardinge 30965f4352fbSJeremy Fitzhardinge /** 30975f4352fbSJeremy Fitzhardinge * alloc_vm_area - allocate a range of kernel address space 30985f4352fbSJeremy Fitzhardinge * @size: size of the area 3099cd12909cSDavid Vrabel * @ptes: returns the PTEs for the address space 31007682486bSRandy Dunlap * 31017682486bSRandy Dunlap * Returns: NULL on failure, vm_struct on success 31025f4352fbSJeremy Fitzhardinge * 31035f4352fbSJeremy Fitzhardinge * This function reserves a range of kernel address space, and 31045f4352fbSJeremy Fitzhardinge * allocates pagetables to map that range. No actual mappings 3105cd12909cSDavid Vrabel * are created. 3106cd12909cSDavid Vrabel * 3107cd12909cSDavid Vrabel * If @ptes is non-NULL, pointers to the PTEs (in init_mm) 3108cd12909cSDavid Vrabel * allocated for the VM area are returned. 31095f4352fbSJeremy Fitzhardinge */ 3110cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes) 31115f4352fbSJeremy Fitzhardinge { 31125f4352fbSJeremy Fitzhardinge struct vm_struct *area; 31135f4352fbSJeremy Fitzhardinge 311423016969SChristoph Lameter area = get_vm_area_caller(size, VM_IOREMAP, 311523016969SChristoph Lameter __builtin_return_address(0)); 31165f4352fbSJeremy Fitzhardinge if (area == NULL) 31175f4352fbSJeremy Fitzhardinge return NULL; 31185f4352fbSJeremy Fitzhardinge 31195f4352fbSJeremy Fitzhardinge /* 31205f4352fbSJeremy Fitzhardinge * This ensures that page tables are constructed for this region 31215f4352fbSJeremy Fitzhardinge * of kernel virtual address space and mapped into init_mm. 31225f4352fbSJeremy Fitzhardinge */ 31235f4352fbSJeremy Fitzhardinge if (apply_to_page_range(&init_mm, (unsigned long)area->addr, 3124cd12909cSDavid Vrabel size, f, ptes ? &ptes : NULL)) { 31255f4352fbSJeremy Fitzhardinge free_vm_area(area); 31265f4352fbSJeremy Fitzhardinge return NULL; 31275f4352fbSJeremy Fitzhardinge } 31285f4352fbSJeremy Fitzhardinge 31295f4352fbSJeremy Fitzhardinge return area; 31305f4352fbSJeremy Fitzhardinge } 31315f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(alloc_vm_area); 31325f4352fbSJeremy Fitzhardinge 31335f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area) 31345f4352fbSJeremy Fitzhardinge { 31355f4352fbSJeremy Fitzhardinge struct vm_struct *ret; 31365f4352fbSJeremy Fitzhardinge ret = remove_vm_area(area->addr); 31375f4352fbSJeremy Fitzhardinge BUG_ON(ret != area); 31385f4352fbSJeremy Fitzhardinge kfree(area); 31395f4352fbSJeremy Fitzhardinge } 31405f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area); 3141a10aa579SChristoph Lameter 31424f8b02b4STejun Heo #ifdef CONFIG_SMP 3143ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n) 3144ca23e405STejun Heo { 31454583e773SGeliang Tang return rb_entry_safe(n, struct vmap_area, rb_node); 3146ca23e405STejun Heo } 3147ca23e405STejun Heo 3148ca23e405STejun Heo /** 314968ad4a33SUladzislau Rezki (Sony) * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to 315068ad4a33SUladzislau Rezki (Sony) * @addr: target address 3151ca23e405STejun Heo * 315268ad4a33SUladzislau Rezki (Sony) * Returns: vmap_area if it is found. If there is no such area 315368ad4a33SUladzislau Rezki (Sony) * the first highest(reverse order) vmap_area is returned 315468ad4a33SUladzislau Rezki (Sony) * i.e. va->va_start < addr && va->va_end < addr or NULL 315568ad4a33SUladzislau Rezki (Sony) * if there are no any areas before @addr. 3156ca23e405STejun Heo */ 315768ad4a33SUladzislau Rezki (Sony) static struct vmap_area * 315868ad4a33SUladzislau Rezki (Sony) pvm_find_va_enclose_addr(unsigned long addr) 3159ca23e405STejun Heo { 316068ad4a33SUladzislau Rezki (Sony) struct vmap_area *va, *tmp; 316168ad4a33SUladzislau Rezki (Sony) struct rb_node *n; 316268ad4a33SUladzislau Rezki (Sony) 316368ad4a33SUladzislau Rezki (Sony) n = free_vmap_area_root.rb_node; 316468ad4a33SUladzislau Rezki (Sony) va = NULL; 3165ca23e405STejun Heo 3166ca23e405STejun Heo while (n) { 316768ad4a33SUladzislau Rezki (Sony) tmp = rb_entry(n, struct vmap_area, rb_node); 316868ad4a33SUladzislau Rezki (Sony) if (tmp->va_start <= addr) { 316968ad4a33SUladzislau Rezki (Sony) va = tmp; 317068ad4a33SUladzislau Rezki (Sony) if (tmp->va_end >= addr) 3171ca23e405STejun Heo break; 3172ca23e405STejun Heo 317368ad4a33SUladzislau Rezki (Sony) n = n->rb_right; 3174ca23e405STejun Heo } else { 317568ad4a33SUladzislau Rezki (Sony) n = n->rb_left; 3176ca23e405STejun Heo } 317768ad4a33SUladzislau Rezki (Sony) } 317868ad4a33SUladzislau Rezki (Sony) 317968ad4a33SUladzislau Rezki (Sony) return va; 3180ca23e405STejun Heo } 3181ca23e405STejun Heo 3182ca23e405STejun Heo /** 318368ad4a33SUladzislau Rezki (Sony) * pvm_determine_end_from_reverse - find the highest aligned address 318468ad4a33SUladzislau Rezki (Sony) * of free block below VMALLOC_END 318568ad4a33SUladzislau Rezki (Sony) * @va: 318668ad4a33SUladzislau Rezki (Sony) * in - the VA we start the search(reverse order); 318768ad4a33SUladzislau Rezki (Sony) * out - the VA with the highest aligned end address. 3188ca23e405STejun Heo * 318968ad4a33SUladzislau Rezki (Sony) * Returns: determined end address within vmap_area 3190ca23e405STejun Heo */ 319168ad4a33SUladzislau Rezki (Sony) static unsigned long 319268ad4a33SUladzislau Rezki (Sony) pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align) 3193ca23e405STejun Heo { 319468ad4a33SUladzislau Rezki (Sony) unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 3195ca23e405STejun Heo unsigned long addr; 3196ca23e405STejun Heo 319768ad4a33SUladzislau Rezki (Sony) if (likely(*va)) { 319868ad4a33SUladzislau Rezki (Sony) list_for_each_entry_from_reverse((*va), 319968ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list, list) { 320068ad4a33SUladzislau Rezki (Sony) addr = min((*va)->va_end & ~(align - 1), vmalloc_end); 320168ad4a33SUladzislau Rezki (Sony) if ((*va)->va_start < addr) 320268ad4a33SUladzislau Rezki (Sony) return addr; 320368ad4a33SUladzislau Rezki (Sony) } 3204ca23e405STejun Heo } 3205ca23e405STejun Heo 320668ad4a33SUladzislau Rezki (Sony) return 0; 3207ca23e405STejun Heo } 3208ca23e405STejun Heo 3209ca23e405STejun Heo /** 3210ca23e405STejun Heo * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator 3211ca23e405STejun Heo * @offsets: array containing offset of each area 3212ca23e405STejun Heo * @sizes: array containing size of each area 3213ca23e405STejun Heo * @nr_vms: the number of areas to allocate 3214ca23e405STejun Heo * @align: alignment, all entries in @offsets and @sizes must be aligned to this 3215ca23e405STejun Heo * 3216ca23e405STejun Heo * Returns: kmalloc'd vm_struct pointer array pointing to allocated 3217ca23e405STejun Heo * vm_structs on success, %NULL on failure 3218ca23e405STejun Heo * 3219ca23e405STejun Heo * Percpu allocator wants to use congruent vm areas so that it can 3220ca23e405STejun Heo * maintain the offsets among percpu areas. This function allocates 3221ec3f64fcSDavid Rientjes * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to 3222ec3f64fcSDavid Rientjes * be scattered pretty far, distance between two areas easily going up 3223ec3f64fcSDavid Rientjes * to gigabytes. To avoid interacting with regular vmallocs, these 3224ec3f64fcSDavid Rientjes * areas are allocated from top. 3225ca23e405STejun Heo * 3226ca23e405STejun Heo * Despite its complicated look, this allocator is rather simple. It 322768ad4a33SUladzislau Rezki (Sony) * does everything top-down and scans free blocks from the end looking 322868ad4a33SUladzislau Rezki (Sony) * for matching base. While scanning, if any of the areas do not fit the 322968ad4a33SUladzislau Rezki (Sony) * base address is pulled down to fit the area. Scanning is repeated till 323068ad4a33SUladzislau Rezki (Sony) * all the areas fit and then all necessary data structures are inserted 323168ad4a33SUladzislau Rezki (Sony) * and the result is returned. 3232ca23e405STejun Heo */ 3233ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, 3234ca23e405STejun Heo const size_t *sizes, int nr_vms, 3235ec3f64fcSDavid Rientjes size_t align) 3236ca23e405STejun Heo { 3237ca23e405STejun Heo const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); 3238ca23e405STejun Heo const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 323968ad4a33SUladzislau Rezki (Sony) struct vmap_area **vas, *va; 3240ca23e405STejun Heo struct vm_struct **vms; 3241ca23e405STejun Heo int area, area2, last_area, term_area; 3242253a496dSDaniel Axtens unsigned long base, start, size, end, last_end, orig_start, orig_end; 3243ca23e405STejun Heo bool purged = false; 324468ad4a33SUladzislau Rezki (Sony) enum fit_type type; 3245ca23e405STejun Heo 3246ca23e405STejun Heo /* verify parameters and allocate data structures */ 3247891c49abSAlexander Kuleshov BUG_ON(offset_in_page(align) || !is_power_of_2(align)); 3248ca23e405STejun Heo for (last_area = 0, area = 0; area < nr_vms; area++) { 3249ca23e405STejun Heo start = offsets[area]; 3250ca23e405STejun Heo end = start + sizes[area]; 3251ca23e405STejun Heo 3252ca23e405STejun Heo /* is everything aligned properly? */ 3253ca23e405STejun Heo BUG_ON(!IS_ALIGNED(offsets[area], align)); 3254ca23e405STejun Heo BUG_ON(!IS_ALIGNED(sizes[area], align)); 3255ca23e405STejun Heo 3256ca23e405STejun Heo /* detect the area with the highest address */ 3257ca23e405STejun Heo if (start > offsets[last_area]) 3258ca23e405STejun Heo last_area = area; 3259ca23e405STejun Heo 3260c568da28SWei Yang for (area2 = area + 1; area2 < nr_vms; area2++) { 3261ca23e405STejun Heo unsigned long start2 = offsets[area2]; 3262ca23e405STejun Heo unsigned long end2 = start2 + sizes[area2]; 3263ca23e405STejun Heo 3264c568da28SWei Yang BUG_ON(start2 < end && start < end2); 3265ca23e405STejun Heo } 3266ca23e405STejun Heo } 3267ca23e405STejun Heo last_end = offsets[last_area] + sizes[last_area]; 3268ca23e405STejun Heo 3269ca23e405STejun Heo if (vmalloc_end - vmalloc_start < last_end) { 3270ca23e405STejun Heo WARN_ON(true); 3271ca23e405STejun Heo return NULL; 3272ca23e405STejun Heo } 3273ca23e405STejun Heo 32744d67d860SThomas Meyer vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL); 32754d67d860SThomas Meyer vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL); 3276ca23e405STejun Heo if (!vas || !vms) 3277f1db7afdSKautuk Consul goto err_free2; 3278ca23e405STejun Heo 3279ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 328068ad4a33SUladzislau Rezki (Sony) vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL); 3281ec3f64fcSDavid Rientjes vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL); 3282ca23e405STejun Heo if (!vas[area] || !vms[area]) 3283ca23e405STejun Heo goto err_free; 3284ca23e405STejun Heo } 3285ca23e405STejun Heo retry: 3286e36176beSUladzislau Rezki (Sony) spin_lock(&free_vmap_area_lock); 3287ca23e405STejun Heo 3288ca23e405STejun Heo /* start scanning - we scan from the top, begin with the last area */ 3289ca23e405STejun Heo area = term_area = last_area; 3290ca23e405STejun Heo start = offsets[area]; 3291ca23e405STejun Heo end = start + sizes[area]; 3292ca23e405STejun Heo 329368ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(vmalloc_end); 329468ad4a33SUladzislau Rezki (Sony) base = pvm_determine_end_from_reverse(&va, align) - end; 3295ca23e405STejun Heo 3296ca23e405STejun Heo while (true) { 3297ca23e405STejun Heo /* 3298ca23e405STejun Heo * base might have underflowed, add last_end before 3299ca23e405STejun Heo * comparing. 3300ca23e405STejun Heo */ 330168ad4a33SUladzislau Rezki (Sony) if (base + last_end < vmalloc_start + last_end) 330268ad4a33SUladzislau Rezki (Sony) goto overflow; 3303ca23e405STejun Heo 3304ca23e405STejun Heo /* 330568ad4a33SUladzislau Rezki (Sony) * Fitting base has not been found. 3306ca23e405STejun Heo */ 330768ad4a33SUladzislau Rezki (Sony) if (va == NULL) 330868ad4a33SUladzislau Rezki (Sony) goto overflow; 3309ca23e405STejun Heo 3310ca23e405STejun Heo /* 3311d8cc323dSQiujun Huang * If required width exceeds current VA block, move 33125336e52cSKuppuswamy Sathyanarayanan * base downwards and then recheck. 33135336e52cSKuppuswamy Sathyanarayanan */ 33145336e52cSKuppuswamy Sathyanarayanan if (base + end > va->va_end) { 33155336e52cSKuppuswamy Sathyanarayanan base = pvm_determine_end_from_reverse(&va, align) - end; 33165336e52cSKuppuswamy Sathyanarayanan term_area = area; 33175336e52cSKuppuswamy Sathyanarayanan continue; 33185336e52cSKuppuswamy Sathyanarayanan } 33195336e52cSKuppuswamy Sathyanarayanan 33205336e52cSKuppuswamy Sathyanarayanan /* 332168ad4a33SUladzislau Rezki (Sony) * If this VA does not fit, move base downwards and recheck. 3322ca23e405STejun Heo */ 33235336e52cSKuppuswamy Sathyanarayanan if (base + start < va->va_start) { 332468ad4a33SUladzislau Rezki (Sony) va = node_to_va(rb_prev(&va->rb_node)); 332568ad4a33SUladzislau Rezki (Sony) base = pvm_determine_end_from_reverse(&va, align) - end; 3326ca23e405STejun Heo term_area = area; 3327ca23e405STejun Heo continue; 3328ca23e405STejun Heo } 3329ca23e405STejun Heo 3330ca23e405STejun Heo /* 3331ca23e405STejun Heo * This area fits, move on to the previous one. If 3332ca23e405STejun Heo * the previous one is the terminal one, we're done. 3333ca23e405STejun Heo */ 3334ca23e405STejun Heo area = (area + nr_vms - 1) % nr_vms; 3335ca23e405STejun Heo if (area == term_area) 3336ca23e405STejun Heo break; 333768ad4a33SUladzislau Rezki (Sony) 3338ca23e405STejun Heo start = offsets[area]; 3339ca23e405STejun Heo end = start + sizes[area]; 334068ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(base + end); 3341ca23e405STejun Heo } 334268ad4a33SUladzislau Rezki (Sony) 3343ca23e405STejun Heo /* we've found a fitting base, insert all va's */ 3344ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 334568ad4a33SUladzislau Rezki (Sony) int ret; 3346ca23e405STejun Heo 334768ad4a33SUladzislau Rezki (Sony) start = base + offsets[area]; 334868ad4a33SUladzislau Rezki (Sony) size = sizes[area]; 334968ad4a33SUladzislau Rezki (Sony) 335068ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(start); 335168ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(va == NULL)) 335268ad4a33SUladzislau Rezki (Sony) /* It is a BUG(), but trigger recovery instead. */ 335368ad4a33SUladzislau Rezki (Sony) goto recovery; 335468ad4a33SUladzislau Rezki (Sony) 335568ad4a33SUladzislau Rezki (Sony) type = classify_va_fit_type(va, start, size); 335668ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(type == NOTHING_FIT)) 335768ad4a33SUladzislau Rezki (Sony) /* It is a BUG(), but trigger recovery instead. */ 335868ad4a33SUladzislau Rezki (Sony) goto recovery; 335968ad4a33SUladzislau Rezki (Sony) 336068ad4a33SUladzislau Rezki (Sony) ret = adjust_va_to_fit_type(va, start, size, type); 336168ad4a33SUladzislau Rezki (Sony) if (unlikely(ret)) 336268ad4a33SUladzislau Rezki (Sony) goto recovery; 336368ad4a33SUladzislau Rezki (Sony) 336468ad4a33SUladzislau Rezki (Sony) /* Allocated area. */ 336568ad4a33SUladzislau Rezki (Sony) va = vas[area]; 336668ad4a33SUladzislau Rezki (Sony) va->va_start = start; 336768ad4a33SUladzislau Rezki (Sony) va->va_end = start + size; 3368ca23e405STejun Heo } 3369ca23e405STejun Heo 3370e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 3371ca23e405STejun Heo 3372253a496dSDaniel Axtens /* populate the kasan shadow space */ 3373253a496dSDaniel Axtens for (area = 0; area < nr_vms; area++) { 3374253a496dSDaniel Axtens if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area])) 3375253a496dSDaniel Axtens goto err_free_shadow; 3376253a496dSDaniel Axtens 3377253a496dSDaniel Axtens kasan_unpoison_vmalloc((void *)vas[area]->va_start, 3378253a496dSDaniel Axtens sizes[area]); 3379253a496dSDaniel Axtens } 3380253a496dSDaniel Axtens 3381ca23e405STejun Heo /* insert all vm's */ 3382e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 3383e36176beSUladzislau Rezki (Sony) for (area = 0; area < nr_vms; area++) { 3384e36176beSUladzislau Rezki (Sony) insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list); 3385e36176beSUladzislau Rezki (Sony) 3386e36176beSUladzislau Rezki (Sony) setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC, 3387ca23e405STejun Heo pcpu_get_vm_areas); 3388e36176beSUladzislau Rezki (Sony) } 3389e36176beSUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 3390ca23e405STejun Heo 3391ca23e405STejun Heo kfree(vas); 3392ca23e405STejun Heo return vms; 3393ca23e405STejun Heo 339468ad4a33SUladzislau Rezki (Sony) recovery: 3395e36176beSUladzislau Rezki (Sony) /* 3396e36176beSUladzislau Rezki (Sony) * Remove previously allocated areas. There is no 3397e36176beSUladzislau Rezki (Sony) * need in removing these areas from the busy tree, 3398e36176beSUladzislau Rezki (Sony) * because they are inserted only on the final step 3399e36176beSUladzislau Rezki (Sony) * and when pcpu_get_vm_areas() is success. 3400e36176beSUladzislau Rezki (Sony) */ 340168ad4a33SUladzislau Rezki (Sony) while (area--) { 3402253a496dSDaniel Axtens orig_start = vas[area]->va_start; 3403253a496dSDaniel Axtens orig_end = vas[area]->va_end; 3404253a496dSDaniel Axtens va = merge_or_add_vmap_area(vas[area], &free_vmap_area_root, 34053c5c3cfbSDaniel Axtens &free_vmap_area_list); 3406253a496dSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 3407253a496dSDaniel Axtens va->va_start, va->va_end); 340868ad4a33SUladzislau Rezki (Sony) vas[area] = NULL; 340968ad4a33SUladzislau Rezki (Sony) } 341068ad4a33SUladzislau Rezki (Sony) 341168ad4a33SUladzislau Rezki (Sony) overflow: 3412e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 341368ad4a33SUladzislau Rezki (Sony) if (!purged) { 341468ad4a33SUladzislau Rezki (Sony) purge_vmap_area_lazy(); 341568ad4a33SUladzislau Rezki (Sony) purged = true; 341668ad4a33SUladzislau Rezki (Sony) 341768ad4a33SUladzislau Rezki (Sony) /* Before "retry", check if we recover. */ 341868ad4a33SUladzislau Rezki (Sony) for (area = 0; area < nr_vms; area++) { 341968ad4a33SUladzislau Rezki (Sony) if (vas[area]) 342068ad4a33SUladzislau Rezki (Sony) continue; 342168ad4a33SUladzislau Rezki (Sony) 342268ad4a33SUladzislau Rezki (Sony) vas[area] = kmem_cache_zalloc( 342368ad4a33SUladzislau Rezki (Sony) vmap_area_cachep, GFP_KERNEL); 342468ad4a33SUladzislau Rezki (Sony) if (!vas[area]) 342568ad4a33SUladzislau Rezki (Sony) goto err_free; 342668ad4a33SUladzislau Rezki (Sony) } 342768ad4a33SUladzislau Rezki (Sony) 342868ad4a33SUladzislau Rezki (Sony) goto retry; 342968ad4a33SUladzislau Rezki (Sony) } 343068ad4a33SUladzislau Rezki (Sony) 3431ca23e405STejun Heo err_free: 3432ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 343368ad4a33SUladzislau Rezki (Sony) if (vas[area]) 343468ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, vas[area]); 343568ad4a33SUladzislau Rezki (Sony) 3436ca23e405STejun Heo kfree(vms[area]); 3437ca23e405STejun Heo } 3438f1db7afdSKautuk Consul err_free2: 3439ca23e405STejun Heo kfree(vas); 3440ca23e405STejun Heo kfree(vms); 3441ca23e405STejun Heo return NULL; 3442253a496dSDaniel Axtens 3443253a496dSDaniel Axtens err_free_shadow: 3444253a496dSDaniel Axtens spin_lock(&free_vmap_area_lock); 3445253a496dSDaniel Axtens /* 3446253a496dSDaniel Axtens * We release all the vmalloc shadows, even the ones for regions that 3447253a496dSDaniel Axtens * hadn't been successfully added. This relies on kasan_release_vmalloc 3448253a496dSDaniel Axtens * being able to tolerate this case. 3449253a496dSDaniel Axtens */ 3450253a496dSDaniel Axtens for (area = 0; area < nr_vms; area++) { 3451253a496dSDaniel Axtens orig_start = vas[area]->va_start; 3452253a496dSDaniel Axtens orig_end = vas[area]->va_end; 3453253a496dSDaniel Axtens va = merge_or_add_vmap_area(vas[area], &free_vmap_area_root, 3454253a496dSDaniel Axtens &free_vmap_area_list); 3455253a496dSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 3456253a496dSDaniel Axtens va->va_start, va->va_end); 3457253a496dSDaniel Axtens vas[area] = NULL; 3458253a496dSDaniel Axtens kfree(vms[area]); 3459253a496dSDaniel Axtens } 3460253a496dSDaniel Axtens spin_unlock(&free_vmap_area_lock); 3461253a496dSDaniel Axtens kfree(vas); 3462253a496dSDaniel Axtens kfree(vms); 3463253a496dSDaniel Axtens return NULL; 3464ca23e405STejun Heo } 3465ca23e405STejun Heo 3466ca23e405STejun Heo /** 3467ca23e405STejun Heo * pcpu_free_vm_areas - free vmalloc areas for percpu allocator 3468ca23e405STejun Heo * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() 3469ca23e405STejun Heo * @nr_vms: the number of allocated areas 3470ca23e405STejun Heo * 3471ca23e405STejun Heo * Free vm_structs and the array allocated by pcpu_get_vm_areas(). 3472ca23e405STejun Heo */ 3473ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) 3474ca23e405STejun Heo { 3475ca23e405STejun Heo int i; 3476ca23e405STejun Heo 3477ca23e405STejun Heo for (i = 0; i < nr_vms; i++) 3478ca23e405STejun Heo free_vm_area(vms[i]); 3479ca23e405STejun Heo kfree(vms); 3480ca23e405STejun Heo } 34814f8b02b4STejun Heo #endif /* CONFIG_SMP */ 3482a10aa579SChristoph Lameter 3483a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS 3484a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos) 3485e36176beSUladzislau Rezki (Sony) __acquires(&vmap_purge_lock) 3486d4033afdSJoonsoo Kim __acquires(&vmap_area_lock) 3487a10aa579SChristoph Lameter { 3488e36176beSUladzislau Rezki (Sony) mutex_lock(&vmap_purge_lock); 3489d4033afdSJoonsoo Kim spin_lock(&vmap_area_lock); 3490e36176beSUladzislau Rezki (Sony) 34913f500069Szijun_hu return seq_list_start(&vmap_area_list, *pos); 3492a10aa579SChristoph Lameter } 3493a10aa579SChristoph Lameter 3494a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos) 3495a10aa579SChristoph Lameter { 34963f500069Szijun_hu return seq_list_next(p, &vmap_area_list, pos); 3497a10aa579SChristoph Lameter } 3498a10aa579SChristoph Lameter 3499a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p) 3500e36176beSUladzislau Rezki (Sony) __releases(&vmap_purge_lock) 3501d4033afdSJoonsoo Kim __releases(&vmap_area_lock) 3502a10aa579SChristoph Lameter { 3503e36176beSUladzislau Rezki (Sony) mutex_unlock(&vmap_purge_lock); 3504d4033afdSJoonsoo Kim spin_unlock(&vmap_area_lock); 3505a10aa579SChristoph Lameter } 3506a10aa579SChristoph Lameter 3507a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v) 3508a47a126aSEric Dumazet { 3509e5adfffcSKirill A. Shutemov if (IS_ENABLED(CONFIG_NUMA)) { 3510a47a126aSEric Dumazet unsigned int nr, *counters = m->private; 3511a47a126aSEric Dumazet 3512a47a126aSEric Dumazet if (!counters) 3513a47a126aSEric Dumazet return; 3514a47a126aSEric Dumazet 3515af12346cSWanpeng Li if (v->flags & VM_UNINITIALIZED) 3516af12346cSWanpeng Li return; 35177e5b528bSDmitry Vyukov /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ 35187e5b528bSDmitry Vyukov smp_rmb(); 3519af12346cSWanpeng Li 3520a47a126aSEric Dumazet memset(counters, 0, nr_node_ids * sizeof(unsigned int)); 3521a47a126aSEric Dumazet 3522a47a126aSEric Dumazet for (nr = 0; nr < v->nr_pages; nr++) 3523a47a126aSEric Dumazet counters[page_to_nid(v->pages[nr])]++; 3524a47a126aSEric Dumazet 3525a47a126aSEric Dumazet for_each_node_state(nr, N_HIGH_MEMORY) 3526a47a126aSEric Dumazet if (counters[nr]) 3527a47a126aSEric Dumazet seq_printf(m, " N%u=%u", nr, counters[nr]); 3528a47a126aSEric Dumazet } 3529a47a126aSEric Dumazet } 3530a47a126aSEric Dumazet 3531dd3b8353SUladzislau Rezki (Sony) static void show_purge_info(struct seq_file *m) 3532dd3b8353SUladzislau Rezki (Sony) { 3533dd3b8353SUladzislau Rezki (Sony) struct llist_node *head; 3534dd3b8353SUladzislau Rezki (Sony) struct vmap_area *va; 3535dd3b8353SUladzislau Rezki (Sony) 3536dd3b8353SUladzislau Rezki (Sony) head = READ_ONCE(vmap_purge_list.first); 3537dd3b8353SUladzislau Rezki (Sony) if (head == NULL) 3538dd3b8353SUladzislau Rezki (Sony) return; 3539dd3b8353SUladzislau Rezki (Sony) 3540dd3b8353SUladzislau Rezki (Sony) llist_for_each_entry(va, head, purge_list) { 3541dd3b8353SUladzislau Rezki (Sony) seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n", 3542dd3b8353SUladzislau Rezki (Sony) (void *)va->va_start, (void *)va->va_end, 3543dd3b8353SUladzislau Rezki (Sony) va->va_end - va->va_start); 3544dd3b8353SUladzislau Rezki (Sony) } 3545dd3b8353SUladzislau Rezki (Sony) } 3546dd3b8353SUladzislau Rezki (Sony) 3547a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p) 3548a10aa579SChristoph Lameter { 35493f500069Szijun_hu struct vmap_area *va; 3550d4033afdSJoonsoo Kim struct vm_struct *v; 3551d4033afdSJoonsoo Kim 35523f500069Szijun_hu va = list_entry(p, struct vmap_area, list); 35533f500069Szijun_hu 3554c2ce8c14SWanpeng Li /* 3555688fcbfcSPengfei Li * s_show can encounter race with remove_vm_area, !vm on behalf 3556688fcbfcSPengfei Li * of vmap area is being tear down or vm_map_ram allocation. 3557c2ce8c14SWanpeng Li */ 3558688fcbfcSPengfei Li if (!va->vm) { 3559dd3b8353SUladzislau Rezki (Sony) seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n", 356078c72746SYisheng Xie (void *)va->va_start, (void *)va->va_end, 3561dd3b8353SUladzislau Rezki (Sony) va->va_end - va->va_start); 356278c72746SYisheng Xie 3563d4033afdSJoonsoo Kim return 0; 356478c72746SYisheng Xie } 3565d4033afdSJoonsoo Kim 3566d4033afdSJoonsoo Kim v = va->vm; 3567a10aa579SChristoph Lameter 356845ec1690SKees Cook seq_printf(m, "0x%pK-0x%pK %7ld", 3569a10aa579SChristoph Lameter v->addr, v->addr + v->size, v->size); 3570a10aa579SChristoph Lameter 357162c70bceSJoe Perches if (v->caller) 357262c70bceSJoe Perches seq_printf(m, " %pS", v->caller); 357323016969SChristoph Lameter 3574a10aa579SChristoph Lameter if (v->nr_pages) 3575a10aa579SChristoph Lameter seq_printf(m, " pages=%d", v->nr_pages); 3576a10aa579SChristoph Lameter 3577a10aa579SChristoph Lameter if (v->phys_addr) 3578199eaa05SMiles Chen seq_printf(m, " phys=%pa", &v->phys_addr); 3579a10aa579SChristoph Lameter 3580a10aa579SChristoph Lameter if (v->flags & VM_IOREMAP) 3581f4527c90SFabian Frederick seq_puts(m, " ioremap"); 3582a10aa579SChristoph Lameter 3583a10aa579SChristoph Lameter if (v->flags & VM_ALLOC) 3584f4527c90SFabian Frederick seq_puts(m, " vmalloc"); 3585a10aa579SChristoph Lameter 3586a10aa579SChristoph Lameter if (v->flags & VM_MAP) 3587f4527c90SFabian Frederick seq_puts(m, " vmap"); 3588a10aa579SChristoph Lameter 3589a10aa579SChristoph Lameter if (v->flags & VM_USERMAP) 3590f4527c90SFabian Frederick seq_puts(m, " user"); 3591a10aa579SChristoph Lameter 3592fe9041c2SChristoph Hellwig if (v->flags & VM_DMA_COHERENT) 3593fe9041c2SChristoph Hellwig seq_puts(m, " dma-coherent"); 3594fe9041c2SChristoph Hellwig 3595244d63eeSDavid Rientjes if (is_vmalloc_addr(v->pages)) 3596f4527c90SFabian Frederick seq_puts(m, " vpages"); 3597a10aa579SChristoph Lameter 3598a47a126aSEric Dumazet show_numa_info(m, v); 3599a10aa579SChristoph Lameter seq_putc(m, '\n'); 3600dd3b8353SUladzislau Rezki (Sony) 3601dd3b8353SUladzislau Rezki (Sony) /* 3602dd3b8353SUladzislau Rezki (Sony) * As a final step, dump "unpurged" areas. Note, 3603dd3b8353SUladzislau Rezki (Sony) * that entire "/proc/vmallocinfo" output will not 3604dd3b8353SUladzislau Rezki (Sony) * be address sorted, because the purge list is not 3605dd3b8353SUladzislau Rezki (Sony) * sorted. 3606dd3b8353SUladzislau Rezki (Sony) */ 3607dd3b8353SUladzislau Rezki (Sony) if (list_is_last(&va->list, &vmap_area_list)) 3608dd3b8353SUladzislau Rezki (Sony) show_purge_info(m); 3609dd3b8353SUladzislau Rezki (Sony) 3610a10aa579SChristoph Lameter return 0; 3611a10aa579SChristoph Lameter } 3612a10aa579SChristoph Lameter 36135f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = { 3614a10aa579SChristoph Lameter .start = s_start, 3615a10aa579SChristoph Lameter .next = s_next, 3616a10aa579SChristoph Lameter .stop = s_stop, 3617a10aa579SChristoph Lameter .show = s_show, 3618a10aa579SChristoph Lameter }; 36195f6a6a9cSAlexey Dobriyan 36205f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void) 36215f6a6a9cSAlexey Dobriyan { 3622fddda2b7SChristoph Hellwig if (IS_ENABLED(CONFIG_NUMA)) 36230825a6f9SJoe Perches proc_create_seq_private("vmallocinfo", 0400, NULL, 362444414d82SChristoph Hellwig &vmalloc_op, 362544414d82SChristoph Hellwig nr_node_ids * sizeof(unsigned int), NULL); 3626fddda2b7SChristoph Hellwig else 36270825a6f9SJoe Perches proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op); 36285f6a6a9cSAlexey Dobriyan return 0; 36295f6a6a9cSAlexey Dobriyan } 36305f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init); 3631db3808c1SJoonsoo Kim 3632a10aa579SChristoph Lameter #endif 3633