From: Dmitry Ilvokhin <d@ilvokhin.com>
To: Usama Arif <usamaarif642@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Kemeng Shi <shikemeng@huaweicloud.com>,
Kairui Song <kasong@tencent.com>, Nhat Pham <nphamcs@gmail.com>,
Baoquan He <bhe@redhat.com>, Barry Song <baohua@kernel.org>,
Chris Li <chrisl@kernel.org>,
Axel Rasmussen <axelrasmussen@google.com>,
Yuanchu Xie <yuanchu@google.com>, Wei Xu <weixugc@google.com>,
Kiryl Shutsemau <kas@kernel.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
kernel-team@meta.com
Subject: Re: [PATCH] mm: skip folio_activate() for mlocked folios
Date: Mon, 6 Oct 2025 13:05:05 +0000 [thread overview]
Message-ID: <aOO-gUHQmF6zyLwM@shell.ilvokhin.com> (raw)
In-Reply-To: <550dd78f-bf85-41d6-9c3b-472784b2d94e@gmail.com>
On Fri, Oct 03, 2025 at 03:36:08PM +0100, Usama Arif wrote:
>
>
> On 03/10/2025 15:19, Dmitry Ilvokhin wrote:
> > __mlock_folio() should update stats, when lruvec_add_folio() is called,
> > but if folio_test_clear_lru() check failed, then __mlock_folio() gives
>
> nit: s/failed/fails/
I'll cut commit message in v2, so phrase will not be there anymore.
> > up early. From the other hand, folio_mark_accessed() calls
>
> nit: s/From/On/
This one as well.
> > folio_activate() which also calls folio_test_clear_lru() down the line.
> > When folio_activate() successfully removed folio from LRU,
> > __mlock_folio() will not update any stats, which will lead to inaccurate
> > values in /proc/meminfo as well as cgroup memory.stat.
> >
> > To prevent this case from happening also check for folio_test_mlocked()
> > in folio_mark_accessed(). If folio is not yet marked as unevictable, but
> > already marked as mlocked, then skip folio_activate() call to allow
> > __mlock_folio() to make all necessary updates.
>
> Would it make sense to write over here that its safe to skip activating
> an mlocked folio?
Good point, will mention that, because that's my understanding as well.
I think mlocked folio should end up in the unevictable LRU eventually
and if so, mlocked folio being in active LRU is temporary anyway, so it
is should be safe to skip folio_activate() for mlocked folios.
> >
> > To observe the problem mmap() and mlock() big file and check Unevictable
> > and Mlocked values from /proc/meminfo. On freshly booted system without
> > any other mlocked memory we expect them to match or be quite close.
> >
> > See below for more detailed reproduction steps. Source code of stat.c
> > is available at [1].
> >
> > $ head -c 8G < /dev/urandom > /tmp/random.bin
> >
> > $ cc -pedantic -Wall -std=c99 stat.c -O3 -o /tmp/stat
> > $ /tmp/stat
> > Unevictable: 8389668 kB
> > Mlocked: 8389700 kB
> >
> > Need to run binary twice. Problem does not reproduce on the first run,
> > but always reproduces on the second run.
> >
> > $ /tmp/stat
> > Unevictable: 5374676 kB
> > Mlocked: 8389332 kB
> >
> > [1]: https://gist.github.com/ilvokhin/e50c3d2ff5d9f70dcbb378c6695386dd
> >
> > Co-developed-by: Kiryl Shutsemau <kas@kernel.org>
> > Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
> > Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
> > ---
>
>
> Thanks for the patch!
>
> Personally I would just use the comment you have written below to create the commit message.
> You probably dont really need to write all the function calls paths?
>
I'll cut commit message a bit in v2 and make it more succinct, thanks
for the feedback.
> Also, I don't think you need () for all the functions in the commit message, although
> thats my personal preference.
>
> Apart from changes in the commit message, lgtm.
>
> Feel free to add after commit message fixups.
>
> Acked-by: Usama Arif <usamaarif642@gmail.com>
>
Thanks for the review.
>
> > mm/swap.c | 10 ++++++++++
> > 1 file changed, 10 insertions(+)
> >
> > diff --git a/mm/swap.c b/mm/swap.c
> > index 2260dcd2775e..f682f070160b 100644
> > --- a/mm/swap.c
> > +++ b/mm/swap.c
> > @@ -469,6 +469,16 @@ void folio_mark_accessed(struct folio *folio)
> > * this list is never rotated or maintained, so marking an
> > * unevictable page accessed has no effect.
> > */
> > + } else if (folio_test_mlocked(folio)) {
> > + /*
> > + * Pages that are mlocked, but not yet on unevictable LRU.
> > + * They might be still in mlock_fbatch waiting to be processed
> > + * and activating it here might interfere with
> > + * mlock_folio_batch(). __mlock_folio() will fail
> > + * folio_test_clear_lru() check and give up. It happens because
> > + * __folio_batch_add_and_move() clears LRU flag, when adding
> > + * folio to activate batch.
> > + */
> > } else if (!folio_test_active(folio)) {
> > /*
> > * If the folio is on the LRU, queue it for activation via
>
next prev parent reply other threads:[~2025-10-06 13:05 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-03 14:19 Dmitry Ilvokhin
2025-10-03 14:36 ` Usama Arif
2025-10-06 13:05 ` Dmitry Ilvokhin [this message]
2025-10-03 14:41 ` Kiryl Shutsemau
2025-10-06 12:07 ` Dmitry Ilvokhin
2025-10-06 13:13 ` Dmitry Ilvokhin
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=aOO-gUHQmF6zyLwM@shell.ilvokhin.com \
--to=d@ilvokhin.com \
--cc=akpm@linux-foundation.org \
--cc=axelrasmussen@google.com \
--cc=baohua@kernel.org \
--cc=bhe@redhat.com \
--cc=chrisl@kernel.org \
--cc=kas@kernel.org \
--cc=kasong@tencent.com \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=shikemeng@huaweicloud.com \
--cc=usamaarif642@gmail.com \
--cc=weixugc@google.com \
--cc=yuanchu@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