Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795047628; b=CerIsvDzUBHmeE946uwfy8YeIE2OduFQJ7P0brEetnWLjTbSZP/NiIVoZTN/gt+w6dE5 V0gkaIWRouyq7I/QqS55Fw4NY7i1YhPxhjqAypmDYgwjBMRkpZWjNBYrGvVOZOPH7oxxY 9CgA21c76tYZ76Pk8kdHuhbRv6qhRhweuSlbakgm8YefyLGyyu4sZ8kaFXhFFeOG/Y3TC mxy0l5k7uiUywZ4fASlxUbDDautABd45+rleJDbZDPsszjludT6KgSI525KSkCfspuEt0 z9usNDEG5VDeSm3tm315lsO06pe8z5VU0smN99Xx9nKT/WcUeUHu7fH9ReUc2lPz0Gw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795047628; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=TbP/LeeQAUHkeJWvbIZDXR/lU/TWdiT1l+2CjNDX/vE=; b=FSUqp1V3msDtnWJ86w+c1QAhCm2rKvINl3P2bTr4t+Ll+/bGsuxh2/88PeZbOl9FUV5D VoQGbEVaxretK4UJAb9JGhXj5LOk3xSJPQULY+Q5sMfh5F0maBXjlElAMP3MVMyByi3SA DowTYbYONjcKeTQv35ysHxRJ6nh/Z4+GOBAq0XaAGx1H4dyhnFc4LXxxa+J13pwtyOpZU loU/WFsjuP7Lq6U3KIc+lxEk5zOvY/z/tGPFnR1jtxoIHlLJOSPEU/JxlZZdbPwGjSagN UxfrB3LAtAmxEONwMc0xVqr0+8CWF+WlMUb1NKglVv5giYHCAcOQnD4ioWvulV6cosw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=mRIvPj9v; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60080606A31C; Thu, 19 Nov 2026 00:20:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=mRIvPj9v Received: by smtp.kernel.org (Postfix) with ESMTPSA id B10871F000C4; Thu, 19 Nov 2026 00:20:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795047628; bh=TbP/LeeQAUHkeJWvbIZDXR/lU/TWdiT1l+2CjNDX/vE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=mRIvPj9vL6LqsQIKb1x5JhRqz09+qgnnZcrQd9s8eu+WHyR0lk33MXI9N6o1nB1ky H4bOLHJW2W04VxD9fLewfJ+cUmf0nnEUbN8us2CVxxPrm2fqTUv786pUJdDaYHtyUr SQMNXTFSwocvu/8BoVQIox6BuEfYa6Fv+Z8B2Dzg= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Chris Mason , Pratyush Yadav , Baolin Wang , "David Hildenbrand (Arm)" , Sasha Levin Subject: [PATCH 6.1 1706/2857] mm/shmem: make shmem_file() check the files inode Date: Wed, 18 Nov 2026 15:45:40 +0100 Message-ID: <20261118150302.758499792@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150201.293410965@linuxfoundation.org> References: <20261118150201.293410965@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Hildenbrand (Arm) [ Upstream commit d3710111839d11339e414b1bb3fea3d05ab83f6e ] 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 Suggested-by: Pratyush Yadav Reviewed-by: Pratyush Yadav (Google) Link: https://patch.msgid.link/20261031-fixes_core-v3-3-d6437171ecc3@kernel.org Reviewed-by: Baolin Wang Signed-off-by: David Hildenbrand (Arm) [ sashal: adjusted surrounding context to preserve the existing inline shmem_mapping() implementation ] Signed-off-by: Sasha Levin --- 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 d500ea967dc73..19a1965ff1175 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -75,12 +75,18 @@ 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); extern const struct address_space_operations shmem_aops; static inline bool shmem_mapping(struct address_space *mapping) { return mapping->a_ops == &shmem_aops; } #else +static inline bool shmem_file(struct file *file) +{ + return false; +} + static inline bool shmem_mapping(struct address_space *mapping) { return false; @@ -123,15 +129,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); -} - /* * If fallocate(FALLOC_FL_KEEP_SIZE) has been used, there may be pages * beyond i_size's notion of EOF, which fallocate has committed to reserving: diff --git a/mm/khugepaged.c b/mm/khugepaged.c index f21b78228cb40..c9ee735a2b1d9 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1774,12 +1774,12 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, struct collapse_control *cc) { struct address_space *mapping = file->f_mapping; + const bool is_shmem = shmem_mapping(mapping); struct page *hpage; pgoff_t index, end = start + HPAGE_PMD_NR; LIST_HEAD(pagelist); XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER); int nr_none = 0, result = SCAN_SUCCEED; - bool is_shmem = shmem_file(file); int nr; VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem); diff --git a/mm/shmem.c b/mm/shmem.c index 9db6a2488f456..1ed9c1e9abb99 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -239,6 +239,11 @@ static const struct inode_operations shmem_special_inode_operations; static const struct vm_operations_struct shmem_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 vma_is_shmem(struct vm_area_struct *vma) { return vma->vm_ops == &shmem_vm_ops; -- 2.53.0