Re: [PATCH] mm: slab: Avoid the use of one-element array and use struct_size() helper

2020-07-29 Thread Gustavo A. R. Silva
Hi,

If you are going to copy/paste this, please at least CC the people
that is originally working on these changes, in this case _me_. One
needs to be very careful when doing these transformations. This
code doesn't even exist in linux-next.

If you want to land your first kernel patch, I encourage you to start
in drivers/staging/, while you get the hang of the kernel development
process and you know how to work with linux-next. :)

Thanks
--
Gustavo

On 7/29/20 09:50, Qianli Zhao wrote:
> From: Qianli Zhao 
> 
> There is a regular need in the kernel to provide a way to declare having a
> dynamically sized set of trailing elements in a structure. Kernel code should
> always use “flexible array members”[1] for these cases. The older style of
> one-element or zero-length arrays should no longer be used[2].
> 
> Make use of the struct_size() helper instead of an open-coded version
> in order to avoid any potential type mistakes.
> 
> [1] https://en.wikipedia.org/wiki/Flexible_array_member
> [2] https://github.com/KSPP/linux/issues/21
> 
> Signed-off-by: Qianli Zhao 
> ---
>  mm/slab.h| 2 +-
>  mm/slab_common.c | 7 ++-
>  2 files changed, 3 insertions(+), 6 deletions(-)
> 
> diff --git a/mm/slab.h b/mm/slab.h
> index 74f7e09..c12fb65 100644
> --- a/mm/slab.h
> +++ b/mm/slab.h
> @@ -34,7 +34,7 @@ struct kmem_cache {
>  
>  struct memcg_cache_array {
>   struct rcu_head rcu;
> - struct kmem_cache *entries[0];
> + struct kmem_cache *entries[];
>  };
>  
>  /*
> diff --git a/mm/slab_common.c b/mm/slab_common.c
> index fe8b684..56f4818 100644
> --- a/mm/slab_common.c
> +++ b/mm/slab_common.c
> @@ -166,9 +166,7 @@ static int init_memcg_params(struct kmem_cache *s,
>   if (!memcg_nr_cache_ids)
>   return 0;
>  
> - arr = kvzalloc(sizeof(struct memcg_cache_array) +
> -memcg_nr_cache_ids * sizeof(void *),
> -GFP_KERNEL);
> + arr = kvzalloc(struct_size(arr, entries, memcg_nr_cache_ids), 
> GFP_KERNEL);
>   if (!arr)
>   return -ENOMEM;
>  
> @@ -199,8 +197,7 @@ static int update_memcg_params(struct kmem_cache *s, int 
> new_array_size)
>  {
>   struct memcg_cache_array *old, *new;
>  
> - new = kvzalloc(sizeof(struct memcg_cache_array) +
> -new_array_size * sizeof(void *), GFP_KERNEL);
> + new = kvzalloc(struct_size(new, entries, new_array_size), GFP_KERNEL);
>   if (!new)
>   return -ENOMEM;
>  
> 


Re: [PATCH] mm: slab: Avoid the use of one-element array and use struct_size() helper

2020-07-29 Thread David Rientjes
On Wed, 29 Jul 2020, Qianli Zhao wrote:

> From: Qianli Zhao 
> 
> There is a regular need in the kernel to provide a way to declare having a
> dynamically sized set of trailing elements in a structure. Kernel code should
> always use “flexible array members”[1] for these cases. The older style of
> one-element or zero-length arrays should no longer be used[2].
> 
> Make use of the struct_size() helper instead of an open-coded version
> in order to avoid any potential type mistakes.
> 
> [1] https://en.wikipedia.org/wiki/Flexible_array_member
> [2] https://github.com/KSPP/linux/issues/21
> 
> Signed-off-by: Qianli Zhao 
> ---
>  mm/slab.h| 2 +-
>  mm/slab_common.c | 7 ++-
>  2 files changed, 3 insertions(+), 6 deletions(-)
> 
> diff --git a/mm/slab.h b/mm/slab.h
> index 74f7e09..c12fb65 100644
> --- a/mm/slab.h
> +++ b/mm/slab.h
> @@ -34,7 +34,7 @@ struct kmem_cache {
>  
>  struct memcg_cache_array {
>   struct rcu_head rcu;
> - struct kmem_cache *entries[0];
> + struct kmem_cache *entries[];
>  };
>  
>  /*

This is removed in the -mm tree, see 
https://www.ozlabs.org/~akpm/mmotm/broken-out/mm-memcg-slab-use-a-single-set-of-kmem_caches-for-all-allocations.patch

> diff --git a/mm/slab_common.c b/mm/slab_common.c
> index fe8b684..56f4818 100644
> --- a/mm/slab_common.c
> +++ b/mm/slab_common.c
> @@ -166,9 +166,7 @@ static int init_memcg_params(struct kmem_cache *s,
>   if (!memcg_nr_cache_ids)
>   return 0;
>  
> - arr = kvzalloc(sizeof(struct memcg_cache_array) +
> -memcg_nr_cache_ids * sizeof(void *),
> -GFP_KERNEL);
> + arr = kvzalloc(struct_size(arr, entries, memcg_nr_cache_ids), 
> GFP_KERNEL);
>   if (!arr)
>   return -ENOMEM;
>  
> @@ -199,8 +197,7 @@ static int update_memcg_params(struct kmem_cache *s, int 
> new_array_size)
>  {
>   struct memcg_cache_array *old, *new;
>  
> - new = kvzalloc(sizeof(struct memcg_cache_array) +
> -new_array_size * sizeof(void *), GFP_KERNEL);
> + new = kvzalloc(struct_size(new, entries, new_array_size), GFP_KERNEL);
>   if (!new)
>   return -ENOMEM;
>  
> -- 
> 2.7.4
> 
> 

[PATCH] mm: slab: Avoid the use of one-element array and use struct_size() helper

2020-07-29 Thread Qianli Zhao
From: Qianli Zhao 

There is a regular need in the kernel to provide a way to declare having a
dynamically sized set of trailing elements in a structure. Kernel code should
always use “flexible array members”[1] for these cases. The older style of
one-element or zero-length arrays should no longer be used[2].

Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes.

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://github.com/KSPP/linux/issues/21

Signed-off-by: Qianli Zhao 
---
 mm/slab.h| 2 +-
 mm/slab_common.c | 7 ++-
 2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/mm/slab.h b/mm/slab.h
index 74f7e09..c12fb65 100644
--- a/mm/slab.h
+++ b/mm/slab.h
@@ -34,7 +34,7 @@ struct kmem_cache {
 
 struct memcg_cache_array {
struct rcu_head rcu;
-   struct kmem_cache *entries[0];
+   struct kmem_cache *entries[];
 };
 
 /*
diff --git a/mm/slab_common.c b/mm/slab_common.c
index fe8b684..56f4818 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -166,9 +166,7 @@ static int init_memcg_params(struct kmem_cache *s,
if (!memcg_nr_cache_ids)
return 0;
 
-   arr = kvzalloc(sizeof(struct memcg_cache_array) +
-  memcg_nr_cache_ids * sizeof(void *),
-  GFP_KERNEL);
+   arr = kvzalloc(struct_size(arr, entries, memcg_nr_cache_ids), 
GFP_KERNEL);
if (!arr)
return -ENOMEM;
 
@@ -199,8 +197,7 @@ static int update_memcg_params(struct kmem_cache *s, int 
new_array_size)
 {
struct memcg_cache_array *old, *new;
 
-   new = kvzalloc(sizeof(struct memcg_cache_array) +
-  new_array_size * sizeof(void *), GFP_KERNEL);
+   new = kvzalloc(struct_size(new, entries, new_array_size), GFP_KERNEL);
if (!new)
return -ENOMEM;
 
-- 
2.7.4