hugetlb.c 57 KB

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