Re: [PATCH 1/1] mm/madvise: enhance lazyfreeing with mTHP in madvise_free

From: Lance Yang
Date: Mon Feb 26 2024 - 04:17:33 EST


Hey Fengwei,

Thanks for taking time to review!

> On Mon, Feb 26, 2024 at 10:38 AM Yin Fengwei <fengwei.yin@xxxxxxxxx> wrote:
> > On Sun, Feb 25, 2024 at 8:32 PM Lance Yang <ioworker0@xxxxxxxxx> wrote:
[...]
> > --- a/mm/madvise.c
> > +++ b/mm/madvise.c
> > @@ -676,11 +676,43 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
> > */
> > if (folio_test_large(folio)) {
> > int err;
> > + unsigned long next_addr, align;
> >
> > - if (folio_estimated_sharers(folio) != 1)
> > - break;
> > - if (!folio_trylock(folio))
> > - break;
> > + if (folio_estimated_sharers(folio) != 1 ||
> > + !folio_trylock(folio))
> > + goto skip_large_folio;
> > +
> > + align = folio_nr_pages(folio) * PAGE_SIZE;
> > + next_addr = ALIGN_DOWN(addr + align, align);
> There is a possible corner case:
> If there is a cow folio associated with this folio and the cow folio
> has smaller size than this folio for whatever reason, this change can't
> handle it correctly.

Thanks for pointing that out; it's very helpful to me!
I made some changes. Could you please check if this corner case is now resolved?

As a diff against this patch.

diff --git a/mm/madvise.c b/mm/madvise.c
index bcbf56595a2e..c7aacc9f9536 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -686,10 +686,12 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
next_addr = ALIGN_DOWN(addr + align, align);

/*
- * If we mark only the subpages as lazyfree,
- * split the large folio.
+ * If we mark only the subpages as lazyfree, or
+ * if there is a cow folio associated with this folio,
+ * then split the large folio.
*/
- if (next_addr > end || next_addr - addr != align)
+ if (next_addr > end || next_addr - addr != align ||
+ folio_total_mapcount(folio) != folio_nr_pages(folio))
goto split_large_folio;

/*
---

Thanks again for your time!

Best,
Lance