Re: [PATCH] skip I_CLEAR state inodes

From: Jan Kara
Date: Wed Jun 03 2009 - 10:00:37 EST


On Wed 03-06-09 21:32:02, Wu Fengguang wrote:
> [reply the easy part first]
> On Tue, Jun 02, 2009 at 07:37:36PM +0800, Jan Kara wrote:
> > On Tue 02-06-09 16:55:23, Wu Fengguang wrote:
> > > On Tue, Jun 02, 2009 at 05:38:35AM +0800, Eric Sandeen wrote:
> > > > Wu Fengguang wrote:
> > > > > Add I_CLEAR tests to drop_pagecache_sb(), generic_sync_sb_inodes() and
> > > > > add_dquot_ref().
> > > > >
> > > > > clear_inode() will switch inode state from I_FREEING to I_CLEAR,
> > > > > and do so _outside_ of inode_lock. So any I_FREEING testing is
> > > > > incomplete without the testing of I_CLEAR.
> > > > >
> > > > > Masayoshi MIZUMA first discovered the bug in drop_pagecache_sb() and
> > > > > Jan Kara reminds fixing the other two cases. Thanks!
> > > >
> > > > Is there a reason it's not done for __sync_single_inode as well?
> > >
> > > It missed the glance because it don't have an obvious '|' in the line ;)
> > >
> > > > Jeff Layton asked the question and I'm following it up :)
> > > >
> > > > __sync_single_inode currently only tests I_FREEING, but I think we are
> > > > safe because __sync_single_inode sets I_SYNC, and clear_inode waits for
> > > > I_SYNC to be cleared before it changes I_STATE.
> > >
> > > But I_SYNC is removed just before the I_FREEING test, so we still have
> > > a small race window?
> > >
> > > > On the other hand, testing I_CLEAR here probably would be safe anyway,
> > > > and it'd be bonus points for consistency?
> > >
> > > So let's add the I_CLEAR test?
> > >
> > > > Same basic question for generic_sync_sb_inodes, which has a
> > > > BUG_ON(inode->i_state & I_FREEING), seems like this could check I_CLWAR
> > > > as well?
> > >
> > > Yes, we can add I_CLEAR here to catch more error condition.
> > >
> > > Thanks,
> > > Fengguang
> > >
> > > ---
> > > skip I_CLEAR state inodes in writeback routines
> > >
> > > The I_FREEING test in __sync_single_inode() is racy because
> > > clear_inode() can set i_state to I_CLEAR between the clear of I_SYNC
> > > and the test of I_FREEING.
> > >
> > > Also extend the coverage of BUG_ON(I_FREEING) to I_CLEAR.
> > >
> > > Reported-by: Jeff Layton <jlayton@xxxxxxxxxx>
> > > Reported-by: Eric Sandeen <sandeen@xxxxxxxxxxx>
> > > Signed-off-by: Wu Fengguang <fengguang.wu@xxxxxxxxx>
> > > ---
> > > fs/fs-writeback.c | 4 ++--
> > > 1 file changed, 2 insertions(+), 2 deletions(-)
> > >
> > > --- linux.orig/fs/fs-writeback.c
> > > +++ linux/fs/fs-writeback.c
> > > @@ -316,7 +316,7 @@ __sync_single_inode(struct inode *inode,
> > > spin_lock(&inode_lock);
> > > WARN_ON(inode->i_state & I_NEW);
> > > inode->i_state &= ~I_SYNC;
> > > - if (!(inode->i_state & I_FREEING)) {
> > > + if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
> > > if (!(inode->i_state & I_DIRTY) &&
> > > mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
> > Is the whole if needed? I had an impression that everyone calling
> > __sync_single_inode() should better take care it does not race with inode
> > freeing... So WARN_ON would be more appropriate IMHO.
>
> The caller doesn't matter here, because we temporarily dropped inode_lock
> in __sync_single_inode() ?
But the problem is basically what I described in the part you cut from
the email - if the caller of __sync_single_inode() does not have a
reference to the inode or some other means of protecting inode from being
released, then we have a problem because e.g. __writeback_single_inode()
can sleep and before I_SYNC is set, all the clear_inode() can finish and
we end up calling do_writepages() on invalidated mapping.
So in my opinion everybody calling __writeback_single_inode() and thus
__sync_single_inode() should do an equivalent of igrab() or be sure inode
cannot be freed e.g. because we have a file open.

Honza
--
Jan Kara <jack@xxxxxxx>
SUSE Labs, CR
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/