From: jooseong.lee Date: Wed, 25 Mar 2015 01:58:44 +0000 (+0900) Subject: security: smack: add kmem_cache for smack_rule allocations X-Git-Tag: submit/tizen/20160422.055611~1^2~72 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f2be802502df388bf55de717a9dc4c6ba4c25a98;p=sdk%2Femulator%2Femulator-kernel.git security: smack: add kmem_cache for smack_rule allocations 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 --- diff --git a/security/smack/smack.h b/security/smack/smack.h index 020307ef0972..45aa224b91ae 100644 --- a/security/smack/smack.h +++ b/security/smack/smack.h @@ -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 diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c index 4b5515ec8620..523e106a274c 100644 --- a/security/smack/smack_lsm.c +++ b/security/smack/smack_lsm.c @@ -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"); diff --git a/security/smack/smackfs.c b/security/smack/smackfs.c index 32b248820840..e602a34bf7e1 100644 --- a/security/smack/smackfs.c +++ b/security/smack/smackfs.c @@ -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;