From: Anand Jain <anand.jain@oracle.com>
To: fdmanana@kernel.org, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH v2 3/3] btrfs: fix race between swap file activation and snapshot creation
Date: Thu, 11 Feb 2021 01:19:46 +0800 [thread overview]
Message-ID: <49bc4f75-e1ae-74c3-7a8b-c5aba2457857@oracle.com> (raw)
In-Reply-To: <e7669602a731fc542034cbe933b067326c5aa3ad.1612529182.git.fdmanana@suse.com>
On 05/02/2021 20:55, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
>
> When creating a snapshot we check if the current number of swap files, in
> the root, is non-zero, and if it is, we error out and warn that we can not
> create the snapshot because there are active swap files.
>
> However this is racy because when a task started activation of a swap
> file, another task might have started already snapshot creation and might
> have seen the counter for the number of swap files as zero. This means
> that after the swap file is activated we may end up with a snapshot of the
> same root successfully created, and therefore when the first write to the
> swap file happens it has to fall back into COW mode, which should never
> happen for active swap files.
>
> Basically what can happen is:
>
> 1) Task A starts snapshot creation and enters ioctl.c:create_snapshot().
> There it sees that root->nr_swapfiles has a value of 0 so it continues;
>
> 2) Task B enters btrfs_swap_activate(). It is not aware that another task
> started snapshot creation but it did not finish yet. It increments
> root->nr_swapfiles from 0 to 1;
>
> 3) Task B checks that the file meets all requirements to be an active
> swap file - it has NOCOW set, there are no snapshots for the inode's
> root at the moment, no file holes, no reflinked extents, etc;
>
> 4) Task B returns success and now the file is an active swap file;
>
> 5) Task A commits the transaction to create the snapshot and finishes.
> The swap file's extents are now shared between the original root and
> the snapshot;
>
> 6) A write into an extent of the swap file is attempted - there is a
> snapshot of the file's root, so we fall back to COW mode and therefore
> the physical location of the extent changes on disk.
>
> So fix this by taking the snapshot lock during swap file activation before
> locking the extent range, as that is the order in which we lock these
> during buffered writes.
>
> Fixes: ed46ff3d42378 ("Btrfs: support swap files")
> Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Thanks, Anand
> ---
> fs/btrfs/inode.c | 21 +++++++++++++++++++--
> 1 file changed, 19 insertions(+), 2 deletions(-)
>
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 715ae1320383..a9fb6a4eb9dd 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -10116,7 +10116,8 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
> sector_t *span)
> {
> struct inode *inode = file_inode(file);
> - struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
> + struct btrfs_root *root = BTRFS_I(inode)->root;
> + struct btrfs_fs_info *fs_info = root->fs_info;
> struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
> struct extent_state *cached_state = NULL;
> struct extent_map *em = NULL;
> @@ -10167,13 +10168,27 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
> "cannot activate swapfile while exclusive operation is running");
> return -EBUSY;
> }
> +
> + /*
> + * Prevent snapshot creation while we are activating the swap file.
> + * We do not want to race with snapshot creation. If snapshot creation
> + * already started before we bumped nr_swapfiles from 0 to 1 and
> + * completes before the first write into the swap file after it is
> + * activated, than that write would fallback to COW.
> + */
> + if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
> + btrfs_exclop_finish(fs_info);
> + btrfs_warn(fs_info,
> + "cannot activate swapfile because snapshot creation is in progress");
> + return -EINVAL;
> + }
> /*
> * Snapshots can create extents which require COW even if NODATACOW is
> * set. We use this counter to prevent snapshots. We must increment it
> * before walking the extents because we don't want a concurrent
> * snapshot to run after we've already checked the extents.
> */
> - atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
> + atomic_inc(&root->nr_swapfiles);
>
> isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
>
> @@ -10319,6 +10334,8 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
> if (ret)
> btrfs_swap_deactivate(file);
>
> + btrfs_drew_write_unlock(&root->snapshot_lock);
> +
> btrfs_exclop_finish(fs_info);
>
> if (ret)
>
next prev parent reply other threads:[~2021-02-10 17:21 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-03 11:17 [PATCH 0/4] btrfs: fix a couple swapfile support bugs fdmanana
2021-02-03 11:17 ` [PATCH 1/4] btrfs: avoid checking for RO block group twice during nocow writeback fdmanana
2021-02-04 7:47 ` Anand Jain
2021-02-03 11:17 ` [PATCH 2/4] btrfs: fix race between writes to swap files and scrub fdmanana
2021-02-04 8:48 ` Anand Jain
2021-02-04 10:11 ` Filipe Manana
2021-02-05 7:44 ` Anand Jain
2021-02-05 12:54 ` Filipe Manana
2021-02-03 11:17 ` [PATCH 3/4] btrfs: remove no longer used function btrfs_extent_readonly() fdmanana
2021-02-03 11:17 ` [PATCH 4/4] btrfs: fix race between swap file activation and snapshot creation fdmanana
2021-02-05 12:55 ` [PATCH v2 0/3] btrfs: fix a couple swapfile support bugs fdmanana
2021-02-05 12:55 ` [PATCH v2 1/3] btrfs: avoid checking for RO block group twice during nocow writeback fdmanana
2021-02-10 12:28 ` Anand Jain
2021-02-05 12:55 ` [PATCH v2 2/3] btrfs: fix race between writes to swap files and scrub fdmanana
2021-02-10 16:54 ` Anand Jain
2021-02-05 12:55 ` [PATCH v2 3/3] btrfs: fix race between swap file activation and snapshot creation fdmanana
2021-02-10 17:19 ` Anand Jain [this message]
2021-02-10 22:15 ` [PATCH v2 0/3] btrfs: fix a couple swapfile support bugs David Sterba
2021-02-10 15:30 ` [PATCH 0/4] " Josef Bacik
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=49bc4f75-e1ae-74c3-7a8b-c5aba2457857@oracle.com \
--to=anand.jain@oracle.com \
--cc=fdmanana@kernel.org \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).