From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Pratyush Yadav <pratyush@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>, Qi Zheng <qi.zheng@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
Lorenzo Stoakes <ljs@kernel.org>,
Kairui Song <kasong@tencent.com>, Barry Song <baohua@kernel.org>,
Axel Rasmussen <axelrasmussen@google.com>,
Yuanchu Xie <yuanchu@google.com>, Wei Xu <weixugc@google.com>,
Baoquan He <baoquan.he@linux.dev>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>, Kees Cook <kees@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Jann Horn <jannh@google.com>, Pedro Falcato <pfalcato@suse.de>,
Hugh Dickins <hughd@google.com>,
Pasha Tatashin <pasha.tatashin@soleen.com>,
"Matthew Wilcox (Oracle)" <willy@infradead.org>,
Jan Kara <jack@suse.cz>, Zi Yan <ziy@nvidia.com>,
Lance Yang <lance.yang@linux.dev>,
Rik van Riel <riel@surriel.com>, Harry Yoo <harry@kernel.org>,
Alistair Popple <apopple@nvidia.com>,
Nico Pache <nico.pache@linux.dev>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Usama Arif <usama.arif@linux.dev>,
Kiryl Shutsemau <kas@kernel.org>,
Matthew Brost <matthew.brost@intel.com>,
John Hubbard <jhubbard@nvidia.com>, Peter Xu <peterx@redhat.com>,
Will Deacon <will@kernel.org>,
"Aneesh Kumar K.V" <aneesh.kumar@kernel.org>,
Nick Piggin <npiggin@gmail.com>,
Oscar Salvador <osalvador@suse.de>,
Minchan Kim <minchan@kernel.org>,
Muchun Song <muchun.song@linux.dev>, Xu Xin <xu.xin@linux.dev>,
Chengming Zhou <chengming.zhou@linux.dev>,
Chris Li <chrisl@kernel.org>,
Kemeng Shi <shikemeng@huaweicloud.com>,
Nhat Pham <nphamcs@gmail.com>,
Youngjun Park <youngjun.park@lge.com>,
Gregory Price <gourry@gourry.net>,
Joshua Hahn <joshua.hahnjy@gmail.com>,
Rakie Kim <rakie.kim@sk.com>,
Ying Huang <ying.huang@linux.alibaba.com>,
Jason Gunthorpe <jgg@ziepe.ca>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
kexec@lists.infradead.org, linux-fsdevel@vger.kernel.org,
linux-arch@vger.kernel.org, linux-cxl@vger.kernel.org,
stable@vger.kernel.org, Chris Mason <mason@kernel.org>
Subject: Re: [PATCH v2 03/47] mm/memfd: check for real shmem inodes before accessing shmem state
Date: Fri, 30 Oct 2026 13:06:45 +0100 [thread overview]
Message-ID: <297adf4c-1792-4dff-a7c8-2694dc2b1446@kernel.org> (raw)
In-Reply-To: <2vxzmrrvmnil.fsf@kernel.org>
On 10/30/26 12:33, Pratyush Yadav wrote:
> Hi David,
>
> On Thu, Oct 29 2026, David Hildenbrand (Arm) wrote:
>
>> shmem_file() only checks a file's backing mapping. That mapping can belong
>> to another file, as with a Coda file backed by a tmpfs container after
>> mmap(). It does not establish that file_inode(file) is a shmem inode.
>>
>> Seal handling and memfd preservation use this check before accessing
>> shmem inode state, so they can access an inode of the wrong type.
>>
>> Add file_inode_is_shmem() to check the inode's file operations in both
>> paths. This identifies regular shmem inodes, including those used by
>> memfds and ordinary tmpfs files, independently of wrapper file operations.
>>
>> Fixes: c49fcfcda8b5 ("mm/shmem: update file sealing comments and file checking")
>> Fixes: b3749f174d68 ("mm: memfd_luo: allow preserving memfd")
>> Cc: stable@vger.kernel.org
>> Reported-by: Chris Mason <mason@kernel.org>
>> Reviewed-by: Rik van Riel <riel@surriel.com>
>> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
>> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
>
> Maybe you missed my email on v1 [0], so here's another try:
I did miss it! Thanks for pointing that out again.
>
> Why not update shmem_file() to do this check instead? Having a
> shmem_file() that might not always return the right result on a file
> seems like a landmine.
The current fix focuses on the sealing check indeed.
>
> For example, you missed check_memfd_seals() in udmabuf or
check_memfd_seals() calls memfd_fcntl() where memfd_get_seals() would now properly reject it.
> memfd_pin_folios() in gup.
memfd_pin_folios() with sealing is a head-scratcher in general. I don't think it would be a problem here, though.
The bigger question is if that path should just reject coda files as you point out below. I think yes.
> Both want to only look at "proper" shmem
> files, and should not do any work on a Coda file with a shmem mapping.
> Plus, someone later down the line might use shmem_file() without knowing
> this subtle problem, and we probably would miss it in review.
>
> For callers that only care about the mapping should then use
> shmem_mapping(). I am not sure if there are any. Perhaps that would be
> collapse_file()? But TBH I don't really understand khugepaged all that
> much. All other callers of shmem_file() seem to care about the actual
> inode.
Looks like collapse_file() would then use shmem_mapping() for the time being. God this is confusing.
What about the following replacement:
From 6e3c4f92a651e36ed1ae8d7659d2ca08543fa430 Mon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <david@kernel.org>
Date: Sat, 24 Oct 2026 21:08:33 +0200
Subject: [PATCH] mm/shmem: make shmem_file() check the file's inode
shmem_file() only checks if file->f_mapping is a shmem mapping. This
mapping can belong to another file, however, as with a Coda file backed
by a tmpfs container after mmap().
So it does not guarantee that the file inode is an actual shmem inode.
Seal handling and memfd preservation use this check before accessing
shmem inode state, so they can access an inode of the wrong type.
Let's make shmem_file() check the inode's file operations instead, to
identify shmem inodes reliably. This will identify regular shmem inodes,
including memfds and ordinary tmpfs files, independently of wrapper file
operations.
Use shmem_mapping() in collapse_file() instead to not change the behavior
of collapse_file(): it operates on the page cache and uses mapping->host
for shmem inode operations. Focusing on the file does not seem relevant.
Fixes: c49fcfcda8b5 ("mm/shmem: update file sealing comments and file checking")
Fixes: b3749f174d68 ("mm: memfd_luo: allow preserving memfd")
Cc: stable@vger.kernel.org
Reported-by: Chris Mason <mason@kernel.org>
Suggested-by: Pratyush Yadav <pratyush@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
include/linux/shmem_fs.h | 15 ++++++---------
mm/khugepaged.c | 2 +-
mm/shmem.c | 5 +++++
3 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h
index a7c7a96a7cbf..f89b4b151133 100644
--- a/include/linux/shmem_fs.h
+++ b/include/linux/shmem_fs.h
@@ -117,8 +117,14 @@ extern unsigned long shmem_get_unmapped_area(struct file *, unsigned long addr,
unsigned long len, unsigned long pgoff, unsigned long flags);
extern int shmem_lock(struct file *file, int lock, struct ucounts *ucounts);
#ifdef CONFIG_SHMEM
+bool shmem_file(struct file *file);
bool shmem_mapping(const struct address_space *mapping);
#else
+static inline bool shmem_file(struct file *file)
+{
+ return false;
+}
+
static inline bool shmem_mapping(const struct address_space *mapping)
{
return false;
@@ -193,15 +199,6 @@ static inline struct page *shmem_read_mapping_page(
mapping_gfp_mask(mapping));
}
-static inline bool shmem_file(struct file *file)
-{
- if (!IS_ENABLED(CONFIG_SHMEM))
- return false;
- if (!file || !file->f_mapping)
- return false;
- return shmem_mapping(file->f_mapping);
-}
-
/* Must be called with inode lock taken exclusive. */
static inline void shmem_freeze(struct inode *inode, bool freeze)
{
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 4b71d010b2af..3a6f4b7b2845 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -2230,6 +2230,7 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
struct file *file, pgoff_t start, struct collapse_control *cc)
{
struct address_space *mapping = file->f_mapping;
+ const bool is_shmem = shmem_mapping(mapping);
struct page *dst;
struct folio *folio, *tmp, *new_folio;
unsigned long new_pfn = -1;
@@ -2238,7 +2239,6 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
enum scan_result result = SCAN_SUCCEED;
int nr_none = 0;
- bool is_shmem = shmem_file(file);
/*
* MADV_COLLAPSE ignores shmem huge config, so do not check shmem
diff --git a/mm/shmem.c b/mm/shmem.c
index af21a89abb0f..421ba066995c 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -275,6 +275,11 @@ static const struct vm_operations_struct shmem_vm_ops;
static const struct vm_operations_struct shmem_anon_vm_ops;
static struct file_system_type shmem_fs_type;
+bool shmem_file(struct file *file)
+{
+ return file && file_inode(file)->i_fop == &shmem_file_operations;
+}
+
bool shmem_mapping(const struct address_space *mapping)
{
return mapping->a_ops == &shmem_aops;
--
2.43.0
--
Cheers,
David
next prev parent reply other threads:[~2026-10-30 12:07 UTC|newest]
Thread overview: 69+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-29 20:32 [PATCH v2 00/47] mm: various (urgent?) fixes David Hildenbrand (Arm)
2026-10-29 20:32 ` [PATCH v2 01/47] mm/mglru: don't use stack pointers when walking PUD and PMD tables David Hildenbrand (Arm)
2026-10-30 0:20 ` SJ Park
2026-10-30 10:21 ` David Hildenbrand (Arm)
2026-10-29 20:32 ` [PATCH v2 02/47] fork: take mmap_lock before copying mm_struct David Hildenbrand (Arm)
2026-10-30 0:34 ` SJ Park
2026-10-29 20:32 ` [PATCH v2 03/47] mm/memfd: check for real shmem inodes before accessing shmem state David Hildenbrand (Arm)
2026-10-30 11:33 ` Pratyush Yadav
2026-10-30 12:06 ` David Hildenbrand (Arm) [this message]
2026-10-30 12:29 ` Pratyush Yadav
2026-10-30 14:35 ` David Hildenbrand (Arm)
2026-10-29 20:32 ` [PATCH v2 04/47] mm/memfd: make f_pos accesses atomic David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 05/47] mm/truncate: look again if a folio left the mapping David Hildenbrand (Arm)
2026-10-30 17:13 ` Kiryl Shutsemau
2026-10-30 17:15 ` Zi Yan
2026-10-29 20:33 ` [PATCH v2 06/47] mm/filemap: read mapping->a_ops->free_folio under i_lock David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 07/47] mm/filemap: don't lose a folio lock wakeup to a signal David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 08/47] mm/filemap: finish fault cleanup before dropping the fault lock David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 09/47] mm/rmap: handle device-exclusive entries in folio_referenced_one() David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 10/47] mm/huge_memory: fix list corruption when deferred_split_scan() races with deferred_split_folio() David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 11/47] mm/huge_memory: properly handle refcounted PUD mappings on fork David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 12/47] mm/huge_memory: fix livelock on device private folio David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 13/47] mm/huge_memory: fix truncate racing with PMD zapping David Hildenbrand (Arm)
2026-10-29 22:37 ` Gregory Price (Meta)
2026-10-29 20:33 ` [PATCH v2 14/47] mm/huge_memory: fix zapping migration entries for device-private THPs David Hildenbrand (Arm)
2026-10-29 22:30 ` Gregory Price (Meta)
2026-10-30 11:10 ` David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 15/47] mm/khugepaged: always recheck the pmd in try_collapse_pte_mapped_thp() David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 16/47] mm/khugepaged: fix PMD table lock leak " David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 17/47] mm/khugepaged: preserve dirty bits when collapsing PTE-mapped THPs David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 18/47] mm/khugepaged: check if PTE table is empty in try_collapse_pte_mapped_thp() David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 19/47] mm/gup: recheck the PUD under PTL in follow_pud_mask() David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 20/47] mm/gup: fix clearing folios in memfd_pin_folios() David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 21/47] mm/gup: fix out-of-bounds write for nr_pages == 0 in GUP-fast David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 22/47] mm/gup: check for overflow when converting nr_pages to bytes " David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 23/47] mm/userfaultfd: write-lock the VMA before waking waiters when unregistering David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 24/47] mm/userfaultfd: wake all waiters on unregister David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 25/47] mm/userfaultfd: wake waiters when registration modes are removed David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 26/47] mm/userfaultfd: compare exact registration modes before skipping David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 27/47] mm/mmu_gather: fix flushing ranges with 4 GiB or larger David Hildenbrand (Arm)
2026-10-30 0:57 ` SJ Park
2026-10-30 11:15 ` David Hildenbrand (Arm)
2026-10-30 11:19 ` David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 28/47] mm/mmu_notifier: finish non-blocking interval invalidation on error David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 29/47] mm/mmu_notifier: set ->mm only after mmgrab() David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 30/47] mm/mremap: use pudp_huge_get_and_clear() when moving huge PUDs David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 31/47] mm/mremap: recheck PUDs under the page table lock David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 32/47] mm/mremap: fix moving PUD leaves David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 33/47] mm/memory_hotplug: avoid races in pfn_to_online_page() with dying pgmap David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 34/47] mm/madvise: fix data loss from MADV_FREE on a KSM page David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 35/47] mm/memory: reject remote COW faults on PFN mappings David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 36/47] mm: fix unmap_mapping_range() beyond 4 GiB on 32-bit David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 37/47] mm: fix unkillable fault loop on MAP_DROPPABLE memory David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 38/47] mm: fix deadlock of mremap() against UFFDIO_MOVE David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 39/47] mm/ksm: reset the unstable trees of all nodes David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 40/47] mm/ksm: fix use of a freed anon_vma at swap-in David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 41/47] mm/ksm: check large swap-cache folios before mapping them David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 42/47] mm/vma: call ->may_split() before a partial merge David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 43/47] mm/huge_memory: split huge PUDs when adjusting VMA boundaries David Hildenbrand (Arm)
2026-10-30 17:14 ` Kiryl Shutsemau
2026-10-29 20:33 ` [PATCH v2 44/47] mm/vma: don't leak the charge for stack growth David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 45/47] mm/cma: don't release CMA pages still in use David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 46/47] mm/memory-tiers: unregister empty tiers outside memory_tier_lock David Hildenbrand (Arm)
2026-10-29 20:33 ` [PATCH v2 47/47] fs/proc/task_mmu: refuse addresses beyond 32 bits on 32bit David Hildenbrand (Arm)
2026-10-30 11:19 ` [PATCH v2 00/47] mm: various (urgent?) fixes Muhammad Usama Anjum
2026-10-30 11:35 ` David Hildenbrand (Arm)
2026-10-30 12:12 ` Muhammad Usama Anjum
2026-10-30 13:10 ` Chris Mason
2026-10-30 14:37 ` David Hildenbrand (Arm)
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=297adf4c-1792-4dff-a7c8-2694dc2b1446@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=aneesh.kumar@kernel.org \
--cc=apopple@nvidia.com \
--cc=axelrasmussen@google.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=baoquan.he@linux.dev \
--cc=chengming.zhou@linux.dev \
--cc=chrisl@kernel.org \
--cc=dev.jain@arm.com \
--cc=gourry@gourry.net \
--cc=hannes@cmpxchg.org \
--cc=harry@kernel.org \
--cc=hughd@google.com \
--cc=jack@suse.cz \
--cc=jannh@google.com \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=joshua.hahnjy@gmail.com \
--cc=kas@kernel.org \
--cc=kasong@tencent.com \
--cc=kees@kernel.org \
--cc=kexec@lists.infradead.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-arch@vger.kernel.org \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mason@kernel.org \
--cc=matthew.brost@intel.com \
--cc=mhocko@kernel.org \
--cc=mhocko@suse.com \
--cc=minchan@kernel.org \
--cc=muchun.song@linux.dev \
--cc=nico.pache@linux.dev \
--cc=nphamcs@gmail.com \
--cc=npiggin@gmail.com \
--cc=osalvador@suse.de \
--cc=pasha.tatashin@soleen.com \
--cc=peterx@redhat.com \
--cc=peterz@infradead.org \
--cc=pfalcato@suse.de \
--cc=pratyush@kernel.org \
--cc=qi.zheng@linux.dev \
--cc=rakie.kim@sk.com \
--cc=riel@surriel.com \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=shakeel.butt@linux.dev \
--cc=shikemeng@huaweicloud.com \
--cc=stable@vger.kernel.org \
--cc=surenb@google.com \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=weixugc@google.com \
--cc=will@kernel.org \
--cc=willy@infradead.org \
--cc=xu.xin@linux.dev \
--cc=ying.huang@linux.alibaba.com \
--cc=youngjun.park@lge.com \
--cc=yuanchu@google.com \
--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