Skip to contentSkip to conversation

buffer: avoid extending i_size after failed writes

PATCH In review Hungyu Lin 2 participants 3 messages Nov 3

Subsystems (2)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 buffer: avoid extending i_size after failed writes

generic_write_end() updates i_size to pos + copied even when a nonempty write copied no data. Since prefaulting moved to the slow write path, a pwrite() past EOF from an unreadable buffer can fail with EFAULT after extending the file to the write offset.

For example, on ext2, a one-byte pwrite() at offset 4096 into an empty file fails with EFAULT but leaves a 4096-byte file.

Require copied data before extending i_size for a nonempty write. Keep zero-length write_end() calls working, since generic_cont_expand_simple() uses them for expanding truncate operations.

Also call pagecache_isize_extended() only when i_size changed. Otherwise, blocking the size update would leave its endpoint beyond EOF and trigger its warning.

Fixes 665575cff098 ("filemap: move prefaulting out of hot write path")
---
 fs/buffer.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/buffer.c +2 -2
index 5d3852b726d6b113542e9761315cfdaf99f14587..f88ca7c079bee3462288fdebf57131da60b74798 100644
@@ -2203,7 +2203,7 @@ int generic_write_end(const struct kiocb *iocb, struct address_space *mapping,
 	 * But it's important to update i_size while still holding folio lock:
 	 * page writeout could otherwise come in and zero beyond i_size.
 	 */
-	if (pos + copied > inode->i_size) {
+	if ((copied || !len) && pos + copied > inode->i_size) {
 		i_size_write(inode, pos + copied);
 		i_size_changed = true;
 	}
@@ -2211,7 +2211,7 @@ int generic_write_end(const struct kiocb *iocb, struct address_space *mapping,
 	folio_unlock(folio);
 	folio_put(folio);
 
-	if (old_size < pos)
+	if (i_size_changed && old_size < pos)
 		pagecache_isize_extended(inode, old_size, pos);
 	/*
 	 * Don't mark the inode dirty under folio lock. First, it unnecessarily

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.43.0
#1 lore raw
Quoted 16 lines · Hungyu Lin

generic_write_end() updates i_size to pos + copied even when a nonempty write copied no data. Since prefaulting moved to the slow write path, a pwrite() past EOF from an unreadable buffer can fail with EFAULT after extending the file to the write offset.

For example, on ext2, a one-byte pwrite() at offset 4096 into an empty file fails with EFAULT but leaves a 4096-byte file.

Require copied data before extending i_size for a nonempty write. Keep zero-length write_end() calls working, since generic_cont_expand_simple() uses them for expanding truncate operations.

Also call pagecache_isize_extended() only when i_size changed. Otherwise, blocking the size update would leave its endpoint beyond EOF and trigger its warning.

Quoted 5 lines
Fixes 665575cff098 ("filemap: move prefaulting out of hot write path")

Jan is okay with what I proposed here:

https://lore.kernel.org/all/at-XtWBQg3jv3H8Z@thinkstation

Do you want to give it a try instead? Or want me to submit a proper patch?

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
author
#2 lore raw
Quoted 1 line

Do you want to give it a try instead? Or want me to submit a proper patch?

Thanks for the suggestion. I'll try your approach in generic_perform_write() and send a patch after testing.

Thanks,
Hungyu