hugetlb.c 59 KB

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