Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit fba6544

Browse files
luca020400gregkh
authored andcommitted
btrfs: add cancellation points to trim loops
commit 6931385 upstream. There are reports that system cannot suspend due to running trim because the task responsible for trimming the device isn't able to finish in time, especially since we have a free extent discarding phase, which can trim a lot of unallocated space. There are no limits on the trim size (unlike the block group part). Since trime isn't a critical call it can be interrupted at any time, in such cases we stop the trim, report the amount of discarded bytes and return an error. Link: https://bugzilla.kernel.org/show_bug.cgi?id=219180 Link: https://bugzilla.suse.com/show_bug.cgi?id=1229737 CC: stable@vger.kernel.org # 5.15+ Signed-off-by: Luca Stefani <luca.stefani.ge1@gmail.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 21e90ee commit fba6544

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

fs/btrfs/extent-tree.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,11 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
13161316
start += bytes_to_discard;
13171317
bytes_left -= bytes_to_discard;
13181318
*discarded_bytes += bytes_to_discard;
1319+
1320+
if (btrfs_trim_interrupted()) {
1321+
ret = -ERESTARTSYS;
1322+
break;
1323+
}
13191324
}
13201325

13211326
return ret;
@@ -6470,7 +6475,7 @@ static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed)
64706475
start += len;
64716476
*trimmed += bytes;
64726477

6473-
if (fatal_signal_pending(current)) {
6478+
if (btrfs_trim_interrupted()) {
64746479
ret = -ERESTARTSYS;
64756480
break;
64766481
}

fs/btrfs/free-space-cache.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3809,7 +3809,7 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group,
38093809
if (async && *total_trimmed)
38103810
break;
38113811

3812-
if (fatal_signal_pending(current)) {
3812+
if (btrfs_trim_interrupted()) {
38133813
ret = -ERESTARTSYS;
38143814
break;
38153815
}
@@ -4000,7 +4000,7 @@ static int trim_bitmaps(struct btrfs_block_group *block_group,
40004000
}
40014001
block_group->discard_cursor = start;
40024002

4003-
if (fatal_signal_pending(current)) {
4003+
if (btrfs_trim_interrupted()) {
40044004
if (start != offset)
40054005
reset_trimming_bitmap(ctl, offset);
40064006
ret = -ERESTARTSYS;

fs/btrfs/free-space-cache.h

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/list.h>
1111
#include <linux/spinlock.h>
1212
#include <linux/mutex.h>
13+
#include <linux/freezer.h>
1314
#include "fs.h"
1415

1516
struct inode;
@@ -56,6 +57,11 @@ static inline bool btrfs_free_space_trimming_bitmap(
5657
return (info->trim_state == BTRFS_TRIM_STATE_TRIMMING);
5758
}
5859

60+
static inline bool btrfs_trim_interrupted(void)
61+
{
62+
return fatal_signal_pending(current) || freezing(current);
63+
}
64+
5965
/*
6066
* Deltas are an effective way to populate global statistics. Give macro names
6167
* to make it clear what we're doing. An example is discard_extents in

0 commit comments

Comments
 (0)