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