security: smack: add kmem_cache for smack_rule allocations
authorjooseong.lee <jooseong.lee@samsung.com>
Wed, 25 Mar 2015 01:58:44 +0000 (10:58 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Wed, 25 Mar 2015 05:23:14 +0000 (14:23 +0900)
On ARM, sizeof(struct smack_rule)==20. Allocation by kmalloc() uses a
32-byte-long chunk to allocate 20 bytes. Just ask ksize().  It means that 40%
of memory is simply wasted for padding bytes.

The problem is fixed in this patch by using kmem_cache. The cache allocates
struct smack_rule using 24-byte-long chunks according to ksize(). This reduces
amount of used memory by 25%.

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

index 020307e..45aa224 100644 (file)
@@ -266,6 +266,9 @@ extern struct mutex smack_known_lock;
 extern struct list_head smack_known_list;
 extern struct list_head smk_netlbladdr_list;
 
+/* Cache for fast and thrifty allocations */
+extern struct kmem_cache *smack_rule_cache;
+
 extern struct security_operations smack_ops;
 
 #define SMACK_HASH_SLOTS 16
index 4b5515e..523e106 100644 (file)
@@ -4016,6 +4016,9 @@ static __init void init_smack_known_list(void)
        smk_insert_entry(&smack_known_web);
 }
 
+/* KMEM caches for fast and thrifty allocations */
+struct kmem_cache *smack_rule_cache;
+
 /**
  * smack_init - initialize the smack system
  *
@@ -4029,10 +4032,16 @@ static __init int smack_init(void)
        if (!security_module_enable(&smack_ops))
                return 0;
 
+       smack_rule_cache = KMEM_CACHE(smack_rule, 0);
+       if (!smack_rule_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_rule_cache);
                return -ENOMEM;
+       }
 
        printk(KERN_INFO "Smack:  Initializing.\n");
 
index 32b2488..e602a34 100644 (file)
@@ -235,7 +235,7 @@ static int smk_set_access(struct smack_parsed_rule *srp,
        }
 
        if (found == 0) {
-               sp = kzalloc(sizeof(*sp), GFP_KERNEL);
+               sp = kmem_cache_zalloc(smack_rule_cache, GFP_KERNEL);
                if (sp == NULL) {
                        rc = -ENOMEM;
                        goto out;