Skip to contentSkip to conversation

fuse: don't mark inode DAX without CONFIG_FS_DAX

PATCH In review Miklos Szeredi 2 participants 3 messages Nov 19 – Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 fuse: don't mark inode DAX without CONFIG_FS_DAX

fuse_init_inode() installs fuse_dax_aops and sets S_DAX for any regular file whose lookup reply carries FUSE_ATTR_DAX.

Without CONFIG_FS_DAX, S_DAX is zero and IS_DAX() is constant false, so all of the checks that keep such inodes away from the page cache are compiled out: the "DAX inode without FOPEN_PASSTHROUGH" rejection in fuse_file_io_open(), the O_TRUNC check in fuse_open(), the truncate check in fuse_do_setattr(), the check in fuse_file_fallocate() and the one in fuse_passthrough_open(). The address space operations are installed regardless, so the inode ends up in caching I/O mode with aops that have neither ->read_folio nor ->readahead, and a buffered read, mmap fault or splice oopses on a NULL callback. Any user able to mount a fuse filesystem can trigger this.

Only honor FUSE_ATTR_DAX if DAX can work at all.

Fixes 2fbe4586cff3 ("fuse: add support for opening dax device as backing")
---
 fs/fuse/inode.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/fuse/inode.c +8 -1
index 3c0a629cc2fe..c6bfc66317ad 100644
@@ -418,7 +418,14 @@ static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr,
 		fuse_init_common(inode);
 		fuse_init_file_inode(inode, attr->flags);
 
-		if ((attr->flags & FUSE_ATTR_DAX) && !fuse_inode_vdax(inode)) {
+		/*
+		 * Without CONFIG_FS_DAX, S_DAX is zero and IS_DAX() is
+		 * constant false, so all the checks that keep DAX inodes away
+		 * from the page cache are compiled out.  Don't install address
+		 * space operations that have no ->read_folio in that case.
+		 */
+		if (IS_ENABLED(CONFIG_FS_DAX) && (attr->flags & FUSE_ATTR_DAX) &&
+		    !fuse_inode_vdax(inode)) {
 			inode->i_flags |= S_DAX;
 			inode->i_data.a_ops = &fuse_dax_aops;
 		}
Signature · 1 line
2.56.1
#1 lore raw

PATCH 2/2 fuse: advertise support for DAX mappings

Reuse the FUSE_HAS_INODE_DAX flag, which previously was only used for the virtiofs case.

The two uses are unrelated: for virtiofs the flag means that host memory can be mapped directly into the guest, while here it means that the server may back an inode with a dax device opened via FUSE_DEV_IOC_BACKING_OPEN.

Gate the advertisement on IS_REACHABLE(CONFIG_DEV_DAX_FSDEV), which is what fuse_backing_open_file() requires before it accepts a dax device as backing; CONFIG_FS_DAX alone is not sufficient.

Plain fuse servers should check both FUSE_HAS_INODE_DAX and FUSE_PASSTHROUGH_V2 before enabling passthrough dax support.

Stop advertising FUSE_PASSTHROUGH(_V2) in the virtiofs case, which doesn't support passthrough mode.

Note: FUSE_HAS_INODE_DAX flag in the INIT reply is ignored, so it's handled slightly differently from the virtiofs case. This is deliberate. The server needs to check both FUSE_PASSTHROUGH_V2 and FUSE_HAS_INODE_DAX before enabling DAX based extent map if it wants to bail or fall back to plain I/O mode on kernels not supporting this feature. A broken server that does not check the features yet sets FUSE_ATTR_DAX will result in an error on open. This is not backward compatible, since this flag was silently ignored previously, but unlikely to be an issue in real life.

Fixes 2fbe4586cff3 ("fuse: add support for opening dax device as backing")
---
 fs/fuse/fuse_i.h          | 13 +++++++++++++
 fs/fuse/inode.c           | 32 +++++++++++++++++++++++++-------
 fs/fuse/virtio_fs.c       |  1 +
 include/uapi/linux/fuse.h | 10 ++++++++--
 4 files changed, 47 insertions(+), 9 deletions(-)
fs/fuse/fuse_i.h +13 -0
index a033b0bc4576..e0992fcbcc55 100644
@@ -421,6 +421,7 @@ struct fuse_fs_context {
 	bool no_req_timeout:1;
 	bool legacy_opts_show:1;
 	bool syncfs_capable:1;
+	bool is_virtiofs:1;
 	enum fuse_vdax_mode vdax_mode;
 	unsigned int max_read;
 	unsigned int blksize;
@@ -761,6 +762,9 @@ struct fuse_conn {
 	/** @sync_init: Is synchronous FUSE_INIT allowed? */
 	unsigned int sync_init:1;
 
+	/** @is_virtiofs: Is this a virtio-fs connection? */
+	unsigned int is_virtiofs:1;
+
 	/** @max_stack_depth: Maximum stack depth for passthrough backing files */
 	int max_stack_depth;
 
@@ -1273,6 +1277,15 @@ void fuse_free_conn(struct fuse_conn *fc);
 
 /* dax.c */
 
+static inline bool fuse_conn_vdax(struct fuse_conn *fc)
+{
+#ifdef CONFIG_FUSE_VDAX
+	return fc->vdax;
+#else
+	return false;
+#endif
+}
+
 static inline bool fuse_inode_vdax(struct inode *inode)
 {
 #ifdef CONFIG_FUSE_VDAX
fs/fuse/inode.c +25 -7
index c6bfc66317ad..af6d9e0af190 100644
@@ -385,7 +385,7 @@ static void fuse_change_attributes_i(struct inode *inode, struct fuse_attr *attr
 			invalidate_inode_pages2(inode->i_mapping);
 	}
 
-	if (IS_ENABLED(CONFIG_FUSE_VDAX))
+	if (fuse_conn_vdax(fc))
 		fuse_vdax_dontcache(inode, attr->flags);
 }
 
@@ -419,13 +419,18 @@ static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr,
 		fuse_init_file_inode(inode, attr->flags);
 
 		/*
+		 * FUSE_ATTR_DAX means a passthrough extent map on a dax
+		 * device, which only exists on a plain fuse connection.  On
+		 * virtiofs the same bit means per inode virtiofs DAX, which
+		 * is handled by fuse_vdax_inode_init() instead.
+		 *
 		 * Without CONFIG_FS_DAX, S_DAX is zero and IS_DAX() is
 		 * constant false, so all the checks that keep DAX inodes away
 		 * from the page cache are compiled out.  Don't install address
 		 * space operations that have no ->read_folio in that case.
 		 */
-		if (IS_ENABLED(CONFIG_FS_DAX) && (attr->flags & FUSE_ATTR_DAX) &&
-		    !fuse_inode_vdax(inode)) {
+		if (IS_ENABLED(CONFIG_FS_DAX) && !fc->is_virtiofs &&
+		    (attr->flags & FUSE_ATTR_DAX)) {
 			inode->i_flags |= S_DAX;
 			inode->i_data.a_ops = &fuse_dax_aops;
 		}
@@ -1394,7 +1399,7 @@ static void process_init_reply(struct fuse_args *args, int error)
 				    !fuse_vdax_check_alignment(fc, arg->map_alignment)) {
 					ok = false;
 				}
-				if (flags & FUSE_HAS_INODE_DAX)
+				if (fuse_conn_vdax(fc) && (flags & FUSE_HAS_INODE_DAX))
 					fc->inode_vdax = 1;
 			}
 			if (flags & FUSE_HANDLE_KILLPRIV_V2) {
@@ -1423,7 +1428,7 @@ static void process_init_reply(struct fuse_args *args, int error)
 			 * and FUSE_WRITEBACK_CACHE, current design doesn't handle
 			 * them together.
 			 */
-			if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH) &&
+			if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH) && !fm->fc->is_virtiofs &&
 			    (flags & (FUSE_PASSTHROUGH | FUSE_PASSTHROUGH_V2)) &&
 			    arg->max_stack_depth > 0 &&
 			    arg->max_stack_depth <= FILESYSTEM_MAX_STACK_DEPTH &&
@@ -1516,13 +1521,25 @@ static struct fuse_init_args *fuse_new_init(struct fuse_mount *fm)
 #ifdef CONFIG_FUSE_VDAX
 	if (fm->fc->vdax)
 		flags |= FUSE_MAP_ALIGNMENT;
-	if (fuse_is_inode_vdax_mode(fm->fc->vdax_mode))
+	if (fm->fc->vdax && fuse_is_inode_vdax_mode(fm->fc->vdax_mode))
 		flags |= FUSE_HAS_INODE_DAX;
 #endif
 	if (fm->fc->auto_submounts)
 		flags |= FUSE_SUBMOUNTS;
-	if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+	/*
+	 * Passthrough requires a backing file registered with
+	 * FUSE_DEV_IOC_BACKING_OPEN on the fuse device, which a virtiofs
+	 * server has no way to do.
+	 */
+	if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH) && !fm->fc->is_virtiofs) {
 		flags |= FUSE_PASSTHROUGH | FUSE_PASSTHROUGH_V2;
+		/*
+		 * Per inode DAX needs a dax device opened as backing, which
+		 * is unrelated to how virtiofs uses the same flag above.
+		 */
+		if (IS_REACHABLE(CONFIG_DEV_DAX_FSDEV))
+			flags |= FUSE_HAS_INODE_DAX;
+	}
 	/* Only offered to sufficiently privileged servers; see
 	 * fuse_syncfs_enable().
 	 */
@@ -1830,6 +1847,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
 	fc->no_control = ctx->no_control;
 	fc->no_force_umount = ctx->no_force_umount;
 	fc->no_req_timeout = ctx->no_req_timeout;
+	fc->is_virtiofs = ctx->is_virtiofs;
 
 	err = -ENOMEM;
 	root = fuse_get_root_inode(sb, ctx->rootmode);
fs/fuse/virtio_fs.c +1 -0
index cdd6f5c50d6d..e17c96fa876e 100644
@@ -1827,6 +1827,7 @@ static int virtio_fs_init_fs_context(struct fs_context *fsc)
 	ctx = kzalloc_obj(struct fuse_fs_context);
 	if (!ctx)
 		return -ENOMEM;
+	ctx->is_virtiofs = true;
 	fsc->fs_private = ctx;
 	fsc->ops = &virtio_fs_context_ops;
 	return 0;
include/uapi/linux/fuse.h +8 -2
index 616430a2973c..cb9e9554f0ef 100644
@@ -257,6 +257,7 @@
  *  - add backing_id_64 to fuse_open_out
  *  - add FUSE_NOTIFY_BACKING_MAP, fuse_notify_backing_map_out, fuse_extent
  *  - add FUSE_BACKING_MAP_CREATE, FUSE_BACKING_MAP_CYCLIC
+ *  - extend the meaning of FUSE_HAS_INODE_DAX and FUSE_ATTR_DAX
  */
 
 #ifndef _LINUX_FUSE_H
@@ -460,7 +461,11 @@ struct fuse_file_lock {
  * FUSE_INIT_RESERVED: reserved, do not use
  * FUSE_SECURITY_CTX:	add security context to create, mkdir, symlink, and
  *			mknod
- * FUSE_HAS_INODE_DAX:  use per inode DAX
+ * FUSE_HAS_INODE_DAX:  use per inode DAX.  The meaning depends on the
+ *			transport: on virtio-fs it indicates per-inode DAX
+ *			support, while on /dev/fuse (where FUSE_PASSTHROUGH_V2
+ *			is also offered) it indicates support for passthrough
+ *			extent maps on a DAX device.
  * FUSE_CREATE_SUPP_GROUP: add supplementary group info to create, mkdir,
  *			symlink and mknod (single group that matches parent)
  * FUSE_HAS_EXPIRE_ONLY: kernel supports expiry-only entry invalidation
@@ -612,7 +617,8 @@ struct fuse_file_lock {
  * fuse_attr flags
  *
  * FUSE_ATTR_SUBMOUNT: Object is a submount root
- * FUSE_ATTR_DAX: Enable DAX for this file in per inode DAX mode
+ * FUSE_ATTR_DAX: Enable DAX for this file in per inode DAX mode and in
+ *		  extent mapping passthrough mode
  */
 #define FUSE_ATTR_SUBMOUNT      (1 << 0)
 #define FUSE_ATTR_DAX		(1 << 1)
Signature · 1 line
2.56.1
#2 lore raw

Re: [PATCH 2/2] fuse: advertise support for DAX mappings

Hi Miklos,

Quoted 40 lines · Miklos Szeredi

Reuse the FUSE_HAS_INODE_DAX flag, which previously was only used for the virtiofs case.

The two uses are unrelated: for virtiofs the flag means that host memory can be mapped directly into the guest, while here it means that the server may back an inode with a dax device opened via FUSE_DEV_IOC_BACKING_OPEN.

Gate the advertisement on IS_REACHABLE(CONFIG_DEV_DAX_FSDEV), which is what fuse_backing_open_file() requires before it accepts a dax device as backing; CONFIG_FS_DAX alone is not sufficient.

Plain fuse servers should check both FUSE_HAS_INODE_DAX and FUSE_PASSTHROUGH_V2 before enabling passthrough dax support.

Stop advertising FUSE_PASSTHROUGH(_V2) in the virtiofs case, which doesn't support passthrough mode.

Note: FUSE_HAS_INODE_DAX flag in the INIT reply is ignored, so it's handled slightly differently from the virtiofs case. This is deliberate. The server needs to check both FUSE_PASSTHROUGH_V2 and FUSE_HAS_INODE_DAX before enabling DAX based extent map if it wants to bail or fall back to plain I/O mode on kernels not supporting this feature. A broken server that does not check the features yet sets FUSE_ATTR_DAX will result in an error on open. This is not backward compatible, since this flag was silently ignored previously, but unlikely to be an issue in real life.

Fixes 2fbe4586cff3 ("fuse: add support for opening dax device as backing")
---
 fs/fuse/fuse_i.h          | 13 +++++++++++++
 fs/fuse/inode.c           | 32 +++++++++++++++++++++++++-------
 fs/fuse/virtio_fs.c       |  1 +
 include/uapi/linux/fuse.h | 10 ++++++++--
 4 files changed, 47 insertions(+), 9 deletions(-)
fs/fuse/fuse_i.h +0 -0
index a033b0bc4576..e0992fcbcc55 100644

fs/fuse/fuse_i.h struct fuse_fs_context { · line 424

Quoted 4 lines
fs/fuse/fuse_i.h +1 -0
@@ -421,6 +421,7 @@ struct fuse_fs_context {
 	bool no_req_timeout:1;
 	bool legacy_opts_show:1;
 	bool syncfs_capable:1;
+	bool is_virtiofs:1;

is_virtiofs feels a bit hardcoded, and if a third
transport case comes along later, a bool won't be enough
to represent it. Could we consider introducing an enum (e.g., dax_attr) to indicate which semantics FUSE_HAS_INODE_DAX / FUSE_ATTR_DAX has on a given connection—with a single value per connection such as NONE, VIRTIOFS_INODE, or PASSTHROUGH—to make it easier to extend? Just a thought for reference, of course.

Quoted 34 lines
fs/fuse/fuse_i.h +12 -0
 	enum fuse_vdax_mode vdax_mode;
 	unsigned int max_read;
 	unsigned int blksize;
@@ -761,6 +762,9 @@ struct fuse_conn {
 	/** @sync_init: Is synchronous FUSE_INIT allowed? */
 	unsigned int sync_init:1;
 
+	/** @is_virtiofs: Is this a virtio-fs connection? */
+	unsigned int is_virtiofs:1;
+
 	/** @max_stack_depth: Maximum stack depth for passthrough backing files */
 	int max_stack_depth;
 
@@ -1273,6 +1277,15 @@ void fuse_free_conn(struct fuse_conn *fc);
 
 /* dax.c */
 
+static inline bool fuse_conn_vdax(struct fuse_conn *fc)
+{
+#ifdef CONFIG_FUSE_VDAX
+	return fc->vdax;
+#else
+	return false;
+#endif
+}
+
 static inline bool fuse_inode_vdax(struct inode *inode)
 {
 #ifdef CONFIG_FUSE_VDAX
fs/fuse/inode.c +0 -0
index c6bfc66317ad..af6d9e0af190 100644

fs/fuse/inode.c static void fuse_change_attributes_i(struct inode *inode, struct fuse_attr *attr · line 390

Quoted 7 lines
fs/fuse/inode.c +1 -1
@@ -385,7 +385,7 @@ static void fuse_change_attributes_i(struct inode *inode, struct fuse_attr *attr
 			invalidate_inode_pages2(inode->i_mapping);
 	}
 
-	if (IS_ENABLED(CONFIG_FUSE_VDAX))
+	if (fuse_conn_vdax(fc))
 		fuse_vdax_dontcache(inode, attr->flags);
 }

fuse_vdax_dontcache() forces a new inode when virtiofs per-inode DAX and FUSE_ATTR_DAX disagree, but that helper is for a connection with a virtiofs DAX window. Gating it on the window leaves passthrough DAX with no update path: a later getattr that sets or clears FUSE_ATTR_DAX keeps the cached inode, including address_space operations chosen for the old flag. It seems it might be better changed to something like the following:

if (fuse_conn_vdax(fc))

	fuse_vdax_dontcache(inode, attr->flags);
else if (IS_ENABLED(CONFIG_FS_DAX) && !fc->is_virtiofs &&
	S_ISREG(inode->i_mode) &&
	((bool)IS_DAX(inode) != (bool)(attr->flags & FUSE_ATTR_DAX)))
		d_mark_dontcache(inode);

Thanks,
Li Wang