hugetlb.c 135 KB

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