From patchwork Mon Apr 8 04:24:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13620559 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 C1C1BC67861 for ; Mon, 8 Apr 2024 04:25:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 55F0C6B0088; Mon, 8 Apr 2024 00:25:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 50CF96B0089; Mon, 8 Apr 2024 00:25:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 388996B008A; Mon, 8 Apr 2024 00:25:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 187D36B0088 for ; Mon, 8 Apr 2024 00:25:07 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 91237160513 for ; Mon, 8 Apr 2024 04:25:06 +0000 (UTC) X-FDA: 81985074612.19.EDDCE1B Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf06.hostedemail.com (Postfix) with ESMTP id DF511180014 for ; Mon, 8 Apr 2024 04:25:04 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Evvu1yhq; spf=pass (imf06.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.179 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=1712550304; 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=5PrJ/ANkryEW7CJ+WJBbTdeBa44gWubQPwVbhS14t9k=; b=XTZl6LRuJywFRBzXuWisPMHKX1UzTgHQZSBgTwKmJ19sQoL6B5m2C8JmAWLc9zWD7vCLKa dXZApEDYU2BdPloG0AU7QvIEwfEo2+t2Z3tvJ1e0DtL162PntWMErf6yxMlANYZNTfDA+z UTPEqZm5bprPLSirNvpWXnrP57570l0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712550304; a=rsa-sha256; cv=none; b=fG0wmOwbfs6p/bf7rYpFGhK7PTXNr/GLKE1o3bq0BcoJ0bIY6FzgYB2gbOv+RT8QkbL/cN SpWDgKGEf4KIvVpOye7fBfH1NAilMuB+cIvws/LED5cwnnb2qNkMObRc1vb15IgC9RmxHy vPFVsHkgeR6YNXc2YibJgn2LWJkSWH0= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Evvu1yhq; spf=pass (imf06.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6ed20fb620fso702514b3a.2 for ; Sun, 07 Apr 2024 21:25:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712550304; x=1713155104; 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=5PrJ/ANkryEW7CJ+WJBbTdeBa44gWubQPwVbhS14t9k=; b=Evvu1yhqCkLf8ACh9fRIWeRf/TNcJyEDUuz4b4WEKbnL5keZQQ/TwGnx4pWNR88sDd LqQspKwL6z63aqCOI2aWChTorhANbf+NryKueOVpmpewKYhXlWmC50r2UW9PrTw/CDVF CVrrRrIccRSY8eIr7yTdfYwAxnl5CLyCtpKo8Wj1/mz2LGt+DTDB7c9CROmbqIsS28/f me/jiSMPuwMfAz0Tbu855/8qJWr0yvAC61AI0A3iYReylQY+3FXTxg1lrn2iTXsWckip KO+ApccgX13z3z2NesiAGyypQ4wZWVGHZ4iiGtp5JlNZuZLLJ1H/+Wk1C596u+s/OOBj f2ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712550304; x=1713155104; 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=5PrJ/ANkryEW7CJ+WJBbTdeBa44gWubQPwVbhS14t9k=; b=YlPkWT972AwV3Op1ba2NqrhnVAu52e1a5BsPerNj5wQYDMA2Is28CLHTUdpHO+j+yy iMZxmrG0/tHOUE8KHHDuciRIZVRivc1AeSaYxNsILa/AufbxmBF8MtySG8rREcKVM9jk fs5y96aCbavixfb7IFjFV0FGm/Jp4EUbhAzISH3iwBGlFaNa/lfq83Bg0DZ8CsHLjuXI dCKoyAUfx7EFw3A/eAiRNg4X/iTDdAb67Cfx3rOhf6OoZUm527G1bKBhEIZoLwAUBfzI AxY1yTtMY2D0wl/cxijeS3lD7PMJIBvS40Dx+klE2kezc98t45qRwcJUYopyhqjM1Swm Mzsg== X-Forwarded-Encrypted: i=1; AJvYcCUg8VjKQ50tSJ8pNJyapyS335F/OwgPFcSbVCoscf+uE5is6paE/wVAJ3VHSVS4AW2Vya+npli829/2YrfdSKC6Fxs= X-Gm-Message-State: AOJu0YxNjTTH11HtAcgPe3uay7gEKu7Z12Qu3Btd+F9CbCiS54r5Dsjl QKcPoY2Pe/LqIDDf/o8/cyNE23KZVWU1QHbR28TTaZuTg8oefSJK X-Google-Smtp-Source: AGHT+IHgvW4D9q14vF5+KTBGSUvoeHTEnFznIzfkTxepc4GsFqzcMQKJQZJGvDAWWKml0EBiM0dg1g== X-Received: by 2002:a05:6a00:4f84:b0:6ea:afd1:90e6 with SMTP id ld4-20020a056a004f8400b006eaafd190e6mr7988047pfb.6.1712550303756; Sun, 07 Apr 2024 21:25:03 -0700 (PDT) Received: from LancedeMBP.lan ([112.10.225.217]) by smtp.gmail.com with ESMTPSA id p20-20020a056a000b5400b006eab6ac1f83sm5465628pfo.0.2024.04.07.21.25.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Apr 2024 21:25:03 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: ryan.roberts@arm.com, david@redhat.com, 21cnbao@gmail.com, mhocko@suse.com, fengwei.yin@intel.com, zokeefe@google.com, shy828301@gmail.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 v5 2/2] mm/arm64: override mkold_clean_ptes() batch helper Date: Mon, 8 Apr 2024 12:24:37 +0800 Message-Id: <20240408042437.10951-3-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240408042437.10951-1-ioworker0@gmail.com> References: <20240408042437.10951-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: DF511180014 X-Rspam-User: X-Stat-Signature: ys5h8zzr5ppwka6rkmsusbg7gfbdk1xg X-Rspamd-Server: rspam03 X-HE-Tag: 1712550304-980483 X-HE-Meta: U2FsdGVkX1+ZC/dekdDnOW8fgzrfsXwfYYw2twDyLz/LNj2k2W5qnBYlFsmSB0/EFWMkN03ycloaWgtn033W2hQmb3UuIBAeaKfMr89D7U/gApPrYYW4Wg2wU8qapqxJXLxhA8Vlbi9SwMoFTV5nTFQoXmHBoIaB3lt7fVA3LcmiEDoUn1qozfINjax8uHkMkXiEeotekUAXfsz7jMStw1XuhzkbrWrYHFyn/IzUNkMr1SKW1stTceW/o83/X12YmxPxCTw4t6FVeE9kdDLtODfOoClGTwG23r7CttgBI9kre96rEA0P0wpNQRsLxG58njC8ZGX+EKA+GJz+9gptUOY/fo6AuqT05XGfrN8wcEon5prHqeoosczAtIZ8qRtxtJI975dfvaR5EmjwCK6jOS6tLzHMKPcDJ7oElk4R8Afq2Gkrvg8tKjurWG5J/nsEDilvkMSqdS8PhUVO/PAOIlpm7+pA7IJXx6sSwbjHN9goAqrHbSKKnk45abk5+HyokWwQi1Ujqx5+K1RceoHh3a9o50TqvXsXE8U6CaaB7Lbr/eUMd9phL3p4GF9TSlR3N+BqKNpsRwjlxy3OvtaR2vP6pUxyE528Z/QG0020iuyaT5AQx7j9fcFXDd4yEH8v8ZKfQNlEy0VECmRWOpi3F4h2msAAtfzKTXQvwz6wguKgvf1VGX8XI7ic3WPGt8W8/94tU0yjzvF9akZCNqkMmDP34/oodH2yc+N/3pIsR0dWhskNdxxVFEXBE+sr4++oL2UdJ8dPLxOfmjU1j6Pj0UbsIUlH2qJ0M38rkbhFYFe3G1H97BjR3jOxcgfrDcxmMsbGy1JO6J1A9iyC8/fWz6zgcVr92L0tOnjZb6M2xxzBV2z783XYMwzK/CxTMMaCzBglan7nTnndUftbKfowuTe1ZcH9M/zOM82Fj4sDe6pkhDo15+7X8cq7/Z7GP5di0pQtIoEv/Lm4nylOiAW Lx3xNcmr VkYq0iGNr30efKR9gE2F94pKg6h64UGRZ+FdABVtuUhzqyNY6eln1Tybt0AjSsY1krW2A8Ah239kuc+w9U6sgUcWIbJAaQotklN7/dxvDRGVvOH/fd+ZLFbymhJ/p47g8f9z+SUqaofmejJq1Tvbp9jsobFZDZTDSjdzq 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 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 | 55 ++++++++++++++++++++++++++++++++ arch/arm64/mm/contpte.c | 15 +++++++++ 2 files changed, 70 insertions(+) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 9fd8613b2db2..395754638a9a 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1223,6 +1223,34 @@ static inline void __wrprotect_ptes(struct mm_struct *mm, unsigned long address, __ptep_set_wrprotect(mm, address, ptep); } +static inline void ___ptep_mkold_clean(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte) +{ + pte_t old_pte; + + 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 __ptep_mkold_clean(struct mm_struct *mm, unsigned long addr, + pte_t *ptep) +{ + ___ptep_mkold_clean(mm, addr, ptep, __ptep_get(ptep)); +} + +static inline void __mkold_clean_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr) +{ + unsigned int i; + + for (i = 0; i < nr; i++, addr += PAGE_SIZE, ptep++) + __ptep_mkold_clean(mm, addr, ptep); +} + #ifdef CONFIG_TRANSPARENT_HUGEPAGE #define __HAVE_ARCH_PMDP_SET_WRPROTECT static inline void pmdp_set_wrprotect(struct mm_struct *mm, @@ -1379,6 +1407,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_mkold_clean_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr); static __always_inline void contpte_try_fold(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte) @@ -1603,6 +1633,30 @@ 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 mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr) +{ + if (likely(nr == 1)) { + /* + * Optimization: mkold_clean_ptes() can only be called for present + * ptes so we only need to check contig bit as condition for unfold, + * and we can remove the contig bit from the pte we read to avoid + * re-reading. This speeds up madvise(MADV_FREE) which is sensitive + * for order-0 folios. Equivalent to contpte_try_unfold(). + */ + pte_t orig_pte = __ptep_get(ptep); + + if (unlikely(pte_cont(orig_pte))) { + __contpte_try_unfold(mm, addr, ptep, orig_pte); + orig_pte = pte_mknoncont(orig_pte); + } + ___ptep_mkold_clean(mm, addr, ptep, orig_pte); + } else { + contpte_mkold_clean_ptes(mm, addr, ptep, nr); + } +} + #else /* CONFIG_ARM64_CONTPTE */ #define ptep_get __ptep_get @@ -1622,6 +1676,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..dbff9c5e9eff 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -361,6 +361,21 @@ void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr, } EXPORT_SYMBOL_GPL(contpte_wrprotect_ptes); +void contpte_mkold_clean_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr) +{ + /* + * If clearing the young and dirty bits for an entire contig range, we can + * avoid unfolding. Just set old/clean and wait for the later mmu_gather + * flush to invalidate the tlb. If it's a partial range though, we need to + * unfold. + */ + + contpte_try_unfold_partial(mm, addr, ptep, nr); + __mkold_clean_ptes(mm, addr, ptep, nr); +} +EXPORT_SYMBOL_GPL(contpte_mkold_clean_ptes); + int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty)