[PATCH v2 1/4] device: provide devm_krealloc_array()

From: Bartosz Golaszewski
Date: Mon Nov 02 2020 - 09:26:25 EST


From: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx>

When allocating an array of elements, users should check for
multiplication overflow or preferably use one of the provided helpers
like: devm_kmalloc_array().

This provides devm_krealloc_array() for users who want to reallocate
managed arrays.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx>
---
include/linux/device.h | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/include/linux/device.h b/include/linux/device.h
index 5ed101be7b2e..e77203faea55 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -226,6 +226,17 @@ static inline void *devm_kmalloc_array(struct device *dev,

return devm_kmalloc(dev, bytes, flags);
}
+static inline void *
+devm_krealloc_array(struct device *dev, void *ptr, size_t new_n,
+ size_t new_size, gfp_t gfp)
+{
+ size_t bytes;
+
+ if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
+ return NULL;
+
+ return devm_krealloc(dev, ptr, bytes, gfp);
+}
static inline void *devm_kcalloc(struct device *dev,
size_t n, size_t size, gfp_t flags)
{
--
2.29.1