hugetlbpage.c 10 KB

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