aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustavo A. R. Silva <gustavoars@kernel.org>2021-09-28 17:17:05 -0500
committerJason A. Donenfeld <Jason@zx2c4.com>2021-12-03 23:24:03 +0100
commit5325bc82aa7b6d60fbda2ad4f64b1c676cea01fe (patch)
treefa1dc491b5a3d86e123ab1401518f88ec2831bc3
parente44c78cb66b78c24c2e54b862665db15fca41289 (diff)
downloadwireguard-linux-compat-5325bc82aa7b6d60fbda2ad4f64b1c676cea01fe.tar.gz
ratelimiter: use kvcalloc() instead of kvzalloc()
Use 2-factor argument form kvcalloc() instead of kvzalloc(). Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--src/compat/compat.h22
-rw-r--r--src/ratelimiter.c4
2 files changed, 24 insertions, 2 deletions
diff --git a/src/compat/compat.h b/src/compat/compat.h
index 0270a28..cd1894e 100644
--- a/src/compat/compat.h
+++ b/src/compat/compat.h
@@ -515,6 +515,28 @@ static inline void __compat_kvfree(const void *addr)
#define kvfree __compat_kvfree
#endif
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0)
+#include <linux/vmalloc.h>
+#include <linux/mm.h>
+static inline void *__compat_kvmalloc_array(size_t n, size_t size, gfp_t flags)
+{
+ if (n != 0 && SIZE_MAX / n < size)
+ return NULL;
+ return kvmalloc(n * size, flags);
+}
+#define kvmalloc_array __compat_kvmalloc_array
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
+#include <linux/vmalloc.h>
+#include <linux/mm.h>
+static inline void *__compat_kvcalloc(size_t n, size_t size, gfp_t flags)
+{
+ return kvmalloc_array(n, size, flags | __GFP_ZERO);
+}
+#define kvcalloc __compat_kvcalloc
+#endif
+
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 9)
#include <linux/netdevice.h>
#define priv_destructor destructor
diff --git a/src/ratelimiter.c b/src/ratelimiter.c
index e33ec72..ecee41f 100644
--- a/src/ratelimiter.c
+++ b/src/ratelimiter.c
@@ -188,12 +188,12 @@ int wg_ratelimiter_init(void)
(1U << 14) / sizeof(struct hlist_head)));
max_entries = table_size * 8;
- table_v4 = kvzalloc(table_size * sizeof(*table_v4), GFP_KERNEL);
+ table_v4 = kvcalloc(table_size, sizeof(*table_v4), GFP_KERNEL);
if (unlikely(!table_v4))
goto err_kmemcache;
#if IS_ENABLED(CONFIG_IPV6)
- table_v6 = kvzalloc(table_size * sizeof(*table_v6), GFP_KERNEL);
+ table_v6 = kvcalloc(table_size, sizeof(*table_v6), GFP_KERNEL);
if (unlikely(!table_v6)) {
kvfree(table_v4);
goto err_kmemcache;