hugetlb.c 21 KB

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