]> git.ipfire.org Git - thirdparty/kernel/stable.git/commit
btrfs: fix race with memory mapped writes when activating swap file
authorFilipe Manana <fdmanana@suse.com>
Fri, 29 Nov 2024 12:25:30 +0000 (12:25 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 2 Jan 2025 09:34:24 +0000 (10:34 +0100)
commitf6174bb982a831b96b8ebbd7c4aabe29d5ad97b7
treed4b8758da91940e8821fa0d18b35b470321b20e5
parentf6279a98db132da0cfff18712a1b06478c32007f
btrfs: fix race with memory mapped writes when activating swap file

commit 0525064bb82e50d59543b62b9d41a606198a4a44 upstream.

When activating the swap file we flush all delalloc and wait for ordered
extent completion, so that we don't miss any delalloc and extents before
we check that the file's extent layout is usable for a swap file and
activate the swap file. We are called with the inode's VFS lock acquired,
so we won't race with buffered and direct IO writes, however we can still
race with memory mapped writes since they don't acquire the inode's VFS
lock. The race window is between flushing all delalloc and locking the
whole file's extent range, since memory mapped writes lock an extent range
with the length of a page.

Fix this by acquiring the inode's mmap lock before we flush delalloc.

CC: stable@vger.kernel.org # 5.4+
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/btrfs/inode.c