Improve neigh_flush_dev performance
Subsystems (4)
PATCH v8 1/6 neighbour: Add hlist_node to struct neighbour
In reply to Gilad NaamanAdd a doubly-linked node to neighbours, so that they can be deleted without iterating the entire bucket they're in.
--- include/net/neighbour.h | 2 ++ net/core/neighbour.c | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-)
include/net/neighbour.h +2 -0
@@ -136,6 +136,7 @@ struct neigh_statistics {struct neighbour {struct neighbour __rcu *next; +struct hlist_node hash;struct neigh_table *tbl;struct neigh_parms *parms;unsigned long confirmed;
@@ -191,6 +192,7 @@ struct pneigh_entry {struct neigh_hash_table {struct neighbour __rcu **hash_buckets; +struct hlist_head *hash_heads;unsigned int hash_shift;__u32 hash_rnd[NEIGH_NUM_HASH_RND ];struct rcu_head rcu;
net/core/neighbour.c +19 -1
@@ -216,6 +216,7 @@ static bool neigh_del(struct neighbour *n, struct neighbour __rcu **np, neigh =rcu_dereference_protected (n->next,lockdep_is_held (&tbl->lock));rcu_assign_pointer (*np, neigh); +hlist_del_rcu (&n->hash);neigh_mark_dead (n); retval = true; }
@@ -402,6 +403,7 @@ static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev,rcu_assign_pointer (*np,rcu_dereference_protected (n->next,lockdep_is_held (&tbl->lock))); +hlist_del_rcu (&n->hash);write_lock (&n->lock);neigh_del_timer (n);neigh_mark_dead (n);
@@ -529,20 +531,30 @@ static void neigh_get_hash_rnd(u32 *x)static struct neigh_hash_table *neigh_hash_alloc (unsigned int shift) { +size_t hash_heads_size = (1 << shift) *sizeof (struct hlist_head );size_t size = (1 << shift) *sizeof (struct neighbour *); -struct neigh_hash_table *ret;struct neighbour __rcu **buckets; +struct hlist_head *hash_heads; +struct neigh_hash_table *ret;int i; ret =kmalloc (sizeof (*ret),GFP_ATOMIC );if (!ret)return NULL ; + buckets =kvzalloc (size,GFP_ATOMIC );if (!buckets) {kfree (ret);return NULL ; } + hash_heads =kvzalloc (hash_heads_size,GFP_ATOMIC ); +if (!hash_heads) { +kvfree (buckets); +kfree (ret); +return NULL ; + } ret->hash_buckets = buckets; + ret->hash_heads = hash_heads; ret->hash_shift = shift;for (i =0 ; i <NEIGH_NUM_HASH_RND ; i++)neigh_get_hash_rnd (&ret->hash_rnd[i]);
@@ -556,6 +568,7 @@ static void neigh_hash_free_rcu(struct rcu_head *head) rcu);kvfree (nht->hash_buckets); +kvfree (nht->hash_heads);kfree (nht); }
@@ -592,6 +605,8 @@ static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl, new_nht->hash_buckets[hash], lockdep_is_held(&tbl->lock)));rcu_assign_pointer (new_nht->hash_buckets[hash], n); +hlist_del_rcu (&n->hash); +hlist_add_head_rcu (&n->hash, &new_nht->hash_heads[hash]); } }
@@ -702,6 +717,7 @@ ___neigh_create(struct neigh_table *tbl, const void *pkey,rcu_dereference_protected (nht->hash_buckets[hash_val], lockdep_is_held(&tbl->lock)));rcu_assign_pointer (nht->hash_buckets[hash_val], n); +hlist_add_head_rcu (&n->hash, &nht->hash_heads[hash_val]);write_unlock_bh (&tbl->lock);neigh_dbg (2 ,"neigh %p is created\n" , n); rc = n;
@@ -987,6 +1003,7 @@ static void neigh_periodic_work(struct work_struct *work)rcu_assign_pointer (*np,rcu_dereference_protected (n->next,lockdep_is_held (&tbl->lock))); +hlist_del_rcu (&n->hash);neigh_mark_dead (n);write_unlock (&n->lock);neigh_cleanup_and_release (n);
@@ -3116,6 +3133,7 @@ void __neigh_for_each_release(struct neigh_table *tbl,rcu_assign_pointer (*np,rcu_dereference_protected (n->next,lockdep_is_held (&tbl->lock))); +hlist_del_rcu (&n->hash);neigh_mark_dead (n); }else np = &n->next;
Signature · 1 line
2.34.1
PATCH v8 2/6 neighbour: Define neigh_for_each_in_bucket
In reply to Gilad NaamanIntroduce neigh_for_each_in_bucket in neighbour.h, to help iterate over the neighbour table more succinctly.
--- include/net/neighbour.h | 4 ++++ 1 file changed, 4 insertions(+)
include/net/neighbour.h +4 -0
@@ -277,6 +277,10 @@ static inline void *neighbour_priv(const struct neighbour *n)extern const struct nla_policy nda_policy[]; +#define neigh_for_each_in_bucket (pos, head) hlist_for_each_entry(pos, head, hash) +#define neigh_for_each_in_bucket_safe (pos, tmp, head) \ + hlist_for_each_entry_safe(pos, tmp, head, hash) +static inline bool neigh_key_eq32 (const struct neighbour *n,const void *pkey) {return *(const u32 *)n->primary_key == *(const u32 *)pkey;
Signature · 1 line
2.34.1
PATCH v8 4/6 neighbour: Convert iteration to use hlist+macro
In reply to Gilad NaamanRemove all usage of the bare neighbour::next pointer, replacing them with neighbour::hash and its for_each macro.
--- include/net/neighbour.h | 5 +---- net/core/neighbour.c | 47 ++++++++++++++++------------------------- 2 files changed, 19 insertions(+), 33 deletions(-)
include/net/neighbour.h +1 -4
@@ -309,12 +309,9 @@ static inline struct neighbour *___neigh_lookup_noref(u32 hash_val; hash_val =hash (pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); -for (n =rcu_dereference (nht->hash_buckets[hash_val]); - n !=NULL ; - n =rcu_dereference (n->next)) { +neigh_for_each_in_bucket (n, &nht->hash_heads[hash_val])if (n->dev == dev &&key_eq (n, pkey))return n; - }return NULL ; }
net/core/neighbour.c +18 -29
112 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v8 3/6 neighbour: Convert seq_file functions to use hlist
In reply to Gilad NaamanConvert seq_file-related neighbour functionality to use neighbour::hash and the related for_each macro.
--- net/core/neighbour.c | 104 ++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 56 deletions(-)
net/core/neighbour.c +48 -56
@@ -3193,43 +3193,53 @@ EXPORT_SYMBOL(neigh_xmit);#ifdef CONFIG_PROC_FS -static struct neighbour *neigh_get_first (struct seq_file *seq) +static struct neighbour *neigh_get_valid (struct seq_file *seq, +struct neighbour *n, +loff_t *pos) {struct neigh_seq_state *state = seq->private;struct net *net =seq_file_net (seq); + +if (!net_eq (dev_net (n->dev), net)) +return NULL ; + +if (state->neigh_sub_iter) { +loff_t fakep =0 ; +void *v; + + v = state->neigh_sub_iter (state, n, pos ? pos : &fakep); +if (!v) +return NULL ; +if (pos) +return v; + } + +if (!(state->flags &NEIGH_SEQ_SKIP_NOARP )) +return n; + +if (READ_ONCE (n->nud_state) & ~NUD_NOARP ) +return n; + +return NULL ; +} + +static struct neighbour *neigh_get_first (struct seq_file *seq) +{ +struct neigh_seq_state *state = seq->private;struct neigh_hash_table *nht = state->nht; -struct neighbour *n =NULL ; -int bucket; +struct neighbour *n, *tmp; state->flags &= ~NEIGH_SEQ_IS_PNEIGH ; -for (bucket =0 ; bucket < (1 << nht->hash_shift); bucket++) { - n =rcu_dereference (nht->hash_buckets[bucket]); - -while (n) { -if (!net_eq (dev_net (n->dev), net)) - goto next; -if (state->neigh_sub_iter) { -loff_t fakep =0 ; -void *v; - v = state->neigh_sub_iter (state, n, &fakep); -if (!v) - goto next; - } -if (!(state->flags &NEIGH_SEQ_SKIP_NOARP )) -break ; -if (READ_ONCE (n->nud_state) & ~NUD_NOARP ) -break ; -next: - n =rcu_dereference (n->next); +while (++state->bucket < (1 << nht->hash_shift)) { +neigh_for_each_in_bucket (n, &nht->hash_heads[state->bucket]) { + tmp =neigh_get_valid (seq, n,NULL ); +if (tmp) +return tmp; } - -if (n) -break ; } - state->bucket = bucket; -return n; +return NULL ; } static struct neighbour *neigh_get_next(struct seq_file *seq,
@@ -3237,46 +3247,28 @@ static struct neighbour *neigh_get_next(struct seq_file *seq, loff_t *pos) {struct neigh_seq_state *state = seq->private; -struct net *net =seq_file_net (seq); -struct neigh_hash_table *nht = state->nht; +struct neighbour *tmp;if (state->neigh_sub_iter) {void *v = state->neigh_sub_iter (state, n, pos); +if (v)return n; } - n =rcu_dereference (n->next); - -while (1 ) { -while (n) { -if (!net_eq (dev_net (n->dev), net)) - goto next; -if (state->neigh_sub_iter) { -void *v = state->neigh_sub_iter (state, n, pos); -if (v) -return n; - goto next; - } -if (!(state->flags &NEIGH_SEQ_SKIP_NOARP )) -break ; -if (READ_ONCE (n->nud_state) & ~NUD_NOARP ) -break ; -next: - n =rcu_dereference (n->next); +hlist_for_each_entry_continue (n, hash) { + tmp =neigh_get_valid (seq, n, pos); +if (tmp) { + n = tmp; + goto out; } - -if (n) -break ; - -if (++state->bucket >= (1 << nht->hash_shift)) -break ; - - n =rcu_dereference (nht->hash_buckets[state->bucket]); } + n =neigh_get_first (seq); +out:if (n && pos) --(*pos); +return n; }
@@ -3379,7 +3371,7 @@ void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tblstruct neigh_seq_state *state = seq->private; state->tbl = tbl; - state->bucket =0 ; + state->bucket =-1 ; state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH );rcu_read_lock ();
Signature · 1 line
2.34.1
PATCH v8 5/6 neighbour: Remove bare neighbour::next pointer
In reply to Gilad NaamanRemove the now-unused neighbour::next pointer, leaving struct neighbour solely with the hlist_node implementation.
--- include/net/neighbour.h | 4 +- net/core/neighbour.c | 90 +++++------------------------------------ net/ipv4/arp.c | 2 +- 3 files changed, 12 insertions(+), 84 deletions(-)
include/net/neighbour.h +1 -3
@@ -135,7 +135,6 @@ struct neigh_statistics {#define NEIGH_CACHE_STAT_INC (tbl, field) this_cpu_inc((tbl)->stats->field)struct neighbour { -struct neighbour __rcu *next;struct hlist_node hash;struct neigh_table *tbl;struct neigh_parms *parms;
@@ -191,7 +190,6 @@ struct pneigh_entry {#define NEIGH_NUM_HASH_RND 4struct neigh_hash_table { -struct neighbour __rcu **hash_buckets;struct hlist_head *hash_heads;unsigned int hash_shift;__u32 hash_rnd[NEIGH_NUM_HASH_RND ];
@@ -352,7 +350,7 @@ int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb,int neigh_update (struct neighbour *neigh,const u8 *lladdr,u8 new,u32 flags,u32 nlmsg_pid);void __neigh_set_probe_once (struct neighbour *neigh); -bool neigh_remove_one (struct neighbour *ndel,struct neigh_table *tbl); +bool neigh_remove_one (struct neighbour *ndel);void neigh_changeaddr (struct neigh_table *tbl,struct net_device *dev);int neigh_ifdown (struct neigh_table *tbl,struct net_device *dev);int neigh_carrier_down (struct neigh_table *tbl,struct net_device *dev);
net/core/neighbour.c +10 -80
212 lines not on this page · raw message
net/ipv4/arp.c +1 -1
@@ -1215,7 +1215,7 @@ int arp_invalidate(struct net_device *dev, __be32 ip, bool force) NEIGH_UPDATE_F_ADMIN, 0);write_lock_bh (&tbl->lock);neigh_release (neigh); -neigh_remove_one (neigh, tbl); +neigh_remove_one (neigh);write_unlock_bh (&tbl->lock); }
Signature · 1 line
2.34.1
PATCH v8 6/6 neighbour: Create netdev->neighbour association
In reply to Gilad NaamanCreate a mapping between a netdev and its neighoburs, allowing for much cheaper flushes.
--- .../networking/net_cachelines/net_device.rst | 1 + include/linux/netdevice.h | 7 ++ include/net/neighbour.h | 9 +- include/net/neighbour_tables.h | 12 +++ net/core/neighbour.c | 96 +++++++++++-------- 5 files changed, 80 insertions(+), 45 deletions(-) create mode 100644 include/net/neighbour_tables.h
Documentation/networking/net_cachelines/net_device.rst +1 -0
@@ -188,4 +188,5 @@ u64 max_pacing_offload_horizon struct_napi_config* napi_config unsigned_long gro_flush_timeout u32 napi_defer_hard_irqs +struct hlist_head neighbours[2] =================================== =========================== =================== =================== ===================================================================================
include/linux/netdevice.h +7 -0
@@ -52,6 +52,7 @@#include <net/net_trackers.h> #include <net/net_debug.h> #include <net/dropreason-core.h> +#include <net/neighbour_tables.h> struct netpoll_info ;struct device ;
@@ -2032,6 +2033,9 @@ enum netdev_reg_state {* @napi_defer_hard_irqs: If not zero, provides a counter that would * allow to avoid NIC hard IRQ, on busy queues. * +* @neighbours: List heads pointing to this device's neighbours' +* dev_list, one per address-family. +* * FIXME: cleanup struct net_device such that network protocol info * moves out. */
@@ -2440,6 +2444,9 @@ struct net_device {*/ struct net_shaper_hierarchy *net_shaper_hierarchy;#endif + +struct hlist_head neighbours[NEIGH_NR_TABLES ]; + u8 priv[] ____cacheline_aligned__counted_by (priv_len ); }____cacheline_aligned ;
include/net/neighbour.h +2 -7
@@ -29,6 +29,7 @@#include <linux/sysctl.h> #include <linux/workqueue.h> #include <net/rtnetlink.h> +#include <net/neighbour_tables.h> /* * NUD stands for "neighbor unreachability detection"
@@ -136,6 +137,7 @@ struct neigh_statistics {struct neighbour {struct hlist_node hash; +struct hlist_node dev_list;struct neigh_table *tbl;struct neigh_parms *parms;unsigned long confirmed;
@@ -236,13 +238,6 @@ struct neigh_table {struct pneigh_entry **phash_buckets; }; -enum { -NEIGH_ARP_TABLE =0 , -NEIGH_ND_TABLE =1 , -NEIGH_NR_TABLES , -NEIGH_LINK_TABLE =NEIGH_NR_TABLES /* Pseudo table for neigh_xmit */ -}; -static inline int neigh_parms_family (struct neigh_parms *p) {return p->tbl->family;
include/net/neighbour_tables.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _NET_NEIGHBOUR_TABLES_H +#define _NET_NEIGHBOUR_TABLES_H + +enum { +NEIGH_ARP_TABLE =0 , +NEIGH_ND_TABLE =1 , +NEIGH_NR_TABLES , +NEIGH_LINK_TABLE =NEIGH_NR_TABLES /* Pseudo table for neigh_xmit */ +}; + +#endif
net/core/neighbour.c +58 -38
136 lines not on this page · raw message
Signature · 1 line
2.34.1
v8 cover Improve neigh_flush_dev performance
This patchsets improves the performance of neigh_flush_dev.
Currently, the only way to implement it requires traversing all neighbours known to the kernel, across all network-namespaces.
This means that some flows are slowed down as a function of neigh-scale, even if the specific link they're handling has little to no neighbours.
In order to solve this, this patchset adds a netdev->neighbours list, as well as making the original linked-list doubly-, so that it is possible to unlink neighbours without traversing the hash-bucket to obtain the previous neighbour.
The original use-case we encountered was mass-deletion of links (12K VLANs) while there are 50K ARPs and 50K NDPs in the system; though the slowdowns would also appear when the links are set down.
Changes in v8:
- Rebase and absorb alloc_pages->kvzalloc changes
Gilad Naaman (6):
neighbour: Add hlist_node to struct neighbour
neighbour: Define neigh_for_each_in_bucket
neighbour: Convert seq_file functions to use hlist
neighbour: Convert iteration to use hlist+macro
neighbour: Remove bare neighbour::next pointer
neighbour: Create netdev->neighbour association
.../networking/net_cachelines/net_device.rst | 1 + include/linux/netdevice.h | 7 + include/net/neighbour.h | 24 +- include/net/neighbour_tables.h | 12 + net/core/neighbour.c | 325 ++++++++---------- net/ipv4/arp.c | 2 +- 6 files changed, 168 insertions(+), 203 deletions(-) create mode 100644 include/net/neighbour_tables.h
Signature · 1 line
2.34.1
Re: [PATCH net-next v8 1/6] neighbour: Add hlist_node to struct neighbour
In reply to Gilad NaamanFrom: Gilad Naaman <[email protected]>
Date: Mon, 4 Nov 2024 08:04:29 +0000
Quoted 4 lines
Add a doubly-linked node to neighbours, so that they can be deleted without iterating the entire bucket they're in.
Signed-off-by Gilad Naaman <[email protected]>
Re: [PATCH net-next v8 2/6] neighbour: Define neigh_for_each_in_bucket
In reply to Gilad NaamanFrom: Gilad Naaman <[email protected]>
Date: Mon, 4 Nov 2024 08:04:30 +0000
Quoted 4 lines
Introduce
neigh_for_each_in_bucketinneighbour.h, to help iterate over the neighbour table more succinctly.Signed-off-by Gilad Naaman <[email protected]>
Re: [PATCH net-next v8 3/6] neighbour: Convert seq_file functions to use hlist
In reply to Gilad NaamanFrom: Gilad Naaman <[email protected]>
Date: Mon, 4 Nov 2024 08:04:31 +0000
Quoted 4 lines
Convert seq_file-related neighbour functionality to use
neighbour::hashand the relatedfor_eachmacro.Signed-off-by Gilad Naaman <[email protected]>
Re: [PATCH net-next v8 4/6] neighbour: Convert iteration to use hlist+macro
In reply to Gilad NaamanFrom: Gilad Naaman <[email protected]>
Date: Mon, 4 Nov 2024 08:04:32 +0000
Quoted 4 lines
Remove all usage of the bare
neighbour::nextpointer, replacing them withneighbour::hashand itsfor_eachmacro.Signed-off-by Gilad Naaman <[email protected]>
Re: [PATCH net-next v8 4/6] neighbour: Convert iteration to use hlist+macro
In reply to Gilad NaamanFrom: Gilad Naaman <[email protected]>
Date: Mon, 4 Nov 2024 08:04:32 +0000
include/net/neighbour.h static inline struct neighbour *___neigh_lookup_noref( · line 312
Quoted 12 lines
include/net/neighbour.h +1
-3@@ -309,12 +309,9 @@ static inline struct neighbour *___neigh_lookup_noref(u32 hash_val; hash_val =hash (pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); -for (n =rcu_dereference (nht->hash_buckets[hash_val]); - n !=NULL ; - n =rcu_dereference (n->next)) { +neigh_for_each_in_bucket (n, &nht->hash_heads[hash_val])
Sorry, I missed this part needs to be _rcu version.
You can keep my Reviewed-by tag in v9.
Quoted 6 lines
include/net/neighbour.h +0
-1if (n->dev == dev &&key_eq (n, pkey))return n; - }return NULL ; }
Re: [PATCH net-next v8 5/6] neighbour: Remove bare neighbour::next pointer
In reply to Gilad NaamanFrom: Gilad Naaman <[email protected]>
Date: Mon, 4 Nov 2024 08:04:33 +0000
Quoted 4 lines
Remove the now-unused
neighbour::nextpointer, leaving struct neighbour solely with thehlist_nodeimplementation.Signed-off-by Gilad Naaman <[email protected]>
Re: [PATCH net-next v8 4/6] neighbour: Convert iteration to use hlist+macro
In reply to Kuniyuki IwashimaQuoted 15 lines
include/net/neighbour.h static inline struct neighbour *___neigh_lookup_noref( · line 312
Quoted 12 lines
include/net/neighbour.h +1
-3@@ -309,12 +309,9 @@ static inline struct neighbour *___neigh_lookup_noref(u32 hash_val; hash_val =hash (pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); -for (n =rcu_dereference (nht->hash_buckets[hash_val]); - n !=NULL ; - n =rcu_dereference (n->next)) { +neigh_for_each_in_bucket (n, &nht->hash_heads[hash_val])Sorry, I missed this part needs to be
_rcuversion.
Quoted 2 lines
You can keep my Reviewed-by tag in v9.
No problem at all, will do.
Is it possible that the _rcu version will also be needed in neigh_dump_table? It is called from an rcu_read_locked section, and it doesn't hold the table-lock.
Re: [PATCH net-next v8 4/6] neighbour: Convert iteration to use hlist+macro
From: Gilad Naaman <[email protected]>
Date: Thu, 7 Nov 2024 05:39:46 +0000
Quoted 15 lines
Quoted 15 lines
include/net/neighbour.h static inline struct neighbour *___neigh_lookup_noref( · line 312
Quoted 12 lines
include/net/neighbour.h +1
-3@@ -309,12 +309,9 @@ static inline struct neighbour *___neigh_lookup_noref(u32 hash_val; hash_val =hash (pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); -for (n =rcu_dereference (nht->hash_buckets[hash_val]); - n !=NULL ; - n =rcu_dereference (n->next)) { +neigh_for_each_in_bucket (n, &nht->hash_heads[hash_val])Sorry, I missed this part needs to be
_rcuversion.
Quoted 7 lines
Quoted 2 lines
You can keep my Reviewed-by tag in v9.
No problem at all, will do.
Is it possible that the
_rcuversion will also be needed inneigh_dump_table? It is called from anrcu_read_locked section, and it doesn't hold the table-lock.
Right, there also should use _rcu one.