Skip to content

Commit

Permalink
Security: smack: replace kzalloc with kmem_cache for inode_smack
Browse files Browse the repository at this point in the history
The patch use kmem_cache to allocate/free inode_smack since they are
alloced in high volumes making it a perfect case for kmem_cache.

As per analysis, 24 bytes of memory is wasted per allocation due
to internal fragmentation. With kmem_cache, this can be avoided.

Accounting of memory allocation is below :
 total       slack            net      count-alloc/free        caller
Before (with kzalloc)
1919872      719952          1919872      29998/0          new_inode_smack+0x14

After (with kmem_cache)
1201680          0           1201680      30042/0          new_inode_smack+0x18

>From above data, we found that 719952 bytes(~700 KB) of memory is
saved on allocation of 29998 smack inodes.

Signed-off-by: Rohit <rohit.kr@samsung.com>
  • Loading branch information
Rohit authored and Casey Schaufler committed Oct 31, 2014
1 parent 6c892df commit 1a5b472
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions security/smack/smack_lsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#define SMK_SENDING 2

LIST_HEAD(smk_ipv6_port_list);
static struct kmem_cache *smack_inode_cache;

#ifdef CONFIG_SECURITY_SMACK_BRINGUP
static void smk_bu_mode(int mode, char *s)
Expand Down Expand Up @@ -240,7 +241,7 @@ struct inode_smack *new_inode_smack(struct smack_known *skp)
{
struct inode_smack *isp;

isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
if (isp == NULL)
return NULL;

Expand Down Expand Up @@ -767,7 +768,7 @@ static int smack_inode_alloc_security(struct inode *inode)
*/
static void smack_inode_free_security(struct inode *inode)
{
kfree(inode->i_security);
kmem_cache_free(smack_inode_cache, inode->i_security);
inode->i_security = NULL;
}

Expand Down Expand Up @@ -4265,10 +4266,16 @@ static __init int smack_init(void)
if (!security_module_enable(&smack_ops))
return 0;

smack_inode_cache = KMEM_CACHE(inode_smack, 0);
if (!smack_inode_cache)
return -ENOMEM;

tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
GFP_KERNEL);
if (tsp == NULL)
if (tsp == NULL) {
kmem_cache_destroy(smack_inode_cache);
return -ENOMEM;
}

printk(KERN_INFO "Smack: Initializing.\n");

Expand Down

0 comments on commit 1a5b472

Please sign in to comment.