Skip to contentSkip to conversation

f2fs:Fix incomplete search range in f2fs_get_victim when f2fs_need_rand_seg is enabled

PATCH Superseded liujinbao1 2 participants 3 messages Jan 7 – Feb 13 F2FS file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

liujinbao1

author
#0 lore raw

PATCH 1/1 f2fs:Fix incomplete search range in f2fs_get_victim when f2fs_need_rand_seg is enabled

From: liujinbao1 <[email protected]>

During the f2fs_get_victim process, when the f2fs_need_rand_seg is enabled in select_policy, p->offset is a random value, and the search range is from p->offset to MAIN_SECS. When segno >= last_segment, the loop breaks and exits directly without searching the range from 0 to p->offset.This results in an incomplete search when the random offset is not zero.

---
 fs/f2fs/gc.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/f2fs/gc.c +6 -4
index 384fa7e2085b..100b8fc3e65a 100644
@@ -781,6 +781,7 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
 	unsigned int valid_thresh_ratio = 100;
 	bool is_atgc;
 	int ret = 0;
+	unsigned int original_offset;
 
 	mutex_lock(&dirty_i->seglist_lock);
 	last_segment = MAIN_SECS(sbi) * SEGS_PER_SEC(sbi);
@@ -799,6 +800,7 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
 	p.min_segno = NULL_SEGNO;
 	p.oldest_age = 0;
 	p.min_cost = get_max_cost(sbi, &p);
+	original_offset = p.offset;
 
 	is_atgc = (p.gc_mode == GC_AT || p.alloc_mode == AT_SSR);
 	nsearched = 0;
@@ -859,11 +861,11 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
 				p.offset / p.ofs_unit);
 		segno = unit_no * p.ofs_unit;
 		if (segno >= last_segment) {
-			if (sm->last_victim[p.gc_mode]) {
-				last_segment =
-					sm->last_victim[p.gc_mode];
-				sm->last_victim[p.gc_mode] = 0;
+			if (original_offset != 0) {
+				last_segment = original_offset;
 				p.offset = 0;
+				if (sm->last_victim[p.gc_mode])
+					sm->last_victim[p.gc_mode] = 0;
 				continue;
 			}
 			break;
Signature · 8 lines
2.25.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw
Quoted 7 lines · On 1/7/2026 9:15 PM, liujinbao1 wrote:

From: liujinbao1 <[email protected]>

During the f2fs_get_victim process, when the f2fs_need_rand_seg is enabled in select_policy, p->offset is a random value, and the search range is from p->offset to MAIN_SECS. When segno >= last_segment, the loop breaks and exits directly without searching the range from 0 to p->offset.This results in an incomplete search when the random offset is not zero.

What about updating sm->last_victim[p.gc_mode] as well when p->offset is not zero in select_policy()?

select_policy()
...

	/* let's select beginning hot/small space first. */
	if (f2fs_need_rand_seg(sbi)) {
		p->offset = get_random_u32_below(MAIN_SECS(sbi) *
						SEGS_PER_SEC(sbi));
		if (p->offset)
			sm->last_victim[p.gc_mode] = p->offset;
	}

Then we can expect p->offset being reset to zero, and last_segment being reset to sm->last_victim[p.gc_mode] in below logic?

		if (segno >= last_segment) {
			if (sm->last_victim[p.gc_mode]) {
				last_segment =
					sm->last_victim[p.gc_mode];
				sm->last_victim[p.gc_mode] = 0;
				p.offset = 0;
				continue;
			}
			break;
		}

Thanks,

Quoted 32 lines
---
  fs/f2fs/gc.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
fs/f2fs/gc.c +2 -2
index 384fa7e2085b..100b8fc3e65a 100644
@@ -781,6 +781,7 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
  	unsigned int valid_thresh_ratio = 100;
  	bool is_atgc;
  	int ret = 0;
+	unsigned int original_offset;
 
  	mutex_lock(&dirty_i->seglist_lock);
  	last_segment = MAIN_SECS(sbi) * SEGS_PER_SEC(sbi);
@@ -799,6 +800,7 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
  	p.min_segno = NULL_SEGNO;
  	p.oldest_age = 0;
  	p.min_cost = get_max_cost(sbi, &p);
+	original_offset = p.offset;
 
  	is_atgc = (p.gc_mode == GC_AT || p.alloc_mode == AT_SSR);
  	nsearched = 0;
@@ -859,11 +861,11 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
  				p.offset / p.ofs_unit);
  		segno = unit_no * p.ofs_unit;
  		if (segno >= last_segment) {
-			if (sm->last_victim[p.gc_mode]) {
-				last_segment =

fs/f2fs/gc.c int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result, · line 871

Quoted 10 lines
fs/f2fs/gc.c +4 -2
-					sm->last_victim[p.gc_mode];
-				sm->last_victim[p.gc_mode] = 0;
+			if (original_offset != 0) {
+				last_segment = original_offset;
  				p.offset = 0;
+				if (sm->last_victim[p.gc_mode])
+					sm->last_victim[p.gc_mode] = 0;
  				continue;
  			}
  			break;

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

author
#2 lore raw
Quoted 36 lines · Chao Yu
Quoted 7 lines · On 1/7/2026 9:15 PM, liujinbao1 wrote:

From: liujinbao1 <[email protected]>

During the f2fs_get_victim process, when the f2fs_need_rand_seg is enabled in select_policy, p->offset is a random value, and the search range is from p->offset to MAIN_SECS. When segno >= last_segment, the loop breaks and exits directly without searching the range from 0 to p->offset.This results in an incomplete search when the random offset is not zero.

What about updating sm->last_victim[p.gc_mode] as well when p->offset is not zero in select_policy()?

select_policy()
...
    /* let's select beginning hot/small space first. */ if (f2fs_need_rand_seg(sbi)) {
        p->offset = get_random_u32_below(MAIN_SECS(sbi) * SEGS_PER_SEC(sbi));
        if (p->offset) sm->last_victim[p.gc_mode] = p->offset; }

Then we can expect p->offset being reset to zero, and last_segment being reset to sm->last_victim[p.gc_mode] in below logic?

        if (segno >= last_segment) {
            if (sm->last_victim[p.gc_mode]) {
                last_segment =
                    sm->last_victim[p.gc_mode];
                sm->last_victim[p.gc_mode] = 0;
                p.offset = 0;
                continue;
            }
            break;
        }

Quoted 2 lines

Thanks,

Yes,you are right, thanks

Quoted 35 lines
Quoted 35 lines

---
  fs/f2fs/gc.c | 10 ++++++----

  1 file changed, 6 insertions(+), 4 deletions(-)
fs/f2fs/gc.c +2 -4
index 384fa7e2085b..100b8fc3e65a 100644
@@ -781,6 +781,7 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi, 
 unsigned int *result,
       unsigned int valid_thresh_ratio = 100;
       bool is_atgc;
       int ret = 0;
+    unsigned int original_offset;
         mutex_lock(&dirty_i->seglist_lock);
       last_segment = MAIN_SECS(sbi) * SEGS_PER_SEC(sbi);
@@ -799,6 +800,7 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi, 
 unsigned int *result,
       p.min_segno = NULL_SEGNO;
       p.oldest_age = 0;
       p.min_cost = get_max_cost(sbi, &p);
+    original_offset = p.offset;
         is_atgc = (p.gc_mode == GC_AT || p.alloc_mode == AT_SSR);
       nsearched = 0;
@@ -859,11 +861,11 @@ int f2fs_get_victim(struct f2fs_sb_info *sbi, 
 unsigned int *result,
                   p.offset / p.ofs_unit);
           segno = unit_no * p.ofs_unit;
           if (segno >= last_segment) {
-            if (sm->last_victim[p.gc_mode]) {
-                last_segment =
-                    sm->last_victim[p.gc_mode];
-                sm->last_victim[p.gc_mode] = 0;
Quoted 9 lines
Quoted 8 lines
fs/f2fs/gc.c +4 -0
+            if (original_offset != 0) {
+                last_segment = original_offset;
                   p.offset = 0;
+                if (sm->last_victim[p.gc_mode])
+                    sm->last_victim[p.gc_mode] = 0;
                   continue;
               }

              break;

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