[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20231017052322.2636-2-kkartik@nvidia.com>
Date: Tue, 17 Oct 2023 10:53:15 +0530
From: Kartik <kkartik@...dia.com>
To: <thierry.reding@...il.com>, <jonathanh@...dia.com>,
<keescook@...omium.org>, <andy@...nel.org>, <akpm@...ux-foundation.org>,
<arnd@...db.de>, <ulf.hansson@...aro.org>, <linus.walleij@...aro.org>,
<kkartik@...dia.com>, <pshete@...dia.com>, <petlozup@...dia.com>,
<frank.li@...o.com>, <robh@...nel.org>, <stefank@...dia.com>,
<pdeschrijver@...dia.com>, <christophe.jaillet@...adoo.fr>,
<linux-tegra@...r.kernel.org>, <linux-kernel@...r.kernel.org>,
<linux-hardening@...r.kernel.org>, <linux-mm@...ck.org>
Subject: [PATCH v7 1/8] mm/util: Introduce kmemdup_array()
Introduce kmemdup_array() API to duplicate `n` number of elements
from a given array. This internally uses kmemdup to allocate and duplicate
the `src` array.
Signed-off-by: Kartik <kkartik@...dia.com>
Acked-by: Kees Cook <keescook@...omium.org>
---
v4 -> v5:
* Simplify the implementation by just returning kmemdup(size_mul).
---
include/linux/string.h | 1 +
mm/util.c | 17 +++++++++++++++++
2 files changed, 18 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h
index 3c920b6d609b..fcfa1062c79b 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -180,6 +180,7 @@ extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
extern void *kmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2);
extern void *kvmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2);
extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
+extern void *kmemdup_array(const void *src, size_t element_size, size_t count, gfp_t gfp);
extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
extern void argv_free(char **argv);
diff --git a/mm/util.c b/mm/util.c
index 6eddd891198e..8801446140d6 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -135,6 +135,23 @@ void *kmemdup(const void *src, size_t len, gfp_t gfp)
}
EXPORT_SYMBOL(kmemdup);
+/**
+ * kmemdup_array - duplicate a given array.
+ *
+ * @src: array to duplicate.
+ * @element_size: size of each element of array.
+ * @count: number of elements to duplicate from array.
+ * @gfp: GFP mask to use.
+ *
+ * Return: duplicated array of @src or %NULL in case of error,
+ * result is physically contiguous. Use kfree() to free.
+ */
+void *kmemdup_array(const void *src, size_t element_size, size_t count, gfp_t gfp)
+{
+ return kmemdup(src, size_mul(element_size, count), gfp);
+}
+EXPORT_SYMBOL(kmemdup_array);
+
/**
* kvmemdup - duplicate region of memory
*
--
2.34.1
Powered by blists - more mailing lists