Skip to content

Commit 5a9b911

Browse files
mjguzikbrauner
authored andcommitted
vfs: partially sanitize i_state zeroing on inode creation
new_inode used to have the following: spin_lock(&inode_lock); inodes_stat.nr_inodes++; list_add(&inode->i_list, &inode_in_use); list_add(&inode->i_sb_list, &sb->s_inodes); inode->i_ino = ++last_ino; inode->i_state = 0; spin_unlock(&inode_lock); over time things disappeared, got moved around or got replaced (global inode lock with a per-inode lock), eventually this got reduced to: spin_lock(&inode->i_lock); inode->i_state = 0; spin_unlock(&inode->i_lock); But the lock acquire here does not synchronize against anyone. Additionally iget5_locked performs i_state = 0 assignment without any locks to begin with, the two combined look confusing at best. It looks like the current state is a leftover which was not cleaned up. Ideally it would be an invariant that i_state == 0 to begin with, but achieving that would require dealing with all filesystem alloc handlers one by one. In the meantime drop the misleading locking and move i_state zeroing to inode_init_always so that others don't need to deal with it by hand. Signed-off-by: Mateusz Guzik <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Jan Kara <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent ddd4cd4 commit 5a9b911

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

fs/inode.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
162162
inode->i_sb = sb;
163163
inode->i_blkbits = sb->s_blocksize_bits;
164164
inode->i_flags = 0;
165+
inode->i_state = 0;
165166
atomic64_set(&inode->i_sequence, 0);
166167
atomic_set(&inode->i_count, 1);
167168
inode->i_op = &empty_iops;
@@ -231,6 +232,7 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
231232

232233
if (unlikely(security_inode_alloc(inode)))
233234
return -ENOMEM;
235+
234236
this_cpu_inc(nr_inodes);
235237

236238
return 0;
@@ -1023,14 +1025,7 @@ EXPORT_SYMBOL(get_next_ino);
10231025
*/
10241026
struct inode *new_inode_pseudo(struct super_block *sb)
10251027
{
1026-
struct inode *inode = alloc_inode(sb);
1027-
1028-
if (inode) {
1029-
spin_lock(&inode->i_lock);
1030-
inode->i_state = 0;
1031-
spin_unlock(&inode->i_lock);
1032-
}
1033-
return inode;
1028+
return alloc_inode(sb);
10341029
}
10351030

10361031
/**
@@ -1254,7 +1249,6 @@ struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
12541249
struct inode *new = alloc_inode(sb);
12551250

12561251
if (new) {
1257-
new->i_state = 0;
12581252
inode = inode_insert5(new, hashval, test, set, data);
12591253
if (unlikely(inode != new))
12601254
destroy_inode(new);
@@ -1297,7 +1291,6 @@ struct inode *iget5_locked_rcu(struct super_block *sb, unsigned long hashval,
12971291

12981292
new = alloc_inode(sb);
12991293
if (new) {
1300-
new->i_state = 0;
13011294
inode = inode_insert5(new, hashval, test, set, data);
13021295
if (unlikely(inode != new))
13031296
destroy_inode(new);

0 commit comments

Comments
 (0)