hugetlbpage.c 9.3 KB

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