contpte.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2023 ARM Ltd.
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/efi.h>
  7. #include <linux/export.h>
  8. #include <asm/tlbflush.h>
  9. static inline bool mm_is_user(struct mm_struct *mm)
  10. {
  11. /*
  12. * Don't attempt to apply the contig bit to kernel mappings, because
  13. * dynamically adding/removing the contig bit can cause page faults.
  14. * These racing faults are ok for user space, since they get serialized
  15. * on the PTL. But kernel mappings can't tolerate faults.
  16. */
  17. if (unlikely(mm_is_efi(mm)))
  18. return false;
  19. return mm != &init_mm;
  20. }
  21. static inline pte_t *contpte_align_down(pte_t *ptep)
  22. {
  23. return PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES);
  24. }
  25. static void contpte_try_unfold_partial(struct mm_struct *mm, unsigned long addr,
  26. pte_t *ptep, unsigned int nr)
  27. {
  28. /*
  29. * Unfold any partially covered contpte block at the beginning and end
  30. * of the range.
  31. */
  32. if (ptep != contpte_align_down(ptep) || nr < CONT_PTES)
  33. contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep));
  34. if (ptep + nr != contpte_align_down(ptep + nr)) {
  35. unsigned long last_addr = addr + PAGE_SIZE * (nr - 1);
  36. pte_t *last_ptep = ptep + nr - 1;
  37. contpte_try_unfold(mm, last_addr, last_ptep,
  38. __ptep_get(last_ptep));
  39. }
  40. }
  41. static void contpte_convert(struct mm_struct *mm, unsigned long addr,
  42. pte_t *ptep, pte_t pte)
  43. {
  44. struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
  45. unsigned long start_addr;
  46. pte_t *start_ptep;
  47. int i;
  48. start_ptep = ptep = contpte_align_down(ptep);
  49. start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
  50. pte = pfn_pte(ALIGN_DOWN(pte_pfn(pte), CONT_PTES), pte_pgprot(pte));
  51. for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE) {
  52. pte_t ptent = __ptep_get_and_clear(mm, addr, ptep);
  53. if (pte_dirty(ptent))
  54. pte = pte_mkdirty(pte);
  55. if (pte_young(ptent))
  56. pte = pte_mkyoung(pte);
  57. }
  58. __flush_tlb_range(&vma, start_addr, addr, PAGE_SIZE, true, 3);
  59. __set_ptes(mm, start_addr, start_ptep, pte, CONT_PTES);
  60. }
  61. void __contpte_try_fold(struct mm_struct *mm, unsigned long addr,
  62. pte_t *ptep, pte_t pte)
  63. {
  64. /*
  65. * We have already checked that the virtual and pysical addresses are
  66. * correctly aligned for a contpte mapping in contpte_try_fold() so the
  67. * remaining checks are to ensure that the contpte range is fully
  68. * covered by a single folio, and ensure that all the ptes are valid
  69. * with contiguous PFNs and matching prots. We ignore the state of the
  70. * access and dirty bits for the purpose of deciding if its a contiguous
  71. * range; the folding process will generate a single contpte entry which
  72. * has a single access and dirty bit. Those 2 bits are the logical OR of
  73. * their respective bits in the constituent pte entries. In order to
  74. * ensure the contpte range is covered by a single folio, we must
  75. * recover the folio from the pfn, but special mappings don't have a
  76. * folio backing them. Fortunately contpte_try_fold() already checked
  77. * that the pte is not special - we never try to fold special mappings.
  78. * Note we can't use vm_normal_page() for this since we don't have the
  79. * vma.
  80. */
  81. unsigned long folio_start, folio_end;
  82. unsigned long cont_start, cont_end;
  83. pte_t expected_pte, subpte;
  84. struct folio *folio;
  85. struct page *page;
  86. unsigned long pfn;
  87. pte_t *orig_ptep;
  88. pgprot_t prot;
  89. int i;
  90. if (!mm_is_user(mm))
  91. return;
  92. page = pte_page(pte);
  93. folio = page_folio(page);
  94. folio_start = addr - (page - &folio->page) * PAGE_SIZE;
  95. folio_end = folio_start + folio_nr_pages(folio) * PAGE_SIZE;
  96. cont_start = ALIGN_DOWN(addr, CONT_PTE_SIZE);
  97. cont_end = cont_start + CONT_PTE_SIZE;
  98. if (folio_start > cont_start || folio_end < cont_end)
  99. return;
  100. pfn = ALIGN_DOWN(pte_pfn(pte), CONT_PTES);
  101. prot = pte_pgprot(pte_mkold(pte_mkclean(pte)));
  102. expected_pte = pfn_pte(pfn, prot);
  103. orig_ptep = ptep;
  104. ptep = contpte_align_down(ptep);
  105. for (i = 0; i < CONT_PTES; i++) {
  106. subpte = pte_mkold(pte_mkclean(__ptep_get(ptep)));
  107. if (!pte_same(subpte, expected_pte))
  108. return;
  109. expected_pte = pte_advance_pfn(expected_pte, 1);
  110. ptep++;
  111. }
  112. pte = pte_mkcont(pte);
  113. contpte_convert(mm, addr, orig_ptep, pte);
  114. }
  115. EXPORT_SYMBOL_GPL(__contpte_try_fold);
  116. void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
  117. pte_t *ptep, pte_t pte)
  118. {
  119. /*
  120. * We have already checked that the ptes are contiguous in
  121. * contpte_try_unfold(), so just check that the mm is user space.
  122. */
  123. if (!mm_is_user(mm))
  124. return;
  125. pte = pte_mknoncont(pte);
  126. contpte_convert(mm, addr, ptep, pte);
  127. }
  128. EXPORT_SYMBOL_GPL(__contpte_try_unfold);
  129. pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte)
  130. {
  131. /*
  132. * Gather access/dirty bits, which may be populated in any of the ptes
  133. * of the contig range. We are guaranteed to be holding the PTL, so any
  134. * contiguous range cannot be unfolded or otherwise modified under our
  135. * feet.
  136. */
  137. pte_t pte;
  138. int i;
  139. ptep = contpte_align_down(ptep);
  140. for (i = 0; i < CONT_PTES; i++, ptep++) {
  141. pte = __ptep_get(ptep);
  142. if (pte_dirty(pte))
  143. orig_pte = pte_mkdirty(orig_pte);
  144. if (pte_young(pte))
  145. orig_pte = pte_mkyoung(orig_pte);
  146. }
  147. return orig_pte;
  148. }
  149. EXPORT_SYMBOL_GPL(contpte_ptep_get);
  150. pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
  151. {
  152. /*
  153. * The ptep_get_lockless() API requires us to read and return *orig_ptep
  154. * so that it is self-consistent, without the PTL held, so we may be
  155. * racing with other threads modifying the pte. Usually a READ_ONCE()
  156. * would suffice, but for the contpte case, we also need to gather the
  157. * access and dirty bits from across all ptes in the contiguous block,
  158. * and we can't read all of those neighbouring ptes atomically, so any
  159. * contiguous range may be unfolded/modified/refolded under our feet.
  160. * Therefore we ensure we read a _consistent_ contpte range by checking
  161. * that all ptes in the range are valid and have CONT_PTE set, that all
  162. * pfns are contiguous and that all pgprots are the same (ignoring
  163. * access/dirty). If we find a pte that is not consistent, then we must
  164. * be racing with an update so start again. If the target pte does not
  165. * have CONT_PTE set then that is considered consistent on its own
  166. * because it is not part of a contpte range.
  167. */
  168. pgprot_t orig_prot;
  169. unsigned long pfn;
  170. pte_t orig_pte;
  171. pgprot_t prot;
  172. pte_t *ptep;
  173. pte_t pte;
  174. int i;
  175. retry:
  176. orig_pte = __ptep_get(orig_ptep);
  177. if (!pte_valid_cont(orig_pte))
  178. return orig_pte;
  179. orig_prot = pte_pgprot(pte_mkold(pte_mkclean(orig_pte)));
  180. ptep = contpte_align_down(orig_ptep);
  181. pfn = pte_pfn(orig_pte) - (orig_ptep - ptep);
  182. for (i = 0; i < CONT_PTES; i++, ptep++, pfn++) {
  183. pte = __ptep_get(ptep);
  184. prot = pte_pgprot(pte_mkold(pte_mkclean(pte)));
  185. if (!pte_valid_cont(pte) ||
  186. pte_pfn(pte) != pfn ||
  187. pgprot_val(prot) != pgprot_val(orig_prot))
  188. goto retry;
  189. if (pte_dirty(pte))
  190. orig_pte = pte_mkdirty(orig_pte);
  191. if (pte_young(pte))
  192. orig_pte = pte_mkyoung(orig_pte);
  193. }
  194. return orig_pte;
  195. }
  196. EXPORT_SYMBOL_GPL(contpte_ptep_get_lockless);
  197. void contpte_set_ptes(struct mm_struct *mm, unsigned long addr,
  198. pte_t *ptep, pte_t pte, unsigned int nr)
  199. {
  200. unsigned long next;
  201. unsigned long end;
  202. unsigned long pfn;
  203. pgprot_t prot;
  204. /*
  205. * The set_ptes() spec guarantees that when nr > 1, the initial state of
  206. * all ptes is not-present. Therefore we never need to unfold or
  207. * otherwise invalidate a range before we set the new ptes.
  208. * contpte_set_ptes() should never be called for nr < 2.
  209. */
  210. VM_WARN_ON(nr == 1);
  211. if (!mm_is_user(mm))
  212. return __set_ptes(mm, addr, ptep, pte, nr);
  213. end = addr + (nr << PAGE_SHIFT);
  214. pfn = pte_pfn(pte);
  215. prot = pte_pgprot(pte);
  216. do {
  217. next = pte_cont_addr_end(addr, end);
  218. nr = (next - addr) >> PAGE_SHIFT;
  219. pte = pfn_pte(pfn, prot);
  220. if (((addr | next | (pfn << PAGE_SHIFT)) & ~CONT_PTE_MASK) == 0)
  221. pte = pte_mkcont(pte);
  222. else
  223. pte = pte_mknoncont(pte);
  224. __set_ptes(mm, addr, ptep, pte, nr);
  225. addr = next;
  226. ptep += nr;
  227. pfn += nr;
  228. } while (addr != end);
  229. }
  230. EXPORT_SYMBOL_GPL(contpte_set_ptes);
  231. void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr,
  232. pte_t *ptep, unsigned int nr, int full)
  233. {
  234. contpte_try_unfold_partial(mm, addr, ptep, nr);
  235. __clear_full_ptes(mm, addr, ptep, nr, full);
  236. }
  237. EXPORT_SYMBOL_GPL(contpte_clear_full_ptes);
  238. pte_t contpte_get_and_clear_full_ptes(struct mm_struct *mm,
  239. unsigned long addr, pte_t *ptep,
  240. unsigned int nr, int full)
  241. {
  242. contpte_try_unfold_partial(mm, addr, ptep, nr);
  243. return __get_and_clear_full_ptes(mm, addr, ptep, nr, full);
  244. }
  245. EXPORT_SYMBOL_GPL(contpte_get_and_clear_full_ptes);
  246. int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma,
  247. unsigned long addr, pte_t *ptep)
  248. {
  249. /*
  250. * ptep_clear_flush_young() technically requires us to clear the access
  251. * flag for a _single_ pte. However, the core-mm code actually tracks
  252. * access/dirty per folio, not per page. And since we only create a
  253. * contig range when the range is covered by a single folio, we can get
  254. * away with clearing young for the whole contig range here, so we avoid
  255. * having to unfold.
  256. */
  257. int young = 0;
  258. int i;
  259. ptep = contpte_align_down(ptep);
  260. addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
  261. for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE)
  262. young |= __ptep_test_and_clear_young(vma, addr, ptep);
  263. return young;
  264. }
  265. EXPORT_SYMBOL_GPL(contpte_ptep_test_and_clear_young);
  266. int contpte_ptep_clear_flush_young(struct vm_area_struct *vma,
  267. unsigned long addr, pte_t *ptep)
  268. {
  269. int young;
  270. young = contpte_ptep_test_and_clear_young(vma, addr, ptep);
  271. if (young) {
  272. /*
  273. * See comment in __ptep_clear_flush_young(); same rationale for
  274. * eliding the trailing DSB applies here.
  275. */
  276. addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
  277. __flush_tlb_range_nosync(vma, addr, addr + CONT_PTE_SIZE,
  278. PAGE_SIZE, true, 3);
  279. }
  280. return young;
  281. }
  282. EXPORT_SYMBOL_GPL(contpte_ptep_clear_flush_young);
  283. void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr,
  284. pte_t *ptep, unsigned int nr)
  285. {
  286. /*
  287. * If wrprotecting an entire contig range, we can avoid unfolding. Just
  288. * set wrprotect and wait for the later mmu_gather flush to invalidate
  289. * the tlb. Until the flush, the page may or may not be wrprotected.
  290. * After the flush, it is guaranteed wrprotected. If it's a partial
  291. * range though, we must unfold, because we can't have a case where
  292. * CONT_PTE is set but wrprotect applies to a subset of the PTEs; this
  293. * would cause it to continue to be unpredictable after the flush.
  294. */
  295. contpte_try_unfold_partial(mm, addr, ptep, nr);
  296. __wrprotect_ptes(mm, addr, ptep, nr);
  297. }
  298. EXPORT_SYMBOL_GPL(contpte_wrprotect_ptes);
  299. void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma,
  300. unsigned long addr, pte_t *ptep,
  301. unsigned int nr, cydp_t flags)
  302. {
  303. /*
  304. * We can safely clear access/dirty without needing to unfold from
  305. * the architectures perspective, even when contpte is set. If the
  306. * range starts or ends midway through a contpte block, we can just
  307. * expand to include the full contpte block. While this is not
  308. * exactly what the core-mm asked for, it tracks access/dirty per
  309. * folio, not per page. And since we only create a contpte block
  310. * when it is covered by a single folio, we can get away with
  311. * clearing access/dirty for the whole block.
  312. */
  313. unsigned long start = addr;
  314. unsigned long end = start + nr * PAGE_SIZE;
  315. if (pte_cont(__ptep_get(ptep + nr - 1)))
  316. end = ALIGN(end, CONT_PTE_SIZE);
  317. if (pte_cont(__ptep_get(ptep))) {
  318. start = ALIGN_DOWN(start, CONT_PTE_SIZE);
  319. ptep = contpte_align_down(ptep);
  320. }
  321. __clear_young_dirty_ptes(vma, start, ptep, (end - start) / PAGE_SIZE, flags);
  322. }
  323. EXPORT_SYMBOL_GPL(contpte_clear_young_dirty_ptes);
  324. int contpte_ptep_set_access_flags(struct vm_area_struct *vma,
  325. unsigned long addr, pte_t *ptep,
  326. pte_t entry, int dirty)
  327. {
  328. unsigned long start_addr;
  329. pte_t orig_pte;
  330. int i;
  331. /*
  332. * Gather the access/dirty bits for the contiguous range. If nothing has
  333. * changed, its a noop.
  334. */
  335. orig_pte = pte_mknoncont(ptep_get(ptep));
  336. if (pte_val(orig_pte) == pte_val(entry))
  337. return 0;
  338. /*
  339. * We can fix up access/dirty bits without having to unfold the contig
  340. * range. But if the write bit is changing, we must unfold.
  341. */
  342. if (pte_write(orig_pte) == pte_write(entry)) {
  343. /*
  344. * For HW access management, we technically only need to update
  345. * the flag on a single pte in the range. But for SW access
  346. * management, we need to update all the ptes to prevent extra
  347. * faults. Avoid per-page tlb flush in __ptep_set_access_flags()
  348. * and instead flush the whole range at the end.
  349. */
  350. ptep = contpte_align_down(ptep);
  351. start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
  352. /*
  353. * We are not advancing entry because __ptep_set_access_flags()
  354. * only consumes access flags from entry. And since we have checked
  355. * for the whole contpte block and returned early, pte_same()
  356. * within __ptep_set_access_flags() is likely false.
  357. */
  358. for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE)
  359. __ptep_set_access_flags(vma, addr, ptep, entry, 0);
  360. if (dirty)
  361. __flush_tlb_range(vma, start_addr, addr,
  362. PAGE_SIZE, true, 3);
  363. } else {
  364. __contpte_try_unfold(vma->vm_mm, addr, ptep, orig_pte);
  365. __ptep_set_access_flags(vma, addr, ptep, entry, dirty);
  366. }
  367. return 1;
  368. }
  369. EXPORT_SYMBOL_GPL(contpte_ptep_set_access_flags);