hugetlb.c 28 KB

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