Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Copyright (C) 1993 Linus Torvalds |
| 4 | * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 |
| 5 | * SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000 |
| 6 | * Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002 |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 7 | * Numa awareness, Christoph Lameter, SGI, June 2005 |
Uladzislau Rezki (Sony) | d758ffe | 2020-08-06 23:24:18 -0700 | [diff] [blame] | 8 | * Improving global KVA allocator, Uladzislau Rezki, Sony, May 2019 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 11 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/mm.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/highmem.h> |
Ingo Molnar | c3edc40 | 2017-02-02 08:35:14 +0100 | [diff] [blame] | 15 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/interrupt.h> |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 19 | #include <linux/proc_fs.h> |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 21 | #include <linux/set_memory.h> |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 22 | #include <linux/debugobjects.h> |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 23 | #include <linux/kallsyms.h> |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 24 | #include <linux/list.h> |
Chris Wilson | 4da56b9 | 2016-04-04 14:46:42 +0100 | [diff] [blame] | 25 | #include <linux/notifier.h> |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 26 | #include <linux/rbtree.h> |
Matthew Wilcox (Oracle) | 0f14599 | 2020-08-06 23:24:05 -0700 | [diff] [blame] | 27 | #include <linux/xarray.h> |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 28 | #include <linux/rcupdate.h> |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 29 | #include <linux/pfn.h> |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 30 | #include <linux/kmemleak.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 31 | #include <linux/atomic.h> |
Gideon Israel Dsouza | 3b32123 | 2014-04-07 15:37:26 -0700 | [diff] [blame] | 32 | #include <linux/compiler.h> |
Al Viro | 32fcfd4 | 2013-03-10 20:14:08 -0400 | [diff] [blame] | 33 | #include <linux/llist.h> |
Toshi Kani | 0f616be | 2015-04-14 15:47:17 -0700 | [diff] [blame] | 34 | #include <linux/bitops.h> |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 35 | #include <linux/rbtree_augmented.h> |
Jann Horn | bdebd6a2 | 2020-04-20 18:14:11 -0700 | [diff] [blame] | 36 | #include <linux/overflow.h> |
Nicholas Piggin | c0eb315 | 2021-04-29 22:58:13 -0700 | [diff] [blame] | 37 | #include <linux/pgtable.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 38 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/tlbflush.h> |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 40 | #include <asm/shmparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Mel Gorman | dd56b04 | 2015-11-06 16:28:43 -0800 | [diff] [blame] | 42 | #include "internal.h" |
Joerg Roedel | 2a681cf | 2020-08-06 23:22:55 -0700 | [diff] [blame] | 43 | #include "pgalloc-track.h" |
Mel Gorman | dd56b04 | 2015-11-06 16:28:43 -0800 | [diff] [blame] | 44 | |
Ingo Molnar | 186525b | 2019-11-29 08:17:25 +0100 | [diff] [blame] | 45 | bool is_vmalloc_addr(const void *x) |
| 46 | { |
| 47 | unsigned long addr = (unsigned long)x; |
| 48 | |
| 49 | return addr >= VMALLOC_START && addr < VMALLOC_END; |
| 50 | } |
| 51 | EXPORT_SYMBOL(is_vmalloc_addr); |
| 52 | |
Al Viro | 32fcfd4 | 2013-03-10 20:14:08 -0400 | [diff] [blame] | 53 | struct vfree_deferred { |
| 54 | struct llist_head list; |
| 55 | struct work_struct wq; |
| 56 | }; |
| 57 | static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred); |
| 58 | |
| 59 | static void __vunmap(const void *, int); |
| 60 | |
| 61 | static void free_work(struct work_struct *w) |
| 62 | { |
| 63 | struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq); |
Byungchul Park | 894e58c | 2017-09-06 16:24:26 -0700 | [diff] [blame] | 64 | struct llist_node *t, *llnode; |
| 65 | |
| 66 | llist_for_each_safe(llnode, t, llist_del_all(&p->list)) |
| 67 | __vunmap((void *)llnode, 1); |
Al Viro | 32fcfd4 | 2013-03-10 20:14:08 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 70 | /*** Page table manipulation functions ***/ |
Adrian Bunk | b221385 | 2006-09-25 23:31:02 -0700 | [diff] [blame] | 71 | |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 72 | static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, |
| 73 | pgtbl_mod_mask *mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
| 75 | pte_t *pte; |
| 76 | |
| 77 | pte = pte_offset_kernel(pmd, addr); |
| 78 | do { |
| 79 | pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte); |
| 80 | WARN_ON(!pte_none(ptent) && !pte_present(ptent)); |
| 81 | } while (pte++, addr += PAGE_SIZE, addr != end); |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 82 | *mask |= PGTBL_PTE_MODIFIED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 85 | static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end, |
| 86 | pgtbl_mod_mask *mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
| 88 | pmd_t *pmd; |
| 89 | unsigned long next; |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 90 | int cleared; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | pmd = pmd_offset(pud, addr); |
| 93 | do { |
| 94 | next = pmd_addr_end(addr, end); |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 95 | |
| 96 | cleared = pmd_clear_huge(pmd); |
| 97 | if (cleared || pmd_bad(*pmd)) |
| 98 | *mask |= PGTBL_PMD_MODIFIED; |
| 99 | |
| 100 | if (cleared) |
Toshi Kani | b9820d8 | 2015-04-14 15:47:26 -0700 | [diff] [blame] | 101 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (pmd_none_or_clear_bad(pmd)) |
| 103 | continue; |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 104 | vunmap_pte_range(pmd, addr, next, mask); |
Aneesh Kumar K.V | e47110e | 2020-08-20 17:42:05 -0700 | [diff] [blame] | 105 | |
| 106 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } while (pmd++, addr = next, addr != end); |
| 108 | } |
| 109 | |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 110 | static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, |
| 111 | pgtbl_mod_mask *mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
| 113 | pud_t *pud; |
| 114 | unsigned long next; |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 115 | int cleared; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 117 | pud = pud_offset(p4d, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | do { |
| 119 | next = pud_addr_end(addr, end); |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 120 | |
| 121 | cleared = pud_clear_huge(pud); |
| 122 | if (cleared || pud_bad(*pud)) |
| 123 | *mask |= PGTBL_PUD_MODIFIED; |
| 124 | |
| 125 | if (cleared) |
Toshi Kani | b9820d8 | 2015-04-14 15:47:26 -0700 | [diff] [blame] | 126 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | if (pud_none_or_clear_bad(pud)) |
| 128 | continue; |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 129 | vunmap_pmd_range(pud, addr, next, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } while (pud++, addr = next, addr != end); |
| 131 | } |
| 132 | |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 133 | static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end, |
| 134 | pgtbl_mod_mask *mask) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 135 | { |
| 136 | p4d_t *p4d; |
| 137 | unsigned long next; |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 138 | int cleared; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 139 | |
| 140 | p4d = p4d_offset(pgd, addr); |
| 141 | do { |
| 142 | next = p4d_addr_end(addr, end); |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 143 | |
| 144 | cleared = p4d_clear_huge(p4d); |
| 145 | if (cleared || p4d_bad(*p4d)) |
| 146 | *mask |= PGTBL_P4D_MODIFIED; |
| 147 | |
| 148 | if (cleared) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 149 | continue; |
| 150 | if (p4d_none_or_clear_bad(p4d)) |
| 151 | continue; |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 152 | vunmap_pud_range(p4d, addr, next, mask); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 153 | } while (p4d++, addr = next, addr != end); |
| 154 | } |
| 155 | |
Christoph Hellwig | b521c43 | 2020-06-01 21:51:07 -0700 | [diff] [blame] | 156 | /** |
| 157 | * unmap_kernel_range_noflush - unmap kernel VM area |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 158 | * @start: start of the VM area to unmap |
Christoph Hellwig | b521c43 | 2020-06-01 21:51:07 -0700 | [diff] [blame] | 159 | * @size: size of the VM area to unmap |
| 160 | * |
| 161 | * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size specify |
| 162 | * should have been allocated using get_vm_area() and its friends. |
| 163 | * |
| 164 | * NOTE: |
| 165 | * This function does NOT do any cache flushing. The caller is responsible |
| 166 | * for calling flush_cache_vunmap() on to-be-mapped areas before calling this |
| 167 | * function and flush_tlb_kernel_range() after. |
| 168 | */ |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 169 | void unmap_kernel_range_noflush(unsigned long start, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 171 | unsigned long end = start + size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | unsigned long next; |
Christoph Hellwig | b521c43 | 2020-06-01 21:51:07 -0700 | [diff] [blame] | 173 | pgd_t *pgd; |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 174 | unsigned long addr = start; |
| 175 | pgtbl_mod_mask mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | BUG_ON(addr >= end); |
| 178 | pgd = pgd_offset_k(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | do { |
| 180 | next = pgd_addr_end(addr, end); |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 181 | if (pgd_bad(*pgd)) |
| 182 | mask |= PGTBL_PGD_MODIFIED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | if (pgd_none_or_clear_bad(pgd)) |
| 184 | continue; |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 185 | vunmap_p4d_range(pgd, addr, next, &mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } while (pgd++, addr = next, addr != end); |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 187 | |
| 188 | if (mask & ARCH_PAGE_TABLE_SYNC_MASK) |
| 189 | arch_sync_kernel_mappings(start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Nicholas Piggin | 0a26488 | 2021-04-29 22:58:19 -0700 | [diff] [blame^] | 192 | static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr, |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 193 | unsigned long end, pgprot_t prot, struct page **pages, int *nr, |
| 194 | pgtbl_mod_mask *mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
| 196 | pte_t *pte; |
| 197 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 198 | /* |
| 199 | * nr is a running index into the array which helps higher level |
| 200 | * callers keep track of where we're up to. |
| 201 | */ |
| 202 | |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 203 | pte = pte_alloc_kernel_track(pmd, addr, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | if (!pte) |
| 205 | return -ENOMEM; |
| 206 | do { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 207 | struct page *page = pages[*nr]; |
| 208 | |
| 209 | if (WARN_ON(!pte_none(*pte))) |
| 210 | return -EBUSY; |
| 211 | if (WARN_ON(!page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | return -ENOMEM; |
| 213 | set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 214 | (*nr)++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } while (pte++, addr += PAGE_SIZE, addr != end); |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 216 | *mask |= PGTBL_PTE_MODIFIED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
Nicholas Piggin | 0a26488 | 2021-04-29 22:58:19 -0700 | [diff] [blame^] | 220 | static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr, |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 221 | unsigned long end, pgprot_t prot, struct page **pages, int *nr, |
| 222 | pgtbl_mod_mask *mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
| 224 | pmd_t *pmd; |
| 225 | unsigned long next; |
| 226 | |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 227 | pmd = pmd_alloc_track(&init_mm, pud, addr, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | if (!pmd) |
| 229 | return -ENOMEM; |
| 230 | do { |
| 231 | next = pmd_addr_end(addr, end); |
Nicholas Piggin | 0a26488 | 2021-04-29 22:58:19 -0700 | [diff] [blame^] | 232 | if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | return -ENOMEM; |
| 234 | } while (pmd++, addr = next, addr != end); |
| 235 | return 0; |
| 236 | } |
| 237 | |
Nicholas Piggin | 0a26488 | 2021-04-29 22:58:19 -0700 | [diff] [blame^] | 238 | static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr, |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 239 | unsigned long end, pgprot_t prot, struct page **pages, int *nr, |
| 240 | pgtbl_mod_mask *mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | pud_t *pud; |
| 243 | unsigned long next; |
| 244 | |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 245 | pud = pud_alloc_track(&init_mm, p4d, addr, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | if (!pud) |
| 247 | return -ENOMEM; |
| 248 | do { |
| 249 | next = pud_addr_end(addr, end); |
Nicholas Piggin | 0a26488 | 2021-04-29 22:58:19 -0700 | [diff] [blame^] | 250 | if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | return -ENOMEM; |
| 252 | } while (pud++, addr = next, addr != end); |
| 253 | return 0; |
| 254 | } |
| 255 | |
Nicholas Piggin | 0a26488 | 2021-04-29 22:58:19 -0700 | [diff] [blame^] | 256 | static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr, |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 257 | unsigned long end, pgprot_t prot, struct page **pages, int *nr, |
| 258 | pgtbl_mod_mask *mask) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 259 | { |
| 260 | p4d_t *p4d; |
| 261 | unsigned long next; |
| 262 | |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 263 | p4d = p4d_alloc_track(&init_mm, pgd, addr, mask); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 264 | if (!p4d) |
| 265 | return -ENOMEM; |
| 266 | do { |
| 267 | next = p4d_addr_end(addr, end); |
Nicholas Piggin | 0a26488 | 2021-04-29 22:58:19 -0700 | [diff] [blame^] | 268 | if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask)) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 269 | return -ENOMEM; |
| 270 | } while (p4d++, addr = next, addr != end); |
| 271 | return 0; |
| 272 | } |
| 273 | |
Christoph Hellwig | b521c43 | 2020-06-01 21:51:07 -0700 | [diff] [blame] | 274 | /** |
| 275 | * map_kernel_range_noflush - map kernel VM area with the specified pages |
| 276 | * @addr: start of the VM area to map |
| 277 | * @size: size of the VM area to map |
| 278 | * @prot: page protection flags to use |
| 279 | * @pages: pages to map |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 280 | * |
Christoph Hellwig | b521c43 | 2020-06-01 21:51:07 -0700 | [diff] [blame] | 281 | * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size specify should |
| 282 | * have been allocated using get_vm_area() and its friends. |
| 283 | * |
| 284 | * NOTE: |
| 285 | * This function does NOT do any cache flushing. The caller is responsible for |
| 286 | * calling flush_cache_vmap() on to-be-mapped areas before calling this |
| 287 | * function. |
| 288 | * |
| 289 | * RETURNS: |
Christoph Hellwig | 60bb446 | 2020-06-01 21:51:15 -0700 | [diff] [blame] | 290 | * 0 on success, -errno on failure. |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 291 | */ |
Christoph Hellwig | b521c43 | 2020-06-01 21:51:07 -0700 | [diff] [blame] | 292 | int map_kernel_range_noflush(unsigned long addr, unsigned long size, |
| 293 | pgprot_t prot, struct page **pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 295 | unsigned long start = addr; |
Christoph Hellwig | b521c43 | 2020-06-01 21:51:07 -0700 | [diff] [blame] | 296 | unsigned long end = addr + size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | unsigned long next; |
Christoph Hellwig | b521c43 | 2020-06-01 21:51:07 -0700 | [diff] [blame] | 298 | pgd_t *pgd; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 299 | int err = 0; |
| 300 | int nr = 0; |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 301 | pgtbl_mod_mask mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | BUG_ON(addr >= end); |
| 304 | pgd = pgd_offset_k(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | do { |
| 306 | next = pgd_addr_end(addr, end); |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 307 | if (pgd_bad(*pgd)) |
| 308 | mask |= PGTBL_PGD_MODIFIED; |
Nicholas Piggin | 0a26488 | 2021-04-29 22:58:19 -0700 | [diff] [blame^] | 309 | err = vmap_pages_p4d_range(pgd, addr, next, prot, pages, &nr, &mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if (err) |
Figo.zhang | bf88c8c | 2009-09-21 17:01:47 -0700 | [diff] [blame] | 311 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } while (pgd++, addr = next, addr != end); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 313 | |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 314 | if (mask & ARCH_PAGE_TABLE_SYNC_MASK) |
| 315 | arch_sync_kernel_mappings(start, end); |
| 316 | |
Christoph Hellwig | 60bb446 | 2020-06-01 21:51:15 -0700 | [diff] [blame] | 317 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Christoph Hellwig | ed1f324 | 2020-06-01 21:51:19 -0700 | [diff] [blame] | 320 | int map_kernel_range(unsigned long start, unsigned long size, pgprot_t prot, |
| 321 | struct page **pages) |
Tejun Heo | 8fc4898 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 322 | { |
| 323 | int ret; |
| 324 | |
Christoph Hellwig | a29adb6 | 2020-06-01 21:51:11 -0700 | [diff] [blame] | 325 | ret = map_kernel_range_noflush(start, size, prot, pages); |
| 326 | flush_cache_vmap(start, start + size); |
Tejun Heo | 8fc4898 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 327 | return ret; |
| 328 | } |
| 329 | |
KAMEZAWA Hiroyuki | 81ac3ad | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 330 | int is_vmalloc_or_module_addr(const void *x) |
Linus Torvalds | 73bdf0a | 2008-10-15 08:35:12 -0700 | [diff] [blame] | 331 | { |
| 332 | /* |
Russell King | ab4f2ee | 2008-11-06 17:11:07 +0000 | [diff] [blame] | 333 | * ARM, x86-64 and sparc64 put modules in a special place, |
Linus Torvalds | 73bdf0a | 2008-10-15 08:35:12 -0700 | [diff] [blame] | 334 | * and fall back on vmalloc() if that fails. Others |
| 335 | * just put it in the vmalloc space. |
| 336 | */ |
| 337 | #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) |
| 338 | unsigned long addr = (unsigned long)x; |
| 339 | if (addr >= MODULES_VADDR && addr < MODULES_END) |
| 340 | return 1; |
| 341 | #endif |
| 342 | return is_vmalloc_addr(x); |
| 343 | } |
| 344 | |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 345 | /* |
Nicholas Piggin | c0eb315 | 2021-04-29 22:58:13 -0700 | [diff] [blame] | 346 | * Walk a vmap address to the struct page it maps. Huge vmap mappings will |
| 347 | * return the tail page that corresponds to the base page address, which |
| 348 | * matches small vmap mappings. |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 349 | */ |
malc | add688f | 2014-01-27 17:06:53 -0800 | [diff] [blame] | 350 | struct page *vmalloc_to_page(const void *vmalloc_addr) |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 351 | { |
| 352 | unsigned long addr = (unsigned long) vmalloc_addr; |
malc | add688f | 2014-01-27 17:06:53 -0800 | [diff] [blame] | 353 | struct page *page = NULL; |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 354 | pgd_t *pgd = pgd_offset_k(addr); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 355 | p4d_t *p4d; |
| 356 | pud_t *pud; |
| 357 | pmd_t *pmd; |
| 358 | pte_t *ptep, pte; |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 359 | |
Ingo Molnar | 7aa413d | 2008-06-19 13:28:11 +0200 | [diff] [blame] | 360 | /* |
| 361 | * XXX we might need to change this if we add VIRTUAL_BUG_ON for |
| 362 | * architectures that do not vmalloc module space |
| 363 | */ |
Linus Torvalds | 73bdf0a | 2008-10-15 08:35:12 -0700 | [diff] [blame] | 364 | VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); |
Jiri Slaby | 59ea746 | 2008-06-12 13:56:40 +0200 | [diff] [blame] | 365 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 366 | if (pgd_none(*pgd)) |
| 367 | return NULL; |
Nicholas Piggin | c0eb315 | 2021-04-29 22:58:13 -0700 | [diff] [blame] | 368 | if (WARN_ON_ONCE(pgd_leaf(*pgd))) |
| 369 | return NULL; /* XXX: no allowance for huge pgd */ |
| 370 | if (WARN_ON_ONCE(pgd_bad(*pgd))) |
| 371 | return NULL; |
| 372 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 373 | p4d = p4d_offset(pgd, addr); |
| 374 | if (p4d_none(*p4d)) |
| 375 | return NULL; |
Nicholas Piggin | c0eb315 | 2021-04-29 22:58:13 -0700 | [diff] [blame] | 376 | if (p4d_leaf(*p4d)) |
| 377 | return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT); |
| 378 | if (WARN_ON_ONCE(p4d_bad(*p4d))) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 379 | return NULL; |
Nicholas Piggin | c0eb315 | 2021-04-29 22:58:13 -0700 | [diff] [blame] | 380 | |
| 381 | pud = pud_offset(p4d, addr); |
| 382 | if (pud_none(*pud)) |
| 383 | return NULL; |
| 384 | if (pud_leaf(*pud)) |
| 385 | return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
| 386 | if (WARN_ON_ONCE(pud_bad(*pud))) |
| 387 | return NULL; |
| 388 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 389 | pmd = pmd_offset(pud, addr); |
Nicholas Piggin | c0eb315 | 2021-04-29 22:58:13 -0700 | [diff] [blame] | 390 | if (pmd_none(*pmd)) |
| 391 | return NULL; |
| 392 | if (pmd_leaf(*pmd)) |
| 393 | return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
| 394 | if (WARN_ON_ONCE(pmd_bad(*pmd))) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 395 | return NULL; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 396 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 397 | ptep = pte_offset_map(pmd, addr); |
| 398 | pte = *ptep; |
| 399 | if (pte_present(pte)) |
| 400 | page = pte_page(pte); |
| 401 | pte_unmap(ptep); |
Nicholas Piggin | c0eb315 | 2021-04-29 22:58:13 -0700 | [diff] [blame] | 402 | |
malc | add688f | 2014-01-27 17:06:53 -0800 | [diff] [blame] | 403 | return page; |
Jianyu Zhan | ece86e22 | 2014-01-21 15:49:12 -0800 | [diff] [blame] | 404 | } |
| 405 | EXPORT_SYMBOL(vmalloc_to_page); |
| 406 | |
malc | add688f | 2014-01-27 17:06:53 -0800 | [diff] [blame] | 407 | /* |
| 408 | * Map a vmalloc()-space virtual address to the physical page frame number. |
| 409 | */ |
| 410 | unsigned long vmalloc_to_pfn(const void *vmalloc_addr) |
| 411 | { |
| 412 | return page_to_pfn(vmalloc_to_page(vmalloc_addr)); |
| 413 | } |
| 414 | EXPORT_SYMBOL(vmalloc_to_pfn); |
| 415 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 416 | |
| 417 | /*** Global kva allocator ***/ |
| 418 | |
Uladzislau Rezki (Sony) | bb850f4 | 2019-05-17 14:31:34 -0700 | [diff] [blame] | 419 | #define DEBUG_AUGMENT_PROPAGATE_CHECK 0 |
Uladzislau Rezki (Sony) | a6cf4e0 | 2019-05-17 14:31:37 -0700 | [diff] [blame] | 420 | #define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0 |
Uladzislau Rezki (Sony) | bb850f4 | 2019-05-17 14:31:34 -0700 | [diff] [blame] | 421 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 422 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 423 | static DEFINE_SPINLOCK(vmap_area_lock); |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 424 | static DEFINE_SPINLOCK(free_vmap_area_lock); |
Joonsoo Kim | f1c4069 | 2013-04-29 15:07:37 -0700 | [diff] [blame] | 425 | /* Export for kexec only */ |
| 426 | LIST_HEAD(vmap_area_list); |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 427 | static struct rb_root vmap_area_root = RB_ROOT; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 428 | static bool vmap_initialized __read_mostly; |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 429 | |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 430 | static struct rb_root purge_vmap_area_root = RB_ROOT; |
| 431 | static LIST_HEAD(purge_vmap_area_list); |
| 432 | static DEFINE_SPINLOCK(purge_vmap_area_lock); |
| 433 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 434 | /* |
| 435 | * This kmem_cache is used for vmap_area objects. Instead of |
| 436 | * allocating from slab we reuse an object from this cache to |
| 437 | * make things faster. Especially in "no edge" splitting of |
| 438 | * free block. |
| 439 | */ |
| 440 | static struct kmem_cache *vmap_area_cachep; |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 441 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 442 | /* |
| 443 | * This linked list is used in pair with free_vmap_area_root. |
| 444 | * It gives O(1) access to prev/next to perform fast coalescing. |
| 445 | */ |
| 446 | static LIST_HEAD(free_vmap_area_list); |
| 447 | |
| 448 | /* |
| 449 | * This augment red-black tree represents the free vmap space. |
| 450 | * All vmap_area objects in this tree are sorted by va->va_start |
| 451 | * address. It is used for allocation and merging when a vmap |
| 452 | * object is released. |
| 453 | * |
| 454 | * Each vmap_area node contains a maximum available free block |
| 455 | * of its sub-tree, right or left. Therefore it is possible to |
| 456 | * find a lowest match of free area. |
| 457 | */ |
| 458 | static struct rb_root free_vmap_area_root = RB_ROOT; |
| 459 | |
Uladzislau Rezki (Sony) | 82dd23e | 2019-07-11 20:58:57 -0700 | [diff] [blame] | 460 | /* |
| 461 | * Preload a CPU with one object for "no edge" split case. The |
| 462 | * aim is to get rid of allocations from the atomic context, thus |
| 463 | * to use more permissive allocation masks. |
| 464 | */ |
| 465 | static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node); |
| 466 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 467 | static __always_inline unsigned long |
| 468 | va_size(struct vmap_area *va) |
| 469 | { |
| 470 | return (va->va_end - va->va_start); |
| 471 | } |
| 472 | |
| 473 | static __always_inline unsigned long |
| 474 | get_subtree_max_size(struct rb_node *node) |
| 475 | { |
| 476 | struct vmap_area *va; |
| 477 | |
| 478 | va = rb_entry_safe(node, struct vmap_area, rb_node); |
| 479 | return va ? va->subtree_max_size : 0; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Gets called when remove the node and rotate. |
| 484 | */ |
| 485 | static __always_inline unsigned long |
| 486 | compute_subtree_max_size(struct vmap_area *va) |
| 487 | { |
| 488 | return max3(va_size(va), |
| 489 | get_subtree_max_size(va->rb_node.rb_left), |
| 490 | get_subtree_max_size(va->rb_node.rb_right)); |
| 491 | } |
| 492 | |
Michel Lespinasse | 315cc06 | 2019-09-25 16:46:07 -0700 | [diff] [blame] | 493 | RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb, |
| 494 | struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size) |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 495 | |
| 496 | static void purge_vmap_area_lazy(void); |
| 497 | static BLOCKING_NOTIFIER_HEAD(vmap_notify_list); |
| 498 | static unsigned long lazy_max_pages(void); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 499 | |
Roman Gushchin | 97105f0 | 2019-07-11 21:00:13 -0700 | [diff] [blame] | 500 | static atomic_long_t nr_vmalloc_pages; |
| 501 | |
| 502 | unsigned long vmalloc_nr_pages(void) |
| 503 | { |
| 504 | return atomic_long_read(&nr_vmalloc_pages); |
| 505 | } |
| 506 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 507 | static struct vmap_area *__find_vmap_area(unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 509 | struct rb_node *n = vmap_area_root.rb_node; |
| 510 | |
| 511 | while (n) { |
| 512 | struct vmap_area *va; |
| 513 | |
| 514 | va = rb_entry(n, struct vmap_area, rb_node); |
| 515 | if (addr < va->va_start) |
| 516 | n = n->rb_left; |
HATAYAMA Daisuke | cef2ac3 | 2013-07-03 15:02:17 -0700 | [diff] [blame] | 517 | else if (addr >= va->va_end) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 518 | n = n->rb_right; |
| 519 | else |
| 520 | return va; |
| 521 | } |
| 522 | |
| 523 | return NULL; |
| 524 | } |
| 525 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 526 | /* |
| 527 | * This function returns back addresses of parent node |
| 528 | * and its left or right link for further processing. |
Uladzislau Rezki (Sony) | 9c801f6 | 2020-08-06 23:24:24 -0700 | [diff] [blame] | 529 | * |
| 530 | * Otherwise NULL is returned. In that case all further |
| 531 | * steps regarding inserting of conflicting overlap range |
| 532 | * have to be declined and actually considered as a bug. |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 533 | */ |
| 534 | static __always_inline struct rb_node ** |
| 535 | find_va_links(struct vmap_area *va, |
| 536 | struct rb_root *root, struct rb_node *from, |
| 537 | struct rb_node **parent) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 538 | { |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 539 | struct vmap_area *tmp_va; |
| 540 | struct rb_node **link; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 541 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 542 | if (root) { |
| 543 | link = &root->rb_node; |
| 544 | if (unlikely(!*link)) { |
| 545 | *parent = NULL; |
| 546 | return link; |
| 547 | } |
| 548 | } else { |
| 549 | link = &from; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 552 | /* |
| 553 | * Go to the bottom of the tree. When we hit the last point |
| 554 | * we end up with parent rb_node and correct direction, i name |
| 555 | * it link, where the new va->rb_node will be attached to. |
| 556 | */ |
| 557 | do { |
| 558 | tmp_va = rb_entry(*link, struct vmap_area, rb_node); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 559 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 560 | /* |
| 561 | * During the traversal we also do some sanity check. |
| 562 | * Trigger the BUG() if there are sides(left/right) |
| 563 | * or full overlaps. |
| 564 | */ |
| 565 | if (va->va_start < tmp_va->va_end && |
| 566 | va->va_end <= tmp_va->va_start) |
| 567 | link = &(*link)->rb_left; |
| 568 | else if (va->va_end > tmp_va->va_start && |
| 569 | va->va_start >= tmp_va->va_end) |
| 570 | link = &(*link)->rb_right; |
Uladzislau Rezki (Sony) | 9c801f6 | 2020-08-06 23:24:24 -0700 | [diff] [blame] | 571 | else { |
| 572 | WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n", |
| 573 | va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end); |
| 574 | |
| 575 | return NULL; |
| 576 | } |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 577 | } while (*link); |
| 578 | |
| 579 | *parent = &tmp_va->rb_node; |
| 580 | return link; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 583 | static __always_inline struct list_head * |
| 584 | get_va_next_sibling(struct rb_node *parent, struct rb_node **link) |
| 585 | { |
| 586 | struct list_head *list; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 587 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 588 | if (unlikely(!parent)) |
| 589 | /* |
| 590 | * The red-black tree where we try to find VA neighbors |
| 591 | * before merging or inserting is empty, i.e. it means |
| 592 | * there is no free vmap space. Normally it does not |
| 593 | * happen but we handle this case anyway. |
| 594 | */ |
| 595 | return NULL; |
| 596 | |
| 597 | list = &rb_entry(parent, struct vmap_area, rb_node)->list; |
| 598 | return (&parent->rb_right == link ? list->next : list); |
| 599 | } |
| 600 | |
| 601 | static __always_inline void |
| 602 | link_va(struct vmap_area *va, struct rb_root *root, |
| 603 | struct rb_node *parent, struct rb_node **link, struct list_head *head) |
| 604 | { |
| 605 | /* |
| 606 | * VA is still not in the list, but we can |
| 607 | * identify its future previous list_head node. |
| 608 | */ |
| 609 | if (likely(parent)) { |
| 610 | head = &rb_entry(parent, struct vmap_area, rb_node)->list; |
| 611 | if (&parent->rb_right != link) |
| 612 | head = head->prev; |
| 613 | } |
| 614 | |
| 615 | /* Insert to the rb-tree */ |
| 616 | rb_link_node(&va->rb_node, parent, link); |
| 617 | if (root == &free_vmap_area_root) { |
| 618 | /* |
| 619 | * Some explanation here. Just perform simple insertion |
| 620 | * to the tree. We do not set va->subtree_max_size to |
| 621 | * its current size before calling rb_insert_augmented(). |
| 622 | * It is because of we populate the tree from the bottom |
| 623 | * to parent levels when the node _is_ in the tree. |
| 624 | * |
| 625 | * Therefore we set subtree_max_size to zero after insertion, |
| 626 | * to let __augment_tree_propagate_from() puts everything to |
| 627 | * the correct order later on. |
| 628 | */ |
| 629 | rb_insert_augmented(&va->rb_node, |
| 630 | root, &free_vmap_area_rb_augment_cb); |
| 631 | va->subtree_max_size = 0; |
| 632 | } else { |
| 633 | rb_insert_color(&va->rb_node, root); |
| 634 | } |
| 635 | |
| 636 | /* Address-sort this list */ |
| 637 | list_add(&va->list, head); |
| 638 | } |
| 639 | |
| 640 | static __always_inline void |
| 641 | unlink_va(struct vmap_area *va, struct rb_root *root) |
| 642 | { |
Uladzislau Rezki (Sony) | 460e42d | 2019-07-11 20:59:03 -0700 | [diff] [blame] | 643 | if (WARN_ON(RB_EMPTY_NODE(&va->rb_node))) |
| 644 | return; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 645 | |
Uladzislau Rezki (Sony) | 460e42d | 2019-07-11 20:59:03 -0700 | [diff] [blame] | 646 | if (root == &free_vmap_area_root) |
| 647 | rb_erase_augmented(&va->rb_node, |
| 648 | root, &free_vmap_area_rb_augment_cb); |
| 649 | else |
| 650 | rb_erase(&va->rb_node, root); |
| 651 | |
| 652 | list_del(&va->list); |
| 653 | RB_CLEAR_NODE(&va->rb_node); |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Uladzislau Rezki (Sony) | bb850f4 | 2019-05-17 14:31:34 -0700 | [diff] [blame] | 656 | #if DEBUG_AUGMENT_PROPAGATE_CHECK |
| 657 | static void |
Uladzislau Rezki (Sony) | da27c9e | 2020-08-06 23:24:12 -0700 | [diff] [blame] | 658 | augment_tree_propagate_check(void) |
Uladzislau Rezki (Sony) | bb850f4 | 2019-05-17 14:31:34 -0700 | [diff] [blame] | 659 | { |
| 660 | struct vmap_area *va; |
Uladzislau Rezki (Sony) | da27c9e | 2020-08-06 23:24:12 -0700 | [diff] [blame] | 661 | unsigned long computed_size; |
Uladzislau Rezki (Sony) | bb850f4 | 2019-05-17 14:31:34 -0700 | [diff] [blame] | 662 | |
Uladzislau Rezki (Sony) | da27c9e | 2020-08-06 23:24:12 -0700 | [diff] [blame] | 663 | list_for_each_entry(va, &free_vmap_area_list, list) { |
| 664 | computed_size = compute_subtree_max_size(va); |
| 665 | if (computed_size != va->subtree_max_size) |
| 666 | pr_emerg("tree is corrupted: %lu, %lu\n", |
| 667 | va_size(va), va->subtree_max_size); |
Uladzislau Rezki (Sony) | bb850f4 | 2019-05-17 14:31:34 -0700 | [diff] [blame] | 668 | } |
Uladzislau Rezki (Sony) | bb850f4 | 2019-05-17 14:31:34 -0700 | [diff] [blame] | 669 | } |
| 670 | #endif |
| 671 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 672 | /* |
| 673 | * This function populates subtree_max_size from bottom to upper |
| 674 | * levels starting from VA point. The propagation must be done |
| 675 | * when VA size is modified by changing its va_start/va_end. Or |
| 676 | * in case of newly inserting of VA to the tree. |
| 677 | * |
| 678 | * It means that __augment_tree_propagate_from() must be called: |
| 679 | * - After VA has been inserted to the tree(free path); |
| 680 | * - After VA has been shrunk(allocation path); |
| 681 | * - After VA has been increased(merging path). |
| 682 | * |
| 683 | * Please note that, it does not mean that upper parent nodes |
| 684 | * and their subtree_max_size are recalculated all the time up |
| 685 | * to the root node. |
| 686 | * |
| 687 | * 4--8 |
| 688 | * /\ |
| 689 | * / \ |
| 690 | * / \ |
| 691 | * 2--2 8--8 |
| 692 | * |
| 693 | * For example if we modify the node 4, shrinking it to 2, then |
| 694 | * no any modification is required. If we shrink the node 2 to 1 |
| 695 | * its subtree_max_size is updated only, and set to 1. If we shrink |
| 696 | * the node 8 to 6, then its subtree_max_size is set to 6 and parent |
| 697 | * node becomes 4--6. |
| 698 | */ |
| 699 | static __always_inline void |
| 700 | augment_tree_propagate_from(struct vmap_area *va) |
| 701 | { |
Uladzislau Rezki (Sony) | 15ae144 | 2020-08-06 23:24:15 -0700 | [diff] [blame] | 702 | /* |
| 703 | * Populate the tree from bottom towards the root until |
| 704 | * the calculated maximum available size of checked node |
| 705 | * is equal to its current one. |
| 706 | */ |
| 707 | free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL); |
Uladzislau Rezki (Sony) | bb850f4 | 2019-05-17 14:31:34 -0700 | [diff] [blame] | 708 | |
| 709 | #if DEBUG_AUGMENT_PROPAGATE_CHECK |
Uladzislau Rezki (Sony) | da27c9e | 2020-08-06 23:24:12 -0700 | [diff] [blame] | 710 | augment_tree_propagate_check(); |
Uladzislau Rezki (Sony) | bb850f4 | 2019-05-17 14:31:34 -0700 | [diff] [blame] | 711 | #endif |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static void |
| 715 | insert_vmap_area(struct vmap_area *va, |
| 716 | struct rb_root *root, struct list_head *head) |
| 717 | { |
| 718 | struct rb_node **link; |
| 719 | struct rb_node *parent; |
| 720 | |
| 721 | link = find_va_links(va, root, NULL, &parent); |
Uladzislau Rezki (Sony) | 9c801f6 | 2020-08-06 23:24:24 -0700 | [diff] [blame] | 722 | if (link) |
| 723 | link_va(va, root, parent, link, head); |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static void |
| 727 | insert_vmap_area_augment(struct vmap_area *va, |
| 728 | struct rb_node *from, struct rb_root *root, |
| 729 | struct list_head *head) |
| 730 | { |
| 731 | struct rb_node **link; |
| 732 | struct rb_node *parent; |
| 733 | |
| 734 | if (from) |
| 735 | link = find_va_links(va, NULL, from, &parent); |
| 736 | else |
| 737 | link = find_va_links(va, root, NULL, &parent); |
| 738 | |
Uladzislau Rezki (Sony) | 9c801f6 | 2020-08-06 23:24:24 -0700 | [diff] [blame] | 739 | if (link) { |
| 740 | link_va(va, root, parent, link, head); |
| 741 | augment_tree_propagate_from(va); |
| 742 | } |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /* |
| 746 | * Merge de-allocated chunk of VA memory with previous |
| 747 | * and next free blocks. If coalesce is not done a new |
| 748 | * free area is inserted. If VA has been merged, it is |
| 749 | * freed. |
Uladzislau Rezki (Sony) | 9c801f6 | 2020-08-06 23:24:24 -0700 | [diff] [blame] | 750 | * |
| 751 | * Please note, it can return NULL in case of overlap |
| 752 | * ranges, followed by WARN() report. Despite it is a |
| 753 | * buggy behaviour, a system can be alive and keep |
| 754 | * ongoing. |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 755 | */ |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 756 | static __always_inline struct vmap_area * |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 757 | merge_or_add_vmap_area(struct vmap_area *va, |
| 758 | struct rb_root *root, struct list_head *head) |
| 759 | { |
| 760 | struct vmap_area *sibling; |
| 761 | struct list_head *next; |
| 762 | struct rb_node **link; |
| 763 | struct rb_node *parent; |
| 764 | bool merged = false; |
| 765 | |
| 766 | /* |
| 767 | * Find a place in the tree where VA potentially will be |
| 768 | * inserted, unless it is merged with its sibling/siblings. |
| 769 | */ |
| 770 | link = find_va_links(va, root, NULL, &parent); |
Uladzislau Rezki (Sony) | 9c801f6 | 2020-08-06 23:24:24 -0700 | [diff] [blame] | 771 | if (!link) |
| 772 | return NULL; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 773 | |
| 774 | /* |
| 775 | * Get next node of VA to check if merging can be done. |
| 776 | */ |
| 777 | next = get_va_next_sibling(parent, link); |
| 778 | if (unlikely(next == NULL)) |
| 779 | goto insert; |
| 780 | |
| 781 | /* |
| 782 | * start end |
| 783 | * | | |
| 784 | * |<------VA------>|<-----Next----->| |
| 785 | * | | |
| 786 | * start end |
| 787 | */ |
| 788 | if (next != head) { |
| 789 | sibling = list_entry(next, struct vmap_area, list); |
| 790 | if (sibling->va_start == va->va_end) { |
| 791 | sibling->va_start = va->va_start; |
| 792 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 793 | /* Free vmap_area object. */ |
| 794 | kmem_cache_free(vmap_area_cachep, va); |
| 795 | |
| 796 | /* Point to the new merged area. */ |
| 797 | va = sibling; |
| 798 | merged = true; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | /* |
| 803 | * start end |
| 804 | * | | |
| 805 | * |<-----Prev----->|<------VA------>| |
| 806 | * | | |
| 807 | * start end |
| 808 | */ |
| 809 | if (next->prev != head) { |
| 810 | sibling = list_entry(next->prev, struct vmap_area, list); |
| 811 | if (sibling->va_end == va->va_start) { |
Uladzislau Rezki (Sony) | 5dd7864 | 2020-08-06 23:24:09 -0700 | [diff] [blame] | 812 | /* |
| 813 | * If both neighbors are coalesced, it is important |
| 814 | * to unlink the "next" node first, followed by merging |
| 815 | * with "previous" one. Otherwise the tree might not be |
| 816 | * fully populated if a sibling's augmented value is |
| 817 | * "normalized" because of rotation operations. |
| 818 | */ |
Uladzislau Rezki (Sony) | 54f63d9 | 2019-07-11 20:59:00 -0700 | [diff] [blame] | 819 | if (merged) |
| 820 | unlink_va(va, root); |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 821 | |
Uladzislau Rezki (Sony) | 5dd7864 | 2020-08-06 23:24:09 -0700 | [diff] [blame] | 822 | sibling->va_end = va->va_end; |
| 823 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 824 | /* Free vmap_area object. */ |
| 825 | kmem_cache_free(vmap_area_cachep, va); |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 826 | |
| 827 | /* Point to the new merged area. */ |
| 828 | va = sibling; |
| 829 | merged = true; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | |
| 833 | insert: |
Uladzislau Rezki (Sony) | 5dd7864 | 2020-08-06 23:24:09 -0700 | [diff] [blame] | 834 | if (!merged) |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 835 | link_va(va, root, parent, link, head); |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 836 | |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 837 | return va; |
| 838 | } |
| 839 | |
| 840 | static __always_inline struct vmap_area * |
| 841 | merge_or_add_vmap_area_augment(struct vmap_area *va, |
| 842 | struct rb_root *root, struct list_head *head) |
| 843 | { |
| 844 | va = merge_or_add_vmap_area(va, root, head); |
| 845 | if (va) |
| 846 | augment_tree_propagate_from(va); |
| 847 | |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 848 | return va; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | static __always_inline bool |
| 852 | is_within_this_va(struct vmap_area *va, unsigned long size, |
| 853 | unsigned long align, unsigned long vstart) |
| 854 | { |
| 855 | unsigned long nva_start_addr; |
| 856 | |
| 857 | if (va->va_start > vstart) |
| 858 | nva_start_addr = ALIGN(va->va_start, align); |
| 859 | else |
| 860 | nva_start_addr = ALIGN(vstart, align); |
| 861 | |
| 862 | /* Can be overflowed due to big size or alignment. */ |
| 863 | if (nva_start_addr + size < nva_start_addr || |
| 864 | nva_start_addr < vstart) |
| 865 | return false; |
| 866 | |
| 867 | return (nva_start_addr + size <= va->va_end); |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * Find the first free block(lowest start address) in the tree, |
| 872 | * that will accomplish the request corresponding to passing |
| 873 | * parameters. |
| 874 | */ |
| 875 | static __always_inline struct vmap_area * |
| 876 | find_vmap_lowest_match(unsigned long size, |
| 877 | unsigned long align, unsigned long vstart) |
| 878 | { |
| 879 | struct vmap_area *va; |
| 880 | struct rb_node *node; |
| 881 | unsigned long length; |
| 882 | |
| 883 | /* Start from the root. */ |
| 884 | node = free_vmap_area_root.rb_node; |
| 885 | |
| 886 | /* Adjust the search size for alignment overhead. */ |
| 887 | length = size + align - 1; |
| 888 | |
| 889 | while (node) { |
| 890 | va = rb_entry(node, struct vmap_area, rb_node); |
| 891 | |
| 892 | if (get_subtree_max_size(node->rb_left) >= length && |
| 893 | vstart < va->va_start) { |
| 894 | node = node->rb_left; |
| 895 | } else { |
| 896 | if (is_within_this_va(va, size, align, vstart)) |
| 897 | return va; |
| 898 | |
| 899 | /* |
| 900 | * Does not make sense to go deeper towards the right |
| 901 | * sub-tree if it does not have a free block that is |
| 902 | * equal or bigger to the requested search length. |
| 903 | */ |
| 904 | if (get_subtree_max_size(node->rb_right) >= length) { |
| 905 | node = node->rb_right; |
| 906 | continue; |
| 907 | } |
| 908 | |
| 909 | /* |
Andrew Morton | 3806b04 | 2019-05-31 22:30:03 -0700 | [diff] [blame] | 910 | * OK. We roll back and find the first right sub-tree, |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 911 | * that will satisfy the search criteria. It can happen |
| 912 | * only once due to "vstart" restriction. |
| 913 | */ |
| 914 | while ((node = rb_parent(node))) { |
| 915 | va = rb_entry(node, struct vmap_area, rb_node); |
| 916 | if (is_within_this_va(va, size, align, vstart)) |
| 917 | return va; |
| 918 | |
| 919 | if (get_subtree_max_size(node->rb_right) >= length && |
| 920 | vstart <= va->va_start) { |
| 921 | node = node->rb_right; |
| 922 | break; |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | return NULL; |
| 929 | } |
| 930 | |
Uladzislau Rezki (Sony) | a6cf4e0 | 2019-05-17 14:31:37 -0700 | [diff] [blame] | 931 | #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK |
| 932 | #include <linux/random.h> |
| 933 | |
| 934 | static struct vmap_area * |
| 935 | find_vmap_lowest_linear_match(unsigned long size, |
| 936 | unsigned long align, unsigned long vstart) |
| 937 | { |
| 938 | struct vmap_area *va; |
| 939 | |
| 940 | list_for_each_entry(va, &free_vmap_area_list, list) { |
| 941 | if (!is_within_this_va(va, size, align, vstart)) |
| 942 | continue; |
| 943 | |
| 944 | return va; |
| 945 | } |
| 946 | |
| 947 | return NULL; |
| 948 | } |
| 949 | |
| 950 | static void |
| 951 | find_vmap_lowest_match_check(unsigned long size) |
| 952 | { |
| 953 | struct vmap_area *va_1, *va_2; |
| 954 | unsigned long vstart; |
| 955 | unsigned int rnd; |
| 956 | |
| 957 | get_random_bytes(&rnd, sizeof(rnd)); |
| 958 | vstart = VMALLOC_START + rnd; |
| 959 | |
| 960 | va_1 = find_vmap_lowest_match(size, 1, vstart); |
| 961 | va_2 = find_vmap_lowest_linear_match(size, 1, vstart); |
| 962 | |
| 963 | if (va_1 != va_2) |
| 964 | pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n", |
| 965 | va_1, va_2, vstart); |
| 966 | } |
| 967 | #endif |
| 968 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 969 | enum fit_type { |
| 970 | NOTHING_FIT = 0, |
| 971 | FL_FIT_TYPE = 1, /* full fit */ |
| 972 | LE_FIT_TYPE = 2, /* left edge fit */ |
| 973 | RE_FIT_TYPE = 3, /* right edge fit */ |
| 974 | NE_FIT_TYPE = 4 /* no edge fit */ |
| 975 | }; |
| 976 | |
| 977 | static __always_inline enum fit_type |
| 978 | classify_va_fit_type(struct vmap_area *va, |
| 979 | unsigned long nva_start_addr, unsigned long size) |
| 980 | { |
| 981 | enum fit_type type; |
| 982 | |
| 983 | /* Check if it is within VA. */ |
| 984 | if (nva_start_addr < va->va_start || |
| 985 | nva_start_addr + size > va->va_end) |
| 986 | return NOTHING_FIT; |
| 987 | |
| 988 | /* Now classify. */ |
| 989 | if (va->va_start == nva_start_addr) { |
| 990 | if (va->va_end == nva_start_addr + size) |
| 991 | type = FL_FIT_TYPE; |
| 992 | else |
| 993 | type = LE_FIT_TYPE; |
| 994 | } else if (va->va_end == nva_start_addr + size) { |
| 995 | type = RE_FIT_TYPE; |
| 996 | } else { |
| 997 | type = NE_FIT_TYPE; |
| 998 | } |
| 999 | |
| 1000 | return type; |
| 1001 | } |
| 1002 | |
| 1003 | static __always_inline int |
| 1004 | adjust_va_to_fit_type(struct vmap_area *va, |
| 1005 | unsigned long nva_start_addr, unsigned long size, |
| 1006 | enum fit_type type) |
| 1007 | { |
Arnd Bergmann | 2c92923 | 2019-06-28 12:07:09 -0700 | [diff] [blame] | 1008 | struct vmap_area *lva = NULL; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 1009 | |
| 1010 | if (type == FL_FIT_TYPE) { |
| 1011 | /* |
| 1012 | * No need to split VA, it fully fits. |
| 1013 | * |
| 1014 | * | | |
| 1015 | * V NVA V |
| 1016 | * |---------------| |
| 1017 | */ |
| 1018 | unlink_va(va, &free_vmap_area_root); |
| 1019 | kmem_cache_free(vmap_area_cachep, va); |
| 1020 | } else if (type == LE_FIT_TYPE) { |
| 1021 | /* |
| 1022 | * Split left edge of fit VA. |
| 1023 | * |
| 1024 | * | | |
| 1025 | * V NVA V R |
| 1026 | * |-------|-------| |
| 1027 | */ |
| 1028 | va->va_start += size; |
| 1029 | } else if (type == RE_FIT_TYPE) { |
| 1030 | /* |
| 1031 | * Split right edge of fit VA. |
| 1032 | * |
| 1033 | * | | |
| 1034 | * L V NVA V |
| 1035 | * |-------|-------| |
| 1036 | */ |
| 1037 | va->va_end = nva_start_addr; |
| 1038 | } else if (type == NE_FIT_TYPE) { |
| 1039 | /* |
| 1040 | * Split no edge of fit VA. |
| 1041 | * |
| 1042 | * | | |
| 1043 | * L V NVA V R |
| 1044 | * |---|-------|---| |
| 1045 | */ |
Uladzislau Rezki (Sony) | 82dd23e | 2019-07-11 20:58:57 -0700 | [diff] [blame] | 1046 | lva = __this_cpu_xchg(ne_fit_preload_node, NULL); |
| 1047 | if (unlikely(!lva)) { |
| 1048 | /* |
| 1049 | * For percpu allocator we do not do any pre-allocation |
| 1050 | * and leave it as it is. The reason is it most likely |
| 1051 | * never ends up with NE_FIT_TYPE splitting. In case of |
| 1052 | * percpu allocations offsets and sizes are aligned to |
| 1053 | * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE |
| 1054 | * are its main fitting cases. |
| 1055 | * |
| 1056 | * There are a few exceptions though, as an example it is |
| 1057 | * a first allocation (early boot up) when we have "one" |
| 1058 | * big free space that has to be split. |
Uladzislau Rezki (Sony) | 060650a | 2019-11-30 17:54:40 -0800 | [diff] [blame] | 1059 | * |
| 1060 | * Also we can hit this path in case of regular "vmap" |
| 1061 | * allocations, if "this" current CPU was not preloaded. |
| 1062 | * See the comment in alloc_vmap_area() why. If so, then |
| 1063 | * GFP_NOWAIT is used instead to get an extra object for |
| 1064 | * split purpose. That is rare and most time does not |
| 1065 | * occur. |
| 1066 | * |
| 1067 | * What happens if an allocation gets failed. Basically, |
| 1068 | * an "overflow" path is triggered to purge lazily freed |
| 1069 | * areas to free some memory, then, the "retry" path is |
| 1070 | * triggered to repeat one more time. See more details |
| 1071 | * in alloc_vmap_area() function. |
Uladzislau Rezki (Sony) | 82dd23e | 2019-07-11 20:58:57 -0700 | [diff] [blame] | 1072 | */ |
| 1073 | lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT); |
| 1074 | if (!lva) |
| 1075 | return -1; |
| 1076 | } |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 1077 | |
| 1078 | /* |
| 1079 | * Build the remainder. |
| 1080 | */ |
| 1081 | lva->va_start = va->va_start; |
| 1082 | lva->va_end = nva_start_addr; |
| 1083 | |
| 1084 | /* |
| 1085 | * Shrink this VA to remaining size. |
| 1086 | */ |
| 1087 | va->va_start = nva_start_addr + size; |
| 1088 | } else { |
| 1089 | return -1; |
| 1090 | } |
| 1091 | |
| 1092 | if (type != FL_FIT_TYPE) { |
| 1093 | augment_tree_propagate_from(va); |
| 1094 | |
Arnd Bergmann | 2c92923 | 2019-06-28 12:07:09 -0700 | [diff] [blame] | 1095 | if (lva) /* type == NE_FIT_TYPE */ |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 1096 | insert_vmap_area_augment(lva, &va->rb_node, |
| 1097 | &free_vmap_area_root, &free_vmap_area_list); |
| 1098 | } |
| 1099 | |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | /* |
| 1104 | * Returns a start address of the newly allocated area, if success. |
| 1105 | * Otherwise a vend is returned that indicates failure. |
| 1106 | */ |
| 1107 | static __always_inline unsigned long |
| 1108 | __alloc_vmap_area(unsigned long size, unsigned long align, |
Uladzislau Rezki (Sony) | cacca6b | 2019-07-11 20:58:53 -0700 | [diff] [blame] | 1109 | unsigned long vstart, unsigned long vend) |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 1110 | { |
| 1111 | unsigned long nva_start_addr; |
| 1112 | struct vmap_area *va; |
| 1113 | enum fit_type type; |
| 1114 | int ret; |
| 1115 | |
| 1116 | va = find_vmap_lowest_match(size, align, vstart); |
| 1117 | if (unlikely(!va)) |
| 1118 | return vend; |
| 1119 | |
| 1120 | if (va->va_start > vstart) |
| 1121 | nva_start_addr = ALIGN(va->va_start, align); |
| 1122 | else |
| 1123 | nva_start_addr = ALIGN(vstart, align); |
| 1124 | |
| 1125 | /* Check the "vend" restriction. */ |
| 1126 | if (nva_start_addr + size > vend) |
| 1127 | return vend; |
| 1128 | |
| 1129 | /* Classify what we have found. */ |
| 1130 | type = classify_va_fit_type(va, nva_start_addr, size); |
| 1131 | if (WARN_ON_ONCE(type == NOTHING_FIT)) |
| 1132 | return vend; |
| 1133 | |
| 1134 | /* Update the free vmap_area. */ |
| 1135 | ret = adjust_va_to_fit_type(va, nva_start_addr, size, type); |
| 1136 | if (ret) |
| 1137 | return vend; |
| 1138 | |
Uladzislau Rezki (Sony) | a6cf4e0 | 2019-05-17 14:31:37 -0700 | [diff] [blame] | 1139 | #if DEBUG_AUGMENT_LOWEST_MATCH_CHECK |
| 1140 | find_vmap_lowest_match_check(size); |
| 1141 | #endif |
| 1142 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 1143 | return nva_start_addr; |
| 1144 | } |
Chris Wilson | 4da56b9 | 2016-04-04 14:46:42 +0100 | [diff] [blame] | 1145 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1146 | /* |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 1147 | * Free a region of KVA allocated by alloc_vmap_area |
| 1148 | */ |
| 1149 | static void free_vmap_area(struct vmap_area *va) |
| 1150 | { |
| 1151 | /* |
| 1152 | * Remove from the busy tree/list. |
| 1153 | */ |
| 1154 | spin_lock(&vmap_area_lock); |
| 1155 | unlink_va(va, &vmap_area_root); |
| 1156 | spin_unlock(&vmap_area_lock); |
| 1157 | |
| 1158 | /* |
| 1159 | * Insert/Merge it back to the free tree/list. |
| 1160 | */ |
| 1161 | spin_lock(&free_vmap_area_lock); |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 1162 | merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list); |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 1163 | spin_unlock(&free_vmap_area_lock); |
| 1164 | } |
| 1165 | |
| 1166 | /* |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1167 | * Allocate a region of KVA of the specified size and alignment, within the |
| 1168 | * vstart and vend. |
| 1169 | */ |
| 1170 | static struct vmap_area *alloc_vmap_area(unsigned long size, |
| 1171 | unsigned long align, |
| 1172 | unsigned long vstart, unsigned long vend, |
| 1173 | int node, gfp_t gfp_mask) |
| 1174 | { |
Uladzislau Rezki (Sony) | 82dd23e | 2019-07-11 20:58:57 -0700 | [diff] [blame] | 1175 | struct vmap_area *va, *pva; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | unsigned long addr; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1177 | int purged = 0; |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 1178 | int ret; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1179 | |
Nick Piggin | 7766970 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 1180 | BUG_ON(!size); |
Alexander Kuleshov | 891c49a | 2015-11-05 18:46:51 -0800 | [diff] [blame] | 1181 | BUG_ON(offset_in_page(size)); |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 1182 | BUG_ON(!is_power_of_2(align)); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1183 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 1184 | if (unlikely(!vmap_initialized)) |
| 1185 | return ERR_PTR(-EBUSY); |
| 1186 | |
Christoph Hellwig | 5803ed2 | 2016-12-12 16:44:20 -0800 | [diff] [blame] | 1187 | might_sleep(); |
Uladzislau Rezki (Sony) | f07116d | 2019-11-30 17:54:37 -0800 | [diff] [blame] | 1188 | gfp_mask = gfp_mask & GFP_RECLAIM_MASK; |
Chris Wilson | 4da56b9 | 2016-04-04 14:46:42 +0100 | [diff] [blame] | 1189 | |
Uladzislau Rezki (Sony) | f07116d | 2019-11-30 17:54:37 -0800 | [diff] [blame] | 1190 | va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1191 | if (unlikely(!va)) |
| 1192 | return ERR_PTR(-ENOMEM); |
| 1193 | |
Catalin Marinas | 7f88f88 | 2013-11-12 15:07:45 -0800 | [diff] [blame] | 1194 | /* |
| 1195 | * Only scan the relevant parts containing pointers to other objects |
| 1196 | * to avoid false negatives. |
| 1197 | */ |
Uladzislau Rezki (Sony) | f07116d | 2019-11-30 17:54:37 -0800 | [diff] [blame] | 1198 | kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask); |
Catalin Marinas | 7f88f88 | 2013-11-12 15:07:45 -0800 | [diff] [blame] | 1199 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1200 | retry: |
Uladzislau Rezki (Sony) | 82dd23e | 2019-07-11 20:58:57 -0700 | [diff] [blame] | 1201 | /* |
Uladzislau Rezki (Sony) | 81f1ba5 | 2019-11-30 17:54:33 -0800 | [diff] [blame] | 1202 | * Preload this CPU with one extra vmap_area object. It is used |
| 1203 | * when fit type of free area is NE_FIT_TYPE. Please note, it |
| 1204 | * does not guarantee that an allocation occurs on a CPU that |
| 1205 | * is preloaded, instead we minimize the case when it is not. |
| 1206 | * It can happen because of cpu migration, because there is a |
| 1207 | * race until the below spinlock is taken. |
Uladzislau Rezki (Sony) | 82dd23e | 2019-07-11 20:58:57 -0700 | [diff] [blame] | 1208 | * |
| 1209 | * The preload is done in non-atomic context, thus it allows us |
| 1210 | * to use more permissive allocation masks to be more stable under |
Uladzislau Rezki (Sony) | 81f1ba5 | 2019-11-30 17:54:33 -0800 | [diff] [blame] | 1211 | * low memory condition and high memory pressure. In rare case, |
| 1212 | * if not preloaded, GFP_NOWAIT is used. |
Uladzislau Rezki (Sony) | 82dd23e | 2019-07-11 20:58:57 -0700 | [diff] [blame] | 1213 | * |
Uladzislau Rezki (Sony) | 81f1ba5 | 2019-11-30 17:54:33 -0800 | [diff] [blame] | 1214 | * Set "pva" to NULL here, because of "retry" path. |
Uladzislau Rezki (Sony) | 82dd23e | 2019-07-11 20:58:57 -0700 | [diff] [blame] | 1215 | */ |
Uladzislau Rezki (Sony) | 81f1ba5 | 2019-11-30 17:54:33 -0800 | [diff] [blame] | 1216 | pva = NULL; |
Uladzislau Rezki (Sony) | 82dd23e | 2019-07-11 20:58:57 -0700 | [diff] [blame] | 1217 | |
Uladzislau Rezki (Sony) | 81f1ba5 | 2019-11-30 17:54:33 -0800 | [diff] [blame] | 1218 | if (!this_cpu_read(ne_fit_preload_node)) |
| 1219 | /* |
| 1220 | * Even if it fails we do not really care about that. |
| 1221 | * Just proceed as it is. If needed "overflow" path |
| 1222 | * will refill the cache we allocate from. |
| 1223 | */ |
Uladzislau Rezki (Sony) | f07116d | 2019-11-30 17:54:37 -0800 | [diff] [blame] | 1224 | pva = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node); |
Uladzislau Rezki (Sony) | 82dd23e | 2019-07-11 20:58:57 -0700 | [diff] [blame] | 1225 | |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 1226 | spin_lock(&free_vmap_area_lock); |
Uladzislau Rezki (Sony) | 81f1ba5 | 2019-11-30 17:54:33 -0800 | [diff] [blame] | 1227 | |
| 1228 | if (pva && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, pva)) |
| 1229 | kmem_cache_free(vmap_area_cachep, pva); |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 1230 | |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 1231 | /* |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 1232 | * If an allocation fails, the "vend" address is |
| 1233 | * returned. Therefore trigger the overflow path. |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 1234 | */ |
Uladzislau Rezki (Sony) | cacca6b | 2019-07-11 20:58:53 -0700 | [diff] [blame] | 1235 | addr = __alloc_vmap_area(size, align, vstart, vend); |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 1236 | spin_unlock(&free_vmap_area_lock); |
| 1237 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 1238 | if (unlikely(addr == vend)) |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 1239 | goto overflow; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1240 | |
| 1241 | va->va_start = addr; |
| 1242 | va->va_end = addr + size; |
Pengfei Li | 688fcbf | 2019-09-23 15:36:39 -0700 | [diff] [blame] | 1243 | va->vm = NULL; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 1244 | |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 1245 | |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 1246 | spin_lock(&vmap_area_lock); |
| 1247 | insert_vmap_area(va, &vmap_area_root, &vmap_area_list); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1248 | spin_unlock(&vmap_area_lock); |
| 1249 | |
Wang Xiaoqiang | 61e1655 | 2016-01-15 16:57:19 -0800 | [diff] [blame] | 1250 | BUG_ON(!IS_ALIGNED(va->va_start, align)); |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 1251 | BUG_ON(va->va_start < vstart); |
| 1252 | BUG_ON(va->va_end > vend); |
| 1253 | |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 1254 | ret = kasan_populate_vmalloc(addr, size); |
| 1255 | if (ret) { |
| 1256 | free_vmap_area(va); |
| 1257 | return ERR_PTR(ret); |
| 1258 | } |
| 1259 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1260 | return va; |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 1261 | |
| 1262 | overflow: |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 1263 | if (!purged) { |
| 1264 | purge_vmap_area_lazy(); |
| 1265 | purged = 1; |
| 1266 | goto retry; |
| 1267 | } |
Chris Wilson | 4da56b9 | 2016-04-04 14:46:42 +0100 | [diff] [blame] | 1268 | |
| 1269 | if (gfpflags_allow_blocking(gfp_mask)) { |
| 1270 | unsigned long freed = 0; |
| 1271 | blocking_notifier_call_chain(&vmap_notify_list, 0, &freed); |
| 1272 | if (freed > 0) { |
| 1273 | purged = 0; |
| 1274 | goto retry; |
| 1275 | } |
| 1276 | } |
| 1277 | |
Florian Fainelli | 03497d7 | 2017-04-27 11:19:00 -0700 | [diff] [blame] | 1278 | if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) |
Joe Perches | 756a025 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 1279 | pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n", |
| 1280 | size); |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 1281 | |
| 1282 | kmem_cache_free(vmap_area_cachep, va); |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 1283 | return ERR_PTR(-EBUSY); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Chris Wilson | 4da56b9 | 2016-04-04 14:46:42 +0100 | [diff] [blame] | 1286 | int register_vmap_purge_notifier(struct notifier_block *nb) |
| 1287 | { |
| 1288 | return blocking_notifier_chain_register(&vmap_notify_list, nb); |
| 1289 | } |
| 1290 | EXPORT_SYMBOL_GPL(register_vmap_purge_notifier); |
| 1291 | |
| 1292 | int unregister_vmap_purge_notifier(struct notifier_block *nb) |
| 1293 | { |
| 1294 | return blocking_notifier_chain_unregister(&vmap_notify_list, nb); |
| 1295 | } |
| 1296 | EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier); |
| 1297 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1298 | /* |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1299 | * lazy_max_pages is the maximum amount of virtual address space we gather up |
| 1300 | * before attempting to purge with a TLB flush. |
| 1301 | * |
| 1302 | * There is a tradeoff here: a larger number will cover more kernel page tables |
| 1303 | * and take slightly longer to purge, but it will linearly reduce the number of |
| 1304 | * global TLB flushes that must be performed. It would seem natural to scale |
| 1305 | * this number up linearly with the number of CPUs (because vmapping activity |
| 1306 | * could also scale linearly with the number of CPUs), however it is likely |
| 1307 | * that in practice, workloads might be constrained in other ways that mean |
| 1308 | * vmap activity will not scale linearly with CPUs. Also, I want to be |
| 1309 | * conservative and not introduce a big latency on huge systems, so go with |
| 1310 | * a less aggressive log scale. It will still be an improvement over the old |
| 1311 | * code, and it will be simple to change the scale factor if we find that it |
| 1312 | * becomes a problem on bigger systems. |
| 1313 | */ |
| 1314 | static unsigned long lazy_max_pages(void) |
| 1315 | { |
| 1316 | unsigned int log; |
| 1317 | |
| 1318 | log = fls(num_online_cpus()); |
| 1319 | |
| 1320 | return log * (32UL * 1024 * 1024 / PAGE_SIZE); |
| 1321 | } |
| 1322 | |
Uladzislau Rezki (Sony) | 4d36e6f | 2019-05-14 15:41:25 -0700 | [diff] [blame] | 1323 | static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1324 | |
Christoph Hellwig | 0574ecd | 2016-12-12 16:44:07 -0800 | [diff] [blame] | 1325 | /* |
| 1326 | * Serialize vmap purging. There is no actual criticial section protected |
| 1327 | * by this look, but we want to avoid concurrent calls for performance |
| 1328 | * reasons and to make the pcpu_get_vm_areas more deterministic. |
| 1329 | */ |
Christoph Hellwig | f9e0997 | 2016-12-12 16:44:23 -0800 | [diff] [blame] | 1330 | static DEFINE_MUTEX(vmap_purge_lock); |
Christoph Hellwig | 0574ecd | 2016-12-12 16:44:07 -0800 | [diff] [blame] | 1331 | |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 1332 | /* for per-CPU blocks */ |
| 1333 | static void purge_fragmented_blocks_allcpus(void); |
| 1334 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1335 | /* |
Cliff Wickman | 3ee48b6 | 2010-09-16 11:44:02 -0500 | [diff] [blame] | 1336 | * called before a call to iounmap() if the caller wants vm_area_struct's |
| 1337 | * immediately freed. |
| 1338 | */ |
| 1339 | void set_iounmap_nonlazy(void) |
| 1340 | { |
Uladzislau Rezki (Sony) | 4d36e6f | 2019-05-14 15:41:25 -0700 | [diff] [blame] | 1341 | atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1); |
Cliff Wickman | 3ee48b6 | 2010-09-16 11:44:02 -0500 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | /* |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1345 | * Purges all lazily-freed vmap areas. |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1346 | */ |
Christoph Hellwig | 0574ecd | 2016-12-12 16:44:07 -0800 | [diff] [blame] | 1347 | static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1348 | { |
Uladzislau Rezki (Sony) | 4d36e6f | 2019-05-14 15:41:25 -0700 | [diff] [blame] | 1349 | unsigned long resched_threshold; |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 1350 | struct list_head local_pure_list; |
| 1351 | struct vmap_area *va, *n_va; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1352 | |
Christoph Hellwig | 0574ecd | 2016-12-12 16:44:07 -0800 | [diff] [blame] | 1353 | lockdep_assert_held(&vmap_purge_lock); |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 1354 | |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 1355 | spin_lock(&purge_vmap_area_lock); |
| 1356 | purge_vmap_area_root = RB_ROOT; |
| 1357 | list_replace_init(&purge_vmap_area_list, &local_pure_list); |
| 1358 | spin_unlock(&purge_vmap_area_lock); |
| 1359 | |
| 1360 | if (unlikely(list_empty(&local_pure_list))) |
Uladzislau Rezki (Sony) | 68571be9 | 2019-05-14 15:41:22 -0700 | [diff] [blame] | 1361 | return false; |
| 1362 | |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 1363 | start = min(start, |
| 1364 | list_first_entry(&local_pure_list, |
| 1365 | struct vmap_area, list)->va_start); |
| 1366 | |
| 1367 | end = max(end, |
| 1368 | list_last_entry(&local_pure_list, |
| 1369 | struct vmap_area, list)->va_end); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1370 | |
Christoph Hellwig | 0574ecd | 2016-12-12 16:44:07 -0800 | [diff] [blame] | 1371 | flush_tlb_kernel_range(start, end); |
Uladzislau Rezki (Sony) | 4d36e6f | 2019-05-14 15:41:25 -0700 | [diff] [blame] | 1372 | resched_threshold = lazy_max_pages() << 1; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1373 | |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 1374 | spin_lock(&free_vmap_area_lock); |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 1375 | list_for_each_entry_safe(va, n_va, &local_pure_list, list) { |
Uladzislau Rezki (Sony) | 4d36e6f | 2019-05-14 15:41:25 -0700 | [diff] [blame] | 1376 | unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT; |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 1377 | unsigned long orig_start = va->va_start; |
| 1378 | unsigned long orig_end = va->va_end; |
Joel Fernandes | 763b218 | 2016-12-12 16:44:26 -0800 | [diff] [blame] | 1379 | |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 1380 | /* |
| 1381 | * Finally insert or merge lazily-freed area. It is |
| 1382 | * detached and there is no need to "unlink" it from |
| 1383 | * anything. |
| 1384 | */ |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 1385 | va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root, |
| 1386 | &free_vmap_area_list); |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 1387 | |
Uladzislau Rezki (Sony) | 9c801f6 | 2020-08-06 23:24:24 -0700 | [diff] [blame] | 1388 | if (!va) |
| 1389 | continue; |
| 1390 | |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 1391 | if (is_vmalloc_or_module_addr((void *)orig_start)) |
| 1392 | kasan_release_vmalloc(orig_start, orig_end, |
| 1393 | va->va_start, va->va_end); |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 1394 | |
Uladzislau Rezki (Sony) | 4d36e6f | 2019-05-14 15:41:25 -0700 | [diff] [blame] | 1395 | atomic_long_sub(nr, &vmap_lazy_nr); |
Uladzislau Rezki (Sony) | 68571be9 | 2019-05-14 15:41:22 -0700 | [diff] [blame] | 1396 | |
Uladzislau Rezki (Sony) | 4d36e6f | 2019-05-14 15:41:25 -0700 | [diff] [blame] | 1397 | if (atomic_long_read(&vmap_lazy_nr) < resched_threshold) |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 1398 | cond_resched_lock(&free_vmap_area_lock); |
Joel Fernandes | 763b218 | 2016-12-12 16:44:26 -0800 | [diff] [blame] | 1399 | } |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 1400 | spin_unlock(&free_vmap_area_lock); |
Christoph Hellwig | 0574ecd | 2016-12-12 16:44:07 -0800 | [diff] [blame] | 1401 | return true; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | /* |
Nick Piggin | 496850e | 2008-11-19 15:36:33 -0800 | [diff] [blame] | 1405 | * Kick off a purge of the outstanding lazy areas. Don't bother if somebody |
| 1406 | * is already purging. |
| 1407 | */ |
| 1408 | static void try_purge_vmap_area_lazy(void) |
| 1409 | { |
Christoph Hellwig | f9e0997 | 2016-12-12 16:44:23 -0800 | [diff] [blame] | 1410 | if (mutex_trylock(&vmap_purge_lock)) { |
Christoph Hellwig | 0574ecd | 2016-12-12 16:44:07 -0800 | [diff] [blame] | 1411 | __purge_vmap_area_lazy(ULONG_MAX, 0); |
Christoph Hellwig | f9e0997 | 2016-12-12 16:44:23 -0800 | [diff] [blame] | 1412 | mutex_unlock(&vmap_purge_lock); |
Christoph Hellwig | 0574ecd | 2016-12-12 16:44:07 -0800 | [diff] [blame] | 1413 | } |
Nick Piggin | 496850e | 2008-11-19 15:36:33 -0800 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | /* |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1417 | * Kick off a purge of the outstanding lazy areas. |
| 1418 | */ |
| 1419 | static void purge_vmap_area_lazy(void) |
| 1420 | { |
Christoph Hellwig | f9e0997 | 2016-12-12 16:44:23 -0800 | [diff] [blame] | 1421 | mutex_lock(&vmap_purge_lock); |
Christoph Hellwig | 0574ecd | 2016-12-12 16:44:07 -0800 | [diff] [blame] | 1422 | purge_fragmented_blocks_allcpus(); |
| 1423 | __purge_vmap_area_lazy(ULONG_MAX, 0); |
Christoph Hellwig | f9e0997 | 2016-12-12 16:44:23 -0800 | [diff] [blame] | 1424 | mutex_unlock(&vmap_purge_lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | /* |
Jeremy Fitzhardinge | 64141da | 2010-12-02 14:31:18 -0800 | [diff] [blame] | 1428 | * Free a vmap area, caller ensuring that the area has been unmapped |
| 1429 | * and flush_cache_vunmap had been called for the correct range |
| 1430 | * previously. |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1431 | */ |
Jeremy Fitzhardinge | 64141da | 2010-12-02 14:31:18 -0800 | [diff] [blame] | 1432 | static void free_vmap_area_noflush(struct vmap_area *va) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1433 | { |
Uladzislau Rezki (Sony) | 4d36e6f | 2019-05-14 15:41:25 -0700 | [diff] [blame] | 1434 | unsigned long nr_lazy; |
Chris Wilson | 80c4bd7 | 2016-05-20 16:57:38 -0700 | [diff] [blame] | 1435 | |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 1436 | spin_lock(&vmap_area_lock); |
| 1437 | unlink_va(va, &vmap_area_root); |
| 1438 | spin_unlock(&vmap_area_lock); |
| 1439 | |
Uladzislau Rezki (Sony) | 4d36e6f | 2019-05-14 15:41:25 -0700 | [diff] [blame] | 1440 | nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >> |
| 1441 | PAGE_SHIFT, &vmap_lazy_nr); |
Chris Wilson | 80c4bd7 | 2016-05-20 16:57:38 -0700 | [diff] [blame] | 1442 | |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 1443 | /* |
| 1444 | * Merge or place it to the purge tree/list. |
| 1445 | */ |
| 1446 | spin_lock(&purge_vmap_area_lock); |
| 1447 | merge_or_add_vmap_area(va, |
| 1448 | &purge_vmap_area_root, &purge_vmap_area_list); |
| 1449 | spin_unlock(&purge_vmap_area_lock); |
Chris Wilson | 80c4bd7 | 2016-05-20 16:57:38 -0700 | [diff] [blame] | 1450 | |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 1451 | /* After this point, we may free va at any time */ |
Chris Wilson | 80c4bd7 | 2016-05-20 16:57:38 -0700 | [diff] [blame] | 1452 | if (unlikely(nr_lazy > lazy_max_pages())) |
Nick Piggin | 496850e | 2008-11-19 15:36:33 -0800 | [diff] [blame] | 1453 | try_purge_vmap_area_lazy(); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
Nick Piggin | b29acbd | 2008-12-01 13:13:47 -0800 | [diff] [blame] | 1456 | /* |
| 1457 | * Free and unmap a vmap area |
| 1458 | */ |
| 1459 | static void free_unmap_vmap_area(struct vmap_area *va) |
| 1460 | { |
| 1461 | flush_cache_vunmap(va->va_start, va->va_end); |
Christoph Hellwig | 855e57a | 2020-06-01 21:51:23 -0700 | [diff] [blame] | 1462 | unmap_kernel_range_noflush(va->va_start, va->va_end - va->va_start); |
Vlastimil Babka | 8e57f8a | 2020-01-13 16:29:20 -0800 | [diff] [blame] | 1463 | if (debug_pagealloc_enabled_static()) |
Chintan Pandya | 82a2e92 | 2018-06-07 17:06:46 -0700 | [diff] [blame] | 1464 | flush_tlb_kernel_range(va->va_start, va->va_end); |
| 1465 | |
Christoph Hellwig | c8eef01 | 2016-12-12 16:44:01 -0800 | [diff] [blame] | 1466 | free_vmap_area_noflush(va); |
Nick Piggin | b29acbd | 2008-12-01 13:13:47 -0800 | [diff] [blame] | 1467 | } |
| 1468 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1469 | static struct vmap_area *find_vmap_area(unsigned long addr) |
| 1470 | { |
| 1471 | struct vmap_area *va; |
| 1472 | |
| 1473 | spin_lock(&vmap_area_lock); |
| 1474 | va = __find_vmap_area(addr); |
| 1475 | spin_unlock(&vmap_area_lock); |
| 1476 | |
| 1477 | return va; |
| 1478 | } |
| 1479 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1480 | /*** Per cpu kva allocator ***/ |
| 1481 | |
| 1482 | /* |
| 1483 | * vmap space is limited especially on 32 bit architectures. Ensure there is |
| 1484 | * room for at least 16 percpu vmap blocks per CPU. |
| 1485 | */ |
| 1486 | /* |
| 1487 | * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able |
| 1488 | * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess |
| 1489 | * instead (we just need a rough idea) |
| 1490 | */ |
| 1491 | #if BITS_PER_LONG == 32 |
| 1492 | #define VMALLOC_SPACE (128UL*1024*1024) |
| 1493 | #else |
| 1494 | #define VMALLOC_SPACE (128UL*1024*1024*1024) |
| 1495 | #endif |
| 1496 | |
| 1497 | #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) |
| 1498 | #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ |
| 1499 | #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ |
| 1500 | #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) |
| 1501 | #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ |
| 1502 | #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ |
Clemens Ladisch | f982f915 | 2011-06-21 22:09:50 +0200 | [diff] [blame] | 1503 | #define VMAP_BBMAP_BITS \ |
| 1504 | VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ |
| 1505 | VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ |
| 1506 | VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16)) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1507 | |
| 1508 | #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) |
| 1509 | |
| 1510 | struct vmap_block_queue { |
| 1511 | spinlock_t lock; |
| 1512 | struct list_head free; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1513 | }; |
| 1514 | |
| 1515 | struct vmap_block { |
| 1516 | spinlock_t lock; |
| 1517 | struct vmap_area *va; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1518 | unsigned long free, dirty; |
Roman Pen | 7d61bfe | 2015-04-15 16:13:55 -0700 | [diff] [blame] | 1519 | unsigned long dirty_min, dirty_max; /*< dirty range */ |
Nick Piggin | de56042 | 2010-02-01 22:24:18 +1100 | [diff] [blame] | 1520 | struct list_head free_list; |
| 1521 | struct rcu_head rcu_head; |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 1522 | struct list_head purge; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1523 | }; |
| 1524 | |
| 1525 | /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ |
| 1526 | static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); |
| 1527 | |
| 1528 | /* |
Matthew Wilcox (Oracle) | 0f14599 | 2020-08-06 23:24:05 -0700 | [diff] [blame] | 1529 | * XArray of vmap blocks, indexed by address, to quickly find a vmap block |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1530 | * in the free path. Could get rid of this if we change the API to return a |
| 1531 | * "cookie" from alloc, to be passed to free. But no big deal yet. |
| 1532 | */ |
Matthew Wilcox (Oracle) | 0f14599 | 2020-08-06 23:24:05 -0700 | [diff] [blame] | 1533 | static DEFINE_XARRAY(vmap_blocks); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1534 | |
| 1535 | /* |
| 1536 | * We should probably have a fallback mechanism to allocate virtual memory |
| 1537 | * out of partially filled vmap blocks. However vmap block sizing should be |
| 1538 | * fairly reasonable according to the vmalloc size, so it shouldn't be a |
| 1539 | * big problem. |
| 1540 | */ |
| 1541 | |
| 1542 | static unsigned long addr_to_vb_idx(unsigned long addr) |
| 1543 | { |
| 1544 | addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); |
| 1545 | addr /= VMAP_BLOCK_SIZE; |
| 1546 | return addr; |
| 1547 | } |
| 1548 | |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1549 | static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off) |
| 1550 | { |
| 1551 | unsigned long addr; |
| 1552 | |
| 1553 | addr = va_start + (pages_off << PAGE_SHIFT); |
| 1554 | BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start)); |
| 1555 | return (void *)addr; |
| 1556 | } |
| 1557 | |
| 1558 | /** |
| 1559 | * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this |
| 1560 | * block. Of course pages number can't exceed VMAP_BBMAP_BITS |
| 1561 | * @order: how many 2^order pages should be occupied in newly allocated block |
| 1562 | * @gfp_mask: flags for the page level allocator |
| 1563 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1564 | * Return: virtual address in a newly allocated block or ERR_PTR(-errno) |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1565 | */ |
| 1566 | static void *new_vmap_block(unsigned int order, gfp_t gfp_mask) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1567 | { |
| 1568 | struct vmap_block_queue *vbq; |
| 1569 | struct vmap_block *vb; |
| 1570 | struct vmap_area *va; |
| 1571 | unsigned long vb_idx; |
| 1572 | int node, err; |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1573 | void *vaddr; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1574 | |
| 1575 | node = numa_node_id(); |
| 1576 | |
| 1577 | vb = kmalloc_node(sizeof(struct vmap_block), |
| 1578 | gfp_mask & GFP_RECLAIM_MASK, node); |
| 1579 | if (unlikely(!vb)) |
| 1580 | return ERR_PTR(-ENOMEM); |
| 1581 | |
| 1582 | va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, |
| 1583 | VMALLOC_START, VMALLOC_END, |
| 1584 | node, gfp_mask); |
Tobias Klauser | ddf9c6d4 | 2011-01-13 15:46:15 -0800 | [diff] [blame] | 1585 | if (IS_ERR(va)) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1586 | kfree(vb); |
Julia Lawall | e7d8634 | 2010-08-09 17:18:28 -0700 | [diff] [blame] | 1587 | return ERR_CAST(va); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1590 | vaddr = vmap_block_vaddr(va->va_start, 0); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1591 | spin_lock_init(&vb->lock); |
| 1592 | vb->va = va; |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1593 | /* At least something should be left free */ |
| 1594 | BUG_ON(VMAP_BBMAP_BITS <= (1UL << order)); |
| 1595 | vb->free = VMAP_BBMAP_BITS - (1UL << order); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1596 | vb->dirty = 0; |
Roman Pen | 7d61bfe | 2015-04-15 16:13:55 -0700 | [diff] [blame] | 1597 | vb->dirty_min = VMAP_BBMAP_BITS; |
| 1598 | vb->dirty_max = 0; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1599 | INIT_LIST_HEAD(&vb->free_list); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1600 | |
| 1601 | vb_idx = addr_to_vb_idx(va->va_start); |
Matthew Wilcox (Oracle) | 0f14599 | 2020-08-06 23:24:05 -0700 | [diff] [blame] | 1602 | err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask); |
| 1603 | if (err) { |
| 1604 | kfree(vb); |
| 1605 | free_vmap_area(va); |
| 1606 | return ERR_PTR(err); |
| 1607 | } |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1608 | |
| 1609 | vbq = &get_cpu_var(vmap_block_queue); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1610 | spin_lock(&vbq->lock); |
Roman Pen | 68ac546 | 2015-04-15 16:13:48 -0700 | [diff] [blame] | 1611 | list_add_tail_rcu(&vb->free_list, &vbq->free); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1612 | spin_unlock(&vbq->lock); |
Tejun Heo | 3f04ba8 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 1613 | put_cpu_var(vmap_block_queue); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1614 | |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1615 | return vaddr; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1618 | static void free_vmap_block(struct vmap_block *vb) |
| 1619 | { |
| 1620 | struct vmap_block *tmp; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1621 | |
Matthew Wilcox (Oracle) | 0f14599 | 2020-08-06 23:24:05 -0700 | [diff] [blame] | 1622 | tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start)); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1623 | BUG_ON(tmp != vb); |
| 1624 | |
Jeremy Fitzhardinge | 64141da | 2010-12-02 14:31:18 -0800 | [diff] [blame] | 1625 | free_vmap_area_noflush(vb->va); |
Lai Jiangshan | 22a3c7d | 2011-03-18 12:13:08 +0800 | [diff] [blame] | 1626 | kfree_rcu(vb, rcu_head); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 1629 | static void purge_fragmented_blocks(int cpu) |
| 1630 | { |
| 1631 | LIST_HEAD(purge); |
| 1632 | struct vmap_block *vb; |
| 1633 | struct vmap_block *n_vb; |
| 1634 | struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); |
| 1635 | |
| 1636 | rcu_read_lock(); |
| 1637 | list_for_each_entry_rcu(vb, &vbq->free, free_list) { |
| 1638 | |
| 1639 | if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS)) |
| 1640 | continue; |
| 1641 | |
| 1642 | spin_lock(&vb->lock); |
| 1643 | if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) { |
| 1644 | vb->free = 0; /* prevent further allocs after releasing lock */ |
| 1645 | vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */ |
Roman Pen | 7d61bfe | 2015-04-15 16:13:55 -0700 | [diff] [blame] | 1646 | vb->dirty_min = 0; |
| 1647 | vb->dirty_max = VMAP_BBMAP_BITS; |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 1648 | spin_lock(&vbq->lock); |
| 1649 | list_del_rcu(&vb->free_list); |
| 1650 | spin_unlock(&vbq->lock); |
| 1651 | spin_unlock(&vb->lock); |
| 1652 | list_add_tail(&vb->purge, &purge); |
| 1653 | } else |
| 1654 | spin_unlock(&vb->lock); |
| 1655 | } |
| 1656 | rcu_read_unlock(); |
| 1657 | |
| 1658 | list_for_each_entry_safe(vb, n_vb, &purge, purge) { |
| 1659 | list_del(&vb->purge); |
| 1660 | free_vmap_block(vb); |
| 1661 | } |
| 1662 | } |
| 1663 | |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 1664 | static void purge_fragmented_blocks_allcpus(void) |
| 1665 | { |
| 1666 | int cpu; |
| 1667 | |
| 1668 | for_each_possible_cpu(cpu) |
| 1669 | purge_fragmented_blocks(cpu); |
| 1670 | } |
| 1671 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1672 | static void *vb_alloc(unsigned long size, gfp_t gfp_mask) |
| 1673 | { |
| 1674 | struct vmap_block_queue *vbq; |
| 1675 | struct vmap_block *vb; |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1676 | void *vaddr = NULL; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1677 | unsigned int order; |
| 1678 | |
Alexander Kuleshov | 891c49a | 2015-11-05 18:46:51 -0800 | [diff] [blame] | 1679 | BUG_ON(offset_in_page(size)); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1680 | BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); |
Jan Kara | aa91c4d | 2012-07-31 16:41:37 -0700 | [diff] [blame] | 1681 | if (WARN_ON(size == 0)) { |
| 1682 | /* |
| 1683 | * Allocating 0 bytes isn't what caller wants since |
| 1684 | * get_order(0) returns funny result. Just warn and terminate |
| 1685 | * early. |
| 1686 | */ |
| 1687 | return NULL; |
| 1688 | } |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1689 | order = get_order(size); |
| 1690 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1691 | rcu_read_lock(); |
| 1692 | vbq = &get_cpu_var(vmap_block_queue); |
| 1693 | list_for_each_entry_rcu(vb, &vbq->free, free_list) { |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1694 | unsigned long pages_off; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1695 | |
| 1696 | spin_lock(&vb->lock); |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1697 | if (vb->free < (1UL << order)) { |
| 1698 | spin_unlock(&vb->lock); |
| 1699 | continue; |
| 1700 | } |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 1701 | |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1702 | pages_off = VMAP_BBMAP_BITS - vb->free; |
| 1703 | vaddr = vmap_block_vaddr(vb->va->va_start, pages_off); |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 1704 | vb->free -= 1UL << order; |
| 1705 | if (vb->free == 0) { |
| 1706 | spin_lock(&vbq->lock); |
| 1707 | list_del_rcu(&vb->free_list); |
| 1708 | spin_unlock(&vbq->lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1709 | } |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1710 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1711 | spin_unlock(&vb->lock); |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 1712 | break; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1713 | } |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 1714 | |
Tejun Heo | 3f04ba8 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 1715 | put_cpu_var(vmap_block_queue); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1716 | rcu_read_unlock(); |
| 1717 | |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1718 | /* Allocate new block if nothing was found */ |
| 1719 | if (!vaddr) |
| 1720 | vaddr = new_vmap_block(order, gfp_mask); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1721 | |
Roman Pen | cf725ce | 2015-04-15 16:13:52 -0700 | [diff] [blame] | 1722 | return vaddr; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Christoph Hellwig | 78a0e8c | 2020-06-01 21:51:02 -0700 | [diff] [blame] | 1725 | static void vb_free(unsigned long addr, unsigned long size) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1726 | { |
| 1727 | unsigned long offset; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1728 | unsigned int order; |
| 1729 | struct vmap_block *vb; |
| 1730 | |
Alexander Kuleshov | 891c49a | 2015-11-05 18:46:51 -0800 | [diff] [blame] | 1731 | BUG_ON(offset_in_page(size)); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1732 | BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); |
Nick Piggin | b29acbd | 2008-12-01 13:13:47 -0800 | [diff] [blame] | 1733 | |
Christoph Hellwig | 78a0e8c | 2020-06-01 21:51:02 -0700 | [diff] [blame] | 1734 | flush_cache_vunmap(addr, addr + size); |
Nick Piggin | b29acbd | 2008-12-01 13:13:47 -0800 | [diff] [blame] | 1735 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1736 | order = get_order(size); |
Christoph Hellwig | 78a0e8c | 2020-06-01 21:51:02 -0700 | [diff] [blame] | 1737 | offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT; |
Matthew Wilcox (Oracle) | 0f14599 | 2020-08-06 23:24:05 -0700 | [diff] [blame] | 1738 | vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr)); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1739 | |
Christoph Hellwig | b521c43 | 2020-06-01 21:51:07 -0700 | [diff] [blame] | 1740 | unmap_kernel_range_noflush(addr, size); |
Jeremy Fitzhardinge | 64141da | 2010-12-02 14:31:18 -0800 | [diff] [blame] | 1741 | |
Vlastimil Babka | 8e57f8a | 2020-01-13 16:29:20 -0800 | [diff] [blame] | 1742 | if (debug_pagealloc_enabled_static()) |
Christoph Hellwig | 78a0e8c | 2020-06-01 21:51:02 -0700 | [diff] [blame] | 1743 | flush_tlb_kernel_range(addr, addr + size); |
Chintan Pandya | 82a2e92 | 2018-06-07 17:06:46 -0700 | [diff] [blame] | 1744 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1745 | spin_lock(&vb->lock); |
Roman Pen | 7d61bfe | 2015-04-15 16:13:55 -0700 | [diff] [blame] | 1746 | |
| 1747 | /* Expand dirty range */ |
| 1748 | vb->dirty_min = min(vb->dirty_min, offset); |
| 1749 | vb->dirty_max = max(vb->dirty_max, offset + (1UL << order)); |
MinChan Kim | d086817 | 2009-03-31 15:19:26 -0700 | [diff] [blame] | 1750 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1751 | vb->dirty += 1UL << order; |
| 1752 | if (vb->dirty == VMAP_BBMAP_BITS) { |
Nick Piggin | de56042 | 2010-02-01 22:24:18 +1100 | [diff] [blame] | 1753 | BUG_ON(vb->free); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1754 | spin_unlock(&vb->lock); |
| 1755 | free_vmap_block(vb); |
| 1756 | } else |
| 1757 | spin_unlock(&vb->lock); |
| 1758 | } |
| 1759 | |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 1760 | static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1761 | { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1762 | int cpu; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1763 | |
Jeremy Fitzhardinge | 9b46333 | 2008-10-28 19:22:34 +1100 | [diff] [blame] | 1764 | if (unlikely(!vmap_initialized)) |
| 1765 | return; |
| 1766 | |
Christoph Hellwig | 5803ed2 | 2016-12-12 16:44:20 -0800 | [diff] [blame] | 1767 | might_sleep(); |
| 1768 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1769 | for_each_possible_cpu(cpu) { |
| 1770 | struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); |
| 1771 | struct vmap_block *vb; |
| 1772 | |
| 1773 | rcu_read_lock(); |
| 1774 | list_for_each_entry_rcu(vb, &vbq->free, free_list) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1775 | spin_lock(&vb->lock); |
Roman Pen | 7d61bfe | 2015-04-15 16:13:55 -0700 | [diff] [blame] | 1776 | if (vb->dirty) { |
| 1777 | unsigned long va_start = vb->va->va_start; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1778 | unsigned long s, e; |
Joonsoo Kim | b136be5e | 2013-09-11 14:21:40 -0700 | [diff] [blame] | 1779 | |
Roman Pen | 7d61bfe | 2015-04-15 16:13:55 -0700 | [diff] [blame] | 1780 | s = va_start + (vb->dirty_min << PAGE_SHIFT); |
| 1781 | e = va_start + (vb->dirty_max << PAGE_SHIFT); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1782 | |
Roman Pen | 7d61bfe | 2015-04-15 16:13:55 -0700 | [diff] [blame] | 1783 | start = min(s, start); |
| 1784 | end = max(e, end); |
| 1785 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1786 | flush = 1; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1787 | } |
| 1788 | spin_unlock(&vb->lock); |
| 1789 | } |
| 1790 | rcu_read_unlock(); |
| 1791 | } |
| 1792 | |
Christoph Hellwig | f9e0997 | 2016-12-12 16:44:23 -0800 | [diff] [blame] | 1793 | mutex_lock(&vmap_purge_lock); |
Christoph Hellwig | 0574ecd | 2016-12-12 16:44:07 -0800 | [diff] [blame] | 1794 | purge_fragmented_blocks_allcpus(); |
| 1795 | if (!__purge_vmap_area_lazy(start, end) && flush) |
| 1796 | flush_tlb_kernel_range(start, end); |
Christoph Hellwig | f9e0997 | 2016-12-12 16:44:23 -0800 | [diff] [blame] | 1797 | mutex_unlock(&vmap_purge_lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1798 | } |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 1799 | |
| 1800 | /** |
| 1801 | * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer |
| 1802 | * |
| 1803 | * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily |
| 1804 | * to amortize TLB flushing overheads. What this means is that any page you |
| 1805 | * have now, may, in a former life, have been mapped into kernel virtual |
| 1806 | * address by the vmap layer and so there might be some CPUs with TLB entries |
| 1807 | * still referencing that page (additional to the regular 1:1 kernel mapping). |
| 1808 | * |
| 1809 | * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can |
| 1810 | * be sure that none of the pages we have control over will have any aliases |
| 1811 | * from the vmap layer. |
| 1812 | */ |
| 1813 | void vm_unmap_aliases(void) |
| 1814 | { |
| 1815 | unsigned long start = ULONG_MAX, end = 0; |
| 1816 | int flush = 0; |
| 1817 | |
| 1818 | _vm_unmap_aliases(start, end, flush); |
| 1819 | } |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1820 | EXPORT_SYMBOL_GPL(vm_unmap_aliases); |
| 1821 | |
| 1822 | /** |
| 1823 | * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram |
| 1824 | * @mem: the pointer returned by vm_map_ram |
| 1825 | * @count: the count passed to that vm_map_ram call (cannot unmap partial) |
| 1826 | */ |
| 1827 | void vm_unmap_ram(const void *mem, unsigned int count) |
| 1828 | { |
Guillermo Julián Moreno | 65ee03c | 2016-06-03 14:55:33 -0700 | [diff] [blame] | 1829 | unsigned long size = (unsigned long)count << PAGE_SHIFT; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1830 | unsigned long addr = (unsigned long)mem; |
Christoph Hellwig | 9c3acf6 | 2016-12-12 16:44:04 -0800 | [diff] [blame] | 1831 | struct vmap_area *va; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1832 | |
Christoph Hellwig | 5803ed2 | 2016-12-12 16:44:20 -0800 | [diff] [blame] | 1833 | might_sleep(); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1834 | BUG_ON(!addr); |
| 1835 | BUG_ON(addr < VMALLOC_START); |
| 1836 | BUG_ON(addr > VMALLOC_END); |
Shawn Lin | a1c0b1a | 2016-03-17 14:20:37 -0700 | [diff] [blame] | 1837 | BUG_ON(!PAGE_ALIGNED(addr)); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1838 | |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 1839 | kasan_poison_vmalloc(mem, size); |
| 1840 | |
Christoph Hellwig | 9c3acf6 | 2016-12-12 16:44:04 -0800 | [diff] [blame] | 1841 | if (likely(count <= VMAP_MAX_ALLOC)) { |
Chintan Pandya | 05e3ff9 | 2018-06-07 17:06:53 -0700 | [diff] [blame] | 1842 | debug_check_no_locks_freed(mem, size); |
Christoph Hellwig | 78a0e8c | 2020-06-01 21:51:02 -0700 | [diff] [blame] | 1843 | vb_free(addr, size); |
Christoph Hellwig | 9c3acf6 | 2016-12-12 16:44:04 -0800 | [diff] [blame] | 1844 | return; |
| 1845 | } |
| 1846 | |
| 1847 | va = find_vmap_area(addr); |
| 1848 | BUG_ON(!va); |
Chintan Pandya | 05e3ff9 | 2018-06-07 17:06:53 -0700 | [diff] [blame] | 1849 | debug_check_no_locks_freed((void *)va->va_start, |
| 1850 | (va->va_end - va->va_start)); |
Christoph Hellwig | 9c3acf6 | 2016-12-12 16:44:04 -0800 | [diff] [blame] | 1851 | free_unmap_vmap_area(va); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1852 | } |
| 1853 | EXPORT_SYMBOL(vm_unmap_ram); |
| 1854 | |
| 1855 | /** |
| 1856 | * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) |
| 1857 | * @pages: an array of pointers to the pages to be mapped |
| 1858 | * @count: number of pages |
| 1859 | * @node: prefer to allocate data structures on this node |
Randy Dunlap | e99c97a | 2008-10-29 14:01:09 -0700 | [diff] [blame] | 1860 | * |
Gioh Kim | 3643763 | 2014-04-07 15:37:37 -0700 | [diff] [blame] | 1861 | * If you use this function for less than VMAP_MAX_ALLOC pages, it could be |
| 1862 | * faster than vmap so it's good. But if you mix long-life and short-life |
| 1863 | * objects with vm_map_ram(), it could consume lots of address space through |
| 1864 | * fragmentation (especially on a 32bit machine). You could see failures in |
| 1865 | * the end. Please use this function for short-lived objects. |
| 1866 | * |
Randy Dunlap | e99c97a | 2008-10-29 14:01:09 -0700 | [diff] [blame] | 1867 | * Returns: a pointer to the address that has been mapped, or %NULL on failure |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1868 | */ |
Christoph Hellwig | d4efd79 | 2020-06-01 21:51:27 -0700 | [diff] [blame] | 1869 | void *vm_map_ram(struct page **pages, unsigned int count, int node) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1870 | { |
Guillermo Julián Moreno | 65ee03c | 2016-06-03 14:55:33 -0700 | [diff] [blame] | 1871 | unsigned long size = (unsigned long)count << PAGE_SHIFT; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1872 | unsigned long addr; |
| 1873 | void *mem; |
| 1874 | |
| 1875 | if (likely(count <= VMAP_MAX_ALLOC)) { |
| 1876 | mem = vb_alloc(size, GFP_KERNEL); |
| 1877 | if (IS_ERR(mem)) |
| 1878 | return NULL; |
| 1879 | addr = (unsigned long)mem; |
| 1880 | } else { |
| 1881 | struct vmap_area *va; |
| 1882 | va = alloc_vmap_area(size, PAGE_SIZE, |
| 1883 | VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); |
| 1884 | if (IS_ERR(va)) |
| 1885 | return NULL; |
| 1886 | |
| 1887 | addr = va->va_start; |
| 1888 | mem = (void *)addr; |
| 1889 | } |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 1890 | |
| 1891 | kasan_unpoison_vmalloc(mem, size); |
| 1892 | |
Christoph Hellwig | d4efd79 | 2020-06-01 21:51:27 -0700 | [diff] [blame] | 1893 | if (map_kernel_range(addr, size, PAGE_KERNEL, pages) < 0) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1894 | vm_unmap_ram(mem, count); |
| 1895 | return NULL; |
| 1896 | } |
| 1897 | return mem; |
| 1898 | } |
| 1899 | EXPORT_SYMBOL(vm_map_ram); |
| 1900 | |
Joonsoo Kim | 4341fa4 | 2013-04-29 15:07:39 -0700 | [diff] [blame] | 1901 | static struct vm_struct *vmlist __initdata; |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 1902 | |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1903 | /** |
Nicolas Pitre | be9b733 | 2011-08-25 00:24:21 -0400 | [diff] [blame] | 1904 | * vm_area_add_early - add vmap area early during boot |
| 1905 | * @vm: vm_struct to add |
| 1906 | * |
| 1907 | * This function is used to add fixed kernel vm area to vmlist before |
| 1908 | * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags |
| 1909 | * should contain proper values and the other fields should be zero. |
| 1910 | * |
| 1911 | * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. |
| 1912 | */ |
| 1913 | void __init vm_area_add_early(struct vm_struct *vm) |
| 1914 | { |
| 1915 | struct vm_struct *tmp, **p; |
| 1916 | |
| 1917 | BUG_ON(vmap_initialized); |
| 1918 | for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { |
| 1919 | if (tmp->addr >= vm->addr) { |
| 1920 | BUG_ON(tmp->addr < vm->addr + vm->size); |
| 1921 | break; |
| 1922 | } else |
| 1923 | BUG_ON(tmp->addr + tmp->size > vm->addr); |
| 1924 | } |
| 1925 | vm->next = *p; |
| 1926 | *p = vm; |
| 1927 | } |
| 1928 | |
| 1929 | /** |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1930 | * vm_area_register_early - register vmap area early during boot |
| 1931 | * @vm: vm_struct to register |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1932 | * @align: requested alignment |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1933 | * |
| 1934 | * This function is used to register kernel vm area before |
| 1935 | * vmalloc_init() is called. @vm->size and @vm->flags should contain |
| 1936 | * proper values on entry and other fields should be zero. On return, |
| 1937 | * vm->addr contains the allocated address. |
| 1938 | * |
| 1939 | * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. |
| 1940 | */ |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1941 | void __init vm_area_register_early(struct vm_struct *vm, size_t align) |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1942 | { |
| 1943 | static size_t vm_init_off __initdata; |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1944 | unsigned long addr; |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1945 | |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1946 | addr = ALIGN(VMALLOC_START + vm_init_off, align); |
| 1947 | vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START; |
| 1948 | |
| 1949 | vm->addr = (void *)addr; |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1950 | |
Nicolas Pitre | be9b733 | 2011-08-25 00:24:21 -0400 | [diff] [blame] | 1951 | vm_area_add_early(vm); |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1952 | } |
| 1953 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 1954 | static void vmap_init_free_space(void) |
| 1955 | { |
| 1956 | unsigned long vmap_start = 1; |
| 1957 | const unsigned long vmap_end = ULONG_MAX; |
| 1958 | struct vmap_area *busy, *free; |
| 1959 | |
| 1960 | /* |
| 1961 | * B F B B B F |
| 1962 | * -|-----|.....|-----|-----|-----|.....|- |
| 1963 | * | The KVA space | |
| 1964 | * |<--------------------------------->| |
| 1965 | */ |
| 1966 | list_for_each_entry(busy, &vmap_area_list, list) { |
| 1967 | if (busy->va_start - vmap_start > 0) { |
| 1968 | free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); |
| 1969 | if (!WARN_ON_ONCE(!free)) { |
| 1970 | free->va_start = vmap_start; |
| 1971 | free->va_end = busy->va_start; |
| 1972 | |
| 1973 | insert_vmap_area_augment(free, NULL, |
| 1974 | &free_vmap_area_root, |
| 1975 | &free_vmap_area_list); |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | vmap_start = busy->va_end; |
| 1980 | } |
| 1981 | |
| 1982 | if (vmap_end - vmap_start > 0) { |
| 1983 | free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); |
| 1984 | if (!WARN_ON_ONCE(!free)) { |
| 1985 | free->va_start = vmap_start; |
| 1986 | free->va_end = vmap_end; |
| 1987 | |
| 1988 | insert_vmap_area_augment(free, NULL, |
| 1989 | &free_vmap_area_root, |
| 1990 | &free_vmap_area_list); |
| 1991 | } |
| 1992 | } |
| 1993 | } |
| 1994 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1995 | void __init vmalloc_init(void) |
| 1996 | { |
Ivan Kokshaysky | 822c18f | 2009-01-15 13:50:48 -0800 | [diff] [blame] | 1997 | struct vmap_area *va; |
| 1998 | struct vm_struct *tmp; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1999 | int i; |
| 2000 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 2001 | /* |
| 2002 | * Create the cache for vmap_area objects. |
| 2003 | */ |
| 2004 | vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC); |
| 2005 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2006 | for_each_possible_cpu(i) { |
| 2007 | struct vmap_block_queue *vbq; |
Al Viro | 32fcfd4 | 2013-03-10 20:14:08 -0400 | [diff] [blame] | 2008 | struct vfree_deferred *p; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2009 | |
| 2010 | vbq = &per_cpu(vmap_block_queue, i); |
| 2011 | spin_lock_init(&vbq->lock); |
| 2012 | INIT_LIST_HEAD(&vbq->free); |
Al Viro | 32fcfd4 | 2013-03-10 20:14:08 -0400 | [diff] [blame] | 2013 | p = &per_cpu(vfree_deferred, i); |
| 2014 | init_llist_head(&p->list); |
| 2015 | INIT_WORK(&p->wq, free_work); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2016 | } |
Jeremy Fitzhardinge | 9b46333 | 2008-10-28 19:22:34 +1100 | [diff] [blame] | 2017 | |
Ivan Kokshaysky | 822c18f | 2009-01-15 13:50:48 -0800 | [diff] [blame] | 2018 | /* Import existing vmlist entries. */ |
| 2019 | for (tmp = vmlist; tmp; tmp = tmp->next) { |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 2020 | va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); |
| 2021 | if (WARN_ON_ONCE(!va)) |
| 2022 | continue; |
| 2023 | |
Ivan Kokshaysky | 822c18f | 2009-01-15 13:50:48 -0800 | [diff] [blame] | 2024 | va->va_start = (unsigned long)tmp->addr; |
| 2025 | va->va_end = va->va_start + tmp->size; |
KyongHo | dbda591 | 2012-05-29 15:06:49 -0700 | [diff] [blame] | 2026 | va->vm = tmp; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 2027 | insert_vmap_area(va, &vmap_area_root, &vmap_area_list); |
Ivan Kokshaysky | 822c18f | 2009-01-15 13:50:48 -0800 | [diff] [blame] | 2028 | } |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2029 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 2030 | /* |
| 2031 | * Now we can initialize a free vmap space. |
| 2032 | */ |
| 2033 | vmap_init_free_space(); |
Jeremy Fitzhardinge | 9b46333 | 2008-10-28 19:22:34 +1100 | [diff] [blame] | 2034 | vmap_initialized = true; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2035 | } |
| 2036 | |
Tejun Heo | 8fc4898 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 2037 | /** |
Tejun Heo | 8fc4898 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 2038 | * unmap_kernel_range - unmap kernel VM area and flush cache and TLB |
| 2039 | * @addr: start of the VM area to unmap |
| 2040 | * @size: size of the VM area to unmap |
| 2041 | * |
| 2042 | * Similar to unmap_kernel_range_noflush() but flushes vcache before |
| 2043 | * the unmapping and tlb after. |
| 2044 | */ |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2045 | void unmap_kernel_range(unsigned long addr, unsigned long size) |
| 2046 | { |
| 2047 | unsigned long end = addr + size; |
Tejun Heo | f6fcba7 | 2009-02-20 15:38:48 -0800 | [diff] [blame] | 2048 | |
| 2049 | flush_cache_vunmap(addr, end); |
Christoph Hellwig | b521c43 | 2020-06-01 21:51:07 -0700 | [diff] [blame] | 2050 | unmap_kernel_range_noflush(addr, size); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2051 | flush_tlb_kernel_range(addr, end); |
| 2052 | } |
| 2053 | |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 2054 | static inline void setup_vmalloc_vm_locked(struct vm_struct *vm, |
| 2055 | struct vmap_area *va, unsigned long flags, const void *caller) |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2056 | { |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2057 | vm->flags = flags; |
| 2058 | vm->addr = (void *)va->va_start; |
| 2059 | vm->size = va->va_end - va->va_start; |
| 2060 | vm->caller = caller; |
Minchan Kim | db1aeca | 2012-01-10 15:08:39 -0800 | [diff] [blame] | 2061 | va->vm = vm; |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, |
| 2065 | unsigned long flags, const void *caller) |
| 2066 | { |
| 2067 | spin_lock(&vmap_area_lock); |
| 2068 | setup_vmalloc_vm_locked(vm, va, flags, caller); |
Joonsoo Kim | c69480a | 2013-04-29 15:07:30 -0700 | [diff] [blame] | 2069 | spin_unlock(&vmap_area_lock); |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 2070 | } |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2071 | |
Zhang Yanfei | 20fc02b | 2013-07-08 15:59:58 -0700 | [diff] [blame] | 2072 | static void clear_vm_uninitialized_flag(struct vm_struct *vm) |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 2073 | { |
Joonsoo Kim | d4033af | 2013-04-29 15:07:35 -0700 | [diff] [blame] | 2074 | /* |
Zhang Yanfei | 20fc02b | 2013-07-08 15:59:58 -0700 | [diff] [blame] | 2075 | * Before removing VM_UNINITIALIZED, |
Joonsoo Kim | d4033af | 2013-04-29 15:07:35 -0700 | [diff] [blame] | 2076 | * we should make sure that vm has proper values. |
| 2077 | * Pair with smp_rmb() in show_numa_info(). |
| 2078 | */ |
| 2079 | smp_wmb(); |
Zhang Yanfei | 20fc02b | 2013-07-08 15:59:58 -0700 | [diff] [blame] | 2080 | vm->flags &= ~VM_UNINITIALIZED; |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2081 | } |
| 2082 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2083 | static struct vm_struct *__get_vm_area_node(unsigned long size, |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 2084 | unsigned long align, unsigned long flags, unsigned long start, |
Marek Szyprowski | 5e6cafc | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 2085 | unsigned long end, int node, gfp_t gfp_mask, const void *caller) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2086 | { |
Kautuk Consul | 0006526 | 2011-12-19 17:12:04 -0800 | [diff] [blame] | 2087 | struct vmap_area *va; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2088 | struct vm_struct *area; |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 2089 | unsigned long requested_size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | |
Giridhar Pemmasani | 52fd24c | 2006-10-28 10:38:34 -0700 | [diff] [blame] | 2091 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | size = PAGE_ALIGN(size); |
OGAWA Hirofumi | 31be830 | 2006-11-16 01:19:29 -0800 | [diff] [blame] | 2093 | if (unlikely(!size)) |
| 2094 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | |
zijun_hu | 252e5c6 | 2016-10-07 16:57:26 -0700 | [diff] [blame] | 2096 | if (flags & VM_IOREMAP) |
| 2097 | align = 1ul << clamp_t(int, get_count_order_long(size), |
| 2098 | PAGE_SHIFT, IOREMAP_MAX_ORDER); |
| 2099 | |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2100 | area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | if (unlikely(!area)) |
| 2102 | return NULL; |
| 2103 | |
Andrey Ryabinin | 71394fe | 2015-02-13 14:40:03 -0800 | [diff] [blame] | 2104 | if (!(flags & VM_NO_GUARD)) |
| 2105 | size += PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2107 | va = alloc_vmap_area(size, align, start, end, node, gfp_mask); |
| 2108 | if (IS_ERR(va)) { |
| 2109 | kfree(area); |
| 2110 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 2113 | kasan_unpoison_vmalloc((void *)va->va_start, requested_size); |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 2114 | |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 2115 | setup_vmalloc_vm(area, va, flags, caller); |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 2116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | return area; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | } |
| 2119 | |
Benjamin Herrenschmidt | c296861 | 2009-02-18 14:48:12 -0800 | [diff] [blame] | 2120 | struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, |
| 2121 | unsigned long start, unsigned long end, |
Marek Szyprowski | 5e6cafc | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 2122 | const void *caller) |
Benjamin Herrenschmidt | c296861 | 2009-02-18 14:48:12 -0800 | [diff] [blame] | 2123 | { |
David Rientjes | 00ef2d2 | 2013-02-22 16:35:36 -0800 | [diff] [blame] | 2124 | return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE, |
| 2125 | GFP_KERNEL, caller); |
Benjamin Herrenschmidt | c296861 | 2009-02-18 14:48:12 -0800 | [diff] [blame] | 2126 | } |
| 2127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2129 | * get_vm_area - reserve a contiguous kernel virtual area |
| 2130 | * @size: size of the area |
| 2131 | * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2133 | * Search an area of @size in the kernel virtual mapping area, |
| 2134 | * and reserved it for out purposes. Returns the area descriptor |
| 2135 | * on success or %NULL on failure. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2136 | * |
| 2137 | * Return: the area descriptor on success or %NULL on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | */ |
| 2139 | struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) |
| 2140 | { |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 2141 | return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, |
David Rientjes | 00ef2d2 | 2013-02-22 16:35:36 -0800 | [diff] [blame] | 2142 | NUMA_NO_NODE, GFP_KERNEL, |
| 2143 | __builtin_return_address(0)); |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, |
Marek Szyprowski | 5e6cafc | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 2147 | const void *caller) |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 2148 | { |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 2149 | return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, |
David Rientjes | 00ef2d2 | 2013-02-22 16:35:36 -0800 | [diff] [blame] | 2150 | NUMA_NO_NODE, GFP_KERNEL, caller); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 2153 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2154 | * find_vm_area - find a continuous kernel virtual area |
| 2155 | * @addr: base address |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 2156 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2157 | * Search for the kernel VM area starting at @addr, and return it. |
| 2158 | * It is up to the caller to do all required locking to keep the returned |
| 2159 | * pointer valid. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2160 | * |
Hui Su | 7464061 | 2020-10-13 16:54:51 -0700 | [diff] [blame] | 2161 | * Return: the area descriptor on success or %NULL on failure. |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 2162 | */ |
| 2163 | struct vm_struct *find_vm_area(const void *addr) |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2164 | { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2165 | struct vmap_area *va; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2166 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2167 | va = find_vmap_area((unsigned long)addr); |
Pengfei Li | 688fcbf | 2019-09-23 15:36:39 -0700 | [diff] [blame] | 2168 | if (!va) |
| 2169 | return NULL; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2170 | |
Pengfei Li | 688fcbf | 2019-09-23 15:36:39 -0700 | [diff] [blame] | 2171 | return va->vm; |
Andi Kleen | 7856dfe | 2005-05-20 14:27:57 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2175 | * remove_vm_area - find and remove a continuous kernel virtual area |
| 2176 | * @addr: base address |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2178 | * Search for the kernel VM area starting at @addr, and remove it. |
| 2179 | * This function returns the found VM area, but using it is NOT safe |
| 2180 | * on SMP machines, except for its size or flags. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2181 | * |
Hui Su | 7464061 | 2020-10-13 16:54:51 -0700 | [diff] [blame] | 2182 | * Return: the area descriptor on success or %NULL on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 2184 | struct vm_struct *remove_vm_area(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2186 | struct vmap_area *va; |
| 2187 | |
Christoph Hellwig | 5803ed2 | 2016-12-12 16:44:20 -0800 | [diff] [blame] | 2188 | might_sleep(); |
| 2189 | |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 2190 | spin_lock(&vmap_area_lock); |
| 2191 | va = __find_vmap_area((unsigned long)addr); |
Pengfei Li | 688fcbf | 2019-09-23 15:36:39 -0700 | [diff] [blame] | 2192 | if (va && va->vm) { |
Minchan Kim | db1aeca | 2012-01-10 15:08:39 -0800 | [diff] [blame] | 2193 | struct vm_struct *vm = va->vm; |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 2194 | |
Joonsoo Kim | c69480a | 2013-04-29 15:07:30 -0700 | [diff] [blame] | 2195 | va->vm = NULL; |
Joonsoo Kim | c69480a | 2013-04-29 15:07:30 -0700 | [diff] [blame] | 2196 | spin_unlock(&vmap_area_lock); |
| 2197 | |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 2198 | kasan_free_shadow(vm); |
KAMEZAWA Hiroyuki | dd32c27 | 2009-09-21 17:02:32 -0700 | [diff] [blame] | 2199 | free_unmap_vmap_area(va); |
KAMEZAWA Hiroyuki | dd32c27 | 2009-09-21 17:02:32 -0700 | [diff] [blame] | 2200 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2201 | return vm; |
| 2202 | } |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 2203 | |
| 2204 | spin_unlock(&vmap_area_lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2205 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 2208 | static inline void set_area_direct_map(const struct vm_struct *area, |
| 2209 | int (*set_direct_map)(struct page *page)) |
| 2210 | { |
| 2211 | int i; |
| 2212 | |
| 2213 | for (i = 0; i < area->nr_pages; i++) |
| 2214 | if (page_address(area->pages[i])) |
| 2215 | set_direct_map(area->pages[i]); |
| 2216 | } |
| 2217 | |
| 2218 | /* Handle removing and resetting vm mappings related to the vm_struct. */ |
| 2219 | static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages) |
| 2220 | { |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 2221 | unsigned long start = ULONG_MAX, end = 0; |
| 2222 | int flush_reset = area->flags & VM_FLUSH_RESET_PERMS; |
Rick Edgecombe | 31e6734 | 2019-05-27 14:10:58 -0700 | [diff] [blame] | 2223 | int flush_dmap = 0; |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 2224 | int i; |
| 2225 | |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 2226 | remove_vm_area(area->addr); |
| 2227 | |
| 2228 | /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */ |
| 2229 | if (!flush_reset) |
| 2230 | return; |
| 2231 | |
| 2232 | /* |
| 2233 | * If not deallocating pages, just do the flush of the VM area and |
| 2234 | * return. |
| 2235 | */ |
| 2236 | if (!deallocate_pages) { |
| 2237 | vm_unmap_aliases(); |
| 2238 | return; |
| 2239 | } |
| 2240 | |
| 2241 | /* |
| 2242 | * If execution gets here, flush the vm mapping and reset the direct |
| 2243 | * map. Find the start and end range of the direct mappings to make sure |
| 2244 | * the vm_unmap_aliases() flush includes the direct map. |
| 2245 | */ |
| 2246 | for (i = 0; i < area->nr_pages; i++) { |
Rick Edgecombe | 8e41f87 | 2019-05-27 14:10:57 -0700 | [diff] [blame] | 2247 | unsigned long addr = (unsigned long)page_address(area->pages[i]); |
| 2248 | if (addr) { |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 2249 | start = min(addr, start); |
Rick Edgecombe | 8e41f87 | 2019-05-27 14:10:57 -0700 | [diff] [blame] | 2250 | end = max(addr + PAGE_SIZE, end); |
Rick Edgecombe | 31e6734 | 2019-05-27 14:10:58 -0700 | [diff] [blame] | 2251 | flush_dmap = 1; |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | /* |
| 2256 | * Set direct map to something invalid so that it won't be cached if |
| 2257 | * there are any accesses after the TLB flush, then flush the TLB and |
| 2258 | * reset the direct map permissions to the default. |
| 2259 | */ |
| 2260 | set_area_direct_map(area, set_direct_map_invalid_noflush); |
Rick Edgecombe | 31e6734 | 2019-05-27 14:10:58 -0700 | [diff] [blame] | 2261 | _vm_unmap_aliases(start, end, flush_dmap); |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 2262 | set_area_direct_map(area, set_direct_map_default_noflush); |
| 2263 | } |
| 2264 | |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 2265 | static void __vunmap(const void *addr, int deallocate_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | { |
| 2267 | struct vm_struct *area; |
| 2268 | |
| 2269 | if (!addr) |
| 2270 | return; |
| 2271 | |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 2272 | if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n", |
Dan Carpenter | ab15d9b | 2013-07-08 15:59:53 -0700 | [diff] [blame] | 2273 | addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | |
Liviu Dudau | 6ade203 | 2019-03-05 15:42:54 -0800 | [diff] [blame] | 2276 | area = find_vm_area(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | if (unlikely(!area)) { |
Arjan van de Ven | 4c8573e | 2008-07-25 19:45:37 -0700 | [diff] [blame] | 2278 | WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | return; |
| 2281 | } |
| 2282 | |
Chintan Pandya | 05e3ff9 | 2018-06-07 17:06:53 -0700 | [diff] [blame] | 2283 | debug_check_no_locks_freed(area->addr, get_vm_area_size(area)); |
| 2284 | debug_check_no_obj_freed(area->addr, get_vm_area_size(area)); |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 2285 | |
Vincenzo Frascino | c041098 | 2020-12-14 19:09:06 -0800 | [diff] [blame] | 2286 | kasan_poison_vmalloc(area->addr, get_vm_area_size(area)); |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 2287 | |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 2288 | vm_remove_mappings(area, deallocate_pages); |
| 2289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | if (deallocate_pages) { |
| 2291 | int i; |
| 2292 | |
| 2293 | for (i = 0; i < area->nr_pages; i++) { |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 2294 | struct page *page = area->pages[i]; |
| 2295 | |
| 2296 | BUG_ON(!page); |
Vladimir Davydov | 4949148 | 2016-07-26 15:24:24 -0700 | [diff] [blame] | 2297 | __free_pages(page, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | } |
Roman Gushchin | 97105f0 | 2019-07-11 21:00:13 -0700 | [diff] [blame] | 2299 | atomic_long_sub(area->nr_pages, &nr_vmalloc_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | |
David Rientjes | 244d63e | 2016-01-14 15:19:35 -0800 | [diff] [blame] | 2301 | kvfree(area->pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | kfree(area); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | } |
Andrey Ryabinin | bf22e37 | 2016-12-12 16:44:10 -0800 | [diff] [blame] | 2306 | |
| 2307 | static inline void __vfree_deferred(const void *addr) |
| 2308 | { |
| 2309 | /* |
| 2310 | * Use raw_cpu_ptr() because this can be called from preemptible |
| 2311 | * context. Preemption is absolutely fine here, because the llist_add() |
| 2312 | * implementation is lockless, so it works even if we are adding to |
Jeongtae Park | 73221d8 | 2020-06-04 16:47:19 -0700 | [diff] [blame] | 2313 | * another cpu's list. schedule_work() should be fine with this too. |
Andrey Ryabinin | bf22e37 | 2016-12-12 16:44:10 -0800 | [diff] [blame] | 2314 | */ |
| 2315 | struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred); |
| 2316 | |
| 2317 | if (llist_add((struct llist_node *)addr, &p->list)) |
| 2318 | schedule_work(&p->wq); |
| 2319 | } |
| 2320 | |
| 2321 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2322 | * vfree_atomic - release memory allocated by vmalloc() |
| 2323 | * @addr: memory base address |
Andrey Ryabinin | bf22e37 | 2016-12-12 16:44:10 -0800 | [diff] [blame] | 2324 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2325 | * This one is just like vfree() but can be called in any atomic context |
| 2326 | * except NMIs. |
Andrey Ryabinin | bf22e37 | 2016-12-12 16:44:10 -0800 | [diff] [blame] | 2327 | */ |
| 2328 | void vfree_atomic(const void *addr) |
| 2329 | { |
| 2330 | BUG_ON(in_nmi()); |
| 2331 | |
| 2332 | kmemleak_free(addr); |
| 2333 | |
| 2334 | if (!addr) |
| 2335 | return; |
| 2336 | __vfree_deferred(addr); |
| 2337 | } |
| 2338 | |
Roman Penyaev | c67dc62 | 2019-03-05 15:43:24 -0800 | [diff] [blame] | 2339 | static void __vfree(const void *addr) |
| 2340 | { |
| 2341 | if (unlikely(in_interrupt())) |
| 2342 | __vfree_deferred(addr); |
| 2343 | else |
| 2344 | __vunmap(addr, 1); |
| 2345 | } |
| 2346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | /** |
Matthew Wilcox (Oracle) | fa30747 | 2020-10-17 16:15:03 -0700 | [diff] [blame] | 2348 | * vfree - Release memory allocated by vmalloc() |
| 2349 | * @addr: Memory base address |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | * |
Matthew Wilcox (Oracle) | fa30747 | 2020-10-17 16:15:03 -0700 | [diff] [blame] | 2351 | * Free the virtually continuous memory area starting at @addr, as obtained |
| 2352 | * from one of the vmalloc() family of APIs. This will usually also free the |
| 2353 | * physical memory underlying the virtual allocation, but that memory is |
| 2354 | * reference counted, so it will not be freed until the last user goes away. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | * |
Matthew Wilcox (Oracle) | fa30747 | 2020-10-17 16:15:03 -0700 | [diff] [blame] | 2356 | * If @addr is NULL, no operation is performed. |
Andrew Morton | c9fcee5 | 2013-05-07 16:18:18 -0700 | [diff] [blame] | 2357 | * |
Matthew Wilcox (Oracle) | fa30747 | 2020-10-17 16:15:03 -0700 | [diff] [blame] | 2358 | * Context: |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2359 | * May sleep if called *not* from interrupt context. |
Matthew Wilcox (Oracle) | fa30747 | 2020-10-17 16:15:03 -0700 | [diff] [blame] | 2360 | * Must not be called in NMI context (strictly speaking, it could be |
| 2361 | * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling |
| 2362 | * conventions for vfree() arch-depenedent would be a really bad idea). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 2364 | void vfree(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | { |
Al Viro | 32fcfd4 | 2013-03-10 20:14:08 -0400 | [diff] [blame] | 2366 | BUG_ON(in_nmi()); |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 2367 | |
| 2368 | kmemleak_free(addr); |
| 2369 | |
Andrey Ryabinin | a8dda16 | 2018-10-26 15:07:07 -0700 | [diff] [blame] | 2370 | might_sleep_if(!in_interrupt()); |
| 2371 | |
Al Viro | 32fcfd4 | 2013-03-10 20:14:08 -0400 | [diff] [blame] | 2372 | if (!addr) |
| 2373 | return; |
Roman Penyaev | c67dc62 | 2019-03-05 15:43:24 -0800 | [diff] [blame] | 2374 | |
| 2375 | __vfree(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | EXPORT_SYMBOL(vfree); |
| 2378 | |
| 2379 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2380 | * vunmap - release virtual mapping obtained by vmap() |
| 2381 | * @addr: memory base address |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2383 | * Free the virtually contiguous memory area starting at @addr, |
| 2384 | * which was created from the page array passed to vmap(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2386 | * Must not be called in interrupt context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 2388 | void vunmap(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | { |
| 2390 | BUG_ON(in_interrupt()); |
Peter Zijlstra | 34754b6 | 2009-02-25 16:04:03 +0100 | [diff] [blame] | 2391 | might_sleep(); |
Al Viro | 32fcfd4 | 2013-03-10 20:14:08 -0400 | [diff] [blame] | 2392 | if (addr) |
| 2393 | __vunmap(addr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | EXPORT_SYMBOL(vunmap); |
| 2396 | |
| 2397 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2398 | * vmap - map an array of pages into virtually contiguous space |
| 2399 | * @pages: array of page pointers |
| 2400 | * @count: number of pages to map |
| 2401 | * @flags: vm_area->flags |
| 2402 | * @prot: page protection for the mapping |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | * |
Christoph Hellwig | b944afc | 2020-10-17 16:15:06 -0700 | [diff] [blame] | 2404 | * Maps @count pages from @pages into contiguous kernel virtual space. |
| 2405 | * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself |
| 2406 | * (which must be kmalloc or vmalloc memory) and one reference per pages in it |
| 2407 | * are transferred from the caller to vmap(), and will be freed / dropped when |
| 2408 | * vfree() is called on the return value. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2409 | * |
| 2410 | * Return: the address of the area or %NULL on failure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | */ |
| 2412 | void *vmap(struct page **pages, unsigned int count, |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2413 | unsigned long flags, pgprot_t prot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | { |
| 2415 | struct vm_struct *area; |
Guillermo Julián Moreno | 65ee03c | 2016-06-03 14:55:33 -0700 | [diff] [blame] | 2416 | unsigned long size; /* In bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | |
Peter Zijlstra | 34754b6 | 2009-02-25 16:04:03 +0100 | [diff] [blame] | 2418 | might_sleep(); |
| 2419 | |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 2420 | if (count > totalram_pages()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | return NULL; |
| 2422 | |
Guillermo Julián Moreno | 65ee03c | 2016-06-03 14:55:33 -0700 | [diff] [blame] | 2423 | size = (unsigned long)count << PAGE_SHIFT; |
| 2424 | area = get_vm_area_caller(size, flags, __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | if (!area) |
| 2426 | return NULL; |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 2427 | |
Christoph Hellwig | cca98e9 | 2020-06-01 21:51:32 -0700 | [diff] [blame] | 2428 | if (map_kernel_range((unsigned long)area->addr, size, pgprot_nx(prot), |
Christoph Hellwig | ed1f324 | 2020-06-01 21:51:19 -0700 | [diff] [blame] | 2429 | pages) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | vunmap(area->addr); |
| 2431 | return NULL; |
| 2432 | } |
| 2433 | |
Miaohe Lin | c22ee52 | 2021-01-12 15:49:18 -0800 | [diff] [blame] | 2434 | if (flags & VM_MAP_PUT_PAGES) { |
Christoph Hellwig | b944afc | 2020-10-17 16:15:06 -0700 | [diff] [blame] | 2435 | area->pages = pages; |
Miaohe Lin | c22ee52 | 2021-01-12 15:49:18 -0800 | [diff] [blame] | 2436 | area->nr_pages = count; |
| 2437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | return area->addr; |
| 2439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | EXPORT_SYMBOL(vmap); |
| 2441 | |
Christoph Hellwig | 3e9a9e2 | 2020-10-17 16:15:10 -0700 | [diff] [blame] | 2442 | #ifdef CONFIG_VMAP_PFN |
| 2443 | struct vmap_pfn_data { |
| 2444 | unsigned long *pfns; |
| 2445 | pgprot_t prot; |
| 2446 | unsigned int idx; |
| 2447 | }; |
| 2448 | |
| 2449 | static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private) |
| 2450 | { |
| 2451 | struct vmap_pfn_data *data = private; |
| 2452 | |
| 2453 | if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx]))) |
| 2454 | return -EINVAL; |
| 2455 | *pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot)); |
| 2456 | return 0; |
| 2457 | } |
| 2458 | |
| 2459 | /** |
| 2460 | * vmap_pfn - map an array of PFNs into virtually contiguous space |
| 2461 | * @pfns: array of PFNs |
| 2462 | * @count: number of pages to map |
| 2463 | * @prot: page protection for the mapping |
| 2464 | * |
| 2465 | * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns |
| 2466 | * the start address of the mapping. |
| 2467 | */ |
| 2468 | void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot) |
| 2469 | { |
| 2470 | struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) }; |
| 2471 | struct vm_struct *area; |
| 2472 | |
| 2473 | area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP, |
| 2474 | __builtin_return_address(0)); |
| 2475 | if (!area) |
| 2476 | return NULL; |
| 2477 | if (apply_to_page_range(&init_mm, (unsigned long)area->addr, |
| 2478 | count * PAGE_SIZE, vmap_pfn_apply, &data)) { |
| 2479 | free_vm_area(area); |
| 2480 | return NULL; |
| 2481 | } |
| 2482 | return area->addr; |
| 2483 | } |
| 2484 | EXPORT_SYMBOL_GPL(vmap_pfn); |
| 2485 | #endif /* CONFIG_VMAP_PFN */ |
| 2486 | |
Adrian Bunk | e31d9eb | 2008-02-04 22:29:09 -0800 | [diff] [blame] | 2487 | static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, |
Wanpeng Li | 3722e13 | 2013-11-12 15:07:29 -0800 | [diff] [blame] | 2488 | pgprot_t prot, int node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | { |
David Rientjes | 930f036 | 2014-08-06 16:06:28 -0700 | [diff] [blame] | 2490 | const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; |
Christoph Hellwig | f255935 | 2020-10-17 16:15:43 -0700 | [diff] [blame] | 2491 | unsigned int nr_pages = get_vm_area_size(area) >> PAGE_SHIFT; |
Andrew Morton | 34fe653 | 2020-12-14 19:08:43 -0800 | [diff] [blame] | 2492 | unsigned long array_size; |
| 2493 | unsigned int i; |
Christoph Hellwig | f255935 | 2020-10-17 16:15:43 -0700 | [diff] [blame] | 2494 | struct page **pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | |
Andrew Morton | 34fe653 | 2020-12-14 19:08:43 -0800 | [diff] [blame] | 2496 | array_size = (unsigned long)nr_pages * sizeof(struct page *); |
Christoph Hellwig | f255935 | 2020-10-17 16:15:43 -0700 | [diff] [blame] | 2497 | gfp_mask |= __GFP_NOWARN; |
| 2498 | if (!(gfp_mask & (GFP_DMA | GFP_DMA32))) |
| 2499 | gfp_mask |= __GFP_HIGHMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | /* Please note that the recursion is strictly bounded. */ |
Jan Kiszka | 8757d5f | 2006-07-14 00:23:56 -0700 | [diff] [blame] | 2502 | if (array_size > PAGE_SIZE) { |
Christoph Hellwig | f255935 | 2020-10-17 16:15:43 -0700 | [diff] [blame] | 2503 | pages = __vmalloc_node(array_size, 1, nested_gfp, node, |
| 2504 | area->caller); |
Andrew Morton | 286e1ea | 2006-10-17 00:09:57 -0700 | [diff] [blame] | 2505 | } else { |
Jan Beulich | 976d6df | 2009-12-14 17:58:39 -0800 | [diff] [blame] | 2506 | pages = kmalloc_node(array_size, nested_gfp, node); |
Andrew Morton | 286e1ea | 2006-10-17 00:09:57 -0700 | [diff] [blame] | 2507 | } |
Austin Kim | 7ea36242 | 2019-09-23 15:36:42 -0700 | [diff] [blame] | 2508 | |
| 2509 | if (!pages) { |
Uladzislau Rezki (Sony) | 8945a72 | 2020-12-14 19:08:46 -0800 | [diff] [blame] | 2510 | free_vm_area(area); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | return NULL; |
| 2512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | |
Austin Kim | 7ea36242 | 2019-09-23 15:36:42 -0700 | [diff] [blame] | 2514 | area->pages = pages; |
| 2515 | area->nr_pages = nr_pages; |
| 2516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | for (i = 0; i < area->nr_pages; i++) { |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 2518 | struct page *page; |
| 2519 | |
Jianguo Wu | 4b90951 | 2013-11-12 15:07:11 -0800 | [diff] [blame] | 2520 | if (node == NUMA_NO_NODE) |
Christoph Hellwig | f255935 | 2020-10-17 16:15:43 -0700 | [diff] [blame] | 2521 | page = alloc_page(gfp_mask); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 2522 | else |
Christoph Hellwig | f255935 | 2020-10-17 16:15:43 -0700 | [diff] [blame] | 2523 | page = alloc_pages_node(node, gfp_mask, 0); |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 2524 | |
| 2525 | if (unlikely(!page)) { |
Hui Su | 82afbc3 | 2020-10-13 16:54:48 -0700 | [diff] [blame] | 2526 | /* Successfully allocated i pages, free them in __vfree() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | area->nr_pages = i; |
Roman Gushchin | 97105f0 | 2019-07-11 21:00:13 -0700 | [diff] [blame] | 2528 | atomic_long_add(area->nr_pages, &nr_vmalloc_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | goto fail; |
| 2530 | } |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 2531 | area->pages[i] = page; |
Liu Xiang | dcf61ff | 2019-11-30 17:54:30 -0800 | [diff] [blame] | 2532 | if (gfpflags_allow_blocking(gfp_mask)) |
Eric Dumazet | 660654f | 2014-08-06 16:06:25 -0700 | [diff] [blame] | 2533 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | } |
Roman Gushchin | 97105f0 | 2019-07-11 21:00:13 -0700 | [diff] [blame] | 2535 | atomic_long_add(area->nr_pages, &nr_vmalloc_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | |
Christoph Hellwig | ed1f324 | 2020-06-01 21:51:19 -0700 | [diff] [blame] | 2537 | if (map_kernel_range((unsigned long)area->addr, get_vm_area_size(area), |
| 2538 | prot, pages) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | goto fail; |
Christoph Hellwig | ed1f324 | 2020-06-01 21:51:19 -0700 | [diff] [blame] | 2540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | return area->addr; |
| 2542 | |
| 2543 | fail: |
Michal Hocko | a8e9925 | 2017-02-22 15:46:10 -0800 | [diff] [blame] | 2544 | warn_alloc(gfp_mask, NULL, |
Michal Hocko | 7877cdc | 2016-10-07 17:01:55 -0700 | [diff] [blame] | 2545 | "vmalloc: allocation failure, allocated %ld of %ld bytes", |
Dave Hansen | 22943ab | 2011-05-24 17:12:18 -0700 | [diff] [blame] | 2546 | (area->nr_pages*PAGE_SIZE), area->size); |
Roman Penyaev | c67dc62 | 2019-03-05 15:43:24 -0800 | [diff] [blame] | 2547 | __vfree(area->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | return NULL; |
| 2549 | } |
| 2550 | |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 2551 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2552 | * __vmalloc_node_range - allocate virtually contiguous memory |
| 2553 | * @size: allocation size |
| 2554 | * @align: desired alignment |
| 2555 | * @start: vm area range start |
| 2556 | * @end: vm area range end |
| 2557 | * @gfp_mask: flags for the page level allocator |
| 2558 | * @prot: protection mask for the allocated pages |
| 2559 | * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD) |
| 2560 | * @node: node to use for allocation or NUMA_NO_NODE |
| 2561 | * @caller: caller's return address |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 2562 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2563 | * Allocate enough pages to cover @size from the page level |
| 2564 | * allocator with @gfp_mask flags. Map them into contiguous |
| 2565 | * kernel virtual space, using a pagetable protection of @prot. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2566 | * |
| 2567 | * Return: the address of the area or %NULL on failure |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 2568 | */ |
| 2569 | void *__vmalloc_node_range(unsigned long size, unsigned long align, |
| 2570 | unsigned long start, unsigned long end, gfp_t gfp_mask, |
Andrey Ryabinin | cb9e3c2 | 2015-02-13 14:40:07 -0800 | [diff] [blame] | 2571 | pgprot_t prot, unsigned long vm_flags, int node, |
| 2572 | const void *caller) |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 2573 | { |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 2574 | struct vm_struct *area; |
| 2575 | void *addr; |
| 2576 | unsigned long real_size = size; |
| 2577 | |
| 2578 | size = PAGE_ALIGN(size); |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 2579 | if (!size || (size >> PAGE_SHIFT) > totalram_pages()) |
Joe Perches | de7d2b5 | 2011-10-31 17:08:48 -0700 | [diff] [blame] | 2580 | goto fail; |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 2581 | |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 2582 | area = __get_vm_area_node(real_size, align, VM_ALLOC | VM_UNINITIALIZED | |
Andrey Ryabinin | cb9e3c2 | 2015-02-13 14:40:07 -0800 | [diff] [blame] | 2583 | vm_flags, start, end, node, gfp_mask, caller); |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 2584 | if (!area) |
Joe Perches | de7d2b5 | 2011-10-31 17:08:48 -0700 | [diff] [blame] | 2585 | goto fail; |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 2586 | |
Wanpeng Li | 3722e13 | 2013-11-12 15:07:29 -0800 | [diff] [blame] | 2587 | addr = __vmalloc_area_node(area, gfp_mask, prot, node); |
Mel Gorman | 1368edf | 2011-12-08 14:34:30 -0800 | [diff] [blame] | 2588 | if (!addr) |
Wanpeng Li | b82225f3 | 2013-11-12 15:07:33 -0800 | [diff] [blame] | 2589 | return NULL; |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 2590 | |
| 2591 | /* |
Zhang Yanfei | 20fc02b | 2013-07-08 15:59:58 -0700 | [diff] [blame] | 2592 | * In this function, newly allocated vm_struct has VM_UNINITIALIZED |
| 2593 | * flag. It means that vm_struct is not fully initialized. |
Joonsoo Kim | 4341fa4 | 2013-04-29 15:07:39 -0700 | [diff] [blame] | 2594 | * Now, it is fully initialized, so remove this flag here. |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 2595 | */ |
Zhang Yanfei | 20fc02b | 2013-07-08 15:59:58 -0700 | [diff] [blame] | 2596 | clear_vm_uninitialized_flag(area); |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 2597 | |
Catalin Marinas | 94f4a16 | 2017-07-06 15:40:22 -0700 | [diff] [blame] | 2598 | kmemleak_vmalloc(area, size, gfp_mask); |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 2599 | |
| 2600 | return addr; |
Joe Perches | de7d2b5 | 2011-10-31 17:08:48 -0700 | [diff] [blame] | 2601 | |
| 2602 | fail: |
Michal Hocko | a8e9925 | 2017-02-22 15:46:10 -0800 | [diff] [blame] | 2603 | warn_alloc(gfp_mask, NULL, |
Michal Hocko | 7877cdc | 2016-10-07 17:01:55 -0700 | [diff] [blame] | 2604 | "vmalloc: allocation failure: %lu bytes", real_size); |
Joe Perches | de7d2b5 | 2011-10-31 17:08:48 -0700 | [diff] [blame] | 2605 | return NULL; |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 2606 | } |
| 2607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2609 | * __vmalloc_node - allocate virtually contiguous memory |
| 2610 | * @size: allocation size |
| 2611 | * @align: desired alignment |
| 2612 | * @gfp_mask: flags for the page level allocator |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2613 | * @node: node to use for allocation or NUMA_NO_NODE |
| 2614 | * @caller: caller's return address |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | * |
Christoph Hellwig | f38fcb9 | 2020-06-01 21:51:45 -0700 | [diff] [blame] | 2616 | * Allocate enough pages to cover @size from the page level allocator with |
| 2617 | * @gfp_mask flags. Map them into contiguous kernel virtual space. |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 2618 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2619 | * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL |
| 2620 | * and __GFP_NOFAIL are not supported |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 2621 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2622 | * Any use of gfp flags outside of GFP_KERNEL should be consulted |
| 2623 | * with mm people. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2624 | * |
| 2625 | * Return: pointer to the allocated memory or %NULL on error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | */ |
Christoph Hellwig | 2b90594 | 2020-06-01 21:51:53 -0700 | [diff] [blame] | 2627 | void *__vmalloc_node(unsigned long size, unsigned long align, |
Christoph Hellwig | f38fcb9 | 2020-06-01 21:51:45 -0700 | [diff] [blame] | 2628 | gfp_t gfp_mask, int node, const void *caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | { |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 2630 | return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, |
Christoph Hellwig | f38fcb9 | 2020-06-01 21:51:45 -0700 | [diff] [blame] | 2631 | gfp_mask, PAGE_KERNEL, 0, node, caller); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | } |
Christoph Hellwig | c3f896d | 2020-06-01 21:51:57 -0700 | [diff] [blame] | 2633 | /* |
| 2634 | * This is only for performance analysis of vmalloc and stress purpose. |
| 2635 | * It is required by vmalloc test module, therefore do not use it other |
| 2636 | * than that. |
| 2637 | */ |
| 2638 | #ifdef CONFIG_TEST_VMALLOC_MODULE |
| 2639 | EXPORT_SYMBOL_GPL(__vmalloc_node); |
| 2640 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | |
Christoph Hellwig | 88dca4c | 2020-06-01 21:51:40 -0700 | [diff] [blame] | 2642 | void *__vmalloc(unsigned long size, gfp_t gfp_mask) |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 2643 | { |
Christoph Hellwig | f38fcb9 | 2020-06-01 21:51:45 -0700 | [diff] [blame] | 2644 | return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE, |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 2645 | __builtin_return_address(0)); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 2646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | EXPORT_SYMBOL(__vmalloc); |
| 2648 | |
| 2649 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2650 | * vmalloc - allocate virtually contiguous memory |
| 2651 | * @size: allocation size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2653 | * Allocate enough pages to cover @size from the page level |
| 2654 | * allocator and map them into contiguous kernel virtual space. |
| 2655 | * |
| 2656 | * For tight control over page level allocator and protection flags |
| 2657 | * use __vmalloc() instead. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2658 | * |
| 2659 | * Return: pointer to the allocated memory or %NULL on error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | */ |
| 2661 | void *vmalloc(unsigned long size) |
| 2662 | { |
Christoph Hellwig | 4d39d72 | 2020-06-01 21:51:49 -0700 | [diff] [blame] | 2663 | return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE, |
| 2664 | __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | EXPORT_SYMBOL(vmalloc); |
| 2667 | |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 2668 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2669 | * vzalloc - allocate virtually contiguous memory with zero fill |
| 2670 | * @size: allocation size |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 2671 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2672 | * Allocate enough pages to cover @size from the page level |
| 2673 | * allocator and map them into contiguous kernel virtual space. |
| 2674 | * The memory allocated is set to zero. |
| 2675 | * |
| 2676 | * For tight control over page level allocator and protection flags |
| 2677 | * use __vmalloc() instead. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2678 | * |
| 2679 | * Return: pointer to the allocated memory or %NULL on error |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 2680 | */ |
| 2681 | void *vzalloc(unsigned long size) |
| 2682 | { |
Christoph Hellwig | 4d39d72 | 2020-06-01 21:51:49 -0700 | [diff] [blame] | 2683 | return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE, |
| 2684 | __builtin_return_address(0)); |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 2685 | } |
| 2686 | EXPORT_SYMBOL(vzalloc); |
| 2687 | |
| 2688 | /** |
Rolf Eike Beer | ead0408 | 2006-09-27 01:50:13 -0700 | [diff] [blame] | 2689 | * vmalloc_user - allocate zeroed virtually contiguous memory for userspace |
| 2690 | * @size: allocation size |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2691 | * |
Rolf Eike Beer | ead0408 | 2006-09-27 01:50:13 -0700 | [diff] [blame] | 2692 | * The resulting memory area is zeroed so it can be mapped to userspace |
| 2693 | * without leaking data. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2694 | * |
| 2695 | * Return: pointer to the allocated memory or %NULL on error |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2696 | */ |
| 2697 | void *vmalloc_user(unsigned long size) |
| 2698 | { |
Roman Penyaev | bc84c53 | 2019-03-05 15:43:27 -0800 | [diff] [blame] | 2699 | return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, |
| 2700 | GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL, |
| 2701 | VM_USERMAP, NUMA_NO_NODE, |
| 2702 | __builtin_return_address(0)); |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2703 | } |
| 2704 | EXPORT_SYMBOL(vmalloc_user); |
| 2705 | |
| 2706 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2707 | * vmalloc_node - allocate memory on a specific node |
| 2708 | * @size: allocation size |
| 2709 | * @node: numa node |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 2710 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2711 | * Allocate enough pages to cover @size from the page level |
| 2712 | * allocator and map them into contiguous kernel virtual space. |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 2713 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2714 | * For tight control over page level allocator and protection flags |
| 2715 | * use __vmalloc() instead. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2716 | * |
| 2717 | * Return: pointer to the allocated memory or %NULL on error |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 2718 | */ |
| 2719 | void *vmalloc_node(unsigned long size, int node) |
| 2720 | { |
Christoph Hellwig | f38fcb9 | 2020-06-01 21:51:45 -0700 | [diff] [blame] | 2721 | return __vmalloc_node(size, 1, GFP_KERNEL, node, |
| 2722 | __builtin_return_address(0)); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 2723 | } |
| 2724 | EXPORT_SYMBOL(vmalloc_node); |
| 2725 | |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 2726 | /** |
| 2727 | * vzalloc_node - allocate memory on a specific node with zero fill |
| 2728 | * @size: allocation size |
| 2729 | * @node: numa node |
| 2730 | * |
| 2731 | * Allocate enough pages to cover @size from the page level |
| 2732 | * allocator and map them into contiguous kernel virtual space. |
| 2733 | * The memory allocated is set to zero. |
| 2734 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2735 | * Return: pointer to the allocated memory or %NULL on error |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 2736 | */ |
| 2737 | void *vzalloc_node(unsigned long size, int node) |
| 2738 | { |
Christoph Hellwig | 4d39d72 | 2020-06-01 21:51:49 -0700 | [diff] [blame] | 2739 | return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node, |
| 2740 | __builtin_return_address(0)); |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 2741 | } |
| 2742 | EXPORT_SYMBOL(vzalloc_node); |
| 2743 | |
Andi Kleen | 0d08e0d | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 2744 | #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) |
Michal Hocko | 698d083 | 2018-02-21 14:46:01 -0800 | [diff] [blame] | 2745 | #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) |
Andi Kleen | 0d08e0d | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 2746 | #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) |
Michal Hocko | 698d083 | 2018-02-21 14:46:01 -0800 | [diff] [blame] | 2747 | #define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL) |
Andi Kleen | 0d08e0d | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 2748 | #else |
Michal Hocko | 698d083 | 2018-02-21 14:46:01 -0800 | [diff] [blame] | 2749 | /* |
| 2750 | * 64b systems should always have either DMA or DMA32 zones. For others |
| 2751 | * GFP_DMA32 should do the right thing and use the normal zone. |
| 2752 | */ |
| 2753 | #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL |
Andi Kleen | 0d08e0d | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 2754 | #endif |
| 2755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2757 | * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) |
| 2758 | * @size: allocation size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2760 | * Allocate enough 32bit PA addressable pages to cover @size from the |
| 2761 | * page level allocator and map them into contiguous kernel virtual space. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2762 | * |
| 2763 | * Return: pointer to the allocated memory or %NULL on error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | */ |
| 2765 | void *vmalloc_32(unsigned long size) |
| 2766 | { |
Christoph Hellwig | f38fcb9 | 2020-06-01 21:51:45 -0700 | [diff] [blame] | 2767 | return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE, |
| 2768 | __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | EXPORT_SYMBOL(vmalloc_32); |
| 2771 | |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2772 | /** |
Rolf Eike Beer | ead0408 | 2006-09-27 01:50:13 -0700 | [diff] [blame] | 2773 | * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2774 | * @size: allocation size |
Rolf Eike Beer | ead0408 | 2006-09-27 01:50:13 -0700 | [diff] [blame] | 2775 | * |
| 2776 | * The resulting memory area is 32bit addressable and zeroed so it can be |
| 2777 | * mapped to userspace without leaking data. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2778 | * |
| 2779 | * Return: pointer to the allocated memory or %NULL on error |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2780 | */ |
| 2781 | void *vmalloc_32_user(unsigned long size) |
| 2782 | { |
Roman Penyaev | bc84c53 | 2019-03-05 15:43:27 -0800 | [diff] [blame] | 2783 | return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END, |
| 2784 | GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, |
| 2785 | VM_USERMAP, NUMA_NO_NODE, |
| 2786 | __builtin_return_address(0)); |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2787 | } |
| 2788 | EXPORT_SYMBOL(vmalloc_32_user); |
| 2789 | |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2790 | /* |
| 2791 | * small helper routine , copy contents to buf from addr. |
| 2792 | * If the page is not present, fill zero. |
| 2793 | */ |
| 2794 | |
| 2795 | static int aligned_vread(char *buf, char *addr, unsigned long count) |
| 2796 | { |
| 2797 | struct page *p; |
| 2798 | int copied = 0; |
| 2799 | |
| 2800 | while (count) { |
| 2801 | unsigned long offset, length; |
| 2802 | |
Alexander Kuleshov | 891c49a | 2015-11-05 18:46:51 -0800 | [diff] [blame] | 2803 | offset = offset_in_page(addr); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2804 | length = PAGE_SIZE - offset; |
| 2805 | if (length > count) |
| 2806 | length = count; |
| 2807 | p = vmalloc_to_page(addr); |
| 2808 | /* |
| 2809 | * To do safe access to this _mapped_ area, we need |
| 2810 | * lock. But adding lock here means that we need to add |
| 2811 | * overhead of vmalloc()/vfree() calles for this _debug_ |
| 2812 | * interface, rarely used. Instead of that, we'll use |
| 2813 | * kmap() and get small overhead in this access function. |
| 2814 | */ |
| 2815 | if (p) { |
| 2816 | /* |
| 2817 | * we can expect USER0 is not used (see vread/vwrite's |
| 2818 | * function description) |
| 2819 | */ |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 2820 | void *map = kmap_atomic(p); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2821 | memcpy(buf, map + offset, length); |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 2822 | kunmap_atomic(map); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2823 | } else |
| 2824 | memset(buf, 0, length); |
| 2825 | |
| 2826 | addr += length; |
| 2827 | buf += length; |
| 2828 | copied += length; |
| 2829 | count -= length; |
| 2830 | } |
| 2831 | return copied; |
| 2832 | } |
| 2833 | |
| 2834 | static int aligned_vwrite(char *buf, char *addr, unsigned long count) |
| 2835 | { |
| 2836 | struct page *p; |
| 2837 | int copied = 0; |
| 2838 | |
| 2839 | while (count) { |
| 2840 | unsigned long offset, length; |
| 2841 | |
Alexander Kuleshov | 891c49a | 2015-11-05 18:46:51 -0800 | [diff] [blame] | 2842 | offset = offset_in_page(addr); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2843 | length = PAGE_SIZE - offset; |
| 2844 | if (length > count) |
| 2845 | length = count; |
| 2846 | p = vmalloc_to_page(addr); |
| 2847 | /* |
| 2848 | * To do safe access to this _mapped_ area, we need |
| 2849 | * lock. But adding lock here means that we need to add |
| 2850 | * overhead of vmalloc()/vfree() calles for this _debug_ |
| 2851 | * interface, rarely used. Instead of that, we'll use |
| 2852 | * kmap() and get small overhead in this access function. |
| 2853 | */ |
| 2854 | if (p) { |
| 2855 | /* |
| 2856 | * we can expect USER0 is not used (see vread/vwrite's |
| 2857 | * function description) |
| 2858 | */ |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 2859 | void *map = kmap_atomic(p); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2860 | memcpy(map + offset, buf, length); |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 2861 | kunmap_atomic(map); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2862 | } |
| 2863 | addr += length; |
| 2864 | buf += length; |
| 2865 | copied += length; |
| 2866 | count -= length; |
| 2867 | } |
| 2868 | return copied; |
| 2869 | } |
| 2870 | |
| 2871 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2872 | * vread() - read vmalloc area in a safe way. |
| 2873 | * @buf: buffer for reading data |
| 2874 | * @addr: vm address. |
| 2875 | * @count: number of bytes to be read. |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2876 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2877 | * This function checks that addr is a valid vmalloc'ed area, and |
| 2878 | * copy data from that area to a given buffer. If the given memory range |
| 2879 | * of [addr...addr+count) includes some valid address, data is copied to |
| 2880 | * proper area of @buf. If there are memory holes, they'll be zero-filled. |
| 2881 | * IOREMAP area is treated as memory hole and no copy is done. |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2882 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2883 | * If [addr...addr+count) doesn't includes any intersects with alive |
| 2884 | * vm_struct area, returns 0. @buf should be kernel's buffer. |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2885 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2886 | * Note: In usual ops, vread() is never necessary because the caller |
| 2887 | * should know vmalloc() area is valid and can use memcpy(). |
| 2888 | * This is for routines which have to access vmalloc area without |
Geert Uytterhoeven | d9009d6 | 2019-07-11 20:59:06 -0700 | [diff] [blame] | 2889 | * any information, as /dev/kmem. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2890 | * |
| 2891 | * Return: number of bytes for which addr and buf should be increased |
| 2892 | * (same number as @count) or %0 if [addr...addr+count) doesn't |
| 2893 | * include any intersection with valid vmalloc area |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2894 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | long vread(char *buf, char *addr, unsigned long count) |
| 2896 | { |
Joonsoo Kim | e81ce85 | 2013-04-29 15:07:32 -0700 | [diff] [blame] | 2897 | struct vmap_area *va; |
| 2898 | struct vm_struct *vm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | char *vaddr, *buf_start = buf; |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2900 | unsigned long buflen = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | unsigned long n; |
| 2902 | |
| 2903 | /* Don't allow overflow */ |
| 2904 | if ((unsigned long) addr + count < count) |
| 2905 | count = -(unsigned long) addr; |
| 2906 | |
Joonsoo Kim | e81ce85 | 2013-04-29 15:07:32 -0700 | [diff] [blame] | 2907 | spin_lock(&vmap_area_lock); |
Serapheim Dimitropoulos | f608788 | 2021-04-29 22:58:07 -0700 | [diff] [blame] | 2908 | va = __find_vmap_area((unsigned long)addr); |
| 2909 | if (!va) |
| 2910 | goto finished; |
| 2911 | list_for_each_entry_from(va, &vmap_area_list, list) { |
Joonsoo Kim | e81ce85 | 2013-04-29 15:07:32 -0700 | [diff] [blame] | 2912 | if (!count) |
| 2913 | break; |
| 2914 | |
Pengfei Li | 688fcbf | 2019-09-23 15:36:39 -0700 | [diff] [blame] | 2915 | if (!va->vm) |
Joonsoo Kim | e81ce85 | 2013-04-29 15:07:32 -0700 | [diff] [blame] | 2916 | continue; |
| 2917 | |
| 2918 | vm = va->vm; |
| 2919 | vaddr = (char *) vm->addr; |
Wanpeng Li | 762216a | 2013-09-11 14:22:42 -0700 | [diff] [blame] | 2920 | if (addr >= vaddr + get_vm_area_size(vm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2921 | continue; |
| 2922 | while (addr < vaddr) { |
| 2923 | if (count == 0) |
| 2924 | goto finished; |
| 2925 | *buf = '\0'; |
| 2926 | buf++; |
| 2927 | addr++; |
| 2928 | count--; |
| 2929 | } |
Wanpeng Li | 762216a | 2013-09-11 14:22:42 -0700 | [diff] [blame] | 2930 | n = vaddr + get_vm_area_size(vm) - addr; |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2931 | if (n > count) |
| 2932 | n = count; |
Joonsoo Kim | e81ce85 | 2013-04-29 15:07:32 -0700 | [diff] [blame] | 2933 | if (!(vm->flags & VM_IOREMAP)) |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2934 | aligned_vread(buf, addr, n); |
| 2935 | else /* IOREMAP area is treated as memory hole */ |
| 2936 | memset(buf, 0, n); |
| 2937 | buf += n; |
| 2938 | addr += n; |
| 2939 | count -= n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | } |
| 2941 | finished: |
Joonsoo Kim | e81ce85 | 2013-04-29 15:07:32 -0700 | [diff] [blame] | 2942 | spin_unlock(&vmap_area_lock); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2943 | |
| 2944 | if (buf == buf_start) |
| 2945 | return 0; |
| 2946 | /* zero-fill memory holes */ |
| 2947 | if (buf != buf_start + buflen) |
| 2948 | memset(buf, 0, buflen - (buf - buf_start)); |
| 2949 | |
| 2950 | return buflen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | } |
| 2952 | |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2953 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2954 | * vwrite() - write vmalloc area in a safe way. |
| 2955 | * @buf: buffer for source data |
| 2956 | * @addr: vm address. |
| 2957 | * @count: number of bytes to be read. |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2958 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2959 | * This function checks that addr is a valid vmalloc'ed area, and |
| 2960 | * copy data from a buffer to the given addr. If specified range of |
| 2961 | * [addr...addr+count) includes some valid address, data is copied from |
| 2962 | * proper area of @buf. If there are memory holes, no copy to hole. |
| 2963 | * IOREMAP area is treated as memory hole and no copy is done. |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2964 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2965 | * If [addr...addr+count) doesn't includes any intersects with alive |
| 2966 | * vm_struct area, returns 0. @buf should be kernel's buffer. |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2967 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 2968 | * Note: In usual ops, vwrite() is never necessary because the caller |
| 2969 | * should know vmalloc() area is valid and can use memcpy(). |
| 2970 | * This is for routines which have to access vmalloc area without |
Geert Uytterhoeven | d9009d6 | 2019-07-11 20:59:06 -0700 | [diff] [blame] | 2971 | * any information, as /dev/kmem. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2972 | * |
| 2973 | * Return: number of bytes for which addr and buf should be |
| 2974 | * increased (same number as @count) or %0 if [addr...addr+count) |
| 2975 | * doesn't include any intersection with valid vmalloc area |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2976 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | long vwrite(char *buf, char *addr, unsigned long count) |
| 2978 | { |
Joonsoo Kim | e81ce85 | 2013-04-29 15:07:32 -0700 | [diff] [blame] | 2979 | struct vmap_area *va; |
| 2980 | struct vm_struct *vm; |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2981 | char *vaddr; |
| 2982 | unsigned long n, buflen; |
| 2983 | int copied = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | |
| 2985 | /* Don't allow overflow */ |
| 2986 | if ((unsigned long) addr + count < count) |
| 2987 | count = -(unsigned long) addr; |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2988 | buflen = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | |
Joonsoo Kim | e81ce85 | 2013-04-29 15:07:32 -0700 | [diff] [blame] | 2990 | spin_lock(&vmap_area_lock); |
| 2991 | list_for_each_entry(va, &vmap_area_list, list) { |
| 2992 | if (!count) |
| 2993 | break; |
| 2994 | |
Pengfei Li | 688fcbf | 2019-09-23 15:36:39 -0700 | [diff] [blame] | 2995 | if (!va->vm) |
Joonsoo Kim | e81ce85 | 2013-04-29 15:07:32 -0700 | [diff] [blame] | 2996 | continue; |
| 2997 | |
| 2998 | vm = va->vm; |
| 2999 | vaddr = (char *) vm->addr; |
Wanpeng Li | 762216a | 2013-09-11 14:22:42 -0700 | [diff] [blame] | 3000 | if (addr >= vaddr + get_vm_area_size(vm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | continue; |
| 3002 | while (addr < vaddr) { |
| 3003 | if (count == 0) |
| 3004 | goto finished; |
| 3005 | buf++; |
| 3006 | addr++; |
| 3007 | count--; |
| 3008 | } |
Wanpeng Li | 762216a | 2013-09-11 14:22:42 -0700 | [diff] [blame] | 3009 | n = vaddr + get_vm_area_size(vm) - addr; |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 3010 | if (n > count) |
| 3011 | n = count; |
Joonsoo Kim | e81ce85 | 2013-04-29 15:07:32 -0700 | [diff] [blame] | 3012 | if (!(vm->flags & VM_IOREMAP)) { |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 3013 | aligned_vwrite(buf, addr, n); |
| 3014 | copied++; |
| 3015 | } |
| 3016 | buf += n; |
| 3017 | addr += n; |
| 3018 | count -= n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | } |
| 3020 | finished: |
Joonsoo Kim | e81ce85 | 2013-04-29 15:07:32 -0700 | [diff] [blame] | 3021 | spin_unlock(&vmap_area_lock); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 3022 | if (!copied) |
| 3023 | return 0; |
| 3024 | return buflen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | } |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 3026 | |
| 3027 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 3028 | * remap_vmalloc_range_partial - map vmalloc pages to userspace |
| 3029 | * @vma: vma to cover |
| 3030 | * @uaddr: target user address to start at |
| 3031 | * @kaddr: virtual address of vmalloc kernel memory |
Jann Horn | bdebd6a2 | 2020-04-20 18:14:11 -0700 | [diff] [blame] | 3032 | * @pgoff: offset from @kaddr to start at |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 3033 | * @size: size of map area |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 3034 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 3035 | * Returns: 0 for success, -Exxx on failure |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 3036 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 3037 | * This function checks that @kaddr is a valid vmalloc'ed area, |
| 3038 | * and that it is big enough to cover the range starting at |
| 3039 | * @uaddr in @vma. Will return failure if that criteria isn't |
| 3040 | * met. |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 3041 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 3042 | * Similar to remap_pfn_range() (see mm/memory.c) |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 3043 | */ |
| 3044 | int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr, |
Jann Horn | bdebd6a2 | 2020-04-20 18:14:11 -0700 | [diff] [blame] | 3045 | void *kaddr, unsigned long pgoff, |
| 3046 | unsigned long size) |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 3047 | { |
| 3048 | struct vm_struct *area; |
Jann Horn | bdebd6a2 | 2020-04-20 18:14:11 -0700 | [diff] [blame] | 3049 | unsigned long off; |
| 3050 | unsigned long end_index; |
| 3051 | |
| 3052 | if (check_shl_overflow(pgoff, PAGE_SHIFT, &off)) |
| 3053 | return -EINVAL; |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 3054 | |
| 3055 | size = PAGE_ALIGN(size); |
| 3056 | |
| 3057 | if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr)) |
| 3058 | return -EINVAL; |
| 3059 | |
| 3060 | area = find_vm_area(kaddr); |
| 3061 | if (!area) |
| 3062 | return -EINVAL; |
| 3063 | |
Christoph Hellwig | fe9041c | 2019-06-03 08:55:13 +0200 | [diff] [blame] | 3064 | if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT))) |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 3065 | return -EINVAL; |
| 3066 | |
Jann Horn | bdebd6a2 | 2020-04-20 18:14:11 -0700 | [diff] [blame] | 3067 | if (check_add_overflow(size, off, &end_index) || |
| 3068 | end_index > get_vm_area_size(area)) |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 3069 | return -EINVAL; |
Jann Horn | bdebd6a2 | 2020-04-20 18:14:11 -0700 | [diff] [blame] | 3070 | kaddr += off; |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 3071 | |
| 3072 | do { |
| 3073 | struct page *page = vmalloc_to_page(kaddr); |
| 3074 | int ret; |
| 3075 | |
| 3076 | ret = vm_insert_page(vma, uaddr, page); |
| 3077 | if (ret) |
| 3078 | return ret; |
| 3079 | |
| 3080 | uaddr += PAGE_SIZE; |
| 3081 | kaddr += PAGE_SIZE; |
| 3082 | size -= PAGE_SIZE; |
| 3083 | } while (size > 0); |
| 3084 | |
| 3085 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
| 3086 | |
| 3087 | return 0; |
| 3088 | } |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 3089 | |
| 3090 | /** |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 3091 | * remap_vmalloc_range - map vmalloc pages to userspace |
| 3092 | * @vma: vma to cover (map full range of vma) |
| 3093 | * @addr: vmalloc memory |
| 3094 | * @pgoff: number of pages into addr before first page to map |
Randy Dunlap | 7682486 | 2008-03-19 17:00:40 -0700 | [diff] [blame] | 3095 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 3096 | * Returns: 0 for success, -Exxx on failure |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 3097 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 3098 | * This function checks that addr is a valid vmalloc'ed area, and |
| 3099 | * that it is big enough to cover the vma. Will return failure if |
| 3100 | * that criteria isn't met. |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 3101 | * |
Mike Rapoport | 92eac16 | 2019-03-05 15:48:36 -0800 | [diff] [blame] | 3102 | * Similar to remap_pfn_range() (see mm/memory.c) |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 3103 | */ |
| 3104 | int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, |
| 3105 | unsigned long pgoff) |
| 3106 | { |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 3107 | return remap_vmalloc_range_partial(vma, vma->vm_start, |
Jann Horn | bdebd6a2 | 2020-04-20 18:14:11 -0700 | [diff] [blame] | 3108 | addr, pgoff, |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 3109 | vma->vm_end - vma->vm_start); |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 3110 | } |
| 3111 | EXPORT_SYMBOL(remap_vmalloc_range); |
| 3112 | |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 3113 | void free_vm_area(struct vm_struct *area) |
| 3114 | { |
| 3115 | struct vm_struct *ret; |
| 3116 | ret = remove_vm_area(area->addr); |
| 3117 | BUG_ON(ret != area); |
| 3118 | kfree(area); |
| 3119 | } |
| 3120 | EXPORT_SYMBOL_GPL(free_vm_area); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3121 | |
Tejun Heo | 4f8b02b | 2010-09-03 18:22:47 +0200 | [diff] [blame] | 3122 | #ifdef CONFIG_SMP |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3123 | static struct vmap_area *node_to_va(struct rb_node *n) |
| 3124 | { |
Geliang Tang | 4583e77 | 2017-02-22 15:41:54 -0800 | [diff] [blame] | 3125 | return rb_entry_safe(n, struct vmap_area, rb_node); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | /** |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3129 | * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to |
| 3130 | * @addr: target address |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3131 | * |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3132 | * Returns: vmap_area if it is found. If there is no such area |
| 3133 | * the first highest(reverse order) vmap_area is returned |
| 3134 | * i.e. va->va_start < addr && va->va_end < addr or NULL |
| 3135 | * if there are no any areas before @addr. |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3136 | */ |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3137 | static struct vmap_area * |
| 3138 | pvm_find_va_enclose_addr(unsigned long addr) |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3139 | { |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3140 | struct vmap_area *va, *tmp; |
| 3141 | struct rb_node *n; |
| 3142 | |
| 3143 | n = free_vmap_area_root.rb_node; |
| 3144 | va = NULL; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3145 | |
| 3146 | while (n) { |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3147 | tmp = rb_entry(n, struct vmap_area, rb_node); |
| 3148 | if (tmp->va_start <= addr) { |
| 3149 | va = tmp; |
| 3150 | if (tmp->va_end >= addr) |
| 3151 | break; |
| 3152 | |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3153 | n = n->rb_right; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3154 | } else { |
| 3155 | n = n->rb_left; |
| 3156 | } |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3157 | } |
| 3158 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3159 | return va; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | /** |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3163 | * pvm_determine_end_from_reverse - find the highest aligned address |
| 3164 | * of free block below VMALLOC_END |
| 3165 | * @va: |
| 3166 | * in - the VA we start the search(reverse order); |
| 3167 | * out - the VA with the highest aligned end address. |
Alex Shi | 799fa85 | 2020-12-14 19:08:53 -0800 | [diff] [blame] | 3168 | * @align: alignment for required highest address |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3169 | * |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3170 | * Returns: determined end address within vmap_area |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3171 | */ |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3172 | static unsigned long |
| 3173 | pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align) |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3174 | { |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3175 | unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3176 | unsigned long addr; |
| 3177 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3178 | if (likely(*va)) { |
| 3179 | list_for_each_entry_from_reverse((*va), |
| 3180 | &free_vmap_area_list, list) { |
| 3181 | addr = min((*va)->va_end & ~(align - 1), vmalloc_end); |
| 3182 | if ((*va)->va_start < addr) |
| 3183 | return addr; |
| 3184 | } |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3185 | } |
| 3186 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3187 | return 0; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | /** |
| 3191 | * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator |
| 3192 | * @offsets: array containing offset of each area |
| 3193 | * @sizes: array containing size of each area |
| 3194 | * @nr_vms: the number of areas to allocate |
| 3195 | * @align: alignment, all entries in @offsets and @sizes must be aligned to this |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3196 | * |
| 3197 | * Returns: kmalloc'd vm_struct pointer array pointing to allocated |
| 3198 | * vm_structs on success, %NULL on failure |
| 3199 | * |
| 3200 | * Percpu allocator wants to use congruent vm areas so that it can |
| 3201 | * maintain the offsets among percpu areas. This function allocates |
David Rientjes | ec3f64f | 2011-01-13 15:46:01 -0800 | [diff] [blame] | 3202 | * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to |
| 3203 | * be scattered pretty far, distance between two areas easily going up |
| 3204 | * to gigabytes. To avoid interacting with regular vmallocs, these |
| 3205 | * areas are allocated from top. |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3206 | * |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3207 | * Despite its complicated look, this allocator is rather simple. It |
| 3208 | * does everything top-down and scans free blocks from the end looking |
| 3209 | * for matching base. While scanning, if any of the areas do not fit the |
| 3210 | * base address is pulled down to fit the area. Scanning is repeated till |
| 3211 | * all the areas fit and then all necessary data structures are inserted |
| 3212 | * and the result is returned. |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3213 | */ |
| 3214 | struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, |
| 3215 | const size_t *sizes, int nr_vms, |
David Rientjes | ec3f64f | 2011-01-13 15:46:01 -0800 | [diff] [blame] | 3216 | size_t align) |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3217 | { |
| 3218 | const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); |
| 3219 | const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3220 | struct vmap_area **vas, *va; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3221 | struct vm_struct **vms; |
| 3222 | int area, area2, last_area, term_area; |
Daniel Axtens | 253a496 | 2019-12-17 20:51:49 -0800 | [diff] [blame] | 3223 | unsigned long base, start, size, end, last_end, orig_start, orig_end; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3224 | bool purged = false; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3225 | enum fit_type type; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3226 | |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3227 | /* verify parameters and allocate data structures */ |
Alexander Kuleshov | 891c49a | 2015-11-05 18:46:51 -0800 | [diff] [blame] | 3228 | BUG_ON(offset_in_page(align) || !is_power_of_2(align)); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3229 | for (last_area = 0, area = 0; area < nr_vms; area++) { |
| 3230 | start = offsets[area]; |
| 3231 | end = start + sizes[area]; |
| 3232 | |
| 3233 | /* is everything aligned properly? */ |
| 3234 | BUG_ON(!IS_ALIGNED(offsets[area], align)); |
| 3235 | BUG_ON(!IS_ALIGNED(sizes[area], align)); |
| 3236 | |
| 3237 | /* detect the area with the highest address */ |
| 3238 | if (start > offsets[last_area]) |
| 3239 | last_area = area; |
| 3240 | |
Wei Yang | c568da2 | 2017-09-06 16:24:09 -0700 | [diff] [blame] | 3241 | for (area2 = area + 1; area2 < nr_vms; area2++) { |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3242 | unsigned long start2 = offsets[area2]; |
| 3243 | unsigned long end2 = start2 + sizes[area2]; |
| 3244 | |
Wei Yang | c568da2 | 2017-09-06 16:24:09 -0700 | [diff] [blame] | 3245 | BUG_ON(start2 < end && start < end2); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3246 | } |
| 3247 | } |
| 3248 | last_end = offsets[last_area] + sizes[last_area]; |
| 3249 | |
| 3250 | if (vmalloc_end - vmalloc_start < last_end) { |
| 3251 | WARN_ON(true); |
| 3252 | return NULL; |
| 3253 | } |
| 3254 | |
Thomas Meyer | 4d67d86 | 2012-05-29 15:06:21 -0700 | [diff] [blame] | 3255 | vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL); |
| 3256 | vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3257 | if (!vas || !vms) |
Kautuk Consul | f1db7af | 2012-01-12 17:20:08 -0800 | [diff] [blame] | 3258 | goto err_free2; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3259 | |
| 3260 | for (area = 0; area < nr_vms; area++) { |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3261 | vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL); |
David Rientjes | ec3f64f | 2011-01-13 15:46:01 -0800 | [diff] [blame] | 3262 | vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3263 | if (!vas[area] || !vms[area]) |
| 3264 | goto err_free; |
| 3265 | } |
| 3266 | retry: |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 3267 | spin_lock(&free_vmap_area_lock); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3268 | |
| 3269 | /* start scanning - we scan from the top, begin with the last area */ |
| 3270 | area = term_area = last_area; |
| 3271 | start = offsets[area]; |
| 3272 | end = start + sizes[area]; |
| 3273 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3274 | va = pvm_find_va_enclose_addr(vmalloc_end); |
| 3275 | base = pvm_determine_end_from_reverse(&va, align) - end; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3276 | |
| 3277 | while (true) { |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3278 | /* |
| 3279 | * base might have underflowed, add last_end before |
| 3280 | * comparing. |
| 3281 | */ |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3282 | if (base + last_end < vmalloc_start + last_end) |
| 3283 | goto overflow; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3284 | |
| 3285 | /* |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3286 | * Fitting base has not been found. |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3287 | */ |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3288 | if (va == NULL) |
| 3289 | goto overflow; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3290 | |
| 3291 | /* |
Qiujun Huang | d8cc323 | 2020-04-06 20:04:02 -0700 | [diff] [blame] | 3292 | * If required width exceeds current VA block, move |
Kuppuswamy Sathyanarayanan | 5336e52 | 2019-08-13 15:37:31 -0700 | [diff] [blame] | 3293 | * base downwards and then recheck. |
| 3294 | */ |
| 3295 | if (base + end > va->va_end) { |
| 3296 | base = pvm_determine_end_from_reverse(&va, align) - end; |
| 3297 | term_area = area; |
| 3298 | continue; |
| 3299 | } |
| 3300 | |
| 3301 | /* |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3302 | * If this VA does not fit, move base downwards and recheck. |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3303 | */ |
Kuppuswamy Sathyanarayanan | 5336e52 | 2019-08-13 15:37:31 -0700 | [diff] [blame] | 3304 | if (base + start < va->va_start) { |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3305 | va = node_to_va(rb_prev(&va->rb_node)); |
| 3306 | base = pvm_determine_end_from_reverse(&va, align) - end; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3307 | term_area = area; |
| 3308 | continue; |
| 3309 | } |
| 3310 | |
| 3311 | /* |
| 3312 | * This area fits, move on to the previous one. If |
| 3313 | * the previous one is the terminal one, we're done. |
| 3314 | */ |
| 3315 | area = (area + nr_vms - 1) % nr_vms; |
| 3316 | if (area == term_area) |
| 3317 | break; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3318 | |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3319 | start = offsets[area]; |
| 3320 | end = start + sizes[area]; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3321 | va = pvm_find_va_enclose_addr(base + end); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3322 | } |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3323 | |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3324 | /* we've found a fitting base, insert all va's */ |
| 3325 | for (area = 0; area < nr_vms; area++) { |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3326 | int ret; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3327 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3328 | start = base + offsets[area]; |
| 3329 | size = sizes[area]; |
| 3330 | |
| 3331 | va = pvm_find_va_enclose_addr(start); |
| 3332 | if (WARN_ON_ONCE(va == NULL)) |
| 3333 | /* It is a BUG(), but trigger recovery instead. */ |
| 3334 | goto recovery; |
| 3335 | |
| 3336 | type = classify_va_fit_type(va, start, size); |
| 3337 | if (WARN_ON_ONCE(type == NOTHING_FIT)) |
| 3338 | /* It is a BUG(), but trigger recovery instead. */ |
| 3339 | goto recovery; |
| 3340 | |
| 3341 | ret = adjust_va_to_fit_type(va, start, size, type); |
| 3342 | if (unlikely(ret)) |
| 3343 | goto recovery; |
| 3344 | |
| 3345 | /* Allocated area. */ |
| 3346 | va = vas[area]; |
| 3347 | va->va_start = start; |
| 3348 | va->va_end = start + size; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3349 | } |
| 3350 | |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 3351 | spin_unlock(&free_vmap_area_lock); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3352 | |
Daniel Axtens | 253a496 | 2019-12-17 20:51:49 -0800 | [diff] [blame] | 3353 | /* populate the kasan shadow space */ |
| 3354 | for (area = 0; area < nr_vms; area++) { |
| 3355 | if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area])) |
| 3356 | goto err_free_shadow; |
| 3357 | |
| 3358 | kasan_unpoison_vmalloc((void *)vas[area]->va_start, |
| 3359 | sizes[area]); |
| 3360 | } |
| 3361 | |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3362 | /* insert all vm's */ |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 3363 | spin_lock(&vmap_area_lock); |
| 3364 | for (area = 0; area < nr_vms; area++) { |
| 3365 | insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list); |
| 3366 | |
| 3367 | setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC, |
Zhang Yanfei | 3645cb4 | 2013-07-03 15:04:48 -0700 | [diff] [blame] | 3368 | pcpu_get_vm_areas); |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 3369 | } |
| 3370 | spin_unlock(&vmap_area_lock); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3371 | |
| 3372 | kfree(vas); |
| 3373 | return vms; |
| 3374 | |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3375 | recovery: |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 3376 | /* |
| 3377 | * Remove previously allocated areas. There is no |
| 3378 | * need in removing these areas from the busy tree, |
| 3379 | * because they are inserted only on the final step |
| 3380 | * and when pcpu_get_vm_areas() is success. |
| 3381 | */ |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3382 | while (area--) { |
Daniel Axtens | 253a496 | 2019-12-17 20:51:49 -0800 | [diff] [blame] | 3383 | orig_start = vas[area]->va_start; |
| 3384 | orig_end = vas[area]->va_end; |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 3385 | va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, |
| 3386 | &free_vmap_area_list); |
Uladzislau Rezki (Sony) | 9c801f6 | 2020-08-06 23:24:24 -0700 | [diff] [blame] | 3387 | if (va) |
| 3388 | kasan_release_vmalloc(orig_start, orig_end, |
| 3389 | va->va_start, va->va_end); |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3390 | vas[area] = NULL; |
| 3391 | } |
| 3392 | |
| 3393 | overflow: |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 3394 | spin_unlock(&free_vmap_area_lock); |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3395 | if (!purged) { |
| 3396 | purge_vmap_area_lazy(); |
| 3397 | purged = true; |
| 3398 | |
| 3399 | /* Before "retry", check if we recover. */ |
| 3400 | for (area = 0; area < nr_vms; area++) { |
| 3401 | if (vas[area]) |
| 3402 | continue; |
| 3403 | |
| 3404 | vas[area] = kmem_cache_zalloc( |
| 3405 | vmap_area_cachep, GFP_KERNEL); |
| 3406 | if (!vas[area]) |
| 3407 | goto err_free; |
| 3408 | } |
| 3409 | |
| 3410 | goto retry; |
| 3411 | } |
| 3412 | |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3413 | err_free: |
| 3414 | for (area = 0; area < nr_vms; area++) { |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 3415 | if (vas[area]) |
| 3416 | kmem_cache_free(vmap_area_cachep, vas[area]); |
| 3417 | |
Kautuk Consul | f1db7af | 2012-01-12 17:20:08 -0800 | [diff] [blame] | 3418 | kfree(vms[area]); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3419 | } |
Kautuk Consul | f1db7af | 2012-01-12 17:20:08 -0800 | [diff] [blame] | 3420 | err_free2: |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3421 | kfree(vas); |
| 3422 | kfree(vms); |
| 3423 | return NULL; |
Daniel Axtens | 253a496 | 2019-12-17 20:51:49 -0800 | [diff] [blame] | 3424 | |
| 3425 | err_free_shadow: |
| 3426 | spin_lock(&free_vmap_area_lock); |
| 3427 | /* |
| 3428 | * We release all the vmalloc shadows, even the ones for regions that |
| 3429 | * hadn't been successfully added. This relies on kasan_release_vmalloc |
| 3430 | * being able to tolerate this case. |
| 3431 | */ |
| 3432 | for (area = 0; area < nr_vms; area++) { |
| 3433 | orig_start = vas[area]->va_start; |
| 3434 | orig_end = vas[area]->va_end; |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 3435 | va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, |
| 3436 | &free_vmap_area_list); |
Uladzislau Rezki (Sony) | 9c801f6 | 2020-08-06 23:24:24 -0700 | [diff] [blame] | 3437 | if (va) |
| 3438 | kasan_release_vmalloc(orig_start, orig_end, |
| 3439 | va->va_start, va->va_end); |
Daniel Axtens | 253a496 | 2019-12-17 20:51:49 -0800 | [diff] [blame] | 3440 | vas[area] = NULL; |
| 3441 | kfree(vms[area]); |
| 3442 | } |
| 3443 | spin_unlock(&free_vmap_area_lock); |
| 3444 | kfree(vas); |
| 3445 | kfree(vms); |
| 3446 | return NULL; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | /** |
| 3450 | * pcpu_free_vm_areas - free vmalloc areas for percpu allocator |
| 3451 | * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() |
| 3452 | * @nr_vms: the number of allocated areas |
| 3453 | * |
| 3454 | * Free vm_structs and the array allocated by pcpu_get_vm_areas(). |
| 3455 | */ |
| 3456 | void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) |
| 3457 | { |
| 3458 | int i; |
| 3459 | |
| 3460 | for (i = 0; i < nr_vms; i++) |
| 3461 | free_vm_area(vms[i]); |
| 3462 | kfree(vms); |
| 3463 | } |
Tejun Heo | 4f8b02b | 2010-09-03 18:22:47 +0200 | [diff] [blame] | 3464 | #endif /* CONFIG_SMP */ |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3465 | |
Paul E. McKenney | 5bb1bb3 | 2021-01-07 13:46:11 -0800 | [diff] [blame] | 3466 | #ifdef CONFIG_PRINTK |
Paul E. McKenney | 98f1808 | 2020-12-08 16:13:57 -0800 | [diff] [blame] | 3467 | bool vmalloc_dump_obj(void *object) |
| 3468 | { |
| 3469 | struct vm_struct *vm; |
| 3470 | void *objp = (void *)PAGE_ALIGN((unsigned long)object); |
| 3471 | |
| 3472 | vm = find_vm_area(objp); |
| 3473 | if (!vm) |
| 3474 | return false; |
Paul E. McKenney | bd34dcd | 2020-12-09 15:15:27 -0800 | [diff] [blame] | 3475 | pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n", |
| 3476 | vm->nr_pages, (unsigned long)vm->addr, vm->caller); |
Paul E. McKenney | 98f1808 | 2020-12-08 16:13:57 -0800 | [diff] [blame] | 3477 | return true; |
| 3478 | } |
Paul E. McKenney | 5bb1bb3 | 2021-01-07 13:46:11 -0800 | [diff] [blame] | 3479 | #endif |
Paul E. McKenney | 98f1808 | 2020-12-08 16:13:57 -0800 | [diff] [blame] | 3480 | |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3481 | #ifdef CONFIG_PROC_FS |
| 3482 | static void *s_start(struct seq_file *m, loff_t *pos) |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 3483 | __acquires(&vmap_purge_lock) |
Joonsoo Kim | d4033af | 2013-04-29 15:07:35 -0700 | [diff] [blame] | 3484 | __acquires(&vmap_area_lock) |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3485 | { |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 3486 | mutex_lock(&vmap_purge_lock); |
Joonsoo Kim | d4033af | 2013-04-29 15:07:35 -0700 | [diff] [blame] | 3487 | spin_lock(&vmap_area_lock); |
Uladzislau Rezki (Sony) | e36176b | 2019-11-30 17:54:47 -0800 | [diff] [blame] | 3488 | |
zijun_hu | 3f50006 | 2016-12-12 16:42:17 -0800 | [diff] [blame] | 3489 | return seq_list_start(&vmap_area_list, *pos); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | static void *s_next(struct seq_file *m, void *p, loff_t *pos) |
| 3493 | { |
zijun_hu | 3f50006 | 2016-12-12 16:42:17 -0800 | [diff] [blame] | 3494 | return seq_list_next(p, &vmap_area_list, pos); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | static void s_stop(struct seq_file *m, void *p) |
Joonsoo Kim | d4033af | 2013-04-29 15:07:35 -0700 | [diff] [blame] | 3498 | __releases(&vmap_area_lock) |
Waiman Long | 0a7dd4e | 2020-12-14 19:08:59 -0800 | [diff] [blame] | 3499 | __releases(&vmap_purge_lock) |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3500 | { |
Joonsoo Kim | d4033af | 2013-04-29 15:07:35 -0700 | [diff] [blame] | 3501 | spin_unlock(&vmap_area_lock); |
Waiman Long | 0a7dd4e | 2020-12-14 19:08:59 -0800 | [diff] [blame] | 3502 | mutex_unlock(&vmap_purge_lock); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3503 | } |
| 3504 | |
Eric Dumazet | a47a126 | 2008-07-23 21:27:38 -0700 | [diff] [blame] | 3505 | static void show_numa_info(struct seq_file *m, struct vm_struct *v) |
| 3506 | { |
Kirill A. Shutemov | e5adfff | 2012-12-11 16:00:29 -0800 | [diff] [blame] | 3507 | if (IS_ENABLED(CONFIG_NUMA)) { |
Eric Dumazet | a47a126 | 2008-07-23 21:27:38 -0700 | [diff] [blame] | 3508 | unsigned int nr, *counters = m->private; |
| 3509 | |
| 3510 | if (!counters) |
| 3511 | return; |
| 3512 | |
Wanpeng Li | af12346 | 2013-11-12 15:07:32 -0800 | [diff] [blame] | 3513 | if (v->flags & VM_UNINITIALIZED) |
| 3514 | return; |
Dmitry Vyukov | 7e5b528 | 2014-12-12 16:56:30 -0800 | [diff] [blame] | 3515 | /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */ |
| 3516 | smp_rmb(); |
Wanpeng Li | af12346 | 2013-11-12 15:07:32 -0800 | [diff] [blame] | 3517 | |
Eric Dumazet | a47a126 | 2008-07-23 21:27:38 -0700 | [diff] [blame] | 3518 | memset(counters, 0, nr_node_ids * sizeof(unsigned int)); |
| 3519 | |
| 3520 | for (nr = 0; nr < v->nr_pages; nr++) |
| 3521 | counters[page_to_nid(v->pages[nr])]++; |
| 3522 | |
| 3523 | for_each_node_state(nr, N_HIGH_MEMORY) |
| 3524 | if (counters[nr]) |
| 3525 | seq_printf(m, " N%u=%u", nr, counters[nr]); |
| 3526 | } |
| 3527 | } |
| 3528 | |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 3529 | static void show_purge_info(struct seq_file *m) |
| 3530 | { |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 3531 | struct vmap_area *va; |
| 3532 | |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 3533 | spin_lock(&purge_vmap_area_lock); |
| 3534 | list_for_each_entry(va, &purge_vmap_area_list, list) { |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 3535 | seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n", |
| 3536 | (void *)va->va_start, (void *)va->va_end, |
| 3537 | va->va_end - va->va_start); |
| 3538 | } |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 3539 | spin_unlock(&purge_vmap_area_lock); |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 3540 | } |
| 3541 | |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3542 | static int s_show(struct seq_file *m, void *p) |
| 3543 | { |
zijun_hu | 3f50006 | 2016-12-12 16:42:17 -0800 | [diff] [blame] | 3544 | struct vmap_area *va; |
Joonsoo Kim | d4033af | 2013-04-29 15:07:35 -0700 | [diff] [blame] | 3545 | struct vm_struct *v; |
| 3546 | |
zijun_hu | 3f50006 | 2016-12-12 16:42:17 -0800 | [diff] [blame] | 3547 | va = list_entry(p, struct vmap_area, list); |
| 3548 | |
Wanpeng Li | c2ce8c1 | 2013-11-12 15:07:31 -0800 | [diff] [blame] | 3549 | /* |
Pengfei Li | 688fcbf | 2019-09-23 15:36:39 -0700 | [diff] [blame] | 3550 | * s_show can encounter race with remove_vm_area, !vm on behalf |
| 3551 | * of vmap area is being tear down or vm_map_ram allocation. |
Wanpeng Li | c2ce8c1 | 2013-11-12 15:07:31 -0800 | [diff] [blame] | 3552 | */ |
Pengfei Li | 688fcbf | 2019-09-23 15:36:39 -0700 | [diff] [blame] | 3553 | if (!va->vm) { |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 3554 | seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n", |
Yisheng Xie | 78c7274 | 2017-07-10 15:48:09 -0700 | [diff] [blame] | 3555 | (void *)va->va_start, (void *)va->va_end, |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 3556 | va->va_end - va->va_start); |
Yisheng Xie | 78c7274 | 2017-07-10 15:48:09 -0700 | [diff] [blame] | 3557 | |
Joonsoo Kim | d4033af | 2013-04-29 15:07:35 -0700 | [diff] [blame] | 3558 | return 0; |
Yisheng Xie | 78c7274 | 2017-07-10 15:48:09 -0700 | [diff] [blame] | 3559 | } |
Joonsoo Kim | d4033af | 2013-04-29 15:07:35 -0700 | [diff] [blame] | 3560 | |
Joonsoo Kim | d4033af | 2013-04-29 15:07:35 -0700 | [diff] [blame] | 3561 | v = va->vm; |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3562 | |
Kees Cook | 45ec169 | 2012-10-08 16:34:09 -0700 | [diff] [blame] | 3563 | seq_printf(m, "0x%pK-0x%pK %7ld", |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3564 | v->addr, v->addr + v->size, v->size); |
| 3565 | |
Joe Perches | 62c70bc | 2011-01-13 15:45:52 -0800 | [diff] [blame] | 3566 | if (v->caller) |
| 3567 | seq_printf(m, " %pS", v->caller); |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 3568 | |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3569 | if (v->nr_pages) |
| 3570 | seq_printf(m, " pages=%d", v->nr_pages); |
| 3571 | |
| 3572 | if (v->phys_addr) |
Miles Chen | 199eaa0 | 2017-02-24 14:59:51 -0800 | [diff] [blame] | 3573 | seq_printf(m, " phys=%pa", &v->phys_addr); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3574 | |
| 3575 | if (v->flags & VM_IOREMAP) |
Fabian Frederick | f4527c9 | 2014-06-04 16:08:09 -0700 | [diff] [blame] | 3576 | seq_puts(m, " ioremap"); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3577 | |
| 3578 | if (v->flags & VM_ALLOC) |
Fabian Frederick | f4527c9 | 2014-06-04 16:08:09 -0700 | [diff] [blame] | 3579 | seq_puts(m, " vmalloc"); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3580 | |
| 3581 | if (v->flags & VM_MAP) |
Fabian Frederick | f4527c9 | 2014-06-04 16:08:09 -0700 | [diff] [blame] | 3582 | seq_puts(m, " vmap"); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3583 | |
| 3584 | if (v->flags & VM_USERMAP) |
Fabian Frederick | f4527c9 | 2014-06-04 16:08:09 -0700 | [diff] [blame] | 3585 | seq_puts(m, " user"); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3586 | |
Christoph Hellwig | fe9041c | 2019-06-03 08:55:13 +0200 | [diff] [blame] | 3587 | if (v->flags & VM_DMA_COHERENT) |
| 3588 | seq_puts(m, " dma-coherent"); |
| 3589 | |
David Rientjes | 244d63e | 2016-01-14 15:19:35 -0800 | [diff] [blame] | 3590 | if (is_vmalloc_addr(v->pages)) |
Fabian Frederick | f4527c9 | 2014-06-04 16:08:09 -0700 | [diff] [blame] | 3591 | seq_puts(m, " vpages"); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3592 | |
Eric Dumazet | a47a126 | 2008-07-23 21:27:38 -0700 | [diff] [blame] | 3593 | show_numa_info(m, v); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3594 | seq_putc(m, '\n'); |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 3595 | |
| 3596 | /* |
Uladzislau Rezki (Sony) | 96e2db4 | 2020-12-14 19:08:49 -0800 | [diff] [blame] | 3597 | * As a final step, dump "unpurged" areas. |
Uladzislau Rezki (Sony) | dd3b835 | 2019-09-23 15:36:36 -0700 | [diff] [blame] | 3598 | */ |
| 3599 | if (list_is_last(&va->list, &vmap_area_list)) |
| 3600 | show_purge_info(m); |
| 3601 | |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3602 | return 0; |
| 3603 | } |
| 3604 | |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 3605 | static const struct seq_operations vmalloc_op = { |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3606 | .start = s_start, |
| 3607 | .next = s_next, |
| 3608 | .stop = s_stop, |
| 3609 | .show = s_show, |
| 3610 | }; |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 3611 | |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 3612 | static int __init proc_vmalloc_init(void) |
| 3613 | { |
Christoph Hellwig | fddda2b | 2018-04-13 19:44:18 +0200 | [diff] [blame] | 3614 | if (IS_ENABLED(CONFIG_NUMA)) |
Joe Perches | 0825a6f | 2018-06-14 15:27:58 -0700 | [diff] [blame] | 3615 | proc_create_seq_private("vmallocinfo", 0400, NULL, |
Christoph Hellwig | 44414d8 | 2018-04-24 17:05:17 +0200 | [diff] [blame] | 3616 | &vmalloc_op, |
| 3617 | nr_node_ids * sizeof(unsigned int), NULL); |
Christoph Hellwig | fddda2b | 2018-04-13 19:44:18 +0200 | [diff] [blame] | 3618 | else |
Joe Perches | 0825a6f | 2018-06-14 15:27:58 -0700 | [diff] [blame] | 3619 | proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op); |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 3620 | return 0; |
| 3621 | } |
| 3622 | module_init(proc_vmalloc_init); |
Joonsoo Kim | db3808c | 2013-04-29 15:07:28 -0700 | [diff] [blame] | 3623 | |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 3624 | #endif |