hugetlb.c 28 KB

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