The patch titled Subject: mm: slab: avoid the use of one-element array and use struct_size() helper has been added to the -mm tree. Its filename is mm-slab-avoid-the-use-of-one-element-array-and-use-struct_size-helper.patch This patch should soon appear at http://ozlabs.org/~akpm/mmots/broken-out/mm-slab-avoid-the-use-of-one-element-array-and-use-struct_size-helper.patch and later at http://ozlabs.org/~akpm/mmotm/broken-out/mm-slab-avoid-the-use-of-one-element-array-and-use-struct_size-helper.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Qianli Zhao <zhaoqianli@xxxxxxxxxx> Subject: mm: slab: avoid the use of one-element array and use struct_size() helper 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 Link: http://lkml.kernel.org/r/1596034214-15010-1-git-send-email-zhaoqianligood@xxxxxxxxx Signed-off-by: Qianli Zhao <zhaoqianli@xxxxxxxxxx> Cc: Christoph Lameter <cl@xxxxxxxxx> Cc: Pekka Enberg <penberg@xxxxxxxxxx> Cc: David Rientjes <rientjes@xxxxxxxxxx> Cc: Joonsoo Kim <iamjoonsoo.kim@xxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- mm/slab.h | 2 +- mm/slab_common.c | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) --- a/mm/slab_common.c~mm-slab-avoid-the-use-of-one-element-array-and-use-struct_size-helper +++ a/mm/slab_common.c @@ -168,9 +168,7 @@ static int init_memcg_params(struct kmem 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; @@ -201,8 +199,7 @@ static int update_memcg_params(struct km { 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; --- a/mm/slab.h~mm-slab-avoid-the-use-of-one-element-array-and-use-struct_size-helper +++ a/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[]; }; /* _ Patches currently in -mm which might be from zhaoqianli@xxxxxxxxxx are mm-slab-avoid-the-use-of-one-element-array-and-use-struct_size-helper.patch