Skip to contentSkip to conversation

iomap: fix error handling regressions

PATCH Superseded Andrea Parri 4 participants 16 messages Sep 24 – Sep 25

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover iomap: fix error handling regressions

Adding Carlos Maiolino to the To: list since patch 2 (new in v2) touches fs/xfs/ outside the iomap directory.

Fix three independent error handling regressions in iomap:

- clear the writeback context after a failed ->writeback_submit() call, preventing an already completed ioend from being submitted again;
- preserve an iomap iteration error when iomap_fiemap() has a pending extent to emit; and
- preserve a direct I/O data path error when sub-block tail zeroing succeeds.

The three fixes have no ordering dependencies and can be applied or backported independently. Each issue was reproduced before and after its respective fix.

Patch 2 is not a fix and carries no stable tag: it adds an XFS error tag (requested by Christoph) that lets patch 1's ->writeback_submit() failure be injected without the boot-param hack used for the original reproduction. I used it to write an xfstest reproducing the bug (list corruption caught via CONFIG_DEBUG_LIST on an unfixed kernel, clean writeback failure on a fixed one); I'll send that separately to [email protected] once this series is out for review.

Changes since v1:
- Patch 1: reworded the added comment to fit 80 columns (Christoph); answered Christoph's request for a reproducer in the commit message (fault-injected ->writeback_submit() failure on a reflinked XFS file, reliably hits list_add double add on the unfixed kernel); confirmed with Christoph that the final ->writeback_submit() call in iomap_writepages() needs no equivalent fix, since wpc is never reused after it; picked up the version-scoped stable tag Darrick suggested; picked up Brian's Reviewed-by.
- Patch 2: new, added per Christoph's request.
- Patch 3 (was 2/3): no code change; picked up the version-scoped stable tag Darrick suggested; picked up Reviewed-by tags from Brian, Darrick, and Christoph.
- Patch 4 (was 3/3): reworked per Christoph's suggestion to drop the zerror/ret handling entirely instead of just fixing its type, since iomap_dio_zero() can only fail via a can't-happen WARN_ON_ONCE() assert; this also resolves Darrick's question about the local variable's type, since there's no longer a local variable to type.

Andrea Parri (4):
iomap: don't resubmit an ioend after ->writeback_submit() failed
xfs: add an error tag to inject a ->writeback_submit() failure
iomap: don't lose a fiemap iteration error when emitting the last
extent
iomap: don't lose a failed direct I/O bio's error when zeroing the
tail

 fs/iomap/direct-io.c         | 19 +++++++------------
 fs/iomap/fiemap.c            |  8 ++++----
 fs/iomap/ioend.c             | 10 +++++++++-
 fs/xfs/libxfs/xfs_errortag.h |  6 ++++--
 fs/xfs/xfs_reflink.c         |  5 +++++
 5 files changed, 29 insertions(+), 19 deletions(-)

base-commit: 93f51579e7df248780214094418f205253383cc5

Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/4 iomap: don't resubmit an ioend after ->writeback_submit() failed

iomap_add_to_ioend() submits the pending ioend through ->writeback_submit() before allocating a new one for the current range. When the submission fails the helper completes the ioend with an error, but iomap_add_to_ioend() returns the error without clearing wpc->wb_ctx. iomap_writepages() then submits whatever wpc->wb_ctx points to, so the already completed ioend is submitted a second time.

For XFS the second bio_endio() lands in xfs_end_bio(), which list_add_tail()s the already linked ioend into ip->i_ioend_list. This corrupts the list and leaves a use-after-free/double-free window against the ioend completion worker. Reproduced with a fault-injected ->writeback_submit() failure on a reflinked XFS file with several CoW writeback ranges in flight: the unfixed kernel hits repeated "list_add double add" warnings from __list_add_valid_or_report(), the fixed kernel fails writeback cleanly.

Clear wpc->wb_ctx when ->writeback_submit() fails. The old iomap_submit_ioend() cleared the context unconditionally; that clear was lost when submission moved to iomap_ioend_writeback_submit(). The final ->writeback_submit() call in iomap_writepages() needs no equivalent fix:
it is the last thing the function does before returning, and every caller allocates its iomap_writepage_ctx on the stack for a single call, so wpc->wb_ctx is never read again afterwards.

Fixes f4fa7981fa26 ("iomap: hide ioends from the generic writeback code")
Cc <[email protected]> # v6.17
Assisted-by LLM
---
 fs/iomap/ioend.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
fs/iomap/ioend.c +9 -1
index 7bbbb417f9152..32ae292a84cbe 100644
@@ -246,8 +246,16 @@ ssize_t iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, struct folio *folio,
 new_ioend:
 		if (ioend) {
 			error = wpc->ops->writeback_submit(wpc, 0);
-			if (error)
+			if (error) {
+				/*
+				 * ->writeback_submit() completed the ioend
+				 * with an error, so drop the stale context;
+				 * iomap_writepages() would otherwise submit
+				 * it a second time.
+				 */
+				wpc->wb_ctx = NULL;
 				return error;
+			}
 		}
 		wpc->wb_ctx = ioend = iomap_alloc_ioend(wpc, pos, ioend_flags);
 	}
Signature · 1 line
2.53.0
#6 lore raw

PATCH v2 2/4 xfs: add an error tag to inject a ->writeback_submit() failure

In reply to Andrea Parri

The only in-tree way for xfs_writeback_submit() to fail is a failing xfs_reflink_convert_cow(), which requires a shared (reflinked) CoW extent. There was previously no way to exercise that failure path from userspace, which made it hard to write an xfstest for it, e.g. for "iomap: don't resubmit an ioend after ->writeback_submit() failed".

Add XFS_ERRTAG_WB_COW_CONVERT_ERROR to force xfs_reflink_convert_cow() to fail with -EIO, so that ->writeback_submit() failure and its callers' error handling can be exercised with error injection alone, without needing a corrupted or racy COW fork.

---
 fs/xfs/libxfs/xfs_errortag.h | 6 ++++--
 fs/xfs/xfs_reflink.c         | 5 +++++
 2 files changed, 9 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_errortag.h +4 -2
index 6de207fed2d89..e6b0864051b0a 100644
@@ -75,7 +75,8 @@
 #define XFS_ERRTAG_METAFILE_RESV_CRITICAL		45
 #define XFS_ERRTAG_FORCE_ZERO_RANGE			46
 #define XFS_ERRTAG_ZONE_RESET				47
-#define XFS_ERRTAG_MAX					48
+#define XFS_ERRTAG_WB_COW_CONVERT_ERROR		48
+#define XFS_ERRTAG_MAX					49
 
 /*
  * Random factors for above tags, 1 means always, 2 means 1/2 time, etc.
@@ -137,7 +138,8 @@ XFS_ERRTAG(WRITE_DELAY_MS,	write_delay_ms,		3000) \
 XFS_ERRTAG(EXCHMAPS_FINISH_ONE,	exchmaps_finish_one,	1) \
 XFS_ERRTAG(METAFILE_RESV_CRITICAL, metafile_resv_crit,	4) \
 XFS_ERRTAG(FORCE_ZERO_RANGE,	force_zero_range,	4) \
-XFS_ERRTAG(ZONE_RESET,		zone_reset,		1)
+XFS_ERRTAG(ZONE_RESET,		zone_reset,		1) \
+XFS_ERRTAG(WB_COW_CONVERT_ERROR, wb_cow_convert_error,	1)
 #endif /* XFS_ERRTAG */
 
 #endif /* __XFS_ERRORTAG_H_ */
fs/xfs/xfs_reflink.c +5 -0
index 4801361366359..104542b10d9e0 100644
@@ -34,6 +34,8 @@
 #include "xfs_rtalloc.h"
 #include "xfs_rtgroup.h"
 #include "xfs_metafile.h"
+#include "xfs_errortag.h"
+#include "xfs_error.h"
 
 /*
  * Copy on Write of Shared Blocks
@@ -346,6 +348,9 @@ xfs_reflink_convert_cow(
 
 	ASSERT(count != 0);
 
+	if (XFS_TEST_ERROR(mp, XFS_ERRTAG_WB_COW_CONVERT_ERROR))
+		return -EIO;
+
 	xfs_ilock(ip, XFS_ILOCK_EXCL);
 	error = xfs_reflink_convert_cow_locked(ip, offset_fsb, count_fsb);
 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
Signature · 1 line
2.53.0
#9 lore raw

PATCH v2 3/4 iomap: don't lose a fiemap iteration error when emitting the last extent

In reply to Andrea Parri

iomap_fiemap() emits extents one behind: iomap_fiemap_iter() flushes the previous extent and remembers the current one, and the remembered extent is written with FIEMAP_EXTENT_LAST after the iteration loop.

That final flush overwrites ret, so when ->iomap_begin() fails partway through the iteration the error is replaced by the result of iomap_to_fiemap() (zero on success) and iomap_fiemap() returns success with a truncated extent list whose last entry is wrongly marked as the last extent in the file. The pre-iomap_iter code returned the error from inside the loop, before flushing the pending extent.

Check for the iteration error before flushing the pending extent, so that real errors are propagated and only a successful iteration emits the final FIEMAP_EXTENT_LAST extent. -ENOENT (no mapping) is still not an error, and the pending extent is still emitted in that case.

Fixes 7892386d3571 ("iomap: switch iomap_fiemap to use iomap_iter")
Cc <[email protected]> # v5.15
Assisted-by LLM
---
 fs/iomap/fiemap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/iomap/fiemap.c +4 -4
index d11dadff82865..54b824b7edb5c 100644
@@ -76,15 +76,15 @@ int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,
 	while ((ret = iomap_iter(&iter, ops)) > 0)
 		iter.status = iomap_fiemap_iter(&iter, fi, &prev);
 
+	/* inode with no (attribute) mapping will give ENOENT */
+	if (ret < 0 && ret != -ENOENT)
+		return ret;
+
 	if (prev.type != IOMAP_HOLE) {
 		ret = iomap_to_fiemap(fi, &prev, FIEMAP_EXTENT_LAST);
 		if (ret < 0)
 			return ret;
 	}
-
-	/* inode with no (attribute) mapping will give ENOENT */
-	if (ret < 0 && ret != -ENOENT)
-		return ret;
 	return 0;
 }
 EXPORT_SYMBOL_GPL(iomap_fiemap);
Signature · 1 line
2.53.0
#10 lore raw

PATCH v2 4/4 iomap: don't lose a failed direct I/O bio's error when zeroing the tail

In reply to Andrea Parri

iomap_dio_bio_iter() falls through to the sub-block tail zeroing when the data bio submission fails, so that the rest of the block is still zeroed and stale data is not exposed. The zeroing result was assigned to ret, which overwrote the submission error with the successful zeroing result (zero) and the failed write was reported as success.

iomap_dio_zero() can only return an error from a can't-happen WARN_ON_ONCE() (nr_vecs exceeding BIO_MAX_VECS, which the existing comment there says "shall never be reached" for any in-tree filesystem), so it isn't a real runtime failure worth reporting to userspace, let alone one worth losing the actual submission error for. Make iomap_dio_zero() return void and drop the error handling at both call sites instead of threading the result through a separate variable.

Fixes 10553a91652d ("iomap: fix iomap_dio_zero() for fs bs > system page size")
Assisted-by LLM
---
 fs/iomap/direct-io.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)
fs/iomap/direct-io.c +7 -12
index 8b4039d16ce89..e00995c296c79 100644
@@ -296,8 +296,9 @@ u32 iomap_finish_ioend_direct(struct iomap_ioend *ioend)
 	return vec_count;
 }
 
-static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
-		loff_t pos, unsigned len)
+static void iomap_dio_zero(const struct iomap_iter *iter,
+			   struct iomap_dio *dio, loff_t pos,
+			   unsigned int len)
 {
 	struct inode *inode = file_inode(dio->iocb->ki_filp);
 	struct bio *bio;
@@ -305,14 +306,14 @@ static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
 	int nr_vecs = max(1, i_blocksize(inode) / folio_size(zero_folio));
 
 	if (!len)
-		return 0;
+		return;
 
 	/*
 	 * This limit shall never be reached as most filesystems have a
 	 * maximum blocksize of 64k.
 	 */
 	if (WARN_ON_ONCE(nr_vecs > BIO_MAX_VECS))
-		return -EINVAL;
+		return;
 
 	bio = iomap_dio_alloc_bio(iter, dio, nr_vecs,
 				  REQ_OP_WRITE | REQ_SYNC | REQ_IDLE);
@@ -328,8 +329,6 @@ static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
 		len -= io_len;
 	}
 	iomap_dio_submit_bio(iter, dio, bio, pos);
-
-	return 0;
 }
 
 static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter,
@@ -541,10 +540,7 @@ static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio)
 	if (need_zeroout) {
 		/* zero out from the start of the block to the write offset */
 		pad = pos & (fs_block_size - 1);
-
-		ret = iomap_dio_zero(iter, dio, pos - pad, pad);
-		if (ret)
-			goto out;
+		iomap_dio_zero(iter, dio, pos - pad, pad);
 	}
 
 	do {
@@ -582,8 +578,7 @@ static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio)
 		/* zero out from the end of the write to the end of the block */
 		pad = pos & (fs_block_size - 1);
 		if (pad)
-			ret = iomap_dio_zero(iter, dio, pos,
-					     fs_block_size - pad);
+			iomap_dio_zero(iter, dio, pos, fs_block_size - pad);
 	}
 out:
 	/* Undo iter limitation to current extent */
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v2 1/4] iomap: don't resubmit an ioend after ->writeback_submit() failed

In reply to Andrea Parri
Quoted 40 lines · Andrea Parri

iomap_add_to_ioend() submits the pending ioend through ->writeback_submit() before allocating a new one for the current range. When the submission fails the helper completes the ioend with an error, but iomap_add_to_ioend() returns the error without clearing wpc->wb_ctx. iomap_writepages() then submits whatever wpc->wb_ctx points to, so the already completed ioend is submitted a second time.

For XFS the second bio_endio() lands in xfs_end_bio(), which list_add_tail()s the already linked ioend into ip->i_ioend_list. This corrupts the list and leaves a use-after-free/double-free window against the ioend completion worker. Reproduced with a fault-injected ->writeback_submit() failure on a reflinked XFS file with several CoW writeback ranges in flight: the unfixed kernel hits repeated "list_add double add" warnings from __list_add_valid_or_report(), the fixed kernel fails writeback cleanly.

Clear wpc->wb_ctx when ->writeback_submit() fails. The old iomap_submit_ioend() cleared the context unconditionally; that clear was lost when submission moved to iomap_ioend_writeback_submit(). The final ->writeback_submit() call in iomap_writepages() needs no equivalent fix:
it is the last thing the function does before returning, and every caller allocates its iomap_writepage_ctx on the stack for a single call, so wpc->wb_ctx is never read again afterwards.

Fixes f4fa7981fa26 ("iomap: hide ioends from the generic writeback code")
Cc <[email protected]> # v6.17
Assisted-by LLM
---
 fs/iomap/ioend.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
fs/iomap/ioend.c +0 -0
index 7bbbb417f9152..32ae292a84cbe 100644
@@ -246,8 +246,16 @@ ssize_t iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, struct folio *folio,
 new_ioend:
 		if (ioend) {

fs/iomap/ioend.c ssize_t iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, struct folio *folio, · line 256

Quoted 10 lines
fs/iomap/ioend.c +8 -1
 			error = wpc->ops->writeback_submit(wpc, 0);
-			if (error)
+			if (error) {
+				/*
+				 * ->writeback_submit() completed the ioend
+				 * with an error, so drop the stale context;
+				 * iomap_writepages() would otherwise submit
+				 * it a second time.
+				 */
+				wpc->wb_ctx = NULL;

I looked around the codebase and saw that iomap_writepages also calls ->writeback_submit. Does that need to null out wb_ctx?

My guess is that none of the callers do anything with wpc after iomap_writepages returns so it's not harming anyone, but we should drop the stale context too, right?

--D

Quoted 9 lines
fs/iomap/ioend.c +1 -0
 				return error;
+			}
 		}
 		wpc->wb_ctx = ioend = iomap_alloc_ioend(wpc, pos, ioend_flags);
 	}
Signature · 1 line
2.53.0
Reviewed-by on v2 2/4
#7 lore raw

Re: [PATCH v2 2/4] xfs: add an error tag to inject a ->writeback_submit() failure

In reply to Andrea Parri
Quoted 7 lines · Andrea Parri

The only in-tree way for xfs_writeback_submit() to fail is a failing xfs_reflink_convert_cow(), which requires a shared (reflinked) CoW extent. There was previously no way to exercise that failure path from userspace, which made it hard to write an xfstest for it, e.g. for "iomap: don't resubmit an ioend after ->writeback_submit() failed".

Quoted 8 lines

Add XFS_ERRTAG_WB_COW_CONVERT_ERROR to force xfs_reflink_convert_cow() to fail with -EIO, so that ->writeback_submit() failure and its callers' error handling can be exercised with error injection alone, without needing a corrupted or racy COW fork.

Looks good to me,

--D

Quoted 56 lines
---
 fs/xfs/libxfs/xfs_errortag.h | 6 ++++--
 fs/xfs/xfs_reflink.c         | 5 +++++
 2 files changed, 9 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_errortag.h +4 -2
index 6de207fed2d89..e6b0864051b0a 100644
@@ -75,7 +75,8 @@
 #define XFS_ERRTAG_METAFILE_RESV_CRITICAL		45
 #define XFS_ERRTAG_FORCE_ZERO_RANGE			46
 #define XFS_ERRTAG_ZONE_RESET				47
-#define XFS_ERRTAG_MAX					48
+#define XFS_ERRTAG_WB_COW_CONVERT_ERROR		48
+#define XFS_ERRTAG_MAX					49
 
 /*
  * Random factors for above tags, 1 means always, 2 means 1/2 time, etc.
@@ -137,7 +138,8 @@ XFS_ERRTAG(WRITE_DELAY_MS,	write_delay_ms,		3000) \
 XFS_ERRTAG(EXCHMAPS_FINISH_ONE,	exchmaps_finish_one,	1) \
 XFS_ERRTAG(METAFILE_RESV_CRITICAL, metafile_resv_crit,	4) \
 XFS_ERRTAG(FORCE_ZERO_RANGE,	force_zero_range,	4) \
-XFS_ERRTAG(ZONE_RESET,		zone_reset,		1)
+XFS_ERRTAG(ZONE_RESET,		zone_reset,		1) \
+XFS_ERRTAG(WB_COW_CONVERT_ERROR, wb_cow_convert_error,	1)
 #endif /* XFS_ERRTAG */
 
 #endif /* __XFS_ERRORTAG_H_ */
fs/xfs/xfs_reflink.c +5 -0
index 4801361366359..104542b10d9e0 100644
@@ -34,6 +34,8 @@
 #include "xfs_rtalloc.h"
 #include "xfs_rtgroup.h"
 #include "xfs_metafile.h"
+#include "xfs_errortag.h"
+#include "xfs_error.h"
 
 /*
  * Copy on Write of Shared Blocks
@@ -346,6 +348,9 @@ xfs_reflink_convert_cow(
 
 	ASSERT(count != 0);
 
+	if (XFS_TEST_ERROR(mp, XFS_ERRTAG_WB_COW_CONVERT_ERROR))
+		return -EIO;
+
 	xfs_ilock(ip, XFS_ILOCK_EXCL);
 	error = xfs_reflink_convert_cow_locked(ip, offset_fsb, count_fsb);
 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
Signature · 1 line
2.53.0
Reviewed-by on v2 4/4
#11 lore raw

Re: [PATCH v2 4/4] iomap: don't lose a failed direct I/O bio's error when zeroing the tail

In reply to Andrea Parri
Quoted 15 lines · Andrea Parri

iomap_dio_bio_iter() falls through to the sub-block tail zeroing when the data bio submission fails, so that the rest of the block is still zeroed and stale data is not exposed. The zeroing result was assigned to ret, which overwrote the submission error with the successful zeroing result (zero) and the failed write was reported as success.

iomap_dio_zero() can only return an error from a can't-happen WARN_ON_ONCE() (nr_vecs exceeding BIO_MAX_VECS, which the existing comment there says "shall never be reached" for any in-tree filesystem), so it isn't a real runtime failure worth reporting to userspace, let alone one worth losing the actual submission error for. Make iomap_dio_zero() return void and drop the error handling at both call sites instead of threading the result through a separate variable.

Quoted 2 lines
Fixes 10553a91652d ("iomap: fix iomap_dio_zero() for fs bs > system page size")
Cc <[email protected]> # v6.12
Quoted 61 lines
Assisted-by LLM
---
 fs/iomap/direct-io.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)
fs/iomap/direct-io.c +6 -10
index 8b4039d16ce89..e00995c296c79 100644
@@ -296,8 +296,9 @@ u32 iomap_finish_ioend_direct(struct iomap_ioend *ioend)
 	return vec_count;
 }
 
-static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
-		loff_t pos, unsigned len)
+static void iomap_dio_zero(const struct iomap_iter *iter,
+			   struct iomap_dio *dio, loff_t pos,
+			   unsigned int len)
 {
 	struct inode *inode = file_inode(dio->iocb->ki_filp);
 	struct bio *bio;
@@ -305,14 +306,14 @@ static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
 	int nr_vecs = max(1, i_blocksize(inode) / folio_size(zero_folio));
 
 	if (!len)
-		return 0;
+		return;
 
 	/*
 	 * This limit shall never be reached as most filesystems have a
 	 * maximum blocksize of 64k.
 	 */
 	if (WARN_ON_ONCE(nr_vecs > BIO_MAX_VECS))
-		return -EINVAL;
+		return;
 
 	bio = iomap_dio_alloc_bio(iter, dio, nr_vecs,
 				  REQ_OP_WRITE | REQ_SYNC | REQ_IDLE);
@@ -328,8 +329,6 @@ static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
 		len -= io_len;
 	}
 	iomap_dio_submit_bio(iter, dio, bio, pos);
-
-	return 0;
 }
 
 static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter,
@@ -541,10 +540,7 @@ static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio)
 	if (need_zeroout) {
 		/* zero out from the start of the block to the write offset */
 		pad = pos & (fs_block_size - 1);
-
-		ret = iomap_dio_zero(iter, dio, pos - pad, pad);
-		if (ret)
-			goto out;
+		iomap_dio_zero(iter, dio, pos - pad, pad);
 	}
 
 	do {

fs/iomap/direct-io.c static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio) · line 581

Quoted 6 lines
fs/iomap/direct-io.c +1 -2
@@ -582,8 +578,7 @@ static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio)
 		/* zero out from the end of the write to the end of the block */
 		pad = pos & (fs_block_size - 1);
 		if (pad)
-			ret = iomap_dio_zero(iter, dio, pos,
-					     fs_block_size - pad);
+			iomap_dio_zero(iter, dio, pos, fs_block_size - pad);

I think this is the original cause of the bug, right? We might have already had a nonzero ret, and the assignment here blows that away. Right?

If the answer to that is yes, then

(I should probably whine about how changing the function signature of iomap_dio_zero should be a separate patch to make it more obvious what's the actual fix...)

--D

Quoted 7 lines
fs/iomap/direct-io.c +0 -0
 	}
 out:
 	/* Undo iter limitation to current extent */
Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCH v2 1/4] iomap: don't resubmit an ioend after ->writeback_submit() failed

In reply to Darrick J. Wong
Quoted 6 lines · Darrick J. Wong

I looked around the codebase and saw that iomap_writepages also calls ->writeback_submit. Does that need to null out wb_ctx?

My guess is that none of the callers do anything with wpc after iomap_writepages returns so it's not harming anyone, but we should drop the stale context too, right?

Your guess is right: that ->writeback_submit() call is the last thing iomap_writepages() does, and all of its callers (xfs, gfs2, zonefs, exfat, fuse, ntfs, ntfs3) have the wpc on the stack for that single call, so wb_ctx is never looked at again. Christoph made the same point on v1 [1], and the last paragraph of the commit message now spells it out.

Clearing it there as well would be harmless but dead code, so I'd rather leave it out; happy to add it if you feel strongly about it.

[1] https://lore.kernel.org/all/[email protected]/

Thanks!
Andrea

#12 lore raw

Re: [PATCH v2 4/4] iomap: don't lose a failed direct I/O bio's error when zeroing the tail

In reply to Darrick J. Wong
Quoted 3 lines · Darrick J. Wong
Quoted 1 line
Cc <[email protected]> # v6.12

Fixed for v3.

Quoted 11 lines

fs/iomap/direct-io.c static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio) · line 581

Quoted 7 lines
fs/iomap/direct-io.c +1 -2
@@ -582,8 +578,7 @@ static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio)
 		/* zero out from the end of the write to the end of the block */
 		pad = pos & (fs_block_size - 1);
 		if (pad)
-			ret = iomap_dio_zero(iter, dio, pos,
-					     fs_block_size - pad);
+			iomap_dio_zero(iter, dio, pos, fs_block_size - pad);

I think this is the original cause of the bug, right? We might have already had a nonzero ret, and the assignment here blows that away. Right?

Right. When iomap_dio_bio_iter_one() fails we break out of the loop with ret < 0 and fall through to the tail zeroing, and this assignment replaced that error with iomap_dio_zero()'s 0.

Quoted 2 lines

If the answer to that is yes, then

Thanks, picked up for v3.

Quoted 3 lines

(I should probably whine about how changing the function signature of iomap_dio_zero should be a separate patch to make it more obvious what's the actual fix...)

Fair point. FWIW, before 10553a91652d iomap_dio_zero() returned void and neither call site touched ret; that commit made it return int and added both "ret = iomap_dio_zero(...)" assignments. So the signature change here is really undoing the part of 10553a91652d that introduced the bug, and keeping it in one patch makes the stable backport self-contained. If you'd still prefer a one-line fix followed by the void conversion, I'm happy to split it.

Thanks!
Andrea

#4 lore raw

Re: [PATCH v2 1/4] iomap: don't resubmit an ioend after ->writeback_submit() failed

In reply to Andrea Parri
Quoted 15 lines · Andrea Parri
Quoted 6 lines · Darrick J. Wong

I looked around the codebase and saw that iomap_writepages also calls ->writeback_submit. Does that need to null out wb_ctx?

My guess is that none of the callers do anything with wpc after iomap_writepages returns so it's not harming anyone, but we should drop the stale context too, right?

Your guess is right: that ->writeback_submit() call is the last thing iomap_writepages() does, and all of its callers (xfs, gfs2, zonefs, exfat, fuse, ntfs, ntfs3) have the wpc on the stack for that single call, so wb_ctx is never looked at again. Christoph made the same point on v1 [1], and the last paragraph of the commit message now spells it out.

Quoted 4 lines

Clearing it there as well would be harmless but dead code, so I'd rather leave it out; happy to add it if you feel strongly about it.

[1] https://lore.kernel.org/all/[email protected]/

LOL ok thanks for the reminder. My brain is fried this week, in a good way. ;)

--D

Quoted 3 lines

Thanks!
Andrea

#13 lore raw

Re: [PATCH v2 4/4] iomap: don't lose a failed direct I/O bio's error when zeroing the tail

In reply to Andrea Parri
Quoted 32 lines · Andrea Parri
Quoted 3 lines · Darrick J. Wong
Quoted 1 line
Cc <[email protected]> # v6.12

Fixed for v3.

Quoted 11 lines

fs/iomap/direct-io.c static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio) · line 581

Quoted 7 lines
fs/iomap/direct-io.c +1 -2
@@ -582,8 +578,7 @@ static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio)
 		/* zero out from the end of the write to the end of the block */
 		pad = pos & (fs_block_size - 1);
 		if (pad)
-			ret = iomap_dio_zero(iter, dio, pos,
-					     fs_block_size - pad);
+			iomap_dio_zero(iter, dio, pos, fs_block_size - pad);

I think this is the original cause of the bug, right? We might have already had a nonzero ret, and the assignment here blows that away. Right?

Right. When iomap_dio_bio_iter_one() fails we break out of the loop with ret < 0 and fall through to the tail zeroing, and this assignment replaced that error with iomap_dio_zero()'s 0.

Quoted 2 lines

If the answer to that is yes, then

Thanks, picked up for v3.

Quoted 3 lines

(I should probably whine about how changing the function signature of iomap_dio_zero should be a separate patch to make it more obvious what's the actual fix...)

Quoted 7 lines

Fair point. FWIW, before 10553a91652d iomap_dio_zero() returned void and neither call site touched ret; that commit made it return int and added both "ret = iomap_dio_zero(...)" assignments. So the signature change here is really undoing the part of 10553a91652d that introduced the bug, and keeping it in one patch makes the stable backport self-contained. If you'd still prefer a one-line fix followed by the void conversion, I'm happy to split it.

I'd prefer that, though I don't have a strong opinion. Someone in another multiverse could have come along and decided "URrgghg, iomap_dio_zero doesn't need a farking return value!" and accidentally fixed this bug.

Eh, whatever, just leave it as it is. :)

--D

Quoted 3 lines

Thanks!
Andrea

#14 lore raw

Re: [PATCH v2 4/4] iomap: don't lose a failed direct I/O bio's error when zeroing the tail

In reply to Andrea Parri

fs/iomap/direct-io.c u32 iomap_finish_ioend_direct(struct iomap_ioend *ioend) · line 301

Quoted 5 lines
fs/iomap/direct-io.c +3 -2
-static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
-		loff_t pos, unsigned len)
+static void iomap_dio_zero(const struct iomap_iter *iter,
+			   struct iomap_dio *dio, loff_t pos,
+			   unsigned int len)

Please drop this reformatting. While not all of the kernel agrees, the two-tab indent style is technically much superior and we want to stick to it for iomap.

Otherwise thi looks great, thanks!

Reviewed-by Pankaj Raghav on v2 4/4
#15 lore raw

Re: [PATCH v2 4/4] iomap: don't lose a failed direct I/O bio's error when zeroing the tail

In reply to Andrea Parri
Quoted 11 lines · Andrea Parri

iomap_dio_bio_iter() falls through to the sub-block tail zeroing when the data bio submission fails, so that the rest of the block is still zeroed and stale data is not exposed. The zeroing result was assigned to ret, which overwrote the submission error with the successful zeroing result (zero) and the failed write was reported as success.

iomap_dio_zero() can only return an error from a can't-happen WARN_ON_ONCE() (nr_vecs exceeding BIO_MAX_VECS, which the existing comment there says "shall never be reached" for any in-tree filesystem), so it isn't a real runtime failure worth reporting to userspace, let alone one worth losing the actual submission error for.

I initially did not have a return value for this function but later I had to add it based on comments. But I missed the return value being overwritten.

Quoted 9 lines

Make iomap_dio_zero() return void and drop the error handling at both call sites instead of threading the result through a separate variable.

Fixes 10553a91652d ("iomap: fix iomap_dio_zero() for fs bs > system page size")
Assisted-by LLM

---

Looks good except the comment from Christoph regarding the reformatting.

--
Pankaj