hugetlb.c 21 KB

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