hugetlbpage.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * arch/arm64/mm/hugetlbpage.c
  3. *
  4. * Copyright (C) 2013 Linaro Ltd.
  5. *
  6. * Based on arch/x86/mm/hugetlbpage.c.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/fs.h>
  19. #include <linux/mm.h>
  20. #include <linux/hugetlb.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/err.h>
  23. #include <linux/sysctl.h>
  24. #include <asm/mman.h>
  25. #include <asm/tlb.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm/pgalloc.h>
  28. int pmd_huge(pmd_t pmd)
  29. {
  30. return pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT);
  31. }
  32. int pud_huge(pud_t pud)
  33. {
  34. #ifndef __PAGETABLE_PMD_FOLDED
  35. return pud_val(pud) && !(pud_val(pud) & PUD_TABLE_BIT);
  36. #else
  37. return 0;
  38. #endif
  39. }
  40. static int find_num_contig(struct mm_struct *mm, unsigned long addr,
  41. pte_t *ptep, size_t *pgsize)
  42. {
  43. pgd_t *pgd = pgd_offset(mm, addr);
  44. pud_t *pud;
  45. pmd_t *pmd;
  46. *pgsize = PAGE_SIZE;
  47. pud = pud_offset(pgd, addr);
  48. pmd = pmd_offset(pud, addr);
  49. if ((pte_t *)pmd == ptep) {
  50. *pgsize = PMD_SIZE;
  51. return CONT_PMDS;
  52. }
  53. return CONT_PTES;
  54. }
  55. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
  56. pte_t *ptep, pte_t pte)
  57. {
  58. size_t pgsize;
  59. int i;
  60. int ncontig;
  61. unsigned long pfn;
  62. pgprot_t hugeprot;
  63. if (!pte_cont(pte)) {
  64. set_pte_at(mm, addr, ptep, pte);
  65. return;
  66. }
  67. ncontig = find_num_contig(mm, addr, ptep, &pgsize);
  68. pfn = pte_pfn(pte);
  69. hugeprot = __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte));
  70. for (i = 0; i < ncontig; i++) {
  71. pr_debug("%s: set pte %p to 0x%llx\n", __func__, ptep,
  72. pte_val(pfn_pte(pfn, hugeprot)));
  73. set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
  74. ptep++;
  75. pfn += pgsize >> PAGE_SHIFT;
  76. addr += pgsize;
  77. }
  78. }
  79. pte_t *huge_pte_alloc(struct mm_struct *mm,
  80. unsigned long addr, unsigned long sz)
  81. {
  82. pgd_t *pgd;
  83. pud_t *pud;
  84. pte_t *pte = NULL;
  85. pr_debug("%s: addr:0x%lx sz:0x%lx\n", __func__, addr, sz);
  86. pgd = pgd_offset(mm, addr);
  87. pud = pud_alloc(mm, pgd, addr);
  88. if (!pud)
  89. return NULL;
  90. if (sz == PUD_SIZE) {
  91. pte = (pte_t *)pud;
  92. } else if (sz == (PAGE_SIZE * CONT_PTES)) {
  93. pmd_t *pmd = pmd_alloc(mm, pud, addr);
  94. WARN_ON(addr & (sz - 1));
  95. /*
  96. * Note that if this code were ever ported to the
  97. * 32-bit arm platform then it will cause trouble in
  98. * the case where CONFIG_HIGHPTE is set, since there
  99. * will be no pte_unmap() to correspond with this
  100. * pte_alloc_map().
  101. */
  102. pte = pte_alloc_map(mm, pmd, addr);
  103. } else if (sz == PMD_SIZE) {
  104. if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) &&
  105. pud_none(*pud))
  106. pte = huge_pmd_share(mm, addr, pud);
  107. else
  108. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  109. } else if (sz == (PMD_SIZE * CONT_PMDS)) {
  110. pmd_t *pmd;
  111. pmd = pmd_alloc(mm, pud, addr);
  112. WARN_ON(addr & (sz - 1));
  113. return (pte_t *)pmd;
  114. }
  115. pr_debug("%s: addr:0x%lx sz:0x%lx ret pte=%p/0x%llx\n", __func__, addr,
  116. sz, pte, pte_val(*pte));
  117. return pte;
  118. }
  119. pte_t *huge_pte_offset(struct mm_struct *mm,
  120. unsigned long addr, unsigned long sz)
  121. {
  122. pgd_t *pgd;
  123. pud_t *pud;
  124. pmd_t *pmd;
  125. pgd = pgd_offset(mm, addr);
  126. pr_debug("%s: addr:0x%lx pgd:%p\n", __func__, addr, pgd);
  127. if (!pgd_present(*pgd))
  128. return NULL;
  129. pud = pud_offset(pgd, addr);
  130. if (pud_none(*pud))
  131. return NULL;
  132. /* swap or huge page */
  133. if (!pud_present(*pud) || pud_huge(*pud))
  134. return (pte_t *)pud;
  135. /* table; check the next level */
  136. pmd = pmd_offset(pud, addr);
  137. if (pmd_none(*pmd))
  138. return NULL;
  139. if (!pmd_present(*pmd) || pmd_huge(*pmd))
  140. return (pte_t *)pmd;
  141. return NULL;
  142. }
  143. pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
  144. struct page *page, int writable)
  145. {
  146. size_t pagesize = huge_page_size(hstate_vma(vma));
  147. if (pagesize == CONT_PTE_SIZE) {
  148. entry = pte_mkcont(entry);
  149. } else if (pagesize == CONT_PMD_SIZE) {
  150. entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
  151. } else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
  152. pr_warn("%s: unrecognized huge page size 0x%lx\n",
  153. __func__, pagesize);
  154. }
  155. return entry;
  156. }
  157. pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
  158. unsigned long addr, pte_t *ptep)
  159. {
  160. pte_t pte;
  161. if (pte_cont(*ptep)) {
  162. int ncontig, i;
  163. size_t pgsize;
  164. bool is_dirty = false;
  165. ncontig = find_num_contig(mm, addr, ptep, &pgsize);
  166. /* save the 1st pte to return */
  167. pte = ptep_get_and_clear(mm, addr, ptep);
  168. for (i = 1, addr += pgsize; i < ncontig; ++i, addr += pgsize) {
  169. /*
  170. * If HW_AFDBM is enabled, then the HW could
  171. * turn on the dirty bit for any of the page
  172. * in the set, so check them all.
  173. */
  174. ++ptep;
  175. if (pte_dirty(ptep_get_and_clear(mm, addr, ptep)))
  176. is_dirty = true;
  177. }
  178. if (is_dirty)
  179. return pte_mkdirty(pte);
  180. else
  181. return pte;
  182. } else {
  183. return ptep_get_and_clear(mm, addr, ptep);
  184. }
  185. }
  186. int huge_ptep_set_access_flags(struct vm_area_struct *vma,
  187. unsigned long addr, pte_t *ptep,
  188. pte_t pte, int dirty)
  189. {
  190. if (pte_cont(pte)) {
  191. int ncontig, i, changed = 0;
  192. size_t pgsize = 0;
  193. unsigned long pfn = pte_pfn(pte);
  194. /* Select all bits except the pfn */
  195. pgprot_t hugeprot =
  196. __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^
  197. pte_val(pte));
  198. pfn = pte_pfn(pte);
  199. ncontig = find_num_contig(vma->vm_mm, addr, ptep,
  200. &pgsize);
  201. for (i = 0; i < ncontig; ++i, ++ptep, addr += pgsize) {
  202. changed |= ptep_set_access_flags(vma, addr, ptep,
  203. pfn_pte(pfn,
  204. hugeprot),
  205. dirty);
  206. pfn += pgsize >> PAGE_SHIFT;
  207. }
  208. return changed;
  209. } else {
  210. return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
  211. }
  212. }
  213. void huge_ptep_set_wrprotect(struct mm_struct *mm,
  214. unsigned long addr, pte_t *ptep)
  215. {
  216. if (pte_cont(*ptep)) {
  217. int ncontig, i;
  218. size_t pgsize = 0;
  219. ncontig = find_num_contig(mm, addr, ptep, &pgsize);
  220. for (i = 0; i < ncontig; ++i, ++ptep, addr += pgsize)
  221. ptep_set_wrprotect(mm, addr, ptep);
  222. } else {
  223. ptep_set_wrprotect(mm, addr, ptep);
  224. }
  225. }
  226. void huge_ptep_clear_flush(struct vm_area_struct *vma,
  227. unsigned long addr, pte_t *ptep)
  228. {
  229. if (pte_cont(*ptep)) {
  230. int ncontig, i;
  231. size_t pgsize = 0;
  232. ncontig = find_num_contig(vma->vm_mm, addr, ptep,
  233. &pgsize);
  234. for (i = 0; i < ncontig; ++i, ++ptep, addr += pgsize)
  235. ptep_clear_flush(vma, addr, ptep);
  236. } else {
  237. ptep_clear_flush(vma, addr, ptep);
  238. }
  239. }
  240. static __init int setup_hugepagesz(char *opt)
  241. {
  242. unsigned long ps = memparse(opt, &opt);
  243. if (ps == PMD_SIZE) {
  244. hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
  245. } else if (ps == PUD_SIZE) {
  246. hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
  247. } else {
  248. hugetlb_bad_size();
  249. pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10);
  250. return 0;
  251. }
  252. return 1;
  253. }
  254. __setup("hugepagesz=", setup_hugepagesz);