hugetlb.c 21 KB

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