blob: e17469362b09050f8bb11b2aa7144156792733fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/vmalloc.c
3 *
4 * Copyright (C) 1993 Linus Torvalds
5 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
6 * SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000
7 * Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002
Christoph Lameter930fc452005-10-29 18:15:41 -07008 * Numa awareness, Christoph Lameter, SGI, June 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Nick Piggindb64fe02008-10-18 20:27:03 -070011#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/highmem.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040015#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
17#include <linux/spinlock.h>
18#include <linux/interrupt.h>
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +040019#include <linux/proc_fs.h>
Christoph Lametera10aa572008-04-28 02:12:40 -070020#include <linux/seq_file.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070021#include <linux/debugobjects.h>
Christoph Lameter23016962008-04-28 02:12:42 -070022#include <linux/kallsyms.h>
Nick Piggindb64fe02008-10-18 20:27:03 -070023#include <linux/list.h>
24#include <linux/rbtree.h>
25#include <linux/radix-tree.h>
26#include <linux/rcupdate.h>
Tejun Heof0aa6612009-02-20 16:29:08 +090027#include <linux/pfn.h>
Catalin Marinas89219d32009-06-11 13:23:19 +010028#include <linux/kmemleak.h>
Arun Sharma60063492011-07-26 16:09:06 -070029#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
31#include <asm/tlbflush.h>
David Miller2dca6992009-09-21 12:22:34 -070032#include <asm/shmparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Nick Piggindb64fe02008-10-18 20:27:03 -070034/*** Page table manipulation functions ***/
Adrian Bunkb2213852006-09-25 23:31:02 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
37{
38 pte_t *pte;
39
40 pte = pte_offset_kernel(pmd, addr);
41 do {
42 pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
43 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
44 } while (pte++, addr += PAGE_SIZE, addr != end);
45}
46
Nick Piggindb64fe02008-10-18 20:27:03 -070047static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 pmd_t *pmd;
50 unsigned long next;
51
52 pmd = pmd_offset(pud, addr);
53 do {
54 next = pmd_addr_end(addr, end);
55 if (pmd_none_or_clear_bad(pmd))
56 continue;
57 vunmap_pte_range(pmd, addr, next);
58 } while (pmd++, addr = next, addr != end);
59}
60
Nick Piggindb64fe02008-10-18 20:27:03 -070061static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 pud_t *pud;
64 unsigned long next;
65
66 pud = pud_offset(pgd, addr);
67 do {
68 next = pud_addr_end(addr, end);
69 if (pud_none_or_clear_bad(pud))
70 continue;
71 vunmap_pmd_range(pud, addr, next);
72 } while (pud++, addr = next, addr != end);
73}
74
Nick Piggindb64fe02008-10-18 20:27:03 -070075static void vunmap_page_range(unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 pgd_t *pgd;
78 unsigned long next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 BUG_ON(addr >= end);
81 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 do {
83 next = pgd_addr_end(addr, end);
84 if (pgd_none_or_clear_bad(pgd))
85 continue;
86 vunmap_pud_range(pgd, addr, next);
87 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
Nick Piggindb64fe02008-10-18 20:27:03 -070091 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 pte_t *pte;
94
Nick Piggindb64fe02008-10-18 20:27:03 -070095 /*
96 * nr is a running index into the array which helps higher level
97 * callers keep track of where we're up to.
98 */
99
Hugh Dickins872fec12005-10-29 18:16:21 -0700100 pte = pte_alloc_kernel(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (!pte)
102 return -ENOMEM;
103 do {
Nick Piggindb64fe02008-10-18 20:27:03 -0700104 struct page *page = pages[*nr];
105
106 if (WARN_ON(!pte_none(*pte)))
107 return -EBUSY;
108 if (WARN_ON(!page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -ENOMEM;
110 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
Nick Piggindb64fe02008-10-18 20:27:03 -0700111 (*nr)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 } while (pte++, addr += PAGE_SIZE, addr != end);
113 return 0;
114}
115
Nick Piggindb64fe02008-10-18 20:27:03 -0700116static int vmap_pmd_range(pud_t *pud, unsigned long addr,
117 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 pmd_t *pmd;
120 unsigned long next;
121
122 pmd = pmd_alloc(&init_mm, pud, addr);
123 if (!pmd)
124 return -ENOMEM;
125 do {
126 next = pmd_addr_end(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700127 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return -ENOMEM;
129 } while (pmd++, addr = next, addr != end);
130 return 0;
131}
132
Nick Piggindb64fe02008-10-18 20:27:03 -0700133static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
134 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 pud_t *pud;
137 unsigned long next;
138
139 pud = pud_alloc(&init_mm, pgd, addr);
140 if (!pud)
141 return -ENOMEM;
142 do {
143 next = pud_addr_end(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700144 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -ENOMEM;
146 } while (pud++, addr = next, addr != end);
147 return 0;
148}
149
Nick Piggindb64fe02008-10-18 20:27:03 -0700150/*
151 * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
152 * will have pfns corresponding to the "pages" array.
153 *
154 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
155 */
Tejun Heo8fc48982009-02-20 16:29:08 +0900156static int vmap_page_range_noflush(unsigned long start, unsigned long end,
157 pgprot_t prot, struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 pgd_t *pgd;
160 unsigned long next;
Adam Lackorzynski2e4e27c2009-01-04 12:00:46 -0800161 unsigned long addr = start;
Nick Piggindb64fe02008-10-18 20:27:03 -0700162 int err = 0;
163 int nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 BUG_ON(addr >= end);
166 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 do {
168 next = pgd_addr_end(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700169 err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (err)
Figo.zhangbf88c8c2009-09-21 17:01:47 -0700171 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 } while (pgd++, addr = next, addr != end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700173
Nick Piggindb64fe02008-10-18 20:27:03 -0700174 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Tejun Heo8fc48982009-02-20 16:29:08 +0900177static int vmap_page_range(unsigned long start, unsigned long end,
178 pgprot_t prot, struct page **pages)
179{
180 int ret;
181
182 ret = vmap_page_range_noflush(start, end, prot, pages);
183 flush_cache_vmap(start, end);
184 return ret;
185}
186
Laura Abbottd3263482013-08-22 13:46:07 -0700187#ifdef ENABLE_VMALLOC_SAVING
188int is_vmalloc_addr(const void *x)
189{
190 struct rb_node *n;
191 struct vmap_area *va;
192 int ret = 0;
193
194 spin_lock(&vmap_area_lock);
195
196 for (n = rb_first(vmap_area_root); n; rb_next(n)) {
197 va = rb_entry(n, struct vmap_area, rb_node);
198 if (x >= va->va_start && x < va->va_end) {
199 ret = 1;
200 break;
201 }
202 }
203
204 spin_unlock(&vmap_area_lock);
205 return ret;
206}
207#else
208int is_vmalloc_addr(const void *x)
209{
210 unsigned long addr = (unsigned long)x;
211
212 return addr >= VMALLOC_START && addr < VMALLOC_END;
213}
214#endif
215EXPORT_SYMBOL(is_vmalloc_addr);
216
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700217int is_vmalloc_or_module_addr(const void *x)
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700218{
219 /*
Russell Kingab4f2ee2008-11-06 17:11:07 +0000220 * ARM, x86-64 and sparc64 put modules in a special place,
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700221 * and fall back on vmalloc() if that fails. Others
222 * just put it in the vmalloc space.
223 */
224#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
225 unsigned long addr = (unsigned long)x;
226 if (addr >= MODULES_VADDR && addr < MODULES_END)
227 return 1;
228#endif
229 return is_vmalloc_addr(x);
230}
231
Christoph Lameter48667e72008-02-04 22:28:31 -0800232/*
Nick Piggindb64fe02008-10-18 20:27:03 -0700233 * Walk a vmap address to the struct page it maps.
Christoph Lameter48667e72008-02-04 22:28:31 -0800234 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800235struct page *vmalloc_to_page(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800236{
237 unsigned long addr = (unsigned long) vmalloc_addr;
238 struct page *page = NULL;
239 pgd_t *pgd = pgd_offset_k(addr);
Christoph Lameter48667e72008-02-04 22:28:31 -0800240
Ingo Molnar7aa413d2008-06-19 13:28:11 +0200241 /*
242 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
243 * architectures that do not vmalloc module space
244 */
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700245 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
Jiri Slaby59ea7462008-06-12 13:56:40 +0200246
Christoph Lameter48667e72008-02-04 22:28:31 -0800247 if (!pgd_none(*pgd)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700248 pud_t *pud = pud_offset(pgd, addr);
Christoph Lameter48667e72008-02-04 22:28:31 -0800249 if (!pud_none(*pud)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700250 pmd_t *pmd = pmd_offset(pud, addr);
Christoph Lameter48667e72008-02-04 22:28:31 -0800251 if (!pmd_none(*pmd)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700252 pte_t *ptep, pte;
253
Christoph Lameter48667e72008-02-04 22:28:31 -0800254 ptep = pte_offset_map(pmd, addr);
255 pte = *ptep;
256 if (pte_present(pte))
257 page = pte_page(pte);
258 pte_unmap(ptep);
259 }
260 }
261 }
262 return page;
263}
264EXPORT_SYMBOL(vmalloc_to_page);
265
266/*
267 * Map a vmalloc()-space virtual address to the physical page frame number.
268 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800269unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800270{
271 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
272}
273EXPORT_SYMBOL(vmalloc_to_pfn);
274
Nick Piggindb64fe02008-10-18 20:27:03 -0700275
276/*** Global kva allocator ***/
277
278#define VM_LAZY_FREE 0x01
279#define VM_LAZY_FREEING 0x02
280#define VM_VM_AREA 0x04
281
282struct vmap_area {
283 unsigned long va_start;
284 unsigned long va_end;
285 unsigned long flags;
286 struct rb_node rb_node; /* address sorted rbtree */
287 struct list_head list; /* address sorted list */
288 struct list_head purge_list; /* "lazy purge" list */
Minchan Kimdb1aeca2012-01-10 15:08:39 -0800289 struct vm_struct *vm;
Nick Piggindb64fe02008-10-18 20:27:03 -0700290 struct rcu_head rcu_head;
291};
292
293static DEFINE_SPINLOCK(vmap_area_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700294static LIST_HEAD(vmap_area_list);
Nick Piggin89699602011-03-22 16:30:36 -0700295static struct rb_root vmap_area_root = RB_ROOT;
296
297/* The vmap cache globals are protected by vmap_area_lock */
298static struct rb_node *free_vmap_cache;
299static unsigned long cached_hole_size;
300static unsigned long cached_vstart;
301static unsigned long cached_align;
302
Tejun Heoca23e402009-08-14 15:00:52 +0900303static unsigned long vmap_area_pcpu_hole;
Nick Piggindb64fe02008-10-18 20:27:03 -0700304
305static struct vmap_area *__find_vmap_area(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Nick Piggindb64fe02008-10-18 20:27:03 -0700307 struct rb_node *n = vmap_area_root.rb_node;
308
309 while (n) {
310 struct vmap_area *va;
311
312 va = rb_entry(n, struct vmap_area, rb_node);
313 if (addr < va->va_start)
314 n = n->rb_left;
315 else if (addr > va->va_start)
316 n = n->rb_right;
317 else
318 return va;
319 }
320
321 return NULL;
322}
323
324static void __insert_vmap_area(struct vmap_area *va)
325{
326 struct rb_node **p = &vmap_area_root.rb_node;
327 struct rb_node *parent = NULL;
328 struct rb_node *tmp;
329
330 while (*p) {
Namhyung Kim170168d2010-10-26 14:22:02 -0700331 struct vmap_area *tmp_va;
Nick Piggindb64fe02008-10-18 20:27:03 -0700332
333 parent = *p;
Namhyung Kim170168d2010-10-26 14:22:02 -0700334 tmp_va = rb_entry(parent, struct vmap_area, rb_node);
335 if (va->va_start < tmp_va->va_end)
Nick Piggindb64fe02008-10-18 20:27:03 -0700336 p = &(*p)->rb_left;
Namhyung Kim170168d2010-10-26 14:22:02 -0700337 else if (va->va_end > tmp_va->va_start)
Nick Piggindb64fe02008-10-18 20:27:03 -0700338 p = &(*p)->rb_right;
339 else
340 BUG();
341 }
342
343 rb_link_node(&va->rb_node, parent, p);
344 rb_insert_color(&va->rb_node, &vmap_area_root);
345
346 /* address-sort this list so it is usable like the vmlist */
347 tmp = rb_prev(&va->rb_node);
348 if (tmp) {
349 struct vmap_area *prev;
350 prev = rb_entry(tmp, struct vmap_area, rb_node);
351 list_add_rcu(&va->list, &prev->list);
352 } else
353 list_add_rcu(&va->list, &vmap_area_list);
354}
355
356static void purge_vmap_area_lazy(void);
357
358/*
359 * Allocate a region of KVA of the specified size and alignment, within the
360 * vstart and vend.
361 */
362static struct vmap_area *alloc_vmap_area(unsigned long size,
363 unsigned long align,
364 unsigned long vstart, unsigned long vend,
365 int node, gfp_t gfp_mask)
366{
367 struct vmap_area *va;
368 struct rb_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 unsigned long addr;
Nick Piggindb64fe02008-10-18 20:27:03 -0700370 int purged = 0;
Nick Piggin89699602011-03-22 16:30:36 -0700371 struct vmap_area *first;
Nick Piggindb64fe02008-10-18 20:27:03 -0700372
Nick Piggin77669702009-02-27 14:03:03 -0800373 BUG_ON(!size);
Nick Piggindb64fe02008-10-18 20:27:03 -0700374 BUG_ON(size & ~PAGE_MASK);
Nick Piggin89699602011-03-22 16:30:36 -0700375 BUG_ON(!is_power_of_2(align));
Nick Piggindb64fe02008-10-18 20:27:03 -0700376
Nick Piggindb64fe02008-10-18 20:27:03 -0700377 va = kmalloc_node(sizeof(struct vmap_area),
378 gfp_mask & GFP_RECLAIM_MASK, node);
379 if (unlikely(!va))
380 return ERR_PTR(-ENOMEM);
381
382retry:
383 spin_lock(&vmap_area_lock);
Nick Piggin89699602011-03-22 16:30:36 -0700384 /*
385 * Invalidate cache if we have more permissive parameters.
386 * cached_hole_size notes the largest hole noticed _below_
387 * the vmap_area cached in free_vmap_cache: if size fits
388 * into that hole, we want to scan from vstart to reuse
389 * the hole instead of allocating above free_vmap_cache.
390 * Note that __free_vmap_area may update free_vmap_cache
391 * without updating cached_hole_size or cached_align.
392 */
393 if (!free_vmap_cache ||
394 size < cached_hole_size ||
395 vstart < cached_vstart ||
396 align < cached_align) {
397nocache:
398 cached_hole_size = 0;
399 free_vmap_cache = NULL;
400 }
401 /* record if we encounter less permissive parameters */
402 cached_vstart = vstart;
403 cached_align = align;
Nick Piggin77669702009-02-27 14:03:03 -0800404
Nick Piggin89699602011-03-22 16:30:36 -0700405 /* find starting point for our search */
406 if (free_vmap_cache) {
407 first = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
Johannes Weiner248ac0e2011-05-24 17:11:43 -0700408 addr = ALIGN(first->va_end, align);
Nick Piggin89699602011-03-22 16:30:36 -0700409 if (addr < vstart)
410 goto nocache;
411 if (addr + size - 1 < addr)
412 goto overflow;
Nick Piggindb64fe02008-10-18 20:27:03 -0700413
Nick Piggin89699602011-03-22 16:30:36 -0700414 } else {
415 addr = ALIGN(vstart, align);
416 if (addr + size - 1 < addr)
417 goto overflow;
418
419 n = vmap_area_root.rb_node;
420 first = NULL;
421
422 while (n) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700423 struct vmap_area *tmp;
424 tmp = rb_entry(n, struct vmap_area, rb_node);
425 if (tmp->va_end >= addr) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700426 first = tmp;
Nick Piggin89699602011-03-22 16:30:36 -0700427 if (tmp->va_start <= addr)
428 break;
429 n = n->rb_left;
430 } else
Nick Piggindb64fe02008-10-18 20:27:03 -0700431 n = n->rb_right;
Nick Piggin89699602011-03-22 16:30:36 -0700432 }
Nick Piggindb64fe02008-10-18 20:27:03 -0700433
434 if (!first)
435 goto found;
Nick Piggindb64fe02008-10-18 20:27:03 -0700436 }
Nick Piggin89699602011-03-22 16:30:36 -0700437
438 /* from the starting point, walk areas until a suitable hole is found */
Johannes Weiner248ac0e2011-05-24 17:11:43 -0700439 while (addr + size > first->va_start && addr + size <= vend) {
Nick Piggin89699602011-03-22 16:30:36 -0700440 if (addr + cached_hole_size < first->va_start)
441 cached_hole_size = first->va_start - addr;
Johannes Weiner248ac0e2011-05-24 17:11:43 -0700442 addr = ALIGN(first->va_end, align);
Nick Piggin89699602011-03-22 16:30:36 -0700443 if (addr + size - 1 < addr)
444 goto overflow;
445
446 n = rb_next(&first->rb_node);
447 if (n)
448 first = rb_entry(n, struct vmap_area, rb_node);
449 else
450 goto found;
451 }
452
Nick Piggindb64fe02008-10-18 20:27:03 -0700453found:
Nick Piggin89699602011-03-22 16:30:36 -0700454 if (addr + size > vend)
455 goto overflow;
Nick Piggindb64fe02008-10-18 20:27:03 -0700456
457 va->va_start = addr;
458 va->va_end = addr + size;
459 va->flags = 0;
460 __insert_vmap_area(va);
Nick Piggin89699602011-03-22 16:30:36 -0700461 free_vmap_cache = &va->rb_node;
Nick Piggindb64fe02008-10-18 20:27:03 -0700462 spin_unlock(&vmap_area_lock);
463
Nick Piggin89699602011-03-22 16:30:36 -0700464 BUG_ON(va->va_start & (align-1));
465 BUG_ON(va->va_start < vstart);
466 BUG_ON(va->va_end > vend);
467
Nick Piggindb64fe02008-10-18 20:27:03 -0700468 return va;
Nick Piggin89699602011-03-22 16:30:36 -0700469
470overflow:
471 spin_unlock(&vmap_area_lock);
472 if (!purged) {
473 purge_vmap_area_lazy();
474 purged = 1;
475 goto retry;
476 }
477 if (printk_ratelimit())
478 printk(KERN_WARNING
479 "vmap allocation for size %lu failed: "
480 "use vmalloc=<size> to increase size.\n", size);
481 kfree(va);
482 return ERR_PTR(-EBUSY);
Nick Piggindb64fe02008-10-18 20:27:03 -0700483}
484
Nick Piggindb64fe02008-10-18 20:27:03 -0700485static void __free_vmap_area(struct vmap_area *va)
486{
487 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
Nick Piggin89699602011-03-22 16:30:36 -0700488
489 if (free_vmap_cache) {
490 if (va->va_end < cached_vstart) {
491 free_vmap_cache = NULL;
492 } else {
493 struct vmap_area *cache;
494 cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
495 if (va->va_start <= cache->va_start) {
496 free_vmap_cache = rb_prev(&va->rb_node);
497 /*
498 * We don't try to update cached_hole_size or
499 * cached_align, but it won't go very wrong.
500 */
501 }
502 }
503 }
Nick Piggindb64fe02008-10-18 20:27:03 -0700504 rb_erase(&va->rb_node, &vmap_area_root);
505 RB_CLEAR_NODE(&va->rb_node);
506 list_del_rcu(&va->list);
507
Tejun Heoca23e402009-08-14 15:00:52 +0900508 /*
509 * Track the highest possible candidate for pcpu area
510 * allocation. Areas outside of vmalloc area can be returned
511 * here too, consider only end addresses which fall inside
512 * vmalloc area proper.
513 */
514 if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
515 vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
516
Lai Jiangshan14769de2011-03-18 12:12:19 +0800517 kfree_rcu(va, rcu_head);
Nick Piggindb64fe02008-10-18 20:27:03 -0700518}
519
520/*
521 * Free a region of KVA allocated by alloc_vmap_area
522 */
523static void free_vmap_area(struct vmap_area *va)
524{
525 spin_lock(&vmap_area_lock);
526 __free_vmap_area(va);
527 spin_unlock(&vmap_area_lock);
528}
529
530/*
531 * Clear the pagetable entries of a given vmap_area
532 */
533static void unmap_vmap_area(struct vmap_area *va)
534{
535 vunmap_page_range(va->va_start, va->va_end);
536}
537
Nick Piggincd528582009-01-06 14:39:20 -0800538static void vmap_debug_free_range(unsigned long start, unsigned long end)
539{
540 /*
541 * Unmap page tables and force a TLB flush immediately if
542 * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
543 * bugs similarly to those in linear kernel virtual address
544 * space after a page has been freed.
545 *
546 * All the lazy freeing logic is still retained, in order to
547 * minimise intrusiveness of this debugging feature.
548 *
549 * This is going to be *slow* (linear kernel virtual address
550 * debugging doesn't do a broadcast TLB flush so it is a lot
551 * faster).
552 */
553#ifdef CONFIG_DEBUG_PAGEALLOC
554 vunmap_page_range(start, end);
555 flush_tlb_kernel_range(start, end);
556#endif
557}
558
Nick Piggindb64fe02008-10-18 20:27:03 -0700559/*
560 * lazy_max_pages is the maximum amount of virtual address space we gather up
561 * before attempting to purge with a TLB flush.
562 *
563 * There is a tradeoff here: a larger number will cover more kernel page tables
564 * and take slightly longer to purge, but it will linearly reduce the number of
565 * global TLB flushes that must be performed. It would seem natural to scale
566 * this number up linearly with the number of CPUs (because vmapping activity
567 * could also scale linearly with the number of CPUs), however it is likely
568 * that in practice, workloads might be constrained in other ways that mean
569 * vmap activity will not scale linearly with CPUs. Also, I want to be
570 * conservative and not introduce a big latency on huge systems, so go with
571 * a less aggressive log scale. It will still be an improvement over the old
572 * code, and it will be simple to change the scale factor if we find that it
573 * becomes a problem on bigger systems.
574 */
575static unsigned long lazy_max_pages(void)
576{
577 unsigned int log;
578
579 log = fls(num_online_cpus());
580
581 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
582}
583
584static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
585
Nick Piggin02b709d2010-02-01 22:25:57 +1100586/* for per-CPU blocks */
587static void purge_fragmented_blocks_allcpus(void);
588
Nick Piggindb64fe02008-10-18 20:27:03 -0700589/*
Cliff Wickman3ee48b62010-09-16 11:44:02 -0500590 * called before a call to iounmap() if the caller wants vm_area_struct's
591 * immediately freed.
592 */
593void set_iounmap_nonlazy(void)
594{
595 atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
596}
597
598/*
Nick Piggindb64fe02008-10-18 20:27:03 -0700599 * Purges all lazily-freed vmap areas.
600 *
601 * If sync is 0 then don't purge if there is already a purge in progress.
602 * If force_flush is 1, then flush kernel TLBs between *start and *end even
603 * if we found no lazy vmap areas to unmap (callers can use this to optimise
604 * their own TLB flushing).
605 * Returns with *start = min(*start, lowest purged address)
606 * *end = max(*end, highest purged address)
607 */
608static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
609 int sync, int force_flush)
610{
Andrew Morton46666d82009-01-15 13:51:15 -0800611 static DEFINE_SPINLOCK(purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700612 LIST_HEAD(valist);
613 struct vmap_area *va;
Vegard Nossumcbb76672009-02-27 14:03:04 -0800614 struct vmap_area *n_va;
Nick Piggindb64fe02008-10-18 20:27:03 -0700615 int nr = 0;
616
617 /*
618 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
619 * should not expect such behaviour. This just simplifies locking for
620 * the case that isn't actually used at the moment anyway.
621 */
622 if (!sync && !force_flush) {
Andrew Morton46666d82009-01-15 13:51:15 -0800623 if (!spin_trylock(&purge_lock))
Nick Piggindb64fe02008-10-18 20:27:03 -0700624 return;
625 } else
Andrew Morton46666d82009-01-15 13:51:15 -0800626 spin_lock(&purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700627
Nick Piggin02b709d2010-02-01 22:25:57 +1100628 if (sync)
629 purge_fragmented_blocks_allcpus();
630
Nick Piggindb64fe02008-10-18 20:27:03 -0700631 rcu_read_lock();
632 list_for_each_entry_rcu(va, &vmap_area_list, list) {
633 if (va->flags & VM_LAZY_FREE) {
634 if (va->va_start < *start)
635 *start = va->va_start;
636 if (va->va_end > *end)
637 *end = va->va_end;
638 nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -0700639 list_add_tail(&va->purge_list, &valist);
640 va->flags |= VM_LAZY_FREEING;
641 va->flags &= ~VM_LAZY_FREE;
642 }
643 }
644 rcu_read_unlock();
645
Yongseok Koh88f50042010-01-19 17:33:49 +0900646 if (nr)
Nick Piggindb64fe02008-10-18 20:27:03 -0700647 atomic_sub(nr, &vmap_lazy_nr);
Nick Piggindb64fe02008-10-18 20:27:03 -0700648
649 if (nr || force_flush)
650 flush_tlb_kernel_range(*start, *end);
651
652 if (nr) {
653 spin_lock(&vmap_area_lock);
Vegard Nossumcbb76672009-02-27 14:03:04 -0800654 list_for_each_entry_safe(va, n_va, &valist, purge_list)
Nick Piggindb64fe02008-10-18 20:27:03 -0700655 __free_vmap_area(va);
656 spin_unlock(&vmap_area_lock);
657 }
Andrew Morton46666d82009-01-15 13:51:15 -0800658 spin_unlock(&purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700659}
660
661/*
Nick Piggin496850e2008-11-19 15:36:33 -0800662 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
663 * is already purging.
664 */
665static void try_purge_vmap_area_lazy(void)
666{
667 unsigned long start = ULONG_MAX, end = 0;
668
669 __purge_vmap_area_lazy(&start, &end, 0, 0);
670}
671
672/*
Nick Piggindb64fe02008-10-18 20:27:03 -0700673 * Kick off a purge of the outstanding lazy areas.
674 */
675static void purge_vmap_area_lazy(void)
676{
677 unsigned long start = ULONG_MAX, end = 0;
678
Nick Piggin496850e2008-11-19 15:36:33 -0800679 __purge_vmap_area_lazy(&start, &end, 1, 0);
Nick Piggindb64fe02008-10-18 20:27:03 -0700680}
681
682/*
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -0800683 * Free a vmap area, caller ensuring that the area has been unmapped
684 * and flush_cache_vunmap had been called for the correct range
685 * previously.
Nick Piggindb64fe02008-10-18 20:27:03 -0700686 */
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -0800687static void free_vmap_area_noflush(struct vmap_area *va)
Nick Piggindb64fe02008-10-18 20:27:03 -0700688{
689 va->flags |= VM_LAZY_FREE;
690 atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
691 if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
Nick Piggin496850e2008-11-19 15:36:33 -0800692 try_purge_vmap_area_lazy();
Nick Piggindb64fe02008-10-18 20:27:03 -0700693}
694
Nick Pigginb29acbd2008-12-01 13:13:47 -0800695/*
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -0800696 * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
697 * called for the correct range previously.
698 */
699static void free_unmap_vmap_area_noflush(struct vmap_area *va)
700{
701 unmap_vmap_area(va);
702 free_vmap_area_noflush(va);
703}
704
705/*
Nick Pigginb29acbd2008-12-01 13:13:47 -0800706 * Free and unmap a vmap area
707 */
708static void free_unmap_vmap_area(struct vmap_area *va)
709{
710 flush_cache_vunmap(va->va_start, va->va_end);
711 free_unmap_vmap_area_noflush(va);
712}
713
Nick Piggindb64fe02008-10-18 20:27:03 -0700714static struct vmap_area *find_vmap_area(unsigned long addr)
715{
716 struct vmap_area *va;
717
718 spin_lock(&vmap_area_lock);
719 va = __find_vmap_area(addr);
720 spin_unlock(&vmap_area_lock);
721
722 return va;
723}
724
725static void free_unmap_vmap_area_addr(unsigned long addr)
726{
727 struct vmap_area *va;
728
729 va = find_vmap_area(addr);
730 BUG_ON(!va);
731 free_unmap_vmap_area(va);
732}
733
734
735/*** Per cpu kva allocator ***/
736
737/*
738 * vmap space is limited especially on 32 bit architectures. Ensure there is
739 * room for at least 16 percpu vmap blocks per CPU.
740 */
741/*
742 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
743 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
744 * instead (we just need a rough idea)
745 */
746#if BITS_PER_LONG == 32
747#define VMALLOC_SPACE (128UL*1024*1024)
748#else
749#define VMALLOC_SPACE (128UL*1024*1024*1024)
750#endif
751
752#define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
753#define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
754#define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
755#define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
756#define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
757#define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
Clemens Ladischf982f912011-06-21 22:09:50 +0200758#define VMAP_BBMAP_BITS \
759 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
760 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
761 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
Nick Piggindb64fe02008-10-18 20:27:03 -0700762
763#define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
764
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +1100765static bool vmap_initialized __read_mostly = false;
766
Nick Piggindb64fe02008-10-18 20:27:03 -0700767struct vmap_block_queue {
768 spinlock_t lock;
769 struct list_head free;
Nick Piggindb64fe02008-10-18 20:27:03 -0700770};
771
772struct vmap_block {
773 spinlock_t lock;
774 struct vmap_area *va;
775 struct vmap_block_queue *vbq;
776 unsigned long free, dirty;
777 DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
778 DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
Nick Pigginde560422010-02-01 22:24:18 +1100779 struct list_head free_list;
780 struct rcu_head rcu_head;
Nick Piggin02b709d2010-02-01 22:25:57 +1100781 struct list_head purge;
Nick Piggindb64fe02008-10-18 20:27:03 -0700782};
783
784/* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
785static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
786
787/*
788 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
789 * in the free path. Could get rid of this if we change the API to return a
790 * "cookie" from alloc, to be passed to free. But no big deal yet.
791 */
792static DEFINE_SPINLOCK(vmap_block_tree_lock);
793static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
794
795/*
796 * We should probably have a fallback mechanism to allocate virtual memory
797 * out of partially filled vmap blocks. However vmap block sizing should be
798 * fairly reasonable according to the vmalloc size, so it shouldn't be a
799 * big problem.
800 */
801
802static unsigned long addr_to_vb_idx(unsigned long addr)
803{
804 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
805 addr /= VMAP_BLOCK_SIZE;
806 return addr;
807}
808
809static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
810{
811 struct vmap_block_queue *vbq;
812 struct vmap_block *vb;
813 struct vmap_area *va;
814 unsigned long vb_idx;
815 int node, err;
816
817 node = numa_node_id();
818
819 vb = kmalloc_node(sizeof(struct vmap_block),
820 gfp_mask & GFP_RECLAIM_MASK, node);
821 if (unlikely(!vb))
822 return ERR_PTR(-ENOMEM);
823
824 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
825 VMALLOC_START, VMALLOC_END,
826 node, gfp_mask);
Tobias Klauserddf9c6d2011-01-13 15:46:15 -0800827 if (IS_ERR(va)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700828 kfree(vb);
Julia Lawalle7d86342010-08-09 17:18:28 -0700829 return ERR_CAST(va);
Nick Piggindb64fe02008-10-18 20:27:03 -0700830 }
831
832 err = radix_tree_preload(gfp_mask);
833 if (unlikely(err)) {
834 kfree(vb);
835 free_vmap_area(va);
836 return ERR_PTR(err);
837 }
838
839 spin_lock_init(&vb->lock);
840 vb->va = va;
841 vb->free = VMAP_BBMAP_BITS;
842 vb->dirty = 0;
843 bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
844 bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
845 INIT_LIST_HEAD(&vb->free_list);
Nick Piggindb64fe02008-10-18 20:27:03 -0700846
847 vb_idx = addr_to_vb_idx(va->va_start);
848 spin_lock(&vmap_block_tree_lock);
849 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
850 spin_unlock(&vmap_block_tree_lock);
851 BUG_ON(err);
852 radix_tree_preload_end();
853
854 vbq = &get_cpu_var(vmap_block_queue);
855 vb->vbq = vbq;
856 spin_lock(&vbq->lock);
Nick Pigginde560422010-02-01 22:24:18 +1100857 list_add_rcu(&vb->free_list, &vbq->free);
Nick Piggindb64fe02008-10-18 20:27:03 -0700858 spin_unlock(&vbq->lock);
Tejun Heo3f04ba82009-10-29 22:34:12 +0900859 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -0700860
861 return vb;
862}
863
Nick Piggindb64fe02008-10-18 20:27:03 -0700864static void free_vmap_block(struct vmap_block *vb)
865{
866 struct vmap_block *tmp;
867 unsigned long vb_idx;
868
Nick Piggindb64fe02008-10-18 20:27:03 -0700869 vb_idx = addr_to_vb_idx(vb->va->va_start);
870 spin_lock(&vmap_block_tree_lock);
871 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
872 spin_unlock(&vmap_block_tree_lock);
873 BUG_ON(tmp != vb);
874
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -0800875 free_vmap_area_noflush(vb->va);
Lai Jiangshan22a3c7d2011-03-18 12:13:08 +0800876 kfree_rcu(vb, rcu_head);
Nick Piggindb64fe02008-10-18 20:27:03 -0700877}
878
Nick Piggin02b709d2010-02-01 22:25:57 +1100879static void purge_fragmented_blocks(int cpu)
880{
881 LIST_HEAD(purge);
882 struct vmap_block *vb;
883 struct vmap_block *n_vb;
884 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
885
886 rcu_read_lock();
887 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
888
889 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
890 continue;
891
892 spin_lock(&vb->lock);
893 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
894 vb->free = 0; /* prevent further allocs after releasing lock */
895 vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
896 bitmap_fill(vb->alloc_map, VMAP_BBMAP_BITS);
897 bitmap_fill(vb->dirty_map, VMAP_BBMAP_BITS);
898 spin_lock(&vbq->lock);
899 list_del_rcu(&vb->free_list);
900 spin_unlock(&vbq->lock);
901 spin_unlock(&vb->lock);
902 list_add_tail(&vb->purge, &purge);
903 } else
904 spin_unlock(&vb->lock);
905 }
906 rcu_read_unlock();
907
908 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
909 list_del(&vb->purge);
910 free_vmap_block(vb);
911 }
912}
913
914static void purge_fragmented_blocks_thiscpu(void)
915{
916 purge_fragmented_blocks(smp_processor_id());
917}
918
919static void purge_fragmented_blocks_allcpus(void)
920{
921 int cpu;
922
923 for_each_possible_cpu(cpu)
924 purge_fragmented_blocks(cpu);
925}
926
Nick Piggindb64fe02008-10-18 20:27:03 -0700927static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
928{
929 struct vmap_block_queue *vbq;
930 struct vmap_block *vb;
931 unsigned long addr = 0;
932 unsigned int order;
Nick Piggin02b709d2010-02-01 22:25:57 +1100933 int purge = 0;
Nick Piggindb64fe02008-10-18 20:27:03 -0700934
935 BUG_ON(size & ~PAGE_MASK);
936 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
937 order = get_order(size);
938
939again:
940 rcu_read_lock();
941 vbq = &get_cpu_var(vmap_block_queue);
942 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
943 int i;
944
945 spin_lock(&vb->lock);
Nick Piggin02b709d2010-02-01 22:25:57 +1100946 if (vb->free < 1UL << order)
947 goto next;
948
Nick Piggindb64fe02008-10-18 20:27:03 -0700949 i = bitmap_find_free_region(vb->alloc_map,
950 VMAP_BBMAP_BITS, order);
951
Nick Piggin02b709d2010-02-01 22:25:57 +1100952 if (i < 0) {
953 if (vb->free + vb->dirty == VMAP_BBMAP_BITS) {
954 /* fragmented and no outstanding allocations */
955 BUG_ON(vb->dirty != VMAP_BBMAP_BITS);
956 purge = 1;
Nick Piggindb64fe02008-10-18 20:27:03 -0700957 }
Nick Piggin02b709d2010-02-01 22:25:57 +1100958 goto next;
959 }
960 addr = vb->va->va_start + (i << PAGE_SHIFT);
961 BUG_ON(addr_to_vb_idx(addr) !=
962 addr_to_vb_idx(vb->va->va_start));
963 vb->free -= 1UL << order;
964 if (vb->free == 0) {
965 spin_lock(&vbq->lock);
966 list_del_rcu(&vb->free_list);
967 spin_unlock(&vbq->lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700968 }
969 spin_unlock(&vb->lock);
Nick Piggin02b709d2010-02-01 22:25:57 +1100970 break;
971next:
972 spin_unlock(&vb->lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700973 }
Nick Piggin02b709d2010-02-01 22:25:57 +1100974
975 if (purge)
976 purge_fragmented_blocks_thiscpu();
977
Tejun Heo3f04ba82009-10-29 22:34:12 +0900978 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -0700979 rcu_read_unlock();
980
981 if (!addr) {
982 vb = new_vmap_block(gfp_mask);
983 if (IS_ERR(vb))
984 return vb;
985 goto again;
986 }
987
988 return (void *)addr;
989}
990
991static void vb_free(const void *addr, unsigned long size)
992{
993 unsigned long offset;
994 unsigned long vb_idx;
995 unsigned int order;
996 struct vmap_block *vb;
997
998 BUG_ON(size & ~PAGE_MASK);
999 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001000
1001 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
1002
Nick Piggindb64fe02008-10-18 20:27:03 -07001003 order = get_order(size);
1004
1005 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
1006
1007 vb_idx = addr_to_vb_idx((unsigned long)addr);
1008 rcu_read_lock();
1009 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
1010 rcu_read_unlock();
1011 BUG_ON(!vb);
1012
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001013 vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
1014
Nick Piggindb64fe02008-10-18 20:27:03 -07001015 spin_lock(&vb->lock);
Nick Pigginde560422010-02-01 22:24:18 +11001016 BUG_ON(bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order));
MinChan Kimd0868172009-03-31 15:19:26 -07001017
Nick Piggindb64fe02008-10-18 20:27:03 -07001018 vb->dirty += 1UL << order;
1019 if (vb->dirty == VMAP_BBMAP_BITS) {
Nick Pigginde560422010-02-01 22:24:18 +11001020 BUG_ON(vb->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001021 spin_unlock(&vb->lock);
1022 free_vmap_block(vb);
1023 } else
1024 spin_unlock(&vb->lock);
1025}
1026
1027/**
1028 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1029 *
1030 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1031 * to amortize TLB flushing overheads. What this means is that any page you
1032 * have now, may, in a former life, have been mapped into kernel virtual
1033 * address by the vmap layer and so there might be some CPUs with TLB entries
1034 * still referencing that page (additional to the regular 1:1 kernel mapping).
1035 *
1036 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1037 * be sure that none of the pages we have control over will have any aliases
1038 * from the vmap layer.
1039 */
1040void vm_unmap_aliases(void)
1041{
1042 unsigned long start = ULONG_MAX, end = 0;
1043 int cpu;
1044 int flush = 0;
1045
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001046 if (unlikely(!vmap_initialized))
1047 return;
1048
Nick Piggindb64fe02008-10-18 20:27:03 -07001049 for_each_possible_cpu(cpu) {
1050 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1051 struct vmap_block *vb;
1052
1053 rcu_read_lock();
1054 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1055 int i;
1056
1057 spin_lock(&vb->lock);
1058 i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
1059 while (i < VMAP_BBMAP_BITS) {
1060 unsigned long s, e;
1061 int j;
1062 j = find_next_zero_bit(vb->dirty_map,
1063 VMAP_BBMAP_BITS, i);
1064
1065 s = vb->va->va_start + (i << PAGE_SHIFT);
1066 e = vb->va->va_start + (j << PAGE_SHIFT);
Nick Piggindb64fe02008-10-18 20:27:03 -07001067 flush = 1;
1068
1069 if (s < start)
1070 start = s;
1071 if (e > end)
1072 end = e;
1073
1074 i = j;
1075 i = find_next_bit(vb->dirty_map,
1076 VMAP_BBMAP_BITS, i);
1077 }
1078 spin_unlock(&vb->lock);
1079 }
1080 rcu_read_unlock();
1081 }
1082
1083 __purge_vmap_area_lazy(&start, &end, 1, flush);
1084}
1085EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1086
1087/**
1088 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1089 * @mem: the pointer returned by vm_map_ram
1090 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1091 */
1092void vm_unmap_ram(const void *mem, unsigned int count)
1093{
1094 unsigned long size = count << PAGE_SHIFT;
1095 unsigned long addr = (unsigned long)mem;
1096
1097 BUG_ON(!addr);
1098 BUG_ON(addr < VMALLOC_START);
1099 BUG_ON(addr > VMALLOC_END);
1100 BUG_ON(addr & (PAGE_SIZE-1));
1101
1102 debug_check_no_locks_freed(mem, size);
Nick Piggincd528582009-01-06 14:39:20 -08001103 vmap_debug_free_range(addr, addr+size);
Nick Piggindb64fe02008-10-18 20:27:03 -07001104
1105 if (likely(count <= VMAP_MAX_ALLOC))
1106 vb_free(mem, size);
1107 else
1108 free_unmap_vmap_area_addr(addr);
1109}
1110EXPORT_SYMBOL(vm_unmap_ram);
1111
1112/**
1113 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1114 * @pages: an array of pointers to the pages to be mapped
1115 * @count: number of pages
1116 * @node: prefer to allocate data structures on this node
1117 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
Randy Dunlape99c97a2008-10-29 14:01:09 -07001118 *
1119 * Returns: a pointer to the address that has been mapped, or %NULL on failure
Nick Piggindb64fe02008-10-18 20:27:03 -07001120 */
1121void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1122{
1123 unsigned long size = count << PAGE_SHIFT;
1124 unsigned long addr;
1125 void *mem;
1126
1127 if (likely(count <= VMAP_MAX_ALLOC)) {
1128 mem = vb_alloc(size, GFP_KERNEL);
1129 if (IS_ERR(mem))
1130 return NULL;
1131 addr = (unsigned long)mem;
1132 } else {
1133 struct vmap_area *va;
1134 va = alloc_vmap_area(size, PAGE_SIZE,
1135 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1136 if (IS_ERR(va))
1137 return NULL;
1138
1139 addr = va->va_start;
1140 mem = (void *)addr;
1141 }
1142 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1143 vm_unmap_ram(mem, count);
1144 return NULL;
1145 }
1146 return mem;
1147}
1148EXPORT_SYMBOL(vm_map_ram);
Neeti Desaic278c942013-06-10 17:14:21 -07001149/**
1150 * vm_area_check_early - check if vmap area is already mapped
1151 * @vm: vm_struct to be checked
1152 *
1153 * This function is used to check if the vmap area has been
1154 * mapped already. @vm->addr, @vm->size and @vm->flags should
1155 * contain proper values.
1156 *
1157 */
1158int __init vm_area_check_early(struct vm_struct *vm)
1159{
1160 struct vm_struct *tmp, **p;
Nick Piggindb64fe02008-10-18 20:27:03 -07001161
Neeti Desaic278c942013-06-10 17:14:21 -07001162 BUG_ON(vmap_initialized);
1163 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1164 if (tmp->addr >= vm->addr) {
1165 if (tmp->addr < vm->addr + vm->size)
1166 return 1;
1167 } else {
1168 if (tmp->addr + tmp->size > vm->addr)
1169 return 1;
1170 }
1171 }
1172 return 0;
1173}
Tejun Heof0aa6612009-02-20 16:29:08 +09001174/**
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001175 * vm_area_add_early - add vmap area early during boot
1176 * @vm: vm_struct to add
1177 *
1178 * This function is used to add fixed kernel vm area to vmlist before
1179 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
1180 * should contain proper values and the other fields should be zero.
1181 *
1182 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1183 */
1184void __init vm_area_add_early(struct vm_struct *vm)
1185{
1186 struct vm_struct *tmp, **p;
1187
1188 BUG_ON(vmap_initialized);
1189 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1190 if (tmp->addr >= vm->addr) {
1191 BUG_ON(tmp->addr < vm->addr + vm->size);
1192 break;
1193 } else
1194 BUG_ON(tmp->addr + tmp->size > vm->addr);
1195 }
1196 vm->next = *p;
1197 *p = vm;
1198}
1199
1200/**
Tejun Heof0aa6612009-02-20 16:29:08 +09001201 * vm_area_register_early - register vmap area early during boot
1202 * @vm: vm_struct to register
Tejun Heoc0c0a292009-02-24 11:57:21 +09001203 * @align: requested alignment
Tejun Heof0aa6612009-02-20 16:29:08 +09001204 *
1205 * This function is used to register kernel vm area before
1206 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1207 * proper values on entry and other fields should be zero. On return,
1208 * vm->addr contains the allocated address.
1209 *
1210 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1211 */
Tejun Heoc0c0a292009-02-24 11:57:21 +09001212void __init vm_area_register_early(struct vm_struct *vm, size_t align)
Tejun Heof0aa6612009-02-20 16:29:08 +09001213{
1214 static size_t vm_init_off __initdata;
Tejun Heoc0c0a292009-02-24 11:57:21 +09001215 unsigned long addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001216
Tejun Heoc0c0a292009-02-24 11:57:21 +09001217 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1218 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1219
1220 vm->addr = (void *)addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001221
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001222 vm_area_add_early(vm);
Tejun Heof0aa6612009-02-20 16:29:08 +09001223}
1224
Nick Piggindb64fe02008-10-18 20:27:03 -07001225void __init vmalloc_init(void)
1226{
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001227 struct vmap_area *va;
1228 struct vm_struct *tmp;
Nick Piggindb64fe02008-10-18 20:27:03 -07001229 int i;
1230
1231 for_each_possible_cpu(i) {
1232 struct vmap_block_queue *vbq;
1233
1234 vbq = &per_cpu(vmap_block_queue, i);
1235 spin_lock_init(&vbq->lock);
1236 INIT_LIST_HEAD(&vbq->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001237 }
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001238
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001239 /* Import existing vmlist entries. */
1240 for (tmp = vmlist; tmp; tmp = tmp->next) {
Pekka Enberg43ebdac2009-05-25 15:01:35 +03001241 va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
KyongHofa002622012-05-29 15:06:49 -07001242 va->flags = VM_VM_AREA;
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001243 va->va_start = (unsigned long)tmp->addr;
1244 va->va_end = va->va_start + tmp->size;
KyongHofa002622012-05-29 15:06:49 -07001245 va->vm = tmp;
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001246 __insert_vmap_area(va);
1247 }
Tejun Heoca23e402009-08-14 15:00:52 +09001248
1249 vmap_area_pcpu_hole = VMALLOC_END;
1250
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001251 vmap_initialized = true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001252}
1253
Tejun Heo8fc48982009-02-20 16:29:08 +09001254/**
1255 * map_kernel_range_noflush - map kernel VM area with the specified pages
1256 * @addr: start of the VM area to map
1257 * @size: size of the VM area to map
1258 * @prot: page protection flags to use
1259 * @pages: pages to map
1260 *
1261 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1262 * specify should have been allocated using get_vm_area() and its
1263 * friends.
1264 *
1265 * NOTE:
1266 * This function does NOT do any cache flushing. The caller is
1267 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1268 * before calling this function.
1269 *
1270 * RETURNS:
1271 * The number of pages mapped on success, -errno on failure.
1272 */
1273int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1274 pgprot_t prot, struct page **pages)
1275{
1276 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1277}
1278
1279/**
1280 * unmap_kernel_range_noflush - unmap kernel VM area
1281 * @addr: start of the VM area to unmap
1282 * @size: size of the VM area to unmap
1283 *
1284 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1285 * specify should have been allocated using get_vm_area() and its
1286 * friends.
1287 *
1288 * NOTE:
1289 * This function does NOT do any cache flushing. The caller is
1290 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1291 * before calling this function and flush_tlb_kernel_range() after.
1292 */
1293void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1294{
1295 vunmap_page_range(addr, addr + size);
1296}
Huang Ying81e88fd2011-01-12 14:44:55 +08001297EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
Tejun Heo8fc48982009-02-20 16:29:08 +09001298
1299/**
1300 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1301 * @addr: start of the VM area to unmap
1302 * @size: size of the VM area to unmap
1303 *
1304 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1305 * the unmapping and tlb after.
1306 */
Nick Piggindb64fe02008-10-18 20:27:03 -07001307void unmap_kernel_range(unsigned long addr, unsigned long size)
1308{
1309 unsigned long end = addr + size;
Tejun Heof6fcba72009-02-20 15:38:48 -08001310
1311 flush_cache_vunmap(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -07001312 vunmap_page_range(addr, end);
1313 flush_tlb_kernel_range(addr, end);
1314}
1315
1316int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
1317{
1318 unsigned long addr = (unsigned long)area->addr;
1319 unsigned long end = addr + area->size - PAGE_SIZE;
1320 int err;
1321
1322 err = vmap_page_range(addr, end, prot, *pages);
1323 if (err > 0) {
1324 *pages += err;
1325 err = 0;
1326 }
1327
1328 return err;
1329}
1330EXPORT_SYMBOL_GPL(map_vm_area);
1331
1332/*** Old vmalloc interfaces ***/
1333DEFINE_RWLOCK(vmlist_lock);
1334struct vm_struct *vmlist;
1335
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001336static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001337 unsigned long flags, const void *caller)
Tejun Heocf88c792009-08-14 15:00:52 +09001338{
Tejun Heocf88c792009-08-14 15:00:52 +09001339 vm->flags = flags;
1340 vm->addr = (void *)va->va_start;
1341 vm->size = va->va_end - va->va_start;
1342 vm->caller = caller;
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001343 va->vm = vm;
Tejun Heocf88c792009-08-14 15:00:52 +09001344 va->flags |= VM_VM_AREA;
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001345}
Tejun Heocf88c792009-08-14 15:00:52 +09001346
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001347static void insert_vmalloc_vmlist(struct vm_struct *vm)
1348{
1349 struct vm_struct *tmp, **p;
1350
1351 vm->flags &= ~VM_UNLIST;
Tejun Heocf88c792009-08-14 15:00:52 +09001352 write_lock(&vmlist_lock);
1353 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1354 if (tmp->addr >= vm->addr)
1355 break;
1356 }
1357 vm->next = *p;
1358 *p = vm;
1359 write_unlock(&vmlist_lock);
1360}
1361
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001362static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001363 unsigned long flags, const void *caller)
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001364{
1365 setup_vmalloc_vm(vm, va, flags, caller);
1366 insert_vmalloc_vmlist(vm);
1367}
1368
Nick Piggindb64fe02008-10-18 20:27:03 -07001369static struct vm_struct *__get_vm_area_node(unsigned long size,
David Miller2dca6992009-09-21 12:22:34 -07001370 unsigned long align, unsigned long flags, unsigned long start,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001371 unsigned long end, int node, gfp_t gfp_mask, const void *caller)
Nick Piggindb64fe02008-10-18 20:27:03 -07001372{
Kautuk Consul00065262011-12-19 17:12:04 -08001373 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001374 struct vm_struct *area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -07001376 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (flags & VM_IOREMAP) {
1378 int bit = fls(size);
1379
1380 if (bit > IOREMAP_MAX_ORDER)
1381 bit = IOREMAP_MAX_ORDER;
1382 else if (bit < PAGE_SHIFT)
1383 bit = PAGE_SHIFT;
1384
1385 align = 1ul << bit;
1386 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 size = PAGE_ALIGN(size);
OGAWA Hirofumi31be8302006-11-16 01:19:29 -08001389 if (unlikely(!size))
1390 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Tejun Heocf88c792009-08-14 15:00:52 +09001392 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (unlikely(!area))
1394 return NULL;
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /*
1397 * We always allocate a guard page.
1398 */
1399 size += PAGE_SIZE;
1400
Nick Piggindb64fe02008-10-18 20:27:03 -07001401 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1402 if (IS_ERR(va)) {
1403 kfree(area);
1404 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001407 /*
1408 * When this function is called from __vmalloc_node_range,
1409 * we do not add vm_struct to vmlist here to avoid
1410 * accessing uninitialized members of vm_struct such as
1411 * pages and nr_pages fields. They will be set later.
1412 * To distinguish it from others, we use a VM_UNLIST flag.
1413 */
1414 if (flags & VM_UNLIST)
1415 setup_vmalloc_vm(area, va, flags, caller);
1416 else
1417 insert_vmalloc_vm(area, va, flags, caller);
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Christoph Lameter930fc452005-10-29 18:15:41 -07001422struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1423 unsigned long start, unsigned long end)
1424{
David Miller2dca6992009-09-21 12:22:34 -07001425 return __get_vm_area_node(size, 1, flags, start, end, -1, GFP_KERNEL,
Christoph Lameter23016962008-04-28 02:12:42 -07001426 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001427}
Rusty Russell5992b6d2007-07-19 01:49:21 -07001428EXPORT_SYMBOL_GPL(__get_vm_area);
Christoph Lameter930fc452005-10-29 18:15:41 -07001429
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001430struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1431 unsigned long start, unsigned long end,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001432 const void *caller)
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001433{
David Miller2dca6992009-09-21 12:22:34 -07001434 return __get_vm_area_node(size, 1, flags, start, end, -1, GFP_KERNEL,
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001435 caller);
1436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438/**
Simon Arlott183ff222007-10-20 01:27:18 +02001439 * get_vm_area - reserve a contiguous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * @size: size of the area
1441 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
1442 *
1443 * Search an area of @size in the kernel virtual mapping area,
1444 * and reserved it for out purposes. Returns the area descriptor
1445 * on success or %NULL on failure.
1446 */
1447struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
1448{
Neeti Desaie6ccd032013-06-14 17:39:33 -07001449#ifdef CONFIG_ENABLE_VMALLOC_SAVING
1450 return __get_vm_area_node(size, 1, flags, PAGE_OFFSET, VMALLOC_END,
1451 -1, GFP_KERNEL, __builtin_return_address(0));
1452#else
David Miller2dca6992009-09-21 12:22:34 -07001453 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
Christoph Lameter23016962008-04-28 02:12:42 -07001454 -1, GFP_KERNEL, __builtin_return_address(0));
Neeti Desaie6ccd032013-06-14 17:39:33 -07001455#endif
1456
Christoph Lameter23016962008-04-28 02:12:42 -07001457}
1458
1459struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001460 const void *caller)
Christoph Lameter23016962008-04-28 02:12:42 -07001461{
Neeti Desaie6ccd032013-06-14 17:39:33 -07001462#ifdef CONFIG_ENABLE_VMALLOC_SAVING
1463 return __get_vm_area_node(size, 1, flags, PAGE_OFFSET, VMALLOC_END,
Christoph Lameter23016962008-04-28 02:12:42 -07001464 -1, GFP_KERNEL, caller);
Neeti Desaie6ccd032013-06-14 17:39:33 -07001465#else
1466 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
1467 -1, GFP_KERNEL, __builtin_return_address(0));
1468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Marek Szyprowskicb4d7a62012-07-30 09:11:33 +02001471/**
1472 * find_vm_area - find a continuous kernel virtual area
1473 * @addr: base address
1474 *
1475 * Search for the kernel VM area starting at @addr, and return it.
1476 * It is up to the caller to do all required locking to keep the returned
1477 * pointer valid.
1478 */
1479struct vm_struct *find_vm_area(const void *addr)
Nick Piggin83342312006-06-23 02:03:20 -07001480{
Nick Piggindb64fe02008-10-18 20:27:03 -07001481 struct vmap_area *va;
Nick Piggin83342312006-06-23 02:03:20 -07001482
Nick Piggindb64fe02008-10-18 20:27:03 -07001483 va = find_vmap_area((unsigned long)addr);
1484 if (va && va->flags & VM_VM_AREA)
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001485 return va->vm;
Nick Piggin83342312006-06-23 02:03:20 -07001486
Andi Kleen7856dfe2005-05-20 14:27:57 -07001487 return NULL;
Andi Kleen7856dfe2005-05-20 14:27:57 -07001488}
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490/**
Simon Arlott183ff222007-10-20 01:27:18 +02001491 * remove_vm_area - find and remove a continuous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 * @addr: base address
1493 *
1494 * Search for the kernel VM area starting at @addr, and remove it.
1495 * This function returns the found VM area, but using it is NOT safe
Andi Kleen7856dfe2005-05-20 14:27:57 -07001496 * on SMP machines, except for its size or flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001498struct vm_struct *remove_vm_area(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Nick Piggindb64fe02008-10-18 20:27:03 -07001500 struct vmap_area *va;
1501
1502 va = find_vmap_area((unsigned long)addr);
1503 if (va && va->flags & VM_VM_AREA) {
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001504 struct vm_struct *vm = va->vm;
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001505
1506 if (!(vm->flags & VM_UNLIST)) {
1507 struct vm_struct *tmp, **p;
1508 /*
1509 * remove from list and disallow access to
1510 * this vm_struct before unmap. (address range
1511 * confliction is maintained by vmap.)
1512 */
1513 write_lock(&vmlist_lock);
1514 for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
1515 ;
1516 *p = tmp->next;
1517 write_unlock(&vmlist_lock);
1518 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001519
KAMEZAWA Hiroyukidd32c272009-09-21 17:02:32 -07001520 vmap_debug_free_range(va->va_start, va->va_end);
1521 free_unmap_vmap_area(va);
1522 vm->size -= PAGE_SIZE;
1523
Nick Piggindb64fe02008-10-18 20:27:03 -07001524 return vm;
1525 }
1526 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001529static void __vunmap(const void *addr, int deallocate_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 struct vm_struct *area;
1532
1533 if (!addr)
1534 return;
1535
1536 if ((PAGE_SIZE-1) & (unsigned long)addr) {
Arjan van de Ven4c8573e2008-07-25 19:45:37 -07001537 WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return;
1539 }
1540
1541 area = remove_vm_area(addr);
1542 if (unlikely(!area)) {
Arjan van de Ven4c8573e2008-07-25 19:45:37 -07001543 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return;
1546 }
1547
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001548 debug_check_no_locks_freed(addr, area->size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001549 debug_check_no_obj_freed(addr, area->size);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (deallocate_pages) {
1552 int i;
1553
1554 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001555 struct page *page = area->pages[i];
1556
1557 BUG_ON(!page);
1558 __free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560
Jan Kiszka8757d5f2006-07-14 00:23:56 -07001561 if (area->flags & VM_VPAGES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 vfree(area->pages);
1563 else
1564 kfree(area->pages);
1565 }
1566
1567 kfree(area);
1568 return;
1569}
1570
1571/**
1572 * vfree - release memory allocated by vmalloc()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 * @addr: memory base address
1574 *
Simon Arlott183ff222007-10-20 01:27:18 +02001575 * Free the virtually continuous memory area starting at @addr, as
Pekka Enberg80e93ef2005-09-09 13:10:16 -07001576 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1577 * NULL, no operation is performed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07001579 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001581void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 BUG_ON(in_interrupt());
Catalin Marinas89219d32009-06-11 13:23:19 +01001584
1585 kmemleak_free(addr);
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 __vunmap(addr, 1);
1588}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589EXPORT_SYMBOL(vfree);
1590
1591/**
1592 * vunmap - release virtual mapping obtained by vmap()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 * @addr: memory base address
1594 *
1595 * Free the virtually contiguous memory area starting at @addr,
1596 * which was created from the page array passed to vmap().
1597 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07001598 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001600void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 BUG_ON(in_interrupt());
Peter Zijlstra34754b62009-02-25 16:04:03 +01001603 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 __vunmap(addr, 0);
1605}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606EXPORT_SYMBOL(vunmap);
1607
1608/**
1609 * vmap - map an array of pages into virtually contiguous space
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * @pages: array of page pointers
1611 * @count: number of pages to map
1612 * @flags: vm_area->flags
1613 * @prot: page protection for the mapping
1614 *
1615 * Maps @count pages from @pages into contiguous kernel virtual
1616 * space.
1617 */
1618void *vmap(struct page **pages, unsigned int count,
1619 unsigned long flags, pgprot_t prot)
1620{
1621 struct vm_struct *area;
1622
Peter Zijlstra34754b62009-02-25 16:04:03 +01001623 might_sleep();
1624
Jan Beulich44813742009-09-21 17:03:05 -07001625 if (count > totalram_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return NULL;
1627
Christoph Lameter23016962008-04-28 02:12:42 -07001628 area = get_vm_area_caller((count << PAGE_SHIFT), flags,
1629 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (!area)
1631 return NULL;
Christoph Lameter23016962008-04-28 02:12:42 -07001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (map_vm_area(area, prot, &pages)) {
1634 vunmap(area->addr);
1635 return NULL;
1636 }
1637
1638 return area->addr;
1639}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640EXPORT_SYMBOL(vmap);
1641
David Miller2dca6992009-09-21 12:22:34 -07001642static void *__vmalloc_node(unsigned long size, unsigned long align,
1643 gfp_t gfp_mask, pgprot_t prot,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001644 int node, const void *caller);
Adrian Bunke31d9eb2008-02-04 22:29:09 -08001645static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001646 pgprot_t prot, int node, const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Dave Hansen22943ab2011-05-24 17:12:18 -07001648 const int order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 struct page **pages;
1650 unsigned int nr_pages, array_size, i;
Jan Beulich976d6df2009-12-14 17:58:39 -08001651 gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
1654 array_size = (nr_pages * sizeof(struct page *));
1655
1656 area->nr_pages = nr_pages;
1657 /* Please note that the recursion is strictly bounded. */
Jan Kiszka8757d5f2006-07-14 00:23:56 -07001658 if (array_size > PAGE_SIZE) {
Jan Beulich976d6df2009-12-14 17:58:39 -08001659 pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,
Christoph Lameter23016962008-04-28 02:12:42 -07001660 PAGE_KERNEL, node, caller);
Jan Kiszka8757d5f2006-07-14 00:23:56 -07001661 area->flags |= VM_VPAGES;
Andrew Morton286e1ea2006-10-17 00:09:57 -07001662 } else {
Jan Beulich976d6df2009-12-14 17:58:39 -08001663 pages = kmalloc_node(array_size, nested_gfp, node);
Andrew Morton286e1ea2006-10-17 00:09:57 -07001664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 area->pages = pages;
Christoph Lameter23016962008-04-28 02:12:42 -07001666 area->caller = caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (!area->pages) {
1668 remove_vm_area(area->addr);
1669 kfree(area);
1670 return NULL;
1671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001674 struct page *page;
Dave Hansen22943ab2011-05-24 17:12:18 -07001675 gfp_t tmp_mask = gfp_mask | __GFP_NOWARN;
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001676
Christoph Lameter930fc452005-10-29 18:15:41 -07001677 if (node < 0)
Dave Hansen22943ab2011-05-24 17:12:18 -07001678 page = alloc_page(tmp_mask);
Christoph Lameter930fc452005-10-29 18:15:41 -07001679 else
Dave Hansen22943ab2011-05-24 17:12:18 -07001680 page = alloc_pages_node(node, tmp_mask, order);
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001681
1682 if (unlikely(!page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /* Successfully allocated i pages, free them in __vunmap() */
1684 area->nr_pages = i;
1685 goto fail;
1686 }
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001687 area->pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689
1690 if (map_vm_area(area, prot, &pages))
1691 goto fail;
1692 return area->addr;
1693
1694fail:
Joe Perches3ee9a4f2011-10-31 17:08:35 -07001695 warn_alloc_failed(gfp_mask, order,
1696 "vmalloc: allocation failure, allocated %ld of %ld bytes\n",
Dave Hansen22943ab2011-05-24 17:12:18 -07001697 (area->nr_pages*PAGE_SIZE), area->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 vfree(area->addr);
1699 return NULL;
1700}
1701
David Rientjesd0a21262011-01-13 15:46:02 -08001702/**
1703 * __vmalloc_node_range - allocate virtually contiguous memory
1704 * @size: allocation size
1705 * @align: desired alignment
1706 * @start: vm area range start
1707 * @end: vm area range end
1708 * @gfp_mask: flags for the page level allocator
1709 * @prot: protection mask for the allocated pages
1710 * @node: node to use for allocation or -1
1711 * @caller: caller's return address
1712 *
1713 * Allocate enough pages to cover @size from the page level
1714 * allocator with @gfp_mask flags. Map them into contiguous
1715 * kernel virtual space, using a pagetable protection of @prot.
1716 */
1717void *__vmalloc_node_range(unsigned long size, unsigned long align,
1718 unsigned long start, unsigned long end, gfp_t gfp_mask,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001719 pgprot_t prot, int node, const void *caller)
Christoph Lameter930fc452005-10-29 18:15:41 -07001720{
David Rientjesd0a21262011-01-13 15:46:02 -08001721 struct vm_struct *area;
1722 void *addr;
1723 unsigned long real_size = size;
Jack Cheung59f9f1c2011-11-29 16:52:49 -08001724#ifdef CONFIG_FIX_MOVABLE_ZONE
1725 unsigned long total_pages = total_unmovable_pages;
1726#else
1727 unsigned long total_pages = totalram_pages;
1728#endif
David Rientjesd0a21262011-01-13 15:46:02 -08001729
1730 size = PAGE_ALIGN(size);
Jack Cheung59f9f1c2011-11-29 16:52:49 -08001731 if (!size || (size >> PAGE_SHIFT) > total_pages)
Joe Perchesde7d2b52011-10-31 17:08:48 -07001732 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08001733
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001734 area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNLIST,
1735 start, end, node, gfp_mask, caller);
David Rientjesd0a21262011-01-13 15:46:02 -08001736 if (!area)
Joe Perchesde7d2b52011-10-31 17:08:48 -07001737 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08001738
1739 addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
Mel Gorman1368edf2011-12-08 14:34:30 -08001740 if (!addr)
1741 return NULL;
Catalin Marinas89219d32009-06-11 13:23:19 +01001742
1743 /*
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001744 * In this function, newly allocated vm_struct is not added
1745 * to vmlist at __get_vm_area_node(). so, it is added here.
1746 */
1747 insert_vmalloc_vmlist(area);
1748
1749 /*
Catalin Marinas89219d32009-06-11 13:23:19 +01001750 * A ref_count = 3 is needed because the vm_struct and vmap_area
1751 * structures allocated in the __get_vm_area_node() function contain
1752 * references to the virtual address of the vmalloc'ed block.
1753 */
David Rientjesd0a21262011-01-13 15:46:02 -08001754 kmemleak_alloc(addr, real_size, 3, gfp_mask);
Catalin Marinas89219d32009-06-11 13:23:19 +01001755
1756 return addr;
Joe Perchesde7d2b52011-10-31 17:08:48 -07001757
1758fail:
1759 warn_alloc_failed(gfp_mask, 0,
1760 "vmalloc: allocation failure: %lu bytes\n",
1761 real_size);
1762 return NULL;
Christoph Lameter930fc452005-10-29 18:15:41 -07001763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765/**
Christoph Lameter930fc452005-10-29 18:15:41 -07001766 * __vmalloc_node - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 * @size: allocation size
David Miller2dca6992009-09-21 12:22:34 -07001768 * @align: desired alignment
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 * @gfp_mask: flags for the page level allocator
1770 * @prot: protection mask for the allocated pages
Randy Dunlapd44e0782005-11-07 01:01:10 -08001771 * @node: node to use for allocation or -1
Randy Dunlapc85d1942008-05-01 04:34:48 -07001772 * @caller: caller's return address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 *
1774 * Allocate enough pages to cover @size from the page level
1775 * allocator with @gfp_mask flags. Map them into contiguous
1776 * kernel virtual space, using a pagetable protection of @prot.
1777 */
David Miller2dca6992009-09-21 12:22:34 -07001778static void *__vmalloc_node(unsigned long size, unsigned long align,
1779 gfp_t gfp_mask, pgprot_t prot,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001780 int node, const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
David Rientjesd0a21262011-01-13 15:46:02 -08001782 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
1783 gfp_mask, prot, node, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Christoph Lameter930fc452005-10-29 18:15:41 -07001786void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1787{
David Miller2dca6992009-09-21 12:22:34 -07001788 return __vmalloc_node(size, 1, gfp_mask, prot, -1,
Christoph Lameter23016962008-04-28 02:12:42 -07001789 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001790}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791EXPORT_SYMBOL(__vmalloc);
1792
Dave Younge1ca7782010-10-26 14:22:06 -07001793static inline void *__vmalloc_node_flags(unsigned long size,
1794 int node, gfp_t flags)
1795{
1796 return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
1797 node, __builtin_return_address(0));
1798}
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800/**
1801 * vmalloc - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 * Allocate enough pages to cover @size from the page level
1804 * allocator and map them into contiguous kernel virtual space.
1805 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02001806 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 * use __vmalloc() instead.
1808 */
1809void *vmalloc(unsigned long size)
1810{
Dave Younge1ca7782010-10-26 14:22:06 -07001811 return __vmalloc_node_flags(size, -1, GFP_KERNEL | __GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813EXPORT_SYMBOL(vmalloc);
1814
Christoph Lameter930fc452005-10-29 18:15:41 -07001815/**
Dave Younge1ca7782010-10-26 14:22:06 -07001816 * vzalloc - allocate virtually contiguous memory with zero fill
1817 * @size: allocation size
1818 * Allocate enough pages to cover @size from the page level
1819 * allocator and map them into contiguous kernel virtual space.
1820 * The memory allocated is set to zero.
1821 *
1822 * For tight control over page level allocator and protection flags
1823 * use __vmalloc() instead.
1824 */
1825void *vzalloc(unsigned long size)
1826{
1827 return __vmalloc_node_flags(size, -1,
1828 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1829}
1830EXPORT_SYMBOL(vzalloc);
1831
1832/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07001833 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1834 * @size: allocation size
Nick Piggin83342312006-06-23 02:03:20 -07001835 *
Rolf Eike Beeread04082006-09-27 01:50:13 -07001836 * The resulting memory area is zeroed so it can be mapped to userspace
1837 * without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -07001838 */
1839void *vmalloc_user(unsigned long size)
1840{
1841 struct vm_struct *area;
1842 void *ret;
1843
David Miller2dca6992009-09-21 12:22:34 -07001844 ret = __vmalloc_node(size, SHMLBA,
1845 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
Glauber Costa84877842009-01-06 14:39:19 -08001846 PAGE_KERNEL, -1, __builtin_return_address(0));
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001847 if (ret) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001848 area = find_vm_area(ret);
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001849 area->flags |= VM_USERMAP;
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001850 }
Nick Piggin83342312006-06-23 02:03:20 -07001851 return ret;
1852}
1853EXPORT_SYMBOL(vmalloc_user);
1854
1855/**
Christoph Lameter930fc452005-10-29 18:15:41 -07001856 * vmalloc_node - allocate memory on a specific node
Christoph Lameter930fc452005-10-29 18:15:41 -07001857 * @size: allocation size
Randy Dunlapd44e0782005-11-07 01:01:10 -08001858 * @node: numa node
Christoph Lameter930fc452005-10-29 18:15:41 -07001859 *
1860 * Allocate enough pages to cover @size from the page level
1861 * allocator and map them into contiguous kernel virtual space.
1862 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02001863 * For tight control over page level allocator and protection flags
Christoph Lameter930fc452005-10-29 18:15:41 -07001864 * use __vmalloc() instead.
1865 */
1866void *vmalloc_node(unsigned long size, int node)
1867{
David Miller2dca6992009-09-21 12:22:34 -07001868 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
Christoph Lameter23016962008-04-28 02:12:42 -07001869 node, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001870}
1871EXPORT_SYMBOL(vmalloc_node);
1872
Dave Younge1ca7782010-10-26 14:22:06 -07001873/**
1874 * vzalloc_node - allocate memory on a specific node with zero fill
1875 * @size: allocation size
1876 * @node: numa node
1877 *
1878 * Allocate enough pages to cover @size from the page level
1879 * allocator and map them into contiguous kernel virtual space.
1880 * The memory allocated is set to zero.
1881 *
1882 * For tight control over page level allocator and protection flags
1883 * use __vmalloc_node() instead.
1884 */
1885void *vzalloc_node(unsigned long size, int node)
1886{
1887 return __vmalloc_node_flags(size, node,
1888 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1889}
1890EXPORT_SYMBOL(vzalloc_node);
1891
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001892#ifndef PAGE_KERNEL_EXEC
1893# define PAGE_KERNEL_EXEC PAGE_KERNEL
1894#endif
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896/**
1897 * vmalloc_exec - allocate virtually contiguous, executable memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 * @size: allocation size
1899 *
1900 * Kernel-internal function to allocate enough pages to cover @size
1901 * the page level allocator and map them into contiguous and
1902 * executable kernel virtual space.
1903 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02001904 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 * use __vmalloc() instead.
1906 */
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908void *vmalloc_exec(unsigned long size)
1909{
David Miller2dca6992009-09-21 12:22:34 -07001910 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
Glauber Costa84877842009-01-06 14:39:19 -08001911 -1, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Andi Kleen0d08e0d2007-05-02 19:27:12 +02001914#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
Benjamin Herrenschmidt7ac674f2007-07-19 01:49:10 -07001915#define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02001916#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
Benjamin Herrenschmidt7ac674f2007-07-19 01:49:10 -07001917#define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02001918#else
1919#define GFP_VMALLOC32 GFP_KERNEL
1920#endif
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922/**
1923 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 * @size: allocation size
1925 *
1926 * Allocate enough 32bit PA addressable pages to cover @size from the
1927 * page level allocator and map them into contiguous kernel virtual space.
1928 */
1929void *vmalloc_32(unsigned long size)
1930{
David Miller2dca6992009-09-21 12:22:34 -07001931 return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
Glauber Costa84877842009-01-06 14:39:19 -08001932 -1, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934EXPORT_SYMBOL(vmalloc_32);
1935
Nick Piggin83342312006-06-23 02:03:20 -07001936/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07001937 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
Nick Piggin83342312006-06-23 02:03:20 -07001938 * @size: allocation size
Rolf Eike Beeread04082006-09-27 01:50:13 -07001939 *
1940 * The resulting memory area is 32bit addressable and zeroed so it can be
1941 * mapped to userspace without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -07001942 */
1943void *vmalloc_32_user(unsigned long size)
1944{
1945 struct vm_struct *area;
1946 void *ret;
1947
David Miller2dca6992009-09-21 12:22:34 -07001948 ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
Glauber Costa84877842009-01-06 14:39:19 -08001949 -1, __builtin_return_address(0));
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001950 if (ret) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001951 area = find_vm_area(ret);
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001952 area->flags |= VM_USERMAP;
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001953 }
Nick Piggin83342312006-06-23 02:03:20 -07001954 return ret;
1955}
1956EXPORT_SYMBOL(vmalloc_32_user);
1957
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07001958/*
1959 * small helper routine , copy contents to buf from addr.
1960 * If the page is not present, fill zero.
1961 */
1962
1963static int aligned_vread(char *buf, char *addr, unsigned long count)
1964{
1965 struct page *p;
1966 int copied = 0;
1967
1968 while (count) {
1969 unsigned long offset, length;
1970
1971 offset = (unsigned long)addr & ~PAGE_MASK;
1972 length = PAGE_SIZE - offset;
1973 if (length > count)
1974 length = count;
1975 p = vmalloc_to_page(addr);
1976 /*
1977 * To do safe access to this _mapped_ area, we need
1978 * lock. But adding lock here means that we need to add
1979 * overhead of vmalloc()/vfree() calles for this _debug_
1980 * interface, rarely used. Instead of that, we'll use
1981 * kmap() and get small overhead in this access function.
1982 */
1983 if (p) {
1984 /*
1985 * we can expect USER0 is not used (see vread/vwrite's
1986 * function description)
1987 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08001988 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07001989 memcpy(buf, map + offset, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08001990 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07001991 } else
1992 memset(buf, 0, length);
1993
1994 addr += length;
1995 buf += length;
1996 copied += length;
1997 count -= length;
1998 }
1999 return copied;
2000}
2001
2002static int aligned_vwrite(char *buf, char *addr, unsigned long count)
2003{
2004 struct page *p;
2005 int copied = 0;
2006
2007 while (count) {
2008 unsigned long offset, length;
2009
2010 offset = (unsigned long)addr & ~PAGE_MASK;
2011 length = PAGE_SIZE - offset;
2012 if (length > count)
2013 length = count;
2014 p = vmalloc_to_page(addr);
2015 /*
2016 * To do safe access to this _mapped_ area, we need
2017 * lock. But adding lock here means that we need to add
2018 * overhead of vmalloc()/vfree() calles for this _debug_
2019 * interface, rarely used. Instead of that, we'll use
2020 * kmap() and get small overhead in this access function.
2021 */
2022 if (p) {
2023 /*
2024 * we can expect USER0 is not used (see vread/vwrite's
2025 * function description)
2026 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002027 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002028 memcpy(map + offset, buf, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002029 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002030 }
2031 addr += length;
2032 buf += length;
2033 copied += length;
2034 count -= length;
2035 }
2036 return copied;
2037}
2038
2039/**
2040 * vread() - read vmalloc area in a safe way.
2041 * @buf: buffer for reading data
2042 * @addr: vm address.
2043 * @count: number of bytes to be read.
2044 *
2045 * Returns # of bytes which addr and buf should be increased.
2046 * (same number to @count). Returns 0 if [addr...addr+count) doesn't
2047 * includes any intersect with alive vmalloc area.
2048 *
2049 * This function checks that addr is a valid vmalloc'ed area, and
2050 * copy data from that area to a given buffer. If the given memory range
2051 * of [addr...addr+count) includes some valid address, data is copied to
2052 * proper area of @buf. If there are memory holes, they'll be zero-filled.
2053 * IOREMAP area is treated as memory hole and no copy is done.
2054 *
2055 * If [addr...addr+count) doesn't includes any intersects with alive
2056 * vm_struct area, returns 0.
2057 * @buf should be kernel's buffer. Because this function uses KM_USER0,
2058 * the caller should guarantee KM_USER0 is not used.
2059 *
2060 * Note: In usual ops, vread() is never necessary because the caller
2061 * should know vmalloc() area is valid and can use memcpy().
2062 * This is for routines which have to access vmalloc area without
2063 * any informaion, as /dev/kmem.
2064 *
2065 */
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067long vread(char *buf, char *addr, unsigned long count)
2068{
2069 struct vm_struct *tmp;
2070 char *vaddr, *buf_start = buf;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002071 unsigned long buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 unsigned long n;
2073
2074 /* Don't allow overflow */
2075 if ((unsigned long) addr + count < count)
2076 count = -(unsigned long) addr;
2077
2078 read_lock(&vmlist_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002079 for (tmp = vmlist; count && tmp; tmp = tmp->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 vaddr = (char *) tmp->addr;
2081 if (addr >= vaddr + tmp->size - PAGE_SIZE)
2082 continue;
2083 while (addr < vaddr) {
2084 if (count == 0)
2085 goto finished;
2086 *buf = '\0';
2087 buf++;
2088 addr++;
2089 count--;
2090 }
2091 n = vaddr + tmp->size - PAGE_SIZE - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002092 if (n > count)
2093 n = count;
2094 if (!(tmp->flags & VM_IOREMAP))
2095 aligned_vread(buf, addr, n);
2096 else /* IOREMAP area is treated as memory hole */
2097 memset(buf, 0, n);
2098 buf += n;
2099 addr += n;
2100 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
2102finished:
2103 read_unlock(&vmlist_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002104
2105 if (buf == buf_start)
2106 return 0;
2107 /* zero-fill memory holes */
2108 if (buf != buf_start + buflen)
2109 memset(buf, 0, buflen - (buf - buf_start));
2110
2111 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002114/**
2115 * vwrite() - write vmalloc area in a safe way.
2116 * @buf: buffer for source data
2117 * @addr: vm address.
2118 * @count: number of bytes to be read.
2119 *
2120 * Returns # of bytes which addr and buf should be incresed.
2121 * (same number to @count).
2122 * If [addr...addr+count) doesn't includes any intersect with valid
2123 * vmalloc area, returns 0.
2124 *
2125 * This function checks that addr is a valid vmalloc'ed area, and
2126 * copy data from a buffer to the given addr. If specified range of
2127 * [addr...addr+count) includes some valid address, data is copied from
2128 * proper area of @buf. If there are memory holes, no copy to hole.
2129 * IOREMAP area is treated as memory hole and no copy is done.
2130 *
2131 * If [addr...addr+count) doesn't includes any intersects with alive
2132 * vm_struct area, returns 0.
2133 * @buf should be kernel's buffer. Because this function uses KM_USER0,
2134 * the caller should guarantee KM_USER0 is not used.
2135 *
2136 * Note: In usual ops, vwrite() is never necessary because the caller
2137 * should know vmalloc() area is valid and can use memcpy().
2138 * This is for routines which have to access vmalloc area without
2139 * any informaion, as /dev/kmem.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002140 */
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142long vwrite(char *buf, char *addr, unsigned long count)
2143{
2144 struct vm_struct *tmp;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002145 char *vaddr;
2146 unsigned long n, buflen;
2147 int copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 /* Don't allow overflow */
2150 if ((unsigned long) addr + count < count)
2151 count = -(unsigned long) addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002152 buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 read_lock(&vmlist_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002155 for (tmp = vmlist; count && tmp; tmp = tmp->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 vaddr = (char *) tmp->addr;
2157 if (addr >= vaddr + tmp->size - PAGE_SIZE)
2158 continue;
2159 while (addr < vaddr) {
2160 if (count == 0)
2161 goto finished;
2162 buf++;
2163 addr++;
2164 count--;
2165 }
2166 n = vaddr + tmp->size - PAGE_SIZE - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002167 if (n > count)
2168 n = count;
2169 if (!(tmp->flags & VM_IOREMAP)) {
2170 aligned_vwrite(buf, addr, n);
2171 copied++;
2172 }
2173 buf += n;
2174 addr += n;
2175 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177finished:
2178 read_unlock(&vmlist_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002179 if (!copied)
2180 return 0;
2181 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
Nick Piggin83342312006-06-23 02:03:20 -07002183
2184/**
2185 * remap_vmalloc_range - map vmalloc pages to userspace
Nick Piggin83342312006-06-23 02:03:20 -07002186 * @vma: vma to cover (map full range of vma)
2187 * @addr: vmalloc memory
2188 * @pgoff: number of pages into addr before first page to map
Randy Dunlap76824862008-03-19 17:00:40 -07002189 *
2190 * Returns: 0 for success, -Exxx on failure
Nick Piggin83342312006-06-23 02:03:20 -07002191 *
2192 * This function checks that addr is a valid vmalloc'ed area, and
2193 * that it is big enough to cover the vma. Will return failure if
2194 * that criteria isn't met.
2195 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002196 * Similar to remap_pfn_range() (see mm/memory.c)
Nick Piggin83342312006-06-23 02:03:20 -07002197 */
2198int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
2199 unsigned long pgoff)
2200{
2201 struct vm_struct *area;
2202 unsigned long uaddr = vma->vm_start;
2203 unsigned long usize = vma->vm_end - vma->vm_start;
Nick Piggin83342312006-06-23 02:03:20 -07002204
2205 if ((PAGE_SIZE-1) & (unsigned long)addr)
2206 return -EINVAL;
2207
Nick Piggindb64fe02008-10-18 20:27:03 -07002208 area = find_vm_area(addr);
Nick Piggin83342312006-06-23 02:03:20 -07002209 if (!area)
Nick Piggindb64fe02008-10-18 20:27:03 -07002210 return -EINVAL;
Nick Piggin83342312006-06-23 02:03:20 -07002211
2212 if (!(area->flags & VM_USERMAP))
Nick Piggindb64fe02008-10-18 20:27:03 -07002213 return -EINVAL;
Nick Piggin83342312006-06-23 02:03:20 -07002214
2215 if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
Nick Piggindb64fe02008-10-18 20:27:03 -07002216 return -EINVAL;
Nick Piggin83342312006-06-23 02:03:20 -07002217
2218 addr += pgoff << PAGE_SHIFT;
2219 do {
2220 struct page *page = vmalloc_to_page(addr);
Nick Piggindb64fe02008-10-18 20:27:03 -07002221 int ret;
2222
Nick Piggin83342312006-06-23 02:03:20 -07002223 ret = vm_insert_page(vma, uaddr, page);
2224 if (ret)
2225 return ret;
2226
2227 uaddr += PAGE_SIZE;
2228 addr += PAGE_SIZE;
2229 usize -= PAGE_SIZE;
2230 } while (usize > 0);
2231
2232 /* Prevent "things" like memory migration? VM_flags need a cleanup... */
2233 vma->vm_flags |= VM_RESERVED;
2234
Nick Piggindb64fe02008-10-18 20:27:03 -07002235 return 0;
Nick Piggin83342312006-06-23 02:03:20 -07002236}
2237EXPORT_SYMBOL(remap_vmalloc_range);
2238
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07002239/*
2240 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
2241 * have one.
2242 */
2243void __attribute__((weak)) vmalloc_sync_all(void)
2244{
2245}
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002246
2247
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002248static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002249{
David Vrabelcd129092011-09-29 16:53:32 +01002250 pte_t ***p = data;
2251
2252 if (p) {
2253 *(*p) = pte;
2254 (*p)++;
2255 }
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002256 return 0;
2257}
2258
2259/**
2260 * alloc_vm_area - allocate a range of kernel address space
2261 * @size: size of the area
David Vrabelcd129092011-09-29 16:53:32 +01002262 * @ptes: returns the PTEs for the address space
Randy Dunlap76824862008-03-19 17:00:40 -07002263 *
2264 * Returns: NULL on failure, vm_struct on success
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002265 *
2266 * This function reserves a range of kernel address space, and
2267 * allocates pagetables to map that range. No actual mappings
David Vrabelcd129092011-09-29 16:53:32 +01002268 * are created.
2269 *
2270 * If @ptes is non-NULL, pointers to the PTEs (in init_mm)
2271 * allocated for the VM area are returned.
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002272 */
David Vrabelcd129092011-09-29 16:53:32 +01002273struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002274{
2275 struct vm_struct *area;
2276
Christoph Lameter23016962008-04-28 02:12:42 -07002277 area = get_vm_area_caller(size, VM_IOREMAP,
2278 __builtin_return_address(0));
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002279 if (area == NULL)
2280 return NULL;
2281
2282 /*
2283 * This ensures that page tables are constructed for this region
2284 * of kernel virtual address space and mapped into init_mm.
2285 */
2286 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
David Vrabelcd129092011-09-29 16:53:32 +01002287 size, f, ptes ? &ptes : NULL)) {
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002288 free_vm_area(area);
2289 return NULL;
2290 }
2291
David Vrabeld63c8a02011-09-14 16:22:02 -07002292 /*
2293 * If the allocated address space is passed to a hypercall
2294 * before being used then we cannot rely on a page fault to
2295 * trigger an update of the page tables. So sync all the page
2296 * tables here.
2297 */
2298 vmalloc_sync_all();
2299
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002300 return area;
2301}
2302EXPORT_SYMBOL_GPL(alloc_vm_area);
2303
2304void free_vm_area(struct vm_struct *area)
2305{
2306 struct vm_struct *ret;
2307 ret = remove_vm_area(area->addr);
2308 BUG_ON(ret != area);
2309 kfree(area);
2310}
2311EXPORT_SYMBOL_GPL(free_vm_area);
Christoph Lametera10aa572008-04-28 02:12:40 -07002312
Tejun Heo4f8b02b2010-09-03 18:22:47 +02002313#ifdef CONFIG_SMP
Tejun Heoca23e402009-08-14 15:00:52 +09002314static struct vmap_area *node_to_va(struct rb_node *n)
2315{
2316 return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
2317}
2318
2319/**
2320 * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
2321 * @end: target address
2322 * @pnext: out arg for the next vmap_area
2323 * @pprev: out arg for the previous vmap_area
2324 *
2325 * Returns: %true if either or both of next and prev are found,
2326 * %false if no vmap_area exists
2327 *
2328 * Find vmap_areas end addresses of which enclose @end. ie. if not
2329 * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
2330 */
2331static bool pvm_find_next_prev(unsigned long end,
2332 struct vmap_area **pnext,
2333 struct vmap_area **pprev)
2334{
2335 struct rb_node *n = vmap_area_root.rb_node;
2336 struct vmap_area *va = NULL;
2337
2338 while (n) {
2339 va = rb_entry(n, struct vmap_area, rb_node);
2340 if (end < va->va_end)
2341 n = n->rb_left;
2342 else if (end > va->va_end)
2343 n = n->rb_right;
2344 else
2345 break;
2346 }
2347
2348 if (!va)
2349 return false;
2350
2351 if (va->va_end > end) {
2352 *pnext = va;
2353 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2354 } else {
2355 *pprev = va;
2356 *pnext = node_to_va(rb_next(&(*pprev)->rb_node));
2357 }
2358 return true;
2359}
2360
2361/**
2362 * pvm_determine_end - find the highest aligned address between two vmap_areas
2363 * @pnext: in/out arg for the next vmap_area
2364 * @pprev: in/out arg for the previous vmap_area
2365 * @align: alignment
2366 *
2367 * Returns: determined end address
2368 *
2369 * Find the highest aligned address between *@pnext and *@pprev below
2370 * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned
2371 * down address is between the end addresses of the two vmap_areas.
2372 *
2373 * Please note that the address returned by this function may fall
2374 * inside *@pnext vmap_area. The caller is responsible for checking
2375 * that.
2376 */
2377static unsigned long pvm_determine_end(struct vmap_area **pnext,
2378 struct vmap_area **pprev,
2379 unsigned long align)
2380{
2381 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2382 unsigned long addr;
2383
2384 if (*pnext)
2385 addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
2386 else
2387 addr = vmalloc_end;
2388
2389 while (*pprev && (*pprev)->va_end > addr) {
2390 *pnext = *pprev;
2391 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2392 }
2393
2394 return addr;
2395}
2396
2397/**
2398 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
2399 * @offsets: array containing offset of each area
2400 * @sizes: array containing size of each area
2401 * @nr_vms: the number of areas to allocate
2402 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
Tejun Heoca23e402009-08-14 15:00:52 +09002403 *
2404 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
2405 * vm_structs on success, %NULL on failure
2406 *
2407 * Percpu allocator wants to use congruent vm areas so that it can
2408 * maintain the offsets among percpu areas. This function allocates
David Rientjesec3f64f2011-01-13 15:46:01 -08002409 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
2410 * be scattered pretty far, distance between two areas easily going up
2411 * to gigabytes. To avoid interacting with regular vmallocs, these
2412 * areas are allocated from top.
Tejun Heoca23e402009-08-14 15:00:52 +09002413 *
2414 * Despite its complicated look, this allocator is rather simple. It
2415 * does everything top-down and scans areas from the end looking for
2416 * matching slot. While scanning, if any of the areas overlaps with
2417 * existing vmap_area, the base address is pulled down to fit the
2418 * area. Scanning is repeated till all the areas fit and then all
2419 * necessary data structres are inserted and the result is returned.
2420 */
2421struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
2422 const size_t *sizes, int nr_vms,
David Rientjesec3f64f2011-01-13 15:46:01 -08002423 size_t align)
Tejun Heoca23e402009-08-14 15:00:52 +09002424{
2425 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
2426 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2427 struct vmap_area **vas, *prev, *next;
2428 struct vm_struct **vms;
2429 int area, area2, last_area, term_area;
2430 unsigned long base, start, end, last_end;
2431 bool purged = false;
2432
Tejun Heoca23e402009-08-14 15:00:52 +09002433 /* verify parameters and allocate data structures */
2434 BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align));
2435 for (last_area = 0, area = 0; area < nr_vms; area++) {
2436 start = offsets[area];
2437 end = start + sizes[area];
2438
2439 /* is everything aligned properly? */
2440 BUG_ON(!IS_ALIGNED(offsets[area], align));
2441 BUG_ON(!IS_ALIGNED(sizes[area], align));
2442
2443 /* detect the area with the highest address */
2444 if (start > offsets[last_area])
2445 last_area = area;
2446
2447 for (area2 = 0; area2 < nr_vms; area2++) {
2448 unsigned long start2 = offsets[area2];
2449 unsigned long end2 = start2 + sizes[area2];
2450
2451 if (area2 == area)
2452 continue;
2453
2454 BUG_ON(start2 >= start && start2 < end);
2455 BUG_ON(end2 <= end && end2 > start);
2456 }
2457 }
2458 last_end = offsets[last_area] + sizes[last_area];
2459
2460 if (vmalloc_end - vmalloc_start < last_end) {
2461 WARN_ON(true);
2462 return NULL;
2463 }
2464
David Rientjesec3f64f2011-01-13 15:46:01 -08002465 vms = kzalloc(sizeof(vms[0]) * nr_vms, GFP_KERNEL);
2466 vas = kzalloc(sizeof(vas[0]) * nr_vms, GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09002467 if (!vas || !vms)
Kautuk Consulf1db7af2012-01-12 17:20:08 -08002468 goto err_free2;
Tejun Heoca23e402009-08-14 15:00:52 +09002469
2470 for (area = 0; area < nr_vms; area++) {
David Rientjesec3f64f2011-01-13 15:46:01 -08002471 vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL);
2472 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09002473 if (!vas[area] || !vms[area])
2474 goto err_free;
2475 }
2476retry:
2477 spin_lock(&vmap_area_lock);
2478
2479 /* start scanning - we scan from the top, begin with the last area */
2480 area = term_area = last_area;
2481 start = offsets[area];
2482 end = start + sizes[area];
2483
2484 if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2485 base = vmalloc_end - last_end;
2486 goto found;
2487 }
2488 base = pvm_determine_end(&next, &prev, align) - end;
2489
2490 while (true) {
2491 BUG_ON(next && next->va_end <= base + end);
2492 BUG_ON(prev && prev->va_end > base + end);
2493
2494 /*
2495 * base might have underflowed, add last_end before
2496 * comparing.
2497 */
2498 if (base + last_end < vmalloc_start + last_end) {
2499 spin_unlock(&vmap_area_lock);
2500 if (!purged) {
2501 purge_vmap_area_lazy();
2502 purged = true;
2503 goto retry;
2504 }
2505 goto err_free;
2506 }
2507
2508 /*
2509 * If next overlaps, move base downwards so that it's
2510 * right below next and then recheck.
2511 */
2512 if (next && next->va_start < base + end) {
2513 base = pvm_determine_end(&next, &prev, align) - end;
2514 term_area = area;
2515 continue;
2516 }
2517
2518 /*
2519 * If prev overlaps, shift down next and prev and move
2520 * base so that it's right below new next and then
2521 * recheck.
2522 */
2523 if (prev && prev->va_end > base + start) {
2524 next = prev;
2525 prev = node_to_va(rb_prev(&next->rb_node));
2526 base = pvm_determine_end(&next, &prev, align) - end;
2527 term_area = area;
2528 continue;
2529 }
2530
2531 /*
2532 * This area fits, move on to the previous one. If
2533 * the previous one is the terminal one, we're done.
2534 */
2535 area = (area + nr_vms - 1) % nr_vms;
2536 if (area == term_area)
2537 break;
2538 start = offsets[area];
2539 end = start + sizes[area];
2540 pvm_find_next_prev(base + end, &next, &prev);
2541 }
2542found:
2543 /* we've found a fitting base, insert all va's */
2544 for (area = 0; area < nr_vms; area++) {
2545 struct vmap_area *va = vas[area];
2546
2547 va->va_start = base + offsets[area];
2548 va->va_end = va->va_start + sizes[area];
2549 __insert_vmap_area(va);
2550 }
2551
2552 vmap_area_pcpu_hole = base + offsets[last_area];
2553
2554 spin_unlock(&vmap_area_lock);
2555
2556 /* insert all vm's */
2557 for (area = 0; area < nr_vms; area++)
2558 insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2559 pcpu_get_vm_areas);
2560
2561 kfree(vas);
2562 return vms;
2563
2564err_free:
2565 for (area = 0; area < nr_vms; area++) {
Kautuk Consulf1db7af2012-01-12 17:20:08 -08002566 kfree(vas[area]);
2567 kfree(vms[area]);
Tejun Heoca23e402009-08-14 15:00:52 +09002568 }
Kautuk Consulf1db7af2012-01-12 17:20:08 -08002569err_free2:
Tejun Heoca23e402009-08-14 15:00:52 +09002570 kfree(vas);
2571 kfree(vms);
2572 return NULL;
2573}
2574
2575/**
2576 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2577 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2578 * @nr_vms: the number of allocated areas
2579 *
2580 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2581 */
2582void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2583{
2584 int i;
2585
2586 for (i = 0; i < nr_vms; i++)
2587 free_vm_area(vms[i]);
2588 kfree(vms);
2589}
Tejun Heo4f8b02b2010-09-03 18:22:47 +02002590#endif /* CONFIG_SMP */
Christoph Lametera10aa572008-04-28 02:12:40 -07002591
2592#ifdef CONFIG_PROC_FS
2593static void *s_start(struct seq_file *m, loff_t *pos)
Namhyung Kime199b5d2010-10-26 14:22:03 -07002594 __acquires(&vmlist_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07002595{
2596 loff_t n = *pos;
2597 struct vm_struct *v;
2598
2599 read_lock(&vmlist_lock);
2600 v = vmlist;
2601 while (n > 0 && v) {
2602 n--;
2603 v = v->next;
2604 }
2605 if (!n)
2606 return v;
2607
2608 return NULL;
2609
2610}
2611
2612static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2613{
2614 struct vm_struct *v = p;
2615
2616 ++*pos;
2617 return v->next;
2618}
2619
2620static void s_stop(struct seq_file *m, void *p)
Namhyung Kime199b5d2010-10-26 14:22:03 -07002621 __releases(&vmlist_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07002622{
2623 read_unlock(&vmlist_lock);
2624}
2625
Eric Dumazeta47a1262008-07-23 21:27:38 -07002626static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2627{
2628 if (NUMA_BUILD) {
2629 unsigned int nr, *counters = m->private;
2630
2631 if (!counters)
2632 return;
2633
2634 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2635
2636 for (nr = 0; nr < v->nr_pages; nr++)
2637 counters[page_to_nid(v->pages[nr])]++;
2638
2639 for_each_node_state(nr, N_HIGH_MEMORY)
2640 if (counters[nr])
2641 seq_printf(m, " N%u=%u", nr, counters[nr]);
2642 }
2643}
2644
Christoph Lametera10aa572008-04-28 02:12:40 -07002645static int s_show(struct seq_file *m, void *p)
2646{
2647 struct vm_struct *v = p;
2648
2649 seq_printf(m, "0x%p-0x%p %7ld",
2650 v->addr, v->addr + v->size, v->size);
2651
Joe Perches62c70bc2011-01-13 15:45:52 -08002652 if (v->caller)
2653 seq_printf(m, " %pS", v->caller);
Christoph Lameter23016962008-04-28 02:12:42 -07002654
Christoph Lametera10aa572008-04-28 02:12:40 -07002655 if (v->nr_pages)
2656 seq_printf(m, " pages=%d", v->nr_pages);
2657
2658 if (v->phys_addr)
Kenji Kaneshigeffa71f32010-06-18 12:22:40 +09002659 seq_printf(m, " phys=%llx", (unsigned long long)v->phys_addr);
Christoph Lametera10aa572008-04-28 02:12:40 -07002660
2661 if (v->flags & VM_IOREMAP)
2662 seq_printf(m, " ioremap");
2663
2664 if (v->flags & VM_ALLOC)
2665 seq_printf(m, " vmalloc");
2666
2667 if (v->flags & VM_MAP)
2668 seq_printf(m, " vmap");
2669
2670 if (v->flags & VM_USERMAP)
2671 seq_printf(m, " user");
2672
2673 if (v->flags & VM_VPAGES)
2674 seq_printf(m, " vpages");
2675
Laura Abbottd3263482013-08-22 13:46:07 -07002676 if (v->flags & VM_LOWMEM)
2677 seq_printf(m, " lowmem");
2678
Eric Dumazeta47a1262008-07-23 21:27:38 -07002679 show_numa_info(m, v);
Christoph Lametera10aa572008-04-28 02:12:40 -07002680 seq_putc(m, '\n');
2681 return 0;
2682}
2683
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002684static const struct seq_operations vmalloc_op = {
Christoph Lametera10aa572008-04-28 02:12:40 -07002685 .start = s_start,
2686 .next = s_next,
2687 .stop = s_stop,
2688 .show = s_show,
2689};
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002690
2691static int vmalloc_open(struct inode *inode, struct file *file)
2692{
2693 unsigned int *ptr = NULL;
2694 int ret;
2695
Kulikov Vasiliy51980ac2010-08-09 17:19:58 -07002696 if (NUMA_BUILD) {
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002697 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
Kulikov Vasiliy51980ac2010-08-09 17:19:58 -07002698 if (ptr == NULL)
2699 return -ENOMEM;
2700 }
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002701 ret = seq_open(file, &vmalloc_op);
2702 if (!ret) {
2703 struct seq_file *m = file->private_data;
2704 m->private = ptr;
2705 } else
2706 kfree(ptr);
2707 return ret;
2708}
2709
2710static const struct file_operations proc_vmalloc_operations = {
2711 .open = vmalloc_open,
2712 .read = seq_read,
2713 .llseek = seq_lseek,
2714 .release = seq_release_private,
2715};
2716
2717static int __init proc_vmalloc_init(void)
2718{
2719 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
2720 return 0;
2721}
2722module_init(proc_vmalloc_init);
Christoph Lametera10aa572008-04-28 02:12:40 -07002723#endif
2724