Skip to content

Commit 619f75d

Browse files
jankaratytso
authored andcommitted
ext4: fix warning in ext4_dio_write_end_io()
The syzbot has reported that it can hit the warning in ext4_dio_write_end_io() because i_size < i_disksize. Indeed the reproducer creates a race between DIO IO completion and truncate expanding the file and thus ext4_dio_write_end_io() sees an inconsistent inode state where i_disksize is already updated but i_size is not updated yet. Since we are careful when setting up DIO write and consider it extending (and thus performing the IO synchronously with i_rwsem held exclusively) whenever it goes past either of i_size or i_disksize, we can use the same test during IO completion without risking entering ext4_handle_inode_extension() without i_rwsem held. This way we make it obvious both i_size and i_disksize are large enough when we report DIO completion without relying on unreliable WARN_ON. Reported-by: <syzbot+47479b71cdfc78f56d30@syzkaller.appspotmail.com> Fixes: 9156289 ("ext4: properly sync file size update after O_SYNC direct IO") Signed-off-by: Jan Kara <jack@suse.cz> Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Link: https://lore.kernel.org/r/20231130095653.22679-1-jack@suse.cz Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 6a3afb6 commit 619f75d

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

fs/ext4/file.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,10 @@ static void ext4_inode_extension_cleanup(struct inode *inode, ssize_t count)
349349
return;
350350
}
351351
/*
352-
* If i_disksize got extended due to writeback of delalloc blocks while
353-
* the DIO was running we could fail to cleanup the orphan list in
354-
* ext4_handle_inode_extension(). Do it now.
352+
* If i_disksize got extended either due to writeback of delalloc
353+
* blocks or extending truncate while the DIO was running we could fail
354+
* to cleanup the orphan list in ext4_handle_inode_extension(). Do it
355+
* now.
355356
*/
356357
if (!list_empty(&EXT4_I(inode)->i_orphan) && inode->i_nlink) {
357358
handle_t *handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
@@ -386,10 +387,11 @@ static int ext4_dio_write_end_io(struct kiocb *iocb, ssize_t size,
386387
* blocks. But the code in ext4_iomap_alloc() is careful to use
387388
* zeroed/unwritten extents if this is possible; thus we won't leave
388389
* uninitialized blocks in a file even if we didn't succeed in writing
389-
* as much as we intended.
390+
* as much as we intended. Also we can race with truncate or write
391+
* expanding the file so we have to be a bit careful here.
390392
*/
391-
WARN_ON_ONCE(i_size_read(inode) < READ_ONCE(EXT4_I(inode)->i_disksize));
392-
if (pos + size <= READ_ONCE(EXT4_I(inode)->i_disksize))
393+
if (pos + size <= READ_ONCE(EXT4_I(inode)->i_disksize) &&
394+
pos + size <= i_size_read(inode))
393395
return size;
394396
return ext4_handle_inode_extension(inode, pos, size);
395397
}

0 commit comments

Comments
 (0)