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 46*cf243da6SUladzislau Rezki (Sony) #define CREATE_TRACE_POINTS 47*cf243da6SUladzislau Rezki (Sony) #include <trace/events/vmalloc.h> 48*cf243da6SUladzislau Rezki (Sony) 49dd56b046SMel Gorman #include "internal.h" 502a681cfaSJoerg Roedel #include "pgalloc-track.h" 51dd56b046SMel Gorman 5282a70ce0SChristoph Hellwig #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP 5382a70ce0SChristoph Hellwig static unsigned int __ro_after_init ioremap_max_page_shift = BITS_PER_LONG - 1; 5482a70ce0SChristoph Hellwig 5582a70ce0SChristoph Hellwig static int __init set_nohugeiomap(char *str) 5682a70ce0SChristoph Hellwig { 5782a70ce0SChristoph Hellwig ioremap_max_page_shift = PAGE_SHIFT; 5882a70ce0SChristoph Hellwig return 0; 5982a70ce0SChristoph Hellwig } 6082a70ce0SChristoph Hellwig early_param("nohugeiomap", set_nohugeiomap); 6182a70ce0SChristoph Hellwig #else /* CONFIG_HAVE_ARCH_HUGE_VMAP */ 6282a70ce0SChristoph Hellwig static const unsigned int ioremap_max_page_shift = PAGE_SHIFT; 6382a70ce0SChristoph Hellwig #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ 6482a70ce0SChristoph Hellwig 65121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 66121e6f32SNicholas Piggin static bool __ro_after_init vmap_allow_huge = true; 67121e6f32SNicholas Piggin 68121e6f32SNicholas Piggin static int __init set_nohugevmalloc(char *str) 69121e6f32SNicholas Piggin { 70121e6f32SNicholas Piggin vmap_allow_huge = false; 71121e6f32SNicholas Piggin return 0; 72121e6f32SNicholas Piggin } 73121e6f32SNicholas Piggin early_param("nohugevmalloc", set_nohugevmalloc); 74121e6f32SNicholas Piggin #else /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */ 75121e6f32SNicholas Piggin static const bool vmap_allow_huge = false; 76121e6f32SNicholas Piggin #endif /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */ 77121e6f32SNicholas Piggin 78186525bdSIngo Molnar bool is_vmalloc_addr(const void *x) 79186525bdSIngo Molnar { 804aff1dc4SAndrey Konovalov unsigned long addr = (unsigned long)kasan_reset_tag(x); 81186525bdSIngo Molnar 82186525bdSIngo Molnar return addr >= VMALLOC_START && addr < VMALLOC_END; 83186525bdSIngo Molnar } 84186525bdSIngo Molnar EXPORT_SYMBOL(is_vmalloc_addr); 85186525bdSIngo Molnar 8632fcfd40SAl Viro struct vfree_deferred { 8732fcfd40SAl Viro struct llist_head list; 8832fcfd40SAl Viro struct work_struct wq; 8932fcfd40SAl Viro }; 9032fcfd40SAl Viro static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred); 9132fcfd40SAl Viro 9232fcfd40SAl Viro static void __vunmap(const void *, int); 9332fcfd40SAl Viro 9432fcfd40SAl Viro static void free_work(struct work_struct *w) 9532fcfd40SAl Viro { 9632fcfd40SAl Viro struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq); 97894e58c1SByungchul Park struct llist_node *t, *llnode; 98894e58c1SByungchul Park 99894e58c1SByungchul Park llist_for_each_safe(llnode, t, llist_del_all(&p->list)) 100894e58c1SByungchul Park __vunmap((void *)llnode, 1); 10132fcfd40SAl Viro } 10232fcfd40SAl Viro 103db64fe02SNick Piggin /*** Page table manipulation functions ***/ 1045e9e3d77SNicholas Piggin static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 1055e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 106f7ee1f13SChristophe Leroy unsigned int max_page_shift, pgtbl_mod_mask *mask) 1075e9e3d77SNicholas Piggin { 1085e9e3d77SNicholas Piggin pte_t *pte; 1095e9e3d77SNicholas Piggin u64 pfn; 110f7ee1f13SChristophe Leroy unsigned long size = PAGE_SIZE; 1115e9e3d77SNicholas Piggin 1125e9e3d77SNicholas Piggin pfn = phys_addr >> PAGE_SHIFT; 1135e9e3d77SNicholas Piggin pte = pte_alloc_kernel_track(pmd, addr, mask); 1145e9e3d77SNicholas Piggin if (!pte) 1155e9e3d77SNicholas Piggin return -ENOMEM; 1165e9e3d77SNicholas Piggin do { 1175e9e3d77SNicholas Piggin BUG_ON(!pte_none(*pte)); 118f7ee1f13SChristophe Leroy 119f7ee1f13SChristophe Leroy #ifdef CONFIG_HUGETLB_PAGE 120f7ee1f13SChristophe Leroy size = arch_vmap_pte_range_map_size(addr, end, pfn, max_page_shift); 121f7ee1f13SChristophe Leroy if (size != PAGE_SIZE) { 122f7ee1f13SChristophe Leroy pte_t entry = pfn_pte(pfn, prot); 123f7ee1f13SChristophe Leroy 124f7ee1f13SChristophe Leroy entry = arch_make_huge_pte(entry, ilog2(size), 0); 125f7ee1f13SChristophe Leroy set_huge_pte_at(&init_mm, addr, pte, entry); 126f7ee1f13SChristophe Leroy pfn += PFN_DOWN(size); 127f7ee1f13SChristophe Leroy continue; 128f7ee1f13SChristophe Leroy } 129f7ee1f13SChristophe Leroy #endif 1305e9e3d77SNicholas Piggin set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot)); 1315e9e3d77SNicholas Piggin pfn++; 132f7ee1f13SChristophe Leroy } while (pte += PFN_DOWN(size), addr += size, addr != end); 1335e9e3d77SNicholas Piggin *mask |= PGTBL_PTE_MODIFIED; 1345e9e3d77SNicholas Piggin return 0; 1355e9e3d77SNicholas Piggin } 1365e9e3d77SNicholas Piggin 1375e9e3d77SNicholas Piggin static int vmap_try_huge_pmd(pmd_t *pmd, unsigned long addr, unsigned long end, 1385e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 1395e9e3d77SNicholas Piggin unsigned int max_page_shift) 1405e9e3d77SNicholas Piggin { 1415e9e3d77SNicholas Piggin if (max_page_shift < PMD_SHIFT) 1425e9e3d77SNicholas Piggin return 0; 1435e9e3d77SNicholas Piggin 1445e9e3d77SNicholas Piggin if (!arch_vmap_pmd_supported(prot)) 1455e9e3d77SNicholas Piggin return 0; 1465e9e3d77SNicholas Piggin 1475e9e3d77SNicholas Piggin if ((end - addr) != PMD_SIZE) 1485e9e3d77SNicholas Piggin return 0; 1495e9e3d77SNicholas Piggin 1505e9e3d77SNicholas Piggin if (!IS_ALIGNED(addr, PMD_SIZE)) 1515e9e3d77SNicholas Piggin return 0; 1525e9e3d77SNicholas Piggin 1535e9e3d77SNicholas Piggin if (!IS_ALIGNED(phys_addr, PMD_SIZE)) 1545e9e3d77SNicholas Piggin return 0; 1555e9e3d77SNicholas Piggin 1565e9e3d77SNicholas Piggin if (pmd_present(*pmd) && !pmd_free_pte_page(pmd, addr)) 1575e9e3d77SNicholas Piggin return 0; 1585e9e3d77SNicholas Piggin 1595e9e3d77SNicholas Piggin return pmd_set_huge(pmd, phys_addr, prot); 1605e9e3d77SNicholas Piggin } 1615e9e3d77SNicholas Piggin 1625e9e3d77SNicholas Piggin static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end, 1635e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 1645e9e3d77SNicholas Piggin unsigned int max_page_shift, pgtbl_mod_mask *mask) 1655e9e3d77SNicholas Piggin { 1665e9e3d77SNicholas Piggin pmd_t *pmd; 1675e9e3d77SNicholas Piggin unsigned long next; 1685e9e3d77SNicholas Piggin 1695e9e3d77SNicholas Piggin pmd = pmd_alloc_track(&init_mm, pud, addr, mask); 1705e9e3d77SNicholas Piggin if (!pmd) 1715e9e3d77SNicholas Piggin return -ENOMEM; 1725e9e3d77SNicholas Piggin do { 1735e9e3d77SNicholas Piggin next = pmd_addr_end(addr, end); 1745e9e3d77SNicholas Piggin 1755e9e3d77SNicholas Piggin if (vmap_try_huge_pmd(pmd, addr, next, phys_addr, prot, 1765e9e3d77SNicholas Piggin max_page_shift)) { 1775e9e3d77SNicholas Piggin *mask |= PGTBL_PMD_MODIFIED; 1785e9e3d77SNicholas Piggin continue; 1795e9e3d77SNicholas Piggin } 1805e9e3d77SNicholas Piggin 181f7ee1f13SChristophe Leroy if (vmap_pte_range(pmd, addr, next, phys_addr, prot, max_page_shift, mask)) 1825e9e3d77SNicholas Piggin return -ENOMEM; 1835e9e3d77SNicholas Piggin } while (pmd++, phys_addr += (next - addr), addr = next, addr != end); 1845e9e3d77SNicholas Piggin return 0; 1855e9e3d77SNicholas Piggin } 1865e9e3d77SNicholas Piggin 1875e9e3d77SNicholas Piggin static int vmap_try_huge_pud(pud_t *pud, unsigned long addr, unsigned long end, 1885e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 1895e9e3d77SNicholas Piggin unsigned int max_page_shift) 1905e9e3d77SNicholas Piggin { 1915e9e3d77SNicholas Piggin if (max_page_shift < PUD_SHIFT) 1925e9e3d77SNicholas Piggin return 0; 1935e9e3d77SNicholas Piggin 1945e9e3d77SNicholas Piggin if (!arch_vmap_pud_supported(prot)) 1955e9e3d77SNicholas Piggin return 0; 1965e9e3d77SNicholas Piggin 1975e9e3d77SNicholas Piggin if ((end - addr) != PUD_SIZE) 1985e9e3d77SNicholas Piggin return 0; 1995e9e3d77SNicholas Piggin 2005e9e3d77SNicholas Piggin if (!IS_ALIGNED(addr, PUD_SIZE)) 2015e9e3d77SNicholas Piggin return 0; 2025e9e3d77SNicholas Piggin 2035e9e3d77SNicholas Piggin if (!IS_ALIGNED(phys_addr, PUD_SIZE)) 2045e9e3d77SNicholas Piggin return 0; 2055e9e3d77SNicholas Piggin 2065e9e3d77SNicholas Piggin if (pud_present(*pud) && !pud_free_pmd_page(pud, addr)) 2075e9e3d77SNicholas Piggin return 0; 2085e9e3d77SNicholas Piggin 2095e9e3d77SNicholas Piggin return pud_set_huge(pud, phys_addr, prot); 2105e9e3d77SNicholas Piggin } 2115e9e3d77SNicholas Piggin 2125e9e3d77SNicholas Piggin static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, 2135e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 2145e9e3d77SNicholas Piggin unsigned int max_page_shift, pgtbl_mod_mask *mask) 2155e9e3d77SNicholas Piggin { 2165e9e3d77SNicholas Piggin pud_t *pud; 2175e9e3d77SNicholas Piggin unsigned long next; 2185e9e3d77SNicholas Piggin 2195e9e3d77SNicholas Piggin pud = pud_alloc_track(&init_mm, p4d, addr, mask); 2205e9e3d77SNicholas Piggin if (!pud) 2215e9e3d77SNicholas Piggin return -ENOMEM; 2225e9e3d77SNicholas Piggin do { 2235e9e3d77SNicholas Piggin next = pud_addr_end(addr, end); 2245e9e3d77SNicholas Piggin 2255e9e3d77SNicholas Piggin if (vmap_try_huge_pud(pud, addr, next, phys_addr, prot, 2265e9e3d77SNicholas Piggin max_page_shift)) { 2275e9e3d77SNicholas Piggin *mask |= PGTBL_PUD_MODIFIED; 2285e9e3d77SNicholas Piggin continue; 2295e9e3d77SNicholas Piggin } 2305e9e3d77SNicholas Piggin 2315e9e3d77SNicholas Piggin if (vmap_pmd_range(pud, addr, next, phys_addr, prot, 2325e9e3d77SNicholas Piggin max_page_shift, mask)) 2335e9e3d77SNicholas Piggin return -ENOMEM; 2345e9e3d77SNicholas Piggin } while (pud++, phys_addr += (next - addr), addr = next, addr != end); 2355e9e3d77SNicholas Piggin return 0; 2365e9e3d77SNicholas Piggin } 2375e9e3d77SNicholas Piggin 2385e9e3d77SNicholas Piggin static int vmap_try_huge_p4d(p4d_t *p4d, unsigned long addr, unsigned long end, 2395e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 2405e9e3d77SNicholas Piggin unsigned int max_page_shift) 2415e9e3d77SNicholas Piggin { 2425e9e3d77SNicholas Piggin if (max_page_shift < P4D_SHIFT) 2435e9e3d77SNicholas Piggin return 0; 2445e9e3d77SNicholas Piggin 2455e9e3d77SNicholas Piggin if (!arch_vmap_p4d_supported(prot)) 2465e9e3d77SNicholas Piggin return 0; 2475e9e3d77SNicholas Piggin 2485e9e3d77SNicholas Piggin if ((end - addr) != P4D_SIZE) 2495e9e3d77SNicholas Piggin return 0; 2505e9e3d77SNicholas Piggin 2515e9e3d77SNicholas Piggin if (!IS_ALIGNED(addr, P4D_SIZE)) 2525e9e3d77SNicholas Piggin return 0; 2535e9e3d77SNicholas Piggin 2545e9e3d77SNicholas Piggin if (!IS_ALIGNED(phys_addr, P4D_SIZE)) 2555e9e3d77SNicholas Piggin return 0; 2565e9e3d77SNicholas Piggin 2575e9e3d77SNicholas Piggin if (p4d_present(*p4d) && !p4d_free_pud_page(p4d, addr)) 2585e9e3d77SNicholas Piggin return 0; 2595e9e3d77SNicholas Piggin 2605e9e3d77SNicholas Piggin return p4d_set_huge(p4d, phys_addr, prot); 2615e9e3d77SNicholas Piggin } 2625e9e3d77SNicholas Piggin 2635e9e3d77SNicholas Piggin static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end, 2645e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 2655e9e3d77SNicholas Piggin unsigned int max_page_shift, pgtbl_mod_mask *mask) 2665e9e3d77SNicholas Piggin { 2675e9e3d77SNicholas Piggin p4d_t *p4d; 2685e9e3d77SNicholas Piggin unsigned long next; 2695e9e3d77SNicholas Piggin 2705e9e3d77SNicholas Piggin p4d = p4d_alloc_track(&init_mm, pgd, addr, mask); 2715e9e3d77SNicholas Piggin if (!p4d) 2725e9e3d77SNicholas Piggin return -ENOMEM; 2735e9e3d77SNicholas Piggin do { 2745e9e3d77SNicholas Piggin next = p4d_addr_end(addr, end); 2755e9e3d77SNicholas Piggin 2765e9e3d77SNicholas Piggin if (vmap_try_huge_p4d(p4d, addr, next, phys_addr, prot, 2775e9e3d77SNicholas Piggin max_page_shift)) { 2785e9e3d77SNicholas Piggin *mask |= PGTBL_P4D_MODIFIED; 2795e9e3d77SNicholas Piggin continue; 2805e9e3d77SNicholas Piggin } 2815e9e3d77SNicholas Piggin 2825e9e3d77SNicholas Piggin if (vmap_pud_range(p4d, addr, next, phys_addr, prot, 2835e9e3d77SNicholas Piggin max_page_shift, mask)) 2845e9e3d77SNicholas Piggin return -ENOMEM; 2855e9e3d77SNicholas Piggin } while (p4d++, phys_addr += (next - addr), addr = next, addr != end); 2865e9e3d77SNicholas Piggin return 0; 2875e9e3d77SNicholas Piggin } 2885e9e3d77SNicholas Piggin 2895d87510dSNicholas Piggin static int vmap_range_noflush(unsigned long addr, unsigned long end, 2905e9e3d77SNicholas Piggin phys_addr_t phys_addr, pgprot_t prot, 2915e9e3d77SNicholas Piggin unsigned int max_page_shift) 2925e9e3d77SNicholas Piggin { 2935e9e3d77SNicholas Piggin pgd_t *pgd; 2945e9e3d77SNicholas Piggin unsigned long start; 2955e9e3d77SNicholas Piggin unsigned long next; 2965e9e3d77SNicholas Piggin int err; 2975e9e3d77SNicholas Piggin pgtbl_mod_mask mask = 0; 2985e9e3d77SNicholas Piggin 2995e9e3d77SNicholas Piggin might_sleep(); 3005e9e3d77SNicholas Piggin BUG_ON(addr >= end); 3015e9e3d77SNicholas Piggin 3025e9e3d77SNicholas Piggin start = addr; 3035e9e3d77SNicholas Piggin pgd = pgd_offset_k(addr); 3045e9e3d77SNicholas Piggin do { 3055e9e3d77SNicholas Piggin next = pgd_addr_end(addr, end); 3065e9e3d77SNicholas Piggin err = vmap_p4d_range(pgd, addr, next, phys_addr, prot, 3075e9e3d77SNicholas Piggin max_page_shift, &mask); 3085e9e3d77SNicholas Piggin if (err) 3095e9e3d77SNicholas Piggin break; 3105e9e3d77SNicholas Piggin } while (pgd++, phys_addr += (next - addr), addr = next, addr != end); 3115e9e3d77SNicholas Piggin 3125e9e3d77SNicholas Piggin if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 3135e9e3d77SNicholas Piggin arch_sync_kernel_mappings(start, end); 3145e9e3d77SNicholas Piggin 3155e9e3d77SNicholas Piggin return err; 3165e9e3d77SNicholas Piggin } 317b221385bSAdrian Bunk 31882a70ce0SChristoph Hellwig int ioremap_page_range(unsigned long addr, unsigned long end, 31982a70ce0SChristoph Hellwig phys_addr_t phys_addr, pgprot_t prot) 3205d87510dSNicholas Piggin { 3215d87510dSNicholas Piggin int err; 3225d87510dSNicholas Piggin 3238491502fSChristoph Hellwig err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot), 32482a70ce0SChristoph Hellwig ioremap_max_page_shift); 3255d87510dSNicholas Piggin flush_cache_vmap(addr, end); 326b073d7f8SAlexander Potapenko if (!err) 327b073d7f8SAlexander Potapenko kmsan_ioremap_page_range(addr, end, phys_addr, prot, 328b073d7f8SAlexander Potapenko ioremap_max_page_shift); 3295d87510dSNicholas Piggin return err; 3305d87510dSNicholas Piggin } 3315d87510dSNicholas Piggin 3322ba3e694SJoerg Roedel static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, 3332ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 3341da177e4SLinus Torvalds { 3351da177e4SLinus Torvalds pte_t *pte; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds pte = pte_offset_kernel(pmd, addr); 3381da177e4SLinus Torvalds do { 3391da177e4SLinus Torvalds pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte); 3401da177e4SLinus Torvalds WARN_ON(!pte_none(ptent) && !pte_present(ptent)); 3411da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 3422ba3e694SJoerg Roedel *mask |= PGTBL_PTE_MODIFIED; 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3452ba3e694SJoerg Roedel static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end, 3462ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 3471da177e4SLinus Torvalds { 3481da177e4SLinus Torvalds pmd_t *pmd; 3491da177e4SLinus Torvalds unsigned long next; 3502ba3e694SJoerg Roedel int cleared; 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 3531da177e4SLinus Torvalds do { 3541da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 3552ba3e694SJoerg Roedel 3562ba3e694SJoerg Roedel cleared = pmd_clear_huge(pmd); 3572ba3e694SJoerg Roedel if (cleared || pmd_bad(*pmd)) 3582ba3e694SJoerg Roedel *mask |= PGTBL_PMD_MODIFIED; 3592ba3e694SJoerg Roedel 3602ba3e694SJoerg Roedel if (cleared) 361b9820d8fSToshi Kani continue; 3621da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 3631da177e4SLinus Torvalds continue; 3642ba3e694SJoerg Roedel vunmap_pte_range(pmd, addr, next, mask); 365e47110e9SAneesh Kumar K.V 366e47110e9SAneesh Kumar K.V cond_resched(); 3671da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3702ba3e694SJoerg Roedel static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, 3712ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 3721da177e4SLinus Torvalds { 3731da177e4SLinus Torvalds pud_t *pud; 3741da177e4SLinus Torvalds unsigned long next; 3752ba3e694SJoerg Roedel int cleared; 3761da177e4SLinus Torvalds 377c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 3781da177e4SLinus Torvalds do { 3791da177e4SLinus Torvalds next = pud_addr_end(addr, end); 3802ba3e694SJoerg Roedel 3812ba3e694SJoerg Roedel cleared = pud_clear_huge(pud); 3822ba3e694SJoerg Roedel if (cleared || pud_bad(*pud)) 3832ba3e694SJoerg Roedel *mask |= PGTBL_PUD_MODIFIED; 3842ba3e694SJoerg Roedel 3852ba3e694SJoerg Roedel if (cleared) 386b9820d8fSToshi Kani continue; 3871da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 3881da177e4SLinus Torvalds continue; 3892ba3e694SJoerg Roedel vunmap_pmd_range(pud, addr, next, mask); 3901da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3932ba3e694SJoerg Roedel static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end, 3942ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 395c2febafcSKirill A. Shutemov { 396c2febafcSKirill A. Shutemov p4d_t *p4d; 397c2febafcSKirill A. Shutemov unsigned long next; 398c2febafcSKirill A. Shutemov 399c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 400c2febafcSKirill A. Shutemov do { 401c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 4022ba3e694SJoerg Roedel 403c8db8c26SLi kunyu p4d_clear_huge(p4d); 404c8db8c26SLi kunyu if (p4d_bad(*p4d)) 4052ba3e694SJoerg Roedel *mask |= PGTBL_P4D_MODIFIED; 4062ba3e694SJoerg Roedel 407c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 408c2febafcSKirill A. Shutemov continue; 4092ba3e694SJoerg Roedel vunmap_pud_range(p4d, addr, next, mask); 410c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 411c2febafcSKirill A. Shutemov } 412c2febafcSKirill A. Shutemov 4134ad0ae8cSNicholas Piggin /* 4144ad0ae8cSNicholas Piggin * vunmap_range_noflush is similar to vunmap_range, but does not 4154ad0ae8cSNicholas Piggin * flush caches or TLBs. 416b521c43fSChristoph Hellwig * 4174ad0ae8cSNicholas Piggin * The caller is responsible for calling flush_cache_vmap() before calling 4184ad0ae8cSNicholas Piggin * this function, and flush_tlb_kernel_range after it has returned 4194ad0ae8cSNicholas Piggin * successfully (and before the addresses are expected to cause a page fault 4204ad0ae8cSNicholas Piggin * or be re-mapped for something else, if TLB flushes are being delayed or 4214ad0ae8cSNicholas Piggin * coalesced). 422b521c43fSChristoph Hellwig * 4234ad0ae8cSNicholas Piggin * This is an internal function only. Do not use outside mm/. 424b521c43fSChristoph Hellwig */ 425b073d7f8SAlexander Potapenko void __vunmap_range_noflush(unsigned long start, unsigned long end) 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds unsigned long next; 428b521c43fSChristoph Hellwig pgd_t *pgd; 4292ba3e694SJoerg Roedel unsigned long addr = start; 4302ba3e694SJoerg Roedel pgtbl_mod_mask mask = 0; 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds BUG_ON(addr >= end); 4331da177e4SLinus Torvalds pgd = pgd_offset_k(addr); 4341da177e4SLinus Torvalds do { 4351da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 4362ba3e694SJoerg Roedel if (pgd_bad(*pgd)) 4372ba3e694SJoerg Roedel mask |= PGTBL_PGD_MODIFIED; 4381da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 4391da177e4SLinus Torvalds continue; 4402ba3e694SJoerg Roedel vunmap_p4d_range(pgd, addr, next, &mask); 4411da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 4422ba3e694SJoerg Roedel 4432ba3e694SJoerg Roedel if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 4442ba3e694SJoerg Roedel arch_sync_kernel_mappings(start, end); 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 447b073d7f8SAlexander Potapenko void vunmap_range_noflush(unsigned long start, unsigned long end) 448b073d7f8SAlexander Potapenko { 449b073d7f8SAlexander Potapenko kmsan_vunmap_range_noflush(start, end); 450b073d7f8SAlexander Potapenko __vunmap_range_noflush(start, end); 451b073d7f8SAlexander Potapenko } 452b073d7f8SAlexander Potapenko 4534ad0ae8cSNicholas Piggin /** 4544ad0ae8cSNicholas Piggin * vunmap_range - unmap kernel virtual addresses 4554ad0ae8cSNicholas Piggin * @addr: start of the VM area to unmap 4564ad0ae8cSNicholas Piggin * @end: end of the VM area to unmap (non-inclusive) 4574ad0ae8cSNicholas Piggin * 4584ad0ae8cSNicholas Piggin * Clears any present PTEs in the virtual address range, flushes TLBs and 4594ad0ae8cSNicholas Piggin * caches. Any subsequent access to the address before it has been re-mapped 4604ad0ae8cSNicholas Piggin * is a kernel bug. 4614ad0ae8cSNicholas Piggin */ 4624ad0ae8cSNicholas Piggin void vunmap_range(unsigned long addr, unsigned long end) 4634ad0ae8cSNicholas Piggin { 4644ad0ae8cSNicholas Piggin flush_cache_vunmap(addr, end); 4654ad0ae8cSNicholas Piggin vunmap_range_noflush(addr, end); 4664ad0ae8cSNicholas Piggin flush_tlb_kernel_range(addr, end); 4674ad0ae8cSNicholas Piggin } 4684ad0ae8cSNicholas Piggin 4690a264884SNicholas Piggin static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr, 4702ba3e694SJoerg Roedel unsigned long end, pgprot_t prot, struct page **pages, int *nr, 4712ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 4721da177e4SLinus Torvalds { 4731da177e4SLinus Torvalds pte_t *pte; 4741da177e4SLinus Torvalds 475db64fe02SNick Piggin /* 476db64fe02SNick Piggin * nr is a running index into the array which helps higher level 477db64fe02SNick Piggin * callers keep track of where we're up to. 478db64fe02SNick Piggin */ 479db64fe02SNick Piggin 4802ba3e694SJoerg Roedel pte = pte_alloc_kernel_track(pmd, addr, mask); 4811da177e4SLinus Torvalds if (!pte) 4821da177e4SLinus Torvalds return -ENOMEM; 4831da177e4SLinus Torvalds do { 484db64fe02SNick Piggin struct page *page = pages[*nr]; 485db64fe02SNick Piggin 486db64fe02SNick Piggin if (WARN_ON(!pte_none(*pte))) 487db64fe02SNick Piggin return -EBUSY; 488db64fe02SNick Piggin if (WARN_ON(!page)) 4891da177e4SLinus Torvalds return -ENOMEM; 4904fcdcc12SYury Norov if (WARN_ON(!pfn_valid(page_to_pfn(page)))) 4914fcdcc12SYury Norov return -EINVAL; 4924fcdcc12SYury Norov 4931da177e4SLinus Torvalds set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); 494db64fe02SNick Piggin (*nr)++; 4951da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 4962ba3e694SJoerg Roedel *mask |= PGTBL_PTE_MODIFIED; 4971da177e4SLinus Torvalds return 0; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 5000a264884SNicholas Piggin static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr, 5012ba3e694SJoerg Roedel unsigned long end, pgprot_t prot, struct page **pages, int *nr, 5022ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 5031da177e4SLinus Torvalds { 5041da177e4SLinus Torvalds pmd_t *pmd; 5051da177e4SLinus Torvalds unsigned long next; 5061da177e4SLinus Torvalds 5072ba3e694SJoerg Roedel pmd = pmd_alloc_track(&init_mm, pud, addr, mask); 5081da177e4SLinus Torvalds if (!pmd) 5091da177e4SLinus Torvalds return -ENOMEM; 5101da177e4SLinus Torvalds do { 5111da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 5120a264884SNicholas Piggin if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask)) 5131da177e4SLinus Torvalds return -ENOMEM; 5141da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 5151da177e4SLinus Torvalds return 0; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 5180a264884SNicholas Piggin static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr, 5192ba3e694SJoerg Roedel unsigned long end, pgprot_t prot, struct page **pages, int *nr, 5202ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 5211da177e4SLinus Torvalds { 5221da177e4SLinus Torvalds pud_t *pud; 5231da177e4SLinus Torvalds unsigned long next; 5241da177e4SLinus Torvalds 5252ba3e694SJoerg Roedel pud = pud_alloc_track(&init_mm, p4d, addr, mask); 5261da177e4SLinus Torvalds if (!pud) 5271da177e4SLinus Torvalds return -ENOMEM; 5281da177e4SLinus Torvalds do { 5291da177e4SLinus Torvalds next = pud_addr_end(addr, end); 5300a264884SNicholas Piggin if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask)) 5311da177e4SLinus Torvalds return -ENOMEM; 5321da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 5331da177e4SLinus Torvalds return 0; 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds 5360a264884SNicholas Piggin static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr, 5372ba3e694SJoerg Roedel unsigned long end, pgprot_t prot, struct page **pages, int *nr, 5382ba3e694SJoerg Roedel pgtbl_mod_mask *mask) 539c2febafcSKirill A. Shutemov { 540c2febafcSKirill A. Shutemov p4d_t *p4d; 541c2febafcSKirill A. Shutemov unsigned long next; 542c2febafcSKirill A. Shutemov 5432ba3e694SJoerg Roedel p4d = p4d_alloc_track(&init_mm, pgd, addr, mask); 544c2febafcSKirill A. Shutemov if (!p4d) 545c2febafcSKirill A. Shutemov return -ENOMEM; 546c2febafcSKirill A. Shutemov do { 547c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 5480a264884SNicholas Piggin if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask)) 549c2febafcSKirill A. Shutemov return -ENOMEM; 550c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 551c2febafcSKirill A. Shutemov return 0; 552c2febafcSKirill A. Shutemov } 553c2febafcSKirill A. Shutemov 554121e6f32SNicholas Piggin static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, 555121e6f32SNicholas Piggin pgprot_t prot, struct page **pages) 556121e6f32SNicholas Piggin { 557121e6f32SNicholas Piggin unsigned long start = addr; 558121e6f32SNicholas Piggin pgd_t *pgd; 559121e6f32SNicholas Piggin unsigned long next; 560121e6f32SNicholas Piggin int err = 0; 561121e6f32SNicholas Piggin int nr = 0; 562121e6f32SNicholas Piggin pgtbl_mod_mask mask = 0; 563121e6f32SNicholas Piggin 564121e6f32SNicholas Piggin BUG_ON(addr >= end); 565121e6f32SNicholas Piggin pgd = pgd_offset_k(addr); 566121e6f32SNicholas Piggin do { 567121e6f32SNicholas Piggin next = pgd_addr_end(addr, end); 568121e6f32SNicholas Piggin if (pgd_bad(*pgd)) 569121e6f32SNicholas Piggin mask |= PGTBL_PGD_MODIFIED; 570121e6f32SNicholas Piggin err = vmap_pages_p4d_range(pgd, addr, next, prot, pages, &nr, &mask); 571121e6f32SNicholas Piggin if (err) 572121e6f32SNicholas Piggin return err; 573121e6f32SNicholas Piggin } while (pgd++, addr = next, addr != end); 574121e6f32SNicholas Piggin 575121e6f32SNicholas Piggin if (mask & ARCH_PAGE_TABLE_SYNC_MASK) 576121e6f32SNicholas Piggin arch_sync_kernel_mappings(start, end); 577121e6f32SNicholas Piggin 578121e6f32SNicholas Piggin return 0; 579121e6f32SNicholas Piggin } 580121e6f32SNicholas Piggin 581b67177ecSNicholas Piggin /* 582b67177ecSNicholas Piggin * vmap_pages_range_noflush is similar to vmap_pages_range, but does not 583b67177ecSNicholas Piggin * flush caches. 584b67177ecSNicholas Piggin * 585b67177ecSNicholas Piggin * The caller is responsible for calling flush_cache_vmap() after this 586b67177ecSNicholas Piggin * function returns successfully and before the addresses are accessed. 587b67177ecSNicholas Piggin * 588b67177ecSNicholas Piggin * This is an internal function only. Do not use outside mm/. 589b67177ecSNicholas Piggin */ 590b073d7f8SAlexander Potapenko int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, 591121e6f32SNicholas Piggin pgprot_t prot, struct page **pages, unsigned int page_shift) 592121e6f32SNicholas Piggin { 593121e6f32SNicholas Piggin unsigned int i, nr = (end - addr) >> PAGE_SHIFT; 594121e6f32SNicholas Piggin 595121e6f32SNicholas Piggin WARN_ON(page_shift < PAGE_SHIFT); 596121e6f32SNicholas Piggin 597121e6f32SNicholas Piggin if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC) || 598121e6f32SNicholas Piggin page_shift == PAGE_SHIFT) 599121e6f32SNicholas Piggin return vmap_small_pages_range_noflush(addr, end, prot, pages); 600121e6f32SNicholas Piggin 601121e6f32SNicholas Piggin for (i = 0; i < nr; i += 1U << (page_shift - PAGE_SHIFT)) { 602121e6f32SNicholas Piggin int err; 603121e6f32SNicholas Piggin 604121e6f32SNicholas Piggin err = vmap_range_noflush(addr, addr + (1UL << page_shift), 60508262ac5SMatthew Wilcox page_to_phys(pages[i]), prot, 606121e6f32SNicholas Piggin page_shift); 607121e6f32SNicholas Piggin if (err) 608121e6f32SNicholas Piggin return err; 609121e6f32SNicholas Piggin 610121e6f32SNicholas Piggin addr += 1UL << page_shift; 611121e6f32SNicholas Piggin } 612121e6f32SNicholas Piggin 613121e6f32SNicholas Piggin return 0; 614121e6f32SNicholas Piggin } 615121e6f32SNicholas Piggin 616b073d7f8SAlexander Potapenko int vmap_pages_range_noflush(unsigned long addr, unsigned long end, 617b073d7f8SAlexander Potapenko pgprot_t prot, struct page **pages, unsigned int page_shift) 618b073d7f8SAlexander Potapenko { 619b073d7f8SAlexander Potapenko kmsan_vmap_pages_range_noflush(addr, end, prot, pages, page_shift); 620b073d7f8SAlexander Potapenko return __vmap_pages_range_noflush(addr, end, prot, pages, page_shift); 621b073d7f8SAlexander Potapenko } 622b073d7f8SAlexander Potapenko 623b67177ecSNicholas Piggin /** 624b67177ecSNicholas Piggin * vmap_pages_range - map pages to a kernel virtual address 625b67177ecSNicholas Piggin * @addr: start of the VM area to map 626b67177ecSNicholas Piggin * @end: end of the VM area to map (non-inclusive) 627b67177ecSNicholas Piggin * @prot: page protection flags to use 628b67177ecSNicholas Piggin * @pages: pages to map (always PAGE_SIZE pages) 629b67177ecSNicholas Piggin * @page_shift: maximum shift that the pages may be mapped with, @pages must 630b67177ecSNicholas Piggin * be aligned and contiguous up to at least this shift. 631b67177ecSNicholas Piggin * 632b67177ecSNicholas Piggin * RETURNS: 633b67177ecSNicholas Piggin * 0 on success, -errno on failure. 634b67177ecSNicholas Piggin */ 635121e6f32SNicholas Piggin static int vmap_pages_range(unsigned long addr, unsigned long end, 636121e6f32SNicholas Piggin pgprot_t prot, struct page **pages, unsigned int page_shift) 637121e6f32SNicholas Piggin { 638121e6f32SNicholas Piggin int err; 639121e6f32SNicholas Piggin 640121e6f32SNicholas Piggin err = vmap_pages_range_noflush(addr, end, prot, pages, page_shift); 641121e6f32SNicholas Piggin flush_cache_vmap(addr, end); 642121e6f32SNicholas Piggin return err; 643121e6f32SNicholas Piggin } 644121e6f32SNicholas Piggin 64581ac3ad9SKAMEZAWA Hiroyuki int is_vmalloc_or_module_addr(const void *x) 64673bdf0a6SLinus Torvalds { 64773bdf0a6SLinus Torvalds /* 648ab4f2ee1SRussell King * ARM, x86-64 and sparc64 put modules in a special place, 64973bdf0a6SLinus Torvalds * and fall back on vmalloc() if that fails. Others 65073bdf0a6SLinus Torvalds * just put it in the vmalloc space. 65173bdf0a6SLinus Torvalds */ 65273bdf0a6SLinus Torvalds #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) 6534aff1dc4SAndrey Konovalov unsigned long addr = (unsigned long)kasan_reset_tag(x); 65473bdf0a6SLinus Torvalds if (addr >= MODULES_VADDR && addr < MODULES_END) 65573bdf0a6SLinus Torvalds return 1; 65673bdf0a6SLinus Torvalds #endif 65773bdf0a6SLinus Torvalds return is_vmalloc_addr(x); 65873bdf0a6SLinus Torvalds } 65973bdf0a6SLinus Torvalds 66048667e7aSChristoph Lameter /* 661c0eb315aSNicholas Piggin * Walk a vmap address to the struct page it maps. Huge vmap mappings will 662c0eb315aSNicholas Piggin * return the tail page that corresponds to the base page address, which 663c0eb315aSNicholas Piggin * matches small vmap mappings. 66448667e7aSChristoph Lameter */ 665add688fbSmalc struct page *vmalloc_to_page(const void *vmalloc_addr) 66648667e7aSChristoph Lameter { 66748667e7aSChristoph Lameter unsigned long addr = (unsigned long) vmalloc_addr; 668add688fbSmalc struct page *page = NULL; 66948667e7aSChristoph Lameter pgd_t *pgd = pgd_offset_k(addr); 670c2febafcSKirill A. Shutemov p4d_t *p4d; 671c2febafcSKirill A. Shutemov pud_t *pud; 672c2febafcSKirill A. Shutemov pmd_t *pmd; 673c2febafcSKirill A. Shutemov pte_t *ptep, pte; 67448667e7aSChristoph Lameter 6757aa413deSIngo Molnar /* 6767aa413deSIngo Molnar * XXX we might need to change this if we add VIRTUAL_BUG_ON for 6777aa413deSIngo Molnar * architectures that do not vmalloc module space 6787aa413deSIngo Molnar */ 67973bdf0a6SLinus Torvalds VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); 68059ea7463SJiri Slaby 681c2febafcSKirill A. Shutemov if (pgd_none(*pgd)) 682c2febafcSKirill A. Shutemov return NULL; 683c0eb315aSNicholas Piggin if (WARN_ON_ONCE(pgd_leaf(*pgd))) 684c0eb315aSNicholas Piggin return NULL; /* XXX: no allowance for huge pgd */ 685c0eb315aSNicholas Piggin if (WARN_ON_ONCE(pgd_bad(*pgd))) 686c0eb315aSNicholas Piggin return NULL; 687c0eb315aSNicholas Piggin 688c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 689c2febafcSKirill A. Shutemov if (p4d_none(*p4d)) 690c2febafcSKirill A. Shutemov return NULL; 691c0eb315aSNicholas Piggin if (p4d_leaf(*p4d)) 692c0eb315aSNicholas Piggin return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT); 693c0eb315aSNicholas Piggin if (WARN_ON_ONCE(p4d_bad(*p4d))) 694c2febafcSKirill A. Shutemov return NULL; 695c0eb315aSNicholas Piggin 696c0eb315aSNicholas Piggin pud = pud_offset(p4d, addr); 697c0eb315aSNicholas Piggin if (pud_none(*pud)) 698c0eb315aSNicholas Piggin return NULL; 699c0eb315aSNicholas Piggin if (pud_leaf(*pud)) 700c0eb315aSNicholas Piggin return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); 701c0eb315aSNicholas Piggin if (WARN_ON_ONCE(pud_bad(*pud))) 702c0eb315aSNicholas Piggin return NULL; 703c0eb315aSNicholas Piggin 704c2febafcSKirill A. Shutemov pmd = pmd_offset(pud, addr); 705c0eb315aSNicholas Piggin if (pmd_none(*pmd)) 706c0eb315aSNicholas Piggin return NULL; 707c0eb315aSNicholas Piggin if (pmd_leaf(*pmd)) 708c0eb315aSNicholas Piggin return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); 709c0eb315aSNicholas Piggin if (WARN_ON_ONCE(pmd_bad(*pmd))) 710c2febafcSKirill A. Shutemov return NULL; 711db64fe02SNick Piggin 71248667e7aSChristoph Lameter ptep = pte_offset_map(pmd, addr); 71348667e7aSChristoph Lameter pte = *ptep; 71448667e7aSChristoph Lameter if (pte_present(pte)) 715add688fbSmalc page = pte_page(pte); 71648667e7aSChristoph Lameter pte_unmap(ptep); 717c0eb315aSNicholas Piggin 718add688fbSmalc return page; 719ece86e22SJianyu Zhan } 720ece86e22SJianyu Zhan EXPORT_SYMBOL(vmalloc_to_page); 721ece86e22SJianyu Zhan 722add688fbSmalc /* 723add688fbSmalc * Map a vmalloc()-space virtual address to the physical page frame number. 724add688fbSmalc */ 725add688fbSmalc unsigned long vmalloc_to_pfn(const void *vmalloc_addr) 726add688fbSmalc { 727add688fbSmalc return page_to_pfn(vmalloc_to_page(vmalloc_addr)); 728add688fbSmalc } 729add688fbSmalc EXPORT_SYMBOL(vmalloc_to_pfn); 730add688fbSmalc 731db64fe02SNick Piggin 732db64fe02SNick Piggin /*** Global kva allocator ***/ 733db64fe02SNick Piggin 734bb850f4dSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_PROPAGATE_CHECK 0 735a6cf4e0fSUladzislau Rezki (Sony) #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0 736bb850f4dSUladzislau Rezki (Sony) 737db64fe02SNick Piggin 738db64fe02SNick Piggin static DEFINE_SPINLOCK(vmap_area_lock); 739e36176beSUladzislau Rezki (Sony) static DEFINE_SPINLOCK(free_vmap_area_lock); 740f1c4069eSJoonsoo Kim /* Export for kexec only */ 741f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list); 74289699605SNick Piggin static struct rb_root vmap_area_root = RB_ROOT; 74368ad4a33SUladzislau Rezki (Sony) static bool vmap_initialized __read_mostly; 74489699605SNick Piggin 74596e2db45SUladzislau Rezki (Sony) static struct rb_root purge_vmap_area_root = RB_ROOT; 74696e2db45SUladzislau Rezki (Sony) static LIST_HEAD(purge_vmap_area_list); 74796e2db45SUladzislau Rezki (Sony) static DEFINE_SPINLOCK(purge_vmap_area_lock); 74896e2db45SUladzislau Rezki (Sony) 74968ad4a33SUladzislau Rezki (Sony) /* 75068ad4a33SUladzislau Rezki (Sony) * This kmem_cache is used for vmap_area objects. Instead of 75168ad4a33SUladzislau Rezki (Sony) * allocating from slab we reuse an object from this cache to 75268ad4a33SUladzislau Rezki (Sony) * make things faster. Especially in "no edge" splitting of 75368ad4a33SUladzislau Rezki (Sony) * free block. 75468ad4a33SUladzislau Rezki (Sony) */ 75568ad4a33SUladzislau Rezki (Sony) static struct kmem_cache *vmap_area_cachep; 75689699605SNick Piggin 75768ad4a33SUladzislau Rezki (Sony) /* 75868ad4a33SUladzislau Rezki (Sony) * This linked list is used in pair with free_vmap_area_root. 75968ad4a33SUladzislau Rezki (Sony) * It gives O(1) access to prev/next to perform fast coalescing. 76068ad4a33SUladzislau Rezki (Sony) */ 76168ad4a33SUladzislau Rezki (Sony) static LIST_HEAD(free_vmap_area_list); 76268ad4a33SUladzislau Rezki (Sony) 76368ad4a33SUladzislau Rezki (Sony) /* 76468ad4a33SUladzislau Rezki (Sony) * This augment red-black tree represents the free vmap space. 76568ad4a33SUladzislau Rezki (Sony) * All vmap_area objects in this tree are sorted by va->va_start 76668ad4a33SUladzislau Rezki (Sony) * address. It is used for allocation and merging when a vmap 76768ad4a33SUladzislau Rezki (Sony) * object is released. 76868ad4a33SUladzislau Rezki (Sony) * 76968ad4a33SUladzislau Rezki (Sony) * Each vmap_area node contains a maximum available free block 77068ad4a33SUladzislau Rezki (Sony) * of its sub-tree, right or left. Therefore it is possible to 77168ad4a33SUladzislau Rezki (Sony) * find a lowest match of free area. 77268ad4a33SUladzislau Rezki (Sony) */ 77368ad4a33SUladzislau Rezki (Sony) static struct rb_root free_vmap_area_root = RB_ROOT; 77468ad4a33SUladzislau Rezki (Sony) 77582dd23e8SUladzislau Rezki (Sony) /* 77682dd23e8SUladzislau Rezki (Sony) * Preload a CPU with one object for "no edge" split case. The 77782dd23e8SUladzislau Rezki (Sony) * aim is to get rid of allocations from the atomic context, thus 77882dd23e8SUladzislau Rezki (Sony) * to use more permissive allocation masks. 77982dd23e8SUladzislau Rezki (Sony) */ 78082dd23e8SUladzislau Rezki (Sony) static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node); 78182dd23e8SUladzislau Rezki (Sony) 78268ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 78368ad4a33SUladzislau Rezki (Sony) va_size(struct vmap_area *va) 78468ad4a33SUladzislau Rezki (Sony) { 78568ad4a33SUladzislau Rezki (Sony) return (va->va_end - va->va_start); 78668ad4a33SUladzislau Rezki (Sony) } 78768ad4a33SUladzislau Rezki (Sony) 78868ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 78968ad4a33SUladzislau Rezki (Sony) get_subtree_max_size(struct rb_node *node) 79068ad4a33SUladzislau Rezki (Sony) { 79168ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 79268ad4a33SUladzislau Rezki (Sony) 79368ad4a33SUladzislau Rezki (Sony) va = rb_entry_safe(node, struct vmap_area, rb_node); 79468ad4a33SUladzislau Rezki (Sony) return va ? va->subtree_max_size : 0; 79568ad4a33SUladzislau Rezki (Sony) } 79668ad4a33SUladzislau Rezki (Sony) 797315cc066SMichel Lespinasse RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb, 798315cc066SMichel Lespinasse struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size) 79968ad4a33SUladzislau Rezki (Sony) 80068ad4a33SUladzislau Rezki (Sony) static void purge_vmap_area_lazy(void); 80168ad4a33SUladzislau Rezki (Sony) static BLOCKING_NOTIFIER_HEAD(vmap_notify_list); 802690467c8SUladzislau Rezki (Sony) static void drain_vmap_area_work(struct work_struct *work); 803690467c8SUladzislau Rezki (Sony) static DECLARE_WORK(drain_vmap_work, drain_vmap_area_work); 804db64fe02SNick Piggin 80597105f0aSRoman Gushchin static atomic_long_t nr_vmalloc_pages; 80697105f0aSRoman Gushchin 80797105f0aSRoman Gushchin unsigned long vmalloc_nr_pages(void) 80897105f0aSRoman Gushchin { 80997105f0aSRoman Gushchin return atomic_long_read(&nr_vmalloc_pages); 81097105f0aSRoman Gushchin } 81197105f0aSRoman Gushchin 812153090f2SBaoquan He /* Look up the first VA which satisfies addr < va_end, NULL if none. */ 813f181234aSChen Wandun static struct vmap_area *find_vmap_area_exceed_addr(unsigned long addr) 814f181234aSChen Wandun { 815f181234aSChen Wandun struct vmap_area *va = NULL; 816f181234aSChen Wandun struct rb_node *n = vmap_area_root.rb_node; 817f181234aSChen Wandun 8184aff1dc4SAndrey Konovalov addr = (unsigned long)kasan_reset_tag((void *)addr); 8194aff1dc4SAndrey Konovalov 820f181234aSChen Wandun while (n) { 821f181234aSChen Wandun struct vmap_area *tmp; 822f181234aSChen Wandun 823f181234aSChen Wandun tmp = rb_entry(n, struct vmap_area, rb_node); 824f181234aSChen Wandun if (tmp->va_end > addr) { 825f181234aSChen Wandun va = tmp; 826f181234aSChen Wandun if (tmp->va_start <= addr) 827f181234aSChen Wandun break; 828f181234aSChen Wandun 829f181234aSChen Wandun n = n->rb_left; 830f181234aSChen Wandun } else 831f181234aSChen Wandun n = n->rb_right; 832f181234aSChen Wandun } 833f181234aSChen Wandun 834f181234aSChen Wandun return va; 835f181234aSChen Wandun } 836f181234aSChen Wandun 837899c6efeSUladzislau Rezki (Sony) static struct vmap_area *__find_vmap_area(unsigned long addr, struct rb_root *root) 8381da177e4SLinus Torvalds { 839899c6efeSUladzislau Rezki (Sony) struct rb_node *n = root->rb_node; 840db64fe02SNick Piggin 8414aff1dc4SAndrey Konovalov addr = (unsigned long)kasan_reset_tag((void *)addr); 8424aff1dc4SAndrey Konovalov 843db64fe02SNick Piggin while (n) { 844db64fe02SNick Piggin struct vmap_area *va; 845db64fe02SNick Piggin 846db64fe02SNick Piggin va = rb_entry(n, struct vmap_area, rb_node); 847db64fe02SNick Piggin if (addr < va->va_start) 848db64fe02SNick Piggin n = n->rb_left; 849cef2ac3fSHATAYAMA Daisuke else if (addr >= va->va_end) 850db64fe02SNick Piggin n = n->rb_right; 851db64fe02SNick Piggin else 852db64fe02SNick Piggin return va; 853db64fe02SNick Piggin } 854db64fe02SNick Piggin 855db64fe02SNick Piggin return NULL; 856db64fe02SNick Piggin } 857db64fe02SNick Piggin 85868ad4a33SUladzislau Rezki (Sony) /* 85968ad4a33SUladzislau Rezki (Sony) * This function returns back addresses of parent node 86068ad4a33SUladzislau Rezki (Sony) * and its left or right link for further processing. 8619c801f61SUladzislau Rezki (Sony) * 8629c801f61SUladzislau Rezki (Sony) * Otherwise NULL is returned. In that case all further 8639c801f61SUladzislau Rezki (Sony) * steps regarding inserting of conflicting overlap range 8649c801f61SUladzislau Rezki (Sony) * have to be declined and actually considered as a bug. 86568ad4a33SUladzislau Rezki (Sony) */ 86668ad4a33SUladzislau Rezki (Sony) static __always_inline struct rb_node ** 86768ad4a33SUladzislau Rezki (Sony) find_va_links(struct vmap_area *va, 86868ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct rb_node *from, 86968ad4a33SUladzislau Rezki (Sony) struct rb_node **parent) 870db64fe02SNick Piggin { 871170168d0SNamhyung Kim struct vmap_area *tmp_va; 87268ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 873db64fe02SNick Piggin 87468ad4a33SUladzislau Rezki (Sony) if (root) { 87568ad4a33SUladzislau Rezki (Sony) link = &root->rb_node; 87668ad4a33SUladzislau Rezki (Sony) if (unlikely(!*link)) { 87768ad4a33SUladzislau Rezki (Sony) *parent = NULL; 87868ad4a33SUladzislau Rezki (Sony) return link; 87968ad4a33SUladzislau Rezki (Sony) } 88068ad4a33SUladzislau Rezki (Sony) } else { 88168ad4a33SUladzislau Rezki (Sony) link = &from; 88268ad4a33SUladzislau Rezki (Sony) } 88368ad4a33SUladzislau Rezki (Sony) 88468ad4a33SUladzislau Rezki (Sony) /* 88568ad4a33SUladzislau Rezki (Sony) * Go to the bottom of the tree. When we hit the last point 88668ad4a33SUladzislau Rezki (Sony) * we end up with parent rb_node and correct direction, i name 88768ad4a33SUladzislau Rezki (Sony) * it link, where the new va->rb_node will be attached to. 88868ad4a33SUladzislau Rezki (Sony) */ 88968ad4a33SUladzislau Rezki (Sony) do { 89068ad4a33SUladzislau Rezki (Sony) tmp_va = rb_entry(*link, struct vmap_area, rb_node); 89168ad4a33SUladzislau Rezki (Sony) 89268ad4a33SUladzislau Rezki (Sony) /* 89368ad4a33SUladzislau Rezki (Sony) * During the traversal we also do some sanity check. 89468ad4a33SUladzislau Rezki (Sony) * Trigger the BUG() if there are sides(left/right) 89568ad4a33SUladzislau Rezki (Sony) * or full overlaps. 89668ad4a33SUladzislau Rezki (Sony) */ 897753df96bSBaoquan He if (va->va_end <= tmp_va->va_start) 89868ad4a33SUladzislau Rezki (Sony) link = &(*link)->rb_left; 899753df96bSBaoquan He else if (va->va_start >= tmp_va->va_end) 90068ad4a33SUladzislau Rezki (Sony) link = &(*link)->rb_right; 9019c801f61SUladzislau Rezki (Sony) else { 9029c801f61SUladzislau Rezki (Sony) WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n", 9039c801f61SUladzislau Rezki (Sony) va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end); 9049c801f61SUladzislau Rezki (Sony) 9059c801f61SUladzislau Rezki (Sony) return NULL; 9069c801f61SUladzislau Rezki (Sony) } 90768ad4a33SUladzislau Rezki (Sony) } while (*link); 90868ad4a33SUladzislau Rezki (Sony) 90968ad4a33SUladzislau Rezki (Sony) *parent = &tmp_va->rb_node; 91068ad4a33SUladzislau Rezki (Sony) return link; 911db64fe02SNick Piggin } 912db64fe02SNick Piggin 91368ad4a33SUladzislau Rezki (Sony) static __always_inline struct list_head * 91468ad4a33SUladzislau Rezki (Sony) get_va_next_sibling(struct rb_node *parent, struct rb_node **link) 91568ad4a33SUladzislau Rezki (Sony) { 91668ad4a33SUladzislau Rezki (Sony) struct list_head *list; 917db64fe02SNick Piggin 91868ad4a33SUladzislau Rezki (Sony) if (unlikely(!parent)) 91968ad4a33SUladzislau Rezki (Sony) /* 92068ad4a33SUladzislau Rezki (Sony) * The red-black tree where we try to find VA neighbors 92168ad4a33SUladzislau Rezki (Sony) * before merging or inserting is empty, i.e. it means 92268ad4a33SUladzislau Rezki (Sony) * there is no free vmap space. Normally it does not 92368ad4a33SUladzislau Rezki (Sony) * happen but we handle this case anyway. 92468ad4a33SUladzislau Rezki (Sony) */ 92568ad4a33SUladzislau Rezki (Sony) return NULL; 92668ad4a33SUladzislau Rezki (Sony) 92768ad4a33SUladzislau Rezki (Sony) list = &rb_entry(parent, struct vmap_area, rb_node)->list; 92868ad4a33SUladzislau Rezki (Sony) return (&parent->rb_right == link ? list->next : list); 929db64fe02SNick Piggin } 930db64fe02SNick Piggin 93168ad4a33SUladzislau Rezki (Sony) static __always_inline void 9328eb510dbSUladzislau Rezki (Sony) __link_va(struct vmap_area *va, struct rb_root *root, 9338eb510dbSUladzislau Rezki (Sony) struct rb_node *parent, struct rb_node **link, 9348eb510dbSUladzislau Rezki (Sony) struct list_head *head, bool augment) 93568ad4a33SUladzislau Rezki (Sony) { 93668ad4a33SUladzislau Rezki (Sony) /* 93768ad4a33SUladzislau Rezki (Sony) * VA is still not in the list, but we can 93868ad4a33SUladzislau Rezki (Sony) * identify its future previous list_head node. 93968ad4a33SUladzislau Rezki (Sony) */ 94068ad4a33SUladzislau Rezki (Sony) if (likely(parent)) { 94168ad4a33SUladzislau Rezki (Sony) head = &rb_entry(parent, struct vmap_area, rb_node)->list; 94268ad4a33SUladzislau Rezki (Sony) if (&parent->rb_right != link) 94368ad4a33SUladzislau Rezki (Sony) head = head->prev; 94468ad4a33SUladzislau Rezki (Sony) } 945db64fe02SNick Piggin 94668ad4a33SUladzislau Rezki (Sony) /* Insert to the rb-tree */ 94768ad4a33SUladzislau Rezki (Sony) rb_link_node(&va->rb_node, parent, link); 9488eb510dbSUladzislau Rezki (Sony) if (augment) { 94968ad4a33SUladzislau Rezki (Sony) /* 95068ad4a33SUladzislau Rezki (Sony) * Some explanation here. Just perform simple insertion 95168ad4a33SUladzislau Rezki (Sony) * to the tree. We do not set va->subtree_max_size to 95268ad4a33SUladzislau Rezki (Sony) * its current size before calling rb_insert_augmented(). 953153090f2SBaoquan He * It is because we populate the tree from the bottom 95468ad4a33SUladzislau Rezki (Sony) * to parent levels when the node _is_ in the tree. 95568ad4a33SUladzislau Rezki (Sony) * 95668ad4a33SUladzislau Rezki (Sony) * Therefore we set subtree_max_size to zero after insertion, 95768ad4a33SUladzislau Rezki (Sony) * to let __augment_tree_propagate_from() puts everything to 95868ad4a33SUladzislau Rezki (Sony) * the correct order later on. 95968ad4a33SUladzislau Rezki (Sony) */ 96068ad4a33SUladzislau Rezki (Sony) rb_insert_augmented(&va->rb_node, 96168ad4a33SUladzislau Rezki (Sony) root, &free_vmap_area_rb_augment_cb); 96268ad4a33SUladzislau Rezki (Sony) va->subtree_max_size = 0; 96368ad4a33SUladzislau Rezki (Sony) } else { 96468ad4a33SUladzislau Rezki (Sony) rb_insert_color(&va->rb_node, root); 96568ad4a33SUladzislau Rezki (Sony) } 96668ad4a33SUladzislau Rezki (Sony) 96768ad4a33SUladzislau Rezki (Sony) /* Address-sort this list */ 96868ad4a33SUladzislau Rezki (Sony) list_add(&va->list, head); 96968ad4a33SUladzislau Rezki (Sony) } 97068ad4a33SUladzislau Rezki (Sony) 97168ad4a33SUladzislau Rezki (Sony) static __always_inline void 9728eb510dbSUladzislau Rezki (Sony) link_va(struct vmap_area *va, struct rb_root *root, 9738eb510dbSUladzislau Rezki (Sony) struct rb_node *parent, struct rb_node **link, 9748eb510dbSUladzislau Rezki (Sony) struct list_head *head) 9758eb510dbSUladzislau Rezki (Sony) { 9768eb510dbSUladzislau Rezki (Sony) __link_va(va, root, parent, link, head, false); 9778eb510dbSUladzislau Rezki (Sony) } 9788eb510dbSUladzislau Rezki (Sony) 9798eb510dbSUladzislau Rezki (Sony) static __always_inline void 9808eb510dbSUladzislau Rezki (Sony) link_va_augment(struct vmap_area *va, struct rb_root *root, 9818eb510dbSUladzislau Rezki (Sony) struct rb_node *parent, struct rb_node **link, 9828eb510dbSUladzislau Rezki (Sony) struct list_head *head) 9838eb510dbSUladzislau Rezki (Sony) { 9848eb510dbSUladzislau Rezki (Sony) __link_va(va, root, parent, link, head, true); 9858eb510dbSUladzislau Rezki (Sony) } 9868eb510dbSUladzislau Rezki (Sony) 9878eb510dbSUladzislau Rezki (Sony) static __always_inline void 9888eb510dbSUladzislau Rezki (Sony) __unlink_va(struct vmap_area *va, struct rb_root *root, bool augment) 98968ad4a33SUladzislau Rezki (Sony) { 990460e42d1SUladzislau Rezki (Sony) if (WARN_ON(RB_EMPTY_NODE(&va->rb_node))) 991460e42d1SUladzislau Rezki (Sony) return; 992460e42d1SUladzislau Rezki (Sony) 9938eb510dbSUladzislau Rezki (Sony) if (augment) 99468ad4a33SUladzislau Rezki (Sony) rb_erase_augmented(&va->rb_node, 99568ad4a33SUladzislau Rezki (Sony) root, &free_vmap_area_rb_augment_cb); 99668ad4a33SUladzislau Rezki (Sony) else 99768ad4a33SUladzislau Rezki (Sony) rb_erase(&va->rb_node, root); 99868ad4a33SUladzislau Rezki (Sony) 9995d7a7c54SUladzislau Rezki (Sony) list_del_init(&va->list); 100068ad4a33SUladzislau Rezki (Sony) RB_CLEAR_NODE(&va->rb_node); 100168ad4a33SUladzislau Rezki (Sony) } 100268ad4a33SUladzislau Rezki (Sony) 10038eb510dbSUladzislau Rezki (Sony) static __always_inline void 10048eb510dbSUladzislau Rezki (Sony) unlink_va(struct vmap_area *va, struct rb_root *root) 10058eb510dbSUladzislau Rezki (Sony) { 10068eb510dbSUladzislau Rezki (Sony) __unlink_va(va, root, false); 10078eb510dbSUladzislau Rezki (Sony) } 10088eb510dbSUladzislau Rezki (Sony) 10098eb510dbSUladzislau Rezki (Sony) static __always_inline void 10108eb510dbSUladzislau Rezki (Sony) unlink_va_augment(struct vmap_area *va, struct rb_root *root) 10118eb510dbSUladzislau Rezki (Sony) { 10128eb510dbSUladzislau Rezki (Sony) __unlink_va(va, root, true); 10138eb510dbSUladzislau Rezki (Sony) } 10148eb510dbSUladzislau Rezki (Sony) 1015bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK 1016c3385e84SJiapeng Chong /* 1017c3385e84SJiapeng Chong * Gets called when remove the node and rotate. 1018c3385e84SJiapeng Chong */ 1019c3385e84SJiapeng Chong static __always_inline unsigned long 1020c3385e84SJiapeng Chong compute_subtree_max_size(struct vmap_area *va) 1021c3385e84SJiapeng Chong { 1022c3385e84SJiapeng Chong return max3(va_size(va), 1023c3385e84SJiapeng Chong get_subtree_max_size(va->rb_node.rb_left), 1024c3385e84SJiapeng Chong get_subtree_max_size(va->rb_node.rb_right)); 1025c3385e84SJiapeng Chong } 1026c3385e84SJiapeng Chong 1027bb850f4dSUladzislau Rezki (Sony) static void 1028da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(void) 1029bb850f4dSUladzislau Rezki (Sony) { 1030bb850f4dSUladzislau Rezki (Sony) struct vmap_area *va; 1031da27c9edSUladzislau Rezki (Sony) unsigned long computed_size; 1032bb850f4dSUladzislau Rezki (Sony) 1033da27c9edSUladzislau Rezki (Sony) list_for_each_entry(va, &free_vmap_area_list, list) { 1034da27c9edSUladzislau Rezki (Sony) computed_size = compute_subtree_max_size(va); 1035da27c9edSUladzislau Rezki (Sony) if (computed_size != va->subtree_max_size) 1036bb850f4dSUladzislau Rezki (Sony) pr_emerg("tree is corrupted: %lu, %lu\n", 1037bb850f4dSUladzislau Rezki (Sony) va_size(va), va->subtree_max_size); 1038bb850f4dSUladzislau Rezki (Sony) } 1039bb850f4dSUladzislau Rezki (Sony) } 1040bb850f4dSUladzislau Rezki (Sony) #endif 1041bb850f4dSUladzislau Rezki (Sony) 104268ad4a33SUladzislau Rezki (Sony) /* 104368ad4a33SUladzislau Rezki (Sony) * This function populates subtree_max_size from bottom to upper 104468ad4a33SUladzislau Rezki (Sony) * levels starting from VA point. The propagation must be done 104568ad4a33SUladzislau Rezki (Sony) * when VA size is modified by changing its va_start/va_end. Or 104668ad4a33SUladzislau Rezki (Sony) * in case of newly inserting of VA to the tree. 104768ad4a33SUladzislau Rezki (Sony) * 104868ad4a33SUladzislau Rezki (Sony) * It means that __augment_tree_propagate_from() must be called: 104968ad4a33SUladzislau Rezki (Sony) * - After VA has been inserted to the tree(free path); 105068ad4a33SUladzislau Rezki (Sony) * - After VA has been shrunk(allocation path); 105168ad4a33SUladzislau Rezki (Sony) * - After VA has been increased(merging path). 105268ad4a33SUladzislau Rezki (Sony) * 105368ad4a33SUladzislau Rezki (Sony) * Please note that, it does not mean that upper parent nodes 105468ad4a33SUladzislau Rezki (Sony) * and their subtree_max_size are recalculated all the time up 105568ad4a33SUladzislau Rezki (Sony) * to the root node. 105668ad4a33SUladzislau Rezki (Sony) * 105768ad4a33SUladzislau Rezki (Sony) * 4--8 105868ad4a33SUladzislau Rezki (Sony) * /\ 105968ad4a33SUladzislau Rezki (Sony) * / \ 106068ad4a33SUladzislau Rezki (Sony) * / \ 106168ad4a33SUladzislau Rezki (Sony) * 2--2 8--8 106268ad4a33SUladzislau Rezki (Sony) * 106368ad4a33SUladzislau Rezki (Sony) * For example if we modify the node 4, shrinking it to 2, then 106468ad4a33SUladzislau Rezki (Sony) * no any modification is required. If we shrink the node 2 to 1 106568ad4a33SUladzislau Rezki (Sony) * its subtree_max_size is updated only, and set to 1. If we shrink 106668ad4a33SUladzislau Rezki (Sony) * the node 8 to 6, then its subtree_max_size is set to 6 and parent 106768ad4a33SUladzislau Rezki (Sony) * node becomes 4--6. 106868ad4a33SUladzislau Rezki (Sony) */ 106968ad4a33SUladzislau Rezki (Sony) static __always_inline void 107068ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(struct vmap_area *va) 107168ad4a33SUladzislau Rezki (Sony) { 107268ad4a33SUladzislau Rezki (Sony) /* 107315ae144fSUladzislau Rezki (Sony) * Populate the tree from bottom towards the root until 107415ae144fSUladzislau Rezki (Sony) * the calculated maximum available size of checked node 107515ae144fSUladzislau Rezki (Sony) * is equal to its current one. 107668ad4a33SUladzislau Rezki (Sony) */ 107715ae144fSUladzislau Rezki (Sony) free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL); 1078bb850f4dSUladzislau Rezki (Sony) 1079bb850f4dSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_PROPAGATE_CHECK 1080da27c9edSUladzislau Rezki (Sony) augment_tree_propagate_check(); 1081bb850f4dSUladzislau Rezki (Sony) #endif 108268ad4a33SUladzislau Rezki (Sony) } 108368ad4a33SUladzislau Rezki (Sony) 108468ad4a33SUladzislau Rezki (Sony) static void 108568ad4a33SUladzislau Rezki (Sony) insert_vmap_area(struct vmap_area *va, 108668ad4a33SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 108768ad4a33SUladzislau Rezki (Sony) { 108868ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 108968ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 109068ad4a33SUladzislau Rezki (Sony) 109168ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 10929c801f61SUladzislau Rezki (Sony) if (link) 109368ad4a33SUladzislau Rezki (Sony) link_va(va, root, parent, link, head); 109468ad4a33SUladzislau Rezki (Sony) } 109568ad4a33SUladzislau Rezki (Sony) 109668ad4a33SUladzislau Rezki (Sony) static void 109768ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(struct vmap_area *va, 109868ad4a33SUladzislau Rezki (Sony) struct rb_node *from, struct rb_root *root, 109968ad4a33SUladzislau Rezki (Sony) struct list_head *head) 110068ad4a33SUladzislau Rezki (Sony) { 110168ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 110268ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 110368ad4a33SUladzislau Rezki (Sony) 110468ad4a33SUladzislau Rezki (Sony) if (from) 110568ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, NULL, from, &parent); 110668ad4a33SUladzislau Rezki (Sony) else 110768ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 110868ad4a33SUladzislau Rezki (Sony) 11099c801f61SUladzislau Rezki (Sony) if (link) { 11108eb510dbSUladzislau Rezki (Sony) link_va_augment(va, root, parent, link, head); 111168ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 111268ad4a33SUladzislau Rezki (Sony) } 11139c801f61SUladzislau Rezki (Sony) } 111468ad4a33SUladzislau Rezki (Sony) 111568ad4a33SUladzislau Rezki (Sony) /* 111668ad4a33SUladzislau Rezki (Sony) * Merge de-allocated chunk of VA memory with previous 111768ad4a33SUladzislau Rezki (Sony) * and next free blocks. If coalesce is not done a new 111868ad4a33SUladzislau Rezki (Sony) * free area is inserted. If VA has been merged, it is 111968ad4a33SUladzislau Rezki (Sony) * freed. 11209c801f61SUladzislau Rezki (Sony) * 11219c801f61SUladzislau Rezki (Sony) * Please note, it can return NULL in case of overlap 11229c801f61SUladzislau Rezki (Sony) * ranges, followed by WARN() report. Despite it is a 11239c801f61SUladzislau Rezki (Sony) * buggy behaviour, a system can be alive and keep 11249c801f61SUladzislau Rezki (Sony) * ongoing. 112568ad4a33SUladzislau Rezki (Sony) */ 11263c5c3cfbSDaniel Axtens static __always_inline struct vmap_area * 11278eb510dbSUladzislau Rezki (Sony) __merge_or_add_vmap_area(struct vmap_area *va, 11288eb510dbSUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head, bool augment) 112968ad4a33SUladzislau Rezki (Sony) { 113068ad4a33SUladzislau Rezki (Sony) struct vmap_area *sibling; 113168ad4a33SUladzislau Rezki (Sony) struct list_head *next; 113268ad4a33SUladzislau Rezki (Sony) struct rb_node **link; 113368ad4a33SUladzislau Rezki (Sony) struct rb_node *parent; 113468ad4a33SUladzislau Rezki (Sony) bool merged = false; 113568ad4a33SUladzislau Rezki (Sony) 113668ad4a33SUladzislau Rezki (Sony) /* 113768ad4a33SUladzislau Rezki (Sony) * Find a place in the tree where VA potentially will be 113868ad4a33SUladzislau Rezki (Sony) * inserted, unless it is merged with its sibling/siblings. 113968ad4a33SUladzislau Rezki (Sony) */ 114068ad4a33SUladzislau Rezki (Sony) link = find_va_links(va, root, NULL, &parent); 11419c801f61SUladzislau Rezki (Sony) if (!link) 11429c801f61SUladzislau Rezki (Sony) return NULL; 114368ad4a33SUladzislau Rezki (Sony) 114468ad4a33SUladzislau Rezki (Sony) /* 114568ad4a33SUladzislau Rezki (Sony) * Get next node of VA to check if merging can be done. 114668ad4a33SUladzislau Rezki (Sony) */ 114768ad4a33SUladzislau Rezki (Sony) next = get_va_next_sibling(parent, link); 114868ad4a33SUladzislau Rezki (Sony) if (unlikely(next == NULL)) 114968ad4a33SUladzislau Rezki (Sony) goto insert; 115068ad4a33SUladzislau Rezki (Sony) 115168ad4a33SUladzislau Rezki (Sony) /* 115268ad4a33SUladzislau Rezki (Sony) * start end 115368ad4a33SUladzislau Rezki (Sony) * | | 115468ad4a33SUladzislau Rezki (Sony) * |<------VA------>|<-----Next----->| 115568ad4a33SUladzislau Rezki (Sony) * | | 115668ad4a33SUladzislau Rezki (Sony) * start end 115768ad4a33SUladzislau Rezki (Sony) */ 115868ad4a33SUladzislau Rezki (Sony) if (next != head) { 115968ad4a33SUladzislau Rezki (Sony) sibling = list_entry(next, struct vmap_area, list); 116068ad4a33SUladzislau Rezki (Sony) if (sibling->va_start == va->va_end) { 116168ad4a33SUladzislau Rezki (Sony) sibling->va_start = va->va_start; 116268ad4a33SUladzislau Rezki (Sony) 116368ad4a33SUladzislau Rezki (Sony) /* Free vmap_area object. */ 116468ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 116568ad4a33SUladzislau Rezki (Sony) 116668ad4a33SUladzislau Rezki (Sony) /* Point to the new merged area. */ 116768ad4a33SUladzislau Rezki (Sony) va = sibling; 116868ad4a33SUladzislau Rezki (Sony) merged = true; 116968ad4a33SUladzislau Rezki (Sony) } 117068ad4a33SUladzislau Rezki (Sony) } 117168ad4a33SUladzislau Rezki (Sony) 117268ad4a33SUladzislau Rezki (Sony) /* 117368ad4a33SUladzislau Rezki (Sony) * start end 117468ad4a33SUladzislau Rezki (Sony) * | | 117568ad4a33SUladzislau Rezki (Sony) * |<-----Prev----->|<------VA------>| 117668ad4a33SUladzislau Rezki (Sony) * | | 117768ad4a33SUladzislau Rezki (Sony) * start end 117868ad4a33SUladzislau Rezki (Sony) */ 117968ad4a33SUladzislau Rezki (Sony) if (next->prev != head) { 118068ad4a33SUladzislau Rezki (Sony) sibling = list_entry(next->prev, struct vmap_area, list); 118168ad4a33SUladzislau Rezki (Sony) if (sibling->va_end == va->va_start) { 11825dd78640SUladzislau Rezki (Sony) /* 11835dd78640SUladzislau Rezki (Sony) * If both neighbors are coalesced, it is important 11845dd78640SUladzislau Rezki (Sony) * to unlink the "next" node first, followed by merging 11855dd78640SUladzislau Rezki (Sony) * with "previous" one. Otherwise the tree might not be 11865dd78640SUladzislau Rezki (Sony) * fully populated if a sibling's augmented value is 11875dd78640SUladzislau Rezki (Sony) * "normalized" because of rotation operations. 11885dd78640SUladzislau Rezki (Sony) */ 118954f63d9dSUladzislau Rezki (Sony) if (merged) 11908eb510dbSUladzislau Rezki (Sony) __unlink_va(va, root, augment); 119168ad4a33SUladzislau Rezki (Sony) 11925dd78640SUladzislau Rezki (Sony) sibling->va_end = va->va_end; 11935dd78640SUladzislau Rezki (Sony) 119468ad4a33SUladzislau Rezki (Sony) /* Free vmap_area object. */ 119568ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 11963c5c3cfbSDaniel Axtens 11973c5c3cfbSDaniel Axtens /* Point to the new merged area. */ 11983c5c3cfbSDaniel Axtens va = sibling; 11993c5c3cfbSDaniel Axtens merged = true; 120068ad4a33SUladzislau Rezki (Sony) } 120168ad4a33SUladzislau Rezki (Sony) } 120268ad4a33SUladzislau Rezki (Sony) 120368ad4a33SUladzislau Rezki (Sony) insert: 12045dd78640SUladzislau Rezki (Sony) if (!merged) 12058eb510dbSUladzislau Rezki (Sony) __link_va(va, root, parent, link, head, augment); 12063c5c3cfbSDaniel Axtens 120796e2db45SUladzislau Rezki (Sony) return va; 120896e2db45SUladzislau Rezki (Sony) } 120996e2db45SUladzislau Rezki (Sony) 121096e2db45SUladzislau Rezki (Sony) static __always_inline struct vmap_area * 12118eb510dbSUladzislau Rezki (Sony) merge_or_add_vmap_area(struct vmap_area *va, 12128eb510dbSUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 12138eb510dbSUladzislau Rezki (Sony) { 12148eb510dbSUladzislau Rezki (Sony) return __merge_or_add_vmap_area(va, root, head, false); 12158eb510dbSUladzislau Rezki (Sony) } 12168eb510dbSUladzislau Rezki (Sony) 12178eb510dbSUladzislau Rezki (Sony) static __always_inline struct vmap_area * 121896e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area_augment(struct vmap_area *va, 121996e2db45SUladzislau Rezki (Sony) struct rb_root *root, struct list_head *head) 122096e2db45SUladzislau Rezki (Sony) { 12218eb510dbSUladzislau Rezki (Sony) va = __merge_or_add_vmap_area(va, root, head, true); 122296e2db45SUladzislau Rezki (Sony) if (va) 12235dd78640SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 122496e2db45SUladzislau Rezki (Sony) 12253c5c3cfbSDaniel Axtens return va; 122668ad4a33SUladzislau Rezki (Sony) } 122768ad4a33SUladzislau Rezki (Sony) 122868ad4a33SUladzislau Rezki (Sony) static __always_inline bool 122968ad4a33SUladzislau Rezki (Sony) is_within_this_va(struct vmap_area *va, unsigned long size, 123068ad4a33SUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 123168ad4a33SUladzislau Rezki (Sony) { 123268ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr; 123368ad4a33SUladzislau Rezki (Sony) 123468ad4a33SUladzislau Rezki (Sony) if (va->va_start > vstart) 123568ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(va->va_start, align); 123668ad4a33SUladzislau Rezki (Sony) else 123768ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(vstart, align); 123868ad4a33SUladzislau Rezki (Sony) 123968ad4a33SUladzislau Rezki (Sony) /* Can be overflowed due to big size or alignment. */ 124068ad4a33SUladzislau Rezki (Sony) if (nva_start_addr + size < nva_start_addr || 124168ad4a33SUladzislau Rezki (Sony) nva_start_addr < vstart) 124268ad4a33SUladzislau Rezki (Sony) return false; 124368ad4a33SUladzislau Rezki (Sony) 124468ad4a33SUladzislau Rezki (Sony) return (nva_start_addr + size <= va->va_end); 124568ad4a33SUladzislau Rezki (Sony) } 124668ad4a33SUladzislau Rezki (Sony) 124768ad4a33SUladzislau Rezki (Sony) /* 124868ad4a33SUladzislau Rezki (Sony) * Find the first free block(lowest start address) in the tree, 124968ad4a33SUladzislau Rezki (Sony) * that will accomplish the request corresponding to passing 12509333fe98SUladzislau Rezki * parameters. Please note, with an alignment bigger than PAGE_SIZE, 12519333fe98SUladzislau Rezki * a search length is adjusted to account for worst case alignment 12529333fe98SUladzislau Rezki * overhead. 125368ad4a33SUladzislau Rezki (Sony) */ 125468ad4a33SUladzislau Rezki (Sony) static __always_inline struct vmap_area * 1255f9863be4SUladzislau Rezki (Sony) find_vmap_lowest_match(struct rb_root *root, unsigned long size, 1256f9863be4SUladzislau Rezki (Sony) unsigned long align, unsigned long vstart, bool adjust_search_size) 125768ad4a33SUladzislau Rezki (Sony) { 125868ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 125968ad4a33SUladzislau Rezki (Sony) struct rb_node *node; 12609333fe98SUladzislau Rezki unsigned long length; 126168ad4a33SUladzislau Rezki (Sony) 126268ad4a33SUladzislau Rezki (Sony) /* Start from the root. */ 1263f9863be4SUladzislau Rezki (Sony) node = root->rb_node; 126468ad4a33SUladzislau Rezki (Sony) 12659333fe98SUladzislau Rezki /* Adjust the search size for alignment overhead. */ 12669333fe98SUladzislau Rezki length = adjust_search_size ? size + align - 1 : size; 12679333fe98SUladzislau Rezki 126868ad4a33SUladzislau Rezki (Sony) while (node) { 126968ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 127068ad4a33SUladzislau Rezki (Sony) 12719333fe98SUladzislau Rezki if (get_subtree_max_size(node->rb_left) >= length && 127268ad4a33SUladzislau Rezki (Sony) vstart < va->va_start) { 127368ad4a33SUladzislau Rezki (Sony) node = node->rb_left; 127468ad4a33SUladzislau Rezki (Sony) } else { 127568ad4a33SUladzislau Rezki (Sony) if (is_within_this_va(va, size, align, vstart)) 127668ad4a33SUladzislau Rezki (Sony) return va; 127768ad4a33SUladzislau Rezki (Sony) 127868ad4a33SUladzislau Rezki (Sony) /* 127968ad4a33SUladzislau Rezki (Sony) * Does not make sense to go deeper towards the right 128068ad4a33SUladzislau Rezki (Sony) * sub-tree if it does not have a free block that is 12819333fe98SUladzislau Rezki * equal or bigger to the requested search length. 128268ad4a33SUladzislau Rezki (Sony) */ 12839333fe98SUladzislau Rezki if (get_subtree_max_size(node->rb_right) >= length) { 128468ad4a33SUladzislau Rezki (Sony) node = node->rb_right; 128568ad4a33SUladzislau Rezki (Sony) continue; 128668ad4a33SUladzislau Rezki (Sony) } 128768ad4a33SUladzislau Rezki (Sony) 128868ad4a33SUladzislau Rezki (Sony) /* 12893806b041SAndrew Morton * OK. We roll back and find the first right sub-tree, 129068ad4a33SUladzislau Rezki (Sony) * that will satisfy the search criteria. It can happen 12919f531973SUladzislau Rezki (Sony) * due to "vstart" restriction or an alignment overhead 12929f531973SUladzislau Rezki (Sony) * that is bigger then PAGE_SIZE. 129368ad4a33SUladzislau Rezki (Sony) */ 129468ad4a33SUladzislau Rezki (Sony) while ((node = rb_parent(node))) { 129568ad4a33SUladzislau Rezki (Sony) va = rb_entry(node, struct vmap_area, rb_node); 129668ad4a33SUladzislau Rezki (Sony) if (is_within_this_va(va, size, align, vstart)) 129768ad4a33SUladzislau Rezki (Sony) return va; 129868ad4a33SUladzislau Rezki (Sony) 12999333fe98SUladzislau Rezki if (get_subtree_max_size(node->rb_right) >= length && 130068ad4a33SUladzislau Rezki (Sony) vstart <= va->va_start) { 13019f531973SUladzislau Rezki (Sony) /* 13029f531973SUladzislau Rezki (Sony) * Shift the vstart forward. Please note, we update it with 13039f531973SUladzislau Rezki (Sony) * parent's start address adding "1" because we do not want 13049f531973SUladzislau Rezki (Sony) * to enter same sub-tree after it has already been checked 13059f531973SUladzislau Rezki (Sony) * and no suitable free block found there. 13069f531973SUladzislau Rezki (Sony) */ 13079f531973SUladzislau Rezki (Sony) vstart = va->va_start + 1; 130868ad4a33SUladzislau Rezki (Sony) node = node->rb_right; 130968ad4a33SUladzislau Rezki (Sony) break; 131068ad4a33SUladzislau Rezki (Sony) } 131168ad4a33SUladzislau Rezki (Sony) } 131268ad4a33SUladzislau Rezki (Sony) } 131368ad4a33SUladzislau Rezki (Sony) } 131468ad4a33SUladzislau Rezki (Sony) 131568ad4a33SUladzislau Rezki (Sony) return NULL; 131668ad4a33SUladzislau Rezki (Sony) } 131768ad4a33SUladzislau Rezki (Sony) 1318a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 1319a6cf4e0fSUladzislau Rezki (Sony) #include <linux/random.h> 1320a6cf4e0fSUladzislau Rezki (Sony) 1321a6cf4e0fSUladzislau Rezki (Sony) static struct vmap_area * 1322bd1264c3SSong Liu find_vmap_lowest_linear_match(struct list_head *head, unsigned long size, 1323a6cf4e0fSUladzislau Rezki (Sony) unsigned long align, unsigned long vstart) 1324a6cf4e0fSUladzislau Rezki (Sony) { 1325a6cf4e0fSUladzislau Rezki (Sony) struct vmap_area *va; 1326a6cf4e0fSUladzislau Rezki (Sony) 1327bd1264c3SSong Liu list_for_each_entry(va, head, list) { 1328a6cf4e0fSUladzislau Rezki (Sony) if (!is_within_this_va(va, size, align, vstart)) 1329a6cf4e0fSUladzislau Rezki (Sony) continue; 1330a6cf4e0fSUladzislau Rezki (Sony) 1331a6cf4e0fSUladzislau Rezki (Sony) return va; 1332a6cf4e0fSUladzislau Rezki (Sony) } 1333a6cf4e0fSUladzislau Rezki (Sony) 1334a6cf4e0fSUladzislau Rezki (Sony) return NULL; 1335a6cf4e0fSUladzislau Rezki (Sony) } 1336a6cf4e0fSUladzislau Rezki (Sony) 1337a6cf4e0fSUladzislau Rezki (Sony) static void 1338bd1264c3SSong Liu find_vmap_lowest_match_check(struct rb_root *root, struct list_head *head, 1339bd1264c3SSong Liu unsigned long size, unsigned long align) 1340a6cf4e0fSUladzislau Rezki (Sony) { 1341a6cf4e0fSUladzislau Rezki (Sony) struct vmap_area *va_1, *va_2; 1342a6cf4e0fSUladzislau Rezki (Sony) unsigned long vstart; 1343a6cf4e0fSUladzislau Rezki (Sony) unsigned int rnd; 1344a6cf4e0fSUladzislau Rezki (Sony) 1345a6cf4e0fSUladzislau Rezki (Sony) get_random_bytes(&rnd, sizeof(rnd)); 1346a6cf4e0fSUladzislau Rezki (Sony) vstart = VMALLOC_START + rnd; 1347a6cf4e0fSUladzislau Rezki (Sony) 1348bd1264c3SSong Liu va_1 = find_vmap_lowest_match(root, size, align, vstart, false); 1349bd1264c3SSong Liu va_2 = find_vmap_lowest_linear_match(head, size, align, vstart); 1350a6cf4e0fSUladzislau Rezki (Sony) 1351a6cf4e0fSUladzislau Rezki (Sony) if (va_1 != va_2) 1352a6cf4e0fSUladzislau Rezki (Sony) pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n", 1353a6cf4e0fSUladzislau Rezki (Sony) va_1, va_2, vstart); 1354a6cf4e0fSUladzislau Rezki (Sony) } 1355a6cf4e0fSUladzislau Rezki (Sony) #endif 1356a6cf4e0fSUladzislau Rezki (Sony) 135768ad4a33SUladzislau Rezki (Sony) enum fit_type { 135868ad4a33SUladzislau Rezki (Sony) NOTHING_FIT = 0, 135968ad4a33SUladzislau Rezki (Sony) FL_FIT_TYPE = 1, /* full fit */ 136068ad4a33SUladzislau Rezki (Sony) LE_FIT_TYPE = 2, /* left edge fit */ 136168ad4a33SUladzislau Rezki (Sony) RE_FIT_TYPE = 3, /* right edge fit */ 136268ad4a33SUladzislau Rezki (Sony) NE_FIT_TYPE = 4 /* no edge fit */ 136368ad4a33SUladzislau Rezki (Sony) }; 136468ad4a33SUladzislau Rezki (Sony) 136568ad4a33SUladzislau Rezki (Sony) static __always_inline enum fit_type 136668ad4a33SUladzislau Rezki (Sony) classify_va_fit_type(struct vmap_area *va, 136768ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr, unsigned long size) 136868ad4a33SUladzislau Rezki (Sony) { 136968ad4a33SUladzislau Rezki (Sony) enum fit_type type; 137068ad4a33SUladzislau Rezki (Sony) 137168ad4a33SUladzislau Rezki (Sony) /* Check if it is within VA. */ 137268ad4a33SUladzislau Rezki (Sony) if (nva_start_addr < va->va_start || 137368ad4a33SUladzislau Rezki (Sony) nva_start_addr + size > va->va_end) 137468ad4a33SUladzislau Rezki (Sony) return NOTHING_FIT; 137568ad4a33SUladzislau Rezki (Sony) 137668ad4a33SUladzislau Rezki (Sony) /* Now classify. */ 137768ad4a33SUladzislau Rezki (Sony) if (va->va_start == nva_start_addr) { 137868ad4a33SUladzislau Rezki (Sony) if (va->va_end == nva_start_addr + size) 137968ad4a33SUladzislau Rezki (Sony) type = FL_FIT_TYPE; 138068ad4a33SUladzislau Rezki (Sony) else 138168ad4a33SUladzislau Rezki (Sony) type = LE_FIT_TYPE; 138268ad4a33SUladzislau Rezki (Sony) } else if (va->va_end == nva_start_addr + size) { 138368ad4a33SUladzislau Rezki (Sony) type = RE_FIT_TYPE; 138468ad4a33SUladzislau Rezki (Sony) } else { 138568ad4a33SUladzislau Rezki (Sony) type = NE_FIT_TYPE; 138668ad4a33SUladzislau Rezki (Sony) } 138768ad4a33SUladzislau Rezki (Sony) 138868ad4a33SUladzislau Rezki (Sony) return type; 138968ad4a33SUladzislau Rezki (Sony) } 139068ad4a33SUladzislau Rezki (Sony) 139168ad4a33SUladzislau Rezki (Sony) static __always_inline int 1392f9863be4SUladzislau Rezki (Sony) adjust_va_to_fit_type(struct rb_root *root, struct list_head *head, 1393f9863be4SUladzislau Rezki (Sony) struct vmap_area *va, unsigned long nva_start_addr, 1394f9863be4SUladzislau Rezki (Sony) unsigned long size) 139568ad4a33SUladzislau Rezki (Sony) { 13962c929233SArnd Bergmann struct vmap_area *lva = NULL; 13971b23ff80SBaoquan He enum fit_type type = classify_va_fit_type(va, nva_start_addr, size); 139868ad4a33SUladzislau Rezki (Sony) 139968ad4a33SUladzislau Rezki (Sony) if (type == FL_FIT_TYPE) { 140068ad4a33SUladzislau Rezki (Sony) /* 140168ad4a33SUladzislau Rezki (Sony) * No need to split VA, it fully fits. 140268ad4a33SUladzislau Rezki (Sony) * 140368ad4a33SUladzislau Rezki (Sony) * | | 140468ad4a33SUladzislau Rezki (Sony) * V NVA V 140568ad4a33SUladzislau Rezki (Sony) * |---------------| 140668ad4a33SUladzislau Rezki (Sony) */ 1407f9863be4SUladzislau Rezki (Sony) unlink_va_augment(va, root); 140868ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 140968ad4a33SUladzislau Rezki (Sony) } else if (type == LE_FIT_TYPE) { 141068ad4a33SUladzislau Rezki (Sony) /* 141168ad4a33SUladzislau Rezki (Sony) * Split left edge of fit VA. 141268ad4a33SUladzislau Rezki (Sony) * 141368ad4a33SUladzislau Rezki (Sony) * | | 141468ad4a33SUladzislau Rezki (Sony) * V NVA V R 141568ad4a33SUladzislau Rezki (Sony) * |-------|-------| 141668ad4a33SUladzislau Rezki (Sony) */ 141768ad4a33SUladzislau Rezki (Sony) va->va_start += size; 141868ad4a33SUladzislau Rezki (Sony) } else if (type == RE_FIT_TYPE) { 141968ad4a33SUladzislau Rezki (Sony) /* 142068ad4a33SUladzislau Rezki (Sony) * Split right edge of fit VA. 142168ad4a33SUladzislau Rezki (Sony) * 142268ad4a33SUladzislau Rezki (Sony) * | | 142368ad4a33SUladzislau Rezki (Sony) * L V NVA V 142468ad4a33SUladzislau Rezki (Sony) * |-------|-------| 142568ad4a33SUladzislau Rezki (Sony) */ 142668ad4a33SUladzislau Rezki (Sony) va->va_end = nva_start_addr; 142768ad4a33SUladzislau Rezki (Sony) } else if (type == NE_FIT_TYPE) { 142868ad4a33SUladzislau Rezki (Sony) /* 142968ad4a33SUladzislau Rezki (Sony) * Split no edge of fit VA. 143068ad4a33SUladzislau Rezki (Sony) * 143168ad4a33SUladzislau Rezki (Sony) * | | 143268ad4a33SUladzislau Rezki (Sony) * L V NVA V R 143368ad4a33SUladzislau Rezki (Sony) * |---|-------|---| 143468ad4a33SUladzislau Rezki (Sony) */ 143582dd23e8SUladzislau Rezki (Sony) lva = __this_cpu_xchg(ne_fit_preload_node, NULL); 143682dd23e8SUladzislau Rezki (Sony) if (unlikely(!lva)) { 143782dd23e8SUladzislau Rezki (Sony) /* 143882dd23e8SUladzislau Rezki (Sony) * For percpu allocator we do not do any pre-allocation 143982dd23e8SUladzislau Rezki (Sony) * and leave it as it is. The reason is it most likely 144082dd23e8SUladzislau Rezki (Sony) * never ends up with NE_FIT_TYPE splitting. In case of 144182dd23e8SUladzislau Rezki (Sony) * percpu allocations offsets and sizes are aligned to 144282dd23e8SUladzislau Rezki (Sony) * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE 144382dd23e8SUladzislau Rezki (Sony) * are its main fitting cases. 144482dd23e8SUladzislau Rezki (Sony) * 144582dd23e8SUladzislau Rezki (Sony) * There are a few exceptions though, as an example it is 144682dd23e8SUladzislau Rezki (Sony) * a first allocation (early boot up) when we have "one" 144782dd23e8SUladzislau Rezki (Sony) * big free space that has to be split. 1448060650a2SUladzislau Rezki (Sony) * 1449060650a2SUladzislau Rezki (Sony) * Also we can hit this path in case of regular "vmap" 1450060650a2SUladzislau Rezki (Sony) * allocations, if "this" current CPU was not preloaded. 1451060650a2SUladzislau Rezki (Sony) * See the comment in alloc_vmap_area() why. If so, then 1452060650a2SUladzislau Rezki (Sony) * GFP_NOWAIT is used instead to get an extra object for 1453060650a2SUladzislau Rezki (Sony) * split purpose. That is rare and most time does not 1454060650a2SUladzislau Rezki (Sony) * occur. 1455060650a2SUladzislau Rezki (Sony) * 1456060650a2SUladzislau Rezki (Sony) * What happens if an allocation gets failed. Basically, 1457060650a2SUladzislau Rezki (Sony) * an "overflow" path is triggered to purge lazily freed 1458060650a2SUladzislau Rezki (Sony) * areas to free some memory, then, the "retry" path is 1459060650a2SUladzislau Rezki (Sony) * triggered to repeat one more time. See more details 1460060650a2SUladzislau Rezki (Sony) * in alloc_vmap_area() function. 146182dd23e8SUladzislau Rezki (Sony) */ 146268ad4a33SUladzislau Rezki (Sony) lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT); 146382dd23e8SUladzislau Rezki (Sony) if (!lva) 146468ad4a33SUladzislau Rezki (Sony) return -1; 146582dd23e8SUladzislau Rezki (Sony) } 146668ad4a33SUladzislau Rezki (Sony) 146768ad4a33SUladzislau Rezki (Sony) /* 146868ad4a33SUladzislau Rezki (Sony) * Build the remainder. 146968ad4a33SUladzislau Rezki (Sony) */ 147068ad4a33SUladzislau Rezki (Sony) lva->va_start = va->va_start; 147168ad4a33SUladzislau Rezki (Sony) lva->va_end = nva_start_addr; 147268ad4a33SUladzislau Rezki (Sony) 147368ad4a33SUladzislau Rezki (Sony) /* 147468ad4a33SUladzislau Rezki (Sony) * Shrink this VA to remaining size. 147568ad4a33SUladzislau Rezki (Sony) */ 147668ad4a33SUladzislau Rezki (Sony) va->va_start = nva_start_addr + size; 147768ad4a33SUladzislau Rezki (Sony) } else { 147868ad4a33SUladzislau Rezki (Sony) return -1; 147968ad4a33SUladzislau Rezki (Sony) } 148068ad4a33SUladzislau Rezki (Sony) 148168ad4a33SUladzislau Rezki (Sony) if (type != FL_FIT_TYPE) { 148268ad4a33SUladzislau Rezki (Sony) augment_tree_propagate_from(va); 148368ad4a33SUladzislau Rezki (Sony) 14842c929233SArnd Bergmann if (lva) /* type == NE_FIT_TYPE */ 1485f9863be4SUladzislau Rezki (Sony) insert_vmap_area_augment(lva, &va->rb_node, root, head); 148668ad4a33SUladzislau Rezki (Sony) } 148768ad4a33SUladzislau Rezki (Sony) 148868ad4a33SUladzislau Rezki (Sony) return 0; 148968ad4a33SUladzislau Rezki (Sony) } 149068ad4a33SUladzislau Rezki (Sony) 149168ad4a33SUladzislau Rezki (Sony) /* 149268ad4a33SUladzislau Rezki (Sony) * Returns a start address of the newly allocated area, if success. 149368ad4a33SUladzislau Rezki (Sony) * Otherwise a vend is returned that indicates failure. 149468ad4a33SUladzislau Rezki (Sony) */ 149568ad4a33SUladzislau Rezki (Sony) static __always_inline unsigned long 1496f9863be4SUladzislau Rezki (Sony) __alloc_vmap_area(struct rb_root *root, struct list_head *head, 1497f9863be4SUladzislau Rezki (Sony) unsigned long size, unsigned long align, 1498cacca6baSUladzislau Rezki (Sony) unsigned long vstart, unsigned long vend) 149968ad4a33SUladzislau Rezki (Sony) { 15009333fe98SUladzislau Rezki bool adjust_search_size = true; 150168ad4a33SUladzislau Rezki (Sony) unsigned long nva_start_addr; 150268ad4a33SUladzislau Rezki (Sony) struct vmap_area *va; 150368ad4a33SUladzislau Rezki (Sony) int ret; 150468ad4a33SUladzislau Rezki (Sony) 15059333fe98SUladzislau Rezki /* 15069333fe98SUladzislau Rezki * Do not adjust when: 15079333fe98SUladzislau Rezki * a) align <= PAGE_SIZE, because it does not make any sense. 15089333fe98SUladzislau Rezki * All blocks(their start addresses) are at least PAGE_SIZE 15099333fe98SUladzislau Rezki * aligned anyway; 15109333fe98SUladzislau Rezki * b) a short range where a requested size corresponds to exactly 15119333fe98SUladzislau Rezki * specified [vstart:vend] interval and an alignment > PAGE_SIZE. 15129333fe98SUladzislau Rezki * With adjusted search length an allocation would not succeed. 15139333fe98SUladzislau Rezki */ 15149333fe98SUladzislau Rezki if (align <= PAGE_SIZE || (align > PAGE_SIZE && (vend - vstart) == size)) 15159333fe98SUladzislau Rezki adjust_search_size = false; 15169333fe98SUladzislau Rezki 1517f9863be4SUladzislau Rezki (Sony) va = find_vmap_lowest_match(root, size, align, vstart, adjust_search_size); 151868ad4a33SUladzislau Rezki (Sony) if (unlikely(!va)) 151968ad4a33SUladzislau Rezki (Sony) return vend; 152068ad4a33SUladzislau Rezki (Sony) 152168ad4a33SUladzislau Rezki (Sony) if (va->va_start > vstart) 152268ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(va->va_start, align); 152368ad4a33SUladzislau Rezki (Sony) else 152468ad4a33SUladzislau Rezki (Sony) nva_start_addr = ALIGN(vstart, align); 152568ad4a33SUladzislau Rezki (Sony) 152668ad4a33SUladzislau Rezki (Sony) /* Check the "vend" restriction. */ 152768ad4a33SUladzislau Rezki (Sony) if (nva_start_addr + size > vend) 152868ad4a33SUladzislau Rezki (Sony) return vend; 152968ad4a33SUladzislau Rezki (Sony) 153068ad4a33SUladzislau Rezki (Sony) /* Update the free vmap_area. */ 1531f9863be4SUladzislau Rezki (Sony) ret = adjust_va_to_fit_type(root, head, va, nva_start_addr, size); 15321b23ff80SBaoquan He if (WARN_ON_ONCE(ret)) 153368ad4a33SUladzislau Rezki (Sony) return vend; 153468ad4a33SUladzislau Rezki (Sony) 1535a6cf4e0fSUladzislau Rezki (Sony) #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK 1536bd1264c3SSong Liu find_vmap_lowest_match_check(root, head, size, align); 1537a6cf4e0fSUladzislau Rezki (Sony) #endif 1538a6cf4e0fSUladzislau Rezki (Sony) 153968ad4a33SUladzislau Rezki (Sony) return nva_start_addr; 154068ad4a33SUladzislau Rezki (Sony) } 15414da56b99SChris Wilson 1542db64fe02SNick Piggin /* 1543d98c9e83SAndrey Ryabinin * Free a region of KVA allocated by alloc_vmap_area 1544d98c9e83SAndrey Ryabinin */ 1545d98c9e83SAndrey Ryabinin static void free_vmap_area(struct vmap_area *va) 1546d98c9e83SAndrey Ryabinin { 1547d98c9e83SAndrey Ryabinin /* 1548d98c9e83SAndrey Ryabinin * Remove from the busy tree/list. 1549d98c9e83SAndrey Ryabinin */ 1550d98c9e83SAndrey Ryabinin spin_lock(&vmap_area_lock); 1551d98c9e83SAndrey Ryabinin unlink_va(va, &vmap_area_root); 1552d98c9e83SAndrey Ryabinin spin_unlock(&vmap_area_lock); 1553d98c9e83SAndrey Ryabinin 1554d98c9e83SAndrey Ryabinin /* 1555d98c9e83SAndrey Ryabinin * Insert/Merge it back to the free tree/list. 1556d98c9e83SAndrey Ryabinin */ 1557d98c9e83SAndrey Ryabinin spin_lock(&free_vmap_area_lock); 155896e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list); 1559d98c9e83SAndrey Ryabinin spin_unlock(&free_vmap_area_lock); 1560d98c9e83SAndrey Ryabinin } 1561d98c9e83SAndrey Ryabinin 1562187f8cc4SUladzislau Rezki (Sony) static inline void 1563187f8cc4SUladzislau Rezki (Sony) preload_this_cpu_lock(spinlock_t *lock, gfp_t gfp_mask, int node) 1564187f8cc4SUladzislau Rezki (Sony) { 1565187f8cc4SUladzislau Rezki (Sony) struct vmap_area *va = NULL; 1566187f8cc4SUladzislau Rezki (Sony) 1567187f8cc4SUladzislau Rezki (Sony) /* 1568187f8cc4SUladzislau Rezki (Sony) * Preload this CPU with one extra vmap_area object. It is used 1569187f8cc4SUladzislau Rezki (Sony) * when fit type of free area is NE_FIT_TYPE. It guarantees that 1570187f8cc4SUladzislau Rezki (Sony) * a CPU that does an allocation is preloaded. 1571187f8cc4SUladzislau Rezki (Sony) * 1572187f8cc4SUladzislau Rezki (Sony) * We do it in non-atomic context, thus it allows us to use more 1573187f8cc4SUladzislau Rezki (Sony) * permissive allocation masks to be more stable under low memory 1574187f8cc4SUladzislau Rezki (Sony) * condition and high memory pressure. 1575187f8cc4SUladzislau Rezki (Sony) */ 1576187f8cc4SUladzislau Rezki (Sony) if (!this_cpu_read(ne_fit_preload_node)) 1577187f8cc4SUladzislau Rezki (Sony) va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); 1578187f8cc4SUladzislau Rezki (Sony) 1579187f8cc4SUladzislau Rezki (Sony) spin_lock(lock); 1580187f8cc4SUladzislau Rezki (Sony) 1581187f8cc4SUladzislau Rezki (Sony) if (va && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, va)) 1582187f8cc4SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 1583187f8cc4SUladzislau Rezki (Sony) } 1584187f8cc4SUladzislau Rezki (Sony) 1585d98c9e83SAndrey Ryabinin /* 1586db64fe02SNick Piggin * Allocate a region of KVA of the specified size and alignment, within the 1587db64fe02SNick Piggin * vstart and vend. 1588db64fe02SNick Piggin */ 1589db64fe02SNick Piggin static struct vmap_area *alloc_vmap_area(unsigned long size, 1590db64fe02SNick Piggin unsigned long align, 1591db64fe02SNick Piggin unsigned long vstart, unsigned long vend, 1592db64fe02SNick Piggin int node, gfp_t gfp_mask) 1593db64fe02SNick Piggin { 1594187f8cc4SUladzislau Rezki (Sony) struct vmap_area *va; 159512e376a6SUladzislau Rezki (Sony) unsigned long freed; 15961da177e4SLinus Torvalds unsigned long addr; 1597db64fe02SNick Piggin int purged = 0; 1598d98c9e83SAndrey Ryabinin int ret; 1599db64fe02SNick Piggin 16007766970cSNick Piggin BUG_ON(!size); 1601891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 160289699605SNick Piggin BUG_ON(!is_power_of_2(align)); 1603db64fe02SNick Piggin 160468ad4a33SUladzislau Rezki (Sony) if (unlikely(!vmap_initialized)) 160568ad4a33SUladzislau Rezki (Sony) return ERR_PTR(-EBUSY); 160668ad4a33SUladzislau Rezki (Sony) 16075803ed29SChristoph Hellwig might_sleep(); 1608f07116d7SUladzislau Rezki (Sony) gfp_mask = gfp_mask & GFP_RECLAIM_MASK; 16094da56b99SChris Wilson 1610f07116d7SUladzislau Rezki (Sony) va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); 1611db64fe02SNick Piggin if (unlikely(!va)) 1612db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 1613db64fe02SNick Piggin 16147f88f88fSCatalin Marinas /* 16157f88f88fSCatalin Marinas * Only scan the relevant parts containing pointers to other objects 16167f88f88fSCatalin Marinas * to avoid false negatives. 16177f88f88fSCatalin Marinas */ 1618f07116d7SUladzislau Rezki (Sony) kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask); 16197f88f88fSCatalin Marinas 1620db64fe02SNick Piggin retry: 1621187f8cc4SUladzislau Rezki (Sony) preload_this_cpu_lock(&free_vmap_area_lock, gfp_mask, node); 1622f9863be4SUladzislau Rezki (Sony) addr = __alloc_vmap_area(&free_vmap_area_root, &free_vmap_area_list, 1623f9863be4SUladzislau Rezki (Sony) size, align, vstart, vend); 1624187f8cc4SUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 162568ad4a33SUladzislau Rezki (Sony) 1626*cf243da6SUladzislau Rezki (Sony) trace_alloc_vmap_area(addr, size, align, vstart, vend, addr == vend); 1627*cf243da6SUladzislau Rezki (Sony) 162889699605SNick Piggin /* 162968ad4a33SUladzislau Rezki (Sony) * If an allocation fails, the "vend" address is 163068ad4a33SUladzislau Rezki (Sony) * returned. Therefore trigger the overflow path. 163189699605SNick Piggin */ 163268ad4a33SUladzislau Rezki (Sony) if (unlikely(addr == vend)) 163389699605SNick Piggin goto overflow; 163489699605SNick Piggin 163589699605SNick Piggin va->va_start = addr; 163689699605SNick Piggin va->va_end = addr + size; 1637688fcbfcSPengfei Li va->vm = NULL; 163868ad4a33SUladzislau Rezki (Sony) 1639e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 1640e36176beSUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 164189699605SNick Piggin spin_unlock(&vmap_area_lock); 164289699605SNick Piggin 164361e16557SWang Xiaoqiang BUG_ON(!IS_ALIGNED(va->va_start, align)); 164489699605SNick Piggin BUG_ON(va->va_start < vstart); 164589699605SNick Piggin BUG_ON(va->va_end > vend); 164689699605SNick Piggin 1647d98c9e83SAndrey Ryabinin ret = kasan_populate_vmalloc(addr, size); 1648d98c9e83SAndrey Ryabinin if (ret) { 1649d98c9e83SAndrey Ryabinin free_vmap_area(va); 1650d98c9e83SAndrey Ryabinin return ERR_PTR(ret); 1651d98c9e83SAndrey Ryabinin } 1652d98c9e83SAndrey Ryabinin 165389699605SNick Piggin return va; 165489699605SNick Piggin 16557766970cSNick Piggin overflow: 1656db64fe02SNick Piggin if (!purged) { 1657db64fe02SNick Piggin purge_vmap_area_lazy(); 1658db64fe02SNick Piggin purged = 1; 1659db64fe02SNick Piggin goto retry; 1660db64fe02SNick Piggin } 16614da56b99SChris Wilson 166212e376a6SUladzislau Rezki (Sony) freed = 0; 16634da56b99SChris Wilson blocking_notifier_call_chain(&vmap_notify_list, 0, &freed); 166412e376a6SUladzislau Rezki (Sony) 16654da56b99SChris Wilson if (freed > 0) { 16664da56b99SChris Wilson purged = 0; 16674da56b99SChris Wilson goto retry; 16684da56b99SChris Wilson } 16694da56b99SChris Wilson 167003497d76SFlorian Fainelli if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) 1671756a025fSJoe Perches pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n", 1672756a025fSJoe Perches size); 167368ad4a33SUladzislau Rezki (Sony) 167468ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, va); 1675db64fe02SNick Piggin return ERR_PTR(-EBUSY); 1676db64fe02SNick Piggin } 1677db64fe02SNick Piggin 16784da56b99SChris Wilson int register_vmap_purge_notifier(struct notifier_block *nb) 16794da56b99SChris Wilson { 16804da56b99SChris Wilson return blocking_notifier_chain_register(&vmap_notify_list, nb); 16814da56b99SChris Wilson } 16824da56b99SChris Wilson EXPORT_SYMBOL_GPL(register_vmap_purge_notifier); 16834da56b99SChris Wilson 16844da56b99SChris Wilson int unregister_vmap_purge_notifier(struct notifier_block *nb) 16854da56b99SChris Wilson { 16864da56b99SChris Wilson return blocking_notifier_chain_unregister(&vmap_notify_list, nb); 16874da56b99SChris Wilson } 16884da56b99SChris Wilson EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier); 16894da56b99SChris Wilson 1690db64fe02SNick Piggin /* 1691db64fe02SNick Piggin * lazy_max_pages is the maximum amount of virtual address space we gather up 1692db64fe02SNick Piggin * before attempting to purge with a TLB flush. 1693db64fe02SNick Piggin * 1694db64fe02SNick Piggin * There is a tradeoff here: a larger number will cover more kernel page tables 1695db64fe02SNick Piggin * and take slightly longer to purge, but it will linearly reduce the number of 1696db64fe02SNick Piggin * global TLB flushes that must be performed. It would seem natural to scale 1697db64fe02SNick Piggin * this number up linearly with the number of CPUs (because vmapping activity 1698db64fe02SNick Piggin * could also scale linearly with the number of CPUs), however it is likely 1699db64fe02SNick Piggin * that in practice, workloads might be constrained in other ways that mean 1700db64fe02SNick Piggin * vmap activity will not scale linearly with CPUs. Also, I want to be 1701db64fe02SNick Piggin * conservative and not introduce a big latency on huge systems, so go with 1702db64fe02SNick Piggin * a less aggressive log scale. It will still be an improvement over the old 1703db64fe02SNick Piggin * code, and it will be simple to change the scale factor if we find that it 1704db64fe02SNick Piggin * becomes a problem on bigger systems. 1705db64fe02SNick Piggin */ 1706db64fe02SNick Piggin static unsigned long lazy_max_pages(void) 1707db64fe02SNick Piggin { 1708db64fe02SNick Piggin unsigned int log; 1709db64fe02SNick Piggin 1710db64fe02SNick Piggin log = fls(num_online_cpus()); 1711db64fe02SNick Piggin 1712db64fe02SNick Piggin return log * (32UL * 1024 * 1024 / PAGE_SIZE); 1713db64fe02SNick Piggin } 1714db64fe02SNick Piggin 17154d36e6f8SUladzislau Rezki (Sony) static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0); 1716db64fe02SNick Piggin 17170574ecd1SChristoph Hellwig /* 1718f0953a1bSIngo Molnar * Serialize vmap purging. There is no actual critical section protected 1719153090f2SBaoquan He * by this lock, but we want to avoid concurrent calls for performance 17200574ecd1SChristoph Hellwig * reasons and to make the pcpu_get_vm_areas more deterministic. 17210574ecd1SChristoph Hellwig */ 1722f9e09977SChristoph Hellwig static DEFINE_MUTEX(vmap_purge_lock); 17230574ecd1SChristoph Hellwig 172402b709dfSNick Piggin /* for per-CPU blocks */ 172502b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void); 172602b709dfSNick Piggin 17273ee48b6aSCliff Wickman /* 1728db64fe02SNick Piggin * Purges all lazily-freed vmap areas. 1729db64fe02SNick Piggin */ 17300574ecd1SChristoph Hellwig static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end) 1731db64fe02SNick Piggin { 17324d36e6f8SUladzislau Rezki (Sony) unsigned long resched_threshold; 1733baa468a6SBaoquan He struct list_head local_purge_list; 173496e2db45SUladzislau Rezki (Sony) struct vmap_area *va, *n_va; 1735db64fe02SNick Piggin 17360574ecd1SChristoph Hellwig lockdep_assert_held(&vmap_purge_lock); 173702b709dfSNick Piggin 173896e2db45SUladzislau Rezki (Sony) spin_lock(&purge_vmap_area_lock); 173996e2db45SUladzislau Rezki (Sony) purge_vmap_area_root = RB_ROOT; 1740baa468a6SBaoquan He list_replace_init(&purge_vmap_area_list, &local_purge_list); 174196e2db45SUladzislau Rezki (Sony) spin_unlock(&purge_vmap_area_lock); 174296e2db45SUladzislau Rezki (Sony) 1743baa468a6SBaoquan He if (unlikely(list_empty(&local_purge_list))) 174468571be9SUladzislau Rezki (Sony) return false; 174568571be9SUladzislau Rezki (Sony) 174696e2db45SUladzislau Rezki (Sony) start = min(start, 1747baa468a6SBaoquan He list_first_entry(&local_purge_list, 174896e2db45SUladzislau Rezki (Sony) struct vmap_area, list)->va_start); 174996e2db45SUladzislau Rezki (Sony) 175096e2db45SUladzislau Rezki (Sony) end = max(end, 1751baa468a6SBaoquan He list_last_entry(&local_purge_list, 175296e2db45SUladzislau Rezki (Sony) struct vmap_area, list)->va_end); 1753db64fe02SNick Piggin 17540574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 17554d36e6f8SUladzislau Rezki (Sony) resched_threshold = lazy_max_pages() << 1; 1756db64fe02SNick Piggin 1757e36176beSUladzislau Rezki (Sony) spin_lock(&free_vmap_area_lock); 1758baa468a6SBaoquan He list_for_each_entry_safe(va, n_va, &local_purge_list, list) { 17594d36e6f8SUladzislau Rezki (Sony) unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT; 17603c5c3cfbSDaniel Axtens unsigned long orig_start = va->va_start; 17613c5c3cfbSDaniel Axtens unsigned long orig_end = va->va_end; 1762763b218dSJoel Fernandes 1763dd3b8353SUladzislau Rezki (Sony) /* 1764dd3b8353SUladzislau Rezki (Sony) * Finally insert or merge lazily-freed area. It is 1765dd3b8353SUladzislau Rezki (Sony) * detached and there is no need to "unlink" it from 1766dd3b8353SUladzislau Rezki (Sony) * anything. 1767dd3b8353SUladzislau Rezki (Sony) */ 176896e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root, 17693c5c3cfbSDaniel Axtens &free_vmap_area_list); 17703c5c3cfbSDaniel Axtens 17719c801f61SUladzislau Rezki (Sony) if (!va) 17729c801f61SUladzislau Rezki (Sony) continue; 17739c801f61SUladzislau Rezki (Sony) 17743c5c3cfbSDaniel Axtens if (is_vmalloc_or_module_addr((void *)orig_start)) 17753c5c3cfbSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 17763c5c3cfbSDaniel Axtens va->va_start, va->va_end); 1777dd3b8353SUladzislau Rezki (Sony) 17784d36e6f8SUladzislau Rezki (Sony) atomic_long_sub(nr, &vmap_lazy_nr); 177968571be9SUladzislau Rezki (Sony) 17804d36e6f8SUladzislau Rezki (Sony) if (atomic_long_read(&vmap_lazy_nr) < resched_threshold) 1781e36176beSUladzislau Rezki (Sony) cond_resched_lock(&free_vmap_area_lock); 1782763b218dSJoel Fernandes } 1783e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 17840574ecd1SChristoph Hellwig return true; 1785db64fe02SNick Piggin } 1786db64fe02SNick Piggin 1787db64fe02SNick Piggin /* 1788db64fe02SNick Piggin * Kick off a purge of the outstanding lazy areas. 1789db64fe02SNick Piggin */ 1790db64fe02SNick Piggin static void purge_vmap_area_lazy(void) 1791db64fe02SNick Piggin { 1792f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 17930574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 17940574ecd1SChristoph Hellwig __purge_vmap_area_lazy(ULONG_MAX, 0); 1795f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 1796db64fe02SNick Piggin } 1797db64fe02SNick Piggin 1798690467c8SUladzislau Rezki (Sony) static void drain_vmap_area_work(struct work_struct *work) 1799690467c8SUladzislau Rezki (Sony) { 1800690467c8SUladzislau Rezki (Sony) unsigned long nr_lazy; 1801690467c8SUladzislau Rezki (Sony) 1802690467c8SUladzislau Rezki (Sony) do { 1803690467c8SUladzislau Rezki (Sony) mutex_lock(&vmap_purge_lock); 1804690467c8SUladzislau Rezki (Sony) __purge_vmap_area_lazy(ULONG_MAX, 0); 1805690467c8SUladzislau Rezki (Sony) mutex_unlock(&vmap_purge_lock); 1806690467c8SUladzislau Rezki (Sony) 1807690467c8SUladzislau Rezki (Sony) /* Recheck if further work is required. */ 1808690467c8SUladzislau Rezki (Sony) nr_lazy = atomic_long_read(&vmap_lazy_nr); 1809690467c8SUladzislau Rezki (Sony) } while (nr_lazy > lazy_max_pages()); 1810690467c8SUladzislau Rezki (Sony) } 1811690467c8SUladzislau Rezki (Sony) 1812db64fe02SNick Piggin /* 181364141da5SJeremy Fitzhardinge * Free a vmap area, caller ensuring that the area has been unmapped 181464141da5SJeremy Fitzhardinge * and flush_cache_vunmap had been called for the correct range 181564141da5SJeremy Fitzhardinge * previously. 1816db64fe02SNick Piggin */ 181764141da5SJeremy Fitzhardinge static void free_vmap_area_noflush(struct vmap_area *va) 1818db64fe02SNick Piggin { 18194d36e6f8SUladzislau Rezki (Sony) unsigned long nr_lazy; 182080c4bd7aSChris Wilson 1821dd3b8353SUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 1822dd3b8353SUladzislau Rezki (Sony) unlink_va(va, &vmap_area_root); 1823dd3b8353SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 1824dd3b8353SUladzislau Rezki (Sony) 18254d36e6f8SUladzislau Rezki (Sony) nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >> 18264d36e6f8SUladzislau Rezki (Sony) PAGE_SHIFT, &vmap_lazy_nr); 182780c4bd7aSChris Wilson 182896e2db45SUladzislau Rezki (Sony) /* 182996e2db45SUladzislau Rezki (Sony) * Merge or place it to the purge tree/list. 183096e2db45SUladzislau Rezki (Sony) */ 183196e2db45SUladzislau Rezki (Sony) spin_lock(&purge_vmap_area_lock); 183296e2db45SUladzislau Rezki (Sony) merge_or_add_vmap_area(va, 183396e2db45SUladzislau Rezki (Sony) &purge_vmap_area_root, &purge_vmap_area_list); 183496e2db45SUladzislau Rezki (Sony) spin_unlock(&purge_vmap_area_lock); 183580c4bd7aSChris Wilson 183696e2db45SUladzislau Rezki (Sony) /* After this point, we may free va at any time */ 183780c4bd7aSChris Wilson if (unlikely(nr_lazy > lazy_max_pages())) 1838690467c8SUladzislau Rezki (Sony) schedule_work(&drain_vmap_work); 1839db64fe02SNick Piggin } 1840db64fe02SNick Piggin 1841b29acbdcSNick Piggin /* 1842b29acbdcSNick Piggin * Free and unmap a vmap area 1843b29acbdcSNick Piggin */ 1844b29acbdcSNick Piggin static void free_unmap_vmap_area(struct vmap_area *va) 1845b29acbdcSNick Piggin { 1846b29acbdcSNick Piggin flush_cache_vunmap(va->va_start, va->va_end); 18474ad0ae8cSNicholas Piggin vunmap_range_noflush(va->va_start, va->va_end); 18488e57f8acSVlastimil Babka if (debug_pagealloc_enabled_static()) 184982a2e924SChintan Pandya flush_tlb_kernel_range(va->va_start, va->va_end); 185082a2e924SChintan Pandya 1851c8eef01eSChristoph Hellwig free_vmap_area_noflush(va); 1852b29acbdcSNick Piggin } 1853b29acbdcSNick Piggin 1854993d0b28SMatthew Wilcox (Oracle) struct vmap_area *find_vmap_area(unsigned long addr) 1855db64fe02SNick Piggin { 1856db64fe02SNick Piggin struct vmap_area *va; 1857db64fe02SNick Piggin 1858db64fe02SNick Piggin spin_lock(&vmap_area_lock); 1859899c6efeSUladzislau Rezki (Sony) va = __find_vmap_area(addr, &vmap_area_root); 1860db64fe02SNick Piggin spin_unlock(&vmap_area_lock); 1861db64fe02SNick Piggin 1862db64fe02SNick Piggin return va; 1863db64fe02SNick Piggin } 1864db64fe02SNick Piggin 1865db64fe02SNick Piggin /*** Per cpu kva allocator ***/ 1866db64fe02SNick Piggin 1867db64fe02SNick Piggin /* 1868db64fe02SNick Piggin * vmap space is limited especially on 32 bit architectures. Ensure there is 1869db64fe02SNick Piggin * room for at least 16 percpu vmap blocks per CPU. 1870db64fe02SNick Piggin */ 1871db64fe02SNick Piggin /* 1872db64fe02SNick Piggin * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able 1873db64fe02SNick Piggin * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess 1874db64fe02SNick Piggin * instead (we just need a rough idea) 1875db64fe02SNick Piggin */ 1876db64fe02SNick Piggin #if BITS_PER_LONG == 32 1877db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024) 1878db64fe02SNick Piggin #else 1879db64fe02SNick Piggin #define VMALLOC_SPACE (128UL*1024*1024*1024) 1880db64fe02SNick Piggin #endif 1881db64fe02SNick Piggin 1882db64fe02SNick Piggin #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) 1883db64fe02SNick Piggin #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ 1884db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ 1885db64fe02SNick Piggin #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) 1886db64fe02SNick Piggin #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ 1887db64fe02SNick Piggin #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ 1888f982f915SClemens Ladisch #define VMAP_BBMAP_BITS \ 1889f982f915SClemens Ladisch VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ 1890db64fe02SNick Piggin VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ 1891f982f915SClemens Ladisch VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16)) 1892db64fe02SNick Piggin 1893db64fe02SNick Piggin #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) 1894db64fe02SNick Piggin 1895db64fe02SNick Piggin struct vmap_block_queue { 1896db64fe02SNick Piggin spinlock_t lock; 1897db64fe02SNick Piggin struct list_head free; 1898db64fe02SNick Piggin }; 1899db64fe02SNick Piggin 1900db64fe02SNick Piggin struct vmap_block { 1901db64fe02SNick Piggin spinlock_t lock; 1902db64fe02SNick Piggin struct vmap_area *va; 1903db64fe02SNick Piggin unsigned long free, dirty; 19047d61bfe8SRoman Pen unsigned long dirty_min, dirty_max; /*< dirty range */ 1905db64fe02SNick Piggin struct list_head free_list; 1906db64fe02SNick Piggin struct rcu_head rcu_head; 190702b709dfSNick Piggin struct list_head purge; 1908db64fe02SNick Piggin }; 1909db64fe02SNick Piggin 1910db64fe02SNick Piggin /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ 1911db64fe02SNick Piggin static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); 1912db64fe02SNick Piggin 1913db64fe02SNick Piggin /* 19140f14599cSMatthew Wilcox (Oracle) * XArray of vmap blocks, indexed by address, to quickly find a vmap block 1915db64fe02SNick Piggin * in the free path. Could get rid of this if we change the API to return a 1916db64fe02SNick Piggin * "cookie" from alloc, to be passed to free. But no big deal yet. 1917db64fe02SNick Piggin */ 19180f14599cSMatthew Wilcox (Oracle) static DEFINE_XARRAY(vmap_blocks); 1919db64fe02SNick Piggin 1920db64fe02SNick Piggin /* 1921db64fe02SNick Piggin * We should probably have a fallback mechanism to allocate virtual memory 1922db64fe02SNick Piggin * out of partially filled vmap blocks. However vmap block sizing should be 1923db64fe02SNick Piggin * fairly reasonable according to the vmalloc size, so it shouldn't be a 1924db64fe02SNick Piggin * big problem. 1925db64fe02SNick Piggin */ 1926db64fe02SNick Piggin 1927db64fe02SNick Piggin static unsigned long addr_to_vb_idx(unsigned long addr) 1928db64fe02SNick Piggin { 1929db64fe02SNick Piggin addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); 1930db64fe02SNick Piggin addr /= VMAP_BLOCK_SIZE; 1931db64fe02SNick Piggin return addr; 1932db64fe02SNick Piggin } 1933db64fe02SNick Piggin 1934cf725ce2SRoman Pen static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off) 1935cf725ce2SRoman Pen { 1936cf725ce2SRoman Pen unsigned long addr; 1937cf725ce2SRoman Pen 1938cf725ce2SRoman Pen addr = va_start + (pages_off << PAGE_SHIFT); 1939cf725ce2SRoman Pen BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start)); 1940cf725ce2SRoman Pen return (void *)addr; 1941cf725ce2SRoman Pen } 1942cf725ce2SRoman Pen 1943cf725ce2SRoman Pen /** 1944cf725ce2SRoman Pen * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this 1945cf725ce2SRoman Pen * block. Of course pages number can't exceed VMAP_BBMAP_BITS 1946cf725ce2SRoman Pen * @order: how many 2^order pages should be occupied in newly allocated block 1947cf725ce2SRoman Pen * @gfp_mask: flags for the page level allocator 1948cf725ce2SRoman Pen * 1949a862f68aSMike Rapoport * Return: virtual address in a newly allocated block or ERR_PTR(-errno) 1950cf725ce2SRoman Pen */ 1951cf725ce2SRoman Pen static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) 1952db64fe02SNick Piggin { 1953db64fe02SNick Piggin struct vmap_block_queue *vbq; 1954db64fe02SNick Piggin struct vmap_block *vb; 1955db64fe02SNick Piggin struct vmap_area *va; 1956db64fe02SNick Piggin unsigned long vb_idx; 1957db64fe02SNick Piggin int node, err; 1958cf725ce2SRoman Pen void *vaddr; 1959db64fe02SNick Piggin 1960db64fe02SNick Piggin node = numa_node_id(); 1961db64fe02SNick Piggin 1962db64fe02SNick Piggin vb = kmalloc_node(sizeof(struct vmap_block), 1963db64fe02SNick Piggin gfp_mask & GFP_RECLAIM_MASK, node); 1964db64fe02SNick Piggin if (unlikely(!vb)) 1965db64fe02SNick Piggin return ERR_PTR(-ENOMEM); 1966db64fe02SNick Piggin 1967db64fe02SNick Piggin va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, 1968db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, 1969db64fe02SNick Piggin node, gfp_mask); 1970ddf9c6d4STobias Klauser if (IS_ERR(va)) { 1971db64fe02SNick Piggin kfree(vb); 1972e7d86340SJulia Lawall return ERR_CAST(va); 1973db64fe02SNick Piggin } 1974db64fe02SNick Piggin 1975cf725ce2SRoman Pen vaddr = vmap_block_vaddr(va->va_start, 0); 1976db64fe02SNick Piggin spin_lock_init(&vb->lock); 1977db64fe02SNick Piggin vb->va = va; 1978cf725ce2SRoman Pen /* At least something should be left free */ 1979cf725ce2SRoman Pen BUG_ON(VMAP_BBMAP_BITS <= (1UL << order)); 1980cf725ce2SRoman Pen vb->free = VMAP_BBMAP_BITS - (1UL << order); 1981db64fe02SNick Piggin vb->dirty = 0; 19827d61bfe8SRoman Pen vb->dirty_min = VMAP_BBMAP_BITS; 19837d61bfe8SRoman Pen vb->dirty_max = 0; 1984db64fe02SNick Piggin INIT_LIST_HEAD(&vb->free_list); 1985db64fe02SNick Piggin 1986db64fe02SNick Piggin vb_idx = addr_to_vb_idx(va->va_start); 19870f14599cSMatthew Wilcox (Oracle) err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask); 19880f14599cSMatthew Wilcox (Oracle) if (err) { 19890f14599cSMatthew Wilcox (Oracle) kfree(vb); 19900f14599cSMatthew Wilcox (Oracle) free_vmap_area(va); 19910f14599cSMatthew Wilcox (Oracle) return ERR_PTR(err); 19920f14599cSMatthew Wilcox (Oracle) } 1993db64fe02SNick Piggin 19943f804920SSebastian Andrzej Siewior vbq = raw_cpu_ptr(&vmap_block_queue); 1995db64fe02SNick Piggin spin_lock(&vbq->lock); 199668ac546fSRoman Pen list_add_tail_rcu(&vb->free_list, &vbq->free); 1997db64fe02SNick Piggin spin_unlock(&vbq->lock); 1998db64fe02SNick Piggin 1999cf725ce2SRoman Pen return vaddr; 2000db64fe02SNick Piggin } 2001db64fe02SNick Piggin 2002db64fe02SNick Piggin static void free_vmap_block(struct vmap_block *vb) 2003db64fe02SNick Piggin { 2004db64fe02SNick Piggin struct vmap_block *tmp; 2005db64fe02SNick Piggin 20060f14599cSMatthew Wilcox (Oracle) tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start)); 2007db64fe02SNick Piggin BUG_ON(tmp != vb); 2008db64fe02SNick Piggin 200964141da5SJeremy Fitzhardinge free_vmap_area_noflush(vb->va); 201022a3c7d1SLai Jiangshan kfree_rcu(vb, rcu_head); 2011db64fe02SNick Piggin } 2012db64fe02SNick Piggin 201302b709dfSNick Piggin static void purge_fragmented_blocks(int cpu) 201402b709dfSNick Piggin { 201502b709dfSNick Piggin LIST_HEAD(purge); 201602b709dfSNick Piggin struct vmap_block *vb; 201702b709dfSNick Piggin struct vmap_block *n_vb; 201802b709dfSNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 201902b709dfSNick Piggin 202002b709dfSNick Piggin rcu_read_lock(); 202102b709dfSNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 202202b709dfSNick Piggin 202302b709dfSNick Piggin if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS)) 202402b709dfSNick Piggin continue; 202502b709dfSNick Piggin 202602b709dfSNick Piggin spin_lock(&vb->lock); 202702b709dfSNick Piggin if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) { 202802b709dfSNick Piggin vb->free = 0; /* prevent further allocs after releasing lock */ 202902b709dfSNick Piggin vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */ 20307d61bfe8SRoman Pen vb->dirty_min = 0; 20317d61bfe8SRoman Pen vb->dirty_max = VMAP_BBMAP_BITS; 203202b709dfSNick Piggin spin_lock(&vbq->lock); 203302b709dfSNick Piggin list_del_rcu(&vb->free_list); 203402b709dfSNick Piggin spin_unlock(&vbq->lock); 203502b709dfSNick Piggin spin_unlock(&vb->lock); 203602b709dfSNick Piggin list_add_tail(&vb->purge, &purge); 203702b709dfSNick Piggin } else 203802b709dfSNick Piggin spin_unlock(&vb->lock); 203902b709dfSNick Piggin } 204002b709dfSNick Piggin rcu_read_unlock(); 204102b709dfSNick Piggin 204202b709dfSNick Piggin list_for_each_entry_safe(vb, n_vb, &purge, purge) { 204302b709dfSNick Piggin list_del(&vb->purge); 204402b709dfSNick Piggin free_vmap_block(vb); 204502b709dfSNick Piggin } 204602b709dfSNick Piggin } 204702b709dfSNick Piggin 204802b709dfSNick Piggin static void purge_fragmented_blocks_allcpus(void) 204902b709dfSNick Piggin { 205002b709dfSNick Piggin int cpu; 205102b709dfSNick Piggin 205202b709dfSNick Piggin for_each_possible_cpu(cpu) 205302b709dfSNick Piggin purge_fragmented_blocks(cpu); 205402b709dfSNick Piggin } 205502b709dfSNick Piggin 2056db64fe02SNick Piggin static void *vb_alloc(unsigned long size, gfp_t gfp_mask) 2057db64fe02SNick Piggin { 2058db64fe02SNick Piggin struct vmap_block_queue *vbq; 2059db64fe02SNick Piggin struct vmap_block *vb; 2060cf725ce2SRoman Pen void *vaddr = NULL; 2061db64fe02SNick Piggin unsigned int order; 2062db64fe02SNick Piggin 2063891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 2064db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 2065aa91c4d8SJan Kara if (WARN_ON(size == 0)) { 2066aa91c4d8SJan Kara /* 2067aa91c4d8SJan Kara * Allocating 0 bytes isn't what caller wants since 2068aa91c4d8SJan Kara * get_order(0) returns funny result. Just warn and terminate 2069aa91c4d8SJan Kara * early. 2070aa91c4d8SJan Kara */ 2071aa91c4d8SJan Kara return NULL; 2072aa91c4d8SJan Kara } 2073db64fe02SNick Piggin order = get_order(size); 2074db64fe02SNick Piggin 2075db64fe02SNick Piggin rcu_read_lock(); 20763f804920SSebastian Andrzej Siewior vbq = raw_cpu_ptr(&vmap_block_queue); 2077db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 2078cf725ce2SRoman Pen unsigned long pages_off; 2079db64fe02SNick Piggin 2080db64fe02SNick Piggin spin_lock(&vb->lock); 2081cf725ce2SRoman Pen if (vb->free < (1UL << order)) { 2082cf725ce2SRoman Pen spin_unlock(&vb->lock); 2083cf725ce2SRoman Pen continue; 2084cf725ce2SRoman Pen } 208502b709dfSNick Piggin 2086cf725ce2SRoman Pen pages_off = VMAP_BBMAP_BITS - vb->free; 2087cf725ce2SRoman Pen vaddr = vmap_block_vaddr(vb->va->va_start, pages_off); 2088db64fe02SNick Piggin vb->free -= 1UL << order; 2089db64fe02SNick Piggin if (vb->free == 0) { 2090db64fe02SNick Piggin spin_lock(&vbq->lock); 2091de560423SNick Piggin list_del_rcu(&vb->free_list); 2092db64fe02SNick Piggin spin_unlock(&vbq->lock); 2093db64fe02SNick Piggin } 2094cf725ce2SRoman Pen 2095db64fe02SNick Piggin spin_unlock(&vb->lock); 2096db64fe02SNick Piggin break; 2097db64fe02SNick Piggin } 209802b709dfSNick Piggin 2099db64fe02SNick Piggin rcu_read_unlock(); 2100db64fe02SNick Piggin 2101cf725ce2SRoman Pen /* Allocate new block if nothing was found */ 2102cf725ce2SRoman Pen if (!vaddr) 2103cf725ce2SRoman Pen vaddr = new_vmap_block(order, gfp_mask); 2104db64fe02SNick Piggin 2105cf725ce2SRoman Pen return vaddr; 2106db64fe02SNick Piggin } 2107db64fe02SNick Piggin 210878a0e8c4SChristoph Hellwig static void vb_free(unsigned long addr, unsigned long size) 2109db64fe02SNick Piggin { 2110db64fe02SNick Piggin unsigned long offset; 2111db64fe02SNick Piggin unsigned int order; 2112db64fe02SNick Piggin struct vmap_block *vb; 2113db64fe02SNick Piggin 2114891c49abSAlexander Kuleshov BUG_ON(offset_in_page(size)); 2115db64fe02SNick Piggin BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); 2116b29acbdcSNick Piggin 211778a0e8c4SChristoph Hellwig flush_cache_vunmap(addr, addr + size); 2118b29acbdcSNick Piggin 2119db64fe02SNick Piggin order = get_order(size); 212078a0e8c4SChristoph Hellwig offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT; 21210f14599cSMatthew Wilcox (Oracle) vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr)); 2122db64fe02SNick Piggin 21234ad0ae8cSNicholas Piggin vunmap_range_noflush(addr, addr + size); 212464141da5SJeremy Fitzhardinge 21258e57f8acSVlastimil Babka if (debug_pagealloc_enabled_static()) 212678a0e8c4SChristoph Hellwig flush_tlb_kernel_range(addr, addr + size); 212782a2e924SChintan Pandya 2128db64fe02SNick Piggin spin_lock(&vb->lock); 21297d61bfe8SRoman Pen 21307d61bfe8SRoman Pen /* Expand dirty range */ 21317d61bfe8SRoman Pen vb->dirty_min = min(vb->dirty_min, offset); 21327d61bfe8SRoman Pen vb->dirty_max = max(vb->dirty_max, offset + (1UL << order)); 2133d086817dSMinChan Kim 2134db64fe02SNick Piggin vb->dirty += 1UL << order; 2135db64fe02SNick Piggin if (vb->dirty == VMAP_BBMAP_BITS) { 2136de560423SNick Piggin BUG_ON(vb->free); 2137db64fe02SNick Piggin spin_unlock(&vb->lock); 2138db64fe02SNick Piggin free_vmap_block(vb); 2139db64fe02SNick Piggin } else 2140db64fe02SNick Piggin spin_unlock(&vb->lock); 2141db64fe02SNick Piggin } 2142db64fe02SNick Piggin 2143868b104dSRick Edgecombe static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush) 2144db64fe02SNick Piggin { 2145db64fe02SNick Piggin int cpu; 2146db64fe02SNick Piggin 21479b463334SJeremy Fitzhardinge if (unlikely(!vmap_initialized)) 21489b463334SJeremy Fitzhardinge return; 21499b463334SJeremy Fitzhardinge 21505803ed29SChristoph Hellwig might_sleep(); 21515803ed29SChristoph Hellwig 2152db64fe02SNick Piggin for_each_possible_cpu(cpu) { 2153db64fe02SNick Piggin struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); 2154db64fe02SNick Piggin struct vmap_block *vb; 2155db64fe02SNick Piggin 2156db64fe02SNick Piggin rcu_read_lock(); 2157db64fe02SNick Piggin list_for_each_entry_rcu(vb, &vbq->free, free_list) { 2158db64fe02SNick Piggin spin_lock(&vb->lock); 2159ad216c03SVijayanand Jitta if (vb->dirty && vb->dirty != VMAP_BBMAP_BITS) { 21607d61bfe8SRoman Pen unsigned long va_start = vb->va->va_start; 2161db64fe02SNick Piggin unsigned long s, e; 2162b136be5eSJoonsoo Kim 21637d61bfe8SRoman Pen s = va_start + (vb->dirty_min << PAGE_SHIFT); 21647d61bfe8SRoman Pen e = va_start + (vb->dirty_max << PAGE_SHIFT); 2165db64fe02SNick Piggin 21667d61bfe8SRoman Pen start = min(s, start); 21677d61bfe8SRoman Pen end = max(e, end); 21687d61bfe8SRoman Pen 2169db64fe02SNick Piggin flush = 1; 2170db64fe02SNick Piggin } 2171db64fe02SNick Piggin spin_unlock(&vb->lock); 2172db64fe02SNick Piggin } 2173db64fe02SNick Piggin rcu_read_unlock(); 2174db64fe02SNick Piggin } 2175db64fe02SNick Piggin 2176f9e09977SChristoph Hellwig mutex_lock(&vmap_purge_lock); 21770574ecd1SChristoph Hellwig purge_fragmented_blocks_allcpus(); 21780574ecd1SChristoph Hellwig if (!__purge_vmap_area_lazy(start, end) && flush) 21790574ecd1SChristoph Hellwig flush_tlb_kernel_range(start, end); 2180f9e09977SChristoph Hellwig mutex_unlock(&vmap_purge_lock); 2181db64fe02SNick Piggin } 2182868b104dSRick Edgecombe 2183868b104dSRick Edgecombe /** 2184868b104dSRick Edgecombe * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer 2185868b104dSRick Edgecombe * 2186868b104dSRick Edgecombe * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily 2187868b104dSRick Edgecombe * to amortize TLB flushing overheads. What this means is that any page you 2188868b104dSRick Edgecombe * have now, may, in a former life, have been mapped into kernel virtual 2189868b104dSRick Edgecombe * address by the vmap layer and so there might be some CPUs with TLB entries 2190868b104dSRick Edgecombe * still referencing that page (additional to the regular 1:1 kernel mapping). 2191868b104dSRick Edgecombe * 2192868b104dSRick Edgecombe * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can 2193868b104dSRick Edgecombe * be sure that none of the pages we have control over will have any aliases 2194868b104dSRick Edgecombe * from the vmap layer. 2195868b104dSRick Edgecombe */ 2196868b104dSRick Edgecombe void vm_unmap_aliases(void) 2197868b104dSRick Edgecombe { 2198868b104dSRick Edgecombe unsigned long start = ULONG_MAX, end = 0; 2199868b104dSRick Edgecombe int flush = 0; 2200868b104dSRick Edgecombe 2201868b104dSRick Edgecombe _vm_unmap_aliases(start, end, flush); 2202868b104dSRick Edgecombe } 2203db64fe02SNick Piggin EXPORT_SYMBOL_GPL(vm_unmap_aliases); 2204db64fe02SNick Piggin 2205db64fe02SNick Piggin /** 2206db64fe02SNick Piggin * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram 2207db64fe02SNick Piggin * @mem: the pointer returned by vm_map_ram 2208db64fe02SNick Piggin * @count: the count passed to that vm_map_ram call (cannot unmap partial) 2209db64fe02SNick Piggin */ 2210db64fe02SNick Piggin void vm_unmap_ram(const void *mem, unsigned int count) 2211db64fe02SNick Piggin { 221265ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 22134aff1dc4SAndrey Konovalov unsigned long addr = (unsigned long)kasan_reset_tag(mem); 22149c3acf60SChristoph Hellwig struct vmap_area *va; 2215db64fe02SNick Piggin 22165803ed29SChristoph Hellwig might_sleep(); 2217db64fe02SNick Piggin BUG_ON(!addr); 2218db64fe02SNick Piggin BUG_ON(addr < VMALLOC_START); 2219db64fe02SNick Piggin BUG_ON(addr > VMALLOC_END); 2220a1c0b1a0SShawn Lin BUG_ON(!PAGE_ALIGNED(addr)); 2221db64fe02SNick Piggin 2222d98c9e83SAndrey Ryabinin kasan_poison_vmalloc(mem, size); 2223d98c9e83SAndrey Ryabinin 22249c3acf60SChristoph Hellwig if (likely(count <= VMAP_MAX_ALLOC)) { 222505e3ff95SChintan Pandya debug_check_no_locks_freed(mem, size); 222678a0e8c4SChristoph Hellwig vb_free(addr, size); 22279c3acf60SChristoph Hellwig return; 22289c3acf60SChristoph Hellwig } 22299c3acf60SChristoph Hellwig 22309c3acf60SChristoph Hellwig va = find_vmap_area(addr); 22319c3acf60SChristoph Hellwig BUG_ON(!va); 223205e3ff95SChintan Pandya debug_check_no_locks_freed((void *)va->va_start, 223305e3ff95SChintan Pandya (va->va_end - va->va_start)); 22349c3acf60SChristoph Hellwig free_unmap_vmap_area(va); 2235db64fe02SNick Piggin } 2236db64fe02SNick Piggin EXPORT_SYMBOL(vm_unmap_ram); 2237db64fe02SNick Piggin 2238db64fe02SNick Piggin /** 2239db64fe02SNick Piggin * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) 2240db64fe02SNick Piggin * @pages: an array of pointers to the pages to be mapped 2241db64fe02SNick Piggin * @count: number of pages 2242db64fe02SNick Piggin * @node: prefer to allocate data structures on this node 2243e99c97adSRandy Dunlap * 224436437638SGioh Kim * If you use this function for less than VMAP_MAX_ALLOC pages, it could be 224536437638SGioh Kim * faster than vmap so it's good. But if you mix long-life and short-life 224636437638SGioh Kim * objects with vm_map_ram(), it could consume lots of address space through 224736437638SGioh Kim * fragmentation (especially on a 32bit machine). You could see failures in 224836437638SGioh Kim * the end. Please use this function for short-lived objects. 224936437638SGioh Kim * 2250e99c97adSRandy Dunlap * Returns: a pointer to the address that has been mapped, or %NULL on failure 2251db64fe02SNick Piggin */ 2252d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node) 2253db64fe02SNick Piggin { 225465ee03c4SGuillermo Julián Moreno unsigned long size = (unsigned long)count << PAGE_SHIFT; 2255db64fe02SNick Piggin unsigned long addr; 2256db64fe02SNick Piggin void *mem; 2257db64fe02SNick Piggin 2258db64fe02SNick Piggin if (likely(count <= VMAP_MAX_ALLOC)) { 2259db64fe02SNick Piggin mem = vb_alloc(size, GFP_KERNEL); 2260db64fe02SNick Piggin if (IS_ERR(mem)) 2261db64fe02SNick Piggin return NULL; 2262db64fe02SNick Piggin addr = (unsigned long)mem; 2263db64fe02SNick Piggin } else { 2264db64fe02SNick Piggin struct vmap_area *va; 2265db64fe02SNick Piggin va = alloc_vmap_area(size, PAGE_SIZE, 2266db64fe02SNick Piggin VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); 2267db64fe02SNick Piggin if (IS_ERR(va)) 2268db64fe02SNick Piggin return NULL; 2269db64fe02SNick Piggin 2270db64fe02SNick Piggin addr = va->va_start; 2271db64fe02SNick Piggin mem = (void *)addr; 2272db64fe02SNick Piggin } 2273d98c9e83SAndrey Ryabinin 2274b67177ecSNicholas Piggin if (vmap_pages_range(addr, addr + size, PAGE_KERNEL, 2275b67177ecSNicholas Piggin pages, PAGE_SHIFT) < 0) { 2276db64fe02SNick Piggin vm_unmap_ram(mem, count); 2277db64fe02SNick Piggin return NULL; 2278db64fe02SNick Piggin } 2279b67177ecSNicholas Piggin 228023689e91SAndrey Konovalov /* 228123689e91SAndrey Konovalov * Mark the pages as accessible, now that they are mapped. 228223689e91SAndrey Konovalov * With hardware tag-based KASAN, marking is skipped for 228323689e91SAndrey Konovalov * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc(). 228423689e91SAndrey Konovalov */ 2285f6e39794SAndrey Konovalov mem = kasan_unpoison_vmalloc(mem, size, KASAN_VMALLOC_PROT_NORMAL); 228619f1c3acSAndrey Konovalov 2287db64fe02SNick Piggin return mem; 2288db64fe02SNick Piggin } 2289db64fe02SNick Piggin EXPORT_SYMBOL(vm_map_ram); 2290db64fe02SNick Piggin 22914341fa45SJoonsoo Kim static struct vm_struct *vmlist __initdata; 229292eac168SMike Rapoport 2293121e6f32SNicholas Piggin static inline unsigned int vm_area_page_order(struct vm_struct *vm) 2294121e6f32SNicholas Piggin { 2295121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 2296121e6f32SNicholas Piggin return vm->page_order; 2297121e6f32SNicholas Piggin #else 2298121e6f32SNicholas Piggin return 0; 2299121e6f32SNicholas Piggin #endif 2300121e6f32SNicholas Piggin } 2301121e6f32SNicholas Piggin 2302121e6f32SNicholas Piggin static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int order) 2303121e6f32SNicholas Piggin { 2304121e6f32SNicholas Piggin #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 2305121e6f32SNicholas Piggin vm->page_order = order; 2306121e6f32SNicholas Piggin #else 2307121e6f32SNicholas Piggin BUG_ON(order != 0); 2308121e6f32SNicholas Piggin #endif 2309121e6f32SNicholas Piggin } 2310121e6f32SNicholas Piggin 2311f0aa6617STejun Heo /** 2312be9b7335SNicolas Pitre * vm_area_add_early - add vmap area early during boot 2313be9b7335SNicolas Pitre * @vm: vm_struct to add 2314be9b7335SNicolas Pitre * 2315be9b7335SNicolas Pitre * This function is used to add fixed kernel vm area to vmlist before 2316be9b7335SNicolas Pitre * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags 2317be9b7335SNicolas Pitre * should contain proper values and the other fields should be zero. 2318be9b7335SNicolas Pitre * 2319be9b7335SNicolas Pitre * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 2320be9b7335SNicolas Pitre */ 2321be9b7335SNicolas Pitre void __init vm_area_add_early(struct vm_struct *vm) 2322be9b7335SNicolas Pitre { 2323be9b7335SNicolas Pitre struct vm_struct *tmp, **p; 2324be9b7335SNicolas Pitre 2325be9b7335SNicolas Pitre BUG_ON(vmap_initialized); 2326be9b7335SNicolas Pitre for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { 2327be9b7335SNicolas Pitre if (tmp->addr >= vm->addr) { 2328be9b7335SNicolas Pitre BUG_ON(tmp->addr < vm->addr + vm->size); 2329be9b7335SNicolas Pitre break; 2330be9b7335SNicolas Pitre } else 2331be9b7335SNicolas Pitre BUG_ON(tmp->addr + tmp->size > vm->addr); 2332be9b7335SNicolas Pitre } 2333be9b7335SNicolas Pitre vm->next = *p; 2334be9b7335SNicolas Pitre *p = vm; 2335be9b7335SNicolas Pitre } 2336be9b7335SNicolas Pitre 2337be9b7335SNicolas Pitre /** 2338f0aa6617STejun Heo * vm_area_register_early - register vmap area early during boot 2339f0aa6617STejun Heo * @vm: vm_struct to register 2340c0c0a293STejun Heo * @align: requested alignment 2341f0aa6617STejun Heo * 2342f0aa6617STejun Heo * This function is used to register kernel vm area before 2343f0aa6617STejun Heo * vmalloc_init() is called. @vm->size and @vm->flags should contain 2344f0aa6617STejun Heo * proper values on entry and other fields should be zero. On return, 2345f0aa6617STejun Heo * vm->addr contains the allocated address. 2346f0aa6617STejun Heo * 2347f0aa6617STejun Heo * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. 2348f0aa6617STejun Heo */ 2349c0c0a293STejun Heo void __init vm_area_register_early(struct vm_struct *vm, size_t align) 2350f0aa6617STejun Heo { 23510eb68437SKefeng Wang unsigned long addr = ALIGN(VMALLOC_START, align); 23520eb68437SKefeng Wang struct vm_struct *cur, **p; 2353f0aa6617STejun Heo 23540eb68437SKefeng Wang BUG_ON(vmap_initialized); 2355c0c0a293STejun Heo 23560eb68437SKefeng Wang for (p = &vmlist; (cur = *p) != NULL; p = &cur->next) { 23570eb68437SKefeng Wang if ((unsigned long)cur->addr - addr >= vm->size) 23580eb68437SKefeng Wang break; 23590eb68437SKefeng Wang addr = ALIGN((unsigned long)cur->addr + cur->size, align); 23600eb68437SKefeng Wang } 23610eb68437SKefeng Wang 23620eb68437SKefeng Wang BUG_ON(addr > VMALLOC_END - vm->size); 2363c0c0a293STejun Heo vm->addr = (void *)addr; 23640eb68437SKefeng Wang vm->next = *p; 23650eb68437SKefeng Wang *p = vm; 23663252b1d8SKefeng Wang kasan_populate_early_vm_area_shadow(vm->addr, vm->size); 2367f0aa6617STejun Heo } 2368f0aa6617STejun Heo 236968ad4a33SUladzislau Rezki (Sony) static void vmap_init_free_space(void) 237068ad4a33SUladzislau Rezki (Sony) { 237168ad4a33SUladzislau Rezki (Sony) unsigned long vmap_start = 1; 237268ad4a33SUladzislau Rezki (Sony) const unsigned long vmap_end = ULONG_MAX; 237368ad4a33SUladzislau Rezki (Sony) struct vmap_area *busy, *free; 237468ad4a33SUladzislau Rezki (Sony) 237568ad4a33SUladzislau Rezki (Sony) /* 237668ad4a33SUladzislau Rezki (Sony) * B F B B B F 237768ad4a33SUladzislau Rezki (Sony) * -|-----|.....|-----|-----|-----|.....|- 237868ad4a33SUladzislau Rezki (Sony) * | The KVA space | 237968ad4a33SUladzislau Rezki (Sony) * |<--------------------------------->| 238068ad4a33SUladzislau Rezki (Sony) */ 238168ad4a33SUladzislau Rezki (Sony) list_for_each_entry(busy, &vmap_area_list, list) { 238268ad4a33SUladzislau Rezki (Sony) if (busy->va_start - vmap_start > 0) { 238368ad4a33SUladzislau Rezki (Sony) free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 238468ad4a33SUladzislau Rezki (Sony) if (!WARN_ON_ONCE(!free)) { 238568ad4a33SUladzislau Rezki (Sony) free->va_start = vmap_start; 238668ad4a33SUladzislau Rezki (Sony) free->va_end = busy->va_start; 238768ad4a33SUladzislau Rezki (Sony) 238868ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(free, NULL, 238968ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, 239068ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list); 239168ad4a33SUladzislau Rezki (Sony) } 239268ad4a33SUladzislau Rezki (Sony) } 239368ad4a33SUladzislau Rezki (Sony) 239468ad4a33SUladzislau Rezki (Sony) vmap_start = busy->va_end; 239568ad4a33SUladzislau Rezki (Sony) } 239668ad4a33SUladzislau Rezki (Sony) 239768ad4a33SUladzislau Rezki (Sony) if (vmap_end - vmap_start > 0) { 239868ad4a33SUladzislau Rezki (Sony) free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 239968ad4a33SUladzislau Rezki (Sony) if (!WARN_ON_ONCE(!free)) { 240068ad4a33SUladzislau Rezki (Sony) free->va_start = vmap_start; 240168ad4a33SUladzislau Rezki (Sony) free->va_end = vmap_end; 240268ad4a33SUladzislau Rezki (Sony) 240368ad4a33SUladzislau Rezki (Sony) insert_vmap_area_augment(free, NULL, 240468ad4a33SUladzislau Rezki (Sony) &free_vmap_area_root, 240568ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list); 240668ad4a33SUladzislau Rezki (Sony) } 240768ad4a33SUladzislau Rezki (Sony) } 240868ad4a33SUladzislau Rezki (Sony) } 240968ad4a33SUladzislau Rezki (Sony) 2410db64fe02SNick Piggin void __init vmalloc_init(void) 2411db64fe02SNick Piggin { 2412822c18f2SIvan Kokshaysky struct vmap_area *va; 2413822c18f2SIvan Kokshaysky struct vm_struct *tmp; 2414db64fe02SNick Piggin int i; 2415db64fe02SNick Piggin 241668ad4a33SUladzislau Rezki (Sony) /* 241768ad4a33SUladzislau Rezki (Sony) * Create the cache for vmap_area objects. 241868ad4a33SUladzislau Rezki (Sony) */ 241968ad4a33SUladzislau Rezki (Sony) vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC); 242068ad4a33SUladzislau Rezki (Sony) 2421db64fe02SNick Piggin for_each_possible_cpu(i) { 2422db64fe02SNick Piggin struct vmap_block_queue *vbq; 242332fcfd40SAl Viro struct vfree_deferred *p; 2424db64fe02SNick Piggin 2425db64fe02SNick Piggin vbq = &per_cpu(vmap_block_queue, i); 2426db64fe02SNick Piggin spin_lock_init(&vbq->lock); 2427db64fe02SNick Piggin INIT_LIST_HEAD(&vbq->free); 242832fcfd40SAl Viro p = &per_cpu(vfree_deferred, i); 242932fcfd40SAl Viro init_llist_head(&p->list); 243032fcfd40SAl Viro INIT_WORK(&p->wq, free_work); 2431db64fe02SNick Piggin } 24329b463334SJeremy Fitzhardinge 2433822c18f2SIvan Kokshaysky /* Import existing vmlist entries. */ 2434822c18f2SIvan Kokshaysky for (tmp = vmlist; tmp; tmp = tmp->next) { 243568ad4a33SUladzislau Rezki (Sony) va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); 243668ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(!va)) 243768ad4a33SUladzislau Rezki (Sony) continue; 243868ad4a33SUladzislau Rezki (Sony) 2439822c18f2SIvan Kokshaysky va->va_start = (unsigned long)tmp->addr; 2440822c18f2SIvan Kokshaysky va->va_end = va->va_start + tmp->size; 2441dbda591dSKyongHo va->vm = tmp; 244268ad4a33SUladzislau Rezki (Sony) insert_vmap_area(va, &vmap_area_root, &vmap_area_list); 2443822c18f2SIvan Kokshaysky } 2444ca23e405STejun Heo 244568ad4a33SUladzislau Rezki (Sony) /* 244668ad4a33SUladzislau Rezki (Sony) * Now we can initialize a free vmap space. 244768ad4a33SUladzislau Rezki (Sony) */ 244868ad4a33SUladzislau Rezki (Sony) vmap_init_free_space(); 24499b463334SJeremy Fitzhardinge vmap_initialized = true; 2450db64fe02SNick Piggin } 2451db64fe02SNick Piggin 2452e36176beSUladzislau Rezki (Sony) static inline void setup_vmalloc_vm_locked(struct vm_struct *vm, 2453e36176beSUladzislau Rezki (Sony) struct vmap_area *va, unsigned long flags, const void *caller) 2454cf88c790STejun Heo { 2455cf88c790STejun Heo vm->flags = flags; 2456cf88c790STejun Heo vm->addr = (void *)va->va_start; 2457cf88c790STejun Heo vm->size = va->va_end - va->va_start; 2458cf88c790STejun Heo vm->caller = caller; 2459db1aecafSMinchan Kim va->vm = vm; 2460e36176beSUladzislau Rezki (Sony) } 2461e36176beSUladzislau Rezki (Sony) 2462e36176beSUladzislau Rezki (Sony) static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, 2463e36176beSUladzislau Rezki (Sony) unsigned long flags, const void *caller) 2464e36176beSUladzislau Rezki (Sony) { 2465e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 2466e36176beSUladzislau Rezki (Sony) setup_vmalloc_vm_locked(vm, va, flags, caller); 2467c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 2468f5252e00SMitsuo Hayasaka } 2469cf88c790STejun Heo 247020fc02b4SZhang Yanfei static void clear_vm_uninitialized_flag(struct vm_struct *vm) 2471f5252e00SMitsuo Hayasaka { 2472d4033afdSJoonsoo Kim /* 247320fc02b4SZhang Yanfei * Before removing VM_UNINITIALIZED, 2474d4033afdSJoonsoo Kim * we should make sure that vm has proper values. 2475d4033afdSJoonsoo Kim * Pair with smp_rmb() in show_numa_info(). 2476d4033afdSJoonsoo Kim */ 2477d4033afdSJoonsoo Kim smp_wmb(); 247820fc02b4SZhang Yanfei vm->flags &= ~VM_UNINITIALIZED; 2479cf88c790STejun Heo } 2480cf88c790STejun Heo 2481db64fe02SNick Piggin static struct vm_struct *__get_vm_area_node(unsigned long size, 24827ca3027bSDaniel Axtens unsigned long align, unsigned long shift, unsigned long flags, 24837ca3027bSDaniel Axtens unsigned long start, unsigned long end, int node, 24847ca3027bSDaniel Axtens gfp_t gfp_mask, const void *caller) 2485db64fe02SNick Piggin { 24860006526dSKautuk Consul struct vmap_area *va; 2487db64fe02SNick Piggin struct vm_struct *area; 2488d98c9e83SAndrey Ryabinin unsigned long requested_size = size; 24891da177e4SLinus Torvalds 249052fd24caSGiridhar Pemmasani BUG_ON(in_interrupt()); 24917ca3027bSDaniel Axtens size = ALIGN(size, 1ul << shift); 249231be8309SOGAWA Hirofumi if (unlikely(!size)) 249331be8309SOGAWA Hirofumi return NULL; 24941da177e4SLinus Torvalds 2495252e5c6eSzijun_hu if (flags & VM_IOREMAP) 2496252e5c6eSzijun_hu align = 1ul << clamp_t(int, get_count_order_long(size), 2497252e5c6eSzijun_hu PAGE_SHIFT, IOREMAP_MAX_ORDER); 2498252e5c6eSzijun_hu 2499cf88c790STejun Heo area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); 25001da177e4SLinus Torvalds if (unlikely(!area)) 25011da177e4SLinus Torvalds return NULL; 25021da177e4SLinus Torvalds 250371394fe5SAndrey Ryabinin if (!(flags & VM_NO_GUARD)) 25041da177e4SLinus Torvalds size += PAGE_SIZE; 25051da177e4SLinus Torvalds 2506db64fe02SNick Piggin va = alloc_vmap_area(size, align, start, end, node, gfp_mask); 2507db64fe02SNick Piggin if (IS_ERR(va)) { 2508db64fe02SNick Piggin kfree(area); 2509db64fe02SNick Piggin return NULL; 25101da177e4SLinus Torvalds } 25111da177e4SLinus Torvalds 2512d98c9e83SAndrey Ryabinin setup_vmalloc_vm(area, va, flags, caller); 25133c5c3cfbSDaniel Axtens 251419f1c3acSAndrey Konovalov /* 251519f1c3acSAndrey Konovalov * Mark pages for non-VM_ALLOC mappings as accessible. Do it now as a 251619f1c3acSAndrey Konovalov * best-effort approach, as they can be mapped outside of vmalloc code. 251719f1c3acSAndrey Konovalov * For VM_ALLOC mappings, the pages are marked as accessible after 251819f1c3acSAndrey Konovalov * getting mapped in __vmalloc_node_range(). 251923689e91SAndrey Konovalov * With hardware tag-based KASAN, marking is skipped for 252023689e91SAndrey Konovalov * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc(). 252119f1c3acSAndrey Konovalov */ 252219f1c3acSAndrey Konovalov if (!(flags & VM_ALLOC)) 252323689e91SAndrey Konovalov area->addr = kasan_unpoison_vmalloc(area->addr, requested_size, 2524f6e39794SAndrey Konovalov KASAN_VMALLOC_PROT_NORMAL); 25251d96320fSAndrey Konovalov 25261da177e4SLinus Torvalds return area; 25271da177e4SLinus Torvalds } 25281da177e4SLinus Torvalds 2529c2968612SBenjamin Herrenschmidt struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, 2530c2968612SBenjamin Herrenschmidt unsigned long start, unsigned long end, 25315e6cafc8SMarek Szyprowski const void *caller) 2532c2968612SBenjamin Herrenschmidt { 25337ca3027bSDaniel Axtens return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, start, end, 25347ca3027bSDaniel Axtens NUMA_NO_NODE, GFP_KERNEL, caller); 2535c2968612SBenjamin Herrenschmidt } 2536c2968612SBenjamin Herrenschmidt 25371da177e4SLinus Torvalds /** 2538183ff22bSSimon Arlott * get_vm_area - reserve a contiguous kernel virtual area 25391da177e4SLinus Torvalds * @size: size of the area 25401da177e4SLinus Torvalds * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC 25411da177e4SLinus Torvalds * 25421da177e4SLinus Torvalds * Search an area of @size in the kernel virtual mapping area, 25431da177e4SLinus Torvalds * and reserved it for out purposes. Returns the area descriptor 25441da177e4SLinus Torvalds * on success or %NULL on failure. 2545a862f68aSMike Rapoport * 2546a862f68aSMike Rapoport * Return: the area descriptor on success or %NULL on failure. 25471da177e4SLinus Torvalds */ 25481da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) 25491da177e4SLinus Torvalds { 25507ca3027bSDaniel Axtens return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, 25517ca3027bSDaniel Axtens VMALLOC_START, VMALLOC_END, 255200ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, 255300ef2d2fSDavid Rientjes __builtin_return_address(0)); 255423016969SChristoph Lameter } 255523016969SChristoph Lameter 255623016969SChristoph Lameter struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, 25575e6cafc8SMarek Szyprowski const void *caller) 255823016969SChristoph Lameter { 25597ca3027bSDaniel Axtens return __get_vm_area_node(size, 1, PAGE_SHIFT, flags, 25607ca3027bSDaniel Axtens VMALLOC_START, VMALLOC_END, 256100ef2d2fSDavid Rientjes NUMA_NO_NODE, GFP_KERNEL, caller); 25621da177e4SLinus Torvalds } 25631da177e4SLinus Torvalds 2564e9da6e99SMarek Szyprowski /** 2565e9da6e99SMarek Szyprowski * find_vm_area - find a continuous kernel virtual area 2566e9da6e99SMarek Szyprowski * @addr: base address 2567e9da6e99SMarek Szyprowski * 2568e9da6e99SMarek Szyprowski * Search for the kernel VM area starting at @addr, and return it. 2569e9da6e99SMarek Szyprowski * It is up to the caller to do all required locking to keep the returned 2570e9da6e99SMarek Szyprowski * pointer valid. 2571a862f68aSMike Rapoport * 257274640617SHui Su * Return: the area descriptor on success or %NULL on failure. 2573e9da6e99SMarek Szyprowski */ 2574e9da6e99SMarek Szyprowski struct vm_struct *find_vm_area(const void *addr) 257583342314SNick Piggin { 2576db64fe02SNick Piggin struct vmap_area *va; 257783342314SNick Piggin 2578db64fe02SNick Piggin va = find_vmap_area((unsigned long)addr); 2579688fcbfcSPengfei Li if (!va) 25807856dfebSAndi Kleen return NULL; 2581688fcbfcSPengfei Li 2582688fcbfcSPengfei Li return va->vm; 25837856dfebSAndi Kleen } 25847856dfebSAndi Kleen 25851da177e4SLinus Torvalds /** 2586183ff22bSSimon Arlott * remove_vm_area - find and remove a continuous kernel virtual area 25871da177e4SLinus Torvalds * @addr: base address 25881da177e4SLinus Torvalds * 25891da177e4SLinus Torvalds * Search for the kernel VM area starting at @addr, and remove it. 25901da177e4SLinus Torvalds * This function returns the found VM area, but using it is NOT safe 25917856dfebSAndi Kleen * on SMP machines, except for its size or flags. 2592a862f68aSMike Rapoport * 259374640617SHui Su * Return: the area descriptor on success or %NULL on failure. 25941da177e4SLinus Torvalds */ 2595b3bdda02SChristoph Lameter struct vm_struct *remove_vm_area(const void *addr) 25961da177e4SLinus Torvalds { 2597db64fe02SNick Piggin struct vmap_area *va; 2598db64fe02SNick Piggin 25995803ed29SChristoph Hellwig might_sleep(); 26005803ed29SChristoph Hellwig 2601dd3b8353SUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 2602899c6efeSUladzislau Rezki (Sony) va = __find_vmap_area((unsigned long)addr, &vmap_area_root); 2603688fcbfcSPengfei Li if (va && va->vm) { 2604db1aecafSMinchan Kim struct vm_struct *vm = va->vm; 2605f5252e00SMitsuo Hayasaka 2606c69480adSJoonsoo Kim va->vm = NULL; 2607c69480adSJoonsoo Kim spin_unlock(&vmap_area_lock); 2608c69480adSJoonsoo Kim 260963840de2SAndrey Konovalov kasan_free_module_shadow(vm); 2610dd32c279SKAMEZAWA Hiroyuki free_unmap_vmap_area(va); 2611dd32c279SKAMEZAWA Hiroyuki 2612db64fe02SNick Piggin return vm; 2613db64fe02SNick Piggin } 2614dd3b8353SUladzislau Rezki (Sony) 2615dd3b8353SUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 2616db64fe02SNick Piggin return NULL; 26171da177e4SLinus Torvalds } 26181da177e4SLinus Torvalds 2619868b104dSRick Edgecombe static inline void set_area_direct_map(const struct vm_struct *area, 2620868b104dSRick Edgecombe int (*set_direct_map)(struct page *page)) 2621868b104dSRick Edgecombe { 2622868b104dSRick Edgecombe int i; 2623868b104dSRick Edgecombe 2624121e6f32SNicholas Piggin /* HUGE_VMALLOC passes small pages to set_direct_map */ 2625868b104dSRick Edgecombe for (i = 0; i < area->nr_pages; i++) 2626868b104dSRick Edgecombe if (page_address(area->pages[i])) 2627868b104dSRick Edgecombe set_direct_map(area->pages[i]); 2628868b104dSRick Edgecombe } 2629868b104dSRick Edgecombe 2630868b104dSRick Edgecombe /* Handle removing and resetting vm mappings related to the vm_struct. */ 2631868b104dSRick Edgecombe static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages) 2632868b104dSRick Edgecombe { 2633868b104dSRick Edgecombe unsigned long start = ULONG_MAX, end = 0; 2634121e6f32SNicholas Piggin unsigned int page_order = vm_area_page_order(area); 2635868b104dSRick Edgecombe int flush_reset = area->flags & VM_FLUSH_RESET_PERMS; 263631e67340SRick Edgecombe int flush_dmap = 0; 2637868b104dSRick Edgecombe int i; 2638868b104dSRick Edgecombe 2639868b104dSRick Edgecombe remove_vm_area(area->addr); 2640868b104dSRick Edgecombe 2641868b104dSRick Edgecombe /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */ 2642868b104dSRick Edgecombe if (!flush_reset) 2643868b104dSRick Edgecombe return; 2644868b104dSRick Edgecombe 2645868b104dSRick Edgecombe /* 2646868b104dSRick Edgecombe * If not deallocating pages, just do the flush of the VM area and 2647868b104dSRick Edgecombe * return. 2648868b104dSRick Edgecombe */ 2649868b104dSRick Edgecombe if (!deallocate_pages) { 2650868b104dSRick Edgecombe vm_unmap_aliases(); 2651868b104dSRick Edgecombe return; 2652868b104dSRick Edgecombe } 2653868b104dSRick Edgecombe 2654868b104dSRick Edgecombe /* 2655868b104dSRick Edgecombe * If execution gets here, flush the vm mapping and reset the direct 2656868b104dSRick Edgecombe * map. Find the start and end range of the direct mappings to make sure 2657868b104dSRick Edgecombe * the vm_unmap_aliases() flush includes the direct map. 2658868b104dSRick Edgecombe */ 2659121e6f32SNicholas Piggin for (i = 0; i < area->nr_pages; i += 1U << page_order) { 26608e41f872SRick Edgecombe unsigned long addr = (unsigned long)page_address(area->pages[i]); 26618e41f872SRick Edgecombe if (addr) { 2662121e6f32SNicholas Piggin unsigned long page_size; 2663121e6f32SNicholas Piggin 2664121e6f32SNicholas Piggin page_size = PAGE_SIZE << page_order; 2665868b104dSRick Edgecombe start = min(addr, start); 2666121e6f32SNicholas Piggin end = max(addr + page_size, end); 266731e67340SRick Edgecombe flush_dmap = 1; 2668868b104dSRick Edgecombe } 2669868b104dSRick Edgecombe } 2670868b104dSRick Edgecombe 2671868b104dSRick Edgecombe /* 2672868b104dSRick Edgecombe * Set direct map to something invalid so that it won't be cached if 2673868b104dSRick Edgecombe * there are any accesses after the TLB flush, then flush the TLB and 2674868b104dSRick Edgecombe * reset the direct map permissions to the default. 2675868b104dSRick Edgecombe */ 2676868b104dSRick Edgecombe set_area_direct_map(area, set_direct_map_invalid_noflush); 267731e67340SRick Edgecombe _vm_unmap_aliases(start, end, flush_dmap); 2678868b104dSRick Edgecombe set_area_direct_map(area, set_direct_map_default_noflush); 2679868b104dSRick Edgecombe } 2680868b104dSRick Edgecombe 2681b3bdda02SChristoph Lameter static void __vunmap(const void *addr, int deallocate_pages) 26821da177e4SLinus Torvalds { 26831da177e4SLinus Torvalds struct vm_struct *area; 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds if (!addr) 26861da177e4SLinus Torvalds return; 26871da177e4SLinus Torvalds 2688e69e9d4aSHATAYAMA Daisuke if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n", 2689ab15d9b4SDan Carpenter addr)) 26901da177e4SLinus Torvalds return; 26911da177e4SLinus Torvalds 26926ade2032SLiviu Dudau area = find_vm_area(addr); 26931da177e4SLinus Torvalds if (unlikely(!area)) { 26944c8573e2SArjan van de Ven WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", 26951da177e4SLinus Torvalds addr); 26961da177e4SLinus Torvalds return; 26971da177e4SLinus Torvalds } 26981da177e4SLinus Torvalds 269905e3ff95SChintan Pandya debug_check_no_locks_freed(area->addr, get_vm_area_size(area)); 270005e3ff95SChintan Pandya debug_check_no_obj_freed(area->addr, get_vm_area_size(area)); 27019a11b49aSIngo Molnar 2702c041098cSVincenzo Frascino kasan_poison_vmalloc(area->addr, get_vm_area_size(area)); 27033c5c3cfbSDaniel Axtens 2704868b104dSRick Edgecombe vm_remove_mappings(area, deallocate_pages); 2705868b104dSRick Edgecombe 27061da177e4SLinus Torvalds if (deallocate_pages) { 27073b8000aeSNicholas Piggin int i; 27081da177e4SLinus Torvalds 27093b8000aeSNicholas Piggin for (i = 0; i < area->nr_pages; i++) { 2710bf53d6f8SChristoph Lameter struct page *page = area->pages[i]; 2711bf53d6f8SChristoph Lameter 2712bf53d6f8SChristoph Lameter BUG_ON(!page); 27133b8000aeSNicholas Piggin mod_memcg_page_state(page, MEMCG_VMALLOC, -1); 27143b8000aeSNicholas Piggin /* 27153b8000aeSNicholas Piggin * High-order allocs for huge vmallocs are split, so 27163b8000aeSNicholas Piggin * can be freed as an array of order-0 allocations 27173b8000aeSNicholas Piggin */ 27183b8000aeSNicholas Piggin __free_pages(page, 0); 2719a850e932SRafael Aquini cond_resched(); 27201da177e4SLinus Torvalds } 272197105f0aSRoman Gushchin atomic_long_sub(area->nr_pages, &nr_vmalloc_pages); 27221da177e4SLinus Torvalds 2723244d63eeSDavid Rientjes kvfree(area->pages); 27241da177e4SLinus Torvalds } 27251da177e4SLinus Torvalds 27261da177e4SLinus Torvalds kfree(area); 27271da177e4SLinus Torvalds } 27281da177e4SLinus Torvalds 2729bf22e37aSAndrey Ryabinin static inline void __vfree_deferred(const void *addr) 2730bf22e37aSAndrey Ryabinin { 2731bf22e37aSAndrey Ryabinin /* 2732bf22e37aSAndrey Ryabinin * Use raw_cpu_ptr() because this can be called from preemptible 2733bf22e37aSAndrey Ryabinin * context. Preemption is absolutely fine here, because the llist_add() 2734bf22e37aSAndrey Ryabinin * implementation is lockless, so it works even if we are adding to 273573221d88SJeongtae Park * another cpu's list. schedule_work() should be fine with this too. 2736bf22e37aSAndrey Ryabinin */ 2737bf22e37aSAndrey Ryabinin struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred); 2738bf22e37aSAndrey Ryabinin 2739bf22e37aSAndrey Ryabinin if (llist_add((struct llist_node *)addr, &p->list)) 2740bf22e37aSAndrey Ryabinin schedule_work(&p->wq); 2741bf22e37aSAndrey Ryabinin } 2742bf22e37aSAndrey Ryabinin 2743bf22e37aSAndrey Ryabinin /** 2744bf22e37aSAndrey Ryabinin * vfree_atomic - release memory allocated by vmalloc() 2745bf22e37aSAndrey Ryabinin * @addr: memory base address 2746bf22e37aSAndrey Ryabinin * 2747bf22e37aSAndrey Ryabinin * This one is just like vfree() but can be called in any atomic context 2748bf22e37aSAndrey Ryabinin * except NMIs. 2749bf22e37aSAndrey Ryabinin */ 2750bf22e37aSAndrey Ryabinin void vfree_atomic(const void *addr) 2751bf22e37aSAndrey Ryabinin { 2752bf22e37aSAndrey Ryabinin BUG_ON(in_nmi()); 2753bf22e37aSAndrey Ryabinin 2754bf22e37aSAndrey Ryabinin kmemleak_free(addr); 2755bf22e37aSAndrey Ryabinin 2756bf22e37aSAndrey Ryabinin if (!addr) 2757bf22e37aSAndrey Ryabinin return; 2758bf22e37aSAndrey Ryabinin __vfree_deferred(addr); 2759bf22e37aSAndrey Ryabinin } 2760bf22e37aSAndrey Ryabinin 2761c67dc624SRoman Penyaev static void __vfree(const void *addr) 2762c67dc624SRoman Penyaev { 2763c67dc624SRoman Penyaev if (unlikely(in_interrupt())) 2764c67dc624SRoman Penyaev __vfree_deferred(addr); 2765c67dc624SRoman Penyaev else 2766c67dc624SRoman Penyaev __vunmap(addr, 1); 2767c67dc624SRoman Penyaev } 2768c67dc624SRoman Penyaev 27691da177e4SLinus Torvalds /** 2770fa307474SMatthew Wilcox (Oracle) * vfree - Release memory allocated by vmalloc() 2771fa307474SMatthew Wilcox (Oracle) * @addr: Memory base address 27721da177e4SLinus Torvalds * 2773fa307474SMatthew Wilcox (Oracle) * Free the virtually continuous memory area starting at @addr, as obtained 2774fa307474SMatthew Wilcox (Oracle) * from one of the vmalloc() family of APIs. This will usually also free the 2775fa307474SMatthew Wilcox (Oracle) * physical memory underlying the virtual allocation, but that memory is 2776fa307474SMatthew Wilcox (Oracle) * reference counted, so it will not be freed until the last user goes away. 27771da177e4SLinus Torvalds * 2778fa307474SMatthew Wilcox (Oracle) * If @addr is NULL, no operation is performed. 277932fcfd40SAl Viro * 2780fa307474SMatthew Wilcox (Oracle) * Context: 27813ca4ea3aSAndrey Ryabinin * May sleep if called *not* from interrupt context. 2782fa307474SMatthew Wilcox (Oracle) * Must not be called in NMI context (strictly speaking, it could be 2783fa307474SMatthew Wilcox (Oracle) * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling 2784f0953a1bSIngo Molnar * conventions for vfree() arch-dependent would be a really bad idea). 27851da177e4SLinus Torvalds */ 2786b3bdda02SChristoph Lameter void vfree(const void *addr) 27871da177e4SLinus Torvalds { 278832fcfd40SAl Viro BUG_ON(in_nmi()); 278989219d37SCatalin Marinas 279089219d37SCatalin Marinas kmemleak_free(addr); 279189219d37SCatalin Marinas 2792a8dda165SAndrey Ryabinin might_sleep_if(!in_interrupt()); 2793a8dda165SAndrey Ryabinin 279432fcfd40SAl Viro if (!addr) 279532fcfd40SAl Viro return; 2796c67dc624SRoman Penyaev 2797c67dc624SRoman Penyaev __vfree(addr); 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds EXPORT_SYMBOL(vfree); 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds /** 28021da177e4SLinus Torvalds * vunmap - release virtual mapping obtained by vmap() 28031da177e4SLinus Torvalds * @addr: memory base address 28041da177e4SLinus Torvalds * 28051da177e4SLinus Torvalds * Free the virtually contiguous memory area starting at @addr, 28061da177e4SLinus Torvalds * which was created from the page array passed to vmap(). 28071da177e4SLinus Torvalds * 280880e93effSPekka Enberg * Must not be called in interrupt context. 28091da177e4SLinus Torvalds */ 2810b3bdda02SChristoph Lameter void vunmap(const void *addr) 28111da177e4SLinus Torvalds { 28121da177e4SLinus Torvalds BUG_ON(in_interrupt()); 281334754b69SPeter Zijlstra might_sleep(); 281432fcfd40SAl Viro if (addr) 28151da177e4SLinus Torvalds __vunmap(addr, 0); 28161da177e4SLinus Torvalds } 28171da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap); 28181da177e4SLinus Torvalds 28191da177e4SLinus Torvalds /** 28201da177e4SLinus Torvalds * vmap - map an array of pages into virtually contiguous space 28211da177e4SLinus Torvalds * @pages: array of page pointers 28221da177e4SLinus Torvalds * @count: number of pages to map 28231da177e4SLinus Torvalds * @flags: vm_area->flags 28241da177e4SLinus Torvalds * @prot: page protection for the mapping 28251da177e4SLinus Torvalds * 2826b944afc9SChristoph Hellwig * Maps @count pages from @pages into contiguous kernel virtual space. 2827b944afc9SChristoph Hellwig * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself 2828b944afc9SChristoph Hellwig * (which must be kmalloc or vmalloc memory) and one reference per pages in it 2829b944afc9SChristoph Hellwig * are transferred from the caller to vmap(), and will be freed / dropped when 2830b944afc9SChristoph Hellwig * vfree() is called on the return value. 2831a862f68aSMike Rapoport * 2832a862f68aSMike Rapoport * Return: the address of the area or %NULL on failure 28331da177e4SLinus Torvalds */ 28341da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, 28351da177e4SLinus Torvalds unsigned long flags, pgprot_t prot) 28361da177e4SLinus Torvalds { 28371da177e4SLinus Torvalds struct vm_struct *area; 2838b67177ecSNicholas Piggin unsigned long addr; 283965ee03c4SGuillermo Julián Moreno unsigned long size; /* In bytes */ 28401da177e4SLinus Torvalds 284134754b69SPeter Zijlstra might_sleep(); 284234754b69SPeter Zijlstra 2843bd1a8fb2SPeter Zijlstra /* 2844bd1a8fb2SPeter Zijlstra * Your top guard is someone else's bottom guard. Not having a top 2845bd1a8fb2SPeter Zijlstra * guard compromises someone else's mappings too. 2846bd1a8fb2SPeter Zijlstra */ 2847bd1a8fb2SPeter Zijlstra if (WARN_ON_ONCE(flags & VM_NO_GUARD)) 2848bd1a8fb2SPeter Zijlstra flags &= ~VM_NO_GUARD; 2849bd1a8fb2SPeter Zijlstra 2850ca79b0c2SArun KS if (count > totalram_pages()) 28511da177e4SLinus Torvalds return NULL; 28521da177e4SLinus Torvalds 285365ee03c4SGuillermo Julián Moreno size = (unsigned long)count << PAGE_SHIFT; 285465ee03c4SGuillermo Julián Moreno area = get_vm_area_caller(size, flags, __builtin_return_address(0)); 28551da177e4SLinus Torvalds if (!area) 28561da177e4SLinus Torvalds return NULL; 285723016969SChristoph Lameter 2858b67177ecSNicholas Piggin addr = (unsigned long)area->addr; 2859b67177ecSNicholas Piggin if (vmap_pages_range(addr, addr + size, pgprot_nx(prot), 2860b67177ecSNicholas Piggin pages, PAGE_SHIFT) < 0) { 28611da177e4SLinus Torvalds vunmap(area->addr); 28621da177e4SLinus Torvalds return NULL; 28631da177e4SLinus Torvalds } 28641da177e4SLinus Torvalds 2865c22ee528SMiaohe Lin if (flags & VM_MAP_PUT_PAGES) { 2866b944afc9SChristoph Hellwig area->pages = pages; 2867c22ee528SMiaohe Lin area->nr_pages = count; 2868c22ee528SMiaohe Lin } 28691da177e4SLinus Torvalds return area->addr; 28701da177e4SLinus Torvalds } 28711da177e4SLinus Torvalds EXPORT_SYMBOL(vmap); 28721da177e4SLinus Torvalds 28733e9a9e25SChristoph Hellwig #ifdef CONFIG_VMAP_PFN 28743e9a9e25SChristoph Hellwig struct vmap_pfn_data { 28753e9a9e25SChristoph Hellwig unsigned long *pfns; 28763e9a9e25SChristoph Hellwig pgprot_t prot; 28773e9a9e25SChristoph Hellwig unsigned int idx; 28783e9a9e25SChristoph Hellwig }; 28793e9a9e25SChristoph Hellwig 28803e9a9e25SChristoph Hellwig static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private) 28813e9a9e25SChristoph Hellwig { 28823e9a9e25SChristoph Hellwig struct vmap_pfn_data *data = private; 28833e9a9e25SChristoph Hellwig 28843e9a9e25SChristoph Hellwig if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx]))) 28853e9a9e25SChristoph Hellwig return -EINVAL; 28863e9a9e25SChristoph Hellwig *pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot)); 28873e9a9e25SChristoph Hellwig return 0; 28883e9a9e25SChristoph Hellwig } 28893e9a9e25SChristoph Hellwig 28903e9a9e25SChristoph Hellwig /** 28913e9a9e25SChristoph Hellwig * vmap_pfn - map an array of PFNs into virtually contiguous space 28923e9a9e25SChristoph Hellwig * @pfns: array of PFNs 28933e9a9e25SChristoph Hellwig * @count: number of pages to map 28943e9a9e25SChristoph Hellwig * @prot: page protection for the mapping 28953e9a9e25SChristoph Hellwig * 28963e9a9e25SChristoph Hellwig * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns 28973e9a9e25SChristoph Hellwig * the start address of the mapping. 28983e9a9e25SChristoph Hellwig */ 28993e9a9e25SChristoph Hellwig void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot) 29003e9a9e25SChristoph Hellwig { 29013e9a9e25SChristoph Hellwig struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) }; 29023e9a9e25SChristoph Hellwig struct vm_struct *area; 29033e9a9e25SChristoph Hellwig 29043e9a9e25SChristoph Hellwig area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP, 29053e9a9e25SChristoph Hellwig __builtin_return_address(0)); 29063e9a9e25SChristoph Hellwig if (!area) 29073e9a9e25SChristoph Hellwig return NULL; 29083e9a9e25SChristoph Hellwig if (apply_to_page_range(&init_mm, (unsigned long)area->addr, 29093e9a9e25SChristoph Hellwig count * PAGE_SIZE, vmap_pfn_apply, &data)) { 29103e9a9e25SChristoph Hellwig free_vm_area(area); 29113e9a9e25SChristoph Hellwig return NULL; 29123e9a9e25SChristoph Hellwig } 29133e9a9e25SChristoph Hellwig return area->addr; 29143e9a9e25SChristoph Hellwig } 29153e9a9e25SChristoph Hellwig EXPORT_SYMBOL_GPL(vmap_pfn); 29163e9a9e25SChristoph Hellwig #endif /* CONFIG_VMAP_PFN */ 29173e9a9e25SChristoph Hellwig 291812b9f873SUladzislau Rezki static inline unsigned int 291912b9f873SUladzislau Rezki vm_area_alloc_pages(gfp_t gfp, int nid, 2920343ab817SUladzislau Rezki (Sony) unsigned int order, unsigned int nr_pages, struct page **pages) 292112b9f873SUladzislau Rezki { 292212b9f873SUladzislau Rezki unsigned int nr_allocated = 0; 2923ffb29b1cSChen Wandun struct page *page; 2924ffb29b1cSChen Wandun int i; 292512b9f873SUladzislau Rezki 292612b9f873SUladzislau Rezki /* 292712b9f873SUladzislau Rezki * For order-0 pages we make use of bulk allocator, if 292812b9f873SUladzislau Rezki * the page array is partly or not at all populated due 292912b9f873SUladzislau Rezki * to fails, fallback to a single page allocator that is 293012b9f873SUladzislau Rezki * more permissive. 293112b9f873SUladzislau Rezki */ 2932c00b6b96SChen Wandun if (!order) { 29339376130cSMichal Hocko gfp_t bulk_gfp = gfp & ~__GFP_NOFAIL; 29349376130cSMichal Hocko 2935343ab817SUladzislau Rezki (Sony) while (nr_allocated < nr_pages) { 2936343ab817SUladzislau Rezki (Sony) unsigned int nr, nr_pages_request; 2937343ab817SUladzislau Rezki (Sony) 2938343ab817SUladzislau Rezki (Sony) /* 2939343ab817SUladzislau Rezki (Sony) * A maximum allowed request is hard-coded and is 100 2940343ab817SUladzislau Rezki (Sony) * pages per call. That is done in order to prevent a 2941343ab817SUladzislau Rezki (Sony) * long preemption off scenario in the bulk-allocator 2942343ab817SUladzislau Rezki (Sony) * so the range is [1:100]. 2943343ab817SUladzislau Rezki (Sony) */ 2944343ab817SUladzislau Rezki (Sony) nr_pages_request = min(100U, nr_pages - nr_allocated); 2945343ab817SUladzislau Rezki (Sony) 2946c00b6b96SChen Wandun /* memory allocation should consider mempolicy, we can't 2947c00b6b96SChen Wandun * wrongly use nearest node when nid == NUMA_NO_NODE, 2948c00b6b96SChen Wandun * otherwise memory may be allocated in only one node, 294998af39d5SYixuan Cao * but mempolicy wants to alloc memory by interleaving. 2950c00b6b96SChen Wandun */ 2951c00b6b96SChen Wandun if (IS_ENABLED(CONFIG_NUMA) && nid == NUMA_NO_NODE) 29529376130cSMichal Hocko nr = alloc_pages_bulk_array_mempolicy(bulk_gfp, 2953c00b6b96SChen Wandun nr_pages_request, 2954c00b6b96SChen Wandun pages + nr_allocated); 2955c00b6b96SChen Wandun 2956c00b6b96SChen Wandun else 29579376130cSMichal Hocko nr = alloc_pages_bulk_array_node(bulk_gfp, nid, 2958c00b6b96SChen Wandun nr_pages_request, 2959c00b6b96SChen Wandun pages + nr_allocated); 2960343ab817SUladzislau Rezki (Sony) 2961343ab817SUladzislau Rezki (Sony) nr_allocated += nr; 2962343ab817SUladzislau Rezki (Sony) cond_resched(); 2963343ab817SUladzislau Rezki (Sony) 2964343ab817SUladzislau Rezki (Sony) /* 2965343ab817SUladzislau Rezki (Sony) * If zero or pages were obtained partly, 2966343ab817SUladzislau Rezki (Sony) * fallback to a single page allocator. 2967343ab817SUladzislau Rezki (Sony) */ 2968343ab817SUladzislau Rezki (Sony) if (nr != nr_pages_request) 2969343ab817SUladzislau Rezki (Sony) break; 2970343ab817SUladzislau Rezki (Sony) } 29713b8000aeSNicholas Piggin } 297212b9f873SUladzislau Rezki 297312b9f873SUladzislau Rezki /* High-order pages or fallback path if "bulk" fails. */ 297412b9f873SUladzislau Rezki 2975ffb29b1cSChen Wandun while (nr_allocated < nr_pages) { 2976dd544141SVasily Averin if (fatal_signal_pending(current)) 2977dd544141SVasily Averin break; 2978dd544141SVasily Averin 2979ffb29b1cSChen Wandun if (nid == NUMA_NO_NODE) 2980ffb29b1cSChen Wandun page = alloc_pages(gfp, order); 2981ffb29b1cSChen Wandun else 298212b9f873SUladzislau Rezki page = alloc_pages_node(nid, gfp, order); 298312b9f873SUladzislau Rezki if (unlikely(!page)) 298412b9f873SUladzislau Rezki break; 29853b8000aeSNicholas Piggin /* 29863b8000aeSNicholas Piggin * Higher order allocations must be able to be treated as 29873b8000aeSNicholas Piggin * indepdenent small pages by callers (as they can with 29883b8000aeSNicholas Piggin * small-page vmallocs). Some drivers do their own refcounting 29893b8000aeSNicholas Piggin * on vmalloc_to_page() pages, some use page->mapping, 29903b8000aeSNicholas Piggin * page->lru, etc. 29913b8000aeSNicholas Piggin */ 29923b8000aeSNicholas Piggin if (order) 29933b8000aeSNicholas Piggin split_page(page, order); 299412b9f873SUladzislau Rezki 299512b9f873SUladzislau Rezki /* 299612b9f873SUladzislau Rezki * Careful, we allocate and map page-order pages, but 299712b9f873SUladzislau Rezki * tracking is done per PAGE_SIZE page so as to keep the 299812b9f873SUladzislau Rezki * vm_struct APIs independent of the physical/mapped size. 299912b9f873SUladzislau Rezki */ 300012b9f873SUladzislau Rezki for (i = 0; i < (1U << order); i++) 300112b9f873SUladzislau Rezki pages[nr_allocated + i] = page + i; 300212b9f873SUladzislau Rezki 300312b9f873SUladzislau Rezki cond_resched(); 300412b9f873SUladzislau Rezki nr_allocated += 1U << order; 300512b9f873SUladzislau Rezki } 300612b9f873SUladzislau Rezki 300712b9f873SUladzislau Rezki return nr_allocated; 300812b9f873SUladzislau Rezki } 300912b9f873SUladzislau Rezki 3010e31d9eb5SAdrian Bunk static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, 3011121e6f32SNicholas Piggin pgprot_t prot, unsigned int page_shift, 3012121e6f32SNicholas Piggin int node) 30131da177e4SLinus Torvalds { 3014930f036bSDavid Rientjes const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; 30159376130cSMichal Hocko bool nofail = gfp_mask & __GFP_NOFAIL; 3016121e6f32SNicholas Piggin unsigned long addr = (unsigned long)area->addr; 3017121e6f32SNicholas Piggin unsigned long size = get_vm_area_size(area); 301834fe6537SAndrew Morton unsigned long array_size; 3019121e6f32SNicholas Piggin unsigned int nr_small_pages = size >> PAGE_SHIFT; 3020121e6f32SNicholas Piggin unsigned int page_order; 3021451769ebSMichal Hocko unsigned int flags; 3022451769ebSMichal Hocko int ret; 30231da177e4SLinus Torvalds 3024121e6f32SNicholas Piggin array_size = (unsigned long)nr_small_pages * sizeof(struct page *); 3025f255935bSChristoph Hellwig gfp_mask |= __GFP_NOWARN; 3026f255935bSChristoph Hellwig if (!(gfp_mask & (GFP_DMA | GFP_DMA32))) 3027f255935bSChristoph Hellwig gfp_mask |= __GFP_HIGHMEM; 30281da177e4SLinus Torvalds 30291da177e4SLinus Torvalds /* Please note that the recursion is strictly bounded. */ 30308757d5faSJan Kiszka if (array_size > PAGE_SIZE) { 30315c1f4e69SUladzislau Rezki (Sony) area->pages = __vmalloc_node(array_size, 1, nested_gfp, node, 3032f255935bSChristoph Hellwig area->caller); 3033286e1ea3SAndrew Morton } else { 30345c1f4e69SUladzislau Rezki (Sony) area->pages = kmalloc_node(array_size, nested_gfp, node); 3035286e1ea3SAndrew Morton } 30367ea36242SAustin Kim 30375c1f4e69SUladzislau Rezki (Sony) if (!area->pages) { 3038c3d77172SUladzislau Rezki (Sony) warn_alloc(gfp_mask, NULL, 3039f4bdfeafSUladzislau Rezki (Sony) "vmalloc error: size %lu, failed to allocated page array size %lu", 3040d70bec8cSNicholas Piggin nr_small_pages * PAGE_SIZE, array_size); 3041cd61413bSUladzislau Rezki (Sony) free_vm_area(area); 30421da177e4SLinus Torvalds return NULL; 30431da177e4SLinus Torvalds } 30441da177e4SLinus Torvalds 3045121e6f32SNicholas Piggin set_vm_area_page_order(area, page_shift - PAGE_SHIFT); 3046121e6f32SNicholas Piggin page_order = vm_area_page_order(area); 3047121e6f32SNicholas Piggin 3048c3d77172SUladzislau Rezki (Sony) area->nr_pages = vm_area_alloc_pages(gfp_mask | __GFP_NOWARN, 3049c3d77172SUladzislau Rezki (Sony) node, page_order, nr_small_pages, area->pages); 30505c1f4e69SUladzislau Rezki (Sony) 305197105f0aSRoman Gushchin atomic_long_add(area->nr_pages, &nr_vmalloc_pages); 30524e5aa1f4SShakeel Butt if (gfp_mask & __GFP_ACCOUNT) { 30533b8000aeSNicholas Piggin int i; 30544e5aa1f4SShakeel Butt 30553b8000aeSNicholas Piggin for (i = 0; i < area->nr_pages; i++) 30563b8000aeSNicholas Piggin mod_memcg_page_state(area->pages[i], MEMCG_VMALLOC, 1); 30574e5aa1f4SShakeel Butt } 30585c1f4e69SUladzislau Rezki (Sony) 30595c1f4e69SUladzislau Rezki (Sony) /* 30605c1f4e69SUladzislau Rezki (Sony) * If not enough pages were obtained to accomplish an 30615c1f4e69SUladzislau Rezki (Sony) * allocation request, free them via __vfree() if any. 30625c1f4e69SUladzislau Rezki (Sony) */ 30635c1f4e69SUladzislau Rezki (Sony) if (area->nr_pages != nr_small_pages) { 3064c3d77172SUladzislau Rezki (Sony) warn_alloc(gfp_mask, NULL, 3065f4bdfeafSUladzislau Rezki (Sony) "vmalloc error: size %lu, page order %u, failed to allocate pages", 3066d70bec8cSNicholas Piggin area->nr_pages * PAGE_SIZE, page_order); 30671da177e4SLinus Torvalds goto fail; 30681da177e4SLinus Torvalds } 3069121e6f32SNicholas Piggin 3070451769ebSMichal Hocko /* 3071451769ebSMichal Hocko * page tables allocations ignore external gfp mask, enforce it 3072451769ebSMichal Hocko * by the scope API 3073451769ebSMichal Hocko */ 3074451769ebSMichal Hocko if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO) 3075451769ebSMichal Hocko flags = memalloc_nofs_save(); 3076451769ebSMichal Hocko else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0) 3077451769ebSMichal Hocko flags = memalloc_noio_save(); 3078451769ebSMichal Hocko 30799376130cSMichal Hocko do { 3080451769ebSMichal Hocko ret = vmap_pages_range(addr, addr + size, prot, area->pages, 3081451769ebSMichal Hocko page_shift); 30829376130cSMichal Hocko if (nofail && (ret < 0)) 30839376130cSMichal Hocko schedule_timeout_uninterruptible(1); 30849376130cSMichal Hocko } while (nofail && (ret < 0)); 3085451769ebSMichal Hocko 3086451769ebSMichal Hocko if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO) 3087451769ebSMichal Hocko memalloc_nofs_restore(flags); 3088451769ebSMichal Hocko else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0) 3089451769ebSMichal Hocko memalloc_noio_restore(flags); 3090451769ebSMichal Hocko 3091451769ebSMichal Hocko if (ret < 0) { 3092c3d77172SUladzislau Rezki (Sony) warn_alloc(gfp_mask, NULL, 3093f4bdfeafSUladzislau Rezki (Sony) "vmalloc error: size %lu, failed to map pages", 3094d70bec8cSNicholas Piggin area->nr_pages * PAGE_SIZE); 30951da177e4SLinus Torvalds goto fail; 3096d70bec8cSNicholas Piggin } 3097ed1f324cSChristoph Hellwig 30981da177e4SLinus Torvalds return area->addr; 30991da177e4SLinus Torvalds 31001da177e4SLinus Torvalds fail: 3101c67dc624SRoman Penyaev __vfree(area->addr); 31021da177e4SLinus Torvalds return NULL; 31031da177e4SLinus Torvalds } 31041da177e4SLinus Torvalds 3105d0a21265SDavid Rientjes /** 3106d0a21265SDavid Rientjes * __vmalloc_node_range - allocate virtually contiguous memory 3107d0a21265SDavid Rientjes * @size: allocation size 3108d0a21265SDavid Rientjes * @align: desired alignment 3109d0a21265SDavid Rientjes * @start: vm area range start 3110d0a21265SDavid Rientjes * @end: vm area range end 3111d0a21265SDavid Rientjes * @gfp_mask: flags for the page level allocator 3112d0a21265SDavid Rientjes * @prot: protection mask for the allocated pages 3113cb9e3c29SAndrey Ryabinin * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD) 311400ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 3115d0a21265SDavid Rientjes * @caller: caller's return address 3116d0a21265SDavid Rientjes * 3117d0a21265SDavid Rientjes * Allocate enough pages to cover @size from the page level 3118b7d90e7aSMichal Hocko * allocator with @gfp_mask flags. Please note that the full set of gfp 311930d3f011SMichal Hocko * flags are not supported. GFP_KERNEL, GFP_NOFS and GFP_NOIO are all 312030d3f011SMichal Hocko * supported. 312130d3f011SMichal Hocko * Zone modifiers are not supported. From the reclaim modifiers 312230d3f011SMichal Hocko * __GFP_DIRECT_RECLAIM is required (aka GFP_NOWAIT is not supported) 312330d3f011SMichal Hocko * and only __GFP_NOFAIL is supported (i.e. __GFP_NORETRY and 312430d3f011SMichal Hocko * __GFP_RETRY_MAYFAIL are not supported). 312530d3f011SMichal Hocko * 312630d3f011SMichal Hocko * __GFP_NOWARN can be used to suppress failures messages. 3127b7d90e7aSMichal Hocko * 3128b7d90e7aSMichal Hocko * Map them into contiguous kernel virtual space, using a pagetable 3129b7d90e7aSMichal Hocko * protection of @prot. 3130a862f68aSMike Rapoport * 3131a862f68aSMike Rapoport * Return: the address of the area or %NULL on failure 3132d0a21265SDavid Rientjes */ 3133d0a21265SDavid Rientjes void *__vmalloc_node_range(unsigned long size, unsigned long align, 3134d0a21265SDavid Rientjes unsigned long start, unsigned long end, gfp_t gfp_mask, 3135cb9e3c29SAndrey Ryabinin pgprot_t prot, unsigned long vm_flags, int node, 3136cb9e3c29SAndrey Ryabinin const void *caller) 3137930fc45aSChristoph Lameter { 3138d0a21265SDavid Rientjes struct vm_struct *area; 313919f1c3acSAndrey Konovalov void *ret; 3140f6e39794SAndrey Konovalov kasan_vmalloc_flags_t kasan_flags = KASAN_VMALLOC_NONE; 3141d0a21265SDavid Rientjes unsigned long real_size = size; 3142121e6f32SNicholas Piggin unsigned long real_align = align; 3143121e6f32SNicholas Piggin unsigned int shift = PAGE_SHIFT; 3144d0a21265SDavid Rientjes 3145d70bec8cSNicholas Piggin if (WARN_ON_ONCE(!size)) 3146d70bec8cSNicholas Piggin return NULL; 3147d70bec8cSNicholas Piggin 3148d70bec8cSNicholas Piggin if ((size >> PAGE_SHIFT) > totalram_pages()) { 3149d70bec8cSNicholas Piggin warn_alloc(gfp_mask, NULL, 3150f4bdfeafSUladzislau Rezki (Sony) "vmalloc error: size %lu, exceeds total pages", 3151f4bdfeafSUladzislau Rezki (Sony) real_size); 3152d70bec8cSNicholas Piggin return NULL; 3153121e6f32SNicholas Piggin } 3154d0a21265SDavid Rientjes 3155559089e0SSong Liu if (vmap_allow_huge && (vm_flags & VM_ALLOW_HUGE_VMAP)) { 3156121e6f32SNicholas Piggin unsigned long size_per_node; 3157121e6f32SNicholas Piggin 3158121e6f32SNicholas Piggin /* 3159121e6f32SNicholas Piggin * Try huge pages. Only try for PAGE_KERNEL allocations, 3160121e6f32SNicholas Piggin * others like modules don't yet expect huge pages in 3161121e6f32SNicholas Piggin * their allocations due to apply_to_page_range not 3162121e6f32SNicholas Piggin * supporting them. 3163121e6f32SNicholas Piggin */ 3164121e6f32SNicholas Piggin 3165121e6f32SNicholas Piggin size_per_node = size; 3166121e6f32SNicholas Piggin if (node == NUMA_NO_NODE) 3167121e6f32SNicholas Piggin size_per_node /= num_online_nodes(); 31683382bbeeSChristophe Leroy if (arch_vmap_pmd_supported(prot) && size_per_node >= PMD_SIZE) 3169121e6f32SNicholas Piggin shift = PMD_SHIFT; 31703382bbeeSChristophe Leroy else 31713382bbeeSChristophe Leroy shift = arch_vmap_pte_supported_shift(size_per_node); 31723382bbeeSChristophe Leroy 3173121e6f32SNicholas Piggin align = max(real_align, 1UL << shift); 3174121e6f32SNicholas Piggin size = ALIGN(real_size, 1UL << shift); 3175121e6f32SNicholas Piggin } 3176121e6f32SNicholas Piggin 3177121e6f32SNicholas Piggin again: 31787ca3027bSDaniel Axtens area = __get_vm_area_node(real_size, align, shift, VM_ALLOC | 31797ca3027bSDaniel Axtens VM_UNINITIALIZED | vm_flags, start, end, node, 31807ca3027bSDaniel Axtens gfp_mask, caller); 3181d70bec8cSNicholas Piggin if (!area) { 31829376130cSMichal Hocko bool nofail = gfp_mask & __GFP_NOFAIL; 3183d70bec8cSNicholas Piggin warn_alloc(gfp_mask, NULL, 31849376130cSMichal Hocko "vmalloc error: size %lu, vm_struct allocation failed%s", 31859376130cSMichal Hocko real_size, (nofail) ? ". Retrying." : ""); 31869376130cSMichal Hocko if (nofail) { 31879376130cSMichal Hocko schedule_timeout_uninterruptible(1); 31889376130cSMichal Hocko goto again; 31899376130cSMichal Hocko } 3190de7d2b56SJoe Perches goto fail; 3191d70bec8cSNicholas Piggin } 3192d0a21265SDavid Rientjes 3193f6e39794SAndrey Konovalov /* 3194f6e39794SAndrey Konovalov * Prepare arguments for __vmalloc_area_node() and 3195f6e39794SAndrey Konovalov * kasan_unpoison_vmalloc(). 3196f6e39794SAndrey Konovalov */ 3197f6e39794SAndrey Konovalov if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) { 3198f6e39794SAndrey Konovalov if (kasan_hw_tags_enabled()) { 319901d92c7fSAndrey Konovalov /* 320001d92c7fSAndrey Konovalov * Modify protection bits to allow tagging. 3201f6e39794SAndrey Konovalov * This must be done before mapping. 320201d92c7fSAndrey Konovalov */ 320301d92c7fSAndrey Konovalov prot = arch_vmap_pgprot_tagged(prot); 320401d92c7fSAndrey Konovalov 320523689e91SAndrey Konovalov /* 3206f6e39794SAndrey Konovalov * Skip page_alloc poisoning and zeroing for physical 3207f6e39794SAndrey Konovalov * pages backing VM_ALLOC mapping. Memory is instead 3208f6e39794SAndrey Konovalov * poisoned and zeroed by kasan_unpoison_vmalloc(). 320923689e91SAndrey Konovalov */ 321023689e91SAndrey Konovalov gfp_mask |= __GFP_SKIP_KASAN_UNPOISON | __GFP_SKIP_ZERO; 321123689e91SAndrey Konovalov } 321223689e91SAndrey Konovalov 3213f6e39794SAndrey Konovalov /* Take note that the mapping is PAGE_KERNEL. */ 3214f6e39794SAndrey Konovalov kasan_flags |= KASAN_VMALLOC_PROT_NORMAL; 3215f6e39794SAndrey Konovalov } 3216f6e39794SAndrey Konovalov 321701d92c7fSAndrey Konovalov /* Allocate physical pages and map them into vmalloc space. */ 321819f1c3acSAndrey Konovalov ret = __vmalloc_area_node(area, gfp_mask, prot, shift, node); 321919f1c3acSAndrey Konovalov if (!ret) 3220121e6f32SNicholas Piggin goto fail; 322189219d37SCatalin Marinas 322223689e91SAndrey Konovalov /* 322323689e91SAndrey Konovalov * Mark the pages as accessible, now that they are mapped. 32246c2f761dSAndrey Konovalov * The condition for setting KASAN_VMALLOC_INIT should complement the 32256c2f761dSAndrey Konovalov * one in post_alloc_hook() with regards to the __GFP_SKIP_ZERO check 32266c2f761dSAndrey Konovalov * to make sure that memory is initialized under the same conditions. 3227f6e39794SAndrey Konovalov * Tag-based KASAN modes only assign tags to normal non-executable 3228f6e39794SAndrey Konovalov * allocations, see __kasan_unpoison_vmalloc(). 322923689e91SAndrey Konovalov */ 3230f6e39794SAndrey Konovalov kasan_flags |= KASAN_VMALLOC_VM_ALLOC; 32316c2f761dSAndrey Konovalov if (!want_init_on_free() && want_init_on_alloc(gfp_mask) && 32326c2f761dSAndrey Konovalov (gfp_mask & __GFP_SKIP_ZERO)) 323323689e91SAndrey Konovalov kasan_flags |= KASAN_VMALLOC_INIT; 3234f6e39794SAndrey Konovalov /* KASAN_VMALLOC_PROT_NORMAL already set if required. */ 323523689e91SAndrey Konovalov area->addr = kasan_unpoison_vmalloc(area->addr, real_size, kasan_flags); 323619f1c3acSAndrey Konovalov 323789219d37SCatalin Marinas /* 323820fc02b4SZhang Yanfei * In this function, newly allocated vm_struct has VM_UNINITIALIZED 323920fc02b4SZhang Yanfei * flag. It means that vm_struct is not fully initialized. 32404341fa45SJoonsoo Kim * Now, it is fully initialized, so remove this flag here. 3241f5252e00SMitsuo Hayasaka */ 324220fc02b4SZhang Yanfei clear_vm_uninitialized_flag(area); 3243f5252e00SMitsuo Hayasaka 32447ca3027bSDaniel Axtens size = PAGE_ALIGN(size); 324560115fa5SKefeng Wang if (!(vm_flags & VM_DEFER_KMEMLEAK)) 324694f4a161SCatalin Marinas kmemleak_vmalloc(area, size, gfp_mask); 324789219d37SCatalin Marinas 324819f1c3acSAndrey Konovalov return area->addr; 3249de7d2b56SJoe Perches 3250de7d2b56SJoe Perches fail: 3251121e6f32SNicholas Piggin if (shift > PAGE_SHIFT) { 3252121e6f32SNicholas Piggin shift = PAGE_SHIFT; 3253121e6f32SNicholas Piggin align = real_align; 3254121e6f32SNicholas Piggin size = real_size; 3255121e6f32SNicholas Piggin goto again; 3256121e6f32SNicholas Piggin } 3257121e6f32SNicholas Piggin 3258de7d2b56SJoe Perches return NULL; 3259930fc45aSChristoph Lameter } 3260930fc45aSChristoph Lameter 32611da177e4SLinus Torvalds /** 3262930fc45aSChristoph Lameter * __vmalloc_node - allocate virtually contiguous memory 32631da177e4SLinus Torvalds * @size: allocation size 32642dca6999SDavid Miller * @align: desired alignment 32651da177e4SLinus Torvalds * @gfp_mask: flags for the page level allocator 326600ef2d2fSDavid Rientjes * @node: node to use for allocation or NUMA_NO_NODE 3267c85d194bSRandy Dunlap * @caller: caller's return address 32681da177e4SLinus Torvalds * 3269f38fcb9cSChristoph Hellwig * Allocate enough pages to cover @size from the page level allocator with 3270f38fcb9cSChristoph Hellwig * @gfp_mask flags. Map them into contiguous kernel virtual space. 3271a7c3e901SMichal Hocko * 3272dcda9b04SMichal Hocko * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL 3273a7c3e901SMichal Hocko * and __GFP_NOFAIL are not supported 3274a7c3e901SMichal Hocko * 3275a7c3e901SMichal Hocko * Any use of gfp flags outside of GFP_KERNEL should be consulted 3276a7c3e901SMichal Hocko * with mm people. 3277a862f68aSMike Rapoport * 3278a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 32791da177e4SLinus Torvalds */ 32802b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align, 3281f38fcb9cSChristoph Hellwig gfp_t gfp_mask, int node, const void *caller) 32821da177e4SLinus Torvalds { 3283d0a21265SDavid Rientjes return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, 3284f38fcb9cSChristoph Hellwig gfp_mask, PAGE_KERNEL, 0, node, caller); 32851da177e4SLinus Torvalds } 3286c3f896dcSChristoph Hellwig /* 3287c3f896dcSChristoph Hellwig * This is only for performance analysis of vmalloc and stress purpose. 3288c3f896dcSChristoph Hellwig * It is required by vmalloc test module, therefore do not use it other 3289c3f896dcSChristoph Hellwig * than that. 3290c3f896dcSChristoph Hellwig */ 3291c3f896dcSChristoph Hellwig #ifdef CONFIG_TEST_VMALLOC_MODULE 3292c3f896dcSChristoph Hellwig EXPORT_SYMBOL_GPL(__vmalloc_node); 3293c3f896dcSChristoph Hellwig #endif 32941da177e4SLinus Torvalds 329588dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask) 3296930fc45aSChristoph Lameter { 3297f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE, 329823016969SChristoph Lameter __builtin_return_address(0)); 3299930fc45aSChristoph Lameter } 33001da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc); 33011da177e4SLinus Torvalds 33021da177e4SLinus Torvalds /** 33031da177e4SLinus Torvalds * vmalloc - allocate virtually contiguous memory 33041da177e4SLinus Torvalds * @size: allocation size 330592eac168SMike Rapoport * 33061da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 33071da177e4SLinus Torvalds * allocator and map them into contiguous kernel virtual space. 33081da177e4SLinus Torvalds * 3309c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 33101da177e4SLinus Torvalds * use __vmalloc() instead. 3311a862f68aSMike Rapoport * 3312a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 33131da177e4SLinus Torvalds */ 33141da177e4SLinus Torvalds void *vmalloc(unsigned long size) 33151da177e4SLinus Torvalds { 33164d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE, 33174d39d728SChristoph Hellwig __builtin_return_address(0)); 33181da177e4SLinus Torvalds } 33191da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc); 33201da177e4SLinus Torvalds 3321930fc45aSChristoph Lameter /** 3322559089e0SSong Liu * vmalloc_huge - allocate virtually contiguous memory, allow huge pages 332315a64f5aSClaudio Imbrenda * @size: allocation size 3324559089e0SSong Liu * @gfp_mask: flags for the page level allocator 332515a64f5aSClaudio Imbrenda * 3326559089e0SSong Liu * Allocate enough pages to cover @size from the page level 332715a64f5aSClaudio Imbrenda * allocator and map them into contiguous kernel virtual space. 3328559089e0SSong Liu * If @size is greater than or equal to PMD_SIZE, allow using 3329559089e0SSong Liu * huge pages for the memory 333015a64f5aSClaudio Imbrenda * 333115a64f5aSClaudio Imbrenda * Return: pointer to the allocated memory or %NULL on error 333215a64f5aSClaudio Imbrenda */ 3333559089e0SSong Liu void *vmalloc_huge(unsigned long size, gfp_t gfp_mask) 333415a64f5aSClaudio Imbrenda { 333515a64f5aSClaudio Imbrenda return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, 3336559089e0SSong Liu gfp_mask, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP, 333715a64f5aSClaudio Imbrenda NUMA_NO_NODE, __builtin_return_address(0)); 333815a64f5aSClaudio Imbrenda } 3339559089e0SSong Liu EXPORT_SYMBOL_GPL(vmalloc_huge); 334015a64f5aSClaudio Imbrenda 334115a64f5aSClaudio Imbrenda /** 3342e1ca7788SDave Young * vzalloc - allocate virtually contiguous memory with zero fill 3343e1ca7788SDave Young * @size: allocation size 334492eac168SMike Rapoport * 3345e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 3346e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 3347e1ca7788SDave Young * The memory allocated is set to zero. 3348e1ca7788SDave Young * 3349e1ca7788SDave Young * For tight control over page level allocator and protection flags 3350e1ca7788SDave Young * use __vmalloc() instead. 3351a862f68aSMike Rapoport * 3352a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 3353e1ca7788SDave Young */ 3354e1ca7788SDave Young void *vzalloc(unsigned long size) 3355e1ca7788SDave Young { 33564d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE, 33574d39d728SChristoph Hellwig __builtin_return_address(0)); 3358e1ca7788SDave Young } 3359e1ca7788SDave Young EXPORT_SYMBOL(vzalloc); 3360e1ca7788SDave Young 3361e1ca7788SDave Young /** 3362ead04089SRolf Eike Beer * vmalloc_user - allocate zeroed virtually contiguous memory for userspace 336383342314SNick Piggin * @size: allocation size 3364ead04089SRolf Eike Beer * 3365ead04089SRolf Eike Beer * The resulting memory area is zeroed so it can be mapped to userspace 3366ead04089SRolf Eike Beer * without leaking data. 3367a862f68aSMike Rapoport * 3368a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 336983342314SNick Piggin */ 337083342314SNick Piggin void *vmalloc_user(unsigned long size) 337183342314SNick Piggin { 3372bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 3373bc84c535SRoman Penyaev GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL, 3374bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 337500ef2d2fSDavid Rientjes __builtin_return_address(0)); 337683342314SNick Piggin } 337783342314SNick Piggin EXPORT_SYMBOL(vmalloc_user); 337883342314SNick Piggin 337983342314SNick Piggin /** 3380930fc45aSChristoph Lameter * vmalloc_node - allocate memory on a specific node 3381930fc45aSChristoph Lameter * @size: allocation size 3382d44e0780SRandy Dunlap * @node: numa node 3383930fc45aSChristoph Lameter * 3384930fc45aSChristoph Lameter * Allocate enough pages to cover @size from the page level 3385930fc45aSChristoph Lameter * allocator and map them into contiguous kernel virtual space. 3386930fc45aSChristoph Lameter * 3387c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 3388930fc45aSChristoph Lameter * use __vmalloc() instead. 3389a862f68aSMike Rapoport * 3390a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 3391930fc45aSChristoph Lameter */ 3392930fc45aSChristoph Lameter void *vmalloc_node(unsigned long size, int node) 3393930fc45aSChristoph Lameter { 3394f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL, node, 3395f38fcb9cSChristoph Hellwig __builtin_return_address(0)); 3396930fc45aSChristoph Lameter } 3397930fc45aSChristoph Lameter EXPORT_SYMBOL(vmalloc_node); 3398930fc45aSChristoph Lameter 3399e1ca7788SDave Young /** 3400e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill 3401e1ca7788SDave Young * @size: allocation size 3402e1ca7788SDave Young * @node: numa node 3403e1ca7788SDave Young * 3404e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 3405e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 3406e1ca7788SDave Young * The memory allocated is set to zero. 3407e1ca7788SDave Young * 3408a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 3409e1ca7788SDave Young */ 3410e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node) 3411e1ca7788SDave Young { 34124d39d728SChristoph Hellwig return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node, 34134d39d728SChristoph Hellwig __builtin_return_address(0)); 3414e1ca7788SDave Young } 3415e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node); 3416e1ca7788SDave Young 34170d08e0d3SAndi Kleen #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) 3418698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) 34190d08e0d3SAndi Kleen #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) 3420698d0831SMichal Hocko #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL) 34210d08e0d3SAndi Kleen #else 3422698d0831SMichal Hocko /* 3423698d0831SMichal Hocko * 64b systems should always have either DMA or DMA32 zones. For others 3424698d0831SMichal Hocko * GFP_DMA32 should do the right thing and use the normal zone. 3425698d0831SMichal Hocko */ 342668d68ff6SZhiyuan Dai #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) 34270d08e0d3SAndi Kleen #endif 34280d08e0d3SAndi Kleen 34291da177e4SLinus Torvalds /** 34301da177e4SLinus Torvalds * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 34311da177e4SLinus Torvalds * @size: allocation size 34321da177e4SLinus Torvalds * 34331da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 34341da177e4SLinus Torvalds * page level allocator and map them into contiguous kernel virtual space. 3435a862f68aSMike Rapoport * 3436a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 34371da177e4SLinus Torvalds */ 34381da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 34391da177e4SLinus Torvalds { 3440f38fcb9cSChristoph Hellwig return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE, 3441f38fcb9cSChristoph Hellwig __builtin_return_address(0)); 34421da177e4SLinus Torvalds } 34431da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32); 34441da177e4SLinus Torvalds 344583342314SNick Piggin /** 3446ead04089SRolf Eike Beer * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 344783342314SNick Piggin * @size: allocation size 3448ead04089SRolf Eike Beer * 3449ead04089SRolf Eike Beer * The resulting memory area is 32bit addressable and zeroed so it can be 3450ead04089SRolf Eike Beer * mapped to userspace without leaking data. 3451a862f68aSMike Rapoport * 3452a862f68aSMike Rapoport * Return: pointer to the allocated memory or %NULL on error 345383342314SNick Piggin */ 345483342314SNick Piggin void *vmalloc_32_user(unsigned long size) 345583342314SNick Piggin { 3456bc84c535SRoman Penyaev return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, 3457bc84c535SRoman Penyaev GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, 3458bc84c535SRoman Penyaev VM_USERMAP, NUMA_NO_NODE, 34595a82ac71SRoman Penyaev __builtin_return_address(0)); 346083342314SNick Piggin } 346183342314SNick Piggin EXPORT_SYMBOL(vmalloc_32_user); 346283342314SNick Piggin 3463d0107eb0SKAMEZAWA Hiroyuki /* 3464d0107eb0SKAMEZAWA Hiroyuki * small helper routine , copy contents to buf from addr. 3465d0107eb0SKAMEZAWA Hiroyuki * If the page is not present, fill zero. 3466d0107eb0SKAMEZAWA Hiroyuki */ 3467d0107eb0SKAMEZAWA Hiroyuki 3468d0107eb0SKAMEZAWA Hiroyuki static int aligned_vread(char *buf, char *addr, unsigned long count) 3469d0107eb0SKAMEZAWA Hiroyuki { 3470d0107eb0SKAMEZAWA Hiroyuki struct page *p; 3471d0107eb0SKAMEZAWA Hiroyuki int copied = 0; 3472d0107eb0SKAMEZAWA Hiroyuki 3473d0107eb0SKAMEZAWA Hiroyuki while (count) { 3474d0107eb0SKAMEZAWA Hiroyuki unsigned long offset, length; 3475d0107eb0SKAMEZAWA Hiroyuki 3476891c49abSAlexander Kuleshov offset = offset_in_page(addr); 3477d0107eb0SKAMEZAWA Hiroyuki length = PAGE_SIZE - offset; 3478d0107eb0SKAMEZAWA Hiroyuki if (length > count) 3479d0107eb0SKAMEZAWA Hiroyuki length = count; 3480d0107eb0SKAMEZAWA Hiroyuki p = vmalloc_to_page(addr); 3481d0107eb0SKAMEZAWA Hiroyuki /* 3482d0107eb0SKAMEZAWA Hiroyuki * To do safe access to this _mapped_ area, we need 3483d0107eb0SKAMEZAWA Hiroyuki * lock. But adding lock here means that we need to add 3484f0953a1bSIngo Molnar * overhead of vmalloc()/vfree() calls for this _debug_ 3485d0107eb0SKAMEZAWA Hiroyuki * interface, rarely used. Instead of that, we'll use 3486d0107eb0SKAMEZAWA Hiroyuki * kmap() and get small overhead in this access function. 3487d0107eb0SKAMEZAWA Hiroyuki */ 3488d0107eb0SKAMEZAWA Hiroyuki if (p) { 3489f7c8ce44SDavid Hildenbrand /* We can expect USER0 is not used -- see vread() */ 34909b04c5feSCong Wang void *map = kmap_atomic(p); 3491d0107eb0SKAMEZAWA Hiroyuki memcpy(buf, map + offset, length); 34929b04c5feSCong Wang kunmap_atomic(map); 3493d0107eb0SKAMEZAWA Hiroyuki } else 3494d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, length); 3495d0107eb0SKAMEZAWA Hiroyuki 3496d0107eb0SKAMEZAWA Hiroyuki addr += length; 3497d0107eb0SKAMEZAWA Hiroyuki buf += length; 3498d0107eb0SKAMEZAWA Hiroyuki copied += length; 3499d0107eb0SKAMEZAWA Hiroyuki count -= length; 3500d0107eb0SKAMEZAWA Hiroyuki } 3501d0107eb0SKAMEZAWA Hiroyuki return copied; 3502d0107eb0SKAMEZAWA Hiroyuki } 3503d0107eb0SKAMEZAWA Hiroyuki 3504d0107eb0SKAMEZAWA Hiroyuki /** 3505d0107eb0SKAMEZAWA Hiroyuki * vread() - read vmalloc area in a safe way. 3506d0107eb0SKAMEZAWA Hiroyuki * @buf: buffer for reading data 3507d0107eb0SKAMEZAWA Hiroyuki * @addr: vm address. 3508d0107eb0SKAMEZAWA Hiroyuki * @count: number of bytes to be read. 3509d0107eb0SKAMEZAWA Hiroyuki * 3510d0107eb0SKAMEZAWA Hiroyuki * This function checks that addr is a valid vmalloc'ed area, and 3511d0107eb0SKAMEZAWA Hiroyuki * copy data from that area to a given buffer. If the given memory range 3512d0107eb0SKAMEZAWA Hiroyuki * of [addr...addr+count) includes some valid address, data is copied to 3513d0107eb0SKAMEZAWA Hiroyuki * proper area of @buf. If there are memory holes, they'll be zero-filled. 3514d0107eb0SKAMEZAWA Hiroyuki * IOREMAP area is treated as memory hole and no copy is done. 3515d0107eb0SKAMEZAWA Hiroyuki * 3516d0107eb0SKAMEZAWA Hiroyuki * If [addr...addr+count) doesn't includes any intersects with alive 3517a8e5202dSCong Wang * vm_struct area, returns 0. @buf should be kernel's buffer. 3518d0107eb0SKAMEZAWA Hiroyuki * 3519d0107eb0SKAMEZAWA Hiroyuki * Note: In usual ops, vread() is never necessary because the caller 3520d0107eb0SKAMEZAWA Hiroyuki * should know vmalloc() area is valid and can use memcpy(). 3521d0107eb0SKAMEZAWA Hiroyuki * This is for routines which have to access vmalloc area without 3522bbcd53c9SDavid Hildenbrand * any information, as /proc/kcore. 3523a862f68aSMike Rapoport * 3524a862f68aSMike Rapoport * Return: number of bytes for which addr and buf should be increased 3525a862f68aSMike Rapoport * (same number as @count) or %0 if [addr...addr+count) doesn't 3526a862f68aSMike Rapoport * include any intersection with valid vmalloc area 3527d0107eb0SKAMEZAWA Hiroyuki */ 35281da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 35291da177e4SLinus Torvalds { 3530e81ce85fSJoonsoo Kim struct vmap_area *va; 3531e81ce85fSJoonsoo Kim struct vm_struct *vm; 35321da177e4SLinus Torvalds char *vaddr, *buf_start = buf; 3533d0107eb0SKAMEZAWA Hiroyuki unsigned long buflen = count; 35341da177e4SLinus Torvalds unsigned long n; 35351da177e4SLinus Torvalds 35364aff1dc4SAndrey Konovalov addr = kasan_reset_tag(addr); 35374aff1dc4SAndrey Konovalov 35381da177e4SLinus Torvalds /* Don't allow overflow */ 35391da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 35401da177e4SLinus Torvalds count = -(unsigned long) addr; 35411da177e4SLinus Torvalds 3542e81ce85fSJoonsoo Kim spin_lock(&vmap_area_lock); 3543f181234aSChen Wandun va = find_vmap_area_exceed_addr((unsigned long)addr); 3544f608788cSSerapheim Dimitropoulos if (!va) 3545f608788cSSerapheim Dimitropoulos goto finished; 3546f181234aSChen Wandun 3547f181234aSChen Wandun /* no intersects with alive vmap_area */ 3548f181234aSChen Wandun if ((unsigned long)addr + count <= va->va_start) 3549f181234aSChen Wandun goto finished; 3550f181234aSChen Wandun 3551f608788cSSerapheim Dimitropoulos list_for_each_entry_from(va, &vmap_area_list, list) { 3552e81ce85fSJoonsoo Kim if (!count) 3553e81ce85fSJoonsoo Kim break; 3554e81ce85fSJoonsoo Kim 3555688fcbfcSPengfei Li if (!va->vm) 3556e81ce85fSJoonsoo Kim continue; 3557e81ce85fSJoonsoo Kim 3558e81ce85fSJoonsoo Kim vm = va->vm; 3559e81ce85fSJoonsoo Kim vaddr = (char *) vm->addr; 3560762216abSWanpeng Li if (addr >= vaddr + get_vm_area_size(vm)) 35611da177e4SLinus Torvalds continue; 35621da177e4SLinus Torvalds while (addr < vaddr) { 35631da177e4SLinus Torvalds if (count == 0) 35641da177e4SLinus Torvalds goto finished; 35651da177e4SLinus Torvalds *buf = '\0'; 35661da177e4SLinus Torvalds buf++; 35671da177e4SLinus Torvalds addr++; 35681da177e4SLinus Torvalds count--; 35691da177e4SLinus Torvalds } 3570762216abSWanpeng Li n = vaddr + get_vm_area_size(vm) - addr; 3571d0107eb0SKAMEZAWA Hiroyuki if (n > count) 3572d0107eb0SKAMEZAWA Hiroyuki n = count; 3573e81ce85fSJoonsoo Kim if (!(vm->flags & VM_IOREMAP)) 3574d0107eb0SKAMEZAWA Hiroyuki aligned_vread(buf, addr, n); 3575d0107eb0SKAMEZAWA Hiroyuki else /* IOREMAP area is treated as memory hole */ 3576d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, n); 3577d0107eb0SKAMEZAWA Hiroyuki buf += n; 3578d0107eb0SKAMEZAWA Hiroyuki addr += n; 3579d0107eb0SKAMEZAWA Hiroyuki count -= n; 35801da177e4SLinus Torvalds } 35811da177e4SLinus Torvalds finished: 3582e81ce85fSJoonsoo Kim spin_unlock(&vmap_area_lock); 3583d0107eb0SKAMEZAWA Hiroyuki 3584d0107eb0SKAMEZAWA Hiroyuki if (buf == buf_start) 3585d0107eb0SKAMEZAWA Hiroyuki return 0; 3586d0107eb0SKAMEZAWA Hiroyuki /* zero-fill memory holes */ 3587d0107eb0SKAMEZAWA Hiroyuki if (buf != buf_start + buflen) 3588d0107eb0SKAMEZAWA Hiroyuki memset(buf, 0, buflen - (buf - buf_start)); 3589d0107eb0SKAMEZAWA Hiroyuki 3590d0107eb0SKAMEZAWA Hiroyuki return buflen; 35911da177e4SLinus Torvalds } 35921da177e4SLinus Torvalds 3593d0107eb0SKAMEZAWA Hiroyuki /** 3594e69e9d4aSHATAYAMA Daisuke * remap_vmalloc_range_partial - map vmalloc pages to userspace 3595e69e9d4aSHATAYAMA Daisuke * @vma: vma to cover 3596e69e9d4aSHATAYAMA Daisuke * @uaddr: target user address to start at 3597e69e9d4aSHATAYAMA Daisuke * @kaddr: virtual address of vmalloc kernel memory 3598bdebd6a2SJann Horn * @pgoff: offset from @kaddr to start at 3599e69e9d4aSHATAYAMA Daisuke * @size: size of map area 3600e69e9d4aSHATAYAMA Daisuke * 3601e69e9d4aSHATAYAMA Daisuke * Returns: 0 for success, -Exxx on failure 3602e69e9d4aSHATAYAMA Daisuke * 3603e69e9d4aSHATAYAMA Daisuke * This function checks that @kaddr is a valid vmalloc'ed area, 3604e69e9d4aSHATAYAMA Daisuke * and that it is big enough to cover the range starting at 3605e69e9d4aSHATAYAMA Daisuke * @uaddr in @vma. Will return failure if that criteria isn't 3606e69e9d4aSHATAYAMA Daisuke * met. 3607e69e9d4aSHATAYAMA Daisuke * 3608e69e9d4aSHATAYAMA Daisuke * Similar to remap_pfn_range() (see mm/memory.c) 3609e69e9d4aSHATAYAMA Daisuke */ 3610e69e9d4aSHATAYAMA Daisuke int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr, 3611bdebd6a2SJann Horn void *kaddr, unsigned long pgoff, 3612bdebd6a2SJann Horn unsigned long size) 3613e69e9d4aSHATAYAMA Daisuke { 3614e69e9d4aSHATAYAMA Daisuke struct vm_struct *area; 3615bdebd6a2SJann Horn unsigned long off; 3616bdebd6a2SJann Horn unsigned long end_index; 3617bdebd6a2SJann Horn 3618bdebd6a2SJann Horn if (check_shl_overflow(pgoff, PAGE_SHIFT, &off)) 3619bdebd6a2SJann Horn return -EINVAL; 3620e69e9d4aSHATAYAMA Daisuke 3621e69e9d4aSHATAYAMA Daisuke size = PAGE_ALIGN(size); 3622e69e9d4aSHATAYAMA Daisuke 3623e69e9d4aSHATAYAMA Daisuke if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr)) 3624e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3625e69e9d4aSHATAYAMA Daisuke 3626e69e9d4aSHATAYAMA Daisuke area = find_vm_area(kaddr); 3627e69e9d4aSHATAYAMA Daisuke if (!area) 3628e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3629e69e9d4aSHATAYAMA Daisuke 3630fe9041c2SChristoph Hellwig if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT))) 3631e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3632e69e9d4aSHATAYAMA Daisuke 3633bdebd6a2SJann Horn if (check_add_overflow(size, off, &end_index) || 3634bdebd6a2SJann Horn end_index > get_vm_area_size(area)) 3635e69e9d4aSHATAYAMA Daisuke return -EINVAL; 3636bdebd6a2SJann Horn kaddr += off; 3637e69e9d4aSHATAYAMA Daisuke 3638e69e9d4aSHATAYAMA Daisuke do { 3639e69e9d4aSHATAYAMA Daisuke struct page *page = vmalloc_to_page(kaddr); 3640e69e9d4aSHATAYAMA Daisuke int ret; 3641e69e9d4aSHATAYAMA Daisuke 3642e69e9d4aSHATAYAMA Daisuke ret = vm_insert_page(vma, uaddr, page); 3643e69e9d4aSHATAYAMA Daisuke if (ret) 3644e69e9d4aSHATAYAMA Daisuke return ret; 3645e69e9d4aSHATAYAMA Daisuke 3646e69e9d4aSHATAYAMA Daisuke uaddr += PAGE_SIZE; 3647e69e9d4aSHATAYAMA Daisuke kaddr += PAGE_SIZE; 3648e69e9d4aSHATAYAMA Daisuke size -= PAGE_SIZE; 3649e69e9d4aSHATAYAMA Daisuke } while (size > 0); 3650e69e9d4aSHATAYAMA Daisuke 3651e69e9d4aSHATAYAMA Daisuke vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 3652e69e9d4aSHATAYAMA Daisuke 3653e69e9d4aSHATAYAMA Daisuke return 0; 3654e69e9d4aSHATAYAMA Daisuke } 3655e69e9d4aSHATAYAMA Daisuke 3656e69e9d4aSHATAYAMA Daisuke /** 365783342314SNick Piggin * remap_vmalloc_range - map vmalloc pages to userspace 365883342314SNick Piggin * @vma: vma to cover (map full range of vma) 365983342314SNick Piggin * @addr: vmalloc memory 366083342314SNick Piggin * @pgoff: number of pages into addr before first page to map 36617682486bSRandy Dunlap * 36627682486bSRandy Dunlap * Returns: 0 for success, -Exxx on failure 366383342314SNick Piggin * 366483342314SNick Piggin * This function checks that addr is a valid vmalloc'ed area, and 366583342314SNick Piggin * that it is big enough to cover the vma. Will return failure if 366683342314SNick Piggin * that criteria isn't met. 366783342314SNick Piggin * 366872fd4a35SRobert P. J. Day * Similar to remap_pfn_range() (see mm/memory.c) 366983342314SNick Piggin */ 367083342314SNick Piggin int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 367183342314SNick Piggin unsigned long pgoff) 367283342314SNick Piggin { 3673e69e9d4aSHATAYAMA Daisuke return remap_vmalloc_range_partial(vma, vma->vm_start, 3674bdebd6a2SJann Horn addr, pgoff, 3675e69e9d4aSHATAYAMA Daisuke vma->vm_end - vma->vm_start); 367683342314SNick Piggin } 367783342314SNick Piggin EXPORT_SYMBOL(remap_vmalloc_range); 367883342314SNick Piggin 36795f4352fbSJeremy Fitzhardinge void free_vm_area(struct vm_struct *area) 36805f4352fbSJeremy Fitzhardinge { 36815f4352fbSJeremy Fitzhardinge struct vm_struct *ret; 36825f4352fbSJeremy Fitzhardinge ret = remove_vm_area(area->addr); 36835f4352fbSJeremy Fitzhardinge BUG_ON(ret != area); 36845f4352fbSJeremy Fitzhardinge kfree(area); 36855f4352fbSJeremy Fitzhardinge } 36865f4352fbSJeremy Fitzhardinge EXPORT_SYMBOL_GPL(free_vm_area); 3687a10aa579SChristoph Lameter 36884f8b02b4STejun Heo #ifdef CONFIG_SMP 3689ca23e405STejun Heo static struct vmap_area *node_to_va(struct rb_node *n) 3690ca23e405STejun Heo { 36914583e773SGeliang Tang return rb_entry_safe(n, struct vmap_area, rb_node); 3692ca23e405STejun Heo } 3693ca23e405STejun Heo 3694ca23e405STejun Heo /** 369568ad4a33SUladzislau Rezki (Sony) * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to 369668ad4a33SUladzislau Rezki (Sony) * @addr: target address 3697ca23e405STejun Heo * 369868ad4a33SUladzislau Rezki (Sony) * Returns: vmap_area if it is found. If there is no such area 369968ad4a33SUladzislau Rezki (Sony) * the first highest(reverse order) vmap_area is returned 370068ad4a33SUladzislau Rezki (Sony) * i.e. va->va_start < addr && va->va_end < addr or NULL 370168ad4a33SUladzislau Rezki (Sony) * if there are no any areas before @addr. 3702ca23e405STejun Heo */ 370368ad4a33SUladzislau Rezki (Sony) static struct vmap_area * 370468ad4a33SUladzislau Rezki (Sony) pvm_find_va_enclose_addr(unsigned long addr) 3705ca23e405STejun Heo { 370668ad4a33SUladzislau Rezki (Sony) struct vmap_area *va, *tmp; 370768ad4a33SUladzislau Rezki (Sony) struct rb_node *n; 370868ad4a33SUladzislau Rezki (Sony) 370968ad4a33SUladzislau Rezki (Sony) n = free_vmap_area_root.rb_node; 371068ad4a33SUladzislau Rezki (Sony) va = NULL; 3711ca23e405STejun Heo 3712ca23e405STejun Heo while (n) { 371368ad4a33SUladzislau Rezki (Sony) tmp = rb_entry(n, struct vmap_area, rb_node); 371468ad4a33SUladzislau Rezki (Sony) if (tmp->va_start <= addr) { 371568ad4a33SUladzislau Rezki (Sony) va = tmp; 371668ad4a33SUladzislau Rezki (Sony) if (tmp->va_end >= addr) 3717ca23e405STejun Heo break; 3718ca23e405STejun Heo 371968ad4a33SUladzislau Rezki (Sony) n = n->rb_right; 3720ca23e405STejun Heo } else { 372168ad4a33SUladzislau Rezki (Sony) n = n->rb_left; 3722ca23e405STejun Heo } 372368ad4a33SUladzislau Rezki (Sony) } 372468ad4a33SUladzislau Rezki (Sony) 372568ad4a33SUladzislau Rezki (Sony) return va; 3726ca23e405STejun Heo } 3727ca23e405STejun Heo 3728ca23e405STejun Heo /** 372968ad4a33SUladzislau Rezki (Sony) * pvm_determine_end_from_reverse - find the highest aligned address 373068ad4a33SUladzislau Rezki (Sony) * of free block below VMALLOC_END 373168ad4a33SUladzislau Rezki (Sony) * @va: 373268ad4a33SUladzislau Rezki (Sony) * in - the VA we start the search(reverse order); 373368ad4a33SUladzislau Rezki (Sony) * out - the VA with the highest aligned end address. 3734799fa85dSAlex Shi * @align: alignment for required highest address 3735ca23e405STejun Heo * 373668ad4a33SUladzislau Rezki (Sony) * Returns: determined end address within vmap_area 3737ca23e405STejun Heo */ 373868ad4a33SUladzislau Rezki (Sony) static unsigned long 373968ad4a33SUladzislau Rezki (Sony) pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align) 3740ca23e405STejun Heo { 374168ad4a33SUladzislau Rezki (Sony) unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 3742ca23e405STejun Heo unsigned long addr; 3743ca23e405STejun Heo 374468ad4a33SUladzislau Rezki (Sony) if (likely(*va)) { 374568ad4a33SUladzislau Rezki (Sony) list_for_each_entry_from_reverse((*va), 374668ad4a33SUladzislau Rezki (Sony) &free_vmap_area_list, list) { 374768ad4a33SUladzislau Rezki (Sony) addr = min((*va)->va_end & ~(align - 1), vmalloc_end); 374868ad4a33SUladzislau Rezki (Sony) if ((*va)->va_start < addr) 374968ad4a33SUladzislau Rezki (Sony) return addr; 375068ad4a33SUladzislau Rezki (Sony) } 3751ca23e405STejun Heo } 3752ca23e405STejun Heo 375368ad4a33SUladzislau Rezki (Sony) return 0; 3754ca23e405STejun Heo } 3755ca23e405STejun Heo 3756ca23e405STejun Heo /** 3757ca23e405STejun Heo * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator 3758ca23e405STejun Heo * @offsets: array containing offset of each area 3759ca23e405STejun Heo * @sizes: array containing size of each area 3760ca23e405STejun Heo * @nr_vms: the number of areas to allocate 3761ca23e405STejun Heo * @align: alignment, all entries in @offsets and @sizes must be aligned to this 3762ca23e405STejun Heo * 3763ca23e405STejun Heo * Returns: kmalloc'd vm_struct pointer array pointing to allocated 3764ca23e405STejun Heo * vm_structs on success, %NULL on failure 3765ca23e405STejun Heo * 3766ca23e405STejun Heo * Percpu allocator wants to use congruent vm areas so that it can 3767ca23e405STejun Heo * maintain the offsets among percpu areas. This function allocates 3768ec3f64fcSDavid Rientjes * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to 3769ec3f64fcSDavid Rientjes * be scattered pretty far, distance between two areas easily going up 3770ec3f64fcSDavid Rientjes * to gigabytes. To avoid interacting with regular vmallocs, these 3771ec3f64fcSDavid Rientjes * areas are allocated from top. 3772ca23e405STejun Heo * 3773ca23e405STejun Heo * Despite its complicated look, this allocator is rather simple. It 377468ad4a33SUladzislau Rezki (Sony) * does everything top-down and scans free blocks from the end looking 377568ad4a33SUladzislau Rezki (Sony) * for matching base. While scanning, if any of the areas do not fit the 377668ad4a33SUladzislau Rezki (Sony) * base address is pulled down to fit the area. Scanning is repeated till 377768ad4a33SUladzislau Rezki (Sony) * all the areas fit and then all necessary data structures are inserted 377868ad4a33SUladzislau Rezki (Sony) * and the result is returned. 3779ca23e405STejun Heo */ 3780ca23e405STejun Heo struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, 3781ca23e405STejun Heo const size_t *sizes, int nr_vms, 3782ec3f64fcSDavid Rientjes size_t align) 3783ca23e405STejun Heo { 3784ca23e405STejun Heo const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); 3785ca23e405STejun Heo const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); 378668ad4a33SUladzislau Rezki (Sony) struct vmap_area **vas, *va; 3787ca23e405STejun Heo struct vm_struct **vms; 3788ca23e405STejun Heo int area, area2, last_area, term_area; 3789253a496dSDaniel Axtens unsigned long base, start, size, end, last_end, orig_start, orig_end; 3790ca23e405STejun Heo bool purged = false; 3791ca23e405STejun Heo 3792ca23e405STejun Heo /* verify parameters and allocate data structures */ 3793891c49abSAlexander Kuleshov BUG_ON(offset_in_page(align) || !is_power_of_2(align)); 3794ca23e405STejun Heo for (last_area = 0, area = 0; area < nr_vms; area++) { 3795ca23e405STejun Heo start = offsets[area]; 3796ca23e405STejun Heo end = start + sizes[area]; 3797ca23e405STejun Heo 3798ca23e405STejun Heo /* is everything aligned properly? */ 3799ca23e405STejun Heo BUG_ON(!IS_ALIGNED(offsets[area], align)); 3800ca23e405STejun Heo BUG_ON(!IS_ALIGNED(sizes[area], align)); 3801ca23e405STejun Heo 3802ca23e405STejun Heo /* detect the area with the highest address */ 3803ca23e405STejun Heo if (start > offsets[last_area]) 3804ca23e405STejun Heo last_area = area; 3805ca23e405STejun Heo 3806c568da28SWei Yang for (area2 = area + 1; area2 < nr_vms; area2++) { 3807ca23e405STejun Heo unsigned long start2 = offsets[area2]; 3808ca23e405STejun Heo unsigned long end2 = start2 + sizes[area2]; 3809ca23e405STejun Heo 3810c568da28SWei Yang BUG_ON(start2 < end && start < end2); 3811ca23e405STejun Heo } 3812ca23e405STejun Heo } 3813ca23e405STejun Heo last_end = offsets[last_area] + sizes[last_area]; 3814ca23e405STejun Heo 3815ca23e405STejun Heo if (vmalloc_end - vmalloc_start < last_end) { 3816ca23e405STejun Heo WARN_ON(true); 3817ca23e405STejun Heo return NULL; 3818ca23e405STejun Heo } 3819ca23e405STejun Heo 38204d67d860SThomas Meyer vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL); 38214d67d860SThomas Meyer vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL); 3822ca23e405STejun Heo if (!vas || !vms) 3823f1db7afdSKautuk Consul goto err_free2; 3824ca23e405STejun Heo 3825ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 382668ad4a33SUladzislau Rezki (Sony) vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL); 3827ec3f64fcSDavid Rientjes vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL); 3828ca23e405STejun Heo if (!vas[area] || !vms[area]) 3829ca23e405STejun Heo goto err_free; 3830ca23e405STejun Heo } 3831ca23e405STejun Heo retry: 3832e36176beSUladzislau Rezki (Sony) spin_lock(&free_vmap_area_lock); 3833ca23e405STejun Heo 3834ca23e405STejun Heo /* start scanning - we scan from the top, begin with the last area */ 3835ca23e405STejun Heo area = term_area = last_area; 3836ca23e405STejun Heo start = offsets[area]; 3837ca23e405STejun Heo end = start + sizes[area]; 3838ca23e405STejun Heo 383968ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(vmalloc_end); 384068ad4a33SUladzislau Rezki (Sony) base = pvm_determine_end_from_reverse(&va, align) - end; 3841ca23e405STejun Heo 3842ca23e405STejun Heo while (true) { 3843ca23e405STejun Heo /* 3844ca23e405STejun Heo * base might have underflowed, add last_end before 3845ca23e405STejun Heo * comparing. 3846ca23e405STejun Heo */ 384768ad4a33SUladzislau Rezki (Sony) if (base + last_end < vmalloc_start + last_end) 384868ad4a33SUladzislau Rezki (Sony) goto overflow; 3849ca23e405STejun Heo 3850ca23e405STejun Heo /* 385168ad4a33SUladzislau Rezki (Sony) * Fitting base has not been found. 3852ca23e405STejun Heo */ 385368ad4a33SUladzislau Rezki (Sony) if (va == NULL) 385468ad4a33SUladzislau Rezki (Sony) goto overflow; 3855ca23e405STejun Heo 3856ca23e405STejun Heo /* 3857d8cc323dSQiujun Huang * If required width exceeds current VA block, move 38585336e52cSKuppuswamy Sathyanarayanan * base downwards and then recheck. 38595336e52cSKuppuswamy Sathyanarayanan */ 38605336e52cSKuppuswamy Sathyanarayanan if (base + end > va->va_end) { 38615336e52cSKuppuswamy Sathyanarayanan base = pvm_determine_end_from_reverse(&va, align) - end; 38625336e52cSKuppuswamy Sathyanarayanan term_area = area; 38635336e52cSKuppuswamy Sathyanarayanan continue; 38645336e52cSKuppuswamy Sathyanarayanan } 38655336e52cSKuppuswamy Sathyanarayanan 38665336e52cSKuppuswamy Sathyanarayanan /* 386768ad4a33SUladzislau Rezki (Sony) * If this VA does not fit, move base downwards and recheck. 3868ca23e405STejun Heo */ 38695336e52cSKuppuswamy Sathyanarayanan if (base + start < va->va_start) { 387068ad4a33SUladzislau Rezki (Sony) va = node_to_va(rb_prev(&va->rb_node)); 387168ad4a33SUladzislau Rezki (Sony) base = pvm_determine_end_from_reverse(&va, align) - end; 3872ca23e405STejun Heo term_area = area; 3873ca23e405STejun Heo continue; 3874ca23e405STejun Heo } 3875ca23e405STejun Heo 3876ca23e405STejun Heo /* 3877ca23e405STejun Heo * This area fits, move on to the previous one. If 3878ca23e405STejun Heo * the previous one is the terminal one, we're done. 3879ca23e405STejun Heo */ 3880ca23e405STejun Heo area = (area + nr_vms - 1) % nr_vms; 3881ca23e405STejun Heo if (area == term_area) 3882ca23e405STejun Heo break; 388368ad4a33SUladzislau Rezki (Sony) 3884ca23e405STejun Heo start = offsets[area]; 3885ca23e405STejun Heo end = start + sizes[area]; 388668ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(base + end); 3887ca23e405STejun Heo } 388868ad4a33SUladzislau Rezki (Sony) 3889ca23e405STejun Heo /* we've found a fitting base, insert all va's */ 3890ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 389168ad4a33SUladzislau Rezki (Sony) int ret; 3892ca23e405STejun Heo 389368ad4a33SUladzislau Rezki (Sony) start = base + offsets[area]; 389468ad4a33SUladzislau Rezki (Sony) size = sizes[area]; 389568ad4a33SUladzislau Rezki (Sony) 389668ad4a33SUladzislau Rezki (Sony) va = pvm_find_va_enclose_addr(start); 389768ad4a33SUladzislau Rezki (Sony) if (WARN_ON_ONCE(va == NULL)) 389868ad4a33SUladzislau Rezki (Sony) /* It is a BUG(), but trigger recovery instead. */ 389968ad4a33SUladzislau Rezki (Sony) goto recovery; 390068ad4a33SUladzislau Rezki (Sony) 3901f9863be4SUladzislau Rezki (Sony) ret = adjust_va_to_fit_type(&free_vmap_area_root, 3902f9863be4SUladzislau Rezki (Sony) &free_vmap_area_list, 3903f9863be4SUladzislau Rezki (Sony) va, start, size); 39041b23ff80SBaoquan He if (WARN_ON_ONCE(unlikely(ret))) 390568ad4a33SUladzislau Rezki (Sony) /* It is a BUG(), but trigger recovery instead. */ 390668ad4a33SUladzislau Rezki (Sony) goto recovery; 390768ad4a33SUladzislau Rezki (Sony) 390868ad4a33SUladzislau Rezki (Sony) /* Allocated area. */ 390968ad4a33SUladzislau Rezki (Sony) va = vas[area]; 391068ad4a33SUladzislau Rezki (Sony) va->va_start = start; 391168ad4a33SUladzislau Rezki (Sony) va->va_end = start + size; 3912ca23e405STejun Heo } 3913ca23e405STejun Heo 3914e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 3915ca23e405STejun Heo 3916253a496dSDaniel Axtens /* populate the kasan shadow space */ 3917253a496dSDaniel Axtens for (area = 0; area < nr_vms; area++) { 3918253a496dSDaniel Axtens if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area])) 3919253a496dSDaniel Axtens goto err_free_shadow; 3920253a496dSDaniel Axtens } 3921253a496dSDaniel Axtens 3922ca23e405STejun Heo /* insert all vm's */ 3923e36176beSUladzislau Rezki (Sony) spin_lock(&vmap_area_lock); 3924e36176beSUladzislau Rezki (Sony) for (area = 0; area < nr_vms; area++) { 3925e36176beSUladzislau Rezki (Sony) insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list); 3926e36176beSUladzislau Rezki (Sony) 3927e36176beSUladzislau Rezki (Sony) setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC, 3928ca23e405STejun Heo pcpu_get_vm_areas); 3929e36176beSUladzislau Rezki (Sony) } 3930e36176beSUladzislau Rezki (Sony) spin_unlock(&vmap_area_lock); 3931ca23e405STejun Heo 393219f1c3acSAndrey Konovalov /* 393319f1c3acSAndrey Konovalov * Mark allocated areas as accessible. Do it now as a best-effort 393419f1c3acSAndrey Konovalov * approach, as they can be mapped outside of vmalloc code. 393523689e91SAndrey Konovalov * With hardware tag-based KASAN, marking is skipped for 393623689e91SAndrey Konovalov * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc(). 393719f1c3acSAndrey Konovalov */ 39381d96320fSAndrey Konovalov for (area = 0; area < nr_vms; area++) 39391d96320fSAndrey Konovalov vms[area]->addr = kasan_unpoison_vmalloc(vms[area]->addr, 3940f6e39794SAndrey Konovalov vms[area]->size, KASAN_VMALLOC_PROT_NORMAL); 39411d96320fSAndrey Konovalov 3942ca23e405STejun Heo kfree(vas); 3943ca23e405STejun Heo return vms; 3944ca23e405STejun Heo 394568ad4a33SUladzislau Rezki (Sony) recovery: 3946e36176beSUladzislau Rezki (Sony) /* 3947e36176beSUladzislau Rezki (Sony) * Remove previously allocated areas. There is no 3948e36176beSUladzislau Rezki (Sony) * need in removing these areas from the busy tree, 3949e36176beSUladzislau Rezki (Sony) * because they are inserted only on the final step 3950e36176beSUladzislau Rezki (Sony) * and when pcpu_get_vm_areas() is success. 3951e36176beSUladzislau Rezki (Sony) */ 395268ad4a33SUladzislau Rezki (Sony) while (area--) { 3953253a496dSDaniel Axtens orig_start = vas[area]->va_start; 3954253a496dSDaniel Axtens orig_end = vas[area]->va_end; 395596e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, 39563c5c3cfbSDaniel Axtens &free_vmap_area_list); 39579c801f61SUladzislau Rezki (Sony) if (va) 3958253a496dSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 3959253a496dSDaniel Axtens va->va_start, va->va_end); 396068ad4a33SUladzislau Rezki (Sony) vas[area] = NULL; 396168ad4a33SUladzislau Rezki (Sony) } 396268ad4a33SUladzislau Rezki (Sony) 396368ad4a33SUladzislau Rezki (Sony) overflow: 3964e36176beSUladzislau Rezki (Sony) spin_unlock(&free_vmap_area_lock); 396568ad4a33SUladzislau Rezki (Sony) if (!purged) { 396668ad4a33SUladzislau Rezki (Sony) purge_vmap_area_lazy(); 396768ad4a33SUladzislau Rezki (Sony) purged = true; 396868ad4a33SUladzislau Rezki (Sony) 396968ad4a33SUladzislau Rezki (Sony) /* Before "retry", check if we recover. */ 397068ad4a33SUladzislau Rezki (Sony) for (area = 0; area < nr_vms; area++) { 397168ad4a33SUladzislau Rezki (Sony) if (vas[area]) 397268ad4a33SUladzislau Rezki (Sony) continue; 397368ad4a33SUladzislau Rezki (Sony) 397468ad4a33SUladzislau Rezki (Sony) vas[area] = kmem_cache_zalloc( 397568ad4a33SUladzislau Rezki (Sony) vmap_area_cachep, GFP_KERNEL); 397668ad4a33SUladzislau Rezki (Sony) if (!vas[area]) 397768ad4a33SUladzislau Rezki (Sony) goto err_free; 397868ad4a33SUladzislau Rezki (Sony) } 397968ad4a33SUladzislau Rezki (Sony) 398068ad4a33SUladzislau Rezki (Sony) goto retry; 398168ad4a33SUladzislau Rezki (Sony) } 398268ad4a33SUladzislau Rezki (Sony) 3983ca23e405STejun Heo err_free: 3984ca23e405STejun Heo for (area = 0; area < nr_vms; area++) { 398568ad4a33SUladzislau Rezki (Sony) if (vas[area]) 398668ad4a33SUladzislau Rezki (Sony) kmem_cache_free(vmap_area_cachep, vas[area]); 398768ad4a33SUladzislau Rezki (Sony) 3988ca23e405STejun Heo kfree(vms[area]); 3989ca23e405STejun Heo } 3990f1db7afdSKautuk Consul err_free2: 3991ca23e405STejun Heo kfree(vas); 3992ca23e405STejun Heo kfree(vms); 3993ca23e405STejun Heo return NULL; 3994253a496dSDaniel Axtens 3995253a496dSDaniel Axtens err_free_shadow: 3996253a496dSDaniel Axtens spin_lock(&free_vmap_area_lock); 3997253a496dSDaniel Axtens /* 3998253a496dSDaniel Axtens * We release all the vmalloc shadows, even the ones for regions that 3999253a496dSDaniel Axtens * hadn't been successfully added. This relies on kasan_release_vmalloc 4000253a496dSDaniel Axtens * being able to tolerate this case. 4001253a496dSDaniel Axtens */ 4002253a496dSDaniel Axtens for (area = 0; area < nr_vms; area++) { 4003253a496dSDaniel Axtens orig_start = vas[area]->va_start; 4004253a496dSDaniel Axtens orig_end = vas[area]->va_end; 400596e2db45SUladzislau Rezki (Sony) va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, 4006253a496dSDaniel Axtens &free_vmap_area_list); 40079c801f61SUladzislau Rezki (Sony) if (va) 4008253a496dSDaniel Axtens kasan_release_vmalloc(orig_start, orig_end, 4009253a496dSDaniel Axtens va->va_start, va->va_end); 4010253a496dSDaniel Axtens vas[area] = NULL; 4011253a496dSDaniel Axtens kfree(vms[area]); 4012253a496dSDaniel Axtens } 4013253a496dSDaniel Axtens spin_unlock(&free_vmap_area_lock); 4014253a496dSDaniel Axtens kfree(vas); 4015253a496dSDaniel Axtens kfree(vms); 4016253a496dSDaniel Axtens return NULL; 4017ca23e405STejun Heo } 4018ca23e405STejun Heo 4019ca23e405STejun Heo /** 4020ca23e405STejun Heo * pcpu_free_vm_areas - free vmalloc areas for percpu allocator 4021ca23e405STejun Heo * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() 4022ca23e405STejun Heo * @nr_vms: the number of allocated areas 4023ca23e405STejun Heo * 4024ca23e405STejun Heo * Free vm_structs and the array allocated by pcpu_get_vm_areas(). 4025ca23e405STejun Heo */ 4026ca23e405STejun Heo void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) 4027ca23e405STejun Heo { 4028ca23e405STejun Heo int i; 4029ca23e405STejun Heo 4030ca23e405STejun Heo for (i = 0; i < nr_vms; i++) 4031ca23e405STejun Heo free_vm_area(vms[i]); 4032ca23e405STejun Heo kfree(vms); 4033ca23e405STejun Heo } 40344f8b02b4STejun Heo #endif /* CONFIG_SMP */ 4035a10aa579SChristoph Lameter 40365bb1bb35SPaul E. McKenney #ifdef CONFIG_PRINTK 403798f18083SPaul E. McKenney bool vmalloc_dump_obj(void *object) 403898f18083SPaul E. McKenney { 403998f18083SPaul E. McKenney struct vm_struct *vm; 404098f18083SPaul E. McKenney void *objp = (void *)PAGE_ALIGN((unsigned long)object); 404198f18083SPaul E. McKenney 404298f18083SPaul E. McKenney vm = find_vm_area(objp); 404398f18083SPaul E. McKenney if (!vm) 404498f18083SPaul E. McKenney return false; 4045bd34dcd4SPaul E. McKenney pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n", 4046bd34dcd4SPaul E. McKenney vm->nr_pages, (unsigned long)vm->addr, vm->caller); 404798f18083SPaul E. McKenney return true; 404898f18083SPaul E. McKenney } 40495bb1bb35SPaul E. McKenney #endif 405098f18083SPaul E. McKenney 4051a10aa579SChristoph Lameter #ifdef CONFIG_PROC_FS 4052a10aa579SChristoph Lameter static void *s_start(struct seq_file *m, loff_t *pos) 4053e36176beSUladzislau Rezki (Sony) __acquires(&vmap_purge_lock) 4054d4033afdSJoonsoo Kim __acquires(&vmap_area_lock) 4055a10aa579SChristoph Lameter { 4056e36176beSUladzislau Rezki (Sony) mutex_lock(&vmap_purge_lock); 4057d4033afdSJoonsoo Kim spin_lock(&vmap_area_lock); 4058e36176beSUladzislau Rezki (Sony) 40593f500069Szijun_hu return seq_list_start(&vmap_area_list, *pos); 4060a10aa579SChristoph Lameter } 4061a10aa579SChristoph Lameter 4062a10aa579SChristoph Lameter static void *s_next(struct seq_file *m, void *p, loff_t *pos) 4063a10aa579SChristoph Lameter { 40643f500069Szijun_hu return seq_list_next(p, &vmap_area_list, pos); 4065a10aa579SChristoph Lameter } 4066a10aa579SChristoph Lameter 4067a10aa579SChristoph Lameter static void s_stop(struct seq_file *m, void *p) 4068d4033afdSJoonsoo Kim __releases(&vmap_area_lock) 40690a7dd4e9SWaiman Long __releases(&vmap_purge_lock) 4070a10aa579SChristoph Lameter { 4071d4033afdSJoonsoo Kim spin_unlock(&vmap_area_lock); 40720a7dd4e9SWaiman Long mutex_unlock(&vmap_purge_lock); 4073a10aa579SChristoph Lameter } 4074a10aa579SChristoph Lameter 4075a47a126aSEric Dumazet static void show_numa_info(struct seq_file *m, struct vm_struct *v) 4076a47a126aSEric Dumazet { 4077e5adfffcSKirill A. Shutemov if (IS_ENABLED(CONFIG_NUMA)) { 4078a47a126aSEric Dumazet unsigned int nr, *counters = m->private; 407951e50b3aSEric Dumazet unsigned int step = 1U << vm_area_page_order(v); 4080a47a126aSEric Dumazet 4081a47a126aSEric Dumazet if (!counters) 4082a47a126aSEric Dumazet return; 4083a47a126aSEric Dumazet 4084af12346cSWanpeng Li if (v->flags & VM_UNINITIALIZED) 4085af12346cSWanpeng Li return; 40867e5b528bSDmitry Vyukov /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ 40877e5b528bSDmitry Vyukov smp_rmb(); 4088af12346cSWanpeng Li 4089a47a126aSEric Dumazet memset(counters, 0, nr_node_ids * sizeof(unsigned int)); 4090a47a126aSEric Dumazet 409151e50b3aSEric Dumazet for (nr = 0; nr < v->nr_pages; nr += step) 409251e50b3aSEric Dumazet counters[page_to_nid(v->pages[nr])] += step; 4093a47a126aSEric Dumazet for_each_node_state(nr, N_HIGH_MEMORY) 4094a47a126aSEric Dumazet if (counters[nr]) 4095a47a126aSEric Dumazet seq_printf(m, " N%u=%u", nr, counters[nr]); 4096a47a126aSEric Dumazet } 4097a47a126aSEric Dumazet } 4098a47a126aSEric Dumazet 4099dd3b8353SUladzislau Rezki (Sony) static void show_purge_info(struct seq_file *m) 4100dd3b8353SUladzislau Rezki (Sony) { 4101dd3b8353SUladzislau Rezki (Sony) struct vmap_area *va; 4102dd3b8353SUladzislau Rezki (Sony) 410396e2db45SUladzislau Rezki (Sony) spin_lock(&purge_vmap_area_lock); 410496e2db45SUladzislau Rezki (Sony) list_for_each_entry(va, &purge_vmap_area_list, list) { 4105dd3b8353SUladzislau Rezki (Sony) seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n", 4106dd3b8353SUladzislau Rezki (Sony) (void *)va->va_start, (void *)va->va_end, 4107dd3b8353SUladzislau Rezki (Sony) va->va_end - va->va_start); 4108dd3b8353SUladzislau Rezki (Sony) } 410996e2db45SUladzislau Rezki (Sony) spin_unlock(&purge_vmap_area_lock); 4110dd3b8353SUladzislau Rezki (Sony) } 4111dd3b8353SUladzislau Rezki (Sony) 4112a10aa579SChristoph Lameter static int s_show(struct seq_file *m, void *p) 4113a10aa579SChristoph Lameter { 41143f500069Szijun_hu struct vmap_area *va; 4115d4033afdSJoonsoo Kim struct vm_struct *v; 4116d4033afdSJoonsoo Kim 41173f500069Szijun_hu va = list_entry(p, struct vmap_area, list); 41183f500069Szijun_hu 4119c2ce8c14SWanpeng Li /* 4120688fcbfcSPengfei Li * s_show can encounter race with remove_vm_area, !vm on behalf 4121688fcbfcSPengfei Li * of vmap area is being tear down or vm_map_ram allocation. 4122c2ce8c14SWanpeng Li */ 4123688fcbfcSPengfei Li if (!va->vm) { 4124dd3b8353SUladzislau Rezki (Sony) seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n", 412578c72746SYisheng Xie (void *)va->va_start, (void *)va->va_end, 4126dd3b8353SUladzislau Rezki (Sony) va->va_end - va->va_start); 412778c72746SYisheng Xie 41287cc7913eSEric Dumazet goto final; 412978c72746SYisheng Xie } 4130d4033afdSJoonsoo Kim 4131d4033afdSJoonsoo Kim v = va->vm; 4132a10aa579SChristoph Lameter 413345ec1690SKees Cook seq_printf(m, "0x%pK-0x%pK %7ld", 4134a10aa579SChristoph Lameter v->addr, v->addr + v->size, v->size); 4135a10aa579SChristoph Lameter 413662c70bceSJoe Perches if (v->caller) 413762c70bceSJoe Perches seq_printf(m, " %pS", v->caller); 413823016969SChristoph Lameter 4139a10aa579SChristoph Lameter if (v->nr_pages) 4140a10aa579SChristoph Lameter seq_printf(m, " pages=%d", v->nr_pages); 4141a10aa579SChristoph Lameter 4142a10aa579SChristoph Lameter if (v->phys_addr) 4143199eaa05SMiles Chen seq_printf(m, " phys=%pa", &v->phys_addr); 4144a10aa579SChristoph Lameter 4145a10aa579SChristoph Lameter if (v->flags & VM_IOREMAP) 4146f4527c90SFabian Frederick seq_puts(m, " ioremap"); 4147a10aa579SChristoph Lameter 4148a10aa579SChristoph Lameter if (v->flags & VM_ALLOC) 4149f4527c90SFabian Frederick seq_puts(m, " vmalloc"); 4150a10aa579SChristoph Lameter 4151a10aa579SChristoph Lameter if (v->flags & VM_MAP) 4152f4527c90SFabian Frederick seq_puts(m, " vmap"); 4153a10aa579SChristoph Lameter 4154a10aa579SChristoph Lameter if (v->flags & VM_USERMAP) 4155f4527c90SFabian Frederick seq_puts(m, " user"); 4156a10aa579SChristoph Lameter 4157fe9041c2SChristoph Hellwig if (v->flags & VM_DMA_COHERENT) 4158fe9041c2SChristoph Hellwig seq_puts(m, " dma-coherent"); 4159fe9041c2SChristoph Hellwig 4160244d63eeSDavid Rientjes if (is_vmalloc_addr(v->pages)) 4161f4527c90SFabian Frederick seq_puts(m, " vpages"); 4162a10aa579SChristoph Lameter 4163a47a126aSEric Dumazet show_numa_info(m, v); 4164a10aa579SChristoph Lameter seq_putc(m, '\n'); 4165dd3b8353SUladzislau Rezki (Sony) 4166dd3b8353SUladzislau Rezki (Sony) /* 416796e2db45SUladzislau Rezki (Sony) * As a final step, dump "unpurged" areas. 4168dd3b8353SUladzislau Rezki (Sony) */ 41697cc7913eSEric Dumazet final: 4170dd3b8353SUladzislau Rezki (Sony) if (list_is_last(&va->list, &vmap_area_list)) 4171dd3b8353SUladzislau Rezki (Sony) show_purge_info(m); 4172dd3b8353SUladzislau Rezki (Sony) 4173a10aa579SChristoph Lameter return 0; 4174a10aa579SChristoph Lameter } 4175a10aa579SChristoph Lameter 41765f6a6a9cSAlexey Dobriyan static const struct seq_operations vmalloc_op = { 4177a10aa579SChristoph Lameter .start = s_start, 4178a10aa579SChristoph Lameter .next = s_next, 4179a10aa579SChristoph Lameter .stop = s_stop, 4180a10aa579SChristoph Lameter .show = s_show, 4181a10aa579SChristoph Lameter }; 41825f6a6a9cSAlexey Dobriyan 41835f6a6a9cSAlexey Dobriyan static int __init proc_vmalloc_init(void) 41845f6a6a9cSAlexey Dobriyan { 4185fddda2b7SChristoph Hellwig if (IS_ENABLED(CONFIG_NUMA)) 41860825a6f9SJoe Perches proc_create_seq_private("vmallocinfo", 0400, NULL, 418744414d82SChristoph Hellwig &vmalloc_op, 418844414d82SChristoph Hellwig nr_node_ids * sizeof(unsigned int), NULL); 4189fddda2b7SChristoph Hellwig else 41900825a6f9SJoe Perches proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op); 41915f6a6a9cSAlexey Dobriyan return 0; 41925f6a6a9cSAlexey Dobriyan } 41935f6a6a9cSAlexey Dobriyan module_init(proc_vmalloc_init); 4194db3808c1SJoonsoo Kim 4195a10aa579SChristoph Lameter #endif 4196