Re: [PATCH] mm/khugepaged: reject shmem collapse ranges beyond i_size

From: Jérémy Jean

Date: Sun Nov 15 2026 - 15:22:49 EST


On 2026-11-13 03:42, Baolin Wang wrote:
On 11/13/26 6:50 AM, Andrew Morton wrote:
On Thu, 12 Nov 2026 18:11:35 +0000 Pedro Falcato <pfalcato@xxxxxxx> wrote:

On Thu, Nov 12, 2026 at 02:23:35PM +0000, Jérémy Jean wrote:
MADV_COLLAPSE lets collapse_file() treat absent shmem pages as holes
filled with zeros. It does not check i_size, which allows one present
page to become a huge mapping that covers the whole collapse range,
including pages beyond EOF. This makes those pages writable. A later
file extension can expose their contents after F_SEAL_WRITE.

Hold inode i_rwsem in read mode while collapsing shmem so i_size
cannot change. Reject a collapse if its page range extends past
i_size rounded up to PAGE_SIZE. Keep i_size_read() outside the
page-cache XArray lock because its 32-bit SMP reader is not IRQ
safe.

I don't think this is an issue. shmem/tmpfs purposefully maps with large pages
across i_size, and collapsing across i_size is a feature, not a bug.

Yes. finish_fault() even has comments explaining the special case for shmem/tmpfs.

/*
* Do not allow to map with PTEs beyond i_size and with PMD
* across i_size to preserve SIGBUS semantics.
*
* Make an exception for shmem/tmpfs that for long time
* intentionally mapped with PMDs across i_size.
*/
needs_fallback = !shmem_mapping(mapping) &&
file_end < folio_next_index(folio);

hoo boy, this again, See
https://lore-kernel.gnuweeb.org/linux-mm/nwkpojnictbg6jicccpxyfi63ol4tmfajrlx24u5xxoxx7lv5f@jkw3o7j6tlfs/T/
and enjoy.

It's not good that this sequence permits non-zero data to be exposed in
areas which ftruncate() created - the ftruncate() contract says it
doesn't do that.

That's a valid concern and this is a known issue for shmem/tmpfs. Brian previously posted a patchset to fix it:
https://lore.kernel.org/all/20251112162522.412295-1-bfoster@xxxxxxxxxx/

Hello,

Thank you all for the quick feedbacks. I unfortunately did not find
and read the links you shared nor the historical discussions before I
prepared the patch. I'm sorry for reheating what it seems to me like
a sensitive subject; it was definitely not my intention.

Jérémy , you haven't explained why this matters. Is there some
userspace code which is encountering problems because of this?
Why did you propose cc:stable backporting?

My initial understanding was that there was a security boundary
crossed, and that's why I submitted the patch and added stable in the
exchange, so that it could get backported. While I'm now a bit
confused about the situation, I have no problem in accepting that I
was wrong and I'm totally fine with just dropping the patch if there
was already a discussion and suggest patches.

Regards,
Jérémy