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