vmalloc.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783
  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/sched/signal.h>
  15. #include <linux/slab.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/debugobjects.h>
  21. #include <linux/kallsyms.h>
  22. #include <linux/list.h>
  23. #include <linux/notifier.h>
  24. #include <linux/rbtree.h>
  25. #include <linux/radix-tree.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/pfn.h>
  28. #include <linux/kmemleak.h>
  29. #include <linux/atomic.h>
  30. #include <linux/compiler.h>
  31. #include <linux/llist.h>
  32. #include <linux/bitops.h>
  33. #include <linux/uaccess.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/shmparam.h>
  36. #include "internal.h"
  37. struct vfree_deferred {
  38. struct llist_head list;
  39. struct work_struct wq;
  40. };
  41. static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
  42. static void __vunmap(const void *, int);
  43. static void free_work(struct work_struct *w)
  44. {
  45. struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
  46. struct llist_node *llnode = llist_del_all(&p->list);
  47. while (llnode) {
  48. void *p = llnode;
  49. llnode = llist_next(llnode);
  50. __vunmap(p, 1);
  51. }
  52. }
  53. /*** Page table manipulation functions ***/
  54. static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
  55. {
  56. pte_t *pte;
  57. pte = pte_offset_kernel(pmd, addr);
  58. do {
  59. pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
  60. WARN_ON(!pte_none(ptent) && !pte_present(ptent));
  61. } while (pte++, addr += PAGE_SIZE, addr != end);
  62. }
  63. static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
  64. {
  65. pmd_t *pmd;
  66. unsigned long next;
  67. pmd = pmd_offset(pud, addr);
  68. do {
  69. next = pmd_addr_end(addr, end);
  70. if (pmd_clear_huge(pmd))
  71. continue;
  72. if (pmd_none_or_clear_bad(pmd))
  73. continue;
  74. vunmap_pte_range(pmd, addr, next);
  75. } while (pmd++, addr = next, addr != end);
  76. }
  77. static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end)
  78. {
  79. pud_t *pud;
  80. unsigned long next;
  81. pud = pud_offset(p4d, addr);
  82. do {
  83. next = pud_addr_end(addr, end);
  84. if (pud_clear_huge(pud))
  85. continue;
  86. if (pud_none_or_clear_bad(pud))
  87. continue;
  88. vunmap_pmd_range(pud, addr, next);
  89. } while (pud++, addr = next, addr != end);
  90. }
  91. static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end)
  92. {
  93. p4d_t *p4d;
  94. unsigned long next;
  95. p4d = p4d_offset(pgd, addr);
  96. do {
  97. next = p4d_addr_end(addr, end);
  98. if (p4d_clear_huge(p4d))
  99. continue;
  100. if (p4d_none_or_clear_bad(p4d))
  101. continue;
  102. vunmap_pud_range(p4d, addr, next);
  103. } while (p4d++, addr = next, addr != end);
  104. }
  105. static void vunmap_page_range(unsigned long addr, unsigned long end)
  106. {
  107. pgd_t *pgd;
  108. unsigned long next;
  109. BUG_ON(addr >= end);
  110. pgd = pgd_offset_k(addr);
  111. do {
  112. next = pgd_addr_end(addr, end);
  113. if (pgd_none_or_clear_bad(pgd))
  114. continue;
  115. vunmap_p4d_range(pgd, addr, next);
  116. } while (pgd++, addr = next, addr != end);
  117. }
  118. static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
  119. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  120. {
  121. pte_t *pte;
  122. /*
  123. * nr is a running index into the array which helps higher level
  124. * callers keep track of where we're up to.
  125. */
  126. pte = pte_alloc_kernel(pmd, addr);
  127. if (!pte)
  128. return -ENOMEM;
  129. do {
  130. struct page *page = pages[*nr];
  131. if (WARN_ON(!pte_none(*pte)))
  132. return -EBUSY;
  133. if (WARN_ON(!page))
  134. return -ENOMEM;
  135. set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
  136. (*nr)++;
  137. } while (pte++, addr += PAGE_SIZE, addr != end);
  138. return 0;
  139. }
  140. static int vmap_pmd_range(pud_t *pud, unsigned long addr,
  141. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  142. {
  143. pmd_t *pmd;
  144. unsigned long next;
  145. pmd = pmd_alloc(&init_mm, pud, addr);
  146. if (!pmd)
  147. return -ENOMEM;
  148. do {
  149. next = pmd_addr_end(addr, end);
  150. if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
  151. return -ENOMEM;
  152. } while (pmd++, addr = next, addr != end);
  153. return 0;
  154. }
  155. static int vmap_pud_range(p4d_t *p4d, unsigned long addr,
  156. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  157. {
  158. pud_t *pud;
  159. unsigned long next;
  160. pud = pud_alloc(&init_mm, p4d, addr);
  161. if (!pud)
  162. return -ENOMEM;
  163. do {
  164. next = pud_addr_end(addr, end);
  165. if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
  166. return -ENOMEM;
  167. } while (pud++, addr = next, addr != end);
  168. return 0;
  169. }
  170. static int vmap_p4d_range(pgd_t *pgd, unsigned long addr,
  171. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  172. {
  173. p4d_t *p4d;
  174. unsigned long next;
  175. p4d = p4d_alloc(&init_mm, pgd, addr);
  176. if (!p4d)
  177. return -ENOMEM;
  178. do {
  179. next = p4d_addr_end(addr, end);
  180. if (vmap_pud_range(p4d, addr, next, prot, pages, nr))
  181. return -ENOMEM;
  182. } while (p4d++, addr = next, addr != end);
  183. return 0;
  184. }
  185. /*
  186. * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
  187. * will have pfns corresponding to the "pages" array.
  188. *
  189. * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
  190. */
  191. static int vmap_page_range_noflush(unsigned long start, unsigned long end,
  192. pgprot_t prot, struct page **pages)
  193. {
  194. pgd_t *pgd;
  195. unsigned long next;
  196. unsigned long addr = start;
  197. int err = 0;
  198. int nr = 0;
  199. BUG_ON(addr >= end);
  200. pgd = pgd_offset_k(addr);
  201. do {
  202. next = pgd_addr_end(addr, end);
  203. err = vmap_p4d_range(pgd, addr, next, prot, pages, &nr);
  204. if (err)
  205. return err;
  206. } while (pgd++, addr = next, addr != end);
  207. return nr;
  208. }
  209. static int vmap_page_range(unsigned long start, unsigned long end,
  210. pgprot_t prot, struct page **pages)
  211. {
  212. int ret;
  213. ret = vmap_page_range_noflush(start, end, prot, pages);
  214. flush_cache_vmap(start, end);
  215. return ret;
  216. }
  217. int is_vmalloc_or_module_addr(const void *x)
  218. {
  219. /*
  220. * ARM, x86-64 and sparc64 put modules in a special place,
  221. * and fall back on vmalloc() if that fails. Others
  222. * just put it in the vmalloc space.
  223. */
  224. #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
  225. unsigned long addr = (unsigned long)x;
  226. if (addr >= MODULES_VADDR && addr < MODULES_END)
  227. return 1;
  228. #endif
  229. return is_vmalloc_addr(x);
  230. }
  231. /*
  232. * Walk a vmap address to the struct page it maps.
  233. */
  234. struct page *vmalloc_to_page(const void *vmalloc_addr)
  235. {
  236. unsigned long addr = (unsigned long) vmalloc_addr;
  237. struct page *page = NULL;
  238. pgd_t *pgd = pgd_offset_k(addr);
  239. p4d_t *p4d;
  240. pud_t *pud;
  241. pmd_t *pmd;
  242. pte_t *ptep, pte;
  243. /*
  244. * XXX we might need to change this if we add VIRTUAL_BUG_ON for
  245. * architectures that do not vmalloc module space
  246. */
  247. VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
  248. if (pgd_none(*pgd))
  249. return NULL;
  250. p4d = p4d_offset(pgd, addr);
  251. if (p4d_none(*p4d))
  252. return NULL;
  253. pud = pud_offset(p4d, addr);
  254. /*
  255. * Don't dereference bad PUD or PMD (below) entries. This will also
  256. * identify huge mappings, which we may encounter on architectures
  257. * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be
  258. * identified as vmalloc addresses by is_vmalloc_addr(), but are
  259. * not [unambiguously] associated with a struct page, so there is
  260. * no correct value to return for them.
  261. */
  262. WARN_ON_ONCE(pud_bad(*pud));
  263. if (pud_none(*pud) || pud_bad(*pud))
  264. return NULL;
  265. pmd = pmd_offset(pud, addr);
  266. WARN_ON_ONCE(pmd_bad(*pmd));
  267. if (pmd_none(*pmd) || pmd_bad(*pmd))
  268. return NULL;
  269. ptep = pte_offset_map(pmd, addr);
  270. pte = *ptep;
  271. if (pte_present(pte))
  272. page = pte_page(pte);
  273. pte_unmap(ptep);
  274. return page;
  275. }
  276. EXPORT_SYMBOL(vmalloc_to_page);
  277. /*
  278. * Map a vmalloc()-space virtual address to the physical page frame number.
  279. */
  280. unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
  281. {
  282. return page_to_pfn(vmalloc_to_page(vmalloc_addr));
  283. }
  284. EXPORT_SYMBOL(vmalloc_to_pfn);
  285. /*** Global kva allocator ***/
  286. #define VM_LAZY_FREE 0x02
  287. #define VM_VM_AREA 0x04
  288. static DEFINE_SPINLOCK(vmap_area_lock);
  289. /* Export for kexec only */
  290. LIST_HEAD(vmap_area_list);
  291. static LLIST_HEAD(vmap_purge_list);
  292. static struct rb_root vmap_area_root = RB_ROOT;
  293. /* The vmap cache globals are protected by vmap_area_lock */
  294. static struct rb_node *free_vmap_cache;
  295. static unsigned long cached_hole_size;
  296. static unsigned long cached_vstart;
  297. static unsigned long cached_align;
  298. static unsigned long vmap_area_pcpu_hole;
  299. static struct vmap_area *__find_vmap_area(unsigned long addr)
  300. {
  301. struct rb_node *n = vmap_area_root.rb_node;
  302. while (n) {
  303. struct vmap_area *va;
  304. va = rb_entry(n, struct vmap_area, rb_node);
  305. if (addr < va->va_start)
  306. n = n->rb_left;
  307. else if (addr >= va->va_end)
  308. n = n->rb_right;
  309. else
  310. return va;
  311. }
  312. return NULL;
  313. }
  314. static void __insert_vmap_area(struct vmap_area *va)
  315. {
  316. struct rb_node **p = &vmap_area_root.rb_node;
  317. struct rb_node *parent = NULL;
  318. struct rb_node *tmp;
  319. while (*p) {
  320. struct vmap_area *tmp_va;
  321. parent = *p;
  322. tmp_va = rb_entry(parent, struct vmap_area, rb_node);
  323. if (va->va_start < tmp_va->va_end)
  324. p = &(*p)->rb_left;
  325. else if (va->va_end > tmp_va->va_start)
  326. p = &(*p)->rb_right;
  327. else
  328. BUG();
  329. }
  330. rb_link_node(&va->rb_node, parent, p);
  331. rb_insert_color(&va->rb_node, &vmap_area_root);
  332. /* address-sort this list */
  333. tmp = rb_prev(&va->rb_node);
  334. if (tmp) {
  335. struct vmap_area *prev;
  336. prev = rb_entry(tmp, struct vmap_area, rb_node);
  337. list_add_rcu(&va->list, &prev->list);
  338. } else
  339. list_add_rcu(&va->list, &vmap_area_list);
  340. }
  341. static void purge_vmap_area_lazy(void);
  342. static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
  343. /*
  344. * Allocate a region of KVA of the specified size and alignment, within the
  345. * vstart and vend.
  346. */
  347. static struct vmap_area *alloc_vmap_area(unsigned long size,
  348. unsigned long align,
  349. unsigned long vstart, unsigned long vend,
  350. int node, gfp_t gfp_mask)
  351. {
  352. struct vmap_area *va;
  353. struct rb_node *n;
  354. unsigned long addr;
  355. int purged = 0;
  356. struct vmap_area *first;
  357. BUG_ON(!size);
  358. BUG_ON(offset_in_page(size));
  359. BUG_ON(!is_power_of_2(align));
  360. might_sleep();
  361. va = kmalloc_node(sizeof(struct vmap_area),
  362. gfp_mask & GFP_RECLAIM_MASK, node);
  363. if (unlikely(!va))
  364. return ERR_PTR(-ENOMEM);
  365. /*
  366. * Only scan the relevant parts containing pointers to other objects
  367. * to avoid false negatives.
  368. */
  369. kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK);
  370. retry:
  371. spin_lock(&vmap_area_lock);
  372. /*
  373. * Invalidate cache if we have more permissive parameters.
  374. * cached_hole_size notes the largest hole noticed _below_
  375. * the vmap_area cached in free_vmap_cache: if size fits
  376. * into that hole, we want to scan from vstart to reuse
  377. * the hole instead of allocating above free_vmap_cache.
  378. * Note that __free_vmap_area may update free_vmap_cache
  379. * without updating cached_hole_size or cached_align.
  380. */
  381. if (!free_vmap_cache ||
  382. size < cached_hole_size ||
  383. vstart < cached_vstart ||
  384. align < cached_align) {
  385. nocache:
  386. cached_hole_size = 0;
  387. free_vmap_cache = NULL;
  388. }
  389. /* record if we encounter less permissive parameters */
  390. cached_vstart = vstart;
  391. cached_align = align;
  392. /* find starting point for our search */
  393. if (free_vmap_cache) {
  394. first = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
  395. addr = ALIGN(first->va_end, align);
  396. if (addr < vstart)
  397. goto nocache;
  398. if (addr + size < addr)
  399. goto overflow;
  400. } else {
  401. addr = ALIGN(vstart, align);
  402. if (addr + size < addr)
  403. goto overflow;
  404. n = vmap_area_root.rb_node;
  405. first = NULL;
  406. while (n) {
  407. struct vmap_area *tmp;
  408. tmp = rb_entry(n, struct vmap_area, rb_node);
  409. if (tmp->va_end >= addr) {
  410. first = tmp;
  411. if (tmp->va_start <= addr)
  412. break;
  413. n = n->rb_left;
  414. } else
  415. n = n->rb_right;
  416. }
  417. if (!first)
  418. goto found;
  419. }
  420. /* from the starting point, walk areas until a suitable hole is found */
  421. while (addr + size > first->va_start && addr + size <= vend) {
  422. if (addr + cached_hole_size < first->va_start)
  423. cached_hole_size = first->va_start - addr;
  424. addr = ALIGN(first->va_end, align);
  425. if (addr + size < addr)
  426. goto overflow;
  427. if (list_is_last(&first->list, &vmap_area_list))
  428. goto found;
  429. first = list_next_entry(first, list);
  430. }
  431. found:
  432. if (addr + size > vend)
  433. goto overflow;
  434. va->va_start = addr;
  435. va->va_end = addr + size;
  436. va->flags = 0;
  437. __insert_vmap_area(va);
  438. free_vmap_cache = &va->rb_node;
  439. spin_unlock(&vmap_area_lock);
  440. BUG_ON(!IS_ALIGNED(va->va_start, align));
  441. BUG_ON(va->va_start < vstart);
  442. BUG_ON(va->va_end > vend);
  443. return va;
  444. overflow:
  445. spin_unlock(&vmap_area_lock);
  446. if (!purged) {
  447. purge_vmap_area_lazy();
  448. purged = 1;
  449. goto retry;
  450. }
  451. if (gfpflags_allow_blocking(gfp_mask)) {
  452. unsigned long freed = 0;
  453. blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
  454. if (freed > 0) {
  455. purged = 0;
  456. goto retry;
  457. }
  458. }
  459. if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
  460. pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
  461. size);
  462. kfree(va);
  463. return ERR_PTR(-EBUSY);
  464. }
  465. int register_vmap_purge_notifier(struct notifier_block *nb)
  466. {
  467. return blocking_notifier_chain_register(&vmap_notify_list, nb);
  468. }
  469. EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
  470. int unregister_vmap_purge_notifier(struct notifier_block *nb)
  471. {
  472. return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
  473. }
  474. EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
  475. static void __free_vmap_area(struct vmap_area *va)
  476. {
  477. BUG_ON(RB_EMPTY_NODE(&va->rb_node));
  478. if (free_vmap_cache) {
  479. if (va->va_end < cached_vstart) {
  480. free_vmap_cache = NULL;
  481. } else {
  482. struct vmap_area *cache;
  483. cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
  484. if (va->va_start <= cache->va_start) {
  485. free_vmap_cache = rb_prev(&va->rb_node);
  486. /*
  487. * We don't try to update cached_hole_size or
  488. * cached_align, but it won't go very wrong.
  489. */
  490. }
  491. }
  492. }
  493. rb_erase(&va->rb_node, &vmap_area_root);
  494. RB_CLEAR_NODE(&va->rb_node);
  495. list_del_rcu(&va->list);
  496. /*
  497. * Track the highest possible candidate for pcpu area
  498. * allocation. Areas outside of vmalloc area can be returned
  499. * here too, consider only end addresses which fall inside
  500. * vmalloc area proper.
  501. */
  502. if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
  503. vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
  504. kfree_rcu(va, rcu_head);
  505. }
  506. /*
  507. * Free a region of KVA allocated by alloc_vmap_area
  508. */
  509. static void free_vmap_area(struct vmap_area *va)
  510. {
  511. spin_lock(&vmap_area_lock);
  512. __free_vmap_area(va);
  513. spin_unlock(&vmap_area_lock);
  514. }
  515. /*
  516. * Clear the pagetable entries of a given vmap_area
  517. */
  518. static void unmap_vmap_area(struct vmap_area *va)
  519. {
  520. vunmap_page_range(va->va_start, va->va_end);
  521. }
  522. static void vmap_debug_free_range(unsigned long start, unsigned long end)
  523. {
  524. /*
  525. * Unmap page tables and force a TLB flush immediately if pagealloc
  526. * debugging is enabled. This catches use after free bugs similarly to
  527. * those in linear kernel virtual address space after a page has been
  528. * freed.
  529. *
  530. * All the lazy freeing logic is still retained, in order to minimise
  531. * intrusiveness of this debugging feature.
  532. *
  533. * This is going to be *slow* (linear kernel virtual address debugging
  534. * doesn't do a broadcast TLB flush so it is a lot faster).
  535. */
  536. if (debug_pagealloc_enabled()) {
  537. vunmap_page_range(start, end);
  538. flush_tlb_kernel_range(start, end);
  539. }
  540. }
  541. /*
  542. * lazy_max_pages is the maximum amount of virtual address space we gather up
  543. * before attempting to purge with a TLB flush.
  544. *
  545. * There is a tradeoff here: a larger number will cover more kernel page tables
  546. * and take slightly longer to purge, but it will linearly reduce the number of
  547. * global TLB flushes that must be performed. It would seem natural to scale
  548. * this number up linearly with the number of CPUs (because vmapping activity
  549. * could also scale linearly with the number of CPUs), however it is likely
  550. * that in practice, workloads might be constrained in other ways that mean
  551. * vmap activity will not scale linearly with CPUs. Also, I want to be
  552. * conservative and not introduce a big latency on huge systems, so go with
  553. * a less aggressive log scale. It will still be an improvement over the old
  554. * code, and it will be simple to change the scale factor if we find that it
  555. * becomes a problem on bigger systems.
  556. */
  557. static unsigned long lazy_max_pages(void)
  558. {
  559. unsigned int log;
  560. log = fls(num_online_cpus());
  561. return log * (32UL * 1024 * 1024 / PAGE_SIZE);
  562. }
  563. static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
  564. /*
  565. * Serialize vmap purging. There is no actual criticial section protected
  566. * by this look, but we want to avoid concurrent calls for performance
  567. * reasons and to make the pcpu_get_vm_areas more deterministic.
  568. */
  569. static DEFINE_MUTEX(vmap_purge_lock);
  570. /* for per-CPU blocks */
  571. static void purge_fragmented_blocks_allcpus(void);
  572. /*
  573. * called before a call to iounmap() if the caller wants vm_area_struct's
  574. * immediately freed.
  575. */
  576. void set_iounmap_nonlazy(void)
  577. {
  578. atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
  579. }
  580. /*
  581. * Purges all lazily-freed vmap areas.
  582. */
  583. static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
  584. {
  585. struct llist_node *valist;
  586. struct vmap_area *va;
  587. struct vmap_area *n_va;
  588. bool do_free = false;
  589. lockdep_assert_held(&vmap_purge_lock);
  590. valist = llist_del_all(&vmap_purge_list);
  591. llist_for_each_entry(va, valist, purge_list) {
  592. if (va->va_start < start)
  593. start = va->va_start;
  594. if (va->va_end > end)
  595. end = va->va_end;
  596. do_free = true;
  597. }
  598. if (!do_free)
  599. return false;
  600. flush_tlb_kernel_range(start, end);
  601. spin_lock(&vmap_area_lock);
  602. llist_for_each_entry_safe(va, n_va, valist, purge_list) {
  603. int nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
  604. __free_vmap_area(va);
  605. atomic_sub(nr, &vmap_lazy_nr);
  606. cond_resched_lock(&vmap_area_lock);
  607. }
  608. spin_unlock(&vmap_area_lock);
  609. return true;
  610. }
  611. /*
  612. * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
  613. * is already purging.
  614. */
  615. static void try_purge_vmap_area_lazy(void)
  616. {
  617. if (mutex_trylock(&vmap_purge_lock)) {
  618. __purge_vmap_area_lazy(ULONG_MAX, 0);
  619. mutex_unlock(&vmap_purge_lock);
  620. }
  621. }
  622. /*
  623. * Kick off a purge of the outstanding lazy areas.
  624. */
  625. static void purge_vmap_area_lazy(void)
  626. {
  627. mutex_lock(&vmap_purge_lock);
  628. purge_fragmented_blocks_allcpus();
  629. __purge_vmap_area_lazy(ULONG_MAX, 0);
  630. mutex_unlock(&vmap_purge_lock);
  631. }
  632. /*
  633. * Free a vmap area, caller ensuring that the area has been unmapped
  634. * and flush_cache_vunmap had been called for the correct range
  635. * previously.
  636. */
  637. static void free_vmap_area_noflush(struct vmap_area *va)
  638. {
  639. int nr_lazy;
  640. nr_lazy = atomic_add_return((va->va_end - va->va_start) >> PAGE_SHIFT,
  641. &vmap_lazy_nr);
  642. /* After this point, we may free va at any time */
  643. llist_add(&va->purge_list, &vmap_purge_list);
  644. if (unlikely(nr_lazy > lazy_max_pages()))
  645. try_purge_vmap_area_lazy();
  646. }
  647. /*
  648. * Free and unmap a vmap area
  649. */
  650. static void free_unmap_vmap_area(struct vmap_area *va)
  651. {
  652. flush_cache_vunmap(va->va_start, va->va_end);
  653. unmap_vmap_area(va);
  654. free_vmap_area_noflush(va);
  655. }
  656. static struct vmap_area *find_vmap_area(unsigned long addr)
  657. {
  658. struct vmap_area *va;
  659. spin_lock(&vmap_area_lock);
  660. va = __find_vmap_area(addr);
  661. spin_unlock(&vmap_area_lock);
  662. return va;
  663. }
  664. /*** Per cpu kva allocator ***/
  665. /*
  666. * vmap space is limited especially on 32 bit architectures. Ensure there is
  667. * room for at least 16 percpu vmap blocks per CPU.
  668. */
  669. /*
  670. * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
  671. * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
  672. * instead (we just need a rough idea)
  673. */
  674. #if BITS_PER_LONG == 32
  675. #define VMALLOC_SPACE (128UL*1024*1024)
  676. #else
  677. #define VMALLOC_SPACE (128UL*1024*1024*1024)
  678. #endif
  679. #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
  680. #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
  681. #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
  682. #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
  683. #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
  684. #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
  685. #define VMAP_BBMAP_BITS \
  686. VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
  687. VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
  688. VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
  689. #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
  690. static bool vmap_initialized __read_mostly = false;
  691. struct vmap_block_queue {
  692. spinlock_t lock;
  693. struct list_head free;
  694. };
  695. struct vmap_block {
  696. spinlock_t lock;
  697. struct vmap_area *va;
  698. unsigned long free, dirty;
  699. unsigned long dirty_min, dirty_max; /*< dirty range */
  700. struct list_head free_list;
  701. struct rcu_head rcu_head;
  702. struct list_head purge;
  703. };
  704. /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
  705. static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
  706. /*
  707. * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
  708. * in the free path. Could get rid of this if we change the API to return a
  709. * "cookie" from alloc, to be passed to free. But no big deal yet.
  710. */
  711. static DEFINE_SPINLOCK(vmap_block_tree_lock);
  712. static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
  713. /*
  714. * We should probably have a fallback mechanism to allocate virtual memory
  715. * out of partially filled vmap blocks. However vmap block sizing should be
  716. * fairly reasonable according to the vmalloc size, so it shouldn't be a
  717. * big problem.
  718. */
  719. static unsigned long addr_to_vb_idx(unsigned long addr)
  720. {
  721. addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
  722. addr /= VMAP_BLOCK_SIZE;
  723. return addr;
  724. }
  725. static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
  726. {
  727. unsigned long addr;
  728. addr = va_start + (pages_off << PAGE_SHIFT);
  729. BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
  730. return (void *)addr;
  731. }
  732. /**
  733. * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
  734. * block. Of course pages number can't exceed VMAP_BBMAP_BITS
  735. * @order: how many 2^order pages should be occupied in newly allocated block
  736. * @gfp_mask: flags for the page level allocator
  737. *
  738. * Returns: virtual address in a newly allocated block or ERR_PTR(-errno)
  739. */
  740. static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
  741. {
  742. struct vmap_block_queue *vbq;
  743. struct vmap_block *vb;
  744. struct vmap_area *va;
  745. unsigned long vb_idx;
  746. int node, err;
  747. void *vaddr;
  748. node = numa_node_id();
  749. vb = kmalloc_node(sizeof(struct vmap_block),
  750. gfp_mask & GFP_RECLAIM_MASK, node);
  751. if (unlikely(!vb))
  752. return ERR_PTR(-ENOMEM);
  753. va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
  754. VMALLOC_START, VMALLOC_END,
  755. node, gfp_mask);
  756. if (IS_ERR(va)) {
  757. kfree(vb);
  758. return ERR_CAST(va);
  759. }
  760. err = radix_tree_preload(gfp_mask);
  761. if (unlikely(err)) {
  762. kfree(vb);
  763. free_vmap_area(va);
  764. return ERR_PTR(err);
  765. }
  766. vaddr = vmap_block_vaddr(va->va_start, 0);
  767. spin_lock_init(&vb->lock);
  768. vb->va = va;
  769. /* At least something should be left free */
  770. BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
  771. vb->free = VMAP_BBMAP_BITS - (1UL << order);
  772. vb->dirty = 0;
  773. vb->dirty_min = VMAP_BBMAP_BITS;
  774. vb->dirty_max = 0;
  775. INIT_LIST_HEAD(&vb->free_list);
  776. vb_idx = addr_to_vb_idx(va->va_start);
  777. spin_lock(&vmap_block_tree_lock);
  778. err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
  779. spin_unlock(&vmap_block_tree_lock);
  780. BUG_ON(err);
  781. radix_tree_preload_end();
  782. vbq = &get_cpu_var(vmap_block_queue);
  783. spin_lock(&vbq->lock);
  784. list_add_tail_rcu(&vb->free_list, &vbq->free);
  785. spin_unlock(&vbq->lock);
  786. put_cpu_var(vmap_block_queue);
  787. return vaddr;
  788. }
  789. static void free_vmap_block(struct vmap_block *vb)
  790. {
  791. struct vmap_block *tmp;
  792. unsigned long vb_idx;
  793. vb_idx = addr_to_vb_idx(vb->va->va_start);
  794. spin_lock(&vmap_block_tree_lock);
  795. tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
  796. spin_unlock(&vmap_block_tree_lock);
  797. BUG_ON(tmp != vb);
  798. free_vmap_area_noflush(vb->va);
  799. kfree_rcu(vb, rcu_head);
  800. }
  801. static void purge_fragmented_blocks(int cpu)
  802. {
  803. LIST_HEAD(purge);
  804. struct vmap_block *vb;
  805. struct vmap_block *n_vb;
  806. struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
  807. rcu_read_lock();
  808. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  809. if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
  810. continue;
  811. spin_lock(&vb->lock);
  812. if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
  813. vb->free = 0; /* prevent further allocs after releasing lock */
  814. vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
  815. vb->dirty_min = 0;
  816. vb->dirty_max = VMAP_BBMAP_BITS;
  817. spin_lock(&vbq->lock);
  818. list_del_rcu(&vb->free_list);
  819. spin_unlock(&vbq->lock);
  820. spin_unlock(&vb->lock);
  821. list_add_tail(&vb->purge, &purge);
  822. } else
  823. spin_unlock(&vb->lock);
  824. }
  825. rcu_read_unlock();
  826. list_for_each_entry_safe(vb, n_vb, &purge, purge) {
  827. list_del(&vb->purge);
  828. free_vmap_block(vb);
  829. }
  830. }
  831. static void purge_fragmented_blocks_allcpus(void)
  832. {
  833. int cpu;
  834. for_each_possible_cpu(cpu)
  835. purge_fragmented_blocks(cpu);
  836. }
  837. static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  838. {
  839. struct vmap_block_queue *vbq;
  840. struct vmap_block *vb;
  841. void *vaddr = NULL;
  842. unsigned int order;
  843. BUG_ON(offset_in_page(size));
  844. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  845. if (WARN_ON(size == 0)) {
  846. /*
  847. * Allocating 0 bytes isn't what caller wants since
  848. * get_order(0) returns funny result. Just warn and terminate
  849. * early.
  850. */
  851. return NULL;
  852. }
  853. order = get_order(size);
  854. rcu_read_lock();
  855. vbq = &get_cpu_var(vmap_block_queue);
  856. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  857. unsigned long pages_off;
  858. spin_lock(&vb->lock);
  859. if (vb->free < (1UL << order)) {
  860. spin_unlock(&vb->lock);
  861. continue;
  862. }
  863. pages_off = VMAP_BBMAP_BITS - vb->free;
  864. vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
  865. vb->free -= 1UL << order;
  866. if (vb->free == 0) {
  867. spin_lock(&vbq->lock);
  868. list_del_rcu(&vb->free_list);
  869. spin_unlock(&vbq->lock);
  870. }
  871. spin_unlock(&vb->lock);
  872. break;
  873. }
  874. put_cpu_var(vmap_block_queue);
  875. rcu_read_unlock();
  876. /* Allocate new block if nothing was found */
  877. if (!vaddr)
  878. vaddr = new_vmap_block(order, gfp_mask);
  879. return vaddr;
  880. }
  881. static void vb_free(const void *addr, unsigned long size)
  882. {
  883. unsigned long offset;
  884. unsigned long vb_idx;
  885. unsigned int order;
  886. struct vmap_block *vb;
  887. BUG_ON(offset_in_page(size));
  888. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  889. flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
  890. order = get_order(size);
  891. offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
  892. offset >>= PAGE_SHIFT;
  893. vb_idx = addr_to_vb_idx((unsigned long)addr);
  894. rcu_read_lock();
  895. vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
  896. rcu_read_unlock();
  897. BUG_ON(!vb);
  898. vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
  899. spin_lock(&vb->lock);
  900. /* Expand dirty range */
  901. vb->dirty_min = min(vb->dirty_min, offset);
  902. vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
  903. vb->dirty += 1UL << order;
  904. if (vb->dirty == VMAP_BBMAP_BITS) {
  905. BUG_ON(vb->free);
  906. spin_unlock(&vb->lock);
  907. free_vmap_block(vb);
  908. } else
  909. spin_unlock(&vb->lock);
  910. }
  911. /**
  912. * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
  913. *
  914. * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
  915. * to amortize TLB flushing overheads. What this means is that any page you
  916. * have now, may, in a former life, have been mapped into kernel virtual
  917. * address by the vmap layer and so there might be some CPUs with TLB entries
  918. * still referencing that page (additional to the regular 1:1 kernel mapping).
  919. *
  920. * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
  921. * be sure that none of the pages we have control over will have any aliases
  922. * from the vmap layer.
  923. */
  924. void vm_unmap_aliases(void)
  925. {
  926. unsigned long start = ULONG_MAX, end = 0;
  927. int cpu;
  928. int flush = 0;
  929. if (unlikely(!vmap_initialized))
  930. return;
  931. might_sleep();
  932. for_each_possible_cpu(cpu) {
  933. struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
  934. struct vmap_block *vb;
  935. rcu_read_lock();
  936. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  937. spin_lock(&vb->lock);
  938. if (vb->dirty) {
  939. unsigned long va_start = vb->va->va_start;
  940. unsigned long s, e;
  941. s = va_start + (vb->dirty_min << PAGE_SHIFT);
  942. e = va_start + (vb->dirty_max << PAGE_SHIFT);
  943. start = min(s, start);
  944. end = max(e, end);
  945. flush = 1;
  946. }
  947. spin_unlock(&vb->lock);
  948. }
  949. rcu_read_unlock();
  950. }
  951. mutex_lock(&vmap_purge_lock);
  952. purge_fragmented_blocks_allcpus();
  953. if (!__purge_vmap_area_lazy(start, end) && flush)
  954. flush_tlb_kernel_range(start, end);
  955. mutex_unlock(&vmap_purge_lock);
  956. }
  957. EXPORT_SYMBOL_GPL(vm_unmap_aliases);
  958. /**
  959. * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
  960. * @mem: the pointer returned by vm_map_ram
  961. * @count: the count passed to that vm_map_ram call (cannot unmap partial)
  962. */
  963. void vm_unmap_ram(const void *mem, unsigned int count)
  964. {
  965. unsigned long size = (unsigned long)count << PAGE_SHIFT;
  966. unsigned long addr = (unsigned long)mem;
  967. struct vmap_area *va;
  968. might_sleep();
  969. BUG_ON(!addr);
  970. BUG_ON(addr < VMALLOC_START);
  971. BUG_ON(addr > VMALLOC_END);
  972. BUG_ON(!PAGE_ALIGNED(addr));
  973. debug_check_no_locks_freed(mem, size);
  974. vmap_debug_free_range(addr, addr+size);
  975. if (likely(count <= VMAP_MAX_ALLOC)) {
  976. vb_free(mem, size);
  977. return;
  978. }
  979. va = find_vmap_area(addr);
  980. BUG_ON(!va);
  981. free_unmap_vmap_area(va);
  982. }
  983. EXPORT_SYMBOL(vm_unmap_ram);
  984. /**
  985. * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
  986. * @pages: an array of pointers to the pages to be mapped
  987. * @count: number of pages
  988. * @node: prefer to allocate data structures on this node
  989. * @prot: memory protection to use. PAGE_KERNEL for regular RAM
  990. *
  991. * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
  992. * faster than vmap so it's good. But if you mix long-life and short-life
  993. * objects with vm_map_ram(), it could consume lots of address space through
  994. * fragmentation (especially on a 32bit machine). You could see failures in
  995. * the end. Please use this function for short-lived objects.
  996. *
  997. * Returns: a pointer to the address that has been mapped, or %NULL on failure
  998. */
  999. void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
  1000. {
  1001. unsigned long size = (unsigned long)count << PAGE_SHIFT;
  1002. unsigned long addr;
  1003. void *mem;
  1004. if (likely(count <= VMAP_MAX_ALLOC)) {
  1005. mem = vb_alloc(size, GFP_KERNEL);
  1006. if (IS_ERR(mem))
  1007. return NULL;
  1008. addr = (unsigned long)mem;
  1009. } else {
  1010. struct vmap_area *va;
  1011. va = alloc_vmap_area(size, PAGE_SIZE,
  1012. VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
  1013. if (IS_ERR(va))
  1014. return NULL;
  1015. addr = va->va_start;
  1016. mem = (void *)addr;
  1017. }
  1018. if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
  1019. vm_unmap_ram(mem, count);
  1020. return NULL;
  1021. }
  1022. return mem;
  1023. }
  1024. EXPORT_SYMBOL(vm_map_ram);
  1025. static struct vm_struct *vmlist __initdata;
  1026. /**
  1027. * vm_area_add_early - add vmap area early during boot
  1028. * @vm: vm_struct to add
  1029. *
  1030. * This function is used to add fixed kernel vm area to vmlist before
  1031. * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
  1032. * should contain proper values and the other fields should be zero.
  1033. *
  1034. * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
  1035. */
  1036. void __init vm_area_add_early(struct vm_struct *vm)
  1037. {
  1038. struct vm_struct *tmp, **p;
  1039. BUG_ON(vmap_initialized);
  1040. for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
  1041. if (tmp->addr >= vm->addr) {
  1042. BUG_ON(tmp->addr < vm->addr + vm->size);
  1043. break;
  1044. } else
  1045. BUG_ON(tmp->addr + tmp->size > vm->addr);
  1046. }
  1047. vm->next = *p;
  1048. *p = vm;
  1049. }
  1050. /**
  1051. * vm_area_register_early - register vmap area early during boot
  1052. * @vm: vm_struct to register
  1053. * @align: requested alignment
  1054. *
  1055. * This function is used to register kernel vm area before
  1056. * vmalloc_init() is called. @vm->size and @vm->flags should contain
  1057. * proper values on entry and other fields should be zero. On return,
  1058. * vm->addr contains the allocated address.
  1059. *
  1060. * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
  1061. */
  1062. void __init vm_area_register_early(struct vm_struct *vm, size_t align)
  1063. {
  1064. static size_t vm_init_off __initdata;
  1065. unsigned long addr;
  1066. addr = ALIGN(VMALLOC_START + vm_init_off, align);
  1067. vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
  1068. vm->addr = (void *)addr;
  1069. vm_area_add_early(vm);
  1070. }
  1071. void __init vmalloc_init(void)
  1072. {
  1073. struct vmap_area *va;
  1074. struct vm_struct *tmp;
  1075. int i;
  1076. for_each_possible_cpu(i) {
  1077. struct vmap_block_queue *vbq;
  1078. struct vfree_deferred *p;
  1079. vbq = &per_cpu(vmap_block_queue, i);
  1080. spin_lock_init(&vbq->lock);
  1081. INIT_LIST_HEAD(&vbq->free);
  1082. p = &per_cpu(vfree_deferred, i);
  1083. init_llist_head(&p->list);
  1084. INIT_WORK(&p->wq, free_work);
  1085. }
  1086. /* Import existing vmlist entries. */
  1087. for (tmp = vmlist; tmp; tmp = tmp->next) {
  1088. va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
  1089. va->flags = VM_VM_AREA;
  1090. va->va_start = (unsigned long)tmp->addr;
  1091. va->va_end = va->va_start + tmp->size;
  1092. va->vm = tmp;
  1093. __insert_vmap_area(va);
  1094. }
  1095. vmap_area_pcpu_hole = VMALLOC_END;
  1096. vmap_initialized = true;
  1097. }
  1098. /**
  1099. * map_kernel_range_noflush - map kernel VM area with the specified pages
  1100. * @addr: start of the VM area to map
  1101. * @size: size of the VM area to map
  1102. * @prot: page protection flags to use
  1103. * @pages: pages to map
  1104. *
  1105. * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
  1106. * specify should have been allocated using get_vm_area() and its
  1107. * friends.
  1108. *
  1109. * NOTE:
  1110. * This function does NOT do any cache flushing. The caller is
  1111. * responsible for calling flush_cache_vmap() on to-be-mapped areas
  1112. * before calling this function.
  1113. *
  1114. * RETURNS:
  1115. * The number of pages mapped on success, -errno on failure.
  1116. */
  1117. int map_kernel_range_noflush(unsigned long addr, unsigned long size,
  1118. pgprot_t prot, struct page **pages)
  1119. {
  1120. return vmap_page_range_noflush(addr, addr + size, prot, pages);
  1121. }
  1122. /**
  1123. * unmap_kernel_range_noflush - unmap kernel VM area
  1124. * @addr: start of the VM area to unmap
  1125. * @size: size of the VM area to unmap
  1126. *
  1127. * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
  1128. * specify should have been allocated using get_vm_area() and its
  1129. * friends.
  1130. *
  1131. * NOTE:
  1132. * This function does NOT do any cache flushing. The caller is
  1133. * responsible for calling flush_cache_vunmap() on to-be-mapped areas
  1134. * before calling this function and flush_tlb_kernel_range() after.
  1135. */
  1136. void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
  1137. {
  1138. vunmap_page_range(addr, addr + size);
  1139. }
  1140. EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
  1141. /**
  1142. * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
  1143. * @addr: start of the VM area to unmap
  1144. * @size: size of the VM area to unmap
  1145. *
  1146. * Similar to unmap_kernel_range_noflush() but flushes vcache before
  1147. * the unmapping and tlb after.
  1148. */
  1149. void unmap_kernel_range(unsigned long addr, unsigned long size)
  1150. {
  1151. unsigned long end = addr + size;
  1152. flush_cache_vunmap(addr, end);
  1153. vunmap_page_range(addr, end);
  1154. flush_tlb_kernel_range(addr, end);
  1155. }
  1156. EXPORT_SYMBOL_GPL(unmap_kernel_range);
  1157. int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages)
  1158. {
  1159. unsigned long addr = (unsigned long)area->addr;
  1160. unsigned long end = addr + get_vm_area_size(area);
  1161. int err;
  1162. err = vmap_page_range(addr, end, prot, pages);
  1163. return err > 0 ? 0 : err;
  1164. }
  1165. EXPORT_SYMBOL_GPL(map_vm_area);
  1166. static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
  1167. unsigned long flags, const void *caller)
  1168. {
  1169. spin_lock(&vmap_area_lock);
  1170. vm->flags = flags;
  1171. vm->addr = (void *)va->va_start;
  1172. vm->size = va->va_end - va->va_start;
  1173. vm->caller = caller;
  1174. va->vm = vm;
  1175. va->flags |= VM_VM_AREA;
  1176. spin_unlock(&vmap_area_lock);
  1177. }
  1178. static void clear_vm_uninitialized_flag(struct vm_struct *vm)
  1179. {
  1180. /*
  1181. * Before removing VM_UNINITIALIZED,
  1182. * we should make sure that vm has proper values.
  1183. * Pair with smp_rmb() in show_numa_info().
  1184. */
  1185. smp_wmb();
  1186. vm->flags &= ~VM_UNINITIALIZED;
  1187. }
  1188. static struct vm_struct *__get_vm_area_node(unsigned long size,
  1189. unsigned long align, unsigned long flags, unsigned long start,
  1190. unsigned long end, int node, gfp_t gfp_mask, const void *caller)
  1191. {
  1192. struct vmap_area *va;
  1193. struct vm_struct *area;
  1194. BUG_ON(in_interrupt());
  1195. size = PAGE_ALIGN(size);
  1196. if (unlikely(!size))
  1197. return NULL;
  1198. if (flags & VM_IOREMAP)
  1199. align = 1ul << clamp_t(int, get_count_order_long(size),
  1200. PAGE_SHIFT, IOREMAP_MAX_ORDER);
  1201. area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
  1202. if (unlikely(!area))
  1203. return NULL;
  1204. if (!(flags & VM_NO_GUARD))
  1205. size += PAGE_SIZE;
  1206. va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
  1207. if (IS_ERR(va)) {
  1208. kfree(area);
  1209. return NULL;
  1210. }
  1211. setup_vmalloc_vm(area, va, flags, caller);
  1212. return area;
  1213. }
  1214. struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
  1215. unsigned long start, unsigned long end)
  1216. {
  1217. return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
  1218. GFP_KERNEL, __builtin_return_address(0));
  1219. }
  1220. EXPORT_SYMBOL_GPL(__get_vm_area);
  1221. struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
  1222. unsigned long start, unsigned long end,
  1223. const void *caller)
  1224. {
  1225. return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
  1226. GFP_KERNEL, caller);
  1227. }
  1228. /**
  1229. * get_vm_area - reserve a contiguous kernel virtual area
  1230. * @size: size of the area
  1231. * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
  1232. *
  1233. * Search an area of @size in the kernel virtual mapping area,
  1234. * and reserved it for out purposes. Returns the area descriptor
  1235. * on success or %NULL on failure.
  1236. */
  1237. struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
  1238. {
  1239. return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
  1240. NUMA_NO_NODE, GFP_KERNEL,
  1241. __builtin_return_address(0));
  1242. }
  1243. struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
  1244. const void *caller)
  1245. {
  1246. return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
  1247. NUMA_NO_NODE, GFP_KERNEL, caller);
  1248. }
  1249. /**
  1250. * find_vm_area - find a continuous kernel virtual area
  1251. * @addr: base address
  1252. *
  1253. * Search for the kernel VM area starting at @addr, and return it.
  1254. * It is up to the caller to do all required locking to keep the returned
  1255. * pointer valid.
  1256. */
  1257. struct vm_struct *find_vm_area(const void *addr)
  1258. {
  1259. struct vmap_area *va;
  1260. va = find_vmap_area((unsigned long)addr);
  1261. if (va && va->flags & VM_VM_AREA)
  1262. return va->vm;
  1263. return NULL;
  1264. }
  1265. /**
  1266. * remove_vm_area - find and remove a continuous kernel virtual area
  1267. * @addr: base address
  1268. *
  1269. * Search for the kernel VM area starting at @addr, and remove it.
  1270. * This function returns the found VM area, but using it is NOT safe
  1271. * on SMP machines, except for its size or flags.
  1272. */
  1273. struct vm_struct *remove_vm_area(const void *addr)
  1274. {
  1275. struct vmap_area *va;
  1276. might_sleep();
  1277. va = find_vmap_area((unsigned long)addr);
  1278. if (va && va->flags & VM_VM_AREA) {
  1279. struct vm_struct *vm = va->vm;
  1280. spin_lock(&vmap_area_lock);
  1281. va->vm = NULL;
  1282. va->flags &= ~VM_VM_AREA;
  1283. va->flags |= VM_LAZY_FREE;
  1284. spin_unlock(&vmap_area_lock);
  1285. vmap_debug_free_range(va->va_start, va->va_end);
  1286. kasan_free_shadow(vm);
  1287. free_unmap_vmap_area(va);
  1288. return vm;
  1289. }
  1290. return NULL;
  1291. }
  1292. static void __vunmap(const void *addr, int deallocate_pages)
  1293. {
  1294. struct vm_struct *area;
  1295. if (!addr)
  1296. return;
  1297. if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
  1298. addr))
  1299. return;
  1300. area = remove_vm_area(addr);
  1301. if (unlikely(!area)) {
  1302. WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
  1303. addr);
  1304. return;
  1305. }
  1306. debug_check_no_locks_freed(addr, get_vm_area_size(area));
  1307. debug_check_no_obj_freed(addr, get_vm_area_size(area));
  1308. if (deallocate_pages) {
  1309. int i;
  1310. for (i = 0; i < area->nr_pages; i++) {
  1311. struct page *page = area->pages[i];
  1312. BUG_ON(!page);
  1313. __free_pages(page, 0);
  1314. }
  1315. kvfree(area->pages);
  1316. }
  1317. kfree(area);
  1318. return;
  1319. }
  1320. static inline void __vfree_deferred(const void *addr)
  1321. {
  1322. /*
  1323. * Use raw_cpu_ptr() because this can be called from preemptible
  1324. * context. Preemption is absolutely fine here, because the llist_add()
  1325. * implementation is lockless, so it works even if we are adding to
  1326. * nother cpu's list. schedule_work() should be fine with this too.
  1327. */
  1328. struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
  1329. if (llist_add((struct llist_node *)addr, &p->list))
  1330. schedule_work(&p->wq);
  1331. }
  1332. /**
  1333. * vfree_atomic - release memory allocated by vmalloc()
  1334. * @addr: memory base address
  1335. *
  1336. * This one is just like vfree() but can be called in any atomic context
  1337. * except NMIs.
  1338. */
  1339. void vfree_atomic(const void *addr)
  1340. {
  1341. BUG_ON(in_nmi());
  1342. kmemleak_free(addr);
  1343. if (!addr)
  1344. return;
  1345. __vfree_deferred(addr);
  1346. }
  1347. /**
  1348. * vfree - release memory allocated by vmalloc()
  1349. * @addr: memory base address
  1350. *
  1351. * Free the virtually continuous memory area starting at @addr, as
  1352. * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
  1353. * NULL, no operation is performed.
  1354. *
  1355. * Must not be called in NMI context (strictly speaking, only if we don't
  1356. * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
  1357. * conventions for vfree() arch-depenedent would be a really bad idea)
  1358. *
  1359. * NOTE: assumes that the object at @addr has a size >= sizeof(llist_node)
  1360. */
  1361. void vfree(const void *addr)
  1362. {
  1363. BUG_ON(in_nmi());
  1364. kmemleak_free(addr);
  1365. if (!addr)
  1366. return;
  1367. if (unlikely(in_interrupt()))
  1368. __vfree_deferred(addr);
  1369. else
  1370. __vunmap(addr, 1);
  1371. }
  1372. EXPORT_SYMBOL(vfree);
  1373. /**
  1374. * vunmap - release virtual mapping obtained by vmap()
  1375. * @addr: memory base address
  1376. *
  1377. * Free the virtually contiguous memory area starting at @addr,
  1378. * which was created from the page array passed to vmap().
  1379. *
  1380. * Must not be called in interrupt context.
  1381. */
  1382. void vunmap(const void *addr)
  1383. {
  1384. BUG_ON(in_interrupt());
  1385. might_sleep();
  1386. if (addr)
  1387. __vunmap(addr, 0);
  1388. }
  1389. EXPORT_SYMBOL(vunmap);
  1390. /**
  1391. * vmap - map an array of pages into virtually contiguous space
  1392. * @pages: array of page pointers
  1393. * @count: number of pages to map
  1394. * @flags: vm_area->flags
  1395. * @prot: page protection for the mapping
  1396. *
  1397. * Maps @count pages from @pages into contiguous kernel virtual
  1398. * space.
  1399. */
  1400. void *vmap(struct page **pages, unsigned int count,
  1401. unsigned long flags, pgprot_t prot)
  1402. {
  1403. struct vm_struct *area;
  1404. unsigned long size; /* In bytes */
  1405. might_sleep();
  1406. if (count > totalram_pages)
  1407. return NULL;
  1408. size = (unsigned long)count << PAGE_SHIFT;
  1409. area = get_vm_area_caller(size, flags, __builtin_return_address(0));
  1410. if (!area)
  1411. return NULL;
  1412. if (map_vm_area(area, prot, pages)) {
  1413. vunmap(area->addr);
  1414. return NULL;
  1415. }
  1416. return area->addr;
  1417. }
  1418. EXPORT_SYMBOL(vmap);
  1419. static void *__vmalloc_node(unsigned long size, unsigned long align,
  1420. gfp_t gfp_mask, pgprot_t prot,
  1421. int node, const void *caller);
  1422. static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  1423. pgprot_t prot, int node)
  1424. {
  1425. struct page **pages;
  1426. unsigned int nr_pages, array_size, i;
  1427. const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
  1428. const gfp_t alloc_mask = gfp_mask | __GFP_HIGHMEM | __GFP_NOWARN;
  1429. nr_pages = get_vm_area_size(area) >> PAGE_SHIFT;
  1430. array_size = (nr_pages * sizeof(struct page *));
  1431. area->nr_pages = nr_pages;
  1432. /* Please note that the recursion is strictly bounded. */
  1433. if (array_size > PAGE_SIZE) {
  1434. pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,
  1435. PAGE_KERNEL, node, area->caller);
  1436. } else {
  1437. pages = kmalloc_node(array_size, nested_gfp, node);
  1438. }
  1439. area->pages = pages;
  1440. if (!area->pages) {
  1441. remove_vm_area(area->addr);
  1442. kfree(area);
  1443. return NULL;
  1444. }
  1445. for (i = 0; i < area->nr_pages; i++) {
  1446. struct page *page;
  1447. if (fatal_signal_pending(current)) {
  1448. area->nr_pages = i;
  1449. goto fail_no_warn;
  1450. }
  1451. if (node == NUMA_NO_NODE)
  1452. page = alloc_page(alloc_mask);
  1453. else
  1454. page = alloc_pages_node(node, alloc_mask, 0);
  1455. if (unlikely(!page)) {
  1456. /* Successfully allocated i pages, free them in __vunmap() */
  1457. area->nr_pages = i;
  1458. goto fail;
  1459. }
  1460. area->pages[i] = page;
  1461. if (gfpflags_allow_blocking(gfp_mask))
  1462. cond_resched();
  1463. }
  1464. if (map_vm_area(area, prot, pages))
  1465. goto fail;
  1466. return area->addr;
  1467. fail:
  1468. warn_alloc(gfp_mask, NULL,
  1469. "vmalloc: allocation failure, allocated %ld of %ld bytes",
  1470. (area->nr_pages*PAGE_SIZE), area->size);
  1471. fail_no_warn:
  1472. vfree(area->addr);
  1473. return NULL;
  1474. }
  1475. /**
  1476. * __vmalloc_node_range - allocate virtually contiguous memory
  1477. * @size: allocation size
  1478. * @align: desired alignment
  1479. * @start: vm area range start
  1480. * @end: vm area range end
  1481. * @gfp_mask: flags for the page level allocator
  1482. * @prot: protection mask for the allocated pages
  1483. * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD)
  1484. * @node: node to use for allocation or NUMA_NO_NODE
  1485. * @caller: caller's return address
  1486. *
  1487. * Allocate enough pages to cover @size from the page level
  1488. * allocator with @gfp_mask flags. Map them into contiguous
  1489. * kernel virtual space, using a pagetable protection of @prot.
  1490. */
  1491. void *__vmalloc_node_range(unsigned long size, unsigned long align,
  1492. unsigned long start, unsigned long end, gfp_t gfp_mask,
  1493. pgprot_t prot, unsigned long vm_flags, int node,
  1494. const void *caller)
  1495. {
  1496. struct vm_struct *area;
  1497. void *addr;
  1498. unsigned long real_size = size;
  1499. size = PAGE_ALIGN(size);
  1500. if (!size || (size >> PAGE_SHIFT) > totalram_pages)
  1501. goto fail;
  1502. area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED |
  1503. vm_flags, start, end, node, gfp_mask, caller);
  1504. if (!area)
  1505. goto fail;
  1506. addr = __vmalloc_area_node(area, gfp_mask, prot, node);
  1507. if (!addr)
  1508. return NULL;
  1509. /*
  1510. * In this function, newly allocated vm_struct has VM_UNINITIALIZED
  1511. * flag. It means that vm_struct is not fully initialized.
  1512. * Now, it is fully initialized, so remove this flag here.
  1513. */
  1514. clear_vm_uninitialized_flag(area);
  1515. kmemleak_vmalloc(area, size, gfp_mask);
  1516. return addr;
  1517. fail:
  1518. warn_alloc(gfp_mask, NULL,
  1519. "vmalloc: allocation failure: %lu bytes", real_size);
  1520. return NULL;
  1521. }
  1522. /**
  1523. * __vmalloc_node - allocate virtually contiguous memory
  1524. * @size: allocation size
  1525. * @align: desired alignment
  1526. * @gfp_mask: flags for the page level allocator
  1527. * @prot: protection mask for the allocated pages
  1528. * @node: node to use for allocation or NUMA_NO_NODE
  1529. * @caller: caller's return address
  1530. *
  1531. * Allocate enough pages to cover @size from the page level
  1532. * allocator with @gfp_mask flags. Map them into contiguous
  1533. * kernel virtual space, using a pagetable protection of @prot.
  1534. *
  1535. * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
  1536. * and __GFP_NOFAIL are not supported
  1537. *
  1538. * Any use of gfp flags outside of GFP_KERNEL should be consulted
  1539. * with mm people.
  1540. *
  1541. */
  1542. static void *__vmalloc_node(unsigned long size, unsigned long align,
  1543. gfp_t gfp_mask, pgprot_t prot,
  1544. int node, const void *caller)
  1545. {
  1546. return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
  1547. gfp_mask, prot, 0, node, caller);
  1548. }
  1549. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  1550. {
  1551. return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE,
  1552. __builtin_return_address(0));
  1553. }
  1554. EXPORT_SYMBOL(__vmalloc);
  1555. static inline void *__vmalloc_node_flags(unsigned long size,
  1556. int node, gfp_t flags)
  1557. {
  1558. return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
  1559. node, __builtin_return_address(0));
  1560. }
  1561. void *__vmalloc_node_flags_caller(unsigned long size, int node, gfp_t flags,
  1562. void *caller)
  1563. {
  1564. return __vmalloc_node(size, 1, flags, PAGE_KERNEL, node, caller);
  1565. }
  1566. /**
  1567. * vmalloc - allocate virtually contiguous memory
  1568. * @size: allocation size
  1569. * Allocate enough pages to cover @size from the page level
  1570. * allocator and map them into contiguous kernel virtual space.
  1571. *
  1572. * For tight control over page level allocator and protection flags
  1573. * use __vmalloc() instead.
  1574. */
  1575. void *vmalloc(unsigned long size)
  1576. {
  1577. return __vmalloc_node_flags(size, NUMA_NO_NODE,
  1578. GFP_KERNEL);
  1579. }
  1580. EXPORT_SYMBOL(vmalloc);
  1581. /**
  1582. * vzalloc - allocate virtually contiguous memory with zero fill
  1583. * @size: allocation size
  1584. * Allocate enough pages to cover @size from the page level
  1585. * allocator and map them into contiguous kernel virtual space.
  1586. * The memory allocated is set to zero.
  1587. *
  1588. * For tight control over page level allocator and protection flags
  1589. * use __vmalloc() instead.
  1590. */
  1591. void *vzalloc(unsigned long size)
  1592. {
  1593. return __vmalloc_node_flags(size, NUMA_NO_NODE,
  1594. GFP_KERNEL | __GFP_ZERO);
  1595. }
  1596. EXPORT_SYMBOL(vzalloc);
  1597. /**
  1598. * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
  1599. * @size: allocation size
  1600. *
  1601. * The resulting memory area is zeroed so it can be mapped to userspace
  1602. * without leaking data.
  1603. */
  1604. void *vmalloc_user(unsigned long size)
  1605. {
  1606. struct vm_struct *area;
  1607. void *ret;
  1608. ret = __vmalloc_node(size, SHMLBA,
  1609. GFP_KERNEL | __GFP_ZERO,
  1610. PAGE_KERNEL, NUMA_NO_NODE,
  1611. __builtin_return_address(0));
  1612. if (ret) {
  1613. area = find_vm_area(ret);
  1614. area->flags |= VM_USERMAP;
  1615. }
  1616. return ret;
  1617. }
  1618. EXPORT_SYMBOL(vmalloc_user);
  1619. /**
  1620. * vmalloc_node - allocate memory on a specific node
  1621. * @size: allocation size
  1622. * @node: numa node
  1623. *
  1624. * Allocate enough pages to cover @size from the page level
  1625. * allocator and map them into contiguous kernel virtual space.
  1626. *
  1627. * For tight control over page level allocator and protection flags
  1628. * use __vmalloc() instead.
  1629. */
  1630. void *vmalloc_node(unsigned long size, int node)
  1631. {
  1632. return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL,
  1633. node, __builtin_return_address(0));
  1634. }
  1635. EXPORT_SYMBOL(vmalloc_node);
  1636. /**
  1637. * vzalloc_node - allocate memory on a specific node with zero fill
  1638. * @size: allocation size
  1639. * @node: numa node
  1640. *
  1641. * Allocate enough pages to cover @size from the page level
  1642. * allocator and map them into contiguous kernel virtual space.
  1643. * The memory allocated is set to zero.
  1644. *
  1645. * For tight control over page level allocator and protection flags
  1646. * use __vmalloc_node() instead.
  1647. */
  1648. void *vzalloc_node(unsigned long size, int node)
  1649. {
  1650. return __vmalloc_node_flags(size, node,
  1651. GFP_KERNEL | __GFP_ZERO);
  1652. }
  1653. EXPORT_SYMBOL(vzalloc_node);
  1654. #ifndef PAGE_KERNEL_EXEC
  1655. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  1656. #endif
  1657. /**
  1658. * vmalloc_exec - allocate virtually contiguous, executable memory
  1659. * @size: allocation size
  1660. *
  1661. * Kernel-internal function to allocate enough pages to cover @size
  1662. * the page level allocator and map them into contiguous and
  1663. * executable kernel virtual space.
  1664. *
  1665. * For tight control over page level allocator and protection flags
  1666. * use __vmalloc() instead.
  1667. */
  1668. void *vmalloc_exec(unsigned long size)
  1669. {
  1670. return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL_EXEC,
  1671. NUMA_NO_NODE, __builtin_return_address(0));
  1672. }
  1673. #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
  1674. #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
  1675. #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
  1676. #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
  1677. #else
  1678. #define GFP_VMALLOC32 GFP_KERNEL
  1679. #endif
  1680. /**
  1681. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  1682. * @size: allocation size
  1683. *
  1684. * Allocate enough 32bit PA addressable pages to cover @size from the
  1685. * page level allocator and map them into contiguous kernel virtual space.
  1686. */
  1687. void *vmalloc_32(unsigned long size)
  1688. {
  1689. return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
  1690. NUMA_NO_NODE, __builtin_return_address(0));
  1691. }
  1692. EXPORT_SYMBOL(vmalloc_32);
  1693. /**
  1694. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  1695. * @size: allocation size
  1696. *
  1697. * The resulting memory area is 32bit addressable and zeroed so it can be
  1698. * mapped to userspace without leaking data.
  1699. */
  1700. void *vmalloc_32_user(unsigned long size)
  1701. {
  1702. struct vm_struct *area;
  1703. void *ret;
  1704. ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
  1705. NUMA_NO_NODE, __builtin_return_address(0));
  1706. if (ret) {
  1707. area = find_vm_area(ret);
  1708. area->flags |= VM_USERMAP;
  1709. }
  1710. return ret;
  1711. }
  1712. EXPORT_SYMBOL(vmalloc_32_user);
  1713. /*
  1714. * small helper routine , copy contents to buf from addr.
  1715. * If the page is not present, fill zero.
  1716. */
  1717. static int aligned_vread(char *buf, char *addr, unsigned long count)
  1718. {
  1719. struct page *p;
  1720. int copied = 0;
  1721. while (count) {
  1722. unsigned long offset, length;
  1723. offset = offset_in_page(addr);
  1724. length = PAGE_SIZE - offset;
  1725. if (length > count)
  1726. length = count;
  1727. p = vmalloc_to_page(addr);
  1728. /*
  1729. * To do safe access to this _mapped_ area, we need
  1730. * lock. But adding lock here means that we need to add
  1731. * overhead of vmalloc()/vfree() calles for this _debug_
  1732. * interface, rarely used. Instead of that, we'll use
  1733. * kmap() and get small overhead in this access function.
  1734. */
  1735. if (p) {
  1736. /*
  1737. * we can expect USER0 is not used (see vread/vwrite's
  1738. * function description)
  1739. */
  1740. void *map = kmap_atomic(p);
  1741. memcpy(buf, map + offset, length);
  1742. kunmap_atomic(map);
  1743. } else
  1744. memset(buf, 0, length);
  1745. addr += length;
  1746. buf += length;
  1747. copied += length;
  1748. count -= length;
  1749. }
  1750. return copied;
  1751. }
  1752. static int aligned_vwrite(char *buf, char *addr, unsigned long count)
  1753. {
  1754. struct page *p;
  1755. int copied = 0;
  1756. while (count) {
  1757. unsigned long offset, length;
  1758. offset = offset_in_page(addr);
  1759. length = PAGE_SIZE - offset;
  1760. if (length > count)
  1761. length = count;
  1762. p = vmalloc_to_page(addr);
  1763. /*
  1764. * To do safe access to this _mapped_ area, we need
  1765. * lock. But adding lock here means that we need to add
  1766. * overhead of vmalloc()/vfree() calles for this _debug_
  1767. * interface, rarely used. Instead of that, we'll use
  1768. * kmap() and get small overhead in this access function.
  1769. */
  1770. if (p) {
  1771. /*
  1772. * we can expect USER0 is not used (see vread/vwrite's
  1773. * function description)
  1774. */
  1775. void *map = kmap_atomic(p);
  1776. memcpy(map + offset, buf, length);
  1777. kunmap_atomic(map);
  1778. }
  1779. addr += length;
  1780. buf += length;
  1781. copied += length;
  1782. count -= length;
  1783. }
  1784. return copied;
  1785. }
  1786. /**
  1787. * vread() - read vmalloc area in a safe way.
  1788. * @buf: buffer for reading data
  1789. * @addr: vm address.
  1790. * @count: number of bytes to be read.
  1791. *
  1792. * Returns # of bytes which addr and buf should be increased.
  1793. * (same number to @count). Returns 0 if [addr...addr+count) doesn't
  1794. * includes any intersect with alive vmalloc area.
  1795. *
  1796. * This function checks that addr is a valid vmalloc'ed area, and
  1797. * copy data from that area to a given buffer. If the given memory range
  1798. * of [addr...addr+count) includes some valid address, data is copied to
  1799. * proper area of @buf. If there are memory holes, they'll be zero-filled.
  1800. * IOREMAP area is treated as memory hole and no copy is done.
  1801. *
  1802. * If [addr...addr+count) doesn't includes any intersects with alive
  1803. * vm_struct area, returns 0. @buf should be kernel's buffer.
  1804. *
  1805. * Note: In usual ops, vread() is never necessary because the caller
  1806. * should know vmalloc() area is valid and can use memcpy().
  1807. * This is for routines which have to access vmalloc area without
  1808. * any informaion, as /dev/kmem.
  1809. *
  1810. */
  1811. long vread(char *buf, char *addr, unsigned long count)
  1812. {
  1813. struct vmap_area *va;
  1814. struct vm_struct *vm;
  1815. char *vaddr, *buf_start = buf;
  1816. unsigned long buflen = count;
  1817. unsigned long n;
  1818. /* Don't allow overflow */
  1819. if ((unsigned long) addr + count < count)
  1820. count = -(unsigned long) addr;
  1821. spin_lock(&vmap_area_lock);
  1822. list_for_each_entry(va, &vmap_area_list, list) {
  1823. if (!count)
  1824. break;
  1825. if (!(va->flags & VM_VM_AREA))
  1826. continue;
  1827. vm = va->vm;
  1828. vaddr = (char *) vm->addr;
  1829. if (addr >= vaddr + get_vm_area_size(vm))
  1830. continue;
  1831. while (addr < vaddr) {
  1832. if (count == 0)
  1833. goto finished;
  1834. *buf = '\0';
  1835. buf++;
  1836. addr++;
  1837. count--;
  1838. }
  1839. n = vaddr + get_vm_area_size(vm) - addr;
  1840. if (n > count)
  1841. n = count;
  1842. if (!(vm->flags & VM_IOREMAP))
  1843. aligned_vread(buf, addr, n);
  1844. else /* IOREMAP area is treated as memory hole */
  1845. memset(buf, 0, n);
  1846. buf += n;
  1847. addr += n;
  1848. count -= n;
  1849. }
  1850. finished:
  1851. spin_unlock(&vmap_area_lock);
  1852. if (buf == buf_start)
  1853. return 0;
  1854. /* zero-fill memory holes */
  1855. if (buf != buf_start + buflen)
  1856. memset(buf, 0, buflen - (buf - buf_start));
  1857. return buflen;
  1858. }
  1859. /**
  1860. * vwrite() - write vmalloc area in a safe way.
  1861. * @buf: buffer for source data
  1862. * @addr: vm address.
  1863. * @count: number of bytes to be read.
  1864. *
  1865. * Returns # of bytes which addr and buf should be incresed.
  1866. * (same number to @count).
  1867. * If [addr...addr+count) doesn't includes any intersect with valid
  1868. * vmalloc area, returns 0.
  1869. *
  1870. * This function checks that addr is a valid vmalloc'ed area, and
  1871. * copy data from a buffer to the given addr. If specified range of
  1872. * [addr...addr+count) includes some valid address, data is copied from
  1873. * proper area of @buf. If there are memory holes, no copy to hole.
  1874. * IOREMAP area is treated as memory hole and no copy is done.
  1875. *
  1876. * If [addr...addr+count) doesn't includes any intersects with alive
  1877. * vm_struct area, returns 0. @buf should be kernel's buffer.
  1878. *
  1879. * Note: In usual ops, vwrite() is never necessary because the caller
  1880. * should know vmalloc() area is valid and can use memcpy().
  1881. * This is for routines which have to access vmalloc area without
  1882. * any informaion, as /dev/kmem.
  1883. */
  1884. long vwrite(char *buf, char *addr, unsigned long count)
  1885. {
  1886. struct vmap_area *va;
  1887. struct vm_struct *vm;
  1888. char *vaddr;
  1889. unsigned long n, buflen;
  1890. int copied = 0;
  1891. /* Don't allow overflow */
  1892. if ((unsigned long) addr + count < count)
  1893. count = -(unsigned long) addr;
  1894. buflen = count;
  1895. spin_lock(&vmap_area_lock);
  1896. list_for_each_entry(va, &vmap_area_list, list) {
  1897. if (!count)
  1898. break;
  1899. if (!(va->flags & VM_VM_AREA))
  1900. continue;
  1901. vm = va->vm;
  1902. vaddr = (char *) vm->addr;
  1903. if (addr >= vaddr + get_vm_area_size(vm))
  1904. continue;
  1905. while (addr < vaddr) {
  1906. if (count == 0)
  1907. goto finished;
  1908. buf++;
  1909. addr++;
  1910. count--;
  1911. }
  1912. n = vaddr + get_vm_area_size(vm) - addr;
  1913. if (n > count)
  1914. n = count;
  1915. if (!(vm->flags & VM_IOREMAP)) {
  1916. aligned_vwrite(buf, addr, n);
  1917. copied++;
  1918. }
  1919. buf += n;
  1920. addr += n;
  1921. count -= n;
  1922. }
  1923. finished:
  1924. spin_unlock(&vmap_area_lock);
  1925. if (!copied)
  1926. return 0;
  1927. return buflen;
  1928. }
  1929. /**
  1930. * remap_vmalloc_range_partial - map vmalloc pages to userspace
  1931. * @vma: vma to cover
  1932. * @uaddr: target user address to start at
  1933. * @kaddr: virtual address of vmalloc kernel memory
  1934. * @size: size of map area
  1935. *
  1936. * Returns: 0 for success, -Exxx on failure
  1937. *
  1938. * This function checks that @kaddr is a valid vmalloc'ed area,
  1939. * and that it is big enough to cover the range starting at
  1940. * @uaddr in @vma. Will return failure if that criteria isn't
  1941. * met.
  1942. *
  1943. * Similar to remap_pfn_range() (see mm/memory.c)
  1944. */
  1945. int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
  1946. void *kaddr, unsigned long size)
  1947. {
  1948. struct vm_struct *area;
  1949. size = PAGE_ALIGN(size);
  1950. if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
  1951. return -EINVAL;
  1952. area = find_vm_area(kaddr);
  1953. if (!area)
  1954. return -EINVAL;
  1955. if (!(area->flags & VM_USERMAP))
  1956. return -EINVAL;
  1957. if (kaddr + size > area->addr + area->size)
  1958. return -EINVAL;
  1959. do {
  1960. struct page *page = vmalloc_to_page(kaddr);
  1961. int ret;
  1962. ret = vm_insert_page(vma, uaddr, page);
  1963. if (ret)
  1964. return ret;
  1965. uaddr += PAGE_SIZE;
  1966. kaddr += PAGE_SIZE;
  1967. size -= PAGE_SIZE;
  1968. } while (size > 0);
  1969. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  1970. return 0;
  1971. }
  1972. EXPORT_SYMBOL(remap_vmalloc_range_partial);
  1973. /**
  1974. * remap_vmalloc_range - map vmalloc pages to userspace
  1975. * @vma: vma to cover (map full range of vma)
  1976. * @addr: vmalloc memory
  1977. * @pgoff: number of pages into addr before first page to map
  1978. *
  1979. * Returns: 0 for success, -Exxx on failure
  1980. *
  1981. * This function checks that addr is a valid vmalloc'ed area, and
  1982. * that it is big enough to cover the vma. Will return failure if
  1983. * that criteria isn't met.
  1984. *
  1985. * Similar to remap_pfn_range() (see mm/memory.c)
  1986. */
  1987. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  1988. unsigned long pgoff)
  1989. {
  1990. return remap_vmalloc_range_partial(vma, vma->vm_start,
  1991. addr + (pgoff << PAGE_SHIFT),
  1992. vma->vm_end - vma->vm_start);
  1993. }
  1994. EXPORT_SYMBOL(remap_vmalloc_range);
  1995. /*
  1996. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  1997. * have one.
  1998. */
  1999. void __weak vmalloc_sync_all(void)
  2000. {
  2001. }
  2002. static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
  2003. {
  2004. pte_t ***p = data;
  2005. if (p) {
  2006. *(*p) = pte;
  2007. (*p)++;
  2008. }
  2009. return 0;
  2010. }
  2011. /**
  2012. * alloc_vm_area - allocate a range of kernel address space
  2013. * @size: size of the area
  2014. * @ptes: returns the PTEs for the address space
  2015. *
  2016. * Returns: NULL on failure, vm_struct on success
  2017. *
  2018. * This function reserves a range of kernel address space, and
  2019. * allocates pagetables to map that range. No actual mappings
  2020. * are created.
  2021. *
  2022. * If @ptes is non-NULL, pointers to the PTEs (in init_mm)
  2023. * allocated for the VM area are returned.
  2024. */
  2025. struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
  2026. {
  2027. struct vm_struct *area;
  2028. area = get_vm_area_caller(size, VM_IOREMAP,
  2029. __builtin_return_address(0));
  2030. if (area == NULL)
  2031. return NULL;
  2032. /*
  2033. * This ensures that page tables are constructed for this region
  2034. * of kernel virtual address space and mapped into init_mm.
  2035. */
  2036. if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
  2037. size, f, ptes ? &ptes : NULL)) {
  2038. free_vm_area(area);
  2039. return NULL;
  2040. }
  2041. return area;
  2042. }
  2043. EXPORT_SYMBOL_GPL(alloc_vm_area);
  2044. void free_vm_area(struct vm_struct *area)
  2045. {
  2046. struct vm_struct *ret;
  2047. ret = remove_vm_area(area->addr);
  2048. BUG_ON(ret != area);
  2049. kfree(area);
  2050. }
  2051. EXPORT_SYMBOL_GPL(free_vm_area);
  2052. #ifdef CONFIG_SMP
  2053. static struct vmap_area *node_to_va(struct rb_node *n)
  2054. {
  2055. return rb_entry_safe(n, struct vmap_area, rb_node);
  2056. }
  2057. /**
  2058. * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
  2059. * @end: target address
  2060. * @pnext: out arg for the next vmap_area
  2061. * @pprev: out arg for the previous vmap_area
  2062. *
  2063. * Returns: %true if either or both of next and prev are found,
  2064. * %false if no vmap_area exists
  2065. *
  2066. * Find vmap_areas end addresses of which enclose @end. ie. if not
  2067. * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
  2068. */
  2069. static bool pvm_find_next_prev(unsigned long end,
  2070. struct vmap_area **pnext,
  2071. struct vmap_area **pprev)
  2072. {
  2073. struct rb_node *n = vmap_area_root.rb_node;
  2074. struct vmap_area *va = NULL;
  2075. while (n) {
  2076. va = rb_entry(n, struct vmap_area, rb_node);
  2077. if (end < va->va_end)
  2078. n = n->rb_left;
  2079. else if (end > va->va_end)
  2080. n = n->rb_right;
  2081. else
  2082. break;
  2083. }
  2084. if (!va)
  2085. return false;
  2086. if (va->va_end > end) {
  2087. *pnext = va;
  2088. *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
  2089. } else {
  2090. *pprev = va;
  2091. *pnext = node_to_va(rb_next(&(*pprev)->rb_node));
  2092. }
  2093. return true;
  2094. }
  2095. /**
  2096. * pvm_determine_end - find the highest aligned address between two vmap_areas
  2097. * @pnext: in/out arg for the next vmap_area
  2098. * @pprev: in/out arg for the previous vmap_area
  2099. * @align: alignment
  2100. *
  2101. * Returns: determined end address
  2102. *
  2103. * Find the highest aligned address between *@pnext and *@pprev below
  2104. * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned
  2105. * down address is between the end addresses of the two vmap_areas.
  2106. *
  2107. * Please note that the address returned by this function may fall
  2108. * inside *@pnext vmap_area. The caller is responsible for checking
  2109. * that.
  2110. */
  2111. static unsigned long pvm_determine_end(struct vmap_area **pnext,
  2112. struct vmap_area **pprev,
  2113. unsigned long align)
  2114. {
  2115. const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
  2116. unsigned long addr;
  2117. if (*pnext)
  2118. addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
  2119. else
  2120. addr = vmalloc_end;
  2121. while (*pprev && (*pprev)->va_end > addr) {
  2122. *pnext = *pprev;
  2123. *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
  2124. }
  2125. return addr;
  2126. }
  2127. /**
  2128. * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
  2129. * @offsets: array containing offset of each area
  2130. * @sizes: array containing size of each area
  2131. * @nr_vms: the number of areas to allocate
  2132. * @align: alignment, all entries in @offsets and @sizes must be aligned to this
  2133. *
  2134. * Returns: kmalloc'd vm_struct pointer array pointing to allocated
  2135. * vm_structs on success, %NULL on failure
  2136. *
  2137. * Percpu allocator wants to use congruent vm areas so that it can
  2138. * maintain the offsets among percpu areas. This function allocates
  2139. * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
  2140. * be scattered pretty far, distance between two areas easily going up
  2141. * to gigabytes. To avoid interacting with regular vmallocs, these
  2142. * areas are allocated from top.
  2143. *
  2144. * Despite its complicated look, this allocator is rather simple. It
  2145. * does everything top-down and scans areas from the end looking for
  2146. * matching slot. While scanning, if any of the areas overlaps with
  2147. * existing vmap_area, the base address is pulled down to fit the
  2148. * area. Scanning is repeated till all the areas fit and then all
  2149. * necessary data structres are inserted and the result is returned.
  2150. */
  2151. struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
  2152. const size_t *sizes, int nr_vms,
  2153. size_t align)
  2154. {
  2155. const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
  2156. const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
  2157. struct vmap_area **vas, *prev, *next;
  2158. struct vm_struct **vms;
  2159. int area, area2, last_area, term_area;
  2160. unsigned long base, start, end, last_end;
  2161. bool purged = false;
  2162. /* verify parameters and allocate data structures */
  2163. BUG_ON(offset_in_page(align) || !is_power_of_2(align));
  2164. for (last_area = 0, area = 0; area < nr_vms; area++) {
  2165. start = offsets[area];
  2166. end = start + sizes[area];
  2167. /* is everything aligned properly? */
  2168. BUG_ON(!IS_ALIGNED(offsets[area], align));
  2169. BUG_ON(!IS_ALIGNED(sizes[area], align));
  2170. /* detect the area with the highest address */
  2171. if (start > offsets[last_area])
  2172. last_area = area;
  2173. for (area2 = 0; area2 < nr_vms; area2++) {
  2174. unsigned long start2 = offsets[area2];
  2175. unsigned long end2 = start2 + sizes[area2];
  2176. if (area2 == area)
  2177. continue;
  2178. BUG_ON(start2 >= start && start2 < end);
  2179. BUG_ON(end2 <= end && end2 > start);
  2180. }
  2181. }
  2182. last_end = offsets[last_area] + sizes[last_area];
  2183. if (vmalloc_end - vmalloc_start < last_end) {
  2184. WARN_ON(true);
  2185. return NULL;
  2186. }
  2187. vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
  2188. vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
  2189. if (!vas || !vms)
  2190. goto err_free2;
  2191. for (area = 0; area < nr_vms; area++) {
  2192. vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL);
  2193. vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
  2194. if (!vas[area] || !vms[area])
  2195. goto err_free;
  2196. }
  2197. retry:
  2198. spin_lock(&vmap_area_lock);
  2199. /* start scanning - we scan from the top, begin with the last area */
  2200. area = term_area = last_area;
  2201. start = offsets[area];
  2202. end = start + sizes[area];
  2203. if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
  2204. base = vmalloc_end - last_end;
  2205. goto found;
  2206. }
  2207. base = pvm_determine_end(&next, &prev, align) - end;
  2208. while (true) {
  2209. BUG_ON(next && next->va_end <= base + end);
  2210. BUG_ON(prev && prev->va_end > base + end);
  2211. /*
  2212. * base might have underflowed, add last_end before
  2213. * comparing.
  2214. */
  2215. if (base + last_end < vmalloc_start + last_end) {
  2216. spin_unlock(&vmap_area_lock);
  2217. if (!purged) {
  2218. purge_vmap_area_lazy();
  2219. purged = true;
  2220. goto retry;
  2221. }
  2222. goto err_free;
  2223. }
  2224. /*
  2225. * If next overlaps, move base downwards so that it's
  2226. * right below next and then recheck.
  2227. */
  2228. if (next && next->va_start < base + end) {
  2229. base = pvm_determine_end(&next, &prev, align) - end;
  2230. term_area = area;
  2231. continue;
  2232. }
  2233. /*
  2234. * If prev overlaps, shift down next and prev and move
  2235. * base so that it's right below new next and then
  2236. * recheck.
  2237. */
  2238. if (prev && prev->va_end > base + start) {
  2239. next = prev;
  2240. prev = node_to_va(rb_prev(&next->rb_node));
  2241. base = pvm_determine_end(&next, &prev, align) - end;
  2242. term_area = area;
  2243. continue;
  2244. }
  2245. /*
  2246. * This area fits, move on to the previous one. If
  2247. * the previous one is the terminal one, we're done.
  2248. */
  2249. area = (area + nr_vms - 1) % nr_vms;
  2250. if (area == term_area)
  2251. break;
  2252. start = offsets[area];
  2253. end = start + sizes[area];
  2254. pvm_find_next_prev(base + end, &next, &prev);
  2255. }
  2256. found:
  2257. /* we've found a fitting base, insert all va's */
  2258. for (area = 0; area < nr_vms; area++) {
  2259. struct vmap_area *va = vas[area];
  2260. va->va_start = base + offsets[area];
  2261. va->va_end = va->va_start + sizes[area];
  2262. __insert_vmap_area(va);
  2263. }
  2264. vmap_area_pcpu_hole = base + offsets[last_area];
  2265. spin_unlock(&vmap_area_lock);
  2266. /* insert all vm's */
  2267. for (area = 0; area < nr_vms; area++)
  2268. setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
  2269. pcpu_get_vm_areas);
  2270. kfree(vas);
  2271. return vms;
  2272. err_free:
  2273. for (area = 0; area < nr_vms; area++) {
  2274. kfree(vas[area]);
  2275. kfree(vms[area]);
  2276. }
  2277. err_free2:
  2278. kfree(vas);
  2279. kfree(vms);
  2280. return NULL;
  2281. }
  2282. /**
  2283. * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
  2284. * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
  2285. * @nr_vms: the number of allocated areas
  2286. *
  2287. * Free vm_structs and the array allocated by pcpu_get_vm_areas().
  2288. */
  2289. void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
  2290. {
  2291. int i;
  2292. for (i = 0; i < nr_vms; i++)
  2293. free_vm_area(vms[i]);
  2294. kfree(vms);
  2295. }
  2296. #endif /* CONFIG_SMP */
  2297. #ifdef CONFIG_PROC_FS
  2298. static void *s_start(struct seq_file *m, loff_t *pos)
  2299. __acquires(&vmap_area_lock)
  2300. {
  2301. spin_lock(&vmap_area_lock);
  2302. return seq_list_start(&vmap_area_list, *pos);
  2303. }
  2304. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  2305. {
  2306. return seq_list_next(p, &vmap_area_list, pos);
  2307. }
  2308. static void s_stop(struct seq_file *m, void *p)
  2309. __releases(&vmap_area_lock)
  2310. {
  2311. spin_unlock(&vmap_area_lock);
  2312. }
  2313. static void show_numa_info(struct seq_file *m, struct vm_struct *v)
  2314. {
  2315. if (IS_ENABLED(CONFIG_NUMA)) {
  2316. unsigned int nr, *counters = m->private;
  2317. if (!counters)
  2318. return;
  2319. if (v->flags & VM_UNINITIALIZED)
  2320. return;
  2321. /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
  2322. smp_rmb();
  2323. memset(counters, 0, nr_node_ids * sizeof(unsigned int));
  2324. for (nr = 0; nr < v->nr_pages; nr++)
  2325. counters[page_to_nid(v->pages[nr])]++;
  2326. for_each_node_state(nr, N_HIGH_MEMORY)
  2327. if (counters[nr])
  2328. seq_printf(m, " N%u=%u", nr, counters[nr]);
  2329. }
  2330. }
  2331. static int s_show(struct seq_file *m, void *p)
  2332. {
  2333. struct vmap_area *va;
  2334. struct vm_struct *v;
  2335. va = list_entry(p, struct vmap_area, list);
  2336. /*
  2337. * s_show can encounter race with remove_vm_area, !VM_VM_AREA on
  2338. * behalf of vmap area is being tear down or vm_map_ram allocation.
  2339. */
  2340. if (!(va->flags & VM_VM_AREA)) {
  2341. seq_printf(m, "0x%pK-0x%pK %7ld %s\n",
  2342. (void *)va->va_start, (void *)va->va_end,
  2343. va->va_end - va->va_start,
  2344. va->flags & VM_LAZY_FREE ? "unpurged vm_area" : "vm_map_ram");
  2345. return 0;
  2346. }
  2347. v = va->vm;
  2348. seq_printf(m, "0x%pK-0x%pK %7ld",
  2349. v->addr, v->addr + v->size, v->size);
  2350. if (v->caller)
  2351. seq_printf(m, " %pS", v->caller);
  2352. if (v->nr_pages)
  2353. seq_printf(m, " pages=%d", v->nr_pages);
  2354. if (v->phys_addr)
  2355. seq_printf(m, " phys=%pa", &v->phys_addr);
  2356. if (v->flags & VM_IOREMAP)
  2357. seq_puts(m, " ioremap");
  2358. if (v->flags & VM_ALLOC)
  2359. seq_puts(m, " vmalloc");
  2360. if (v->flags & VM_MAP)
  2361. seq_puts(m, " vmap");
  2362. if (v->flags & VM_USERMAP)
  2363. seq_puts(m, " user");
  2364. if (is_vmalloc_addr(v->pages))
  2365. seq_puts(m, " vpages");
  2366. show_numa_info(m, v);
  2367. seq_putc(m, '\n');
  2368. return 0;
  2369. }
  2370. static const struct seq_operations vmalloc_op = {
  2371. .start = s_start,
  2372. .next = s_next,
  2373. .stop = s_stop,
  2374. .show = s_show,
  2375. };
  2376. static int vmalloc_open(struct inode *inode, struct file *file)
  2377. {
  2378. if (IS_ENABLED(CONFIG_NUMA))
  2379. return seq_open_private(file, &vmalloc_op,
  2380. nr_node_ids * sizeof(unsigned int));
  2381. else
  2382. return seq_open(file, &vmalloc_op);
  2383. }
  2384. static const struct file_operations proc_vmalloc_operations = {
  2385. .open = vmalloc_open,
  2386. .read = seq_read,
  2387. .llseek = seq_lseek,
  2388. .release = seq_release_private,
  2389. };
  2390. static int __init proc_vmalloc_init(void)
  2391. {
  2392. proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
  2393. return 0;
  2394. }
  2395. module_init(proc_vmalloc_init);
  2396. #endif