linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: David Rientjes <rientjes@google.com>
To: Qianli Zhao <zhaoqianligood@gmail.com>, Roman Gushchin <guro@fb.com>
Cc: cl@linux.com, penberg@kernel.org, iamjoonsoo.kim@lge.com,
	 Andrew Morton <akpm@linux-foundation.org>,
	linux-mm@kvack.org,  linux-kernel@vger.kernel.org,
	clang-built-linux@googlegroups.com,  zhaoqianli@xiaomi.com
Subject: Re: [PATCH] mm: slab: Avoid the use of one-element array and use struct_size() helper
Date: Wed, 29 Jul 2020 16:40:42 -0700 (PDT)	[thread overview]
Message-ID: <alpine.DEB.2.23.453.2007291640140.3120473@chino.kir.corp.google.com> (raw)
In-Reply-To: <1596034214-15010-1-git-send-email-zhaoqianligood@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2201 bytes --]

On Wed, 29 Jul 2020, Qianli Zhao wrote:

> From: Qianli Zhao <zhaoqianli@xiaomi.com>
> 
> 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 <zhaoqianli@xiaomi.com>
> ---
>  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
> 
> 

  reply	other threads:[~2020-07-29 23:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-29 14:50 [PATCH] mm: slab: Avoid the use of one-element array and use struct_size() helper Qianli Zhao
2020-07-29 23:40 ` David Rientjes [this message]
2020-07-30  0:12 ` Gustavo A. R. Silva

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.23.453.2007291640140.3120473@chino.kir.corp.google.com \
    --to=rientjes@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=cl@linux.com \
    --cc=clang-built-linux@googlegroups.com \
    --cc=guro@fb.com \
    --cc=iamjoonsoo.kim@lge.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=penberg@kernel.org \
    --cc=zhaoqianli@xiaomi.com \
    --cc=zhaoqianligood@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).