Skip to contentSkip to conversation

another attempt to reduce sub-block DIO serialisation

RFC In review Christoph Hellwig 4 participants 25 messages Jan 12, 2021 – Jan 14, 2021

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

This takes the approach from Dave, but adds a new noalloc flag instead of the nowait one, and keeps a simpler calling convention for iomap_dio_rw.

Only lightly tested so far.

#1 lore raw

PATCH 1/10 xfs: factor out a xfs_ilock_iocb helper

Add a helper to factor out the nowait locking logical for the read/write helpers.

---
 fs/xfs/xfs_file.c | 55 +++++++++++++++++++++++++----------------------
 1 file changed, 29 insertions(+), 26 deletions(-)
fs/xfs/xfs_file.c +29 -26
index 5b0f93f738372d..c441cddfa4acbc 100644
@@ -197,6 +197,23 @@ xfs_file_fsync(
 	return error;
 }
 
+static int
+xfs_ilock_iocb(
+	struct kiocb		*iocb,
+	unsigned int		lock_mode)
+{
+	struct xfs_inode	*ip = XFS_I(file_inode(iocb->ki_filp));
+
+	if (iocb->ki_flags & IOCB_NOWAIT) {
+		if (!xfs_ilock_nowait(ip, lock_mode))
+			return -EAGAIN;
+	} else {
+		xfs_ilock(ip, lock_mode);
+	}
+
+	return 0;
+}
+
 STATIC ssize_t
 xfs_file_dio_aio_read(
 	struct kiocb		*iocb,
@@ -213,12 +230,9 @@ xfs_file_dio_aio_read(
 
 	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 = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
+	if (ret)
+		return ret;
 	ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL,
 			is_sync_kiocb(iocb));
 	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
@@ -240,13 +254,9 @@ xfs_file_dax_read(
 	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 = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
+	if (ret)
+		return ret;
 	ret = dax_iomap_rw(iocb, to, &xfs_read_iomap_ops);
 	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
 
@@ -264,12 +274,9 @@ xfs_file_buffered_aio_read(
 
 	trace_xfs_file_buffered_read(ip, 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 = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
+	if (ret)
+		return ret;
 	ret = generic_file_read_iter(iocb, to);
 	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
 
@@ -608,13 +615,9 @@ xfs_file_dax_write(
 	size_t			count;
 	loff_t			pos;
 
-	if (iocb->ki_flags & IOCB_NOWAIT) {
-		if (!xfs_ilock_nowait(ip, iolock))
-			return -EAGAIN;
-	} else {
-		xfs_ilock(ip, iolock);
-	}
-
+	ret = xfs_ilock_iocb(iocb, iolock);
+	if (ret)
+		return ret;
 	ret = xfs_file_aio_write_checks(iocb, from, &iolock);
 	if (ret)
 		goto out;
Signature · 1 line
2.29.2
#3 lore raw

PATCH 2/10 xfs: make xfs_file_aio_write_checks IOCB_NOWAIT-aware

In reply to Christoph Hellwig

Ensure we don't block on the iolock, or waiting for I/O in xfs_file_aio_write_checks if the caller asked to avoid that.

Fixes 29a5d29ec181 ("xfs: nowait aio support")
---
 fs/xfs/xfs_file.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
fs/xfs/xfs_file.c +21 -4
index c441cddfa4acbc..fb4e6f2852bb8b 100644
@@ -335,7 +335,14 @@ xfs_file_aio_write_checks(
 	if (error <= 0)
 		return error;
 
-	error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
+	if (iocb->ki_flags & IOCB_NOWAIT) {
+		error = break_layout(inode, false);
+		if (error == -EWOULDBLOCK)
+			error = -EAGAIN;
+	} else {
+		error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
+	}
+
 	if (error)
 		return error;
 
@@ -346,7 +353,11 @@ xfs_file_aio_write_checks(
 	if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
 		xfs_iunlock(ip, *iolock);
 		*iolock = XFS_IOLOCK_EXCL;
-		xfs_ilock(ip, *iolock);
+		error = xfs_ilock_iocb(iocb, *iolock);
+		if (error) {
+			*iolock = 0;
+			return error;
+		}
 		goto restart;
 	}
 	/*
@@ -368,6 +379,10 @@ xfs_file_aio_write_checks(
 	isize = i_size_read(inode);
 	if (iocb->ki_pos > isize) {
 		spin_unlock(&ip->i_flags_lock);
+
+		if (iocb->ki_flags & IOCB_NOWAIT)
+			return -EAGAIN;
+
 		if (!drained_dio) {
 			if (*iolock == XFS_IOLOCK_SHARED) {
 				xfs_iunlock(ip, *iolock);
@@ -593,7 +608,8 @@ xfs_file_dio_aio_write(
 			   &xfs_dio_write_ops,
 			   is_sync_kiocb(iocb) || unaligned_io);
 out:
-	xfs_iunlock(ip, iolock);
+	if (iolock)
+		xfs_iunlock(ip, iolock);
 
 	/*
 	 * No fallback to buffered IO after short writes for XFS, direct I/O
@@ -632,7 +648,8 @@ xfs_file_dax_write(
 		error = xfs_setfilesize(ip, pos, ret);
 	}
 out:
-	xfs_iunlock(ip, iolock);
+	if (iolock)
+		xfs_iunlock(ip, iolock);
 	if (error)
 		return error;
 
Signature · 1 line
2.29.2
Reviewed-by on v1 2/10
#4 lore raw

Re: [PATCH 02/10] xfs: make xfs_file_aio_write_checks IOCB_NOWAIT-aware

Quoted 9 lines · Christoph Hellwig

Ensure we don't block on the iolock, or waiting for I/O in xfs_file_aio_write_checks if the caller asked to avoid that.

Fixes 29a5d29ec181 ("xfs: nowait aio support")
---
 fs/xfs/xfs_file.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

LGTM

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

PATCH 3/10 xfs: cleanup the read/write helper naming

In reply to Christoph Hellwig

Drop a few pointless aio_ prefixes.

---
 fs/xfs/xfs_file.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
fs/xfs/xfs_file.c +15 -15
index fb4e6f2852bb8b..ae7313ccaa11ed 100644
@@ -215,7 +215,7 @@ xfs_ilock_iocb(
 }
 
 STATIC ssize_t
-xfs_file_dio_aio_read(
+xfs_file_dio_read(
 	struct kiocb		*iocb,
 	struct iov_iter		*to)
 {
@@ -265,7 +265,7 @@ xfs_file_dax_read(
 }
 
 STATIC ssize_t
-xfs_file_buffered_aio_read(
+xfs_file_buffered_read(
 	struct kiocb		*iocb,
 	struct iov_iter		*to)
 {
@@ -300,9 +300,9 @@ xfs_file_read_iter(
 	if (IS_DAX(inode))
 		ret = xfs_file_dax_read(iocb, to);
 	else if (iocb->ki_flags & IOCB_DIRECT)
-		ret = xfs_file_dio_aio_read(iocb, to);
+		ret = xfs_file_dio_read(iocb, to);
 	else
-		ret = xfs_file_buffered_aio_read(iocb, to);
+		ret = xfs_file_buffered_read(iocb, to);
 
 	if (ret > 0)
 		XFS_STATS_ADD(mp, xs_read_bytes, ret);
@@ -317,7 +317,7 @@ xfs_file_read_iter(
  * if called for a direct write beyond i_size.
  */
 STATIC ssize_t
-xfs_file_aio_write_checks(
+xfs_file_write_checks(
 	struct kiocb		*iocb,
 	struct iov_iter		*from,
 	int			*iolock)
@@ -502,7 +502,7 @@ static const struct iomap_dio_ops xfs_dio_write_ops = {
 };
 
 /*
- * xfs_file_dio_aio_write - handle direct IO writes
+ * xfs_file_dio_write - handle direct IO writes
  *
  * Lock the inode appropriately to prepare for and issue a direct IO write.
  * By separating it from the buffered write path we remove all the tricky to
@@ -527,7 +527,7 @@ static const struct iomap_dio_ops xfs_dio_write_ops = {
  * negative return values.
  */
 STATIC ssize_t
-xfs_file_dio_aio_write(
+xfs_file_dio_write(
 	struct kiocb		*iocb,
 	struct iov_iter		*from)
 {
@@ -549,7 +549,7 @@ xfs_file_dio_aio_write(
 	/*
 	 * Don't take the exclusive iolock here unless the I/O is unaligned to
 	 * the file system block size.  We don't need to consider the EOF
-	 * extension case here because xfs_file_aio_write_checks() will relock
+	 * extension case here because xfs_file_write_checks() will relock
 	 * the inode as necessary for EOF zeroing cases and fill out the new
 	 * inode size as appropriate.
 	 */
@@ -580,7 +580,7 @@ xfs_file_dio_aio_write(
 		xfs_ilock(ip, iolock);
 	}
 
-	ret = xfs_file_aio_write_checks(iocb, from, &iolock);
+	ret = xfs_file_write_checks(iocb, from, &iolock);
 	if (ret)
 		goto out;
 	count = iov_iter_count(from);
@@ -590,7 +590,7 @@ xfs_file_dio_aio_write(
 	 * in-flight at the same time or we risk data corruption. Wait for all
 	 * other IO to drain before we submit. If the IO is aligned, demote the
 	 * iolock if we had to take the exclusive lock in
-	 * xfs_file_aio_write_checks() for other reasons.
+	 * xfs_file_write_checks() for other reasons.
 	 */
 	if (unaligned_io) {
 		inode_dio_wait(inode);
@@ -634,7 +634,7 @@ xfs_file_dax_write(
 	ret = xfs_ilock_iocb(iocb, iolock);
 	if (ret)
 		return ret;
-	ret = xfs_file_aio_write_checks(iocb, from, &iolock);
+	ret = xfs_file_write_checks(iocb, from, &iolock);
 	if (ret)
 		goto out;
 
@@ -663,7 +663,7 @@ xfs_file_dax_write(
 }
 
 STATIC ssize_t
-xfs_file_buffered_aio_write(
+xfs_file_buffered_write(
 	struct kiocb		*iocb,
 	struct iov_iter		*from)
 {
@@ -682,7 +682,7 @@ xfs_file_buffered_aio_write(
 	iolock = XFS_IOLOCK_EXCL;
 	xfs_ilock(ip, iolock);
 
-	ret = xfs_file_aio_write_checks(iocb, from, &iolock);
+	ret = xfs_file_write_checks(iocb, from, &iolock);
 	if (ret)
 		goto out;
 
@@ -769,12 +769,12 @@ xfs_file_write_iter(
 		 * CoW.  In all other directio scenarios we do not
 		 * allow an operation to fall back to buffered mode.
 		 */
-		ret = xfs_file_dio_aio_write(iocb, from);
+		ret = xfs_file_dio_write(iocb, from);
 		if (ret != -ENOTBLK)
 			return ret;
 	}
 
-	return xfs_file_buffered_aio_write(iocb, from);
+	return xfs_file_buffered_write(iocb, from);
 }
 
 static void
Signature · 1 line
2.29.2
#7 lore raw

PATCH 4/10 xfs: remove the buffered I/O fallback assert

In reply to Christoph Hellwig

The iomap code has been designed from the start not to do magic fallback, so remove the assert in preparation for further code cleanups.

---
 fs/xfs/xfs_file.c | 6 ------
 1 file changed, 6 deletions(-)
fs/xfs/xfs_file.c +0 -6
index ae7313ccaa11ed..97836ec53397d4 100644
@@ -610,12 +610,6 @@ xfs_file_dio_write(
 out:
 	if (iolock)
 		xfs_iunlock(ip, iolock);
-
-	/*
-	 * No fallback to buffered IO after short writes for XFS, direct I/O
-	 * will either complete fully or return an error.
-	 */
-	ASSERT(ret < 0 || ret == count);
 	return ret;
 }
 
Signature · 1 line
2.29.2
Reviewed-by on v1 4/10
#8 lore raw

Re: [PATCH 04/10] xfs: remove the buffered I/O fallback assert

Quoted 14 lines · Christoph Hellwig

The iomap code has been designed from the start not to do magic fallback, so remove the assert in preparation for further code cleanups.

---
 fs/xfs/xfs_file.c | 6 ------
 1 file changed, 6 deletions(-)
fs/xfs/xfs_file.c +0 -0
index ae7313ccaa11ed..97836ec53397d4 100644
@@ -610,12 +610,6 @@ xfs_file_dio_write(
 out:

fs/xfs/xfs_file.c xfs_file_dio_write( · line 614

Quoted 10 lines
fs/xfs/xfs_file.c +0 -6
 	if (iolock)
 		xfs_iunlock(ip, iolock);
-
-	/*
-	 * No fallback to buffered IO after short writes for XFS, direct I/O
-	 * will either complete fully or return an error.
-	 */
-	ASSERT(ret < 0 || ret == count);
 	return ret;
 }

Looks good.

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

PATCH 5/10 xfs: simplify the read/write tracepoints

In reply to Christoph Hellwig

Pass the iocb and iov_iter to the tracepoints and leave decoding of actual arguments to the code only run when tracing is enabled.

---
 fs/xfs/xfs_file.c  | 20 ++++++++------------
 fs/xfs/xfs_trace.h | 18 +++++++++---------
 2 files changed, 17 insertions(+), 21 deletions(-)
fs/xfs/xfs_file.c +8 -12
index 97836ec53397d4..aa64e78fc3c467 100644
@@ -220,12 +220,11 @@ xfs_file_dio_read(
 	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);
+	trace_xfs_file_direct_read(iocb, to);
 
-	if (!count)
+	if (!iov_iter_count(to))
 		return 0; /* skip atime */
 
 	file_accessed(iocb->ki_filp);
@@ -246,12 +245,11 @@ xfs_file_dax_read(
 	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);
+	trace_xfs_file_dax_read(iocb, to);
 
-	if (!count)
+	if (!iov_iter_count(to))
 		return 0; /* skip atime */
 
 	ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
@@ -272,7 +270,7 @@ xfs_file_buffered_read(
 	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(iocb, to);
 
 	ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
 	if (ret)
@@ -599,7 +597,7 @@ xfs_file_dio_write(
 		iolock = XFS_IOLOCK_SHARED;
 	}
 
-	trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
+	trace_xfs_file_direct_write(iocb, from);
 	/*
 	 * If unaligned, this is the only IO in-flight. Wait on it before we
 	 * release the iolock to prevent subsequent overlapping IO.
@@ -622,7 +620,6 @@ xfs_file_dax_write(
 	struct xfs_inode	*ip = XFS_I(inode);
 	int			iolock = XFS_IOLOCK_EXCL;
 	ssize_t			ret, error = 0;
-	size_t			count;
 	loff_t			pos;
 
 	ret = xfs_ilock_iocb(iocb, iolock);
@@ -633,9 +630,8 @@ xfs_file_dax_write(
 		goto out;
 
 	pos = iocb->ki_pos;
-	count = iov_iter_count(from);
 
-	trace_xfs_file_dax_write(ip, count, pos);
+	trace_xfs_file_dax_write(iocb, from);
 	ret = dax_iomap_rw(iocb, from, &xfs_direct_write_iomap_ops);
 	if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
 		i_size_write(inode, iocb->ki_pos);
@@ -683,7 +679,7 @@ xfs_file_buffered_write(
 	/* We can write back this queue in page reclaim */
 	current->backing_dev_info = inode_to_bdi(inode);
 
-	trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
+	trace_xfs_file_buffered_write(iocb, from);
 	ret = iomap_file_buffered_write(iocb, from,
 			&xfs_buffered_write_iomap_ops);
 	if (likely(ret >= 0))
fs/xfs/xfs_trace.h +9 -9
index 5a263ae3d4f008..a6d04d860a565e 100644
@@ -1287,8 +1287,8 @@ TRACE_EVENT(xfs_log_assign_tail_lsn,
 )
 
 DECLARE_EVENT_CLASS(xfs_file_class,
-	TP_PROTO(struct xfs_inode *ip, size_t count, loff_t offset),
-	TP_ARGS(ip, count, offset),
+	TP_PROTO(struct kiocb *iocb, struct iov_iter *iter),
+	TP_ARGS(iocb, iter),
 	TP_STRUCT__entry(
 		__field(dev_t, dev)
 		__field(xfs_ino_t, ino)
@@ -1297,11 +1297,11 @@ DECLARE_EVENT_CLASS(xfs_file_class,
 		__field(size_t, count)
 	),
 	TP_fast_assign(
-		__entry->dev = VFS_I(ip)->i_sb->s_dev;
-		__entry->ino = ip->i_ino;
-		__entry->size = ip->i_d.di_size;
-		__entry->offset = offset;
-		__entry->count = count;
+		__entry->dev = file_inode(iocb->ki_filp)->i_sb->s_dev;
+		__entry->ino = XFS_I(file_inode(iocb->ki_filp))->i_ino;
+		__entry->size = XFS_I(file_inode(iocb->ki_filp))->i_d.di_size;
+		__entry->offset = iocb->ki_pos;
+		__entry->count = iov_iter_count(iter);
 	),
 	TP_printk("dev %d:%d ino 0x%llx size 0x%llx offset 0x%llx count 0x%zx",
 		  MAJOR(__entry->dev), MINOR(__entry->dev),
@@ -1313,8 +1313,8 @@ DECLARE_EVENT_CLASS(xfs_file_class,
 
 #define DEFINE_RW_EVENT(name)		\
 DEFINE_EVENT(xfs_file_class, name,	\
-	TP_PROTO(struct xfs_inode *ip, size_t count, loff_t offset),	\
-	TP_ARGS(ip, count, offset))
+	TP_PROTO(struct kiocb *iocb, struct iov_iter *iter),		\
+	TP_ARGS(iocb, iter))
 DEFINE_RW_EVENT(xfs_file_buffered_read);
 DEFINE_RW_EVENT(xfs_file_direct_read);
 DEFINE_RW_EVENT(xfs_file_dax_read);
Signature · 1 line
2.29.2
Reviewed-by on v1 5/10
#10 lore raw

Re: [PATCH 05/10] xfs: simplify the read/write tracepoints

Quoted 8 lines · Christoph Hellwig

Pass the iocb and iov_iter to the tracepoints and leave decoding of actual arguments to the code only run when tracing is enabled.

---
 fs/xfs/xfs_file.c  | 20 ++++++++------------
 fs/xfs/xfs_trace.h | 18 +++++++++---------
 2 files changed, 17 insertions(+), 21 deletions(-)

looks ok.

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

PATCH 6/10 xfs: improve the reflink_bounce_dio_write tracepoint

In reply to Christoph Hellwig

Use a more suitable event class.

---
 fs/xfs/xfs_file.c  | 2 +-
 fs/xfs/xfs_trace.h | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/xfs/xfs_file.c +1 -1
index aa64e78fc3c467..a696bd34f71d21 100644
@@ -560,7 +560,7 @@ xfs_file_dio_write(
 		 * files yet, as we can't unshare a partial block.
 		 */
 		if (xfs_is_cow_inode(ip)) {
-			trace_xfs_reflink_bounce_dio_write(ip, iocb->ki_pos, count);
+			trace_xfs_reflink_bounce_dio_write(iocb, from);
 			return -ENOTBLK;
 		}
 		iolock = XFS_IOLOCK_EXCL;
fs/xfs/xfs_trace.h +2 -2
index a6d04d860a565e..0cfd65cd67c190 100644
@@ -1321,6 +1321,8 @@ DEFINE_RW_EVENT(xfs_file_dax_read);
 DEFINE_RW_EVENT(xfs_file_buffered_write);
 DEFINE_RW_EVENT(xfs_file_direct_write);
 DEFINE_RW_EVENT(xfs_file_dax_write);
+DEFINE_RW_EVENT(xfs_reflink_bounce_dio_write);
+
 
 DECLARE_EVENT_CLASS(xfs_imap_class,
 	TP_PROTO(struct xfs_inode *ip, xfs_off_t offset, ssize_t count,
@@ -3294,8 +3296,6 @@ DEFINE_INODE_IREC_EVENT(xfs_reflink_cow_found);
 DEFINE_INODE_IREC_EVENT(xfs_reflink_cow_enospc);
 DEFINE_INODE_IREC_EVENT(xfs_reflink_convert_cow);
 
-DEFINE_SIMPLE_IO_EVENT(xfs_reflink_bounce_dio_write);
-
 DEFINE_SIMPLE_IO_EVENT(xfs_reflink_cancel_cow_range);
 DEFINE_SIMPLE_IO_EVENT(xfs_reflink_end_cow);
 DEFINE_INODE_IREC_EVENT(xfs_reflink_cow_remap);
Signature · 1 line
2.29.2
#13 lore raw

PATCH 7/10 xfs: split unaligned DIO write code out

In reply to Christoph Hellwig

From: Dave Chinner <[email protected]>

The unaligned DIO write path is more convolted than the normal path, and we are about to make it more complex. Keep the block aligned fast path dio write code trim and simple by splitting out the unaligned DIO code from it.

[hch: rebased, fixed a few minor nits]
---
 fs/xfs/xfs_file.c | 168 +++++++++++++++++++++++++---------------------
 1 file changed, 92 insertions(+), 76 deletions(-)
fs/xfs/xfs_file.c +92 -76
index a696bd34f71d21..bffd7240cefb7f 100644
@@ -500,117 +500,133 @@ static const struct iomap_dio_ops xfs_dio_write_ops = {
 };
 
 /*
- * xfs_file_dio_write - handle direct IO writes
+ * Handle block aligned direct IO writes
  *
  * Lock the inode appropriately to prepare for and issue a direct IO write.
- * By separating it from the buffered write path we remove all the tricky to
- * follow locking changes and looping.
  *
  * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
  * until we're sure the bytes at the new EOF have been zeroed and/or the cached
  * pages are flushed out.
+ */
+static noinline ssize_t
+xfs_file_dio_write_aligned(
+	struct xfs_inode	*ip,
+	struct kiocb		*iocb,
+	struct iov_iter		*from)
+{
+	int			iolock = XFS_IOLOCK_SHARED;
+	ssize_t			ret;
+
+	ret = xfs_ilock_iocb(iocb, iolock);
+	if (ret)
+		return ret;
+	ret = xfs_file_write_checks(iocb, from, &iolock);
+	if (ret)
+		goto out_unlock;
+
+	/*
+	 * We don't need to hold the IOLOCK exclusively across the IO, so demote
+	 * the iolock back to shared if we had to take the exclusive lock in
+	 * xfs_file_write_checks() for other reasons.
+	 */
+	if (iolock == XFS_IOLOCK_EXCL) {
+		xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
+		iolock = XFS_IOLOCK_SHARED;
+	}
+	trace_xfs_file_direct_write(iocb, from);
+	ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
+			   &xfs_dio_write_ops, is_sync_kiocb(iocb));
+out_unlock:
+	if (iolock)
+		xfs_iunlock(ip, iolock);
+	return ret;
+}
+
+/*
+ * Handle block unaligned direct IO writes
+ *
+ * In most cases direct IO writes will be done holding IOLOCK_SHARED, allowing
+ * them to be done in parallel with reads and other direct IO writes.  However,
+ * if the I/O is not aligned to filesystem blocks, the direct I/O layer may
+ * need to do sub-block zeroing and that requires serialisation against other
+ * direct I/Os to the same block. In this case we need to serialise the
+ * submission of the unaligned I/Os so that we don't get racing block zeroing in
+ * the dio layer.
  *
- * In most cases the direct IO writes will be done holding IOLOCK_SHARED
- * allowing them to be done in parallel with reads and other direct IO writes.
- * However, if the IO is not aligned to filesystem blocks, the direct IO layer
- * needs to do sub-block zeroing and that requires serialisation against other
- * direct IOs to the same block. In this case we need to serialise the
- * submission of the unaligned IOs so that we don't get racing block zeroing in
- * the dio layer.  To avoid the problem with aio, we also need to wait for
+ * To provide the same serialisation for AIO, we also need to wait for
  * outstanding IOs to complete so that unwritten extent conversion is completed
  * before we try to map the overlapping block. This is currently implemented by
  * hitting it with a big hammer (i.e. inode_dio_wait()).
  *
- * Returns with locks held indicated by @iolock and errors indicated by
- * negative return values.
+ * This means that unaligned dio writes always block. There is no "nowait" fast
+ * path in this code - if IOCB_NOWAIT is set we simply return -EAGAIN up front
+ * and we don't have to worry about that anymore.
  */
-STATIC ssize_t
-xfs_file_dio_write(
+static noinline ssize_t
+xfs_file_dio_write_unaligned(
+	struct xfs_inode	*ip,
 	struct kiocb		*iocb,
 	struct iov_iter		*from)
 {
-	struct file		*file = iocb->ki_filp;
-	struct address_space	*mapping = file->f_mapping;
-	struct inode		*inode = mapping->host;
-	struct xfs_inode	*ip = XFS_I(inode);
-	struct xfs_mount	*mp = ip->i_mount;
-	ssize_t			ret = 0;
-	int			unaligned_io = 0;
-	int			iolock;
-	size_t			count = iov_iter_count(from);
-	struct xfs_buftarg      *target = xfs_inode_buftarg(ip);
+	int			iolock = XFS_IOLOCK_EXCL;
+	ssize_t			ret;
 
-	/* DIO must be aligned to device logical sector size */
-	if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
-		return -EINVAL;
+	/* unaligned dio always waits, bail */
+	if (iocb->ki_flags & IOCB_NOWAIT)
+		return -EAGAIN;
+	xfs_ilock(ip, iolock);
 
 	/*
-	 * Don't take the exclusive iolock here unless the I/O is unaligned to
-	 * the file system block size.  We don't need to consider the EOF
-	 * extension case here because xfs_file_write_checks() will relock
-	 * the inode as necessary for EOF zeroing cases and fill out the new
-	 * inode size as appropriate.
+	 * We can't properly handle unaligned direct I/O to reflink files yet,
+	 * as we can't unshare a partial block.
 	 */
-	if ((iocb->ki_pos & mp->m_blockmask) ||
-	    ((iocb->ki_pos + count) & mp->m_blockmask)) {
-		unaligned_io = 1;
-
-		/*
-		 * We can't properly handle unaligned direct I/O to reflink
-		 * files yet, as we can't unshare a partial block.
-		 */
-		if (xfs_is_cow_inode(ip)) {
-			trace_xfs_reflink_bounce_dio_write(iocb, from);
-			return -ENOTBLK;
-		}
-		iolock = XFS_IOLOCK_EXCL;
-	} else {
-		iolock = XFS_IOLOCK_SHARED;
-	}
-
-	if (iocb->ki_flags & IOCB_NOWAIT) {
-		/* unaligned dio always waits, bail */
-		if (unaligned_io)
-			return -EAGAIN;
-		if (!xfs_ilock_nowait(ip, iolock))
-			return -EAGAIN;
-	} else {
-		xfs_ilock(ip, iolock);
+	if (xfs_is_cow_inode(ip)) {
+		trace_xfs_reflink_bounce_dio_write(iocb, from);
+		ret = -ENOTBLK;
+		goto out_unlock;
 	}
 
 	ret = xfs_file_write_checks(iocb, from, &iolock);
 	if (ret)
-		goto out;
-	count = iov_iter_count(from);
+		goto out_unlock;
 
 	/*
-	 * If we are doing unaligned IO, we can't allow any other overlapping IO
-	 * in-flight at the same time or we risk data corruption. Wait for all
-	 * other IO to drain before we submit. If the IO is aligned, demote the
-	 * iolock if we had to take the exclusive lock in
-	 * xfs_file_write_checks() for other reasons.
+	 * If we are doing unaligned I/O, we can't allow any other overlapping
+	 * I/O in-flight at the same time or we risk data corruption. Wait for
+	 * all other I/O to drain before we submit.
 	 */
-	if (unaligned_io) {
-		inode_dio_wait(inode);
-	} else if (iolock == XFS_IOLOCK_EXCL) {
-		xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
-		iolock = XFS_IOLOCK_SHARED;
-	}
+	inode_dio_wait(VFS_I(ip));
 
-	trace_xfs_file_direct_write(iocb, from);
 	/*
-	 * If unaligned, this is the only IO in-flight. Wait on it before we
-	 * release the iolock to prevent subsequent overlapping IO.
+	 * This must be the only I/O in-flight. Wait on it before we release the
+	 * iolock to prevent subsequent overlapping I/O.
 	 */
+	trace_xfs_file_direct_write(iocb, from);
 	ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
-			   &xfs_dio_write_ops,
-			   is_sync_kiocb(iocb) || unaligned_io);
-out:
+			   &xfs_dio_write_ops, true);
+out_unlock:
 	if (iolock)
 		xfs_iunlock(ip, iolock);
 	return ret;
 }
 
+static ssize_t
+xfs_file_dio_write(
+	struct kiocb		*iocb,
+	struct iov_iter		*from)
+{
+	struct xfs_inode	*ip = XFS_I(file_inode(iocb->ki_filp));
+	struct xfs_buftarg      *target = xfs_inode_buftarg(ip);
+	size_t			count = iov_iter_count(from);
+
+	/* DIO must be aligned to device logical sector size */
+	if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
+		return -EINVAL;
+	if ((iocb->ki_pos | count) & ip->i_mount->m_blockmask)
+		return xfs_file_dio_write_unaligned(ip, iocb, from);
+	return xfs_file_dio_write_aligned(ip, iocb, from);
+}
+
 static noinline ssize_t
 xfs_file_dax_write(
 	struct kiocb		*iocb,
Signature · 1 line
2.29.2
Reviewed-by on v1 7/10
#14 lore raw

Re: [PATCH 07/10] xfs: split unaligned DIO write code out

Quoted 10 lines · Christoph Hellwig

From: Dave Chinner <[email protected]>

The unaligned DIO write path is more convolted than the normal path, and we are about to make it more complex. Keep the block aligned fast path dio write code trim and simple by splitting out the unaligned DIO code from it.

[hch: rebased, fixed a few minor nits]

Looks good.

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

PATCH 8/10 iomap: pass a flags argument to iomap_dio_rw

In reply to Christoph Hellwig

Pass a set of flags to iomap_dio_rw instead of the boolean wait_for_completion argument. The IOMAP_DIO_FORCE_WAIT flag replaces the wait_for_completion, but only needs to be passed when the iocb isn't synchronous to start with to simplify the callers.

---
 fs/btrfs/file.c       |  7 +++----
 fs/ext4/file.c        |  5 ++---
 fs/gfs2/file.c        |  7 ++-----
 fs/iomap/direct-io.c  | 19 ++++++++++---------
 fs/xfs/xfs_file.c     |  7 +++----
 fs/zonefs/super.c     |  4 ++--
 include/linux/iomap.h |  6 ++++--
 7 files changed, 26 insertions(+), 29 deletions(-)
fs/btrfs/file.c +3 -4
index 0e41459b8de667..ddfd2e2adedf58 100644
@@ -1949,8 +1949,8 @@ static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)
 		goto buffered;
 	}
 
-	dio = __iomap_dio_rw(iocb, from, &btrfs_dio_iomap_ops,
-			     &btrfs_dio_ops, is_sync_kiocb(iocb));
+	dio = __iomap_dio_rw(iocb, from, &btrfs_dio_iomap_ops, &btrfs_dio_ops,
+			     0);
 
 	btrfs_inode_unlock(inode, ilock_flags);
 
@@ -3622,8 +3622,7 @@ static ssize_t btrfs_direct_read(struct kiocb *iocb, struct iov_iter *to)
 		return 0;
 
 	btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED);
-	ret = iomap_dio_rw(iocb, to, &btrfs_dio_iomap_ops, &btrfs_dio_ops,
-			   is_sync_kiocb(iocb));
+	ret = iomap_dio_rw(iocb, to, &btrfs_dio_iomap_ops, &btrfs_dio_ops, 0);
 	btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
 	return ret;
 }
fs/ext4/file.c +2 -3
index 3ed8c048fb12c5..5d93495bfc6224 100644
@@ -74,8 +74,7 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
 		return generic_file_read_iter(iocb, to);
 	}
 
-	ret = iomap_dio_rw(iocb, to, &ext4_iomap_ops, NULL,
-			   is_sync_kiocb(iocb));
+	ret = iomap_dio_rw(iocb, to, &ext4_iomap_ops, NULL, 0);
 	inode_unlock_shared(inode);
 
 	file_accessed(iocb->ki_filp);
@@ -550,7 +549,7 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	if (ilock_shared)
 		iomap_ops = &ext4_iomap_overwrite_ops;
 	ret = iomap_dio_rw(iocb, from, iomap_ops, &ext4_dio_write_ops,
-			   is_sync_kiocb(iocb) || unaligned_io || extend);
+			   (unaligned_io || extend) ? IOMAP_DIO_FORCE_WAIT : 0);
 	if (ret == -ENOTBLK)
 		ret = 0;
 
fs/gfs2/file.c +2 -5
index b39b339feddc93..89609c2997177a 100644
@@ -797,9 +797,7 @@ static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to,
 	if (ret)
 		goto out_uninit;
 
-	ret = iomap_dio_rw(iocb, to, &gfs2_iomap_ops, NULL,
-			   is_sync_kiocb(iocb));
-
+	ret = iomap_dio_rw(iocb, to, &gfs2_iomap_ops, NULL, 0);
 	gfs2_glock_dq(gh);
 out_uninit:
 	gfs2_holder_uninit(gh);
@@ -833,8 +831,7 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,
 	if (offset + len > i_size_read(&ip->i_inode))
 		goto out;
 
-	ret = iomap_dio_rw(iocb, from, &gfs2_iomap_ops, NULL,
-			   is_sync_kiocb(iocb));
+	ret = iomap_dio_rw(iocb, from, &gfs2_iomap_ops, NULL, 0);
 	if (ret == -ENOTBLK)
 		ret = 0;
 out:
fs/iomap/direct-io.c +10 -9
index 933f234d5becd0..96dc72debc4b79 100644
@@ -420,7 +420,7 @@ iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
 struct iomap_dio *
 __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
-		bool wait_for_completion)
+		unsigned int dio_flags)
 {
 	struct address_space *mapping = iocb->ki_filp->f_mapping;
 	struct inode *inode = file_inode(iocb->ki_filp);
@@ -434,8 +434,8 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 	if (!count)
 		return NULL;
 
-	if (WARN_ON(is_sync_kiocb(iocb) && !wait_for_completion))
-		return ERR_PTR(-EIO);
+	if (is_sync_kiocb(iocb))
+		dio_flags |= IOMAP_DIO_FORCE_WAIT;
 
 	dio = kmalloc(sizeof(*dio), GFP_KERNEL);
 	if (!dio)
@@ -503,7 +503,8 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 			goto out_free_dio;
 		}
 
-		if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) {
+		if (!(dio_flags & IOMAP_DIO_FORCE_WAIT) &&
+		    !inode->i_sb->s_dio_done_wq) {
 			ret = sb_init_dio_done_wq(inode->i_sb);
 			if (ret < 0)
 				goto out_free_dio;
@@ -519,7 +520,7 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 		if (ret <= 0) {
 			/* magic error code to fall back to buffered I/O */
 			if (ret == -ENOTBLK) {
-				wait_for_completion = true;
+				dio_flags |= IOMAP_DIO_FORCE_WAIT;
 				ret = 0;
 			}
 			break;
@@ -566,9 +567,9 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 	 *	of the final reference, and we will complete and free it here
 	 *	after we got woken by the I/O completion handler.
 	 */
-	dio->wait_for_completion = wait_for_completion;
+	dio->wait_for_completion = dio_flags & IOMAP_DIO_FORCE_WAIT;
 	if (!atomic_dec_and_test(&dio->ref)) {
-		if (!wait_for_completion)
+		if (!(dio_flags & IOMAP_DIO_FORCE_WAIT))
 			return ERR_PTR(-EIOCBQUEUED);
 
 		for (;;) {
@@ -598,11 +599,11 @@ EXPORT_SYMBOL_GPL(__iomap_dio_rw);
 ssize_t
 iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
-		bool wait_for_completion)
+		unsigned int flags)
 {
 	struct iomap_dio *dio;
 
-	dio = __iomap_dio_rw(iocb, iter, ops, dops, wait_for_completion);
+	dio = __iomap_dio_rw(iocb, iter, ops, dops, flags);
 	if (IS_ERR_OR_NULL(dio))
 		return PTR_ERR_OR_ZERO(dio);
 	return iomap_dio_complete(dio);
fs/xfs/xfs_file.c +3 -4
index bffd7240cefb7f..b181db42f2f32f 100644
@@ -232,8 +232,7 @@ xfs_file_dio_read(
 	ret = xfs_ilock_iocb(iocb, XFS_IOLOCK_SHARED);
 	if (ret)
 		return ret;
-	ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL,
-			is_sync_kiocb(iocb));
+	ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL, 0);
 	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
 
 	return ret;
@@ -535,7 +534,7 @@ xfs_file_dio_write_aligned(
 	}
 	trace_xfs_file_direct_write(iocb, from);
 	ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
-			   &xfs_dio_write_ops, is_sync_kiocb(iocb));
+			   &xfs_dio_write_ops, 0);
 out_unlock:
 	if (iolock)
 		xfs_iunlock(ip, iolock);
@@ -603,7 +602,7 @@ xfs_file_dio_write_unaligned(
 	 */
 	trace_xfs_file_direct_write(iocb, from);
 	ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
-			   &xfs_dio_write_ops, true);
+			   &xfs_dio_write_ops, IOMAP_DIO_FORCE_WAIT);
 out_unlock:
 	if (iolock)
 		xfs_iunlock(ip, iolock);
fs/zonefs/super.c +2 -2
index bec47f2d074beb..0e7ab0bc00ae8e 100644
@@ -780,7 +780,7 @@ static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
 		ret = zonefs_file_dio_append(iocb, from);
 	else
 		ret = iomap_dio_rw(iocb, from, &zonefs_iomap_ops,
-				   &zonefs_write_dio_ops, sync);
+				   &zonefs_write_dio_ops, 0);
 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
 	    (ret > 0 || ret == -EIOCBQUEUED)) {
 		if (ret > 0)
@@ -917,7 +917,7 @@ static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
 		}
 		file_accessed(iocb->ki_filp);
 		ret = iomap_dio_rw(iocb, to, &zonefs_iomap_ops,
-				   &zonefs_read_dio_ops, is_sync_kiocb(iocb));
+				   &zonefs_read_dio_ops, 0);
 	} else {
 		ret = generic_file_read_iter(iocb, to);
 		if (ret == -EIO)
include/linux/iomap.h +4 -2
index 5bd3cac4df9cb4..e7865654dd0dca 100644
@@ -256,12 +256,14 @@ struct iomap_dio_ops {
 			struct bio *bio, loff_t file_offset);
 };
 
+#define IOMAP_DIO_FORCE_WAIT	(1 << 0)	/* force waiting for I/O */
+
 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
-		bool wait_for_completion);
+		unsigned int flags);
 struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
-		bool wait_for_completion);
+		unsigned int flags);
 ssize_t iomap_dio_complete(struct iomap_dio *dio);
 int iomap_dio_iopoll(struct kiocb *kiocb, bool spin);
 
Signature · 1 line
2.29.2
#16 lore raw

PATCH 9/10 iomap: add a IOMAP_DIO_NOALLOC flag

In reply to Christoph Hellwig

Add a flag to request that the iomap instances do not allocate blocks by translating it to another new IOMAP_NOALLOC flag.

---
 fs/iomap/direct-io.c  | 2 ++
 include/linux/iomap.h | 2 ++
 2 files changed, 4 insertions(+)
fs/iomap/direct-io.c +2 -0
index 96dc72debc4b79..a01f1d182685b4 100644
@@ -436,6 +436,8 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 
 	if (is_sync_kiocb(iocb))
 		dio_flags |= IOMAP_DIO_FORCE_WAIT;
+	if (dio_flags & IOMAP_DIO_NOALLOC)
+		flags |= IOMAP_NOALLOC;
 
 	dio = kmalloc(sizeof(*dio), GFP_KERNEL);
 	if (!dio)
include/linux/iomap.h +2 -0
index e7865654dd0dca..a92890d5dc6799 100644
@@ -122,6 +122,7 @@ struct iomap_page_ops {
 #define IOMAP_FAULT		(1 << 3) /* mapping for page fault */
 #define IOMAP_DIRECT		(1 << 4) /* direct I/O */
 #define IOMAP_NOWAIT		(1 << 5) /* do not block */
+#define IOMAP_NOALLOC		(1 << 6) /* do not allocate blocks */
 
 struct iomap_ops {
 	/*
@@ -257,6 +258,7 @@ struct iomap_dio_ops {
 };
 
 #define IOMAP_DIO_FORCE_WAIT	(1 << 0)	/* force waiting for I/O */
+#define IOMAP_DIO_NOALLOC	(1 << 1)	/* do not allocate blocks */
 
 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
Signature · 1 line
2.29.2
#17 lore raw

Re: [PATCH 09/10] iomap: add a IOMAP_DIO_NOALLOC flag

Quoted 2 lines · Christoph Hellwig

Add a flag to request that the iomap instances do not allocate blocks by translating it to another new IOMAP_NOALLOC flag.

Except "no allocation" that is not what XFS needs for concurrent sub-block DIO.

We are trying to avoid external sub-block IO outside the range of the user data IO (COW, sub-block zeroing, etc) so that we don't trash adjacent sub-block IO in flight. This means we can't do sub-block zeroing and that then means we can't map unwritten extents or allocate new extents for the sub-block IO. It also means the IO range cannot span EOF because that triggers unconditional sub-block zeroing in iomap_dio_rw_actor().

And because we may have to map multiple extents to fully span an IO range, we have to guarantee that subsequent extents for the IO are also written otherwise we have a partial write abort case. Hence we have single extent limitations as well.

So "no allocation" really doesn't describe what we want this flag to at all.

If we're going to use a flag for this specific functionality, let's call it what it is: IOMAP_DIO_UNALIGNED/IOMAP_UNALIGNED and do two things with it.

1. Make unaligned IO a formal part of the iomap_dio_rw() behaviour so it can do the common checks to for things that need exclusive serialisation for unaligned IO (i.e. avoid IO spanning EOF, abort if there are cached pages over the range, etc).

2. require the filesystem mapping callback do only allow unaligned IO into ranges that are contiguous and don't require mapping state changes or sub-block zeroing to be performed during the sub-block IO.

Cheers,

Dave.

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

Re: [PATCH 09/10] iomap: add a IOMAP_DIO_NOALLOC flag

Quoted 33 lines · Dave Chinner
Quoted 2 lines · Christoph Hellwig

Add a flag to request that the iomap instances do not allocate blocks by translating it to another new IOMAP_NOALLOC flag.

Except "no allocation" that is not what XFS needs for concurrent sub-block DIO.

We are trying to avoid external sub-block IO outside the range of the user data IO (COW, sub-block zeroing, etc) so that we don't trash adjacent sub-block IO in flight. This means we can't do sub-block zeroing and that then means we can't map unwritten extents or allocate new extents for the sub-block IO. It also means the IO range cannot span EOF because that triggers unconditional sub-block zeroing in iomap_dio_rw_actor().

And because we may have to map multiple extents to fully span an IO range, we have to guarantee that subsequent extents for the IO are also written otherwise we have a partial write abort case. Hence we have single extent limitations as well.

So "no allocation" really doesn't describe what we want this flag to at all.

If we're going to use a flag for this specific functionality, let's call it what it is: IOMAP_DIO_UNALIGNED/IOMAP_UNALIGNED and do two things with it.

1. Make unaligned IO a formal part of the iomap_dio_rw() behaviour so it can do the common checks to for things that need exclusive serialisation for unaligned IO (i.e. avoid IO spanning EOF, abort if there are cached pages over the range, etc).

Quoted 6 lines

2. require the filesystem mapping callback do only allow unaligned IO into ranges that are contiguous and don't require mapping state changes or sub-block zeroing to be performed during the sub-block IO.

Something I hadn't thought about before is whether applications might depend on current unaligned dio serialization for coherency and thus break if the kernel suddenly allows concurrent unaligned dio to pass through. Should this be something that is explicitly requested by userspace?

That aside, I agree that the DIO_UNALIGNED approach seems a bit more clear than NOALLOC, but TBH the more I look at this the more Christoph's first approach seems cleanest to me. It is a bit unfortunate to duplicate the mapping lookups and have the extra ILOCK cycle, but the lock is shared and only taken when I/O is unaligned. I don't really see why that is a show stopper yet it's acceptable to fall back to exclusive dio if the target range happens to be discontiguous (but otherwise mapped/written).

So I dunno... to me, I would start with that approach and then as the implementation soaks, perhaps see if we can find a way to optimize away the extra cycle and lookup. In the meantime, performance should still be improved significantly and the behavior fairly predictable. Anyways, I suspect Dave disagrees so that's just my .02. ;) I'll let you guys find some common ground and make a pass at whatever falls out...

Brian

Quoted 7 lines

Cheers,

Dave.

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

Re: [PATCH 09/10] iomap: add a IOMAP_DIO_NOALLOC flag

Quoted 41 lines · Brian Foster
Quoted 33 lines · Dave Chinner
Quoted 2 lines · Christoph Hellwig

Add a flag to request that the iomap instances do not allocate blocks by translating it to another new IOMAP_NOALLOC flag.

Except "no allocation" that is not what XFS needs for concurrent sub-block DIO.

We are trying to avoid external sub-block IO outside the range of the user data IO (COW, sub-block zeroing, etc) so that we don't trash adjacent sub-block IO in flight. This means we can't do sub-block zeroing and that then means we can't map unwritten extents or allocate new extents for the sub-block IO. It also means the IO range cannot span EOF because that triggers unconditional sub-block zeroing in iomap_dio_rw_actor().

And because we may have to map multiple extents to fully span an IO range, we have to guarantee that subsequent extents for the IO are also written otherwise we have a partial write abort case. Hence we have single extent limitations as well.

So "no allocation" really doesn't describe what we want this flag to at all.

If we're going to use a flag for this specific functionality, let's call it what it is: IOMAP_DIO_UNALIGNED/IOMAP_UNALIGNED and do two things with it.

1. Make unaligned IO a formal part of the iomap_dio_rw() behaviour so it can do the common checks to for things that need exclusive serialisation for unaligned IO (i.e. avoid IO spanning EOF, abort if there are cached pages over the range, etc).

Quoted 6 lines

2. require the filesystem mapping callback do only allow unaligned IO into ranges that are contiguous and don't require mapping state changes or sub-block zeroing to be performed during the sub-block IO.

Quoted 5 lines

Something I hadn't thought about before is whether applications might depend on current unaligned dio serialization for coherency and thus break if the kernel suddenly allows concurrent unaligned dio to pass through. Should this be something that is explicitly requested by userspace?

If applications are relying on an undocumented, implementation specific behaviour of a filesystem that only occurs for IOs of a certain size for implicit data coherency between independent, non-overlapping DIOs and/or page cache IO, then they are already broken and need fixing because that behaviour is not guaranteed to occur. e.g. 512 byte block size filesystem does not provide such serialisation, so if the app depends on 512 byte DIOs being serialised completely by the filesytem then it already fails on 512 byte block size filesystems.

So, no, we simply don't care about breaking broken applications that are already broken.

Quoted 8 lines

That aside, I agree that the DIO_UNALIGNED approach seems a bit more clear than NOALLOC, but TBH the more I look at this the more Christoph's first approach seems cleanest to me. It is a bit unfortunate to duplicate the mapping lookups and have the extra ILOCK cycle, but the lock is shared and only taken when I/O is unaligned. I don't really see why that is a show stopper yet it's acceptable to fall back to exclusive dio if the target range happens to be discontiguous (but otherwise mapped/written).

Unnecessary lock cycles in the fast path are always bad. The whole reason this change is being done is for performance to bring it up to par with block aligned IO. Adding an extra lock cycle to the ILOCK on every IO will halve the performance on high IOPs hardware because the ILOCK will be directly exposed to userspace IO submission and hence become the contention point instead of the IOLOCK.

IOWs, the fact taht we take the ILOCK 2x per IO instead of once means that the ILOCK becomes the performance limiting lock (because even shared locking causes cacheline contention) and changes the entire lock profile for the IO path when unaligned IO is being done.

This is also ignoring the fact that the ILOCK is held in exclusive mode during IO completion while doing file size and extent manipulation transactions. IOWs we can block waiting on IO completion before we even decide if we can do the IO with shared locking. Hence there are new IO submission serialisation points in the fast path that will also slow down the cases where we have to do exclusive locking....

So, yeah, I can only see bad things occurring by lifting the ILOCK up into the high level IO path. And, of course, once it's taken there, people will find new reasons to expand it's scope and the problems will only get worse...

Quoted 3 lines

So I dunno... to me, I would start with that approach and then as the implementation soaks, perhaps see if we can find a way to optimize away the extra cycle and lookup.

I don't see how we can determine if we can do the unlaigned IO holding a shared lock without doing an extent lookup. It's the underlying extent state that makes shared locking possible, and I can't think of any other state we can look at to make this decision.

Hence I think this path is simply a dead end with no possibility of further optimisation. Of course, if you can solve the problem without needing an extent lookup, then we can talk about how to avoid racing with actual extent mapping changes done under the ILOCK... :)

Cheers,

Dave.

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

Re: [PATCH 09/10] iomap: add a IOMAP_DIO_NOALLOC flag

Quoted 48 lines · Dave Chinner
Quoted 41 lines · Brian Foster
Quoted 33 lines · Dave Chinner
Quoted 2 lines · Christoph Hellwig

Add a flag to request that the iomap instances do not allocate blocks by translating it to another new IOMAP_NOALLOC flag.

Except "no allocation" that is not what XFS needs for concurrent sub-block DIO.

We are trying to avoid external sub-block IO outside the range of the user data IO (COW, sub-block zeroing, etc) so that we don't trash adjacent sub-block IO in flight. This means we can't do sub-block zeroing and that then means we can't map unwritten extents or allocate new extents for the sub-block IO. It also means the IO range cannot span EOF because that triggers unconditional sub-block zeroing in iomap_dio_rw_actor().

And because we may have to map multiple extents to fully span an IO range, we have to guarantee that subsequent extents for the IO are also written otherwise we have a partial write abort case. Hence we have single extent limitations as well.

So "no allocation" really doesn't describe what we want this flag to at all.

If we're going to use a flag for this specific functionality, let's call it what it is: IOMAP_DIO_UNALIGNED/IOMAP_UNALIGNED and do two things with it.

1. Make unaligned IO a formal part of the iomap_dio_rw() behaviour so it can do the common checks to for things that need exclusive serialisation for unaligned IO (i.e. avoid IO spanning EOF, abort if there are cached pages over the range, etc).

Quoted 6 lines

2. require the filesystem mapping callback do only allow unaligned IO into ranges that are contiguous and don't require mapping state changes or sub-block zeroing to be performed during the sub-block IO.

Quoted 5 lines

Something I hadn't thought about before is whether applications might depend on current unaligned dio serialization for coherency and thus break if the kernel suddenly allows concurrent unaligned dio to pass through. Should this be something that is explicitly requested by userspace?

Quoted 10 lines

If applications are relying on an undocumented, implementation specific behaviour of a filesystem that only occurs for IOs of a certain size for implicit data coherency between independent, non-overlapping DIOs and/or page cache IO, then they are already broken and need fixing because that behaviour is not guaranteed to occur. e.g. 512 byte block size filesystem does not provide such serialisation, so if the app depends on 512 byte DIOs being serialised completely by the filesytem then it already fails on 512 byte block size filesystems.

I'm not sure how the block size relates beyond just changing the alignment requirements..?

Quoted 3 lines

So, no, we simply don't care about breaking broken applications that are already broken.

I agree in general, but I'm not sure that helps us on the "don't break userspace" front. We can call userspace broken all we want, but if some application has such a workload that historically functions correctly due to this serialization and all of a sudden starts to cause data corruption because we decide to remove it, I fear we'd end up taking the blame regardless. :/

I wonder if other fs' provide similar concurrent unaligned dio support..? A quick look at ext4 shows it has similar logic to XFS, btrfs looks like it falls back to buffered I/O...

Quoted 30 lines
Quoted 8 lines

That aside, I agree that the DIO_UNALIGNED approach seems a bit more clear than NOALLOC, but TBH the more I look at this the more Christoph's first approach seems cleanest to me. It is a bit unfortunate to duplicate the mapping lookups and have the extra ILOCK cycle, but the lock is shared and only taken when I/O is unaligned. I don't really see why that is a show stopper yet it's acceptable to fall back to exclusive dio if the target range happens to be discontiguous (but otherwise mapped/written).

Unnecessary lock cycles in the fast path are always bad. The whole reason this change is being done is for performance to bring it up to par with block aligned IO. Adding an extra lock cycle to the ILOCK on every IO will halve the performance on high IOPs hardware because the ILOCK will be directly exposed to userspace IO submission and hence become the contention point instead of the IOLOCK.

IOWs, the fact taht we take the ILOCK 2x per IO instead of once means that the ILOCK becomes the performance limiting lock (because even shared locking causes cacheline contention) and changes the entire lock profile for the IO path when unaligned IO is being done.

This is also ignoring the fact that the ILOCK is held in exclusive mode during IO completion while doing file size and extent manipulation transactions. IOWs we can block waiting on IO completion before we even decide if we can do the IO with shared locking. Hence there are new IO submission serialisation points in the fast path that will also slow down the cases where we have to do exclusive locking....

Quoted 5 lines

So, yeah, I can only see bad things occurring by lifting the ILOCK up into the high level IO path. And, of course, once it's taken there, people will find new reasons to expand it's scope and the problems will only get worse...

I'm not saying the extra ilock cycle is free or even ideal. I'm questioning that the proposed alternatives provide complete functionality when things like unaligned dio that span mappings are always going to fall back to exclusive I/O. There's an obvious tradeoff there between performance and predictability that IMO isn't as cut and dry as you describe. I certainly don't consider that as bringing unaligned dio performance up to par with block aligned dio.

Quoted 9 lines
Quoted 3 lines

So I dunno... to me, I would start with that approach and then as the implementation soaks, perhaps see if we can find a way to optimize away the extra cycle and lookup.

I don't see how we can determine if we can do the unlaigned IO holding a shared lock without doing an extent lookup. It's the underlying extent state that makes shared locking possible, and I can't think of any other state we can look at to make this decision.

Not sure how you got "without doing an extent lookup" from my comment. :P I was referring to the extra/early lookup and lock cycle that we're discussing above wrt to the original series. These subsequent series already do without it, but to me they sacrifice functionality. I'm basically saying that it might be worth to try and make it work first, make it fast(er) second (or otherwise find a way to address the issue in the latest series).

Of course, based on the behavior of other fs' I'm not totally convinced this is a great idea in the first place, at least not without some kind of opt-in from userspace or perhaps broader community consensus..

Brian

Quoted 14 lines

Hence I think this path is simply a dead end with no possibility of further optimisation. Of course, if you can solve the problem without needing an extent lookup, then we can talk about how to avoid racing with actual extent mapping changes done under the ILOCK... :)

Cheers,

Dave.

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

Re: [PATCH 09/10] iomap: add a IOMAP_DIO_NOALLOC flag

Quoted 62 lines · Brian Foster
Quoted 45 lines · Dave Chinner
Quoted 39 lines · Brian Foster
Quoted 32 lines · Dave Chinner
Quoted 2 lines · Christoph Hellwig

Add a flag to request that the iomap instances do not allocate blocks by translating it to another new IOMAP_NOALLOC flag.

Except "no allocation" that is not what XFS needs for concurrent sub-block DIO.

We are trying to avoid external sub-block IO outside the range of the user data IO (COW, sub-block zeroing, etc) so that we don't trash adjacent sub-block IO in flight. This means we can't do sub-block zeroing and that then means we can't map unwritten extents or allocate new extents for the sub-block IO. It also means the IO range cannot span EOF because that triggers unconditional sub-block zeroing in iomap_dio_rw_actor().

And because we may have to map multiple extents to fully span an IO range, we have to guarantee that subsequent extents for the IO are also written otherwise we have a partial write abort case. Hence we have single extent limitations as well.

So "no allocation" really doesn't describe what we want this flag to at all.

If we're going to use a flag for this specific functionality, let's call it what it is: IOMAP_DIO_UNALIGNED/IOMAP_UNALIGNED and do two things with it.

1. Make unaligned IO a formal part of the iomap_dio_rw() behaviour so it can do the common checks to for things that need exclusive serialisation for unaligned IO (i.e. avoid IO spanning EOF, abort if there are cached pages over the range, etc).

Quoted 6 lines

2. require the filesystem mapping callback do only allow unaligned IO into ranges that are contiguous and don't require mapping state changes or sub-block zeroing to be performed during the sub-block IO.

Quoted 5 lines

Something I hadn't thought about before is whether applications might depend on current unaligned dio serialization for coherency and thus break if the kernel suddenly allows concurrent unaligned dio to pass through. Should this be something that is explicitly requested by userspace?

Quoted 10 lines

If applications are relying on an undocumented, implementation specific behaviour of a filesystem that only occurs for IOs of a certain size for implicit data coherency between independent, non-overlapping DIOs and/or page cache IO, then they are already broken and need fixing because that behaviour is not guaranteed to occur. e.g. 512 byte block size filesystem does not provide such serialisation, so if the app depends on 512 byte DIOs being serialised completely by the filesytem then it already fails on 512 byte block size filesystems.

I'm not sure how the block size relates beyond just changing the alignment requirements..?

Quoted 3 lines

So, no, we simply don't care about breaking broken applications that are already broken.

Quoted 6 lines

I agree in general, but I'm not sure that helps us on the "don't break userspace" front. We can call userspace broken all we want, but if some application has such a workload that historically functions correctly due to this serialization and all of a sudden starts to cause data corruption because we decide to remove it, I fear we'd end up taking the blame regardless. :/

I think it's unlikely. Application writers rarely know about such issues, so they can't knowingly depend on them. The sub-sub-genre of application writers who rely on dio/aio will be a lot more careful and wary of the filesystem.

In this particular case, triggering serialization also triggers blocking in io_submit, which is the aio/dio user's worst nightmare, by several orders of magnitude than the runner up. I have code to detect these cases and try to prevent serialization, or, when serialization is inevitable, do the serialization in userspace so my io_submits don't get blocked.

#22 lore raw

Re: [PATCH 09/10] iomap: add a IOMAP_DIO_NOALLOC flag

In reply to Brian Foster
Quoted 5 lines · Brian Foster

Something I hadn't thought about before is whether applications might depend on current unaligned dio serialization for coherency and thus break if the kernel suddenly allows concurrent unaligned dio to pass through. Should this be something that is explicitly requested by userspace?

direct I/O has always been documented as not being synchronized. Also for block devices you won't get any synchronization at all, down to the sector level.

Quoted 9 lines

That aside, I agree that the DIO_UNALIGNED approach seems a bit more clear than NOALLOC, but TBH the more I look at this the more Christoph's first approach seems cleanest to me. It is a bit unfortunate to duplicate the mapping lookups and have the extra ILOCK cycle, but the lock is shared and only taken when I/O is unaligned. I don't really see why that is a show stopper yet it's acceptable to fall back to exclusive dio if the target range happens to be discontiguous (but otherwise mapped/written).

I think both approaches have pros an cons. My original one (which really is yours as you suggested it) has the advantage of having a much simpler structure, and not limititing the non-exclusive I/O to a single extent. The refined version of Dave's approach avoids the extra one or two extent lookups, and any knowledge of extent state above the iomap layer.

#23 lore raw

Re: [PATCH 09/10] iomap: add a IOMAP_DIO_NOALLOC flag

In reply to Dave Chinner
Quoted 6 lines · Dave Chinner
Quoted 2 lines · Christoph Hellwig

Add a flag to request that the iomap instances do not allocate blocks by translating it to another new IOMAP_NOALLOC flag.

Except "no allocation" that is not what XFS needs for concurrent sub-block DIO.

Well, this is just a quick draft. I could not come up with a better name, so I picked on that explains most but not all of what is going on.

Quoted 3 lines

If we're going to use a flag for this specific functionality, let's call it what it is: IOMAP_DIO_UNALIGNED/IOMAP_UNALIGNED and do two things with it.

Sounds fine with me.

Quoted 6 lines

1. Make unaligned IO a formal part of the iomap_dio_rw() behaviour so it can do the common checks to for things that need exclusive serialisation for unaligned IO (i.e. avoid IO spanning EOF, abort if there are cached pages over the range, etc).

Note that these all writes already fall back to buffered I/O if invalidate_inode_pages2_range fails, so there must never be cached pages for direct I/O these days. This is different from NOWAIT I/O where we simply give up if there are any cached pages and don't even try to invalidate them.

Quoted 4 lines

2. require the filesystem mapping callback do only allow unaligned IO into ranges that are contiguous and don't require mapping state changes or sub-block zeroing to be performed during the sub-block IO.

Yeah.

#24 lore raw

PATCH 10/10 xfs: reduce exclusive locking on unaligned dio

In reply to Christoph Hellwig

From: Dave Chinner <[email protected]>

Attempt shared locking for unaligned DIO, but only if the the underlying extent is already allocated and in written state. On failure, retry with the existing exclusive locking.

Test case is fio randrw of 512 byte IOs using AIO and an iodepth of 32 IOs.

Vanilla:

READ: bw=4560KiB/s (4670kB/s), 4560KiB/s-4560KiB/s (4670kB/s-4670kB/s), io=134MiB (140MB), run=30001-30001msec
WRITE: bw=4567KiB/s (4676kB/s), 4567KiB/s-4567KiB/s (4676kB/s-4676kB/s), io=134MiB (140MB), run=30001-30001msec

Patched:
READ: bw=37.6MiB/s (39.4MB/s), 37.6MiB/s-37.6MiB/s (39.4MB/s-39.4MB/s), io=1127MiB (1182MB), run=30002-30002msec
WRITE: bw=37.6MiB/s (39.4MB/s), 37.6MiB/s-37.6MiB/s (39.4MB/s-39.4MB/s), io=1128MiB (1183MB), run=30002-30002msec

That's an improvement from ~18k IOPS to a ~150k IOPS, which is about the IOPS limit of the VM block device setup I'm testing on.

4kB block IO comparison:

READ: bw=296MiB/s (310MB/s), 296MiB/s-296MiB/s (310MB/s-310MB/s), io=8868MiB (9299MB), run=30002-30002msec
WRITE: bw=296MiB/s (310MB/s), 296MiB/s-296MiB/s (310MB/s-310MB/s), io=8878MiB (9309MB), run=30002-30002msec

Which is ~150k IOPS, same as what the test gets for sub-block AIO+DIO writes with this patch.

[hch: rebased, split noalloc from nowait]
---
 fs/xfs/xfs_file.c  | 87 ++++++++++++++++++++++++++++++++--------------
 fs/xfs/xfs_iomap.c | 31 ++++++++++++-----
 2 files changed, 84 insertions(+), 34 deletions(-)
fs/xfs/xfs_file.c +61 -26
index b181db42f2f32f..f06cfcc8bd163a 100644
@@ -544,22 +544,35 @@ xfs_file_dio_write_aligned(
 /*
  * Handle block unaligned direct IO writes
  *
- * In most cases direct IO writes will be done holding IOLOCK_SHARED, allowing
- * them to be done in parallel with reads and other direct IO writes.  However,
- * if the I/O is not aligned to filesystem blocks, the direct I/O layer may
- * need to do sub-block zeroing and that requires serialisation against other
- * direct I/Os to the same block. In this case we need to serialise the
- * submission of the unaligned I/Os so that we don't get racing block zeroing in
- * the dio layer.
+ * In most cases direct IO writes will be done holding IOLOCK_SHARED
+ * allowing them to be done in parallel with reads and other direct IO writes.
+ * However, if the IO is not aligned to filesystem blocks, the direct IO layer
+ * may need to do sub-block zeroing and that requires serialisation against other
+ * direct IOs to the same block. In the case where sub-block zeroing is not
+ * required, we can do concurrent sub-block dios to the same block successfully.
  *
- * To provide the same serialisation for AIO, we also need to wait for
+ * Hence we have two cases here - the shared, optimisitic fast path for written
+ * extents, and everything else that needs exclusive IO path access across the
+ * entire IO.
+ *
+ * For the first case, we do all the checks we need at the mapping layer in the
+ * DIO code as part of the existing NOWAIT infrastructure. Hence all we need to
+ * do to support concurrent subblock dio is first try a non-blocking submission.
+ * If that returns -EAGAIN, then we simply repeat the IO submission with full
+ * IO exclusivity guaranteed so that we avoid racing sub-block zeroing.
+ *
+ * The only wrinkle in this case is that the iomap DIO code always does
+ * partial tail sub-block zeroing for post-EOF writes. Hence for any IO that
+ * _ends_ past the current EOF we need to run with full exclusivity. Note that
+ * we also check for the start of IO being beyond EOF because then zeroing
+ * between the old EOF and the start of the IO is required and that also
+ * requires exclusivity. Hence we avoid lock cycles and blocking under
+ * IOCB_NOWAIT for this situation, too.
+ *
+ * To provide the exclusivity required when using AIO, we also need to wait for
  * outstanding IOs to complete so that unwritten extent conversion is completed
  * before we try to map the overlapping block. This is currently implemented by
  * hitting it with a big hammer (i.e. inode_dio_wait()).
- *
- * This means that unaligned dio writes always block. There is no "nowait" fast
- * path in this code - if IOCB_NOWAIT is set we simply return -EAGAIN up front
- * and we don't have to worry about that anymore.
  */
 static noinline ssize_t
 xfs_file_dio_write_unaligned(
@@ -567,13 +580,27 @@ xfs_file_dio_write_unaligned(
 	struct kiocb		*iocb,
 	struct iov_iter		*from)
 {
-	int			iolock = XFS_IOLOCK_EXCL;
+	size_t			isize = i_size_read(VFS_I(ip));
+	size_t			count = iov_iter_count(from);
+	int			iolock = XFS_IOLOCK_SHARED;
+	unsigned int		flags = IOMAP_DIO_NOALLOC;
 	ssize_t			ret;
 
-	/* unaligned dio always waits, bail */
-	if (iocb->ki_flags & IOCB_NOWAIT)
-		return -EAGAIN;
-	xfs_ilock(ip, iolock);
+	/*
+	 * Extending writes need exclusivity because of the sub-block zeroing
+	 * that the DIO code always does for partial tail blocks beyond EOF.
+	 */
+	if (iocb->ki_pos > isize || iocb->ki_pos + count >= isize) {
+retry_exclusive:
+		if (iocb->ki_flags & IOCB_NOWAIT)
+			return -EAGAIN;
+		iolock = XFS_IOLOCK_EXCL;
+		flags = IOMAP_DIO_FORCE_WAIT;
+	}
+
+	ret = xfs_ilock_iocb(iocb, iolock);
+	if (ret)
+		return ret;
 
 	/*
 	 * We can't properly handle unaligned direct I/O to reflink files yet,
@@ -590,19 +617,27 @@ xfs_file_dio_write_unaligned(
 		goto out_unlock;
 
 	/*
-	 * If we are doing unaligned I/O, we can't allow any other overlapping
-	 * I/O in-flight at the same time or we risk data corruption. Wait for
-	 * all other I/O to drain before we submit.
+	 * If we are doing exclusive unaligned IO, we can't allow any other
+	 * overlapping IO in-flight at the same time or we risk data corruption.
+	 * Wait for all other IO to drain before we submit.
 	 */
-	inode_dio_wait(VFS_I(ip));
+	if (!(flags & IOMAP_DIO_NOALLOC))
+		inode_dio_wait(VFS_I(ip));
 
-	/*
-	 * This must be the only I/O in-flight. Wait on it before we release the
-	 * iolock to prevent subsequent overlapping I/O.
-	 */
 	trace_xfs_file_direct_write(iocb, from);
 	ret = iomap_dio_rw(iocb, from, &xfs_direct_write_iomap_ops,
-			   &xfs_dio_write_ops, IOMAP_DIO_FORCE_WAIT);
+			   &xfs_dio_write_ops, flags);
+	/*
+	 * Retry unaligned IO with exclusive blocking semantics if the DIO
+	 * layer rejected it for mapping or locking reasons. If we are doing
+	 * nonblocking user IO, propagate the error.
+	 */
+	if (ret == -EAGAIN && !(iocb->ki_flags & IOCB_NOWAIT)) {
+		ASSERT(flags & IOMAP_DIO_NOALLOC);
+		xfs_iunlock(ip, iolock);
+		goto retry_exclusive;
+	}
+
 out_unlock:
 	if (iolock)
 		xfs_iunlock(ip, iolock);
fs/xfs/xfs_iomap.c +23 -8
index 7b9ff824e82d48..b0f709af09b4bc 100644
@@ -784,15 +784,30 @@ xfs_direct_write_iomap_begin(
 		goto allocate_blocks;
 
 	/*
-	 * NOWAIT IO needs to span the entire requested IO with a single map so
-	 * that we avoid partial IO failures due to the rest of the IO range not
-	 * covered by this map triggering an EAGAIN condition when it is
-	 * subsequently mapped and aborting the IO.
+	 * NOWAIT and NOALLOC IO needs to span the entire requested IO with a
+	 * single map so that we avoid partial IO failures due to the rest of
+	 * the IO range not covered by this map triggering an EAGAIN condition
+	 * when it is subsequently mapped and aborting the IO.
 	 */
-	if ((flags & IOMAP_NOWAIT) &&
-	    !imap_spans_range(&imap, offset_fsb, end_fsb)) {
+	if (flags & (IOMAP_NOWAIT | IOMAP_NOALLOC)) {
 		error = -EAGAIN;
-		goto out_unlock;
+		if (!imap_spans_range(&imap, offset_fsb, end_fsb))
+			goto out_unlock;
+	}
+
+	/*
+	 * For NOALLOC I/O we can't convert an unwritten extents if the I/O is
+	 * not block size aligned, as such a conversion would have to do
+	 * sub-block zeroing, and that can only be done under an exclusive
+	 * IOLOCK. Hence if this is not a written extent, return EAGAIN to tell
+	 * the caller to try again.
+	 */
+	if (flags & IOMAP_NOALLOC) {
+		error = -EAGAIN;
+		if (imap.br_state != XFS_EXT_NORM &&
+		    ((offset & mp->m_blockmask) ||
+		     ((offset + length) & mp->m_blockmask)))
+			goto out_unlock;
 	}
 
 	xfs_iunlock(ip, lockmode);
@@ -801,7 +816,7 @@ xfs_direct_write_iomap_begin(
 
 allocate_blocks:
 	error = -EAGAIN;
-	if (flags & IOMAP_NOWAIT)
+	if (flags & (IOMAP_NOWAIT | IOMAP_NOALLOC))
 		goto out_unlock;
 
 	/*
Signature · 1 line
2.29.2