Skip to contentSkip to conversation

f2fs: prevent the current section from being selected as a victim during garbage collection

PATCH In review Yohan Joung 3 participants 17 messages Mar 26, 2025 – Apr 2, 2025 F2FS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs: prevent the current section from being selected as a victim during garbage collection

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in use.
This behavior causes inconsistency between the SIT and SSA.

---
 fs/f2fs/gc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/f2fs/gc.c +4 -0
index 2b8f9239bede..4b5d18e395eb 100644
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control)
 		goto stop;
 	}
 
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
 	seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
 				gc_control->should_migrate_blocks,
 				gc_control->one_time);
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 4 lines · Yohan Joung

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in use.
This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

https://lore.kernel.org/linux-f2fs-devel/[email protected]

Thanks,

Quoted 21 lines
---
 fs/f2fs/gc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/f2fs/gc.c +4 -0
index 2b8f9239bede..4b5d18e395eb 100644
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control)
 		goto stop;
 	}
 
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
 	seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
 				gc_control->should_migrate_blocks,
 				gc_control->one_time);
#2 lore raw
Quoted 8 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 7 lines
Quoted 4 lines · Yohan Joung

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in use.
This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

This is an issue that arises when dividing
a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

Because the call stack is different,
I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides. What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment (70961) type [0, 1] in SSA and SIT
[30146.346151][ T1300] Call trace:
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c
[30146.346157][ T1300]  show_stack+0x18/0x28
[30146.346158][ T1300]  dump_stack_lvl+0x50/0x6c
[30146.346161][ T1300]  dump_stack+0x18/0x28
[30146.346162][ T1300]  f2fs_stop_checkpoint+0x1c/0x3c
[30146.346165][ T1300]  do_garbage_collect+0x41c/0x271c
[30146.346167][ T1300]  f2fs_gc+0x27c/0x828
[30146.346168][ T1300]  gc_thread_func+0x290/0x88c
[30146.346169][ T1300]  kthread+0x11c/0x164
[30146.346172][ T1300]  ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961
}
Quoted 21 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.3291947-2-
[email protected]

Thanks,

fs/f2fs/gc.c int f2fs_gc(struct f2fs_sb_info *sbi, struct

Quoted 11 lines
---
 fs/f2fs/gc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/f2fs/gc.c index renamed from fs/f2fs/gc.c +0 -0

2b8f9239bede..4b5d18e395eb 100644

fs/f2fs/gc.c +0 -0
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi, struct

f2fs_gc_control *gc_control)

Quoted 3 lines
fs/f2fs/gc.c +0 -0
 		goto stop;
 	}
 
Quoted 7 lines

fs/f2fs/gc.c int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control) · line 1935

Quoted 7 lines
fs/f2fs/gc.c +4 -0
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
 	seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
 				gc_control->should_migrate_blocks,
 				gc_control->one_time);

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

maintainer
#3 lore raw
Quoted 14 lines · On 3/27/25 14:43, [email protected] wrote:
Quoted 14 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 4 lines · Yohan Joung

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in use.
This behavior causes inconsistency between the SIT and SSA.

Quoted 7 lines
Quoted 1 line

Hi, does this fix your issue?

This is an issue that arises when dividing
a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/ sec_usage_check().

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Thanks,

Quoted 56 lines

Because the call stack is different,
I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides. What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment (70961) type [0, 1] in SSA and SIT
[30146.346151][ T1300] Call trace:
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c
[30146.346157][ T1300]  show_stack+0x18/0x28
[30146.346158][ T1300]  dump_stack_lvl+0x50/0x6c
[30146.346161][ T1300]  dump_stack+0x18/0x28
[30146.346162][ T1300]  f2fs_stop_checkpoint+0x1c/0x3c
[30146.346165][ T1300]  do_garbage_collect+0x41c/0x271c
[30146.346167][ T1300]  f2fs_gc+0x27c/0x828
[30146.346168][ T1300]  gc_thread_func+0x290/0x88c
[30146.346169][ T1300]  kthread+0x11c/0x164
[30146.346172][ T1300]  ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961
}
Quoted 28 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.3291947-2-
[email protected]

Thanks,

fs/f2fs/gc.c int f2fs_gc(struct f2fs_sb_info *sbi, struct

Quoted 11 lines
---
 fs/f2fs/gc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/f2fs/gc.c index renamed from fs/f2fs/gc.c +0 -0

2b8f9239bede..4b5d18e395eb 100644

fs/f2fs/gc.c +0 -0
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi, struct

f2fs_gc_control *gc_control)

Quoted 10 lines
fs/f2fs/gc.c +4 -0
 		goto stop;
 	}
 
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
 	seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
 				gc_control->should_migrate_blocks,
 				gc_control->one_time);
#4 lore raw
Quoted 54 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 4:30 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 18 lines · On 3/27/25 14:43, [email protected] wrote:
Quoted 10 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 7 lines
Quoted 4 lines · Yohan Joung

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in use. This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

Quoted 7 lines

This is an issue that arises when dividing a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section
f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/ sec_usage_check().

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Quoted 1 line

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

@@ -850,15 +850,20 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,

                        p.min_segno = sbi->next_victim_seg[BG_GC];
                        *result = p.min_segno;
                        sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                }
                if (gc_type == FG_GC &&
                                sbi->next_victim_seg[FG_GC] != NULL_SEGNO) {
                        p.min_segno = sbi->next_victim_seg[FG_GC];
                        *result = p.min_segno;
                        sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
-                       goto got_result;
                }

+
+ secno = GET_SEC_FROM_SEG(sbi, segno);
+
+ if (sec_usage_check(sbi, secno))
+ goto next;
+

+               goto got_result;
        }
Quoted 47 lines
Quoted 47 lines

Because the call stack is different,
I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides. What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment (70961)
type [0, 1] in SSA and SIT [30146.346151][ T1300] Call trace:
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c [30146.346157][
T1300]  show_stack+0x18/0x28 [30146.346158][ T1300]
dump_stack_lvl+0x50/0x6c [30146.346161][ T1300]  dump_stack+0x18/0x28
[30146.346162][ T1300]  f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][
T1300]  do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]  gc_thread_func+0x290/0x88c
[30146.346169][ T1300]  kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961
}
Quoted 21 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.3291947-2-
[email protected]

Thanks,

fs/f2fs/gc.c int f2fs_gc(struct f2fs_sb_info *sbi, struct

Quoted 11 lines
---
 fs/f2fs/gc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/f2fs/gc.c index renamed from fs/f2fs/gc.c +0 -0

2b8f9239bede..4b5d18e395eb 100644

fs/f2fs/gc.c +0 -0
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi, struct

f2fs_gc_control *gc_control)

Quoted 3 lines
fs/f2fs/gc.c +0 -0
 		goto stop;
 	}
 
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

fs/f2fs/gc.c int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control) · line 1935

Quoted 7 lines
fs/f2fs/gc.c +4 -0
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
 	seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
 				gc_control->should_migrate_blocks,
 				gc_control->one_time);

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

maintainer
#5 lore raw
Quoted 71 lines · On 2025/3/27 16:00, [email protected] wrote:
Quoted 54 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 4:30 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 18 lines · On 3/27/25 14:43, [email protected] wrote:
Quoted 10 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 7 lines
Quoted 4 lines · Yohan Joung

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in use. This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

Quoted 7 lines

This is an issue that arises when dividing a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section
f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/ sec_usage_check().

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Quoted 1 line

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

(quoted diff) +0 -1
@@ -850,15 +850,20 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
                         p.min_segno = sbi->next_victim_seg[BG_GC];
                         *result = p.min_segno;
                         sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                 }
                 if (gc_type == FG_GC &&
                                 sbi->next_victim_seg[FG_GC] != NULL_SEGNO) {
                         p.min_segno = sbi->next_victim_seg[FG_GC];
                         *result = p.min_segno;
                         sbi->next_victim_seg[FG_GC] = NULL_SEGNO;

Review int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result, · line 867

Quoted 10 lines
(quoted diff) +7 -1
-                       goto got_result;
                 }
+
+               secno = GET_SEC_FROM_SEG(sbi, segno);
+
+               if (sec_usage_check(sbi, secno))
+                       goto next;
+
+               goto got_result;
         }

But still allocator can assign this segment after sec_usage_check() in race condition, right?

IMO, we can clear next_victim_seg[] once section is free in __set_test_and_free()? something like this:

---
 fs/f2fs/segment.h | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
fs/f2fs/segment.h +10 -3
index 0465dc00b349..826e37999085 100644
@@ -473,9 +473,16 @@ static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
 			goto skip_free;
 		next = find_next_bit(free_i->free_segmap,
 				start_segno + SEGS_PER_SEC(sbi), start_segno);
-		if (next >= start_segno + usable_segs) {
-			if (test_and_clear_bit(secno, free_i->free_secmap))
-				free_i->free_sections++;
+		if ((next >= start_segno + usable_segs) &&
+			test_and_clear_bit(secno, free_i->free_secmap)) {
+			free_i->free_sections++;
+
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[BG_GC]) ==
+									secno)
+				sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[FG_GC]) ==
+									secno)
+				sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
 		}
 	}
 skip_free:
Signature · 1 line
2.40.1
Quoted 56 lines
Quoted 55 lines
Quoted 55 lines

Because the call stack is different,
I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides. What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment (70961)
type [0, 1] in SSA and SIT [30146.346151][ T1300] Call trace:
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c [30146.346157][
T1300]  show_stack+0x18/0x28 [30146.346158][ T1300]
dump_stack_lvl+0x50/0x6c [30146.346161][ T1300]  dump_stack+0x18/0x28
[30146.346162][ T1300]  f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][
T1300]  do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]  gc_thread_func+0x290/0x88c
[30146.346169][ T1300]  kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961
}
Quoted 28 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.3291947-2-
[email protected]

Thanks,

fs/f2fs/gc.c int f2fs_gc(struct f2fs_sb_info *sbi, struct

Quoted 11 lines
---
  fs/f2fs/gc.c | 4 ++++
  1 file changed, 4 insertions(+)
fs/f2fs/gc.c index renamed from fs/f2fs/gc.c +0 -0

2b8f9239bede..4b5d18e395eb 100644

fs/f2fs/gc.c +0 -0
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi, struct

f2fs_gc_control *gc_control)

Quoted 10 lines
fs/f2fs/gc.c +4 -0
  		goto stop;
  	}
 
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
  	seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
  				gc_control->should_migrate_blocks,
  				gc_control->one_time);
#6 lore raw
Quoted 90 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 10:48 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [External Mail] Re:
[PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Review int f2fs_get_victim(struct f2fs_sb_info *sbi,

Quoted 63 lines · On 2025/3/27 16:00, [email protected] wrote:
Quoted 56 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 4:30 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [PATCH] f2fs:
prevent the current section from being selected as a victim during garbage collection

Quoted 18 lines · On 3/27/25 14:43, [email protected] wrote:
Quoted 10 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 7 lines
Quoted 4 lines · Yohan Joung

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in use. This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

Quoted 7 lines

This is an issue that arises when dividing a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section
f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/ sec_usage_check().

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Quoted 1 line

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

(quoted diff) +0 -0
@@ -850,15 +850,20 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi,

unsigned int *result,

Quoted 15 lines
(quoted diff) +3 -2
                         p.min_segno = sbi->next_victim_seg[BG_GC];
                         *result = p.min_segno;
                         sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                 }
                 if (gc_type == FG_GC &&
                                 sbi->next_victim_seg[FG_GC] != NULL_SEGNO) {
                         p.min_segno = sbi->next_victim_seg[FG_GC];
                         *result = p.min_segno;
                         sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
-                       goto got_result;
                 }
+
+               secno = GET_SEC_FROM_SEG(sbi, segno);
+
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info *sbi, · line 867

Quoted 5 lines
(quoted diff) +4 -0
+               if (sec_usage_check(sbi, secno))
+                       goto next;
+
+               goto got_result;
         }

But still allocator can assign this segment after sec_usage_check() in race condition, right?

Since the BG GC using next_victim takes place after the SIT update in do_checkpoint, it seems unlikely that a race condition with sec_usage_check will occur.

Quoted 3 lines

IMO, we can clear next_victim_seg[] once section is free in __set_test_and_free()? something like this:

I will test it according to your suggestion.
If there are no issues, can I submit it again with the patch? Thanks

Quoted 91 lines
---
  fs/f2fs/segment.h | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
fs/f2fs/segment.h index renamed from fs/f2fs/segment.h +0 -0

0465dc00b349..826e37999085 100644

fs/f2fs/segment.h +10 -3
@@ -473,9 +473,16 @@ static inline void __set_test_and_free(struct
 f2fs_sb_info *sbi,
  			goto skip_free;
  		next = find_next_bit(free_i->free_segmap,
  				start_segno + SEGS_PER_SEC(sbi), start_segno);
-		if (next >= start_segno + usable_segs) {
-			if (test_and_clear_bit(secno, free_i->free_secmap))
-				free_i->free_sections++;
+		if ((next >= start_segno + usable_segs) &&
+			test_and_clear_bit(secno, free_i->free_secmap)) {
+			free_i->free_sections++;
+
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[BG_GC]) ==
+									secno)
+				sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[FG_GC]) ==
+									secno)
+				sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
  		}
  	}

skip_free:
--
2.40.1

Quoted 58 lines
Quoted 57 lines
Quoted 57 lines

Because the call stack is different, I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides.
What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment (70961)
type [0, 1] in SSA and SIT [30146.346151][ T1300] Call trace:
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c [30146.346157][
T1300]  show_stack+0x18/0x28 [30146.346158][ T1300]
dump_stack_lvl+0x50/0x6c [30146.346161][ T1300]
dump_stack+0x18/0x28 [30146.346162][ T1300]
f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][ T1300]
do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]
gc_thread_func+0x290/0x88c [30146.346169][ T1300]
kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961 }
Quoted 29 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.3291947-2-
[email protected]

Thanks,

Quoted 11 lines
---
  fs/f2fs/gc.c | 4 ++++
  1 file changed, 4 insertions(+)
fs/f2fs/gc.c index renamed from fs/f2fs/gc.c +0 -0

2b8f9239bede..4b5d18e395eb 100644

fs/f2fs/gc.c +0 -0
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi,
Quoted 1 line

struct

f2fs_gc_control *gc_control)

Quoted 10 lines
  		goto stop;
  	}
fs/f2fs/gc.c +4 -0
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
  	seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
  				gc_control->should_migrate_blocks,
  				gc_control->one_time);
maintainer
#7 lore raw
Quoted 98 lines · yohan.joung
Quoted 90 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 10:48 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [External Mail] Re:
[PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Review int f2fs_get_victim(struct f2fs_sb_info *sbi,

Quoted 63 lines · On 2025/3/27 16:00, [email protected] wrote:
Quoted 56 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 4:30 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [PATCH] f2fs:
prevent the current section from being selected as a victim during garbage collection

Quoted 18 lines · On 3/27/25 14:43, [email protected] wrote:
Quoted 10 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 7 lines
Quoted 4 lines · Yohan Joung

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in use. This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

Quoted 7 lines

This is an issue that arises when dividing a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section
f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/ sec_usage_check().

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Quoted 1 line

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

(quoted diff) +0 -0
@@ -850,15 +850,20 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi,

unsigned int *result,

Quoted 15 lines
(quoted diff) +3 -2
                          p.min_segno = sbi->next_victim_seg[BG_GC];
                          *result = p.min_segno;
                          sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                  }
                  if (gc_type == FG_GC &&
                                  sbi->next_victim_seg[FG_GC] != NULL_SEGNO) {
                          p.min_segno = sbi->next_victim_seg[FG_GC];
                          *result = p.min_segno;
                          sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
-                       goto got_result;
                  }
+
+               secno = GET_SEC_FROM_SEG(sbi, segno);
+
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info *sbi, · line 867

Quoted 5 lines
(quoted diff) +4 -0
+               if (sec_usage_check(sbi, secno))
+                       goto next;
+
+               goto got_result;
          }

But still allocator can assign this segment after sec_usage_check() in race condition, right?

Quoted 2 lines

Since the BG GC using next_victim takes place after the SIT update in do_checkpoint, it seems unlikely that a race condition with sec_usage_check will occur.

I mean this:

- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
- f2fs_allocate_data_block
- new_curseg
- get_new_segment find segno #1

- do_garbage_collect

Thanks,

Quoted 98 lines
Quoted 3 lines

IMO, we can clear next_victim_seg[] once section is free in __set_test_and_free()? something like this:

I will test it according to your suggestion.
If there are no issues, can I submit it again with the patch? Thanks

Quoted 91 lines
---
   fs/f2fs/segment.h | 13 ++++++++++---
   1 file changed, 10 insertions(+), 3 deletions(-)
fs/f2fs/segment.h index renamed from fs/f2fs/segment.h +0 -0

0465dc00b349..826e37999085 100644

fs/f2fs/segment.h +10 -3
@@ -473,9 +473,16 @@ static inline void __set_test_and_free(struct
 f2fs_sb_info *sbi,
   			goto skip_free;
   		next = find_next_bit(free_i->free_segmap,
   				start_segno + SEGS_PER_SEC(sbi), start_segno);
-		if (next >= start_segno + usable_segs) {
-			if (test_and_clear_bit(secno, free_i->free_secmap))
-				free_i->free_sections++;
+		if ((next >= start_segno + usable_segs) &&
+			test_and_clear_bit(secno, free_i->free_secmap)) {
+			free_i->free_sections++;
+
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[BG_GC]) ==
+									secno)
+				sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[FG_GC]) ==
+									secno)
+				sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
   		}
   	}

skip_free:
--
2.40.1

Quoted 58 lines
Quoted 57 lines
Quoted 57 lines

Because the call stack is different, I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides.
What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment (70961)
type [0, 1] in SSA and SIT [30146.346151][ T1300] Call trace:
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c [30146.346157][
T1300]  show_stack+0x18/0x28 [30146.346158][ T1300]
dump_stack_lvl+0x50/0x6c [30146.346161][ T1300]
dump_stack+0x18/0x28 [30146.346162][ T1300]
f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][ T1300]
do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]
gc_thread_func+0x290/0x88c [30146.346169][ T1300]
kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961 }
Quoted 29 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.3291947-2-
[email protected]

Thanks,

Quoted 11 lines
---
   fs/f2fs/gc.c | 4 ++++
   1 file changed, 4 insertions(+)
fs/f2fs/gc.c index renamed from fs/f2fs/gc.c +0 -0

2b8f9239bede..4b5d18e395eb 100644

fs/f2fs/gc.c +0 -0
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi,
Quoted 1 line

struct

f2fs_gc_control *gc_control)

Quoted 10 lines
   		goto stop;
   	}
fs/f2fs/gc.c +4 -0
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
   	seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
   				gc_control->should_migrate_blocks,
   				gc_control->one_time);
#8 lore raw
Quoted 116 lines · yohan.joung
Quoted 34 lines
Quoted 34 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 10:48 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [External Mail] Re:
[PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 22 lines · On 2025/3/27 16:00, [email protected] wrote:
Quoted 22 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 4:30 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [PATCH] f2fs:
prevent the current section from being selected as a victim during garbage collection

Quoted 10 lines · On 3/27/25 14:43, [email protected] wrote:
Quoted 10 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines · Yohan Joung
Quoted 3 lines

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in

use.

Quoted 62 lines
Quoted 54 lines

Review int f2fs_get_victim(struct f2fs_sb_info *sbi,

Quoted 38 lines
Quoted 31 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

Quoted 8 lines

This is an issue that arises when dividing a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section
f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/ sec_usage_check().

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Quoted 1 line

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

(quoted diff) +0 -0
@@ -850,15 +850,20 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi,

unsigned int *result,

Quoted 15 lines
(quoted diff) +3 -2
                          p.min_segno = sbi->next_victim_seg[BG_GC];
                          *result = p.min_segno;
                          sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                  }
                  if (gc_type == FG_GC &&
                                  sbi->next_victim_seg[FG_GC] != NULL_SEGNO) {
                          p.min_segno = sbi->next_victim_seg[FG_GC];
                          *result = p.min_segno;
                          sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
-                       goto got_result;
                  }
+
+               secno = GET_SEC_FROM_SEG(sbi, segno);
+
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info *sbi, · line 867

Quoted 5 lines
(quoted diff) +4 -0
+               if (sec_usage_check(sbi, secno))
+                       goto next;
+
+               goto got_result;
          }

But still allocator can assign this segment after sec_usage_check() in race condition, right?

Quoted 2 lines

Since the BG GC using next_victim takes place after the SIT update in do_checkpoint, it seems unlikely that a race condition with

sec_usage_check will occur.

I mean this:

- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
- f2fs_allocate_data_block
- new_curseg
- get_new_segment find segno #1

Quoted 3 lines

- do_garbage_collect

Thanks,

do_checkpoint sec0 free
If sec0 is not freed, then segno1 within sec0 cannot be allocated
- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs (but sec0 is already used)
- f2fs_allocate_data_block
- new_curseg
- get_new_segment find segno #1

- do_garbage_collect

I appreciate your patch, it is under testing.
but I'm wondering if there's a risk of a race condition in this situation

Quoted 100 lines
Quoted 100 lines
Quoted 4 lines

IMO, we can clear next_victim_seg[] once section is free in __set_test_and_free()? something like this:

I will test it according to your suggestion.
If there are no issues, can I submit it again with the patch? Thanks

Quoted 92 lines
---
   fs/f2fs/segment.h | 13 ++++++++++---
   1 file changed, 10 insertions(+), 3 deletions(-)
fs/f2fs/segment.h index renamed from fs/f2fs/segment.h +0 -0

0465dc00b349..826e37999085 100644

fs/f2fs/segment.h +10 -3
@@ -473,9 +473,16 @@ static inline void __set_test_and_free(struct
 f2fs_sb_info *sbi,
   			goto skip_free;
   		next = find_next_bit(free_i->free_segmap,
   				start_segno + SEGS_PER_SEC(sbi), start_segno);
-		if (next >= start_segno + usable_segs) {
-			if (test_and_clear_bit(secno, free_i->free_secmap))
-				free_i->free_sections++;
+		if ((next >= start_segno + usable_segs) &&
+			test_and_clear_bit(secno, free_i->free_secmap)) {
+			free_i->free_sections++;
+
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[BG_GC]) ==
+									secno)
+				sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[FG_GC]) ==
+									secno)
+				sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
   		}
   	}

skip_free:
--
2.40.1

Quoted 59 lines
Quoted 58 lines
Quoted 58 lines

Because the call stack is different, I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides. What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment
(70961) type [0, 1] in SSA and SIT [30146.346151][ T1300] Call trace:
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c [30146.346157][
T1300]  show_stack+0x18/0x28 [30146.346158][ T1300]
dump_stack_lvl+0x50/0x6c [30146.346161][ T1300]
dump_stack+0x18/0x28 [30146.346162][ T1300]
f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][ T1300]
do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]
gc_thread_func+0x290/0x88c [30146.346169][ T1300]
kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961 }
Quoted 30 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.3291947-2
-
[email protected]

Thanks,

Quoted 11 lines
---
   fs/f2fs/gc.c | 4 ++++
   1 file changed, 4 insertions(+)
fs/f2fs/gc.c index renamed from fs/f2fs/gc.c +0 -0

2b8f9239bede..4b5d18e395eb 100644

fs/f2fs/gc.c +0 -0
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi,
Quoted 1 line

struct

f2fs_gc_control *gc_control)

Quoted 10 lines
   		goto stop;
   	}
fs/f2fs/gc.c +4 -0
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
   	seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
   				gc_control->should_migrate_blocks,
   				gc_control->one_time);
maintainer
#9 lore raw
Quoted 130 lines · yohan.joung
Quoted 116 lines · yohan.joung
Quoted 34 lines
Quoted 34 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 10:48 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [External Mail] Re:
[PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 22 lines · On 2025/3/27 16:00, [email protected] wrote:
Quoted 22 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 4:30 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [PATCH] f2fs:
prevent the current section from being selected as a victim during garbage collection

Quoted 10 lines · On 3/27/25 14:43, [email protected] wrote:
Quoted 10 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines · Yohan Joung
Quoted 3 lines

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in

use.

Quoted 62 lines
Quoted 54 lines

Review int f2fs_get_victim(struct f2fs_sb_info *sbi,

Quoted 38 lines
Quoted 31 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

Quoted 8 lines

This is an issue that arises when dividing a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section
f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/ sec_usage_check().

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Quoted 1 line

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

(quoted diff) +0 -0
@@ -850,15 +850,20 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi,

unsigned int *result,

Quoted 15 lines
(quoted diff) +3 -2
                           p.min_segno = sbi->next_victim_seg[BG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
                   if (gc_type == FG_GC &&
                                   sbi->next_victim_seg[FG_GC] != NULL_SEGNO) {
                           p.min_segno = sbi->next_victim_seg[FG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
+
+               secno = GET_SEC_FROM_SEG(sbi, segno);
+
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info *sbi, · line 867

Quoted 5 lines
(quoted diff) +4 -0
+               if (sec_usage_check(sbi, secno))
+                       goto next;
+
+               goto got_result;
           }

But still allocator can assign this segment after sec_usage_check() in race condition, right?

Quoted 2 lines

Since the BG GC using next_victim takes place after the SIT update in do_checkpoint, it seems unlikely that a race condition with

sec_usage_check will occur.

I mean this:

- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
- f2fs_allocate_data_block
- new_curseg
- get_new_segment find segno #1

Quoted 3 lines

- do_garbage_collect

Thanks,

do_checkpoint sec0 free
If sec0 is not freed, then segno1 within sec0 cannot be allocated
- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs (but sec0 is already used)
- f2fs_allocate_data_block
- new_curseg
- get_new_segment find segno #1

Quoted 4 lines

- do_garbage_collect

I appreciate your patch, it is under testing.
but I'm wondering if there's a risk of a race condition in this situation

Oh, yes, I may missed that get_new_segment can return a free segment in partial used section.

So what do you think of this?
- check CURSEG() in do_garbage_collect() and get_victim()
- reset next_victim_seg[] in get_new_segment() and __set_test_and_free() during checkpoint.

Thanks,

Quoted 103 lines
Quoted 102 lines
Quoted 102 lines
Quoted 6 lines

IMO, we can clear next_victim_seg[] once section is free in __set_test_and_free()? something like this:

I will test it according to your suggestion.
If there are no issues, can I submit it again with the patch? Thanks

Quoted 92 lines
---
    fs/f2fs/segment.h | 13 ++++++++++---
    1 file changed, 10 insertions(+), 3 deletions(-)
fs/f2fs/segment.h index renamed from fs/f2fs/segment.h +0 -0

0465dc00b349..826e37999085 100644

fs/f2fs/segment.h +10 -3
@@ -473,9 +473,16 @@ static inline void __set_test_and_free(struct
 f2fs_sb_info *sbi,
    			goto skip_free;
    		next = find_next_bit(free_i->free_segmap,
    				start_segno + SEGS_PER_SEC(sbi), start_segno);
-		if (next >= start_segno + usable_segs) {
-			if (test_and_clear_bit(secno, free_i->free_secmap))
-				free_i->free_sections++;
+		if ((next >= start_segno + usable_segs) &&
+			test_and_clear_bit(secno, free_i->free_secmap)) {
+			free_i->free_sections++;
+
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[BG_GC]) ==
+									secno)
+				sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[FG_GC]) ==
+									secno)
+				sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
    		}
    	}

skip_free:
--
2.40.1

Quoted 59 lines
Quoted 58 lines
Quoted 58 lines

Because the call stack is different, I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides. What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment
(70961) type [0, 1] in SSA and SIT [30146.346151][ T1300] Call trace:
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c [30146.346157][
T1300]  show_stack+0x18/0x28 [30146.346158][ T1300]
dump_stack_lvl+0x50/0x6c [30146.346161][ T1300]
dump_stack+0x18/0x28 [30146.346162][ T1300]
f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][ T1300]
do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]
gc_thread_func+0x290/0x88c [30146.346169][ T1300]
kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961 }
Quoted 30 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.3291947-2
-
[email protected]

Thanks,

Quoted 11 lines
---
    fs/f2fs/gc.c | 4 ++++
    1 file changed, 4 insertions(+)
fs/f2fs/gc.c index renamed from fs/f2fs/gc.c +0 -0

2b8f9239bede..4b5d18e395eb 100644

fs/f2fs/gc.c +0 -0
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi,
Quoted 1 line

struct

f2fs_gc_control *gc_control)

Quoted 10 lines
    		goto stop;
    	}
fs/f2fs/gc.c +4 -0
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
    	seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
    				gc_control->should_migrate_blocks,
    				gc_control->one_time);
#10 lore raw
Quoted 149 lines · yohan.joung
Quoted 55 lines
Quoted 55 lines · yohan.joung
Quoted 35 lines
Quoted 35 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 10:48 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [External Mail] Re:
[PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 23 lines · On 2025/3/27 16:00, [email protected] wrote:
Quoted 23 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 4:30 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [PATCH] f2fs:
prevent the current section from being selected as a victim during garbage collection

Quoted 11 lines · On 3/27/25 14:43, [email protected] wrote:
Quoted 11 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines · Yohan Joung
Quoted 4 lines

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in

use.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 3 lines
Quoted 1 line

This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

This is an issue that arises when dividing a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section
f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/

sec_usage_check().

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 23 lines
Quoted 16 lines

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Quoted 1 line

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

Review int f2fs_get_victim(struct f2fs_sb_info · line 850

Quoted 1 line
(quoted diff) +0 -0
@@ -850,15 +850,20 @@ int f2fs_get_victim(struct f2fs_sb_info
 *sbi,

unsigned int *result,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Review int f2fs_get_victim(struct f2fs_sb_info · line 856

Quoted 7 lines
(quoted diff) +0 -1
                           p.min_segno = sbi->next_victim_seg[BG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
                   if (gc_type == FG_GC &&
                                   sbi->next_victim_seg[FG_GC] != NULL_SEGNO)

{

Quoted 30 lines
Quoted 30 lines
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +3 -1
                           p.min_segno = sbi->next_victim_seg[FG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
+
+               secno = GET_SEC_FROM_SEG(sbi, segno);
+
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info · line 868

Quoted 5 lines
(quoted diff) +4 -0
+               if (sec_usage_check(sbi, secno))
+                       goto next;
+
+               goto got_result;
           }

But still allocator can assign this segment after sec_usage_check() in race condition, right?

Quoted 2 lines

Since the BG GC using next_victim takes place after the SIT update in do_checkpoint, it seems unlikely that a race condition with

sec_usage_check will occur.

I mean this:

- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
- f2fs_allocate_data_block
- new_curseg
- get_new_segment find segno #1

Quoted 6 lines
Quoted 3 lines

- do_garbage_collect

Thanks,

do_checkpoint sec0 free If sec0 is not freed, then

segno1 within sec0 cannot be

Quoted 5 lines

allocated
- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs (but sec0

is already used)

Quoted 3 lines

- f2fs_allocate_data_block
- new_curseg
- get_new_segment find

segno #1

Quoted 6 lines

- do_garbage_collect

I appreciate your patch, it is under testing.
but I'm wondering if there's a risk of a race condition in this situation

Oh, yes, I may missed that get_new_segment can return a free segment in partial used section.

Quoted 6 lines

So what do you think of this?
- check CURSEG() in do_garbage_collect() and get_victim()
- reset next_victim_seg[] in get_new_segment() and __set_test_and_free() during checkpoint.

Thanks,

How about using victim_secmap?
gc_thread

				mutex_lock(&DIRTY_I(sbi)->seglist_lock);
				__set_test_and_free
				check cur section next_victim clear 
				mutex_unlock(&dirty_i->seglist_lock);

mutex_lock(&dirty->seglist_lock);
f2fs_get_victim
mutex_unlock(&dirty_i->seglist_lock);

static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

                if (next >= start_segno + usable_segs) {
                        if (test_and_clear_bit(secno, free_i->free_secmap))
                                free_i->free_sections++;
+
+                       if (test_and_clear_bit(secno, dirty_i->victim_secmap))
+                               sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
                }
        }
Quoted 107 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 7 lines

IMO, we can clear next_victim_seg[] once section is free in __set_test_and_free()? something like this:

I will test it according to your suggestion.
If there are no issues, can I submit it again with the patch? Thanks

Quoted 5 lines
---
    fs/f2fs/segment.h | 13 ++++++++++---
    1 file changed, 10 insertions(+), 3 deletions(-)
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

fs/f2fs/segment.h static inline void __set_test_and_free(struct · line 476

Quoted 9 lines
fs/f2fs/segment.h index renamed from fs/f2fs/segment.h +0 -0

0465dc00b349..826e37999085 100644

fs/f2fs/segment.h +0 -0
@@ -473,9 +473,16 @@ static inline void __set_test_and_free(struct
 f2fs_sb_info *sbi,
    			goto skip_free;
    		next = find_next_bit(free_i->free_segmap,
    				start_segno + SEGS_PER_SEC(sbi),

start_segno);

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
fs/f2fs/segment.h +10 -3
-		if (next >= start_segno + usable_segs) {
-			if (test_and_clear_bit(secno, free_i->free_secmap))
-				free_i->free_sections++;
+		if ((next >= start_segno + usable_segs) &&
+			test_and_clear_bit(secno, free_i->free_secmap)) {
+			free_i->free_sections++;
+
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[BG_GC]) ==
+									secno)
+				sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[FG_GC]) ==
+									secno)
+				sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
    		}
    	}

skip_free:
--
2.40.1

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Because the call stack is different, I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides. What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment

(70961) type [0, 1] in SSA and SIT [30146.346151][ T1300] Call

trace:

Quoted 53 lines
Quoted 52 lines
Quoted 52 lines
Quoted 51 lines
Quoted 51 lines
Quoted 50 lines
Quoted 50 lines
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c
[30146.346157][ T1300]  show_stack+0x18/0x28 [30146.346158][
T1300] dump_stack_lvl+0x50/0x6c [30146.346161][ T1300]
dump_stack+0x18/0x28 [30146.346162][ T1300]
f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][ T1300]
do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]
gc_thread_func+0x290/0x88c [30146.346169][ T1300]
kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961 }
Quoted 31 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.3291947
-2
-
[email protected]

Thanks,

Quoted 11 lines
---
    fs/f2fs/gc.c | 4 ++++
    1 file changed, 4 insertions(+)
fs/f2fs/gc.c index renamed from fs/f2fs/gc.c +0 -0

2b8f9239bede..4b5d18e395eb 100644

fs/f2fs/gc.c +0 -0
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi,
Quoted 1 line

struct

f2fs_gc_control *gc_control)

Quoted 10 lines
    		goto stop;
    	}
fs/f2fs/gc.c +4 -0
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
    	seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
    				gc_control->should_migrate_blocks,
    				gc_control->one_time);
maintainer
#11 lore raw
Quoted 171 lines · yohan.joung
Quoted 149 lines · yohan.joung
Quoted 55 lines
Quoted 55 lines · yohan.joung
Quoted 35 lines
Quoted 35 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 10:48 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [External Mail] Re:
[PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 23 lines · On 2025/3/27 16:00, [email protected] wrote:
Quoted 23 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 4:30 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [PATCH] f2fs:
prevent the current section from being selected as a victim during garbage collection

Quoted 11 lines · On 3/27/25 14:43, [email protected] wrote:
Quoted 11 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines · Yohan Joung
Quoted 4 lines

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in

use.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 3 lines
Quoted 1 line

This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

This is an issue that arises when dividing a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section
f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/

sec_usage_check().

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 23 lines
Quoted 16 lines

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Quoted 1 line

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

Review int f2fs_get_victim(struct f2fs_sb_info · line 850

Quoted 1 line
(quoted diff) +0 -0
@@ -850,15 +850,20 @@ int f2fs_get_victim(struct f2fs_sb_info
 *sbi,

unsigned int *result,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Review int f2fs_get_victim(struct f2fs_sb_info · line 856

Quoted 7 lines
(quoted diff) +0 -1
                           p.min_segno = sbi->next_victim_seg[BG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
                   if (gc_type == FG_GC &&
                                   sbi->next_victim_seg[FG_GC] != NULL_SEGNO)

{

Quoted 30 lines
Quoted 30 lines
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +3 -1
                           p.min_segno = sbi->next_victim_seg[FG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
+
+               secno = GET_SEC_FROM_SEG(sbi, segno);
+
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info · line 868

Quoted 5 lines
(quoted diff) +4 -0
+               if (sec_usage_check(sbi, secno))
+                       goto next;
+
+               goto got_result;
           }

But still allocator can assign this segment after sec_usage_check() in race condition, right?

Quoted 2 lines

Since the BG GC using next_victim takes place after the SIT update in do_checkpoint, it seems unlikely that a race condition with

sec_usage_check will occur.

I mean this:

- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
- f2fs_allocate_data_block
- new_curseg
- get_new_segment find segno #1

Quoted 6 lines
Quoted 3 lines

- do_garbage_collect

Thanks,

do_checkpoint sec0 free If sec0 is not freed, then

segno1 within sec0 cannot be

Quoted 5 lines

allocated
- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs (but sec0

is already used)

Quoted 3 lines

- f2fs_allocate_data_block
- new_curseg
- get_new_segment find

segno #1

Quoted 6 lines

- do_garbage_collect

I appreciate your patch, it is under testing.
but I'm wondering if there's a risk of a race condition in this situation

Oh, yes, I may missed that get_new_segment can return a free segment in partial used section.

Quoted 6 lines

So what do you think of this?
- check CURSEG() in do_garbage_collect() and get_victim()
- reset next_victim_seg[] in get_new_segment() and __set_test_and_free() during checkpoint.

Thanks,

How about using victim_secmap?
gc_thread

				mutex_lock(&DIRTY_I(sbi)->seglist_lock);
				__set_test_and_free
				check cur section next_victim clear 
				mutex_unlock(&dirty_i->seglist_lock);

mutex_lock(&dirty->seglist_lock);
f2fs_get_victim
mutex_unlock(&dirty_i->seglist_lock);

static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

                if (next >= start_segno + usable_segs) {
                        if (test_and_clear_bit(secno, free_i->free_secmap))
                                free_i->free_sections++;

fs/f2fs/segment.h static inline void __set_test_and_free(struct f2fs_sb_info *sbi, · line 482

Quoted 3 lines
fs/f2fs/segment.h +3 -0
+
+                       if (test_and_clear_bit(secno, dirty_i->victim_secmap))
+                               sbi->next_victim_seg[BG_GC] = NULL_SEGNO;

Can this happen?

segs_per_sec=2

- seg#0 and seg#1 are all dirty
- all valid blocks are removed in seg#1
- checkpoint -> seg#1 becomes free
- gc select this sec and next_victim_seg=seg#0
- migrate seg#0, next_victim_seg=seg#1
- allocator assigns seg#1 to curseg
- gc tries to migrate seg#1

Thanks,

Quoted 109 lines
                }
        }
Quoted 107 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 7 lines

IMO, we can clear next_victim_seg[] once section is free in __set_test_and_free()? something like this:

I will test it according to your suggestion.
If there are no issues, can I submit it again with the patch? Thanks

Quoted 5 lines
---
    fs/f2fs/segment.h | 13 ++++++++++---
    1 file changed, 10 insertions(+), 3 deletions(-)
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

fs/f2fs/segment.h static inline void __set_test_and_free(struct · line 476

Quoted 9 lines
fs/f2fs/segment.h index renamed from fs/f2fs/segment.h +0 -0

0465dc00b349..826e37999085 100644

fs/f2fs/segment.h +0 -0
@@ -473,9 +473,16 @@ static inline void __set_test_and_free(struct
 f2fs_sb_info *sbi,
    			goto skip_free;
    		next = find_next_bit(free_i->free_segmap,
    				start_segno + SEGS_PER_SEC(sbi),

start_segno);

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
fs/f2fs/segment.h +10 -3
-		if (next >= start_segno + usable_segs) {
-			if (test_and_clear_bit(secno, free_i->free_secmap))
-				free_i->free_sections++;
+		if ((next >= start_segno + usable_segs) &&
+			test_and_clear_bit(secno, free_i->free_secmap)) {
+			free_i->free_sections++;
+
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[BG_GC]) ==
+									secno)
+				sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[FG_GC]) ==
+									secno)
+				sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
    		}
    	}

skip_free:
--
2.40.1

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Because the call stack is different, I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides. What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment

(70961) type [0, 1] in SSA and SIT [30146.346151][ T1300] Call

trace:

Quoted 53 lines
Quoted 52 lines
Quoted 52 lines
Quoted 51 lines
Quoted 51 lines
Quoted 50 lines
Quoted 50 lines
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c
[30146.346157][ T1300]  show_stack+0x18/0x28 [30146.346158][
T1300] dump_stack_lvl+0x50/0x6c [30146.346161][ T1300]
dump_stack+0x18/0x28 [30146.346162][ T1300]
f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][ T1300]
do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]
gc_thread_func+0x290/0x88c [30146.346169][ T1300]
kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961 }
Quoted 31 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.3291947
-2
-
[email protected]

Thanks,

Quoted 11 lines
---
    fs/f2fs/gc.c | 4 ++++
    1 file changed, 4 insertions(+)
fs/f2fs/gc.c index renamed from fs/f2fs/gc.c +0 -0

2b8f9239bede..4b5d18e395eb 100644

fs/f2fs/gc.c +0 -0
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi,
Quoted 1 line

struct

f2fs_gc_control *gc_control)

Quoted 10 lines
    		goto stop;
    	}
fs/f2fs/gc.c +4 -0
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
    	seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type,
    				gc_control->should_migrate_blocks,
    				gc_control->one_time);
#12 lore raw
In reply to Chao Yu
Quoted 202 lines

From: Chao Yu <[email protected]>
Sent: Monday, March 31, 2025 8:36 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; linux-f2fs- [email protected]
Cc: [email protected]; [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [f2fs-dev] [External Mail]
Re: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 174 lines · yohan.joung
Quoted 152 lines · yohan.joung
Quoted 55 lines
Quoted 55 lines · yohan.joung
Quoted 35 lines
Quoted 35 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 10:48 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [External Mail] Re:
[PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 23 lines · On 2025/3/27 16:00, [email protected] wrote:
Quoted 23 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 4:30 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [PATCH] f2fs:
prevent the current section from being selected as a victim during garbage collection

Quoted 11 lines · On 3/27/25 14:43, [email protected] wrote:
Quoted 11 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines · Yohan Joung
Quoted 4 lines

When selecting a victim using next_victim_seg in a large section, the selected section might already have been cleared and designated as the new current section, making it actively in

use.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 3 lines
Quoted 1 line

This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

This is an issue that arises when dividing a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section
f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/

sec_usage_check().

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 23 lines
Quoted 16 lines

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Quoted 1 line

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

Review int f2fs_get_victim(struct f2fs_sb_info · line 850

Quoted 1 line
(quoted diff) +0 -0
@@ -850,15 +850,20 @@ int f2fs_get_victim(struct f2fs_sb_info
 *sbi,

unsigned int *result,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info · line 857

Quoted 8 lines
(quoted diff) +0 -1
                           p.min_segno = sbi->next_victim_seg[BG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
                   if (gc_type == FG_GC &&
                                   sbi->next_victim_seg[FG_GC]
 != NULL_SEGNO)

{

Quoted 31 lines
Quoted 31 lines
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +3 -1
                           p.min_segno = sbi->next_victim_seg[FG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
+
+               secno = GET_SEC_FROM_SEG(sbi, segno);
+
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info · line 869

Quoted 5 lines
(quoted diff) +4 -0
+               if (sec_usage_check(sbi, secno))
+                       goto next;
+
+               goto got_result;
           }

But still allocator can assign this segment after
sec_usage_check() in race condition, right?

Quoted 3 lines

Since the BG GC using next_victim takes place after the SIT update in do_checkpoint, it seems unlikely that a race condition with

sec_usage_check will occur.

I mean this:

- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
- f2fs_allocate_data_block
- new_curseg
- get_new_segment find segno #1

Quoted 6 lines
Quoted 3 lines

- do_garbage_collect

Thanks,

do_checkpoint sec0 free If sec0 is not freed, then

segno1 within sec0 cannot be

Quoted 6 lines

allocated
- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs (but
sec0

is already used)

Quoted 3 lines

- f2fs_allocate_data_block
- new_curseg
- get_new_segment find

segno #1

Quoted 6 lines

- do_garbage_collect

I appreciate your patch, it is under testing.
but I'm wondering if there's a risk of a race condition in this situation

Oh, yes, I may missed that get_new_segment can return a free segment in partial used section.

Quoted 6 lines

So what do you think of this?
- check CURSEG() in do_garbage_collect() and get_victim()
- reset next_victim_seg[] in get_new_segment() and __set_test_and_free() during checkpoint.

Thanks,

How about using victim_secmap?
gc_thread

				mutex_lock(&DIRTY_I(sbi)->seglist_lock);
				__set_test_and_free
				check cur section next_victim clear
				mutex_unlock(&dirty_i->seglist_lock);

mutex_lock(&dirty->seglist_lock);
f2fs_get_victim
mutex_unlock(&dirty_i->seglist_lock);

static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

                if (next >= start_segno + usable_segs) {
                        if (test_and_clear_bit(secno, free_i->free_secmap))
                                free_i->free_sections++;

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+                       if (test_and_clear_bit(secno, dirty_i->victim_secmap))
+                               sbi->next_victim_seg[BG_GC] =
+ NULL_SEGNO;

Can this happen?

segs_per_sec=2

- seg#0 and seg#1 are all dirty
- all valid blocks are removed in seg#1
- checkpoint -> seg#1 becomes free
- gc select this sec and next_victim_seg=seg#0
- migrate seg#0, next_victim_seg=seg#1
- allocator assigns seg#1 to curseg
- gc tries to migrate seg#1

Quoted 1 line

Thanks,

The detailed scenario
segs_per_sec=2
- seg#0 and seg#1 are all dirty
- all valid blocks are removed in seg#1
- gc select this sec and next_victim_seg=seg#0
- migrate seg#0, next_victim_seg=seg#1
- checkpoint -> sec(seg#0, seg#1) becomes free
- allocator assigns sec(seg#0, seg#1) to curseg
- gc tries to migrate seg#1

Quoted 116 lines
Quoted 28 lines
                }
        }
Quoted 25 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 7 lines

IMO, we can clear next_victim_seg[] once section is free in __set_test_and_free()? something like this:

I will test it according to your suggestion.
If there are no issues, can I submit it again with the patch? Thanks

Quoted 5 lines
---
    fs/f2fs/segment.h | 13 ++++++++++---
    1 file changed, 10 insertions(+), 3 deletions(-)
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

fs/f2fs/segment.h static inline void · line 476

Quoted 9 lines
fs/f2fs/segment.h index renamed from fs/f2fs/segment.h +0 -0

0465dc00b349..826e37999085 100644

fs/f2fs/segment.h +0 -0
@@ -473,9 +473,16 @@ static inline void
 __set_test_and_free(struct f2fs_sb_info *sbi,
    			goto skip_free;
    		next = find_next_bit(free_i->free_segmap,
    				start_segno + SEGS_PER_SEC(sbi),

start_segno);

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

fs/f2fs/segment.h static inline void · line 478

Quoted 2 lines
fs/f2fs/segment.h +0 -2
-		if (next >= start_segno + usable_segs) {
-			if (test_and_clear_bit(secno, free_i-

free_secmap))

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

Quoted 3 lines
fs/f2fs/segment.h +2 -1
-				free_i->free_sections++;
+		if ((next >= start_segno + usable_segs) &&
+			test_and_clear_bit(secno, free_i->free_secmap))

{

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

Quoted 3 lines
fs/f2fs/segment.h +3 -0
+			free_i->free_sections++;
+
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[BG_GC])

==

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

Quoted 3 lines
fs/f2fs/segment.h +3 -0
+									secno)
+				sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[FG_GC])

==

Quoted 58 lines
Quoted 58 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
fs/f2fs/segment.h +2 -0
+									secno)
+				sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
    		}
    	}

skip_free:
--
2.40.1

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Because the call stack is different, I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides. What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment

(70961) type [0, 1] in SSA and SIT [30146.346151][ T1300] Call

trace:

Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c
[30146.346157][ T1300]  show_stack+0x18/0x28 [30146.346158][
T1300] dump_stack_lvl+0x50/0x6c [30146.346161][ T1300]
dump_stack+0x18/0x28 [30146.346162][ T1300]
f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][ T1300]
do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]
gc_thread_func+0x290/0x88c [30146.346169][ T1300]
kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961 }
Quoted 22 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.32919
47
-2
-
[email protected]

Thanks,

Quoted 11 lines
---
    fs/f2fs/gc.c | 4 ++++
    1 file changed, 4 insertions(+)
fs/f2fs/gc.c index renamed from fs/f2fs/gc.c +0 -0

2b8f9239bede..4b5d18e395eb 100644

fs/f2fs/gc.c +0 -0
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi,
Quoted 1 line

struct

f2fs_gc_control *gc_control)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

fs/f2fs/gc.c

Quoted 8 lines
    		goto stop;
    	}
fs/f2fs/gc.c +4 -0
+	if (__is_large_section(sbi) &&
+			IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+		goto stop;
+
    	seg_freed = do_garbage_collect(sbi, segno, &gc_list,

gc_type,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines
    				gc_control->should_migrate_blocks,
    				gc_control->one_time);
maintainer
#13 lore raw
Quoted 194 lines · yohan.joung
Quoted 194 lines

From: Chao Yu <[email protected]>
Sent: Monday, March 31, 2025 8:36 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; linux-f2fs- [email protected]
Cc: [email protected]; [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [f2fs-dev] [External Mail]
Re: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 174 lines · yohan.joung
Quoted 152 lines · yohan.joung
Quoted 55 lines
Quoted 55 lines · yohan.joung
Quoted 35 lines
Quoted 35 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 10:48 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [External Mail] Re:
[PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 23 lines · On 2025/3/27 16:00, [email protected] wrote:
Quoted 23 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 4:30 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [PATCH] f2fs:
prevent the current section from being selected as a victim during garbage collection

Quoted 11 lines · On 3/27/25 14:43, [email protected] wrote:
Quoted 11 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines · Yohan Joung
Quoted 4 lines

When selecting a victim using next_victim_seg in a large
section, the selected section might already have been
cleared and designated as the new current section, making it
actively in

use.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 3 lines
Quoted 1 line

This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

This is an issue that arises when dividing a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section
f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/

sec_usage_check().

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 23 lines
Quoted 16 lines

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Quoted 1 line

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

Review int f2fs_get_victim(struct f2fs_sb_info · line 850

Quoted 1 line
(quoted diff) +0 -0
@@ -850,15 +850,20 @@ int f2fs_get_victim(struct f2fs_sb_info
 *sbi,

unsigned int *result,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info · line 857

Quoted 8 lines
(quoted diff) +0 -1
                           p.min_segno = sbi->next_victim_seg[BG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
                   if (gc_type == FG_GC &&
                                   sbi->next_victim_seg[FG_GC]
 != NULL_SEGNO)

{

Quoted 31 lines
Quoted 31 lines
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +3 -1
                           p.min_segno = sbi->next_victim_seg[FG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
+
+               secno = GET_SEC_FROM_SEG(sbi, segno);
+
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info · line 869

Quoted 5 lines
(quoted diff) +4 -0
+               if (sec_usage_check(sbi, secno))
+                       goto next;
+
+               goto got_result;
           }

But still allocator can assign this segment after
sec_usage_check() in race condition, right?

Quoted 3 lines

Since the BG GC using next_victim takes place after the SIT update in do_checkpoint, it seems unlikely that a race condition with

sec_usage_check will occur.

I mean this:

- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
- f2fs_allocate_data_block
- new_curseg
- get_new_segment find segno #1

Quoted 6 lines
Quoted 3 lines

- do_garbage_collect

Thanks,

do_checkpoint sec0 free If sec0 is not freed, then

segno1 within sec0 cannot be

Quoted 6 lines

allocated
- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs (but
sec0

is already used)

Quoted 3 lines

- f2fs_allocate_data_block
- new_curseg
- get_new_segment find

segno #1

Quoted 6 lines

- do_garbage_collect

I appreciate your patch, it is under testing.
but I'm wondering if there's a risk of a race condition in this situation

Oh, yes, I may missed that get_new_segment can return a free segment in partial used section.

Quoted 6 lines

So what do you think of this?
- check CURSEG() in do_garbage_collect() and get_victim()
- reset next_victim_seg[] in get_new_segment() and __set_test_and_free() during checkpoint.

Thanks,

How about using victim_secmap?
gc_thread

				mutex_lock(&DIRTY_I(sbi)->seglist_lock);
				__set_test_and_free
				check cur section next_victim clear
				mutex_unlock(&dirty_i->seglist_lock);

mutex_lock(&dirty->seglist_lock);
f2fs_get_victim
mutex_unlock(&dirty_i->seglist_lock);

static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

                if (next >= start_segno + usable_segs) {
                        if (test_and_clear_bit(secno, free_i->free_secmap))
                                free_i->free_sections++;

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+                       if (test_and_clear_bit(secno, dirty_i->victim_secmap))
+                               sbi->next_victim_seg[BG_GC] =
+ NULL_SEGNO;

Can this happen?

segs_per_sec=2

Quoted 7 lines
Quoted 7 lines

- seg#0 and seg#1 are all dirty
- all valid blocks are removed in seg#1
- checkpoint -> seg#1 becomes free
- gc select this sec and next_victim_seg=seg#0
- migrate seg#0, next_victim_seg=seg#1
- allocator assigns seg#1 to curseg
- gc tries to migrate seg#1

I meant for above case, below change still can not catch it, right? since next_victim_seg[] was assigned after checkpoint.

+ if (test_and_clear_bit(secno, dirty_i->victim_secmap))
+ sbi->next_victim_seg[BG_GC] = NULL_SEGNO;

Thanks,

Quoted 129 lines
Quoted 2 lines

Thanks,

The detailed scenario
segs_per_sec=2
- seg#0 and seg#1 are all dirty
- all valid blocks are removed in seg#1
- gc select this sec and next_victim_seg=seg#0
- migrate seg#0, next_victim_seg=seg#1
- checkpoint -> sec(seg#0, seg#1) becomes free
- allocator assigns sec(seg#0, seg#1) to curseg
- gc tries to migrate seg#1

Quoted 116 lines
Quoted 28 lines
                }
        }
Quoted 25 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 7 lines

IMO, we can clear next_victim_seg[] once section is free in __set_test_and_free()? something like this:

I will test it according to your suggestion.
If there are no issues, can I submit it again with the patch? Thanks

Quoted 5 lines
---
    fs/f2fs/segment.h | 13 ++++++++++---
    1 file changed, 10 insertions(+), 3 deletions(-)
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

fs/f2fs/segment.h static inline void · line 476

Quoted 9 lines
fs/f2fs/segment.h index renamed from fs/f2fs/segment.h +0 -0

0465dc00b349..826e37999085 100644

fs/f2fs/segment.h +0 -0
@@ -473,9 +473,16 @@ static inline void
 __set_test_and_free(struct f2fs_sb_info *sbi,
    			goto skip_free;
    		next = find_next_bit(free_i->free_segmap,
    				start_segno + SEGS_PER_SEC(sbi),

start_segno);

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

fs/f2fs/segment.h static inline void · line 478

Quoted 2 lines
fs/f2fs/segment.h +0 -2
-		if (next >= start_segno + usable_segs) {
-			if (test_and_clear_bit(secno, free_i-

free_secmap))

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

Quoted 3 lines
fs/f2fs/segment.h +2 -1
-				free_i->free_sections++;
+		if ((next >= start_segno + usable_segs) &&
+			test_and_clear_bit(secno, free_i->free_secmap))

{

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

Quoted 3 lines
fs/f2fs/segment.h +3 -0
+			free_i->free_sections++;
+
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[BG_GC])

==

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

Quoted 3 lines
fs/f2fs/segment.h +3 -0
+									secno)
+				sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[FG_GC])

==

Quoted 58 lines
Quoted 58 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
fs/f2fs/segment.h +2 -0
+									secno)
+				sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
    		}
    	}

skip_free:
--
2.40.1

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Because the call stack is different, I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides. What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment

(70961) type [0, 1] in SSA and SIT [30146.346151][ T1300] Call

trace:

Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c
[30146.346157][ T1300]  show_stack+0x18/0x28 [30146.346158][
T1300] dump_stack_lvl+0x50/0x6c [30146.346161][ T1300]
dump_stack+0x18/0x28 [30146.346162][ T1300]
f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][ T1300]
do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]
gc_thread_func+0x290/0x88c [30146.346169][ T1300]
kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961 }
Quoted 22 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.32919
47
-2
-
[email protected]

Thanks,

Quoted 12 lines

Signed-off-by: Yohan Joung <[email protected]>
---
fs/f2fs/gc.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c index
2b8f9239bede..4b5d18e395eb 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi,
struct

f2fs_gc_control *gc_control)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

goto stop;
}

+ if (__is_large_section(sbi) &&
+ IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+ goto stop;
+
seg_freed = do_garbage_collect(sbi, segno, &gc_list,

gc_type,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines

gc_control->should_migrate_blocks,
gc_control->one_time);

#14 lore raw
Quoted 229 lines

-----Original Message-----
From: Chao Yu <[email protected]>
Sent: Tuesday, April 1, 2025 2:42 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; linux-f2fs- [email protected]
Cc: [email protected]; [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [f2fs-dev] [External Mail]
Re: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 15 lines · yohan.joung
Quoted 15 lines

From: Chao Yu <[email protected]>
Sent: Monday, March 31, 2025 8:36 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; linux-f2fs- [email protected]
Cc: [email protected]; [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [f2fs-dev] [External Mail]
Re: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 2 lines · yohan.joung
Quoted 2 lines · yohan.joung
Quoted 1 line
Quoted 1 line · yohan.joung
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 10:48 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [External Mail]

Re:

Quoted 163 lines
Quoted 163 lines
Quoted 163 lines
Quoted 145 lines
Quoted 48 lines
Quoted 48 lines
Quoted 29 lines
Quoted 29 lines

[PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 25 lines · On 2025/3/27 16:00, [email protected] wrote:
Quoted 25 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 4:30 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [PATCH] f2fs:
prevent the current section from being selected as a victim during garbage collection

Quoted 12 lines · On 3/27/25 14:43, [email protected] wrote:
Quoted 12 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected];
[email protected]
Cc: [email protected];
[email protected];
linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE
<[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the
current section from being selected as a victim during
garbage collection

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines · Yohan Joung

When selecting a victim using next_victim_seg in a large
section, the selected section might already have been
cleared and designated as the new current section, making
it actively in

use.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 3 lines

This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

This is an issue that arises when dividing a large section into segments for garbage collection.
caused by the background GC (garbage collection) thread in large section
f2fs_gc(victim_section) -> f2fs_clear_prefree_segments(victim_section)-> cursec(victim_section) -> f2fs_gc(victim_section by next_victim_seg)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/

sec_usage_check().

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 23 lines
Quoted 16 lines

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Quoted 1 line

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

Review int f2fs_get_victim(struct f2fs_sb_info · line 850

Quoted 1 line
(quoted diff) +0 -0
@@ -850,15 +850,20 @@ int f2fs_get_victim(struct f2fs_sb_info
 *sbi,

unsigned int *result,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info · line 857

Quoted 8 lines
(quoted diff) +0 -1
                           p.min_segno = sbi->next_victim_seg[BG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
                   if (gc_type == FG_GC &&
                                   sbi->next_victim_seg[FG_GC]
 != NULL_SEGNO)

{

Quoted 31 lines
Quoted 31 lines
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +3 -1
                           p.min_segno = sbi->next_victim_seg[FG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
+
+               secno = GET_SEC_FROM_SEG(sbi, segno);
+
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info · line 869

Quoted 5 lines
(quoted diff) +4 -0
+               if (sec_usage_check(sbi, secno))
+                       goto next;
+
+               goto got_result;
           }

But still allocator can assign this segment after sec_usage_check() in race condition, right?

Quoted 3 lines

Since the BG GC using next_victim takes place after the SIT update in do_checkpoint, it seems unlikely that a race condition with

sec_usage_check will occur.

I mean this:

- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
- f2fs_allocate_data_block
- new_curseg
- get_new_segment find segno #1

Quoted 6 lines
Quoted 3 lines

- do_garbage_collect

Thanks,

do_checkpoint sec0 free If sec0 is not freed, then

segno1 within sec0 cannot be

Quoted 7 lines

allocated
- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
(but
sec0

is already used)

Quoted 3 lines

- f2fs_allocate_data_block
- new_curseg
- get_new_segment find

segno #1

Quoted 6 lines

- do_garbage_collect

I appreciate your patch, it is under testing.
but I'm wondering if there's a risk of a race condition in this situation

Oh, yes, I may missed that get_new_segment can return a free segment in partial used section.

Quoted 6 lines

So what do you think of this?
- check CURSEG() in do_garbage_collect() and get_victim()
- reset next_victim_seg[] in get_new_segment() and __set_test_and_free() during checkpoint.

Thanks,

How about using victim_secmap?
gc_thread

				mutex_lock(&DIRTY_I(sbi)->seglist_lock);
				__set_test_and_free
				check cur section next_victim clear
				mutex_unlock(&dirty_i->seglist_lock);

mutex_lock(&dirty->seglist_lock);
f2fs_get_victim
mutex_unlock(&dirty_i->seglist_lock);

Quoted 21 lines
Quoted 6 lines

Review

Quoted 6 lines

static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

                if (next >= start_segno + usable_segs) {
                        if (test_and_clear_bit(secno, free_i->free_secmap))
                                free_i->free_sections++;
(quoted diff) +2 -0
+
+                       if (test_and_clear_bit(secno, dirty_i-

victim_secmap))

Quoted 14 lines
Quoted 2 lines

+ sbi->next_victim_seg[BG_GC] =
+ NULL_SEGNO;

Can this happen?

segs_per_sec=2

- seg#0 and seg#1 are all dirty
- all valid blocks are removed in seg#1
- checkpoint -> seg#1 becomes free
- gc select this sec and next_victim_seg=seg#0
- migrate seg#0, next_victim_seg=seg#1
- allocator assigns seg#1 to curseg
- gc tries to migrate seg#1

I meant for above case, below change still can not catch it, right? since next_victim_seg[] was assigned after checkpoint.

(quoted diff) +2 -0
+	if (test_and_clear_bit(secno, dirty_i->victim_secmap))
+		sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
Quoted 2 lines

Thanks,

I understood what you said.
It seems that without a checkpoint,
we won't be able to allocate seg#1 in sec0
because when requesting a segment allocation,
if it's not within the same section, it checks in a new secmap. please let me know, if there's anything I've missed Thanks

static int get_new_segment(struct f2fs_sb_info *sbi,

			unsigned int *newseg, bool new_sec, bool pinning)

	if (!new_sec && ((*newseg + 1) % SEGS_PER_SEC(sbi))) {
		segno = find_next_zero_bit(free_i->free_segmap,
			GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
		if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
			goto got_it;
	}

find_other_zone:

	secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
Quoted 132 lines
Quoted 114 lines
Quoted 2 lines

Thanks,

The detailed scenario
segs_per_sec=2
- seg#0 and seg#1 are all dirty
- all valid blocks are removed in seg#1
- gc select this sec and next_victim_seg=seg#0
- migrate seg#0, next_victim_seg=seg#1
- checkpoint -> sec(seg#0, seg#1) becomes free
- allocator assigns sec(seg#0, seg#1) to curseg
- gc tries to migrate seg#1

Quoted 103 lines
Quoted 28 lines
                }
        }
Quoted 25 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 7 lines

IMO, we can clear next_victim_seg[] once section is free in __set_test_and_free()? something like this:

I will test it according to your suggestion.
If there are no issues, can I submit it again with the patch? Thanks

Quoted 5 lines
---
    fs/f2fs/segment.h | 13 ++++++++++---
    1 file changed, 10 insertions(+), 3 deletions(-)
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

fs/f2fs/segment.h static inline void · line 476

Quoted 9 lines
fs/f2fs/segment.h index renamed from fs/f2fs/segment.h +0 -0

0465dc00b349..826e37999085 100644

fs/f2fs/segment.h +0 -0
@@ -473,9 +473,16 @@ static inline void
 __set_test_and_free(struct f2fs_sb_info *sbi,
    			goto skip_free;
    		next = find_next_bit(free_i->free_segmap,
    				start_segno + SEGS_PER_SEC(sbi),

start_segno);

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

fs/f2fs/segment.h static inline void · line 478

Quoted 2 lines
fs/f2fs/segment.h +0 -2
-		if (next >= start_segno + usable_segs) {
-			if (test_and_clear_bit(secno, free_i-

free_secmap))

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void · line 478

Quoted 3 lines
fs/f2fs/segment.h +2 -1
-				free_i->free_sections++;
+		if ((next >= start_segno + usable_segs) &&
+			test_and_clear_bit(secno, free_i->free_secmap))

{

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void · line 481

Quoted 3 lines
fs/f2fs/segment.h +3 -0
+			free_i->free_sections++;
+
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[BG_GC])

==

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void · line 484

Quoted 3 lines
fs/f2fs/segment.h +3 -0
+									secno)
+				sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[FG_GC])

==

Quoted 60 lines
Quoted 60 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
fs/f2fs/segment.h +2 -0
+									secno)
+				sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
    		}
    	}

skip_free:
--
2.40.1

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Because the call stack is different, I think that in order to handle everything at once, we need to address it within do_garbage_collect, or otherwise include it on both sides. What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment

(70961) type [0, 1] in SSA and SIT [30146.346151][ T1300] Call

trace:

Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
[30146.346152][ T1300]  dump_backtrace+0xe8/0x10c
[30146.346157][ T1300]  show_stack+0x18/0x28 [30146.346158][
T1300] dump_stack_lvl+0x50/0x6c [30146.346161][ T1300]
dump_stack+0x18/0x28 [30146.346162][ T1300]
f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][ T1300]
do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]
gc_thread_func+0x290/0x88c [30146.346169][ T1300]
kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20
struct curseg_info : 0xffffff803f95e800 {
	segno        : 0x11531 : 70961
}
struct f2fs_sb_info : 0xffffff8811d12000 {
	next_victim_seg[0] : 0x11531 : 70961 }
Quoted 23 lines

https://lore.kernel.org/linux-f2fs-devel/20250325080646.329
19
47
-2
-
[email protected]

Thanks,

Signed-off-by: Yohan Joung <[email protected]>
---
fs/f2fs/gc.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c index
2b8f9239bede..4b5d18e395eb 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info
*sbi, struct
f2fs_gc_control *gc_control)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

goto stop;
}

+ if (__is_large_section(sbi) &&
+ IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi,

segno)))

Quoted 12 lines
Quoted 10 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+ goto stop;
+
seg_freed = do_garbage_collect(sbi, segno, &gc_list,

gc_type,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

gc_control->should_migrate_blocks,
gc_control->one_time);

maintainer
#15 lore raw
Quoted 229 lines · yohan.joung
Quoted 229 lines

-----Original Message-----
From: Chao Yu <[email protected]>
Sent: Tuesday, April 1, 2025 2:42 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; linux-f2fs- [email protected]
Cc: [email protected]; [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [f2fs-dev] [External Mail]
Re: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 15 lines · yohan.joung
Quoted 15 lines

From: Chao Yu <[email protected]>
Sent: Monday, March 31, 2025 8:36 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; linux-f2fs- [email protected]
Cc: [email protected]; [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [f2fs-dev] [External Mail]
Re: [External Mail] Re: [PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 2 lines · yohan.joung
Quoted 2 lines · yohan.joung
Quoted 1 line
Quoted 1 line · yohan.joung
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 10:48 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [External Mail]

Re:

Quoted 163 lines
Quoted 163 lines
Quoted 163 lines
Quoted 145 lines
Quoted 48 lines
Quoted 48 lines
Quoted 29 lines
Quoted 29 lines

[PATCH] f2fs: prevent the current section from being selected as a victim during garbage collection

Quoted 25 lines · On 2025/3/27 16:00, [email protected] wrote:
Quoted 25 lines

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 4:30 PM
To: 정요한(JOUNG YOHAN) Mobile AE <[email protected]>; Yohan Joung <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; 김필현(KIM PILHYUN) Mobile AE <[email protected]>
Subject: [External Mail] Re: [External Mail] Re: [PATCH] f2fs:
prevent the current section from being selected as a victim during garbage collection

Quoted 12 lines · On 3/27/25 14:43, [email protected] wrote:

From: Chao Yu <[email protected]>
Sent: Thursday, March 27, 2025 3:02 PM
To: Yohan Joung <[email protected]>; [email protected];
[email protected]
Cc: [email protected];
[email protected];
linux- [email protected]; 정요한(JOUNG YOHAN) Mobile AE
<[email protected]>
Subject: [External Mail] Re: [PATCH] f2fs: prevent the
current section from being selected as a victim during
garbage collection

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

On 3/26/25 22:14, Yohan Joung wrote:
When selecting a victim using next_victim_seg in a large
section, the selected section might already have been
cleared and designated as the new current section, making
it actively in

use.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

This is an issue that arises when dividing a large section
into segments for garbage collection.
caused by the background GC (garbage collection) thread in
large section
f2fs_gc(victim_section) ->
f2fs_clear_prefree_segments(victim_section)->
cursec(victim_section) -> f2fs_gc(victim_section by
next_victim_seg)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I didn't get it, why f2fs_get_victim() will return section which is used by curseg? It should be avoided by checking w/

sec_usage_check().

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 23 lines
Quoted 16 lines

Or we missed to check gcing section which next_victim_seg points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Quoted 1 line

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

Review int f2fs_get_victim(struct f2fs_sb_info · line 850

Quoted 1 line
(quoted diff) +0 -0
@@ -850,15 +850,20 @@ int f2fs_get_victim(struct f2fs_sb_info
 *sbi,

unsigned int *result,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info · line 857

Quoted 8 lines
(quoted diff) +0 -1
                           p.min_segno = sbi->next_victim_seg[BG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
                   if (gc_type == FG_GC &&
                                   sbi->next_victim_seg[FG_GC]
 != NULL_SEGNO)

{

Quoted 31 lines
Quoted 31 lines
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +3 -1
                           p.min_segno = sbi->next_victim_seg[FG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
+
+               secno = GET_SEC_FROM_SEG(sbi, segno);
+
Quoted 8 lines

Review int f2fs_get_victim(struct f2fs_sb_info · line 869

Quoted 5 lines
(quoted diff) +4 -0
+               if (sec_usage_check(sbi, secno))
+                       goto next;
+
+               goto got_result;
           }

But still allocator can assign this segment after sec_usage_check() in race condition, right?

Quoted 3 lines

Since the BG GC using next_victim takes place after the SIT update in do_checkpoint, it seems unlikely that a race condition with

sec_usage_check will occur.

I mean this:

- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
- f2fs_allocate_data_block
- new_curseg
- get_new_segment find segno #1

Quoted 6 lines
Quoted 3 lines

- do_garbage_collect

Thanks,

do_checkpoint sec0 free If sec0 is not freed, then

segno1 within sec0 cannot be

Quoted 7 lines

allocated
- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
(but
sec0

is already used)

Quoted 3 lines

- f2fs_allocate_data_block
- new_curseg
- get_new_segment find

segno #1

Quoted 6 lines

- do_garbage_collect

I appreciate your patch, it is under testing.
but I'm wondering if there's a risk of a race condition in this situation

Oh, yes, I may missed that get_new_segment can return a free segment in partial used section.

Quoted 6 lines

So what do you think of this?
- check CURSEG() in do_garbage_collect() and get_victim()
- reset next_victim_seg[] in get_new_segment() and __set_test_and_free() during checkpoint.

Thanks,

How about using victim_secmap?
gc_thread

				mutex_lock(&DIRTY_I(sbi)->seglist_lock);
				__set_test_and_free
				check cur section next_victim clear
				mutex_unlock(&dirty_i->seglist_lock);

mutex_lock(&dirty->seglist_lock);
f2fs_get_victim
mutex_unlock(&dirty_i->seglist_lock);

Quoted 21 lines
Quoted 6 lines

Review

Quoted 6 lines

static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

                if (next >= start_segno + usable_segs) {
                        if (test_and_clear_bit(secno, free_i->free_secmap))
                                free_i->free_sections++;
(quoted diff) +2 -0
+
+                       if (test_and_clear_bit(secno, dirty_i-

victim_secmap))

Quoted 14 lines
Quoted 2 lines

+ sbi->next_victim_seg[BG_GC] =
+ NULL_SEGNO;

Can this happen?

segs_per_sec=2

- seg#0 and seg#1 are all dirty
- all valid blocks are removed in seg#1
- checkpoint -> seg#1 becomes free
- gc select this sec and next_victim_seg=seg#0
- migrate seg#0, next_victim_seg=seg#1
- allocator assigns seg#1 to curseg
- gc tries to migrate seg#1

I meant for above case, below change still can not catch it, right? since next_victim_seg[] was assigned after checkpoint.

(quoted diff) +2 -0
+	if (test_and_clear_bit(secno, dirty_i->victim_secmap))
+		sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
Quoted 8 lines
Quoted 2 lines

Thanks,

I understood what you said.
It seems that without a checkpoint,
we won't be able to allocate seg#1 in sec0
because when requesting a segment allocation,
if it's not within the same section, it checks in a new secmap. please let me know, if there's anything I've missed

Oh, I see, it always try to allocate segments sequentially in a large section-size image, thanks for your explanation. :)

static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type) {
...

	if (__is_large_section(sbi)) {
...
		return curseg->segno;

...
}

static int new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec) {
...

	segno = __get_next_segno(sbi, type);
	ret = get_new_segment(sbi, &segno, new_sec, pinning);

...
}

BTW, I guess it could be more efficient if we can reuse free segment in dirty section for conventional device.

Quoted 12 lines

Thanks

static int get_new_segment(struct f2fs_sb_info *sbi,

			unsigned int *newseg, bool new_sec, bool pinning)

	if (!new_sec && ((*newseg + 1) % SEGS_PER_SEC(sbi))) {
		segno = find_next_zero_bit(free_i->free_segmap,
			GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
		if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
			goto got_it;
	}
Quoted 12 lines

find_other_zone:

	secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
Quoted 9 lines
Quoted 9 lines
Quoted 2 lines

Thanks,

The detailed scenario
segs_per_sec=2
- seg#0 and seg#1 are all dirty
- all valid blocks are removed in seg#1
- gc select this sec and next_victim_seg=seg#0
- migrate seg#0, next_victim_seg=seg#1
- checkpoint -> sec(seg#0, seg#1) becomes free

So, for this case, we can handle it only in checkpoint.

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (test_and_clear_bit(secno, dirty_i->victim_secmap))
+		sbi->next_victim_seg[BG_GC] = NULL_SEGNO;

Do we need to handle sbi->next_victim_seg[FG_GC] as well?

Thanks,

Quoted 125 lines
Quoted 123 lines
Quoted 105 lines

- allocator assigns sec(seg#0, seg#1) to curseg
- gc tries to migrate seg#1

Quoted 103 lines
Quoted 28 lines
                }
        }
Quoted 25 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 7 lines

IMO, we can clear next_victim_seg[] once section is free in __set_test_and_free()? something like this:

I will test it according to your suggestion.
If there are no issues, can I submit it again with the patch? Thanks

Quoted 5 lines
---
    fs/f2fs/segment.h | 13 ++++++++++---
    1 file changed, 10 insertions(+), 3 deletions(-)
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

fs/f2fs/segment.h static inline void · line 476

Quoted 9 lines
fs/f2fs/segment.h index renamed from fs/f2fs/segment.h +0 -0

0465dc00b349..826e37999085 100644

fs/f2fs/segment.h +0 -0
@@ -473,9 +473,16 @@ static inline void
 __set_test_and_free(struct f2fs_sb_info *sbi,
    			goto skip_free;
    		next = find_next_bit(free_i->free_segmap,
    				start_segno + SEGS_PER_SEC(sbi),

start_segno);

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

fs/f2fs/segment.h static inline void · line 478

Quoted 2 lines
fs/f2fs/segment.h +0 -2
-		if (next >= start_segno + usable_segs) {
-			if (test_and_clear_bit(secno, free_i-

free_secmap))

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void · line 478

Quoted 3 lines
fs/f2fs/segment.h +2 -1
-				free_i->free_sections++;
+		if ((next >= start_segno + usable_segs) &&
+			test_and_clear_bit(secno, free_i->free_secmap))

{

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void · line 481

Quoted 3 lines
fs/f2fs/segment.h +3 -0
+			free_i->free_sections++;
+
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[BG_GC])

==

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void · line 484

Quoted 3 lines
fs/f2fs/segment.h +3 -0
+									secno)
+				sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[FG_GC])

==

Quoted 60 lines
Quoted 60 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
fs/f2fs/segment.h +2 -0
+									secno)
+				sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
    		}
    	}

skip_free:
--
2.40.1

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Because the call stack is different, I think that in order
to handle everything at once, we need to address it within
do_garbage_collect, or otherwise include it on both sides.
What do you think?

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent segment
(70961) type [0, 1] in SSA and SIT [30146.346151][ T1300]
Call

trace:

Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines

[30146.346152][ T1300] dump_backtrace+0xe8/0x10c
[30146.346157][ T1300] show_stack+0x18/0x28 [30146.346158][
T1300] dump_stack_lvl+0x50/0x6c [30146.346161][ T1300]
dump_stack+0x18/0x28 [30146.346162][ T1300]
f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][ T1300]
do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]
gc_thread_func+0x290/0x88c [30146.346169][ T1300]
kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20

struct curseg_info : 0xffffff803f95e800 {
segno : 0x11531 : 70961
}

struct f2fs_sb_info : 0xffffff8811d12000 {
next_victim_seg[0] : 0x11531 : 70961 }

https://lore.kernel.org/linux-f2fs-devel/20250325080646.329
19
47
-2
-
[email protected]

Thanks,

Signed-off-by: Yohan Joung <[email protected]>
---
fs/f2fs/gc.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c index
2b8f9239bede..4b5d18e395eb 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info
*sbi, struct
f2fs_gc_control *gc_control)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

goto stop;
}

+ if (__is_large_section(sbi) &&
+ IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi,

segno)))

Quoted 12 lines
Quoted 10 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+ goto stop;
+
seg_freed = do_garbage_collect(sbi, segno, &gc_list,

gc_type,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

gc_control->should_migrate_blocks,
gc_control->one_time);

#16 lore raw
Quoted 184 lines
Quoted 102 lines
Quoted 102 lines
Quoted 102 lines
Quoted 102 lines
Quoted 102 lines
Quoted 102 lines
Quoted 19 lines
Quoted 19 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

When selecting a victim using next_victim_seg in a large
section, the selected section might already have been
cleared and designated as the new current section,
making it actively in

use.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

This behavior causes inconsistency between the SIT and SSA.

Hi, does this fix your issue?

This is an issue that arises when dividing a large section
into segments for garbage collection.
caused by the background GC (garbage collection) thread in
large section
f2fs_gc(victim_section) ->
f2fs_clear_prefree_segments(victim_section)->
cursec(victim_section) -> f2fs_gc(victim_section by
next_victim_seg)

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I didn't get it, why f2fs_get_victim() will return section
which is used by curseg? It should be avoided by checking
w/

sec_usage_check().

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 23 lines
Quoted 17 lines

Or we missed to check gcing section which next_victim_seg
points to during get_new_segment()?

Can this happen?

e.g.
- bggc selects sec #0
- next_victim_seg: seg #0
- migrate seg #0 and stop
- next_victim_seg: seg #1
- checkpoint, set sec #0 free if sec #0 has no valid blocks
- allocate seg #0 in sec #0 for curseg
- curseg moves to seg #1 after allocation
- bggc tries to migrate seg #1

Thanks,

That's correct
In f2fs_get_victim, we use next_victim_seg to directly jump to got_result, thereby bypassing sec_usage_check What do you think about this change?

Review int f2fs_get_victim(struct · line 850

Quoted 1 line
(quoted diff) +0 -0
@@ -850,15 +850,20 @@ int f2fs_get_victim(struct
 f2fs_sb_info *sbi,

unsigned int *result,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review int f2fs_get_victim(struct · line 857

Quoted 8 lines
(quoted diff) +0 -1
                           p.min_segno = sbi->next_victim_seg[BG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
                   if (gc_type == FG_GC &&
 
 sbi->next_victim_seg[FG_GC] != NULL_SEGNO)

{

Quoted 31 lines
Quoted 31 lines
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +3 -1
                           p.min_segno = sbi->next_victim_seg[FG_GC];
                           *result = p.min_segno;
                           sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
-                       goto got_result;
                   }
+
+               secno = GET_SEC_FROM_SEG(sbi, segno);
+
Quoted 8 lines

Review int f2fs_get_victim(struct · line 869

Quoted 5 lines
(quoted diff) +4 -0
+               if (sec_usage_check(sbi, secno))
+                       goto next;
+
+               goto got_result;
           }

But still allocator can assign this segment after
sec_usage_check() in race condition, right?

Quoted 3 lines

Since the BG GC using next_victim takes place after the SIT update in do_checkpoint, it seems unlikely that a race condition with

sec_usage_check will occur.

I mean this:

- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
- f2fs_allocate_data_block
- new_curseg
- get_new_segment find segno #1

Quoted 6 lines
Quoted 3 lines

- do_garbage_collect

Thanks,

do_checkpoint sec0 free If sec0 is not freed, then

segno1 within sec0 cannot be

Quoted 7 lines

allocated
- gc_thread
- f2fs_gc
- f2fs_get_victim
- sec_usage_check --- segno #1 is not used in any cursegs
(but
sec0

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

is already used)

Quoted 1 line
							-

f2fs_allocate_data_block

Quoted 59 lines
Quoted 59 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 13 lines
Quoted 2 lines

- new_curseg
- get_new_segment find

segno #1

Quoted 6 lines

- do_garbage_collect

I appreciate your patch, it is under testing.
but I'm wondering if there's a risk of a race condition in this situation

Oh, yes, I may missed that get_new_segment can return a free segment in partial used section.

Quoted 6 lines

So what do you think of this?
- check CURSEG() in do_garbage_collect() and get_victim()
- reset next_victim_seg[] in get_new_segment() and __set_test_and_free() during checkpoint.

Thanks,

How about using victim_secmap?
gc_thread

				mutex_lock(&DIRTY_I(sbi)->seglist_lock);
				__set_test_and_free
				check cur section next_victim clear
				mutex_unlock(&dirty_i->seglist_lock);

mutex_lock(&dirty->seglist_lock);
f2fs_get_victim
mutex_unlock(&dirty_i->seglist_lock);

Quoted 21 lines
Quoted 6 lines

Review

Quoted 6 lines

static inline void __set_test_and_free(struct f2fs_sb_info *sbi,

                if (next >= start_segno + usable_segs) {
                        if (test_and_clear_bit(secno, free_i->free_secmap))
                                free_i->free_sections++;
(quoted diff) +2 -0
+
+                       if (test_and_clear_bit(secno, dirty_i-

victim_secmap))

Quoted 14 lines
Quoted 2 lines

+ sbi->next_victim_seg[BG_GC] =
+ NULL_SEGNO;

Can this happen?

segs_per_sec=2

- seg#0 and seg#1 are all dirty
- all valid blocks are removed in seg#1
- checkpoint -> seg#1 becomes free
- gc select this sec and next_victim_seg=seg#0
- migrate seg#0, next_victim_seg=seg#1
- allocator assigns seg#1 to curseg
- gc tries to migrate seg#1

I meant for above case, below change still can not catch it, right? since next_victim_seg[] was assigned after checkpoint.

(quoted diff) +2 -0
+	if (test_and_clear_bit(secno, dirty_i->victim_secmap))
+		sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
Quoted 8 lines
Quoted 2 lines

Thanks,

I understood what you said.
It seems that without a checkpoint,
we won't be able to allocate seg#1 in sec0 because when requesting a segment allocation, if it's not within the same section, it checks in a new secmap.
please let me know, if there's anything I've missed

Oh, I see, it always try to allocate segments sequentially in a large section-size image, thanks for your explanation. :)

static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type) { ...

	if (__is_large_section(sbi)) {
...
		return curseg->segno;

...
}

Quoted 8 lines

static int new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec) { ...

	segno = __get_next_segno(sbi, type);
	ret = get_new_segment(sbi, &segno, new_sec, pinning); ...
}

BTW, I guess it could be more efficient if we can reuse free segment in dirty section for conventional device.a

It might cause file fragmentation, so we should consider it

Quoted 27 lines
Quoted 13 lines

Thanks

static int get_new_segment(struct f2fs_sb_info *sbi,

			unsigned int *newseg, bool new_sec, bool pinning)

	if (!new_sec && ((*newseg + 1) % SEGS_PER_SEC(sbi))) {
		segno = find_next_zero_bit(free_i->free_segmap,
			GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
		if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
			goto got_it;
	}
Quoted 13 lines

find_other_zone:

	secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi),

hint);

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines

Thanks,

The detailed scenario
segs_per_sec=2
- seg#0 and seg#1 are all dirty
- all valid blocks are removed in seg#1
- gc select this sec and next_victim_seg=seg#0
- migrate seg#0, next_victim_seg=seg#1
- checkpoint -> sec(seg#0, seg#1) becomes free

Quoted 6 lines

So, for this case, we can handle it only in checkpoint.

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (test_and_clear_bit(secno, dirty_i->victim_secmap))
+		sbi->next_victim_seg[BG_GC] = NULL_SEGNO;

Do we need to handle sbi->next_victim_seg[FG_GC] as well?

I'll add it and submit the patch.
Thanks.

Quoted 132 lines

Thanks,

Quoted 129 lines
Quoted 127 lines
Quoted 108 lines

- allocator assigns sec(seg#0, seg#1) to curseg
- gc tries to migrate seg#1

Quoted 106 lines
Quoted 28 lines
                }
        }
Quoted 25 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 7 lines

IMO, we can clear next_victim_seg[] once section is free in __set_test_and_free()? something like this:

I will test it according to your suggestion.
If there are no issues, can I submit it again with the patch? Thanks

Quoted 5 lines
---
    fs/f2fs/segment.h | 13 ++++++++++---
    1 file changed, 10 insertions(+), 3 deletions(-)
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

fs/f2fs/segment.h static inline void · line 476

Quoted 9 lines
fs/f2fs/segment.h index renamed from fs/f2fs/segment.h +0 -0

0465dc00b349..826e37999085 100644

fs/f2fs/segment.h +0 -0
@@ -473,9 +473,16 @@ static inline void
 __set_test_and_free(struct f2fs_sb_info *sbi,
    			goto skip_free;
    		next = find_next_bit(free_i->free_segmap,
    				start_segno + SEGS_PER_SEC(sbi),

start_segno);

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

fs/f2fs/segment.h static inline void · line 478

Quoted 2 lines
fs/f2fs/segment.h +0 -2
-		if (next >= start_segno + usable_segs) {
-			if (test_and_clear_bit(secno, free_i-

free_secmap))

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void · line 478

Quoted 3 lines
fs/f2fs/segment.h +2 -1
-				free_i->free_sections++;
+		if ((next >= start_segno + usable_segs) &&
+			test_and_clear_bit(secno, free_i->free_secmap))

{

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void · line 481

Quoted 3 lines
fs/f2fs/segment.h +3 -0
+			free_i->free_sections++;
+
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[BG_GC])

==

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fs/f2fs/segment.h static inline void · line 484

Quoted 3 lines
fs/f2fs/segment.h +3 -0
+									secno)
+				sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
+			if (GET_SEC_FROM_SEG(sbi->next_victim_seg[FG_GC])

==

Quoted 63 lines
Quoted 63 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
fs/f2fs/segment.h +2 -0
+									secno)
+				sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
    		}
    	}

skip_free:
--
2.40.1

Quoted 7 lines
Quoted 7 lines

Because the call stack is different, I think that in order
to handle everything at once, we need to address it within
do_garbage_collect, or otherwise include it on both sides.
What do you think?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

[30146.337471][ T1300] F2FS-fs (dm-54): Inconsistent
segment
(70961) type [0, 1] in SSA and SIT [30146.346151][ T1300]
Call

trace:

Quoted 43 lines
Quoted 43 lines
Quoted 43 lines
Quoted 43 lines
Quoted 43 lines
Quoted 43 lines

[30146.346152][ T1300] dump_backtrace+0xe8/0x10c
[30146.346157][ T1300] show_stack+0x18/0x28
[30146.346158][ T1300] dump_stack_lvl+0x50/0x6c
[30146.346161][ T1300]
dump_stack+0x18/0x28 [30146.346162][ T1300]
f2fs_stop_checkpoint+0x1c/0x3c [30146.346165][ T1300]
do_garbage_collect+0x41c/0x271c [30146.346167][ T1300]
f2fs_gc+0x27c/0x828 [30146.346168][ T1300]
gc_thread_func+0x290/0x88c [30146.346169][ T1300]
kthread+0x11c/0x164 [30146.346172][ T1300]
ret_from_fork+0x10/0x20

struct curseg_info : 0xffffff803f95e800 {
segno : 0x11531 : 70961
}

struct f2fs_sb_info : 0xffffff8811d12000 {
next_victim_seg[0] : 0x11531 : 70961 }

https://lore.kernel.org/linux-f2fs-devel/20250325080646.3
29
19
47
-2
-
[email protected]

Thanks,

Signed-off-by: Yohan Joung <[email protected]>
---
fs/f2fs/gc.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c index
2b8f9239bede..4b5d18e395eb 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -1926,6 +1926,10 @@ int f2fs_gc(struct f2fs_sb_info
*sbi, struct
f2fs_gc_control *gc_control)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

goto stop;
}

+ if (__is_large_section(sbi) &&
+ IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi,

segno)))

Quoted 13 lines
Quoted 11 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

+ goto stop;
+
seg_freed = do_garbage_collect(sbi, segno,
&gc_list,

gc_type,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

gc_control->should_migrate_blocks,
gc_control->one_time);