Commit 7031769e authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge tag 'vfs-6.17-rc1.mmap_prepare' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs

Pull mmap_prepare updates from Christian Brauner:
 "Last cycle we introduce f_op->mmap_prepare() in c84bf6dd ("mm:
  introduce new .mmap_prepare() file callback").

  This is preferred to the existing f_op->mmap() hook as it does require
  a VMA to be established yet, thus allowing the mmap logic to invoke
  this hook far, far earlier, prior to inserting a VMA into the virtual
  address space, or performing any other heavy handed operations.

  This allows for much simpler unwinding on error, and for there to be a
  single attempt at merging a VMA rather than having to possibly
  reattempt a merge based on potentially altered VMA state.

  Far more importantly, it prevents inappropriate manipulation of
  incompletely initialised VMA state, which is something that has been
  the cause of bugs and complexity in the past.

  The intent is to gradually deprecate f_op->mmap, and in that vein this
  series coverts the majority of file systems to using f_op->mmap_prepare.

  Prerequisite steps are taken - firstly ensuring all checks for mmap
  capabilities use the file_has_valid_mmap_hooks() helper rather than
  directly checking for f_op->mmap (which is now not a valid check) and
  secondly updating daxdev_mapping_supported() to not require a VMA
  parameter to allow ext4 and xfs to be converted.

  Commit bb666b7c ("mm: add mmap_prepare() compatibility layer for
  nested file systems") handles the nasty edge-case of nested file
  systems like overlayfs, which introduces a compatibility shim to allow
  f_op->mmap_prepare() to be invoked from an f_op->mmap() callback.

  This allows for nested filesystems to continue to function correctly
  with all file systems regardless of which callback is used. Once we
  finally convert all file systems, this shim can be removed.

  As a result, ecryptfs, fuse, and overlayfs remain unaltered so they
  can nest all other file systems.

  We additionally do not update resctl - as this requires an update to
  remap_pfn_range() (or an alternative to it) which we defer to a later
  series, equally we do not update cramfs which needs a mixed mapping
  insertion with the same issue, nor do we update procfs, hugetlbfs,
  syfs or kernfs all of which require VMAs for internal state and hooks.
  We shall return to all of these later"

* tag 'vfs-6.17-rc1.mmap_prepare' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs:
  doc: update porting, vfs documentation to describe mmap_prepare()
  fs: replace mmap hook with .mmap_prepare for simple mappings
  fs: convert most other generic_file_*mmap() users to .mmap_prepare()
  fs: convert simple use of generic_file_*_mmap() to .mmap_prepare()
  mm/filemap: introduce generic_file_*_mmap_prepare() helpers
  fs/xfs: transition from deprecated .mmap hook to .mmap_prepare
  fs/ext4: transition from deprecated .mmap hook to .mmap_prepare
  fs/dax: make it possible to check dev dax support without a VMA
  fs: consistently use can_mmap_file() helper
  mm/nommu: use file_has_valid_mmap_hooks() helper
  mm: rename call_mmap/mmap_prepare to vfs_mmap/mmap_prepare
parents 278c7d9b 425c8bb3
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -1273,3 +1273,15 @@ to have them set. Better yet, think hard whether you need different
->d_op for different dentries - if not, just use set_default_d_op()
at mount time and be done with that.  Currently procfs is the only
thing that really needs ->d_op varying between dentries.

---

**highly recommended**

The file operations mmap() callback is deprecated in favour of
mmap_prepare(). This passes a pointer to a vm_area_desc to the callback
rather than a VMA, as the VMA at this stage is not yet valid.

The vm_area_desc provides the minimum required information for a filesystem
to initialise state upon memory mapping of a file-backed region, and output
parameters for the file system to set this state.
+18 −4
Original line number Diff line number Diff line
@@ -1072,12 +1072,14 @@ This describes how the VFS can manipulate an open file. As of kernel

	struct file_operations {
		struct module *owner;
		fop_flags_t fop_flags;
		loff_t (*llseek) (struct file *, loff_t, int);
		ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
		ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
		ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
		ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
		int (*iopoll)(struct kiocb *kiocb, bool spin);
		int (*iopoll)(struct kiocb *kiocb, struct io_comp_batch *,
				unsigned int flags);
		int (*iterate_shared) (struct file *, struct dir_context *);
		__poll_t (*poll) (struct file *, struct poll_table_struct *);
		long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
@@ -1094,18 +1096,24 @@ This describes how the VFS can manipulate an open file. As of kernel
		int (*flock) (struct file *, int, struct file_lock *);
		ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
		ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
		int (*setlease)(struct file *, long, struct file_lock **, void **);
		void (*splice_eof)(struct file *file);
		int (*setlease)(struct file *, int, struct file_lease **, void **);
		long (*fallocate)(struct file *file, int mode, loff_t offset,
				  loff_t len);
		void (*show_fdinfo)(struct seq_file *m, struct file *f);
	#ifndef CONFIG_MMU
		unsigned (*mmap_capabilities)(struct file *);
	#endif
		ssize_t (*copy_file_range)(struct file *, loff_t, struct file *, loff_t, size_t, unsigned int);
		ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
				loff_t, size_t, unsigned int);
		loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,
					   struct file *file_out, loff_t pos_out,
					   loff_t len, unsigned int remap_flags);
		int (*fadvise)(struct file *, loff_t, loff_t, int);
		int (*uring_cmd)(struct io_uring_cmd *ioucmd, unsigned int issue_flags);
		int (*uring_cmd_iopoll)(struct io_uring_cmd *, struct io_comp_batch *,
					unsigned int poll_flags);
		int (*mmap_prepare)(struct vm_area_desc *);
	};

Again, all methods are called without any locks being held, unless
@@ -1145,7 +1153,8 @@ otherwise noted.
	 used on 64 bit kernels.

``mmap``
	called by the mmap(2) system call
	called by the mmap(2) system call. Deprecated in favour of
	``mmap_prepare``.

``open``
	called by the VFS when an inode should be opened.  When the VFS
@@ -1222,6 +1231,11 @@ otherwise noted.
``fadvise``
	possibly called by the fadvise64() system call.

``mmap_prepare``
	Called by the mmap(2) system call. Allows a VFS to set up a
	file-backed memory mapping, most notably establishing relevant
	private state and VMA callbacks.

Note that the file operations are implemented by the specific
filesystem in which the inode resides.  When opening a device node
(character or block special) most filesystems will call special
+6 −6
Original line number Diff line number Diff line
@@ -920,14 +920,14 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start,
	return error;
}

static int blkdev_mmap(struct file *file, struct vm_area_struct *vma)
static int blkdev_mmap_prepare(struct vm_area_desc *desc)
{
	struct inode *bd_inode = bdev_file_inode(file);
	struct file *file = desc->file;

	if (bdev_read_only(I_BDEV(bd_inode)))
		return generic_file_readonly_mmap(file, vma);
	if (bdev_read_only(I_BDEV(bdev_file_inode(file))))
		return generic_file_readonly_mmap_prepare(desc);

	return generic_file_mmap(file, vma);
	return generic_file_mmap_prepare(desc);
}

const struct file_operations def_blk_fops = {
@@ -937,7 +937,7 @@ const struct file_operations def_blk_fops = {
	.read_iter	= blkdev_read_iter,
	.write_iter	= blkdev_write_iter,
	.iopoll		= iocb_bio_iopoll,
	.mmap		= blkdev_mmap,
	.mmap_prepare	= blkdev_mmap_prepare,
	.fsync		= blkdev_fsync,
	.unlocked_ioctl	= blkdev_ioctl,
#ifdef CONFIG_COMPAT
+1 −1
Original line number Diff line number Diff line
@@ -105,7 +105,7 @@ static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *
	if (!obj->base.filp)
		return -ENODEV;

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

+7 −6
Original line number Diff line number Diff line
@@ -454,9 +454,10 @@ int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end,
}

static int
v9fs_file_mmap(struct file *filp, struct vm_area_struct *vma)
v9fs_file_mmap_prepare(struct vm_area_desc *desc)
{
	int retval;
	struct file *filp = desc->file;
	struct inode *inode = file_inode(filp);
	struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);

@@ -464,12 +465,12 @@ v9fs_file_mmap(struct file *filp, struct vm_area_struct *vma)

	if (!(v9ses->cache & CACHE_WRITEBACK)) {
		p9_debug(P9_DEBUG_CACHE, "(read-only mmap mode)");
		return generic_file_readonly_mmap(filp, vma);
		return generic_file_readonly_mmap_prepare(desc);
	}

	retval = generic_file_mmap(filp, vma);
	retval = generic_file_mmap_prepare(desc);
	if (!retval)
		vma->vm_ops = &v9fs_mmap_file_vm_ops;
		desc->vm_ops = &v9fs_mmap_file_vm_ops;

	return retval;
}
@@ -516,7 +517,7 @@ const struct file_operations v9fs_file_operations = {
	.open = v9fs_file_open,
	.release = v9fs_dir_release,
	.lock = v9fs_file_lock,
	.mmap = generic_file_readonly_mmap,
	.mmap_prepare = generic_file_readonly_mmap_prepare,
	.splice_read = v9fs_file_splice_read,
	.splice_write = iter_file_splice_write,
	.fsync = v9fs_file_fsync,
@@ -531,7 +532,7 @@ const struct file_operations v9fs_file_operations_dotl = {
	.release = v9fs_dir_release,
	.lock = v9fs_file_lock_dotl,
	.flock = v9fs_file_flock_dotl,
	.mmap = v9fs_file_mmap,
	.mmap_prepare = v9fs_file_mmap_prepare,
	.splice_read = v9fs_file_splice_read,
	.splice_write = iter_file_splice_write,
	.fsync = v9fs_file_fsync_dotl,
Loading