hugetlbpage.c 9.9 KB

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