mapping_dirty_helpers.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 = *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 = *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 = pmd_read_atomic(pmd);
  116. if (!pmd_trans_unstable(&pmdval))
  117. return 0;
  118. if (pmd_none(pmdval)) {
  119. walk->action = ACTION_AGAIN;
  120. return 0;
  121. }
  122. /* Huge pmd, present or migrated */
  123. walk->action = ACTION_CONTINUE;
  124. if (pmd_trans_huge(pmdval) || pmd_devmap(pmdval))
  125. WARN_ON(pmd_write(pmdval) || pmd_dirty(pmdval));
  126. return 0;
  127. }
  128. /*
  129. * wp_clean_pud_entry - The pagewalk pud callback.
  130. *
  131. * Dirty-tracking should take place on the PTE level, so
  132. * WARN() if encountering a dirty huge puds.
  133. * Furthermore, never split huge puds, since that currently
  134. * causes dirty info loss. The pagefault handler should do
  135. * that if needed.
  136. */
  137. static int wp_clean_pud_entry(pud_t *pud, unsigned long addr, unsigned long end,
  138. struct mm_walk *walk)
  139. {
  140. pud_t pudval = READ_ONCE(*pud);
  141. if (!pud_trans_unstable(&pudval))
  142. return 0;
  143. if (pud_none(pudval)) {
  144. walk->action = ACTION_AGAIN;
  145. return 0;
  146. }
  147. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  148. /* Huge pud */
  149. walk->action = ACTION_CONTINUE;
  150. if (pud_trans_huge(pudval) || pud_devmap(pudval))
  151. WARN_ON(pud_write(pudval) || pud_dirty(pudval));
  152. #endif
  153. return 0;
  154. }
  155. /*
  156. * wp_clean_pre_vma - The pagewalk pre_vma callback.
  157. *
  158. * The pre_vma callback performs the cache flush, stages the tlb flush
  159. * and calls the necessary mmu notifiers.
  160. */
  161. static int wp_clean_pre_vma(unsigned long start, unsigned long end,
  162. struct mm_walk *walk)
  163. {
  164. struct wp_walk *wpwalk = walk->private;
  165. wpwalk->tlbflush_start = end;
  166. wpwalk->tlbflush_end = start;
  167. mmu_notifier_range_init(&wpwalk->range, MMU_NOTIFY_PROTECTION_PAGE, 0,
  168. walk->vma, walk->mm, start, end);
  169. mmu_notifier_invalidate_range_start(&wpwalk->range);
  170. flush_cache_range(walk->vma, start, end);
  171. /*
  172. * We're not using tlb_gather_mmu() since typically
  173. * only a small subrange of PTEs are affected, whereas
  174. * tlb_gather_mmu() records the full range.
  175. */
  176. inc_tlb_flush_pending(walk->mm);
  177. return 0;
  178. }
  179. /*
  180. * wp_clean_post_vma - The pagewalk post_vma callback.
  181. *
  182. * The post_vma callback performs the tlb flush and calls necessary mmu
  183. * notifiers.
  184. */
  185. static void wp_clean_post_vma(struct mm_walk *walk)
  186. {
  187. struct wp_walk *wpwalk = walk->private;
  188. if (mm_tlb_flush_nested(walk->mm))
  189. flush_tlb_range(walk->vma, wpwalk->range.start,
  190. wpwalk->range.end);
  191. else if (wpwalk->tlbflush_end > wpwalk->tlbflush_start)
  192. flush_tlb_range(walk->vma, wpwalk->tlbflush_start,
  193. wpwalk->tlbflush_end);
  194. mmu_notifier_invalidate_range_end(&wpwalk->range);
  195. dec_tlb_flush_pending(walk->mm);
  196. }
  197. /*
  198. * wp_clean_test_walk - The pagewalk test_walk callback.
  199. *
  200. * Won't perform dirty-tracking on COW, read-only or HUGETLB vmas.
  201. */
  202. static int wp_clean_test_walk(unsigned long start, unsigned long end,
  203. struct mm_walk *walk)
  204. {
  205. unsigned long vm_flags = READ_ONCE(walk->vma->vm_flags);
  206. /* Skip non-applicable VMAs */
  207. if ((vm_flags & (VM_SHARED | VM_MAYWRITE | VM_HUGETLB)) !=
  208. (VM_SHARED | VM_MAYWRITE))
  209. return 1;
  210. return 0;
  211. }
  212. static const struct mm_walk_ops clean_walk_ops = {
  213. .pte_entry = clean_record_pte,
  214. .pmd_entry = wp_clean_pmd_entry,
  215. .pud_entry = wp_clean_pud_entry,
  216. .test_walk = wp_clean_test_walk,
  217. .pre_vma = wp_clean_pre_vma,
  218. .post_vma = wp_clean_post_vma
  219. };
  220. static const struct mm_walk_ops wp_walk_ops = {
  221. .pte_entry = wp_pte,
  222. .pmd_entry = wp_clean_pmd_entry,
  223. .pud_entry = wp_clean_pud_entry,
  224. .test_walk = wp_clean_test_walk,
  225. .pre_vma = wp_clean_pre_vma,
  226. .post_vma = wp_clean_post_vma
  227. };
  228. /**
  229. * wp_shared_mapping_range - Write-protect all ptes in an address space range
  230. * @mapping: The address_space we want to write protect
  231. * @first_index: The first page offset in the range
  232. * @nr: Number of incremental page offsets to cover
  233. *
  234. * Note: This function currently skips transhuge page-table entries, since
  235. * it's intended for dirty-tracking on the PTE level. It will warn on
  236. * encountering transhuge write-enabled entries, though, and can easily be
  237. * extended to handle them as well.
  238. *
  239. * Return: The number of ptes actually write-protected. Note that
  240. * already write-protected ptes are not counted.
  241. */
  242. unsigned long wp_shared_mapping_range(struct address_space *mapping,
  243. pgoff_t first_index, pgoff_t nr)
  244. {
  245. struct wp_walk wpwalk = { .total = 0 };
  246. i_mmap_lock_read(mapping);
  247. WARN_ON(walk_page_mapping(mapping, first_index, nr, &wp_walk_ops,
  248. &wpwalk));
  249. i_mmap_unlock_read(mapping);
  250. return wpwalk.total;
  251. }
  252. EXPORT_SYMBOL_GPL(wp_shared_mapping_range);
  253. /**
  254. * clean_record_shared_mapping_range - Clean and record all ptes in an
  255. * address space range
  256. * @mapping: The address_space we want to clean
  257. * @first_index: The first page offset in the range
  258. * @nr: Number of incremental page offsets to cover
  259. * @bitmap_pgoff: The page offset of the first bit in @bitmap
  260. * @bitmap: Pointer to a bitmap of at least @nr bits. The bitmap needs to
  261. * cover the whole range @first_index..@first_index + @nr.
  262. * @start: Pointer to number of the first set bit in @bitmap.
  263. * is modified as new bits are set by the function.
  264. * @end: Pointer to the number of the last set bit in @bitmap.
  265. * none set. The value is modified as new bits are set by the function.
  266. *
  267. * Note: When this function returns there is no guarantee that a CPU has
  268. * not already dirtied new ptes. However it will not clean any ptes not
  269. * reported in the bitmap. The guarantees are as follows:
  270. * a) All ptes dirty when the function starts executing will end up recorded
  271. * in the bitmap.
  272. * b) All ptes dirtied after that will either remain dirty, be recorded in the
  273. * bitmap or both.
  274. *
  275. * If a caller needs to make sure all dirty ptes are picked up and none
  276. * additional are added, it first needs to write-protect the address-space
  277. * range and make sure new writers are blocked in page_mkwrite() or
  278. * pfn_mkwrite(). And then after a TLB flush following the write-protection
  279. * pick up all dirty bits.
  280. *
  281. * This function currently skips transhuge page-table entries, since
  282. * it's intended for dirty-tracking on the PTE level. It will warn on
  283. * encountering transhuge dirty entries, though, and can easily be extended
  284. * to handle them as well.
  285. *
  286. * Return: The number of dirty ptes actually cleaned.
  287. */
  288. unsigned long clean_record_shared_mapping_range(struct address_space *mapping,
  289. pgoff_t first_index, pgoff_t nr,
  290. pgoff_t bitmap_pgoff,
  291. unsigned long *bitmap,
  292. pgoff_t *start,
  293. pgoff_t *end)
  294. {
  295. bool none_set = (*start >= *end);
  296. struct clean_walk cwalk = {
  297. .base = { .total = 0 },
  298. .bitmap_pgoff = bitmap_pgoff,
  299. .bitmap = bitmap,
  300. .start = none_set ? nr : *start,
  301. .end = none_set ? 0 : *end,
  302. };
  303. i_mmap_lock_read(mapping);
  304. WARN_ON(walk_page_mapping(mapping, first_index, nr, &clean_walk_ops,
  305. &cwalk.base));
  306. i_mmap_unlock_read(mapping);
  307. *start = cwalk.start;
  308. *end = cwalk.end;
  309. return cwalk.base.total;
  310. }
  311. EXPORT_SYMBOL_GPL(clean_record_shared_mapping_range);