Security: smack: replace kzalloc with kmem_cache for inode_smack
authorjooseong.lee <jooseong.lee@samsung.com>
Wed, 25 Mar 2015 02:02:03 +0000 (11:02 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Wed, 25 Mar 2015 05:23:38 +0000 (14:23 +0900)
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.

Change-Id: Ia930c48bb06eb9c461eb3cf449e25a3a53be7299
Signed-off-by: jooseong.lee <jooseong.lee@samsung.com>
security/smack/smack_lsm.c

index df3f603..f7d0d43 100644 (file)
@@ -53,6 +53,7 @@
 #define SMK_SENDING    2
 
 LIST_HEAD(smk_ipv6_port_list);
+static struct kmem_cache *smack_inode_cache;
 
 /**
  * smk_fetch - Fetch the smack label from a file.
@@ -95,7 +96,7 @@ struct inode_smack *new_inode_smack(char *smack)
 {
        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;
 
@@ -621,7 +622,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;
 }
 
@@ -4043,11 +4044,19 @@ static __init int smack_init(void)
                return -ENOMEM;
        }
 
+       smack_inode_cache = KMEM_CACHE(inode_smack, 0);
+       if (!smack_inode_cache) {
+               kmem_cache_destroy(smack_master_list_cache);
+               kmem_cache_destroy(smack_rule_cache);
+               return -ENOMEM;
+       }
+
        tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
                                GFP_KERNEL);
        if (tsp == NULL) {
                kmem_cache_destroy(smack_master_list_cache);
                kmem_cache_destroy(smack_rule_cache);
+               kmem_cache_destroy(smack_inode_cache);
                return -ENOMEM;
        }