blob: 204b8243d8abb11cb9abd2f37dd8864ad0a346aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Christoph Lameter930fc452005-10-29 18:15:41 -07008 * Numa awareness, Christoph Lameter, SGI, June 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Nick Piggindb64fe02008-10-18 20:27:03 -070011#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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>
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +040018#include <linux/proc_fs.h>
Christoph Lametera10aa572008-04-28 02:12:40 -070019#include <linux/seq_file.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070020#include <linux/debugobjects.h>
Christoph Lameter23016962008-04-28 02:12:42 -070021#include <linux/kallsyms.h>
Nick Piggindb64fe02008-10-18 20:27:03 -070022#include <linux/list.h>
23#include <linux/rbtree.h>
24#include <linux/radix-tree.h>
25#include <linux/rcupdate.h>
Tejun Heof0aa6612009-02-20 16:29:08 +090026#include <linux/pfn.h>
Catalin Marinas89219d32009-06-11 13:23:19 +010027#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Nick Piggindb64fe02008-10-18 20:27:03 -070029#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
31#include <asm/tlbflush.h>
32
33
Nick Piggindb64fe02008-10-18 20:27:03 -070034/*** Page table manipulation functions ***/
Adrian Bunkb2213852006-09-25 23:31:02 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static 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
Nick Piggindb64fe02008-10-18 20:27:03 -070047static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
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
Nick Piggindb64fe02008-10-18 20:27:03 -070061static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
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
Nick Piggindb64fe02008-10-18 20:27:03 -070075static void vunmap_page_range(unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 pgd_t *pgd;
78 unsigned long next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 BUG_ON(addr >= end);
81 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
Nick Piggindb64fe02008-10-18 20:27:03 -070091 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 pte_t *pte;
94
Nick Piggindb64fe02008-10-18 20:27:03 -070095 /*
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
Hugh Dickins872fec12005-10-29 18:16:21 -0700100 pte = pte_alloc_kernel(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (!pte)
102 return -ENOMEM;
103 do {
Nick Piggindb64fe02008-10-18 20:27:03 -0700104 struct page *page = pages[*nr];
105
106 if (WARN_ON(!pte_none(*pte)))
107 return -EBUSY;
108 if (WARN_ON(!page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -ENOMEM;
110 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
Nick Piggindb64fe02008-10-18 20:27:03 -0700111 (*nr)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 } while (pte++, addr += PAGE_SIZE, addr != end);
113 return 0;
114}
115
Nick Piggindb64fe02008-10-18 20:27:03 -0700116static int vmap_pmd_range(pud_t *pud, unsigned long addr,
117 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
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);
Nick Piggindb64fe02008-10-18 20:27:03 -0700127 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return -ENOMEM;
129 } while (pmd++, addr = next, addr != end);
130 return 0;
131}
132
Nick Piggindb64fe02008-10-18 20:27:03 -0700133static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
134 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
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);
Nick Piggindb64fe02008-10-18 20:27:03 -0700144 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -ENOMEM;
146 } while (pud++, addr = next, addr != end);
147 return 0;
148}
149
Nick Piggindb64fe02008-10-18 20:27:03 -0700150/*
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 */
Tejun Heo8fc48982009-02-20 16:29:08 +0900156static int vmap_page_range_noflush(unsigned long start, unsigned long end,
157 pgprot_t prot, struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 pgd_t *pgd;
160 unsigned long next;
Adam Lackorzynski2e4e27c2009-01-04 12:00:46 -0800161 unsigned long addr = start;
Nick Piggindb64fe02008-10-18 20:27:03 -0700162 int err = 0;
163 int nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 BUG_ON(addr >= end);
166 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 do {
168 next = pgd_addr_end(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700169 err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (err)
171 break;
172 } while (pgd++, addr = next, addr != end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700173
174 if (unlikely(err))
175 return err;
176 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Tejun Heo8fc48982009-02-20 16:29:08 +0900179static 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
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700189static inline int is_vmalloc_or_module_addr(const void *x)
190{
191 /*
Russell Kingab4f2ee2008-11-06 17:11:07 +0000192 * ARM, x86-64 and sparc64 put modules in a special place,
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700193 * 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
Christoph Lameter48667e72008-02-04 22:28:31 -0800204/*
Nick Piggindb64fe02008-10-18 20:27:03 -0700205 * Walk a vmap address to the struct page it maps.
Christoph Lameter48667e72008-02-04 22:28:31 -0800206 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800207struct page *vmalloc_to_page(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800208{
209 unsigned long addr = (unsigned long) vmalloc_addr;
210 struct page *page = NULL;
211 pgd_t *pgd = pgd_offset_k(addr);
Christoph Lameter48667e72008-02-04 22:28:31 -0800212
Ingo Molnar7aa413d2008-06-19 13:28:11 +0200213 /*
214 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
215 * architectures that do not vmalloc module space
216 */
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700217 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
Jiri Slaby59ea7462008-06-12 13:56:40 +0200218
Christoph Lameter48667e72008-02-04 22:28:31 -0800219 if (!pgd_none(*pgd)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700220 pud_t *pud = pud_offset(pgd, addr);
Christoph Lameter48667e72008-02-04 22:28:31 -0800221 if (!pud_none(*pud)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700222 pmd_t *pmd = pmd_offset(pud, addr);
Christoph Lameter48667e72008-02-04 22:28:31 -0800223 if (!pmd_none(*pmd)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700224 pte_t *ptep, pte;
225
Christoph Lameter48667e72008-02-04 22:28:31 -0800226 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}
236EXPORT_SYMBOL(vmalloc_to_page);
237
238/*
239 * Map a vmalloc()-space virtual address to the physical page frame number.
240 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800241unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800242{
243 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
244}
245EXPORT_SYMBOL(vmalloc_to_pfn);
246
Nick Piggindb64fe02008-10-18 20:27:03 -0700247
248/*** Global kva allocator ***/
249
250#define VM_LAZY_FREE 0x01
251#define VM_LAZY_FREEING 0x02
252#define VM_VM_AREA 0x04
253
254struct 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
265static DEFINE_SPINLOCK(vmap_area_lock);
266static struct rb_root vmap_area_root = RB_ROOT;
267static LIST_HEAD(vmap_area_list);
Tejun Heoca23e402009-08-14 15:00:52 +0900268static unsigned long vmap_area_pcpu_hole;
Nick Piggindb64fe02008-10-18 20:27:03 -0700269
270static struct vmap_area *__find_vmap_area(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Nick Piggindb64fe02008-10-18 20:27:03 -0700272 struct rb_node *n = vmap_area_root.rb_node;
273
274 while (n) {
275 struct vmap_area *va;
276
277 va = rb_entry(n, struct vmap_area, rb_node);
278 if (addr < va->va_start)
279 n = n->rb_left;
280 else if (addr > va->va_start)
281 n = n->rb_right;
282 else
283 return va;
284 }
285
286 return NULL;
287}
288
289static void __insert_vmap_area(struct vmap_area *va)
290{
291 struct rb_node **p = &vmap_area_root.rb_node;
292 struct rb_node *parent = NULL;
293 struct rb_node *tmp;
294
295 while (*p) {
296 struct vmap_area *tmp;
297
298 parent = *p;
299 tmp = rb_entry(parent, struct vmap_area, rb_node);
300 if (va->va_start < tmp->va_end)
301 p = &(*p)->rb_left;
302 else if (va->va_end > tmp->va_start)
303 p = &(*p)->rb_right;
304 else
305 BUG();
306 }
307
308 rb_link_node(&va->rb_node, parent, p);
309 rb_insert_color(&va->rb_node, &vmap_area_root);
310
311 /* address-sort this list so it is usable like the vmlist */
312 tmp = rb_prev(&va->rb_node);
313 if (tmp) {
314 struct vmap_area *prev;
315 prev = rb_entry(tmp, struct vmap_area, rb_node);
316 list_add_rcu(&va->list, &prev->list);
317 } else
318 list_add_rcu(&va->list, &vmap_area_list);
319}
320
321static void purge_vmap_area_lazy(void);
322
323/*
324 * Allocate a region of KVA of the specified size and alignment, within the
325 * vstart and vend.
326 */
327static struct vmap_area *alloc_vmap_area(unsigned long size,
328 unsigned long align,
329 unsigned long vstart, unsigned long vend,
330 int node, gfp_t gfp_mask)
331{
332 struct vmap_area *va;
333 struct rb_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 unsigned long addr;
Nick Piggindb64fe02008-10-18 20:27:03 -0700335 int purged = 0;
336
Nick Piggin77669702009-02-27 14:03:03 -0800337 BUG_ON(!size);
Nick Piggindb64fe02008-10-18 20:27:03 -0700338 BUG_ON(size & ~PAGE_MASK);
339
Nick Piggindb64fe02008-10-18 20:27:03 -0700340 va = kmalloc_node(sizeof(struct vmap_area),
341 gfp_mask & GFP_RECLAIM_MASK, node);
342 if (unlikely(!va))
343 return ERR_PTR(-ENOMEM);
344
345retry:
Glauber Costa0ae15132008-11-19 15:36:33 -0800346 addr = ALIGN(vstart, align);
347
Nick Piggindb64fe02008-10-18 20:27:03 -0700348 spin_lock(&vmap_area_lock);
Nick Piggin77669702009-02-27 14:03:03 -0800349 if (addr + size - 1 < addr)
350 goto overflow;
351
Nick Piggindb64fe02008-10-18 20:27:03 -0700352 /* XXX: could have a last_hole cache */
353 n = vmap_area_root.rb_node;
354 if (n) {
355 struct vmap_area *first = NULL;
356
357 do {
358 struct vmap_area *tmp;
359 tmp = rb_entry(n, struct vmap_area, rb_node);
360 if (tmp->va_end >= addr) {
361 if (!first && tmp->va_start < addr + size)
362 first = tmp;
363 n = n->rb_left;
364 } else {
365 first = tmp;
366 n = n->rb_right;
367 }
368 } while (n);
369
370 if (!first)
371 goto found;
372
373 if (first->va_end < addr) {
374 n = rb_next(&first->rb_node);
375 if (n)
376 first = rb_entry(n, struct vmap_area, rb_node);
377 else
378 goto found;
379 }
380
Nick Pigginf011c2d2008-11-19 15:36:32 -0800381 while (addr + size > first->va_start && addr + size <= vend) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700382 addr = ALIGN(first->va_end + PAGE_SIZE, align);
Nick Piggin77669702009-02-27 14:03:03 -0800383 if (addr + size - 1 < addr)
384 goto overflow;
Nick Piggindb64fe02008-10-18 20:27:03 -0700385
386 n = rb_next(&first->rb_node);
387 if (n)
388 first = rb_entry(n, struct vmap_area, rb_node);
389 else
390 goto found;
391 }
392 }
393found:
394 if (addr + size > vend) {
Nick Piggin77669702009-02-27 14:03:03 -0800395overflow:
Nick Piggindb64fe02008-10-18 20:27:03 -0700396 spin_unlock(&vmap_area_lock);
397 if (!purged) {
398 purge_vmap_area_lazy();
399 purged = 1;
400 goto retry;
401 }
402 if (printk_ratelimit())
Glauber Costac1279c42009-01-06 14:39:18 -0800403 printk(KERN_WARNING
404 "vmap allocation for size %lu failed: "
405 "use vmalloc=<size> to increase size.\n", size);
Ralph Wuerthner2498ce42009-05-06 16:02:59 -0700406 kfree(va);
Nick Piggindb64fe02008-10-18 20:27:03 -0700407 return ERR_PTR(-EBUSY);
408 }
409
410 BUG_ON(addr & (align-1));
411
412 va->va_start = addr;
413 va->va_end = addr + size;
414 va->flags = 0;
415 __insert_vmap_area(va);
416 spin_unlock(&vmap_area_lock);
417
418 return va;
419}
420
421static void rcu_free_va(struct rcu_head *head)
422{
423 struct vmap_area *va = container_of(head, struct vmap_area, rcu_head);
424
425 kfree(va);
426}
427
428static void __free_vmap_area(struct vmap_area *va)
429{
430 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
431 rb_erase(&va->rb_node, &vmap_area_root);
432 RB_CLEAR_NODE(&va->rb_node);
433 list_del_rcu(&va->list);
434
Tejun Heoca23e402009-08-14 15:00:52 +0900435 /*
436 * Track the highest possible candidate for pcpu area
437 * allocation. Areas outside of vmalloc area can be returned
438 * here too, consider only end addresses which fall inside
439 * vmalloc area proper.
440 */
441 if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
442 vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
443
Nick Piggindb64fe02008-10-18 20:27:03 -0700444 call_rcu(&va->rcu_head, rcu_free_va);
445}
446
447/*
448 * Free a region of KVA allocated by alloc_vmap_area
449 */
450static void free_vmap_area(struct vmap_area *va)
451{
452 spin_lock(&vmap_area_lock);
453 __free_vmap_area(va);
454 spin_unlock(&vmap_area_lock);
455}
456
457/*
458 * Clear the pagetable entries of a given vmap_area
459 */
460static void unmap_vmap_area(struct vmap_area *va)
461{
462 vunmap_page_range(va->va_start, va->va_end);
463}
464
Nick Piggincd528582009-01-06 14:39:20 -0800465static void vmap_debug_free_range(unsigned long start, unsigned long end)
466{
467 /*
468 * Unmap page tables and force a TLB flush immediately if
469 * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
470 * bugs similarly to those in linear kernel virtual address
471 * space after a page has been freed.
472 *
473 * All the lazy freeing logic is still retained, in order to
474 * minimise intrusiveness of this debugging feature.
475 *
476 * This is going to be *slow* (linear kernel virtual address
477 * debugging doesn't do a broadcast TLB flush so it is a lot
478 * faster).
479 */
480#ifdef CONFIG_DEBUG_PAGEALLOC
481 vunmap_page_range(start, end);
482 flush_tlb_kernel_range(start, end);
483#endif
484}
485
Nick Piggindb64fe02008-10-18 20:27:03 -0700486/*
487 * lazy_max_pages is the maximum amount of virtual address space we gather up
488 * before attempting to purge with a TLB flush.
489 *
490 * There is a tradeoff here: a larger number will cover more kernel page tables
491 * and take slightly longer to purge, but it will linearly reduce the number of
492 * global TLB flushes that must be performed. It would seem natural to scale
493 * this number up linearly with the number of CPUs (because vmapping activity
494 * could also scale linearly with the number of CPUs), however it is likely
495 * that in practice, workloads might be constrained in other ways that mean
496 * vmap activity will not scale linearly with CPUs. Also, I want to be
497 * conservative and not introduce a big latency on huge systems, so go with
498 * a less aggressive log scale. It will still be an improvement over the old
499 * code, and it will be simple to change the scale factor if we find that it
500 * becomes a problem on bigger systems.
501 */
502static unsigned long lazy_max_pages(void)
503{
504 unsigned int log;
505
506 log = fls(num_online_cpus());
507
508 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
509}
510
511static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
512
513/*
514 * Purges all lazily-freed vmap areas.
515 *
516 * If sync is 0 then don't purge if there is already a purge in progress.
517 * If force_flush is 1, then flush kernel TLBs between *start and *end even
518 * if we found no lazy vmap areas to unmap (callers can use this to optimise
519 * their own TLB flushing).
520 * Returns with *start = min(*start, lowest purged address)
521 * *end = max(*end, highest purged address)
522 */
523static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
524 int sync, int force_flush)
525{
Andrew Morton46666d82009-01-15 13:51:15 -0800526 static DEFINE_SPINLOCK(purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700527 LIST_HEAD(valist);
528 struct vmap_area *va;
Vegard Nossumcbb76672009-02-27 14:03:04 -0800529 struct vmap_area *n_va;
Nick Piggindb64fe02008-10-18 20:27:03 -0700530 int nr = 0;
531
532 /*
533 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
534 * should not expect such behaviour. This just simplifies locking for
535 * the case that isn't actually used at the moment anyway.
536 */
537 if (!sync && !force_flush) {
Andrew Morton46666d82009-01-15 13:51:15 -0800538 if (!spin_trylock(&purge_lock))
Nick Piggindb64fe02008-10-18 20:27:03 -0700539 return;
540 } else
Andrew Morton46666d82009-01-15 13:51:15 -0800541 spin_lock(&purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700542
543 rcu_read_lock();
544 list_for_each_entry_rcu(va, &vmap_area_list, list) {
545 if (va->flags & VM_LAZY_FREE) {
546 if (va->va_start < *start)
547 *start = va->va_start;
548 if (va->va_end > *end)
549 *end = va->va_end;
550 nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
551 unmap_vmap_area(va);
552 list_add_tail(&va->purge_list, &valist);
553 va->flags |= VM_LAZY_FREEING;
554 va->flags &= ~VM_LAZY_FREE;
555 }
556 }
557 rcu_read_unlock();
558
559 if (nr) {
560 BUG_ON(nr > atomic_read(&vmap_lazy_nr));
561 atomic_sub(nr, &vmap_lazy_nr);
562 }
563
564 if (nr || force_flush)
565 flush_tlb_kernel_range(*start, *end);
566
567 if (nr) {
568 spin_lock(&vmap_area_lock);
Vegard Nossumcbb76672009-02-27 14:03:04 -0800569 list_for_each_entry_safe(va, n_va, &valist, purge_list)
Nick Piggindb64fe02008-10-18 20:27:03 -0700570 __free_vmap_area(va);
571 spin_unlock(&vmap_area_lock);
572 }
Andrew Morton46666d82009-01-15 13:51:15 -0800573 spin_unlock(&purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700574}
575
576/*
Nick Piggin496850e2008-11-19 15:36:33 -0800577 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
578 * is already purging.
579 */
580static void try_purge_vmap_area_lazy(void)
581{
582 unsigned long start = ULONG_MAX, end = 0;
583
584 __purge_vmap_area_lazy(&start, &end, 0, 0);
585}
586
587/*
Nick Piggindb64fe02008-10-18 20:27:03 -0700588 * Kick off a purge of the outstanding lazy areas.
589 */
590static void purge_vmap_area_lazy(void)
591{
592 unsigned long start = ULONG_MAX, end = 0;
593
Nick Piggin496850e2008-11-19 15:36:33 -0800594 __purge_vmap_area_lazy(&start, &end, 1, 0);
Nick Piggindb64fe02008-10-18 20:27:03 -0700595}
596
597/*
Nick Pigginb29acbd2008-12-01 13:13:47 -0800598 * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
599 * called for the correct range previously.
Nick Piggindb64fe02008-10-18 20:27:03 -0700600 */
Nick Pigginb29acbd2008-12-01 13:13:47 -0800601static void free_unmap_vmap_area_noflush(struct vmap_area *va)
Nick Piggindb64fe02008-10-18 20:27:03 -0700602{
603 va->flags |= VM_LAZY_FREE;
604 atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
605 if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
Nick Piggin496850e2008-11-19 15:36:33 -0800606 try_purge_vmap_area_lazy();
Nick Piggindb64fe02008-10-18 20:27:03 -0700607}
608
Nick Pigginb29acbd2008-12-01 13:13:47 -0800609/*
610 * Free and unmap a vmap area
611 */
612static void free_unmap_vmap_area(struct vmap_area *va)
613{
614 flush_cache_vunmap(va->va_start, va->va_end);
615 free_unmap_vmap_area_noflush(va);
616}
617
Nick Piggindb64fe02008-10-18 20:27:03 -0700618static struct vmap_area *find_vmap_area(unsigned long addr)
619{
620 struct vmap_area *va;
621
622 spin_lock(&vmap_area_lock);
623 va = __find_vmap_area(addr);
624 spin_unlock(&vmap_area_lock);
625
626 return va;
627}
628
629static void free_unmap_vmap_area_addr(unsigned long addr)
630{
631 struct vmap_area *va;
632
633 va = find_vmap_area(addr);
634 BUG_ON(!va);
635 free_unmap_vmap_area(va);
636}
637
638
639/*** Per cpu kva allocator ***/
640
641/*
642 * vmap space is limited especially on 32 bit architectures. Ensure there is
643 * room for at least 16 percpu vmap blocks per CPU.
644 */
645/*
646 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
647 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
648 * instead (we just need a rough idea)
649 */
650#if BITS_PER_LONG == 32
651#define VMALLOC_SPACE (128UL*1024*1024)
652#else
653#define VMALLOC_SPACE (128UL*1024*1024*1024)
654#endif
655
656#define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
657#define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
658#define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
659#define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
660#define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
661#define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
662#define VMAP_BBMAP_BITS VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
663 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
664 VMALLOC_PAGES / NR_CPUS / 16))
665
666#define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
667
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +1100668static bool vmap_initialized __read_mostly = false;
669
Nick Piggindb64fe02008-10-18 20:27:03 -0700670struct vmap_block_queue {
671 spinlock_t lock;
672 struct list_head free;
673 struct list_head dirty;
674 unsigned int nr_dirty;
675};
676
677struct vmap_block {
678 spinlock_t lock;
679 struct vmap_area *va;
680 struct vmap_block_queue *vbq;
681 unsigned long free, dirty;
682 DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
683 DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
684 union {
MinChan Kimd0868172009-03-31 15:19:26 -0700685 struct list_head free_list;
Nick Piggindb64fe02008-10-18 20:27:03 -0700686 struct rcu_head rcu_head;
687 };
688};
689
690/* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
691static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
692
693/*
694 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
695 * in the free path. Could get rid of this if we change the API to return a
696 * "cookie" from alloc, to be passed to free. But no big deal yet.
697 */
698static DEFINE_SPINLOCK(vmap_block_tree_lock);
699static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
700
701/*
702 * We should probably have a fallback mechanism to allocate virtual memory
703 * out of partially filled vmap blocks. However vmap block sizing should be
704 * fairly reasonable according to the vmalloc size, so it shouldn't be a
705 * big problem.
706 */
707
708static unsigned long addr_to_vb_idx(unsigned long addr)
709{
710 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
711 addr /= VMAP_BLOCK_SIZE;
712 return addr;
713}
714
715static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
716{
717 struct vmap_block_queue *vbq;
718 struct vmap_block *vb;
719 struct vmap_area *va;
720 unsigned long vb_idx;
721 int node, err;
722
723 node = numa_node_id();
724
725 vb = kmalloc_node(sizeof(struct vmap_block),
726 gfp_mask & GFP_RECLAIM_MASK, node);
727 if (unlikely(!vb))
728 return ERR_PTR(-ENOMEM);
729
730 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
731 VMALLOC_START, VMALLOC_END,
732 node, gfp_mask);
733 if (unlikely(IS_ERR(va))) {
734 kfree(vb);
735 return ERR_PTR(PTR_ERR(va));
736 }
737
738 err = radix_tree_preload(gfp_mask);
739 if (unlikely(err)) {
740 kfree(vb);
741 free_vmap_area(va);
742 return ERR_PTR(err);
743 }
744
745 spin_lock_init(&vb->lock);
746 vb->va = va;
747 vb->free = VMAP_BBMAP_BITS;
748 vb->dirty = 0;
749 bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
750 bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
751 INIT_LIST_HEAD(&vb->free_list);
Nick Piggindb64fe02008-10-18 20:27:03 -0700752
753 vb_idx = addr_to_vb_idx(va->va_start);
754 spin_lock(&vmap_block_tree_lock);
755 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
756 spin_unlock(&vmap_block_tree_lock);
757 BUG_ON(err);
758 radix_tree_preload_end();
759
760 vbq = &get_cpu_var(vmap_block_queue);
761 vb->vbq = vbq;
762 spin_lock(&vbq->lock);
763 list_add(&vb->free_list, &vbq->free);
764 spin_unlock(&vbq->lock);
765 put_cpu_var(vmap_cpu_blocks);
766
767 return vb;
768}
769
770static void rcu_free_vb(struct rcu_head *head)
771{
772 struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
773
774 kfree(vb);
775}
776
777static void free_vmap_block(struct vmap_block *vb)
778{
779 struct vmap_block *tmp;
780 unsigned long vb_idx;
781
MinChan Kimd0868172009-03-31 15:19:26 -0700782 BUG_ON(!list_empty(&vb->free_list));
Nick Piggindb64fe02008-10-18 20:27:03 -0700783
784 vb_idx = addr_to_vb_idx(vb->va->va_start);
785 spin_lock(&vmap_block_tree_lock);
786 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
787 spin_unlock(&vmap_block_tree_lock);
788 BUG_ON(tmp != vb);
789
Nick Pigginb29acbd2008-12-01 13:13:47 -0800790 free_unmap_vmap_area_noflush(vb->va);
Nick Piggindb64fe02008-10-18 20:27:03 -0700791 call_rcu(&vb->rcu_head, rcu_free_vb);
792}
793
794static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
795{
796 struct vmap_block_queue *vbq;
797 struct vmap_block *vb;
798 unsigned long addr = 0;
799 unsigned int order;
800
801 BUG_ON(size & ~PAGE_MASK);
802 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
803 order = get_order(size);
804
805again:
806 rcu_read_lock();
807 vbq = &get_cpu_var(vmap_block_queue);
808 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
809 int i;
810
811 spin_lock(&vb->lock);
812 i = bitmap_find_free_region(vb->alloc_map,
813 VMAP_BBMAP_BITS, order);
814
815 if (i >= 0) {
816 addr = vb->va->va_start + (i << PAGE_SHIFT);
817 BUG_ON(addr_to_vb_idx(addr) !=
818 addr_to_vb_idx(vb->va->va_start));
819 vb->free -= 1UL << order;
820 if (vb->free == 0) {
821 spin_lock(&vbq->lock);
822 list_del_init(&vb->free_list);
823 spin_unlock(&vbq->lock);
824 }
825 spin_unlock(&vb->lock);
826 break;
827 }
828 spin_unlock(&vb->lock);
829 }
830 put_cpu_var(vmap_cpu_blocks);
831 rcu_read_unlock();
832
833 if (!addr) {
834 vb = new_vmap_block(gfp_mask);
835 if (IS_ERR(vb))
836 return vb;
837 goto again;
838 }
839
840 return (void *)addr;
841}
842
843static void vb_free(const void *addr, unsigned long size)
844{
845 unsigned long offset;
846 unsigned long vb_idx;
847 unsigned int order;
848 struct vmap_block *vb;
849
850 BUG_ON(size & ~PAGE_MASK);
851 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Nick Pigginb29acbd2008-12-01 13:13:47 -0800852
853 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
854
Nick Piggindb64fe02008-10-18 20:27:03 -0700855 order = get_order(size);
856
857 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
858
859 vb_idx = addr_to_vb_idx((unsigned long)addr);
860 rcu_read_lock();
861 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
862 rcu_read_unlock();
863 BUG_ON(!vb);
864
865 spin_lock(&vb->lock);
866 bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
MinChan Kimd0868172009-03-31 15:19:26 -0700867
Nick Piggindb64fe02008-10-18 20:27:03 -0700868 vb->dirty += 1UL << order;
869 if (vb->dirty == VMAP_BBMAP_BITS) {
870 BUG_ON(vb->free || !list_empty(&vb->free_list));
871 spin_unlock(&vb->lock);
872 free_vmap_block(vb);
873 } else
874 spin_unlock(&vb->lock);
875}
876
877/**
878 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
879 *
880 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
881 * to amortize TLB flushing overheads. What this means is that any page you
882 * have now, may, in a former life, have been mapped into kernel virtual
883 * address by the vmap layer and so there might be some CPUs with TLB entries
884 * still referencing that page (additional to the regular 1:1 kernel mapping).
885 *
886 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
887 * be sure that none of the pages we have control over will have any aliases
888 * from the vmap layer.
889 */
890void vm_unmap_aliases(void)
891{
892 unsigned long start = ULONG_MAX, end = 0;
893 int cpu;
894 int flush = 0;
895
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +1100896 if (unlikely(!vmap_initialized))
897 return;
898
Nick Piggindb64fe02008-10-18 20:27:03 -0700899 for_each_possible_cpu(cpu) {
900 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
901 struct vmap_block *vb;
902
903 rcu_read_lock();
904 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
905 int i;
906
907 spin_lock(&vb->lock);
908 i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
909 while (i < VMAP_BBMAP_BITS) {
910 unsigned long s, e;
911 int j;
912 j = find_next_zero_bit(vb->dirty_map,
913 VMAP_BBMAP_BITS, i);
914
915 s = vb->va->va_start + (i << PAGE_SHIFT);
916 e = vb->va->va_start + (j << PAGE_SHIFT);
917 vunmap_page_range(s, e);
918 flush = 1;
919
920 if (s < start)
921 start = s;
922 if (e > end)
923 end = e;
924
925 i = j;
926 i = find_next_bit(vb->dirty_map,
927 VMAP_BBMAP_BITS, i);
928 }
929 spin_unlock(&vb->lock);
930 }
931 rcu_read_unlock();
932 }
933
934 __purge_vmap_area_lazy(&start, &end, 1, flush);
935}
936EXPORT_SYMBOL_GPL(vm_unmap_aliases);
937
938/**
939 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
940 * @mem: the pointer returned by vm_map_ram
941 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
942 */
943void vm_unmap_ram(const void *mem, unsigned int count)
944{
945 unsigned long size = count << PAGE_SHIFT;
946 unsigned long addr = (unsigned long)mem;
947
948 BUG_ON(!addr);
949 BUG_ON(addr < VMALLOC_START);
950 BUG_ON(addr > VMALLOC_END);
951 BUG_ON(addr & (PAGE_SIZE-1));
952
953 debug_check_no_locks_freed(mem, size);
Nick Piggincd528582009-01-06 14:39:20 -0800954 vmap_debug_free_range(addr, addr+size);
Nick Piggindb64fe02008-10-18 20:27:03 -0700955
956 if (likely(count <= VMAP_MAX_ALLOC))
957 vb_free(mem, size);
958 else
959 free_unmap_vmap_area_addr(addr);
960}
961EXPORT_SYMBOL(vm_unmap_ram);
962
963/**
964 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
965 * @pages: an array of pointers to the pages to be mapped
966 * @count: number of pages
967 * @node: prefer to allocate data structures on this node
968 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
Randy Dunlape99c97a2008-10-29 14:01:09 -0700969 *
970 * Returns: a pointer to the address that has been mapped, or %NULL on failure
Nick Piggindb64fe02008-10-18 20:27:03 -0700971 */
972void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
973{
974 unsigned long size = count << PAGE_SHIFT;
975 unsigned long addr;
976 void *mem;
977
978 if (likely(count <= VMAP_MAX_ALLOC)) {
979 mem = vb_alloc(size, GFP_KERNEL);
980 if (IS_ERR(mem))
981 return NULL;
982 addr = (unsigned long)mem;
983 } else {
984 struct vmap_area *va;
985 va = alloc_vmap_area(size, PAGE_SIZE,
986 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
987 if (IS_ERR(va))
988 return NULL;
989
990 addr = va->va_start;
991 mem = (void *)addr;
992 }
993 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
994 vm_unmap_ram(mem, count);
995 return NULL;
996 }
997 return mem;
998}
999EXPORT_SYMBOL(vm_map_ram);
1000
Tejun Heof0aa6612009-02-20 16:29:08 +09001001/**
1002 * vm_area_register_early - register vmap area early during boot
1003 * @vm: vm_struct to register
Tejun Heoc0c0a292009-02-24 11:57:21 +09001004 * @align: requested alignment
Tejun Heof0aa6612009-02-20 16:29:08 +09001005 *
1006 * This function is used to register kernel vm area before
1007 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1008 * proper values on entry and other fields should be zero. On return,
1009 * vm->addr contains the allocated address.
1010 *
1011 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1012 */
Tejun Heoc0c0a292009-02-24 11:57:21 +09001013void __init vm_area_register_early(struct vm_struct *vm, size_t align)
Tejun Heof0aa6612009-02-20 16:29:08 +09001014{
1015 static size_t vm_init_off __initdata;
Tejun Heoc0c0a292009-02-24 11:57:21 +09001016 unsigned long addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001017
Tejun Heoc0c0a292009-02-24 11:57:21 +09001018 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1019 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1020
1021 vm->addr = (void *)addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001022
1023 vm->next = vmlist;
1024 vmlist = vm;
1025}
1026
Nick Piggindb64fe02008-10-18 20:27:03 -07001027void __init vmalloc_init(void)
1028{
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001029 struct vmap_area *va;
1030 struct vm_struct *tmp;
Nick Piggindb64fe02008-10-18 20:27:03 -07001031 int i;
1032
1033 for_each_possible_cpu(i) {
1034 struct vmap_block_queue *vbq;
1035
1036 vbq = &per_cpu(vmap_block_queue, i);
1037 spin_lock_init(&vbq->lock);
1038 INIT_LIST_HEAD(&vbq->free);
1039 INIT_LIST_HEAD(&vbq->dirty);
1040 vbq->nr_dirty = 0;
1041 }
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001042
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001043 /* Import existing vmlist entries. */
1044 for (tmp = vmlist; tmp; tmp = tmp->next) {
Pekka Enberg43ebdac2009-05-25 15:01:35 +03001045 va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001046 va->flags = tmp->flags | VM_VM_AREA;
1047 va->va_start = (unsigned long)tmp->addr;
1048 va->va_end = va->va_start + tmp->size;
1049 __insert_vmap_area(va);
1050 }
Tejun Heoca23e402009-08-14 15:00:52 +09001051
1052 vmap_area_pcpu_hole = VMALLOC_END;
1053
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001054 vmap_initialized = true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001055}
1056
Tejun Heo8fc48982009-02-20 16:29:08 +09001057/**
1058 * map_kernel_range_noflush - map kernel VM area with the specified pages
1059 * @addr: start of the VM area to map
1060 * @size: size of the VM area to map
1061 * @prot: page protection flags to use
1062 * @pages: pages to map
1063 *
1064 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1065 * specify should have been allocated using get_vm_area() and its
1066 * friends.
1067 *
1068 * NOTE:
1069 * This function does NOT do any cache flushing. The caller is
1070 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1071 * before calling this function.
1072 *
1073 * RETURNS:
1074 * The number of pages mapped on success, -errno on failure.
1075 */
1076int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1077 pgprot_t prot, struct page **pages)
1078{
1079 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1080}
1081
1082/**
1083 * unmap_kernel_range_noflush - unmap kernel VM area
1084 * @addr: start of the VM area to unmap
1085 * @size: size of the VM area to unmap
1086 *
1087 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1088 * specify should have been allocated using get_vm_area() and its
1089 * friends.
1090 *
1091 * NOTE:
1092 * This function does NOT do any cache flushing. The caller is
1093 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1094 * before calling this function and flush_tlb_kernel_range() after.
1095 */
1096void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1097{
1098 vunmap_page_range(addr, addr + size);
1099}
1100
1101/**
1102 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1103 * @addr: start of the VM area to unmap
1104 * @size: size of the VM area to unmap
1105 *
1106 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1107 * the unmapping and tlb after.
1108 */
Nick Piggindb64fe02008-10-18 20:27:03 -07001109void unmap_kernel_range(unsigned long addr, unsigned long size)
1110{
1111 unsigned long end = addr + size;
Tejun Heof6fcba72009-02-20 15:38:48 -08001112
1113 flush_cache_vunmap(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -07001114 vunmap_page_range(addr, end);
1115 flush_tlb_kernel_range(addr, end);
1116}
1117
1118int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
1119{
1120 unsigned long addr = (unsigned long)area->addr;
1121 unsigned long end = addr + area->size - PAGE_SIZE;
1122 int err;
1123
1124 err = vmap_page_range(addr, end, prot, *pages);
1125 if (err > 0) {
1126 *pages += err;
1127 err = 0;
1128 }
1129
1130 return err;
1131}
1132EXPORT_SYMBOL_GPL(map_vm_area);
1133
1134/*** Old vmalloc interfaces ***/
1135DEFINE_RWLOCK(vmlist_lock);
1136struct vm_struct *vmlist;
1137
Tejun Heocf88c792009-08-14 15:00:52 +09001138static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
1139 unsigned long flags, void *caller)
1140{
1141 struct vm_struct *tmp, **p;
1142
1143 vm->flags = flags;
1144 vm->addr = (void *)va->va_start;
1145 vm->size = va->va_end - va->va_start;
1146 vm->caller = caller;
1147 va->private = vm;
1148 va->flags |= VM_VM_AREA;
1149
1150 write_lock(&vmlist_lock);
1151 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1152 if (tmp->addr >= vm->addr)
1153 break;
1154 }
1155 vm->next = *p;
1156 *p = vm;
1157 write_unlock(&vmlist_lock);
1158}
1159
Nick Piggindb64fe02008-10-18 20:27:03 -07001160static struct vm_struct *__get_vm_area_node(unsigned long size,
1161 unsigned long flags, unsigned long start, unsigned long end,
1162 int node, gfp_t gfp_mask, void *caller)
1163{
1164 static struct vmap_area *va;
1165 struct vm_struct *area;
Nick Piggindb64fe02008-10-18 20:27:03 -07001166 unsigned long align = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -07001168 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (flags & VM_IOREMAP) {
1170 int bit = fls(size);
1171
1172 if (bit > IOREMAP_MAX_ORDER)
1173 bit = IOREMAP_MAX_ORDER;
1174 else if (bit < PAGE_SHIFT)
1175 bit = PAGE_SHIFT;
1176
1177 align = 1ul << bit;
1178 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 size = PAGE_ALIGN(size);
OGAWA Hirofumi31be8302006-11-16 01:19:29 -08001181 if (unlikely(!size))
1182 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Tejun Heocf88c792009-08-14 15:00:52 +09001184 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (unlikely(!area))
1186 return NULL;
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /*
1189 * We always allocate a guard page.
1190 */
1191 size += PAGE_SIZE;
1192
Nick Piggindb64fe02008-10-18 20:27:03 -07001193 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1194 if (IS_ERR(va)) {
1195 kfree(area);
1196 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Tejun Heocf88c792009-08-14 15:00:52 +09001199 insert_vmalloc_vm(area, va, flags, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Christoph Lameter930fc452005-10-29 18:15:41 -07001203struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1204 unsigned long start, unsigned long end)
1205{
Christoph Lameter23016962008-04-28 02:12:42 -07001206 return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1207 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001208}
Rusty Russell5992b6d2007-07-19 01:49:21 -07001209EXPORT_SYMBOL_GPL(__get_vm_area);
Christoph Lameter930fc452005-10-29 18:15:41 -07001210
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001211struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1212 unsigned long start, unsigned long end,
1213 void *caller)
1214{
1215 return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1216 caller);
1217}
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219/**
Simon Arlott183ff222007-10-20 01:27:18 +02001220 * get_vm_area - reserve a contiguous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 * @size: size of the area
1222 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
1223 *
1224 * Search an area of @size in the kernel virtual mapping area,
1225 * and reserved it for out purposes. Returns the area descriptor
1226 * on success or %NULL on failure.
1227 */
1228struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
1229{
Christoph Lameter23016962008-04-28 02:12:42 -07001230 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
1231 -1, GFP_KERNEL, __builtin_return_address(0));
1232}
1233
1234struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
1235 void *caller)
1236{
1237 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
1238 -1, GFP_KERNEL, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -07001241struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
1242 int node, gfp_t gfp_mask)
Christoph Lameter930fc452005-10-29 18:15:41 -07001243{
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -07001244 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
Christoph Lameter23016962008-04-28 02:12:42 -07001245 gfp_mask, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001246}
1247
Nick Piggindb64fe02008-10-18 20:27:03 -07001248static struct vm_struct *find_vm_area(const void *addr)
Nick Piggin83342312006-06-23 02:03:20 -07001249{
Nick Piggindb64fe02008-10-18 20:27:03 -07001250 struct vmap_area *va;
Nick Piggin83342312006-06-23 02:03:20 -07001251
Nick Piggindb64fe02008-10-18 20:27:03 -07001252 va = find_vmap_area((unsigned long)addr);
1253 if (va && va->flags & VM_VM_AREA)
1254 return va->private;
Nick Piggin83342312006-06-23 02:03:20 -07001255
Andi Kleen7856dfe2005-05-20 14:27:57 -07001256 return NULL;
Andi Kleen7856dfe2005-05-20 14:27:57 -07001257}
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259/**
Simon Arlott183ff222007-10-20 01:27:18 +02001260 * remove_vm_area - find and remove a continuous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 * @addr: base address
1262 *
1263 * Search for the kernel VM area starting at @addr, and remove it.
1264 * This function returns the found VM area, but using it is NOT safe
Andi Kleen7856dfe2005-05-20 14:27:57 -07001265 * on SMP machines, except for its size or flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001267struct vm_struct *remove_vm_area(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Nick Piggindb64fe02008-10-18 20:27:03 -07001269 struct vmap_area *va;
1270
1271 va = find_vmap_area((unsigned long)addr);
1272 if (va && va->flags & VM_VM_AREA) {
1273 struct vm_struct *vm = va->private;
1274 struct vm_struct *tmp, **p;
Nick Piggincd528582009-01-06 14:39:20 -08001275
1276 vmap_debug_free_range(va->va_start, va->va_end);
Nick Piggindb64fe02008-10-18 20:27:03 -07001277 free_unmap_vmap_area(va);
1278 vm->size -= PAGE_SIZE;
1279
1280 write_lock(&vmlist_lock);
1281 for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
1282 ;
1283 *p = tmp->next;
1284 write_unlock(&vmlist_lock);
1285
1286 return vm;
1287 }
1288 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001291static void __vunmap(const void *addr, int deallocate_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 struct vm_struct *area;
1294
1295 if (!addr)
1296 return;
1297
1298 if ((PAGE_SIZE-1) & (unsigned long)addr) {
Arjan van de Ven4c8573e2008-07-25 19:45:37 -07001299 WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return;
1301 }
1302
1303 area = remove_vm_area(addr);
1304 if (unlikely(!area)) {
Arjan van de Ven4c8573e2008-07-25 19:45:37 -07001305 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return;
1308 }
1309
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001310 debug_check_no_locks_freed(addr, area->size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001311 debug_check_no_obj_freed(addr, area->size);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (deallocate_pages) {
1314 int i;
1315
1316 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001317 struct page *page = area->pages[i];
1318
1319 BUG_ON(!page);
1320 __free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322
Jan Kiszka8757d5f2006-07-14 00:23:56 -07001323 if (area->flags & VM_VPAGES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 vfree(area->pages);
1325 else
1326 kfree(area->pages);
1327 }
1328
1329 kfree(area);
1330 return;
1331}
1332
1333/**
1334 * vfree - release memory allocated by vmalloc()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * @addr: memory base address
1336 *
Simon Arlott183ff222007-10-20 01:27:18 +02001337 * Free the virtually continuous memory area starting at @addr, as
Pekka Enberg80e93ef2005-09-09 13:10:16 -07001338 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1339 * NULL, no operation is performed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07001341 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001343void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 BUG_ON(in_interrupt());
Catalin Marinas89219d32009-06-11 13:23:19 +01001346
1347 kmemleak_free(addr);
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 __vunmap(addr, 1);
1350}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351EXPORT_SYMBOL(vfree);
1352
1353/**
1354 * vunmap - release virtual mapping obtained by vmap()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 * @addr: memory base address
1356 *
1357 * Free the virtually contiguous memory area starting at @addr,
1358 * which was created from the page array passed to vmap().
1359 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07001360 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001362void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 BUG_ON(in_interrupt());
Peter Zijlstra34754b62009-02-25 16:04:03 +01001365 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 __vunmap(addr, 0);
1367}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368EXPORT_SYMBOL(vunmap);
1369
1370/**
1371 * vmap - map an array of pages into virtually contiguous space
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * @pages: array of page pointers
1373 * @count: number of pages to map
1374 * @flags: vm_area->flags
1375 * @prot: page protection for the mapping
1376 *
1377 * Maps @count pages from @pages into contiguous kernel virtual
1378 * space.
1379 */
1380void *vmap(struct page **pages, unsigned int count,
1381 unsigned long flags, pgprot_t prot)
1382{
1383 struct vm_struct *area;
1384
Peter Zijlstra34754b62009-02-25 16:04:03 +01001385 might_sleep();
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (count > num_physpages)
1388 return NULL;
1389
Christoph Lameter23016962008-04-28 02:12:42 -07001390 area = get_vm_area_caller((count << PAGE_SHIFT), flags,
1391 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (!area)
1393 return NULL;
Christoph Lameter23016962008-04-28 02:12:42 -07001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (map_vm_area(area, prot, &pages)) {
1396 vunmap(area->addr);
1397 return NULL;
1398 }
1399
1400 return area->addr;
1401}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402EXPORT_SYMBOL(vmap);
1403
Nick Piggindb64fe02008-10-18 20:27:03 -07001404static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1405 int node, void *caller);
Adrian Bunke31d9eb2008-02-04 22:29:09 -08001406static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
Christoph Lameter23016962008-04-28 02:12:42 -07001407 pgprot_t prot, int node, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 struct page **pages;
1410 unsigned int nr_pages, array_size, i;
1411
1412 nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
1413 array_size = (nr_pages * sizeof(struct page *));
1414
1415 area->nr_pages = nr_pages;
1416 /* Please note that the recursion is strictly bounded. */
Jan Kiszka8757d5f2006-07-14 00:23:56 -07001417 if (array_size > PAGE_SIZE) {
Christoph Lameter94f60302007-07-17 04:03:29 -07001418 pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
Christoph Lameter23016962008-04-28 02:12:42 -07001419 PAGE_KERNEL, node, caller);
Jan Kiszka8757d5f2006-07-14 00:23:56 -07001420 area->flags |= VM_VPAGES;
Andrew Morton286e1ea2006-10-17 00:09:57 -07001421 } else {
1422 pages = kmalloc_node(array_size,
Christoph Lameter6cb06222007-10-16 01:25:41 -07001423 (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
Andrew Morton286e1ea2006-10-17 00:09:57 -07001424 node);
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 area->pages = pages;
Christoph Lameter23016962008-04-28 02:12:42 -07001427 area->caller = caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (!area->pages) {
1429 remove_vm_area(area->addr);
1430 kfree(area);
1431 return NULL;
1432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001435 struct page *page;
1436
Christoph Lameter930fc452005-10-29 18:15:41 -07001437 if (node < 0)
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001438 page = alloc_page(gfp_mask);
Christoph Lameter930fc452005-10-29 18:15:41 -07001439 else
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001440 page = alloc_pages_node(node, gfp_mask, 0);
1441
1442 if (unlikely(!page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* Successfully allocated i pages, free them in __vunmap() */
1444 area->nr_pages = i;
1445 goto fail;
1446 }
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001447 area->pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449
1450 if (map_vm_area(area, prot, &pages))
1451 goto fail;
1452 return area->addr;
1453
1454fail:
1455 vfree(area->addr);
1456 return NULL;
1457}
1458
Christoph Lameter930fc452005-10-29 18:15:41 -07001459void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
1460{
Catalin Marinas89219d32009-06-11 13:23:19 +01001461 void *addr = __vmalloc_area_node(area, gfp_mask, prot, -1,
1462 __builtin_return_address(0));
1463
1464 /*
1465 * A ref_count = 3 is needed because the vm_struct and vmap_area
1466 * structures allocated in the __get_vm_area_node() function contain
1467 * references to the virtual address of the vmalloc'ed block.
1468 */
1469 kmemleak_alloc(addr, area->size - PAGE_SIZE, 3, gfp_mask);
1470
1471 return addr;
Christoph Lameter930fc452005-10-29 18:15:41 -07001472}
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474/**
Christoph Lameter930fc452005-10-29 18:15:41 -07001475 * __vmalloc_node - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 * @size: allocation size
1477 * @gfp_mask: flags for the page level allocator
1478 * @prot: protection mask for the allocated pages
Randy Dunlapd44e0782005-11-07 01:01:10 -08001479 * @node: node to use for allocation or -1
Randy Dunlapc85d1942008-05-01 04:34:48 -07001480 * @caller: caller's return address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 *
1482 * Allocate enough pages to cover @size from the page level
1483 * allocator with @gfp_mask flags. Map them into contiguous
1484 * kernel virtual space, using a pagetable protection of @prot.
1485 */
Adrian Bunkb2213852006-09-25 23:31:02 -07001486static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
Christoph Lameter23016962008-04-28 02:12:42 -07001487 int node, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 struct vm_struct *area;
Catalin Marinas89219d32009-06-11 13:23:19 +01001490 void *addr;
1491 unsigned long real_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 size = PAGE_ALIGN(size);
1494 if (!size || (size >> PAGE_SHIFT) > num_physpages)
1495 return NULL;
1496
Christoph Lameter23016962008-04-28 02:12:42 -07001497 area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
1498 node, gfp_mask, caller);
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (!area)
1501 return NULL;
1502
Catalin Marinas89219d32009-06-11 13:23:19 +01001503 addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
1504
1505 /*
1506 * A ref_count = 3 is needed because the vm_struct and vmap_area
1507 * structures allocated in the __get_vm_area_node() function contain
1508 * references to the virtual address of the vmalloc'ed block.
1509 */
1510 kmemleak_alloc(addr, real_size, 3, gfp_mask);
1511
1512 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Christoph Lameter930fc452005-10-29 18:15:41 -07001515void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1516{
Christoph Lameter23016962008-04-28 02:12:42 -07001517 return __vmalloc_node(size, gfp_mask, prot, -1,
1518 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520EXPORT_SYMBOL(__vmalloc);
1521
1522/**
1523 * vmalloc - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 * Allocate enough pages to cover @size from the page level
1526 * allocator and map them into contiguous kernel virtual space.
1527 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02001528 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 * use __vmalloc() instead.
1530 */
1531void *vmalloc(unsigned long size)
1532{
Christoph Lameter23016962008-04-28 02:12:42 -07001533 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1534 -1, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536EXPORT_SYMBOL(vmalloc);
1537
Christoph Lameter930fc452005-10-29 18:15:41 -07001538/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07001539 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1540 * @size: allocation size
Nick Piggin83342312006-06-23 02:03:20 -07001541 *
Rolf Eike Beeread04082006-09-27 01:50:13 -07001542 * The resulting memory area is zeroed so it can be mapped to userspace
1543 * without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -07001544 */
1545void *vmalloc_user(unsigned long size)
1546{
1547 struct vm_struct *area;
1548 void *ret;
1549
Glauber Costa84877842009-01-06 14:39:19 -08001550 ret = __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
1551 PAGE_KERNEL, -1, __builtin_return_address(0));
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001552 if (ret) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001553 area = find_vm_area(ret);
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001554 area->flags |= VM_USERMAP;
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001555 }
Nick Piggin83342312006-06-23 02:03:20 -07001556 return ret;
1557}
1558EXPORT_SYMBOL(vmalloc_user);
1559
1560/**
Christoph Lameter930fc452005-10-29 18:15:41 -07001561 * vmalloc_node - allocate memory on a specific node
Christoph Lameter930fc452005-10-29 18:15:41 -07001562 * @size: allocation size
Randy Dunlapd44e0782005-11-07 01:01:10 -08001563 * @node: numa node
Christoph Lameter930fc452005-10-29 18:15:41 -07001564 *
1565 * Allocate enough pages to cover @size from the page level
1566 * allocator and map them into contiguous kernel virtual space.
1567 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02001568 * For tight control over page level allocator and protection flags
Christoph Lameter930fc452005-10-29 18:15:41 -07001569 * use __vmalloc() instead.
1570 */
1571void *vmalloc_node(unsigned long size, int node)
1572{
Christoph Lameter23016962008-04-28 02:12:42 -07001573 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1574 node, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001575}
1576EXPORT_SYMBOL(vmalloc_node);
1577
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001578#ifndef PAGE_KERNEL_EXEC
1579# define PAGE_KERNEL_EXEC PAGE_KERNEL
1580#endif
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582/**
1583 * vmalloc_exec - allocate virtually contiguous, executable memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 * @size: allocation size
1585 *
1586 * Kernel-internal function to allocate enough pages to cover @size
1587 * the page level allocator and map them into contiguous and
1588 * executable kernel virtual space.
1589 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02001590 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 * use __vmalloc() instead.
1592 */
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594void *vmalloc_exec(unsigned long size)
1595{
Glauber Costa84877842009-01-06 14:39:19 -08001596 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
1597 -1, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Andi Kleen0d08e0d2007-05-02 19:27:12 +02001600#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
Benjamin Herrenschmidt7ac674f2007-07-19 01:49:10 -07001601#define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02001602#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
Benjamin Herrenschmidt7ac674f2007-07-19 01:49:10 -07001603#define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02001604#else
1605#define GFP_VMALLOC32 GFP_KERNEL
1606#endif
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608/**
1609 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * @size: allocation size
1611 *
1612 * Allocate enough 32bit PA addressable pages to cover @size from the
1613 * page level allocator and map them into contiguous kernel virtual space.
1614 */
1615void *vmalloc_32(unsigned long size)
1616{
Glauber Costa84877842009-01-06 14:39:19 -08001617 return __vmalloc_node(size, GFP_VMALLOC32, PAGE_KERNEL,
1618 -1, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620EXPORT_SYMBOL(vmalloc_32);
1621
Nick Piggin83342312006-06-23 02:03:20 -07001622/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07001623 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
Nick Piggin83342312006-06-23 02:03:20 -07001624 * @size: allocation size
Rolf Eike Beeread04082006-09-27 01:50:13 -07001625 *
1626 * The resulting memory area is 32bit addressable and zeroed so it can be
1627 * mapped to userspace without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -07001628 */
1629void *vmalloc_32_user(unsigned long size)
1630{
1631 struct vm_struct *area;
1632 void *ret;
1633
Glauber Costa84877842009-01-06 14:39:19 -08001634 ret = __vmalloc_node(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
1635 -1, __builtin_return_address(0));
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001636 if (ret) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001637 area = find_vm_area(ret);
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001638 area->flags |= VM_USERMAP;
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001639 }
Nick Piggin83342312006-06-23 02:03:20 -07001640 return ret;
1641}
1642EXPORT_SYMBOL(vmalloc_32_user);
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644long vread(char *buf, char *addr, unsigned long count)
1645{
1646 struct vm_struct *tmp;
1647 char *vaddr, *buf_start = buf;
1648 unsigned long n;
1649
1650 /* Don't allow overflow */
1651 if ((unsigned long) addr + count < count)
1652 count = -(unsigned long) addr;
1653
1654 read_lock(&vmlist_lock);
1655 for (tmp = vmlist; tmp; tmp = tmp->next) {
1656 vaddr = (char *) tmp->addr;
1657 if (addr >= vaddr + tmp->size - PAGE_SIZE)
1658 continue;
1659 while (addr < vaddr) {
1660 if (count == 0)
1661 goto finished;
1662 *buf = '\0';
1663 buf++;
1664 addr++;
1665 count--;
1666 }
1667 n = vaddr + tmp->size - PAGE_SIZE - addr;
1668 do {
1669 if (count == 0)
1670 goto finished;
1671 *buf = *addr;
1672 buf++;
1673 addr++;
1674 count--;
1675 } while (--n > 0);
1676 }
1677finished:
1678 read_unlock(&vmlist_lock);
1679 return buf - buf_start;
1680}
1681
1682long vwrite(char *buf, char *addr, unsigned long count)
1683{
1684 struct vm_struct *tmp;
1685 char *vaddr, *buf_start = buf;
1686 unsigned long n;
1687
1688 /* Don't allow overflow */
1689 if ((unsigned long) addr + count < count)
1690 count = -(unsigned long) addr;
1691
1692 read_lock(&vmlist_lock);
1693 for (tmp = vmlist; tmp; tmp = tmp->next) {
1694 vaddr = (char *) tmp->addr;
1695 if (addr >= vaddr + tmp->size - PAGE_SIZE)
1696 continue;
1697 while (addr < vaddr) {
1698 if (count == 0)
1699 goto finished;
1700 buf++;
1701 addr++;
1702 count--;
1703 }
1704 n = vaddr + tmp->size - PAGE_SIZE - addr;
1705 do {
1706 if (count == 0)
1707 goto finished;
1708 *addr = *buf;
1709 buf++;
1710 addr++;
1711 count--;
1712 } while (--n > 0);
1713 }
1714finished:
1715 read_unlock(&vmlist_lock);
1716 return buf - buf_start;
1717}
Nick Piggin83342312006-06-23 02:03:20 -07001718
1719/**
1720 * remap_vmalloc_range - map vmalloc pages to userspace
Nick Piggin83342312006-06-23 02:03:20 -07001721 * @vma: vma to cover (map full range of vma)
1722 * @addr: vmalloc memory
1723 * @pgoff: number of pages into addr before first page to map
Randy Dunlap76824862008-03-19 17:00:40 -07001724 *
1725 * Returns: 0 for success, -Exxx on failure
Nick Piggin83342312006-06-23 02:03:20 -07001726 *
1727 * This function checks that addr is a valid vmalloc'ed area, and
1728 * that it is big enough to cover the vma. Will return failure if
1729 * that criteria isn't met.
1730 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001731 * Similar to remap_pfn_range() (see mm/memory.c)
Nick Piggin83342312006-06-23 02:03:20 -07001732 */
1733int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1734 unsigned long pgoff)
1735{
1736 struct vm_struct *area;
1737 unsigned long uaddr = vma->vm_start;
1738 unsigned long usize = vma->vm_end - vma->vm_start;
Nick Piggin83342312006-06-23 02:03:20 -07001739
1740 if ((PAGE_SIZE-1) & (unsigned long)addr)
1741 return -EINVAL;
1742
Nick Piggindb64fe02008-10-18 20:27:03 -07001743 area = find_vm_area(addr);
Nick Piggin83342312006-06-23 02:03:20 -07001744 if (!area)
Nick Piggindb64fe02008-10-18 20:27:03 -07001745 return -EINVAL;
Nick Piggin83342312006-06-23 02:03:20 -07001746
1747 if (!(area->flags & VM_USERMAP))
Nick Piggindb64fe02008-10-18 20:27:03 -07001748 return -EINVAL;
Nick Piggin83342312006-06-23 02:03:20 -07001749
1750 if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
Nick Piggindb64fe02008-10-18 20:27:03 -07001751 return -EINVAL;
Nick Piggin83342312006-06-23 02:03:20 -07001752
1753 addr += pgoff << PAGE_SHIFT;
1754 do {
1755 struct page *page = vmalloc_to_page(addr);
Nick Piggindb64fe02008-10-18 20:27:03 -07001756 int ret;
1757
Nick Piggin83342312006-06-23 02:03:20 -07001758 ret = vm_insert_page(vma, uaddr, page);
1759 if (ret)
1760 return ret;
1761
1762 uaddr += PAGE_SIZE;
1763 addr += PAGE_SIZE;
1764 usize -= PAGE_SIZE;
1765 } while (usize > 0);
1766
1767 /* Prevent "things" like memory migration? VM_flags need a cleanup... */
1768 vma->vm_flags |= VM_RESERVED;
1769
Nick Piggindb64fe02008-10-18 20:27:03 -07001770 return 0;
Nick Piggin83342312006-06-23 02:03:20 -07001771}
1772EXPORT_SYMBOL(remap_vmalloc_range);
1773
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07001774/*
1775 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
1776 * have one.
1777 */
1778void __attribute__((weak)) vmalloc_sync_all(void)
1779{
1780}
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07001781
1782
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08001783static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07001784{
1785 /* apply_to_page_range() does all the hard work. */
1786 return 0;
1787}
1788
1789/**
1790 * alloc_vm_area - allocate a range of kernel address space
1791 * @size: size of the area
Randy Dunlap76824862008-03-19 17:00:40 -07001792 *
1793 * Returns: NULL on failure, vm_struct on success
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07001794 *
1795 * This function reserves a range of kernel address space, and
1796 * allocates pagetables to map that range. No actual mappings
1797 * are created. If the kernel address space is not shared
1798 * between processes, it syncs the pagetable across all
1799 * processes.
1800 */
1801struct vm_struct *alloc_vm_area(size_t size)
1802{
1803 struct vm_struct *area;
1804
Christoph Lameter23016962008-04-28 02:12:42 -07001805 area = get_vm_area_caller(size, VM_IOREMAP,
1806 __builtin_return_address(0));
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07001807 if (area == NULL)
1808 return NULL;
1809
1810 /*
1811 * This ensures that page tables are constructed for this region
1812 * of kernel virtual address space and mapped into init_mm.
1813 */
1814 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
1815 area->size, f, NULL)) {
1816 free_vm_area(area);
1817 return NULL;
1818 }
1819
1820 /* Make sure the pagetables are constructed in process kernel
1821 mappings */
1822 vmalloc_sync_all();
1823
1824 return area;
1825}
1826EXPORT_SYMBOL_GPL(alloc_vm_area);
1827
1828void free_vm_area(struct vm_struct *area)
1829{
1830 struct vm_struct *ret;
1831 ret = remove_vm_area(area->addr);
1832 BUG_ON(ret != area);
1833 kfree(area);
1834}
1835EXPORT_SYMBOL_GPL(free_vm_area);
Christoph Lametera10aa572008-04-28 02:12:40 -07001836
Tejun Heoca23e402009-08-14 15:00:52 +09001837static struct vmap_area *node_to_va(struct rb_node *n)
1838{
1839 return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
1840}
1841
1842/**
1843 * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
1844 * @end: target address
1845 * @pnext: out arg for the next vmap_area
1846 * @pprev: out arg for the previous vmap_area
1847 *
1848 * Returns: %true if either or both of next and prev are found,
1849 * %false if no vmap_area exists
1850 *
1851 * Find vmap_areas end addresses of which enclose @end. ie. if not
1852 * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
1853 */
1854static bool pvm_find_next_prev(unsigned long end,
1855 struct vmap_area **pnext,
1856 struct vmap_area **pprev)
1857{
1858 struct rb_node *n = vmap_area_root.rb_node;
1859 struct vmap_area *va = NULL;
1860
1861 while (n) {
1862 va = rb_entry(n, struct vmap_area, rb_node);
1863 if (end < va->va_end)
1864 n = n->rb_left;
1865 else if (end > va->va_end)
1866 n = n->rb_right;
1867 else
1868 break;
1869 }
1870
1871 if (!va)
1872 return false;
1873
1874 if (va->va_end > end) {
1875 *pnext = va;
1876 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
1877 } else {
1878 *pprev = va;
1879 *pnext = node_to_va(rb_next(&(*pprev)->rb_node));
1880 }
1881 return true;
1882}
1883
1884/**
1885 * pvm_determine_end - find the highest aligned address between two vmap_areas
1886 * @pnext: in/out arg for the next vmap_area
1887 * @pprev: in/out arg for the previous vmap_area
1888 * @align: alignment
1889 *
1890 * Returns: determined end address
1891 *
1892 * Find the highest aligned address between *@pnext and *@pprev below
1893 * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned
1894 * down address is between the end addresses of the two vmap_areas.
1895 *
1896 * Please note that the address returned by this function may fall
1897 * inside *@pnext vmap_area. The caller is responsible for checking
1898 * that.
1899 */
1900static unsigned long pvm_determine_end(struct vmap_area **pnext,
1901 struct vmap_area **pprev,
1902 unsigned long align)
1903{
1904 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
1905 unsigned long addr;
1906
1907 if (*pnext)
1908 addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
1909 else
1910 addr = vmalloc_end;
1911
1912 while (*pprev && (*pprev)->va_end > addr) {
1913 *pnext = *pprev;
1914 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
1915 }
1916
1917 return addr;
1918}
1919
1920/**
1921 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
1922 * @offsets: array containing offset of each area
1923 * @sizes: array containing size of each area
1924 * @nr_vms: the number of areas to allocate
1925 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
1926 * @gfp_mask: allocation mask
1927 *
1928 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
1929 * vm_structs on success, %NULL on failure
1930 *
1931 * Percpu allocator wants to use congruent vm areas so that it can
1932 * maintain the offsets among percpu areas. This function allocates
1933 * congruent vmalloc areas for it. These areas tend to be scattered
1934 * pretty far, distance between two areas easily going up to
1935 * gigabytes. To avoid interacting with regular vmallocs, these areas
1936 * are allocated from top.
1937 *
1938 * Despite its complicated look, this allocator is rather simple. It
1939 * does everything top-down and scans areas from the end looking for
1940 * matching slot. While scanning, if any of the areas overlaps with
1941 * existing vmap_area, the base address is pulled down to fit the
1942 * area. Scanning is repeated till all the areas fit and then all
1943 * necessary data structres are inserted and the result is returned.
1944 */
1945struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
1946 const size_t *sizes, int nr_vms,
1947 size_t align, gfp_t gfp_mask)
1948{
1949 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
1950 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
1951 struct vmap_area **vas, *prev, *next;
1952 struct vm_struct **vms;
1953 int area, area2, last_area, term_area;
1954 unsigned long base, start, end, last_end;
1955 bool purged = false;
1956
1957 gfp_mask &= GFP_RECLAIM_MASK;
1958
1959 /* verify parameters and allocate data structures */
1960 BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align));
1961 for (last_area = 0, area = 0; area < nr_vms; area++) {
1962 start = offsets[area];
1963 end = start + sizes[area];
1964
1965 /* is everything aligned properly? */
1966 BUG_ON(!IS_ALIGNED(offsets[area], align));
1967 BUG_ON(!IS_ALIGNED(sizes[area], align));
1968
1969 /* detect the area with the highest address */
1970 if (start > offsets[last_area])
1971 last_area = area;
1972
1973 for (area2 = 0; area2 < nr_vms; area2++) {
1974 unsigned long start2 = offsets[area2];
1975 unsigned long end2 = start2 + sizes[area2];
1976
1977 if (area2 == area)
1978 continue;
1979
1980 BUG_ON(start2 >= start && start2 < end);
1981 BUG_ON(end2 <= end && end2 > start);
1982 }
1983 }
1984 last_end = offsets[last_area] + sizes[last_area];
1985
1986 if (vmalloc_end - vmalloc_start < last_end) {
1987 WARN_ON(true);
1988 return NULL;
1989 }
1990
1991 vms = kzalloc(sizeof(vms[0]) * nr_vms, gfp_mask);
1992 vas = kzalloc(sizeof(vas[0]) * nr_vms, gfp_mask);
1993 if (!vas || !vms)
1994 goto err_free;
1995
1996 for (area = 0; area < nr_vms; area++) {
1997 vas[area] = kzalloc(sizeof(struct vmap_area), gfp_mask);
1998 vms[area] = kzalloc(sizeof(struct vm_struct), gfp_mask);
1999 if (!vas[area] || !vms[area])
2000 goto err_free;
2001 }
2002retry:
2003 spin_lock(&vmap_area_lock);
2004
2005 /* start scanning - we scan from the top, begin with the last area */
2006 area = term_area = last_area;
2007 start = offsets[area];
2008 end = start + sizes[area];
2009
2010 if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2011 base = vmalloc_end - last_end;
2012 goto found;
2013 }
2014 base = pvm_determine_end(&next, &prev, align) - end;
2015
2016 while (true) {
2017 BUG_ON(next && next->va_end <= base + end);
2018 BUG_ON(prev && prev->va_end > base + end);
2019
2020 /*
2021 * base might have underflowed, add last_end before
2022 * comparing.
2023 */
2024 if (base + last_end < vmalloc_start + last_end) {
2025 spin_unlock(&vmap_area_lock);
2026 if (!purged) {
2027 purge_vmap_area_lazy();
2028 purged = true;
2029 goto retry;
2030 }
2031 goto err_free;
2032 }
2033
2034 /*
2035 * If next overlaps, move base downwards so that it's
2036 * right below next and then recheck.
2037 */
2038 if (next && next->va_start < base + end) {
2039 base = pvm_determine_end(&next, &prev, align) - end;
2040 term_area = area;
2041 continue;
2042 }
2043
2044 /*
2045 * If prev overlaps, shift down next and prev and move
2046 * base so that it's right below new next and then
2047 * recheck.
2048 */
2049 if (prev && prev->va_end > base + start) {
2050 next = prev;
2051 prev = node_to_va(rb_prev(&next->rb_node));
2052 base = pvm_determine_end(&next, &prev, align) - end;
2053 term_area = area;
2054 continue;
2055 }
2056
2057 /*
2058 * This area fits, move on to the previous one. If
2059 * the previous one is the terminal one, we're done.
2060 */
2061 area = (area + nr_vms - 1) % nr_vms;
2062 if (area == term_area)
2063 break;
2064 start = offsets[area];
2065 end = start + sizes[area];
2066 pvm_find_next_prev(base + end, &next, &prev);
2067 }
2068found:
2069 /* we've found a fitting base, insert all va's */
2070 for (area = 0; area < nr_vms; area++) {
2071 struct vmap_area *va = vas[area];
2072
2073 va->va_start = base + offsets[area];
2074 va->va_end = va->va_start + sizes[area];
2075 __insert_vmap_area(va);
2076 }
2077
2078 vmap_area_pcpu_hole = base + offsets[last_area];
2079
2080 spin_unlock(&vmap_area_lock);
2081
2082 /* insert all vm's */
2083 for (area = 0; area < nr_vms; area++)
2084 insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2085 pcpu_get_vm_areas);
2086
2087 kfree(vas);
2088 return vms;
2089
2090err_free:
2091 for (area = 0; area < nr_vms; area++) {
2092 if (vas)
2093 kfree(vas[area]);
2094 if (vms)
2095 kfree(vms[area]);
2096 }
2097 kfree(vas);
2098 kfree(vms);
2099 return NULL;
2100}
2101
2102/**
2103 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2104 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2105 * @nr_vms: the number of allocated areas
2106 *
2107 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2108 */
2109void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2110{
2111 int i;
2112
2113 for (i = 0; i < nr_vms; i++)
2114 free_vm_area(vms[i]);
2115 kfree(vms);
2116}
Christoph Lametera10aa572008-04-28 02:12:40 -07002117
2118#ifdef CONFIG_PROC_FS
2119static void *s_start(struct seq_file *m, loff_t *pos)
2120{
2121 loff_t n = *pos;
2122 struct vm_struct *v;
2123
2124 read_lock(&vmlist_lock);
2125 v = vmlist;
2126 while (n > 0 && v) {
2127 n--;
2128 v = v->next;
2129 }
2130 if (!n)
2131 return v;
2132
2133 return NULL;
2134
2135}
2136
2137static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2138{
2139 struct vm_struct *v = p;
2140
2141 ++*pos;
2142 return v->next;
2143}
2144
2145static void s_stop(struct seq_file *m, void *p)
2146{
2147 read_unlock(&vmlist_lock);
2148}
2149
Eric Dumazeta47a1262008-07-23 21:27:38 -07002150static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2151{
2152 if (NUMA_BUILD) {
2153 unsigned int nr, *counters = m->private;
2154
2155 if (!counters)
2156 return;
2157
2158 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2159
2160 for (nr = 0; nr < v->nr_pages; nr++)
2161 counters[page_to_nid(v->pages[nr])]++;
2162
2163 for_each_node_state(nr, N_HIGH_MEMORY)
2164 if (counters[nr])
2165 seq_printf(m, " N%u=%u", nr, counters[nr]);
2166 }
2167}
2168
Christoph Lametera10aa572008-04-28 02:12:40 -07002169static int s_show(struct seq_file *m, void *p)
2170{
2171 struct vm_struct *v = p;
2172
2173 seq_printf(m, "0x%p-0x%p %7ld",
2174 v->addr, v->addr + v->size, v->size);
2175
Christoph Lameter23016962008-04-28 02:12:42 -07002176 if (v->caller) {
Hugh Dickins9c246242008-12-09 13:14:27 -08002177 char buff[KSYM_SYMBOL_LEN];
Christoph Lameter23016962008-04-28 02:12:42 -07002178
2179 seq_putc(m, ' ');
2180 sprint_symbol(buff, (unsigned long)v->caller);
2181 seq_puts(m, buff);
2182 }
2183
Christoph Lametera10aa572008-04-28 02:12:40 -07002184 if (v->nr_pages)
2185 seq_printf(m, " pages=%d", v->nr_pages);
2186
2187 if (v->phys_addr)
2188 seq_printf(m, " phys=%lx", v->phys_addr);
2189
2190 if (v->flags & VM_IOREMAP)
2191 seq_printf(m, " ioremap");
2192
2193 if (v->flags & VM_ALLOC)
2194 seq_printf(m, " vmalloc");
2195
2196 if (v->flags & VM_MAP)
2197 seq_printf(m, " vmap");
2198
2199 if (v->flags & VM_USERMAP)
2200 seq_printf(m, " user");
2201
2202 if (v->flags & VM_VPAGES)
2203 seq_printf(m, " vpages");
2204
Eric Dumazeta47a1262008-07-23 21:27:38 -07002205 show_numa_info(m, v);
Christoph Lametera10aa572008-04-28 02:12:40 -07002206 seq_putc(m, '\n');
2207 return 0;
2208}
2209
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002210static const struct seq_operations vmalloc_op = {
Christoph Lametera10aa572008-04-28 02:12:40 -07002211 .start = s_start,
2212 .next = s_next,
2213 .stop = s_stop,
2214 .show = s_show,
2215};
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002216
2217static int vmalloc_open(struct inode *inode, struct file *file)
2218{
2219 unsigned int *ptr = NULL;
2220 int ret;
2221
2222 if (NUMA_BUILD)
2223 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
2224 ret = seq_open(file, &vmalloc_op);
2225 if (!ret) {
2226 struct seq_file *m = file->private_data;
2227 m->private = ptr;
2228 } else
2229 kfree(ptr);
2230 return ret;
2231}
2232
2233static const struct file_operations proc_vmalloc_operations = {
2234 .open = vmalloc_open,
2235 .read = seq_read,
2236 .llseek = seq_lseek,
2237 .release = seq_release_private,
2238};
2239
2240static int __init proc_vmalloc_init(void)
2241{
2242 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
2243 return 0;
2244}
2245module_init(proc_vmalloc_init);
Christoph Lametera10aa572008-04-28 02:12:40 -07002246#endif
2247