1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Copyright (C) 1993 Linus Torvalds 41da177e4SLinus Torvalds * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 51da177e4SLinus Torvalds * SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000 61da177e4SLinus Torvalds * Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002 7930fc45aSChristoph Lameter * Numa awareness, Christoph Lameter, SGI, June 2005 8d758ffe6SUladzislau Rezki (Sony) * Improving global KVA allocator, Uladzislau Rezki, Sony, May 2019 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 11db64fe02SNick Piggin #include <linux/vmalloc.h> 121da177e4SLinus Torvalds #include <linux/mm.h> 131da177e4SLinus Torvalds #include <linux/module.h> 141da177e4SLinus Torvalds #include <linux/highmem.h> 15c3edc401SIngo Molnar #include <linux/sched/signal.h> 161da177e4SLinus Torvalds #include <linux/slab.h> 171da177e4SLinus Torvalds #include <linux/spinlock.h> 181da177e4SLinus Torvalds #include <linux/interrupt.h> 195f6a6a9cSAlexey Dobriyan #include <linux/proc_fs.h> 20a10aa579SChristoph Lameter #include <linux/seq_file.h> 21868b104dSRick Edgecombe #include <linux/set_memory.h> 223ac7fe5aSThomas Gleixner #include <linux/debugobjects.h> 2323016969SChristoph Lameter #include <linux/kallsyms.h> 24db64fe02SNick Piggin #include <linux/list.h> 254da56b99SChris Wilson #include <linux/notifier.h> 26db64fe02SNick Piggin #include <linux/rbtree.h> 270f14599cSMatthew Wilcox (Oracle) #include <linux/xarray.h> 28db64fe02SNick Piggin #include <linux/rcupdate.h> 29f0aa6617STejun Heo #include <linux/pfn.h> 3089219d37SCatalin Marinas #include <linux/kmemleak.h> 3160063497SArun Sharma #include <linux/atomic.h> 323b32123dSGideon Israel Dsouza #include <linux/compiler.h> 3332fcfd40SAl Viro #include <linux/llist.h> 340f616be1SToshi Kani #include <linux/bitops.h> 3568ad4a33SUladzislau Rezki (Sony) #include <linux/rbtree_augmented.h> 36bdebd6a2SJann Horn #include <linux/overflow.h> 37*c0eb315aSNicholas Piggin #include <linux/pgtable.h> 387c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 391da177e4SLinus Torvalds #include <asm/tlbflush.h> 402dca6999SDavid Miller #include <asm/shmparam.h> 411da177e4SLinus Torvalds 42dd56b046SMel Gorman #include "internal.h" 432a681cfaSJoerg Roedel #include "pgalloc-track.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 722ba3e694SJoerg Roedel static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 732ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 741da177e4SLinus Torvalds { 751da177e4SLinus Torvalds pte_t *pte; 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds pte = pte_offset_kernel(pmd, addr); 781da177e4SLinus Torvalds do { 791da177e4SLinus Torvalds pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte); 801da177e4SLinus Torvalds WARN_ON(!pte_none(ptent) && !pte_present(ptent)); 811da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 822ba3e694SJoerg Roedel *mask |= PGTBL_PTE_MODIFIED; 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 852ba3e694SJoerg Roedel static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end, 862ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 871da177e4SLinus Torvalds { 881da177e4SLinus Torvalds pmd_t *pmd; 891da177e4SLinus Torvalds unsigned long next; 902ba3e694SJoerg Roedel int cleared; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 931da177e4SLinus Torvalds do { 941da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 952ba3e694SJoerg Roedel 962ba3e694SJoerg Roedel cleared = pmd_clear_huge(pmd); 972ba3e694SJoerg Roedel if (cleared || pmd_bad(*pmd)) 982ba3e694SJoerg Roedel *mask |= PGTBL_PMD_MODIFIED; 992ba3e694SJoerg Roedel 1002ba3e694SJoerg Roedel if (cleared) 101b9820d8fSToshi Kani continue; 1021da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 1031da177e4SLinus Torvalds continue; 1042ba3e694SJoerg Roedel vunmap_pte_range(pmd, addr, next, mask); 105e47110e9SAneesh Kumar K.V 106e47110e9SAneesh Kumar K.V cond_resched(); 1071da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1102ba3e694SJoerg Roedel static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, 1112ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 1121da177e4SLinus Torvalds { 1131da177e4SLinus Torvalds pud_t *pud; 1141da177e4SLinus Torvalds unsigned long next; 1152ba3e694SJoerg Roedel int cleared; 1161da177e4SLinus Torvalds 117c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 1181da177e4SLinus Torvalds do { 1191da177e4SLinus Torvalds next = pud_addr_end(addr, end); 1202ba3e694SJoerg Roedel 1212ba3e694SJoerg Roedel cleared = pud_clear_huge(pud); 1222ba3e694SJoerg Roedel if (cleared || pud_bad(*pud)) 1232ba3e694SJoerg Roedel *mask |= PGTBL_PUD_MODIFIED; 1242ba3e694SJoerg Roedel 1252ba3e694SJoerg Roedel if (cleared) 126b9820d8fSToshi Kani continue; 1271da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 1281da177e4SLinus Torvalds continue; 1292ba3e694SJoerg Roedel vunmap_pmd_range(pud, addr, next, mask); 1301da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 1332ba3e694SJoerg Roedel static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end, 1342ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 135c2febafcSKirill A. Shutemov { 136c2febafcSKirill A. Shutemov p4d_t *p4d; 137c2febafcSKirill A. Shutemov unsigned long next; 1382ba3e694SJoerg Roedel int cleared; 139c2febafcSKirill A. Shutemov 140c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 141c2febafcSKirill A. Shutemov do { 142c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1432ba3e694SJoerg Roedel 1442ba3e694SJoerg Roedel cleared = p4d_clear_huge(p4d); 1452ba3e694SJoerg Roedel if (cleared || p4d_bad(*p4d)) 1462ba3e694SJoerg Roedel *mask |= PGTBL_P4D_MODIFIED; 1472ba3e694SJoerg Roedel 1482ba3e694SJoerg Roedel if (cleared) 149c2febafcSKirill A. Shutemov continue; 150c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 151c2febafcSKirill A. Shutemov continue; 1522ba3e694SJoerg Roedel vunmap_pud_range(p4d, addr, next, mask); 153c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 154c2febafcSKirill A. Shutemov } 155c2febafcSKirill A. Shutemov 156b521c43fSChristoph Hellwig /** 157b521c43fSChristoph Hellwig * unmap_kernel_range_noflush - unmap kernel VM area 1582ba3e694SJoerg Roedel * @start: start of the VM area to unmap 159b521c43fSChristoph Hellwig * @size: size of the VM area to unmap 160b521c43fSChristoph Hellwig * 161b521c43fSChristoph Hellwig * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size specify 162b521c43fSChristoph Hellwig * should have been allocated using get_vm_area() and its friends. 163b521c43fSChristoph Hellwig * 164b521c43fSChristoph Hellwig * NOTE: 165b521c43fSChristoph Hellwig * This function does NOT do any cache flushing. The caller is responsible 166b521c43fSChristoph Hellwig * for calling flush_cache_vunmap() on to-be-mapped areas before calling this 167b521c43fSChristoph Hellwig * function and flush_tlb_kernel_range() after. 168b521c43fSChristoph Hellwig */ 1692ba3e694SJoerg Roedel void unmap_kernel_range_noflush(unsigned long start, unsigned long size) 1701da177e4SLinus Torvalds { 1712ba3e694SJoerg Roedel unsigned long end = start + size; 1721da177e4SLinus Torvalds unsigned long next; 173b521c43fSChristoph Hellwig pgd_t *pgd; 1742ba3e694SJoerg Roedel unsigned long addr = start; 1752ba3e694SJoerg Roedel pgtbl_mod_mask mask = 0; 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds BUG_ON(addr >= end); 1781da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 1791da177e4SLinus Torvalds do { 1801da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 1812ba3e694SJoerg Roedel if (pgd_bad(*pgd)) 1822ba3e694SJoerg Roedel mask |= PGTBL_PGD_MODIFIED; 1831da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 1841da177e4SLinus Torvalds continue; 1852ba3e694SJoerg Roedel vunmap_p4d_range(pgd, addr, next, &mask); 1861da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 1872ba3e694SJoerg Roedel 1882ba3e694SJoerg Roedel if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 1892ba3e694SJoerg Roedel arch_sync_kernel_mappings(start, end); 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds static int vmap_pte_range(pmd_t *pmd, unsigned long addr, 1932ba3e694SJoerg Roedel unsigned long end, pgprot_t prot, struct page **pages, int *nr, 1942ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 1951da177e4SLinus Torvalds { 1961da177e4SLinus Torvalds pte_t *pte; 1971da177e4SLinus Torvalds 198db64fe02SNick Piggin /* 199db64fe02SNick Piggin * nr is a running index into the array which helps higher level 200db64fe02SNick Piggin * callers keep track of where we're up to. 201db64fe02SNick Piggin */ 202db64fe02SNick Piggin 2032ba3e694SJoerg Roedel pte = pte_alloc_kernel_track(pmd, addr, mask); 2041da177e4SLinus Torvalds if (!pte) 2051da177e4SLinus Torvalds return -ENOMEM; 2061da177e4SLinus Torvalds do { 207db64fe02SNick Piggin struct page *page = pages[*nr]; 208db64fe02SNick Piggin 209db64fe02SNick Piggin if (WARN_ON(!pte_none(*pte))) 210db64fe02SNick Piggin return -EBUSY; 211db64fe02SNick Piggin if (WARN_ON(!page)) 2121da177e4SLinus Torvalds return -ENOMEM; 2131da177e4SLinus Torvalds set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); 214db64fe02SNick Piggin (*nr)++; 2151da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 2162ba3e694SJoerg Roedel *mask |= PGTBL_PTE_MODIFIED; 2171da177e4SLinus Torvalds return 0; 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 220db64fe02SNick Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr, 2212ba3e694SJoerg Roedel unsigned long end, pgprot_t prot, struct page **pages, int *nr, 2222ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 2231da177e4SLinus Torvalds { 2241da177e4SLinus Torvalds pmd_t *pmd; 2251da177e4SLinus Torvalds unsigned long next; 2261da177e4SLinus Torvalds 2272ba3e694SJoerg Roedel pmd = pmd_alloc_track(&init_mm, pud, addr, mask); 2281da177e4SLinus Torvalds if (!pmd) 2291da177e4SLinus Torvalds return -ENOMEM; 2301da177e4SLinus Torvalds do { 2311da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 2322ba3e694SJoerg Roedel if (vmap_pte_range(pmd, addr, next, prot, pages, nr, mask)) 2331da177e4SLinus Torvalds return -ENOMEM; 2341da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 2351da177e4SLinus Torvalds return 0; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 238c2febafcSKirill A. Shutemov static int vmap_pud_range(p4d_t *p4d, unsigned long addr, 2392ba3e694SJoerg Roedel unsigned long end, pgprot_t prot, struct page **pages, int *nr, 2402ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 2411da177e4SLinus Torvalds { 2421da177e4SLinus Torvalds pud_t *pud; 2431da177e4SLinus Torvalds unsigned long next; 2441da177e4SLinus Torvalds 2452ba3e694SJoerg Roedel pud = pud_alloc_track(&init_mm, p4d, addr, mask); 2461da177e4SLinus Torvalds if (!pud) 2471da177e4SLinus Torvalds return -ENOMEM; 2481da177e4SLinus Torvalds do { 2491da177e4SLinus Torvalds next = pud_addr_end(addr, end); 2502ba3e694SJoerg Roedel if (vmap_pmd_range(pud, addr, next, prot, pages, nr, mask)) 2511da177e4SLinus Torvalds return -ENOMEM; 2521da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 2531da177e4SLinus Torvalds return 0; 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 256c2febafcSKirill A. Shutemov static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, 2572ba3e694SJoerg Roedel unsigned long end, pgprot_t prot, struct page **pages, int *nr, 2582ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 259c2febafcSKirill A. Shutemov { 260c2febafcSKirill A. Shutemov p4d_t *p4d; 261c2febafcSKirill A. Shutemov unsigned long next; 262c2febafcSKirill A. Shutemov 2632ba3e694SJoerg Roedel p4d = p4d_alloc_track(&init_mm, pgd, addr, mask); 264c2febafcSKirill A. Shutemov if (!p4d) 265c2febafcSKirill A. Shutemov return -ENOMEM; 266c2febafcSKirill A. Shutemov do { 267c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 2682ba3e694SJoerg Roedel if (vmap_pud_range(p4d, addr, next, prot, pages, nr, mask)) 269c2febafcSKirill A. Shutemov return -ENOMEM; 270c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 271c2febafcSKirill A. Shutemov return 0; 272c2febafcSKirill A. Shutemov } 273c2febafcSKirill A. Shutemov 274b521c43fSChristoph Hellwig /** 275b521c43fSChristoph Hellwig * map_kernel_range_noflush - map kernel VM area with the specified pages 276b521c43fSChristoph Hellwig * @addr: start of the VM area to map 277b521c43fSChristoph Hellwig * @size: size of the VM area to map 278b521c43fSChristoph Hellwig * @prot: page protection flags to use 279b521c43fSChristoph Hellwig * @pages: pages to map 280db64fe02SNick Piggin * 281b521c43fSChristoph Hellwig * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size specify should 282b521c43fSChristoph Hellwig * have been allocated using get_vm_area() and its friends. 283b521c43fSChristoph Hellwig * 284b521c43fSChristoph Hellwig * NOTE: 285b521c43fSChristoph Hellwig * This function does NOT do any cache flushing. The caller is responsible for 286b521c43fSChristoph Hellwig * calling flush_cache_vmap() on to-be-mapped areas before calling this 287b521c43fSChristoph Hellwig * function. 288b521c43fSChristoph Hellwig * 289b521c43fSChristoph Hellwig * RETURNS: 29060bb4465SChristoph Hellwig * 0 on success, -errno on failure. 291db64fe02SNick Piggin */ 292b521c43fSChristoph Hellwig int map_kernel_range_noflush(unsigned long addr, unsigned long size, 293db64fe02SNick Piggin pgprot_t prot, struct page **pages) 2941da177e4SLinus Torvalds { 2952ba3e694SJoerg Roedel unsigned long start = addr; 296b521c43fSChristoph Hellwig unsigned long end = addr + size; 2971da177e4SLinus Torvalds unsigned long next; 298b521c43fSChristoph Hellwig pgd_t *pgd; 299db64fe02SNick Piggin int err = 0; 300db64fe02SNick Piggin int nr = 0; 3012ba3e694SJoerg Roedel pgtbl_mod_mask mask = 0; 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds BUG_ON(addr >= end); 3041da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 3051da177e4SLinus Torvalds do { 3061da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 3072ba3e694SJoerg Roedel if (pgd_bad(*pgd)) 3082ba3e694SJoerg Roedel mask |= PGTBL_PGD_MODIFIED; 3092ba3e694SJoerg Roedel err = vmap_p4d_range(pgd, addr, next, prot, pages, &nr, &mask); 3101da177e4SLinus Torvalds if (err) 311bf88c8c8SFigo.zhang return err; 3121da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 313db64fe02SNick Piggin 3142ba3e694SJoerg Roedel if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 3152ba3e694SJoerg Roedel arch_sync_kernel_mappings(start, end); 3162ba3e694SJoerg Roedel 31760bb4465SChristoph Hellwig return 0; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 320ed1f324cSChristoph Hellwig int map_kernel_range(unsigned long start, unsigned long size, pgprot_t prot, 321ed1f324cSChristoph Hellwig struct page **pages) 3228fc48985STejun Heo { 3238fc48985STejun Heo int ret; 3248fc48985STejun Heo 325a29adb62SChristoph Hellwig ret = map_kernel_range_noflush(start, size, prot, pages); 326a29adb62SChristoph Hellwig flush_cache_vmap(start, start + size); 3278fc48985STejun Heo return ret; 3288fc48985STejun Heo } 3298fc48985STejun Heo 33081ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x) 33173bdf0a6SLinus Torvalds { 33273bdf0a6SLinus Torvalds /* 333ab4f2ee1SRussell King * ARM, x86-64 and sparc64 put modules in a special place, 33473bdf0a6SLinus Torvalds * and fall back on vmalloc() if that fails. Others 33573bdf0a6SLinus Torvalds * just put it in the vmalloc space. 33673bdf0a6SLinus Torvalds */ 33773bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) 33873bdf0a6SLinus Torvalds unsigned long addr = (unsigned long)x; 33973bdf0a6SLinus Torvalds if (addr >= MODULES_VADDR && addr < MODULES_END) 34073bdf0a6SLinus Torvalds return 1; 34173bdf0a6SLinus Torvalds #endif 34273bdf0a6SLinus Torvalds return is_vmalloc_addr(x); 34373bdf0a6SLinus Torvalds } 34473bdf0a6SLinus Torvalds 34548667e7aSChristoph Lameter /* 346*c0eb315aSNicholas Piggin * Walk a vmap address to the struct page it maps. Huge vmap mappings will 347*c0eb315aSNicholas Piggin * return the tail page that corresponds to the base page address, which 348*c0eb315aSNicholas Piggin * matches small vmap mappings. 34948667e7aSChristoph Lameter */ 350add688fbSmalc struct page *vmalloc_to_page(const void *vmalloc_addr) 35148667e7aSChristoph Lameter { 35248667e7aSChristoph Lameter unsigned long addr = (unsigned long) vmalloc_addr; 353add688fbSmalc struct page *page = NULL; 35448667e7aSChristoph Lameter pgd_t *pgd = pgd_offset_k(addr); 355c2febafcSKirill A. Shutemov p4d_t *p4d; 356c2febafcSKirill A. Shutemov pud_t *pud; 357c2febafcSKirill A. Shutemov pmd_t *pmd; 358c2febafcSKirill A. Shutemov pte_t *ptep, pte; 35948667e7aSChristoph Lameter 3607aa413deSIngo Molnar /* 3617aa413deSIngo Molnar * XXX we might need to change this if we add VIRTUAL_BUG_ON for 3627aa413deSIngo Molnar * architectures that do not vmalloc module space 3637aa413deSIngo Molnar */ 36473bdf0a6SLinus Torvalds VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); 36559ea7463SJiri Slaby 366c2febafcSKirill A. Shutemov if (pgd_none(*pgd)) 367c2febafcSKirill A. Shutemov return NULL; 368*c0eb315aSNicholas Piggin if (WARN_ON_ONCE(pgd_leaf(*pgd))) 369*c0eb315aSNicholas Piggin return NULL; /* XXX: no allowance for huge pgd */ 370*c0eb315aSNicholas Piggin if (WARN_ON_ONCE(pgd_bad(*pgd))) 371*c0eb315aSNicholas Piggin return NULL; 372*c0eb315aSNicholas Piggin 373c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 374c2febafcSKirill A. Shutemov if (p4d_none(*p4d)) 375c2febafcSKirill A. Shutemov return NULL; 376*c0eb315aSNicholas Piggin if (p4d_leaf(*p4d)) 377*c0eb315aSNicholas Piggin return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT); 378*c0eb315aSNicholas Piggin if (WARN_ON_ONCE(p4d_bad(*p4d))) 379c2febafcSKirill A. Shutemov return NULL; 380*c0eb315aSNicholas Piggin 381*c0eb315aSNicholas Piggin pud = pud_offset(p4d, addr); 382*c0eb315aSNicholas Piggin if (pud_none(*pud)) 383*c0eb315aSNicholas Piggin return NULL; 384*c0eb315aSNicholas Piggin if (pud_leaf(*pud)) 385*c0eb315aSNicholas Piggin return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); 386*c0eb315aSNicholas Piggin if (WARN_ON_ONCE(pud_bad(*pud))) 387*c0eb315aSNicholas Piggin return NULL; 388*c0eb315aSNicholas Piggin 389c2febafcSKirill A. Shutemov pmd = pmd_offset(pud, addr); 390*c0eb315aSNicholas Piggin if (pmd_none(*pmd)) 391*c0eb315aSNicholas Piggin return NULL; 392*c0eb315aSNicholas Piggin if (pmd_leaf(*pmd)) 393*c0eb315aSNicholas Piggin return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); 394*c0eb315aSNicholas Piggin if (WARN_ON_ONCE(pmd_bad(*pmd))) 395c2febafcSKirill A. Shutemov return NULL; 396db64fe02SNick Piggin 39748667e7aSChristoph Lameter ptep = pte_offset_map(pmd, addr); 39848667e7aSChristoph Lameter pte = *ptep; 39948667e7aSChristoph Lameter if (pte_present(pte)) 400add688fbSmalc page = pte_page(pte); 40148667e7aSChristoph Lameter pte_unmap(ptep); 402*c0eb315aSNicholas Piggin 403add688fbSmalc return page; 404ece86e22SJianyu Zhan } 405ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page); 406ece86e22SJianyu Zhan 407add688fbSmalc /* 408add688fbSmalc * Map a vmalloc()-space virtual address to the physical page frame number. 409add688fbSmalc */ 410add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr) 411add688fbSmalc { 412add688fbSmalc return page_to_pfn(vmalloc_to_page(vmalloc_addr)); 413add688fbSmalc } 414add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn); 415add688fbSmalc 416db64fe02SNick Piggin 417db64fe02SNick Piggin /*** Global kva allocator ***/ 418db64fe02SNick Piggin 419bb850f4dSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_PROPAGATE_CHECK 0 420a6cf4e0fSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0 421bb850f4dSUladzislau Rezki (Sony) 422db64fe02SNick Piggin 423db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock); 424e36176beSUladzislau Rezki (Sony) static DEFINE_SPINLOCK(free_vmap_area_lock); 425f1c4069eSJoonsoo Kim /* Export for kexec only */ 426f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list); 42789699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT; 42868ad4a33SUladzislau Rezki (Sony) static bool vmap_initialized __read_mostly; 42989699605SNick Piggin 43096e2db45SUladzislau Rezki (Sony) static struct rb_root purge_vmap_area_root = RB_ROOT; 43196e2db45SUladzislau Rezki (Sony) static LIST_HEAD(purge_vmap_area_list); 43296e2db45SUladzislau Rezki (Sony) static DEFINE_SPINLOCK(purge_vmap_area_lock); 43396e2db45SUladzislau Rezki (Sony) 43468ad4a33SUladzislau Rezki (Sony) /* 43568ad4a33SUladzislau Rezki (Sony) * This kmem_cache is used for vmap_area objects. Instead of 43668ad4a33SUladzislau Rezki (Sony) * allocating from slab we reuse an object from this cache to 43768ad4a33SUladzislau Rezki (Sony) * make things faster. Especially in "no edge" splitting of 43868ad4a33SUladzislau Rezki (Sony) * free block. 43968ad4a33SUladzislau Rezki (Sony) */ 44068ad4a33SUladzislau Rezki (Sony) static struct kmem_cache *vmap_area_cachep; 44189699605SNick Piggin 44268ad4a33SUladzislau Rezki (Sony) /* 44368ad4a33SUladzislau Rezki (Sony) * This linked list is used in pair with free_vmap_area_root. 44468ad4a33SUladzislau Rezki (Sony) * It gives O(1) access to prev/next to perform fast coalescing. 44568ad4a33SUladzislau Rezki (Sony) */ 44668ad4a33SUladzislau Rezki (Sony) static LIST_HEAD(free_vmap_area_list); 44768ad4a33SUladzislau Rezki (Sony) 44868ad4a33SUladzislau Rezki (Sony) /* 44968ad4a33SUladzislau Rezki (Sony) * This augment red-black tree represents the free vmap space. 45068ad4a33SUladzislau Rezki (Sony) * All vmap_area objects in this tree are sorted by va->va_start 45168ad4a33SUladzislau Rezki (Sony) * address. It is used for allocation and merging when a vmap 45268ad4a33SUladzislau Rezki (Sony) * object is released. 45368ad4a33SUladzislau Rezki (Sony) * 45468ad4a33SUladzislau Rezki (Sony) * Each vmap_area node contains a maximum available free block 45568ad4a33SUladzislau Rezki (Sony) * of its sub-tree, right or left. Therefore it is possible to 45668ad4a33SUladzislau Rezki (Sony) * find a lowest match of free area. 45768ad4a33SUladzislau Rezki (Sony) */ 45868ad4a33SUladzislau Rezki (Sony) static struct rb_root free_vmap_area_root = RB_ROOT; 45968ad4a33SUladzislau Rezki (Sony) 46082dd23e8SUladzislau Rezki (Sony) /* 46182dd23e8SUladzislau Rezki (Sony) * Preload a CPU with one object for "no edge" split case. The 46282dd23e8SUladzislau Rezki (Sony) * aim is to get rid of allocations from the atomic context, thus 46382dd23e8SUladzislau Rezki (Sony) * to use more permissive allocation masks. 46482dd23e8SUladzislau Rezki (Sony) */ 46582dd23e8SUladzislau Rezki (Sony) static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node); 46682dd23e8SUladzislau Rezki (Sony) 46768ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 46868ad4a33SUladzislau Rezki (Sony) va_size(struct vmap_area *va) 46968ad4a33SUladzislau Rezki (Sony) { 47068ad4a33SUladzislau Rezki (Sony) return (va->va_end - va->va_start); 47168ad4a33SUladzislau Rezki (Sony) } 47268ad4a33SUladzislau Rezki (Sony) 47368ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 47468ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(struct rb_node *node) 47568ad4a33SUladzislau Rezki (Sony) { 47668ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 47768ad4a33SUladzislau Rezki (Sony) 47868ad4a33SUladzislau Rezki (Sony) va = rb_entry_safe(node, struct vmap_area, rb_node); 47968ad4a33SUladzislau Rezki (Sony) return va ? va->subtree_max_size : 0; 48068ad4a33SUladzislau Rezki (Sony) } 48168ad4a33SUladzislau Rezki (Sony) 48268ad4a33SUladzislau Rezki (Sony) /* 48368ad4a33SUladzislau Rezki (Sony) * Gets called when remove the node and rotate. 48468ad4a33SUladzislau Rezki (Sony) */ 48568ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 48668ad4a33SUladzislau Rezki (Sony) compute_subtree_max_size(struct vmap_area *va) 48768ad4a33SUladzislau Rezki (Sony) { 48868ad4a33SUladzislau Rezki (Sony) return max3(va_size(va), 48968ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(va->rb_node.rb_left), 49068ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(va->rb_node.rb_right)); 49168ad4a33SUladzislau Rezki (Sony) } 49268ad4a33SUladzislau Rezki (Sony) 493315cc066SMichel Lespinasse RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb, 494315cc066SMichel Lespinasse struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size) 49568ad4a33SUladzislau Rezki (Sony) 49668ad4a33SUladzislau Rezki (Sony) static void purge_vmap_area_lazy(void); 49768ad4a33SUladzislau Rezki (Sony) static BLOCKING_NOTIFIER_HEAD(vmap_notify_list); 49868ad4a33SUladzislau Rezki (Sony) static unsigned long lazy_max_pages(void); 499db64fe02SNick Piggin 50097105f0aSRoman Gushchin static atomic_long_t nr_vmalloc_pages; 50197105f0aSRoman Gushchin 50297105f0aSRoman Gushchin unsigned long vmalloc_nr_pages(void) 50397105f0aSRoman Gushchin { 50497105f0aSRoman Gushchin return atomic_long_read(&nr_vmalloc_pages); 50597105f0aSRoman Gushchin } 50697105f0aSRoman Gushchin 507db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr) 5081da177e4SLinus Torvalds { 509db64fe02SNick Piggin struct rb_node *n = vmap_area_root.rb_node; 510db64fe02SNick Piggin 511db64fe02SNick Piggin while (n) { 512db64fe02SNick Piggin struct vmap_area *va; 513db64fe02SNick Piggin 514db64fe02SNick Piggin va = rb_entry(n, struct vmap_area, rb_node); 515db64fe02SNick Piggin if (addr < va->va_start) 516db64fe02SNick Piggin n = n->rb_left; 517cef2ac3fSHATAYAMA Daisuke else if (addr >= va->va_end) 518db64fe02SNick Piggin n = n->rb_right; 519db64fe02SNick Piggin else 520db64fe02SNick Piggin return va; 521db64fe02SNick Piggin } 522db64fe02SNick Piggin 523db64fe02SNick Piggin return NULL; 524db64fe02SNick Piggin } 525db64fe02SNick Piggin 52668ad4a33SUladzislau Rezki (Sony) /* 52768ad4a33SUladzislau Rezki (Sony) * This function returns back addresses of parent node 52868ad4a33SUladzislau Rezki (Sony) * and its left or right link for further processing. 5299c801f61SUladzislau Rezki (Sony) * 5309c801f61SUladzislau Rezki (Sony) * Otherwise NULL is returned. In that case all further 5319c801f61SUladzislau Rezki (Sony) * steps regarding inserting of conflicting overlap range 5329c801f61SUladzislau Rezki (Sony) * have to be declined and actually considered as a bug. 53368ad4a33SUladzislau Rezki (Sony) */ 53468ad4a33SUladzislau Rezki (Sony) static __always_inline struct rb_node ** 53568ad4a33SUladzislau Rezki (Sony) find_va_links(struct vmap_area *va, 53668ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct rb_node *from, 53768ad4a33SUladzislau Rezki (Sony) struct rb_node **parent) 538db64fe02SNick Piggin { 539170168d0SNamhyung Kim struct vmap_area *tmp_va; 54068ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 541db64fe02SNick Piggin 54268ad4a33SUladzislau Rezki (Sony) if (root) { 54368ad4a33SUladzislau Rezki (Sony) link = &root->rb_node; 54468ad4a33SUladzislau Rezki (Sony) if (unlikely(!*link)) { 54568ad4a33SUladzislau Rezki (Sony) *parent = NULL; 54668ad4a33SUladzislau Rezki (Sony) return link; 54768ad4a33SUladzislau Rezki (Sony) } 54868ad4a33SUladzislau Rezki (Sony) } else { 54968ad4a33SUladzislau Rezki (Sony) link = &from; 55068ad4a33SUladzislau Rezki (Sony) } 55168ad4a33SUladzislau Rezki (Sony) 55268ad4a33SUladzislau Rezki (Sony) /* 55368ad4a33SUladzislau Rezki (Sony) * Go to the bottom of the tree. When we hit the last point 55468ad4a33SUladzislau Rezki (Sony) * we end up with parent rb_node and correct direction, i name 55568ad4a33SUladzislau Rezki (Sony) * it link, where the new va->rb_node will be attached to. 55668ad4a33SUladzislau Rezki (Sony) */ 55768ad4a33SUladzislau Rezki (Sony) do { 55868ad4a33SUladzislau Rezki (Sony) tmp_va = rb_entry(*link, struct vmap_area, rb_node); 55968ad4a33SUladzislau Rezki (Sony) 56068ad4a33SUladzislau Rezki (Sony) /* 56168ad4a33SUladzislau Rezki (Sony) * During the traversal we also do some sanity check. 56268ad4a33SUladzislau Rezki (Sony) * Trigger the BUG() if there are sides(left/right) 56368ad4a33SUladzislau Rezki (Sony) * or full overlaps. 56468ad4a33SUladzislau Rezki (Sony) */ 56568ad4a33SUladzislau Rezki (Sony) if (va->va_start < tmp_va->va_end && 56668ad4a33SUladzislau Rezki (Sony) va->va_end <= tmp_va->va_start) 56768ad4a33SUladzislau Rezki (Sony) link = &(*link)->rb_left; 56868ad4a33SUladzislau Rezki (Sony) else if (va->va_end > tmp_va->va_start && 56968ad4a33SUladzislau Rezki (Sony) va->va_start >= tmp_va->va_end) 57068ad4a33SUladzislau Rezki (Sony) link = &(*link)->rb_right; 5719c801f61SUladzislau Rezki (Sony) else { 5729c801f61SUladzislau Rezki (Sony) WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n", 5739c801f61SUladzislau Rezki (Sony) va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end); 5749c801f61SUladzislau Rezki (Sony) 5759c801f61SUladzislau Rezki (Sony) return NULL; 5769c801f61SUladzislau Rezki (Sony) } 57768ad4a33SUladzislau Rezki (Sony) } while (*link); 57868ad4a33SUladzislau Rezki (Sony) 57968ad4a33SUladzislau Rezki (Sony) *parent = &tmp_va->rb_node; 58068ad4a33SUladzislau Rezki (Sony) return link; 581db64fe02SNick Piggin } 582db64fe02SNick Piggin 58368ad4a33SUladzislau Rezki (Sony) static __always_inline struct list_head * 58468ad4a33SUladzislau Rezki (Sony) get_va_next_sibling(struct rb_node *parent, struct rb_node **link) 58568ad4a33SUladzislau Rezki (Sony) { 58668ad4a33SUladzislau Rezki (Sony) struct list_head *list; 587db64fe02SNick Piggin 58868ad4a33SUladzislau Rezki (Sony) if (unlikely(!parent)) 58968ad4a33SUladzislau Rezki (Sony) /* 59068ad4a33SUladzislau Rezki (Sony) * The red-black tree where we try to find VA neighbors 59168ad4a33SUladzislau Rezki (Sony) * before merging or inserting is empty, i.e. it means 59268ad4a33SUladzislau Rezki (Sony) * there is no free vmap space. Normally it does not 59368ad4a33SUladzislau Rezki (Sony) * happen but we handle this case anyway. 59468ad4a33SUladzislau Rezki (Sony) */ 59568ad4a33SUladzislau Rezki (Sony) return NULL; 59668ad4a33SUladzislau Rezki (Sony) 59768ad4a33SUladzislau Rezki (Sony) list = &rb_entry(parent, struct vmap_area, rb_node)->list; 59868ad4a33SUladzislau Rezki (Sony) return (&parent->rb_right == link ? list->next : list); 599db64fe02SNick Piggin } 600db64fe02SNick Piggin 60168ad4a33SUladzislau Rezki (Sony) static __always_inline void 60268ad4a33SUladzislau Rezki (Sony) link_va(struct vmap_area *va, struct rb_root *root, 60368ad4a33SUladzislau Rezki (Sony) struct rb_node *parent, struct rb_node **link, struct list_head *head) 60468ad4a33SUladzislau Rezki (Sony) { 60568ad4a33SUladzislau Rezki (Sony) /* 60668ad4a33SUladzislau Rezki (Sony) * VA is still not in the list, but we can 60768ad4a33SUladzislau Rezki (Sony) * identify its future previous list_head node. 60868ad4a33SUladzislau Rezki (Sony) */ 60968ad4a33SUladzislau Rezki (Sony) if (likely(parent)) { 61068ad4a33SUladzislau Rezki (Sony) head = &rb_entry(parent, struct vmap_area, rb_node)->list; 61168ad4a33SUladzislau Rezki (Sony) if (&parent->rb_right != link) 61268ad4a33SUladzislau Rezki (Sony) head = head->prev; 61368ad4a33SUladzislau Rezki (Sony) } 614db64fe02SNick Piggin 61568ad4a33SUladzislau Rezki (Sony) /* Insert to the rb-tree */ 61668ad4a33SUladzislau Rezki (Sony) rb_link_node(&va->rb_node, parent, link); 61768ad4a33SUladzislau Rezki (Sony) if (root == &free_vmap_area_root) { 61868ad4a33SUladzislau Rezki (Sony) /* 61968ad4a33SUladzislau Rezki (Sony) * Some explanation here. Just perform simple insertion 62068ad4a33SUladzislau Rezki (Sony) * to the tree. We do not set va->subtree_max_size to 62168ad4a33SUladzislau Rezki (Sony) * its current size before calling rb_insert_augmented(). 62268ad4a33SUladzislau Rezki (Sony) * It is because of we populate the tree from the bottom 62368ad4a33SUladzislau Rezki (Sony) * to parent levels when the node _is_ in the tree. 62468ad4a33SUladzislau Rezki (Sony) * 62568ad4a33SUladzislau Rezki (Sony) * Therefore we set subtree_max_size to zero after insertion, 62668ad4a33SUladzislau Rezki (Sony) * to let __augment_tree_propagate_from() puts everything to 62768ad4a33SUladzislau Rezki (Sony) * the correct order later on. 62868ad4a33SUladzislau Rezki (Sony) */ 62968ad4a33SUladzislau Rezki (Sony) rb_insert_augmented(&va->rb_node, 63068ad4a33SUladzislau Rezki (Sony) root, &free_vmap_area_rb_augment_cb); 63168ad4a33SUladzislau Rezki (Sony) va->subtree_max_size = 0; 63268ad4a33SUladzislau Rezki (Sony) } else { 63368ad4a33SUladzislau Rezki (Sony) rb_insert_color(&va->rb_node, root); 63468ad4a33SUladzislau Rezki (Sony) } 63568ad4a33SUladzislau Rezki (Sony) 63668ad4a33SUladzislau Rezki (Sony) /* Address-sort this list */ 63768ad4a33SUladzislau Rezki (Sony) list_add(&va->list, head); 63868ad4a33SUladzislau Rezki (Sony) } 63968ad4a33SUladzislau Rezki (Sony) 64068ad4a33SUladzislau Rezki (Sony) static __always_inline void 64168ad4a33SUladzislau Rezki (Sony) unlink_va(struct vmap_area *va, struct rb_root *root) 64268ad4a33SUladzislau Rezki (Sony) { 643460e42d1SUladzislau Rezki (Sony) if (WARN_ON(RB_EMPTY_NODE(&va->rb_node))) 644460e42d1SUladzislau Rezki (Sony) return; 645460e42d1SUladzislau Rezki (Sony) 64668ad4a33SUladzislau Rezki (Sony) if (root == &free_vmap_area_root) 64768ad4a33SUladzislau Rezki (Sony) rb_erase_augmented(&va->rb_node, 64868ad4a33SUladzislau Rezki (Sony) root, &free_vmap_area_rb_augment_cb); 64968ad4a33SUladzislau Rezki (Sony) else 65068ad4a33SUladzislau Rezki (Sony) rb_erase(&va->rb_node, root); 65168ad4a33SUladzislau Rezki (Sony) 65268ad4a33SUladzislau Rezki (Sony) list_del(&va->list); 65368ad4a33SUladzislau Rezki (Sony) RB_CLEAR_NODE(&va->rb_node); 65468ad4a33SUladzislau Rezki (Sony) } 65568ad4a33SUladzislau Rezki (Sony) 656bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK 657bb850f4dSUladzislau Rezki (Sony) static void 658da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(void) 659bb850f4dSUladzislau Rezki (Sony) { 660bb850f4dSUladzislau Rezki (Sony) struct vmap_area *va; 661da27c9edSUladzislau Rezki (Sony) unsigned long computed_size; 662bb850f4dSUladzislau Rezki (Sony) 663da27c9edSUladzislau Rezki (Sony) list_for_each_entry(va, &free_vmap_area_list, list) { 664da27c9edSUladzislau Rezki (Sony) computed_size = compute_subtree_max_size(va); 665da27c9edSUladzislau Rezki (Sony) if (computed_size != va->subtree_max_size) 666bb850f4dSUladzislau Rezki (Sony) pr_emerg("tree is corrupted: %lu, %lu\n", 667bb850f4dSUladzislau Rezki (Sony) va_size(va), va->subtree_max_size); 668bb850f4dSUladzislau Rezki (Sony) } 669bb850f4dSUladzislau Rezki (Sony) } 670bb850f4dSUladzislau Rezki (Sony) #endif 671bb850f4dSUladzislau Rezki (Sony) 67268ad4a33SUladzislau Rezki (Sony) /* 67368ad4a33SUladzislau Rezki (Sony) * This function populates subtree_max_size from bottom to upper 67468ad4a33SUladzislau Rezki (Sony) * levels starting from VA point. The propagation must be done 67568ad4a33SUladzislau Rezki (Sony) * when VA size is modified by changing its va_start/va_end. Or 67668ad4a33SUladzislau Rezki (Sony) * in case of newly inserting of VA to the tree. 67768ad4a33SUladzislau Rezki (Sony) * 67868ad4a33SUladzislau Rezki (Sony) * It means that __augment_tree_propagate_from() must be called: 67968ad4a33SUladzislau Rezki (Sony) * - After VA has been inserted to the tree(free path); 68068ad4a33SUladzislau Rezki (Sony) * - After VA has been shrunk(allocation path); 68168ad4a33SUladzislau Rezki (Sony) * - After VA has been increased(merging path). 68268ad4a33SUladzislau Rezki (Sony) * 68368ad4a33SUladzislau Rezki (Sony) * Please note that, it does not mean that upper parent nodes 68468ad4a33SUladzislau Rezki (Sony) * and their subtree_max_size are recalculated all the time up 68568ad4a33SUladzislau Rezki (Sony) * to the root node. 68668ad4a33SUladzislau Rezki (Sony) * 68768ad4a33SUladzislau Rezki (Sony) * 4--8 68868ad4a33SUladzislau Rezki (Sony) * /\ 68968ad4a33SUladzislau Rezki (Sony) * / \ 69068ad4a33SUladzislau Rezki (Sony) * / \ 69168ad4a33SUladzislau Rezki (Sony) * 2--2 8--8 69268ad4a33SUladzislau Rezki (Sony) * 69368ad4a33SUladzislau Rezki (Sony) * For example if we modify the node 4, shrinking it to 2, then 69468ad4a33SUladzislau Rezki (Sony) * no any modification is required. If we shrink the node 2 to 1 69568ad4a33SUladzislau Rezki (Sony) * its subtree_max_size is updated only, and set to 1. If we shrink 69668ad4a33SUladzislau Rezki (Sony) * the node 8 to 6, then its subtree_max_size is set to 6 and parent 69768ad4a33SUladzislau Rezki (Sony) * node becomes 4--6. 69868ad4a33SUladzislau Rezki (Sony) */ 69968ad4a33SUladzislau Rezki (Sony) static __always_inline void 70068ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(struct vmap_area *va) 70168ad4a33SUladzislau Rezki (Sony) { 70268ad4a33SUladzislau Rezki (Sony) /* 70315ae144fSUladzislau Rezki (Sony) * Populate the tree from bottom towards the root until 70415ae144fSUladzislau Rezki (Sony) * the calculated maximum available size of checked node 70515ae144fSUladzislau Rezki (Sony) * is equal to its current one. 70668ad4a33SUladzislau Rezki (Sony) */ 70715ae144fSUladzislau Rezki (Sony) free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL); 708bb850f4dSUladzislau Rezki (Sony) 709bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK 710da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(); 711bb850f4dSUladzislau Rezki (Sony) #endif 71268ad4a33SUladzislau Rezki (Sony) } 71368ad4a33SUladzislau Rezki (Sony) 71468ad4a33SUladzislau Rezki (Sony) static void 71568ad4a33SUladzislau Rezki (Sony) insert_vmap_area(struct vmap_area *va, 71668ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 71768ad4a33SUladzislau Rezki (Sony) { 71868ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 71968ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 72068ad4a33SUladzislau Rezki (Sony) 72168ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 7229c801f61SUladzislau Rezki (Sony) if (link) 72368ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 72468ad4a33SUladzislau Rezki (Sony) } 72568ad4a33SUladzislau Rezki (Sony) 72668ad4a33SUladzislau Rezki (Sony) static void 72768ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(struct vmap_area *va, 72868ad4a33SUladzislau Rezki (Sony) struct rb_node *from, struct rb_root *root, 72968ad4a33SUladzislau Rezki (Sony) struct list_head *head) 73068ad4a33SUladzislau Rezki (Sony) { 73168ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 73268ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 73368ad4a33SUladzislau Rezki (Sony) 73468ad4a33SUladzislau Rezki (Sony) if (from) 73568ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, NULL, from, &parent); 73668ad4a33SUladzislau Rezki (Sony) else 73768ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 73868ad4a33SUladzislau Rezki (Sony) 7399c801f61SUladzislau Rezki (Sony) if (link) { 74068ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 74168ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 74268ad4a33SUladzislau Rezki (Sony) } 7439c801f61SUladzislau Rezki (Sony) } 74468ad4a33SUladzislau Rezki (Sony) 74568ad4a33SUladzislau Rezki (Sony) /* 74668ad4a33SUladzislau Rezki (Sony) * Merge de-allocated chunk of VA memory with previous 74768ad4a33SUladzislau Rezki (Sony) * and next free blocks. If coalesce is not done a new 74868ad4a33SUladzislau Rezki (Sony) * free area is inserted. If VA has been merged, it is 74968ad4a33SUladzislau Rezki (Sony) * freed. 7509c801f61SUladzislau Rezki (Sony) * 7519c801f61SUladzislau Rezki (Sony) * Please note, it can return NULL in case of overlap 7529c801f61SUladzislau Rezki (Sony) * ranges, followed by WARN() report. Despite it is a 7539c801f61SUladzislau Rezki (Sony) * buggy behaviour, a system can be alive and keep 7549c801f61SUladzislau Rezki (Sony) * ongoing. 75568ad4a33SUladzislau Rezki (Sony) */ 7563c5c3cfbSDaniel Axtens static __always_inline struct vmap_area * 75768ad4a33SUladzislau Rezki (Sony) merge_or_add_vmap_area(struct vmap_area *va, 75868ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 75968ad4a33SUladzislau Rezki (Sony) { 76068ad4a33SUladzislau Rezki (Sony) struct vmap_area *sibling; 76168ad4a33SUladzislau Rezki (Sony) struct list_head *next; 76268ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 76368ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 76468ad4a33SUladzislau Rezki (Sony) bool merged = false; 76568ad4a33SUladzislau Rezki (Sony) 76668ad4a33SUladzislau Rezki (Sony) /* 76768ad4a33SUladzislau Rezki (Sony) * Find a place in the tree where VA potentially will be 76868ad4a33SUladzislau Rezki (Sony) * inserted, unless it is merged with its sibling/siblings. 76968ad4a33SUladzislau Rezki (Sony) */ 77068ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 7719c801f61SUladzislau Rezki (Sony) if (!link) 7729c801f61SUladzislau Rezki (Sony) return NULL; 77368ad4a33SUladzislau Rezki (Sony) 77468ad4a33SUladzislau Rezki (Sony) /* 77568ad4a33SUladzislau Rezki (Sony) * Get next node of VA to check if merging can be done. 77668ad4a33SUladzislau Rezki (Sony) */ 77768ad4a33SUladzislau Rezki (Sony) next = get_va_next_sibling(parent, link); 77868ad4a33SUladzislau Rezki (Sony) if (unlikely(next == NULL)) 77968ad4a33SUladzislau Rezki (Sony) goto insert; 78068ad4a33SUladzislau Rezki (Sony) 78168ad4a33SUladzislau Rezki (Sony) /* 78268ad4a33SUladzislau Rezki (Sony) * start end 78368ad4a33SUladzislau Rezki (Sony) * | | 78468ad4a33SUladzislau Rezki (Sony) * |<------VA------>|<-----Next----->| 78568ad4a33SUladzislau Rezki (Sony) * | | 78668ad4a33SUladzislau Rezki (Sony) * start end 78768ad4a33SUladzislau Rezki (Sony) */ 78868ad4a33SUladzislau Rezki (Sony) if (next != head) { 78968ad4a33SUladzislau Rezki (Sony) sibling = list_entry(next, struct vmap_area, list); 79068ad4a33SUladzislau Rezki (Sony) if (sibling->va_start == va->va_end) { 79168ad4a33SUladzislau Rezki (Sony) sibling->va_start = va->va_start; 79268ad4a33SUladzislau Rezki (Sony) 79368ad4a33SUladzislau Rezki (Sony) /* Free vmap_area object. */ 79468ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 79568ad4a33SUladzislau Rezki (Sony) 79668ad4a33SUladzislau Rezki (Sony) /* Point to the new merged area. */ 79768ad4a33SUladzislau Rezki (Sony) va = sibling; 79868ad4a33SUladzislau Rezki (Sony) merged = true; 79968ad4a33SUladzislau Rezki (Sony) } 80068ad4a33SUladzislau Rezki (Sony) } 80168ad4a33SUladzislau Rezki (Sony) 80268ad4a33SUladzislau Rezki (Sony) /* 80368ad4a33SUladzislau Rezki (Sony) * start end 80468ad4a33SUladzislau Rezki (Sony) * | | 80568ad4a33SUladzislau Rezki (Sony) * |<-----Prev----->|<------VA------>| 80668ad4a33SUladzislau Rezki (Sony) * | | 80768ad4a33SUladzislau Rezki (Sony) * start end 80868ad4a33SUladzislau Rezki (Sony) */ 80968ad4a33SUladzislau Rezki (Sony) if (next->prev != head) { 81068ad4a33SUladzislau Rezki (Sony) sibling = list_entry(next->prev, struct vmap_area, list); 81168ad4a33SUladzislau Rezki (Sony) if (sibling->va_end == va->va_start) { 8125dd78640SUladzislau Rezki (Sony) /* 8135dd78640SUladzislau Rezki (Sony) * If both neighbors are coalesced, it is important 8145dd78640SUladzislau Rezki (Sony) * to unlink the "next" node first, followed by merging 8155dd78640SUladzislau Rezki (Sony) * with "previous" one. Otherwise the tree might not be 8165dd78640SUladzislau Rezki (Sony) * fully populated if a sibling's augmented value is 8175dd78640SUladzislau Rezki (Sony) * "normalized" because of rotation operations. 8185dd78640SUladzislau Rezki (Sony) */ 81954f63d9dSUladzislau Rezki (Sony) if (merged) 82068ad4a33SUladzislau Rezki (Sony) unlink_va(va, root); 82168ad4a33SUladzislau Rezki (Sony) 8225dd78640SUladzislau Rezki (Sony) sibling->va_end = va->va_end; 8235dd78640SUladzislau Rezki (Sony) 82468ad4a33SUladzislau Rezki (Sony) /* Free vmap_area object. */ 82568ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 8263c5c3cfbSDaniel Axtens 8273c5c3cfbSDaniel Axtens /* Point to the new merged area. */ 8283c5c3cfbSDaniel Axtens va = sibling; 8293c5c3cfbSDaniel Axtens merged = true; 83068ad4a33SUladzislau Rezki (Sony) } 83168ad4a33SUladzislau Rezki (Sony) } 83268ad4a33SUladzislau Rezki (Sony) 83368ad4a33SUladzislau Rezki (Sony) insert: 8345dd78640SUladzislau Rezki (Sony) if (!merged) 83568ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 8363c5c3cfbSDaniel Axtens 83796e2db45SUladzislau Rezki (Sony) return va; 83896e2db45SUladzislau Rezki (Sony) } 83996e2db45SUladzislau Rezki (Sony) 84096e2db45SUladzislau Rezki (Sony) static __always_inline struct vmap_area * 84196e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area_augment(struct vmap_area *va, 84296e2db45SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 84396e2db45SUladzislau Rezki (Sony) { 84496e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area(va, root, head); 84596e2db45SUladzislau Rezki (Sony) if (va) 8465dd78640SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 84796e2db45SUladzislau Rezki (Sony) 8483c5c3cfbSDaniel Axtens return va; 84968ad4a33SUladzislau Rezki (Sony) } 85068ad4a33SUladzislau Rezki (Sony) 85168ad4a33SUladzislau Rezki (Sony) static __always_inline bool 85268ad4a33SUladzislau Rezki (Sony) is_within_this_va(struct vmap_area *va, unsigned long size, 85368ad4a33SUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 85468ad4a33SUladzislau Rezki (Sony) { 85568ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr; 85668ad4a33SUladzislau Rezki (Sony) 85768ad4a33SUladzislau Rezki (Sony) if (va->va_start > vstart) 85868ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(va->va_start, align); 85968ad4a33SUladzislau Rezki (Sony) else 86068ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(vstart, align); 86168ad4a33SUladzislau Rezki (Sony) 86268ad4a33SUladzislau Rezki (Sony) /* Can be overflowed due to big size or alignment. */ 86368ad4a33SUladzislau Rezki (Sony) if (nva_start_addr + size < nva_start_addr || 86468ad4a33SUladzislau Rezki (Sony) nva_start_addr < vstart) 86568ad4a33SUladzislau Rezki (Sony) return false; 86668ad4a33SUladzislau Rezki (Sony) 86768ad4a33SUladzislau Rezki (Sony) return (nva_start_addr + size <= va->va_end); 86868ad4a33SUladzislau Rezki (Sony) } 86968ad4a33SUladzislau Rezki (Sony) 87068ad4a33SUladzislau Rezki (Sony) /* 87168ad4a33SUladzislau Rezki (Sony) * Find the first free block(lowest start address) in the tree, 87268ad4a33SUladzislau Rezki (Sony) * that will accomplish the request corresponding to passing 87368ad4a33SUladzislau Rezki (Sony) * parameters. 87468ad4a33SUladzislau Rezki (Sony) */ 87568ad4a33SUladzislau Rezki (Sony) static __always_inline struct vmap_area * 87668ad4a33SUladzislau Rezki (Sony) find_vmap_lowest_match(unsigned long size, 87768ad4a33SUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 87868ad4a33SUladzislau Rezki (Sony) { 87968ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 88068ad4a33SUladzislau Rezki (Sony) struct rb_node *node; 88168ad4a33SUladzislau Rezki (Sony) unsigned long length; 88268ad4a33SUladzislau Rezki (Sony) 88368ad4a33SUladzislau Rezki (Sony) /* Start from the root. */ 88468ad4a33SUladzislau Rezki (Sony) node = free_vmap_area_root.rb_node; 88568ad4a33SUladzislau Rezki (Sony) 88668ad4a33SUladzislau Rezki (Sony) /* Adjust the search size for alignment overhead. */ 88768ad4a33SUladzislau Rezki (Sony) length = size + align - 1; 88868ad4a33SUladzislau Rezki (Sony) 88968ad4a33SUladzislau Rezki (Sony) while (node) { 89068ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 89168ad4a33SUladzislau Rezki (Sony) 89268ad4a33SUladzislau Rezki (Sony) if (get_subtree_max_size(node->rb_left) >= length && 89368ad4a33SUladzislau Rezki (Sony) vstart < va->va_start) { 89468ad4a33SUladzislau Rezki (Sony) node = node->rb_left; 89568ad4a33SUladzislau Rezki (Sony) } else { 89668ad4a33SUladzislau Rezki (Sony) if (is_within_this_va(va, size, align, vstart)) 89768ad4a33SUladzislau Rezki (Sony) return va; 89868ad4a33SUladzislau Rezki (Sony) 89968ad4a33SUladzislau Rezki (Sony) /* 90068ad4a33SUladzislau Rezki (Sony) * Does not make sense to go deeper towards the right 90168ad4a33SUladzislau Rezki (Sony) * sub-tree if it does not have a free block that is 90268ad4a33SUladzislau Rezki (Sony) * equal or bigger to the requested search length. 90368ad4a33SUladzislau Rezki (Sony) */ 90468ad4a33SUladzislau Rezki (Sony) if (get_subtree_max_size(node->rb_right) >= length) { 90568ad4a33SUladzislau Rezki (Sony) node = node->rb_right; 90668ad4a33SUladzislau Rezki (Sony) continue; 90768ad4a33SUladzislau Rezki (Sony) } 90868ad4a33SUladzislau Rezki (Sony) 90968ad4a33SUladzislau Rezki (Sony) /* 9103806b041SAndrew Morton * OK. We roll back and find the first right sub-tree, 91168ad4a33SUladzislau Rezki (Sony) * that will satisfy the search criteria. It can happen 91268ad4a33SUladzislau Rezki (Sony) * only once due to "vstart" restriction. 91368ad4a33SUladzislau Rezki (Sony) */ 91468ad4a33SUladzislau Rezki (Sony) while ((node = rb_parent(node))) { 91568ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 91668ad4a33SUladzislau Rezki (Sony) if (is_within_this_va(va, size, align, vstart)) 91768ad4a33SUladzislau Rezki (Sony) return va; 91868ad4a33SUladzislau Rezki (Sony) 91968ad4a33SUladzislau Rezki (Sony) if (get_subtree_max_size(node->rb_right) >= length && 92068ad4a33SUladzislau Rezki (Sony) vstart <= va->va_start) { 92168ad4a33SUladzislau Rezki (Sony) node = node->rb_right; 92268ad4a33SUladzislau Rezki (Sony) break; 92368ad4a33SUladzislau Rezki (Sony) } 92468ad4a33SUladzislau Rezki (Sony) } 92568ad4a33SUladzislau Rezki (Sony) } 92668ad4a33SUladzislau Rezki (Sony) } 92768ad4a33SUladzislau Rezki (Sony) 92868ad4a33SUladzislau Rezki (Sony) return NULL; 92968ad4a33SUladzislau Rezki (Sony) } 93068ad4a33SUladzislau Rezki (Sony) 931a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 932a6cf4e0fSUladzislau Rezki (Sony) #include <linux/random.h> 933a6cf4e0fSUladzislau Rezki (Sony) 934a6cf4e0fSUladzislau Rezki (Sony) static struct vmap_area * 935a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_linear_match(unsigned long size, 936a6cf4e0fSUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 937a6cf4e0fSUladzislau Rezki (Sony) { 938a6cf4e0fSUladzislau Rezki (Sony) struct vmap_area *va; 939a6cf4e0fSUladzislau Rezki (Sony) 940a6cf4e0fSUladzislau Rezki (Sony) list_for_each_entry(va, &free_vmap_area_list, list) { 941a6cf4e0fSUladzislau Rezki (Sony) if (!is_within_this_va(va, size, align, vstart)) 942a6cf4e0fSUladzislau Rezki (Sony) continue; 943a6cf4e0fSUladzislau Rezki (Sony) 944a6cf4e0fSUladzislau Rezki (Sony) return va; 945a6cf4e0fSUladzislau Rezki (Sony) } 946a6cf4e0fSUladzislau Rezki (Sony) 947a6cf4e0fSUladzislau Rezki (Sony) return NULL; 948a6cf4e0fSUladzislau Rezki (Sony) } 949a6cf4e0fSUladzislau Rezki (Sony) 950a6cf4e0fSUladzislau Rezki (Sony) static void 951a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_match_check(unsigned long size) 952a6cf4e0fSUladzislau Rezki (Sony) { 953a6cf4e0fSUladzislau Rezki (Sony) struct vmap_area *va_1, *va_2; 954a6cf4e0fSUladzislau Rezki (Sony) unsigned long vstart; 955a6cf4e0fSUladzislau Rezki (Sony) unsigned int rnd; 956a6cf4e0fSUladzislau Rezki (Sony) 957a6cf4e0fSUladzislau Rezki (Sony) get_random_bytes(&rnd, sizeof(rnd)); 958a6cf4e0fSUladzislau Rezki (Sony) vstart = VMALLOC_START + rnd; 959a6cf4e0fSUladzislau Rezki (Sony) 960a6cf4e0fSUladzislau Rezki (Sony) va_1 = find_vmap_lowest_match(size, 1, vstart); 961a6cf4e0fSUladzislau Rezki (Sony) va_2 = find_vmap_lowest_linear_match(size, 1, vstart); 962a6cf4e0fSUladzislau Rezki (Sony) 963a6cf4e0fSUladzislau Rezki (Sony) if (va_1 != va_2) 964a6cf4e0fSUladzislau Rezki (Sony) pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n", 965a6cf4e0fSUladzislau Rezki (Sony) va_1, va_2, vstart); 966a6cf4e0fSUladzislau Rezki (Sony) } 967a6cf4e0fSUladzislau Rezki (Sony) #endif 968a6cf4e0fSUladzislau Rezki (Sony) 96968ad4a33SUladzislau Rezki (Sony) enum fit_type { 97068ad4a33SUladzislau Rezki (Sony) NOTHING_FIT = 0, 97168ad4a33SUladzislau Rezki (Sony) FL_FIT_TYPE = 1, /* full fit */ 97268ad4a33SUladzislau Rezki (Sony) LE_FIT_TYPE = 2, /* left edge fit */ 97368ad4a33SUladzislau Rezki (Sony) RE_FIT_TYPE = 3, /* right edge fit */ 97468ad4a33SUladzislau Rezki (Sony) NE_FIT_TYPE = 4 /* no edge fit */ 97568ad4a33SUladzislau Rezki (Sony) }; 97668ad4a33SUladzislau Rezki (Sony) 97768ad4a33SUladzislau Rezki (Sony) static __always_inline enum fit_type 97868ad4a33SUladzislau Rezki (Sony) classify_va_fit_type(struct vmap_area *va, 97968ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr, unsigned long size) 98068ad4a33SUladzislau Rezki (Sony) { 98168ad4a33SUladzislau Rezki (Sony) enum fit_type type; 98268ad4a33SUladzislau Rezki (Sony) 98368ad4a33SUladzislau Rezki (Sony) /* Check if it is within VA. */ 98468ad4a33SUladzislau Rezki (Sony) if (nva_start_addr < va->va_start || 98568ad4a33SUladzislau Rezki (Sony) nva_start_addr + size > va->va_end) 98668ad4a33SUladzislau Rezki (Sony) return NOTHING_FIT; 98768ad4a33SUladzislau Rezki (Sony) 98868ad4a33SUladzislau Rezki (Sony) /* Now classify. */ 98968ad4a33SUladzislau Rezki (Sony) if (va->va_start == nva_start_addr) { 99068ad4a33SUladzislau Rezki (Sony) if (va->va_end == nva_start_addr + size) 99168ad4a33SUladzislau Rezki (Sony) type = FL_FIT_TYPE; 99268ad4a33SUladzislau Rezki (Sony) else 99368ad4a33SUladzislau Rezki (Sony) type = LE_FIT_TYPE; 99468ad4a33SUladzislau Rezki (Sony) } else if (va->va_end == nva_start_addr + size) { 99568ad4a33SUladzislau Rezki (Sony) type = RE_FIT_TYPE; 99668ad4a33SUladzislau Rezki (Sony) } else { 99768ad4a33SUladzislau Rezki (Sony) type = NE_FIT_TYPE; 99868ad4a33SUladzislau Rezki (Sony) } 99968ad4a33SUladzislau Rezki (Sony) 100068ad4a33SUladzislau Rezki (Sony) return type; 100168ad4a33SUladzislau Rezki (Sony) } 100268ad4a33SUladzislau Rezki (Sony) 100368ad4a33SUladzislau Rezki (Sony) static __always_inline int 100468ad4a33SUladzislau Rezki (Sony) adjust_va_to_fit_type(struct vmap_area *va, 100568ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr, unsigned long size, 100668ad4a33SUladzislau Rezki (Sony) enum fit_type type) 100768ad4a33SUladzislau Rezki (Sony) { 10082c929233SArnd Bergmann struct vmap_area *lva = NULL; 100968ad4a33SUladzislau Rezki (Sony) 101068ad4a33SUladzislau Rezki (Sony) if (type == FL_FIT_TYPE) { 101168ad4a33SUladzislau Rezki (Sony) /* 101268ad4a33SUladzislau Rezki (Sony) * No need to split VA, it fully fits. 101368ad4a33SUladzislau Rezki (Sony) * 101468ad4a33SUladzislau Rezki (Sony) * | | 101568ad4a33SUladzislau Rezki (Sony) * V NVA V 101668ad4a33SUladzislau Rezki (Sony) * |---------------| 101768ad4a33SUladzislau Rezki (Sony) */ 101868ad4a33SUladzislau Rezki (Sony) unlink_va(va, &free_vmap_area_root); 101968ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 102068ad4a33SUladzislau Rezki (Sony) } else if (type == LE_FIT_TYPE) { 102168ad4a33SUladzislau Rezki (Sony) /* 102268ad4a33SUladzislau Rezki (Sony) * Split left edge of fit VA. 102368ad4a33SUladzislau Rezki (Sony) * 102468ad4a33SUladzislau Rezki (Sony) * | | 102568ad4a33SUladzislau Rezki (Sony) * V NVA V R 102668ad4a33SUladzislau Rezki (Sony) * |-------|-------| 102768ad4a33SUladzislau Rezki (Sony) */ 102868ad4a33SUladzislau Rezki (Sony) va->va_start += size; 102968ad4a33SUladzislau Rezki (Sony) } else if (type == RE_FIT_TYPE) { 103068ad4a33SUladzislau Rezki (Sony) /* 103168ad4a33SUladzislau Rezki (Sony) * Split right edge of fit VA. 103268ad4a33SUladzislau Rezki (Sony) * 103368ad4a33SUladzislau Rezki (Sony) * | | 103468ad4a33SUladzislau Rezki (Sony) * L V NVA V 103568ad4a33SUladzislau Rezki (Sony) * |-------|-------| 103668ad4a33SUladzislau Rezki (Sony) */ 103768ad4a33SUladzislau Rezki (Sony) va->va_end = nva_start_addr; 103868ad4a33SUladzislau Rezki (Sony) } else if (type == NE_FIT_TYPE) { 103968ad4a33SUladzislau Rezki (Sony) /* 104068ad4a33SUladzislau Rezki (Sony) * Split no edge of fit VA. 104168ad4a33SUladzislau Rezki (Sony) * 104268ad4a33SUladzislau Rezki (Sony) * | | 104368ad4a33SUladzislau Rezki (Sony) * L V NVA V R 104468ad4a33SUladzislau Rezki (Sony) * |---|-------|---| 104568ad4a33SUladzislau Rezki (Sony) */ 104682dd23e8SUladzislau Rezki (Sony) lva = __this_cpu_xchg(ne_fit_preload_node, NULL); 104782dd23e8SUladzislau Rezki (Sony) if (unlikely(!lva)) { 104882dd23e8SUladzislau Rezki (Sony) /* 104982dd23e8SUladzislau Rezki (Sony) * For percpu allocator we do not do any pre-allocation 105082dd23e8SUladzislau Rezki (Sony) * and leave it as it is. The reason is it most likely 105182dd23e8SUladzislau Rezki (Sony) * never ends up with NE_FIT_TYPE splitting. In case of 105282dd23e8SUladzislau Rezki (Sony) * percpu allocations offsets and sizes are aligned to 105382dd23e8SUladzislau Rezki (Sony) * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE 105482dd23e8SUladzislau Rezki (Sony) * are its main fitting cases. 105582dd23e8SUladzislau Rezki (Sony) * 105682dd23e8SUladzislau Rezki (Sony) * There are a few exceptions though, as an example it is 105782dd23e8SUladzislau Rezki (Sony) * a first allocation (early boot up) when we have "one" 105882dd23e8SUladzislau Rezki (Sony) * big free space that has to be split. 1059060650a2SUladzislau Rezki (Sony) * 1060060650a2SUladzislau Rezki (Sony) * Also we can hit this path in case of regular "vmap" 1061060650a2SUladzislau Rezki (Sony) * allocations, if "this" current CPU was not preloaded. 1062060650a2SUladzislau Rezki (Sony) * See the comment in alloc_vmap_area() why. If so, then 1063060650a2SUladzislau Rezki (Sony) * GFP_NOWAIT is used instead to get an extra object for 1064060650a2SUladzislau Rezki (Sony) * split purpose. That is rare and most time does not 1065060650a2SUladzislau Rezki (Sony) * occur. 1066060650a2SUladzislau Rezki (Sony) * 1067060650a2SUladzislau Rezki (Sony) * What happens if an allocation gets failed. Basically, 1068060650a2SUladzislau Rezki (Sony) * an "overflow" path is triggered to purge lazily freed 1069060650a2SUladzislau Rezki (Sony) * areas to free some memory, then, the "retry" path is 1070060650a2SUladzislau Rezki (Sony) * triggered to repeat one more time. See more details 1071060650a2SUladzislau Rezki (Sony) * in alloc_vmap_area() function. 107282dd23e8SUladzislau Rezki (Sony) */ 107368ad4a33SUladzislau Rezki (Sony) lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT); 107482dd23e8SUladzislau Rezki (Sony) if (!lva) 107568ad4a33SUladzislau Rezki (Sony) return -1; 107682dd23e8SUladzislau Rezki (Sony) } 107768ad4a33SUladzislau Rezki (Sony) 107868ad4a33SUladzislau Rezki (Sony) /* 107968ad4a33SUladzislau Rezki (Sony) * Build the remainder. 108068ad4a33SUladzislau Rezki (Sony) */ 108168ad4a33SUladzislau Rezki (Sony) lva->va_start = va->va_start; 108268ad4a33SUladzislau Rezki (Sony) lva->va_end = nva_start_addr; 108368ad4a33SUladzislau Rezki (Sony) 108468ad4a33SUladzislau Rezki (Sony) /* 108568ad4a33SUladzislau Rezki (Sony) * Shrink this VA to remaining size. 108668ad4a33SUladzislau Rezki (Sony) */ 108768ad4a33SUladzislau Rezki (Sony) va->va_start = nva_start_addr + size; 108868ad4a33SUladzislau Rezki (Sony) } else { 108968ad4a33SUladzislau Rezki (Sony) return -1; 109068ad4a33SUladzislau Rezki (Sony) } 109168ad4a33SUladzislau Rezki (Sony) 109268ad4a33SUladzislau Rezki (Sony) if (type != FL_FIT_TYPE) { 109368ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 109468ad4a33SUladzislau Rezki (Sony) 10952c929233SArnd Bergmann if (lva) /* type == NE_FIT_TYPE */ 109668ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(lva, &va->rb_node, 109768ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, &free_vmap_area_list); 109868ad4a33SUladzislau Rezki (Sony) } 109968ad4a33SUladzislau Rezki (Sony) 110068ad4a33SUladzislau Rezki (Sony) return 0; 110168ad4a33SUladzislau Rezki (Sony) } 110268ad4a33SUladzislau Rezki (Sony) 110368ad4a33SUladzislau Rezki (Sony) /* 110468ad4a33SUladzislau Rezki (Sony) * Returns a start address of the newly allocated area, if success. 110568ad4a33SUladzislau Rezki (Sony) * Otherwise a vend is returned that indicates failure. 110668ad4a33SUladzislau Rezki (Sony) */ 110768ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 110868ad4a33SUladzislau Rezki (Sony) __alloc_vmap_area(unsigned long size, unsigned long align, 1109cacca6baSUladzislau Rezki (Sony) unsigned long vstart, unsigned long vend) 111068ad4a33SUladzislau Rezki (Sony) { 111168ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr; 111268ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 111368ad4a33SUladzislau Rezki (Sony) enum fit_type type; 111468ad4a33SUladzislau Rezki (Sony) int ret; 111568ad4a33SUladzislau Rezki (Sony) 111668ad4a33SUladzislau Rezki (Sony) va = find_vmap_lowest_match(size, align, vstart); 111768ad4a33SUladzislau Rezki (Sony) if (unlikely(!va)) 111868ad4a33SUladzislau Rezki (Sony) return vend; 111968ad4a33SUladzislau Rezki (Sony) 112068ad4a33SUladzislau Rezki (Sony) if (va->va_start > vstart) 112168ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(va->va_start, align); 112268ad4a33SUladzislau Rezki (Sony) else 112368ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(vstart, align); 112468ad4a33SUladzislau Rezki (Sony) 112568ad4a33SUladzislau Rezki (Sony) /* Check the "vend" restriction. */ 112668ad4a33SUladzislau Rezki (Sony) if (nva_start_addr + size > vend) 112768ad4a33SUladzislau Rezki (Sony) return vend; 112868ad4a33SUladzislau Rezki (Sony) 112968ad4a33SUladzislau Rezki (Sony) /* Classify what we have found. */ 113068ad4a33SUladzislau Rezki (Sony) type = classify_va_fit_type(va, nva_start_addr, size); 113168ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(type == NOTHING_FIT)) 113268ad4a33SUladzislau Rezki (Sony) return vend; 113368ad4a33SUladzislau Rezki (Sony) 113468ad4a33SUladzislau Rezki (Sony) /* Update the free vmap_area. */ 113568ad4a33SUladzislau Rezki (Sony) ret = adjust_va_to_fit_type(va, nva_start_addr, size, type); 113668ad4a33SUladzislau Rezki (Sony) if (ret) 113768ad4a33SUladzislau Rezki (Sony) return vend; 113868ad4a33SUladzislau Rezki (Sony) 1139a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 1140a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_match_check(size); 1141a6cf4e0fSUladzislau Rezki (Sony) #endif 1142a6cf4e0fSUladzislau Rezki (Sony) 114368ad4a33SUladzislau Rezki (Sony) return nva_start_addr; 114468ad4a33SUladzislau Rezki (Sony) } 11454da56b99SChris Wilson 1146db64fe02SNick Piggin /* 1147d98c9e83SAndrey Ryabinin * Free a region of KVA allocated by alloc_vmap_area 1148d98c9e83SAndrey Ryabinin */ 1149d98c9e83SAndrey Ryabinin static void free_vmap_area(struct vmap_area *va) 1150d98c9e83SAndrey Ryabinin { 1151d98c9e83SAndrey Ryabinin /* 1152d98c9e83SAndrey Ryabinin * Remove from the busy tree/list. 1153d98c9e83SAndrey Ryabinin */ 1154d98c9e83SAndrey Ryabinin spin_lock(&vmap_area_lock); 1155d98c9e83SAndrey Ryabinin unlink_va(va, &vmap_area_root); 1156d98c9e83SAndrey Ryabinin spin_unlock(&vmap_area_lock); 1157d98c9e83SAndrey Ryabinin 1158d98c9e83SAndrey Ryabinin /* 1159d98c9e83SAndrey Ryabinin * Insert/Merge it back to the free tree/list. 1160d98c9e83SAndrey Ryabinin */ 1161d98c9e83SAndrey Ryabinin spin_lock(&free_vmap_area_lock); 116296e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list); 1163d98c9e83SAndrey Ryabinin spin_unlock(&free_vmap_area_lock); 1164d98c9e83SAndrey Ryabinin } 1165d98c9e83SAndrey Ryabinin 1166d98c9e83SAndrey Ryabinin /* 1167db64fe02SNick Piggin * Allocate a region of KVA of the specified size and alignment, within the 1168db64fe02SNick Piggin * vstart and vend. 1169db64fe02SNick Piggin */ 1170db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size, 1171db64fe02SNick Piggin unsigned long align, 1172db64fe02SNick Piggin unsigned long vstart, unsigned long vend, 1173db64fe02SNick Piggin int node, gfp_t gfp_mask) 1174db64fe02SNick Piggin { 117582dd23e8SUladzislau Rezki (Sony) struct vmap_area *va, *pva; 11761da177e4SLinus Torvalds unsigned long addr; 1177db64fe02SNick Piggin int purged = 0; 1178d98c9e83SAndrey Ryabinin int ret; 1179db64fe02SNick Piggin 11807766970cSNick Piggin BUG_ON(!size); 1181891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 118289699605SNick Piggin BUG_ON(!is_power_of_2(align)); 1183db64fe02SNick Piggin 118468ad4a33SUladzislau Rezki (Sony) if (unlikely(!vmap_initialized)) 118568ad4a33SUladzislau Rezki (Sony) return ERR_PTR(-EBUSY); 118668ad4a33SUladzislau Rezki (Sony) 11875803ed29SChristoph Hellwig might_sleep(); 1188f07116d7SUladzislau Rezki (Sony) gfp_mask = gfp_mask & GFP_RECLAIM_MASK; 11894da56b99SChris Wilson 1190f07116d7SUladzislau Rezki (Sony) va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); 1191db64fe02SNick Piggin if (unlikely(!va)) 1192db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 1193db64fe02SNick Piggin 11947f88f88fSCatalin Marinas /* 11957f88f88fSCatalin Marinas * Only scan the relevant parts containing pointers to other objects 11967f88f88fSCatalin Marinas * to avoid false negatives. 11977f88f88fSCatalin Marinas */ 1198f07116d7SUladzislau Rezki (Sony) kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask); 11997f88f88fSCatalin Marinas 1200db64fe02SNick Piggin retry: 120182dd23e8SUladzislau Rezki (Sony) /* 120281f1ba58SUladzislau Rezki (Sony) * Preload this CPU with one extra vmap_area object. It is used 120381f1ba58SUladzislau Rezki (Sony) * when fit type of free area is NE_FIT_TYPE. Please note, it 120481f1ba58SUladzislau Rezki (Sony) * does not guarantee that an allocation occurs on a CPU that 120581f1ba58SUladzislau Rezki (Sony) * is preloaded, instead we minimize the case when it is not. 120681f1ba58SUladzislau Rezki (Sony) * It can happen because of cpu migration, because there is a 120781f1ba58SUladzislau Rezki (Sony) * race until the below spinlock is taken. 120882dd23e8SUladzislau Rezki (Sony) * 120982dd23e8SUladzislau Rezki (Sony) * The preload is done in non-atomic context, thus it allows us 121082dd23e8SUladzislau Rezki (Sony) * to use more permissive allocation masks to be more stable under 121181f1ba58SUladzislau Rezki (Sony) * low memory condition and high memory pressure. In rare case, 121281f1ba58SUladzislau Rezki (Sony) * if not preloaded, GFP_NOWAIT is used. 121382dd23e8SUladzislau Rezki (Sony) * 121481f1ba58SUladzislau Rezki (Sony) * Set "pva" to NULL here, because of "retry" path. 121582dd23e8SUladzislau Rezki (Sony) */ 121681f1ba58SUladzislau Rezki (Sony) pva = NULL; 121782dd23e8SUladzislau Rezki (Sony) 121881f1ba58SUladzislau Rezki (Sony) if (!this_cpu_read(ne_fit_preload_node)) 121981f1ba58SUladzislau Rezki (Sony) /* 122081f1ba58SUladzislau Rezki (Sony) * Even if it fails we do not really care about that. 122181f1ba58SUladzislau Rezki (Sony) * Just proceed as it is. If needed "overflow" path 122281f1ba58SUladzislau Rezki (Sony) * will refill the cache we allocate from. 122381f1ba58SUladzislau Rezki (Sony) */ 1224f07116d7SUladzislau Rezki (Sony) pva = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); 122582dd23e8SUladzislau Rezki (Sony) 1226e36176beSUladzislau Rezki (Sony) spin_lock(&free_vmap_area_lock); 122781f1ba58SUladzislau Rezki (Sony) 122881f1ba58SUladzislau Rezki (Sony) if (pva && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, pva)) 122981f1ba58SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, pva); 123068ad4a33SUladzislau Rezki (Sony) 123189699605SNick Piggin /* 123268ad4a33SUladzislau Rezki (Sony) * If an allocation fails, the "vend" address is 123368ad4a33SUladzislau Rezki (Sony) * returned. Therefore trigger the overflow path. 123489699605SNick Piggin */ 1235cacca6baSUladzislau Rezki (Sony) addr = __alloc_vmap_area(size, align, vstart, vend); 1236e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 1237e36176beSUladzislau Rezki (Sony) 123868ad4a33SUladzislau Rezki (Sony) if (unlikely(addr == vend)) 123989699605SNick Piggin goto overflow; 124089699605SNick Piggin 124189699605SNick Piggin va->va_start = addr; 124289699605SNick Piggin va->va_end = addr + size; 1243688fcbfcSPengfei Li va->vm = NULL; 124468ad4a33SUladzislau Rezki (Sony) 1245d98c9e83SAndrey Ryabinin 1246e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 1247e36176beSUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 124889699605SNick Piggin spin_unlock(&vmap_area_lock); 124989699605SNick Piggin 125061e16557SWang Xiaoqiang BUG_ON(!IS_ALIGNED(va->va_start, align)); 125189699605SNick Piggin BUG_ON(va->va_start < vstart); 125289699605SNick Piggin BUG_ON(va->va_end > vend); 125389699605SNick Piggin 1254d98c9e83SAndrey Ryabinin ret = kasan_populate_vmalloc(addr, size); 1255d98c9e83SAndrey Ryabinin if (ret) { 1256d98c9e83SAndrey Ryabinin free_vmap_area(va); 1257d98c9e83SAndrey Ryabinin return ERR_PTR(ret); 1258d98c9e83SAndrey Ryabinin } 1259d98c9e83SAndrey Ryabinin 126089699605SNick Piggin return va; 126189699605SNick Piggin 12627766970cSNick Piggin overflow: 1263db64fe02SNick Piggin if (!purged) { 1264db64fe02SNick Piggin purge_vmap_area_lazy(); 1265db64fe02SNick Piggin purged = 1; 1266db64fe02SNick Piggin goto retry; 1267db64fe02SNick Piggin } 12684da56b99SChris Wilson 12694da56b99SChris Wilson if (gfpflags_allow_blocking(gfp_mask)) { 12704da56b99SChris Wilson unsigned long freed = 0; 12714da56b99SChris Wilson blocking_notifier_call_chain(&vmap_notify_list, 0, &freed); 12724da56b99SChris Wilson if (freed > 0) { 12734da56b99SChris Wilson purged = 0; 12744da56b99SChris Wilson goto retry; 12754da56b99SChris Wilson } 12764da56b99SChris Wilson } 12774da56b99SChris Wilson 127803497d76SFlorian Fainelli if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) 1279756a025fSJoe Perches pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n", 1280756a025fSJoe Perches size); 128168ad4a33SUladzislau Rezki (Sony) 128268ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 1283db64fe02SNick Piggin return ERR_PTR(-EBUSY); 1284db64fe02SNick Piggin } 1285db64fe02SNick Piggin 12864da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb) 12874da56b99SChris Wilson { 12884da56b99SChris Wilson return blocking_notifier_chain_register(&vmap_notify_list, nb); 12894da56b99SChris Wilson } 12904da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier); 12914da56b99SChris Wilson 12924da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb) 12934da56b99SChris Wilson { 12944da56b99SChris Wilson return blocking_notifier_chain_unregister(&vmap_notify_list, nb); 12954da56b99SChris Wilson } 12964da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier); 12974da56b99SChris Wilson 1298db64fe02SNick Piggin /* 1299db64fe02SNick Piggin * lazy_max_pages is the maximum amount of virtual address space we gather up 1300db64fe02SNick Piggin * before attempting to purge with a TLB flush. 1301db64fe02SNick Piggin * 1302db64fe02SNick Piggin * There is a tradeoff here: a larger number will cover more kernel page tables 1303db64fe02SNick Piggin * and take slightly longer to purge, but it will linearly reduce the number of 1304db64fe02SNick Piggin * global TLB flushes that must be performed. It would seem natural to scale 1305db64fe02SNick Piggin * this number up linearly with the number of CPUs (because vmapping activity 1306db64fe02SNick Piggin * could also scale linearly with the number of CPUs), however it is likely 1307db64fe02SNick Piggin * that in practice, workloads might be constrained in other ways that mean 1308db64fe02SNick Piggin * vmap activity will not scale linearly with CPUs. Also, I want to be 1309db64fe02SNick Piggin * conservative and not introduce a big latency on huge systems, so go with 1310db64fe02SNick Piggin * a less aggressive log scale. It will still be an improvement over the old 1311db64fe02SNick Piggin * code, and it will be simple to change the scale factor if we find that it 1312db64fe02SNick Piggin * becomes a problem on bigger systems. 1313db64fe02SNick Piggin */ 1314db64fe02SNick Piggin static unsigned long lazy_max_pages(void) 1315db64fe02SNick Piggin { 1316db64fe02SNick Piggin unsigned int log; 1317db64fe02SNick Piggin 1318db64fe02SNick Piggin log = fls(num_online_cpus()); 1319db64fe02SNick Piggin 1320db64fe02SNick Piggin return log * (32UL * 1024 * 1024 / PAGE_SIZE); 1321db64fe02SNick Piggin } 1322db64fe02SNick Piggin 13234d36e6f8SUladzislau Rezki (Sony) static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0); 1324db64fe02SNick Piggin 13250574ecd1SChristoph Hellwig /* 13260574ecd1SChristoph Hellwig * Serialize vmap purging. There is no actual criticial section protected 13270574ecd1SChristoph Hellwig * by this look, but we want to avoid concurrent calls for performance 13280574ecd1SChristoph Hellwig * reasons and to make the pcpu_get_vm_areas more deterministic. 13290574ecd1SChristoph Hellwig */ 1330f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock); 13310574ecd1SChristoph Hellwig 133202b709dfSNick Piggin /* for per-CPU blocks */ 133302b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void); 133402b709dfSNick Piggin 1335db64fe02SNick Piggin /* 13363ee48b6aSCliff Wickman * called before a call to iounmap() if the caller wants vm_area_struct's 13373ee48b6aSCliff Wickman * immediately freed. 13383ee48b6aSCliff Wickman */ 13393ee48b6aSCliff Wickman void set_iounmap_nonlazy(void) 13403ee48b6aSCliff Wickman { 13414d36e6f8SUladzislau Rezki (Sony) atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1); 13423ee48b6aSCliff Wickman } 13433ee48b6aSCliff Wickman 13443ee48b6aSCliff Wickman /* 1345db64fe02SNick Piggin * Purges all lazily-freed vmap areas. 1346db64fe02SNick Piggin */ 13470574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end) 1348db64fe02SNick Piggin { 13494d36e6f8SUladzislau Rezki (Sony) unsigned long resched_threshold; 135096e2db45SUladzislau Rezki (Sony) struct list_head local_pure_list; 135196e2db45SUladzislau Rezki (Sony) struct vmap_area *va, *n_va; 1352db64fe02SNick Piggin 13530574ecd1SChristoph Hellwig lockdep_assert_held(&vmap_purge_lock); 135402b709dfSNick Piggin 135596e2db45SUladzislau Rezki (Sony) spin_lock(&purge_vmap_area_lock); 135696e2db45SUladzislau Rezki (Sony) purge_vmap_area_root = RB_ROOT; 135796e2db45SUladzislau Rezki (Sony) list_replace_init(&purge_vmap_area_list, &local_pure_list); 135896e2db45SUladzislau Rezki (Sony) spin_unlock(&purge_vmap_area_lock); 135996e2db45SUladzislau Rezki (Sony) 136096e2db45SUladzislau Rezki (Sony) if (unlikely(list_empty(&local_pure_list))) 136168571be9SUladzislau Rezki (Sony) return false; 136268571be9SUladzislau Rezki (Sony) 136396e2db45SUladzislau Rezki (Sony) start = min(start, 136496e2db45SUladzislau Rezki (Sony) list_first_entry(&local_pure_list, 136596e2db45SUladzislau Rezki (Sony) struct vmap_area, list)->va_start); 136696e2db45SUladzislau Rezki (Sony) 136796e2db45SUladzislau Rezki (Sony) end = max(end, 136896e2db45SUladzislau Rezki (Sony) list_last_entry(&local_pure_list, 136996e2db45SUladzislau Rezki (Sony) struct vmap_area, list)->va_end); 1370db64fe02SNick Piggin 13710574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 13724d36e6f8SUladzislau Rezki (Sony) resched_threshold = lazy_max_pages() << 1; 1373db64fe02SNick Piggin 1374e36176beSUladzislau Rezki (Sony) spin_lock(&free_vmap_area_lock); 137596e2db45SUladzislau Rezki (Sony) list_for_each_entry_safe(va, n_va, &local_pure_list, list) { 13764d36e6f8SUladzislau Rezki (Sony) unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT; 13773c5c3cfbSDaniel Axtens unsigned long orig_start = va->va_start; 13783c5c3cfbSDaniel Axtens unsigned long orig_end = va->va_end; 1379763b218dSJoel Fernandes 1380dd3b8353SUladzislau Rezki (Sony) /* 1381dd3b8353SUladzislau Rezki (Sony) * Finally insert or merge lazily-freed area. It is 1382dd3b8353SUladzislau Rezki (Sony) * detached and there is no need to "unlink" it from 1383dd3b8353SUladzislau Rezki (Sony) * anything. 1384dd3b8353SUladzislau Rezki (Sony) */ 138596e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root, 13863c5c3cfbSDaniel Axtens &free_vmap_area_list); 13873c5c3cfbSDaniel Axtens 13889c801f61SUladzislau Rezki (Sony) if (!va) 13899c801f61SUladzislau Rezki (Sony) continue; 13909c801f61SUladzislau Rezki (Sony) 13913c5c3cfbSDaniel Axtens if (is_vmalloc_or_module_addr((void *)orig_start)) 13923c5c3cfbSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 13933c5c3cfbSDaniel Axtens va->va_start, va->va_end); 1394dd3b8353SUladzislau Rezki (Sony) 13954d36e6f8SUladzislau Rezki (Sony) atomic_long_sub(nr, &vmap_lazy_nr); 139668571be9SUladzislau Rezki (Sony) 13974d36e6f8SUladzislau Rezki (Sony) if (atomic_long_read(&vmap_lazy_nr) < resched_threshold) 1398e36176beSUladzislau Rezki (Sony) cond_resched_lock(&free_vmap_area_lock); 1399763b218dSJoel Fernandes } 1400e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 14010574ecd1SChristoph Hellwig return true; 1402db64fe02SNick Piggin } 1403db64fe02SNick Piggin 1404db64fe02SNick Piggin /* 1405496850e5SNick Piggin * Kick off a purge of the outstanding lazy areas. Don't bother if somebody 1406496850e5SNick Piggin * is already purging. 1407496850e5SNick Piggin */ 1408496850e5SNick Piggin static void try_purge_vmap_area_lazy(void) 1409496850e5SNick Piggin { 1410f9e09977SChristoph Hellwig if (mutex_trylock(&vmap_purge_lock)) { 14110574ecd1SChristoph Hellwig __purge_vmap_area_lazy(ULONG_MAX, 0); 1412f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 14130574ecd1SChristoph Hellwig } 1414496850e5SNick Piggin } 1415496850e5SNick Piggin 1416496850e5SNick Piggin /* 1417db64fe02SNick Piggin * Kick off a purge of the outstanding lazy areas. 1418db64fe02SNick Piggin */ 1419db64fe02SNick Piggin static void purge_vmap_area_lazy(void) 1420db64fe02SNick Piggin { 1421f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 14220574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 14230574ecd1SChristoph Hellwig __purge_vmap_area_lazy(ULONG_MAX, 0); 1424f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 1425db64fe02SNick Piggin } 1426db64fe02SNick Piggin 1427db64fe02SNick Piggin /* 142864141da5SJeremy Fitzhardinge * Free a vmap area, caller ensuring that the area has been unmapped 142964141da5SJeremy Fitzhardinge * and flush_cache_vunmap had been called for the correct range 143064141da5SJeremy Fitzhardinge * previously. 1431db64fe02SNick Piggin */ 143264141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va) 1433db64fe02SNick Piggin { 14344d36e6f8SUladzislau Rezki (Sony) unsigned long nr_lazy; 143580c4bd7aSChris Wilson 1436dd3b8353SUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 1437dd3b8353SUladzislau Rezki (Sony) unlink_va(va, &vmap_area_root); 1438dd3b8353SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 1439dd3b8353SUladzislau Rezki (Sony) 14404d36e6f8SUladzislau Rezki (Sony) nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >> 14414d36e6f8SUladzislau Rezki (Sony) PAGE_SHIFT, &vmap_lazy_nr); 144280c4bd7aSChris Wilson 144396e2db45SUladzislau Rezki (Sony) /* 144496e2db45SUladzislau Rezki (Sony) * Merge or place it to the purge tree/list. 144596e2db45SUladzislau Rezki (Sony) */ 144696e2db45SUladzislau Rezki (Sony) spin_lock(&purge_vmap_area_lock); 144796e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area(va, 144896e2db45SUladzislau Rezki (Sony) &purge_vmap_area_root, &purge_vmap_area_list); 144996e2db45SUladzislau Rezki (Sony) spin_unlock(&purge_vmap_area_lock); 145080c4bd7aSChris Wilson 145196e2db45SUladzislau Rezki (Sony) /* After this point, we may free va at any time */ 145280c4bd7aSChris Wilson if (unlikely(nr_lazy > lazy_max_pages())) 1453496850e5SNick Piggin try_purge_vmap_area_lazy(); 1454db64fe02SNick Piggin } 1455db64fe02SNick Piggin 1456b29acbdcSNick Piggin /* 1457b29acbdcSNick Piggin * Free and unmap a vmap area 1458b29acbdcSNick Piggin */ 1459b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va) 1460b29acbdcSNick Piggin { 1461b29acbdcSNick Piggin flush_cache_vunmap(va->va_start, va->va_end); 1462855e57a1SChristoph Hellwig unmap_kernel_range_noflush(va->va_start, va->va_end - va->va_start); 14638e57f8acSVlastimil Babka if (debug_pagealloc_enabled_static()) 146482a2e924SChintan Pandya flush_tlb_kernel_range(va->va_start, va->va_end); 146582a2e924SChintan Pandya 1466c8eef01eSChristoph Hellwig free_vmap_area_noflush(va); 1467b29acbdcSNick Piggin } 1468b29acbdcSNick Piggin 1469db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr) 1470db64fe02SNick Piggin { 1471db64fe02SNick Piggin struct vmap_area *va; 1472db64fe02SNick Piggin 1473db64fe02SNick Piggin spin_lock(&vmap_area_lock); 1474db64fe02SNick Piggin va = __find_vmap_area(addr); 1475db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 1476db64fe02SNick Piggin 1477db64fe02SNick Piggin return va; 1478db64fe02SNick Piggin } 1479db64fe02SNick Piggin 1480db64fe02SNick Piggin /*** Per cpu kva allocator ***/ 1481db64fe02SNick Piggin 1482db64fe02SNick Piggin /* 1483db64fe02SNick Piggin * vmap space is limited especially on 32 bit architectures. Ensure there is 1484db64fe02SNick Piggin * room for at least 16 percpu vmap blocks per CPU. 1485db64fe02SNick Piggin */ 1486db64fe02SNick Piggin /* 1487db64fe02SNick Piggin * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able 1488db64fe02SNick Piggin * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess 1489db64fe02SNick Piggin * instead (we just need a rough idea) 1490db64fe02SNick Piggin */ 1491db64fe02SNick Piggin #if BITS_PER_LONG == 32 1492db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024) 1493db64fe02SNick Piggin #else 1494db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024*1024) 1495db64fe02SNick Piggin #endif 1496db64fe02SNick Piggin 1497db64fe02SNick Piggin #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) 1498db64fe02SNick Piggin #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ 1499db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ 1500db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) 1501db64fe02SNick Piggin #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ 1502db64fe02SNick Piggin #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ 1503f982f915SClemens Ladisch #define VMAP_BBMAP_BITS \ 1504f982f915SClemens Ladisch VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ 1505db64fe02SNick Piggin VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ 1506f982f915SClemens Ladisch VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16)) 1507db64fe02SNick Piggin 1508db64fe02SNick Piggin #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) 1509db64fe02SNick Piggin 1510db64fe02SNick Piggin struct vmap_block_queue { 1511db64fe02SNick Piggin spinlock_t lock; 1512db64fe02SNick Piggin struct list_head free; 1513db64fe02SNick Piggin }; 1514db64fe02SNick Piggin 1515db64fe02SNick Piggin struct vmap_block { 1516db64fe02SNick Piggin spinlock_t lock; 1517db64fe02SNick Piggin struct vmap_area *va; 1518db64fe02SNick Piggin unsigned long free, dirty; 15197d61bfe8SRoman Pen unsigned long dirty_min, dirty_max; /*< dirty range */ 1520db64fe02SNick Piggin struct list_head free_list; 1521db64fe02SNick Piggin struct rcu_head rcu_head; 152202b709dfSNick Piggin struct list_head purge; 1523db64fe02SNick Piggin }; 1524db64fe02SNick Piggin 1525db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ 1526db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); 1527db64fe02SNick Piggin 1528db64fe02SNick Piggin /* 15290f14599cSMatthew Wilcox (Oracle) * XArray of vmap blocks, indexed by address, to quickly find a vmap block 1530db64fe02SNick Piggin * in the free path. Could get rid of this if we change the API to return a 1531db64fe02SNick Piggin * "cookie" from alloc, to be passed to free. But no big deal yet. 1532db64fe02SNick Piggin */ 15330f14599cSMatthew Wilcox (Oracle) static DEFINE_XARRAY(vmap_blocks); 1534db64fe02SNick Piggin 1535db64fe02SNick Piggin /* 1536db64fe02SNick Piggin * We should probably have a fallback mechanism to allocate virtual memory 1537db64fe02SNick Piggin * out of partially filled vmap blocks. However vmap block sizing should be 1538db64fe02SNick Piggin * fairly reasonable according to the vmalloc size, so it shouldn't be a 1539db64fe02SNick Piggin * big problem. 1540db64fe02SNick Piggin */ 1541db64fe02SNick Piggin 1542db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr) 1543db64fe02SNick Piggin { 1544db64fe02SNick Piggin addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); 1545db64fe02SNick Piggin addr /= VMAP_BLOCK_SIZE; 1546db64fe02SNick Piggin return addr; 1547db64fe02SNick Piggin } 1548db64fe02SNick Piggin 1549cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off) 1550cf725ce2SRoman Pen { 1551cf725ce2SRoman Pen unsigned long addr; 1552cf725ce2SRoman Pen 1553cf725ce2SRoman Pen addr = va_start + (pages_off << PAGE_SHIFT); 1554cf725ce2SRoman Pen BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start)); 1555cf725ce2SRoman Pen return (void *)addr; 1556cf725ce2SRoman Pen } 1557cf725ce2SRoman Pen 1558cf725ce2SRoman Pen /** 1559cf725ce2SRoman Pen * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this 1560cf725ce2SRoman Pen * block. Of course pages number can't exceed VMAP_BBMAP_BITS 1561cf725ce2SRoman Pen * @order: how many 2^order pages should be occupied in newly allocated block 1562cf725ce2SRoman Pen * @gfp_mask: flags for the page level allocator 1563cf725ce2SRoman Pen * 1564a862f68aSMike Rapoport * Return: virtual address in a newly allocated block or ERR_PTR(-errno) 1565cf725ce2SRoman Pen */ 1566cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) 1567db64fe02SNick Piggin { 1568db64fe02SNick Piggin struct vmap_block_queue *vbq; 1569db64fe02SNick Piggin struct vmap_block *vb; 1570db64fe02SNick Piggin struct vmap_area *va; 1571db64fe02SNick Piggin unsigned long vb_idx; 1572db64fe02SNick Piggin int node, err; 1573cf725ce2SRoman Pen void *vaddr; 1574db64fe02SNick Piggin 1575db64fe02SNick Piggin node = numa_node_id(); 1576db64fe02SNick Piggin 1577db64fe02SNick Piggin vb = kmalloc_node(sizeof(struct vmap_block), 1578db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 1579db64fe02SNick Piggin if (unlikely(!vb)) 1580db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 1581db64fe02SNick Piggin 1582db64fe02SNick Piggin va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, 1583db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, 1584db64fe02SNick Piggin node, gfp_mask); 1585ddf9c6d4STobias Klauser if (IS_ERR(va)) { 1586db64fe02SNick Piggin kfree(vb); 1587e7d86340SJulia Lawall return ERR_CAST(va); 1588db64fe02SNick Piggin } 1589db64fe02SNick Piggin 1590cf725ce2SRoman Pen vaddr = vmap_block_vaddr(va->va_start, 0); 1591db64fe02SNick Piggin spin_lock_init(&vb->lock); 1592db64fe02SNick Piggin vb->va = va; 1593cf725ce2SRoman Pen /* At least something should be left free */ 1594cf725ce2SRoman Pen BUG_ON(VMAP_BBMAP_BITS <= (1UL << order)); 1595cf725ce2SRoman Pen vb->free = VMAP_BBMAP_BITS - (1UL << order); 1596db64fe02SNick Piggin vb->dirty = 0; 15977d61bfe8SRoman Pen vb->dirty_min = VMAP_BBMAP_BITS; 15987d61bfe8SRoman Pen vb->dirty_max = 0; 1599db64fe02SNick Piggin INIT_LIST_HEAD(&vb->free_list); 1600db64fe02SNick Piggin 1601db64fe02SNick Piggin vb_idx = addr_to_vb_idx(va->va_start); 16020f14599cSMatthew Wilcox (Oracle) err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask); 16030f14599cSMatthew Wilcox (Oracle) if (err) { 16040f14599cSMatthew Wilcox (Oracle) kfree(vb); 16050f14599cSMatthew Wilcox (Oracle) free_vmap_area(va); 16060f14599cSMatthew Wilcox (Oracle) return ERR_PTR(err); 16070f14599cSMatthew Wilcox (Oracle) } 1608db64fe02SNick Piggin 1609db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 1610db64fe02SNick Piggin spin_lock(&vbq->lock); 161168ac546fSRoman Pen list_add_tail_rcu(&vb->free_list, &vbq->free); 1612db64fe02SNick Piggin spin_unlock(&vbq->lock); 16133f04ba85STejun Heo put_cpu_var(vmap_block_queue); 1614db64fe02SNick Piggin 1615cf725ce2SRoman Pen return vaddr; 1616db64fe02SNick Piggin } 1617db64fe02SNick Piggin 1618db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb) 1619db64fe02SNick Piggin { 1620db64fe02SNick Piggin struct vmap_block *tmp; 1621db64fe02SNick Piggin 16220f14599cSMatthew Wilcox (Oracle) tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start)); 1623db64fe02SNick Piggin BUG_ON(tmp != vb); 1624db64fe02SNick Piggin 162564141da5SJeremy Fitzhardinge free_vmap_area_noflush(vb->va); 162622a3c7d1SLai Jiangshan kfree_rcu(vb, rcu_head); 1627db64fe02SNick Piggin } 1628db64fe02SNick Piggin 162902b709dfSNick Piggin static void purge_fragmented_blocks(int cpu) 163002b709dfSNick Piggin { 163102b709dfSNick Piggin LIST_HEAD(purge); 163202b709dfSNick Piggin struct vmap_block *vb; 163302b709dfSNick Piggin struct vmap_block *n_vb; 163402b709dfSNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 163502b709dfSNick Piggin 163602b709dfSNick Piggin rcu_read_lock(); 163702b709dfSNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 163802b709dfSNick Piggin 163902b709dfSNick Piggin if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS)) 164002b709dfSNick Piggin continue; 164102b709dfSNick Piggin 164202b709dfSNick Piggin spin_lock(&vb->lock); 164302b709dfSNick Piggin if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) { 164402b709dfSNick Piggin vb->free = 0; /* prevent further allocs after releasing lock */ 164502b709dfSNick Piggin vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */ 16467d61bfe8SRoman Pen vb->dirty_min = 0; 16477d61bfe8SRoman Pen vb->dirty_max = VMAP_BBMAP_BITS; 164802b709dfSNick Piggin spin_lock(&vbq->lock); 164902b709dfSNick Piggin list_del_rcu(&vb->free_list); 165002b709dfSNick Piggin spin_unlock(&vbq->lock); 165102b709dfSNick Piggin spin_unlock(&vb->lock); 165202b709dfSNick Piggin list_add_tail(&vb->purge, &purge); 165302b709dfSNick Piggin } else 165402b709dfSNick Piggin spin_unlock(&vb->lock); 165502b709dfSNick Piggin } 165602b709dfSNick Piggin rcu_read_unlock(); 165702b709dfSNick Piggin 165802b709dfSNick Piggin list_for_each_entry_safe(vb, n_vb, &purge, purge) { 165902b709dfSNick Piggin list_del(&vb->purge); 166002b709dfSNick Piggin free_vmap_block(vb); 166102b709dfSNick Piggin } 166202b709dfSNick Piggin } 166302b709dfSNick Piggin 166402b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void) 166502b709dfSNick Piggin { 166602b709dfSNick Piggin int cpu; 166702b709dfSNick Piggin 166802b709dfSNick Piggin for_each_possible_cpu(cpu) 166902b709dfSNick Piggin purge_fragmented_blocks(cpu); 167002b709dfSNick Piggin } 167102b709dfSNick Piggin 1672db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask) 1673db64fe02SNick Piggin { 1674db64fe02SNick Piggin struct vmap_block_queue *vbq; 1675db64fe02SNick Piggin struct vmap_block *vb; 1676cf725ce2SRoman Pen void *vaddr = NULL; 1677db64fe02SNick Piggin unsigned int order; 1678db64fe02SNick Piggin 1679891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 1680db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 1681aa91c4d8SJan Kara if (WARN_ON(size == 0)) { 1682aa91c4d8SJan Kara /* 1683aa91c4d8SJan Kara * Allocating 0 bytes isn't what caller wants since 1684aa91c4d8SJan Kara * get_order(0) returns funny result. Just warn and terminate 1685aa91c4d8SJan Kara * early. 1686aa91c4d8SJan Kara */ 1687aa91c4d8SJan Kara return NULL; 1688aa91c4d8SJan Kara } 1689db64fe02SNick Piggin order = get_order(size); 1690db64fe02SNick Piggin 1691db64fe02SNick Piggin rcu_read_lock(); 1692db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 1693db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 1694cf725ce2SRoman Pen unsigned long pages_off; 1695db64fe02SNick Piggin 1696db64fe02SNick Piggin spin_lock(&vb->lock); 1697cf725ce2SRoman Pen if (vb->free < (1UL << order)) { 1698cf725ce2SRoman Pen spin_unlock(&vb->lock); 1699cf725ce2SRoman Pen continue; 1700cf725ce2SRoman Pen } 170102b709dfSNick Piggin 1702cf725ce2SRoman Pen pages_off = VMAP_BBMAP_BITS - vb->free; 1703cf725ce2SRoman Pen vaddr = vmap_block_vaddr(vb->va->va_start, pages_off); 1704db64fe02SNick Piggin vb->free -= 1UL << order; 1705db64fe02SNick Piggin if (vb->free == 0) { 1706db64fe02SNick Piggin spin_lock(&vbq->lock); 1707de560423SNick Piggin list_del_rcu(&vb->free_list); 1708db64fe02SNick Piggin spin_unlock(&vbq->lock); 1709db64fe02SNick Piggin } 1710cf725ce2SRoman Pen 1711db64fe02SNick Piggin spin_unlock(&vb->lock); 1712db64fe02SNick Piggin break; 1713db64fe02SNick Piggin } 171402b709dfSNick Piggin 17153f04ba85STejun Heo put_cpu_var(vmap_block_queue); 1716db64fe02SNick Piggin rcu_read_unlock(); 1717db64fe02SNick Piggin 1718cf725ce2SRoman Pen /* Allocate new block if nothing was found */ 1719cf725ce2SRoman Pen if (!vaddr) 1720cf725ce2SRoman Pen vaddr = new_vmap_block(order, gfp_mask); 1721db64fe02SNick Piggin 1722cf725ce2SRoman Pen return vaddr; 1723db64fe02SNick Piggin } 1724db64fe02SNick Piggin 172578a0e8c4SChristoph Hellwig static void vb_free(unsigned long addr, unsigned long size) 1726db64fe02SNick Piggin { 1727db64fe02SNick Piggin unsigned long offset; 1728db64fe02SNick Piggin unsigned int order; 1729db64fe02SNick Piggin struct vmap_block *vb; 1730db64fe02SNick Piggin 1731891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 1732db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 1733b29acbdcSNick Piggin 173478a0e8c4SChristoph Hellwig flush_cache_vunmap(addr, addr + size); 1735b29acbdcSNick Piggin 1736db64fe02SNick Piggin order = get_order(size); 173778a0e8c4SChristoph Hellwig offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT; 17380f14599cSMatthew Wilcox (Oracle) vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr)); 1739db64fe02SNick Piggin 1740b521c43fSChristoph Hellwig unmap_kernel_range_noflush(addr, size); 174164141da5SJeremy Fitzhardinge 17428e57f8acSVlastimil Babka if (debug_pagealloc_enabled_static()) 174378a0e8c4SChristoph Hellwig flush_tlb_kernel_range(addr, addr + size); 174482a2e924SChintan Pandya 1745db64fe02SNick Piggin spin_lock(&vb->lock); 17467d61bfe8SRoman Pen 17477d61bfe8SRoman Pen /* Expand dirty range */ 17487d61bfe8SRoman Pen vb->dirty_min = min(vb->dirty_min, offset); 17497d61bfe8SRoman Pen vb->dirty_max = max(vb->dirty_max, offset + (1UL << order)); 1750d086817dSMinChan Kim 1751db64fe02SNick Piggin vb->dirty += 1UL << order; 1752db64fe02SNick Piggin if (vb->dirty == VMAP_BBMAP_BITS) { 1753de560423SNick Piggin BUG_ON(vb->free); 1754db64fe02SNick Piggin spin_unlock(&vb->lock); 1755db64fe02SNick Piggin free_vmap_block(vb); 1756db64fe02SNick Piggin } else 1757db64fe02SNick Piggin spin_unlock(&vb->lock); 1758db64fe02SNick Piggin } 1759db64fe02SNick Piggin 1760868b104dSRick Edgecombe static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush) 1761db64fe02SNick Piggin { 1762db64fe02SNick Piggin int cpu; 1763db64fe02SNick Piggin 17649b463334SJeremy Fitzhardinge if (unlikely(!vmap_initialized)) 17659b463334SJeremy Fitzhardinge return; 17669b463334SJeremy Fitzhardinge 17675803ed29SChristoph Hellwig might_sleep(); 17685803ed29SChristoph Hellwig 1769db64fe02SNick Piggin for_each_possible_cpu(cpu) { 1770db64fe02SNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 1771db64fe02SNick Piggin struct vmap_block *vb; 1772db64fe02SNick Piggin 1773db64fe02SNick Piggin rcu_read_lock(); 1774db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 1775db64fe02SNick Piggin spin_lock(&vb->lock); 17767d61bfe8SRoman Pen if (vb->dirty) { 17777d61bfe8SRoman Pen unsigned long va_start = vb->va->va_start; 1778db64fe02SNick Piggin unsigned long s, e; 1779b136be5eSJoonsoo Kim 17807d61bfe8SRoman Pen s = va_start + (vb->dirty_min << PAGE_SHIFT); 17817d61bfe8SRoman Pen e = va_start + (vb->dirty_max << PAGE_SHIFT); 1782db64fe02SNick Piggin 17837d61bfe8SRoman Pen start = min(s, start); 17847d61bfe8SRoman Pen end = max(e, end); 17857d61bfe8SRoman Pen 1786db64fe02SNick Piggin flush = 1; 1787db64fe02SNick Piggin } 1788db64fe02SNick Piggin spin_unlock(&vb->lock); 1789db64fe02SNick Piggin } 1790db64fe02SNick Piggin rcu_read_unlock(); 1791db64fe02SNick Piggin } 1792db64fe02SNick Piggin 1793f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 17940574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 17950574ecd1SChristoph Hellwig if (!__purge_vmap_area_lazy(start, end) && flush) 17960574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 1797f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 1798db64fe02SNick Piggin } 1799868b104dSRick Edgecombe 1800868b104dSRick Edgecombe /** 1801868b104dSRick Edgecombe * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer 1802868b104dSRick Edgecombe * 1803868b104dSRick Edgecombe * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily 1804868b104dSRick Edgecombe * to amortize TLB flushing overheads. What this means is that any page you 1805868b104dSRick Edgecombe * have now, may, in a former life, have been mapped into kernel virtual 1806868b104dSRick Edgecombe * address by the vmap layer and so there might be some CPUs with TLB entries 1807868b104dSRick Edgecombe * still referencing that page (additional to the regular 1:1 kernel mapping). 1808868b104dSRick Edgecombe * 1809868b104dSRick Edgecombe * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can 1810868b104dSRick Edgecombe * be sure that none of the pages we have control over will have any aliases 1811868b104dSRick Edgecombe * from the vmap layer. 1812868b104dSRick Edgecombe */ 1813868b104dSRick Edgecombe void vm_unmap_aliases(void) 1814868b104dSRick Edgecombe { 1815868b104dSRick Edgecombe unsigned long start = ULONG_MAX, end = 0; 1816868b104dSRick Edgecombe int flush = 0; 1817868b104dSRick Edgecombe 1818868b104dSRick Edgecombe _vm_unmap_aliases(start, end, flush); 1819868b104dSRick Edgecombe } 1820db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases); 1821db64fe02SNick Piggin 1822db64fe02SNick Piggin /** 1823db64fe02SNick Piggin * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram 1824db64fe02SNick Piggin * @mem: the pointer returned by vm_map_ram 1825db64fe02SNick Piggin * @count: the count passed to that vm_map_ram call (cannot unmap partial) 1826db64fe02SNick Piggin */ 1827db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count) 1828db64fe02SNick Piggin { 182965ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 1830db64fe02SNick Piggin unsigned long addr = (unsigned long)mem; 18319c3acf60SChristoph Hellwig struct vmap_area *va; 1832db64fe02SNick Piggin 18335803ed29SChristoph Hellwig might_sleep(); 1834db64fe02SNick Piggin BUG_ON(!addr); 1835db64fe02SNick Piggin BUG_ON(addr < VMALLOC_START); 1836db64fe02SNick Piggin BUG_ON(addr > VMALLOC_END); 1837a1c0b1a0SShawn Lin BUG_ON(!PAGE_ALIGNED(addr)); 1838db64fe02SNick Piggin 1839d98c9e83SAndrey Ryabinin kasan_poison_vmalloc(mem, size); 1840d98c9e83SAndrey Ryabinin 18419c3acf60SChristoph Hellwig if (likely(count <= VMAP_MAX_ALLOC)) { 184205e3ff95SChintan Pandya debug_check_no_locks_freed(mem, size); 184378a0e8c4SChristoph Hellwig vb_free(addr, size); 18449c3acf60SChristoph Hellwig return; 18459c3acf60SChristoph Hellwig } 18469c3acf60SChristoph Hellwig 18479c3acf60SChristoph Hellwig va = find_vmap_area(addr); 18489c3acf60SChristoph Hellwig BUG_ON(!va); 184905e3ff95SChintan Pandya debug_check_no_locks_freed((void *)va->va_start, 185005e3ff95SChintan Pandya (va->va_end - va->va_start)); 18519c3acf60SChristoph Hellwig free_unmap_vmap_area(va); 1852db64fe02SNick Piggin } 1853db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram); 1854db64fe02SNick Piggin 1855db64fe02SNick Piggin /** 1856db64fe02SNick Piggin * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) 1857db64fe02SNick Piggin * @pages: an array of pointers to the pages to be mapped 1858db64fe02SNick Piggin * @count: number of pages 1859db64fe02SNick Piggin * @node: prefer to allocate data structures on this node 1860e99c97adSRandy Dunlap * 186136437638SGioh Kim * If you use this function for less than VMAP_MAX_ALLOC pages, it could be 186236437638SGioh Kim * faster than vmap so it's good. But if you mix long-life and short-life 186336437638SGioh Kim * objects with vm_map_ram(), it could consume lots of address space through 186436437638SGioh Kim * fragmentation (especially on a 32bit machine). You could see failures in 186536437638SGioh Kim * the end. Please use this function for short-lived objects. 186636437638SGioh Kim * 1867e99c97adSRandy Dunlap * Returns: a pointer to the address that has been mapped, or %NULL on failure 1868db64fe02SNick Piggin */ 1869d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node) 1870db64fe02SNick Piggin { 187165ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 1872db64fe02SNick Piggin unsigned long addr; 1873db64fe02SNick Piggin void *mem; 1874db64fe02SNick Piggin 1875db64fe02SNick Piggin if (likely(count <= VMAP_MAX_ALLOC)) { 1876db64fe02SNick Piggin mem = vb_alloc(size, GFP_KERNEL); 1877db64fe02SNick Piggin if (IS_ERR(mem)) 1878db64fe02SNick Piggin return NULL; 1879db64fe02SNick Piggin addr = (unsigned long)mem; 1880db64fe02SNick Piggin } else { 1881db64fe02SNick Piggin struct vmap_area *va; 1882db64fe02SNick Piggin va = alloc_vmap_area(size, PAGE_SIZE, 1883db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); 1884db64fe02SNick Piggin if (IS_ERR(va)) 1885db64fe02SNick Piggin return NULL; 1886db64fe02SNick Piggin 1887db64fe02SNick Piggin addr = va->va_start; 1888db64fe02SNick Piggin mem = (void *)addr; 1889db64fe02SNick Piggin } 1890d98c9e83SAndrey Ryabinin 1891d98c9e83SAndrey Ryabinin kasan_unpoison_vmalloc(mem, size); 1892d98c9e83SAndrey Ryabinin 1893d4efd79aSChristoph Hellwig if (map_kernel_range(addr, size, PAGE_KERNEL, pages) < 0) { 1894db64fe02SNick Piggin vm_unmap_ram(mem, count); 1895db64fe02SNick Piggin return NULL; 1896db64fe02SNick Piggin } 1897db64fe02SNick Piggin return mem; 1898db64fe02SNick Piggin } 1899db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram); 1900db64fe02SNick Piggin 19014341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata; 190292eac168SMike Rapoport 1903f0aa6617STejun Heo /** 1904be9b7335SNicolas Pitre * vm_area_add_early - add vmap area early during boot 1905be9b7335SNicolas Pitre * @vm: vm_struct to add 1906be9b7335SNicolas Pitre * 1907be9b7335SNicolas Pitre * This function is used to add fixed kernel vm area to vmlist before 1908be9b7335SNicolas Pitre * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags 1909be9b7335SNicolas Pitre * should contain proper values and the other fields should be zero. 1910be9b7335SNicolas Pitre * 1911be9b7335SNicolas Pitre * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 1912be9b7335SNicolas Pitre */ 1913be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm) 1914be9b7335SNicolas Pitre { 1915be9b7335SNicolas Pitre struct vm_struct *tmp, **p; 1916be9b7335SNicolas Pitre 1917be9b7335SNicolas Pitre BUG_ON(vmap_initialized); 1918be9b7335SNicolas Pitre for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { 1919be9b7335SNicolas Pitre if (tmp->addr >= vm->addr) { 1920be9b7335SNicolas Pitre BUG_ON(tmp->addr < vm->addr + vm->size); 1921be9b7335SNicolas Pitre break; 1922be9b7335SNicolas Pitre } else 1923be9b7335SNicolas Pitre BUG_ON(tmp->addr + tmp->size > vm->addr); 1924be9b7335SNicolas Pitre } 1925be9b7335SNicolas Pitre vm->next = *p; 1926be9b7335SNicolas Pitre *p = vm; 1927be9b7335SNicolas Pitre } 1928be9b7335SNicolas Pitre 1929be9b7335SNicolas Pitre /** 1930f0aa6617STejun Heo * vm_area_register_early - register vmap area early during boot 1931f0aa6617STejun Heo * @vm: vm_struct to register 1932c0c0a293STejun Heo * @align: requested alignment 1933f0aa6617STejun Heo * 1934f0aa6617STejun Heo * This function is used to register kernel vm area before 1935f0aa6617STejun Heo * vmalloc_init() is called. @vm->size and @vm->flags should contain 1936f0aa6617STejun Heo * proper values on entry and other fields should be zero. On return, 1937f0aa6617STejun Heo * vm->addr contains the allocated address. 1938f0aa6617STejun Heo * 1939f0aa6617STejun Heo * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 1940f0aa6617STejun Heo */ 1941c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align) 1942f0aa6617STejun Heo { 1943f0aa6617STejun Heo static size_t vm_init_off __initdata; 1944c0c0a293STejun Heo unsigned long addr; 1945f0aa6617STejun Heo 1946c0c0a293STejun Heo addr = ALIGN(VMALLOC_START + vm_init_off, align); 1947c0c0a293STejun Heo vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START; 1948c0c0a293STejun Heo 1949c0c0a293STejun Heo vm->addr = (void *)addr; 1950f0aa6617STejun Heo 1951be9b7335SNicolas Pitre vm_area_add_early(vm); 1952f0aa6617STejun Heo } 1953f0aa6617STejun Heo 195468ad4a33SUladzislau Rezki (Sony) static void vmap_init_free_space(void) 195568ad4a33SUladzislau Rezki (Sony) { 195668ad4a33SUladzislau Rezki (Sony) unsigned long vmap_start = 1; 195768ad4a33SUladzislau Rezki (Sony) const unsigned long vmap_end = ULONG_MAX; 195868ad4a33SUladzislau Rezki (Sony) struct vmap_area *busy, *free; 195968ad4a33SUladzislau Rezki (Sony) 196068ad4a33SUladzislau Rezki (Sony) /* 196168ad4a33SUladzislau Rezki (Sony) * B F B B B F 196268ad4a33SUladzislau Rezki (Sony) * -|-----|.....|-----|-----|-----|.....|- 196368ad4a33SUladzislau Rezki (Sony) * | The KVA space | 196468ad4a33SUladzislau Rezki (Sony) * |<--------------------------------->| 196568ad4a33SUladzislau Rezki (Sony) */ 196668ad4a33SUladzislau Rezki (Sony) list_for_each_entry(busy, &vmap_area_list, list) { 196768ad4a33SUladzislau Rezki (Sony) if (busy->va_start - vmap_start > 0) { 196868ad4a33SUladzislau Rezki (Sony) free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 196968ad4a33SUladzislau Rezki (Sony) if (!WARN_ON_ONCE(!free)) { 197068ad4a33SUladzislau Rezki (Sony) free->va_start = vmap_start; 197168ad4a33SUladzislau Rezki (Sony) free->va_end = busy->va_start; 197268ad4a33SUladzislau Rezki (Sony) 197368ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(free, NULL, 197468ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, 197568ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list); 197668ad4a33SUladzislau Rezki (Sony) } 197768ad4a33SUladzislau Rezki (Sony) } 197868ad4a33SUladzislau Rezki (Sony) 197968ad4a33SUladzislau Rezki (Sony) vmap_start = busy->va_end; 198068ad4a33SUladzislau Rezki (Sony) } 198168ad4a33SUladzislau Rezki (Sony) 198268ad4a33SUladzislau Rezki (Sony) if (vmap_end - vmap_start > 0) { 198368ad4a33SUladzislau Rezki (Sony) free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 198468ad4a33SUladzislau Rezki (Sony) if (!WARN_ON_ONCE(!free)) { 198568ad4a33SUladzislau Rezki (Sony) free->va_start = vmap_start; 198668ad4a33SUladzislau Rezki (Sony) free->va_end = vmap_end; 198768ad4a33SUladzislau Rezki (Sony) 198868ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(free, NULL, 198968ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, 199068ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list); 199168ad4a33SUladzislau Rezki (Sony) } 199268ad4a33SUladzislau Rezki (Sony) } 199368ad4a33SUladzislau Rezki (Sony) } 199468ad4a33SUladzislau Rezki (Sony) 1995db64fe02SNick Piggin void __init vmalloc_init(void) 1996db64fe02SNick Piggin { 1997822c18f2SIvan Kokshaysky struct vmap_area *va; 1998822c18f2SIvan Kokshaysky struct vm_struct *tmp; 1999db64fe02SNick Piggin int i; 2000db64fe02SNick Piggin 200168ad4a33SUladzislau Rezki (Sony) /* 200268ad4a33SUladzislau Rezki (Sony) * Create the cache for vmap_area objects. 200368ad4a33SUladzislau Rezki (Sony) */ 200468ad4a33SUladzislau Rezki (Sony) vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC); 200568ad4a33SUladzislau Rezki (Sony) 2006db64fe02SNick Piggin for_each_possible_cpu(i) { 2007db64fe02SNick Piggin struct vmap_block_queue *vbq; 200832fcfd40SAl Viro struct vfree_deferred *p; 2009db64fe02SNick Piggin 2010db64fe02SNick Piggin vbq = &per_cpu(vmap_block_queue, i); 2011db64fe02SNick Piggin spin_lock_init(&vbq->lock); 2012db64fe02SNick Piggin INIT_LIST_HEAD(&vbq->free); 201332fcfd40SAl Viro p = &per_cpu(vfree_deferred, i); 201432fcfd40SAl Viro init_llist_head(&p->list); 201532fcfd40SAl Viro INIT_WORK(&p->wq, free_work); 2016db64fe02SNick Piggin } 20179b463334SJeremy Fitzhardinge 2018822c18f2SIvan Kokshaysky /* Import existing vmlist entries. */ 2019822c18f2SIvan Kokshaysky for (tmp = vmlist; tmp; tmp = tmp->next) { 202068ad4a33SUladzislau Rezki (Sony) va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 202168ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(!va)) 202268ad4a33SUladzislau Rezki (Sony) continue; 202368ad4a33SUladzislau Rezki (Sony) 2024822c18f2SIvan Kokshaysky va->va_start = (unsigned long)tmp->addr; 2025822c18f2SIvan Kokshaysky va->va_end = va->va_start + tmp->size; 2026dbda591dSKyongHo va->vm = tmp; 202768ad4a33SUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 2028822c18f2SIvan Kokshaysky } 2029ca23e405STejun Heo 203068ad4a33SUladzislau Rezki (Sony) /* 203168ad4a33SUladzislau Rezki (Sony) * Now we can initialize a free vmap space. 203268ad4a33SUladzislau Rezki (Sony) */ 203368ad4a33SUladzislau Rezki (Sony) vmap_init_free_space(); 20349b463334SJeremy Fitzhardinge vmap_initialized = true; 2035db64fe02SNick Piggin } 2036db64fe02SNick Piggin 20378fc48985STejun Heo /** 20388fc48985STejun Heo * unmap_kernel_range - unmap kernel VM area and flush cache and TLB 20398fc48985STejun Heo * @addr: start of the VM area to unmap 20408fc48985STejun Heo * @size: size of the VM area to unmap 20418fc48985STejun Heo * 20428fc48985STejun Heo * Similar to unmap_kernel_range_noflush() but flushes vcache before 20438fc48985STejun Heo * the unmapping and tlb after. 20448fc48985STejun Heo */ 2045db64fe02SNick Piggin void unmap_kernel_range(unsigned long addr, unsigned long size) 2046db64fe02SNick Piggin { 2047db64fe02SNick Piggin unsigned long end = addr + size; 2048f6fcba70STejun Heo 2049f6fcba70STejun Heo flush_cache_vunmap(addr, end); 2050b521c43fSChristoph Hellwig unmap_kernel_range_noflush(addr, size); 2051db64fe02SNick Piggin flush_tlb_kernel_range(addr, end); 2052db64fe02SNick Piggin } 2053db64fe02SNick Piggin 2054e36176beSUladzislau Rezki (Sony) static inline void setup_vmalloc_vm_locked(struct vm_struct *vm, 2055e36176beSUladzislau Rezki (Sony) struct vmap_area *va, unsigned long flags, const void *caller) 2056cf88c790STejun Heo { 2057cf88c790STejun Heo vm->flags = flags; 2058cf88c790STejun Heo vm->addr = (void *)va->va_start; 2059cf88c790STejun Heo vm->size = va->va_end - va->va_start; 2060cf88c790STejun Heo vm->caller = caller; 2061db1aecafSMinchan Kim va->vm = vm; 2062e36176beSUladzislau Rezki (Sony) } 2063e36176beSUladzislau Rezki (Sony) 2064e36176beSUladzislau Rezki (Sony) static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, 2065e36176beSUladzislau Rezki (Sony) unsigned long flags, const void *caller) 2066e36176beSUladzislau Rezki (Sony) { 2067e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 2068e36176beSUladzislau Rezki (Sony) setup_vmalloc_vm_locked(vm, va, flags, caller); 2069c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 2070f5252e00SMitsuo Hayasaka } 2071cf88c790STejun Heo 207220fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm) 2073f5252e00SMitsuo Hayasaka { 2074d4033afdSJoonsoo Kim /* 207520fc02b4SZhang Yanfei * Before removing VM_UNINITIALIZED, 2076d4033afdSJoonsoo Kim * we should make sure that vm has proper values. 2077d4033afdSJoonsoo Kim * Pair with smp_rmb() in show_numa_info(). 2078d4033afdSJoonsoo Kim */ 2079d4033afdSJoonsoo Kim smp_wmb(); 208020fc02b4SZhang Yanfei vm->flags &= ~VM_UNINITIALIZED; 2081cf88c790STejun Heo } 2082cf88c790STejun Heo 2083db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size, 20842dca6999SDavid Miller unsigned long align, unsigned long flags, unsigned long start, 20855e6cafc8SMarek Szyprowski unsigned long end, int node, gfp_t gfp_mask, const void *caller) 2086db64fe02SNick Piggin { 20870006526dSKautuk Consul struct vmap_area *va; 2088db64fe02SNick Piggin struct vm_struct *area; 2089d98c9e83SAndrey Ryabinin unsigned long requested_size = size; 20901da177e4SLinus Torvalds 209152fd24caSGiridhar Pemmasani BUG_ON(in_interrupt()); 20921da177e4SLinus Torvalds size = PAGE_ALIGN(size); 209331be8309SOGAWA Hirofumi if (unlikely(!size)) 209431be8309SOGAWA Hirofumi return NULL; 20951da177e4SLinus Torvalds 2096252e5c6eSzijun_hu if (flags & VM_IOREMAP) 2097252e5c6eSzijun_hu align = 1ul << clamp_t(int, get_count_order_long(size), 2098252e5c6eSzijun_hu PAGE_SHIFT, IOREMAP_MAX_ORDER); 2099252e5c6eSzijun_hu 2100cf88c790STejun Heo area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); 21011da177e4SLinus Torvalds if (unlikely(!area)) 21021da177e4SLinus Torvalds return NULL; 21031da177e4SLinus Torvalds 210471394fe5SAndrey Ryabinin if (!(flags & VM_NO_GUARD)) 21051da177e4SLinus Torvalds size += PAGE_SIZE; 21061da177e4SLinus Torvalds 2107db64fe02SNick Piggin va = alloc_vmap_area(size, align, start, end, node, gfp_mask); 2108db64fe02SNick Piggin if (IS_ERR(va)) { 2109db64fe02SNick Piggin kfree(area); 2110db64fe02SNick Piggin return NULL; 21111da177e4SLinus Torvalds } 21121da177e4SLinus Torvalds 2113d98c9e83SAndrey Ryabinin kasan_unpoison_vmalloc((void *)va->va_start, requested_size); 2114f5252e00SMitsuo Hayasaka 2115d98c9e83SAndrey Ryabinin setup_vmalloc_vm(area, va, flags, caller); 21163c5c3cfbSDaniel Axtens 21171da177e4SLinus Torvalds return area; 21181da177e4SLinus Torvalds } 21191da177e4SLinus Torvalds 2120c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, 2121c2968612SBenjamin Herrenschmidt unsigned long start, unsigned long end, 21225e6cafc8SMarek Szyprowski const void *caller) 2123c2968612SBenjamin Herrenschmidt { 212400ef2d2fSDavid Rientjes return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE, 212500ef2d2fSDavid Rientjes GFP_KERNEL, caller); 2126c2968612SBenjamin Herrenschmidt } 2127c2968612SBenjamin Herrenschmidt 21281da177e4SLinus Torvalds /** 2129183ff22bSSimon Arlott * get_vm_area - reserve a contiguous kernel virtual area 21301da177e4SLinus Torvalds * @size: size of the area 21311da177e4SLinus Torvalds * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC 21321da177e4SLinus Torvalds * 21331da177e4SLinus Torvalds * Search an area of @size in the kernel virtual mapping area, 21341da177e4SLinus Torvalds * and reserved it for out purposes. Returns the area descriptor 21351da177e4SLinus Torvalds * on success or %NULL on failure. 2136a862f68aSMike Rapoport * 2137a862f68aSMike Rapoport * Return: the area descriptor on success or %NULL on failure. 21381da177e4SLinus Torvalds */ 21391da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) 21401da177e4SLinus Torvalds { 21412dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 214200ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, 214300ef2d2fSDavid Rientjes __builtin_return_address(0)); 214423016969SChristoph Lameter } 214523016969SChristoph Lameter 214623016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, 21475e6cafc8SMarek Szyprowski const void *caller) 214823016969SChristoph Lameter { 21492dca6999SDavid Miller return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, 215000ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, caller); 21511da177e4SLinus Torvalds } 21521da177e4SLinus Torvalds 2153e9da6e99SMarek Szyprowski /** 2154e9da6e99SMarek Szyprowski * find_vm_area - find a continuous kernel virtual area 2155e9da6e99SMarek Szyprowski * @addr: base address 2156e9da6e99SMarek Szyprowski * 2157e9da6e99SMarek Szyprowski * Search for the kernel VM area starting at @addr, and return it. 2158e9da6e99SMarek Szyprowski * It is up to the caller to do all required locking to keep the returned 2159e9da6e99SMarek Szyprowski * pointer valid. 2160a862f68aSMike Rapoport * 216174640617SHui Su * Return: the area descriptor on success or %NULL on failure. 2162e9da6e99SMarek Szyprowski */ 2163e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr) 216483342314SNick Piggin { 2165db64fe02SNick Piggin struct vmap_area *va; 216683342314SNick Piggin 2167db64fe02SNick Piggin va = find_vmap_area((unsigned long)addr); 2168688fcbfcSPengfei Li if (!va) 21697856dfebSAndi Kleen return NULL; 2170688fcbfcSPengfei Li 2171688fcbfcSPengfei Li return va->vm; 21727856dfebSAndi Kleen } 21737856dfebSAndi Kleen 21741da177e4SLinus Torvalds /** 2175183ff22bSSimon Arlott * remove_vm_area - find and remove a continuous kernel virtual area 21761da177e4SLinus Torvalds * @addr: base address 21771da177e4SLinus Torvalds * 21781da177e4SLinus Torvalds * Search for the kernel VM area starting at @addr, and remove it. 21791da177e4SLinus Torvalds * This function returns the found VM area, but using it is NOT safe 21807856dfebSAndi Kleen * on SMP machines, except for its size or flags. 2181a862f68aSMike Rapoport * 218274640617SHui Su * Return: the area descriptor on success or %NULL on failure. 21831da177e4SLinus Torvalds */ 2184b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr) 21851da177e4SLinus Torvalds { 2186db64fe02SNick Piggin struct vmap_area *va; 2187db64fe02SNick Piggin 21885803ed29SChristoph Hellwig might_sleep(); 21895803ed29SChristoph Hellwig 2190dd3b8353SUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 2191dd3b8353SUladzislau Rezki (Sony) va = __find_vmap_area((unsigned long)addr); 2192688fcbfcSPengfei Li if (va && va->vm) { 2193db1aecafSMinchan Kim struct vm_struct *vm = va->vm; 2194f5252e00SMitsuo Hayasaka 2195c69480adSJoonsoo Kim va->vm = NULL; 2196c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 2197c69480adSJoonsoo Kim 2198a5af5aa8SAndrey Ryabinin kasan_free_shadow(vm); 2199dd32c279SKAMEZAWA Hiroyuki free_unmap_vmap_area(va); 2200dd32c279SKAMEZAWA Hiroyuki 2201db64fe02SNick Piggin return vm; 2202db64fe02SNick Piggin } 2203dd3b8353SUladzislau Rezki (Sony) 2204dd3b8353SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 2205db64fe02SNick Piggin return NULL; 22061da177e4SLinus Torvalds } 22071da177e4SLinus Torvalds 2208868b104dSRick Edgecombe static inline void set_area_direct_map(const struct vm_struct *area, 2209868b104dSRick Edgecombe int (*set_direct_map)(struct page *page)) 2210868b104dSRick Edgecombe { 2211868b104dSRick Edgecombe int i; 2212868b104dSRick Edgecombe 2213868b104dSRick Edgecombe for (i = 0; i < area->nr_pages; i++) 2214868b104dSRick Edgecombe if (page_address(area->pages[i])) 2215868b104dSRick Edgecombe set_direct_map(area->pages[i]); 2216868b104dSRick Edgecombe } 2217868b104dSRick Edgecombe 2218868b104dSRick Edgecombe /* Handle removing and resetting vm mappings related to the vm_struct. */ 2219868b104dSRick Edgecombe static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages) 2220868b104dSRick Edgecombe { 2221868b104dSRick Edgecombe unsigned long start = ULONG_MAX, end = 0; 2222868b104dSRick Edgecombe int flush_reset = area->flags & VM_FLUSH_RESET_PERMS; 222331e67340SRick Edgecombe int flush_dmap = 0; 2224868b104dSRick Edgecombe int i; 2225868b104dSRick Edgecombe 2226868b104dSRick Edgecombe remove_vm_area(area->addr); 2227868b104dSRick Edgecombe 2228868b104dSRick Edgecombe /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */ 2229868b104dSRick Edgecombe if (!flush_reset) 2230868b104dSRick Edgecombe return; 2231868b104dSRick Edgecombe 2232868b104dSRick Edgecombe /* 2233868b104dSRick Edgecombe * If not deallocating pages, just do the flush of the VM area and 2234868b104dSRick Edgecombe * return. 2235868b104dSRick Edgecombe */ 2236868b104dSRick Edgecombe if (!deallocate_pages) { 2237868b104dSRick Edgecombe vm_unmap_aliases(); 2238868b104dSRick Edgecombe return; 2239868b104dSRick Edgecombe } 2240868b104dSRick Edgecombe 2241868b104dSRick Edgecombe /* 2242868b104dSRick Edgecombe * If execution gets here, flush the vm mapping and reset the direct 2243868b104dSRick Edgecombe * map. Find the start and end range of the direct mappings to make sure 2244868b104dSRick Edgecombe * the vm_unmap_aliases() flush includes the direct map. 2245868b104dSRick Edgecombe */ 2246868b104dSRick Edgecombe for (i = 0; i < area->nr_pages; i++) { 22478e41f872SRick Edgecombe unsigned long addr = (unsigned long)page_address(area->pages[i]); 22488e41f872SRick Edgecombe if (addr) { 2249868b104dSRick Edgecombe start = min(addr, start); 22508e41f872SRick Edgecombe end = max(addr + PAGE_SIZE, end); 225131e67340SRick Edgecombe flush_dmap = 1; 2252868b104dSRick Edgecombe } 2253868b104dSRick Edgecombe } 2254868b104dSRick Edgecombe 2255868b104dSRick Edgecombe /* 2256868b104dSRick Edgecombe * Set direct map to something invalid so that it won't be cached if 2257868b104dSRick Edgecombe * there are any accesses after the TLB flush, then flush the TLB and 2258868b104dSRick Edgecombe * reset the direct map permissions to the default. 2259868b104dSRick Edgecombe */ 2260868b104dSRick Edgecombe set_area_direct_map(area, set_direct_map_invalid_noflush); 226131e67340SRick Edgecombe _vm_unmap_aliases(start, end, flush_dmap); 2262868b104dSRick Edgecombe set_area_direct_map(area, set_direct_map_default_noflush); 2263868b104dSRick Edgecombe } 2264868b104dSRick Edgecombe 2265b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages) 22661da177e4SLinus Torvalds { 22671da177e4SLinus Torvalds struct vm_struct *area; 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds if (!addr) 22701da177e4SLinus Torvalds return; 22711da177e4SLinus Torvalds 2272e69e9d4aSHATAYAMA Daisuke if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n", 2273ab15d9b4SDan Carpenter addr)) 22741da177e4SLinus Torvalds return; 22751da177e4SLinus Torvalds 22766ade2032SLiviu Dudau area = find_vm_area(addr); 22771da177e4SLinus Torvalds if (unlikely(!area)) { 22784c8573e2SArjan van de Ven WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", 22791da177e4SLinus Torvalds addr); 22801da177e4SLinus Torvalds return; 22811da177e4SLinus Torvalds } 22821da177e4SLinus Torvalds 228305e3ff95SChintan Pandya debug_check_no_locks_freed(area->addr, get_vm_area_size(area)); 228405e3ff95SChintan Pandya debug_check_no_obj_freed(area->addr, get_vm_area_size(area)); 22859a11b49aSIngo Molnar 2286c041098cSVincenzo Frascino kasan_poison_vmalloc(area->addr, get_vm_area_size(area)); 22873c5c3cfbSDaniel Axtens 2288868b104dSRick Edgecombe vm_remove_mappings(area, deallocate_pages); 2289868b104dSRick Edgecombe 22901da177e4SLinus Torvalds if (deallocate_pages) { 22911da177e4SLinus Torvalds int i; 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds for (i = 0; i < area->nr_pages; i++) { 2294bf53d6f8SChristoph Lameter struct page *page = area->pages[i]; 2295bf53d6f8SChristoph Lameter 2296bf53d6f8SChristoph Lameter BUG_ON(!page); 22974949148aSVladimir Davydov __free_pages(page, 0); 22981da177e4SLinus Torvalds } 229997105f0aSRoman Gushchin atomic_long_sub(area->nr_pages, &nr_vmalloc_pages); 23001da177e4SLinus Torvalds 2301244d63eeSDavid Rientjes kvfree(area->pages); 23021da177e4SLinus Torvalds } 23031da177e4SLinus Torvalds 23041da177e4SLinus Torvalds kfree(area); 23051da177e4SLinus Torvalds } 23061da177e4SLinus Torvalds 2307bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr) 2308bf22e37aSAndrey Ryabinin { 2309bf22e37aSAndrey Ryabinin /* 2310bf22e37aSAndrey Ryabinin * Use raw_cpu_ptr() because this can be called from preemptible 2311bf22e37aSAndrey Ryabinin * context. Preemption is absolutely fine here, because the llist_add() 2312bf22e37aSAndrey Ryabinin * implementation is lockless, so it works even if we are adding to 231373221d88SJeongtae Park * another cpu's list. schedule_work() should be fine with this too. 2314bf22e37aSAndrey Ryabinin */ 2315bf22e37aSAndrey Ryabinin struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred); 2316bf22e37aSAndrey Ryabinin 2317bf22e37aSAndrey Ryabinin if (llist_add((struct llist_node *)addr, &p->list)) 2318bf22e37aSAndrey Ryabinin schedule_work(&p->wq); 2319bf22e37aSAndrey Ryabinin } 2320bf22e37aSAndrey Ryabinin 2321bf22e37aSAndrey Ryabinin /** 2322bf22e37aSAndrey Ryabinin * vfree_atomic - release memory allocated by vmalloc() 2323bf22e37aSAndrey Ryabinin * @addr: memory base address 2324bf22e37aSAndrey Ryabinin * 2325bf22e37aSAndrey Ryabinin * This one is just like vfree() but can be called in any atomic context 2326bf22e37aSAndrey Ryabinin * except NMIs. 2327bf22e37aSAndrey Ryabinin */ 2328bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr) 2329bf22e37aSAndrey Ryabinin { 2330bf22e37aSAndrey Ryabinin BUG_ON(in_nmi()); 2331bf22e37aSAndrey Ryabinin 2332bf22e37aSAndrey Ryabinin kmemleak_free(addr); 2333bf22e37aSAndrey Ryabinin 2334bf22e37aSAndrey Ryabinin if (!addr) 2335bf22e37aSAndrey Ryabinin return; 2336bf22e37aSAndrey Ryabinin __vfree_deferred(addr); 2337bf22e37aSAndrey Ryabinin } 2338bf22e37aSAndrey Ryabinin 2339c67dc624SRoman Penyaev static void __vfree(const void *addr) 2340c67dc624SRoman Penyaev { 2341c67dc624SRoman Penyaev if (unlikely(in_interrupt())) 2342c67dc624SRoman Penyaev __vfree_deferred(addr); 2343c67dc624SRoman Penyaev else 2344c67dc624SRoman Penyaev __vunmap(addr, 1); 2345c67dc624SRoman Penyaev } 2346c67dc624SRoman Penyaev 23471da177e4SLinus Torvalds /** 2348fa307474SMatthew Wilcox (Oracle) * vfree - Release memory allocated by vmalloc() 2349fa307474SMatthew Wilcox (Oracle) * @addr: Memory base address 23501da177e4SLinus Torvalds * 2351fa307474SMatthew Wilcox (Oracle) * Free the virtually continuous memory area starting at @addr, as obtained 2352fa307474SMatthew Wilcox (Oracle) * from one of the vmalloc() family of APIs. This will usually also free the 2353fa307474SMatthew Wilcox (Oracle) * physical memory underlying the virtual allocation, but that memory is 2354fa307474SMatthew Wilcox (Oracle) * reference counted, so it will not be freed until the last user goes away. 23551da177e4SLinus Torvalds * 2356fa307474SMatthew Wilcox (Oracle) * If @addr is NULL, no operation is performed. 235732fcfd40SAl Viro * 2358fa307474SMatthew Wilcox (Oracle) * Context: 23593ca4ea3aSAndrey Ryabinin * May sleep if called *not* from interrupt context. 2360fa307474SMatthew Wilcox (Oracle) * Must not be called in NMI context (strictly speaking, it could be 2361fa307474SMatthew Wilcox (Oracle) * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling 2362fa307474SMatthew Wilcox (Oracle) * conventions for vfree() arch-depenedent would be a really bad idea). 23631da177e4SLinus Torvalds */ 2364b3bdda02SChristoph Lameter void vfree(const void *addr) 23651da177e4SLinus Torvalds { 236632fcfd40SAl Viro BUG_ON(in_nmi()); 236789219d37SCatalin Marinas 236889219d37SCatalin Marinas kmemleak_free(addr); 236989219d37SCatalin Marinas 2370a8dda165SAndrey Ryabinin might_sleep_if(!in_interrupt()); 2371a8dda165SAndrey Ryabinin 237232fcfd40SAl Viro if (!addr) 237332fcfd40SAl Viro return; 2374c67dc624SRoman Penyaev 2375c67dc624SRoman Penyaev __vfree(addr); 23761da177e4SLinus Torvalds } 23771da177e4SLinus Torvalds EXPORT_SYMBOL(vfree); 23781da177e4SLinus Torvalds 23791da177e4SLinus Torvalds /** 23801da177e4SLinus Torvalds * vunmap - release virtual mapping obtained by vmap() 23811da177e4SLinus Torvalds * @addr: memory base address 23821da177e4SLinus Torvalds * 23831da177e4SLinus Torvalds * Free the virtually contiguous memory area starting at @addr, 23841da177e4SLinus Torvalds * which was created from the page array passed to vmap(). 23851da177e4SLinus Torvalds * 238680e93effSPekka Enberg * Must not be called in interrupt context. 23871da177e4SLinus Torvalds */ 2388b3bdda02SChristoph Lameter void vunmap(const void *addr) 23891da177e4SLinus Torvalds { 23901da177e4SLinus Torvalds BUG_ON(in_interrupt()); 239134754b69SPeter Zijlstra might_sleep(); 239232fcfd40SAl Viro if (addr) 23931da177e4SLinus Torvalds __vunmap(addr, 0); 23941da177e4SLinus Torvalds } 23951da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap); 23961da177e4SLinus Torvalds 23971da177e4SLinus Torvalds /** 23981da177e4SLinus Torvalds * vmap - map an array of pages into virtually contiguous space 23991da177e4SLinus Torvalds * @pages: array of page pointers 24001da177e4SLinus Torvalds * @count: number of pages to map 24011da177e4SLinus Torvalds * @flags: vm_area->flags 24021da177e4SLinus Torvalds * @prot: page protection for the mapping 24031da177e4SLinus Torvalds * 2404b944afc9SChristoph Hellwig * Maps @count pages from @pages into contiguous kernel virtual space. 2405b944afc9SChristoph Hellwig * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself 2406b944afc9SChristoph Hellwig * (which must be kmalloc or vmalloc memory) and one reference per pages in it 2407b944afc9SChristoph Hellwig * are transferred from the caller to vmap(), and will be freed / dropped when 2408b944afc9SChristoph Hellwig * vfree() is called on the return value. 2409a862f68aSMike Rapoport * 2410a862f68aSMike Rapoport * Return: the address of the area or %NULL on failure 24111da177e4SLinus Torvalds */ 24121da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, 24131da177e4SLinus Torvalds unsigned long flags, pgprot_t prot) 24141da177e4SLinus Torvalds { 24151da177e4SLinus Torvalds struct vm_struct *area; 241665ee03c4SGuillermo Julián Moreno unsigned long size; /* In bytes */ 24171da177e4SLinus Torvalds 241834754b69SPeter Zijlstra might_sleep(); 241934754b69SPeter Zijlstra 2420ca79b0c2SArun KS if (count > totalram_pages()) 24211da177e4SLinus Torvalds return NULL; 24221da177e4SLinus Torvalds 242365ee03c4SGuillermo Julián Moreno size = (unsigned long)count << PAGE_SHIFT; 242465ee03c4SGuillermo Julián Moreno area = get_vm_area_caller(size, flags, __builtin_return_address(0)); 24251da177e4SLinus Torvalds if (!area) 24261da177e4SLinus Torvalds return NULL; 242723016969SChristoph Lameter 2428cca98e9fSChristoph Hellwig if (map_kernel_range((unsigned long)area->addr, size, pgprot_nx(prot), 2429ed1f324cSChristoph Hellwig pages) < 0) { 24301da177e4SLinus Torvalds vunmap(area->addr); 24311da177e4SLinus Torvalds return NULL; 24321da177e4SLinus Torvalds } 24331da177e4SLinus Torvalds 2434c22ee528SMiaohe Lin if (flags & VM_MAP_PUT_PAGES) { 2435b944afc9SChristoph Hellwig area->pages = pages; 2436c22ee528SMiaohe Lin area->nr_pages = count; 2437c22ee528SMiaohe Lin } 24381da177e4SLinus Torvalds return area->addr; 24391da177e4SLinus Torvalds } 24401da177e4SLinus Torvalds EXPORT_SYMBOL(vmap); 24411da177e4SLinus Torvalds 24423e9a9e25SChristoph Hellwig #ifdef CONFIG_VMAP_PFN 24433e9a9e25SChristoph Hellwig struct vmap_pfn_data { 24443e9a9e25SChristoph Hellwig unsigned long *pfns; 24453e9a9e25SChristoph Hellwig pgprot_t prot; 24463e9a9e25SChristoph Hellwig unsigned int idx; 24473e9a9e25SChristoph Hellwig }; 24483e9a9e25SChristoph Hellwig 24493e9a9e25SChristoph Hellwig static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private) 24503e9a9e25SChristoph Hellwig { 24513e9a9e25SChristoph Hellwig struct vmap_pfn_data *data = private; 24523e9a9e25SChristoph Hellwig 24533e9a9e25SChristoph Hellwig if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx]))) 24543e9a9e25SChristoph Hellwig return -EINVAL; 24553e9a9e25SChristoph Hellwig *pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot)); 24563e9a9e25SChristoph Hellwig return 0; 24573e9a9e25SChristoph Hellwig } 24583e9a9e25SChristoph Hellwig 24593e9a9e25SChristoph Hellwig /** 24603e9a9e25SChristoph Hellwig * vmap_pfn - map an array of PFNs into virtually contiguous space 24613e9a9e25SChristoph Hellwig * @pfns: array of PFNs 24623e9a9e25SChristoph Hellwig * @count: number of pages to map 24633e9a9e25SChristoph Hellwig * @prot: page protection for the mapping 24643e9a9e25SChristoph Hellwig * 24653e9a9e25SChristoph Hellwig * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns 24663e9a9e25SChristoph Hellwig * the start address of the mapping. 24673e9a9e25SChristoph Hellwig */ 24683e9a9e25SChristoph Hellwig void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot) 24693e9a9e25SChristoph Hellwig { 24703e9a9e25SChristoph Hellwig struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) }; 24713e9a9e25SChristoph Hellwig struct vm_struct *area; 24723e9a9e25SChristoph Hellwig 24733e9a9e25SChristoph Hellwig area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP, 24743e9a9e25SChristoph Hellwig __builtin_return_address(0)); 24753e9a9e25SChristoph Hellwig if (!area) 24763e9a9e25SChristoph Hellwig return NULL; 24773e9a9e25SChristoph Hellwig if (apply_to_page_range(&init_mm, (unsigned long)area->addr, 24783e9a9e25SChristoph Hellwig count * PAGE_SIZE, vmap_pfn_apply, &data)) { 24793e9a9e25SChristoph Hellwig free_vm_area(area); 24803e9a9e25SChristoph Hellwig return NULL; 24813e9a9e25SChristoph Hellwig } 24823e9a9e25SChristoph Hellwig return area->addr; 24833e9a9e25SChristoph Hellwig } 24843e9a9e25SChristoph Hellwig EXPORT_SYMBOL_GPL(vmap_pfn); 24853e9a9e25SChristoph Hellwig #endif /* CONFIG_VMAP_PFN */ 24863e9a9e25SChristoph Hellwig 2487e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, 24883722e13cSWanpeng Li pgprot_t prot, int node) 24891da177e4SLinus Torvalds { 2490930f036bSDavid Rientjes const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; 2491f255935bSChristoph Hellwig unsigned int nr_pages = get_vm_area_size(area) >> PAGE_SHIFT; 249234fe6537SAndrew Morton unsigned long array_size; 249334fe6537SAndrew Morton unsigned int i; 2494f255935bSChristoph Hellwig struct page **pages; 24951da177e4SLinus Torvalds 249634fe6537SAndrew Morton array_size = (unsigned long)nr_pages * sizeof(struct page *); 2497f255935bSChristoph Hellwig gfp_mask |= __GFP_NOWARN; 2498f255935bSChristoph Hellwig if (!(gfp_mask & (GFP_DMA | GFP_DMA32))) 2499f255935bSChristoph Hellwig gfp_mask |= __GFP_HIGHMEM; 25001da177e4SLinus Torvalds 25011da177e4SLinus Torvalds /* Please note that the recursion is strictly bounded. */ 25028757d5faSJan Kiszka if (array_size > PAGE_SIZE) { 2503f255935bSChristoph Hellwig pages = __vmalloc_node(array_size, 1, nested_gfp, node, 2504f255935bSChristoph Hellwig area->caller); 2505286e1ea3SAndrew Morton } else { 2506976d6dfbSJan Beulich pages = kmalloc_node(array_size, nested_gfp, node); 2507286e1ea3SAndrew Morton } 25087ea36242SAustin Kim 25097ea36242SAustin Kim if (!pages) { 25108945a723SUladzislau Rezki (Sony) free_vm_area(area); 25111da177e4SLinus Torvalds return NULL; 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 25147ea36242SAustin Kim area->pages = pages; 25157ea36242SAustin Kim area->nr_pages = nr_pages; 25167ea36242SAustin Kim 25171da177e4SLinus Torvalds for (i = 0; i < area->nr_pages; i++) { 2518bf53d6f8SChristoph Lameter struct page *page; 2519bf53d6f8SChristoph Lameter 25204b90951cSJianguo Wu if (node == NUMA_NO_NODE) 2521f255935bSChristoph Hellwig page = alloc_page(gfp_mask); 2522930fc45aSChristoph Lameter else 2523f255935bSChristoph Hellwig page = alloc_pages_node(node, gfp_mask, 0); 2524bf53d6f8SChristoph Lameter 2525bf53d6f8SChristoph Lameter if (unlikely(!page)) { 252682afbc32SHui Su /* Successfully allocated i pages, free them in __vfree() */ 25271da177e4SLinus Torvalds area->nr_pages = i; 252897105f0aSRoman Gushchin atomic_long_add(area->nr_pages, &nr_vmalloc_pages); 25291da177e4SLinus Torvalds goto fail; 25301da177e4SLinus Torvalds } 2531bf53d6f8SChristoph Lameter area->pages[i] = page; 2532dcf61ff0SLiu Xiang if (gfpflags_allow_blocking(gfp_mask)) 2533660654f9SEric Dumazet cond_resched(); 25341da177e4SLinus Torvalds } 253597105f0aSRoman Gushchin atomic_long_add(area->nr_pages, &nr_vmalloc_pages); 25361da177e4SLinus Torvalds 2537ed1f324cSChristoph Hellwig if (map_kernel_range((unsigned long)area->addr, get_vm_area_size(area), 2538ed1f324cSChristoph Hellwig prot, pages) < 0) 25391da177e4SLinus Torvalds goto fail; 2540ed1f324cSChristoph Hellwig 25411da177e4SLinus Torvalds return area->addr; 25421da177e4SLinus Torvalds 25431da177e4SLinus Torvalds fail: 2544a8e99259SMichal Hocko warn_alloc(gfp_mask, NULL, 25457877cdccSMichal Hocko "vmalloc: allocation failure, allocated %ld of %ld bytes", 254622943ab1SDave Hansen (area->nr_pages*PAGE_SIZE), area->size); 2547c67dc624SRoman Penyaev __vfree(area->addr); 25481da177e4SLinus Torvalds return NULL; 25491da177e4SLinus Torvalds } 25501da177e4SLinus Torvalds 2551d0a21265SDavid Rientjes /** 2552d0a21265SDavid Rientjes * __vmalloc_node_range - allocate virtually contiguous memory 2553d0a21265SDavid Rientjes * @size: allocation size 2554d0a21265SDavid Rientjes * @align: desired alignment 2555d0a21265SDavid Rientjes * @start: vm area range start 2556d0a21265SDavid Rientjes * @end: vm area range end 2557d0a21265SDavid Rientjes * @gfp_mask: flags for the page level allocator 2558d0a21265SDavid Rientjes * @prot: protection mask for the allocated pages 2559cb9e3c29SAndrey Ryabinin * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD) 256000ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 2561d0a21265SDavid Rientjes * @caller: caller's return address 2562d0a21265SDavid Rientjes * 2563d0a21265SDavid Rientjes * Allocate enough pages to cover @size from the page level 2564d0a21265SDavid Rientjes * allocator with @gfp_mask flags. Map them into contiguous 2565d0a21265SDavid Rientjes * kernel virtual space, using a pagetable protection of @prot. 2566a862f68aSMike Rapoport * 2567a862f68aSMike Rapoport * Return: the address of the area or %NULL on failure 2568d0a21265SDavid Rientjes */ 2569d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align, 2570d0a21265SDavid Rientjes unsigned long start, unsigned long end, gfp_t gfp_mask, 2571cb9e3c29SAndrey Ryabinin pgprot_t prot, unsigned long vm_flags, int node, 2572cb9e3c29SAndrey Ryabinin const void *caller) 2573930fc45aSChristoph Lameter { 2574d0a21265SDavid Rientjes struct vm_struct *area; 2575d0a21265SDavid Rientjes void *addr; 2576d0a21265SDavid Rientjes unsigned long real_size = size; 2577d0a21265SDavid Rientjes 2578d0a21265SDavid Rientjes size = PAGE_ALIGN(size); 2579ca79b0c2SArun KS if (!size || (size >> PAGE_SHIFT) > totalram_pages()) 2580de7d2b56SJoe Perches goto fail; 2581d0a21265SDavid Rientjes 2582d98c9e83SAndrey Ryabinin area = __get_vm_area_node(real_size, align, VM_ALLOC | VM_UNINITIALIZED | 2583cb9e3c29SAndrey Ryabinin vm_flags, start, end, node, gfp_mask, caller); 2584d0a21265SDavid Rientjes if (!area) 2585de7d2b56SJoe Perches goto fail; 2586d0a21265SDavid Rientjes 25873722e13cSWanpeng Li addr = __vmalloc_area_node(area, gfp_mask, prot, node); 25881368edf0SMel Gorman if (!addr) 2589b82225f3SWanpeng Li return NULL; 259089219d37SCatalin Marinas 259189219d37SCatalin Marinas /* 259220fc02b4SZhang Yanfei * In this function, newly allocated vm_struct has VM_UNINITIALIZED 259320fc02b4SZhang Yanfei * flag. It means that vm_struct is not fully initialized. 25944341fa45SJoonsoo Kim * Now, it is fully initialized, so remove this flag here. 2595f5252e00SMitsuo Hayasaka */ 259620fc02b4SZhang Yanfei clear_vm_uninitialized_flag(area); 2597f5252e00SMitsuo Hayasaka 259894f4a161SCatalin Marinas kmemleak_vmalloc(area, size, gfp_mask); 259989219d37SCatalin Marinas 260089219d37SCatalin Marinas return addr; 2601de7d2b56SJoe Perches 2602de7d2b56SJoe Perches fail: 2603a8e99259SMichal Hocko warn_alloc(gfp_mask, NULL, 26047877cdccSMichal Hocko "vmalloc: allocation failure: %lu bytes", real_size); 2605de7d2b56SJoe Perches return NULL; 2606930fc45aSChristoph Lameter } 2607930fc45aSChristoph Lameter 26081da177e4SLinus Torvalds /** 2609930fc45aSChristoph Lameter * __vmalloc_node - allocate virtually contiguous memory 26101da177e4SLinus Torvalds * @size: allocation size 26112dca6999SDavid Miller * @align: desired alignment 26121da177e4SLinus Torvalds * @gfp_mask: flags for the page level allocator 261300ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 2614c85d194bSRandy Dunlap * @caller: caller's return address 26151da177e4SLinus Torvalds * 2616f38fcb9cSChristoph Hellwig * Allocate enough pages to cover @size from the page level allocator with 2617f38fcb9cSChristoph Hellwig * @gfp_mask flags. Map them into contiguous kernel virtual space. 2618a7c3e901SMichal Hocko * 2619dcda9b04SMichal Hocko * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL 2620a7c3e901SMichal Hocko * and __GFP_NOFAIL are not supported 2621a7c3e901SMichal Hocko * 2622a7c3e901SMichal Hocko * Any use of gfp flags outside of GFP_KERNEL should be consulted 2623a7c3e901SMichal Hocko * with mm people. 2624a862f68aSMike Rapoport * 2625a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 26261da177e4SLinus Torvalds */ 26272b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align, 2628f38fcb9cSChristoph Hellwig gfp_t gfp_mask, int node, const void *caller) 26291da177e4SLinus Torvalds { 2630d0a21265SDavid Rientjes return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, 2631f38fcb9cSChristoph Hellwig gfp_mask, PAGE_KERNEL, 0, node, caller); 26321da177e4SLinus Torvalds } 2633c3f896dcSChristoph Hellwig /* 2634c3f896dcSChristoph Hellwig * This is only for performance analysis of vmalloc and stress purpose. 2635c3f896dcSChristoph Hellwig * It is required by vmalloc test module, therefore do not use it other 2636c3f896dcSChristoph Hellwig * than that. 2637c3f896dcSChristoph Hellwig */ 2638c3f896dcSChristoph Hellwig #ifdef CONFIG_TEST_VMALLOC_MODULE 2639c3f896dcSChristoph Hellwig EXPORT_SYMBOL_GPL(__vmalloc_node); 2640c3f896dcSChristoph Hellwig #endif 26411da177e4SLinus Torvalds 264288dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask) 2643930fc45aSChristoph Lameter { 2644f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE, 264523016969SChristoph Lameter __builtin_return_address(0)); 2646930fc45aSChristoph Lameter } 26471da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc); 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds /** 26501da177e4SLinus Torvalds * vmalloc - allocate virtually contiguous memory 26511da177e4SLinus Torvalds * @size: allocation size 265292eac168SMike Rapoport * 26531da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 26541da177e4SLinus Torvalds * allocator and map them into contiguous kernel virtual space. 26551da177e4SLinus Torvalds * 2656c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 26571da177e4SLinus Torvalds * use __vmalloc() instead. 2658a862f68aSMike Rapoport * 2659a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 26601da177e4SLinus Torvalds */ 26611da177e4SLinus Torvalds void *vmalloc(unsigned long size) 26621da177e4SLinus Torvalds { 26634d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE, 26644d39d728SChristoph Hellwig __builtin_return_address(0)); 26651da177e4SLinus Torvalds } 26661da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc); 26671da177e4SLinus Torvalds 2668930fc45aSChristoph Lameter /** 2669e1ca7788SDave Young * vzalloc - allocate virtually contiguous memory with zero fill 2670e1ca7788SDave Young * @size: allocation size 267192eac168SMike Rapoport * 2672e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 2673e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 2674e1ca7788SDave Young * The memory allocated is set to zero. 2675e1ca7788SDave Young * 2676e1ca7788SDave Young * For tight control over page level allocator and protection flags 2677e1ca7788SDave Young * use __vmalloc() instead. 2678a862f68aSMike Rapoport * 2679a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 2680e1ca7788SDave Young */ 2681e1ca7788SDave Young void *vzalloc(unsigned long size) 2682e1ca7788SDave Young { 26834d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE, 26844d39d728SChristoph Hellwig __builtin_return_address(0)); 2685e1ca7788SDave Young } 2686e1ca7788SDave Young EXPORT_SYMBOL(vzalloc); 2687e1ca7788SDave Young 2688e1ca7788SDave Young /** 2689ead04089SRolf Eike Beer * vmalloc_user - allocate zeroed virtually contiguous memory for userspace 269083342314SNick Piggin * @size: allocation size 2691ead04089SRolf Eike Beer * 2692ead04089SRolf Eike Beer * The resulting memory area is zeroed so it can be mapped to userspace 2693ead04089SRolf Eike Beer * without leaking data. 2694a862f68aSMike Rapoport * 2695a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 269683342314SNick Piggin */ 269783342314SNick Piggin void *vmalloc_user(unsigned long size) 269883342314SNick Piggin { 2699bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 2700bc84c535SRoman Penyaev GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL, 2701bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 270200ef2d2fSDavid Rientjes __builtin_return_address(0)); 270383342314SNick Piggin } 270483342314SNick Piggin EXPORT_SYMBOL(vmalloc_user); 270583342314SNick Piggin 270683342314SNick Piggin /** 2707930fc45aSChristoph Lameter * vmalloc_node - allocate memory on a specific node 2708930fc45aSChristoph Lameter * @size: allocation size 2709d44e0780SRandy Dunlap * @node: numa node 2710930fc45aSChristoph Lameter * 2711930fc45aSChristoph Lameter * Allocate enough pages to cover @size from the page level 2712930fc45aSChristoph Lameter * allocator and map them into contiguous kernel virtual space. 2713930fc45aSChristoph Lameter * 2714c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 2715930fc45aSChristoph Lameter * use __vmalloc() instead. 2716a862f68aSMike Rapoport * 2717a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 2718930fc45aSChristoph Lameter */ 2719930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node) 2720930fc45aSChristoph Lameter { 2721f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL, node, 2722f38fcb9cSChristoph Hellwig __builtin_return_address(0)); 2723930fc45aSChristoph Lameter } 2724930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node); 2725930fc45aSChristoph Lameter 2726e1ca7788SDave Young /** 2727e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill 2728e1ca7788SDave Young * @size: allocation size 2729e1ca7788SDave Young * @node: numa node 2730e1ca7788SDave Young * 2731e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 2732e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 2733e1ca7788SDave Young * The memory allocated is set to zero. 2734e1ca7788SDave Young * 2735a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 2736e1ca7788SDave Young */ 2737e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node) 2738e1ca7788SDave Young { 27394d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node, 27404d39d728SChristoph Hellwig __builtin_return_address(0)); 2741e1ca7788SDave Young } 2742e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node); 2743e1ca7788SDave Young 27440d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) 2745698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) 27460d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) 2747698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL) 27480d08e0d3SAndi Kleen #else 2749698d0831SMichal Hocko /* 2750698d0831SMichal Hocko * 64b systems should always have either DMA or DMA32 zones. For others 2751698d0831SMichal Hocko * GFP_DMA32 should do the right thing and use the normal zone. 2752698d0831SMichal Hocko */ 2753698d0831SMichal Hocko #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL 27540d08e0d3SAndi Kleen #endif 27550d08e0d3SAndi Kleen 27561da177e4SLinus Torvalds /** 27571da177e4SLinus Torvalds * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 27581da177e4SLinus Torvalds * @size: allocation size 27591da177e4SLinus Torvalds * 27601da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 27611da177e4SLinus Torvalds * page level allocator and map them into contiguous kernel virtual space. 2762a862f68aSMike Rapoport * 2763a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 27641da177e4SLinus Torvalds */ 27651da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 27661da177e4SLinus Torvalds { 2767f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE, 2768f38fcb9cSChristoph Hellwig __builtin_return_address(0)); 27691da177e4SLinus Torvalds } 27701da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32); 27711da177e4SLinus Torvalds 277283342314SNick Piggin /** 2773ead04089SRolf Eike Beer * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 277483342314SNick Piggin * @size: allocation size 2775ead04089SRolf Eike Beer * 2776ead04089SRolf Eike Beer * The resulting memory area is 32bit addressable and zeroed so it can be 2777ead04089SRolf Eike Beer * mapped to userspace without leaking data. 2778a862f68aSMike Rapoport * 2779a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 278083342314SNick Piggin */ 278183342314SNick Piggin void *vmalloc_32_user(unsigned long size) 278283342314SNick Piggin { 2783bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 2784bc84c535SRoman Penyaev GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, 2785bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 27865a82ac71SRoman Penyaev __builtin_return_address(0)); 278783342314SNick Piggin } 278883342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user); 278983342314SNick Piggin 2790d0107eb0SKAMEZAWA Hiroyuki /* 2791d0107eb0SKAMEZAWA Hiroyuki * small helper routine , copy contents to buf from addr. 2792d0107eb0SKAMEZAWA Hiroyuki * If the page is not present, fill zero. 2793d0107eb0SKAMEZAWA Hiroyuki */ 2794d0107eb0SKAMEZAWA Hiroyuki 2795d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count) 2796d0107eb0SKAMEZAWA Hiroyuki { 2797d0107eb0SKAMEZAWA Hiroyuki struct page *p; 2798d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 2799d0107eb0SKAMEZAWA Hiroyuki 2800d0107eb0SKAMEZAWA Hiroyuki while (count) { 2801d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 2802d0107eb0SKAMEZAWA Hiroyuki 2803891c49abSAlexander Kuleshov offset = offset_in_page(addr); 2804d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 2805d0107eb0SKAMEZAWA Hiroyuki if (length > count) 2806d0107eb0SKAMEZAWA Hiroyuki length = count; 2807d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 2808d0107eb0SKAMEZAWA Hiroyuki /* 2809d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 2810d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 2811d0107eb0SKAMEZAWA Hiroyuki * overhead of vmalloc()/vfree() calles for this _debug_ 2812d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 2813d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 2814d0107eb0SKAMEZAWA Hiroyuki */ 2815d0107eb0SKAMEZAWA Hiroyuki if (p) { 2816d0107eb0SKAMEZAWA Hiroyuki /* 2817d0107eb0SKAMEZAWA Hiroyuki * we can expect USER0 is not used (see vread/vwrite's 2818d0107eb0SKAMEZAWA Hiroyuki * function description) 2819d0107eb0SKAMEZAWA Hiroyuki */ 28209b04c5feSCong Wang void *map = kmap_atomic(p); 2821d0107eb0SKAMEZAWA Hiroyuki memcpy(buf, map + offset, length); 28229b04c5feSCong Wang kunmap_atomic(map); 2823d0107eb0SKAMEZAWA Hiroyuki } else 2824d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, length); 2825d0107eb0SKAMEZAWA Hiroyuki 2826d0107eb0SKAMEZAWA Hiroyuki addr += length; 2827d0107eb0SKAMEZAWA Hiroyuki buf += length; 2828d0107eb0SKAMEZAWA Hiroyuki copied += length; 2829d0107eb0SKAMEZAWA Hiroyuki count -= length; 2830d0107eb0SKAMEZAWA Hiroyuki } 2831d0107eb0SKAMEZAWA Hiroyuki return copied; 2832d0107eb0SKAMEZAWA Hiroyuki } 2833d0107eb0SKAMEZAWA Hiroyuki 2834d0107eb0SKAMEZAWA Hiroyuki static int aligned_vwrite(char *buf, char *addr, unsigned long count) 2835d0107eb0SKAMEZAWA Hiroyuki { 2836d0107eb0SKAMEZAWA Hiroyuki struct page *p; 2837d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 2838d0107eb0SKAMEZAWA Hiroyuki 2839d0107eb0SKAMEZAWA Hiroyuki while (count) { 2840d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 2841d0107eb0SKAMEZAWA Hiroyuki 2842891c49abSAlexander Kuleshov offset = offset_in_page(addr); 2843d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 2844d0107eb0SKAMEZAWA Hiroyuki if (length > count) 2845d0107eb0SKAMEZAWA Hiroyuki length = count; 2846d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 2847d0107eb0SKAMEZAWA Hiroyuki /* 2848d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 2849d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 2850d0107eb0SKAMEZAWA Hiroyuki * overhead of vmalloc()/vfree() calles for this _debug_ 2851d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 2852d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 2853d0107eb0SKAMEZAWA Hiroyuki */ 2854d0107eb0SKAMEZAWA Hiroyuki if (p) { 2855d0107eb0SKAMEZAWA Hiroyuki /* 2856d0107eb0SKAMEZAWA Hiroyuki * we can expect USER0 is not used (see vread/vwrite's 2857d0107eb0SKAMEZAWA Hiroyuki * function description) 2858d0107eb0SKAMEZAWA Hiroyuki */ 28599b04c5feSCong Wang void *map = kmap_atomic(p); 2860d0107eb0SKAMEZAWA Hiroyuki memcpy(map + offset, buf, length); 28619b04c5feSCong Wang kunmap_atomic(map); 2862d0107eb0SKAMEZAWA Hiroyuki } 2863d0107eb0SKAMEZAWA Hiroyuki addr += length; 2864d0107eb0SKAMEZAWA Hiroyuki buf += length; 2865d0107eb0SKAMEZAWA Hiroyuki copied += length; 2866d0107eb0SKAMEZAWA Hiroyuki count -= length; 2867d0107eb0SKAMEZAWA Hiroyuki } 2868d0107eb0SKAMEZAWA Hiroyuki return copied; 2869d0107eb0SKAMEZAWA Hiroyuki } 2870d0107eb0SKAMEZAWA Hiroyuki 2871d0107eb0SKAMEZAWA Hiroyuki /** 2872d0107eb0SKAMEZAWA Hiroyuki * vread() - read vmalloc area in a safe way. 2873d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for reading data 2874d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 2875d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 2876d0107eb0SKAMEZAWA Hiroyuki * 2877d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 2878d0107eb0SKAMEZAWA Hiroyuki * copy data from that area to a given buffer. If the given memory range 2879d0107eb0SKAMEZAWA Hiroyuki * of [addr...addr+count) includes some valid address, data is copied to 2880d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, they'll be zero-filled. 2881d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 2882d0107eb0SKAMEZAWA Hiroyuki * 2883d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 2884a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 2885d0107eb0SKAMEZAWA Hiroyuki * 2886d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vread() is never necessary because the caller 2887d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 2888d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 2889d9009d67SGeert Uytterhoeven * any information, as /dev/kmem. 2890a862f68aSMike Rapoport * 2891a862f68aSMike Rapoport * Return: number of bytes for which addr and buf should be increased 2892a862f68aSMike Rapoport * (same number as @count) or %0 if [addr...addr+count) doesn't 2893a862f68aSMike Rapoport * include any intersection with valid vmalloc area 2894d0107eb0SKAMEZAWA Hiroyuki */ 28951da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 28961da177e4SLinus Torvalds { 2897e81ce85fSJoonsoo Kim struct vmap_area *va; 2898e81ce85fSJoonsoo Kim struct vm_struct *vm; 28991da177e4SLinus Torvalds char *vaddr, *buf_start = buf; 2900d0107eb0SKAMEZAWA Hiroyuki unsigned long buflen = count; 29011da177e4SLinus Torvalds unsigned long n; 29021da177e4SLinus Torvalds 29031da177e4SLinus Torvalds /* Don't allow overflow */ 29041da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 29051da177e4SLinus Torvalds count = -(unsigned long) addr; 29061da177e4SLinus Torvalds 2907e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 2908f608788cSSerapheim Dimitropoulos va = __find_vmap_area((unsigned long)addr); 2909f608788cSSerapheim Dimitropoulos if (!va) 2910f608788cSSerapheim Dimitropoulos goto finished; 2911f608788cSSerapheim Dimitropoulos list_for_each_entry_from(va, &vmap_area_list, list) { 2912e81ce85fSJoonsoo Kim if (!count) 2913e81ce85fSJoonsoo Kim break; 2914e81ce85fSJoonsoo Kim 2915688fcbfcSPengfei Li if (!va->vm) 2916e81ce85fSJoonsoo Kim continue; 2917e81ce85fSJoonsoo Kim 2918e81ce85fSJoonsoo Kim vm = va->vm; 2919e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 2920762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 29211da177e4SLinus Torvalds continue; 29221da177e4SLinus Torvalds while (addr < vaddr) { 29231da177e4SLinus Torvalds if (count == 0) 29241da177e4SLinus Torvalds goto finished; 29251da177e4SLinus Torvalds *buf = '\0'; 29261da177e4SLinus Torvalds buf++; 29271da177e4SLinus Torvalds addr++; 29281da177e4SLinus Torvalds count--; 29291da177e4SLinus Torvalds } 2930762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 2931d0107eb0SKAMEZAWA Hiroyuki if (n > count) 2932d0107eb0SKAMEZAWA Hiroyuki n = count; 2933e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) 2934d0107eb0SKAMEZAWA Hiroyuki aligned_vread(buf, addr, n); 2935d0107eb0SKAMEZAWA Hiroyuki else /* IOREMAP area is treated as memory hole */ 2936d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, n); 2937d0107eb0SKAMEZAWA Hiroyuki buf += n; 2938d0107eb0SKAMEZAWA Hiroyuki addr += n; 2939d0107eb0SKAMEZAWA Hiroyuki count -= n; 29401da177e4SLinus Torvalds } 29411da177e4SLinus Torvalds finished: 2942e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 2943d0107eb0SKAMEZAWA Hiroyuki 2944d0107eb0SKAMEZAWA Hiroyuki if (buf == buf_start) 2945d0107eb0SKAMEZAWA Hiroyuki return 0; 2946d0107eb0SKAMEZAWA Hiroyuki /* zero-fill memory holes */ 2947d0107eb0SKAMEZAWA Hiroyuki if (buf != buf_start + buflen) 2948d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, buflen - (buf - buf_start)); 2949d0107eb0SKAMEZAWA Hiroyuki 2950d0107eb0SKAMEZAWA Hiroyuki return buflen; 29511da177e4SLinus Torvalds } 29521da177e4SLinus Torvalds 2953d0107eb0SKAMEZAWA Hiroyuki /** 2954d0107eb0SKAMEZAWA Hiroyuki * vwrite() - write vmalloc area in a safe way. 2955d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for source data 2956d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 2957d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 2958d0107eb0SKAMEZAWA Hiroyuki * 2959d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 2960d0107eb0SKAMEZAWA Hiroyuki * copy data from a buffer to the given addr. If specified range of 2961d0107eb0SKAMEZAWA Hiroyuki * [addr...addr+count) includes some valid address, data is copied from 2962d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, no copy to hole. 2963d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 2964d0107eb0SKAMEZAWA Hiroyuki * 2965d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 2966a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 2967d0107eb0SKAMEZAWA Hiroyuki * 2968d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vwrite() is never necessary because the caller 2969d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 2970d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 2971d9009d67SGeert Uytterhoeven * any information, as /dev/kmem. 2972a862f68aSMike Rapoport * 2973a862f68aSMike Rapoport * Return: number of bytes for which addr and buf should be 2974a862f68aSMike Rapoport * increased (same number as @count) or %0 if [addr...addr+count) 2975a862f68aSMike Rapoport * doesn't include any intersection with valid vmalloc area 2976d0107eb0SKAMEZAWA Hiroyuki */ 29771da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count) 29781da177e4SLinus Torvalds { 2979e81ce85fSJoonsoo Kim struct vmap_area *va; 2980e81ce85fSJoonsoo Kim struct vm_struct *vm; 2981d0107eb0SKAMEZAWA Hiroyuki char *vaddr; 2982d0107eb0SKAMEZAWA Hiroyuki unsigned long n, buflen; 2983d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 29841da177e4SLinus Torvalds 29851da177e4SLinus Torvalds /* Don't allow overflow */ 29861da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 29871da177e4SLinus Torvalds count = -(unsigned long) addr; 2988d0107eb0SKAMEZAWA Hiroyuki buflen = count; 29891da177e4SLinus Torvalds 2990e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 2991e81ce85fSJoonsoo Kim list_for_each_entry(va, &vmap_area_list, list) { 2992e81ce85fSJoonsoo Kim if (!count) 2993e81ce85fSJoonsoo Kim break; 2994e81ce85fSJoonsoo Kim 2995688fcbfcSPengfei Li if (!va->vm) 2996e81ce85fSJoonsoo Kim continue; 2997e81ce85fSJoonsoo Kim 2998e81ce85fSJoonsoo Kim vm = va->vm; 2999e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 3000762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 30011da177e4SLinus Torvalds continue; 30021da177e4SLinus Torvalds while (addr < vaddr) { 30031da177e4SLinus Torvalds if (count == 0) 30041da177e4SLinus Torvalds goto finished; 30051da177e4SLinus Torvalds buf++; 30061da177e4SLinus Torvalds addr++; 30071da177e4SLinus Torvalds count--; 30081da177e4SLinus Torvalds } 3009762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 3010d0107eb0SKAMEZAWA Hiroyuki if (n > count) 3011d0107eb0SKAMEZAWA Hiroyuki n = count; 3012e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) { 3013d0107eb0SKAMEZAWA Hiroyuki aligned_vwrite(buf, addr, n); 3014d0107eb0SKAMEZAWA Hiroyuki copied++; 3015d0107eb0SKAMEZAWA Hiroyuki } 3016d0107eb0SKAMEZAWA Hiroyuki buf += n; 3017d0107eb0SKAMEZAWA Hiroyuki addr += n; 3018d0107eb0SKAMEZAWA Hiroyuki count -= n; 30191da177e4SLinus Torvalds } 30201da177e4SLinus Torvalds finished: 3021e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 3022d0107eb0SKAMEZAWA Hiroyuki if (!copied) 3023d0107eb0SKAMEZAWA Hiroyuki return 0; 3024d0107eb0SKAMEZAWA Hiroyuki return buflen; 30251da177e4SLinus Torvalds } 302683342314SNick Piggin 302783342314SNick Piggin /** 3028e69e9d4aSHATAYAMA Daisuke * remap_vmalloc_range_partial - map vmalloc pages to userspace 3029e69e9d4aSHATAYAMA Daisuke * @vma: vma to cover 3030e69e9d4aSHATAYAMA Daisuke * @uaddr: target user address to start at 3031e69e9d4aSHATAYAMA Daisuke * @kaddr: virtual address of vmalloc kernel memory 3032bdebd6a2SJann Horn * @pgoff: offset from @kaddr to start at 3033e69e9d4aSHATAYAMA Daisuke * @size: size of map area 3034e69e9d4aSHATAYAMA Daisuke * 3035e69e9d4aSHATAYAMA Daisuke * Returns: 0 for success, -Exxx on failure 3036e69e9d4aSHATAYAMA Daisuke * 3037e69e9d4aSHATAYAMA Daisuke * This function checks that @kaddr is a valid vmalloc'ed area, 3038e69e9d4aSHATAYAMA Daisuke * and that it is big enough to cover the range starting at 3039e69e9d4aSHATAYAMA Daisuke * @uaddr in @vma. Will return failure if that criteria isn't 3040e69e9d4aSHATAYAMA Daisuke * met. 3041e69e9d4aSHATAYAMA Daisuke * 3042e69e9d4aSHATAYAMA Daisuke * Similar to remap_pfn_range() (see mm/memory.c) 3043e69e9d4aSHATAYAMA Daisuke */ 3044e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr, 3045bdebd6a2SJann Horn void *kaddr, unsigned long pgoff, 3046bdebd6a2SJann Horn unsigned long size) 3047e69e9d4aSHATAYAMA Daisuke { 3048e69e9d4aSHATAYAMA Daisuke struct vm_struct *area; 3049bdebd6a2SJann Horn unsigned long off; 3050bdebd6a2SJann Horn unsigned long end_index; 3051bdebd6a2SJann Horn 3052bdebd6a2SJann Horn if (check_shl_overflow(pgoff, PAGE_SHIFT, &off)) 3053bdebd6a2SJann Horn return -EINVAL; 3054e69e9d4aSHATAYAMA Daisuke 3055e69e9d4aSHATAYAMA Daisuke size = PAGE_ALIGN(size); 3056e69e9d4aSHATAYAMA Daisuke 3057e69e9d4aSHATAYAMA Daisuke if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr)) 3058e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3059e69e9d4aSHATAYAMA Daisuke 3060e69e9d4aSHATAYAMA Daisuke area = find_vm_area(kaddr); 3061e69e9d4aSHATAYAMA Daisuke if (!area) 3062e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3063e69e9d4aSHATAYAMA Daisuke 3064fe9041c2SChristoph Hellwig if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT))) 3065e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3066e69e9d4aSHATAYAMA Daisuke 3067bdebd6a2SJann Horn if (check_add_overflow(size, off, &end_index) || 3068bdebd6a2SJann Horn end_index > get_vm_area_size(area)) 3069e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3070bdebd6a2SJann Horn kaddr += off; 3071e69e9d4aSHATAYAMA Daisuke 3072e69e9d4aSHATAYAMA Daisuke do { 3073e69e9d4aSHATAYAMA Daisuke struct page *page = vmalloc_to_page(kaddr); 3074e69e9d4aSHATAYAMA Daisuke int ret; 3075e69e9d4aSHATAYAMA Daisuke 3076e69e9d4aSHATAYAMA Daisuke ret = vm_insert_page(vma, uaddr, page); 3077e69e9d4aSHATAYAMA Daisuke if (ret) 3078e69e9d4aSHATAYAMA Daisuke return ret; 3079e69e9d4aSHATAYAMA Daisuke 3080e69e9d4aSHATAYAMA Daisuke uaddr += PAGE_SIZE; 3081e69e9d4aSHATAYAMA Daisuke kaddr += PAGE_SIZE; 3082e69e9d4aSHATAYAMA Daisuke size -= PAGE_SIZE; 3083e69e9d4aSHATAYAMA Daisuke } while (size > 0); 3084e69e9d4aSHATAYAMA Daisuke 3085e69e9d4aSHATAYAMA Daisuke vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 3086e69e9d4aSHATAYAMA Daisuke 3087e69e9d4aSHATAYAMA Daisuke return 0; 3088e69e9d4aSHATAYAMA Daisuke } 3089e69e9d4aSHATAYAMA Daisuke 3090e69e9d4aSHATAYAMA Daisuke /** 309183342314SNick Piggin * remap_vmalloc_range - map vmalloc pages to userspace 309283342314SNick Piggin * @vma: vma to cover (map full range of vma) 309383342314SNick Piggin * @addr: vmalloc memory 309483342314SNick Piggin * @pgoff: number of pages into addr before first page to map 30957682486bSRandy Dunlap * 30967682486bSRandy Dunlap * Returns: 0 for success, -Exxx on failure 309783342314SNick Piggin * 309883342314SNick Piggin * This function checks that addr is a valid vmalloc'ed area, and 309983342314SNick Piggin * that it is big enough to cover the vma. Will return failure if 310083342314SNick Piggin * that criteria isn't met. 310183342314SNick Piggin * 310272fd4a35SRobert P. J. Day * Similar to remap_pfn_range() (see mm/memory.c) 310383342314SNick Piggin */ 310483342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 310583342314SNick Piggin unsigned long pgoff) 310683342314SNick Piggin { 3107e69e9d4aSHATAYAMA Daisuke return remap_vmalloc_range_partial(vma, vma->vm_start, 3108bdebd6a2SJann Horn addr, pgoff, 3109e69e9d4aSHATAYAMA Daisuke vma->vm_end - vma->vm_start); 311083342314SNick Piggin } 311183342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range); 311283342314SNick Piggin 31135f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area) 31145f4352fbSJeremy Fitzhardinge { 31155f4352fbSJeremy Fitzhardinge struct vm_struct *ret; 31165f4352fbSJeremy Fitzhardinge ret = remove_vm_area(area->addr); 31175f4352fbSJeremy Fitzhardinge BUG_ON(ret != area); 31185f4352fbSJeremy Fitzhardinge kfree(area); 31195f4352fbSJeremy Fitzhardinge } 31205f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area); 3121a10aa579SChristoph Lameter 31224f8b02b4STejun Heo #ifdef CONFIG_SMP 3123ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n) 3124ca23e405STejun Heo { 31254583e773SGeliang Tang return rb_entry_safe(n, struct vmap_area, rb_node); 3126ca23e405STejun Heo } 3127ca23e405STejun Heo 3128ca23e405STejun Heo /** 312968ad4a33SUladzislau Rezki (Sony) * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to 313068ad4a33SUladzislau Rezki (Sony) * @addr: target address 3131ca23e405STejun Heo * 313268ad4a33SUladzislau Rezki (Sony) * Returns: vmap_area if it is found. If there is no such area 313368ad4a33SUladzislau Rezki (Sony) * the first highest(reverse order) vmap_area is returned 313468ad4a33SUladzislau Rezki (Sony) * i.e. va->va_start < addr && va->va_end < addr or NULL 313568ad4a33SUladzislau Rezki (Sony) * if there are no any areas before @addr. 3136ca23e405STejun Heo */ 313768ad4a33SUladzislau Rezki (Sony) static struct vmap_area * 313868ad4a33SUladzislau Rezki (Sony) pvm_find_va_enclose_addr(unsigned long addr) 3139ca23e405STejun Heo { 314068ad4a33SUladzislau Rezki (Sony) struct vmap_area *va, *tmp; 314168ad4a33SUladzislau Rezki (Sony) struct rb_node *n; 314268ad4a33SUladzislau Rezki (Sony) 314368ad4a33SUladzislau Rezki (Sony) n = free_vmap_area_root.rb_node; 314468ad4a33SUladzislau Rezki (Sony) va = NULL; 3145ca23e405STejun Heo 3146ca23e405STejun Heo while (n) { 314768ad4a33SUladzislau Rezki (Sony) tmp = rb_entry(n, struct vmap_area, rb_node); 314868ad4a33SUladzislau Rezki (Sony) if (tmp->va_start <= addr) { 314968ad4a33SUladzislau Rezki (Sony) va = tmp; 315068ad4a33SUladzislau Rezki (Sony) if (tmp->va_end >= addr) 3151ca23e405STejun Heo break; 3152ca23e405STejun Heo 315368ad4a33SUladzislau Rezki (Sony) n = n->rb_right; 3154ca23e405STejun Heo } else { 315568ad4a33SUladzislau Rezki (Sony) n = n->rb_left; 3156ca23e405STejun Heo } 315768ad4a33SUladzislau Rezki (Sony) } 315868ad4a33SUladzislau Rezki (Sony) 315968ad4a33SUladzislau Rezki (Sony) return va; 3160ca23e405STejun Heo } 3161ca23e405STejun Heo 3162ca23e405STejun Heo /** 316368ad4a33SUladzislau Rezki (Sony) * pvm_determine_end_from_reverse - find the highest aligned address 316468ad4a33SUladzislau Rezki (Sony) * of free block below VMALLOC_END 316568ad4a33SUladzislau Rezki (Sony) * @va: 316668ad4a33SUladzislau Rezki (Sony) * in - the VA we start the search(reverse order); 316768ad4a33SUladzislau Rezki (Sony) * out - the VA with the highest aligned end address. 3168799fa85dSAlex Shi * @align: alignment for required highest address 3169ca23e405STejun Heo * 317068ad4a33SUladzislau Rezki (Sony) * Returns: determined end address within vmap_area 3171ca23e405STejun Heo */ 317268ad4a33SUladzislau Rezki (Sony) static unsigned long 317368ad4a33SUladzislau Rezki (Sony) pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align) 3174ca23e405STejun Heo { 317568ad4a33SUladzislau Rezki (Sony) unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 3176ca23e405STejun Heo unsigned long addr; 3177ca23e405STejun Heo 317868ad4a33SUladzislau Rezki (Sony) if (likely(*va)) { 317968ad4a33SUladzislau Rezki (Sony) list_for_each_entry_from_reverse((*va), 318068ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list, list) { 318168ad4a33SUladzislau Rezki (Sony) addr = min((*va)->va_end & ~(align - 1), vmalloc_end); 318268ad4a33SUladzislau Rezki (Sony) if ((*va)->va_start < addr) 318368ad4a33SUladzislau Rezki (Sony) return addr; 318468ad4a33SUladzislau Rezki (Sony) } 3185ca23e405STejun Heo } 3186ca23e405STejun Heo 318768ad4a33SUladzislau Rezki (Sony) return 0; 3188ca23e405STejun Heo } 3189ca23e405STejun Heo 3190ca23e405STejun Heo /** 3191ca23e405STejun Heo * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator 3192ca23e405STejun Heo * @offsets: array containing offset of each area 3193ca23e405STejun Heo * @sizes: array containing size of each area 3194ca23e405STejun Heo * @nr_vms: the number of areas to allocate 3195ca23e405STejun Heo * @align: alignment, all entries in @offsets and @sizes must be aligned to this 3196ca23e405STejun Heo * 3197ca23e405STejun Heo * Returns: kmalloc'd vm_struct pointer array pointing to allocated 3198ca23e405STejun Heo * vm_structs on success, %NULL on failure 3199ca23e405STejun Heo * 3200ca23e405STejun Heo * Percpu allocator wants to use congruent vm areas so that it can 3201ca23e405STejun Heo * maintain the offsets among percpu areas. This function allocates 3202ec3f64fcSDavid Rientjes * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to 3203ec3f64fcSDavid Rientjes * be scattered pretty far, distance between two areas easily going up 3204ec3f64fcSDavid Rientjes * to gigabytes. To avoid interacting with regular vmallocs, these 3205ec3f64fcSDavid Rientjes * areas are allocated from top. 3206ca23e405STejun Heo * 3207ca23e405STejun Heo * Despite its complicated look, this allocator is rather simple. It 320868ad4a33SUladzislau Rezki (Sony) * does everything top-down and scans free blocks from the end looking 320968ad4a33SUladzislau Rezki (Sony) * for matching base. While scanning, if any of the areas do not fit the 321068ad4a33SUladzislau Rezki (Sony) * base address is pulled down to fit the area. Scanning is repeated till 321168ad4a33SUladzislau Rezki (Sony) * all the areas fit and then all necessary data structures are inserted 321268ad4a33SUladzislau Rezki (Sony) * and the result is returned. 3213ca23e405STejun Heo */ 3214ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, 3215ca23e405STejun Heo const size_t *sizes, int nr_vms, 3216ec3f64fcSDavid Rientjes size_t align) 3217ca23e405STejun Heo { 3218ca23e405STejun Heo const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); 3219ca23e405STejun Heo const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 322068ad4a33SUladzislau Rezki (Sony) struct vmap_area **vas, *va; 3221ca23e405STejun Heo struct vm_struct **vms; 3222ca23e405STejun Heo int area, area2, last_area, term_area; 3223253a496dSDaniel Axtens unsigned long base, start, size, end, last_end, orig_start, orig_end; 3224ca23e405STejun Heo bool purged = false; 322568ad4a33SUladzislau Rezki (Sony) enum fit_type type; 3226ca23e405STejun Heo 3227ca23e405STejun Heo /* verify parameters and allocate data structures */ 3228891c49abSAlexander Kuleshov BUG_ON(offset_in_page(align) || !is_power_of_2(align)); 3229ca23e405STejun Heo for (last_area = 0, area = 0; area < nr_vms; area++) { 3230ca23e405STejun Heo start = offsets[area]; 3231ca23e405STejun Heo end = start + sizes[area]; 3232ca23e405STejun Heo 3233ca23e405STejun Heo /* is everything aligned properly? */ 3234ca23e405STejun Heo BUG_ON(!IS_ALIGNED(offsets[area], align)); 3235ca23e405STejun Heo BUG_ON(!IS_ALIGNED(sizes[area], align)); 3236ca23e405STejun Heo 3237ca23e405STejun Heo /* detect the area with the highest address */ 3238ca23e405STejun Heo if (start > offsets[last_area]) 3239ca23e405STejun Heo last_area = area; 3240ca23e405STejun Heo 3241c568da28SWei Yang for (area2 = area + 1; area2 < nr_vms; area2++) { 3242ca23e405STejun Heo unsigned long start2 = offsets[area2]; 3243ca23e405STejun Heo unsigned long end2 = start2 + sizes[area2]; 3244ca23e405STejun Heo 3245c568da28SWei Yang BUG_ON(start2 < end && start < end2); 3246ca23e405STejun Heo } 3247ca23e405STejun Heo } 3248ca23e405STejun Heo last_end = offsets[last_area] + sizes[last_area]; 3249ca23e405STejun Heo 3250ca23e405STejun Heo if (vmalloc_end - vmalloc_start < last_end) { 3251ca23e405STejun Heo WARN_ON(true); 3252ca23e405STejun Heo return NULL; 3253ca23e405STejun Heo } 3254ca23e405STejun Heo 32554d67d860SThomas Meyer vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL); 32564d67d860SThomas Meyer vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL); 3257ca23e405STejun Heo if (!vas || !vms) 3258f1db7afdSKautuk Consul goto err_free2; 3259ca23e405STejun Heo 3260ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 326168ad4a33SUladzislau Rezki (Sony) vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL); 3262ec3f64fcSDavid Rientjes vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL); 3263ca23e405STejun Heo if (!vas[area] || !vms[area]) 3264ca23e405STejun Heo goto err_free; 3265ca23e405STejun Heo } 3266ca23e405STejun Heo retry: 3267e36176beSUladzislau Rezki (Sony) spin_lock(&free_vmap_area_lock); 3268ca23e405STejun Heo 3269ca23e405STejun Heo /* start scanning - we scan from the top, begin with the last area */ 3270ca23e405STejun Heo area = term_area = last_area; 3271ca23e405STejun Heo start = offsets[area]; 3272ca23e405STejun Heo end = start + sizes[area]; 3273ca23e405STejun Heo 327468ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(vmalloc_end); 327568ad4a33SUladzislau Rezki (Sony) base = pvm_determine_end_from_reverse(&va, align) - end; 3276ca23e405STejun Heo 3277ca23e405STejun Heo while (true) { 3278ca23e405STejun Heo /* 3279ca23e405STejun Heo * base might have underflowed, add last_end before 3280ca23e405STejun Heo * comparing. 3281ca23e405STejun Heo */ 328268ad4a33SUladzislau Rezki (Sony) if (base + last_end < vmalloc_start + last_end) 328368ad4a33SUladzislau Rezki (Sony) goto overflow; 3284ca23e405STejun Heo 3285ca23e405STejun Heo /* 328668ad4a33SUladzislau Rezki (Sony) * Fitting base has not been found. 3287ca23e405STejun Heo */ 328868ad4a33SUladzislau Rezki (Sony) if (va == NULL) 328968ad4a33SUladzislau Rezki (Sony) goto overflow; 3290ca23e405STejun Heo 3291ca23e405STejun Heo /* 3292d8cc323dSQiujun Huang * If required width exceeds current VA block, move 32935336e52cSKuppuswamy Sathyanarayanan * base downwards and then recheck. 32945336e52cSKuppuswamy Sathyanarayanan */ 32955336e52cSKuppuswamy Sathyanarayanan if (base + end > va->va_end) { 32965336e52cSKuppuswamy Sathyanarayanan base = pvm_determine_end_from_reverse(&va, align) - end; 32975336e52cSKuppuswamy Sathyanarayanan term_area = area; 32985336e52cSKuppuswamy Sathyanarayanan continue; 32995336e52cSKuppuswamy Sathyanarayanan } 33005336e52cSKuppuswamy Sathyanarayanan 33015336e52cSKuppuswamy Sathyanarayanan /* 330268ad4a33SUladzislau Rezki (Sony) * If this VA does not fit, move base downwards and recheck. 3303ca23e405STejun Heo */ 33045336e52cSKuppuswamy Sathyanarayanan if (base + start < va->va_start) { 330568ad4a33SUladzislau Rezki (Sony) va = node_to_va(rb_prev(&va->rb_node)); 330668ad4a33SUladzislau Rezki (Sony) base = pvm_determine_end_from_reverse(&va, align) - end; 3307ca23e405STejun Heo term_area = area; 3308ca23e405STejun Heo continue; 3309ca23e405STejun Heo } 3310ca23e405STejun Heo 3311ca23e405STejun Heo /* 3312ca23e405STejun Heo * This area fits, move on to the previous one. If 3313ca23e405STejun Heo * the previous one is the terminal one, we're done. 3314ca23e405STejun Heo */ 3315ca23e405STejun Heo area = (area + nr_vms - 1) % nr_vms; 3316ca23e405STejun Heo if (area == term_area) 3317ca23e405STejun Heo break; 331868ad4a33SUladzislau Rezki (Sony) 3319ca23e405STejun Heo start = offsets[area]; 3320ca23e405STejun Heo end = start + sizes[area]; 332168ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(base + end); 3322ca23e405STejun Heo } 332368ad4a33SUladzislau Rezki (Sony) 3324ca23e405STejun Heo /* we've found a fitting base, insert all va's */ 3325ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 332668ad4a33SUladzislau Rezki (Sony) int ret; 3327ca23e405STejun Heo 332868ad4a33SUladzislau Rezki (Sony) start = base + offsets[area]; 332968ad4a33SUladzislau Rezki (Sony) size = sizes[area]; 333068ad4a33SUladzislau Rezki (Sony) 333168ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(start); 333268ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(va == NULL)) 333368ad4a33SUladzislau Rezki (Sony) /* It is a BUG(), but trigger recovery instead. */ 333468ad4a33SUladzislau Rezki (Sony) goto recovery; 333568ad4a33SUladzislau Rezki (Sony) 333668ad4a33SUladzislau Rezki (Sony) type = classify_va_fit_type(va, start, size); 333768ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(type == NOTHING_FIT)) 333868ad4a33SUladzislau Rezki (Sony) /* It is a BUG(), but trigger recovery instead. */ 333968ad4a33SUladzislau Rezki (Sony) goto recovery; 334068ad4a33SUladzislau Rezki (Sony) 334168ad4a33SUladzislau Rezki (Sony) ret = adjust_va_to_fit_type(va, start, size, type); 334268ad4a33SUladzislau Rezki (Sony) if (unlikely(ret)) 334368ad4a33SUladzislau Rezki (Sony) goto recovery; 334468ad4a33SUladzislau Rezki (Sony) 334568ad4a33SUladzislau Rezki (Sony) /* Allocated area. */ 334668ad4a33SUladzislau Rezki (Sony) va = vas[area]; 334768ad4a33SUladzislau Rezki (Sony) va->va_start = start; 334868ad4a33SUladzislau Rezki (Sony) va->va_end = start + size; 3349ca23e405STejun Heo } 3350ca23e405STejun Heo 3351e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 3352ca23e405STejun Heo 3353253a496dSDaniel Axtens /* populate the kasan shadow space */ 3354253a496dSDaniel Axtens for (area = 0; area < nr_vms; area++) { 3355253a496dSDaniel Axtens if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area])) 3356253a496dSDaniel Axtens goto err_free_shadow; 3357253a496dSDaniel Axtens 3358253a496dSDaniel Axtens kasan_unpoison_vmalloc((void *)vas[area]->va_start, 3359253a496dSDaniel Axtens sizes[area]); 3360253a496dSDaniel Axtens } 3361253a496dSDaniel Axtens 3362ca23e405STejun Heo /* insert all vm's */ 3363e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 3364e36176beSUladzislau Rezki (Sony) for (area = 0; area < nr_vms; area++) { 3365e36176beSUladzislau Rezki (Sony) insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list); 3366e36176beSUladzislau Rezki (Sony) 3367e36176beSUladzislau Rezki (Sony) setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC, 3368ca23e405STejun Heo pcpu_get_vm_areas); 3369e36176beSUladzislau Rezki (Sony) } 3370e36176beSUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 3371ca23e405STejun Heo 3372ca23e405STejun Heo kfree(vas); 3373ca23e405STejun Heo return vms; 3374ca23e405STejun Heo 337568ad4a33SUladzislau Rezki (Sony) recovery: 3376e36176beSUladzislau Rezki (Sony) /* 3377e36176beSUladzislau Rezki (Sony) * Remove previously allocated areas. There is no 3378e36176beSUladzislau Rezki (Sony) * need in removing these areas from the busy tree, 3379e36176beSUladzislau Rezki (Sony) * because they are inserted only on the final step 3380e36176beSUladzislau Rezki (Sony) * and when pcpu_get_vm_areas() is success. 3381e36176beSUladzislau Rezki (Sony) */ 338268ad4a33SUladzislau Rezki (Sony) while (area--) { 3383253a496dSDaniel Axtens orig_start = vas[area]->va_start; 3384253a496dSDaniel Axtens orig_end = vas[area]->va_end; 338596e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, 33863c5c3cfbSDaniel Axtens &free_vmap_area_list); 33879c801f61SUladzislau Rezki (Sony) if (va) 3388253a496dSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 3389253a496dSDaniel Axtens va->va_start, va->va_end); 339068ad4a33SUladzislau Rezki (Sony) vas[area] = NULL; 339168ad4a33SUladzislau Rezki (Sony) } 339268ad4a33SUladzislau Rezki (Sony) 339368ad4a33SUladzislau Rezki (Sony) overflow: 3394e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 339568ad4a33SUladzislau Rezki (Sony) if (!purged) { 339668ad4a33SUladzislau Rezki (Sony) purge_vmap_area_lazy(); 339768ad4a33SUladzislau Rezki (Sony) purged = true; 339868ad4a33SUladzislau Rezki (Sony) 339968ad4a33SUladzislau Rezki (Sony) /* Before "retry", check if we recover. */ 340068ad4a33SUladzislau Rezki (Sony) for (area = 0; area < nr_vms; area++) { 340168ad4a33SUladzislau Rezki (Sony) if (vas[area]) 340268ad4a33SUladzislau Rezki (Sony) continue; 340368ad4a33SUladzislau Rezki (Sony) 340468ad4a33SUladzislau Rezki (Sony) vas[area] = kmem_cache_zalloc( 340568ad4a33SUladzislau Rezki (Sony) vmap_area_cachep, GFP_KERNEL); 340668ad4a33SUladzislau Rezki (Sony) if (!vas[area]) 340768ad4a33SUladzislau Rezki (Sony) goto err_free; 340868ad4a33SUladzislau Rezki (Sony) } 340968ad4a33SUladzislau Rezki (Sony) 341068ad4a33SUladzislau Rezki (Sony) goto retry; 341168ad4a33SUladzislau Rezki (Sony) } 341268ad4a33SUladzislau Rezki (Sony) 3413ca23e405STejun Heo err_free: 3414ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 341568ad4a33SUladzislau Rezki (Sony) if (vas[area]) 341668ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, vas[area]); 341768ad4a33SUladzislau Rezki (Sony) 3418ca23e405STejun Heo kfree(vms[area]); 3419ca23e405STejun Heo } 3420f1db7afdSKautuk Consul err_free2: 3421ca23e405STejun Heo kfree(vas); 3422ca23e405STejun Heo kfree(vms); 3423ca23e405STejun Heo return NULL; 3424253a496dSDaniel Axtens 3425253a496dSDaniel Axtens err_free_shadow: 3426253a496dSDaniel Axtens spin_lock(&free_vmap_area_lock); 3427253a496dSDaniel Axtens /* 3428253a496dSDaniel Axtens * We release all the vmalloc shadows, even the ones for regions that 3429253a496dSDaniel Axtens * hadn't been successfully added. This relies on kasan_release_vmalloc 3430253a496dSDaniel Axtens * being able to tolerate this case. 3431253a496dSDaniel Axtens */ 3432253a496dSDaniel Axtens for (area = 0; area < nr_vms; area++) { 3433253a496dSDaniel Axtens orig_start = vas[area]->va_start; 3434253a496dSDaniel Axtens orig_end = vas[area]->va_end; 343596e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, 3436253a496dSDaniel Axtens &free_vmap_area_list); 34379c801f61SUladzislau Rezki (Sony) if (va) 3438253a496dSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 3439253a496dSDaniel Axtens va->va_start, va->va_end); 3440253a496dSDaniel Axtens vas[area] = NULL; 3441253a496dSDaniel Axtens kfree(vms[area]); 3442253a496dSDaniel Axtens } 3443253a496dSDaniel Axtens spin_unlock(&free_vmap_area_lock); 3444253a496dSDaniel Axtens kfree(vas); 3445253a496dSDaniel Axtens kfree(vms); 3446253a496dSDaniel Axtens return NULL; 3447ca23e405STejun Heo } 3448ca23e405STejun Heo 3449ca23e405STejun Heo /** 3450ca23e405STejun Heo * pcpu_free_vm_areas - free vmalloc areas for percpu allocator 3451ca23e405STejun Heo * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() 3452ca23e405STejun Heo * @nr_vms: the number of allocated areas 3453ca23e405STejun Heo * 3454ca23e405STejun Heo * Free vm_structs and the array allocated by pcpu_get_vm_areas(). 3455ca23e405STejun Heo */ 3456ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) 3457ca23e405STejun Heo { 3458ca23e405STejun Heo int i; 3459ca23e405STejun Heo 3460ca23e405STejun Heo for (i = 0; i < nr_vms; i++) 3461ca23e405STejun Heo free_vm_area(vms[i]); 3462ca23e405STejun Heo kfree(vms); 3463ca23e405STejun Heo } 34644f8b02b4STejun Heo #endif /* CONFIG_SMP */ 3465a10aa579SChristoph Lameter 34665bb1bb35SPaul E. McKenney #ifdef CONFIG_PRINTK 346798f18083SPaul E. McKenney bool vmalloc_dump_obj(void *object) 346898f18083SPaul E. McKenney { 346998f18083SPaul E. McKenney struct vm_struct *vm; 347098f18083SPaul E. McKenney void *objp = (void *)PAGE_ALIGN((unsigned long)object); 347198f18083SPaul E. McKenney 347298f18083SPaul E. McKenney vm = find_vm_area(objp); 347398f18083SPaul E. McKenney if (!vm) 347498f18083SPaul E. McKenney return false; 3475bd34dcd4SPaul E. McKenney pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n", 3476bd34dcd4SPaul E. McKenney vm->nr_pages, (unsigned long)vm->addr, vm->caller); 347798f18083SPaul E. McKenney return true; 347898f18083SPaul E. McKenney } 34795bb1bb35SPaul E. McKenney #endif 348098f18083SPaul E. McKenney 3481a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS 3482a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos) 3483e36176beSUladzislau Rezki (Sony) __acquires(&vmap_purge_lock) 3484d4033afdSJoonsoo Kim __acquires(&vmap_area_lock) 3485a10aa579SChristoph Lameter { 3486e36176beSUladzislau Rezki (Sony) mutex_lock(&vmap_purge_lock); 3487d4033afdSJoonsoo Kim spin_lock(&vmap_area_lock); 3488e36176beSUladzislau Rezki (Sony) 34893f500069Szijun_hu return seq_list_start(&vmap_area_list, *pos); 3490a10aa579SChristoph Lameter } 3491a10aa579SChristoph Lameter 3492a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos) 3493a10aa579SChristoph Lameter { 34943f500069Szijun_hu return seq_list_next(p, &vmap_area_list, pos); 3495a10aa579SChristoph Lameter } 3496a10aa579SChristoph Lameter 3497a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p) 3498d4033afdSJoonsoo Kim __releases(&vmap_area_lock) 34990a7dd4e9SWaiman Long __releases(&vmap_purge_lock) 3500a10aa579SChristoph Lameter { 3501d4033afdSJoonsoo Kim spin_unlock(&vmap_area_lock); 35020a7dd4e9SWaiman Long mutex_unlock(&vmap_purge_lock); 3503a10aa579SChristoph Lameter } 3504a10aa579SChristoph Lameter 3505a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v) 3506a47a126aSEric Dumazet { 3507e5adfffcSKirill A. Shutemov if (IS_ENABLED(CONFIG_NUMA)) { 3508a47a126aSEric Dumazet unsigned int nr, *counters = m->private; 3509a47a126aSEric Dumazet 3510a47a126aSEric Dumazet if (!counters) 3511a47a126aSEric Dumazet return; 3512a47a126aSEric Dumazet 3513af12346cSWanpeng Li if (v->flags & VM_UNINITIALIZED) 3514af12346cSWanpeng Li return; 35157e5b528bSDmitry Vyukov /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ 35167e5b528bSDmitry Vyukov smp_rmb(); 3517af12346cSWanpeng Li 3518a47a126aSEric Dumazet memset(counters, 0, nr_node_ids * sizeof(unsigned int)); 3519a47a126aSEric Dumazet 3520a47a126aSEric Dumazet for (nr = 0; nr < v->nr_pages; nr++) 3521a47a126aSEric Dumazet counters[page_to_nid(v->pages[nr])]++; 3522a47a126aSEric Dumazet 3523a47a126aSEric Dumazet for_each_node_state(nr, N_HIGH_MEMORY) 3524a47a126aSEric Dumazet if (counters[nr]) 3525a47a126aSEric Dumazet seq_printf(m, " N%u=%u", nr, counters[nr]); 3526a47a126aSEric Dumazet } 3527a47a126aSEric Dumazet } 3528a47a126aSEric Dumazet 3529dd3b8353SUladzislau Rezki (Sony) static void show_purge_info(struct seq_file *m) 3530dd3b8353SUladzislau Rezki (Sony) { 3531dd3b8353SUladzislau Rezki (Sony) struct vmap_area *va; 3532dd3b8353SUladzislau Rezki (Sony) 353396e2db45SUladzislau Rezki (Sony) spin_lock(&purge_vmap_area_lock); 353496e2db45SUladzislau Rezki (Sony) list_for_each_entry(va, &purge_vmap_area_list, list) { 3535dd3b8353SUladzislau Rezki (Sony) seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n", 3536dd3b8353SUladzislau Rezki (Sony) (void *)va->va_start, (void *)va->va_end, 3537dd3b8353SUladzislau Rezki (Sony) va->va_end - va->va_start); 3538dd3b8353SUladzislau Rezki (Sony) } 353996e2db45SUladzislau Rezki (Sony) spin_unlock(&purge_vmap_area_lock); 3540dd3b8353SUladzislau Rezki (Sony) } 3541dd3b8353SUladzislau Rezki (Sony) 3542a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p) 3543a10aa579SChristoph Lameter { 35443f500069Szijun_hu struct vmap_area *va; 3545d4033afdSJoonsoo Kim struct vm_struct *v; 3546d4033afdSJoonsoo Kim 35473f500069Szijun_hu va = list_entry(p, struct vmap_area, list); 35483f500069Szijun_hu 3549c2ce8c14SWanpeng Li /* 3550688fcbfcSPengfei Li * s_show can encounter race with remove_vm_area, !vm on behalf 3551688fcbfcSPengfei Li * of vmap area is being tear down or vm_map_ram allocation. 3552c2ce8c14SWanpeng Li */ 3553688fcbfcSPengfei Li if (!va->vm) { 3554dd3b8353SUladzislau Rezki (Sony) seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n", 355578c72746SYisheng Xie (void *)va->va_start, (void *)va->va_end, 3556dd3b8353SUladzislau Rezki (Sony) va->va_end - va->va_start); 355778c72746SYisheng Xie 3558d4033afdSJoonsoo Kim return 0; 355978c72746SYisheng Xie } 3560d4033afdSJoonsoo Kim 3561d4033afdSJoonsoo Kim v = va->vm; 3562a10aa579SChristoph Lameter 356345ec1690SKees Cook seq_printf(m, "0x%pK-0x%pK %7ld", 3564a10aa579SChristoph Lameter v->addr, v->addr + v->size, v->size); 3565a10aa579SChristoph Lameter 356662c70bceSJoe Perches if (v->caller) 356762c70bceSJoe Perches seq_printf(m, " %pS", v->caller); 356823016969SChristoph Lameter 3569a10aa579SChristoph Lameter if (v->nr_pages) 3570a10aa579SChristoph Lameter seq_printf(m, " pages=%d", v->nr_pages); 3571a10aa579SChristoph Lameter 3572a10aa579SChristoph Lameter if (v->phys_addr) 3573199eaa05SMiles Chen seq_printf(m, " phys=%pa", &v->phys_addr); 3574a10aa579SChristoph Lameter 3575a10aa579SChristoph Lameter if (v->flags & VM_IOREMAP) 3576f4527c90SFabian Frederick seq_puts(m, " ioremap"); 3577a10aa579SChristoph Lameter 3578a10aa579SChristoph Lameter if (v->flags & VM_ALLOC) 3579f4527c90SFabian Frederick seq_puts(m, " vmalloc"); 3580a10aa579SChristoph Lameter 3581a10aa579SChristoph Lameter if (v->flags & VM_MAP) 3582f4527c90SFabian Frederick seq_puts(m, " vmap"); 3583a10aa579SChristoph Lameter 3584a10aa579SChristoph Lameter if (v->flags & VM_USERMAP) 3585f4527c90SFabian Frederick seq_puts(m, " user"); 3586a10aa579SChristoph Lameter 3587fe9041c2SChristoph Hellwig if (v->flags & VM_DMA_COHERENT) 3588fe9041c2SChristoph Hellwig seq_puts(m, " dma-coherent"); 3589fe9041c2SChristoph Hellwig 3590244d63eeSDavid Rientjes if (is_vmalloc_addr(v->pages)) 3591f4527c90SFabian Frederick seq_puts(m, " vpages"); 3592a10aa579SChristoph Lameter 3593a47a126aSEric Dumazet show_numa_info(m, v); 3594a10aa579SChristoph Lameter seq_putc(m, '\n'); 3595dd3b8353SUladzislau Rezki (Sony) 3596dd3b8353SUladzislau Rezki (Sony) /* 359796e2db45SUladzislau Rezki (Sony) * As a final step, dump "unpurged" areas. 3598dd3b8353SUladzislau Rezki (Sony) */ 3599dd3b8353SUladzislau Rezki (Sony) if (list_is_last(&va->list, &vmap_area_list)) 3600dd3b8353SUladzislau Rezki (Sony) show_purge_info(m); 3601dd3b8353SUladzislau Rezki (Sony) 3602a10aa579SChristoph Lameter return 0; 3603a10aa579SChristoph Lameter } 3604a10aa579SChristoph Lameter 36055f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = { 3606a10aa579SChristoph Lameter .start = s_start, 3607a10aa579SChristoph Lameter .next = s_next, 3608a10aa579SChristoph Lameter .stop = s_stop, 3609a10aa579SChristoph Lameter .show = s_show, 3610a10aa579SChristoph Lameter }; 36115f6a6a9cSAlexey Dobriyan 36125f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void) 36135f6a6a9cSAlexey Dobriyan { 3614fddda2b7SChristoph Hellwig if (IS_ENABLED(CONFIG_NUMA)) 36150825a6f9SJoe Perches proc_create_seq_private("vmallocinfo", 0400, NULL, 361644414d82SChristoph Hellwig &vmalloc_op, 361744414d82SChristoph Hellwig nr_node_ids * sizeof(unsigned int), NULL); 3618fddda2b7SChristoph Hellwig else 36190825a6f9SJoe Perches proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op); 36205f6a6a9cSAlexey Dobriyan return 0; 36215f6a6a9cSAlexey Dobriyan } 36225f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init); 3623db3808c1SJoonsoo Kim 3624a10aa579SChristoph Lameter #endif 3625