Skip to contentSkip to conversation

fs: assert on ->i_count in iput_final()

PATCH Applied Mateusz Guzik 3 participants 7 messages Oct 1, 2025 – Oct 6, 2025 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: assert on ->i_count in iput_final()

Notably make sure the count is 0 after the return from ->drop_inode(), provided we are going to drop.

Inspired by suspicious games played by f2fs.

---

boots on ext4 without splats

 fs/inode.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/inode.c +7 -0
index ec9339024ac3..fa82cb810af4 100644
@@ -1879,6 +1879,7 @@ static void iput_final(struct inode *inode)
 	int drop;
 
 	WARN_ON(inode->i_state & I_NEW);
+	VFS_BUG_ON_INODE(atomic_read(&inode->i_count) != 0, inode);
 
 	if (op->drop_inode)
 		drop = op->drop_inode(inode);
@@ -1893,6 +1894,12 @@ static void iput_final(struct inode *inode)
 		return;
 	}
 
+	/*
+	 * Re-check ->i_count in case the ->drop_inode() hooks played games.
+	 * Note we only execute this if the verdict was to drop the inode.
+	 */
+	VFS_BUG_ON_INODE(atomic_read(&inode->i_count) != 0, inode);
+
 	state = inode->i_state;
 	if (!drop) {
 		WRITE_ONCE(inode->i_state, state | I_WILL_FREE);
Signature · 1 line
2.34.1
reviewer
#1 lore raw
Quoted 4 lines · Mateusz Guzik

Notably make sure the count is 0 after the return from ->drop_inode(), provided we are going to drop.

Inspired by suspicious games played by f2fs.

Whoo, those are indeed interesting.

Quoted 13 lines

---

boots on ext4 without splats

 fs/inode.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/inode.c +0 -0
index ec9339024ac3..fa82cb810af4 100644

fs/inode.c static void iput_final(struct inode *inode) · line 1882

Quoted 4 lines
fs/inode.c +1 -0
@@ -1879,6 +1879,7 @@ static void iput_final(struct inode *inode)
 	int drop;
 
 	WARN_ON(inode->i_state & I_NEW);
+	VFS_BUG_ON_INODE(atomic_read(&inode->i_count) != 0, inode);

This seems pointless given when iput_final() is called...

fs/inode.c static void iput_final(struct inode *inode) · line 1902

Quoted 12 lines
fs/inode.c +6 -0
 	if (op->drop_inode)
 		drop = op->drop_inode(inode);
@@ -1893,6 +1894,12 @@ static void iput_final(struct inode *inode)
 		return;
 	}
 
+	/*
+	 * Re-check ->i_count in case the ->drop_inode() hooks played games.
+	 * Note we only execute this if the verdict was to drop the inode.
+	 */
+	VFS_BUG_ON_INODE(atomic_read(&inode->i_count) != 0, inode);
+

I'm not sure this can catch much but OK...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#2 lore raw
Quoted 7 lines · Jan Kara
Quoted 7 lines
fs/inode.c +0 -0
index ec9339024ac3..fa82cb810af4 100644
@@ -1879,6 +1879,7 @@ static void iput_final(struct inode *inode)
      int drop;
 
Quoted 5 lines

fs/inode.c static void iput_final(struct inode *inode) · line 1882

Quoted 2 lines
fs/inode.c +1 -0
      WARN_ON(inode->i_state & I_NEW);
+     VFS_BUG_ON_INODE(atomic_read(&inode->i_count) != 0, inode);

This seems pointless given when iput_final() is called...

This and the other check explicitly "wrap" the ->drop_inode call.

Quoted 6 lines
Quoted 6 lines
fs/inode.c +0 -0
      if (op->drop_inode)
              drop = op->drop_inode(inode);
@@ -1893,6 +1894,12 @@ static void iput_final(struct inode *inode)
              return;
      }
 
Quoted 9 lines

fs/inode.c static void iput_final(struct inode *inode) · line 1902

Quoted 6 lines
fs/inode.c +6 -0
+     /*
+      * Re-check ->i_count in case the ->drop_inode() hooks played games.
+      * Note we only execute this if the verdict was to drop the inode.
+      */
+     VFS_BUG_ON_INODE(atomic_read(&inode->i_count) != 0, inode);
+

I'm not sure this can catch much but OK...

It can catch drop routines which bumped the ref but did not release it, or which indicated to continue with drop while someone else snatched the reference.

Preferaby the APIs would prevent that in the first place, but there is quite a bit of shit-shoveling before that happens.

reviewer
#3 lore raw
Quoted 8 lines · Mateusz Guzik
Quoted 7 lines · Jan Kara
Quoted 7 lines
fs/inode.c +0 -0
index ec9339024ac3..fa82cb810af4 100644
@@ -1879,6 +1879,7 @@ static void iput_final(struct inode *inode)
      int drop;
 
Quoted 7 lines
Quoted 5 lines

fs/inode.c static void iput_final(struct inode *inode) · line 1882

Quoted 2 lines
fs/inode.c +1 -0
      WARN_ON(inode->i_state & I_NEW);
+     VFS_BUG_ON_INODE(atomic_read(&inode->i_count) != 0, inode);

This seems pointless given when iput_final() is called...

This and the other check explicitly "wrap" the ->drop_inode call.

I understand but given iput() has just decremented i_count to 0 before calling iput_final() this beginning of the "wrap" looks pretty pointless to me.

Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
fs/inode.c +0 -0
      if (op->drop_inode)
              drop = op->drop_inode(inode);
@@ -1893,6 +1894,12 @@ static void iput_final(struct inode *inode)
              return;
      }
 
Quoted 9 lines

fs/inode.c static void iput_final(struct inode *inode) · line 1902

Quoted 6 lines
fs/inode.c +6 -0
+     /*
+      * Re-check ->i_count in case the ->drop_inode() hooks played games.
+      * Note we only execute this if the verdict was to drop the inode.
+      */
+     VFS_BUG_ON_INODE(atomic_read(&inode->i_count) != 0, inode);
+

I'm not sure this can catch much but OK...

Quoted 3 lines

It can catch drop routines which bumped the ref but did not release it, or which indicated to continue with drop while someone else snatched the reference.

Right.

Quoted 2 lines

Preferaby the APIs would prevent that in the first place, but there is quite a bit of shit-shoveling before that happens.

Agreed.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw
Quoted 16 lines · Jan Kara
Quoted 14 lines · Mateusz Guzik
Quoted 12 lines · Jan Kara

fs/inode.c static void iput_final(struct inode *inode) · line 1882

Quoted 9 lines
fs/inode.c +1 -0
index ec9339024ac3..fa82cb810af4 100644
@@ -1879,6 +1879,7 @@ static void iput_final(struct inode *inode)
      int drop;
 
      WARN_ON(inode->i_state & I_NEW);
+     VFS_BUG_ON_INODE(atomic_read(&inode->i_count) != 0, inode);

This seems pointless given when iput_final() is called...

Quoted 6 lines
Quoted 1 line

This and the other check explicitly "wrap" the ->drop_inode call.

I understand but given iput() has just decremented i_count to 0 before calling iput_final() this beginning of the "wrap" looks pretty pointless to me.

To my understanding you are not NAKing the patch, are merely not particularly fond of it. ;)

Given that these asserts don't show up in production kernels, the layer should be moving towards always spelling out all assumptions at the entry point. Worst case does not hurt in production anyway, best case it will catch something.

For iput_final specifically, at the moment there is only one consumer so this indeed may look overzealous.

But for the sake of argument suppose someone noticed that dentry_unlink_inode() performs:

        spin_unlock(&inode->i_lock);
        if (!inode->i_nlink)
                fsnotify_inoderemove(inode);
        if (dentry->d_op && dentry->d_op->d_iput)
                dentry->d_op->d_iput(dentry, inode);
        else
                iput(inode);

... and that with some minor rototoiling the inode lock can survive both fsnotify and custom d_iput in the common case. Should that happen, iput_locked() could be added to shave off a lock trip in the common case of whacking the inode. But then there is 2 consumers of iput_final. etc.

reviewer
#5 lore raw
Quoted 19 lines · Mateusz Guzik
Quoted 18 lines · Jan Kara
Quoted 15 lines · Mateusz Guzik
Quoted 7 lines · Jan Kara
Quoted 7 lines
fs/inode.c +0 -0
index ec9339024ac3..fa82cb810af4 100644
@@ -1879,6 +1879,7 @@ static void iput_final(struct inode *inode)
      int drop;
 
Quoted 5 lines

fs/inode.c static void iput_final(struct inode *inode) · line 1882

Quoted 2 lines
fs/inode.c +1 -0
      WARN_ON(inode->i_state & I_NEW);
+     VFS_BUG_ON_INODE(atomic_read(&inode->i_count) != 0, inode);

This seems pointless given when iput_final() is called...

This and the other check explicitly "wrap" the ->drop_inode call.

Quoted 7 lines
Quoted 4 lines

I understand but given iput() has just decremented i_count to 0 before calling iput_final() this beginning of the "wrap" looks pretty pointless to me.

To my understanding you are not NAKing the patch, are merely not particularly fond of it. ;)

Yes, it isn't annoying me enough to nak it but I couldn't resist complaining :)

Quoted 4 lines

Given that these asserts don't show up in production kernels, the layer should be moving towards always spelling out all assumptions at the entry point. Worst case does not hurt in production anyway, best case it will catch something.

Well, I think that when we get too many asserts, the code is harder to read.

Quoted 13 lines

For iput_final specifically, at the moment there is only one consumer so this indeed may look overzealous.

But for the sake of argument suppose someone noticed that dentry_unlink_inode() performs:

        spin_unlock(&inode->i_lock);
        if (!inode->i_nlink)
                fsnotify_inoderemove(inode);
        if (dentry->d_op && dentry->d_op->d_iput)
                dentry->d_op->d_iput(dentry, inode);
        else
                iput(inode);
Quoted 5 lines

... and that with some minor rototoiling the inode lock can survive both fsnotify and custom d_iput in the common case. Should that happen, iput_locked() could be added to shave off a lock trip in the common case of whacking the inode. But then there is 2 consumers of iput_final. etc.

Right. And when we grow second iput_final() caller, I'd withdraw my complaint about pointless assert ;).

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#6 lore raw
In reply to Mateusz Guzik
Quoted 6 lines · Mateusz Guzik

Notably make sure the count is 0 after the return from ->drop_inode(), provided we are going to drop.

Inspired by suspicious games played by f2fs.

Applied to the vfs-6.19.inode branch of the vfs/vfs.git tree. Patches in the vfs-6.19.inode branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-6.19.inode

[1/1] fs: assert on ->i_count in iput_final()

      https://git.kernel.org/vfs/vfs/c/655c4a4f00fc