ipv4: fib: Convert RTM_NEWROUTE and RTM_DELROUTE to per-netns RTNL.
Subsystems (3)
cover ipv4: fib: Convert RTM_NEWROUTE and RTM_DELROUTE to per-netns RTNL.
Patch 1 is a misc cleanup.
Patch 2 ~ 8 converts two fib_info hash tables to per-netns. Patch 9 ~ 12 converts rtnl_lock() to rtnl_net_lcok().
Kuniyuki Iwashima (12):
ipv4: fib: Use cached net in fib_inetaddr_event().
ipv4: fib: Allocate fib_info_hash[] and fib_info_laddrhash[] by
kvmalloc_array().
ipv4: fib: Allocate fib_info_hash[] during netns initialisation.
ipv4: fib: Make fib_info_hashfn() return struct hlist_head.
ipv4: fib: Remove fib_info_laddrhash pointer.
ipv4: fib: Remove fib_info_hash_size.
ipv4: fib: Add fib_info_hash_grow().
ipv4: fib: Namespacify fib_info hash tables.
ipv4: fib: Hold rtnl_net_lock() for ip_fib_net_exit().
ipv4: fib: Hold rtnl_net_lock() in ip_rt_ioctl().
ipv4: fib: Move fib_valid_key_len() to rtm_to_fib_config().
ipv4: fib: Convert RTM_NEWROUTE and RTM_DELROUTE to per-netns RTNL.
include/net/ip_fib.h | 2 + include/net/netns/ipv4.h | 3 + net/ipv4/fib_frontend.c | 72 ++++++++++---- net/ipv4/fib_semantics.c | 207 +++++++++++++++++++-------------------- net/ipv4/fib_trie.c | 22 ----- 5 files changed, 158 insertions(+), 148 deletions(-)
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH 1/12 ipv4: fib: Use cached net in fib_inetaddr_event().
net is available in fib_inetaddr_event(), let's use it.
--- net/ipv4/fib_frontend.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/fib_frontend.c +2 -2
@@ -1450,7 +1450,7 @@ static int fib_inetaddr_event(struct notifier_block *this, unsigned long event,fib_sync_up (dev,RTNH_F_DEAD );#endif atomic_inc (&net->ipv4.dev_addr_genid); -rt_cache_flush (dev_net (dev)); +rt_cache_flush (net);break ;case NETDEV_DOWN :fib_del_ifaddr (ifa,NULL );
@@ -1461,7 +1461,7 @@ static int fib_inetaddr_event(struct notifier_block *this, unsigned long event,*/ fib_disable_ip (dev, event, true); }else { -rt_cache_flush (dev_net (dev)); +rt_cache_flush (net); }break ; }
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v1 net-next 01/12] ipv4: fib: Use cached net in fib_inetaddr_event().
Quoted 4 lines · Kuniyuki Iwashima
net is available in
fib_inetaddr_event(), let's use it.Signed-off-by Kuniyuki Iwashima <[email protected]>
PATCH 2/12 ipv4: fib: Allocate fib_info_hash[] and fib_info_laddrhash[] by kvmalloc_array().
In reply to Kuniyuki IwashimaBoth fib_info_hash[] and fib_info_laddrhash[] are hash tables for struct fib_info and are allocated by kvzmalloc() separately.
Let's replace the two kvzmalloc() calls with kvmalloc_array() to remove the fib_info_laddrhash pointer later.
Note that fib_info_hash_alloc() allocates a new hash table based on fib_info_hash_bits because we will remove fib_info_hash_size later.
--- net/ipv4/fib_semantics.c | 44 ++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-)
net/ipv4/fib_semantics.c +26 -18
@@ -357,6 +357,19 @@ static inline unsigned int fib_info_hashfn(struct fib_info *fi)return fib_info_hashfn_result (fi->fib_net, val); } +static struct hlist_head *fib_info_hash_alloc (unsigned int hash_bits) +{ +/* The second half is used for prefsrc */ +return kvmalloc_array ((1 << hash_bits) *2 , +sizeof (struct hlist_head *), +GFP_KERNEL | __GFP_ZERO); +} + +static void fib_info_hash_free (struct hlist_head *head) +{ +kvfree (head); +} +/* no metrics, only nexthop id */ static struct fib_info *fib_find_info_nh (struct net *net,const struct fib_config *cfg)
@@ -1249,9 +1262,9 @@ fib_info_laddrhash_bucket(const struct net *net, __be32 val) }static void fib_info_hash_move (struct hlist_head *new_info_hash, -struct hlist_head *new_laddrhash,unsigned int new_size) { +struct hlist_head *new_laddrhash = new_info_hash + new_size;struct hlist_head *old_info_hash, *old_laddrhash;unsigned int old_size = fib_info_hash_size;unsigned int i;
@@ -1293,8 +1306,7 @@ static void fib_info_hash_move(struct hlist_head *new_info_hash, } } -kvfree (old_info_hash); -kvfree (old_laddrhash); +fib_info_hash_free (old_info_hash); } __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
@@ -1412,22 +1424,18 @@ struct fib_info *fib_create_info(struct fib_config *cfg, err = -ENOBUFS ;if (fib_info_cnt >= fib_info_hash_size) { -unsigned int new_size = fib_info_hash_size <<1 ;struct hlist_head *new_info_hash; -struct hlist_head *new_laddrhash; -size_t bytes; - -if (!new_size) - new_size =16 ; - bytes = (size_t )new_size *sizeof (struct hlist_head *); - new_info_hash =kvzalloc (bytes,GFP_KERNEL ); - new_laddrhash =kvzalloc (bytes,GFP_KERNEL ); -if (!new_info_hash || !new_laddrhash) { -kvfree (new_info_hash); -kvfree (new_laddrhash); - }else { -fib_info_hash_move (new_info_hash, new_laddrhash, new_size); - } +unsigned int new_hash_bits; + +if (!fib_info_hash_bits) + new_hash_bits =4 ; +else + new_hash_bits = fib_info_hash_bits +1 ; + + new_info_hash =fib_info_hash_alloc (new_hash_bits); +if (new_info_hash) +fib_info_hash_move (new_info_hash,1 << new_hash_bits); +if (!fib_info_hash_size) goto failure; }
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v1 net-next 02/12] ipv4: fib: Allocate fib_info_hash[] and fib_info_laddrhash[] by kvmalloc_array().
Quoted 11 lines · Kuniyuki Iwashima
Both fib_info_hash[] and fib_info_laddrhash[] are hash tables for struct
fib_infoand are allocated bykvzmalloc()separately.Let's replace the two
kvzmalloc()calls withkvmalloc_array()to remove thefib_info_laddrhashpointer later.Note that
fib_info_hash_alloc()allocates a new hash table based onfib_info_hash_bitsbecause we will removefib_info_hash_sizelater.Signed-off-by Kuniyuki Iwashima <[email protected]>
PATCH 3/12 ipv4: fib: Allocate fib_info_hash[] during netns initialisation.
In reply to Kuniyuki IwashimaWe will allocate fib_info_hash[] and fib_info_laddrhash[] for each netns.
Currently, fib_info_hash[] is allocated when the first route is added.
Let's move the first allocation to a new __net_init function.
--- include/net/ip_fib.h | 2 ++ net/ipv4/fib_frontend.c | 9 ++++++++ net/ipv4/fib_semantics.c | 45 ++++++++++++++++++++++++++++------------ 3 files changed, 43 insertions(+), 13 deletions(-)
include/net/ip_fib.h +2 -0
@@ -162,6 +162,8 @@ struct fib_info { struct fib_nh fib_nh[] __counted_by(fib_nhs); }; +int __net_init fib4_semantics_init (struct net *net); +void __net_exit fib4_semantics_exit(struct net *net);#ifdef CONFIG_IP_MULTIPLE_TABLES struct fib_rule ;
net/ipv4/fib_frontend.c +9 -0
@@ -1615,9 +1615,15 @@ static int __net_init fib_net_init(struct net *net) error =ip_fib_net_init (net);if (error <0 ) goto out; + + error =fib4_semantics_init (net); +if (error) + goto out_semantics; + error =nl_fib_lookup_init (net);if (error <0 ) goto out_nlfl; + error =fib_proc_init (net);if (error <0 ) goto out_proc;
@@ -1627,6 +1633,8 @@ static int __net_init fib_net_init(struct net *net) out_proc:nl_fib_lookup_exit (net); out_nlfl: +fib4_semantics_init (net); +out_semantics:rtnl_lock ();ip_fib_net_exit (net);rtnl_unlock ();
@@ -1637,6 +1645,7 @@ static void __net_exit fib_net_exit(struct net *net) {fib_proc_exit (net);nl_fib_lookup_exit (net); +fib4_semantics_init (net); } static void __net_exit fib_net_exit_batch(struct list_head *net_list)
net/ipv4/fib_semantics.c +32 -13
70 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v1 net-next 03/12] ipv4: fib: Allocate fib_info_hash[] during netns initialisation.
Quoted 8 lines · Kuniyuki Iwashima
We will allocate fib_info_hash[] and fib_info_laddrhash[] for each netns.
Currently, fib_info_hash[] is allocated when the first route is added.
Let's move the first allocation to a new
__net_initfunction.Signed-off-by Kuniyuki Iwashima <[email protected]>
David Ahern
maintainerRe: [PATCH v1 net-next 03/12] ipv4: fib: Allocate fib_info_hash[] during netns initialisation.
In reply to Kuniyuki IwashimaQuoted 21 lines · Kuniyuki Iwashima
net/ipv4/fib_frontend.c +6
-0@@ -1615,9 +1615,15 @@ static int __net_init fib_net_init(struct net *net) error =ip_fib_net_init (net);if (error <0 ) goto out; + + error =fib4_semantics_init (net); +if (error) + goto out_semantics; + error =nl_fib_lookup_init (net);if (error <0 ) goto out_nlfl; + error =fib_proc_init (net);if (error <0 ) goto out_proc;
net/ipv4/fib_frontend.c static int __net_init fib_net_init(struct net *net) · line 1636
Quoted 4 lines
net/ipv4/fib_frontend.c +1
-0@@ -1627,6 +1633,8 @@ static int __net_init fib_net_init(struct net *net) out_proc:nl_fib_lookup_exit (net); out_nlfl: +fib4_semantics_init (net);
_exit?
Re: [PATCH v1 net-next 03/12] ipv4: fib: Allocate fib_info_hash[] during netns initialisation.
Quoted 21 lines · David Ahern
Quoted 19 lines · Kuniyuki Iwashima
net/ipv4/fib_frontend.c +6
-0@@ -1615,9 +1615,15 @@ static int __net_init fib_net_init(struct net *net) error =ip_fib_net_init (net);if (error <0 ) goto out; + + error =fib4_semantics_init (net); +if (error) + goto out_semantics; + error =nl_fib_lookup_init (net);if (error <0 ) goto out_nlfl; + error =fib_proc_init (net);if (error <0 )
Quoted 8 lines
net/ipv4/fib_frontend.c static int __net_init fib_net_init(struct net *net) · line 1636
Quoted 6 lines
net/ipv4/fib_frontend.c +1
-0goto out_proc;@@ -1627,6 +1633,8 @@ static int __net_init fib_net_init(struct net *net) out_proc:nl_fib_lookup_exit (net); out_nlfl: +fib4_semantics_init (net);
_exit?
Yes, this was mentioned yesterday.
David Ahern
maintainerRe: [PATCH v1 net-next 03/12] ipv4: fib: Allocate fib_info_hash[] during netns initialisation.
Quoted 9 lines · Eric Dumazet
Quoted 7 lines
net/ipv4/fib_frontend.c static int __net_init fib_net_init(struct net *net) · line 1636
Quoted 5 lines
net/ipv4/fib_frontend.c +1
-0@@ -1627,6 +1633,8 @@ static int __net_init fib_net_init(struct net *net) out_proc:nl_fib_lookup_exit (net); out_nlfl: +fib4_semantics_init (net);
_exit?Yes, this was mentioned yesterday.
not in this code path but rather fib_net_exit. Hence me pointing out this one only
Re: [PATCH v1 net-next 03/12] ipv4: fib: Allocate fib_info_hash[] during netns initialisation.
From: David Ahern <[email protected]>
Date: Wed, 26 Feb 2025 11:12:13 -0700
Quoted 9 lines · Eric Dumazet
Quoted 8 lines
Quoted 7 lines
net/ipv4/fib_frontend.c static int __net_init fib_net_init(struct net *net) · line 1636
Quoted 5 lines
net/ipv4/fib_frontend.c +1
-0@@ -1627,6 +1633,8 @@ static int __net_init fib_net_init(struct net *net) out_proc:nl_fib_lookup_exit (net); out_nlfl: +fib4_semantics_init (net);
_exit?
Quoted 4 lines
Quoted 1 line
Yes, this was mentioned yesterday.
not in this code path but rather
fib_net_exit. Hence me pointing out this one only
Sorry, actually I realised the path and fixed just after sending the diff yesterday.
Will send v2 shortly.
Thank you both for the review !
PATCH 4/12 ipv4: fib: Make fib_info_hashfn() return struct hlist_head.
In reply to Kuniyuki IwashimaEvery time fib_info_hashfn() returns a hash value, we fetch &fib_info_hash[hash].
Let's return the hlist_head pointer from fib_info_hashfn() and rename it to fib_info_hash_bucket() to match a similar function, fib_info_laddrhash_bucket().
Note that we need to move the fib_info_hash assignment earlier in fib_info_hash_move() to use fib_info_hash_bucket() in the for loop.
--- net/ipv4/fib_semantics.c | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-)
net/ipv4/fib_semantics.c +9 -19
@@ -338,7 +338,7 @@ static unsigned int fib_info_hashfn_result(const struct net *net,return hash_32 (val ^net_hash_mix (net), fib_info_hash_bits); } -static inline unsigned int fib_info_hashfn (struct fib_info *fi) +static struct hlist_head *fib_info_hash_bucket (struct fib_info *fi) {unsigned int val;
@@ -354,7 +354,7 @@ static inline unsigned int fib_info_hashfn(struct fib_info *fi) } endfor_nexthops(fi) } -return fib_info_hashfn_result (fi->fib_net, val); +return &fib_info_hash[fib_info_hashfn_result (fi->fib_net, val)]; }static struct hlist_head *fib_info_hash_alloc (unsigned int hash_bits)
@@ -405,12 +405,8 @@ static struct fib_info *fib_find_info_nh(struct net *net,static struct fib_info *fib_find_info (struct fib_info *nfi) { -struct hlist_head *head; +struct hlist_head *head =fib_info_hash_bucket (nfi);struct fib_info *fi; -unsigned int hash; - - hash =fib_info_hashfn (nfi); - head = &fib_info_hash[hash];hlist_for_each_entry (fi, head, fib_hash) {if (!net_eq (fi->fib_net, nfi->fib_net))
@@ -1274,22 +1270,16 @@ static void fib_info_hash_move(struct hlist_head *new_info_hash, old_laddrhash = fib_info_laddrhash; fib_info_hash_size = new_size; fib_info_hash_bits =ilog2 (new_size); + fib_info_hash = new_info_hash;for (i =0 ; i < old_size; i++) { -struct hlist_head *head = &fib_info_hash[i]; +struct hlist_head *head = &old_info_hash[i];struct hlist_node *n;struct fib_info *fi; -hlist_for_each_entry_safe (fi, n, head, fib_hash) { -struct hlist_head *dest; -unsigned int new_hash; - - new_hash =fib_info_hashfn (fi); - dest = &new_info_hash[new_hash]; -hlist_add_head (&fi->fib_hash, dest); - } +hlist_for_each_entry_safe (fi, n, head, fib_hash) +hlist_add_head (&fi->fib_hash,fib_info_hash_bucket (fi)); } - fib_info_hash = new_info_hash; fib_info_laddrhash = new_laddrhash;for (i =0 ; i < old_size; i++) {
@@ -1573,8 +1563,8 @@ struct fib_info *fib_create_info(struct fib_config *cfg,refcount_set (&fi->fib_clntref,1 ); fib_info_cnt++; -hlist_add_head (&fi->fib_hash, - &fib_info_hash[fib_info_hashfn (fi)]); +hlist_add_head (&fi->fib_hash,fib_info_hash_bucket (fi)); +if (fi->fib_prefsrc) {struct hlist_head *head;
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v1 net-next 04/12] ipv4: fib: Make fib_info_hashfn() return struct hlist_head.
Quoted 4 lines · Kuniyuki Iwashima
Every time
fib_info_hashfn()returns a hash value, we fetch &fib_info_hash[hash].
Quoted 8 lines
Let's return the
hlist_headpointer fromfib_info_hashfn()and rename it tofib_info_hash_bucket()to match a similar function,fib_info_laddrhash_bucket().Note that we need to move the
fib_info_hashassignment earlier infib_info_hash_move()to usefib_info_hash_bucket()in the for loop.Signed-off-by Kuniyuki Iwashima <[email protected]>
Nice !
PATCH 5/12 ipv4: fib: Remove fib_info_laddrhash pointer.
In reply to Kuniyuki IwashimaWe will allocate the fib_info hash tables per netns.
There are 5 global variables for fib_info hash tables:fib_info_hash, fib_info_laddrhash, fib_info_hash_size, fib_info_hash_bits, fib_info_cnt.
However, fib_info_laddrhash and fib_info_hash_size can be easily calculated from fib_info_hash and fib_info_hash_bits.
Let's remove the fib_info_laddrhash pointer and instead use fib_info_hash + (1 << fib_info_hash_bits).
While at it, fib_info_laddrhash_bucket() is moved near other hash-table-specific functions.
--- net/ipv4/fib_semantics.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-)
net/ipv4/fib_semantics.c +11 -16
@@ -51,7 +51,6 @@#include "fib_lookup.h" static struct hlist_head *fib_info_hash; -static struct hlist_head *fib_info_laddrhash;static unsigned int fib_info_hash_size;static unsigned int fib_info_hash_bits;static unsigned int fib_info_cnt;
@@ -357,6 +356,15 @@ static struct hlist_head *fib_info_hash_bucket(struct fib_info *fi)return &fib_info_hash[fib_info_hashfn_result (fi->fib_net, val)]; } +static struct hlist_head *fib_info_laddrhash_bucket (const struct net *net, +__be32 val) +{ +u32 slot =hash_32 (net_hash_mix (net) ^ (__force u32 )val, + fib_info_hash_bits); + +return &fib_info_hash[(1 << fib_info_hash_bits) + slot]; +} +static struct hlist_head *fib_info_hash_alloc (unsigned int hash_bits) {/* The second half is used for prefsrc */
@@ -1248,26 +1256,15 @@ int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,return err; } -static struct hlist_head * -fib_info_laddrhash_bucket (const struct net *net,__be32 val) -{ -u32 slot =hash_32 (net_hash_mix (net) ^ (__force u32 )val, - fib_info_hash_bits); - -return &fib_info_laddrhash[slot]; -} -static void fib_info_hash_move (struct hlist_head *new_info_hash,unsigned int new_size) { -struct hlist_head *new_laddrhash = new_info_hash + new_size; -struct hlist_head *old_info_hash, *old_laddrhash;unsigned int old_size = fib_info_hash_size; +struct hlist_head *old_info_hash;unsigned int i;ASSERT_RTNL (); old_info_hash = fib_info_hash; - old_laddrhash = fib_info_laddrhash; fib_info_hash_size = new_size; fib_info_hash_bits =ilog2 (new_size); fib_info_hash = new_info_hash;
@@ -1281,9 +1278,8 @@ static void fib_info_hash_move(struct hlist_head *new_info_hash,hlist_add_head (&fi->fib_hash,fib_info_hash_bucket (fi)); } - fib_info_laddrhash = new_laddrhash;for (i =0 ; i < old_size; i++) { -struct hlist_head *lhead = &old_laddrhash[i]; +struct hlist_head *lhead = &old_info_hash[old_size + i];struct hlist_node *n;struct fib_info *fi;
@@ -2262,7 +2258,6 @@ int __net_init fib4_semantics_init(struct net *net) fib_info_hash_bits = hash_bits; fib_info_hash_size =1 << hash_bits; - fib_info_laddrhash = fib_info_hash + fib_info_hash_size;return 0 ; }
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v1 net-next 05/12] ipv4: fib: Remove fib_info_laddrhash pointer.
Quoted 10 lines · Kuniyuki Iwashima
We will allocate the
fib_infohash tables per netns.There are 5 global variables for
fib_infohash tables:fib_info_hash,fib_info_laddrhash,fib_info_hash_size,fib_info_hash_bits,fib_info_cnt.However,
fib_info_laddrhashandfib_info_hash_sizecan be easily calculated fromfib_info_hashandfib_info_hash_bits.
Quoted 7 lines
Let's remove the
fib_info_laddrhashpointer and instead usefib_info_hash+ (1 <<fib_info_hash_bits).While at it,
fib_info_laddrhash_bucket()is moved near other hash-table-specific functions.Signed-off-by Kuniyuki Iwashima <[email protected]>
PATCH 6/12 ipv4: fib: Remove fib_info_hash_size.
In reply to Kuniyuki IwashimaWe will allocate the fib_info hash tables per netns.
There are 5 global variables for fib_info hash tables:fib_info_hash, fib_info_laddrhash, fib_info_hash_size, fib_info_hash_bits, fib_info_cnt.
However, fib_info_laddrhash and fib_info_hash_size can be easily calculated from fib_info_hash and fib_info_hash_bits.
Let's remove fib_info_hash_size and use (1 << fib_info_hash_bits) instead.
Now we need not pass the new hash table size to fib_info_hash_move().
--- net/ipv4/fib_semantics.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
net/ipv4/fib_semantics.c +6 -11
@@ -51,7 +51,6 @@#include "fib_lookup.h" static struct hlist_head *fib_info_hash; -static unsigned int fib_info_hash_size;static unsigned int fib_info_hash_bits;static unsigned int fib_info_cnt;
@@ -1256,17 +1255,15 @@ int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,return err; } -static void fib_info_hash_move (struct hlist_head *new_info_hash, -unsigned int new_size) +static void fib_info_hash_move (struct hlist_head *new_info_hash) { -unsigned int old_size = fib_info_hash_size; +unsigned int old_size =1 << fib_info_hash_bits;struct hlist_head *old_info_hash;unsigned int i;ASSERT_RTNL (); old_info_hash = fib_info_hash; - fib_info_hash_size = new_size; - fib_info_hash_bits =ilog2 (new_size); + fib_info_hash_bits +=1 ; fib_info_hash = new_info_hash;for (i =0 ; i < old_size; i++) {
@@ -1407,13 +1404,12 @@ struct fib_info *fib_create_info(struct fib_config *cfg, }#endif -if (fib_info_cnt >= fib_info_hash_size) { -unsigned int new_hash_bits = fib_info_hash_bits +1 ; +if (fib_info_cnt >= (1 << fib_info_hash_bits)) {struct hlist_head *new_info_hash; - new_info_hash =fib_info_hash_alloc (new_hash_bits); + new_info_hash =fib_info_hash_alloc (fib_info_hash_bits +1 );if (new_info_hash) -fib_info_hash_move (new_info_hash,1 << new_hash_bits); +fib_info_hash_move (new_info_hash); } fi =kzalloc (struct_size (fi, fib_nh, nhs),GFP_KERNEL );
@@ -2257,7 +2253,6 @@ int __net_init fib4_semantics_init(struct net *net)return -ENOMEM ; fib_info_hash_bits = hash_bits; - fib_info_hash_size =1 << hash_bits;return 0 ; }
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v1 net-next 06/12] ipv4: fib: Remove fib_info_hash_size.
Quoted 10 lines · Kuniyuki Iwashima
We will allocate the
fib_infohash tables per netns.There are 5 global variables for
fib_infohash tables:fib_info_hash,fib_info_laddrhash,fib_info_hash_size,fib_info_hash_bits,fib_info_cnt.However,
fib_info_laddrhashandfib_info_hash_sizecan be easily calculated fromfib_info_hashandfib_info_hash_bits.
Quoted 6 lines
Let's remove
fib_info_hash_sizeand use (1 <<fib_info_hash_bits) instead.Now we need not pass the new hash table size to
fib_info_hash_move().Signed-off-by Kuniyuki Iwashima <[email protected]>
PATCH 7/12 ipv4: fib: Add fib_info_hash_grow().
In reply to Kuniyuki IwashimaWhen the number of struct fib_info exceeds the hash table size in fib_create_info(), we try to allocate a new hash table with the doubled size.
The allocation is done in fib_create_info(), and if successful, each struct fib_info is moved to the new hash table by fib_info_hash_move().
Let's integrate the allocation and fib_info_hash_move() as fib_info_hash_grow() to make the following change cleaner.
While at it, fib_info_hash_grow() is placed near other hash-table-specific functions.
--- net/ipv4/fib_semantics.c | 85 +++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 44 deletions(-)
net/ipv4/fib_semantics.c +41 -44
@@ -377,6 +377,46 @@ static void fib_info_hash_free(struct hlist_head *head)kvfree (head); } +static void fib_info_hash_grow (void ) +{ +struct hlist_head *new_info_hash, *old_info_hash; +unsigned int old_size =1 << fib_info_hash_bits; +unsigned int i; + +if (fib_info_cnt < old_size) +return ; + + new_info_hash =fib_info_hash_alloc (fib_info_hash_bits +1 ); +if (!new_info_hash) +return ; + + old_info_hash = fib_info_hash; + fib_info_hash = new_info_hash; + fib_info_hash_bits +=1 ; + +for (i =0 ; i < old_size; i++) { +struct hlist_head *head = &old_info_hash[i]; +struct hlist_node *n; +struct fib_info *fi; + +hlist_for_each_entry_safe (fi, n, head, fib_hash) +hlist_add_head (&fi->fib_hash,fib_info_hash_bucket (fi)); + } + +for (i =0 ; i < old_size; i++) { +struct hlist_head *lhead = &old_info_hash[old_size + i]; +struct hlist_node *n; +struct fib_info *fi; + +hlist_for_each_entry_safe (fi, n, lhead, fib_lhash) +hlist_add_head (&fi->fib_lhash, +fib_info_laddrhash_bucket (fi->fib_net, + fi->fib_prefsrc)); + } + +fib_info_hash_free (old_info_hash); +} +/* no metrics, only nexthop id */ static struct fib_info *fib_find_info_nh (struct net *net,const struct fib_config *cfg)
@@ -1255,43 +1295,6 @@ int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,return err; } -static void fib_info_hash_move (struct hlist_head *new_info_hash) -{ -unsigned int old_size =1 << fib_info_hash_bits; -struct hlist_head *old_info_hash; -unsigned int i; - -ASSERT_RTNL (); - old_info_hash = fib_info_hash; - fib_info_hash_bits +=1 ; - fib_info_hash = new_info_hash; - -for (i =0 ; i < old_size; i++) { -struct hlist_head *head = &old_info_hash[i]; -struct hlist_node *n; -struct fib_info *fi; - -hlist_for_each_entry_safe (fi, n, head, fib_hash) -hlist_add_head (&fi->fib_hash,fib_info_hash_bucket (fi)); - } - -for (i =0 ; i < old_size; i++) { -struct hlist_head *lhead = &old_info_hash[old_size + i]; -struct hlist_node *n; -struct fib_info *fi; - -hlist_for_each_entry_safe (fi, n, lhead, fib_lhash) { -struct hlist_head *ldest; - - ldest =fib_info_laddrhash_bucket (fi->fib_net, - fi->fib_prefsrc); -hlist_add_head (&fi->fib_lhash, ldest); - } - } - -fib_info_hash_free (old_info_hash); -} -__be32 fib_info_update_nhc_saddr (struct net *net,struct fib_nh_common *nhc,unsigned char scope) {
@@ -1404,13 +1407,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg, }#endif -if (fib_info_cnt >= (1 << fib_info_hash_bits)) { -struct hlist_head *new_info_hash; - - new_info_hash =fib_info_hash_alloc (fib_info_hash_bits +1 ); -if (new_info_hash) -fib_info_hash_move (new_info_hash); - } +fib_info_hash_grow (); fi =kzalloc (struct_size (fi, fib_nh, nhs),GFP_KERNEL );if (!fi) {
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v1 net-next 07/12] ipv4: fib: Add fib_info_hash_grow().
Quoted 8 lines · Kuniyuki Iwashima
When the number of struct
fib_infoexceeds the hash table size infib_create_info(), we try to allocate a new hash table with the doubled size.The allocation is done in
fib_create_info(), and if successful, each structfib_infois moved to the new hash table byfib_info_hash_move().
Quoted 7 lines
Let's integrate the allocation and
fib_info_hash_move()asfib_info_hash_grow()to make the following change cleaner.While at it,
fib_info_hash_grow()is placed near other hash-table-specific functions.Signed-off-by Kuniyuki Iwashima <[email protected]>
PATCH 8/12 ipv4: fib: Namespacify fib_info hash tables.
In reply to Kuniyuki IwashimaWe will convert RTM_NEWROUTE and RTM_DELROUTE to per-netns RTNL. Then, we need to have per-netns hash tables for struct fib_info.
Let's allocate the hash tables per netns.
fib_info_hash, fib_info_hash_bits, and fib_info_cnt are now moved to struct netns_ipv4 and accessed with net->ipv4.fib_XXX.
Also, the netns checks are removed from fib_find_info_nh() and fib_find_info().
--- include/net/netns/ipv4.h | 3 ++ net/ipv4/fib_semantics.c | 61 +++++++++++++++++----------------------- 2 files changed, 29 insertions(+), 35 deletions(-)
include/net/netns/ipv4.h +3 -0
@@ -111,6 +111,9 @@ struct netns_ipv4 {#endif struct hlist_head *fib_table_hash;struct sock *fibnl; +struct hlist_head *fib_info_hash; +unsigned int fib_info_hash_bits; +unsigned int fib_info_cnt;struct sock *mc_autojoin_sk;
net/ipv4/fib_semantics.c +26 -35
152 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v1 net-next 08/12] ipv4: fib: Namespacify fib_info hash tables.
Quoted 6 lines · Kuniyuki Iwashima
We will convert
RTM_NEWROUTEandRTM_DELROUTEto per-netns RTNL. Then, we need to have per-netns hash tables for structfib_info.Let's allocate the hash tables per netns.
Quoted 7 lines
fib_info_hash,fib_info_hash_bits, andfib_info_cntare now moved to structnetns_ipv4and accessed withnet->ipv4.fib_XXX.Also, the netns checks are removed from
fib_find_info_nh()andfib_find_info().Signed-off-by Kuniyuki Iwashima <[email protected]>
PATCH 9/12 ipv4: fib: Hold rtnl_net_lock() for ip_fib_net_exit().
In reply to Kuniyuki Iwashimaip_fib_net_exit() requires RTNL and is called from fib_net_init() and fib_net_exit_batch().
Let's hold rtnl_net_lock() before ip_fib_net_exit().
--- net/ipv4/fib_frontend.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
net/ipv4/fib_frontend.c +7 -5
@@ -1575,7 +1575,7 @@ static void ip_fib_net_exit(struct net *net) {int i; -ASSERT_RTNL (); +ASSERT_RTNL_NET (net);#ifdef CONFIG_IP_MULTIPLE_TABLES RCU_INIT_POINTER (net->ipv4.fib_main,NULL );RCU_INIT_POINTER (net->ipv4.fib_default,NULL );
@@ -1635,9 +1635,9 @@ static int __net_init fib_net_init(struct net *net) out_nlfl:fib4_semantics_init (net); out_semantics: -rtnl_lock (); +rtnl_net_lock (net);ip_fib_net_exit (net); -rtnl_unlock (); +rtnl_net_unlock (net); goto out; }
@@ -1653,9 +1653,11 @@ static void __net_exit fib_net_exit_batch(struct list_head *net_list)struct net *net;rtnl_lock (); -list_for_each_entry (net, net_list, exit_list) +list_for_each_entry (net, net_list, exit_list) { +__rtnl_net_lock (net);ip_fib_net_exit (net); - +__rtnl_net_unlock (net); + }rtnl_unlock (); }
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v1 net-next 09/12] ipv4: fib: Hold rtnl_net_lock() for ip_fib_net_exit().
Quoted 7 lines · Kuniyuki Iwashima
ip_fib_net_exit()requires RTNL and is called fromfib_net_init()andfib_net_exit_batch().Let's hold
rtnl_net_lock()beforeip_fib_net_exit().Signed-off-by Kuniyuki Iwashima <[email protected]>
PATCH 10/12 ipv4: fib: Hold rtnl_net_lock() in ip_rt_ioctl().
In reply to Kuniyuki Iwashimaioctl(SIOCADDRT/SIOCDELRT) calls ip_rt_ioctl() to add/remove a route in the netns of the specified socket.
Let's hold rtnl_net_lock() there.
Note that rtentry_to_fib_config() can be called without rtnl_net_lock() if we convert rtentry.dev handling to RCU later.
--- net/ipv4/fib_frontend.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
net/ipv4/fib_frontend.c +4 -6
@@ -553,18 +553,16 @@ static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,const struct in_ifaddr *ifa;struct in_device *in_dev; - in_dev =__in_dev_get_rtnl (dev); + in_dev =__in_dev_get_rtnl_net (dev);if (!in_dev)return -ENODEV ; *colon =':' ; -rcu_read_lock (); -in_dev_for_each_ifa_rcu (ifa, in_dev) { +in_dev_for_each_ifa_rtnl_net (net, ifa, in_dev) {if (strcmp (ifa->ifa_label, devname) ==0 )break ; } -rcu_read_unlock ();if (!ifa)return -ENODEV ;
@@ -635,7 +633,7 @@ int ip_rt_ioctl(struct net *net, unsigned int cmd, struct rtentry *rt)if (!ns_capable (net->user_ns,CAP_NET_ADMIN ))return -EPERM ; -rtnl_lock (); +rtnl_net_lock (net); err =rtentry_to_fib_config (net, cmd, rt, &cfg);if (err ==0 ) {struct fib_table *tb;
@@ -659,7 +657,7 @@ int ip_rt_ioctl(struct net *net, unsigned int cmd, struct rtentry *rt)/* allocated by rtentry_to_fib_config() */ kfree (cfg.fc_mx); } -rtnl_unlock (); +rtnl_net_unlock (net);return err; }return -EINVAL ;
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v1 net-next 10/12] ipv4: fib: Hold rtnl_net_lock() in ip_rt_ioctl().
Quoted 10 lines · Kuniyuki Iwashima
ioctl(SIOCADDRT/SIOCDELRT) calls
ip_rt_ioctl()to add/remove a route in the netns of the specified socket.Let's hold
rtnl_net_lock()there.Note that
rtentry_to_fib_config()can be called withoutrtnl_net_lock()if we convert rtentry.dev handling to RCU later.Signed-off-by Kuniyuki Iwashima <[email protected]>
PATCH 11/12 ipv4: fib: Move fib_valid_key_len() to rtm_to_fib_config().
In reply to Kuniyuki Iwashimafib_valid_key_len() is called in the beginning of fib_table_insert() or fib_table_delete() to check if the prefix length is valid.
fib_table_insert() and fib_table_delete() are called from 3 paths
- ip_rt_ioctl()
- inet_rtm_newroute() / inet_rtm_delroute()
- fib_magic()
In the first ioctl() path, rtentry_to_fib_config() checks the prefix length with bad_mask(). Also, fib_magic() always passes the correct
prefix: 32 or ifa->ifa_prefixlen, which is already validated.
Let's move fib_valid_key_len() to the rtnetlink path, rtm_to_fib_config().
While at it, 2 direct returns in rtm_to_fib_config() are changed to goto to match other places in the same function
--- net/ipv4/fib_frontend.c | 18 ++++++++++++++++-- net/ipv4/fib_trie.c | 22 ---------------------- 2 files changed, 16 insertions(+), 24 deletions(-)
net/ipv4/fib_frontend.c +16 -2
@@ -835,19 +835,33 @@ static int rtm_to_fib_config(struct net *net, struct sk_buff *skb, } } +if (cfg->fc_dst_len >32 ) { +NL_SET_ERR_MSG (extack,"Invalid prefix length" ); + err = -EINVAL ; + goto errout; + } + +if (cfg->fc_dst_len <32 && (ntohl (cfg->fc_dst) << cfg->fc_dst_len)) { +NL_SET_ERR_MSG (extack,"Invalid prefix for given prefix length" ); + err = -EINVAL ; + goto errout; + } +if (cfg->fc_nh_id) {if (cfg->fc_oif || cfg->fc_gw_family || cfg->fc_encap || cfg->fc_mp) {NL_SET_ERR_MSG (extack,"Nexthop specification and nexthop id are mutually exclusive" ); -return -EINVAL ; + err = -EINVAL ; + goto errout; } }if (has_gw && has_via) {NL_SET_ERR_MSG (extack,"Nexthop configuration can not contain both GATEWAY and VIA" ); -return -EINVAL ; + err = -EINVAL ; + goto errout; }if (!cfg->fc_table)
net/ipv4/fib_trie.c +0 -22
@@ -1187,22 +1187,6 @@ static int fib_insert_alias(struct trie *t, struct key_vector *tp,return 0 ; } -static bool fib_valid_key_len (u32 key,u8 plen,struct netlink_ext_ack *extack) -{ -if (plen >KEYLENGTH ) { -NL_SET_ERR_MSG (extack,"Invalid prefix length" ); -return false; - } - -if ((plen <KEYLENGTH ) && (key << plen)) { -NL_SET_ERR_MSG (extack, -"Invalid prefix for given prefix length" ); -return false; - } - -return true; -} -static void fib_remove_alias (struct trie *t,struct key_vector *tp,struct key_vector *l,struct fib_alias *old);
@@ -1223,9 +1207,6 @@ int fib_table_insert(struct net *net, struct fib_table *tb, key =ntohl (cfg->fc_dst); -if (!fib_valid_key_len (key, plen, extack)) -return -EINVAL ; -pr_debug ("Insert table=%u %08x/%d\n" , tb->tb_id, key, plen); fi =fib_create_info (cfg, extack);
@@ -1717,9 +1698,6 @@ int fib_table_delete(struct net *net, struct fib_table *tb, key =ntohl (cfg->fc_dst); -if (!fib_valid_key_len (key, plen, extack)) -return -EINVAL ; - l =fib_find_node (t, &tp, key);if (!l)return -ESRCH ;
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v1 net-next 11/12] ipv4: fib: Move fib_valid_key_len() to rtm_to_fib_config().
Quoted 14 lines · Kuniyuki Iwashima
fib_valid_key_len()is called in the beginning offib_table_insert()orfib_table_delete()to check if the prefix length is valid.
fib_table_insert()andfib_table_delete()are called from 3 paths-
ip_rt_ioctl()
-inet_rtm_newroute()/inet_rtm_delroute()
-fib_magic()In the first
ioctl()path,rtentry_to_fib_config()checks the prefix length withbad_mask(). Also,fib_magic()always passes the correct
prefix: 32 orifa->ifa_prefixlen, which is already validated.
Quoted 6 lines
Let's move
fib_valid_key_len()to the rtnetlink path,rtm_to_fib_config().While at it, 2 direct returns in
rtm_to_fib_config()are changed to goto to match other places in the same functionSigned-off-by Kuniyuki Iwashima <[email protected]>
PATCH 12/12 ipv4: fib: Convert RTM_NEWROUTE and RTM_DELROUTE to per-netns RTNL.
In reply to Kuniyuki IwashimaWe converted fib_info hash tables to per-netns one and now ready to convert RTM_NEWROUTE and RTM_DELROUTE to per-netns RTNL.
Let's hold rtnl_net_lock() in inet_rtm_newroute() and inet_rtm_delroute().
--- net/ipv4/fib_frontend.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
net/ipv4/fib_frontend.c +14 -5
@@ -884,20 +884,24 @@ static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,if (err <0 ) goto errout; +rtnl_net_lock (net); +if (cfg.fc_nh_id && !nexthop_find_by_id (net, cfg.fc_nh_id)) {NL_SET_ERR_MSG (extack,"Nexthop id does not exist" ); err = -EINVAL ; - goto errout; + goto unlock; } tb =fib_get_table (net, cfg.fc_table);if (!tb) {NL_SET_ERR_MSG (extack,"FIB table does not exist" ); err = -ESRCH ; - goto errout; + goto unlock; } err =fib_table_delete (net, tb, &cfg, extack); +unlock: +rtnl_net_unlock (net); errout:return err; }
@@ -914,15 +918,20 @@ static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,if (err <0 ) goto errout; +rtnl_net_lock (net); + tb =fib_new_table (net, cfg.fc_table);if (!tb) { err = -ENOBUFS ; - goto errout; + goto unlock; } err =fib_table_insert (net, tb, &cfg, extack);if (!err && cfg.fc_type ==RTN_LOCAL ) net->ipv4.fib_has_custom_local_routes = true; + +unlock: +rtnl_net_unlock (net); errout:return err; }
@@ -1681,9 +1690,9 @@ static struct pernet_operations fib_net_ops = {static const struct rtnl_msg_handler fib_rtnl_msg_handlers[]__initconst = { {.protocol =PF_INET , .msgtype =RTM_NEWROUTE , - .doit = inet_rtm_newroute}, + .doit = inet_rtm_newroute, .flags =RTNL_FLAG_DOIT_PERNET }, {.protocol =PF_INET , .msgtype =RTM_DELROUTE , - .doit = inet_rtm_delroute}, + .doit = inet_rtm_delroute, .flags =RTNL_FLAG_DOIT_PERNET }, {.protocol =PF_INET , .msgtype =RTM_GETROUTE , .dumpit = inet_dump_fib, .flags =RTNL_FLAG_DUMP_UNLOCKED |RTNL_FLAG_DUMP_SPLIT_NLM_DONE }, };
Signature · 1 line
2.39.5 (Apple Git-154)
Quoted 7 lines · Kuniyuki Iwashima
We converted
fib_infohash tables to per-netns one and now ready to convertRTM_NEWROUTEandRTM_DELROUTEto per-netns RTNL.Let's hold
rtnl_net_lock()ininet_rtm_newroute()andinet_rtm_delroute().Signed-off-by Kuniyuki Iwashima <[email protected]>
Jakub Kicinski
maintainerQuoted 3 lines · On Tue, 25 Feb 2025 10:22:38 -0800 Kuniyuki Iwashima wrote:
Patch 1 is a misc cleanup.
Patch 2 ~ 8 converts twofib_infohash tables to per-netns. Patch 9 ~ 12 convertsrtnl_lock()tortnl_net_lcok().
Breaks quite a few tests :(
unreferenced object 0xffff88800bfc6800 (size 256):
comm "ip", pid 577, jiffies 4294699578 hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
backtrace (crc 0):
__kmalloc_node_noprof+0x35d/0x4a0
fib4_semantics_init+0x25/0xf0
fib_net_init+0x17e/0x340
ops_init+0x189/0x550
setup_net+0x189/0x750
copy_net_ns+0x1f7/0x340
create_new_namespaces+0x35f/0x920
unshare_nsproxy_namespaces+0x8d/0x130
ksys_unshare+0x2a9/0x660
__x64_sys_unshare+0x31/0x40
do_syscall_64+0xc1/0x1d0
entry_SYSCALL_64_after_hwframe+0x77/0x7fSignature · 1 line
pw-bot: cr
From: Jakub Kicinski <[email protected]>
Date: Tue, 25 Feb 2025 16:24:48 -0800
Quoted 6 lines · On Tue, 25 Feb 2025 10:22:38 -0800 Kuniyuki Iwashima wrote:
Quoted 3 lines
Patch 1 is a misc cleanup.
Patch 2 ~ 8 converts twofib_infohash tables to per-netns. Patch 9 ~ 12 convertsrtnl_lock()tortnl_net_lcok().Breaks quite a few tests :(
Oh, sorry... why I didn't notice this silly mistake :/ I enabled kmemleak on my debug config.
Will fix in v2.
---8<---
net/ipv4/fib_frontend.c +1 -1
@@ -1666,7 +1666,7 @@ static void __net_exit fib_net_exit(struct net *net) {fib_proc_exit (net);nl_fib_lookup_exit (net); -fib4_semantics_init (net); +fib4_semantics_exit (net); } static void __net_exit fib_net_exit_batch(struct list_head *net_list)
---8<---
Quoted 10 lines
unreferenced object
0xffff88800bfc6800(size 256):
comm "ip", pid 577, jiffies 4294699578 hex dump (first 32 bytes):00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace (crc 0): __kmalloc_node_noprof+0x35d/0x4a0 fib4_semantics_init+0x25/0xf0 fib_net_init+0x17e/0x340