hugetlb.c 121 KB

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