]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
fs: add icount_read_once() and stop open-coding ->i_count loads
authorMateusz Guzik <mjguzik@gmail.com>
Tue, 21 Apr 2026 18:25:36 +0000 (20:25 +0200)
committerChristian Brauner <brauner@kernel.org>
Mon, 11 May 2026 21:12:28 +0000 (23:12 +0200)
Similarly to inode_state_read_once(), it makes the caller spell out
they acknowledge instability of the returned value.

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
Link: https://patch.msgid.link/20260421182538.1215894-2-mjguzik@gmail.com
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Christian Brauner <brauner@kernel.org>
14 files changed:
arch/powerpc/platforms/cell/spufs/file.c
fs/btrfs/inode.c
fs/ceph/mds_client.c
fs/ext4/ialloc.c
fs/hpfs/inode.c
fs/inode.c
fs/nfs/inode.c
fs/smb/client/inode.c
fs/ubifs/super.c
fs/xfs/xfs_inode.c
fs/xfs/xfs_trace.h
include/linux/fs.h
include/trace/events/filelock.h
security/landlock/fs.c

index 10fa9b844fccd8cae2b2340c2878c0be1887927f..f6de8c1169d5bfc794c6cca535cf29c7fd417610 100644 (file)
@@ -1430,7 +1430,7 @@ static int spufs_mfc_open(struct inode *inode, struct file *file)
        if (ctx->owner != current->mm)
                return -EINVAL;
 
-       if (icount_read(inode) != 1)
+       if (icount_read_once(inode) != 1)
                return -EBUSY;
 
        mutex_lock(&ctx->mapping_lock);
index 40474014c03f1330d80b1cef21b0e752b3258d54..7dc1b8168278292915b03dab40fe1a4f192ee200 100644 (file)
@@ -4745,7 +4745,7 @@ static void btrfs_prune_dentries(struct btrfs_root *root)
 
        inode = btrfs_find_first_inode(root, min_ino);
        while (inode) {
-               if (icount_read(&inode->vfs_inode) > 1)
+               if (icount_read_once(&inode->vfs_inode) > 1)
                        d_prune_aliases(&inode->vfs_inode);
 
                min_ino = btrfs_ino(inode) + 1;
index ed17e0023705e6d4e387ff7e5bd7fb53efdbaeb7..0edb6a25150121266737c09188f3037097fa44f7 100644 (file)
@@ -2267,7 +2267,7 @@ static int trim_caps_cb(struct inode *inode, int mds, void *arg)
                        int count;
                        dput(dentry);
                        d_prune_aliases(inode);
-                       count = icount_read(inode);
+                       count = icount_read_once(inode);
                        if (count == 1)
                                (*remaining)--;
                        doutc(cl, "%p %llx.%llx cap %p pruned, count now %d\n",
index 3fd8f00998521fb4ec1bcab7d718b3b1e1fc5e76..8c80d50875167880a63230937ca85d8581b2d66f 100644 (file)
@@ -252,10 +252,10 @@ void ext4_free_inode(handle_t *handle, struct inode *inode)
                       "nonexistent device\n", __func__, __LINE__);
                return;
        }
-       if (icount_read(inode) > 1) {
+       if (icount_read_once(inode) > 1) {
                ext4_msg(sb, KERN_ERR, "%s:%d: inode #%llu: count=%d",
                         __func__, __LINE__, inode->i_ino,
-                        icount_read(inode));
+                        icount_read_once(inode));
                return;
        }
        if (inode->i_nlink) {
index 0e932cc8be1b28353cffd8e62e26f77a02394edb..1b4fcf760aadcd5eb88c1ffc028bfa1f53c451b8 100644 (file)
@@ -184,7 +184,7 @@ void hpfs_write_inode(struct inode *i)
        struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
        struct inode *parent;
        if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
-       if (hpfs_inode->i_rddir_off && !icount_read(i)) {
+       if (hpfs_inode->i_rddir_off && !icount_read_once(i)) {
                if (*hpfs_inode->i_rddir_off)
                        pr_err("write_inode: some position still there\n");
                kfree(hpfs_inode->i_rddir_off);
index 6a3cbc7dcd28c75723b44d7f69f214e5b35f1443..3338ef2710999936f0c05c3d4dda8be1fda9baca 100644 (file)
@@ -902,7 +902,7 @@ void evict_inodes(struct super_block *sb)
 again:
        spin_lock(&sb->s_inode_list_lock);
        list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
-               if (icount_read(inode))
+               if (icount_read_once(inode))
                        continue;
 
                spin_lock(&inode->i_lock);
@@ -1920,7 +1920,7 @@ static void iput_final(struct inode *inode)
        int drop;
 
        WARN_ON(inode_state_read(inode) & I_NEW);
-       VFS_BUG_ON_INODE(atomic_read(&inode->i_count) != 0, inode);
+       VFS_BUG_ON_INODE(icount_read(inode) != 0, inode);
 
        if (op->drop_inode)
                drop = op->drop_inode(inode);
@@ -1939,7 +1939,7 @@ static void iput_final(struct inode *inode)
         * 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);
+       VFS_BUG_ON_INODE(icount_read(inode) != 0, inode);
 
        if (drop) {
                inode_state_set(inode, I_FREEING);
@@ -1983,7 +1983,7 @@ retry:
         * equal to one, then two CPUs racing to further drop it can both
         * conclude it's fine.
         */
-       VFS_BUG_ON_INODE(atomic_read(&inode->i_count) < 1, inode);
+       VFS_BUG_ON_INODE(icount_read_once(inode) < 1, inode);
 
        if (atomic_add_unless(&inode->i_count, -1, 1))
                return;
@@ -2017,7 +2017,7 @@ EXPORT_SYMBOL(iput);
 void iput_not_last(struct inode *inode)
 {
        VFS_BUG_ON_INODE(inode_state_read_once(inode) & (I_FREEING | I_CLEAR), inode);
-       VFS_BUG_ON_INODE(atomic_read(&inode->i_count) < 2, inode);
+       VFS_BUG_ON_INODE(icount_read_once(inode) < 2, inode);
 
        WARN_ON(atomic_sub_return(1, &inode->i_count) == 0);
 }
@@ -3040,7 +3040,7 @@ void dump_inode(struct inode *inode, const char *reason)
        }
 
        state = inode_state_read_once(inode);
-       count = atomic_read(&inode->i_count);
+       count = icount_read_once(inode);
 
        if (!sb ||
            get_kernel_nofault(s_type, &sb->s_type) || !s_type ||
index e26030e736966b4df4052ee32364b778680e3919..cd9930dee00e26034158d8b08dfad0ca9ed2d4b9 100644 (file)
@@ -608,7 +608,7 @@ nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
                inode->i_sb->s_id,
                (unsigned long long)NFS_FILEID(inode),
                nfs_display_fhandle_hash(fh),
-               icount_read(inode));
+               icount_read_once(inode));
 
 out:
        return inode;
@@ -2255,7 +2255,7 @@ static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
        dfprintk(VFS, "NFS: %s(%s/%llu fh_crc=0x%08x ct=%d info=0x%llx)\n",
                        __func__, inode->i_sb->s_id, inode->i_ino,
                        nfs_display_fhandle_hash(NFS_FH(inode)),
-                       icount_read(inode), fattr->valid);
+                       icount_read_once(inode), fattr->valid);
 
        if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
                /* Only a mounted-on-fileid? Just exit */
index 16a5310155d5ad40a2dc0c7207af9da2295173ad..b3e7db1fd6c34b60768c21b338d2f6d7a24cbbc4 100644 (file)
@@ -2843,7 +2843,7 @@ int cifs_revalidate_dentry_attr(struct dentry *dentry)
        }
 
        cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
-                full_path, inode, icount_read(inode),
+                full_path, inode, icount_read_once(inode),
                 dentry, cifs_get_time(dentry), jiffies);
 
 again:
index 9a77d8b64ffa70f9d5b695fb3d87c22cb223704f..38972786817e7cc5a38b6e22ed5e78652577a6c5 100644 (file)
@@ -358,7 +358,7 @@ static void ubifs_evict_inode(struct inode *inode)
                goto out;
 
        dbg_gen("inode %llu, mode %#x", inode->i_ino, (int)inode->i_mode);
-       ubifs_assert(c, !icount_read(inode));
+       ubifs_assert(c, !icount_read_once(inode));
 
        truncate_inode_pages_final(&inode->i_data);
 
index beaa26ec62da4055a53b85c0b6a5e002b91989bd..4f659eba6ae518f5d0feb27f92a9473ca46a4e6b 100644 (file)
@@ -1046,7 +1046,7 @@ xfs_itruncate_extents_flags(
        int                     error = 0;
 
        xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
-       if (icount_read(VFS_I(ip)))
+       if (icount_read_once(VFS_I(ip)))
                xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL);
        if (whichfork == XFS_DATA_FORK)
                ASSERT(new_size <= XFS_ISIZE(ip));
index 1c098cfc5c00d3cca2210aa1c7fa94be7e8aa72f..f87c738d84b2484b9d041ed6f6c9fd3082ee9b61 100644 (file)
@@ -1158,7 +1158,7 @@ DECLARE_EVENT_CLASS(xfs_iref_class,
        TP_fast_assign(
                __entry->dev = VFS_I(ip)->i_sb->s_dev;
                __entry->ino = ip->i_ino;
-               __entry->count = icount_read(VFS_I(ip));
+               __entry->count = icount_read_once(VFS_I(ip));
                __entry->pincount = atomic_read(&ip->i_pincount);
                __entry->iflags = ip->i_flags;
                __entry->caller_ip = caller_ip;
index 11559c513dfbb2f9bf15a8c7516f031aab3e094f..61ce251ad83ef79a89a3a738d7980304363cf117 100644 (file)
@@ -2218,8 +2218,21 @@ static inline void mark_inode_dirty_sync(struct inode *inode)
        __mark_inode_dirty(inode, I_DIRTY_SYNC);
 }
 
+/*
+ * returns the refcount on the inode. it can change arbitrarily.
+ */
+static inline int icount_read_once(const struct inode *inode)
+{
+       return atomic_read(&inode->i_count);
+}
+
+/*
+ * returns the refcount on the inode. The lock guarantees no new references
+ * are added, but references can be dropped as long as the result is > 0.
+ */
 static inline int icount_read(const struct inode *inode)
 {
+       lockdep_assert_held(&inode->i_lock);
        return atomic_read(&inode->i_count);
 }
 
index 1167748862449ef6ff04c40b568ea8c3bbb08207..c8c8847bb6f6667980d700808fa5048d65289203 100644 (file)
@@ -190,7 +190,7 @@ TRACE_EVENT(generic_add_lease,
                __entry->i_ino = inode->i_ino;
                __entry->wcount = atomic_read(&inode->i_writecount);
                __entry->rcount = atomic_read(&inode->i_readcount);
-               __entry->icount = icount_read(inode);
+               __entry->icount = icount_read_once(inode);
                __entry->owner = fl->c.flc_owner;
                __entry->flags = fl->c.flc_flags;
                __entry->type = fl->c.flc_type;
index c1ecfe239032618f1983cb2a3f6072c6e334cc22..32d560f12dbd3819bceff77409b35fbc34f4e4d6 100644 (file)
@@ -1278,7 +1278,7 @@ static void hook_sb_delete(struct super_block *const sb)
                struct landlock_object *object;
 
                /* Only handles referenced inodes. */
-               if (!icount_read(inode))
+               if (!icount_read_once(inode))
                        continue;
 
                /*