blob: 830a5580c5d742b22040bda2f97a499fbb024c31 [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
Ingo Molnar7aa413d2008-06-19 13:28:11 +0200183 /*
184 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
185 * architectures that do not vmalloc module space
186 */
Jiri Slaby59ea7462008-06-12 13:56:40 +0200187 VIRTUAL_BUG_ON(!is_vmalloc_addr(vmalloc_addr) &&
188 !is_module_address(addr));
189
Christoph Lameter48667e72008-02-04 22:28:31 -0800190 if (!pgd_none(*pgd)) {
191 pud = pud_offset(pgd, addr);
192 if (!pud_none(*pud)) {
193 pmd = pmd_offset(pud, addr);
194 if (!pmd_none(*pmd)) {
195 ptep = pte_offset_map(pmd, addr);
196 pte = *ptep;
197 if (pte_present(pte))
198 page = pte_page(pte);
199 pte_unmap(ptep);
200 }
201 }
202 }
203 return page;
204}
205EXPORT_SYMBOL(vmalloc_to_page);
206
207/*
208 * Map a vmalloc()-space virtual address to the physical page frame number.
209 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800210unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800211{
212 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
213}
214EXPORT_SYMBOL(vmalloc_to_pfn);
215
Christoph Lameter23016962008-04-28 02:12:42 -0700216static struct vm_struct *
217__get_vm_area_node(unsigned long size, unsigned long flags, unsigned long start,
218 unsigned long end, int node, gfp_t gfp_mask, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct vm_struct **p, *tmp, *area;
221 unsigned long align = 1;
222 unsigned long addr;
223
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -0700224 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (flags & VM_IOREMAP) {
226 int bit = fls(size);
227
228 if (bit > IOREMAP_MAX_ORDER)
229 bit = IOREMAP_MAX_ORDER;
230 else if (bit < PAGE_SHIFT)
231 bit = PAGE_SHIFT;
232
233 align = 1ul << bit;
234 }
235 addr = ALIGN(start, align);
236 size = PAGE_ALIGN(size);
OGAWA Hirofumi31be8302006-11-16 01:19:29 -0800237 if (unlikely(!size))
238 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Christoph Lameter6cb06222007-10-16 01:25:41 -0700240 area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (unlikely(!area))
243 return NULL;
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /*
246 * We always allocate a guard page.
247 */
248 size += PAGE_SIZE;
249
250 write_lock(&vmlist_lock);
251 for (p = &vmlist; (tmp = *p) != NULL ;p = &tmp->next) {
252 if ((unsigned long)tmp->addr < addr) {
253 if((unsigned long)tmp->addr + tmp->size >= addr)
254 addr = ALIGN(tmp->size +
255 (unsigned long)tmp->addr, align);
256 continue;
257 }
258 if ((size + addr) < addr)
259 goto out;
260 if (size + addr <= (unsigned long)tmp->addr)
261 goto found;
262 addr = ALIGN(tmp->size + (unsigned long)tmp->addr, align);
263 if (addr > end - size)
264 goto out;
265 }
Robert Bragg5dc33182008-02-04 22:29:18 -0800266 if ((size + addr) < addr)
267 goto out;
268 if (addr > end - size)
269 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271found:
272 area->next = *p;
273 *p = area;
274
275 area->flags = flags;
276 area->addr = (void *)addr;
277 area->size = size;
278 area->pages = NULL;
279 area->nr_pages = 0;
280 area->phys_addr = 0;
Christoph Lameter23016962008-04-28 02:12:42 -0700281 area->caller = caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 write_unlock(&vmlist_lock);
283
284 return area;
285
286out:
287 write_unlock(&vmlist_lock);
288 kfree(area);
289 if (printk_ratelimit())
290 printk(KERN_WARNING "allocation failed: out of vmalloc space - use vmalloc=<size> to increase size.\n");
291 return NULL;
292}
293
Christoph Lameter930fc452005-10-29 18:15:41 -0700294struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
295 unsigned long start, unsigned long end)
296{
Christoph Lameter23016962008-04-28 02:12:42 -0700297 return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
298 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -0700299}
Rusty Russell5992b6d2007-07-19 01:49:21 -0700300EXPORT_SYMBOL_GPL(__get_vm_area);
Christoph Lameter930fc452005-10-29 18:15:41 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/**
Simon Arlott183ff222007-10-20 01:27:18 +0200303 * get_vm_area - reserve a contiguous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * @size: size of the area
305 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
306 *
307 * Search an area of @size in the kernel virtual mapping area,
308 * and reserved it for out purposes. Returns the area descriptor
309 * on success or %NULL on failure.
310 */
311struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
312{
Christoph Lameter23016962008-04-28 02:12:42 -0700313 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
314 -1, GFP_KERNEL, __builtin_return_address(0));
315}
316
317struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
318 void *caller)
319{
320 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
321 -1, GFP_KERNEL, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -0700324struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
325 int node, gfp_t gfp_mask)
Christoph Lameter930fc452005-10-29 18:15:41 -0700326{
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -0700327 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
Christoph Lameter23016962008-04-28 02:12:42 -0700328 gfp_mask, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -0700329}
330
Andi Kleen7856dfe2005-05-20 14:27:57 -0700331/* Caller must hold vmlist_lock */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800332static struct vm_struct *__find_vm_area(const void *addr)
Nick Piggin83342312006-06-23 02:03:20 -0700333{
334 struct vm_struct *tmp;
335
336 for (tmp = vmlist; tmp != NULL; tmp = tmp->next) {
337 if (tmp->addr == addr)
338 break;
339 }
340
341 return tmp;
342}
343
344/* Caller must hold vmlist_lock */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800345static struct vm_struct *__remove_vm_area(const void *addr)
Andi Kleen7856dfe2005-05-20 14:27:57 -0700346{
347 struct vm_struct **p, *tmp;
348
349 for (p = &vmlist ; (tmp = *p) != NULL ;p = &tmp->next) {
350 if (tmp->addr == addr)
351 goto found;
352 }
353 return NULL;
354
355found:
356 unmap_vm_area(tmp);
357 *p = tmp->next;
358
359 /*
360 * Remove the guard page.
361 */
362 tmp->size -= PAGE_SIZE;
363 return tmp;
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/**
Simon Arlott183ff222007-10-20 01:27:18 +0200367 * remove_vm_area - find and remove a continuous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * @addr: base address
369 *
370 * Search for the kernel VM area starting at @addr, and remove it.
371 * This function returns the found VM area, but using it is NOT safe
Andi Kleen7856dfe2005-05-20 14:27:57 -0700372 * on SMP machines, except for its size or flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800374struct vm_struct *remove_vm_area(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Andi Kleen7856dfe2005-05-20 14:27:57 -0700376 struct vm_struct *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 write_lock(&vmlist_lock);
Andi Kleen7856dfe2005-05-20 14:27:57 -0700378 v = __remove_vm_area(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 write_unlock(&vmlist_lock);
Andi Kleen7856dfe2005-05-20 14:27:57 -0700380 return v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800383static void __vunmap(const void *addr, int deallocate_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct vm_struct *area;
386
387 if (!addr)
388 return;
389
390 if ((PAGE_SIZE-1) & (unsigned long)addr) {
391 printk(KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
392 WARN_ON(1);
393 return;
394 }
395
396 area = remove_vm_area(addr);
397 if (unlikely(!area)) {
398 printk(KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
399 addr);
400 WARN_ON(1);
401 return;
402 }
403
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700404 debug_check_no_locks_freed(addr, area->size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700405 debug_check_no_obj_freed(addr, area->size);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (deallocate_pages) {
408 int i;
409
410 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -0800411 struct page *page = area->pages[i];
412
413 BUG_ON(!page);
414 __free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
Jan Kiszka8757d5f2006-07-14 00:23:56 -0700417 if (area->flags & VM_VPAGES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 vfree(area->pages);
419 else
420 kfree(area->pages);
421 }
422
423 kfree(area);
424 return;
425}
426
427/**
428 * vfree - release memory allocated by vmalloc()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * @addr: memory base address
430 *
Simon Arlott183ff222007-10-20 01:27:18 +0200431 * Free the virtually continuous memory area starting at @addr, as
Pekka Enberg80e93ef2005-09-09 13:10:16 -0700432 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
433 * NULL, no operation is performed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -0700435 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800437void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 BUG_ON(in_interrupt());
440 __vunmap(addr, 1);
441}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442EXPORT_SYMBOL(vfree);
443
444/**
445 * vunmap - release virtual mapping obtained by vmap()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * @addr: memory base address
447 *
448 * Free the virtually contiguous memory area starting at @addr,
449 * which was created from the page array passed to vmap().
450 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -0700451 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800453void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 BUG_ON(in_interrupt());
456 __vunmap(addr, 0);
457}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458EXPORT_SYMBOL(vunmap);
459
460/**
461 * vmap - map an array of pages into virtually contiguous space
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 * @pages: array of page pointers
463 * @count: number of pages to map
464 * @flags: vm_area->flags
465 * @prot: page protection for the mapping
466 *
467 * Maps @count pages from @pages into contiguous kernel virtual
468 * space.
469 */
470void *vmap(struct page **pages, unsigned int count,
471 unsigned long flags, pgprot_t prot)
472{
473 struct vm_struct *area;
474
475 if (count > num_physpages)
476 return NULL;
477
Christoph Lameter23016962008-04-28 02:12:42 -0700478 area = get_vm_area_caller((count << PAGE_SHIFT), flags,
479 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (!area)
481 return NULL;
Christoph Lameter23016962008-04-28 02:12:42 -0700482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (map_vm_area(area, prot, &pages)) {
484 vunmap(area->addr);
485 return NULL;
486 }
487
488 return area->addr;
489}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490EXPORT_SYMBOL(vmap);
491
Adrian Bunke31d9eb2008-02-04 22:29:09 -0800492static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
Christoph Lameter23016962008-04-28 02:12:42 -0700493 pgprot_t prot, int node, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct page **pages;
496 unsigned int nr_pages, array_size, i;
497
498 nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
499 array_size = (nr_pages * sizeof(struct page *));
500
501 area->nr_pages = nr_pages;
502 /* Please note that the recursion is strictly bounded. */
Jan Kiszka8757d5f2006-07-14 00:23:56 -0700503 if (array_size > PAGE_SIZE) {
Christoph Lameter94f60302007-07-17 04:03:29 -0700504 pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
Christoph Lameter23016962008-04-28 02:12:42 -0700505 PAGE_KERNEL, node, caller);
Jan Kiszka8757d5f2006-07-14 00:23:56 -0700506 area->flags |= VM_VPAGES;
Andrew Morton286e1ea2006-10-17 00:09:57 -0700507 } else {
508 pages = kmalloc_node(array_size,
Christoph Lameter6cb06222007-10-16 01:25:41 -0700509 (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
Andrew Morton286e1ea2006-10-17 00:09:57 -0700510 node);
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 area->pages = pages;
Christoph Lameter23016962008-04-28 02:12:42 -0700513 area->caller = caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!area->pages) {
515 remove_vm_area(area->addr);
516 kfree(area);
517 return NULL;
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -0800521 struct page *page;
522
Christoph Lameter930fc452005-10-29 18:15:41 -0700523 if (node < 0)
Christoph Lameterbf53d6f2008-02-04 22:28:34 -0800524 page = alloc_page(gfp_mask);
Christoph Lameter930fc452005-10-29 18:15:41 -0700525 else
Christoph Lameterbf53d6f2008-02-04 22:28:34 -0800526 page = alloc_pages_node(node, gfp_mask, 0);
527
528 if (unlikely(!page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* Successfully allocated i pages, free them in __vunmap() */
530 area->nr_pages = i;
531 goto fail;
532 }
Christoph Lameterbf53d6f2008-02-04 22:28:34 -0800533 area->pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
536 if (map_vm_area(area, prot, &pages))
537 goto fail;
538 return area->addr;
539
540fail:
541 vfree(area->addr);
542 return NULL;
543}
544
Christoph Lameter930fc452005-10-29 18:15:41 -0700545void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
546{
Christoph Lameter23016962008-04-28 02:12:42 -0700547 return __vmalloc_area_node(area, gfp_mask, prot, -1,
548 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -0700549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/**
Christoph Lameter930fc452005-10-29 18:15:41 -0700552 * __vmalloc_node - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * @size: allocation size
554 * @gfp_mask: flags for the page level allocator
555 * @prot: protection mask for the allocated pages
Randy Dunlapd44e0782005-11-07 01:01:10 -0800556 * @node: node to use for allocation or -1
Randy Dunlapc85d1942008-05-01 04:34:48 -0700557 * @caller: caller's return address
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 *
559 * Allocate enough pages to cover @size from the page level
560 * allocator with @gfp_mask flags. Map them into contiguous
561 * kernel virtual space, using a pagetable protection of @prot.
562 */
Adrian Bunkb2213852006-09-25 23:31:02 -0700563static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
Christoph Lameter23016962008-04-28 02:12:42 -0700564 int node, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct vm_struct *area;
567
568 size = PAGE_ALIGN(size);
569 if (!size || (size >> PAGE_SHIFT) > num_physpages)
570 return NULL;
571
Christoph Lameter23016962008-04-28 02:12:42 -0700572 area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
573 node, gfp_mask, caller);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (!area)
576 return NULL;
577
Christoph Lameter23016962008-04-28 02:12:42 -0700578 return __vmalloc_area_node(area, gfp_mask, prot, node, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Christoph Lameter930fc452005-10-29 18:15:41 -0700581void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
582{
Christoph Lameter23016962008-04-28 02:12:42 -0700583 return __vmalloc_node(size, gfp_mask, prot, -1,
584 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -0700585}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586EXPORT_SYMBOL(__vmalloc);
587
588/**
589 * vmalloc - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * Allocate enough pages to cover @size from the page level
592 * allocator and map them into contiguous kernel virtual space.
593 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200594 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * use __vmalloc() instead.
596 */
597void *vmalloc(unsigned long size)
598{
Christoph Lameter23016962008-04-28 02:12:42 -0700599 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
600 -1, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602EXPORT_SYMBOL(vmalloc);
603
Christoph Lameter930fc452005-10-29 18:15:41 -0700604/**
Rolf Eike Beeread04082006-09-27 01:50:13 -0700605 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
606 * @size: allocation size
Nick Piggin83342312006-06-23 02:03:20 -0700607 *
Rolf Eike Beeread04082006-09-27 01:50:13 -0700608 * The resulting memory area is zeroed so it can be mapped to userspace
609 * without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -0700610 */
611void *vmalloc_user(unsigned long size)
612{
613 struct vm_struct *area;
614 void *ret;
615
616 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
Eric Dumazet2b4ac442006-11-10 12:27:48 -0800617 if (ret) {
618 write_lock(&vmlist_lock);
619 area = __find_vm_area(ret);
620 area->flags |= VM_USERMAP;
621 write_unlock(&vmlist_lock);
622 }
Nick Piggin83342312006-06-23 02:03:20 -0700623 return ret;
624}
625EXPORT_SYMBOL(vmalloc_user);
626
627/**
Christoph Lameter930fc452005-10-29 18:15:41 -0700628 * vmalloc_node - allocate memory on a specific node
Christoph Lameter930fc452005-10-29 18:15:41 -0700629 * @size: allocation size
Randy Dunlapd44e0782005-11-07 01:01:10 -0800630 * @node: numa node
Christoph Lameter930fc452005-10-29 18:15:41 -0700631 *
632 * Allocate enough pages to cover @size from the page level
633 * allocator and map them into contiguous kernel virtual space.
634 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200635 * For tight control over page level allocator and protection flags
Christoph Lameter930fc452005-10-29 18:15:41 -0700636 * use __vmalloc() instead.
637 */
638void *vmalloc_node(unsigned long size, int node)
639{
Christoph Lameter23016962008-04-28 02:12:42 -0700640 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
641 node, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -0700642}
643EXPORT_SYMBOL(vmalloc_node);
644
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700645#ifndef PAGE_KERNEL_EXEC
646# define PAGE_KERNEL_EXEC PAGE_KERNEL
647#endif
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/**
650 * vmalloc_exec - allocate virtually contiguous, executable memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * @size: allocation size
652 *
653 * Kernel-internal function to allocate enough pages to cover @size
654 * the page level allocator and map them into contiguous and
655 * executable kernel virtual space.
656 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200657 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 * use __vmalloc() instead.
659 */
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661void *vmalloc_exec(unsigned long size)
662{
663 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
664}
665
Andi Kleen0d08e0d2007-05-02 19:27:12 +0200666#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
Benjamin Herrenschmidt7ac674f2007-07-19 01:49:10 -0700667#define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +0200668#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
Benjamin Herrenschmidt7ac674f2007-07-19 01:49:10 -0700669#define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +0200670#else
671#define GFP_VMALLOC32 GFP_KERNEL
672#endif
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674/**
675 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 * @size: allocation size
677 *
678 * Allocate enough 32bit PA addressable pages to cover @size from the
679 * page level allocator and map them into contiguous kernel virtual space.
680 */
681void *vmalloc_32(unsigned long size)
682{
Andi Kleen0d08e0d2007-05-02 19:27:12 +0200683 return __vmalloc(size, GFP_VMALLOC32, PAGE_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685EXPORT_SYMBOL(vmalloc_32);
686
Nick Piggin83342312006-06-23 02:03:20 -0700687/**
Rolf Eike Beeread04082006-09-27 01:50:13 -0700688 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
Nick Piggin83342312006-06-23 02:03:20 -0700689 * @size: allocation size
Rolf Eike Beeread04082006-09-27 01:50:13 -0700690 *
691 * The resulting memory area is 32bit addressable and zeroed so it can be
692 * mapped to userspace without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -0700693 */
694void *vmalloc_32_user(unsigned long size)
695{
696 struct vm_struct *area;
697 void *ret;
698
Andi Kleen0d08e0d2007-05-02 19:27:12 +0200699 ret = __vmalloc(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL);
Eric Dumazet2b4ac442006-11-10 12:27:48 -0800700 if (ret) {
701 write_lock(&vmlist_lock);
702 area = __find_vm_area(ret);
703 area->flags |= VM_USERMAP;
704 write_unlock(&vmlist_lock);
705 }
Nick Piggin83342312006-06-23 02:03:20 -0700706 return ret;
707}
708EXPORT_SYMBOL(vmalloc_32_user);
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710long vread(char *buf, char *addr, unsigned long count)
711{
712 struct vm_struct *tmp;
713 char *vaddr, *buf_start = buf;
714 unsigned long n;
715
716 /* Don't allow overflow */
717 if ((unsigned long) addr + count < count)
718 count = -(unsigned long) addr;
719
720 read_lock(&vmlist_lock);
721 for (tmp = vmlist; tmp; tmp = tmp->next) {
722 vaddr = (char *) tmp->addr;
723 if (addr >= vaddr + tmp->size - PAGE_SIZE)
724 continue;
725 while (addr < vaddr) {
726 if (count == 0)
727 goto finished;
728 *buf = '\0';
729 buf++;
730 addr++;
731 count--;
732 }
733 n = vaddr + tmp->size - PAGE_SIZE - addr;
734 do {
735 if (count == 0)
736 goto finished;
737 *buf = *addr;
738 buf++;
739 addr++;
740 count--;
741 } while (--n > 0);
742 }
743finished:
744 read_unlock(&vmlist_lock);
745 return buf - buf_start;
746}
747
748long vwrite(char *buf, char *addr, unsigned long count)
749{
750 struct vm_struct *tmp;
751 char *vaddr, *buf_start = buf;
752 unsigned long n;
753
754 /* Don't allow overflow */
755 if ((unsigned long) addr + count < count)
756 count = -(unsigned long) addr;
757
758 read_lock(&vmlist_lock);
759 for (tmp = vmlist; tmp; tmp = tmp->next) {
760 vaddr = (char *) tmp->addr;
761 if (addr >= vaddr + tmp->size - PAGE_SIZE)
762 continue;
763 while (addr < vaddr) {
764 if (count == 0)
765 goto finished;
766 buf++;
767 addr++;
768 count--;
769 }
770 n = vaddr + tmp->size - PAGE_SIZE - addr;
771 do {
772 if (count == 0)
773 goto finished;
774 *addr = *buf;
775 buf++;
776 addr++;
777 count--;
778 } while (--n > 0);
779 }
780finished:
781 read_unlock(&vmlist_lock);
782 return buf - buf_start;
783}
Nick Piggin83342312006-06-23 02:03:20 -0700784
785/**
786 * remap_vmalloc_range - map vmalloc pages to userspace
Nick Piggin83342312006-06-23 02:03:20 -0700787 * @vma: vma to cover (map full range of vma)
788 * @addr: vmalloc memory
789 * @pgoff: number of pages into addr before first page to map
Randy Dunlap76824862008-03-19 17:00:40 -0700790 *
791 * Returns: 0 for success, -Exxx on failure
Nick Piggin83342312006-06-23 02:03:20 -0700792 *
793 * This function checks that addr is a valid vmalloc'ed area, and
794 * that it is big enough to cover the vma. Will return failure if
795 * that criteria isn't met.
796 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800797 * Similar to remap_pfn_range() (see mm/memory.c)
Nick Piggin83342312006-06-23 02:03:20 -0700798 */
799int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
800 unsigned long pgoff)
801{
802 struct vm_struct *area;
803 unsigned long uaddr = vma->vm_start;
804 unsigned long usize = vma->vm_end - vma->vm_start;
805 int ret;
806
807 if ((PAGE_SIZE-1) & (unsigned long)addr)
808 return -EINVAL;
809
810 read_lock(&vmlist_lock);
811 area = __find_vm_area(addr);
812 if (!area)
813 goto out_einval_locked;
814
815 if (!(area->flags & VM_USERMAP))
816 goto out_einval_locked;
817
818 if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
819 goto out_einval_locked;
820 read_unlock(&vmlist_lock);
821
822 addr += pgoff << PAGE_SHIFT;
823 do {
824 struct page *page = vmalloc_to_page(addr);
825 ret = vm_insert_page(vma, uaddr, page);
826 if (ret)
827 return ret;
828
829 uaddr += PAGE_SIZE;
830 addr += PAGE_SIZE;
831 usize -= PAGE_SIZE;
832 } while (usize > 0);
833
834 /* Prevent "things" like memory migration? VM_flags need a cleanup... */
835 vma->vm_flags |= VM_RESERVED;
836
837 return ret;
838
839out_einval_locked:
840 read_unlock(&vmlist_lock);
841 return -EINVAL;
842}
843EXPORT_SYMBOL(remap_vmalloc_range);
844
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700845/*
846 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
847 * have one.
848 */
849void __attribute__((weak)) vmalloc_sync_all(void)
850{
851}
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -0700852
853
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800854static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -0700855{
856 /* apply_to_page_range() does all the hard work. */
857 return 0;
858}
859
860/**
861 * alloc_vm_area - allocate a range of kernel address space
862 * @size: size of the area
Randy Dunlap76824862008-03-19 17:00:40 -0700863 *
864 * Returns: NULL on failure, vm_struct on success
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -0700865 *
866 * This function reserves a range of kernel address space, and
867 * allocates pagetables to map that range. No actual mappings
868 * are created. If the kernel address space is not shared
869 * between processes, it syncs the pagetable across all
870 * processes.
871 */
872struct vm_struct *alloc_vm_area(size_t size)
873{
874 struct vm_struct *area;
875
Christoph Lameter23016962008-04-28 02:12:42 -0700876 area = get_vm_area_caller(size, VM_IOREMAP,
877 __builtin_return_address(0));
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -0700878 if (area == NULL)
879 return NULL;
880
881 /*
882 * This ensures that page tables are constructed for this region
883 * of kernel virtual address space and mapped into init_mm.
884 */
885 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
886 area->size, f, NULL)) {
887 free_vm_area(area);
888 return NULL;
889 }
890
891 /* Make sure the pagetables are constructed in process kernel
892 mappings */
893 vmalloc_sync_all();
894
895 return area;
896}
897EXPORT_SYMBOL_GPL(alloc_vm_area);
898
899void free_vm_area(struct vm_struct *area)
900{
901 struct vm_struct *ret;
902 ret = remove_vm_area(area->addr);
903 BUG_ON(ret != area);
904 kfree(area);
905}
906EXPORT_SYMBOL_GPL(free_vm_area);
Christoph Lametera10aa572008-04-28 02:12:40 -0700907
908
909#ifdef CONFIG_PROC_FS
910static void *s_start(struct seq_file *m, loff_t *pos)
911{
912 loff_t n = *pos;
913 struct vm_struct *v;
914
915 read_lock(&vmlist_lock);
916 v = vmlist;
917 while (n > 0 && v) {
918 n--;
919 v = v->next;
920 }
921 if (!n)
922 return v;
923
924 return NULL;
925
926}
927
928static void *s_next(struct seq_file *m, void *p, loff_t *pos)
929{
930 struct vm_struct *v = p;
931
932 ++*pos;
933 return v->next;
934}
935
936static void s_stop(struct seq_file *m, void *p)
937{
938 read_unlock(&vmlist_lock);
939}
940
941static int s_show(struct seq_file *m, void *p)
942{
943 struct vm_struct *v = p;
944
945 seq_printf(m, "0x%p-0x%p %7ld",
946 v->addr, v->addr + v->size, v->size);
947
Christoph Lameter23016962008-04-28 02:12:42 -0700948 if (v->caller) {
949 char buff[2 * KSYM_NAME_LEN];
950
951 seq_putc(m, ' ');
952 sprint_symbol(buff, (unsigned long)v->caller);
953 seq_puts(m, buff);
954 }
955
Christoph Lametera10aa572008-04-28 02:12:40 -0700956 if (v->nr_pages)
957 seq_printf(m, " pages=%d", v->nr_pages);
958
959 if (v->phys_addr)
960 seq_printf(m, " phys=%lx", v->phys_addr);
961
962 if (v->flags & VM_IOREMAP)
963 seq_printf(m, " ioremap");
964
965 if (v->flags & VM_ALLOC)
966 seq_printf(m, " vmalloc");
967
968 if (v->flags & VM_MAP)
969 seq_printf(m, " vmap");
970
971 if (v->flags & VM_USERMAP)
972 seq_printf(m, " user");
973
974 if (v->flags & VM_VPAGES)
975 seq_printf(m, " vpages");
976
977 seq_putc(m, '\n');
978 return 0;
979}
980
981const struct seq_operations vmalloc_op = {
982 .start = s_start,
983 .next = s_next,
984 .stop = s_stop,
985 .show = s_show,
986};
987#endif
988