Skip to contentSkip to conversation

f2fs: force discard when undiscard_blks exceeds threshold

PATCH New liujinbao1 1 participant 1 message Sep 5 F2FS file system

lore
Share & tools
lore

liujinbao1

author
#0 lore raw

PATCH 1/1 f2fs: force discard when undiscard_blks exceeds threshold

From: liujinbao1 <[email protected]>

Asynchronous discard issuance can build up a backlog.
Force discard when undiscard_blks is large relative to free space, or when undiscard_blks is too large under high utilization. This reclaims pending discards in time and keeps UFS performance stable.

---
 Documentation/ABI/testing/sysfs-fs-f2fs | 14 +++++++++++++
 fs/f2fs/f2fs.h                          |  4 ++++
 fs/f2fs/segment.c                       | 28 ++++++++++++++++++++++++-
 fs/f2fs/sysfs.c                         | 11 ++++++++++
 4 files changed, 56 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-fs-f2fs +14 -0
index 0cebc89799dd..be1ced3dc8db 100644
@@ -791,6 +791,20 @@ Contact:	"Chao Yu" <[email protected]>
 Description:	It controls to enable/disable IO aware feature for background discard.
 		By default, the value is 1 which indicates IO aware is on.
 
+What:		/sys/fs/f2fs/<disk>/discard_free_ratio
+Date:		September 2026
+Contact:	"Jinbao Liu" <[email protected]>
+Description:	Controls when background discard should switch to forced mode
+		when storage utilization is low, based on the amount of undiscard_blks
+		relative to free blocks.
+
+What:		/sys/fs/f2fs/<disk>/discard_blks_thresh
+Date:		September 2026
+Contact:	"Jinbao Liu" <[email protected]>
+Description:	Controls when background discard should switch to forced mode
+		when storage utilization is high, based on the number of pending
+		undiscard blocks.
+
 What:		/sys/fs/f2fs/<disk>/blkzone_alloc_policy
 Date:		July 2024
 Contact:	"Yuanhong Liao" <[email protected]>
fs/f2fs/f2fs.h +4 -0
index 2f7ab5888b07..36f2c58b0671 100644
@@ -325,6 +325,8 @@ enum {
 #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
 #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
 #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
+#define DEF_DISCARD_FREE_RATIO		80	/* force discard over 80% free blocks */
+#define DEF_DISCARD_BLKS_THRESH		13107200	/* 50GB in 4KB blocks */
 
 enum cp_time {
 	CP_TIME_START,		/* begin */
@@ -542,6 +544,8 @@ struct discard_cmd_control {
 	unsigned int max_ordered_discard;	/* maximum discard granularity issued by lba order */
 	unsigned int discard_io_aware;		/* io_aware policy */
 	unsigned int undiscard_blks;		/* # of undiscard blocks */
+	unsigned int discard_free_ratio;	/* discard ratio of free blocks to force discard */
+	unsigned int discard_blks_thresh;	/* discard threshold to force discard, in blocks */
 	unsigned int next_pos;			/* next discard position */
 	atomic_t issued_discard;		/* # of issued discard */
 	atomic_t queued_discard;		/* # of queued discard */
fs/f2fs/segment.c +27 -1
index 63b712d3d599..da4ed7e71c0b 100644
@@ -1957,6 +1957,29 @@ bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi, bool need_check)
 	return !dropped;
 }
 
+static bool f2fs_available_free_space(struct f2fs_sb_info *sbi)
+{
+	struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
+	unsigned int avail_user_block_count;
+	unsigned int total_free_blocks;
+
+	spin_lock(&sbi->stat_lock);
+	avail_user_block_count = sbi->user_block_count -
+					sbi->current_reserved_blocks;
+	total_free_blocks = avail_user_block_count - (unsigned int)valid_user_blocks(sbi);
+	spin_unlock(&sbi->stat_lock);
+
+	if (utilization(sbi) < dcc->discard_urgent_util) {
+		if (dcc->undiscard_blks >=
+			div_u64((u64)total_free_blocks * dcc->discard_free_ratio, 100))
+			return false;
+	} else if (dcc->undiscard_blks > dcc->discard_blks_thresh) {
+		return false;
+	}
+
+	return true;
+}
+
 static int issue_discard_thread(void *data)
 {
 	struct f2fs_sb_info *sbi = data;
@@ -1974,7 +1997,8 @@ static int issue_discard_thread(void *data)
 				msecs_to_jiffies(wait_ms));
 
 		if (sbi->gc_mode == GC_URGENT_HIGH ||
-			!f2fs_available_free_memory(sbi, DISCARD_CACHE))
+			!f2fs_available_free_memory(sbi, DISCARD_CACHE) ||
+			!f2fs_available_free_space(sbi))
 			__init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE,
 						MIN_DISCARD_GRANULARITY);
 		else
@@ -2393,6 +2417,8 @@ static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
 	dcc->mid_discard_issue_time = DEF_MID_DISCARD_ISSUE_TIME;
 	dcc->max_discard_issue_time = DEF_MAX_DISCARD_ISSUE_TIME;
 	dcc->discard_urgent_util = DEF_DISCARD_URGENT_UTIL;
+	dcc->discard_free_ratio = DEF_DISCARD_FREE_RATIO;
+	dcc->discard_blks_thresh = DEF_DISCARD_BLKS_THRESH;
 	dcc->undiscard_blks = 0;
 	dcc->next_pos = 0;
 	dcc->root = RB_ROOT_CACHED;
fs/f2fs/sysfs.c +11 -0
index 811e350a1430..ff12df62e83f 100644
@@ -642,6 +642,13 @@ static ssize_t __sbi_store(struct f2fs_attr *a,
 		return count;
 	}
 
+	if (!strcmp(a->attr.name, "discard_free_ratio")) {
+		if (t > 100)
+			return -EINVAL;
+		*ui = t;
+		return count;
+	}
+
 	if (!strcmp(a->attr.name, "discard_io_aware")) {
 		if (t >= DPOLICY_IO_AWARE_MAX)
 			return -EINVAL;
@@ -1250,6 +1257,8 @@ DCC_INFO_GENERAL_RW_ATTR(discard_urgent_util);
 DCC_INFO_GENERAL_RW_ATTR(discard_granularity);
 DCC_INFO_GENERAL_RW_ATTR(max_ordered_discard);
 DCC_INFO_GENERAL_RW_ATTR(discard_io_aware);
+DCC_INFO_GENERAL_RW_ATTR(discard_free_ratio);
+DCC_INFO_GENERAL_RW_ATTR(discard_blks_thresh);
 
 /* NM_INFO ATTR */
 NM_INFO_RW_ATTR(max_roll_forward_node_blocks, max_rf_node_blocks);
@@ -1430,6 +1439,8 @@ static struct attribute *f2fs_attrs[] = {
 	ATTR_LIST(discard_granularity),
 	ATTR_LIST(max_ordered_discard),
 	ATTR_LIST(discard_io_aware),
+	ATTR_LIST(discard_free_ratio),
+	ATTR_LIST(discard_blks_thresh),
 	ATTR_LIST(pending_discard),
 	ATTR_LIST(gc_mode),
 	ATTR_LIST(ipu_policy),
Signature · 8 lines
2.43.0



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