aboutsummaryrefslogtreecommitdiffstats
path: root/mm/vmalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'mm/vmalloc.c')
-rw-r--r--mm/vmalloc.c150
1 files changed, 126 insertions, 24 deletions
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 520a75980269..f8189a4b3e13 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -23,7 +23,8 @@
23#include <linux/rbtree.h> 23#include <linux/rbtree.h>
24#include <linux/radix-tree.h> 24#include <linux/radix-tree.h>
25#include <linux/rcupdate.h> 25#include <linux/rcupdate.h>
26#include <linux/bootmem.h> 26#include <linux/pfn.h>
27#include <linux/kmemleak.h>
27 28
28#include <asm/atomic.h> 29#include <asm/atomic.h>
29#include <asm/uaccess.h> 30#include <asm/uaccess.h>
@@ -152,8 +153,8 @@ static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
152 * 153 *
153 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N] 154 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
154 */ 155 */
155static int vmap_page_range(unsigned long start, unsigned long end, 156static int vmap_page_range_noflush(unsigned long start, unsigned long end,
156 pgprot_t prot, struct page **pages) 157 pgprot_t prot, struct page **pages)
157{ 158{
158 pgd_t *pgd; 159 pgd_t *pgd;
159 unsigned long next; 160 unsigned long next;
@@ -169,13 +170,22 @@ static int vmap_page_range(unsigned long start, unsigned long end,
169 if (err) 170 if (err)
170 break; 171 break;
171 } while (pgd++, addr = next, addr != end); 172 } while (pgd++, addr = next, addr != end);
172 flush_cache_vmap(start, end);
173 173
174 if (unlikely(err)) 174 if (unlikely(err))
175 return err; 175 return err;
176 return nr; 176 return nr;
177} 177}
178 178
179static 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
179static inline int is_vmalloc_or_module_addr(const void *x) 189static inline int is_vmalloc_or_module_addr(const void *x)
180{ 190{
181 /* 191 /*
@@ -392,6 +402,7 @@ overflow:
392 printk(KERN_WARNING 402 printk(KERN_WARNING
393 "vmap allocation for size %lu failed: " 403 "vmap allocation for size %lu failed: "
394 "use vmalloc=<size> to increase size.\n", size); 404 "use vmalloc=<size> to increase size.\n", size);
405 kfree(va);
395 return ERR_PTR(-EBUSY); 406 return ERR_PTR(-EBUSY);
396 } 407 }
397 408
@@ -661,10 +672,7 @@ struct vmap_block {
661 DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS); 672 DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
662 DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS); 673 DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
663 union { 674 union {
664 struct { 675 struct list_head free_list;
665 struct list_head free_list;
666 struct list_head dirty_list;
667 };
668 struct rcu_head rcu_head; 676 struct rcu_head rcu_head;
669 }; 677 };
670}; 678};
@@ -731,7 +739,6 @@ static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
731 bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS); 739 bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
732 bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS); 740 bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
733 INIT_LIST_HEAD(&vb->free_list); 741 INIT_LIST_HEAD(&vb->free_list);
734 INIT_LIST_HEAD(&vb->dirty_list);
735 742
736 vb_idx = addr_to_vb_idx(va->va_start); 743 vb_idx = addr_to_vb_idx(va->va_start);
737 spin_lock(&vmap_block_tree_lock); 744 spin_lock(&vmap_block_tree_lock);
@@ -762,12 +769,7 @@ static void free_vmap_block(struct vmap_block *vb)
762 struct vmap_block *tmp; 769 struct vmap_block *tmp;
763 unsigned long vb_idx; 770 unsigned long vb_idx;
764 771
765 spin_lock(&vb->vbq->lock); 772 BUG_ON(!list_empty(&vb->free_list));
766 if (!list_empty(&vb->free_list))
767 list_del(&vb->free_list);
768 if (!list_empty(&vb->dirty_list))
769 list_del(&vb->dirty_list);
770 spin_unlock(&vb->vbq->lock);
771 773
772 vb_idx = addr_to_vb_idx(vb->va->va_start); 774 vb_idx = addr_to_vb_idx(vb->va->va_start);
773 spin_lock(&vmap_block_tree_lock); 775 spin_lock(&vmap_block_tree_lock);
@@ -852,11 +854,7 @@ static void vb_free(const void *addr, unsigned long size)
852 854
853 spin_lock(&vb->lock); 855 spin_lock(&vb->lock);
854 bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order); 856 bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
855 if (!vb->dirty) { 857
856 spin_lock(&vb->vbq->lock);
857 list_add(&vb->dirty_list, &vb->vbq->dirty);
858 spin_unlock(&vb->vbq->lock);
859 }
860 vb->dirty += 1UL << order; 858 vb->dirty += 1UL << order;
861 if (vb->dirty == VMAP_BBMAP_BITS) { 859 if (vb->dirty == VMAP_BBMAP_BITS) {
862 BUG_ON(vb->free || !list_empty(&vb->free_list)); 860 BUG_ON(vb->free || !list_empty(&vb->free_list));
@@ -990,6 +988,32 @@ void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t pro
990} 988}
991EXPORT_SYMBOL(vm_map_ram); 989EXPORT_SYMBOL(vm_map_ram);
992 990
991/**
992 * vm_area_register_early - register vmap area early during boot
993 * @vm: vm_struct to register
994 * @align: requested alignment
995 *
996 * This function is used to register kernel vm area before
997 * vmalloc_init() is called. @vm->size and @vm->flags should contain
998 * proper values on entry and other fields should be zero. On return,
999 * vm->addr contains the allocated address.
1000 *
1001 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1002 */
1003void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1004{
1005 static size_t vm_init_off __initdata;
1006 unsigned long addr;
1007
1008 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1009 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1010
1011 vm->addr = (void *)addr;
1012
1013 vm->next = vmlist;
1014 vmlist = vm;
1015}
1016
993void __init vmalloc_init(void) 1017void __init vmalloc_init(void)
994{ 1018{
995 struct vmap_area *va; 1019 struct vmap_area *va;
@@ -1008,7 +1032,7 @@ void __init vmalloc_init(void)
1008 1032
1009 /* Import existing vmlist entries. */ 1033 /* Import existing vmlist entries. */
1010 for (tmp = vmlist; tmp; tmp = tmp->next) { 1034 for (tmp = vmlist; tmp; tmp = tmp->next) {
1011 va = alloc_bootmem(sizeof(struct vmap_area)); 1035 va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
1012 va->flags = tmp->flags | VM_VM_AREA; 1036 va->flags = tmp->flags | VM_VM_AREA;
1013 va->va_start = (unsigned long)tmp->addr; 1037 va->va_start = (unsigned long)tmp->addr;
1014 va->va_end = va->va_start + tmp->size; 1038 va->va_end = va->va_start + tmp->size;
@@ -1017,6 +1041,58 @@ void __init vmalloc_init(void)
1017 vmap_initialized = true; 1041 vmap_initialized = true;
1018} 1042}
1019 1043
1044/**
1045 * map_kernel_range_noflush - map kernel VM area with the specified pages
1046 * @addr: start of the VM area to map
1047 * @size: size of the VM area to map
1048 * @prot: page protection flags to use
1049 * @pages: pages to map
1050 *
1051 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1052 * specify should have been allocated using get_vm_area() and its
1053 * friends.
1054 *
1055 * NOTE:
1056 * This function does NOT do any cache flushing. The caller is
1057 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1058 * before calling this function.
1059 *
1060 * RETURNS:
1061 * The number of pages mapped on success, -errno on failure.
1062 */
1063int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1064 pgprot_t prot, struct page **pages)
1065{
1066 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1067}
1068
1069/**
1070 * unmap_kernel_range_noflush - unmap kernel VM area
1071 * @addr: start of the VM area to unmap
1072 * @size: size of the VM area to unmap
1073 *
1074 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1075 * specify should have been allocated using get_vm_area() and its
1076 * friends.
1077 *
1078 * NOTE:
1079 * This function does NOT do any cache flushing. The caller is
1080 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1081 * before calling this function and flush_tlb_kernel_range() after.
1082 */
1083void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1084{
1085 vunmap_page_range(addr, addr + size);
1086}
1087
1088/**
1089 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1090 * @addr: start of the VM area to unmap
1091 * @size: size of the VM area to unmap
1092 *
1093 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1094 * the unmapping and tlb after.
1095 */
1020void unmap_kernel_range(unsigned long addr, unsigned long size) 1096void unmap_kernel_range(unsigned long addr, unsigned long size)
1021{ 1097{
1022 unsigned long end = addr + size; 1098 unsigned long end = addr + size;
@@ -1251,6 +1327,9 @@ static void __vunmap(const void *addr, int deallocate_pages)
1251void vfree(const void *addr) 1327void vfree(const void *addr)
1252{ 1328{
1253 BUG_ON(in_interrupt()); 1329 BUG_ON(in_interrupt());
1330
1331 kmemleak_free(addr);
1332
1254 __vunmap(addr, 1); 1333 __vunmap(addr, 1);
1255} 1334}
1256EXPORT_SYMBOL(vfree); 1335EXPORT_SYMBOL(vfree);
@@ -1267,6 +1346,7 @@ EXPORT_SYMBOL(vfree);
1267void vunmap(const void *addr) 1346void vunmap(const void *addr)
1268{ 1347{
1269 BUG_ON(in_interrupt()); 1348 BUG_ON(in_interrupt());
1349 might_sleep();
1270 __vunmap(addr, 0); 1350 __vunmap(addr, 0);
1271} 1351}
1272EXPORT_SYMBOL(vunmap); 1352EXPORT_SYMBOL(vunmap);
@@ -1286,6 +1366,8 @@ void *vmap(struct page **pages, unsigned int count,
1286{ 1366{
1287 struct vm_struct *area; 1367 struct vm_struct *area;
1288 1368
1369 might_sleep();
1370
1289 if (count > num_physpages) 1371 if (count > num_physpages)
1290 return NULL; 1372 return NULL;
1291 1373
@@ -1360,8 +1442,17 @@ fail:
1360 1442
1361void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot) 1443void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
1362{ 1444{
1363 return __vmalloc_area_node(area, gfp_mask, prot, -1, 1445 void *addr = __vmalloc_area_node(area, gfp_mask, prot, -1,
1364 __builtin_return_address(0)); 1446 __builtin_return_address(0));
1447
1448 /*
1449 * A ref_count = 3 is needed because the vm_struct and vmap_area
1450 * structures allocated in the __get_vm_area_node() function contain
1451 * references to the virtual address of the vmalloc'ed block.
1452 */
1453 kmemleak_alloc(addr, area->size - PAGE_SIZE, 3, gfp_mask);
1454
1455 return addr;
1365} 1456}
1366 1457
1367/** 1458/**
@@ -1380,6 +1471,8 @@ static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1380 int node, void *caller) 1471 int node, void *caller)
1381{ 1472{
1382 struct vm_struct *area; 1473 struct vm_struct *area;
1474 void *addr;
1475 unsigned long real_size = size;
1383 1476
1384 size = PAGE_ALIGN(size); 1477 size = PAGE_ALIGN(size);
1385 if (!size || (size >> PAGE_SHIFT) > num_physpages) 1478 if (!size || (size >> PAGE_SHIFT) > num_physpages)
@@ -1391,7 +1484,16 @@ static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1391 if (!area) 1484 if (!area)
1392 return NULL; 1485 return NULL;
1393 1486
1394 return __vmalloc_area_node(area, gfp_mask, prot, node, caller); 1487 addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
1488
1489 /*
1490 * A ref_count = 3 is needed because the vm_struct and vmap_area
1491 * structures allocated in the __get_vm_area_node() function contain
1492 * references to the virtual address of the vmalloc'ed block.
1493 */
1494 kmemleak_alloc(addr, real_size, 3, gfp_mask);
1495
1496 return addr;
1395} 1497}
1396 1498
1397void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) 1499void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)