Skip to contentSkip to conversation

Enable per-file/per-directory DAX operations V5

PATCH Superseded [email protected] 6 participants 45 messages Feb 27, 2020 – Apr 6, 2020

Subsystems (10)

v5 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v5 cover Enable per-file/per-directory DAX operations V5

From: Ira Weiny <[email protected]>

Changes from V4:
* Open code the aops lock rather than add it to the xfs_ilock() subsystem (Darrick's comments were obsoleted by this change)
* Fix lkp build suggestions and bugs

Changes from V3:
* Remove global locking... :-D
* put back per inode locking and remove pre-mature optimizations
* Fix issues with Directories having IS_DAX() set
* Fix kernel crash issues reported by Jeff
* Add some clean up patches
* Consolidate diflags to iflags functions
* Update/add documentation
* Reorder/rename patches quite a bit

Changes from V2:

* Move i_dax_sem to be a global percpu_rw_sem rather than per inode Internal discussions with Dan determined this would be easier, just as performant, and slightly less overhead that having it in the SB as suggested by Jan
* Fix locking order in comments and throughout code
* Change "mode" to "state" throughout commits
* Add CONFIG_FS_DAX wrapper to disable inode_[un]lock_state() when not configured
* Add static branch for which is activated by a device which supports DAX in XFS
* Change "lock/unlock" to up/down read/write as appropriate Previous names were over simplified
* Update comments/documentation

* Remove the xfs specific lock to the vfs (global) layer.
* Fix i_dax_sem locking order and comments

* Move 'i_mapped' count from struct inode to struct address_space and rename it to mmap_count
* Add inode_has_mappings() call

* Fix build issues
* Clean up syntax spacing and minor issues
* Update man page text for STATX_ATTR_DAX
* Add reviewed-by's
* Rebase to 5.6

Rename patch:
from: fs/xfs: Add lock/unlock state to xfs
to: fs/xfs: Add write DAX lock to xfs layer
Add patch:
fs/xfs: Clarify lockdep dependency for xfs_isilocked()
Drop patch:
fs/xfs: Fix truncate up

At LSF/MM'19 [1] [2] we discussed applications that overestimate memory consumption due to their inability to detect whether the kernel will instantiate page cache for a file, and cases where a global dax enable via a mount option is too coarse.

The following patch series enables selecting the use of DAX on individual files and/or directories on xfs, and lays some groundwork to do so in ext4. In this scheme the dax mount option can be omitted to allow the per-file property to take effect.

The insight at LSF/MM was to separate the per-mount or per-file "physical" capability switch from an "effective" attribute for the file.

At LSF/MM we discussed the difficulties of switching the DAX state of a file with active mappings / page cache. It was thought the races could be avoided by limiting DAX state flips to 0-length files.

However, this turns out to not be true.[3] This is because address space operations (a_ops) may be in use at any time the inode is referenced and users have expressed a desire to be able to change the DAX state on a file with data in it. For those reasons this patch set allows changing the DAX state flag on a file as long as it is not current mapped.

Details of when and how DAX state can be changed on a file is included in a documentation patch.

It should be noted that the physical DAX flag inheritance is not shown in this patch set as it was maintained from previous work on XFS. The physical DAX flag and it's inheritance will need to be added to other file systems for user control.

As submitted this works on real hardware testing.

[1] https://lwn.net/Articles/787973/
[2] https://lwn.net/Articles/787233/
[3] https://lkml.org/lkml/2019/10/20/96
[4] https://patchwork.kernel.org/patch/11310511/

To: [email protected]
Cc: Alexander Viro <[email protected]>
Cc: "Darrick J. Wong" <[email protected]>
Cc: Dan Williams <[email protected]>
Cc: Dave Chinner <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: "Theodore Y. Ts'o" <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

Ira Weiny (12):
fs/xfs: Remove unnecessary initialization of i_rwsem
fs: Remove unneeded IS_DAX() check
fs/stat: Define DAX statx attribute
fs/xfs: Isolate the physical DAX flag from enabled fs/xfs: Create function xfs_inode_enable_dax()
fs: Add locking for a dynamic address space operations state
fs: Prevent DAX state change if file is mmap'ed fs/xfs: Hold off aops users while changing DAX state fs/xfs: Clean up locking in dax invalidate
fs/xfs: Allow toggle of effective DAX flag
fs/xfs: Remove xfs_diflags_to_linux() Documentation/dax: Update Usage section

 Documentation/filesystems/dax.txt | 84 +++++++++++++++++++++++++-
 Documentation/filesystems/vfs.rst | 16 +++++
 fs/attr.c                         |  1 +
 fs/inode.c                        | 16 ++++-
 fs/iomap/buffered-io.c            |  1 +
 fs/open.c                         |  4 ++
 fs/stat.c                         |  5 ++
 fs/xfs/xfs_icache.c               |  5 +-
 fs/xfs/xfs_inode.h                |  2 +
 fs/xfs/xfs_ioctl.c                | 98 +++++++++++++++----------------
 fs/xfs/xfs_iops.c                 | 69 +++++++++++++++-------
 include/linux/fs.h                | 73 ++++++++++++++++++++++-
 include/uapi/linux/stat.h         |  1 +
 mm/fadvise.c                      |  7 ++-
 mm/filemap.c                      |  4 ++
 mm/huge_memory.c                  |  1 +
 mm/khugepaged.c                   |  2 +
 mm/mmap.c                         | 19 +++++-
 mm/util.c                         |  9 ++-
 19 files changed, 328 insertions(+), 89 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH v5 1/12 fs/xfs: Remove unnecessary initialization of i_rwsem

From: Ira Weiny <[email protected]>

xfs_reinit_inode() -> inode_init_always() already handles calling init_rwsem(i_rwsem). Doing so again is unneeded.

---
New for V4:

NOTE: This was found while ensuring the new i_aops_sem was properly handled. It seems like this is a layering violation so I think it is worth cleaning up so as to not confuse others.

---
 fs/xfs/xfs_icache.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/xfs/xfs_icache.c +1 -3
index 8dc2e5414276..836a1f09be03 100644
@@ -419,6 +419,7 @@ xfs_iget_cache_hit(
 		spin_unlock(&ip->i_flags_lock);
 		rcu_read_unlock();
 
+		ASSERT(!rwsem_is_locked(&inode->i_rwsem));
 		error = xfs_reinit_inode(mp, inode);
 		if (error) {
 			bool wake;
@@ -452,9 +453,6 @@ xfs_iget_cache_hit(
 		ip->i_sick = 0;
 		ip->i_checked = 0;
 
-		ASSERT(!rwsem_is_locked(&inode->i_rwsem));
-		init_rwsem(&inode->i_rwsem);
-
 		spin_unlock(&ip->i_flags_lock);
 		spin_unlock(&pag->pag_ici_lock);
 	} else {
Signature · 1 line
2.21.0
author
#2 lore raw

Re: [PATCH V5 01/12] fs/xfs: Remove unnecessary initialization of i_rwsem

Quoted 6 lines · Ira Weiny

From: Ira Weiny <[email protected]>

xfs_reinit_inode() -> inode_init_always() already handles calling init_rwsem(i_rwsem). Doing so again is unneeded.

Ok... I'm going to blame exchange...

Dan just pointed out that I mist this response from Dave...

Saying this patch was wrong.

https://lore.kernel.org/lkml/[email protected]/

Sorry, I need to read that email and look at it.

Ira

Quoted 36 lines

---
New for V4:

NOTE: This was found while ensuring the new i_aops_sem was properly handled. It seems like this is a layering violation so I think it is worth cleaning up so as to not confuse others.

---
 fs/xfs/xfs_icache.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/xfs/xfs_icache.c +1 -3
index 8dc2e5414276..836a1f09be03 100644
@@ -419,6 +419,7 @@ xfs_iget_cache_hit(
 		spin_unlock(&ip->i_flags_lock);
 		rcu_read_unlock();
 
+		ASSERT(!rwsem_is_locked(&inode->i_rwsem));
 		error = xfs_reinit_inode(mp, inode);
 		if (error) {
 			bool wake;
@@ -452,9 +453,6 @@ xfs_iget_cache_hit(
 		ip->i_sick = 0;
 		ip->i_checked = 0;
 
-		ASSERT(!rwsem_is_locked(&inode->i_rwsem));
-		init_rwsem(&inode->i_rwsem);
-
 		spin_unlock(&ip->i_flags_lock);
 		spin_unlock(&pag->pag_ici_lock);
 	} else {
Signature · 1 line
2.21.0
#3 lore raw

PATCH v5 2/12 fs: Remove unneeded IS_DAX() check

In reply to [email protected]

From: Ira Weiny <[email protected]>

Remove the check because DAX now has it's own read/write methods and file systems which support DAX check IS_DAX() prior to IOCB_DIRECT on their own. Therefore, it does not matter if the file state is DAX when the iocb flags are created.

---
Changes from v3:
Reword commit message.
Reordered to be a 'pre-cleanup' patch

---
 include/linux/fs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/fs.h +1 -1
index 3cd4fe6b845e..63d1e533a07d 100644
@@ -3388,7 +3388,7 @@ extern int file_update_time(struct file *file);
 
 static inline bool io_is_direct(struct file *filp)
 {
-	return (filp->f_flags & O_DIRECT) || IS_DAX(filp->f_mapping->host);
+	return (filp->f_flags & O_DIRECT);
 }
 
 static inline bool vma_is_dax(struct vm_area_struct *vma)
Signature · 1 line
2.21.0
#4 lore raw

PATCH v5 3/12 fs/stat: Define DAX statx attribute

In reply to [email protected]

From: Ira Weiny <[email protected]>

In order for users to determine if a file is currently operating in DAX state (effective DAX). Define a statx attribute value and set that attribute if the effective DAX flag is set.

To go along with this we propose the following addition to the statx man page:

STATX_ATTR_DAX

The file is in the DAX (cpu direct access) state. DAX state attempts to minimize software cache effects for both I/O and memory mappings of this file. It requires a file system which has been configured to support DAX.

DAX generally assumes all accesses are via cpu load / store instructions which can minimize overhead for small accesses, but may adversely affect cpu utilization for large transfers.

File I/O is done directly to/from user-space buffers and memory mapped I/O may be performed with direct memory mappings that bypass kernel page cache.

While the DAX property tends to result in data being transferred synchronously, it does not give the same guarantees of O_SYNC where data and the necessary metadata are transferred together.

A DAX file may support being mapped with the MAP_SYNC flag, which enables a program to use CPU cache flush instructions to persist CPU store operations without an explicit fsync(2). See mmap(2) for more information.

---
Changes from V2:
Update man page text with comments from Darrick, Jan, Dan, and Dave.

---
 fs/stat.c                 | 3 +++
 include/uapi/linux/stat.h | 1 +
 2 files changed, 4 insertions(+)
fs/stat.c +3 -0
index 030008796479..894699c74dde 100644
@@ -79,6 +79,9 @@ int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
 	if (IS_AUTOMOUNT(inode))
 		stat->attributes |= STATX_ATTR_AUTOMOUNT;
 
+	if (IS_DAX(inode))
+		stat->attributes |= STATX_ATTR_DAX;
+
 	if (inode->i_op->getattr)
 		return inode->i_op->getattr(path, stat, request_mask,
 					    query_flags);
include/uapi/linux/stat.h +1 -0
index ad80a5c885d5..e5f9d5517f6b 100644
@@ -169,6 +169,7 @@ struct statx {
 #define STATX_ATTR_ENCRYPTED		0x00000800 /* [I] File requires key to decrypt in fs */
 #define STATX_ATTR_AUTOMOUNT		0x00001000 /* Dir: Automount trigger */
 #define STATX_ATTR_VERITY		0x00100000 /* [I] Verity protected file */
+#define STATX_ATTR_DAX			0x00002000 /* [I] File is DAX */
 
 
 #endif /* _UAPI_LINUX_STAT_H */
Signature · 1 line
2.21.0
#5 lore raw

PATCH v5 4/12 fs/xfs: Isolate the physical DAX flag from enabled

In reply to [email protected]

From: Ira Weiny <[email protected]>

xfs_ioctl_setattr_dax_invalidate() currently checks if the DAX flag is changing as a quick check.

But the implementation mixes the physical (XFS_DIFLAG2_DAX) and the enabled (S_DAX) DAX flags.

Remove the use of the enabled flag when determining if a change of the physical flag is required.

Furthermore, we want the physical flag, XFS_DIFLAG2_DAX, to be changed regardless of if the underlying storage can support DAX or not.

The enabled flag, IS_DAX(), will be set later IFF the inode supports dax in a follow on patch.

---
Changes from V3:
Remove the underlying storage support check Rework commit message
Reorder patch

---
 fs/xfs/xfs_ioctl.c | 20 ++++----------------
 1 file changed, 4 insertions(+), 16 deletions(-)
fs/xfs/xfs_ioctl.c +4 -16
index d42de92cb283..25e12ce85075 100644
@@ -1190,28 +1190,16 @@ xfs_ioctl_setattr_dax_invalidate(
 	int			*join_flags)
 {
 	struct inode		*inode = VFS_I(ip);
-	struct super_block	*sb = inode->i_sb;
 	int			error;
 
 	*join_flags = 0;
 
-	/*
-	 * It is only valid to set the DAX flag on regular files and
-	 * directories on filesystems where the block size is equal to the page
-	 * size. On directories it serves as an inherited hint so we don't
-	 * have to check the device for dax support or flush pagecache.
-	 */
-	if (fa->fsx_xflags & FS_XFLAG_DAX) {
-		struct xfs_buftarg	*target = xfs_inode_buftarg(ip);
-
-		if (!bdev_dax_supported(target->bt_bdev, sb->s_blocksize))
-			return -EINVAL;
-	}
-
 	/* If the DAX state is not changing, we have nothing to do here. */
-	if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
+	if ((fa->fsx_xflags & FS_XFLAG_DAX) &&
+	    (ip->i_d.di_flags2 & XFS_DIFLAG2_DAX))
 		return 0;
-	if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
+	if (!(fa->fsx_xflags & FS_XFLAG_DAX) &&
+	    !(ip->i_d.di_flags2 & XFS_DIFLAG2_DAX))
 		return 0;
 
 	if (S_ISDIR(inode->i_mode))
Signature · 1 line
2.21.0
#6 lore raw

PATCH v5 5/12 fs/xfs: Create function xfs_inode_enable_dax()

In reply to [email protected]

From: Ira Weiny <[email protected]>

xfs_inode_supports_dax() should reflect if the inode can support DAX not that it is enabled for DAX.

Change the use of xfs_inode_supports_dax() to reflect only if the inode and underlying storage support dax.

Add a new function xfs_inode_enable_dax() which reflects if the inode should be enabled for DAX.

---
Changes from v3:
Update functions and names to be more clear Update commit message
Merge with
'fs/xfs: Clean up DAX support check' don't allow IS_DAX() on a directory use STATIC macro for static make xfs_inode_supports_dax() static

---
 fs/xfs/xfs_iops.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
fs/xfs/xfs_iops.c +19 -6
index 81f2f93caec0..ff711efc5247 100644
@@ -1237,19 +1237,18 @@ static const struct inode_operations xfs_inline_symlink_inode_operations = {
 };
 
 /* Figure out if this file actually supports DAX. */
-static bool
+STATIC bool
 xfs_inode_supports_dax(
 	struct xfs_inode	*ip)
 {
 	struct xfs_mount	*mp = ip->i_mount;
 
 	/* Only supported on non-reflinked files. */
-	if (!S_ISREG(VFS_I(ip)->i_mode) || xfs_is_reflink_inode(ip))
+	if (xfs_is_reflink_inode(ip))
 		return false;
 
-	/* DAX mount option or DAX iflag must be set. */
-	if (!(mp->m_flags & XFS_MOUNT_DAX) &&
-	    !(ip->i_d.di_flags2 & XFS_DIFLAG2_DAX))
+	/* Only supported on regular files. */
+	if (!S_ISREG(VFS_I(ip)->i_mode))
 		return false;
 
 	/* Block size must match page size */
@@ -1260,6 +1259,20 @@ xfs_inode_supports_dax(
 	return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
 }
 
+STATIC bool
+xfs_inode_enable_dax(
+	struct xfs_inode *ip)
+{
+	if (!xfs_inode_supports_dax(ip))
+		return false;
+
+	if (ip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
+		return true;
+	if (ip->i_mount->m_flags & XFS_MOUNT_DAX)
+		return true;
+	return false;
+}
+
 STATIC void
 xfs_diflags_to_iflags(
 	struct inode		*inode,
@@ -1278,7 +1291,7 @@ xfs_diflags_to_iflags(
 		inode->i_flags |= S_SYNC;
 	if (flags & XFS_DIFLAG_NOATIME)
 		inode->i_flags |= S_NOATIME;
-	if (xfs_inode_supports_dax(ip))
+	if (xfs_inode_enable_dax(ip))
 		inode->i_flags |= S_DAX;
 }
 
Signature · 1 line
2.21.0
#7 lore raw

Re: [PATCH V5 05/12] fs/xfs: Create function xfs_inode_enable_dax()

Quoted 42 lines · On Wed, Feb 26, 2020 at 09:24:35PM -0800, [email protected] wrote:

From: Ira Weiny <[email protected]>

xfs_inode_supports_dax() should reflect if the inode can support DAX not that it is enabled for DAX.

Change the use of xfs_inode_supports_dax() to reflect only if the inode and underlying storage support dax.

Add a new function xfs_inode_enable_dax() which reflects if the inode should be enabled for DAX.

---
Changes from v3:
Update functions and names to be more clear Update commit message
Merge with
'fs/xfs: Clean up DAX support check' don't allow IS_DAX() on a directory use STATIC macro for static make xfs_inode_supports_dax() static

---
 fs/xfs/xfs_iops.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
fs/xfs/xfs_iops.c +1 -1
index 81f2f93caec0..ff711efc5247 100644
@@ -1237,19 +1237,18 @@ static const struct inode_operations xfs_inline_symlink_inode_operations = {
 };
 
 /* Figure out if this file actually supports DAX. */
-static bool
+STATIC bool
 xfs_inode_supports_dax(
 	struct xfs_inode	*ip)
 {
 	struct xfs_mount	*mp = ip->i_mount;
 
 	/* Only supported on non-reflinked files. */

fs/xfs/xfs_iops.c static const struct inode_operations xfs_inline_symlink_inode_operations = { · line 1252

Quoted 10 lines
fs/xfs/xfs_iops.c +3 -4
-	if (!S_ISREG(VFS_I(ip)->i_mode) || xfs_is_reflink_inode(ip))
+	if (xfs_is_reflink_inode(ip))
 		return false;
 
-	/* DAX mount option or DAX iflag must be set. */
-	if (!(mp->m_flags & XFS_MOUNT_DAX) &&
-	    !(ip->i_d.di_flags2 & XFS_DIFLAG2_DAX))
+	/* Only supported on regular files. */
+	if (!S_ISREG(VFS_I(ip)->i_mode))
 		return false;

Why split the initial check? The "non-reflinked files" comment is pretty explicit, so splitting it just to add a "only support on regular files" comment doesn't actually add any value here.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#8 lore raw

PATCH v5 6/12 fs: Add locking for a dynamic address space operations state

In reply to [email protected]

From: Ira Weiny <[email protected]>

DAX requires special address space operations (aops). Changing DAX state therefore requires changing those aops.

However, many functions require aops to remain consistent through a deep call stack.

Define a vfs level inode rwsem to protect aops throughout call stacks which require them.

Finally, define calls to be used in subsequent patches when aops usage needs to be quiesced by the file system.

---
Changes from V4:
Fix 0-day build issue.

Changes from V3:
Convert from global to a per-inode rwsem
Remove pre-optimization
Remove static branch stuff
Change function names from inode_dax_state_* to inode_aops_*
I kept 'inode' as the synchronization is at the inode level now (probably where it belongs)...

and I still prefer *_[down|up]_[read|write] as those names better reflect the use and interaction between users (readers) and writers better. 'XX_start_aop()' would have to be matched with something like 'XX_wait_for_aop_user()' and 'XX_release_aop_users()' or something which does not make sense on the 'writer' side.

Changes from V2

Rebase on linux-next-08-02-2020

Fix locking order
Change all references from mode to state where appropriate add CONFIG_FS_DAX requirement for state change
Use a static branch to enable locking only when a dax capable
device has been seen.

Move the lock to a global vfs lock

this does a few things
1) preps us better for ext4 support
2) removes funky callbacks from inode ops
3) remove complexity from XFS and probably from ext4 later

We can do this because
1) the locking order is required to be at the highest level anyway, so why complicate xfs
2) We had to move the sem to the super_block because it is too heavy for the inode.
3) After internal discussions with Dan we decided that this would be easier, just as performant, and with slightly less overhead than in the VFS SB.

We also change the functions names to up/down; read/write as appropriate. Previous names were over simplified.

Update comments and documentation

squash: add locking

squash:...

---
 Documentation/filesystems/vfs.rst | 16 ++++++++
 fs/attr.c                         |  1 +
 fs/inode.c                        | 15 ++++++--
 fs/iomap/buffered-io.c            |  1 +
 fs/open.c                         |  4 ++
 fs/stat.c                         |  2 +
 fs/xfs/xfs_icache.c               |  1 +
 include/linux/fs.h                | 64 ++++++++++++++++++++++++++++++-
 mm/fadvise.c                      |  7 +++-
 mm/filemap.c                      |  4 ++
 mm/huge_memory.c                  |  1 +
 mm/khugepaged.c                   |  2 +
 mm/util.c                         |  9 ++++-
 13 files changed, 119 insertions(+), 8 deletions(-)
Documentation/filesystems/vfs.rst +16 -0
index 7d4d09dd5e6d..4a10a232f8e2 100644
@@ -934,6 +934,22 @@ cache in your filesystem.  The following members are defined:
 	Called during swapoff on files where swap_activate was
 	successful.
 
+Changing DAX 'state' dynamically
+----------------------------------
+
+Some file systems which support DAX want to be able to change the DAX state
+dyanically.  To switch the state safely we lock the inode state in all "normal"
+file system operations and restrict state changes to those operations.  The
+specific rules are.
+
+        1) the direct_IO address_space_operation must be supported in all
+           potential a_ops vectors for any state suported by the inode.
+
+        3) DAX state changes shall not be allowed while the file is mmap'ed
+        4) For non-mmaped operations the VFS layer must take the read lock for any
+           use of IS_DAX()
+        5) Filesystems take the write lock when changing DAX states.
+
 
 The File Object
 ===============
fs/attr.c +1 -0
index b4bbdbd4c8ca..9b15f73d1079 100644
@@ -332,6 +332,7 @@ int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **de
 	if (error)
 		return error;
 
+	/* DAX read state should already be held here */
 	if (inode->i_op->setattr)
 		error = inode->i_op->setattr(dentry, attr);
 	else
fs/inode.c +12 -3
index 7d57068b6b7a..6e4f1cc872f2 100644
@@ -200,6 +200,7 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
 #endif
 	inode->i_flctx = NULL;
 	this_cpu_inc(nr_inodes);
+	init_rwsem(&inode->i_aops_sem);
 
 	return 0;
 out:
@@ -1616,11 +1617,19 @@ EXPORT_SYMBOL(iput);
  */
 int bmap(struct inode *inode, sector_t *block)
 {
-	if (!inode->i_mapping->a_ops->bmap)
-		return -EINVAL;
+	int ret = 0;
+
+	inode_aops_down_read(inode);
+	if (!inode->i_mapping->a_ops->bmap) {
+		ret = -EINVAL;
+		goto err;
+	}
 
 	*block = inode->i_mapping->a_ops->bmap(inode->i_mapping, *block);
-	return 0;
+
+err:
+	inode_aops_up_read(inode);
+	return ret;
 }
 EXPORT_SYMBOL(bmap);
 #endif
fs/iomap/buffered-io.c +1 -0
index 7c84c4c027c4..e313a34d5fa6 100644
@@ -999,6 +999,7 @@ iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count,
 		offset = offset_in_page(pos);
 		bytes = min_t(loff_t, PAGE_SIZE - offset, count);
 
+		/* DAX state read should already be held here */
 		if (IS_DAX(inode))
 			status = iomap_dax_zero(pos, offset, bytes, iomap);
 		else
fs/open.c +4 -0
index 0788b3715731..3abf0bfac462 100644
@@ -59,10 +59,12 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
 	if (ret)
 		newattrs.ia_valid |= ret | ATTR_FORCE;
 
+	inode_aops_down_read(dentry->d_inode);
 	inode_lock(dentry->d_inode);
 	/* Note any delegations or leases have already been broken: */
 	ret = notify_change(dentry, &newattrs, NULL);
 	inode_unlock(dentry->d_inode);
+	inode_aops_up_read(dentry->d_inode);
 	return ret;
 }
 
@@ -306,7 +308,9 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
 		return -EOPNOTSUPP;
 
 	file_start_write(file);
+	inode_aops_down_read(inode);
 	ret = file->f_op->fallocate(file, mode, offset, len);
+	inode_aops_up_read(inode);
 
 	/*
 	 * Create inotify and fanotify events.
fs/stat.c +2 -0
index 894699c74dde..274b3ccc82b1 100644
@@ -79,8 +79,10 @@ int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
 	if (IS_AUTOMOUNT(inode))
 		stat->attributes |= STATX_ATTR_AUTOMOUNT;
 
+	inode_aops_down_read(inode);
 	if (IS_DAX(inode))
 		stat->attributes |= STATX_ATTR_DAX;
+	inode_aops_up_read(inode);
 
 	if (inode->i_op->getattr)
 		return inode->i_op->getattr(path, stat, request_mask,
fs/xfs/xfs_icache.c +1 -0
index 836a1f09be03..3e83a97dc047 100644
@@ -420,6 +420,7 @@ xfs_iget_cache_hit(
 		rcu_read_unlock();
 
 		ASSERT(!rwsem_is_locked(&inode->i_rwsem));
+		ASSERT(!rwsem_is_locked(&inode->i_aops_sem));
 		error = xfs_reinit_inode(mp, inode);
 		if (error) {
 			bool wake;
include/linux/fs.h +62 -2

102 lines not on this page · raw message

mm/fadvise.c +5 -2
index 4f17c83db575..6a30febb11e0 100644
@@ -48,6 +48,8 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
 	bdi = inode_to_bdi(mapping->host);
 
 	if (IS_DAX(inode) || (bdi == &noop_backing_dev_info)) {
+		int ret = 0;
+
 		switch (advice) {
 		case POSIX_FADV_NORMAL:
 		case POSIX_FADV_RANDOM:
@@ -58,9 +60,10 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
 			/* no bad return value, but ignore advice */
 			break;
 		default:
-			return -EINVAL;
+			ret = -EINVAL;
 		}
-		return 0;
+
+		return ret;
 	}
 
 	/*
mm/filemap.c +4 -0
index 1784478270e1..3a7863ba51b9 100644
@@ -2293,6 +2293,8 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
 		 * and return.  Otherwise fallthrough to buffered io for
 		 * the rest of the read.  Buffered reads will not work for
 		 * DAX files, so don't bother trying.
+		 *
+		 * IS_DAX is protected under ->read_iter lock
 		 */
 		if (retval < 0 || !count || iocb->ki_pos >= size ||
 		    IS_DAX(inode))
@@ -3377,6 +3379,8 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 		 * holes, for example.  For DAX files, a buffered write will
 		 * not succeed (even if it did, DAX does not handle dirty
 		 * page-cache pages correctly).
+		 *
+		 * IS_DAX is protected under ->write_iter lock
 		 */
 		if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
 			goto out;
mm/huge_memory.c +1 -0
index b08b199f9a11..3d05bd10d83e 100644
@@ -572,6 +572,7 @@ unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
 	unsigned long ret;
 	loff_t off = (loff_t)pgoff << PAGE_SHIFT;
 
+	/* Should not need locking here because mmap is not allowed */
 	if (!IS_DAX(filp->f_mapping->host) || !IS_ENABLED(CONFIG_FS_DAX_PMD))
 		goto out;
 
mm/khugepaged.c +2 -0
index b679908743cb..f048178e2b93 100644
@@ -1592,9 +1592,11 @@ static void collapse_file(struct mm_struct *mm,
 		} else {	/* !is_shmem */
 			if (!page || xa_is_value(page)) {
 				xas_unlock_irq(&xas);
+				inode_aops_down_read(file->f_inode);
 				page_cache_sync_readahead(mapping, &file->f_ra,
 							  file, index,
 							  PAGE_SIZE);
+				inode_aops_up_read(file->f_inode);
 				/* drain pagevecs to help isolate_lru_page() */
 				lru_add_drain();
 				page = find_lock_page(mapping, index);
mm/util.c +8 -1
index 988d11e6c17c..a4fb0670137d 100644
@@ -501,11 +501,18 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 
 	ret = security_mmap_file(file, prot, flag);
 	if (!ret) {
-		if (down_write_killable(&mm->mmap_sem))
+		if (file)
+			inode_aops_down_read(file_inode(file));
+		if (down_write_killable(&mm->mmap_sem)) {
+			if (file)
+				inode_aops_up_read(file_inode(file));
 			return -EINTR;
+		}
 		ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
 				    &populate, &uf);
 		up_write(&mm->mmap_sem);
+		if (file)
+			inode_aops_up_read(file_inode(file));
 		userfaultfd_unmap_complete(mm, &uf);
 		if (populate)
 			mm_populate(ret, populate);
Signature · 1 line
2.21.0
#9 lore raw

Re: [PATCH V5 06/12] fs: Add locking for a dynamic address space operations state

Quoted 15 lines · On Wed, Feb 26, 2020 at 09:24:36PM -0800, [email protected] wrote:

From: Ira Weiny <[email protected]>

DAX requires special address space operations (aops). Changing DAX state therefore requires changing those aops.

However, many functions require aops to remain consistent through a deep call stack.

Define a vfs level inode rwsem to protect aops throughout call stacks which require them.

Finally, define calls to be used in subsequent patches when aops usage needs to be quiesced by the file system.

....

fs/stat.c int vfs_getattr_nosec(const struct path *path, struct kstat *stat, · line 85

Quoted 12 lines
fs/stat.c +2 -0
index 894699c74dde..274b3ccc82b1 100644
@@ -79,8 +79,10 @@ int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
 	if (IS_AUTOMOUNT(inode))
 		stat->attributes |= STATX_ATTR_AUTOMOUNT;
 
+	inode_aops_down_read(inode);
 	if (IS_DAX(inode))
 		stat->attributes |= STATX_ATTR_DAX;
+	inode_aops_up_read(inode);

No locking needed here. statx() is racy to begin with (i.e. information will be wrong by the time the syscall gets back to userspace. Hence it really doesn't matter if we race checking the flag here or not, and because this is a lockless fast path for many worklaods (e.g. git), keeping locking out of the stat() fast path is desirable.

Quoted 7 lines
fs/stat.c +0 -0
 	if (inode->i_op->getattr)
 		return inode->i_op->getattr(path, stat, request_mask,
fs/xfs/xfs_icache.c +0 -0
index 836a1f09be03..3e83a97dc047 100644

fs/xfs/xfs_icache.c xfs_iget_cache_hit( · line 426

Quoted 7 lines
fs/xfs/xfs_icache.c +1 -0
@@ -420,6 +420,7 @@ xfs_iget_cache_hit(
 		rcu_read_unlock();
 
 		ASSERT(!rwsem_is_locked(&inode->i_rwsem));
+		ASSERT(!rwsem_is_locked(&inode->i_aops_sem));
 		error = xfs_reinit_inode(mp, inode);
 		if (error) {
 			bool wake;

No need for this - aops can never be called on an XFS inode in the reclaimable state - it's not visible to the VFS at this point. If you need to assert that the lock has not been leaked, then this needs to be done at the point where the VFS reclaims the inode (i.e. the evict() path), not deep in XFS.

Quoted 15 lines

static inline ssize_t call_read_iter(struct file *file, struct kiocb *kio,

fs/xfs/xfs_icache.c +7 -1
 				     struct iov_iter *iter)
 {
-	return file->f_op->read_iter(kio, iter);
+	struct inode		*inode = file_inode(kio->ki_filp);
+	ssize_t ret;
+
+	inode_aops_down_read(inode);
+	ret = file->f_op->read_iter(kio, iter);
+	inode_aops_up_read(inode);
+	return ret;
 }
 
 static inline ssize_t call_write_iter(struct file *file, struct kiocb *kio,
 				      struct iov_iter *iter)

fs/xfs/xfs_icache.c

Quoted 10 lines
fs/xfs/xfs_icache.c +7 -1
 {
-	return file->f_op->write_iter(kio, iter);
+	struct inode		*inode = file_inode(kio->ki_filp);
+	ssize_t ret;
+
+	inode_aops_down_read(inode);
+	ret = file->f_op->write_iter(kio, iter);
+	inode_aops_up_read(inode);
+	return ret;
 }

I'm really on the fence about this. I don't really like it, but I can't really put my finger on why :/

Quoted 16 lines

static inline int call_mmap(struct file *file, struct vm_area_struct *vma)

mm/fadvise.c +2 -0
index 4f17c83db575..6a30febb11e0 100644
@@ -48,6 +48,8 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
 	bdi = inode_to_bdi(mapping->host);
 
 	if (IS_DAX(inode) || (bdi == &noop_backing_dev_info)) {
+		int ret = 0;
+
 		switch (advice) {
 		case POSIX_FADV_NORMAL:
 		case POSIX_FADV_RANDOM:

mm/fadvise.c int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice) · line 67

Quoted 10 lines
mm/fadvise.c +3 -2
@@ -58,9 +60,10 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
 			/* no bad return value, but ignore advice */
 			break;
 		default:
-			return -EINVAL;
+			ret = -EINVAL;
 		}
-		return 0;
+
+		return ret;
 	}

Completely spurious changes?

Quoted 7 lines
mm/fadvise.c +0 -0
 	/*
mm/filemap.c +0 -0
index 1784478270e1..3a7863ba51b9 100644

mm/filemap.c generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) · line 2300

Quoted 8 lines
mm/filemap.c +2 -0
@@ -2293,6 +2293,8 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
 		 * and return.  Otherwise fallthrough to buffered io for
 		 * the rest of the read.  Buffered reads will not work for
 		 * DAX files, so don't bother trying.
+		 *
+		 * IS_DAX is protected under ->read_iter lock
 		 */
 		if (retval < 0 || !count || iocb->ki_pos >= size ||
 		    IS_DAX(inode))

This check is in the DIO path, be we can't do DIO on DAX enabled files to begin with, so we can only get here if S_DAX is not set on the file.

Further, if IOCB_DIRECT is set, neither ext4 nor XFS call generic_file_read_iter(); they run the iomap_dio_rw() path directly instead. Only ext2 calls generic_file_read_iter() to do direct IO, so it's the only filesystem that needs this IS_DAX() check in it.

I think we should fix ext2 to be implemented like ext4 and XFS - they implement the buffered IO fallback, should it be required, themselves and never use generic_file_read_iter() for direct IO.

That would allow us to add this to generic_file_read_iter():

	if (WARN_ON_ONCE(IS_DAX(inode))
		return -EINVAL;

to indicate that this should never be called directly on a DAX capable filesystem. This places all the responsibility for managing DAX behaviour on the filesystem, which then allows us to reason more solidly about how the filesystem IO paths use and check the S_DAX flag.

i.e. changing the on-disk flag already locks out the filesystem IO path via the i_rwsem(), and all the filesystem IO paths (buffered, direct IO and dax) are serialised by this flag. Hence we can check once in the filesystem path once we have the i_rwsem held and know that S_DAX will not change until we release it.

..... and now I realise what I was sitting on the fence about....

I don't like the aops locking in call_read/write_iter() because it is actually redundant: the filesystem should be doing the necessary locking in the IO path via the i_rwsem to prevent S_DAX from changing while it is doing the IO.

IOWs, we need to restructure the locking inside the filesystem read_iter and write_iter methods so that the i_rwsem protects the S_DAX flag from changing dynamically. They all do:

	if (dax)
		do_dax_io()
	if (direct)
		do_direct_io()
	do_buffered_io()

And then we take the i_rwsem inside each of those functions and do the IO. What we actually need to do is something like this:

	inode_lock_shared()
	if (dax)
		do_dax_io()
	if (direct)
		do_direct_io()
	do_buffered_io()
	inode_unlock_shared()

And remove the inode locking from inside the individual IO methods themselves. It's a bit more complex than this because buffered writes require exclusive locking, but this completely removes the need for holding an aops lock over these methods.

I've attached a couple of untested patches (I've compiled them, so they must be good!) to demonstrate what I mean for the XFS IO path. The read side removes a heap of duplicate code, but the write side is .... unfortunately complex. Have to think about that more.

mm/filemap.c ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from) · line 3386

Quoted 9 lines
mm/filemap.c +2 -0
@@ -3377,6 +3379,8 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 		 * holes, for example.  For DAX files, a buffered write will
 		 * not succeed (even if it did, DAX does not handle dirty
 		 * page-cache pages correctly).
+		 *
+		 * IS_DAX is protected under ->write_iter lock
 		 */
 		if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
 			goto out;

Same here - this should never be called for DAX+iomap capable filesystems.

Quoted 17 lines
mm/huge_memory.c +1 -0
index b08b199f9a11..3d05bd10d83e 100644
@@ -572,6 +572,7 @@ unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
 	unsigned long ret;
 	loff_t off = (loff_t)pgoff << PAGE_SHIFT;
 
+	/* Should not need locking here because mmap is not allowed */
 	if (!IS_DAX(filp->f_mapping->host) || !IS_ENABLED(CONFIG_FS_DAX_PMD))
 		goto out;
 
mm/khugepaged.c +0 -0
index b679908743cb..f048178e2b93 100644

mm/khugepaged.c static void collapse_file(struct mm_struct *mm, · line 1599

Quoted 8 lines
mm/khugepaged.c +2 -0
@@ -1592,9 +1592,11 @@ static void collapse_file(struct mm_struct *mm,
 		} else {	/* !is_shmem */
 			if (!page || xa_is_value(page)) {
 				xas_unlock_irq(&xas);
+				inode_aops_down_read(file->f_inode);
 				page_cache_sync_readahead(mapping, &file->f_ra,
 							  file, index,
 							  PAGE_SIZE);
+				inode_aops_up_read(file->f_inode);

Why is this readahead call needing aops protection, but not anywhere else? And if this is not being done while holding a filesystem lock (like i_rwsem or MMAPLOCK) then how is this safe against concurent hole punch? i.e. doesn't it have exactly the same problems as readahead in vfs_fadvise(WILL_NEED)?

Quoted 17 lines
mm/khugepaged.c +0 -0
 				/* drain pagevecs to help isolate_lru_page() */
 				lru_add_drain();
 				page = find_lock_page(mapping, index);
mm/util.c +5 -1
index 988d11e6c17c..a4fb0670137d 100644
@@ -501,11 +501,18 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 
 	ret = security_mmap_file(file, prot, flag);
 	if (!ret) {
-		if (down_write_killable(&mm->mmap_sem))
+		if (file)
+			inode_aops_down_read(file_inode(file));
+		if (down_write_killable(&mm->mmap_sem)) {
+			if (file)
+				inode_aops_up_read(file_inode(file));

mm/util.c unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, · line 518

Quoted 10 lines
mm/util.c +3 -0
 			return -EINTR;
+		}
 		ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
 				    &populate, &uf);
 		up_write(&mm->mmap_sem);
+		if (file)
+			inode_aops_up_read(file_inode(file));
 		userfaultfd_unmap_complete(mm, &uf);
 		if (populate)
 			mm_populate(ret, populate);

So this path calls the fops->mmap() filesystem method that we check IS_DAX() in. As Christoph has mentioned, this could likely go away if we took the XFS_MMAPLOCK_SHARED() inside xfs_file_mmap(), as that would then serialise new mmaps against the transaction where we are changing the on disk flag. i.e. all new attempts to mmap() the file would then get blocked by the filesystem while the change is taking place...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#10 lore raw

Re: [PATCH V5 06/12] fs: Add locking for a dynamic address space operations state

Quoted 78 lines · Dave Chinner
Quoted 6 lines
(quoted diff) +0 -0
 	/*
mm/filemap.c +0 -0
index 1784478270e1..3a7863ba51b9 100644

mm/filemap.c generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) · line 2300

Quoted 8 lines
mm/filemap.c +2 -0
@@ -2293,6 +2293,8 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
 		 * and return.  Otherwise fallthrough to buffered io for
 		 * the rest of the read.  Buffered reads will not work for
 		 * DAX files, so don't bother trying.
+		 *
+		 * IS_DAX is protected under ->read_iter lock
 		 */
 		if (retval < 0 || !count || iocb->ki_pos >= size ||
 		    IS_DAX(inode))

This check is in the DIO path, be we can't do DIO on DAX enabled files to begin with, so we can only get here if S_DAX is not set on the file.

Further, if IOCB_DIRECT is set, neither ext4 nor XFS call generic_file_read_iter(); they run the iomap_dio_rw() path directly instead. Only ext2 calls generic_file_read_iter() to do direct IO, so it's the only filesystem that needs this IS_DAX() check in it.

I think we should fix ext2 to be implemented like ext4 and XFS - they implement the buffered IO fallback, should it be required, themselves and never use generic_file_read_iter() for direct IO.

That would allow us to add this to generic_file_read_iter():

	if (WARN_ON_ONCE(IS_DAX(inode))
		return -EINVAL;

to indicate that this should never be called directly on a DAX capable filesystem. This places all the responsibility for managing DAX behaviour on the filesystem, which then allows us to reason more solidly about how the filesystem IO paths use and check the S_DAX flag.

i.e. changing the on-disk flag already locks out the filesystem IO path via the i_rwsem(), and all the filesystem IO paths (buffered, direct IO and dax) are serialised by this flag. Hence we can check once in the filesystem path once we have the i_rwsem held and know that S_DAX will not change until we release it.

..... and now I realise what I was sitting on the fence about....

I don't like the aops locking in call_read/write_iter() because it is actually redundant: the filesystem should be doing the necessary locking in the IO path via the i_rwsem to prevent S_DAX from changing while it is doing the IO.

IOWs, we need to restructure the locking inside the filesystem read_iter and write_iter methods so that the i_rwsem protects the S_DAX flag from changing dynamically. They all do:

	if (dax)
		do_dax_io()
	if (direct)
		do_direct_io()
	do_buffered_io()

And then we take the i_rwsem inside each of those functions and do the IO. What we actually need to do is something like this:

	inode_lock_shared()
	if (dax)
		do_dax_io()
	if (direct)
		do_direct_io()
	do_buffered_io()
	inode_unlock_shared()

And remove the inode locking from inside the individual IO methods themselves. It's a bit more complex than this because buffered writes require exclusive locking, but this completely removes the need for holding an aops lock over these methods.

Quoted 4 lines

I've attached a couple of untested patches (I've compiled them, so they must be good!) to demonstrate what I mean for the XFS IO path. The read side removes a heap of duplicate code, but the write side is .... unfortunately complex. Have to think about that more.

And now with patches...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]

xfs: pulling read IO path locking up to protect S_DAX checks

From: Dave Chinner <[email protected]>

---
 fs/xfs/xfs_file.c | 77 +++++++++++++++++--------------------------------------
 1 file changed, 24 insertions(+), 53 deletions(-)
fs/xfs/xfs_file.c +24 -53
index b8a4a3f29b36..7d31a0e76b68 100644
@@ -176,28 +176,12 @@ xfs_file_dio_aio_read(
 	struct kiocb		*iocb,
 	struct iov_iter		*to)
 {
-	struct xfs_inode	*ip = XFS_I(file_inode(iocb->ki_filp));
-	size_t			count = iov_iter_count(to);
-	ssize_t			ret;
-
-	trace_xfs_file_direct_read(ip, count, iocb->ki_pos);
-
-	if (!count)
-		return 0; /* skip atime */
+	trace_xfs_file_direct_read(XFS_I(file_inode(iocb->ki_filp)),
+					iov_iter_count(to), iocb->ki_pos);
 
 	file_accessed(iocb->ki_filp);
-
-	if (iocb->ki_flags & IOCB_NOWAIT) {
-		if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
-			return -EAGAIN;
-	} else {
-		xfs_ilock(ip, XFS_IOLOCK_SHARED);
-	}
-	ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL,
+	return iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL,
 			is_sync_kiocb(iocb));
-	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
-
-	return ret;
 }
 
 static noinline ssize_t
@@ -205,27 +189,12 @@ xfs_file_dax_read(
 	struct kiocb		*iocb,
 	struct iov_iter		*to)
 {
-	struct xfs_inode	*ip = XFS_I(iocb->ki_filp->f_mapping->host);
-	size_t			count = iov_iter_count(to);
-	ssize_t			ret = 0;
-
-	trace_xfs_file_dax_read(ip, count, iocb->ki_pos);
-
-	if (!count)
-		return 0; /* skip atime */
-
-	if (iocb->ki_flags & IOCB_NOWAIT) {
-		if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
-			return -EAGAIN;
-	} else {
-		xfs_ilock(ip, XFS_IOLOCK_SHARED);
-	}
-
-	ret = dax_iomap_rw(iocb, to, &xfs_read_iomap_ops);
-	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
+	trace_xfs_file_dax_read(XFS_I(file_inode(iocb->ki_filp)),
+					iov_iter_count(to), iocb->ki_pos);
 
 	file_accessed(iocb->ki_filp);
-	return ret;
+	return dax_iomap_rw(iocb, to, &xfs_read_iomap_ops);
+
 }
 
 STATIC ssize_t
@@ -233,21 +202,10 @@ xfs_file_buffered_aio_read(
 	struct kiocb		*iocb,
 	struct iov_iter		*to)
 {
-	struct xfs_inode	*ip = XFS_I(file_inode(iocb->ki_filp));
-	ssize_t			ret;
-
-	trace_xfs_file_buffered_read(ip, iov_iter_count(to), iocb->ki_pos);
+	trace_xfs_file_buffered_read(XFS_I(file_inode(iocb->ki_filp)),
+					iov_iter_count(to), iocb->ki_pos);
 
-	if (iocb->ki_flags & IOCB_NOWAIT) {
-		if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
-			return -EAGAIN;
-	} else {
-		xfs_ilock(ip, XFS_IOLOCK_SHARED);
-	}
-	ret = generic_file_read_iter(iocb, to);
-	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
-
-	return ret;
+	return generic_file_read_iter(iocb, to);
 }
 
 STATIC ssize_t
@@ -256,7 +214,8 @@ xfs_file_read_iter(
 	struct iov_iter		*to)
 {
 	struct inode		*inode = file_inode(iocb->ki_filp);
-	struct xfs_mount	*mp = XFS_I(inode)->i_mount;
+	struct xfs_inode	*ip = XFS_I(inode);
+	struct xfs_mount	*mp = ip->i_mount;
 	ssize_t			ret = 0;
 
 	XFS_STATS_INC(mp, xs_read_calls);
@@ -264,6 +223,16 @@ xfs_file_read_iter(
 	if (XFS_FORCED_SHUTDOWN(mp))
 		return -EIO;
 
+	if (!iov_iter_count(to))
+		return 0; /* skip atime */
+
+	if (iocb->ki_flags & IOCB_NOWAIT) {
+		if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
+			return -EAGAIN;
+	} else {
+		xfs_ilock(ip, XFS_IOLOCK_SHARED);
+	}
+
 	if (IS_DAX(inode))
 		ret = xfs_file_dax_read(iocb, to);
 	else if (iocb->ki_flags & IOCB_DIRECT)
@@ -271,6 +240,8 @@ xfs_file_read_iter(
 	else
 		ret = xfs_file_buffered_aio_read(iocb, to);
 
+	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
+
 	if (ret > 0)
 		XFS_STATS_ADD(mp, xs_read_bytes, ret);
 	return ret;

xfs: pull write IO locking up to protect S_DAX

From: Dave Chinner <[email protected]>

Much more complex than the read side because of all the lock juggling we for the different types of writes and all the various metadata updates a write might need prior to dispatching the data IO.

Not really happy about the way the high level logic turned out; could probably be make cleaner and smarter with a bit more thought.

---
 fs/xfs/xfs_file.c | 205 ++++++++++++++++++++++++++++++------------------------
 1 file changed, 115 insertions(+), 90 deletions(-)
fs/xfs/xfs_file.c +115 -90

334 lines not on this page · raw message

#11 lore raw

PATCH v5 7/12 fs: Prevent DAX state change if file is mmap'ed

In reply to [email protected]

From: Ira Weiny <[email protected]>

Page faults need to ensure the inode DAX configuration is correct and consistent with the vmf information at the time of the fault. There is no easy way to ensure the vmf information is correct if a DAX change is in progress. Furthermore, there is no good use case to require changing DAX configs while the file is mmap'ed.

Track mmap's of the file and fail the DAX change if the file is mmap'ed.

---
Changes from V2:

move 'i_mapped' to struct address_space and rename mmap_count Add inode_has_mappings() helper for FS's
Change reference to "mode" to "state"
---
Changes from V3:
Fix htmldoc error from the kbuild test robot.

Rebase cleanups

---
 fs/inode.c         |  1 +
 fs/xfs/xfs_ioctl.c |  9 +++++++++
 include/linux/fs.h |  7 +++++++
 mm/mmap.c          | 19 +++++++++++++++++--
 4 files changed, 34 insertions(+), 2 deletions(-)
fs/inode.c +1 -0
index 6e4f1cc872f2..613a045075bd 100644
@@ -372,6 +372,7 @@ static void __address_space_init_once(struct address_space *mapping)
 	INIT_LIST_HEAD(&mapping->private_list);
 	spin_lock_init(&mapping->private_lock);
 	mapping->i_mmap = RB_ROOT_CACHED;
+	atomic64_set(&mapping->mmap_count, 0);
 }
 
 void address_space_init_once(struct address_space *mapping)
fs/xfs/xfs_ioctl.c +9 -0
index 25e12ce85075..498fae2ef9f6 100644
@@ -1207,6 +1207,15 @@ xfs_ioctl_setattr_dax_invalidate(
 
 	/* lock, flush and invalidate mapping in preparation for flag change */
 	xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
+
+	/*
+	 * If there is a mapping in place we must remain in our current state.
+	 */
+	if (inode_has_mappings(inode)) {
+		error = -EBUSY;
+		goto out_unlock;
+	}
+
 	error = filemap_write_and_wait(inode->i_mapping);
 	if (error)
 		goto out_unlock;
include/linux/fs.h +7 -0
index 22cc1aa980f5..cf18a3c38562 100644
@@ -438,6 +438,7 @@ int pagecache_write_end(struct file *, struct address_space *mapping,
  * @nr_thps: Number of THPs in the pagecache (non-shmem only).
  * @i_mmap: Tree of private and shared mappings.
  * @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable.
+ * @mmap_count: The number of times this AS has been mmap'ed
  * @nrpages: Number of page entries, protected by the i_pages lock.
  * @nrexceptional: Shadow or DAX entries, protected by the i_pages lock.
  * @writeback_index: Writeback starts here.
@@ -459,6 +460,7 @@ struct address_space {
 #endif
 	struct rb_root_cached	i_mmap;
 	struct rw_semaphore	i_mmap_rwsem;
+	atomic64_t              mmap_count;
 	unsigned long		nrpages;
 	unsigned long		nrexceptional;
 	pgoff_t			writeback_index;
@@ -1937,6 +1939,11 @@ static inline void inode_aops_up_write(struct inode *inode)
 #define inode_aops_up_write(inode) do { (void)(inode); } while (0)
 #endif /* CONFIG_FS_DAX */
 
+static inline bool inode_has_mappings(struct inode *inode)
+{
+	return (atomic64_read(&inode->i_mapping->mmap_count) != 0);
+}
+
 static inline ssize_t call_read_iter(struct file *file, struct kiocb *kio,
 				     struct iov_iter *iter)
 {
mm/mmap.c +17 -2
index 7cc2562b99fd..6bb16a0996b5 100644
@@ -171,12 +171,17 @@ void unlink_file_vma(struct vm_area_struct *vma)
 static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
 {
 	struct vm_area_struct *next = vma->vm_next;
+	struct file *f = vma->vm_file;
 
 	might_sleep();
 	if (vma->vm_ops && vma->vm_ops->close)
 		vma->vm_ops->close(vma);
-	if (vma->vm_file)
-		fput(vma->vm_file);
+	if (f) {
+		struct inode *inode = file_inode(f);
+		if (inode)
+			atomic64_dec(&inode->i_mapping->mmap_count);
+		fput(f);
+	}
 	mpol_put(vma_policy(vma));
 	vm_area_free(vma);
 	return next;
@@ -1830,6 +1835,16 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 
 	vma_set_page_prot(vma);
 
+	/*
+	 * Track if there is mapping in place such that a state change
+	 * does not occur on a file which is mapped
+	 */
+	if (file) {
+		struct inode		*inode = file_inode(file);
+
+		atomic64_inc(&inode->i_mapping->mmap_count);
+	}
+
 	return addr;
 
 unmap_and_free_vma:
Signature · 1 line
2.21.0
#12 lore raw

PATCH v5 8/12 fs/xfs: Hold off aops users while changing DAX state

In reply to [email protected]

From: Ira Weiny <[email protected]>

XFS requires the use of the aops of an inode to be quiesced prior to changing the aops vector to/from the DAX vector.

Take the aops write lock while changing DAX state.

---
Changes from v4:
Open code the aops write lock
Obsolete: Clean up lock name in comments
Obsolete: Change #define: XFS_DAX_EXCL => XFS_AOPSLOCK_EXCL

Changes from v3:
Change locking function names to reflect changes in previous patches.

Changes from V2:
Change name of patch (WAS: fs/xfs: Add lock/unlock state to xfs) Remove the xfs specific lock and move to the vfs layer.
We still use XFS_LOCK_DAX_EXCL to be able to pass this flag through to the transaction code. But we no longer have a lock specific to xfs. This removes a lot of code from the XFS layer, preps us for using this in ext4, and is actually more straight forward now that all the locking requirements are better known.

Fix locking order comment
Rework for new 'state' names
(Other comments on the previous patch are not applicable with new patch as much of the code was removed in favor of the vfs level lock)

---
 fs/xfs/xfs_ioctl.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
fs/xfs/xfs_ioctl.c +11 -0
index 498fae2ef9f6..6c4d4ea3b6b6 100644
@@ -1444,7 +1444,11 @@ xfs_ioctl_setattr(
 	 * or cancel time, so need to be passed through to
 	 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
 	 * appropriately.
+	 *
+	 * Further we hold off aops users until we have completed any potential
+	 * changing of aops due to attribute changes.
 	 */
+	inode_aops_down_write(VFS_I(ip));
 	code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
 	if (code)
 		goto error_free_dquots;
@@ -1527,6 +1531,7 @@ xfs_ioctl_setattr(
 	xfs_qm_dqrele(udqp);
 	xfs_qm_dqrele(pdqp);
 
+	inode_aops_up_write(VFS_I(ip));
 	return code;
 
 error_trans_cancel:
@@ -1534,6 +1539,7 @@ xfs_ioctl_setattr(
 error_free_dquots:
 	xfs_qm_dqrele(udqp);
 	xfs_qm_dqrele(pdqp);
+	inode_aops_up_write(VFS_I(ip));
 	return code;
 }
 
@@ -1603,7 +1609,11 @@ xfs_ioc_setxflags(
 	 * or cancel time, so need to be passed through to
 	 * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
 	 * appropriately.
+	 *
+	 * Further we hold off aops users until we have completed any potential
+	 * changing of aops due to attribute changes.
 	 */
+	inode_aops_down_write(VFS_I(ip));
 	error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
 	if (error)
 		goto out_drop_write;
@@ -1630,6 +1640,7 @@ xfs_ioc_setxflags(
 	error = xfs_trans_commit(tp);
 out_drop_write:
 	mnt_drop_write_file(filp);
+	inode_aops_up_write(VFS_I(ip));
 	return error;
 }
 
Signature · 1 line
2.21.0
#13 lore raw

PATCH v5 9/12 fs/xfs: Clean up locking in dax invalidate

In reply to [email protected]

From: Ira Weiny <[email protected]>

Define a variable to hold the lock flags to ensure that the correct locks are returned or released on error.

---
 fs/xfs/xfs_ioctl.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/xfs/xfs_ioctl.c +6 -4
index 6c4d4ea3b6b6..40ae791cfb41 100644
@@ -1190,7 +1190,7 @@ xfs_ioctl_setattr_dax_invalidate(
 	int			*join_flags)
 {
 	struct inode		*inode = VFS_I(ip);
-	int			error;
+	int			error, flags;
 
 	*join_flags = 0;
 
@@ -1205,8 +1205,10 @@ xfs_ioctl_setattr_dax_invalidate(
 	if (S_ISDIR(inode->i_mode))
 		return 0;
 
+	flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
+
 	/* lock, flush and invalidate mapping in preparation for flag change */
-	xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
+	xfs_ilock(ip, flags);
 
 	/*
 	 * If there is a mapping in place we must remain in our current state.
@@ -1223,11 +1225,11 @@ xfs_ioctl_setattr_dax_invalidate(
 	if (error)
 		goto out_unlock;
 
-	*join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
+	*join_flags = flags;
 	return 0;
 
 out_unlock:
-	xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
+	xfs_iunlock(ip, flags);
 	return error;
 
 }
Signature · 1 line
2.21.0
#14 lore raw

PATCH v5 10/12 fs/xfs: Allow toggle of effective DAX flag

In reply to [email protected]

From: Ira Weiny <[email protected]>

Now that locking of the inode aops is in place we can allow a DAX state change.

Use the new xfs_inode_enable_dax() to set IS_DAX() as needed and update the aops vector after the enabled state change.

---
Changes from v4:
Adjust for open coding of the aops lock

Changes from V3:
Remove static branch stuff.
Fix bugs found by Jeff by using xfs_inode_enable_dax() Condition xfs_ioctl_setattr_dax_invalidate() on CONFIG_FS_DAX

Changes from V2:
Add in lock_dax_state_static_key static branch enabling. Rebase updates

---
 fs/xfs/xfs_inode.h |  2 ++
 fs/xfs/xfs_ioctl.c | 20 +++++++++++++++++---
 fs/xfs/xfs_iops.c  | 17 ++++++++++++-----
 3 files changed, 31 insertions(+), 8 deletions(-)
fs/xfs/xfs_inode.h +2 -0
index 492e53992fa9..7d4968915dec 100644
@@ -466,6 +466,8 @@ int	xfs_break_layouts(struct inode *inode, uint *iolock,
 /* from xfs_iops.c */
 extern void xfs_setup_inode(struct xfs_inode *ip);
 extern void xfs_setup_iops(struct xfs_inode *ip);
+extern void xfs_setup_a_ops(struct xfs_inode *ip);
+extern bool xfs_inode_enable_dax(struct xfs_inode *ip);
 
 /*
  * When setting up a newly allocated inode, we need to call
fs/xfs/xfs_ioctl.c +17 -3
index 40ae791cfb41..38e91de44a6f 100644
@@ -1123,12 +1123,11 @@ xfs_diflags_to_linux(
 		inode->i_flags |= S_NOATIME;
 	else
 		inode->i_flags &= ~S_NOATIME;
-#if 0	/* disabled until the flag switching races are sorted out */
-	if (xflags & FS_XFLAG_DAX)
+
+	if (xfs_inode_enable_dax(ip))
 		inode->i_flags |= S_DAX;
 	else
 		inode->i_flags &= ~S_DAX;
-#endif
 }
 
 static int
@@ -1183,6 +1182,7 @@ xfs_ioctl_setattr_xflags(
  * so that the cache invalidation is atomic with respect to the DAX flag
  * manipulation.
  */
+#if defined(CONFIG_FS_DAX)
 static int
 xfs_ioctl_setattr_dax_invalidate(
 	struct xfs_inode	*ip,
@@ -1233,6 +1233,16 @@ xfs_ioctl_setattr_dax_invalidate(
 	return error;
 
 }
+#else /* !CONFIG_FS_DAX */
+static int
+xfs_ioctl_setattr_dax_invalidate(
+	struct xfs_inode	*ip,
+	struct fsxattr		*fa,
+	int			*join_flags)
+{
+	return 0;
+}
+#endif
 
 /*
  * Set up the transaction structure for the setattr operation, checking that we
@@ -1524,6 +1534,8 @@ xfs_ioctl_setattr(
 	else
 		ip->i_d.di_cowextsize = 0;
 
+	xfs_setup_a_ops(ip);
+
 	code = xfs_trans_commit(tp);
 
 	/*
@@ -1639,6 +1651,8 @@ xfs_ioc_setxflags(
 		goto out_drop_write;
 	}
 
+	xfs_setup_a_ops(ip);
+
 	error = xfs_trans_commit(tp);
 out_drop_write:
 	mnt_drop_write_file(filp);
fs/xfs/xfs_iops.c +12 -5
index ff711efc5247..8f023be39705 100644
@@ -1259,7 +1259,7 @@ xfs_inode_supports_dax(
 	return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
 }
 
-STATIC bool
+bool
 xfs_inode_enable_dax(
 	struct xfs_inode *ip)
 {
@@ -1355,6 +1355,16 @@ xfs_setup_inode(
 	}
 }
 
+void xfs_setup_a_ops(struct xfs_inode *ip)
+{
+	struct inode		*inode = &ip->i_vnode;
+
+	if (IS_DAX(inode))
+		inode->i_mapping->a_ops = &xfs_dax_aops;
+	else
+		inode->i_mapping->a_ops = &xfs_address_space_operations;
+}
+
 void
 xfs_setup_iops(
 	struct xfs_inode	*ip)
@@ -1365,10 +1375,7 @@ xfs_setup_iops(
 	case S_IFREG:
 		inode->i_op = &xfs_inode_operations;
 		inode->i_fop = &xfs_file_operations;
-		if (IS_DAX(inode))
-			inode->i_mapping->a_ops = &xfs_dax_aops;
-		else
-			inode->i_mapping->a_ops = &xfs_address_space_operations;
+		xfs_setup_a_ops(ip);
 		break;
 	case S_IFDIR:
 		if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
Signature · 1 line
2.21.0
#15 lore raw

PATCH v5 11/12 fs/xfs: Remove xfs_diflags_to_linux()

In reply to [email protected]

From: Ira Weiny <[email protected]>

The functionality in xfs_diflags_to_linux() is now identical to xfs_diflags_to_iflags().

Remove xfs_diflags_to_linux() and call xfs_diflags_to_iflags() directly.

While we are here simplify xfs_diflags_to_iflags() to take just struct xfs_inode.

And use xfs_ip2xflags() to ensure future diflags are included correctly.

---
Changes from V4:
Pick up lkp build suggestion (make xfs_inode_enable_dax() static)

---
 fs/xfs/xfs_inode.h |  2 +-
 fs/xfs/xfs_ioctl.c | 32 +-------------------------------
 fs/xfs/xfs_iops.c  | 31 +++++++++++++++++++------------
 3 files changed, 21 insertions(+), 44 deletions(-)
fs/xfs/xfs_inode.h +1 -1
index 7d4968915dec..2c8c2804f88b 100644
@@ -467,7 +467,7 @@ int	xfs_break_layouts(struct inode *inode, uint *iolock,
 extern void xfs_setup_inode(struct xfs_inode *ip);
 extern void xfs_setup_iops(struct xfs_inode *ip);
 extern void xfs_setup_a_ops(struct xfs_inode *ip);
-extern bool xfs_inode_enable_dax(struct xfs_inode *ip);
+extern void xfs_diflags_to_iflags(struct xfs_inode *ip);
 
 /*
  * When setting up a newly allocated inode, we need to call
fs/xfs/xfs_ioctl.c +1 -31
index 38e91de44a6f..bd67359a3251 100644
@@ -1100,36 +1100,6 @@ xfs_flags2diflags2(
 	return di_flags2;
 }
 
-STATIC void
-xfs_diflags_to_linux(
-	struct xfs_inode	*ip)
-{
-	struct inode		*inode = VFS_I(ip);
-	unsigned int		xflags = xfs_ip2xflags(ip);
-
-	if (xflags & FS_XFLAG_IMMUTABLE)
-		inode->i_flags |= S_IMMUTABLE;
-	else
-		inode->i_flags &= ~S_IMMUTABLE;
-	if (xflags & FS_XFLAG_APPEND)
-		inode->i_flags |= S_APPEND;
-	else
-		inode->i_flags &= ~S_APPEND;
-	if (xflags & FS_XFLAG_SYNC)
-		inode->i_flags |= S_SYNC;
-	else
-		inode->i_flags &= ~S_SYNC;
-	if (xflags & FS_XFLAG_NOATIME)
-		inode->i_flags |= S_NOATIME;
-	else
-		inode->i_flags &= ~S_NOATIME;
-
-	if (xfs_inode_enable_dax(ip))
-		inode->i_flags |= S_DAX;
-	else
-		inode->i_flags &= ~S_DAX;
-}
-
 static int
 xfs_ioctl_setattr_xflags(
 	struct xfs_trans	*tp,
@@ -1167,7 +1137,7 @@ xfs_ioctl_setattr_xflags(
 	ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
 	ip->i_d.di_flags2 = di_flags2;
 
-	xfs_diflags_to_linux(ip);
+	xfs_diflags_to_iflags(ip);
 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
 	XFS_STATS_INC(mp, xs_ig_attrchg);
fs/xfs/xfs_iops.c +19 -12
index 8f023be39705..c63cf0b73b75 100644
@@ -1259,7 +1259,7 @@ xfs_inode_supports_dax(
 	return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
 }
 
-bool
+static bool
 xfs_inode_enable_dax(
 	struct xfs_inode *ip)
 {
@@ -1273,26 +1273,33 @@ xfs_inode_enable_dax(
 	return false;
 }
 
-STATIC void
+void
 xfs_diflags_to_iflags(
-	struct inode		*inode,
 	struct xfs_inode	*ip)
 {
-	uint16_t		flags = ip->i_d.di_flags;
-
-	inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC |
-			    S_NOATIME | S_DAX);
+	struct inode		*inode = VFS_I(ip);
+	uint16_t		diflags = xfs_ip2xflags(ip);
 
-	if (flags & XFS_DIFLAG_IMMUTABLE)
+	if (diflags & FS_XFLAG_IMMUTABLE)
 		inode->i_flags |= S_IMMUTABLE;
-	if (flags & XFS_DIFLAG_APPEND)
+	else
+		inode->i_flags &= ~S_IMMUTABLE;
+	if (diflags & FS_XFLAG_APPEND)
 		inode->i_flags |= S_APPEND;
-	if (flags & XFS_DIFLAG_SYNC)
+	else
+		inode->i_flags &= ~S_APPEND;
+	if (diflags & FS_XFLAG_SYNC)
 		inode->i_flags |= S_SYNC;
-	if (flags & XFS_DIFLAG_NOATIME)
+	else
+		inode->i_flags &= ~S_SYNC;
+	if (diflags & FS_XFLAG_NOATIME)
 		inode->i_flags |= S_NOATIME;
+	else
+		inode->i_flags &= ~S_NOATIME;
 	if (xfs_inode_enable_dax(ip))
 		inode->i_flags |= S_DAX;
+	else
+		inode->i_flags &= ~S_DAX;
 }
 
 /*
@@ -1321,7 +1328,7 @@ xfs_setup_inode(
 	inode->i_gid    = xfs_gid_to_kgid(ip->i_d.di_gid);
 
 	i_size_write(inode, ip->i_d.di_size);
-	xfs_diflags_to_iflags(inode, ip);
+	xfs_diflags_to_iflags(ip);
 
 	if (S_ISDIR(inode->i_mode)) {
 		/*
Signature · 1 line
2.21.0
#16 lore raw

PATCH v5 12/12 Documentation/dax: Update Usage section

In reply to [email protected]

From: Ira Weiny <[email protected]>

Update the Usage section to reflect the new individual dax selection functionality.

---
 Documentation/filesystems/dax.txt | 84 ++++++++++++++++++++++++++++++-
 1 file changed, 82 insertions(+), 2 deletions(-)
Documentation/filesystems/dax.txt +82 -2
index 679729442fd2..32e37c550f76 100644
@@ -20,8 +20,88 @@ Usage
 If you have a block device which supports DAX, you can make a filesystem
 on it as usual.  The DAX code currently only supports files with a block
 size equal to your kernel's PAGE_SIZE, so you may need to specify a block
-size when creating the filesystem.  When mounting it, use the "-o dax"
-option on the command line or add 'dax' to the options in /etc/fstab.
+size when creating the filesystem.
+
+Enabling DAX on an individual file basis (XFS)
+----------------------------------------------
+
+There are 2 per file dax flags.  One is a physical configuration setting and
+the other a currently enabled state.
+
+The physical configuration setting is maintained on individual file and
+directory inodes.  It is preserved within the file system.  This 'physical'
+config setting can be set using an ioctl and/or an application such as "xfs_io
+-c 'chattr [-+]x'".  Files and directories automatically inherit their physical
+dax setting from their parent directory when created.  Therefore, setting the
+physical dax setting at directory creation time can be used to set a default
+behavior for that sub-tree.  Doing so on the root directory acts to set a
+default for the entire file system.
+
+To clarify inheritance here are 3 examples:
+
+Example A:
+
+mkdir -p a/b/c
+xfs_io 'chattr +x' a
+mkdir a/b/c/d
+mkdir a/e
+
+	dax: a,e
+	no dax: b,c,d
+
+Example B:
+
+mkdir a
+xfs_io 'chattr +x' a
+mkdir -p a/b/c/d
+
+	dax: a,b,c,d
+	no dax:
+
+Example C:
+
+mkdir -p a/b/c
+xfs_io 'chattr +x' c
+mkdir a/b/c/d
+
+	dax: c,d
+	no dax: a,b
+
+
+The current inode enabled state is set when a file inode is loaded and it is
+determined that the underlying media supports dax.
+
+statx can be used to query the file's current enabled state.  NOTE that a
+directory will never be operating in a dax state.  Therefore, the dax config
+state must be queried to see what config state a file or sub-directory will
+inherit from a directory.
+
+NOTE: Setting a file or directory's config state with xfs_io is possible even
+if the underlying media does not support dax.
+
+
+Enabling dax on a file system wide basis ('-o dax' mount option)
+----------------------------------------------------------------
+
+The physical dax configuration of all files can be overridden using a mount
+option.  In summary:
+
+	(physical flag || mount option) && capable device == dax in effect
+	(  <xfs_io>    ||  <'-o dax'> ) && capable device == <statx dax true>
+
+To enable the mount override, use "-o dax" on the command line or add
+'dax' to the options in /etc/fstab
+
+Using the mount option does not change the physical configured state of
+individual files.  Therefore, remounting _without_ the mount option will allow
+the file system to set file's enabled state directly based on their config
+setting.
+
+NOTE: Setting a file or directory's physical config state is possible while the
+file system is mounted with the dax override.  However, the file's enabled
+state will continue to be overridden and "dax enabled" until the mount option
+is removed and a remount performed.  At that point the file's physical config
+state dictates the enabled state.
 
 
 Implementation Tips for Block Driver Writers
Signature · 1 line
2.21.0
#17 lore raw
In reply to [email protected]

FYI, I still will fully NAK any series that adds additional locks and thus atomic instructions to basically every fs call, and grows the inode by a rw_semaphore plus and atomic64_t. I also think the whole idea of switching operation vectors at runtime is fatally flawed and we should never add such code, nevermind just for a fringe usecase of a fringe feature.

Quoted 134 lines · On Wed, Feb 26, 2020 at 09:24:30PM -0800, [email protected] wrote:

From: Ira Weiny <[email protected]>

Changes from V4:
* Open code the aops lock rather than add it to the xfs_ilock() subsystem (Darrick's comments were obsoleted by this change)
* Fix lkp build suggestions and bugs

Changes from V3:
* Remove global locking... :-D
* put back per inode locking and remove pre-mature optimizations
* Fix issues with Directories having IS_DAX() set
* Fix kernel crash issues reported by Jeff
* Add some clean up patches
* Consolidate diflags to iflags functions
* Update/add documentation
* Reorder/rename patches quite a bit

Changes from V2:

* Move i_dax_sem to be a global percpu_rw_sem rather than per inode Internal discussions with Dan determined this would be easier, just as performant, and slightly less overhead that having it in the SB as suggested by Jan
* Fix locking order in comments and throughout code
* Change "mode" to "state" throughout commits
* Add CONFIG_FS_DAX wrapper to disable inode_[un]lock_state() when not configured
* Add static branch for which is activated by a device which supports DAX in XFS
* Change "lock/unlock" to up/down read/write as appropriate Previous names were over simplified
* Update comments/documentation

* Remove the xfs specific lock to the vfs (global) layer.
* Fix i_dax_sem locking order and comments

* Move 'i_mapped' count from struct inode to struct address_space and rename it to mmap_count
* Add inode_has_mappings() call

* Fix build issues
* Clean up syntax spacing and minor issues
* Update man page text for STATX_ATTR_DAX
* Add reviewed-by's
* Rebase to 5.6

Rename patch:
from: fs/xfs: Add lock/unlock state to xfs
to: fs/xfs: Add write DAX lock to xfs layer
Add patch:
fs/xfs: Clarify lockdep dependency for xfs_isilocked()
Drop patch:
fs/xfs: Fix truncate up

At LSF/MM'19 [1] [2] we discussed applications that overestimate memory consumption due to their inability to detect whether the kernel will instantiate page cache for a file, and cases where a global dax enable via a mount option is too coarse.

The following patch series enables selecting the use of DAX on individual files and/or directories on xfs, and lays some groundwork to do so in ext4. In this scheme the dax mount option can be omitted to allow the per-file property to take effect.

The insight at LSF/MM was to separate the per-mount or per-file "physical" capability switch from an "effective" attribute for the file.

At LSF/MM we discussed the difficulties of switching the DAX state of a file with active mappings / page cache. It was thought the races could be avoided by limiting DAX state flips to 0-length files.

However, this turns out to not be true.[3] This is because address space operations (a_ops) may be in use at any time the inode is referenced and users have expressed a desire to be able to change the DAX state on a file with data in it. For those reasons this patch set allows changing the DAX state flag on a file as long as it is not current mapped.

Details of when and how DAX state can be changed on a file is included in a documentation patch.

It should be noted that the physical DAX flag inheritance is not shown in this patch set as it was maintained from previous work on XFS. The physical DAX flag and it's inheritance will need to be added to other file systems for user control.

As submitted this works on real hardware testing.

[1] https://lwn.net/Articles/787973/
[2] https://lwn.net/Articles/787233/
[3] https://lkml.org/lkml/2019/10/20/96
[4] https://patchwork.kernel.org/patch/11310511/

To: [email protected]
Cc: Alexander Viro <[email protected]>
Cc: "Darrick J. Wong" <[email protected]>
Cc: Dan Williams <[email protected]>
Cc: Dave Chinner <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: "Theodore Y. Ts'o" <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

Ira Weiny (12):
fs/xfs: Remove unnecessary initialization of i_rwsem
fs: Remove unneeded IS_DAX() check
fs/stat: Define DAX statx attribute
fs/xfs: Isolate the physical DAX flag from enabled fs/xfs: Create function xfs_inode_enable_dax()
fs: Add locking for a dynamic address space operations state
fs: Prevent DAX state change if file is mmap'ed fs/xfs: Hold off aops users while changing DAX state fs/xfs: Clean up locking in dax invalidate
fs/xfs: Allow toggle of effective DAX flag
fs/xfs: Remove xfs_diflags_to_linux() Documentation/dax: Update Usage section

 Documentation/filesystems/dax.txt | 84 +++++++++++++++++++++++++-
 Documentation/filesystems/vfs.rst | 16 +++++
 fs/attr.c                         |  1 +
 fs/inode.c                        | 16 ++++-
 fs/iomap/buffered-io.c            |  1 +
 fs/open.c                         |  4 ++
 fs/stat.c                         |  5 ++
 fs/xfs/xfs_icache.c               |  5 +-
 fs/xfs/xfs_inode.h                |  2 +
 fs/xfs/xfs_ioctl.c                | 98 +++++++++++++++----------------
 fs/xfs/xfs_iops.c                 | 69 +++++++++++++++-------
 include/linux/fs.h                | 73 ++++++++++++++++++++++-
Quoted 11 lines
 include/uapi/linux/stat.h         |  1 +
 mm/fadvise.c                      |  7 ++-
 mm/filemap.c                      |  4 ++
 mm/huge_memory.c                  |  1 +
 mm/khugepaged.c                   |  2 +
 mm/mmap.c                         | 19 +++++-
 mm/util.c                         |  9 ++-
 19 files changed, 328 insertions(+), 89 deletions(-)
Signature · 1 line
2.21.0

---end quoted text---

author
#18 lore raw
Quoted 6 lines · Christoph Hellwig

FYI, I still will fully NAK any series that adds additional locks and thus atomic instructions to basically every fs call, and grows the inode by a rw_semaphore plus and atomic64_t. I also think the whole idea of switching operation vectors at runtime is fatally flawed and we should never add such code, nevermind just for a fringe usecase of a fringe feature.

Being new to this area of the kernel I'm not clear on the history...

It was my understanding that the per-file flag support was a requirement to removing the experimental designation from DAX. Is this still the case?

Ira

Quoted 148 lines · On Wed, Feb 26, 2020 at 09:24:30PM -0800, [email protected] wrote:
Quoted 134 lines

From: Ira Weiny <[email protected]>

Changes from V4:
* Open code the aops lock rather than add it to the xfs_ilock() subsystem (Darrick's comments were obsoleted by this change)
* Fix lkp build suggestions and bugs

Changes from V3:
* Remove global locking... :-D
* put back per inode locking and remove pre-mature optimizations
* Fix issues with Directories having IS_DAX() set
* Fix kernel crash issues reported by Jeff
* Add some clean up patches
* Consolidate diflags to iflags functions
* Update/add documentation
* Reorder/rename patches quite a bit

Changes from V2:

* Move i_dax_sem to be a global percpu_rw_sem rather than per inode Internal discussions with Dan determined this would be easier, just as performant, and slightly less overhead that having it in the SB as suggested by Jan
* Fix locking order in comments and throughout code
* Change "mode" to "state" throughout commits
* Add CONFIG_FS_DAX wrapper to disable inode_[un]lock_state() when not configured
* Add static branch for which is activated by a device which supports DAX in XFS
* Change "lock/unlock" to up/down read/write as appropriate Previous names were over simplified
* Update comments/documentation

* Remove the xfs specific lock to the vfs (global) layer.
* Fix i_dax_sem locking order and comments

* Move 'i_mapped' count from struct inode to struct address_space and rename it to mmap_count
* Add inode_has_mappings() call

* Fix build issues
* Clean up syntax spacing and minor issues
* Update man page text for STATX_ATTR_DAX
* Add reviewed-by's
* Rebase to 5.6

Rename patch:
from: fs/xfs: Add lock/unlock state to xfs
to: fs/xfs: Add write DAX lock to xfs layer
Add patch:
fs/xfs: Clarify lockdep dependency for xfs_isilocked()
Drop patch:
fs/xfs: Fix truncate up

At LSF/MM'19 [1] [2] we discussed applications that overestimate memory consumption due to their inability to detect whether the kernel will instantiate page cache for a file, and cases where a global dax enable via a mount option is too coarse.

The following patch series enables selecting the use of DAX on individual files and/or directories on xfs, and lays some groundwork to do so in ext4. In this scheme the dax mount option can be omitted to allow the per-file property to take effect.

The insight at LSF/MM was to separate the per-mount or per-file "physical" capability switch from an "effective" attribute for the file.

At LSF/MM we discussed the difficulties of switching the DAX state of a file with active mappings / page cache. It was thought the races could be avoided by limiting DAX state flips to 0-length files.

However, this turns out to not be true.[3] This is because address space operations (a_ops) may be in use at any time the inode is referenced and users have expressed a desire to be able to change the DAX state on a file with data in it. For those reasons this patch set allows changing the DAX state flag on a file as long as it is not current mapped.

Details of when and how DAX state can be changed on a file is included in a documentation patch.

It should be noted that the physical DAX flag inheritance is not shown in this patch set as it was maintained from previous work on XFS. The physical DAX flag and it's inheritance will need to be added to other file systems for user control.

As submitted this works on real hardware testing.

[1] https://lwn.net/Articles/787973/
[2] https://lwn.net/Articles/787233/
[3] https://lkml.org/lkml/2019/10/20/96
[4] https://patchwork.kernel.org/patch/11310511/

To: [email protected]
Cc: Alexander Viro <[email protected]>
Cc: "Darrick J. Wong" <[email protected]>
Cc: Dan Williams <[email protected]>
Cc: Dave Chinner <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: "Theodore Y. Ts'o" <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

Ira Weiny (12):
fs/xfs: Remove unnecessary initialization of i_rwsem
fs: Remove unneeded IS_DAX() check
fs/stat: Define DAX statx attribute
fs/xfs: Isolate the physical DAX flag from enabled fs/xfs: Create function xfs_inode_enable_dax()
fs: Add locking for a dynamic address space operations state
fs: Prevent DAX state change if file is mmap'ed fs/xfs: Hold off aops users while changing DAX state fs/xfs: Clean up locking in dax invalidate
fs/xfs: Allow toggle of effective DAX flag
fs/xfs: Remove xfs_diflags_to_linux() Documentation/dax: Update Usage section

 Documentation/filesystems/dax.txt | 84 +++++++++++++++++++++++++-
 Documentation/filesystems/vfs.rst | 16 +++++
 fs/attr.c                         |  1 +
 fs/inode.c                        | 16 ++++-
 fs/iomap/buffered-io.c            |  1 +
 fs/open.c                         |  4 ++
 fs/stat.c                         |  5 ++
 fs/xfs/xfs_icache.c               |  5 +-
 fs/xfs/xfs_inode.h                |  2 +
 fs/xfs/xfs_ioctl.c                | 98 +++++++++++++++----------------
 fs/xfs/xfs_iops.c                 | 69 +++++++++++++++-------
 include/linux/fs.h                | 73 ++++++++++++++++++++++-
Quoted 11 lines
 include/uapi/linux/stat.h         |  1 +
 mm/fadvise.c                      |  7 ++-
 mm/filemap.c                      |  4 ++
 mm/huge_memory.c                  |  1 +
 mm/khugepaged.c                   |  2 +
 mm/mmap.c                         | 19 +++++-
 mm/util.c                         |  9 ++-
 19 files changed, 328 insertions(+), 89 deletions(-)
Signature · 1 line
2.21.0

---end quoted text---

#19 lore raw
Quoted 9 lines · Ira Weiny
Quoted 6 lines · Christoph Hellwig

FYI, I still will fully NAK any series that adds additional locks and thus atomic instructions to basically every fs call, and grows the inode by a rw_semaphore plus and atomic64_t. I also think the whole idea of switching operation vectors at runtime is fatally flawed and we should never add such code, nevermind just for a fringe usecase of a fringe feature.

Being new to this area of the kernel I'm not clear on the history...

I /think/ the TLDR version in no particular order is:

- Some people expressed interest in being able to control page cache vs. direct access on pmem hardware at a higher granularity than the entire fs.

- Dave Chinner(?) added the per-inode flag intending it to be the sign that would flip on DAX.

- Someone (I forget who) made it a mount option that would enable it for all files regardless of inode flags and whatnot.

- Eric Sandeen(?) complained that the behavior of the dax inode flag was weird, particularly the part where you set the iflag and at some point if and when the inode gets reclaimed and then reconstituted then the change will finally take place.

- Christoph Hellwig objected on various grounds (the kernel is responsible for selecting the most appropriate hardware abstraction for the usage pattern; a binary flag doesn't capture enough detail for potential future pmem hardware; and now additional locking overhead).

- There's been (I hope) a long term understanding that the mount option will go away eventually, and not after we remove the EXPERIMENTAL tags.

(FWIW I tend to agree with Eric and Christoph, but I also thought it would be useful at least to see what changeable file operations would be like; if there were other users who had already implemented it; and how much of an apetite there was for revoke().)

Hopefully I summarized that more or less accurately...

Quoted 2 lines

It was my understanding that the per-file flag support was a requirement to removing the experimental designation from DAX. Is this still the case?

Nailing down the meaning of the per-file dax flag is/was the requirement, even if we kill it off entirely in the end.

Given Christoph's veto threat, I suppose that leaves the following options?

1) Leave the inode flag (FS_XFLAG_DAX) as it is, and export the S_DAX status via statx. Document that changes to FS_XFLAG_DAX do not take effect immediately and that one must check statx to find out the real mode. If we choose this, I would also deprecate the dax mount option; send in my mkfs.xfs patch to make it so that you can set FS_XFLAG_DAX on all files at mkfs time; and we can finally lay this whole thing to rest. This is the closest to what we have today.

2) Withdraw FS_XFLAG_DAX entirely, and let the kernel choose based on usage patterns, hardware heuristics, or spiteful arbitrariness.

Can we please pick (1) and just be done with this? I want to move on.

--D

There are still other things that need to be ironed out WRT pmem:

a) reflink and page/pfn/whatever sharing -- fix the mm or (ab)use the xfs buffer cache, or something worse?

b) getting our stories straight on how to clear poison, and whether or not we can come up with a better story for ZERO_FILE_RANGE on pmem. In the ideal world I'd love to see Z_F_R actually memset(0) the pmem and clear poison, at least if the file->pmem mappings were contiguous.

c) wiring up xfs to hwpoison, or wiring up hwpoison to xfs, or otherwise figuring out how to get storage to tell xfs that it lost something so that maybe xfs can fix it quickly

Quoted 150 lines

Ira

Quoted 148 lines · On Wed, Feb 26, 2020 at 09:24:30PM -0800, [email protected] wrote:
Quoted 134 lines

From: Ira Weiny <[email protected]>

Changes from V4:
* Open code the aops lock rather than add it to the xfs_ilock() subsystem (Darrick's comments were obsoleted by this change)
* Fix lkp build suggestions and bugs

Changes from V3:
* Remove global locking... :-D
* put back per inode locking and remove pre-mature optimizations
* Fix issues with Directories having IS_DAX() set
* Fix kernel crash issues reported by Jeff
* Add some clean up patches
* Consolidate diflags to iflags functions
* Update/add documentation
* Reorder/rename patches quite a bit

Changes from V2:

* Move i_dax_sem to be a global percpu_rw_sem rather than per inode Internal discussions with Dan determined this would be easier, just as performant, and slightly less overhead that having it in the SB as suggested by Jan
* Fix locking order in comments and throughout code
* Change "mode" to "state" throughout commits
* Add CONFIG_FS_DAX wrapper to disable inode_[un]lock_state() when not configured
* Add static branch for which is activated by a device which supports DAX in XFS
* Change "lock/unlock" to up/down read/write as appropriate Previous names were over simplified
* Update comments/documentation

* Remove the xfs specific lock to the vfs (global) layer.
* Fix i_dax_sem locking order and comments

* Move 'i_mapped' count from struct inode to struct address_space and rename it to mmap_count
* Add inode_has_mappings() call

* Fix build issues
* Clean up syntax spacing and minor issues
* Update man page text for STATX_ATTR_DAX
* Add reviewed-by's
* Rebase to 5.6

Rename patch:
from: fs/xfs: Add lock/unlock state to xfs
to: fs/xfs: Add write DAX lock to xfs layer
Add patch:
fs/xfs: Clarify lockdep dependency for xfs_isilocked()
Drop patch:
fs/xfs: Fix truncate up

At LSF/MM'19 [1] [2] we discussed applications that overestimate memory consumption due to their inability to detect whether the kernel will instantiate page cache for a file, and cases where a global dax enable via a mount option is too coarse.

The following patch series enables selecting the use of DAX on individual files and/or directories on xfs, and lays some groundwork to do so in ext4. In this scheme the dax mount option can be omitted to allow the per-file property to take effect.

The insight at LSF/MM was to separate the per-mount or per-file "physical" capability switch from an "effective" attribute for the file.

At LSF/MM we discussed the difficulties of switching the DAX state of a file with active mappings / page cache. It was thought the races could be avoided by limiting DAX state flips to 0-length files.

However, this turns out to not be true.[3] This is because address space operations (a_ops) may be in use at any time the inode is referenced and users have expressed a desire to be able to change the DAX state on a file with data in it. For those reasons this patch set allows changing the DAX state flag on a file as long as it is not current mapped.

Details of when and how DAX state can be changed on a file is included in a documentation patch.

It should be noted that the physical DAX flag inheritance is not shown in this patch set as it was maintained from previous work on XFS. The physical DAX flag and it's inheritance will need to be added to other file systems for user control.

As submitted this works on real hardware testing.

[1] https://lwn.net/Articles/787973/
[2] https://lwn.net/Articles/787233/
[3] https://lkml.org/lkml/2019/10/20/96
[4] https://patchwork.kernel.org/patch/11310511/

To: [email protected]
Cc: Alexander Viro <[email protected]>
Cc: "Darrick J. Wong" <[email protected]>
Cc: Dan Williams <[email protected]>
Cc: Dave Chinner <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: "Theodore Y. Ts'o" <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

Ira Weiny (12):
fs/xfs: Remove unnecessary initialization of i_rwsem
fs: Remove unneeded IS_DAX() check
fs/stat: Define DAX statx attribute
fs/xfs: Isolate the physical DAX flag from enabled fs/xfs: Create function xfs_inode_enable_dax()
fs: Add locking for a dynamic address space operations state
fs: Prevent DAX state change if file is mmap'ed fs/xfs: Hold off aops users while changing DAX state fs/xfs: Clean up locking in dax invalidate
fs/xfs: Allow toggle of effective DAX flag
fs/xfs: Remove xfs_diflags_to_linux() Documentation/dax: Update Usage section

 Documentation/filesystems/dax.txt | 84 +++++++++++++++++++++++++-
 Documentation/filesystems/vfs.rst | 16 +++++
 fs/attr.c                         |  1 +
 fs/inode.c                        | 16 ++++-
 fs/iomap/buffered-io.c            |  1 +
 fs/open.c                         |  4 ++
 fs/stat.c                         |  5 ++
 fs/xfs/xfs_icache.c               |  5 +-
 fs/xfs/xfs_inode.h                |  2 +
 fs/xfs/xfs_ioctl.c                | 98 +++++++++++++++----------------
 fs/xfs/xfs_iops.c                 | 69 +++++++++++++++-------
 include/linux/fs.h                | 73 ++++++++++++++++++++++-
Quoted 11 lines
 include/uapi/linux/stat.h         |  1 +
 mm/fadvise.c                      |  7 ++-
 mm/filemap.c                      |  4 ++
 mm/huge_memory.c                  |  1 +
 mm/khugepaged.c                   |  2 +
 mm/mmap.c                         | 19 +++++-
 mm/util.c                         |  9 ++-
 19 files changed, 328 insertions(+), 89 deletions(-)
Signature · 1 line
2.21.0

---end quoted text---

#20 lore raw
Quoted 10 lines · Darrick J. Wong

1) Leave the inode flag (FS_XFLAG_DAX) as it is, and export the S_DAX status via statx. Document that changes to FS_XFLAG_DAX do not take effect immediately and that one must check statx to find out the real mode. If we choose this, I would also deprecate the dax mount option; send in my mkfs.xfs patch to make it so that you can set FS_XFLAG_DAX on all files at mkfs time; and we can finally lay this whole thing to rest. This is the closest to what we have today.

2) Withdraw FS_XFLAG_DAX entirely, and let the kernel choose based on usage patterns, hardware heuristics, or spiteful arbitrariness.

3) Only allow changing FS_XFLAG_DAX on directories or files that do not have blocks allocated to them yet, and side step all the hard problems.

Which of course still side steps the hard question of what it actually is supposed to mean..

#21 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Darrick J. Wong

1) Leave the inode flag (FS_XFLAG_DAX) as it is, and export the S_DAX status via statx. Document that changes to FS_XFLAG_DAX do not take effect immediately and that one must check statx to find out the real mode. If we choose this, I would also deprecate the dax mount option; send in my mkfs.xfs patch to make it so that you can set FS_XFLAG_DAX on all files at mkfs time; and we can finally lay this whole thing to rest. This is the closest to what we have today.

Quoted 6 lines
Quoted 2 lines

2) Withdraw FS_XFLAG_DAX entirely, and let the kernel choose based on usage patterns, hardware heuristics, or spiteful arbitrariness.

3) Only allow changing FS_XFLAG_DAX on directories or files that do not have blocks allocated to them yet, and side step all the hard problems.

This sounds reasonable to me.

As for deprecating the mount option, I think at a minimum it needs to continue be accepted as an option even if it is ignored to not break existing setups. We're currently going through the prolonged flag day of people discovering that if they update xfsprogs they need to specify "-m reflink=0" to mkfs.xfs. That pain seems to have only been a road bump not a showstopper based on the bug reports I've seen. If anything it has added helpful pressure towards getting reflink support bumped up in the priority. Hopefully the xfs position that the dax mount option can be ignored makes it possible to implement the same policy on ext4, and we can just move on...

Quoted 2 lines

Which of course still side steps the hard question of what it actually is supposed to mean..

If we have statx to indicate the effective dax-state that addresses the pain for applications that want to account for dax in their page cache pressure estimates, and lets FS_XFLAG_DAX not need to specify precise semantics.

maintainer
#22 lore raw
Quoted 18 lines · Dan Williams
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Darrick J. Wong

1) Leave the inode flag (FS_XFLAG_DAX) as it is, and export the S_DAX status via statx. Document that changes to FS_XFLAG_DAX do not take effect immediately and that one must check statx to find out the real mode. If we choose this, I would also deprecate the dax mount option; send in my mkfs.xfs patch to make it so that you can set FS_XFLAG_DAX on all files at mkfs time; and we can finally lay this whole thing to rest. This is the closest to what we have today.

Quoted 6 lines
Quoted 2 lines

2) Withdraw FS_XFLAG_DAX entirely, and let the kernel choose based on usage patterns, hardware heuristics, or spiteful arbitrariness.

3) Only allow changing FS_XFLAG_DAX on directories or files that do not have blocks allocated to them yet, and side step all the hard problems.

Quoted 5 lines

This sounds reasonable to me.

As for deprecating the mount option, I think at a minimum it needs to continue be accepted as an option even if it is ignored to not break existing setups.

Agreed. But that's how we usually deprecate mount options. Also I'd say that statx() support for reporting DAX state and some education of programmers using DAX is required before we deprecate the mount option since currently applications check 'dax' mount option to determine how much memory they need to set aside for page cache before they consume everything else on the machine...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#23 lore raw
Quoted 6 lines · Jan Kara
Quoted 5 lines

This sounds reasonable to me.

As for deprecating the mount option, I think at a minimum it needs to continue be accepted as an option even if it is ignored to not break existing setups.

Quoted 6 lines

Agreed. But that's how we usually deprecate mount options. Also I'd say that statx() support for reporting DAX state and some education of programmers using DAX is required before we deprecate the mount option since currently applications check 'dax' mount option to determine how much memory they need to set aside for page cache before they consume everything else on the machine...

I don't even think we should deprecate it. It isn't painful to maintain and actually useful for testing. Instead we should expand it into a tristate:

dax=off
dax=flag
dax=always

where the existing "dax" option maps to "dax=always" and nodax maps to "dax=off". and dax=flag becomes the default for DAX capable devices.

#24 lore raw
Quoted 18 lines · Christoph Hellwig
Quoted 6 lines · Jan Kara
Quoted 5 lines

This sounds reasonable to me.

As for deprecating the mount option, I think at a minimum it needs to continue be accepted as an option even if it is ignored to not break existing setups.

Quoted 6 lines

Agreed. But that's how we usually deprecate mount options. Also I'd say that statx() support for reporting DAX state and some education of programmers using DAX is required before we deprecate the mount option since currently applications check 'dax' mount option to determine how much memory they need to set aside for page cache before they consume everything else on the machine...

I don't even think we should deprecate it. It isn't painful to maintain and actually useful for testing. Instead we should expand it into a tristate:

Quoted 6 lines

dax=off
dax=flag
dax=always

where the existing "dax" option maps to "dax=always" and nodax maps to "dax=off". and dax=flag becomes the default for DAX capable devices.

That works for me. In summary:

- Applications must call statx to discover the current S_DAX state.

- There exists an advisory file inode flag FS_XFLAG_DAX that can be changed on files that have no blocks allocated to them. Changing this flag does not necessarily change the S_DAX state immediately but programs can query the S_DAX state via statx.

If FS_XFLAG_DAX is set and the fs is on pmem then it will always enable S_DAX at inode load time; if FS_XFLAG_DAX is not set, it will never enable S_DAX. Unless overridden...

- There exists a dax= mount option. dax=off means "never set S_DAX, ignore FS_XFLAG_DAX"; dax=always means "always set S_DAX (at least on pmem), ignore FS_XFLAG_DAX"; and dax=iflag means "follow FS_XFLAG_DAX" and is the default. "dax" by itself means "dax=always". "nodax" means "dax=off".

- There exists an advisory directory inode flag FS_XFLAG_DAX that can be changed at any time. The flag state is copied into any files or subdirectories created within that directory. If programs require that file access runs in S_DAX mode, they'll have to create those files themselves inside a directory with FS_XFLAG_DAX set, or mount the fs with dax=always.

Ok? Let's please get this part finished for 5.8, then we can get back to arguing about fs-rmap and reflink and dax and whatnot.

--D

maintainer
#25 lore raw
Quoted 26 lines · Darrick J. Wong
Quoted 18 lines · Christoph Hellwig
Quoted 6 lines · Jan Kara
Quoted 5 lines

This sounds reasonable to me.

As for deprecating the mount option, I think at a minimum it needs to continue be accepted as an option even if it is ignored to not break existing setups.

Quoted 6 lines

Agreed. But that's how we usually deprecate mount options. Also I'd say that statx() support for reporting DAX state and some education of programmers using DAX is required before we deprecate the mount option since currently applications check 'dax' mount option to determine how much memory they need to set aside for page cache before they consume everything else on the machine...

I don't even think we should deprecate it. It isn't painful to maintain and actually useful for testing. Instead we should expand it into a tristate:

Quoted 6 lines

dax=off
dax=flag
dax=always

where the existing "dax" option maps to "dax=always" and nodax maps to "dax=off". and dax=flag becomes the default for DAX capable devices.

Quoted 8 lines

That works for me. In summary:

- Applications must call statx to discover the current S_DAX state.

- There exists an advisory file inode flag FS_XFLAG_DAX that can be changed on files that have no blocks allocated to them. Changing this flag does not necessarily change the S_DAX state immediately but programs can query the S_DAX state via statx.

I generally like the proposal but I think the fact that toggling FS_XFLAG_DAX will not have immediate effect on S_DAX will cause quite some confusion and ultimately bug reports. I'm thinking whether we could somehow improve this... For example an ioctl that would try to make set inode flags effective by evicting the inode (and returning EBUSY if the eviction is impossible for some reason)?

Honza

Quoted 22 lines

If FS_XFLAG_DAX is set and the fs is on pmem then it will always enable S_DAX at inode load time; if FS_XFLAG_DAX is not set, it will never enable S_DAX. Unless overridden...

- There exists a dax= mount option. dax=off means "never set S_DAX, ignore FS_XFLAG_DAX"; dax=always means "always set S_DAX (at least on pmem), ignore FS_XFLAG_DAX"; and dax=iflag means "follow FS_XFLAG_DAX" and is the default. "dax" by itself means "dax=always". "nodax" means "dax=off".

- There exists an advisory directory inode flag FS_XFLAG_DAX that can be changed at any time. The flag state is copied into any files or subdirectories created within that directory. If programs require that file access runs in S_DAX mode, they'll have to create those files themselves inside a directory with FS_XFLAG_DAX set, or mount the fs with dax=always.

Ok? Let's please get this part finished for 5.8, then we can get back to arguing about fs-rmap and reflink and dax and whatnot.

--D

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#26 lore raw
Quoted 7 lines · Jan Kara
Quoted 6 lines

- Applications must call statx to discover the current S_DAX state.

- There exists an advisory file inode flag FS_XFLAG_DAX that can be changed on files that have no blocks allocated to them. Changing this flag does not necessarily change the S_DAX state immediately but programs can query the S_DAX state via statx.

Quoted 6 lines

I generally like the proposal but I think the fact that toggling FS_XFLAG_DAX will not have immediate effect on S_DAX will cause quite some confusion and ultimately bug reports. I'm thinking whether we could somehow improve this... For example an ioctl that would try to make set inode flags effective by evicting the inode (and returning EBUSY if the eviction is impossible for some reason)?

I'd just return an error for that case, don't play silly games like evicting the inode.

author
#27 lore raw
Quoted 15 lines · Christoph Hellwig
Quoted 7 lines · Jan Kara
Quoted 6 lines

- Applications must call statx to discover the current S_DAX state.

- There exists an advisory file inode flag FS_XFLAG_DAX that can be changed on files that have no blocks allocated to them. Changing this flag does not necessarily change the S_DAX state immediately but programs can query the S_DAX state via statx.

Quoted 6 lines

I generally like the proposal but I think the fact that toggling FS_XFLAG_DAX will not have immediate effect on S_DAX will cause quite some confusion and ultimately bug reports. I'm thinking whether we could somehow improve this... For example an ioctl that would try to make set inode flags effective by evicting the inode (and returning EBUSY if the eviction is impossible for some reason)?

Quoted 2 lines

I'd just return an error for that case, don't play silly games like evicting the inode.

I think I agree with Christoph here. But I want to clarify. I was heading in a direction of failing the ioctl completely. But we could have the flag change with an appropriate error which could let the user know the change has been delayed.

But I don't immediately see what error code is appropriate for such an indication. Candidates I can envision:

EAGAIN
ERESTART
EUSERS
EINPROGRESS

None are perfect but I'm leaning toward EINPROGRESS.

Ira

#28 lore raw
Quoted 11 lines · Ira Weiny
Quoted 2 lines

I'd just return an error for that case, don't play silly games like evicting the inode.

I think I agree with Christoph here. But I want to clarify. I was heading in a direction of failing the ioctl completely. But we could have the flag change with an appropriate error which could let the user know the change has been delayed.

But I don't immediately see what error code is appropriate for such an indication. Candidates I can envision:

Quoted 6 lines

EAGAIN
ERESTART
EUSERS
EINPROGRESS

None are perfect but I'm leaning toward EINPROGRESS.

I really, really dislike that idea. The whole point of not forcing evictions is to make it clear - no this inode is "busy" you can't do that. A reasonably smart application can try to evict itself.

But returning an error and doing a lazy change anyway is straight from the playbook for arcane and confusing API designs.

author
#29 lore raw
Quoted 17 lines · Christoph Hellwig
Quoted 16 lines · Ira Weiny
Quoted 2 lines

I'd just return an error for that case, don't play silly games like evicting the inode.

I think I agree with Christoph here. But I want to clarify. I was heading in a direction of failing the ioctl completely. But we could have the flag change with an appropriate error which could let the user know the change has been delayed.

But I don't immediately see what error code is appropriate for such an indication. Candidates I can envision:

EAGAIN
ERESTART
EUSERS
EINPROGRESS

Quoted 5 lines
Quoted 1 line

None are perfect but I'm leaning toward EINPROGRESS.

I really, really dislike that idea. The whole point of not forcing evictions is to make it clear - no this inode is "busy" you can't do that. A reasonably smart application can try to evict itself.

I don't understand. What Darrick proposed would never need any evictions. If the file has blocks allocated the FS_XFLAG_DAX flag can not be changed. So I don't see what good eviction would do at all.

Quoted 3 lines

But returning an error and doing a lazy change anyway is straight from the playbook for arcane and confusing API designs.

Jan countered with a proposal that the FS_XFLAG_DAX does change with blocks allocated. But that S_DAX would change on eviction. Adding that some eviction ioctl could be added.

You then proposed just returning an error for that case. (This lead me to believe that you were ok with an eviction based change of S_DAX.)

So I agreed that changing S_DAX could be delayed until an explicit eviction. But, to aid the 'smart application', a different error code could be used to indicate that the FS_XFLAG_DAX had been changed but that until that explicit eviction occurs S_DAX would remain.

So I don't fully follow what you mean by 'lazy change'?

Do you still really, really dislike an explicit eviction method for changing the S_DAX flag?

If FS_XFLAG_DAX can never be changed on a file with blocks allocated and the user wants to change the mode of operations on their 'data'; they would have to create a new file with the proper setting and move the data there. For example copy the file into a directory marked FS_XFLAG_DAX==true?

I'm ok with either interface as I think both could be clear if documented.

Jan?

Ira

maintainer
#30 lore raw
Quoted 20 lines · Ira Weiny
Quoted 19 lines · Christoph Hellwig
Quoted 17 lines · Ira Weiny
Quoted 2 lines

I'd just return an error for that case, don't play silly games like evicting the inode.

I think I agree with Christoph here. But I want to clarify. I was heading in a direction of failing the ioctl completely. But we could have the flag change with an appropriate error which could let the user know the change has been delayed.

But I don't immediately see what error code is appropriate for such an indication. Candidates I can envision:

EAGAIN
ERESTART
EUSERS
EINPROGRESS

None are perfect but I'm leaning toward EINPROGRESS.

Quoted 7 lines
Quoted 3 lines

I really, really dislike that idea. The whole point of not forcing evictions is to make it clear - no this inode is "busy" you can't do that. A reasonably smart application can try to evict itself.

I don't understand. What Darrick proposed would never need any evictions. If the file has blocks allocated the FS_XFLAG_DAX flag can not be changed. So I don't see what good eviction would do at all.

I guess there's some confusion here (may well be than on my side). Darrick propose that we can switch FS_XFLAG_DAX only when file has no blocks allocated - fine by me. But that still does not mean than we can switch S_DAX immediately, does it? Because that would still mean we need to switch aops on living inode and that's ... difficult and Christoph didn't want to clutter the code with it.

So I've understood Darrick's proposal as: Just switch FS_XFLAG_DAX flag, S_DAX flag will magically switch when inode gets evicted and the inode gets reloaded from the disk again. Did I misunderstand anything?

And my thinking was that this is surprising behavior for the user and so it will likely generate lots of bug reports along the lines of "DAX inode flag does not work!". So I was pondering how to make the behavior less confusing... The ioctl I've suggested was just a poor attempt at that.

Quoted 6 lines
Quoted 2 lines

But returning an error and doing a lazy change anyway is straight from the playbook for arcane and confusing API designs.

Jan countered with a proposal that the FS_XFLAG_DAX does change with blocks allocated. But that S_DAX would change on eviction. Adding that some eviction ioctl could be added.

No, I didn't mean that we can change FS_XFLAG_DAX with blocks allocated. I was still speaking about the case without blocks allocated.

Quoted 13 lines

You then proposed just returning an error for that case. (This lead me to believe that you were ok with an eviction based change of S_DAX.)

So I agreed that changing S_DAX could be delayed until an explicit eviction. But, to aid the 'smart application', a different error code could be used to indicate that the FS_XFLAG_DAX had been changed but that until that explicit eviction occurs S_DAX would remain.

So I don't fully follow what you mean by 'lazy change'?

Do you still really, really dislike an explicit eviction method for changing the S_DAX flag?

Quoted 6 lines

If FS_XFLAG_DAX can never be changed on a file with blocks allocated and the user wants to change the mode of operations on their 'data'; they would have to create a new file with the proper setting and move the data there. For example copy the file into a directory marked FS_XFLAG_DAX==true?

I'm ok with either interface as I think both could be clear if documented.

I agree that what Darrick suggested is technically easily doable and can be documented. But it is not natural behavior (i.e., different than all inode flags we have) and we know how careful people are when reading documentation...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#31 lore raw
Quoted 36 lines · Jan Kara
Quoted 20 lines · Ira Weiny
Quoted 19 lines · Christoph Hellwig
Quoted 17 lines · Ira Weiny
Quoted 2 lines

I'd just return an error for that case, don't play silly games like evicting the inode.

I think I agree with Christoph here. But I want to clarify. I was heading in a direction of failing the ioctl completely. But we could have the flag change with an appropriate error which could let the user know the change has been delayed.

But I don't immediately see what error code is appropriate for such an indication. Candidates I can envision:

EAGAIN
ERESTART
EUSERS
EINPROGRESS

None are perfect but I'm leaning toward EINPROGRESS.

Quoted 7 lines
Quoted 3 lines

I really, really dislike that idea. The whole point of not forcing evictions is to make it clear - no this inode is "busy" you can't do that. A reasonably smart application can try to evict itself.

I don't understand. What Darrick proposed would never need any evictions. If the file has blocks allocated the FS_XFLAG_DAX flag can not be changed. So I don't see what good eviction would do at all.

I guess there's some confusion here (may well be than on my side). Darrick propose that we can switch FS_XFLAG_DAX only when file has no blocks allocated - fine by me. But that still does not mean than we can switch S_DAX immediately, does it? Because that would still mean we need to switch aops on living inode and that's ... difficult and Christoph didn't want to clutter the code with it.

Quoted 8 lines

So I've understood Darrick's proposal as: Just switch FS_XFLAG_DAX flag, S_DAX flag will magically switch when inode gets evicted and the inode gets reloaded from the disk again. Did I misunderstand anything?

And my thinking was that this is surprising behavior for the user and so it will likely generate lots of bug reports along the lines of "DAX inode flag does not work!". So I was pondering how to make the behavior less confusing... The ioctl I've suggested was just a poor attempt at that.

Ok but then I don't understand Christophs comment to "just return an error for that case"? Which case?

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

But returning an error and doing a lazy change anyway is straight from the playbook for arcane and confusing API designs.

Jan countered with a proposal that the FS_XFLAG_DAX does change with blocks allocated. But that S_DAX would change on eviction. Adding that some eviction ioctl could be added.

No, I didn't mean that we can change FS_XFLAG_DAX with blocks allocated. I was still speaking about the case without blocks allocated.

Ah ok good point. But again what 'error' do we return when FS_XFLAG_DAX changed but S_DAX did not?

Quoted 19 lines
Quoted 19 lines

You then proposed just returning an error for that case. (This lead me to believe that you were ok with an eviction based change of S_DAX.)

So I agreed that changing S_DAX could be delayed until an explicit eviction. But, to aid the 'smart application', a different error code could be used to indicate that the FS_XFLAG_DAX had been changed but that until that explicit eviction occurs S_DAX would remain.

So I don't fully follow what you mean by 'lazy change'?

Do you still really, really dislike an explicit eviction method for changing the S_DAX flag?

If FS_XFLAG_DAX can never be changed on a file with blocks allocated and the user wants to change the mode of operations on their 'data'; they would have to create a new file with the proper setting and move the data there. For example copy the file into a directory marked FS_XFLAG_DAX==true?

Quoted 7 lines
Quoted 1 line

I'm ok with either interface as I think both could be clear if documented.

I agree that what Darrick suggested is technically easily doable and can be documented. But it is not natural behavior (i.e., different than all inode flags we have) and we know how careful people are when reading documentation...

Ok For 5.8 why don't we not allow FS_XFLAG_DAX to be changed on files _at_ _all_...

In summary:

- Applications must call statx to discover the current S_DAX state.

- There exists an advisory file inode flag FS_XFLAG_DAX that is set based on the parent directory FS_XFLAG_DAX inode flag. (There is no way to change this flag after file creation.)

If FS_XFLAG_DAX is set and the fs is on pmem then it will enable S_DAX at inode load time; if FS_XFLAG_DAX is not set, it will not enable S_DAX. Unless overridden...

- There exists a dax= mount option.

"-o dax=off" means "never set S_DAX, ignore FS_XFLAG_DAX"
"-o nodax" means "dax=off"
"-o dax=always" means "always set S_DAX (at least on pmem), ignore FS_XFLAG_DAX"
"-o dax" by itself means "dax=always"
"-o dax=iflag" means "follow FS_XFLAG_DAX" and is the default

- There exists an advisory directory inode flag FS_XFLAG_DAX that can be changed at any time. The flag state is copied into any files or subdirectories when they are created within that directory. If programs require file access runs in S_DAX mode, they'll have to create those files inside a directory with FS_XFLAG_DAX set, or mount the fs with an appropriate dax mount option.

???

Ira

author
#32 lore raw

On Fri, Apr 03, 2020 at 11:18:43AM -0700, 'Ira Weiny' wrote:
[snip]

Quoted 3 lines

Ok For 5.8 why don't we not allow FS_XFLAG_DAX to be changed on files _at_ _all_...

To be fair, I believe Christoph advocated for this at one time or another...

Ira

Quoted 33 lines

In summary:

- Applications must call statx to discover the current S_DAX state.

- There exists an advisory file inode flag FS_XFLAG_DAX that is set based on the parent directory FS_XFLAG_DAX inode flag. (There is no way to change this flag after file creation.)

If FS_XFLAG_DAX is set and the fs is on pmem then it will enable S_DAX at inode load time; if FS_XFLAG_DAX is not set, it will not enable S_DAX. Unless overridden...

- There exists a dax= mount option.

"-o dax=off" means "never set S_DAX, ignore FS_XFLAG_DAX"
"-o nodax" means "dax=off"
"-o dax=always" means "always set S_DAX (at least on pmem), ignore FS_XFLAG_DAX"
"-o dax" by itself means "dax=always"
"-o dax=iflag" means "follow FS_XFLAG_DAX" and is the default

- There exists an advisory directory inode flag FS_XFLAG_DAX that can be changed at any time. The flag state is copied into any files or subdirectories when they are created within that directory. If programs require file access runs in S_DAX mode, they'll have to create those files inside a directory with FS_XFLAG_DAX set, or mount the fs with an appropriate dax mount option.

???

Ira

#33 lore raw
In reply to Ira Weiny
Quoted 90 lines · Ira Weiny
Quoted 36 lines · Jan Kara
Quoted 20 lines · Ira Weiny
Quoted 19 lines · Christoph Hellwig
Quoted 17 lines · Ira Weiny
Quoted 2 lines

I'd just return an error for that case, don't play silly games like evicting the inode.

I think I agree with Christoph here. But I want to clarify. I was heading in a direction of failing the ioctl completely. But we could have the flag change with an appropriate error which could let the user know the change has been delayed.

But I don't immediately see what error code is appropriate for such an indication. Candidates I can envision:

EAGAIN
ERESTART
EUSERS
EINPROGRESS

None are perfect but I'm leaning toward EINPROGRESS.

Quoted 7 lines
Quoted 3 lines

I really, really dislike that idea. The whole point of not forcing evictions is to make it clear - no this inode is "busy" you can't do that. A reasonably smart application can try to evict itself.

I don't understand. What Darrick proposed would never need any evictions. If the file has blocks allocated the FS_XFLAG_DAX flag can not be changed. So I don't see what good eviction would do at all.

I guess there's some confusion here (may well be than on my side). Darrick propose that we can switch FS_XFLAG_DAX only when file has no blocks allocated - fine by me. But that still does not mean than we can switch S_DAX immediately, does it? Because that would still mean we need to switch aops on living inode and that's ... difficult and Christoph didn't want to clutter the code with it.

Quoted 8 lines

So I've understood Darrick's proposal as: Just switch FS_XFLAG_DAX flag, S_DAX flag will magically switch when inode gets evicted and the inode gets reloaded from the disk again. Did I misunderstand anything?

And my thinking was that this is surprising behavior for the user and so it will likely generate lots of bug reports along the lines of "DAX inode flag does not work!". So I was pondering how to make the behavior less confusing... The ioctl I've suggested was just a poor attempt at that.

Ok but then I don't understand Christophs comment to "just return an error for that case"? Which case?

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

But returning an error and doing a lazy change anyway is straight from the playbook for arcane and confusing API designs.

Jan countered with a proposal that the FS_XFLAG_DAX does change with blocks allocated. But that S_DAX would change on eviction. Adding that some eviction ioctl could be added.

No, I didn't mean that we can change FS_XFLAG_DAX with blocks allocated. I was still speaking about the case without blocks allocated.

Ah ok good point. But again what 'error' do we return when FS_XFLAG_DAX changed but S_DAX did not?

Quoted 19 lines
Quoted 19 lines

You then proposed just returning an error for that case. (This lead me to believe that you were ok with an eviction based change of S_DAX.)

So I agreed that changing S_DAX could be delayed until an explicit eviction. But, to aid the 'smart application', a different error code could be used to indicate that the FS_XFLAG_DAX had been changed but that until that explicit eviction occurs S_DAX would remain.

So I don't fully follow what you mean by 'lazy change'?

Do you still really, really dislike an explicit eviction method for changing the S_DAX flag?

If FS_XFLAG_DAX can never be changed on a file with blocks allocated and the user wants to change the mode of operations on their 'data'; they would have to create a new file with the proper setting and move the data there. For example copy the file into a directory marked FS_XFLAG_DAX==true?

Quoted 7 lines
Quoted 1 line

I'm ok with either interface as I think both could be clear if documented.

I agree that what Darrick suggested is technically easily doable and can be documented. But it is not natural behavior (i.e., different than all inode flags we have) and we know how careful people are when reading documentation...

Quoted 6 lines

Ok For 5.8 why don't we not allow FS_XFLAG_DAX to be changed on files _at_ _all_...

In summary:

- Applications must call statx to discover the current S_DAX state.

Ok.

Quoted 7 lines

- There exists an advisory file inode flag FS_XFLAG_DAX that is set based on the parent directory FS_XFLAG_DAX inode flag. (There is no way to change this flag after file creation.)

If FS_XFLAG_DAX is set and the fs is on pmem then it will enable S_DAX at inode load time; if FS_XFLAG_DAX is not set, it will not enable S_DAX. Unless overridden...

Ok, fine with me. :)

Quoted 4 lines

- There exists a dax= mount option.

"-o dax=off" means "never set S_DAX, ignore FS_XFLAG_DAX"
"-o nodax" means "dax=off"

I surveyed the three fses that support dax and found that none of the filesystems actually have a 'nodax' flag. Now would be the time not to add such a thing, and make people specify dax=off instead. It would be handy if we could have a single fsparam_enum for figuring out the dax mount options.

Quoted 8 lines

"-o dax=always" means "always set S_DAX (at least on pmem), ignore FS_XFLAG_DAX"
"-o dax" by itself means "dax=always"
"-o dax=iflag" means "follow FS_XFLAG_DAX" and is the default

- There exists an advisory directory inode flag FS_XFLAG_DAX that can be changed at any time. The flag state is copied into any files or subdirectories when they are created within that directory. If programs require file access runs in S_DAX mode, they'll have to create those files

"...they must create..."

Quoted 2 lines

inside a directory with FS_XFLAG_DAX set, or mount the fs with an appropriate dax mount option.

Otherwise seems ok to me.

--D

Quoted 6 lines

???

Ira

author
#34 lore raw
Quoted 11 lines
Quoted 4 lines

In summary:

- Applications must call statx to discover the current S_DAX state.

Ok.

Quoted 4 lines

- There exists an advisory file inode flag FS_XFLAG_DAX that is set based on the parent directory FS_XFLAG_DAX inode flag. (There is no way to change this flag after file creation.)

Quoted 5 lines
Quoted 3 lines

If FS_XFLAG_DAX is set and the fs is on pmem then it will enable S_DAX at inode load time; if FS_XFLAG_DAX is not set, it will not enable S_DAX. Unless overridden...

Ok, fine with me. :)

:-D

Quoted 11 lines
Quoted 5 lines

- There exists a dax= mount option.

"-o dax=off" means "never set S_DAX, ignore FS_XFLAG_DAX"
"-o nodax" means "dax=off"

I surveyed the three fses that support dax and found that none of the filesystems actually have a 'nodax' flag. Now would be the time not to add such a thing, and make people specify dax=off instead. It would be handy if we could have a single fsparam_enum for figuring out the dax mount options.

yes good point.

I'm working on updating the documentation patch and I think this might also be better as:

-o dax=never

Which is the opposite of 'always'.

Quoted 11 lines
Quoted 9 lines

"-o dax=always" means "always set S_DAX (at least on pmem), ignore FS_XFLAG_DAX"
"-o dax" by itself means "dax=always"
"-o dax=iflag" means "follow FS_XFLAG_DAX" and is the default

- There exists an advisory directory inode flag FS_XFLAG_DAX that can be changed at any time. The flag state is copied into any files or subdirectories when they are created within that directory. If programs require file access runs in S_DAX mode, they'll have to create those files

"...they must create..."

yes

Quoted 5 lines
Quoted 3 lines

inside a directory with FS_XFLAG_DAX set, or mount the fs with an appropriate dax mount option.

Otherwise seems ok to me.

Thanks!
Ira

maintainer
#35 lore raw
In reply to Ira Weiny
Quoted 7 lines · Ira Weiny

Ok For 5.8 why don't we not allow FS_XFLAG_DAX to be changed on files _at_ _all_...

In summary:

- Applications must call statx to discover the current S_DAX state.

Quoted 7 lines

- There exists an advisory file inode flag FS_XFLAG_DAX that is set based on the parent directory FS_XFLAG_DAX inode flag. (There is no way to change this flag after file creation.)

If FS_XFLAG_DAX is set and the fs is on pmem then it will enable S_DAX at inode load time; if FS_XFLAG_DAX is not set, it will not enable S_DAX. Unless overridden...

OK, after considering all the options we were hashing out here, I think this is the best API. There isn't the confusing "S_DAX will magically switch on inode eviction" and although the functionality is limited, I think 90% of users would end up using the functionality like this anyway.

Quoted 8 lines

- There exists a dax= mount option.

"-o dax=off" means "never set S_DAX, ignore FS_XFLAG_DAX"
"-o nodax" means "dax=off"
"-o dax=always" means "always set S_DAX (at least on pmem), ignore FS_XFLAG_DAX"
"-o dax" by itself means "dax=always"
"-o dax=iflag" means "follow FS_XFLAG_DAX" and is the default

Quoted 6 lines

- There exists an advisory directory inode flag FS_XFLAG_DAX that can be changed at any time. The flag state is copied into any files or subdirectories when they are created within that directory. If programs require file access runs in S_DAX mode, they'll have to create those files inside a directory with FS_XFLAG_DAX set, or mount the fs with an appropriate dax mount option.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#36 lore raw
In reply to Jan Kara
Quoted 29 lines · Jan Kara
Quoted 20 lines · Ira Weiny
Quoted 19 lines · Christoph Hellwig
Quoted 17 lines · Ira Weiny
Quoted 2 lines

I'd just return an error for that case, don't play silly games like evicting the inode.

I think I agree with Christoph here. But I want to clarify. I was heading in a direction of failing the ioctl completely. But we could have the flag change with an appropriate error which could let the user know the change has been delayed.

But I don't immediately see what error code is appropriate for such an indication. Candidates I can envision:

EAGAIN
ERESTART
EUSERS
EINPROGRESS

None are perfect but I'm leaning toward EINPROGRESS.

Quoted 7 lines
Quoted 3 lines

I really, really dislike that idea. The whole point of not forcing evictions is to make it clear - no this inode is "busy" you can't do that. A reasonably smart application can try to evict itself.

I don't understand. What Darrick proposed would never need any evictions. If the file has blocks allocated the FS_XFLAG_DAX flag can not be changed. So I don't see what good eviction would do at all.

Quoted 6 lines

I guess there's some confusion here (may well be than on my side). Darrick propose that we can switch FS_XFLAG_DAX only when file has no blocks allocated - fine by me. But that still does not mean than we can switch S_DAX immediately, does it? Because that would still mean we need to switch aops on living inode and that's ... difficult and Christoph didn't want to clutter the code with it.

IIRC, the reason Ira was trying to introduce this file operations lock is because there isn't any other safe way to change the operations dynamically, because some of those operations don't start taking any locks at all until after we've accessed the file operations pointer.

Now that I think about it some more, that's also means we can't change S_DAX even on files without blocks allocated. Look at fallocate, it doesn't even take i_rwsem until we've called into (say) xfs_file_fallocate.

Ok, so with that in mind, I think we simply have to say that FS_XFLAG_DAX is 100% advisory, and S_DAX will magically switch some time in the future or after the next umount/mount cycle.

FSSETXATTR can't evict an inode it has just set FS_XFLAG_DAX on, because it applies that change to the fd passed into ioctl(), which means that the caller has a reference to the fd -> file -> inode, which means the inode is unevictable until after the call completes.

IOWs,

Quoted 3 lines

So I've understood Darrick's proposal as: Just switch FS_XFLAG_DAX flag, S_DAX flag will magically switch when inode gets evicted and the inode gets reloaded from the disk again. Did I misunderstand anything?

That was /my/ understanding. :)

Quoted 4 lines

And my thinking was that this is surprising behavior for the user and so it will likely generate lots of bug reports along the lines of "DAX inode flag does not work!". So I was pondering how to make the behavior less confusing... The ioctl I've suggested was just a poor attempt at that.

At best, userspace uses FSSETXATTR to change FS_XFLAG_DAX, and then calls some as-yet-undefined ioctl to try to evict the inode from memory. I'm not sure how you'd actually do that, though, considering that you'd have to close the fd and as soon as that happens the inode can disappear permanently.

Ok, that's not sane. Forget I ever wrote that.

Quoted 17 lines
Quoted 6 lines
Quoted 2 lines

But returning an error and doing a lazy change anyway is straight from the playbook for arcane and confusing API designs.

Jan countered with a proposal that the FS_XFLAG_DAX does change with blocks allocated. But that S_DAX would change on eviction. Adding that some eviction ioctl could be added.

No, I didn't mean that we can change FS_XFLAG_DAX with blocks allocated. I was still speaking about the case without blocks allocated.

Quoted 7 lines

You then proposed just returning an error for that case. (This lead me to believe that you were ok with an eviction based change of S_DAX.)

So I agreed that changing S_DAX could be delayed until an explicit eviction. But, to aid the 'smart application', a different error code could be used to indicate that the FS_XFLAG_DAX had been changed but that until that explicit eviction occurs S_DAX would remain.

There's no point in returning a magic error code from FSSETXATTR, as I realized above. To restate: FSSETXATTR can't evict the inode, so it would always have to return the magic error code. The best we can do is tell userspace that they can set the advisory FS_XFLAG_DAX flag and then check STATX_ATTR_DAX immediately afterwards. If some day in the future we get smarter and can change it immediately, the statx output will reflect that.

Quoted 10 lines
Quoted 10 lines

So I don't fully follow what you mean by 'lazy change'?

Do you still really, really dislike an explicit eviction method for changing the S_DAX flag?

If FS_XFLAG_DAX can never be changed on a file with blocks allocated and the user wants to change the mode of operations on their 'data'; they would have to create a new file with the proper setting and move the data there. For example copy the file into a directory marked FS_XFLAG_DAX==true?

Quoted 6 lines
Quoted 1 line

I'm ok with either interface as I think both could be clear if documented.

I agree that what Darrick suggested is technically easily doable and can be documented. But it is not natural behavior (i.e., different than all inode flags we have) and we know how careful people are when reading documentation...

To reflect all that I've rambled in this thread, I withdraw the previous paragraph and submit this one for consideration:

- There exists an advisory file inode flag FS_XFLAG_DAX.

If FS_XFLAG_DAX is set and the fs is on pmem then it will always enable S_DAX at inode load time; if FS_XFLAG_DAX is not set, it will never enable S_DAX. The advice can be overridden by mount option.

Changing this flag does not necessarily change the S_DAX state immediately but programs can query the S_DAX state via statx to detect when the new advice has gone into effect.

Consider this from the perspective of minimizing changes to userspace programs between now and the future. If your program really wants S_DAX, you can write:

	fd = open(...);

	ioctl(fd, FSGETXATTR, &fsx);

	if (fsx.xflags & FS_XFLAG_DAX)
		return 0;

	fsx.xflags |= FS_XFLAG_DAX;
	ioctl(fd, FSSETXATTR, &fsx);

	do {
		statx(fd, STATX_GIVE_ME_EVERYTHING, &statx);
		if (statx.attrs & STATX_ATTR_DAX)
			return 0;

		/* do stupid magic to evict things */
	} while (we haven't gotten bored and wandered away);

This code snippet will work with the current limitations of the kernel, and it'll continue working even on Linux v5000 where we finally figure out how to change S_DAX on the fly.

--D

Quoted 6 lines

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#37 lore raw
In reply to Christoph Hellwig
Quoted 17 lines · Christoph Hellwig
Quoted 16 lines · Ira Weiny
Quoted 2 lines

I'd just return an error for that case, don't play silly games like evicting the inode.

I think I agree with Christoph here. But I want to clarify. I was heading in a direction of failing the ioctl completely. But we could have the flag change with an appropriate error which could let the user know the change has been delayed.

But I don't immediately see what error code is appropriate for such an indication. Candidates I can envision:

EAGAIN
ERESTART
EUSERS
EINPROGRESS

Quoted 8 lines
Quoted 1 line

None are perfect but I'm leaning toward EINPROGRESS.

I really, really dislike that idea. The whole point of not forcing evictions is to make it clear - no this inode is "busy" you can't do that. A reasonably smart application can try to evict itself.

But returning an error and doing a lazy change anyway is straight from the playbook for arcane and confusing API designs.

Agreed. That's why I wrote that applications can set FS_XFLAG_DAX and then query statx for STATX_ATTR_DAX to find out if it actually took effect, and that if applications require it immediately they can either create a file in a FS_XFLAG_DAX directory, or the admin can mount with dax=always. No magic return values required or desired anywhere.

I don't know what "try to evict the inode" magic means, but I'm fairly sure I don't want to. ;)

--D

author
#38 lore raw
In reply to Darrick J. Wong
Quoted 45 lines · Darrick J. Wong
Quoted 18 lines · Christoph Hellwig
Quoted 6 lines · Jan Kara
Quoted 5 lines

This sounds reasonable to me.

As for deprecating the mount option, I think at a minimum it needs to continue be accepted as an option even if it is ignored to not break existing setups.

Quoted 6 lines

Agreed. But that's how we usually deprecate mount options. Also I'd say that statx() support for reporting DAX state and some education of programmers using DAX is required before we deprecate the mount option since currently applications check 'dax' mount option to determine how much memory they need to set aside for page cache before they consume everything else on the machine...

I don't even think we should deprecate it. It isn't painful to maintain and actually useful for testing. Instead we should expand it into a tristate:

Quoted 6 lines

dax=off
dax=flag
dax=always

where the existing "dax" option maps to "dax=always" and nodax maps to "dax=off". and dax=flag becomes the default for DAX capable devices.

That works for me. In summary:

- Applications must call statx to discover the current S_DAX state.

- There exists an advisory file inode flag FS_XFLAG_DAX that can be changed on files that have no blocks allocated to them. Changing this flag does not necessarily change the S_DAX state immediately but programs can query the S_DAX state via statx.

If FS_XFLAG_DAX is set and the fs is on pmem then it will always enable S_DAX at inode load time; if FS_XFLAG_DAX is not set, it will never enable S_DAX. Unless overridden...

- There exists a dax= mount option. dax=off means "never set S_DAX, ignore FS_XFLAG_DAX"; dax=always means "always set S_DAX (at least on pmem), ignore FS_XFLAG_DAX"; and dax=iflag means "follow FS_XFLAG_DAX" and is the default. "dax" by itself means "dax=always". "nodax" means "dax=off".

Quoted 6 lines

- There exists an advisory directory inode flag FS_XFLAG_DAX that can be changed at any time. The flag state is copied into any files or subdirectories created within that directory. If programs require that file access runs in S_DAX mode, they'll have to create those files themselves inside a directory with FS_XFLAG_DAX set, or mount the fs with dax=always.

One other thing to add here. They _can_ set the FS_XFLAG_DAX on a file with data and force an eviction to get S_DAX to change.

I think that is a nice reason to have a different error code returned.

Quoted 3 lines

Ok? Let's please get this part finished for 5.8, then we can get back to arguing about fs-rmap and reflink and dax and whatnot.

I'm happy to see you motivated to get this in.

I'm starting with a new xfstest to make sure we agree on the semantics prior to more patches. I hope to have the xfstest patch sent tomorrow sometime.

Ira

Quoted 2 lines

--D

#39 lore raw
In reply to Darrick J. Wong
Quoted 4 lines · Darrick J. Wong

There are still other things that need to be ironed out WRT pmem:

a) reflink and page/pfn/whatever sharing -- fix the mm or (ab)use the xfs buffer cache, or something worse?

I don't think we need either. We just need to remove the DAX page association for hwpoison that requires the struct page to store the mapping and index. Get rid of that and we should be able to safely map the same page into different inode address spaces at the same time. When we write a shared page, we COW it immediately and replace the page in the inode's mapping tree, so we can't actually write to a shared page...

IOWs, the dax_associate_page() related functionality probably needs to be a filesystem callout - part of the aops vector, I think, so that device dax can still use it. That way XFS can go it's own way, while ext4 and device dax can continue to use the existing mechanism mechanisn that is currently implemented....

XFS can then make use of rmapbt to find the owners on a bad page notification, and run the "kill userspace dead dead dead" lookup on each mapping/index tuple rather than pass it around on a struct page. i.e. we'll do a kill scan for each mapping/index owner tuple we find, not just one.

That requires converting all the current vma killer code to pass mapping/index tuples around rather than the struct page. That kill code doesn't actually need the struct page, it just needs the mapping/index tuple to match to the vmas that have it mapped into userspace.

Quoted 4 lines

b) getting our stories straight on how to clear poison, and whether or not we can come up with a better story for ZERO_FILE_RANGE on pmem. In the ideal world I'd love to see Z_F_R actually memset(0) the pmem and clear poison, at least if the file->pmem mappings were contiguous.

Are you talking about ZFR from userspace through the filesystem (how do you clear poison in free space?) or ZFR on the dax device fro either userspace or the kernel filesystem code?

Quoted 3 lines

c) wiring up xfs to hwpoison, or wiring up hwpoison to xfs, or otherwise figuring out how to get storage to tell xfs that it lost something so that maybe xfs can fix it quickly

Yup, I think that's a dax device callback into the filesystem. i.e the hwpoison gets delivered to the dax device, which then calls the fs function rather than do it's current "dax_lock_page(), kill userspace dead dead dead, dax_unlock_page()" dance. The filesystem can do a much more intricate dance and wreak far more havoc on userspace than what the dax device can do.....

Copious amounts of unused time are things I don't have, unfortunately. Only having 7 fingers to type with right now doesn't help, either.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#40 lore raw
Quoted 5 lines · Dave Chinner

IOWs, the dax_associate_page() related functionality probably needs to be a filesystem callout - part of the aops vector, I think, so that device dax can still use it. That way XFS can go it's own way, while ext4 and device dax can continue to use the existing mechanism mechanisn that is currently implemented....

s/XFS/XFS with rmap/, as most XFS file systems currently don't have that enabled we'll also need to keep the legacy path around.

#41 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Dave Chinner

IOWs, the dax_associate_page() related functionality probably needs to be a filesystem callout - part of the aops vector, I think, so that device dax can still use it. That way XFS can go it's own way, while ext4 and device dax can continue to use the existing mechanism mechanisn that is currently implemented....

s/XFS/XFS with rmap/, as most XFS file systems currently don't have that enabled we'll also need to keep the legacy path around.

Agree, it needs to be an opt-in capability as ext4 and xfs w/o rmap will stay on the legacy path for the foreseeable future.

#42 lore raw
In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Dave Chinner

IOWs, the dax_associate_page() related functionality probably needs to be a filesystem callout - part of the aops vector, I think, so that device dax can still use it. That way XFS can go it's own way, while ext4 and device dax can continue to use the existing mechanism mechanisn that is currently implemented....

s/XFS/XFS with rmap/, as most XFS file systems currently don't have that enabled we'll also need to keep the legacy path around.

Sure, that's trivially easy to handle in the XFS code once the callouts are in place.

But, quite frankly, we can enforce rmap to be enabled enabled because nobody is using a reflink enabled FS w/ DAX right now. Everyone will have to mkfs their filesystems anyway to enable reflink+dax, so we simply don't allow reflink+dax to be enabled unless rmap is also enabled. Simple, easy, trivial.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#43 lore raw
Quoted 11 lines · Dave Chinner
Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Dave Chinner

IOWs, the dax_associate_page() related functionality probably needs to be a filesystem callout - part of the aops vector, I think, so that device dax can still use it. That way XFS can go it's own way, while ext4 and device dax can continue to use the existing mechanism mechanisn that is currently implemented....

s/XFS/XFS with rmap/, as most XFS file systems currently don't have that enabled we'll also need to keep the legacy path around.

Quoted 8 lines

Sure, that's trivially easy to handle in the XFS code once the callouts are in place.

But, quite frankly, we can enforce rmap to be enabled enabled because nobody is using a reflink enabled FS w/ DAX right now. Everyone will have to mkfs their filesystems anyway to enable reflink+dax, so we simply don't allow reflink+dax to be enabled unless rmap is also enabled. Simple, easy, trivial.

Heh, this reminds me that I need to get that rmap performance analysis report out to the list... it does have a fairly substantial performance impact (in its current not-terribly-optimized state) but otoh enables self-repair.

--D

Quoted 6 lines

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#44 lore raw
In reply to Dave Chinner
Quoted 9 lines · Dave Chinner
Quoted 8 lines
Quoted 5 lines

IOWs, the dax_associate_page() related functionality probably needs to be a filesystem callout - part of the aops vector, I think, so that device dax can still use it. That way XFS can go it's own way, while ext4 and device dax can continue to use the existing mechanism mechanisn that is currently implemented....

s/XFS/XFS with rmap/, as most XFS file systems currently don't have that enabled we'll also need to keep the legacy path around.

Quoted 8 lines

Sure, that's trivially easy to handle in the XFS code once the callouts are in place.

But, quite frankly, we can enforce rmap to be enabled enabled because nobody is using a reflink enabled FS w/ DAX right now. Everyone will have to mkfs their filesystems anyway to enable reflink+dax, so we simply don't allow reflink+dax to be enabled unless rmap is also enabled. Simple, easy, trivial.

True, I think rmap will be required for DAX+reflink. But we still need the legacy infrastructure for error reporting.