On Mon, Nov 07, 2022 at 09:41:35AM +0000, fdmanana@xxxxxxxxxx wrote: > From: Filipe Manana <fdmanana@xxxxxxxx> > > commit 8184620ae21213d51eaf2e0bd4186baacb928172 upstream. > > When doing a direct IO write using a iocb with nowait and dsync set, we > end up not syncing the file once the write completes. > > This is because we tell iomap to not call generic_write_sync(), which > would result in calling btrfs_sync_file(), in order to avoid a deadlock > since iomap can call it while we are holding the inode's lock and > btrfs_sync_file() needs to acquire the inode's lock. The deadlock happens > only if the write happens synchronously, when iomap_dio_rw() calls > iomap_dio_complete() before it returns. Instead we do the sync ourselves > at btrfs_do_write_iter(). > > For a nowait write however we can end up not doing the sync ourselves at > at btrfs_do_write_iter() because the write could have been queued, and > therefore we get -EIOCBQUEUED returned from iomap in such case. That makes > us skip the sync call at btrfs_do_write_iter(), as we don't do it for > any error returned from btrfs_direct_write(). We can't simply do the call > even if -EIOCBQUEUED is returned, since that would block the task waiting > for IO, both for the data since there are bios still in progress as well > as potentially blocking when joining a log transaction and when syncing > the log (writing log trees, super blocks, etc). > > So let iomap do the sync call itself and in order to avoid deadlocks for > the case of synchronous writes (without nowait), use __iomap_dio_rw() and > have ourselves call iomap_dio_complete() after unlocking the inode. > > A test case will later be sent for fstests, after this is fixed in Linus' > tree. > > Fixes: 51bd9563b678 ("btrfs: fix deadlock due to page faults during direct IO reads and writes") > Reported-by: Марк Коренберг <socketpair@xxxxxxxxx> > Link: https://lore.kernel.org/linux-btrfs/CAEmTpZGRKbzc16fWPvxbr6AfFsQoLmz-Lcg-7OgJOZDboJ+SGQ@xxxxxxxxxxxxxx/ > CC: stable@xxxxxxxxxxxxxxx # 6.0+ > Signed-off-by: Filipe Manana <fdmanana@xxxxxxxx> > Signed-off-by: David Sterba <dsterba@xxxxxxxx> > --- > > The commit in the Fixes tag was backported to 5.15 stable releases, so > this patch is meant for 5.15.x and was tested on top of 5.15.77. Now queued up, thanks. greg k-h