f2fs: allow to change discard policy based on cached discard cmds
authorSahitya Tummala <stummala@codeaurora.org>
Tue, 16 Mar 2021 09:29:18 +0000 (14:59 +0530)
committerJaegeuk Kim <jaegeuk@kernel.org>
Fri, 26 Mar 2021 17:27:44 +0000 (10:27 -0700)
With the default DPOLICY_BG discard thread is ioaware, which prevents
the discard thread from issuing the discard commands. On low RAM setups,
it is observed that these discard commands in the cache are consuming
high memory. This patch aims to relax the memory pressure on the system
due to f2fs pending discard cmds by changing the policy to DPOLICY_FORCE
based on the nm_i->ram_thresh configured.

Signed-off-by: Sahitya Tummala <stummala@codeaurora.org>
Reviewed-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/node.c
fs/f2fs/node.h
fs/f2fs/segment.c

index 45c8cf1afe6661b2ca2bfd30fa5ae65acee47def..3eb724bb659466cebc39badb1bbc2c222b74073e 100644 (file)
@@ -43,11 +43,15 @@ int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
 bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
 {
        struct f2fs_nm_info *nm_i = NM_I(sbi);
+       struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
        struct sysinfo val;
        unsigned long avail_ram;
        unsigned long mem_size = 0;
        bool res = false;
 
+       if (!nm_i)
+               return true;
+
        si_meminfo(&val);
 
        /* only uses low memory */
@@ -89,6 +93,10 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
                /* it allows 20% / total_ram for inmemory pages */
                mem_size = get_pages(sbi, F2FS_INMEM_PAGES);
                res = mem_size < (val.totalram / 5);
+       } else if (type == DISCARD_CACHE) {
+               mem_size = (atomic_read(&dcc->discard_cmd_cnt) *
+                               sizeof(struct discard_cmd)) >> PAGE_SHIFT;
+               res = mem_size < (avail_ram * nm_i->ram_thresh / 100);
        } else {
                if (!sbi->sb->s_bdi->wb.dirty_exceeded)
                        return true;
index f84541b57acbbc06a29d87256232a4b1b311501b..7a45c0f106295f2133f9bf1785db32579dff11dc 100644 (file)
@@ -147,6 +147,7 @@ enum mem_type {
        INO_ENTRIES,    /* indicates inode entries */
        EXTENT_CACHE,   /* indicates extent cache */
        INMEM_PAGES,    /* indicates inmemory pages */
+       DISCARD_CACHE,  /* indicates memory of cached discard cmds */
        BASE_CHECK,     /* check kernel status */
 };
 
index 33cb8aa5ec8fdc599a317109cc9318f795c085d1..ad48f1f16387735bf1921c2ffe1bb7035e2e93ff 100644 (file)
@@ -1762,7 +1762,8 @@ static int issue_discard_thread(void *data)
                if (!atomic_read(&dcc->discard_cmd_cnt))
                        continue;
 
-               if (sbi->gc_mode == GC_URGENT_HIGH)
+               if (sbi->gc_mode == GC_URGENT_HIGH ||
+                       !f2fs_available_free_memory(sbi, DISCARD_CACHE))
                        __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, 1);
 
                sb_start_intwrite(sbi->sb);