Skip to contentSkip to conversation

btrfs: fixes for reclaim

PATCH In review Naohiro Aota 6 participants 18 messages Jun 6, 2023 – Jun 12, 2023 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: fixes for reclaim

There are several issues on the reclaim process out there:

- Long-running reclaim process blocks removing unused BGs
- Belonging to the reclaim list blocks it goes to the unused list
- It tries relocation even when FS is read-only
- Temporal failure keep a block group un-reclaimed

This series fixes them.

Naohiro Aota (4):
btrfs: delete unused BGs while reclaiming BGs
btrfs: move out now unused BG from the reclaim list
btrfs: bail out reclaim process if filesystem is read-only
btrfs: reinsert BGs failed to reclaim

 fs/btrfs/block-group.c | 43 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/4 btrfs: delete unused BGs while reclaiming BGs

The reclaiming process only starts after the FS volumes are allocated to a certain level (75% by default). Thus, the list of reclaiming target block groups can build up so huge at the time the reclaim process kicks in. On a test run, there were over 1000 BGs in the reclaim list.

As the reclaim involves rewriting the data, it takes really long time to reclaim the BGs. While the reclaim is running, btrfs_delete_unused_bgs() won't proceed because the reclaim side is holding fs_info->reclaim_bgs_lock. As a result, we will have a large number of unused BGs kept in the unused list. On my test run, I got 1057 unused BGs.

Since deleting a block group is relatively easy and fast work, we can call btrfs_delete_unused_bgs() while it reclaims BGs, to avoid building up unused BGs.

Fixes 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")
---
 fs/btrfs/block-group.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
fs/btrfs/block-group.c +14 -0
index 618ba7670e66..c5547da0f6eb 100644
@@ -1824,10 +1824,24 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
 
 next:
 		btrfs_put_block_group(bg);
+
+		mutex_unlock(&fs_info->reclaim_bgs_lock);
+		/*
+		 * Reclaiming all the BGs in the list can take really long.
+		 * Prioritize cleanning up unused BGs.
+		 */
+		btrfs_delete_unused_bgs(fs_info);
+		/*
+		 * If we are interrupted by a balance, we can just bail out. The
+		 * cleaner thread call me again if necessary.
+		 */
+		if (!mutex_trylock(&fs_info->reclaim_bgs_lock))
+			goto end;
 		spin_lock(&fs_info->unused_bgs_lock);
 	}
 	spin_unlock(&fs_info->unused_bgs_lock);
 	mutex_unlock(&fs_info->reclaim_bgs_lock);
+end:
 	btrfs_exclop_finish(fs_info);
 	sb_end_write(fs_info->sb);
 }
Signature · 1 line
2.40.1
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] btrfs: delete unused BGs while reclaiming BGs

Quoted 12 lines · Naohiro Aota

The reclaiming process only starts after the FS volumes are allocated to a certain level (75% by default). Thus, the list of reclaiming target block groups can build up so huge at the time the reclaim process kicks in. On a test run, there were over 1000 BGs in the reclaim list.

As the reclaim involves rewriting the data, it takes really long time to reclaim the BGs. While the reclaim is running, btrfs_delete_unused_bgs() won't proceed because the reclaim side is holding fs_info->reclaim_bgs_lock. As a result, we will have a large number of unused BGs kept in the unused list. On my test run, I got 1057 unused BGs.

Quoted 7 lines

Since deleting a block group is relatively easy and fast work, we can call btrfs_delete_unused_bgs() while it reclaims BGs, to avoid building up unused BGs.

Fixes 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")

Looks good, great catch.

Quoted 36 lines
---
 fs/btrfs/block-group.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
fs/btrfs/block-group.c +14 -1
index 618ba7670e66..c5547da0f6eb 100644
@@ -1824,10 +1824,24 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
 
 next:
                btrfs_put_block_group(bg);
+
+               mutex_unlock(&fs_info->reclaim_bgs_lock);
+               /*
+                * Reclaiming all the BGs in the list can take really long.
+                * Prioritize cleanning up unused BGs.
+                */
+               btrfs_delete_unused_bgs(fs_info);
+               /*
+                * If we are interrupted by a balance, we can just bail out. The
+                * cleaner thread call me again if necessary.
+                */
+               if (!mutex_trylock(&fs_info->reclaim_bgs_lock))
+                       goto end;
                spin_lock(&fs_info->unused_bgs_lock);
        }
        spin_unlock(&fs_info->unused_bgs_lock);
        mutex_unlock(&fs_info->reclaim_bgs_lock);
+end:
        btrfs_exclop_finish(fs_info);
        sb_end_write(fs_info->sb);
 }
--

2.40.1

#4 lore raw

Re: [PATCH 1/4] btrfs: delete unused BGs while reclaiming BGs

In reply to Naohiro Aota
Quoted 33 lines · Naohiro Aota

The reclaiming process only starts after the FS volumes are allocated to a certain level (75% by default). Thus, the list of reclaiming target block groups can build up so huge at the time the reclaim process kicks in. On a test run, there were over 1000 BGs in the reclaim list.

As the reclaim involves rewriting the data, it takes really long time to reclaim the BGs. While the reclaim is running, btrfs_delete_unused_bgs() won't proceed because the reclaim side is holding fs_info->reclaim_bgs_lock. As a result, we will have a large number of unused BGs kept in the unused list. On my test run, I got 1057 unused BGs.

Since deleting a block group is relatively easy and fast work, we can call btrfs_delete_unused_bgs() while it reclaims BGs, to avoid building up unused BGs.

Fixes 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")
---
 fs/btrfs/block-group.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
fs/btrfs/block-group.c +3 -0
index 618ba7670e66..c5547da0f6eb 100644
@@ -1824,10 +1824,24 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
 
 next:
 		btrfs_put_block_group(bg);
+
+		mutex_unlock(&fs_info->reclaim_bgs_lock);
+		/*

fs/btrfs/block-group.c void btrfs_reclaim_bgs_work(struct work_struct *work) · line 1839

Quoted 10 lines
fs/btrfs/block-group.c +10 -0
+		 * Reclaiming all the BGs in the list can take really long.
+		 * Prioritize cleanning up unused BGs.
+		 */
+		btrfs_delete_unused_bgs(fs_info);
+		/*
+		 * If we are interrupted by a balance, we can just bail out. The
+		 * cleaner thread call me again if necessary.
+		 */
+		if (!mutex_trylock(&fs_info->reclaim_bgs_lock))
+			goto end;

I agree that this fix makes sense and a lot of reclaim should not block deleting unused bgs.

However, it feels quite hacky to me, because by the current design, we are explicitly calling btrfs_delete_unused_bgs as a first class cleanup action from the cleaner thread, before calling reclaim. I think a little more cleanup to integrate the two together would reduce the "throw things at the wall" feel here.

I would propose either:
1. Run them in parallel and make sure they release locks appropriately so that everyone makes good forward progress. I think it's a good model, but kind of a departure from the single cleaner thread so maybe risky/a pain to code.
2. Just get rid of the explicit delete unused from cleaner and integrate it as a first class component of this reclaim loop. This loop becomes *the* delete unused work except shutdown type cases.

FWIW, not a NAK, just my 2c.

Thanks,
Boris

Quoted 11 lines
fs/btrfs/block-group.c +1 -0
 		spin_lock(&fs_info->unused_bgs_lock);
 	}
 	spin_unlock(&fs_info->unused_bgs_lock);
 	mutex_unlock(&fs_info->reclaim_bgs_lock);
+end:
 	btrfs_exclop_finish(fs_info);
 	sb_end_write(fs_info->sb);
 }
Signature · 1 line
2.40.1
#5 lore raw

Re: [PATCH 1/4] btrfs: delete unused BGs while reclaiming BGs

Quoted 63 lines · Boris Burkov
Quoted 33 lines · Naohiro Aota

The reclaiming process only starts after the FS volumes are allocated to a certain level (75% by default). Thus, the list of reclaiming target block groups can build up so huge at the time the reclaim process kicks in. On a test run, there were over 1000 BGs in the reclaim list.

As the reclaim involves rewriting the data, it takes really long time to reclaim the BGs. While the reclaim is running, btrfs_delete_unused_bgs() won't proceed because the reclaim side is holding fs_info->reclaim_bgs_lock. As a result, we will have a large number of unused BGs kept in the unused list. On my test run, I got 1057 unused BGs.

Since deleting a block group is relatively easy and fast work, we can call btrfs_delete_unused_bgs() while it reclaims BGs, to avoid building up unused BGs.

Fixes 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")
---
 fs/btrfs/block-group.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
fs/btrfs/block-group.c +3 -0
index 618ba7670e66..c5547da0f6eb 100644
@@ -1824,10 +1824,24 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
 
 next:
 		btrfs_put_block_group(bg);
+
+		mutex_unlock(&fs_info->reclaim_bgs_lock);
+		/*

fs/btrfs/block-group.c void btrfs_reclaim_bgs_work(struct work_struct *work) · line 1839

Quoted 10 lines
fs/btrfs/block-group.c +10 -0
+		 * Reclaiming all the BGs in the list can take really long.
+		 * Prioritize cleanning up unused BGs.
+		 */
+		btrfs_delete_unused_bgs(fs_info);
+		/*
+		 * If we are interrupted by a balance, we can just bail out. The
+		 * cleaner thread call me again if necessary.
+		 */
+		if (!mutex_trylock(&fs_info->reclaim_bgs_lock))
+			goto end;

I agree that this fix makes sense and a lot of reclaim should not block deleting unused bgs.

However, it feels quite hacky to me, because by the current design, we are explicitly calling btrfs_delete_unused_bgs as a first class cleanup action from the cleaner thread, before calling reclaim. I think a little more cleanup to integrate the two together would reduce the "throw things at the wall" feel here.

I would propose either:
1. Run them in parallel and make sure they release locks appropriately so that everyone makes good forward progress. I think it's a good model, but kind of a departure from the single cleaner thread so maybe risky/a pain to code.
2. Just get rid of the explicit delete unused from cleaner and integrate it as a first class component of this reclaim loop. This loop becomes *the* delete unused work except shutdown type cases.

Quoted 1 line

FWIW, not a NAK, just my 2c.

Thank you for your suggestion. Yeah, I also considered something like your option 2, but I hesitated to do so as a fix patch because it will change the current cleaner and reclaim thread model. I'll rework them an a integrated loop if btrfs devs agree with it.

Quoted 14 lines

Thanks,
Boris

Quoted 11 lines
fs/btrfs/block-group.c +1 -0
 		spin_lock(&fs_info->unused_bgs_lock);
 	}
 	spin_unlock(&fs_info->unused_bgs_lock);
 	mutex_unlock(&fs_info->reclaim_bgs_lock);
+end:
 	btrfs_exclop_finish(fs_info);
 	sb_end_write(fs_info->sb);
 }
Signature · 1 line
2.40.1
#6 lore raw

PATCH 2/4 btrfs: move out now unused BG from the reclaim list

In reply to Naohiro Aota

An unused block group is easy to remove to free up space and should be reclaimed fast. Such block group can often already be a target of the reclaim process. As we check list_empty(&bg->bg_list), we keep it in the reclaim list. That block group is never reclaimed until the file system is filled e.g, 75%.

Instead, we can move unused block group to the unused list and delete it fast.

Fixes 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")
---
 fs/btrfs/block-group.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +4 -1
index c5547da0f6eb..d5bba02167be 100644
@@ -1633,11 +1633,14 @@ void btrfs_mark_bg_unused(struct btrfs_block_group *bg)
 {
 	struct btrfs_fs_info *fs_info = bg->fs_info;
 
+	trace_btrfs_add_unused_block_group(bg);
 	spin_lock(&fs_info->unused_bgs_lock);
 	if (list_empty(&bg->bg_list)) {
 		btrfs_get_block_group(bg);
-		trace_btrfs_add_unused_block_group(bg);
 		list_add_tail(&bg->bg_list, &fs_info->unused_bgs);
+	} else {
+		/* Pull out the BG from the reclaim_bgs list. */
+		list_move_tail(&bg->bg_list, &fs_info->unused_bgs);
 	}
 	spin_unlock(&fs_info->unused_bgs_lock);
 }
Signature · 1 line
2.40.1
Reviewed-by on v1 2/4
#7 lore raw

Re: [PATCH 2/4] btrfs: move out now unused BG from the reclaim list

Quoted 7 lines · Naohiro Aota

An unused block group is easy to remove to free up space and should be reclaimed fast. Such block group can often already be a target of the reclaim process. As we check list_empty(&bg->bg_list), we keep it in the reclaim list. That block group is never reclaimed until the file system is filled e.g, 75%.

Quoted 6 lines

Instead, we can move unused block group to the unused list and delete it fast.

Fixes 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")

Looks good, thanks.

Quoted 27 lines
---
 fs/btrfs/block-group.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +4 -2
index c5547da0f6eb..d5bba02167be 100644
@@ -1633,11 +1633,14 @@ void btrfs_mark_bg_unused(struct btrfs_block_group *bg)
 {
        struct btrfs_fs_info *fs_info = bg->fs_info;
 
+       trace_btrfs_add_unused_block_group(bg);
        spin_lock(&fs_info->unused_bgs_lock);
        if (list_empty(&bg->bg_list)) {
                btrfs_get_block_group(bg);
-               trace_btrfs_add_unused_block_group(bg);
                list_add_tail(&bg->bg_list, &fs_info->unused_bgs);
+       } else {
+               /* Pull out the BG from the reclaim_bgs list. */
+               list_move_tail(&bg->bg_list, &fs_info->unused_bgs);
        }
        spin_unlock(&fs_info->unused_bgs_lock);
 }
--

2.40.1

#9 lore raw

PATCH 3/4 btrfs: bail out reclaim process if filesystem is read-only

In reply to Naohiro Aota

When a filesystem is read-only, we cannot reclaim a block group as it cannot rewrite the data. Just bail out in that case.

Note that it can drop BGs in this case. As we did sb_start_write(), read-only filesystem means we got a fatal error and forced read-only. There is no chance to reclaim them again.

Fixes 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")
---
 fs/btrfs/block-group.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
fs/btrfs/block-group.c +9 -2
index d5bba02167be..db9bee071434 100644
@@ -1794,8 +1794,15 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
 		}
 		spin_unlock(&bg->lock);
 
-		/* Get out fast, in case we're unmounting the filesystem */
-		if (btrfs_fs_closing(fs_info)) {
+		/*
+		 * Get out fast, in case we're read-only or unmounting the
+		 * filesystem. It is OK to drop block groups from the list even
+		 * for the read-only case. As we did sb_start_write(), "mount -o
+		 * ro" won't happen and read-only FS means it is forced
+		 * read-only due to a fatal error. So, it never get back to
+		 * read-write to let us reclaime again.
+		 */
+		if (btrfs_need_cleaner_sleep(fs_info)) {
 			up_write(&space_info->groups_sem);
 			goto next;
 		}
Signature · 1 line
2.40.1
Reviewed-by on v1 3/4
#10 lore raw

Re: [PATCH 3/4] btrfs: bail out reclaim process if filesystem is read-only

Quoted 20 lines · Naohiro Aota

When a filesystem is read-only, we cannot reclaim a block group as it cannot rewrite the data. Just bail out in that case.

Note that it can drop BGs in this case. As we did sb_start_write(), read-only filesystem means we got a fatal error and forced read-only. There is no chance to reclaim them again.

Fixes 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")
---
 fs/btrfs/block-group.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
fs/btrfs/block-group.c +0 -0
index d5bba02167be..db9bee071434 100644

fs/btrfs/block-group.c void btrfs_reclaim_bgs_work(struct work_struct *work) · line 1801

Quoted 10 lines
fs/btrfs/block-group.c +5 -2
@@ -1794,8 +1794,15 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
                }
                spin_unlock(&bg->lock);
 
-               /* Get out fast, in case we're unmounting the filesystem */
-               if (btrfs_fs_closing(fs_info)) {
+               /*
+                * Get out fast, in case we're read-only or unmounting the
+                * filesystem. It is OK to drop block groups from the list even
+                * for the read-only case. As we did sb_start_write(), "mount -o
+                * ro" won't happen and read-only FS means it is forced

I think here you mean "mount -o remount,ro", just to be more clear.

fs/btrfs/block-group.c void btrfs_reclaim_bgs_work(struct work_struct *work) · line 1802

Quoted 1 line
fs/btrfs/block-group.c +1 -0
+                * read-only due to a fatal error. So, it never get back to

get -> gets

fs/btrfs/block-group.c void btrfs_reclaim_bgs_work(struct work_struct *work) · line 1803

Quoted 1 line
fs/btrfs/block-group.c +1 -0
+                * read-write to let us reclaime again.

reclaime -> reclaim

Other than that, it looks good to me.

Thanks.

Quoted 8 lines
fs/btrfs/block-group.c +2 -1
+                */
+               if (btrfs_need_cleaner_sleep(fs_info)) {
                        up_write(&space_info->groups_sem);
                        goto next;
                }
--

2.40.1

#12 lore raw

PATCH 4/4 btrfs: reinsert BGs failed to reclaim

In reply to Naohiro Aota

The reclaim process can temporally fail. For example, if the space is getting tight, it fails to make the block group read-only. If there are no further writes on that block group, the block group will never get back to the reclaim list, and the BG never get reclaimed. In a certain workload, we can leave many such block groups never reclaimed.

So, let's get it back to the list and give it a chance to be reclaimed.

Fixes 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")
---
 fs/btrfs/block-group.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +12 -1
index db9bee071434..36e0d9b5d824 100644
@@ -1833,7 +1833,18 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
 		}
 
 next:
-		btrfs_put_block_group(bg);
+		if (!ret) {
+			btrfs_put_block_group(bg);
+		} else {
+			spin_lock(&bg->lock);
+			spin_lock(&fs_info->unused_bgs_lock);
+			if (list_empty(&bg->bg_list))
+				list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
+			else
+				btrfs_put_block_group(bg);
+			spin_unlock(&fs_info->unused_bgs_lock);
+			spin_unlock(&bg->lock);
+		}
 
 		mutex_unlock(&fs_info->reclaim_bgs_lock);
 		/*
Signature · 1 line
2.40.1
#13 lore raw

Re: [PATCH 4/4] btrfs: reinsert BGs failed to reclaim

Quoted 2 lines · Naohiro Aota

The reclaim process can temporally fail. For example, if the space is

temporally -> temporarily

Quoted 3 lines

getting tight, it fails to make the block group read-only. If there are no further writes on that block group, the block group will never get back to the reclaim list, and the BG never get reclaimed. In a certain workload, we

get -> gets

Quoted 16 lines

can leave many such block groups never reclaimed.

So, let's get it back to the list and give it a chance to be reclaimed.

Fixes 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")
---
 fs/btrfs/block-group.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +0 -0
index db9bee071434..36e0d9b5d824 100644

fs/btrfs/block-group.c void btrfs_reclaim_bgs_work(struct work_struct *work) · line 1839

Quoted 8 lines
fs/btrfs/block-group.c +4 -1
@@ -1833,7 +1833,18 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
                }
 
 next:
-               btrfs_put_block_group(bg);
+               if (!ret) {
+                       btrfs_put_block_group(bg);
+               } else {
+                       spin_lock(&bg->lock);

Why do we need to take bg->lock?
The ->bg_list is protected by fs_info->unused_bgs_lock alone.

fs/btrfs/block-group.c void btrfs_reclaim_bgs_work(struct work_struct *work) · line 1847

Quoted 8 lines
fs/btrfs/block-group.c +8 -0
+                       spin_lock(&fs_info->unused_bgs_lock);
+                       if (list_empty(&bg->bg_list))
+                               list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
+                       else
+                               btrfs_put_block_group(bg);
+                       spin_unlock(&fs_info->unused_bgs_lock);
+                       spin_unlock(&bg->lock);
+               }

Also, this is very similar to btrfs_mark_bg_to_reclaim(), so we should use that, and simply have:

btrfs_mark_bg_to_reclaim();
btrfs_put_block_group(bg);

Thanks.

Quoted 6 lines
fs/btrfs/block-group.c +0 -1
                mutex_unlock(&fs_info->reclaim_bgs_lock);
                /*
--

2.40.1

#14 lore raw

Re: [PATCH 4/4] btrfs: reinsert BGs failed to reclaim

Quoted 10 lines · Filipe Manana

Review

Quoted 8 lines · Naohiro Aota
(quoted diff) +8 -0
+                       spin_lock(&fs_info->unused_bgs_lock);
+                       if (list_empty(&bg->bg_list))
+                               list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
+                       else
+                               btrfs_put_block_group(bg);
+                       spin_unlock(&fs_info->unused_bgs_lock);
+                       spin_unlock(&bg->lock);
+               }
Quoted 5 lines

Also, this is very similar to btrfs_mark_bg_to_reclaim(), so we should use that, and simply have:

btrfs_mark_bg_to_reclaim();
btrfs_put_block_group(bg);

I can fold the diff below if you agree

fs/btrfs/block-group.c +3 -12
@@ -1833,18 +1833,9 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
                }
 
 next:
-               if (!ret) {
-                       btrfs_put_block_group(bg);
-               } else {
-                       spin_lock(&bg->lock);
-                       spin_lock(&fs_info->unused_bgs_lock);
-                       if (list_empty(&bg->bg_list))
-                               list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
-                       else
-                               btrfs_put_block_group(bg);
-                       spin_unlock(&fs_info->unused_bgs_lock);
-                       spin_unlock(&bg->lock);
-               }
+               if (!ret)
+                       btrfs_mark_bg_to_reclaim(bg);
+               btrfs_put_block_group(bg);
 
                mutex_unlock(&fs_info->reclaim_bgs_lock);
                /*

---

#15 lore raw

Re: [PATCH 4/4] btrfs: reinsert BGs failed to reclaim

Quoted 11 lines · David Sterba
Quoted 10 lines · Filipe Manana
Quoted 8 lines · Naohiro Aota
(quoted diff) +8 -0
+                       spin_lock(&fs_info->unused_bgs_lock);
+                       if (list_empty(&bg->bg_list))
+                               list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
+                       else
+                               btrfs_put_block_group(bg);
+                       spin_unlock(&fs_info->unused_bgs_lock);
+                       spin_unlock(&bg->lock);
+               }
Quoted 5 lines
Quoted 5 lines

Also, this is very similar to btrfs_mark_bg_to_reclaim(), so we should use that, and simply have:

btrfs_mark_bg_to_reclaim();
btrfs_put_block_group(bg);

Yeah, this looks nice. Thank you.

Quoted 13 lines

I can fold the diff below if you agree

fs/btrfs/block-group.c +0 -4
@@ -1833,18 +1833,9 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
                }
 
 next:
-               if (!ret) {
-                       btrfs_put_block_group(bg);
-               } else {
-                       spin_lock(&bg->lock);

fs/btrfs/block-group.c void btrfs_reclaim_bgs_work(struct work_struct *work) · line 1837

Quoted 10 lines
fs/btrfs/block-group.c +2 -8
-                       spin_lock(&fs_info->unused_bgs_lock);
-                       if (list_empty(&bg->bg_list))
-                               list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
-                       else
-                               btrfs_put_block_group(bg);
-                       spin_unlock(&fs_info->unused_bgs_lock);
-                       spin_unlock(&bg->lock);
-               }
+               if (!ret)
+                       btrfs_mark_bg_to_reclaim(bg);

Thank you for folding this, but the condition is flipped. We should add it back to the list in a failure case.

Quoted 5 lines
fs/btrfs/block-group.c +1 -1
+               btrfs_put_block_group(bg);
 
                mutex_unlock(&fs_info->reclaim_bgs_lock);
                /*
---
#16 lore raw

Re: [PATCH 4/4] btrfs: reinsert BGs failed to reclaim

Quoted 38 lines · Naohiro Aota
Quoted 11 lines · David Sterba
Quoted 10 lines · Filipe Manana
Quoted 8 lines · Naohiro Aota
(quoted diff) +8 -0
+                       spin_lock(&fs_info->unused_bgs_lock);
+                       if (list_empty(&bg->bg_list))
+                               list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
+                       else
+                               btrfs_put_block_group(bg);
+                       spin_unlock(&fs_info->unused_bgs_lock);
+                       spin_unlock(&bg->lock);
+               }
Quoted 5 lines
Quoted 5 lines

Also, this is very similar to btrfs_mark_bg_to_reclaim(), so we should use that, and simply have:

btrfs_mark_bg_to_reclaim();
btrfs_put_block_group(bg);

Yeah, this looks nice. Thank you.

Quoted 18 lines

I can fold the diff below if you agree

fs/btrfs/block-group.c +0 -9
@@ -1833,18 +1833,9 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
                }
 
 next:
-               if (!ret) {
-                       btrfs_put_block_group(bg);
-               } else {
-                       spin_lock(&bg->lock);
-                       spin_lock(&fs_info->unused_bgs_lock);
-                       if (list_empty(&bg->bg_list))
-                               list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
-                       else
-                               btrfs_put_block_group(bg);
Quoted 8 lines

fs/btrfs/block-group.c void btrfs_reclaim_bgs_work(struct work_struct *work) · line 1837

Quoted 5 lines
fs/btrfs/block-group.c +2 -3
-                       spin_unlock(&fs_info->unused_bgs_lock);
-                       spin_unlock(&bg->lock);
-               }
+               if (!ret)
+                       btrfs_mark_bg_to_reclaim(bg);

Thank you for folding this, but the condition is flipped. We should add it back to the list in a failure case.

Ah right, fixed and pushed. The whole diff becomes:

               if (ret)
                       btrfs_mark_bg_to_reclaim(bg);
#17 lore raw
In reply to Naohiro Aota
Quoted 7 lines · Naohiro Aota

There are several issues on the reclaim process out there:

- Long-running reclaim process blocks removing unused BGs
- Belonging to the reclaim list blocks it goes to the unused list
- It tries relocation even when FS is read-only
- Temporal failure keep a block group un-reclaimed

Quoted 7 lines

This series fixes them.

Naohiro Aota (4):
btrfs: delete unused BGs while reclaiming BGs
btrfs: move out now unused BG from the reclaim list
btrfs: bail out reclaim process if filesystem is read-only
btrfs: reinsert BGs failed to reclaim

Added to misc-next, with the fixups, thanks.