From: Yang Shi <shy828301@gmail.com>
To: Lance Yang <ioworker0@gmail.com>
Cc: akpm@linux-foundation.org, mhocko@suse.com, zokeefe@google.com,
david@redhat.com, songmuchun@bytedance.com, peterx@redhat.com,
minchan@kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/1] mm/khugepaged: skip copying lazyfree pages on collapse
Date: Fri, 2 Feb 2024 09:43:30 -0800 [thread overview]
Message-ID: <CAHbLzkq=7Z9PQFHOWnrbcS0YWexw40OSHbfQCNqV5+9gsDnYEQ@mail.gmail.com> (raw)
In-Reply-To: <CAK1f24ng_+rFd0Z5TiuZ86QUtso_09g5ksZ+p8Va+m-tK85JoQ@mail.gmail.com>
On Fri, Feb 2, 2024 at 3:23 AM Lance Yang <ioworker0@gmail.com> wrote:
>
> On Fri, Feb 2, 2024 at 4:37 AM Yang Shi <shy828301@gmail.com> wrote:
> >
> > On Thu, Feb 1, 2024 at 4:53 AM Lance Yang <ioworker0@gmail.com> wrote:
> > >
> > > The collapsing behavior of khugepaged with pages
> > > marked using MADV_FREE might cause confusion
> > > among users.
> > >
> > > For instance, allocate a 2MB chunk using mmap and
> > > later release it by MADV_FREE. Khugepaged will not
> > > collapse this chunk. From the user's perspective,
> > > it treats lazyfree pages as pte_none. However,
> > > for some pages marked as lazyfree with MADV_FREE,
> > > khugepaged might collapse this chunk and copy
> > > these pages to a new huge page. This inconsistency
> > > in behavior could be confusing for users.
> > >
> > > After a successful MADV_FREE operation, if there is
> > > no subsequent write, the kernel can free the pages
> > > at any time. Therefore, in my opinion, counting
> > > lazyfree pages in max_pte_none seems reasonable.
> > >
> > > Perhaps treating MADV_FREE like MADV_DONTNEED, not
> > > copying lazyfree pages when khugepaged collapses
> > > huge pages in the background better aligns with
> > > user expectations.
> > >
> > > Signed-off-by: Lance Yang <ioworker0@gmail.com>
> > > ---
> > > mm/khugepaged.c | 10 +++++++++-
> > > 1 file changed, 9 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> > > index 2b219acb528e..6cbf46d42c6a 100644
> > > --- a/mm/khugepaged.c
> > > +++ b/mm/khugepaged.c
> > > @@ -777,6 +777,7 @@ static int __collapse_huge_page_copy(pte_t *pte,
> > > pmd_t orig_pmd,
> > > struct vm_area_struct *vma,
> > > unsigned long address,
> > > + struct collapse_control *cc,
> > > spinlock_t *ptl,
> > > struct list_head *compound_pagelist)
> > > {
> > > @@ -797,6 +798,13 @@ static int __collapse_huge_page_copy(pte_t *pte,
> > > continue;
> > > }
> > > src_page = pte_page(pteval);
> > > +
> > > + if (cc->is_khugepaged
> > > + && !folio_test_swapbacked(page_folio(src_page))) {
> > > + clear_user_highpage(page, _address);
> > > + continue;
> >
> > If the page was written before khugepaged collapsed it, and khugepaged
> > collapsed the page before memory reclaim kicked in, didn't this
> > somehow cause data corruption?
> >
>
> Thanks a lot! Yang, you're correct; indeed, there is
> a potential issue with data corruption.
>
> I took a look at the check for lazyfree pages in
> smaps_pte_entry.
>
> Here's the modification:
> if (cc->is_khugepaged && !PageSwapBacked(src_page)
> && !pte_dirty(pteval) && !PageDirty(src_page)) {
> clear_user_highpage(page, _address);
> continue;
> }
This may be ok. But as I said in another reply, this may still incur
data corruption.
>
> Could you please take a look?
>
> Thanks,
> Lance
>
> > > + }
> > > +
> > > if (copy_mc_user_highpage(page, src_page, _address, vma) > 0) {
> > > result = SCAN_COPY_MC;
> > > break;
> > > @@ -1205,7 +1213,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
> > > anon_vma_unlock_write(vma->anon_vma);
> > >
> > > result = __collapse_huge_page_copy(pte, hpage, pmd, _pmd,
> > > - vma, address, pte_ptl,
> > > + vma, address, cc, pte_ptl,
> > > &compound_pagelist);
> > > pte_unmap(pte);
> > > if (unlikely(result != SCAN_SUCCEED))
> > > --
> > > 2.33.1
> > >
next prev parent reply other threads:[~2024-02-02 17:43 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-01 12:52 Lance Yang
2024-02-01 13:49 ` Lance Yang
2024-02-01 20:37 ` Yang Shi
2024-02-02 11:23 ` Lance Yang
2024-02-02 17:43 ` Yang Shi [this message]
2024-02-02 10:06 ` Michal Hocko
2024-02-02 11:18 ` Lance Yang
2024-02-02 12:27 ` Michal Hocko
2024-02-02 12:52 ` Lance Yang
2024-02-02 12:57 ` Michal Hocko
2024-02-02 13:46 ` Lance Yang
2024-02-02 14:20 ` Lance Yang
2024-02-02 14:42 ` Michal Hocko
2024-02-02 14:52 ` Lance Yang
2024-02-02 15:26 ` David Hildenbrand
2024-02-02 15:38 ` Michal Hocko
2024-02-02 17:42 ` Yang Shi
2024-02-03 4:17 ` Lance Yang
2024-02-05 19:41 ` Yang Shi
2024-02-05 9:45 ` Michal Hocko
2024-02-05 19:43 ` Yang Shi
2024-02-05 20:26 ` Zach O'Keefe
2024-02-20 10:15 ` 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='CAHbLzkq=7Z9PQFHOWnrbcS0YWexw40OSHbfQCNqV5+9gsDnYEQ@mail.gmail.com' \
--to=shy828301@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=ioworker0@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=minchan@kernel.org \
--cc=peterx@redhat.com \
--cc=songmuchun@bytedance.com \
--cc=zokeefe@google.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