[PATCH] f2fs: introduce discard_urgent_percent node

From: Yangtao Li
Date: Fri Apr 21 2023 - 14:44:31 EST


There will be IO performance degradation in the following scenarios:

-The space utilization rate remains below 80%(e.g. 79%)
-There are many big discard commands generated
-Always have read and write IO generated

The performance degradation is due to a large number of undiscard blocks
while the space occupancy rate is high. Space utilization is always lower
than discard_urgent_util, so discard thread is not running at
min_discard_issue_time interval. Since there are not many discard cmds,
the !f2fs_available_free_memory(DISCARD_CACHE) condition is not met,
and there is no chance to execute the DPOLICY_FORCE strategy.

By adjusting discard_io_aware_gran, the accumulation of discard can be
prevented, but this will affect the normal read and write IO.

This patch introduces another solution. By considering the space
utilization and the proportion of undiscard blocks, reducing the running
cycle of the discard thread, and reducing the granularity of the discard
cmd.

Signed-off-by: Yangtao Li <frank.li@xxxxxxxx>
---
Documentation/ABI/testing/sysfs-fs-f2fs | 9 +++++++++
fs/f2fs/f2fs.h | 2 ++
fs/f2fs/segment.c | 6 +++++-
fs/f2fs/segment.h | 8 ++++++++
fs/f2fs/sysfs.c | 9 +++++++++
5 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/Documentation/ABI/testing/sysfs-fs-f2fs b/Documentation/ABI/testing/sysfs-fs-f2fs
index 8140fc98f5ae..2fcb80b073e3 100644
--- a/Documentation/ABI/testing/sysfs-fs-f2fs
+++ b/Documentation/ABI/testing/sysfs-fs-f2fs
@@ -740,3 +740,12 @@ Description: When compress cache is on, it controls cached page
If cached page percent exceed threshold, then deny caching compress page.
The value should be in range of (0, 100], by default it was initialized
as 20(%).
+
+What: /sys/fs/f2fs/<disk>/discard_urgent_percent
+Date: April 2023
+Contact: "Yangtao Li" <frank.li@xxxxxxxx>
+Description: When percent((valid_user_blocks + undiscard_blks) / user_block_count) exceeds this,
+ do background DISCARD aggressively. Does DISCARD forcibly in a period of given
+ min_discard_issue_time when the number of discards is not 0 and set discard
+ granularity to 1.
+ Default: 80
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index d211ee89c158..7a7b380f5307 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -233,6 +233,7 @@ enum {
#define DEF_MID_DISCARD_ISSUE_TIME 500 /* 500 ms, if device busy */
#define DEF_MAX_DISCARD_ISSUE_TIME 60000 /* 60 s, if no candidates */
#define DEF_DISCARD_URGENT_UTIL 80 /* do more discard over 80% */
+#define DEF_DISCARD_URGENT_PERCENT 80 /* do more discard over 80% */
#define DEF_CP_INTERVAL 60 /* 60 secs */
#define DEF_IDLE_INTERVAL 5 /* 5 secs */
#define DEF_DISABLE_INTERVAL 5 /* 5 secs */
@@ -403,6 +404,7 @@ struct discard_cmd_control {
unsigned int max_discard_issue_time; /* max. interval between discard issue */
unsigned int discard_io_aware_gran; /* minimum discard granularity not be aware of I/O */
unsigned int discard_urgent_util; /* utilization which issue discard proactively */
+ unsigned int discard_urgent_percent; /* percent which issue discard proactively */
unsigned int discard_granularity; /* discard granularity */
unsigned int max_ordered_discard; /* maximum discard granularity issued by lba order */
unsigned int undiscard_blks; /* # of undiscard blocks */
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index a005948dca93..47a8d97bb291 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -1166,13 +1166,16 @@ static void __init_discard_policy(struct f2fs_sb_info *sbi,
dpolicy->timeout = false;

if (discard_type == DPOLICY_BG) {
+ int percent = undiscard_percent(sbi) + utilization(sbi);
+
dpolicy->min_interval = dcc->min_discard_issue_time;
dpolicy->mid_interval = dcc->mid_discard_issue_time;
dpolicy->max_interval = dcc->max_discard_issue_time;
dpolicy->io_aware = true;
dpolicy->sync = false;
dpolicy->ordered = true;
- if (utilization(sbi) > dcc->discard_urgent_util) {
+ if (utilization(sbi) > dcc->discard_urgent_util ||
+ percent > dcc->discard_urgent_percent) {
dpolicy->granularity = MIN_DISCARD_GRANULARITY;
if (atomic_read(&dcc->discard_cmd_cnt))
dpolicy->max_interval =
@@ -2197,6 +2200,7 @@ 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_urgent_percent = DEF_DISCARD_URGENT_PERCENT;
dcc->undiscard_blks = 0;
dcc->next_pos = 0;
dcc->root = RB_ROOT_CACHED;
diff --git a/fs/f2fs/segment.h b/fs/f2fs/segment.h
index 2ca8fb5d0dc4..278b85acddac 100644
--- a/fs/f2fs/segment.h
+++ b/fs/f2fs/segment.h
@@ -669,6 +669,14 @@ static inline int utilization(struct f2fs_sb_info *sbi)
sbi->user_block_count);
}

+static inline int undiscard_percent(struct f2fs_sb_info *sbi)
+{
+ struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
+
+ return div_u64((u64)dcc->undiscard_blks * 100,
+ sbi->user_block_count);
+}
+
/*
* Sometimes f2fs may be better to drop out-of-place update policy.
* And, users can control the policy through sysfs entries.
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index e319e05cf23e..a0798e74a870 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -724,6 +724,13 @@ static ssize_t __sbi_store(struct f2fs_attr *a,
return count;
}

+ if (!strcmp(a->attr.name, "discard_urgent_percent")) {
+ if (t > 100)
+ return -EINVAL;
+ *ui = t;
+ return count;
+ }
+
*ui = (unsigned int)t;

return count;
@@ -913,6 +920,7 @@ DCC_INFO_GENERAL_RW_ATTR(mid_discard_issue_time);
DCC_INFO_GENERAL_RW_ATTR(max_discard_issue_time);
DCC_INFO_GENERAL_RW_ATTR(discard_io_aware_gran);
DCC_INFO_GENERAL_RW_ATTR(discard_urgent_util);
+DCC_INFO_GENERAL_RW_ATTR(discard_urgent_percent);
DCC_INFO_GENERAL_RW_ATTR(discard_granularity);
DCC_INFO_GENERAL_RW_ATTR(max_ordered_discard);

@@ -1059,6 +1067,7 @@ static struct attribute *f2fs_attrs[] = {
ATTR_LIST(max_discard_issue_time),
ATTR_LIST(discard_io_aware_gran),
ATTR_LIST(discard_urgent_util),
+ ATTR_LIST(discard_urgent_percent),
ATTR_LIST(discard_granularity),
ATTR_LIST(max_ordered_discard),
ATTR_LIST(pending_discard),
--
2.39.0