From: Matthew Wilcox <willy@infradead.org>
To: Shijie Luo <luoshijie1@huawei.com>
Cc: linux-fsdevel@vger.kernel.org, viro@zeniv.linux.org.uk,
lihaotian9@huawei.com, lutianxiong@huawei.com, jack@suse.cz,
linfeilong@huawei.com
Subject: Re: [PATCH RESEND] fs: fix race condition oops between destroy_inode and writeback_sb_inodes
Date: Sat, 19 Sep 2020 15:56:32 +0100 [thread overview]
Message-ID: <20200919145632.GM32101@casper.infradead.org> (raw)
In-Reply-To: <20200919093923.19016-1-luoshijie1@huawei.com>
On Sat, Sep 19, 2020 at 05:39:23AM -0400, Shijie Luo wrote:
> There is a race condition between destroy_inode and writeback_sb_inodes,
> thread-1 thread-2
> wb_workfn
> writeback_inodes_wb
> __writeback_inodes_wb
> writeback_sb_inodes
> wbc_attach_and_unlock_inode
> iget_locked
> destroy_inode
> inode_detach_wb
> inode->i_wb = NULL;
>
> inode_to_wb_and_lock_list
> locked_inode_to_wb_and_lock_list
> wb_get
> oops
>
> so destroy inode after adding I_FREEING to inode state and the I_SYNC state
> being cleared.
>
> Reported-by: Tianxiong Lu <lutianxiong@huawei.com>
> Signed-off-by: Shijie Luo <luoshijie1@huawei.com>
> Signed-off-by: Haotian Li <lihaotian9@huawei.com>
> ---
> fs/inode.c | 14 +++++++++++++-
> 1 file changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/fs/inode.c b/fs/inode.c
> index 72c4c347afb7..b28a2a9e15d5 100644
> --- a/fs/inode.c
> +++ b/fs/inode.c
> @@ -1148,10 +1148,17 @@ struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
> struct inode *new = alloc_inode(sb);
>
> if (new) {
> + spin_lock(&new->i_lock);
> new->i_state = 0;
> + spin_unlock(&new->i_lock);
This part is unnecessary. We just allocated 'new' two lines above;
nobody else can see 'new' yet. We make it visible with hlist_add_head_rcu()
which uses rcu_assign_pointer() whch contains a memory barrier, so it's
impossible for another CPU to see a stale i_state.
> inode = inode_insert5(new, hashval, test, set, data);
> - if (unlikely(inode != new))
> + if (unlikely(inode != new)) {
> + spin_lock(&new->i_lock);
> + new->i_state |= I_FREEING;
> + spin_unlock(&new->i_lock);
> + inode_wait_for_writeback(new);
> destroy_inode(new);
This doesn't make sense either. If an inode is returned here which is not
'new', then adding 'new' to the hash failed, and new was never visible
to another CPU.
> @@ -1218,6 +1225,11 @@ struct inode *iget_locked(struct super_block *sb, unsigned long ino)
> * allocated.
> */
> spin_unlock(&inode_hash_lock);
> +
> + spin_lock(&inode->i_lock);
> + inode->i_state |= I_FREEING;
> + spin_unlock(&inode->i_lock);
> + inode_wait_for_writeback(inode);
> destroy_inode(inode);
Again, this doesn't make sense. This is also a codepath which failed to
make 'inode' visible to any other thread.
I don't understand how this patch could fix anything.
next prev parent reply other threads:[~2020-09-19 14:56 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-19 9:39 [PATCH RESEND] fs: fix race condition oops between destroy_inode and writeback_sb_inodes Shijie Luo
2020-09-19 14:56 ` Matthew Wilcox [this message]
2020-09-21 8:29 ` Shijie Luo
2020-09-21 10:25 ` Jan Kara
2020-09-24 14:00 ` Shijie Luo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200919145632.GM32101@casper.infradead.org \
--to=willy@infradead.org \
--cc=jack@suse.cz \
--cc=lihaotian9@huawei.com \
--cc=linfeilong@huawei.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=luoshijie1@huawei.com \
--cc=lutianxiong@huawei.com \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).