hugetlbpage.c 7.8 KB

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