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