hugetlb.c 88 KB

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