hugetlb.c 118 KB

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