From: Barry Song <21cnbao@gmail.com>
To: baolin.wang@linux.alibaba.com
Cc: 21cnbao@gmail.com, akpm@linux-foundation.org, chrisl@kernel.org,
david@redhat.com, ioworker0@gmail.com, kasong@tencent.com,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-riscv@lists.infradead.org, lorenzo.stoakes@oracle.com,
ryan.roberts@arm.com, v-songbaohua@oppo.com, x86@kernel.org,
ying.huang@intel.com, zhengtangquan@oppo.com
Subject: Re: [PATCH v2 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap
Date: Tue, 14 Jan 2025 19:00:59 +1300 [thread overview]
Message-ID: <20250114060059.14058-1-21cnbao@gmail.com> (raw)
In-Reply-To: <20250114040914.9986-1-21cnbao@gmail.com>
> > > if (!pvmw.pte) {
> > > + lazyfree = folio_test_anon(folio) && !folio_test_swapbacked(folio);
> >
> > You've checked lazyfree here, so can we remove the duplicate check in
> > unmap_huge_pmd_locked()? Then the code should be:
> >
> > if (lazyfree && unmap_huge_pmd_locked(...))
> > goto walk_done;
>
>
> right. it seems unmap_huge_pmd_locked() only handles lazyfree pmd-mapped
> thp. so i guess the code could be:
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index aea49f7125f1..c4c3a7896de4 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -3131,11 +3131,10 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
> 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));
> + VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio);
> + VM_WARN_ON_FOLIO(folio_test_swapbacked(folio), folio);
>
> - if (folio_test_anon(folio) && !folio_test_swapbacked(folio))
> - return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
> -
> - return false;
> + return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
> }
>
> static void remap_page(struct folio *folio, unsigned long nr, int flags)
> diff --git a/mm/rmap.c b/mm/rmap.c
> index 02c4e4b2cd7b..72907eb1b8fe 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -1671,7 +1671,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
> DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0);
> pte_t pteval;
> struct page *subpage;
> - bool anon_exclusive, lazyfree, ret = true;
> + bool anon_exclusive, ret = true;
> struct mmu_notifier_range range;
> enum ttu_flags flags = (enum ttu_flags)(long)arg;
> int nr_pages = 1;
> @@ -1724,18 +1724,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
> }
>
> if (!pvmw.pte) {
> - lazyfree = folio_test_anon(folio) && !folio_test_swapbacked(folio);
> -
> - if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd,
> - folio))
> - goto walk_done;
> - /*
> - * unmap_huge_pmd_locked has either already marked
> - * the folio as swap-backed or decided to retain it
> - * due to GUP or speculative references.
> - */
> - if (lazyfree)
> + if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) {
> + if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio))
> + goto walk_done;
> + /*
> + * unmap_huge_pmd_locked has either already marked
> + * the folio as swap-backed or decided to retain it
> + * due to GUP or speculative references.
> + */
> goto walk_abort;
> + }
>
> if (flags & TTU_SPLIT_HUGE_PMD) {
> /*
>
> >
> > > if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd,
> > > folio))
> > > goto walk_done;
> > > + /*
> > > + * unmap_huge_pmd_locked has either already marked
> > > + * the folio as swap-backed or decided to retain it
> > > + * due to GUP or speculative references.
> > > + */
> > > + if (lazyfree)
> > > + goto walk_abort;
> > >
> > > if (flags & TTU_SPLIT_HUGE_PMD) {
> > > /*
The final diff is as follows.
Baolin, do you have any additional comments before I send out v3?
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 3d3ebdc002d5..47cc8c3f8f80 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -3070,8 +3070,12 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
int ref_count, map_count;
pmd_t orig_pmd = *pmdp;
- if (folio_test_dirty(folio) || pmd_dirty(orig_pmd))
+ if (pmd_dirty(orig_pmd))
+ folio_set_dirty(folio);
+ if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) {
+ folio_set_swapbacked(folio);
return false;
+ }
orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp);
@@ -3098,8 +3102,15 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
*
* The only folio refs must be one from isolation plus the rmap(s).
*/
- if (folio_test_dirty(folio) || pmd_dirty(orig_pmd) ||
- ref_count != map_count + 1) {
+ if (pmd_dirty(orig_pmd))
+ folio_set_dirty(folio);
+ if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) {
+ folio_set_swapbacked(folio);
+ set_pmd_at(mm, addr, pmdp, orig_pmd);
+ return false;
+ }
+
+ if (ref_count != map_count + 1) {
set_pmd_at(mm, addr, pmdp, orig_pmd);
return false;
}
@@ -3119,12 +3130,11 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
{
VM_WARN_ON_FOLIO(!folio_test_pmd_mappable(folio), folio);
VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
+ VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio);
+ VM_WARN_ON_FOLIO(folio_test_swapbacked(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;
+ return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
}
static void remap_page(struct folio *folio, unsigned long nr, int flags)
diff --git a/mm/rmap.c b/mm/rmap.c
index 3ef659310797..72907eb1b8fe 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1724,9 +1724,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
}
if (!pvmw.pte) {
- if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd,
- folio))
- goto walk_done;
+ if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) {
+ if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio))
+ goto walk_done;
+ /*
+ * unmap_huge_pmd_locked has either already marked
+ * the folio as swap-backed or decided to retain it
+ * due to GUP or speculative references.
+ */
+ goto walk_abort;
+ }
if (flags & TTU_SPLIT_HUGE_PMD) {
/*
--
2.39.3 (Apple Git-146)
next prev parent reply other threads:[~2025-01-14 6:02 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-13 3:38 [PATCH v2 0/4] mm: batched unmap lazyfree large folios during reclamation Barry Song
2025-01-13 3:38 ` [PATCH v2 1/4] mm: Set folio swapbacked iff folios are dirty in try_to_unmap_one Barry Song
2025-01-13 13:19 ` David Hildenbrand
2025-01-13 13:20 ` David Hildenbrand
2025-01-13 21:56 ` Barry Song
2025-01-14 2:55 ` Baolin Wang
2025-01-14 6:05 ` Lance Yang
2025-01-13 3:38 ` [PATCH v2 2/4] mm: Support tlbbatch flush for a range of PTEs Barry Song
2025-01-13 16:48 ` Will Deacon
2025-01-14 9:52 ` David Hildenbrand
2025-01-14 10:37 ` Barry Song
2025-01-13 3:39 ` [PATCH v2 3/4] mm: Support batched unmap for lazyfree large folios during reclamation Barry Song
2025-01-13 3:39 ` [PATCH v2 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap Barry Song
2025-01-14 3:40 ` Baolin Wang
2025-01-14 4:09 ` Barry Song
2025-01-14 6:00 ` Barry Song [this message]
2025-01-14 7:51 ` Baolin Wang
2025-01-14 6:30 ` Lance Yang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250114060059.14058-1-21cnbao@gmail.com \
--to=21cnbao@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=chrisl@kernel.org \
--cc=david@redhat.com \
--cc=ioworker0@gmail.com \
--cc=kasong@tencent.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-riscv@lists.infradead.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=ryan.roberts@arm.com \
--cc=v-songbaohua@oppo.com \
--cc=x86@kernel.org \
--cc=ying.huang@intel.com \
--cc=zhengtangquan@oppo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox