hugetlbpage.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /*
  64. * Code needs to be expanded to handle huge swap and migration
  65. * entries. Needed for HUGETLB and MEMORY_FAILURE.
  66. */
  67. WARN_ON(!pte_present(pte));
  68. if (!pte_cont(pte)) {
  69. set_pte_at(mm, addr, ptep, pte);
  70. return;
  71. }
  72. ncontig = find_num_contig(mm, addr, ptep, &pgsize);
  73. pfn = pte_pfn(pte);
  74. hugeprot = __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte));
  75. for (i = 0; i < ncontig; i++) {
  76. pr_debug("%s: set pte %p to 0x%llx\n", __func__, ptep,
  77. pte_val(pfn_pte(pfn, hugeprot)));
  78. set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
  79. ptep++;
  80. pfn += pgsize >> PAGE_SHIFT;
  81. addr += pgsize;
  82. }
  83. }
  84. pte_t *huge_pte_alloc(struct mm_struct *mm,
  85. unsigned long addr, unsigned long sz)
  86. {
  87. pgd_t *pgd;
  88. pud_t *pud;
  89. pte_t *pte = NULL;
  90. pr_debug("%s: addr:0x%lx sz:0x%lx\n", __func__, addr, sz);
  91. pgd = pgd_offset(mm, addr);
  92. pud = pud_alloc(mm, pgd, addr);
  93. if (!pud)
  94. return NULL;
  95. if (sz == PUD_SIZE) {
  96. pte = (pte_t *)pud;
  97. } else if (sz == (PAGE_SIZE * CONT_PTES)) {
  98. pmd_t *pmd = pmd_alloc(mm, pud, addr);
  99. WARN_ON(addr & (sz - 1));
  100. /*
  101. * Note that if this code were ever ported to the
  102. * 32-bit arm platform then it will cause trouble in
  103. * the case where CONFIG_HIGHPTE is set, since there
  104. * will be no pte_unmap() to correspond with this
  105. * pte_alloc_map().
  106. */
  107. pte = pte_alloc_map(mm, pmd, addr);
  108. } else if (sz == PMD_SIZE) {
  109. if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) &&
  110. pud_none(*pud))
  111. pte = huge_pmd_share(mm, addr, pud);
  112. else
  113. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  114. } else if (sz == (PMD_SIZE * CONT_PMDS)) {
  115. pmd_t *pmd;
  116. pmd = pmd_alloc(mm, pud, addr);
  117. WARN_ON(addr & (sz - 1));
  118. return (pte_t *)pmd;
  119. }
  120. pr_debug("%s: addr:0x%lx sz:0x%lx ret pte=%p/0x%llx\n", __func__, addr,
  121. sz, pte, pte_val(*pte));
  122. return pte;
  123. }
  124. pte_t *huge_pte_offset(struct mm_struct *mm,
  125. unsigned long addr, unsigned long sz)
  126. {
  127. pgd_t *pgd;
  128. pud_t *pud;
  129. pmd_t *pmd;
  130. pgd = pgd_offset(mm, addr);
  131. pr_debug("%s: addr:0x%lx pgd:%p\n", __func__, addr, pgd);
  132. if (!pgd_present(*pgd))
  133. return NULL;
  134. pud = pud_offset(pgd, addr);
  135. if (pud_none(*pud))
  136. return NULL;
  137. /* swap or huge page */
  138. if (!pud_present(*pud) || pud_huge(*pud))
  139. return (pte_t *)pud;
  140. /* table; check the next level */
  141. pmd = pmd_offset(pud, addr);
  142. if (pmd_none(*pmd))
  143. return NULL;
  144. if (!pmd_present(*pmd) || pmd_huge(*pmd))
  145. return (pte_t *)pmd;
  146. return NULL;
  147. }
  148. pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
  149. struct page *page, int writable)
  150. {
  151. size_t pagesize = huge_page_size(hstate_vma(vma));
  152. if (pagesize == CONT_PTE_SIZE) {
  153. entry = pte_mkcont(entry);
  154. } else if (pagesize == CONT_PMD_SIZE) {
  155. entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
  156. } else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
  157. pr_warn("%s: unrecognized huge page size 0x%lx\n",
  158. __func__, pagesize);
  159. }
  160. return entry;
  161. }
  162. pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
  163. unsigned long addr, pte_t *ptep)
  164. {
  165. pte_t pte;
  166. if (pte_cont(*ptep)) {
  167. int ncontig, i;
  168. size_t pgsize;
  169. bool is_dirty = false;
  170. ncontig = find_num_contig(mm, addr, ptep, &pgsize);
  171. /* save the 1st pte to return */
  172. pte = ptep_get_and_clear(mm, addr, ptep);
  173. for (i = 1, addr += pgsize; i < ncontig; ++i, addr += pgsize) {
  174. /*
  175. * If HW_AFDBM is enabled, then the HW could
  176. * turn on the dirty bit for any of the page
  177. * in the set, so check them all.
  178. */
  179. ++ptep;
  180. if (pte_dirty(ptep_get_and_clear(mm, addr, ptep)))
  181. is_dirty = true;
  182. }
  183. if (is_dirty)
  184. return pte_mkdirty(pte);
  185. else
  186. return pte;
  187. } else {
  188. return ptep_get_and_clear(mm, addr, ptep);
  189. }
  190. }
  191. int huge_ptep_set_access_flags(struct vm_area_struct *vma,
  192. unsigned long addr, pte_t *ptep,
  193. pte_t pte, int dirty)
  194. {
  195. if (pte_cont(pte)) {
  196. int ncontig, i, changed = 0;
  197. size_t pgsize = 0;
  198. unsigned long pfn = pte_pfn(pte);
  199. /* Select all bits except the pfn */
  200. pgprot_t hugeprot =
  201. __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^
  202. pte_val(pte));
  203. pfn = pte_pfn(pte);
  204. ncontig = find_num_contig(vma->vm_mm, addr, ptep,
  205. &pgsize);
  206. for (i = 0; i < ncontig; ++i, ++ptep, addr += pgsize) {
  207. changed |= ptep_set_access_flags(vma, addr, ptep,
  208. pfn_pte(pfn,
  209. hugeprot),
  210. dirty);
  211. pfn += pgsize >> PAGE_SHIFT;
  212. }
  213. return changed;
  214. } else {
  215. return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
  216. }
  217. }
  218. void huge_ptep_set_wrprotect(struct mm_struct *mm,
  219. unsigned long addr, pte_t *ptep)
  220. {
  221. if (pte_cont(*ptep)) {
  222. int ncontig, i;
  223. size_t pgsize = 0;
  224. ncontig = find_num_contig(mm, addr, ptep, &pgsize);
  225. for (i = 0; i < ncontig; ++i, ++ptep, addr += pgsize)
  226. ptep_set_wrprotect(mm, addr, ptep);
  227. } else {
  228. ptep_set_wrprotect(mm, addr, ptep);
  229. }
  230. }
  231. void huge_ptep_clear_flush(struct vm_area_struct *vma,
  232. unsigned long addr, pte_t *ptep)
  233. {
  234. if (pte_cont(*ptep)) {
  235. int ncontig, i;
  236. size_t pgsize = 0;
  237. ncontig = find_num_contig(vma->vm_mm, addr, ptep,
  238. &pgsize);
  239. for (i = 0; i < ncontig; ++i, ++ptep, addr += pgsize)
  240. ptep_clear_flush(vma, addr, ptep);
  241. } else {
  242. ptep_clear_flush(vma, addr, ptep);
  243. }
  244. }
  245. static __init int setup_hugepagesz(char *opt)
  246. {
  247. unsigned long ps = memparse(opt, &opt);
  248. if (ps == PMD_SIZE) {
  249. hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
  250. } else if (ps == PUD_SIZE) {
  251. hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
  252. } else {
  253. hugetlb_bad_size();
  254. pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10);
  255. return 0;
  256. }
  257. return 1;
  258. }
  259. __setup("hugepagesz=", setup_hugepagesz);