From: Jeff Layton <jlayton@kernel.org>
To: Qu Wenruo <wqu@suse.com>, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH] btrfs: properly cleanup replace_task when the replace failed to start
Date: Wed, 12 Aug 2026 08:03:36 -0400 [thread overview]
Message-ID: <5f0a52c918ba270886df4e5479370f286aee1adf.camel@kernel.org> (raw)
In-Reply-To: <2bb108e8d3204506a80562fec03d0d365583631f.1786348282.git.wqu@suse.com>
I ran this through some LLM review and it seems to think that this fix
isn't complete. Pasting the review comments verbatim below:
On Mon, 2026-08-10 at 17:21 +0930, Qu Wenruo wrote:
> In the function btrfs_dev_replace_start(), we have several error paths
> that assigns replace_start without reverting it back to NULL.
This isn't a bug, but should replace_start be replace_task here?
> But since dev_replace->rwsem is incorrectly updated, a process
> triggering the update will no longer be protected from dev-replace's
> device list modification, thus later IO can get stale device info,
> triggering things like use-after-free.
The thing left incorrectly updated is dev_replace->replace_task, not
dev_replace->rwsem, right?
> diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
> index 72cba7fed942..5fc1dec88fb2 100644
> --- a/fs/btrfs/dev-replace.c
> +++ b/fs/btrfs/dev-replace.c
> @@ -633,7 +633,6 @@ static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
> goto leave;
>
> down_write(&dev_replace->rwsem);
> - dev_replace->replace_task = current;
> switch (dev_replace->replace_state) {
> case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
> case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
> @@ -647,6 +646,7 @@ static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
> goto leave;
> }
>
> + dev_replace->replace_task = current;
> dev_replace->cont_reading_from_srcdev_mode = read_src;
> dev_replace->srcdev = src_device;
> dev_replace->tgtdev = tgt_device;
> @@ -693,6 +693,7 @@ static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
> BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
> dev_replace->srcdev = NULL;
> dev_replace->tgtdev = NULL;
> + dev_replace->replace_task = NULL;
> up_write(&dev_replace->rwsem);
> goto leave;
> }
Both of the paths handled here are inside btrfs_dev_replace_start(). Is
the same reset needed in btrfs_dev_replace_finishing()? That function
only clears replace_task on the path that completes the swap:
fs/btrfs/dev-replace.c:btrfs_dev_replace_finishing() {
...
list_add(&tgt_device->dev_alloc_list, &fs_devices->alloc_list);
fs_devices->rw_devices++;
dev_replace->replace_task = NULL;
up_write(&dev_replace->rwsem);
...
}
The scrub_ret error path returns earlier. It clears srcdev and tgtdev and
moves the state to canceled, but leaves replace_task pointing at the task
that started the replace:
fs/btrfs/dev-replace.c:btrfs_dev_replace_finishing() {
...
down_write(&dev_replace->rwsem);
dev_replace->replace_state =
scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
: BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
dev_replace->tgtdev = NULL;
dev_replace->srcdev = NULL;
...
} else {
if (scrub_ret != -ECANCELED)
btrfs_err(fs_info, ...);
error:
up_write(&dev_replace->rwsem);
...
return scrub_ret;
}
...
}
An ordinary "btrfs replace cancel" ends up there:
btrfs_dev_replace_cancel()
btrfs_scrub_cancel()
btrfs_dev_replace_start()
ret = btrfs_scrub_dev() -> -ECANCELED
ret = btrfs_dev_replace_finishing(fs_info, ret)
-> error: ... return scrub_ret
The ioctl then returns to userspace with dev_replace->replace_task still
set to the task that ran the ioctl. Isn't that the same stale pointer the
commit message describes for the already-started case, only reachable
without a second replace or an allocation failure?
The two other early returns in btrfs_dev_replace_finishing(), the
btrfs_start_delalloc_roots() failure and the btrfs_start_transaction()
failure in the commit loop, leave it set as well.
There is a second effect once replace_task is stale but still compares
equal to a live task. btrfs_map_block() reads it twice:
fs/btrfs/volumes.c:btrfs_map_block() {
...
if (dev_replace->replace_task != current)
down_read(&dev_replace->rwsem);
dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
/*
* Hold the semaphore for read during the whole operation, write is
* requested at commit time but must wait.
*/
if (!dev_replace_is_ongoing && dev_replace->replace_task != current)
up_read(&dev_replace->rwsem);
...
}
If a later btrfs_dev_replace_start() assigns replace_task between those
two reads, the first test skips down_read() while the second one runs
up_read(). Can that release an rwsem this task never acquired?
--
Jeff Layton <jlayton@kernel.org>
next prev parent reply other threads:[~2026-08-12 12:03 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-10 7:51 [PATCH] btrfs: properly cleanup replace_task when the replace failed to start Qu Wenruo
2026-08-12 12:03 ` Jeff Layton [this message]
2026-08-12 21:41 ` Qu Wenruo
2026-08-12 22:24 ` Qu Wenruo
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=5f0a52c918ba270886df4e5479370f286aee1adf.camel@kernel.org \
--to=jlayton@kernel.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=wqu@suse.com \
/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