From patchwork Tue Apr 2 12:40:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13613887 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 73DB0C6FD1F for ; Tue, 2 Apr 2024 12:40:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F116C6B0089; Tue, 2 Apr 2024 08:40:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC1BF6B008A; Tue, 2 Apr 2024 08:40:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3BE06B008C; Tue, 2 Apr 2024 08:40:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B42BE6B0089 for ; Tue, 2 Apr 2024 08:40:45 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 4D22E1608A8 for ; Tue, 2 Apr 2024 12:40:45 +0000 (UTC) X-FDA: 81964550850.24.35D77BB Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf18.hostedemail.com (Postfix) with ESMTP id 951AA1C0023 for ; Tue, 2 Apr 2024 12:40:43 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="VxjHo/tq"; spf=pass (imf18.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712061643; a=rsa-sha256; cv=none; b=2+SzYMZuiW0OCWRGo+uxhgJQe70dQXUkNobrpaUTOpk++4xM9nGp94Jy+WZZOqsMgc2Eb3 N/o3/ZDxYRUe8sD8SqbCvZ2qxlRw6ZYnhYJV7iMUvaJXXK4+465ToArWXMZQAeFqCMII5H Okgj5gfz0zEG2fHgLdUPOuwrcEuge2A= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="VxjHo/tq"; spf=pass (imf18.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712061643; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1SLRrUoatbOMxRSqAUzYjv3kxNJrInRhmdCDhk/HGpw=; b=nVOLoYvbVFDc/lVlvGO7fSVSZt8Anjm442MsZr73aUZ2HEJwJJj1I8LIb1+hswEHXzEa2P OJXVYgdEIXukVaaNOeaiHjaGH4MXnV5nl4JGDoW98Fv2vxW3D+uncJCcLF9JwPYEXcRtY5 Av8+0gpall4nCt/ixk9uUJ3ghVvYYm0= Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-6e74aa08d15so3927773b3a.1 for ; Tue, 02 Apr 2024 05:40:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712061642; x=1712666442; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1SLRrUoatbOMxRSqAUzYjv3kxNJrInRhmdCDhk/HGpw=; b=VxjHo/tq8/ek0Bf9gtp0aSUhlPGESPKy2voZ2n11x14PkEs0/YS3/13qoMfiADBvX4 w/EZWEQaObOlNDbV6w2aqWkEPC3M24zlrcDS4LmhETuBLxzv5UIjyKhOhvqynj7k2euY BroA6y/5vqm5o0YT7744Pbu74xvesS7+40oOdYC4504dNWlvF0Ca0o5Gsg2HM4tZXUKq QNMMZxKc1NE7bLh8h6a+HWs+rEHCkHhGEc/Wz6qKznaAIRhJQcp9xvQc7sh7RkUz/vxQ 8nRk4rTh5al4ugSma3jPqgqAlebBKdwd0kx3C+Qs33NSMFeQzGrEGsghIZhdUWdjRiNa IuXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712061642; x=1712666442; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1SLRrUoatbOMxRSqAUzYjv3kxNJrInRhmdCDhk/HGpw=; b=EduamzWpJtQBW18KPKBPg42OHgCDEIw6wTF9wySI3llDBHQl8QXPgVbnv5UfHKl5lL fTMD3XKEP/LD1uSUOIzWp1LQe4GECOeFlhSosSCnn4xShjqXW3jvOgDpuhIL5ataIl+o /wave56ym0kP1GajqpLc8UDxESKt63txqC+fEuNJmN53bbBQsOIb8xjbUT7MU8AdamDD EF2o/iEbtSnxJdsxrsaoSScTOAxgJJAYB7koHeY7YH+4n6uLYB1QsnWxIOsnIaJ5JKA1 0zZOIYQdO3nzo8EFC1pch+NVVUxPnVsWtPnVe6YT4Jlv5vz1yHdQaHReC2qeqZ8s/w8B D30Q== X-Forwarded-Encrypted: i=1; AJvYcCWUwI4q1jXgrO0yh9hjM287r/5A4CT6/62NaUvFys3WSqxCkO+WNIdRLCv8fKG2q0/efGiqvqx3RQ589GBPNZJ4DuA= X-Gm-Message-State: AOJu0Yw6pmipvIZRLctZ/qzOy3/aDyMswq+OKaH3LSEgg6egjpByPrjb OcZDcTWxtLOFVngXMZWTZJJJCsrjqxepUGw5tdxwF6+8H0hrM3rE X-Google-Smtp-Source: AGHT+IFKWkuIBM1Aj0ChduZSzbmJ78sI/uhS5RIdZ7rqHTmgijXUTzIkIb5KcmhxMEPmKX6nCpXloQ== X-Received: by 2002:a05:6a20:d90f:b0:1a7:f15:16a with SMTP id jd15-20020a056a20d90f00b001a70f15016amr9929114pzb.18.1712061642455; Tue, 02 Apr 2024 05:40:42 -0700 (PDT) Received: from LancedeMBP.lan ([112.10.225.9]) by smtp.gmail.com with ESMTPSA id h5-20020a62b405000000b006e664031f10sm9708232pfn.51.2024.04.02.05.40.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Apr 2024 05:40:42 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: zokeefe@google.com, ryan.roberts@arm.com, 21cnbao@gmail.com, shy828301@gmail.com, david@redhat.com, mhocko@suse.com, fengwei.yin@intel.com, xiehuan09@gmail.com, wangkefeng.wang@huawei.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Lance Yang Subject: [PATCH v4 1/2] mm/madvise: introduce mkold_clean_ptes() batch helper Date: Tue, 2 Apr 2024 20:40:28 +0800 Message-Id: <20240402124029.47846-2-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240402124029.47846-1-ioworker0@gmail.com> References: <20240402124029.47846-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 951AA1C0023 X-Stat-Signature: 9ne7hhqaczwhixdryhjjwd7yyq9wuyah X-Rspam-User: X-HE-Tag: 1712061643-249738 X-HE-Meta: U2FsdGVkX1+1aeIs1E5pef0W+mjkm1ETG8gYAHf1/dVTWulsendWrNk3sYDQh3KJck8zqG0TlnONITvI8BhnIz8/x9QuoX1LX6YAJnVgCBM0B4MR0j4lD8/IJ01XoxUQgX45BsxmRxrqXAfl8oQPrgIs8iXVhiiqmEC1S4f9VfwDaOXliPJnFEdUWf7FOft/xP+uvJCvSgHIqJheVCJCJfi/bPbNiyhD4aMiy16afoSikZ0niv+kb5luZulB7K4BAuoJJ/kg7rf3ozG2K2RcWrhrBsLROPZlbOrKIT7W1jW2efHVtWU3CZeqL9XKsaeYgNY8NHPh1rzMSq3whgJspLXzrt3qZBbW0uIyNSh97DwxBmaPMHjojki0x1vLFcsigUx2fX3saAGngLQiFzvF1a5U/pIUrXtHNzHOm3/ZXHamuwj/eme7n8vnYk11Ng2xYhZGWZtfWaQ7D1uJ6f6OroU6jw/kcOkNP49cuNIb3zSdzPgumTFhwtsrBwUxMX4tZdqUY2xst/Zq/yBT6oc4kuFYfCyTQUESQvLQu3uJUkHsFxZLRR9/at7cQvvg/6bZOD7/XjTimE+m/JxuEXHyhOFu2T3W3jDEJxcCPkxh/ZJlax/b9ba2ubVq4Ewvvw7aaqA+99UXIRHWkrcqARcF4hsnWlwyOraze/FG4m/+7pobtPZgCA2xtIRmfgMa670DOqO57Ktea5LQrjhvq2LPZAwwomVOjox8w+DRLU0/Bx2h5zY/MIVMxP8eEHG0CXMm8OjqeEyyqWnR4sChuLw1NCZc9ZGD8UKR9M895em+H9DOCcxkUlbIHGvhlUyvHrPEjyASf/A0IlFdvLzBVQD1Y0tBD2o6L2uplifH6eDqU9/SlcWVHi+tljpXLYZhrLqSVp5jfFlXD6bPgwwlPm3tMfRhxqY7ngmEk5M58Y1c8tVtt02JsGYh1yBSGvl5Lzp0we3AOCXyWOlY38arIf4 DvTMVLjx FvN7jxbMepFauVx5RelaAfrVUX+2H17TSj0On9vDxEs2jPfSj92zX0nfkBIuGLQUL7foeAhG38XkyCC5MgQqRcIJsi/XezHeXJGrPeG0dJQFraUkLeeyk25spNnQh4Zh51LxEiPF7Ap8E/3wsZAOGMggGzfJSSd9VdU6m X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Change the code that clears young and dirty bits from the PTEs to use ptep_get_and_clear_full() and set_pte_at(), via the new mkold_clean_ptes() batch helper function. Unfortunately, the per-pte get_and_clear/modify/set approach would result in unfolding/refolding for contpte mappings on arm64. So we need to override mkold_clean_ptes() for arm64 to avoid it. Suggested-by: David Hildenbrand Suggested-by: Barry Song <21cnbao@gmail.com> Suggested-by: Ryan Roberts Signed-off-by: Lance Yang --- arch/arm64/include/asm/pgtable.h | 36 ++++++++++++++++++++++++++++++++ arch/arm64/mm/contpte.c | 10 +++++++++ include/linux/pgtable.h | 30 ++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 9fd8613b2db2..b032c107090c 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1086,6 +1086,27 @@ static inline bool pud_user_accessible_page(pud_t pud) } #endif +static inline void __mkold_clean_pte(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep) +{ + pte_t old_pte, pte; + + pte = __ptep_get(ptep); + do { + old_pte = pte; + pte = pte_mkclean(pte_mkold(pte)); + pte_val(pte) = cmpxchg_relaxed(&pte_val(*ptep), + pte_val(old_pte), pte_val(pte)); + } while (pte_val(pte) != pte_val(old_pte)); +} + +static inline void mkold_clean_ptes(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, unsigned int nr, int full) +{ + for (; nr-- > 0; ptep++, addr += PAGE_SIZE) + __mkold_clean_pte(vma, addr, ptep); +} + /* * Atomic pte/pmd modifications. */ @@ -1379,6 +1400,8 @@ extern void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr, extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty); +extern void contpte_ptep_mkold_clean(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep); static __always_inline void contpte_try_fold(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte) @@ -1603,6 +1626,18 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma, return contpte_ptep_set_access_flags(vma, addr, ptep, entry, dirty); } +#define mkold_clean_ptes mkold_clean_ptes +static inline void mkold_clean_ptes(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, unsigned int nr, int full) +{ + pte_t orig_pte = __ptep_get(ptep); + + if (likely(!pte_valid_cont(orig_pte))) + return __mkold_clean_ptes(vma, addr, ptep, nr, full); + + return contpte_ptep_mkold_clean(vma, addr, ptep); +} + #else /* CONFIG_ARM64_CONTPTE */ #define ptep_get __ptep_get @@ -1622,6 +1657,7 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma, #define wrprotect_ptes __wrprotect_ptes #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS #define ptep_set_access_flags __ptep_set_access_flags +#define mkold_clean_ptes __mkold_clean_ptes #endif /* CONFIG_ARM64_CONTPTE */ diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c index 1b64b4c3f8bf..560622cfb2a9 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -322,6 +322,16 @@ int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma, } EXPORT_SYMBOL_GPL(contpte_ptep_test_and_clear_young); +void contpte_ptep_mkold_clean(struct vm_area_struct *vma, unsigned long addr, + pte_t *ptep) +{ + ptep = contpte_align_down(ptep); + addr = ALIGN_DOWN(addr, CONT_PTE_SIZE); + + __mkold_clean_ptes(vma, addr, ptep, CONT_PTES, 0); +} +EXPORT_SYMBOL_GPL(contpte_ptep_mkold_clean); + int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index fa8f92f6e2d7..fd30779fe487 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -391,6 +391,36 @@ static inline void mkold_ptes(struct vm_area_struct *vma, unsigned long addr, } #endif +#ifndef mkold_clean_ptes +/** + * mkold_clean_ptes - Mark PTEs that map consecutive pages of the same folio + * as old and clean. + * @vma: VMA the pages are mapped into. + * @addr: Address the first page is mapped at. + * @ptep: Page table pointer for the first entry. + * @nr: Number of entries to mark old and clean. + * + * May be overridden by the architecture; otherwise, implemented as a simple + * loop over ptep_get_and_clear_full(). + * + * Note that PTE bits in the PTE range besides the PFN can differ. For example, + * some PTEs might be write-protected. + * + * Context: The caller holds the page table lock. The PTEs map consecutive + * pages that belong to the same folio. The PTEs are all in the same PMD. + */ +static inline void mkold_clean_ptes(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, unsigned int nr, int full) +{ + pte_t pte; + + for (; nr-- > 0; ptep++, addr += PAGE_SIZE) { + pte = ptep_get_and_clear_full(vma->vm_mm, addr, ptep, full); + set_pte_at(vma->vm_mm, addr, ptep, pte_mkclean(pte_mkold(pte))); + } +} +#endif + #ifndef __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG) static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,