hugetlb.c 118 KB

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