vmalloc.c 40 KB

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