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