slab: provide and use krealloc_array()
Subsystems (19)
cover slab: provide and use krealloc_array()
From: Bartosz Golaszewski <[email protected]>
Andy brought to my attention the fact that users allocating an array of equally sized elements should check if the size multiplication doesn't overflow. This is why we have helpers like kmalloc_array().
However we don't have krealloc_array() equivalent and there are many users who do their own multiplication when calling krealloc() for arrays.
This series provides krealloc_array() and uses it in a couple places.
A separate series will follow adding devm_krealloc_array() which is needed in the xilinx adc driver.
Bartosz Golaszewski (8):
mm: slab: provide krealloc_array()
ALSA: pcm: use krealloc_array()
vhost: vringh: use krealloc_array()
pinctrl: use krealloc_array()
edac: ghes: use krealloc_array()
drm: atomic: use krealloc_array()
hwtracing: intel: use krealloc_array()
dma-buf: use krealloc_array()
drivers/dma-buf/sync_file.c | 4 ++-- drivers/edac/ghes_edac.c | 4 ++-- drivers/gpu/drm/drm_atomic.c | 3 ++- drivers/hwtracing/intel_th/msu.c | 2 +- drivers/pinctrl/pinctrl-utils.c | 2 +- drivers/vhost/vringh.c | 3 ++- include/linux/slab.h | 11 +++++++++++ sound/core/pcm_lib.c | 4 ++-- 8 files changed, 23 insertions(+), 10 deletions(-)
Signature · 1 line
2.29.1
PATCH 1/8 mm: slab: provide krealloc_array()
From: Bartosz Golaszewski <[email protected]>
When allocating an array of elements, users should check for multiplication overflow or preferably use one of the provided helpers
like: kmalloc_array().
There's no krealloc_array() counterpart but there are many users who use regular krealloc() to reallocate arrays. Let's provide an actual krealloc_array() implementation.
--- include/linux/slab.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/linux/slab.h +11 -0
@@ -592,6 +592,17 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)return __kmalloc (bytes, flags); } +static __must_check inline void * +krealloc_array (void *p,size_t new_n,size_t new_size,gfp_t flags) +{ +size_t bytes; + +if (unlikely (check_mul_overflow (new_n, new_size, &bytes))) +return NULL ; + +return krealloc (p, bytes, flags); +} +/** * kcalloc - allocate memory for an array. The memory is set to zero. * @n: number of elements.
Signature · 1 line
2.29.1
PATCH 3/8 vhost: vringh: use krealloc_array()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
--- drivers/vhost/vringh.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/vhost/vringh.c +2 -1
@@ -198,7 +198,8 @@ static int resize_iovec(struct vringh_kiov *iov, gfp_t gfp) flag = (iov->max_num &VRINGH_IOV_ALLOCATED );if (flag) - new =krealloc (iov->iov, new_num *sizeof (struct iovec ), gfp); + new =krealloc_array (iov->iov, new_num, +sizeof (struct iovec ), gfp);else { new =kmalloc_array (new_num,sizeof (struct iovec ), gfp);if (new) {
Signature · 1 line
2.29.1
PATCH 2/8 ALSA: pcm: use krealloc_array()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
--- sound/core/pcm_lib.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
sound/core/pcm_lib.c +2 -2
@@ -1129,8 +1129,8 @@ int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,if (constrs->rules_num >= constrs->rules_all) {struct snd_pcm_hw_rule *new;unsigned int new_rules = constrs->rules_all +16 ; - new =krealloc (constrs->rules, new_rules *sizeof (*c), -GFP_KERNEL ); + new =krealloc_array (constrs->rules, new_rules, +sizeof (*c),GFP_KERNEL );if (!new) {va_end (args);return -ENOMEM ;
Signature · 1 line
2.29.1
PATCH 4/8 pinctrl: use krealloc_array()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
--- drivers/pinctrl/pinctrl-utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pinctrl/pinctrl-utils.c +1 -1
@@ -39,7 +39,7 @@ int pinctrl_utils_reserve_map(struct pinctrl_dev *pctldev,if (old_num >= new_num)return 0 ; - new_map =krealloc (*map,sizeof (*new_map) * new_num,GFP_KERNEL ); + new_map =krealloc_array (*map, new_num,sizeof (*new_map),GFP_KERNEL );if (!new_map) {dev_err (pctldev->dev,"krealloc(map) failed\n" );return -ENOMEM ;
Signature · 1 line
2.29.1
PATCH 5/8 edac: ghes: use krealloc_array()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
--- drivers/edac/ghes_edac.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/edac/ghes_edac.c +2 -2
@@ -207,8 +207,8 @@ static void enumerate_dimms(const struct dmi_header *dh, void *arg)if (!hw->num_dimms || !(hw->num_dimms %16 )) {struct dimm_info *new; - new =krealloc (hw->dimms, (hw->num_dimms +16 ) *sizeof (struct dimm_info ), -GFP_KERNEL ); + new =krealloc_array (hw->dimms, hw->num_dimms +16 , +sizeof (struct dimm_info ),GFP_KERNEL );if (!new) {WARN_ON_ONCE (1 );return ;
Signature · 1 line
2.29.1
PATCH 6/8 drm: atomic: use krealloc_array()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
--- drivers/gpu/drm/drm_atomic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_atomic.c +2 -1
@@ -960,7 +960,8 @@ drm_atomic_get_connector_state(struct drm_atomic_state *state,struct __drm_connnectors_state *c;int alloc =max (index +1 , config->num_connector); - c =krealloc (state->connectors, alloc *sizeof (*state->connectors),GFP_KERNEL ); + c =krealloc_array (state->connectors, alloc, +sizeof (*state->connectors),GFP_KERNEL );if (!c)return ERR_PTR (-ENOMEM );
Signature · 1 line
2.29.1
PATCH 7/8 hwtracing: intel: use krealloc_array()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
--- drivers/hwtracing/intel_th/msu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwtracing/intel_th/msu.c +1 -1
@@ -2002,7 +2002,7 @@ nr_pages_store(struct device *dev, struct device_attribute *attr, } nr_wins++; - rewin =krealloc (win,sizeof (*win) * nr_wins,GFP_KERNEL ); + rewin =krealloc_array (win, nr_wins,sizeof (*win),GFP_KERNEL );if (!rewin) {kfree (win);return -ENOMEM ;
Signature · 1 line
2.29.1
PATCH 8/8 dma-buf: use krealloc_array()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
--- drivers/dma-buf/sync_file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma-buf/sync_file.c +2 -2
@@ -270,8 +270,8 @@ static struct sync_file *sync_file_merge(const char *name, struct sync_file *a, fences[i++] =dma_fence_get (a_fences[0 ]);if (num_fences > i) { - nfences =krealloc (fences, i *sizeof (*fences), -GFP_KERNEL ); + nfences =krealloc_array (fences, i, +sizeof (*fences),GFP_KERNEL );if (!nfences) goto err;
Signature · 1 line
2.29.1
Re: [PATCH 8/8] dma-buf: use krealloc_array()
Am 27.10.20 um 13:17 schrieb Bartosz Golaszewski:
Quoted 6 lines
From: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
Signed-off-by Bartosz Golaszewski <[email protected]>
Quoted 9 lines
--- drivers/dma-buf/sync_file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/dma-buf/sync_file.c +0
-0
drivers/dma-buf/sync_file.c static struct sync_file *sync_file_merge(const char *name, struct sync_file *a, · line 277
Quoted 10 lines
drivers/dma-buf/sync_file.c +2
-2@@ -270,8 +270,8 @@ static struct sync_file *sync_file_merge(const char *name, struct sync_file *a, fences[i++] =dma_fence_get (a_fences[0 ]);if (num_fences > i) { - nfences =krealloc (fences, i *sizeof (*fences), -GFP_KERNEL ); + nfences =krealloc_array (fences, i, +sizeof (*fences),GFP_KERNEL );if (!nfences) goto err;
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH 2/8] ALSA: pcm: use krealloc_array()
In reply to Bartosz GolaszewskiQuoted 7 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
Signed-off-by Bartosz Golaszewski <[email protected]>
thanks,
Takashi
Re: [PATCH 3/8] vhost: vringh: use krealloc_array()
In reply to Bartosz GolaszewskiQuoted 6 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
Signed-off-by Bartosz Golaszewski <[email protected]>
No problem with the patch, it does introduce some symmetry in the code.
Quoted 20 lines
--- drivers/vhost/vringh.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/vhost/vringh.c +2
-1@@ -198,7 +198,8 @@ static int resize_iovec(struct vringh_kiov *iov, gfp_t gfp) flag = (iov->max_num &VRINGH_IOV_ALLOCATED );if (flag) - new =krealloc (iov->iov, new_num *sizeof (struct iovec ), gfp); + new =krealloc_array (iov->iov, new_num, +sizeof (struct iovec ), gfp);else { new =kmalloc_array (new_num,sizeof (struct iovec ), gfp);if (new) {Signature · 1 line
2.29.1
Re: [PATCH 3/8] vhost: vringh: use krealloc_array()
Quoted 9 lines · Michael S. Tsirkin
Quoted 6 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
Signed-off-by Bartosz Golaszewski <[email protected]>No problem with the patch, it does introduce some symmetry in the code.
Perhaps more symmetry by using kmemdup
--- drivers/vhost/vringh.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/vhost/vringh.c +10 -13
@@ -191,26 +191,23 @@ static int move_to_indirect(const struct vringh *vrh,static int resize_iovec (struct vringh_kiov *iov,gfp_t gfp) {struct kvec *new; -unsigned int flag, new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED ) *2 ; +size_t new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED ) *2 ; +size_t size;if (new_num <8 ) new_num =8 ; - flag = (iov->max_num &VRINGH_IOV_ALLOCATED ); -if (flag) - new =krealloc (iov->iov, new_num *sizeof (struct iovec ), gfp); -else { - new =kmalloc_array (new_num,sizeof (struct iovec ), gfp); -if (new) { -memcpy (new, iov->iov, - iov->max_num *sizeof (struct iovec )); - flag =VRINGH_IOV_ALLOCATED ; - } - } +if (unlikely (check_mul_overflow (new_num,sizeof (struct iovec ), &size))) +return -ENOMEM ; + +if (iov->max_num &VRINGH_IOV_ALLOCATED ) + new =krealloc (iov->iov, size, gfp); +else + new =kmemdup (iov->iov, size, gfp);if (!new)return -ENOMEM ; iov->iov = new; - iov->max_num = (new_num | flag); + iov->max_num = new_num |VRINGH_IOV_ALLOCATED ;return 0 ; }
Re: [PATCH 3/8] vhost: vringh: use krealloc_array()
Quoted 36 lines · Joe Perches
Quoted 9 lines · Michael S. Tsirkin
Quoted 6 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
Signed-off-by Bartosz Golaszewski <[email protected]>No problem with the patch, it does introduce some symmetry in the code.
Perhaps more symmetry by using kmemdup
--- drivers/vhost/vringh.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)drivers/vhost/vringh.c +2
-5@@ -191,26 +191,23 @@ static int move_to_indirect(const struct vringh *vrh,static int resize_iovec (struct vringh_kiov *iov,gfp_t gfp) {struct kvec *new; -unsigned int flag, new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED ) *2 ; +size_t new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED ) *2 ; +size_t size;if (new_num <8 ) new_num =8 ; - flag = (iov->max_num &VRINGH_IOV_ALLOCATED ); -if (flag) - new =krealloc (iov->iov, new_num *sizeof (struct iovec ), gfp); -else {
drivers/vhost/vringh.c static int move_to_indirect(const struct vringh *vrh, · line 202
Quoted 10 lines
drivers/vhost/vringh.c +3
-7- new =kmalloc_array (new_num,sizeof (struct iovec ), gfp); -if (new) { -memcpy (new, iov->iov, - iov->max_num *sizeof (struct iovec )); - flag =VRINGH_IOV_ALLOCATED ; - } - } +if (unlikely (check_mul_overflow (new_num,sizeof (struct iovec ), &size))) +return -ENOMEM ; +
The whole point of using helpers such as kmalloc_array() is not doing these checks manually.
Bartosz
Quoted 14 lines
drivers/vhost/vringh.c +5
-1+if (iov->max_num &VRINGH_IOV_ALLOCATED ) + new =krealloc (iov->iov, size, gfp); +else + new =kmemdup (iov->iov, size, gfp);if (!new)return -ENOMEM ; iov->iov = new; - iov->max_num = (new_num | flag); + iov->max_num = new_num |VRINGH_IOV_ALLOCATED ;return 0 ; }
Re: [PATCH 3/8] vhost: vringh: use krealloc_array()
Quoted 42 lines · Bartosz Golaszewski
Quoted 41 lines · Joe Perches
Quoted 9 lines · Michael S. Tsirkin
Quoted 6 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
Signed-off-by Bartosz Golaszewski <[email protected]>No problem with the patch, it does introduce some symmetry in the code.
Perhaps more symmetry by using kmemdup
---
drivers/vhost/vringh.c | 23 ++++++++++-------------1 file changed, 10 insertions(+), 13 deletions(-)drivers/vhost/vringh.c +2
-10@@ -191,26 +191,23 @@ static int move_to_indirect(const struct vringh *vrh, static int resize_iovec(struct vringh_kiov *iov, gfp_t gfp) { struct kvec *new; -unsigned int flag, new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED ) *2 ; +size_t new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED ) *2 ; +size_t size; if (new_num < 8) new_num = 8; - flag = (iov->max_num &VRINGH_IOV_ALLOCATED ); -if (flag) - new =krealloc (iov->iov, new_num *sizeof (struct iovec ), gfp); -else { - new =kmalloc_array (new_num,sizeof (struct iovec ), gfp); -if (new) { -memcpy (new, iov->iov, - iov->max_num *sizeof (struct iovec )); - flag =VRINGH_IOV_ALLOCATED ;
Quoted 8 lines
drivers/vhost/vringh.c static int move_to_indirect(const struct vringh *vrh, · line 202
Quoted 5 lines
drivers/vhost/vringh.c +3
-2- } - } +if (unlikely (check_mul_overflow (new_num,sizeof (struct iovec ), &size))) +return -ENOMEM ; +The whole point of using helpers such as
kmalloc_array()is not doing these checks manually.
Tradeoffs for in readability for overflow and not mistyping or doing the multiplication of iov->max_num * sizeof(struct iovec) twice.
Just fyi:
the realloc doesn't do a multiplication overflow test as written so the suggestion is slightly more resistant to defect.
Re: [PATCH 3/8] vhost: vringh: use krealloc_array()
Quoted 53 lines · Joe Perches
Quoted 42 lines · Bartosz Golaszewski
Quoted 41 lines · Joe Perches
Quoted 9 lines · Michael S. Tsirkin
Quoted 6 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
Signed-off-by Bartosz Golaszewski <[email protected]>No problem with the patch, it does introduce some symmetry in the code.
Perhaps more symmetry by using kmemdup
--- drivers/vhost/vringh.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)drivers/vhost/vringh.c +2
-10@@ -191,26 +191,23 @@ static int move_to_indirect(const struct vringh *vrh,static int resize_iovec (struct vringh_kiov *iov,gfp_t gfp) {struct kvec *new; -unsigned int flag, new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED ) *2 ; +size_t new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED ) *2 ; +size_t size;if (new_num <8 ) new_num =8 ; - flag = (iov->max_num &VRINGH_IOV_ALLOCATED ); -if (flag) - new =krealloc (iov->iov, new_num *sizeof (struct iovec ), gfp); -else { - new =kmalloc_array (new_num,sizeof (struct iovec ), gfp); -if (new) { -memcpy (new, iov->iov, - iov->max_num *sizeof (struct iovec )); - flag =VRINGH_IOV_ALLOCATED ;Quoted 8 lines
drivers/vhost/vringh.c static int move_to_indirect(const struct vringh *vrh, · line 202
Quoted 5 lines
drivers/vhost/vringh.c +3
-2- } - } +if (unlikely (check_mul_overflow (new_num,sizeof (struct iovec ), &size))) +return -ENOMEM ; +The whole point of using helpers such as
kmalloc_array()is not doing these checks manually.
Quoted 3 lines
Tradeoffs for in readability for overflow and not mistyping or doing the multiplication of
iov->max_num* sizeof(struct iovec) twice.
It's out of scope for this series - I want to add users for krealloc_array(), not refactor code I don't really know. If the maintainer of this bit objects, it can be dropped.
Quoted 5 lines
Just fyi:
the realloc doesn't do a multiplication overflow test as written so the suggestion is slightly more resistant to defect.
I'm not sure what your point is. I used krealloc_array() exactly for this reason - to add the overflow test.
BTW I suppose kmalloc_array() here can be replaced with krealloc_array() if the original pointer is NULL the first time it's called.
Bartosz
Re: [PATCH 1/8] mm: slab: provide krealloc_array()
In reply to Bartosz GolaszewskiQuoted 11 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
When allocating an array of elements, users should check for multiplication overflow or preferably use one of the provided helpers
like:kmalloc_array().There's no
krealloc_array()counterpart but there are many users who use regularkrealloc()to reallocate arrays. Let's provide an actualkrealloc_array()implementation.Signed-off-by Bartosz Golaszewski <[email protected]>
Makes sense.
Quoted 17 lines
--- include/linux/slab.h | 11 +++++++++++ 1 file changed, 11 insertions(+)include/linux/slab.h +4
-0@@ -592,6 +592,17 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)return __kmalloc (bytes, flags); } +static __must_check inline void * +krealloc_array (void *p,size_t new_n,size_t new_size,gfp_t flags) +{ +size_t bytes;
include/linux/slab.h static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) · line 608
Quoted 10 lines
include/linux/slab.h +7
-0+ +if (unlikely (check_mul_overflow (new_n, new_size, &bytes))) +return NULL ; + +return krealloc (p, bytes, flags); +} +/** * kcalloc - allocate memory for an array. The memory is set to zero. * @n: number of elements.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH 5/8] edac: ghes: use krealloc_array()
In reply to Bartosz GolaszewskiQuoted 16 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
Signed-off-by Bartosz Golaszewski <[email protected]>--- drivers/edac/ghes_edac.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/edac/ghes_edac.c +0
-0
drivers/edac/ghes_edac.c static void enumerate_dimms(const struct dmi_header *dh, void *arg) · line 214
Quoted 10 lines
drivers/edac/ghes_edac.c +2
-2@@ -207,8 +207,8 @@ static void enumerate_dimms(const struct dmi_header *dh, void *arg)if (!hw->num_dimms || !(hw->num_dimms %16 )) {struct dimm_info *new; - new =krealloc (hw->dimms, (hw->num_dimms +16 ) *sizeof (struct dimm_info ), -GFP_KERNEL ); + new =krealloc_array (hw->dimms, hw->num_dimms +16 , +sizeof (struct dimm_info ),GFP_KERNEL );if (!new) {WARN_ON_ONCE (1 );return ;
Sure, why not.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH 6/8] drm: atomic: use krealloc_array()
In reply to Bartosz GolaszewskiQuoted 6 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
Signed-off-by Bartosz Golaszewski <[email protected]>
I don't expect conflicts with this going through some other tree, so please make that happen. Or resend once I can apply this to drm trees.
Thanks, Daniel
Quoted 21 lines
--- drivers/gpu/drm/drm_atomic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_atomic.c +2
-1@@ -960,7 +960,8 @@ drm_atomic_get_connector_state(struct drm_atomic_state *state,struct __drm_connnectors_state *c;int alloc =max (index +1 , config->num_connector); - c =krealloc (state->connectors, alloc *sizeof (*state->connectors),GFP_KERNEL ); + c =krealloc_array (state->connectors, alloc, +sizeof (*state->connectors),GFP_KERNEL );if (!c)return ERR_PTR (-ENOMEM );Signature · 1 line
2.29.1
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
Re: [PATCH 1/8] mm: slab: provide krealloc_array()
In reply to Bartosz GolaszewskiQuoted 20 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
When allocating an array of elements, users should check for multiplication overflow or preferably use one of the provided helpers
like:kmalloc_array().There's no
krealloc_array()counterpart but there are many users who use regularkrealloc()to reallocate arrays. Let's provide an actualkrealloc_array()implementation.Signed-off-by Bartosz Golaszewski <[email protected]>--- include/linux/slab.h | 11 +++++++++++ 1 file changed, 11 insertions(+)include/linux/slab.h +0
-0
include/linux/slab.h static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) · line 594
Quoted 3 lines
include/linux/slab.h +0
-0@@ -592,6 +592,17 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)return __kmalloc (bytes, flags); }
Can you please add kernel-doc here and a word or two about this function to Documentation/core-api/memory-allocation.rst?
Quoted 18 lines
include/linux/slab.h +11
-0+static __must_check inline void * +krealloc_array (void *p,size_t new_n,size_t new_size,gfp_t flags) +{ +size_t bytes; + +if (unlikely (check_mul_overflow (new_n, new_size, &bytes))) +return NULL ; + +return krealloc (p, bytes, flags); +} +/** * kcalloc - allocate memory for an array. The memory is set to zero. * @n: number of elements. Signature · 1 line
2.29.1
Signature · 6 lines
Sincerely yours, Mike. _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Andy Shevchenko
Reviewed-byRe: [PATCH 4/8] pinctrl: use krealloc_array()
In reply to Andy ShevchenkoQuoted 5 lines · Andy Shevchenko
Quoted 4 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
After addressing a nit in previous message:
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 4/8] pinctrl: use krealloc_array()
In reply to Bartosz GolaszewskiQuoted 15 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Use the helper that checks for overflows internally instead of manually calculating the size of the new array.
Signed-off-by Bartosz Golaszewski <[email protected]>--- drivers/pinctrl/pinctrl-utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/pinctrl/pinctrl-utils.c +0
-0
drivers/pinctrl/pinctrl-utils.c int pinctrl_utils_reserve_map(struct pinctrl_dev *pctldev, · line 44
Quoted 7 lines
drivers/pinctrl/pinctrl-utils.c +1
-1@@ -39,7 +39,7 @@ int pinctrl_utils_reserve_map(struct pinctrl_dev *pctldev,if (old_num >= new_num)return 0 ; - new_map =krealloc (*map,sizeof (*new_map) * new_num,GFP_KERNEL ); + new_map =krealloc_array (*map, new_num,sizeof (*new_map),GFP_KERNEL );if (!new_map) {dev_err (pctldev->dev,"krealloc(map) failed\n" );
...and here?
Quoted 4 lines
drivers/pinctrl/pinctrl-utils.c +0
-0return -ENOMEM ;Signature · 1 line
2.29.1
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 6 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Andy brought to my attention the fact that users allocating an array of equally sized elements should check if the size multiplication doesn't overflow. This is why we have helpers like
kmalloc_array().
Quoted 7 lines
However we don't have
krealloc_array()equivalent and there are many users who do their own multiplication when callingkrealloc()for arrays.This series provides
krealloc_array()and uses it in a couple places.A separate series will follow adding
devm_krealloc_array()which is needed in the xilinx adc driver.
The series:
I really like this.
Yours,
Linus Walleij
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel