Skip to contentSkip to conversation

mm-do-not-assume-file-==-vma-vm_file-in-compat_vma_mmap_prepare.patch removed from -mm tree

merged mm-stable Andrew Morton 1 participant 1 message Sep 23, 2025

lore
Share & tools
lore

Andrew Morton

author tree notice Applied
#0 lore raw

The quilt patch titled

     Subject: mm: do not assume file == vma->vm_file in compat_vma_mmap_prepare()
has been removed from the -mm tree.  Its filename was
     mm-do-not-assume-file-==-vma-vm_file-in-compat_vma_mmap_prepare.patch

This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: Lorenzo Stoakes <[email protected]>
Subject: mm: do not assume file == vma->vm_file in compat_vma_mmap_prepare()
Date: Wed, 3 Sep 2025 18:48:42 +0100

In commit bb666b7c2707 ("mm: add mmap_prepare() compatibility layer for nested file systems") we introduced the ability for stacked drivers and file systems to correctly invoke the f_op->mmap_prepare() handler from an f_op->mmap() handler via a compatibility layer implemented in compat_vma_mmap_prepare().

This populates vm_area_desc fields according to those found in the (not yet fully initialised) VMA passed to f_op->mmap().

However this function implicitly assumes that the struct file which we are operating upon is equal to vma->vm_file. This is not a safe assumption in all cases.

The only really sane situation in which this matters would be something like e.g. i915_gem_dmabuf_mmap() which invokes vfs_mmap() against obj->base.filp:

	ret = vfs_mmap(obj->base.filp, vma);
	if (ret)
		return ret;

And then sets the VMA's file to this, should the mmap operation succeed:

	vma_set_file(vma, obj->base.filp);

That is - it is the file that is intended to back the VMA mapping.

This is not an issue currently, as so far we have only implemented f_op->mmap_prepare() handlers for some file systems and internal mm uses, and the only stacked f_op->mmap() operations that can be performed upon these are those in backing_file_mmap() and coda_file_mmap(), both of which use vma->vm_file.

However, moving forward, as we convert drivers to using f_op->mmap_prepare(), this will become a problem.

Resolve this issue by explicitly setting desc->file to the provided file parameter and update callers accordingly.

Callers are expected to read desc->file and update desc->vm_file - the former will be the file provided by the caller (if stacked, this may differ from vma->vm_file).

If the caller needs to differentiate between the two they therefore now can.

While we are here, also provide a variant of compat_vma_mmap_prepare() that operates against a pointer to any file_operations struct and does not assume that the file_operations struct we are interested in is file->f_op.

This function is __compat_vma_mmap_prepare() and we invoke it from compat_vma_mmap_prepare() so that we share code between the two functions.

This is important, because some drivers provide hooks in a separate struct, for instance struct drm_device provides an fops field for this purpose.

Also update the VMA selftests accordingly.

---

 include/linux/fs.h               |    2 
 mm/util.c                        |   62 ++++++++++++++++++-----------
 tools/testing/vma/vma_internal.h |   12 ++++-
 3 files changed, 50 insertions(+), 26 deletions(-)
include/linux/fs.h renamed from include/linux/fs.h~mm-do-not-assume-file-==-vma-vm_file-in-compat_vma_mmap_prepare +2 -0
@@ -2279,6 +2279,8 @@ static inline bool can_mmap_file(struct
 	return true;
 }
 
+int __compat_vma_mmap_prepare(const struct file_operations *f_op,
+		struct file *file, struct vm_area_struct *vma);
 int compat_vma_mmap_prepare(struct file *file, struct vm_area_struct *vma);
 
 static inline int vfs_mmap(struct file *file, struct vm_area_struct *vma)
mm/util.c renamed from mm/util.c~mm-do-not-assume-file-==-vma-vm_file-in-compat_vma_mmap_prepare +39 -23

80 lines not on this page · raw message

tools/testing/vma/vma_internal.h renamed from tools/testing/vma/vma_internal.h~mm-do-not-assume-file-==-vma-vm_file-in-compat_vma_mmap_prepare +9 -3
@@ -1466,8 +1466,8 @@ static inline void free_anon_vma_name(st
 static inline void set_vma_from_desc(struct vm_area_struct *vma,
 		struct vm_area_desc *desc);
 
-static inline int compat_vma_mmap_prepare(struct file *file,
-		struct vm_area_struct *vma)
+static inline int __compat_vma_mmap_prepare(const struct file_operations *f_op,
+		struct file *file, struct vm_area_struct *vma)
 {
 	struct vm_area_desc desc = {
 		.mm = vma->vm_mm,
@@ -1482,7 +1482,7 @@ static inline int compat_vma_mmap_prepar
 	};
 	int err;
 
-	err = file->f_op->mmap_prepare(&desc);
+	err = f_op->mmap_prepare(&desc);
 	if (err)
 		return err;
 	set_vma_from_desc(vma, &desc);
@@ -1490,6 +1490,12 @@ static inline int compat_vma_mmap_prepar
 	return 0;
 }
 
+static inline int compat_vma_mmap_prepare(struct file *file,
+		struct vm_area_struct *vma)
+{
+	return __compat_vma_mmap_prepare(file->f_op, file, vma);
+}
+
 /* Did the driver provide valid mmap hook configuration? */
 static inline bool can_mmap_file(struct file *file)
 {

_

Patches currently in -mm which might be from [email protected] are