Skip to content

Commit 3590053

Browse files
GustavoARSilvazx2c4
authored andcommitted
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>
1 parent 84349af commit 3590053

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/compat/compat.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,28 @@ static inline void __compat_kvfree(const void *addr)
515515
#define kvfree __compat_kvfree
516516
#endif
517517

518+
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0)
519+
#include <linux/vmalloc.h>
520+
#include <linux/mm.h>
521+
static inline void *__compat_kvmalloc_array(size_t n, size_t size, gfp_t flags)
522+
{
523+
if (n != 0 && SIZE_MAX / n < size)
524+
return NULL;
525+
return kvmalloc(n * size, flags);
526+
}
527+
#define kvmalloc_array __compat_kvmalloc_array
528+
#endif
529+
530+
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
531+
#include <linux/vmalloc.h>
532+
#include <linux/mm.h>
533+
static inline void *__compat_kvcalloc(size_t n, size_t size, gfp_t flags)
534+
{
535+
return kvmalloc_array(n, size, flags | __GFP_ZERO);
536+
}
537+
#define kvcalloc __compat_kvcalloc
538+
#endif
539+
518540
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 9)
519541
#include <linux/netdevice.h>
520542
#define priv_destructor destructor

src/ratelimiter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ int wg_ratelimiter_init(void)
188188
(1U << 14) / sizeof(struct hlist_head)));
189189
max_entries = table_size * 8;
190190

191-
table_v4 = kvzalloc(table_size * sizeof(*table_v4), GFP_KERNEL);
191+
table_v4 = kvcalloc(table_size, sizeof(*table_v4), GFP_KERNEL);
192192
if (unlikely(!table_v4))
193193
goto err_kmemcache;
194194

195195
#if IS_ENABLED(CONFIG_IPV6)
196-
table_v6 = kvzalloc(table_size * sizeof(*table_v6), GFP_KERNEL);
196+
table_v6 = kvcalloc(table_size, sizeof(*table_v6), GFP_KERNEL);
197197
if (unlikely(!table_v6)) {
198198
kvfree(table_v4);
199199
goto err_kmemcache;

0 commit comments

Comments
 (0)