Skip to contentSkip to conversation

btrfs: Don't block system suspend during fstrim

PATCH Superseded Luca Stefani 2 participants 8 messages Sep 16, 2024 Btrfs file system

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover btrfs: Don't block system suspend during fstrim

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

v1: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/
v3: https://lore.kernel.org/lkml/[email protected]/
Original discussion: https://lore.kernel.org/lkml/[email protected]/

Luca Stefani (3):
btrfs: Always update fstrim_range on failure
btrfs: Split remaining space to discard in chunks
btrfs: Don't block system suspend during fstrim

 fs/btrfs/extent-tree.c | 47 +++++++++++++++++++++++++++++++++++-------
 fs/btrfs/ioctl.c       |  4 +---
 2 files changed, 40 insertions(+), 11 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH v4 1/3 btrfs: Always update fstrim_range on failure

Even in case of failure we could've discarded some data and userspace should be made aware of it, so copy fstrim_range to userspace regardless.

Also make sure to update the trimmed bytes amount even if btrfs_trim_free_extents fails.

---
 fs/btrfs/extent-tree.c | 4 ++--
 fs/btrfs/ioctl.c       | 4 +---
 2 files changed, 3 insertions(+), 5 deletions(-)
fs/btrfs/extent-tree.c +2 -2
index feec49e6f9c8..a5966324607d 100644
@@ -6551,13 +6551,13 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
 			continue;
 
 		ret = btrfs_trim_free_extents(device, &group_trimmed);
+
+		trimmed += group_trimmed;
 		if (ret) {
 			dev_failed++;
 			dev_ret = ret;
 			break;
 		}
-
-		trimmed += group_trimmed;
 	}
 	mutex_unlock(&fs_devices->device_list_mutex);
 
fs/btrfs/ioctl.c +1 -3
index e0a664b8a46a..94d8f29b04c5 100644
@@ -543,13 +543,11 @@ static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
 
 	range.minlen = max(range.minlen, minlen);
 	ret = btrfs_trim_fs(fs_info, &range);
-	if (ret < 0)
-		return ret;
 
 	if (copy_to_user(arg, &range, sizeof(range)))
 		return -EFAULT;
 
-	return 0;
+	return ret;
 }
 
 int __pure btrfs_is_empty_uuid(const u8 *uuid)
Signature · 1 line
2.46.0
#2 lore raw

PATCH v4 2/3 btrfs: Split remaining space to discard in chunks

In reply to 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_SIZE in preparation of introduction of cancellation signals handling.

---
 fs/btrfs/extent-tree.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
fs/btrfs/extent-tree.c +19 -5
index a5966324607d..cbe66d0acff8 100644
@@ -1239,7 +1239,7 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
 			       u64 *discarded_bytes)
 {
 	int j, ret = 0;
-	u64 bytes_left, end;
+	u64 bytes_left, bytes_to_discard, end;
 	u64 aligned_start = ALIGN(start, 1 << SECTOR_SHIFT);
 
 	/* Adjust the range to be aligned to 512B sectors if necessary. */
@@ -1300,13 +1300,27 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
 		bytes_left = end - start;
 	}
 
-	if (bytes_left) {
+	while (bytes_left) {
+		if (bytes_left > BTRFS_MAX_DATA_CHUNK_SIZE)
+			bytes_to_discard = BTRFS_MAX_DATA_CHUNK_SIZE;
+		else
+			bytes_to_discard = 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
#3 lore raw

Re: [PATCH v4 2/3] btrfs: Split remaining space to discard in chunks

Quoted 29 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_SIZE in preparation of introduction of cancellation signals handling.

---
  fs/btrfs/extent-tree.c | 24 +++++++++++++++++++-----
  1 file changed, 19 insertions(+), 5 deletions(-)
fs/btrfs/extent-tree.c +1 -1
index a5966324607d..cbe66d0acff8 100644
@@ -1239,7 +1239,7 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
  			       u64 *discarded_bytes)
  {
  	int j, ret = 0;
-	u64 bytes_left, end;
+	u64 bytes_left, bytes_to_discard, end;
  	u64 aligned_start = ALIGN(start, 1 << SECTOR_SHIFT);
 
  	/* Adjust the range to be aligned to 512B sectors if necessary. */

fs/btrfs/extent-tree.c static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, · line 1305

Quoted 7 lines
fs/btrfs/extent-tree.c +3 -1
@@ -1300,13 +1300,27 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
  		bytes_left = end - start;
  	}
 
-	if (bytes_left) {
+	while (bytes_left) {
+		if (bytes_left > BTRFS_MAX_DATA_CHUNK_SIZE)
+			bytes_to_discard = BTRFS_MAX_DATA_CHUNK_SIZE;

That MAX_DATA_CHUNK_SIZE is only possible for RAID0/RAID10/RAID5/RAID6, by spanning the device extents across multiple devices.

For each device, the maximum size is limited to 1G (check init_alloc_chunk_ctl_policy_regular()).

So you can just limit it to 1G instead.
(If you want, you can also extract that into a macro as a cleanup).

Furthermore, you can use min() instead of a if ().

So you only need:

		bytes_to_discard = min(SZ_1G, bytes_left);

Otherwise this looks good enough to me.
If the 1G size is not good enough, we can later tune it to smaller values.

Personally speaking I think 1G would be enough.

Thanks,
Qu

Quoted 24 lines
fs/btrfs/extent-tree.c +15 -3
+		else
+			bytes_to_discard = 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;
  }
 
#4 lore raw

Re: [PATCH v4 2/3] btrfs: Split remaining space to discard in chunks

Quoted 42 lines · Qu Wenruo
Quoted 29 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_SIZE in preparation of introduction of cancellation signals handling.

---
  fs/btrfs/extent-tree.c | 24 +++++++++++++++++++-----

  1 file changed, 19 insertions(+), 5 deletions(-)
fs/btrfs/extent-tree.c +1 -1
index a5966324607d..cbe66d0acff8 100644
@@ -1239,7 +1239,7 @@ static int btrfs_issue_discard(struct 
 block_device *bdev, u64 start, u64 len,
                      u64 *discarded_bytes)
   {
       int j, ret = 0;
-    u64 bytes_left, end;
+    u64 bytes_left, bytes_to_discard, end;
       u64 aligned_start = ALIGN(start, 1 << SECTOR_SHIFT);
       /* Adjust the range to be aligned to 512B sectors if necessary. */

fs/btrfs/extent-tree.c static int btrfs_issue_discard(struct · line 1305

Quoted 7 lines
fs/btrfs/extent-tree.c +3 -1
@@ -1300,13 +1300,27 @@ static int btrfs_issue_discard(struct 
 block_device *bdev, u64 start, u64 len,
           bytes_left = end - start;
       }
-    if (bytes_left) {
+    while (bytes_left) {
+        if (bytes_left > BTRFS_MAX_DATA_CHUNK_SIZE)
+            bytes_to_discard = BTRFS_MAX_DATA_CHUNK_SIZE;

That MAX_DATA_CHUNK_SIZE is only possible for RAID0/RAID10/RAID5/RAID6, by spanning the device extents across multiple devices.

Quoted 4 lines

For each device, the maximum size is limited to 1G (check init_alloc_chunk_ctl_policy_regular()).

So you can just limit it to 1G instead.
(If you want, you can also extract that into a macro as a cleanup).

I think SZ_1G is enough for now.

Quoted 7 lines

Furthermore, you can use min() instead of a if ().

So you only need:

        bytes_to_discard = min(SZ_1G, bytes_left);

Quoted 7 lines

Otherwise this looks good enough to me.
If the 1G size is not good enough, we can later tune it to smaller values.

Personally speaking I think 1G would be enough.

Thanks,
Qu

Ack, done in v5

Quoted 23 lines
Quoted 23 lines
fs/btrfs/extent-tree.c +15 -3
+        else
+            bytes_to_discard = 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;
   }
#5 lore raw

PATCH v4 3/3 btrfs: Don't block system suspend during fstrim

In reply to 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).

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 | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
fs/btrfs/extent-tree.c +18 -1
index cbe66d0acff8..ab2e5d366a3a 100644
@@ -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)
 {
@@ -1319,6 +1325,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;
@@ -6473,7 +6484,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;
 		}
@@ -6522,6 +6533,9 @@ 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())
+			break;
+
 		if (cache->start >= range_end) {
 			btrfs_put_block_group(cache);
 			break;
@@ -6561,6 +6575,9 @@ 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())
+			break;
+
 		if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
 			continue;
 
Signature · 1 line
2.46.0
#6 lore raw
Quoted 63 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).

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 | 19 ++++++++++++++++++-
  1 file changed, 18 insertions(+), 1 deletion(-)
fs/btrfs/extent-tree.c +12 -1
index cbe66d0acff8..ab2e5d366a3a 100644
@@ -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)
  {
@@ -1319,6 +1325,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;
@@ -6473,7 +6484,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;
  		}

fs/btrfs/extent-tree.c int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) · line 6538

Quoted 6 lines
fs/btrfs/extent-tree.c +3 -0
@@ -6522,6 +6533,9 @@ 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())
+			break;
+

Please update @bg_ret return value.

fs/btrfs/extent-tree.c int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) · line 6580

Quoted 10 lines
fs/btrfs/extent-tree.c +3 -0
  		if (cache->start >= range_end) {
  			btrfs_put_block_group(cache);
  			break;
@@ -6561,6 +6575,9 @@ 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())
+			break;
+

The same here, please update @dev_ret.

Thanks,
Qu

Quoted 3 lines
fs/btrfs/extent-tree.c +0 -0
  		if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
  			continue;
 
#7 lore raw
Quoted 66 lines · Qu Wenruo
Quoted 63 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).

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 | 19 ++++++++++++++++++-

  1 file changed, 18 insertions(+), 1 deletion(-)
fs/btrfs/extent-tree.c +12 -1
index cbe66d0acff8..ab2e5d366a3a 100644
@@ -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)
   {
@@ -1319,6 +1325,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;
@@ -6473,7 +6484,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;
           }
@@ -6522,6 +6533,9 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, 
Quoted 8 lines

fs/btrfs/extent-tree.c int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) · line 6538

Quoted 6 lines
fs/btrfs/extent-tree.c +3 -0
 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())
+            break;
+

Please update @bg_ret return value.

Done in v5.

Quoted 13 lines

fs/btrfs/extent-tree.c int btrfs_trim_fs(struct btrfs_fs_info *fs_info, · line 6580

Quoted 11 lines
fs/btrfs/extent-tree.c +3 -0
           if (cache->start >= range_end) {
               btrfs_put_block_group(cache);
               break;
@@ -6561,6 +6575,9 @@ 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())
+            break;
+

The same here, please update @dev_ret.

ditto.

Quoted 5 lines

Thanks,
Qu

Quoted 2 lines
fs/btrfs/extent-tree.c +0 -0
           if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
               continue;