lsm: fs: Use inode_free_callback to free i_security in RCU callback
Subsystems (6)
RFC v2 1/1 lsm: fs: Use inode_free_callback to free i_security in RCU callback
inode->i_security needes to be freed from RCU callback. A rcu_head was added to i_security to call the RCU callback. However, since struct inode already has i_rcu, the extra rcu_head is wasteful. Specifically, when any LSM uses i_security, a rcu_head (two pointers) is allocated for each inode.
Rename i_callback to inode_free_callback and call security_inode_free_rcu from it to free i_security so that a rcu_head is saved for each inode. Special care are needed for file systems that provide a destroy_inode() callback, but not a free_inode() callback. Specifically, the following logic are added to handle such cases:
- XFS recycles inode after destroy_inode. The inodes are freed from recycle logic. Let xfs_inode_free_callback() call inode_free_callback.
- Let pipe free inode from a RCU callback.
- Let btrfs-test free inode from a RCU callback.
---
Changes v1 => v2:
1. Wrap security_inode_free_rcu inside inode_free_callback so that the interface is cleaner.
RFC v1: https://lore.kernel.org/linux-fsdevel/[email protected]/
--- Documentation/filesystems/vfs.rst | 5 +++- fs/btrfs/fs.h | 1 + fs/btrfs/inode.c | 4 +++ fs/btrfs/tests/btrfs-tests.c | 1 + fs/inode.c | 20 +++++++++---- fs/pipe.c | 1 - fs/xfs/xfs_icache.c | 1 + include/linux/fs.h | 2 ++ include/linux/security.h | 4 +++ security/security.c | 48 +++++++++++++++++++------------ 10 files changed, 60 insertions(+), 27 deletions(-)
Documentation/filesystems/vfs.rst +4 -1
@@ -306,7 +306,10 @@ or bottom half). ``free_inode`` this method is called from RCU callback. If you use call_rcu() in ->destroy_inode to free 'struct inode' memory, then it's - better to release memory in this method. + better to release memory in this method. Some filesystems may + decide not to provide a free_inode() method, but to free the + inode through a different code path. In this case, the filesystem + should call inode_free_callback() before freeing the inode. ``dirty_inode`` this method is called by the VFS when an inode is marked dirty.
fs/btrfs/fs.h +1 -0
@@ -1068,6 +1068,7 @@ static inline int btrfs_is_testing(const struct btrfs_fs_info *fs_info) }void btrfs_test_destroy_inode (struct inode *inode); +void btrfs_test_free_inode (struct inode *inode);#else
fs/btrfs/inode.c +4 -0
@@ -7707,6 +7707,10 @@ void btrfs_test_destroy_inode(struct inode *inode) {btrfs_drop_extent_map_range (BTRFS_I (inode),0 , (u64)-1 , false);kfree (BTRFS_I (inode)->file_extent_tree); +} + +void btrfs_test_free_inode (struct inode *inode) +{kmem_cache_free (btrfs_inode_cachep,BTRFS_I (inode)); }#endif
fs/btrfs/tests/btrfs-tests.c +1 -0
@@ -35,6 +35,7 @@ const char *test_error[] = {static const struct super_operations btrfs_test_super_ops = { .alloc_inode = btrfs_alloc_inode, .destroy_inode = btrfs_test_destroy_inode, + .free_inode = btrfs_test_free_inode, };
fs/inode.c +14 -6
@@ -318,13 +318,13 @@ void free_inode_nonrcu(struct inode *inode) }EXPORT_SYMBOL (free_inode_nonrcu); -static void i_callback (struct rcu_head *head) +void inode_free_callback (struct rcu_head *head) { struct inode *inode = container_of(head, struct inode, i_rcu); + +security_inode_free_rcu (inode);if (inode->free_inode) inode->free_inode (inode); -else -free_inode_nonrcu (inode); }static struct inode *alloc_inode (struct super_block *sb)
@@ -346,8 +346,11 @@ static struct inode *alloc_inode(struct super_block *sb)if (!ops->free_inode)return NULL ; } - inode->free_inode = ops->free_inode; -i_callback (&inode->i_rcu); +if (ops->free_inode) + inode->free_inode = ops->free_inode; +else + inode->free_inode = free_inode_nonrcu; +inode_free_callback (&inode->i_rcu);return NULL ; }
@@ -387,8 +390,13 @@ static void destroy_inode(struct inode *inode)if (!ops->free_inode)return ; } +if (ops->free_inode) + inode->free_inode = ops->free_inode; +else + inode->free_inode = free_inode_nonrcu; + inode->free_inode = ops->free_inode; -call_rcu (&inode->i_rcu, i_callback); +call_rcu (&inode->i_rcu, inode_free_callback); }/**
fs/pipe.c +0 -1
@@ -1422,7 +1422,6 @@ long pipe_fcntl(struct file *file, unsigned int cmd, unsigned int arg) }static const struct super_operations pipefs_ops = { - .destroy_inode = free_inode_nonrcu, .statfs = simple_statfs, };
fs/xfs/xfs_icache.c +1 -0
@@ -162,6 +162,7 @@ xfs_inode_free_callback( ip->i_itemp =NULL ; } +inode_free_callback (&inode->i_rcu);kmem_cache_free (xfs_inode_cache, ip); }
include/linux/fs.h +2 -0
@@ -3810,4 +3810,6 @@ static inline bool vfs_empty_path(int dfd, const char __user *path)int generic_atomic_write_valid (struct kiocb *iocb,struct iov_iter *iter); +void inode_free_callback (struct rcu_head *head); +#endif /* _LINUX_FS_H */
include/linux/security.h +4 -0
@@ -400,6 +400,7 @@ int security_path_notify(const struct path *path, u64 mask, unsigned int obj_type);int security_inode_alloc (struct inode *inode,gfp_t gfp);void security_inode_free (struct inode *inode); +void security_inode_free_rcu (struct inode *inode);int security_inode_init_security (struct inode *inode,struct inode *dir,const struct qstr *qstr,initxattrs initxattrs,void *fs_data);
@@ -860,6 +861,9 @@ static inline int security_inode_alloc(struct inode *inode, gfp_t gfp)static inline void security_inode_free (struct inode *inode) { } +static inline void security_inode_free_rcu (struct inode *inode) +{ } + static inline int security_dentry_init_security(struct dentry *dentry, int mode, const struct qstr *name,
security/security.c +29 -19
78 lines not on this page · raw message
Signature · 1 line
2.43.5
Quoted 6 lines · Song Liu
inode->i_securityneedes to be freed from RCU callback. Arcu_headwas added toi_securityto call the RCU callback. However, since struct inode already hasi_rcu, the extrarcu_headis wasteful. Specifically, when any LSM usesi_security, arcu_head(two pointers) is allocated for each inode.
Quoted 8 lines
Rename
i_callbacktoinode_free_callbackand callsecurity_inode_free_rcufrom it to freei_securityso that arcu_headis saved for each inode. Special care are needed for file systems that provide adestroy_inode()callback, but not afree_inode()callback. Specifically, the following logic are added to handle such cases:- XFS recycles inode after
destroy_inode. The inodes are freed from recycle logic. Letxfs_inode_free_callback()callinode_free_callback.
NACK. That's a massive layering violation.
LSMs are VFS layer functionality. They *must* be removed from the VFS inode before ->destroy_inode() is called. If a filesystem supplies ->destroy_inode(), then it -owns- the inode exclusively from that point onwards. All VFS and 3rd party stuff hanging off the inode must be removed from the inode before ->destroy_inode() is called.
Them's the rules, and hacking around LSM object allocation/freeing to try to handle how filesystems manage inodes -underneath- the VFS is just asking for problems. LSM object management needs to be handled entirely by the generic LSM VFS hooks, not tightly coupled to VFS and/or low level filesystem inode lifecycle management.
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Hi Dave,
Thanks for your comments!
Quoted 18 lines · Dave Chinner
Quoted 6 lines · Song Liu
inode->i_securityneedes to be freed from RCU callback. Arcu_headwas added toi_securityto call the RCU callback. However, since struct inode already hasi_rcu, the extrarcu_headis wasteful. Specifically, when any LSM usesi_security, arcu_head(two pointers) is allocated for each inode.Quoted 8 lines
Rename
i_callbacktoinode_free_callbackand callsecurity_inode_free_rcufrom it to freei_securityso that arcu_headis saved for each inode. Special care are needed for file systems that provide adestroy_inode()callback, but not afree_inode()callback. Specifically, the following logic are added to handle such cases:- XFS recycles inode after
destroy_inode. The inodes are freed from recycle logic. Letxfs_inode_free_callback()callinode_free_callback.
Quoted 8 lines
NACK. That's a massive layering violation.
LSMs are VFS layer functionality. They *must* be removed from the VFS inode before ->destroy_inode() is called. If a filesystem supplies ->destroy_inode(), then it -owns- the inode exclusively from that point onwards. All VFS and 3rd party stuff hanging off the inode must be removed from the inode before ->destroy_inode() is called.
To be honest, I am not sure this rule is true. Most filesystems provide a free_inode() callback:
$ grep "\.free_inode.*=" fs/*/*.c | wc
54 221 2808For all these cases, VFS layer is still in charge of freeing the inode after a RCU grace period. From vfs.rst:
``free_inode``
this method is called from RCU callback. If you use call_rcu() in ->destroy_inode to free 'struct inode' memory, then it's better to release memory in this method.
Quoted 5 lines
Them's the rules, and hacking around LSM object allocation/freeing to try to handle how filesystems manage inodes -underneath- the VFS is just asking for problems. LSM object management needs to be handled entirely by the generic LSM VFS hooks, not tightly coupled to VFS and/or low level filesystem inode lifecycle management.
Unfortunately, many filesystems already call various security_* hooks directly:
$ grep "include <linux/security.h>" fs/*/*.c -rI | wc
46 92 2144
(46 files in various filesystems call into the security layer.)Therefore, I don't feel this change alone is massive layering violation. Instead, it is probably making existing layering violation a bit worse. I personally think it is not making things too worse. On the other hand, saving a rcu_head per inode is non-trivial savings.
It worth note that many systems allocate memory on i_security, even the user is not using any LSM. This is because some LSMs can only be disabled at compile time. Distro kernels are more likely to enable them to fit various users' needs. Therefore, the i_security->rcu_head "tax" is not somebody else's problem. We probably just don't know we are paying it.
That being said, I am not too attached to this change. If folks think allocating an extra rcu_head per inode is the better trade-off. I am OK with that answer. I put "RFC" there for comments.
I hope this makes sense.
Thanks again for your comments.
Song