hugetlbpage.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. if (!pmdp)
  191. return NULL;
  192. WARN_ON(addr & (sz - 1));
  193. /*
  194. * Note that if this code were ever ported to the
  195. * 32-bit arm platform then it will cause trouble in
  196. * the case where CONFIG_HIGHPTE is set, since there
  197. * will be no pte_unmap() to correspond with this
  198. * pte_alloc_map().
  199. */
  200. ptep = pte_alloc_map(mm, pmdp, addr);
  201. } else if (sz == PMD_SIZE) {
  202. if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) &&
  203. pud_none(READ_ONCE(*pudp)))
  204. ptep = huge_pmd_share(mm, addr, pudp);
  205. else
  206. ptep = (pte_t *)pmd_alloc(mm, pudp, addr);
  207. } else if (sz == (PMD_SIZE * CONT_PMDS)) {
  208. pmdp = pmd_alloc(mm, pudp, addr);
  209. WARN_ON(addr & (sz - 1));
  210. return (pte_t *)pmdp;
  211. }
  212. return ptep;
  213. }
  214. pte_t *huge_pte_offset(struct mm_struct *mm,
  215. unsigned long addr, unsigned long sz)
  216. {
  217. pgd_t *pgdp;
  218. pud_t *pudp, pud;
  219. pmd_t *pmdp, pmd;
  220. pgdp = pgd_offset(mm, addr);
  221. if (!pgd_present(READ_ONCE(*pgdp)))
  222. return NULL;
  223. pudp = pud_offset(pgdp, addr);
  224. pud = READ_ONCE(*pudp);
  225. if (sz != PUD_SIZE && pud_none(pud))
  226. return NULL;
  227. /* hugepage or swap? */
  228. if (pud_huge(pud) || !pud_present(pud))
  229. return (pte_t *)pudp;
  230. /* table; check the next level */
  231. if (sz == CONT_PMD_SIZE)
  232. addr &= CONT_PMD_MASK;
  233. pmdp = pmd_offset(pudp, addr);
  234. pmd = READ_ONCE(*pmdp);
  235. if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
  236. pmd_none(pmd))
  237. return NULL;
  238. if (pmd_huge(pmd) || !pmd_present(pmd))
  239. return (pte_t *)pmdp;
  240. if (sz == CONT_PTE_SIZE)
  241. return pte_offset_kernel(pmdp, (addr & CONT_PTE_MASK));
  242. return NULL;
  243. }
  244. pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
  245. struct page *page, int writable)
  246. {
  247. size_t pagesize = huge_page_size(hstate_vma(vma));
  248. if (pagesize == CONT_PTE_SIZE) {
  249. entry = pte_mkcont(entry);
  250. } else if (pagesize == CONT_PMD_SIZE) {
  251. entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
  252. } else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
  253. pr_warn("%s: unrecognized huge page size 0x%lx\n",
  254. __func__, pagesize);
  255. }
  256. return entry;
  257. }
  258. void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
  259. pte_t *ptep, unsigned long sz)
  260. {
  261. int i, ncontig;
  262. size_t pgsize;
  263. ncontig = num_contig_ptes(sz, &pgsize);
  264. for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
  265. pte_clear(mm, addr, ptep);
  266. }
  267. pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
  268. unsigned long addr, pte_t *ptep)
  269. {
  270. int ncontig;
  271. size_t pgsize;
  272. pte_t orig_pte = huge_ptep_get(ptep);
  273. if (!pte_cont(orig_pte))
  274. return ptep_get_and_clear(mm, addr, ptep);
  275. ncontig = find_num_contig(mm, addr, ptep, &pgsize);
  276. return get_clear_flush(mm, addr, ptep, pgsize, ncontig);
  277. }
  278. /*
  279. * huge_ptep_set_access_flags will update access flags (dirty, accesssed)
  280. * and write permission.
  281. *
  282. * For a contiguous huge pte range we need to check whether or not write
  283. * permission has to change only on the first pte in the set. Then for
  284. * all the contiguous ptes we need to check whether or not there is a
  285. * discrepancy between dirty or young.
  286. */
  287. static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig)
  288. {
  289. int i;
  290. if (pte_write(pte) != pte_write(huge_ptep_get(ptep)))
  291. return 1;
  292. for (i = 0; i < ncontig; i++) {
  293. pte_t orig_pte = huge_ptep_get(ptep + i);
  294. if (pte_dirty(pte) != pte_dirty(orig_pte))
  295. return 1;
  296. if (pte_young(pte) != pte_young(orig_pte))
  297. return 1;
  298. }
  299. return 0;
  300. }
  301. int huge_ptep_set_access_flags(struct vm_area_struct *vma,
  302. unsigned long addr, pte_t *ptep,
  303. pte_t pte, int dirty)
  304. {
  305. int ncontig, i;
  306. size_t pgsize = 0;
  307. unsigned long pfn = pte_pfn(pte), dpfn;
  308. pgprot_t hugeprot;
  309. pte_t orig_pte;
  310. if (!pte_cont(pte))
  311. return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
  312. ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
  313. dpfn = pgsize >> PAGE_SHIFT;
  314. if (!__cont_access_flags_changed(ptep, pte, ncontig))
  315. return 0;
  316. orig_pte = get_clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
  317. /* Make sure we don't lose the dirty or young state */
  318. if (pte_dirty(orig_pte))
  319. pte = pte_mkdirty(pte);
  320. if (pte_young(orig_pte))
  321. pte = pte_mkyoung(pte);
  322. hugeprot = pte_pgprot(pte);
  323. for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
  324. set_pte_at(vma->vm_mm, addr, ptep, pfn_pte(pfn, hugeprot));
  325. return 1;
  326. }
  327. void huge_ptep_set_wrprotect(struct mm_struct *mm,
  328. unsigned long addr, pte_t *ptep)
  329. {
  330. unsigned long pfn, dpfn;
  331. pgprot_t hugeprot;
  332. int ncontig, i;
  333. size_t pgsize;
  334. pte_t pte;
  335. if (!pte_cont(READ_ONCE(*ptep))) {
  336. ptep_set_wrprotect(mm, addr, ptep);
  337. return;
  338. }
  339. ncontig = find_num_contig(mm, addr, ptep, &pgsize);
  340. dpfn = pgsize >> PAGE_SHIFT;
  341. pte = get_clear_flush(mm, addr, ptep, pgsize, ncontig);
  342. pte = pte_wrprotect(pte);
  343. hugeprot = pte_pgprot(pte);
  344. pfn = pte_pfn(pte);
  345. for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
  346. set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
  347. }
  348. void huge_ptep_clear_flush(struct vm_area_struct *vma,
  349. unsigned long addr, pte_t *ptep)
  350. {
  351. size_t pgsize;
  352. int ncontig;
  353. if (!pte_cont(READ_ONCE(*ptep))) {
  354. ptep_clear_flush(vma, addr, ptep);
  355. return;
  356. }
  357. ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
  358. clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
  359. }
  360. static __init int setup_hugepagesz(char *opt)
  361. {
  362. unsigned long ps = memparse(opt, &opt);
  363. switch (ps) {
  364. #ifdef CONFIG_ARM64_4K_PAGES
  365. case PUD_SIZE:
  366. #endif
  367. case PMD_SIZE * CONT_PMDS:
  368. case PMD_SIZE:
  369. case PAGE_SIZE * CONT_PTES:
  370. hugetlb_add_hstate(ilog2(ps) - PAGE_SHIFT);
  371. return 1;
  372. }
  373. hugetlb_bad_size();
  374. pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10);
  375. return 0;
  376. }
  377. __setup("hugepagesz=", setup_hugepagesz);
  378. #ifdef CONFIG_ARM64_64K_PAGES
  379. static __init int add_default_hugepagesz(void)
  380. {
  381. if (size_to_hstate(CONT_PTES * PAGE_SIZE) == NULL)
  382. hugetlb_add_hstate(CONT_PTE_SHIFT);
  383. return 0;
  384. }
  385. arch_initcall(add_default_hugepagesz);
  386. #endif