Message ID | 20201027121725.24660-2-brgl@bgdev.pl |
---|---|
State | Not Applicable |
Delegated to: | David Miller |
Headers | show |
Series | slab: provide and use krealloc_array() | expand |
Context | Check | Description |
---|---|---|
jkicinski/cover_letter | success | Link |
jkicinski/fixes_present | success | Link |
jkicinski/patch_count | success | Link |
jkicinski/tree_selection | success | Guessed tree name to be net-next |
jkicinski/subject_prefix | success | Link |
jkicinski/source_inline | success | Was 0 now: 0 |
jkicinski/verify_signedoff | success | Link |
jkicinski/module_param | success | Was 0 now: 0 |
jkicinski/build_32bit | fail | Errors and warnings before: 30429 this patch: 30429 |
jkicinski/kdoc | success | Errors and warnings before: 0 this patch: 0 |
jkicinski/verify_fixes | success | Link |
jkicinski/checkpatch | fail | Link |
jkicinski/build_allmodconfig_warn | success | Errors and warnings before: 28729 this patch: 28729 |
jkicinski/header_inline | success | Link |
jkicinski/stable | success | Stable not CCed |
On 10/27/20 1:17 PM, Bartosz Golaszewski wrote: > From: Bartosz Golaszewski <bgolaszewski@baylibre.com> > > 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. > > Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com> Makes sense. Acked-by: Vlastimil Babka <vbabka@suse.cz> > --- > include/linux/slab.h | 11 +++++++++++ > 1 file changed, 11 insertions(+) > > diff --git a/include/linux/slab.h b/include/linux/slab.h > index dd6897f62010..0e6683affee7 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -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. >
On Tue, Oct 27, 2020 at 01:17:18PM +0100, Bartosz Golaszewski wrote: > From: Bartosz Golaszewski <bgolaszewski@baylibre.com> > > 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. > > Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com> > --- > include/linux/slab.h | 11 +++++++++++ > 1 file changed, 11 insertions(+) > > diff --git a/include/linux/slab.h b/include/linux/slab.h > index dd6897f62010..0e6683affee7 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -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? > +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. > -- > 2.29.1 > >
diff --git a/include/linux/slab.h b/include/linux/slab.h index dd6897f62010..0e6683affee7 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -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.