btrfs: Don't block system suspend during fstrim
v5 cover btrfs: Don't block system suspend during fstrim
Changes since v4:
* Set chunk size to 1G
* Set proper error return codes in case of interruption
* Dropped fstrim_range fixup as pulled in -next
Changes since v3:
* Went back to manual chunk size
Changes since v2:
* Use blk_alloc_discard_bio directly
* Reset ret to ERESTARTSYS
Changes since v1:
* Use bio_discard_limit to calculate chunk size
* Makes use of the split chunks
Original discussion: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/
v3: https://lore.kernel.org/lkml/[email protected]/
v4: https://lore.kernel.org/lkml/[email protected]/
Luca Stefani (2):
btrfs: Split remaining space to discard in chunks
btrfs: Don't block system suspend during fstrim
fs/btrfs/extent-tree.c | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-)
Signature · 1 line
2.46.0
PATCH v5 1/2 btrfs: Split remaining space to discard in chunks
Per Qu Wenruo in case we have a very large disk, e.g. 8TiB device, mostly empty although we will do the split according to our super block locations, the last super block ends at 256G, we can submit a huge discard for the range [256G, 8T), causing a super large delay.
We now split the space left to discard based on BTRFS_MAX_DATA_CHUNK_SIZE in preparation of introduction of cancellation signals handling.
--- fs/btrfs/extent-tree.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
fs/btrfs/extent-tree.c +15 -4
@@ -1300,13 +1300,24 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, bytes_left = end - start; } -if (bytes_left) { +while (bytes_left) { +u64 bytes_to_discard =min (SZ_1G , bytes_left); + ret =blkdev_issue_discard (bdev, start >>SECTOR_SHIFT , - bytes_left >>SECTOR_SHIFT , + bytes_to_discard >>SECTOR_SHIFT ,GFP_NOFS ); -if (!ret) - *discarded_bytes += bytes_left; + +if (ret) { +if (ret != -EOPNOTSUPP ) +break ; +continue ; + } + + start += bytes_to_discard; + bytes_left -= bytes_to_discard; + *discarded_bytes += bytes_to_discard; } +return ret; }
Signature · 1 line
2.46.0
Re: [PATCH v5 1/2] btrfs: Split remaining space to discard in chunks
Quoted 20 lines · Luca Stefani
Per Qu Wenruo in case we have a very large disk, e.g. 8TiB device, mostly empty although we will do the split according to our super block locations, the last super block ends at 256G, we can submit a huge discard for the range [256G, 8T), causing a super large delay.
We now split the space left to discard based on
BTRFS_MAX_DATA_CHUNK_SIZEin preparation of introduction of cancellation signals handling.Signed-off-by Luca Stefani <[email protected]>--- fs/btrfs/extent-tree.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)fs/btrfs/extent-tree.c +0
-0
fs/btrfs/extent-tree.c static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, · line 1304
Quoted 6 lines
fs/btrfs/extent-tree.c +2
-1@@ -1300,13 +1300,24 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, bytes_left = end - start; } -if (bytes_left) { +while (bytes_left) { +u64 bytes_to_discard =min (SZ_1G , bytes_left);
Please define a separate constant for that and also mention it in the changelog instead of BTRFS_MAX_DATA_CHUNK_SIZE.
Quoted 25 lines
fs/btrfs/extent-tree.c +13
-3+ ret =blkdev_issue_discard (bdev, start >>SECTOR_SHIFT , - bytes_left >>SECTOR_SHIFT , + bytes_to_discard >>SECTOR_SHIFT ,GFP_NOFS ); -if (!ret) - *discarded_bytes += bytes_left; + +if (ret) { +if (ret != -EOPNOTSUPP ) +break ; +continue ; + } + + start += bytes_to_discard; + bytes_left -= bytes_to_discard; + *discarded_bytes += bytes_to_discard; } +return ret; }Signature · 1 line
2.46.0
PATCH v5 2/2 btrfs: Don't block system suspend during fstrim
In reply to Luca StefaniSometimes the system isn't able to suspend 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, and there is no limits on the trim size (unlike the block group part).
Since discard 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 failure.
--- fs/btrfs/extent-tree.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
fs/btrfs/extent-tree.c +22 -1
@@ -16,6 +16,7 @@#include <linux/percpu_counter.h> #include <linux/lockdep.h> #include <linux/crc32c.h> +#include <linux/freezer.h> #include "ctree.h" #include "extent-tree.h" #include "transaction.h"
@@ -1235,6 +1236,11 @@ static int remove_extent_backref(struct btrfs_trans_handle *trans,return ret; } +static bool btrfs_trim_interrupted (void ) +{ +return fatal_signal_pending (current) ||freezing (current); +} +static int btrfs_issue_discard (struct block_device *bdev,u64 start,u64 len,u64 *discarded_bytes) {
@@ -1316,6 +1322,11 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, start += bytes_to_discard; bytes_left -= bytes_to_discard; *discarded_bytes += bytes_to_discard; + +if (btrfs_trim_interrupted ()) { + ret = -ERESTARTSYS ; +break ; + } }return ret;
@@ -6470,7 +6481,7 @@ static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed) start += len; *trimmed += bytes; -if (fatal_signal_pending (current)) { +if (btrfs_trim_interrupted ()) { ret = -ERESTARTSYS ;break ; }
@@ -6519,6 +6530,11 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) cache =btrfs_lookup_first_block_group (fs_info, range->start);for (; cache; cache =btrfs_next_block_group (cache)) { +if (btrfs_trim_interrupted ()) { + bg_ret = -ERESTARTSYS ; +break ; + } +if (cache->start >= range_end) {btrfs_put_block_group (cache);break ;
@@ -6558,6 +6574,11 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)mutex_lock (&fs_devices->device_list_mutex);list_for_each_entry (device, &fs_devices->devices, dev_list) { +if (btrfs_trim_interrupted ()) { + dev_ret = -ERESTARTSYS ; +break ; + } +if (test_bit (BTRFS_DEV_STATE_MISSING , &device->dev_state))continue ;
Signature · 1 line
2.46.0
Quoted 6 lines · Luca Stefani
Sometimes the system isn't able to suspend 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, and there is no limits on the trim size (unlike the block group part).
Quoted 7 lines
Since discard 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 failure.
I went through the cancellation points, some of them don't seem to be necessary, eg. in a big loop when some function is called to do trim (extents, bitmaps) and then again does the signal and freezing check.
Next, some of the functions are called from async discard and errors are not checked: btrfs_trim_block_group_bitmaps() called from btrfs_discard_workfn().
Ther's also check for signals pending in trim_bitmaps() in free-space-cache.c. Given that the space cache code is on the way out we don't necesssarily need to fix it but if the patch gets backported to older kernels it still makes sense.
Quoted 62 lines
--- fs/btrfs/extent-tree.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)fs/btrfs/extent-tree.c +17
-1@@ -16,6 +16,7 @@#include <linux/percpu_counter.h> #include <linux/lockdep.h> #include <linux/crc32c.h> +#include <linux/freezer.h> #include "ctree.h" #include "extent-tree.h" #include "transaction.h" @@ -1235,6 +1236,11 @@ static int remove_extent_backref(struct btrfs_trans_handle *trans,return ret; } +static bool btrfs_trim_interrupted (void ) +{ +return fatal_signal_pending (current) ||freezing (current); +} +static int btrfs_issue_discard (struct block_device *bdev,u64 start,u64 len,u64 *discarded_bytes) {@@ -1316,6 +1322,11 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, start += bytes_to_discard; bytes_left -= bytes_to_discard; *discarded_bytes += bytes_to_discard; + +if (btrfs_trim_interrupted ()) { + ret = -ERESTARTSYS ; +break ; + } }return ret;@@ -6470,7 +6481,7 @@ static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed) start += len; *trimmed += bytes; -if (fatal_signal_pending (current)) { +if (btrfs_trim_interrupted ()) { ret = -ERESTARTSYS ;break ; }@@ -6519,6 +6530,11 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) cache =btrfs_lookup_first_block_group (fs_info, range->start);for (; cache; cache =btrfs_next_block_group (cache)) { +if (btrfs_trim_interrupted ()) { + bg_ret = -ERESTARTSYS ; +break ; + } +if (cache->start >= range_end) {btrfs_put_block_group (cache);break ;
fs/btrfs/extent-tree.c int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) · line 6578
Quoted 5 lines
fs/btrfs/extent-tree.c +2
-0@@ -6558,6 +6574,11 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)mutex_lock (&fs_devices->device_list_mutex);list_for_each_entry (device, &fs_devices->devices, dev_list) { +if (btrfs_trim_interrupted ()) { + dev_ret = -ERESTARTSYS ;
This one seems redundant.
Quoted 9 lines
fs/btrfs/extent-tree.c +3
-0+break ; + } +if (test_bit (BTRFS_DEV_STATE_MISSING , &device->dev_state))continue ;Signature · 1 line
2.46.0
Quoted 15 lines · David Sterba
Quoted 6 lines · Luca Stefani
Sometimes the system isn't able to suspend 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, and there is no limits on the trim size (unlike the block group part).
Quoted 7 lines
Since discard 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 failure.
Quoted 7 lines
I went through the cancellation points, some of them don't seem to be necessary, eg. in a big loop when some function is called to do trim (extents, bitmaps) and then again does the signal and freezing check.
Next, some of the functions are called from async discard and errors are not checked:
btrfs_trim_block_group_bitmaps()called frombtrfs_discard_workfn().
Both btrfs_trim_block_group_bitmaps and btrfs_trim_block_group_extents ret codes are never checked indeed in btrfs_discard_workfn. I'll fix that up in another CL.
Quoted 5 lines
Ther's also check for signals pending in
trim_bitmaps()infree-space-cache.c. Given that the space cache code is on the way out we don't necesssarily need to fix it but if the patch gets backported to older kernels it still makes sense.
Ah I missed this one, will fix it.
There's a few more instances of fatal_signal_pending but I don't know if they should be translated or not, will focus on the one you mentioned and trim_no_bitmap which seems to do similar checks for fatal signals.
Quoted 80 lines
Quoted 63 lines
--- fs/btrfs/extent-tree.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)fs/btrfs/extent-tree.c +17
-1@@ -16,6 +16,7 @@#include <linux/percpu_counter.h> #include <linux/lockdep.h> #include <linux/crc32c.h> +#include <linux/freezer.h> #include "ctree.h" #include "extent-tree.h" #include "transaction.h" @@ -1235,6 +1236,11 @@ static int remove_extent_backref(struct btrfs_trans_handle *trans,return ret; } +static bool btrfs_trim_interrupted (void ) +{ +return fatal_signal_pending (current) ||freezing (current); +} +static int btrfs_issue_discard (struct block_device *bdev,u64 start,u64 len,u64 *discarded_bytes) {@@ -1316,6 +1322,11 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, start += bytes_to_discard; bytes_left -= bytes_to_discard; *discarded_bytes += bytes_to_discard; + +if (btrfs_trim_interrupted ()) { + ret = -ERESTARTSYS ; +break ; + } }return ret;@@ -6470,7 +6481,7 @@ static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed) start += len; *trimmed += bytes; -if (fatal_signal_pending (current)) { +if (btrfs_trim_interrupted ()) { ret = -ERESTARTSYS ;break ; }@@ -6519,6 +6530,11 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) cache =btrfs_lookup_first_block_group (fs_info, range->start);for (; cache; cache =btrfs_next_block_group (cache)) { +if (btrfs_trim_interrupted ()) { + bg_ret = -ERESTARTSYS ; +break ; + } +if (cache->start >= range_end) {btrfs_put_block_group (cache);break ;fs/btrfs/extent-tree.c int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) · line 6578
Quoted 5 lines
fs/btrfs/extent-tree.c +2
-0@@ -6558,6 +6574,11 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)mutex_lock (&fs_devices->device_list_mutex);list_for_each_entry (device, &fs_devices->devices, dev_list) { +if (btrfs_trim_interrupted ()) { + dev_ret = -ERESTARTSYS ;This one seems redundant.
Quoted 9 lines
fs/btrfs/extent-tree.c +3
-0+break ; + } +if (test_bit (BTRFS_DEV_STATE_MISSING , &device->dev_state))continue ;Signature · 1 line
2.46.0