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