hugetlb.c 98 KB

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