hugetlb.c 27 KB

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