Received: by 10.223.164.202 with SMTP id h10csp2733064wrb; Sun, 12 Nov 2017 17:14:18 -0800 (PST) X-Google-Smtp-Source: AGs4zMaJphSKvBcwXciu3HlBU42EzF7VZ7Ho4IH2F1bFgIaauxMkTjdyds9WyaxP1gO+OAC6ZWBY X-Received: by 10.84.195.131 with SMTP id j3mr7260961pld.286.1510535658378; Sun, 12 Nov 2017 17:14:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510535658; cv=none; d=google.com; s=arc-20160816; b=gPROkJE0KGCBW9TMFFcCj2UQaEmliG8tgBIxNs3kitC7ALRWNxSXDKhE/SX5M/Z+sa m6SGohAgkzgDL6JABECMg97FnDk2ovxbeeelw9+aJZsWFjVWCr599hLPCks3FBtHDf51 ampm9h8Ad3GYO8HhXKNXny+KsMjWJAPryoQDuWPnpxO8P9oNeiqarXOtwl5cb8ozkFGJ 2Rwyxbq4mWYylnPkZKP+ERd9m97iEysl2h+1NBACjV8d5h7jNqrENnV/+Bzi4yMYCqHZ 2FqpBlWButMm9aIQe8hLOZJud4ax1y6R1+QVeRw6ChHHkUwA+NMaRVqs3qbEPVgCUuNH FyNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:arc-authentication-results; bh=h+PRzaLgfxgNXGSkMWmyNHlxMtg4bwXfC+0iCKn7uv0=; b=wcZGTLHLSWfUU5T1oqt1CZMK/yDWVkucRSCaNV3mwuF+oFCQc3aIXDywIUS9nh49fp FcLhmLLkGQC46G0UdvwIdXHnhSVIhv6uHAoANezn7jVxUFEcz1pH9uVjURTYYWS+Wk3B U7WEGizsUjfYqibCcvfmQlnlsLwLAR/jvv7t8u7qgRi3YKkLVg25/wQtk+wwphe+scSk MvNCuL7dC0+SrgCqM2fXmoXr0JF6dxIiccMtfhX+n+4aEn4RL/7+agKGU/h94g+9Rd7C ESy+wWSlmWeaAdVVGrG3Wp5O24Do2P3kA0rcGLH5gt5CjBmb4YOH55sJ936dLXSPCZdv Zybg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j5si12821472pgt.437.2017.11.12.17.14.05; Sun, 12 Nov 2017 17:14:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751358AbdKMBN2 (ORCPT + 87 others); Sun, 12 Nov 2017 20:13:28 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:10520 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751061AbdKMBN0 (ORCPT ); Sun, 12 Nov 2017 20:13:26 -0500 Received: from 172.30.72.60 (EHLO DGGEMS410-HUB.china.huawei.com) ([172.30.72.60]) by dggrg05-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id DKV03316; Mon, 13 Nov 2017 09:12:55 +0800 (CST) Received: from [127.0.0.1] (10.111.220.140) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.361.1; Mon, 13 Nov 2017 09:12:12 +0800 Subject: Re: [PATCH v4] f2fs: fix out-of-free problem caused by atomic write To: , , , CC: , , , , References: <1509934071-116656-1-git-send-email-yunlong.song@huawei.com> <1510107446-52423-1-git-send-email-yunlong.song@huawei.com> From: Yunlong Song Message-ID: <4cbdba1e-9d9e-6de8-e367-3fd305d23eac@huawei.com> Date: Mon, 13 Nov 2017 09:11:39 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <1510107446-52423-1-git-send-email-yunlong.song@huawei.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Originating-IP: [10.111.220.140] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090202.5A08F197.008E,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 800ca351ad9c1667e4218c1cff500ea7 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ping... On 2017/11/8 10:17, Yunlong Song wrote: > f2fs_balance_fs only actives once in the commit_inmem_pages, but there > are more than one page to commit, so all the other pages will miss the > check. This will lead to out-of-free problem when commit a very large > file. However, we cannot do f2fs_balance_fs for each inmem page, since > this will break atomicity. As a result, we should do f2fs_balance_fs > for all the inmem pages together. > > Signed-off-by: Yunlong Song > --- > fs/f2fs/debug.c | 5 +++-- > fs/f2fs/f2fs.h | 26 ++++++++++++++++++++++++-- > fs/f2fs/segment.c | 30 ++++++++++++++++++++++++------ > fs/f2fs/segment.h | 4 +++- > 4 files changed, 54 insertions(+), 11 deletions(-) > > diff --git a/fs/f2fs/debug.c b/fs/f2fs/debug.c > index f7eec50..41c47c4 100644 > --- a/fs/f2fs/debug.c > +++ b/fs/f2fs/debug.c > @@ -50,6 +50,7 @@ static void update_general_status(struct f2fs_sb_info *sbi) > si->ndirty_files = sbi->ndirty_inode[FILE_INODE]; > si->ndirty_all = sbi->ndirty_inode[DIRTY_META]; > si->inmem_pages = get_pages(sbi, F2FS_INMEM_PAGES); > + si->inmem_commit_pages = get_pages(sbi, F2FS_INMEM_COMMIT_PAGES); > si->aw_cnt = atomic_read(&sbi->aw_cnt); > si->vw_cnt = atomic_read(&sbi->vw_cnt); > si->max_aw_cnt = atomic_read(&sbi->max_aw_cnt); > @@ -360,9 +361,9 @@ static int stat_show(struct seq_file *s, void *v) > si->nr_discarding, si->nr_discarded, > si->nr_discard_cmd, si->undiscard_blks); > seq_printf(s, " - inmem: %4d, atomic IO: %4d (Max. %4d), " > - "volatile IO: %4d (Max. %4d)\n", > + "volatile IO: %4d (Max. %4d), commit: %4d\n", > si->inmem_pages, si->aw_cnt, si->max_aw_cnt, > - si->vw_cnt, si->max_vw_cnt); > + si->vw_cnt, si->max_vw_cnt, si->inmem_commit_pages); > seq_printf(s, " - nodes: %4d in %4d\n", > si->ndirty_node, si->node_pages); > seq_printf(s, " - dents: %4d in dirs:%4d (%4d)\n", > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h > index 13a96b8..749bdb6 100644 > --- a/fs/f2fs/f2fs.h > +++ b/fs/f2fs/f2fs.h > @@ -610,6 +610,7 @@ struct f2fs_inode_info { > struct list_head inmem_pages; /* inmemory pages managed by f2fs */ > struct task_struct *inmem_task; /* store inmemory task */ > struct mutex inmem_lock; /* lock for inmemory pages */ > + unsigned long inmem_blocks; /* inmemory blocks */ > struct extent_tree *extent_tree; /* cached extent_tree entry */ > struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */ > struct rw_semaphore i_mmap_sem; > @@ -863,6 +864,7 @@ enum count_type { > F2FS_DIRTY_NODES, > F2FS_DIRTY_META, > F2FS_INMEM_PAGES, > + F2FS_INMEM_COMMIT_PAGES, > F2FS_DIRTY_IMETA, > F2FS_WB_CP_DATA, > F2FS_WB_DATA, > @@ -1600,7 +1602,21 @@ static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type) > atomic_inc(&sbi->nr_pages[count_type]); > > if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES || > - count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA) > + count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA || > + count_type == F2FS_INMEM_COMMIT_PAGES) > + return; > + > + set_sbi_flag(sbi, SBI_IS_DIRTY); > +} > + > +static inline void inc_pages_count(struct f2fs_sb_info *sbi, int count_type, > + int pages) > +{ > + atomic_add(pages, &sbi->nr_pages[count_type]); > + > + if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES || > + count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA || > + count_type == F2FS_INMEM_COMMIT_PAGES) > return; > > set_sbi_flag(sbi, SBI_IS_DIRTY); > @@ -1618,6 +1634,12 @@ static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type) > atomic_dec(&sbi->nr_pages[count_type]); > } > > +static inline void dec_pages_count(struct f2fs_sb_info *sbi, int count_type, > + int pages) > +{ > + atomic_sub(pages, &sbi->nr_pages[count_type]); > +} > + > static inline void inode_dec_dirty_pages(struct inode *inode) > { > if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && > @@ -2716,7 +2738,7 @@ struct f2fs_stat_info { > unsigned long long hit_total, total_ext; > int ext_tree, zombie_tree, ext_node; > int ndirty_node, ndirty_dent, ndirty_meta, ndirty_data, ndirty_imeta; > - int inmem_pages; > + int inmem_pages, inmem_commit_pages; > unsigned int ndirty_dirs, ndirty_files, ndirty_all; > int nats, dirty_nats, sits, dirty_sits; > int free_nids, avail_nids, alloc_nids; > diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c > index 46dfbca..2ff1bba4 100644 > --- a/fs/f2fs/segment.c > +++ b/fs/f2fs/segment.c > @@ -174,6 +174,8 @@ bool need_SSR(struct f2fs_sb_info *sbi) > int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES); > int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS); > int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA); > + int inmem_commit_secs = get_blocktype_secs(sbi, > + F2FS_INMEM_COMMIT_PAGES); > > if (test_opt(sbi, LFS)) > return false; > @@ -181,7 +183,7 @@ bool need_SSR(struct f2fs_sb_info *sbi) > return true; > > return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs + > - 2 * reserved_sections(sbi)); > + inmem_commit_secs + 2 * reserved_sections(sbi)); > } > > void register_inmem_page(struct inode *inode, struct page *page) > @@ -210,6 +212,7 @@ void register_inmem_page(struct inode *inode, struct page *page) > list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]); > spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); > inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); > + fi->inmem_blocks++; > mutex_unlock(&fi->inmem_lock); > > trace_f2fs_register_inmem_page(page, INMEM); > @@ -221,6 +224,7 @@ static int __revoke_inmem_pages(struct inode *inode, > struct f2fs_sb_info *sbi = F2FS_I_SB(inode); > struct inmem_pages *cur, *tmp; > int err = 0; > + struct f2fs_inode_info *fi = F2FS_I(inode); > > list_for_each_entry_safe(cur, tmp, head, list) { > struct page *page = cur->page; > @@ -263,6 +267,7 @@ static int __revoke_inmem_pages(struct inode *inode, > list_del(&cur->list); > kmem_cache_free(inmem_entry_slab, cur); > dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); > + fi->inmem_blocks--; > } > return err; > } > @@ -302,6 +307,10 @@ void drop_inmem_pages(struct inode *inode) > if (!list_empty(&fi->inmem_ilist)) > list_del_init(&fi->inmem_ilist); > spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); > + if (fi->inmem_blocks) { > + f2fs_bug_on(sbi, 1); > + fi->inmem_blocks = 0; > + } > mutex_unlock(&fi->inmem_lock); > > clear_inode_flag(inode, FI_ATOMIC_FILE); > @@ -326,6 +335,7 @@ void drop_inmem_page(struct inode *inode, struct page *page) > > f2fs_bug_on(sbi, !cur || cur->page != page); > list_del(&cur->list); > + fi->inmem_blocks--; > mutex_unlock(&fi->inmem_lock); > > dec_page_count(sbi, F2FS_INMEM_PAGES); > @@ -354,7 +364,7 @@ static int __commit_inmem_pages(struct inode *inode, > .io_type = FS_DATA_IO, > }; > pgoff_t last_idx = ULONG_MAX; > - int err = 0; > + int err = 0, inmem_blocks = fi->inmem_blocks; > > list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) { > struct page *page = cur->page; > @@ -390,6 +400,8 @@ static int __commit_inmem_pages(struct inode *inode, > } > unlock_page(page); > list_move_tail(&cur->list, revoke_list); > + dec_page_count(sbi, F2FS_INMEM_COMMIT_PAGES); > + inmem_blocks--; > } > > if (last_idx != ULONG_MAX) > @@ -397,6 +409,8 @@ static int __commit_inmem_pages(struct inode *inode, > > if (!err) > __revoke_inmem_pages(inode, revoke_list, false, false); > + else > + dec_pages_count(sbi, F2FS_INMEM_COMMIT_PAGES, inmem_blocks); > > return err; > } > @@ -409,12 +423,12 @@ int commit_inmem_pages(struct inode *inode) > int err; > > INIT_LIST_HEAD(&revoke_list); > + set_inode_flag(inode, FI_ATOMIC_COMMIT); > + mutex_lock(&fi->inmem_lock); > + inc_pages_count(sbi, F2FS_INMEM_COMMIT_PAGES, fi->inmem_blocks); > f2fs_balance_fs(sbi, true); > f2fs_lock_op(sbi); > > - set_inode_flag(inode, FI_ATOMIC_COMMIT); > - > - mutex_lock(&fi->inmem_lock); > err = __commit_inmem_pages(inode, &revoke_list); > if (err) { > int ret; > @@ -437,11 +451,15 @@ int commit_inmem_pages(struct inode *inode) > if (!list_empty(&fi->inmem_ilist)) > list_del_init(&fi->inmem_ilist); > spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); > + if (fi->inmem_blocks) { > + f2fs_bug_on(sbi, 1); > + fi->inmem_blocks = 0; > + } > + f2fs_unlock_op(sbi); > mutex_unlock(&fi->inmem_lock); > > clear_inode_flag(inode, FI_ATOMIC_COMMIT); > > - f2fs_unlock_op(sbi); > return err; > } > > diff --git a/fs/f2fs/segment.h b/fs/f2fs/segment.h > index 8d93652..f3885de 100644 > --- a/fs/f2fs/segment.h > +++ b/fs/f2fs/segment.h > @@ -503,12 +503,14 @@ static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi, > int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES); > int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS); > int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA); > + int inmem_commit_secs = get_blocktype_secs(sbi, > + F2FS_INMEM_COMMIT_PAGES); > > if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) > return false; > > return (free_sections(sbi) + freed) <= > - (node_secs + 2 * dent_secs + imeta_secs + > + (node_secs + 2 * dent_secs + imeta_secs + inmem_commit_secs + > reserved_sections(sbi) + needed); > } > -- Thanks, Yunlong Song From 1583482952507398027@xxx Wed Nov 08 07:43:42 +0000 2017 X-GM-THRID: 1582330312060990093 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread