iomap: don't revert iov_iter on partially completed buffered writes
Subsystems (2)
PATCH 1/1 iomap: don't revert iov_iter on partially completed buffered writes
Gregg reports that the iomap retry behavior for nonblocking (nowait) append writes is broken. The problem occurs when an append write is first submitted in non-blocking mode (i.e. via io_uring), partially completes before hitting -EAGAIN, and then is resubmitted from blocking context.
The specific problem is that at least one iteration of the loop in iomap_write_iter() completes in non-blocking context and thus has bumped i_size. The next iteration hits -EAGAIN, reverts the iov_iter and returns. io_uring retries the entire append write from blocking context, but since i_size has already been increased, the data that was partially written on the first attempt is rewritten at the new i_size. This is essentially an intra-write data corruption since the data written to the file does not reflect the write from userspace.
This problem is already fixed on master as of commit 1a1a3b574b97 ("iomap: advance the iter directly on buffered writes"). That commit was primarily intended to clean up iomap iter state tracking, but it also happened to remove the iov_iter revert and thus accidentally fix this problem as well. Without the revert, iomap will commit partial progress internally and loop once more before it more than likely hits -EAGAIN and returns partial progress consistent with the inode updates. This means the blocking retry from io_uring will pick up where the first attempt left off at the current i_size and perform the remainder of the write correctly.
---
Hi all,
This relates to the discussion here[1]. Refer to the link for further details and the custom reproducer. Since this is a stable-only patch, I'd like to see at least one ack if possible from an iomap developer.
Note that this patch introduces an extra iomap iteration in the write iter path before an -EAGAIN will return, but I went this route because this is current upstream behavior and I didn't want to introduce novel behavior in -stable, as trivial as it might be. This was initially developed as a custom/selective backport of 1a1a3b574b97, but as it turns out this is also effectively a revert of commit 18e419f6e80a. So FWIW, this is somewhat historical behavior as well.
I plan to float a patch upstream to fix that loop wart soon. That would seem overkill to fix in stable to me, but if it does prove necessary we can revisit something like the custom version posted in [1] as a stable-worthy variant. I'd just prefer to only do that if/after that change proves acceptable upstream.
Finally, note that I'm not intimately familiar with -stable process so I'm just sending a 6.12.y version here. Earlier branches can either also include this, revert 18e419f6e80a directly, or I can post targeted patches if needed. Thoughts, reviews, flames appreciated.
Brian
fs/iomap/buffered-io.c | 4 ---- 1 file changed, 4 deletions(-)
fs/iomap/buffered-io.c +0 -4
@@ -1037,10 +1037,6 @@ static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i) } }while (iov_iter_count (i) && length); -if (status == -EAGAIN ) { -iov_iter_revert (i, total_written); -return -EAGAIN ; - }return total_written ? total_written : status; }
Signature · 1 line
2.54.0
Quoted 4 lines · Brian Foster
Finally, note that I'm not intimately familiar with -stable process so I'm just sending a 6.12.y version here. Earlier branches can either also include this, revert
18e419f6e80adirectly, or I can post targeted patches if needed. Thoughts, reviews, flames appreciated.
Queued for 6.12.y, 6.6.y and 6.1.y. Thanks!
--
Thanks,
Sasha