Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp825061ybn; Wed, 2 Oct 2019 06:49:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqx7CiRID6kwLJaeyKIsQh5CFQs8SKK68nwUOokMTtRoGfqr0TVmV2RgYL0xdLPrR8A7oPGx X-Received: by 2002:a17:906:5109:: with SMTP id w9mr3075804ejk.282.1570024174877; Wed, 02 Oct 2019 06:49:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570024174; cv=none; d=google.com; s=arc-20160816; b=exWBcem3BaOrSbq1oRNGOVw9/WGeyDeEm6PNTYljHHSxDK3/J3zC0Em/9ZHDFILypk l5iJ5xyDGSjpQkDOHJ22YOZbuRZsci77NxhTZCfC5cLeFpfeJKuCgdWeC/oa6HyhqqHH xLf7N/pTL+lPVy6uqCRRRskQxoE0GoNjbwpj5cJs05E6xMXhbvFPurQYgi3XcNuoJfB3 /V+CqPIFr/ePCRBv2qfVSRv6ryNwfysI8Fww9Ns1bZXA15cIq276lYBuo2iAZmLpnNYL z1Pd5kKbvbRqsL+BSkZ+KbSJXdEJKFzX+LKl/lvYlsc4U+asMIVO/nWcNy43k76iKuZB g8sQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JS7qi87I9XiPjWzc4JltvzWm814mw7VjTAARaKxrsKI=; b=hQC3AYWyVMhx9gELRr2xVAd/BSzNhXcL0t7DGeOfydHQB5Gafkr8WbdjJHbTnH9Z7x ucstYzzPhmaK4ihIkX0VMXHJnev6d5kZ6ZMDwoKsB4LHiuLYOJ8jIr+CYSd+P8/q9c5r ppiaYKxGLtVcYlP7nEqfeSF/zxJ3vgrcRn/DCEw0mTikf569GjJnTlhrtt4SxpZvx5Mw nFfTJgqk6fUr2NDpxsgSxHm5AUqTiq2xo79lWRfskKG1cm98Q1voKoW6aNM0MeDdyG75 Q6ApaCNf3ZznPtf8Q2feq3dDCQDeu7WrHGLI5rSfXjhiLsYBMT7Z1QmHanW1h4YSahej KmBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@shipmail.org header.s=mail header.b=OuuLqXYF; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id me23si10716727ejb.344.2019.10.02.06.49.09; Wed, 02 Oct 2019 06:49:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail (test mode) header.i=@shipmail.org header.s=mail header.b=OuuLqXYF; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728024AbfJBNr4 (ORCPT + 99 others); Wed, 2 Oct 2019 09:47:56 -0400 Received: from pio-pvt-msa2.bahnhof.se ([79.136.2.41]:37464 "EHLO pio-pvt-msa2.bahnhof.se" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727918AbfJBNrw (ORCPT ); Wed, 2 Oct 2019 09:47:52 -0400 Received: from localhost (localhost [127.0.0.1]) by pio-pvt-msa2.bahnhof.se (Postfix) with ESMTP id 870673F5C7; Wed, 2 Oct 2019 15:47:44 +0200 (CEST) Authentication-Results: pio-pvt-msa2.bahnhof.se; dkim=pass (1024-bit key; unprotected) header.d=shipmail.org header.i=@shipmail.org header.b=OuuLqXYF; dkim-atps=neutral X-Virus-Scanned: Debian amavisd-new at bahnhof.se X-Spam-Flag: NO X-Spam-Score: -2.1 X-Spam-Level: X-Spam-Status: No, score=-2.1 tagged_above=-999 required=6.31 tests=[BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1] autolearn=ham autolearn_force=no Received: from pio-pvt-msa2.bahnhof.se ([127.0.0.1]) by localhost (pio-pvt-msa2.bahnhof.se [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id wG_Sy2dRkw6V; Wed, 2 Oct 2019 15:47:40 +0200 (CEST) Received: from mail1.shipmail.org (h-205-35.A357.priv.bahnhof.se [155.4.205.35]) (Authenticated sender: mb878879) by pio-pvt-msa2.bahnhof.se (Postfix) with ESMTPA id 4D9543F536; Wed, 2 Oct 2019 15:47:40 +0200 (CEST) Received: from localhost.localdomain.localdomain (h-205-35.A357.priv.bahnhof.se [155.4.205.35]) by mail1.shipmail.org (Postfix) with ESMTPSA id 0439F3604FE; Wed, 2 Oct 2019 15:47:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=shipmail.org; s=mail; t=1570024060; bh=1Dcdq1pS8CY3EbjhHaX6/ZmBSMc4b9v0nJ3lxZNu35I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OuuLqXYF1DZGSoDT15QZyva+I1jURaTVUAtRObf+NuxPnGAyhNfnL5qwR57iXH7HL vOAJQkdkYAGf1KLN3jdUd3yYftZ+LgnoYyt39hQOAQ7uUMCVcaFErxjgHG2B/LZxxX WiE/wd541svJTninIbn9aE34xCI9iZMytfii+fhE= From: =?UTF-8?q?Thomas=20Hellstr=C3=B6m=20=28VMware=29?= To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: torvalds@linux-foundation.org, Thomas Hellstrom , Andrew Morton , Matthew Wilcox , Will Deacon , Peter Zijlstra , Rik van Riel , Minchan Kim , Michal Hocko , Huang Ying , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , "Kirill A . Shutemov" Subject: [PATCH v3 3/7] mm: Add write-protect and clean utilities for address space ranges Date: Wed, 2 Oct 2019 15:47:26 +0200 Message-Id: <20191002134730.40985-4-thomas_os@shipmail.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191002134730.40985-1-thomas_os@shipmail.org> References: <20191002134730.40985-1-thomas_os@shipmail.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Hellstrom Add two utilities to a) write-protect and b) clean all ptes pointing into a range of an address space. The utilities are intended to aid in tracking dirty pages (either driver-allocated system memory or pci device memory). The write-protect utility should be used in conjunction with page_mkwrite() and pfn_mkwrite() to trigger write page-faults on page accesses. Typically one would want to use this on sparse accesses into large memory regions. The clean utility should be used to utilize hardware dirtying functionality and avoid the overhead of page-faults, typically on large accesses into small memory regions. Cc: Andrew Morton Cc: Matthew Wilcox Cc: Will Deacon Cc: Peter Zijlstra Cc: Rik van Riel Cc: Minchan Kim Cc: Michal Hocko Cc: Huang Ying Cc: Jérôme Glisse Cc: Kirill A. Shutemov Signed-off-by: Thomas Hellstrom --- include/linux/mm.h | 13 +- mm/Kconfig | 3 + mm/Makefile | 1 + mm/as_dirty_helpers.c | 315 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 331 insertions(+), 1 deletion(-) create mode 100644 mm/as_dirty_helpers.c diff --git a/include/linux/mm.h b/include/linux/mm.h index cc292273e6ba..4a9b02f7f91c 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2637,7 +2637,6 @@ typedef int (*pte_fn_t)(pte_t *pte, unsigned long addr, void *data); extern int apply_to_page_range(struct mm_struct *mm, unsigned long address, unsigned long size, pte_fn_t fn, void *data); - #ifdef CONFIG_PAGE_POISONING extern bool page_poisoning_enabled(void); extern void kernel_poison_pages(struct page *page, int numpages, int enable); @@ -2878,5 +2877,17 @@ static inline int pages_identical(struct page *page1, struct page *page2) return !memcmp_pages(page1, page2); } +#ifdef CONFIG_AS_DIRTY_HELPERS +unsigned long as_dirty_clean(struct address_space *mapping, + pgoff_t first_index, pgoff_t nr, + pgoff_t bitmap_pgoff, + unsigned long *bitmap, + pgoff_t *start, + pgoff_t *end); + +unsigned long as_dirty_wrprotect(struct address_space *mapping, + pgoff_t first_index, pgoff_t nr); +#endif + #endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */ diff --git a/mm/Kconfig b/mm/Kconfig index a5dae9a7eb51..7a0538fe507f 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -736,4 +736,7 @@ config ARCH_HAS_PTE_SPECIAL config ARCH_HAS_HUGEPD bool +config AS_DIRTY_HELPERS + bool + endmenu diff --git a/mm/Makefile b/mm/Makefile index d996846697ef..828bf4278c9d 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -107,3 +107,4 @@ obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o obj-$(CONFIG_ZONE_DEVICE) += memremap.o obj-$(CONFIG_HMM_MIRROR) += hmm.o obj-$(CONFIG_MEMFD_CREATE) += memfd.o +obj-$(CONFIG_AS_DIRTY_HELPERS) += as_dirty_helpers.o diff --git a/mm/as_dirty_helpers.c b/mm/as_dirty_helpers.c new file mode 100644 index 000000000000..2cbf23a86fb4 --- /dev/null +++ b/mm/as_dirty_helpers.c @@ -0,0 +1,315 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include + +/** + * struct as_dirty_walk - Private struct for pagetable walk callbacks + * @range: Range for mmu notifiers + * @tlbflush_start: Address of first modified pte + * @tlbflush_end: Address of last modified pte + 1 + * @total: Total number of modified ptes + * @wrprotect: Whether this is a write-protect or a clean operation + */ +struct as_dirty_walk { + struct mmu_notifier_range range; + unsigned long tlbflush_start; + unsigned long tlbflush_end; + unsigned long total; + unsigned int wrprotect; +}; + +/** + * as_dirty_pte_wrprotect - Write-protect a pte + * @pte: Pointer to the pte + * @addr: The virtual page address + * @walk: pagetable walk callback argument + * + * The function write-protects a pte and records the range in + * virtual address space of touched ptes for efficient range TLB flushes. + */ +static void as_dirty_pte_wrprotect(pte_t *pte, unsigned long addr, + struct mm_walk *walk) +{ + struct as_dirty_walk *adw = walk->private; + pte_t ptent = *pte; + + if (pte_write(ptent)) { + pte_t old_pte = ptep_modify_prot_start(walk->vma, addr, pte); + + ptent = pte_wrprotect(old_pte); + ptep_modify_prot_commit(walk->vma, addr, pte, old_pte, ptent); + adw->total++; + adw->tlbflush_start = min(adw->tlbflush_start, addr); + adw->tlbflush_end = max(adw->tlbflush_end, addr + PAGE_SIZE); + } +} + +/** + * struct as_dirty_walk_clean - Private struct for the as_dirty_walk_clean + * function. + * @base: struct as_dirty_walk we derive from + * @bitmap_pgoff: Address_space Page offset of the first bit in @bitmap + * @bitmap: Bitmap with one bit for each page offset in the address_space range + * covered. + * @start: Address_space page offset of first modified pte relative + * to @bitmap_pgoff + * @end: Address_space page offset of last modified pte relative + * to @bitmap_pgoff + */ +struct as_dirty_walk_clean { + struct as_dirty_walk base; + pgoff_t bitmap_pgoff; + unsigned long *bitmap; + pgoff_t start; + pgoff_t end; +}; + +#define to_as_dirty_walk_clean(_adw) \ + container_of(_adw, struct as_dirty_walk_clean, base) + +/** + * as_dirty_pte_clean - Clean a pte + * @pte: Pointer to the pte + * @addr: The virtual page address + * @walk: pagetable walk callback argument + * + * The function cleans a pte and records the range in + * virtual address space of touched ptes for efficient TLB flushes. + * It also records dirty ptes in a bitmap representing page offsets + * in the address_space, as well as the first and last of the bits + * touched. + */ +static void as_dirty_pte_clean(pte_t *pte, unsigned long addr, + struct mm_walk *walk) +{ + struct as_dirty_walk *adw = walk->private; + struct as_dirty_walk_clean *clean = to_as_dirty_walk_clean(adw); + pte_t ptent = *pte; + + if (pte_dirty(ptent)) { + pgoff_t pgoff = ((addr - walk->vma->vm_start) >> PAGE_SHIFT) + + walk->vma->vm_pgoff - clean->bitmap_pgoff; + pte_t old_pte = ptep_modify_prot_start(walk->vma, addr, pte); + + ptent = pte_mkclean(old_pte); + ptep_modify_prot_commit(walk->vma, addr, pte, old_pte, ptent); + + adw->total++; + adw->tlbflush_start = min(adw->tlbflush_start, addr); + adw->tlbflush_end = max(adw->tlbflush_end, addr + PAGE_SIZE); + + __set_bit(pgoff, clean->bitmap); + clean->start = min(clean->start, pgoff); + clean->end = max(clean->end, pgoff + 1); + } +} + +/* + * as_dirty_pmd_entry - The pagewalk pmd callback. + * + * Loops over ptes and calls the appropriate PTE callback. + * The pmd callback is needed to take the page-table lock and to + * avoid unnecessarily splitting huge pmd entries. + */ +static int as_dirty_pmd_entry(pmd_t *pmd, unsigned long addr, unsigned long end, + struct mm_walk *walk) +{ + struct mm_struct *mm = walk->mm; + struct as_dirty_walk *adw = walk->private; + pte_t *pte; + spinlock_t *ptl; + + /* Ignore huge pmds. Dirty tracking is done on the PTE level! */ + if (pmd_trans_unstable(pmd)) + return 0; + + pte = (mm == &init_mm) ? + pte_offset_kernel(pmd, addr) : + pte_offset_map_lock(mm, pmd, addr, &ptl); + + arch_enter_lazy_mmu_mode(); + + do { + if (adw->wrprotect) + as_dirty_pte_wrprotect(pte++, addr, walk); + else + as_dirty_pte_clean(pte++, addr, walk); + } while (addr += PAGE_SIZE, addr != end); + + arch_leave_lazy_mmu_mode(); + + if (mm != &init_mm) + pte_unmap_unlock(pte - 1, ptl); + + return 0; +} + +/* + * as_dirty_pud_entry - The pagewalk pud callback. + * + * The pud callback is needed solely to avoid unnecessarily splitting huge + * pud entries. + */ +static int as_dirty_pud_entry(pud_t *pud, unsigned long addr, unsigned long end, + struct mm_walk *walk) +{ + /* Ignore huge puds. Dirty tracking is done on the PTE level. */ + return 0; +} + +/* + * as_dirty_pre_vma - The pagewalk pre_vma callback. + * + * The pre_vma callback performs the cache flush, stages the tlb flush + * and calls the necessary mmu notifiers. + */ +static int as_dirty_pre_vma(unsigned long start, unsigned long end, + struct mm_walk *walk) +{ + struct as_dirty_walk *adw = walk->private; + + adw->tlbflush_start = end; + adw->tlbflush_end = start; + + mmu_notifier_range_init(&adw->range, MMU_NOTIFY_PROTECTION_PAGE, 0, + walk->vma, walk->mm, start, end); + mmu_notifier_invalidate_range_start(&adw->range); + flush_cache_range(walk->vma, start, end); + + /* + * We're not using tlb_gather_mmu() since typically + * only a small subrange of PTEs are affected, whereas + * tlb_gather_mmu() records the full range. + */ + inc_tlb_flush_pending(walk->mm); + + return 0; +} + +/* + * as_dirty_post_vma - The pagewalk post_vma callback. + * + * The post_vma callback performs the tlb flush and calls necessary mmu + * notifiers. + */ +static void as_dirty_post_vma(struct mm_walk *walk) +{ + struct as_dirty_walk *adw = walk->private; + + if (adw->tlbflush_end > adw->tlbflush_start) + flush_tlb_range(walk->vma, adw->tlbflush_start, + adw->tlbflush_end); + + mmu_notifier_invalidate_range_end(&adw->range); + dec_tlb_flush_pending(walk->mm); +} + +/* + * as_dirty_test_walk - The pagewalk test_walk callback. + * + * Won't perform dirty-tracking on COW, read-only or HUGETLB vmas. + */ +static int as_dirty_test_walk(unsigned long start, unsigned long end, + struct mm_walk *walk) +{ + /* Skip non-applicable VMAs */ + if ((walk->vma->vm_flags & (VM_SHARED | VM_WRITE | VM_HUGETLB)) != + (VM_SHARED | VM_WRITE)) + return 1; + + return 0; +} + +static const struct mm_walk_ops walk_ops = { + .pmd_entry = as_dirty_pmd_entry, + .pud_entry = as_dirty_pud_entry, + .test_walk = as_dirty_test_walk, + .pre_vma = as_dirty_pre_vma, + .post_vma = as_dirty_post_vma +}; + +/** + * as_dirty_wrprotect - Write-protect all ptes in an address_space range + * @mapping: The address_space we want to write protect + * @first_index: The first page offset in the range + * @nr: Number of incremental page offsets to cover + * + * Return: The number of ptes actually write-protected. Note that + * already write-protected ptes are not counted. + */ +unsigned long as_dirty_wrprotect(struct address_space *mapping, + pgoff_t first_index, pgoff_t nr) +{ + struct as_dirty_walk adw = { .total = 0, + .wrprotect = 1}; + + i_mmap_lock_read(mapping); + WARN_ON(walk_page_mapping(mapping, first_index, nr, &walk_ops, &adw)); + i_mmap_unlock_read(mapping); + + return adw.total; +} +EXPORT_SYMBOL_GPL(as_dirty_wrprotect); + +/** + * as_dirty_clean - Clean all ptes in an address_space range + * @mapping: The address_space we want to clean + * @first_index: The first page offset in the range + * @nr: Number of incremental page offsets to cover + * @bitmap_pgoff: The page offset of the first bit in @bitmap + * @bitmap: Pointer to a bitmap of at least @nr bits. The bitmap needs to + * cover the whole range @first_index..@first_index + @nr. + * @start: Pointer to number of the first set bit in @bitmap. + * is modified as new bits are set by the function. + * @end: Pointer to the number of the last set bit in @bitmap. + * none set. The value is modified as new bits are set by the function. + * + * Note: When this function returns there is no guarantee that a CPU has + * not already dirtied new ptes. However it will not clean any ptes not + * reported in the bitmap. + * + * If a caller needs to make sure all dirty ptes are picked up and none + * additional are added, it first needs to write-protect the address-space + * range and make sure new writers are blocked in page_mkwrite() or + * pfn_mkwrite(). And then after a TLB flush following the write-protection + * pick up all dirty bits. + * + * WARNING: This function should only be used for address spaces whose + * vmas are marked VM_IO and that do not contain huge pages. + * To avoid interference with COW'd pages, vmas not marked VM_SHARED are + * simply skipped. + * + * Return: The number of dirty ptes actually cleaned. + */ +unsigned long as_dirty_clean(struct address_space *mapping, + pgoff_t first_index, pgoff_t nr, + pgoff_t bitmap_pgoff, + unsigned long *bitmap, + pgoff_t *start, + pgoff_t *end) +{ + bool none_set = (*start >= *end); + struct as_dirty_walk_clean clean = { + .base = { .total = 0, + .wrprotect = 0}, + .bitmap_pgoff = bitmap_pgoff, + .bitmap = bitmap, + .start = none_set ? nr : *start, + .end = none_set ? 0 : *end, + }; + + i_mmap_lock_read(mapping); + WARN_ON(walk_page_mapping(mapping, first_index, nr, &walk_ops, + &clean.base)); + i_mmap_unlock_read(mapping); + + *start = clean.start; + *end = clean.end; + + return clean.base.total; +} +EXPORT_SYMBOL_GPL(as_dirty_clean); -- 2.20.1