• [f2fs-dev] [PATCH v4] f2fs: introduce discard_granularity sysfs ent

    From Chao Yu@21:1/5 to Ju Hyung Park on Mon Oct 2 22:50:02 2017
    Hi Park,

    Thanks for the report, could have a try with below patch:

    From 5fa30e8cdcb93f210e25142c48a884be383c6121 Mon Sep 17 00:00:00 2001
    From: Chao Yu <yuchao0@huawei.com>
    Date: Mon, 2 Oct 2017 02:50:16 +0800
    Subject: [PATCH] f2fs: fix potential panic during fstrim

    As Ju Hyung Park reported:

    "When 'fstrim' is called for manual trim, a BUG() can be triggered
    randomly with this patch.

    I'm seeing this issue on both x86 Desktop and arm64 Android phone.

    On x86 Desktop, this was caused during Ubuntu boot-up. I have a
    cronjob installed which calls 'fstrim -v /' during boot. On arm64
    Android, this was caused during GC looping with 1ms gc_min_sleep_time
    & gc_max_sleep_time."

    Root cause of this issue is that f2fs_wait_discard_bios can only be
    used by f2fs_put_super, because during put_super there must be no
    other referrers, so it can ignore discard entry's reference count
    when removing the entry, otherwise in other caller we will hit bug_on
    in __remove_discard_cmd as there may be other issuer added reference
    count in discard entry.

    Thread A Thread B
    - issue_discard_thread
    - f2fs_ioc_fitrim
    - f2fs_trim_fs
    - f2fs_wait_discard_bios
    - __issue_discard_cmd
    - __submit_discard_cmd
    - __wait_discard_cmd
    - dc->ref++
    - __wait_one_discard_bio
    - __wait_discard_cmd
    - __remove_discard_cmd
    - f2fs_bug_on(sbi, dc->ref)

    Fixes: 969d1b180d987c2be02de890d0fff0f66a0e80de
    Reported-by: Ju Hyung Park <qkrwngud825@gmail.com>
    Signed-off-by: Chao Yu <yuchao0@huawei.com>
    ---
    fs/f2fs/f2fs.h | 2 +-
    fs/f2fs/segment.c | 6 +++---
    fs/f2fs/super.c | 2 +-
    3 files changed, 5 insertions(+), 5 deletions(-)

    diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
    index 9a7c90386947..4b4a72f392be 100644
    --- a/fs/f2fs/f2fs.h
    +++ b/fs/f2fs/f2fs.h
    @@ -2525,7 +2525,7 @@ void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
    bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
    void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new);
    void stop_discard_thread(struct f2fs_sb_info *sbi);
    -void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
    +void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi, bool umount);
    void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
    void release_discard_addrs(struct f2fs_sb_info *sbi);
    int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
    diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
    index dedf0209d820..e28245b7e44e 100644
    --- a/fs/f2fs/segment.c
    +++ b/