Skip to contentSkip to conversation

ipvs: Use flexible array for MH lookup table

PATCH In review Rosen Penev 2 participants 3 messages May 19 – May 22

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipvs: Use flexible array for MH lookup table

Store the Maglev hash lookup table in the scheduler state allocation instead of allocating it separately.

This keeps the lookup table tied to the RCU-freed state lifetime and simplifies the allocation and cleanup paths.

Assisted-by Codex:GPT-5.5
---
 net/netfilter/ipvs/ip_vs_mh.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
net/netfilter/ipvs/ip_vs_mh.c +2 -9
index 020863047562..d31d3c6d4216 100644
@@ -59,11 +59,11 @@ static int primes[] = {251, 509, 1021, 2039, 4093,
 
 struct ip_vs_mh_state {
 	struct rcu_head			rcu_head;
-	struct ip_vs_mh_lookup		*lookup;
 	struct ip_vs_mh_dest_setup	*dest_setup;
 	hsiphash_key_t			hash1, hash2;
 	int				gcd;
 	int				rshift;
+	struct ip_vs_mh_lookup		lookup[];
 };
 
 static inline void generate_hash_secret(hsiphash_key_t *hash1,
@@ -372,7 +372,6 @@ static void ip_vs_mh_state_free(struct rcu_head *head)
 	struct ip_vs_mh_state *s;
 
 	s = container_of(head, struct ip_vs_mh_state, rcu_head);
-	kfree(s->lookup);
 	kfree(s);
 }
 
@@ -382,16 +381,10 @@ static int ip_vs_mh_init_svc(struct ip_vs_service *svc)
 	struct ip_vs_mh_state *s;
 
 	/* Allocate the MH table for this service */
-	s = kzalloc_obj(*s);
+	s = kzalloc_flex(*s, lookup, IP_VS_MH_TAB_SIZE);
 	if (!s)
 		return -ENOMEM;
 
-	s->lookup = kzalloc_objs(struct ip_vs_mh_lookup, IP_VS_MH_TAB_SIZE);
-	if (!s->lookup) {
-		kfree(s);
-		return -ENOMEM;
-	}
-
 	generate_hash_secret(&s->hash1, &s->hash2);
 	s->gcd = ip_vs_mh_gcd_weight(svc);
 	s->rshift = ip_vs_mh_shift_weight(svc, s->gcd);
Signature · 1 line
2.54.0
maintainer
#1 lore raw

Hello,

net/netfilter/ipvs/ip_vs_mh.c static void ip_vs_mh_state_free(struct rcu_head *head)

Quoted 30 lines · Rosen Penev

Store the Maglev hash lookup table in the scheduler state allocation instead of allocating it separately.

This keeps the lookup table tied to the RCU-freed state lifetime and simplifies the allocation and cleanup paths.

Assisted-by Codex:GPT-5.5
---
 net/netfilter/ipvs/ip_vs_mh.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
net/netfilter/ipvs/ip_vs_mh.c +1 -1
index 020863047562..d31d3c6d4216 100644
@@ -59,11 +59,11 @@ static int primes[] = {251, 509, 1021, 2039, 4093,
 
 struct ip_vs_mh_state {
 	struct rcu_head			rcu_head;
-	struct ip_vs_mh_lookup		*lookup;
 	struct ip_vs_mh_dest_setup	*dest_setup;
 	hsiphash_key_t			hash1, hash2;
 	int				gcd;
 	int				rshift;
+	struct ip_vs_mh_lookup		lookup[];
 };
 
 static inline void generate_hash_secret(hsiphash_key_t *hash1,
@@ -372,7 +372,6 @@ static void ip_vs_mh_state_free(struct rcu_head *head)

We can even drop this function and replace it with
kfree(s) in ip_vs_mh_init_svc() and with kfree_rcu(s, rcu_head) in ip_vs_mh_done_svc(), you can see ip_vs_dh.c for reference...

Quoted 27 lines
net/netfilter/ipvs/ip_vs_mh.c +1 -8
 	struct ip_vs_mh_state *s;
 
 	s = container_of(head, struct ip_vs_mh_state, rcu_head);
-	kfree(s->lookup);
 	kfree(s);
 }
 
@@ -382,16 +381,10 @@ static int ip_vs_mh_init_svc(struct ip_vs_service *svc)
 	struct ip_vs_mh_state *s;
 
 	/* Allocate the MH table for this service */
-	s = kzalloc_obj(*s);
+	s = kzalloc_flex(*s, lookup, IP_VS_MH_TAB_SIZE);
 	if (!s)
 		return -ENOMEM;
 
-	s->lookup = kzalloc_objs(struct ip_vs_mh_lookup, IP_VS_MH_TAB_SIZE);
-	if (!s->lookup) {
-		kfree(s);
-		return -ENOMEM;
-	}
-
 	generate_hash_secret(&s->hash1, &s->hash2);
 	s->gcd = ip_vs_mh_gcd_weight(svc);
 	s->rshift = ip_vs_mh_shift_weight(svc, s->gcd);
Signature · 1 line
2.54.0

Regards

--
Julian Anastasov <[email protected]>

maintainer
#2 lore raw
In reply to Rosen Penev

Hello,

Quoted 8 lines · Rosen Penev

Store the Maglev hash lookup table in the scheduler state allocation instead of allocating it separately.

This keeps the lookup table tied to the RCU-freed state lifetime and simplifies the allocation and cleanup paths.

Assisted-by Codex:GPT-5.5

Thanks for the efforts but it is better to keep the table
allocated separately. As Sashiko [1] points out, the table size is properly selected and we risk double-sized allocations if we apply this patch.

As for the problem in ip_vs_edit_service(), I have to
provide separate bugfix...

[1]:
https://sashiko.dev/#/patchset/20260519015506.634185-1-rosenp%40gmail.com

Regards

--
Julian Anastasov <[email protected]>