vmalloc.c 70 KB

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