hugetlb.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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_shared(struct vm_area_struct *vma,
  312. unsigned long addr)
  313. {
  314. struct page *page;
  315. spin_lock(&hugetlb_lock);
  316. page = dequeue_huge_page(vma, addr);
  317. spin_unlock(&hugetlb_lock);
  318. return page;
  319. }
  320. static struct page *alloc_huge_page_private(struct vm_area_struct *vma,
  321. unsigned long addr)
  322. {
  323. struct page *page = NULL;
  324. spin_lock(&hugetlb_lock);
  325. if (free_huge_pages > resv_huge_pages)
  326. page = dequeue_huge_page(vma, addr);
  327. spin_unlock(&hugetlb_lock);
  328. if (!page)
  329. page = alloc_buddy_huge_page(vma, addr);
  330. return page;
  331. }
  332. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  333. unsigned long addr)
  334. {
  335. struct page *page;
  336. if (vma->vm_flags & VM_MAYSHARE)
  337. page = alloc_huge_page_shared(vma, addr);
  338. else
  339. page = alloc_huge_page_private(vma, addr);
  340. if (page)
  341. set_page_refcounted(page);
  342. return page;
  343. }
  344. static int __init hugetlb_init(void)
  345. {
  346. unsigned long i;
  347. if (HPAGE_SHIFT == 0)
  348. return 0;
  349. for (i = 0; i < MAX_NUMNODES; ++i)
  350. INIT_LIST_HEAD(&hugepage_freelists[i]);
  351. hugetlb_next_nid = first_node(node_online_map);
  352. for (i = 0; i < max_huge_pages; ++i) {
  353. if (!alloc_fresh_huge_page())
  354. break;
  355. }
  356. max_huge_pages = free_huge_pages = nr_huge_pages = i;
  357. printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
  358. return 0;
  359. }
  360. module_init(hugetlb_init);
  361. static int __init hugetlb_setup(char *s)
  362. {
  363. if (sscanf(s, "%lu", &max_huge_pages) <= 0)
  364. max_huge_pages = 0;
  365. return 1;
  366. }
  367. __setup("hugepages=", hugetlb_setup);
  368. static unsigned int cpuset_mems_nr(unsigned int *array)
  369. {
  370. int node;
  371. unsigned int nr = 0;
  372. for_each_node_mask(node, cpuset_current_mems_allowed)
  373. nr += array[node];
  374. return nr;
  375. }
  376. #ifdef CONFIG_SYSCTL
  377. #ifdef CONFIG_HIGHMEM
  378. static void try_to_free_low(unsigned long count)
  379. {
  380. int i;
  381. for (i = 0; i < MAX_NUMNODES; ++i) {
  382. struct page *page, *next;
  383. list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
  384. if (count >= nr_huge_pages)
  385. return;
  386. if (PageHighMem(page))
  387. continue;
  388. list_del(&page->lru);
  389. update_and_free_page(page);
  390. free_huge_pages--;
  391. free_huge_pages_node[page_to_nid(page)]--;
  392. }
  393. }
  394. }
  395. #else
  396. static inline void try_to_free_low(unsigned long count)
  397. {
  398. }
  399. #endif
  400. #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
  401. static unsigned long set_max_huge_pages(unsigned long count)
  402. {
  403. unsigned long min_count, ret;
  404. /*
  405. * Increase the pool size
  406. * First take pages out of surplus state. Then make up the
  407. * remaining difference by allocating fresh huge pages.
  408. */
  409. spin_lock(&hugetlb_lock);
  410. while (surplus_huge_pages && count > persistent_huge_pages) {
  411. if (!adjust_pool_surplus(-1))
  412. break;
  413. }
  414. while (count > persistent_huge_pages) {
  415. int ret;
  416. /*
  417. * If this allocation races such that we no longer need the
  418. * page, free_huge_page will handle it by freeing the page
  419. * and reducing the surplus.
  420. */
  421. spin_unlock(&hugetlb_lock);
  422. ret = alloc_fresh_huge_page();
  423. spin_lock(&hugetlb_lock);
  424. if (!ret)
  425. goto out;
  426. }
  427. /*
  428. * Decrease the pool size
  429. * First return free pages to the buddy allocator (being careful
  430. * to keep enough around to satisfy reservations). Then place
  431. * pages into surplus state as needed so the pool will shrink
  432. * to the desired size as pages become free.
  433. */
  434. min_count = resv_huge_pages + nr_huge_pages - free_huge_pages;
  435. min_count = max(count, min_count);
  436. try_to_free_low(min_count);
  437. while (min_count < persistent_huge_pages) {
  438. struct page *page = dequeue_huge_page(NULL, 0);
  439. if (!page)
  440. break;
  441. update_and_free_page(page);
  442. }
  443. while (count < persistent_huge_pages) {
  444. if (!adjust_pool_surplus(1))
  445. break;
  446. }
  447. out:
  448. ret = persistent_huge_pages;
  449. spin_unlock(&hugetlb_lock);
  450. return ret;
  451. }
  452. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  453. struct file *file, void __user *buffer,
  454. size_t *length, loff_t *ppos)
  455. {
  456. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  457. max_huge_pages = set_max_huge_pages(max_huge_pages);
  458. return 0;
  459. }
  460. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  461. struct file *file, void __user *buffer,
  462. size_t *length, loff_t *ppos)
  463. {
  464. proc_dointvec(table, write, file, buffer, length, ppos);
  465. if (hugepages_treat_as_movable)
  466. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  467. else
  468. htlb_alloc_mask = GFP_HIGHUSER;
  469. return 0;
  470. }
  471. #endif /* CONFIG_SYSCTL */
  472. int hugetlb_report_meminfo(char *buf)
  473. {
  474. return sprintf(buf,
  475. "HugePages_Total: %5lu\n"
  476. "HugePages_Free: %5lu\n"
  477. "HugePages_Rsvd: %5lu\n"
  478. "HugePages_Surp: %5lu\n"
  479. "Hugepagesize: %5lu kB\n",
  480. nr_huge_pages,
  481. free_huge_pages,
  482. resv_huge_pages,
  483. surplus_huge_pages,
  484. HPAGE_SIZE/1024);
  485. }
  486. int hugetlb_report_node_meminfo(int nid, char *buf)
  487. {
  488. return sprintf(buf,
  489. "Node %d HugePages_Total: %5u\n"
  490. "Node %d HugePages_Free: %5u\n",
  491. nid, nr_huge_pages_node[nid],
  492. nid, free_huge_pages_node[nid]);
  493. }
  494. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  495. unsigned long hugetlb_total_pages(void)
  496. {
  497. return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
  498. }
  499. /*
  500. * We cannot handle pagefaults against hugetlb pages at all. They cause
  501. * handle_mm_fault() to try to instantiate regular-sized pages in the
  502. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  503. * this far.
  504. */
  505. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  506. {
  507. BUG();
  508. return 0;
  509. }
  510. struct vm_operations_struct hugetlb_vm_ops = {
  511. .fault = hugetlb_vm_op_fault,
  512. };
  513. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  514. int writable)
  515. {
  516. pte_t entry;
  517. if (writable) {
  518. entry =
  519. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  520. } else {
  521. entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  522. }
  523. entry = pte_mkyoung(entry);
  524. entry = pte_mkhuge(entry);
  525. return entry;
  526. }
  527. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  528. unsigned long address, pte_t *ptep)
  529. {
  530. pte_t entry;
  531. entry = pte_mkwrite(pte_mkdirty(*ptep));
  532. if (ptep_set_access_flags(vma, address, ptep, entry, 1)) {
  533. update_mmu_cache(vma, address, entry);
  534. }
  535. }
  536. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  537. struct vm_area_struct *vma)
  538. {
  539. pte_t *src_pte, *dst_pte, entry;
  540. struct page *ptepage;
  541. unsigned long addr;
  542. int cow;
  543. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  544. for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
  545. src_pte = huge_pte_offset(src, addr);
  546. if (!src_pte)
  547. continue;
  548. dst_pte = huge_pte_alloc(dst, addr);
  549. if (!dst_pte)
  550. goto nomem;
  551. spin_lock(&dst->page_table_lock);
  552. spin_lock(&src->page_table_lock);
  553. if (!pte_none(*src_pte)) {
  554. if (cow)
  555. ptep_set_wrprotect(src, addr, src_pte);
  556. entry = *src_pte;
  557. ptepage = pte_page(entry);
  558. get_page(ptepage);
  559. set_huge_pte_at(dst, addr, dst_pte, entry);
  560. }
  561. spin_unlock(&src->page_table_lock);
  562. spin_unlock(&dst->page_table_lock);
  563. }
  564. return 0;
  565. nomem:
  566. return -ENOMEM;
  567. }
  568. void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  569. unsigned long end)
  570. {
  571. struct mm_struct *mm = vma->vm_mm;
  572. unsigned long address;
  573. pte_t *ptep;
  574. pte_t pte;
  575. struct page *page;
  576. struct page *tmp;
  577. /*
  578. * A page gathering list, protected by per file i_mmap_lock. The
  579. * lock is used to avoid list corruption from multiple unmapping
  580. * of the same page since we are using page->lru.
  581. */
  582. LIST_HEAD(page_list);
  583. WARN_ON(!is_vm_hugetlb_page(vma));
  584. BUG_ON(start & ~HPAGE_MASK);
  585. BUG_ON(end & ~HPAGE_MASK);
  586. spin_lock(&mm->page_table_lock);
  587. for (address = start; address < end; address += HPAGE_SIZE) {
  588. ptep = huge_pte_offset(mm, address);
  589. if (!ptep)
  590. continue;
  591. if (huge_pmd_unshare(mm, &address, ptep))
  592. continue;
  593. pte = huge_ptep_get_and_clear(mm, address, ptep);
  594. if (pte_none(pte))
  595. continue;
  596. page = pte_page(pte);
  597. if (pte_dirty(pte))
  598. set_page_dirty(page);
  599. list_add(&page->lru, &page_list);
  600. }
  601. spin_unlock(&mm->page_table_lock);
  602. flush_tlb_range(vma, start, end);
  603. list_for_each_entry_safe(page, tmp, &page_list, lru) {
  604. list_del(&page->lru);
  605. put_page(page);
  606. }
  607. }
  608. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  609. unsigned long end)
  610. {
  611. /*
  612. * It is undesirable to test vma->vm_file as it should be non-null
  613. * for valid hugetlb area. However, vm_file will be NULL in the error
  614. * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
  615. * do_mmap_pgoff() nullifies vma->vm_file before calling this function
  616. * to clean up. Since no pte has actually been setup, it is safe to
  617. * do nothing in this case.
  618. */
  619. if (vma->vm_file) {
  620. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  621. __unmap_hugepage_range(vma, start, end);
  622. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  623. }
  624. }
  625. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  626. unsigned long address, pte_t *ptep, pte_t pte)
  627. {
  628. struct page *old_page, *new_page;
  629. int avoidcopy;
  630. old_page = pte_page(pte);
  631. /* If no-one else is actually using this page, avoid the copy
  632. * and just make the page writable */
  633. avoidcopy = (page_count(old_page) == 1);
  634. if (avoidcopy) {
  635. set_huge_ptep_writable(vma, address, ptep);
  636. return 0;
  637. }
  638. page_cache_get(old_page);
  639. new_page = alloc_huge_page(vma, address);
  640. if (!new_page) {
  641. page_cache_release(old_page);
  642. return VM_FAULT_OOM;
  643. }
  644. spin_unlock(&mm->page_table_lock);
  645. copy_huge_page(new_page, old_page, address, vma);
  646. spin_lock(&mm->page_table_lock);
  647. ptep = huge_pte_offset(mm, address & HPAGE_MASK);
  648. if (likely(pte_same(*ptep, pte))) {
  649. /* Break COW */
  650. set_huge_pte_at(mm, address, ptep,
  651. make_huge_pte(vma, new_page, 1));
  652. /* Make the old page be freed below */
  653. new_page = old_page;
  654. }
  655. page_cache_release(new_page);
  656. page_cache_release(old_page);
  657. return 0;
  658. }
  659. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  660. unsigned long address, pte_t *ptep, int write_access)
  661. {
  662. int ret = VM_FAULT_SIGBUS;
  663. unsigned long idx;
  664. unsigned long size;
  665. struct page *page;
  666. struct address_space *mapping;
  667. pte_t new_pte;
  668. mapping = vma->vm_file->f_mapping;
  669. idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
  670. + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
  671. /*
  672. * Use page lock to guard against racing truncation
  673. * before we get page_table_lock.
  674. */
  675. retry:
  676. page = find_lock_page(mapping, idx);
  677. if (!page) {
  678. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  679. if (idx >= size)
  680. goto out;
  681. if (hugetlb_get_quota(mapping))
  682. goto out;
  683. page = alloc_huge_page(vma, address);
  684. if (!page) {
  685. hugetlb_put_quota(mapping);
  686. ret = VM_FAULT_OOM;
  687. goto out;
  688. }
  689. clear_huge_page(page, address);
  690. if (vma->vm_flags & VM_SHARED) {
  691. int err;
  692. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  693. if (err) {
  694. put_page(page);
  695. hugetlb_put_quota(mapping);
  696. if (err == -EEXIST)
  697. goto retry;
  698. goto out;
  699. }
  700. } else
  701. lock_page(page);
  702. }
  703. spin_lock(&mm->page_table_lock);
  704. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  705. if (idx >= size)
  706. goto backout;
  707. ret = 0;
  708. if (!pte_none(*ptep))
  709. goto backout;
  710. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  711. && (vma->vm_flags & VM_SHARED)));
  712. set_huge_pte_at(mm, address, ptep, new_pte);
  713. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  714. /* Optimization, do the COW without a second fault */
  715. ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
  716. }
  717. spin_unlock(&mm->page_table_lock);
  718. unlock_page(page);
  719. out:
  720. return ret;
  721. backout:
  722. spin_unlock(&mm->page_table_lock);
  723. hugetlb_put_quota(mapping);
  724. unlock_page(page);
  725. put_page(page);
  726. goto out;
  727. }
  728. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  729. unsigned long address, int write_access)
  730. {
  731. pte_t *ptep;
  732. pte_t entry;
  733. int ret;
  734. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  735. ptep = huge_pte_alloc(mm, address);
  736. if (!ptep)
  737. return VM_FAULT_OOM;
  738. /*
  739. * Serialize hugepage allocation and instantiation, so that we don't
  740. * get spurious allocation failures if two CPUs race to instantiate
  741. * the same page in the page cache.
  742. */
  743. mutex_lock(&hugetlb_instantiation_mutex);
  744. entry = *ptep;
  745. if (pte_none(entry)) {
  746. ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
  747. mutex_unlock(&hugetlb_instantiation_mutex);
  748. return ret;
  749. }
  750. ret = 0;
  751. spin_lock(&mm->page_table_lock);
  752. /* Check for a racing update before calling hugetlb_cow */
  753. if (likely(pte_same(entry, *ptep)))
  754. if (write_access && !pte_write(entry))
  755. ret = hugetlb_cow(mm, vma, address, ptep, entry);
  756. spin_unlock(&mm->page_table_lock);
  757. mutex_unlock(&hugetlb_instantiation_mutex);
  758. return ret;
  759. }
  760. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  761. struct page **pages, struct vm_area_struct **vmas,
  762. unsigned long *position, int *length, int i,
  763. int write)
  764. {
  765. unsigned long pfn_offset;
  766. unsigned long vaddr = *position;
  767. int remainder = *length;
  768. spin_lock(&mm->page_table_lock);
  769. while (vaddr < vma->vm_end && remainder) {
  770. pte_t *pte;
  771. struct page *page;
  772. /*
  773. * Some archs (sparc64, sh*) have multiple pte_ts to
  774. * each hugepage. We have to make * sure we get the
  775. * first, for the page indexing below to work.
  776. */
  777. pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
  778. if (!pte || pte_none(*pte)) {
  779. int ret;
  780. spin_unlock(&mm->page_table_lock);
  781. ret = hugetlb_fault(mm, vma, vaddr, write);
  782. spin_lock(&mm->page_table_lock);
  783. if (!(ret & VM_FAULT_ERROR))
  784. continue;
  785. remainder = 0;
  786. if (!i)
  787. i = -EFAULT;
  788. break;
  789. }
  790. pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
  791. page = pte_page(*pte);
  792. same_page:
  793. if (pages) {
  794. get_page(page);
  795. pages[i] = page + pfn_offset;
  796. }
  797. if (vmas)
  798. vmas[i] = vma;
  799. vaddr += PAGE_SIZE;
  800. ++pfn_offset;
  801. --remainder;
  802. ++i;
  803. if (vaddr < vma->vm_end && remainder &&
  804. pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
  805. /*
  806. * We use pfn_offset to avoid touching the pageframes
  807. * of this compound page.
  808. */
  809. goto same_page;
  810. }
  811. }
  812. spin_unlock(&mm->page_table_lock);
  813. *length = remainder;
  814. *position = vaddr;
  815. return i;
  816. }
  817. void hugetlb_change_protection(struct vm_area_struct *vma,
  818. unsigned long address, unsigned long end, pgprot_t newprot)
  819. {
  820. struct mm_struct *mm = vma->vm_mm;
  821. unsigned long start = address;
  822. pte_t *ptep;
  823. pte_t pte;
  824. BUG_ON(address >= end);
  825. flush_cache_range(vma, address, end);
  826. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  827. spin_lock(&mm->page_table_lock);
  828. for (; address < end; address += HPAGE_SIZE) {
  829. ptep = huge_pte_offset(mm, address);
  830. if (!ptep)
  831. continue;
  832. if (huge_pmd_unshare(mm, &address, ptep))
  833. continue;
  834. if (!pte_none(*ptep)) {
  835. pte = huge_ptep_get_and_clear(mm, address, ptep);
  836. pte = pte_mkhuge(pte_modify(pte, newprot));
  837. set_huge_pte_at(mm, address, ptep, pte);
  838. }
  839. }
  840. spin_unlock(&mm->page_table_lock);
  841. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  842. flush_tlb_range(vma, start, end);
  843. }
  844. struct file_region {
  845. struct list_head link;
  846. long from;
  847. long to;
  848. };
  849. static long region_add(struct list_head *head, long f, long t)
  850. {
  851. struct file_region *rg, *nrg, *trg;
  852. /* Locate the region we are either in or before. */
  853. list_for_each_entry(rg, head, link)
  854. if (f <= rg->to)
  855. break;
  856. /* Round our left edge to the current segment if it encloses us. */
  857. if (f > rg->from)
  858. f = rg->from;
  859. /* Check for and consume any regions we now overlap with. */
  860. nrg = rg;
  861. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  862. if (&rg->link == head)
  863. break;
  864. if (rg->from > t)
  865. break;
  866. /* If this area reaches higher then extend our area to
  867. * include it completely. If this is not the first area
  868. * which we intend to reuse, free it. */
  869. if (rg->to > t)
  870. t = rg->to;
  871. if (rg != nrg) {
  872. list_del(&rg->link);
  873. kfree(rg);
  874. }
  875. }
  876. nrg->from = f;
  877. nrg->to = t;
  878. return 0;
  879. }
  880. static long region_chg(struct list_head *head, long f, long t)
  881. {
  882. struct file_region *rg, *nrg;
  883. long chg = 0;
  884. /* Locate the region we are before or in. */
  885. list_for_each_entry(rg, head, link)
  886. if (f <= rg->to)
  887. break;
  888. /* If we are below the current region then a new region is required.
  889. * Subtle, allocate a new region at the position but make it zero
  890. * size such that we can guarantee to record the reservation. */
  891. if (&rg->link == head || t < rg->from) {
  892. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  893. if (!nrg)
  894. return -ENOMEM;
  895. nrg->from = f;
  896. nrg->to = f;
  897. INIT_LIST_HEAD(&nrg->link);
  898. list_add(&nrg->link, rg->link.prev);
  899. return t - f;
  900. }
  901. /* Round our left edge to the current segment if it encloses us. */
  902. if (f > rg->from)
  903. f = rg->from;
  904. chg = t - f;
  905. /* Check for and consume any regions we now overlap with. */
  906. list_for_each_entry(rg, rg->link.prev, link) {
  907. if (&rg->link == head)
  908. break;
  909. if (rg->from > t)
  910. return chg;
  911. /* We overlap with this area, if it extends futher than
  912. * us then we must extend ourselves. Account for its
  913. * existing reservation. */
  914. if (rg->to > t) {
  915. chg += rg->to - t;
  916. t = rg->to;
  917. }
  918. chg -= rg->to - rg->from;
  919. }
  920. return chg;
  921. }
  922. static long region_truncate(struct list_head *head, long end)
  923. {
  924. struct file_region *rg, *trg;
  925. long chg = 0;
  926. /* Locate the region we are either in or before. */
  927. list_for_each_entry(rg, head, link)
  928. if (end <= rg->to)
  929. break;
  930. if (&rg->link == head)
  931. return 0;
  932. /* If we are in the middle of a region then adjust it. */
  933. if (end > rg->from) {
  934. chg = rg->to - end;
  935. rg->to = end;
  936. rg = list_entry(rg->link.next, typeof(*rg), link);
  937. }
  938. /* Drop any remaining regions. */
  939. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  940. if (&rg->link == head)
  941. break;
  942. chg += rg->to - rg->from;
  943. list_del(&rg->link);
  944. kfree(rg);
  945. }
  946. return chg;
  947. }
  948. static int hugetlb_acct_memory(long delta)
  949. {
  950. int ret = -ENOMEM;
  951. spin_lock(&hugetlb_lock);
  952. /*
  953. * When cpuset is configured, it breaks the strict hugetlb page
  954. * reservation as the accounting is done on a global variable. Such
  955. * reservation is completely rubbish in the presence of cpuset because
  956. * the reservation is not checked against page availability for the
  957. * current cpuset. Application can still potentially OOM'ed by kernel
  958. * with lack of free htlb page in cpuset that the task is in.
  959. * Attempt to enforce strict accounting with cpuset is almost
  960. * impossible (or too ugly) because cpuset is too fluid that
  961. * task or memory node can be dynamically moved between cpusets.
  962. *
  963. * The change of semantics for shared hugetlb mapping with cpuset is
  964. * undesirable. However, in order to preserve some of the semantics,
  965. * we fall back to check against current free page availability as
  966. * a best attempt and hopefully to minimize the impact of changing
  967. * semantics that cpuset has.
  968. */
  969. if (delta > 0) {
  970. if (gather_surplus_pages(delta) < 0)
  971. goto out;
  972. if (delta > cpuset_mems_nr(free_huge_pages_node))
  973. goto out;
  974. }
  975. ret = 0;
  976. resv_huge_pages += delta;
  977. if (delta < 0)
  978. return_unused_surplus_pages((unsigned long) -delta);
  979. out:
  980. spin_unlock(&hugetlb_lock);
  981. return ret;
  982. }
  983. int hugetlb_reserve_pages(struct inode *inode, long from, long to)
  984. {
  985. long ret, chg;
  986. chg = region_chg(&inode->i_mapping->private_list, from, to);
  987. if (chg < 0)
  988. return chg;
  989. ret = hugetlb_acct_memory(chg);
  990. if (ret < 0)
  991. return ret;
  992. region_add(&inode->i_mapping->private_list, from, to);
  993. return 0;
  994. }
  995. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  996. {
  997. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  998. hugetlb_acct_memory(freed - chg);
  999. }