vmalloc.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  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. va = kmalloc_node(sizeof(struct vmap_area),
  281. gfp_mask & GFP_RECLAIM_MASK, node);
  282. if (unlikely(!va))
  283. return ERR_PTR(-ENOMEM);
  284. retry:
  285. addr = ALIGN(vstart, align);
  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. Don't bother if somebody
  452. * is already purging.
  453. */
  454. static void try_purge_vmap_area_lazy(void)
  455. {
  456. unsigned long start = ULONG_MAX, end = 0;
  457. __purge_vmap_area_lazy(&start, &end, 0, 0);
  458. }
  459. /*
  460. * Kick off a purge of the outstanding lazy areas.
  461. */
  462. static void purge_vmap_area_lazy(void)
  463. {
  464. unsigned long start = ULONG_MAX, end = 0;
  465. __purge_vmap_area_lazy(&start, &end, 1, 0);
  466. }
  467. /*
  468. * Free and unmap a vmap area
  469. */
  470. static void free_unmap_vmap_area(struct vmap_area *va)
  471. {
  472. va->flags |= VM_LAZY_FREE;
  473. atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
  474. if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
  475. try_purge_vmap_area_lazy();
  476. }
  477. static struct vmap_area *find_vmap_area(unsigned long addr)
  478. {
  479. struct vmap_area *va;
  480. spin_lock(&vmap_area_lock);
  481. va = __find_vmap_area(addr);
  482. spin_unlock(&vmap_area_lock);
  483. return va;
  484. }
  485. static void free_unmap_vmap_area_addr(unsigned long addr)
  486. {
  487. struct vmap_area *va;
  488. va = find_vmap_area(addr);
  489. BUG_ON(!va);
  490. free_unmap_vmap_area(va);
  491. }
  492. /*** Per cpu kva allocator ***/
  493. /*
  494. * vmap space is limited especially on 32 bit architectures. Ensure there is
  495. * room for at least 16 percpu vmap blocks per CPU.
  496. */
  497. /*
  498. * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
  499. * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
  500. * instead (we just need a rough idea)
  501. */
  502. #if BITS_PER_LONG == 32
  503. #define VMALLOC_SPACE (128UL*1024*1024)
  504. #else
  505. #define VMALLOC_SPACE (128UL*1024*1024*1024)
  506. #endif
  507. #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
  508. #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
  509. #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
  510. #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
  511. #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
  512. #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
  513. #define VMAP_BBMAP_BITS VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
  514. VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
  515. VMALLOC_PAGES / NR_CPUS / 16))
  516. #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
  517. static bool vmap_initialized __read_mostly = false;
  518. struct vmap_block_queue {
  519. spinlock_t lock;
  520. struct list_head free;
  521. struct list_head dirty;
  522. unsigned int nr_dirty;
  523. };
  524. struct vmap_block {
  525. spinlock_t lock;
  526. struct vmap_area *va;
  527. struct vmap_block_queue *vbq;
  528. unsigned long free, dirty;
  529. DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
  530. DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
  531. union {
  532. struct {
  533. struct list_head free_list;
  534. struct list_head dirty_list;
  535. };
  536. struct rcu_head rcu_head;
  537. };
  538. };
  539. /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
  540. static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
  541. /*
  542. * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
  543. * in the free path. Could get rid of this if we change the API to return a
  544. * "cookie" from alloc, to be passed to free. But no big deal yet.
  545. */
  546. static DEFINE_SPINLOCK(vmap_block_tree_lock);
  547. static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
  548. /*
  549. * We should probably have a fallback mechanism to allocate virtual memory
  550. * out of partially filled vmap blocks. However vmap block sizing should be
  551. * fairly reasonable according to the vmalloc size, so it shouldn't be a
  552. * big problem.
  553. */
  554. static unsigned long addr_to_vb_idx(unsigned long addr)
  555. {
  556. addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
  557. addr /= VMAP_BLOCK_SIZE;
  558. return addr;
  559. }
  560. static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
  561. {
  562. struct vmap_block_queue *vbq;
  563. struct vmap_block *vb;
  564. struct vmap_area *va;
  565. unsigned long vb_idx;
  566. int node, err;
  567. node = numa_node_id();
  568. vb = kmalloc_node(sizeof(struct vmap_block),
  569. gfp_mask & GFP_RECLAIM_MASK, node);
  570. if (unlikely(!vb))
  571. return ERR_PTR(-ENOMEM);
  572. va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
  573. VMALLOC_START, VMALLOC_END,
  574. node, gfp_mask);
  575. if (unlikely(IS_ERR(va))) {
  576. kfree(vb);
  577. return ERR_PTR(PTR_ERR(va));
  578. }
  579. err = radix_tree_preload(gfp_mask);
  580. if (unlikely(err)) {
  581. kfree(vb);
  582. free_vmap_area(va);
  583. return ERR_PTR(err);
  584. }
  585. spin_lock_init(&vb->lock);
  586. vb->va = va;
  587. vb->free = VMAP_BBMAP_BITS;
  588. vb->dirty = 0;
  589. bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
  590. bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
  591. INIT_LIST_HEAD(&vb->free_list);
  592. INIT_LIST_HEAD(&vb->dirty_list);
  593. vb_idx = addr_to_vb_idx(va->va_start);
  594. spin_lock(&vmap_block_tree_lock);
  595. err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
  596. spin_unlock(&vmap_block_tree_lock);
  597. BUG_ON(err);
  598. radix_tree_preload_end();
  599. vbq = &get_cpu_var(vmap_block_queue);
  600. vb->vbq = vbq;
  601. spin_lock(&vbq->lock);
  602. list_add(&vb->free_list, &vbq->free);
  603. spin_unlock(&vbq->lock);
  604. put_cpu_var(vmap_cpu_blocks);
  605. return vb;
  606. }
  607. static void rcu_free_vb(struct rcu_head *head)
  608. {
  609. struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
  610. kfree(vb);
  611. }
  612. static void free_vmap_block(struct vmap_block *vb)
  613. {
  614. struct vmap_block *tmp;
  615. unsigned long vb_idx;
  616. spin_lock(&vb->vbq->lock);
  617. if (!list_empty(&vb->free_list))
  618. list_del(&vb->free_list);
  619. if (!list_empty(&vb->dirty_list))
  620. list_del(&vb->dirty_list);
  621. spin_unlock(&vb->vbq->lock);
  622. vb_idx = addr_to_vb_idx(vb->va->va_start);
  623. spin_lock(&vmap_block_tree_lock);
  624. tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
  625. spin_unlock(&vmap_block_tree_lock);
  626. BUG_ON(tmp != vb);
  627. free_unmap_vmap_area(vb->va);
  628. call_rcu(&vb->rcu_head, rcu_free_vb);
  629. }
  630. static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  631. {
  632. struct vmap_block_queue *vbq;
  633. struct vmap_block *vb;
  634. unsigned long addr = 0;
  635. unsigned int order;
  636. BUG_ON(size & ~PAGE_MASK);
  637. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  638. order = get_order(size);
  639. again:
  640. rcu_read_lock();
  641. vbq = &get_cpu_var(vmap_block_queue);
  642. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  643. int i;
  644. spin_lock(&vb->lock);
  645. i = bitmap_find_free_region(vb->alloc_map,
  646. VMAP_BBMAP_BITS, order);
  647. if (i >= 0) {
  648. addr = vb->va->va_start + (i << PAGE_SHIFT);
  649. BUG_ON(addr_to_vb_idx(addr) !=
  650. addr_to_vb_idx(vb->va->va_start));
  651. vb->free -= 1UL << order;
  652. if (vb->free == 0) {
  653. spin_lock(&vbq->lock);
  654. list_del_init(&vb->free_list);
  655. spin_unlock(&vbq->lock);
  656. }
  657. spin_unlock(&vb->lock);
  658. break;
  659. }
  660. spin_unlock(&vb->lock);
  661. }
  662. put_cpu_var(vmap_cpu_blocks);
  663. rcu_read_unlock();
  664. if (!addr) {
  665. vb = new_vmap_block(gfp_mask);
  666. if (IS_ERR(vb))
  667. return vb;
  668. goto again;
  669. }
  670. return (void *)addr;
  671. }
  672. static void vb_free(const void *addr, unsigned long size)
  673. {
  674. unsigned long offset;
  675. unsigned long vb_idx;
  676. unsigned int order;
  677. struct vmap_block *vb;
  678. BUG_ON(size & ~PAGE_MASK);
  679. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  680. order = get_order(size);
  681. offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
  682. vb_idx = addr_to_vb_idx((unsigned long)addr);
  683. rcu_read_lock();
  684. vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
  685. rcu_read_unlock();
  686. BUG_ON(!vb);
  687. spin_lock(&vb->lock);
  688. bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
  689. if (!vb->dirty) {
  690. spin_lock(&vb->vbq->lock);
  691. list_add(&vb->dirty_list, &vb->vbq->dirty);
  692. spin_unlock(&vb->vbq->lock);
  693. }
  694. vb->dirty += 1UL << order;
  695. if (vb->dirty == VMAP_BBMAP_BITS) {
  696. BUG_ON(vb->free || !list_empty(&vb->free_list));
  697. spin_unlock(&vb->lock);
  698. free_vmap_block(vb);
  699. } else
  700. spin_unlock(&vb->lock);
  701. }
  702. /**
  703. * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
  704. *
  705. * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
  706. * to amortize TLB flushing overheads. What this means is that any page you
  707. * have now, may, in a former life, have been mapped into kernel virtual
  708. * address by the vmap layer and so there might be some CPUs with TLB entries
  709. * still referencing that page (additional to the regular 1:1 kernel mapping).
  710. *
  711. * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
  712. * be sure that none of the pages we have control over will have any aliases
  713. * from the vmap layer.
  714. */
  715. void vm_unmap_aliases(void)
  716. {
  717. unsigned long start = ULONG_MAX, end = 0;
  718. int cpu;
  719. int flush = 0;
  720. if (unlikely(!vmap_initialized))
  721. return;
  722. for_each_possible_cpu(cpu) {
  723. struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
  724. struct vmap_block *vb;
  725. rcu_read_lock();
  726. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  727. int i;
  728. spin_lock(&vb->lock);
  729. i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
  730. while (i < VMAP_BBMAP_BITS) {
  731. unsigned long s, e;
  732. int j;
  733. j = find_next_zero_bit(vb->dirty_map,
  734. VMAP_BBMAP_BITS, i);
  735. s = vb->va->va_start + (i << PAGE_SHIFT);
  736. e = vb->va->va_start + (j << PAGE_SHIFT);
  737. vunmap_page_range(s, e);
  738. flush = 1;
  739. if (s < start)
  740. start = s;
  741. if (e > end)
  742. end = e;
  743. i = j;
  744. i = find_next_bit(vb->dirty_map,
  745. VMAP_BBMAP_BITS, i);
  746. }
  747. spin_unlock(&vb->lock);
  748. }
  749. rcu_read_unlock();
  750. }
  751. __purge_vmap_area_lazy(&start, &end, 1, flush);
  752. }
  753. EXPORT_SYMBOL_GPL(vm_unmap_aliases);
  754. /**
  755. * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
  756. * @mem: the pointer returned by vm_map_ram
  757. * @count: the count passed to that vm_map_ram call (cannot unmap partial)
  758. */
  759. void vm_unmap_ram(const void *mem, unsigned int count)
  760. {
  761. unsigned long size = count << PAGE_SHIFT;
  762. unsigned long addr = (unsigned long)mem;
  763. BUG_ON(!addr);
  764. BUG_ON(addr < VMALLOC_START);
  765. BUG_ON(addr > VMALLOC_END);
  766. BUG_ON(addr & (PAGE_SIZE-1));
  767. debug_check_no_locks_freed(mem, size);
  768. if (likely(count <= VMAP_MAX_ALLOC))
  769. vb_free(mem, size);
  770. else
  771. free_unmap_vmap_area_addr(addr);
  772. }
  773. EXPORT_SYMBOL(vm_unmap_ram);
  774. /**
  775. * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
  776. * @pages: an array of pointers to the pages to be mapped
  777. * @count: number of pages
  778. * @node: prefer to allocate data structures on this node
  779. * @prot: memory protection to use. PAGE_KERNEL for regular RAM
  780. *
  781. * Returns: a pointer to the address that has been mapped, or %NULL on failure
  782. */
  783. void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
  784. {
  785. unsigned long size = count << PAGE_SHIFT;
  786. unsigned long addr;
  787. void *mem;
  788. if (likely(count <= VMAP_MAX_ALLOC)) {
  789. mem = vb_alloc(size, GFP_KERNEL);
  790. if (IS_ERR(mem))
  791. return NULL;
  792. addr = (unsigned long)mem;
  793. } else {
  794. struct vmap_area *va;
  795. va = alloc_vmap_area(size, PAGE_SIZE,
  796. VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
  797. if (IS_ERR(va))
  798. return NULL;
  799. addr = va->va_start;
  800. mem = (void *)addr;
  801. }
  802. if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
  803. vm_unmap_ram(mem, count);
  804. return NULL;
  805. }
  806. return mem;
  807. }
  808. EXPORT_SYMBOL(vm_map_ram);
  809. void __init vmalloc_init(void)
  810. {
  811. int i;
  812. for_each_possible_cpu(i) {
  813. struct vmap_block_queue *vbq;
  814. vbq = &per_cpu(vmap_block_queue, i);
  815. spin_lock_init(&vbq->lock);
  816. INIT_LIST_HEAD(&vbq->free);
  817. INIT_LIST_HEAD(&vbq->dirty);
  818. vbq->nr_dirty = 0;
  819. }
  820. vmap_initialized = true;
  821. }
  822. void unmap_kernel_range(unsigned long addr, unsigned long size)
  823. {
  824. unsigned long end = addr + size;
  825. vunmap_page_range(addr, end);
  826. flush_tlb_kernel_range(addr, end);
  827. }
  828. int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
  829. {
  830. unsigned long addr = (unsigned long)area->addr;
  831. unsigned long end = addr + area->size - PAGE_SIZE;
  832. int err;
  833. err = vmap_page_range(addr, end, prot, *pages);
  834. if (err > 0) {
  835. *pages += err;
  836. err = 0;
  837. }
  838. return err;
  839. }
  840. EXPORT_SYMBOL_GPL(map_vm_area);
  841. /*** Old vmalloc interfaces ***/
  842. DEFINE_RWLOCK(vmlist_lock);
  843. struct vm_struct *vmlist;
  844. static struct vm_struct *__get_vm_area_node(unsigned long size,
  845. unsigned long flags, unsigned long start, unsigned long end,
  846. int node, gfp_t gfp_mask, void *caller)
  847. {
  848. static struct vmap_area *va;
  849. struct vm_struct *area;
  850. struct vm_struct *tmp, **p;
  851. unsigned long align = 1;
  852. BUG_ON(in_interrupt());
  853. if (flags & VM_IOREMAP) {
  854. int bit = fls(size);
  855. if (bit > IOREMAP_MAX_ORDER)
  856. bit = IOREMAP_MAX_ORDER;
  857. else if (bit < PAGE_SHIFT)
  858. bit = PAGE_SHIFT;
  859. align = 1ul << bit;
  860. }
  861. size = PAGE_ALIGN(size);
  862. if (unlikely(!size))
  863. return NULL;
  864. area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
  865. if (unlikely(!area))
  866. return NULL;
  867. /*
  868. * We always allocate a guard page.
  869. */
  870. size += PAGE_SIZE;
  871. va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
  872. if (IS_ERR(va)) {
  873. kfree(area);
  874. return NULL;
  875. }
  876. area->flags = flags;
  877. area->addr = (void *)va->va_start;
  878. area->size = size;
  879. area->pages = NULL;
  880. area->nr_pages = 0;
  881. area->phys_addr = 0;
  882. area->caller = caller;
  883. va->private = area;
  884. va->flags |= VM_VM_AREA;
  885. write_lock(&vmlist_lock);
  886. for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
  887. if (tmp->addr >= area->addr)
  888. break;
  889. }
  890. area->next = *p;
  891. *p = area;
  892. write_unlock(&vmlist_lock);
  893. return area;
  894. }
  895. struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
  896. unsigned long start, unsigned long end)
  897. {
  898. return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
  899. __builtin_return_address(0));
  900. }
  901. EXPORT_SYMBOL_GPL(__get_vm_area);
  902. /**
  903. * get_vm_area - reserve a contiguous kernel virtual area
  904. * @size: size of the area
  905. * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
  906. *
  907. * Search an area of @size in the kernel virtual mapping area,
  908. * and reserved it for out purposes. Returns the area descriptor
  909. * on success or %NULL on failure.
  910. */
  911. struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
  912. {
  913. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
  914. -1, GFP_KERNEL, __builtin_return_address(0));
  915. }
  916. struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
  917. void *caller)
  918. {
  919. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
  920. -1, GFP_KERNEL, caller);
  921. }
  922. struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
  923. int node, gfp_t gfp_mask)
  924. {
  925. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
  926. gfp_mask, __builtin_return_address(0));
  927. }
  928. static struct vm_struct *find_vm_area(const void *addr)
  929. {
  930. struct vmap_area *va;
  931. va = find_vmap_area((unsigned long)addr);
  932. if (va && va->flags & VM_VM_AREA)
  933. return va->private;
  934. return NULL;
  935. }
  936. /**
  937. * remove_vm_area - find and remove a continuous kernel virtual area
  938. * @addr: base address
  939. *
  940. * Search for the kernel VM area starting at @addr, and remove it.
  941. * This function returns the found VM area, but using it is NOT safe
  942. * on SMP machines, except for its size or flags.
  943. */
  944. struct vm_struct *remove_vm_area(const void *addr)
  945. {
  946. struct vmap_area *va;
  947. va = find_vmap_area((unsigned long)addr);
  948. if (va && va->flags & VM_VM_AREA) {
  949. struct vm_struct *vm = va->private;
  950. struct vm_struct *tmp, **p;
  951. free_unmap_vmap_area(va);
  952. vm->size -= PAGE_SIZE;
  953. write_lock(&vmlist_lock);
  954. for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
  955. ;
  956. *p = tmp->next;
  957. write_unlock(&vmlist_lock);
  958. return vm;
  959. }
  960. return NULL;
  961. }
  962. static void __vunmap(const void *addr, int deallocate_pages)
  963. {
  964. struct vm_struct *area;
  965. if (!addr)
  966. return;
  967. if ((PAGE_SIZE-1) & (unsigned long)addr) {
  968. WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
  969. return;
  970. }
  971. area = remove_vm_area(addr);
  972. if (unlikely(!area)) {
  973. WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
  974. addr);
  975. return;
  976. }
  977. debug_check_no_locks_freed(addr, area->size);
  978. debug_check_no_obj_freed(addr, area->size);
  979. if (deallocate_pages) {
  980. int i;
  981. for (i = 0; i < area->nr_pages; i++) {
  982. struct page *page = area->pages[i];
  983. BUG_ON(!page);
  984. __free_page(page);
  985. }
  986. if (area->flags & VM_VPAGES)
  987. vfree(area->pages);
  988. else
  989. kfree(area->pages);
  990. }
  991. kfree(area);
  992. return;
  993. }
  994. /**
  995. * vfree - release memory allocated by vmalloc()
  996. * @addr: memory base address
  997. *
  998. * Free the virtually continuous memory area starting at @addr, as
  999. * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
  1000. * NULL, no operation is performed.
  1001. *
  1002. * Must not be called in interrupt context.
  1003. */
  1004. void vfree(const void *addr)
  1005. {
  1006. BUG_ON(in_interrupt());
  1007. __vunmap(addr, 1);
  1008. }
  1009. EXPORT_SYMBOL(vfree);
  1010. /**
  1011. * vunmap - release virtual mapping obtained by vmap()
  1012. * @addr: memory base address
  1013. *
  1014. * Free the virtually contiguous memory area starting at @addr,
  1015. * which was created from the page array passed to vmap().
  1016. *
  1017. * Must not be called in interrupt context.
  1018. */
  1019. void vunmap(const void *addr)
  1020. {
  1021. BUG_ON(in_interrupt());
  1022. __vunmap(addr, 0);
  1023. }
  1024. EXPORT_SYMBOL(vunmap);
  1025. /**
  1026. * vmap - map an array of pages into virtually contiguous space
  1027. * @pages: array of page pointers
  1028. * @count: number of pages to map
  1029. * @flags: vm_area->flags
  1030. * @prot: page protection for the mapping
  1031. *
  1032. * Maps @count pages from @pages into contiguous kernel virtual
  1033. * space.
  1034. */
  1035. void *vmap(struct page **pages, unsigned int count,
  1036. unsigned long flags, pgprot_t prot)
  1037. {
  1038. struct vm_struct *area;
  1039. if (count > num_physpages)
  1040. return NULL;
  1041. area = get_vm_area_caller((count << PAGE_SHIFT), flags,
  1042. __builtin_return_address(0));
  1043. if (!area)
  1044. return NULL;
  1045. if (map_vm_area(area, prot, &pages)) {
  1046. vunmap(area->addr);
  1047. return NULL;
  1048. }
  1049. return area->addr;
  1050. }
  1051. EXPORT_SYMBOL(vmap);
  1052. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  1053. int node, void *caller);
  1054. static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  1055. pgprot_t prot, int node, void *caller)
  1056. {
  1057. struct page **pages;
  1058. unsigned int nr_pages, array_size, i;
  1059. nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
  1060. array_size = (nr_pages * sizeof(struct page *));
  1061. area->nr_pages = nr_pages;
  1062. /* Please note that the recursion is strictly bounded. */
  1063. if (array_size > PAGE_SIZE) {
  1064. pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
  1065. PAGE_KERNEL, node, caller);
  1066. area->flags |= VM_VPAGES;
  1067. } else {
  1068. pages = kmalloc_node(array_size,
  1069. (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
  1070. node);
  1071. }
  1072. area->pages = pages;
  1073. area->caller = caller;
  1074. if (!area->pages) {
  1075. remove_vm_area(area->addr);
  1076. kfree(area);
  1077. return NULL;
  1078. }
  1079. for (i = 0; i < area->nr_pages; i++) {
  1080. struct page *page;
  1081. if (node < 0)
  1082. page = alloc_page(gfp_mask);
  1083. else
  1084. page = alloc_pages_node(node, gfp_mask, 0);
  1085. if (unlikely(!page)) {
  1086. /* Successfully allocated i pages, free them in __vunmap() */
  1087. area->nr_pages = i;
  1088. goto fail;
  1089. }
  1090. area->pages[i] = page;
  1091. }
  1092. if (map_vm_area(area, prot, &pages))
  1093. goto fail;
  1094. return area->addr;
  1095. fail:
  1096. vfree(area->addr);
  1097. return NULL;
  1098. }
  1099. void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
  1100. {
  1101. return __vmalloc_area_node(area, gfp_mask, prot, -1,
  1102. __builtin_return_address(0));
  1103. }
  1104. /**
  1105. * __vmalloc_node - allocate virtually contiguous memory
  1106. * @size: allocation size
  1107. * @gfp_mask: flags for the page level allocator
  1108. * @prot: protection mask for the allocated pages
  1109. * @node: node to use for allocation or -1
  1110. * @caller: caller's return address
  1111. *
  1112. * Allocate enough pages to cover @size from the page level
  1113. * allocator with @gfp_mask flags. Map them into contiguous
  1114. * kernel virtual space, using a pagetable protection of @prot.
  1115. */
  1116. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  1117. int node, void *caller)
  1118. {
  1119. struct vm_struct *area;
  1120. size = PAGE_ALIGN(size);
  1121. if (!size || (size >> PAGE_SHIFT) > num_physpages)
  1122. return NULL;
  1123. area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
  1124. node, gfp_mask, caller);
  1125. if (!area)
  1126. return NULL;
  1127. return __vmalloc_area_node(area, gfp_mask, prot, node, caller);
  1128. }
  1129. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  1130. {
  1131. return __vmalloc_node(size, gfp_mask, prot, -1,
  1132. __builtin_return_address(0));
  1133. }
  1134. EXPORT_SYMBOL(__vmalloc);
  1135. /**
  1136. * vmalloc - allocate virtually contiguous memory
  1137. * @size: allocation size
  1138. * Allocate enough pages to cover @size from the page level
  1139. * allocator and map them into contiguous kernel virtual space.
  1140. *
  1141. * For tight control over page level allocator and protection flags
  1142. * use __vmalloc() instead.
  1143. */
  1144. void *vmalloc(unsigned long size)
  1145. {
  1146. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
  1147. -1, __builtin_return_address(0));
  1148. }
  1149. EXPORT_SYMBOL(vmalloc);
  1150. /**
  1151. * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
  1152. * @size: allocation size
  1153. *
  1154. * The resulting memory area is zeroed so it can be mapped to userspace
  1155. * without leaking data.
  1156. */
  1157. void *vmalloc_user(unsigned long size)
  1158. {
  1159. struct vm_struct *area;
  1160. void *ret;
  1161. ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
  1162. if (ret) {
  1163. area = find_vm_area(ret);
  1164. area->flags |= VM_USERMAP;
  1165. }
  1166. return ret;
  1167. }
  1168. EXPORT_SYMBOL(vmalloc_user);
  1169. /**
  1170. * vmalloc_node - allocate memory on a specific node
  1171. * @size: allocation size
  1172. * @node: numa node
  1173. *
  1174. * Allocate enough pages to cover @size from the page level
  1175. * allocator and map them into contiguous kernel virtual space.
  1176. *
  1177. * For tight control over page level allocator and protection flags
  1178. * use __vmalloc() instead.
  1179. */
  1180. void *vmalloc_node(unsigned long size, int node)
  1181. {
  1182. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
  1183. node, __builtin_return_address(0));
  1184. }
  1185. EXPORT_SYMBOL(vmalloc_node);
  1186. #ifndef PAGE_KERNEL_EXEC
  1187. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  1188. #endif
  1189. /**
  1190. * vmalloc_exec - allocate virtually contiguous, executable memory
  1191. * @size: allocation size
  1192. *
  1193. * Kernel-internal function to allocate enough pages to cover @size
  1194. * the page level allocator and map them into contiguous and
  1195. * executable kernel virtual space.
  1196. *
  1197. * For tight control over page level allocator and protection flags
  1198. * use __vmalloc() instead.
  1199. */
  1200. void *vmalloc_exec(unsigned long size)
  1201. {
  1202. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
  1203. }
  1204. #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
  1205. #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
  1206. #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
  1207. #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
  1208. #else
  1209. #define GFP_VMALLOC32 GFP_KERNEL
  1210. #endif
  1211. /**
  1212. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  1213. * @size: allocation size
  1214. *
  1215. * Allocate enough 32bit PA addressable pages to cover @size from the
  1216. * page level allocator and map them into contiguous kernel virtual space.
  1217. */
  1218. void *vmalloc_32(unsigned long size)
  1219. {
  1220. return __vmalloc(size, GFP_VMALLOC32, PAGE_KERNEL);
  1221. }
  1222. EXPORT_SYMBOL(vmalloc_32);
  1223. /**
  1224. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  1225. * @size: allocation size
  1226. *
  1227. * The resulting memory area is 32bit addressable and zeroed so it can be
  1228. * mapped to userspace without leaking data.
  1229. */
  1230. void *vmalloc_32_user(unsigned long size)
  1231. {
  1232. struct vm_struct *area;
  1233. void *ret;
  1234. ret = __vmalloc(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL);
  1235. if (ret) {
  1236. area = find_vm_area(ret);
  1237. area->flags |= VM_USERMAP;
  1238. }
  1239. return ret;
  1240. }
  1241. EXPORT_SYMBOL(vmalloc_32_user);
  1242. long vread(char *buf, char *addr, unsigned long count)
  1243. {
  1244. struct vm_struct *tmp;
  1245. char *vaddr, *buf_start = buf;
  1246. unsigned long n;
  1247. /* Don't allow overflow */
  1248. if ((unsigned long) addr + count < count)
  1249. count = -(unsigned long) addr;
  1250. read_lock(&vmlist_lock);
  1251. for (tmp = vmlist; tmp; tmp = tmp->next) {
  1252. vaddr = (char *) tmp->addr;
  1253. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  1254. continue;
  1255. while (addr < vaddr) {
  1256. if (count == 0)
  1257. goto finished;
  1258. *buf = '\0';
  1259. buf++;
  1260. addr++;
  1261. count--;
  1262. }
  1263. n = vaddr + tmp->size - PAGE_SIZE - addr;
  1264. do {
  1265. if (count == 0)
  1266. goto finished;
  1267. *buf = *addr;
  1268. buf++;
  1269. addr++;
  1270. count--;
  1271. } while (--n > 0);
  1272. }
  1273. finished:
  1274. read_unlock(&vmlist_lock);
  1275. return buf - buf_start;
  1276. }
  1277. long vwrite(char *buf, char *addr, unsigned long count)
  1278. {
  1279. struct vm_struct *tmp;
  1280. char *vaddr, *buf_start = buf;
  1281. unsigned long n;
  1282. /* Don't allow overflow */
  1283. if ((unsigned long) addr + count < count)
  1284. count = -(unsigned long) addr;
  1285. read_lock(&vmlist_lock);
  1286. for (tmp = vmlist; tmp; tmp = tmp->next) {
  1287. vaddr = (char *) tmp->addr;
  1288. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  1289. continue;
  1290. while (addr < vaddr) {
  1291. if (count == 0)
  1292. goto finished;
  1293. buf++;
  1294. addr++;
  1295. count--;
  1296. }
  1297. n = vaddr + tmp->size - PAGE_SIZE - addr;
  1298. do {
  1299. if (count == 0)
  1300. goto finished;
  1301. *addr = *buf;
  1302. buf++;
  1303. addr++;
  1304. count--;
  1305. } while (--n > 0);
  1306. }
  1307. finished:
  1308. read_unlock(&vmlist_lock);
  1309. return buf - buf_start;
  1310. }
  1311. /**
  1312. * remap_vmalloc_range - map vmalloc pages to userspace
  1313. * @vma: vma to cover (map full range of vma)
  1314. * @addr: vmalloc memory
  1315. * @pgoff: number of pages into addr before first page to map
  1316. *
  1317. * Returns: 0 for success, -Exxx on failure
  1318. *
  1319. * This function checks that addr is a valid vmalloc'ed area, and
  1320. * that it is big enough to cover the vma. Will return failure if
  1321. * that criteria isn't met.
  1322. *
  1323. * Similar to remap_pfn_range() (see mm/memory.c)
  1324. */
  1325. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  1326. unsigned long pgoff)
  1327. {
  1328. struct vm_struct *area;
  1329. unsigned long uaddr = vma->vm_start;
  1330. unsigned long usize = vma->vm_end - vma->vm_start;
  1331. if ((PAGE_SIZE-1) & (unsigned long)addr)
  1332. return -EINVAL;
  1333. area = find_vm_area(addr);
  1334. if (!area)
  1335. return -EINVAL;
  1336. if (!(area->flags & VM_USERMAP))
  1337. return -EINVAL;
  1338. if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
  1339. return -EINVAL;
  1340. addr += pgoff << PAGE_SHIFT;
  1341. do {
  1342. struct page *page = vmalloc_to_page(addr);
  1343. int ret;
  1344. ret = vm_insert_page(vma, uaddr, page);
  1345. if (ret)
  1346. return ret;
  1347. uaddr += PAGE_SIZE;
  1348. addr += PAGE_SIZE;
  1349. usize -= PAGE_SIZE;
  1350. } while (usize > 0);
  1351. /* Prevent "things" like memory migration? VM_flags need a cleanup... */
  1352. vma->vm_flags |= VM_RESERVED;
  1353. return 0;
  1354. }
  1355. EXPORT_SYMBOL(remap_vmalloc_range);
  1356. /*
  1357. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  1358. * have one.
  1359. */
  1360. void __attribute__((weak)) vmalloc_sync_all(void)
  1361. {
  1362. }
  1363. static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
  1364. {
  1365. /* apply_to_page_range() does all the hard work. */
  1366. return 0;
  1367. }
  1368. /**
  1369. * alloc_vm_area - allocate a range of kernel address space
  1370. * @size: size of the area
  1371. *
  1372. * Returns: NULL on failure, vm_struct on success
  1373. *
  1374. * This function reserves a range of kernel address space, and
  1375. * allocates pagetables to map that range. No actual mappings
  1376. * are created. If the kernel address space is not shared
  1377. * between processes, it syncs the pagetable across all
  1378. * processes.
  1379. */
  1380. struct vm_struct *alloc_vm_area(size_t size)
  1381. {
  1382. struct vm_struct *area;
  1383. area = get_vm_area_caller(size, VM_IOREMAP,
  1384. __builtin_return_address(0));
  1385. if (area == NULL)
  1386. return NULL;
  1387. /*
  1388. * This ensures that page tables are constructed for this region
  1389. * of kernel virtual address space and mapped into init_mm.
  1390. */
  1391. if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
  1392. area->size, f, NULL)) {
  1393. free_vm_area(area);
  1394. return NULL;
  1395. }
  1396. /* Make sure the pagetables are constructed in process kernel
  1397. mappings */
  1398. vmalloc_sync_all();
  1399. return area;
  1400. }
  1401. EXPORT_SYMBOL_GPL(alloc_vm_area);
  1402. void free_vm_area(struct vm_struct *area)
  1403. {
  1404. struct vm_struct *ret;
  1405. ret = remove_vm_area(area->addr);
  1406. BUG_ON(ret != area);
  1407. kfree(area);
  1408. }
  1409. EXPORT_SYMBOL_GPL(free_vm_area);
  1410. #ifdef CONFIG_PROC_FS
  1411. static void *s_start(struct seq_file *m, loff_t *pos)
  1412. {
  1413. loff_t n = *pos;
  1414. struct vm_struct *v;
  1415. read_lock(&vmlist_lock);
  1416. v = vmlist;
  1417. while (n > 0 && v) {
  1418. n--;
  1419. v = v->next;
  1420. }
  1421. if (!n)
  1422. return v;
  1423. return NULL;
  1424. }
  1425. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  1426. {
  1427. struct vm_struct *v = p;
  1428. ++*pos;
  1429. return v->next;
  1430. }
  1431. static void s_stop(struct seq_file *m, void *p)
  1432. {
  1433. read_unlock(&vmlist_lock);
  1434. }
  1435. static void show_numa_info(struct seq_file *m, struct vm_struct *v)
  1436. {
  1437. if (NUMA_BUILD) {
  1438. unsigned int nr, *counters = m->private;
  1439. if (!counters)
  1440. return;
  1441. memset(counters, 0, nr_node_ids * sizeof(unsigned int));
  1442. for (nr = 0; nr < v->nr_pages; nr++)
  1443. counters[page_to_nid(v->pages[nr])]++;
  1444. for_each_node_state(nr, N_HIGH_MEMORY)
  1445. if (counters[nr])
  1446. seq_printf(m, " N%u=%u", nr, counters[nr]);
  1447. }
  1448. }
  1449. static int s_show(struct seq_file *m, void *p)
  1450. {
  1451. struct vm_struct *v = p;
  1452. seq_printf(m, "0x%p-0x%p %7ld",
  1453. v->addr, v->addr + v->size, v->size);
  1454. if (v->caller) {
  1455. char buff[2 * KSYM_NAME_LEN];
  1456. seq_putc(m, ' ');
  1457. sprint_symbol(buff, (unsigned long)v->caller);
  1458. seq_puts(m, buff);
  1459. }
  1460. if (v->nr_pages)
  1461. seq_printf(m, " pages=%d", v->nr_pages);
  1462. if (v->phys_addr)
  1463. seq_printf(m, " phys=%lx", v->phys_addr);
  1464. if (v->flags & VM_IOREMAP)
  1465. seq_printf(m, " ioremap");
  1466. if (v->flags & VM_ALLOC)
  1467. seq_printf(m, " vmalloc");
  1468. if (v->flags & VM_MAP)
  1469. seq_printf(m, " vmap");
  1470. if (v->flags & VM_USERMAP)
  1471. seq_printf(m, " user");
  1472. if (v->flags & VM_VPAGES)
  1473. seq_printf(m, " vpages");
  1474. show_numa_info(m, v);
  1475. seq_putc(m, '\n');
  1476. return 0;
  1477. }
  1478. static const struct seq_operations vmalloc_op = {
  1479. .start = s_start,
  1480. .next = s_next,
  1481. .stop = s_stop,
  1482. .show = s_show,
  1483. };
  1484. static int vmalloc_open(struct inode *inode, struct file *file)
  1485. {
  1486. unsigned int *ptr = NULL;
  1487. int ret;
  1488. if (NUMA_BUILD)
  1489. ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
  1490. ret = seq_open(file, &vmalloc_op);
  1491. if (!ret) {
  1492. struct seq_file *m = file->private_data;
  1493. m->private = ptr;
  1494. } else
  1495. kfree(ptr);
  1496. return ret;
  1497. }
  1498. static const struct file_operations proc_vmalloc_operations = {
  1499. .open = vmalloc_open,
  1500. .read = seq_read,
  1501. .llseek = seq_lseek,
  1502. .release = seq_release_private,
  1503. };
  1504. static int __init proc_vmalloc_init(void)
  1505. {
  1506. proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
  1507. return 0;
  1508. }
  1509. module_init(proc_vmalloc_init);
  1510. #endif