vmalloc.c 68 KB

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