hugetlb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. lazy_mmu_prot_update(entry);
  311. }
  312. }
  313. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  314. struct vm_area_struct *vma)
  315. {
  316. pte_t *src_pte, *dst_pte, entry;
  317. struct page *ptepage;
  318. unsigned long addr;
  319. int cow;
  320. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  321. for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
  322. src_pte = huge_pte_offset(src, addr);
  323. if (!src_pte)
  324. continue;
  325. dst_pte = huge_pte_alloc(dst, addr);
  326. if (!dst_pte)
  327. goto nomem;
  328. spin_lock(&dst->page_table_lock);
  329. spin_lock(&src->page_table_lock);
  330. if (!pte_none(*src_pte)) {
  331. if (cow)
  332. ptep_set_wrprotect(src, addr, src_pte);
  333. entry = *src_pte;
  334. ptepage = pte_page(entry);
  335. get_page(ptepage);
  336. set_huge_pte_at(dst, addr, dst_pte, entry);
  337. }
  338. spin_unlock(&src->page_table_lock);
  339. spin_unlock(&dst->page_table_lock);
  340. }
  341. return 0;
  342. nomem:
  343. return -ENOMEM;
  344. }
  345. void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  346. unsigned long end)
  347. {
  348. struct mm_struct *mm = vma->vm_mm;
  349. unsigned long address;
  350. pte_t *ptep;
  351. pte_t pte;
  352. struct page *page;
  353. struct page *tmp;
  354. /*
  355. * A page gathering list, protected by per file i_mmap_lock. The
  356. * lock is used to avoid list corruption from multiple unmapping
  357. * of the same page since we are using page->lru.
  358. */
  359. LIST_HEAD(page_list);
  360. WARN_ON(!is_vm_hugetlb_page(vma));
  361. BUG_ON(start & ~HPAGE_MASK);
  362. BUG_ON(end & ~HPAGE_MASK);
  363. spin_lock(&mm->page_table_lock);
  364. for (address = start; address < end; address += HPAGE_SIZE) {
  365. ptep = huge_pte_offset(mm, address);
  366. if (!ptep)
  367. continue;
  368. if (huge_pmd_unshare(mm, &address, ptep))
  369. continue;
  370. pte = huge_ptep_get_and_clear(mm, address, ptep);
  371. if (pte_none(pte))
  372. continue;
  373. page = pte_page(pte);
  374. if (pte_dirty(pte))
  375. set_page_dirty(page);
  376. list_add(&page->lru, &page_list);
  377. }
  378. spin_unlock(&mm->page_table_lock);
  379. flush_tlb_range(vma, start, end);
  380. list_for_each_entry_safe(page, tmp, &page_list, lru) {
  381. list_del(&page->lru);
  382. put_page(page);
  383. }
  384. }
  385. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  386. unsigned long end)
  387. {
  388. /*
  389. * It is undesirable to test vma->vm_file as it should be non-null
  390. * for valid hugetlb area. However, vm_file will be NULL in the error
  391. * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
  392. * do_mmap_pgoff() nullifies vma->vm_file before calling this function
  393. * to clean up. Since no pte has actually been setup, it is safe to
  394. * do nothing in this case.
  395. */
  396. if (vma->vm_file) {
  397. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  398. __unmap_hugepage_range(vma, start, end);
  399. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  400. }
  401. }
  402. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  403. unsigned long address, pte_t *ptep, pte_t pte)
  404. {
  405. struct page *old_page, *new_page;
  406. int avoidcopy;
  407. old_page = pte_page(pte);
  408. /* If no-one else is actually using this page, avoid the copy
  409. * and just make the page writable */
  410. avoidcopy = (page_count(old_page) == 1);
  411. if (avoidcopy) {
  412. set_huge_ptep_writable(vma, address, ptep);
  413. return 0;
  414. }
  415. page_cache_get(old_page);
  416. new_page = alloc_huge_page(vma, address);
  417. if (!new_page) {
  418. page_cache_release(old_page);
  419. return VM_FAULT_OOM;
  420. }
  421. spin_unlock(&mm->page_table_lock);
  422. copy_huge_page(new_page, old_page, address, vma);
  423. spin_lock(&mm->page_table_lock);
  424. ptep = huge_pte_offset(mm, address & HPAGE_MASK);
  425. if (likely(pte_same(*ptep, pte))) {
  426. /* Break COW */
  427. set_huge_pte_at(mm, address, ptep,
  428. make_huge_pte(vma, new_page, 1));
  429. /* Make the old page be freed below */
  430. new_page = old_page;
  431. }
  432. page_cache_release(new_page);
  433. page_cache_release(old_page);
  434. return 0;
  435. }
  436. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  437. unsigned long address, pte_t *ptep, int write_access)
  438. {
  439. int ret = VM_FAULT_SIGBUS;
  440. unsigned long idx;
  441. unsigned long size;
  442. struct page *page;
  443. struct address_space *mapping;
  444. pte_t new_pte;
  445. mapping = vma->vm_file->f_mapping;
  446. idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
  447. + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
  448. /*
  449. * Use page lock to guard against racing truncation
  450. * before we get page_table_lock.
  451. */
  452. retry:
  453. page = find_lock_page(mapping, idx);
  454. if (!page) {
  455. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  456. if (idx >= size)
  457. goto out;
  458. if (hugetlb_get_quota(mapping))
  459. goto out;
  460. page = alloc_huge_page(vma, address);
  461. if (!page) {
  462. hugetlb_put_quota(mapping);
  463. ret = VM_FAULT_OOM;
  464. goto out;
  465. }
  466. clear_huge_page(page, address);
  467. if (vma->vm_flags & VM_SHARED) {
  468. int err;
  469. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  470. if (err) {
  471. put_page(page);
  472. hugetlb_put_quota(mapping);
  473. if (err == -EEXIST)
  474. goto retry;
  475. goto out;
  476. }
  477. } else
  478. lock_page(page);
  479. }
  480. spin_lock(&mm->page_table_lock);
  481. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  482. if (idx >= size)
  483. goto backout;
  484. ret = 0;
  485. if (!pte_none(*ptep))
  486. goto backout;
  487. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  488. && (vma->vm_flags & VM_SHARED)));
  489. set_huge_pte_at(mm, address, ptep, new_pte);
  490. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  491. /* Optimization, do the COW without a second fault */
  492. ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
  493. }
  494. spin_unlock(&mm->page_table_lock);
  495. unlock_page(page);
  496. out:
  497. return ret;
  498. backout:
  499. spin_unlock(&mm->page_table_lock);
  500. hugetlb_put_quota(mapping);
  501. unlock_page(page);
  502. put_page(page);
  503. goto out;
  504. }
  505. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  506. unsigned long address, int write_access)
  507. {
  508. pte_t *ptep;
  509. pte_t entry;
  510. int ret;
  511. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  512. ptep = huge_pte_alloc(mm, address);
  513. if (!ptep)
  514. return VM_FAULT_OOM;
  515. /*
  516. * Serialize hugepage allocation and instantiation, so that we don't
  517. * get spurious allocation failures if two CPUs race to instantiate
  518. * the same page in the page cache.
  519. */
  520. mutex_lock(&hugetlb_instantiation_mutex);
  521. entry = *ptep;
  522. if (pte_none(entry)) {
  523. ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
  524. mutex_unlock(&hugetlb_instantiation_mutex);
  525. return ret;
  526. }
  527. ret = 0;
  528. spin_lock(&mm->page_table_lock);
  529. /* Check for a racing update before calling hugetlb_cow */
  530. if (likely(pte_same(entry, *ptep)))
  531. if (write_access && !pte_write(entry))
  532. ret = hugetlb_cow(mm, vma, address, ptep, entry);
  533. spin_unlock(&mm->page_table_lock);
  534. mutex_unlock(&hugetlb_instantiation_mutex);
  535. return ret;
  536. }
  537. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  538. struct page **pages, struct vm_area_struct **vmas,
  539. unsigned long *position, int *length, int i)
  540. {
  541. unsigned long pfn_offset;
  542. unsigned long vaddr = *position;
  543. int remainder = *length;
  544. spin_lock(&mm->page_table_lock);
  545. while (vaddr < vma->vm_end && remainder) {
  546. pte_t *pte;
  547. struct page *page;
  548. /*
  549. * Some archs (sparc64, sh*) have multiple pte_ts to
  550. * each hugepage. We have to make * sure we get the
  551. * first, for the page indexing below to work.
  552. */
  553. pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
  554. if (!pte || pte_none(*pte)) {
  555. int ret;
  556. spin_unlock(&mm->page_table_lock);
  557. ret = hugetlb_fault(mm, vma, vaddr, 0);
  558. spin_lock(&mm->page_table_lock);
  559. if (!(ret & VM_FAULT_ERROR))
  560. continue;
  561. remainder = 0;
  562. if (!i)
  563. i = -EFAULT;
  564. break;
  565. }
  566. pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
  567. page = pte_page(*pte);
  568. same_page:
  569. if (pages) {
  570. get_page(page);
  571. pages[i] = page + pfn_offset;
  572. }
  573. if (vmas)
  574. vmas[i] = vma;
  575. vaddr += PAGE_SIZE;
  576. ++pfn_offset;
  577. --remainder;
  578. ++i;
  579. if (vaddr < vma->vm_end && remainder &&
  580. pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
  581. /*
  582. * We use pfn_offset to avoid touching the pageframes
  583. * of this compound page.
  584. */
  585. goto same_page;
  586. }
  587. }
  588. spin_unlock(&mm->page_table_lock);
  589. *length = remainder;
  590. *position = vaddr;
  591. return i;
  592. }
  593. void hugetlb_change_protection(struct vm_area_struct *vma,
  594. unsigned long address, unsigned long end, pgprot_t newprot)
  595. {
  596. struct mm_struct *mm = vma->vm_mm;
  597. unsigned long start = address;
  598. pte_t *ptep;
  599. pte_t pte;
  600. BUG_ON(address >= end);
  601. flush_cache_range(vma, address, end);
  602. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  603. spin_lock(&mm->page_table_lock);
  604. for (; address < end; address += HPAGE_SIZE) {
  605. ptep = huge_pte_offset(mm, address);
  606. if (!ptep)
  607. continue;
  608. if (huge_pmd_unshare(mm, &address, ptep))
  609. continue;
  610. if (!pte_none(*ptep)) {
  611. pte = huge_ptep_get_and_clear(mm, address, ptep);
  612. pte = pte_mkhuge(pte_modify(pte, newprot));
  613. set_huge_pte_at(mm, address, ptep, pte);
  614. lazy_mmu_prot_update(pte);
  615. }
  616. }
  617. spin_unlock(&mm->page_table_lock);
  618. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  619. flush_tlb_range(vma, start, end);
  620. }
  621. struct file_region {
  622. struct list_head link;
  623. long from;
  624. long to;
  625. };
  626. static long region_add(struct list_head *head, long f, long t)
  627. {
  628. struct file_region *rg, *nrg, *trg;
  629. /* Locate the region we are either in or before. */
  630. list_for_each_entry(rg, head, link)
  631. if (f <= rg->to)
  632. break;
  633. /* Round our left edge to the current segment if it encloses us. */
  634. if (f > rg->from)
  635. f = rg->from;
  636. /* Check for and consume any regions we now overlap with. */
  637. nrg = rg;
  638. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  639. if (&rg->link == head)
  640. break;
  641. if (rg->from > t)
  642. break;
  643. /* If this area reaches higher then extend our area to
  644. * include it completely. If this is not the first area
  645. * which we intend to reuse, free it. */
  646. if (rg->to > t)
  647. t = rg->to;
  648. if (rg != nrg) {
  649. list_del(&rg->link);
  650. kfree(rg);
  651. }
  652. }
  653. nrg->from = f;
  654. nrg->to = t;
  655. return 0;
  656. }
  657. static long region_chg(struct list_head *head, long f, long t)
  658. {
  659. struct file_region *rg, *nrg;
  660. long chg = 0;
  661. /* Locate the region we are before or in. */
  662. list_for_each_entry(rg, head, link)
  663. if (f <= rg->to)
  664. break;
  665. /* If we are below the current region then a new region is required.
  666. * Subtle, allocate a new region at the position but make it zero
  667. * size such that we can guarentee to record the reservation. */
  668. if (&rg->link == head || t < rg->from) {
  669. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  670. if (nrg == 0)
  671. return -ENOMEM;
  672. nrg->from = f;
  673. nrg->to = f;
  674. INIT_LIST_HEAD(&nrg->link);
  675. list_add(&nrg->link, rg->link.prev);
  676. return t - f;
  677. }
  678. /* Round our left edge to the current segment if it encloses us. */
  679. if (f > rg->from)
  680. f = rg->from;
  681. chg = t - f;
  682. /* Check for and consume any regions we now overlap with. */
  683. list_for_each_entry(rg, rg->link.prev, link) {
  684. if (&rg->link == head)
  685. break;
  686. if (rg->from > t)
  687. return chg;
  688. /* We overlap with this area, if it extends futher than
  689. * us then we must extend ourselves. Account for its
  690. * existing reservation. */
  691. if (rg->to > t) {
  692. chg += rg->to - t;
  693. t = rg->to;
  694. }
  695. chg -= rg->to - rg->from;
  696. }
  697. return chg;
  698. }
  699. static long region_truncate(struct list_head *head, long end)
  700. {
  701. struct file_region *rg, *trg;
  702. long chg = 0;
  703. /* Locate the region we are either in or before. */
  704. list_for_each_entry(rg, head, link)
  705. if (end <= rg->to)
  706. break;
  707. if (&rg->link == head)
  708. return 0;
  709. /* If we are in the middle of a region then adjust it. */
  710. if (end > rg->from) {
  711. chg = rg->to - end;
  712. rg->to = end;
  713. rg = list_entry(rg->link.next, typeof(*rg), link);
  714. }
  715. /* Drop any remaining regions. */
  716. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  717. if (&rg->link == head)
  718. break;
  719. chg += rg->to - rg->from;
  720. list_del(&rg->link);
  721. kfree(rg);
  722. }
  723. return chg;
  724. }
  725. static int hugetlb_acct_memory(long delta)
  726. {
  727. int ret = -ENOMEM;
  728. spin_lock(&hugetlb_lock);
  729. if ((delta + resv_huge_pages) <= free_huge_pages) {
  730. resv_huge_pages += delta;
  731. ret = 0;
  732. }
  733. spin_unlock(&hugetlb_lock);
  734. return ret;
  735. }
  736. int hugetlb_reserve_pages(struct inode *inode, long from, long to)
  737. {
  738. long ret, chg;
  739. chg = region_chg(&inode->i_mapping->private_list, from, to);
  740. if (chg < 0)
  741. return chg;
  742. /*
  743. * When cpuset is configured, it breaks the strict hugetlb page
  744. * reservation as the accounting is done on a global variable. Such
  745. * reservation is completely rubbish in the presence of cpuset because
  746. * the reservation is not checked against page availability for the
  747. * current cpuset. Application can still potentially OOM'ed by kernel
  748. * with lack of free htlb page in cpuset that the task is in.
  749. * Attempt to enforce strict accounting with cpuset is almost
  750. * impossible (or too ugly) because cpuset is too fluid that
  751. * task or memory node can be dynamically moved between cpusets.
  752. *
  753. * The change of semantics for shared hugetlb mapping with cpuset is
  754. * undesirable. However, in order to preserve some of the semantics,
  755. * we fall back to check against current free page availability as
  756. * a best attempt and hopefully to minimize the impact of changing
  757. * semantics that cpuset has.
  758. */
  759. if (chg > cpuset_mems_nr(free_huge_pages_node))
  760. return -ENOMEM;
  761. ret = hugetlb_acct_memory(chg);
  762. if (ret < 0)
  763. return ret;
  764. region_add(&inode->i_mapping->private_list, from, to);
  765. return 0;
  766. }
  767. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  768. {
  769. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  770. hugetlb_acct_memory(freed - chg);
  771. }