vmalloc.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  1. /*
  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
  8. * Numa awareness, Christoph Lameter, SGI, June 2005
  9. */
  10. #include <linux/vmalloc.h>
  11. #include <linux/mm.h>
  12. #include <linux/module.h>
  13. #include <linux/highmem.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/debugobjects.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/list.h>
  22. #include <linux/rbtree.h>
  23. #include <linux/radix-tree.h>
  24. #include <linux/rcupdate.h>
  25. #include <linux/bootmem.h>
  26. #include <linux/pfn.h>
  27. #include <asm/atomic.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/tlbflush.h>
  30. /*** Page table manipulation functions ***/
  31. static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
  32. {
  33. pte_t *pte;
  34. pte = pte_offset_kernel(pmd, addr);
  35. do {
  36. pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
  37. WARN_ON(!pte_none(ptent) && !pte_present(ptent));
  38. } while (pte++, addr += PAGE_SIZE, addr != end);
  39. }
  40. static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
  41. {
  42. pmd_t *pmd;
  43. unsigned long next;
  44. pmd = pmd_offset(pud, addr);
  45. do {
  46. next = pmd_addr_end(addr, end);
  47. if (pmd_none_or_clear_bad(pmd))
  48. continue;
  49. vunmap_pte_range(pmd, addr, next);
  50. } while (pmd++, addr = next, addr != end);
  51. }
  52. static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
  53. {
  54. pud_t *pud;
  55. unsigned long next;
  56. pud = pud_offset(pgd, addr);
  57. do {
  58. next = pud_addr_end(addr, end);
  59. if (pud_none_or_clear_bad(pud))
  60. continue;
  61. vunmap_pmd_range(pud, addr, next);
  62. } while (pud++, addr = next, addr != end);
  63. }
  64. static void vunmap_page_range(unsigned long addr, unsigned long end)
  65. {
  66. pgd_t *pgd;
  67. unsigned long next;
  68. BUG_ON(addr >= end);
  69. pgd = pgd_offset_k(addr);
  70. do {
  71. next = pgd_addr_end(addr, end);
  72. if (pgd_none_or_clear_bad(pgd))
  73. continue;
  74. vunmap_pud_range(pgd, addr, next);
  75. } while (pgd++, addr = next, addr != end);
  76. }
  77. static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
  78. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  79. {
  80. pte_t *pte;
  81. /*
  82. * nr is a running index into the array which helps higher level
  83. * callers keep track of where we're up to.
  84. */
  85. pte = pte_alloc_kernel(pmd, addr);
  86. if (!pte)
  87. return -ENOMEM;
  88. do {
  89. struct page *page = pages[*nr];
  90. if (WARN_ON(!pte_none(*pte)))
  91. return -EBUSY;
  92. if (WARN_ON(!page))
  93. return -ENOMEM;
  94. set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
  95. (*nr)++;
  96. } while (pte++, addr += PAGE_SIZE, addr != end);
  97. return 0;
  98. }
  99. static int vmap_pmd_range(pud_t *pud, unsigned long addr,
  100. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  101. {
  102. pmd_t *pmd;
  103. unsigned long next;
  104. pmd = pmd_alloc(&init_mm, pud, addr);
  105. if (!pmd)
  106. return -ENOMEM;
  107. do {
  108. next = pmd_addr_end(addr, end);
  109. if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
  110. return -ENOMEM;
  111. } while (pmd++, addr = next, addr != end);
  112. return 0;
  113. }
  114. static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
  115. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  116. {
  117. pud_t *pud;
  118. unsigned long next;
  119. pud = pud_alloc(&init_mm, pgd, addr);
  120. if (!pud)
  121. return -ENOMEM;
  122. do {
  123. next = pud_addr_end(addr, end);
  124. if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
  125. return -ENOMEM;
  126. } while (pud++, addr = next, addr != end);
  127. return 0;
  128. }
  129. /*
  130. * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
  131. * will have pfns corresponding to the "pages" array.
  132. *
  133. * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
  134. */
  135. static int vmap_page_range(unsigned long start, unsigned long end,
  136. pgprot_t prot, struct page **pages)
  137. {
  138. pgd_t *pgd;
  139. unsigned long next;
  140. unsigned long addr = start;
  141. int err = 0;
  142. int nr = 0;
  143. BUG_ON(addr >= end);
  144. pgd = pgd_offset_k(addr);
  145. do {
  146. next = pgd_addr_end(addr, end);
  147. err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
  148. if (err)
  149. break;
  150. } while (pgd++, addr = next, addr != end);
  151. flush_cache_vmap(start, end);
  152. if (unlikely(err))
  153. return err;
  154. return nr;
  155. }
  156. static inline int is_vmalloc_or_module_addr(const void *x)
  157. {
  158. /*
  159. * ARM, x86-64 and sparc64 put modules in a special place,
  160. * and fall back on vmalloc() if that fails. Others
  161. * just put it in the vmalloc space.
  162. */
  163. #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
  164. unsigned long addr = (unsigned long)x;
  165. if (addr >= MODULES_VADDR && addr < MODULES_END)
  166. return 1;
  167. #endif
  168. return is_vmalloc_addr(x);
  169. }
  170. /*
  171. * Walk a vmap address to the struct page it maps.
  172. */
  173. struct page *vmalloc_to_page(const void *vmalloc_addr)
  174. {
  175. unsigned long addr = (unsigned long) vmalloc_addr;
  176. struct page *page = NULL;
  177. pgd_t *pgd = pgd_offset_k(addr);
  178. /*
  179. * XXX we might need to change this if we add VIRTUAL_BUG_ON for
  180. * architectures that do not vmalloc module space
  181. */
  182. VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
  183. if (!pgd_none(*pgd)) {
  184. pud_t *pud = pud_offset(pgd, addr);
  185. if (!pud_none(*pud)) {
  186. pmd_t *pmd = pmd_offset(pud, addr);
  187. if (!pmd_none(*pmd)) {
  188. pte_t *ptep, pte;
  189. ptep = pte_offset_map(pmd, addr);
  190. pte = *ptep;
  191. if (pte_present(pte))
  192. page = pte_page(pte);
  193. pte_unmap(ptep);
  194. }
  195. }
  196. }
  197. return page;
  198. }
  199. EXPORT_SYMBOL(vmalloc_to_page);
  200. /*
  201. * Map a vmalloc()-space virtual address to the physical page frame number.
  202. */
  203. unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
  204. {
  205. return page_to_pfn(vmalloc_to_page(vmalloc_addr));
  206. }
  207. EXPORT_SYMBOL(vmalloc_to_pfn);
  208. /*** Global kva allocator ***/
  209. #define VM_LAZY_FREE 0x01
  210. #define VM_LAZY_FREEING 0x02
  211. #define VM_VM_AREA 0x04
  212. struct vmap_area {
  213. unsigned long va_start;
  214. unsigned long va_end;
  215. unsigned long flags;
  216. struct rb_node rb_node; /* address sorted rbtree */
  217. struct list_head list; /* address sorted list */
  218. struct list_head purge_list; /* "lazy purge" list */
  219. void *private;
  220. struct rcu_head rcu_head;
  221. };
  222. static DEFINE_SPINLOCK(vmap_area_lock);
  223. static struct rb_root vmap_area_root = RB_ROOT;
  224. static LIST_HEAD(vmap_area_list);
  225. static struct vmap_area *__find_vmap_area(unsigned long addr)
  226. {
  227. struct rb_node *n = vmap_area_root.rb_node;
  228. while (n) {
  229. struct vmap_area *va;
  230. va = rb_entry(n, struct vmap_area, rb_node);
  231. if (addr < va->va_start)
  232. n = n->rb_left;
  233. else if (addr > va->va_start)
  234. n = n->rb_right;
  235. else
  236. return va;
  237. }
  238. return NULL;
  239. }
  240. static void __insert_vmap_area(struct vmap_area *va)
  241. {
  242. struct rb_node **p = &vmap_area_root.rb_node;
  243. struct rb_node *parent = NULL;
  244. struct rb_node *tmp;
  245. while (*p) {
  246. struct vmap_area *tmp;
  247. parent = *p;
  248. tmp = rb_entry(parent, struct vmap_area, rb_node);
  249. if (va->va_start < tmp->va_end)
  250. p = &(*p)->rb_left;
  251. else if (va->va_end > tmp->va_start)
  252. p = &(*p)->rb_right;
  253. else
  254. BUG();
  255. }
  256. rb_link_node(&va->rb_node, parent, p);
  257. rb_insert_color(&va->rb_node, &vmap_area_root);
  258. /* address-sort this list so it is usable like the vmlist */
  259. tmp = rb_prev(&va->rb_node);
  260. if (tmp) {
  261. struct vmap_area *prev;
  262. prev = rb_entry(tmp, struct vmap_area, rb_node);
  263. list_add_rcu(&va->list, &prev->list);
  264. } else
  265. list_add_rcu(&va->list, &vmap_area_list);
  266. }
  267. static void purge_vmap_area_lazy(void);
  268. /*
  269. * Allocate a region of KVA of the specified size and alignment, within the
  270. * vstart and vend.
  271. */
  272. static struct vmap_area *alloc_vmap_area(unsigned long size,
  273. unsigned long align,
  274. unsigned long vstart, unsigned long vend,
  275. int node, gfp_t gfp_mask)
  276. {
  277. struct vmap_area *va;
  278. struct rb_node *n;
  279. unsigned long addr;
  280. int purged = 0;
  281. BUG_ON(size & ~PAGE_MASK);
  282. va = kmalloc_node(sizeof(struct vmap_area),
  283. gfp_mask & GFP_RECLAIM_MASK, node);
  284. if (unlikely(!va))
  285. return ERR_PTR(-ENOMEM);
  286. retry:
  287. addr = ALIGN(vstart, align);
  288. spin_lock(&vmap_area_lock);
  289. /* XXX: could have a last_hole cache */
  290. n = vmap_area_root.rb_node;
  291. if (n) {
  292. struct vmap_area *first = NULL;
  293. do {
  294. struct vmap_area *tmp;
  295. tmp = rb_entry(n, struct vmap_area, rb_node);
  296. if (tmp->va_end >= addr) {
  297. if (!first && tmp->va_start < addr + size)
  298. first = tmp;
  299. n = n->rb_left;
  300. } else {
  301. first = tmp;
  302. n = n->rb_right;
  303. }
  304. } while (n);
  305. if (!first)
  306. goto found;
  307. if (first->va_end < addr) {
  308. n = rb_next(&first->rb_node);
  309. if (n)
  310. first = rb_entry(n, struct vmap_area, rb_node);
  311. else
  312. goto found;
  313. }
  314. while (addr + size > first->va_start && addr + size <= vend) {
  315. addr = ALIGN(first->va_end + PAGE_SIZE, align);
  316. n = rb_next(&first->rb_node);
  317. if (n)
  318. first = rb_entry(n, struct vmap_area, rb_node);
  319. else
  320. goto found;
  321. }
  322. }
  323. found:
  324. if (addr + size > vend) {
  325. spin_unlock(&vmap_area_lock);
  326. if (!purged) {
  327. purge_vmap_area_lazy();
  328. purged = 1;
  329. goto retry;
  330. }
  331. if (printk_ratelimit())
  332. printk(KERN_WARNING
  333. "vmap allocation for size %lu failed: "
  334. "use vmalloc=<size> to increase size.\n", size);
  335. return ERR_PTR(-EBUSY);
  336. }
  337. BUG_ON(addr & (align-1));
  338. va->va_start = addr;
  339. va->va_end = addr + size;
  340. va->flags = 0;
  341. __insert_vmap_area(va);
  342. spin_unlock(&vmap_area_lock);
  343. return va;
  344. }
  345. static void rcu_free_va(struct rcu_head *head)
  346. {
  347. struct vmap_area *va = container_of(head, struct vmap_area, rcu_head);
  348. kfree(va);
  349. }
  350. static void __free_vmap_area(struct vmap_area *va)
  351. {
  352. BUG_ON(RB_EMPTY_NODE(&va->rb_node));
  353. rb_erase(&va->rb_node, &vmap_area_root);
  354. RB_CLEAR_NODE(&va->rb_node);
  355. list_del_rcu(&va->list);
  356. call_rcu(&va->rcu_head, rcu_free_va);
  357. }
  358. /*
  359. * Free a region of KVA allocated by alloc_vmap_area
  360. */
  361. static void free_vmap_area(struct vmap_area *va)
  362. {
  363. spin_lock(&vmap_area_lock);
  364. __free_vmap_area(va);
  365. spin_unlock(&vmap_area_lock);
  366. }
  367. /*
  368. * Clear the pagetable entries of a given vmap_area
  369. */
  370. static void unmap_vmap_area(struct vmap_area *va)
  371. {
  372. vunmap_page_range(va->va_start, va->va_end);
  373. }
  374. static void vmap_debug_free_range(unsigned long start, unsigned long end)
  375. {
  376. /*
  377. * Unmap page tables and force a TLB flush immediately if
  378. * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
  379. * bugs similarly to those in linear kernel virtual address
  380. * space after a page has been freed.
  381. *
  382. * All the lazy freeing logic is still retained, in order to
  383. * minimise intrusiveness of this debugging feature.
  384. *
  385. * This is going to be *slow* (linear kernel virtual address
  386. * debugging doesn't do a broadcast TLB flush so it is a lot
  387. * faster).
  388. */
  389. #ifdef CONFIG_DEBUG_PAGEALLOC
  390. vunmap_page_range(start, end);
  391. flush_tlb_kernel_range(start, end);
  392. #endif
  393. }
  394. /*
  395. * lazy_max_pages is the maximum amount of virtual address space we gather up
  396. * before attempting to purge with a TLB flush.
  397. *
  398. * There is a tradeoff here: a larger number will cover more kernel page tables
  399. * and take slightly longer to purge, but it will linearly reduce the number of
  400. * global TLB flushes that must be performed. It would seem natural to scale
  401. * this number up linearly with the number of CPUs (because vmapping activity
  402. * could also scale linearly with the number of CPUs), however it is likely
  403. * that in practice, workloads might be constrained in other ways that mean
  404. * vmap activity will not scale linearly with CPUs. Also, I want to be
  405. * conservative and not introduce a big latency on huge systems, so go with
  406. * a less aggressive log scale. It will still be an improvement over the old
  407. * code, and it will be simple to change the scale factor if we find that it
  408. * becomes a problem on bigger systems.
  409. */
  410. static unsigned long lazy_max_pages(void)
  411. {
  412. unsigned int log;
  413. log = fls(num_online_cpus());
  414. return log * (32UL * 1024 * 1024 / PAGE_SIZE);
  415. }
  416. static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
  417. /*
  418. * Purges all lazily-freed vmap areas.
  419. *
  420. * If sync is 0 then don't purge if there is already a purge in progress.
  421. * If force_flush is 1, then flush kernel TLBs between *start and *end even
  422. * if we found no lazy vmap areas to unmap (callers can use this to optimise
  423. * their own TLB flushing).
  424. * Returns with *start = min(*start, lowest purged address)
  425. * *end = max(*end, highest purged address)
  426. */
  427. static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
  428. int sync, int force_flush)
  429. {
  430. static DEFINE_SPINLOCK(purge_lock);
  431. LIST_HEAD(valist);
  432. struct vmap_area *va;
  433. int nr = 0;
  434. /*
  435. * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
  436. * should not expect such behaviour. This just simplifies locking for
  437. * the case that isn't actually used at the moment anyway.
  438. */
  439. if (!sync && !force_flush) {
  440. if (!spin_trylock(&purge_lock))
  441. return;
  442. } else
  443. spin_lock(&purge_lock);
  444. rcu_read_lock();
  445. list_for_each_entry_rcu(va, &vmap_area_list, list) {
  446. if (va->flags & VM_LAZY_FREE) {
  447. if (va->va_start < *start)
  448. *start = va->va_start;
  449. if (va->va_end > *end)
  450. *end = va->va_end;
  451. nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
  452. unmap_vmap_area(va);
  453. list_add_tail(&va->purge_list, &valist);
  454. va->flags |= VM_LAZY_FREEING;
  455. va->flags &= ~VM_LAZY_FREE;
  456. }
  457. }
  458. rcu_read_unlock();
  459. if (nr) {
  460. BUG_ON(nr > atomic_read(&vmap_lazy_nr));
  461. atomic_sub(nr, &vmap_lazy_nr);
  462. }
  463. if (nr || force_flush)
  464. flush_tlb_kernel_range(*start, *end);
  465. if (nr) {
  466. spin_lock(&vmap_area_lock);
  467. list_for_each_entry(va, &valist, purge_list)
  468. __free_vmap_area(va);
  469. spin_unlock(&vmap_area_lock);
  470. }
  471. spin_unlock(&purge_lock);
  472. }
  473. /*
  474. * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
  475. * is already purging.
  476. */
  477. static void try_purge_vmap_area_lazy(void)
  478. {
  479. unsigned long start = ULONG_MAX, end = 0;
  480. __purge_vmap_area_lazy(&start, &end, 0, 0);
  481. }
  482. /*
  483. * Kick off a purge of the outstanding lazy areas.
  484. */
  485. static void purge_vmap_area_lazy(void)
  486. {
  487. unsigned long start = ULONG_MAX, end = 0;
  488. __purge_vmap_area_lazy(&start, &end, 1, 0);
  489. }
  490. /*
  491. * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
  492. * called for the correct range previously.
  493. */
  494. static void free_unmap_vmap_area_noflush(struct vmap_area *va)
  495. {
  496. va->flags |= VM_LAZY_FREE;
  497. atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
  498. if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
  499. try_purge_vmap_area_lazy();
  500. }
  501. /*
  502. * Free and unmap a vmap area
  503. */
  504. static void free_unmap_vmap_area(struct vmap_area *va)
  505. {
  506. flush_cache_vunmap(va->va_start, va->va_end);
  507. free_unmap_vmap_area_noflush(va);
  508. }
  509. static struct vmap_area *find_vmap_area(unsigned long addr)
  510. {
  511. struct vmap_area *va;
  512. spin_lock(&vmap_area_lock);
  513. va = __find_vmap_area(addr);
  514. spin_unlock(&vmap_area_lock);
  515. return va;
  516. }
  517. static void free_unmap_vmap_area_addr(unsigned long addr)
  518. {
  519. struct vmap_area *va;
  520. va = find_vmap_area(addr);
  521. BUG_ON(!va);
  522. free_unmap_vmap_area(va);
  523. }
  524. /*** Per cpu kva allocator ***/
  525. /*
  526. * vmap space is limited especially on 32 bit architectures. Ensure there is
  527. * room for at least 16 percpu vmap blocks per CPU.
  528. */
  529. /*
  530. * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
  531. * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
  532. * instead (we just need a rough idea)
  533. */
  534. #if BITS_PER_LONG == 32
  535. #define VMALLOC_SPACE (128UL*1024*1024)
  536. #else
  537. #define VMALLOC_SPACE (128UL*1024*1024*1024)
  538. #endif
  539. #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
  540. #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
  541. #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
  542. #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
  543. #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
  544. #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
  545. #define VMAP_BBMAP_BITS VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
  546. VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
  547. VMALLOC_PAGES / NR_CPUS / 16))
  548. #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
  549. static bool vmap_initialized __read_mostly = false;
  550. struct vmap_block_queue {
  551. spinlock_t lock;
  552. struct list_head free;
  553. struct list_head dirty;
  554. unsigned int nr_dirty;
  555. };
  556. struct vmap_block {
  557. spinlock_t lock;
  558. struct vmap_area *va;
  559. struct vmap_block_queue *vbq;
  560. unsigned long free, dirty;
  561. DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
  562. DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
  563. union {
  564. struct {
  565. struct list_head free_list;
  566. struct list_head dirty_list;
  567. };
  568. struct rcu_head rcu_head;
  569. };
  570. };
  571. /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
  572. static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
  573. /*
  574. * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
  575. * in the free path. Could get rid of this if we change the API to return a
  576. * "cookie" from alloc, to be passed to free. But no big deal yet.
  577. */
  578. static DEFINE_SPINLOCK(vmap_block_tree_lock);
  579. static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
  580. /*
  581. * We should probably have a fallback mechanism to allocate virtual memory
  582. * out of partially filled vmap blocks. However vmap block sizing should be
  583. * fairly reasonable according to the vmalloc size, so it shouldn't be a
  584. * big problem.
  585. */
  586. static unsigned long addr_to_vb_idx(unsigned long addr)
  587. {
  588. addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
  589. addr /= VMAP_BLOCK_SIZE;
  590. return addr;
  591. }
  592. static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
  593. {
  594. struct vmap_block_queue *vbq;
  595. struct vmap_block *vb;
  596. struct vmap_area *va;
  597. unsigned long vb_idx;
  598. int node, err;
  599. node = numa_node_id();
  600. vb = kmalloc_node(sizeof(struct vmap_block),
  601. gfp_mask & GFP_RECLAIM_MASK, node);
  602. if (unlikely(!vb))
  603. return ERR_PTR(-ENOMEM);
  604. va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
  605. VMALLOC_START, VMALLOC_END,
  606. node, gfp_mask);
  607. if (unlikely(IS_ERR(va))) {
  608. kfree(vb);
  609. return ERR_PTR(PTR_ERR(va));
  610. }
  611. err = radix_tree_preload(gfp_mask);
  612. if (unlikely(err)) {
  613. kfree(vb);
  614. free_vmap_area(va);
  615. return ERR_PTR(err);
  616. }
  617. spin_lock_init(&vb->lock);
  618. vb->va = va;
  619. vb->free = VMAP_BBMAP_BITS;
  620. vb->dirty = 0;
  621. bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
  622. bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
  623. INIT_LIST_HEAD(&vb->free_list);
  624. INIT_LIST_HEAD(&vb->dirty_list);
  625. vb_idx = addr_to_vb_idx(va->va_start);
  626. spin_lock(&vmap_block_tree_lock);
  627. err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
  628. spin_unlock(&vmap_block_tree_lock);
  629. BUG_ON(err);
  630. radix_tree_preload_end();
  631. vbq = &get_cpu_var(vmap_block_queue);
  632. vb->vbq = vbq;
  633. spin_lock(&vbq->lock);
  634. list_add(&vb->free_list, &vbq->free);
  635. spin_unlock(&vbq->lock);
  636. put_cpu_var(vmap_cpu_blocks);
  637. return vb;
  638. }
  639. static void rcu_free_vb(struct rcu_head *head)
  640. {
  641. struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
  642. kfree(vb);
  643. }
  644. static void free_vmap_block(struct vmap_block *vb)
  645. {
  646. struct vmap_block *tmp;
  647. unsigned long vb_idx;
  648. spin_lock(&vb->vbq->lock);
  649. if (!list_empty(&vb->free_list))
  650. list_del(&vb->free_list);
  651. if (!list_empty(&vb->dirty_list))
  652. list_del(&vb->dirty_list);
  653. spin_unlock(&vb->vbq->lock);
  654. vb_idx = addr_to_vb_idx(vb->va->va_start);
  655. spin_lock(&vmap_block_tree_lock);
  656. tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
  657. spin_unlock(&vmap_block_tree_lock);
  658. BUG_ON(tmp != vb);
  659. free_unmap_vmap_area_noflush(vb->va);
  660. call_rcu(&vb->rcu_head, rcu_free_vb);
  661. }
  662. static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  663. {
  664. struct vmap_block_queue *vbq;
  665. struct vmap_block *vb;
  666. unsigned long addr = 0;
  667. unsigned int order;
  668. BUG_ON(size & ~PAGE_MASK);
  669. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  670. order = get_order(size);
  671. again:
  672. rcu_read_lock();
  673. vbq = &get_cpu_var(vmap_block_queue);
  674. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  675. int i;
  676. spin_lock(&vb->lock);
  677. i = bitmap_find_free_region(vb->alloc_map,
  678. VMAP_BBMAP_BITS, order);
  679. if (i >= 0) {
  680. addr = vb->va->va_start + (i << PAGE_SHIFT);
  681. BUG_ON(addr_to_vb_idx(addr) !=
  682. addr_to_vb_idx(vb->va->va_start));
  683. vb->free -= 1UL << order;
  684. if (vb->free == 0) {
  685. spin_lock(&vbq->lock);
  686. list_del_init(&vb->free_list);
  687. spin_unlock(&vbq->lock);
  688. }
  689. spin_unlock(&vb->lock);
  690. break;
  691. }
  692. spin_unlock(&vb->lock);
  693. }
  694. put_cpu_var(vmap_cpu_blocks);
  695. rcu_read_unlock();
  696. if (!addr) {
  697. vb = new_vmap_block(gfp_mask);
  698. if (IS_ERR(vb))
  699. return vb;
  700. goto again;
  701. }
  702. return (void *)addr;
  703. }
  704. static void vb_free(const void *addr, unsigned long size)
  705. {
  706. unsigned long offset;
  707. unsigned long vb_idx;
  708. unsigned int order;
  709. struct vmap_block *vb;
  710. BUG_ON(size & ~PAGE_MASK);
  711. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  712. flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
  713. order = get_order(size);
  714. offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
  715. vb_idx = addr_to_vb_idx((unsigned long)addr);
  716. rcu_read_lock();
  717. vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
  718. rcu_read_unlock();
  719. BUG_ON(!vb);
  720. spin_lock(&vb->lock);
  721. bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
  722. if (!vb->dirty) {
  723. spin_lock(&vb->vbq->lock);
  724. list_add(&vb->dirty_list, &vb->vbq->dirty);
  725. spin_unlock(&vb->vbq->lock);
  726. }
  727. vb->dirty += 1UL << order;
  728. if (vb->dirty == VMAP_BBMAP_BITS) {
  729. BUG_ON(vb->free || !list_empty(&vb->free_list));
  730. spin_unlock(&vb->lock);
  731. free_vmap_block(vb);
  732. } else
  733. spin_unlock(&vb->lock);
  734. }
  735. /**
  736. * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
  737. *
  738. * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
  739. * to amortize TLB flushing overheads. What this means is that any page you
  740. * have now, may, in a former life, have been mapped into kernel virtual
  741. * address by the vmap layer and so there might be some CPUs with TLB entries
  742. * still referencing that page (additional to the regular 1:1 kernel mapping).
  743. *
  744. * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
  745. * be sure that none of the pages we have control over will have any aliases
  746. * from the vmap layer.
  747. */
  748. void vm_unmap_aliases(void)
  749. {
  750. unsigned long start = ULONG_MAX, end = 0;
  751. int cpu;
  752. int flush = 0;
  753. if (unlikely(!vmap_initialized))
  754. return;
  755. for_each_possible_cpu(cpu) {
  756. struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
  757. struct vmap_block *vb;
  758. rcu_read_lock();
  759. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  760. int i;
  761. spin_lock(&vb->lock);
  762. i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
  763. while (i < VMAP_BBMAP_BITS) {
  764. unsigned long s, e;
  765. int j;
  766. j = find_next_zero_bit(vb->dirty_map,
  767. VMAP_BBMAP_BITS, i);
  768. s = vb->va->va_start + (i << PAGE_SHIFT);
  769. e = vb->va->va_start + (j << PAGE_SHIFT);
  770. vunmap_page_range(s, e);
  771. flush = 1;
  772. if (s < start)
  773. start = s;
  774. if (e > end)
  775. end = e;
  776. i = j;
  777. i = find_next_bit(vb->dirty_map,
  778. VMAP_BBMAP_BITS, i);
  779. }
  780. spin_unlock(&vb->lock);
  781. }
  782. rcu_read_unlock();
  783. }
  784. __purge_vmap_area_lazy(&start, &end, 1, flush);
  785. }
  786. EXPORT_SYMBOL_GPL(vm_unmap_aliases);
  787. /**
  788. * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
  789. * @mem: the pointer returned by vm_map_ram
  790. * @count: the count passed to that vm_map_ram call (cannot unmap partial)
  791. */
  792. void vm_unmap_ram(const void *mem, unsigned int count)
  793. {
  794. unsigned long size = count << PAGE_SHIFT;
  795. unsigned long addr = (unsigned long)mem;
  796. BUG_ON(!addr);
  797. BUG_ON(addr < VMALLOC_START);
  798. BUG_ON(addr > VMALLOC_END);
  799. BUG_ON(addr & (PAGE_SIZE-1));
  800. debug_check_no_locks_freed(mem, size);
  801. vmap_debug_free_range(addr, addr+size);
  802. if (likely(count <= VMAP_MAX_ALLOC))
  803. vb_free(mem, size);
  804. else
  805. free_unmap_vmap_area_addr(addr);
  806. }
  807. EXPORT_SYMBOL(vm_unmap_ram);
  808. /**
  809. * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
  810. * @pages: an array of pointers to the pages to be mapped
  811. * @count: number of pages
  812. * @node: prefer to allocate data structures on this node
  813. * @prot: memory protection to use. PAGE_KERNEL for regular RAM
  814. *
  815. * Returns: a pointer to the address that has been mapped, or %NULL on failure
  816. */
  817. void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
  818. {
  819. unsigned long size = count << PAGE_SHIFT;
  820. unsigned long addr;
  821. void *mem;
  822. if (likely(count <= VMAP_MAX_ALLOC)) {
  823. mem = vb_alloc(size, GFP_KERNEL);
  824. if (IS_ERR(mem))
  825. return NULL;
  826. addr = (unsigned long)mem;
  827. } else {
  828. struct vmap_area *va;
  829. va = alloc_vmap_area(size, PAGE_SIZE,
  830. VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
  831. if (IS_ERR(va))
  832. return NULL;
  833. addr = va->va_start;
  834. mem = (void *)addr;
  835. }
  836. if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
  837. vm_unmap_ram(mem, count);
  838. return NULL;
  839. }
  840. return mem;
  841. }
  842. EXPORT_SYMBOL(vm_map_ram);
  843. /**
  844. * vm_area_register_early - register vmap area early during boot
  845. * @vm: vm_struct to register
  846. * @size: size of area to register
  847. *
  848. * This function is used to register kernel vm area before
  849. * vmalloc_init() is called. @vm->size and @vm->flags should contain
  850. * proper values on entry and other fields should be zero. On return,
  851. * vm->addr contains the allocated address.
  852. *
  853. * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
  854. */
  855. void __init vm_area_register_early(struct vm_struct *vm)
  856. {
  857. static size_t vm_init_off __initdata;
  858. vm->addr = (void *)VMALLOC_START + vm_init_off;
  859. vm_init_off = PFN_ALIGN(vm_init_off + vm->size);
  860. vm->next = vmlist;
  861. vmlist = vm;
  862. }
  863. void __init vmalloc_init(void)
  864. {
  865. struct vmap_area *va;
  866. struct vm_struct *tmp;
  867. int i;
  868. for_each_possible_cpu(i) {
  869. struct vmap_block_queue *vbq;
  870. vbq = &per_cpu(vmap_block_queue, i);
  871. spin_lock_init(&vbq->lock);
  872. INIT_LIST_HEAD(&vbq->free);
  873. INIT_LIST_HEAD(&vbq->dirty);
  874. vbq->nr_dirty = 0;
  875. }
  876. /* Import existing vmlist entries. */
  877. for (tmp = vmlist; tmp; tmp = tmp->next) {
  878. va = alloc_bootmem(sizeof(struct vmap_area));
  879. va->flags = tmp->flags | VM_VM_AREA;
  880. va->va_start = (unsigned long)tmp->addr;
  881. va->va_end = va->va_start + tmp->size;
  882. __insert_vmap_area(va);
  883. }
  884. vmap_initialized = true;
  885. }
  886. void unmap_kernel_range(unsigned long addr, unsigned long size)
  887. {
  888. unsigned long end = addr + size;
  889. flush_cache_vunmap(addr, end);
  890. vunmap_page_range(addr, end);
  891. flush_tlb_kernel_range(addr, end);
  892. }
  893. int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
  894. {
  895. unsigned long addr = (unsigned long)area->addr;
  896. unsigned long end = addr + area->size - PAGE_SIZE;
  897. int err;
  898. err = vmap_page_range(addr, end, prot, *pages);
  899. if (err > 0) {
  900. *pages += err;
  901. err = 0;
  902. }
  903. return err;
  904. }
  905. EXPORT_SYMBOL_GPL(map_vm_area);
  906. /*** Old vmalloc interfaces ***/
  907. DEFINE_RWLOCK(vmlist_lock);
  908. struct vm_struct *vmlist;
  909. static struct vm_struct *__get_vm_area_node(unsigned long size,
  910. unsigned long flags, unsigned long start, unsigned long end,
  911. int node, gfp_t gfp_mask, void *caller)
  912. {
  913. static struct vmap_area *va;
  914. struct vm_struct *area;
  915. struct vm_struct *tmp, **p;
  916. unsigned long align = 1;
  917. BUG_ON(in_interrupt());
  918. if (flags & VM_IOREMAP) {
  919. int bit = fls(size);
  920. if (bit > IOREMAP_MAX_ORDER)
  921. bit = IOREMAP_MAX_ORDER;
  922. else if (bit < PAGE_SHIFT)
  923. bit = PAGE_SHIFT;
  924. align = 1ul << bit;
  925. }
  926. size = PAGE_ALIGN(size);
  927. if (unlikely(!size))
  928. return NULL;
  929. area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
  930. if (unlikely(!area))
  931. return NULL;
  932. /*
  933. * We always allocate a guard page.
  934. */
  935. size += PAGE_SIZE;
  936. va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
  937. if (IS_ERR(va)) {
  938. kfree(area);
  939. return NULL;
  940. }
  941. area->flags = flags;
  942. area->addr = (void *)va->va_start;
  943. area->size = size;
  944. area->pages = NULL;
  945. area->nr_pages = 0;
  946. area->phys_addr = 0;
  947. area->caller = caller;
  948. va->private = area;
  949. va->flags |= VM_VM_AREA;
  950. write_lock(&vmlist_lock);
  951. for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
  952. if (tmp->addr >= area->addr)
  953. break;
  954. }
  955. area->next = *p;
  956. *p = area;
  957. write_unlock(&vmlist_lock);
  958. return area;
  959. }
  960. struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
  961. unsigned long start, unsigned long end)
  962. {
  963. return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
  964. __builtin_return_address(0));
  965. }
  966. EXPORT_SYMBOL_GPL(__get_vm_area);
  967. /**
  968. * get_vm_area - reserve a contiguous kernel virtual area
  969. * @size: size of the area
  970. * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
  971. *
  972. * Search an area of @size in the kernel virtual mapping area,
  973. * and reserved it for out purposes. Returns the area descriptor
  974. * on success or %NULL on failure.
  975. */
  976. struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
  977. {
  978. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
  979. -1, GFP_KERNEL, __builtin_return_address(0));
  980. }
  981. struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
  982. void *caller)
  983. {
  984. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
  985. -1, GFP_KERNEL, caller);
  986. }
  987. struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
  988. int node, gfp_t gfp_mask)
  989. {
  990. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
  991. gfp_mask, __builtin_return_address(0));
  992. }
  993. static struct vm_struct *find_vm_area(const void *addr)
  994. {
  995. struct vmap_area *va;
  996. va = find_vmap_area((unsigned long)addr);
  997. if (va && va->flags & VM_VM_AREA)
  998. return va->private;
  999. return NULL;
  1000. }
  1001. /**
  1002. * remove_vm_area - find and remove a continuous kernel virtual area
  1003. * @addr: base address
  1004. *
  1005. * Search for the kernel VM area starting at @addr, and remove it.
  1006. * This function returns the found VM area, but using it is NOT safe
  1007. * on SMP machines, except for its size or flags.
  1008. */
  1009. struct vm_struct *remove_vm_area(const void *addr)
  1010. {
  1011. struct vmap_area *va;
  1012. va = find_vmap_area((unsigned long)addr);
  1013. if (va && va->flags & VM_VM_AREA) {
  1014. struct vm_struct *vm = va->private;
  1015. struct vm_struct *tmp, **p;
  1016. vmap_debug_free_range(va->va_start, va->va_end);
  1017. free_unmap_vmap_area(va);
  1018. vm->size -= PAGE_SIZE;
  1019. write_lock(&vmlist_lock);
  1020. for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
  1021. ;
  1022. *p = tmp->next;
  1023. write_unlock(&vmlist_lock);
  1024. return vm;
  1025. }
  1026. return NULL;
  1027. }
  1028. static void __vunmap(const void *addr, int deallocate_pages)
  1029. {
  1030. struct vm_struct *area;
  1031. if (!addr)
  1032. return;
  1033. if ((PAGE_SIZE-1) & (unsigned long)addr) {
  1034. WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
  1035. return;
  1036. }
  1037. area = remove_vm_area(addr);
  1038. if (unlikely(!area)) {
  1039. WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
  1040. addr);
  1041. return;
  1042. }
  1043. debug_check_no_locks_freed(addr, area->size);
  1044. debug_check_no_obj_freed(addr, area->size);
  1045. if (deallocate_pages) {
  1046. int i;
  1047. for (i = 0; i < area->nr_pages; i++) {
  1048. struct page *page = area->pages[i];
  1049. BUG_ON(!page);
  1050. __free_page(page);
  1051. }
  1052. if (area->flags & VM_VPAGES)
  1053. vfree(area->pages);
  1054. else
  1055. kfree(area->pages);
  1056. }
  1057. kfree(area);
  1058. return;
  1059. }
  1060. /**
  1061. * vfree - release memory allocated by vmalloc()
  1062. * @addr: memory base address
  1063. *
  1064. * Free the virtually continuous memory area starting at @addr, as
  1065. * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
  1066. * NULL, no operation is performed.
  1067. *
  1068. * Must not be called in interrupt context.
  1069. */
  1070. void vfree(const void *addr)
  1071. {
  1072. BUG_ON(in_interrupt());
  1073. __vunmap(addr, 1);
  1074. }
  1075. EXPORT_SYMBOL(vfree);
  1076. /**
  1077. * vunmap - release virtual mapping obtained by vmap()
  1078. * @addr: memory base address
  1079. *
  1080. * Free the virtually contiguous memory area starting at @addr,
  1081. * which was created from the page array passed to vmap().
  1082. *
  1083. * Must not be called in interrupt context.
  1084. */
  1085. void vunmap(const void *addr)
  1086. {
  1087. BUG_ON(in_interrupt());
  1088. __vunmap(addr, 0);
  1089. }
  1090. EXPORT_SYMBOL(vunmap);
  1091. /**
  1092. * vmap - map an array of pages into virtually contiguous space
  1093. * @pages: array of page pointers
  1094. * @count: number of pages to map
  1095. * @flags: vm_area->flags
  1096. * @prot: page protection for the mapping
  1097. *
  1098. * Maps @count pages from @pages into contiguous kernel virtual
  1099. * space.
  1100. */
  1101. void *vmap(struct page **pages, unsigned int count,
  1102. unsigned long flags, pgprot_t prot)
  1103. {
  1104. struct vm_struct *area;
  1105. if (count > num_physpages)
  1106. return NULL;
  1107. area = get_vm_area_caller((count << PAGE_SHIFT), flags,
  1108. __builtin_return_address(0));
  1109. if (!area)
  1110. return NULL;
  1111. if (map_vm_area(area, prot, &pages)) {
  1112. vunmap(area->addr);
  1113. return NULL;
  1114. }
  1115. return area->addr;
  1116. }
  1117. EXPORT_SYMBOL(vmap);
  1118. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  1119. int node, void *caller);
  1120. static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  1121. pgprot_t prot, int node, void *caller)
  1122. {
  1123. struct page **pages;
  1124. unsigned int nr_pages, array_size, i;
  1125. nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
  1126. array_size = (nr_pages * sizeof(struct page *));
  1127. area->nr_pages = nr_pages;
  1128. /* Please note that the recursion is strictly bounded. */
  1129. if (array_size > PAGE_SIZE) {
  1130. pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
  1131. PAGE_KERNEL, node, caller);
  1132. area->flags |= VM_VPAGES;
  1133. } else {
  1134. pages = kmalloc_node(array_size,
  1135. (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
  1136. node);
  1137. }
  1138. area->pages = pages;
  1139. area->caller = caller;
  1140. if (!area->pages) {
  1141. remove_vm_area(area->addr);
  1142. kfree(area);
  1143. return NULL;
  1144. }
  1145. for (i = 0; i < area->nr_pages; i++) {
  1146. struct page *page;
  1147. if (node < 0)
  1148. page = alloc_page(gfp_mask);
  1149. else
  1150. page = alloc_pages_node(node, gfp_mask, 0);
  1151. if (unlikely(!page)) {
  1152. /* Successfully allocated i pages, free them in __vunmap() */
  1153. area->nr_pages = i;
  1154. goto fail;
  1155. }
  1156. area->pages[i] = page;
  1157. }
  1158. if (map_vm_area(area, prot, &pages))
  1159. goto fail;
  1160. return area->addr;
  1161. fail:
  1162. vfree(area->addr);
  1163. return NULL;
  1164. }
  1165. void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
  1166. {
  1167. return __vmalloc_area_node(area, gfp_mask, prot, -1,
  1168. __builtin_return_address(0));
  1169. }
  1170. /**
  1171. * __vmalloc_node - allocate virtually contiguous memory
  1172. * @size: allocation size
  1173. * @gfp_mask: flags for the page level allocator
  1174. * @prot: protection mask for the allocated pages
  1175. * @node: node to use for allocation or -1
  1176. * @caller: caller's return address
  1177. *
  1178. * Allocate enough pages to cover @size from the page level
  1179. * allocator with @gfp_mask flags. Map them into contiguous
  1180. * kernel virtual space, using a pagetable protection of @prot.
  1181. */
  1182. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  1183. int node, void *caller)
  1184. {
  1185. struct vm_struct *area;
  1186. size = PAGE_ALIGN(size);
  1187. if (!size || (size >> PAGE_SHIFT) > num_physpages)
  1188. return NULL;
  1189. area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
  1190. node, gfp_mask, caller);
  1191. if (!area)
  1192. return NULL;
  1193. return __vmalloc_area_node(area, gfp_mask, prot, node, caller);
  1194. }
  1195. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  1196. {
  1197. return __vmalloc_node(size, gfp_mask, prot, -1,
  1198. __builtin_return_address(0));
  1199. }
  1200. EXPORT_SYMBOL(__vmalloc);
  1201. /**
  1202. * vmalloc - allocate virtually contiguous memory
  1203. * @size: allocation size
  1204. * Allocate enough pages to cover @size from the page level
  1205. * allocator and map them into contiguous kernel virtual space.
  1206. *
  1207. * For tight control over page level allocator and protection flags
  1208. * use __vmalloc() instead.
  1209. */
  1210. void *vmalloc(unsigned long size)
  1211. {
  1212. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
  1213. -1, __builtin_return_address(0));
  1214. }
  1215. EXPORT_SYMBOL(vmalloc);
  1216. /**
  1217. * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
  1218. * @size: allocation size
  1219. *
  1220. * The resulting memory area is zeroed so it can be mapped to userspace
  1221. * without leaking data.
  1222. */
  1223. void *vmalloc_user(unsigned long size)
  1224. {
  1225. struct vm_struct *area;
  1226. void *ret;
  1227. ret = __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
  1228. PAGE_KERNEL, -1, __builtin_return_address(0));
  1229. if (ret) {
  1230. area = find_vm_area(ret);
  1231. area->flags |= VM_USERMAP;
  1232. }
  1233. return ret;
  1234. }
  1235. EXPORT_SYMBOL(vmalloc_user);
  1236. /**
  1237. * vmalloc_node - allocate memory on a specific node
  1238. * @size: allocation size
  1239. * @node: numa node
  1240. *
  1241. * Allocate enough pages to cover @size from the page level
  1242. * allocator and map them into contiguous kernel virtual space.
  1243. *
  1244. * For tight control over page level allocator and protection flags
  1245. * use __vmalloc() instead.
  1246. */
  1247. void *vmalloc_node(unsigned long size, int node)
  1248. {
  1249. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
  1250. node, __builtin_return_address(0));
  1251. }
  1252. EXPORT_SYMBOL(vmalloc_node);
  1253. #ifndef PAGE_KERNEL_EXEC
  1254. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  1255. #endif
  1256. /**
  1257. * vmalloc_exec - allocate virtually contiguous, executable memory
  1258. * @size: allocation size
  1259. *
  1260. * Kernel-internal function to allocate enough pages to cover @size
  1261. * the page level allocator and map them into contiguous and
  1262. * executable kernel virtual space.
  1263. *
  1264. * For tight control over page level allocator and protection flags
  1265. * use __vmalloc() instead.
  1266. */
  1267. void *vmalloc_exec(unsigned long size)
  1268. {
  1269. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
  1270. -1, __builtin_return_address(0));
  1271. }
  1272. #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
  1273. #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
  1274. #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
  1275. #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
  1276. #else
  1277. #define GFP_VMALLOC32 GFP_KERNEL
  1278. #endif
  1279. /**
  1280. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  1281. * @size: allocation size
  1282. *
  1283. * Allocate enough 32bit PA addressable pages to cover @size from the
  1284. * page level allocator and map them into contiguous kernel virtual space.
  1285. */
  1286. void *vmalloc_32(unsigned long size)
  1287. {
  1288. return __vmalloc_node(size, GFP_VMALLOC32, PAGE_KERNEL,
  1289. -1, __builtin_return_address(0));
  1290. }
  1291. EXPORT_SYMBOL(vmalloc_32);
  1292. /**
  1293. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  1294. * @size: allocation size
  1295. *
  1296. * The resulting memory area is 32bit addressable and zeroed so it can be
  1297. * mapped to userspace without leaking data.
  1298. */
  1299. void *vmalloc_32_user(unsigned long size)
  1300. {
  1301. struct vm_struct *area;
  1302. void *ret;
  1303. ret = __vmalloc_node(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
  1304. -1, __builtin_return_address(0));
  1305. if (ret) {
  1306. area = find_vm_area(ret);
  1307. area->flags |= VM_USERMAP;
  1308. }
  1309. return ret;
  1310. }
  1311. EXPORT_SYMBOL(vmalloc_32_user);
  1312. long vread(char *buf, char *addr, unsigned long count)
  1313. {
  1314. struct vm_struct *tmp;
  1315. char *vaddr, *buf_start = buf;
  1316. unsigned long n;
  1317. /* Don't allow overflow */
  1318. if ((unsigned long) addr + count < count)
  1319. count = -(unsigned long) addr;
  1320. read_lock(&vmlist_lock);
  1321. for (tmp = vmlist; tmp; tmp = tmp->next) {
  1322. vaddr = (char *) tmp->addr;
  1323. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  1324. continue;
  1325. while (addr < vaddr) {
  1326. if (count == 0)
  1327. goto finished;
  1328. *buf = '\0';
  1329. buf++;
  1330. addr++;
  1331. count--;
  1332. }
  1333. n = vaddr + tmp->size - PAGE_SIZE - addr;
  1334. do {
  1335. if (count == 0)
  1336. goto finished;
  1337. *buf = *addr;
  1338. buf++;
  1339. addr++;
  1340. count--;
  1341. } while (--n > 0);
  1342. }
  1343. finished:
  1344. read_unlock(&vmlist_lock);
  1345. return buf - buf_start;
  1346. }
  1347. long vwrite(char *buf, char *addr, unsigned long count)
  1348. {
  1349. struct vm_struct *tmp;
  1350. char *vaddr, *buf_start = buf;
  1351. unsigned long n;
  1352. /* Don't allow overflow */
  1353. if ((unsigned long) addr + count < count)
  1354. count = -(unsigned long) addr;
  1355. read_lock(&vmlist_lock);
  1356. for (tmp = vmlist; tmp; tmp = tmp->next) {
  1357. vaddr = (char *) tmp->addr;
  1358. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  1359. continue;
  1360. while (addr < vaddr) {
  1361. if (count == 0)
  1362. goto finished;
  1363. buf++;
  1364. addr++;
  1365. count--;
  1366. }
  1367. n = vaddr + tmp->size - PAGE_SIZE - addr;
  1368. do {
  1369. if (count == 0)
  1370. goto finished;
  1371. *addr = *buf;
  1372. buf++;
  1373. addr++;
  1374. count--;
  1375. } while (--n > 0);
  1376. }
  1377. finished:
  1378. read_unlock(&vmlist_lock);
  1379. return buf - buf_start;
  1380. }
  1381. /**
  1382. * remap_vmalloc_range - map vmalloc pages to userspace
  1383. * @vma: vma to cover (map full range of vma)
  1384. * @addr: vmalloc memory
  1385. * @pgoff: number of pages into addr before first page to map
  1386. *
  1387. * Returns: 0 for success, -Exxx on failure
  1388. *
  1389. * This function checks that addr is a valid vmalloc'ed area, and
  1390. * that it is big enough to cover the vma. Will return failure if
  1391. * that criteria isn't met.
  1392. *
  1393. * Similar to remap_pfn_range() (see mm/memory.c)
  1394. */
  1395. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  1396. unsigned long pgoff)
  1397. {
  1398. struct vm_struct *area;
  1399. unsigned long uaddr = vma->vm_start;
  1400. unsigned long usize = vma->vm_end - vma->vm_start;
  1401. if ((PAGE_SIZE-1) & (unsigned long)addr)
  1402. return -EINVAL;
  1403. area = find_vm_area(addr);
  1404. if (!area)
  1405. return -EINVAL;
  1406. if (!(area->flags & VM_USERMAP))
  1407. return -EINVAL;
  1408. if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
  1409. return -EINVAL;
  1410. addr += pgoff << PAGE_SHIFT;
  1411. do {
  1412. struct page *page = vmalloc_to_page(addr);
  1413. int ret;
  1414. ret = vm_insert_page(vma, uaddr, page);
  1415. if (ret)
  1416. return ret;
  1417. uaddr += PAGE_SIZE;
  1418. addr += PAGE_SIZE;
  1419. usize -= PAGE_SIZE;
  1420. } while (usize > 0);
  1421. /* Prevent "things" like memory migration? VM_flags need a cleanup... */
  1422. vma->vm_flags |= VM_RESERVED;
  1423. return 0;
  1424. }
  1425. EXPORT_SYMBOL(remap_vmalloc_range);
  1426. /*
  1427. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  1428. * have one.
  1429. */
  1430. void __attribute__((weak)) vmalloc_sync_all(void)
  1431. {
  1432. }
  1433. static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
  1434. {
  1435. /* apply_to_page_range() does all the hard work. */
  1436. return 0;
  1437. }
  1438. /**
  1439. * alloc_vm_area - allocate a range of kernel address space
  1440. * @size: size of the area
  1441. *
  1442. * Returns: NULL on failure, vm_struct on success
  1443. *
  1444. * This function reserves a range of kernel address space, and
  1445. * allocates pagetables to map that range. No actual mappings
  1446. * are created. If the kernel address space is not shared
  1447. * between processes, it syncs the pagetable across all
  1448. * processes.
  1449. */
  1450. struct vm_struct *alloc_vm_area(size_t size)
  1451. {
  1452. struct vm_struct *area;
  1453. area = get_vm_area_caller(size, VM_IOREMAP,
  1454. __builtin_return_address(0));
  1455. if (area == NULL)
  1456. return NULL;
  1457. /*
  1458. * This ensures that page tables are constructed for this region
  1459. * of kernel virtual address space and mapped into init_mm.
  1460. */
  1461. if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
  1462. area->size, f, NULL)) {
  1463. free_vm_area(area);
  1464. return NULL;
  1465. }
  1466. /* Make sure the pagetables are constructed in process kernel
  1467. mappings */
  1468. vmalloc_sync_all();
  1469. return area;
  1470. }
  1471. EXPORT_SYMBOL_GPL(alloc_vm_area);
  1472. void free_vm_area(struct vm_struct *area)
  1473. {
  1474. struct vm_struct *ret;
  1475. ret = remove_vm_area(area->addr);
  1476. BUG_ON(ret != area);
  1477. kfree(area);
  1478. }
  1479. EXPORT_SYMBOL_GPL(free_vm_area);
  1480. #ifdef CONFIG_PROC_FS
  1481. static void *s_start(struct seq_file *m, loff_t *pos)
  1482. {
  1483. loff_t n = *pos;
  1484. struct vm_struct *v;
  1485. read_lock(&vmlist_lock);
  1486. v = vmlist;
  1487. while (n > 0 && v) {
  1488. n--;
  1489. v = v->next;
  1490. }
  1491. if (!n)
  1492. return v;
  1493. return NULL;
  1494. }
  1495. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  1496. {
  1497. struct vm_struct *v = p;
  1498. ++*pos;
  1499. return v->next;
  1500. }
  1501. static void s_stop(struct seq_file *m, void *p)
  1502. {
  1503. read_unlock(&vmlist_lock);
  1504. }
  1505. static void show_numa_info(struct seq_file *m, struct vm_struct *v)
  1506. {
  1507. if (NUMA_BUILD) {
  1508. unsigned int nr, *counters = m->private;
  1509. if (!counters)
  1510. return;
  1511. memset(counters, 0, nr_node_ids * sizeof(unsigned int));
  1512. for (nr = 0; nr < v->nr_pages; nr++)
  1513. counters[page_to_nid(v->pages[nr])]++;
  1514. for_each_node_state(nr, N_HIGH_MEMORY)
  1515. if (counters[nr])
  1516. seq_printf(m, " N%u=%u", nr, counters[nr]);
  1517. }
  1518. }
  1519. static int s_show(struct seq_file *m, void *p)
  1520. {
  1521. struct vm_struct *v = p;
  1522. seq_printf(m, "0x%p-0x%p %7ld",
  1523. v->addr, v->addr + v->size, v->size);
  1524. if (v->caller) {
  1525. char buff[KSYM_SYMBOL_LEN];
  1526. seq_putc(m, ' ');
  1527. sprint_symbol(buff, (unsigned long)v->caller);
  1528. seq_puts(m, buff);
  1529. }
  1530. if (v->nr_pages)
  1531. seq_printf(m, " pages=%d", v->nr_pages);
  1532. if (v->phys_addr)
  1533. seq_printf(m, " phys=%lx", v->phys_addr);
  1534. if (v->flags & VM_IOREMAP)
  1535. seq_printf(m, " ioremap");
  1536. if (v->flags & VM_ALLOC)
  1537. seq_printf(m, " vmalloc");
  1538. if (v->flags & VM_MAP)
  1539. seq_printf(m, " vmap");
  1540. if (v->flags & VM_USERMAP)
  1541. seq_printf(m, " user");
  1542. if (v->flags & VM_VPAGES)
  1543. seq_printf(m, " vpages");
  1544. show_numa_info(m, v);
  1545. seq_putc(m, '\n');
  1546. return 0;
  1547. }
  1548. static const struct seq_operations vmalloc_op = {
  1549. .start = s_start,
  1550. .next = s_next,
  1551. .stop = s_stop,
  1552. .show = s_show,
  1553. };
  1554. static int vmalloc_open(struct inode *inode, struct file *file)
  1555. {
  1556. unsigned int *ptr = NULL;
  1557. int ret;
  1558. if (NUMA_BUILD)
  1559. ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
  1560. ret = seq_open(file, &vmalloc_op);
  1561. if (!ret) {
  1562. struct seq_file *m = file->private_data;
  1563. m->private = ptr;
  1564. } else
  1565. kfree(ptr);
  1566. return ret;
  1567. }
  1568. static const struct file_operations proc_vmalloc_operations = {
  1569. .open = vmalloc_open,
  1570. .read = seq_read,
  1571. .llseek = seq_lseek,
  1572. .release = seq_release_private,
  1573. };
  1574. static int __init proc_vmalloc_init(void)
  1575. {
  1576. proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
  1577. return 0;
  1578. }
  1579. module_init(proc_vmalloc_init);
  1580. #endif