hugetlb.c 24 KB

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