vmalloc.c 72 KB

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