slab: provide and use krealloc_array()
Subsystems (21)
v3 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.
v1 -> v2:
- added a kernel doc for krealloc_array()
- mentioned krealloc et al in the docs
- collected review tags
v2 -> v3:
- add a patch improving krealloc()'s kerneldoc
- fix a typo
- improve .rst doc
- tweak line breaks
Bartosz Golaszewski (9):
mm: slab: clarify krealloc()'s behavior with __GFP_ZERO
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()
Documentation/core-api/memory-allocation.rst | 4 ++++ drivers/dma-buf/sync_file.c | 3 +-- 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 | 18 ++++++++++++++++++ mm/slab_common.c | 6 +++--- sound/core/pcm_lib.c | 4 ++-- 10 files changed, 36 insertions(+), 13 deletions(-)
Signature · 1 line
2.29.1
PATCH v3 1/9 mm: slab: clarify krealloc()'s behavior with __GFP_ZERO
From: Bartosz Golaszewski <[email protected]>
__GFP_ZERO is ignored by krealloc() (unless we fall-back to kmalloc() path, in which case it's honored). Point that out in the kerneldoc.
--- mm/slab_common.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/slab_common.c +3 -3
@@ -1091,9 +1091,9 @@ static __always_inline void *__do_krealloc(const void *p, size_t new_size,* @flags: the type of memory to allocate. * * The contents of the object pointed to are preserved up to the -* lesser of the new and old sizes. If @p is %NULL, krealloc() -* behaves exactly like kmalloc(). If @new_size is 0 and @p is not a -* %NULL pointer, the object pointed to is freed. +* lesser of the new and old sizes (__GFP_ZERO flag is effectively ignored). +* If @p is %NULL, krealloc() behaves exactly like kmalloc(). If @new_size +* is 0 and @p is not a %NULL pointer, the object pointed to is freed. * * Return: pointer to the allocated memory or %NULL in case of error */
Signature · 1 line
2.29.1
PATCH v3 2/9 mm: slab: provide krealloc_array()
In reply to Bartosz GolaszewskiFrom: 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.
While at it: add some documentation regarding krealloc.
--- Documentation/core-api/memory-allocation.rst | 4 ++++ include/linux/slab.h | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+)
Documentation/core-api/memory-allocation.rst +4 -0
@@ -147,6 +147,10 @@ The address of a chunk allocated with `kmalloc` is aligned to at least ARCH_KMALLOC_MINALIGN bytes. For sizes which are a power of two, the alignment is also guaranteed to be at least the respective size. +Chunks allocated with kmalloc() can be resized with krealloc(). Similarly +to kmalloc_array(): a helper for resizing arrays is provided in the form of +krealloc_array(). + For large allocations you can use vmalloc() and vzalloc(), or directly request pages from the page allocator. The memory allocated by `vmalloc` and related functions is not physically contiguous.
include/linux/slab.h +18 -0
@@ -592,6 +592,24 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)return __kmalloc (bytes, flags); } +/** +* krealloc_array - reallocate memory for an array. +* @p: pointer to the memory chunk to reallocate +* @new_n: new number of elements to alloc +* @new_size: new size of a single member of the array +* @flags: the type of memory to allocate (see kmalloc) +*/ +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 v3 3/9 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 v3 4/9 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 v3 5/9 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
Re: [PATCH v3 5/9] pinctrl: use krealloc_array()
Quoted 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]>Reviewed-by Andy Shevchenko <[email protected]>
Yours,
Linus Walleij
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v3 6/9 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 v3 7/9 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 v3 8/9 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 v3 9/9 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 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/dma-buf/sync_file.c +1 -2
@@ -270,8 +270,7 @@ 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