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);
  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 zonelist *zonelist = huge_zonelist(vma, address,
  65. htlb_alloc_mask);
  66. struct zone **z;
  67. for (z = zonelist->zones; *z; z++) {
  68. nid = zone_to_nid(*z);
  69. if (cpuset_zone_allowed_softwall(*z, htlb_alloc_mask) &&
  70. !list_empty(&hugepage_freelists[nid])) {
  71. page = list_entry(hugepage_freelists[nid].next,
  72. struct page, lru);
  73. list_del(&page->lru);
  74. free_huge_pages--;
  75. free_huge_pages_node[nid]--;
  76. break;
  77. }
  78. }
  79. return page;
  80. }
  81. static void free_huge_page(struct page *page)
  82. {
  83. BUG_ON(page_count(page));
  84. INIT_LIST_HEAD(&page->lru);
  85. spin_lock(&hugetlb_lock);
  86. enqueue_huge_page(page);
  87. spin_unlock(&hugetlb_lock);
  88. }
  89. static int alloc_fresh_huge_page(void)
  90. {
  91. static int prev_nid;
  92. struct page *page;
  93. int nid;
  94. /*
  95. * Copy static prev_nid to local nid, work on that, then copy it
  96. * back to prev_nid afterwards: otherwise there's a window in which
  97. * a racer might pass invalid nid MAX_NUMNODES to alloc_pages_node.
  98. * But we don't need to use a spin_lock here: it really doesn't
  99. * matter if occasionally a racer chooses the same nid as we do.
  100. */
  101. nid = next_node(prev_nid, node_online_map);
  102. if (nid == MAX_NUMNODES)
  103. nid = first_node(node_online_map);
  104. prev_nid = nid;
  105. page = alloc_pages_node(nid, htlb_alloc_mask|__GFP_COMP|__GFP_NOWARN,
  106. HUGETLB_PAGE_ORDER);
  107. if (page) {
  108. set_compound_page_dtor(page, free_huge_page);
  109. spin_lock(&hugetlb_lock);
  110. nr_huge_pages++;
  111. nr_huge_pages_node[page_to_nid(page)]++;
  112. spin_unlock(&hugetlb_lock);
  113. put_page(page); /* free it into the hugepage allocator */
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  119. unsigned long addr)
  120. {
  121. struct page *page;
  122. spin_lock(&hugetlb_lock);
  123. if (vma->vm_flags & VM_MAYSHARE)
  124. resv_huge_pages--;
  125. else if (free_huge_pages <= resv_huge_pages)
  126. goto fail;
  127. page = dequeue_huge_page(vma, addr);
  128. if (!page)
  129. goto fail;
  130. spin_unlock(&hugetlb_lock);
  131. set_page_refcounted(page);
  132. return page;
  133. fail:
  134. if (vma->vm_flags & VM_MAYSHARE)
  135. resv_huge_pages++;
  136. spin_unlock(&hugetlb_lock);
  137. return NULL;
  138. }
  139. static int __init hugetlb_init(void)
  140. {
  141. unsigned long i;
  142. if (HPAGE_SHIFT == 0)
  143. return 0;
  144. for (i = 0; i < MAX_NUMNODES; ++i)
  145. INIT_LIST_HEAD(&hugepage_freelists[i]);
  146. for (i = 0; i < max_huge_pages; ++i) {
  147. if (!alloc_fresh_huge_page())
  148. break;
  149. }
  150. max_huge_pages = free_huge_pages = nr_huge_pages = i;
  151. printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
  152. return 0;
  153. }
  154. module_init(hugetlb_init);
  155. static int __init hugetlb_setup(char *s)
  156. {
  157. if (sscanf(s, "%lu", &max_huge_pages) <= 0)
  158. max_huge_pages = 0;
  159. return 1;
  160. }
  161. __setup("hugepages=", hugetlb_setup);
  162. static unsigned int cpuset_mems_nr(unsigned int *array)
  163. {
  164. int node;
  165. unsigned int nr = 0;
  166. for_each_node_mask(node, cpuset_current_mems_allowed)
  167. nr += array[node];
  168. return nr;
  169. }
  170. #ifdef CONFIG_SYSCTL
  171. static void update_and_free_page(struct page *page)
  172. {
  173. int i;
  174. nr_huge_pages--;
  175. nr_huge_pages_node[page_to_nid(page)]--;
  176. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
  177. page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
  178. 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
  179. 1 << PG_private | 1<< PG_writeback);
  180. }
  181. set_compound_page_dtor(page, NULL);
  182. set_page_refcounted(page);
  183. __free_pages(page, HUGETLB_PAGE_ORDER);
  184. }
  185. #ifdef CONFIG_HIGHMEM
  186. static void try_to_free_low(unsigned long count)
  187. {
  188. int i;
  189. for (i = 0; i < MAX_NUMNODES; ++i) {
  190. struct page *page, *next;
  191. list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
  192. if (PageHighMem(page))
  193. continue;
  194. list_del(&page->lru);
  195. update_and_free_page(page);
  196. free_huge_pages--;
  197. free_huge_pages_node[page_to_nid(page)]--;
  198. if (count >= nr_huge_pages)
  199. return;
  200. }
  201. }
  202. }
  203. #else
  204. static inline void try_to_free_low(unsigned long count)
  205. {
  206. }
  207. #endif
  208. static unsigned long set_max_huge_pages(unsigned long count)
  209. {
  210. while (count > nr_huge_pages) {
  211. if (!alloc_fresh_huge_page())
  212. return nr_huge_pages;
  213. }
  214. if (count >= nr_huge_pages)
  215. return nr_huge_pages;
  216. spin_lock(&hugetlb_lock);
  217. count = max(count, resv_huge_pages);
  218. try_to_free_low(count);
  219. while (count < nr_huge_pages) {
  220. struct page *page = dequeue_huge_page(NULL, 0);
  221. if (!page)
  222. break;
  223. update_and_free_page(page);
  224. }
  225. spin_unlock(&hugetlb_lock);
  226. return nr_huge_pages;
  227. }
  228. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  229. struct file *file, void __user *buffer,
  230. size_t *length, loff_t *ppos)
  231. {
  232. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  233. max_huge_pages = set_max_huge_pages(max_huge_pages);
  234. return 0;
  235. }
  236. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  237. struct file *file, void __user *buffer,
  238. size_t *length, loff_t *ppos)
  239. {
  240. proc_dointvec(table, write, file, buffer, length, ppos);
  241. if (hugepages_treat_as_movable)
  242. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  243. else
  244. htlb_alloc_mask = GFP_HIGHUSER;
  245. return 0;
  246. }
  247. #endif /* CONFIG_SYSCTL */
  248. int hugetlb_report_meminfo(char *buf)
  249. {
  250. return sprintf(buf,
  251. "HugePages_Total: %5lu\n"
  252. "HugePages_Free: %5lu\n"
  253. "HugePages_Rsvd: %5lu\n"
  254. "Hugepagesize: %5lu kB\n",
  255. nr_huge_pages,
  256. free_huge_pages,
  257. resv_huge_pages,
  258. HPAGE_SIZE/1024);
  259. }
  260. int hugetlb_report_node_meminfo(int nid, char *buf)
  261. {
  262. return sprintf(buf,
  263. "Node %d HugePages_Total: %5u\n"
  264. "Node %d HugePages_Free: %5u\n",
  265. nid, nr_huge_pages_node[nid],
  266. nid, free_huge_pages_node[nid]);
  267. }
  268. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  269. unsigned long hugetlb_total_pages(void)
  270. {
  271. return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
  272. }
  273. /*
  274. * We cannot handle pagefaults against hugetlb pages at all. They cause
  275. * handle_mm_fault() to try to instantiate regular-sized pages in the
  276. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  277. * this far.
  278. */
  279. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  280. {
  281. BUG();
  282. return 0;
  283. }
  284. struct vm_operations_struct hugetlb_vm_ops = {
  285. .fault = hugetlb_vm_op_fault,
  286. };
  287. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  288. int writable)
  289. {
  290. pte_t entry;
  291. if (writable) {
  292. entry =
  293. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  294. } else {
  295. entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  296. }
  297. entry = pte_mkyoung(entry);
  298. entry = pte_mkhuge(entry);
  299. return entry;
  300. }
  301. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  302. unsigned long address, pte_t *ptep)
  303. {
  304. pte_t entry;
  305. entry = pte_mkwrite(pte_mkdirty(*ptep));
  306. if (ptep_set_access_flags(vma, address, ptep, entry, 1)) {
  307. update_mmu_cache(vma, address, entry);
  308. lazy_mmu_prot_update(entry);
  309. }
  310. }
  311. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  312. struct vm_area_struct *vma)
  313. {
  314. pte_t *src_pte, *dst_pte, entry;
  315. struct page *ptepage;
  316. unsigned long addr;
  317. int cow;
  318. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  319. for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
  320. src_pte = huge_pte_offset(src, addr);
  321. if (!src_pte)
  322. continue;
  323. dst_pte = huge_pte_alloc(dst, addr);
  324. if (!dst_pte)
  325. goto nomem;
  326. spin_lock(&dst->page_table_lock);
  327. spin_lock(&src->page_table_lock);
  328. if (!pte_none(*src_pte)) {
  329. if (cow)
  330. ptep_set_wrprotect(src, addr, src_pte);
  331. entry = *src_pte;
  332. ptepage = pte_page(entry);
  333. get_page(ptepage);
  334. set_huge_pte_at(dst, addr, dst_pte, entry);
  335. }
  336. spin_unlock(&src->page_table_lock);
  337. spin_unlock(&dst->page_table_lock);
  338. }
  339. return 0;
  340. nomem:
  341. return -ENOMEM;
  342. }
  343. void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  344. unsigned long end)
  345. {
  346. struct mm_struct *mm = vma->vm_mm;
  347. unsigned long address;
  348. pte_t *ptep;
  349. pte_t pte;
  350. struct page *page;
  351. struct page *tmp;
  352. /*
  353. * A page gathering list, protected by per file i_mmap_lock. The
  354. * lock is used to avoid list corruption from multiple unmapping
  355. * of the same page since we are using page->lru.
  356. */
  357. LIST_HEAD(page_list);
  358. WARN_ON(!is_vm_hugetlb_page(vma));
  359. BUG_ON(start & ~HPAGE_MASK);
  360. BUG_ON(end & ~HPAGE_MASK);
  361. spin_lock(&mm->page_table_lock);
  362. for (address = start; address < end; address += HPAGE_SIZE) {
  363. ptep = huge_pte_offset(mm, address);
  364. if (!ptep)
  365. continue;
  366. if (huge_pmd_unshare(mm, &address, ptep))
  367. continue;
  368. pte = huge_ptep_get_and_clear(mm, address, ptep);
  369. if (pte_none(pte))
  370. continue;
  371. page = pte_page(pte);
  372. if (pte_dirty(pte))
  373. set_page_dirty(page);
  374. list_add(&page->lru, &page_list);
  375. }
  376. spin_unlock(&mm->page_table_lock);
  377. flush_tlb_range(vma, start, end);
  378. list_for_each_entry_safe(page, tmp, &page_list, lru) {
  379. list_del(&page->lru);
  380. put_page(page);
  381. }
  382. }
  383. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  384. unsigned long end)
  385. {
  386. /*
  387. * It is undesirable to test vma->vm_file as it should be non-null
  388. * for valid hugetlb area. However, vm_file will be NULL in the error
  389. * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
  390. * do_mmap_pgoff() nullifies vma->vm_file before calling this function
  391. * to clean up. Since no pte has actually been setup, it is safe to
  392. * do nothing in this case.
  393. */
  394. if (vma->vm_file) {
  395. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  396. __unmap_hugepage_range(vma, start, end);
  397. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  398. }
  399. }
  400. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  401. unsigned long address, pte_t *ptep, pte_t pte)
  402. {
  403. struct page *old_page, *new_page;
  404. int avoidcopy;
  405. old_page = pte_page(pte);
  406. /* If no-one else is actually using this page, avoid the copy
  407. * and just make the page writable */
  408. avoidcopy = (page_count(old_page) == 1);
  409. if (avoidcopy) {
  410. set_huge_ptep_writable(vma, address, ptep);
  411. return 0;
  412. }
  413. page_cache_get(old_page);
  414. new_page = alloc_huge_page(vma, address);
  415. if (!new_page) {
  416. page_cache_release(old_page);
  417. return VM_FAULT_OOM;
  418. }
  419. spin_unlock(&mm->page_table_lock);
  420. copy_huge_page(new_page, old_page, address, vma);
  421. spin_lock(&mm->page_table_lock);
  422. ptep = huge_pte_offset(mm, address & HPAGE_MASK);
  423. if (likely(pte_same(*ptep, pte))) {
  424. /* Break COW */
  425. set_huge_pte_at(mm, address, ptep,
  426. make_huge_pte(vma, new_page, 1));
  427. /* Make the old page be freed below */
  428. new_page = old_page;
  429. }
  430. page_cache_release(new_page);
  431. page_cache_release(old_page);
  432. return 0;
  433. }
  434. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  435. unsigned long address, pte_t *ptep, int write_access)
  436. {
  437. int ret = VM_FAULT_SIGBUS;
  438. unsigned long idx;
  439. unsigned long size;
  440. struct page *page;
  441. struct address_space *mapping;
  442. pte_t new_pte;
  443. mapping = vma->vm_file->f_mapping;
  444. idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
  445. + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
  446. /*
  447. * Use page lock to guard against racing truncation
  448. * before we get page_table_lock.
  449. */
  450. retry:
  451. page = find_lock_page(mapping, idx);
  452. if (!page) {
  453. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  454. if (idx >= size)
  455. goto out;
  456. if (hugetlb_get_quota(mapping))
  457. goto out;
  458. page = alloc_huge_page(vma, address);
  459. if (!page) {
  460. hugetlb_put_quota(mapping);
  461. ret = VM_FAULT_OOM;
  462. goto out;
  463. }
  464. clear_huge_page(page, address);
  465. if (vma->vm_flags & VM_SHARED) {
  466. int err;
  467. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  468. if (err) {
  469. put_page(page);
  470. hugetlb_put_quota(mapping);
  471. if (err == -EEXIST)
  472. goto retry;
  473. goto out;
  474. }
  475. } else
  476. lock_page(page);
  477. }
  478. spin_lock(&mm->page_table_lock);
  479. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  480. if (idx >= size)
  481. goto backout;
  482. ret = 0;
  483. if (!pte_none(*ptep))
  484. goto backout;
  485. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  486. && (vma->vm_flags & VM_SHARED)));
  487. set_huge_pte_at(mm, address, ptep, new_pte);
  488. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  489. /* Optimization, do the COW without a second fault */
  490. ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
  491. }
  492. spin_unlock(&mm->page_table_lock);
  493. unlock_page(page);
  494. out:
  495. return ret;
  496. backout:
  497. spin_unlock(&mm->page_table_lock);
  498. hugetlb_put_quota(mapping);
  499. unlock_page(page);
  500. put_page(page);
  501. goto out;
  502. }
  503. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  504. unsigned long address, int write_access)
  505. {
  506. pte_t *ptep;
  507. pte_t entry;
  508. int ret;
  509. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  510. ptep = huge_pte_alloc(mm, address);
  511. if (!ptep)
  512. return VM_FAULT_OOM;
  513. /*
  514. * Serialize hugepage allocation and instantiation, so that we don't
  515. * get spurious allocation failures if two CPUs race to instantiate
  516. * the same page in the page cache.
  517. */
  518. mutex_lock(&hugetlb_instantiation_mutex);
  519. entry = *ptep;
  520. if (pte_none(entry)) {
  521. ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
  522. mutex_unlock(&hugetlb_instantiation_mutex);
  523. return ret;
  524. }
  525. ret = 0;
  526. spin_lock(&mm->page_table_lock);
  527. /* Check for a racing update before calling hugetlb_cow */
  528. if (likely(pte_same(entry, *ptep)))
  529. if (write_access && !pte_write(entry))
  530. ret = hugetlb_cow(mm, vma, address, ptep, entry);
  531. spin_unlock(&mm->page_table_lock);
  532. mutex_unlock(&hugetlb_instantiation_mutex);
  533. return ret;
  534. }
  535. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  536. struct page **pages, struct vm_area_struct **vmas,
  537. unsigned long *position, int *length, int i)
  538. {
  539. unsigned long pfn_offset;
  540. unsigned long vaddr = *position;
  541. int remainder = *length;
  542. spin_lock(&mm->page_table_lock);
  543. while (vaddr < vma->vm_end && remainder) {
  544. pte_t *pte;
  545. struct page *page;
  546. /*
  547. * Some archs (sparc64, sh*) have multiple pte_ts to
  548. * each hugepage. We have to make * sure we get the
  549. * first, for the page indexing below to work.
  550. */
  551. pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
  552. if (!pte || pte_none(*pte)) {
  553. int ret;
  554. spin_unlock(&mm->page_table_lock);
  555. ret = hugetlb_fault(mm, vma, vaddr, 0);
  556. spin_lock(&mm->page_table_lock);
  557. if (!(ret & VM_FAULT_ERROR))
  558. continue;
  559. remainder = 0;
  560. if (!i)
  561. i = -EFAULT;
  562. break;
  563. }
  564. pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
  565. page = pte_page(*pte);
  566. same_page:
  567. if (pages) {
  568. get_page(page);
  569. pages[i] = page + pfn_offset;
  570. }
  571. if (vmas)
  572. vmas[i] = vma;
  573. vaddr += PAGE_SIZE;
  574. ++pfn_offset;
  575. --remainder;
  576. ++i;
  577. if (vaddr < vma->vm_end && remainder &&
  578. pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
  579. /*
  580. * We use pfn_offset to avoid touching the pageframes
  581. * of this compound page.
  582. */
  583. goto same_page;
  584. }
  585. }
  586. spin_unlock(&mm->page_table_lock);
  587. *length = remainder;
  588. *position = vaddr;
  589. return i;
  590. }
  591. void hugetlb_change_protection(struct vm_area_struct *vma,
  592. unsigned long address, unsigned long end, pgprot_t newprot)
  593. {
  594. struct mm_struct *mm = vma->vm_mm;
  595. unsigned long start = address;
  596. pte_t *ptep;
  597. pte_t pte;
  598. BUG_ON(address >= end);
  599. flush_cache_range(vma, address, end);
  600. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  601. spin_lock(&mm->page_table_lock);
  602. for (; address < end; address += HPAGE_SIZE) {
  603. ptep = huge_pte_offset(mm, address);
  604. if (!ptep)
  605. continue;
  606. if (huge_pmd_unshare(mm, &address, ptep))
  607. continue;
  608. if (!pte_none(*ptep)) {
  609. pte = huge_ptep_get_and_clear(mm, address, ptep);
  610. pte = pte_mkhuge(pte_modify(pte, newprot));
  611. set_huge_pte_at(mm, address, ptep, pte);
  612. lazy_mmu_prot_update(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. }