Skip to content

Commit 4f325e2

Browse files
Julian Anastasovummakynes
authored andcommitted
ipvs: dynamically limit the connection hash table
As we allow the hash table to be configured to rows above 2^20, we should limit it depending on the available memory to some sane values. Switch to kvmalloc allocation to better select the needed allocation type. Signed-off-by: Julian Anastasov <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 04292c6 commit 4f325e2

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

net/netfilter/ipvs/ip_vs_conn.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <linux/net.h>
2727
#include <linux/kernel.h>
2828
#include <linux/module.h>
29-
#include <linux/vmalloc.h>
3029
#include <linux/proc_fs.h> /* for proc_net_* */
3130
#include <linux/slab.h>
3231
#include <linux/seq_file.h>
@@ -1482,13 +1481,21 @@ void __net_exit ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs)
14821481
int __init ip_vs_conn_init(void)
14831482
{
14841483
size_t tab_array_size;
1484+
int max_avail;
1485+
#if BITS_PER_LONG > 32
1486+
int max = 27;
1487+
#else
1488+
int max = 20;
1489+
#endif
1490+
int min = 8;
14851491
int idx;
14861492

1487-
/* Compute size and mask */
1488-
if (ip_vs_conn_tab_bits < 8 || ip_vs_conn_tab_bits > 27) {
1489-
pr_info("conn_tab_bits not in [8, 27]. Using default value\n");
1490-
ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS;
1491-
}
1493+
max_avail = order_base_2(totalram_pages()) + PAGE_SHIFT;
1494+
max_avail -= 2; /* ~4 in hash row */
1495+
max_avail -= 1; /* IPVS up to 1/2 of mem */
1496+
max_avail -= order_base_2(sizeof(struct ip_vs_conn));
1497+
max = clamp(max, min, max_avail);
1498+
ip_vs_conn_tab_bits = clamp_val(ip_vs_conn_tab_bits, min, max);
14921499
ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
14931500
ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
14941501

@@ -1497,7 +1504,8 @@ int __init ip_vs_conn_init(void)
14971504
*/
14981505
tab_array_size = array_size(ip_vs_conn_tab_size,
14991506
sizeof(*ip_vs_conn_tab));
1500-
ip_vs_conn_tab = vmalloc(tab_array_size);
1507+
ip_vs_conn_tab = kvmalloc_array(ip_vs_conn_tab_size,
1508+
sizeof(*ip_vs_conn_tab), GFP_KERNEL);
15011509
if (!ip_vs_conn_tab)
15021510
return -ENOMEM;
15031511

@@ -1506,7 +1514,7 @@ int __init ip_vs_conn_init(void)
15061514
sizeof(struct ip_vs_conn), 0,
15071515
SLAB_HWCACHE_ALIGN, NULL);
15081516
if (!ip_vs_conn_cachep) {
1509-
vfree(ip_vs_conn_tab);
1517+
kvfree(ip_vs_conn_tab);
15101518
return -ENOMEM;
15111519
}
15121520

@@ -1534,5 +1542,5 @@ void ip_vs_conn_cleanup(void)
15341542
rcu_barrier();
15351543
/* Release the empty cache */
15361544
kmem_cache_destroy(ip_vs_conn_cachep);
1537-
vfree(ip_vs_conn_tab);
1545+
kvfree(ip_vs_conn_tab);
15381546
}

0 commit comments

Comments
 (0)