hugetlb.c 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949
  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/memblock.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. if (!pfn_valid(i))
  927. return false;
  928. page = pfn_to_page(i);
  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. * A return code of zero implies that the subpool will be under its
  1081. * minimum size if the reservation is not restored after page is free.
  1082. * Therefore, force restore_reserve operation.
  1083. */
  1084. if (hugepage_subpool_put_pages(spool, 1) == 0)
  1085. restore_reserve = true;
  1086. spin_lock(&hugetlb_lock);
  1087. clear_page_huge_active(page);
  1088. hugetlb_cgroup_uncharge_page(hstate_index(h),
  1089. pages_per_huge_page(h), page);
  1090. if (restore_reserve)
  1091. h->resv_huge_pages++;
  1092. if (PageHugeTemporary(page)) {
  1093. list_del(&page->lru);
  1094. ClearPageHugeTemporary(page);
  1095. update_and_free_page(h, page);
  1096. } else if (h->surplus_huge_pages_node[nid]) {
  1097. /* remove the page from active list */
  1098. list_del(&page->lru);
  1099. update_and_free_page(h, page);
  1100. h->surplus_huge_pages--;
  1101. h->surplus_huge_pages_node[nid]--;
  1102. } else {
  1103. arch_clear_hugepage_flags(page);
  1104. enqueue_huge_page(h, page);
  1105. }
  1106. spin_unlock(&hugetlb_lock);
  1107. }
  1108. static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
  1109. {
  1110. INIT_LIST_HEAD(&page->lru);
  1111. set_compound_page_dtor(page, HUGETLB_PAGE_DTOR);
  1112. spin_lock(&hugetlb_lock);
  1113. set_hugetlb_cgroup(page, NULL);
  1114. h->nr_huge_pages++;
  1115. h->nr_huge_pages_node[nid]++;
  1116. spin_unlock(&hugetlb_lock);
  1117. }
  1118. static void prep_compound_gigantic_page(struct page *page, unsigned int order)
  1119. {
  1120. int i;
  1121. int nr_pages = 1 << order;
  1122. struct page *p = page + 1;
  1123. /* we rely on prep_new_huge_page to set the destructor */
  1124. set_compound_order(page, order);
  1125. __ClearPageReserved(page);
  1126. __SetPageHead(page);
  1127. for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
  1128. /*
  1129. * For gigantic hugepages allocated through bootmem at
  1130. * boot, it's safer to be consistent with the not-gigantic
  1131. * hugepages and clear the PG_reserved bit from all tail pages
  1132. * too. Otherwse drivers using get_user_pages() to access tail
  1133. * pages may get the reference counting wrong if they see
  1134. * PG_reserved set on a tail page (despite the head page not
  1135. * having PG_reserved set). Enforcing this consistency between
  1136. * head and tail pages allows drivers to optimize away a check
  1137. * on the head page when they need know if put_page() is needed
  1138. * after get_user_pages().
  1139. */
  1140. __ClearPageReserved(p);
  1141. set_page_count(p, 0);
  1142. set_compound_head(p, page);
  1143. }
  1144. atomic_set(compound_mapcount_ptr(page), -1);
  1145. }
  1146. /*
  1147. * PageHuge() only returns true for hugetlbfs pages, but not for normal or
  1148. * transparent huge pages. See the PageTransHuge() documentation for more
  1149. * details.
  1150. */
  1151. int PageHuge(struct page *page)
  1152. {
  1153. if (!PageCompound(page))
  1154. return 0;
  1155. page = compound_head(page);
  1156. return page[1].compound_dtor == HUGETLB_PAGE_DTOR;
  1157. }
  1158. EXPORT_SYMBOL_GPL(PageHuge);
  1159. /*
  1160. * PageHeadHuge() only returns true for hugetlbfs head page, but not for
  1161. * normal or transparent huge pages.
  1162. */
  1163. int PageHeadHuge(struct page *page_head)
  1164. {
  1165. if (!PageHead(page_head))
  1166. return 0;
  1167. return get_compound_page_dtor(page_head) == free_huge_page;
  1168. }
  1169. pgoff_t __basepage_index(struct page *page)
  1170. {
  1171. struct page *page_head = compound_head(page);
  1172. pgoff_t index = page_index(page_head);
  1173. unsigned long compound_idx;
  1174. if (!PageHuge(page_head))
  1175. return page_index(page);
  1176. if (compound_order(page_head) >= MAX_ORDER)
  1177. compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
  1178. else
  1179. compound_idx = page - page_head;
  1180. return (index << compound_order(page_head)) + compound_idx;
  1181. }
  1182. static struct page *alloc_buddy_huge_page(struct hstate *h,
  1183. gfp_t gfp_mask, int nid, nodemask_t *nmask)
  1184. {
  1185. int order = huge_page_order(h);
  1186. struct page *page;
  1187. gfp_mask |= __GFP_COMP|__GFP_RETRY_MAYFAIL|__GFP_NOWARN;
  1188. if (nid == NUMA_NO_NODE)
  1189. nid = numa_mem_id();
  1190. page = __alloc_pages_nodemask(gfp_mask, order, nid, nmask);
  1191. if (page)
  1192. __count_vm_event(HTLB_BUDDY_PGALLOC);
  1193. else
  1194. __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  1195. return page;
  1196. }
  1197. /*
  1198. * Common helper to allocate a fresh hugetlb page. All specific allocators
  1199. * should use this function to get new hugetlb pages
  1200. */
  1201. static struct page *alloc_fresh_huge_page(struct hstate *h,
  1202. gfp_t gfp_mask, int nid, nodemask_t *nmask)
  1203. {
  1204. struct page *page;
  1205. if (hstate_is_gigantic(h))
  1206. page = alloc_gigantic_page(h, gfp_mask, nid, nmask);
  1207. else
  1208. page = alloc_buddy_huge_page(h, gfp_mask,
  1209. nid, nmask);
  1210. if (!page)
  1211. return NULL;
  1212. if (hstate_is_gigantic(h))
  1213. prep_compound_gigantic_page(page, huge_page_order(h));
  1214. prep_new_huge_page(h, page, page_to_nid(page));
  1215. return page;
  1216. }
  1217. /*
  1218. * Allocates a fresh page to the hugetlb allocator pool in the node interleaved
  1219. * manner.
  1220. */
  1221. static int alloc_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
  1222. {
  1223. struct page *page;
  1224. int nr_nodes, node;
  1225. gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
  1226. for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
  1227. page = alloc_fresh_huge_page(h, gfp_mask, node, nodes_allowed);
  1228. if (page)
  1229. break;
  1230. }
  1231. if (!page)
  1232. return 0;
  1233. put_page(page); /* free it into the hugepage allocator */
  1234. return 1;
  1235. }
  1236. /*
  1237. * Free huge page from pool from next node to free.
  1238. * Attempt to keep persistent huge pages more or less
  1239. * balanced over allowed nodes.
  1240. * Called with hugetlb_lock locked.
  1241. */
  1242. static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  1243. bool acct_surplus)
  1244. {
  1245. int nr_nodes, node;
  1246. int ret = 0;
  1247. for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
  1248. /*
  1249. * If we're returning unused surplus pages, only examine
  1250. * nodes with surplus pages.
  1251. */
  1252. if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
  1253. !list_empty(&h->hugepage_freelists[node])) {
  1254. struct page *page =
  1255. list_entry(h->hugepage_freelists[node].next,
  1256. struct page, lru);
  1257. list_del(&page->lru);
  1258. h->free_huge_pages--;
  1259. h->free_huge_pages_node[node]--;
  1260. if (acct_surplus) {
  1261. h->surplus_huge_pages--;
  1262. h->surplus_huge_pages_node[node]--;
  1263. }
  1264. update_and_free_page(h, page);
  1265. ret = 1;
  1266. break;
  1267. }
  1268. }
  1269. return ret;
  1270. }
  1271. /*
  1272. * Dissolve a given free hugepage into free buddy pages. This function does
  1273. * nothing for in-use (including surplus) hugepages. Returns -EBUSY if the
  1274. * dissolution fails because a give page is not a free hugepage, or because
  1275. * free hugepages are fully reserved.
  1276. */
  1277. int dissolve_free_huge_page(struct page *page)
  1278. {
  1279. int rc = -EBUSY;
  1280. spin_lock(&hugetlb_lock);
  1281. if (PageHuge(page) && !page_count(page)) {
  1282. struct page *head = compound_head(page);
  1283. struct hstate *h = page_hstate(head);
  1284. int nid = page_to_nid(head);
  1285. if (h->free_huge_pages - h->resv_huge_pages == 0)
  1286. goto out;
  1287. /*
  1288. * Move PageHWPoison flag from head page to the raw error page,
  1289. * which makes any subpages rather than the error page reusable.
  1290. */
  1291. if (PageHWPoison(head) && page != head) {
  1292. SetPageHWPoison(page);
  1293. ClearPageHWPoison(head);
  1294. }
  1295. list_del(&head->lru);
  1296. h->free_huge_pages--;
  1297. h->free_huge_pages_node[nid]--;
  1298. h->max_huge_pages--;
  1299. update_and_free_page(h, head);
  1300. rc = 0;
  1301. }
  1302. out:
  1303. spin_unlock(&hugetlb_lock);
  1304. return rc;
  1305. }
  1306. /*
  1307. * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
  1308. * make specified memory blocks removable from the system.
  1309. * Note that this will dissolve a free gigantic hugepage completely, if any
  1310. * part of it lies within the given range.
  1311. * Also note that if dissolve_free_huge_page() returns with an error, all
  1312. * free hugepages that were dissolved before that error are lost.
  1313. */
  1314. int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
  1315. {
  1316. unsigned long pfn;
  1317. struct page *page;
  1318. int rc = 0;
  1319. if (!hugepages_supported())
  1320. return rc;
  1321. for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) {
  1322. page = pfn_to_page(pfn);
  1323. if (PageHuge(page) && !page_count(page)) {
  1324. rc = dissolve_free_huge_page(page);
  1325. if (rc)
  1326. break;
  1327. }
  1328. }
  1329. return rc;
  1330. }
  1331. /*
  1332. * Allocates a fresh surplus page from the page allocator.
  1333. */
  1334. static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask,
  1335. int nid, nodemask_t *nmask)
  1336. {
  1337. struct page *page = NULL;
  1338. if (hstate_is_gigantic(h))
  1339. return NULL;
  1340. spin_lock(&hugetlb_lock);
  1341. if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages)
  1342. goto out_unlock;
  1343. spin_unlock(&hugetlb_lock);
  1344. page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask);
  1345. if (!page)
  1346. return NULL;
  1347. spin_lock(&hugetlb_lock);
  1348. /*
  1349. * We could have raced with the pool size change.
  1350. * Double check that and simply deallocate the new page
  1351. * if we would end up overcommiting the surpluses. Abuse
  1352. * temporary page to workaround the nasty free_huge_page
  1353. * codeflow
  1354. */
  1355. if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
  1356. SetPageHugeTemporary(page);
  1357. put_page(page);
  1358. page = NULL;
  1359. } else {
  1360. h->surplus_huge_pages++;
  1361. h->surplus_huge_pages_node[page_to_nid(page)]++;
  1362. }
  1363. out_unlock:
  1364. spin_unlock(&hugetlb_lock);
  1365. return page;
  1366. }
  1367. static struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask,
  1368. int nid, nodemask_t *nmask)
  1369. {
  1370. struct page *page;
  1371. if (hstate_is_gigantic(h))
  1372. return NULL;
  1373. page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask);
  1374. if (!page)
  1375. return NULL;
  1376. /*
  1377. * We do not account these pages as surplus because they are only
  1378. * temporary and will be released properly on the last reference
  1379. */
  1380. SetPageHugeTemporary(page);
  1381. return page;
  1382. }
  1383. /*
  1384. * Use the VMA's mpolicy to allocate a huge page from the buddy.
  1385. */
  1386. static
  1387. struct page *alloc_buddy_huge_page_with_mpol(struct hstate *h,
  1388. struct vm_area_struct *vma, unsigned long addr)
  1389. {
  1390. struct page *page;
  1391. struct mempolicy *mpol;
  1392. gfp_t gfp_mask = htlb_alloc_mask(h);
  1393. int nid;
  1394. nodemask_t *nodemask;
  1395. nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask);
  1396. page = alloc_surplus_huge_page(h, gfp_mask, nid, nodemask);
  1397. mpol_cond_put(mpol);
  1398. return page;
  1399. }
  1400. /* page migration callback function */
  1401. struct page *alloc_huge_page_node(struct hstate *h, int nid)
  1402. {
  1403. gfp_t gfp_mask = htlb_alloc_mask(h);
  1404. struct page *page = NULL;
  1405. if (nid != NUMA_NO_NODE)
  1406. gfp_mask |= __GFP_THISNODE;
  1407. spin_lock(&hugetlb_lock);
  1408. if (h->free_huge_pages - h->resv_huge_pages > 0)
  1409. page = dequeue_huge_page_nodemask(h, gfp_mask, nid, NULL);
  1410. spin_unlock(&hugetlb_lock);
  1411. if (!page)
  1412. page = alloc_migrate_huge_page(h, gfp_mask, nid, NULL);
  1413. return page;
  1414. }
  1415. /* page migration callback function */
  1416. struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid,
  1417. nodemask_t *nmask)
  1418. {
  1419. gfp_t gfp_mask = htlb_alloc_mask(h);
  1420. spin_lock(&hugetlb_lock);
  1421. if (h->free_huge_pages - h->resv_huge_pages > 0) {
  1422. struct page *page;
  1423. page = dequeue_huge_page_nodemask(h, gfp_mask, preferred_nid, nmask);
  1424. if (page) {
  1425. spin_unlock(&hugetlb_lock);
  1426. return page;
  1427. }
  1428. }
  1429. spin_unlock(&hugetlb_lock);
  1430. return alloc_migrate_huge_page(h, gfp_mask, preferred_nid, nmask);
  1431. }
  1432. /* mempolicy aware migration callback */
  1433. struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma,
  1434. unsigned long address)
  1435. {
  1436. struct mempolicy *mpol;
  1437. nodemask_t *nodemask;
  1438. struct page *page;
  1439. gfp_t gfp_mask;
  1440. int node;
  1441. gfp_mask = htlb_alloc_mask(h);
  1442. node = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
  1443. page = alloc_huge_page_nodemask(h, node, nodemask);
  1444. mpol_cond_put(mpol);
  1445. return page;
  1446. }
  1447. /*
  1448. * Increase the hugetlb pool such that it can accommodate a reservation
  1449. * of size 'delta'.
  1450. */
  1451. static int gather_surplus_pages(struct hstate *h, int delta)
  1452. {
  1453. struct list_head surplus_list;
  1454. struct page *page, *tmp;
  1455. int ret, i;
  1456. int needed, allocated;
  1457. bool alloc_ok = true;
  1458. needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
  1459. if (needed <= 0) {
  1460. h->resv_huge_pages += delta;
  1461. return 0;
  1462. }
  1463. allocated = 0;
  1464. INIT_LIST_HEAD(&surplus_list);
  1465. ret = -ENOMEM;
  1466. retry:
  1467. spin_unlock(&hugetlb_lock);
  1468. for (i = 0; i < needed; i++) {
  1469. page = alloc_surplus_huge_page(h, htlb_alloc_mask(h),
  1470. NUMA_NO_NODE, NULL);
  1471. if (!page) {
  1472. alloc_ok = false;
  1473. break;
  1474. }
  1475. list_add(&page->lru, &surplus_list);
  1476. cond_resched();
  1477. }
  1478. allocated += i;
  1479. /*
  1480. * After retaking hugetlb_lock, we need to recalculate 'needed'
  1481. * because either resv_huge_pages or free_huge_pages may have changed.
  1482. */
  1483. spin_lock(&hugetlb_lock);
  1484. needed = (h->resv_huge_pages + delta) -
  1485. (h->free_huge_pages + allocated);
  1486. if (needed > 0) {
  1487. if (alloc_ok)
  1488. goto retry;
  1489. /*
  1490. * We were not able to allocate enough pages to
  1491. * satisfy the entire reservation so we free what
  1492. * we've allocated so far.
  1493. */
  1494. goto free;
  1495. }
  1496. /*
  1497. * The surplus_list now contains _at_least_ the number of extra pages
  1498. * needed to accommodate the reservation. Add the appropriate number
  1499. * of pages to the hugetlb pool and free the extras back to the buddy
  1500. * allocator. Commit the entire reservation here to prevent another
  1501. * process from stealing the pages as they are added to the pool but
  1502. * before they are reserved.
  1503. */
  1504. needed += allocated;
  1505. h->resv_huge_pages += delta;
  1506. ret = 0;
  1507. /* Free the needed pages to the hugetlb pool */
  1508. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  1509. if ((--needed) < 0)
  1510. break;
  1511. /*
  1512. * This page is now managed by the hugetlb allocator and has
  1513. * no users -- drop the buddy allocator's reference.
  1514. */
  1515. put_page_testzero(page);
  1516. VM_BUG_ON_PAGE(page_count(page), page);
  1517. enqueue_huge_page(h, page);
  1518. }
  1519. free:
  1520. spin_unlock(&hugetlb_lock);
  1521. /* Free unnecessary surplus pages to the buddy allocator */
  1522. list_for_each_entry_safe(page, tmp, &surplus_list, lru)
  1523. put_page(page);
  1524. spin_lock(&hugetlb_lock);
  1525. return ret;
  1526. }
  1527. /*
  1528. * This routine has two main purposes:
  1529. * 1) Decrement the reservation count (resv_huge_pages) by the value passed
  1530. * in unused_resv_pages. This corresponds to the prior adjustments made
  1531. * to the associated reservation map.
  1532. * 2) Free any unused surplus pages that may have been allocated to satisfy
  1533. * the reservation. As many as unused_resv_pages may be freed.
  1534. *
  1535. * Called with hugetlb_lock held. However, the lock could be dropped (and
  1536. * reacquired) during calls to cond_resched_lock. Whenever dropping the lock,
  1537. * we must make sure nobody else can claim pages we are in the process of
  1538. * freeing. Do this by ensuring resv_huge_page always is greater than the
  1539. * number of huge pages we plan to free when dropping the lock.
  1540. */
  1541. static void return_unused_surplus_pages(struct hstate *h,
  1542. unsigned long unused_resv_pages)
  1543. {
  1544. unsigned long nr_pages;
  1545. /* Cannot return gigantic pages currently */
  1546. if (hstate_is_gigantic(h))
  1547. goto out;
  1548. /*
  1549. * Part (or even all) of the reservation could have been backed
  1550. * by pre-allocated pages. Only free surplus pages.
  1551. */
  1552. nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
  1553. /*
  1554. * We want to release as many surplus pages as possible, spread
  1555. * evenly across all nodes with memory. Iterate across these nodes
  1556. * until we can no longer free unreserved surplus pages. This occurs
  1557. * when the nodes with surplus pages have no free pages.
  1558. * free_pool_huge_page() will balance the the freed pages across the
  1559. * on-line nodes with memory and will handle the hstate accounting.
  1560. *
  1561. * Note that we decrement resv_huge_pages as we free the pages. If
  1562. * we drop the lock, resv_huge_pages will still be sufficiently large
  1563. * to cover subsequent pages we may free.
  1564. */
  1565. while (nr_pages--) {
  1566. h->resv_huge_pages--;
  1567. unused_resv_pages--;
  1568. if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1))
  1569. goto out;
  1570. cond_resched_lock(&hugetlb_lock);
  1571. }
  1572. out:
  1573. /* Fully uncommit the reservation */
  1574. h->resv_huge_pages -= unused_resv_pages;
  1575. }
  1576. /*
  1577. * vma_needs_reservation, vma_commit_reservation and vma_end_reservation
  1578. * are used by the huge page allocation routines to manage reservations.
  1579. *
  1580. * vma_needs_reservation is called to determine if the huge page at addr
  1581. * within the vma has an associated reservation. If a reservation is
  1582. * needed, the value 1 is returned. The caller is then responsible for
  1583. * managing the global reservation and subpool usage counts. After
  1584. * the huge page has been allocated, vma_commit_reservation is called
  1585. * to add the page to the reservation map. If the page allocation fails,
  1586. * the reservation must be ended instead of committed. vma_end_reservation
  1587. * is called in such cases.
  1588. *
  1589. * In the normal case, vma_commit_reservation returns the same value
  1590. * as the preceding vma_needs_reservation call. The only time this
  1591. * is not the case is if a reserve map was changed between calls. It
  1592. * is the responsibility of the caller to notice the difference and
  1593. * take appropriate action.
  1594. *
  1595. * vma_add_reservation is used in error paths where a reservation must
  1596. * be restored when a newly allocated huge page must be freed. It is
  1597. * to be called after calling vma_needs_reservation to determine if a
  1598. * reservation exists.
  1599. */
  1600. enum vma_resv_mode {
  1601. VMA_NEEDS_RESV,
  1602. VMA_COMMIT_RESV,
  1603. VMA_END_RESV,
  1604. VMA_ADD_RESV,
  1605. };
  1606. static long __vma_reservation_common(struct hstate *h,
  1607. struct vm_area_struct *vma, unsigned long addr,
  1608. enum vma_resv_mode mode)
  1609. {
  1610. struct resv_map *resv;
  1611. pgoff_t idx;
  1612. long ret;
  1613. resv = vma_resv_map(vma);
  1614. if (!resv)
  1615. return 1;
  1616. idx = vma_hugecache_offset(h, vma, addr);
  1617. switch (mode) {
  1618. case VMA_NEEDS_RESV:
  1619. ret = region_chg(resv, idx, idx + 1);
  1620. break;
  1621. case VMA_COMMIT_RESV:
  1622. ret = region_add(resv, idx, idx + 1);
  1623. break;
  1624. case VMA_END_RESV:
  1625. region_abort(resv, idx, idx + 1);
  1626. ret = 0;
  1627. break;
  1628. case VMA_ADD_RESV:
  1629. if (vma->vm_flags & VM_MAYSHARE)
  1630. ret = region_add(resv, idx, idx + 1);
  1631. else {
  1632. region_abort(resv, idx, idx + 1);
  1633. ret = region_del(resv, idx, idx + 1);
  1634. }
  1635. break;
  1636. default:
  1637. BUG();
  1638. }
  1639. if (vma->vm_flags & VM_MAYSHARE)
  1640. return ret;
  1641. else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && ret >= 0) {
  1642. /*
  1643. * In most cases, reserves always exist for private mappings.
  1644. * However, a file associated with mapping could have been
  1645. * hole punched or truncated after reserves were consumed.
  1646. * As subsequent fault on such a range will not use reserves.
  1647. * Subtle - The reserve map for private mappings has the
  1648. * opposite meaning than that of shared mappings. If NO
  1649. * entry is in the reserve map, it means a reservation exists.
  1650. * If an entry exists in the reserve map, it means the
  1651. * reservation has already been consumed. As a result, the
  1652. * return value of this routine is the opposite of the
  1653. * value returned from reserve map manipulation routines above.
  1654. */
  1655. if (ret)
  1656. return 0;
  1657. else
  1658. return 1;
  1659. }
  1660. else
  1661. return ret < 0 ? ret : 0;
  1662. }
  1663. static long vma_needs_reservation(struct hstate *h,
  1664. struct vm_area_struct *vma, unsigned long addr)
  1665. {
  1666. return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV);
  1667. }
  1668. static long vma_commit_reservation(struct hstate *h,
  1669. struct vm_area_struct *vma, unsigned long addr)
  1670. {
  1671. return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV);
  1672. }
  1673. static void vma_end_reservation(struct hstate *h,
  1674. struct vm_area_struct *vma, unsigned long addr)
  1675. {
  1676. (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV);
  1677. }
  1678. static long vma_add_reservation(struct hstate *h,
  1679. struct vm_area_struct *vma, unsigned long addr)
  1680. {
  1681. return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV);
  1682. }
  1683. /*
  1684. * This routine is called to restore a reservation on error paths. In the
  1685. * specific error paths, a huge page was allocated (via alloc_huge_page)
  1686. * and is about to be freed. If a reservation for the page existed,
  1687. * alloc_huge_page would have consumed the reservation and set PagePrivate
  1688. * in the newly allocated page. When the page is freed via free_huge_page,
  1689. * the global reservation count will be incremented if PagePrivate is set.
  1690. * However, free_huge_page can not adjust the reserve map. Adjust the
  1691. * reserve map here to be consistent with global reserve count adjustments
  1692. * to be made by free_huge_page.
  1693. */
  1694. static void restore_reserve_on_error(struct hstate *h,
  1695. struct vm_area_struct *vma, unsigned long address,
  1696. struct page *page)
  1697. {
  1698. if (unlikely(PagePrivate(page))) {
  1699. long rc = vma_needs_reservation(h, vma, address);
  1700. if (unlikely(rc < 0)) {
  1701. /*
  1702. * Rare out of memory condition in reserve map
  1703. * manipulation. Clear PagePrivate so that
  1704. * global reserve count will not be incremented
  1705. * by free_huge_page. This will make it appear
  1706. * as though the reservation for this page was
  1707. * consumed. This may prevent the task from
  1708. * faulting in the page at a later time. This
  1709. * is better than inconsistent global huge page
  1710. * accounting of reserve counts.
  1711. */
  1712. ClearPagePrivate(page);
  1713. } else if (rc) {
  1714. rc = vma_add_reservation(h, vma, address);
  1715. if (unlikely(rc < 0))
  1716. /*
  1717. * See above comment about rare out of
  1718. * memory condition.
  1719. */
  1720. ClearPagePrivate(page);
  1721. } else
  1722. vma_end_reservation(h, vma, address);
  1723. }
  1724. }
  1725. struct page *alloc_huge_page(struct vm_area_struct *vma,
  1726. unsigned long addr, int avoid_reserve)
  1727. {
  1728. struct hugepage_subpool *spool = subpool_vma(vma);
  1729. struct hstate *h = hstate_vma(vma);
  1730. struct page *page;
  1731. long map_chg, map_commit;
  1732. long gbl_chg;
  1733. int ret, idx;
  1734. struct hugetlb_cgroup *h_cg;
  1735. idx = hstate_index(h);
  1736. /*
  1737. * Examine the region/reserve map to determine if the process
  1738. * has a reservation for the page to be allocated. A return
  1739. * code of zero indicates a reservation exists (no change).
  1740. */
  1741. map_chg = gbl_chg = vma_needs_reservation(h, vma, addr);
  1742. if (map_chg < 0)
  1743. return ERR_PTR(-ENOMEM);
  1744. /*
  1745. * Processes that did not create the mapping will have no
  1746. * reserves as indicated by the region/reserve map. Check
  1747. * that the allocation will not exceed the subpool limit.
  1748. * Allocations for MAP_NORESERVE mappings also need to be
  1749. * checked against any subpool limit.
  1750. */
  1751. if (map_chg || avoid_reserve) {
  1752. gbl_chg = hugepage_subpool_get_pages(spool, 1);
  1753. if (gbl_chg < 0) {
  1754. vma_end_reservation(h, vma, addr);
  1755. return ERR_PTR(-ENOSPC);
  1756. }
  1757. /*
  1758. * Even though there was no reservation in the region/reserve
  1759. * map, there could be reservations associated with the
  1760. * subpool that can be used. This would be indicated if the
  1761. * return value of hugepage_subpool_get_pages() is zero.
  1762. * However, if avoid_reserve is specified we still avoid even
  1763. * the subpool reservations.
  1764. */
  1765. if (avoid_reserve)
  1766. gbl_chg = 1;
  1767. }
  1768. ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
  1769. if (ret)
  1770. goto out_subpool_put;
  1771. spin_lock(&hugetlb_lock);
  1772. /*
  1773. * glb_chg is passed to indicate whether or not a page must be taken
  1774. * from the global free pool (global change). gbl_chg == 0 indicates
  1775. * a reservation exists for the allocation.
  1776. */
  1777. page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, gbl_chg);
  1778. if (!page) {
  1779. spin_unlock(&hugetlb_lock);
  1780. page = alloc_buddy_huge_page_with_mpol(h, vma, addr);
  1781. if (!page)
  1782. goto out_uncharge_cgroup;
  1783. if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) {
  1784. SetPagePrivate(page);
  1785. h->resv_huge_pages--;
  1786. }
  1787. spin_lock(&hugetlb_lock);
  1788. list_move(&page->lru, &h->hugepage_activelist);
  1789. /* Fall through */
  1790. }
  1791. hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page);
  1792. spin_unlock(&hugetlb_lock);
  1793. set_page_private(page, (unsigned long)spool);
  1794. map_commit = vma_commit_reservation(h, vma, addr);
  1795. if (unlikely(map_chg > map_commit)) {
  1796. /*
  1797. * The page was added to the reservation map between
  1798. * vma_needs_reservation and vma_commit_reservation.
  1799. * This indicates a race with hugetlb_reserve_pages.
  1800. * Adjust for the subpool count incremented above AND
  1801. * in hugetlb_reserve_pages for the same page. Also,
  1802. * the reservation count added in hugetlb_reserve_pages
  1803. * no longer applies.
  1804. */
  1805. long rsv_adjust;
  1806. rsv_adjust = hugepage_subpool_put_pages(spool, 1);
  1807. hugetlb_acct_memory(h, -rsv_adjust);
  1808. }
  1809. return page;
  1810. out_uncharge_cgroup:
  1811. hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg);
  1812. out_subpool_put:
  1813. if (map_chg || avoid_reserve)
  1814. hugepage_subpool_put_pages(spool, 1);
  1815. vma_end_reservation(h, vma, addr);
  1816. return ERR_PTR(-ENOSPC);
  1817. }
  1818. int alloc_bootmem_huge_page(struct hstate *h)
  1819. __attribute__ ((weak, alias("__alloc_bootmem_huge_page")));
  1820. int __alloc_bootmem_huge_page(struct hstate *h)
  1821. {
  1822. struct huge_bootmem_page *m;
  1823. int nr_nodes, node;
  1824. for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {
  1825. void *addr;
  1826. addr = memblock_alloc_try_nid_raw(
  1827. huge_page_size(h), huge_page_size(h),
  1828. 0, MEMBLOCK_ALLOC_ACCESSIBLE, node);
  1829. if (addr) {
  1830. /*
  1831. * Use the beginning of the huge page to store the
  1832. * huge_bootmem_page struct (until gather_bootmem
  1833. * puts them into the mem_map).
  1834. */
  1835. m = addr;
  1836. goto found;
  1837. }
  1838. }
  1839. return 0;
  1840. found:
  1841. BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h)));
  1842. /* Put them into a private list first because mem_map is not up yet */
  1843. INIT_LIST_HEAD(&m->list);
  1844. list_add(&m->list, &huge_boot_pages);
  1845. m->hstate = h;
  1846. return 1;
  1847. }
  1848. static void __init prep_compound_huge_page(struct page *page,
  1849. unsigned int order)
  1850. {
  1851. if (unlikely(order > (MAX_ORDER - 1)))
  1852. prep_compound_gigantic_page(page, order);
  1853. else
  1854. prep_compound_page(page, order);
  1855. }
  1856. /* Put bootmem huge pages into the standard lists after mem_map is up */
  1857. static void __init gather_bootmem_prealloc(void)
  1858. {
  1859. struct huge_bootmem_page *m;
  1860. list_for_each_entry(m, &huge_boot_pages, list) {
  1861. struct page *page = virt_to_page(m);
  1862. struct hstate *h = m->hstate;
  1863. WARN_ON(page_count(page) != 1);
  1864. prep_compound_huge_page(page, h->order);
  1865. WARN_ON(PageReserved(page));
  1866. prep_new_huge_page(h, page, page_to_nid(page));
  1867. put_page(page); /* free it into the hugepage allocator */
  1868. /*
  1869. * If we had gigantic hugepages allocated at boot time, we need
  1870. * to restore the 'stolen' pages to totalram_pages in order to
  1871. * fix confusing memory reports from free(1) and another
  1872. * side-effects, like CommitLimit going negative.
  1873. */
  1874. if (hstate_is_gigantic(h))
  1875. adjust_managed_page_count(page, 1 << h->order);
  1876. cond_resched();
  1877. }
  1878. }
  1879. static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
  1880. {
  1881. unsigned long i;
  1882. for (i = 0; i < h->max_huge_pages; ++i) {
  1883. if (hstate_is_gigantic(h)) {
  1884. if (!alloc_bootmem_huge_page(h))
  1885. break;
  1886. } else if (!alloc_pool_huge_page(h,
  1887. &node_states[N_MEMORY]))
  1888. break;
  1889. cond_resched();
  1890. }
  1891. if (i < h->max_huge_pages) {
  1892. char buf[32];
  1893. string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
  1894. pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n",
  1895. h->max_huge_pages, buf, i);
  1896. h->max_huge_pages = i;
  1897. }
  1898. }
  1899. static void __init hugetlb_init_hstates(void)
  1900. {
  1901. struct hstate *h;
  1902. for_each_hstate(h) {
  1903. if (minimum_order > huge_page_order(h))
  1904. minimum_order = huge_page_order(h);
  1905. /* oversize hugepages were init'ed in early boot */
  1906. if (!hstate_is_gigantic(h))
  1907. hugetlb_hstate_alloc_pages(h);
  1908. }
  1909. VM_BUG_ON(minimum_order == UINT_MAX);
  1910. }
  1911. static void __init report_hugepages(void)
  1912. {
  1913. struct hstate *h;
  1914. for_each_hstate(h) {
  1915. char buf[32];
  1916. string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
  1917. pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n",
  1918. buf, h->free_huge_pages);
  1919. }
  1920. }
  1921. #ifdef CONFIG_HIGHMEM
  1922. static void try_to_free_low(struct hstate *h, unsigned long count,
  1923. nodemask_t *nodes_allowed)
  1924. {
  1925. int i;
  1926. if (hstate_is_gigantic(h))
  1927. return;
  1928. for_each_node_mask(i, *nodes_allowed) {
  1929. struct page *page, *next;
  1930. struct list_head *freel = &h->hugepage_freelists[i];
  1931. list_for_each_entry_safe(page, next, freel, lru) {
  1932. if (count >= h->nr_huge_pages)
  1933. return;
  1934. if (PageHighMem(page))
  1935. continue;
  1936. list_del(&page->lru);
  1937. update_and_free_page(h, page);
  1938. h->free_huge_pages--;
  1939. h->free_huge_pages_node[page_to_nid(page)]--;
  1940. }
  1941. }
  1942. }
  1943. #else
  1944. static inline void try_to_free_low(struct hstate *h, unsigned long count,
  1945. nodemask_t *nodes_allowed)
  1946. {
  1947. }
  1948. #endif
  1949. /*
  1950. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  1951. * balanced by operating on them in a round-robin fashion.
  1952. * Returns 1 if an adjustment was made.
  1953. */
  1954. static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
  1955. int delta)
  1956. {
  1957. int nr_nodes, node;
  1958. VM_BUG_ON(delta != -1 && delta != 1);
  1959. if (delta < 0) {
  1960. for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
  1961. if (h->surplus_huge_pages_node[node])
  1962. goto found;
  1963. }
  1964. } else {
  1965. for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
  1966. if (h->surplus_huge_pages_node[node] <
  1967. h->nr_huge_pages_node[node])
  1968. goto found;
  1969. }
  1970. }
  1971. return 0;
  1972. found:
  1973. h->surplus_huge_pages += delta;
  1974. h->surplus_huge_pages_node[node] += delta;
  1975. return 1;
  1976. }
  1977. #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
  1978. static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
  1979. nodemask_t *nodes_allowed)
  1980. {
  1981. unsigned long min_count, ret;
  1982. if (hstate_is_gigantic(h) && !gigantic_page_supported())
  1983. return h->max_huge_pages;
  1984. /*
  1985. * Increase the pool size
  1986. * First take pages out of surplus state. Then make up the
  1987. * remaining difference by allocating fresh huge pages.
  1988. *
  1989. * We might race with alloc_surplus_huge_page() here and be unable
  1990. * to convert a surplus huge page to a normal huge page. That is
  1991. * not critical, though, it just means the overall size of the
  1992. * pool might be one hugepage larger than it needs to be, but
  1993. * within all the constraints specified by the sysctls.
  1994. */
  1995. spin_lock(&hugetlb_lock);
  1996. while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
  1997. if (!adjust_pool_surplus(h, nodes_allowed, -1))
  1998. break;
  1999. }
  2000. while (count > persistent_huge_pages(h)) {
  2001. /*
  2002. * If this allocation races such that we no longer need the
  2003. * page, free_huge_page will handle it by freeing the page
  2004. * and reducing the surplus.
  2005. */
  2006. spin_unlock(&hugetlb_lock);
  2007. /* yield cpu to avoid soft lockup */
  2008. cond_resched();
  2009. ret = alloc_pool_huge_page(h, nodes_allowed);
  2010. spin_lock(&hugetlb_lock);
  2011. if (!ret)
  2012. goto out;
  2013. /* Bail for signals. Probably ctrl-c from user */
  2014. if (signal_pending(current))
  2015. goto out;
  2016. }
  2017. /*
  2018. * Decrease the pool size
  2019. * First return free pages to the buddy allocator (being careful
  2020. * to keep enough around to satisfy reservations). Then place
  2021. * pages into surplus state as needed so the pool will shrink
  2022. * to the desired size as pages become free.
  2023. *
  2024. * By placing pages into the surplus state independent of the
  2025. * overcommit value, we are allowing the surplus pool size to
  2026. * exceed overcommit. There are few sane options here. Since
  2027. * alloc_surplus_huge_page() is checking the global counter,
  2028. * though, we'll note that we're not allowed to exceed surplus
  2029. * and won't grow the pool anywhere else. Not until one of the
  2030. * sysctls are changed, or the surplus pages go out of use.
  2031. */
  2032. min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
  2033. min_count = max(count, min_count);
  2034. try_to_free_low(h, min_count, nodes_allowed);
  2035. while (min_count < persistent_huge_pages(h)) {
  2036. if (!free_pool_huge_page(h, nodes_allowed, 0))
  2037. break;
  2038. cond_resched_lock(&hugetlb_lock);
  2039. }
  2040. while (count < persistent_huge_pages(h)) {
  2041. if (!adjust_pool_surplus(h, nodes_allowed, 1))
  2042. break;
  2043. }
  2044. out:
  2045. ret = persistent_huge_pages(h);
  2046. spin_unlock(&hugetlb_lock);
  2047. return ret;
  2048. }
  2049. #define HSTATE_ATTR_RO(_name) \
  2050. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  2051. #define HSTATE_ATTR(_name) \
  2052. static struct kobj_attribute _name##_attr = \
  2053. __ATTR(_name, 0644, _name##_show, _name##_store)
  2054. static struct kobject *hugepages_kobj;
  2055. static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  2056. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
  2057. static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
  2058. {
  2059. int i;
  2060. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  2061. if (hstate_kobjs[i] == kobj) {
  2062. if (nidp)
  2063. *nidp = NUMA_NO_NODE;
  2064. return &hstates[i];
  2065. }
  2066. return kobj_to_node_hstate(kobj, nidp);
  2067. }
  2068. static ssize_t nr_hugepages_show_common(struct kobject *kobj,
  2069. struct kobj_attribute *attr, char *buf)
  2070. {
  2071. struct hstate *h;
  2072. unsigned long nr_huge_pages;
  2073. int nid;
  2074. h = kobj_to_hstate(kobj, &nid);
  2075. if (nid == NUMA_NO_NODE)
  2076. nr_huge_pages = h->nr_huge_pages;
  2077. else
  2078. nr_huge_pages = h->nr_huge_pages_node[nid];
  2079. return sprintf(buf, "%lu\n", nr_huge_pages);
  2080. }
  2081. static ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
  2082. struct hstate *h, int nid,
  2083. unsigned long count, size_t len)
  2084. {
  2085. int err;
  2086. NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
  2087. if (hstate_is_gigantic(h) && !gigantic_page_supported()) {
  2088. err = -EINVAL;
  2089. goto out;
  2090. }
  2091. if (nid == NUMA_NO_NODE) {
  2092. /*
  2093. * global hstate attribute
  2094. */
  2095. if (!(obey_mempolicy &&
  2096. init_nodemask_of_mempolicy(nodes_allowed))) {
  2097. NODEMASK_FREE(nodes_allowed);
  2098. nodes_allowed = &node_states[N_MEMORY];
  2099. }
  2100. } else if (nodes_allowed) {
  2101. /*
  2102. * per node hstate attribute: adjust count to global,
  2103. * but restrict alloc/free to the specified node.
  2104. */
  2105. count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
  2106. init_nodemask_of_node(nodes_allowed, nid);
  2107. } else
  2108. nodes_allowed = &node_states[N_MEMORY];
  2109. h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed);
  2110. if (nodes_allowed != &node_states[N_MEMORY])
  2111. NODEMASK_FREE(nodes_allowed);
  2112. return len;
  2113. out:
  2114. NODEMASK_FREE(nodes_allowed);
  2115. return err;
  2116. }
  2117. static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
  2118. struct kobject *kobj, const char *buf,
  2119. size_t len)
  2120. {
  2121. struct hstate *h;
  2122. unsigned long count;
  2123. int nid;
  2124. int err;
  2125. err = kstrtoul(buf, 10, &count);
  2126. if (err)
  2127. return err;
  2128. h = kobj_to_hstate(kobj, &nid);
  2129. return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len);
  2130. }
  2131. static ssize_t nr_hugepages_show(struct kobject *kobj,
  2132. struct kobj_attribute *attr, char *buf)
  2133. {
  2134. return nr_hugepages_show_common(kobj, attr, buf);
  2135. }
  2136. static ssize_t nr_hugepages_store(struct kobject *kobj,
  2137. struct kobj_attribute *attr, const char *buf, size_t len)
  2138. {
  2139. return nr_hugepages_store_common(false, kobj, buf, len);
  2140. }
  2141. HSTATE_ATTR(nr_hugepages);
  2142. #ifdef CONFIG_NUMA
  2143. /*
  2144. * hstate attribute for optionally mempolicy-based constraint on persistent
  2145. * huge page alloc/free.
  2146. */
  2147. static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
  2148. struct kobj_attribute *attr, char *buf)
  2149. {
  2150. return nr_hugepages_show_common(kobj, attr, buf);
  2151. }
  2152. static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
  2153. struct kobj_attribute *attr, const char *buf, size_t len)
  2154. {
  2155. return nr_hugepages_store_common(true, kobj, buf, len);
  2156. }
  2157. HSTATE_ATTR(nr_hugepages_mempolicy);
  2158. #endif
  2159. static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
  2160. struct kobj_attribute *attr, char *buf)
  2161. {
  2162. struct hstate *h = kobj_to_hstate(kobj, NULL);
  2163. return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
  2164. }
  2165. static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
  2166. struct kobj_attribute *attr, const char *buf, size_t count)
  2167. {
  2168. int err;
  2169. unsigned long input;
  2170. struct hstate *h = kobj_to_hstate(kobj, NULL);
  2171. if (hstate_is_gigantic(h))
  2172. return -EINVAL;
  2173. err = kstrtoul(buf, 10, &input);
  2174. if (err)
  2175. return err;
  2176. spin_lock(&hugetlb_lock);
  2177. h->nr_overcommit_huge_pages = input;
  2178. spin_unlock(&hugetlb_lock);
  2179. return count;
  2180. }
  2181. HSTATE_ATTR(nr_overcommit_hugepages);
  2182. static ssize_t free_hugepages_show(struct kobject *kobj,
  2183. struct kobj_attribute *attr, char *buf)
  2184. {
  2185. struct hstate *h;
  2186. unsigned long free_huge_pages;
  2187. int nid;
  2188. h = kobj_to_hstate(kobj, &nid);
  2189. if (nid == NUMA_NO_NODE)
  2190. free_huge_pages = h->free_huge_pages;
  2191. else
  2192. free_huge_pages = h->free_huge_pages_node[nid];
  2193. return sprintf(buf, "%lu\n", free_huge_pages);
  2194. }
  2195. HSTATE_ATTR_RO(free_hugepages);
  2196. static ssize_t resv_hugepages_show(struct kobject *kobj,
  2197. struct kobj_attribute *attr, char *buf)
  2198. {
  2199. struct hstate *h = kobj_to_hstate(kobj, NULL);
  2200. return sprintf(buf, "%lu\n", h->resv_huge_pages);
  2201. }
  2202. HSTATE_ATTR_RO(resv_hugepages);
  2203. static ssize_t surplus_hugepages_show(struct kobject *kobj,
  2204. struct kobj_attribute *attr, char *buf)
  2205. {
  2206. struct hstate *h;
  2207. unsigned long surplus_huge_pages;
  2208. int nid;
  2209. h = kobj_to_hstate(kobj, &nid);
  2210. if (nid == NUMA_NO_NODE)
  2211. surplus_huge_pages = h->surplus_huge_pages;
  2212. else
  2213. surplus_huge_pages = h->surplus_huge_pages_node[nid];
  2214. return sprintf(buf, "%lu\n", surplus_huge_pages);
  2215. }
  2216. HSTATE_ATTR_RO(surplus_hugepages);
  2217. static struct attribute *hstate_attrs[] = {
  2218. &nr_hugepages_attr.attr,
  2219. &nr_overcommit_hugepages_attr.attr,
  2220. &free_hugepages_attr.attr,
  2221. &resv_hugepages_attr.attr,
  2222. &surplus_hugepages_attr.attr,
  2223. #ifdef CONFIG_NUMA
  2224. &nr_hugepages_mempolicy_attr.attr,
  2225. #endif
  2226. NULL,
  2227. };
  2228. static const struct attribute_group hstate_attr_group = {
  2229. .attrs = hstate_attrs,
  2230. };
  2231. static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
  2232. struct kobject **hstate_kobjs,
  2233. const struct attribute_group *hstate_attr_group)
  2234. {
  2235. int retval;
  2236. int hi = hstate_index(h);
  2237. hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
  2238. if (!hstate_kobjs[hi])
  2239. return -ENOMEM;
  2240. retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
  2241. if (retval)
  2242. kobject_put(hstate_kobjs[hi]);
  2243. return retval;
  2244. }
  2245. static void __init hugetlb_sysfs_init(void)
  2246. {
  2247. struct hstate *h;
  2248. int err;
  2249. hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
  2250. if (!hugepages_kobj)
  2251. return;
  2252. for_each_hstate(h) {
  2253. err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
  2254. hstate_kobjs, &hstate_attr_group);
  2255. if (err)
  2256. pr_err("Hugetlb: Unable to add hstate %s", h->name);
  2257. }
  2258. }
  2259. #ifdef CONFIG_NUMA
  2260. /*
  2261. * node_hstate/s - associate per node hstate attributes, via their kobjects,
  2262. * with node devices in node_devices[] using a parallel array. The array
  2263. * index of a node device or _hstate == node id.
  2264. * This is here to avoid any static dependency of the node device driver, in
  2265. * the base kernel, on the hugetlb module.
  2266. */
  2267. struct node_hstate {
  2268. struct kobject *hugepages_kobj;
  2269. struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  2270. };
  2271. static struct node_hstate node_hstates[MAX_NUMNODES];
  2272. /*
  2273. * A subset of global hstate attributes for node devices
  2274. */
  2275. static struct attribute *per_node_hstate_attrs[] = {
  2276. &nr_hugepages_attr.attr,
  2277. &free_hugepages_attr.attr,
  2278. &surplus_hugepages_attr.attr,
  2279. NULL,
  2280. };
  2281. static const struct attribute_group per_node_hstate_attr_group = {
  2282. .attrs = per_node_hstate_attrs,
  2283. };
  2284. /*
  2285. * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
  2286. * Returns node id via non-NULL nidp.
  2287. */
  2288. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  2289. {
  2290. int nid;
  2291. for (nid = 0; nid < nr_node_ids; nid++) {
  2292. struct node_hstate *nhs = &node_hstates[nid];
  2293. int i;
  2294. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  2295. if (nhs->hstate_kobjs[i] == kobj) {
  2296. if (nidp)
  2297. *nidp = nid;
  2298. return &hstates[i];
  2299. }
  2300. }
  2301. BUG();
  2302. return NULL;
  2303. }
  2304. /*
  2305. * Unregister hstate attributes from a single node device.
  2306. * No-op if no hstate attributes attached.
  2307. */
  2308. static void hugetlb_unregister_node(struct node *node)
  2309. {
  2310. struct hstate *h;
  2311. struct node_hstate *nhs = &node_hstates[node->dev.id];
  2312. if (!nhs->hugepages_kobj)
  2313. return; /* no hstate attributes */
  2314. for_each_hstate(h) {
  2315. int idx = hstate_index(h);
  2316. if (nhs->hstate_kobjs[idx]) {
  2317. kobject_put(nhs->hstate_kobjs[idx]);
  2318. nhs->hstate_kobjs[idx] = NULL;
  2319. }
  2320. }
  2321. kobject_put(nhs->hugepages_kobj);
  2322. nhs->hugepages_kobj = NULL;
  2323. }
  2324. /*
  2325. * Register hstate attributes for a single node device.
  2326. * No-op if attributes already registered.
  2327. */
  2328. static void hugetlb_register_node(struct node *node)
  2329. {
  2330. struct hstate *h;
  2331. struct node_hstate *nhs = &node_hstates[node->dev.id];
  2332. int err;
  2333. if (nhs->hugepages_kobj)
  2334. return; /* already allocated */
  2335. nhs->hugepages_kobj = kobject_create_and_add("hugepages",
  2336. &node->dev.kobj);
  2337. if (!nhs->hugepages_kobj)
  2338. return;
  2339. for_each_hstate(h) {
  2340. err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
  2341. nhs->hstate_kobjs,
  2342. &per_node_hstate_attr_group);
  2343. if (err) {
  2344. pr_err("Hugetlb: Unable to add hstate %s for node %d\n",
  2345. h->name, node->dev.id);
  2346. hugetlb_unregister_node(node);
  2347. break;
  2348. }
  2349. }
  2350. }
  2351. /*
  2352. * hugetlb init time: register hstate attributes for all registered node
  2353. * devices of nodes that have memory. All on-line nodes should have
  2354. * registered their associated device by this time.
  2355. */
  2356. static void __init hugetlb_register_all_nodes(void)
  2357. {
  2358. int nid;
  2359. for_each_node_state(nid, N_MEMORY) {
  2360. struct node *node = node_devices[nid];
  2361. if (node->dev.id == nid)
  2362. hugetlb_register_node(node);
  2363. }
  2364. /*
  2365. * Let the node device driver know we're here so it can
  2366. * [un]register hstate attributes on node hotplug.
  2367. */
  2368. register_hugetlbfs_with_node(hugetlb_register_node,
  2369. hugetlb_unregister_node);
  2370. }
  2371. #else /* !CONFIG_NUMA */
  2372. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  2373. {
  2374. BUG();
  2375. if (nidp)
  2376. *nidp = -1;
  2377. return NULL;
  2378. }
  2379. static void hugetlb_register_all_nodes(void) { }
  2380. #endif
  2381. static int __init hugetlb_init(void)
  2382. {
  2383. int i;
  2384. if (!hugepages_supported())
  2385. return 0;
  2386. if (!size_to_hstate(default_hstate_size)) {
  2387. if (default_hstate_size != 0) {
  2388. pr_err("HugeTLB: unsupported default_hugepagesz %lu. Reverting to %lu\n",
  2389. default_hstate_size, HPAGE_SIZE);
  2390. }
  2391. default_hstate_size = HPAGE_SIZE;
  2392. if (!size_to_hstate(default_hstate_size))
  2393. hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
  2394. }
  2395. default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
  2396. if (default_hstate_max_huge_pages) {
  2397. if (!default_hstate.max_huge_pages)
  2398. default_hstate.max_huge_pages = default_hstate_max_huge_pages;
  2399. }
  2400. hugetlb_init_hstates();
  2401. gather_bootmem_prealloc();
  2402. report_hugepages();
  2403. hugetlb_sysfs_init();
  2404. hugetlb_register_all_nodes();
  2405. hugetlb_cgroup_file_init();
  2406. #ifdef CONFIG_SMP
  2407. num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus());
  2408. #else
  2409. num_fault_mutexes = 1;
  2410. #endif
  2411. hugetlb_fault_mutex_table =
  2412. kmalloc_array(num_fault_mutexes, sizeof(struct mutex),
  2413. GFP_KERNEL);
  2414. BUG_ON(!hugetlb_fault_mutex_table);
  2415. for (i = 0; i < num_fault_mutexes; i++)
  2416. mutex_init(&hugetlb_fault_mutex_table[i]);
  2417. return 0;
  2418. }
  2419. subsys_initcall(hugetlb_init);
  2420. /* Should be called on processing a hugepagesz=... option */
  2421. void __init hugetlb_bad_size(void)
  2422. {
  2423. parsed_valid_hugepagesz = false;
  2424. }
  2425. void __init hugetlb_add_hstate(unsigned int order)
  2426. {
  2427. struct hstate *h;
  2428. unsigned long i;
  2429. if (size_to_hstate(PAGE_SIZE << order)) {
  2430. pr_warn("hugepagesz= specified twice, ignoring\n");
  2431. return;
  2432. }
  2433. BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
  2434. BUG_ON(order == 0);
  2435. h = &hstates[hugetlb_max_hstate++];
  2436. h->order = order;
  2437. h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
  2438. h->nr_huge_pages = 0;
  2439. h->free_huge_pages = 0;
  2440. for (i = 0; i < MAX_NUMNODES; ++i)
  2441. INIT_LIST_HEAD(&h->hugepage_freelists[i]);
  2442. INIT_LIST_HEAD(&h->hugepage_activelist);
  2443. h->next_nid_to_alloc = first_memory_node;
  2444. h->next_nid_to_free = first_memory_node;
  2445. snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
  2446. huge_page_size(h)/1024);
  2447. parsed_hstate = h;
  2448. }
  2449. static int __init hugetlb_nrpages_setup(char *s)
  2450. {
  2451. unsigned long *mhp;
  2452. static unsigned long *last_mhp;
  2453. if (!parsed_valid_hugepagesz) {
  2454. pr_warn("hugepages = %s preceded by "
  2455. "an unsupported hugepagesz, ignoring\n", s);
  2456. parsed_valid_hugepagesz = true;
  2457. return 1;
  2458. }
  2459. /*
  2460. * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
  2461. * so this hugepages= parameter goes to the "default hstate".
  2462. */
  2463. else if (!hugetlb_max_hstate)
  2464. mhp = &default_hstate_max_huge_pages;
  2465. else
  2466. mhp = &parsed_hstate->max_huge_pages;
  2467. if (mhp == last_mhp) {
  2468. pr_warn("hugepages= specified twice without interleaving hugepagesz=, ignoring\n");
  2469. return 1;
  2470. }
  2471. if (sscanf(s, "%lu", mhp) <= 0)
  2472. *mhp = 0;
  2473. /*
  2474. * Global state is always initialized later in hugetlb_init.
  2475. * But we need to allocate >= MAX_ORDER hstates here early to still
  2476. * use the bootmem allocator.
  2477. */
  2478. if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER)
  2479. hugetlb_hstate_alloc_pages(parsed_hstate);
  2480. last_mhp = mhp;
  2481. return 1;
  2482. }
  2483. __setup("hugepages=", hugetlb_nrpages_setup);
  2484. static int __init hugetlb_default_setup(char *s)
  2485. {
  2486. default_hstate_size = memparse(s, &s);
  2487. return 1;
  2488. }
  2489. __setup("default_hugepagesz=", hugetlb_default_setup);
  2490. static unsigned int cpuset_mems_nr(unsigned int *array)
  2491. {
  2492. int node;
  2493. unsigned int nr = 0;
  2494. for_each_node_mask(node, cpuset_current_mems_allowed)
  2495. nr += array[node];
  2496. return nr;
  2497. }
  2498. #ifdef CONFIG_SYSCTL
  2499. static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
  2500. struct ctl_table *table, int write,
  2501. void __user *buffer, size_t *length, loff_t *ppos)
  2502. {
  2503. struct hstate *h = &default_hstate;
  2504. unsigned long tmp = h->max_huge_pages;
  2505. int ret;
  2506. if (!hugepages_supported())
  2507. return -EOPNOTSUPP;
  2508. table->data = &tmp;
  2509. table->maxlen = sizeof(unsigned long);
  2510. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  2511. if (ret)
  2512. goto out;
  2513. if (write)
  2514. ret = __nr_hugepages_store_common(obey_mempolicy, h,
  2515. NUMA_NO_NODE, tmp, *length);
  2516. out:
  2517. return ret;
  2518. }
  2519. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  2520. void __user *buffer, size_t *length, loff_t *ppos)
  2521. {
  2522. return hugetlb_sysctl_handler_common(false, table, write,
  2523. buffer, length, ppos);
  2524. }
  2525. #ifdef CONFIG_NUMA
  2526. int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
  2527. void __user *buffer, size_t *length, loff_t *ppos)
  2528. {
  2529. return hugetlb_sysctl_handler_common(true, table, write,
  2530. buffer, length, ppos);
  2531. }
  2532. #endif /* CONFIG_NUMA */
  2533. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  2534. void __user *buffer,
  2535. size_t *length, loff_t *ppos)
  2536. {
  2537. struct hstate *h = &default_hstate;
  2538. unsigned long tmp;
  2539. int ret;
  2540. if (!hugepages_supported())
  2541. return -EOPNOTSUPP;
  2542. tmp = h->nr_overcommit_huge_pages;
  2543. if (write && hstate_is_gigantic(h))
  2544. return -EINVAL;
  2545. table->data = &tmp;
  2546. table->maxlen = sizeof(unsigned long);
  2547. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  2548. if (ret)
  2549. goto out;
  2550. if (write) {
  2551. spin_lock(&hugetlb_lock);
  2552. h->nr_overcommit_huge_pages = tmp;
  2553. spin_unlock(&hugetlb_lock);
  2554. }
  2555. out:
  2556. return ret;
  2557. }
  2558. #endif /* CONFIG_SYSCTL */
  2559. void hugetlb_report_meminfo(struct seq_file *m)
  2560. {
  2561. struct hstate *h;
  2562. unsigned long total = 0;
  2563. if (!hugepages_supported())
  2564. return;
  2565. for_each_hstate(h) {
  2566. unsigned long count = h->nr_huge_pages;
  2567. total += (PAGE_SIZE << huge_page_order(h)) * count;
  2568. if (h == &default_hstate)
  2569. seq_printf(m,
  2570. "HugePages_Total: %5lu\n"
  2571. "HugePages_Free: %5lu\n"
  2572. "HugePages_Rsvd: %5lu\n"
  2573. "HugePages_Surp: %5lu\n"
  2574. "Hugepagesize: %8lu kB\n",
  2575. count,
  2576. h->free_huge_pages,
  2577. h->resv_huge_pages,
  2578. h->surplus_huge_pages,
  2579. (PAGE_SIZE << huge_page_order(h)) / 1024);
  2580. }
  2581. seq_printf(m, "Hugetlb: %8lu kB\n", total / 1024);
  2582. }
  2583. int hugetlb_report_node_meminfo(int nid, char *buf)
  2584. {
  2585. struct hstate *h = &default_hstate;
  2586. if (!hugepages_supported())
  2587. return 0;
  2588. return sprintf(buf,
  2589. "Node %d HugePages_Total: %5u\n"
  2590. "Node %d HugePages_Free: %5u\n"
  2591. "Node %d HugePages_Surp: %5u\n",
  2592. nid, h->nr_huge_pages_node[nid],
  2593. nid, h->free_huge_pages_node[nid],
  2594. nid, h->surplus_huge_pages_node[nid]);
  2595. }
  2596. void hugetlb_show_meminfo(void)
  2597. {
  2598. struct hstate *h;
  2599. int nid;
  2600. if (!hugepages_supported())
  2601. return;
  2602. for_each_node_state(nid, N_MEMORY)
  2603. for_each_hstate(h)
  2604. pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
  2605. nid,
  2606. h->nr_huge_pages_node[nid],
  2607. h->free_huge_pages_node[nid],
  2608. h->surplus_huge_pages_node[nid],
  2609. 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
  2610. }
  2611. void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm)
  2612. {
  2613. seq_printf(m, "HugetlbPages:\t%8lu kB\n",
  2614. atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10));
  2615. }
  2616. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  2617. unsigned long hugetlb_total_pages(void)
  2618. {
  2619. struct hstate *h;
  2620. unsigned long nr_total_pages = 0;
  2621. for_each_hstate(h)
  2622. nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
  2623. return nr_total_pages;
  2624. }
  2625. static int hugetlb_acct_memory(struct hstate *h, long delta)
  2626. {
  2627. int ret = -ENOMEM;
  2628. spin_lock(&hugetlb_lock);
  2629. /*
  2630. * When cpuset is configured, it breaks the strict hugetlb page
  2631. * reservation as the accounting is done on a global variable. Such
  2632. * reservation is completely rubbish in the presence of cpuset because
  2633. * the reservation is not checked against page availability for the
  2634. * current cpuset. Application can still potentially OOM'ed by kernel
  2635. * with lack of free htlb page in cpuset that the task is in.
  2636. * Attempt to enforce strict accounting with cpuset is almost
  2637. * impossible (or too ugly) because cpuset is too fluid that
  2638. * task or memory node can be dynamically moved between cpusets.
  2639. *
  2640. * The change of semantics for shared hugetlb mapping with cpuset is
  2641. * undesirable. However, in order to preserve some of the semantics,
  2642. * we fall back to check against current free page availability as
  2643. * a best attempt and hopefully to minimize the impact of changing
  2644. * semantics that cpuset has.
  2645. */
  2646. if (delta > 0) {
  2647. if (gather_surplus_pages(h, delta) < 0)
  2648. goto out;
  2649. if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
  2650. return_unused_surplus_pages(h, delta);
  2651. goto out;
  2652. }
  2653. }
  2654. ret = 0;
  2655. if (delta < 0)
  2656. return_unused_surplus_pages(h, (unsigned long) -delta);
  2657. out:
  2658. spin_unlock(&hugetlb_lock);
  2659. return ret;
  2660. }
  2661. static void hugetlb_vm_op_open(struct vm_area_struct *vma)
  2662. {
  2663. struct resv_map *resv = vma_resv_map(vma);
  2664. /*
  2665. * This new VMA should share its siblings reservation map if present.
  2666. * The VMA will only ever have a valid reservation map pointer where
  2667. * it is being copied for another still existing VMA. As that VMA
  2668. * has a reference to the reservation map it cannot disappear until
  2669. * after this open call completes. It is therefore safe to take a
  2670. * new reference here without additional locking.
  2671. */
  2672. if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
  2673. kref_get(&resv->refs);
  2674. }
  2675. static void hugetlb_vm_op_close(struct vm_area_struct *vma)
  2676. {
  2677. struct hstate *h = hstate_vma(vma);
  2678. struct resv_map *resv = vma_resv_map(vma);
  2679. struct hugepage_subpool *spool = subpool_vma(vma);
  2680. unsigned long reserve, start, end;
  2681. long gbl_reserve;
  2682. if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
  2683. return;
  2684. start = vma_hugecache_offset(h, vma, vma->vm_start);
  2685. end = vma_hugecache_offset(h, vma, vma->vm_end);
  2686. reserve = (end - start) - region_count(resv, start, end);
  2687. kref_put(&resv->refs, resv_map_release);
  2688. if (reserve) {
  2689. /*
  2690. * Decrement reserve counts. The global reserve count may be
  2691. * adjusted if the subpool has a minimum size.
  2692. */
  2693. gbl_reserve = hugepage_subpool_put_pages(spool, reserve);
  2694. hugetlb_acct_memory(h, -gbl_reserve);
  2695. }
  2696. }
  2697. static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
  2698. {
  2699. if (addr & ~(huge_page_mask(hstate_vma(vma))))
  2700. return -EINVAL;
  2701. return 0;
  2702. }
  2703. static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
  2704. {
  2705. struct hstate *hstate = hstate_vma(vma);
  2706. return 1UL << huge_page_shift(hstate);
  2707. }
  2708. /*
  2709. * We cannot handle pagefaults against hugetlb pages at all. They cause
  2710. * handle_mm_fault() to try to instantiate regular-sized pages in the
  2711. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  2712. * this far.
  2713. */
  2714. static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf)
  2715. {
  2716. BUG();
  2717. return 0;
  2718. }
  2719. /*
  2720. * When a new function is introduced to vm_operations_struct and added
  2721. * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops.
  2722. * This is because under System V memory model, mappings created via
  2723. * shmget/shmat with "huge page" specified are backed by hugetlbfs files,
  2724. * their original vm_ops are overwritten with shm_vm_ops.
  2725. */
  2726. const struct vm_operations_struct hugetlb_vm_ops = {
  2727. .fault = hugetlb_vm_op_fault,
  2728. .open = hugetlb_vm_op_open,
  2729. .close = hugetlb_vm_op_close,
  2730. .split = hugetlb_vm_op_split,
  2731. .pagesize = hugetlb_vm_op_pagesize,
  2732. };
  2733. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  2734. int writable)
  2735. {
  2736. pte_t entry;
  2737. if (writable) {
  2738. entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
  2739. vma->vm_page_prot)));
  2740. } else {
  2741. entry = huge_pte_wrprotect(mk_huge_pte(page,
  2742. vma->vm_page_prot));
  2743. }
  2744. entry = pte_mkyoung(entry);
  2745. entry = pte_mkhuge(entry);
  2746. entry = arch_make_huge_pte(entry, vma, page, writable);
  2747. return entry;
  2748. }
  2749. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  2750. unsigned long address, pte_t *ptep)
  2751. {
  2752. pte_t entry;
  2753. entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)));
  2754. if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
  2755. update_mmu_cache(vma, address, ptep);
  2756. }
  2757. bool is_hugetlb_entry_migration(pte_t pte)
  2758. {
  2759. swp_entry_t swp;
  2760. if (huge_pte_none(pte) || pte_present(pte))
  2761. return false;
  2762. swp = pte_to_swp_entry(pte);
  2763. if (non_swap_entry(swp) && is_migration_entry(swp))
  2764. return true;
  2765. else
  2766. return false;
  2767. }
  2768. static int is_hugetlb_entry_hwpoisoned(pte_t pte)
  2769. {
  2770. swp_entry_t swp;
  2771. if (huge_pte_none(pte) || pte_present(pte))
  2772. return 0;
  2773. swp = pte_to_swp_entry(pte);
  2774. if (non_swap_entry(swp) && is_hwpoison_entry(swp))
  2775. return 1;
  2776. else
  2777. return 0;
  2778. }
  2779. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  2780. struct vm_area_struct *vma)
  2781. {
  2782. pte_t *src_pte, *dst_pte, entry, dst_entry;
  2783. struct page *ptepage;
  2784. unsigned long addr;
  2785. int cow;
  2786. struct hstate *h = hstate_vma(vma);
  2787. unsigned long sz = huge_page_size(h);
  2788. unsigned long mmun_start; /* For mmu_notifiers */
  2789. unsigned long mmun_end; /* For mmu_notifiers */
  2790. int ret = 0;
  2791. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  2792. mmun_start = vma->vm_start;
  2793. mmun_end = vma->vm_end;
  2794. if (cow)
  2795. mmu_notifier_invalidate_range_start(src, mmun_start, mmun_end);
  2796. for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
  2797. spinlock_t *src_ptl, *dst_ptl;
  2798. src_pte = huge_pte_offset(src, addr, sz);
  2799. if (!src_pte)
  2800. continue;
  2801. dst_pte = huge_pte_alloc(dst, addr, sz);
  2802. if (!dst_pte) {
  2803. ret = -ENOMEM;
  2804. break;
  2805. }
  2806. /*
  2807. * If the pagetables are shared don't copy or take references.
  2808. * dst_pte == src_pte is the common case of src/dest sharing.
  2809. *
  2810. * However, src could have 'unshared' and dst shares with
  2811. * another vma. If dst_pte !none, this implies sharing.
  2812. * Check here before taking page table lock, and once again
  2813. * after taking the lock below.
  2814. */
  2815. dst_entry = huge_ptep_get(dst_pte);
  2816. if ((dst_pte == src_pte) || !huge_pte_none(dst_entry))
  2817. continue;
  2818. dst_ptl = huge_pte_lock(h, dst, dst_pte);
  2819. src_ptl = huge_pte_lockptr(h, src, src_pte);
  2820. spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
  2821. entry = huge_ptep_get(src_pte);
  2822. dst_entry = huge_ptep_get(dst_pte);
  2823. if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) {
  2824. /*
  2825. * Skip if src entry none. Also, skip in the
  2826. * unlikely case dst entry !none as this implies
  2827. * sharing with another vma.
  2828. */
  2829. ;
  2830. } else if (unlikely(is_hugetlb_entry_migration(entry) ||
  2831. is_hugetlb_entry_hwpoisoned(entry))) {
  2832. swp_entry_t swp_entry = pte_to_swp_entry(entry);
  2833. if (is_write_migration_entry(swp_entry) && cow) {
  2834. /*
  2835. * COW mappings require pages in both
  2836. * parent and child to be set to read.
  2837. */
  2838. make_migration_entry_read(&swp_entry);
  2839. entry = swp_entry_to_pte(swp_entry);
  2840. set_huge_swap_pte_at(src, addr, src_pte,
  2841. entry, sz);
  2842. }
  2843. set_huge_swap_pte_at(dst, addr, dst_pte, entry, sz);
  2844. } else {
  2845. if (cow) {
  2846. /*
  2847. * No need to notify as we are downgrading page
  2848. * table protection not changing it to point
  2849. * to a new page.
  2850. *
  2851. * See Documentation/vm/mmu_notifier.rst
  2852. */
  2853. huge_ptep_set_wrprotect(src, addr, src_pte);
  2854. }
  2855. entry = huge_ptep_get(src_pte);
  2856. ptepage = pte_page(entry);
  2857. get_page(ptepage);
  2858. page_dup_rmap(ptepage, true);
  2859. set_huge_pte_at(dst, addr, dst_pte, entry);
  2860. hugetlb_count_add(pages_per_huge_page(h), dst);
  2861. }
  2862. spin_unlock(src_ptl);
  2863. spin_unlock(dst_ptl);
  2864. }
  2865. if (cow)
  2866. mmu_notifier_invalidate_range_end(src, mmun_start, mmun_end);
  2867. return ret;
  2868. }
  2869. void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  2870. unsigned long start, unsigned long end,
  2871. struct page *ref_page)
  2872. {
  2873. struct mm_struct *mm = vma->vm_mm;
  2874. unsigned long address;
  2875. pte_t *ptep;
  2876. pte_t pte;
  2877. spinlock_t *ptl;
  2878. struct page *page;
  2879. struct hstate *h = hstate_vma(vma);
  2880. unsigned long sz = huge_page_size(h);
  2881. unsigned long mmun_start = start; /* For mmu_notifiers */
  2882. unsigned long mmun_end = end; /* For mmu_notifiers */
  2883. WARN_ON(!is_vm_hugetlb_page(vma));
  2884. BUG_ON(start & ~huge_page_mask(h));
  2885. BUG_ON(end & ~huge_page_mask(h));
  2886. /*
  2887. * This is a hugetlb vma, all the pte entries should point
  2888. * to huge page.
  2889. */
  2890. tlb_remove_check_page_size_change(tlb, sz);
  2891. tlb_start_vma(tlb, vma);
  2892. /*
  2893. * If sharing possible, alert mmu notifiers of worst case.
  2894. */
  2895. adjust_range_if_pmd_sharing_possible(vma, &mmun_start, &mmun_end);
  2896. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  2897. address = start;
  2898. for (; address < end; address += sz) {
  2899. ptep = huge_pte_offset(mm, address, sz);
  2900. if (!ptep)
  2901. continue;
  2902. ptl = huge_pte_lock(h, mm, ptep);
  2903. if (huge_pmd_unshare(mm, &address, ptep)) {
  2904. spin_unlock(ptl);
  2905. /*
  2906. * We just unmapped a page of PMDs by clearing a PUD.
  2907. * The caller's TLB flush range should cover this area.
  2908. */
  2909. continue;
  2910. }
  2911. pte = huge_ptep_get(ptep);
  2912. if (huge_pte_none(pte)) {
  2913. spin_unlock(ptl);
  2914. continue;
  2915. }
  2916. /*
  2917. * Migrating hugepage or HWPoisoned hugepage is already
  2918. * unmapped and its refcount is dropped, so just clear pte here.
  2919. */
  2920. if (unlikely(!pte_present(pte))) {
  2921. huge_pte_clear(mm, address, ptep, sz);
  2922. spin_unlock(ptl);
  2923. continue;
  2924. }
  2925. page = pte_page(pte);
  2926. /*
  2927. * If a reference page is supplied, it is because a specific
  2928. * page is being unmapped, not a range. Ensure the page we
  2929. * are about to unmap is the actual page of interest.
  2930. */
  2931. if (ref_page) {
  2932. if (page != ref_page) {
  2933. spin_unlock(ptl);
  2934. continue;
  2935. }
  2936. /*
  2937. * Mark the VMA as having unmapped its page so that
  2938. * future faults in this VMA will fail rather than
  2939. * looking like data was lost
  2940. */
  2941. set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
  2942. }
  2943. pte = huge_ptep_get_and_clear(mm, address, ptep);
  2944. tlb_remove_huge_tlb_entry(h, tlb, ptep, address);
  2945. if (huge_pte_dirty(pte))
  2946. set_page_dirty(page);
  2947. hugetlb_count_sub(pages_per_huge_page(h), mm);
  2948. page_remove_rmap(page, true);
  2949. spin_unlock(ptl);
  2950. tlb_remove_page_size(tlb, page, huge_page_size(h));
  2951. /*
  2952. * Bail out after unmapping reference page if supplied
  2953. */
  2954. if (ref_page)
  2955. break;
  2956. }
  2957. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  2958. tlb_end_vma(tlb, vma);
  2959. }
  2960. void __unmap_hugepage_range_final(struct mmu_gather *tlb,
  2961. struct vm_area_struct *vma, unsigned long start,
  2962. unsigned long end, struct page *ref_page)
  2963. {
  2964. __unmap_hugepage_range(tlb, vma, start, end, ref_page);
  2965. /*
  2966. * Clear this flag so that x86's huge_pmd_share page_table_shareable
  2967. * test will fail on a vma being torn down, and not grab a page table
  2968. * on its way out. We're lucky that the flag has such an appropriate
  2969. * name, and can in fact be safely cleared here. We could clear it
  2970. * before the __unmap_hugepage_range above, but all that's necessary
  2971. * is to clear it before releasing the i_mmap_rwsem. This works
  2972. * because in the context this is called, the VMA is about to be
  2973. * destroyed and the i_mmap_rwsem is held.
  2974. */
  2975. vma->vm_flags &= ~VM_MAYSHARE;
  2976. }
  2977. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  2978. unsigned long end, struct page *ref_page)
  2979. {
  2980. struct mm_struct *mm;
  2981. struct mmu_gather tlb;
  2982. unsigned long tlb_start = start;
  2983. unsigned long tlb_end = end;
  2984. /*
  2985. * If shared PMDs were possibly used within this vma range, adjust
  2986. * start/end for worst case tlb flushing.
  2987. * Note that we can not be sure if PMDs are shared until we try to
  2988. * unmap pages. However, we want to make sure TLB flushing covers
  2989. * the largest possible range.
  2990. */
  2991. adjust_range_if_pmd_sharing_possible(vma, &tlb_start, &tlb_end);
  2992. mm = vma->vm_mm;
  2993. tlb_gather_mmu(&tlb, mm, tlb_start, tlb_end);
  2994. __unmap_hugepage_range(&tlb, vma, start, end, ref_page);
  2995. tlb_finish_mmu(&tlb, tlb_start, tlb_end);
  2996. }
  2997. /*
  2998. * This is called when the original mapper is failing to COW a MAP_PRIVATE
  2999. * mappping it owns the reserve page for. The intention is to unmap the page
  3000. * from other VMAs and let the children be SIGKILLed if they are faulting the
  3001. * same region.
  3002. */
  3003. static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
  3004. struct page *page, unsigned long address)
  3005. {
  3006. struct hstate *h = hstate_vma(vma);
  3007. struct vm_area_struct *iter_vma;
  3008. struct address_space *mapping;
  3009. pgoff_t pgoff;
  3010. /*
  3011. * vm_pgoff is in PAGE_SIZE units, hence the different calculation
  3012. * from page cache lookup which is in HPAGE_SIZE units.
  3013. */
  3014. address = address & huge_page_mask(h);
  3015. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
  3016. vma->vm_pgoff;
  3017. mapping = vma->vm_file->f_mapping;
  3018. /*
  3019. * Take the mapping lock for the duration of the table walk. As
  3020. * this mapping should be shared between all the VMAs,
  3021. * __unmap_hugepage_range() is called as the lock is already held
  3022. */
  3023. i_mmap_lock_write(mapping);
  3024. vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
  3025. /* Do not unmap the current VMA */
  3026. if (iter_vma == vma)
  3027. continue;
  3028. /*
  3029. * Shared VMAs have their own reserves and do not affect
  3030. * MAP_PRIVATE accounting but it is possible that a shared
  3031. * VMA is using the same page so check and skip such VMAs.
  3032. */
  3033. if (iter_vma->vm_flags & VM_MAYSHARE)
  3034. continue;
  3035. /*
  3036. * Unmap the page from other VMAs without their own reserves.
  3037. * They get marked to be SIGKILLed if they fault in these
  3038. * areas. This is because a future no-page fault on this VMA
  3039. * could insert a zeroed page instead of the data existing
  3040. * from the time of fork. This would look like data corruption
  3041. */
  3042. if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
  3043. unmap_hugepage_range(iter_vma, address,
  3044. address + huge_page_size(h), page);
  3045. }
  3046. i_mmap_unlock_write(mapping);
  3047. }
  3048. /*
  3049. * Hugetlb_cow() should be called with page lock of the original hugepage held.
  3050. * Called with hugetlb_instantiation_mutex held and pte_page locked so we
  3051. * cannot race with other handlers or page migration.
  3052. * Keep the pte_same checks anyway to make transition from the mutex easier.
  3053. */
  3054. static vm_fault_t hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  3055. unsigned long address, pte_t *ptep,
  3056. struct page *pagecache_page, spinlock_t *ptl)
  3057. {
  3058. pte_t pte;
  3059. struct hstate *h = hstate_vma(vma);
  3060. struct page *old_page, *new_page;
  3061. int outside_reserve = 0;
  3062. vm_fault_t ret = 0;
  3063. unsigned long mmun_start; /* For mmu_notifiers */
  3064. unsigned long mmun_end; /* For mmu_notifiers */
  3065. unsigned long haddr = address & huge_page_mask(h);
  3066. pte = huge_ptep_get(ptep);
  3067. old_page = pte_page(pte);
  3068. retry_avoidcopy:
  3069. /* If no-one else is actually using this page, avoid the copy
  3070. * and just make the page writable */
  3071. if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
  3072. page_move_anon_rmap(old_page, vma);
  3073. set_huge_ptep_writable(vma, haddr, ptep);
  3074. return 0;
  3075. }
  3076. /*
  3077. * If the process that created a MAP_PRIVATE mapping is about to
  3078. * perform a COW due to a shared page count, attempt to satisfy
  3079. * the allocation without using the existing reserves. The pagecache
  3080. * page is used to determine if the reserve at this address was
  3081. * consumed or not. If reserves were used, a partial faulted mapping
  3082. * at the time of fork() could consume its reserves on COW instead
  3083. * of the full address range.
  3084. */
  3085. if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
  3086. old_page != pagecache_page)
  3087. outside_reserve = 1;
  3088. get_page(old_page);
  3089. /*
  3090. * Drop page table lock as buddy allocator may be called. It will
  3091. * be acquired again before returning to the caller, as expected.
  3092. */
  3093. spin_unlock(ptl);
  3094. new_page = alloc_huge_page(vma, haddr, outside_reserve);
  3095. if (IS_ERR(new_page)) {
  3096. /*
  3097. * If a process owning a MAP_PRIVATE mapping fails to COW,
  3098. * it is due to references held by a child and an insufficient
  3099. * huge page pool. To guarantee the original mappers
  3100. * reliability, unmap the page from child processes. The child
  3101. * may get SIGKILLed if it later faults.
  3102. */
  3103. if (outside_reserve) {
  3104. put_page(old_page);
  3105. BUG_ON(huge_pte_none(pte));
  3106. unmap_ref_private(mm, vma, old_page, haddr);
  3107. BUG_ON(huge_pte_none(pte));
  3108. spin_lock(ptl);
  3109. ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
  3110. if (likely(ptep &&
  3111. pte_same(huge_ptep_get(ptep), pte)))
  3112. goto retry_avoidcopy;
  3113. /*
  3114. * race occurs while re-acquiring page table
  3115. * lock, and our job is done.
  3116. */
  3117. return 0;
  3118. }
  3119. ret = vmf_error(PTR_ERR(new_page));
  3120. goto out_release_old;
  3121. }
  3122. /*
  3123. * When the original hugepage is shared one, it does not have
  3124. * anon_vma prepared.
  3125. */
  3126. if (unlikely(anon_vma_prepare(vma))) {
  3127. ret = VM_FAULT_OOM;
  3128. goto out_release_all;
  3129. }
  3130. copy_user_huge_page(new_page, old_page, address, vma,
  3131. pages_per_huge_page(h));
  3132. __SetPageUptodate(new_page);
  3133. set_page_huge_active(new_page);
  3134. mmun_start = haddr;
  3135. mmun_end = mmun_start + huge_page_size(h);
  3136. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  3137. /*
  3138. * Retake the page table lock to check for racing updates
  3139. * before the page tables are altered
  3140. */
  3141. spin_lock(ptl);
  3142. ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
  3143. if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) {
  3144. ClearPagePrivate(new_page);
  3145. /* Break COW */
  3146. huge_ptep_clear_flush(vma, haddr, ptep);
  3147. mmu_notifier_invalidate_range(mm, mmun_start, mmun_end);
  3148. set_huge_pte_at(mm, haddr, ptep,
  3149. make_huge_pte(vma, new_page, 1));
  3150. page_remove_rmap(old_page, true);
  3151. hugepage_add_new_anon_rmap(new_page, vma, haddr);
  3152. /* Make the old page be freed below */
  3153. new_page = old_page;
  3154. }
  3155. spin_unlock(ptl);
  3156. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  3157. out_release_all:
  3158. restore_reserve_on_error(h, vma, haddr, new_page);
  3159. put_page(new_page);
  3160. out_release_old:
  3161. put_page(old_page);
  3162. spin_lock(ptl); /* Caller expects lock to be held */
  3163. return ret;
  3164. }
  3165. /* Return the pagecache page at a given address within a VMA */
  3166. static struct page *hugetlbfs_pagecache_page(struct hstate *h,
  3167. struct vm_area_struct *vma, unsigned long address)
  3168. {
  3169. struct address_space *mapping;
  3170. pgoff_t idx;
  3171. mapping = vma->vm_file->f_mapping;
  3172. idx = vma_hugecache_offset(h, vma, address);
  3173. return find_lock_page(mapping, idx);
  3174. }
  3175. /*
  3176. * Return whether there is a pagecache page to back given address within VMA.
  3177. * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
  3178. */
  3179. static bool hugetlbfs_pagecache_present(struct hstate *h,
  3180. struct vm_area_struct *vma, unsigned long address)
  3181. {
  3182. struct address_space *mapping;
  3183. pgoff_t idx;
  3184. struct page *page;
  3185. mapping = vma->vm_file->f_mapping;
  3186. idx = vma_hugecache_offset(h, vma, address);
  3187. page = find_get_page(mapping, idx);
  3188. if (page)
  3189. put_page(page);
  3190. return page != NULL;
  3191. }
  3192. int huge_add_to_page_cache(struct page *page, struct address_space *mapping,
  3193. pgoff_t idx)
  3194. {
  3195. struct inode *inode = mapping->host;
  3196. struct hstate *h = hstate_inode(inode);
  3197. int err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  3198. if (err)
  3199. return err;
  3200. ClearPagePrivate(page);
  3201. /*
  3202. * set page dirty so that it will not be removed from cache/file
  3203. * by non-hugetlbfs specific code paths.
  3204. */
  3205. set_page_dirty(page);
  3206. spin_lock(&inode->i_lock);
  3207. inode->i_blocks += blocks_per_huge_page(h);
  3208. spin_unlock(&inode->i_lock);
  3209. return 0;
  3210. }
  3211. static vm_fault_t hugetlb_no_page(struct mm_struct *mm,
  3212. struct vm_area_struct *vma,
  3213. struct address_space *mapping, pgoff_t idx,
  3214. unsigned long address, pte_t *ptep, unsigned int flags)
  3215. {
  3216. struct hstate *h = hstate_vma(vma);
  3217. vm_fault_t ret = VM_FAULT_SIGBUS;
  3218. int anon_rmap = 0;
  3219. unsigned long size;
  3220. struct page *page;
  3221. pte_t new_pte;
  3222. spinlock_t *ptl;
  3223. unsigned long haddr = address & huge_page_mask(h);
  3224. /*
  3225. * Currently, we are forced to kill the process in the event the
  3226. * original mapper has unmapped pages from the child due to a failed
  3227. * COW. Warn that such a situation has occurred as it may not be obvious
  3228. */
  3229. if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
  3230. pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n",
  3231. current->pid);
  3232. return ret;
  3233. }
  3234. /*
  3235. * Use page lock to guard against racing truncation
  3236. * before we get page_table_lock.
  3237. */
  3238. retry:
  3239. page = find_lock_page(mapping, idx);
  3240. if (!page) {
  3241. size = i_size_read(mapping->host) >> huge_page_shift(h);
  3242. if (idx >= size)
  3243. goto out;
  3244. /*
  3245. * Check for page in userfault range
  3246. */
  3247. if (userfaultfd_missing(vma)) {
  3248. u32 hash;
  3249. struct vm_fault vmf = {
  3250. .vma = vma,
  3251. .address = haddr,
  3252. .flags = flags,
  3253. /*
  3254. * Hard to debug if it ends up being
  3255. * used by a callee that assumes
  3256. * something about the other
  3257. * uninitialized fields... same as in
  3258. * memory.c
  3259. */
  3260. };
  3261. /*
  3262. * hugetlb_fault_mutex must be dropped before
  3263. * handling userfault. Reacquire after handling
  3264. * fault to make calling code simpler.
  3265. */
  3266. hash = hugetlb_fault_mutex_hash(h, mm, vma, mapping,
  3267. idx, haddr);
  3268. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  3269. ret = handle_userfault(&vmf, VM_UFFD_MISSING);
  3270. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  3271. goto out;
  3272. }
  3273. page = alloc_huge_page(vma, haddr, 0);
  3274. if (IS_ERR(page)) {
  3275. ret = vmf_error(PTR_ERR(page));
  3276. goto out;
  3277. }
  3278. clear_huge_page(page, address, pages_per_huge_page(h));
  3279. __SetPageUptodate(page);
  3280. set_page_huge_active(page);
  3281. if (vma->vm_flags & VM_MAYSHARE) {
  3282. int err = huge_add_to_page_cache(page, mapping, idx);
  3283. if (err) {
  3284. put_page(page);
  3285. if (err == -EEXIST)
  3286. goto retry;
  3287. goto out;
  3288. }
  3289. } else {
  3290. lock_page(page);
  3291. if (unlikely(anon_vma_prepare(vma))) {
  3292. ret = VM_FAULT_OOM;
  3293. goto backout_unlocked;
  3294. }
  3295. anon_rmap = 1;
  3296. }
  3297. } else {
  3298. /*
  3299. * If memory error occurs between mmap() and fault, some process
  3300. * don't have hwpoisoned swap entry for errored virtual address.
  3301. * So we need to block hugepage fault by PG_hwpoison bit check.
  3302. */
  3303. if (unlikely(PageHWPoison(page))) {
  3304. ret = VM_FAULT_HWPOISON |
  3305. VM_FAULT_SET_HINDEX(hstate_index(h));
  3306. goto backout_unlocked;
  3307. }
  3308. }
  3309. /*
  3310. * If we are going to COW a private mapping later, we examine the
  3311. * pending reservations for this page now. This will ensure that
  3312. * any allocations necessary to record that reservation occur outside
  3313. * the spinlock.
  3314. */
  3315. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
  3316. if (vma_needs_reservation(h, vma, haddr) < 0) {
  3317. ret = VM_FAULT_OOM;
  3318. goto backout_unlocked;
  3319. }
  3320. /* Just decrements count, does not deallocate */
  3321. vma_end_reservation(h, vma, haddr);
  3322. }
  3323. ptl = huge_pte_lock(h, mm, ptep);
  3324. size = i_size_read(mapping->host) >> huge_page_shift(h);
  3325. if (idx >= size)
  3326. goto backout;
  3327. ret = 0;
  3328. if (!huge_pte_none(huge_ptep_get(ptep)))
  3329. goto backout;
  3330. if (anon_rmap) {
  3331. ClearPagePrivate(page);
  3332. hugepage_add_new_anon_rmap(page, vma, haddr);
  3333. } else
  3334. page_dup_rmap(page, true);
  3335. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  3336. && (vma->vm_flags & VM_SHARED)));
  3337. set_huge_pte_at(mm, haddr, ptep, new_pte);
  3338. hugetlb_count_add(pages_per_huge_page(h), mm);
  3339. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
  3340. /* Optimization, do the COW without a second fault */
  3341. ret = hugetlb_cow(mm, vma, address, ptep, page, ptl);
  3342. }
  3343. spin_unlock(ptl);
  3344. unlock_page(page);
  3345. out:
  3346. return ret;
  3347. backout:
  3348. spin_unlock(ptl);
  3349. backout_unlocked:
  3350. unlock_page(page);
  3351. restore_reserve_on_error(h, vma, haddr, page);
  3352. put_page(page);
  3353. goto out;
  3354. }
  3355. #ifdef CONFIG_SMP
  3356. u32 hugetlb_fault_mutex_hash(struct hstate *h, struct mm_struct *mm,
  3357. struct vm_area_struct *vma,
  3358. struct address_space *mapping,
  3359. pgoff_t idx, unsigned long address)
  3360. {
  3361. unsigned long key[2];
  3362. u32 hash;
  3363. if (vma->vm_flags & VM_SHARED) {
  3364. key[0] = (unsigned long) mapping;
  3365. key[1] = idx;
  3366. } else {
  3367. key[0] = (unsigned long) mm;
  3368. key[1] = address >> huge_page_shift(h);
  3369. }
  3370. hash = jhash2((u32 *)&key, sizeof(key)/sizeof(u32), 0);
  3371. return hash & (num_fault_mutexes - 1);
  3372. }
  3373. #else
  3374. /*
  3375. * For uniprocesor systems we always use a single mutex, so just
  3376. * return 0 and avoid the hashing overhead.
  3377. */
  3378. u32 hugetlb_fault_mutex_hash(struct hstate *h, struct mm_struct *mm,
  3379. struct vm_area_struct *vma,
  3380. struct address_space *mapping,
  3381. pgoff_t idx, unsigned long address)
  3382. {
  3383. return 0;
  3384. }
  3385. #endif
  3386. vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  3387. unsigned long address, unsigned int flags)
  3388. {
  3389. pte_t *ptep, entry;
  3390. spinlock_t *ptl;
  3391. vm_fault_t ret;
  3392. u32 hash;
  3393. pgoff_t idx;
  3394. struct page *page = NULL;
  3395. struct page *pagecache_page = NULL;
  3396. struct hstate *h = hstate_vma(vma);
  3397. struct address_space *mapping;
  3398. int need_wait_lock = 0;
  3399. unsigned long haddr = address & huge_page_mask(h);
  3400. ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
  3401. if (ptep) {
  3402. entry = huge_ptep_get(ptep);
  3403. if (unlikely(is_hugetlb_entry_migration(entry))) {
  3404. migration_entry_wait_huge(vma, mm, ptep);
  3405. return 0;
  3406. } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
  3407. return VM_FAULT_HWPOISON_LARGE |
  3408. VM_FAULT_SET_HINDEX(hstate_index(h));
  3409. } else {
  3410. ptep = huge_pte_alloc(mm, haddr, huge_page_size(h));
  3411. if (!ptep)
  3412. return VM_FAULT_OOM;
  3413. }
  3414. mapping = vma->vm_file->f_mapping;
  3415. idx = vma_hugecache_offset(h, vma, haddr);
  3416. /*
  3417. * Serialize hugepage allocation and instantiation, so that we don't
  3418. * get spurious allocation failures if two CPUs race to instantiate
  3419. * the same page in the page cache.
  3420. */
  3421. hash = hugetlb_fault_mutex_hash(h, mm, vma, mapping, idx, haddr);
  3422. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  3423. entry = huge_ptep_get(ptep);
  3424. if (huge_pte_none(entry)) {
  3425. ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags);
  3426. goto out_mutex;
  3427. }
  3428. ret = 0;
  3429. /*
  3430. * entry could be a migration/hwpoison entry at this point, so this
  3431. * check prevents the kernel from going below assuming that we have
  3432. * a active hugepage in pagecache. This goto expects the 2nd page fault,
  3433. * and is_hugetlb_entry_(migration|hwpoisoned) check will properly
  3434. * handle it.
  3435. */
  3436. if (!pte_present(entry))
  3437. goto out_mutex;
  3438. /*
  3439. * If we are going to COW the mapping later, we examine the pending
  3440. * reservations for this page now. This will ensure that any
  3441. * allocations necessary to record that reservation occur outside the
  3442. * spinlock. For private mappings, we also lookup the pagecache
  3443. * page now as it is used to determine if a reservation has been
  3444. * consumed.
  3445. */
  3446. if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) {
  3447. if (vma_needs_reservation(h, vma, haddr) < 0) {
  3448. ret = VM_FAULT_OOM;
  3449. goto out_mutex;
  3450. }
  3451. /* Just decrements count, does not deallocate */
  3452. vma_end_reservation(h, vma, haddr);
  3453. if (!(vma->vm_flags & VM_MAYSHARE))
  3454. pagecache_page = hugetlbfs_pagecache_page(h,
  3455. vma, haddr);
  3456. }
  3457. ptl = huge_pte_lock(h, mm, ptep);
  3458. /* Check for a racing update before calling hugetlb_cow */
  3459. if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
  3460. goto out_ptl;
  3461. /*
  3462. * hugetlb_cow() requires page locks of pte_page(entry) and
  3463. * pagecache_page, so here we need take the former one
  3464. * when page != pagecache_page or !pagecache_page.
  3465. */
  3466. page = pte_page(entry);
  3467. if (page != pagecache_page)
  3468. if (!trylock_page(page)) {
  3469. need_wait_lock = 1;
  3470. goto out_ptl;
  3471. }
  3472. get_page(page);
  3473. if (flags & FAULT_FLAG_WRITE) {
  3474. if (!huge_pte_write(entry)) {
  3475. ret = hugetlb_cow(mm, vma, address, ptep,
  3476. pagecache_page, ptl);
  3477. goto out_put_page;
  3478. }
  3479. entry = huge_pte_mkdirty(entry);
  3480. }
  3481. entry = pte_mkyoung(entry);
  3482. if (huge_ptep_set_access_flags(vma, haddr, ptep, entry,
  3483. flags & FAULT_FLAG_WRITE))
  3484. update_mmu_cache(vma, haddr, ptep);
  3485. out_put_page:
  3486. if (page != pagecache_page)
  3487. unlock_page(page);
  3488. put_page(page);
  3489. out_ptl:
  3490. spin_unlock(ptl);
  3491. if (pagecache_page) {
  3492. unlock_page(pagecache_page);
  3493. put_page(pagecache_page);
  3494. }
  3495. out_mutex:
  3496. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  3497. /*
  3498. * Generally it's safe to hold refcount during waiting page lock. But
  3499. * here we just wait to defer the next page fault to avoid busy loop and
  3500. * the page is not used after unlocked before returning from the current
  3501. * page fault. So we are safe from accessing freed page, even if we wait
  3502. * here without taking refcount.
  3503. */
  3504. if (need_wait_lock)
  3505. wait_on_page_locked(page);
  3506. return ret;
  3507. }
  3508. /*
  3509. * Used by userfaultfd UFFDIO_COPY. Based on mcopy_atomic_pte with
  3510. * modifications for huge pages.
  3511. */
  3512. int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
  3513. pte_t *dst_pte,
  3514. struct vm_area_struct *dst_vma,
  3515. unsigned long dst_addr,
  3516. unsigned long src_addr,
  3517. struct page **pagep)
  3518. {
  3519. struct address_space *mapping;
  3520. pgoff_t idx;
  3521. unsigned long size;
  3522. int vm_shared = dst_vma->vm_flags & VM_SHARED;
  3523. struct hstate *h = hstate_vma(dst_vma);
  3524. pte_t _dst_pte;
  3525. spinlock_t *ptl;
  3526. int ret;
  3527. struct page *page;
  3528. if (!*pagep) {
  3529. ret = -ENOMEM;
  3530. page = alloc_huge_page(dst_vma, dst_addr, 0);
  3531. if (IS_ERR(page))
  3532. goto out;
  3533. ret = copy_huge_page_from_user(page,
  3534. (const void __user *) src_addr,
  3535. pages_per_huge_page(h), false);
  3536. /* fallback to copy_from_user outside mmap_sem */
  3537. if (unlikely(ret)) {
  3538. ret = -ENOENT;
  3539. *pagep = page;
  3540. /* don't free the page */
  3541. goto out;
  3542. }
  3543. } else {
  3544. page = *pagep;
  3545. *pagep = NULL;
  3546. }
  3547. /*
  3548. * The memory barrier inside __SetPageUptodate makes sure that
  3549. * preceding stores to the page contents become visible before
  3550. * the set_pte_at() write.
  3551. */
  3552. __SetPageUptodate(page);
  3553. set_page_huge_active(page);
  3554. mapping = dst_vma->vm_file->f_mapping;
  3555. idx = vma_hugecache_offset(h, dst_vma, dst_addr);
  3556. /*
  3557. * If shared, add to page cache
  3558. */
  3559. if (vm_shared) {
  3560. size = i_size_read(mapping->host) >> huge_page_shift(h);
  3561. ret = -EFAULT;
  3562. if (idx >= size)
  3563. goto out_release_nounlock;
  3564. /*
  3565. * Serialization between remove_inode_hugepages() and
  3566. * huge_add_to_page_cache() below happens through the
  3567. * hugetlb_fault_mutex_table that here must be hold by
  3568. * the caller.
  3569. */
  3570. ret = huge_add_to_page_cache(page, mapping, idx);
  3571. if (ret)
  3572. goto out_release_nounlock;
  3573. }
  3574. ptl = huge_pte_lockptr(h, dst_mm, dst_pte);
  3575. spin_lock(ptl);
  3576. /*
  3577. * Recheck the i_size after holding PT lock to make sure not
  3578. * to leave any page mapped (as page_mapped()) beyond the end
  3579. * of the i_size (remove_inode_hugepages() is strict about
  3580. * enforcing that). If we bail out here, we'll also leave a
  3581. * page in the radix tree in the vm_shared case beyond the end
  3582. * of the i_size, but remove_inode_hugepages() will take care
  3583. * of it as soon as we drop the hugetlb_fault_mutex_table.
  3584. */
  3585. size = i_size_read(mapping->host) >> huge_page_shift(h);
  3586. ret = -EFAULT;
  3587. if (idx >= size)
  3588. goto out_release_unlock;
  3589. ret = -EEXIST;
  3590. if (!huge_pte_none(huge_ptep_get(dst_pte)))
  3591. goto out_release_unlock;
  3592. if (vm_shared) {
  3593. page_dup_rmap(page, true);
  3594. } else {
  3595. ClearPagePrivate(page);
  3596. hugepage_add_new_anon_rmap(page, dst_vma, dst_addr);
  3597. }
  3598. _dst_pte = make_huge_pte(dst_vma, page, dst_vma->vm_flags & VM_WRITE);
  3599. if (dst_vma->vm_flags & VM_WRITE)
  3600. _dst_pte = huge_pte_mkdirty(_dst_pte);
  3601. _dst_pte = pte_mkyoung(_dst_pte);
  3602. set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
  3603. (void)huge_ptep_set_access_flags(dst_vma, dst_addr, dst_pte, _dst_pte,
  3604. dst_vma->vm_flags & VM_WRITE);
  3605. hugetlb_count_add(pages_per_huge_page(h), dst_mm);
  3606. /* No need to invalidate - it was non-present before */
  3607. update_mmu_cache(dst_vma, dst_addr, dst_pte);
  3608. spin_unlock(ptl);
  3609. if (vm_shared)
  3610. unlock_page(page);
  3611. ret = 0;
  3612. out:
  3613. return ret;
  3614. out_release_unlock:
  3615. spin_unlock(ptl);
  3616. if (vm_shared)
  3617. unlock_page(page);
  3618. out_release_nounlock:
  3619. put_page(page);
  3620. goto out;
  3621. }
  3622. long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  3623. struct page **pages, struct vm_area_struct **vmas,
  3624. unsigned long *position, unsigned long *nr_pages,
  3625. long i, unsigned int flags, int *nonblocking)
  3626. {
  3627. unsigned long pfn_offset;
  3628. unsigned long vaddr = *position;
  3629. unsigned long remainder = *nr_pages;
  3630. struct hstate *h = hstate_vma(vma);
  3631. int err = -EFAULT;
  3632. while (vaddr < vma->vm_end && remainder) {
  3633. pte_t *pte;
  3634. spinlock_t *ptl = NULL;
  3635. int absent;
  3636. struct page *page;
  3637. /*
  3638. * If we have a pending SIGKILL, don't keep faulting pages and
  3639. * potentially allocating memory.
  3640. */
  3641. if (unlikely(fatal_signal_pending(current))) {
  3642. remainder = 0;
  3643. break;
  3644. }
  3645. /*
  3646. * Some archs (sparc64, sh*) have multiple pte_ts to
  3647. * each hugepage. We have to make sure we get the
  3648. * first, for the page indexing below to work.
  3649. *
  3650. * Note that page table lock is not held when pte is null.
  3651. */
  3652. pte = huge_pte_offset(mm, vaddr & huge_page_mask(h),
  3653. huge_page_size(h));
  3654. if (pte)
  3655. ptl = huge_pte_lock(h, mm, pte);
  3656. absent = !pte || huge_pte_none(huge_ptep_get(pte));
  3657. /*
  3658. * When coredumping, it suits get_dump_page if we just return
  3659. * an error where there's an empty slot with no huge pagecache
  3660. * to back it. This way, we avoid allocating a hugepage, and
  3661. * the sparse dumpfile avoids allocating disk blocks, but its
  3662. * huge holes still show up with zeroes where they need to be.
  3663. */
  3664. if (absent && (flags & FOLL_DUMP) &&
  3665. !hugetlbfs_pagecache_present(h, vma, vaddr)) {
  3666. if (pte)
  3667. spin_unlock(ptl);
  3668. remainder = 0;
  3669. break;
  3670. }
  3671. /*
  3672. * We need call hugetlb_fault for both hugepages under migration
  3673. * (in which case hugetlb_fault waits for the migration,) and
  3674. * hwpoisoned hugepages (in which case we need to prevent the
  3675. * caller from accessing to them.) In order to do this, we use
  3676. * here is_swap_pte instead of is_hugetlb_entry_migration and
  3677. * is_hugetlb_entry_hwpoisoned. This is because it simply covers
  3678. * both cases, and because we can't follow correct pages
  3679. * directly from any kind of swap entries.
  3680. */
  3681. if (absent || is_swap_pte(huge_ptep_get(pte)) ||
  3682. ((flags & FOLL_WRITE) &&
  3683. !huge_pte_write(huge_ptep_get(pte)))) {
  3684. vm_fault_t ret;
  3685. unsigned int fault_flags = 0;
  3686. if (pte)
  3687. spin_unlock(ptl);
  3688. if (flags & FOLL_WRITE)
  3689. fault_flags |= FAULT_FLAG_WRITE;
  3690. if (nonblocking)
  3691. fault_flags |= FAULT_FLAG_ALLOW_RETRY;
  3692. if (flags & FOLL_NOWAIT)
  3693. fault_flags |= FAULT_FLAG_ALLOW_RETRY |
  3694. FAULT_FLAG_RETRY_NOWAIT;
  3695. if (flags & FOLL_TRIED) {
  3696. VM_WARN_ON_ONCE(fault_flags &
  3697. FAULT_FLAG_ALLOW_RETRY);
  3698. fault_flags |= FAULT_FLAG_TRIED;
  3699. }
  3700. ret = hugetlb_fault(mm, vma, vaddr, fault_flags);
  3701. if (ret & VM_FAULT_ERROR) {
  3702. err = vm_fault_to_errno(ret, flags);
  3703. remainder = 0;
  3704. break;
  3705. }
  3706. if (ret & VM_FAULT_RETRY) {
  3707. if (nonblocking)
  3708. *nonblocking = 0;
  3709. *nr_pages = 0;
  3710. /*
  3711. * VM_FAULT_RETRY must not return an
  3712. * error, it will return zero
  3713. * instead.
  3714. *
  3715. * No need to update "position" as the
  3716. * caller will not check it after
  3717. * *nr_pages is set to 0.
  3718. */
  3719. return i;
  3720. }
  3721. continue;
  3722. }
  3723. pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
  3724. page = pte_page(huge_ptep_get(pte));
  3725. same_page:
  3726. if (pages) {
  3727. pages[i] = mem_map_offset(page, pfn_offset);
  3728. get_page(pages[i]);
  3729. }
  3730. if (vmas)
  3731. vmas[i] = vma;
  3732. vaddr += PAGE_SIZE;
  3733. ++pfn_offset;
  3734. --remainder;
  3735. ++i;
  3736. if (vaddr < vma->vm_end && remainder &&
  3737. pfn_offset < pages_per_huge_page(h)) {
  3738. /*
  3739. * We use pfn_offset to avoid touching the pageframes
  3740. * of this compound page.
  3741. */
  3742. goto same_page;
  3743. }
  3744. spin_unlock(ptl);
  3745. }
  3746. *nr_pages = remainder;
  3747. /*
  3748. * setting position is actually required only if remainder is
  3749. * not zero but it's faster not to add a "if (remainder)"
  3750. * branch.
  3751. */
  3752. *position = vaddr;
  3753. return i ? i : err;
  3754. }
  3755. #ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
  3756. /*
  3757. * ARCHes with special requirements for evicting HUGETLB backing TLB entries can
  3758. * implement this.
  3759. */
  3760. #define flush_hugetlb_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
  3761. #endif
  3762. unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
  3763. unsigned long address, unsigned long end, pgprot_t newprot)
  3764. {
  3765. struct mm_struct *mm = vma->vm_mm;
  3766. unsigned long start = address;
  3767. pte_t *ptep;
  3768. pte_t pte;
  3769. struct hstate *h = hstate_vma(vma);
  3770. unsigned long pages = 0;
  3771. unsigned long f_start = start;
  3772. unsigned long f_end = end;
  3773. bool shared_pmd = false;
  3774. /*
  3775. * In the case of shared PMDs, the area to flush could be beyond
  3776. * start/end. Set f_start/f_end to cover the maximum possible
  3777. * range if PMD sharing is possible.
  3778. */
  3779. adjust_range_if_pmd_sharing_possible(vma, &f_start, &f_end);
  3780. BUG_ON(address >= end);
  3781. flush_cache_range(vma, f_start, f_end);
  3782. mmu_notifier_invalidate_range_start(mm, f_start, f_end);
  3783. i_mmap_lock_write(vma->vm_file->f_mapping);
  3784. for (; address < end; address += huge_page_size(h)) {
  3785. spinlock_t *ptl;
  3786. ptep = huge_pte_offset(mm, address, huge_page_size(h));
  3787. if (!ptep)
  3788. continue;
  3789. ptl = huge_pte_lock(h, mm, ptep);
  3790. if (huge_pmd_unshare(mm, &address, ptep)) {
  3791. pages++;
  3792. spin_unlock(ptl);
  3793. shared_pmd = true;
  3794. continue;
  3795. }
  3796. pte = huge_ptep_get(ptep);
  3797. if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
  3798. spin_unlock(ptl);
  3799. continue;
  3800. }
  3801. if (unlikely(is_hugetlb_entry_migration(pte))) {
  3802. swp_entry_t entry = pte_to_swp_entry(pte);
  3803. if (is_write_migration_entry(entry)) {
  3804. pte_t newpte;
  3805. make_migration_entry_read(&entry);
  3806. newpte = swp_entry_to_pte(entry);
  3807. set_huge_swap_pte_at(mm, address, ptep,
  3808. newpte, huge_page_size(h));
  3809. pages++;
  3810. }
  3811. spin_unlock(ptl);
  3812. continue;
  3813. }
  3814. if (!huge_pte_none(pte)) {
  3815. pte = huge_ptep_get_and_clear(mm, address, ptep);
  3816. pte = pte_mkhuge(huge_pte_modify(pte, newprot));
  3817. pte = arch_make_huge_pte(pte, vma, NULL, 0);
  3818. set_huge_pte_at(mm, address, ptep, pte);
  3819. pages++;
  3820. }
  3821. spin_unlock(ptl);
  3822. }
  3823. /*
  3824. * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare
  3825. * may have cleared our pud entry and done put_page on the page table:
  3826. * once we release i_mmap_rwsem, another task can do the final put_page
  3827. * and that page table be reused and filled with junk. If we actually
  3828. * did unshare a page of pmds, flush the range corresponding to the pud.
  3829. */
  3830. if (shared_pmd)
  3831. flush_hugetlb_tlb_range(vma, f_start, f_end);
  3832. else
  3833. flush_hugetlb_tlb_range(vma, start, end);
  3834. /*
  3835. * No need to call mmu_notifier_invalidate_range() we are downgrading
  3836. * page table protection not changing it to point to a new page.
  3837. *
  3838. * See Documentation/vm/mmu_notifier.rst
  3839. */
  3840. i_mmap_unlock_write(vma->vm_file->f_mapping);
  3841. mmu_notifier_invalidate_range_end(mm, f_start, f_end);
  3842. return pages << h->order;
  3843. }
  3844. int hugetlb_reserve_pages(struct inode *inode,
  3845. long from, long to,
  3846. struct vm_area_struct *vma,
  3847. vm_flags_t vm_flags)
  3848. {
  3849. long ret, chg;
  3850. struct hstate *h = hstate_inode(inode);
  3851. struct hugepage_subpool *spool = subpool_inode(inode);
  3852. struct resv_map *resv_map;
  3853. long gbl_reserve;
  3854. /* This should never happen */
  3855. if (from > to) {
  3856. VM_WARN(1, "%s called with a negative range\n", __func__);
  3857. return -EINVAL;
  3858. }
  3859. /*
  3860. * Only apply hugepage reservation if asked. At fault time, an
  3861. * attempt will be made for VM_NORESERVE to allocate a page
  3862. * without using reserves
  3863. */
  3864. if (vm_flags & VM_NORESERVE)
  3865. return 0;
  3866. /*
  3867. * Shared mappings base their reservation on the number of pages that
  3868. * are already allocated on behalf of the file. Private mappings need
  3869. * to reserve the full area even if read-only as mprotect() may be
  3870. * called to make the mapping read-write. Assume !vma is a shm mapping
  3871. */
  3872. if (!vma || vma->vm_flags & VM_MAYSHARE) {
  3873. resv_map = inode_resv_map(inode);
  3874. chg = region_chg(resv_map, from, to);
  3875. } else {
  3876. resv_map = resv_map_alloc();
  3877. if (!resv_map)
  3878. return -ENOMEM;
  3879. chg = to - from;
  3880. set_vma_resv_map(vma, resv_map);
  3881. set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
  3882. }
  3883. if (chg < 0) {
  3884. ret = chg;
  3885. goto out_err;
  3886. }
  3887. /*
  3888. * There must be enough pages in the subpool for the mapping. If
  3889. * the subpool has a minimum size, there may be some global
  3890. * reservations already in place (gbl_reserve).
  3891. */
  3892. gbl_reserve = hugepage_subpool_get_pages(spool, chg);
  3893. if (gbl_reserve < 0) {
  3894. ret = -ENOSPC;
  3895. goto out_err;
  3896. }
  3897. /*
  3898. * Check enough hugepages are available for the reservation.
  3899. * Hand the pages back to the subpool if there are not
  3900. */
  3901. ret = hugetlb_acct_memory(h, gbl_reserve);
  3902. if (ret < 0) {
  3903. /* put back original number of pages, chg */
  3904. (void)hugepage_subpool_put_pages(spool, chg);
  3905. goto out_err;
  3906. }
  3907. /*
  3908. * Account for the reservations made. Shared mappings record regions
  3909. * that have reservations as they are shared by multiple VMAs.
  3910. * When the last VMA disappears, the region map says how much
  3911. * the reservation was and the page cache tells how much of
  3912. * the reservation was consumed. Private mappings are per-VMA and
  3913. * only the consumed reservations are tracked. When the VMA
  3914. * disappears, the original reservation is the VMA size and the
  3915. * consumed reservations are stored in the map. Hence, nothing
  3916. * else has to be done for private mappings here
  3917. */
  3918. if (!vma || vma->vm_flags & VM_MAYSHARE) {
  3919. long add = region_add(resv_map, from, to);
  3920. if (unlikely(chg > add)) {
  3921. /*
  3922. * pages in this range were added to the reserve
  3923. * map between region_chg and region_add. This
  3924. * indicates a race with alloc_huge_page. Adjust
  3925. * the subpool and reserve counts modified above
  3926. * based on the difference.
  3927. */
  3928. long rsv_adjust;
  3929. rsv_adjust = hugepage_subpool_put_pages(spool,
  3930. chg - add);
  3931. hugetlb_acct_memory(h, -rsv_adjust);
  3932. }
  3933. }
  3934. return 0;
  3935. out_err:
  3936. if (!vma || vma->vm_flags & VM_MAYSHARE)
  3937. /* Don't call region_abort if region_chg failed */
  3938. if (chg >= 0)
  3939. region_abort(resv_map, from, to);
  3940. if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
  3941. kref_put(&resv_map->refs, resv_map_release);
  3942. return ret;
  3943. }
  3944. long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
  3945. long freed)
  3946. {
  3947. struct hstate *h = hstate_inode(inode);
  3948. struct resv_map *resv_map = inode_resv_map(inode);
  3949. long chg = 0;
  3950. struct hugepage_subpool *spool = subpool_inode(inode);
  3951. long gbl_reserve;
  3952. if (resv_map) {
  3953. chg = region_del(resv_map, start, end);
  3954. /*
  3955. * region_del() can fail in the rare case where a region
  3956. * must be split and another region descriptor can not be
  3957. * allocated. If end == LONG_MAX, it will not fail.
  3958. */
  3959. if (chg < 0)
  3960. return chg;
  3961. }
  3962. spin_lock(&inode->i_lock);
  3963. inode->i_blocks -= (blocks_per_huge_page(h) * freed);
  3964. spin_unlock(&inode->i_lock);
  3965. /*
  3966. * If the subpool has a minimum size, the number of global
  3967. * reservations to be released may be adjusted.
  3968. */
  3969. gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed));
  3970. hugetlb_acct_memory(h, -gbl_reserve);
  3971. return 0;
  3972. }
  3973. #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
  3974. static unsigned long page_table_shareable(struct vm_area_struct *svma,
  3975. struct vm_area_struct *vma,
  3976. unsigned long addr, pgoff_t idx)
  3977. {
  3978. unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
  3979. svma->vm_start;
  3980. unsigned long sbase = saddr & PUD_MASK;
  3981. unsigned long s_end = sbase + PUD_SIZE;
  3982. /* Allow segments to share if only one is marked locked */
  3983. unsigned long vm_flags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
  3984. unsigned long svm_flags = svma->vm_flags & VM_LOCKED_CLEAR_MASK;
  3985. /*
  3986. * match the virtual addresses, permission and the alignment of the
  3987. * page table page.
  3988. */
  3989. if (pmd_index(addr) != pmd_index(saddr) ||
  3990. vm_flags != svm_flags ||
  3991. sbase < svma->vm_start || svma->vm_end < s_end)
  3992. return 0;
  3993. return saddr;
  3994. }
  3995. static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr)
  3996. {
  3997. unsigned long base = addr & PUD_MASK;
  3998. unsigned long end = base + PUD_SIZE;
  3999. /*
  4000. * check on proper vm_flags and page table alignment
  4001. */
  4002. if (vma->vm_flags & VM_MAYSHARE && range_in_vma(vma, base, end))
  4003. return true;
  4004. return false;
  4005. }
  4006. /*
  4007. * Determine if start,end range within vma could be mapped by shared pmd.
  4008. * If yes, adjust start and end to cover range associated with possible
  4009. * shared pmd mappings.
  4010. */
  4011. void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
  4012. unsigned long *start, unsigned long *end)
  4013. {
  4014. unsigned long check_addr = *start;
  4015. if (!(vma->vm_flags & VM_MAYSHARE))
  4016. return;
  4017. for (check_addr = *start; check_addr < *end; check_addr += PUD_SIZE) {
  4018. unsigned long a_start = check_addr & PUD_MASK;
  4019. unsigned long a_end = a_start + PUD_SIZE;
  4020. /*
  4021. * If sharing is possible, adjust start/end if necessary.
  4022. */
  4023. if (range_in_vma(vma, a_start, a_end)) {
  4024. if (a_start < *start)
  4025. *start = a_start;
  4026. if (a_end > *end)
  4027. *end = a_end;
  4028. }
  4029. }
  4030. }
  4031. /*
  4032. * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
  4033. * and returns the corresponding pte. While this is not necessary for the
  4034. * !shared pmd case because we can allocate the pmd later as well, it makes the
  4035. * code much cleaner. pmd allocation is essential for the shared case because
  4036. * pud has to be populated inside the same i_mmap_rwsem section - otherwise
  4037. * racing tasks could either miss the sharing (see huge_pte_offset) or select a
  4038. * bad pmd for sharing.
  4039. */
  4040. pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
  4041. {
  4042. struct vm_area_struct *vma = find_vma(mm, addr);
  4043. struct address_space *mapping = vma->vm_file->f_mapping;
  4044. pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
  4045. vma->vm_pgoff;
  4046. struct vm_area_struct *svma;
  4047. unsigned long saddr;
  4048. pte_t *spte = NULL;
  4049. pte_t *pte;
  4050. spinlock_t *ptl;
  4051. if (!vma_shareable(vma, addr))
  4052. return (pte_t *)pmd_alloc(mm, pud, addr);
  4053. i_mmap_lock_write(mapping);
  4054. vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
  4055. if (svma == vma)
  4056. continue;
  4057. saddr = page_table_shareable(svma, vma, addr, idx);
  4058. if (saddr) {
  4059. spte = huge_pte_offset(svma->vm_mm, saddr,
  4060. vma_mmu_pagesize(svma));
  4061. if (spte) {
  4062. get_page(virt_to_page(spte));
  4063. break;
  4064. }
  4065. }
  4066. }
  4067. if (!spte)
  4068. goto out;
  4069. ptl = huge_pte_lock(hstate_vma(vma), mm, spte);
  4070. if (pud_none(*pud)) {
  4071. pud_populate(mm, pud,
  4072. (pmd_t *)((unsigned long)spte & PAGE_MASK));
  4073. mm_inc_nr_pmds(mm);
  4074. } else {
  4075. put_page(virt_to_page(spte));
  4076. }
  4077. spin_unlock(ptl);
  4078. out:
  4079. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  4080. i_mmap_unlock_write(mapping);
  4081. return pte;
  4082. }
  4083. /*
  4084. * unmap huge page backed by shared pte.
  4085. *
  4086. * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
  4087. * indicated by page_count > 1, unmap is achieved by clearing pud and
  4088. * decrementing the ref count. If count == 1, the pte page is not shared.
  4089. *
  4090. * called with page table lock held.
  4091. *
  4092. * returns: 1 successfully unmapped a shared pte page
  4093. * 0 the underlying pte page is not shared, or it is the last user
  4094. */
  4095. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  4096. {
  4097. pgd_t *pgd = pgd_offset(mm, *addr);
  4098. p4d_t *p4d = p4d_offset(pgd, *addr);
  4099. pud_t *pud = pud_offset(p4d, *addr);
  4100. BUG_ON(page_count(virt_to_page(ptep)) == 0);
  4101. if (page_count(virt_to_page(ptep)) == 1)
  4102. return 0;
  4103. pud_clear(pud);
  4104. put_page(virt_to_page(ptep));
  4105. mm_dec_nr_pmds(mm);
  4106. *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
  4107. return 1;
  4108. }
  4109. #define want_pmd_share() (1)
  4110. #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
  4111. pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
  4112. {
  4113. return NULL;
  4114. }
  4115. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  4116. {
  4117. return 0;
  4118. }
  4119. void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
  4120. unsigned long *start, unsigned long *end)
  4121. {
  4122. }
  4123. #define want_pmd_share() (0)
  4124. #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
  4125. #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
  4126. pte_t *huge_pte_alloc(struct mm_struct *mm,
  4127. unsigned long addr, unsigned long sz)
  4128. {
  4129. pgd_t *pgd;
  4130. p4d_t *p4d;
  4131. pud_t *pud;
  4132. pte_t *pte = NULL;
  4133. pgd = pgd_offset(mm, addr);
  4134. p4d = p4d_alloc(mm, pgd, addr);
  4135. if (!p4d)
  4136. return NULL;
  4137. pud = pud_alloc(mm, p4d, addr);
  4138. if (pud) {
  4139. if (sz == PUD_SIZE) {
  4140. pte = (pte_t *)pud;
  4141. } else {
  4142. BUG_ON(sz != PMD_SIZE);
  4143. if (want_pmd_share() && pud_none(*pud))
  4144. pte = huge_pmd_share(mm, addr, pud);
  4145. else
  4146. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  4147. }
  4148. }
  4149. BUG_ON(pte && pte_present(*pte) && !pte_huge(*pte));
  4150. return pte;
  4151. }
  4152. /*
  4153. * huge_pte_offset() - Walk the page table to resolve the hugepage
  4154. * entry at address @addr
  4155. *
  4156. * Return: Pointer to page table or swap entry (PUD or PMD) for
  4157. * address @addr, or NULL if a p*d_none() entry is encountered and the
  4158. * size @sz doesn't match the hugepage size at this level of the page
  4159. * table.
  4160. */
  4161. pte_t *huge_pte_offset(struct mm_struct *mm,
  4162. unsigned long addr, unsigned long sz)
  4163. {
  4164. pgd_t *pgd;
  4165. p4d_t *p4d;
  4166. pud_t *pud;
  4167. pmd_t *pmd;
  4168. pgd = pgd_offset(mm, addr);
  4169. if (!pgd_present(*pgd))
  4170. return NULL;
  4171. p4d = p4d_offset(pgd, addr);
  4172. if (!p4d_present(*p4d))
  4173. return NULL;
  4174. pud = pud_offset(p4d, addr);
  4175. if (sz != PUD_SIZE && pud_none(*pud))
  4176. return NULL;
  4177. /* hugepage or swap? */
  4178. if (pud_huge(*pud) || !pud_present(*pud))
  4179. return (pte_t *)pud;
  4180. pmd = pmd_offset(pud, addr);
  4181. if (sz != PMD_SIZE && pmd_none(*pmd))
  4182. return NULL;
  4183. /* hugepage or swap? */
  4184. if (pmd_huge(*pmd) || !pmd_present(*pmd))
  4185. return (pte_t *)pmd;
  4186. return NULL;
  4187. }
  4188. #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
  4189. /*
  4190. * These functions are overwritable if your architecture needs its own
  4191. * behavior.
  4192. */
  4193. struct page * __weak
  4194. follow_huge_addr(struct mm_struct *mm, unsigned long address,
  4195. int write)
  4196. {
  4197. return ERR_PTR(-EINVAL);
  4198. }
  4199. struct page * __weak
  4200. follow_huge_pd(struct vm_area_struct *vma,
  4201. unsigned long address, hugepd_t hpd, int flags, int pdshift)
  4202. {
  4203. WARN(1, "hugepd follow called with no support for hugepage directory format\n");
  4204. return NULL;
  4205. }
  4206. struct page * __weak
  4207. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  4208. pmd_t *pmd, int flags)
  4209. {
  4210. struct page *page = NULL;
  4211. spinlock_t *ptl;
  4212. pte_t pte;
  4213. retry:
  4214. ptl = pmd_lockptr(mm, pmd);
  4215. spin_lock(ptl);
  4216. /*
  4217. * make sure that the address range covered by this pmd is not
  4218. * unmapped from other threads.
  4219. */
  4220. if (!pmd_huge(*pmd))
  4221. goto out;
  4222. pte = huge_ptep_get((pte_t *)pmd);
  4223. if (pte_present(pte)) {
  4224. page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT);
  4225. if (flags & FOLL_GET)
  4226. get_page(page);
  4227. } else {
  4228. if (is_hugetlb_entry_migration(pte)) {
  4229. spin_unlock(ptl);
  4230. __migration_entry_wait(mm, (pte_t *)pmd, ptl);
  4231. goto retry;
  4232. }
  4233. /*
  4234. * hwpoisoned entry is treated as no_page_table in
  4235. * follow_page_mask().
  4236. */
  4237. }
  4238. out:
  4239. spin_unlock(ptl);
  4240. return page;
  4241. }
  4242. struct page * __weak
  4243. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  4244. pud_t *pud, int flags)
  4245. {
  4246. if (flags & FOLL_GET)
  4247. return NULL;
  4248. return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT);
  4249. }
  4250. struct page * __weak
  4251. follow_huge_pgd(struct mm_struct *mm, unsigned long address, pgd_t *pgd, int flags)
  4252. {
  4253. if (flags & FOLL_GET)
  4254. return NULL;
  4255. return pte_page(*(pte_t *)pgd) + ((address & ~PGDIR_MASK) >> PAGE_SHIFT);
  4256. }
  4257. bool isolate_huge_page(struct page *page, struct list_head *list)
  4258. {
  4259. bool ret = true;
  4260. VM_BUG_ON_PAGE(!PageHead(page), page);
  4261. spin_lock(&hugetlb_lock);
  4262. if (!page_huge_active(page) || !get_page_unless_zero(page)) {
  4263. ret = false;
  4264. goto unlock;
  4265. }
  4266. clear_page_huge_active(page);
  4267. list_move_tail(&page->lru, list);
  4268. unlock:
  4269. spin_unlock(&hugetlb_lock);
  4270. return ret;
  4271. }
  4272. void putback_active_hugepage(struct page *page)
  4273. {
  4274. VM_BUG_ON_PAGE(!PageHead(page), page);
  4275. spin_lock(&hugetlb_lock);
  4276. set_page_huge_active(page);
  4277. list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
  4278. spin_unlock(&hugetlb_lock);
  4279. put_page(page);
  4280. }
  4281. void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason)
  4282. {
  4283. struct hstate *h = page_hstate(oldpage);
  4284. hugetlb_cgroup_migrate(oldpage, newpage);
  4285. set_page_owner_migrate_reason(newpage, reason);
  4286. /*
  4287. * transfer temporary state of the new huge page. This is
  4288. * reverse to other transitions because the newpage is going to
  4289. * be final while the old one will be freed so it takes over
  4290. * the temporary status.
  4291. *
  4292. * Also note that we have to transfer the per-node surplus state
  4293. * here as well otherwise the global surplus count will not match
  4294. * the per-node's.
  4295. */
  4296. if (PageHugeTemporary(newpage)) {
  4297. int old_nid = page_to_nid(oldpage);
  4298. int new_nid = page_to_nid(newpage);
  4299. SetPageHugeTemporary(oldpage);
  4300. ClearPageHugeTemporary(newpage);
  4301. spin_lock(&hugetlb_lock);
  4302. if (h->surplus_huge_pages_node[old_nid]) {
  4303. h->surplus_huge_pages_node[old_nid]--;
  4304. h->surplus_huge_pages_node[new_nid]++;
  4305. }
  4306. spin_unlock(&hugetlb_lock);
  4307. }
  4308. }