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
8 * Numa awareness, Christoph Lameter, SGI, June 2005
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>
17 #include <linux/seq_file.h>
18 #include <linux/debugobjects.h>
19 #include <linux/vmalloc.h>
20 #include <linux/kallsyms.h>
22 #include <asm/uaccess.h>
23 #include <asm/tlbflush.h>
26 DEFINE_RWLOCK(vmlist_lock);
27 struct vm_struct *vmlist;
29 static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
30 int node, void *caller);
32 static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
36 pte = pte_offset_kernel(pmd, addr);
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);
43 static inline void vunmap_pmd_range(pud_t *pud, unsigned long addr,
49 pmd = pmd_offset(pud, addr);
51 next = pmd_addr_end(addr, end);
52 if (pmd_none_or_clear_bad(pmd))
54 vunmap_pte_range(pmd, addr, next);
55 } while (pmd++, addr = next, addr != end);
58 static inline void vunmap_pud_range(pgd_t *pgd, unsigned long addr,
64 pud = pud_offset(pgd, addr);
66 next = pud_addr_end(addr, end);
67 if (pud_none_or_clear_bad(pud))
69 vunmap_pmd_range(pud, addr, next);
70 } while (pud++, addr = next, addr != end);
73 void unmap_kernel_range(unsigned long addr, unsigned long size)
77 unsigned long start = addr;
78 unsigned long end = addr + size;
81 pgd = pgd_offset_k(addr);
82 flush_cache_vunmap(addr, end);
84 next = pgd_addr_end(addr, end);
85 if (pgd_none_or_clear_bad(pgd))
87 vunmap_pud_range(pgd, addr, next);
88 } while (pgd++, addr = next, addr != end);
89 flush_tlb_kernel_range(start, end);
92 static void unmap_vm_area(struct vm_struct *area)
94 unmap_kernel_range((unsigned long)area->addr, area->size);
97 static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
98 unsigned long end, pgprot_t prot, struct page ***pages)
102 pte = pte_alloc_kernel(pmd, addr);
106 struct page *page = **pages;
107 WARN_ON(!pte_none(*pte));
110 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
112 } while (pte++, addr += PAGE_SIZE, addr != end);
116 static inline int vmap_pmd_range(pud_t *pud, unsigned long addr,
117 unsigned long end, pgprot_t prot, struct page ***pages)
122 pmd = pmd_alloc(&init_mm, pud, addr);
126 next = pmd_addr_end(addr, end);
127 if (vmap_pte_range(pmd, addr, next, prot, pages))
129 } while (pmd++, addr = next, addr != end);
133 static inline int vmap_pud_range(pgd_t *pgd, unsigned long addr,
134 unsigned long end, pgprot_t prot, struct page ***pages)
139 pud = pud_alloc(&init_mm, pgd, addr);
143 next = pud_addr_end(addr, end);
144 if (vmap_pmd_range(pud, addr, next, prot, pages))
146 } while (pud++, addr = next, addr != end);
150 int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
154 unsigned long addr = (unsigned long) area->addr;
155 unsigned long end = addr + area->size - PAGE_SIZE;
159 pgd = pgd_offset_k(addr);
161 next = pgd_addr_end(addr, end);
162 err = vmap_pud_range(pgd, addr, next, prot, pages);
165 } while (pgd++, addr = next, addr != end);
166 flush_cache_vmap((unsigned long) area->addr, end);
169 EXPORT_SYMBOL_GPL(map_vm_area);
172 * Map a vmalloc()-space virtual address to the physical page.
174 struct page *vmalloc_to_page(const void *vmalloc_addr)
176 unsigned long addr = (unsigned long) vmalloc_addr;
177 struct page *page = NULL;
178 pgd_t *pgd = pgd_offset_k(addr);
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);
190 if (pte_present(pte))
191 page = pte_page(pte);
198 EXPORT_SYMBOL(vmalloc_to_page);
201 * Map a vmalloc()-space virtual address to the physical page frame number.
203 unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
205 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
207 EXPORT_SYMBOL(vmalloc_to_pfn);
209 static 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)
213 struct vm_struct **p, *tmp, *area;
214 unsigned long align = 1;
217 BUG_ON(in_interrupt());
218 if (flags & VM_IOREMAP) {
221 if (bit > IOREMAP_MAX_ORDER)
222 bit = IOREMAP_MAX_ORDER;
223 else if (bit < PAGE_SHIFT)
228 addr = ALIGN(start, align);
229 size = PAGE_ALIGN(size);
233 area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
239 * We always allocate a guard page.
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);
251 if ((size + addr) < addr)
253 if (size + addr <= (unsigned long)tmp->addr)
255 addr = ALIGN(tmp->size + (unsigned long)tmp->addr, align);
256 if (addr > end - size)
259 if ((size + addr) < addr)
261 if (addr > end - size)
269 area->addr = (void *)addr;
274 area->caller = caller;
275 write_unlock(&vmlist_lock);
280 write_unlock(&vmlist_lock);
282 if (printk_ratelimit())
283 printk(KERN_WARNING "allocation failed: out of vmalloc space - use vmalloc=<size> to increase size.\n");
287 struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
288 unsigned long start, unsigned long end)
290 return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
291 __builtin_return_address(0));
293 EXPORT_SYMBOL_GPL(__get_vm_area);
296 * get_vm_area - reserve a contiguous kernel virtual area
297 * @size: size of the area
298 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
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.
304 struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
306 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
307 -1, GFP_KERNEL, __builtin_return_address(0));
310 struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
313 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
314 -1, GFP_KERNEL, caller);
317 struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
318 int node, gfp_t gfp_mask)
320 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
321 gfp_mask, __builtin_return_address(0));
324 /* Caller must hold vmlist_lock */
325 static struct vm_struct *__find_vm_area(const void *addr)
327 struct vm_struct *tmp;
329 for (tmp = vmlist; tmp != NULL; tmp = tmp->next) {
330 if (tmp->addr == addr)
337 /* Caller must hold vmlist_lock */
338 static struct vm_struct *__remove_vm_area(const void *addr)
340 struct vm_struct **p, *tmp;
342 for (p = &vmlist ; (tmp = *p) != NULL ;p = &tmp->next) {
343 if (tmp->addr == addr)
353 * Remove the guard page.
355 tmp->size -= PAGE_SIZE;
360 * remove_vm_area - find and remove a continuous kernel virtual area
361 * @addr: base address
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
365 * on SMP machines, except for its size or flags.
367 struct vm_struct *remove_vm_area(const void *addr)
370 write_lock(&vmlist_lock);
371 v = __remove_vm_area(addr);
372 write_unlock(&vmlist_lock);
376 static void __vunmap(const void *addr, int deallocate_pages)
378 struct vm_struct *area;
383 if ((PAGE_SIZE-1) & (unsigned long)addr) {
384 WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
388 area = remove_vm_area(addr);
389 if (unlikely(!area)) {
390 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
395 debug_check_no_locks_freed(addr, area->size);
396 debug_check_no_obj_freed(addr, area->size);
398 if (deallocate_pages) {
401 for (i = 0; i < area->nr_pages; i++) {
402 struct page *page = area->pages[i];
408 if (area->flags & VM_VPAGES)
419 * vfree - release memory allocated by vmalloc()
420 * @addr: memory base address
422 * Free the virtually continuous memory area starting at @addr, as
423 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
424 * NULL, no operation is performed.
426 * Must not be called in interrupt context.
428 void vfree(const void *addr)
430 BUG_ON(in_interrupt());
433 EXPORT_SYMBOL(vfree);
436 * vunmap - release virtual mapping obtained by vmap()
437 * @addr: memory base address
439 * Free the virtually contiguous memory area starting at @addr,
440 * which was created from the page array passed to vmap().
442 * Must not be called in interrupt context.
444 void vunmap(const void *addr)
446 BUG_ON(in_interrupt());
449 EXPORT_SYMBOL(vunmap);
452 * vmap - map an array of pages into virtually contiguous space
453 * @pages: array of page pointers
454 * @count: number of pages to map
455 * @flags: vm_area->flags
456 * @prot: page protection for the mapping
458 * Maps @count pages from @pages into contiguous kernel virtual
461 void *vmap(struct page **pages, unsigned int count,
462 unsigned long flags, pgprot_t prot)
464 struct vm_struct *area;
466 if (count > num_physpages)
469 area = get_vm_area_caller((count << PAGE_SHIFT), flags,
470 __builtin_return_address(0));
474 if (map_vm_area(area, prot, &pages)) {
483 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
484 pgprot_t prot, int node, void *caller)
487 unsigned int nr_pages, array_size, i;
489 nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
490 array_size = (nr_pages * sizeof(struct page *));
492 area->nr_pages = nr_pages;
493 /* Please note that the recursion is strictly bounded. */
494 if (array_size > PAGE_SIZE) {
495 pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
496 PAGE_KERNEL, node, caller);
497 area->flags |= VM_VPAGES;
499 pages = kmalloc_node(array_size,
500 (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
504 area->caller = caller;
506 remove_vm_area(area->addr);
511 for (i = 0; i < area->nr_pages; i++) {
515 page = alloc_page(gfp_mask);
517 page = alloc_pages_node(node, gfp_mask, 0);
519 if (unlikely(!page)) {
520 /* Successfully allocated i pages, free them in __vunmap() */
524 area->pages[i] = page;
527 if (map_vm_area(area, prot, &pages))
536 void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
538 return __vmalloc_area_node(area, gfp_mask, prot, -1,
539 __builtin_return_address(0));
543 * __vmalloc_node - allocate virtually contiguous memory
544 * @size: allocation size
545 * @gfp_mask: flags for the page level allocator
546 * @prot: protection mask for the allocated pages
547 * @node: node to use for allocation or -1
548 * @caller: caller's return address
550 * Allocate enough pages to cover @size from the page level
551 * allocator with @gfp_mask flags. Map them into contiguous
552 * kernel virtual space, using a pagetable protection of @prot.
554 static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
555 int node, void *caller)
557 struct vm_struct *area;
559 size = PAGE_ALIGN(size);
560 if (!size || (size >> PAGE_SHIFT) > num_physpages)
563 area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
564 node, gfp_mask, caller);
569 return __vmalloc_area_node(area, gfp_mask, prot, node, caller);
572 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
574 return __vmalloc_node(size, gfp_mask, prot, -1,
575 __builtin_return_address(0));
577 EXPORT_SYMBOL(__vmalloc);
580 * vmalloc - allocate virtually contiguous memory
581 * @size: allocation size
582 * Allocate enough pages to cover @size from the page level
583 * allocator and map them into contiguous kernel virtual space.
585 * For tight control over page level allocator and protection flags
586 * use __vmalloc() instead.
588 void *vmalloc(unsigned long size)
590 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
591 -1, __builtin_return_address(0));
593 EXPORT_SYMBOL(vmalloc);
596 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
597 * @size: allocation size
599 * The resulting memory area is zeroed so it can be mapped to userspace
600 * without leaking data.
602 void *vmalloc_user(unsigned long size)
604 struct vm_struct *area;
607 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
609 write_lock(&vmlist_lock);
610 area = __find_vm_area(ret);
611 area->flags |= VM_USERMAP;
612 write_unlock(&vmlist_lock);
616 EXPORT_SYMBOL(vmalloc_user);
619 * vmalloc_node - allocate memory on a specific node
620 * @size: allocation size
623 * Allocate enough pages to cover @size from the page level
624 * allocator and map them into contiguous kernel virtual space.
626 * For tight control over page level allocator and protection flags
627 * use __vmalloc() instead.
629 void *vmalloc_node(unsigned long size, int node)
631 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
632 node, __builtin_return_address(0));
634 EXPORT_SYMBOL(vmalloc_node);
636 #ifndef PAGE_KERNEL_EXEC
637 # define PAGE_KERNEL_EXEC PAGE_KERNEL
641 * vmalloc_exec - allocate virtually contiguous, executable memory
642 * @size: allocation size
644 * Kernel-internal function to allocate enough pages to cover @size
645 * the page level allocator and map them into contiguous and
646 * executable kernel virtual space.
648 * For tight control over page level allocator and protection flags
649 * use __vmalloc() instead.
652 void *vmalloc_exec(unsigned long size)
654 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
657 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
658 #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
659 #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
660 #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
662 #define GFP_VMALLOC32 GFP_KERNEL
666 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
667 * @size: allocation size
669 * Allocate enough 32bit PA addressable pages to cover @size from the
670 * page level allocator and map them into contiguous kernel virtual space.
672 void *vmalloc_32(unsigned long size)
674 return __vmalloc(size, GFP_VMALLOC32, PAGE_KERNEL);
676 EXPORT_SYMBOL(vmalloc_32);
679 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
680 * @size: allocation size
682 * The resulting memory area is 32bit addressable and zeroed so it can be
683 * mapped to userspace without leaking data.
685 void *vmalloc_32_user(unsigned long size)
687 struct vm_struct *area;
690 ret = __vmalloc(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL);
692 write_lock(&vmlist_lock);
693 area = __find_vm_area(ret);
694 area->flags |= VM_USERMAP;
695 write_unlock(&vmlist_lock);
699 EXPORT_SYMBOL(vmalloc_32_user);
701 long vread(char *buf, char *addr, unsigned long count)
703 struct vm_struct *tmp;
704 char *vaddr, *buf_start = buf;
707 /* Don't allow overflow */
708 if ((unsigned long) addr + count < count)
709 count = -(unsigned long) addr;
711 read_lock(&vmlist_lock);
712 for (tmp = vmlist; tmp; tmp = tmp->next) {
713 vaddr = (char *) tmp->addr;
714 if (addr >= vaddr + tmp->size - PAGE_SIZE)
716 while (addr < vaddr) {
724 n = vaddr + tmp->size - PAGE_SIZE - addr;
735 read_unlock(&vmlist_lock);
736 return buf - buf_start;
739 long vwrite(char *buf, char *addr, unsigned long count)
741 struct vm_struct *tmp;
742 char *vaddr, *buf_start = buf;
745 /* Don't allow overflow */
746 if ((unsigned long) addr + count < count)
747 count = -(unsigned long) addr;
749 read_lock(&vmlist_lock);
750 for (tmp = vmlist; tmp; tmp = tmp->next) {
751 vaddr = (char *) tmp->addr;
752 if (addr >= vaddr + tmp->size - PAGE_SIZE)
754 while (addr < vaddr) {
761 n = vaddr + tmp->size - PAGE_SIZE - addr;
772 read_unlock(&vmlist_lock);
773 return buf - buf_start;
777 * remap_vmalloc_range - map vmalloc pages to userspace
778 * @vma: vma to cover (map full range of vma)
779 * @addr: vmalloc memory
780 * @pgoff: number of pages into addr before first page to map
782 * Returns: 0 for success, -Exxx on failure
784 * This function checks that addr is a valid vmalloc'ed area, and
785 * that it is big enough to cover the vma. Will return failure if
786 * that criteria isn't met.
788 * Similar to remap_pfn_range() (see mm/memory.c)
790 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
793 struct vm_struct *area;
794 unsigned long uaddr = vma->vm_start;
795 unsigned long usize = vma->vm_end - vma->vm_start;
798 if ((PAGE_SIZE-1) & (unsigned long)addr)
801 read_lock(&vmlist_lock);
802 area = __find_vm_area(addr);
804 goto out_einval_locked;
806 if (!(area->flags & VM_USERMAP))
807 goto out_einval_locked;
809 if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
810 goto out_einval_locked;
811 read_unlock(&vmlist_lock);
813 addr += pgoff << PAGE_SHIFT;
815 struct page *page = vmalloc_to_page(addr);
816 ret = vm_insert_page(vma, uaddr, page);
825 /* Prevent "things" like memory migration? VM_flags need a cleanup... */
826 vma->vm_flags |= VM_RESERVED;
831 read_unlock(&vmlist_lock);
834 EXPORT_SYMBOL(remap_vmalloc_range);
837 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
840 void __attribute__((weak)) vmalloc_sync_all(void)
845 static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
847 /* apply_to_page_range() does all the hard work. */
852 * alloc_vm_area - allocate a range of kernel address space
853 * @size: size of the area
855 * Returns: NULL on failure, vm_struct on success
857 * This function reserves a range of kernel address space, and
858 * allocates pagetables to map that range. No actual mappings
859 * are created. If the kernel address space is not shared
860 * between processes, it syncs the pagetable across all
863 struct vm_struct *alloc_vm_area(size_t size)
865 struct vm_struct *area;
867 area = get_vm_area_caller(size, VM_IOREMAP,
868 __builtin_return_address(0));
873 * This ensures that page tables are constructed for this region
874 * of kernel virtual address space and mapped into init_mm.
876 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
877 area->size, f, NULL)) {
882 /* Make sure the pagetables are constructed in process kernel
888 EXPORT_SYMBOL_GPL(alloc_vm_area);
890 void free_vm_area(struct vm_struct *area)
892 struct vm_struct *ret;
893 ret = remove_vm_area(area->addr);
897 EXPORT_SYMBOL_GPL(free_vm_area);
900 #ifdef CONFIG_PROC_FS
901 static void *s_start(struct seq_file *m, loff_t *pos)
906 read_lock(&vmlist_lock);
919 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
921 struct vm_struct *v = p;
927 static void s_stop(struct seq_file *m, void *p)
929 read_unlock(&vmlist_lock);
932 static void show_numa_info(struct seq_file *m, struct vm_struct *v)
935 unsigned int nr, *counters = m->private;
940 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
942 for (nr = 0; nr < v->nr_pages; nr++)
943 counters[page_to_nid(v->pages[nr])]++;
945 for_each_node_state(nr, N_HIGH_MEMORY)
947 seq_printf(m, " N%u=%u", nr, counters[nr]);
951 static int s_show(struct seq_file *m, void *p)
953 struct vm_struct *v = p;
955 seq_printf(m, "0x%p-0x%p %7ld",
956 v->addr, v->addr + v->size, v->size);
959 char buff[2 * KSYM_NAME_LEN];
962 sprint_symbol(buff, (unsigned long)v->caller);
967 seq_printf(m, " pages=%d", v->nr_pages);
970 seq_printf(m, " phys=%lx", v->phys_addr);
972 if (v->flags & VM_IOREMAP)
973 seq_printf(m, " ioremap");
975 if (v->flags & VM_ALLOC)
976 seq_printf(m, " vmalloc");
978 if (v->flags & VM_MAP)
979 seq_printf(m, " vmap");
981 if (v->flags & VM_USERMAP)
982 seq_printf(m, " user");
984 if (v->flags & VM_VPAGES)
985 seq_printf(m, " vpages");
987 show_numa_info(m, v);
992 const struct seq_operations vmalloc_op = {