From patchwork Thu Apr 18 13:44: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: 13634763 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 BA191C4345F for ; Thu, 18 Apr 2024 13:45:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E90746B0093; Thu, 18 Apr 2024 09:44:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DCB6B6B0096; Thu, 18 Apr 2024 09:44:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BCD616B0099; Thu, 18 Apr 2024 09:44:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8A7326B0093 for ; Thu, 18 Apr 2024 09:44:59 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 343151A13C7 for ; Thu, 18 Apr 2024 13:44:59 +0000 (UTC) X-FDA: 82022773518.23.7CFF677 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by imf25.hostedemail.com (Postfix) with ESMTP id 596A5A0023 for ; Thu, 18 Apr 2024 13:44:56 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=EFJm8QRM; spf=pass (imf25.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.214.176 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=1713447896; a=rsa-sha256; cv=none; b=kPK3518/KbuOD9svJ46StRfp+Kkt/L5re282eulKE/EqabYX3hs09mOr+q/Z7YVzN2kjV+ mXjlrCINNJxaFd4W6lLfMowVqNnSQThxEajXPknaipCOjYzZtBtsqxm0IvPEGjjJ890f2Y XdrU0/f2mFLxeB0pkPsXwYvrTMGiZhc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=EFJm8QRM; spf=pass (imf25.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.214.176 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=1713447896; 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=2bptmvyfGYVn+izLGYKs1uKRCf7NdX4woHvJn9zQskE=; b=wVxFbHfsxacvigUCmdtnYuFDNLIp0i0MNYdvUxWCy2YRikzojxg94ccfybygYwGjEQ1PtZ no8IuPOwh31/PTlvkQf8FLvVIPPggVr/ejM9IBulKK1mJbxReFeRqZlh1OHZGrWl9F6g82 4GPA/RwCagXjsy02YUUuNCnRh9CyHbc= Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-1e83a2a4f2cso5238805ad.1 for ; Thu, 18 Apr 2024 06:44:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713447895; x=1714052695; 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=2bptmvyfGYVn+izLGYKs1uKRCf7NdX4woHvJn9zQskE=; b=EFJm8QRMQWgeGrS3hGAlkKb4ZJ5Mj/a3VIyoQ651nJPR0ADoaJCh0My0DVGxYD6BKP QI0d+7c6a+PP3V5sZOEwBIZXsumRG8QP4PZQv6sdEBoCOpNh6hhp3xUQKJ9J+z3nNdNF 9k+s2f58FPU8T/6TKw5I6y6bUa6bXDGWbM0/vlbbd/KgipjFMvbB41tb6Lxs+SKtAVpi vMIvm4+kxW50q0+KA3y5LIxWqbwnaLOD/+Fy3cKhvNQC73WxiJ/acSaK5Gg50tQ/7U03 112sxNaBgRD+Dy9gkI2liMQz8blvsNP+C+hyLXDKE+BXP+KuQSMqBg2EcMnIseyrGL7j DKWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713447895; x=1714052695; 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=2bptmvyfGYVn+izLGYKs1uKRCf7NdX4woHvJn9zQskE=; b=aTK3c9IwR0QeF+JTsI5U9pHRP3z3hJJ/FPGIz2ytOxvo8lXIjg7d3ErOyWEKcqN86i bhHHCjJoaRXDgdbCg3X3txVcW8vT0ZSoiSvFPLag/EnjKfqR2So6o2++5Ueqh8X9KFs6 jEnpwmICZU3E+2PGU2Jw8QERidAqQdtq9JvODMQs8iU1bJez1UnmGXhWK64KmsRTVKW/ tr0b2+sDtizj6Xe+rIeigXtYWX6F7n+cjm68MPAaXlhf2AlIjn3+j0b4LJ5BD6mB1AXT t+8BOtArKUjwm+eizxzPOOfEMQX/VErsdn9QvUCy0XHN5Hbj3Ch+V7y78CvVkkcbtMM0 7Nyg== X-Forwarded-Encrypted: i=1; AJvYcCWTAqfbj/gal2Kmd8Fkk1Z/SM4osMBH3aRihWCipZPANOhJTzCjprIKskJAnEL6KiTy8aceR+ObSRhAbT/OUNA4upw= X-Gm-Message-State: AOJu0YzuD/i9svSUGusbjFZMIGD+LkovUaNkUJN+zfPYfH/epL2UUYGg jWtvKTxoYFtetXxhyK9KVD9j1jSEH7wMUmoj5AcHVpf6TTdxGVPS X-Google-Smtp-Source: AGHT+IFF3gVncyN3TR7hbP07PirPfqqnSr7IxJxLr9POXIPSQcFBSJypWZU1PP2Je3jZ98XwlW4btg== X-Received: by 2002:a17:902:f687:b0:1e4:3e67:2bbb with SMTP id l7-20020a170902f68700b001e43e672bbbmr2797169plg.48.1713447895139; Thu, 18 Apr 2024 06:44:55 -0700 (PDT) Received: from LancedeMBP.lan ([112.10.225.217]) by smtp.gmail.com with ESMTPSA id d8-20020a170902b70800b001e4fdcf67desm1504837pls.299.2024.04.18.06.44.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 06:44:54 -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 v10 3/4] mm/memory: add any_dirty optional pointer to folio_pte_batch() Date: Thu, 18 Apr 2024 21:44:34 +0800 Message-Id: <20240418134435.6092-4-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240418134435.6092-1-ioworker0@gmail.com> References: <20240418134435.6092-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 596A5A0023 X-Stat-Signature: r9qug1uw98j3y1qwssw6k7wi3jikwndu X-Rspam-User: X-HE-Tag: 1713447896-878588 X-HE-Meta: U2FsdGVkX19O5iN5XPdpLm8EjfCcNHLPJX7Yya2Evh3aEfzC6AhCJToaxGZuqzEJ/JWNg1IC1U8mBiy8T0efZmqbj0KydAgFuz71fCdMftrQmnmzBZElynzDtX5NI8RdvSVIXzc2u276ilt3kIRHUZk9IWBaHEUi+Sm91re+4O1YlFmmiFwjVsbb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6RtzIxaRrsNtWGP2SINK5QrFQOOWZ2Nv0mHz9516JG178/b9ypp7HQbQgrrloqT2hLXlRJLM1MJ21B5R4w4P3wnFoB9y/vkoIyXc21XZ30e2qPV0SqhGUcTRYLc+JwHRLp3jZ8iXiVE6yX4yzxNWs4pFn7pGFoP3dxqGZ29yREk36gnKLX3kFTSsqytlXpUQNrc/xXG5m+xAfVitMKxMkVQcJVHfZ/WGxoy8= 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 adds the any_dirty pointer as an optional parameter to folio_pte_batch() function. By using both the any_young and any_dirty pointers, madvise_free can make smarter decisions about whether to clear the PTEs when marking large folios as lazyfree. Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Signed-off-by: Lance Yang --- mm/internal.h | 12 ++++++++++-- mm/madvise.c | 19 ++++++++++++++----- mm/memory.c | 4 ++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index c6483f73ec13..daa59cef85d7 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -134,6 +134,8 @@ static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) * first one is writable. * @any_young: Optional pointer to indicate whether any entry except the * first one is young. + * @any_dirty: Optional pointer to indicate whether any entry except the + * first one is dirty. * * Detect a PTE batch: consecutive (present) PTEs that map consecutive * pages of the same large folio. @@ -149,18 +151,20 @@ static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) */ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, pte_t *start_ptep, pte_t pte, int max_nr, fpb_t flags, - bool *any_writable, bool *any_young) + bool *any_writable, bool *any_young, bool *any_dirty) { unsigned long folio_end_pfn = folio_pfn(folio) + folio_nr_pages(folio); const pte_t *end_ptep = start_ptep + max_nr; pte_t expected_pte, *ptep; - bool writable, young; + bool writable, young, dirty; int nr; if (any_writable) *any_writable = false; if (any_young) *any_young = false; + if (any_dirty) + *any_dirty = false; VM_WARN_ON_FOLIO(!pte_present(pte), folio); VM_WARN_ON_FOLIO(!folio_test_large(folio) || max_nr < 1, folio); @@ -176,6 +180,8 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, writable = !!pte_write(pte); if (any_young) young = !!pte_young(pte); + if (any_dirty) + dirty = !!pte_dirty(pte); pte = __pte_batch_clear_ignored(pte, flags); if (!pte_same(pte, expected_pte)) @@ -193,6 +199,8 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, *any_writable |= writable; if (any_young) *any_young |= young; + if (any_dirty) + *any_dirty |= dirty; nr = pte_batch_hint(ptep, pte); expected_pte = pte_advance_pfn(expected_pte, nr); diff --git a/mm/madvise.c b/mm/madvise.c index f5e3699e7b54..4597a3568e7e 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -321,6 +321,18 @@ static inline bool can_do_file_pageout(struct vm_area_struct *vma) file_permission(vma->vm_file, MAY_WRITE) == 0; } +static inline int madvise_folio_pte_batch(unsigned long addr, unsigned long end, + struct folio *folio, pte_t *ptep, + pte_t pte, bool *any_young, + bool *any_dirty) +{ + const fpb_t fpb_flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY; + int max_nr = (end - addr) / PAGE_SIZE; + + return folio_pte_batch(folio, addr, ptep, pte, max_nr, fpb_flags, NULL, + any_young, any_dirty); +} + static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, struct mm_walk *walk) @@ -456,13 +468,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, * next pte in the range. */ if (folio_test_large(folio)) { - const fpb_t fpb_flags = FPB_IGNORE_DIRTY | - FPB_IGNORE_SOFT_DIRTY; - int max_nr = (end - addr) / PAGE_SIZE; bool any_young; - nr = folio_pte_batch(folio, addr, pte, ptent, max_nr, - fpb_flags, NULL, &any_young); + nr = madvise_folio_pte_batch(addr, end, folio, pte, + ptent, &any_young, NULL); if (any_young) ptent = pte_mkyoung(ptent); diff --git a/mm/memory.c b/mm/memory.c index 33d87b64d15d..9e07d1b9020c 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -989,7 +989,7 @@ copy_present_ptes(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma flags |= FPB_IGNORE_SOFT_DIRTY; nr = folio_pte_batch(folio, addr, src_pte, pte, max_nr, flags, - &any_writable, NULL); + &any_writable, NULL, NULL); folio_ref_add(folio, nr); if (folio_test_anon(folio)) { if (unlikely(folio_try_dup_anon_rmap_ptes(folio, page, @@ -1558,7 +1558,7 @@ static inline int zap_present_ptes(struct mmu_gather *tlb, */ if (unlikely(folio_test_large(folio) && max_nr != 1)) { nr = folio_pte_batch(folio, addr, pte, ptent, max_nr, fpb_flags, - NULL, NULL); + NULL, NULL, NULL); zap_present_folio_ptes(tlb, vma, folio, page, pte, ptent, nr, addr, details, rss, force_flush,