hugetlb.c 76 KB

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