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> 285da96bddSMel Gorman #include <linux/io.h> 29db64fe02SNick Piggin #include <linux/rcupdate.h> 30f0aa6617STejun Heo #include <linux/pfn.h> 3189219d37SCatalin Marinas #include <linux/kmemleak.h> 3260063497SArun Sharma #include <linux/atomic.h> 333b32123dSGideon Israel Dsouza #include <linux/compiler.h> 344e5aa1f4SShakeel Butt #include <linux/memcontrol.h> 3532fcfd40SAl Viro #include <linux/llist.h> 360f616be1SToshi Kani #include <linux/bitops.h> 3768ad4a33SUladzislau Rezki (Sony) #include <linux/rbtree_augmented.h> 38bdebd6a2SJann Horn #include <linux/overflow.h> 39c0eb315aSNicholas Piggin #include <linux/pgtable.h> 407c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 41f7ee1f13SChristophe Leroy #include <linux/hugetlb.h> 42451769ebSMichal Hocko #include <linux/sched/mm.h> 431da177e4SLinus Torvalds #include <asm/tlbflush.h> 442dca6999SDavid Miller #include <asm/shmparam.h> 451da177e4SLinus Torvalds 46dd56b046SMel Gorman #include "internal.h" 472a681cfaSJoerg Roedel #include "pgalloc-track.h" 48dd56b046SMel Gorman 4982a70ce0SChristoph Hellwig #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP 5082a70ce0SChristoph Hellwig static unsigned int __ro_after_init ioremap_max_page_shift = BITS_PER_LONG - 1; 5182a70ce0SChristoph Hellwig 5282a70ce0SChristoph Hellwig static int __init set_nohugeiomap(char *str) 5382a70ce0SChristoph Hellwig { 5482a70ce0SChristoph Hellwig ioremap_max_page_shift = PAGE_SHIFT; 5582a70ce0SChristoph Hellwig return 0; 5682a70ce0SChristoph Hellwig } 5782a70ce0SChristoph Hellwig early_param("nohugeiomap", set_nohugeiomap); 5882a70ce0SChristoph Hellwig #else /* CONFIG_HAVE_ARCH_HUGE_VMAP */ 5982a70ce0SChristoph Hellwig static const unsigned int ioremap_max_page_shift = PAGE_SHIFT; 6082a70ce0SChristoph Hellwig #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ 6182a70ce0SChristoph Hellwig 62121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 63121e6f32SNicholas Piggin static bool __ro_after_init vmap_allow_huge = true; 64121e6f32SNicholas Piggin 65121e6f32SNicholas Piggin static int __init set_nohugevmalloc(char *str) 66121e6f32SNicholas Piggin { 67121e6f32SNicholas Piggin vmap_allow_huge = false; 68121e6f32SNicholas Piggin return 0; 69121e6f32SNicholas Piggin } 70121e6f32SNicholas Piggin early_param("nohugevmalloc", set_nohugevmalloc); 71121e6f32SNicholas Piggin #else /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */ 72121e6f32SNicholas Piggin static const bool vmap_allow_huge = false; 73121e6f32SNicholas Piggin #endif /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */ 74121e6f32SNicholas Piggin 75186525bdSIngo Molnar bool is_vmalloc_addr(const void *x) 76186525bdSIngo Molnar { 77186525bdSIngo Molnar unsigned long addr = (unsigned long)x; 78186525bdSIngo Molnar 79186525bdSIngo Molnar return addr >= VMALLOC_START && addr < VMALLOC_END; 80186525bdSIngo Molnar } 81186525bdSIngo Molnar EXPORT_SYMBOL(is_vmalloc_addr); 82186525bdSIngo Molnar 8332fcfd40SAl Viro struct vfree_deferred { 8432fcfd40SAl Viro struct llist_head list; 8532fcfd40SAl Viro struct work_struct wq; 8632fcfd40SAl Viro }; 8732fcfd40SAl Viro static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred); 8832fcfd40SAl Viro 8932fcfd40SAl Viro static void __vunmap(const void *, int); 9032fcfd40SAl Viro 9132fcfd40SAl Viro static void free_work(struct work_struct *w) 9232fcfd40SAl Viro { 9332fcfd40SAl Viro struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq); 94894e58c1SByungchul Park struct llist_node *t, *llnode; 95894e58c1SByungchul Park 96894e58c1SByungchul Park llist_for_each_safe(llnode, t, llist_del_all(&p->list)) 97894e58c1SByungchul Park __vunmap((void *)llnode, 1); 9832fcfd40SAl Viro } 9932fcfd40SAl Viro 100db64fe02SNick Piggin /*** Page table manipulation functions ***/ 1015e9e3d77SNicholas Piggin static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 1025e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 103f7ee1f13SChristophe Leroy unsigned int max_page_shift, pgtbl_mod_mask *mask) 1045e9e3d77SNicholas Piggin { 1055e9e3d77SNicholas Piggin pte_t *pte; 1065e9e3d77SNicholas Piggin u64 pfn; 107f7ee1f13SChristophe Leroy unsigned long size = PAGE_SIZE; 1085e9e3d77SNicholas Piggin 1095e9e3d77SNicholas Piggin pfn = phys_addr >> PAGE_SHIFT; 1105e9e3d77SNicholas Piggin pte = pte_alloc_kernel_track(pmd, addr, mask); 1115e9e3d77SNicholas Piggin if (!pte) 1125e9e3d77SNicholas Piggin return -ENOMEM; 1135e9e3d77SNicholas Piggin do { 1145e9e3d77SNicholas Piggin BUG_ON(!pte_none(*pte)); 115f7ee1f13SChristophe Leroy 116f7ee1f13SChristophe Leroy #ifdef CONFIG_HUGETLB_PAGE 117f7ee1f13SChristophe Leroy size = arch_vmap_pte_range_map_size(addr, end, pfn, max_page_shift); 118f7ee1f13SChristophe Leroy if (size != PAGE_SIZE) { 119f7ee1f13SChristophe Leroy pte_t entry = pfn_pte(pfn, prot); 120f7ee1f13SChristophe Leroy 121f7ee1f13SChristophe Leroy entry = arch_make_huge_pte(entry, ilog2(size), 0); 122f7ee1f13SChristophe Leroy set_huge_pte_at(&init_mm, addr, pte, entry); 123f7ee1f13SChristophe Leroy pfn += PFN_DOWN(size); 124f7ee1f13SChristophe Leroy continue; 125f7ee1f13SChristophe Leroy } 126f7ee1f13SChristophe Leroy #endif 1275e9e3d77SNicholas Piggin set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot)); 1285e9e3d77SNicholas Piggin pfn++; 129f7ee1f13SChristophe Leroy } while (pte += PFN_DOWN(size), addr += size, addr != end); 1305e9e3d77SNicholas Piggin *mask |= PGTBL_PTE_MODIFIED; 1315e9e3d77SNicholas Piggin return 0; 1325e9e3d77SNicholas Piggin } 1335e9e3d77SNicholas Piggin 1345e9e3d77SNicholas Piggin static int vmap_try_huge_pmd(pmd_t *pmd, unsigned long addr, unsigned long end, 1355e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 1365e9e3d77SNicholas Piggin unsigned int max_page_shift) 1375e9e3d77SNicholas Piggin { 1385e9e3d77SNicholas Piggin if (max_page_shift < PMD_SHIFT) 1395e9e3d77SNicholas Piggin return 0; 1405e9e3d77SNicholas Piggin 1415e9e3d77SNicholas Piggin if (!arch_vmap_pmd_supported(prot)) 1425e9e3d77SNicholas Piggin return 0; 1435e9e3d77SNicholas Piggin 1445e9e3d77SNicholas Piggin if ((end - addr) != PMD_SIZE) 1455e9e3d77SNicholas Piggin return 0; 1465e9e3d77SNicholas Piggin 1475e9e3d77SNicholas Piggin if (!IS_ALIGNED(addr, PMD_SIZE)) 1485e9e3d77SNicholas Piggin return 0; 1495e9e3d77SNicholas Piggin 1505e9e3d77SNicholas Piggin if (!IS_ALIGNED(phys_addr, PMD_SIZE)) 1515e9e3d77SNicholas Piggin return 0; 1525e9e3d77SNicholas Piggin 1535e9e3d77SNicholas Piggin if (pmd_present(*pmd) && !pmd_free_pte_page(pmd, addr)) 1545e9e3d77SNicholas Piggin return 0; 1555e9e3d77SNicholas Piggin 1565e9e3d77SNicholas Piggin return pmd_set_huge(pmd, phys_addr, prot); 1575e9e3d77SNicholas Piggin } 1585e9e3d77SNicholas Piggin 1595e9e3d77SNicholas Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end, 1605e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 1615e9e3d77SNicholas Piggin unsigned int max_page_shift, pgtbl_mod_mask *mask) 1625e9e3d77SNicholas Piggin { 1635e9e3d77SNicholas Piggin pmd_t *pmd; 1645e9e3d77SNicholas Piggin unsigned long next; 1655e9e3d77SNicholas Piggin 1665e9e3d77SNicholas Piggin pmd = pmd_alloc_track(&init_mm, pud, addr, mask); 1675e9e3d77SNicholas Piggin if (!pmd) 1685e9e3d77SNicholas Piggin return -ENOMEM; 1695e9e3d77SNicholas Piggin do { 1705e9e3d77SNicholas Piggin next = pmd_addr_end(addr, end); 1715e9e3d77SNicholas Piggin 1725e9e3d77SNicholas Piggin if (vmap_try_huge_pmd(pmd, addr, next, phys_addr, prot, 1735e9e3d77SNicholas Piggin max_page_shift)) { 1745e9e3d77SNicholas Piggin *mask |= PGTBL_PMD_MODIFIED; 1755e9e3d77SNicholas Piggin continue; 1765e9e3d77SNicholas Piggin } 1775e9e3d77SNicholas Piggin 178f7ee1f13SChristophe Leroy if (vmap_pte_range(pmd, addr, next, phys_addr, prot, max_page_shift, mask)) 1795e9e3d77SNicholas Piggin return -ENOMEM; 1805e9e3d77SNicholas Piggin } while (pmd++, phys_addr += (next - addr), addr = next, addr != end); 1815e9e3d77SNicholas Piggin return 0; 1825e9e3d77SNicholas Piggin } 1835e9e3d77SNicholas Piggin 1845e9e3d77SNicholas Piggin static int vmap_try_huge_pud(pud_t *pud, unsigned long addr, unsigned long end, 1855e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 1865e9e3d77SNicholas Piggin unsigned int max_page_shift) 1875e9e3d77SNicholas Piggin { 1885e9e3d77SNicholas Piggin if (max_page_shift < PUD_SHIFT) 1895e9e3d77SNicholas Piggin return 0; 1905e9e3d77SNicholas Piggin 1915e9e3d77SNicholas Piggin if (!arch_vmap_pud_supported(prot)) 1925e9e3d77SNicholas Piggin return 0; 1935e9e3d77SNicholas Piggin 1945e9e3d77SNicholas Piggin if ((end - addr) != PUD_SIZE) 1955e9e3d77SNicholas Piggin return 0; 1965e9e3d77SNicholas Piggin 1975e9e3d77SNicholas Piggin if (!IS_ALIGNED(addr, PUD_SIZE)) 1985e9e3d77SNicholas Piggin return 0; 1995e9e3d77SNicholas Piggin 2005e9e3d77SNicholas Piggin if (!IS_ALIGNED(phys_addr, PUD_SIZE)) 2015e9e3d77SNicholas Piggin return 0; 2025e9e3d77SNicholas Piggin 2035e9e3d77SNicholas Piggin if (pud_present(*pud) && !pud_free_pmd_page(pud, addr)) 2045e9e3d77SNicholas Piggin return 0; 2055e9e3d77SNicholas Piggin 2065e9e3d77SNicholas Piggin return pud_set_huge(pud, phys_addr, prot); 2075e9e3d77SNicholas Piggin } 2085e9e3d77SNicholas Piggin 2095e9e3d77SNicholas Piggin static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, 2105e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 2115e9e3d77SNicholas Piggin unsigned int max_page_shift, pgtbl_mod_mask *mask) 2125e9e3d77SNicholas Piggin { 2135e9e3d77SNicholas Piggin pud_t *pud; 2145e9e3d77SNicholas Piggin unsigned long next; 2155e9e3d77SNicholas Piggin 2165e9e3d77SNicholas Piggin pud = pud_alloc_track(&init_mm, p4d, addr, mask); 2175e9e3d77SNicholas Piggin if (!pud) 2185e9e3d77SNicholas Piggin return -ENOMEM; 2195e9e3d77SNicholas Piggin do { 2205e9e3d77SNicholas Piggin next = pud_addr_end(addr, end); 2215e9e3d77SNicholas Piggin 2225e9e3d77SNicholas Piggin if (vmap_try_huge_pud(pud, addr, next, phys_addr, prot, 2235e9e3d77SNicholas Piggin max_page_shift)) { 2245e9e3d77SNicholas Piggin *mask |= PGTBL_PUD_MODIFIED; 2255e9e3d77SNicholas Piggin continue; 2265e9e3d77SNicholas Piggin } 2275e9e3d77SNicholas Piggin 2285e9e3d77SNicholas Piggin if (vmap_pmd_range(pud, addr, next, phys_addr, prot, 2295e9e3d77SNicholas Piggin max_page_shift, mask)) 2305e9e3d77SNicholas Piggin return -ENOMEM; 2315e9e3d77SNicholas Piggin } while (pud++, phys_addr += (next - addr), addr = next, addr != end); 2325e9e3d77SNicholas Piggin return 0; 2335e9e3d77SNicholas Piggin } 2345e9e3d77SNicholas Piggin 2355e9e3d77SNicholas Piggin static int vmap_try_huge_p4d(p4d_t *p4d, unsigned long addr, unsigned long end, 2365e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 2375e9e3d77SNicholas Piggin unsigned int max_page_shift) 2385e9e3d77SNicholas Piggin { 2395e9e3d77SNicholas Piggin if (max_page_shift < P4D_SHIFT) 2405e9e3d77SNicholas Piggin return 0; 2415e9e3d77SNicholas Piggin 2425e9e3d77SNicholas Piggin if (!arch_vmap_p4d_supported(prot)) 2435e9e3d77SNicholas Piggin return 0; 2445e9e3d77SNicholas Piggin 2455e9e3d77SNicholas Piggin if ((end - addr) != P4D_SIZE) 2465e9e3d77SNicholas Piggin return 0; 2475e9e3d77SNicholas Piggin 2485e9e3d77SNicholas Piggin if (!IS_ALIGNED(addr, P4D_SIZE)) 2495e9e3d77SNicholas Piggin return 0; 2505e9e3d77SNicholas Piggin 2515e9e3d77SNicholas Piggin if (!IS_ALIGNED(phys_addr, P4D_SIZE)) 2525e9e3d77SNicholas Piggin return 0; 2535e9e3d77SNicholas Piggin 2545e9e3d77SNicholas Piggin if (p4d_present(*p4d) && !p4d_free_pud_page(p4d, addr)) 2555e9e3d77SNicholas Piggin return 0; 2565e9e3d77SNicholas Piggin 2575e9e3d77SNicholas Piggin return p4d_set_huge(p4d, phys_addr, prot); 2585e9e3d77SNicholas Piggin } 2595e9e3d77SNicholas Piggin 2605e9e3d77SNicholas Piggin static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end, 2615e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 2625e9e3d77SNicholas Piggin unsigned int max_page_shift, pgtbl_mod_mask *mask) 2635e9e3d77SNicholas Piggin { 2645e9e3d77SNicholas Piggin p4d_t *p4d; 2655e9e3d77SNicholas Piggin unsigned long next; 2665e9e3d77SNicholas Piggin 2675e9e3d77SNicholas Piggin p4d = p4d_alloc_track(&init_mm, pgd, addr, mask); 2685e9e3d77SNicholas Piggin if (!p4d) 2695e9e3d77SNicholas Piggin return -ENOMEM; 2705e9e3d77SNicholas Piggin do { 2715e9e3d77SNicholas Piggin next = p4d_addr_end(addr, end); 2725e9e3d77SNicholas Piggin 2735e9e3d77SNicholas Piggin if (vmap_try_huge_p4d(p4d, addr, next, phys_addr, prot, 2745e9e3d77SNicholas Piggin max_page_shift)) { 2755e9e3d77SNicholas Piggin *mask |= PGTBL_P4D_MODIFIED; 2765e9e3d77SNicholas Piggin continue; 2775e9e3d77SNicholas Piggin } 2785e9e3d77SNicholas Piggin 2795e9e3d77SNicholas Piggin if (vmap_pud_range(p4d, addr, next, phys_addr, prot, 2805e9e3d77SNicholas Piggin max_page_shift, mask)) 2815e9e3d77SNicholas Piggin return -ENOMEM; 2825e9e3d77SNicholas Piggin } while (p4d++, phys_addr += (next - addr), addr = next, addr != end); 2835e9e3d77SNicholas Piggin return 0; 2845e9e3d77SNicholas Piggin } 2855e9e3d77SNicholas Piggin 2865d87510dSNicholas Piggin static int vmap_range_noflush(unsigned long addr, unsigned long end, 2875e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 2885e9e3d77SNicholas Piggin unsigned int max_page_shift) 2895e9e3d77SNicholas Piggin { 2905e9e3d77SNicholas Piggin pgd_t *pgd; 2915e9e3d77SNicholas Piggin unsigned long start; 2925e9e3d77SNicholas Piggin unsigned long next; 2935e9e3d77SNicholas Piggin int err; 2945e9e3d77SNicholas Piggin pgtbl_mod_mask mask = 0; 2955e9e3d77SNicholas Piggin 2965e9e3d77SNicholas Piggin might_sleep(); 2975e9e3d77SNicholas Piggin BUG_ON(addr >= end); 2985e9e3d77SNicholas Piggin 2995e9e3d77SNicholas Piggin start = addr; 3005e9e3d77SNicholas Piggin pgd = pgd_offset_k(addr); 3015e9e3d77SNicholas Piggin do { 3025e9e3d77SNicholas Piggin next = pgd_addr_end(addr, end); 3035e9e3d77SNicholas Piggin err = vmap_p4d_range(pgd, addr, next, phys_addr, prot, 3045e9e3d77SNicholas Piggin max_page_shift, &mask); 3055e9e3d77SNicholas Piggin if (err) 3065e9e3d77SNicholas Piggin break; 3075e9e3d77SNicholas Piggin } while (pgd++, phys_addr += (next - addr), addr = next, addr != end); 3085e9e3d77SNicholas Piggin 3095e9e3d77SNicholas Piggin if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 3105e9e3d77SNicholas Piggin arch_sync_kernel_mappings(start, end); 3115e9e3d77SNicholas Piggin 3125e9e3d77SNicholas Piggin return err; 3135e9e3d77SNicholas Piggin } 314b221385bSAdrian Bunk 31582a70ce0SChristoph Hellwig int ioremap_page_range(unsigned long addr, unsigned long end, 31682a70ce0SChristoph Hellwig phys_addr_t phys_addr, pgprot_t prot) 3175d87510dSNicholas Piggin { 3185d87510dSNicholas Piggin int err; 3195d87510dSNicholas Piggin 3208491502fSChristoph Hellwig err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot), 32182a70ce0SChristoph Hellwig ioremap_max_page_shift); 3225d87510dSNicholas Piggin flush_cache_vmap(addr, end); 3235d87510dSNicholas Piggin return err; 3245d87510dSNicholas Piggin } 3255d87510dSNicholas Piggin 3262ba3e694SJoerg Roedel static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 3272ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 3281da177e4SLinus Torvalds { 3291da177e4SLinus Torvalds pte_t *pte; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds pte = pte_offset_kernel(pmd, addr); 3321da177e4SLinus Torvalds do { 3331da177e4SLinus Torvalds pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte); 3341da177e4SLinus Torvalds WARN_ON(!pte_none(ptent) && !pte_present(ptent)); 3351da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 3362ba3e694SJoerg Roedel *mask |= PGTBL_PTE_MODIFIED; 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 3392ba3e694SJoerg Roedel static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end, 3402ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 3411da177e4SLinus Torvalds { 3421da177e4SLinus Torvalds pmd_t *pmd; 3431da177e4SLinus Torvalds unsigned long next; 3442ba3e694SJoerg Roedel int cleared; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 3471da177e4SLinus Torvalds do { 3481da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 3492ba3e694SJoerg Roedel 3502ba3e694SJoerg Roedel cleared = pmd_clear_huge(pmd); 3512ba3e694SJoerg Roedel if (cleared || pmd_bad(*pmd)) 3522ba3e694SJoerg Roedel *mask |= PGTBL_PMD_MODIFIED; 3532ba3e694SJoerg Roedel 3542ba3e694SJoerg Roedel if (cleared) 355b9820d8fSToshi Kani continue; 3561da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 3571da177e4SLinus Torvalds continue; 3582ba3e694SJoerg Roedel vunmap_pte_range(pmd, addr, next, mask); 359e47110e9SAneesh Kumar K.V 360e47110e9SAneesh Kumar K.V cond_resched(); 3611da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 3642ba3e694SJoerg Roedel static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, 3652ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 3661da177e4SLinus Torvalds { 3671da177e4SLinus Torvalds pud_t *pud; 3681da177e4SLinus Torvalds unsigned long next; 3692ba3e694SJoerg Roedel int cleared; 3701da177e4SLinus Torvalds 371c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 3721da177e4SLinus Torvalds do { 3731da177e4SLinus Torvalds next = pud_addr_end(addr, end); 3742ba3e694SJoerg Roedel 3752ba3e694SJoerg Roedel cleared = pud_clear_huge(pud); 3762ba3e694SJoerg Roedel if (cleared || pud_bad(*pud)) 3772ba3e694SJoerg Roedel *mask |= PGTBL_PUD_MODIFIED; 3782ba3e694SJoerg Roedel 3792ba3e694SJoerg Roedel if (cleared) 380b9820d8fSToshi Kani continue; 3811da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 3821da177e4SLinus Torvalds continue; 3832ba3e694SJoerg Roedel vunmap_pmd_range(pud, addr, next, mask); 3841da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 3872ba3e694SJoerg Roedel static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end, 3882ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 389c2febafcSKirill A. Shutemov { 390c2febafcSKirill A. Shutemov p4d_t *p4d; 391c2febafcSKirill A. Shutemov unsigned long next; 3922ba3e694SJoerg Roedel int cleared; 393c2febafcSKirill A. Shutemov 394c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 395c2febafcSKirill A. Shutemov do { 396c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 3972ba3e694SJoerg Roedel 3982ba3e694SJoerg Roedel cleared = p4d_clear_huge(p4d); 3992ba3e694SJoerg Roedel if (cleared || p4d_bad(*p4d)) 4002ba3e694SJoerg Roedel *mask |= PGTBL_P4D_MODIFIED; 4012ba3e694SJoerg Roedel 4022ba3e694SJoerg Roedel if (cleared) 403c2febafcSKirill A. Shutemov continue; 404c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 405c2febafcSKirill A. Shutemov continue; 4062ba3e694SJoerg Roedel vunmap_pud_range(p4d, addr, next, mask); 407c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 408c2febafcSKirill A. Shutemov } 409c2febafcSKirill A. Shutemov 4104ad0ae8cSNicholas Piggin /* 4114ad0ae8cSNicholas Piggin * vunmap_range_noflush is similar to vunmap_range, but does not 4124ad0ae8cSNicholas Piggin * flush caches or TLBs. 413b521c43fSChristoph Hellwig * 4144ad0ae8cSNicholas Piggin * The caller is responsible for calling flush_cache_vmap() before calling 4154ad0ae8cSNicholas Piggin * this function, and flush_tlb_kernel_range after it has returned 4164ad0ae8cSNicholas Piggin * successfully (and before the addresses are expected to cause a page fault 4174ad0ae8cSNicholas Piggin * or be re-mapped for something else, if TLB flushes are being delayed or 4184ad0ae8cSNicholas Piggin * coalesced). 419b521c43fSChristoph Hellwig * 4204ad0ae8cSNicholas Piggin * This is an internal function only. Do not use outside mm/. 421b521c43fSChristoph Hellwig */ 4224ad0ae8cSNicholas Piggin void vunmap_range_noflush(unsigned long start, unsigned long end) 4231da177e4SLinus Torvalds { 4241da177e4SLinus Torvalds unsigned long next; 425b521c43fSChristoph Hellwig pgd_t *pgd; 4262ba3e694SJoerg Roedel unsigned long addr = start; 4272ba3e694SJoerg Roedel pgtbl_mod_mask mask = 0; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds BUG_ON(addr >= end); 4301da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 4311da177e4SLinus Torvalds do { 4321da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 4332ba3e694SJoerg Roedel if (pgd_bad(*pgd)) 4342ba3e694SJoerg Roedel mask |= PGTBL_PGD_MODIFIED; 4351da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 4361da177e4SLinus Torvalds continue; 4372ba3e694SJoerg Roedel vunmap_p4d_range(pgd, addr, next, &mask); 4381da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 4392ba3e694SJoerg Roedel 4402ba3e694SJoerg Roedel if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 4412ba3e694SJoerg Roedel arch_sync_kernel_mappings(start, end); 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds 4444ad0ae8cSNicholas Piggin /** 4454ad0ae8cSNicholas Piggin * vunmap_range - unmap kernel virtual addresses 4464ad0ae8cSNicholas Piggin * @addr: start of the VM area to unmap 4474ad0ae8cSNicholas Piggin * @end: end of the VM area to unmap (non-inclusive) 4484ad0ae8cSNicholas Piggin * 4494ad0ae8cSNicholas Piggin * Clears any present PTEs in the virtual address range, flushes TLBs and 4504ad0ae8cSNicholas Piggin * caches. Any subsequent access to the address before it has been re-mapped 4514ad0ae8cSNicholas Piggin * is a kernel bug. 4524ad0ae8cSNicholas Piggin */ 4534ad0ae8cSNicholas Piggin void vunmap_range(unsigned long addr, unsigned long end) 4544ad0ae8cSNicholas Piggin { 4554ad0ae8cSNicholas Piggin flush_cache_vunmap(addr, end); 4564ad0ae8cSNicholas Piggin vunmap_range_noflush(addr, end); 4574ad0ae8cSNicholas Piggin flush_tlb_kernel_range(addr, end); 4584ad0ae8cSNicholas Piggin } 4594ad0ae8cSNicholas Piggin 4600a264884SNicholas Piggin static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr, 4612ba3e694SJoerg Roedel unsigned long end, pgprot_t prot, struct page **pages, int *nr, 4622ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 4631da177e4SLinus Torvalds { 4641da177e4SLinus Torvalds pte_t *pte; 4651da177e4SLinus Torvalds 466db64fe02SNick Piggin /* 467db64fe02SNick Piggin * nr is a running index into the array which helps higher level 468db64fe02SNick Piggin * callers keep track of where we're up to. 469db64fe02SNick Piggin */ 470db64fe02SNick Piggin 4712ba3e694SJoerg Roedel pte = pte_alloc_kernel_track(pmd, addr, mask); 4721da177e4SLinus Torvalds if (!pte) 4731da177e4SLinus Torvalds return -ENOMEM; 4741da177e4SLinus Torvalds do { 475db64fe02SNick Piggin struct page *page = pages[*nr]; 476db64fe02SNick Piggin 477db64fe02SNick Piggin if (WARN_ON(!pte_none(*pte))) 478db64fe02SNick Piggin return -EBUSY; 479db64fe02SNick Piggin if (WARN_ON(!page)) 4801da177e4SLinus Torvalds return -ENOMEM; 4811da177e4SLinus Torvalds set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); 482db64fe02SNick Piggin (*nr)++; 4831da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 4842ba3e694SJoerg Roedel *mask |= PGTBL_PTE_MODIFIED; 4851da177e4SLinus Torvalds return 0; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4880a264884SNicholas Piggin static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr, 4892ba3e694SJoerg Roedel unsigned long end, pgprot_t prot, struct page **pages, int *nr, 4902ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 4911da177e4SLinus Torvalds { 4921da177e4SLinus Torvalds pmd_t *pmd; 4931da177e4SLinus Torvalds unsigned long next; 4941da177e4SLinus Torvalds 4952ba3e694SJoerg Roedel pmd = pmd_alloc_track(&init_mm, pud, addr, mask); 4961da177e4SLinus Torvalds if (!pmd) 4971da177e4SLinus Torvalds return -ENOMEM; 4981da177e4SLinus Torvalds do { 4991da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 5000a264884SNicholas Piggin if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask)) 5011da177e4SLinus Torvalds return -ENOMEM; 5021da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 5031da177e4SLinus Torvalds return 0; 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 5060a264884SNicholas Piggin static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr, 5072ba3e694SJoerg Roedel unsigned long end, pgprot_t prot, struct page **pages, int *nr, 5082ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 5091da177e4SLinus Torvalds { 5101da177e4SLinus Torvalds pud_t *pud; 5111da177e4SLinus Torvalds unsigned long next; 5121da177e4SLinus Torvalds 5132ba3e694SJoerg Roedel pud = pud_alloc_track(&init_mm, p4d, addr, mask); 5141da177e4SLinus Torvalds if (!pud) 5151da177e4SLinus Torvalds return -ENOMEM; 5161da177e4SLinus Torvalds do { 5171da177e4SLinus Torvalds next = pud_addr_end(addr, end); 5180a264884SNicholas Piggin if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask)) 5191da177e4SLinus Torvalds return -ENOMEM; 5201da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 5211da177e4SLinus Torvalds return 0; 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 5240a264884SNicholas Piggin static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr, 5252ba3e694SJoerg Roedel unsigned long end, pgprot_t prot, struct page **pages, int *nr, 5262ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 527c2febafcSKirill A. Shutemov { 528c2febafcSKirill A. Shutemov p4d_t *p4d; 529c2febafcSKirill A. Shutemov unsigned long next; 530c2febafcSKirill A. Shutemov 5312ba3e694SJoerg Roedel p4d = p4d_alloc_track(&init_mm, pgd, addr, mask); 532c2febafcSKirill A. Shutemov if (!p4d) 533c2febafcSKirill A. Shutemov return -ENOMEM; 534c2febafcSKirill A. Shutemov do { 535c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 5360a264884SNicholas Piggin if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask)) 537c2febafcSKirill A. Shutemov return -ENOMEM; 538c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 539c2febafcSKirill A. Shutemov return 0; 540c2febafcSKirill A. Shutemov } 541c2febafcSKirill A. Shutemov 542121e6f32SNicholas Piggin static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, 543121e6f32SNicholas Piggin pgprot_t prot, struct page **pages) 544121e6f32SNicholas Piggin { 545121e6f32SNicholas Piggin unsigned long start = addr; 546121e6f32SNicholas Piggin pgd_t *pgd; 547121e6f32SNicholas Piggin unsigned long next; 548121e6f32SNicholas Piggin int err = 0; 549121e6f32SNicholas Piggin int nr = 0; 550121e6f32SNicholas Piggin pgtbl_mod_mask mask = 0; 551121e6f32SNicholas Piggin 552121e6f32SNicholas Piggin BUG_ON(addr >= end); 553121e6f32SNicholas Piggin pgd = pgd_offset_k(addr); 554121e6f32SNicholas Piggin do { 555121e6f32SNicholas Piggin next = pgd_addr_end(addr, end); 556121e6f32SNicholas Piggin if (pgd_bad(*pgd)) 557121e6f32SNicholas Piggin mask |= PGTBL_PGD_MODIFIED; 558121e6f32SNicholas Piggin err = vmap_pages_p4d_range(pgd, addr, next, prot, pages, &nr, &mask); 559121e6f32SNicholas Piggin if (err) 560121e6f32SNicholas Piggin return err; 561121e6f32SNicholas Piggin } while (pgd++, addr = next, addr != end); 562121e6f32SNicholas Piggin 563121e6f32SNicholas Piggin if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 564121e6f32SNicholas Piggin arch_sync_kernel_mappings(start, end); 565121e6f32SNicholas Piggin 566121e6f32SNicholas Piggin return 0; 567121e6f32SNicholas Piggin } 568121e6f32SNicholas Piggin 569b67177ecSNicholas Piggin /* 570b67177ecSNicholas Piggin * vmap_pages_range_noflush is similar to vmap_pages_range, but does not 571b67177ecSNicholas Piggin * flush caches. 572b67177ecSNicholas Piggin * 573b67177ecSNicholas Piggin * The caller is responsible for calling flush_cache_vmap() after this 574b67177ecSNicholas Piggin * function returns successfully and before the addresses are accessed. 575b67177ecSNicholas Piggin * 576b67177ecSNicholas Piggin * This is an internal function only. Do not use outside mm/. 577b67177ecSNicholas Piggin */ 578b67177ecSNicholas Piggin int vmap_pages_range_noflush(unsigned long addr, unsigned long end, 579121e6f32SNicholas Piggin pgprot_t prot, struct page **pages, unsigned int page_shift) 580121e6f32SNicholas Piggin { 581121e6f32SNicholas Piggin unsigned int i, nr = (end - addr) >> PAGE_SHIFT; 582121e6f32SNicholas Piggin 583121e6f32SNicholas Piggin WARN_ON(page_shift < PAGE_SHIFT); 584121e6f32SNicholas Piggin 585121e6f32SNicholas Piggin if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC) || 586121e6f32SNicholas Piggin page_shift == PAGE_SHIFT) 587121e6f32SNicholas Piggin return vmap_small_pages_range_noflush(addr, end, prot, pages); 588121e6f32SNicholas Piggin 589121e6f32SNicholas Piggin for (i = 0; i < nr; i += 1U << (page_shift - PAGE_SHIFT)) { 590121e6f32SNicholas Piggin int err; 591121e6f32SNicholas Piggin 592121e6f32SNicholas Piggin err = vmap_range_noflush(addr, addr + (1UL << page_shift), 593121e6f32SNicholas Piggin __pa(page_address(pages[i])), prot, 594121e6f32SNicholas Piggin page_shift); 595121e6f32SNicholas Piggin if (err) 596121e6f32SNicholas Piggin return err; 597121e6f32SNicholas Piggin 598121e6f32SNicholas Piggin addr += 1UL << page_shift; 599121e6f32SNicholas Piggin } 600121e6f32SNicholas Piggin 601121e6f32SNicholas Piggin return 0; 602121e6f32SNicholas Piggin } 603121e6f32SNicholas Piggin 604b67177ecSNicholas Piggin /** 605b67177ecSNicholas Piggin * vmap_pages_range - map pages to a kernel virtual address 606b67177ecSNicholas Piggin * @addr: start of the VM area to map 607b67177ecSNicholas Piggin * @end: end of the VM area to map (non-inclusive) 608b67177ecSNicholas Piggin * @prot: page protection flags to use 609b67177ecSNicholas Piggin * @pages: pages to map (always PAGE_SIZE pages) 610b67177ecSNicholas Piggin * @page_shift: maximum shift that the pages may be mapped with, @pages must 611b67177ecSNicholas Piggin * be aligned and contiguous up to at least this shift. 612b67177ecSNicholas Piggin * 613b67177ecSNicholas Piggin * RETURNS: 614b67177ecSNicholas Piggin * 0 on success, -errno on failure. 615b67177ecSNicholas Piggin */ 616121e6f32SNicholas Piggin static int vmap_pages_range(unsigned long addr, unsigned long end, 617121e6f32SNicholas Piggin pgprot_t prot, struct page **pages, unsigned int page_shift) 618121e6f32SNicholas Piggin { 619121e6f32SNicholas Piggin int err; 620121e6f32SNicholas Piggin 621121e6f32SNicholas Piggin err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift); 622121e6f32SNicholas Piggin flush_cache_vmap(addr, end); 623121e6f32SNicholas Piggin return err; 624121e6f32SNicholas Piggin } 625121e6f32SNicholas Piggin 62681ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x) 62773bdf0a6SLinus Torvalds { 62873bdf0a6SLinus Torvalds /* 629ab4f2ee1SRussell King * ARM, x86-64 and sparc64 put modules in a special place, 63073bdf0a6SLinus Torvalds * and fall back on vmalloc() if that fails. Others 63173bdf0a6SLinus Torvalds * just put it in the vmalloc space. 63273bdf0a6SLinus Torvalds */ 63373bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) 63473bdf0a6SLinus Torvalds unsigned long addr = (unsigned long)x; 63573bdf0a6SLinus Torvalds if (addr >= MODULES_VADDR && addr < MODULES_END) 63673bdf0a6SLinus Torvalds return 1; 63773bdf0a6SLinus Torvalds #endif 63873bdf0a6SLinus Torvalds return is_vmalloc_addr(x); 63973bdf0a6SLinus Torvalds } 64073bdf0a6SLinus Torvalds 64148667e7aSChristoph Lameter /* 642c0eb315aSNicholas Piggin * Walk a vmap address to the struct page it maps. Huge vmap mappings will 643c0eb315aSNicholas Piggin * return the tail page that corresponds to the base page address, which 644c0eb315aSNicholas Piggin * matches small vmap mappings. 64548667e7aSChristoph Lameter */ 646add688fbSmalc struct page *vmalloc_to_page(const void *vmalloc_addr) 64748667e7aSChristoph Lameter { 64848667e7aSChristoph Lameter unsigned long addr = (unsigned long) vmalloc_addr; 649add688fbSmalc struct page *page = NULL; 65048667e7aSChristoph Lameter pgd_t *pgd = pgd_offset_k(addr); 651c2febafcSKirill A. Shutemov p4d_t *p4d; 652c2febafcSKirill A. Shutemov pud_t *pud; 653c2febafcSKirill A. Shutemov pmd_t *pmd; 654c2febafcSKirill A. Shutemov pte_t *ptep, pte; 65548667e7aSChristoph Lameter 6567aa413deSIngo Molnar /* 6577aa413deSIngo Molnar * XXX we might need to change this if we add VIRTUAL_BUG_ON for 6587aa413deSIngo Molnar * architectures that do not vmalloc module space 6597aa413deSIngo Molnar */ 66073bdf0a6SLinus Torvalds VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); 66159ea7463SJiri Slaby 662c2febafcSKirill A. Shutemov if (pgd_none(*pgd)) 663c2febafcSKirill A. Shutemov return NULL; 664c0eb315aSNicholas Piggin if (WARN_ON_ONCE(pgd_leaf(*pgd))) 665c0eb315aSNicholas Piggin return NULL; /* XXX: no allowance for huge pgd */ 666c0eb315aSNicholas Piggin if (WARN_ON_ONCE(pgd_bad(*pgd))) 667c0eb315aSNicholas Piggin return NULL; 668c0eb315aSNicholas Piggin 669c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 670c2febafcSKirill A. Shutemov if (p4d_none(*p4d)) 671c2febafcSKirill A. Shutemov return NULL; 672c0eb315aSNicholas Piggin if (p4d_leaf(*p4d)) 673c0eb315aSNicholas Piggin return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT); 674c0eb315aSNicholas Piggin if (WARN_ON_ONCE(p4d_bad(*p4d))) 675c2febafcSKirill A. Shutemov return NULL; 676c0eb315aSNicholas Piggin 677c0eb315aSNicholas Piggin pud = pud_offset(p4d, addr); 678c0eb315aSNicholas Piggin if (pud_none(*pud)) 679c0eb315aSNicholas Piggin return NULL; 680c0eb315aSNicholas Piggin if (pud_leaf(*pud)) 681c0eb315aSNicholas Piggin return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); 682c0eb315aSNicholas Piggin if (WARN_ON_ONCE(pud_bad(*pud))) 683c0eb315aSNicholas Piggin return NULL; 684c0eb315aSNicholas Piggin 685c2febafcSKirill A. Shutemov pmd = pmd_offset(pud, addr); 686c0eb315aSNicholas Piggin if (pmd_none(*pmd)) 687c0eb315aSNicholas Piggin return NULL; 688c0eb315aSNicholas Piggin if (pmd_leaf(*pmd)) 689c0eb315aSNicholas Piggin return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); 690c0eb315aSNicholas Piggin if (WARN_ON_ONCE(pmd_bad(*pmd))) 691c2febafcSKirill A. Shutemov return NULL; 692db64fe02SNick Piggin 69348667e7aSChristoph Lameter ptep = pte_offset_map(pmd, addr); 69448667e7aSChristoph Lameter pte = *ptep; 69548667e7aSChristoph Lameter if (pte_present(pte)) 696add688fbSmalc page = pte_page(pte); 69748667e7aSChristoph Lameter pte_unmap(ptep); 698c0eb315aSNicholas Piggin 699add688fbSmalc return page; 700ece86e22SJianyu Zhan } 701ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page); 702ece86e22SJianyu Zhan 703add688fbSmalc /* 704add688fbSmalc * Map a vmalloc()-space virtual address to the physical page frame number. 705add688fbSmalc */ 706add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr) 707add688fbSmalc { 708add688fbSmalc return page_to_pfn(vmalloc_to_page(vmalloc_addr)); 709add688fbSmalc } 710add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn); 711add688fbSmalc 712db64fe02SNick Piggin 713db64fe02SNick Piggin /*** Global kva allocator ***/ 714db64fe02SNick Piggin 715bb850f4dSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_PROPAGATE_CHECK 0 716a6cf4e0fSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0 717bb850f4dSUladzislau Rezki (Sony) 718db64fe02SNick Piggin 719db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock); 720e36176beSUladzislau Rezki (Sony) static DEFINE_SPINLOCK(free_vmap_area_lock); 721f1c4069eSJoonsoo Kim /* Export for kexec only */ 722f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list); 72389699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT; 72468ad4a33SUladzislau Rezki (Sony) static bool vmap_initialized __read_mostly; 72589699605SNick Piggin 72696e2db45SUladzislau Rezki (Sony) static struct rb_root purge_vmap_area_root = RB_ROOT; 72796e2db45SUladzislau Rezki (Sony) static LIST_HEAD(purge_vmap_area_list); 72896e2db45SUladzislau Rezki (Sony) static DEFINE_SPINLOCK(purge_vmap_area_lock); 72996e2db45SUladzislau Rezki (Sony) 73068ad4a33SUladzislau Rezki (Sony) /* 73168ad4a33SUladzislau Rezki (Sony) * This kmem_cache is used for vmap_area objects. Instead of 73268ad4a33SUladzislau Rezki (Sony) * allocating from slab we reuse an object from this cache to 73368ad4a33SUladzislau Rezki (Sony) * make things faster. Especially in "no edge" splitting of 73468ad4a33SUladzislau Rezki (Sony) * free block. 73568ad4a33SUladzislau Rezki (Sony) */ 73668ad4a33SUladzislau Rezki (Sony) static struct kmem_cache *vmap_area_cachep; 73789699605SNick Piggin 73868ad4a33SUladzislau Rezki (Sony) /* 73968ad4a33SUladzislau Rezki (Sony) * This linked list is used in pair with free_vmap_area_root. 74068ad4a33SUladzislau Rezki (Sony) * It gives O(1) access to prev/next to perform fast coalescing. 74168ad4a33SUladzislau Rezki (Sony) */ 74268ad4a33SUladzislau Rezki (Sony) static LIST_HEAD(free_vmap_area_list); 74368ad4a33SUladzislau Rezki (Sony) 74468ad4a33SUladzislau Rezki (Sony) /* 74568ad4a33SUladzislau Rezki (Sony) * This augment red-black tree represents the free vmap space. 74668ad4a33SUladzislau Rezki (Sony) * All vmap_area objects in this tree are sorted by va->va_start 74768ad4a33SUladzislau Rezki (Sony) * address. It is used for allocation and merging when a vmap 74868ad4a33SUladzislau Rezki (Sony) * object is released. 74968ad4a33SUladzislau Rezki (Sony) * 75068ad4a33SUladzislau Rezki (Sony) * Each vmap_area node contains a maximum available free block 75168ad4a33SUladzislau Rezki (Sony) * of its sub-tree, right or left. Therefore it is possible to 75268ad4a33SUladzislau Rezki (Sony) * find a lowest match of free area. 75368ad4a33SUladzislau Rezki (Sony) */ 75468ad4a33SUladzislau Rezki (Sony) static struct rb_root free_vmap_area_root = RB_ROOT; 75568ad4a33SUladzislau Rezki (Sony) 75682dd23e8SUladzislau Rezki (Sony) /* 75782dd23e8SUladzislau Rezki (Sony) * Preload a CPU with one object for "no edge" split case. The 75882dd23e8SUladzislau Rezki (Sony) * aim is to get rid of allocations from the atomic context, thus 75982dd23e8SUladzislau Rezki (Sony) * to use more permissive allocation masks. 76082dd23e8SUladzislau Rezki (Sony) */ 76182dd23e8SUladzislau Rezki (Sony) static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node); 76282dd23e8SUladzislau Rezki (Sony) 76368ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 76468ad4a33SUladzislau Rezki (Sony) va_size(struct vmap_area *va) 76568ad4a33SUladzislau Rezki (Sony) { 76668ad4a33SUladzislau Rezki (Sony) return (va->va_end - va->va_start); 76768ad4a33SUladzislau Rezki (Sony) } 76868ad4a33SUladzislau Rezki (Sony) 76968ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 77068ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(struct rb_node *node) 77168ad4a33SUladzislau Rezki (Sony) { 77268ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 77368ad4a33SUladzislau Rezki (Sony) 77468ad4a33SUladzislau Rezki (Sony) va = rb_entry_safe(node, struct vmap_area, rb_node); 77568ad4a33SUladzislau Rezki (Sony) return va ? va->subtree_max_size : 0; 77668ad4a33SUladzislau Rezki (Sony) } 77768ad4a33SUladzislau Rezki (Sony) 77868ad4a33SUladzislau Rezki (Sony) /* 77968ad4a33SUladzislau Rezki (Sony) * Gets called when remove the node and rotate. 78068ad4a33SUladzislau Rezki (Sony) */ 78168ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 78268ad4a33SUladzislau Rezki (Sony) compute_subtree_max_size(struct vmap_area *va) 78368ad4a33SUladzislau Rezki (Sony) { 78468ad4a33SUladzislau Rezki (Sony) return max3(va_size(va), 78568ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(va->rb_node.rb_left), 78668ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(va->rb_node.rb_right)); 78768ad4a33SUladzislau Rezki (Sony) } 78868ad4a33SUladzislau Rezki (Sony) 789315cc066SMichel Lespinasse RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb, 790315cc066SMichel Lespinasse struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size) 79168ad4a33SUladzislau Rezki (Sony) 79268ad4a33SUladzislau Rezki (Sony) static void purge_vmap_area_lazy(void); 79368ad4a33SUladzislau Rezki (Sony) static BLOCKING_NOTIFIER_HEAD(vmap_notify_list); 794690467c8SUladzislau Rezki (Sony) static void drain_vmap_area_work(struct work_struct *work); 795690467c8SUladzislau Rezki (Sony) static DECLARE_WORK(drain_vmap_work, drain_vmap_area_work); 796db64fe02SNick Piggin 79797105f0aSRoman Gushchin static atomic_long_t nr_vmalloc_pages; 79897105f0aSRoman Gushchin 79997105f0aSRoman Gushchin unsigned long vmalloc_nr_pages(void) 80097105f0aSRoman Gushchin { 80197105f0aSRoman Gushchin return atomic_long_read(&nr_vmalloc_pages); 80297105f0aSRoman Gushchin } 80397105f0aSRoman Gushchin 804f181234aSChen Wandun static struct vmap_area *find_vmap_area_exceed_addr(unsigned long addr) 805f181234aSChen Wandun { 806f181234aSChen Wandun struct vmap_area *va = NULL; 807f181234aSChen Wandun struct rb_node *n = vmap_area_root.rb_node; 808f181234aSChen Wandun 809f181234aSChen Wandun while (n) { 810f181234aSChen Wandun struct vmap_area *tmp; 811f181234aSChen Wandun 812f181234aSChen Wandun tmp = rb_entry(n, struct vmap_area, rb_node); 813f181234aSChen Wandun if (tmp->va_end > addr) { 814f181234aSChen Wandun va = tmp; 815f181234aSChen Wandun if (tmp->va_start <= addr) 816f181234aSChen Wandun break; 817f181234aSChen Wandun 818f181234aSChen Wandun n = n->rb_left; 819f181234aSChen Wandun } else 820f181234aSChen Wandun n = n->rb_right; 821f181234aSChen Wandun } 822f181234aSChen Wandun 823f181234aSChen Wandun return va; 824f181234aSChen Wandun } 825f181234aSChen Wandun 826db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr) 8271da177e4SLinus Torvalds { 828db64fe02SNick Piggin struct rb_node *n = vmap_area_root.rb_node; 829db64fe02SNick Piggin 830db64fe02SNick Piggin while (n) { 831db64fe02SNick Piggin struct vmap_area *va; 832db64fe02SNick Piggin 833db64fe02SNick Piggin va = rb_entry(n, struct vmap_area, rb_node); 834db64fe02SNick Piggin if (addr < va->va_start) 835db64fe02SNick Piggin n = n->rb_left; 836cef2ac3fSHATAYAMA Daisuke else if (addr >= va->va_end) 837db64fe02SNick Piggin n = n->rb_right; 838db64fe02SNick Piggin else 839db64fe02SNick Piggin return va; 840db64fe02SNick Piggin } 841db64fe02SNick Piggin 842db64fe02SNick Piggin return NULL; 843db64fe02SNick Piggin } 844db64fe02SNick Piggin 84568ad4a33SUladzislau Rezki (Sony) /* 84668ad4a33SUladzislau Rezki (Sony) * This function returns back addresses of parent node 84768ad4a33SUladzislau Rezki (Sony) * and its left or right link for further processing. 8489c801f61SUladzislau Rezki (Sony) * 8499c801f61SUladzislau Rezki (Sony) * Otherwise NULL is returned. In that case all further 8509c801f61SUladzislau Rezki (Sony) * steps regarding inserting of conflicting overlap range 8519c801f61SUladzislau Rezki (Sony) * have to be declined and actually considered as a bug. 85268ad4a33SUladzislau Rezki (Sony) */ 85368ad4a33SUladzislau Rezki (Sony) static __always_inline struct rb_node ** 85468ad4a33SUladzislau Rezki (Sony) find_va_links(struct vmap_area *va, 85568ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct rb_node *from, 85668ad4a33SUladzislau Rezki (Sony) struct rb_node **parent) 857db64fe02SNick Piggin { 858170168d0SNamhyung Kim struct vmap_area *tmp_va; 85968ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 860db64fe02SNick Piggin 86168ad4a33SUladzislau Rezki (Sony) if (root) { 86268ad4a33SUladzislau Rezki (Sony) link = &root->rb_node; 86368ad4a33SUladzislau Rezki (Sony) if (unlikely(!*link)) { 86468ad4a33SUladzislau Rezki (Sony) *parent = NULL; 86568ad4a33SUladzislau Rezki (Sony) return link; 86668ad4a33SUladzislau Rezki (Sony) } 86768ad4a33SUladzislau Rezki (Sony) } else { 86868ad4a33SUladzislau Rezki (Sony) link = &from; 86968ad4a33SUladzislau Rezki (Sony) } 87068ad4a33SUladzislau Rezki (Sony) 87168ad4a33SUladzislau Rezki (Sony) /* 87268ad4a33SUladzislau Rezki (Sony) * Go to the bottom of the tree. When we hit the last point 87368ad4a33SUladzislau Rezki (Sony) * we end up with parent rb_node and correct direction, i name 87468ad4a33SUladzislau Rezki (Sony) * it link, where the new va->rb_node will be attached to. 87568ad4a33SUladzislau Rezki (Sony) */ 87668ad4a33SUladzislau Rezki (Sony) do { 87768ad4a33SUladzislau Rezki (Sony) tmp_va = rb_entry(*link, struct vmap_area, rb_node); 87868ad4a33SUladzislau Rezki (Sony) 87968ad4a33SUladzislau Rezki (Sony) /* 88068ad4a33SUladzislau Rezki (Sony) * During the traversal we also do some sanity check. 88168ad4a33SUladzislau Rezki (Sony) * Trigger the BUG() if there are sides(left/right) 88268ad4a33SUladzislau Rezki (Sony) * or full overlaps. 88368ad4a33SUladzislau Rezki (Sony) */ 88468ad4a33SUladzislau Rezki (Sony) if (va->va_start < tmp_va->va_end && 88568ad4a33SUladzislau Rezki (Sony) va->va_end <= tmp_va->va_start) 88668ad4a33SUladzislau Rezki (Sony) link = &(*link)->rb_left; 88768ad4a33SUladzislau Rezki (Sony) else if (va->va_end > tmp_va->va_start && 88868ad4a33SUladzislau Rezki (Sony) va->va_start >= tmp_va->va_end) 88968ad4a33SUladzislau Rezki (Sony) link = &(*link)->rb_right; 8909c801f61SUladzislau Rezki (Sony) else { 8919c801f61SUladzislau Rezki (Sony) WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n", 8929c801f61SUladzislau Rezki (Sony) va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end); 8939c801f61SUladzislau Rezki (Sony) 8949c801f61SUladzislau Rezki (Sony) return NULL; 8959c801f61SUladzislau Rezki (Sony) } 89668ad4a33SUladzislau Rezki (Sony) } while (*link); 89768ad4a33SUladzislau Rezki (Sony) 89868ad4a33SUladzislau Rezki (Sony) *parent = &tmp_va->rb_node; 89968ad4a33SUladzislau Rezki (Sony) return link; 900db64fe02SNick Piggin } 901db64fe02SNick Piggin 90268ad4a33SUladzislau Rezki (Sony) static __always_inline struct list_head * 90368ad4a33SUladzislau Rezki (Sony) get_va_next_sibling(struct rb_node *parent, struct rb_node **link) 90468ad4a33SUladzislau Rezki (Sony) { 90568ad4a33SUladzislau Rezki (Sony) struct list_head *list; 906db64fe02SNick Piggin 90768ad4a33SUladzislau Rezki (Sony) if (unlikely(!parent)) 90868ad4a33SUladzislau Rezki (Sony) /* 90968ad4a33SUladzislau Rezki (Sony) * The red-black tree where we try to find VA neighbors 91068ad4a33SUladzislau Rezki (Sony) * before merging or inserting is empty, i.e. it means 91168ad4a33SUladzislau Rezki (Sony) * there is no free vmap space. Normally it does not 91268ad4a33SUladzislau Rezki (Sony) * happen but we handle this case anyway. 91368ad4a33SUladzislau Rezki (Sony) */ 91468ad4a33SUladzislau Rezki (Sony) return NULL; 91568ad4a33SUladzislau Rezki (Sony) 91668ad4a33SUladzislau Rezki (Sony) list = &rb_entry(parent, struct vmap_area, rb_node)->list; 91768ad4a33SUladzislau Rezki (Sony) return (&parent->rb_right == link ? list->next : list); 918db64fe02SNick Piggin } 919db64fe02SNick Piggin 92068ad4a33SUladzislau Rezki (Sony) static __always_inline void 92168ad4a33SUladzislau Rezki (Sony) link_va(struct vmap_area *va, struct rb_root *root, 92268ad4a33SUladzislau Rezki (Sony) struct rb_node *parent, struct rb_node **link, struct list_head *head) 92368ad4a33SUladzislau Rezki (Sony) { 92468ad4a33SUladzislau Rezki (Sony) /* 92568ad4a33SUladzislau Rezki (Sony) * VA is still not in the list, but we can 92668ad4a33SUladzislau Rezki (Sony) * identify its future previous list_head node. 92768ad4a33SUladzislau Rezki (Sony) */ 92868ad4a33SUladzislau Rezki (Sony) if (likely(parent)) { 92968ad4a33SUladzislau Rezki (Sony) head = &rb_entry(parent, struct vmap_area, rb_node)->list; 93068ad4a33SUladzislau Rezki (Sony) if (&parent->rb_right != link) 93168ad4a33SUladzislau Rezki (Sony) head = head->prev; 93268ad4a33SUladzislau Rezki (Sony) } 933db64fe02SNick Piggin 93468ad4a33SUladzislau Rezki (Sony) /* Insert to the rb-tree */ 93568ad4a33SUladzislau Rezki (Sony) rb_link_node(&va->rb_node, parent, link); 93668ad4a33SUladzislau Rezki (Sony) if (root == &free_vmap_area_root) { 93768ad4a33SUladzislau Rezki (Sony) /* 93868ad4a33SUladzislau Rezki (Sony) * Some explanation here. Just perform simple insertion 93968ad4a33SUladzislau Rezki (Sony) * to the tree. We do not set va->subtree_max_size to 94068ad4a33SUladzislau Rezki (Sony) * its current size before calling rb_insert_augmented(). 94168ad4a33SUladzislau Rezki (Sony) * It is because of we populate the tree from the bottom 94268ad4a33SUladzislau Rezki (Sony) * to parent levels when the node _is_ in the tree. 94368ad4a33SUladzislau Rezki (Sony) * 94468ad4a33SUladzislau Rezki (Sony) * Therefore we set subtree_max_size to zero after insertion, 94568ad4a33SUladzislau Rezki (Sony) * to let __augment_tree_propagate_from() puts everything to 94668ad4a33SUladzislau Rezki (Sony) * the correct order later on. 94768ad4a33SUladzislau Rezki (Sony) */ 94868ad4a33SUladzislau Rezki (Sony) rb_insert_augmented(&va->rb_node, 94968ad4a33SUladzislau Rezki (Sony) root, &free_vmap_area_rb_augment_cb); 95068ad4a33SUladzislau Rezki (Sony) va->subtree_max_size = 0; 95168ad4a33SUladzislau Rezki (Sony) } else { 95268ad4a33SUladzislau Rezki (Sony) rb_insert_color(&va->rb_node, root); 95368ad4a33SUladzislau Rezki (Sony) } 95468ad4a33SUladzislau Rezki (Sony) 95568ad4a33SUladzislau Rezki (Sony) /* Address-sort this list */ 95668ad4a33SUladzislau Rezki (Sony) list_add(&va->list, head); 95768ad4a33SUladzislau Rezki (Sony) } 95868ad4a33SUladzislau Rezki (Sony) 95968ad4a33SUladzislau Rezki (Sony) static __always_inline void 96068ad4a33SUladzislau Rezki (Sony) unlink_va(struct vmap_area *va, struct rb_root *root) 96168ad4a33SUladzislau Rezki (Sony) { 962460e42d1SUladzislau Rezki (Sony) if (WARN_ON(RB_EMPTY_NODE(&va->rb_node))) 963460e42d1SUladzislau Rezki (Sony) return; 964460e42d1SUladzislau Rezki (Sony) 96568ad4a33SUladzislau Rezki (Sony) if (root == &free_vmap_area_root) 96668ad4a33SUladzislau Rezki (Sony) rb_erase_augmented(&va->rb_node, 96768ad4a33SUladzislau Rezki (Sony) root, &free_vmap_area_rb_augment_cb); 96868ad4a33SUladzislau Rezki (Sony) else 96968ad4a33SUladzislau Rezki (Sony) rb_erase(&va->rb_node, root); 97068ad4a33SUladzislau Rezki (Sony) 97168ad4a33SUladzislau Rezki (Sony) list_del(&va->list); 97268ad4a33SUladzislau Rezki (Sony) RB_CLEAR_NODE(&va->rb_node); 97368ad4a33SUladzislau Rezki (Sony) } 97468ad4a33SUladzislau Rezki (Sony) 975bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK 976bb850f4dSUladzislau Rezki (Sony) static void 977da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(void) 978bb850f4dSUladzislau Rezki (Sony) { 979bb850f4dSUladzislau Rezki (Sony) struct vmap_area *va; 980da27c9edSUladzislau Rezki (Sony) unsigned long computed_size; 981bb850f4dSUladzislau Rezki (Sony) 982da27c9edSUladzislau Rezki (Sony) list_for_each_entry(va, &free_vmap_area_list, list) { 983da27c9edSUladzislau Rezki (Sony) computed_size = compute_subtree_max_size(va); 984da27c9edSUladzislau Rezki (Sony) if (computed_size != va->subtree_max_size) 985bb850f4dSUladzislau Rezki (Sony) pr_emerg("tree is corrupted: %lu, %lu\n", 986bb850f4dSUladzislau Rezki (Sony) va_size(va), va->subtree_max_size); 987bb850f4dSUladzislau Rezki (Sony) } 988bb850f4dSUladzislau Rezki (Sony) } 989bb850f4dSUladzislau Rezki (Sony) #endif 990bb850f4dSUladzislau Rezki (Sony) 99168ad4a33SUladzislau Rezki (Sony) /* 99268ad4a33SUladzislau Rezki (Sony) * This function populates subtree_max_size from bottom to upper 99368ad4a33SUladzislau Rezki (Sony) * levels starting from VA point. The propagation must be done 99468ad4a33SUladzislau Rezki (Sony) * when VA size is modified by changing its va_start/va_end. Or 99568ad4a33SUladzislau Rezki (Sony) * in case of newly inserting of VA to the tree. 99668ad4a33SUladzislau Rezki (Sony) * 99768ad4a33SUladzislau Rezki (Sony) * It means that __augment_tree_propagate_from() must be called: 99868ad4a33SUladzislau Rezki (Sony) * - After VA has been inserted to the tree(free path); 99968ad4a33SUladzislau Rezki (Sony) * - After VA has been shrunk(allocation path); 100068ad4a33SUladzislau Rezki (Sony) * - After VA has been increased(merging path). 100168ad4a33SUladzislau Rezki (Sony) * 100268ad4a33SUladzislau Rezki (Sony) * Please note that, it does not mean that upper parent nodes 100368ad4a33SUladzislau Rezki (Sony) * and their subtree_max_size are recalculated all the time up 100468ad4a33SUladzislau Rezki (Sony) * to the root node. 100568ad4a33SUladzislau Rezki (Sony) * 100668ad4a33SUladzislau Rezki (Sony) * 4--8 100768ad4a33SUladzislau Rezki (Sony) * /\ 100868ad4a33SUladzislau Rezki (Sony) * / \ 100968ad4a33SUladzislau Rezki (Sony) * / \ 101068ad4a33SUladzislau Rezki (Sony) * 2--2 8--8 101168ad4a33SUladzislau Rezki (Sony) * 101268ad4a33SUladzislau Rezki (Sony) * For example if we modify the node 4, shrinking it to 2, then 101368ad4a33SUladzislau Rezki (Sony) * no any modification is required. If we shrink the node 2 to 1 101468ad4a33SUladzislau Rezki (Sony) * its subtree_max_size is updated only, and set to 1. If we shrink 101568ad4a33SUladzislau Rezki (Sony) * the node 8 to 6, then its subtree_max_size is set to 6 and parent 101668ad4a33SUladzislau Rezki (Sony) * node becomes 4--6. 101768ad4a33SUladzislau Rezki (Sony) */ 101868ad4a33SUladzislau Rezki (Sony) static __always_inline void 101968ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(struct vmap_area *va) 102068ad4a33SUladzislau Rezki (Sony) { 102168ad4a33SUladzislau Rezki (Sony) /* 102215ae144fSUladzislau Rezki (Sony) * Populate the tree from bottom towards the root until 102315ae144fSUladzislau Rezki (Sony) * the calculated maximum available size of checked node 102415ae144fSUladzislau Rezki (Sony) * is equal to its current one. 102568ad4a33SUladzislau Rezki (Sony) */ 102615ae144fSUladzislau Rezki (Sony) free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL); 1027bb850f4dSUladzislau Rezki (Sony) 1028bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK 1029da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(); 1030bb850f4dSUladzislau Rezki (Sony) #endif 103168ad4a33SUladzislau Rezki (Sony) } 103268ad4a33SUladzislau Rezki (Sony) 103368ad4a33SUladzislau Rezki (Sony) static void 103468ad4a33SUladzislau Rezki (Sony) insert_vmap_area(struct vmap_area *va, 103568ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 103668ad4a33SUladzislau Rezki (Sony) { 103768ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 103868ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 103968ad4a33SUladzislau Rezki (Sony) 104068ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 10419c801f61SUladzislau Rezki (Sony) if (link) 104268ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 104368ad4a33SUladzislau Rezki (Sony) } 104468ad4a33SUladzislau Rezki (Sony) 104568ad4a33SUladzislau Rezki (Sony) static void 104668ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(struct vmap_area *va, 104768ad4a33SUladzislau Rezki (Sony) struct rb_node *from, struct rb_root *root, 104868ad4a33SUladzislau Rezki (Sony) struct list_head *head) 104968ad4a33SUladzislau Rezki (Sony) { 105068ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 105168ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 105268ad4a33SUladzislau Rezki (Sony) 105368ad4a33SUladzislau Rezki (Sony) if (from) 105468ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, NULL, from, &parent); 105568ad4a33SUladzislau Rezki (Sony) else 105668ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 105768ad4a33SUladzislau Rezki (Sony) 10589c801f61SUladzislau Rezki (Sony) if (link) { 105968ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 106068ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 106168ad4a33SUladzislau Rezki (Sony) } 10629c801f61SUladzislau Rezki (Sony) } 106368ad4a33SUladzislau Rezki (Sony) 106468ad4a33SUladzislau Rezki (Sony) /* 106568ad4a33SUladzislau Rezki (Sony) * Merge de-allocated chunk of VA memory with previous 106668ad4a33SUladzislau Rezki (Sony) * and next free blocks. If coalesce is not done a new 106768ad4a33SUladzislau Rezki (Sony) * free area is inserted. If VA has been merged, it is 106868ad4a33SUladzislau Rezki (Sony) * freed. 10699c801f61SUladzislau Rezki (Sony) * 10709c801f61SUladzislau Rezki (Sony) * Please note, it can return NULL in case of overlap 10719c801f61SUladzislau Rezki (Sony) * ranges, followed by WARN() report. Despite it is a 10729c801f61SUladzislau Rezki (Sony) * buggy behaviour, a system can be alive and keep 10739c801f61SUladzislau Rezki (Sony) * ongoing. 107468ad4a33SUladzislau Rezki (Sony) */ 10753c5c3cfbSDaniel Axtens static __always_inline struct vmap_area * 107668ad4a33SUladzislau Rezki (Sony) merge_or_add_vmap_area(struct vmap_area *va, 107768ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 107868ad4a33SUladzislau Rezki (Sony) { 107968ad4a33SUladzislau Rezki (Sony) struct vmap_area *sibling; 108068ad4a33SUladzislau Rezki (Sony) struct list_head *next; 108168ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 108268ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 108368ad4a33SUladzislau Rezki (Sony) bool merged = false; 108468ad4a33SUladzislau Rezki (Sony) 108568ad4a33SUladzislau Rezki (Sony) /* 108668ad4a33SUladzislau Rezki (Sony) * Find a place in the tree where VA potentially will be 108768ad4a33SUladzislau Rezki (Sony) * inserted, unless it is merged with its sibling/siblings. 108868ad4a33SUladzislau Rezki (Sony) */ 108968ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 10909c801f61SUladzislau Rezki (Sony) if (!link) 10919c801f61SUladzislau Rezki (Sony) return NULL; 109268ad4a33SUladzislau Rezki (Sony) 109368ad4a33SUladzislau Rezki (Sony) /* 109468ad4a33SUladzislau Rezki (Sony) * Get next node of VA to check if merging can be done. 109568ad4a33SUladzislau Rezki (Sony) */ 109668ad4a33SUladzislau Rezki (Sony) next = get_va_next_sibling(parent, link); 109768ad4a33SUladzislau Rezki (Sony) if (unlikely(next == NULL)) 109868ad4a33SUladzislau Rezki (Sony) goto insert; 109968ad4a33SUladzislau Rezki (Sony) 110068ad4a33SUladzislau Rezki (Sony) /* 110168ad4a33SUladzislau Rezki (Sony) * start end 110268ad4a33SUladzislau Rezki (Sony) * | | 110368ad4a33SUladzislau Rezki (Sony) * |<------VA------>|<-----Next----->| 110468ad4a33SUladzislau Rezki (Sony) * | | 110568ad4a33SUladzislau Rezki (Sony) * start end 110668ad4a33SUladzislau Rezki (Sony) */ 110768ad4a33SUladzislau Rezki (Sony) if (next != head) { 110868ad4a33SUladzislau Rezki (Sony) sibling = list_entry(next, struct vmap_area, list); 110968ad4a33SUladzislau Rezki (Sony) if (sibling->va_start == va->va_end) { 111068ad4a33SUladzislau Rezki (Sony) sibling->va_start = va->va_start; 111168ad4a33SUladzislau Rezki (Sony) 111268ad4a33SUladzislau Rezki (Sony) /* Free vmap_area object. */ 111368ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 111468ad4a33SUladzislau Rezki (Sony) 111568ad4a33SUladzislau Rezki (Sony) /* Point to the new merged area. */ 111668ad4a33SUladzislau Rezki (Sony) va = sibling; 111768ad4a33SUladzislau Rezki (Sony) merged = true; 111868ad4a33SUladzislau Rezki (Sony) } 111968ad4a33SUladzislau Rezki (Sony) } 112068ad4a33SUladzislau Rezki (Sony) 112168ad4a33SUladzislau Rezki (Sony) /* 112268ad4a33SUladzislau Rezki (Sony) * start end 112368ad4a33SUladzislau Rezki (Sony) * | | 112468ad4a33SUladzislau Rezki (Sony) * |<-----Prev----->|<------VA------>| 112568ad4a33SUladzislau Rezki (Sony) * | | 112668ad4a33SUladzislau Rezki (Sony) * start end 112768ad4a33SUladzislau Rezki (Sony) */ 112868ad4a33SUladzislau Rezki (Sony) if (next->prev != head) { 112968ad4a33SUladzislau Rezki (Sony) sibling = list_entry(next->prev, struct vmap_area, list); 113068ad4a33SUladzislau Rezki (Sony) if (sibling->va_end == va->va_start) { 11315dd78640SUladzislau Rezki (Sony) /* 11325dd78640SUladzislau Rezki (Sony) * If both neighbors are coalesced, it is important 11335dd78640SUladzislau Rezki (Sony) * to unlink the "next" node first, followed by merging 11345dd78640SUladzislau Rezki (Sony) * with "previous" one. Otherwise the tree might not be 11355dd78640SUladzislau Rezki (Sony) * fully populated if a sibling's augmented value is 11365dd78640SUladzislau Rezki (Sony) * "normalized" because of rotation operations. 11375dd78640SUladzislau Rezki (Sony) */ 113854f63d9dSUladzislau Rezki (Sony) if (merged) 113968ad4a33SUladzislau Rezki (Sony) unlink_va(va, root); 114068ad4a33SUladzislau Rezki (Sony) 11415dd78640SUladzislau Rezki (Sony) sibling->va_end = va->va_end; 11425dd78640SUladzislau Rezki (Sony) 114368ad4a33SUladzislau Rezki (Sony) /* Free vmap_area object. */ 114468ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 11453c5c3cfbSDaniel Axtens 11463c5c3cfbSDaniel Axtens /* Point to the new merged area. */ 11473c5c3cfbSDaniel Axtens va = sibling; 11483c5c3cfbSDaniel Axtens merged = true; 114968ad4a33SUladzislau Rezki (Sony) } 115068ad4a33SUladzislau Rezki (Sony) } 115168ad4a33SUladzislau Rezki (Sony) 115268ad4a33SUladzislau Rezki (Sony) insert: 11535dd78640SUladzislau Rezki (Sony) if (!merged) 115468ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 11553c5c3cfbSDaniel Axtens 115696e2db45SUladzislau Rezki (Sony) return va; 115796e2db45SUladzislau Rezki (Sony) } 115896e2db45SUladzislau Rezki (Sony) 115996e2db45SUladzislau Rezki (Sony) static __always_inline struct vmap_area * 116096e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area_augment(struct vmap_area *va, 116196e2db45SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 116296e2db45SUladzislau Rezki (Sony) { 116396e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area(va, root, head); 116496e2db45SUladzislau Rezki (Sony) if (va) 11655dd78640SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 116696e2db45SUladzislau Rezki (Sony) 11673c5c3cfbSDaniel Axtens return va; 116868ad4a33SUladzislau Rezki (Sony) } 116968ad4a33SUladzislau Rezki (Sony) 117068ad4a33SUladzislau Rezki (Sony) static __always_inline bool 117168ad4a33SUladzislau Rezki (Sony) is_within_this_va(struct vmap_area *va, unsigned long size, 117268ad4a33SUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 117368ad4a33SUladzislau Rezki (Sony) { 117468ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr; 117568ad4a33SUladzislau Rezki (Sony) 117668ad4a33SUladzislau Rezki (Sony) if (va->va_start > vstart) 117768ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(va->va_start, align); 117868ad4a33SUladzislau Rezki (Sony) else 117968ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(vstart, align); 118068ad4a33SUladzislau Rezki (Sony) 118168ad4a33SUladzislau Rezki (Sony) /* Can be overflowed due to big size or alignment. */ 118268ad4a33SUladzislau Rezki (Sony) if (nva_start_addr + size < nva_start_addr || 118368ad4a33SUladzislau Rezki (Sony) nva_start_addr < vstart) 118468ad4a33SUladzislau Rezki (Sony) return false; 118568ad4a33SUladzislau Rezki (Sony) 118668ad4a33SUladzislau Rezki (Sony) return (nva_start_addr + size <= va->va_end); 118768ad4a33SUladzislau Rezki (Sony) } 118868ad4a33SUladzislau Rezki (Sony) 118968ad4a33SUladzislau Rezki (Sony) /* 119068ad4a33SUladzislau Rezki (Sony) * Find the first free block(lowest start address) in the tree, 119168ad4a33SUladzislau Rezki (Sony) * that will accomplish the request corresponding to passing 11929333fe98SUladzislau Rezki * parameters. Please note, with an alignment bigger than PAGE_SIZE, 11939333fe98SUladzislau Rezki * a search length is adjusted to account for worst case alignment 11949333fe98SUladzislau Rezki * overhead. 119568ad4a33SUladzislau Rezki (Sony) */ 119668ad4a33SUladzislau Rezki (Sony) static __always_inline struct vmap_area * 11979333fe98SUladzislau Rezki find_vmap_lowest_match(unsigned long size, unsigned long align, 11989333fe98SUladzislau Rezki unsigned long vstart, bool adjust_search_size) 119968ad4a33SUladzislau Rezki (Sony) { 120068ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 120168ad4a33SUladzislau Rezki (Sony) struct rb_node *node; 12029333fe98SUladzislau Rezki unsigned long length; 120368ad4a33SUladzislau Rezki (Sony) 120468ad4a33SUladzislau Rezki (Sony) /* Start from the root. */ 120568ad4a33SUladzislau Rezki (Sony) node = free_vmap_area_root.rb_node; 120668ad4a33SUladzislau Rezki (Sony) 12079333fe98SUladzislau Rezki /* Adjust the search size for alignment overhead. */ 12089333fe98SUladzislau Rezki length = adjust_search_size ? size + align - 1 : size; 12099333fe98SUladzislau Rezki 121068ad4a33SUladzislau Rezki (Sony) while (node) { 121168ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 121268ad4a33SUladzislau Rezki (Sony) 12139333fe98SUladzislau Rezki if (get_subtree_max_size(node->rb_left) >= length && 121468ad4a33SUladzislau Rezki (Sony) vstart < va->va_start) { 121568ad4a33SUladzislau Rezki (Sony) node = node->rb_left; 121668ad4a33SUladzislau Rezki (Sony) } else { 121768ad4a33SUladzislau Rezki (Sony) if (is_within_this_va(va, size, align, vstart)) 121868ad4a33SUladzislau Rezki (Sony) return va; 121968ad4a33SUladzislau Rezki (Sony) 122068ad4a33SUladzislau Rezki (Sony) /* 122168ad4a33SUladzislau Rezki (Sony) * Does not make sense to go deeper towards the right 122268ad4a33SUladzislau Rezki (Sony) * sub-tree if it does not have a free block that is 12239333fe98SUladzislau Rezki * equal or bigger to the requested search length. 122468ad4a33SUladzislau Rezki (Sony) */ 12259333fe98SUladzislau Rezki if (get_subtree_max_size(node->rb_right) >= length) { 122668ad4a33SUladzislau Rezki (Sony) node = node->rb_right; 122768ad4a33SUladzislau Rezki (Sony) continue; 122868ad4a33SUladzislau Rezki (Sony) } 122968ad4a33SUladzislau Rezki (Sony) 123068ad4a33SUladzislau Rezki (Sony) /* 12313806b041SAndrew Morton * OK. We roll back and find the first right sub-tree, 123268ad4a33SUladzislau Rezki (Sony) * that will satisfy the search criteria. It can happen 12339f531973SUladzislau Rezki (Sony) * due to "vstart" restriction or an alignment overhead 12349f531973SUladzislau Rezki (Sony) * that is bigger then PAGE_SIZE. 123568ad4a33SUladzislau Rezki (Sony) */ 123668ad4a33SUladzislau Rezki (Sony) while ((node = rb_parent(node))) { 123768ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 123868ad4a33SUladzislau Rezki (Sony) if (is_within_this_va(va, size, align, vstart)) 123968ad4a33SUladzislau Rezki (Sony) return va; 124068ad4a33SUladzislau Rezki (Sony) 12419333fe98SUladzislau Rezki if (get_subtree_max_size(node->rb_right) >= length && 124268ad4a33SUladzislau Rezki (Sony) vstart <= va->va_start) { 12439f531973SUladzislau Rezki (Sony) /* 12449f531973SUladzislau Rezki (Sony) * Shift the vstart forward. Please note, we update it with 12459f531973SUladzislau Rezki (Sony) * parent's start address adding "1" because we do not want 12469f531973SUladzislau Rezki (Sony) * to enter same sub-tree after it has already been checked 12479f531973SUladzislau Rezki (Sony) * and no suitable free block found there. 12489f531973SUladzislau Rezki (Sony) */ 12499f531973SUladzislau Rezki (Sony) vstart = va->va_start + 1; 125068ad4a33SUladzislau Rezki (Sony) node = node->rb_right; 125168ad4a33SUladzislau Rezki (Sony) break; 125268ad4a33SUladzislau Rezki (Sony) } 125368ad4a33SUladzislau Rezki (Sony) } 125468ad4a33SUladzislau Rezki (Sony) } 125568ad4a33SUladzislau Rezki (Sony) } 125668ad4a33SUladzislau Rezki (Sony) 125768ad4a33SUladzislau Rezki (Sony) return NULL; 125868ad4a33SUladzislau Rezki (Sony) } 125968ad4a33SUladzislau Rezki (Sony) 1260a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 1261a6cf4e0fSUladzislau Rezki (Sony) #include <linux/random.h> 1262a6cf4e0fSUladzislau Rezki (Sony) 1263a6cf4e0fSUladzislau Rezki (Sony) static struct vmap_area * 1264a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_linear_match(unsigned long size, 1265a6cf4e0fSUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 1266a6cf4e0fSUladzislau Rezki (Sony) { 1267a6cf4e0fSUladzislau Rezki (Sony) struct vmap_area *va; 1268a6cf4e0fSUladzislau Rezki (Sony) 1269a6cf4e0fSUladzislau Rezki (Sony) list_for_each_entry(va, &free_vmap_area_list, list) { 1270a6cf4e0fSUladzislau Rezki (Sony) if (!is_within_this_va(va, size, align, vstart)) 1271a6cf4e0fSUladzislau Rezki (Sony) continue; 1272a6cf4e0fSUladzislau Rezki (Sony) 1273a6cf4e0fSUladzislau Rezki (Sony) return va; 1274a6cf4e0fSUladzislau Rezki (Sony) } 1275a6cf4e0fSUladzislau Rezki (Sony) 1276a6cf4e0fSUladzislau Rezki (Sony) return NULL; 1277a6cf4e0fSUladzislau Rezki (Sony) } 1278a6cf4e0fSUladzislau Rezki (Sony) 1279a6cf4e0fSUladzislau Rezki (Sony) static void 1280066fed59SUladzislau Rezki (Sony) find_vmap_lowest_match_check(unsigned long size, unsigned long align) 1281a6cf4e0fSUladzislau Rezki (Sony) { 1282a6cf4e0fSUladzislau Rezki (Sony) struct vmap_area *va_1, *va_2; 1283a6cf4e0fSUladzislau Rezki (Sony) unsigned long vstart; 1284a6cf4e0fSUladzislau Rezki (Sony) unsigned int rnd; 1285a6cf4e0fSUladzislau Rezki (Sony) 1286a6cf4e0fSUladzislau Rezki (Sony) get_random_bytes(&rnd, sizeof(rnd)); 1287a6cf4e0fSUladzislau Rezki (Sony) vstart = VMALLOC_START + rnd; 1288a6cf4e0fSUladzislau Rezki (Sony) 12899333fe98SUladzislau Rezki va_1 = find_vmap_lowest_match(size, align, vstart, false); 1290066fed59SUladzislau Rezki (Sony) va_2 = find_vmap_lowest_linear_match(size, align, vstart); 1291a6cf4e0fSUladzislau Rezki (Sony) 1292a6cf4e0fSUladzislau Rezki (Sony) if (va_1 != va_2) 1293a6cf4e0fSUladzislau Rezki (Sony) pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n", 1294a6cf4e0fSUladzislau Rezki (Sony) va_1, va_2, vstart); 1295a6cf4e0fSUladzislau Rezki (Sony) } 1296a6cf4e0fSUladzislau Rezki (Sony) #endif 1297a6cf4e0fSUladzislau Rezki (Sony) 129868ad4a33SUladzislau Rezki (Sony) enum fit_type { 129968ad4a33SUladzislau Rezki (Sony) NOTHING_FIT = 0, 130068ad4a33SUladzislau Rezki (Sony) FL_FIT_TYPE = 1, /* full fit */ 130168ad4a33SUladzislau Rezki (Sony) LE_FIT_TYPE = 2, /* left edge fit */ 130268ad4a33SUladzislau Rezki (Sony) RE_FIT_TYPE = 3, /* right edge fit */ 130368ad4a33SUladzislau Rezki (Sony) NE_FIT_TYPE = 4 /* no edge fit */ 130468ad4a33SUladzislau Rezki (Sony) }; 130568ad4a33SUladzislau Rezki (Sony) 130668ad4a33SUladzislau Rezki (Sony) static __always_inline enum fit_type 130768ad4a33SUladzislau Rezki (Sony) classify_va_fit_type(struct vmap_area *va, 130868ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr, unsigned long size) 130968ad4a33SUladzislau Rezki (Sony) { 131068ad4a33SUladzislau Rezki (Sony) enum fit_type type; 131168ad4a33SUladzislau Rezki (Sony) 131268ad4a33SUladzislau Rezki (Sony) /* Check if it is within VA. */ 131368ad4a33SUladzislau Rezki (Sony) if (nva_start_addr < va->va_start || 131468ad4a33SUladzislau Rezki (Sony) nva_start_addr + size > va->va_end) 131568ad4a33SUladzislau Rezki (Sony) return NOTHING_FIT; 131668ad4a33SUladzislau Rezki (Sony) 131768ad4a33SUladzislau Rezki (Sony) /* Now classify. */ 131868ad4a33SUladzislau Rezki (Sony) if (va->va_start == nva_start_addr) { 131968ad4a33SUladzislau Rezki (Sony) if (va->va_end == nva_start_addr + size) 132068ad4a33SUladzislau Rezki (Sony) type = FL_FIT_TYPE; 132168ad4a33SUladzislau Rezki (Sony) else 132268ad4a33SUladzislau Rezki (Sony) type = LE_FIT_TYPE; 132368ad4a33SUladzislau Rezki (Sony) } else if (va->va_end == nva_start_addr + size) { 132468ad4a33SUladzislau Rezki (Sony) type = RE_FIT_TYPE; 132568ad4a33SUladzislau Rezki (Sony) } else { 132668ad4a33SUladzislau Rezki (Sony) type = NE_FIT_TYPE; 132768ad4a33SUladzislau Rezki (Sony) } 132868ad4a33SUladzislau Rezki (Sony) 132968ad4a33SUladzislau Rezki (Sony) return type; 133068ad4a33SUladzislau Rezki (Sony) } 133168ad4a33SUladzislau Rezki (Sony) 133268ad4a33SUladzislau Rezki (Sony) static __always_inline int 133368ad4a33SUladzislau Rezki (Sony) adjust_va_to_fit_type(struct vmap_area *va, 133468ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr, unsigned long size, 133568ad4a33SUladzislau Rezki (Sony) enum fit_type type) 133668ad4a33SUladzislau Rezki (Sony) { 13372c929233SArnd Bergmann struct vmap_area *lva = NULL; 133868ad4a33SUladzislau Rezki (Sony) 133968ad4a33SUladzislau Rezki (Sony) if (type == FL_FIT_TYPE) { 134068ad4a33SUladzislau Rezki (Sony) /* 134168ad4a33SUladzislau Rezki (Sony) * No need to split VA, it fully fits. 134268ad4a33SUladzislau Rezki (Sony) * 134368ad4a33SUladzislau Rezki (Sony) * | | 134468ad4a33SUladzislau Rezki (Sony) * V NVA V 134568ad4a33SUladzislau Rezki (Sony) * |---------------| 134668ad4a33SUladzislau Rezki (Sony) */ 134768ad4a33SUladzislau Rezki (Sony) unlink_va(va, &free_vmap_area_root); 134868ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 134968ad4a33SUladzislau Rezki (Sony) } else if (type == LE_FIT_TYPE) { 135068ad4a33SUladzislau Rezki (Sony) /* 135168ad4a33SUladzislau Rezki (Sony) * Split left edge of fit VA. 135268ad4a33SUladzislau Rezki (Sony) * 135368ad4a33SUladzislau Rezki (Sony) * | | 135468ad4a33SUladzislau Rezki (Sony) * V NVA V R 135568ad4a33SUladzislau Rezki (Sony) * |-------|-------| 135668ad4a33SUladzislau Rezki (Sony) */ 135768ad4a33SUladzislau Rezki (Sony) va->va_start += size; 135868ad4a33SUladzislau Rezki (Sony) } else if (type == RE_FIT_TYPE) { 135968ad4a33SUladzislau Rezki (Sony) /* 136068ad4a33SUladzislau Rezki (Sony) * Split right edge of fit VA. 136168ad4a33SUladzislau Rezki (Sony) * 136268ad4a33SUladzislau Rezki (Sony) * | | 136368ad4a33SUladzislau Rezki (Sony) * L V NVA V 136468ad4a33SUladzislau Rezki (Sony) * |-------|-------| 136568ad4a33SUladzislau Rezki (Sony) */ 136668ad4a33SUladzislau Rezki (Sony) va->va_end = nva_start_addr; 136768ad4a33SUladzislau Rezki (Sony) } else if (type == NE_FIT_TYPE) { 136868ad4a33SUladzislau Rezki (Sony) /* 136968ad4a33SUladzislau Rezki (Sony) * Split no edge of fit VA. 137068ad4a33SUladzislau Rezki (Sony) * 137168ad4a33SUladzislau Rezki (Sony) * | | 137268ad4a33SUladzislau Rezki (Sony) * L V NVA V R 137368ad4a33SUladzislau Rezki (Sony) * |---|-------|---| 137468ad4a33SUladzislau Rezki (Sony) */ 137582dd23e8SUladzislau Rezki (Sony) lva = __this_cpu_xchg(ne_fit_preload_node, NULL); 137682dd23e8SUladzislau Rezki (Sony) if (unlikely(!lva)) { 137782dd23e8SUladzislau Rezki (Sony) /* 137882dd23e8SUladzislau Rezki (Sony) * For percpu allocator we do not do any pre-allocation 137982dd23e8SUladzislau Rezki (Sony) * and leave it as it is. The reason is it most likely 138082dd23e8SUladzislau Rezki (Sony) * never ends up with NE_FIT_TYPE splitting. In case of 138182dd23e8SUladzislau Rezki (Sony) * percpu allocations offsets and sizes are aligned to 138282dd23e8SUladzislau Rezki (Sony) * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE 138382dd23e8SUladzislau Rezki (Sony) * are its main fitting cases. 138482dd23e8SUladzislau Rezki (Sony) * 138582dd23e8SUladzislau Rezki (Sony) * There are a few exceptions though, as an example it is 138682dd23e8SUladzislau Rezki (Sony) * a first allocation (early boot up) when we have "one" 138782dd23e8SUladzislau Rezki (Sony) * big free space that has to be split. 1388060650a2SUladzislau Rezki (Sony) * 1389060650a2SUladzislau Rezki (Sony) * Also we can hit this path in case of regular "vmap" 1390060650a2SUladzislau Rezki (Sony) * allocations, if "this" current CPU was not preloaded. 1391060650a2SUladzislau Rezki (Sony) * See the comment in alloc_vmap_area() why. If so, then 1392060650a2SUladzislau Rezki (Sony) * GFP_NOWAIT is used instead to get an extra object for 1393060650a2SUladzislau Rezki (Sony) * split purpose. That is rare and most time does not 1394060650a2SUladzislau Rezki (Sony) * occur. 1395060650a2SUladzislau Rezki (Sony) * 1396060650a2SUladzislau Rezki (Sony) * What happens if an allocation gets failed. Basically, 1397060650a2SUladzislau Rezki (Sony) * an "overflow" path is triggered to purge lazily freed 1398060650a2SUladzislau Rezki (Sony) * areas to free some memory, then, the "retry" path is 1399060650a2SUladzislau Rezki (Sony) * triggered to repeat one more time. See more details 1400060650a2SUladzislau Rezki (Sony) * in alloc_vmap_area() function. 140182dd23e8SUladzislau Rezki (Sony) */ 140268ad4a33SUladzislau Rezki (Sony) lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT); 140382dd23e8SUladzislau Rezki (Sony) if (!lva) 140468ad4a33SUladzislau Rezki (Sony) return -1; 140582dd23e8SUladzislau Rezki (Sony) } 140668ad4a33SUladzislau Rezki (Sony) 140768ad4a33SUladzislau Rezki (Sony) /* 140868ad4a33SUladzislau Rezki (Sony) * Build the remainder. 140968ad4a33SUladzislau Rezki (Sony) */ 141068ad4a33SUladzislau Rezki (Sony) lva->va_start = va->va_start; 141168ad4a33SUladzislau Rezki (Sony) lva->va_end = nva_start_addr; 141268ad4a33SUladzislau Rezki (Sony) 141368ad4a33SUladzislau Rezki (Sony) /* 141468ad4a33SUladzislau Rezki (Sony) * Shrink this VA to remaining size. 141568ad4a33SUladzislau Rezki (Sony) */ 141668ad4a33SUladzislau Rezki (Sony) va->va_start = nva_start_addr + size; 141768ad4a33SUladzislau Rezki (Sony) } else { 141868ad4a33SUladzislau Rezki (Sony) return -1; 141968ad4a33SUladzislau Rezki (Sony) } 142068ad4a33SUladzislau Rezki (Sony) 142168ad4a33SUladzislau Rezki (Sony) if (type != FL_FIT_TYPE) { 142268ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 142368ad4a33SUladzislau Rezki (Sony) 14242c929233SArnd Bergmann if (lva) /* type == NE_FIT_TYPE */ 142568ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(lva, &va->rb_node, 142668ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, &free_vmap_area_list); 142768ad4a33SUladzislau Rezki (Sony) } 142868ad4a33SUladzislau Rezki (Sony) 142968ad4a33SUladzislau Rezki (Sony) return 0; 143068ad4a33SUladzislau Rezki (Sony) } 143168ad4a33SUladzislau Rezki (Sony) 143268ad4a33SUladzislau Rezki (Sony) /* 143368ad4a33SUladzislau Rezki (Sony) * Returns a start address of the newly allocated area, if success. 143468ad4a33SUladzislau Rezki (Sony) * Otherwise a vend is returned that indicates failure. 143568ad4a33SUladzislau Rezki (Sony) */ 143668ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 143768ad4a33SUladzislau Rezki (Sony) __alloc_vmap_area(unsigned long size, unsigned long align, 1438cacca6baSUladzislau Rezki (Sony) unsigned long vstart, unsigned long vend) 143968ad4a33SUladzislau Rezki (Sony) { 14409333fe98SUladzislau Rezki bool adjust_search_size = true; 144168ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr; 144268ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 144368ad4a33SUladzislau Rezki (Sony) enum fit_type type; 144468ad4a33SUladzislau Rezki (Sony) int ret; 144568ad4a33SUladzislau Rezki (Sony) 14469333fe98SUladzislau Rezki /* 14479333fe98SUladzislau Rezki * Do not adjust when: 14489333fe98SUladzislau Rezki * a) align <= PAGE_SIZE, because it does not make any sense. 14499333fe98SUladzislau Rezki * All blocks(their start addresses) are at least PAGE_SIZE 14509333fe98SUladzislau Rezki * aligned anyway; 14519333fe98SUladzislau Rezki * b) a short range where a requested size corresponds to exactly 14529333fe98SUladzislau Rezki * specified [vstart:vend] interval and an alignment > PAGE_SIZE. 14539333fe98SUladzislau Rezki * With adjusted search length an allocation would not succeed. 14549333fe98SUladzislau Rezki */ 14559333fe98SUladzislau Rezki if (align <= PAGE_SIZE || (align > PAGE_SIZE && (vend - vstart) == size)) 14569333fe98SUladzislau Rezki adjust_search_size = false; 14579333fe98SUladzislau Rezki 14589333fe98SUladzislau Rezki va = find_vmap_lowest_match(size, align, vstart, adjust_search_size); 145968ad4a33SUladzislau Rezki (Sony) if (unlikely(!va)) 146068ad4a33SUladzislau Rezki (Sony) return vend; 146168ad4a33SUladzislau Rezki (Sony) 146268ad4a33SUladzislau Rezki (Sony) if (va->va_start > vstart) 146368ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(va->va_start, align); 146468ad4a33SUladzislau Rezki (Sony) else 146568ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(vstart, align); 146668ad4a33SUladzislau Rezki (Sony) 146768ad4a33SUladzislau Rezki (Sony) /* Check the "vend" restriction. */ 146868ad4a33SUladzislau Rezki (Sony) if (nva_start_addr + size > vend) 146968ad4a33SUladzislau Rezki (Sony) return vend; 147068ad4a33SUladzislau Rezki (Sony) 147168ad4a33SUladzislau Rezki (Sony) /* Classify what we have found. */ 147268ad4a33SUladzislau Rezki (Sony) type = classify_va_fit_type(va, nva_start_addr, size); 147368ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(type == NOTHING_FIT)) 147468ad4a33SUladzislau Rezki (Sony) return vend; 147568ad4a33SUladzislau Rezki (Sony) 147668ad4a33SUladzislau Rezki (Sony) /* Update the free vmap_area. */ 147768ad4a33SUladzislau Rezki (Sony) ret = adjust_va_to_fit_type(va, nva_start_addr, size, type); 147868ad4a33SUladzislau Rezki (Sony) if (ret) 147968ad4a33SUladzislau Rezki (Sony) return vend; 148068ad4a33SUladzislau Rezki (Sony) 1481a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 1482066fed59SUladzislau Rezki (Sony) find_vmap_lowest_match_check(size, align); 1483a6cf4e0fSUladzislau Rezki (Sony) #endif 1484a6cf4e0fSUladzislau Rezki (Sony) 148568ad4a33SUladzislau Rezki (Sony) return nva_start_addr; 148668ad4a33SUladzislau Rezki (Sony) } 14874da56b99SChris Wilson 1488db64fe02SNick Piggin /* 1489d98c9e83SAndrey Ryabinin * Free a region of KVA allocated by alloc_vmap_area 1490d98c9e83SAndrey Ryabinin */ 1491d98c9e83SAndrey Ryabinin static void free_vmap_area(struct vmap_area *va) 1492d98c9e83SAndrey Ryabinin { 1493d98c9e83SAndrey Ryabinin /* 1494d98c9e83SAndrey Ryabinin * Remove from the busy tree/list. 1495d98c9e83SAndrey Ryabinin */ 1496d98c9e83SAndrey Ryabinin spin_lock(&vmap_area_lock); 1497d98c9e83SAndrey Ryabinin unlink_va(va, &vmap_area_root); 1498d98c9e83SAndrey Ryabinin spin_unlock(&vmap_area_lock); 1499d98c9e83SAndrey Ryabinin 1500d98c9e83SAndrey Ryabinin /* 1501d98c9e83SAndrey Ryabinin * Insert/Merge it back to the free tree/list. 1502d98c9e83SAndrey Ryabinin */ 1503d98c9e83SAndrey Ryabinin spin_lock(&free_vmap_area_lock); 150496e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list); 1505d98c9e83SAndrey Ryabinin spin_unlock(&free_vmap_area_lock); 1506d98c9e83SAndrey Ryabinin } 1507d98c9e83SAndrey Ryabinin 1508187f8cc4SUladzislau Rezki (Sony) static inline void 1509187f8cc4SUladzislau Rezki (Sony) preload_this_cpu_lock(spinlock_t *lock, gfp_t gfp_mask, int node) 1510187f8cc4SUladzislau Rezki (Sony) { 1511187f8cc4SUladzislau Rezki (Sony) struct vmap_area *va = NULL; 1512187f8cc4SUladzislau Rezki (Sony) 1513187f8cc4SUladzislau Rezki (Sony) /* 1514187f8cc4SUladzislau Rezki (Sony) * Preload this CPU with one extra vmap_area object. It is used 1515187f8cc4SUladzislau Rezki (Sony) * when fit type of free area is NE_FIT_TYPE. It guarantees that 1516187f8cc4SUladzislau Rezki (Sony) * a CPU that does an allocation is preloaded. 1517187f8cc4SUladzislau Rezki (Sony) * 1518187f8cc4SUladzislau Rezki (Sony) * We do it in non-atomic context, thus it allows us to use more 1519187f8cc4SUladzislau Rezki (Sony) * permissive allocation masks to be more stable under low memory 1520187f8cc4SUladzislau Rezki (Sony) * condition and high memory pressure. 1521187f8cc4SUladzislau Rezki (Sony) */ 1522187f8cc4SUladzislau Rezki (Sony) if (!this_cpu_read(ne_fit_preload_node)) 1523187f8cc4SUladzislau Rezki (Sony) va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); 1524187f8cc4SUladzislau Rezki (Sony) 1525187f8cc4SUladzislau Rezki (Sony) spin_lock(lock); 1526187f8cc4SUladzislau Rezki (Sony) 1527187f8cc4SUladzislau Rezki (Sony) if (va && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, va)) 1528187f8cc4SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 1529187f8cc4SUladzislau Rezki (Sony) } 1530187f8cc4SUladzislau Rezki (Sony) 1531d98c9e83SAndrey Ryabinin /* 1532db64fe02SNick Piggin * Allocate a region of KVA of the specified size and alignment, within the 1533db64fe02SNick Piggin * vstart and vend. 1534db64fe02SNick Piggin */ 1535db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size, 1536db64fe02SNick Piggin unsigned long align, 1537db64fe02SNick Piggin unsigned long vstart, unsigned long vend, 1538db64fe02SNick Piggin int node, gfp_t gfp_mask) 1539db64fe02SNick Piggin { 1540187f8cc4SUladzislau Rezki (Sony) struct vmap_area *va; 154112e376a6SUladzislau Rezki (Sony) unsigned long freed; 15421da177e4SLinus Torvalds unsigned long addr; 1543db64fe02SNick Piggin int purged = 0; 1544d98c9e83SAndrey Ryabinin int ret; 1545db64fe02SNick Piggin 15467766970cSNick Piggin BUG_ON(!size); 1547891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 154889699605SNick Piggin BUG_ON(!is_power_of_2(align)); 1549db64fe02SNick Piggin 155068ad4a33SUladzislau Rezki (Sony) if (unlikely(!vmap_initialized)) 155168ad4a33SUladzislau Rezki (Sony) return ERR_PTR(-EBUSY); 155268ad4a33SUladzislau Rezki (Sony) 15535803ed29SChristoph Hellwig might_sleep(); 1554f07116d7SUladzislau Rezki (Sony) gfp_mask = gfp_mask & GFP_RECLAIM_MASK; 15554da56b99SChris Wilson 1556f07116d7SUladzislau Rezki (Sony) va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); 1557db64fe02SNick Piggin if (unlikely(!va)) 1558db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 1559db64fe02SNick Piggin 15607f88f88fSCatalin Marinas /* 15617f88f88fSCatalin Marinas * Only scan the relevant parts containing pointers to other objects 15627f88f88fSCatalin Marinas * to avoid false negatives. 15637f88f88fSCatalin Marinas */ 1564f07116d7SUladzislau Rezki (Sony) kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask); 15657f88f88fSCatalin Marinas 1566db64fe02SNick Piggin retry: 1567187f8cc4SUladzislau Rezki (Sony) preload_this_cpu_lock(&free_vmap_area_lock, gfp_mask, node); 1568187f8cc4SUladzislau Rezki (Sony) addr = __alloc_vmap_area(size, align, vstart, vend); 1569187f8cc4SUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 157068ad4a33SUladzislau Rezki (Sony) 157189699605SNick Piggin /* 157268ad4a33SUladzislau Rezki (Sony) * If an allocation fails, the "vend" address is 157368ad4a33SUladzislau Rezki (Sony) * returned. Therefore trigger the overflow path. 157489699605SNick Piggin */ 157568ad4a33SUladzislau Rezki (Sony) if (unlikely(addr == vend)) 157689699605SNick Piggin goto overflow; 157789699605SNick Piggin 157889699605SNick Piggin va->va_start = addr; 157989699605SNick Piggin va->va_end = addr + size; 1580688fcbfcSPengfei Li va->vm = NULL; 158168ad4a33SUladzislau Rezki (Sony) 1582e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 1583e36176beSUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 158489699605SNick Piggin spin_unlock(&vmap_area_lock); 158589699605SNick Piggin 158661e16557SWang Xiaoqiang BUG_ON(!IS_ALIGNED(va->va_start, align)); 158789699605SNick Piggin BUG_ON(va->va_start < vstart); 158889699605SNick Piggin BUG_ON(va->va_end > vend); 158989699605SNick Piggin 1590d98c9e83SAndrey Ryabinin ret = kasan_populate_vmalloc(addr, size); 1591d98c9e83SAndrey Ryabinin if (ret) { 1592d98c9e83SAndrey Ryabinin free_vmap_area(va); 1593d98c9e83SAndrey Ryabinin return ERR_PTR(ret); 1594d98c9e83SAndrey Ryabinin } 1595d98c9e83SAndrey Ryabinin 159689699605SNick Piggin return va; 159789699605SNick Piggin 15987766970cSNick Piggin overflow: 1599db64fe02SNick Piggin if (!purged) { 1600db64fe02SNick Piggin purge_vmap_area_lazy(); 1601db64fe02SNick Piggin purged = 1; 1602db64fe02SNick Piggin goto retry; 1603db64fe02SNick Piggin } 16044da56b99SChris Wilson 160512e376a6SUladzislau Rezki (Sony) freed = 0; 16064da56b99SChris Wilson blocking_notifier_call_chain(&vmap_notify_list, 0, &freed); 160712e376a6SUladzislau Rezki (Sony) 16084da56b99SChris Wilson if (freed > 0) { 16094da56b99SChris Wilson purged = 0; 16104da56b99SChris Wilson goto retry; 16114da56b99SChris Wilson } 16124da56b99SChris Wilson 161303497d76SFlorian Fainelli if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) 1614756a025fSJoe Perches pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n", 1615756a025fSJoe Perches size); 161668ad4a33SUladzislau Rezki (Sony) 161768ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 1618db64fe02SNick Piggin return ERR_PTR(-EBUSY); 1619db64fe02SNick Piggin } 1620db64fe02SNick Piggin 16214da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb) 16224da56b99SChris Wilson { 16234da56b99SChris Wilson return blocking_notifier_chain_register(&vmap_notify_list, nb); 16244da56b99SChris Wilson } 16254da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier); 16264da56b99SChris Wilson 16274da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb) 16284da56b99SChris Wilson { 16294da56b99SChris Wilson return blocking_notifier_chain_unregister(&vmap_notify_list, nb); 16304da56b99SChris Wilson } 16314da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier); 16324da56b99SChris Wilson 1633db64fe02SNick Piggin /* 1634db64fe02SNick Piggin * lazy_max_pages is the maximum amount of virtual address space we gather up 1635db64fe02SNick Piggin * before attempting to purge with a TLB flush. 1636db64fe02SNick Piggin * 1637db64fe02SNick Piggin * There is a tradeoff here: a larger number will cover more kernel page tables 1638db64fe02SNick Piggin * and take slightly longer to purge, but it will linearly reduce the number of 1639db64fe02SNick Piggin * global TLB flushes that must be performed. It would seem natural to scale 1640db64fe02SNick Piggin * this number up linearly with the number of CPUs (because vmapping activity 1641db64fe02SNick Piggin * could also scale linearly with the number of CPUs), however it is likely 1642db64fe02SNick Piggin * that in practice, workloads might be constrained in other ways that mean 1643db64fe02SNick Piggin * vmap activity will not scale linearly with CPUs. Also, I want to be 1644db64fe02SNick Piggin * conservative and not introduce a big latency on huge systems, so go with 1645db64fe02SNick Piggin * a less aggressive log scale. It will still be an improvement over the old 1646db64fe02SNick Piggin * code, and it will be simple to change the scale factor if we find that it 1647db64fe02SNick Piggin * becomes a problem on bigger systems. 1648db64fe02SNick Piggin */ 1649db64fe02SNick Piggin static unsigned long lazy_max_pages(void) 1650db64fe02SNick Piggin { 1651db64fe02SNick Piggin unsigned int log; 1652db64fe02SNick Piggin 1653db64fe02SNick Piggin log = fls(num_online_cpus()); 1654db64fe02SNick Piggin 1655db64fe02SNick Piggin return log * (32UL * 1024 * 1024 / PAGE_SIZE); 1656db64fe02SNick Piggin } 1657db64fe02SNick Piggin 16584d36e6f8SUladzislau Rezki (Sony) static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0); 1659db64fe02SNick Piggin 16600574ecd1SChristoph Hellwig /* 1661f0953a1bSIngo Molnar * Serialize vmap purging. There is no actual critical section protected 16620574ecd1SChristoph Hellwig * by this look, but we want to avoid concurrent calls for performance 16630574ecd1SChristoph Hellwig * reasons and to make the pcpu_get_vm_areas more deterministic. 16640574ecd1SChristoph Hellwig */ 1665f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock); 16660574ecd1SChristoph Hellwig 166702b709dfSNick Piggin /* for per-CPU blocks */ 166802b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void); 166902b709dfSNick Piggin 16705da96bddSMel Gorman #ifdef CONFIG_X86_64 1671db64fe02SNick Piggin /* 16723ee48b6aSCliff Wickman * called before a call to iounmap() if the caller wants vm_area_struct's 16733ee48b6aSCliff Wickman * immediately freed. 16743ee48b6aSCliff Wickman */ 16753ee48b6aSCliff Wickman void set_iounmap_nonlazy(void) 16763ee48b6aSCliff Wickman { 16774d36e6f8SUladzislau Rezki (Sony) atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1); 16783ee48b6aSCliff Wickman } 16795da96bddSMel Gorman #endif /* CONFIG_X86_64 */ 16803ee48b6aSCliff Wickman 16813ee48b6aSCliff Wickman /* 1682db64fe02SNick Piggin * Purges all lazily-freed vmap areas. 1683db64fe02SNick Piggin */ 16840574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end) 1685db64fe02SNick Piggin { 16864d36e6f8SUladzislau Rezki (Sony) unsigned long resched_threshold; 168796e2db45SUladzislau Rezki (Sony) struct list_head local_pure_list; 168896e2db45SUladzislau Rezki (Sony) struct vmap_area *va, *n_va; 1689db64fe02SNick Piggin 16900574ecd1SChristoph Hellwig lockdep_assert_held(&vmap_purge_lock); 169102b709dfSNick Piggin 169296e2db45SUladzislau Rezki (Sony) spin_lock(&purge_vmap_area_lock); 169396e2db45SUladzislau Rezki (Sony) purge_vmap_area_root = RB_ROOT; 169496e2db45SUladzislau Rezki (Sony) list_replace_init(&purge_vmap_area_list, &local_pure_list); 169596e2db45SUladzislau Rezki (Sony) spin_unlock(&purge_vmap_area_lock); 169696e2db45SUladzislau Rezki (Sony) 169796e2db45SUladzislau Rezki (Sony) if (unlikely(list_empty(&local_pure_list))) 169868571be9SUladzislau Rezki (Sony) return false; 169968571be9SUladzislau Rezki (Sony) 170096e2db45SUladzislau Rezki (Sony) start = min(start, 170196e2db45SUladzislau Rezki (Sony) list_first_entry(&local_pure_list, 170296e2db45SUladzislau Rezki (Sony) struct vmap_area, list)->va_start); 170396e2db45SUladzislau Rezki (Sony) 170496e2db45SUladzislau Rezki (Sony) end = max(end, 170596e2db45SUladzislau Rezki (Sony) list_last_entry(&local_pure_list, 170696e2db45SUladzislau Rezki (Sony) struct vmap_area, list)->va_end); 1707db64fe02SNick Piggin 17080574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 17094d36e6f8SUladzislau Rezki (Sony) resched_threshold = lazy_max_pages() << 1; 1710db64fe02SNick Piggin 1711e36176beSUladzislau Rezki (Sony) spin_lock(&free_vmap_area_lock); 171296e2db45SUladzislau Rezki (Sony) list_for_each_entry_safe(va, n_va, &local_pure_list, list) { 17134d36e6f8SUladzislau Rezki (Sony) unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT; 17143c5c3cfbSDaniel Axtens unsigned long orig_start = va->va_start; 17153c5c3cfbSDaniel Axtens unsigned long orig_end = va->va_end; 1716763b218dSJoel Fernandes 1717dd3b8353SUladzislau Rezki (Sony) /* 1718dd3b8353SUladzislau Rezki (Sony) * Finally insert or merge lazily-freed area. It is 1719dd3b8353SUladzislau Rezki (Sony) * detached and there is no need to "unlink" it from 1720dd3b8353SUladzislau Rezki (Sony) * anything. 1721dd3b8353SUladzislau Rezki (Sony) */ 172296e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root, 17233c5c3cfbSDaniel Axtens &free_vmap_area_list); 17243c5c3cfbSDaniel Axtens 17259c801f61SUladzislau Rezki (Sony) if (!va) 17269c801f61SUladzislau Rezki (Sony) continue; 17279c801f61SUladzislau Rezki (Sony) 17283c5c3cfbSDaniel Axtens if (is_vmalloc_or_module_addr((void *)orig_start)) 17293c5c3cfbSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 17303c5c3cfbSDaniel Axtens va->va_start, va->va_end); 1731dd3b8353SUladzislau Rezki (Sony) 17324d36e6f8SUladzislau Rezki (Sony) atomic_long_sub(nr, &vmap_lazy_nr); 173368571be9SUladzislau Rezki (Sony) 17344d36e6f8SUladzislau Rezki (Sony) if (atomic_long_read(&vmap_lazy_nr) < resched_threshold) 1735e36176beSUladzislau Rezki (Sony) cond_resched_lock(&free_vmap_area_lock); 1736763b218dSJoel Fernandes } 1737e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 17380574ecd1SChristoph Hellwig return true; 1739db64fe02SNick Piggin } 1740db64fe02SNick Piggin 1741db64fe02SNick Piggin /* 1742db64fe02SNick Piggin * Kick off a purge of the outstanding lazy areas. 1743db64fe02SNick Piggin */ 1744db64fe02SNick Piggin static void purge_vmap_area_lazy(void) 1745db64fe02SNick Piggin { 1746f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 17470574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 17480574ecd1SChristoph Hellwig __purge_vmap_area_lazy(ULONG_MAX, 0); 1749f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 1750db64fe02SNick Piggin } 1751db64fe02SNick Piggin 1752690467c8SUladzislau Rezki (Sony) static void drain_vmap_area_work(struct work_struct *work) 1753690467c8SUladzislau Rezki (Sony) { 1754690467c8SUladzislau Rezki (Sony) unsigned long nr_lazy; 1755690467c8SUladzislau Rezki (Sony) 1756690467c8SUladzislau Rezki (Sony) do { 1757690467c8SUladzislau Rezki (Sony) mutex_lock(&vmap_purge_lock); 1758690467c8SUladzislau Rezki (Sony) __purge_vmap_area_lazy(ULONG_MAX, 0); 1759690467c8SUladzislau Rezki (Sony) mutex_unlock(&vmap_purge_lock); 1760690467c8SUladzislau Rezki (Sony) 1761690467c8SUladzislau Rezki (Sony) /* Recheck if further work is required. */ 1762690467c8SUladzislau Rezki (Sony) nr_lazy = atomic_long_read(&vmap_lazy_nr); 1763690467c8SUladzislau Rezki (Sony) } while (nr_lazy > lazy_max_pages()); 1764690467c8SUladzislau Rezki (Sony) } 1765690467c8SUladzislau Rezki (Sony) 1766db64fe02SNick Piggin /* 176764141da5SJeremy Fitzhardinge * Free a vmap area, caller ensuring that the area has been unmapped 176864141da5SJeremy Fitzhardinge * and flush_cache_vunmap had been called for the correct range 176964141da5SJeremy Fitzhardinge * previously. 1770db64fe02SNick Piggin */ 177164141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va) 1772db64fe02SNick Piggin { 17734d36e6f8SUladzislau Rezki (Sony) unsigned long nr_lazy; 177480c4bd7aSChris Wilson 1775dd3b8353SUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 1776dd3b8353SUladzislau Rezki (Sony) unlink_va(va, &vmap_area_root); 1777dd3b8353SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 1778dd3b8353SUladzislau Rezki (Sony) 17794d36e6f8SUladzislau Rezki (Sony) nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >> 17804d36e6f8SUladzislau Rezki (Sony) PAGE_SHIFT, &vmap_lazy_nr); 178180c4bd7aSChris Wilson 178296e2db45SUladzislau Rezki (Sony) /* 178396e2db45SUladzislau Rezki (Sony) * Merge or place it to the purge tree/list. 178496e2db45SUladzislau Rezki (Sony) */ 178596e2db45SUladzislau Rezki (Sony) spin_lock(&purge_vmap_area_lock); 178696e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area(va, 178796e2db45SUladzislau Rezki (Sony) &purge_vmap_area_root, &purge_vmap_area_list); 178896e2db45SUladzislau Rezki (Sony) spin_unlock(&purge_vmap_area_lock); 178980c4bd7aSChris Wilson 179096e2db45SUladzislau Rezki (Sony) /* After this point, we may free va at any time */ 179180c4bd7aSChris Wilson if (unlikely(nr_lazy > lazy_max_pages())) 1792690467c8SUladzislau Rezki (Sony) schedule_work(&drain_vmap_work); 1793db64fe02SNick Piggin } 1794db64fe02SNick Piggin 1795b29acbdcSNick Piggin /* 1796b29acbdcSNick Piggin * Free and unmap a vmap area 1797b29acbdcSNick Piggin */ 1798b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va) 1799b29acbdcSNick Piggin { 1800b29acbdcSNick Piggin flush_cache_vunmap(va->va_start, va->va_end); 18014ad0ae8cSNicholas Piggin vunmap_range_noflush(va->va_start, va->va_end); 18028e57f8acSVlastimil Babka if (debug_pagealloc_enabled_static()) 180382a2e924SChintan Pandya flush_tlb_kernel_range(va->va_start, va->va_end); 180482a2e924SChintan Pandya 1805c8eef01eSChristoph Hellwig free_vmap_area_noflush(va); 1806b29acbdcSNick Piggin } 1807b29acbdcSNick Piggin 1808db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr) 1809db64fe02SNick Piggin { 1810db64fe02SNick Piggin struct vmap_area *va; 1811db64fe02SNick Piggin 1812db64fe02SNick Piggin spin_lock(&vmap_area_lock); 1813db64fe02SNick Piggin va = __find_vmap_area(addr); 1814db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 1815db64fe02SNick Piggin 1816db64fe02SNick Piggin return va; 1817db64fe02SNick Piggin } 1818db64fe02SNick Piggin 1819db64fe02SNick Piggin /*** Per cpu kva allocator ***/ 1820db64fe02SNick Piggin 1821db64fe02SNick Piggin /* 1822db64fe02SNick Piggin * vmap space is limited especially on 32 bit architectures. Ensure there is 1823db64fe02SNick Piggin * room for at least 16 percpu vmap blocks per CPU. 1824db64fe02SNick Piggin */ 1825db64fe02SNick Piggin /* 1826db64fe02SNick Piggin * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able 1827db64fe02SNick Piggin * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess 1828db64fe02SNick Piggin * instead (we just need a rough idea) 1829db64fe02SNick Piggin */ 1830db64fe02SNick Piggin #if BITS_PER_LONG == 32 1831db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024) 1832db64fe02SNick Piggin #else 1833db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024*1024) 1834db64fe02SNick Piggin #endif 1835db64fe02SNick Piggin 1836db64fe02SNick Piggin #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) 1837db64fe02SNick Piggin #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ 1838db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ 1839db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) 1840db64fe02SNick Piggin #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ 1841db64fe02SNick Piggin #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ 1842f982f915SClemens Ladisch #define VMAP_BBMAP_BITS \ 1843f982f915SClemens Ladisch VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ 1844db64fe02SNick Piggin VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ 1845f982f915SClemens Ladisch VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16)) 1846db64fe02SNick Piggin 1847db64fe02SNick Piggin #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) 1848db64fe02SNick Piggin 1849db64fe02SNick Piggin struct vmap_block_queue { 1850db64fe02SNick Piggin spinlock_t lock; 1851db64fe02SNick Piggin struct list_head free; 1852db64fe02SNick Piggin }; 1853db64fe02SNick Piggin 1854db64fe02SNick Piggin struct vmap_block { 1855db64fe02SNick Piggin spinlock_t lock; 1856db64fe02SNick Piggin struct vmap_area *va; 1857db64fe02SNick Piggin unsigned long free, dirty; 18587d61bfe8SRoman Pen unsigned long dirty_min, dirty_max; /*< dirty range */ 1859db64fe02SNick Piggin struct list_head free_list; 1860db64fe02SNick Piggin struct rcu_head rcu_head; 186102b709dfSNick Piggin struct list_head purge; 1862db64fe02SNick Piggin }; 1863db64fe02SNick Piggin 1864db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ 1865db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); 1866db64fe02SNick Piggin 1867db64fe02SNick Piggin /* 18680f14599cSMatthew Wilcox (Oracle) * XArray of vmap blocks, indexed by address, to quickly find a vmap block 1869db64fe02SNick Piggin * in the free path. Could get rid of this if we change the API to return a 1870db64fe02SNick Piggin * "cookie" from alloc, to be passed to free. But no big deal yet. 1871db64fe02SNick Piggin */ 18720f14599cSMatthew Wilcox (Oracle) static DEFINE_XARRAY(vmap_blocks); 1873db64fe02SNick Piggin 1874db64fe02SNick Piggin /* 1875db64fe02SNick Piggin * We should probably have a fallback mechanism to allocate virtual memory 1876db64fe02SNick Piggin * out of partially filled vmap blocks. However vmap block sizing should be 1877db64fe02SNick Piggin * fairly reasonable according to the vmalloc size, so it shouldn't be a 1878db64fe02SNick Piggin * big problem. 1879db64fe02SNick Piggin */ 1880db64fe02SNick Piggin 1881db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr) 1882db64fe02SNick Piggin { 1883db64fe02SNick Piggin addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); 1884db64fe02SNick Piggin addr /= VMAP_BLOCK_SIZE; 1885db64fe02SNick Piggin return addr; 1886db64fe02SNick Piggin } 1887db64fe02SNick Piggin 1888cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off) 1889cf725ce2SRoman Pen { 1890cf725ce2SRoman Pen unsigned long addr; 1891cf725ce2SRoman Pen 1892cf725ce2SRoman Pen addr = va_start + (pages_off << PAGE_SHIFT); 1893cf725ce2SRoman Pen BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start)); 1894cf725ce2SRoman Pen return (void *)addr; 1895cf725ce2SRoman Pen } 1896cf725ce2SRoman Pen 1897cf725ce2SRoman Pen /** 1898cf725ce2SRoman Pen * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this 1899cf725ce2SRoman Pen * block. Of course pages number can't exceed VMAP_BBMAP_BITS 1900cf725ce2SRoman Pen * @order: how many 2^order pages should be occupied in newly allocated block 1901cf725ce2SRoman Pen * @gfp_mask: flags for the page level allocator 1902cf725ce2SRoman Pen * 1903a862f68aSMike Rapoport * Return: virtual address in a newly allocated block or ERR_PTR(-errno) 1904cf725ce2SRoman Pen */ 1905cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) 1906db64fe02SNick Piggin { 1907db64fe02SNick Piggin struct vmap_block_queue *vbq; 1908db64fe02SNick Piggin struct vmap_block *vb; 1909db64fe02SNick Piggin struct vmap_area *va; 1910db64fe02SNick Piggin unsigned long vb_idx; 1911db64fe02SNick Piggin int node, err; 1912cf725ce2SRoman Pen void *vaddr; 1913db64fe02SNick Piggin 1914db64fe02SNick Piggin node = numa_node_id(); 1915db64fe02SNick Piggin 1916db64fe02SNick Piggin vb = kmalloc_node(sizeof(struct vmap_block), 1917db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 1918db64fe02SNick Piggin if (unlikely(!vb)) 1919db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 1920db64fe02SNick Piggin 1921db64fe02SNick Piggin va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, 1922db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, 1923db64fe02SNick Piggin node, gfp_mask); 1924ddf9c6d4STobias Klauser if (IS_ERR(va)) { 1925db64fe02SNick Piggin kfree(vb); 1926e7d86340SJulia Lawall return ERR_CAST(va); 1927db64fe02SNick Piggin } 1928db64fe02SNick Piggin 1929cf725ce2SRoman Pen vaddr = vmap_block_vaddr(va->va_start, 0); 1930db64fe02SNick Piggin spin_lock_init(&vb->lock); 1931db64fe02SNick Piggin vb->va = va; 1932cf725ce2SRoman Pen /* At least something should be left free */ 1933cf725ce2SRoman Pen BUG_ON(VMAP_BBMAP_BITS <= (1UL << order)); 1934cf725ce2SRoman Pen vb->free = VMAP_BBMAP_BITS - (1UL << order); 1935db64fe02SNick Piggin vb->dirty = 0; 19367d61bfe8SRoman Pen vb->dirty_min = VMAP_BBMAP_BITS; 19377d61bfe8SRoman Pen vb->dirty_max = 0; 1938db64fe02SNick Piggin INIT_LIST_HEAD(&vb->free_list); 1939db64fe02SNick Piggin 1940db64fe02SNick Piggin vb_idx = addr_to_vb_idx(va->va_start); 19410f14599cSMatthew Wilcox (Oracle) err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask); 19420f14599cSMatthew Wilcox (Oracle) if (err) { 19430f14599cSMatthew Wilcox (Oracle) kfree(vb); 19440f14599cSMatthew Wilcox (Oracle) free_vmap_area(va); 19450f14599cSMatthew Wilcox (Oracle) return ERR_PTR(err); 19460f14599cSMatthew Wilcox (Oracle) } 1947db64fe02SNick Piggin 1948db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 1949db64fe02SNick Piggin spin_lock(&vbq->lock); 195068ac546fSRoman Pen list_add_tail_rcu(&vb->free_list, &vbq->free); 1951db64fe02SNick Piggin spin_unlock(&vbq->lock); 19523f04ba85STejun Heo put_cpu_var(vmap_block_queue); 1953db64fe02SNick Piggin 1954cf725ce2SRoman Pen return vaddr; 1955db64fe02SNick Piggin } 1956db64fe02SNick Piggin 1957db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb) 1958db64fe02SNick Piggin { 1959db64fe02SNick Piggin struct vmap_block *tmp; 1960db64fe02SNick Piggin 19610f14599cSMatthew Wilcox (Oracle) tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start)); 1962db64fe02SNick Piggin BUG_ON(tmp != vb); 1963db64fe02SNick Piggin 196464141da5SJeremy Fitzhardinge free_vmap_area_noflush(vb->va); 196522a3c7d1SLai Jiangshan kfree_rcu(vb, rcu_head); 1966db64fe02SNick Piggin } 1967db64fe02SNick Piggin 196802b709dfSNick Piggin static void purge_fragmented_blocks(int cpu) 196902b709dfSNick Piggin { 197002b709dfSNick Piggin LIST_HEAD(purge); 197102b709dfSNick Piggin struct vmap_block *vb; 197202b709dfSNick Piggin struct vmap_block *n_vb; 197302b709dfSNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 197402b709dfSNick Piggin 197502b709dfSNick Piggin rcu_read_lock(); 197602b709dfSNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 197702b709dfSNick Piggin 197802b709dfSNick Piggin if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS)) 197902b709dfSNick Piggin continue; 198002b709dfSNick Piggin 198102b709dfSNick Piggin spin_lock(&vb->lock); 198202b709dfSNick Piggin if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) { 198302b709dfSNick Piggin vb->free = 0; /* prevent further allocs after releasing lock */ 198402b709dfSNick Piggin vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */ 19857d61bfe8SRoman Pen vb->dirty_min = 0; 19867d61bfe8SRoman Pen vb->dirty_max = VMAP_BBMAP_BITS; 198702b709dfSNick Piggin spin_lock(&vbq->lock); 198802b709dfSNick Piggin list_del_rcu(&vb->free_list); 198902b709dfSNick Piggin spin_unlock(&vbq->lock); 199002b709dfSNick Piggin spin_unlock(&vb->lock); 199102b709dfSNick Piggin list_add_tail(&vb->purge, &purge); 199202b709dfSNick Piggin } else 199302b709dfSNick Piggin spin_unlock(&vb->lock); 199402b709dfSNick Piggin } 199502b709dfSNick Piggin rcu_read_unlock(); 199602b709dfSNick Piggin 199702b709dfSNick Piggin list_for_each_entry_safe(vb, n_vb, &purge, purge) { 199802b709dfSNick Piggin list_del(&vb->purge); 199902b709dfSNick Piggin free_vmap_block(vb); 200002b709dfSNick Piggin } 200102b709dfSNick Piggin } 200202b709dfSNick Piggin 200302b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void) 200402b709dfSNick Piggin { 200502b709dfSNick Piggin int cpu; 200602b709dfSNick Piggin 200702b709dfSNick Piggin for_each_possible_cpu(cpu) 200802b709dfSNick Piggin purge_fragmented_blocks(cpu); 200902b709dfSNick Piggin } 201002b709dfSNick Piggin 2011db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask) 2012db64fe02SNick Piggin { 2013db64fe02SNick Piggin struct vmap_block_queue *vbq; 2014db64fe02SNick Piggin struct vmap_block *vb; 2015cf725ce2SRoman Pen void *vaddr = NULL; 2016db64fe02SNick Piggin unsigned int order; 2017db64fe02SNick Piggin 2018891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 2019db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 2020aa91c4d8SJan Kara if (WARN_ON(size == 0)) { 2021aa91c4d8SJan Kara /* 2022aa91c4d8SJan Kara * Allocating 0 bytes isn't what caller wants since 2023aa91c4d8SJan Kara * get_order(0) returns funny result. Just warn and terminate 2024aa91c4d8SJan Kara * early. 2025aa91c4d8SJan Kara */ 2026aa91c4d8SJan Kara return NULL; 2027aa91c4d8SJan Kara } 2028db64fe02SNick Piggin order = get_order(size); 2029db64fe02SNick Piggin 2030db64fe02SNick Piggin rcu_read_lock(); 2031db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 2032db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 2033cf725ce2SRoman Pen unsigned long pages_off; 2034db64fe02SNick Piggin 2035db64fe02SNick Piggin spin_lock(&vb->lock); 2036cf725ce2SRoman Pen if (vb->free < (1UL << order)) { 2037cf725ce2SRoman Pen spin_unlock(&vb->lock); 2038cf725ce2SRoman Pen continue; 2039cf725ce2SRoman Pen } 204002b709dfSNick Piggin 2041cf725ce2SRoman Pen pages_off = VMAP_BBMAP_BITS - vb->free; 2042cf725ce2SRoman Pen vaddr = vmap_block_vaddr(vb->va->va_start, pages_off); 2043db64fe02SNick Piggin vb->free -= 1UL << order; 2044db64fe02SNick Piggin if (vb->free == 0) { 2045db64fe02SNick Piggin spin_lock(&vbq->lock); 2046de560423SNick Piggin list_del_rcu(&vb->free_list); 2047db64fe02SNick Piggin spin_unlock(&vbq->lock); 2048db64fe02SNick Piggin } 2049cf725ce2SRoman Pen 2050db64fe02SNick Piggin spin_unlock(&vb->lock); 2051db64fe02SNick Piggin break; 2052db64fe02SNick Piggin } 205302b709dfSNick Piggin 20543f04ba85STejun Heo put_cpu_var(vmap_block_queue); 2055db64fe02SNick Piggin rcu_read_unlock(); 2056db64fe02SNick Piggin 2057cf725ce2SRoman Pen /* Allocate new block if nothing was found */ 2058cf725ce2SRoman Pen if (!vaddr) 2059cf725ce2SRoman Pen vaddr = new_vmap_block(order, gfp_mask); 2060db64fe02SNick Piggin 2061cf725ce2SRoman Pen return vaddr; 2062db64fe02SNick Piggin } 2063db64fe02SNick Piggin 206478a0e8c4SChristoph Hellwig static void vb_free(unsigned long addr, unsigned long size) 2065db64fe02SNick Piggin { 2066db64fe02SNick Piggin unsigned long offset; 2067db64fe02SNick Piggin unsigned int order; 2068db64fe02SNick Piggin struct vmap_block *vb; 2069db64fe02SNick Piggin 2070891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 2071db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 2072b29acbdcSNick Piggin 207378a0e8c4SChristoph Hellwig flush_cache_vunmap(addr, addr + size); 2074b29acbdcSNick Piggin 2075db64fe02SNick Piggin order = get_order(size); 207678a0e8c4SChristoph Hellwig offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT; 20770f14599cSMatthew Wilcox (Oracle) vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr)); 2078db64fe02SNick Piggin 20794ad0ae8cSNicholas Piggin vunmap_range_noflush(addr, addr + size); 208064141da5SJeremy Fitzhardinge 20818e57f8acSVlastimil Babka if (debug_pagealloc_enabled_static()) 208278a0e8c4SChristoph Hellwig flush_tlb_kernel_range(addr, addr + size); 208382a2e924SChintan Pandya 2084db64fe02SNick Piggin spin_lock(&vb->lock); 20857d61bfe8SRoman Pen 20867d61bfe8SRoman Pen /* Expand dirty range */ 20877d61bfe8SRoman Pen vb->dirty_min = min(vb->dirty_min, offset); 20887d61bfe8SRoman Pen vb->dirty_max = max(vb->dirty_max, offset + (1UL << order)); 2089d086817dSMinChan Kim 2090db64fe02SNick Piggin vb->dirty += 1UL << order; 2091db64fe02SNick Piggin if (vb->dirty == VMAP_BBMAP_BITS) { 2092de560423SNick Piggin BUG_ON(vb->free); 2093db64fe02SNick Piggin spin_unlock(&vb->lock); 2094db64fe02SNick Piggin free_vmap_block(vb); 2095db64fe02SNick Piggin } else 2096db64fe02SNick Piggin spin_unlock(&vb->lock); 2097db64fe02SNick Piggin } 2098db64fe02SNick Piggin 2099868b104dSRick Edgecombe static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush) 2100db64fe02SNick Piggin { 2101db64fe02SNick Piggin int cpu; 2102db64fe02SNick Piggin 21039b463334SJeremy Fitzhardinge if (unlikely(!vmap_initialized)) 21049b463334SJeremy Fitzhardinge return; 21059b463334SJeremy Fitzhardinge 21065803ed29SChristoph Hellwig might_sleep(); 21075803ed29SChristoph Hellwig 2108db64fe02SNick Piggin for_each_possible_cpu(cpu) { 2109db64fe02SNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 2110db64fe02SNick Piggin struct vmap_block *vb; 2111db64fe02SNick Piggin 2112db64fe02SNick Piggin rcu_read_lock(); 2113db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 2114db64fe02SNick Piggin spin_lock(&vb->lock); 2115ad216c03SVijayanand Jitta if (vb->dirty && vb->dirty != VMAP_BBMAP_BITS) { 21167d61bfe8SRoman Pen unsigned long va_start = vb->va->va_start; 2117db64fe02SNick Piggin unsigned long s, e; 2118b136be5eSJoonsoo Kim 21197d61bfe8SRoman Pen s = va_start + (vb->dirty_min << PAGE_SHIFT); 21207d61bfe8SRoman Pen e = va_start + (vb->dirty_max << PAGE_SHIFT); 2121db64fe02SNick Piggin 21227d61bfe8SRoman Pen start = min(s, start); 21237d61bfe8SRoman Pen end = max(e, end); 21247d61bfe8SRoman Pen 2125db64fe02SNick Piggin flush = 1; 2126db64fe02SNick Piggin } 2127db64fe02SNick Piggin spin_unlock(&vb->lock); 2128db64fe02SNick Piggin } 2129db64fe02SNick Piggin rcu_read_unlock(); 2130db64fe02SNick Piggin } 2131db64fe02SNick Piggin 2132f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 21330574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 21340574ecd1SChristoph Hellwig if (!__purge_vmap_area_lazy(start, end) && flush) 21350574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 2136f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 2137db64fe02SNick Piggin } 2138868b104dSRick Edgecombe 2139868b104dSRick Edgecombe /** 2140868b104dSRick Edgecombe * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer 2141868b104dSRick Edgecombe * 2142868b104dSRick Edgecombe * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily 2143868b104dSRick Edgecombe * to amortize TLB flushing overheads. What this means is that any page you 2144868b104dSRick Edgecombe * have now, may, in a former life, have been mapped into kernel virtual 2145868b104dSRick Edgecombe * address by the vmap layer and so there might be some CPUs with TLB entries 2146868b104dSRick Edgecombe * still referencing that page (additional to the regular 1:1 kernel mapping). 2147868b104dSRick Edgecombe * 2148868b104dSRick Edgecombe * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can 2149868b104dSRick Edgecombe * be sure that none of the pages we have control over will have any aliases 2150868b104dSRick Edgecombe * from the vmap layer. 2151868b104dSRick Edgecombe */ 2152868b104dSRick Edgecombe void vm_unmap_aliases(void) 2153868b104dSRick Edgecombe { 2154868b104dSRick Edgecombe unsigned long start = ULONG_MAX, end = 0; 2155868b104dSRick Edgecombe int flush = 0; 2156868b104dSRick Edgecombe 2157868b104dSRick Edgecombe _vm_unmap_aliases(start, end, flush); 2158868b104dSRick Edgecombe } 2159db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases); 2160db64fe02SNick Piggin 2161db64fe02SNick Piggin /** 2162db64fe02SNick Piggin * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram 2163db64fe02SNick Piggin * @mem: the pointer returned by vm_map_ram 2164db64fe02SNick Piggin * @count: the count passed to that vm_map_ram call (cannot unmap partial) 2165db64fe02SNick Piggin */ 2166db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count) 2167db64fe02SNick Piggin { 216865ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 2169db64fe02SNick Piggin unsigned long addr = (unsigned long)mem; 21709c3acf60SChristoph Hellwig struct vmap_area *va; 2171db64fe02SNick Piggin 21725803ed29SChristoph Hellwig might_sleep(); 2173db64fe02SNick Piggin BUG_ON(!addr); 2174db64fe02SNick Piggin BUG_ON(addr < VMALLOC_START); 2175db64fe02SNick Piggin BUG_ON(addr > VMALLOC_END); 2176a1c0b1a0SShawn Lin BUG_ON(!PAGE_ALIGNED(addr)); 2177db64fe02SNick Piggin 2178d98c9e83SAndrey Ryabinin kasan_poison_vmalloc(mem, size); 2179d98c9e83SAndrey Ryabinin 21809c3acf60SChristoph Hellwig if (likely(count <= VMAP_MAX_ALLOC)) { 218105e3ff95SChintan Pandya debug_check_no_locks_freed(mem, size); 218278a0e8c4SChristoph Hellwig vb_free(addr, size); 21839c3acf60SChristoph Hellwig return; 21849c3acf60SChristoph Hellwig } 21859c3acf60SChristoph Hellwig 21869c3acf60SChristoph Hellwig va = find_vmap_area(addr); 21879c3acf60SChristoph Hellwig BUG_ON(!va); 218805e3ff95SChintan Pandya debug_check_no_locks_freed((void *)va->va_start, 218905e3ff95SChintan Pandya (va->va_end - va->va_start)); 21909c3acf60SChristoph Hellwig free_unmap_vmap_area(va); 2191db64fe02SNick Piggin } 2192db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram); 2193db64fe02SNick Piggin 2194db64fe02SNick Piggin /** 2195db64fe02SNick Piggin * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) 2196db64fe02SNick Piggin * @pages: an array of pointers to the pages to be mapped 2197db64fe02SNick Piggin * @count: number of pages 2198db64fe02SNick Piggin * @node: prefer to allocate data structures on this node 2199e99c97adSRandy Dunlap * 220036437638SGioh Kim * If you use this function for less than VMAP_MAX_ALLOC pages, it could be 220136437638SGioh Kim * faster than vmap so it's good. But if you mix long-life and short-life 220236437638SGioh Kim * objects with vm_map_ram(), it could consume lots of address space through 220336437638SGioh Kim * fragmentation (especially on a 32bit machine). You could see failures in 220436437638SGioh Kim * the end. Please use this function for short-lived objects. 220536437638SGioh Kim * 2206e99c97adSRandy Dunlap * Returns: a pointer to the address that has been mapped, or %NULL on failure 2207db64fe02SNick Piggin */ 2208d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node) 2209db64fe02SNick Piggin { 221065ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 2211db64fe02SNick Piggin unsigned long addr; 2212db64fe02SNick Piggin void *mem; 2213db64fe02SNick Piggin 2214db64fe02SNick Piggin if (likely(count <= VMAP_MAX_ALLOC)) { 2215db64fe02SNick Piggin mem = vb_alloc(size, GFP_KERNEL); 2216db64fe02SNick Piggin if (IS_ERR(mem)) 2217db64fe02SNick Piggin return NULL; 2218db64fe02SNick Piggin addr = (unsigned long)mem; 2219db64fe02SNick Piggin } else { 2220db64fe02SNick Piggin struct vmap_area *va; 2221db64fe02SNick Piggin va = alloc_vmap_area(size, PAGE_SIZE, 2222db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); 2223db64fe02SNick Piggin if (IS_ERR(va)) 2224db64fe02SNick Piggin return NULL; 2225db64fe02SNick Piggin 2226db64fe02SNick Piggin addr = va->va_start; 2227db64fe02SNick Piggin mem = (void *)addr; 2228db64fe02SNick Piggin } 2229d98c9e83SAndrey Ryabinin 2230d98c9e83SAndrey Ryabinin kasan_unpoison_vmalloc(mem, size); 2231d98c9e83SAndrey Ryabinin 2232b67177ecSNicholas Piggin if (vmap_pages_range(addr, addr + size, PAGE_KERNEL, 2233b67177ecSNicholas Piggin pages, PAGE_SHIFT) < 0) { 2234db64fe02SNick Piggin vm_unmap_ram(mem, count); 2235db64fe02SNick Piggin return NULL; 2236db64fe02SNick Piggin } 2237b67177ecSNicholas Piggin 2238db64fe02SNick Piggin return mem; 2239db64fe02SNick Piggin } 2240db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram); 2241db64fe02SNick Piggin 22424341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata; 224392eac168SMike Rapoport 2244121e6f32SNicholas Piggin static inline unsigned int vm_area_page_order(struct vm_struct *vm) 2245121e6f32SNicholas Piggin { 2246121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 2247121e6f32SNicholas Piggin return vm->page_order; 2248121e6f32SNicholas Piggin #else 2249121e6f32SNicholas Piggin return 0; 2250121e6f32SNicholas Piggin #endif 2251121e6f32SNicholas Piggin } 2252121e6f32SNicholas Piggin 2253121e6f32SNicholas Piggin static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int order) 2254121e6f32SNicholas Piggin { 2255121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 2256121e6f32SNicholas Piggin vm->page_order = order; 2257121e6f32SNicholas Piggin #else 2258121e6f32SNicholas Piggin BUG_ON(order != 0); 2259121e6f32SNicholas Piggin #endif 2260121e6f32SNicholas Piggin } 2261121e6f32SNicholas Piggin 2262f0aa6617STejun Heo /** 2263be9b7335SNicolas Pitre * vm_area_add_early - add vmap area early during boot 2264be9b7335SNicolas Pitre * @vm: vm_struct to add 2265be9b7335SNicolas Pitre * 2266be9b7335SNicolas Pitre * This function is used to add fixed kernel vm area to vmlist before 2267be9b7335SNicolas Pitre * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags 2268be9b7335SNicolas Pitre * should contain proper values and the other fields should be zero. 2269be9b7335SNicolas Pitre * 2270be9b7335SNicolas Pitre * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 2271be9b7335SNicolas Pitre */ 2272be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm) 2273be9b7335SNicolas Pitre { 2274be9b7335SNicolas Pitre struct vm_struct *tmp, **p; 2275be9b7335SNicolas Pitre 2276be9b7335SNicolas Pitre BUG_ON(vmap_initialized); 2277be9b7335SNicolas Pitre for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { 2278be9b7335SNicolas Pitre if (tmp->addr >= vm->addr) { 2279be9b7335SNicolas Pitre BUG_ON(tmp->addr < vm->addr + vm->size); 2280be9b7335SNicolas Pitre break; 2281be9b7335SNicolas Pitre } else 2282be9b7335SNicolas Pitre BUG_ON(tmp->addr + tmp->size > vm->addr); 2283be9b7335SNicolas Pitre } 2284be9b7335SNicolas Pitre vm->next = *p; 2285be9b7335SNicolas Pitre *p = vm; 2286be9b7335SNicolas Pitre } 2287be9b7335SNicolas Pitre 2288be9b7335SNicolas Pitre /** 2289f0aa6617STejun Heo * vm_area_register_early - register vmap area early during boot 2290f0aa6617STejun Heo * @vm: vm_struct to register 2291c0c0a293STejun Heo * @align: requested alignment 2292f0aa6617STejun Heo * 2293f0aa6617STejun Heo * This function is used to register kernel vm area before 2294f0aa6617STejun Heo * vmalloc_init() is called. @vm->size and @vm->flags should contain 2295f0aa6617STejun Heo * proper values on entry and other fields should be zero. On return, 2296f0aa6617STejun Heo * vm->addr contains the allocated address. 2297f0aa6617STejun Heo * 2298f0aa6617STejun Heo * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 2299f0aa6617STejun Heo */ 2300c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align) 2301f0aa6617STejun Heo { 23020eb68437SKefeng Wang unsigned long addr = ALIGN(VMALLOC_START, align); 23030eb68437SKefeng Wang struct vm_struct *cur, **p; 2304f0aa6617STejun Heo 23050eb68437SKefeng Wang BUG_ON(vmap_initialized); 2306c0c0a293STejun Heo 23070eb68437SKefeng Wang for (p = &vmlist; (cur = *p) != NULL; p = &cur->next) { 23080eb68437SKefeng Wang if ((unsigned long)cur->addr - addr >= vm->size) 23090eb68437SKefeng Wang break; 23100eb68437SKefeng Wang addr = ALIGN((unsigned long)cur->addr + cur->size, align); 23110eb68437SKefeng Wang } 23120eb68437SKefeng Wang 23130eb68437SKefeng Wang BUG_ON(addr > VMALLOC_END - vm->size); 2314c0c0a293STejun Heo vm->addr = (void *)addr; 23150eb68437SKefeng Wang vm->next = *p; 23160eb68437SKefeng Wang *p = vm; 23173252b1d8SKefeng Wang kasan_populate_early_vm_area_shadow(vm->addr, vm->size); 2318f0aa6617STejun Heo } 2319f0aa6617STejun Heo 232068ad4a33SUladzislau Rezki (Sony) static void vmap_init_free_space(void) 232168ad4a33SUladzislau Rezki (Sony) { 232268ad4a33SUladzislau Rezki (Sony) unsigned long vmap_start = 1; 232368ad4a33SUladzislau Rezki (Sony) const unsigned long vmap_end = ULONG_MAX; 232468ad4a33SUladzislau Rezki (Sony) struct vmap_area *busy, *free; 232568ad4a33SUladzislau Rezki (Sony) 232668ad4a33SUladzislau Rezki (Sony) /* 232768ad4a33SUladzislau Rezki (Sony) * B F B B B F 232868ad4a33SUladzislau Rezki (Sony) * -|-----|.....|-----|-----|-----|.....|- 232968ad4a33SUladzislau Rezki (Sony) * | The KVA space | 233068ad4a33SUladzislau Rezki (Sony) * |<--------------------------------->| 233168ad4a33SUladzislau Rezki (Sony) */ 233268ad4a33SUladzislau Rezki (Sony) list_for_each_entry(busy, &vmap_area_list, list) { 233368ad4a33SUladzislau Rezki (Sony) if (busy->va_start - vmap_start > 0) { 233468ad4a33SUladzislau Rezki (Sony) free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 233568ad4a33SUladzislau Rezki (Sony) if (!WARN_ON_ONCE(!free)) { 233668ad4a33SUladzislau Rezki (Sony) free->va_start = vmap_start; 233768ad4a33SUladzislau Rezki (Sony) free->va_end = busy->va_start; 233868ad4a33SUladzislau Rezki (Sony) 233968ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(free, NULL, 234068ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, 234168ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list); 234268ad4a33SUladzislau Rezki (Sony) } 234368ad4a33SUladzislau Rezki (Sony) } 234468ad4a33SUladzislau Rezki (Sony) 234568ad4a33SUladzislau Rezki (Sony) vmap_start = busy->va_end; 234668ad4a33SUladzislau Rezki (Sony) } 234768ad4a33SUladzislau Rezki (Sony) 234868ad4a33SUladzislau Rezki (Sony) if (vmap_end - vmap_start > 0) { 234968ad4a33SUladzislau Rezki (Sony) free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 235068ad4a33SUladzislau Rezki (Sony) if (!WARN_ON_ONCE(!free)) { 235168ad4a33SUladzislau Rezki (Sony) free->va_start = vmap_start; 235268ad4a33SUladzislau Rezki (Sony) free->va_end = vmap_end; 235368ad4a33SUladzislau Rezki (Sony) 235468ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(free, NULL, 235568ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, 235668ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list); 235768ad4a33SUladzislau Rezki (Sony) } 235868ad4a33SUladzislau Rezki (Sony) } 235968ad4a33SUladzislau Rezki (Sony) } 236068ad4a33SUladzislau Rezki (Sony) 2361db64fe02SNick Piggin void __init vmalloc_init(void) 2362db64fe02SNick Piggin { 2363822c18f2SIvan Kokshaysky struct vmap_area *va; 2364822c18f2SIvan Kokshaysky struct vm_struct *tmp; 2365db64fe02SNick Piggin int i; 2366db64fe02SNick Piggin 236768ad4a33SUladzislau Rezki (Sony) /* 236868ad4a33SUladzislau Rezki (Sony) * Create the cache for vmap_area objects. 236968ad4a33SUladzislau Rezki (Sony) */ 237068ad4a33SUladzislau Rezki (Sony) vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC); 237168ad4a33SUladzislau Rezki (Sony) 2372db64fe02SNick Piggin for_each_possible_cpu(i) { 2373db64fe02SNick Piggin struct vmap_block_queue *vbq; 237432fcfd40SAl Viro struct vfree_deferred *p; 2375db64fe02SNick Piggin 2376db64fe02SNick Piggin vbq = &per_cpu(vmap_block_queue, i); 2377db64fe02SNick Piggin spin_lock_init(&vbq->lock); 2378db64fe02SNick Piggin INIT_LIST_HEAD(&vbq->free); 237932fcfd40SAl Viro p = &per_cpu(vfree_deferred, i); 238032fcfd40SAl Viro init_llist_head(&p->list); 238132fcfd40SAl Viro INIT_WORK(&p->wq, free_work); 2382db64fe02SNick Piggin } 23839b463334SJeremy Fitzhardinge 2384822c18f2SIvan Kokshaysky /* Import existing vmlist entries. */ 2385822c18f2SIvan Kokshaysky for (tmp = vmlist; tmp; tmp = tmp->next) { 238668ad4a33SUladzislau Rezki (Sony) va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 238768ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(!va)) 238868ad4a33SUladzislau Rezki (Sony) continue; 238968ad4a33SUladzislau Rezki (Sony) 2390822c18f2SIvan Kokshaysky va->va_start = (unsigned long)tmp->addr; 2391822c18f2SIvan Kokshaysky va->va_end = va->va_start + tmp->size; 2392dbda591dSKyongHo va->vm = tmp; 239368ad4a33SUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 2394822c18f2SIvan Kokshaysky } 2395ca23e405STejun Heo 239668ad4a33SUladzislau Rezki (Sony) /* 239768ad4a33SUladzislau Rezki (Sony) * Now we can initialize a free vmap space. 239868ad4a33SUladzislau Rezki (Sony) */ 239968ad4a33SUladzislau Rezki (Sony) vmap_init_free_space(); 24009b463334SJeremy Fitzhardinge vmap_initialized = true; 2401db64fe02SNick Piggin } 2402db64fe02SNick Piggin 2403e36176beSUladzislau Rezki (Sony) static inline void setup_vmalloc_vm_locked(struct vm_struct *vm, 2404e36176beSUladzislau Rezki (Sony) struct vmap_area *va, unsigned long flags, const void *caller) 2405cf88c790STejun Heo { 2406cf88c790STejun Heo vm->flags = flags; 2407cf88c790STejun Heo vm->addr = (void *)va->va_start; 2408cf88c790STejun Heo vm->size = va->va_end - va->va_start; 2409cf88c790STejun Heo vm->caller = caller; 2410db1aecafSMinchan Kim va->vm = vm; 2411e36176beSUladzislau Rezki (Sony) } 2412e36176beSUladzislau Rezki (Sony) 2413e36176beSUladzislau Rezki (Sony) static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, 2414e36176beSUladzislau Rezki (Sony) unsigned long flags, const void *caller) 2415e36176beSUladzislau Rezki (Sony) { 2416e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 2417e36176beSUladzislau Rezki (Sony) setup_vmalloc_vm_locked(vm, va, flags, caller); 2418c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 2419f5252e00SMitsuo Hayasaka } 2420cf88c790STejun Heo 242120fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm) 2422f5252e00SMitsuo Hayasaka { 2423d4033afdSJoonsoo Kim /* 242420fc02b4SZhang Yanfei * Before removing VM_UNINITIALIZED, 2425d4033afdSJoonsoo Kim * we should make sure that vm has proper values. 2426d4033afdSJoonsoo Kim * Pair with smp_rmb() in show_numa_info(). 2427d4033afdSJoonsoo Kim */ 2428d4033afdSJoonsoo Kim smp_wmb(); 242920fc02b4SZhang Yanfei vm->flags &= ~VM_UNINITIALIZED; 2430cf88c790STejun Heo } 2431cf88c790STejun Heo 2432db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size, 24337ca3027bSDaniel Axtens unsigned long align, unsigned long shift, unsigned long flags, 24347ca3027bSDaniel Axtens unsigned long start, unsigned long end, int node, 24357ca3027bSDaniel Axtens gfp_t gfp_mask, const void *caller) 2436db64fe02SNick Piggin { 24370006526dSKautuk Consul struct vmap_area *va; 2438db64fe02SNick Piggin struct vm_struct *area; 2439d98c9e83SAndrey Ryabinin unsigned long requested_size = size; 24401da177e4SLinus Torvalds 244152fd24caSGiridhar Pemmasani BUG_ON(in_interrupt()); 24427ca3027bSDaniel Axtens size = ALIGN(size, 1ul << shift); 244331be8309SOGAWA Hirofumi if (unlikely(!size)) 244431be8309SOGAWA Hirofumi return NULL; 24451da177e4SLinus Torvalds 2446252e5c6eSzijun_hu if (flags & VM_IOREMAP) 2447252e5c6eSzijun_hu align = 1ul << clamp_t(int, get_count_order_long(size), 2448252e5c6eSzijun_hu PAGE_SHIFT, IOREMAP_MAX_ORDER); 2449252e5c6eSzijun_hu 2450cf88c790STejun Heo area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); 24511da177e4SLinus Torvalds if (unlikely(!area)) 24521da177e4SLinus Torvalds return NULL; 24531da177e4SLinus Torvalds 245471394fe5SAndrey Ryabinin if (!(flags & VM_NO_GUARD)) 24551da177e4SLinus Torvalds size += PAGE_SIZE; 24561da177e4SLinus Torvalds 2457db64fe02SNick Piggin va = alloc_vmap_area(size, align, start, end, node, gfp_mask); 2458db64fe02SNick Piggin if (IS_ERR(va)) { 2459db64fe02SNick Piggin kfree(area); 2460db64fe02SNick Piggin return NULL; 24611da177e4SLinus Torvalds } 24621da177e4SLinus Torvalds 2463d98c9e83SAndrey Ryabinin kasan_unpoison_vmalloc((void *)va->va_start, requested_size); 2464f5252e00SMitsuo Hayasaka 2465d98c9e83SAndrey Ryabinin setup_vmalloc_vm(area, va, flags, caller); 24663c5c3cfbSDaniel Axtens 24671da177e4SLinus Torvalds return area; 24681da177e4SLinus Torvalds } 24691da177e4SLinus Torvalds 2470c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, 2471c2968612SBenjamin Herrenschmidt unsigned long start, unsigned long end, 24725e6cafc8SMarek Szyprowski const void *caller) 2473c2968612SBenjamin Herrenschmidt { 24747ca3027bSDaniel Axtens return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, start, end, 24757ca3027bSDaniel Axtens NUMA_NO_NODE, GFP_KERNEL, caller); 2476c2968612SBenjamin Herrenschmidt } 2477c2968612SBenjamin Herrenschmidt 24781da177e4SLinus Torvalds /** 2479183ff22bSSimon Arlott * get_vm_area - reserve a contiguous kernel virtual area 24801da177e4SLinus Torvalds * @size: size of the area 24811da177e4SLinus Torvalds * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC 24821da177e4SLinus Torvalds * 24831da177e4SLinus Torvalds * Search an area of @size in the kernel virtual mapping area, 24841da177e4SLinus Torvalds * and reserved it for out purposes. Returns the area descriptor 24851da177e4SLinus Torvalds * on success or %NULL on failure. 2486a862f68aSMike Rapoport * 2487a862f68aSMike Rapoport * Return: the area descriptor on success or %NULL on failure. 24881da177e4SLinus Torvalds */ 24891da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) 24901da177e4SLinus Torvalds { 24917ca3027bSDaniel Axtens return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, 24927ca3027bSDaniel Axtens VMALLOC_START, VMALLOC_END, 249300ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, 249400ef2d2fSDavid Rientjes __builtin_return_address(0)); 249523016969SChristoph Lameter } 249623016969SChristoph Lameter 249723016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, 24985e6cafc8SMarek Szyprowski const void *caller) 249923016969SChristoph Lameter { 25007ca3027bSDaniel Axtens return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, 25017ca3027bSDaniel Axtens VMALLOC_START, VMALLOC_END, 250200ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, caller); 25031da177e4SLinus Torvalds } 25041da177e4SLinus Torvalds 2505e9da6e99SMarek Szyprowski /** 2506e9da6e99SMarek Szyprowski * find_vm_area - find a continuous kernel virtual area 2507e9da6e99SMarek Szyprowski * @addr: base address 2508e9da6e99SMarek Szyprowski * 2509e9da6e99SMarek Szyprowski * Search for the kernel VM area starting at @addr, and return it. 2510e9da6e99SMarek Szyprowski * It is up to the caller to do all required locking to keep the returned 2511e9da6e99SMarek Szyprowski * pointer valid. 2512a862f68aSMike Rapoport * 251374640617SHui Su * Return: the area descriptor on success or %NULL on failure. 2514e9da6e99SMarek Szyprowski */ 2515e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr) 251683342314SNick Piggin { 2517db64fe02SNick Piggin struct vmap_area *va; 251883342314SNick Piggin 2519db64fe02SNick Piggin va = find_vmap_area((unsigned long)addr); 2520688fcbfcSPengfei Li if (!va) 25217856dfebSAndi Kleen return NULL; 2522688fcbfcSPengfei Li 2523688fcbfcSPengfei Li return va->vm; 25247856dfebSAndi Kleen } 25257856dfebSAndi Kleen 25261da177e4SLinus Torvalds /** 2527183ff22bSSimon Arlott * remove_vm_area - find and remove a continuous kernel virtual area 25281da177e4SLinus Torvalds * @addr: base address 25291da177e4SLinus Torvalds * 25301da177e4SLinus Torvalds * Search for the kernel VM area starting at @addr, and remove it. 25311da177e4SLinus Torvalds * This function returns the found VM area, but using it is NOT safe 25327856dfebSAndi Kleen * on SMP machines, except for its size or flags. 2533a862f68aSMike Rapoport * 253474640617SHui Su * Return: the area descriptor on success or %NULL on failure. 25351da177e4SLinus Torvalds */ 2536b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr) 25371da177e4SLinus Torvalds { 2538db64fe02SNick Piggin struct vmap_area *va; 2539db64fe02SNick Piggin 25405803ed29SChristoph Hellwig might_sleep(); 25415803ed29SChristoph Hellwig 2542dd3b8353SUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 2543dd3b8353SUladzislau Rezki (Sony) va = __find_vmap_area((unsigned long)addr); 2544688fcbfcSPengfei Li if (va && va->vm) { 2545db1aecafSMinchan Kim struct vm_struct *vm = va->vm; 2546f5252e00SMitsuo Hayasaka 2547c69480adSJoonsoo Kim va->vm = NULL; 2548c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 2549c69480adSJoonsoo Kim 2550a5af5aa8SAndrey Ryabinin kasan_free_shadow(vm); 2551dd32c279SKAMEZAWA Hiroyuki free_unmap_vmap_area(va); 2552dd32c279SKAMEZAWA Hiroyuki 2553db64fe02SNick Piggin return vm; 2554db64fe02SNick Piggin } 2555dd3b8353SUladzislau Rezki (Sony) 2556dd3b8353SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 2557db64fe02SNick Piggin return NULL; 25581da177e4SLinus Torvalds } 25591da177e4SLinus Torvalds 2560868b104dSRick Edgecombe static inline void set_area_direct_map(const struct vm_struct *area, 2561868b104dSRick Edgecombe int (*set_direct_map)(struct page *page)) 2562868b104dSRick Edgecombe { 2563868b104dSRick Edgecombe int i; 2564868b104dSRick Edgecombe 2565121e6f32SNicholas Piggin /* HUGE_VMALLOC passes small pages to set_direct_map */ 2566868b104dSRick Edgecombe for (i = 0; i < area->nr_pages; i++) 2567868b104dSRick Edgecombe if (page_address(area->pages[i])) 2568868b104dSRick Edgecombe set_direct_map(area->pages[i]); 2569868b104dSRick Edgecombe } 2570868b104dSRick Edgecombe 2571868b104dSRick Edgecombe /* Handle removing and resetting vm mappings related to the vm_struct. */ 2572868b104dSRick Edgecombe static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages) 2573868b104dSRick Edgecombe { 2574868b104dSRick Edgecombe unsigned long start = ULONG_MAX, end = 0; 2575121e6f32SNicholas Piggin unsigned int page_order = vm_area_page_order(area); 2576868b104dSRick Edgecombe int flush_reset = area->flags & VM_FLUSH_RESET_PERMS; 257731e67340SRick Edgecombe int flush_dmap = 0; 2578868b104dSRick Edgecombe int i; 2579868b104dSRick Edgecombe 2580868b104dSRick Edgecombe remove_vm_area(area->addr); 2581868b104dSRick Edgecombe 2582868b104dSRick Edgecombe /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */ 2583868b104dSRick Edgecombe if (!flush_reset) 2584868b104dSRick Edgecombe return; 2585868b104dSRick Edgecombe 2586868b104dSRick Edgecombe /* 2587868b104dSRick Edgecombe * If not deallocating pages, just do the flush of the VM area and 2588868b104dSRick Edgecombe * return. 2589868b104dSRick Edgecombe */ 2590868b104dSRick Edgecombe if (!deallocate_pages) { 2591868b104dSRick Edgecombe vm_unmap_aliases(); 2592868b104dSRick Edgecombe return; 2593868b104dSRick Edgecombe } 2594868b104dSRick Edgecombe 2595868b104dSRick Edgecombe /* 2596868b104dSRick Edgecombe * If execution gets here, flush the vm mapping and reset the direct 2597868b104dSRick Edgecombe * map. Find the start and end range of the direct mappings to make sure 2598868b104dSRick Edgecombe * the vm_unmap_aliases() flush includes the direct map. 2599868b104dSRick Edgecombe */ 2600121e6f32SNicholas Piggin for (i = 0; i < area->nr_pages; i += 1U << page_order) { 26018e41f872SRick Edgecombe unsigned long addr = (unsigned long)page_address(area->pages[i]); 26028e41f872SRick Edgecombe if (addr) { 2603121e6f32SNicholas Piggin unsigned long page_size; 2604121e6f32SNicholas Piggin 2605121e6f32SNicholas Piggin page_size = PAGE_SIZE << page_order; 2606868b104dSRick Edgecombe start = min(addr, start); 2607121e6f32SNicholas Piggin end = max(addr + page_size, end); 260831e67340SRick Edgecombe flush_dmap = 1; 2609868b104dSRick Edgecombe } 2610868b104dSRick Edgecombe } 2611868b104dSRick Edgecombe 2612868b104dSRick Edgecombe /* 2613868b104dSRick Edgecombe * Set direct map to something invalid so that it won't be cached if 2614868b104dSRick Edgecombe * there are any accesses after the TLB flush, then flush the TLB and 2615868b104dSRick Edgecombe * reset the direct map permissions to the default. 2616868b104dSRick Edgecombe */ 2617868b104dSRick Edgecombe set_area_direct_map(area, set_direct_map_invalid_noflush); 261831e67340SRick Edgecombe _vm_unmap_aliases(start, end, flush_dmap); 2619868b104dSRick Edgecombe set_area_direct_map(area, set_direct_map_default_noflush); 2620868b104dSRick Edgecombe } 2621868b104dSRick Edgecombe 2622b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages) 26231da177e4SLinus Torvalds { 26241da177e4SLinus Torvalds struct vm_struct *area; 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds if (!addr) 26271da177e4SLinus Torvalds return; 26281da177e4SLinus Torvalds 2629e69e9d4aSHATAYAMA Daisuke if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n", 2630ab15d9b4SDan Carpenter addr)) 26311da177e4SLinus Torvalds return; 26321da177e4SLinus Torvalds 26336ade2032SLiviu Dudau area = find_vm_area(addr); 26341da177e4SLinus Torvalds if (unlikely(!area)) { 26354c8573e2SArjan van de Ven WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", 26361da177e4SLinus Torvalds addr); 26371da177e4SLinus Torvalds return; 26381da177e4SLinus Torvalds } 26391da177e4SLinus Torvalds 264005e3ff95SChintan Pandya debug_check_no_locks_freed(area->addr, get_vm_area_size(area)); 264105e3ff95SChintan Pandya debug_check_no_obj_freed(area->addr, get_vm_area_size(area)); 26429a11b49aSIngo Molnar 2643c041098cSVincenzo Frascino kasan_poison_vmalloc(area->addr, get_vm_area_size(area)); 26443c5c3cfbSDaniel Axtens 2645868b104dSRick Edgecombe vm_remove_mappings(area, deallocate_pages); 2646868b104dSRick Edgecombe 26471da177e4SLinus Torvalds if (deallocate_pages) { 2648121e6f32SNicholas Piggin unsigned int page_order = vm_area_page_order(area); 26494e5aa1f4SShakeel Butt int i, step = 1U << page_order; 26501da177e4SLinus Torvalds 26514e5aa1f4SShakeel Butt for (i = 0; i < area->nr_pages; i += step) { 2652bf53d6f8SChristoph Lameter struct page *page = area->pages[i]; 2653bf53d6f8SChristoph Lameter 2654bf53d6f8SChristoph Lameter BUG_ON(!page); 26554e5aa1f4SShakeel Butt mod_memcg_page_state(page, MEMCG_VMALLOC, -step); 2656121e6f32SNicholas Piggin __free_pages(page, page_order); 2657a850e932SRafael Aquini cond_resched(); 26581da177e4SLinus Torvalds } 265997105f0aSRoman Gushchin atomic_long_sub(area->nr_pages, &nr_vmalloc_pages); 26601da177e4SLinus Torvalds 2661244d63eeSDavid Rientjes kvfree(area->pages); 26621da177e4SLinus Torvalds } 26631da177e4SLinus Torvalds 26641da177e4SLinus Torvalds kfree(area); 26651da177e4SLinus Torvalds } 26661da177e4SLinus Torvalds 2667bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr) 2668bf22e37aSAndrey Ryabinin { 2669bf22e37aSAndrey Ryabinin /* 2670bf22e37aSAndrey Ryabinin * Use raw_cpu_ptr() because this can be called from preemptible 2671bf22e37aSAndrey Ryabinin * context. Preemption is absolutely fine here, because the llist_add() 2672bf22e37aSAndrey Ryabinin * implementation is lockless, so it works even if we are adding to 267373221d88SJeongtae Park * another cpu's list. schedule_work() should be fine with this too. 2674bf22e37aSAndrey Ryabinin */ 2675bf22e37aSAndrey Ryabinin struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred); 2676bf22e37aSAndrey Ryabinin 2677bf22e37aSAndrey Ryabinin if (llist_add((struct llist_node *)addr, &p->list)) 2678bf22e37aSAndrey Ryabinin schedule_work(&p->wq); 2679bf22e37aSAndrey Ryabinin } 2680bf22e37aSAndrey Ryabinin 2681bf22e37aSAndrey Ryabinin /** 2682bf22e37aSAndrey Ryabinin * vfree_atomic - release memory allocated by vmalloc() 2683bf22e37aSAndrey Ryabinin * @addr: memory base address 2684bf22e37aSAndrey Ryabinin * 2685bf22e37aSAndrey Ryabinin * This one is just like vfree() but can be called in any atomic context 2686bf22e37aSAndrey Ryabinin * except NMIs. 2687bf22e37aSAndrey Ryabinin */ 2688bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr) 2689bf22e37aSAndrey Ryabinin { 2690bf22e37aSAndrey Ryabinin BUG_ON(in_nmi()); 2691bf22e37aSAndrey Ryabinin 2692bf22e37aSAndrey Ryabinin kmemleak_free(addr); 2693bf22e37aSAndrey Ryabinin 2694bf22e37aSAndrey Ryabinin if (!addr) 2695bf22e37aSAndrey Ryabinin return; 2696bf22e37aSAndrey Ryabinin __vfree_deferred(addr); 2697bf22e37aSAndrey Ryabinin } 2698bf22e37aSAndrey Ryabinin 2699c67dc624SRoman Penyaev static void __vfree(const void *addr) 2700c67dc624SRoman Penyaev { 2701c67dc624SRoman Penyaev if (unlikely(in_interrupt())) 2702c67dc624SRoman Penyaev __vfree_deferred(addr); 2703c67dc624SRoman Penyaev else 2704c67dc624SRoman Penyaev __vunmap(addr, 1); 2705c67dc624SRoman Penyaev } 2706c67dc624SRoman Penyaev 27071da177e4SLinus Torvalds /** 2708fa307474SMatthew Wilcox (Oracle) * vfree - Release memory allocated by vmalloc() 2709fa307474SMatthew Wilcox (Oracle) * @addr: Memory base address 27101da177e4SLinus Torvalds * 2711fa307474SMatthew Wilcox (Oracle) * Free the virtually continuous memory area starting at @addr, as obtained 2712fa307474SMatthew Wilcox (Oracle) * from one of the vmalloc() family of APIs. This will usually also free the 2713fa307474SMatthew Wilcox (Oracle) * physical memory underlying the virtual allocation, but that memory is 2714fa307474SMatthew Wilcox (Oracle) * reference counted, so it will not be freed until the last user goes away. 27151da177e4SLinus Torvalds * 2716fa307474SMatthew Wilcox (Oracle) * If @addr is NULL, no operation is performed. 271732fcfd40SAl Viro * 2718fa307474SMatthew Wilcox (Oracle) * Context: 27193ca4ea3aSAndrey Ryabinin * May sleep if called *not* from interrupt context. 2720fa307474SMatthew Wilcox (Oracle) * Must not be called in NMI context (strictly speaking, it could be 2721fa307474SMatthew Wilcox (Oracle) * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling 2722f0953a1bSIngo Molnar * conventions for vfree() arch-dependent would be a really bad idea). 27231da177e4SLinus Torvalds */ 2724b3bdda02SChristoph Lameter void vfree(const void *addr) 27251da177e4SLinus Torvalds { 272632fcfd40SAl Viro BUG_ON(in_nmi()); 272789219d37SCatalin Marinas 272889219d37SCatalin Marinas kmemleak_free(addr); 272989219d37SCatalin Marinas 2730a8dda165SAndrey Ryabinin might_sleep_if(!in_interrupt()); 2731a8dda165SAndrey Ryabinin 273232fcfd40SAl Viro if (!addr) 273332fcfd40SAl Viro return; 2734c67dc624SRoman Penyaev 2735c67dc624SRoman Penyaev __vfree(addr); 27361da177e4SLinus Torvalds } 27371da177e4SLinus Torvalds EXPORT_SYMBOL(vfree); 27381da177e4SLinus Torvalds 27391da177e4SLinus Torvalds /** 27401da177e4SLinus Torvalds * vunmap - release virtual mapping obtained by vmap() 27411da177e4SLinus Torvalds * @addr: memory base address 27421da177e4SLinus Torvalds * 27431da177e4SLinus Torvalds * Free the virtually contiguous memory area starting at @addr, 27441da177e4SLinus Torvalds * which was created from the page array passed to vmap(). 27451da177e4SLinus Torvalds * 274680e93effSPekka Enberg * Must not be called in interrupt context. 27471da177e4SLinus Torvalds */ 2748b3bdda02SChristoph Lameter void vunmap(const void *addr) 27491da177e4SLinus Torvalds { 27501da177e4SLinus Torvalds BUG_ON(in_interrupt()); 275134754b69SPeter Zijlstra might_sleep(); 275232fcfd40SAl Viro if (addr) 27531da177e4SLinus Torvalds __vunmap(addr, 0); 27541da177e4SLinus Torvalds } 27551da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap); 27561da177e4SLinus Torvalds 27571da177e4SLinus Torvalds /** 27581da177e4SLinus Torvalds * vmap - map an array of pages into virtually contiguous space 27591da177e4SLinus Torvalds * @pages: array of page pointers 27601da177e4SLinus Torvalds * @count: number of pages to map 27611da177e4SLinus Torvalds * @flags: vm_area->flags 27621da177e4SLinus Torvalds * @prot: page protection for the mapping 27631da177e4SLinus Torvalds * 2764b944afc9SChristoph Hellwig * Maps @count pages from @pages into contiguous kernel virtual space. 2765b944afc9SChristoph Hellwig * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself 2766b944afc9SChristoph Hellwig * (which must be kmalloc or vmalloc memory) and one reference per pages in it 2767b944afc9SChristoph Hellwig * are transferred from the caller to vmap(), and will be freed / dropped when 2768b944afc9SChristoph Hellwig * vfree() is called on the return value. 2769a862f68aSMike Rapoport * 2770a862f68aSMike Rapoport * Return: the address of the area or %NULL on failure 27711da177e4SLinus Torvalds */ 27721da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, 27731da177e4SLinus Torvalds unsigned long flags, pgprot_t prot) 27741da177e4SLinus Torvalds { 27751da177e4SLinus Torvalds struct vm_struct *area; 2776b67177ecSNicholas Piggin unsigned long addr; 277765ee03c4SGuillermo Julián Moreno unsigned long size; /* In bytes */ 27781da177e4SLinus Torvalds 277934754b69SPeter Zijlstra might_sleep(); 278034754b69SPeter Zijlstra 2781bd1a8fb2SPeter Zijlstra /* 2782bd1a8fb2SPeter Zijlstra * Your top guard is someone else's bottom guard. Not having a top 2783bd1a8fb2SPeter Zijlstra * guard compromises someone else's mappings too. 2784bd1a8fb2SPeter Zijlstra */ 2785bd1a8fb2SPeter Zijlstra if (WARN_ON_ONCE(flags & VM_NO_GUARD)) 2786bd1a8fb2SPeter Zijlstra flags &= ~VM_NO_GUARD; 2787bd1a8fb2SPeter Zijlstra 2788ca79b0c2SArun KS if (count > totalram_pages()) 27891da177e4SLinus Torvalds return NULL; 27901da177e4SLinus Torvalds 279165ee03c4SGuillermo Julián Moreno size = (unsigned long)count << PAGE_SHIFT; 279265ee03c4SGuillermo Julián Moreno area = get_vm_area_caller(size, flags, __builtin_return_address(0)); 27931da177e4SLinus Torvalds if (!area) 27941da177e4SLinus Torvalds return NULL; 279523016969SChristoph Lameter 2796b67177ecSNicholas Piggin addr = (unsigned long)area->addr; 2797b67177ecSNicholas Piggin if (vmap_pages_range(addr, addr + size, pgprot_nx(prot), 2798b67177ecSNicholas Piggin pages, PAGE_SHIFT) < 0) { 27991da177e4SLinus Torvalds vunmap(area->addr); 28001da177e4SLinus Torvalds return NULL; 28011da177e4SLinus Torvalds } 28021da177e4SLinus Torvalds 2803c22ee528SMiaohe Lin if (flags & VM_MAP_PUT_PAGES) { 2804b944afc9SChristoph Hellwig area->pages = pages; 2805c22ee528SMiaohe Lin area->nr_pages = count; 2806c22ee528SMiaohe Lin } 28071da177e4SLinus Torvalds return area->addr; 28081da177e4SLinus Torvalds } 28091da177e4SLinus Torvalds EXPORT_SYMBOL(vmap); 28101da177e4SLinus Torvalds 28113e9a9e25SChristoph Hellwig #ifdef CONFIG_VMAP_PFN 28123e9a9e25SChristoph Hellwig struct vmap_pfn_data { 28133e9a9e25SChristoph Hellwig unsigned long *pfns; 28143e9a9e25SChristoph Hellwig pgprot_t prot; 28153e9a9e25SChristoph Hellwig unsigned int idx; 28163e9a9e25SChristoph Hellwig }; 28173e9a9e25SChristoph Hellwig 28183e9a9e25SChristoph Hellwig static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private) 28193e9a9e25SChristoph Hellwig { 28203e9a9e25SChristoph Hellwig struct vmap_pfn_data *data = private; 28213e9a9e25SChristoph Hellwig 28223e9a9e25SChristoph Hellwig if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx]))) 28233e9a9e25SChristoph Hellwig return -EINVAL; 28243e9a9e25SChristoph Hellwig *pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot)); 28253e9a9e25SChristoph Hellwig return 0; 28263e9a9e25SChristoph Hellwig } 28273e9a9e25SChristoph Hellwig 28283e9a9e25SChristoph Hellwig /** 28293e9a9e25SChristoph Hellwig * vmap_pfn - map an array of PFNs into virtually contiguous space 28303e9a9e25SChristoph Hellwig * @pfns: array of PFNs 28313e9a9e25SChristoph Hellwig * @count: number of pages to map 28323e9a9e25SChristoph Hellwig * @prot: page protection for the mapping 28333e9a9e25SChristoph Hellwig * 28343e9a9e25SChristoph Hellwig * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns 28353e9a9e25SChristoph Hellwig * the start address of the mapping. 28363e9a9e25SChristoph Hellwig */ 28373e9a9e25SChristoph Hellwig void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot) 28383e9a9e25SChristoph Hellwig { 28393e9a9e25SChristoph Hellwig struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) }; 28403e9a9e25SChristoph Hellwig struct vm_struct *area; 28413e9a9e25SChristoph Hellwig 28423e9a9e25SChristoph Hellwig area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP, 28433e9a9e25SChristoph Hellwig __builtin_return_address(0)); 28443e9a9e25SChristoph Hellwig if (!area) 28453e9a9e25SChristoph Hellwig return NULL; 28463e9a9e25SChristoph Hellwig if (apply_to_page_range(&init_mm, (unsigned long)area->addr, 28473e9a9e25SChristoph Hellwig count * PAGE_SIZE, vmap_pfn_apply, &data)) { 28483e9a9e25SChristoph Hellwig free_vm_area(area); 28493e9a9e25SChristoph Hellwig return NULL; 28503e9a9e25SChristoph Hellwig } 28513e9a9e25SChristoph Hellwig return area->addr; 28523e9a9e25SChristoph Hellwig } 28533e9a9e25SChristoph Hellwig EXPORT_SYMBOL_GPL(vmap_pfn); 28543e9a9e25SChristoph Hellwig #endif /* CONFIG_VMAP_PFN */ 28553e9a9e25SChristoph Hellwig 285612b9f873SUladzislau Rezki static inline unsigned int 285712b9f873SUladzislau Rezki vm_area_alloc_pages(gfp_t gfp, int nid, 2858343ab817SUladzislau Rezki (Sony) unsigned int order, unsigned int nr_pages, struct page **pages) 285912b9f873SUladzislau Rezki { 286012b9f873SUladzislau Rezki unsigned int nr_allocated = 0; 2861ffb29b1cSChen Wandun struct page *page; 2862ffb29b1cSChen Wandun int i; 286312b9f873SUladzislau Rezki 286412b9f873SUladzislau Rezki /* 286512b9f873SUladzislau Rezki * For order-0 pages we make use of bulk allocator, if 286612b9f873SUladzislau Rezki * the page array is partly or not at all populated due 286712b9f873SUladzislau Rezki * to fails, fallback to a single page allocator that is 286812b9f873SUladzislau Rezki * more permissive. 286912b9f873SUladzislau Rezki */ 2870c00b6b96SChen Wandun if (!order) { 28719376130cSMichal Hocko gfp_t bulk_gfp = gfp & ~__GFP_NOFAIL; 28729376130cSMichal Hocko 2873343ab817SUladzislau Rezki (Sony) while (nr_allocated < nr_pages) { 2874343ab817SUladzislau Rezki (Sony) unsigned int nr, nr_pages_request; 2875343ab817SUladzislau Rezki (Sony) 2876343ab817SUladzislau Rezki (Sony) /* 2877343ab817SUladzislau Rezki (Sony) * A maximum allowed request is hard-coded and is 100 2878343ab817SUladzislau Rezki (Sony) * pages per call. That is done in order to prevent a 2879343ab817SUladzislau Rezki (Sony) * long preemption off scenario in the bulk-allocator 2880343ab817SUladzislau Rezki (Sony) * so the range is [1:100]. 2881343ab817SUladzislau Rezki (Sony) */ 2882343ab817SUladzislau Rezki (Sony) nr_pages_request = min(100U, nr_pages - nr_allocated); 2883343ab817SUladzislau Rezki (Sony) 2884c00b6b96SChen Wandun /* memory allocation should consider mempolicy, we can't 2885c00b6b96SChen Wandun * wrongly use nearest node when nid == NUMA_NO_NODE, 2886c00b6b96SChen Wandun * otherwise memory may be allocated in only one node, 2887c00b6b96SChen Wandun * but mempolcy want to alloc memory by interleaving. 2888c00b6b96SChen Wandun */ 2889c00b6b96SChen Wandun if (IS_ENABLED(CONFIG_NUMA) && nid == NUMA_NO_NODE) 28909376130cSMichal Hocko nr = alloc_pages_bulk_array_mempolicy(bulk_gfp, 2891c00b6b96SChen Wandun nr_pages_request, 2892c00b6b96SChen Wandun pages + nr_allocated); 2893c00b6b96SChen Wandun 2894c00b6b96SChen Wandun else 28959376130cSMichal Hocko nr = alloc_pages_bulk_array_node(bulk_gfp, nid, 2896c00b6b96SChen Wandun nr_pages_request, 2897c00b6b96SChen Wandun pages + nr_allocated); 2898343ab817SUladzislau Rezki (Sony) 2899343ab817SUladzislau Rezki (Sony) nr_allocated += nr; 2900343ab817SUladzislau Rezki (Sony) cond_resched(); 2901343ab817SUladzislau Rezki (Sony) 2902343ab817SUladzislau Rezki (Sony) /* 2903343ab817SUladzislau Rezki (Sony) * If zero or pages were obtained partly, 2904343ab817SUladzislau Rezki (Sony) * fallback to a single page allocator. 2905343ab817SUladzislau Rezki (Sony) */ 2906343ab817SUladzislau Rezki (Sony) if (nr != nr_pages_request) 2907343ab817SUladzislau Rezki (Sony) break; 2908343ab817SUladzislau Rezki (Sony) } 2909c00b6b96SChen Wandun } else 291012b9f873SUladzislau Rezki /* 291112b9f873SUladzislau Rezki * Compound pages required for remap_vmalloc_page if 291212b9f873SUladzislau Rezki * high-order pages. 291312b9f873SUladzislau Rezki */ 291412b9f873SUladzislau Rezki gfp |= __GFP_COMP; 291512b9f873SUladzislau Rezki 291612b9f873SUladzislau Rezki /* High-order pages or fallback path if "bulk" fails. */ 291712b9f873SUladzislau Rezki 2918ffb29b1cSChen Wandun while (nr_allocated < nr_pages) { 2919dd544141SVasily Averin if (fatal_signal_pending(current)) 2920dd544141SVasily Averin break; 2921dd544141SVasily Averin 2922ffb29b1cSChen Wandun if (nid == NUMA_NO_NODE) 2923ffb29b1cSChen Wandun page = alloc_pages(gfp, order); 2924ffb29b1cSChen Wandun else 292512b9f873SUladzislau Rezki page = alloc_pages_node(nid, gfp, order); 292612b9f873SUladzislau Rezki if (unlikely(!page)) 292712b9f873SUladzislau Rezki break; 292812b9f873SUladzislau Rezki 292912b9f873SUladzislau Rezki /* 293012b9f873SUladzislau Rezki * Careful, we allocate and map page-order pages, but 293112b9f873SUladzislau Rezki * tracking is done per PAGE_SIZE page so as to keep the 293212b9f873SUladzislau Rezki * vm_struct APIs independent of the physical/mapped size. 293312b9f873SUladzislau Rezki */ 293412b9f873SUladzislau Rezki for (i = 0; i < (1U << order); i++) 293512b9f873SUladzislau Rezki pages[nr_allocated + i] = page + i; 293612b9f873SUladzislau Rezki 293712b9f873SUladzislau Rezki cond_resched(); 293812b9f873SUladzislau Rezki nr_allocated += 1U << order; 293912b9f873SUladzislau Rezki } 294012b9f873SUladzislau Rezki 294112b9f873SUladzislau Rezki return nr_allocated; 294212b9f873SUladzislau Rezki } 294312b9f873SUladzislau Rezki 2944e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, 2945121e6f32SNicholas Piggin pgprot_t prot, unsigned int page_shift, 2946121e6f32SNicholas Piggin int node) 29471da177e4SLinus Torvalds { 2948930f036bSDavid Rientjes const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; 29499376130cSMichal Hocko bool nofail = gfp_mask & __GFP_NOFAIL; 2950121e6f32SNicholas Piggin unsigned long addr = (unsigned long)area->addr; 2951121e6f32SNicholas Piggin unsigned long size = get_vm_area_size(area); 295234fe6537SAndrew Morton unsigned long array_size; 2953121e6f32SNicholas Piggin unsigned int nr_small_pages = size >> PAGE_SHIFT; 2954121e6f32SNicholas Piggin unsigned int page_order; 2955451769ebSMichal Hocko unsigned int flags; 2956451769ebSMichal Hocko int ret; 29571da177e4SLinus Torvalds 2958121e6f32SNicholas Piggin array_size = (unsigned long)nr_small_pages * sizeof(struct page *); 2959f255935bSChristoph Hellwig gfp_mask |= __GFP_NOWARN; 2960f255935bSChristoph Hellwig if (!(gfp_mask & (GFP_DMA | GFP_DMA32))) 2961f255935bSChristoph Hellwig gfp_mask |= __GFP_HIGHMEM; 29621da177e4SLinus Torvalds 29631da177e4SLinus Torvalds /* Please note that the recursion is strictly bounded. */ 29648757d5faSJan Kiszka if (array_size > PAGE_SIZE) { 29655c1f4e69SUladzislau Rezki (Sony) area->pages = __vmalloc_node(array_size, 1, nested_gfp, node, 2966f255935bSChristoph Hellwig area->caller); 2967286e1ea3SAndrew Morton } else { 29685c1f4e69SUladzislau Rezki (Sony) area->pages = kmalloc_node(array_size, nested_gfp, node); 2969286e1ea3SAndrew Morton } 29707ea36242SAustin Kim 29715c1f4e69SUladzislau Rezki (Sony) if (!area->pages) { 2972*c3d77172SUladzislau Rezki (Sony) warn_alloc(gfp_mask, NULL, 2973f4bdfeafSUladzislau Rezki (Sony) "vmalloc error: size %lu, failed to allocated page array size %lu", 2974d70bec8cSNicholas Piggin nr_small_pages * PAGE_SIZE, array_size); 2975cd61413bSUladzislau Rezki (Sony) free_vm_area(area); 29761da177e4SLinus Torvalds return NULL; 29771da177e4SLinus Torvalds } 29781da177e4SLinus Torvalds 2979121e6f32SNicholas Piggin set_vm_area_page_order(area, page_shift - PAGE_SHIFT); 2980121e6f32SNicholas Piggin page_order = vm_area_page_order(area); 2981121e6f32SNicholas Piggin 2982*c3d77172SUladzislau Rezki (Sony) area->nr_pages = vm_area_alloc_pages(gfp_mask | __GFP_NOWARN, 2983*c3d77172SUladzislau Rezki (Sony) node, page_order, nr_small_pages, area->pages); 29845c1f4e69SUladzislau Rezki (Sony) 298597105f0aSRoman Gushchin atomic_long_add(area->nr_pages, &nr_vmalloc_pages); 29864e5aa1f4SShakeel Butt if (gfp_mask & __GFP_ACCOUNT) { 29874e5aa1f4SShakeel Butt int i, step = 1U << page_order; 29884e5aa1f4SShakeel Butt 29894e5aa1f4SShakeel Butt for (i = 0; i < area->nr_pages; i += step) 29904e5aa1f4SShakeel Butt mod_memcg_page_state(area->pages[i], MEMCG_VMALLOC, 29914e5aa1f4SShakeel Butt step); 29924e5aa1f4SShakeel Butt } 29935c1f4e69SUladzislau Rezki (Sony) 29945c1f4e69SUladzislau Rezki (Sony) /* 29955c1f4e69SUladzislau Rezki (Sony) * If not enough pages were obtained to accomplish an 29965c1f4e69SUladzislau Rezki (Sony) * allocation request, free them via __vfree() if any. 29975c1f4e69SUladzislau Rezki (Sony) */ 29985c1f4e69SUladzislau Rezki (Sony) if (area->nr_pages != nr_small_pages) { 2999*c3d77172SUladzislau Rezki (Sony) warn_alloc(gfp_mask, NULL, 3000f4bdfeafSUladzislau Rezki (Sony) "vmalloc error: size %lu, page order %u, failed to allocate pages", 3001d70bec8cSNicholas Piggin area->nr_pages * PAGE_SIZE, page_order); 30021da177e4SLinus Torvalds goto fail; 30031da177e4SLinus Torvalds } 3004121e6f32SNicholas Piggin 3005451769ebSMichal Hocko /* 3006451769ebSMichal Hocko * page tables allocations ignore external gfp mask, enforce it 3007451769ebSMichal Hocko * by the scope API 3008451769ebSMichal Hocko */ 3009451769ebSMichal Hocko if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO) 3010451769ebSMichal Hocko flags = memalloc_nofs_save(); 3011451769ebSMichal Hocko else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0) 3012451769ebSMichal Hocko flags = memalloc_noio_save(); 3013451769ebSMichal Hocko 30149376130cSMichal Hocko do { 3015451769ebSMichal Hocko ret = vmap_pages_range(addr, addr + size, prot, area->pages, 3016451769ebSMichal Hocko page_shift); 30179376130cSMichal Hocko if (nofail && (ret < 0)) 30189376130cSMichal Hocko schedule_timeout_uninterruptible(1); 30199376130cSMichal Hocko } while (nofail && (ret < 0)); 3020451769ebSMichal Hocko 3021451769ebSMichal Hocko if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO) 3022451769ebSMichal Hocko memalloc_nofs_restore(flags); 3023451769ebSMichal Hocko else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0) 3024451769ebSMichal Hocko memalloc_noio_restore(flags); 3025451769ebSMichal Hocko 3026451769ebSMichal Hocko if (ret < 0) { 3027*c3d77172SUladzislau Rezki (Sony) warn_alloc(gfp_mask, NULL, 3028f4bdfeafSUladzislau Rezki (Sony) "vmalloc error: size %lu, failed to map pages", 3029d70bec8cSNicholas Piggin area->nr_pages * PAGE_SIZE); 30301da177e4SLinus Torvalds goto fail; 3031d70bec8cSNicholas Piggin } 3032ed1f324cSChristoph Hellwig 30331da177e4SLinus Torvalds return area->addr; 30341da177e4SLinus Torvalds 30351da177e4SLinus Torvalds fail: 3036c67dc624SRoman Penyaev __vfree(area->addr); 30371da177e4SLinus Torvalds return NULL; 30381da177e4SLinus Torvalds } 30391da177e4SLinus Torvalds 3040d0a21265SDavid Rientjes /** 3041d0a21265SDavid Rientjes * __vmalloc_node_range - allocate virtually contiguous memory 3042d0a21265SDavid Rientjes * @size: allocation size 3043d0a21265SDavid Rientjes * @align: desired alignment 3044d0a21265SDavid Rientjes * @start: vm area range start 3045d0a21265SDavid Rientjes * @end: vm area range end 3046d0a21265SDavid Rientjes * @gfp_mask: flags for the page level allocator 3047d0a21265SDavid Rientjes * @prot: protection mask for the allocated pages 3048cb9e3c29SAndrey Ryabinin * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD) 304900ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 3050d0a21265SDavid Rientjes * @caller: caller's return address 3051d0a21265SDavid Rientjes * 3052d0a21265SDavid Rientjes * Allocate enough pages to cover @size from the page level 3053b7d90e7aSMichal Hocko * allocator with @gfp_mask flags. Please note that the full set of gfp 305430d3f011SMichal Hocko * flags are not supported. GFP_KERNEL, GFP_NOFS and GFP_NOIO are all 305530d3f011SMichal Hocko * supported. 305630d3f011SMichal Hocko * Zone modifiers are not supported. From the reclaim modifiers 305730d3f011SMichal Hocko * __GFP_DIRECT_RECLAIM is required (aka GFP_NOWAIT is not supported) 305830d3f011SMichal Hocko * and only __GFP_NOFAIL is supported (i.e. __GFP_NORETRY and 305930d3f011SMichal Hocko * __GFP_RETRY_MAYFAIL are not supported). 306030d3f011SMichal Hocko * 306130d3f011SMichal Hocko * __GFP_NOWARN can be used to suppress failures messages. 3062b7d90e7aSMichal Hocko * 3063b7d90e7aSMichal Hocko * Map them into contiguous kernel virtual space, using a pagetable 3064b7d90e7aSMichal Hocko * protection of @prot. 3065a862f68aSMike Rapoport * 3066a862f68aSMike Rapoport * Return: the address of the area or %NULL on failure 3067d0a21265SDavid Rientjes */ 3068d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align, 3069d0a21265SDavid Rientjes unsigned long start, unsigned long end, gfp_t gfp_mask, 3070cb9e3c29SAndrey Ryabinin pgprot_t prot, unsigned long vm_flags, int node, 3071cb9e3c29SAndrey Ryabinin const void *caller) 3072930fc45aSChristoph Lameter { 3073d0a21265SDavid Rientjes struct vm_struct *area; 3074d0a21265SDavid Rientjes void *addr; 3075d0a21265SDavid Rientjes unsigned long real_size = size; 3076121e6f32SNicholas Piggin unsigned long real_align = align; 3077121e6f32SNicholas Piggin unsigned int shift = PAGE_SHIFT; 3078d0a21265SDavid Rientjes 3079d70bec8cSNicholas Piggin if (WARN_ON_ONCE(!size)) 3080d70bec8cSNicholas Piggin return NULL; 3081d70bec8cSNicholas Piggin 3082d70bec8cSNicholas Piggin if ((size >> PAGE_SHIFT) > totalram_pages()) { 3083d70bec8cSNicholas Piggin warn_alloc(gfp_mask, NULL, 3084f4bdfeafSUladzislau Rezki (Sony) "vmalloc error: size %lu, exceeds total pages", 3085f4bdfeafSUladzislau Rezki (Sony) real_size); 3086d70bec8cSNicholas Piggin return NULL; 3087121e6f32SNicholas Piggin } 3088d0a21265SDavid Rientjes 30893382bbeeSChristophe Leroy if (vmap_allow_huge && !(vm_flags & VM_NO_HUGE_VMAP)) { 3090121e6f32SNicholas Piggin unsigned long size_per_node; 3091121e6f32SNicholas Piggin 3092121e6f32SNicholas Piggin /* 3093121e6f32SNicholas Piggin * Try huge pages. Only try for PAGE_KERNEL allocations, 3094121e6f32SNicholas Piggin * others like modules don't yet expect huge pages in 3095121e6f32SNicholas Piggin * their allocations due to apply_to_page_range not 3096121e6f32SNicholas Piggin * supporting them. 3097121e6f32SNicholas Piggin */ 3098121e6f32SNicholas Piggin 3099121e6f32SNicholas Piggin size_per_node = size; 3100121e6f32SNicholas Piggin if (node == NUMA_NO_NODE) 3101121e6f32SNicholas Piggin size_per_node /= num_online_nodes(); 31023382bbeeSChristophe Leroy if (arch_vmap_pmd_supported(prot) && size_per_node >= PMD_SIZE) 3103121e6f32SNicholas Piggin shift = PMD_SHIFT; 31043382bbeeSChristophe Leroy else 31053382bbeeSChristophe Leroy shift = arch_vmap_pte_supported_shift(size_per_node); 31063382bbeeSChristophe Leroy 3107121e6f32SNicholas Piggin align = max(real_align, 1UL << shift); 3108121e6f32SNicholas Piggin size = ALIGN(real_size, 1UL << shift); 3109121e6f32SNicholas Piggin } 3110121e6f32SNicholas Piggin 3111121e6f32SNicholas Piggin again: 31127ca3027bSDaniel Axtens area = __get_vm_area_node(real_size, align, shift, VM_ALLOC | 31137ca3027bSDaniel Axtens VM_UNINITIALIZED | vm_flags, start, end, node, 31147ca3027bSDaniel Axtens gfp_mask, caller); 3115d70bec8cSNicholas Piggin if (!area) { 31169376130cSMichal Hocko bool nofail = gfp_mask & __GFP_NOFAIL; 3117d70bec8cSNicholas Piggin warn_alloc(gfp_mask, NULL, 31189376130cSMichal Hocko "vmalloc error: size %lu, vm_struct allocation failed%s", 31199376130cSMichal Hocko real_size, (nofail) ? ". Retrying." : ""); 31209376130cSMichal Hocko if (nofail) { 31219376130cSMichal Hocko schedule_timeout_uninterruptible(1); 31229376130cSMichal Hocko goto again; 31239376130cSMichal Hocko } 3124de7d2b56SJoe Perches goto fail; 3125d70bec8cSNicholas Piggin } 3126d0a21265SDavid Rientjes 3127121e6f32SNicholas Piggin addr = __vmalloc_area_node(area, gfp_mask, prot, shift, node); 31281368edf0SMel Gorman if (!addr) 3129121e6f32SNicholas Piggin goto fail; 313089219d37SCatalin Marinas 313189219d37SCatalin Marinas /* 313220fc02b4SZhang Yanfei * In this function, newly allocated vm_struct has VM_UNINITIALIZED 313320fc02b4SZhang Yanfei * flag. It means that vm_struct is not fully initialized. 31344341fa45SJoonsoo Kim * Now, it is fully initialized, so remove this flag here. 3135f5252e00SMitsuo Hayasaka */ 313620fc02b4SZhang Yanfei clear_vm_uninitialized_flag(area); 3137f5252e00SMitsuo Hayasaka 31387ca3027bSDaniel Axtens size = PAGE_ALIGN(size); 313960115fa5SKefeng Wang if (!(vm_flags & VM_DEFER_KMEMLEAK)) 314094f4a161SCatalin Marinas kmemleak_vmalloc(area, size, gfp_mask); 314189219d37SCatalin Marinas 314289219d37SCatalin Marinas return addr; 3143de7d2b56SJoe Perches 3144de7d2b56SJoe Perches fail: 3145121e6f32SNicholas Piggin if (shift > PAGE_SHIFT) { 3146121e6f32SNicholas Piggin shift = PAGE_SHIFT; 3147121e6f32SNicholas Piggin align = real_align; 3148121e6f32SNicholas Piggin size = real_size; 3149121e6f32SNicholas Piggin goto again; 3150121e6f32SNicholas Piggin } 3151121e6f32SNicholas Piggin 3152de7d2b56SJoe Perches return NULL; 3153930fc45aSChristoph Lameter } 3154930fc45aSChristoph Lameter 31551da177e4SLinus Torvalds /** 3156930fc45aSChristoph Lameter * __vmalloc_node - allocate virtually contiguous memory 31571da177e4SLinus Torvalds * @size: allocation size 31582dca6999SDavid Miller * @align: desired alignment 31591da177e4SLinus Torvalds * @gfp_mask: flags for the page level allocator 316000ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 3161c85d194bSRandy Dunlap * @caller: caller's return address 31621da177e4SLinus Torvalds * 3163f38fcb9cSChristoph Hellwig * Allocate enough pages to cover @size from the page level allocator with 3164f38fcb9cSChristoph Hellwig * @gfp_mask flags. Map them into contiguous kernel virtual space. 3165a7c3e901SMichal Hocko * 3166dcda9b04SMichal Hocko * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL 3167a7c3e901SMichal Hocko * and __GFP_NOFAIL are not supported 3168a7c3e901SMichal Hocko * 3169a7c3e901SMichal Hocko * Any use of gfp flags outside of GFP_KERNEL should be consulted 3170a7c3e901SMichal Hocko * with mm people. 3171a862f68aSMike Rapoport * 3172a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 31731da177e4SLinus Torvalds */ 31742b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align, 3175f38fcb9cSChristoph Hellwig gfp_t gfp_mask, int node, const void *caller) 31761da177e4SLinus Torvalds { 3177d0a21265SDavid Rientjes return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, 3178f38fcb9cSChristoph Hellwig gfp_mask, PAGE_KERNEL, 0, node, caller); 31791da177e4SLinus Torvalds } 3180c3f896dcSChristoph Hellwig /* 3181c3f896dcSChristoph Hellwig * This is only for performance analysis of vmalloc and stress purpose. 3182c3f896dcSChristoph Hellwig * It is required by vmalloc test module, therefore do not use it other 3183c3f896dcSChristoph Hellwig * than that. 3184c3f896dcSChristoph Hellwig */ 3185c3f896dcSChristoph Hellwig #ifdef CONFIG_TEST_VMALLOC_MODULE 3186c3f896dcSChristoph Hellwig EXPORT_SYMBOL_GPL(__vmalloc_node); 3187c3f896dcSChristoph Hellwig #endif 31881da177e4SLinus Torvalds 318988dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask) 3190930fc45aSChristoph Lameter { 3191f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE, 319223016969SChristoph Lameter __builtin_return_address(0)); 3193930fc45aSChristoph Lameter } 31941da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc); 31951da177e4SLinus Torvalds 31961da177e4SLinus Torvalds /** 31971da177e4SLinus Torvalds * vmalloc - allocate virtually contiguous memory 31981da177e4SLinus Torvalds * @size: allocation size 319992eac168SMike Rapoport * 32001da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 32011da177e4SLinus Torvalds * allocator and map them into contiguous kernel virtual space. 32021da177e4SLinus Torvalds * 3203c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 32041da177e4SLinus Torvalds * use __vmalloc() instead. 3205a862f68aSMike Rapoport * 3206a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 32071da177e4SLinus Torvalds */ 32081da177e4SLinus Torvalds void *vmalloc(unsigned long size) 32091da177e4SLinus Torvalds { 32104d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE, 32114d39d728SChristoph Hellwig __builtin_return_address(0)); 32121da177e4SLinus Torvalds } 32131da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc); 32141da177e4SLinus Torvalds 3215930fc45aSChristoph Lameter /** 321615a64f5aSClaudio Imbrenda * vmalloc_no_huge - allocate virtually contiguous memory using small pages 321715a64f5aSClaudio Imbrenda * @size: allocation size 321815a64f5aSClaudio Imbrenda * 321915a64f5aSClaudio Imbrenda * Allocate enough non-huge pages to cover @size from the page level 322015a64f5aSClaudio Imbrenda * allocator and map them into contiguous kernel virtual space. 322115a64f5aSClaudio Imbrenda * 322215a64f5aSClaudio Imbrenda * Return: pointer to the allocated memory or %NULL on error 322315a64f5aSClaudio Imbrenda */ 322415a64f5aSClaudio Imbrenda void *vmalloc_no_huge(unsigned long size) 322515a64f5aSClaudio Imbrenda { 322615a64f5aSClaudio Imbrenda return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, 322715a64f5aSClaudio Imbrenda GFP_KERNEL, PAGE_KERNEL, VM_NO_HUGE_VMAP, 322815a64f5aSClaudio Imbrenda NUMA_NO_NODE, __builtin_return_address(0)); 322915a64f5aSClaudio Imbrenda } 323015a64f5aSClaudio Imbrenda EXPORT_SYMBOL(vmalloc_no_huge); 323115a64f5aSClaudio Imbrenda 323215a64f5aSClaudio Imbrenda /** 3233e1ca7788SDave Young * vzalloc - allocate virtually contiguous memory with zero fill 3234e1ca7788SDave Young * @size: allocation size 323592eac168SMike Rapoport * 3236e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 3237e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 3238e1ca7788SDave Young * The memory allocated is set to zero. 3239e1ca7788SDave Young * 3240e1ca7788SDave Young * For tight control over page level allocator and protection flags 3241e1ca7788SDave Young * use __vmalloc() instead. 3242a862f68aSMike Rapoport * 3243a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 3244e1ca7788SDave Young */ 3245e1ca7788SDave Young void *vzalloc(unsigned long size) 3246e1ca7788SDave Young { 32474d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE, 32484d39d728SChristoph Hellwig __builtin_return_address(0)); 3249e1ca7788SDave Young } 3250e1ca7788SDave Young EXPORT_SYMBOL(vzalloc); 3251e1ca7788SDave Young 3252e1ca7788SDave Young /** 3253ead04089SRolf Eike Beer * vmalloc_user - allocate zeroed virtually contiguous memory for userspace 325483342314SNick Piggin * @size: allocation size 3255ead04089SRolf Eike Beer * 3256ead04089SRolf Eike Beer * The resulting memory area is zeroed so it can be mapped to userspace 3257ead04089SRolf Eike Beer * without leaking data. 3258a862f68aSMike Rapoport * 3259a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 326083342314SNick Piggin */ 326183342314SNick Piggin void *vmalloc_user(unsigned long size) 326283342314SNick Piggin { 3263bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 3264bc84c535SRoman Penyaev GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL, 3265bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 326600ef2d2fSDavid Rientjes __builtin_return_address(0)); 326783342314SNick Piggin } 326883342314SNick Piggin EXPORT_SYMBOL(vmalloc_user); 326983342314SNick Piggin 327083342314SNick Piggin /** 3271930fc45aSChristoph Lameter * vmalloc_node - allocate memory on a specific node 3272930fc45aSChristoph Lameter * @size: allocation size 3273d44e0780SRandy Dunlap * @node: numa node 3274930fc45aSChristoph Lameter * 3275930fc45aSChristoph Lameter * Allocate enough pages to cover @size from the page level 3276930fc45aSChristoph Lameter * allocator and map them into contiguous kernel virtual space. 3277930fc45aSChristoph Lameter * 3278c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 3279930fc45aSChristoph Lameter * use __vmalloc() instead. 3280a862f68aSMike Rapoport * 3281a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 3282930fc45aSChristoph Lameter */ 3283930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node) 3284930fc45aSChristoph Lameter { 3285f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL, node, 3286f38fcb9cSChristoph Hellwig __builtin_return_address(0)); 3287930fc45aSChristoph Lameter } 3288930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node); 3289930fc45aSChristoph Lameter 3290e1ca7788SDave Young /** 3291e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill 3292e1ca7788SDave Young * @size: allocation size 3293e1ca7788SDave Young * @node: numa node 3294e1ca7788SDave Young * 3295e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 3296e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 3297e1ca7788SDave Young * The memory allocated is set to zero. 3298e1ca7788SDave Young * 3299a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 3300e1ca7788SDave Young */ 3301e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node) 3302e1ca7788SDave Young { 33034d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node, 33044d39d728SChristoph Hellwig __builtin_return_address(0)); 3305e1ca7788SDave Young } 3306e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node); 3307e1ca7788SDave Young 33080d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) 3309698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) 33100d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) 3311698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL) 33120d08e0d3SAndi Kleen #else 3313698d0831SMichal Hocko /* 3314698d0831SMichal Hocko * 64b systems should always have either DMA or DMA32 zones. For others 3315698d0831SMichal Hocko * GFP_DMA32 should do the right thing and use the normal zone. 3316698d0831SMichal Hocko */ 331768d68ff6SZhiyuan Dai #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) 33180d08e0d3SAndi Kleen #endif 33190d08e0d3SAndi Kleen 33201da177e4SLinus Torvalds /** 33211da177e4SLinus Torvalds * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 33221da177e4SLinus Torvalds * @size: allocation size 33231da177e4SLinus Torvalds * 33241da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 33251da177e4SLinus Torvalds * page level allocator and map them into contiguous kernel virtual space. 3326a862f68aSMike Rapoport * 3327a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 33281da177e4SLinus Torvalds */ 33291da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 33301da177e4SLinus Torvalds { 3331f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE, 3332f38fcb9cSChristoph Hellwig __builtin_return_address(0)); 33331da177e4SLinus Torvalds } 33341da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32); 33351da177e4SLinus Torvalds 333683342314SNick Piggin /** 3337ead04089SRolf Eike Beer * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 333883342314SNick Piggin * @size: allocation size 3339ead04089SRolf Eike Beer * 3340ead04089SRolf Eike Beer * The resulting memory area is 32bit addressable and zeroed so it can be 3341ead04089SRolf Eike Beer * mapped to userspace without leaking data. 3342a862f68aSMike Rapoport * 3343a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 334483342314SNick Piggin */ 334583342314SNick Piggin void *vmalloc_32_user(unsigned long size) 334683342314SNick Piggin { 3347bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 3348bc84c535SRoman Penyaev GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, 3349bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 33505a82ac71SRoman Penyaev __builtin_return_address(0)); 335183342314SNick Piggin } 335283342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user); 335383342314SNick Piggin 3354d0107eb0SKAMEZAWA Hiroyuki /* 3355d0107eb0SKAMEZAWA Hiroyuki * small helper routine , copy contents to buf from addr. 3356d0107eb0SKAMEZAWA Hiroyuki * If the page is not present, fill zero. 3357d0107eb0SKAMEZAWA Hiroyuki */ 3358d0107eb0SKAMEZAWA Hiroyuki 3359d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count) 3360d0107eb0SKAMEZAWA Hiroyuki { 3361d0107eb0SKAMEZAWA Hiroyuki struct page *p; 3362d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 3363d0107eb0SKAMEZAWA Hiroyuki 3364d0107eb0SKAMEZAWA Hiroyuki while (count) { 3365d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 3366d0107eb0SKAMEZAWA Hiroyuki 3367891c49abSAlexander Kuleshov offset = offset_in_page(addr); 3368d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 3369d0107eb0SKAMEZAWA Hiroyuki if (length > count) 3370d0107eb0SKAMEZAWA Hiroyuki length = count; 3371d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 3372d0107eb0SKAMEZAWA Hiroyuki /* 3373d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 3374d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 3375f0953a1bSIngo Molnar * overhead of vmalloc()/vfree() calls for this _debug_ 3376d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 3377d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 3378d0107eb0SKAMEZAWA Hiroyuki */ 3379d0107eb0SKAMEZAWA Hiroyuki if (p) { 3380f7c8ce44SDavid Hildenbrand /* We can expect USER0 is not used -- see vread() */ 33819b04c5feSCong Wang void *map = kmap_atomic(p); 3382d0107eb0SKAMEZAWA Hiroyuki memcpy(buf, map + offset, length); 33839b04c5feSCong Wang kunmap_atomic(map); 3384d0107eb0SKAMEZAWA Hiroyuki } else 3385d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, length); 3386d0107eb0SKAMEZAWA Hiroyuki 3387d0107eb0SKAMEZAWA Hiroyuki addr += length; 3388d0107eb0SKAMEZAWA Hiroyuki buf += length; 3389d0107eb0SKAMEZAWA Hiroyuki copied += length; 3390d0107eb0SKAMEZAWA Hiroyuki count -= length; 3391d0107eb0SKAMEZAWA Hiroyuki } 3392d0107eb0SKAMEZAWA Hiroyuki return copied; 3393d0107eb0SKAMEZAWA Hiroyuki } 3394d0107eb0SKAMEZAWA Hiroyuki 3395d0107eb0SKAMEZAWA Hiroyuki /** 3396d0107eb0SKAMEZAWA Hiroyuki * vread() - read vmalloc area in a safe way. 3397d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for reading data 3398d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 3399d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 3400d0107eb0SKAMEZAWA Hiroyuki * 3401d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 3402d0107eb0SKAMEZAWA Hiroyuki * copy data from that area to a given buffer. If the given memory range 3403d0107eb0SKAMEZAWA Hiroyuki * of [addr...addr+count) includes some valid address, data is copied to 3404d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, they'll be zero-filled. 3405d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 3406d0107eb0SKAMEZAWA Hiroyuki * 3407d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 3408a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 3409d0107eb0SKAMEZAWA Hiroyuki * 3410d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vread() is never necessary because the caller 3411d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 3412d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 3413bbcd53c9SDavid Hildenbrand * any information, as /proc/kcore. 3414a862f68aSMike Rapoport * 3415a862f68aSMike Rapoport * Return: number of bytes for which addr and buf should be increased 3416a862f68aSMike Rapoport * (same number as @count) or %0 if [addr...addr+count) doesn't 3417a862f68aSMike Rapoport * include any intersection with valid vmalloc area 3418d0107eb0SKAMEZAWA Hiroyuki */ 34191da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 34201da177e4SLinus Torvalds { 3421e81ce85fSJoonsoo Kim struct vmap_area *va; 3422e81ce85fSJoonsoo Kim struct vm_struct *vm; 34231da177e4SLinus Torvalds char *vaddr, *buf_start = buf; 3424d0107eb0SKAMEZAWA Hiroyuki unsigned long buflen = count; 34251da177e4SLinus Torvalds unsigned long n; 34261da177e4SLinus Torvalds 34271da177e4SLinus Torvalds /* Don't allow overflow */ 34281da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 34291da177e4SLinus Torvalds count = -(unsigned long) addr; 34301da177e4SLinus Torvalds 3431e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 3432f181234aSChen Wandun va = find_vmap_area_exceed_addr((unsigned long)addr); 3433f608788cSSerapheim Dimitropoulos if (!va) 3434f608788cSSerapheim Dimitropoulos goto finished; 3435f181234aSChen Wandun 3436f181234aSChen Wandun /* no intersects with alive vmap_area */ 3437f181234aSChen Wandun if ((unsigned long)addr + count <= va->va_start) 3438f181234aSChen Wandun goto finished; 3439f181234aSChen Wandun 3440f608788cSSerapheim Dimitropoulos list_for_each_entry_from(va, &vmap_area_list, list) { 3441e81ce85fSJoonsoo Kim if (!count) 3442e81ce85fSJoonsoo Kim break; 3443e81ce85fSJoonsoo Kim 3444688fcbfcSPengfei Li if (!va->vm) 3445e81ce85fSJoonsoo Kim continue; 3446e81ce85fSJoonsoo Kim 3447e81ce85fSJoonsoo Kim vm = va->vm; 3448e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 3449762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 34501da177e4SLinus Torvalds continue; 34511da177e4SLinus Torvalds while (addr < vaddr) { 34521da177e4SLinus Torvalds if (count == 0) 34531da177e4SLinus Torvalds goto finished; 34541da177e4SLinus Torvalds *buf = '\0'; 34551da177e4SLinus Torvalds buf++; 34561da177e4SLinus Torvalds addr++; 34571da177e4SLinus Torvalds count--; 34581da177e4SLinus Torvalds } 3459762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 3460d0107eb0SKAMEZAWA Hiroyuki if (n > count) 3461d0107eb0SKAMEZAWA Hiroyuki n = count; 3462e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) 3463d0107eb0SKAMEZAWA Hiroyuki aligned_vread(buf, addr, n); 3464d0107eb0SKAMEZAWA Hiroyuki else /* IOREMAP area is treated as memory hole */ 3465d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, n); 3466d0107eb0SKAMEZAWA Hiroyuki buf += n; 3467d0107eb0SKAMEZAWA Hiroyuki addr += n; 3468d0107eb0SKAMEZAWA Hiroyuki count -= n; 34691da177e4SLinus Torvalds } 34701da177e4SLinus Torvalds finished: 3471e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 3472d0107eb0SKAMEZAWA Hiroyuki 3473d0107eb0SKAMEZAWA Hiroyuki if (buf == buf_start) 3474d0107eb0SKAMEZAWA Hiroyuki return 0; 3475d0107eb0SKAMEZAWA Hiroyuki /* zero-fill memory holes */ 3476d0107eb0SKAMEZAWA Hiroyuki if (buf != buf_start + buflen) 3477d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, buflen - (buf - buf_start)); 3478d0107eb0SKAMEZAWA Hiroyuki 3479d0107eb0SKAMEZAWA Hiroyuki return buflen; 34801da177e4SLinus Torvalds } 34811da177e4SLinus Torvalds 3482d0107eb0SKAMEZAWA Hiroyuki /** 3483e69e9d4aSHATAYAMA Daisuke * remap_vmalloc_range_partial - map vmalloc pages to userspace 3484e69e9d4aSHATAYAMA Daisuke * @vma: vma to cover 3485e69e9d4aSHATAYAMA Daisuke * @uaddr: target user address to start at 3486e69e9d4aSHATAYAMA Daisuke * @kaddr: virtual address of vmalloc kernel memory 3487bdebd6a2SJann Horn * @pgoff: offset from @kaddr to start at 3488e69e9d4aSHATAYAMA Daisuke * @size: size of map area 3489e69e9d4aSHATAYAMA Daisuke * 3490e69e9d4aSHATAYAMA Daisuke * Returns: 0 for success, -Exxx on failure 3491e69e9d4aSHATAYAMA Daisuke * 3492e69e9d4aSHATAYAMA Daisuke * This function checks that @kaddr is a valid vmalloc'ed area, 3493e69e9d4aSHATAYAMA Daisuke * and that it is big enough to cover the range starting at 3494e69e9d4aSHATAYAMA Daisuke * @uaddr in @vma. Will return failure if that criteria isn't 3495e69e9d4aSHATAYAMA Daisuke * met. 3496e69e9d4aSHATAYAMA Daisuke * 3497e69e9d4aSHATAYAMA Daisuke * Similar to remap_pfn_range() (see mm/memory.c) 3498e69e9d4aSHATAYAMA Daisuke */ 3499e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr, 3500bdebd6a2SJann Horn void *kaddr, unsigned long pgoff, 3501bdebd6a2SJann Horn unsigned long size) 3502e69e9d4aSHATAYAMA Daisuke { 3503e69e9d4aSHATAYAMA Daisuke struct vm_struct *area; 3504bdebd6a2SJann Horn unsigned long off; 3505bdebd6a2SJann Horn unsigned long end_index; 3506bdebd6a2SJann Horn 3507bdebd6a2SJann Horn if (check_shl_overflow(pgoff, PAGE_SHIFT, &off)) 3508bdebd6a2SJann Horn return -EINVAL; 3509e69e9d4aSHATAYAMA Daisuke 3510e69e9d4aSHATAYAMA Daisuke size = PAGE_ALIGN(size); 3511e69e9d4aSHATAYAMA Daisuke 3512e69e9d4aSHATAYAMA Daisuke if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr)) 3513e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3514e69e9d4aSHATAYAMA Daisuke 3515e69e9d4aSHATAYAMA Daisuke area = find_vm_area(kaddr); 3516e69e9d4aSHATAYAMA Daisuke if (!area) 3517e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3518e69e9d4aSHATAYAMA Daisuke 3519fe9041c2SChristoph Hellwig if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT))) 3520e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3521e69e9d4aSHATAYAMA Daisuke 3522bdebd6a2SJann Horn if (check_add_overflow(size, off, &end_index) || 3523bdebd6a2SJann Horn end_index > get_vm_area_size(area)) 3524e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3525bdebd6a2SJann Horn kaddr += off; 3526e69e9d4aSHATAYAMA Daisuke 3527e69e9d4aSHATAYAMA Daisuke do { 3528e69e9d4aSHATAYAMA Daisuke struct page *page = vmalloc_to_page(kaddr); 3529e69e9d4aSHATAYAMA Daisuke int ret; 3530e69e9d4aSHATAYAMA Daisuke 3531e69e9d4aSHATAYAMA Daisuke ret = vm_insert_page(vma, uaddr, page); 3532e69e9d4aSHATAYAMA Daisuke if (ret) 3533e69e9d4aSHATAYAMA Daisuke return ret; 3534e69e9d4aSHATAYAMA Daisuke 3535e69e9d4aSHATAYAMA Daisuke uaddr += PAGE_SIZE; 3536e69e9d4aSHATAYAMA Daisuke kaddr += PAGE_SIZE; 3537e69e9d4aSHATAYAMA Daisuke size -= PAGE_SIZE; 3538e69e9d4aSHATAYAMA Daisuke } while (size > 0); 3539e69e9d4aSHATAYAMA Daisuke 3540e69e9d4aSHATAYAMA Daisuke vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 3541e69e9d4aSHATAYAMA Daisuke 3542e69e9d4aSHATAYAMA Daisuke return 0; 3543e69e9d4aSHATAYAMA Daisuke } 3544e69e9d4aSHATAYAMA Daisuke 3545e69e9d4aSHATAYAMA Daisuke /** 354683342314SNick Piggin * remap_vmalloc_range - map vmalloc pages to userspace 354783342314SNick Piggin * @vma: vma to cover (map full range of vma) 354883342314SNick Piggin * @addr: vmalloc memory 354983342314SNick Piggin * @pgoff: number of pages into addr before first page to map 35507682486bSRandy Dunlap * 35517682486bSRandy Dunlap * Returns: 0 for success, -Exxx on failure 355283342314SNick Piggin * 355383342314SNick Piggin * This function checks that addr is a valid vmalloc'ed area, and 355483342314SNick Piggin * that it is big enough to cover the vma. Will return failure if 355583342314SNick Piggin * that criteria isn't met. 355683342314SNick Piggin * 355772fd4a35SRobert P. J. Day * Similar to remap_pfn_range() (see mm/memory.c) 355883342314SNick Piggin */ 355983342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 356083342314SNick Piggin unsigned long pgoff) 356183342314SNick Piggin { 3562e69e9d4aSHATAYAMA Daisuke return remap_vmalloc_range_partial(vma, vma->vm_start, 3563bdebd6a2SJann Horn addr, pgoff, 3564e69e9d4aSHATAYAMA Daisuke vma->vm_end - vma->vm_start); 356583342314SNick Piggin } 356683342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range); 356783342314SNick Piggin 35685f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area) 35695f4352fbSJeremy Fitzhardinge { 35705f4352fbSJeremy Fitzhardinge struct vm_struct *ret; 35715f4352fbSJeremy Fitzhardinge ret = remove_vm_area(area->addr); 35725f4352fbSJeremy Fitzhardinge BUG_ON(ret != area); 35735f4352fbSJeremy Fitzhardinge kfree(area); 35745f4352fbSJeremy Fitzhardinge } 35755f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area); 3576a10aa579SChristoph Lameter 35774f8b02b4STejun Heo #ifdef CONFIG_SMP 3578ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n) 3579ca23e405STejun Heo { 35804583e773SGeliang Tang return rb_entry_safe(n, struct vmap_area, rb_node); 3581ca23e405STejun Heo } 3582ca23e405STejun Heo 3583ca23e405STejun Heo /** 358468ad4a33SUladzislau Rezki (Sony) * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to 358568ad4a33SUladzislau Rezki (Sony) * @addr: target address 3586ca23e405STejun Heo * 358768ad4a33SUladzislau Rezki (Sony) * Returns: vmap_area if it is found. If there is no such area 358868ad4a33SUladzislau Rezki (Sony) * the first highest(reverse order) vmap_area is returned 358968ad4a33SUladzislau Rezki (Sony) * i.e. va->va_start < addr && va->va_end < addr or NULL 359068ad4a33SUladzislau Rezki (Sony) * if there are no any areas before @addr. 3591ca23e405STejun Heo */ 359268ad4a33SUladzislau Rezki (Sony) static struct vmap_area * 359368ad4a33SUladzislau Rezki (Sony) pvm_find_va_enclose_addr(unsigned long addr) 3594ca23e405STejun Heo { 359568ad4a33SUladzislau Rezki (Sony) struct vmap_area *va, *tmp; 359668ad4a33SUladzislau Rezki (Sony) struct rb_node *n; 359768ad4a33SUladzislau Rezki (Sony) 359868ad4a33SUladzislau Rezki (Sony) n = free_vmap_area_root.rb_node; 359968ad4a33SUladzislau Rezki (Sony) va = NULL; 3600ca23e405STejun Heo 3601ca23e405STejun Heo while (n) { 360268ad4a33SUladzislau Rezki (Sony) tmp = rb_entry(n, struct vmap_area, rb_node); 360368ad4a33SUladzislau Rezki (Sony) if (tmp->va_start <= addr) { 360468ad4a33SUladzislau Rezki (Sony) va = tmp; 360568ad4a33SUladzislau Rezki (Sony) if (tmp->va_end >= addr) 3606ca23e405STejun Heo break; 3607ca23e405STejun Heo 360868ad4a33SUladzislau Rezki (Sony) n = n->rb_right; 3609ca23e405STejun Heo } else { 361068ad4a33SUladzislau Rezki (Sony) n = n->rb_left; 3611ca23e405STejun Heo } 361268ad4a33SUladzislau Rezki (Sony) } 361368ad4a33SUladzislau Rezki (Sony) 361468ad4a33SUladzislau Rezki (Sony) return va; 3615ca23e405STejun Heo } 3616ca23e405STejun Heo 3617ca23e405STejun Heo /** 361868ad4a33SUladzislau Rezki (Sony) * pvm_determine_end_from_reverse - find the highest aligned address 361968ad4a33SUladzislau Rezki (Sony) * of free block below VMALLOC_END 362068ad4a33SUladzislau Rezki (Sony) * @va: 362168ad4a33SUladzislau Rezki (Sony) * in - the VA we start the search(reverse order); 362268ad4a33SUladzislau Rezki (Sony) * out - the VA with the highest aligned end address. 3623799fa85dSAlex Shi * @align: alignment for required highest address 3624ca23e405STejun Heo * 362568ad4a33SUladzislau Rezki (Sony) * Returns: determined end address within vmap_area 3626ca23e405STejun Heo */ 362768ad4a33SUladzislau Rezki (Sony) static unsigned long 362868ad4a33SUladzislau Rezki (Sony) pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align) 3629ca23e405STejun Heo { 363068ad4a33SUladzislau Rezki (Sony) unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 3631ca23e405STejun Heo unsigned long addr; 3632ca23e405STejun Heo 363368ad4a33SUladzislau Rezki (Sony) if (likely(*va)) { 363468ad4a33SUladzislau Rezki (Sony) list_for_each_entry_from_reverse((*va), 363568ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list, list) { 363668ad4a33SUladzislau Rezki (Sony) addr = min((*va)->va_end & ~(align - 1), vmalloc_end); 363768ad4a33SUladzislau Rezki (Sony) if ((*va)->va_start < addr) 363868ad4a33SUladzislau Rezki (Sony) return addr; 363968ad4a33SUladzislau Rezki (Sony) } 3640ca23e405STejun Heo } 3641ca23e405STejun Heo 364268ad4a33SUladzislau Rezki (Sony) return 0; 3643ca23e405STejun Heo } 3644ca23e405STejun Heo 3645ca23e405STejun Heo /** 3646ca23e405STejun Heo * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator 3647ca23e405STejun Heo * @offsets: array containing offset of each area 3648ca23e405STejun Heo * @sizes: array containing size of each area 3649ca23e405STejun Heo * @nr_vms: the number of areas to allocate 3650ca23e405STejun Heo * @align: alignment, all entries in @offsets and @sizes must be aligned to this 3651ca23e405STejun Heo * 3652ca23e405STejun Heo * Returns: kmalloc'd vm_struct pointer array pointing to allocated 3653ca23e405STejun Heo * vm_structs on success, %NULL on failure 3654ca23e405STejun Heo * 3655ca23e405STejun Heo * Percpu allocator wants to use congruent vm areas so that it can 3656ca23e405STejun Heo * maintain the offsets among percpu areas. This function allocates 3657ec3f64fcSDavid Rientjes * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to 3658ec3f64fcSDavid Rientjes * be scattered pretty far, distance between two areas easily going up 3659ec3f64fcSDavid Rientjes * to gigabytes. To avoid interacting with regular vmallocs, these 3660ec3f64fcSDavid Rientjes * areas are allocated from top. 3661ca23e405STejun Heo * 3662ca23e405STejun Heo * Despite its complicated look, this allocator is rather simple. It 366368ad4a33SUladzislau Rezki (Sony) * does everything top-down and scans free blocks from the end looking 366468ad4a33SUladzislau Rezki (Sony) * for matching base. While scanning, if any of the areas do not fit the 366568ad4a33SUladzislau Rezki (Sony) * base address is pulled down to fit the area. Scanning is repeated till 366668ad4a33SUladzislau Rezki (Sony) * all the areas fit and then all necessary data structures are inserted 366768ad4a33SUladzislau Rezki (Sony) * and the result is returned. 3668ca23e405STejun Heo */ 3669ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, 3670ca23e405STejun Heo const size_t *sizes, int nr_vms, 3671ec3f64fcSDavid Rientjes size_t align) 3672ca23e405STejun Heo { 3673ca23e405STejun Heo const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); 3674ca23e405STejun Heo const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 367568ad4a33SUladzislau Rezki (Sony) struct vmap_area **vas, *va; 3676ca23e405STejun Heo struct vm_struct **vms; 3677ca23e405STejun Heo int area, area2, last_area, term_area; 3678253a496dSDaniel Axtens unsigned long base, start, size, end, last_end, orig_start, orig_end; 3679ca23e405STejun Heo bool purged = false; 368068ad4a33SUladzislau Rezki (Sony) enum fit_type type; 3681ca23e405STejun Heo 3682ca23e405STejun Heo /* verify parameters and allocate data structures */ 3683891c49abSAlexander Kuleshov BUG_ON(offset_in_page(align) || !is_power_of_2(align)); 3684ca23e405STejun Heo for (last_area = 0, area = 0; area < nr_vms; area++) { 3685ca23e405STejun Heo start = offsets[area]; 3686ca23e405STejun Heo end = start + sizes[area]; 3687ca23e405STejun Heo 3688ca23e405STejun Heo /* is everything aligned properly? */ 3689ca23e405STejun Heo BUG_ON(!IS_ALIGNED(offsets[area], align)); 3690ca23e405STejun Heo BUG_ON(!IS_ALIGNED(sizes[area], align)); 3691ca23e405STejun Heo 3692ca23e405STejun Heo /* detect the area with the highest address */ 3693ca23e405STejun Heo if (start > offsets[last_area]) 3694ca23e405STejun Heo last_area = area; 3695ca23e405STejun Heo 3696c568da28SWei Yang for (area2 = area + 1; area2 < nr_vms; area2++) { 3697ca23e405STejun Heo unsigned long start2 = offsets[area2]; 3698ca23e405STejun Heo unsigned long end2 = start2 + sizes[area2]; 3699ca23e405STejun Heo 3700c568da28SWei Yang BUG_ON(start2 < end && start < end2); 3701ca23e405STejun Heo } 3702ca23e405STejun Heo } 3703ca23e405STejun Heo last_end = offsets[last_area] + sizes[last_area]; 3704ca23e405STejun Heo 3705ca23e405STejun Heo if (vmalloc_end - vmalloc_start < last_end) { 3706ca23e405STejun Heo WARN_ON(true); 3707ca23e405STejun Heo return NULL; 3708ca23e405STejun Heo } 3709ca23e405STejun Heo 37104d67d860SThomas Meyer vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL); 37114d67d860SThomas Meyer vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL); 3712ca23e405STejun Heo if (!vas || !vms) 3713f1db7afdSKautuk Consul goto err_free2; 3714ca23e405STejun Heo 3715ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 371668ad4a33SUladzislau Rezki (Sony) vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL); 3717ec3f64fcSDavid Rientjes vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL); 3718ca23e405STejun Heo if (!vas[area] || !vms[area]) 3719ca23e405STejun Heo goto err_free; 3720ca23e405STejun Heo } 3721ca23e405STejun Heo retry: 3722e36176beSUladzislau Rezki (Sony) spin_lock(&free_vmap_area_lock); 3723ca23e405STejun Heo 3724ca23e405STejun Heo /* start scanning - we scan from the top, begin with the last area */ 3725ca23e405STejun Heo area = term_area = last_area; 3726ca23e405STejun Heo start = offsets[area]; 3727ca23e405STejun Heo end = start + sizes[area]; 3728ca23e405STejun Heo 372968ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(vmalloc_end); 373068ad4a33SUladzislau Rezki (Sony) base = pvm_determine_end_from_reverse(&va, align) - end; 3731ca23e405STejun Heo 3732ca23e405STejun Heo while (true) { 3733ca23e405STejun Heo /* 3734ca23e405STejun Heo * base might have underflowed, add last_end before 3735ca23e405STejun Heo * comparing. 3736ca23e405STejun Heo */ 373768ad4a33SUladzislau Rezki (Sony) if (base + last_end < vmalloc_start + last_end) 373868ad4a33SUladzislau Rezki (Sony) goto overflow; 3739ca23e405STejun Heo 3740ca23e405STejun Heo /* 374168ad4a33SUladzislau Rezki (Sony) * Fitting base has not been found. 3742ca23e405STejun Heo */ 374368ad4a33SUladzislau Rezki (Sony) if (va == NULL) 374468ad4a33SUladzislau Rezki (Sony) goto overflow; 3745ca23e405STejun Heo 3746ca23e405STejun Heo /* 3747d8cc323dSQiujun Huang * If required width exceeds current VA block, move 37485336e52cSKuppuswamy Sathyanarayanan * base downwards and then recheck. 37495336e52cSKuppuswamy Sathyanarayanan */ 37505336e52cSKuppuswamy Sathyanarayanan if (base + end > va->va_end) { 37515336e52cSKuppuswamy Sathyanarayanan base = pvm_determine_end_from_reverse(&va, align) - end; 37525336e52cSKuppuswamy Sathyanarayanan term_area = area; 37535336e52cSKuppuswamy Sathyanarayanan continue; 37545336e52cSKuppuswamy Sathyanarayanan } 37555336e52cSKuppuswamy Sathyanarayanan 37565336e52cSKuppuswamy Sathyanarayanan /* 375768ad4a33SUladzislau Rezki (Sony) * If this VA does not fit, move base downwards and recheck. 3758ca23e405STejun Heo */ 37595336e52cSKuppuswamy Sathyanarayanan if (base + start < va->va_start) { 376068ad4a33SUladzislau Rezki (Sony) va = node_to_va(rb_prev(&va->rb_node)); 376168ad4a33SUladzislau Rezki (Sony) base = pvm_determine_end_from_reverse(&va, align) - end; 3762ca23e405STejun Heo term_area = area; 3763ca23e405STejun Heo continue; 3764ca23e405STejun Heo } 3765ca23e405STejun Heo 3766ca23e405STejun Heo /* 3767ca23e405STejun Heo * This area fits, move on to the previous one. If 3768ca23e405STejun Heo * the previous one is the terminal one, we're done. 3769ca23e405STejun Heo */ 3770ca23e405STejun Heo area = (area + nr_vms - 1) % nr_vms; 3771ca23e405STejun Heo if (area == term_area) 3772ca23e405STejun Heo break; 377368ad4a33SUladzislau Rezki (Sony) 3774ca23e405STejun Heo start = offsets[area]; 3775ca23e405STejun Heo end = start + sizes[area]; 377668ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(base + end); 3777ca23e405STejun Heo } 377868ad4a33SUladzislau Rezki (Sony) 3779ca23e405STejun Heo /* we've found a fitting base, insert all va's */ 3780ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 378168ad4a33SUladzislau Rezki (Sony) int ret; 3782ca23e405STejun Heo 378368ad4a33SUladzislau Rezki (Sony) start = base + offsets[area]; 378468ad4a33SUladzislau Rezki (Sony) size = sizes[area]; 378568ad4a33SUladzislau Rezki (Sony) 378668ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(start); 378768ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(va == NULL)) 378868ad4a33SUladzislau Rezki (Sony) /* It is a BUG(), but trigger recovery instead. */ 378968ad4a33SUladzislau Rezki (Sony) goto recovery; 379068ad4a33SUladzislau Rezki (Sony) 379168ad4a33SUladzislau Rezki (Sony) type = classify_va_fit_type(va, start, size); 379268ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(type == NOTHING_FIT)) 379368ad4a33SUladzislau Rezki (Sony) /* It is a BUG(), but trigger recovery instead. */ 379468ad4a33SUladzislau Rezki (Sony) goto recovery; 379568ad4a33SUladzislau Rezki (Sony) 379668ad4a33SUladzislau Rezki (Sony) ret = adjust_va_to_fit_type(va, start, size, type); 379768ad4a33SUladzislau Rezki (Sony) if (unlikely(ret)) 379868ad4a33SUladzislau Rezki (Sony) goto recovery; 379968ad4a33SUladzislau Rezki (Sony) 380068ad4a33SUladzislau Rezki (Sony) /* Allocated area. */ 380168ad4a33SUladzislau Rezki (Sony) va = vas[area]; 380268ad4a33SUladzislau Rezki (Sony) va->va_start = start; 380368ad4a33SUladzislau Rezki (Sony) va->va_end = start + size; 3804ca23e405STejun Heo } 3805ca23e405STejun Heo 3806e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 3807ca23e405STejun Heo 3808253a496dSDaniel Axtens /* populate the kasan shadow space */ 3809253a496dSDaniel Axtens for (area = 0; area < nr_vms; area++) { 3810253a496dSDaniel Axtens if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area])) 3811253a496dSDaniel Axtens goto err_free_shadow; 3812253a496dSDaniel Axtens 3813253a496dSDaniel Axtens kasan_unpoison_vmalloc((void *)vas[area]->va_start, 3814253a496dSDaniel Axtens sizes[area]); 3815253a496dSDaniel Axtens } 3816253a496dSDaniel Axtens 3817ca23e405STejun Heo /* insert all vm's */ 3818e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 3819e36176beSUladzislau Rezki (Sony) for (area = 0; area < nr_vms; area++) { 3820e36176beSUladzislau Rezki (Sony) insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list); 3821e36176beSUladzislau Rezki (Sony) 3822e36176beSUladzislau Rezki (Sony) setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC, 3823ca23e405STejun Heo pcpu_get_vm_areas); 3824e36176beSUladzislau Rezki (Sony) } 3825e36176beSUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 3826ca23e405STejun Heo 3827ca23e405STejun Heo kfree(vas); 3828ca23e405STejun Heo return vms; 3829ca23e405STejun Heo 383068ad4a33SUladzislau Rezki (Sony) recovery: 3831e36176beSUladzislau Rezki (Sony) /* 3832e36176beSUladzislau Rezki (Sony) * Remove previously allocated areas. There is no 3833e36176beSUladzislau Rezki (Sony) * need in removing these areas from the busy tree, 3834e36176beSUladzislau Rezki (Sony) * because they are inserted only on the final step 3835e36176beSUladzislau Rezki (Sony) * and when pcpu_get_vm_areas() is success. 3836e36176beSUladzislau Rezki (Sony) */ 383768ad4a33SUladzislau Rezki (Sony) while (area--) { 3838253a496dSDaniel Axtens orig_start = vas[area]->va_start; 3839253a496dSDaniel Axtens orig_end = vas[area]->va_end; 384096e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, 38413c5c3cfbSDaniel Axtens &free_vmap_area_list); 38429c801f61SUladzislau Rezki (Sony) if (va) 3843253a496dSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 3844253a496dSDaniel Axtens va->va_start, va->va_end); 384568ad4a33SUladzislau Rezki (Sony) vas[area] = NULL; 384668ad4a33SUladzislau Rezki (Sony) } 384768ad4a33SUladzislau Rezki (Sony) 384868ad4a33SUladzislau Rezki (Sony) overflow: 3849e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 385068ad4a33SUladzislau Rezki (Sony) if (!purged) { 385168ad4a33SUladzislau Rezki (Sony) purge_vmap_area_lazy(); 385268ad4a33SUladzislau Rezki (Sony) purged = true; 385368ad4a33SUladzislau Rezki (Sony) 385468ad4a33SUladzislau Rezki (Sony) /* Before "retry", check if we recover. */ 385568ad4a33SUladzislau Rezki (Sony) for (area = 0; area < nr_vms; area++) { 385668ad4a33SUladzislau Rezki (Sony) if (vas[area]) 385768ad4a33SUladzislau Rezki (Sony) continue; 385868ad4a33SUladzislau Rezki (Sony) 385968ad4a33SUladzislau Rezki (Sony) vas[area] = kmem_cache_zalloc( 386068ad4a33SUladzislau Rezki (Sony) vmap_area_cachep, GFP_KERNEL); 386168ad4a33SUladzislau Rezki (Sony) if (!vas[area]) 386268ad4a33SUladzislau Rezki (Sony) goto err_free; 386368ad4a33SUladzislau Rezki (Sony) } 386468ad4a33SUladzislau Rezki (Sony) 386568ad4a33SUladzislau Rezki (Sony) goto retry; 386668ad4a33SUladzislau Rezki (Sony) } 386768ad4a33SUladzislau Rezki (Sony) 3868ca23e405STejun Heo err_free: 3869ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 387068ad4a33SUladzislau Rezki (Sony) if (vas[area]) 387168ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, vas[area]); 387268ad4a33SUladzislau Rezki (Sony) 3873ca23e405STejun Heo kfree(vms[area]); 3874ca23e405STejun Heo } 3875f1db7afdSKautuk Consul err_free2: 3876ca23e405STejun Heo kfree(vas); 3877ca23e405STejun Heo kfree(vms); 3878ca23e405STejun Heo return NULL; 3879253a496dSDaniel Axtens 3880253a496dSDaniel Axtens err_free_shadow: 3881253a496dSDaniel Axtens spin_lock(&free_vmap_area_lock); 3882253a496dSDaniel Axtens /* 3883253a496dSDaniel Axtens * We release all the vmalloc shadows, even the ones for regions that 3884253a496dSDaniel Axtens * hadn't been successfully added. This relies on kasan_release_vmalloc 3885253a496dSDaniel Axtens * being able to tolerate this case. 3886253a496dSDaniel Axtens */ 3887253a496dSDaniel Axtens for (area = 0; area < nr_vms; area++) { 3888253a496dSDaniel Axtens orig_start = vas[area]->va_start; 3889253a496dSDaniel Axtens orig_end = vas[area]->va_end; 389096e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, 3891253a496dSDaniel Axtens &free_vmap_area_list); 38929c801f61SUladzislau Rezki (Sony) if (va) 3893253a496dSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 3894253a496dSDaniel Axtens va->va_start, va->va_end); 3895253a496dSDaniel Axtens vas[area] = NULL; 3896253a496dSDaniel Axtens kfree(vms[area]); 3897253a496dSDaniel Axtens } 3898253a496dSDaniel Axtens spin_unlock(&free_vmap_area_lock); 3899253a496dSDaniel Axtens kfree(vas); 3900253a496dSDaniel Axtens kfree(vms); 3901253a496dSDaniel Axtens return NULL; 3902ca23e405STejun Heo } 3903ca23e405STejun Heo 3904ca23e405STejun Heo /** 3905ca23e405STejun Heo * pcpu_free_vm_areas - free vmalloc areas for percpu allocator 3906ca23e405STejun Heo * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() 3907ca23e405STejun Heo * @nr_vms: the number of allocated areas 3908ca23e405STejun Heo * 3909ca23e405STejun Heo * Free vm_structs and the array allocated by pcpu_get_vm_areas(). 3910ca23e405STejun Heo */ 3911ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) 3912ca23e405STejun Heo { 3913ca23e405STejun Heo int i; 3914ca23e405STejun Heo 3915ca23e405STejun Heo for (i = 0; i < nr_vms; i++) 3916ca23e405STejun Heo free_vm_area(vms[i]); 3917ca23e405STejun Heo kfree(vms); 3918ca23e405STejun Heo } 39194f8b02b4STejun Heo #endif /* CONFIG_SMP */ 3920a10aa579SChristoph Lameter 39215bb1bb35SPaul E. McKenney #ifdef CONFIG_PRINTK 392298f18083SPaul E. McKenney bool vmalloc_dump_obj(void *object) 392398f18083SPaul E. McKenney { 392498f18083SPaul E. McKenney struct vm_struct *vm; 392598f18083SPaul E. McKenney void *objp = (void *)PAGE_ALIGN((unsigned long)object); 392698f18083SPaul E. McKenney 392798f18083SPaul E. McKenney vm = find_vm_area(objp); 392898f18083SPaul E. McKenney if (!vm) 392998f18083SPaul E. McKenney return false; 3930bd34dcd4SPaul E. McKenney pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n", 3931bd34dcd4SPaul E. McKenney vm->nr_pages, (unsigned long)vm->addr, vm->caller); 393298f18083SPaul E. McKenney return true; 393398f18083SPaul E. McKenney } 39345bb1bb35SPaul E. McKenney #endif 393598f18083SPaul E. McKenney 3936a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS 3937a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos) 3938e36176beSUladzislau Rezki (Sony) __acquires(&vmap_purge_lock) 3939d4033afdSJoonsoo Kim __acquires(&vmap_area_lock) 3940a10aa579SChristoph Lameter { 3941e36176beSUladzislau Rezki (Sony) mutex_lock(&vmap_purge_lock); 3942d4033afdSJoonsoo Kim spin_lock(&vmap_area_lock); 3943e36176beSUladzislau Rezki (Sony) 39443f500069Szijun_hu return seq_list_start(&vmap_area_list, *pos); 3945a10aa579SChristoph Lameter } 3946a10aa579SChristoph Lameter 3947a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos) 3948a10aa579SChristoph Lameter { 39493f500069Szijun_hu return seq_list_next(p, &vmap_area_list, pos); 3950a10aa579SChristoph Lameter } 3951a10aa579SChristoph Lameter 3952a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p) 3953d4033afdSJoonsoo Kim __releases(&vmap_area_lock) 39540a7dd4e9SWaiman Long __releases(&vmap_purge_lock) 3955a10aa579SChristoph Lameter { 3956d4033afdSJoonsoo Kim spin_unlock(&vmap_area_lock); 39570a7dd4e9SWaiman Long mutex_unlock(&vmap_purge_lock); 3958a10aa579SChristoph Lameter } 3959a10aa579SChristoph Lameter 3960a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v) 3961a47a126aSEric Dumazet { 3962e5adfffcSKirill A. Shutemov if (IS_ENABLED(CONFIG_NUMA)) { 3963a47a126aSEric Dumazet unsigned int nr, *counters = m->private; 396451e50b3aSEric Dumazet unsigned int step = 1U << vm_area_page_order(v); 3965a47a126aSEric Dumazet 3966a47a126aSEric Dumazet if (!counters) 3967a47a126aSEric Dumazet return; 3968a47a126aSEric Dumazet 3969af12346cSWanpeng Li if (v->flags & VM_UNINITIALIZED) 3970af12346cSWanpeng Li return; 39717e5b528bSDmitry Vyukov /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ 39727e5b528bSDmitry Vyukov smp_rmb(); 3973af12346cSWanpeng Li 3974a47a126aSEric Dumazet memset(counters, 0, nr_node_ids * sizeof(unsigned int)); 3975a47a126aSEric Dumazet 397651e50b3aSEric Dumazet for (nr = 0; nr < v->nr_pages; nr += step) 397751e50b3aSEric Dumazet counters[page_to_nid(v->pages[nr])] += step; 3978a47a126aSEric Dumazet for_each_node_state(nr, N_HIGH_MEMORY) 3979a47a126aSEric Dumazet if (counters[nr]) 3980a47a126aSEric Dumazet seq_printf(m, " N%u=%u", nr, counters[nr]); 3981a47a126aSEric Dumazet } 3982a47a126aSEric Dumazet } 3983a47a126aSEric Dumazet 3984dd3b8353SUladzislau Rezki (Sony) static void show_purge_info(struct seq_file *m) 3985dd3b8353SUladzislau Rezki (Sony) { 3986dd3b8353SUladzislau Rezki (Sony) struct vmap_area *va; 3987dd3b8353SUladzislau Rezki (Sony) 398896e2db45SUladzislau Rezki (Sony) spin_lock(&purge_vmap_area_lock); 398996e2db45SUladzislau Rezki (Sony) list_for_each_entry(va, &purge_vmap_area_list, list) { 3990dd3b8353SUladzislau Rezki (Sony) seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n", 3991dd3b8353SUladzislau Rezki (Sony) (void *)va->va_start, (void *)va->va_end, 3992dd3b8353SUladzislau Rezki (Sony) va->va_end - va->va_start); 3993dd3b8353SUladzislau Rezki (Sony) } 399496e2db45SUladzislau Rezki (Sony) spin_unlock(&purge_vmap_area_lock); 3995dd3b8353SUladzislau Rezki (Sony) } 3996dd3b8353SUladzislau Rezki (Sony) 3997a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p) 3998a10aa579SChristoph Lameter { 39993f500069Szijun_hu struct vmap_area *va; 4000d4033afdSJoonsoo Kim struct vm_struct *v; 4001d4033afdSJoonsoo Kim 40023f500069Szijun_hu va = list_entry(p, struct vmap_area, list); 40033f500069Szijun_hu 4004c2ce8c14SWanpeng Li /* 4005688fcbfcSPengfei Li * s_show can encounter race with remove_vm_area, !vm on behalf 4006688fcbfcSPengfei Li * of vmap area is being tear down or vm_map_ram allocation. 4007c2ce8c14SWanpeng Li */ 4008688fcbfcSPengfei Li if (!va->vm) { 4009dd3b8353SUladzislau Rezki (Sony) seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n", 401078c72746SYisheng Xie (void *)va->va_start, (void *)va->va_end, 4011dd3b8353SUladzislau Rezki (Sony) va->va_end - va->va_start); 401278c72746SYisheng Xie 40137cc7913eSEric Dumazet goto final; 401478c72746SYisheng Xie } 4015d4033afdSJoonsoo Kim 4016d4033afdSJoonsoo Kim v = va->vm; 4017a10aa579SChristoph Lameter 401845ec1690SKees Cook seq_printf(m, "0x%pK-0x%pK %7ld", 4019a10aa579SChristoph Lameter v->addr, v->addr + v->size, v->size); 4020a10aa579SChristoph Lameter 402162c70bceSJoe Perches if (v->caller) 402262c70bceSJoe Perches seq_printf(m, " %pS", v->caller); 402323016969SChristoph Lameter 4024a10aa579SChristoph Lameter if (v->nr_pages) 4025a10aa579SChristoph Lameter seq_printf(m, " pages=%d", v->nr_pages); 4026a10aa579SChristoph Lameter 4027a10aa579SChristoph Lameter if (v->phys_addr) 4028199eaa05SMiles Chen seq_printf(m, " phys=%pa", &v->phys_addr); 4029a10aa579SChristoph Lameter 4030a10aa579SChristoph Lameter if (v->flags & VM_IOREMAP) 4031f4527c90SFabian Frederick seq_puts(m, " ioremap"); 4032a10aa579SChristoph Lameter 4033a10aa579SChristoph Lameter if (v->flags & VM_ALLOC) 4034f4527c90SFabian Frederick seq_puts(m, " vmalloc"); 4035a10aa579SChristoph Lameter 4036a10aa579SChristoph Lameter if (v->flags & VM_MAP) 4037f4527c90SFabian Frederick seq_puts(m, " vmap"); 4038a10aa579SChristoph Lameter 4039a10aa579SChristoph Lameter if (v->flags & VM_USERMAP) 4040f4527c90SFabian Frederick seq_puts(m, " user"); 4041a10aa579SChristoph Lameter 4042fe9041c2SChristoph Hellwig if (v->flags & VM_DMA_COHERENT) 4043fe9041c2SChristoph Hellwig seq_puts(m, " dma-coherent"); 4044fe9041c2SChristoph Hellwig 4045244d63eeSDavid Rientjes if (is_vmalloc_addr(v->pages)) 4046f4527c90SFabian Frederick seq_puts(m, " vpages"); 4047a10aa579SChristoph Lameter 4048a47a126aSEric Dumazet show_numa_info(m, v); 4049a10aa579SChristoph Lameter seq_putc(m, '\n'); 4050dd3b8353SUladzislau Rezki (Sony) 4051dd3b8353SUladzislau Rezki (Sony) /* 405296e2db45SUladzislau Rezki (Sony) * As a final step, dump "unpurged" areas. 4053dd3b8353SUladzislau Rezki (Sony) */ 40547cc7913eSEric Dumazet final: 4055dd3b8353SUladzislau Rezki (Sony) if (list_is_last(&va->list, &vmap_area_list)) 4056dd3b8353SUladzislau Rezki (Sony) show_purge_info(m); 4057dd3b8353SUladzislau Rezki (Sony) 4058a10aa579SChristoph Lameter return 0; 4059a10aa579SChristoph Lameter } 4060a10aa579SChristoph Lameter 40615f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = { 4062a10aa579SChristoph Lameter .start = s_start, 4063a10aa579SChristoph Lameter .next = s_next, 4064a10aa579SChristoph Lameter .stop = s_stop, 4065a10aa579SChristoph Lameter .show = s_show, 4066a10aa579SChristoph Lameter }; 40675f6a6a9cSAlexey Dobriyan 40685f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void) 40695f6a6a9cSAlexey Dobriyan { 4070fddda2b7SChristoph Hellwig if (IS_ENABLED(CONFIG_NUMA)) 40710825a6f9SJoe Perches proc_create_seq_private("vmallocinfo", 0400, NULL, 407244414d82SChristoph Hellwig &vmalloc_op, 407344414d82SChristoph Hellwig nr_node_ids * sizeof(unsigned int), NULL); 4074fddda2b7SChristoph Hellwig else 40750825a6f9SJoe Perches proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op); 40765f6a6a9cSAlexey Dobriyan return 0; 40775f6a6a9cSAlexey Dobriyan } 40785f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init); 4079db3808c1SJoonsoo Kim 4080a10aa579SChristoph Lameter #endif 4081