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