Skip to contentSkip to conversation

f2fs: recompute the pinned area boundary after resizing

PATCH In review Zhan Xusheng 1 participant 3 messages Sep 22 – Sep 23 F2FS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs: recompute the pinned area boundary after resizing

sbi->pinned_area_max_secno is computed at mount and remount time only. f2fs_resize_fs() shrinks MAIN_SECS(sbi) without recomputing it:

mount -o resizable_tail_secno=4 MAIN_SECS 248, boundary 244
F2FS_IOC_RESIZE_FS, -24 sections MAIN_SECS 224, boundary 244

get_new_segment() scans free_secmap up to that boundary for pinned allocations, and the removed sections are free there: the bitmap is sized once in build_free_segmap() and free_segment_range() requires the range to be free before it completes. Nothing between the find_next_zero_bit() and got_it compares secno against MAIN_SECS(sbi), so a pinned section can land past the end of the shrunk filesystem. has_unpinned_area() also turns false, which disables the range GC retry in f2fs_allocate_pinning_section().

update_fs_metadata() already clamps allocate_section_hint when MAIN_SECS() changes; recompute the pinned boundary there too. Recomputing rather than clamping keeps what resizable_tail_secno asks for: the boundary becomes 220 at MAIN_SECS 224, so a further shrink is still protected.

Fixes c966d29e01bb ("f2fs: support resizable tail section and unify pinned allocation")

---
Measured on x86_64 under QEMU with a 512 MiB image (1 segment per section); the boundary values above are read from /sys/fs/f2fs/<dev>/pinned_area_max_secno before and after the resize. I did not fill sections 0..223 to make a pinned allocation actually select one of the removed sections; that part is from reading get_new_segment().

 fs/f2fs/f2fs.h  | 1 +
 fs/f2fs/gc.c    | 1 +
 fs/f2fs/super.c | 6 +++---
 3 files changed, 5 insertions(+), 3 deletions(-)
fs/f2fs/f2fs.h +1 -0
index 9940a6cecf1a..07387be40f65 100644
@@ -3979,6 +3979,7 @@ void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error);
 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
 int f2fs_sync_fs(struct super_block *sb, int sync);
 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
+void f2fs_adjust_pinned_area_boundary(struct f2fs_sb_info *sbi);
 
 /*
  * hash.c
fs/f2fs/gc.c +1 -0
index 0a00180c21dc..a20ac70d53b8 100644
@@ -2328,6 +2328,7 @@ static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs)
 	MAIN_SECS(sbi) += secs;
 	if (sbi->allocate_section_hint > MAIN_SECS(sbi))
 		sbi->allocate_section_hint = MAIN_SECS(sbi);
+	f2fs_adjust_pinned_area_boundary(sbi);
 	FREE_I(sbi)->free_sections = (int)FREE_I(sbi)->free_sections + secs;
 	FREE_I(sbi)->free_segments = (int)FREE_I(sbi)->free_segments + segs;
 	F2FS_CKPT(sbi)->user_block_count = cpu_to_le64(user_block_count + blks);
fs/f2fs/super.c +3 -3
index 1314b6ccced9..65ef8be2a614 100644
@@ -554,7 +554,7 @@ static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
 			F2FS_OPTION(sbi).unusable_cap_perc);
 }
 
-static inline void adjust_pinned_area_boundary(struct f2fs_sb_info *sbi)
+void f2fs_adjust_pinned_area_boundary(struct f2fs_sb_info *sbi)
 {
 	sbi->pinned_area_max_secno = MAIN_SECS(sbi);
 	if (f2fs_sb_has_blkzoned(sbi) && sbi->first_seq_zone_segno != NULL_SEGNO)
@@ -3078,7 +3078,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
 		(test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
 
-	adjust_pinned_area_boundary(sbi);
+	f2fs_adjust_pinned_area_boundary(sbi);
 	limit_reserve_root(sbi);
 	fc->sb_flags = (flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
 
@@ -5321,7 +5321,7 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 	/* get segno of first zoned block device */
 	sbi->first_seq_zone_segno = get_first_seq_zone_segno(sbi);
 
-	adjust_pinned_area_boundary(sbi);
+	f2fs_adjust_pinned_area_boundary(sbi);
 
 	sbi->reserved_pin_section = f2fs_sb_has_blkzoned(sbi) ?
 			ZONED_PIN_SEC_REQUIRED_COUNT :

base-commit: f0100363d8c374bd8e9ea7c9ba02744f0b802ca4

Signature · 8 lines
2.43.0



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

Please hold this one.

resizable_tail_secno is only checked against the section count at mount and remount, so after a shrink it can exceed MAIN_SECS(), and the subtraction in f2fs_adjust_pinned_area_boundary() is unsigned:

	sbi->pinned_area_max_secno = min(sbi->pinned_area_max_secno,
		MAIN_SECS(sbi) - F2FS_OPTION(sbi).resizable_tail_secno);

resizable_tail_secno=200 on a 248 section image, shrunk to 192 sections:

without the patch MAIN_SECS 192, boundary 48
with the patch MAIN_SECS 192, boundary 192

min() picks MAIN_SECS(), has_unpinned_area() turns false and the reservation is gone. The stale 48 kept more of the tail clear, so here the patch makes things worse.

On an exact match the boundary becomes 0 and

	f2fs_gc_range(sbi, 0,
		sbi->pinned_area_max_secno * SEGS_PER_SEC(sbi) - 1, ...)

passes UINT_MAX as end_seg. f2fs_gc_range() does not bound end_seg and get_valid_blocks() indexes sit_i->sentries[] with no check. I did not hit the exact match; that part is from reading the code.

What should happen when the reservation no longer fits: refuse the resize, drop it, or clamp resizable_tail_secno? I will respin once that is settled.

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

#2 lore raw
In reply to Zhan Xusheng

Wenjie's v2 from Aug 29 already covers this and more: it rejects a shrink that would not leave the tail, which mine would have let wrap, rechecks the tail after the freeze, and restores the boundary when the final checkpoint rolls the resize back.

	https://lore.kernel.org/r/[email protected]

Please drop mine. I searched the git history but not the list.

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