lsm: fs: Use i_callback to free i_security in RCU callback
Subsystems (6)
RFC 1/1 lsm: fs: Use i_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.
Add security_inode_free_rcu() to i_callback 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() and xfs_inode_alloc() call security_inode_free_rcu() before freeing the inode.
- Let pipe free inode from a RCU callback.
- Let btrfs-test free inode from a RCU callback.
--- Documentation/filesystems/vfs.rst | 8 ++++- fs/btrfs/fs.h | 1 + fs/btrfs/inode.c | 4 +++ fs/btrfs/tests/btrfs-tests.c | 1 + fs/inode.c | 2 ++ fs/pipe.c | 1 - fs/xfs/xfs_icache.c | 3 ++ include/linux/security.h | 4 +++ security/security.c | 49 +++++++++++++++++++------------ 9 files changed, 53 insertions(+), 20 deletions(-)
Documentation/filesystems/vfs.rst +7 -1
@@ -306,7 +306,13 @@ 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. LSMs leverage the + vfs RCU callback (i_callback) to free inode->i_security after a + RCU grace period. If the filesystem provides a destroy_inode() + callback but not a free_inode() callback, it is responsible to + call security_inode_free_rcu() in the filesystem's RCU callback. + For example, xfs calls security_inode_free_rcu() from + xfs_inode_free_callback(). ``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 +2 -0
@@ -321,6 +321,8 @@ EXPORT_SYMBOL(free_inode_nonrcu);static void i_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
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 +3 -0
@@ -30,6 +30,7 @@#include "xfs_metafile.h" #include <linux/iversion.h> +#include <linux/security.h> /* Radix tree tags for incore inode tree. */
@@ -97,6 +98,7 @@ xfs_inode_alloc( ip =alloc_inode_sb (mp->m_super, xfs_inode_cache,GFP_KERNEL | __GFP_NOFAIL);if (inode_init_always (mp->m_super,VFS_I (ip))) { +security_inode_free_rcu (VFS_I (ip));kmem_cache_free (xfs_inode_cache, ip);return NULL ; }
@@ -162,6 +164,7 @@ xfs_inode_free_callback( ip->i_itemp =NULL ; } +security_inode_free_rcu (inode);kmem_cache_free (xfs_inode_cache, ip); }
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 +31 -18
75 lines not on this page · raw message
Signature · 1 line
2.43.5
Paul Moore
maintainerQuoted 13 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.Add
security_inode_free_rcu()toi_callbackto 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:
Quoted 5 lines
- XFS recycles inode after
destroy_inode. The inodes are freed from recycle logic. Letxfs_inode_free_callback()andxfs_inode_alloc()callsecurity_inode_free_rcu()before freeing the inode.
- Let pipe free inode from a RCU callback.
- Let btrfs-test free inode from a RCU callback.
If I recall correctly, historically the vfs devs have pushed back on filesystem specific changes such as this, requiring LSM hooks to operate at the VFS layer unless there was absolutely no other choice.
From a LSM perspective I'm also a little concerned that this approach is too reliant on individual filesystems doing the right thing with respect to LSM hooks which I worry will result in some ugly bugs in the future.
Quoted 12 lines
Signed-off-by Song Liu <[email protected]>--- Documentation/filesystems/vfs.rst | 8 ++++- fs/btrfs/fs.h | 1 + fs/btrfs/inode.c | 4 +++ fs/btrfs/tests/btrfs-tests.c | 1 + fs/inode.c | 2 ++ fs/pipe.c | 1 - fs/xfs/xfs_icache.c | 3 ++ include/linux/security.h | 4 +++ security/security.c | 49 +++++++++++++++++++------------ 9 files changed, 53 insertions(+), 20 deletions(-)
Signature · 1 line
paul-moore.com
Hi Paul,
Thanks for your quick review!
Quoted 21 lines · Paul Moore
Quoted 13 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.Add
security_inode_free_rcu()toi_callbackto 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:Quoted 5 lines
- XFS recycles inode after
destroy_inode. The inodes are freed from recycle logic. Letxfs_inode_free_callback()andxfs_inode_alloc()callsecurity_inode_free_rcu()before freeing the inode.
- Let pipe free inode from a RCU callback.
- Let btrfs-test free inode from a RCU callback.
Quoted 8 lines
If I recall correctly, historically the vfs devs have pushed back on filesystem specific changes such as this, requiring LSM hooks to operate at the VFS layer unless there was absolutely no other choice.
From a LSM perspective I'm also a little concerned that this approach is too reliant on individual filesystems doing the right thing with respect to LSM hooks which I worry will result in some ugly bugs in the future.
Totally agree with the concerns. However, given the savings is quite significant (saving two pointers per inode), I think the it may justify the extra effort to maintain the logic. Note that, some LSMs are enabled in most systems and cannot be easily disabled, so I am assuming most systems will see the savings.
Thanks,
Song
Paul Moore
maintainerQuoted 31 lines · Song Liu
Quoted 21 lines · Paul Moore
Quoted 13 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.Add
security_inode_free_rcu()toi_callbackto 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:Quoted 5 lines
- XFS recycles inode after
destroy_inode. The inodes are freed from recycle logic. Letxfs_inode_free_callback()andxfs_inode_alloc()callsecurity_inode_free_rcu()before freeing the inode.
- Let pipe free inode from a RCU callback.
- Let btrfs-test free inode from a RCU callback.Quoted 8 lines
If I recall correctly, historically the vfs devs have pushed back on filesystem specific changes such as this, requiring LSM hooks to operate at the VFS layer unless there was absolutely no other choice.
From a LSM perspective I'm also a little concerned that this approach is too reliant on individual filesystems doing the right thing with respect to LSM hooks which I worry will result in some ugly bugs in the future.
Quoted 5 lines
Totally agree with the concerns. However, given the savings is quite significant (saving two pointers per inode), I think the it may justify the extra effort to maintain the logic. Note that, some LSMs are enabled in most systems and cannot be easily disabled, so I am assuming most systems will see the savings.
I suggest trying to find a solution that is not as fragile in the face of cross subsystem changes and ideally also limits the number of times the LSM calls must be made in individual filesystems.
Signature · 1 line
paul-moore.com
Al Viro
maintainerQuoted 1 line
- Let pipe free inode from a RCU callback.
... which hurts the systems with LSM crap disabled. NAK.
Quoted 39 lines · Paul Moore
Quoted 31 lines · Song Liu
Quoted 21 lines · Paul Moore
Quoted 13 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.Add
security_inode_free_rcu()toi_callbackto 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:Quoted 5 lines
- XFS recycles inode after
destroy_inode. The inodes are freed from recycle logic. Letxfs_inode_free_callback()andxfs_inode_alloc()callsecurity_inode_free_rcu()before freeing the inode.
- Let pipe free inode from a RCU callback.
- Let btrfs-test free inode from a RCU callback.Quoted 8 lines
If I recall correctly, historically the vfs devs have pushed back on filesystem specific changes such as this, requiring LSM hooks to operate at the VFS layer unless there was absolutely no other choice.
From a LSM perspective I'm also a little concerned that this approach is too reliant on individual filesystems doing the right thing with respect to LSM hooks which I worry will result in some ugly bugs in the future.
Quoted 5 lines
Totally agree with the concerns. However, given the savings is quite significant (saving two pointers per inode), I think the it may justify the extra effort to maintain the logic. Note that, some LSMs are enabled in most systems and cannot be easily disabled, so I am assuming most systems will see the savings.
Quoted 3 lines
I suggest trying to find a solution that is not as fragile in the face of cross subsystem changes and ideally also limits the number of times the LSM calls must be made in individual filesystems.
There are three (groups of) subsystems here: VFS, file systems, and LSM. It is not really possible to do this without crossing subsystem boundaries. Specifically, since VFS allow a file system to have destroy_inode callback, but not free_inode callback, we will need such file systems to handle rcu callback. Does this make sense?
Suggestions on how we can solve this better are always appreciated.
Thanks,
Song
Quoted 5 lines · Al Viro
Quoted 2 lines
- Let pipe free inode from a RCU callback.
... which hurts the systems with LSM crap disabled. NAK.
How do we measure the overhead in such cases? AFAICT, the overhead is very small:
1. Many (most) systems have some LSM enabled anyway.
2. pipe create/release is not on any hot path. On a busy system with 176 CPUs, I measured ~30 pipe create/release per second.
3. The overhead of a rcu callback is small.
Given these measures, I don't think "hurts the system without LSM" justifies 2 extra pointers per inode.
Thanks,
Song