hugetlbpage.c 11 KB

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