hugetlb.c 62 KB

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