From patchwork Sat Apr 13 00:22:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13628387 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 9A4C8C4345F for ; Sat, 13 Apr 2024 00:23:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 32D1F6B0087; Fri, 12 Apr 2024 20:23:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DDA76B0088; Fri, 12 Apr 2024 20:23:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17F2D6B0089; Fri, 12 Apr 2024 20:23:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id EDDA06B0087 for ; Fri, 12 Apr 2024 20:23:01 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A83E4141046 for ; Sat, 13 Apr 2024 00:23:01 +0000 (UTC) X-FDA: 82002608562.14.87A57BA Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf11.hostedemail.com (Postfix) with ESMTP id F086B40007 for ; Sat, 13 Apr 2024 00:22:59 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mKHHzYMK; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712967780; 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=1OvPRCdhoiay+bQidKgk1FXNIyrlSTLCMUCQGI9Z8wE=; b=WMYwNBmNlFYZkRvUWBmrW41TQghZRqztB9sQeqOujfuZFzlH+Le+16B6v5zGPanHe4UXM/ 6sTZud30s2kv3ZVYgSOVy4QRi0PQmr0BIlj8x8opEkyM3ET+SdSmpSjgVFJt6B1pHVk/dq NrqWQ9EaAsP2poqR2O0QEgYtVMmtUgc= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mKHHzYMK; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712967780; a=rsa-sha256; cv=none; b=ArqQ29MCw4YgP5JMUHV/+Hr0fLI4+T6y3XWGo9ROuT5fwYa8CKypUHkNlHweRqBVjZlfP+ N/wxnWHZlDYzJfN/K71p3p4DBg0Hm28vn6h4RjY8I1T0a9/WdzFwOV61S4/jD3qNkO9vUE KaeNfDK5K9oUiUFCU6z4Xk80Spac5no= Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1e3ff14f249so11126205ad.1 for ; Fri, 12 Apr 2024 17:22:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712967779; x=1713572579; 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=1OvPRCdhoiay+bQidKgk1FXNIyrlSTLCMUCQGI9Z8wE=; b=mKHHzYMKKWqayOzRHPtQJrmfc3Jbk8ysUf5GOdSLt4WrS+DILlDnZowHq+399+8gUq fWpYPvMmN7xrp/BGAQ+zzlGHUrbqJxL0mUD8AuMHVuirRQtE8Ne2FWzuePxYrZzXryzd Ae6Vdbt80oA/2bG2wnImq4h1kWfUOEXeyaeg0ffhc/hyvCiP/4kzUQ5J8v4aAaWSPBJS lMG/CZPvxTjpf8w1L6REqc05+UJs0hNH98TH0k3a092w4AuUpI0jNGYvrPSeoBLu2SCY RQPJsxPidG94eKiioSUWd+79Z2nS4vuUdqG7V88cCkfDJukwUxEWRNL2FhhEkjJcWwwo ob2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712967779; x=1713572579; 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=1OvPRCdhoiay+bQidKgk1FXNIyrlSTLCMUCQGI9Z8wE=; b=hNAMcKIf6NJCY0jEHq6gnKTMuE3Qo8GaaLx2oAySbyzItrvJlmlX9pE3V9nyD1DBf3 O0pzf46lgZZDVhmK+iLDkWawELX0pMaGSGfz7NA/KGUNDXsVVKLDTG6uym1L+9wlCXAr w8cN97yb/BxQ6I/OnDkGtc81WD1HhQO4FVUxGpZIRqbLabZZYbxbPoT1td8imiO0cPnp lqRWneWQhTflJeXH2jyJ67fvIybH75McpNvJCbfk2Z30esyNOsuc5Utdy7B67F5ToL+M oGgn1FxlPmH2vUvUE+NjhyKI5lB3BWZDLne35TCTmdSpioaJTANsu5NhPTcx25GBc9XM 7m1g== X-Forwarded-Encrypted: i=1; AJvYcCWqvbtUTW4yJ4lRioXUAPhYPxDQbY9ldMuZdI6hOVRPXNq8CY0//jHJoLi2iExkAX/db9TiWHmsUSQl7GVt2stpkdg= X-Gm-Message-State: AOJu0YxwePrTF+PUpi11NEQZdFuueEx3e2RZaCEEXyFj/06J2LJZveED YE+4M/ZzT7Ey616nqfb9rv8s2cD3GM0eg+5HmViZH50YycsyY79s X-Google-Smtp-Source: AGHT+IE6RtlVv/rhkh0e6s0wZJMcHXtBBXCMRcTjEYkjnCpyvQLOKnLnjHsAXiCwN9+1akOrIapfJw== X-Received: by 2002:a17:903:2443:b0:1e5:62:7a81 with SMTP id l3-20020a170903244300b001e500627a81mr5783849pls.22.1712967778837; Fri, 12 Apr 2024 17:22:58 -0700 (PDT) Received: from LancedeMBP.lan ([112.10.225.217]) by smtp.gmail.com with ESMTPSA id a17-20020a170902ee9100b001e2a4ac7bf9sm3569618pld.111.2024.04.12.17.22.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 17:22:58 -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 v6 2/2] mm/arm64: override clear_young_dirty_ptes() batch helper Date: Sat, 13 Apr 2024 08:22:19 +0800 Message-Id: <20240413002219.71246-3-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240413002219.71246-1-ioworker0@gmail.com> References: <20240413002219.71246-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: h7k399gwzmkrabspiqn5zttbh4x9bu5r X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: F086B40007 X-HE-Tag: 1712967779-739252 X-HE-Meta: U2FsdGVkX184p+5MRg0VO5OJxlkpir/KR3tvbZ61fYxHTZ1sRteCeo1OovN0E0pK2/ENhsAWkNZO7W+kgDEtZBvS2wGyCcSQXO4ADG6qB/WLgZzTPlo7WCt7B94EY4rAESE7z/60Sm+eqLtD9HbnSs+aDAnzGSklCEwXYP3pKfR2vqIu2TF12FbDWNpLbAzEv84CJ3ey5lv8lihdVzUtxOZybfAqjLDe5hXEiyf0ZzQ2anuJj+PKQJpe3kYmv9iDo1stvlKh9x6abDL9pz0t6fnHurIwlDoQMTk+hHJf+Uua76Uif6tjbqwhu5BD5PcRzzqAnLCxZvGNk5G/+BrI6cN9vQOboT4NHOJQMTS3hf4cWG/yx1XAI8nxTDYMCR4TuuhPwnlwE6SXF97F8lvHRsMJRhVtVkE5P0KR482AJDqr0b/01L9DENk3ZYC4MQ4B8EES0E6cM2BBomBUvOGKRNyEW+GKO87d4Pd2jiS0u89psqpBEVwJpYZBoe94VTk2Bg6EHqCdksVeEBGhHTlkGUguTxgHx0fyLGK51p9haV3otuu9tpnYdicz1oe+6aWaKc5yvOy8dfbeoasUNrqPJg/zlUFlWDvUEUXCCa2akVRL28rxsq4HTLcjAyTpVcs+wL0Pn65gJL47Bs2ZxARGeVHRr3oTU9/FB7zJyb1mQnzNyR9OVeTikJEFgnbc7i/JjFQuEmgpH9uds6RIlq9o5tpXB3HtYkyUmLX6O9LW5P8KftxYXt2cU3ibJalyE7y4jGjkN2rz21bMrvYBeMfVKY5BbCzYWnvVZyr4bT2oQXx8e2ZS4CGwKBJ6x274wdGe9ui3TLqVNWPjQjsL+TKhEAuXvoEgu99HAuktO/iL1dNmwbat1Uqb4uww/bUlRIYQE1sE2pOKvIRsOc9D7/t127T1kq88tlv+/rNegXU4N8U9aPy9VcLFo3xxi0A75/1f6ewJmrIdNZwKnqPxf+1 cligqEv4 zwqpJe6Tc5+tWWIrLcllyyt7JsryXMMDbPwBW/H91haOw4vpYeDs43baS0pZAtoM2iVLmbKMt8NJ1HHaZr6i2qGrWoMHUitsgROu1YAX41PFOAMc7TtVxtlP9eMNSRZ5rVeUHqXwst8ciPgFEtEclbVpo8NOOHd9+WnTB 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: The per-pte get_and_clear/modify/set approach would result in unfolding/refolding for contpte mappings on arm64. So we need to override clear_young_dirty_ptes() for arm64 to avoid it. Suggested-by: David Hildenbrand Suggested-by: Barry Song <21cnbao@gmail.com> Signed-off-by: Ryan Roberts Signed-off-by: Lance Yang --- arch/arm64/include/asm/pgtable.h | 37 ++++++++++++++++++++++++++++++++ arch/arm64/mm/contpte.c | 28 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 9fd8613b2db2..f951774dd2d6 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1223,6 +1223,28 @@ static inline void __wrprotect_ptes(struct mm_struct *mm, unsigned long address, __ptep_set_wrprotect(mm, address, ptep); } +static inline void __clear_young_dirty_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, cydp_t flags) +{ + pte_t pte; + + for (;;) { + pte = __ptep_get(ptep); + + if (flags | CYDP_CLEAR_YOUNG) + pte = pte_mkold(pte); + if (flags | CYDP_CLEAR_DIRTY) + pte = pte_mkclean(pte); + + __set_pte(ptep, pte); + if (--nr == 0) + break; + ptep++; + addr += PAGE_SIZE; + } +} + #ifdef CONFIG_TRANSPARENT_HUGEPAGE #define __HAVE_ARCH_PMDP_SET_WRPROTECT static inline void pmdp_set_wrprotect(struct mm_struct *mm, @@ -1379,6 +1401,9 @@ 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_clear_young_dirty_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, cydp_t flags); static __always_inline void contpte_try_fold(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte) @@ -1603,6 +1628,17 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma, return contpte_ptep_set_access_flags(vma, addr, ptep, entry, dirty); } +#define clear_young_dirty_ptes clear_young_dirty_ptes +static inline void clear_young_dirty_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, cydp_t flags) +{ + if (likely(nr == 1 && !pte_cont(__ptep_get(ptep)))) + __clear_young_dirty_ptes(mm, addr, ptep, nr, flags); + else + contpte_clear_young_dirty_ptes(mm, addr, ptep, nr, flags); +} + #else /* CONFIG_ARM64_CONTPTE */ #define ptep_get __ptep_get @@ -1622,6 +1658,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 clear_young_dirty_ptes __clear_young_dirty_ptes #endif /* CONFIG_ARM64_CONTPTE */ diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c index 1b64b4c3f8bf..bf3b089d9641 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -361,6 +361,34 @@ void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr, } EXPORT_SYMBOL_GPL(contpte_wrprotect_ptes); +void contpte_clear_young_dirty_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr, cydp_t flags) +{ + /* + * We can safely clear access/dirty without needing to unfold from + * the architectures perspective, even when contpte is set. If the + * range starts or ends midway through a contpte block, we can just + * expand to include the full contpte block. While this is not + * exactly what the core-mm asked for, it tracks access/dirty per + * folio, not per page. And since we only create a contpte block + * when it is covered by a single folio, we can get away with + * clearing access/dirty for the whole block. + */ + unsigned int start = addr; + unsigned int end = start + nr; + + if (pte_cont(__ptep_get(ptep + nr - 1))) + end = ALIGN(end, CONT_PTE_SIZE); + + if (pte_cont(__ptep_get(ptep))) { + start = ALIGN_DOWN(start, CONT_PTE_SIZE); + ptep = contpte_align_down(ptep); + } + + __clear_young_dirty_ptes(mm, start, ptep, end - start, flags); +} +EXPORT_SYMBOL_GPL(contpte_clear_young_dirty_ptes); + int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty)