hugetlbpage.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. /*
  41. * Select all bits except the pfn
  42. */
  43. static inline pgprot_t pte_pgprot(pte_t pte)
  44. {
  45. unsigned long pfn = pte_pfn(pte);
  46. return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte));
  47. }
  48. static int find_num_contig(struct mm_struct *mm, unsigned long addr,
  49. pte_t *ptep, size_t *pgsize)
  50. {
  51. pgd_t *pgd = pgd_offset(mm, addr);
  52. pud_t *pud;
  53. pmd_t *pmd;
  54. *pgsize = PAGE_SIZE;
  55. pud = pud_offset(pgd, addr);
  56. pmd = pmd_offset(pud, addr);
  57. if ((pte_t *)pmd == ptep) {
  58. *pgsize = PMD_SIZE;
  59. return CONT_PMDS;
  60. }
  61. return CONT_PTES;
  62. }
  63. /*
  64. * Changing some bits of contiguous entries requires us to follow a
  65. * Break-Before-Make approach, breaking the whole contiguous set
  66. * before we can change any entries. See ARM DDI 0487A.k_iss10775,
  67. * "Misprogramming of the Contiguous bit", page D4-1762.
  68. *
  69. * This helper performs the break step.
  70. */
  71. static pte_t get_clear_flush(struct mm_struct *mm,
  72. unsigned long addr,
  73. pte_t *ptep,
  74. unsigned long pgsize,
  75. unsigned long ncontig)
  76. {
  77. struct vm_area_struct vma = { .vm_mm = mm };
  78. pte_t orig_pte = huge_ptep_get(ptep);
  79. bool valid = pte_valid(orig_pte);
  80. unsigned long i, saddr = addr;
  81. for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
  82. pte_t pte = ptep_get_and_clear(mm, addr, ptep);
  83. /*
  84. * If HW_AFDBM is enabled, then the HW could turn on
  85. * the dirty bit for any page in the set, so check
  86. * them all. All hugetlb entries are already young.
  87. */
  88. if (pte_dirty(pte))
  89. orig_pte = pte_mkdirty(orig_pte);
  90. }
  91. if (valid)
  92. flush_tlb_range(&vma, saddr, addr);
  93. return orig_pte;
  94. }
  95. /*
  96. * Changing some bits of contiguous entries requires us to follow a
  97. * Break-Before-Make approach, breaking the whole contiguous set
  98. * before we can change any entries. See ARM DDI 0487A.k_iss10775,
  99. * "Misprogramming of the Contiguous bit", page D4-1762.
  100. *
  101. * This helper performs the break step for use cases where the
  102. * original pte is not needed.
  103. */
  104. static void clear_flush(struct mm_struct *mm,
  105. unsigned long addr,
  106. pte_t *ptep,
  107. unsigned long pgsize,
  108. unsigned long ncontig)
  109. {
  110. struct vm_area_struct vma = { .vm_mm = mm };
  111. unsigned long i, saddr = addr;
  112. for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
  113. pte_clear(mm, addr, ptep);
  114. flush_tlb_range(&vma, saddr, addr);
  115. }
  116. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
  117. pte_t *ptep, pte_t pte)
  118. {
  119. size_t pgsize;
  120. int i;
  121. int ncontig;
  122. unsigned long pfn, dpfn;
  123. pgprot_t hugeprot;
  124. /*
  125. * Code needs to be expanded to handle huge swap and migration
  126. * entries. Needed for HUGETLB and MEMORY_FAILURE.
  127. */
  128. WARN_ON(!pte_present(pte));
  129. if (!pte_cont(pte)) {
  130. set_pte_at(mm, addr, ptep, pte);
  131. return;
  132. }
  133. ncontig = find_num_contig(mm, addr, ptep, &pgsize);
  134. pfn = pte_pfn(pte);
  135. dpfn = pgsize >> PAGE_SHIFT;
  136. hugeprot = pte_pgprot(pte);
  137. clear_flush(mm, addr, ptep, pgsize, ncontig);
  138. for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) {
  139. pr_debug("%s: set pte %p to 0x%llx\n", __func__, ptep,
  140. pte_val(pfn_pte(pfn, hugeprot)));
  141. set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
  142. }
  143. }
  144. pte_t *huge_pte_alloc(struct mm_struct *mm,
  145. unsigned long addr, unsigned long sz)
  146. {
  147. pgd_t *pgd;
  148. pud_t *pud;
  149. pte_t *pte = NULL;
  150. pr_debug("%s: addr:0x%lx sz:0x%lx\n", __func__, addr, sz);
  151. pgd = pgd_offset(mm, addr);
  152. pud = pud_alloc(mm, pgd, addr);
  153. if (!pud)
  154. return NULL;
  155. if (sz == PUD_SIZE) {
  156. pte = (pte_t *)pud;
  157. } else if (sz == (PAGE_SIZE * CONT_PTES)) {
  158. pmd_t *pmd = pmd_alloc(mm, pud, addr);
  159. WARN_ON(addr & (sz - 1));
  160. /*
  161. * Note that if this code were ever ported to the
  162. * 32-bit arm platform then it will cause trouble in
  163. * the case where CONFIG_HIGHPTE is set, since there
  164. * will be no pte_unmap() to correspond with this
  165. * pte_alloc_map().
  166. */
  167. pte = pte_alloc_map(mm, pmd, addr);
  168. } else if (sz == PMD_SIZE) {
  169. if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) &&
  170. pud_none(*pud))
  171. pte = huge_pmd_share(mm, addr, pud);
  172. else
  173. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  174. } else if (sz == (PMD_SIZE * CONT_PMDS)) {
  175. pmd_t *pmd;
  176. pmd = pmd_alloc(mm, pud, addr);
  177. WARN_ON(addr & (sz - 1));
  178. return (pte_t *)pmd;
  179. }
  180. pr_debug("%s: addr:0x%lx sz:0x%lx ret pte=%p/0x%llx\n", __func__, addr,
  181. sz, pte, pte_val(*pte));
  182. return pte;
  183. }
  184. pte_t *huge_pte_offset(struct mm_struct *mm,
  185. unsigned long addr, unsigned long sz)
  186. {
  187. pgd_t *pgd;
  188. pud_t *pud;
  189. pmd_t *pmd;
  190. pgd = pgd_offset(mm, addr);
  191. pr_debug("%s: addr:0x%lx pgd:%p\n", __func__, addr, pgd);
  192. if (!pgd_present(*pgd))
  193. return NULL;
  194. pud = pud_offset(pgd, addr);
  195. if (sz != PUD_SIZE && pud_none(*pud))
  196. return NULL;
  197. /* hugepage or swap? */
  198. if (pud_huge(*pud) || !pud_present(*pud))
  199. return (pte_t *)pud;
  200. /* table; check the next level */
  201. if (sz == CONT_PMD_SIZE)
  202. addr &= CONT_PMD_MASK;
  203. pmd = pmd_offset(pud, addr);
  204. if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
  205. pmd_none(*pmd))
  206. return NULL;
  207. if (pmd_huge(*pmd) || !pmd_present(*pmd))
  208. return (pte_t *)pmd;
  209. if (sz == CONT_PTE_SIZE) {
  210. pte_t *pte = pte_offset_kernel(pmd, (addr & CONT_PTE_MASK));
  211. return pte;
  212. }
  213. return NULL;
  214. }
  215. pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
  216. struct page *page, int writable)
  217. {
  218. size_t pagesize = huge_page_size(hstate_vma(vma));
  219. if (pagesize == CONT_PTE_SIZE) {
  220. entry = pte_mkcont(entry);
  221. } else if (pagesize == CONT_PMD_SIZE) {
  222. entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
  223. } else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
  224. pr_warn("%s: unrecognized huge page size 0x%lx\n",
  225. __func__, pagesize);
  226. }
  227. return entry;
  228. }
  229. pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
  230. unsigned long addr, pte_t *ptep)
  231. {
  232. int ncontig;
  233. size_t pgsize;
  234. pte_t orig_pte = huge_ptep_get(ptep);
  235. if (!pte_cont(orig_pte))
  236. return ptep_get_and_clear(mm, addr, ptep);
  237. ncontig = find_num_contig(mm, addr, ptep, &pgsize);
  238. return get_clear_flush(mm, addr, ptep, pgsize, ncontig);
  239. }
  240. int huge_ptep_set_access_flags(struct vm_area_struct *vma,
  241. unsigned long addr, pte_t *ptep,
  242. pte_t pte, int dirty)
  243. {
  244. int ncontig, i, changed = 0;
  245. size_t pgsize = 0;
  246. unsigned long pfn = pte_pfn(pte), dpfn;
  247. pgprot_t hugeprot;
  248. pte_t orig_pte;
  249. if (!pte_cont(pte))
  250. return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
  251. ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
  252. dpfn = pgsize >> PAGE_SHIFT;
  253. orig_pte = get_clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
  254. if (!pte_same(orig_pte, pte))
  255. changed = 1;
  256. /* Make sure we don't lose the dirty state */
  257. if (pte_dirty(orig_pte))
  258. pte = pte_mkdirty(pte);
  259. hugeprot = pte_pgprot(pte);
  260. for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
  261. set_pte_at(vma->vm_mm, addr, ptep, pfn_pte(pfn, hugeprot));
  262. return changed;
  263. }
  264. void huge_ptep_set_wrprotect(struct mm_struct *mm,
  265. unsigned long addr, pte_t *ptep)
  266. {
  267. unsigned long pfn, dpfn;
  268. pgprot_t hugeprot;
  269. int ncontig, i;
  270. size_t pgsize;
  271. pte_t pte;
  272. if (!pte_cont(*ptep)) {
  273. ptep_set_wrprotect(mm, addr, ptep);
  274. return;
  275. }
  276. ncontig = find_num_contig(mm, addr, ptep, &pgsize);
  277. dpfn = pgsize >> PAGE_SHIFT;
  278. pte = get_clear_flush(mm, addr, ptep, pgsize, ncontig);
  279. pte = pte_wrprotect(pte);
  280. hugeprot = pte_pgprot(pte);
  281. pfn = pte_pfn(pte);
  282. for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
  283. set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
  284. }
  285. void huge_ptep_clear_flush(struct vm_area_struct *vma,
  286. unsigned long addr, pte_t *ptep)
  287. {
  288. size_t pgsize;
  289. int ncontig;
  290. if (!pte_cont(*ptep)) {
  291. ptep_clear_flush(vma, addr, ptep);
  292. return;
  293. }
  294. ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
  295. clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
  296. }
  297. static __init int setup_hugepagesz(char *opt)
  298. {
  299. unsigned long ps = memparse(opt, &opt);
  300. if (ps == PMD_SIZE) {
  301. hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
  302. } else if (ps == PUD_SIZE) {
  303. hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
  304. } else {
  305. hugetlb_bad_size();
  306. pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10);
  307. return 0;
  308. }
  309. return 1;
  310. }
  311. __setup("hugepagesz=", setup_hugepagesz);