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 { 774aff1dc4SAndrey Konovalov unsigned long addr = (unsigned long)kasan_reset_tag(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) 6344aff1dc4SAndrey Konovalov unsigned long addr = (unsigned long)kasan_reset_tag(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) 778315cc066SMichel Lespinasse RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb, 779315cc066SMichel Lespinasse struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size) 78068ad4a33SUladzislau Rezki (Sony) 78168ad4a33SUladzislau Rezki (Sony) static void purge_vmap_area_lazy(void); 78268ad4a33SUladzislau Rezki (Sony) static BLOCKING_NOTIFIER_HEAD(vmap_notify_list); 783690467c8SUladzislau Rezki (Sony) static void drain_vmap_area_work(struct work_struct *work); 784690467c8SUladzislau Rezki (Sony) static DECLARE_WORK(drain_vmap_work, drain_vmap_area_work); 785db64fe02SNick Piggin 78697105f0aSRoman Gushchin static atomic_long_t nr_vmalloc_pages; 78797105f0aSRoman Gushchin 78897105f0aSRoman Gushchin unsigned long vmalloc_nr_pages(void) 78997105f0aSRoman Gushchin { 79097105f0aSRoman Gushchin return atomic_long_read(&nr_vmalloc_pages); 79197105f0aSRoman Gushchin } 79297105f0aSRoman Gushchin 793f181234aSChen Wandun static struct vmap_area *find_vmap_area_exceed_addr(unsigned long addr) 794f181234aSChen Wandun { 795f181234aSChen Wandun struct vmap_area *va = NULL; 796f181234aSChen Wandun struct rb_node *n = vmap_area_root.rb_node; 797f181234aSChen Wandun 7984aff1dc4SAndrey Konovalov addr = (unsigned long)kasan_reset_tag((void *)addr); 7994aff1dc4SAndrey Konovalov 800f181234aSChen Wandun while (n) { 801f181234aSChen Wandun struct vmap_area *tmp; 802f181234aSChen Wandun 803f181234aSChen Wandun tmp = rb_entry(n, struct vmap_area, rb_node); 804f181234aSChen Wandun if (tmp->va_end > addr) { 805f181234aSChen Wandun va = tmp; 806f181234aSChen Wandun if (tmp->va_start <= addr) 807f181234aSChen Wandun break; 808f181234aSChen Wandun 809f181234aSChen Wandun n = n->rb_left; 810f181234aSChen Wandun } else 811f181234aSChen Wandun n = n->rb_right; 812f181234aSChen Wandun } 813f181234aSChen Wandun 814f181234aSChen Wandun return va; 815f181234aSChen Wandun } 816f181234aSChen Wandun 817db64fe02SNick Piggin static struct vmap_area *__find_vmap_area(unsigned long addr) 8181da177e4SLinus Torvalds { 819db64fe02SNick Piggin struct rb_node *n = vmap_area_root.rb_node; 820db64fe02SNick Piggin 8214aff1dc4SAndrey Konovalov addr = (unsigned long)kasan_reset_tag((void *)addr); 8224aff1dc4SAndrey Konovalov 823db64fe02SNick Piggin while (n) { 824db64fe02SNick Piggin struct vmap_area *va; 825db64fe02SNick Piggin 826db64fe02SNick Piggin va = rb_entry(n, struct vmap_area, rb_node); 827db64fe02SNick Piggin if (addr < va->va_start) 828db64fe02SNick Piggin n = n->rb_left; 829cef2ac3fSHATAYAMA Daisuke else if (addr >= va->va_end) 830db64fe02SNick Piggin n = n->rb_right; 831db64fe02SNick Piggin else 832db64fe02SNick Piggin return va; 833db64fe02SNick Piggin } 834db64fe02SNick Piggin 835db64fe02SNick Piggin return NULL; 836db64fe02SNick Piggin } 837db64fe02SNick Piggin 83868ad4a33SUladzislau Rezki (Sony) /* 83968ad4a33SUladzislau Rezki (Sony) * This function returns back addresses of parent node 84068ad4a33SUladzislau Rezki (Sony) * and its left or right link for further processing. 8419c801f61SUladzislau Rezki (Sony) * 8429c801f61SUladzislau Rezki (Sony) * Otherwise NULL is returned. In that case all further 8439c801f61SUladzislau Rezki (Sony) * steps regarding inserting of conflicting overlap range 8449c801f61SUladzislau Rezki (Sony) * have to be declined and actually considered as a bug. 84568ad4a33SUladzislau Rezki (Sony) */ 84668ad4a33SUladzislau Rezki (Sony) static __always_inline struct rb_node ** 84768ad4a33SUladzislau Rezki (Sony) find_va_links(struct vmap_area *va, 84868ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct rb_node *from, 84968ad4a33SUladzislau Rezki (Sony) struct rb_node **parent) 850db64fe02SNick Piggin { 851170168d0SNamhyung Kim struct vmap_area *tmp_va; 85268ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 853db64fe02SNick Piggin 85468ad4a33SUladzislau Rezki (Sony) if (root) { 85568ad4a33SUladzislau Rezki (Sony) link = &root->rb_node; 85668ad4a33SUladzislau Rezki (Sony) if (unlikely(!*link)) { 85768ad4a33SUladzislau Rezki (Sony) *parent = NULL; 85868ad4a33SUladzislau Rezki (Sony) return link; 85968ad4a33SUladzislau Rezki (Sony) } 86068ad4a33SUladzislau Rezki (Sony) } else { 86168ad4a33SUladzislau Rezki (Sony) link = &from; 86268ad4a33SUladzislau Rezki (Sony) } 86368ad4a33SUladzislau Rezki (Sony) 86468ad4a33SUladzislau Rezki (Sony) /* 86568ad4a33SUladzislau Rezki (Sony) * Go to the bottom of the tree. When we hit the last point 86668ad4a33SUladzislau Rezki (Sony) * we end up with parent rb_node and correct direction, i name 86768ad4a33SUladzislau Rezki (Sony) * it link, where the new va->rb_node will be attached to. 86868ad4a33SUladzislau Rezki (Sony) */ 86968ad4a33SUladzislau Rezki (Sony) do { 87068ad4a33SUladzislau Rezki (Sony) tmp_va = rb_entry(*link, struct vmap_area, rb_node); 87168ad4a33SUladzislau Rezki (Sony) 87268ad4a33SUladzislau Rezki (Sony) /* 87368ad4a33SUladzislau Rezki (Sony) * During the traversal we also do some sanity check. 87468ad4a33SUladzislau Rezki (Sony) * Trigger the BUG() if there are sides(left/right) 87568ad4a33SUladzislau Rezki (Sony) * or full overlaps. 87668ad4a33SUladzislau Rezki (Sony) */ 87768ad4a33SUladzislau Rezki (Sony) if (va->va_start < tmp_va->va_end && 87868ad4a33SUladzislau Rezki (Sony) va->va_end <= tmp_va->va_start) 87968ad4a33SUladzislau Rezki (Sony) link = &(*link)->rb_left; 88068ad4a33SUladzislau Rezki (Sony) else if (va->va_end > tmp_va->va_start && 88168ad4a33SUladzislau Rezki (Sony) va->va_start >= tmp_va->va_end) 88268ad4a33SUladzislau Rezki (Sony) link = &(*link)->rb_right; 8839c801f61SUladzislau Rezki (Sony) else { 8849c801f61SUladzislau Rezki (Sony) WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n", 8859c801f61SUladzislau Rezki (Sony) va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end); 8869c801f61SUladzislau Rezki (Sony) 8879c801f61SUladzislau Rezki (Sony) return NULL; 8889c801f61SUladzislau Rezki (Sony) } 88968ad4a33SUladzislau Rezki (Sony) } while (*link); 89068ad4a33SUladzislau Rezki (Sony) 89168ad4a33SUladzislau Rezki (Sony) *parent = &tmp_va->rb_node; 89268ad4a33SUladzislau Rezki (Sony) return link; 893db64fe02SNick Piggin } 894db64fe02SNick Piggin 89568ad4a33SUladzislau Rezki (Sony) static __always_inline struct list_head * 89668ad4a33SUladzislau Rezki (Sony) get_va_next_sibling(struct rb_node *parent, struct rb_node **link) 89768ad4a33SUladzislau Rezki (Sony) { 89868ad4a33SUladzislau Rezki (Sony) struct list_head *list; 899db64fe02SNick Piggin 90068ad4a33SUladzislau Rezki (Sony) if (unlikely(!parent)) 90168ad4a33SUladzislau Rezki (Sony) /* 90268ad4a33SUladzislau Rezki (Sony) * The red-black tree where we try to find VA neighbors 90368ad4a33SUladzislau Rezki (Sony) * before merging or inserting is empty, i.e. it means 90468ad4a33SUladzislau Rezki (Sony) * there is no free vmap space. Normally it does not 90568ad4a33SUladzislau Rezki (Sony) * happen but we handle this case anyway. 90668ad4a33SUladzislau Rezki (Sony) */ 90768ad4a33SUladzislau Rezki (Sony) return NULL; 90868ad4a33SUladzislau Rezki (Sony) 90968ad4a33SUladzislau Rezki (Sony) list = &rb_entry(parent, struct vmap_area, rb_node)->list; 91068ad4a33SUladzislau Rezki (Sony) return (&parent->rb_right == link ? list->next : list); 911db64fe02SNick Piggin } 912db64fe02SNick Piggin 91368ad4a33SUladzislau Rezki (Sony) static __always_inline void 91468ad4a33SUladzislau Rezki (Sony) link_va(struct vmap_area *va, struct rb_root *root, 91568ad4a33SUladzislau Rezki (Sony) struct rb_node *parent, struct rb_node **link, struct list_head *head) 91668ad4a33SUladzislau Rezki (Sony) { 91768ad4a33SUladzislau Rezki (Sony) /* 91868ad4a33SUladzislau Rezki (Sony) * VA is still not in the list, but we can 91968ad4a33SUladzislau Rezki (Sony) * identify its future previous list_head node. 92068ad4a33SUladzislau Rezki (Sony) */ 92168ad4a33SUladzislau Rezki (Sony) if (likely(parent)) { 92268ad4a33SUladzislau Rezki (Sony) head = &rb_entry(parent, struct vmap_area, rb_node)->list; 92368ad4a33SUladzislau Rezki (Sony) if (&parent->rb_right != link) 92468ad4a33SUladzislau Rezki (Sony) head = head->prev; 92568ad4a33SUladzislau Rezki (Sony) } 926db64fe02SNick Piggin 92768ad4a33SUladzislau Rezki (Sony) /* Insert to the rb-tree */ 92868ad4a33SUladzislau Rezki (Sony) rb_link_node(&va->rb_node, parent, link); 92968ad4a33SUladzislau Rezki (Sony) if (root == &free_vmap_area_root) { 93068ad4a33SUladzislau Rezki (Sony) /* 93168ad4a33SUladzislau Rezki (Sony) * Some explanation here. Just perform simple insertion 93268ad4a33SUladzislau Rezki (Sony) * to the tree. We do not set va->subtree_max_size to 93368ad4a33SUladzislau Rezki (Sony) * its current size before calling rb_insert_augmented(). 93468ad4a33SUladzislau Rezki (Sony) * It is because of we populate the tree from the bottom 93568ad4a33SUladzislau Rezki (Sony) * to parent levels when the node _is_ in the tree. 93668ad4a33SUladzislau Rezki (Sony) * 93768ad4a33SUladzislau Rezki (Sony) * Therefore we set subtree_max_size to zero after insertion, 93868ad4a33SUladzislau Rezki (Sony) * to let __augment_tree_propagate_from() puts everything to 93968ad4a33SUladzislau Rezki (Sony) * the correct order later on. 94068ad4a33SUladzislau Rezki (Sony) */ 94168ad4a33SUladzislau Rezki (Sony) rb_insert_augmented(&va->rb_node, 94268ad4a33SUladzislau Rezki (Sony) root, &free_vmap_area_rb_augment_cb); 94368ad4a33SUladzislau Rezki (Sony) va->subtree_max_size = 0; 94468ad4a33SUladzislau Rezki (Sony) } else { 94568ad4a33SUladzislau Rezki (Sony) rb_insert_color(&va->rb_node, root); 94668ad4a33SUladzislau Rezki (Sony) } 94768ad4a33SUladzislau Rezki (Sony) 94868ad4a33SUladzislau Rezki (Sony) /* Address-sort this list */ 94968ad4a33SUladzislau Rezki (Sony) list_add(&va->list, head); 95068ad4a33SUladzislau Rezki (Sony) } 95168ad4a33SUladzislau Rezki (Sony) 95268ad4a33SUladzislau Rezki (Sony) static __always_inline void 95368ad4a33SUladzislau Rezki (Sony) unlink_va(struct vmap_area *va, struct rb_root *root) 95468ad4a33SUladzislau Rezki (Sony) { 955460e42d1SUladzislau Rezki (Sony) if (WARN_ON(RB_EMPTY_NODE(&va->rb_node))) 956460e42d1SUladzislau Rezki (Sony) return; 957460e42d1SUladzislau Rezki (Sony) 95868ad4a33SUladzislau Rezki (Sony) if (root == &free_vmap_area_root) 95968ad4a33SUladzislau Rezki (Sony) rb_erase_augmented(&va->rb_node, 96068ad4a33SUladzislau Rezki (Sony) root, &free_vmap_area_rb_augment_cb); 96168ad4a33SUladzislau Rezki (Sony) else 96268ad4a33SUladzislau Rezki (Sony) rb_erase(&va->rb_node, root); 96368ad4a33SUladzislau Rezki (Sony) 96468ad4a33SUladzislau Rezki (Sony) list_del(&va->list); 96568ad4a33SUladzislau Rezki (Sony) RB_CLEAR_NODE(&va->rb_node); 96668ad4a33SUladzislau Rezki (Sony) } 96768ad4a33SUladzislau Rezki (Sony) 968bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK 969c3385e84SJiapeng Chong /* 970c3385e84SJiapeng Chong * Gets called when remove the node and rotate. 971c3385e84SJiapeng Chong */ 972c3385e84SJiapeng Chong static __always_inline unsigned long 973c3385e84SJiapeng Chong compute_subtree_max_size(struct vmap_area *va) 974c3385e84SJiapeng Chong { 975c3385e84SJiapeng Chong return max3(va_size(va), 976c3385e84SJiapeng Chong get_subtree_max_size(va->rb_node.rb_left), 977c3385e84SJiapeng Chong get_subtree_max_size(va->rb_node.rb_right)); 978c3385e84SJiapeng Chong } 979c3385e84SJiapeng Chong 980bb850f4dSUladzislau Rezki (Sony) static void 981da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(void) 982bb850f4dSUladzislau Rezki (Sony) { 983bb850f4dSUladzislau Rezki (Sony) struct vmap_area *va; 984da27c9edSUladzislau Rezki (Sony) unsigned long computed_size; 985bb850f4dSUladzislau Rezki (Sony) 986da27c9edSUladzislau Rezki (Sony) list_for_each_entry(va, &free_vmap_area_list, list) { 987da27c9edSUladzislau Rezki (Sony) computed_size = compute_subtree_max_size(va); 988da27c9edSUladzislau Rezki (Sony) if (computed_size != va->subtree_max_size) 989bb850f4dSUladzislau Rezki (Sony) pr_emerg("tree is corrupted: %lu, %lu\n", 990bb850f4dSUladzislau Rezki (Sony) va_size(va), va->subtree_max_size); 991bb850f4dSUladzislau Rezki (Sony) } 992bb850f4dSUladzislau Rezki (Sony) } 993bb850f4dSUladzislau Rezki (Sony) #endif 994bb850f4dSUladzislau Rezki (Sony) 99568ad4a33SUladzislau Rezki (Sony) /* 99668ad4a33SUladzislau Rezki (Sony) * This function populates subtree_max_size from bottom to upper 99768ad4a33SUladzislau Rezki (Sony) * levels starting from VA point. The propagation must be done 99868ad4a33SUladzislau Rezki (Sony) * when VA size is modified by changing its va_start/va_end. Or 99968ad4a33SUladzislau Rezki (Sony) * in case of newly inserting of VA to the tree. 100068ad4a33SUladzislau Rezki (Sony) * 100168ad4a33SUladzislau Rezki (Sony) * It means that __augment_tree_propagate_from() must be called: 100268ad4a33SUladzislau Rezki (Sony) * - After VA has been inserted to the tree(free path); 100368ad4a33SUladzislau Rezki (Sony) * - After VA has been shrunk(allocation path); 100468ad4a33SUladzislau Rezki (Sony) * - After VA has been increased(merging path). 100568ad4a33SUladzislau Rezki (Sony) * 100668ad4a33SUladzislau Rezki (Sony) * Please note that, it does not mean that upper parent nodes 100768ad4a33SUladzislau Rezki (Sony) * and their subtree_max_size are recalculated all the time up 100868ad4a33SUladzislau Rezki (Sony) * to the root node. 100968ad4a33SUladzislau Rezki (Sony) * 101068ad4a33SUladzislau Rezki (Sony) * 4--8 101168ad4a33SUladzislau Rezki (Sony) * /\ 101268ad4a33SUladzislau Rezki (Sony) * / \ 101368ad4a33SUladzislau Rezki (Sony) * / \ 101468ad4a33SUladzislau Rezki (Sony) * 2--2 8--8 101568ad4a33SUladzislau Rezki (Sony) * 101668ad4a33SUladzislau Rezki (Sony) * For example if we modify the node 4, shrinking it to 2, then 101768ad4a33SUladzislau Rezki (Sony) * no any modification is required. If we shrink the node 2 to 1 101868ad4a33SUladzislau Rezki (Sony) * its subtree_max_size is updated only, and set to 1. If we shrink 101968ad4a33SUladzislau Rezki (Sony) * the node 8 to 6, then its subtree_max_size is set to 6 and parent 102068ad4a33SUladzislau Rezki (Sony) * node becomes 4--6. 102168ad4a33SUladzislau Rezki (Sony) */ 102268ad4a33SUladzislau Rezki (Sony) static __always_inline void 102368ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(struct vmap_area *va) 102468ad4a33SUladzislau Rezki (Sony) { 102568ad4a33SUladzislau Rezki (Sony) /* 102615ae144fSUladzislau Rezki (Sony) * Populate the tree from bottom towards the root until 102715ae144fSUladzislau Rezki (Sony) * the calculated maximum available size of checked node 102815ae144fSUladzislau Rezki (Sony) * is equal to its current one. 102968ad4a33SUladzislau Rezki (Sony) */ 103015ae144fSUladzislau Rezki (Sony) free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL); 1031bb850f4dSUladzislau Rezki (Sony) 1032bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK 1033da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(); 1034bb850f4dSUladzislau Rezki (Sony) #endif 103568ad4a33SUladzislau Rezki (Sony) } 103668ad4a33SUladzislau Rezki (Sony) 103768ad4a33SUladzislau Rezki (Sony) static void 103868ad4a33SUladzislau Rezki (Sony) insert_vmap_area(struct vmap_area *va, 103968ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 104068ad4a33SUladzislau Rezki (Sony) { 104168ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 104268ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 104368ad4a33SUladzislau Rezki (Sony) 104468ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 10459c801f61SUladzislau Rezki (Sony) if (link) 104668ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 104768ad4a33SUladzislau Rezki (Sony) } 104868ad4a33SUladzislau Rezki (Sony) 104968ad4a33SUladzislau Rezki (Sony) static void 105068ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(struct vmap_area *va, 105168ad4a33SUladzislau Rezki (Sony) struct rb_node *from, struct rb_root *root, 105268ad4a33SUladzislau Rezki (Sony) struct list_head *head) 105368ad4a33SUladzislau Rezki (Sony) { 105468ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 105568ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 105668ad4a33SUladzislau Rezki (Sony) 105768ad4a33SUladzislau Rezki (Sony) if (from) 105868ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, NULL, from, &parent); 105968ad4a33SUladzislau Rezki (Sony) else 106068ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 106168ad4a33SUladzislau Rezki (Sony) 10629c801f61SUladzislau Rezki (Sony) if (link) { 106368ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 106468ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 106568ad4a33SUladzislau Rezki (Sony) } 10669c801f61SUladzislau Rezki (Sony) } 106768ad4a33SUladzislau Rezki (Sony) 106868ad4a33SUladzislau Rezki (Sony) /* 106968ad4a33SUladzislau Rezki (Sony) * Merge de-allocated chunk of VA memory with previous 107068ad4a33SUladzislau Rezki (Sony) * and next free blocks. If coalesce is not done a new 107168ad4a33SUladzislau Rezki (Sony) * free area is inserted. If VA has been merged, it is 107268ad4a33SUladzislau Rezki (Sony) * freed. 10739c801f61SUladzislau Rezki (Sony) * 10749c801f61SUladzislau Rezki (Sony) * Please note, it can return NULL in case of overlap 10759c801f61SUladzislau Rezki (Sony) * ranges, followed by WARN() report. Despite it is a 10769c801f61SUladzislau Rezki (Sony) * buggy behaviour, a system can be alive and keep 10779c801f61SUladzislau Rezki (Sony) * ongoing. 107868ad4a33SUladzislau Rezki (Sony) */ 10793c5c3cfbSDaniel Axtens static __always_inline struct vmap_area * 108068ad4a33SUladzislau Rezki (Sony) merge_or_add_vmap_area(struct vmap_area *va, 108168ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 108268ad4a33SUladzislau Rezki (Sony) { 108368ad4a33SUladzislau Rezki (Sony) struct vmap_area *sibling; 108468ad4a33SUladzislau Rezki (Sony) struct list_head *next; 108568ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 108668ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 108768ad4a33SUladzislau Rezki (Sony) bool merged = false; 108868ad4a33SUladzislau Rezki (Sony) 108968ad4a33SUladzislau Rezki (Sony) /* 109068ad4a33SUladzislau Rezki (Sony) * Find a place in the tree where VA potentially will be 109168ad4a33SUladzislau Rezki (Sony) * inserted, unless it is merged with its sibling/siblings. 109268ad4a33SUladzislau Rezki (Sony) */ 109368ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 10949c801f61SUladzislau Rezki (Sony) if (!link) 10959c801f61SUladzislau Rezki (Sony) return NULL; 109668ad4a33SUladzislau Rezki (Sony) 109768ad4a33SUladzislau Rezki (Sony) /* 109868ad4a33SUladzislau Rezki (Sony) * Get next node of VA to check if merging can be done. 109968ad4a33SUladzislau Rezki (Sony) */ 110068ad4a33SUladzislau Rezki (Sony) next = get_va_next_sibling(parent, link); 110168ad4a33SUladzislau Rezki (Sony) if (unlikely(next == NULL)) 110268ad4a33SUladzislau Rezki (Sony) goto insert; 110368ad4a33SUladzislau Rezki (Sony) 110468ad4a33SUladzislau Rezki (Sony) /* 110568ad4a33SUladzislau Rezki (Sony) * start end 110668ad4a33SUladzislau Rezki (Sony) * | | 110768ad4a33SUladzislau Rezki (Sony) * |<------VA------>|<-----Next----->| 110868ad4a33SUladzislau Rezki (Sony) * | | 110968ad4a33SUladzislau Rezki (Sony) * start end 111068ad4a33SUladzislau Rezki (Sony) */ 111168ad4a33SUladzislau Rezki (Sony) if (next != head) { 111268ad4a33SUladzislau Rezki (Sony) sibling = list_entry(next, struct vmap_area, list); 111368ad4a33SUladzislau Rezki (Sony) if (sibling->va_start == va->va_end) { 111468ad4a33SUladzislau Rezki (Sony) sibling->va_start = va->va_start; 111568ad4a33SUladzislau Rezki (Sony) 111668ad4a33SUladzislau Rezki (Sony) /* Free vmap_area object. */ 111768ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 111868ad4a33SUladzislau Rezki (Sony) 111968ad4a33SUladzislau Rezki (Sony) /* Point to the new merged area. */ 112068ad4a33SUladzislau Rezki (Sony) va = sibling; 112168ad4a33SUladzislau Rezki (Sony) merged = true; 112268ad4a33SUladzislau Rezki (Sony) } 112368ad4a33SUladzislau Rezki (Sony) } 112468ad4a33SUladzislau Rezki (Sony) 112568ad4a33SUladzislau Rezki (Sony) /* 112668ad4a33SUladzislau Rezki (Sony) * start end 112768ad4a33SUladzislau Rezki (Sony) * | | 112868ad4a33SUladzislau Rezki (Sony) * |<-----Prev----->|<------VA------>| 112968ad4a33SUladzislau Rezki (Sony) * | | 113068ad4a33SUladzislau Rezki (Sony) * start end 113168ad4a33SUladzislau Rezki (Sony) */ 113268ad4a33SUladzislau Rezki (Sony) if (next->prev != head) { 113368ad4a33SUladzislau Rezki (Sony) sibling = list_entry(next->prev, struct vmap_area, list); 113468ad4a33SUladzislau Rezki (Sony) if (sibling->va_end == va->va_start) { 11355dd78640SUladzislau Rezki (Sony) /* 11365dd78640SUladzislau Rezki (Sony) * If both neighbors are coalesced, it is important 11375dd78640SUladzislau Rezki (Sony) * to unlink the "next" node first, followed by merging 11385dd78640SUladzislau Rezki (Sony) * with "previous" one. Otherwise the tree might not be 11395dd78640SUladzislau Rezki (Sony) * fully populated if a sibling's augmented value is 11405dd78640SUladzislau Rezki (Sony) * "normalized" because of rotation operations. 11415dd78640SUladzislau Rezki (Sony) */ 114254f63d9dSUladzislau Rezki (Sony) if (merged) 114368ad4a33SUladzislau Rezki (Sony) unlink_va(va, root); 114468ad4a33SUladzislau Rezki (Sony) 11455dd78640SUladzislau Rezki (Sony) sibling->va_end = va->va_end; 11465dd78640SUladzislau Rezki (Sony) 114768ad4a33SUladzislau Rezki (Sony) /* Free vmap_area object. */ 114868ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 11493c5c3cfbSDaniel Axtens 11503c5c3cfbSDaniel Axtens /* Point to the new merged area. */ 11513c5c3cfbSDaniel Axtens va = sibling; 11523c5c3cfbSDaniel Axtens merged = true; 115368ad4a33SUladzislau Rezki (Sony) } 115468ad4a33SUladzislau Rezki (Sony) } 115568ad4a33SUladzislau Rezki (Sony) 115668ad4a33SUladzislau Rezki (Sony) insert: 11575dd78640SUladzislau Rezki (Sony) if (!merged) 115868ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 11593c5c3cfbSDaniel Axtens 116096e2db45SUladzislau Rezki (Sony) return va; 116196e2db45SUladzislau Rezki (Sony) } 116296e2db45SUladzislau Rezki (Sony) 116396e2db45SUladzislau Rezki (Sony) static __always_inline struct vmap_area * 116496e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area_augment(struct vmap_area *va, 116596e2db45SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 116696e2db45SUladzislau Rezki (Sony) { 116796e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area(va, root, head); 116896e2db45SUladzislau Rezki (Sony) if (va) 11695dd78640SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 117096e2db45SUladzislau Rezki (Sony) 11713c5c3cfbSDaniel Axtens return va; 117268ad4a33SUladzislau Rezki (Sony) } 117368ad4a33SUladzislau Rezki (Sony) 117468ad4a33SUladzislau Rezki (Sony) static __always_inline bool 117568ad4a33SUladzislau Rezki (Sony) is_within_this_va(struct vmap_area *va, unsigned long size, 117668ad4a33SUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 117768ad4a33SUladzislau Rezki (Sony) { 117868ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr; 117968ad4a33SUladzislau Rezki (Sony) 118068ad4a33SUladzislau Rezki (Sony) if (va->va_start > vstart) 118168ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(va->va_start, align); 118268ad4a33SUladzislau Rezki (Sony) else 118368ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(vstart, align); 118468ad4a33SUladzislau Rezki (Sony) 118568ad4a33SUladzislau Rezki (Sony) /* Can be overflowed due to big size or alignment. */ 118668ad4a33SUladzislau Rezki (Sony) if (nva_start_addr + size < nva_start_addr || 118768ad4a33SUladzislau Rezki (Sony) nva_start_addr < vstart) 118868ad4a33SUladzislau Rezki (Sony) return false; 118968ad4a33SUladzislau Rezki (Sony) 119068ad4a33SUladzislau Rezki (Sony) return (nva_start_addr + size <= va->va_end); 119168ad4a33SUladzislau Rezki (Sony) } 119268ad4a33SUladzislau Rezki (Sony) 119368ad4a33SUladzislau Rezki (Sony) /* 119468ad4a33SUladzislau Rezki (Sony) * Find the first free block(lowest start address) in the tree, 119568ad4a33SUladzislau Rezki (Sony) * that will accomplish the request corresponding to passing 11969333fe98SUladzislau Rezki * parameters. Please note, with an alignment bigger than PAGE_SIZE, 11979333fe98SUladzislau Rezki * a search length is adjusted to account for worst case alignment 11989333fe98SUladzislau Rezki * overhead. 119968ad4a33SUladzislau Rezki (Sony) */ 120068ad4a33SUladzislau Rezki (Sony) static __always_inline struct vmap_area * 12019333fe98SUladzislau Rezki find_vmap_lowest_match(unsigned long size, unsigned long align, 12029333fe98SUladzislau Rezki unsigned long vstart, bool adjust_search_size) 120368ad4a33SUladzislau Rezki (Sony) { 120468ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 120568ad4a33SUladzislau Rezki (Sony) struct rb_node *node; 12069333fe98SUladzislau Rezki unsigned long length; 120768ad4a33SUladzislau Rezki (Sony) 120868ad4a33SUladzislau Rezki (Sony) /* Start from the root. */ 120968ad4a33SUladzislau Rezki (Sony) node = free_vmap_area_root.rb_node; 121068ad4a33SUladzislau Rezki (Sony) 12119333fe98SUladzislau Rezki /* Adjust the search size for alignment overhead. */ 12129333fe98SUladzislau Rezki length = adjust_search_size ? size + align - 1 : size; 12139333fe98SUladzislau Rezki 121468ad4a33SUladzislau Rezki (Sony) while (node) { 121568ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 121668ad4a33SUladzislau Rezki (Sony) 12179333fe98SUladzislau Rezki if (get_subtree_max_size(node->rb_left) >= length && 121868ad4a33SUladzislau Rezki (Sony) vstart < va->va_start) { 121968ad4a33SUladzislau Rezki (Sony) node = node->rb_left; 122068ad4a33SUladzislau Rezki (Sony) } else { 122168ad4a33SUladzislau Rezki (Sony) if (is_within_this_va(va, size, align, vstart)) 122268ad4a33SUladzislau Rezki (Sony) return va; 122368ad4a33SUladzislau Rezki (Sony) 122468ad4a33SUladzislau Rezki (Sony) /* 122568ad4a33SUladzislau Rezki (Sony) * Does not make sense to go deeper towards the right 122668ad4a33SUladzislau Rezki (Sony) * sub-tree if it does not have a free block that is 12279333fe98SUladzislau Rezki * equal or bigger to the requested search length. 122868ad4a33SUladzislau Rezki (Sony) */ 12299333fe98SUladzislau Rezki if (get_subtree_max_size(node->rb_right) >= length) { 123068ad4a33SUladzislau Rezki (Sony) node = node->rb_right; 123168ad4a33SUladzislau Rezki (Sony) continue; 123268ad4a33SUladzislau Rezki (Sony) } 123368ad4a33SUladzislau Rezki (Sony) 123468ad4a33SUladzislau Rezki (Sony) /* 12353806b041SAndrew Morton * OK. We roll back and find the first right sub-tree, 123668ad4a33SUladzislau Rezki (Sony) * that will satisfy the search criteria. It can happen 12379f531973SUladzislau Rezki (Sony) * due to "vstart" restriction or an alignment overhead 12389f531973SUladzislau Rezki (Sony) * that is bigger then PAGE_SIZE. 123968ad4a33SUladzislau Rezki (Sony) */ 124068ad4a33SUladzislau Rezki (Sony) while ((node = rb_parent(node))) { 124168ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 124268ad4a33SUladzislau Rezki (Sony) if (is_within_this_va(va, size, align, vstart)) 124368ad4a33SUladzislau Rezki (Sony) return va; 124468ad4a33SUladzislau Rezki (Sony) 12459333fe98SUladzislau Rezki if (get_subtree_max_size(node->rb_right) >= length && 124668ad4a33SUladzislau Rezki (Sony) vstart <= va->va_start) { 12479f531973SUladzislau Rezki (Sony) /* 12489f531973SUladzislau Rezki (Sony) * Shift the vstart forward. Please note, we update it with 12499f531973SUladzislau Rezki (Sony) * parent's start address adding "1" because we do not want 12509f531973SUladzislau Rezki (Sony) * to enter same sub-tree after it has already been checked 12519f531973SUladzislau Rezki (Sony) * and no suitable free block found there. 12529f531973SUladzislau Rezki (Sony) */ 12539f531973SUladzislau Rezki (Sony) vstart = va->va_start + 1; 125468ad4a33SUladzislau Rezki (Sony) node = node->rb_right; 125568ad4a33SUladzislau Rezki (Sony) break; 125668ad4a33SUladzislau Rezki (Sony) } 125768ad4a33SUladzislau Rezki (Sony) } 125868ad4a33SUladzislau Rezki (Sony) } 125968ad4a33SUladzislau Rezki (Sony) } 126068ad4a33SUladzislau Rezki (Sony) 126168ad4a33SUladzislau Rezki (Sony) return NULL; 126268ad4a33SUladzislau Rezki (Sony) } 126368ad4a33SUladzislau Rezki (Sony) 1264a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 1265a6cf4e0fSUladzislau Rezki (Sony) #include <linux/random.h> 1266a6cf4e0fSUladzislau Rezki (Sony) 1267a6cf4e0fSUladzislau Rezki (Sony) static struct vmap_area * 1268a6cf4e0fSUladzislau Rezki (Sony) find_vmap_lowest_linear_match(unsigned long size, 1269a6cf4e0fSUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 1270a6cf4e0fSUladzislau Rezki (Sony) { 1271a6cf4e0fSUladzislau Rezki (Sony) struct vmap_area *va; 1272a6cf4e0fSUladzislau Rezki (Sony) 1273a6cf4e0fSUladzislau Rezki (Sony) list_for_each_entry(va, &free_vmap_area_list, list) { 1274a6cf4e0fSUladzislau Rezki (Sony) if (!is_within_this_va(va, size, align, vstart)) 1275a6cf4e0fSUladzislau Rezki (Sony) continue; 1276a6cf4e0fSUladzislau Rezki (Sony) 1277a6cf4e0fSUladzislau Rezki (Sony) return va; 1278a6cf4e0fSUladzislau Rezki (Sony) } 1279a6cf4e0fSUladzislau Rezki (Sony) 1280a6cf4e0fSUladzislau Rezki (Sony) return NULL; 1281a6cf4e0fSUladzislau Rezki (Sony) } 1282a6cf4e0fSUladzislau Rezki (Sony) 1283a6cf4e0fSUladzislau Rezki (Sony) static void 1284066fed59SUladzislau Rezki (Sony) find_vmap_lowest_match_check(unsigned long size, unsigned long align) 1285a6cf4e0fSUladzislau Rezki (Sony) { 1286a6cf4e0fSUladzislau Rezki (Sony) struct vmap_area *va_1, *va_2; 1287a6cf4e0fSUladzislau Rezki (Sony) unsigned long vstart; 1288a6cf4e0fSUladzislau Rezki (Sony) unsigned int rnd; 1289a6cf4e0fSUladzislau Rezki (Sony) 1290a6cf4e0fSUladzislau Rezki (Sony) get_random_bytes(&rnd, sizeof(rnd)); 1291a6cf4e0fSUladzislau Rezki (Sony) vstart = VMALLOC_START + rnd; 1292a6cf4e0fSUladzislau Rezki (Sony) 12939333fe98SUladzislau Rezki va_1 = find_vmap_lowest_match(size, align, vstart, false); 1294066fed59SUladzislau Rezki (Sony) va_2 = find_vmap_lowest_linear_match(size, align, vstart); 1295a6cf4e0fSUladzislau Rezki (Sony) 1296a6cf4e0fSUladzislau Rezki (Sony) if (va_1 != va_2) 1297a6cf4e0fSUladzislau Rezki (Sony) pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n", 1298a6cf4e0fSUladzislau Rezki (Sony) va_1, va_2, vstart); 1299a6cf4e0fSUladzislau Rezki (Sony) } 1300a6cf4e0fSUladzislau Rezki (Sony) #endif 1301a6cf4e0fSUladzislau Rezki (Sony) 130268ad4a33SUladzislau Rezki (Sony) enum fit_type { 130368ad4a33SUladzislau Rezki (Sony) NOTHING_FIT = 0, 130468ad4a33SUladzislau Rezki (Sony) FL_FIT_TYPE = 1, /* full fit */ 130568ad4a33SUladzislau Rezki (Sony) LE_FIT_TYPE = 2, /* left edge fit */ 130668ad4a33SUladzislau Rezki (Sony) RE_FIT_TYPE = 3, /* right edge fit */ 130768ad4a33SUladzislau Rezki (Sony) NE_FIT_TYPE = 4 /* no edge fit */ 130868ad4a33SUladzislau Rezki (Sony) }; 130968ad4a33SUladzislau Rezki (Sony) 131068ad4a33SUladzislau Rezki (Sony) static __always_inline enum fit_type 131168ad4a33SUladzislau Rezki (Sony) classify_va_fit_type(struct vmap_area *va, 131268ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr, unsigned long size) 131368ad4a33SUladzislau Rezki (Sony) { 131468ad4a33SUladzislau Rezki (Sony) enum fit_type type; 131568ad4a33SUladzislau Rezki (Sony) 131668ad4a33SUladzislau Rezki (Sony) /* Check if it is within VA. */ 131768ad4a33SUladzislau Rezki (Sony) if (nva_start_addr < va->va_start || 131868ad4a33SUladzislau Rezki (Sony) nva_start_addr + size > va->va_end) 131968ad4a33SUladzislau Rezki (Sony) return NOTHING_FIT; 132068ad4a33SUladzislau Rezki (Sony) 132168ad4a33SUladzislau Rezki (Sony) /* Now classify. */ 132268ad4a33SUladzislau Rezki (Sony) if (va->va_start == nva_start_addr) { 132368ad4a33SUladzislau Rezki (Sony) if (va->va_end == nva_start_addr + size) 132468ad4a33SUladzislau Rezki (Sony) type = FL_FIT_TYPE; 132568ad4a33SUladzislau Rezki (Sony) else 132668ad4a33SUladzislau Rezki (Sony) type = LE_FIT_TYPE; 132768ad4a33SUladzislau Rezki (Sony) } else if (va->va_end == nva_start_addr + size) { 132868ad4a33SUladzislau Rezki (Sony) type = RE_FIT_TYPE; 132968ad4a33SUladzislau Rezki (Sony) } else { 133068ad4a33SUladzislau Rezki (Sony) type = NE_FIT_TYPE; 133168ad4a33SUladzislau Rezki (Sony) } 133268ad4a33SUladzislau Rezki (Sony) 133368ad4a33SUladzislau Rezki (Sony) return type; 133468ad4a33SUladzislau Rezki (Sony) } 133568ad4a33SUladzislau Rezki (Sony) 133668ad4a33SUladzislau Rezki (Sony) static __always_inline int 133768ad4a33SUladzislau Rezki (Sony) adjust_va_to_fit_type(struct vmap_area *va, 133868ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr, unsigned long size, 133968ad4a33SUladzislau Rezki (Sony) enum fit_type type) 134068ad4a33SUladzislau Rezki (Sony) { 13412c929233SArnd Bergmann struct vmap_area *lva = NULL; 134268ad4a33SUladzislau Rezki (Sony) 134368ad4a33SUladzislau Rezki (Sony) if (type == FL_FIT_TYPE) { 134468ad4a33SUladzislau Rezki (Sony) /* 134568ad4a33SUladzislau Rezki (Sony) * No need to split VA, it fully fits. 134668ad4a33SUladzislau Rezki (Sony) * 134768ad4a33SUladzislau Rezki (Sony) * | | 134868ad4a33SUladzislau Rezki (Sony) * V NVA V 134968ad4a33SUladzislau Rezki (Sony) * |---------------| 135068ad4a33SUladzislau Rezki (Sony) */ 135168ad4a33SUladzislau Rezki (Sony) unlink_va(va, &free_vmap_area_root); 135268ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 135368ad4a33SUladzislau Rezki (Sony) } else if (type == LE_FIT_TYPE) { 135468ad4a33SUladzislau Rezki (Sony) /* 135568ad4a33SUladzislau Rezki (Sony) * Split left edge of fit VA. 135668ad4a33SUladzislau Rezki (Sony) * 135768ad4a33SUladzislau Rezki (Sony) * | | 135868ad4a33SUladzislau Rezki (Sony) * V NVA V R 135968ad4a33SUladzislau Rezki (Sony) * |-------|-------| 136068ad4a33SUladzislau Rezki (Sony) */ 136168ad4a33SUladzislau Rezki (Sony) va->va_start += size; 136268ad4a33SUladzislau Rezki (Sony) } else if (type == RE_FIT_TYPE) { 136368ad4a33SUladzislau Rezki (Sony) /* 136468ad4a33SUladzislau Rezki (Sony) * Split right edge of fit VA. 136568ad4a33SUladzislau Rezki (Sony) * 136668ad4a33SUladzislau Rezki (Sony) * | | 136768ad4a33SUladzislau Rezki (Sony) * L V NVA V 136868ad4a33SUladzislau Rezki (Sony) * |-------|-------| 136968ad4a33SUladzislau Rezki (Sony) */ 137068ad4a33SUladzislau Rezki (Sony) va->va_end = nva_start_addr; 137168ad4a33SUladzislau Rezki (Sony) } else if (type == NE_FIT_TYPE) { 137268ad4a33SUladzislau Rezki (Sony) /* 137368ad4a33SUladzislau Rezki (Sony) * Split no edge of fit VA. 137468ad4a33SUladzislau Rezki (Sony) * 137568ad4a33SUladzislau Rezki (Sony) * | | 137668ad4a33SUladzislau Rezki (Sony) * L V NVA V R 137768ad4a33SUladzislau Rezki (Sony) * |---|-------|---| 137868ad4a33SUladzislau Rezki (Sony) */ 137982dd23e8SUladzislau Rezki (Sony) lva = __this_cpu_xchg(ne_fit_preload_node, NULL); 138082dd23e8SUladzislau Rezki (Sony) if (unlikely(!lva)) { 138182dd23e8SUladzislau Rezki (Sony) /* 138282dd23e8SUladzislau Rezki (Sony) * For percpu allocator we do not do any pre-allocation 138382dd23e8SUladzislau Rezki (Sony) * and leave it as it is. The reason is it most likely 138482dd23e8SUladzislau Rezki (Sony) * never ends up with NE_FIT_TYPE splitting. In case of 138582dd23e8SUladzislau Rezki (Sony) * percpu allocations offsets and sizes are aligned to 138682dd23e8SUladzislau Rezki (Sony) * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE 138782dd23e8SUladzislau Rezki (Sony) * are its main fitting cases. 138882dd23e8SUladzislau Rezki (Sony) * 138982dd23e8SUladzislau Rezki (Sony) * There are a few exceptions though, as an example it is 139082dd23e8SUladzislau Rezki (Sony) * a first allocation (early boot up) when we have "one" 139182dd23e8SUladzislau Rezki (Sony) * big free space that has to be split. 1392060650a2SUladzislau Rezki (Sony) * 1393060650a2SUladzislau Rezki (Sony) * Also we can hit this path in case of regular "vmap" 1394060650a2SUladzislau Rezki (Sony) * allocations, if "this" current CPU was not preloaded. 1395060650a2SUladzislau Rezki (Sony) * See the comment in alloc_vmap_area() why. If so, then 1396060650a2SUladzislau Rezki (Sony) * GFP_NOWAIT is used instead to get an extra object for 1397060650a2SUladzislau Rezki (Sony) * split purpose. That is rare and most time does not 1398060650a2SUladzislau Rezki (Sony) * occur. 1399060650a2SUladzislau Rezki (Sony) * 1400060650a2SUladzislau Rezki (Sony) * What happens if an allocation gets failed. Basically, 1401060650a2SUladzislau Rezki (Sony) * an "overflow" path is triggered to purge lazily freed 1402060650a2SUladzislau Rezki (Sony) * areas to free some memory, then, the "retry" path is 1403060650a2SUladzislau Rezki (Sony) * triggered to repeat one more time. See more details 1404060650a2SUladzislau Rezki (Sony) * in alloc_vmap_area() function. 140582dd23e8SUladzislau Rezki (Sony) */ 140668ad4a33SUladzislau Rezki (Sony) lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT); 140782dd23e8SUladzislau Rezki (Sony) if (!lva) 140868ad4a33SUladzislau Rezki (Sony) return -1; 140982dd23e8SUladzislau Rezki (Sony) } 141068ad4a33SUladzislau Rezki (Sony) 141168ad4a33SUladzislau Rezki (Sony) /* 141268ad4a33SUladzislau Rezki (Sony) * Build the remainder. 141368ad4a33SUladzislau Rezki (Sony) */ 141468ad4a33SUladzislau Rezki (Sony) lva->va_start = va->va_start; 141568ad4a33SUladzislau Rezki (Sony) lva->va_end = nva_start_addr; 141668ad4a33SUladzislau Rezki (Sony) 141768ad4a33SUladzislau Rezki (Sony) /* 141868ad4a33SUladzislau Rezki (Sony) * Shrink this VA to remaining size. 141968ad4a33SUladzislau Rezki (Sony) */ 142068ad4a33SUladzislau Rezki (Sony) va->va_start = nva_start_addr + size; 142168ad4a33SUladzislau Rezki (Sony) } else { 142268ad4a33SUladzislau Rezki (Sony) return -1; 142368ad4a33SUladzislau Rezki (Sony) } 142468ad4a33SUladzislau Rezki (Sony) 142568ad4a33SUladzislau Rezki (Sony) if (type != FL_FIT_TYPE) { 142668ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 142768ad4a33SUladzislau Rezki (Sony) 14282c929233SArnd Bergmann if (lva) /* type == NE_FIT_TYPE */ 142968ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(lva, &va->rb_node, 143068ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, &free_vmap_area_list); 143168ad4a33SUladzislau Rezki (Sony) } 143268ad4a33SUladzislau Rezki (Sony) 143368ad4a33SUladzislau Rezki (Sony) return 0; 143468ad4a33SUladzislau Rezki (Sony) } 143568ad4a33SUladzislau Rezki (Sony) 143668ad4a33SUladzislau Rezki (Sony) /* 143768ad4a33SUladzislau Rezki (Sony) * Returns a start address of the newly allocated area, if success. 143868ad4a33SUladzislau Rezki (Sony) * Otherwise a vend is returned that indicates failure. 143968ad4a33SUladzislau Rezki (Sony) */ 144068ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 144168ad4a33SUladzislau Rezki (Sony) __alloc_vmap_area(unsigned long size, unsigned long align, 1442cacca6baSUladzislau Rezki (Sony) unsigned long vstart, unsigned long vend) 144368ad4a33SUladzislau Rezki (Sony) { 14449333fe98SUladzislau Rezki bool adjust_search_size = true; 144568ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr; 144668ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 144768ad4a33SUladzislau Rezki (Sony) enum fit_type type; 144868ad4a33SUladzislau Rezki (Sony) int ret; 144968ad4a33SUladzislau Rezki (Sony) 14509333fe98SUladzislau Rezki /* 14519333fe98SUladzislau Rezki * Do not adjust when: 14529333fe98SUladzislau Rezki * a) align <= PAGE_SIZE, because it does not make any sense. 14539333fe98SUladzislau Rezki * All blocks(their start addresses) are at least PAGE_SIZE 14549333fe98SUladzislau Rezki * aligned anyway; 14559333fe98SUladzislau Rezki * b) a short range where a requested size corresponds to exactly 14569333fe98SUladzislau Rezki * specified [vstart:vend] interval and an alignment > PAGE_SIZE. 14579333fe98SUladzislau Rezki * With adjusted search length an allocation would not succeed. 14589333fe98SUladzislau Rezki */ 14599333fe98SUladzislau Rezki if (align <= PAGE_SIZE || (align > PAGE_SIZE && (vend - vstart) == size)) 14609333fe98SUladzislau Rezki adjust_search_size = false; 14619333fe98SUladzislau Rezki 14629333fe98SUladzislau Rezki va = find_vmap_lowest_match(size, align, vstart, adjust_search_size); 146368ad4a33SUladzislau Rezki (Sony) if (unlikely(!va)) 146468ad4a33SUladzislau Rezki (Sony) return vend; 146568ad4a33SUladzislau Rezki (Sony) 146668ad4a33SUladzislau Rezki (Sony) if (va->va_start > vstart) 146768ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(va->va_start, align); 146868ad4a33SUladzislau Rezki (Sony) else 146968ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(vstart, align); 147068ad4a33SUladzislau Rezki (Sony) 147168ad4a33SUladzislau Rezki (Sony) /* Check the "vend" restriction. */ 147268ad4a33SUladzislau Rezki (Sony) if (nva_start_addr + size > vend) 147368ad4a33SUladzislau Rezki (Sony) return vend; 147468ad4a33SUladzislau Rezki (Sony) 147568ad4a33SUladzislau Rezki (Sony) /* Classify what we have found. */ 147668ad4a33SUladzislau Rezki (Sony) type = classify_va_fit_type(va, nva_start_addr, size); 147768ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(type == NOTHING_FIT)) 147868ad4a33SUladzislau Rezki (Sony) return vend; 147968ad4a33SUladzislau Rezki (Sony) 148068ad4a33SUladzislau Rezki (Sony) /* Update the free vmap_area. */ 148168ad4a33SUladzislau Rezki (Sony) ret = adjust_va_to_fit_type(va, nva_start_addr, size, type); 148268ad4a33SUladzislau Rezki (Sony) if (ret) 148368ad4a33SUladzislau Rezki (Sony) return vend; 148468ad4a33SUladzislau Rezki (Sony) 1485a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 1486066fed59SUladzislau Rezki (Sony) find_vmap_lowest_match_check(size, align); 1487a6cf4e0fSUladzislau Rezki (Sony) #endif 1488a6cf4e0fSUladzislau Rezki (Sony) 148968ad4a33SUladzislau Rezki (Sony) return nva_start_addr; 149068ad4a33SUladzislau Rezki (Sony) } 14914da56b99SChris Wilson 1492db64fe02SNick Piggin /* 1493d98c9e83SAndrey Ryabinin * Free a region of KVA allocated by alloc_vmap_area 1494d98c9e83SAndrey Ryabinin */ 1495d98c9e83SAndrey Ryabinin static void free_vmap_area(struct vmap_area *va) 1496d98c9e83SAndrey Ryabinin { 1497d98c9e83SAndrey Ryabinin /* 1498d98c9e83SAndrey Ryabinin * Remove from the busy tree/list. 1499d98c9e83SAndrey Ryabinin */ 1500d98c9e83SAndrey Ryabinin spin_lock(&vmap_area_lock); 1501d98c9e83SAndrey Ryabinin unlink_va(va, &vmap_area_root); 1502d98c9e83SAndrey Ryabinin spin_unlock(&vmap_area_lock); 1503d98c9e83SAndrey Ryabinin 1504d98c9e83SAndrey Ryabinin /* 1505d98c9e83SAndrey Ryabinin * Insert/Merge it back to the free tree/list. 1506d98c9e83SAndrey Ryabinin */ 1507d98c9e83SAndrey Ryabinin spin_lock(&free_vmap_area_lock); 150896e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list); 1509d98c9e83SAndrey Ryabinin spin_unlock(&free_vmap_area_lock); 1510d98c9e83SAndrey Ryabinin } 1511d98c9e83SAndrey Ryabinin 1512187f8cc4SUladzislau Rezki (Sony) static inline void 1513187f8cc4SUladzislau Rezki (Sony) preload_this_cpu_lock(spinlock_t *lock, gfp_t gfp_mask, int node) 1514187f8cc4SUladzislau Rezki (Sony) { 1515187f8cc4SUladzislau Rezki (Sony) struct vmap_area *va = NULL; 1516187f8cc4SUladzislau Rezki (Sony) 1517187f8cc4SUladzislau Rezki (Sony) /* 1518187f8cc4SUladzislau Rezki (Sony) * Preload this CPU with one extra vmap_area object. It is used 1519187f8cc4SUladzislau Rezki (Sony) * when fit type of free area is NE_FIT_TYPE. It guarantees that 1520187f8cc4SUladzislau Rezki (Sony) * a CPU that does an allocation is preloaded. 1521187f8cc4SUladzislau Rezki (Sony) * 1522187f8cc4SUladzislau Rezki (Sony) * We do it in non-atomic context, thus it allows us to use more 1523187f8cc4SUladzislau Rezki (Sony) * permissive allocation masks to be more stable under low memory 1524187f8cc4SUladzislau Rezki (Sony) * condition and high memory pressure. 1525187f8cc4SUladzislau Rezki (Sony) */ 1526187f8cc4SUladzislau Rezki (Sony) if (!this_cpu_read(ne_fit_preload_node)) 1527187f8cc4SUladzislau Rezki (Sony) va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); 1528187f8cc4SUladzislau Rezki (Sony) 1529187f8cc4SUladzislau Rezki (Sony) spin_lock(lock); 1530187f8cc4SUladzislau Rezki (Sony) 1531187f8cc4SUladzislau Rezki (Sony) if (va && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, va)) 1532187f8cc4SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 1533187f8cc4SUladzislau Rezki (Sony) } 1534187f8cc4SUladzislau Rezki (Sony) 1535d98c9e83SAndrey Ryabinin /* 1536db64fe02SNick Piggin * Allocate a region of KVA of the specified size and alignment, within the 1537db64fe02SNick Piggin * vstart and vend. 1538db64fe02SNick Piggin */ 1539db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size, 1540db64fe02SNick Piggin unsigned long align, 1541db64fe02SNick Piggin unsigned long vstart, unsigned long vend, 1542db64fe02SNick Piggin int node, gfp_t gfp_mask) 1543db64fe02SNick Piggin { 1544187f8cc4SUladzislau Rezki (Sony) struct vmap_area *va; 154512e376a6SUladzislau Rezki (Sony) unsigned long freed; 15461da177e4SLinus Torvalds unsigned long addr; 1547db64fe02SNick Piggin int purged = 0; 1548d98c9e83SAndrey Ryabinin int ret; 1549db64fe02SNick Piggin 15507766970cSNick Piggin BUG_ON(!size); 1551891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 155289699605SNick Piggin BUG_ON(!is_power_of_2(align)); 1553db64fe02SNick Piggin 155468ad4a33SUladzislau Rezki (Sony) if (unlikely(!vmap_initialized)) 155568ad4a33SUladzislau Rezki (Sony) return ERR_PTR(-EBUSY); 155668ad4a33SUladzislau Rezki (Sony) 15575803ed29SChristoph Hellwig might_sleep(); 1558f07116d7SUladzislau Rezki (Sony) gfp_mask = gfp_mask & GFP_RECLAIM_MASK; 15594da56b99SChris Wilson 1560f07116d7SUladzislau Rezki (Sony) va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); 1561db64fe02SNick Piggin if (unlikely(!va)) 1562db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 1563db64fe02SNick Piggin 15647f88f88fSCatalin Marinas /* 15657f88f88fSCatalin Marinas * Only scan the relevant parts containing pointers to other objects 15667f88f88fSCatalin Marinas * to avoid false negatives. 15677f88f88fSCatalin Marinas */ 1568f07116d7SUladzislau Rezki (Sony) kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask); 15697f88f88fSCatalin Marinas 1570db64fe02SNick Piggin retry: 1571187f8cc4SUladzislau Rezki (Sony) preload_this_cpu_lock(&free_vmap_area_lock, gfp_mask, node); 1572187f8cc4SUladzislau Rezki (Sony) addr = __alloc_vmap_area(size, align, vstart, vend); 1573187f8cc4SUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 157468ad4a33SUladzislau Rezki (Sony) 157589699605SNick Piggin /* 157668ad4a33SUladzislau Rezki (Sony) * If an allocation fails, the "vend" address is 157768ad4a33SUladzislau Rezki (Sony) * returned. Therefore trigger the overflow path. 157889699605SNick Piggin */ 157968ad4a33SUladzislau Rezki (Sony) if (unlikely(addr == vend)) 158089699605SNick Piggin goto overflow; 158189699605SNick Piggin 158289699605SNick Piggin va->va_start = addr; 158389699605SNick Piggin va->va_end = addr + size; 1584688fcbfcSPengfei Li va->vm = NULL; 158568ad4a33SUladzislau Rezki (Sony) 1586e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 1587e36176beSUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 158889699605SNick Piggin spin_unlock(&vmap_area_lock); 158989699605SNick Piggin 159061e16557SWang Xiaoqiang BUG_ON(!IS_ALIGNED(va->va_start, align)); 159189699605SNick Piggin BUG_ON(va->va_start < vstart); 159289699605SNick Piggin BUG_ON(va->va_end > vend); 159389699605SNick Piggin 1594d98c9e83SAndrey Ryabinin ret = kasan_populate_vmalloc(addr, size); 1595d98c9e83SAndrey Ryabinin if (ret) { 1596d98c9e83SAndrey Ryabinin free_vmap_area(va); 1597d98c9e83SAndrey Ryabinin return ERR_PTR(ret); 1598d98c9e83SAndrey Ryabinin } 1599d98c9e83SAndrey Ryabinin 160089699605SNick Piggin return va; 160189699605SNick Piggin 16027766970cSNick Piggin overflow: 1603db64fe02SNick Piggin if (!purged) { 1604db64fe02SNick Piggin purge_vmap_area_lazy(); 1605db64fe02SNick Piggin purged = 1; 1606db64fe02SNick Piggin goto retry; 1607db64fe02SNick Piggin } 16084da56b99SChris Wilson 160912e376a6SUladzislau Rezki (Sony) freed = 0; 16104da56b99SChris Wilson blocking_notifier_call_chain(&vmap_notify_list, 0, &freed); 161112e376a6SUladzislau Rezki (Sony) 16124da56b99SChris Wilson if (freed > 0) { 16134da56b99SChris Wilson purged = 0; 16144da56b99SChris Wilson goto retry; 16154da56b99SChris Wilson } 16164da56b99SChris Wilson 161703497d76SFlorian Fainelli if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) 1618756a025fSJoe Perches pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n", 1619756a025fSJoe Perches size); 162068ad4a33SUladzislau Rezki (Sony) 162168ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 1622db64fe02SNick Piggin return ERR_PTR(-EBUSY); 1623db64fe02SNick Piggin } 1624db64fe02SNick Piggin 16254da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb) 16264da56b99SChris Wilson { 16274da56b99SChris Wilson return blocking_notifier_chain_register(&vmap_notify_list, nb); 16284da56b99SChris Wilson } 16294da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier); 16304da56b99SChris Wilson 16314da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb) 16324da56b99SChris Wilson { 16334da56b99SChris Wilson return blocking_notifier_chain_unregister(&vmap_notify_list, nb); 16344da56b99SChris Wilson } 16354da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier); 16364da56b99SChris Wilson 1637db64fe02SNick Piggin /* 1638db64fe02SNick Piggin * lazy_max_pages is the maximum amount of virtual address space we gather up 1639db64fe02SNick Piggin * before attempting to purge with a TLB flush. 1640db64fe02SNick Piggin * 1641db64fe02SNick Piggin * There is a tradeoff here: a larger number will cover more kernel page tables 1642db64fe02SNick Piggin * and take slightly longer to purge, but it will linearly reduce the number of 1643db64fe02SNick Piggin * global TLB flushes that must be performed. It would seem natural to scale 1644db64fe02SNick Piggin * this number up linearly with the number of CPUs (because vmapping activity 1645db64fe02SNick Piggin * could also scale linearly with the number of CPUs), however it is likely 1646db64fe02SNick Piggin * that in practice, workloads might be constrained in other ways that mean 1647db64fe02SNick Piggin * vmap activity will not scale linearly with CPUs. Also, I want to be 1648db64fe02SNick Piggin * conservative and not introduce a big latency on huge systems, so go with 1649db64fe02SNick Piggin * a less aggressive log scale. It will still be an improvement over the old 1650db64fe02SNick Piggin * code, and it will be simple to change the scale factor if we find that it 1651db64fe02SNick Piggin * becomes a problem on bigger systems. 1652db64fe02SNick Piggin */ 1653db64fe02SNick Piggin static unsigned long lazy_max_pages(void) 1654db64fe02SNick Piggin { 1655db64fe02SNick Piggin unsigned int log; 1656db64fe02SNick Piggin 1657db64fe02SNick Piggin log = fls(num_online_cpus()); 1658db64fe02SNick Piggin 1659db64fe02SNick Piggin return log * (32UL * 1024 * 1024 / PAGE_SIZE); 1660db64fe02SNick Piggin } 1661db64fe02SNick Piggin 16624d36e6f8SUladzislau Rezki (Sony) static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0); 1663db64fe02SNick Piggin 16640574ecd1SChristoph Hellwig /* 1665f0953a1bSIngo Molnar * Serialize vmap purging. There is no actual critical section protected 16660574ecd1SChristoph Hellwig * by this look, but we want to avoid concurrent calls for performance 16670574ecd1SChristoph Hellwig * reasons and to make the pcpu_get_vm_areas more deterministic. 16680574ecd1SChristoph Hellwig */ 1669f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock); 16700574ecd1SChristoph Hellwig 167102b709dfSNick Piggin /* for per-CPU blocks */ 167202b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void); 167302b709dfSNick Piggin 16743ee48b6aSCliff Wickman /* 1675db64fe02SNick Piggin * Purges all lazily-freed vmap areas. 1676db64fe02SNick Piggin */ 16770574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end) 1678db64fe02SNick Piggin { 16794d36e6f8SUladzislau Rezki (Sony) unsigned long resched_threshold; 168096e2db45SUladzislau Rezki (Sony) struct list_head local_pure_list; 168196e2db45SUladzislau Rezki (Sony) struct vmap_area *va, *n_va; 1682db64fe02SNick Piggin 16830574ecd1SChristoph Hellwig lockdep_assert_held(&vmap_purge_lock); 168402b709dfSNick Piggin 168596e2db45SUladzislau Rezki (Sony) spin_lock(&purge_vmap_area_lock); 168696e2db45SUladzislau Rezki (Sony) purge_vmap_area_root = RB_ROOT; 168796e2db45SUladzislau Rezki (Sony) list_replace_init(&purge_vmap_area_list, &local_pure_list); 168896e2db45SUladzislau Rezki (Sony) spin_unlock(&purge_vmap_area_lock); 168996e2db45SUladzislau Rezki (Sony) 169096e2db45SUladzislau Rezki (Sony) if (unlikely(list_empty(&local_pure_list))) 169168571be9SUladzislau Rezki (Sony) return false; 169268571be9SUladzislau Rezki (Sony) 169396e2db45SUladzislau Rezki (Sony) start = min(start, 169496e2db45SUladzislau Rezki (Sony) list_first_entry(&local_pure_list, 169596e2db45SUladzislau Rezki (Sony) struct vmap_area, list)->va_start); 169696e2db45SUladzislau Rezki (Sony) 169796e2db45SUladzislau Rezki (Sony) end = max(end, 169896e2db45SUladzislau Rezki (Sony) list_last_entry(&local_pure_list, 169996e2db45SUladzislau Rezki (Sony) struct vmap_area, list)->va_end); 1700db64fe02SNick Piggin 17010574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 17024d36e6f8SUladzislau Rezki (Sony) resched_threshold = lazy_max_pages() << 1; 1703db64fe02SNick Piggin 1704e36176beSUladzislau Rezki (Sony) spin_lock(&free_vmap_area_lock); 170596e2db45SUladzislau Rezki (Sony) list_for_each_entry_safe(va, n_va, &local_pure_list, list) { 17064d36e6f8SUladzislau Rezki (Sony) unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT; 17073c5c3cfbSDaniel Axtens unsigned long orig_start = va->va_start; 17083c5c3cfbSDaniel Axtens unsigned long orig_end = va->va_end; 1709763b218dSJoel Fernandes 1710dd3b8353SUladzislau Rezki (Sony) /* 1711dd3b8353SUladzislau Rezki (Sony) * Finally insert or merge lazily-freed area. It is 1712dd3b8353SUladzislau Rezki (Sony) * detached and there is no need to "unlink" it from 1713dd3b8353SUladzislau Rezki (Sony) * anything. 1714dd3b8353SUladzislau Rezki (Sony) */ 171596e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root, 17163c5c3cfbSDaniel Axtens &free_vmap_area_list); 17173c5c3cfbSDaniel Axtens 17189c801f61SUladzislau Rezki (Sony) if (!va) 17199c801f61SUladzislau Rezki (Sony) continue; 17209c801f61SUladzislau Rezki (Sony) 17213c5c3cfbSDaniel Axtens if (is_vmalloc_or_module_addr((void *)orig_start)) 17223c5c3cfbSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 17233c5c3cfbSDaniel Axtens va->va_start, va->va_end); 1724dd3b8353SUladzislau Rezki (Sony) 17254d36e6f8SUladzislau Rezki (Sony) atomic_long_sub(nr, &vmap_lazy_nr); 172668571be9SUladzislau Rezki (Sony) 17274d36e6f8SUladzislau Rezki (Sony) if (atomic_long_read(&vmap_lazy_nr) < resched_threshold) 1728e36176beSUladzislau Rezki (Sony) cond_resched_lock(&free_vmap_area_lock); 1729763b218dSJoel Fernandes } 1730e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 17310574ecd1SChristoph Hellwig return true; 1732db64fe02SNick Piggin } 1733db64fe02SNick Piggin 1734db64fe02SNick Piggin /* 1735db64fe02SNick Piggin * Kick off a purge of the outstanding lazy areas. 1736db64fe02SNick Piggin */ 1737db64fe02SNick Piggin static void purge_vmap_area_lazy(void) 1738db64fe02SNick Piggin { 1739f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 17400574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 17410574ecd1SChristoph Hellwig __purge_vmap_area_lazy(ULONG_MAX, 0); 1742f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 1743db64fe02SNick Piggin } 1744db64fe02SNick Piggin 1745690467c8SUladzislau Rezki (Sony) static void drain_vmap_area_work(struct work_struct *work) 1746690467c8SUladzislau Rezki (Sony) { 1747690467c8SUladzislau Rezki (Sony) unsigned long nr_lazy; 1748690467c8SUladzislau Rezki (Sony) 1749690467c8SUladzislau Rezki (Sony) do { 1750690467c8SUladzislau Rezki (Sony) mutex_lock(&vmap_purge_lock); 1751690467c8SUladzislau Rezki (Sony) __purge_vmap_area_lazy(ULONG_MAX, 0); 1752690467c8SUladzislau Rezki (Sony) mutex_unlock(&vmap_purge_lock); 1753690467c8SUladzislau Rezki (Sony) 1754690467c8SUladzislau Rezki (Sony) /* Recheck if further work is required. */ 1755690467c8SUladzislau Rezki (Sony) nr_lazy = atomic_long_read(&vmap_lazy_nr); 1756690467c8SUladzislau Rezki (Sony) } while (nr_lazy > lazy_max_pages()); 1757690467c8SUladzislau Rezki (Sony) } 1758690467c8SUladzislau Rezki (Sony) 1759db64fe02SNick Piggin /* 176064141da5SJeremy Fitzhardinge * Free a vmap area, caller ensuring that the area has been unmapped 176164141da5SJeremy Fitzhardinge * and flush_cache_vunmap had been called for the correct range 176264141da5SJeremy Fitzhardinge * previously. 1763db64fe02SNick Piggin */ 176464141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va) 1765db64fe02SNick Piggin { 17664d36e6f8SUladzislau Rezki (Sony) unsigned long nr_lazy; 176780c4bd7aSChris Wilson 1768dd3b8353SUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 1769dd3b8353SUladzislau Rezki (Sony) unlink_va(va, &vmap_area_root); 1770dd3b8353SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 1771dd3b8353SUladzislau Rezki (Sony) 17724d36e6f8SUladzislau Rezki (Sony) nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >> 17734d36e6f8SUladzislau Rezki (Sony) PAGE_SHIFT, &vmap_lazy_nr); 177480c4bd7aSChris Wilson 177596e2db45SUladzislau Rezki (Sony) /* 177696e2db45SUladzislau Rezki (Sony) * Merge or place it to the purge tree/list. 177796e2db45SUladzislau Rezki (Sony) */ 177896e2db45SUladzislau Rezki (Sony) spin_lock(&purge_vmap_area_lock); 177996e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area(va, 178096e2db45SUladzislau Rezki (Sony) &purge_vmap_area_root, &purge_vmap_area_list); 178196e2db45SUladzislau Rezki (Sony) spin_unlock(&purge_vmap_area_lock); 178280c4bd7aSChris Wilson 178396e2db45SUladzislau Rezki (Sony) /* After this point, we may free va at any time */ 178480c4bd7aSChris Wilson if (unlikely(nr_lazy > lazy_max_pages())) 1785690467c8SUladzislau Rezki (Sony) schedule_work(&drain_vmap_work); 1786db64fe02SNick Piggin } 1787db64fe02SNick Piggin 1788b29acbdcSNick Piggin /* 1789b29acbdcSNick Piggin * Free and unmap a vmap area 1790b29acbdcSNick Piggin */ 1791b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va) 1792b29acbdcSNick Piggin { 1793b29acbdcSNick Piggin flush_cache_vunmap(va->va_start, va->va_end); 17944ad0ae8cSNicholas Piggin vunmap_range_noflush(va->va_start, va->va_end); 17958e57f8acSVlastimil Babka if (debug_pagealloc_enabled_static()) 179682a2e924SChintan Pandya flush_tlb_kernel_range(va->va_start, va->va_end); 179782a2e924SChintan Pandya 1798c8eef01eSChristoph Hellwig free_vmap_area_noflush(va); 1799b29acbdcSNick Piggin } 1800b29acbdcSNick Piggin 1801db64fe02SNick Piggin static struct vmap_area *find_vmap_area(unsigned long addr) 1802db64fe02SNick Piggin { 1803db64fe02SNick Piggin struct vmap_area *va; 1804db64fe02SNick Piggin 1805db64fe02SNick Piggin spin_lock(&vmap_area_lock); 1806db64fe02SNick Piggin va = __find_vmap_area(addr); 1807db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 1808db64fe02SNick Piggin 1809db64fe02SNick Piggin return va; 1810db64fe02SNick Piggin } 1811db64fe02SNick Piggin 1812db64fe02SNick Piggin /*** Per cpu kva allocator ***/ 1813db64fe02SNick Piggin 1814db64fe02SNick Piggin /* 1815db64fe02SNick Piggin * vmap space is limited especially on 32 bit architectures. Ensure there is 1816db64fe02SNick Piggin * room for at least 16 percpu vmap blocks per CPU. 1817db64fe02SNick Piggin */ 1818db64fe02SNick Piggin /* 1819db64fe02SNick Piggin * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able 1820db64fe02SNick Piggin * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess 1821db64fe02SNick Piggin * instead (we just need a rough idea) 1822db64fe02SNick Piggin */ 1823db64fe02SNick Piggin #if BITS_PER_LONG == 32 1824db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024) 1825db64fe02SNick Piggin #else 1826db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024*1024) 1827db64fe02SNick Piggin #endif 1828db64fe02SNick Piggin 1829db64fe02SNick Piggin #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) 1830db64fe02SNick Piggin #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ 1831db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ 1832db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) 1833db64fe02SNick Piggin #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ 1834db64fe02SNick Piggin #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ 1835f982f915SClemens Ladisch #define VMAP_BBMAP_BITS \ 1836f982f915SClemens Ladisch VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ 1837db64fe02SNick Piggin VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ 1838f982f915SClemens Ladisch VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16)) 1839db64fe02SNick Piggin 1840db64fe02SNick Piggin #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) 1841db64fe02SNick Piggin 1842db64fe02SNick Piggin struct vmap_block_queue { 1843db64fe02SNick Piggin spinlock_t lock; 1844db64fe02SNick Piggin struct list_head free; 1845db64fe02SNick Piggin }; 1846db64fe02SNick Piggin 1847db64fe02SNick Piggin struct vmap_block { 1848db64fe02SNick Piggin spinlock_t lock; 1849db64fe02SNick Piggin struct vmap_area *va; 1850db64fe02SNick Piggin unsigned long free, dirty; 18517d61bfe8SRoman Pen unsigned long dirty_min, dirty_max; /*< dirty range */ 1852db64fe02SNick Piggin struct list_head free_list; 1853db64fe02SNick Piggin struct rcu_head rcu_head; 185402b709dfSNick Piggin struct list_head purge; 1855db64fe02SNick Piggin }; 1856db64fe02SNick Piggin 1857db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ 1858db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); 1859db64fe02SNick Piggin 1860db64fe02SNick Piggin /* 18610f14599cSMatthew Wilcox (Oracle) * XArray of vmap blocks, indexed by address, to quickly find a vmap block 1862db64fe02SNick Piggin * in the free path. Could get rid of this if we change the API to return a 1863db64fe02SNick Piggin * "cookie" from alloc, to be passed to free. But no big deal yet. 1864db64fe02SNick Piggin */ 18650f14599cSMatthew Wilcox (Oracle) static DEFINE_XARRAY(vmap_blocks); 1866db64fe02SNick Piggin 1867db64fe02SNick Piggin /* 1868db64fe02SNick Piggin * We should probably have a fallback mechanism to allocate virtual memory 1869db64fe02SNick Piggin * out of partially filled vmap blocks. However vmap block sizing should be 1870db64fe02SNick Piggin * fairly reasonable according to the vmalloc size, so it shouldn't be a 1871db64fe02SNick Piggin * big problem. 1872db64fe02SNick Piggin */ 1873db64fe02SNick Piggin 1874db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr) 1875db64fe02SNick Piggin { 1876db64fe02SNick Piggin addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); 1877db64fe02SNick Piggin addr /= VMAP_BLOCK_SIZE; 1878db64fe02SNick Piggin return addr; 1879db64fe02SNick Piggin } 1880db64fe02SNick Piggin 1881cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off) 1882cf725ce2SRoman Pen { 1883cf725ce2SRoman Pen unsigned long addr; 1884cf725ce2SRoman Pen 1885cf725ce2SRoman Pen addr = va_start + (pages_off << PAGE_SHIFT); 1886cf725ce2SRoman Pen BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start)); 1887cf725ce2SRoman Pen return (void *)addr; 1888cf725ce2SRoman Pen } 1889cf725ce2SRoman Pen 1890cf725ce2SRoman Pen /** 1891cf725ce2SRoman Pen * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this 1892cf725ce2SRoman Pen * block. Of course pages number can't exceed VMAP_BBMAP_BITS 1893cf725ce2SRoman Pen * @order: how many 2^order pages should be occupied in newly allocated block 1894cf725ce2SRoman Pen * @gfp_mask: flags for the page level allocator 1895cf725ce2SRoman Pen * 1896a862f68aSMike Rapoport * Return: virtual address in a newly allocated block or ERR_PTR(-errno) 1897cf725ce2SRoman Pen */ 1898cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) 1899db64fe02SNick Piggin { 1900db64fe02SNick Piggin struct vmap_block_queue *vbq; 1901db64fe02SNick Piggin struct vmap_block *vb; 1902db64fe02SNick Piggin struct vmap_area *va; 1903db64fe02SNick Piggin unsigned long vb_idx; 1904db64fe02SNick Piggin int node, err; 1905cf725ce2SRoman Pen void *vaddr; 1906db64fe02SNick Piggin 1907db64fe02SNick Piggin node = numa_node_id(); 1908db64fe02SNick Piggin 1909db64fe02SNick Piggin vb = kmalloc_node(sizeof(struct vmap_block), 1910db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 1911db64fe02SNick Piggin if (unlikely(!vb)) 1912db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 1913db64fe02SNick Piggin 1914db64fe02SNick Piggin va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, 1915db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, 1916db64fe02SNick Piggin node, gfp_mask); 1917ddf9c6d4STobias Klauser if (IS_ERR(va)) { 1918db64fe02SNick Piggin kfree(vb); 1919e7d86340SJulia Lawall return ERR_CAST(va); 1920db64fe02SNick Piggin } 1921db64fe02SNick Piggin 1922cf725ce2SRoman Pen vaddr = vmap_block_vaddr(va->va_start, 0); 1923db64fe02SNick Piggin spin_lock_init(&vb->lock); 1924db64fe02SNick Piggin vb->va = va; 1925cf725ce2SRoman Pen /* At least something should be left free */ 1926cf725ce2SRoman Pen BUG_ON(VMAP_BBMAP_BITS <= (1UL << order)); 1927cf725ce2SRoman Pen vb->free = VMAP_BBMAP_BITS - (1UL << order); 1928db64fe02SNick Piggin vb->dirty = 0; 19297d61bfe8SRoman Pen vb->dirty_min = VMAP_BBMAP_BITS; 19307d61bfe8SRoman Pen vb->dirty_max = 0; 1931db64fe02SNick Piggin INIT_LIST_HEAD(&vb->free_list); 1932db64fe02SNick Piggin 1933db64fe02SNick Piggin vb_idx = addr_to_vb_idx(va->va_start); 19340f14599cSMatthew Wilcox (Oracle) err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask); 19350f14599cSMatthew Wilcox (Oracle) if (err) { 19360f14599cSMatthew Wilcox (Oracle) kfree(vb); 19370f14599cSMatthew Wilcox (Oracle) free_vmap_area(va); 19380f14599cSMatthew Wilcox (Oracle) return ERR_PTR(err); 19390f14599cSMatthew Wilcox (Oracle) } 1940db64fe02SNick Piggin 1941db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 1942db64fe02SNick Piggin spin_lock(&vbq->lock); 194368ac546fSRoman Pen list_add_tail_rcu(&vb->free_list, &vbq->free); 1944db64fe02SNick Piggin spin_unlock(&vbq->lock); 19453f04ba85STejun Heo put_cpu_var(vmap_block_queue); 1946db64fe02SNick Piggin 1947cf725ce2SRoman Pen return vaddr; 1948db64fe02SNick Piggin } 1949db64fe02SNick Piggin 1950db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb) 1951db64fe02SNick Piggin { 1952db64fe02SNick Piggin struct vmap_block *tmp; 1953db64fe02SNick Piggin 19540f14599cSMatthew Wilcox (Oracle) tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start)); 1955db64fe02SNick Piggin BUG_ON(tmp != vb); 1956db64fe02SNick Piggin 195764141da5SJeremy Fitzhardinge free_vmap_area_noflush(vb->va); 195822a3c7d1SLai Jiangshan kfree_rcu(vb, rcu_head); 1959db64fe02SNick Piggin } 1960db64fe02SNick Piggin 196102b709dfSNick Piggin static void purge_fragmented_blocks(int cpu) 196202b709dfSNick Piggin { 196302b709dfSNick Piggin LIST_HEAD(purge); 196402b709dfSNick Piggin struct vmap_block *vb; 196502b709dfSNick Piggin struct vmap_block *n_vb; 196602b709dfSNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 196702b709dfSNick Piggin 196802b709dfSNick Piggin rcu_read_lock(); 196902b709dfSNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 197002b709dfSNick Piggin 197102b709dfSNick Piggin if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS)) 197202b709dfSNick Piggin continue; 197302b709dfSNick Piggin 197402b709dfSNick Piggin spin_lock(&vb->lock); 197502b709dfSNick Piggin if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) { 197602b709dfSNick Piggin vb->free = 0; /* prevent further allocs after releasing lock */ 197702b709dfSNick Piggin vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */ 19787d61bfe8SRoman Pen vb->dirty_min = 0; 19797d61bfe8SRoman Pen vb->dirty_max = VMAP_BBMAP_BITS; 198002b709dfSNick Piggin spin_lock(&vbq->lock); 198102b709dfSNick Piggin list_del_rcu(&vb->free_list); 198202b709dfSNick Piggin spin_unlock(&vbq->lock); 198302b709dfSNick Piggin spin_unlock(&vb->lock); 198402b709dfSNick Piggin list_add_tail(&vb->purge, &purge); 198502b709dfSNick Piggin } else 198602b709dfSNick Piggin spin_unlock(&vb->lock); 198702b709dfSNick Piggin } 198802b709dfSNick Piggin rcu_read_unlock(); 198902b709dfSNick Piggin 199002b709dfSNick Piggin list_for_each_entry_safe(vb, n_vb, &purge, purge) { 199102b709dfSNick Piggin list_del(&vb->purge); 199202b709dfSNick Piggin free_vmap_block(vb); 199302b709dfSNick Piggin } 199402b709dfSNick Piggin } 199502b709dfSNick Piggin 199602b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void) 199702b709dfSNick Piggin { 199802b709dfSNick Piggin int cpu; 199902b709dfSNick Piggin 200002b709dfSNick Piggin for_each_possible_cpu(cpu) 200102b709dfSNick Piggin purge_fragmented_blocks(cpu); 200202b709dfSNick Piggin } 200302b709dfSNick Piggin 2004db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask) 2005db64fe02SNick Piggin { 2006db64fe02SNick Piggin struct vmap_block_queue *vbq; 2007db64fe02SNick Piggin struct vmap_block *vb; 2008cf725ce2SRoman Pen void *vaddr = NULL; 2009db64fe02SNick Piggin unsigned int order; 2010db64fe02SNick Piggin 2011891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 2012db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 2013aa91c4d8SJan Kara if (WARN_ON(size == 0)) { 2014aa91c4d8SJan Kara /* 2015aa91c4d8SJan Kara * Allocating 0 bytes isn't what caller wants since 2016aa91c4d8SJan Kara * get_order(0) returns funny result. Just warn and terminate 2017aa91c4d8SJan Kara * early. 2018aa91c4d8SJan Kara */ 2019aa91c4d8SJan Kara return NULL; 2020aa91c4d8SJan Kara } 2021db64fe02SNick Piggin order = get_order(size); 2022db64fe02SNick Piggin 2023db64fe02SNick Piggin rcu_read_lock(); 2024db64fe02SNick Piggin vbq = &get_cpu_var(vmap_block_queue); 2025db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 2026cf725ce2SRoman Pen unsigned long pages_off; 2027db64fe02SNick Piggin 2028db64fe02SNick Piggin spin_lock(&vb->lock); 2029cf725ce2SRoman Pen if (vb->free < (1UL << order)) { 2030cf725ce2SRoman Pen spin_unlock(&vb->lock); 2031cf725ce2SRoman Pen continue; 2032cf725ce2SRoman Pen } 203302b709dfSNick Piggin 2034cf725ce2SRoman Pen pages_off = VMAP_BBMAP_BITS - vb->free; 2035cf725ce2SRoman Pen vaddr = vmap_block_vaddr(vb->va->va_start, pages_off); 2036db64fe02SNick Piggin vb->free -= 1UL << order; 2037db64fe02SNick Piggin if (vb->free == 0) { 2038db64fe02SNick Piggin spin_lock(&vbq->lock); 2039de560423SNick Piggin list_del_rcu(&vb->free_list); 2040db64fe02SNick Piggin spin_unlock(&vbq->lock); 2041db64fe02SNick Piggin } 2042cf725ce2SRoman Pen 2043db64fe02SNick Piggin spin_unlock(&vb->lock); 2044db64fe02SNick Piggin break; 2045db64fe02SNick Piggin } 204602b709dfSNick Piggin 20473f04ba85STejun Heo put_cpu_var(vmap_block_queue); 2048db64fe02SNick Piggin rcu_read_unlock(); 2049db64fe02SNick Piggin 2050cf725ce2SRoman Pen /* Allocate new block if nothing was found */ 2051cf725ce2SRoman Pen if (!vaddr) 2052cf725ce2SRoman Pen vaddr = new_vmap_block(order, gfp_mask); 2053db64fe02SNick Piggin 2054cf725ce2SRoman Pen return vaddr; 2055db64fe02SNick Piggin } 2056db64fe02SNick Piggin 205778a0e8c4SChristoph Hellwig static void vb_free(unsigned long addr, unsigned long size) 2058db64fe02SNick Piggin { 2059db64fe02SNick Piggin unsigned long offset; 2060db64fe02SNick Piggin unsigned int order; 2061db64fe02SNick Piggin struct vmap_block *vb; 2062db64fe02SNick Piggin 2063891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 2064db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 2065b29acbdcSNick Piggin 206678a0e8c4SChristoph Hellwig flush_cache_vunmap(addr, addr + size); 2067b29acbdcSNick Piggin 2068db64fe02SNick Piggin order = get_order(size); 206978a0e8c4SChristoph Hellwig offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT; 20700f14599cSMatthew Wilcox (Oracle) vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr)); 2071db64fe02SNick Piggin 20724ad0ae8cSNicholas Piggin vunmap_range_noflush(addr, addr + size); 207364141da5SJeremy Fitzhardinge 20748e57f8acSVlastimil Babka if (debug_pagealloc_enabled_static()) 207578a0e8c4SChristoph Hellwig flush_tlb_kernel_range(addr, addr + size); 207682a2e924SChintan Pandya 2077db64fe02SNick Piggin spin_lock(&vb->lock); 20787d61bfe8SRoman Pen 20797d61bfe8SRoman Pen /* Expand dirty range */ 20807d61bfe8SRoman Pen vb->dirty_min = min(vb->dirty_min, offset); 20817d61bfe8SRoman Pen vb->dirty_max = max(vb->dirty_max, offset + (1UL << order)); 2082d086817dSMinChan Kim 2083db64fe02SNick Piggin vb->dirty += 1UL << order; 2084db64fe02SNick Piggin if (vb->dirty == VMAP_BBMAP_BITS) { 2085de560423SNick Piggin BUG_ON(vb->free); 2086db64fe02SNick Piggin spin_unlock(&vb->lock); 2087db64fe02SNick Piggin free_vmap_block(vb); 2088db64fe02SNick Piggin } else 2089db64fe02SNick Piggin spin_unlock(&vb->lock); 2090db64fe02SNick Piggin } 2091db64fe02SNick Piggin 2092868b104dSRick Edgecombe static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush) 2093db64fe02SNick Piggin { 2094db64fe02SNick Piggin int cpu; 2095db64fe02SNick Piggin 20969b463334SJeremy Fitzhardinge if (unlikely(!vmap_initialized)) 20979b463334SJeremy Fitzhardinge return; 20989b463334SJeremy Fitzhardinge 20995803ed29SChristoph Hellwig might_sleep(); 21005803ed29SChristoph Hellwig 2101db64fe02SNick Piggin for_each_possible_cpu(cpu) { 2102db64fe02SNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 2103db64fe02SNick Piggin struct vmap_block *vb; 2104db64fe02SNick Piggin 2105db64fe02SNick Piggin rcu_read_lock(); 2106db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 2107db64fe02SNick Piggin spin_lock(&vb->lock); 2108ad216c03SVijayanand Jitta if (vb->dirty && vb->dirty != VMAP_BBMAP_BITS) { 21097d61bfe8SRoman Pen unsigned long va_start = vb->va->va_start; 2110db64fe02SNick Piggin unsigned long s, e; 2111b136be5eSJoonsoo Kim 21127d61bfe8SRoman Pen s = va_start + (vb->dirty_min << PAGE_SHIFT); 21137d61bfe8SRoman Pen e = va_start + (vb->dirty_max << PAGE_SHIFT); 2114db64fe02SNick Piggin 21157d61bfe8SRoman Pen start = min(s, start); 21167d61bfe8SRoman Pen end = max(e, end); 21177d61bfe8SRoman Pen 2118db64fe02SNick Piggin flush = 1; 2119db64fe02SNick Piggin } 2120db64fe02SNick Piggin spin_unlock(&vb->lock); 2121db64fe02SNick Piggin } 2122db64fe02SNick Piggin rcu_read_unlock(); 2123db64fe02SNick Piggin } 2124db64fe02SNick Piggin 2125f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 21260574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 21270574ecd1SChristoph Hellwig if (!__purge_vmap_area_lazy(start, end) && flush) 21280574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 2129f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 2130db64fe02SNick Piggin } 2131868b104dSRick Edgecombe 2132868b104dSRick Edgecombe /** 2133868b104dSRick Edgecombe * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer 2134868b104dSRick Edgecombe * 2135868b104dSRick Edgecombe * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily 2136868b104dSRick Edgecombe * to amortize TLB flushing overheads. What this means is that any page you 2137868b104dSRick Edgecombe * have now, may, in a former life, have been mapped into kernel virtual 2138868b104dSRick Edgecombe * address by the vmap layer and so there might be some CPUs with TLB entries 2139868b104dSRick Edgecombe * still referencing that page (additional to the regular 1:1 kernel mapping). 2140868b104dSRick Edgecombe * 2141868b104dSRick Edgecombe * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can 2142868b104dSRick Edgecombe * be sure that none of the pages we have control over will have any aliases 2143868b104dSRick Edgecombe * from the vmap layer. 2144868b104dSRick Edgecombe */ 2145868b104dSRick Edgecombe void vm_unmap_aliases(void) 2146868b104dSRick Edgecombe { 2147868b104dSRick Edgecombe unsigned long start = ULONG_MAX, end = 0; 2148868b104dSRick Edgecombe int flush = 0; 2149868b104dSRick Edgecombe 2150868b104dSRick Edgecombe _vm_unmap_aliases(start, end, flush); 2151868b104dSRick Edgecombe } 2152db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases); 2153db64fe02SNick Piggin 2154db64fe02SNick Piggin /** 2155db64fe02SNick Piggin * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram 2156db64fe02SNick Piggin * @mem: the pointer returned by vm_map_ram 2157db64fe02SNick Piggin * @count: the count passed to that vm_map_ram call (cannot unmap partial) 2158db64fe02SNick Piggin */ 2159db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count) 2160db64fe02SNick Piggin { 216165ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 21624aff1dc4SAndrey Konovalov unsigned long addr = (unsigned long)kasan_reset_tag(mem); 21639c3acf60SChristoph Hellwig struct vmap_area *va; 2164db64fe02SNick Piggin 21655803ed29SChristoph Hellwig might_sleep(); 2166db64fe02SNick Piggin BUG_ON(!addr); 2167db64fe02SNick Piggin BUG_ON(addr < VMALLOC_START); 2168db64fe02SNick Piggin BUG_ON(addr > VMALLOC_END); 2169a1c0b1a0SShawn Lin BUG_ON(!PAGE_ALIGNED(addr)); 2170db64fe02SNick Piggin 2171d98c9e83SAndrey Ryabinin kasan_poison_vmalloc(mem, size); 2172d98c9e83SAndrey Ryabinin 21739c3acf60SChristoph Hellwig if (likely(count <= VMAP_MAX_ALLOC)) { 217405e3ff95SChintan Pandya debug_check_no_locks_freed(mem, size); 217578a0e8c4SChristoph Hellwig vb_free(addr, size); 21769c3acf60SChristoph Hellwig return; 21779c3acf60SChristoph Hellwig } 21789c3acf60SChristoph Hellwig 21799c3acf60SChristoph Hellwig va = find_vmap_area(addr); 21809c3acf60SChristoph Hellwig BUG_ON(!va); 218105e3ff95SChintan Pandya debug_check_no_locks_freed((void *)va->va_start, 218205e3ff95SChintan Pandya (va->va_end - va->va_start)); 21839c3acf60SChristoph Hellwig free_unmap_vmap_area(va); 2184db64fe02SNick Piggin } 2185db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram); 2186db64fe02SNick Piggin 2187db64fe02SNick Piggin /** 2188db64fe02SNick Piggin * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) 2189db64fe02SNick Piggin * @pages: an array of pointers to the pages to be mapped 2190db64fe02SNick Piggin * @count: number of pages 2191db64fe02SNick Piggin * @node: prefer to allocate data structures on this node 2192e99c97adSRandy Dunlap * 219336437638SGioh Kim * If you use this function for less than VMAP_MAX_ALLOC pages, it could be 219436437638SGioh Kim * faster than vmap so it's good. But if you mix long-life and short-life 219536437638SGioh Kim * objects with vm_map_ram(), it could consume lots of address space through 219636437638SGioh Kim * fragmentation (especially on a 32bit machine). You could see failures in 219736437638SGioh Kim * the end. Please use this function for short-lived objects. 219836437638SGioh Kim * 2199e99c97adSRandy Dunlap * Returns: a pointer to the address that has been mapped, or %NULL on failure 2200db64fe02SNick Piggin */ 2201d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node) 2202db64fe02SNick Piggin { 220365ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 2204db64fe02SNick Piggin unsigned long addr; 2205db64fe02SNick Piggin void *mem; 2206db64fe02SNick Piggin 2207db64fe02SNick Piggin if (likely(count <= VMAP_MAX_ALLOC)) { 2208db64fe02SNick Piggin mem = vb_alloc(size, GFP_KERNEL); 2209db64fe02SNick Piggin if (IS_ERR(mem)) 2210db64fe02SNick Piggin return NULL; 2211db64fe02SNick Piggin addr = (unsigned long)mem; 2212db64fe02SNick Piggin } else { 2213db64fe02SNick Piggin struct vmap_area *va; 2214db64fe02SNick Piggin va = alloc_vmap_area(size, PAGE_SIZE, 2215db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); 2216db64fe02SNick Piggin if (IS_ERR(va)) 2217db64fe02SNick Piggin return NULL; 2218db64fe02SNick Piggin 2219db64fe02SNick Piggin addr = va->va_start; 2220db64fe02SNick Piggin mem = (void *)addr; 2221db64fe02SNick Piggin } 2222d98c9e83SAndrey Ryabinin 2223b67177ecSNicholas Piggin if (vmap_pages_range(addr, addr + size, PAGE_KERNEL, 2224b67177ecSNicholas Piggin pages, PAGE_SHIFT) < 0) { 2225db64fe02SNick Piggin vm_unmap_ram(mem, count); 2226db64fe02SNick Piggin return NULL; 2227db64fe02SNick Piggin } 2228b67177ecSNicholas Piggin 222923689e91SAndrey Konovalov /* 223023689e91SAndrey Konovalov * Mark the pages as accessible, now that they are mapped. 223123689e91SAndrey Konovalov * With hardware tag-based KASAN, marking is skipped for 223223689e91SAndrey Konovalov * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc(). 223323689e91SAndrey Konovalov */ 2234f6e39794SAndrey Konovalov mem = kasan_unpoison_vmalloc(mem, size, KASAN_VMALLOC_PROT_NORMAL); 223519f1c3acSAndrey Konovalov 2236db64fe02SNick Piggin return mem; 2237db64fe02SNick Piggin } 2238db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram); 2239db64fe02SNick Piggin 22404341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata; 224192eac168SMike Rapoport 2242121e6f32SNicholas Piggin static inline unsigned int vm_area_page_order(struct vm_struct *vm) 2243121e6f32SNicholas Piggin { 2244121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 2245121e6f32SNicholas Piggin return vm->page_order; 2246121e6f32SNicholas Piggin #else 2247121e6f32SNicholas Piggin return 0; 2248121e6f32SNicholas Piggin #endif 2249121e6f32SNicholas Piggin } 2250121e6f32SNicholas Piggin 2251121e6f32SNicholas Piggin static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int order) 2252121e6f32SNicholas Piggin { 2253121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 2254121e6f32SNicholas Piggin vm->page_order = order; 2255121e6f32SNicholas Piggin #else 2256121e6f32SNicholas Piggin BUG_ON(order != 0); 2257121e6f32SNicholas Piggin #endif 2258121e6f32SNicholas Piggin } 2259121e6f32SNicholas Piggin 2260f0aa6617STejun Heo /** 2261be9b7335SNicolas Pitre * vm_area_add_early - add vmap area early during boot 2262be9b7335SNicolas Pitre * @vm: vm_struct to add 2263be9b7335SNicolas Pitre * 2264be9b7335SNicolas Pitre * This function is used to add fixed kernel vm area to vmlist before 2265be9b7335SNicolas Pitre * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags 2266be9b7335SNicolas Pitre * should contain proper values and the other fields should be zero. 2267be9b7335SNicolas Pitre * 2268be9b7335SNicolas Pitre * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 2269be9b7335SNicolas Pitre */ 2270be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm) 2271be9b7335SNicolas Pitre { 2272be9b7335SNicolas Pitre struct vm_struct *tmp, **p; 2273be9b7335SNicolas Pitre 2274be9b7335SNicolas Pitre BUG_ON(vmap_initialized); 2275be9b7335SNicolas Pitre for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { 2276be9b7335SNicolas Pitre if (tmp->addr >= vm->addr) { 2277be9b7335SNicolas Pitre BUG_ON(tmp->addr < vm->addr + vm->size); 2278be9b7335SNicolas Pitre break; 2279be9b7335SNicolas Pitre } else 2280be9b7335SNicolas Pitre BUG_ON(tmp->addr + tmp->size > vm->addr); 2281be9b7335SNicolas Pitre } 2282be9b7335SNicolas Pitre vm->next = *p; 2283be9b7335SNicolas Pitre *p = vm; 2284be9b7335SNicolas Pitre } 2285be9b7335SNicolas Pitre 2286be9b7335SNicolas Pitre /** 2287f0aa6617STejun Heo * vm_area_register_early - register vmap area early during boot 2288f0aa6617STejun Heo * @vm: vm_struct to register 2289c0c0a293STejun Heo * @align: requested alignment 2290f0aa6617STejun Heo * 2291f0aa6617STejun Heo * This function is used to register kernel vm area before 2292f0aa6617STejun Heo * vmalloc_init() is called. @vm->size and @vm->flags should contain 2293f0aa6617STejun Heo * proper values on entry and other fields should be zero. On return, 2294f0aa6617STejun Heo * vm->addr contains the allocated address. 2295f0aa6617STejun Heo * 2296f0aa6617STejun Heo * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 2297f0aa6617STejun Heo */ 2298c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align) 2299f0aa6617STejun Heo { 23000eb68437SKefeng Wang unsigned long addr = ALIGN(VMALLOC_START, align); 23010eb68437SKefeng Wang struct vm_struct *cur, **p; 2302f0aa6617STejun Heo 23030eb68437SKefeng Wang BUG_ON(vmap_initialized); 2304c0c0a293STejun Heo 23050eb68437SKefeng Wang for (p = &vmlist; (cur = *p) != NULL; p = &cur->next) { 23060eb68437SKefeng Wang if ((unsigned long)cur->addr - addr >= vm->size) 23070eb68437SKefeng Wang break; 23080eb68437SKefeng Wang addr = ALIGN((unsigned long)cur->addr + cur->size, align); 23090eb68437SKefeng Wang } 23100eb68437SKefeng Wang 23110eb68437SKefeng Wang BUG_ON(addr > VMALLOC_END - vm->size); 2312c0c0a293STejun Heo vm->addr = (void *)addr; 23130eb68437SKefeng Wang vm->next = *p; 23140eb68437SKefeng Wang *p = vm; 23153252b1d8SKefeng Wang kasan_populate_early_vm_area_shadow(vm->addr, vm->size); 2316f0aa6617STejun Heo } 2317f0aa6617STejun Heo 231868ad4a33SUladzislau Rezki (Sony) static void vmap_init_free_space(void) 231968ad4a33SUladzislau Rezki (Sony) { 232068ad4a33SUladzislau Rezki (Sony) unsigned long vmap_start = 1; 232168ad4a33SUladzislau Rezki (Sony) const unsigned long vmap_end = ULONG_MAX; 232268ad4a33SUladzislau Rezki (Sony) struct vmap_area *busy, *free; 232368ad4a33SUladzislau Rezki (Sony) 232468ad4a33SUladzislau Rezki (Sony) /* 232568ad4a33SUladzislau Rezki (Sony) * B F B B B F 232668ad4a33SUladzislau Rezki (Sony) * -|-----|.....|-----|-----|-----|.....|- 232768ad4a33SUladzislau Rezki (Sony) * | The KVA space | 232868ad4a33SUladzislau Rezki (Sony) * |<--------------------------------->| 232968ad4a33SUladzislau Rezki (Sony) */ 233068ad4a33SUladzislau Rezki (Sony) list_for_each_entry(busy, &vmap_area_list, list) { 233168ad4a33SUladzislau Rezki (Sony) if (busy->va_start - vmap_start > 0) { 233268ad4a33SUladzislau Rezki (Sony) free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 233368ad4a33SUladzislau Rezki (Sony) if (!WARN_ON_ONCE(!free)) { 233468ad4a33SUladzislau Rezki (Sony) free->va_start = vmap_start; 233568ad4a33SUladzislau Rezki (Sony) free->va_end = busy->va_start; 233668ad4a33SUladzislau Rezki (Sony) 233768ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(free, NULL, 233868ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, 233968ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list); 234068ad4a33SUladzislau Rezki (Sony) } 234168ad4a33SUladzislau Rezki (Sony) } 234268ad4a33SUladzislau Rezki (Sony) 234368ad4a33SUladzislau Rezki (Sony) vmap_start = busy->va_end; 234468ad4a33SUladzislau Rezki (Sony) } 234568ad4a33SUladzislau Rezki (Sony) 234668ad4a33SUladzislau Rezki (Sony) if (vmap_end - vmap_start > 0) { 234768ad4a33SUladzislau Rezki (Sony) free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 234868ad4a33SUladzislau Rezki (Sony) if (!WARN_ON_ONCE(!free)) { 234968ad4a33SUladzislau Rezki (Sony) free->va_start = vmap_start; 235068ad4a33SUladzislau Rezki (Sony) free->va_end = vmap_end; 235168ad4a33SUladzislau Rezki (Sony) 235268ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(free, NULL, 235368ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, 235468ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list); 235568ad4a33SUladzislau Rezki (Sony) } 235668ad4a33SUladzislau Rezki (Sony) } 235768ad4a33SUladzislau Rezki (Sony) } 235868ad4a33SUladzislau Rezki (Sony) 2359db64fe02SNick Piggin void __init vmalloc_init(void) 2360db64fe02SNick Piggin { 2361822c18f2SIvan Kokshaysky struct vmap_area *va; 2362822c18f2SIvan Kokshaysky struct vm_struct *tmp; 2363db64fe02SNick Piggin int i; 2364db64fe02SNick Piggin 236568ad4a33SUladzislau Rezki (Sony) /* 236668ad4a33SUladzislau Rezki (Sony) * Create the cache for vmap_area objects. 236768ad4a33SUladzislau Rezki (Sony) */ 236868ad4a33SUladzislau Rezki (Sony) vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC); 236968ad4a33SUladzislau Rezki (Sony) 2370db64fe02SNick Piggin for_each_possible_cpu(i) { 2371db64fe02SNick Piggin struct vmap_block_queue *vbq; 237232fcfd40SAl Viro struct vfree_deferred *p; 2373db64fe02SNick Piggin 2374db64fe02SNick Piggin vbq = &per_cpu(vmap_block_queue, i); 2375db64fe02SNick Piggin spin_lock_init(&vbq->lock); 2376db64fe02SNick Piggin INIT_LIST_HEAD(&vbq->free); 237732fcfd40SAl Viro p = &per_cpu(vfree_deferred, i); 237832fcfd40SAl Viro init_llist_head(&p->list); 237932fcfd40SAl Viro INIT_WORK(&p->wq, free_work); 2380db64fe02SNick Piggin } 23819b463334SJeremy Fitzhardinge 2382822c18f2SIvan Kokshaysky /* Import existing vmlist entries. */ 2383822c18f2SIvan Kokshaysky for (tmp = vmlist; tmp; tmp = tmp->next) { 238468ad4a33SUladzislau Rezki (Sony) va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 238568ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(!va)) 238668ad4a33SUladzislau Rezki (Sony) continue; 238768ad4a33SUladzislau Rezki (Sony) 2388822c18f2SIvan Kokshaysky va->va_start = (unsigned long)tmp->addr; 2389822c18f2SIvan Kokshaysky va->va_end = va->va_start + tmp->size; 2390dbda591dSKyongHo va->vm = tmp; 239168ad4a33SUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 2392822c18f2SIvan Kokshaysky } 2393ca23e405STejun Heo 239468ad4a33SUladzislau Rezki (Sony) /* 239568ad4a33SUladzislau Rezki (Sony) * Now we can initialize a free vmap space. 239668ad4a33SUladzislau Rezki (Sony) */ 239768ad4a33SUladzislau Rezki (Sony) vmap_init_free_space(); 23989b463334SJeremy Fitzhardinge vmap_initialized = true; 2399db64fe02SNick Piggin } 2400db64fe02SNick Piggin 2401e36176beSUladzislau Rezki (Sony) static inline void setup_vmalloc_vm_locked(struct vm_struct *vm, 2402e36176beSUladzislau Rezki (Sony) struct vmap_area *va, unsigned long flags, const void *caller) 2403cf88c790STejun Heo { 2404cf88c790STejun Heo vm->flags = flags; 2405cf88c790STejun Heo vm->addr = (void *)va->va_start; 2406cf88c790STejun Heo vm->size = va->va_end - va->va_start; 2407cf88c790STejun Heo vm->caller = caller; 2408db1aecafSMinchan Kim va->vm = vm; 2409e36176beSUladzislau Rezki (Sony) } 2410e36176beSUladzislau Rezki (Sony) 2411e36176beSUladzislau Rezki (Sony) static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, 2412e36176beSUladzislau Rezki (Sony) unsigned long flags, const void *caller) 2413e36176beSUladzislau Rezki (Sony) { 2414e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 2415e36176beSUladzislau Rezki (Sony) setup_vmalloc_vm_locked(vm, va, flags, caller); 2416c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 2417f5252e00SMitsuo Hayasaka } 2418cf88c790STejun Heo 241920fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm) 2420f5252e00SMitsuo Hayasaka { 2421d4033afdSJoonsoo Kim /* 242220fc02b4SZhang Yanfei * Before removing VM_UNINITIALIZED, 2423d4033afdSJoonsoo Kim * we should make sure that vm has proper values. 2424d4033afdSJoonsoo Kim * Pair with smp_rmb() in show_numa_info(). 2425d4033afdSJoonsoo Kim */ 2426d4033afdSJoonsoo Kim smp_wmb(); 242720fc02b4SZhang Yanfei vm->flags &= ~VM_UNINITIALIZED; 2428cf88c790STejun Heo } 2429cf88c790STejun Heo 2430db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size, 24317ca3027bSDaniel Axtens unsigned long align, unsigned long shift, unsigned long flags, 24327ca3027bSDaniel Axtens unsigned long start, unsigned long end, int node, 24337ca3027bSDaniel Axtens gfp_t gfp_mask, const void *caller) 2434db64fe02SNick Piggin { 24350006526dSKautuk Consul struct vmap_area *va; 2436db64fe02SNick Piggin struct vm_struct *area; 2437d98c9e83SAndrey Ryabinin unsigned long requested_size = size; 24381da177e4SLinus Torvalds 243952fd24caSGiridhar Pemmasani BUG_ON(in_interrupt()); 24407ca3027bSDaniel Axtens size = ALIGN(size, 1ul << shift); 244131be8309SOGAWA Hirofumi if (unlikely(!size)) 244231be8309SOGAWA Hirofumi return NULL; 24431da177e4SLinus Torvalds 2444252e5c6eSzijun_hu if (flags & VM_IOREMAP) 2445252e5c6eSzijun_hu align = 1ul << clamp_t(int, get_count_order_long(size), 2446252e5c6eSzijun_hu PAGE_SHIFT, IOREMAP_MAX_ORDER); 2447252e5c6eSzijun_hu 2448cf88c790STejun Heo area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); 24491da177e4SLinus Torvalds if (unlikely(!area)) 24501da177e4SLinus Torvalds return NULL; 24511da177e4SLinus Torvalds 245271394fe5SAndrey Ryabinin if (!(flags & VM_NO_GUARD)) 24531da177e4SLinus Torvalds size += PAGE_SIZE; 24541da177e4SLinus Torvalds 2455db64fe02SNick Piggin va = alloc_vmap_area(size, align, start, end, node, gfp_mask); 2456db64fe02SNick Piggin if (IS_ERR(va)) { 2457db64fe02SNick Piggin kfree(area); 2458db64fe02SNick Piggin return NULL; 24591da177e4SLinus Torvalds } 24601da177e4SLinus Torvalds 2461d98c9e83SAndrey Ryabinin setup_vmalloc_vm(area, va, flags, caller); 24623c5c3cfbSDaniel Axtens 246319f1c3acSAndrey Konovalov /* 246419f1c3acSAndrey Konovalov * Mark pages for non-VM_ALLOC mappings as accessible. Do it now as a 246519f1c3acSAndrey Konovalov * best-effort approach, as they can be mapped outside of vmalloc code. 246619f1c3acSAndrey Konovalov * For VM_ALLOC mappings, the pages are marked as accessible after 246719f1c3acSAndrey Konovalov * getting mapped in __vmalloc_node_range(). 246823689e91SAndrey Konovalov * With hardware tag-based KASAN, marking is skipped for 246923689e91SAndrey Konovalov * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc(). 247019f1c3acSAndrey Konovalov */ 247119f1c3acSAndrey Konovalov if (!(flags & VM_ALLOC)) 247223689e91SAndrey Konovalov area->addr = kasan_unpoison_vmalloc(area->addr, requested_size, 2473f6e39794SAndrey Konovalov KASAN_VMALLOC_PROT_NORMAL); 24741d96320fSAndrey Konovalov 24751da177e4SLinus Torvalds return area; 24761da177e4SLinus Torvalds } 24771da177e4SLinus Torvalds 2478c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, 2479c2968612SBenjamin Herrenschmidt unsigned long start, unsigned long end, 24805e6cafc8SMarek Szyprowski const void *caller) 2481c2968612SBenjamin Herrenschmidt { 24827ca3027bSDaniel Axtens return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, start, end, 24837ca3027bSDaniel Axtens NUMA_NO_NODE, GFP_KERNEL, caller); 2484c2968612SBenjamin Herrenschmidt } 2485c2968612SBenjamin Herrenschmidt 24861da177e4SLinus Torvalds /** 2487183ff22bSSimon Arlott * get_vm_area - reserve a contiguous kernel virtual area 24881da177e4SLinus Torvalds * @size: size of the area 24891da177e4SLinus Torvalds * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC 24901da177e4SLinus Torvalds * 24911da177e4SLinus Torvalds * Search an area of @size in the kernel virtual mapping area, 24921da177e4SLinus Torvalds * and reserved it for out purposes. Returns the area descriptor 24931da177e4SLinus Torvalds * on success or %NULL on failure. 2494a862f68aSMike Rapoport * 2495a862f68aSMike Rapoport * Return: the area descriptor on success or %NULL on failure. 24961da177e4SLinus Torvalds */ 24971da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) 24981da177e4SLinus Torvalds { 24997ca3027bSDaniel Axtens return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, 25007ca3027bSDaniel Axtens VMALLOC_START, VMALLOC_END, 250100ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, 250200ef2d2fSDavid Rientjes __builtin_return_address(0)); 250323016969SChristoph Lameter } 250423016969SChristoph Lameter 250523016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, 25065e6cafc8SMarek Szyprowski const void *caller) 250723016969SChristoph Lameter { 25087ca3027bSDaniel Axtens return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, 25097ca3027bSDaniel Axtens VMALLOC_START, VMALLOC_END, 251000ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, caller); 25111da177e4SLinus Torvalds } 25121da177e4SLinus Torvalds 2513e9da6e99SMarek Szyprowski /** 2514e9da6e99SMarek Szyprowski * find_vm_area - find a continuous kernel virtual area 2515e9da6e99SMarek Szyprowski * @addr: base address 2516e9da6e99SMarek Szyprowski * 2517e9da6e99SMarek Szyprowski * Search for the kernel VM area starting at @addr, and return it. 2518e9da6e99SMarek Szyprowski * It is up to the caller to do all required locking to keep the returned 2519e9da6e99SMarek Szyprowski * pointer valid. 2520a862f68aSMike Rapoport * 252174640617SHui Su * Return: the area descriptor on success or %NULL on failure. 2522e9da6e99SMarek Szyprowski */ 2523e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr) 252483342314SNick Piggin { 2525db64fe02SNick Piggin struct vmap_area *va; 252683342314SNick Piggin 2527db64fe02SNick Piggin va = find_vmap_area((unsigned long)addr); 2528688fcbfcSPengfei Li if (!va) 25297856dfebSAndi Kleen return NULL; 2530688fcbfcSPengfei Li 2531688fcbfcSPengfei Li return va->vm; 25327856dfebSAndi Kleen } 25337856dfebSAndi Kleen 25341da177e4SLinus Torvalds /** 2535183ff22bSSimon Arlott * remove_vm_area - find and remove a continuous kernel virtual area 25361da177e4SLinus Torvalds * @addr: base address 25371da177e4SLinus Torvalds * 25381da177e4SLinus Torvalds * Search for the kernel VM area starting at @addr, and remove it. 25391da177e4SLinus Torvalds * This function returns the found VM area, but using it is NOT safe 25407856dfebSAndi Kleen * on SMP machines, except for its size or flags. 2541a862f68aSMike Rapoport * 254274640617SHui Su * Return: the area descriptor on success or %NULL on failure. 25431da177e4SLinus Torvalds */ 2544b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr) 25451da177e4SLinus Torvalds { 2546db64fe02SNick Piggin struct vmap_area *va; 2547db64fe02SNick Piggin 25485803ed29SChristoph Hellwig might_sleep(); 25495803ed29SChristoph Hellwig 2550dd3b8353SUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 2551dd3b8353SUladzislau Rezki (Sony) va = __find_vmap_area((unsigned long)addr); 2552688fcbfcSPengfei Li if (va && va->vm) { 2553db1aecafSMinchan Kim struct vm_struct *vm = va->vm; 2554f5252e00SMitsuo Hayasaka 2555c69480adSJoonsoo Kim va->vm = NULL; 2556c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 2557c69480adSJoonsoo Kim 255863840de2SAndrey Konovalov kasan_free_module_shadow(vm); 2559dd32c279SKAMEZAWA Hiroyuki free_unmap_vmap_area(va); 2560dd32c279SKAMEZAWA Hiroyuki 2561db64fe02SNick Piggin return vm; 2562db64fe02SNick Piggin } 2563dd3b8353SUladzislau Rezki (Sony) 2564dd3b8353SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 2565db64fe02SNick Piggin return NULL; 25661da177e4SLinus Torvalds } 25671da177e4SLinus Torvalds 2568868b104dSRick Edgecombe static inline void set_area_direct_map(const struct vm_struct *area, 2569868b104dSRick Edgecombe int (*set_direct_map)(struct page *page)) 2570868b104dSRick Edgecombe { 2571868b104dSRick Edgecombe int i; 2572868b104dSRick Edgecombe 2573121e6f32SNicholas Piggin /* HUGE_VMALLOC passes small pages to set_direct_map */ 2574868b104dSRick Edgecombe for (i = 0; i < area->nr_pages; i++) 2575868b104dSRick Edgecombe if (page_address(area->pages[i])) 2576868b104dSRick Edgecombe set_direct_map(area->pages[i]); 2577868b104dSRick Edgecombe } 2578868b104dSRick Edgecombe 2579868b104dSRick Edgecombe /* Handle removing and resetting vm mappings related to the vm_struct. */ 2580868b104dSRick Edgecombe static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages) 2581868b104dSRick Edgecombe { 2582868b104dSRick Edgecombe unsigned long start = ULONG_MAX, end = 0; 2583121e6f32SNicholas Piggin unsigned int page_order = vm_area_page_order(area); 2584868b104dSRick Edgecombe int flush_reset = area->flags & VM_FLUSH_RESET_PERMS; 258531e67340SRick Edgecombe int flush_dmap = 0; 2586868b104dSRick Edgecombe int i; 2587868b104dSRick Edgecombe 2588868b104dSRick Edgecombe remove_vm_area(area->addr); 2589868b104dSRick Edgecombe 2590868b104dSRick Edgecombe /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */ 2591868b104dSRick Edgecombe if (!flush_reset) 2592868b104dSRick Edgecombe return; 2593868b104dSRick Edgecombe 2594868b104dSRick Edgecombe /* 2595868b104dSRick Edgecombe * If not deallocating pages, just do the flush of the VM area and 2596868b104dSRick Edgecombe * return. 2597868b104dSRick Edgecombe */ 2598868b104dSRick Edgecombe if (!deallocate_pages) { 2599868b104dSRick Edgecombe vm_unmap_aliases(); 2600868b104dSRick Edgecombe return; 2601868b104dSRick Edgecombe } 2602868b104dSRick Edgecombe 2603868b104dSRick Edgecombe /* 2604868b104dSRick Edgecombe * If execution gets here, flush the vm mapping and reset the direct 2605868b104dSRick Edgecombe * map. Find the start and end range of the direct mappings to make sure 2606868b104dSRick Edgecombe * the vm_unmap_aliases() flush includes the direct map. 2607868b104dSRick Edgecombe */ 2608121e6f32SNicholas Piggin for (i = 0; i < area->nr_pages; i += 1U << page_order) { 26098e41f872SRick Edgecombe unsigned long addr = (unsigned long)page_address(area->pages[i]); 26108e41f872SRick Edgecombe if (addr) { 2611121e6f32SNicholas Piggin unsigned long page_size; 2612121e6f32SNicholas Piggin 2613121e6f32SNicholas Piggin page_size = PAGE_SIZE << page_order; 2614868b104dSRick Edgecombe start = min(addr, start); 2615121e6f32SNicholas Piggin end = max(addr + page_size, end); 261631e67340SRick Edgecombe flush_dmap = 1; 2617868b104dSRick Edgecombe } 2618868b104dSRick Edgecombe } 2619868b104dSRick Edgecombe 2620868b104dSRick Edgecombe /* 2621868b104dSRick Edgecombe * Set direct map to something invalid so that it won't be cached if 2622868b104dSRick Edgecombe * there are any accesses after the TLB flush, then flush the TLB and 2623868b104dSRick Edgecombe * reset the direct map permissions to the default. 2624868b104dSRick Edgecombe */ 2625868b104dSRick Edgecombe set_area_direct_map(area, set_direct_map_invalid_noflush); 262631e67340SRick Edgecombe _vm_unmap_aliases(start, end, flush_dmap); 2627868b104dSRick Edgecombe set_area_direct_map(area, set_direct_map_default_noflush); 2628868b104dSRick Edgecombe } 2629868b104dSRick Edgecombe 2630b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages) 26311da177e4SLinus Torvalds { 26321da177e4SLinus Torvalds struct vm_struct *area; 26331da177e4SLinus Torvalds 26341da177e4SLinus Torvalds if (!addr) 26351da177e4SLinus Torvalds return; 26361da177e4SLinus Torvalds 2637e69e9d4aSHATAYAMA Daisuke if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n", 2638ab15d9b4SDan Carpenter addr)) 26391da177e4SLinus Torvalds return; 26401da177e4SLinus Torvalds 26416ade2032SLiviu Dudau area = find_vm_area(addr); 26421da177e4SLinus Torvalds if (unlikely(!area)) { 26434c8573e2SArjan van de Ven WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", 26441da177e4SLinus Torvalds addr); 26451da177e4SLinus Torvalds return; 26461da177e4SLinus Torvalds } 26471da177e4SLinus Torvalds 264805e3ff95SChintan Pandya debug_check_no_locks_freed(area->addr, get_vm_area_size(area)); 264905e3ff95SChintan Pandya debug_check_no_obj_freed(area->addr, get_vm_area_size(area)); 26509a11b49aSIngo Molnar 2651c041098cSVincenzo Frascino kasan_poison_vmalloc(area->addr, get_vm_area_size(area)); 26523c5c3cfbSDaniel Axtens 2653868b104dSRick Edgecombe vm_remove_mappings(area, deallocate_pages); 2654868b104dSRick Edgecombe 26551da177e4SLinus Torvalds if (deallocate_pages) { 2656*3b8000aeSNicholas Piggin int i; 26571da177e4SLinus Torvalds 2658*3b8000aeSNicholas Piggin for (i = 0; i < area->nr_pages; i++) { 2659bf53d6f8SChristoph Lameter struct page *page = area->pages[i]; 2660bf53d6f8SChristoph Lameter 2661bf53d6f8SChristoph Lameter BUG_ON(!page); 2662*3b8000aeSNicholas Piggin mod_memcg_page_state(page, MEMCG_VMALLOC, -1); 2663*3b8000aeSNicholas Piggin /* 2664*3b8000aeSNicholas Piggin * High-order allocs for huge vmallocs are split, so 2665*3b8000aeSNicholas Piggin * can be freed as an array of order-0 allocations 2666*3b8000aeSNicholas Piggin */ 2667*3b8000aeSNicholas Piggin __free_pages(page, 0); 2668a850e932SRafael Aquini cond_resched(); 26691da177e4SLinus Torvalds } 267097105f0aSRoman Gushchin atomic_long_sub(area->nr_pages, &nr_vmalloc_pages); 26711da177e4SLinus Torvalds 2672244d63eeSDavid Rientjes kvfree(area->pages); 26731da177e4SLinus Torvalds } 26741da177e4SLinus Torvalds 26751da177e4SLinus Torvalds kfree(area); 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds 2678bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr) 2679bf22e37aSAndrey Ryabinin { 2680bf22e37aSAndrey Ryabinin /* 2681bf22e37aSAndrey Ryabinin * Use raw_cpu_ptr() because this can be called from preemptible 2682bf22e37aSAndrey Ryabinin * context. Preemption is absolutely fine here, because the llist_add() 2683bf22e37aSAndrey Ryabinin * implementation is lockless, so it works even if we are adding to 268473221d88SJeongtae Park * another cpu's list. schedule_work() should be fine with this too. 2685bf22e37aSAndrey Ryabinin */ 2686bf22e37aSAndrey Ryabinin struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred); 2687bf22e37aSAndrey Ryabinin 2688bf22e37aSAndrey Ryabinin if (llist_add((struct llist_node *)addr, &p->list)) 2689bf22e37aSAndrey Ryabinin schedule_work(&p->wq); 2690bf22e37aSAndrey Ryabinin } 2691bf22e37aSAndrey Ryabinin 2692bf22e37aSAndrey Ryabinin /** 2693bf22e37aSAndrey Ryabinin * vfree_atomic - release memory allocated by vmalloc() 2694bf22e37aSAndrey Ryabinin * @addr: memory base address 2695bf22e37aSAndrey Ryabinin * 2696bf22e37aSAndrey Ryabinin * This one is just like vfree() but can be called in any atomic context 2697bf22e37aSAndrey Ryabinin * except NMIs. 2698bf22e37aSAndrey Ryabinin */ 2699bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr) 2700bf22e37aSAndrey Ryabinin { 2701bf22e37aSAndrey Ryabinin BUG_ON(in_nmi()); 2702bf22e37aSAndrey Ryabinin 2703bf22e37aSAndrey Ryabinin kmemleak_free(addr); 2704bf22e37aSAndrey Ryabinin 2705bf22e37aSAndrey Ryabinin if (!addr) 2706bf22e37aSAndrey Ryabinin return; 2707bf22e37aSAndrey Ryabinin __vfree_deferred(addr); 2708bf22e37aSAndrey Ryabinin } 2709bf22e37aSAndrey Ryabinin 2710c67dc624SRoman Penyaev static void __vfree(const void *addr) 2711c67dc624SRoman Penyaev { 2712c67dc624SRoman Penyaev if (unlikely(in_interrupt())) 2713c67dc624SRoman Penyaev __vfree_deferred(addr); 2714c67dc624SRoman Penyaev else 2715c67dc624SRoman Penyaev __vunmap(addr, 1); 2716c67dc624SRoman Penyaev } 2717c67dc624SRoman Penyaev 27181da177e4SLinus Torvalds /** 2719fa307474SMatthew Wilcox (Oracle) * vfree - Release memory allocated by vmalloc() 2720fa307474SMatthew Wilcox (Oracle) * @addr: Memory base address 27211da177e4SLinus Torvalds * 2722fa307474SMatthew Wilcox (Oracle) * Free the virtually continuous memory area starting at @addr, as obtained 2723fa307474SMatthew Wilcox (Oracle) * from one of the vmalloc() family of APIs. This will usually also free the 2724fa307474SMatthew Wilcox (Oracle) * physical memory underlying the virtual allocation, but that memory is 2725fa307474SMatthew Wilcox (Oracle) * reference counted, so it will not be freed until the last user goes away. 27261da177e4SLinus Torvalds * 2727fa307474SMatthew Wilcox (Oracle) * If @addr is NULL, no operation is performed. 272832fcfd40SAl Viro * 2729fa307474SMatthew Wilcox (Oracle) * Context: 27303ca4ea3aSAndrey Ryabinin * May sleep if called *not* from interrupt context. 2731fa307474SMatthew Wilcox (Oracle) * Must not be called in NMI context (strictly speaking, it could be 2732fa307474SMatthew Wilcox (Oracle) * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling 2733f0953a1bSIngo Molnar * conventions for vfree() arch-dependent would be a really bad idea). 27341da177e4SLinus Torvalds */ 2735b3bdda02SChristoph Lameter void vfree(const void *addr) 27361da177e4SLinus Torvalds { 273732fcfd40SAl Viro BUG_ON(in_nmi()); 273889219d37SCatalin Marinas 273989219d37SCatalin Marinas kmemleak_free(addr); 274089219d37SCatalin Marinas 2741a8dda165SAndrey Ryabinin might_sleep_if(!in_interrupt()); 2742a8dda165SAndrey Ryabinin 274332fcfd40SAl Viro if (!addr) 274432fcfd40SAl Viro return; 2745c67dc624SRoman Penyaev 2746c67dc624SRoman Penyaev __vfree(addr); 27471da177e4SLinus Torvalds } 27481da177e4SLinus Torvalds EXPORT_SYMBOL(vfree); 27491da177e4SLinus Torvalds 27501da177e4SLinus Torvalds /** 27511da177e4SLinus Torvalds * vunmap - release virtual mapping obtained by vmap() 27521da177e4SLinus Torvalds * @addr: memory base address 27531da177e4SLinus Torvalds * 27541da177e4SLinus Torvalds * Free the virtually contiguous memory area starting at @addr, 27551da177e4SLinus Torvalds * which was created from the page array passed to vmap(). 27561da177e4SLinus Torvalds * 275780e93effSPekka Enberg * Must not be called in interrupt context. 27581da177e4SLinus Torvalds */ 2759b3bdda02SChristoph Lameter void vunmap(const void *addr) 27601da177e4SLinus Torvalds { 27611da177e4SLinus Torvalds BUG_ON(in_interrupt()); 276234754b69SPeter Zijlstra might_sleep(); 276332fcfd40SAl Viro if (addr) 27641da177e4SLinus Torvalds __vunmap(addr, 0); 27651da177e4SLinus Torvalds } 27661da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap); 27671da177e4SLinus Torvalds 27681da177e4SLinus Torvalds /** 27691da177e4SLinus Torvalds * vmap - map an array of pages into virtually contiguous space 27701da177e4SLinus Torvalds * @pages: array of page pointers 27711da177e4SLinus Torvalds * @count: number of pages to map 27721da177e4SLinus Torvalds * @flags: vm_area->flags 27731da177e4SLinus Torvalds * @prot: page protection for the mapping 27741da177e4SLinus Torvalds * 2775b944afc9SChristoph Hellwig * Maps @count pages from @pages into contiguous kernel virtual space. 2776b944afc9SChristoph Hellwig * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself 2777b944afc9SChristoph Hellwig * (which must be kmalloc or vmalloc memory) and one reference per pages in it 2778b944afc9SChristoph Hellwig * are transferred from the caller to vmap(), and will be freed / dropped when 2779b944afc9SChristoph Hellwig * vfree() is called on the return value. 2780a862f68aSMike Rapoport * 2781a862f68aSMike Rapoport * Return: the address of the area or %NULL on failure 27821da177e4SLinus Torvalds */ 27831da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, 27841da177e4SLinus Torvalds unsigned long flags, pgprot_t prot) 27851da177e4SLinus Torvalds { 27861da177e4SLinus Torvalds struct vm_struct *area; 2787b67177ecSNicholas Piggin unsigned long addr; 278865ee03c4SGuillermo Julián Moreno unsigned long size; /* In bytes */ 27891da177e4SLinus Torvalds 279034754b69SPeter Zijlstra might_sleep(); 279134754b69SPeter Zijlstra 2792bd1a8fb2SPeter Zijlstra /* 2793bd1a8fb2SPeter Zijlstra * Your top guard is someone else's bottom guard. Not having a top 2794bd1a8fb2SPeter Zijlstra * guard compromises someone else's mappings too. 2795bd1a8fb2SPeter Zijlstra */ 2796bd1a8fb2SPeter Zijlstra if (WARN_ON_ONCE(flags & VM_NO_GUARD)) 2797bd1a8fb2SPeter Zijlstra flags &= ~VM_NO_GUARD; 2798bd1a8fb2SPeter Zijlstra 2799ca79b0c2SArun KS if (count > totalram_pages()) 28001da177e4SLinus Torvalds return NULL; 28011da177e4SLinus Torvalds 280265ee03c4SGuillermo Julián Moreno size = (unsigned long)count << PAGE_SHIFT; 280365ee03c4SGuillermo Julián Moreno area = get_vm_area_caller(size, flags, __builtin_return_address(0)); 28041da177e4SLinus Torvalds if (!area) 28051da177e4SLinus Torvalds return NULL; 280623016969SChristoph Lameter 2807b67177ecSNicholas Piggin addr = (unsigned long)area->addr; 2808b67177ecSNicholas Piggin if (vmap_pages_range(addr, addr + size, pgprot_nx(prot), 2809b67177ecSNicholas Piggin pages, PAGE_SHIFT) < 0) { 28101da177e4SLinus Torvalds vunmap(area->addr); 28111da177e4SLinus Torvalds return NULL; 28121da177e4SLinus Torvalds } 28131da177e4SLinus Torvalds 2814c22ee528SMiaohe Lin if (flags & VM_MAP_PUT_PAGES) { 2815b944afc9SChristoph Hellwig area->pages = pages; 2816c22ee528SMiaohe Lin area->nr_pages = count; 2817c22ee528SMiaohe Lin } 28181da177e4SLinus Torvalds return area->addr; 28191da177e4SLinus Torvalds } 28201da177e4SLinus Torvalds EXPORT_SYMBOL(vmap); 28211da177e4SLinus Torvalds 28223e9a9e25SChristoph Hellwig #ifdef CONFIG_VMAP_PFN 28233e9a9e25SChristoph Hellwig struct vmap_pfn_data { 28243e9a9e25SChristoph Hellwig unsigned long *pfns; 28253e9a9e25SChristoph Hellwig pgprot_t prot; 28263e9a9e25SChristoph Hellwig unsigned int idx; 28273e9a9e25SChristoph Hellwig }; 28283e9a9e25SChristoph Hellwig 28293e9a9e25SChristoph Hellwig static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private) 28303e9a9e25SChristoph Hellwig { 28313e9a9e25SChristoph Hellwig struct vmap_pfn_data *data = private; 28323e9a9e25SChristoph Hellwig 28333e9a9e25SChristoph Hellwig if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx]))) 28343e9a9e25SChristoph Hellwig return -EINVAL; 28353e9a9e25SChristoph Hellwig *pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot)); 28363e9a9e25SChristoph Hellwig return 0; 28373e9a9e25SChristoph Hellwig } 28383e9a9e25SChristoph Hellwig 28393e9a9e25SChristoph Hellwig /** 28403e9a9e25SChristoph Hellwig * vmap_pfn - map an array of PFNs into virtually contiguous space 28413e9a9e25SChristoph Hellwig * @pfns: array of PFNs 28423e9a9e25SChristoph Hellwig * @count: number of pages to map 28433e9a9e25SChristoph Hellwig * @prot: page protection for the mapping 28443e9a9e25SChristoph Hellwig * 28453e9a9e25SChristoph Hellwig * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns 28463e9a9e25SChristoph Hellwig * the start address of the mapping. 28473e9a9e25SChristoph Hellwig */ 28483e9a9e25SChristoph Hellwig void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot) 28493e9a9e25SChristoph Hellwig { 28503e9a9e25SChristoph Hellwig struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) }; 28513e9a9e25SChristoph Hellwig struct vm_struct *area; 28523e9a9e25SChristoph Hellwig 28533e9a9e25SChristoph Hellwig area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP, 28543e9a9e25SChristoph Hellwig __builtin_return_address(0)); 28553e9a9e25SChristoph Hellwig if (!area) 28563e9a9e25SChristoph Hellwig return NULL; 28573e9a9e25SChristoph Hellwig if (apply_to_page_range(&init_mm, (unsigned long)area->addr, 28583e9a9e25SChristoph Hellwig count * PAGE_SIZE, vmap_pfn_apply, &data)) { 28593e9a9e25SChristoph Hellwig free_vm_area(area); 28603e9a9e25SChristoph Hellwig return NULL; 28613e9a9e25SChristoph Hellwig } 28623e9a9e25SChristoph Hellwig return area->addr; 28633e9a9e25SChristoph Hellwig } 28643e9a9e25SChristoph Hellwig EXPORT_SYMBOL_GPL(vmap_pfn); 28653e9a9e25SChristoph Hellwig #endif /* CONFIG_VMAP_PFN */ 28663e9a9e25SChristoph Hellwig 286712b9f873SUladzislau Rezki static inline unsigned int 286812b9f873SUladzislau Rezki vm_area_alloc_pages(gfp_t gfp, int nid, 2869343ab817SUladzislau Rezki (Sony) unsigned int order, unsigned int nr_pages, struct page **pages) 287012b9f873SUladzislau Rezki { 287112b9f873SUladzislau Rezki unsigned int nr_allocated = 0; 2872ffb29b1cSChen Wandun struct page *page; 2873ffb29b1cSChen Wandun int i; 287412b9f873SUladzislau Rezki 287512b9f873SUladzislau Rezki /* 287612b9f873SUladzislau Rezki * For order-0 pages we make use of bulk allocator, if 287712b9f873SUladzislau Rezki * the page array is partly or not at all populated due 287812b9f873SUladzislau Rezki * to fails, fallback to a single page allocator that is 287912b9f873SUladzislau Rezki * more permissive. 288012b9f873SUladzislau Rezki */ 2881c00b6b96SChen Wandun if (!order) { 28829376130cSMichal Hocko gfp_t bulk_gfp = gfp & ~__GFP_NOFAIL; 28839376130cSMichal Hocko 2884343ab817SUladzislau Rezki (Sony) while (nr_allocated < nr_pages) { 2885343ab817SUladzislau Rezki (Sony) unsigned int nr, nr_pages_request; 2886343ab817SUladzislau Rezki (Sony) 2887343ab817SUladzislau Rezki (Sony) /* 2888343ab817SUladzislau Rezki (Sony) * A maximum allowed request is hard-coded and is 100 2889343ab817SUladzislau Rezki (Sony) * pages per call. That is done in order to prevent a 2890343ab817SUladzislau Rezki (Sony) * long preemption off scenario in the bulk-allocator 2891343ab817SUladzislau Rezki (Sony) * so the range is [1:100]. 2892343ab817SUladzislau Rezki (Sony) */ 2893343ab817SUladzislau Rezki (Sony) nr_pages_request = min(100U, nr_pages - nr_allocated); 2894343ab817SUladzislau Rezki (Sony) 2895c00b6b96SChen Wandun /* memory allocation should consider mempolicy, we can't 2896c00b6b96SChen Wandun * wrongly use nearest node when nid == NUMA_NO_NODE, 2897c00b6b96SChen Wandun * otherwise memory may be allocated in only one node, 2898c00b6b96SChen Wandun * but mempolcy want to alloc memory by interleaving. 2899c00b6b96SChen Wandun */ 2900c00b6b96SChen Wandun if (IS_ENABLED(CONFIG_NUMA) && nid == NUMA_NO_NODE) 29019376130cSMichal Hocko nr = alloc_pages_bulk_array_mempolicy(bulk_gfp, 2902c00b6b96SChen Wandun nr_pages_request, 2903c00b6b96SChen Wandun pages + nr_allocated); 2904c00b6b96SChen Wandun 2905c00b6b96SChen Wandun else 29069376130cSMichal Hocko nr = alloc_pages_bulk_array_node(bulk_gfp, nid, 2907c00b6b96SChen Wandun nr_pages_request, 2908c00b6b96SChen Wandun pages + nr_allocated); 2909343ab817SUladzislau Rezki (Sony) 2910343ab817SUladzislau Rezki (Sony) nr_allocated += nr; 2911343ab817SUladzislau Rezki (Sony) cond_resched(); 2912343ab817SUladzislau Rezki (Sony) 2913343ab817SUladzislau Rezki (Sony) /* 2914343ab817SUladzislau Rezki (Sony) * If zero or pages were obtained partly, 2915343ab817SUladzislau Rezki (Sony) * fallback to a single page allocator. 2916343ab817SUladzislau Rezki (Sony) */ 2917343ab817SUladzislau Rezki (Sony) if (nr != nr_pages_request) 2918343ab817SUladzislau Rezki (Sony) break; 2919343ab817SUladzislau Rezki (Sony) } 2920*3b8000aeSNicholas Piggin } 292112b9f873SUladzislau Rezki 292212b9f873SUladzislau Rezki /* High-order pages or fallback path if "bulk" fails. */ 292312b9f873SUladzislau Rezki 2924ffb29b1cSChen Wandun while (nr_allocated < nr_pages) { 2925dd544141SVasily Averin if (fatal_signal_pending(current)) 2926dd544141SVasily Averin break; 2927dd544141SVasily Averin 2928ffb29b1cSChen Wandun if (nid == NUMA_NO_NODE) 2929ffb29b1cSChen Wandun page = alloc_pages(gfp, order); 2930ffb29b1cSChen Wandun else 293112b9f873SUladzislau Rezki page = alloc_pages_node(nid, gfp, order); 293212b9f873SUladzislau Rezki if (unlikely(!page)) 293312b9f873SUladzislau Rezki break; 2934*3b8000aeSNicholas Piggin /* 2935*3b8000aeSNicholas Piggin * Higher order allocations must be able to be treated as 2936*3b8000aeSNicholas Piggin * indepdenent small pages by callers (as they can with 2937*3b8000aeSNicholas Piggin * small-page vmallocs). Some drivers do their own refcounting 2938*3b8000aeSNicholas Piggin * on vmalloc_to_page() pages, some use page->mapping, 2939*3b8000aeSNicholas Piggin * page->lru, etc. 2940*3b8000aeSNicholas Piggin */ 2941*3b8000aeSNicholas Piggin if (order) 2942*3b8000aeSNicholas Piggin split_page(page, order); 294312b9f873SUladzislau Rezki 294412b9f873SUladzislau Rezki /* 294512b9f873SUladzislau Rezki * Careful, we allocate and map page-order pages, but 294612b9f873SUladzislau Rezki * tracking is done per PAGE_SIZE page so as to keep the 294712b9f873SUladzislau Rezki * vm_struct APIs independent of the physical/mapped size. 294812b9f873SUladzislau Rezki */ 294912b9f873SUladzislau Rezki for (i = 0; i < (1U << order); i++) 295012b9f873SUladzislau Rezki pages[nr_allocated + i] = page + i; 295112b9f873SUladzislau Rezki 295212b9f873SUladzislau Rezki cond_resched(); 295312b9f873SUladzislau Rezki nr_allocated += 1U << order; 295412b9f873SUladzislau Rezki } 295512b9f873SUladzislau Rezki 295612b9f873SUladzislau Rezki return nr_allocated; 295712b9f873SUladzislau Rezki } 295812b9f873SUladzislau Rezki 2959e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, 2960121e6f32SNicholas Piggin pgprot_t prot, unsigned int page_shift, 2961121e6f32SNicholas Piggin int node) 29621da177e4SLinus Torvalds { 2963930f036bSDavid Rientjes const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; 29649376130cSMichal Hocko bool nofail = gfp_mask & __GFP_NOFAIL; 2965121e6f32SNicholas Piggin unsigned long addr = (unsigned long)area->addr; 2966121e6f32SNicholas Piggin unsigned long size = get_vm_area_size(area); 296734fe6537SAndrew Morton unsigned long array_size; 2968121e6f32SNicholas Piggin unsigned int nr_small_pages = size >> PAGE_SHIFT; 2969121e6f32SNicholas Piggin unsigned int page_order; 2970451769ebSMichal Hocko unsigned int flags; 2971451769ebSMichal Hocko int ret; 29721da177e4SLinus Torvalds 2973121e6f32SNicholas Piggin array_size = (unsigned long)nr_small_pages * sizeof(struct page *); 2974f255935bSChristoph Hellwig gfp_mask |= __GFP_NOWARN; 2975f255935bSChristoph Hellwig if (!(gfp_mask & (GFP_DMA | GFP_DMA32))) 2976f255935bSChristoph Hellwig gfp_mask |= __GFP_HIGHMEM; 29771da177e4SLinus Torvalds 29781da177e4SLinus Torvalds /* Please note that the recursion is strictly bounded. */ 29798757d5faSJan Kiszka if (array_size > PAGE_SIZE) { 29805c1f4e69SUladzislau Rezki (Sony) area->pages = __vmalloc_node(array_size, 1, nested_gfp, node, 2981f255935bSChristoph Hellwig area->caller); 2982286e1ea3SAndrew Morton } else { 29835c1f4e69SUladzislau Rezki (Sony) area->pages = kmalloc_node(array_size, nested_gfp, node); 2984286e1ea3SAndrew Morton } 29857ea36242SAustin Kim 29865c1f4e69SUladzislau Rezki (Sony) if (!area->pages) { 2987c3d77172SUladzislau Rezki (Sony) warn_alloc(gfp_mask, NULL, 2988f4bdfeafSUladzislau Rezki (Sony) "vmalloc error: size %lu, failed to allocated page array size %lu", 2989d70bec8cSNicholas Piggin nr_small_pages * PAGE_SIZE, array_size); 2990cd61413bSUladzislau Rezki (Sony) free_vm_area(area); 29911da177e4SLinus Torvalds return NULL; 29921da177e4SLinus Torvalds } 29931da177e4SLinus Torvalds 2994121e6f32SNicholas Piggin set_vm_area_page_order(area, page_shift - PAGE_SHIFT); 2995121e6f32SNicholas Piggin page_order = vm_area_page_order(area); 2996121e6f32SNicholas Piggin 2997c3d77172SUladzislau Rezki (Sony) area->nr_pages = vm_area_alloc_pages(gfp_mask | __GFP_NOWARN, 2998c3d77172SUladzislau Rezki (Sony) node, page_order, nr_small_pages, area->pages); 29995c1f4e69SUladzislau Rezki (Sony) 300097105f0aSRoman Gushchin atomic_long_add(area->nr_pages, &nr_vmalloc_pages); 30014e5aa1f4SShakeel Butt if (gfp_mask & __GFP_ACCOUNT) { 3002*3b8000aeSNicholas Piggin int i; 30034e5aa1f4SShakeel Butt 3004*3b8000aeSNicholas Piggin for (i = 0; i < area->nr_pages; i++) 3005*3b8000aeSNicholas Piggin mod_memcg_page_state(area->pages[i], MEMCG_VMALLOC, 1); 30064e5aa1f4SShakeel Butt } 30075c1f4e69SUladzislau Rezki (Sony) 30085c1f4e69SUladzislau Rezki (Sony) /* 30095c1f4e69SUladzislau Rezki (Sony) * If not enough pages were obtained to accomplish an 30105c1f4e69SUladzislau Rezki (Sony) * allocation request, free them via __vfree() if any. 30115c1f4e69SUladzislau Rezki (Sony) */ 30125c1f4e69SUladzislau Rezki (Sony) if (area->nr_pages != nr_small_pages) { 3013c3d77172SUladzislau Rezki (Sony) warn_alloc(gfp_mask, NULL, 3014f4bdfeafSUladzislau Rezki (Sony) "vmalloc error: size %lu, page order %u, failed to allocate pages", 3015d70bec8cSNicholas Piggin area->nr_pages * PAGE_SIZE, page_order); 30161da177e4SLinus Torvalds goto fail; 30171da177e4SLinus Torvalds } 3018121e6f32SNicholas Piggin 3019451769ebSMichal Hocko /* 3020451769ebSMichal Hocko * page tables allocations ignore external gfp mask, enforce it 3021451769ebSMichal Hocko * by the scope API 3022451769ebSMichal Hocko */ 3023451769ebSMichal Hocko if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO) 3024451769ebSMichal Hocko flags = memalloc_nofs_save(); 3025451769ebSMichal Hocko else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0) 3026451769ebSMichal Hocko flags = memalloc_noio_save(); 3027451769ebSMichal Hocko 30289376130cSMichal Hocko do { 3029451769ebSMichal Hocko ret = vmap_pages_range(addr, addr + size, prot, area->pages, 3030451769ebSMichal Hocko page_shift); 30319376130cSMichal Hocko if (nofail && (ret < 0)) 30329376130cSMichal Hocko schedule_timeout_uninterruptible(1); 30339376130cSMichal Hocko } while (nofail && (ret < 0)); 3034451769ebSMichal Hocko 3035451769ebSMichal Hocko if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO) 3036451769ebSMichal Hocko memalloc_nofs_restore(flags); 3037451769ebSMichal Hocko else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0) 3038451769ebSMichal Hocko memalloc_noio_restore(flags); 3039451769ebSMichal Hocko 3040451769ebSMichal Hocko if (ret < 0) { 3041c3d77172SUladzislau Rezki (Sony) warn_alloc(gfp_mask, NULL, 3042f4bdfeafSUladzislau Rezki (Sony) "vmalloc error: size %lu, failed to map pages", 3043d70bec8cSNicholas Piggin area->nr_pages * PAGE_SIZE); 30441da177e4SLinus Torvalds goto fail; 3045d70bec8cSNicholas Piggin } 3046ed1f324cSChristoph Hellwig 30471da177e4SLinus Torvalds return area->addr; 30481da177e4SLinus Torvalds 30491da177e4SLinus Torvalds fail: 3050c67dc624SRoman Penyaev __vfree(area->addr); 30511da177e4SLinus Torvalds return NULL; 30521da177e4SLinus Torvalds } 30531da177e4SLinus Torvalds 3054d0a21265SDavid Rientjes /** 3055d0a21265SDavid Rientjes * __vmalloc_node_range - allocate virtually contiguous memory 3056d0a21265SDavid Rientjes * @size: allocation size 3057d0a21265SDavid Rientjes * @align: desired alignment 3058d0a21265SDavid Rientjes * @start: vm area range start 3059d0a21265SDavid Rientjes * @end: vm area range end 3060d0a21265SDavid Rientjes * @gfp_mask: flags for the page level allocator 3061d0a21265SDavid Rientjes * @prot: protection mask for the allocated pages 3062cb9e3c29SAndrey Ryabinin * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD) 306300ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 3064d0a21265SDavid Rientjes * @caller: caller's return address 3065d0a21265SDavid Rientjes * 3066d0a21265SDavid Rientjes * Allocate enough pages to cover @size from the page level 3067b7d90e7aSMichal Hocko * allocator with @gfp_mask flags. Please note that the full set of gfp 306830d3f011SMichal Hocko * flags are not supported. GFP_KERNEL, GFP_NOFS and GFP_NOIO are all 306930d3f011SMichal Hocko * supported. 307030d3f011SMichal Hocko * Zone modifiers are not supported. From the reclaim modifiers 307130d3f011SMichal Hocko * __GFP_DIRECT_RECLAIM is required (aka GFP_NOWAIT is not supported) 307230d3f011SMichal Hocko * and only __GFP_NOFAIL is supported (i.e. __GFP_NORETRY and 307330d3f011SMichal Hocko * __GFP_RETRY_MAYFAIL are not supported). 307430d3f011SMichal Hocko * 307530d3f011SMichal Hocko * __GFP_NOWARN can be used to suppress failures messages. 3076b7d90e7aSMichal Hocko * 3077b7d90e7aSMichal Hocko * Map them into contiguous kernel virtual space, using a pagetable 3078b7d90e7aSMichal Hocko * protection of @prot. 3079a862f68aSMike Rapoport * 3080a862f68aSMike Rapoport * Return: the address of the area or %NULL on failure 3081d0a21265SDavid Rientjes */ 3082d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align, 3083d0a21265SDavid Rientjes unsigned long start, unsigned long end, gfp_t gfp_mask, 3084cb9e3c29SAndrey Ryabinin pgprot_t prot, unsigned long vm_flags, int node, 3085cb9e3c29SAndrey Ryabinin const void *caller) 3086930fc45aSChristoph Lameter { 3087d0a21265SDavid Rientjes struct vm_struct *area; 308819f1c3acSAndrey Konovalov void *ret; 3089f6e39794SAndrey Konovalov kasan_vmalloc_flags_t kasan_flags = KASAN_VMALLOC_NONE; 3090d0a21265SDavid Rientjes unsigned long real_size = size; 3091121e6f32SNicholas Piggin unsigned long real_align = align; 3092121e6f32SNicholas Piggin unsigned int shift = PAGE_SHIFT; 3093d0a21265SDavid Rientjes 3094d70bec8cSNicholas Piggin if (WARN_ON_ONCE(!size)) 3095d70bec8cSNicholas Piggin return NULL; 3096d70bec8cSNicholas Piggin 3097d70bec8cSNicholas Piggin if ((size >> PAGE_SHIFT) > totalram_pages()) { 3098d70bec8cSNicholas Piggin warn_alloc(gfp_mask, NULL, 3099f4bdfeafSUladzislau Rezki (Sony) "vmalloc error: size %lu, exceeds total pages", 3100f4bdfeafSUladzislau Rezki (Sony) real_size); 3101d70bec8cSNicholas Piggin return NULL; 3102121e6f32SNicholas Piggin } 3103d0a21265SDavid Rientjes 3104559089e0SSong Liu if (vmap_allow_huge && (vm_flags & VM_ALLOW_HUGE_VMAP)) { 3105121e6f32SNicholas Piggin unsigned long size_per_node; 3106121e6f32SNicholas Piggin 3107121e6f32SNicholas Piggin /* 3108121e6f32SNicholas Piggin * Try huge pages. Only try for PAGE_KERNEL allocations, 3109121e6f32SNicholas Piggin * others like modules don't yet expect huge pages in 3110121e6f32SNicholas Piggin * their allocations due to apply_to_page_range not 3111121e6f32SNicholas Piggin * supporting them. 3112121e6f32SNicholas Piggin */ 3113121e6f32SNicholas Piggin 3114121e6f32SNicholas Piggin size_per_node = size; 3115121e6f32SNicholas Piggin if (node == NUMA_NO_NODE) 3116121e6f32SNicholas Piggin size_per_node /= num_online_nodes(); 31173382bbeeSChristophe Leroy if (arch_vmap_pmd_supported(prot) && size_per_node >= PMD_SIZE) 3118121e6f32SNicholas Piggin shift = PMD_SHIFT; 31193382bbeeSChristophe Leroy else 31203382bbeeSChristophe Leroy shift = arch_vmap_pte_supported_shift(size_per_node); 31213382bbeeSChristophe Leroy 3122121e6f32SNicholas Piggin align = max(real_align, 1UL << shift); 3123121e6f32SNicholas Piggin size = ALIGN(real_size, 1UL << shift); 3124121e6f32SNicholas Piggin } 3125121e6f32SNicholas Piggin 3126121e6f32SNicholas Piggin again: 31277ca3027bSDaniel Axtens area = __get_vm_area_node(real_size, align, shift, VM_ALLOC | 31287ca3027bSDaniel Axtens VM_UNINITIALIZED | vm_flags, start, end, node, 31297ca3027bSDaniel Axtens gfp_mask, caller); 3130d70bec8cSNicholas Piggin if (!area) { 31319376130cSMichal Hocko bool nofail = gfp_mask & __GFP_NOFAIL; 3132d70bec8cSNicholas Piggin warn_alloc(gfp_mask, NULL, 31339376130cSMichal Hocko "vmalloc error: size %lu, vm_struct allocation failed%s", 31349376130cSMichal Hocko real_size, (nofail) ? ". Retrying." : ""); 31359376130cSMichal Hocko if (nofail) { 31369376130cSMichal Hocko schedule_timeout_uninterruptible(1); 31379376130cSMichal Hocko goto again; 31389376130cSMichal Hocko } 3139de7d2b56SJoe Perches goto fail; 3140d70bec8cSNicholas Piggin } 3141d0a21265SDavid Rientjes 3142f6e39794SAndrey Konovalov /* 3143f6e39794SAndrey Konovalov * Prepare arguments for __vmalloc_area_node() and 3144f6e39794SAndrey Konovalov * kasan_unpoison_vmalloc(). 3145f6e39794SAndrey Konovalov */ 3146f6e39794SAndrey Konovalov if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) { 3147f6e39794SAndrey Konovalov if (kasan_hw_tags_enabled()) { 314801d92c7fSAndrey Konovalov /* 314901d92c7fSAndrey Konovalov * Modify protection bits to allow tagging. 3150f6e39794SAndrey Konovalov * This must be done before mapping. 315101d92c7fSAndrey Konovalov */ 315201d92c7fSAndrey Konovalov prot = arch_vmap_pgprot_tagged(prot); 315301d92c7fSAndrey Konovalov 315423689e91SAndrey Konovalov /* 3155f6e39794SAndrey Konovalov * Skip page_alloc poisoning and zeroing for physical 3156f6e39794SAndrey Konovalov * pages backing VM_ALLOC mapping. Memory is instead 3157f6e39794SAndrey Konovalov * poisoned and zeroed by kasan_unpoison_vmalloc(). 315823689e91SAndrey Konovalov */ 315923689e91SAndrey Konovalov gfp_mask |= __GFP_SKIP_KASAN_UNPOISON | __GFP_SKIP_ZERO; 316023689e91SAndrey Konovalov } 316123689e91SAndrey Konovalov 3162f6e39794SAndrey Konovalov /* Take note that the mapping is PAGE_KERNEL. */ 3163f6e39794SAndrey Konovalov kasan_flags |= KASAN_VMALLOC_PROT_NORMAL; 3164f6e39794SAndrey Konovalov } 3165f6e39794SAndrey Konovalov 316601d92c7fSAndrey Konovalov /* Allocate physical pages and map them into vmalloc space. */ 316719f1c3acSAndrey Konovalov ret = __vmalloc_area_node(area, gfp_mask, prot, shift, node); 316819f1c3acSAndrey Konovalov if (!ret) 3169121e6f32SNicholas Piggin goto fail; 317089219d37SCatalin Marinas 317123689e91SAndrey Konovalov /* 317223689e91SAndrey Konovalov * Mark the pages as accessible, now that they are mapped. 317323689e91SAndrey Konovalov * The init condition should match the one in post_alloc_hook() 317423689e91SAndrey Konovalov * (except for the should_skip_init() check) to make sure that memory 317523689e91SAndrey Konovalov * is initialized under the same conditions regardless of the enabled 317623689e91SAndrey Konovalov * KASAN mode. 3177f6e39794SAndrey Konovalov * Tag-based KASAN modes only assign tags to normal non-executable 3178f6e39794SAndrey Konovalov * allocations, see __kasan_unpoison_vmalloc(). 317923689e91SAndrey Konovalov */ 3180f6e39794SAndrey Konovalov kasan_flags |= KASAN_VMALLOC_VM_ALLOC; 318123689e91SAndrey Konovalov if (!want_init_on_free() && want_init_on_alloc(gfp_mask)) 318223689e91SAndrey Konovalov kasan_flags |= KASAN_VMALLOC_INIT; 3183f6e39794SAndrey Konovalov /* KASAN_VMALLOC_PROT_NORMAL already set if required. */ 318423689e91SAndrey Konovalov area->addr = kasan_unpoison_vmalloc(area->addr, real_size, kasan_flags); 318519f1c3acSAndrey Konovalov 318689219d37SCatalin Marinas /* 318720fc02b4SZhang Yanfei * In this function, newly allocated vm_struct has VM_UNINITIALIZED 318820fc02b4SZhang Yanfei * flag. It means that vm_struct is not fully initialized. 31894341fa45SJoonsoo Kim * Now, it is fully initialized, so remove this flag here. 3190f5252e00SMitsuo Hayasaka */ 319120fc02b4SZhang Yanfei clear_vm_uninitialized_flag(area); 3192f5252e00SMitsuo Hayasaka 31937ca3027bSDaniel Axtens size = PAGE_ALIGN(size); 319460115fa5SKefeng Wang if (!(vm_flags & VM_DEFER_KMEMLEAK)) 319594f4a161SCatalin Marinas kmemleak_vmalloc(area, size, gfp_mask); 319689219d37SCatalin Marinas 319719f1c3acSAndrey Konovalov return area->addr; 3198de7d2b56SJoe Perches 3199de7d2b56SJoe Perches fail: 3200121e6f32SNicholas Piggin if (shift > PAGE_SHIFT) { 3201121e6f32SNicholas Piggin shift = PAGE_SHIFT; 3202121e6f32SNicholas Piggin align = real_align; 3203121e6f32SNicholas Piggin size = real_size; 3204121e6f32SNicholas Piggin goto again; 3205121e6f32SNicholas Piggin } 3206121e6f32SNicholas Piggin 3207de7d2b56SJoe Perches return NULL; 3208930fc45aSChristoph Lameter } 3209930fc45aSChristoph Lameter 32101da177e4SLinus Torvalds /** 3211930fc45aSChristoph Lameter * __vmalloc_node - allocate virtually contiguous memory 32121da177e4SLinus Torvalds * @size: allocation size 32132dca6999SDavid Miller * @align: desired alignment 32141da177e4SLinus Torvalds * @gfp_mask: flags for the page level allocator 321500ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 3216c85d194bSRandy Dunlap * @caller: caller's return address 32171da177e4SLinus Torvalds * 3218f38fcb9cSChristoph Hellwig * Allocate enough pages to cover @size from the page level allocator with 3219f38fcb9cSChristoph Hellwig * @gfp_mask flags. Map them into contiguous kernel virtual space. 3220a7c3e901SMichal Hocko * 3221dcda9b04SMichal Hocko * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL 3222a7c3e901SMichal Hocko * and __GFP_NOFAIL are not supported 3223a7c3e901SMichal Hocko * 3224a7c3e901SMichal Hocko * Any use of gfp flags outside of GFP_KERNEL should be consulted 3225a7c3e901SMichal Hocko * with mm people. 3226a862f68aSMike Rapoport * 3227a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 32281da177e4SLinus Torvalds */ 32292b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align, 3230f38fcb9cSChristoph Hellwig gfp_t gfp_mask, int node, const void *caller) 32311da177e4SLinus Torvalds { 3232d0a21265SDavid Rientjes return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, 3233f38fcb9cSChristoph Hellwig gfp_mask, PAGE_KERNEL, 0, node, caller); 32341da177e4SLinus Torvalds } 3235c3f896dcSChristoph Hellwig /* 3236c3f896dcSChristoph Hellwig * This is only for performance analysis of vmalloc and stress purpose. 3237c3f896dcSChristoph Hellwig * It is required by vmalloc test module, therefore do not use it other 3238c3f896dcSChristoph Hellwig * than that. 3239c3f896dcSChristoph Hellwig */ 3240c3f896dcSChristoph Hellwig #ifdef CONFIG_TEST_VMALLOC_MODULE 3241c3f896dcSChristoph Hellwig EXPORT_SYMBOL_GPL(__vmalloc_node); 3242c3f896dcSChristoph Hellwig #endif 32431da177e4SLinus Torvalds 324488dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask) 3245930fc45aSChristoph Lameter { 3246f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE, 324723016969SChristoph Lameter __builtin_return_address(0)); 3248930fc45aSChristoph Lameter } 32491da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc); 32501da177e4SLinus Torvalds 32511da177e4SLinus Torvalds /** 32521da177e4SLinus Torvalds * vmalloc - allocate virtually contiguous memory 32531da177e4SLinus Torvalds * @size: allocation size 325492eac168SMike Rapoport * 32551da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 32561da177e4SLinus Torvalds * allocator and map them into contiguous kernel virtual space. 32571da177e4SLinus Torvalds * 3258c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 32591da177e4SLinus Torvalds * use __vmalloc() instead. 3260a862f68aSMike Rapoport * 3261a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 32621da177e4SLinus Torvalds */ 32631da177e4SLinus Torvalds void *vmalloc(unsigned long size) 32641da177e4SLinus Torvalds { 32654d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE, 32664d39d728SChristoph Hellwig __builtin_return_address(0)); 32671da177e4SLinus Torvalds } 32681da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc); 32691da177e4SLinus Torvalds 3270930fc45aSChristoph Lameter /** 3271559089e0SSong Liu * vmalloc_huge - allocate virtually contiguous memory, allow huge pages 327215a64f5aSClaudio Imbrenda * @size: allocation size 3273559089e0SSong Liu * @gfp_mask: flags for the page level allocator 327415a64f5aSClaudio Imbrenda * 3275559089e0SSong Liu * Allocate enough pages to cover @size from the page level 327615a64f5aSClaudio Imbrenda * allocator and map them into contiguous kernel virtual space. 3277559089e0SSong Liu * If @size is greater than or equal to PMD_SIZE, allow using 3278559089e0SSong Liu * huge pages for the memory 327915a64f5aSClaudio Imbrenda * 328015a64f5aSClaudio Imbrenda * Return: pointer to the allocated memory or %NULL on error 328115a64f5aSClaudio Imbrenda */ 3282559089e0SSong Liu void *vmalloc_huge(unsigned long size, gfp_t gfp_mask) 328315a64f5aSClaudio Imbrenda { 328415a64f5aSClaudio Imbrenda return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, 3285559089e0SSong Liu gfp_mask, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP, 328615a64f5aSClaudio Imbrenda NUMA_NO_NODE, __builtin_return_address(0)); 328715a64f5aSClaudio Imbrenda } 3288559089e0SSong Liu EXPORT_SYMBOL_GPL(vmalloc_huge); 328915a64f5aSClaudio Imbrenda 329015a64f5aSClaudio Imbrenda /** 3291e1ca7788SDave Young * vzalloc - allocate virtually contiguous memory with zero fill 3292e1ca7788SDave Young * @size: allocation size 329392eac168SMike Rapoport * 3294e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 3295e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 3296e1ca7788SDave Young * The memory allocated is set to zero. 3297e1ca7788SDave Young * 3298e1ca7788SDave Young * For tight control over page level allocator and protection flags 3299e1ca7788SDave Young * use __vmalloc() instead. 3300a862f68aSMike Rapoport * 3301a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 3302e1ca7788SDave Young */ 3303e1ca7788SDave Young void *vzalloc(unsigned long size) 3304e1ca7788SDave Young { 33054d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE, 33064d39d728SChristoph Hellwig __builtin_return_address(0)); 3307e1ca7788SDave Young } 3308e1ca7788SDave Young EXPORT_SYMBOL(vzalloc); 3309e1ca7788SDave Young 3310e1ca7788SDave Young /** 3311ead04089SRolf Eike Beer * vmalloc_user - allocate zeroed virtually contiguous memory for userspace 331283342314SNick Piggin * @size: allocation size 3313ead04089SRolf Eike Beer * 3314ead04089SRolf Eike Beer * The resulting memory area is zeroed so it can be mapped to userspace 3315ead04089SRolf Eike Beer * without leaking data. 3316a862f68aSMike Rapoport * 3317a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 331883342314SNick Piggin */ 331983342314SNick Piggin void *vmalloc_user(unsigned long size) 332083342314SNick Piggin { 3321bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 3322bc84c535SRoman Penyaev GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL, 3323bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 332400ef2d2fSDavid Rientjes __builtin_return_address(0)); 332583342314SNick Piggin } 332683342314SNick Piggin EXPORT_SYMBOL(vmalloc_user); 332783342314SNick Piggin 332883342314SNick Piggin /** 3329930fc45aSChristoph Lameter * vmalloc_node - allocate memory on a specific node 3330930fc45aSChristoph Lameter * @size: allocation size 3331d44e0780SRandy Dunlap * @node: numa node 3332930fc45aSChristoph Lameter * 3333930fc45aSChristoph Lameter * Allocate enough pages to cover @size from the page level 3334930fc45aSChristoph Lameter * allocator and map them into contiguous kernel virtual space. 3335930fc45aSChristoph Lameter * 3336c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 3337930fc45aSChristoph Lameter * use __vmalloc() instead. 3338a862f68aSMike Rapoport * 3339a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 3340930fc45aSChristoph Lameter */ 3341930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node) 3342930fc45aSChristoph Lameter { 3343f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL, node, 3344f38fcb9cSChristoph Hellwig __builtin_return_address(0)); 3345930fc45aSChristoph Lameter } 3346930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node); 3347930fc45aSChristoph Lameter 3348e1ca7788SDave Young /** 3349e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill 3350e1ca7788SDave Young * @size: allocation size 3351e1ca7788SDave Young * @node: numa node 3352e1ca7788SDave Young * 3353e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 3354e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 3355e1ca7788SDave Young * The memory allocated is set to zero. 3356e1ca7788SDave Young * 3357a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 3358e1ca7788SDave Young */ 3359e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node) 3360e1ca7788SDave Young { 33614d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node, 33624d39d728SChristoph Hellwig __builtin_return_address(0)); 3363e1ca7788SDave Young } 3364e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node); 3365e1ca7788SDave Young 33660d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) 3367698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) 33680d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) 3369698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL) 33700d08e0d3SAndi Kleen #else 3371698d0831SMichal Hocko /* 3372698d0831SMichal Hocko * 64b systems should always have either DMA or DMA32 zones. For others 3373698d0831SMichal Hocko * GFP_DMA32 should do the right thing and use the normal zone. 3374698d0831SMichal Hocko */ 337568d68ff6SZhiyuan Dai #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) 33760d08e0d3SAndi Kleen #endif 33770d08e0d3SAndi Kleen 33781da177e4SLinus Torvalds /** 33791da177e4SLinus Torvalds * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 33801da177e4SLinus Torvalds * @size: allocation size 33811da177e4SLinus Torvalds * 33821da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 33831da177e4SLinus Torvalds * page level allocator and map them into contiguous kernel virtual space. 3384a862f68aSMike Rapoport * 3385a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 33861da177e4SLinus Torvalds */ 33871da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 33881da177e4SLinus Torvalds { 3389f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE, 3390f38fcb9cSChristoph Hellwig __builtin_return_address(0)); 33911da177e4SLinus Torvalds } 33921da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32); 33931da177e4SLinus Torvalds 339483342314SNick Piggin /** 3395ead04089SRolf Eike Beer * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 339683342314SNick Piggin * @size: allocation size 3397ead04089SRolf Eike Beer * 3398ead04089SRolf Eike Beer * The resulting memory area is 32bit addressable and zeroed so it can be 3399ead04089SRolf Eike Beer * mapped to userspace without leaking data. 3400a862f68aSMike Rapoport * 3401a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 340283342314SNick Piggin */ 340383342314SNick Piggin void *vmalloc_32_user(unsigned long size) 340483342314SNick Piggin { 3405bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 3406bc84c535SRoman Penyaev GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, 3407bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 34085a82ac71SRoman Penyaev __builtin_return_address(0)); 340983342314SNick Piggin } 341083342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user); 341183342314SNick Piggin 3412d0107eb0SKAMEZAWA Hiroyuki /* 3413d0107eb0SKAMEZAWA Hiroyuki * small helper routine , copy contents to buf from addr. 3414d0107eb0SKAMEZAWA Hiroyuki * If the page is not present, fill zero. 3415d0107eb0SKAMEZAWA Hiroyuki */ 3416d0107eb0SKAMEZAWA Hiroyuki 3417d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count) 3418d0107eb0SKAMEZAWA Hiroyuki { 3419d0107eb0SKAMEZAWA Hiroyuki struct page *p; 3420d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 3421d0107eb0SKAMEZAWA Hiroyuki 3422d0107eb0SKAMEZAWA Hiroyuki while (count) { 3423d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 3424d0107eb0SKAMEZAWA Hiroyuki 3425891c49abSAlexander Kuleshov offset = offset_in_page(addr); 3426d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 3427d0107eb0SKAMEZAWA Hiroyuki if (length > count) 3428d0107eb0SKAMEZAWA Hiroyuki length = count; 3429d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 3430d0107eb0SKAMEZAWA Hiroyuki /* 3431d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 3432d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 3433f0953a1bSIngo Molnar * overhead of vmalloc()/vfree() calls for this _debug_ 3434d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 3435d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 3436d0107eb0SKAMEZAWA Hiroyuki */ 3437d0107eb0SKAMEZAWA Hiroyuki if (p) { 3438f7c8ce44SDavid Hildenbrand /* We can expect USER0 is not used -- see vread() */ 34399b04c5feSCong Wang void *map = kmap_atomic(p); 3440d0107eb0SKAMEZAWA Hiroyuki memcpy(buf, map + offset, length); 34419b04c5feSCong Wang kunmap_atomic(map); 3442d0107eb0SKAMEZAWA Hiroyuki } else 3443d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, length); 3444d0107eb0SKAMEZAWA Hiroyuki 3445d0107eb0SKAMEZAWA Hiroyuki addr += length; 3446d0107eb0SKAMEZAWA Hiroyuki buf += length; 3447d0107eb0SKAMEZAWA Hiroyuki copied += length; 3448d0107eb0SKAMEZAWA Hiroyuki count -= length; 3449d0107eb0SKAMEZAWA Hiroyuki } 3450d0107eb0SKAMEZAWA Hiroyuki return copied; 3451d0107eb0SKAMEZAWA Hiroyuki } 3452d0107eb0SKAMEZAWA Hiroyuki 3453d0107eb0SKAMEZAWA Hiroyuki /** 3454d0107eb0SKAMEZAWA Hiroyuki * vread() - read vmalloc area in a safe way. 3455d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for reading data 3456d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 3457d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 3458d0107eb0SKAMEZAWA Hiroyuki * 3459d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 3460d0107eb0SKAMEZAWA Hiroyuki * copy data from that area to a given buffer. If the given memory range 3461d0107eb0SKAMEZAWA Hiroyuki * of [addr...addr+count) includes some valid address, data is copied to 3462d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, they'll be zero-filled. 3463d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 3464d0107eb0SKAMEZAWA Hiroyuki * 3465d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 3466a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 3467d0107eb0SKAMEZAWA Hiroyuki * 3468d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vread() is never necessary because the caller 3469d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 3470d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 3471bbcd53c9SDavid Hildenbrand * any information, as /proc/kcore. 3472a862f68aSMike Rapoport * 3473a862f68aSMike Rapoport * Return: number of bytes for which addr and buf should be increased 3474a862f68aSMike Rapoport * (same number as @count) or %0 if [addr...addr+count) doesn't 3475a862f68aSMike Rapoport * include any intersection with valid vmalloc area 3476d0107eb0SKAMEZAWA Hiroyuki */ 34771da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 34781da177e4SLinus Torvalds { 3479e81ce85fSJoonsoo Kim struct vmap_area *va; 3480e81ce85fSJoonsoo Kim struct vm_struct *vm; 34811da177e4SLinus Torvalds char *vaddr, *buf_start = buf; 3482d0107eb0SKAMEZAWA Hiroyuki unsigned long buflen = count; 34831da177e4SLinus Torvalds unsigned long n; 34841da177e4SLinus Torvalds 34854aff1dc4SAndrey Konovalov addr = kasan_reset_tag(addr); 34864aff1dc4SAndrey Konovalov 34871da177e4SLinus Torvalds /* Don't allow overflow */ 34881da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 34891da177e4SLinus Torvalds count = -(unsigned long) addr; 34901da177e4SLinus Torvalds 3491e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 3492f181234aSChen Wandun va = find_vmap_area_exceed_addr((unsigned long)addr); 3493f608788cSSerapheim Dimitropoulos if (!va) 3494f608788cSSerapheim Dimitropoulos goto finished; 3495f181234aSChen Wandun 3496f181234aSChen Wandun /* no intersects with alive vmap_area */ 3497f181234aSChen Wandun if ((unsigned long)addr + count <= va->va_start) 3498f181234aSChen Wandun goto finished; 3499f181234aSChen Wandun 3500f608788cSSerapheim Dimitropoulos list_for_each_entry_from(va, &vmap_area_list, list) { 3501e81ce85fSJoonsoo Kim if (!count) 3502e81ce85fSJoonsoo Kim break; 3503e81ce85fSJoonsoo Kim 3504688fcbfcSPengfei Li if (!va->vm) 3505e81ce85fSJoonsoo Kim continue; 3506e81ce85fSJoonsoo Kim 3507e81ce85fSJoonsoo Kim vm = va->vm; 3508e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 3509762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 35101da177e4SLinus Torvalds continue; 35111da177e4SLinus Torvalds while (addr < vaddr) { 35121da177e4SLinus Torvalds if (count == 0) 35131da177e4SLinus Torvalds goto finished; 35141da177e4SLinus Torvalds *buf = '\0'; 35151da177e4SLinus Torvalds buf++; 35161da177e4SLinus Torvalds addr++; 35171da177e4SLinus Torvalds count--; 35181da177e4SLinus Torvalds } 3519762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 3520d0107eb0SKAMEZAWA Hiroyuki if (n > count) 3521d0107eb0SKAMEZAWA Hiroyuki n = count; 3522e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) 3523d0107eb0SKAMEZAWA Hiroyuki aligned_vread(buf, addr, n); 3524d0107eb0SKAMEZAWA Hiroyuki else /* IOREMAP area is treated as memory hole */ 3525d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, n); 3526d0107eb0SKAMEZAWA Hiroyuki buf += n; 3527d0107eb0SKAMEZAWA Hiroyuki addr += n; 3528d0107eb0SKAMEZAWA Hiroyuki count -= n; 35291da177e4SLinus Torvalds } 35301da177e4SLinus Torvalds finished: 3531e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 3532d0107eb0SKAMEZAWA Hiroyuki 3533d0107eb0SKAMEZAWA Hiroyuki if (buf == buf_start) 3534d0107eb0SKAMEZAWA Hiroyuki return 0; 3535d0107eb0SKAMEZAWA Hiroyuki /* zero-fill memory holes */ 3536d0107eb0SKAMEZAWA Hiroyuki if (buf != buf_start + buflen) 3537d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, buflen - (buf - buf_start)); 3538d0107eb0SKAMEZAWA Hiroyuki 3539d0107eb0SKAMEZAWA Hiroyuki return buflen; 35401da177e4SLinus Torvalds } 35411da177e4SLinus Torvalds 3542d0107eb0SKAMEZAWA Hiroyuki /** 3543e69e9d4aSHATAYAMA Daisuke * remap_vmalloc_range_partial - map vmalloc pages to userspace 3544e69e9d4aSHATAYAMA Daisuke * @vma: vma to cover 3545e69e9d4aSHATAYAMA Daisuke * @uaddr: target user address to start at 3546e69e9d4aSHATAYAMA Daisuke * @kaddr: virtual address of vmalloc kernel memory 3547bdebd6a2SJann Horn * @pgoff: offset from @kaddr to start at 3548e69e9d4aSHATAYAMA Daisuke * @size: size of map area 3549e69e9d4aSHATAYAMA Daisuke * 3550e69e9d4aSHATAYAMA Daisuke * Returns: 0 for success, -Exxx on failure 3551e69e9d4aSHATAYAMA Daisuke * 3552e69e9d4aSHATAYAMA Daisuke * This function checks that @kaddr is a valid vmalloc'ed area, 3553e69e9d4aSHATAYAMA Daisuke * and that it is big enough to cover the range starting at 3554e69e9d4aSHATAYAMA Daisuke * @uaddr in @vma. Will return failure if that criteria isn't 3555e69e9d4aSHATAYAMA Daisuke * met. 3556e69e9d4aSHATAYAMA Daisuke * 3557e69e9d4aSHATAYAMA Daisuke * Similar to remap_pfn_range() (see mm/memory.c) 3558e69e9d4aSHATAYAMA Daisuke */ 3559e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr, 3560bdebd6a2SJann Horn void *kaddr, unsigned long pgoff, 3561bdebd6a2SJann Horn unsigned long size) 3562e69e9d4aSHATAYAMA Daisuke { 3563e69e9d4aSHATAYAMA Daisuke struct vm_struct *area; 3564bdebd6a2SJann Horn unsigned long off; 3565bdebd6a2SJann Horn unsigned long end_index; 3566bdebd6a2SJann Horn 3567bdebd6a2SJann Horn if (check_shl_overflow(pgoff, PAGE_SHIFT, &off)) 3568bdebd6a2SJann Horn return -EINVAL; 3569e69e9d4aSHATAYAMA Daisuke 3570e69e9d4aSHATAYAMA Daisuke size = PAGE_ALIGN(size); 3571e69e9d4aSHATAYAMA Daisuke 3572e69e9d4aSHATAYAMA Daisuke if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr)) 3573e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3574e69e9d4aSHATAYAMA Daisuke 3575e69e9d4aSHATAYAMA Daisuke area = find_vm_area(kaddr); 3576e69e9d4aSHATAYAMA Daisuke if (!area) 3577e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3578e69e9d4aSHATAYAMA Daisuke 3579fe9041c2SChristoph Hellwig if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT))) 3580e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3581e69e9d4aSHATAYAMA Daisuke 3582bdebd6a2SJann Horn if (check_add_overflow(size, off, &end_index) || 3583bdebd6a2SJann Horn end_index > get_vm_area_size(area)) 3584e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3585bdebd6a2SJann Horn kaddr += off; 3586e69e9d4aSHATAYAMA Daisuke 3587e69e9d4aSHATAYAMA Daisuke do { 3588e69e9d4aSHATAYAMA Daisuke struct page *page = vmalloc_to_page(kaddr); 3589e69e9d4aSHATAYAMA Daisuke int ret; 3590e69e9d4aSHATAYAMA Daisuke 3591e69e9d4aSHATAYAMA Daisuke ret = vm_insert_page(vma, uaddr, page); 3592e69e9d4aSHATAYAMA Daisuke if (ret) 3593e69e9d4aSHATAYAMA Daisuke return ret; 3594e69e9d4aSHATAYAMA Daisuke 3595e69e9d4aSHATAYAMA Daisuke uaddr += PAGE_SIZE; 3596e69e9d4aSHATAYAMA Daisuke kaddr += PAGE_SIZE; 3597e69e9d4aSHATAYAMA Daisuke size -= PAGE_SIZE; 3598e69e9d4aSHATAYAMA Daisuke } while (size > 0); 3599e69e9d4aSHATAYAMA Daisuke 3600e69e9d4aSHATAYAMA Daisuke vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 3601e69e9d4aSHATAYAMA Daisuke 3602e69e9d4aSHATAYAMA Daisuke return 0; 3603e69e9d4aSHATAYAMA Daisuke } 3604e69e9d4aSHATAYAMA Daisuke 3605e69e9d4aSHATAYAMA Daisuke /** 360683342314SNick Piggin * remap_vmalloc_range - map vmalloc pages to userspace 360783342314SNick Piggin * @vma: vma to cover (map full range of vma) 360883342314SNick Piggin * @addr: vmalloc memory 360983342314SNick Piggin * @pgoff: number of pages into addr before first page to map 36107682486bSRandy Dunlap * 36117682486bSRandy Dunlap * Returns: 0 for success, -Exxx on failure 361283342314SNick Piggin * 361383342314SNick Piggin * This function checks that addr is a valid vmalloc'ed area, and 361483342314SNick Piggin * that it is big enough to cover the vma. Will return failure if 361583342314SNick Piggin * that criteria isn't met. 361683342314SNick Piggin * 361772fd4a35SRobert P. J. Day * Similar to remap_pfn_range() (see mm/memory.c) 361883342314SNick Piggin */ 361983342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 362083342314SNick Piggin unsigned long pgoff) 362183342314SNick Piggin { 3622e69e9d4aSHATAYAMA Daisuke return remap_vmalloc_range_partial(vma, vma->vm_start, 3623bdebd6a2SJann Horn addr, pgoff, 3624e69e9d4aSHATAYAMA Daisuke vma->vm_end - vma->vm_start); 362583342314SNick Piggin } 362683342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range); 362783342314SNick Piggin 36285f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area) 36295f4352fbSJeremy Fitzhardinge { 36305f4352fbSJeremy Fitzhardinge struct vm_struct *ret; 36315f4352fbSJeremy Fitzhardinge ret = remove_vm_area(area->addr); 36325f4352fbSJeremy Fitzhardinge BUG_ON(ret != area); 36335f4352fbSJeremy Fitzhardinge kfree(area); 36345f4352fbSJeremy Fitzhardinge } 36355f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area); 3636a10aa579SChristoph Lameter 36374f8b02b4STejun Heo #ifdef CONFIG_SMP 3638ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n) 3639ca23e405STejun Heo { 36404583e773SGeliang Tang return rb_entry_safe(n, struct vmap_area, rb_node); 3641ca23e405STejun Heo } 3642ca23e405STejun Heo 3643ca23e405STejun Heo /** 364468ad4a33SUladzislau Rezki (Sony) * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to 364568ad4a33SUladzislau Rezki (Sony) * @addr: target address 3646ca23e405STejun Heo * 364768ad4a33SUladzislau Rezki (Sony) * Returns: vmap_area if it is found. If there is no such area 364868ad4a33SUladzislau Rezki (Sony) * the first highest(reverse order) vmap_area is returned 364968ad4a33SUladzislau Rezki (Sony) * i.e. va->va_start < addr && va->va_end < addr or NULL 365068ad4a33SUladzislau Rezki (Sony) * if there are no any areas before @addr. 3651ca23e405STejun Heo */ 365268ad4a33SUladzislau Rezki (Sony) static struct vmap_area * 365368ad4a33SUladzislau Rezki (Sony) pvm_find_va_enclose_addr(unsigned long addr) 3654ca23e405STejun Heo { 365568ad4a33SUladzislau Rezki (Sony) struct vmap_area *va, *tmp; 365668ad4a33SUladzislau Rezki (Sony) struct rb_node *n; 365768ad4a33SUladzislau Rezki (Sony) 365868ad4a33SUladzislau Rezki (Sony) n = free_vmap_area_root.rb_node; 365968ad4a33SUladzislau Rezki (Sony) va = NULL; 3660ca23e405STejun Heo 3661ca23e405STejun Heo while (n) { 366268ad4a33SUladzislau Rezki (Sony) tmp = rb_entry(n, struct vmap_area, rb_node); 366368ad4a33SUladzislau Rezki (Sony) if (tmp->va_start <= addr) { 366468ad4a33SUladzislau Rezki (Sony) va = tmp; 366568ad4a33SUladzislau Rezki (Sony) if (tmp->va_end >= addr) 3666ca23e405STejun Heo break; 3667ca23e405STejun Heo 366868ad4a33SUladzislau Rezki (Sony) n = n->rb_right; 3669ca23e405STejun Heo } else { 367068ad4a33SUladzislau Rezki (Sony) n = n->rb_left; 3671ca23e405STejun Heo } 367268ad4a33SUladzislau Rezki (Sony) } 367368ad4a33SUladzislau Rezki (Sony) 367468ad4a33SUladzislau Rezki (Sony) return va; 3675ca23e405STejun Heo } 3676ca23e405STejun Heo 3677ca23e405STejun Heo /** 367868ad4a33SUladzislau Rezki (Sony) * pvm_determine_end_from_reverse - find the highest aligned address 367968ad4a33SUladzislau Rezki (Sony) * of free block below VMALLOC_END 368068ad4a33SUladzislau Rezki (Sony) * @va: 368168ad4a33SUladzislau Rezki (Sony) * in - the VA we start the search(reverse order); 368268ad4a33SUladzislau Rezki (Sony) * out - the VA with the highest aligned end address. 3683799fa85dSAlex Shi * @align: alignment for required highest address 3684ca23e405STejun Heo * 368568ad4a33SUladzislau Rezki (Sony) * Returns: determined end address within vmap_area 3686ca23e405STejun Heo */ 368768ad4a33SUladzislau Rezki (Sony) static unsigned long 368868ad4a33SUladzislau Rezki (Sony) pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align) 3689ca23e405STejun Heo { 369068ad4a33SUladzislau Rezki (Sony) unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 3691ca23e405STejun Heo unsigned long addr; 3692ca23e405STejun Heo 369368ad4a33SUladzislau Rezki (Sony) if (likely(*va)) { 369468ad4a33SUladzislau Rezki (Sony) list_for_each_entry_from_reverse((*va), 369568ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list, list) { 369668ad4a33SUladzislau Rezki (Sony) addr = min((*va)->va_end & ~(align - 1), vmalloc_end); 369768ad4a33SUladzislau Rezki (Sony) if ((*va)->va_start < addr) 369868ad4a33SUladzislau Rezki (Sony) return addr; 369968ad4a33SUladzislau Rezki (Sony) } 3700ca23e405STejun Heo } 3701ca23e405STejun Heo 370268ad4a33SUladzislau Rezki (Sony) return 0; 3703ca23e405STejun Heo } 3704ca23e405STejun Heo 3705ca23e405STejun Heo /** 3706ca23e405STejun Heo * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator 3707ca23e405STejun Heo * @offsets: array containing offset of each area 3708ca23e405STejun Heo * @sizes: array containing size of each area 3709ca23e405STejun Heo * @nr_vms: the number of areas to allocate 3710ca23e405STejun Heo * @align: alignment, all entries in @offsets and @sizes must be aligned to this 3711ca23e405STejun Heo * 3712ca23e405STejun Heo * Returns: kmalloc'd vm_struct pointer array pointing to allocated 3713ca23e405STejun Heo * vm_structs on success, %NULL on failure 3714ca23e405STejun Heo * 3715ca23e405STejun Heo * Percpu allocator wants to use congruent vm areas so that it can 3716ca23e405STejun Heo * maintain the offsets among percpu areas. This function allocates 3717ec3f64fcSDavid Rientjes * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to 3718ec3f64fcSDavid Rientjes * be scattered pretty far, distance between two areas easily going up 3719ec3f64fcSDavid Rientjes * to gigabytes. To avoid interacting with regular vmallocs, these 3720ec3f64fcSDavid Rientjes * areas are allocated from top. 3721ca23e405STejun Heo * 3722ca23e405STejun Heo * Despite its complicated look, this allocator is rather simple. It 372368ad4a33SUladzislau Rezki (Sony) * does everything top-down and scans free blocks from the end looking 372468ad4a33SUladzislau Rezki (Sony) * for matching base. While scanning, if any of the areas do not fit the 372568ad4a33SUladzislau Rezki (Sony) * base address is pulled down to fit the area. Scanning is repeated till 372668ad4a33SUladzislau Rezki (Sony) * all the areas fit and then all necessary data structures are inserted 372768ad4a33SUladzislau Rezki (Sony) * and the result is returned. 3728ca23e405STejun Heo */ 3729ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, 3730ca23e405STejun Heo const size_t *sizes, int nr_vms, 3731ec3f64fcSDavid Rientjes size_t align) 3732ca23e405STejun Heo { 3733ca23e405STejun Heo const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); 3734ca23e405STejun Heo const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 373568ad4a33SUladzislau Rezki (Sony) struct vmap_area **vas, *va; 3736ca23e405STejun Heo struct vm_struct **vms; 3737ca23e405STejun Heo int area, area2, last_area, term_area; 3738253a496dSDaniel Axtens unsigned long base, start, size, end, last_end, orig_start, orig_end; 3739ca23e405STejun Heo bool purged = false; 374068ad4a33SUladzislau Rezki (Sony) enum fit_type type; 3741ca23e405STejun Heo 3742ca23e405STejun Heo /* verify parameters and allocate data structures */ 3743891c49abSAlexander Kuleshov BUG_ON(offset_in_page(align) || !is_power_of_2(align)); 3744ca23e405STejun Heo for (last_area = 0, area = 0; area < nr_vms; area++) { 3745ca23e405STejun Heo start = offsets[area]; 3746ca23e405STejun Heo end = start + sizes[area]; 3747ca23e405STejun Heo 3748ca23e405STejun Heo /* is everything aligned properly? */ 3749ca23e405STejun Heo BUG_ON(!IS_ALIGNED(offsets[area], align)); 3750ca23e405STejun Heo BUG_ON(!IS_ALIGNED(sizes[area], align)); 3751ca23e405STejun Heo 3752ca23e405STejun Heo /* detect the area with the highest address */ 3753ca23e405STejun Heo if (start > offsets[last_area]) 3754ca23e405STejun Heo last_area = area; 3755ca23e405STejun Heo 3756c568da28SWei Yang for (area2 = area + 1; area2 < nr_vms; area2++) { 3757ca23e405STejun Heo unsigned long start2 = offsets[area2]; 3758ca23e405STejun Heo unsigned long end2 = start2 + sizes[area2]; 3759ca23e405STejun Heo 3760c568da28SWei Yang BUG_ON(start2 < end && start < end2); 3761ca23e405STejun Heo } 3762ca23e405STejun Heo } 3763ca23e405STejun Heo last_end = offsets[last_area] + sizes[last_area]; 3764ca23e405STejun Heo 3765ca23e405STejun Heo if (vmalloc_end - vmalloc_start < last_end) { 3766ca23e405STejun Heo WARN_ON(true); 3767ca23e405STejun Heo return NULL; 3768ca23e405STejun Heo } 3769ca23e405STejun Heo 37704d67d860SThomas Meyer vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL); 37714d67d860SThomas Meyer vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL); 3772ca23e405STejun Heo if (!vas || !vms) 3773f1db7afdSKautuk Consul goto err_free2; 3774ca23e405STejun Heo 3775ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 377668ad4a33SUladzislau Rezki (Sony) vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL); 3777ec3f64fcSDavid Rientjes vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL); 3778ca23e405STejun Heo if (!vas[area] || !vms[area]) 3779ca23e405STejun Heo goto err_free; 3780ca23e405STejun Heo } 3781ca23e405STejun Heo retry: 3782e36176beSUladzislau Rezki (Sony) spin_lock(&free_vmap_area_lock); 3783ca23e405STejun Heo 3784ca23e405STejun Heo /* start scanning - we scan from the top, begin with the last area */ 3785ca23e405STejun Heo area = term_area = last_area; 3786ca23e405STejun Heo start = offsets[area]; 3787ca23e405STejun Heo end = start + sizes[area]; 3788ca23e405STejun Heo 378968ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(vmalloc_end); 379068ad4a33SUladzislau Rezki (Sony) base = pvm_determine_end_from_reverse(&va, align) - end; 3791ca23e405STejun Heo 3792ca23e405STejun Heo while (true) { 3793ca23e405STejun Heo /* 3794ca23e405STejun Heo * base might have underflowed, add last_end before 3795ca23e405STejun Heo * comparing. 3796ca23e405STejun Heo */ 379768ad4a33SUladzislau Rezki (Sony) if (base + last_end < vmalloc_start + last_end) 379868ad4a33SUladzislau Rezki (Sony) goto overflow; 3799ca23e405STejun Heo 3800ca23e405STejun Heo /* 380168ad4a33SUladzislau Rezki (Sony) * Fitting base has not been found. 3802ca23e405STejun Heo */ 380368ad4a33SUladzislau Rezki (Sony) if (va == NULL) 380468ad4a33SUladzislau Rezki (Sony) goto overflow; 3805ca23e405STejun Heo 3806ca23e405STejun Heo /* 3807d8cc323dSQiujun Huang * If required width exceeds current VA block, move 38085336e52cSKuppuswamy Sathyanarayanan * base downwards and then recheck. 38095336e52cSKuppuswamy Sathyanarayanan */ 38105336e52cSKuppuswamy Sathyanarayanan if (base + end > va->va_end) { 38115336e52cSKuppuswamy Sathyanarayanan base = pvm_determine_end_from_reverse(&va, align) - end; 38125336e52cSKuppuswamy Sathyanarayanan term_area = area; 38135336e52cSKuppuswamy Sathyanarayanan continue; 38145336e52cSKuppuswamy Sathyanarayanan } 38155336e52cSKuppuswamy Sathyanarayanan 38165336e52cSKuppuswamy Sathyanarayanan /* 381768ad4a33SUladzislau Rezki (Sony) * If this VA does not fit, move base downwards and recheck. 3818ca23e405STejun Heo */ 38195336e52cSKuppuswamy Sathyanarayanan if (base + start < va->va_start) { 382068ad4a33SUladzislau Rezki (Sony) va = node_to_va(rb_prev(&va->rb_node)); 382168ad4a33SUladzislau Rezki (Sony) base = pvm_determine_end_from_reverse(&va, align) - end; 3822ca23e405STejun Heo term_area = area; 3823ca23e405STejun Heo continue; 3824ca23e405STejun Heo } 3825ca23e405STejun Heo 3826ca23e405STejun Heo /* 3827ca23e405STejun Heo * This area fits, move on to the previous one. If 3828ca23e405STejun Heo * the previous one is the terminal one, we're done. 3829ca23e405STejun Heo */ 3830ca23e405STejun Heo area = (area + nr_vms - 1) % nr_vms; 3831ca23e405STejun Heo if (area == term_area) 3832ca23e405STejun Heo break; 383368ad4a33SUladzislau Rezki (Sony) 3834ca23e405STejun Heo start = offsets[area]; 3835ca23e405STejun Heo end = start + sizes[area]; 383668ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(base + end); 3837ca23e405STejun Heo } 383868ad4a33SUladzislau Rezki (Sony) 3839ca23e405STejun Heo /* we've found a fitting base, insert all va's */ 3840ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 384168ad4a33SUladzislau Rezki (Sony) int ret; 3842ca23e405STejun Heo 384368ad4a33SUladzislau Rezki (Sony) start = base + offsets[area]; 384468ad4a33SUladzislau Rezki (Sony) size = sizes[area]; 384568ad4a33SUladzislau Rezki (Sony) 384668ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(start); 384768ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(va == NULL)) 384868ad4a33SUladzislau Rezki (Sony) /* It is a BUG(), but trigger recovery instead. */ 384968ad4a33SUladzislau Rezki (Sony) goto recovery; 385068ad4a33SUladzislau Rezki (Sony) 385168ad4a33SUladzislau Rezki (Sony) type = classify_va_fit_type(va, start, size); 385268ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(type == NOTHING_FIT)) 385368ad4a33SUladzislau Rezki (Sony) /* It is a BUG(), but trigger recovery instead. */ 385468ad4a33SUladzislau Rezki (Sony) goto recovery; 385568ad4a33SUladzislau Rezki (Sony) 385668ad4a33SUladzislau Rezki (Sony) ret = adjust_va_to_fit_type(va, start, size, type); 385768ad4a33SUladzislau Rezki (Sony) if (unlikely(ret)) 385868ad4a33SUladzislau Rezki (Sony) goto recovery; 385968ad4a33SUladzislau Rezki (Sony) 386068ad4a33SUladzislau Rezki (Sony) /* Allocated area. */ 386168ad4a33SUladzislau Rezki (Sony) va = vas[area]; 386268ad4a33SUladzislau Rezki (Sony) va->va_start = start; 386368ad4a33SUladzislau Rezki (Sony) va->va_end = start + size; 3864ca23e405STejun Heo } 3865ca23e405STejun Heo 3866e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 3867ca23e405STejun Heo 3868253a496dSDaniel Axtens /* populate the kasan shadow space */ 3869253a496dSDaniel Axtens for (area = 0; area < nr_vms; area++) { 3870253a496dSDaniel Axtens if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area])) 3871253a496dSDaniel Axtens goto err_free_shadow; 3872253a496dSDaniel Axtens } 3873253a496dSDaniel Axtens 3874ca23e405STejun Heo /* insert all vm's */ 3875e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 3876e36176beSUladzislau Rezki (Sony) for (area = 0; area < nr_vms; area++) { 3877e36176beSUladzislau Rezki (Sony) insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list); 3878e36176beSUladzislau Rezki (Sony) 3879e36176beSUladzislau Rezki (Sony) setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC, 3880ca23e405STejun Heo pcpu_get_vm_areas); 3881e36176beSUladzislau Rezki (Sony) } 3882e36176beSUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 3883ca23e405STejun Heo 388419f1c3acSAndrey Konovalov /* 388519f1c3acSAndrey Konovalov * Mark allocated areas as accessible. Do it now as a best-effort 388619f1c3acSAndrey Konovalov * approach, as they can be mapped outside of vmalloc code. 388723689e91SAndrey Konovalov * With hardware tag-based KASAN, marking is skipped for 388823689e91SAndrey Konovalov * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc(). 388919f1c3acSAndrey Konovalov */ 38901d96320fSAndrey Konovalov for (area = 0; area < nr_vms; area++) 38911d96320fSAndrey Konovalov vms[area]->addr = kasan_unpoison_vmalloc(vms[area]->addr, 3892f6e39794SAndrey Konovalov vms[area]->size, KASAN_VMALLOC_PROT_NORMAL); 38931d96320fSAndrey Konovalov 3894ca23e405STejun Heo kfree(vas); 3895ca23e405STejun Heo return vms; 3896ca23e405STejun Heo 389768ad4a33SUladzislau Rezki (Sony) recovery: 3898e36176beSUladzislau Rezki (Sony) /* 3899e36176beSUladzislau Rezki (Sony) * Remove previously allocated areas. There is no 3900e36176beSUladzislau Rezki (Sony) * need in removing these areas from the busy tree, 3901e36176beSUladzislau Rezki (Sony) * because they are inserted only on the final step 3902e36176beSUladzislau Rezki (Sony) * and when pcpu_get_vm_areas() is success. 3903e36176beSUladzislau Rezki (Sony) */ 390468ad4a33SUladzislau Rezki (Sony) while (area--) { 3905253a496dSDaniel Axtens orig_start = vas[area]->va_start; 3906253a496dSDaniel Axtens orig_end = vas[area]->va_end; 390796e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, 39083c5c3cfbSDaniel Axtens &free_vmap_area_list); 39099c801f61SUladzislau Rezki (Sony) if (va) 3910253a496dSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 3911253a496dSDaniel Axtens va->va_start, va->va_end); 391268ad4a33SUladzislau Rezki (Sony) vas[area] = NULL; 391368ad4a33SUladzislau Rezki (Sony) } 391468ad4a33SUladzislau Rezki (Sony) 391568ad4a33SUladzislau Rezki (Sony) overflow: 3916e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 391768ad4a33SUladzislau Rezki (Sony) if (!purged) { 391868ad4a33SUladzislau Rezki (Sony) purge_vmap_area_lazy(); 391968ad4a33SUladzislau Rezki (Sony) purged = true; 392068ad4a33SUladzislau Rezki (Sony) 392168ad4a33SUladzislau Rezki (Sony) /* Before "retry", check if we recover. */ 392268ad4a33SUladzislau Rezki (Sony) for (area = 0; area < nr_vms; area++) { 392368ad4a33SUladzislau Rezki (Sony) if (vas[area]) 392468ad4a33SUladzislau Rezki (Sony) continue; 392568ad4a33SUladzislau Rezki (Sony) 392668ad4a33SUladzislau Rezki (Sony) vas[area] = kmem_cache_zalloc( 392768ad4a33SUladzislau Rezki (Sony) vmap_area_cachep, GFP_KERNEL); 392868ad4a33SUladzislau Rezki (Sony) if (!vas[area]) 392968ad4a33SUladzislau Rezki (Sony) goto err_free; 393068ad4a33SUladzislau Rezki (Sony) } 393168ad4a33SUladzislau Rezki (Sony) 393268ad4a33SUladzislau Rezki (Sony) goto retry; 393368ad4a33SUladzislau Rezki (Sony) } 393468ad4a33SUladzislau Rezki (Sony) 3935ca23e405STejun Heo err_free: 3936ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 393768ad4a33SUladzislau Rezki (Sony) if (vas[area]) 393868ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, vas[area]); 393968ad4a33SUladzislau Rezki (Sony) 3940ca23e405STejun Heo kfree(vms[area]); 3941ca23e405STejun Heo } 3942f1db7afdSKautuk Consul err_free2: 3943ca23e405STejun Heo kfree(vas); 3944ca23e405STejun Heo kfree(vms); 3945ca23e405STejun Heo return NULL; 3946253a496dSDaniel Axtens 3947253a496dSDaniel Axtens err_free_shadow: 3948253a496dSDaniel Axtens spin_lock(&free_vmap_area_lock); 3949253a496dSDaniel Axtens /* 3950253a496dSDaniel Axtens * We release all the vmalloc shadows, even the ones for regions that 3951253a496dSDaniel Axtens * hadn't been successfully added. This relies on kasan_release_vmalloc 3952253a496dSDaniel Axtens * being able to tolerate this case. 3953253a496dSDaniel Axtens */ 3954253a496dSDaniel Axtens for (area = 0; area < nr_vms; area++) { 3955253a496dSDaniel Axtens orig_start = vas[area]->va_start; 3956253a496dSDaniel Axtens orig_end = vas[area]->va_end; 395796e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, 3958253a496dSDaniel Axtens &free_vmap_area_list); 39599c801f61SUladzislau Rezki (Sony) if (va) 3960253a496dSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 3961253a496dSDaniel Axtens va->va_start, va->va_end); 3962253a496dSDaniel Axtens vas[area] = NULL; 3963253a496dSDaniel Axtens kfree(vms[area]); 3964253a496dSDaniel Axtens } 3965253a496dSDaniel Axtens spin_unlock(&free_vmap_area_lock); 3966253a496dSDaniel Axtens kfree(vas); 3967253a496dSDaniel Axtens kfree(vms); 3968253a496dSDaniel Axtens return NULL; 3969ca23e405STejun Heo } 3970ca23e405STejun Heo 3971ca23e405STejun Heo /** 3972ca23e405STejun Heo * pcpu_free_vm_areas - free vmalloc areas for percpu allocator 3973ca23e405STejun Heo * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() 3974ca23e405STejun Heo * @nr_vms: the number of allocated areas 3975ca23e405STejun Heo * 3976ca23e405STejun Heo * Free vm_structs and the array allocated by pcpu_get_vm_areas(). 3977ca23e405STejun Heo */ 3978ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) 3979ca23e405STejun Heo { 3980ca23e405STejun Heo int i; 3981ca23e405STejun Heo 3982ca23e405STejun Heo for (i = 0; i < nr_vms; i++) 3983ca23e405STejun Heo free_vm_area(vms[i]); 3984ca23e405STejun Heo kfree(vms); 3985ca23e405STejun Heo } 39864f8b02b4STejun Heo #endif /* CONFIG_SMP */ 3987a10aa579SChristoph Lameter 39885bb1bb35SPaul E. McKenney #ifdef CONFIG_PRINTK 398998f18083SPaul E. McKenney bool vmalloc_dump_obj(void *object) 399098f18083SPaul E. McKenney { 399198f18083SPaul E. McKenney struct vm_struct *vm; 399298f18083SPaul E. McKenney void *objp = (void *)PAGE_ALIGN((unsigned long)object); 399398f18083SPaul E. McKenney 399498f18083SPaul E. McKenney vm = find_vm_area(objp); 399598f18083SPaul E. McKenney if (!vm) 399698f18083SPaul E. McKenney return false; 3997bd34dcd4SPaul E. McKenney pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n", 3998bd34dcd4SPaul E. McKenney vm->nr_pages, (unsigned long)vm->addr, vm->caller); 399998f18083SPaul E. McKenney return true; 400098f18083SPaul E. McKenney } 40015bb1bb35SPaul E. McKenney #endif 400298f18083SPaul E. McKenney 4003a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS 4004a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos) 4005e36176beSUladzislau Rezki (Sony) __acquires(&vmap_purge_lock) 4006d4033afdSJoonsoo Kim __acquires(&vmap_area_lock) 4007a10aa579SChristoph Lameter { 4008e36176beSUladzislau Rezki (Sony) mutex_lock(&vmap_purge_lock); 4009d4033afdSJoonsoo Kim spin_lock(&vmap_area_lock); 4010e36176beSUladzislau Rezki (Sony) 40113f500069Szijun_hu return seq_list_start(&vmap_area_list, *pos); 4012a10aa579SChristoph Lameter } 4013a10aa579SChristoph Lameter 4014a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos) 4015a10aa579SChristoph Lameter { 40163f500069Szijun_hu return seq_list_next(p, &vmap_area_list, pos); 4017a10aa579SChristoph Lameter } 4018a10aa579SChristoph Lameter 4019a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p) 4020d4033afdSJoonsoo Kim __releases(&vmap_area_lock) 40210a7dd4e9SWaiman Long __releases(&vmap_purge_lock) 4022a10aa579SChristoph Lameter { 4023d4033afdSJoonsoo Kim spin_unlock(&vmap_area_lock); 40240a7dd4e9SWaiman Long mutex_unlock(&vmap_purge_lock); 4025a10aa579SChristoph Lameter } 4026a10aa579SChristoph Lameter 4027a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v) 4028a47a126aSEric Dumazet { 4029e5adfffcSKirill A. Shutemov if (IS_ENABLED(CONFIG_NUMA)) { 4030a47a126aSEric Dumazet unsigned int nr, *counters = m->private; 403151e50b3aSEric Dumazet unsigned int step = 1U << vm_area_page_order(v); 4032a47a126aSEric Dumazet 4033a47a126aSEric Dumazet if (!counters) 4034a47a126aSEric Dumazet return; 4035a47a126aSEric Dumazet 4036af12346cSWanpeng Li if (v->flags & VM_UNINITIALIZED) 4037af12346cSWanpeng Li return; 40387e5b528bSDmitry Vyukov /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ 40397e5b528bSDmitry Vyukov smp_rmb(); 4040af12346cSWanpeng Li 4041a47a126aSEric Dumazet memset(counters, 0, nr_node_ids * sizeof(unsigned int)); 4042a47a126aSEric Dumazet 404351e50b3aSEric Dumazet for (nr = 0; nr < v->nr_pages; nr += step) 404451e50b3aSEric Dumazet counters[page_to_nid(v->pages[nr])] += step; 4045a47a126aSEric Dumazet for_each_node_state(nr, N_HIGH_MEMORY) 4046a47a126aSEric Dumazet if (counters[nr]) 4047a47a126aSEric Dumazet seq_printf(m, " N%u=%u", nr, counters[nr]); 4048a47a126aSEric Dumazet } 4049a47a126aSEric Dumazet } 4050a47a126aSEric Dumazet 4051dd3b8353SUladzislau Rezki (Sony) static void show_purge_info(struct seq_file *m) 4052dd3b8353SUladzislau Rezki (Sony) { 4053dd3b8353SUladzislau Rezki (Sony) struct vmap_area *va; 4054dd3b8353SUladzislau Rezki (Sony) 405596e2db45SUladzislau Rezki (Sony) spin_lock(&purge_vmap_area_lock); 405696e2db45SUladzislau Rezki (Sony) list_for_each_entry(va, &purge_vmap_area_list, list) { 4057dd3b8353SUladzislau Rezki (Sony) seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n", 4058dd3b8353SUladzislau Rezki (Sony) (void *)va->va_start, (void *)va->va_end, 4059dd3b8353SUladzislau Rezki (Sony) va->va_end - va->va_start); 4060dd3b8353SUladzislau Rezki (Sony) } 406196e2db45SUladzislau Rezki (Sony) spin_unlock(&purge_vmap_area_lock); 4062dd3b8353SUladzislau Rezki (Sony) } 4063dd3b8353SUladzislau Rezki (Sony) 4064a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p) 4065a10aa579SChristoph Lameter { 40663f500069Szijun_hu struct vmap_area *va; 4067d4033afdSJoonsoo Kim struct vm_struct *v; 4068d4033afdSJoonsoo Kim 40693f500069Szijun_hu va = list_entry(p, struct vmap_area, list); 40703f500069Szijun_hu 4071c2ce8c14SWanpeng Li /* 4072688fcbfcSPengfei Li * s_show can encounter race with remove_vm_area, !vm on behalf 4073688fcbfcSPengfei Li * of vmap area is being tear down or vm_map_ram allocation. 4074c2ce8c14SWanpeng Li */ 4075688fcbfcSPengfei Li if (!va->vm) { 4076dd3b8353SUladzislau Rezki (Sony) seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n", 407778c72746SYisheng Xie (void *)va->va_start, (void *)va->va_end, 4078dd3b8353SUladzislau Rezki (Sony) va->va_end - va->va_start); 407978c72746SYisheng Xie 40807cc7913eSEric Dumazet goto final; 408178c72746SYisheng Xie } 4082d4033afdSJoonsoo Kim 4083d4033afdSJoonsoo Kim v = va->vm; 4084a10aa579SChristoph Lameter 408545ec1690SKees Cook seq_printf(m, "0x%pK-0x%pK %7ld", 4086a10aa579SChristoph Lameter v->addr, v->addr + v->size, v->size); 4087a10aa579SChristoph Lameter 408862c70bceSJoe Perches if (v->caller) 408962c70bceSJoe Perches seq_printf(m, " %pS", v->caller); 409023016969SChristoph Lameter 4091a10aa579SChristoph Lameter if (v->nr_pages) 4092a10aa579SChristoph Lameter seq_printf(m, " pages=%d", v->nr_pages); 4093a10aa579SChristoph Lameter 4094a10aa579SChristoph Lameter if (v->phys_addr) 4095199eaa05SMiles Chen seq_printf(m, " phys=%pa", &v->phys_addr); 4096a10aa579SChristoph Lameter 4097a10aa579SChristoph Lameter if (v->flags & VM_IOREMAP) 4098f4527c90SFabian Frederick seq_puts(m, " ioremap"); 4099a10aa579SChristoph Lameter 4100a10aa579SChristoph Lameter if (v->flags & VM_ALLOC) 4101f4527c90SFabian Frederick seq_puts(m, " vmalloc"); 4102a10aa579SChristoph Lameter 4103a10aa579SChristoph Lameter if (v->flags & VM_MAP) 4104f4527c90SFabian Frederick seq_puts(m, " vmap"); 4105a10aa579SChristoph Lameter 4106a10aa579SChristoph Lameter if (v->flags & VM_USERMAP) 4107f4527c90SFabian Frederick seq_puts(m, " user"); 4108a10aa579SChristoph Lameter 4109fe9041c2SChristoph Hellwig if (v->flags & VM_DMA_COHERENT) 4110fe9041c2SChristoph Hellwig seq_puts(m, " dma-coherent"); 4111fe9041c2SChristoph Hellwig 4112244d63eeSDavid Rientjes if (is_vmalloc_addr(v->pages)) 4113f4527c90SFabian Frederick seq_puts(m, " vpages"); 4114a10aa579SChristoph Lameter 4115a47a126aSEric Dumazet show_numa_info(m, v); 4116a10aa579SChristoph Lameter seq_putc(m, '\n'); 4117dd3b8353SUladzislau Rezki (Sony) 4118dd3b8353SUladzislau Rezki (Sony) /* 411996e2db45SUladzislau Rezki (Sony) * As a final step, dump "unpurged" areas. 4120dd3b8353SUladzislau Rezki (Sony) */ 41217cc7913eSEric Dumazet final: 4122dd3b8353SUladzislau Rezki (Sony) if (list_is_last(&va->list, &vmap_area_list)) 4123dd3b8353SUladzislau Rezki (Sony) show_purge_info(m); 4124dd3b8353SUladzislau Rezki (Sony) 4125a10aa579SChristoph Lameter return 0; 4126a10aa579SChristoph Lameter } 4127a10aa579SChristoph Lameter 41285f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = { 4129a10aa579SChristoph Lameter .start = s_start, 4130a10aa579SChristoph Lameter .next = s_next, 4131a10aa579SChristoph Lameter .stop = s_stop, 4132a10aa579SChristoph Lameter .show = s_show, 4133a10aa579SChristoph Lameter }; 41345f6a6a9cSAlexey Dobriyan 41355f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void) 41365f6a6a9cSAlexey Dobriyan { 4137fddda2b7SChristoph Hellwig if (IS_ENABLED(CONFIG_NUMA)) 41380825a6f9SJoe Perches proc_create_seq_private("vmallocinfo", 0400, NULL, 413944414d82SChristoph Hellwig &vmalloc_op, 414044414d82SChristoph Hellwig nr_node_ids * sizeof(unsigned int), NULL); 4141fddda2b7SChristoph Hellwig else 41420825a6f9SJoe Perches proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op); 41435f6a6a9cSAlexey Dobriyan return 0; 41445f6a6a9cSAlexey Dobriyan } 41455f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init); 4146db3808c1SJoonsoo Kim 4147a10aa579SChristoph Lameter #endif 4148