hugetlb.c 31 KB

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