hugetlb.c 105 KB

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