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