blob: 2a39cf128aba2a7a583501a6686d9214070eb7bd [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
11#include <linux/mm.h>
12#include <linux/module.h>
13#include <linux/highmem.h>
14#include <linux/slab.h>
15#include <linux/spinlock.h>
16#include <linux/interrupt.h>
Christoph Lametera10aa572008-04-28 02:12:40 -070017#include <linux/seq_file.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070018#include <linux/debugobjects.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/vmalloc.h>
Christoph Lameter23016962008-04-28 02:12:42 -070020#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/uaccess.h>
23#include <asm/tlbflush.h>
24
25
26DEFINE_RWLOCK(vmlist_lock);
27struct vm_struct *vmlist;
28
Adrian Bunkb2213852006-09-25 23:31:02 -070029static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
Christoph Lameter23016962008-04-28 02:12:42 -070030 int node, void *caller);
Adrian Bunkb2213852006-09-25 23:31:02 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
33{
34 pte_t *pte;
35
36 pte = pte_offset_kernel(pmd, addr);
37 do {
38 pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
39 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
40 } while (pte++, addr += PAGE_SIZE, addr != end);
41}
42
43static inline void vunmap_pmd_range(pud_t *pud, unsigned long addr,
44 unsigned long end)
45{
46 pmd_t *pmd;
47 unsigned long next;
48
49 pmd = pmd_offset(pud, addr);
50 do {
51 next = pmd_addr_end(addr, end);
52 if (pmd_none_or_clear_bad(pmd))
53 continue;
54 vunmap_pte_range(pmd, addr, next);
55 } while (pmd++, addr = next, addr != end);
56}
57
58static inline void vunmap_pud_range(pgd_t *pgd, unsigned long addr,
59 unsigned long end)
60{
61 pud_t *pud;
62 unsigned long next;
63
64 pud = pud_offset(pgd, addr);
65 do {
66 next = pud_addr_end(addr, end);
67 if (pud_none_or_clear_bad(pud))
68 continue;
69 vunmap_pmd_range(pud, addr, next);
70 } while (pud++, addr = next, addr != end);
71}
72
Benjamin Herrenschmidtc19c03f2007-06-04 15:15:35 +100073void unmap_kernel_range(unsigned long addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 pgd_t *pgd;
76 unsigned long next;
Benjamin Herrenschmidtc19c03f2007-06-04 15:15:35 +100077 unsigned long start = addr;
78 unsigned long end = addr + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 BUG_ON(addr >= end);
81 pgd = pgd_offset_k(addr);
82 flush_cache_vunmap(addr, end);
83 do {
84 next = pgd_addr_end(addr, end);
85 if (pgd_none_or_clear_bad(pgd))
86 continue;
87 vunmap_pud_range(pgd, addr, next);
88 } while (pgd++, addr = next, addr != end);
Benjamin Herrenschmidtc19c03f2007-06-04 15:15:35 +100089 flush_tlb_kernel_range(start, end);
90}
91
92static void unmap_vm_area(struct vm_struct *area)
93{
94 unmap_kernel_range((unsigned long)area->addr, area->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
98 unsigned long end, pgprot_t prot, struct page ***pages)
99{
100 pte_t *pte;
101
Hugh Dickins872fec12005-10-29 18:16:21 -0700102 pte = pte_alloc_kernel(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (!pte)
104 return -ENOMEM;
105 do {
106 struct page *page = **pages;
107 WARN_ON(!pte_none(*pte));
108 if (!page)
109 return -ENOMEM;
110 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
111 (*pages)++;
112 } while (pte++, addr += PAGE_SIZE, addr != end);
113 return 0;
114}
115
116static inline int vmap_pmd_range(pud_t *pud, unsigned long addr,
117 unsigned long end, pgprot_t prot, struct page ***pages)
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);
127 if (vmap_pte_range(pmd, addr, next, prot, pages))
128 return -ENOMEM;
129 } while (pmd++, addr = next, addr != end);
130 return 0;
131}
132
133static inline int vmap_pud_range(pgd_t *pgd, unsigned long addr,
134 unsigned long end, pgprot_t prot, struct page ***pages)
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);
144 if (vmap_pmd_range(pud, addr, next, prot, pages))
145 return -ENOMEM;
146 } while (pud++, addr = next, addr != end);
147 return 0;
148}
149
150int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
151{
152 pgd_t *pgd;
153 unsigned long next;
154 unsigned long addr = (unsigned long) area->addr;
155 unsigned long end = addr + area->size - PAGE_SIZE;
156 int err;
157
158 BUG_ON(addr >= end);
159 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 do {
161 next = pgd_addr_end(addr, end);
162 err = vmap_pud_range(pgd, addr, next, prot, pages);
163 if (err)
164 break;
165 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 flush_cache_vmap((unsigned long) area->addr, end);
167 return err;
168}
Rusty Russell5992b6d2007-07-19 01:49:21 -0700169EXPORT_SYMBOL_GPL(map_vm_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Christoph Lameter48667e72008-02-04 22:28:31 -0800171/*
172 * Map a vmalloc()-space virtual address to the physical page.
173 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800174struct page *vmalloc_to_page(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800175{
176 unsigned long addr = (unsigned long) vmalloc_addr;
177 struct page *page = NULL;
178 pgd_t *pgd = pgd_offset_k(addr);
179 pud_t *pud;
180 pmd_t *pmd;
181 pte_t *ptep, pte;
182
183 if (!pgd_none(*pgd)) {
184 pud = pud_offset(pgd, addr);
185 if (!pud_none(*pud)) {
186 pmd = pmd_offset(pud, addr);
187 if (!pmd_none(*pmd)) {
188 ptep = pte_offset_map(pmd, addr);
189 pte = *ptep;
190 if (pte_present(pte))
191 page = pte_page(pte);
192 pte_unmap(ptep);
193 }
194 }
195 }
196 return page;
197}
198EXPORT_SYMBOL(vmalloc_to_page);
199
200/*
201 * Map a vmalloc()-space virtual address to the physical page frame number.
202 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800203unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800204{
205 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
206}
207EXPORT_SYMBOL(vmalloc_to_pfn);
208
Christoph Lameter23016962008-04-28 02:12:42 -0700209static struct vm_struct *
210__get_vm_area_node(unsigned long size, unsigned long flags, unsigned long start,
211 unsigned long end, int node, gfp_t gfp_mask, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct vm_struct **p, *tmp, *area;
214 unsigned long align = 1;
215 unsigned long addr;
216
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -0700217 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (flags & VM_IOREMAP) {
219 int bit = fls(size);
220
221 if (bit > IOREMAP_MAX_ORDER)
222 bit = IOREMAP_MAX_ORDER;
223 else if (bit < PAGE_SHIFT)
224 bit = PAGE_SHIFT;
225
226 align = 1ul << bit;
227 }
228 addr = ALIGN(start, align);
229 size = PAGE_ALIGN(size);
OGAWA Hirofumi31be8302006-11-16 01:19:29 -0800230 if (unlikely(!size))
231 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Christoph Lameter6cb06222007-10-16 01:25:41 -0700233 area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (unlikely(!area))
236 return NULL;
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /*
239 * We always allocate a guard page.
240 */
241 size += PAGE_SIZE;
242
243 write_lock(&vmlist_lock);
244 for (p = &vmlist; (tmp = *p) != NULL ;p = &tmp->next) {
245 if ((unsigned long)tmp->addr < addr) {
246 if((unsigned long)tmp->addr + tmp->size >= addr)
247 addr = ALIGN(tmp->size +
248 (unsigned long)tmp->addr, align);
249 continue;
250 }
251 if ((size + addr) < addr)
252 goto out;
253 if (size + addr <= (unsigned long)tmp->addr)
254 goto found;
255 addr = ALIGN(tmp->size + (unsigned long)tmp->addr, align);
256 if (addr > end - size)
257 goto out;
258 }
Robert Bragg5dc33182008-02-04 22:29:18 -0800259 if ((size + addr) < addr)
260 goto out;
261 if (addr > end - size)
262 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264found:
265 area->next = *p;
266 *p = area;
267
268 area->flags = flags;
269 area->addr = (void *)addr;
270 area->size = size;
271 area->pages = NULL;
272 area->nr_pages = 0;
273 area->phys_addr = 0;
Christoph Lameter23016962008-04-28 02:12:42 -0700274 area->caller = caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 write_unlock(&vmlist_lock);
276
277 return area;
278
279out:
280 write_unlock(&vmlist_lock);
281 kfree(area);
282 if (printk_ratelimit())
283 printk(KERN_WARNING "allocation failed: out of vmalloc space - use vmalloc=<size> to increase size.\n");
284 return NULL;
285}
286
Christoph Lameter930fc452005-10-29 18:15:41 -0700287struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
288 unsigned long start, unsigned long end)
289{
Christoph Lameter23016962008-04-28 02:12:42 -0700290 return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
291 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -0700292}
Rusty Russell5992b6d2007-07-19 01:49:21 -0700293EXPORT_SYMBOL_GPL(__get_vm_area);
Christoph Lameter930fc452005-10-29 18:15:41 -0700294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/**
Simon Arlott183ff222007-10-20 01:27:18 +0200296 * get_vm_area - reserve a contiguous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * @size: size of the area
298 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
299 *
300 * Search an area of @size in the kernel virtual mapping area,
301 * and reserved it for out purposes. Returns the area descriptor
302 * on success or %NULL on failure.
303 */
304struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
305{
Christoph Lameter23016962008-04-28 02:12:42 -0700306 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
307 -1, GFP_KERNEL, __builtin_return_address(0));
308}
309
310struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
311 void *caller)
312{
313 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
314 -1, GFP_KERNEL, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -0700317struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
318 int node, gfp_t gfp_mask)
Christoph Lameter930fc452005-10-29 18:15:41 -0700319{
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -0700320 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
Christoph Lameter23016962008-04-28 02:12:42 -0700321 gfp_mask, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -0700322}
323
Andi Kleen7856dfe2005-05-20 14:27:57 -0700324/* Caller must hold vmlist_lock */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800325static struct vm_struct *__find_vm_area(const void *addr)
Nick Piggin83342312006-06-23 02:03:20 -0700326{
327 struct vm_struct *tmp;
328
329 for (tmp = vmlist; tmp != NULL; tmp = tmp->next) {
330 if (tmp->addr == addr)
331 break;
332 }
333
334 return tmp;
335}
336
337/* Caller must hold vmlist_lock */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800338static struct vm_struct *__remove_vm_area(const void *addr)
Andi Kleen7856dfe2005-05-20 14:27:57 -0700339{
340 struct vm_struct **p, *tmp;
341
342 for (p = &vmlist ; (tmp = *p) != NULL ;p = &tmp->next) {
343 if (tmp->addr == addr)
344 goto found;
345 }
346 return NULL;
347
348found:
349 unmap_vm_area(tmp);
350 *p = tmp->next;
351
352 /*
353 * Remove the guard page.
354 */
355 tmp->size -= PAGE_SIZE;
356 return tmp;
357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/**
Simon Arlott183ff222007-10-20 01:27:18 +0200360 * remove_vm_area - find and remove a continuous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * @addr: base address
362 *
363 * Search for the kernel VM area starting at @addr, and remove it.
364 * This function returns the found VM area, but using it is NOT safe
Andi Kleen7856dfe2005-05-20 14:27:57 -0700365 * on SMP machines, except for its size or flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800367struct vm_struct *remove_vm_area(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Andi Kleen7856dfe2005-05-20 14:27:57 -0700369 struct vm_struct *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 write_lock(&vmlist_lock);
Andi Kleen7856dfe2005-05-20 14:27:57 -0700371 v = __remove_vm_area(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 write_unlock(&vmlist_lock);
Andi Kleen7856dfe2005-05-20 14:27:57 -0700373 return v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800376static void __vunmap(const void *addr, int deallocate_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct vm_struct *area;
379
380 if (!addr)
381 return;
382
383 if ((PAGE_SIZE-1) & (unsigned long)addr) {
384 printk(KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
385 WARN_ON(1);
386 return;
387 }
388
389 area = remove_vm_area(addr);
390 if (unlikely(!area)) {
391 printk(KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
392 addr);
393 WARN_ON(1);
394 return;
395 }
396
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700397 debug_check_no_locks_freed(addr, area->size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700398 debug_check_no_obj_freed(addr, area->size);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (deallocate_pages) {
401 int i;
402
403 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -0800404 struct page *page = area->pages[i];
405
406 BUG_ON(!page);
407 __free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
Jan Kiszka8757d5f2006-07-14 00:23:56 -0700410 if (area->flags & VM_VPAGES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 vfree(area->pages);
412 else
413 kfree(area->pages);
414 }
415
416 kfree(area);
417 return;
418}
419
420/**
421 * vfree - release memory allocated by vmalloc()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * @addr: memory base address
423 *
Simon Arlott183ff222007-10-20 01:27:18 +0200424 * Free the virtually continuous memory area starting at @addr, as
Pekka Enberg80e93ef2005-09-09 13:10:16 -0700425 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
426 * NULL, no operation is performed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -0700428 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800430void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 BUG_ON(in_interrupt());
433 __vunmap(addr, 1);
434}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435EXPORT_SYMBOL(vfree);
436
437/**
438 * vunmap - release virtual mapping obtained by vmap()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * @addr: memory base address
440 *
441 * Free the virtually contiguous memory area starting at @addr,
442 * which was created from the page array passed to vmap().
443 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -0700444 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800446void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 BUG_ON(in_interrupt());
449 __vunmap(addr, 0);
450}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451EXPORT_SYMBOL(vunmap);
452
453/**
454 * vmap - map an array of pages into virtually contiguous space
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * @pages: array of page pointers
456 * @count: number of pages to map
457 * @flags: vm_area->flags
458 * @prot: page protection for the mapping
459 *
460 * Maps @count pages from @pages into contiguous kernel virtual
461 * space.
462 */
463void *vmap(struct page **pages, unsigned int count,
464 unsigned long flags, pgprot_t prot)
465{
466 struct vm_struct *area;
467
468 if (count > num_physpages)
469 return NULL;
470
Christoph Lameter23016962008-04-28 02:12:42 -0700471 area = get_vm_area_caller((count << PAGE_SHIFT), flags,
472 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (!area)
474 return NULL;
Christoph Lameter23016962008-04-28 02:12:42 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (map_vm_area(area, prot, &pages)) {
477 vunmap(area->addr);
478 return NULL;
479 }
480
481 return area->addr;
482}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483EXPORT_SYMBOL(vmap);
484
Adrian Bunke31d9eb2008-02-04 22:29:09 -0800485static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
Christoph Lameter23016962008-04-28 02:12:42 -0700486 pgprot_t prot, int node, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct page **pages;
489 unsigned int nr_pages, array_size, i;
490
491 nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
492 array_size = (nr_pages * sizeof(struct page *));
493
494 area->nr_pages = nr_pages;
495 /* Please note that the recursion is strictly bounded. */
Jan Kiszka8757d5f2006-07-14 00:23:56 -0700496 if (array_size > PAGE_SIZE) {
Christoph Lameter94f60302007-07-17 04:03:29 -0700497 pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
Christoph Lameter23016962008-04-28 02:12:42 -0700498 PAGE_KERNEL, node, caller);
Jan Kiszka8757d5f2006-07-14 00:23:56 -0700499 area->flags |= VM_VPAGES;
Andrew Morton286e1ea2006-10-17 00:09:57 -0700500 } else {
501 pages = kmalloc_node(array_size,
Christoph Lameter6cb06222007-10-16 01:25:41 -0700502 (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
Andrew Morton286e1ea2006-10-17 00:09:57 -0700503 node);
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 area->pages = pages;
Christoph Lameter23016962008-04-28 02:12:42 -0700506 area->caller = caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!area->pages) {
508 remove_vm_area(area->addr);
509 kfree(area);
510 return NULL;
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -0800514 struct page *page;
515
Christoph Lameter930fc452005-10-29 18:15:41 -0700516 if (node < 0)
Christoph Lameterbf53d6f2008-02-04 22:28:34 -0800517 page = alloc_page(gfp_mask);
Christoph Lameter930fc452005-10-29 18:15:41 -0700518 else
Christoph Lameterbf53d6f2008-02-04 22:28:34 -0800519 page = alloc_pages_node(node, gfp_mask, 0);
520
521 if (unlikely(!page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Successfully allocated i pages, free them in __vunmap() */
523 area->nr_pages = i;
524 goto fail;
525 }
Christoph Lameterbf53d6f2008-02-04 22:28:34 -0800526 area->pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
529 if (map_vm_area(area, prot, &pages))
530 goto fail;
531 return area->addr;
532
533fail:
534 vfree(area->addr);
535 return NULL;
536}
537
Christoph Lameter930fc452005-10-29 18:15:41 -0700538void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
539{
Christoph Lameter23016962008-04-28 02:12:42 -0700540 return __vmalloc_area_node(area, gfp_mask, prot, -1,
541 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -0700542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/**
Christoph Lameter930fc452005-10-29 18:15:41 -0700545 * __vmalloc_node - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * @size: allocation size
547 * @gfp_mask: flags for the page level allocator
548 * @prot: protection mask for the allocated pages
Randy Dunlapd44e0782005-11-07 01:01:10 -0800549 * @node: node to use for allocation or -1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 *
551 * Allocate enough pages to cover @size from the page level
552 * allocator with @gfp_mask flags. Map them into contiguous
553 * kernel virtual space, using a pagetable protection of @prot.
554 */
Adrian Bunkb2213852006-09-25 23:31:02 -0700555static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
Christoph Lameter23016962008-04-28 02:12:42 -0700556 int node, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct vm_struct *area;
559
560 size = PAGE_ALIGN(size);
561 if (!size || (size >> PAGE_SHIFT) > num_physpages)
562 return NULL;
563
Christoph Lameter23016962008-04-28 02:12:42 -0700564 area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
565 node, gfp_mask, caller);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!area)
568 return NULL;
569
Christoph Lameter23016962008-04-28 02:12:42 -0700570 return __vmalloc_area_node(area, gfp_mask, prot, node, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Christoph Lameter930fc452005-10-29 18:15:41 -0700573void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
574{
Christoph Lameter23016962008-04-28 02:12:42 -0700575 return __vmalloc_node(size, gfp_mask, prot, -1,
576 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -0700577}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578EXPORT_SYMBOL(__vmalloc);
579
580/**
581 * vmalloc - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * Allocate enough pages to cover @size from the page level
584 * allocator and map them into contiguous kernel virtual space.
585 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200586 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * use __vmalloc() instead.
588 */
589void *vmalloc(unsigned long size)
590{
Christoph Lameter23016962008-04-28 02:12:42 -0700591 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
592 -1, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594EXPORT_SYMBOL(vmalloc);
595
Christoph Lameter930fc452005-10-29 18:15:41 -0700596/**
Rolf Eike Beeread04082006-09-27 01:50:13 -0700597 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
598 * @size: allocation size
Nick Piggin83342312006-06-23 02:03:20 -0700599 *
Rolf Eike Beeread04082006-09-27 01:50:13 -0700600 * The resulting memory area is zeroed so it can be mapped to userspace
601 * without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -0700602 */
603void *vmalloc_user(unsigned long size)
604{
605 struct vm_struct *area;
606 void *ret;
607
608 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
Eric Dumazet2b4ac442006-11-10 12:27:48 -0800609 if (ret) {
610 write_lock(&vmlist_lock);
611 area = __find_vm_area(ret);
612 area->flags |= VM_USERMAP;
613 write_unlock(&vmlist_lock);
614 }
Nick Piggin83342312006-06-23 02:03:20 -0700615 return ret;
616}
617EXPORT_SYMBOL(vmalloc_user);
618
619/**
Christoph Lameter930fc452005-10-29 18:15:41 -0700620 * vmalloc_node - allocate memory on a specific node
Christoph Lameter930fc452005-10-29 18:15:41 -0700621 * @size: allocation size
Randy Dunlapd44e0782005-11-07 01:01:10 -0800622 * @node: numa node
Christoph Lameter930fc452005-10-29 18:15:41 -0700623 *
624 * Allocate enough pages to cover @size from the page level
625 * allocator and map them into contiguous kernel virtual space.
626 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200627 * For tight control over page level allocator and protection flags
Christoph Lameter930fc452005-10-29 18:15:41 -0700628 * use __vmalloc() instead.
629 */
630void *vmalloc_node(unsigned long size, int node)
631{
Christoph Lameter23016962008-04-28 02:12:42 -0700632 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
633 node, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -0700634}
635EXPORT_SYMBOL(vmalloc_node);
636
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700637#ifndef PAGE_KERNEL_EXEC
638# define PAGE_KERNEL_EXEC PAGE_KERNEL
639#endif
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/**
642 * vmalloc_exec - allocate virtually contiguous, executable memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * @size: allocation size
644 *
645 * Kernel-internal function to allocate enough pages to cover @size
646 * the page level allocator and map them into contiguous and
647 * executable kernel virtual space.
648 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200649 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 * use __vmalloc() instead.
651 */
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653void *vmalloc_exec(unsigned long size)
654{
655 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
656}
657
Andi Kleen0d08e0d2007-05-02 19:27:12 +0200658#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
Benjamin Herrenschmidt7ac674f2007-07-19 01:49:10 -0700659#define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +0200660#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
Benjamin Herrenschmidt7ac674f2007-07-19 01:49:10 -0700661#define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +0200662#else
663#define GFP_VMALLOC32 GFP_KERNEL
664#endif
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/**
667 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * @size: allocation size
669 *
670 * Allocate enough 32bit PA addressable pages to cover @size from the
671 * page level allocator and map them into contiguous kernel virtual space.
672 */
673void *vmalloc_32(unsigned long size)
674{
Andi Kleen0d08e0d2007-05-02 19:27:12 +0200675 return __vmalloc(size, GFP_VMALLOC32, PAGE_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677EXPORT_SYMBOL(vmalloc_32);
678
Nick Piggin83342312006-06-23 02:03:20 -0700679/**
Rolf Eike Beeread04082006-09-27 01:50:13 -0700680 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
Nick Piggin83342312006-06-23 02:03:20 -0700681 * @size: allocation size
Rolf Eike Beeread04082006-09-27 01:50:13 -0700682 *
683 * The resulting memory area is 32bit addressable and zeroed so it can be
684 * mapped to userspace without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -0700685 */
686void *vmalloc_32_user(unsigned long size)
687{
688 struct vm_struct *area;
689 void *ret;
690
Andi Kleen0d08e0d2007-05-02 19:27:12 +0200691 ret = __vmalloc(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL);
Eric Dumazet2b4ac442006-11-10 12:27:48 -0800692 if (ret) {
693 write_lock(&vmlist_lock);
694 area = __find_vm_area(ret);
695 area->flags |= VM_USERMAP;
696 write_unlock(&vmlist_lock);
697 }
Nick Piggin83342312006-06-23 02:03:20 -0700698 return ret;
699}
700EXPORT_SYMBOL(vmalloc_32_user);
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702long vread(char *buf, char *addr, unsigned long count)
703{
704 struct vm_struct *tmp;
705 char *vaddr, *buf_start = buf;
706 unsigned long n;
707
708 /* Don't allow overflow */
709 if ((unsigned long) addr + count < count)
710 count = -(unsigned long) addr;
711
712 read_lock(&vmlist_lock);
713 for (tmp = vmlist; tmp; tmp = tmp->next) {
714 vaddr = (char *) tmp->addr;
715 if (addr >= vaddr + tmp->size - PAGE_SIZE)
716 continue;
717 while (addr < vaddr) {
718 if (count == 0)
719 goto finished;
720 *buf = '\0';
721 buf++;
722 addr++;
723 count--;
724 }
725 n = vaddr + tmp->size - PAGE_SIZE - addr;
726 do {
727 if (count == 0)
728 goto finished;
729 *buf = *addr;
730 buf++;
731 addr++;
732 count--;
733 } while (--n > 0);
734 }
735finished:
736 read_unlock(&vmlist_lock);
737 return buf - buf_start;
738}
739
740long vwrite(char *buf, char *addr, unsigned long count)
741{
742 struct vm_struct *tmp;
743 char *vaddr, *buf_start = buf;
744 unsigned long n;
745
746 /* Don't allow overflow */
747 if ((unsigned long) addr + count < count)
748 count = -(unsigned long) addr;
749
750 read_lock(&vmlist_lock);
751 for (tmp = vmlist; tmp; tmp = tmp->next) {
752 vaddr = (char *) tmp->addr;
753 if (addr >= vaddr + tmp->size - PAGE_SIZE)
754 continue;
755 while (addr < vaddr) {
756 if (count == 0)
757 goto finished;
758 buf++;
759 addr++;
760 count--;
761 }
762 n = vaddr + tmp->size - PAGE_SIZE - addr;
763 do {
764 if (count == 0)
765 goto finished;
766 *addr = *buf;
767 buf++;
768 addr++;
769 count--;
770 } while (--n > 0);
771 }
772finished:
773 read_unlock(&vmlist_lock);
774 return buf - buf_start;
775}
Nick Piggin83342312006-06-23 02:03:20 -0700776
777/**
778 * remap_vmalloc_range - map vmalloc pages to userspace
Nick Piggin83342312006-06-23 02:03:20 -0700779 * @vma: vma to cover (map full range of vma)
780 * @addr: vmalloc memory
781 * @pgoff: number of pages into addr before first page to map
Randy Dunlap76824862008-03-19 17:00:40 -0700782 *
783 * Returns: 0 for success, -Exxx on failure
Nick Piggin83342312006-06-23 02:03:20 -0700784 *
785 * This function checks that addr is a valid vmalloc'ed area, and
786 * that it is big enough to cover the vma. Will return failure if
787 * that criteria isn't met.
788 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800789 * Similar to remap_pfn_range() (see mm/memory.c)
Nick Piggin83342312006-06-23 02:03:20 -0700790 */
791int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
792 unsigned long pgoff)
793{
794 struct vm_struct *area;
795 unsigned long uaddr = vma->vm_start;
796 unsigned long usize = vma->vm_end - vma->vm_start;
797 int ret;
798
799 if ((PAGE_SIZE-1) & (unsigned long)addr)
800 return -EINVAL;
801
802 read_lock(&vmlist_lock);
803 area = __find_vm_area(addr);
804 if (!area)
805 goto out_einval_locked;
806
807 if (!(area->flags & VM_USERMAP))
808 goto out_einval_locked;
809
810 if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
811 goto out_einval_locked;
812 read_unlock(&vmlist_lock);
813
814 addr += pgoff << PAGE_SHIFT;
815 do {
816 struct page *page = vmalloc_to_page(addr);
817 ret = vm_insert_page(vma, uaddr, page);
818 if (ret)
819 return ret;
820
821 uaddr += PAGE_SIZE;
822 addr += PAGE_SIZE;
823 usize -= PAGE_SIZE;
824 } while (usize > 0);
825
826 /* Prevent "things" like memory migration? VM_flags need a cleanup... */
827 vma->vm_flags |= VM_RESERVED;
828
829 return ret;
830
831out_einval_locked:
832 read_unlock(&vmlist_lock);
833 return -EINVAL;
834}
835EXPORT_SYMBOL(remap_vmalloc_range);
836
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700837/*
838 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
839 * have one.
840 */
841void __attribute__((weak)) vmalloc_sync_all(void)
842{
843}
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -0700844
845
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800846static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -0700847{
848 /* apply_to_page_range() does all the hard work. */
849 return 0;
850}
851
852/**
853 * alloc_vm_area - allocate a range of kernel address space
854 * @size: size of the area
Randy Dunlap76824862008-03-19 17:00:40 -0700855 *
856 * Returns: NULL on failure, vm_struct on success
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -0700857 *
858 * This function reserves a range of kernel address space, and
859 * allocates pagetables to map that range. No actual mappings
860 * are created. If the kernel address space is not shared
861 * between processes, it syncs the pagetable across all
862 * processes.
863 */
864struct vm_struct *alloc_vm_area(size_t size)
865{
866 struct vm_struct *area;
867
Christoph Lameter23016962008-04-28 02:12:42 -0700868 area = get_vm_area_caller(size, VM_IOREMAP,
869 __builtin_return_address(0));
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -0700870 if (area == NULL)
871 return NULL;
872
873 /*
874 * This ensures that page tables are constructed for this region
875 * of kernel virtual address space and mapped into init_mm.
876 */
877 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
878 area->size, f, NULL)) {
879 free_vm_area(area);
880 return NULL;
881 }
882
883 /* Make sure the pagetables are constructed in process kernel
884 mappings */
885 vmalloc_sync_all();
886
887 return area;
888}
889EXPORT_SYMBOL_GPL(alloc_vm_area);
890
891void free_vm_area(struct vm_struct *area)
892{
893 struct vm_struct *ret;
894 ret = remove_vm_area(area->addr);
895 BUG_ON(ret != area);
896 kfree(area);
897}
898EXPORT_SYMBOL_GPL(free_vm_area);
Christoph Lametera10aa572008-04-28 02:12:40 -0700899
900
901#ifdef CONFIG_PROC_FS
902static void *s_start(struct seq_file *m, loff_t *pos)
903{
904 loff_t n = *pos;
905 struct vm_struct *v;
906
907 read_lock(&vmlist_lock);
908 v = vmlist;
909 while (n > 0 && v) {
910 n--;
911 v = v->next;
912 }
913 if (!n)
914 return v;
915
916 return NULL;
917
918}
919
920static void *s_next(struct seq_file *m, void *p, loff_t *pos)
921{
922 struct vm_struct *v = p;
923
924 ++*pos;
925 return v->next;
926}
927
928static void s_stop(struct seq_file *m, void *p)
929{
930 read_unlock(&vmlist_lock);
931}
932
933static int s_show(struct seq_file *m, void *p)
934{
935 struct vm_struct *v = p;
936
937 seq_printf(m, "0x%p-0x%p %7ld",
938 v->addr, v->addr + v->size, v->size);
939
Christoph Lameter23016962008-04-28 02:12:42 -0700940 if (v->caller) {
941 char buff[2 * KSYM_NAME_LEN];
942
943 seq_putc(m, ' ');
944 sprint_symbol(buff, (unsigned long)v->caller);
945 seq_puts(m, buff);
946 }
947
Christoph Lametera10aa572008-04-28 02:12:40 -0700948 if (v->nr_pages)
949 seq_printf(m, " pages=%d", v->nr_pages);
950
951 if (v->phys_addr)
952 seq_printf(m, " phys=%lx", v->phys_addr);
953
954 if (v->flags & VM_IOREMAP)
955 seq_printf(m, " ioremap");
956
957 if (v->flags & VM_ALLOC)
958 seq_printf(m, " vmalloc");
959
960 if (v->flags & VM_MAP)
961 seq_printf(m, " vmap");
962
963 if (v->flags & VM_USERMAP)
964 seq_printf(m, " user");
965
966 if (v->flags & VM_VPAGES)
967 seq_printf(m, " vpages");
968
969 seq_putc(m, '\n');
970 return 0;
971}
972
973const struct seq_operations vmalloc_op = {
974 .start = s_start,
975 .next = s_next,
976 .stop = s_stop,
977 .show = s_show,
978};
979#endif
980