vmalloc.c 41 KB

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