hugetlb.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. static void clear_huge_page(struct page *page, unsigned long addr)
  39. {
  40. int i;
  41. might_sleep();
  42. for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
  43. cond_resched();
  44. clear_user_highpage(page + i, addr + i * PAGE_SIZE);
  45. }
  46. }
  47. static void copy_huge_page(struct page *dst, struct page *src,
  48. unsigned long addr, struct vm_area_struct *vma)
  49. {
  50. int i;
  51. might_sleep();
  52. for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
  53. cond_resched();
  54. copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
  55. }
  56. }
  57. static void enqueue_huge_page(struct page *page)
  58. {
  59. int nid = page_to_nid(page);
  60. list_add(&page->lru, &hugepage_freelists[nid]);
  61. free_huge_pages++;
  62. free_huge_pages_node[nid]++;
  63. }
  64. static struct page *dequeue_huge_page(void)
  65. {
  66. int nid;
  67. struct page *page = NULL;
  68. for (nid = 0; nid < MAX_NUMNODES; ++nid) {
  69. if (!list_empty(&hugepage_freelists[nid])) {
  70. page = list_entry(hugepage_freelists[nid].next,
  71. struct page, lru);
  72. list_del(&page->lru);
  73. free_huge_pages--;
  74. free_huge_pages_node[nid]--;
  75. break;
  76. }
  77. }
  78. return page;
  79. }
  80. static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma,
  81. unsigned long address)
  82. {
  83. int nid;
  84. struct page *page = NULL;
  85. struct mempolicy *mpol;
  86. nodemask_t *nodemask;
  87. struct zonelist *zonelist = huge_zonelist(vma, address,
  88. htlb_alloc_mask, &mpol, &nodemask);
  89. struct zone *zone;
  90. struct zoneref *z;
  91. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  92. MAX_NR_ZONES - 1, nodemask) {
  93. nid = zone_to_nid(zone);
  94. if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
  95. !list_empty(&hugepage_freelists[nid])) {
  96. page = list_entry(hugepage_freelists[nid].next,
  97. struct page, lru);
  98. list_del(&page->lru);
  99. free_huge_pages--;
  100. free_huge_pages_node[nid]--;
  101. if (vma && vma->vm_flags & VM_MAYSHARE)
  102. resv_huge_pages--;
  103. break;
  104. }
  105. }
  106. mpol_free(mpol); /* unref if mpol !NULL */
  107. return page;
  108. }
  109. static void update_and_free_page(struct page *page)
  110. {
  111. int i;
  112. nr_huge_pages--;
  113. nr_huge_pages_node[page_to_nid(page)]--;
  114. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
  115. page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
  116. 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
  117. 1 << PG_private | 1<< PG_writeback);
  118. }
  119. set_compound_page_dtor(page, NULL);
  120. set_page_refcounted(page);
  121. __free_pages(page, HUGETLB_PAGE_ORDER);
  122. }
  123. static void free_huge_page(struct page *page)
  124. {
  125. int nid = page_to_nid(page);
  126. struct address_space *mapping;
  127. mapping = (struct address_space *) page_private(page);
  128. set_page_private(page, 0);
  129. BUG_ON(page_count(page));
  130. INIT_LIST_HEAD(&page->lru);
  131. spin_lock(&hugetlb_lock);
  132. if (surplus_huge_pages_node[nid]) {
  133. update_and_free_page(page);
  134. surplus_huge_pages--;
  135. surplus_huge_pages_node[nid]--;
  136. } else {
  137. enqueue_huge_page(page);
  138. }
  139. spin_unlock(&hugetlb_lock);
  140. if (mapping)
  141. hugetlb_put_quota(mapping, 1);
  142. }
  143. /*
  144. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  145. * balanced by operating on them in a round-robin fashion.
  146. * Returns 1 if an adjustment was made.
  147. */
  148. static int adjust_pool_surplus(int delta)
  149. {
  150. static int prev_nid;
  151. int nid = prev_nid;
  152. int ret = 0;
  153. VM_BUG_ON(delta != -1 && delta != 1);
  154. do {
  155. nid = next_node(nid, node_online_map);
  156. if (nid == MAX_NUMNODES)
  157. nid = first_node(node_online_map);
  158. /* To shrink on this node, there must be a surplus page */
  159. if (delta < 0 && !surplus_huge_pages_node[nid])
  160. continue;
  161. /* Surplus cannot exceed the total number of pages */
  162. if (delta > 0 && surplus_huge_pages_node[nid] >=
  163. nr_huge_pages_node[nid])
  164. continue;
  165. surplus_huge_pages += delta;
  166. surplus_huge_pages_node[nid] += delta;
  167. ret = 1;
  168. break;
  169. } while (nid != prev_nid);
  170. prev_nid = nid;
  171. return ret;
  172. }
  173. static struct page *alloc_fresh_huge_page_node(int nid)
  174. {
  175. struct page *page;
  176. page = alloc_pages_node(nid,
  177. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|__GFP_NOWARN,
  178. HUGETLB_PAGE_ORDER);
  179. if (page) {
  180. set_compound_page_dtor(page, free_huge_page);
  181. spin_lock(&hugetlb_lock);
  182. nr_huge_pages++;
  183. nr_huge_pages_node[nid]++;
  184. spin_unlock(&hugetlb_lock);
  185. put_page(page); /* free it into the hugepage allocator */
  186. }
  187. return page;
  188. }
  189. static int alloc_fresh_huge_page(void)
  190. {
  191. struct page *page;
  192. int start_nid;
  193. int next_nid;
  194. int ret = 0;
  195. start_nid = hugetlb_next_nid;
  196. do {
  197. page = alloc_fresh_huge_page_node(hugetlb_next_nid);
  198. if (page)
  199. ret = 1;
  200. /*
  201. * Use a helper variable to find the next node and then
  202. * copy it back to hugetlb_next_nid afterwards:
  203. * otherwise there's a window in which a racer might
  204. * pass invalid nid MAX_NUMNODES to alloc_pages_node.
  205. * But we don't need to use a spin_lock here: it really
  206. * doesn't matter if occasionally a racer chooses the
  207. * same nid as we do. Move nid forward in the mask even
  208. * if we just successfully allocated a hugepage so that
  209. * the next caller gets hugepages on the next node.
  210. */
  211. next_nid = next_node(hugetlb_next_nid, node_online_map);
  212. if (next_nid == MAX_NUMNODES)
  213. next_nid = first_node(node_online_map);
  214. hugetlb_next_nid = next_nid;
  215. } while (!page && hugetlb_next_nid != start_nid);
  216. return ret;
  217. }
  218. static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma,
  219. unsigned long address)
  220. {
  221. struct page *page;
  222. unsigned int nid;
  223. /*
  224. * Assume we will successfully allocate the surplus page to
  225. * prevent racing processes from causing the surplus to exceed
  226. * overcommit
  227. *
  228. * This however introduces a different race, where a process B
  229. * tries to grow the static hugepage pool while alloc_pages() is
  230. * called by process A. B will only examine the per-node
  231. * counters in determining if surplus huge pages can be
  232. * converted to normal huge pages in adjust_pool_surplus(). A
  233. * won't be able to increment the per-node counter, until the
  234. * lock is dropped by B, but B doesn't drop hugetlb_lock until
  235. * no more huge pages can be converted from surplus to normal
  236. * state (and doesn't try to convert again). Thus, we have a
  237. * case where a surplus huge page exists, the pool is grown, and
  238. * the surplus huge page still exists after, even though it
  239. * should just have been converted to a normal huge page. This
  240. * does not leak memory, though, as the hugepage will be freed
  241. * once it is out of use. It also does not allow the counters to
  242. * go out of whack in adjust_pool_surplus() as we don't modify
  243. * the node values until we've gotten the hugepage and only the
  244. * per-node value is checked there.
  245. */
  246. spin_lock(&hugetlb_lock);
  247. if (surplus_huge_pages >= nr_overcommit_huge_pages) {
  248. spin_unlock(&hugetlb_lock);
  249. return NULL;
  250. } else {
  251. nr_huge_pages++;
  252. surplus_huge_pages++;
  253. }
  254. spin_unlock(&hugetlb_lock);
  255. page = alloc_pages(htlb_alloc_mask|__GFP_COMP|__GFP_NOWARN,
  256. HUGETLB_PAGE_ORDER);
  257. spin_lock(&hugetlb_lock);
  258. if (page) {
  259. /*
  260. * This page is now managed by the hugetlb allocator and has
  261. * no users -- drop the buddy allocator's reference.
  262. */
  263. put_page_testzero(page);
  264. VM_BUG_ON(page_count(page));
  265. nid = page_to_nid(page);
  266. set_compound_page_dtor(page, free_huge_page);
  267. /*
  268. * We incremented the global counters already
  269. */
  270. nr_huge_pages_node[nid]++;
  271. surplus_huge_pages_node[nid]++;
  272. } else {
  273. nr_huge_pages--;
  274. surplus_huge_pages--;
  275. }
  276. spin_unlock(&hugetlb_lock);
  277. return page;
  278. }
  279. /*
  280. * Increase the hugetlb pool such that it can accomodate a reservation
  281. * of size 'delta'.
  282. */
  283. static int gather_surplus_pages(int delta)
  284. {
  285. struct list_head surplus_list;
  286. struct page *page, *tmp;
  287. int ret, i;
  288. int needed, allocated;
  289. needed = (resv_huge_pages + delta) - free_huge_pages;
  290. if (needed <= 0) {
  291. resv_huge_pages += delta;
  292. return 0;
  293. }
  294. allocated = 0;
  295. INIT_LIST_HEAD(&surplus_list);
  296. ret = -ENOMEM;
  297. retry:
  298. spin_unlock(&hugetlb_lock);
  299. for (i = 0; i < needed; i++) {
  300. page = alloc_buddy_huge_page(NULL, 0);
  301. if (!page) {
  302. /*
  303. * We were not able to allocate enough pages to
  304. * satisfy the entire reservation so we free what
  305. * we've allocated so far.
  306. */
  307. spin_lock(&hugetlb_lock);
  308. needed = 0;
  309. goto free;
  310. }
  311. list_add(&page->lru, &surplus_list);
  312. }
  313. allocated += needed;
  314. /*
  315. * After retaking hugetlb_lock, we need to recalculate 'needed'
  316. * because either resv_huge_pages or free_huge_pages may have changed.
  317. */
  318. spin_lock(&hugetlb_lock);
  319. needed = (resv_huge_pages + delta) - (free_huge_pages + allocated);
  320. if (needed > 0)
  321. goto retry;
  322. /*
  323. * The surplus_list now contains _at_least_ the number of extra pages
  324. * needed to accomodate the reservation. Add the appropriate number
  325. * of pages to the hugetlb pool and free the extras back to the buddy
  326. * allocator. Commit the entire reservation here to prevent another
  327. * process from stealing the pages as they are added to the pool but
  328. * before they are reserved.
  329. */
  330. needed += allocated;
  331. resv_huge_pages += delta;
  332. ret = 0;
  333. free:
  334. /* Free the needed pages to the hugetlb pool */
  335. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  336. if ((--needed) < 0)
  337. break;
  338. list_del(&page->lru);
  339. enqueue_huge_page(page);
  340. }
  341. /* Free unnecessary surplus pages to the buddy allocator */
  342. if (!list_empty(&surplus_list)) {
  343. spin_unlock(&hugetlb_lock);
  344. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  345. list_del(&page->lru);
  346. /*
  347. * The page has a reference count of zero already, so
  348. * call free_huge_page directly instead of using
  349. * put_page. This must be done with hugetlb_lock
  350. * unlocked which is safe because free_huge_page takes
  351. * hugetlb_lock before deciding how to free the page.
  352. */
  353. free_huge_page(page);
  354. }
  355. spin_lock(&hugetlb_lock);
  356. }
  357. return ret;
  358. }
  359. /*
  360. * When releasing a hugetlb pool reservation, any surplus pages that were
  361. * allocated to satisfy the reservation must be explicitly freed if they were
  362. * never used.
  363. */
  364. static void return_unused_surplus_pages(unsigned long unused_resv_pages)
  365. {
  366. static int nid = -1;
  367. struct page *page;
  368. unsigned long nr_pages;
  369. /*
  370. * We want to release as many surplus pages as possible, spread
  371. * evenly across all nodes. Iterate across all nodes until we
  372. * can no longer free unreserved surplus pages. This occurs when
  373. * the nodes with surplus pages have no free pages.
  374. */
  375. unsigned long remaining_iterations = num_online_nodes();
  376. /* Uncommit the reservation */
  377. resv_huge_pages -= unused_resv_pages;
  378. nr_pages = min(unused_resv_pages, surplus_huge_pages);
  379. while (remaining_iterations-- && nr_pages) {
  380. nid = next_node(nid, node_online_map);
  381. if (nid == MAX_NUMNODES)
  382. nid = first_node(node_online_map);
  383. if (!surplus_huge_pages_node[nid])
  384. continue;
  385. if (!list_empty(&hugepage_freelists[nid])) {
  386. page = list_entry(hugepage_freelists[nid].next,
  387. struct page, lru);
  388. list_del(&page->lru);
  389. update_and_free_page(page);
  390. free_huge_pages--;
  391. free_huge_pages_node[nid]--;
  392. surplus_huge_pages--;
  393. surplus_huge_pages_node[nid]--;
  394. nr_pages--;
  395. remaining_iterations = num_online_nodes();
  396. }
  397. }
  398. }
  399. static struct page *alloc_huge_page_shared(struct vm_area_struct *vma,
  400. unsigned long addr)
  401. {
  402. struct page *page;
  403. spin_lock(&hugetlb_lock);
  404. page = dequeue_huge_page_vma(vma, addr);
  405. spin_unlock(&hugetlb_lock);
  406. return page ? page : ERR_PTR(-VM_FAULT_OOM);
  407. }
  408. static struct page *alloc_huge_page_private(struct vm_area_struct *vma,
  409. unsigned long addr)
  410. {
  411. struct page *page = NULL;
  412. if (hugetlb_get_quota(vma->vm_file->f_mapping, 1))
  413. return ERR_PTR(-VM_FAULT_SIGBUS);
  414. spin_lock(&hugetlb_lock);
  415. if (free_huge_pages > resv_huge_pages)
  416. page = dequeue_huge_page_vma(vma, addr);
  417. spin_unlock(&hugetlb_lock);
  418. if (!page) {
  419. page = alloc_buddy_huge_page(vma, addr);
  420. if (!page) {
  421. hugetlb_put_quota(vma->vm_file->f_mapping, 1);
  422. return ERR_PTR(-VM_FAULT_OOM);
  423. }
  424. }
  425. return page;
  426. }
  427. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  428. unsigned long addr)
  429. {
  430. struct page *page;
  431. struct address_space *mapping = vma->vm_file->f_mapping;
  432. if (vma->vm_flags & VM_MAYSHARE)
  433. page = alloc_huge_page_shared(vma, addr);
  434. else
  435. page = alloc_huge_page_private(vma, addr);
  436. if (!IS_ERR(page)) {
  437. set_page_refcounted(page);
  438. set_page_private(page, (unsigned long) mapping);
  439. }
  440. return page;
  441. }
  442. static int __init hugetlb_init(void)
  443. {
  444. unsigned long i;
  445. if (HPAGE_SHIFT == 0)
  446. return 0;
  447. for (i = 0; i < MAX_NUMNODES; ++i)
  448. INIT_LIST_HEAD(&hugepage_freelists[i]);
  449. hugetlb_next_nid = first_node(node_online_map);
  450. for (i = 0; i < max_huge_pages; ++i) {
  451. if (!alloc_fresh_huge_page())
  452. break;
  453. }
  454. max_huge_pages = free_huge_pages = nr_huge_pages = i;
  455. printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
  456. return 0;
  457. }
  458. module_init(hugetlb_init);
  459. static int __init hugetlb_setup(char *s)
  460. {
  461. if (sscanf(s, "%lu", &max_huge_pages) <= 0)
  462. max_huge_pages = 0;
  463. return 1;
  464. }
  465. __setup("hugepages=", hugetlb_setup);
  466. static unsigned int cpuset_mems_nr(unsigned int *array)
  467. {
  468. int node;
  469. unsigned int nr = 0;
  470. for_each_node_mask(node, cpuset_current_mems_allowed)
  471. nr += array[node];
  472. return nr;
  473. }
  474. #ifdef CONFIG_SYSCTL
  475. #ifdef CONFIG_HIGHMEM
  476. static void try_to_free_low(unsigned long count)
  477. {
  478. int i;
  479. for (i = 0; i < MAX_NUMNODES; ++i) {
  480. struct page *page, *next;
  481. list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
  482. if (count >= nr_huge_pages)
  483. return;
  484. if (PageHighMem(page))
  485. continue;
  486. list_del(&page->lru);
  487. update_and_free_page(page);
  488. free_huge_pages--;
  489. free_huge_pages_node[page_to_nid(page)]--;
  490. }
  491. }
  492. }
  493. #else
  494. static inline void try_to_free_low(unsigned long count)
  495. {
  496. }
  497. #endif
  498. #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
  499. static unsigned long set_max_huge_pages(unsigned long count)
  500. {
  501. unsigned long min_count, ret;
  502. /*
  503. * Increase the pool size
  504. * First take pages out of surplus state. Then make up the
  505. * remaining difference by allocating fresh huge pages.
  506. *
  507. * We might race with alloc_buddy_huge_page() here and be unable
  508. * to convert a surplus huge page to a normal huge page. That is
  509. * not critical, though, it just means the overall size of the
  510. * pool might be one hugepage larger than it needs to be, but
  511. * within all the constraints specified by the sysctls.
  512. */
  513. spin_lock(&hugetlb_lock);
  514. while (surplus_huge_pages && count > persistent_huge_pages) {
  515. if (!adjust_pool_surplus(-1))
  516. break;
  517. }
  518. while (count > persistent_huge_pages) {
  519. int ret;
  520. /*
  521. * If this allocation races such that we no longer need the
  522. * page, free_huge_page will handle it by freeing the page
  523. * and reducing the surplus.
  524. */
  525. spin_unlock(&hugetlb_lock);
  526. ret = alloc_fresh_huge_page();
  527. spin_lock(&hugetlb_lock);
  528. if (!ret)
  529. goto out;
  530. }
  531. /*
  532. * Decrease the pool size
  533. * First return free pages to the buddy allocator (being careful
  534. * to keep enough around to satisfy reservations). Then place
  535. * pages into surplus state as needed so the pool will shrink
  536. * to the desired size as pages become free.
  537. *
  538. * By placing pages into the surplus state independent of the
  539. * overcommit value, we are allowing the surplus pool size to
  540. * exceed overcommit. There are few sane options here. Since
  541. * alloc_buddy_huge_page() is checking the global counter,
  542. * though, we'll note that we're not allowed to exceed surplus
  543. * and won't grow the pool anywhere else. Not until one of the
  544. * sysctls are changed, or the surplus pages go out of use.
  545. */
  546. min_count = resv_huge_pages + nr_huge_pages - free_huge_pages;
  547. min_count = max(count, min_count);
  548. try_to_free_low(min_count);
  549. while (min_count < persistent_huge_pages) {
  550. struct page *page = dequeue_huge_page();
  551. if (!page)
  552. break;
  553. update_and_free_page(page);
  554. }
  555. while (count < persistent_huge_pages) {
  556. if (!adjust_pool_surplus(1))
  557. break;
  558. }
  559. out:
  560. ret = persistent_huge_pages;
  561. spin_unlock(&hugetlb_lock);
  562. return ret;
  563. }
  564. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  565. struct file *file, void __user *buffer,
  566. size_t *length, loff_t *ppos)
  567. {
  568. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  569. max_huge_pages = set_max_huge_pages(max_huge_pages);
  570. return 0;
  571. }
  572. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  573. struct file *file, void __user *buffer,
  574. size_t *length, loff_t *ppos)
  575. {
  576. proc_dointvec(table, write, file, buffer, length, ppos);
  577. if (hugepages_treat_as_movable)
  578. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  579. else
  580. htlb_alloc_mask = GFP_HIGHUSER;
  581. return 0;
  582. }
  583. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  584. struct file *file, void __user *buffer,
  585. size_t *length, loff_t *ppos)
  586. {
  587. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  588. spin_lock(&hugetlb_lock);
  589. nr_overcommit_huge_pages = sysctl_overcommit_huge_pages;
  590. spin_unlock(&hugetlb_lock);
  591. return 0;
  592. }
  593. #endif /* CONFIG_SYSCTL */
  594. int hugetlb_report_meminfo(char *buf)
  595. {
  596. return sprintf(buf,
  597. "HugePages_Total: %5lu\n"
  598. "HugePages_Free: %5lu\n"
  599. "HugePages_Rsvd: %5lu\n"
  600. "HugePages_Surp: %5lu\n"
  601. "Hugepagesize: %5lu kB\n",
  602. nr_huge_pages,
  603. free_huge_pages,
  604. resv_huge_pages,
  605. surplus_huge_pages,
  606. HPAGE_SIZE/1024);
  607. }
  608. int hugetlb_report_node_meminfo(int nid, char *buf)
  609. {
  610. return sprintf(buf,
  611. "Node %d HugePages_Total: %5u\n"
  612. "Node %d HugePages_Free: %5u\n"
  613. "Node %d HugePages_Surp: %5u\n",
  614. nid, nr_huge_pages_node[nid],
  615. nid, free_huge_pages_node[nid],
  616. nid, surplus_huge_pages_node[nid]);
  617. }
  618. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  619. unsigned long hugetlb_total_pages(void)
  620. {
  621. return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
  622. }
  623. /*
  624. * We cannot handle pagefaults against hugetlb pages at all. They cause
  625. * handle_mm_fault() to try to instantiate regular-sized pages in the
  626. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  627. * this far.
  628. */
  629. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  630. {
  631. BUG();
  632. return 0;
  633. }
  634. struct vm_operations_struct hugetlb_vm_ops = {
  635. .fault = hugetlb_vm_op_fault,
  636. };
  637. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  638. int writable)
  639. {
  640. pte_t entry;
  641. if (writable) {
  642. entry =
  643. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  644. } else {
  645. entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  646. }
  647. entry = pte_mkyoung(entry);
  648. entry = pte_mkhuge(entry);
  649. return entry;
  650. }
  651. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  652. unsigned long address, pte_t *ptep)
  653. {
  654. pte_t entry;
  655. entry = pte_mkwrite(pte_mkdirty(*ptep));
  656. if (ptep_set_access_flags(vma, address, ptep, entry, 1)) {
  657. update_mmu_cache(vma, address, entry);
  658. }
  659. }
  660. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  661. struct vm_area_struct *vma)
  662. {
  663. pte_t *src_pte, *dst_pte, entry;
  664. struct page *ptepage;
  665. unsigned long addr;
  666. int cow;
  667. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  668. for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
  669. src_pte = huge_pte_offset(src, addr);
  670. if (!src_pte)
  671. continue;
  672. dst_pte = huge_pte_alloc(dst, addr);
  673. if (!dst_pte)
  674. goto nomem;
  675. /* If the pagetables are shared don't copy or take references */
  676. if (dst_pte == src_pte)
  677. continue;
  678. spin_lock(&dst->page_table_lock);
  679. spin_lock(&src->page_table_lock);
  680. if (!pte_none(*src_pte)) {
  681. if (cow)
  682. ptep_set_wrprotect(src, addr, src_pte);
  683. entry = *src_pte;
  684. ptepage = pte_page(entry);
  685. get_page(ptepage);
  686. set_huge_pte_at(dst, addr, dst_pte, entry);
  687. }
  688. spin_unlock(&src->page_table_lock);
  689. spin_unlock(&dst->page_table_lock);
  690. }
  691. return 0;
  692. nomem:
  693. return -ENOMEM;
  694. }
  695. void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  696. unsigned long end)
  697. {
  698. struct mm_struct *mm = vma->vm_mm;
  699. unsigned long address;
  700. pte_t *ptep;
  701. pte_t pte;
  702. struct page *page;
  703. struct page *tmp;
  704. /*
  705. * A page gathering list, protected by per file i_mmap_lock. The
  706. * lock is used to avoid list corruption from multiple unmapping
  707. * of the same page since we are using page->lru.
  708. */
  709. LIST_HEAD(page_list);
  710. WARN_ON(!is_vm_hugetlb_page(vma));
  711. BUG_ON(start & ~HPAGE_MASK);
  712. BUG_ON(end & ~HPAGE_MASK);
  713. spin_lock(&mm->page_table_lock);
  714. for (address = start; address < end; address += HPAGE_SIZE) {
  715. ptep = huge_pte_offset(mm, address);
  716. if (!ptep)
  717. continue;
  718. if (huge_pmd_unshare(mm, &address, ptep))
  719. continue;
  720. pte = huge_ptep_get_and_clear(mm, address, ptep);
  721. if (pte_none(pte))
  722. continue;
  723. page = pte_page(pte);
  724. if (pte_dirty(pte))
  725. set_page_dirty(page);
  726. list_add(&page->lru, &page_list);
  727. }
  728. spin_unlock(&mm->page_table_lock);
  729. flush_tlb_range(vma, start, end);
  730. list_for_each_entry_safe(page, tmp, &page_list, lru) {
  731. list_del(&page->lru);
  732. put_page(page);
  733. }
  734. }
  735. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  736. unsigned long end)
  737. {
  738. /*
  739. * It is undesirable to test vma->vm_file as it should be non-null
  740. * for valid hugetlb area. However, vm_file will be NULL in the error
  741. * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
  742. * do_mmap_pgoff() nullifies vma->vm_file before calling this function
  743. * to clean up. Since no pte has actually been setup, it is safe to
  744. * do nothing in this case.
  745. */
  746. if (vma->vm_file) {
  747. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  748. __unmap_hugepage_range(vma, start, end);
  749. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  750. }
  751. }
  752. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  753. unsigned long address, pte_t *ptep, pte_t pte)
  754. {
  755. struct page *old_page, *new_page;
  756. int avoidcopy;
  757. old_page = pte_page(pte);
  758. /* If no-one else is actually using this page, avoid the copy
  759. * and just make the page writable */
  760. avoidcopy = (page_count(old_page) == 1);
  761. if (avoidcopy) {
  762. set_huge_ptep_writable(vma, address, ptep);
  763. return 0;
  764. }
  765. page_cache_get(old_page);
  766. new_page = alloc_huge_page(vma, address);
  767. if (IS_ERR(new_page)) {
  768. page_cache_release(old_page);
  769. return -PTR_ERR(new_page);
  770. }
  771. spin_unlock(&mm->page_table_lock);
  772. copy_huge_page(new_page, old_page, address, vma);
  773. __SetPageUptodate(new_page);
  774. spin_lock(&mm->page_table_lock);
  775. ptep = huge_pte_offset(mm, address & HPAGE_MASK);
  776. if (likely(pte_same(*ptep, pte))) {
  777. /* Break COW */
  778. set_huge_pte_at(mm, address, ptep,
  779. make_huge_pte(vma, new_page, 1));
  780. /* Make the old page be freed below */
  781. new_page = old_page;
  782. }
  783. page_cache_release(new_page);
  784. page_cache_release(old_page);
  785. return 0;
  786. }
  787. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  788. unsigned long address, pte_t *ptep, int write_access)
  789. {
  790. int ret = VM_FAULT_SIGBUS;
  791. unsigned long idx;
  792. unsigned long size;
  793. struct page *page;
  794. struct address_space *mapping;
  795. pte_t new_pte;
  796. mapping = vma->vm_file->f_mapping;
  797. idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
  798. + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
  799. /*
  800. * Use page lock to guard against racing truncation
  801. * before we get page_table_lock.
  802. */
  803. retry:
  804. page = find_lock_page(mapping, idx);
  805. if (!page) {
  806. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  807. if (idx >= size)
  808. goto out;
  809. page = alloc_huge_page(vma, address);
  810. if (IS_ERR(page)) {
  811. ret = -PTR_ERR(page);
  812. goto out;
  813. }
  814. clear_huge_page(page, address);
  815. __SetPageUptodate(page);
  816. if (vma->vm_flags & VM_SHARED) {
  817. int err;
  818. struct inode *inode = mapping->host;
  819. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  820. if (err) {
  821. put_page(page);
  822. if (err == -EEXIST)
  823. goto retry;
  824. goto out;
  825. }
  826. spin_lock(&inode->i_lock);
  827. inode->i_blocks += BLOCKS_PER_HUGEPAGE;
  828. spin_unlock(&inode->i_lock);
  829. } else
  830. lock_page(page);
  831. }
  832. spin_lock(&mm->page_table_lock);
  833. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  834. if (idx >= size)
  835. goto backout;
  836. ret = 0;
  837. if (!pte_none(*ptep))
  838. goto backout;
  839. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  840. && (vma->vm_flags & VM_SHARED)));
  841. set_huge_pte_at(mm, address, ptep, new_pte);
  842. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  843. /* Optimization, do the COW without a second fault */
  844. ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
  845. }
  846. spin_unlock(&mm->page_table_lock);
  847. unlock_page(page);
  848. out:
  849. return ret;
  850. backout:
  851. spin_unlock(&mm->page_table_lock);
  852. unlock_page(page);
  853. put_page(page);
  854. goto out;
  855. }
  856. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  857. unsigned long address, int write_access)
  858. {
  859. pte_t *ptep;
  860. pte_t entry;
  861. int ret;
  862. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  863. ptep = huge_pte_alloc(mm, address);
  864. if (!ptep)
  865. return VM_FAULT_OOM;
  866. /*
  867. * Serialize hugepage allocation and instantiation, so that we don't
  868. * get spurious allocation failures if two CPUs race to instantiate
  869. * the same page in the page cache.
  870. */
  871. mutex_lock(&hugetlb_instantiation_mutex);
  872. entry = *ptep;
  873. if (pte_none(entry)) {
  874. ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
  875. mutex_unlock(&hugetlb_instantiation_mutex);
  876. return ret;
  877. }
  878. ret = 0;
  879. spin_lock(&mm->page_table_lock);
  880. /* Check for a racing update before calling hugetlb_cow */
  881. if (likely(pte_same(entry, *ptep)))
  882. if (write_access && !pte_write(entry))
  883. ret = hugetlb_cow(mm, vma, address, ptep, entry);
  884. spin_unlock(&mm->page_table_lock);
  885. mutex_unlock(&hugetlb_instantiation_mutex);
  886. return ret;
  887. }
  888. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  889. struct page **pages, struct vm_area_struct **vmas,
  890. unsigned long *position, int *length, int i,
  891. int write)
  892. {
  893. unsigned long pfn_offset;
  894. unsigned long vaddr = *position;
  895. int remainder = *length;
  896. spin_lock(&mm->page_table_lock);
  897. while (vaddr < vma->vm_end && remainder) {
  898. pte_t *pte;
  899. struct page *page;
  900. /*
  901. * Some archs (sparc64, sh*) have multiple pte_ts to
  902. * each hugepage. We have to make * sure we get the
  903. * first, for the page indexing below to work.
  904. */
  905. pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
  906. if (!pte || pte_none(*pte) || (write && !pte_write(*pte))) {
  907. int ret;
  908. spin_unlock(&mm->page_table_lock);
  909. ret = hugetlb_fault(mm, vma, vaddr, write);
  910. spin_lock(&mm->page_table_lock);
  911. if (!(ret & VM_FAULT_ERROR))
  912. continue;
  913. remainder = 0;
  914. if (!i)
  915. i = -EFAULT;
  916. break;
  917. }
  918. pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
  919. page = pte_page(*pte);
  920. same_page:
  921. if (pages) {
  922. get_page(page);
  923. pages[i] = page + pfn_offset;
  924. }
  925. if (vmas)
  926. vmas[i] = vma;
  927. vaddr += PAGE_SIZE;
  928. ++pfn_offset;
  929. --remainder;
  930. ++i;
  931. if (vaddr < vma->vm_end && remainder &&
  932. pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
  933. /*
  934. * We use pfn_offset to avoid touching the pageframes
  935. * of this compound page.
  936. */
  937. goto same_page;
  938. }
  939. }
  940. spin_unlock(&mm->page_table_lock);
  941. *length = remainder;
  942. *position = vaddr;
  943. return i;
  944. }
  945. void hugetlb_change_protection(struct vm_area_struct *vma,
  946. unsigned long address, unsigned long end, pgprot_t newprot)
  947. {
  948. struct mm_struct *mm = vma->vm_mm;
  949. unsigned long start = address;
  950. pte_t *ptep;
  951. pte_t pte;
  952. BUG_ON(address >= end);
  953. flush_cache_range(vma, address, end);
  954. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  955. spin_lock(&mm->page_table_lock);
  956. for (; address < end; address += HPAGE_SIZE) {
  957. ptep = huge_pte_offset(mm, address);
  958. if (!ptep)
  959. continue;
  960. if (huge_pmd_unshare(mm, &address, ptep))
  961. continue;
  962. if (!pte_none(*ptep)) {
  963. pte = huge_ptep_get_and_clear(mm, address, ptep);
  964. pte = pte_mkhuge(pte_modify(pte, newprot));
  965. set_huge_pte_at(mm, address, ptep, pte);
  966. }
  967. }
  968. spin_unlock(&mm->page_table_lock);
  969. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  970. flush_tlb_range(vma, start, end);
  971. }
  972. struct file_region {
  973. struct list_head link;
  974. long from;
  975. long to;
  976. };
  977. static long region_add(struct list_head *head, long f, long t)
  978. {
  979. struct file_region *rg, *nrg, *trg;
  980. /* Locate the region we are either in or before. */
  981. list_for_each_entry(rg, head, link)
  982. if (f <= rg->to)
  983. break;
  984. /* Round our left edge to the current segment if it encloses us. */
  985. if (f > rg->from)
  986. f = rg->from;
  987. /* Check for and consume any regions we now overlap with. */
  988. nrg = rg;
  989. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  990. if (&rg->link == head)
  991. break;
  992. if (rg->from > t)
  993. break;
  994. /* If this area reaches higher then extend our area to
  995. * include it completely. If this is not the first area
  996. * which we intend to reuse, free it. */
  997. if (rg->to > t)
  998. t = rg->to;
  999. if (rg != nrg) {
  1000. list_del(&rg->link);
  1001. kfree(rg);
  1002. }
  1003. }
  1004. nrg->from = f;
  1005. nrg->to = t;
  1006. return 0;
  1007. }
  1008. static long region_chg(struct list_head *head, long f, long t)
  1009. {
  1010. struct file_region *rg, *nrg;
  1011. long chg = 0;
  1012. /* Locate the region we are before or in. */
  1013. list_for_each_entry(rg, head, link)
  1014. if (f <= rg->to)
  1015. break;
  1016. /* If we are below the current region then a new region is required.
  1017. * Subtle, allocate a new region at the position but make it zero
  1018. * size such that we can guarantee to record the reservation. */
  1019. if (&rg->link == head || t < rg->from) {
  1020. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  1021. if (!nrg)
  1022. return -ENOMEM;
  1023. nrg->from = f;
  1024. nrg->to = f;
  1025. INIT_LIST_HEAD(&nrg->link);
  1026. list_add(&nrg->link, rg->link.prev);
  1027. return t - f;
  1028. }
  1029. /* Round our left edge to the current segment if it encloses us. */
  1030. if (f > rg->from)
  1031. f = rg->from;
  1032. chg = t - f;
  1033. /* Check for and consume any regions we now overlap with. */
  1034. list_for_each_entry(rg, rg->link.prev, link) {
  1035. if (&rg->link == head)
  1036. break;
  1037. if (rg->from > t)
  1038. return chg;
  1039. /* We overlap with this area, if it extends futher than
  1040. * us then we must extend ourselves. Account for its
  1041. * existing reservation. */
  1042. if (rg->to > t) {
  1043. chg += rg->to - t;
  1044. t = rg->to;
  1045. }
  1046. chg -= rg->to - rg->from;
  1047. }
  1048. return chg;
  1049. }
  1050. static long region_truncate(struct list_head *head, long end)
  1051. {
  1052. struct file_region *rg, *trg;
  1053. long chg = 0;
  1054. /* Locate the region we are either in or before. */
  1055. list_for_each_entry(rg, head, link)
  1056. if (end <= rg->to)
  1057. break;
  1058. if (&rg->link == head)
  1059. return 0;
  1060. /* If we are in the middle of a region then adjust it. */
  1061. if (end > rg->from) {
  1062. chg = rg->to - end;
  1063. rg->to = end;
  1064. rg = list_entry(rg->link.next, typeof(*rg), link);
  1065. }
  1066. /* Drop any remaining regions. */
  1067. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  1068. if (&rg->link == head)
  1069. break;
  1070. chg += rg->to - rg->from;
  1071. list_del(&rg->link);
  1072. kfree(rg);
  1073. }
  1074. return chg;
  1075. }
  1076. static int hugetlb_acct_memory(long delta)
  1077. {
  1078. int ret = -ENOMEM;
  1079. spin_lock(&hugetlb_lock);
  1080. /*
  1081. * When cpuset is configured, it breaks the strict hugetlb page
  1082. * reservation as the accounting is done on a global variable. Such
  1083. * reservation is completely rubbish in the presence of cpuset because
  1084. * the reservation is not checked against page availability for the
  1085. * current cpuset. Application can still potentially OOM'ed by kernel
  1086. * with lack of free htlb page in cpuset that the task is in.
  1087. * Attempt to enforce strict accounting with cpuset is almost
  1088. * impossible (or too ugly) because cpuset is too fluid that
  1089. * task or memory node can be dynamically moved between cpusets.
  1090. *
  1091. * The change of semantics for shared hugetlb mapping with cpuset is
  1092. * undesirable. However, in order to preserve some of the semantics,
  1093. * we fall back to check against current free page availability as
  1094. * a best attempt and hopefully to minimize the impact of changing
  1095. * semantics that cpuset has.
  1096. */
  1097. if (delta > 0) {
  1098. if (gather_surplus_pages(delta) < 0)
  1099. goto out;
  1100. if (delta > cpuset_mems_nr(free_huge_pages_node)) {
  1101. return_unused_surplus_pages(delta);
  1102. goto out;
  1103. }
  1104. }
  1105. ret = 0;
  1106. if (delta < 0)
  1107. return_unused_surplus_pages((unsigned long) -delta);
  1108. out:
  1109. spin_unlock(&hugetlb_lock);
  1110. return ret;
  1111. }
  1112. int hugetlb_reserve_pages(struct inode *inode, long from, long to)
  1113. {
  1114. long ret, chg;
  1115. chg = region_chg(&inode->i_mapping->private_list, from, to);
  1116. if (chg < 0)
  1117. return chg;
  1118. if (hugetlb_get_quota(inode->i_mapping, chg))
  1119. return -ENOSPC;
  1120. ret = hugetlb_acct_memory(chg);
  1121. if (ret < 0) {
  1122. hugetlb_put_quota(inode->i_mapping, chg);
  1123. return ret;
  1124. }
  1125. region_add(&inode->i_mapping->private_list, from, to);
  1126. return 0;
  1127. }
  1128. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  1129. {
  1130. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  1131. spin_lock(&inode->i_lock);
  1132. inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed;
  1133. spin_unlock(&inode->i_lock);
  1134. hugetlb_put_quota(inode->i_mapping, (chg - freed));
  1135. hugetlb_acct_memory(-(chg - freed));
  1136. }