From: Andrew Morton Subject: Re: [PATCH] ext3 [linux-2.6.2.]: accessing already freed inodes when under memory pressure Date: Thu, 19 Feb 2004 19:41:05 -0800 Sender: linux-fsdevel-owner@vger.kernel.org Message-ID: <20040219194105.5f1a6e7e.akpm@osdl.org> References: <200402191321.39592.cotte@freenet.de> <1077212393.2070.571.camel@sisko.scot.redhat.com> <200402192114.55214.cotte@freenet.de> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: sct@redhat.com, torvalds@osdl.org, linux-fsdevel@vger.kernel.org, schwidefsky@de.ibm.com, cotte@de.ibm.com Return-path: Received: from fw.osdl.org ([65.172.181.6]:25730 "EHLO mail.osdl.org") by vger.kernel.org with ESMTP id S267620AbUBTDlI (ORCPT ); Thu, 19 Feb 2004 22:41:08 -0500 To: Carsten Otte In-Reply-To: <200402192114.55214.cotte@freenet.de> List-Id: linux-fsdevel.vger.kernel.org Carsten Otte wrote: > > I tried the following *dirty* debugging patch that triggers in iput_final() > exactly when i_count is lowered to zero while the orphan list entry does > still persist. That patch isn't right. Note how we go on to generic_delete_inode() which then does the orphan_del(). The below patch should detect us adding orphan inodes to inode_unused. Could you see if this triggers? It doesn't for me, of course :( Also, I have gone through and verified that all modifications of inode->i_nlink are happening under inode->i_sem, so scrub that theory. I'm a bit suspicious of the code in __sync_single_inode(), where it decides to dump the inode on inode_unused. Maybe it's not checking all the right things. Still, that doesn't explain why the inode gets through the prune_icache checks as well. Both patches are here: diff -puN fs/inode.c~ext3-dirty-debug-patch fs/inode.c --- 25/fs/inode.c~ext3-dirty-debug-patch 2004-02-19 18:32:44.000000000 -0800 +++ 25-akpm/fs/inode.c 2004-02-19 18:50:23.000000000 -0800 @@ -20,6 +20,7 @@ #include #include #include +#include /* * This is needed for the following functions: @@ -1019,6 +1020,14 @@ static void generic_forget_inode(struct if (!hlist_unhashed(&inode->i_hash)) { if (!(inode->i_state & (I_DIRTY|I_LOCK))) { + if (!strcmp(inode->i_sb->s_type->name, "ext3")) { + struct ext3_inode_info *ext3_i; + ext3_i = container_of(inode, + struct ext3_inode_info, + vfs_inode); + if (!list_empty(&ext3_i->i_orphan)) + WARN_ON(1); + } list_del(&inode->i_list); list_add(&inode->i_list, &inode_unused); } @@ -1066,7 +1075,7 @@ static void generic_drop_inode(struct in * held, and the drop function is supposed to release * the lock! */ -static inline void iput_final(struct inode *inode) +static void iput_final(struct inode *inode) { struct super_operations *op = inode->i_sb->s_op; void (*drop)(struct inode *) = generic_drop_inode; diff -puN fs/fs-writeback.c~ext3-dirty-debug-patch fs/fs-writeback.c --- 25/fs/fs-writeback.c~ext3-dirty-debug-patch 2004-02-19 19:33:20.000000000 -0800 +++ 25-akpm/fs/fs-writeback.c 2004-02-19 19:33:23.000000000 -0800 @@ -22,6 +22,7 @@ #include #include #include +#include extern struct super_block *blockdev_superblock; @@ -187,6 +188,14 @@ __sync_single_inode(struct inode *inode, list_move(&inode->i_list, &inode_in_use); } else { mapping->dirtied_when = 0; + if (!strcmp(inode->i_sb->s_type->name, "ext3")) { + struct ext3_inode_info *ext3_i; + ext3_i = container_of(inode, + struct ext3_inode_info, + vfs_inode); + if (!list_empty(&ext3_i->i_orphan)) + WARN_ON(1); + } list_move(&inode->i_list, &inode_unused); } } _ fs/ext3/ialloc.c | 0 fs/ext3/inode.c | 0 fs/ext3/namei.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- fs/ext3/super.c | 0 4 files changed, 47 insertions(+), 2 deletions(-) diff -puN fs/ext3/ialloc.c~ext3-i_nlink-check fs/ext3/ialloc.c diff -puN fs/ext3/inode.c~ext3-i_nlink-check fs/ext3/inode.c diff -puN fs/ext3/namei.c~ext3-i_nlink-check fs/ext3/namei.c --- 25/fs/ext3/namei.c~ext3-i_nlink-check 2004-02-19 19:14:12.000000000 -0800 +++ 25-akpm/fs/ext3/namei.c 2004-02-19 19:20:00.000000000 -0800 @@ -1591,13 +1591,23 @@ static int ext3_delete_entry (handle_t * * do not perform it in these functions. We perform it at the call site, * if it is needed. */ -static inline void ext3_inc_count(handle_t *handle, struct inode *inode) +static void ext3_inc_count(handle_t *handle, struct inode *inode) { + if (!down_trylock(&inode->i_sem)) { + printk("%s: i_sem not held\n", __FUNCTION__); + dump_stack(); + up(&inode->i_sem); + } inode->i_nlink++; } -static inline void ext3_dec_count(handle_t *handle, struct inode *inode) +static void ext3_dec_count(handle_t *handle, struct inode *inode) { + if (!down_trylock(&inode->i_sem)) { + printk("%s: i_sem not held\n", __FUNCTION__); + dump_stack(); + up(&inode->i_sem); + } inode->i_nlink--; } @@ -1712,6 +1722,11 @@ static int ext3_mkdir(struct inode * dir inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize; dir_block = ext3_bread (handle, inode, 0, 1, &err); if (!dir_block) { + if (!down_trylock(&inode->i_sem)) { + printk("%s: i_sem not held\n", __FUNCTION__); + dump_stack(); + up(&inode->i_sem); + } inode->i_nlink--; /* is this nlink == 0? */ ext3_mark_inode_dirty(handle, inode); iput (inode); @@ -2014,6 +2029,11 @@ static int ext3_rmdir (struct inode * di ext3_orphan_add(handle, inode); inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; ext3_mark_inode_dirty(handle, inode); + if (!down_trylock(&dir->i_sem)) { + printk("%s: i_sem not held\n", __FUNCTION__); + dump_stack(); + up(&dir->i_sem); + } dir->i_nlink--; ext3_update_dx_flag(dir); ext3_mark_inode_dirty(handle, dir); @@ -2064,6 +2084,11 @@ static int ext3_unlink(struct inode * di dir->i_ctime = dir->i_mtime = CURRENT_TIME; ext3_update_dx_flag(dir); ext3_mark_inode_dirty(handle, dir); + if (!down_trylock(&inode->i_sem)) { + printk("%s: i_sem not held\n", __FUNCTION__); + dump_stack(); + up(&inode->i_sem); + } inode->i_nlink--; if (!inode->i_nlink) ext3_orphan_add(handle, inode); @@ -2272,6 +2297,11 @@ static int ext3_rename (struct inode * o } if (new_inode) { + if (!down_trylock(&new_inode->i_sem)) { + printk("%s: i_sem not held\n", __FUNCTION__); + dump_stack(); + up(&new_inode->i_sem); + } new_inode->i_nlink--; new_inode->i_ctime = CURRENT_TIME; } @@ -2283,10 +2313,25 @@ static int ext3_rename (struct inode * o PARENT_INO(dir_bh->b_data) = le32_to_cpu(new_dir->i_ino); BUFFER_TRACE(dir_bh, "call ext3_journal_dirty_metadata"); ext3_journal_dirty_metadata(handle, dir_bh); + if (!down_trylock(&old_dir->i_sem)) { + printk("%s: i_sem not held\n", __FUNCTION__); + dump_stack(); + up(&old_dir->i_sem); + } old_dir->i_nlink--; if (new_inode) { + if (!down_trylock(&new_inode->i_sem)) { + printk("%s: i_sem not held\n", __FUNCTION__); + dump_stack(); + up(&new_inode->i_sem); + } new_inode->i_nlink--; } else { + if (!down_trylock(&new_dir->i_sem)) { + printk("%s: i_sem not held\n", __FUNCTION__); + dump_stack(); + up(&new_dir->i_sem); + } new_dir->i_nlink++; ext3_update_dx_flag(new_dir); ext3_mark_inode_dirty(handle, new_dir); diff -puN fs/ext3/super.c~ext3-i_nlink-check fs/ext3/super.c _