summaryrefslogtreecommitdiffstats
path: root/security
diff options
context:
space:
mode:
authorCasey Schaufler <cschaufler@localhost.localdomain>2019-04-02 14:37:12 -0400
committerCasey Schaufler <cschaufler@localhost.localdomain>2019-04-02 14:45:41 -0400
commit4e328b08882a68649df2c2b76fd208b0d0b85503 (patch)
tree1d11925b2e79b19646ff675a0d02bae725f4b21e /security
parent460d95a1d69d5c0352379a3651c9cb6ec09e4ddb (diff)
Smack: Create smack_rule cache to optimize memory usage
This patch allows for small memory optimization by creating the kmem cache for "struct smack_rule" instead of using kzalloc. For adding new smack rule, kzalloc is used to allocate the memory for "struct smack_rule". kzalloc will always allocate 32 or 64 bytes for 1 structure depending upon the kzalloc cache sizes available in system. Although the size of structure is 20 bytes only, resulting in memory wastage per object in the default pool. For e.g., if there are 20000 rules, then it will save 240KB(20000*12) which is crucial for small memory targets. Signed-off-by: Vishal Goel <vishal.goel@samsung.com> Signed-off-by: Amit Sahrawat <a.sahrawat@samsung.com> Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>
Diffstat (limited to 'security')
-rw-r--r--security/smack/smack.h1
-rw-r--r--security/smack/smack_lsm.c11
-rw-r--r--security/smack/smackfs.c2
3 files changed, 11 insertions, 3 deletions
diff --git a/security/smack/smack.h b/security/smack/smack.h
index cf52af77d15e..e41ca1d58484 100644
--- a/security/smack/smack.h
+++ b/security/smack/smack.h
@@ -348,6 +348,7 @@ extern struct list_head smack_onlycap_list;
348 348
349#define SMACK_HASH_SLOTS 16 349#define SMACK_HASH_SLOTS 16
350extern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS]; 350extern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
351extern struct kmem_cache *smack_rule_cache;
351 352
352static inline struct task_smack *smack_cred(const struct cred *cred) 353static inline struct task_smack *smack_cred(const struct cred *cred)
353{ 354{
diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index 5c1613519d5a..bd45c9139d34 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -59,6 +59,7 @@ DEFINE_MUTEX(smack_ipv6_lock);
59static LIST_HEAD(smk_ipv6_port_list); 59static LIST_HEAD(smk_ipv6_port_list);
60#endif 60#endif
61static struct kmem_cache *smack_inode_cache; 61static struct kmem_cache *smack_inode_cache;
62struct kmem_cache *smack_rule_cache;
62int smack_enabled; 63int smack_enabled;
63 64
64#define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s} 65#define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
@@ -354,7 +355,7 @@ static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
354 int rc = 0; 355 int rc = 0;
355 356
356 list_for_each_entry_rcu(orp, ohead, list) { 357 list_for_each_entry_rcu(orp, ohead, list) {
357 nrp = kzalloc(sizeof(struct smack_rule), gfp); 358 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
358 if (nrp == NULL) { 359 if (nrp == NULL) {
359 rc = -ENOMEM; 360 rc = -ENOMEM;
360 break; 361 break;
@@ -1931,7 +1932,7 @@ static void smack_cred_free(struct cred *cred)
1931 list_for_each_safe(l, n, &tsp->smk_rules) { 1932 list_for_each_safe(l, n, &tsp->smk_rules) {
1932 rp = list_entry(l, struct smack_rule, list); 1933 rp = list_entry(l, struct smack_rule, list);
1933 list_del(&rp->list); 1934 list_del(&rp->list);
1934 kfree(rp); 1935 kmem_cache_free(smack_rule_cache, rp);
1935 } 1936 }
1936} 1937}
1937 1938
@@ -4758,6 +4759,12 @@ static __init int smack_init(void)
4758 if (!smack_inode_cache) 4759 if (!smack_inode_cache)
4759 return -ENOMEM; 4760 return -ENOMEM;
4760 4761
4762 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4763 if (!smack_rule_cache) {
4764 kmem_cache_destroy(smack_inode_cache);
4765 return -ENOMEM;
4766 }
4767
4761 /* 4768 /*
4762 * Set the security state for the initial task. 4769 * Set the security state for the initial task.
4763 */ 4770 */
diff --git a/security/smack/smackfs.c b/security/smack/smackfs.c
index 8406738b45f2..47f73a0dabb1 100644
--- a/security/smack/smackfs.c
+++ b/security/smack/smackfs.c
@@ -236,7 +236,7 @@ static int smk_set_access(struct smack_parsed_rule *srp,
236 } 236 }
237 237
238 if (found == 0) { 238 if (found == 0) {
239 sp = kzalloc(sizeof(*sp), GFP_KERNEL); 239 sp = kmem_cache_zalloc(smack_rule_cache, GFP_KERNEL);
240 if (sp == NULL) { 240 if (sp == NULL) {
241 rc = -ENOMEM; 241 rc = -ENOMEM;
242 goto out; 242 goto out;