From: Pedro Falcato <pfalcato@suse.de>
To: David Hildenbrand <david@redhat.com>
Cc: "wuyifeng (C)" <wuyifeng10@huawei.com>,
akpm@linux-foundation.org, linux-mm@kvack.org
Subject: Re: [RFC] mm: MAP_POPULATE on writable anonymous mappings marks pte dirty is necessarily?
Date: Mon, 22 Sep 2025 15:13:45 +0100 [thread overview]
Message-ID: <o5f47zn6wamjfuda67acm27aonhtmm2gbbwztgqvskkzjjn2d7@s4etlkckypuy> (raw)
In-Reply-To: <8e8b4aa4-18b7-4928-a9f0-cd47a59258fc@redhat.com>
On Mon, Sep 22, 2025 at 02:46:44PM +0200, David Hildenbrand wrote:
> On 22.09.25 11:37, Pedro Falcato wrote:
snip
> >
> > > What happens on architectures where the HW doesn't support setting the dirty
> > > bit is that performing a pte_mkwrite() checks whether the pte is dirty. If
> > > it's not dirty the HW write bit will not be set and instead the next
> > > pte_mkdirty() will set the actual HW write bit.
> > >
> > > See pte_mkwrite() handling in arch/sparc/include/asm/pgtable_64.h or
> > > arch/s390/include/asm/pgtable.h
> > >
> > > Of course, setting the dirty bit either way on later access comes with a
> > > price.
> >
> > Ah, yes, the details were a little fuzzy in my head, thanks.
> > I'm trying to swap in (ha!) the details again. We still proactively mark anon
> > folios dirty anyway for $reasons, so optimizing it might be difficult? Not sure
> > if it is _worth_ optimizing for anyway.
>
> I remembered the same thing (proactively mark anon folios dirty) but I
> didn't easily spot it in the code. Did you spot it?
>
> I only found the folio_mark_dirty() calls when unmapping anon pages and we
> stumble over a dirty pte.
>
In shrink_folio_list():
if (folio_test_anon(folio) && folio_test_swapbacked(folio)) {
if (!folio_test_swapcache(folio)) {
/* ... */
/*
* Normally the folio will be dirtied in unmap because its
* pte should be dirty. A special case is MADV_FREE page. The
* page's pte could have dirty bit cleared but the folio's
* SwapBacked flag is still set because clearing the dirty bit
* and SwapBacked flag has no lock protected. For such folio,
* unmap will not set dirty bit for it, so folio reclaim will
* not write the folio out. This can cause data corruption when
* the folio is swapped in later. Always setting the dirty flag
* for the folio solves the problem.
*/
folio_mark_dirty(folio);
}
}
So we assume the folio is dirty due to races with MADV_FREE. Seems like a
somewhat heavy handed solution, but I guess it works nicely for 99.9% of cases.
--
Pedro
next prev parent reply other threads:[~2025-09-22 14:14 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-22 6:19 wuyifeng (C)
2025-09-22 8:45 ` Pedro Falcato
2025-09-22 9:07 ` David Hildenbrand
2025-09-22 9:37 ` Pedro Falcato
2025-09-22 9:49 ` wuyifeng (C)
2025-09-22 12:46 ` David Hildenbrand
2025-09-22 14:13 ` Pedro Falcato [this message]
2025-09-22 14:44 ` David Hildenbrand
2025-09-22 9:00 ` David Hildenbrand
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=o5f47zn6wamjfuda67acm27aonhtmm2gbbwztgqvskkzjjn2d7@s4etlkckypuy \
--to=pfalcato@suse.de \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=linux-mm@kvack.org \
--cc=wuyifeng10@huawei.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