diff options
| author | Kyeongdon Kim <kyeongdon.kim@lge.com> | 2017-09-06 05:50:19 -0400 |
|---|---|---|
| committer | Paul Moore <paul@paul-moore.com> | 2017-09-20 12:01:58 -0400 |
| commit | 7c620ece125cbab7b5dfcb574ee1e64ab8b562cd (patch) | |
| tree | 53535c964faebf8e3ced3151353c8577213a59cc /security | |
| parent | 2bd6bf03f4c1c59381d62c61d03f6cc3fe71f66e (diff) | |
selinux: Use kmem_cache for hashtab_node
During random test as own device to check slub account,
we found some slack memory from hashtab_node(kmalloc-64).
By using kzalloc(), middle of test result like below:
allocated size 240768
request size 45144
slack size 195624
allocation count 3762
So, we want to use kmem_cache_zalloc() and that
can reduce memory size 52byte(slack size/alloc count) per each struct.
Signed-off-by: Kyeongdon Kim <kyeongdon.kim@lge.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
Diffstat (limited to 'security')
| -rw-r--r-- | security/selinux/ss/hashtab.c | 17 | ||||
| -rw-r--r-- | security/selinux/ss/hashtab.h | 4 | ||||
| -rw-r--r-- | security/selinux/ss/services.c | 4 |
3 files changed, 23 insertions, 2 deletions
diff --git a/security/selinux/ss/hashtab.c b/security/selinux/ss/hashtab.c index 686c3917064c..bef7577d1270 100644 --- a/security/selinux/ss/hashtab.c +++ b/security/selinux/ss/hashtab.c | |||
| @@ -9,6 +9,8 @@ | |||
| 9 | #include <linux/sched.h> | 9 | #include <linux/sched.h> |
| 10 | #include "hashtab.h" | 10 | #include "hashtab.h" |
| 11 | 11 | ||
| 12 | static struct kmem_cache *hashtab_node_cachep; | ||
| 13 | |||
| 12 | struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key), | 14 | struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key), |
| 13 | int (*keycmp)(struct hashtab *h, const void *key1, const void *key2), | 15 | int (*keycmp)(struct hashtab *h, const void *key1, const void *key2), |
| 14 | u32 size) | 16 | u32 size) |
| @@ -57,7 +59,7 @@ int hashtab_insert(struct hashtab *h, void *key, void *datum) | |||
| 57 | if (cur && (h->keycmp(h, key, cur->key) == 0)) | 59 | if (cur && (h->keycmp(h, key, cur->key) == 0)) |
| 58 | return -EEXIST; | 60 | return -EEXIST; |
| 59 | 61 | ||
| 60 | newnode = kzalloc(sizeof(*newnode), GFP_KERNEL); | 62 | newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL); |
| 61 | if (!newnode) | 63 | if (!newnode) |
| 62 | return -ENOMEM; | 64 | return -ENOMEM; |
| 63 | newnode->key = key; | 65 | newnode->key = key; |
| @@ -106,7 +108,7 @@ void hashtab_destroy(struct hashtab *h) | |||
| 106 | while (cur) { | 108 | while (cur) { |
| 107 | temp = cur; | 109 | temp = cur; |
| 108 | cur = cur->next; | 110 | cur = cur->next; |
| 109 | kfree(temp); | 111 | kmem_cache_free(hashtab_node_cachep, temp); |
| 110 | } | 112 | } |
| 111 | h->htable[i] = NULL; | 113 | h->htable[i] = NULL; |
| 112 | } | 114 | } |
| @@ -166,3 +168,14 @@ void hashtab_stat(struct hashtab *h, struct hashtab_info *info) | |||
| 166 | info->slots_used = slots_used; | 168 | info->slots_used = slots_used; |
| 167 | info->max_chain_len = max_chain_len; | 169 | info->max_chain_len = max_chain_len; |
| 168 | } | 170 | } |
| 171 | void hashtab_cache_init(void) | ||
| 172 | { | ||
| 173 | hashtab_node_cachep = kmem_cache_create("hashtab_node", | ||
| 174 | sizeof(struct hashtab_node), | ||
| 175 | 0, SLAB_PANIC, NULL); | ||
| 176 | } | ||
| 177 | |||
| 178 | void hashtab_cache_destroy(void) | ||
| 179 | { | ||
| 180 | kmem_cache_destroy(hashtab_node_cachep); | ||
| 181 | } | ||
diff --git a/security/selinux/ss/hashtab.h b/security/selinux/ss/hashtab.h index 009fb5e06172..d6883d3e7c5b 100644 --- a/security/selinux/ss/hashtab.h +++ b/security/selinux/ss/hashtab.h | |||
| @@ -84,4 +84,8 @@ int hashtab_map(struct hashtab *h, | |||
| 84 | /* Fill info with some hash table statistics */ | 84 | /* Fill info with some hash table statistics */ |
| 85 | void hashtab_stat(struct hashtab *h, struct hashtab_info *info); | 85 | void hashtab_stat(struct hashtab *h, struct hashtab_info *info); |
| 86 | 86 | ||
| 87 | /* Use kmem_cache for hashtab_node */ | ||
| 88 | void hashtab_cache_init(void); | ||
| 89 | void hashtab_cache_destroy(void); | ||
| 90 | |||
| 87 | #endif /* _SS_HASHTAB_H */ | 91 | #endif /* _SS_HASHTAB_H */ |
diff --git a/security/selinux/ss/services.c b/security/selinux/ss/services.c index e4a1c0dc561a..33cfe5d3d6cb 100644 --- a/security/selinux/ss/services.c +++ b/security/selinux/ss/services.c | |||
| @@ -2060,10 +2060,12 @@ int security_load_policy(void *data, size_t len) | |||
| 2060 | if (!ss_initialized) { | 2060 | if (!ss_initialized) { |
| 2061 | avtab_cache_init(); | 2061 | avtab_cache_init(); |
| 2062 | ebitmap_cache_init(); | 2062 | ebitmap_cache_init(); |
| 2063 | hashtab_cache_init(); | ||
| 2063 | rc = policydb_read(&policydb, fp); | 2064 | rc = policydb_read(&policydb, fp); |
| 2064 | if (rc) { | 2065 | if (rc) { |
| 2065 | avtab_cache_destroy(); | 2066 | avtab_cache_destroy(); |
| 2066 | ebitmap_cache_destroy(); | 2067 | ebitmap_cache_destroy(); |
| 2068 | hashtab_cache_destroy(); | ||
| 2067 | goto out; | 2069 | goto out; |
| 2068 | } | 2070 | } |
| 2069 | 2071 | ||
| @@ -2075,6 +2077,7 @@ int security_load_policy(void *data, size_t len) | |||
| 2075 | policydb_destroy(&policydb); | 2077 | policydb_destroy(&policydb); |
| 2076 | avtab_cache_destroy(); | 2078 | avtab_cache_destroy(); |
| 2077 | ebitmap_cache_destroy(); | 2079 | ebitmap_cache_destroy(); |
| 2080 | hashtab_cache_destroy(); | ||
| 2078 | goto out; | 2081 | goto out; |
| 2079 | } | 2082 | } |
| 2080 | 2083 | ||
| @@ -2083,6 +2086,7 @@ int security_load_policy(void *data, size_t len) | |||
| 2083 | policydb_destroy(&policydb); | 2086 | policydb_destroy(&policydb); |
| 2084 | avtab_cache_destroy(); | 2087 | avtab_cache_destroy(); |
| 2085 | ebitmap_cache_destroy(); | 2088 | ebitmap_cache_destroy(); |
| 2089 | hashtab_cache_destroy(); | ||
| 2086 | goto out; | 2090 | goto out; |
| 2087 | } | 2091 | } |
| 2088 | 2092 | ||
