hugetlbpage.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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/err.h>
  12. #include <linux/sysctl.h>
  13. #include <asm/mman.h>
  14. #include <asm/tlb.h>
  15. #include <asm/tlbflush.h>
  16. #include <asm/pgalloc.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. /* Allow segments to share if only one is marked locked */
  26. unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED;
  27. unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED;
  28. /*
  29. * match the virtual addresses, permission and the alignment of the
  30. * page table page.
  31. */
  32. if (pmd_index(addr) != pmd_index(saddr) ||
  33. vm_flags != svm_flags ||
  34. sbase < svma->vm_start || svma->vm_end < s_end)
  35. return 0;
  36. return saddr;
  37. }
  38. static int vma_shareable(struct vm_area_struct *vma, unsigned long addr)
  39. {
  40. unsigned long base = addr & PUD_MASK;
  41. unsigned long end = base + PUD_SIZE;
  42. /*
  43. * check on proper vm_flags and page table alignment
  44. */
  45. if (vma->vm_flags & VM_MAYSHARE &&
  46. vma->vm_start <= base && end <= vma->vm_end)
  47. return 1;
  48. return 0;
  49. }
  50. /*
  51. * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
  52. * and returns the corresponding pte. While this is not necessary for the
  53. * !shared pmd case because we can allocate the pmd later as well, it makes the
  54. * code much cleaner. pmd allocation is essential for the shared case because
  55. * pud has to be populated inside the same i_mmap_mutex section - otherwise
  56. * racing tasks could either miss the sharing (see huge_pte_offset) or select a
  57. * bad pmd for sharing.
  58. */
  59. static pte_t *
  60. huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
  61. {
  62. struct vm_area_struct *vma = find_vma(mm, addr);
  63. struct address_space *mapping = vma->vm_file->f_mapping;
  64. pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
  65. vma->vm_pgoff;
  66. struct prio_tree_iter iter;
  67. struct vm_area_struct *svma;
  68. unsigned long saddr;
  69. pte_t *spte = NULL;
  70. pte_t *pte;
  71. if (!vma_shareable(vma, addr))
  72. return (pte_t *)pmd_alloc(mm, pud, addr);
  73. mutex_lock(&mapping->i_mmap_mutex);
  74. vma_prio_tree_foreach(svma, &iter, &mapping->i_mmap, idx, idx) {
  75. if (svma == vma)
  76. continue;
  77. saddr = page_table_shareable(svma, vma, addr, idx);
  78. if (saddr) {
  79. spte = huge_pte_offset(svma->vm_mm, saddr);
  80. if (spte) {
  81. get_page(virt_to_page(spte));
  82. break;
  83. }
  84. }
  85. }
  86. if (!spte)
  87. goto out;
  88. spin_lock(&mm->page_table_lock);
  89. if (pud_none(*pud))
  90. pud_populate(mm, pud, (pmd_t *)((unsigned long)spte & PAGE_MASK));
  91. else
  92. put_page(virt_to_page(spte));
  93. spin_unlock(&mm->page_table_lock);
  94. out:
  95. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  96. mutex_unlock(&mapping->i_mmap_mutex);
  97. return pte;
  98. }
  99. /*
  100. * unmap huge page backed by shared pte.
  101. *
  102. * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
  103. * indicated by page_count > 1, unmap is achieved by clearing pud and
  104. * decrementing the ref count. If count == 1, the pte page is not shared.
  105. *
  106. * called with vma->vm_mm->page_table_lock held.
  107. *
  108. * returns: 1 successfully unmapped a shared pte page
  109. * 0 the underlying pte page is not shared, or it is the last user
  110. */
  111. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  112. {
  113. pgd_t *pgd = pgd_offset(mm, *addr);
  114. pud_t *pud = pud_offset(pgd, *addr);
  115. BUG_ON(page_count(virt_to_page(ptep)) == 0);
  116. if (page_count(virt_to_page(ptep)) == 1)
  117. return 0;
  118. pud_clear(pud);
  119. put_page(virt_to_page(ptep));
  120. *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
  121. return 1;
  122. }
  123. pte_t *huge_pte_alloc(struct mm_struct *mm,
  124. unsigned long addr, unsigned long sz)
  125. {
  126. pgd_t *pgd;
  127. pud_t *pud;
  128. pte_t *pte = NULL;
  129. pgd = pgd_offset(mm, addr);
  130. pud = pud_alloc(mm, pgd, addr);
  131. if (pud) {
  132. if (sz == PUD_SIZE) {
  133. pte = (pte_t *)pud;
  134. } else {
  135. BUG_ON(sz != PMD_SIZE);
  136. if (pud_none(*pud))
  137. pte = huge_pmd_share(mm, addr, pud);
  138. else
  139. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  140. }
  141. }
  142. BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
  143. return pte;
  144. }
  145. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  146. {
  147. pgd_t *pgd;
  148. pud_t *pud;
  149. pmd_t *pmd = NULL;
  150. pgd = pgd_offset(mm, addr);
  151. if (pgd_present(*pgd)) {
  152. pud = pud_offset(pgd, addr);
  153. if (pud_present(*pud)) {
  154. if (pud_large(*pud))
  155. return (pte_t *)pud;
  156. pmd = pmd_offset(pud, addr);
  157. }
  158. }
  159. return (pte_t *) pmd;
  160. }
  161. #if 0 /* This is just for testing */
  162. struct page *
  163. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  164. {
  165. unsigned long start = address;
  166. int length = 1;
  167. int nr;
  168. struct page *page;
  169. struct vm_area_struct *vma;
  170. vma = find_vma(mm, addr);
  171. if (!vma || !is_vm_hugetlb_page(vma))
  172. return ERR_PTR(-EINVAL);
  173. pte = huge_pte_offset(mm, address);
  174. /* hugetlb should be locked, and hence, prefaulted */
  175. WARN_ON(!pte || pte_none(*pte));
  176. page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
  177. WARN_ON(!PageHead(page));
  178. return page;
  179. }
  180. int pmd_huge(pmd_t pmd)
  181. {
  182. return 0;
  183. }
  184. int pud_huge(pud_t pud)
  185. {
  186. return 0;
  187. }
  188. struct page *
  189. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  190. pmd_t *pmd, int write)
  191. {
  192. return NULL;
  193. }
  194. #else
  195. struct page *
  196. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  197. {
  198. return ERR_PTR(-EINVAL);
  199. }
  200. int pmd_huge(pmd_t pmd)
  201. {
  202. return !!(pmd_val(pmd) & _PAGE_PSE);
  203. }
  204. int pud_huge(pud_t pud)
  205. {
  206. return !!(pud_val(pud) & _PAGE_PSE);
  207. }
  208. struct page *
  209. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  210. pmd_t *pmd, int write)
  211. {
  212. struct page *page;
  213. page = pte_page(*(pte_t *)pmd);
  214. if (page)
  215. page += ((address & ~PMD_MASK) >> PAGE_SHIFT);
  216. return page;
  217. }
  218. struct page *
  219. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  220. pud_t *pud, int write)
  221. {
  222. struct page *page;
  223. page = pte_page(*(pte_t *)pud);
  224. if (page)
  225. page += ((address & ~PUD_MASK) >> PAGE_SHIFT);
  226. return page;
  227. }
  228. #endif
  229. /* x86_64 also uses this file */
  230. #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  231. static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
  232. unsigned long addr, unsigned long len,
  233. unsigned long pgoff, unsigned long flags)
  234. {
  235. struct hstate *h = hstate_file(file);
  236. struct mm_struct *mm = current->mm;
  237. struct vm_area_struct *vma;
  238. unsigned long start_addr;
  239. if (len > mm->cached_hole_size) {
  240. start_addr = mm->free_area_cache;
  241. } else {
  242. start_addr = TASK_UNMAPPED_BASE;
  243. mm->cached_hole_size = 0;
  244. }
  245. full_search:
  246. addr = ALIGN(start_addr, huge_page_size(h));
  247. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  248. /* At this point: (!vma || addr < vma->vm_end). */
  249. if (TASK_SIZE - len < addr) {
  250. /*
  251. * Start a new search - just in case we missed
  252. * some holes.
  253. */
  254. if (start_addr != TASK_UNMAPPED_BASE) {
  255. start_addr = TASK_UNMAPPED_BASE;
  256. mm->cached_hole_size = 0;
  257. goto full_search;
  258. }
  259. return -ENOMEM;
  260. }
  261. if (!vma || addr + len <= vma->vm_start) {
  262. mm->free_area_cache = addr + len;
  263. return addr;
  264. }
  265. if (addr + mm->cached_hole_size < vma->vm_start)
  266. mm->cached_hole_size = vma->vm_start - addr;
  267. addr = ALIGN(vma->vm_end, huge_page_size(h));
  268. }
  269. }
  270. static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
  271. unsigned long addr0, unsigned long len,
  272. unsigned long pgoff, unsigned long flags)
  273. {
  274. struct hstate *h = hstate_file(file);
  275. struct mm_struct *mm = current->mm;
  276. struct vm_area_struct *vma;
  277. unsigned long base = mm->mmap_base;
  278. unsigned long addr = addr0;
  279. unsigned long largest_hole = mm->cached_hole_size;
  280. unsigned long start_addr;
  281. /* don't allow allocations above current base */
  282. if (mm->free_area_cache > base)
  283. mm->free_area_cache = base;
  284. if (len <= largest_hole) {
  285. largest_hole = 0;
  286. mm->free_area_cache = base;
  287. }
  288. try_again:
  289. start_addr = mm->free_area_cache;
  290. /* make sure it can fit in the remaining address space */
  291. if (mm->free_area_cache < len)
  292. goto fail;
  293. /* either no address requested or can't fit in requested address hole */
  294. addr = (mm->free_area_cache - len) & huge_page_mask(h);
  295. do {
  296. /*
  297. * Lookup failure means no vma is above this address,
  298. * i.e. return with success:
  299. */
  300. vma = find_vma(mm, addr);
  301. if (!vma)
  302. return addr;
  303. if (addr + len <= vma->vm_start) {
  304. /* remember the address as a hint for next time */
  305. mm->cached_hole_size = largest_hole;
  306. return (mm->free_area_cache = addr);
  307. } else if (mm->free_area_cache == vma->vm_end) {
  308. /* pull free_area_cache down to the first hole */
  309. mm->free_area_cache = vma->vm_start;
  310. mm->cached_hole_size = largest_hole;
  311. }
  312. /* remember the largest hole we saw so far */
  313. if (addr + largest_hole < vma->vm_start)
  314. largest_hole = vma->vm_start - addr;
  315. /* try just below the current vma->vm_start */
  316. addr = (vma->vm_start - len) & huge_page_mask(h);
  317. } while (len <= vma->vm_start);
  318. fail:
  319. /*
  320. * if hint left us with no space for the requested
  321. * mapping then try again:
  322. */
  323. if (start_addr != base) {
  324. mm->free_area_cache = base;
  325. largest_hole = 0;
  326. goto try_again;
  327. }
  328. /*
  329. * A failed mmap() very likely causes application failure,
  330. * so fall back to the bottom-up function here. This scenario
  331. * can happen with large stack limits and large mmap()
  332. * allocations.
  333. */
  334. mm->free_area_cache = TASK_UNMAPPED_BASE;
  335. mm->cached_hole_size = ~0UL;
  336. addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
  337. len, pgoff, flags);
  338. /*
  339. * Restore the topdown base:
  340. */
  341. mm->free_area_cache = base;
  342. mm->cached_hole_size = ~0UL;
  343. return addr;
  344. }
  345. unsigned long
  346. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  347. unsigned long len, unsigned long pgoff, unsigned long flags)
  348. {
  349. struct hstate *h = hstate_file(file);
  350. struct mm_struct *mm = current->mm;
  351. struct vm_area_struct *vma;
  352. if (len & ~huge_page_mask(h))
  353. return -EINVAL;
  354. if (len > TASK_SIZE)
  355. return -ENOMEM;
  356. if (flags & MAP_FIXED) {
  357. if (prepare_hugepage_range(file, addr, len))
  358. return -EINVAL;
  359. return addr;
  360. }
  361. if (addr) {
  362. addr = ALIGN(addr, huge_page_size(h));
  363. vma = find_vma(mm, addr);
  364. if (TASK_SIZE - len >= addr &&
  365. (!vma || addr + len <= vm_start_gap(vma)))
  366. return addr;
  367. }
  368. if (mm->get_unmapped_area == arch_get_unmapped_area)
  369. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  370. pgoff, flags);
  371. else
  372. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  373. pgoff, flags);
  374. }
  375. #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/
  376. #ifdef CONFIG_X86_64
  377. static __init int setup_hugepagesz(char *opt)
  378. {
  379. unsigned long ps = memparse(opt, &opt);
  380. if (ps == PMD_SIZE) {
  381. hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
  382. } else if (ps == PUD_SIZE && cpu_has_gbpages) {
  383. hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
  384. } else {
  385. printk(KERN_ERR "hugepagesz: Unsupported page size %lu M\n",
  386. ps >> 20);
  387. return 0;
  388. }
  389. return 1;
  390. }
  391. __setup("hugepagesz=", setup_hugepagesz);
  392. #endif