hugetlb.c 124 KB

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