The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] security: selinux: Use a kmem_cache for allocation struct file_security_struct
@ 2015-10-05  5:45 Sangwoo
  2015-10-07 18:28 ` Stephen Smalley
  2015-10-08 17:03 ` Paul Moore
  0 siblings, 2 replies; 3+ messages in thread
From: Sangwoo @ 2015-10-05  5:45 UTC (permalink / raw)
  To: paul, sds, eparis, james.l.morris, serge
  Cc: selinux, linux-security-module, linux-kernel, Sangwoo

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>
---
 security/selinux/hooks.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 3f8d567..c20e082 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -126,6 +126,7 @@ int selinux_enabled = 1;
 #endif
 
 static struct kmem_cache *sel_inode_cache;
+static struct kmem_cache *file_security_cache;
 
 /**
  * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
@@ -287,7 +288,7 @@ static int file_alloc_security(struct file *file)
 	struct file_security_struct *fsec;
 	u32 sid = current_sid();
 
-	fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
+	fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
 	if (!fsec)
 		return -ENOMEM;
 
@@ -302,7 +303,7 @@ static void file_free_security(struct file *file)
 {
 	struct file_security_struct *fsec = file->f_security;
 	file->f_security = NULL;
-	kfree(fsec);
+	kmem_cache_free(file_security_cache, fsec);
 }
 
 static int superblock_alloc_security(struct super_block *sb)
@@ -6086,6 +6087,9 @@ static __init int selinux_init(void)
 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
 					    sizeof(struct inode_security_struct),
 					    0, SLAB_PANIC, NULL);
+	file_security_cache = kmem_cache_create("selinux_file_security",
+					    sizeof(struct file_security_struct),
+					    0, SLAB_PANIC, NULL);
 	avc_init();
 
 	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] security: selinux: Use a kmem_cache for allocation struct file_security_struct
  2015-10-05  5:45 [PATCH] security: selinux: Use a kmem_cache for allocation struct file_security_struct Sangwoo
@ 2015-10-07 18:28 ` Stephen Smalley
  2015-10-08 17:03 ` Paul Moore
  1 sibling, 0 replies; 3+ messages in thread
From: Stephen Smalley @ 2015-10-07 18:28 UTC (permalink / raw)
  To: Sangwoo, paul, eparis, james.l.morris, serge
  Cc: selinux, linux-security-module, linux-kernel

On 10/05/2015 01:45 AM, Sangwoo wrote:
> 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>

> ---
>  security/selinux/hooks.c |    8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 3f8d567..c20e082 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -126,6 +126,7 @@ int selinux_enabled = 1;
>  #endif
>  
>  static struct kmem_cache *sel_inode_cache;
> +static struct kmem_cache *file_security_cache;
>  
>  /**
>   * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
> @@ -287,7 +288,7 @@ static int file_alloc_security(struct file *file)
>  	struct file_security_struct *fsec;
>  	u32 sid = current_sid();
>  
> -	fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
> +	fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
>  	if (!fsec)
>  		return -ENOMEM;
>  
> @@ -302,7 +303,7 @@ static void file_free_security(struct file *file)
>  {
>  	struct file_security_struct *fsec = file->f_security;
>  	file->f_security = NULL;
> -	kfree(fsec);
> +	kmem_cache_free(file_security_cache, fsec);
>  }
>  
>  static int superblock_alloc_security(struct super_block *sb)
> @@ -6086,6 +6087,9 @@ static __init int selinux_init(void)
>  	sel_inode_cache = kmem_cache_create("selinux_inode_security",
>  					    sizeof(struct inode_security_struct),
>  					    0, SLAB_PANIC, NULL);
> +	file_security_cache = kmem_cache_create("selinux_file_security",
> +					    sizeof(struct file_security_struct),
> +					    0, SLAB_PANIC, NULL);
>  	avc_init();
>  
>  	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] security: selinux: Use a kmem_cache for allocation struct file_security_struct
  2015-10-05  5:45 [PATCH] security: selinux: Use a kmem_cache for allocation struct file_security_struct Sangwoo
  2015-10-07 18:28 ` Stephen Smalley
@ 2015-10-08 17:03 ` Paul Moore
  1 sibling, 0 replies; 3+ messages in thread
From: Paul Moore @ 2015-10-08 17:03 UTC (permalink / raw)
  To: Sangwoo
  Cc: sds, eparis, james.l.morris, serge, selinux,
	linux-security-module, linux-kernel

On Monday, October 05, 2015 02:45:41 PM Sangwoo wrote:
> 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>
> ---
>  security/selinux/hooks.c |    8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)

Applied, thanks for the patch and the data.  Sorry for the delay, it should be 
in linux-next tomorrow.
 
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 3f8d567..c20e082 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -126,6 +126,7 @@ int selinux_enabled = 1;
>  #endif
> 
>  static struct kmem_cache *sel_inode_cache;
> +static struct kmem_cache *file_security_cache;
> 
>  /**
>   * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
> @@ -287,7 +288,7 @@ static int file_alloc_security(struct file *file)
>  	struct file_security_struct *fsec;
>  	u32 sid = current_sid();
> 
> -	fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
> +	fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
>  	if (!fsec)
>  		return -ENOMEM;
> 
> @@ -302,7 +303,7 @@ static void file_free_security(struct file *file)
>  {
>  	struct file_security_struct *fsec = file->f_security;
>  	file->f_security = NULL;
> -	kfree(fsec);
> +	kmem_cache_free(file_security_cache, fsec);
>  }
> 
>  static int superblock_alloc_security(struct super_block *sb)
> @@ -6086,6 +6087,9 @@ static __init int selinux_init(void)
>  	sel_inode_cache = kmem_cache_create("selinux_inode_security",
>  					    sizeof(struct inode_security_struct),
>  					    0, SLAB_PANIC, NULL);
> +	file_security_cache = kmem_cache_create("selinux_file_security",
> +					    sizeof(struct file_security_struct),
> +					    0, SLAB_PANIC, NULL);
>  	avc_init();
> 
>  	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));

-- 
paul moore
www.paul-moore.com


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-10-08 17:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-05  5:45 [PATCH] security: selinux: Use a kmem_cache for allocation struct file_security_struct Sangwoo
2015-10-07 18:28 ` Stephen Smalley
2015-10-08 17:03 ` Paul Moore

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox