162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#include <linux/pagewalk.h>
362306a36Sopenharmony_ci#include <linux/hugetlb.h>
462306a36Sopenharmony_ci#include <linux/bitops.h>
562306a36Sopenharmony_ci#include <linux/mmu_notifier.h>
662306a36Sopenharmony_ci#include <linux/mm_inline.h>
762306a36Sopenharmony_ci#include <asm/cacheflush.h>
862306a36Sopenharmony_ci#include <asm/tlbflush.h>
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci/**
1162306a36Sopenharmony_ci * struct wp_walk - Private struct for pagetable walk callbacks
1262306a36Sopenharmony_ci * @range: Range for mmu notifiers
1362306a36Sopenharmony_ci * @tlbflush_start: Address of first modified pte
1462306a36Sopenharmony_ci * @tlbflush_end: Address of last modified pte + 1
1562306a36Sopenharmony_ci * @total: Total number of modified ptes
1662306a36Sopenharmony_ci */
1762306a36Sopenharmony_cistruct wp_walk {
1862306a36Sopenharmony_ci	struct mmu_notifier_range range;
1962306a36Sopenharmony_ci	unsigned long tlbflush_start;
2062306a36Sopenharmony_ci	unsigned long tlbflush_end;
2162306a36Sopenharmony_ci	unsigned long total;
2262306a36Sopenharmony_ci};
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/**
2562306a36Sopenharmony_ci * wp_pte - Write-protect a pte
2662306a36Sopenharmony_ci * @pte: Pointer to the pte
2762306a36Sopenharmony_ci * @addr: The start of protecting virtual address
2862306a36Sopenharmony_ci * @end: The end of protecting virtual address
2962306a36Sopenharmony_ci * @walk: pagetable walk callback argument
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * The function write-protects a pte and records the range in
3262306a36Sopenharmony_ci * virtual address space of touched ptes for efficient range TLB flushes.
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_cistatic int wp_pte(pte_t *pte, unsigned long addr, unsigned long end,
3562306a36Sopenharmony_ci		  struct mm_walk *walk)
3662306a36Sopenharmony_ci{
3762306a36Sopenharmony_ci	struct wp_walk *wpwalk = walk->private;
3862306a36Sopenharmony_ci	pte_t ptent = ptep_get(pte);
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	if (pte_write(ptent)) {
4162306a36Sopenharmony_ci		pte_t old_pte = ptep_modify_prot_start(walk->vma, addr, pte);
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci		ptent = pte_wrprotect(old_pte);
4462306a36Sopenharmony_ci		ptep_modify_prot_commit(walk->vma, addr, pte, old_pte, ptent);
4562306a36Sopenharmony_ci		wpwalk->total++;
4662306a36Sopenharmony_ci		wpwalk->tlbflush_start = min(wpwalk->tlbflush_start, addr);
4762306a36Sopenharmony_ci		wpwalk->tlbflush_end = max(wpwalk->tlbflush_end,
4862306a36Sopenharmony_ci					   addr + PAGE_SIZE);
4962306a36Sopenharmony_ci	}
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	return 0;
5262306a36Sopenharmony_ci}
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/**
5562306a36Sopenharmony_ci * struct clean_walk - Private struct for the clean_record_pte function.
5662306a36Sopenharmony_ci * @base: struct wp_walk we derive from
5762306a36Sopenharmony_ci * @bitmap_pgoff: Address_space Page offset of the first bit in @bitmap
5862306a36Sopenharmony_ci * @bitmap: Bitmap with one bit for each page offset in the address_space range
5962306a36Sopenharmony_ci * covered.
6062306a36Sopenharmony_ci * @start: Address_space page offset of first modified pte relative
6162306a36Sopenharmony_ci * to @bitmap_pgoff
6262306a36Sopenharmony_ci * @end: Address_space page offset of last modified pte relative
6362306a36Sopenharmony_ci * to @bitmap_pgoff
6462306a36Sopenharmony_ci */
6562306a36Sopenharmony_cistruct clean_walk {
6662306a36Sopenharmony_ci	struct wp_walk base;
6762306a36Sopenharmony_ci	pgoff_t bitmap_pgoff;
6862306a36Sopenharmony_ci	unsigned long *bitmap;
6962306a36Sopenharmony_ci	pgoff_t start;
7062306a36Sopenharmony_ci	pgoff_t end;
7162306a36Sopenharmony_ci};
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci#define to_clean_walk(_wpwalk) container_of(_wpwalk, struct clean_walk, base)
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci/**
7662306a36Sopenharmony_ci * clean_record_pte - Clean a pte and record its address space offset in a
7762306a36Sopenharmony_ci * bitmap
7862306a36Sopenharmony_ci * @pte: Pointer to the pte
7962306a36Sopenharmony_ci * @addr: The start of virtual address to be clean
8062306a36Sopenharmony_ci * @end: The end of virtual address to be clean
8162306a36Sopenharmony_ci * @walk: pagetable walk callback argument
8262306a36Sopenharmony_ci *
8362306a36Sopenharmony_ci * The function cleans a pte and records the range in
8462306a36Sopenharmony_ci * virtual address space of touched ptes for efficient TLB flushes.
8562306a36Sopenharmony_ci * It also records dirty ptes in a bitmap representing page offsets
8662306a36Sopenharmony_ci * in the address_space, as well as the first and last of the bits
8762306a36Sopenharmony_ci * touched.
8862306a36Sopenharmony_ci */
8962306a36Sopenharmony_cistatic int clean_record_pte(pte_t *pte, unsigned long addr,
9062306a36Sopenharmony_ci			    unsigned long end, struct mm_walk *walk)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci	struct wp_walk *wpwalk = walk->private;
9362306a36Sopenharmony_ci	struct clean_walk *cwalk = to_clean_walk(wpwalk);
9462306a36Sopenharmony_ci	pte_t ptent = ptep_get(pte);
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	if (pte_dirty(ptent)) {
9762306a36Sopenharmony_ci		pgoff_t pgoff = ((addr - walk->vma->vm_start) >> PAGE_SHIFT) +
9862306a36Sopenharmony_ci			walk->vma->vm_pgoff - cwalk->bitmap_pgoff;
9962306a36Sopenharmony_ci		pte_t old_pte = ptep_modify_prot_start(walk->vma, addr, pte);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci		ptent = pte_mkclean(old_pte);
10262306a36Sopenharmony_ci		ptep_modify_prot_commit(walk->vma, addr, pte, old_pte, ptent);
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci		wpwalk->total++;
10562306a36Sopenharmony_ci		wpwalk->tlbflush_start = min(wpwalk->tlbflush_start, addr);
10662306a36Sopenharmony_ci		wpwalk->tlbflush_end = max(wpwalk->tlbflush_end,
10762306a36Sopenharmony_ci					   addr + PAGE_SIZE);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci		__set_bit(pgoff, cwalk->bitmap);
11062306a36Sopenharmony_ci		cwalk->start = min(cwalk->start, pgoff);
11162306a36Sopenharmony_ci		cwalk->end = max(cwalk->end, pgoff + 1);
11262306a36Sopenharmony_ci	}
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	return 0;
11562306a36Sopenharmony_ci}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci/*
11862306a36Sopenharmony_ci * wp_clean_pmd_entry - The pagewalk pmd callback.
11962306a36Sopenharmony_ci *
12062306a36Sopenharmony_ci * Dirty-tracking should take place on the PTE level, so
12162306a36Sopenharmony_ci * WARN() if encountering a dirty huge pmd.
12262306a36Sopenharmony_ci * Furthermore, never split huge pmds, since that currently
12362306a36Sopenharmony_ci * causes dirty info loss. The pagefault handler should do
12462306a36Sopenharmony_ci * that if needed.
12562306a36Sopenharmony_ci */
12662306a36Sopenharmony_cistatic int wp_clean_pmd_entry(pmd_t *pmd, unsigned long addr, unsigned long end,
12762306a36Sopenharmony_ci			      struct mm_walk *walk)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	pmd_t pmdval = pmdp_get_lockless(pmd);
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	/* Do not split a huge pmd, present or migrated */
13262306a36Sopenharmony_ci	if (pmd_trans_huge(pmdval) || pmd_devmap(pmdval)) {
13362306a36Sopenharmony_ci		WARN_ON(pmd_write(pmdval) || pmd_dirty(pmdval));
13462306a36Sopenharmony_ci		walk->action = ACTION_CONTINUE;
13562306a36Sopenharmony_ci	}
13662306a36Sopenharmony_ci	return 0;
13762306a36Sopenharmony_ci}
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci/*
14062306a36Sopenharmony_ci * wp_clean_pud_entry - The pagewalk pud callback.
14162306a36Sopenharmony_ci *
14262306a36Sopenharmony_ci * Dirty-tracking should take place on the PTE level, so
14362306a36Sopenharmony_ci * WARN() if encountering a dirty huge puds.
14462306a36Sopenharmony_ci * Furthermore, never split huge puds, since that currently
14562306a36Sopenharmony_ci * causes dirty info loss. The pagefault handler should do
14662306a36Sopenharmony_ci * that if needed.
14762306a36Sopenharmony_ci */
14862306a36Sopenharmony_cistatic int wp_clean_pud_entry(pud_t *pud, unsigned long addr, unsigned long end,
14962306a36Sopenharmony_ci			      struct mm_walk *walk)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
15262306a36Sopenharmony_ci	pud_t pudval = READ_ONCE(*pud);
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	/* Do not split a huge pud */
15562306a36Sopenharmony_ci	if (pud_trans_huge(pudval) || pud_devmap(pudval)) {
15662306a36Sopenharmony_ci		WARN_ON(pud_write(pudval) || pud_dirty(pudval));
15762306a36Sopenharmony_ci		walk->action = ACTION_CONTINUE;
15862306a36Sopenharmony_ci	}
15962306a36Sopenharmony_ci#endif
16062306a36Sopenharmony_ci	return 0;
16162306a36Sopenharmony_ci}
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci/*
16462306a36Sopenharmony_ci * wp_clean_pre_vma - The pagewalk pre_vma callback.
16562306a36Sopenharmony_ci *
16662306a36Sopenharmony_ci * The pre_vma callback performs the cache flush, stages the tlb flush
16762306a36Sopenharmony_ci * and calls the necessary mmu notifiers.
16862306a36Sopenharmony_ci */
16962306a36Sopenharmony_cistatic int wp_clean_pre_vma(unsigned long start, unsigned long end,
17062306a36Sopenharmony_ci			    struct mm_walk *walk)
17162306a36Sopenharmony_ci{
17262306a36Sopenharmony_ci	struct wp_walk *wpwalk = walk->private;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	wpwalk->tlbflush_start = end;
17562306a36Sopenharmony_ci	wpwalk->tlbflush_end = start;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	mmu_notifier_range_init(&wpwalk->range, MMU_NOTIFY_PROTECTION_PAGE, 0,
17862306a36Sopenharmony_ci				walk->mm, start, end);
17962306a36Sopenharmony_ci	mmu_notifier_invalidate_range_start(&wpwalk->range);
18062306a36Sopenharmony_ci	flush_cache_range(walk->vma, start, end);
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	/*
18362306a36Sopenharmony_ci	 * We're not using tlb_gather_mmu() since typically
18462306a36Sopenharmony_ci	 * only a small subrange of PTEs are affected, whereas
18562306a36Sopenharmony_ci	 * tlb_gather_mmu() records the full range.
18662306a36Sopenharmony_ci	 */
18762306a36Sopenharmony_ci	inc_tlb_flush_pending(walk->mm);
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	return 0;
19062306a36Sopenharmony_ci}
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci/*
19362306a36Sopenharmony_ci * wp_clean_post_vma - The pagewalk post_vma callback.
19462306a36Sopenharmony_ci *
19562306a36Sopenharmony_ci * The post_vma callback performs the tlb flush and calls necessary mmu
19662306a36Sopenharmony_ci * notifiers.
19762306a36Sopenharmony_ci */
19862306a36Sopenharmony_cistatic void wp_clean_post_vma(struct mm_walk *walk)
19962306a36Sopenharmony_ci{
20062306a36Sopenharmony_ci	struct wp_walk *wpwalk = walk->private;
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	if (mm_tlb_flush_nested(walk->mm))
20362306a36Sopenharmony_ci		flush_tlb_range(walk->vma, wpwalk->range.start,
20462306a36Sopenharmony_ci				wpwalk->range.end);
20562306a36Sopenharmony_ci	else if (wpwalk->tlbflush_end > wpwalk->tlbflush_start)
20662306a36Sopenharmony_ci		flush_tlb_range(walk->vma, wpwalk->tlbflush_start,
20762306a36Sopenharmony_ci				wpwalk->tlbflush_end);
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	mmu_notifier_invalidate_range_end(&wpwalk->range);
21062306a36Sopenharmony_ci	dec_tlb_flush_pending(walk->mm);
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci/*
21462306a36Sopenharmony_ci * wp_clean_test_walk - The pagewalk test_walk callback.
21562306a36Sopenharmony_ci *
21662306a36Sopenharmony_ci * Won't perform dirty-tracking on COW, read-only or HUGETLB vmas.
21762306a36Sopenharmony_ci */
21862306a36Sopenharmony_cistatic int wp_clean_test_walk(unsigned long start, unsigned long end,
21962306a36Sopenharmony_ci			      struct mm_walk *walk)
22062306a36Sopenharmony_ci{
22162306a36Sopenharmony_ci	unsigned long vm_flags = READ_ONCE(walk->vma->vm_flags);
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	/* Skip non-applicable VMAs */
22462306a36Sopenharmony_ci	if ((vm_flags & (VM_SHARED | VM_MAYWRITE | VM_HUGETLB)) !=
22562306a36Sopenharmony_ci	    (VM_SHARED | VM_MAYWRITE))
22662306a36Sopenharmony_ci		return 1;
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	return 0;
22962306a36Sopenharmony_ci}
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_cistatic const struct mm_walk_ops clean_walk_ops = {
23262306a36Sopenharmony_ci	.pte_entry = clean_record_pte,
23362306a36Sopenharmony_ci	.pmd_entry = wp_clean_pmd_entry,
23462306a36Sopenharmony_ci	.pud_entry = wp_clean_pud_entry,
23562306a36Sopenharmony_ci	.test_walk = wp_clean_test_walk,
23662306a36Sopenharmony_ci	.pre_vma = wp_clean_pre_vma,
23762306a36Sopenharmony_ci	.post_vma = wp_clean_post_vma
23862306a36Sopenharmony_ci};
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_cistatic const struct mm_walk_ops wp_walk_ops = {
24162306a36Sopenharmony_ci	.pte_entry = wp_pte,
24262306a36Sopenharmony_ci	.pmd_entry = wp_clean_pmd_entry,
24362306a36Sopenharmony_ci	.pud_entry = wp_clean_pud_entry,
24462306a36Sopenharmony_ci	.test_walk = wp_clean_test_walk,
24562306a36Sopenharmony_ci	.pre_vma = wp_clean_pre_vma,
24662306a36Sopenharmony_ci	.post_vma = wp_clean_post_vma
24762306a36Sopenharmony_ci};
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci/**
25062306a36Sopenharmony_ci * wp_shared_mapping_range - Write-protect all ptes in an address space range
25162306a36Sopenharmony_ci * @mapping: The address_space we want to write protect
25262306a36Sopenharmony_ci * @first_index: The first page offset in the range
25362306a36Sopenharmony_ci * @nr: Number of incremental page offsets to cover
25462306a36Sopenharmony_ci *
25562306a36Sopenharmony_ci * Note: This function currently skips transhuge page-table entries, since
25662306a36Sopenharmony_ci * it's intended for dirty-tracking on the PTE level. It will warn on
25762306a36Sopenharmony_ci * encountering transhuge write-enabled entries, though, and can easily be
25862306a36Sopenharmony_ci * extended to handle them as well.
25962306a36Sopenharmony_ci *
26062306a36Sopenharmony_ci * Return: The number of ptes actually write-protected. Note that
26162306a36Sopenharmony_ci * already write-protected ptes are not counted.
26262306a36Sopenharmony_ci */
26362306a36Sopenharmony_ciunsigned long wp_shared_mapping_range(struct address_space *mapping,
26462306a36Sopenharmony_ci				      pgoff_t first_index, pgoff_t nr)
26562306a36Sopenharmony_ci{
26662306a36Sopenharmony_ci	struct wp_walk wpwalk = { .total = 0 };
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	i_mmap_lock_read(mapping);
26962306a36Sopenharmony_ci	WARN_ON(walk_page_mapping(mapping, first_index, nr, &wp_walk_ops,
27062306a36Sopenharmony_ci				  &wpwalk));
27162306a36Sopenharmony_ci	i_mmap_unlock_read(mapping);
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	return wpwalk.total;
27462306a36Sopenharmony_ci}
27562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(wp_shared_mapping_range);
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci/**
27862306a36Sopenharmony_ci * clean_record_shared_mapping_range - Clean and record all ptes in an
27962306a36Sopenharmony_ci * address space range
28062306a36Sopenharmony_ci * @mapping: The address_space we want to clean
28162306a36Sopenharmony_ci * @first_index: The first page offset in the range
28262306a36Sopenharmony_ci * @nr: Number of incremental page offsets to cover
28362306a36Sopenharmony_ci * @bitmap_pgoff: The page offset of the first bit in @bitmap
28462306a36Sopenharmony_ci * @bitmap: Pointer to a bitmap of at least @nr bits. The bitmap needs to
28562306a36Sopenharmony_ci * cover the whole range @first_index..@first_index + @nr.
28662306a36Sopenharmony_ci * @start: Pointer to number of the first set bit in @bitmap.
28762306a36Sopenharmony_ci * is modified as new bits are set by the function.
28862306a36Sopenharmony_ci * @end: Pointer to the number of the last set bit in @bitmap.
28962306a36Sopenharmony_ci * none set. The value is modified as new bits are set by the function.
29062306a36Sopenharmony_ci *
29162306a36Sopenharmony_ci * When this function returns there is no guarantee that a CPU has
29262306a36Sopenharmony_ci * not already dirtied new ptes. However it will not clean any ptes not
29362306a36Sopenharmony_ci * reported in the bitmap. The guarantees are as follows:
29462306a36Sopenharmony_ci *
29562306a36Sopenharmony_ci * * All ptes dirty when the function starts executing will end up recorded
29662306a36Sopenharmony_ci *   in the bitmap.
29762306a36Sopenharmony_ci * * All ptes dirtied after that will either remain dirty, be recorded in the
29862306a36Sopenharmony_ci *   bitmap or both.
29962306a36Sopenharmony_ci *
30062306a36Sopenharmony_ci * If a caller needs to make sure all dirty ptes are picked up and none
30162306a36Sopenharmony_ci * additional are added, it first needs to write-protect the address-space
30262306a36Sopenharmony_ci * range and make sure new writers are blocked in page_mkwrite() or
30362306a36Sopenharmony_ci * pfn_mkwrite(). And then after a TLB flush following the write-protection
30462306a36Sopenharmony_ci * pick up all dirty bits.
30562306a36Sopenharmony_ci *
30662306a36Sopenharmony_ci * This function currently skips transhuge page-table entries, since
30762306a36Sopenharmony_ci * it's intended for dirty-tracking on the PTE level. It will warn on
30862306a36Sopenharmony_ci * encountering transhuge dirty entries, though, and can easily be extended
30962306a36Sopenharmony_ci * to handle them as well.
31062306a36Sopenharmony_ci *
31162306a36Sopenharmony_ci * Return: The number of dirty ptes actually cleaned.
31262306a36Sopenharmony_ci */
31362306a36Sopenharmony_ciunsigned long clean_record_shared_mapping_range(struct address_space *mapping,
31462306a36Sopenharmony_ci						pgoff_t first_index, pgoff_t nr,
31562306a36Sopenharmony_ci						pgoff_t bitmap_pgoff,
31662306a36Sopenharmony_ci						unsigned long *bitmap,
31762306a36Sopenharmony_ci						pgoff_t *start,
31862306a36Sopenharmony_ci						pgoff_t *end)
31962306a36Sopenharmony_ci{
32062306a36Sopenharmony_ci	bool none_set = (*start >= *end);
32162306a36Sopenharmony_ci	struct clean_walk cwalk = {
32262306a36Sopenharmony_ci		.base = { .total = 0 },
32362306a36Sopenharmony_ci		.bitmap_pgoff = bitmap_pgoff,
32462306a36Sopenharmony_ci		.bitmap = bitmap,
32562306a36Sopenharmony_ci		.start = none_set ? nr : *start,
32662306a36Sopenharmony_ci		.end = none_set ? 0 : *end,
32762306a36Sopenharmony_ci	};
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	i_mmap_lock_read(mapping);
33062306a36Sopenharmony_ci	WARN_ON(walk_page_mapping(mapping, first_index, nr, &clean_walk_ops,
33162306a36Sopenharmony_ci				  &cwalk.base));
33262306a36Sopenharmony_ci	i_mmap_unlock_read(mapping);
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci	*start = cwalk.start;
33562306a36Sopenharmony_ci	*end = cwalk.end;
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci	return cwalk.base.total;
33862306a36Sopenharmony_ci}
33962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(clean_record_shared_mapping_range);
340