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> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/spinlock.h> | |
17 | #include <linux/interrupt.h> | |
5f6a6a9c | 18 | #include <linux/proc_fs.h> |
a10aa579 | 19 | #include <linux/seq_file.h> |
3ac7fe5a | 20 | #include <linux/debugobjects.h> |
23016969 | 21 | #include <linux/kallsyms.h> |
db64fe02 NP |
22 | #include <linux/list.h> |
23 | #include <linux/rbtree.h> | |
24 | #include <linux/radix-tree.h> | |
25 | #include <linux/rcupdate.h> | |
822c18f2 | 26 | #include <linux/bootmem.h> |
f0aa6617 | 27 | #include <linux/pfn.h> |
1da177e4 | 28 | |
db64fe02 | 29 | #include <asm/atomic.h> |
1da177e4 LT |
30 | #include <asm/uaccess.h> |
31 | #include <asm/tlbflush.h> | |
32 | ||
33 | ||
db64fe02 | 34 | /*** Page table manipulation functions ***/ |
b221385b | 35 | |
1da177e4 LT |
36 | static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end) |
37 | { | |
38 | pte_t *pte; | |
39 | ||
40 | pte = pte_offset_kernel(pmd, addr); | |
41 | do { | |
42 | pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte); | |
43 | WARN_ON(!pte_none(ptent) && !pte_present(ptent)); | |
44 | } while (pte++, addr += PAGE_SIZE, addr != end); | |
45 | } | |
46 | ||
db64fe02 | 47 | static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end) |
1da177e4 LT |
48 | { |
49 | pmd_t *pmd; | |
50 | unsigned long next; | |
51 | ||
52 | pmd = pmd_offset(pud, addr); | |
53 | do { | |
54 | next = pmd_addr_end(addr, end); | |
55 | if (pmd_none_or_clear_bad(pmd)) | |
56 | continue; | |
57 | vunmap_pte_range(pmd, addr, next); | |
58 | } while (pmd++, addr = next, addr != end); | |
59 | } | |
60 | ||
db64fe02 | 61 | static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end) |
1da177e4 LT |
62 | { |
63 | pud_t *pud; | |
64 | unsigned long next; | |
65 | ||
66 | pud = pud_offset(pgd, addr); | |
67 | do { | |
68 | next = pud_addr_end(addr, end); | |
69 | if (pud_none_or_clear_bad(pud)) | |
70 | continue; | |
71 | vunmap_pmd_range(pud, addr, next); | |
72 | } while (pud++, addr = next, addr != end); | |
73 | } | |
74 | ||
db64fe02 | 75 | static void vunmap_page_range(unsigned long addr, unsigned long end) |
1da177e4 LT |
76 | { |
77 | pgd_t *pgd; | |
78 | unsigned long next; | |
1da177e4 LT |
79 | |
80 | BUG_ON(addr >= end); | |
81 | pgd = pgd_offset_k(addr); | |
1da177e4 LT |
82 | do { |
83 | next = pgd_addr_end(addr, end); | |
84 | if (pgd_none_or_clear_bad(pgd)) | |
85 | continue; | |
86 | vunmap_pud_range(pgd, addr, next); | |
87 | } while (pgd++, addr = next, addr != end); | |
1da177e4 LT |
88 | } |
89 | ||
90 | static int vmap_pte_range(pmd_t *pmd, unsigned long addr, | |
db64fe02 | 91 | unsigned long end, pgprot_t prot, struct page **pages, int *nr) |
1da177e4 LT |
92 | { |
93 | pte_t *pte; | |
94 | ||
db64fe02 NP |
95 | /* |
96 | * nr is a running index into the array which helps higher level | |
97 | * callers keep track of where we're up to. | |
98 | */ | |
99 | ||
872fec16 | 100 | pte = pte_alloc_kernel(pmd, addr); |
1da177e4 LT |
101 | if (!pte) |
102 | return -ENOMEM; | |
103 | do { | |
db64fe02 NP |
104 | struct page *page = pages[*nr]; |
105 | ||
106 | if (WARN_ON(!pte_none(*pte))) | |
107 | return -EBUSY; | |
108 | if (WARN_ON(!page)) | |
1da177e4 LT |
109 | return -ENOMEM; |
110 | set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); | |
db64fe02 | 111 | (*nr)++; |
1da177e4 LT |
112 | } while (pte++, addr += PAGE_SIZE, addr != end); |
113 | return 0; | |
114 | } | |
115 | ||
db64fe02 NP |
116 | static int vmap_pmd_range(pud_t *pud, unsigned long addr, |
117 | unsigned long end, pgprot_t prot, struct page **pages, int *nr) | |
1da177e4 LT |
118 | { |
119 | pmd_t *pmd; | |
120 | unsigned long next; | |
121 | ||
122 | pmd = pmd_alloc(&init_mm, pud, addr); | |
123 | if (!pmd) | |
124 | return -ENOMEM; | |
125 | do { | |
126 | next = pmd_addr_end(addr, end); | |
db64fe02 | 127 | if (vmap_pte_range(pmd, addr, next, prot, pages, nr)) |
1da177e4 LT |
128 | return -ENOMEM; |
129 | } while (pmd++, addr = next, addr != end); | |
130 | return 0; | |
131 | } | |
132 | ||
db64fe02 NP |
133 | static int vmap_pud_range(pgd_t *pgd, unsigned long addr, |
134 | unsigned long end, pgprot_t prot, struct page **pages, int *nr) | |
1da177e4 LT |
135 | { |
136 | pud_t *pud; | |
137 | unsigned long next; | |
138 | ||
139 | pud = pud_alloc(&init_mm, pgd, addr); | |
140 | if (!pud) | |
141 | return -ENOMEM; | |
142 | do { | |
143 | next = pud_addr_end(addr, end); | |
db64fe02 | 144 | if (vmap_pmd_range(pud, addr, next, prot, pages, nr)) |
1da177e4 LT |
145 | return -ENOMEM; |
146 | } while (pud++, addr = next, addr != end); | |
147 | return 0; | |
148 | } | |
149 | ||
db64fe02 NP |
150 | /* |
151 | * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and | |
152 | * will have pfns corresponding to the "pages" array. | |
153 | * | |
154 | * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N] | |
155 | */ | |
8fc48985 TH |
156 | static int vmap_page_range_noflush(unsigned long start, unsigned long end, |
157 | pgprot_t prot, struct page **pages) | |
1da177e4 LT |
158 | { |
159 | pgd_t *pgd; | |
160 | unsigned long next; | |
2e4e27c7 | 161 | unsigned long addr = start; |
db64fe02 NP |
162 | int err = 0; |
163 | int nr = 0; | |
1da177e4 LT |
164 | |
165 | BUG_ON(addr >= end); | |
166 | pgd = pgd_offset_k(addr); | |
1da177e4 LT |
167 | do { |
168 | next = pgd_addr_end(addr, end); | |
db64fe02 | 169 | err = vmap_pud_range(pgd, addr, next, prot, pages, &nr); |
1da177e4 LT |
170 | if (err) |
171 | break; | |
172 | } while (pgd++, addr = next, addr != end); | |
db64fe02 NP |
173 | |
174 | if (unlikely(err)) | |
175 | return err; | |
176 | return nr; | |
1da177e4 LT |
177 | } |
178 | ||
8fc48985 TH |
179 | static int vmap_page_range(unsigned long start, unsigned long end, |
180 | pgprot_t prot, struct page **pages) | |
181 | { | |
182 | int ret; | |
183 | ||
184 | ret = vmap_page_range_noflush(start, end, prot, pages); | |
185 | flush_cache_vmap(start, end); | |
186 | return ret; | |
187 | } | |
188 | ||
73bdf0a6 LT |
189 | static inline int is_vmalloc_or_module_addr(const void *x) |
190 | { | |
191 | /* | |
ab4f2ee1 | 192 | * ARM, x86-64 and sparc64 put modules in a special place, |
73bdf0a6 LT |
193 | * and fall back on vmalloc() if that fails. Others |
194 | * just put it in the vmalloc space. | |
195 | */ | |
196 | #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) | |
197 | unsigned long addr = (unsigned long)x; | |
198 | if (addr >= MODULES_VADDR && addr < MODULES_END) | |
199 | return 1; | |
200 | #endif | |
201 | return is_vmalloc_addr(x); | |
202 | } | |
203 | ||
48667e7a | 204 | /* |
db64fe02 | 205 | * Walk a vmap address to the struct page it maps. |
48667e7a | 206 | */ |
b3bdda02 | 207 | struct page *vmalloc_to_page(const void *vmalloc_addr) |
48667e7a CL |
208 | { |
209 | unsigned long addr = (unsigned long) vmalloc_addr; | |
210 | struct page *page = NULL; | |
211 | pgd_t *pgd = pgd_offset_k(addr); | |
48667e7a | 212 | |
7aa413de IM |
213 | /* |
214 | * XXX we might need to change this if we add VIRTUAL_BUG_ON for | |
215 | * architectures that do not vmalloc module space | |
216 | */ | |
73bdf0a6 | 217 | VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); |
59ea7463 | 218 | |
48667e7a | 219 | if (!pgd_none(*pgd)) { |
db64fe02 | 220 | pud_t *pud = pud_offset(pgd, addr); |
48667e7a | 221 | if (!pud_none(*pud)) { |
db64fe02 | 222 | pmd_t *pmd = pmd_offset(pud, addr); |
48667e7a | 223 | if (!pmd_none(*pmd)) { |
db64fe02 NP |
224 | pte_t *ptep, pte; |
225 | ||
48667e7a CL |
226 | ptep = pte_offset_map(pmd, addr); |
227 | pte = *ptep; | |
228 | if (pte_present(pte)) | |
229 | page = pte_page(pte); | |
230 | pte_unmap(ptep); | |
231 | } | |
232 | } | |
233 | } | |
234 | return page; | |
235 | } | |
236 | EXPORT_SYMBOL(vmalloc_to_page); | |
237 | ||
238 | /* | |
239 | * Map a vmalloc()-space virtual address to the physical page frame number. | |
240 | */ | |
b3bdda02 | 241 | unsigned long vmalloc_to_pfn(const void *vmalloc_addr) |
48667e7a CL |
242 | { |
243 | return page_to_pfn(vmalloc_to_page(vmalloc_addr)); | |
244 | } | |
245 | EXPORT_SYMBOL(vmalloc_to_pfn); | |
246 | ||
db64fe02 NP |
247 | |
248 | /*** Global kva allocator ***/ | |
249 | ||
250 | #define VM_LAZY_FREE 0x01 | |
251 | #define VM_LAZY_FREEING 0x02 | |
252 | #define VM_VM_AREA 0x04 | |
253 | ||
254 | struct vmap_area { | |
255 | unsigned long va_start; | |
256 | unsigned long va_end; | |
257 | unsigned long flags; | |
258 | struct rb_node rb_node; /* address sorted rbtree */ | |
259 | struct list_head list; /* address sorted list */ | |
260 | struct list_head purge_list; /* "lazy purge" list */ | |
261 | void *private; | |
262 | struct rcu_head rcu_head; | |
263 | }; | |
264 | ||
265 | static DEFINE_SPINLOCK(vmap_area_lock); | |
266 | static struct rb_root vmap_area_root = RB_ROOT; | |
267 | static LIST_HEAD(vmap_area_list); | |
268 | ||
269 | static struct vmap_area *__find_vmap_area(unsigned long addr) | |
1da177e4 | 270 | { |
db64fe02 NP |
271 | struct rb_node *n = vmap_area_root.rb_node; |
272 | ||
273 | while (n) { | |
274 | struct vmap_area *va; | |
275 | ||
276 | va = rb_entry(n, struct vmap_area, rb_node); | |
277 | if (addr < va->va_start) | |
278 | n = n->rb_left; | |
279 | else if (addr > va->va_start) | |
280 | n = n->rb_right; | |
281 | else | |
282 | return va; | |
283 | } | |
284 | ||
285 | return NULL; | |
286 | } | |
287 | ||
288 | static void __insert_vmap_area(struct vmap_area *va) | |
289 | { | |
290 | struct rb_node **p = &vmap_area_root.rb_node; | |
291 | struct rb_node *parent = NULL; | |
292 | struct rb_node *tmp; | |
293 | ||
294 | while (*p) { | |
295 | struct vmap_area *tmp; | |
296 | ||
297 | parent = *p; | |
298 | tmp = rb_entry(parent, struct vmap_area, rb_node); | |
299 | if (va->va_start < tmp->va_end) | |
300 | p = &(*p)->rb_left; | |
301 | else if (va->va_end > tmp->va_start) | |
302 | p = &(*p)->rb_right; | |
303 | else | |
304 | BUG(); | |
305 | } | |
306 | ||
307 | rb_link_node(&va->rb_node, parent, p); | |
308 | rb_insert_color(&va->rb_node, &vmap_area_root); | |
309 | ||
310 | /* address-sort this list so it is usable like the vmlist */ | |
311 | tmp = rb_prev(&va->rb_node); | |
312 | if (tmp) { | |
313 | struct vmap_area *prev; | |
314 | prev = rb_entry(tmp, struct vmap_area, rb_node); | |
315 | list_add_rcu(&va->list, &prev->list); | |
316 | } else | |
317 | list_add_rcu(&va->list, &vmap_area_list); | |
318 | } | |
319 | ||
320 | static void purge_vmap_area_lazy(void); | |
321 | ||
322 | /* | |
323 | * Allocate a region of KVA of the specified size and alignment, within the | |
324 | * vstart and vend. | |
325 | */ | |
326 | static struct vmap_area *alloc_vmap_area(unsigned long size, | |
327 | unsigned long align, | |
328 | unsigned long vstart, unsigned long vend, | |
329 | int node, gfp_t gfp_mask) | |
330 | { | |
331 | struct vmap_area *va; | |
332 | struct rb_node *n; | |
1da177e4 | 333 | unsigned long addr; |
db64fe02 NP |
334 | int purged = 0; |
335 | ||
7766970c | 336 | BUG_ON(!size); |
db64fe02 NP |
337 | BUG_ON(size & ~PAGE_MASK); |
338 | ||
db64fe02 NP |
339 | va = kmalloc_node(sizeof(struct vmap_area), |
340 | gfp_mask & GFP_RECLAIM_MASK, node); | |
341 | if (unlikely(!va)) | |
342 | return ERR_PTR(-ENOMEM); | |
343 | ||
344 | retry: | |
0ae15132 GC |
345 | addr = ALIGN(vstart, align); |
346 | ||
db64fe02 | 347 | spin_lock(&vmap_area_lock); |
7766970c NP |
348 | if (addr + size - 1 < addr) |
349 | goto overflow; | |
350 | ||
db64fe02 NP |
351 | /* XXX: could have a last_hole cache */ |
352 | n = vmap_area_root.rb_node; | |
353 | if (n) { | |
354 | struct vmap_area *first = NULL; | |
355 | ||
356 | do { | |
357 | struct vmap_area *tmp; | |
358 | tmp = rb_entry(n, struct vmap_area, rb_node); | |
359 | if (tmp->va_end >= addr) { | |
360 | if (!first && tmp->va_start < addr + size) | |
361 | first = tmp; | |
362 | n = n->rb_left; | |
363 | } else { | |
364 | first = tmp; | |
365 | n = n->rb_right; | |
366 | } | |
367 | } while (n); | |
368 | ||
369 | if (!first) | |
370 | goto found; | |
371 | ||
372 | if (first->va_end < addr) { | |
373 | n = rb_next(&first->rb_node); | |
374 | if (n) | |
375 | first = rb_entry(n, struct vmap_area, rb_node); | |
376 | else | |
377 | goto found; | |
378 | } | |
379 | ||
f011c2da | 380 | while (addr + size > first->va_start && addr + size <= vend) { |
db64fe02 | 381 | addr = ALIGN(first->va_end + PAGE_SIZE, align); |
7766970c NP |
382 | if (addr + size - 1 < addr) |
383 | goto overflow; | |
db64fe02 NP |
384 | |
385 | n = rb_next(&first->rb_node); | |
386 | if (n) | |
387 | first = rb_entry(n, struct vmap_area, rb_node); | |
388 | else | |
389 | goto found; | |
390 | } | |
391 | } | |
392 | found: | |
393 | if (addr + size > vend) { | |
7766970c | 394 | overflow: |
db64fe02 NP |
395 | spin_unlock(&vmap_area_lock); |
396 | if (!purged) { | |
397 | purge_vmap_area_lazy(); | |
398 | purged = 1; | |
399 | goto retry; | |
400 | } | |
401 | if (printk_ratelimit()) | |
c1279c4e GC |
402 | printk(KERN_WARNING |
403 | "vmap allocation for size %lu failed: " | |
404 | "use vmalloc=<size> to increase size.\n", size); | |
db64fe02 NP |
405 | return ERR_PTR(-EBUSY); |
406 | } | |
407 | ||
408 | BUG_ON(addr & (align-1)); | |
409 | ||
410 | va->va_start = addr; | |
411 | va->va_end = addr + size; | |
412 | va->flags = 0; | |
413 | __insert_vmap_area(va); | |
414 | spin_unlock(&vmap_area_lock); | |
415 | ||
416 | return va; | |
417 | } | |
418 | ||
419 | static void rcu_free_va(struct rcu_head *head) | |
420 | { | |
421 | struct vmap_area *va = container_of(head, struct vmap_area, rcu_head); | |
422 | ||
423 | kfree(va); | |
424 | } | |
425 | ||
426 | static void __free_vmap_area(struct vmap_area *va) | |
427 | { | |
428 | BUG_ON(RB_EMPTY_NODE(&va->rb_node)); | |
429 | rb_erase(&va->rb_node, &vmap_area_root); | |
430 | RB_CLEAR_NODE(&va->rb_node); | |
431 | list_del_rcu(&va->list); | |
432 | ||
433 | call_rcu(&va->rcu_head, rcu_free_va); | |
434 | } | |
435 | ||
436 | /* | |
437 | * Free a region of KVA allocated by alloc_vmap_area | |
438 | */ | |
439 | static void free_vmap_area(struct vmap_area *va) | |
440 | { | |
441 | spin_lock(&vmap_area_lock); | |
442 | __free_vmap_area(va); | |
443 | spin_unlock(&vmap_area_lock); | |
444 | } | |
445 | ||
446 | /* | |
447 | * Clear the pagetable entries of a given vmap_area | |
448 | */ | |
449 | static void unmap_vmap_area(struct vmap_area *va) | |
450 | { | |
451 | vunmap_page_range(va->va_start, va->va_end); | |
452 | } | |
453 | ||
cd52858c NP |
454 | static void vmap_debug_free_range(unsigned long start, unsigned long end) |
455 | { | |
456 | /* | |
457 | * Unmap page tables and force a TLB flush immediately if | |
458 | * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free | |
459 | * bugs similarly to those in linear kernel virtual address | |
460 | * space after a page has been freed. | |
461 | * | |
462 | * All the lazy freeing logic is still retained, in order to | |
463 | * minimise intrusiveness of this debugging feature. | |
464 | * | |
465 | * This is going to be *slow* (linear kernel virtual address | |
466 | * debugging doesn't do a broadcast TLB flush so it is a lot | |
467 | * faster). | |
468 | */ | |
469 | #ifdef CONFIG_DEBUG_PAGEALLOC | |
470 | vunmap_page_range(start, end); | |
471 | flush_tlb_kernel_range(start, end); | |
472 | #endif | |
473 | } | |
474 | ||
db64fe02 NP |
475 | /* |
476 | * lazy_max_pages is the maximum amount of virtual address space we gather up | |
477 | * before attempting to purge with a TLB flush. | |
478 | * | |
479 | * There is a tradeoff here: a larger number will cover more kernel page tables | |
480 | * and take slightly longer to purge, but it will linearly reduce the number of | |
481 | * global TLB flushes that must be performed. It would seem natural to scale | |
482 | * this number up linearly with the number of CPUs (because vmapping activity | |
483 | * could also scale linearly with the number of CPUs), however it is likely | |
484 | * that in practice, workloads might be constrained in other ways that mean | |
485 | * vmap activity will not scale linearly with CPUs. Also, I want to be | |
486 | * conservative and not introduce a big latency on huge systems, so go with | |
487 | * a less aggressive log scale. It will still be an improvement over the old | |
488 | * code, and it will be simple to change the scale factor if we find that it | |
489 | * becomes a problem on bigger systems. | |
490 | */ | |
491 | static unsigned long lazy_max_pages(void) | |
492 | { | |
493 | unsigned int log; | |
494 | ||
495 | log = fls(num_online_cpus()); | |
496 | ||
497 | return log * (32UL * 1024 * 1024 / PAGE_SIZE); | |
498 | } | |
499 | ||
500 | static atomic_t vmap_lazy_nr = ATOMIC_INIT(0); | |
501 | ||
502 | /* | |
503 | * Purges all lazily-freed vmap areas. | |
504 | * | |
505 | * If sync is 0 then don't purge if there is already a purge in progress. | |
506 | * If force_flush is 1, then flush kernel TLBs between *start and *end even | |
507 | * if we found no lazy vmap areas to unmap (callers can use this to optimise | |
508 | * their own TLB flushing). | |
509 | * Returns with *start = min(*start, lowest purged address) | |
510 | * *end = max(*end, highest purged address) | |
511 | */ | |
512 | static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end, | |
513 | int sync, int force_flush) | |
514 | { | |
46666d8a | 515 | static DEFINE_SPINLOCK(purge_lock); |
db64fe02 NP |
516 | LIST_HEAD(valist); |
517 | struct vmap_area *va; | |
cbb76676 | 518 | struct vmap_area *n_va; |
db64fe02 NP |
519 | int nr = 0; |
520 | ||
521 | /* | |
522 | * If sync is 0 but force_flush is 1, we'll go sync anyway but callers | |
523 | * should not expect such behaviour. This just simplifies locking for | |
524 | * the case that isn't actually used at the moment anyway. | |
525 | */ | |
526 | if (!sync && !force_flush) { | |
46666d8a | 527 | if (!spin_trylock(&purge_lock)) |
db64fe02 NP |
528 | return; |
529 | } else | |
46666d8a | 530 | spin_lock(&purge_lock); |
db64fe02 NP |
531 | |
532 | rcu_read_lock(); | |
533 | list_for_each_entry_rcu(va, &vmap_area_list, list) { | |
534 | if (va->flags & VM_LAZY_FREE) { | |
535 | if (va->va_start < *start) | |
536 | *start = va->va_start; | |
537 | if (va->va_end > *end) | |
538 | *end = va->va_end; | |
539 | nr += (va->va_end - va->va_start) >> PAGE_SHIFT; | |
540 | unmap_vmap_area(va); | |
541 | list_add_tail(&va->purge_list, &valist); | |
542 | va->flags |= VM_LAZY_FREEING; | |
543 | va->flags &= ~VM_LAZY_FREE; | |
544 | } | |
545 | } | |
546 | rcu_read_unlock(); | |
547 | ||
548 | if (nr) { | |
549 | BUG_ON(nr > atomic_read(&vmap_lazy_nr)); | |
550 | atomic_sub(nr, &vmap_lazy_nr); | |
551 | } | |
552 | ||
553 | if (nr || force_flush) | |
554 | flush_tlb_kernel_range(*start, *end); | |
555 | ||
556 | if (nr) { | |
557 | spin_lock(&vmap_area_lock); | |
cbb76676 | 558 | list_for_each_entry_safe(va, n_va, &valist, purge_list) |
db64fe02 NP |
559 | __free_vmap_area(va); |
560 | spin_unlock(&vmap_area_lock); | |
561 | } | |
46666d8a | 562 | spin_unlock(&purge_lock); |
db64fe02 NP |
563 | } |
564 | ||
496850e5 NP |
565 | /* |
566 | * Kick off a purge of the outstanding lazy areas. Don't bother if somebody | |
567 | * is already purging. | |
568 | */ | |
569 | static void try_purge_vmap_area_lazy(void) | |
570 | { | |
571 | unsigned long start = ULONG_MAX, end = 0; | |
572 | ||
573 | __purge_vmap_area_lazy(&start, &end, 0, 0); | |
574 | } | |
575 | ||
db64fe02 NP |
576 | /* |
577 | * Kick off a purge of the outstanding lazy areas. | |
578 | */ | |
579 | static void purge_vmap_area_lazy(void) | |
580 | { | |
581 | unsigned long start = ULONG_MAX, end = 0; | |
582 | ||
496850e5 | 583 | __purge_vmap_area_lazy(&start, &end, 1, 0); |
db64fe02 NP |
584 | } |
585 | ||
586 | /* | |
b29acbdc NP |
587 | * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been |
588 | * called for the correct range previously. | |
db64fe02 | 589 | */ |
b29acbdc | 590 | static void free_unmap_vmap_area_noflush(struct vmap_area *va) |
db64fe02 NP |
591 | { |
592 | va->flags |= VM_LAZY_FREE; | |
593 | atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr); | |
594 | if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages())) | |
496850e5 | 595 | try_purge_vmap_area_lazy(); |
db64fe02 NP |
596 | } |
597 | ||
b29acbdc NP |
598 | /* |
599 | * Free and unmap a vmap area | |
600 | */ | |
601 | static void free_unmap_vmap_area(struct vmap_area *va) | |
602 | { | |
603 | flush_cache_vunmap(va->va_start, va->va_end); | |
604 | free_unmap_vmap_area_noflush(va); | |
605 | } | |
606 | ||
db64fe02 NP |
607 | static struct vmap_area *find_vmap_area(unsigned long addr) |
608 | { | |
609 | struct vmap_area *va; | |
610 | ||
611 | spin_lock(&vmap_area_lock); | |
612 | va = __find_vmap_area(addr); | |
613 | spin_unlock(&vmap_area_lock); | |
614 | ||
615 | return va; | |
616 | } | |
617 | ||
618 | static void free_unmap_vmap_area_addr(unsigned long addr) | |
619 | { | |
620 | struct vmap_area *va; | |
621 | ||
622 | va = find_vmap_area(addr); | |
623 | BUG_ON(!va); | |
624 | free_unmap_vmap_area(va); | |
625 | } | |
626 | ||
627 | ||
628 | /*** Per cpu kva allocator ***/ | |
629 | ||
630 | /* | |
631 | * vmap space is limited especially on 32 bit architectures. Ensure there is | |
632 | * room for at least 16 percpu vmap blocks per CPU. | |
633 | */ | |
634 | /* | |
635 | * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able | |
636 | * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess | |
637 | * instead (we just need a rough idea) | |
638 | */ | |
639 | #if BITS_PER_LONG == 32 | |
640 | #define VMALLOC_SPACE (128UL*1024*1024) | |
641 | #else | |
642 | #define VMALLOC_SPACE (128UL*1024*1024*1024) | |
643 | #endif | |
644 | ||
645 | #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) | |
646 | #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ | |
647 | #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ | |
648 | #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) | |
649 | #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ | |
650 | #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ | |
651 | #define VMAP_BBMAP_BITS VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ | |
652 | VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ | |
653 | VMALLOC_PAGES / NR_CPUS / 16)) | |
654 | ||
655 | #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) | |
656 | ||
9b463334 JF |
657 | static bool vmap_initialized __read_mostly = false; |
658 | ||
db64fe02 NP |
659 | struct vmap_block_queue { |
660 | spinlock_t lock; | |
661 | struct list_head free; | |
662 | struct list_head dirty; | |
663 | unsigned int nr_dirty; | |
664 | }; | |
665 | ||
666 | struct vmap_block { | |
667 | spinlock_t lock; | |
668 | struct vmap_area *va; | |
669 | struct vmap_block_queue *vbq; | |
670 | unsigned long free, dirty; | |
671 | DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS); | |
672 | DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS); | |
673 | union { | |
674 | struct { | |
675 | struct list_head free_list; | |
676 | struct list_head dirty_list; | |
677 | }; | |
678 | struct rcu_head rcu_head; | |
679 | }; | |
680 | }; | |
681 | ||
682 | /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ | |
683 | static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); | |
684 | ||
685 | /* | |
686 | * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block | |
687 | * in the free path. Could get rid of this if we change the API to return a | |
688 | * "cookie" from alloc, to be passed to free. But no big deal yet. | |
689 | */ | |
690 | static DEFINE_SPINLOCK(vmap_block_tree_lock); | |
691 | static RADIX_TREE(vmap_block_tree, GFP_ATOMIC); | |
692 | ||
693 | /* | |
694 | * We should probably have a fallback mechanism to allocate virtual memory | |
695 | * out of partially filled vmap blocks. However vmap block sizing should be | |
696 | * fairly reasonable according to the vmalloc size, so it shouldn't be a | |
697 | * big problem. | |
698 | */ | |
699 | ||
700 | static unsigned long addr_to_vb_idx(unsigned long addr) | |
701 | { | |
702 | addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); | |
703 | addr /= VMAP_BLOCK_SIZE; | |
704 | return addr; | |
705 | } | |
706 | ||
707 | static struct vmap_block *new_vmap_block(gfp_t gfp_mask) | |
708 | { | |
709 | struct vmap_block_queue *vbq; | |
710 | struct vmap_block *vb; | |
711 | struct vmap_area *va; | |
712 | unsigned long vb_idx; | |
713 | int node, err; | |
714 | ||
715 | node = numa_node_id(); | |
716 | ||
717 | vb = kmalloc_node(sizeof(struct vmap_block), | |
718 | gfp_mask & GFP_RECLAIM_MASK, node); | |
719 | if (unlikely(!vb)) | |
720 | return ERR_PTR(-ENOMEM); | |
721 | ||
722 | va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, | |
723 | VMALLOC_START, VMALLOC_END, | |
724 | node, gfp_mask); | |
725 | if (unlikely(IS_ERR(va))) { | |
726 | kfree(vb); | |
727 | return ERR_PTR(PTR_ERR(va)); | |
728 | } | |
729 | ||
730 | err = radix_tree_preload(gfp_mask); | |
731 | if (unlikely(err)) { | |
732 | kfree(vb); | |
733 | free_vmap_area(va); | |
734 | return ERR_PTR(err); | |
735 | } | |
736 | ||
737 | spin_lock_init(&vb->lock); | |
738 | vb->va = va; | |
739 | vb->free = VMAP_BBMAP_BITS; | |
740 | vb->dirty = 0; | |
741 | bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS); | |
742 | bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS); | |
743 | INIT_LIST_HEAD(&vb->free_list); | |
744 | INIT_LIST_HEAD(&vb->dirty_list); | |
745 | ||
746 | vb_idx = addr_to_vb_idx(va->va_start); | |
747 | spin_lock(&vmap_block_tree_lock); | |
748 | err = radix_tree_insert(&vmap_block_tree, vb_idx, vb); | |
749 | spin_unlock(&vmap_block_tree_lock); | |
750 | BUG_ON(err); | |
751 | radix_tree_preload_end(); | |
752 | ||
753 | vbq = &get_cpu_var(vmap_block_queue); | |
754 | vb->vbq = vbq; | |
755 | spin_lock(&vbq->lock); | |
756 | list_add(&vb->free_list, &vbq->free); | |
757 | spin_unlock(&vbq->lock); | |
758 | put_cpu_var(vmap_cpu_blocks); | |
759 | ||
760 | return vb; | |
761 | } | |
762 | ||
763 | static void rcu_free_vb(struct rcu_head *head) | |
764 | { | |
765 | struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head); | |
766 | ||
767 | kfree(vb); | |
768 | } | |
769 | ||
770 | static void free_vmap_block(struct vmap_block *vb) | |
771 | { | |
772 | struct vmap_block *tmp; | |
773 | unsigned long vb_idx; | |
774 | ||
775 | spin_lock(&vb->vbq->lock); | |
776 | if (!list_empty(&vb->free_list)) | |
777 | list_del(&vb->free_list); | |
778 | if (!list_empty(&vb->dirty_list)) | |
779 | list_del(&vb->dirty_list); | |
780 | spin_unlock(&vb->vbq->lock); | |
781 | ||
782 | vb_idx = addr_to_vb_idx(vb->va->va_start); | |
783 | spin_lock(&vmap_block_tree_lock); | |
784 | tmp = radix_tree_delete(&vmap_block_tree, vb_idx); | |
785 | spin_unlock(&vmap_block_tree_lock); | |
786 | BUG_ON(tmp != vb); | |
787 | ||
b29acbdc | 788 | free_unmap_vmap_area_noflush(vb->va); |
db64fe02 NP |
789 | call_rcu(&vb->rcu_head, rcu_free_vb); |
790 | } | |
791 | ||
792 | static void *vb_alloc(unsigned long size, gfp_t gfp_mask) | |
793 | { | |
794 | struct vmap_block_queue *vbq; | |
795 | struct vmap_block *vb; | |
796 | unsigned long addr = 0; | |
797 | unsigned int order; | |
798 | ||
799 | BUG_ON(size & ~PAGE_MASK); | |
800 | BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); | |
801 | order = get_order(size); | |
802 | ||
803 | again: | |
804 | rcu_read_lock(); | |
805 | vbq = &get_cpu_var(vmap_block_queue); | |
806 | list_for_each_entry_rcu(vb, &vbq->free, free_list) { | |
807 | int i; | |
808 | ||
809 | spin_lock(&vb->lock); | |
810 | i = bitmap_find_free_region(vb->alloc_map, | |
811 | VMAP_BBMAP_BITS, order); | |
812 | ||
813 | if (i >= 0) { | |
814 | addr = vb->va->va_start + (i << PAGE_SHIFT); | |
815 | BUG_ON(addr_to_vb_idx(addr) != | |
816 | addr_to_vb_idx(vb->va->va_start)); | |
817 | vb->free -= 1UL << order; | |
818 | if (vb->free == 0) { | |
819 | spin_lock(&vbq->lock); | |
820 | list_del_init(&vb->free_list); | |
821 | spin_unlock(&vbq->lock); | |
822 | } | |
823 | spin_unlock(&vb->lock); | |
824 | break; | |
825 | } | |
826 | spin_unlock(&vb->lock); | |
827 | } | |
828 | put_cpu_var(vmap_cpu_blocks); | |
829 | rcu_read_unlock(); | |
830 | ||
831 | if (!addr) { | |
832 | vb = new_vmap_block(gfp_mask); | |
833 | if (IS_ERR(vb)) | |
834 | return vb; | |
835 | goto again; | |
836 | } | |
837 | ||
838 | return (void *)addr; | |
839 | } | |
840 | ||
841 | static void vb_free(const void *addr, unsigned long size) | |
842 | { | |
843 | unsigned long offset; | |
844 | unsigned long vb_idx; | |
845 | unsigned int order; | |
846 | struct vmap_block *vb; | |
847 | ||
848 | BUG_ON(size & ~PAGE_MASK); | |
849 | BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); | |
b29acbdc NP |
850 | |
851 | flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size); | |
852 | ||
db64fe02 NP |
853 | order = get_order(size); |
854 | ||
855 | offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1); | |
856 | ||
857 | vb_idx = addr_to_vb_idx((unsigned long)addr); | |
858 | rcu_read_lock(); | |
859 | vb = radix_tree_lookup(&vmap_block_tree, vb_idx); | |
860 | rcu_read_unlock(); | |
861 | BUG_ON(!vb); | |
862 | ||
863 | spin_lock(&vb->lock); | |
864 | bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order); | |
865 | if (!vb->dirty) { | |
866 | spin_lock(&vb->vbq->lock); | |
867 | list_add(&vb->dirty_list, &vb->vbq->dirty); | |
868 | spin_unlock(&vb->vbq->lock); | |
869 | } | |
870 | vb->dirty += 1UL << order; | |
871 | if (vb->dirty == VMAP_BBMAP_BITS) { | |
872 | BUG_ON(vb->free || !list_empty(&vb->free_list)); | |
873 | spin_unlock(&vb->lock); | |
874 | free_vmap_block(vb); | |
875 | } else | |
876 | spin_unlock(&vb->lock); | |
877 | } | |
878 | ||
879 | /** | |
880 | * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer | |
881 | * | |
882 | * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily | |
883 | * to amortize TLB flushing overheads. What this means is that any page you | |
884 | * have now, may, in a former life, have been mapped into kernel virtual | |
885 | * address by the vmap layer and so there might be some CPUs with TLB entries | |
886 | * still referencing that page (additional to the regular 1:1 kernel mapping). | |
887 | * | |
888 | * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can | |
889 | * be sure that none of the pages we have control over will have any aliases | |
890 | * from the vmap layer. | |
891 | */ | |
892 | void vm_unmap_aliases(void) | |
893 | { | |
894 | unsigned long start = ULONG_MAX, end = 0; | |
895 | int cpu; | |
896 | int flush = 0; | |
897 | ||
9b463334 JF |
898 | if (unlikely(!vmap_initialized)) |
899 | return; | |
900 | ||
db64fe02 NP |
901 | for_each_possible_cpu(cpu) { |
902 | struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); | |
903 | struct vmap_block *vb; | |
904 | ||
905 | rcu_read_lock(); | |
906 | list_for_each_entry_rcu(vb, &vbq->free, free_list) { | |
907 | int i; | |
908 | ||
909 | spin_lock(&vb->lock); | |
910 | i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS); | |
911 | while (i < VMAP_BBMAP_BITS) { | |
912 | unsigned long s, e; | |
913 | int j; | |
914 | j = find_next_zero_bit(vb->dirty_map, | |
915 | VMAP_BBMAP_BITS, i); | |
916 | ||
917 | s = vb->va->va_start + (i << PAGE_SHIFT); | |
918 | e = vb->va->va_start + (j << PAGE_SHIFT); | |
919 | vunmap_page_range(s, e); | |
920 | flush = 1; | |
921 | ||
922 | if (s < start) | |
923 | start = s; | |
924 | if (e > end) | |
925 | end = e; | |
926 | ||
927 | i = j; | |
928 | i = find_next_bit(vb->dirty_map, | |
929 | VMAP_BBMAP_BITS, i); | |
930 | } | |
931 | spin_unlock(&vb->lock); | |
932 | } | |
933 | rcu_read_unlock(); | |
934 | } | |
935 | ||
936 | __purge_vmap_area_lazy(&start, &end, 1, flush); | |
937 | } | |
938 | EXPORT_SYMBOL_GPL(vm_unmap_aliases); | |
939 | ||
940 | /** | |
941 | * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram | |
942 | * @mem: the pointer returned by vm_map_ram | |
943 | * @count: the count passed to that vm_map_ram call (cannot unmap partial) | |
944 | */ | |
945 | void vm_unmap_ram(const void *mem, unsigned int count) | |
946 | { | |
947 | unsigned long size = count << PAGE_SHIFT; | |
948 | unsigned long addr = (unsigned long)mem; | |
949 | ||
950 | BUG_ON(!addr); | |
951 | BUG_ON(addr < VMALLOC_START); | |
952 | BUG_ON(addr > VMALLOC_END); | |
953 | BUG_ON(addr & (PAGE_SIZE-1)); | |
954 | ||
955 | debug_check_no_locks_freed(mem, size); | |
cd52858c | 956 | vmap_debug_free_range(addr, addr+size); |
db64fe02 NP |
957 | |
958 | if (likely(count <= VMAP_MAX_ALLOC)) | |
959 | vb_free(mem, size); | |
960 | else | |
961 | free_unmap_vmap_area_addr(addr); | |
962 | } | |
963 | EXPORT_SYMBOL(vm_unmap_ram); | |
964 | ||
965 | /** | |
966 | * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) | |
967 | * @pages: an array of pointers to the pages to be mapped | |
968 | * @count: number of pages | |
969 | * @node: prefer to allocate data structures on this node | |
970 | * @prot: memory protection to use. PAGE_KERNEL for regular RAM | |
e99c97ad RD |
971 | * |
972 | * Returns: a pointer to the address that has been mapped, or %NULL on failure | |
db64fe02 NP |
973 | */ |
974 | void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) | |
975 | { | |
976 | unsigned long size = count << PAGE_SHIFT; | |
977 | unsigned long addr; | |
978 | void *mem; | |
979 | ||
980 | if (likely(count <= VMAP_MAX_ALLOC)) { | |
981 | mem = vb_alloc(size, GFP_KERNEL); | |
982 | if (IS_ERR(mem)) | |
983 | return NULL; | |
984 | addr = (unsigned long)mem; | |
985 | } else { | |
986 | struct vmap_area *va; | |
987 | va = alloc_vmap_area(size, PAGE_SIZE, | |
988 | VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); | |
989 | if (IS_ERR(va)) | |
990 | return NULL; | |
991 | ||
992 | addr = va->va_start; | |
993 | mem = (void *)addr; | |
994 | } | |
995 | if (vmap_page_range(addr, addr + size, prot, pages) < 0) { | |
996 | vm_unmap_ram(mem, count); | |
997 | return NULL; | |
998 | } | |
999 | return mem; | |
1000 | } | |
1001 | EXPORT_SYMBOL(vm_map_ram); | |
1002 | ||
f0aa6617 TH |
1003 | /** |
1004 | * vm_area_register_early - register vmap area early during boot | |
1005 | * @vm: vm_struct to register | |
c0c0a293 | 1006 | * @align: requested alignment |
f0aa6617 TH |
1007 | * |
1008 | * This function is used to register kernel vm area before | |
1009 | * vmalloc_init() is called. @vm->size and @vm->flags should contain | |
1010 | * proper values on entry and other fields should be zero. On return, | |
1011 | * vm->addr contains the allocated address. | |
1012 | * | |
1013 | * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. | |
1014 | */ | |
c0c0a293 | 1015 | void __init vm_area_register_early(struct vm_struct *vm, size_t align) |
f0aa6617 TH |
1016 | { |
1017 | static size_t vm_init_off __initdata; | |
c0c0a293 TH |
1018 | unsigned long addr; |
1019 | ||
1020 | addr = ALIGN(VMALLOC_START + vm_init_off, align); | |
1021 | vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START; | |
f0aa6617 | 1022 | |
c0c0a293 | 1023 | vm->addr = (void *)addr; |
f0aa6617 TH |
1024 | |
1025 | vm->next = vmlist; | |
1026 | vmlist = vm; | |
1027 | } | |
1028 | ||
db64fe02 NP |
1029 | void __init vmalloc_init(void) |
1030 | { | |
822c18f2 IK |
1031 | struct vmap_area *va; |
1032 | struct vm_struct *tmp; | |
db64fe02 NP |
1033 | int i; |
1034 | ||
1035 | for_each_possible_cpu(i) { | |
1036 | struct vmap_block_queue *vbq; | |
1037 | ||
1038 | vbq = &per_cpu(vmap_block_queue, i); | |
1039 | spin_lock_init(&vbq->lock); | |
1040 | INIT_LIST_HEAD(&vbq->free); | |
1041 | INIT_LIST_HEAD(&vbq->dirty); | |
1042 | vbq->nr_dirty = 0; | |
1043 | } | |
9b463334 | 1044 | |
822c18f2 IK |
1045 | /* Import existing vmlist entries. */ |
1046 | for (tmp = vmlist; tmp; tmp = tmp->next) { | |
1047 | va = alloc_bootmem(sizeof(struct vmap_area)); | |
1048 | va->flags = tmp->flags | VM_VM_AREA; | |
1049 | va->va_start = (unsigned long)tmp->addr; | |
1050 | va->va_end = va->va_start + tmp->size; | |
1051 | __insert_vmap_area(va); | |
1052 | } | |
9b463334 | 1053 | vmap_initialized = true; |
db64fe02 NP |
1054 | } |
1055 | ||
8fc48985 TH |
1056 | /** |
1057 | * map_kernel_range_noflush - map kernel VM area with the specified pages | |
1058 | * @addr: start of the VM area to map | |
1059 | * @size: size of the VM area to map | |
1060 | * @prot: page protection flags to use | |
1061 | * @pages: pages to map | |
1062 | * | |
1063 | * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size | |
1064 | * specify should have been allocated using get_vm_area() and its | |
1065 | * friends. | |
1066 | * | |
1067 | * NOTE: | |
1068 | * This function does NOT do any cache flushing. The caller is | |
1069 | * responsible for calling flush_cache_vmap() on to-be-mapped areas | |
1070 | * before calling this function. | |
1071 | * | |
1072 | * RETURNS: | |
1073 | * The number of pages mapped on success, -errno on failure. | |
1074 | */ | |
1075 | int map_kernel_range_noflush(unsigned long addr, unsigned long size, | |
1076 | pgprot_t prot, struct page **pages) | |
1077 | { | |
1078 | return vmap_page_range_noflush(addr, addr + size, prot, pages); | |
1079 | } | |
1080 | ||
1081 | /** | |
1082 | * unmap_kernel_range_noflush - unmap kernel VM area | |
1083 | * @addr: start of the VM area to unmap | |
1084 | * @size: size of the VM area to unmap | |
1085 | * | |
1086 | * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size | |
1087 | * specify should have been allocated using get_vm_area() and its | |
1088 | * friends. | |
1089 | * | |
1090 | * NOTE: | |
1091 | * This function does NOT do any cache flushing. The caller is | |
1092 | * responsible for calling flush_cache_vunmap() on to-be-mapped areas | |
1093 | * before calling this function and flush_tlb_kernel_range() after. | |
1094 | */ | |
1095 | void unmap_kernel_range_noflush(unsigned long addr, unsigned long size) | |
1096 | { | |
1097 | vunmap_page_range(addr, addr + size); | |
1098 | } | |
1099 | ||
1100 | /** | |
1101 | * unmap_kernel_range - unmap kernel VM area and flush cache and TLB | |
1102 | * @addr: start of the VM area to unmap | |
1103 | * @size: size of the VM area to unmap | |
1104 | * | |
1105 | * Similar to unmap_kernel_range_noflush() but flushes vcache before | |
1106 | * the unmapping and tlb after. | |
1107 | */ | |
db64fe02 NP |
1108 | void unmap_kernel_range(unsigned long addr, unsigned long size) |
1109 | { | |
1110 | unsigned long end = addr + size; | |
f6fcba70 TH |
1111 | |
1112 | flush_cache_vunmap(addr, end); | |
db64fe02 NP |
1113 | vunmap_page_range(addr, end); |
1114 | flush_tlb_kernel_range(addr, end); | |
1115 | } | |
1116 | ||
1117 | int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages) | |
1118 | { | |
1119 | unsigned long addr = (unsigned long)area->addr; | |
1120 | unsigned long end = addr + area->size - PAGE_SIZE; | |
1121 | int err; | |
1122 | ||
1123 | err = vmap_page_range(addr, end, prot, *pages); | |
1124 | if (err > 0) { | |
1125 | *pages += err; | |
1126 | err = 0; | |
1127 | } | |
1128 | ||
1129 | return err; | |
1130 | } | |
1131 | EXPORT_SYMBOL_GPL(map_vm_area); | |
1132 | ||
1133 | /*** Old vmalloc interfaces ***/ | |
1134 | DEFINE_RWLOCK(vmlist_lock); | |
1135 | struct vm_struct *vmlist; | |
1136 | ||
1137 | static struct vm_struct *__get_vm_area_node(unsigned long size, | |
1138 | unsigned long flags, unsigned long start, unsigned long end, | |
1139 | int node, gfp_t gfp_mask, void *caller) | |
1140 | { | |
1141 | static struct vmap_area *va; | |
1142 | struct vm_struct *area; | |
1143 | struct vm_struct *tmp, **p; | |
1144 | unsigned long align = 1; | |
1da177e4 | 1145 | |
52fd24ca | 1146 | BUG_ON(in_interrupt()); |
1da177e4 LT |
1147 | if (flags & VM_IOREMAP) { |
1148 | int bit = fls(size); | |
1149 | ||
1150 | if (bit > IOREMAP_MAX_ORDER) | |
1151 | bit = IOREMAP_MAX_ORDER; | |
1152 | else if (bit < PAGE_SHIFT) | |
1153 | bit = PAGE_SHIFT; | |
1154 | ||
1155 | align = 1ul << bit; | |
1156 | } | |
db64fe02 | 1157 | |
1da177e4 | 1158 | size = PAGE_ALIGN(size); |
31be8309 OH |
1159 | if (unlikely(!size)) |
1160 | return NULL; | |
1da177e4 | 1161 | |
6cb06229 | 1162 | area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); |
1da177e4 LT |
1163 | if (unlikely(!area)) |
1164 | return NULL; | |
1165 | ||
1da177e4 LT |
1166 | /* |
1167 | * We always allocate a guard page. | |
1168 | */ | |
1169 | size += PAGE_SIZE; | |
1170 | ||
db64fe02 NP |
1171 | va = alloc_vmap_area(size, align, start, end, node, gfp_mask); |
1172 | if (IS_ERR(va)) { | |
1173 | kfree(area); | |
1174 | return NULL; | |
1da177e4 | 1175 | } |
1da177e4 LT |
1176 | |
1177 | area->flags = flags; | |
db64fe02 | 1178 | area->addr = (void *)va->va_start; |
1da177e4 LT |
1179 | area->size = size; |
1180 | area->pages = NULL; | |
1181 | area->nr_pages = 0; | |
1182 | area->phys_addr = 0; | |
23016969 | 1183 | area->caller = caller; |
db64fe02 NP |
1184 | va->private = area; |
1185 | va->flags |= VM_VM_AREA; | |
1186 | ||
1187 | write_lock(&vmlist_lock); | |
1188 | for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { | |
1189 | if (tmp->addr >= area->addr) | |
1190 | break; | |
1191 | } | |
1192 | area->next = *p; | |
1193 | *p = area; | |
1da177e4 LT |
1194 | write_unlock(&vmlist_lock); |
1195 | ||
1196 | return area; | |
1da177e4 LT |
1197 | } |
1198 | ||
930fc45a CL |
1199 | struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags, |
1200 | unsigned long start, unsigned long end) | |
1201 | { | |
23016969 CL |
1202 | return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL, |
1203 | __builtin_return_address(0)); | |
930fc45a | 1204 | } |
5992b6da | 1205 | EXPORT_SYMBOL_GPL(__get_vm_area); |
930fc45a | 1206 | |
c2968612 BH |
1207 | struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, |
1208 | unsigned long start, unsigned long end, | |
1209 | void *caller) | |
1210 | { | |
1211 | return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL, | |
1212 | caller); | |
1213 | } | |
1214 | ||
1da177e4 | 1215 | /** |
183ff22b | 1216 | * get_vm_area - reserve a contiguous kernel virtual area |
1da177e4 LT |
1217 | * @size: size of the area |
1218 | * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC | |
1219 | * | |
1220 | * Search an area of @size in the kernel virtual mapping area, | |
1221 | * and reserved it for out purposes. Returns the area descriptor | |
1222 | * on success or %NULL on failure. | |
1223 | */ | |
1224 | struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) | |
1225 | { | |
23016969 CL |
1226 | return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, |
1227 | -1, GFP_KERNEL, __builtin_return_address(0)); | |
1228 | } | |
1229 | ||
1230 | struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, | |
1231 | void *caller) | |
1232 | { | |
1233 | return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, | |
1234 | -1, GFP_KERNEL, caller); | |
1da177e4 LT |
1235 | } |
1236 | ||
52fd24ca GP |
1237 | struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags, |
1238 | int node, gfp_t gfp_mask) | |
930fc45a | 1239 | { |
52fd24ca | 1240 | return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node, |
23016969 | 1241 | gfp_mask, __builtin_return_address(0)); |
930fc45a CL |
1242 | } |
1243 | ||
db64fe02 | 1244 | static struct vm_struct *find_vm_area(const void *addr) |
83342314 | 1245 | { |
db64fe02 | 1246 | struct vmap_area *va; |
83342314 | 1247 | |
db64fe02 NP |
1248 | va = find_vmap_area((unsigned long)addr); |
1249 | if (va && va->flags & VM_VM_AREA) | |
1250 | return va->private; | |
1da177e4 | 1251 | |
1da177e4 | 1252 | return NULL; |
1da177e4 LT |
1253 | } |
1254 | ||
7856dfeb | 1255 | /** |
183ff22b | 1256 | * remove_vm_area - find and remove a continuous kernel virtual area |
7856dfeb AK |
1257 | * @addr: base address |
1258 | * | |
1259 | * Search for the kernel VM area starting at @addr, and remove it. | |
1260 | * This function returns the found VM area, but using it is NOT safe | |
1261 | * on SMP machines, except for its size or flags. | |
1262 | */ | |
b3bdda02 | 1263 | struct vm_struct *remove_vm_area(const void *addr) |
7856dfeb | 1264 | { |
db64fe02 NP |
1265 | struct vmap_area *va; |
1266 | ||
1267 | va = find_vmap_area((unsigned long)addr); | |
1268 | if (va && va->flags & VM_VM_AREA) { | |
1269 | struct vm_struct *vm = va->private; | |
1270 | struct vm_struct *tmp, **p; | |
cd52858c NP |
1271 | |
1272 | vmap_debug_free_range(va->va_start, va->va_end); | |
db64fe02 NP |
1273 | free_unmap_vmap_area(va); |
1274 | vm->size -= PAGE_SIZE; | |
1275 | ||
1276 | write_lock(&vmlist_lock); | |
1277 | for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next) | |
1278 | ; | |
1279 | *p = tmp->next; | |
1280 | write_unlock(&vmlist_lock); | |
1281 | ||
1282 | return vm; | |
1283 | } | |
1284 | return NULL; | |
7856dfeb AK |
1285 | } |
1286 | ||
b3bdda02 | 1287 | static void __vunmap(const void *addr, int deallocate_pages) |
1da177e4 LT |
1288 | { |
1289 | struct vm_struct *area; | |
1290 | ||
1291 | if (!addr) | |
1292 | return; | |
1293 | ||
1294 | if ((PAGE_SIZE-1) & (unsigned long)addr) { | |
4c8573e2 | 1295 | WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr); |
1da177e4 LT |
1296 | return; |
1297 | } | |
1298 | ||
1299 | area = remove_vm_area(addr); | |
1300 | if (unlikely(!area)) { | |
4c8573e2 | 1301 | WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", |
1da177e4 | 1302 | addr); |
1da177e4 LT |
1303 | return; |
1304 | } | |
1305 | ||
9a11b49a | 1306 | debug_check_no_locks_freed(addr, area->size); |
3ac7fe5a | 1307 | debug_check_no_obj_freed(addr, area->size); |
9a11b49a | 1308 | |
1da177e4 LT |
1309 | if (deallocate_pages) { |
1310 | int i; | |
1311 | ||
1312 | for (i = 0; i < area->nr_pages; i++) { | |
bf53d6f8 CL |
1313 | struct page *page = area->pages[i]; |
1314 | ||
1315 | BUG_ON(!page); | |
1316 | __free_page(page); | |
1da177e4 LT |
1317 | } |
1318 | ||
8757d5fa | 1319 | if (area->flags & VM_VPAGES) |
1da177e4 LT |
1320 | vfree(area->pages); |
1321 | else | |
1322 | kfree(area->pages); | |
1323 | } | |
1324 | ||
1325 | kfree(area); | |
1326 | return; | |
1327 | } | |
1328 | ||
1329 | /** | |
1330 | * vfree - release memory allocated by vmalloc() | |
1da177e4 LT |
1331 | * @addr: memory base address |
1332 | * | |
183ff22b | 1333 | * Free the virtually continuous memory area starting at @addr, as |
80e93eff PE |
1334 | * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is |
1335 | * NULL, no operation is performed. | |
1da177e4 | 1336 | * |
80e93eff | 1337 | * Must not be called in interrupt context. |
1da177e4 | 1338 | */ |
b3bdda02 | 1339 | void vfree(const void *addr) |
1da177e4 LT |
1340 | { |
1341 | BUG_ON(in_interrupt()); | |
1342 | __vunmap(addr, 1); | |
1343 | } | |
1da177e4 LT |
1344 | EXPORT_SYMBOL(vfree); |
1345 | ||
1346 | /** | |
1347 | * vunmap - release virtual mapping obtained by vmap() | |
1da177e4 LT |
1348 | * @addr: memory base address |
1349 | * | |
1350 | * Free the virtually contiguous memory area starting at @addr, | |
1351 | * which was created from the page array passed to vmap(). | |
1352 | * | |
80e93eff | 1353 | * Must not be called in interrupt context. |
1da177e4 | 1354 | */ |
b3bdda02 | 1355 | void vunmap(const void *addr) |
1da177e4 LT |
1356 | { |
1357 | BUG_ON(in_interrupt()); | |
34754b69 | 1358 | might_sleep(); |
1da177e4 LT |
1359 | __vunmap(addr, 0); |
1360 | } | |
1da177e4 LT |
1361 | EXPORT_SYMBOL(vunmap); |
1362 | ||
1363 | /** | |
1364 | * vmap - map an array of pages into virtually contiguous space | |
1da177e4 LT |
1365 | * @pages: array of page pointers |
1366 | * @count: number of pages to map | |
1367 | * @flags: vm_area->flags | |
1368 | * @prot: page protection for the mapping | |
1369 | * | |
1370 | * Maps @count pages from @pages into contiguous kernel virtual | |
1371 | * space. | |
1372 | */ | |
1373 | void *vmap(struct page **pages, unsigned int count, | |
1374 | unsigned long flags, pgprot_t prot) | |
1375 | { | |
1376 | struct vm_struct *area; | |
1377 | ||
34754b69 PZ |
1378 | might_sleep(); |
1379 | ||
1da177e4 LT |
1380 | if (count > num_physpages) |
1381 | return NULL; | |
1382 | ||
23016969 CL |
1383 | area = get_vm_area_caller((count << PAGE_SHIFT), flags, |
1384 | __builtin_return_address(0)); | |
1da177e4 LT |
1385 | if (!area) |
1386 | return NULL; | |
23016969 | 1387 | |
1da177e4 LT |
1388 | if (map_vm_area(area, prot, &pages)) { |
1389 | vunmap(area->addr); | |
1390 | return NULL; | |
1391 | } | |
1392 | ||
1393 | return area->addr; | |
1394 | } | |
1da177e4 LT |
1395 | EXPORT_SYMBOL(vmap); |
1396 | ||
db64fe02 NP |
1397 | static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot, |
1398 | int node, void *caller); | |
e31d9eb5 | 1399 | static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, |
23016969 | 1400 | pgprot_t prot, int node, void *caller) |
1da177e4 LT |
1401 | { |
1402 | struct page **pages; | |
1403 | unsigned int nr_pages, array_size, i; | |
1404 | ||
1405 | nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT; | |
1406 | array_size = (nr_pages * sizeof(struct page *)); | |
1407 | ||
1408 | area->nr_pages = nr_pages; | |
1409 | /* Please note that the recursion is strictly bounded. */ | |
8757d5fa | 1410 | if (array_size > PAGE_SIZE) { |
94f6030c | 1411 | pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO, |
23016969 | 1412 | PAGE_KERNEL, node, caller); |
8757d5fa | 1413 | area->flags |= VM_VPAGES; |
286e1ea3 AM |
1414 | } else { |
1415 | pages = kmalloc_node(array_size, | |
6cb06229 | 1416 | (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO, |
286e1ea3 AM |
1417 | node); |
1418 | } | |
1da177e4 | 1419 | area->pages = pages; |
23016969 | 1420 | area->caller = caller; |
1da177e4 LT |
1421 | if (!area->pages) { |
1422 | remove_vm_area(area->addr); | |
1423 | kfree(area); | |
1424 | return NULL; | |
1425 | } | |
1da177e4 LT |
1426 | |
1427 | for (i = 0; i < area->nr_pages; i++) { | |
bf53d6f8 CL |
1428 | struct page *page; |
1429 | ||
930fc45a | 1430 | if (node < 0) |
bf53d6f8 | 1431 | page = alloc_page(gfp_mask); |
930fc45a | 1432 | else |
bf53d6f8 CL |
1433 | page = alloc_pages_node(node, gfp_mask, 0); |
1434 | ||
1435 | if (unlikely(!page)) { | |
1da177e4 LT |
1436 | /* Successfully allocated i pages, free them in __vunmap() */ |
1437 | area->nr_pages = i; | |
1438 | goto fail; | |
1439 | } | |
bf53d6f8 | 1440 | area->pages[i] = page; |
1da177e4 LT |
1441 | } |
1442 | ||
1443 | if (map_vm_area(area, prot, &pages)) | |
1444 | goto fail; | |
1445 | return area->addr; | |
1446 | ||
1447 | fail: | |
1448 | vfree(area->addr); | |
1449 | return NULL; | |
1450 | } | |
1451 | ||
930fc45a CL |
1452 | void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot) |
1453 | { | |
23016969 CL |
1454 | return __vmalloc_area_node(area, gfp_mask, prot, -1, |
1455 | __builtin_return_address(0)); | |
930fc45a CL |
1456 | } |
1457 | ||
1da177e4 | 1458 | /** |
930fc45a | 1459 | * __vmalloc_node - allocate virtually contiguous memory |
1da177e4 LT |
1460 | * @size: allocation size |
1461 | * @gfp_mask: flags for the page level allocator | |
1462 | * @prot: protection mask for the allocated pages | |
d44e0780 | 1463 | * @node: node to use for allocation or -1 |
c85d194b | 1464 | * @caller: caller's return address |
1da177e4 LT |
1465 | * |
1466 | * Allocate enough pages to cover @size from the page level | |
1467 | * allocator with @gfp_mask flags. Map them into contiguous | |
1468 | * kernel virtual space, using a pagetable protection of @prot. | |
1469 | */ | |
b221385b | 1470 | static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot, |
23016969 | 1471 | int node, void *caller) |
1da177e4 LT |
1472 | { |
1473 | struct vm_struct *area; | |
1474 | ||
1475 | size = PAGE_ALIGN(size); | |
1476 | if (!size || (size >> PAGE_SHIFT) > num_physpages) | |
1477 | return NULL; | |
1478 | ||
23016969 CL |
1479 | area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END, |
1480 | node, gfp_mask, caller); | |
1481 | ||
1da177e4 LT |
1482 | if (!area) |
1483 | return NULL; | |
1484 | ||
23016969 | 1485 | return __vmalloc_area_node(area, gfp_mask, prot, node, caller); |
1da177e4 LT |
1486 | } |
1487 | ||
930fc45a CL |
1488 | void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) |
1489 | { | |
23016969 CL |
1490 | return __vmalloc_node(size, gfp_mask, prot, -1, |
1491 | __builtin_return_address(0)); | |
930fc45a | 1492 | } |
1da177e4 LT |
1493 | EXPORT_SYMBOL(__vmalloc); |
1494 | ||
1495 | /** | |
1496 | * vmalloc - allocate virtually contiguous memory | |
1da177e4 | 1497 | * @size: allocation size |
1da177e4 LT |
1498 | * Allocate enough pages to cover @size from the page level |
1499 | * allocator and map them into contiguous kernel virtual space. | |
1500 | * | |
c1c8897f | 1501 | * For tight control over page level allocator and protection flags |
1da177e4 LT |
1502 | * use __vmalloc() instead. |
1503 | */ | |
1504 | void *vmalloc(unsigned long size) | |
1505 | { | |
23016969 CL |
1506 | return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL, |
1507 | -1, __builtin_return_address(0)); | |
1da177e4 | 1508 | } |
1da177e4 LT |
1509 | EXPORT_SYMBOL(vmalloc); |
1510 | ||
83342314 | 1511 | /** |
ead04089 REB |
1512 | * vmalloc_user - allocate zeroed virtually contiguous memory for userspace |
1513 | * @size: allocation size | |
83342314 | 1514 | * |
ead04089 REB |
1515 | * The resulting memory area is zeroed so it can be mapped to userspace |
1516 | * without leaking data. | |
83342314 NP |
1517 | */ |
1518 | void *vmalloc_user(unsigned long size) | |
1519 | { | |
1520 | struct vm_struct *area; | |
1521 | void *ret; | |
1522 | ||
84877848 GC |
1523 | ret = __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, |
1524 | PAGE_KERNEL, -1, __builtin_return_address(0)); | |
2b4ac44e | 1525 | if (ret) { |
db64fe02 | 1526 | area = find_vm_area(ret); |
2b4ac44e | 1527 | area->flags |= VM_USERMAP; |
2b4ac44e | 1528 | } |
83342314 NP |
1529 | return ret; |
1530 | } | |
1531 | EXPORT_SYMBOL(vmalloc_user); | |
1532 | ||
930fc45a CL |
1533 | /** |
1534 | * vmalloc_node - allocate memory on a specific node | |
930fc45a | 1535 | * @size: allocation size |
d44e0780 | 1536 | * @node: numa node |
930fc45a CL |
1537 | * |
1538 | * Allocate enough pages to cover @size from the page level | |
1539 | * allocator and map them into contiguous kernel virtual space. | |
1540 | * | |
c1c8897f | 1541 | * For tight control over page level allocator and protection flags |
930fc45a CL |
1542 | * use __vmalloc() instead. |
1543 | */ | |
1544 | void *vmalloc_node(unsigned long size, int node) | |
1545 | { | |
23016969 CL |
1546 | return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL, |
1547 | node, __builtin_return_address(0)); | |
930fc45a CL |
1548 | } |
1549 | EXPORT_SYMBOL(vmalloc_node); | |
1550 | ||
4dc3b16b PP |
1551 | #ifndef PAGE_KERNEL_EXEC |
1552 | # define PAGE_KERNEL_EXEC PAGE_KERNEL | |
1553 | #endif | |
1554 | ||
1da177e4 LT |
1555 | /** |
1556 | * vmalloc_exec - allocate virtually contiguous, executable memory | |
1da177e4 LT |
1557 | * @size: allocation size |
1558 | * | |
1559 | * Kernel-internal function to allocate enough pages to cover @size | |
1560 | * the page level allocator and map them into contiguous and | |
1561 | * executable kernel virtual space. | |
1562 | * | |
c1c8897f | 1563 | * For tight control over page level allocator and protection flags |
1da177e4 LT |
1564 | * use __vmalloc() instead. |
1565 | */ | |
1566 | ||
1da177e4 LT |
1567 | void *vmalloc_exec(unsigned long size) |
1568 | { | |
84877848 GC |
1569 | return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC, |
1570 | -1, __builtin_return_address(0)); | |
1da177e4 LT |
1571 | } |
1572 | ||
0d08e0d3 | 1573 | #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) |
7ac674f5 | 1574 | #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL |
0d08e0d3 | 1575 | #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) |
7ac674f5 | 1576 | #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL |
0d08e0d3 AK |
1577 | #else |
1578 | #define GFP_VMALLOC32 GFP_KERNEL | |
1579 | #endif | |
1580 | ||
1da177e4 LT |
1581 | /** |
1582 | * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) | |
1da177e4 LT |
1583 | * @size: allocation size |
1584 | * | |
1585 | * Allocate enough 32bit PA addressable pages to cover @size from the | |
1586 | * page level allocator and map them into contiguous kernel virtual space. | |
1587 | */ | |
1588 | void *vmalloc_32(unsigned long size) | |
1589 | { | |
84877848 GC |
1590 | return __vmalloc_node(size, GFP_VMALLOC32, PAGE_KERNEL, |
1591 | -1, __builtin_return_address(0)); | |
1da177e4 | 1592 | } |
1da177e4 LT |
1593 | EXPORT_SYMBOL(vmalloc_32); |
1594 | ||
83342314 | 1595 | /** |
ead04089 | 1596 | * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory |
83342314 | 1597 | * @size: allocation size |
ead04089 REB |
1598 | * |
1599 | * The resulting memory area is 32bit addressable and zeroed so it can be | |
1600 | * mapped to userspace without leaking data. | |
83342314 NP |
1601 | */ |
1602 | void *vmalloc_32_user(unsigned long size) | |
1603 | { | |
1604 | struct vm_struct *area; | |
1605 | void *ret; | |
1606 | ||
84877848 GC |
1607 | ret = __vmalloc_node(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, |
1608 | -1, __builtin_return_address(0)); | |
2b4ac44e | 1609 | if (ret) { |
db64fe02 | 1610 | area = find_vm_area(ret); |
2b4ac44e | 1611 | area->flags |= VM_USERMAP; |
2b4ac44e | 1612 | } |
83342314 NP |
1613 | return ret; |
1614 | } | |
1615 | EXPORT_SYMBOL(vmalloc_32_user); | |
1616 | ||
1da177e4 LT |
1617 | long vread(char *buf, char *addr, unsigned long count) |
1618 | { | |
1619 | struct vm_struct *tmp; | |
1620 | char *vaddr, *buf_start = buf; | |
1621 | unsigned long n; | |
1622 | ||
1623 | /* Don't allow overflow */ | |
1624 | if ((unsigned long) addr + count < count) | |
1625 | count = -(unsigned long) addr; | |
1626 | ||
1627 | read_lock(&vmlist_lock); | |
1628 | for (tmp = vmlist; tmp; tmp = tmp->next) { | |
1629 | vaddr = (char *) tmp->addr; | |
1630 | if (addr >= vaddr + tmp->size - PAGE_SIZE) | |
1631 | continue; | |
1632 | while (addr < vaddr) { | |
1633 | if (count == 0) | |
1634 | goto finished; | |
1635 | *buf = '\0'; | |
1636 | buf++; | |
1637 | addr++; | |
1638 | count--; | |
1639 | } | |
1640 | n = vaddr + tmp->size - PAGE_SIZE - addr; | |
1641 | do { | |
1642 | if (count == 0) | |
1643 | goto finished; | |
1644 | *buf = *addr; | |
1645 | buf++; | |
1646 | addr++; | |
1647 | count--; | |
1648 | } while (--n > 0); | |
1649 | } | |
1650 | finished: | |
1651 | read_unlock(&vmlist_lock); | |
1652 | return buf - buf_start; | |
1653 | } | |
1654 | ||
1655 | long vwrite(char *buf, char *addr, unsigned long count) | |
1656 | { | |
1657 | struct vm_struct *tmp; | |
1658 | char *vaddr, *buf_start = buf; | |
1659 | unsigned long n; | |
1660 | ||
1661 | /* Don't allow overflow */ | |
1662 | if ((unsigned long) addr + count < count) | |
1663 | count = -(unsigned long) addr; | |
1664 | ||
1665 | read_lock(&vmlist_lock); | |
1666 | for (tmp = vmlist; tmp; tmp = tmp->next) { | |
1667 | vaddr = (char *) tmp->addr; | |
1668 | if (addr >= vaddr + tmp->size - PAGE_SIZE) | |
1669 | continue; | |
1670 | while (addr < vaddr) { | |
1671 | if (count == 0) | |
1672 | goto finished; | |
1673 | buf++; | |
1674 | addr++; | |
1675 | count--; | |
1676 | } | |
1677 | n = vaddr + tmp->size - PAGE_SIZE - addr; | |
1678 | do { | |
1679 | if (count == 0) | |
1680 | goto finished; | |
1681 | *addr = *buf; | |
1682 | buf++; | |
1683 | addr++; | |
1684 | count--; | |
1685 | } while (--n > 0); | |
1686 | } | |
1687 | finished: | |
1688 | read_unlock(&vmlist_lock); | |
1689 | return buf - buf_start; | |
1690 | } | |
83342314 NP |
1691 | |
1692 | /** | |
1693 | * remap_vmalloc_range - map vmalloc pages to userspace | |
83342314 NP |
1694 | * @vma: vma to cover (map full range of vma) |
1695 | * @addr: vmalloc memory | |
1696 | * @pgoff: number of pages into addr before first page to map | |
7682486b RD |
1697 | * |
1698 | * Returns: 0 for success, -Exxx on failure | |
83342314 NP |
1699 | * |
1700 | * This function checks that addr is a valid vmalloc'ed area, and | |
1701 | * that it is big enough to cover the vma. Will return failure if | |
1702 | * that criteria isn't met. | |
1703 | * | |
72fd4a35 | 1704 | * Similar to remap_pfn_range() (see mm/memory.c) |
83342314 NP |
1705 | */ |
1706 | int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, | |
1707 | unsigned long pgoff) | |
1708 | { | |
1709 | struct vm_struct *area; | |
1710 | unsigned long uaddr = vma->vm_start; | |
1711 | unsigned long usize = vma->vm_end - vma->vm_start; | |
83342314 NP |
1712 | |
1713 | if ((PAGE_SIZE-1) & (unsigned long)addr) | |
1714 | return -EINVAL; | |
1715 | ||
db64fe02 | 1716 | area = find_vm_area(addr); |
83342314 | 1717 | if (!area) |
db64fe02 | 1718 | return -EINVAL; |
83342314 NP |
1719 | |
1720 | if (!(area->flags & VM_USERMAP)) | |
db64fe02 | 1721 | return -EINVAL; |
83342314 NP |
1722 | |
1723 | if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE) | |
db64fe02 | 1724 | return -EINVAL; |
83342314 NP |
1725 | |
1726 | addr += pgoff << PAGE_SHIFT; | |
1727 | do { | |
1728 | struct page *page = vmalloc_to_page(addr); | |
db64fe02 NP |
1729 | int ret; |
1730 | ||
83342314 NP |
1731 | ret = vm_insert_page(vma, uaddr, page); |
1732 | if (ret) | |
1733 | return ret; | |
1734 | ||
1735 | uaddr += PAGE_SIZE; | |
1736 | addr += PAGE_SIZE; | |
1737 | usize -= PAGE_SIZE; | |
1738 | } while (usize > 0); | |
1739 | ||
1740 | /* Prevent "things" like memory migration? VM_flags need a cleanup... */ | |
1741 | vma->vm_flags |= VM_RESERVED; | |
1742 | ||
db64fe02 | 1743 | return 0; |
83342314 NP |
1744 | } |
1745 | EXPORT_SYMBOL(remap_vmalloc_range); | |
1746 | ||
1eeb66a1 CH |
1747 | /* |
1748 | * Implement a stub for vmalloc_sync_all() if the architecture chose not to | |
1749 | * have one. | |
1750 | */ | |
1751 | void __attribute__((weak)) vmalloc_sync_all(void) | |
1752 | { | |
1753 | } | |
5f4352fb JF |
1754 | |
1755 | ||
2f569afd | 1756 | static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data) |
5f4352fb JF |
1757 | { |
1758 | /* apply_to_page_range() does all the hard work. */ | |
1759 | return 0; | |
1760 | } | |
1761 | ||
1762 | /** | |
1763 | * alloc_vm_area - allocate a range of kernel address space | |
1764 | * @size: size of the area | |
7682486b RD |
1765 | * |
1766 | * Returns: NULL on failure, vm_struct on success | |
5f4352fb JF |
1767 | * |
1768 | * This function reserves a range of kernel address space, and | |
1769 | * allocates pagetables to map that range. No actual mappings | |
1770 | * are created. If the kernel address space is not shared | |
1771 | * between processes, it syncs the pagetable across all | |
1772 | * processes. | |
1773 | */ | |
1774 | struct vm_struct *alloc_vm_area(size_t size) | |
1775 | { | |
1776 | struct vm_struct *area; | |
1777 | ||
23016969 CL |
1778 | area = get_vm_area_caller(size, VM_IOREMAP, |
1779 | __builtin_return_address(0)); | |
5f4352fb JF |
1780 | if (area == NULL) |
1781 | return NULL; | |
1782 | ||
1783 | /* | |
1784 | * This ensures that page tables are constructed for this region | |
1785 | * of kernel virtual address space and mapped into init_mm. | |
1786 | */ | |
1787 | if (apply_to_page_range(&init_mm, (unsigned long)area->addr, | |
1788 | area->size, f, NULL)) { | |
1789 | free_vm_area(area); | |
1790 | return NULL; | |
1791 | } | |
1792 | ||
1793 | /* Make sure the pagetables are constructed in process kernel | |
1794 | mappings */ | |
1795 | vmalloc_sync_all(); | |
1796 | ||
1797 | return area; | |
1798 | } | |
1799 | EXPORT_SYMBOL_GPL(alloc_vm_area); | |
1800 | ||
1801 | void free_vm_area(struct vm_struct *area) | |
1802 | { | |
1803 | struct vm_struct *ret; | |
1804 | ret = remove_vm_area(area->addr); | |
1805 | BUG_ON(ret != area); | |
1806 | kfree(area); | |
1807 | } | |
1808 | EXPORT_SYMBOL_GPL(free_vm_area); | |
a10aa579 CL |
1809 | |
1810 | ||
1811 | #ifdef CONFIG_PROC_FS | |
1812 | static void *s_start(struct seq_file *m, loff_t *pos) | |
1813 | { | |
1814 | loff_t n = *pos; | |
1815 | struct vm_struct *v; | |
1816 | ||
1817 | read_lock(&vmlist_lock); | |
1818 | v = vmlist; | |
1819 | while (n > 0 && v) { | |
1820 | n--; | |
1821 | v = v->next; | |
1822 | } | |
1823 | if (!n) | |
1824 | return v; | |
1825 | ||
1826 | return NULL; | |
1827 | ||
1828 | } | |
1829 | ||
1830 | static void *s_next(struct seq_file *m, void *p, loff_t *pos) | |
1831 | { | |
1832 | struct vm_struct *v = p; | |
1833 | ||
1834 | ++*pos; | |
1835 | return v->next; | |
1836 | } | |
1837 | ||
1838 | static void s_stop(struct seq_file *m, void *p) | |
1839 | { | |
1840 | read_unlock(&vmlist_lock); | |
1841 | } | |
1842 | ||
a47a126a ED |
1843 | static void show_numa_info(struct seq_file *m, struct vm_struct *v) |
1844 | { | |
1845 | if (NUMA_BUILD) { | |
1846 | unsigned int nr, *counters = m->private; | |
1847 | ||
1848 | if (!counters) | |
1849 | return; | |
1850 | ||
1851 | memset(counters, 0, nr_node_ids * sizeof(unsigned int)); | |
1852 | ||
1853 | for (nr = 0; nr < v->nr_pages; nr++) | |
1854 | counters[page_to_nid(v->pages[nr])]++; | |
1855 | ||
1856 | for_each_node_state(nr, N_HIGH_MEMORY) | |
1857 | if (counters[nr]) | |
1858 | seq_printf(m, " N%u=%u", nr, counters[nr]); | |
1859 | } | |
1860 | } | |
1861 | ||
a10aa579 CL |
1862 | static int s_show(struct seq_file *m, void *p) |
1863 | { | |
1864 | struct vm_struct *v = p; | |
1865 | ||
1866 | seq_printf(m, "0x%p-0x%p %7ld", | |
1867 | v->addr, v->addr + v->size, v->size); | |
1868 | ||
23016969 | 1869 | if (v->caller) { |
9c246247 | 1870 | char buff[KSYM_SYMBOL_LEN]; |
23016969 CL |
1871 | |
1872 | seq_putc(m, ' '); | |
1873 | sprint_symbol(buff, (unsigned long)v->caller); | |
1874 | seq_puts(m, buff); | |
1875 | } | |
1876 | ||
a10aa579 CL |
1877 | if (v->nr_pages) |
1878 | seq_printf(m, " pages=%d", v->nr_pages); | |
1879 | ||
1880 | if (v->phys_addr) | |
1881 | seq_printf(m, " phys=%lx", v->phys_addr); | |
1882 | ||
1883 | if (v->flags & VM_IOREMAP) | |
1884 | seq_printf(m, " ioremap"); | |
1885 | ||
1886 | if (v->flags & VM_ALLOC) | |
1887 | seq_printf(m, " vmalloc"); | |
1888 | ||
1889 | if (v->flags & VM_MAP) | |
1890 | seq_printf(m, " vmap"); | |
1891 | ||
1892 | if (v->flags & VM_USERMAP) | |
1893 | seq_printf(m, " user"); | |
1894 | ||
1895 | if (v->flags & VM_VPAGES) | |
1896 | seq_printf(m, " vpages"); | |
1897 | ||
a47a126a | 1898 | show_numa_info(m, v); |
a10aa579 CL |
1899 | seq_putc(m, '\n'); |
1900 | return 0; | |
1901 | } | |
1902 | ||
5f6a6a9c | 1903 | static const struct seq_operations vmalloc_op = { |
a10aa579 CL |
1904 | .start = s_start, |
1905 | .next = s_next, | |
1906 | .stop = s_stop, | |
1907 | .show = s_show, | |
1908 | }; | |
5f6a6a9c AD |
1909 | |
1910 | static int vmalloc_open(struct inode *inode, struct file *file) | |
1911 | { | |
1912 | unsigned int *ptr = NULL; | |
1913 | int ret; | |
1914 | ||
1915 | if (NUMA_BUILD) | |
1916 | ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL); | |
1917 | ret = seq_open(file, &vmalloc_op); | |
1918 | if (!ret) { | |
1919 | struct seq_file *m = file->private_data; | |
1920 | m->private = ptr; | |
1921 | } else | |
1922 | kfree(ptr); | |
1923 | return ret; | |
1924 | } | |
1925 | ||
1926 | static const struct file_operations proc_vmalloc_operations = { | |
1927 | .open = vmalloc_open, | |
1928 | .read = seq_read, | |
1929 | .llseek = seq_lseek, | |
1930 | .release = seq_release_private, | |
1931 | }; | |
1932 | ||
1933 | static int __init proc_vmalloc_init(void) | |
1934 | { | |
1935 | proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations); | |
1936 | return 0; | |
1937 | } | |
1938 | module_init(proc_vmalloc_init); | |
a10aa579 CL |
1939 | #endif |
1940 |