hugetlb.c 32 KB

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