mapping_dirty_helpers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/pagewalk.h>
  3. #include <linux/hugetlb.h>
  4. #include <linux/bitops.h>
  5. #include <linux/mmu_notifier.h>
  6. #include <linux/mm_inline.h>
  7. #include <asm/cacheflush.h>
  8. #include <asm/tlbflush.h>
  9. /**
  10. * struct wp_walk - Private struct for pagetable walk callbacks
  11. * @range: Range for mmu notifiers
  12. * @tlbflush_start: Address of first modified pte
  13. * @tlbflush_end: Address of last modified pte + 1
  14. * @total: Total number of modified ptes
  15. */
  16. struct wp_walk {
  17. struct mmu_notifier_range range;
  18. unsigned long tlbflush_start;
  19. unsigned long tlbflush_end;
  20. unsigned long total;
  21. };
  22. /**
  23. * wp_pte - Write-protect a pte
  24. * @pte: Pointer to the pte
  25. * @addr: The start of protecting virtual address
  26. * @end: The end of protecting virtual address
  27. * @walk: pagetable walk callback argument
  28. *
  29. * The function write-protects a pte and records the range in
  30. * virtual address space of touched ptes for efficient range TLB flushes.
  31. */
  32. static int wp_pte(pte_t *pte, unsigned long addr, unsigned long end,
  33. struct mm_walk *walk)
  34. {
  35. struct wp_walk *wpwalk = walk->private;
  36. pte_t ptent = ptep_get(pte);
  37. if (pte_write(ptent)) {
  38. pte_t old_pte = ptep_modify_prot_start(walk->vma, addr, pte);
  39. ptent = pte_wrprotect(old_pte);
  40. ptep_modify_prot_commit(walk->vma, addr, pte, old_pte, ptent);
  41. wpwalk->total++;
  42. wpwalk->tlbflush_start = min(wpwalk->tlbflush_start, addr);
  43. wpwalk->tlbflush_end = max(wpwalk->tlbflush_end,
  44. addr + PAGE_SIZE);
  45. }
  46. return 0;
  47. }
  48. /**
  49. * struct clean_walk - Private struct for the clean_record_pte function.
  50. * @base: struct wp_walk we derive from
  51. * @bitmap_pgoff: Address_space Page offset of the first bit in @bitmap
  52. * @bitmap: Bitmap with one bit for each page offset in the address_space range
  53. * covered.
  54. * @start: Address_space page offset of first modified pte relative
  55. * to @bitmap_pgoff
  56. * @end: Address_space page offset of last modified pte relative
  57. * to @bitmap_pgoff
  58. */
  59. struct clean_walk {
  60. struct wp_walk base;
  61. pgoff_t bitmap_pgoff;
  62. unsigned long *bitmap;
  63. pgoff_t start;
  64. pgoff_t end;
  65. };
  66. #define to_clean_walk(_wpwalk) container_of(_wpwalk, struct clean_walk, base)
  67. /**
  68. * clean_record_pte - Clean a pte and record its address space offset in a
  69. * bitmap
  70. * @pte: Pointer to the pte
  71. * @addr: The start of virtual address to be clean
  72. * @end: The end of virtual address to be clean
  73. * @walk: pagetable walk callback argument
  74. *
  75. * The function cleans a pte and records the range in
  76. * virtual address space of touched ptes for efficient TLB flushes.
  77. * It also records dirty ptes in a bitmap representing page offsets
  78. * in the address_space, as well as the first and last of the bits
  79. * touched.
  80. */
  81. static int clean_record_pte(pte_t *pte, unsigned long addr,
  82. unsigned long end, struct mm_walk *walk)
  83. {
  84. struct wp_walk *wpwalk = walk->private;
  85. struct clean_walk *cwalk = to_clean_walk(wpwalk);
  86. pte_t ptent = ptep_get(pte);
  87. if (pte_dirty(ptent)) {
  88. pgoff_t pgoff = ((addr - walk->vma->vm_start) >> PAGE_SHIFT) +
  89. walk->vma->vm_pgoff - cwalk->bitmap_pgoff;
  90. pte_t old_pte = ptep_modify_prot_start(walk->vma, addr, pte);
  91. ptent = pte_mkclean(old_pte);
  92. ptep_modify_prot_commit(walk->vma, addr, pte, old_pte, ptent);
  93. wpwalk->total++;
  94. wpwalk->tlbflush_start = min(wpwalk->tlbflush_start, addr);
  95. wpwalk->tlbflush_end = max(wpwalk->tlbflush_end,
  96. addr + PAGE_SIZE);
  97. __set_bit(pgoff, cwalk->bitmap);
  98. cwalk->start = min(cwalk->start, pgoff);
  99. cwalk->end = max(cwalk->end, pgoff + 1);
  100. }
  101. return 0;
  102. }
  103. /*
  104. * wp_clean_pmd_entry - The pagewalk pmd callback.
  105. *
  106. * Dirty-tracking should take place on the PTE level, so
  107. * WARN() if encountering a dirty huge pmd.
  108. * Furthermore, never split huge pmds, since that currently
  109. * causes dirty info loss. The pagefault handler should do
  110. * that if needed.
  111. */
  112. static int wp_clean_pmd_entry(pmd_t *pmd, unsigned long addr, unsigned long end,
  113. struct mm_walk *walk)
  114. {
  115. pmd_t pmdval = pmdp_get_lockless(pmd);
  116. /* Do not split a huge pmd, present or migrated */
  117. if (pmd_trans_huge(pmdval) || pmd_devmap(pmdval)) {
  118. WARN_ON(pmd_write(pmdval) || pmd_dirty(pmdval));
  119. walk->action = ACTION_CONTINUE;
  120. }
  121. return 0;
  122. }
  123. /*
  124. * wp_clean_pud_entry - The pagewalk pud callback.
  125. *
  126. * Dirty-tracking should take place on the PTE level, so
  127. * WARN() if encountering a dirty huge puds.
  128. * Furthermore, never split huge puds, since that currently
  129. * causes dirty info loss. The pagefault handler should do
  130. * that if needed.
  131. */
  132. static int wp_clean_pud_entry(pud_t *pud, unsigned long addr, unsigned long end,
  133. struct mm_walk *walk)
  134. {
  135. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  136. pud_t pudval = READ_ONCE(*pud);
  137. /* Do not split a huge pud */
  138. if (pud_trans_huge(pudval) || pud_devmap(pudval)) {
  139. WARN_ON(pud_write(pudval) || pud_dirty(pudval));
  140. walk->action = ACTION_CONTINUE;
  141. }
  142. #endif
  143. return 0;
  144. }
  145. /*
  146. * wp_clean_pre_vma - The pagewalk pre_vma callback.
  147. *
  148. * The pre_vma callback performs the cache flush, stages the tlb flush
  149. * and calls the necessary mmu notifiers.
  150. */
  151. static int wp_clean_pre_vma(unsigned long start, unsigned long end,
  152. struct mm_walk *walk)
  153. {
  154. struct wp_walk *wpwalk = walk->private;
  155. wpwalk->tlbflush_start = end;
  156. wpwalk->tlbflush_end = start;
  157. mmu_notifier_range_init(&wpwalk->range, MMU_NOTIFY_PROTECTION_PAGE, 0,
  158. walk->mm, start, end);
  159. mmu_notifier_invalidate_range_start(&wpwalk->range);
  160. flush_cache_range(walk->vma, start, end);
  161. /*
  162. * We're not using tlb_gather_mmu() since typically
  163. * only a small subrange of PTEs are affected, whereas
  164. * tlb_gather_mmu() records the full range.
  165. */
  166. inc_tlb_flush_pending(walk->mm);
  167. return 0;
  168. }
  169. /*
  170. * wp_clean_post_vma - The pagewalk post_vma callback.
  171. *
  172. * The post_vma callback performs the tlb flush and calls necessary mmu
  173. * notifiers.
  174. */
  175. static void wp_clean_post_vma(struct mm_walk *walk)
  176. {
  177. struct wp_walk *wpwalk = walk->private;
  178. if (mm_tlb_flush_nested(walk->mm))
  179. flush_tlb_range(walk->vma, wpwalk->range.start,
  180. wpwalk->range.end);
  181. else if (wpwalk->tlbflush_end > wpwalk->tlbflush_start)
  182. flush_tlb_range(walk->vma, wpwalk->tlbflush_start,
  183. wpwalk->tlbflush_end);
  184. mmu_notifier_invalidate_range_end(&wpwalk->range);
  185. dec_tlb_flush_pending(walk->mm);
  186. }
  187. /*
  188. * wp_clean_test_walk - The pagewalk test_walk callback.
  189. *
  190. * Won't perform dirty-tracking on COW, read-only or HUGETLB vmas.
  191. */
  192. static int wp_clean_test_walk(unsigned long start, unsigned long end,
  193. struct mm_walk *walk)
  194. {
  195. unsigned long vm_flags = READ_ONCE(walk->vma->vm_flags);
  196. /* Skip non-applicable VMAs */
  197. if ((vm_flags & (VM_SHARED | VM_MAYWRITE | VM_HUGETLB)) !=
  198. (VM_SHARED | VM_MAYWRITE))
  199. return 1;
  200. return 0;
  201. }
  202. static const struct mm_walk_ops clean_walk_ops = {
  203. .pte_entry = clean_record_pte,
  204. .pmd_entry = wp_clean_pmd_entry,
  205. .pud_entry = wp_clean_pud_entry,
  206. .test_walk = wp_clean_test_walk,
  207. .pre_vma = wp_clean_pre_vma,
  208. .post_vma = wp_clean_post_vma
  209. };
  210. static const struct mm_walk_ops wp_walk_ops = {
  211. .pte_entry = wp_pte,
  212. .pmd_entry = wp_clean_pmd_entry,
  213. .pud_entry = wp_clean_pud_entry,
  214. .test_walk = wp_clean_test_walk,
  215. .pre_vma = wp_clean_pre_vma,
  216. .post_vma = wp_clean_post_vma
  217. };
  218. /**
  219. * wp_shared_mapping_range - Write-protect all ptes in an address space range
  220. * @mapping: The address_space we want to write protect
  221. * @first_index: The first page offset in the range
  222. * @nr: Number of incremental page offsets to cover
  223. *
  224. * Note: This function currently skips transhuge page-table entries, since
  225. * it's intended for dirty-tracking on the PTE level. It will warn on
  226. * encountering transhuge write-enabled entries, though, and can easily be
  227. * extended to handle them as well.
  228. *
  229. * Return: The number of ptes actually write-protected. Note that
  230. * already write-protected ptes are not counted.
  231. */
  232. unsigned long wp_shared_mapping_range(struct address_space *mapping,
  233. pgoff_t first_index, pgoff_t nr)
  234. {
  235. struct wp_walk wpwalk = { .total = 0 };
  236. i_mmap_lock_read(mapping);
  237. WARN_ON(walk_page_mapping(mapping, first_index, nr, &wp_walk_ops,
  238. &wpwalk));
  239. i_mmap_unlock_read(mapping);
  240. return wpwalk.total;
  241. }
  242. EXPORT_SYMBOL_GPL(wp_shared_mapping_range);
  243. /**
  244. * clean_record_shared_mapping_range - Clean and record all ptes in an
  245. * address space range
  246. * @mapping: The address_space we want to clean
  247. * @first_index: The first page offset in the range
  248. * @nr: Number of incremental page offsets to cover
  249. * @bitmap_pgoff: The page offset of the first bit in @bitmap
  250. * @bitmap: Pointer to a bitmap of at least @nr bits. The bitmap needs to
  251. * cover the whole range @first_index..@first_index + @nr.
  252. * @start: Pointer to number of the first set bit in @bitmap.
  253. * is modified as new bits are set by the function.
  254. * @end: Pointer to the number of the last set bit in @bitmap.
  255. * none set. The value is modified as new bits are set by the function.
  256. *
  257. * When this function returns there is no guarantee that a CPU has
  258. * not already dirtied new ptes. However it will not clean any ptes not
  259. * reported in the bitmap. The guarantees are as follows:
  260. *
  261. * * All ptes dirty when the function starts executing will end up recorded
  262. * in the bitmap.
  263. * * All ptes dirtied after that will either remain dirty, be recorded in the
  264. * bitmap or both.
  265. *
  266. * If a caller needs to make sure all dirty ptes are picked up and none
  267. * additional are added, it first needs to write-protect the address-space
  268. * range and make sure new writers are blocked in page_mkwrite() or
  269. * pfn_mkwrite(). And then after a TLB flush following the write-protection
  270. * pick up all dirty bits.
  271. *
  272. * This function currently skips transhuge page-table entries, since
  273. * it's intended for dirty-tracking on the PTE level. It will warn on
  274. * encountering transhuge dirty entries, though, and can easily be extended
  275. * to handle them as well.
  276. *
  277. * Return: The number of dirty ptes actually cleaned.
  278. */
  279. unsigned long clean_record_shared_mapping_range(struct address_space *mapping,
  280. pgoff_t first_index, pgoff_t nr,
  281. pgoff_t bitmap_pgoff,
  282. unsigned long *bitmap,
  283. pgoff_t *start,
  284. pgoff_t *end)
  285. {
  286. bool none_set = (*start >= *end);
  287. struct clean_walk cwalk = {
  288. .base = { .total = 0 },
  289. .bitmap_pgoff = bitmap_pgoff,
  290. .bitmap = bitmap,
  291. .start = none_set ? nr : *start,
  292. .end = none_set ? 0 : *end,
  293. };
  294. i_mmap_lock_read(mapping);
  295. WARN_ON(walk_page_mapping(mapping, first_index, nr, &clean_walk_ops,
  296. &cwalk.base));
  297. i_mmap_unlock_read(mapping);
  298. *start = cwalk.start;
  299. *end = cwalk.end;
  300. return cwalk.base.total;
  301. }
  302. EXPORT_SYMBOL_GPL(clean_record_shared_mapping_range);