hugetlbpage.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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,
  112. unsigned long addr, unsigned long sz)
  113. {
  114. pgd_t *pgd;
  115. pud_t *pud;
  116. pte_t *pte = NULL;
  117. pgd = pgd_offset(mm, addr);
  118. pud = pud_alloc(mm, pgd, addr);
  119. if (pud) {
  120. if (pud_none(*pud))
  121. huge_pmd_share(mm, addr, pud);
  122. pte = (pte_t *) pmd_alloc(mm, pud, addr);
  123. }
  124. BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
  125. return pte;
  126. }
  127. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  128. {
  129. pgd_t *pgd;
  130. pud_t *pud;
  131. pmd_t *pmd = NULL;
  132. pgd = pgd_offset(mm, addr);
  133. if (pgd_present(*pgd)) {
  134. pud = pud_offset(pgd, addr);
  135. if (pud_present(*pud))
  136. pmd = pmd_offset(pud, addr);
  137. }
  138. return (pte_t *) pmd;
  139. }
  140. #if 0 /* This is just for testing */
  141. struct page *
  142. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  143. {
  144. unsigned long start = address;
  145. int length = 1;
  146. int nr;
  147. struct page *page;
  148. struct vm_area_struct *vma;
  149. vma = find_vma(mm, addr);
  150. if (!vma || !is_vm_hugetlb_page(vma))
  151. return ERR_PTR(-EINVAL);
  152. pte = huge_pte_offset(mm, address);
  153. /* hugetlb should be locked, and hence, prefaulted */
  154. WARN_ON(!pte || pte_none(*pte));
  155. page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
  156. WARN_ON(!PageHead(page));
  157. return page;
  158. }
  159. int pmd_huge(pmd_t pmd)
  160. {
  161. return 0;
  162. }
  163. struct page *
  164. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  165. pmd_t *pmd, int write)
  166. {
  167. return NULL;
  168. }
  169. #else
  170. struct page *
  171. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  172. {
  173. return ERR_PTR(-EINVAL);
  174. }
  175. int pmd_huge(pmd_t pmd)
  176. {
  177. return !!(pmd_val(pmd) & _PAGE_PSE);
  178. }
  179. struct page *
  180. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  181. pmd_t *pmd, int write)
  182. {
  183. struct page *page;
  184. page = pte_page(*(pte_t *)pmd);
  185. if (page)
  186. page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT);
  187. return page;
  188. }
  189. #endif
  190. /* x86_64 also uses this file */
  191. #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  192. static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
  193. unsigned long addr, unsigned long len,
  194. unsigned long pgoff, unsigned long flags)
  195. {
  196. struct mm_struct *mm = current->mm;
  197. struct vm_area_struct *vma;
  198. unsigned long start_addr;
  199. if (len > mm->cached_hole_size) {
  200. start_addr = mm->free_area_cache;
  201. } else {
  202. start_addr = TASK_UNMAPPED_BASE;
  203. mm->cached_hole_size = 0;
  204. }
  205. full_search:
  206. addr = ALIGN(start_addr, HPAGE_SIZE);
  207. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  208. /* At this point: (!vma || addr < vma->vm_end). */
  209. if (TASK_SIZE - len < addr) {
  210. /*
  211. * Start a new search - just in case we missed
  212. * some holes.
  213. */
  214. if (start_addr != TASK_UNMAPPED_BASE) {
  215. start_addr = TASK_UNMAPPED_BASE;
  216. mm->cached_hole_size = 0;
  217. goto full_search;
  218. }
  219. return -ENOMEM;
  220. }
  221. if (!vma || addr + len <= vma->vm_start) {
  222. mm->free_area_cache = addr + len;
  223. return addr;
  224. }
  225. if (addr + mm->cached_hole_size < vma->vm_start)
  226. mm->cached_hole_size = vma->vm_start - addr;
  227. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  228. }
  229. }
  230. static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
  231. unsigned long addr0, unsigned long len,
  232. unsigned long pgoff, unsigned long flags)
  233. {
  234. struct mm_struct *mm = current->mm;
  235. struct vm_area_struct *vma, *prev_vma;
  236. unsigned long base = mm->mmap_base, addr = addr0;
  237. unsigned long largest_hole = mm->cached_hole_size;
  238. int first_time = 1;
  239. /* don't allow allocations above current base */
  240. if (mm->free_area_cache > base)
  241. mm->free_area_cache = base;
  242. if (len <= largest_hole) {
  243. largest_hole = 0;
  244. mm->free_area_cache = base;
  245. }
  246. try_again:
  247. /* make sure it can fit in the remaining address space */
  248. if (mm->free_area_cache < len)
  249. goto fail;
  250. /* either no address requested or cant fit in requested address hole */
  251. addr = (mm->free_area_cache - len) & HPAGE_MASK;
  252. do {
  253. /*
  254. * Lookup failure means no vma is above this address,
  255. * i.e. return with success:
  256. */
  257. if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
  258. return addr;
  259. /*
  260. * new region fits between prev_vma->vm_end and
  261. * vma->vm_start, use it:
  262. */
  263. if (addr + len <= vma->vm_start &&
  264. (!prev_vma || (addr >= prev_vma->vm_end))) {
  265. /* remember the address as a hint for next time */
  266. mm->cached_hole_size = largest_hole;
  267. return (mm->free_area_cache = addr);
  268. } else {
  269. /* pull free_area_cache down to the first hole */
  270. if (mm->free_area_cache == vma->vm_end) {
  271. mm->free_area_cache = vma->vm_start;
  272. mm->cached_hole_size = largest_hole;
  273. }
  274. }
  275. /* remember the largest hole we saw so far */
  276. if (addr + largest_hole < vma->vm_start)
  277. largest_hole = vma->vm_start - addr;
  278. /* try just below the current vma->vm_start */
  279. addr = (vma->vm_start - len) & HPAGE_MASK;
  280. } while (len <= vma->vm_start);
  281. fail:
  282. /*
  283. * if hint left us with no space for the requested
  284. * mapping then try again:
  285. */
  286. if (first_time) {
  287. mm->free_area_cache = base;
  288. largest_hole = 0;
  289. first_time = 0;
  290. goto try_again;
  291. }
  292. /*
  293. * A failed mmap() very likely causes application failure,
  294. * so fall back to the bottom-up function here. This scenario
  295. * can happen with large stack limits and large mmap()
  296. * allocations.
  297. */
  298. mm->free_area_cache = TASK_UNMAPPED_BASE;
  299. mm->cached_hole_size = ~0UL;
  300. addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
  301. len, pgoff, flags);
  302. /*
  303. * Restore the topdown base:
  304. */
  305. mm->free_area_cache = base;
  306. mm->cached_hole_size = ~0UL;
  307. return addr;
  308. }
  309. unsigned long
  310. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  311. unsigned long len, unsigned long pgoff, unsigned long flags)
  312. {
  313. struct mm_struct *mm = current->mm;
  314. struct vm_area_struct *vma;
  315. if (len & ~HPAGE_MASK)
  316. return -EINVAL;
  317. if (len > TASK_SIZE)
  318. return -ENOMEM;
  319. if (flags & MAP_FIXED) {
  320. if (prepare_hugepage_range(file, addr, len))
  321. return -EINVAL;
  322. return addr;
  323. }
  324. if (addr) {
  325. addr = ALIGN(addr, HPAGE_SIZE);
  326. vma = find_vma(mm, addr);
  327. if (TASK_SIZE - len >= addr &&
  328. (!vma || addr + len <= vma->vm_start))
  329. return addr;
  330. }
  331. if (mm->get_unmapped_area == arch_get_unmapped_area)
  332. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  333. pgoff, flags);
  334. else
  335. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  336. pgoff, flags);
  337. }
  338. #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/