hugetlb.c 80 KB

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