From: Andrew Morton <akpm@linux-foundation.org>
To: Jan Kara <jack@suse.cz>
Cc: Ayush Ranjan <ayushr@modal.com>, Pedro Falcato <pfalcato@suse.de>,
Hugh Dickins <hughd@google.com>,
Matthew Wilcox <willy@infradead.org>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
David Hildenbrand <david@kernel.org>,
Gregory Price <gourry@gourry.net>,
linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [BUG] shmem: FALLOC_FL_PUNCH_HOLE vs fault-around race corrupts page cache / rss counters
Date: Thu, 8 Oct 2026 08:23:04 -0700 [thread overview]
Message-ID: <20261008082304.bc6f4bd6c80fe6432abd4fc5@linux-foundation.org> (raw)
In-Reply-To: <fyswoh7wpxzlbnjjanggjd2jz6yispmeirhwjl3wzi57nacddb@bjkb6kxyfcal>
On Thu, 8 Oct 2026 11:24:43 +0200 Jan Kara <jack@suse.cz> wrote:
> > From: Andrew Morton <akpm@linux-foundation.org>
> > Subject: mm: shmem: serialize fault-around against hole punching
> > Date: Sun Oct 4 09:05:31 PM PDT 2026
> >
> > shmem uses filemap_map_pages() for fault-around. Unlike shmem_fault(),
> > filemap_map_pages() does not participate in shmem's fallocate exclusion
> > protocol.
> >
> > During a partial hole punch of a large shmem folio, the folio can be split
> > and the resulting smaller folios can remain temporarily visible in the page
> > cache while shmem_undo_range() restarts its walk. Fault-around can then map
> > one of those folios again before the hole-punch path removes it.
> >
> > shmem_undo_range() may subsequently delete that folio from the page cache
> > despite the new userspace mapping. This can trigger "still mapped when
> > deleted" warnings and leave stale mappings or inconsistent RSS/page-table
> > accounting behind.
>
> So this part of explanation is either incomplete or wrong in my opinion.
> Yes, shmem_undo_range() calls truncate_inode_partial_folio() which can
> split a large folio. Yes, filemap_map_pages() can map those pages back into
> page tables. But how "shmem_undo_range() may subsequently delete that folio
> from the page cache despite the new userspace mapping" happens is unclear
> to me. After splitting a folio, shmem_undo_range() will restart and find the
> newly split (and mapped) folios and calls truncate_inode_folio() to get rid
> of them. Now truncate_inode_folio() calls truncate_cleanup_folio() which
> does:
>
> if (folio_mapped(folio))
> unmap_mapping_folio(folio);
>
> so the mapping is reliably removed under folio lock. Can you perhaps push
> your agents further to explain in more detail how this "still mapped when
> deleted" happens in their opinion?
back to the drawing board...
You're right. I pushed this further and I no longer think the explanation
in the changelog is sufficient.
filemap_map_pages() takes the folio lock, and shmem_undo_range() also locks
the folio before truncating it. truncate_inode_folio() then calls
truncate_cleanup_folio() while that lock is held, and
truncate_cleanup_folio() calls unmap_mapping_folio() before
filemap_remove_folio().
So if fault-around maps the folio first, truncation should subsequently
unmap it. If truncation gets the folio lock first, fault-around cannot map
it before it is removed from the page cache. I don't see a direct
fault-around-only race which leaves the folio mapped at
filemap_remove_folio().
The earlier fork/copy_page_range() theory did have a way around the folio
lock, since copy_page_range() does not take it. But Ayush's single-process
reproducer rules that explanation out.
The strongest remaining clue is that the reproducer requires repeated
partial splitting and aggressive khugepaged re-collapse, and the production
splat was on an order-3 folio with nonzero mapcount. That makes a
split/re-collapse/rmap or mapcount problem look more likely than the simple
fault-around race I described.
So I don't think this patch is ready as-is. It might suppress the problem by
adding broader serialization, but that would not demonstrate that it fixes
the underlying bug.
A useful next step would be to instrument truncate_cleanup_folio(), e.g. warn
immediately after unmap_mapping_folio() if folio_mapped() is still true.
That should distinguish "unmap failed to remove an existing mapping" from
"some path installed a new mapping afterwards", and narrow this down
considerably.
next prev parent reply other threads:[~2026-10-08 15:23 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-24 6:16 [BUG] shmem: FALLOC_FL_PUNCH_HOLE vs fault-around race corrupts page cache / rss counters Ayush Ranjan
2026-09-24 7:12 ` David Hildenbrand (Arm)
2026-09-24 8:34 ` Pedro Falcato
2026-09-24 9:15 ` Jan Kara
2026-09-25 5:32 ` Ayush Ranjan
2026-09-24 9:30 ` Baolin Wang
2026-09-25 5:33 ` Ayush Ranjan
2026-09-25 5:30 ` Ayush Ranjan
2026-09-25 6:50 ` Ayush Ranjan
2026-10-03 3:31 ` Ayush Ranjan
2026-10-05 5:28 ` Andrew Morton
2026-10-08 9:24 ` Jan Kara
2026-10-08 15:23 ` Andrew Morton [this message]
2026-10-08 15:31 ` Andrew Morton
2026-10-08 13:07 ` Gregory Price
2026-10-08 7:58 ` Baolin Wang
2026-10-08 9:26 ` Jan Kara
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=20261008082304.bc6f4bd6c80fe6432abd4fc5@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=ayushr@modal.com \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=gourry@gourry.net \
--cc=hughd@google.com \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=pfalcato@suse.de \
--cc=willy@infradead.org \
/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