Skip to content

Commit 6320565

Browse files
Sangwoopcmoore
authored andcommitted
selinux: Use a kmem_cache for allocation struct file_security_struct
The size of struct file_security_struct is 16byte at my setup. But, the real allocation size for per each file_security_struct is 64bytes in my setup that kmalloc min size is 64bytes because ARCH_DMA_MINALIGN is 64. This allocation is called every times at file allocation(alloc_file()). So, the total slack memory size(allocated size - request size) is increased exponentially. E.g) Min Kmalloc Size : 64bytes, Unit : bytes Allocated Size | Request Size | Slack Size | Allocation Count --------------------------------------------------------------- 770048 | 192512 | 577536 | 12032 At the result, this change reduce memory usage 42bytes per each file_security_struct Signed-off-by: Sangwoo <sangwoo2.park@lge.com> Acked-by: Stephen Smalley <sds@tycho.nsa.gov> [PM: removed extra subject prefix] Signed-off-by: Paul Moore <pmoore@redhat.com>
1 parent 1d2a168 commit 6320565

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

security/selinux/hooks.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ int selinux_enabled = 1;
126126
#endif
127127

128128
static struct kmem_cache *sel_inode_cache;
129+
static struct kmem_cache *file_security_cache;
129130

130131
/**
131132
* selinux_secmark_enabled - Check to see if SECMARK is currently enabled
@@ -287,7 +288,7 @@ static int file_alloc_security(struct file *file)
287288
struct file_security_struct *fsec;
288289
u32 sid = current_sid();
289290

290-
fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
291+
fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
291292
if (!fsec)
292293
return -ENOMEM;
293294

@@ -302,7 +303,7 @@ static void file_free_security(struct file *file)
302303
{
303304
struct file_security_struct *fsec = file->f_security;
304305
file->f_security = NULL;
305-
kfree(fsec);
306+
kmem_cache_free(file_security_cache, fsec);
306307
}
307308

308309
static int superblock_alloc_security(struct super_block *sb)
@@ -6086,6 +6087,9 @@ static __init int selinux_init(void)
60866087
sel_inode_cache = kmem_cache_create("selinux_inode_security",
60876088
sizeof(struct inode_security_struct),
60886089
0, SLAB_PANIC, NULL);
6090+
file_security_cache = kmem_cache_create("selinux_file_security",
6091+
sizeof(struct file_security_struct),
6092+
0, SLAB_PANIC, NULL);
60896093
avc_init();
60906094

60916095
security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));

0 commit comments

Comments
 (0)