security: smack: add kmem_cache for smack_rule allocations
authorjooseong.lee <jooseong.lee@samsung.com>
Mon, 23 Mar 2015 15:09:14 +0000 (00:09 +0900)
committerjuseong lee <jooseong.lee@samsung.com>
Wed, 25 Mar 2015 00:58:26 +0000 (09:58 +0900)
On ARM, sizeof(struct smack_rule)==20. Allocation by kmalloc() uses a32-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 allocatesstruct smack_rule using 24-byte-long chunks according to ksize(). This reducesamount of used memory by 25%.

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

index 020307ef097296f2eac85be8ae823fdad93ff508..45aa224b91ae4201da293e91566608873716acfc 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 4b5515ec8620302201b4a6bb0adbcabaf2a1fd4d..523e106a274ccb9fafecdfdbfe4deea56f2c0075 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 32b248820840e76b7db365730f12a2f9eb7b29d0..e602a34bf7e1e2480f101a3ce74f5cc6ab1cd2fe 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;