Skip to content

Commit b4958c8

Browse files
leejunilpcmoore
authored andcommitted
selinux: use kmem_cache for ebitmap
The allocated size for each ebitmap_node is 192byte by kzalloc(). Then, ebitmap_node size is fixed, so it's possible to use only 144byte for each object by kmem_cache_zalloc(). It can reduce some dynamic allocation size. Signed-off-by: Junil Lee <junil0814.lee@lge.com> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 8e71bf7 commit b4958c8

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

security/selinux/ss/ebitmap.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#define BITS_PER_U64 (sizeof(u64) * 8)
2626

27+
static struct kmem_cache *ebitmap_node_cachep;
28+
2729
int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2)
2830
{
2931
struct ebitmap_node *n1, *n2;
@@ -54,7 +56,7 @@ int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src)
5456
n = src->node;
5557
prev = NULL;
5658
while (n) {
57-
new = kzalloc(sizeof(*new), GFP_ATOMIC);
59+
new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
5860
if (!new) {
5961
ebitmap_destroy(dst);
6062
return -ENOMEM;
@@ -162,7 +164,7 @@ int ebitmap_netlbl_import(struct ebitmap *ebmap,
162164
if (e_iter == NULL ||
163165
offset >= e_iter->startbit + EBITMAP_SIZE) {
164166
e_prev = e_iter;
165-
e_iter = kzalloc(sizeof(*e_iter), GFP_ATOMIC);
167+
e_iter = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
166168
if (e_iter == NULL)
167169
goto netlbl_import_failure;
168170
e_iter->startbit = offset - (offset % EBITMAP_SIZE);
@@ -288,7 +290,7 @@ int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
288290
prev->next = n->next;
289291
else
290292
e->node = n->next;
291-
kfree(n);
293+
kmem_cache_free(ebitmap_node_cachep, n);
292294
}
293295
return 0;
294296
}
@@ -299,7 +301,7 @@ int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
299301
if (!value)
300302
return 0;
301303

302-
new = kzalloc(sizeof(*new), GFP_ATOMIC);
304+
new = kmem_cache_zalloc(ebitmap_node_cachep, GFP_ATOMIC);
303305
if (!new)
304306
return -ENOMEM;
305307

@@ -332,7 +334,7 @@ void ebitmap_destroy(struct ebitmap *e)
332334
while (n) {
333335
temp = n;
334336
n = n->next;
335-
kfree(temp);
337+
kmem_cache_free(ebitmap_node_cachep, temp);
336338
}
337339

338340
e->highbit = 0;
@@ -400,7 +402,7 @@ int ebitmap_read(struct ebitmap *e, void *fp)
400402

401403
if (!n || startbit >= n->startbit + EBITMAP_SIZE) {
402404
struct ebitmap_node *tmp;
403-
tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
405+
tmp = kmem_cache_zalloc(ebitmap_node_cachep, GFP_KERNEL);
404406
if (!tmp) {
405407
printk(KERN_ERR
406408
"SELinux: ebitmap: out of memory\n");
@@ -519,3 +521,15 @@ int ebitmap_write(struct ebitmap *e, void *fp)
519521
}
520522
return 0;
521523
}
524+
525+
void ebitmap_cache_init(void)
526+
{
527+
ebitmap_node_cachep = kmem_cache_create("ebitmap_node",
528+
sizeof(struct ebitmap_node),
529+
0, SLAB_PANIC, NULL);
530+
}
531+
532+
void ebitmap_cache_destroy(void)
533+
{
534+
kmem_cache_destroy(ebitmap_node_cachep);
535+
}

security/selinux/ss/ebitmap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ void ebitmap_destroy(struct ebitmap *e);
130130
int ebitmap_read(struct ebitmap *e, void *fp);
131131
int ebitmap_write(struct ebitmap *e, void *fp);
132132

133+
void ebitmap_cache_init(void);
134+
void ebitmap_cache_destroy(void);
135+
133136
#ifdef CONFIG_NETLABEL
134137
int ebitmap_netlbl_export(struct ebitmap *ebmap,
135138
struct netlbl_lsm_catmap **catmap);

security/selinux/ss/services.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,9 +2054,11 @@ int security_load_policy(void *data, size_t len)
20542054

20552055
if (!ss_initialized) {
20562056
avtab_cache_init();
2057+
ebitmap_cache_init();
20572058
rc = policydb_read(&policydb, fp);
20582059
if (rc) {
20592060
avtab_cache_destroy();
2061+
ebitmap_cache_destroy();
20602062
goto out;
20612063
}
20622064

@@ -2067,13 +2069,15 @@ int security_load_policy(void *data, size_t len)
20672069
if (rc) {
20682070
policydb_destroy(&policydb);
20692071
avtab_cache_destroy();
2072+
ebitmap_cache_destroy();
20702073
goto out;
20712074
}
20722075

20732076
rc = policydb_load_isids(&policydb, &sidtab);
20742077
if (rc) {
20752078
policydb_destroy(&policydb);
20762079
avtab_cache_destroy();
2080+
ebitmap_cache_destroy();
20772081
goto out;
20782082
}
20792083

0 commit comments

Comments
 (0)