From patchwork Wed Apr 17 14:14:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13633427 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 0BD79C4345F for ; Wed, 17 Apr 2024 14:15:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 985856B0089; Wed, 17 Apr 2024 10:15:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 935B76B008A; Wed, 17 Apr 2024 10:15:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7AE6A6B008C; Wed, 17 Apr 2024 10:15:03 -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 5B9CC6B0089 for ; Wed, 17 Apr 2024 10:15:03 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 179CE412D6 for ; Wed, 17 Apr 2024 14:15:03 +0000 (UTC) X-FDA: 82019220486.29.CE9ABFB Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf25.hostedemail.com (Postfix) with ESMTP id 2F6B1A001A for ; Wed, 17 Apr 2024 14:15:00 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZWbbRorN; spf=pass (imf25.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.214.169 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=1713363301; 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=lkdNcUi/cFtD91tOAv56m6RXgS21TIueDzxwluwn/Lw=; b=dpqKCLI4d18RwlUo/aO9Y7DDdUlag71eUyoH/zevWyDMi/xIc38bvSGLiehig5gQBsNf9f TWMto0QK/JfmgtyHOyVsTnednGDRJPFOzNrK98P/QIBIQ5eazSE0AnA+dDasUrzi6p5UrC WYyxNKdorMx2LiW0QhuQ4+bpQ9u0bTk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713363301; a=rsa-sha256; cv=none; b=bKMxDAqxw03gWpc/YdSztP8HkYtnPcrTf/3/+XyybvYpY9kgZOfYoi8pUgi4oQD1oW+FEt k2kVPbGcNhN7gMjdb4tshk/+AIsD8mWORu+tk0Ji84CNkeHiHicUVSwMhD6mICdig99ftn bSucdz2gZKSEhPShN/Exqf4qFSCOmPs= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZWbbRorN; spf=pass (imf25.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1e455b630acso26569145ad.1 for ; Wed, 17 Apr 2024 07:15:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713363300; x=1713968100; 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=lkdNcUi/cFtD91tOAv56m6RXgS21TIueDzxwluwn/Lw=; b=ZWbbRorNMODJbygLD5Epo4LjRd9DUrtMp/IJiI79V/LE5JN0wXL3R6kdjxehXVR+bs q2iHKGbDG1+MvbbwGp23cSlSSqFOhANVJKMIApNv7at2i6wHRVAgGYTxfFER8yYwl6DC /Th0dGC27oQKpYIZSvGbYu4qn6CeUTj8JANA+mVT/tWuUyKZ+SkHL2NPIz1fGSkB8ZiV QK8dz/6aSitpgjWOTSfktgd4UFjIL/M95rTqw+mzlgjzqr5+DTY2Ryic4G0tDxQJvVbV 8rRwzVBSSGAuyPYi1U/ztTWNHghFquZobT0+opFnqXOCrb5xZefmgjhbKQ2ELSTp1WwK b0yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713363300; x=1713968100; 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=lkdNcUi/cFtD91tOAv56m6RXgS21TIueDzxwluwn/Lw=; b=N/2H4au7o5VecqGhoVh+weFq5+YQWctP8WUywOxmNAsPaiXze7q/D1GR9WsFtLBXvi 80ItXRudPL4qdg79ZBJmCt1bpTYoTlglvINzMfJVYesDDGbY75+6fjr5RfM6VOVV7zCe pfoyZYgqVwGLgkaiCLVQPbvBliuH914C/uLFN1EdwotUhAEL6KlFU/veiquBrOHePR4Z Jf65nSv+25F5LfE6NgaldUEenvFKhRRCuQhV834Wmbevu6Y9GTz09/2rjZyqPAHMm4hW 6kNy8zJRfRuTYMJSeJTBSAPjWTFgi+hjaj0mzztMn28THhalTly+o9W7C4kIcWBuY3nW Ln5g== X-Forwarded-Encrypted: i=1; AJvYcCUDFpGdC0F7oAknn0aBiDQ6/yuafmvlV69Rg5NNLHpNRUhpnkXnsUj/asnHb3rILvRUlCJbwzVqMfYo6EtsD3JHT9Q= X-Gm-Message-State: AOJu0Yx5X3ssiJyeHhh3IqsOpGvthFcBcMxnDRzct2Xa7atCfgnTQdGP ircyK1RsY1YCJW/m/aMra0D7Xr/jbLqjIcW/kEsxavrx7cavh/2D X-Google-Smtp-Source: AGHT+IFNu14u7a3I7+Ll1HOcTWP+Tr+CtfofRjw+IGm+xSJjJO923Owls7pUFZJGkbba4Ch4IATTlg== X-Received: by 2002:a17:903:2303:b0:1e4:b1eb:7dee with SMTP id d3-20020a170903230300b001e4b1eb7deemr16402525plh.47.1713363299859; Wed, 17 Apr 2024 07:14:59 -0700 (PDT) Received: from LancedeMBP.lan ([112.10.225.217]) by smtp.gmail.com with ESMTPSA id s14-20020a170902a50e00b001ddc83fda95sm11613562plq.186.2024.04.17.07.14.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 07:14:59 -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 v8 1/3] mm/madvise: introduce clear_young_dirty_ptes() batch helper Date: Wed, 17 Apr 2024 22:14:34 +0800 Message-Id: <20240417141436.77963-2-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240417141436.77963-1-ioworker0@gmail.com> References: <20240417141436.77963-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 2F6B1A001A X-Stat-Signature: nx6n14fon5uxdk67tpuppyz6anzypmn5 X-HE-Tag: 1713363300-231717 X-HE-Meta: U2FsdGVkX19MLIR2uUCsJI+/Oxg9oLZ2bKhefc51/B1pIRzQv3vK9murHqbXk983+LmNYVrFhdMtr6qxtDNkDuBlYWXN5TryEftrE4AceWYCtSXN73yvXizqzyIQIO7hVjLDq7IU52rxY/KF2YsbvT6LD+n/pUAjbDZ5NYiVofjRE+uC22Qi1zjD9wUtYHAw9RK8HPr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oGXF9hcDqaBmvOaOTk6vrEGLpo+MBajD/tpBIFEMplOYVHitHtsHewyzGekyXKbY5E8XkD3tVVISz7i8zN/EEuHg9+K+6WNzaEq3+Ic3UAdKpUiZ1+keDkwxyXWIMkdZsIEQ5n9R+PRep/Dc713mWmOdpO76FwsbPugkGsCbQLYldxbutdgyJjasSEOPvhdDsweLn+VtUyX9WbDC4GtKjE+uS4UrAJt2jfBaePngXFbY1MN5okxxz7dY8xdWOsVSprAlNT4ZOHBOrEmjmVWKaM958MV9j4LiPVAVQDnFngXtLh/T 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: This commit introduces clear_young_dirty_ptes() to replace mkold_ptes(). By doing so, we can use the same function for both use cases (madvise_pageout and madvise_free), and it also provides the flexibility to only clear the dirty flag in the future if needed. Suggested-by: Ryan Roberts Acked-by: David Hildenbrand Reviewed-by: Ryan Roberts Signed-off-by: Lance Yang --- include/linux/mm_types.h | 9 +++++ include/linux/pgtable.h | 74 ++++++++++++++++++++++++---------------- mm/madvise.c | 3 +- 3 files changed, 55 insertions(+), 31 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index db0adf5721cc..24323c7d0bd4 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1368,6 +1368,15 @@ enum fault_flag { typedef unsigned int __bitwise zap_flags_t; +/* Flags for clear_young_dirty_ptes(). */ +typedef int __bitwise cydp_t; + +/* Clear the access bit */ +#define CYDP_CLEAR_YOUNG ((__force cydp_t)BIT(0)) + +/* Clear the dirty bit */ +#define CYDP_CLEAR_DIRTY ((__force cydp_t)BIT(1)) + /* * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each * other. Here is what they mean, and how to use them: diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index e2f45e22a6d1..18019f037bae 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -361,36 +361,6 @@ static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, } #endif -#ifndef mkold_ptes -/** - * mkold_ptes - Mark PTEs that map consecutive pages of the same folio as old. - * @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. - * - * May be overridden by the architecture; otherwise, implemented as a simple - * loop over ptep_test_and_clear_young(). - * - * 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_ptes(struct vm_area_struct *vma, unsigned long addr, - pte_t *ptep, unsigned int nr) -{ - for (;;) { - ptep_test_and_clear_young(vma, addr, ptep); - if (--nr == 0) - break; - ptep++; - addr += PAGE_SIZE; - } -} -#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, @@ -489,6 +459,50 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, } #endif +#ifndef clear_young_dirty_ptes +/** + * clear_young_dirty_ptes - Mark PTEs that map consecutive pages of the + * same folio as old/clean. + * @mm: Address space 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/clean. + * @flags: Flags to modify the PTE batch semantics. + * + * May be overridden by the architecture; otherwise, implemented by + * get_and_clear/modify/set for each pte in the range. + * + * 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 clear_young_dirty_ptes(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, + unsigned int nr, cydp_t flags) +{ + pte_t pte; + + for (;;) { + if (flags == CYDP_CLEAR_YOUNG) + ptep_test_and_clear_young(vma, addr, ptep); + else { + pte = ptep_get_and_clear(vma->vm_mm, addr, ptep); + if (flags & CYDP_CLEAR_YOUNG) + pte = pte_mkold(pte); + if (flags & CYDP_CLEAR_DIRTY) + pte = pte_mkclean(pte); + set_pte_at(vma->vm_mm, addr, ptep, pte); + } + if (--nr == 0) + break; + ptep++; + addr += PAGE_SIZE; + } +} +#endif + static inline void ptep_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) { diff --git a/mm/madvise.c b/mm/madvise.c index 4b869b682fd5..f5e3699e7b54 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -507,7 +507,8 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, continue; if (!pageout && pte_young(ptent)) { - mkold_ptes(vma, addr, pte, nr); + clear_young_dirty_ptes(vma, addr, pte, nr, + CYDP_CLEAR_YOUNG); tlb_remove_tlb_entries(tlb, pte, nr, addr); }