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