Skip to content

Commit 622c6f8

Browse files
jankaramehmetb0
authored andcommitted
ext4: fix warning in ext4_dio_write_end_io()
BugLink: https://bugs.launchpad.net/bugs/2086242 commit 619f75d upstream. 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: <[email protected]> Fixes: 9156289 ("ext4: properly sync file size update after O_SYNC direct IO") Signed-off-by: Jan Kara <[email protected]> Reviewed-by: Ritesh Harjani (IBM) <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Koichiro Den <[email protected]> Signed-off-by: Roxana Nicolescu <[email protected]>
1 parent 409c280 commit 622c6f8

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

fs/ext4/file.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,10 @@ static void ext4_inode_extension_cleanup(struct inode *inode, bool need_trunc)
322322
return;
323323
}
324324
/*
325-
* If i_disksize got extended due to writeback of delalloc blocks while
326-
* the DIO was running we could fail to cleanup the orphan list in
327-
* ext4_handle_inode_extension(). Do it now.
325+
* If i_disksize got extended either due to writeback of delalloc
326+
* blocks or extending truncate while the DIO was running we could fail
327+
* to cleanup the orphan list in ext4_handle_inode_extension(). Do it
328+
* now.
328329
*/
329330
if (!list_empty(&EXT4_I(inode)->i_orphan) && inode->i_nlink) {
330331
handle_t *handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
@@ -359,10 +360,11 @@ static int ext4_dio_write_end_io(struct kiocb *iocb, ssize_t size,
359360
* blocks. But the code in ext4_iomap_alloc() is careful to use
360361
* zeroed/unwritten extents if this is possible; thus we won't leave
361362
* uninitialized blocks in a file even if we didn't succeed in writing
362-
* as much as we intended.
363+
* as much as we intended. Also we can race with truncate or write
364+
* expanding the file so we have to be a bit careful here.
363365
*/
364-
WARN_ON_ONCE(i_size_read(inode) < READ_ONCE(EXT4_I(inode)->i_disksize));
365-
if (pos + size <= READ_ONCE(EXT4_I(inode)->i_disksize))
366+
if (pos + size <= READ_ONCE(EXT4_I(inode)->i_disksize) &&
367+
pos + size <= i_size_read(inode))
366368
return size;
367369
return ext4_handle_inode_extension(inode, pos, size);
368370
}

0 commit comments

Comments
 (0)