From patchwork Mon Jun 10 12:08:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13691947 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 E53BDC27C55 for ; Mon, 10 Jun 2024 12:08:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7D52C6B009C; Mon, 10 Jun 2024 08:08:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 784176B009D; Mon, 10 Jun 2024 08:08:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 64D446B009E; Mon, 10 Jun 2024 08:08:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 464736B009C for ; Mon, 10 Jun 2024 08:08:25 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id BE55614081E for ; Mon, 10 Jun 2024 12:08:24 +0000 (UTC) X-FDA: 82214856528.03.540974D Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf19.hostedemail.com (Postfix) with ESMTP id C89831A000E for ; Mon, 10 Jun 2024 12:08:22 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=OPhfF4Zo; spf=pass (imf19.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.215.182 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=1718021302; a=rsa-sha256; cv=none; b=Qg2SqpYLEZ4NQ8CpKCAYbvga4+u2P7abwZ5ggL86EQD05DkLgCAyVYi/PhN2uuScuWbGJn VJA+8QU8IQ0Zl6FDGd5uVpfyb3cop0nHPnsgFwQsrjxD8wgz839Oz1q4FFvptUwfW7DESX 6Nr1Mc47Uf6PO2n+2A87zRbQYatTUdg= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=OPhfF4Zo; spf=pass (imf19.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.215.182 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=1718021302; 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=sOCokgIFZnMYHU7kHatHCs1R7LhgAcEFFxJDcineuXY=; b=q9ue5fHGlXocXXZ5RevMPruFRbgOgRV9ndBkXtR6G3IXq+r7kAwJp1pf/0veA7UV2Lm8TI FLC3lOYMIbWBtIW6SOT/cv5KOf33f9BWb6sSxrnPkwEqntreGgm+RF/xVtvHOM+Bd2InRT zyXXwHRc/P410MZS/9TFg/m7+vCVWok= Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-6c5a6151ff8so3325712a12.2 for ; Mon, 10 Jun 2024 05:08:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718021302; x=1718626102; 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=sOCokgIFZnMYHU7kHatHCs1R7LhgAcEFFxJDcineuXY=; b=OPhfF4ZoZRSw8doXiYBoad2idKn24EecDiOZ4XHE3KAzXiHL61iq0qPU0TiutVZkj0 GZy2Pt+MyBczXxqVdFV8q92BEM1rlaHILyLglHhVBGqEKXwMNwcR3o/LA2fLx0bPmghZ ri3CK7fWhbp8J1msZ3MJHTTjcDTDcc2G6IZAURdQlpRm93X85L6uhp8RBG3mRdZXpOWb zTXrS1LKIK2T+XWloE6z9lqTzAEPQHXDEsE/FQplU5VMpAfJ/KEmPOU0sy2SCPY+vFFj pPuNxfh81THoxXTks+Hb2RdPrDmRJXOWvIevy86nmS+K0T5xTAs0qTiLXQOEtIci0hSs VXxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718021302; x=1718626102; 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=sOCokgIFZnMYHU7kHatHCs1R7LhgAcEFFxJDcineuXY=; b=XrX3YPElPs+1cb45XukYHA7xUDQtkvOGaQkC3uYE4TUcxSFXdcwSVSk3tgn+Y7QbQ/ Cak2yjSovo104zF8QMQ4zQHDR4ifaO+oke/pAJfy1WAKQW2VGYVCMcg3ggmlWCWDGjw2 BjHjiIsew7FxzYPT6ZtrhtpfBpqp+XC+iU83gwcHcUR4U2IuM8pBE2ydqOou03kgYHIx NvPeQgkY+rjnR4mdrg6DbFLO1AdLOiyl08RiHE+pXIXD6CS9XMNyq6oaDfdx18uK9PbM 3OolD3o46aLiqSF1YAnTrw0u/IA0j1NPc5Udoi6LP7AtfrinV1QUQsbP2VgF2azjDwir stlg== X-Forwarded-Encrypted: i=1; AJvYcCVieF9xxfNLmQVDWUl+C58ZJHNhHSw01Ps3727k2y39Cgrn0bDqq6XJvkw+xCN/IAMdi13uAAylhdHTnbOYnoUp04M= X-Gm-Message-State: AOJu0Yz0QkZjZliXfiKEh6bdNEyZlw62PaiV4wj5zioEK4DAHlMxab11 MLrPwfn4qDdOEAz6vMCcGOCQJ9ncscr7j9d2okhNQBJsiZsDQb6V X-Google-Smtp-Source: AGHT+IGiVdBdiRyrTFD3fLPkOV4Q3GrQwZQWB402am+vntnYPikYw03+qIKJb0cFryGT4hjcfjPjvA== X-Received: by 2002:a17:903:2b0e:b0:1f4:5dc0:5fe8 with SMTP id d9443c01a7336-1f6d02e22efmr107290335ad.15.1718021301570; Mon, 10 Jun 2024 05:08:21 -0700 (PDT) Received: from LancedeMBP.lan.lan ([2403:2c80:6::304c]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6bd76c1d2sm81011355ad.107.2024.06.10.05.08.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 05:08:21 -0700 (PDT) From: Lance Yang To: ioworker0@gmail.com Cc: 21cnbao@gmail.com, akpm@linux-foundation.org, baolin.wang@linux.alibaba.com, david@redhat.com, fengwei.yin@intel.com, libang.li@antgroup.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, maskray@google.com, mhocko@suse.com, minchan@kernel.org, peterx@redhat.com, ryan.roberts@arm.com, shy828301@gmail.com, sj@kernel.org, songmuchun@bytedance.com, wangkefeng.wang@huawei.com, willy@infradead.org, xiehuan09@gmail.com, ziy@nvidia.com, zokeefe@google.com Subject: [PATCH v7 4/4] mm/vmscan: avoid split lazyfree THP during shrink_folio_list() Date: Mon, 10 Jun 2024 20:08:09 +0800 Message-Id: <20240610120809.66601-1-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240610120209.66311-1-ioworker0@gmail.com> References: <20240610120209.66311-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: C89831A000E X-Stat-Signature: rftjycnyudbteatfmmugjg41615764tt X-HE-Tag: 1718021302-15565 X-HE-Meta: U2FsdGVkX1+4b/5Jcgpod7cYFy6aYnCOiH/eVwsc7JUKGAyzvq0HFDOma/j0Xrp4OXBToPsp0VZiHYam2YqPHcea1OWUARy6jBjv9fllzCfzlgNptkv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h9ydgik+62S+D87et7HuuI3KkvUEkPB53TxV6RT6U92HKWh7boTmX29I7eu59DCUDzpzBxUelfLCyo6yqVuuTXxwtYZ0Hp2W3Wyn+uRR7jRN9EgF5UtffAAOyJnCi/M8UEhmCuV53hUGKymg2NkmGZIPzwmEESswGrXat1+X3vfVMvr6M2OPLWpNwTd+KSF6yr9zkjGNLnYb/Q4zPw9h1BFKp0iZy6y6dkzaLtJGOBP3kF0rvE+QQf0aV7tin6PH8fDPqB9/oPSYu7IpLSYVRq+1768JO/KSMYbLVhVlWyT91fQfADfdfYNXx+sv9gvhkypHz0/uXfKrh0/E2B+qS43/lvDtPv9XjSNaU= 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: When the user no longer requires the pages, they would use madvise(MADV_FREE) to mark the pages as lazy free. Subsequently, they typically would not re-write to that memory again. During memory reclaim, if we detect that the large folio and its PMD are both still marked as clean and there are no unexpected references (such as GUP), so we can just discard the memory lazily, improving the efficiency of memory reclamation in this case. On an Intel i5 CPU, reclaiming 1GiB of lazyfree THPs using mem_cgroup_force_empty() results in the following runtimes in seconds (shorter is better): -------------------------------------------- | Old | New | Change | -------------------------------------------- | 0.683426 | 0.049197 | -92.80% | -------------------------------------------- Suggested-by: Zi Yan Suggested-by: David Hildenbrand Signed-off-by: Lance Yang --- include/linux/huge_mm.h | 9 +++++ mm/huge_memory.c | 80 +++++++++++++++++++++++++++++++++++++++++ mm/rmap.c | 36 +++++++++++++------ 3 files changed, 114 insertions(+), 11 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 4670c6ee118b..020e2344eb86 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -417,6 +417,8 @@ static inline bool thp_migration_supported(void) void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, bool freeze, struct folio *folio); +bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, + pmd_t *pmdp, struct folio *folio); #else /* CONFIG_TRANSPARENT_HUGEPAGE */ @@ -484,6 +486,13 @@ static inline void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, bool freeze, struct folio *folio) {} +static inline bool unmap_huge_pmd_locked(struct vm_area_struct *vma, + unsigned long addr, pmd_t *pmdp, + struct folio *folio) +{ + return false; +} + #define split_huge_pud(__vma, __pmd, __address) \ do { } while (0) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index d2697cc8f9d4..19592d3f1167 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2687,6 +2687,86 @@ static void unmap_folio(struct folio *folio) try_to_unmap_flush(); } +static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma, + unsigned long addr, pmd_t *pmdp, + struct folio *folio) +{ + VM_WARN_ON_FOLIO(folio_test_swapbacked(folio), folio); + VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); + + struct mm_struct *mm = vma->vm_mm; + int ref_count, map_count; + pmd_t orig_pmd = *pmdp; + struct page *page; + + if (unlikely(!pmd_present(orig_pmd) || !pmd_trans_huge(orig_pmd))) + return false; + + page = pmd_page(orig_pmd); + if (unlikely(page_folio(page) != folio)) + return false; + + if (folio_test_dirty(folio) || pmd_dirty(orig_pmd)) { + folio_set_swapbacked(folio); + return false; + } + + orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp); + + /* + * Syncing against concurrent GUP-fast: + * - clear PMD; barrier; read refcount + * - inc refcount; barrier; read PMD + */ + smp_mb(); + + ref_count = folio_ref_count(folio); + map_count = folio_mapcount(folio); + + /* + * Order reads for folio refcount and dirty flag + * (see comments in __remove_mapping()). + */ + smp_rmb(); + + /* + * If the folio or its PMD is redirtied at this point, or if there + * are unexpected references, we will give up to discard this folio + * and remap it. + * + * The only folio refs must be one from isolation plus the rmap(s). + */ + if (folio_test_dirty(folio) || pmd_dirty(orig_pmd)) + folio_set_swapbacked(folio); + + if (folio_test_swapbacked(folio) || ref_count != map_count + 1) { + set_pmd_at(mm, addr, pmdp, orig_pmd); + return false; + } + + folio_remove_rmap_pmd(folio, page, vma); + zap_deposited_table(mm, pmdp); + add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR); + if (vma->vm_flags & VM_LOCKED) + mlock_drain_local(); + folio_put(folio); + + return true; +} + +bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, + pmd_t *pmdp, struct folio *folio) +{ + VM_WARN_ON_FOLIO(!folio_test_pmd_mappable(folio), folio); + VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); + VM_WARN_ON_ONCE(!IS_ALIGNED(addr, HPAGE_PMD_SIZE)); + + if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) + return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio); + + return false; +} + static void remap_page(struct folio *folio, unsigned long nr) { int i = 0; diff --git a/mm/rmap.c b/mm/rmap.c index b77f88695588..8e901636ade9 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1630,6 +1630,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, enum ttu_flags flags = (enum ttu_flags)(long)arg; unsigned long pfn; unsigned long hsz = 0; + bool pmd_mapped = false; /* * When racing against e.g. zap_pte_range() on another cpu, @@ -1676,16 +1677,24 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, goto walk_done_err; } - if (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD)) { - /* - * We temporarily have to drop the PTL and start once - * again from that now-PTE-mapped page table. - */ - split_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, - false, folio); - flags &= ~TTU_SPLIT_HUGE_PMD; - page_vma_mapped_walk_restart(&pvmw); - continue; + if (!pvmw.pte) { + pmd_mapped = true; + if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, + folio)) + goto walk_done; + + if (flags & TTU_SPLIT_HUGE_PMD) { + /* + * We temporarily have to drop the PTL and start + * once again from that now-PTE-mapped page + * table. + */ + split_huge_pmd_locked(vma, pvmw.address, + pvmw.pmd, false, folio); + flags &= ~TTU_SPLIT_HUGE_PMD; + page_vma_mapped_walk_restart(&pvmw); + continue; + } } /* Unexpected PMD-mapped THP? */ @@ -1813,7 +1822,12 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, */ if (unlikely(folio_test_swapbacked(folio) != folio_test_swapcache(folio))) { - WARN_ON_ONCE(1); + /* + * unmap_huge_pmd_locked() will unmark a + * PMD-mapped folio as lazyfree if the folio or + * its PMD was redirtied. + */ + WARN_ON_ONCE(!pmd_mapped); goto walk_done_err; }