Skip to content

Commit dc680de

Browse files
zx2c4davem330
authored andcommitted
wireguard: allowedips: allocate nodes in kmem_cache
The previous commit moved from O(n) to O(1) for removal, but in the process introduced an additional pointer member to a struct that increased the size from 60 to 68 bytes, putting nodes in the 128-byte slab. With deployed systems having as many as 2 million nodes, this represents a significant doubling in memory usage (128 MiB -> 256 MiB). Fix this by using our own kmem_cache, that's sized exactly right. This also makes wireguard's memory usage more transparent in tools like slabtop and /proc/slabinfo. Fixes: e7096c1 ("net: WireGuard secure network tunnel") Suggested-by: Arnd Bergmann <[email protected]> Suggested-by: Matthew Wilcox <[email protected]> Cc: [email protected] Signed-off-by: Jason A. Donenfeld <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f634f41 commit dc680de

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

drivers/net/wireguard/allowedips.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "allowedips.h"
77
#include "peer.h"
88

9+
static struct kmem_cache *node_cache;
10+
911
static void swap_endian(u8 *dst, const u8 *src, u8 bits)
1012
{
1113
if (bits == 32) {
@@ -40,6 +42,11 @@ static void push_rcu(struct allowedips_node **stack,
4042
}
4143
}
4244

45+
static void node_free_rcu(struct rcu_head *rcu)
46+
{
47+
kmem_cache_free(node_cache, container_of(rcu, struct allowedips_node, rcu));
48+
}
49+
4350
static void root_free_rcu(struct rcu_head *rcu)
4451
{
4552
struct allowedips_node *node, *stack[128] = {
@@ -49,7 +56,7 @@ static void root_free_rcu(struct rcu_head *rcu)
4956
while (len > 0 && (node = stack[--len])) {
5057
push_rcu(stack, node->bit[0], &len);
5158
push_rcu(stack, node->bit[1], &len);
52-
kfree(node);
59+
kmem_cache_free(node_cache, node);
5360
}
5461
}
5562

@@ -164,7 +171,7 @@ static int add(struct allowedips_node __rcu **trie, u8 bits, const u8 *key,
164171
return -EINVAL;
165172

166173
if (!rcu_access_pointer(*trie)) {
167-
node = kzalloc(sizeof(*node), GFP_KERNEL);
174+
node = kmem_cache_zalloc(node_cache, GFP_KERNEL);
168175
if (unlikely(!node))
169176
return -ENOMEM;
170177
RCU_INIT_POINTER(node->peer, peer);
@@ -180,7 +187,7 @@ static int add(struct allowedips_node __rcu **trie, u8 bits, const u8 *key,
180187
return 0;
181188
}
182189

183-
newnode = kzalloc(sizeof(*newnode), GFP_KERNEL);
190+
newnode = kmem_cache_zalloc(node_cache, GFP_KERNEL);
184191
if (unlikely(!newnode))
185192
return -ENOMEM;
186193
RCU_INIT_POINTER(newnode->peer, peer);
@@ -213,10 +220,10 @@ static int add(struct allowedips_node __rcu **trie, u8 bits, const u8 *key,
213220
return 0;
214221
}
215222

216-
node = kzalloc(sizeof(*node), GFP_KERNEL);
223+
node = kmem_cache_zalloc(node_cache, GFP_KERNEL);
217224
if (unlikely(!node)) {
218225
list_del(&newnode->peer_list);
219-
kfree(newnode);
226+
kmem_cache_free(node_cache, newnode);
220227
return -ENOMEM;
221228
}
222229
INIT_LIST_HEAD(&node->peer_list);
@@ -306,7 +313,7 @@ void wg_allowedips_remove_by_peer(struct allowedips *table,
306313
if (child)
307314
child->parent_bit = node->parent_bit;
308315
*rcu_dereference_protected(node->parent_bit, lockdep_is_held(lock)) = child;
309-
kfree_rcu(node, rcu);
316+
call_rcu(&node->rcu, node_free_rcu);
310317

311318
/* TODO: Note that we currently don't walk up and down in order to
312319
* free any potential filler nodes. This means that this function
@@ -350,4 +357,16 @@ struct wg_peer *wg_allowedips_lookup_src(struct allowedips *table,
350357
return NULL;
351358
}
352359

360+
int __init wg_allowedips_slab_init(void)
361+
{
362+
node_cache = KMEM_CACHE(allowedips_node, 0);
363+
return node_cache ? 0 : -ENOMEM;
364+
}
365+
366+
void wg_allowedips_slab_uninit(void)
367+
{
368+
rcu_barrier();
369+
kmem_cache_destroy(node_cache);
370+
}
371+
353372
#include "selftest/allowedips.c"

drivers/net/wireguard/allowedips.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct allowedips_node {
1919
u8 bits[16] __aligned(__alignof(u64));
2020

2121
/* Keep rarely used members at bottom to be beyond cache line. */
22-
struct allowedips_node *__rcu *parent_bit; /* XXX: this puts us at 68->128 bytes instead of 60->64 bytes!! */
22+
struct allowedips_node *__rcu *parent_bit;
2323
union {
2424
struct list_head peer_list;
2525
struct rcu_head rcu;
@@ -53,4 +53,7 @@ struct wg_peer *wg_allowedips_lookup_src(struct allowedips *table,
5353
bool wg_allowedips_selftest(void);
5454
#endif
5555

56+
int wg_allowedips_slab_init(void);
57+
void wg_allowedips_slab_uninit(void);
58+
5659
#endif /* _WG_ALLOWEDIPS_H */

drivers/net/wireguard/main.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ static int __init mod_init(void)
2121
{
2222
int ret;
2323

24+
ret = wg_allowedips_slab_init();
25+
if (ret < 0)
26+
goto err_allowedips;
27+
2428
#ifdef DEBUG
29+
ret = -ENOTRECOVERABLE;
2530
if (!wg_allowedips_selftest() || !wg_packet_counter_selftest() ||
2631
!wg_ratelimiter_selftest())
27-
return -ENOTRECOVERABLE;
32+
goto err_peer;
2833
#endif
2934
wg_noise_init();
3035

@@ -50,6 +55,8 @@ static int __init mod_init(void)
5055
err_device:
5156
wg_peer_uninit();
5257
err_peer:
58+
wg_allowedips_slab_uninit();
59+
err_allowedips:
5360
return ret;
5461
}
5562

@@ -58,6 +65,7 @@ static void __exit mod_exit(void)
5865
wg_genetlink_uninit();
5966
wg_device_uninit();
6067
wg_peer_uninit();
68+
wg_allowedips_slab_uninit();
6169
}
6270

6371
module_init(mod_init);

0 commit comments

Comments
 (0)