hugetlb.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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/nodemask.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/mempolicy.h>
  15. #include <linux/cpuset.h>
  16. #include <linux/mutex.h>
  17. #include <asm/page.h>
  18. #include <asm/pgtable.h>
  19. #include <linux/hugetlb.h>
  20. #include "internal.h"
  21. const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
  22. static unsigned long nr_huge_pages, free_huge_pages, resv_huge_pages;
  23. static unsigned long surplus_huge_pages;
  24. static unsigned long nr_overcommit_huge_pages;
  25. unsigned long max_huge_pages;
  26. unsigned long sysctl_overcommit_huge_pages;
  27. static struct list_head hugepage_freelists[MAX_NUMNODES];
  28. static unsigned int nr_huge_pages_node[MAX_NUMNODES];
  29. static unsigned int free_huge_pages_node[MAX_NUMNODES];
  30. static unsigned int surplus_huge_pages_node[MAX_NUMNODES];
  31. static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
  32. unsigned long hugepages_treat_as_movable;
  33. static int hugetlb_next_nid;
  34. /*
  35. * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
  36. */
  37. static DEFINE_SPINLOCK(hugetlb_lock);
  38. /*
  39. * Region tracking -- allows tracking of reservations and instantiated pages
  40. * across the pages in a mapping.
  41. */
  42. struct file_region {
  43. struct list_head link;
  44. long from;
  45. long to;
  46. };
  47. static long region_add(struct list_head *head, long f, long t)
  48. {
  49. struct file_region *rg, *nrg, *trg;
  50. /* Locate the region we are either in or before. */
  51. list_for_each_entry(rg, head, link)
  52. if (f <= rg->to)
  53. break;
  54. /* Round our left edge to the current segment if it encloses us. */
  55. if (f > rg->from)
  56. f = rg->from;
  57. /* Check for and consume any regions we now overlap with. */
  58. nrg = rg;
  59. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  60. if (&rg->link == head)
  61. break;
  62. if (rg->from > t)
  63. break;
  64. /* If this area reaches higher then extend our area to
  65. * include it completely. If this is not the first area
  66. * which we intend to reuse, free it. */
  67. if (rg->to > t)
  68. t = rg->to;
  69. if (rg != nrg) {
  70. list_del(&rg->link);
  71. kfree(rg);
  72. }
  73. }
  74. nrg->from = f;
  75. nrg->to = t;
  76. return 0;
  77. }
  78. static long region_chg(struct list_head *head, long f, long t)
  79. {
  80. struct file_region *rg, *nrg;
  81. long chg = 0;
  82. /* Locate the region we are before or in. */
  83. list_for_each_entry(rg, head, link)
  84. if (f <= rg->to)
  85. break;
  86. /* If we are below the current region then a new region is required.
  87. * Subtle, allocate a new region at the position but make it zero
  88. * size such that we can guarantee to record the reservation. */
  89. if (&rg->link == head || t < rg->from) {
  90. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  91. if (!nrg)
  92. return -ENOMEM;
  93. nrg->from = f;
  94. nrg->to = f;
  95. INIT_LIST_HEAD(&nrg->link);
  96. list_add(&nrg->link, rg->link.prev);
  97. return t - f;
  98. }
  99. /* Round our left edge to the current segment if it encloses us. */
  100. if (f > rg->from)
  101. f = rg->from;
  102. chg = t - f;
  103. /* Check for and consume any regions we now overlap with. */
  104. list_for_each_entry(rg, rg->link.prev, link) {
  105. if (&rg->link == head)
  106. break;
  107. if (rg->from > t)
  108. return chg;
  109. /* We overlap with this area, if it extends futher than
  110. * us then we must extend ourselves. Account for its
  111. * existing reservation. */
  112. if (rg->to > t) {
  113. chg += rg->to - t;
  114. t = rg->to;
  115. }
  116. chg -= rg->to - rg->from;
  117. }
  118. return chg;
  119. }
  120. static long region_truncate(struct list_head *head, long end)
  121. {
  122. struct file_region *rg, *trg;
  123. long chg = 0;
  124. /* Locate the region we are either in or before. */
  125. list_for_each_entry(rg, head, link)
  126. if (end <= rg->to)
  127. break;
  128. if (&rg->link == head)
  129. return 0;
  130. /* If we are in the middle of a region then adjust it. */
  131. if (end > rg->from) {
  132. chg = rg->to - end;
  133. rg->to = end;
  134. rg = list_entry(rg->link.next, typeof(*rg), link);
  135. }
  136. /* Drop any remaining regions. */
  137. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  138. if (&rg->link == head)
  139. break;
  140. chg += rg->to - rg->from;
  141. list_del(&rg->link);
  142. kfree(rg);
  143. }
  144. return chg;
  145. }
  146. /*
  147. * Convert the address within this vma to the page offset within
  148. * the mapping, in base page units.
  149. */
  150. static pgoff_t vma_page_offset(struct vm_area_struct *vma,
  151. unsigned long address)
  152. {
  153. return ((address - vma->vm_start) >> PAGE_SHIFT) +
  154. (vma->vm_pgoff >> PAGE_SHIFT);
  155. }
  156. /*
  157. * Convert the address within this vma to the page offset within
  158. * the mapping, in pagecache page units; huge pages here.
  159. */
  160. static pgoff_t vma_pagecache_offset(struct vm_area_struct *vma,
  161. unsigned long address)
  162. {
  163. return ((address - vma->vm_start) >> HPAGE_SHIFT) +
  164. (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
  165. }
  166. #define HPAGE_RESV_OWNER (1UL << (BITS_PER_LONG - 1))
  167. #define HPAGE_RESV_UNMAPPED (1UL << (BITS_PER_LONG - 2))
  168. #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
  169. /*
  170. * These helpers are used to track how many pages are reserved for
  171. * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
  172. * is guaranteed to have their future faults succeed.
  173. *
  174. * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
  175. * the reserve counters are updated with the hugetlb_lock held. It is safe
  176. * to reset the VMA at fork() time as it is not in use yet and there is no
  177. * chance of the global counters getting corrupted as a result of the values.
  178. */
  179. static unsigned long get_vma_private_data(struct vm_area_struct *vma)
  180. {
  181. return (unsigned long)vma->vm_private_data;
  182. }
  183. static void set_vma_private_data(struct vm_area_struct *vma,
  184. unsigned long value)
  185. {
  186. vma->vm_private_data = (void *)value;
  187. }
  188. static unsigned long vma_resv_huge_pages(struct vm_area_struct *vma)
  189. {
  190. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  191. if (!(vma->vm_flags & VM_SHARED))
  192. return get_vma_private_data(vma) & ~HPAGE_RESV_MASK;
  193. return 0;
  194. }
  195. static void set_vma_resv_huge_pages(struct vm_area_struct *vma,
  196. unsigned long reserve)
  197. {
  198. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  199. VM_BUG_ON(vma->vm_flags & VM_SHARED);
  200. set_vma_private_data(vma,
  201. (get_vma_private_data(vma) & HPAGE_RESV_MASK) | reserve);
  202. }
  203. static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
  204. {
  205. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  206. VM_BUG_ON(vma->vm_flags & VM_SHARED);
  207. set_vma_private_data(vma, get_vma_private_data(vma) | flags);
  208. }
  209. static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
  210. {
  211. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  212. return (get_vma_private_data(vma) & flag) != 0;
  213. }
  214. /* Decrement the reserved pages in the hugepage pool by one */
  215. static void decrement_hugepage_resv_vma(struct vm_area_struct *vma)
  216. {
  217. if (vma->vm_flags & VM_NORESERVE)
  218. return;
  219. if (vma->vm_flags & VM_SHARED) {
  220. /* Shared mappings always use reserves */
  221. resv_huge_pages--;
  222. } else {
  223. /*
  224. * Only the process that called mmap() has reserves for
  225. * private mappings.
  226. */
  227. if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  228. unsigned long flags, reserve;
  229. resv_huge_pages--;
  230. flags = (unsigned long)vma->vm_private_data &
  231. HPAGE_RESV_MASK;
  232. reserve = (unsigned long)vma->vm_private_data - 1;
  233. vma->vm_private_data = (void *)(reserve | flags);
  234. }
  235. }
  236. }
  237. /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
  238. void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
  239. {
  240. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  241. if (!(vma->vm_flags & VM_SHARED))
  242. vma->vm_private_data = (void *)0;
  243. }
  244. /* Returns true if the VMA has associated reserve pages */
  245. static int vma_has_private_reserves(struct vm_area_struct *vma)
  246. {
  247. if (vma->vm_flags & VM_SHARED)
  248. return 0;
  249. if (!vma_resv_huge_pages(vma))
  250. return 0;
  251. return 1;
  252. }
  253. static void clear_huge_page(struct page *page, unsigned long addr)
  254. {
  255. int i;
  256. might_sleep();
  257. for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
  258. cond_resched();
  259. clear_user_highpage(page + i, addr + i * PAGE_SIZE);
  260. }
  261. }
  262. static void copy_huge_page(struct page *dst, struct page *src,
  263. unsigned long addr, struct vm_area_struct *vma)
  264. {
  265. int i;
  266. might_sleep();
  267. for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
  268. cond_resched();
  269. copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
  270. }
  271. }
  272. static void enqueue_huge_page(struct page *page)
  273. {
  274. int nid = page_to_nid(page);
  275. list_add(&page->lru, &hugepage_freelists[nid]);
  276. free_huge_pages++;
  277. free_huge_pages_node[nid]++;
  278. }
  279. static struct page *dequeue_huge_page(void)
  280. {
  281. int nid;
  282. struct page *page = NULL;
  283. for (nid = 0; nid < MAX_NUMNODES; ++nid) {
  284. if (!list_empty(&hugepage_freelists[nid])) {
  285. page = list_entry(hugepage_freelists[nid].next,
  286. struct page, lru);
  287. list_del(&page->lru);
  288. free_huge_pages--;
  289. free_huge_pages_node[nid]--;
  290. break;
  291. }
  292. }
  293. return page;
  294. }
  295. static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma,
  296. unsigned long address, int avoid_reserve)
  297. {
  298. int nid;
  299. struct page *page = NULL;
  300. struct mempolicy *mpol;
  301. nodemask_t *nodemask;
  302. struct zonelist *zonelist = huge_zonelist(vma, address,
  303. htlb_alloc_mask, &mpol, &nodemask);
  304. struct zone *zone;
  305. struct zoneref *z;
  306. /*
  307. * A child process with MAP_PRIVATE mappings created by their parent
  308. * have no page reserves. This check ensures that reservations are
  309. * not "stolen". The child may still get SIGKILLed
  310. */
  311. if (!vma_has_private_reserves(vma) &&
  312. free_huge_pages - resv_huge_pages == 0)
  313. return NULL;
  314. /* If reserves cannot be used, ensure enough pages are in the pool */
  315. if (avoid_reserve && free_huge_pages - resv_huge_pages == 0)
  316. return NULL;
  317. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  318. MAX_NR_ZONES - 1, nodemask) {
  319. nid = zone_to_nid(zone);
  320. if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
  321. !list_empty(&hugepage_freelists[nid])) {
  322. page = list_entry(hugepage_freelists[nid].next,
  323. struct page, lru);
  324. list_del(&page->lru);
  325. free_huge_pages--;
  326. free_huge_pages_node[nid]--;
  327. if (!avoid_reserve)
  328. decrement_hugepage_resv_vma(vma);
  329. break;
  330. }
  331. }
  332. mpol_cond_put(mpol);
  333. return page;
  334. }
  335. static void update_and_free_page(struct page *page)
  336. {
  337. int i;
  338. nr_huge_pages--;
  339. nr_huge_pages_node[page_to_nid(page)]--;
  340. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
  341. page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
  342. 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
  343. 1 << PG_private | 1<< PG_writeback);
  344. }
  345. set_compound_page_dtor(page, NULL);
  346. set_page_refcounted(page);
  347. arch_release_hugepage(page);
  348. __free_pages(page, HUGETLB_PAGE_ORDER);
  349. }
  350. static void free_huge_page(struct page *page)
  351. {
  352. int nid = page_to_nid(page);
  353. struct address_space *mapping;
  354. mapping = (struct address_space *) page_private(page);
  355. set_page_private(page, 0);
  356. BUG_ON(page_count(page));
  357. INIT_LIST_HEAD(&page->lru);
  358. spin_lock(&hugetlb_lock);
  359. if (surplus_huge_pages_node[nid]) {
  360. update_and_free_page(page);
  361. surplus_huge_pages--;
  362. surplus_huge_pages_node[nid]--;
  363. } else {
  364. enqueue_huge_page(page);
  365. }
  366. spin_unlock(&hugetlb_lock);
  367. if (mapping)
  368. hugetlb_put_quota(mapping, 1);
  369. }
  370. /*
  371. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  372. * balanced by operating on them in a round-robin fashion.
  373. * Returns 1 if an adjustment was made.
  374. */
  375. static int adjust_pool_surplus(int delta)
  376. {
  377. static int prev_nid;
  378. int nid = prev_nid;
  379. int ret = 0;
  380. VM_BUG_ON(delta != -1 && delta != 1);
  381. do {
  382. nid = next_node(nid, node_online_map);
  383. if (nid == MAX_NUMNODES)
  384. nid = first_node(node_online_map);
  385. /* To shrink on this node, there must be a surplus page */
  386. if (delta < 0 && !surplus_huge_pages_node[nid])
  387. continue;
  388. /* Surplus cannot exceed the total number of pages */
  389. if (delta > 0 && surplus_huge_pages_node[nid] >=
  390. nr_huge_pages_node[nid])
  391. continue;
  392. surplus_huge_pages += delta;
  393. surplus_huge_pages_node[nid] += delta;
  394. ret = 1;
  395. break;
  396. } while (nid != prev_nid);
  397. prev_nid = nid;
  398. return ret;
  399. }
  400. static struct page *alloc_fresh_huge_page_node(int nid)
  401. {
  402. struct page *page;
  403. page = alloc_pages_node(nid,
  404. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
  405. __GFP_REPEAT|__GFP_NOWARN,
  406. HUGETLB_PAGE_ORDER);
  407. if (page) {
  408. if (arch_prepare_hugepage(page)) {
  409. __free_pages(page, HUGETLB_PAGE_ORDER);
  410. return NULL;
  411. }
  412. set_compound_page_dtor(page, free_huge_page);
  413. spin_lock(&hugetlb_lock);
  414. nr_huge_pages++;
  415. nr_huge_pages_node[nid]++;
  416. spin_unlock(&hugetlb_lock);
  417. put_page(page); /* free it into the hugepage allocator */
  418. }
  419. return page;
  420. }
  421. static int alloc_fresh_huge_page(void)
  422. {
  423. struct page *page;
  424. int start_nid;
  425. int next_nid;
  426. int ret = 0;
  427. start_nid = hugetlb_next_nid;
  428. do {
  429. page = alloc_fresh_huge_page_node(hugetlb_next_nid);
  430. if (page)
  431. ret = 1;
  432. /*
  433. * Use a helper variable to find the next node and then
  434. * copy it back to hugetlb_next_nid afterwards:
  435. * otherwise there's a window in which a racer might
  436. * pass invalid nid MAX_NUMNODES to alloc_pages_node.
  437. * But we don't need to use a spin_lock here: it really
  438. * doesn't matter if occasionally a racer chooses the
  439. * same nid as we do. Move nid forward in the mask even
  440. * if we just successfully allocated a hugepage so that
  441. * the next caller gets hugepages on the next node.
  442. */
  443. next_nid = next_node(hugetlb_next_nid, node_online_map);
  444. if (next_nid == MAX_NUMNODES)
  445. next_nid = first_node(node_online_map);
  446. hugetlb_next_nid = next_nid;
  447. } while (!page && hugetlb_next_nid != start_nid);
  448. if (ret)
  449. count_vm_event(HTLB_BUDDY_PGALLOC);
  450. else
  451. count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  452. return ret;
  453. }
  454. static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma,
  455. unsigned long address)
  456. {
  457. struct page *page;
  458. unsigned int nid;
  459. /*
  460. * Assume we will successfully allocate the surplus page to
  461. * prevent racing processes from causing the surplus to exceed
  462. * overcommit
  463. *
  464. * This however introduces a different race, where a process B
  465. * tries to grow the static hugepage pool while alloc_pages() is
  466. * called by process A. B will only examine the per-node
  467. * counters in determining if surplus huge pages can be
  468. * converted to normal huge pages in adjust_pool_surplus(). A
  469. * won't be able to increment the per-node counter, until the
  470. * lock is dropped by B, but B doesn't drop hugetlb_lock until
  471. * no more huge pages can be converted from surplus to normal
  472. * state (and doesn't try to convert again). Thus, we have a
  473. * case where a surplus huge page exists, the pool is grown, and
  474. * the surplus huge page still exists after, even though it
  475. * should just have been converted to a normal huge page. This
  476. * does not leak memory, though, as the hugepage will be freed
  477. * once it is out of use. It also does not allow the counters to
  478. * go out of whack in adjust_pool_surplus() as we don't modify
  479. * the node values until we've gotten the hugepage and only the
  480. * per-node value is checked there.
  481. */
  482. spin_lock(&hugetlb_lock);
  483. if (surplus_huge_pages >= nr_overcommit_huge_pages) {
  484. spin_unlock(&hugetlb_lock);
  485. return NULL;
  486. } else {
  487. nr_huge_pages++;
  488. surplus_huge_pages++;
  489. }
  490. spin_unlock(&hugetlb_lock);
  491. page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
  492. __GFP_REPEAT|__GFP_NOWARN,
  493. HUGETLB_PAGE_ORDER);
  494. spin_lock(&hugetlb_lock);
  495. if (page) {
  496. /*
  497. * This page is now managed by the hugetlb allocator and has
  498. * no users -- drop the buddy allocator's reference.
  499. */
  500. put_page_testzero(page);
  501. VM_BUG_ON(page_count(page));
  502. nid = page_to_nid(page);
  503. set_compound_page_dtor(page, free_huge_page);
  504. /*
  505. * We incremented the global counters already
  506. */
  507. nr_huge_pages_node[nid]++;
  508. surplus_huge_pages_node[nid]++;
  509. __count_vm_event(HTLB_BUDDY_PGALLOC);
  510. } else {
  511. nr_huge_pages--;
  512. surplus_huge_pages--;
  513. __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  514. }
  515. spin_unlock(&hugetlb_lock);
  516. return page;
  517. }
  518. /*
  519. * Increase the hugetlb pool such that it can accomodate a reservation
  520. * of size 'delta'.
  521. */
  522. static int gather_surplus_pages(int delta)
  523. {
  524. struct list_head surplus_list;
  525. struct page *page, *tmp;
  526. int ret, i;
  527. int needed, allocated;
  528. needed = (resv_huge_pages + delta) - free_huge_pages;
  529. if (needed <= 0) {
  530. resv_huge_pages += delta;
  531. return 0;
  532. }
  533. allocated = 0;
  534. INIT_LIST_HEAD(&surplus_list);
  535. ret = -ENOMEM;
  536. retry:
  537. spin_unlock(&hugetlb_lock);
  538. for (i = 0; i < needed; i++) {
  539. page = alloc_buddy_huge_page(NULL, 0);
  540. if (!page) {
  541. /*
  542. * We were not able to allocate enough pages to
  543. * satisfy the entire reservation so we free what
  544. * we've allocated so far.
  545. */
  546. spin_lock(&hugetlb_lock);
  547. needed = 0;
  548. goto free;
  549. }
  550. list_add(&page->lru, &surplus_list);
  551. }
  552. allocated += needed;
  553. /*
  554. * After retaking hugetlb_lock, we need to recalculate 'needed'
  555. * because either resv_huge_pages or free_huge_pages may have changed.
  556. */
  557. spin_lock(&hugetlb_lock);
  558. needed = (resv_huge_pages + delta) - (free_huge_pages + allocated);
  559. if (needed > 0)
  560. goto retry;
  561. /*
  562. * The surplus_list now contains _at_least_ the number of extra pages
  563. * needed to accomodate the reservation. Add the appropriate number
  564. * of pages to the hugetlb pool and free the extras back to the buddy
  565. * allocator. Commit the entire reservation here to prevent another
  566. * process from stealing the pages as they are added to the pool but
  567. * before they are reserved.
  568. */
  569. needed += allocated;
  570. resv_huge_pages += delta;
  571. ret = 0;
  572. free:
  573. /* Free the needed pages to the hugetlb pool */
  574. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  575. if ((--needed) < 0)
  576. break;
  577. list_del(&page->lru);
  578. enqueue_huge_page(page);
  579. }
  580. /* Free unnecessary surplus pages to the buddy allocator */
  581. if (!list_empty(&surplus_list)) {
  582. spin_unlock(&hugetlb_lock);
  583. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  584. list_del(&page->lru);
  585. /*
  586. * The page has a reference count of zero already, so
  587. * call free_huge_page directly instead of using
  588. * put_page. This must be done with hugetlb_lock
  589. * unlocked which is safe because free_huge_page takes
  590. * hugetlb_lock before deciding how to free the page.
  591. */
  592. free_huge_page(page);
  593. }
  594. spin_lock(&hugetlb_lock);
  595. }
  596. return ret;
  597. }
  598. /*
  599. * When releasing a hugetlb pool reservation, any surplus pages that were
  600. * allocated to satisfy the reservation must be explicitly freed if they were
  601. * never used.
  602. */
  603. static void return_unused_surplus_pages(unsigned long unused_resv_pages)
  604. {
  605. static int nid = -1;
  606. struct page *page;
  607. unsigned long nr_pages;
  608. /*
  609. * We want to release as many surplus pages as possible, spread
  610. * evenly across all nodes. Iterate across all nodes until we
  611. * can no longer free unreserved surplus pages. This occurs when
  612. * the nodes with surplus pages have no free pages.
  613. */
  614. unsigned long remaining_iterations = num_online_nodes();
  615. /* Uncommit the reservation */
  616. resv_huge_pages -= unused_resv_pages;
  617. nr_pages = min(unused_resv_pages, surplus_huge_pages);
  618. while (remaining_iterations-- && nr_pages) {
  619. nid = next_node(nid, node_online_map);
  620. if (nid == MAX_NUMNODES)
  621. nid = first_node(node_online_map);
  622. if (!surplus_huge_pages_node[nid])
  623. continue;
  624. if (!list_empty(&hugepage_freelists[nid])) {
  625. page = list_entry(hugepage_freelists[nid].next,
  626. struct page, lru);
  627. list_del(&page->lru);
  628. update_and_free_page(page);
  629. free_huge_pages--;
  630. free_huge_pages_node[nid]--;
  631. surplus_huge_pages--;
  632. surplus_huge_pages_node[nid]--;
  633. nr_pages--;
  634. remaining_iterations = num_online_nodes();
  635. }
  636. }
  637. }
  638. /*
  639. * Determine if the huge page at addr within the vma has an associated
  640. * reservation. Where it does not we will need to logically increase
  641. * reservation and actually increase quota before an allocation can occur.
  642. * Where any new reservation would be required the reservation change is
  643. * prepared, but not committed. Once the page has been quota'd allocated
  644. * an instantiated the change should be committed via vma_commit_reservation.
  645. * No action is required on failure.
  646. */
  647. static int vma_needs_reservation(struct vm_area_struct *vma, unsigned long addr)
  648. {
  649. struct address_space *mapping = vma->vm_file->f_mapping;
  650. struct inode *inode = mapping->host;
  651. if (vma->vm_flags & VM_SHARED) {
  652. pgoff_t idx = vma_pagecache_offset(vma, addr);
  653. return region_chg(&inode->i_mapping->private_list,
  654. idx, idx + 1);
  655. } else {
  656. if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER))
  657. return 1;
  658. }
  659. return 0;
  660. }
  661. static void vma_commit_reservation(struct vm_area_struct *vma,
  662. unsigned long addr)
  663. {
  664. struct address_space *mapping = vma->vm_file->f_mapping;
  665. struct inode *inode = mapping->host;
  666. if (vma->vm_flags & VM_SHARED) {
  667. pgoff_t idx = vma_pagecache_offset(vma, addr);
  668. region_add(&inode->i_mapping->private_list, idx, idx + 1);
  669. }
  670. }
  671. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  672. unsigned long addr, int avoid_reserve)
  673. {
  674. struct page *page;
  675. struct address_space *mapping = vma->vm_file->f_mapping;
  676. struct inode *inode = mapping->host;
  677. unsigned int chg;
  678. /*
  679. * Processes that did not create the mapping will have no reserves and
  680. * will not have accounted against quota. Check that the quota can be
  681. * made before satisfying the allocation
  682. * MAP_NORESERVE mappings may also need pages and quota allocated
  683. * if no reserve mapping overlaps.
  684. */
  685. chg = vma_needs_reservation(vma, addr);
  686. if (chg < 0)
  687. return ERR_PTR(chg);
  688. if (chg)
  689. if (hugetlb_get_quota(inode->i_mapping, chg))
  690. return ERR_PTR(-ENOSPC);
  691. spin_lock(&hugetlb_lock);
  692. page = dequeue_huge_page_vma(vma, addr, avoid_reserve);
  693. spin_unlock(&hugetlb_lock);
  694. if (!page) {
  695. page = alloc_buddy_huge_page(vma, addr);
  696. if (!page) {
  697. hugetlb_put_quota(inode->i_mapping, chg);
  698. return ERR_PTR(-VM_FAULT_OOM);
  699. }
  700. }
  701. set_page_refcounted(page);
  702. set_page_private(page, (unsigned long) mapping);
  703. vma_commit_reservation(vma, addr);
  704. return page;
  705. }
  706. static int __init hugetlb_init(void)
  707. {
  708. unsigned long i;
  709. if (HPAGE_SHIFT == 0)
  710. return 0;
  711. for (i = 0; i < MAX_NUMNODES; ++i)
  712. INIT_LIST_HEAD(&hugepage_freelists[i]);
  713. hugetlb_next_nid = first_node(node_online_map);
  714. for (i = 0; i < max_huge_pages; ++i) {
  715. if (!alloc_fresh_huge_page())
  716. break;
  717. }
  718. max_huge_pages = free_huge_pages = nr_huge_pages = i;
  719. printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
  720. return 0;
  721. }
  722. module_init(hugetlb_init);
  723. static int __init hugetlb_setup(char *s)
  724. {
  725. if (sscanf(s, "%lu", &max_huge_pages) <= 0)
  726. max_huge_pages = 0;
  727. return 1;
  728. }
  729. __setup("hugepages=", hugetlb_setup);
  730. static unsigned int cpuset_mems_nr(unsigned int *array)
  731. {
  732. int node;
  733. unsigned int nr = 0;
  734. for_each_node_mask(node, cpuset_current_mems_allowed)
  735. nr += array[node];
  736. return nr;
  737. }
  738. #ifdef CONFIG_SYSCTL
  739. #ifdef CONFIG_HIGHMEM
  740. static void try_to_free_low(unsigned long count)
  741. {
  742. int i;
  743. for (i = 0; i < MAX_NUMNODES; ++i) {
  744. struct page *page, *next;
  745. list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
  746. if (count >= nr_huge_pages)
  747. return;
  748. if (PageHighMem(page))
  749. continue;
  750. list_del(&page->lru);
  751. update_and_free_page(page);
  752. free_huge_pages--;
  753. free_huge_pages_node[page_to_nid(page)]--;
  754. }
  755. }
  756. }
  757. #else
  758. static inline void try_to_free_low(unsigned long count)
  759. {
  760. }
  761. #endif
  762. #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
  763. static unsigned long set_max_huge_pages(unsigned long count)
  764. {
  765. unsigned long min_count, ret;
  766. /*
  767. * Increase the pool size
  768. * First take pages out of surplus state. Then make up the
  769. * remaining difference by allocating fresh huge pages.
  770. *
  771. * We might race with alloc_buddy_huge_page() here and be unable
  772. * to convert a surplus huge page to a normal huge page. That is
  773. * not critical, though, it just means the overall size of the
  774. * pool might be one hugepage larger than it needs to be, but
  775. * within all the constraints specified by the sysctls.
  776. */
  777. spin_lock(&hugetlb_lock);
  778. while (surplus_huge_pages && count > persistent_huge_pages) {
  779. if (!adjust_pool_surplus(-1))
  780. break;
  781. }
  782. while (count > persistent_huge_pages) {
  783. /*
  784. * If this allocation races such that we no longer need the
  785. * page, free_huge_page will handle it by freeing the page
  786. * and reducing the surplus.
  787. */
  788. spin_unlock(&hugetlb_lock);
  789. ret = alloc_fresh_huge_page();
  790. spin_lock(&hugetlb_lock);
  791. if (!ret)
  792. goto out;
  793. }
  794. /*
  795. * Decrease the pool size
  796. * First return free pages to the buddy allocator (being careful
  797. * to keep enough around to satisfy reservations). Then place
  798. * pages into surplus state as needed so the pool will shrink
  799. * to the desired size as pages become free.
  800. *
  801. * By placing pages into the surplus state independent of the
  802. * overcommit value, we are allowing the surplus pool size to
  803. * exceed overcommit. There are few sane options here. Since
  804. * alloc_buddy_huge_page() is checking the global counter,
  805. * though, we'll note that we're not allowed to exceed surplus
  806. * and won't grow the pool anywhere else. Not until one of the
  807. * sysctls are changed, or the surplus pages go out of use.
  808. */
  809. min_count = resv_huge_pages + nr_huge_pages - free_huge_pages;
  810. min_count = max(count, min_count);
  811. try_to_free_low(min_count);
  812. while (min_count < persistent_huge_pages) {
  813. struct page *page = dequeue_huge_page();
  814. if (!page)
  815. break;
  816. update_and_free_page(page);
  817. }
  818. while (count < persistent_huge_pages) {
  819. if (!adjust_pool_surplus(1))
  820. break;
  821. }
  822. out:
  823. ret = persistent_huge_pages;
  824. spin_unlock(&hugetlb_lock);
  825. return ret;
  826. }
  827. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  828. struct file *file, void __user *buffer,
  829. size_t *length, loff_t *ppos)
  830. {
  831. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  832. max_huge_pages = set_max_huge_pages(max_huge_pages);
  833. return 0;
  834. }
  835. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  836. struct file *file, void __user *buffer,
  837. size_t *length, loff_t *ppos)
  838. {
  839. proc_dointvec(table, write, file, buffer, length, ppos);
  840. if (hugepages_treat_as_movable)
  841. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  842. else
  843. htlb_alloc_mask = GFP_HIGHUSER;
  844. return 0;
  845. }
  846. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  847. struct file *file, void __user *buffer,
  848. size_t *length, loff_t *ppos)
  849. {
  850. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  851. spin_lock(&hugetlb_lock);
  852. nr_overcommit_huge_pages = sysctl_overcommit_huge_pages;
  853. spin_unlock(&hugetlb_lock);
  854. return 0;
  855. }
  856. #endif /* CONFIG_SYSCTL */
  857. int hugetlb_report_meminfo(char *buf)
  858. {
  859. return sprintf(buf,
  860. "HugePages_Total: %5lu\n"
  861. "HugePages_Free: %5lu\n"
  862. "HugePages_Rsvd: %5lu\n"
  863. "HugePages_Surp: %5lu\n"
  864. "Hugepagesize: %5lu kB\n",
  865. nr_huge_pages,
  866. free_huge_pages,
  867. resv_huge_pages,
  868. surplus_huge_pages,
  869. HPAGE_SIZE/1024);
  870. }
  871. int hugetlb_report_node_meminfo(int nid, char *buf)
  872. {
  873. return sprintf(buf,
  874. "Node %d HugePages_Total: %5u\n"
  875. "Node %d HugePages_Free: %5u\n"
  876. "Node %d HugePages_Surp: %5u\n",
  877. nid, nr_huge_pages_node[nid],
  878. nid, free_huge_pages_node[nid],
  879. nid, surplus_huge_pages_node[nid]);
  880. }
  881. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  882. unsigned long hugetlb_total_pages(void)
  883. {
  884. return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
  885. }
  886. static int hugetlb_acct_memory(long delta)
  887. {
  888. int ret = -ENOMEM;
  889. spin_lock(&hugetlb_lock);
  890. /*
  891. * When cpuset is configured, it breaks the strict hugetlb page
  892. * reservation as the accounting is done on a global variable. Such
  893. * reservation is completely rubbish in the presence of cpuset because
  894. * the reservation is not checked against page availability for the
  895. * current cpuset. Application can still potentially OOM'ed by kernel
  896. * with lack of free htlb page in cpuset that the task is in.
  897. * Attempt to enforce strict accounting with cpuset is almost
  898. * impossible (or too ugly) because cpuset is too fluid that
  899. * task or memory node can be dynamically moved between cpusets.
  900. *
  901. * The change of semantics for shared hugetlb mapping with cpuset is
  902. * undesirable. However, in order to preserve some of the semantics,
  903. * we fall back to check against current free page availability as
  904. * a best attempt and hopefully to minimize the impact of changing
  905. * semantics that cpuset has.
  906. */
  907. if (delta > 0) {
  908. if (gather_surplus_pages(delta) < 0)
  909. goto out;
  910. if (delta > cpuset_mems_nr(free_huge_pages_node)) {
  911. return_unused_surplus_pages(delta);
  912. goto out;
  913. }
  914. }
  915. ret = 0;
  916. if (delta < 0)
  917. return_unused_surplus_pages((unsigned long) -delta);
  918. out:
  919. spin_unlock(&hugetlb_lock);
  920. return ret;
  921. }
  922. static void hugetlb_vm_op_close(struct vm_area_struct *vma)
  923. {
  924. unsigned long reserve = vma_resv_huge_pages(vma);
  925. if (reserve)
  926. hugetlb_acct_memory(-reserve);
  927. }
  928. /*
  929. * We cannot handle pagefaults against hugetlb pages at all. They cause
  930. * handle_mm_fault() to try to instantiate regular-sized pages in the
  931. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  932. * this far.
  933. */
  934. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  935. {
  936. BUG();
  937. return 0;
  938. }
  939. struct vm_operations_struct hugetlb_vm_ops = {
  940. .fault = hugetlb_vm_op_fault,
  941. .close = hugetlb_vm_op_close,
  942. };
  943. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  944. int writable)
  945. {
  946. pte_t entry;
  947. if (writable) {
  948. entry =
  949. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  950. } else {
  951. entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  952. }
  953. entry = pte_mkyoung(entry);
  954. entry = pte_mkhuge(entry);
  955. return entry;
  956. }
  957. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  958. unsigned long address, pte_t *ptep)
  959. {
  960. pte_t entry;
  961. entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
  962. if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
  963. update_mmu_cache(vma, address, entry);
  964. }
  965. }
  966. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  967. struct vm_area_struct *vma)
  968. {
  969. pte_t *src_pte, *dst_pte, entry;
  970. struct page *ptepage;
  971. unsigned long addr;
  972. int cow;
  973. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  974. for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
  975. src_pte = huge_pte_offset(src, addr);
  976. if (!src_pte)
  977. continue;
  978. dst_pte = huge_pte_alloc(dst, addr);
  979. if (!dst_pte)
  980. goto nomem;
  981. /* If the pagetables are shared don't copy or take references */
  982. if (dst_pte == src_pte)
  983. continue;
  984. spin_lock(&dst->page_table_lock);
  985. spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
  986. if (!huge_pte_none(huge_ptep_get(src_pte))) {
  987. if (cow)
  988. huge_ptep_set_wrprotect(src, addr, src_pte);
  989. entry = huge_ptep_get(src_pte);
  990. ptepage = pte_page(entry);
  991. get_page(ptepage);
  992. set_huge_pte_at(dst, addr, dst_pte, entry);
  993. }
  994. spin_unlock(&src->page_table_lock);
  995. spin_unlock(&dst->page_table_lock);
  996. }
  997. return 0;
  998. nomem:
  999. return -ENOMEM;
  1000. }
  1001. void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  1002. unsigned long end, struct page *ref_page)
  1003. {
  1004. struct mm_struct *mm = vma->vm_mm;
  1005. unsigned long address;
  1006. pte_t *ptep;
  1007. pte_t pte;
  1008. struct page *page;
  1009. struct page *tmp;
  1010. /*
  1011. * A page gathering list, protected by per file i_mmap_lock. The
  1012. * lock is used to avoid list corruption from multiple unmapping
  1013. * of the same page since we are using page->lru.
  1014. */
  1015. LIST_HEAD(page_list);
  1016. WARN_ON(!is_vm_hugetlb_page(vma));
  1017. BUG_ON(start & ~HPAGE_MASK);
  1018. BUG_ON(end & ~HPAGE_MASK);
  1019. spin_lock(&mm->page_table_lock);
  1020. for (address = start; address < end; address += HPAGE_SIZE) {
  1021. ptep = huge_pte_offset(mm, address);
  1022. if (!ptep)
  1023. continue;
  1024. if (huge_pmd_unshare(mm, &address, ptep))
  1025. continue;
  1026. /*
  1027. * If a reference page is supplied, it is because a specific
  1028. * page is being unmapped, not a range. Ensure the page we
  1029. * are about to unmap is the actual page of interest.
  1030. */
  1031. if (ref_page) {
  1032. pte = huge_ptep_get(ptep);
  1033. if (huge_pte_none(pte))
  1034. continue;
  1035. page = pte_page(pte);
  1036. if (page != ref_page)
  1037. continue;
  1038. /*
  1039. * Mark the VMA as having unmapped its page so that
  1040. * future faults in this VMA will fail rather than
  1041. * looking like data was lost
  1042. */
  1043. set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
  1044. }
  1045. pte = huge_ptep_get_and_clear(mm, address, ptep);
  1046. if (huge_pte_none(pte))
  1047. continue;
  1048. page = pte_page(pte);
  1049. if (pte_dirty(pte))
  1050. set_page_dirty(page);
  1051. list_add(&page->lru, &page_list);
  1052. }
  1053. spin_unlock(&mm->page_table_lock);
  1054. flush_tlb_range(vma, start, end);
  1055. list_for_each_entry_safe(page, tmp, &page_list, lru) {
  1056. list_del(&page->lru);
  1057. put_page(page);
  1058. }
  1059. }
  1060. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  1061. unsigned long end, struct page *ref_page)
  1062. {
  1063. /*
  1064. * It is undesirable to test vma->vm_file as it should be non-null
  1065. * for valid hugetlb area. However, vm_file will be NULL in the error
  1066. * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
  1067. * do_mmap_pgoff() nullifies vma->vm_file before calling this function
  1068. * to clean up. Since no pte has actually been setup, it is safe to
  1069. * do nothing in this case.
  1070. */
  1071. if (vma->vm_file) {
  1072. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  1073. __unmap_hugepage_range(vma, start, end, ref_page);
  1074. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  1075. }
  1076. }
  1077. /*
  1078. * This is called when the original mapper is failing to COW a MAP_PRIVATE
  1079. * mappping it owns the reserve page for. The intention is to unmap the page
  1080. * from other VMAs and let the children be SIGKILLed if they are faulting the
  1081. * same region.
  1082. */
  1083. int unmap_ref_private(struct mm_struct *mm,
  1084. struct vm_area_struct *vma,
  1085. struct page *page,
  1086. unsigned long address)
  1087. {
  1088. struct vm_area_struct *iter_vma;
  1089. struct address_space *mapping;
  1090. struct prio_tree_iter iter;
  1091. pgoff_t pgoff;
  1092. /*
  1093. * vm_pgoff is in PAGE_SIZE units, hence the different calculation
  1094. * from page cache lookup which is in HPAGE_SIZE units.
  1095. */
  1096. address = address & huge_page_mask(hstate_vma(vma));
  1097. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
  1098. + (vma->vm_pgoff >> PAGE_SHIFT);
  1099. mapping = (struct address_space *)page_private(page);
  1100. vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  1101. /* Do not unmap the current VMA */
  1102. if (iter_vma == vma)
  1103. continue;
  1104. /*
  1105. * Unmap the page from other VMAs without their own reserves.
  1106. * They get marked to be SIGKILLed if they fault in these
  1107. * areas. This is because a future no-page fault on this VMA
  1108. * could insert a zeroed page instead of the data existing
  1109. * from the time of fork. This would look like data corruption
  1110. */
  1111. if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
  1112. unmap_hugepage_range(iter_vma,
  1113. address, address + HPAGE_SIZE,
  1114. page);
  1115. }
  1116. return 1;
  1117. }
  1118. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  1119. unsigned long address, pte_t *ptep, pte_t pte,
  1120. struct page *pagecache_page)
  1121. {
  1122. struct page *old_page, *new_page;
  1123. int avoidcopy;
  1124. int outside_reserve = 0;
  1125. old_page = pte_page(pte);
  1126. retry_avoidcopy:
  1127. /* If no-one else is actually using this page, avoid the copy
  1128. * and just make the page writable */
  1129. avoidcopy = (page_count(old_page) == 1);
  1130. if (avoidcopy) {
  1131. set_huge_ptep_writable(vma, address, ptep);
  1132. return 0;
  1133. }
  1134. /*
  1135. * If the process that created a MAP_PRIVATE mapping is about to
  1136. * perform a COW due to a shared page count, attempt to satisfy
  1137. * the allocation without using the existing reserves. The pagecache
  1138. * page is used to determine if the reserve at this address was
  1139. * consumed or not. If reserves were used, a partial faulted mapping
  1140. * at the time of fork() could consume its reserves on COW instead
  1141. * of the full address range.
  1142. */
  1143. if (!(vma->vm_flags & VM_SHARED) &&
  1144. is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
  1145. old_page != pagecache_page)
  1146. outside_reserve = 1;
  1147. page_cache_get(old_page);
  1148. new_page = alloc_huge_page(vma, address, outside_reserve);
  1149. if (IS_ERR(new_page)) {
  1150. page_cache_release(old_page);
  1151. /*
  1152. * If a process owning a MAP_PRIVATE mapping fails to COW,
  1153. * it is due to references held by a child and an insufficient
  1154. * huge page pool. To guarantee the original mappers
  1155. * reliability, unmap the page from child processes. The child
  1156. * may get SIGKILLed if it later faults.
  1157. */
  1158. if (outside_reserve) {
  1159. BUG_ON(huge_pte_none(pte));
  1160. if (unmap_ref_private(mm, vma, old_page, address)) {
  1161. BUG_ON(page_count(old_page) != 1);
  1162. BUG_ON(huge_pte_none(pte));
  1163. goto retry_avoidcopy;
  1164. }
  1165. WARN_ON_ONCE(1);
  1166. }
  1167. return -PTR_ERR(new_page);
  1168. }
  1169. spin_unlock(&mm->page_table_lock);
  1170. copy_huge_page(new_page, old_page, address, vma);
  1171. __SetPageUptodate(new_page);
  1172. spin_lock(&mm->page_table_lock);
  1173. ptep = huge_pte_offset(mm, address & HPAGE_MASK);
  1174. if (likely(pte_same(huge_ptep_get(ptep), pte))) {
  1175. /* Break COW */
  1176. huge_ptep_clear_flush(vma, address, ptep);
  1177. set_huge_pte_at(mm, address, ptep,
  1178. make_huge_pte(vma, new_page, 1));
  1179. /* Make the old page be freed below */
  1180. new_page = old_page;
  1181. }
  1182. page_cache_release(new_page);
  1183. page_cache_release(old_page);
  1184. return 0;
  1185. }
  1186. /* Return the pagecache page at a given address within a VMA */
  1187. static struct page *hugetlbfs_pagecache_page(struct vm_area_struct *vma,
  1188. unsigned long address)
  1189. {
  1190. struct address_space *mapping;
  1191. pgoff_t idx;
  1192. mapping = vma->vm_file->f_mapping;
  1193. idx = vma_pagecache_offset(vma, address);
  1194. return find_lock_page(mapping, idx);
  1195. }
  1196. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1197. unsigned long address, pte_t *ptep, int write_access)
  1198. {
  1199. int ret = VM_FAULT_SIGBUS;
  1200. pgoff_t idx;
  1201. unsigned long size;
  1202. struct page *page;
  1203. struct address_space *mapping;
  1204. pte_t new_pte;
  1205. /*
  1206. * Currently, we are forced to kill the process in the event the
  1207. * original mapper has unmapped pages from the child due to a failed
  1208. * COW. Warn that such a situation has occured as it may not be obvious
  1209. */
  1210. if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
  1211. printk(KERN_WARNING
  1212. "PID %d killed due to inadequate hugepage pool\n",
  1213. current->pid);
  1214. return ret;
  1215. }
  1216. mapping = vma->vm_file->f_mapping;
  1217. idx = vma_pagecache_offset(vma, address);
  1218. /*
  1219. * Use page lock to guard against racing truncation
  1220. * before we get page_table_lock.
  1221. */
  1222. retry:
  1223. page = find_lock_page(mapping, idx);
  1224. if (!page) {
  1225. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  1226. if (idx >= size)
  1227. goto out;
  1228. page = alloc_huge_page(vma, address, 0);
  1229. if (IS_ERR(page)) {
  1230. ret = -PTR_ERR(page);
  1231. goto out;
  1232. }
  1233. clear_huge_page(page, address);
  1234. __SetPageUptodate(page);
  1235. if (vma->vm_flags & VM_SHARED) {
  1236. int err;
  1237. struct inode *inode = mapping->host;
  1238. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  1239. if (err) {
  1240. put_page(page);
  1241. if (err == -EEXIST)
  1242. goto retry;
  1243. goto out;
  1244. }
  1245. spin_lock(&inode->i_lock);
  1246. inode->i_blocks += BLOCKS_PER_HUGEPAGE;
  1247. spin_unlock(&inode->i_lock);
  1248. } else
  1249. lock_page(page);
  1250. }
  1251. spin_lock(&mm->page_table_lock);
  1252. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  1253. if (idx >= size)
  1254. goto backout;
  1255. ret = 0;
  1256. if (!huge_pte_none(huge_ptep_get(ptep)))
  1257. goto backout;
  1258. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  1259. && (vma->vm_flags & VM_SHARED)));
  1260. set_huge_pte_at(mm, address, ptep, new_pte);
  1261. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  1262. /* Optimization, do the COW without a second fault */
  1263. ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
  1264. }
  1265. spin_unlock(&mm->page_table_lock);
  1266. unlock_page(page);
  1267. out:
  1268. return ret;
  1269. backout:
  1270. spin_unlock(&mm->page_table_lock);
  1271. unlock_page(page);
  1272. put_page(page);
  1273. goto out;
  1274. }
  1275. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  1276. unsigned long address, int write_access)
  1277. {
  1278. pte_t *ptep;
  1279. pte_t entry;
  1280. int ret;
  1281. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  1282. ptep = huge_pte_alloc(mm, address);
  1283. if (!ptep)
  1284. return VM_FAULT_OOM;
  1285. /*
  1286. * Serialize hugepage allocation and instantiation, so that we don't
  1287. * get spurious allocation failures if two CPUs race to instantiate
  1288. * the same page in the page cache.
  1289. */
  1290. mutex_lock(&hugetlb_instantiation_mutex);
  1291. entry = huge_ptep_get(ptep);
  1292. if (huge_pte_none(entry)) {
  1293. ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
  1294. mutex_unlock(&hugetlb_instantiation_mutex);
  1295. return ret;
  1296. }
  1297. ret = 0;
  1298. spin_lock(&mm->page_table_lock);
  1299. /* Check for a racing update before calling hugetlb_cow */
  1300. if (likely(pte_same(entry, huge_ptep_get(ptep))))
  1301. if (write_access && !pte_write(entry)) {
  1302. struct page *page;
  1303. page = hugetlbfs_pagecache_page(vma, address);
  1304. ret = hugetlb_cow(mm, vma, address, ptep, entry, page);
  1305. if (page) {
  1306. unlock_page(page);
  1307. put_page(page);
  1308. }
  1309. }
  1310. spin_unlock(&mm->page_table_lock);
  1311. mutex_unlock(&hugetlb_instantiation_mutex);
  1312. return ret;
  1313. }
  1314. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1315. struct page **pages, struct vm_area_struct **vmas,
  1316. unsigned long *position, int *length, int i,
  1317. int write)
  1318. {
  1319. unsigned long pfn_offset;
  1320. unsigned long vaddr = *position;
  1321. int remainder = *length;
  1322. spin_lock(&mm->page_table_lock);
  1323. while (vaddr < vma->vm_end && remainder) {
  1324. pte_t *pte;
  1325. struct page *page;
  1326. /*
  1327. * Some archs (sparc64, sh*) have multiple pte_ts to
  1328. * each hugepage. We have to make * sure we get the
  1329. * first, for the page indexing below to work.
  1330. */
  1331. pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
  1332. if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
  1333. (write && !pte_write(huge_ptep_get(pte)))) {
  1334. int ret;
  1335. spin_unlock(&mm->page_table_lock);
  1336. ret = hugetlb_fault(mm, vma, vaddr, write);
  1337. spin_lock(&mm->page_table_lock);
  1338. if (!(ret & VM_FAULT_ERROR))
  1339. continue;
  1340. remainder = 0;
  1341. if (!i)
  1342. i = -EFAULT;
  1343. break;
  1344. }
  1345. pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
  1346. page = pte_page(huge_ptep_get(pte));
  1347. same_page:
  1348. if (pages) {
  1349. get_page(page);
  1350. pages[i] = page + pfn_offset;
  1351. }
  1352. if (vmas)
  1353. vmas[i] = vma;
  1354. vaddr += PAGE_SIZE;
  1355. ++pfn_offset;
  1356. --remainder;
  1357. ++i;
  1358. if (vaddr < vma->vm_end && remainder &&
  1359. pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
  1360. /*
  1361. * We use pfn_offset to avoid touching the pageframes
  1362. * of this compound page.
  1363. */
  1364. goto same_page;
  1365. }
  1366. }
  1367. spin_unlock(&mm->page_table_lock);
  1368. *length = remainder;
  1369. *position = vaddr;
  1370. return i;
  1371. }
  1372. void hugetlb_change_protection(struct vm_area_struct *vma,
  1373. unsigned long address, unsigned long end, pgprot_t newprot)
  1374. {
  1375. struct mm_struct *mm = vma->vm_mm;
  1376. unsigned long start = address;
  1377. pte_t *ptep;
  1378. pte_t pte;
  1379. BUG_ON(address >= end);
  1380. flush_cache_range(vma, address, end);
  1381. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  1382. spin_lock(&mm->page_table_lock);
  1383. for (; address < end; address += HPAGE_SIZE) {
  1384. ptep = huge_pte_offset(mm, address);
  1385. if (!ptep)
  1386. continue;
  1387. if (huge_pmd_unshare(mm, &address, ptep))
  1388. continue;
  1389. if (!huge_pte_none(huge_ptep_get(ptep))) {
  1390. pte = huge_ptep_get_and_clear(mm, address, ptep);
  1391. pte = pte_mkhuge(pte_modify(pte, newprot));
  1392. set_huge_pte_at(mm, address, ptep, pte);
  1393. }
  1394. }
  1395. spin_unlock(&mm->page_table_lock);
  1396. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  1397. flush_tlb_range(vma, start, end);
  1398. }
  1399. int hugetlb_reserve_pages(struct inode *inode,
  1400. long from, long to,
  1401. struct vm_area_struct *vma)
  1402. {
  1403. long ret, chg;
  1404. if (vma && vma->vm_flags & VM_NORESERVE)
  1405. return 0;
  1406. /*
  1407. * Shared mappings base their reservation on the number of pages that
  1408. * are already allocated on behalf of the file. Private mappings need
  1409. * to reserve the full area even if read-only as mprotect() may be
  1410. * called to make the mapping read-write. Assume !vma is a shm mapping
  1411. */
  1412. if (!vma || vma->vm_flags & VM_SHARED)
  1413. chg = region_chg(&inode->i_mapping->private_list, from, to);
  1414. else {
  1415. chg = to - from;
  1416. set_vma_resv_huge_pages(vma, chg);
  1417. set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
  1418. }
  1419. if (chg < 0)
  1420. return chg;
  1421. if (hugetlb_get_quota(inode->i_mapping, chg))
  1422. return -ENOSPC;
  1423. ret = hugetlb_acct_memory(chg);
  1424. if (ret < 0) {
  1425. hugetlb_put_quota(inode->i_mapping, chg);
  1426. return ret;
  1427. }
  1428. if (!vma || vma->vm_flags & VM_SHARED)
  1429. region_add(&inode->i_mapping->private_list, from, to);
  1430. return 0;
  1431. }
  1432. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  1433. {
  1434. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  1435. spin_lock(&inode->i_lock);
  1436. inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed;
  1437. spin_unlock(&inode->i_lock);
  1438. hugetlb_put_quota(inode->i_mapping, (chg - freed));
  1439. hugetlb_acct_memory(-(chg - freed));
  1440. }