hugetlb.c 134 KB

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