hugetlb.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150
  1. /*
  2. * Generic hugetlb support.
  3. * (C) William Irwin, April 2004
  4. */
  5. #include <linux/list.h>
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/mm.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/sysctl.h>
  11. #include <linux/highmem.h>
  12. #include <linux/mmu_notifier.h>
  13. #include <linux/nodemask.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/mempolicy.h>
  16. #include <linux/cpuset.h>
  17. #include <linux/mutex.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/slab.h>
  21. #include <linux/rmap.h>
  22. #include <linux/swap.h>
  23. #include <linux/swapops.h>
  24. #include <asm/page.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/tlb.h>
  27. #include <linux/io.h>
  28. #include <linux/hugetlb.h>
  29. #include <linux/hugetlb_cgroup.h>
  30. #include <linux/node.h>
  31. #include <linux/hugetlb_cgroup.h>
  32. #include "internal.h"
  33. const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
  34. static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
  35. unsigned long hugepages_treat_as_movable;
  36. int hugetlb_max_hstate __read_mostly;
  37. unsigned int default_hstate_idx;
  38. struct hstate hstates[HUGE_MAX_HSTATE];
  39. __initdata LIST_HEAD(huge_boot_pages);
  40. /* for command line parsing */
  41. static struct hstate * __initdata parsed_hstate;
  42. static unsigned long __initdata default_hstate_max_huge_pages;
  43. static unsigned long __initdata default_hstate_size;
  44. /*
  45. * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
  46. */
  47. DEFINE_SPINLOCK(hugetlb_lock);
  48. static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
  49. {
  50. bool free = (spool->count == 0) && (spool->used_hpages == 0);
  51. spin_unlock(&spool->lock);
  52. /* If no pages are used, and no other handles to the subpool
  53. * remain, free the subpool the subpool remain */
  54. if (free)
  55. kfree(spool);
  56. }
  57. struct hugepage_subpool *hugepage_new_subpool(long nr_blocks)
  58. {
  59. struct hugepage_subpool *spool;
  60. spool = kmalloc(sizeof(*spool), GFP_KERNEL);
  61. if (!spool)
  62. return NULL;
  63. spin_lock_init(&spool->lock);
  64. spool->count = 1;
  65. spool->max_hpages = nr_blocks;
  66. spool->used_hpages = 0;
  67. return spool;
  68. }
  69. void hugepage_put_subpool(struct hugepage_subpool *spool)
  70. {
  71. spin_lock(&spool->lock);
  72. BUG_ON(!spool->count);
  73. spool->count--;
  74. unlock_or_release_subpool(spool);
  75. }
  76. static int hugepage_subpool_get_pages(struct hugepage_subpool *spool,
  77. long delta)
  78. {
  79. int ret = 0;
  80. if (!spool)
  81. return 0;
  82. spin_lock(&spool->lock);
  83. if ((spool->used_hpages + delta) <= spool->max_hpages) {
  84. spool->used_hpages += delta;
  85. } else {
  86. ret = -ENOMEM;
  87. }
  88. spin_unlock(&spool->lock);
  89. return ret;
  90. }
  91. static void hugepage_subpool_put_pages(struct hugepage_subpool *spool,
  92. long delta)
  93. {
  94. if (!spool)
  95. return;
  96. spin_lock(&spool->lock);
  97. spool->used_hpages -= delta;
  98. /* If hugetlbfs_put_super couldn't free spool due to
  99. * an outstanding quota reference, free it now. */
  100. unlock_or_release_subpool(spool);
  101. }
  102. static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
  103. {
  104. return HUGETLBFS_SB(inode->i_sb)->spool;
  105. }
  106. static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
  107. {
  108. return subpool_inode(vma->vm_file->f_dentry->d_inode);
  109. }
  110. /*
  111. * Region tracking -- allows tracking of reservations and instantiated pages
  112. * across the pages in a mapping.
  113. *
  114. * The region data structures are protected by a combination of the mmap_sem
  115. * and the hugetlb_instantion_mutex. To access or modify a region the caller
  116. * must either hold the mmap_sem for write, or the mmap_sem for read and
  117. * the hugetlb_instantiation mutex:
  118. *
  119. * down_write(&mm->mmap_sem);
  120. * or
  121. * down_read(&mm->mmap_sem);
  122. * mutex_lock(&hugetlb_instantiation_mutex);
  123. */
  124. struct file_region {
  125. struct list_head link;
  126. long from;
  127. long to;
  128. };
  129. static long region_add(struct list_head *head, long f, long t)
  130. {
  131. struct file_region *rg, *nrg, *trg;
  132. /* Locate the region we are either in or before. */
  133. list_for_each_entry(rg, head, link)
  134. if (f <= rg->to)
  135. break;
  136. /* Round our left edge to the current segment if it encloses us. */
  137. if (f > rg->from)
  138. f = rg->from;
  139. /* Check for and consume any regions we now overlap with. */
  140. nrg = rg;
  141. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  142. if (&rg->link == head)
  143. break;
  144. if (rg->from > t)
  145. break;
  146. /* If this area reaches higher then extend our area to
  147. * include it completely. If this is not the first area
  148. * which we intend to reuse, free it. */
  149. if (rg->to > t)
  150. t = rg->to;
  151. if (rg != nrg) {
  152. list_del(&rg->link);
  153. kfree(rg);
  154. }
  155. }
  156. nrg->from = f;
  157. nrg->to = t;
  158. return 0;
  159. }
  160. static long region_chg(struct list_head *head, long f, long t)
  161. {
  162. struct file_region *rg, *nrg;
  163. long chg = 0;
  164. /* Locate the region we are before or in. */
  165. list_for_each_entry(rg, head, link)
  166. if (f <= rg->to)
  167. break;
  168. /* If we are below the current region then a new region is required.
  169. * Subtle, allocate a new region at the position but make it zero
  170. * size such that we can guarantee to record the reservation. */
  171. if (&rg->link == head || t < rg->from) {
  172. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  173. if (!nrg)
  174. return -ENOMEM;
  175. nrg->from = f;
  176. nrg->to = f;
  177. INIT_LIST_HEAD(&nrg->link);
  178. list_add(&nrg->link, rg->link.prev);
  179. return t - f;
  180. }
  181. /* Round our left edge to the current segment if it encloses us. */
  182. if (f > rg->from)
  183. f = rg->from;
  184. chg = t - f;
  185. /* Check for and consume any regions we now overlap with. */
  186. list_for_each_entry(rg, rg->link.prev, link) {
  187. if (&rg->link == head)
  188. break;
  189. if (rg->from > t)
  190. return chg;
  191. /* We overlap with this area, if it extends further than
  192. * us then we must extend ourselves. Account for its
  193. * existing reservation. */
  194. if (rg->to > t) {
  195. chg += rg->to - t;
  196. t = rg->to;
  197. }
  198. chg -= rg->to - rg->from;
  199. }
  200. return chg;
  201. }
  202. static long region_truncate(struct list_head *head, long end)
  203. {
  204. struct file_region *rg, *trg;
  205. long chg = 0;
  206. /* Locate the region we are either in or before. */
  207. list_for_each_entry(rg, head, link)
  208. if (end <= rg->to)
  209. break;
  210. if (&rg->link == head)
  211. return 0;
  212. /* If we are in the middle of a region then adjust it. */
  213. if (end > rg->from) {
  214. chg = rg->to - end;
  215. rg->to = end;
  216. rg = list_entry(rg->link.next, typeof(*rg), link);
  217. }
  218. /* Drop any remaining regions. */
  219. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  220. if (&rg->link == head)
  221. break;
  222. chg += rg->to - rg->from;
  223. list_del(&rg->link);
  224. kfree(rg);
  225. }
  226. return chg;
  227. }
  228. static long region_count(struct list_head *head, long f, long t)
  229. {
  230. struct file_region *rg;
  231. long chg = 0;
  232. /* Locate each segment we overlap with, and count that overlap. */
  233. list_for_each_entry(rg, head, link) {
  234. long seg_from;
  235. long seg_to;
  236. if (rg->to <= f)
  237. continue;
  238. if (rg->from >= t)
  239. break;
  240. seg_from = max(rg->from, f);
  241. seg_to = min(rg->to, t);
  242. chg += seg_to - seg_from;
  243. }
  244. return chg;
  245. }
  246. /*
  247. * Convert the address within this vma to the page offset within
  248. * the mapping, in pagecache page units; huge pages here.
  249. */
  250. static pgoff_t vma_hugecache_offset(struct hstate *h,
  251. struct vm_area_struct *vma, unsigned long address)
  252. {
  253. return ((address - vma->vm_start) >> huge_page_shift(h)) +
  254. (vma->vm_pgoff >> huge_page_order(h));
  255. }
  256. pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
  257. unsigned long address)
  258. {
  259. return vma_hugecache_offset(hstate_vma(vma), vma, address);
  260. }
  261. /*
  262. * Return the size of the pages allocated when backing a VMA. In the majority
  263. * cases this will be same size as used by the page table entries.
  264. */
  265. unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
  266. {
  267. struct hstate *hstate;
  268. if (!is_vm_hugetlb_page(vma))
  269. return PAGE_SIZE;
  270. hstate = hstate_vma(vma);
  271. return 1UL << (hstate->order + PAGE_SHIFT);
  272. }
  273. EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
  274. /*
  275. * Return the page size being used by the MMU to back a VMA. In the majority
  276. * of cases, the page size used by the kernel matches the MMU size. On
  277. * architectures where it differs, an architecture-specific version of this
  278. * function is required.
  279. */
  280. #ifndef vma_mmu_pagesize
  281. unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
  282. {
  283. return vma_kernel_pagesize(vma);
  284. }
  285. #endif
  286. /*
  287. * Flags for MAP_PRIVATE reservations. These are stored in the bottom
  288. * bits of the reservation map pointer, which are always clear due to
  289. * alignment.
  290. */
  291. #define HPAGE_RESV_OWNER (1UL << 0)
  292. #define HPAGE_RESV_UNMAPPED (1UL << 1)
  293. #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
  294. /*
  295. * These helpers are used to track how many pages are reserved for
  296. * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
  297. * is guaranteed to have their future faults succeed.
  298. *
  299. * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
  300. * the reserve counters are updated with the hugetlb_lock held. It is safe
  301. * to reset the VMA at fork() time as it is not in use yet and there is no
  302. * chance of the global counters getting corrupted as a result of the values.
  303. *
  304. * The private mapping reservation is represented in a subtly different
  305. * manner to a shared mapping. A shared mapping has a region map associated
  306. * with the underlying file, this region map represents the backing file
  307. * pages which have ever had a reservation assigned which this persists even
  308. * after the page is instantiated. A private mapping has a region map
  309. * associated with the original mmap which is attached to all VMAs which
  310. * reference it, this region map represents those offsets which have consumed
  311. * reservation ie. where pages have been instantiated.
  312. */
  313. static unsigned long get_vma_private_data(struct vm_area_struct *vma)
  314. {
  315. return (unsigned long)vma->vm_private_data;
  316. }
  317. static void set_vma_private_data(struct vm_area_struct *vma,
  318. unsigned long value)
  319. {
  320. vma->vm_private_data = (void *)value;
  321. }
  322. struct resv_map {
  323. struct kref refs;
  324. struct list_head regions;
  325. };
  326. static struct resv_map *resv_map_alloc(void)
  327. {
  328. struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
  329. if (!resv_map)
  330. return NULL;
  331. kref_init(&resv_map->refs);
  332. INIT_LIST_HEAD(&resv_map->regions);
  333. return resv_map;
  334. }
  335. static void resv_map_release(struct kref *ref)
  336. {
  337. struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
  338. /* Clear out any active regions before we release the map. */
  339. region_truncate(&resv_map->regions, 0);
  340. kfree(resv_map);
  341. }
  342. static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
  343. {
  344. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  345. if (!(vma->vm_flags & VM_MAYSHARE))
  346. return (struct resv_map *)(get_vma_private_data(vma) &
  347. ~HPAGE_RESV_MASK);
  348. return NULL;
  349. }
  350. static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
  351. {
  352. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  353. VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
  354. set_vma_private_data(vma, (get_vma_private_data(vma) &
  355. HPAGE_RESV_MASK) | (unsigned long)map);
  356. }
  357. static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
  358. {
  359. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  360. VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
  361. set_vma_private_data(vma, get_vma_private_data(vma) | flags);
  362. }
  363. static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
  364. {
  365. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  366. return (get_vma_private_data(vma) & flag) != 0;
  367. }
  368. /* Decrement the reserved pages in the hugepage pool by one */
  369. static void decrement_hugepage_resv_vma(struct hstate *h,
  370. struct vm_area_struct *vma)
  371. {
  372. if (vma->vm_flags & VM_NORESERVE)
  373. return;
  374. if (vma->vm_flags & VM_MAYSHARE) {
  375. /* Shared mappings always use reserves */
  376. h->resv_huge_pages--;
  377. } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  378. /*
  379. * Only the process that called mmap() has reserves for
  380. * private mappings.
  381. */
  382. h->resv_huge_pages--;
  383. }
  384. }
  385. /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
  386. void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
  387. {
  388. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  389. if (!(vma->vm_flags & VM_MAYSHARE))
  390. vma->vm_private_data = (void *)0;
  391. }
  392. /* Returns true if the VMA has associated reserve pages */
  393. static int vma_has_reserves(struct vm_area_struct *vma)
  394. {
  395. if (vma->vm_flags & VM_MAYSHARE)
  396. return 1;
  397. if (is_vma_resv_set(vma, HPAGE_RESV_OWNER))
  398. return 1;
  399. return 0;
  400. }
  401. static void copy_gigantic_page(struct page *dst, struct page *src)
  402. {
  403. int i;
  404. struct hstate *h = page_hstate(src);
  405. struct page *dst_base = dst;
  406. struct page *src_base = src;
  407. for (i = 0; i < pages_per_huge_page(h); ) {
  408. cond_resched();
  409. copy_highpage(dst, src);
  410. i++;
  411. dst = mem_map_next(dst, dst_base, i);
  412. src = mem_map_next(src, src_base, i);
  413. }
  414. }
  415. void copy_huge_page(struct page *dst, struct page *src)
  416. {
  417. int i;
  418. struct hstate *h = page_hstate(src);
  419. if (unlikely(pages_per_huge_page(h) > MAX_ORDER_NR_PAGES)) {
  420. copy_gigantic_page(dst, src);
  421. return;
  422. }
  423. might_sleep();
  424. for (i = 0; i < pages_per_huge_page(h); i++) {
  425. cond_resched();
  426. copy_highpage(dst + i, src + i);
  427. }
  428. }
  429. static void enqueue_huge_page(struct hstate *h, struct page *page)
  430. {
  431. int nid = page_to_nid(page);
  432. list_move(&page->lru, &h->hugepage_freelists[nid]);
  433. h->free_huge_pages++;
  434. h->free_huge_pages_node[nid]++;
  435. }
  436. static struct page *dequeue_huge_page_node(struct hstate *h, int nid)
  437. {
  438. struct page *page;
  439. if (list_empty(&h->hugepage_freelists[nid]))
  440. return NULL;
  441. page = list_entry(h->hugepage_freelists[nid].next, struct page, lru);
  442. list_move(&page->lru, &h->hugepage_activelist);
  443. set_page_refcounted(page);
  444. h->free_huge_pages--;
  445. h->free_huge_pages_node[nid]--;
  446. return page;
  447. }
  448. static struct page *dequeue_huge_page_vma(struct hstate *h,
  449. struct vm_area_struct *vma,
  450. unsigned long address, int avoid_reserve)
  451. {
  452. struct page *page = NULL;
  453. struct mempolicy *mpol;
  454. nodemask_t *nodemask;
  455. struct zonelist *zonelist;
  456. struct zone *zone;
  457. struct zoneref *z;
  458. unsigned int cpuset_mems_cookie;
  459. retry_cpuset:
  460. cpuset_mems_cookie = get_mems_allowed();
  461. zonelist = huge_zonelist(vma, address,
  462. htlb_alloc_mask, &mpol, &nodemask);
  463. /*
  464. * A child process with MAP_PRIVATE mappings created by their parent
  465. * have no page reserves. This check ensures that reservations are
  466. * not "stolen". The child may still get SIGKILLed
  467. */
  468. if (!vma_has_reserves(vma) &&
  469. h->free_huge_pages - h->resv_huge_pages == 0)
  470. goto err;
  471. /* If reserves cannot be used, ensure enough pages are in the pool */
  472. if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
  473. goto err;
  474. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  475. MAX_NR_ZONES - 1, nodemask) {
  476. if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask)) {
  477. page = dequeue_huge_page_node(h, zone_to_nid(zone));
  478. if (page) {
  479. if (!avoid_reserve)
  480. decrement_hugepage_resv_vma(h, vma);
  481. break;
  482. }
  483. }
  484. }
  485. mpol_cond_put(mpol);
  486. if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
  487. goto retry_cpuset;
  488. return page;
  489. err:
  490. mpol_cond_put(mpol);
  491. return NULL;
  492. }
  493. static void update_and_free_page(struct hstate *h, struct page *page)
  494. {
  495. int i;
  496. VM_BUG_ON(h->order >= MAX_ORDER);
  497. h->nr_huge_pages--;
  498. h->nr_huge_pages_node[page_to_nid(page)]--;
  499. for (i = 0; i < pages_per_huge_page(h); i++) {
  500. page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
  501. 1 << PG_referenced | 1 << PG_dirty |
  502. 1 << PG_active | 1 << PG_reserved |
  503. 1 << PG_private | 1 << PG_writeback);
  504. }
  505. VM_BUG_ON(hugetlb_cgroup_from_page(page));
  506. set_compound_page_dtor(page, NULL);
  507. set_page_refcounted(page);
  508. arch_release_hugepage(page);
  509. __free_pages(page, huge_page_order(h));
  510. }
  511. struct hstate *size_to_hstate(unsigned long size)
  512. {
  513. struct hstate *h;
  514. for_each_hstate(h) {
  515. if (huge_page_size(h) == size)
  516. return h;
  517. }
  518. return NULL;
  519. }
  520. static void free_huge_page(struct page *page)
  521. {
  522. /*
  523. * Can't pass hstate in here because it is called from the
  524. * compound page destructor.
  525. */
  526. struct hstate *h = page_hstate(page);
  527. int nid = page_to_nid(page);
  528. struct hugepage_subpool *spool =
  529. (struct hugepage_subpool *)page_private(page);
  530. set_page_private(page, 0);
  531. page->mapping = NULL;
  532. BUG_ON(page_count(page));
  533. BUG_ON(page_mapcount(page));
  534. spin_lock(&hugetlb_lock);
  535. hugetlb_cgroup_uncharge_page(hstate_index(h),
  536. pages_per_huge_page(h), page);
  537. if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) {
  538. /* remove the page from active list */
  539. list_del(&page->lru);
  540. update_and_free_page(h, page);
  541. h->surplus_huge_pages--;
  542. h->surplus_huge_pages_node[nid]--;
  543. } else {
  544. enqueue_huge_page(h, page);
  545. }
  546. spin_unlock(&hugetlb_lock);
  547. hugepage_subpool_put_pages(spool, 1);
  548. }
  549. static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
  550. {
  551. INIT_LIST_HEAD(&page->lru);
  552. set_compound_page_dtor(page, free_huge_page);
  553. spin_lock(&hugetlb_lock);
  554. set_hugetlb_cgroup(page, NULL);
  555. h->nr_huge_pages++;
  556. h->nr_huge_pages_node[nid]++;
  557. spin_unlock(&hugetlb_lock);
  558. put_page(page); /* free it into the hugepage allocator */
  559. }
  560. static void prep_compound_gigantic_page(struct page *page, unsigned long order)
  561. {
  562. int i;
  563. int nr_pages = 1 << order;
  564. struct page *p = page + 1;
  565. /* we rely on prep_new_huge_page to set the destructor */
  566. set_compound_order(page, order);
  567. __SetPageHead(page);
  568. for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
  569. __SetPageTail(p);
  570. set_page_count(p, 0);
  571. p->first_page = page;
  572. }
  573. }
  574. int PageHuge(struct page *page)
  575. {
  576. compound_page_dtor *dtor;
  577. if (!PageCompound(page))
  578. return 0;
  579. page = compound_head(page);
  580. dtor = get_compound_page_dtor(page);
  581. return dtor == free_huge_page;
  582. }
  583. EXPORT_SYMBOL_GPL(PageHuge);
  584. static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
  585. {
  586. struct page *page;
  587. if (h->order >= MAX_ORDER)
  588. return NULL;
  589. page = alloc_pages_exact_node(nid,
  590. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
  591. __GFP_REPEAT|__GFP_NOWARN,
  592. huge_page_order(h));
  593. if (page) {
  594. if (arch_prepare_hugepage(page)) {
  595. __free_pages(page, huge_page_order(h));
  596. return NULL;
  597. }
  598. prep_new_huge_page(h, page, nid);
  599. }
  600. return page;
  601. }
  602. /*
  603. * common helper functions for hstate_next_node_to_{alloc|free}.
  604. * We may have allocated or freed a huge page based on a different
  605. * nodes_allowed previously, so h->next_node_to_{alloc|free} might
  606. * be outside of *nodes_allowed. Ensure that we use an allowed
  607. * node for alloc or free.
  608. */
  609. static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
  610. {
  611. nid = next_node(nid, *nodes_allowed);
  612. if (nid == MAX_NUMNODES)
  613. nid = first_node(*nodes_allowed);
  614. VM_BUG_ON(nid >= MAX_NUMNODES);
  615. return nid;
  616. }
  617. static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
  618. {
  619. if (!node_isset(nid, *nodes_allowed))
  620. nid = next_node_allowed(nid, nodes_allowed);
  621. return nid;
  622. }
  623. /*
  624. * returns the previously saved node ["this node"] from which to
  625. * allocate a persistent huge page for the pool and advance the
  626. * next node from which to allocate, handling wrap at end of node
  627. * mask.
  628. */
  629. static int hstate_next_node_to_alloc(struct hstate *h,
  630. nodemask_t *nodes_allowed)
  631. {
  632. int nid;
  633. VM_BUG_ON(!nodes_allowed);
  634. nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
  635. h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
  636. return nid;
  637. }
  638. static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
  639. {
  640. struct page *page;
  641. int start_nid;
  642. int next_nid;
  643. int ret = 0;
  644. start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
  645. next_nid = start_nid;
  646. do {
  647. page = alloc_fresh_huge_page_node(h, next_nid);
  648. if (page) {
  649. ret = 1;
  650. break;
  651. }
  652. next_nid = hstate_next_node_to_alloc(h, nodes_allowed);
  653. } while (next_nid != start_nid);
  654. if (ret)
  655. count_vm_event(HTLB_BUDDY_PGALLOC);
  656. else
  657. count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  658. return ret;
  659. }
  660. /*
  661. * helper for free_pool_huge_page() - return the previously saved
  662. * node ["this node"] from which to free a huge page. Advance the
  663. * next node id whether or not we find a free huge page to free so
  664. * that the next attempt to free addresses the next node.
  665. */
  666. static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
  667. {
  668. int nid;
  669. VM_BUG_ON(!nodes_allowed);
  670. nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
  671. h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
  672. return nid;
  673. }
  674. /*
  675. * Free huge page from pool from next node to free.
  676. * Attempt to keep persistent huge pages more or less
  677. * balanced over allowed nodes.
  678. * Called with hugetlb_lock locked.
  679. */
  680. static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  681. bool acct_surplus)
  682. {
  683. int start_nid;
  684. int next_nid;
  685. int ret = 0;
  686. start_nid = hstate_next_node_to_free(h, nodes_allowed);
  687. next_nid = start_nid;
  688. do {
  689. /*
  690. * If we're returning unused surplus pages, only examine
  691. * nodes with surplus pages.
  692. */
  693. if ((!acct_surplus || h->surplus_huge_pages_node[next_nid]) &&
  694. !list_empty(&h->hugepage_freelists[next_nid])) {
  695. struct page *page =
  696. list_entry(h->hugepage_freelists[next_nid].next,
  697. struct page, lru);
  698. list_del(&page->lru);
  699. h->free_huge_pages--;
  700. h->free_huge_pages_node[next_nid]--;
  701. if (acct_surplus) {
  702. h->surplus_huge_pages--;
  703. h->surplus_huge_pages_node[next_nid]--;
  704. }
  705. update_and_free_page(h, page);
  706. ret = 1;
  707. break;
  708. }
  709. next_nid = hstate_next_node_to_free(h, nodes_allowed);
  710. } while (next_nid != start_nid);
  711. return ret;
  712. }
  713. static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)
  714. {
  715. struct page *page;
  716. unsigned int r_nid;
  717. if (h->order >= MAX_ORDER)
  718. return NULL;
  719. /*
  720. * Assume we will successfully allocate the surplus page to
  721. * prevent racing processes from causing the surplus to exceed
  722. * overcommit
  723. *
  724. * This however introduces a different race, where a process B
  725. * tries to grow the static hugepage pool while alloc_pages() is
  726. * called by process A. B will only examine the per-node
  727. * counters in determining if surplus huge pages can be
  728. * converted to normal huge pages in adjust_pool_surplus(). A
  729. * won't be able to increment the per-node counter, until the
  730. * lock is dropped by B, but B doesn't drop hugetlb_lock until
  731. * no more huge pages can be converted from surplus to normal
  732. * state (and doesn't try to convert again). Thus, we have a
  733. * case where a surplus huge page exists, the pool is grown, and
  734. * the surplus huge page still exists after, even though it
  735. * should just have been converted to a normal huge page. This
  736. * does not leak memory, though, as the hugepage will be freed
  737. * once it is out of use. It also does not allow the counters to
  738. * go out of whack in adjust_pool_surplus() as we don't modify
  739. * the node values until we've gotten the hugepage and only the
  740. * per-node value is checked there.
  741. */
  742. spin_lock(&hugetlb_lock);
  743. if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
  744. spin_unlock(&hugetlb_lock);
  745. return NULL;
  746. } else {
  747. h->nr_huge_pages++;
  748. h->surplus_huge_pages++;
  749. }
  750. spin_unlock(&hugetlb_lock);
  751. if (nid == NUMA_NO_NODE)
  752. page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
  753. __GFP_REPEAT|__GFP_NOWARN,
  754. huge_page_order(h));
  755. else
  756. page = alloc_pages_exact_node(nid,
  757. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
  758. __GFP_REPEAT|__GFP_NOWARN, huge_page_order(h));
  759. if (page && arch_prepare_hugepage(page)) {
  760. __free_pages(page, huge_page_order(h));
  761. page = NULL;
  762. }
  763. spin_lock(&hugetlb_lock);
  764. if (page) {
  765. INIT_LIST_HEAD(&page->lru);
  766. r_nid = page_to_nid(page);
  767. set_compound_page_dtor(page, free_huge_page);
  768. set_hugetlb_cgroup(page, NULL);
  769. /*
  770. * We incremented the global counters already
  771. */
  772. h->nr_huge_pages_node[r_nid]++;
  773. h->surplus_huge_pages_node[r_nid]++;
  774. __count_vm_event(HTLB_BUDDY_PGALLOC);
  775. } else {
  776. h->nr_huge_pages--;
  777. h->surplus_huge_pages--;
  778. __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  779. }
  780. spin_unlock(&hugetlb_lock);
  781. return page;
  782. }
  783. /*
  784. * This allocation function is useful in the context where vma is irrelevant.
  785. * E.g. soft-offlining uses this function because it only cares physical
  786. * address of error page.
  787. */
  788. struct page *alloc_huge_page_node(struct hstate *h, int nid)
  789. {
  790. struct page *page;
  791. spin_lock(&hugetlb_lock);
  792. page = dequeue_huge_page_node(h, nid);
  793. spin_unlock(&hugetlb_lock);
  794. if (!page) {
  795. page = alloc_buddy_huge_page(h, nid);
  796. if (page) {
  797. spin_lock(&hugetlb_lock);
  798. list_move(&page->lru, &h->hugepage_activelist);
  799. spin_unlock(&hugetlb_lock);
  800. }
  801. }
  802. return page;
  803. }
  804. /*
  805. * Increase the hugetlb pool such that it can accommodate a reservation
  806. * of size 'delta'.
  807. */
  808. static int gather_surplus_pages(struct hstate *h, int delta)
  809. {
  810. struct list_head surplus_list;
  811. struct page *page, *tmp;
  812. int ret, i;
  813. int needed, allocated;
  814. bool alloc_ok = true;
  815. needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
  816. if (needed <= 0) {
  817. h->resv_huge_pages += delta;
  818. return 0;
  819. }
  820. allocated = 0;
  821. INIT_LIST_HEAD(&surplus_list);
  822. ret = -ENOMEM;
  823. retry:
  824. spin_unlock(&hugetlb_lock);
  825. for (i = 0; i < needed; i++) {
  826. page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
  827. if (!page) {
  828. alloc_ok = false;
  829. break;
  830. }
  831. list_add(&page->lru, &surplus_list);
  832. }
  833. allocated += i;
  834. /*
  835. * After retaking hugetlb_lock, we need to recalculate 'needed'
  836. * because either resv_huge_pages or free_huge_pages may have changed.
  837. */
  838. spin_lock(&hugetlb_lock);
  839. needed = (h->resv_huge_pages + delta) -
  840. (h->free_huge_pages + allocated);
  841. if (needed > 0) {
  842. if (alloc_ok)
  843. goto retry;
  844. /*
  845. * We were not able to allocate enough pages to
  846. * satisfy the entire reservation so we free what
  847. * we've allocated so far.
  848. */
  849. goto free;
  850. }
  851. /*
  852. * The surplus_list now contains _at_least_ the number of extra pages
  853. * needed to accommodate the reservation. Add the appropriate number
  854. * of pages to the hugetlb pool and free the extras back to the buddy
  855. * allocator. Commit the entire reservation here to prevent another
  856. * process from stealing the pages as they are added to the pool but
  857. * before they are reserved.
  858. */
  859. needed += allocated;
  860. h->resv_huge_pages += delta;
  861. ret = 0;
  862. /* Free the needed pages to the hugetlb pool */
  863. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  864. if ((--needed) < 0)
  865. break;
  866. /*
  867. * This page is now managed by the hugetlb allocator and has
  868. * no users -- drop the buddy allocator's reference.
  869. */
  870. put_page_testzero(page);
  871. VM_BUG_ON(page_count(page));
  872. enqueue_huge_page(h, page);
  873. }
  874. free:
  875. spin_unlock(&hugetlb_lock);
  876. /* Free unnecessary surplus pages to the buddy allocator */
  877. if (!list_empty(&surplus_list)) {
  878. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  879. put_page(page);
  880. }
  881. }
  882. spin_lock(&hugetlb_lock);
  883. return ret;
  884. }
  885. /*
  886. * When releasing a hugetlb pool reservation, any surplus pages that were
  887. * allocated to satisfy the reservation must be explicitly freed if they were
  888. * never used.
  889. * Called with hugetlb_lock held.
  890. */
  891. static void return_unused_surplus_pages(struct hstate *h,
  892. unsigned long unused_resv_pages)
  893. {
  894. unsigned long nr_pages;
  895. /* Uncommit the reservation */
  896. h->resv_huge_pages -= unused_resv_pages;
  897. /* Cannot return gigantic pages currently */
  898. if (h->order >= MAX_ORDER)
  899. return;
  900. nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
  901. /*
  902. * We want to release as many surplus pages as possible, spread
  903. * evenly across all nodes with memory. Iterate across these nodes
  904. * until we can no longer free unreserved surplus pages. This occurs
  905. * when the nodes with surplus pages have no free pages.
  906. * free_pool_huge_page() will balance the the freed pages across the
  907. * on-line nodes with memory and will handle the hstate accounting.
  908. */
  909. while (nr_pages--) {
  910. if (!free_pool_huge_page(h, &node_states[N_HIGH_MEMORY], 1))
  911. break;
  912. }
  913. }
  914. /*
  915. * Determine if the huge page at addr within the vma has an associated
  916. * reservation. Where it does not we will need to logically increase
  917. * reservation and actually increase subpool usage before an allocation
  918. * can occur. Where any new reservation would be required the
  919. * reservation change is prepared, but not committed. Once the page
  920. * has been allocated from the subpool and instantiated the change should
  921. * be committed via vma_commit_reservation. No action is required on
  922. * failure.
  923. */
  924. static long vma_needs_reservation(struct hstate *h,
  925. struct vm_area_struct *vma, unsigned long addr)
  926. {
  927. struct address_space *mapping = vma->vm_file->f_mapping;
  928. struct inode *inode = mapping->host;
  929. if (vma->vm_flags & VM_MAYSHARE) {
  930. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  931. return region_chg(&inode->i_mapping->private_list,
  932. idx, idx + 1);
  933. } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  934. return 1;
  935. } else {
  936. long err;
  937. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  938. struct resv_map *reservations = vma_resv_map(vma);
  939. err = region_chg(&reservations->regions, idx, idx + 1);
  940. if (err < 0)
  941. return err;
  942. return 0;
  943. }
  944. }
  945. static void vma_commit_reservation(struct hstate *h,
  946. struct vm_area_struct *vma, unsigned long addr)
  947. {
  948. struct address_space *mapping = vma->vm_file->f_mapping;
  949. struct inode *inode = mapping->host;
  950. if (vma->vm_flags & VM_MAYSHARE) {
  951. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  952. region_add(&inode->i_mapping->private_list, idx, idx + 1);
  953. } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  954. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  955. struct resv_map *reservations = vma_resv_map(vma);
  956. /* Mark this page used in the map. */
  957. region_add(&reservations->regions, idx, idx + 1);
  958. }
  959. }
  960. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  961. unsigned long addr, int avoid_reserve)
  962. {
  963. struct hugepage_subpool *spool = subpool_vma(vma);
  964. struct hstate *h = hstate_vma(vma);
  965. struct page *page;
  966. long chg;
  967. int ret, idx;
  968. struct hugetlb_cgroup *h_cg;
  969. idx = hstate_index(h);
  970. /*
  971. * Processes that did not create the mapping will have no
  972. * reserves and will not have accounted against subpool
  973. * limit. Check that the subpool limit can be made before
  974. * satisfying the allocation MAP_NORESERVE mappings may also
  975. * need pages and subpool limit allocated allocated if no reserve
  976. * mapping overlaps.
  977. */
  978. chg = vma_needs_reservation(h, vma, addr);
  979. if (chg < 0)
  980. return ERR_PTR(-ENOMEM);
  981. if (chg)
  982. if (hugepage_subpool_get_pages(spool, chg))
  983. return ERR_PTR(-ENOSPC);
  984. ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
  985. if (ret) {
  986. hugepage_subpool_put_pages(spool, chg);
  987. return ERR_PTR(-ENOSPC);
  988. }
  989. spin_lock(&hugetlb_lock);
  990. page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve);
  991. spin_unlock(&hugetlb_lock);
  992. if (!page) {
  993. page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
  994. if (!page) {
  995. hugetlb_cgroup_uncharge_cgroup(idx,
  996. pages_per_huge_page(h),
  997. h_cg);
  998. hugepage_subpool_put_pages(spool, chg);
  999. return ERR_PTR(-ENOSPC);
  1000. }
  1001. spin_lock(&hugetlb_lock);
  1002. list_move(&page->lru, &h->hugepage_activelist);
  1003. spin_unlock(&hugetlb_lock);
  1004. }
  1005. set_page_private(page, (unsigned long)spool);
  1006. vma_commit_reservation(h, vma, addr);
  1007. /* update page cgroup details */
  1008. hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page);
  1009. return page;
  1010. }
  1011. int __weak alloc_bootmem_huge_page(struct hstate *h)
  1012. {
  1013. struct huge_bootmem_page *m;
  1014. int nr_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
  1015. while (nr_nodes) {
  1016. void *addr;
  1017. addr = __alloc_bootmem_node_nopanic(
  1018. NODE_DATA(hstate_next_node_to_alloc(h,
  1019. &node_states[N_HIGH_MEMORY])),
  1020. huge_page_size(h), huge_page_size(h), 0);
  1021. if (addr) {
  1022. /*
  1023. * Use the beginning of the huge page to store the
  1024. * huge_bootmem_page struct (until gather_bootmem
  1025. * puts them into the mem_map).
  1026. */
  1027. m = addr;
  1028. goto found;
  1029. }
  1030. nr_nodes--;
  1031. }
  1032. return 0;
  1033. found:
  1034. BUG_ON((unsigned long)virt_to_phys(m) & (huge_page_size(h) - 1));
  1035. /* Put them into a private list first because mem_map is not up yet */
  1036. list_add(&m->list, &huge_boot_pages);
  1037. m->hstate = h;
  1038. return 1;
  1039. }
  1040. static void prep_compound_huge_page(struct page *page, int order)
  1041. {
  1042. if (unlikely(order > (MAX_ORDER - 1)))
  1043. prep_compound_gigantic_page(page, order);
  1044. else
  1045. prep_compound_page(page, order);
  1046. }
  1047. /* Put bootmem huge pages into the standard lists after mem_map is up */
  1048. static void __init gather_bootmem_prealloc(void)
  1049. {
  1050. struct huge_bootmem_page *m;
  1051. list_for_each_entry(m, &huge_boot_pages, list) {
  1052. struct hstate *h = m->hstate;
  1053. struct page *page;
  1054. #ifdef CONFIG_HIGHMEM
  1055. page = pfn_to_page(m->phys >> PAGE_SHIFT);
  1056. free_bootmem_late((unsigned long)m,
  1057. sizeof(struct huge_bootmem_page));
  1058. #else
  1059. page = virt_to_page(m);
  1060. #endif
  1061. __ClearPageReserved(page);
  1062. WARN_ON(page_count(page) != 1);
  1063. prep_compound_huge_page(page, h->order);
  1064. prep_new_huge_page(h, page, page_to_nid(page));
  1065. /*
  1066. * If we had gigantic hugepages allocated at boot time, we need
  1067. * to restore the 'stolen' pages to totalram_pages in order to
  1068. * fix confusing memory reports from free(1) and another
  1069. * side-effects, like CommitLimit going negative.
  1070. */
  1071. if (h->order > (MAX_ORDER - 1))
  1072. totalram_pages += 1 << h->order;
  1073. }
  1074. }
  1075. static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
  1076. {
  1077. unsigned long i;
  1078. for (i = 0; i < h->max_huge_pages; ++i) {
  1079. if (h->order >= MAX_ORDER) {
  1080. if (!alloc_bootmem_huge_page(h))
  1081. break;
  1082. } else if (!alloc_fresh_huge_page(h,
  1083. &node_states[N_HIGH_MEMORY]))
  1084. break;
  1085. }
  1086. h->max_huge_pages = i;
  1087. }
  1088. static void __init hugetlb_init_hstates(void)
  1089. {
  1090. struct hstate *h;
  1091. for_each_hstate(h) {
  1092. /* oversize hugepages were init'ed in early boot */
  1093. if (h->order < MAX_ORDER)
  1094. hugetlb_hstate_alloc_pages(h);
  1095. }
  1096. }
  1097. static char * __init memfmt(char *buf, unsigned long n)
  1098. {
  1099. if (n >= (1UL << 30))
  1100. sprintf(buf, "%lu GB", n >> 30);
  1101. else if (n >= (1UL << 20))
  1102. sprintf(buf, "%lu MB", n >> 20);
  1103. else
  1104. sprintf(buf, "%lu KB", n >> 10);
  1105. return buf;
  1106. }
  1107. static void __init report_hugepages(void)
  1108. {
  1109. struct hstate *h;
  1110. for_each_hstate(h) {
  1111. char buf[32];
  1112. printk(KERN_INFO "HugeTLB registered %s page size, "
  1113. "pre-allocated %ld pages\n",
  1114. memfmt(buf, huge_page_size(h)),
  1115. h->free_huge_pages);
  1116. }
  1117. }
  1118. #ifdef CONFIG_HIGHMEM
  1119. static void try_to_free_low(struct hstate *h, unsigned long count,
  1120. nodemask_t *nodes_allowed)
  1121. {
  1122. int i;
  1123. if (h->order >= MAX_ORDER)
  1124. return;
  1125. for_each_node_mask(i, *nodes_allowed) {
  1126. struct page *page, *next;
  1127. struct list_head *freel = &h->hugepage_freelists[i];
  1128. list_for_each_entry_safe(page, next, freel, lru) {
  1129. if (count >= h->nr_huge_pages)
  1130. return;
  1131. if (PageHighMem(page))
  1132. continue;
  1133. list_del(&page->lru);
  1134. update_and_free_page(h, page);
  1135. h->free_huge_pages--;
  1136. h->free_huge_pages_node[page_to_nid(page)]--;
  1137. }
  1138. }
  1139. }
  1140. #else
  1141. static inline void try_to_free_low(struct hstate *h, unsigned long count,
  1142. nodemask_t *nodes_allowed)
  1143. {
  1144. }
  1145. #endif
  1146. /*
  1147. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  1148. * balanced by operating on them in a round-robin fashion.
  1149. * Returns 1 if an adjustment was made.
  1150. */
  1151. static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
  1152. int delta)
  1153. {
  1154. int start_nid, next_nid;
  1155. int ret = 0;
  1156. VM_BUG_ON(delta != -1 && delta != 1);
  1157. if (delta < 0)
  1158. start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
  1159. else
  1160. start_nid = hstate_next_node_to_free(h, nodes_allowed);
  1161. next_nid = start_nid;
  1162. do {
  1163. int nid = next_nid;
  1164. if (delta < 0) {
  1165. /*
  1166. * To shrink on this node, there must be a surplus page
  1167. */
  1168. if (!h->surplus_huge_pages_node[nid]) {
  1169. next_nid = hstate_next_node_to_alloc(h,
  1170. nodes_allowed);
  1171. continue;
  1172. }
  1173. }
  1174. if (delta > 0) {
  1175. /*
  1176. * Surplus cannot exceed the total number of pages
  1177. */
  1178. if (h->surplus_huge_pages_node[nid] >=
  1179. h->nr_huge_pages_node[nid]) {
  1180. next_nid = hstate_next_node_to_free(h,
  1181. nodes_allowed);
  1182. continue;
  1183. }
  1184. }
  1185. h->surplus_huge_pages += delta;
  1186. h->surplus_huge_pages_node[nid] += delta;
  1187. ret = 1;
  1188. break;
  1189. } while (next_nid != start_nid);
  1190. return ret;
  1191. }
  1192. #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
  1193. static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
  1194. nodemask_t *nodes_allowed)
  1195. {
  1196. unsigned long min_count, ret;
  1197. if (h->order >= MAX_ORDER)
  1198. return h->max_huge_pages;
  1199. /*
  1200. * Increase the pool size
  1201. * First take pages out of surplus state. Then make up the
  1202. * remaining difference by allocating fresh huge pages.
  1203. *
  1204. * We might race with alloc_buddy_huge_page() here and be unable
  1205. * to convert a surplus huge page to a normal huge page. That is
  1206. * not critical, though, it just means the overall size of the
  1207. * pool might be one hugepage larger than it needs to be, but
  1208. * within all the constraints specified by the sysctls.
  1209. */
  1210. spin_lock(&hugetlb_lock);
  1211. while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
  1212. if (!adjust_pool_surplus(h, nodes_allowed, -1))
  1213. break;
  1214. }
  1215. while (count > persistent_huge_pages(h)) {
  1216. /*
  1217. * If this allocation races such that we no longer need the
  1218. * page, free_huge_page will handle it by freeing the page
  1219. * and reducing the surplus.
  1220. */
  1221. spin_unlock(&hugetlb_lock);
  1222. ret = alloc_fresh_huge_page(h, nodes_allowed);
  1223. spin_lock(&hugetlb_lock);
  1224. if (!ret)
  1225. goto out;
  1226. /* Bail for signals. Probably ctrl-c from user */
  1227. if (signal_pending(current))
  1228. goto out;
  1229. }
  1230. /*
  1231. * Decrease the pool size
  1232. * First return free pages to the buddy allocator (being careful
  1233. * to keep enough around to satisfy reservations). Then place
  1234. * pages into surplus state as needed so the pool will shrink
  1235. * to the desired size as pages become free.
  1236. *
  1237. * By placing pages into the surplus state independent of the
  1238. * overcommit value, we are allowing the surplus pool size to
  1239. * exceed overcommit. There are few sane options here. Since
  1240. * alloc_buddy_huge_page() is checking the global counter,
  1241. * though, we'll note that we're not allowed to exceed surplus
  1242. * and won't grow the pool anywhere else. Not until one of the
  1243. * sysctls are changed, or the surplus pages go out of use.
  1244. */
  1245. min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
  1246. min_count = max(count, min_count);
  1247. try_to_free_low(h, min_count, nodes_allowed);
  1248. while (min_count < persistent_huge_pages(h)) {
  1249. if (!free_pool_huge_page(h, nodes_allowed, 0))
  1250. break;
  1251. }
  1252. while (count < persistent_huge_pages(h)) {
  1253. if (!adjust_pool_surplus(h, nodes_allowed, 1))
  1254. break;
  1255. }
  1256. out:
  1257. ret = persistent_huge_pages(h);
  1258. spin_unlock(&hugetlb_lock);
  1259. return ret;
  1260. }
  1261. #define HSTATE_ATTR_RO(_name) \
  1262. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  1263. #define HSTATE_ATTR(_name) \
  1264. static struct kobj_attribute _name##_attr = \
  1265. __ATTR(_name, 0644, _name##_show, _name##_store)
  1266. static struct kobject *hugepages_kobj;
  1267. static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  1268. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
  1269. static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
  1270. {
  1271. int i;
  1272. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  1273. if (hstate_kobjs[i] == kobj) {
  1274. if (nidp)
  1275. *nidp = NUMA_NO_NODE;
  1276. return &hstates[i];
  1277. }
  1278. return kobj_to_node_hstate(kobj, nidp);
  1279. }
  1280. static ssize_t nr_hugepages_show_common(struct kobject *kobj,
  1281. struct kobj_attribute *attr, char *buf)
  1282. {
  1283. struct hstate *h;
  1284. unsigned long nr_huge_pages;
  1285. int nid;
  1286. h = kobj_to_hstate(kobj, &nid);
  1287. if (nid == NUMA_NO_NODE)
  1288. nr_huge_pages = h->nr_huge_pages;
  1289. else
  1290. nr_huge_pages = h->nr_huge_pages_node[nid];
  1291. return sprintf(buf, "%lu\n", nr_huge_pages);
  1292. }
  1293. static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
  1294. struct kobject *kobj, struct kobj_attribute *attr,
  1295. const char *buf, size_t len)
  1296. {
  1297. int err;
  1298. int nid;
  1299. unsigned long count;
  1300. struct hstate *h;
  1301. NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
  1302. err = strict_strtoul(buf, 10, &count);
  1303. if (err)
  1304. goto out;
  1305. h = kobj_to_hstate(kobj, &nid);
  1306. if (h->order >= MAX_ORDER) {
  1307. err = -EINVAL;
  1308. goto out;
  1309. }
  1310. if (nid == NUMA_NO_NODE) {
  1311. /*
  1312. * global hstate attribute
  1313. */
  1314. if (!(obey_mempolicy &&
  1315. init_nodemask_of_mempolicy(nodes_allowed))) {
  1316. NODEMASK_FREE(nodes_allowed);
  1317. nodes_allowed = &node_states[N_HIGH_MEMORY];
  1318. }
  1319. } else if (nodes_allowed) {
  1320. /*
  1321. * per node hstate attribute: adjust count to global,
  1322. * but restrict alloc/free to the specified node.
  1323. */
  1324. count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
  1325. init_nodemask_of_node(nodes_allowed, nid);
  1326. } else
  1327. nodes_allowed = &node_states[N_HIGH_MEMORY];
  1328. h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed);
  1329. if (nodes_allowed != &node_states[N_HIGH_MEMORY])
  1330. NODEMASK_FREE(nodes_allowed);
  1331. return len;
  1332. out:
  1333. NODEMASK_FREE(nodes_allowed);
  1334. return err;
  1335. }
  1336. static ssize_t nr_hugepages_show(struct kobject *kobj,
  1337. struct kobj_attribute *attr, char *buf)
  1338. {
  1339. return nr_hugepages_show_common(kobj, attr, buf);
  1340. }
  1341. static ssize_t nr_hugepages_store(struct kobject *kobj,
  1342. struct kobj_attribute *attr, const char *buf, size_t len)
  1343. {
  1344. return nr_hugepages_store_common(false, kobj, attr, buf, len);
  1345. }
  1346. HSTATE_ATTR(nr_hugepages);
  1347. #ifdef CONFIG_NUMA
  1348. /*
  1349. * hstate attribute for optionally mempolicy-based constraint on persistent
  1350. * huge page alloc/free.
  1351. */
  1352. static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
  1353. struct kobj_attribute *attr, char *buf)
  1354. {
  1355. return nr_hugepages_show_common(kobj, attr, buf);
  1356. }
  1357. static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
  1358. struct kobj_attribute *attr, const char *buf, size_t len)
  1359. {
  1360. return nr_hugepages_store_common(true, kobj, attr, buf, len);
  1361. }
  1362. HSTATE_ATTR(nr_hugepages_mempolicy);
  1363. #endif
  1364. static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
  1365. struct kobj_attribute *attr, char *buf)
  1366. {
  1367. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1368. return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
  1369. }
  1370. static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
  1371. struct kobj_attribute *attr, const char *buf, size_t count)
  1372. {
  1373. int err;
  1374. unsigned long input;
  1375. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1376. if (h->order >= MAX_ORDER)
  1377. return -EINVAL;
  1378. err = strict_strtoul(buf, 10, &input);
  1379. if (err)
  1380. return err;
  1381. spin_lock(&hugetlb_lock);
  1382. h->nr_overcommit_huge_pages = input;
  1383. spin_unlock(&hugetlb_lock);
  1384. return count;
  1385. }
  1386. HSTATE_ATTR(nr_overcommit_hugepages);
  1387. static ssize_t free_hugepages_show(struct kobject *kobj,
  1388. struct kobj_attribute *attr, char *buf)
  1389. {
  1390. struct hstate *h;
  1391. unsigned long free_huge_pages;
  1392. int nid;
  1393. h = kobj_to_hstate(kobj, &nid);
  1394. if (nid == NUMA_NO_NODE)
  1395. free_huge_pages = h->free_huge_pages;
  1396. else
  1397. free_huge_pages = h->free_huge_pages_node[nid];
  1398. return sprintf(buf, "%lu\n", free_huge_pages);
  1399. }
  1400. HSTATE_ATTR_RO(free_hugepages);
  1401. static ssize_t resv_hugepages_show(struct kobject *kobj,
  1402. struct kobj_attribute *attr, char *buf)
  1403. {
  1404. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1405. return sprintf(buf, "%lu\n", h->resv_huge_pages);
  1406. }
  1407. HSTATE_ATTR_RO(resv_hugepages);
  1408. static ssize_t surplus_hugepages_show(struct kobject *kobj,
  1409. struct kobj_attribute *attr, char *buf)
  1410. {
  1411. struct hstate *h;
  1412. unsigned long surplus_huge_pages;
  1413. int nid;
  1414. h = kobj_to_hstate(kobj, &nid);
  1415. if (nid == NUMA_NO_NODE)
  1416. surplus_huge_pages = h->surplus_huge_pages;
  1417. else
  1418. surplus_huge_pages = h->surplus_huge_pages_node[nid];
  1419. return sprintf(buf, "%lu\n", surplus_huge_pages);
  1420. }
  1421. HSTATE_ATTR_RO(surplus_hugepages);
  1422. static struct attribute *hstate_attrs[] = {
  1423. &nr_hugepages_attr.attr,
  1424. &nr_overcommit_hugepages_attr.attr,
  1425. &free_hugepages_attr.attr,
  1426. &resv_hugepages_attr.attr,
  1427. &surplus_hugepages_attr.attr,
  1428. #ifdef CONFIG_NUMA
  1429. &nr_hugepages_mempolicy_attr.attr,
  1430. #endif
  1431. NULL,
  1432. };
  1433. static struct attribute_group hstate_attr_group = {
  1434. .attrs = hstate_attrs,
  1435. };
  1436. static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
  1437. struct kobject **hstate_kobjs,
  1438. struct attribute_group *hstate_attr_group)
  1439. {
  1440. int retval;
  1441. int hi = hstate_index(h);
  1442. hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
  1443. if (!hstate_kobjs[hi])
  1444. return -ENOMEM;
  1445. retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
  1446. if (retval)
  1447. kobject_put(hstate_kobjs[hi]);
  1448. return retval;
  1449. }
  1450. static void __init hugetlb_sysfs_init(void)
  1451. {
  1452. struct hstate *h;
  1453. int err;
  1454. hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
  1455. if (!hugepages_kobj)
  1456. return;
  1457. for_each_hstate(h) {
  1458. err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
  1459. hstate_kobjs, &hstate_attr_group);
  1460. if (err)
  1461. printk(KERN_ERR "Hugetlb: Unable to add hstate %s",
  1462. h->name);
  1463. }
  1464. }
  1465. #ifdef CONFIG_NUMA
  1466. /*
  1467. * node_hstate/s - associate per node hstate attributes, via their kobjects,
  1468. * with node devices in node_devices[] using a parallel array. The array
  1469. * index of a node device or _hstate == node id.
  1470. * This is here to avoid any static dependency of the node device driver, in
  1471. * the base kernel, on the hugetlb module.
  1472. */
  1473. struct node_hstate {
  1474. struct kobject *hugepages_kobj;
  1475. struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  1476. };
  1477. struct node_hstate node_hstates[MAX_NUMNODES];
  1478. /*
  1479. * A subset of global hstate attributes for node devices
  1480. */
  1481. static struct attribute *per_node_hstate_attrs[] = {
  1482. &nr_hugepages_attr.attr,
  1483. &free_hugepages_attr.attr,
  1484. &surplus_hugepages_attr.attr,
  1485. NULL,
  1486. };
  1487. static struct attribute_group per_node_hstate_attr_group = {
  1488. .attrs = per_node_hstate_attrs,
  1489. };
  1490. /*
  1491. * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
  1492. * Returns node id via non-NULL nidp.
  1493. */
  1494. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  1495. {
  1496. int nid;
  1497. for (nid = 0; nid < nr_node_ids; nid++) {
  1498. struct node_hstate *nhs = &node_hstates[nid];
  1499. int i;
  1500. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  1501. if (nhs->hstate_kobjs[i] == kobj) {
  1502. if (nidp)
  1503. *nidp = nid;
  1504. return &hstates[i];
  1505. }
  1506. }
  1507. BUG();
  1508. return NULL;
  1509. }
  1510. /*
  1511. * Unregister hstate attributes from a single node device.
  1512. * No-op if no hstate attributes attached.
  1513. */
  1514. void hugetlb_unregister_node(struct node *node)
  1515. {
  1516. struct hstate *h;
  1517. struct node_hstate *nhs = &node_hstates[node->dev.id];
  1518. if (!nhs->hugepages_kobj)
  1519. return; /* no hstate attributes */
  1520. for_each_hstate(h) {
  1521. int idx = hstate_index(h);
  1522. if (nhs->hstate_kobjs[idx]) {
  1523. kobject_put(nhs->hstate_kobjs[idx]);
  1524. nhs->hstate_kobjs[idx] = NULL;
  1525. }
  1526. }
  1527. kobject_put(nhs->hugepages_kobj);
  1528. nhs->hugepages_kobj = NULL;
  1529. }
  1530. /*
  1531. * hugetlb module exit: unregister hstate attributes from node devices
  1532. * that have them.
  1533. */
  1534. static void hugetlb_unregister_all_nodes(void)
  1535. {
  1536. int nid;
  1537. /*
  1538. * disable node device registrations.
  1539. */
  1540. register_hugetlbfs_with_node(NULL, NULL);
  1541. /*
  1542. * remove hstate attributes from any nodes that have them.
  1543. */
  1544. for (nid = 0; nid < nr_node_ids; nid++)
  1545. hugetlb_unregister_node(&node_devices[nid]);
  1546. }
  1547. /*
  1548. * Register hstate attributes for a single node device.
  1549. * No-op if attributes already registered.
  1550. */
  1551. void hugetlb_register_node(struct node *node)
  1552. {
  1553. struct hstate *h;
  1554. struct node_hstate *nhs = &node_hstates[node->dev.id];
  1555. int err;
  1556. if (nhs->hugepages_kobj)
  1557. return; /* already allocated */
  1558. nhs->hugepages_kobj = kobject_create_and_add("hugepages",
  1559. &node->dev.kobj);
  1560. if (!nhs->hugepages_kobj)
  1561. return;
  1562. for_each_hstate(h) {
  1563. err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
  1564. nhs->hstate_kobjs,
  1565. &per_node_hstate_attr_group);
  1566. if (err) {
  1567. printk(KERN_ERR "Hugetlb: Unable to add hstate %s"
  1568. " for node %d\n",
  1569. h->name, node->dev.id);
  1570. hugetlb_unregister_node(node);
  1571. break;
  1572. }
  1573. }
  1574. }
  1575. /*
  1576. * hugetlb init time: register hstate attributes for all registered node
  1577. * devices of nodes that have memory. All on-line nodes should have
  1578. * registered their associated device by this time.
  1579. */
  1580. static void hugetlb_register_all_nodes(void)
  1581. {
  1582. int nid;
  1583. for_each_node_state(nid, N_HIGH_MEMORY) {
  1584. struct node *node = &node_devices[nid];
  1585. if (node->dev.id == nid)
  1586. hugetlb_register_node(node);
  1587. }
  1588. /*
  1589. * Let the node device driver know we're here so it can
  1590. * [un]register hstate attributes on node hotplug.
  1591. */
  1592. register_hugetlbfs_with_node(hugetlb_register_node,
  1593. hugetlb_unregister_node);
  1594. }
  1595. #else /* !CONFIG_NUMA */
  1596. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  1597. {
  1598. BUG();
  1599. if (nidp)
  1600. *nidp = -1;
  1601. return NULL;
  1602. }
  1603. static void hugetlb_unregister_all_nodes(void) { }
  1604. static void hugetlb_register_all_nodes(void) { }
  1605. #endif
  1606. static void __exit hugetlb_exit(void)
  1607. {
  1608. struct hstate *h;
  1609. hugetlb_unregister_all_nodes();
  1610. for_each_hstate(h) {
  1611. kobject_put(hstate_kobjs[hstate_index(h)]);
  1612. }
  1613. kobject_put(hugepages_kobj);
  1614. }
  1615. module_exit(hugetlb_exit);
  1616. static int __init hugetlb_init(void)
  1617. {
  1618. /* Some platform decide whether they support huge pages at boot
  1619. * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
  1620. * there is no such support
  1621. */
  1622. if (HPAGE_SHIFT == 0)
  1623. return 0;
  1624. if (!size_to_hstate(default_hstate_size)) {
  1625. default_hstate_size = HPAGE_SIZE;
  1626. if (!size_to_hstate(default_hstate_size))
  1627. hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
  1628. }
  1629. default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
  1630. if (default_hstate_max_huge_pages)
  1631. default_hstate.max_huge_pages = default_hstate_max_huge_pages;
  1632. hugetlb_init_hstates();
  1633. gather_bootmem_prealloc();
  1634. report_hugepages();
  1635. hugetlb_sysfs_init();
  1636. hugetlb_register_all_nodes();
  1637. return 0;
  1638. }
  1639. module_init(hugetlb_init);
  1640. /* Should be called on processing a hugepagesz=... option */
  1641. void __init hugetlb_add_hstate(unsigned order)
  1642. {
  1643. struct hstate *h;
  1644. unsigned long i;
  1645. if (size_to_hstate(PAGE_SIZE << order)) {
  1646. printk(KERN_WARNING "hugepagesz= specified twice, ignoring\n");
  1647. return;
  1648. }
  1649. BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
  1650. BUG_ON(order == 0);
  1651. h = &hstates[hugetlb_max_hstate++];
  1652. h->order = order;
  1653. h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
  1654. h->nr_huge_pages = 0;
  1655. h->free_huge_pages = 0;
  1656. for (i = 0; i < MAX_NUMNODES; ++i)
  1657. INIT_LIST_HEAD(&h->hugepage_freelists[i]);
  1658. INIT_LIST_HEAD(&h->hugepage_activelist);
  1659. h->next_nid_to_alloc = first_node(node_states[N_HIGH_MEMORY]);
  1660. h->next_nid_to_free = first_node(node_states[N_HIGH_MEMORY]);
  1661. snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
  1662. huge_page_size(h)/1024);
  1663. /*
  1664. * Add cgroup control files only if the huge page consists
  1665. * of more than two normal pages. This is because we use
  1666. * page[2].lru.next for storing cgoup details.
  1667. */
  1668. if (order >= HUGETLB_CGROUP_MIN_ORDER)
  1669. hugetlb_cgroup_file_init(hugetlb_max_hstate - 1);
  1670. parsed_hstate = h;
  1671. }
  1672. static int __init hugetlb_nrpages_setup(char *s)
  1673. {
  1674. unsigned long *mhp;
  1675. static unsigned long *last_mhp;
  1676. /*
  1677. * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
  1678. * so this hugepages= parameter goes to the "default hstate".
  1679. */
  1680. if (!hugetlb_max_hstate)
  1681. mhp = &default_hstate_max_huge_pages;
  1682. else
  1683. mhp = &parsed_hstate->max_huge_pages;
  1684. if (mhp == last_mhp) {
  1685. printk(KERN_WARNING "hugepages= specified twice without "
  1686. "interleaving hugepagesz=, ignoring\n");
  1687. return 1;
  1688. }
  1689. if (sscanf(s, "%lu", mhp) <= 0)
  1690. *mhp = 0;
  1691. /*
  1692. * Global state is always initialized later in hugetlb_init.
  1693. * But we need to allocate >= MAX_ORDER hstates here early to still
  1694. * use the bootmem allocator.
  1695. */
  1696. if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER)
  1697. hugetlb_hstate_alloc_pages(parsed_hstate);
  1698. last_mhp = mhp;
  1699. return 1;
  1700. }
  1701. __setup("hugepages=", hugetlb_nrpages_setup);
  1702. static int __init hugetlb_default_setup(char *s)
  1703. {
  1704. default_hstate_size = memparse(s, &s);
  1705. return 1;
  1706. }
  1707. __setup("default_hugepagesz=", hugetlb_default_setup);
  1708. static unsigned int cpuset_mems_nr(unsigned int *array)
  1709. {
  1710. int node;
  1711. unsigned int nr = 0;
  1712. for_each_node_mask(node, cpuset_current_mems_allowed)
  1713. nr += array[node];
  1714. return nr;
  1715. }
  1716. #ifdef CONFIG_SYSCTL
  1717. static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
  1718. struct ctl_table *table, int write,
  1719. void __user *buffer, size_t *length, loff_t *ppos)
  1720. {
  1721. struct hstate *h = &default_hstate;
  1722. unsigned long tmp;
  1723. int ret;
  1724. tmp = h->max_huge_pages;
  1725. if (write && h->order >= MAX_ORDER)
  1726. return -EINVAL;
  1727. table->data = &tmp;
  1728. table->maxlen = sizeof(unsigned long);
  1729. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  1730. if (ret)
  1731. goto out;
  1732. if (write) {
  1733. NODEMASK_ALLOC(nodemask_t, nodes_allowed,
  1734. GFP_KERNEL | __GFP_NORETRY);
  1735. if (!(obey_mempolicy &&
  1736. init_nodemask_of_mempolicy(nodes_allowed))) {
  1737. NODEMASK_FREE(nodes_allowed);
  1738. nodes_allowed = &node_states[N_HIGH_MEMORY];
  1739. }
  1740. h->max_huge_pages = set_max_huge_pages(h, tmp, nodes_allowed);
  1741. if (nodes_allowed != &node_states[N_HIGH_MEMORY])
  1742. NODEMASK_FREE(nodes_allowed);
  1743. }
  1744. out:
  1745. return ret;
  1746. }
  1747. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  1748. void __user *buffer, size_t *length, loff_t *ppos)
  1749. {
  1750. return hugetlb_sysctl_handler_common(false, table, write,
  1751. buffer, length, ppos);
  1752. }
  1753. #ifdef CONFIG_NUMA
  1754. int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
  1755. void __user *buffer, size_t *length, loff_t *ppos)
  1756. {
  1757. return hugetlb_sysctl_handler_common(true, table, write,
  1758. buffer, length, ppos);
  1759. }
  1760. #endif /* CONFIG_NUMA */
  1761. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  1762. void __user *buffer,
  1763. size_t *length, loff_t *ppos)
  1764. {
  1765. proc_dointvec(table, write, buffer, length, ppos);
  1766. if (hugepages_treat_as_movable)
  1767. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  1768. else
  1769. htlb_alloc_mask = GFP_HIGHUSER;
  1770. return 0;
  1771. }
  1772. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  1773. void __user *buffer,
  1774. size_t *length, loff_t *ppos)
  1775. {
  1776. struct hstate *h = &default_hstate;
  1777. unsigned long tmp;
  1778. int ret;
  1779. tmp = h->nr_overcommit_huge_pages;
  1780. if (write && h->order >= MAX_ORDER)
  1781. return -EINVAL;
  1782. table->data = &tmp;
  1783. table->maxlen = sizeof(unsigned long);
  1784. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  1785. if (ret)
  1786. goto out;
  1787. if (write) {
  1788. spin_lock(&hugetlb_lock);
  1789. h->nr_overcommit_huge_pages = tmp;
  1790. spin_unlock(&hugetlb_lock);
  1791. }
  1792. out:
  1793. return ret;
  1794. }
  1795. #endif /* CONFIG_SYSCTL */
  1796. void hugetlb_report_meminfo(struct seq_file *m)
  1797. {
  1798. struct hstate *h = &default_hstate;
  1799. seq_printf(m,
  1800. "HugePages_Total: %5lu\n"
  1801. "HugePages_Free: %5lu\n"
  1802. "HugePages_Rsvd: %5lu\n"
  1803. "HugePages_Surp: %5lu\n"
  1804. "Hugepagesize: %8lu kB\n",
  1805. h->nr_huge_pages,
  1806. h->free_huge_pages,
  1807. h->resv_huge_pages,
  1808. h->surplus_huge_pages,
  1809. 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
  1810. }
  1811. int hugetlb_report_node_meminfo(int nid, char *buf)
  1812. {
  1813. struct hstate *h = &default_hstate;
  1814. return sprintf(buf,
  1815. "Node %d HugePages_Total: %5u\n"
  1816. "Node %d HugePages_Free: %5u\n"
  1817. "Node %d HugePages_Surp: %5u\n",
  1818. nid, h->nr_huge_pages_node[nid],
  1819. nid, h->free_huge_pages_node[nid],
  1820. nid, h->surplus_huge_pages_node[nid]);
  1821. }
  1822. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  1823. unsigned long hugetlb_total_pages(void)
  1824. {
  1825. struct hstate *h = &default_hstate;
  1826. return h->nr_huge_pages * pages_per_huge_page(h);
  1827. }
  1828. static int hugetlb_acct_memory(struct hstate *h, long delta)
  1829. {
  1830. int ret = -ENOMEM;
  1831. spin_lock(&hugetlb_lock);
  1832. /*
  1833. * When cpuset is configured, it breaks the strict hugetlb page
  1834. * reservation as the accounting is done on a global variable. Such
  1835. * reservation is completely rubbish in the presence of cpuset because
  1836. * the reservation is not checked against page availability for the
  1837. * current cpuset. Application can still potentially OOM'ed by kernel
  1838. * with lack of free htlb page in cpuset that the task is in.
  1839. * Attempt to enforce strict accounting with cpuset is almost
  1840. * impossible (or too ugly) because cpuset is too fluid that
  1841. * task or memory node can be dynamically moved between cpusets.
  1842. *
  1843. * The change of semantics for shared hugetlb mapping with cpuset is
  1844. * undesirable. However, in order to preserve some of the semantics,
  1845. * we fall back to check against current free page availability as
  1846. * a best attempt and hopefully to minimize the impact of changing
  1847. * semantics that cpuset has.
  1848. */
  1849. if (delta > 0) {
  1850. if (gather_surplus_pages(h, delta) < 0)
  1851. goto out;
  1852. if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
  1853. return_unused_surplus_pages(h, delta);
  1854. goto out;
  1855. }
  1856. }
  1857. ret = 0;
  1858. if (delta < 0)
  1859. return_unused_surplus_pages(h, (unsigned long) -delta);
  1860. out:
  1861. spin_unlock(&hugetlb_lock);
  1862. return ret;
  1863. }
  1864. static void hugetlb_vm_op_open(struct vm_area_struct *vma)
  1865. {
  1866. struct resv_map *reservations = vma_resv_map(vma);
  1867. /*
  1868. * This new VMA should share its siblings reservation map if present.
  1869. * The VMA will only ever have a valid reservation map pointer where
  1870. * it is being copied for another still existing VMA. As that VMA
  1871. * has a reference to the reservation map it cannot disappear until
  1872. * after this open call completes. It is therefore safe to take a
  1873. * new reference here without additional locking.
  1874. */
  1875. if (reservations)
  1876. kref_get(&reservations->refs);
  1877. }
  1878. static void resv_map_put(struct vm_area_struct *vma)
  1879. {
  1880. struct resv_map *reservations = vma_resv_map(vma);
  1881. if (!reservations)
  1882. return;
  1883. kref_put(&reservations->refs, resv_map_release);
  1884. }
  1885. static void hugetlb_vm_op_close(struct vm_area_struct *vma)
  1886. {
  1887. struct hstate *h = hstate_vma(vma);
  1888. struct resv_map *reservations = vma_resv_map(vma);
  1889. struct hugepage_subpool *spool = subpool_vma(vma);
  1890. unsigned long reserve;
  1891. unsigned long start;
  1892. unsigned long end;
  1893. if (reservations) {
  1894. start = vma_hugecache_offset(h, vma, vma->vm_start);
  1895. end = vma_hugecache_offset(h, vma, vma->vm_end);
  1896. reserve = (end - start) -
  1897. region_count(&reservations->regions, start, end);
  1898. resv_map_put(vma);
  1899. if (reserve) {
  1900. hugetlb_acct_memory(h, -reserve);
  1901. hugepage_subpool_put_pages(spool, reserve);
  1902. }
  1903. }
  1904. }
  1905. /*
  1906. * We cannot handle pagefaults against hugetlb pages at all. They cause
  1907. * handle_mm_fault() to try to instantiate regular-sized pages in the
  1908. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  1909. * this far.
  1910. */
  1911. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1912. {
  1913. BUG();
  1914. return 0;
  1915. }
  1916. const struct vm_operations_struct hugetlb_vm_ops = {
  1917. .fault = hugetlb_vm_op_fault,
  1918. .open = hugetlb_vm_op_open,
  1919. .close = hugetlb_vm_op_close,
  1920. };
  1921. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  1922. int writable)
  1923. {
  1924. pte_t entry;
  1925. if (writable) {
  1926. entry =
  1927. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  1928. } else {
  1929. entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  1930. }
  1931. entry = pte_mkyoung(entry);
  1932. entry = pte_mkhuge(entry);
  1933. entry = arch_make_huge_pte(entry, vma, page, writable);
  1934. return entry;
  1935. }
  1936. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  1937. unsigned long address, pte_t *ptep)
  1938. {
  1939. pte_t entry;
  1940. entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
  1941. if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
  1942. update_mmu_cache(vma, address, ptep);
  1943. }
  1944. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  1945. struct vm_area_struct *vma)
  1946. {
  1947. pte_t *src_pte, *dst_pte, entry;
  1948. struct page *ptepage;
  1949. unsigned long addr;
  1950. int cow;
  1951. struct hstate *h = hstate_vma(vma);
  1952. unsigned long sz = huge_page_size(h);
  1953. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  1954. for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
  1955. src_pte = huge_pte_offset(src, addr);
  1956. if (!src_pte)
  1957. continue;
  1958. dst_pte = huge_pte_alloc(dst, addr, sz);
  1959. if (!dst_pte)
  1960. goto nomem;
  1961. /* If the pagetables are shared don't copy or take references */
  1962. if (dst_pte == src_pte)
  1963. continue;
  1964. spin_lock(&dst->page_table_lock);
  1965. spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
  1966. if (!huge_pte_none(huge_ptep_get(src_pte))) {
  1967. if (cow)
  1968. huge_ptep_set_wrprotect(src, addr, src_pte);
  1969. entry = huge_ptep_get(src_pte);
  1970. ptepage = pte_page(entry);
  1971. get_page(ptepage);
  1972. page_dup_rmap(ptepage);
  1973. set_huge_pte_at(dst, addr, dst_pte, entry);
  1974. }
  1975. spin_unlock(&src->page_table_lock);
  1976. spin_unlock(&dst->page_table_lock);
  1977. }
  1978. return 0;
  1979. nomem:
  1980. return -ENOMEM;
  1981. }
  1982. static int is_hugetlb_entry_migration(pte_t pte)
  1983. {
  1984. swp_entry_t swp;
  1985. if (huge_pte_none(pte) || pte_present(pte))
  1986. return 0;
  1987. swp = pte_to_swp_entry(pte);
  1988. if (non_swap_entry(swp) && is_migration_entry(swp))
  1989. return 1;
  1990. else
  1991. return 0;
  1992. }
  1993. static int is_hugetlb_entry_hwpoisoned(pte_t pte)
  1994. {
  1995. swp_entry_t swp;
  1996. if (huge_pte_none(pte) || pte_present(pte))
  1997. return 0;
  1998. swp = pte_to_swp_entry(pte);
  1999. if (non_swap_entry(swp) && is_hwpoison_entry(swp))
  2000. return 1;
  2001. else
  2002. return 0;
  2003. }
  2004. void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  2005. unsigned long start, unsigned long end,
  2006. struct page *ref_page)
  2007. {
  2008. int force_flush = 0;
  2009. struct mm_struct *mm = vma->vm_mm;
  2010. unsigned long address;
  2011. pte_t *ptep;
  2012. pte_t pte;
  2013. struct page *page;
  2014. struct hstate *h = hstate_vma(vma);
  2015. unsigned long sz = huge_page_size(h);
  2016. WARN_ON(!is_vm_hugetlb_page(vma));
  2017. BUG_ON(start & ~huge_page_mask(h));
  2018. BUG_ON(end & ~huge_page_mask(h));
  2019. tlb_start_vma(tlb, vma);
  2020. mmu_notifier_invalidate_range_start(mm, start, end);
  2021. again:
  2022. spin_lock(&mm->page_table_lock);
  2023. for (address = start; address < end; address += sz) {
  2024. ptep = huge_pte_offset(mm, address);
  2025. if (!ptep)
  2026. continue;
  2027. if (huge_pmd_unshare(mm, &address, ptep))
  2028. continue;
  2029. pte = huge_ptep_get(ptep);
  2030. if (huge_pte_none(pte))
  2031. continue;
  2032. /*
  2033. * HWPoisoned hugepage is already unmapped and dropped reference
  2034. */
  2035. if (unlikely(is_hugetlb_entry_hwpoisoned(pte)))
  2036. continue;
  2037. page = pte_page(pte);
  2038. /*
  2039. * If a reference page is supplied, it is because a specific
  2040. * page is being unmapped, not a range. Ensure the page we
  2041. * are about to unmap is the actual page of interest.
  2042. */
  2043. if (ref_page) {
  2044. if (page != ref_page)
  2045. continue;
  2046. /*
  2047. * Mark the VMA as having unmapped its page so that
  2048. * future faults in this VMA will fail rather than
  2049. * looking like data was lost
  2050. */
  2051. set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
  2052. }
  2053. pte = huge_ptep_get_and_clear(mm, address, ptep);
  2054. tlb_remove_tlb_entry(tlb, ptep, address);
  2055. if (pte_dirty(pte))
  2056. set_page_dirty(page);
  2057. page_remove_rmap(page);
  2058. force_flush = !__tlb_remove_page(tlb, page);
  2059. if (force_flush)
  2060. break;
  2061. /* Bail out after unmapping reference page if supplied */
  2062. if (ref_page)
  2063. break;
  2064. }
  2065. spin_unlock(&mm->page_table_lock);
  2066. /*
  2067. * mmu_gather ran out of room to batch pages, we break out of
  2068. * the PTE lock to avoid doing the potential expensive TLB invalidate
  2069. * and page-free while holding it.
  2070. */
  2071. if (force_flush) {
  2072. force_flush = 0;
  2073. tlb_flush_mmu(tlb);
  2074. if (address < end && !ref_page)
  2075. goto again;
  2076. }
  2077. mmu_notifier_invalidate_range_end(mm, start, end);
  2078. tlb_end_vma(tlb, vma);
  2079. }
  2080. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  2081. unsigned long end, struct page *ref_page)
  2082. {
  2083. struct mm_struct *mm;
  2084. struct mmu_gather tlb;
  2085. mm = vma->vm_mm;
  2086. tlb_gather_mmu(&tlb, mm, 0);
  2087. __unmap_hugepage_range(&tlb, vma, start, end, ref_page);
  2088. tlb_finish_mmu(&tlb, start, end);
  2089. }
  2090. /*
  2091. * This is called when the original mapper is failing to COW a MAP_PRIVATE
  2092. * mappping it owns the reserve page for. The intention is to unmap the page
  2093. * from other VMAs and let the children be SIGKILLed if they are faulting the
  2094. * same region.
  2095. */
  2096. static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
  2097. struct page *page, unsigned long address)
  2098. {
  2099. struct hstate *h = hstate_vma(vma);
  2100. struct vm_area_struct *iter_vma;
  2101. struct address_space *mapping;
  2102. struct prio_tree_iter iter;
  2103. pgoff_t pgoff;
  2104. /*
  2105. * vm_pgoff is in PAGE_SIZE units, hence the different calculation
  2106. * from page cache lookup which is in HPAGE_SIZE units.
  2107. */
  2108. address = address & huge_page_mask(h);
  2109. pgoff = vma_hugecache_offset(h, vma, address);
  2110. mapping = vma->vm_file->f_dentry->d_inode->i_mapping;
  2111. /*
  2112. * Take the mapping lock for the duration of the table walk. As
  2113. * this mapping should be shared between all the VMAs,
  2114. * __unmap_hugepage_range() is called as the lock is already held
  2115. */
  2116. mutex_lock(&mapping->i_mmap_mutex);
  2117. vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  2118. /* Do not unmap the current VMA */
  2119. if (iter_vma == vma)
  2120. continue;
  2121. /*
  2122. * Unmap the page from other VMAs without their own reserves.
  2123. * They get marked to be SIGKILLed if they fault in these
  2124. * areas. This is because a future no-page fault on this VMA
  2125. * could insert a zeroed page instead of the data existing
  2126. * from the time of fork. This would look like data corruption
  2127. */
  2128. if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
  2129. unmap_hugepage_range(iter_vma, address,
  2130. address + huge_page_size(h), page);
  2131. }
  2132. mutex_unlock(&mapping->i_mmap_mutex);
  2133. return 1;
  2134. }
  2135. /*
  2136. * Hugetlb_cow() should be called with page lock of the original hugepage held.
  2137. * Called with hugetlb_instantiation_mutex held and pte_page locked so we
  2138. * cannot race with other handlers or page migration.
  2139. * Keep the pte_same checks anyway to make transition from the mutex easier.
  2140. */
  2141. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  2142. unsigned long address, pte_t *ptep, pte_t pte,
  2143. struct page *pagecache_page)
  2144. {
  2145. struct hstate *h = hstate_vma(vma);
  2146. struct page *old_page, *new_page;
  2147. int avoidcopy;
  2148. int outside_reserve = 0;
  2149. old_page = pte_page(pte);
  2150. retry_avoidcopy:
  2151. /* If no-one else is actually using this page, avoid the copy
  2152. * and just make the page writable */
  2153. avoidcopy = (page_mapcount(old_page) == 1);
  2154. if (avoidcopy) {
  2155. if (PageAnon(old_page))
  2156. page_move_anon_rmap(old_page, vma, address);
  2157. set_huge_ptep_writable(vma, address, ptep);
  2158. return 0;
  2159. }
  2160. /*
  2161. * If the process that created a MAP_PRIVATE mapping is about to
  2162. * perform a COW due to a shared page count, attempt to satisfy
  2163. * the allocation without using the existing reserves. The pagecache
  2164. * page is used to determine if the reserve at this address was
  2165. * consumed or not. If reserves were used, a partial faulted mapping
  2166. * at the time of fork() could consume its reserves on COW instead
  2167. * of the full address range.
  2168. */
  2169. if (!(vma->vm_flags & VM_MAYSHARE) &&
  2170. is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
  2171. old_page != pagecache_page)
  2172. outside_reserve = 1;
  2173. page_cache_get(old_page);
  2174. /* Drop page_table_lock as buddy allocator may be called */
  2175. spin_unlock(&mm->page_table_lock);
  2176. new_page = alloc_huge_page(vma, address, outside_reserve);
  2177. if (IS_ERR(new_page)) {
  2178. long err = PTR_ERR(new_page);
  2179. page_cache_release(old_page);
  2180. /*
  2181. * If a process owning a MAP_PRIVATE mapping fails to COW,
  2182. * it is due to references held by a child and an insufficient
  2183. * huge page pool. To guarantee the original mappers
  2184. * reliability, unmap the page from child processes. The child
  2185. * may get SIGKILLed if it later faults.
  2186. */
  2187. if (outside_reserve) {
  2188. BUG_ON(huge_pte_none(pte));
  2189. if (unmap_ref_private(mm, vma, old_page, address)) {
  2190. BUG_ON(huge_pte_none(pte));
  2191. spin_lock(&mm->page_table_lock);
  2192. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  2193. if (likely(pte_same(huge_ptep_get(ptep), pte)))
  2194. goto retry_avoidcopy;
  2195. /*
  2196. * race occurs while re-acquiring page_table_lock, and
  2197. * our job is done.
  2198. */
  2199. return 0;
  2200. }
  2201. WARN_ON_ONCE(1);
  2202. }
  2203. /* Caller expects lock to be held */
  2204. spin_lock(&mm->page_table_lock);
  2205. if (err == -ENOMEM)
  2206. return VM_FAULT_OOM;
  2207. else
  2208. return VM_FAULT_SIGBUS;
  2209. }
  2210. /*
  2211. * When the original hugepage is shared one, it does not have
  2212. * anon_vma prepared.
  2213. */
  2214. if (unlikely(anon_vma_prepare(vma))) {
  2215. page_cache_release(new_page);
  2216. page_cache_release(old_page);
  2217. /* Caller expects lock to be held */
  2218. spin_lock(&mm->page_table_lock);
  2219. return VM_FAULT_OOM;
  2220. }
  2221. copy_user_huge_page(new_page, old_page, address, vma,
  2222. pages_per_huge_page(h));
  2223. __SetPageUptodate(new_page);
  2224. /*
  2225. * Retake the page_table_lock to check for racing updates
  2226. * before the page tables are altered
  2227. */
  2228. spin_lock(&mm->page_table_lock);
  2229. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  2230. if (likely(pte_same(huge_ptep_get(ptep), pte))) {
  2231. /* Break COW */
  2232. mmu_notifier_invalidate_range_start(mm,
  2233. address & huge_page_mask(h),
  2234. (address & huge_page_mask(h)) + huge_page_size(h));
  2235. huge_ptep_clear_flush(vma, address, ptep);
  2236. set_huge_pte_at(mm, address, ptep,
  2237. make_huge_pte(vma, new_page, 1));
  2238. page_remove_rmap(old_page);
  2239. hugepage_add_new_anon_rmap(new_page, vma, address);
  2240. /* Make the old page be freed below */
  2241. new_page = old_page;
  2242. mmu_notifier_invalidate_range_end(mm,
  2243. address & huge_page_mask(h),
  2244. (address & huge_page_mask(h)) + huge_page_size(h));
  2245. }
  2246. page_cache_release(new_page);
  2247. page_cache_release(old_page);
  2248. return 0;
  2249. }
  2250. /* Return the pagecache page at a given address within a VMA */
  2251. static struct page *hugetlbfs_pagecache_page(struct hstate *h,
  2252. struct vm_area_struct *vma, unsigned long address)
  2253. {
  2254. struct address_space *mapping;
  2255. pgoff_t idx;
  2256. mapping = vma->vm_file->f_mapping;
  2257. idx = vma_hugecache_offset(h, vma, address);
  2258. return find_lock_page(mapping, idx);
  2259. }
  2260. /*
  2261. * Return whether there is a pagecache page to back given address within VMA.
  2262. * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
  2263. */
  2264. static bool hugetlbfs_pagecache_present(struct hstate *h,
  2265. struct vm_area_struct *vma, unsigned long address)
  2266. {
  2267. struct address_space *mapping;
  2268. pgoff_t idx;
  2269. struct page *page;
  2270. mapping = vma->vm_file->f_mapping;
  2271. idx = vma_hugecache_offset(h, vma, address);
  2272. page = find_get_page(mapping, idx);
  2273. if (page)
  2274. put_page(page);
  2275. return page != NULL;
  2276. }
  2277. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2278. unsigned long address, pte_t *ptep, unsigned int flags)
  2279. {
  2280. struct hstate *h = hstate_vma(vma);
  2281. int ret = VM_FAULT_SIGBUS;
  2282. int anon_rmap = 0;
  2283. pgoff_t idx;
  2284. unsigned long size;
  2285. struct page *page;
  2286. struct address_space *mapping;
  2287. pte_t new_pte;
  2288. /*
  2289. * Currently, we are forced to kill the process in the event the
  2290. * original mapper has unmapped pages from the child due to a failed
  2291. * COW. Warn that such a situation has occurred as it may not be obvious
  2292. */
  2293. if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
  2294. printk(KERN_WARNING
  2295. "PID %d killed due to inadequate hugepage pool\n",
  2296. current->pid);
  2297. return ret;
  2298. }
  2299. mapping = vma->vm_file->f_mapping;
  2300. idx = vma_hugecache_offset(h, vma, address);
  2301. /*
  2302. * Use page lock to guard against racing truncation
  2303. * before we get page_table_lock.
  2304. */
  2305. retry:
  2306. page = find_lock_page(mapping, idx);
  2307. if (!page) {
  2308. size = i_size_read(mapping->host) >> huge_page_shift(h);
  2309. if (idx >= size)
  2310. goto out;
  2311. page = alloc_huge_page(vma, address, 0);
  2312. if (IS_ERR(page)) {
  2313. ret = PTR_ERR(page);
  2314. if (ret == -ENOMEM)
  2315. ret = VM_FAULT_OOM;
  2316. else
  2317. ret = VM_FAULT_SIGBUS;
  2318. goto out;
  2319. }
  2320. clear_huge_page(page, address, pages_per_huge_page(h));
  2321. __SetPageUptodate(page);
  2322. if (vma->vm_flags & VM_MAYSHARE) {
  2323. int err;
  2324. struct inode *inode = mapping->host;
  2325. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  2326. if (err) {
  2327. put_page(page);
  2328. if (err == -EEXIST)
  2329. goto retry;
  2330. goto out;
  2331. }
  2332. spin_lock(&inode->i_lock);
  2333. inode->i_blocks += blocks_per_huge_page(h);
  2334. spin_unlock(&inode->i_lock);
  2335. } else {
  2336. lock_page(page);
  2337. if (unlikely(anon_vma_prepare(vma))) {
  2338. ret = VM_FAULT_OOM;
  2339. goto backout_unlocked;
  2340. }
  2341. anon_rmap = 1;
  2342. }
  2343. } else {
  2344. /*
  2345. * If memory error occurs between mmap() and fault, some process
  2346. * don't have hwpoisoned swap entry for errored virtual address.
  2347. * So we need to block hugepage fault by PG_hwpoison bit check.
  2348. */
  2349. if (unlikely(PageHWPoison(page))) {
  2350. ret = VM_FAULT_HWPOISON |
  2351. VM_FAULT_SET_HINDEX(hstate_index(h));
  2352. goto backout_unlocked;
  2353. }
  2354. }
  2355. /*
  2356. * If we are going to COW a private mapping later, we examine the
  2357. * pending reservations for this page now. This will ensure that
  2358. * any allocations necessary to record that reservation occur outside
  2359. * the spinlock.
  2360. */
  2361. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
  2362. if (vma_needs_reservation(h, vma, address) < 0) {
  2363. ret = VM_FAULT_OOM;
  2364. goto backout_unlocked;
  2365. }
  2366. spin_lock(&mm->page_table_lock);
  2367. size = i_size_read(mapping->host) >> huge_page_shift(h);
  2368. if (idx >= size)
  2369. goto backout;
  2370. ret = 0;
  2371. if (!huge_pte_none(huge_ptep_get(ptep)))
  2372. goto backout;
  2373. if (anon_rmap)
  2374. hugepage_add_new_anon_rmap(page, vma, address);
  2375. else
  2376. page_dup_rmap(page);
  2377. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  2378. && (vma->vm_flags & VM_SHARED)));
  2379. set_huge_pte_at(mm, address, ptep, new_pte);
  2380. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
  2381. /* Optimization, do the COW without a second fault */
  2382. ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
  2383. }
  2384. spin_unlock(&mm->page_table_lock);
  2385. unlock_page(page);
  2386. out:
  2387. return ret;
  2388. backout:
  2389. spin_unlock(&mm->page_table_lock);
  2390. backout_unlocked:
  2391. unlock_page(page);
  2392. put_page(page);
  2393. goto out;
  2394. }
  2395. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2396. unsigned long address, unsigned int flags)
  2397. {
  2398. pte_t *ptep;
  2399. pte_t entry;
  2400. int ret;
  2401. struct page *page = NULL;
  2402. struct page *pagecache_page = NULL;
  2403. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  2404. struct hstate *h = hstate_vma(vma);
  2405. address &= huge_page_mask(h);
  2406. ptep = huge_pte_offset(mm, address);
  2407. if (ptep) {
  2408. entry = huge_ptep_get(ptep);
  2409. if (unlikely(is_hugetlb_entry_migration(entry))) {
  2410. migration_entry_wait(mm, (pmd_t *)ptep, address);
  2411. return 0;
  2412. } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
  2413. return VM_FAULT_HWPOISON_LARGE |
  2414. VM_FAULT_SET_HINDEX(hstate_index(h));
  2415. }
  2416. ptep = huge_pte_alloc(mm, address, huge_page_size(h));
  2417. if (!ptep)
  2418. return VM_FAULT_OOM;
  2419. /*
  2420. * Serialize hugepage allocation and instantiation, so that we don't
  2421. * get spurious allocation failures if two CPUs race to instantiate
  2422. * the same page in the page cache.
  2423. */
  2424. mutex_lock(&hugetlb_instantiation_mutex);
  2425. entry = huge_ptep_get(ptep);
  2426. if (huge_pte_none(entry)) {
  2427. ret = hugetlb_no_page(mm, vma, address, ptep, flags);
  2428. goto out_mutex;
  2429. }
  2430. ret = 0;
  2431. /*
  2432. * If we are going to COW the mapping later, we examine the pending
  2433. * reservations for this page now. This will ensure that any
  2434. * allocations necessary to record that reservation occur outside the
  2435. * spinlock. For private mappings, we also lookup the pagecache
  2436. * page now as it is used to determine if a reservation has been
  2437. * consumed.
  2438. */
  2439. if ((flags & FAULT_FLAG_WRITE) && !pte_write(entry)) {
  2440. if (vma_needs_reservation(h, vma, address) < 0) {
  2441. ret = VM_FAULT_OOM;
  2442. goto out_mutex;
  2443. }
  2444. if (!(vma->vm_flags & VM_MAYSHARE))
  2445. pagecache_page = hugetlbfs_pagecache_page(h,
  2446. vma, address);
  2447. }
  2448. /*
  2449. * hugetlb_cow() requires page locks of pte_page(entry) and
  2450. * pagecache_page, so here we need take the former one
  2451. * when page != pagecache_page or !pagecache_page.
  2452. * Note that locking order is always pagecache_page -> page,
  2453. * so no worry about deadlock.
  2454. */
  2455. page = pte_page(entry);
  2456. get_page(page);
  2457. if (page != pagecache_page)
  2458. lock_page(page);
  2459. spin_lock(&mm->page_table_lock);
  2460. /* Check for a racing update before calling hugetlb_cow */
  2461. if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
  2462. goto out_page_table_lock;
  2463. if (flags & FAULT_FLAG_WRITE) {
  2464. if (!pte_write(entry)) {
  2465. ret = hugetlb_cow(mm, vma, address, ptep, entry,
  2466. pagecache_page);
  2467. goto out_page_table_lock;
  2468. }
  2469. entry = pte_mkdirty(entry);
  2470. }
  2471. entry = pte_mkyoung(entry);
  2472. if (huge_ptep_set_access_flags(vma, address, ptep, entry,
  2473. flags & FAULT_FLAG_WRITE))
  2474. update_mmu_cache(vma, address, ptep);
  2475. out_page_table_lock:
  2476. spin_unlock(&mm->page_table_lock);
  2477. if (pagecache_page) {
  2478. unlock_page(pagecache_page);
  2479. put_page(pagecache_page);
  2480. }
  2481. if (page != pagecache_page)
  2482. unlock_page(page);
  2483. put_page(page);
  2484. out_mutex:
  2485. mutex_unlock(&hugetlb_instantiation_mutex);
  2486. return ret;
  2487. }
  2488. /* Can be overriden by architectures */
  2489. __attribute__((weak)) struct page *
  2490. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  2491. pud_t *pud, int write)
  2492. {
  2493. BUG();
  2494. return NULL;
  2495. }
  2496. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2497. struct page **pages, struct vm_area_struct **vmas,
  2498. unsigned long *position, int *length, int i,
  2499. unsigned int flags)
  2500. {
  2501. unsigned long pfn_offset;
  2502. unsigned long vaddr = *position;
  2503. int remainder = *length;
  2504. struct hstate *h = hstate_vma(vma);
  2505. spin_lock(&mm->page_table_lock);
  2506. while (vaddr < vma->vm_end && remainder) {
  2507. pte_t *pte;
  2508. int absent;
  2509. struct page *page;
  2510. /*
  2511. * Some archs (sparc64, sh*) have multiple pte_ts to
  2512. * each hugepage. We have to make sure we get the
  2513. * first, for the page indexing below to work.
  2514. */
  2515. pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
  2516. absent = !pte || huge_pte_none(huge_ptep_get(pte));
  2517. /*
  2518. * When coredumping, it suits get_dump_page if we just return
  2519. * an error where there's an empty slot with no huge pagecache
  2520. * to back it. This way, we avoid allocating a hugepage, and
  2521. * the sparse dumpfile avoids allocating disk blocks, but its
  2522. * huge holes still show up with zeroes where they need to be.
  2523. */
  2524. if (absent && (flags & FOLL_DUMP) &&
  2525. !hugetlbfs_pagecache_present(h, vma, vaddr)) {
  2526. remainder = 0;
  2527. break;
  2528. }
  2529. if (absent ||
  2530. ((flags & FOLL_WRITE) && !pte_write(huge_ptep_get(pte)))) {
  2531. int ret;
  2532. spin_unlock(&mm->page_table_lock);
  2533. ret = hugetlb_fault(mm, vma, vaddr,
  2534. (flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0);
  2535. spin_lock(&mm->page_table_lock);
  2536. if (!(ret & VM_FAULT_ERROR))
  2537. continue;
  2538. remainder = 0;
  2539. break;
  2540. }
  2541. pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
  2542. page = pte_page(huge_ptep_get(pte));
  2543. same_page:
  2544. if (pages) {
  2545. pages[i] = mem_map_offset(page, pfn_offset);
  2546. get_page(pages[i]);
  2547. }
  2548. if (vmas)
  2549. vmas[i] = vma;
  2550. vaddr += PAGE_SIZE;
  2551. ++pfn_offset;
  2552. --remainder;
  2553. ++i;
  2554. if (vaddr < vma->vm_end && remainder &&
  2555. pfn_offset < pages_per_huge_page(h)) {
  2556. /*
  2557. * We use pfn_offset to avoid touching the pageframes
  2558. * of this compound page.
  2559. */
  2560. goto same_page;
  2561. }
  2562. }
  2563. spin_unlock(&mm->page_table_lock);
  2564. *length = remainder;
  2565. *position = vaddr;
  2566. return i ? i : -EFAULT;
  2567. }
  2568. void hugetlb_change_protection(struct vm_area_struct *vma,
  2569. unsigned long address, unsigned long end, pgprot_t newprot)
  2570. {
  2571. struct mm_struct *mm = vma->vm_mm;
  2572. unsigned long start = address;
  2573. pte_t *ptep;
  2574. pte_t pte;
  2575. struct hstate *h = hstate_vma(vma);
  2576. BUG_ON(address >= end);
  2577. flush_cache_range(vma, address, end);
  2578. mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
  2579. spin_lock(&mm->page_table_lock);
  2580. for (; address < end; address += huge_page_size(h)) {
  2581. ptep = huge_pte_offset(mm, address);
  2582. if (!ptep)
  2583. continue;
  2584. if (huge_pmd_unshare(mm, &address, ptep))
  2585. continue;
  2586. if (!huge_pte_none(huge_ptep_get(ptep))) {
  2587. pte = huge_ptep_get_and_clear(mm, address, ptep);
  2588. pte = pte_mkhuge(pte_modify(pte, newprot));
  2589. set_huge_pte_at(mm, address, ptep, pte);
  2590. }
  2591. }
  2592. spin_unlock(&mm->page_table_lock);
  2593. mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
  2594. flush_tlb_range(vma, start, end);
  2595. }
  2596. int hugetlb_reserve_pages(struct inode *inode,
  2597. long from, long to,
  2598. struct vm_area_struct *vma,
  2599. vm_flags_t vm_flags)
  2600. {
  2601. long ret, chg;
  2602. struct hstate *h = hstate_inode(inode);
  2603. struct hugepage_subpool *spool = subpool_inode(inode);
  2604. /*
  2605. * Only apply hugepage reservation if asked. At fault time, an
  2606. * attempt will be made for VM_NORESERVE to allocate a page
  2607. * without using reserves
  2608. */
  2609. if (vm_flags & VM_NORESERVE)
  2610. return 0;
  2611. /*
  2612. * Shared mappings base their reservation on the number of pages that
  2613. * are already allocated on behalf of the file. Private mappings need
  2614. * to reserve the full area even if read-only as mprotect() may be
  2615. * called to make the mapping read-write. Assume !vma is a shm mapping
  2616. */
  2617. if (!vma || vma->vm_flags & VM_MAYSHARE)
  2618. chg = region_chg(&inode->i_mapping->private_list, from, to);
  2619. else {
  2620. struct resv_map *resv_map = resv_map_alloc();
  2621. if (!resv_map)
  2622. return -ENOMEM;
  2623. chg = to - from;
  2624. set_vma_resv_map(vma, resv_map);
  2625. set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
  2626. }
  2627. if (chg < 0) {
  2628. ret = chg;
  2629. goto out_err;
  2630. }
  2631. /* There must be enough pages in the subpool for the mapping */
  2632. if (hugepage_subpool_get_pages(spool, chg)) {
  2633. ret = -ENOSPC;
  2634. goto out_err;
  2635. }
  2636. /*
  2637. * Check enough hugepages are available for the reservation.
  2638. * Hand the pages back to the subpool if there are not
  2639. */
  2640. ret = hugetlb_acct_memory(h, chg);
  2641. if (ret < 0) {
  2642. hugepage_subpool_put_pages(spool, chg);
  2643. goto out_err;
  2644. }
  2645. /*
  2646. * Account for the reservations made. Shared mappings record regions
  2647. * that have reservations as they are shared by multiple VMAs.
  2648. * When the last VMA disappears, the region map says how much
  2649. * the reservation was and the page cache tells how much of
  2650. * the reservation was consumed. Private mappings are per-VMA and
  2651. * only the consumed reservations are tracked. When the VMA
  2652. * disappears, the original reservation is the VMA size and the
  2653. * consumed reservations are stored in the map. Hence, nothing
  2654. * else has to be done for private mappings here
  2655. */
  2656. if (!vma || vma->vm_flags & VM_MAYSHARE)
  2657. region_add(&inode->i_mapping->private_list, from, to);
  2658. return 0;
  2659. out_err:
  2660. if (vma)
  2661. resv_map_put(vma);
  2662. return ret;
  2663. }
  2664. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  2665. {
  2666. struct hstate *h = hstate_inode(inode);
  2667. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  2668. struct hugepage_subpool *spool = subpool_inode(inode);
  2669. spin_lock(&inode->i_lock);
  2670. inode->i_blocks -= (blocks_per_huge_page(h) * freed);
  2671. spin_unlock(&inode->i_lock);
  2672. hugepage_subpool_put_pages(spool, (chg - freed));
  2673. hugetlb_acct_memory(h, -(chg - freed));
  2674. }
  2675. #ifdef CONFIG_MEMORY_FAILURE
  2676. /* Should be called in hugetlb_lock */
  2677. static int is_hugepage_on_freelist(struct page *hpage)
  2678. {
  2679. struct page *page;
  2680. struct page *tmp;
  2681. struct hstate *h = page_hstate(hpage);
  2682. int nid = page_to_nid(hpage);
  2683. list_for_each_entry_safe(page, tmp, &h->hugepage_freelists[nid], lru)
  2684. if (page == hpage)
  2685. return 1;
  2686. return 0;
  2687. }
  2688. /*
  2689. * This function is called from memory failure code.
  2690. * Assume the caller holds page lock of the head page.
  2691. */
  2692. int dequeue_hwpoisoned_huge_page(struct page *hpage)
  2693. {
  2694. struct hstate *h = page_hstate(hpage);
  2695. int nid = page_to_nid(hpage);
  2696. int ret = -EBUSY;
  2697. spin_lock(&hugetlb_lock);
  2698. if (is_hugepage_on_freelist(hpage)) {
  2699. list_del(&hpage->lru);
  2700. set_page_refcounted(hpage);
  2701. h->free_huge_pages--;
  2702. h->free_huge_pages_node[nid]--;
  2703. ret = 0;
  2704. }
  2705. spin_unlock(&hugetlb_lock);
  2706. return ret;
  2707. }
  2708. #endif