hugetlbpage.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * IA-32 Huge TLB Page Support for Kernel.
  3. *
  4. * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/fs.h>
  8. #include <linux/mm.h>
  9. #include <linux/hugetlb.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/slab.h>
  12. #include <linux/err.h>
  13. #include <linux/sysctl.h>
  14. #include <asm/mman.h>
  15. #include <asm/tlb.h>
  16. #include <asm/tlbflush.h>
  17. static unsigned long page_table_shareable(struct vm_area_struct *svma,
  18. struct vm_area_struct *vma,
  19. unsigned long addr, pgoff_t idx)
  20. {
  21. unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
  22. svma->vm_start;
  23. unsigned long sbase = saddr & PUD_MASK;
  24. unsigned long s_end = sbase + PUD_SIZE;
  25. /*
  26. * match the virtual addresses, permission and the alignment of the
  27. * page table page.
  28. */
  29. if (pmd_index(addr) != pmd_index(saddr) ||
  30. vma->vm_flags != svma->vm_flags ||
  31. sbase < svma->vm_start || svma->vm_end < s_end)
  32. return 0;
  33. return saddr;
  34. }
  35. static int vma_shareable(struct vm_area_struct *vma, unsigned long addr)
  36. {
  37. unsigned long base = addr & PUD_MASK;
  38. unsigned long end = base + PUD_SIZE;
  39. /*
  40. * check on proper vm_flags and page table alignment
  41. */
  42. if (vma->vm_flags & VM_MAYSHARE &&
  43. vma->vm_start <= base && end <= vma->vm_end)
  44. return 1;
  45. return 0;
  46. }
  47. /*
  48. * search for a shareable pmd page for hugetlb.
  49. */
  50. static void huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
  51. {
  52. struct vm_area_struct *vma = find_vma(mm, addr);
  53. struct address_space *mapping = vma->vm_file->f_mapping;
  54. pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
  55. vma->vm_pgoff;
  56. struct prio_tree_iter iter;
  57. struct vm_area_struct *svma;
  58. unsigned long saddr;
  59. pte_t *spte = NULL;
  60. if (!vma_shareable(vma, addr))
  61. return;
  62. spin_lock(&mapping->i_mmap_lock);
  63. vma_prio_tree_foreach(svma, &iter, &mapping->i_mmap, idx, idx) {
  64. if (svma == vma)
  65. continue;
  66. saddr = page_table_shareable(svma, vma, addr, idx);
  67. if (saddr) {
  68. spte = huge_pte_offset(svma->vm_mm, saddr);
  69. if (spte) {
  70. get_page(virt_to_page(spte));
  71. break;
  72. }
  73. }
  74. }
  75. if (!spte)
  76. goto out;
  77. spin_lock(&mm->page_table_lock);
  78. if (pud_none(*pud))
  79. pud_populate(mm, pud, (unsigned long) spte & PAGE_MASK);
  80. else
  81. put_page(virt_to_page(spte));
  82. spin_unlock(&mm->page_table_lock);
  83. out:
  84. spin_unlock(&mapping->i_mmap_lock);
  85. }
  86. /*
  87. * unmap huge page backed by shared pte.
  88. *
  89. * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
  90. * indicated by page_count > 1, unmap is achieved by clearing pud and
  91. * decrementing the ref count. If count == 1, the pte page is not shared.
  92. *
  93. * called with vma->vm_mm->page_table_lock held.
  94. *
  95. * returns: 1 successfully unmapped a shared pte page
  96. * 0 the underlying pte page is not shared, or it is the last user
  97. */
  98. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  99. {
  100. pgd_t *pgd = pgd_offset(mm, *addr);
  101. pud_t *pud = pud_offset(pgd, *addr);
  102. BUG_ON(page_count(virt_to_page(ptep)) == 0);
  103. if (page_count(virt_to_page(ptep)) == 1)
  104. return 0;
  105. pud_clear(pud);
  106. put_page(virt_to_page(ptep));
  107. *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
  108. return 1;
  109. }
  110. pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
  111. {
  112. pgd_t *pgd;
  113. pud_t *pud;
  114. pte_t *pte = NULL;
  115. pgd = pgd_offset(mm, addr);
  116. pud = pud_alloc(mm, pgd, addr);
  117. if (pud) {
  118. if (pud_none(*pud))
  119. huge_pmd_share(mm, addr, pud);
  120. pte = (pte_t *) pmd_alloc(mm, pud, addr);
  121. }
  122. BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
  123. return pte;
  124. }
  125. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  126. {
  127. pgd_t *pgd;
  128. pud_t *pud;
  129. pmd_t *pmd = NULL;
  130. pgd = pgd_offset(mm, addr);
  131. if (pgd_present(*pgd)) {
  132. pud = pud_offset(pgd, addr);
  133. if (pud_present(*pud))
  134. pmd = pmd_offset(pud, addr);
  135. }
  136. return (pte_t *) pmd;
  137. }
  138. #if 0 /* This is just for testing */
  139. struct page *
  140. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  141. {
  142. unsigned long start = address;
  143. int length = 1;
  144. int nr;
  145. struct page *page;
  146. struct vm_area_struct *vma;
  147. vma = find_vma(mm, addr);
  148. if (!vma || !is_vm_hugetlb_page(vma))
  149. return ERR_PTR(-EINVAL);
  150. pte = huge_pte_offset(mm, address);
  151. /* hugetlb should be locked, and hence, prefaulted */
  152. WARN_ON(!pte || pte_none(*pte));
  153. page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
  154. WARN_ON(!PageCompound(page));
  155. return page;
  156. }
  157. int pmd_huge(pmd_t pmd)
  158. {
  159. return 0;
  160. }
  161. struct page *
  162. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  163. pmd_t *pmd, int write)
  164. {
  165. return NULL;
  166. }
  167. #else
  168. struct page *
  169. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  170. {
  171. return ERR_PTR(-EINVAL);
  172. }
  173. int pmd_huge(pmd_t pmd)
  174. {
  175. return !!(pmd_val(pmd) & _PAGE_PSE);
  176. }
  177. struct page *
  178. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  179. pmd_t *pmd, int write)
  180. {
  181. struct page *page;
  182. page = pte_page(*(pte_t *)pmd);
  183. if (page)
  184. page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT);
  185. return page;
  186. }
  187. #endif
  188. /* x86_64 also uses this file */
  189. #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  190. static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
  191. unsigned long addr, unsigned long len,
  192. unsigned long pgoff, unsigned long flags)
  193. {
  194. struct mm_struct *mm = current->mm;
  195. struct vm_area_struct *vma;
  196. unsigned long start_addr;
  197. if (len > mm->cached_hole_size) {
  198. start_addr = mm->free_area_cache;
  199. } else {
  200. start_addr = TASK_UNMAPPED_BASE;
  201. mm->cached_hole_size = 0;
  202. }
  203. full_search:
  204. addr = ALIGN(start_addr, HPAGE_SIZE);
  205. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  206. /* At this point: (!vma || addr < vma->vm_end). */
  207. if (TASK_SIZE - len < addr) {
  208. /*
  209. * Start a new search - just in case we missed
  210. * some holes.
  211. */
  212. if (start_addr != TASK_UNMAPPED_BASE) {
  213. start_addr = TASK_UNMAPPED_BASE;
  214. mm->cached_hole_size = 0;
  215. goto full_search;
  216. }
  217. return -ENOMEM;
  218. }
  219. if (!vma || addr + len <= vma->vm_start) {
  220. mm->free_area_cache = addr + len;
  221. return addr;
  222. }
  223. if (addr + mm->cached_hole_size < vma->vm_start)
  224. mm->cached_hole_size = vma->vm_start - addr;
  225. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  226. }
  227. }
  228. static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
  229. unsigned long addr0, unsigned long len,
  230. unsigned long pgoff, unsigned long flags)
  231. {
  232. struct mm_struct *mm = current->mm;
  233. struct vm_area_struct *vma, *prev_vma;
  234. unsigned long base = mm->mmap_base, addr = addr0;
  235. unsigned long largest_hole = mm->cached_hole_size;
  236. int first_time = 1;
  237. /* don't allow allocations above current base */
  238. if (mm->free_area_cache > base)
  239. mm->free_area_cache = base;
  240. if (len <= largest_hole) {
  241. largest_hole = 0;
  242. mm->free_area_cache = base;
  243. }
  244. try_again:
  245. /* make sure it can fit in the remaining address space */
  246. if (mm->free_area_cache < len)
  247. goto fail;
  248. /* either no address requested or cant fit in requested address hole */
  249. addr = (mm->free_area_cache - len) & HPAGE_MASK;
  250. do {
  251. /*
  252. * Lookup failure means no vma is above this address,
  253. * i.e. return with success:
  254. */
  255. if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
  256. return addr;
  257. /*
  258. * new region fits between prev_vma->vm_end and
  259. * vma->vm_start, use it:
  260. */
  261. if (addr + len <= vma->vm_start &&
  262. (!prev_vma || (addr >= prev_vma->vm_end))) {
  263. /* remember the address as a hint for next time */
  264. mm->cached_hole_size = largest_hole;
  265. return (mm->free_area_cache = addr);
  266. } else {
  267. /* pull free_area_cache down to the first hole */
  268. if (mm->free_area_cache == vma->vm_end) {
  269. mm->free_area_cache = vma->vm_start;
  270. mm->cached_hole_size = largest_hole;
  271. }
  272. }
  273. /* remember the largest hole we saw so far */
  274. if (addr + largest_hole < vma->vm_start)
  275. largest_hole = vma->vm_start - addr;
  276. /* try just below the current vma->vm_start */
  277. addr = (vma->vm_start - len) & HPAGE_MASK;
  278. } while (len <= vma->vm_start);
  279. fail:
  280. /*
  281. * if hint left us with no space for the requested
  282. * mapping then try again:
  283. */
  284. if (first_time) {
  285. mm->free_area_cache = base;
  286. largest_hole = 0;
  287. first_time = 0;
  288. goto try_again;
  289. }
  290. /*
  291. * A failed mmap() very likely causes application failure,
  292. * so fall back to the bottom-up function here. This scenario
  293. * can happen with large stack limits and large mmap()
  294. * allocations.
  295. */
  296. mm->free_area_cache = TASK_UNMAPPED_BASE;
  297. mm->cached_hole_size = ~0UL;
  298. addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
  299. len, pgoff, flags);
  300. /*
  301. * Restore the topdown base:
  302. */
  303. mm->free_area_cache = base;
  304. mm->cached_hole_size = ~0UL;
  305. return addr;
  306. }
  307. unsigned long
  308. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  309. unsigned long len, unsigned long pgoff, unsigned long flags)
  310. {
  311. struct mm_struct *mm = current->mm;
  312. struct vm_area_struct *vma;
  313. if (len & ~HPAGE_MASK)
  314. return -EINVAL;
  315. if (len > TASK_SIZE)
  316. return -ENOMEM;
  317. if (flags & MAP_FIXED) {
  318. if (prepare_hugepage_range(addr, len))
  319. return -EINVAL;
  320. return addr;
  321. }
  322. if (addr) {
  323. addr = ALIGN(addr, HPAGE_SIZE);
  324. vma = find_vma(mm, addr);
  325. if (TASK_SIZE - len >= addr &&
  326. (!vma || addr + len <= vma->vm_start))
  327. return addr;
  328. }
  329. if (mm->get_unmapped_area == arch_get_unmapped_area)
  330. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  331. pgoff, flags);
  332. else
  333. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  334. pgoff, flags);
  335. }
  336. #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/