Skip to contentSkip to conversation

Backport inode_hash race fix

PATCH In review Livio Baldini Soares 4 participants 7 messages Aug 14, 2003 – Nov 4, 2003 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Backport inode_hash race fix

Hello,

After trying to "get around" the inode_hash races when removing and
iget()ing the same inode, my code got really ugly, and I got fed up. So yesterday I got Neil's 2.5 patch and backportted it to 2.4.22-rc2.

The patch is very similar to Neil's, except for one (very important to
me) case. Neil's patch only covered the removal of inodes in
generic_delete_inode() (which in 2.4 is the case where i_nlink is zero in iput()). But, as I described in a previous post:
http://marc.theaimsgroup.com/?l=linux-fsdevel&m=105547595519745&w=2

, I frequently get busted in prune_icache(). In Neil's patch prune_icache
is not covered. In my opinion, this case (in prune_icache()), has to be fixed in 2.6 too. Depending on your comments, I may make a patch for 2.6 later.

Please comment if you can, so that I may send this to Marcelo then
2.4.23-pre opens (which should be soon, I think).

best regards,

Signature · 121 lines
  Livio B. Soares

--- inode-orig.c	Thu Aug 14 11:30:47 2003
+++ inode.c	Thu Aug 14 11:57:51 2003
@@ -206,7 +206,8 @@ void __mark_inode_dirty(struct inode *in
 	if ((inode->i_state & flags) != flags) {
 		inode->i_state |= flags;
 		/* Only add valid (ie hashed) inodes to the dirty list */
-		if (!(inode->i_state & I_LOCK) && !list_empty(&inode->i_hash)) {
+		if (!(inode->i_state & (I_LOCK|I_FREEING|I_CLEAR)) &&
+		    !list_empty(&inode->i_hash)) {
 			list_del(&inode->i_list);
 			list_add(&inode->i_list, &sb->s_dirty);
 		}
@@ -235,6 +236,30 @@ static inline void wait_on_inode(struct 
 		__wait_on_inode(inode);
 }
 
+/*
+ * If we try to find an inode in the inode hash while it is being deleted, we
+ * have to wait until the filesystem completes its deletion before reporting
+ * that it isn't found.  This is because iget will immediately call
+ * ->read_inode, and we want to be sure that evidence of the deletion is found
+ * by ->read_inode.
+ *
+ * This call might return early if an inode which shares the waitq is woken up.
+ * This is most easily handled by the caller which will loop around again
+ * looking for the inode.
+ *
+ * This is called with inode_lock held.
+ */
+static void __wait_on_freeing_inode(struct inode *inode)
+{
+        DECLARE_WAITQUEUE(wait, current);
+
+        add_wait_queue(&inode->i_wait, &wait);
+        set_current_state(TASK_UNINTERRUPTIBLE);
+        spin_unlock(&inode_lock);
+        schedule();
+        remove_wait_queue(&inode->i_wait, &wait);
+        spin_lock(&inode_lock);
+}
 
 static inline void write_inode(struct inode *inode, int sync)
 {
@@ -596,6 +621,11 @@ static void dispose_list(struct list_hea
 		if (inode->i_data.nrpages)
 			truncate_inode_pages(&inode->i_data, 0);
 		clear_inode(inode);
+		spin_lock(&inode_lock);
+		list_del(&inode->i_hash);
+		INIT_LIST_HEAD(&inode->i_hash);
+		spin_unlock(&inode_lock);
+		wake_up(&inode->i_wait);
 		destroy_inode(inode);
 		inodes_stat.nr_inodes--;
 	}
@@ -704,6 +734,14 @@ int invalidate_device(kdev_t dev, int do
  *
  * We don't expect to have to call this very often.
  *
+ * We leave the inode in the inode hash table until *after* 
+ * the filesystem's ->delete_inode (in dispose_list) completes.
+ * This ensures that an iget (such as nfsd might instigate) will 
+ * always find up-to-date information either in the hash or on disk.
+ *
+ * I_FREEING is set so that no-one will take a new reference
+ * to the inode while it is being deleted.
+ *
  * N.B. The spinlock is released during the call to
  *      dispose_list.
  */
@@ -736,8 +774,6 @@ void prune_icache(int goal)
 		if (atomic_read(&inode->i_count))
 			continue;
 		list_del(tmp);
-		list_del(&inode->i_hash);
-		INIT_LIST_HEAD(&inode->i_hash);
 		list_add(tmp, freeable);
 		inode->i_state |= I_FREEING;
 		count++;
@@ -790,6 +826,7 @@ static struct inode * find_inode(struct 
 	struct list_head *tmp;
 	struct inode * inode;
 
+repeat:
 	tmp = head;
 	for (;;) {
 		tmp = tmp->next;
@@ -803,6 +840,10 @@ static struct inode * find_inode(struct 
 			continue;
 		if (find_actor && !find_actor(inode, ino, opaque))
 			continue;
+		if (inode->i_state & (I_FREEING|I_CLEAR)) {
+			__wait_on_freeing_inode(inode);
+			goto repeat;
+		}
 		break;
 	}
 	return inode;
@@ -1042,8 +1083,6 @@ void iput(struct inode *inode)
 			return;
 
 		if (!inode->i_nlink) {
-			list_del(&inode->i_hash);
-			INIT_LIST_HEAD(&inode->i_hash);
 			list_del(&inode->i_list);
 			INIT_LIST_HEAD(&inode->i_list);
 			inode->i_state|=I_FREEING;
@@ -1061,6 +1100,11 @@ void iput(struct inode *inode)
 				delete(inode);
 			} else
 				clear_inode(inode);
+			spin_lock(&inode_lock);
+			list_del(&inode->i_hash);
+			INIT_LIST_HEAD(&inode->i_hash);
+			spin_unlock(&inode_lock);
+			wake_up(&inode->i_wait);
 			if (inode->i_state != I_CLEAR)
 				BUG();
 		} else {
#1 lore raw
Quoted 5 lines · Livio Baldini Soares

Hello,

After trying to "get around" the inode_hash races when removing and
iget()ing the same inode, my code got really ugly, and I got fed up. So yesterday I got Neil's 2.5 patch and backportted it to 2.4.22-rc2.

If I attempt to work round the same bug (my read_inode() being called for an inode which is already in core and for which clear_inode() hasn't yet been called), my code gets similarly ugly. I'd very much like to see this fixed in the VFS too.

Quoted 10 lines

The patch is very similar to Neil's, except for one (very important to
me) case. Neil's patch only covered the removal of inodes in
generic_delete_inode() (which in 2.4 is the case where i_nlink is zero in iput()). But, as I described in a previous post:
http://marc.theaimsgroup.com/?l=linux-fsdevel&m=105547595519745&w=2

, I frequently get busted in prune_icache(). In Neil's patch prune_icache
is not covered. In my opinion, this case (in prune_icache()), has to be fixed in 2.6 too. Depending on your comments, I may make a patch for 2.6 later.

Please do.

Signature · 1 line
dwmw2
#2 lore raw
Quoted 11 lines · On Wednesday October 29, [email protected] wrote:
Quoted 5 lines · Livio Baldini Soares

Hello,

After trying to "get around" the inode_hash races when removing and
iget()ing the same inode, my code got really ugly, and I got fed up. So yesterday I got Neil's 2.5 patch and backportted it to 2.4.22-rc2.

If I attempt to work round the same bug (my read_inode() being called for an inode which is already in core and for which clear_inode() hasn't yet been called), my code gets similarly ugly. I'd very much like to see this fixed in the VFS too.

Probably too late for 2.4.23, but worth trying early in 2.4.24-pre. I'll try to remember to push it.

Quoted 11 lines
Quoted 11 lines

The patch is very similar to Neil's, except for one (very important to
me) case. Neil's patch only covered the removal of inodes in
generic_delete_inode() (which in 2.4 is the case where i_nlink is zero in iput()). But, as I described in a previous post:
http://marc.theaimsgroup.com/?l=linux-fsdevel&m=105547595519745&w=2

, I frequently get busted in prune_icache(). In Neil's patch prune_icache
is not covered. In my opinion, this case (in prune_icache()), has to be fixed in 2.6 too. Depending on your comments, I may make a patch for 2.6 later.

Something like the following?
NeilBrown

======================================================
Only unhash an inode *after* it has been cleared

If we unhash the inode earlier, and new inode may be allocated before an old copy of the same inode is properly cleared, and races can ensue in some filesystem.

----------- Diffstat output ------------

 ./fs/inode.c |   11 ++++-------
 1 files changed, 4 insertions(+), 7 deletions(-)
fs/inode.c renamed from fs/inode.c~current~ +4 -7
diff ./fs/inode.c~current~ ./fs/inode.c
@@ -274,6 +274,7 @@ static void dispose_list(struct list_hea
 		if (inode->i_data.nrpages)
 			truncate_inode_pages(&inode->i_data, 0);
 		clear_inode(inode);
+		remove_inode_hash(inode);
 		destroy_inode(inode);
 		nr_disposed++;
 	}
@@ -303,7 +304,6 @@ static int invalidate_list(struct list_h
 			continue;
 		invalidate_inode_buffers(inode);
 		if (!atomic_read(&inode->i_count)) {
-			hlist_del_init(&inode->i_hash);
 			list_del(&inode->i_list);
 			list_add(&inode->i_list, dispose);
 			inode->i_state |= I_FREEING;
@@ -442,7 +442,6 @@ static void prune_icache(int nr_to_scan)
 			if (!can_unuse(inode))
 				continue;
 		}
-		hlist_del_init(&inode->i_hash);
 		list_move(&inode->i_list, &freeable);
 		inode->i_state |= I_FREEING;
 		nr_pruned++;
@@ -963,6 +962,7 @@ void remove_inode_hash(struct inode *ino
 	spin_lock(&inode_lock);
 	hlist_del_init(&inode->i_hash);
 	spin_unlock(&inode_lock);
+	wake_up_inode(inode);
 }
 
 EXPORT_SYMBOL(remove_inode_hash);
@@ -1001,10 +1001,7 @@ void generic_delete_inode(struct inode *
 		delete(inode);
 	} else
 		clear_inode(inode);
-	spin_lock(&inode_lock);
-	hlist_del_init(&inode->i_hash);
-	spin_unlock(&inode_lock);
-	wake_up_inode(inode);
+	remove_inode_hash(inode);
 	if (inode->i_state != I_CLEAR)
 		BUG();
 	destroy_inode(inode);
@@ -1028,7 +1025,6 @@ static void generic_forget_inode(struct 
 		write_inode_now(inode, 1);
 		spin_lock(&inode_lock);
 		inodes_stat.nr_unused--;
-		hlist_del_init(&inode->i_hash);
 	}
 	list_del_init(&inode->i_list);
 	inode->i_state|=I_FREEING;
@@ -1037,6 +1033,7 @@ static void generic_forget_inode(struct 
 	if (inode->i_data.nrpages)
 		truncate_inode_pages(&inode->i_data, 0);
 	clear_inode(inode);
+	remove_inode_hash(inode);
 	destroy_inode(inode);
 }
 
#3 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

" " == Neil Brown <[email protected]> writes:

> unhash an inode *after* it has been cleared

> If we unhash the inode earlier, and new inode may be allocated > before an old copy of the same inode is properly cleared, and > races can ensue in some filesystem.

Huh? Aren't all inodes that call dispose_list() supposed to be in the I_FREEING state?

Surely the correct fix here should be to provide a proper test of the flags in inode->i_state rather than some ad-hoc reordering of inode unhashing vs. freeing...

Cheers,
Trond

#4 lore raw
Quoted 9 lines · On October 30, [email protected] wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

" " == Neil Brown <[email protected]> writes:

> unhash an inode *after* it has been cleared

> If we unhash the inode earlier, and new inode may be allocated > before an old copy of the same inode is properly cleared, and > races can ensue in some filesystem.

Quoted 6 lines

Huh? Aren't all inodes that call dispose_list() supposed to be in the I_FREEING state?

Surely the correct fix here should be to provide a proper test of the flags in inode->i_state rather than some ad-hoc reordering of inode unhashing vs. freeing...

The thing is that inodes hang around in the I_FREEING state for a while. If a lookup happens for an inode just after it has entered I_FREEING then, with the current code, the lookup will fail and quite probably a new inode will be allocated. So now there will be two "struct inode"s referring to the same inode. The "first" one may not have got up to being cleared with ->clear_inode before the "second" one gets filled in with a call to ->read_inode. This leaves the filesystem having to deal with the possibility of there being two "struct inode" for the one inode, which can apparently be awkward (I haven't looked at the code, but I believe it).

With the suggested patch, an inode remains in the hash table until after clear_inode, and is removed just before destroy_inode. The 2.6 inode lookup code already copes with finding an I_FREEING inode and will wait for it to complete it's passage through the I_FREEING state before returning a lookup failure (2.4 doesn't have that code yet, and needs it).

I don't think this issue can be addressed by a "proper test of the flags in inode->i_state", but I'm willing to be educated.

NeilBrown

#5 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

" " == Neil Brown <[email protected]> writes:

> With the suggested patch, an inode remains in the hash table > until after clear_inode, and is removed just before > destroy_inode. The 2.6 inode lookup code already copes with > finding an I_FREEING inode and will wait for it to complete > it's passage through the I_FREEING state before returning a > lookup failure (2.4 doesn't have that code yet, and needs it).

> I don't think this issue can be addressed by a "proper test of > the flags in inode->i_state", but I'm willing to be educated.

In the 2.6 version of generic_forget_inode(), a test is done on inode->i_state for whether or not the inode is dirty. If it is, a synchronous call to write_inode_now() is made before the inode is unhashed.

Why should that sort of test be insufficient to prevent races? What processes are dirtying the inode after we set I_FREEING?

Cheers,
Trond

#6 lore raw
Quoted 2 lines · Trond Myklebust

Why should that sort of test be insufficient to prevent races? What processes are dirtying the inode after we set I_FREEING?

That wasn't the problem. The problem is that some file systems have ancillary data structures or locking rules which make it problematic for read_inode(), read_inode(), clear_inode() to happen in _that_ order for the same inode.

Signature · 1 line
dwmw2