Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: "David Hildenbrand (Arm)" <david@kernel.org>
Cc: Zi Yan <ziy@nvidia.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	 "Liam R. Howlett" <liam@infradead.org>,
	Nico Pache <nico.pache@linux.dev>,
	 Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
	Barry Song <baohua@kernel.org>,
	 Lance Yang <lance.yang@linux.dev>,
	Usama Arif <usama.arif@linux.dev>,
	 Kiryl Shutsemau <kas@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	 Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
	 Boqun Feng <boqun@kernel.org>, Waiman Long <longman@redhat.com>,
	 Suren Baghdasaryan <surenb@google.com>,
	Vlastimil Babka <vbabka@kernel.org>,
	 Shakeel Butt <shakeel.butt@linux.dev>,
	Yonghong Song <yonghong.song@linux.dev>,
	 Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	 Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	 Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	 Song Liu <song@kernel.org>, Jiri Olsa <jolsa@kernel.org>,
	 Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	linux-mm@kvack.org,  linux-kernel@vger.kernel.org,
	bpf@vger.kernel.org
Subject: Re: [PATCH 5/5] mm/khugepaged: drop/reacquire mmap read lock if skipped enough VMAs
Date: Thu, 5 Nov 2026 14:46:29 +0000	[thread overview]
Message-ID: <auyMmGmrW-6WhF4p@gremlin> (raw)
In-Reply-To: <c05cb7a5-35b7-4364-9b7a-a87e54bf0ecf@kernel.org>

On Thu, Nov 05, 2026 at 11:28:47AM +0100, David Hildenbrand (Arm) wrote:
> On 11/3/26 09:27, Lorenzo Stoakes (ARM) wrote:
> >  mm/khugepaged.c | 15 +++++++++++++++
> >  1 file changed, 15 insertions(+)
> >
> > diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> > index 216c4b8d28c4..b9c72ee8b421 100644
> > --- a/mm/khugepaged.c
> > +++ b/mm/khugepaged.c
> > @@ -66,6 +66,12 @@ static DEFINE_READ_MOSTLY_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
> >
> >  static struct kmem_cache *mm_slot_cache __ro_after_init;
> >
> > +/*
> > + * The number of VMAs with nothing to scan to walk before dropping and
> > + * reacquiring the mmap_lock to relieve lock contention.
> > + */
> > +#define KHUGEPAGED_MAX_SKIP_BATCH 1024
>
> I don't quite enjoy this magic number.

Yeah, it's trying to find the least impactful way of saying 'we spent a lot of
time in this loop' more or less.

It was empirically figured out to be a good choice (and is very conservative,
obviously).

An alternative here would be to literally track how much time has been spent in
the loop and if exceeded drop/reacquire lock? That felt icky though.

Another option could be to check to see if cc->progress is, say, 1/4 of
khugepage_pages_to_scan, and then drop/reacquire them?

Since actual collapse drops the lock + exits the loop anyway that should work
OK.

>
> Besides it saying nothing about "VMAS", so the name it self is pretty unclear.

Ack.

>
> I was wondering whether we can give the "cc->progress += 1" a different weight.
> Skipping VMAs seems to be more expensive than we thought.

That could get tricky, the ineligible VMA rejections are all relatively quick
and in any case we don't even check the budget until we reach an eligible one.

The eligible ones being rejected potentially take longer but it could get
complicated.

>
>
> (Maybe we should also just say "skip after X" VMAs in general, whether we
> skipped them or not?)

Hmm yeah true, you might actually find an eligible VMA (spans a PMD) but then
for one of a bunch of different reasons skip it and only cc->progress++, even
though you maybe spent quite some time doing it.

Could keep it simple, something like:

	#define MAX_VMA_SCAN_BATCH	<whatever :)>

	unsigned long nr_vmas_batch = 0;

	...

	for_each_vma(vmi, vma) {
		unsigned long hstart, hend;

		if (nr_vmas_batch++ >= khugepaged_pages_to_scan / 4) {
			... drop + reacquire lock ...
			nr_vmas_batch = 0;
		}

		...
	}

Maybe? Could also potentially only drop the lock if
mmap_lock_is_contended()?

--
Cheers, Lorenzo


      reply	other threads:[~2026-11-05 14:46 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03  8:27 [PATCH 0/5] mm/khugepaged: bound mmap_lock hold time when walking an mm's VMAs Lorenzo Stoakes (ARM)
2026-11-03  8:27 ` [PATCH 1/5] Revert "mm/khugepaged: remove definition of struct khugepaged_mm_slot" Lorenzo Stoakes (ARM)
2026-11-04  7:46   ` Lance Yang
2026-11-05 10:17   ` David Hildenbrand (Arm)
2026-11-03  8:27 ` [PATCH 2/5] mm/khugepaged: scan mm's more efficiently under mmap_lock contention Lorenzo Stoakes (ARM)
2026-11-04  8:43   ` Lance Yang
2026-11-05 10:23   ` David Hildenbrand (Arm)
2026-11-05 14:01     ` Lorenzo Stoakes (ARM)
2026-11-03  8:27 ` [PATCH 3/5] locking/rwsem: add down_read[_killable]_nosteal() Lorenzo Stoakes (ARM)
2026-11-03 11:51   ` Peter Zijlstra
2026-11-03 12:24     ` Lorenzo Stoakes (ARM)
2026-11-05 10:25       ` David Hildenbrand (Arm)
2026-11-05 10:29         ` Lorenzo Stoakes (ARM)
2026-11-03  8:27 ` [PATCH 4/5] mm: add and use mmap_read_lock[_killable]_nosteal() on contention Lorenzo Stoakes (ARM)
2026-11-05 10:16   ` David Hildenbrand (Arm)
2026-11-05 14:02     ` Lorenzo Stoakes (ARM)
2026-11-05 16:23     ` Alexei Starovoitov
2026-11-03  8:27 ` [PATCH 5/5] mm/khugepaged: drop/reacquire mmap read lock if skipped enough VMAs Lorenzo Stoakes (ARM)
2026-11-03  8:49   ` sashiko-bot
2026-11-05 10:28   ` David Hildenbrand (Arm)
2026-11-05 14:46     ` Lorenzo Stoakes (ARM) [this message]

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=auyMmGmrW-6WhF4p@gremlin \
    --to=ljs@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=boqun@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=kas@kernel.org \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=longman@redhat.com \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mingo@redhat.com \
    --cc=nico.pache@linux.dev \
    --cc=peterz@infradead.org \
    --cc=ryan.roberts@arm.com \
    --cc=shakeel.butt@linux.dev \
    --cc=song@kernel.org \
    --cc=surenb@google.com \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=will@kernel.org \
    --cc=yonghong.song@linux.dev \
    --cc=ziy@nvidia.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