hugetlb.c 72 KB

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