hugetlbpage.c 11 KB

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