linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lance Yang <ioworker0@gmail.com>
To: "Zach O'Keefe" <zokeefe@google.com>
Cc: Yang Shi <shy828301@gmail.com>,
	akpm@linux-foundation.org, mhocko@suse.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: bypassing unnecessary scans with MMF_DISABLE_THP check
Date: Tue, 30 Jan 2024 10:37:26 +0800	[thread overview]
Message-ID: <CAK1f24mFmqTmBcx2YqQ=TwD0xhA1DhLwPhONp18yzqKdC--K=w@mail.gmail.com> (raw)
In-Reply-To: <CAAa6QmSSKFjzdG62MZ5bSrhP19AjPEr339WXaWY=OCLsGWVE8w@mail.gmail.com>

Hey Zach,

Thanks for taking time to review!

On Tue, Jan 30, 2024 at 3:04 AM Zach O'Keefe <zokeefe@google.com> wrote:
[...]
> IIUC, there really isn't any correctness race. Claim is just that we

Yes, there is indeed no correctness race.

> can avoid a number of per-vma checks. AFAICT, any task w/
> MMF_DISABLE_THP set will always have each and every vma checked
> (albeit, with a very inexpensive ->vm_mm->flags check)
[...]

IMO, for any task with MMF_DISABLE_THP set, the check
for each VMA can be skipped to avoid redundant operations,
(with a very inexpensive ->mm->flags check)
especially in scenarios with a large address space.

BR,
Lance

On Tue, Jan 30, 2024 at 3:04 AM Zach O'Keefe <zokeefe@google.com> wrote:
>
> On Mon, Jan 29, 2024 at 10:53 AM Yang Shi <shy828301@gmail.com> wrote:
> >
> > On Sun, Jan 28, 2024 at 9:46 PM Lance Yang <ioworker0@gmail.com> wrote:
> > >
> > > khugepaged scans the entire address space in the
> > > background for each given mm, looking for
> > > opportunities to merge sequences of basic pages
> > > into huge pages. However, when an mm is inserted
> > > to the mm_slots list, and the MMF_DISABLE_THP flag
> > > is set later, this scanning process becomes
> > > unnecessary for that mm and can be skipped to avoid
> > > redundant operations, especially in scenarios with
> > > a large address space.
> > >
> > > This commit introduces a check before each scanning
> > > process to test the MMF_DISABLE_THP flag for the
> > > given mm; if the flag is set, the scanning process
> > > is bypassed, thereby improving the efficiency of
> > > khugepaged.
> > >
> > > Signed-off-by: Lance Yang <ioworker0@gmail.com>
> > > ---
> > >  mm/khugepaged.c | 18 ++++++++++++------
> > >  1 file changed, 12 insertions(+), 6 deletions(-)
> > >
> > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> > > index 2b219acb528e..d6a700834edc 100644
> > > --- a/mm/khugepaged.c
> > > +++ b/mm/khugepaged.c
> > > @@ -410,6 +410,12 @@ static inline int hpage_collapse_test_exit(struct mm_struct *mm)
> > >         return atomic_read(&mm->mm_users) == 0;
> > >  }
> > >
> > > +static inline int hpage_collapse_test_exit_or_disable(struct mm_struct *mm)
> > > +{
> > > +       return hpage_collapse_test_exit(mm) ||
> > > +              test_bit(MMF_DISABLE_THP, &mm->flags);
> > > +}
> > > +
> > >  void __khugepaged_enter(struct mm_struct *mm)
> > >  {
> > >         struct khugepaged_mm_slot *mm_slot;
> > > @@ -1422,7 +1428,7 @@ static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
> > >
> > >         lockdep_assert_held(&khugepaged_mm_lock);
> > >
> > > -       if (hpage_collapse_test_exit(mm)) {
> > > +       if (hpage_collapse_test_exit_or_disable(mm)) {
> > >                 /* free mm_slot */
> > >                 hash_del(&slot->hash);
> > >                 list_del(&slot->mm_node);
> > > @@ -2360,7 +2366,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
> > >                 goto breakouterloop_mmap_lock;
> > >
> > >         progress++;
> > > -       if (unlikely(hpage_collapse_test_exit(mm)))
> > > +       if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
> > >                 goto breakouterloop;
> > >
> > >         vma_iter_init(&vmi, mm, khugepaged_scan.address);
> > > @@ -2368,7 +2374,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
> > >                 unsigned long hstart, hend;
> > >
> > >                 cond_resched();
> > > -               if (unlikely(hpage_collapse_test_exit(mm))) {
> > > +               if (unlikely(hpage_collapse_test_exit_or_disable(mm))) {
> >
> > The later thp_vma_allowable_order() does check whether MMF_DISABLE_THP
> > is set or not. And the hugepage_vma_revalidate() after re-acquiring
> > mmap_lock does the same check too. The checking in khugepaged should
> > be already serialized with prctl, which takes mmap_lock in write.
>
> IIUC, there really isn't any correctness race. Claim is just that we
> can avoid a number of per-vma checks. AFAICT, any task w/
> MMF_DISABLE_THP set will always have each and every vma checked
> (albeit, with a very inexpensive ->vm_mm->flags check)
>
> Thanks,
> Zach
>
> > >                         progress++;
> > >                         break;
> > >                 }
> > > @@ -2390,7 +2396,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
> > >                         bool mmap_locked = true;
> > >
> > >                         cond_resched();
> > > -                       if (unlikely(hpage_collapse_test_exit(mm)))
> > > +                       if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
> > >                                 goto breakouterloop;
> > >
> > >                         VM_BUG_ON(khugepaged_scan.address < hstart ||
> > > @@ -2408,7 +2414,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
> > >                                 fput(file);
> > >                                 if (*result == SCAN_PTE_MAPPED_HUGEPAGE) {
> > >                                         mmap_read_lock(mm);
> > > -                                       if (hpage_collapse_test_exit(mm))
> > > +                                       if (hpage_collapse_test_exit_or_disable(mm))
> > >                                                 goto breakouterloop;
> > >                                         *result = collapse_pte_mapped_thp(mm,
> > >                                                 khugepaged_scan.address, false);
> > > @@ -2450,7 +2456,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
> > >          * Release the current mm_slot if this mm is about to die, or
> > >          * if we scanned all vmas of this mm.
> > >          */
> > > -       if (hpage_collapse_test_exit(mm) || !vma) {
> > > +       if (hpage_collapse_test_exit_or_disable(mm) || !vma) {
> > >                 /*
> > >                  * Make sure that if mm_users is reaching zero while
> > >                  * khugepaged runs here, khugepaged_exit will find
> > > --
> > > 2.33.1
> > >

  reply	other threads:[~2024-01-30  2:37 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-29  5:45 [PATCH 1/1] mm/khugepaged: bypassing unnecessary scans with MMF_DISABLE_THP check Lance Yang
2024-01-29 16:28 ` Michal Hocko
2024-01-30  2:12   ` Lance Yang
2024-01-30  3:08     ` Lance Yang
2024-01-30  9:35       ` Michal Hocko
2024-01-30  9:46         ` Lance Yang
2024-01-31  0:37           ` Lance Yang
2024-01-29 18:53 ` Yang Shi
2024-01-29 19:03   ` Zach O'Keefe
2024-01-30  2:37     ` Lance Yang [this message]
2024-01-30  2:21   ` Lance Yang
2024-01-31  9:30 ` Lance Yang
2024-01-31 20:06   ` Yang Shi
2024-02-01  1:13     ` Lance Yang
2024-02-01 18:56       ` Yang Shi
2024-02-03  3:20         ` Lance Yang
2024-02-21 22:11   ` Andrew Morton
2024-02-22  7:43     ` Lance Yang
2024-02-22  7:51   ` Lance Yang
2024-02-22  8:51     ` David Hildenbrand
2024-02-22  9:12       ` Lance Yang
2024-02-22 20:23     ` Yang Shi
2024-02-22 21:11       ` Andrew Morton
2024-02-23  7:59         ` Lance Yang
2024-02-24  1:47         ` Yang Shi
2024-02-24  1:46 ` Yang Shi
2024-02-24  3:54   ` Lance Yang
2024-02-25  4:56     ` 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='CAK1f24mFmqTmBcx2YqQ=TwD0xhA1DhLwPhONp18yzqKdC--K=w@mail.gmail.com' \
    --to=ioworker0@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@redhat.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=shy828301@gmail.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;
as well as URLs for NNTP newsgroup(s).