hugetlb.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232
  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_mutex;
  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_mutex;
  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 (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
  1750. goto out_page_table_lock;
  1751. if (write_access) {
  1752. if (!pte_write(entry)) {
  1753. ret = hugetlb_cow(mm, vma, address, ptep, entry,
  1754. pagecache_page);
  1755. goto out_page_table_lock;
  1756. }
  1757. entry = pte_mkdirty(entry);
  1758. }
  1759. entry = pte_mkyoung(entry);
  1760. if (huge_ptep_set_access_flags(vma, address, ptep, entry, write_access))
  1761. update_mmu_cache(vma, address, entry);
  1762. out_page_table_lock:
  1763. spin_unlock(&mm->page_table_lock);
  1764. if (pagecache_page) {
  1765. unlock_page(pagecache_page);
  1766. put_page(pagecache_page);
  1767. }
  1768. out_mutex:
  1769. mutex_unlock(&hugetlb_instantiation_mutex);
  1770. return ret;
  1771. }
  1772. /* Can be overriden by architectures */
  1773. __attribute__((weak)) struct page *
  1774. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  1775. pud_t *pud, int write)
  1776. {
  1777. BUG();
  1778. return NULL;
  1779. }
  1780. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1781. struct page **pages, struct vm_area_struct **vmas,
  1782. unsigned long *position, int *length, int i,
  1783. int write)
  1784. {
  1785. unsigned long pfn_offset;
  1786. unsigned long vaddr = *position;
  1787. int remainder = *length;
  1788. struct hstate *h = hstate_vma(vma);
  1789. spin_lock(&mm->page_table_lock);
  1790. while (vaddr < vma->vm_end && remainder) {
  1791. pte_t *pte;
  1792. struct page *page;
  1793. /*
  1794. * Some archs (sparc64, sh*) have multiple pte_ts to
  1795. * each hugepage. We have to make * sure we get the
  1796. * first, for the page indexing below to work.
  1797. */
  1798. pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
  1799. if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
  1800. (write && !pte_write(huge_ptep_get(pte)))) {
  1801. int ret;
  1802. spin_unlock(&mm->page_table_lock);
  1803. ret = hugetlb_fault(mm, vma, vaddr, write);
  1804. spin_lock(&mm->page_table_lock);
  1805. if (!(ret & VM_FAULT_ERROR))
  1806. continue;
  1807. remainder = 0;
  1808. if (!i)
  1809. i = -EFAULT;
  1810. break;
  1811. }
  1812. pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
  1813. page = pte_page(huge_ptep_get(pte));
  1814. same_page:
  1815. if (pages) {
  1816. get_page(page);
  1817. pages[i] = page + pfn_offset;
  1818. }
  1819. if (vmas)
  1820. vmas[i] = vma;
  1821. vaddr += PAGE_SIZE;
  1822. ++pfn_offset;
  1823. --remainder;
  1824. ++i;
  1825. if (vaddr < vma->vm_end && remainder &&
  1826. pfn_offset < pages_per_huge_page(h)) {
  1827. /*
  1828. * We use pfn_offset to avoid touching the pageframes
  1829. * of this compound page.
  1830. */
  1831. goto same_page;
  1832. }
  1833. }
  1834. spin_unlock(&mm->page_table_lock);
  1835. *length = remainder;
  1836. *position = vaddr;
  1837. return i;
  1838. }
  1839. void hugetlb_change_protection(struct vm_area_struct *vma,
  1840. unsigned long address, unsigned long end, pgprot_t newprot)
  1841. {
  1842. struct mm_struct *mm = vma->vm_mm;
  1843. unsigned long start = address;
  1844. pte_t *ptep;
  1845. pte_t pte;
  1846. struct hstate *h = hstate_vma(vma);
  1847. BUG_ON(address >= end);
  1848. flush_cache_range(vma, address, end);
  1849. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  1850. spin_lock(&mm->page_table_lock);
  1851. for (; address < end; address += huge_page_size(h)) {
  1852. ptep = huge_pte_offset(mm, address);
  1853. if (!ptep)
  1854. continue;
  1855. if (huge_pmd_unshare(mm, &address, ptep))
  1856. continue;
  1857. if (!huge_pte_none(huge_ptep_get(ptep))) {
  1858. pte = huge_ptep_get_and_clear(mm, address, ptep);
  1859. pte = pte_mkhuge(pte_modify(pte, newprot));
  1860. set_huge_pte_at(mm, address, ptep, pte);
  1861. }
  1862. }
  1863. spin_unlock(&mm->page_table_lock);
  1864. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  1865. flush_tlb_range(vma, start, end);
  1866. }
  1867. int hugetlb_reserve_pages(struct inode *inode,
  1868. long from, long to,
  1869. struct vm_area_struct *vma)
  1870. {
  1871. long ret, chg;
  1872. struct hstate *h = hstate_inode(inode);
  1873. if (vma && vma->vm_flags & VM_NORESERVE)
  1874. return 0;
  1875. /*
  1876. * Shared mappings base their reservation on the number of pages that
  1877. * are already allocated on behalf of the file. Private mappings need
  1878. * to reserve the full area even if read-only as mprotect() may be
  1879. * called to make the mapping read-write. Assume !vma is a shm mapping
  1880. */
  1881. if (!vma || vma->vm_flags & VM_SHARED)
  1882. chg = region_chg(&inode->i_mapping->private_list, from, to);
  1883. else {
  1884. struct resv_map *resv_map = resv_map_alloc();
  1885. if (!resv_map)
  1886. return -ENOMEM;
  1887. chg = to - from;
  1888. set_vma_resv_map(vma, resv_map);
  1889. set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
  1890. }
  1891. if (chg < 0)
  1892. return chg;
  1893. if (hugetlb_get_quota(inode->i_mapping, chg))
  1894. return -ENOSPC;
  1895. ret = hugetlb_acct_memory(h, chg);
  1896. if (ret < 0) {
  1897. hugetlb_put_quota(inode->i_mapping, chg);
  1898. return ret;
  1899. }
  1900. if (!vma || vma->vm_flags & VM_SHARED)
  1901. region_add(&inode->i_mapping->private_list, from, to);
  1902. return 0;
  1903. }
  1904. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  1905. {
  1906. struct hstate *h = hstate_inode(inode);
  1907. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  1908. spin_lock(&inode->i_lock);
  1909. inode->i_blocks -= blocks_per_huge_page(h);
  1910. spin_unlock(&inode->i_lock);
  1911. hugetlb_put_quota(inode->i_mapping, (chg - freed));
  1912. hugetlb_acct_memory(h, -(chg - freed));
  1913. }