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