* [PATCH v3 1/3] btrfs: scrub: add cancel/pause/removed bg checks for raid56 parity stripes
2025-10-19 0:45 [PATCH v3 0/3] btrfs: scrub: enhance freezing and signal handling Qu Wenruo
@ 2025-10-19 0:45 ` Qu Wenruo
2025-10-19 0:45 ` [PATCH v3 2/3] btrfs: scrub: cancel the run if the process or fs is being frozen Qu Wenruo
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Qu Wenruo @ 2025-10-19 0:45 UTC (permalink / raw)
To: linux-btrfs; +Cc: safinaskar, Filipe Manana
For raid56, data and parity stripes are handled differently.
For data stripes they are handled just like regular RAID1/RAID10 stripes,
going through the regular scrub_simple_mirror().
But for parity stripes we have to read out all involved data stripes and
do any needed verification and repair, then scrub the parity stripe.
This process will take a much longer time than a regular stripe, but
unlike scrub_simple_mirror(), we do not check if we should cancel/pause
or the block group is already removed.
Aligned the behavior of scrub_raid56_parity_stripe() to
scrub_simple_mirror(), by adding:
- Cancel check
- Pause check
- Removed block group check
Since those checks are the same from the scrub_simple_mirror(), also
update the comments of scrub_simple_mirror() by:
- Remove too obvious comments
We do not need extra comments on what we're checking, it's really too
obvious.
- Remove a stale comment about pausing
Now the scrub is always queuing all involved stripes, and submit them
in one go, there is no more submission part during pausing.
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
fs/btrfs/scrub.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index fe266785804e..c3e7e543d350 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -2091,6 +2091,20 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
ASSERT(sctx->raid56_data_stripes);
+ if (atomic_read(&fs_info->scrub_cancel_req) ||
+ atomic_read(&sctx->cancel_req))
+ return -ECANCELED;
+
+ if (atomic_read(&fs_info->scrub_pause_req))
+ scrub_blocked_if_needed(fs_info);
+
+ spin_lock(&bg->lock);
+ if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) {
+ spin_unlock(&bg->lock);
+ return 0;
+ }
+ spin_unlock(&bg->lock);
+
/*
* For data stripe search, we cannot reuse the same extent/csum paths,
* as the data stripe bytenr may be smaller than previous extent. Thus
@@ -2261,18 +2275,15 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx,
u64 found_logical = U64_MAX;
u64 cur_physical = physical + cur_logical - logical_start;
- /* Canceled? */
if (atomic_read(&fs_info->scrub_cancel_req) ||
atomic_read(&sctx->cancel_req)) {
ret = -ECANCELED;
break;
}
- /* Paused? */
- if (atomic_read(&fs_info->scrub_pause_req)) {
- /* Push queued extents */
+
+ if (atomic_read(&fs_info->scrub_pause_req))
scrub_blocked_if_needed(fs_info);
- }
- /* Block group removed? */
+
spin_lock(&bg->lock);
if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) {
spin_unlock(&bg->lock);
--
2.51.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v3 2/3] btrfs: scrub: cancel the run if the process or fs is being frozen
2025-10-19 0:45 [PATCH v3 0/3] btrfs: scrub: enhance freezing and signal handling Qu Wenruo
2025-10-19 0:45 ` [PATCH v3 1/3] btrfs: scrub: add cancel/pause/removed bg checks for raid56 parity stripes Qu Wenruo
@ 2025-10-19 0:45 ` Qu Wenruo
2025-10-19 0:45 ` [PATCH v3 3/3] btrfs: scrub: cancel the run if there is a pending signal Qu Wenruo
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Qu Wenruo @ 2025-10-19 0:45 UTC (permalink / raw)
To: linux-btrfs; +Cc: safinaskar, Filipe Manana, Chris Murphy
It's a known bug that btrfs scrub/dev-replace can prevent the system
from suspending.
There are at least two factors involved:
- Holding super_block::s_writers for the whole scrub/dev-replace
duration
We hold that percpu rw semaphore through mnt_want_write_file() for the
whole scrub/dev-replace duration.
That will prevent the fs being frozen, which can be initiated by
either the user (e.g. fsfreeze) or power management suspension/hiberation.
- Stuck in the kernel space for a long time
During suspension all user processes (and some kernel threads) will
be frozen.
But if a user space progress has fallen into kernel (scrub ioctl) and
do not return for a long time, it will make process freezing time out.
Unfortunately scrub/dev-replace is a long running ioctl, and it will
prevent the btrfs process from returning to the user space, thus make pm
suspension/hiberation time out.
Address them in one go:
- Introduce a new helper should_cancel_scrub()
Which includes the existing cancel request and new fs/process freezing
checks.
Here we have to check both fs and process freezing for pm
suspension/hiberation.
Pm can be configured to freeze fses before processes.
(The current default is not to freeze fses, but planned to freeze the
fses as the new default)
Checking only fs freezing will fail pm without fs freezing, as the
process freezing will time out.
Checking only process freezing will fail pm with fs freezing since the
fs freezing happens before process freezing.
And the return value will indicate the reason, -ECANCLED for the
explicitly canceled runs, and -EINTR for fs freeze or pm reasons.
- Cancel the run if should_cancel_scrub() is true
Unfortunately canceling is the only feasible solution here, pausing is
not possible as we will still stay in the kernel space thus will still
prevent the process from being frozen.
This will cause a user impacting behavior change:
Dev-replace can be interrupted by pm, and there is no way to resume
but start from the beginning again.
This means dev-replace may fail on newer kernels, and end users will
need extra steps like using systemd-inhibit to prevent
suspension/hibernation, to get back the old uninterrupted behavior.
This behavior change will need extra documentation updates and
communication with projects involving scrub/dev-replace including
btrfs-progs.
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Link: https://lore.kernel.org/linux-btrfs/d93b2a2d-6ad9-4c49-809f-11d769a6f30a@app.fastmail.com/
Reported-by: Chris Murphy <lists@colorremedies.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
fs/btrfs/scrub.c | 53 +++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 46 insertions(+), 7 deletions(-)
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index c3e7e543d350..bbd5793c2a16 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -2069,6 +2069,47 @@ static int queue_scrub_stripe(struct scrub_ctx *sctx, struct btrfs_block_group *
return 0;
}
+/*
+ * Return 0 if we should not cancel the scrub.
+ * Return <0 if we need to cancel the scrub, returned value will
+ * indicate the reason:
+ * - -ECANCELED
+ * Being explicitly canceled through ioctl.
+ * - -EINTR
+ * Being interrupted by fs/process freezing.
+ */
+static int should_cancel_scrub(const struct scrub_ctx *sctx)
+{
+ struct btrfs_fs_info *fs_info = sctx->fs_info;
+
+ if (atomic_read(&fs_info->scrub_cancel_req) ||
+ atomic_read(&sctx->cancel_req))
+ return -ECANCELED;
+
+ /*
+ * The user (e.g. fsfreeze command) or power management (pm)
+ * suspension/hibernation can freeze the fs.
+ * And pm suspension/hibernation will also freeze all user processes.
+ *
+ * A user process can only be frozen when it is in the user space, thus
+ * we have to cancel the run so that the process can return to the user
+ * space.
+ *
+ * Furthermore we have to check both fs and process freezing, as pm can
+ * be configured to freeze the fses before processes.
+ *
+ * If we only check fs freezing, then suspension without fs freezing
+ * will timeout, as the process is still in the kernel space.
+ *
+ * If we only check process freezing, then suspension with fs freezing
+ * will timeout, as the running scrub will prevent the fs from being frozen.
+ */
+ if (fs_info->sb->s_writers.frozen > SB_UNFROZEN ||
+ freezing(current))
+ return -EINTR;
+ return 0;
+}
+
static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
struct btrfs_device *scrub_dev,
struct btrfs_block_group *bg,
@@ -2091,9 +2132,9 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
ASSERT(sctx->raid56_data_stripes);
- if (atomic_read(&fs_info->scrub_cancel_req) ||
- atomic_read(&sctx->cancel_req))
- return -ECANCELED;
+ ret = should_cancel_scrub(sctx);
+ if (ret < 0)
+ return ret;
if (atomic_read(&fs_info->scrub_pause_req))
scrub_blocked_if_needed(fs_info);
@@ -2275,11 +2316,9 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx,
u64 found_logical = U64_MAX;
u64 cur_physical = physical + cur_logical - logical_start;
- if (atomic_read(&fs_info->scrub_cancel_req) ||
- atomic_read(&sctx->cancel_req)) {
- ret = -ECANCELED;
+ ret = should_cancel_scrub(sctx);
+ if (ret < 0)
break;
- }
if (atomic_read(&fs_info->scrub_pause_req))
scrub_blocked_if_needed(fs_info);
--
2.51.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v3 3/3] btrfs: scrub: cancel the run if there is a pending signal
2025-10-19 0:45 [PATCH v3 0/3] btrfs: scrub: enhance freezing and signal handling Qu Wenruo
2025-10-19 0:45 ` [PATCH v3 1/3] btrfs: scrub: add cancel/pause/removed bg checks for raid56 parity stripes Qu Wenruo
2025-10-19 0:45 ` [PATCH v3 2/3] btrfs: scrub: cancel the run if the process or fs is being frozen Qu Wenruo
@ 2025-10-19 0:45 ` Qu Wenruo
2025-10-19 2:43 ` [PATCH v3 0/3] btrfs: scrub: enhance freezing and signal handling Askar Safin
2025-12-04 20:06 ` Askar Safin
4 siblings, 0 replies; 6+ messages in thread
From: Qu Wenruo @ 2025-10-19 0:45 UTC (permalink / raw)
To: linux-btrfs; +Cc: safinaskar, Filipe Manana
Unlike relocation, scrub never checks pending signals, and even for
relocation is only explicitly checking for fatal signal (SIGKILL), not
for regular ones.
Thankfully relocation can still be interrupted by regular signals by
the usage of wait_on_bit(), which is called with TASK_INTERRUPTIBLE.
Do the same for scrub/dev-replace, so that regular signals can also
cancel the scrub/replace run, and more importantly handle v2 cgroup
freezing which is based on signal handling code inside the kernel, and
freezing() function will not return true for v2 cgroup freezing.
This will address the problem that systemd slice freezing will timeout
on long running scrub/dev-replace.
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
fs/btrfs/scrub.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index bbd5793c2a16..05241324edd3 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -2076,7 +2076,7 @@ static int queue_scrub_stripe(struct scrub_ctx *sctx, struct btrfs_block_group *
* - -ECANCELED
* Being explicitly canceled through ioctl.
* - -EINTR
- * Being interrupted by fs/process freezing.
+ * Being interrupted by signal or fs/process freezing.
*/
static int should_cancel_scrub(const struct scrub_ctx *sctx)
{
@@ -2105,7 +2105,7 @@ static int should_cancel_scrub(const struct scrub_ctx *sctx)
* will timeout, as the running scrub will prevent the fs from being frozen.
*/
if (fs_info->sb->s_writers.frozen > SB_UNFROZEN ||
- freezing(current))
+ freezing(current) || signal_pending(current))
return -EINTR;
return 0;
}
--
2.51.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH v3 0/3] btrfs: scrub: enhance freezing and signal handling
2025-10-19 0:45 [PATCH v3 0/3] btrfs: scrub: enhance freezing and signal handling Qu Wenruo
` (2 preceding siblings ...)
2025-10-19 0:45 ` [PATCH v3 3/3] btrfs: scrub: cancel the run if there is a pending signal Qu Wenruo
@ 2025-10-19 2:43 ` Askar Safin
2025-12-04 20:06 ` Askar Safin
4 siblings, 0 replies; 6+ messages in thread
From: Askar Safin @ 2025-10-19 2:43 UTC (permalink / raw)
To: wqu; +Cc: linux-btrfs, safinaskar
I like this version. Especially EINTR/ECANCELLED distinction. Thank you!
--
Askar Safin
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v3 0/3] btrfs: scrub: enhance freezing and signal handling
2025-10-19 0:45 [PATCH v3 0/3] btrfs: scrub: enhance freezing and signal handling Qu Wenruo
` (3 preceding siblings ...)
2025-10-19 2:43 ` [PATCH v3 0/3] btrfs: scrub: enhance freezing and signal handling Askar Safin
@ 2025-12-04 20:06 ` Askar Safin
4 siblings, 0 replies; 6+ messages in thread
From: Askar Safin @ 2025-12-04 20:06 UTC (permalink / raw)
To: wqu; +Cc: linux-btrfs
Qu Wenruo <wqu@suse.com>:
> It's a long known bug that when scrub/dev-replace is running, power
> management suspension will time out and fail.
Please, also fix btrfs trim.
I just tested hibernation with trim (with current mainline 559e608c4655), and I see that hibernation works
if /sys/power/freeze_filesystems is 0 (current default), but doesn't work if
/sys/power/freeze_filesystems is 1 (future default).
So, I think we need similar thing for trim (i. e. check for superblock
freeze and "signal_pending").
--
Askar Safin
^ permalink raw reply [flat|nested] 6+ messages in thread