Skip to contentSkip to conversation

Improve neigh_flush_dev performance

PATCH Superseded Gilad Naaman 3 participants 9 messages Oct 6, 2024 – Oct 9, 2024

Subsystems (3)

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v2 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 neighbour-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 v2:

- Fix RCU usage
- Document new net_device->neighbours field

Gilad Naaman (2):
Convert neighbour-table to use hlist
Create netdev->neighbour association

 .../networking/net_cachelines/net_device.rst  |   1 +
 include/linux/netdevice.h                     |   6 +
 include/net/neighbour.h                       |  18 +-
 include/net/neighbour_tables.h                |  13 +
 net/core/neighbour.c                          | 224 +++++++++---------
 5 files changed, 144 insertions(+), 118 deletions(-)
 create mode 100644 include/net/neighbour_tables.h
Signature · 1 line
2.46.0
#1 lore raw

PATCH v2 1/2 Convert neighbour-table to use hlist

Use doubly-linked instead of singly-linked list when linking neighbours, so that it is possible to remove neighbours without traversing the entire table.

---
 include/net/neighbour.h |   8 +--
 net/core/neighbour.c    | 124 ++++++++++++++--------------------------
 2 files changed, 46 insertions(+), 86 deletions(-)
include/net/neighbour.h +4 -4
index a44f262a7384..5dde118323e3 100644
@@ -135,7 +135,7 @@ 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	list;
 	struct neigh_table	*tbl;
 	struct neigh_parms	*parms;
 	unsigned long		confirmed;
@@ -190,7 +190,7 @@ struct pneigh_entry {
 #define NEIGH_NUM_HASH_RND	4
 
 struct neigh_hash_table {
-	struct neighbour __rcu	**hash_buckets;
+	struct hlist_head	*hash_buckets;
 	unsigned int		hash_shift;
 	__u32			hash_rnd[NEIGH_NUM_HASH_RND];
 	struct rcu_head		rcu;
@@ -304,9 +304,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]);
+	for (n = (struct neighbour *)rcu_dereference(hlist_first_rcu(&nht->hash_buckets[hash_val]));
 	     n != NULL;
-	     n = rcu_dereference(n->next)) {
+	     n = (struct neighbour *)rcu_dereference(hlist_next_rcu(&n->list))) {
 		if (n->dev == dev && key_eq(n, pkey))
 			return n;
 	}
net/core/neighbour.c +42 -82

285 lines not on this page · raw message

Signature · 1 line
2.46.0
#2 lore raw

Re: [PATCH net-next v2 1/2] Convert neighbour-table to use hlist

From: Gilad Naaman <[email protected]>
Date: Sun, 6 Oct 2024 06:47:42 +0000

Quoted 33 lines

Use doubly-linked instead of singly-linked list when linking neighbours, so that it is possible to remove neighbours without traversing the entire table.

---
 include/net/neighbour.h |   8 +--
 net/core/neighbour.c    | 124 ++++++++++++++--------------------------
 2 files changed, 46 insertions(+), 86 deletions(-)
include/net/neighbour.h +2 -2
index a44f262a7384..5dde118323e3 100644
@@ -135,7 +135,7 @@ 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	list;
 	struct neigh_table	*tbl;
 	struct neigh_parms	*parms;
 	unsigned long		confirmed;
@@ -190,7 +190,7 @@ struct pneigh_entry {
 #define NEIGH_NUM_HASH_RND	4
 
 struct neigh_hash_table {
-	struct neighbour __rcu	**hash_buckets;
+	struct hlist_head	*hash_buckets;
 	unsigned int		hash_shift;
 	__u32			hash_rnd[NEIGH_NUM_HASH_RND];
 	struct rcu_head		rcu;

include/net/neighbour.h static inline struct neighbour *___neigh_lookup_noref( · line 307

Quoted 5 lines
include/net/neighbour.h +1 -1
@@ -304,9 +304,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]);
+	for (n = (struct neighbour *)rcu_dereference(hlist_first_rcu(&nht->hash_buckets[hash_val]));

This for loop and hlist_first_rcu(&nht->hash_buckets[hash_val]) can also be written with a macro and an inline function.

include/net/neighbour.h static inline struct neighbour *___neigh_lookup_noref( · line 309

Quoted 3 lines
include/net/neighbour.h +1 -1
 	     n != NULL;
-	     n = rcu_dereference(n->next)) {
+	     n = (struct neighbour *)rcu_dereference(hlist_next_rcu(&n->list))) {

This part is also reused multiple times so should be an inline function.

I have similar patches for struct in_ifaddr.ifa_next (not upstreamed yet), and this will be a good example for you.
https://github.com/q2ven/linux/commit/a51fdf7ccc14bf6edba58bacf7faaeebe811d41b

Quoted 64 lines
include/net/neighbour.h +0 -0
 		if (n->dev == dev && key_eq(n, pkey))
 			return n;
 	}
net/core/neighbour.c +4 -26
index 77b819cd995b..86b174baae27 100644
@@ -37,6 +37,7 @@
 #include <linux/string.h>
 #include <linux/log2.h>
 #include <linux/inetdevice.h>
+#include <linux/rculist.h>
 #include <net/addrconf.h>
 
 #include <trace/events/neigh.h>
@@ -205,18 +206,13 @@ static void neigh_update_flags(struct neighbour *neigh, u32 flags, int *notify,
 	}
 }
 
-static bool neigh_del(struct neighbour *n, struct neighbour __rcu **np,
-		      struct neigh_table *tbl)
+static bool neigh_del(struct neighbour *n, struct neigh_table *tbl)
 {
 	bool retval = false;
 
 	write_lock(&n->lock);
 	if (refcount_read(&n->refcnt) == 1) {
-		struct neighbour *neigh;
-
-		neigh = rcu_dereference_protected(n->next,
-						  lockdep_is_held(&tbl->lock));
-		rcu_assign_pointer(*np, neigh);
+		hlist_del_rcu(&n->list);
 		neigh_mark_dead(n);
 		retval = true;
 	}
@@ -228,25 +224,7 @@ static bool neigh_del(struct neighbour *n, struct neighbour __rcu **np,
 
 bool neigh_remove_one(struct neighbour *ndel, struct neigh_table *tbl)
 {
-	struct neigh_hash_table *nht;
-	void *pkey = ndel->primary_key;
-	u32 hash_val;
-	struct neighbour *n;
-	struct neighbour __rcu **np;
-
-	nht = rcu_dereference_protected(tbl->nht,
-					lockdep_is_held(&tbl->lock));
-	hash_val = tbl->hash(pkey, ndel->dev, nht->hash_rnd);
-	hash_val = hash_val >> (32 - nht->hash_shift);
-
-	np = &nht->hash_buckets[hash_val];
-	while ((n = rcu_dereference_protected(*np,
-					      lockdep_is_held(&tbl->lock)))) {
-		if (n == ndel)
-			return neigh_del(n, np, tbl);
-		np = &n->next;
-	}
-	return false;
+	return neigh_del(ndel, tbl);
 }
 
 static int neigh_forced_gc(struct neigh_table *tbl)

net/core/neighbour.c static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev, · line 370

Quoted 6 lines
net/core/neighbour.c +2 -1
@@ -388,21 +366,20 @@ static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev,
 
 	for (i = 0; i < (1 << nht->hash_shift); i++) {
 		struct neighbour *n;
-		struct neighbour __rcu **np = &nht->hash_buckets[i];
+		struct neighbour __rcu **np =
+			(struct neighbour __rcu **)&nht->hash_buckets[i].first;

This will be no longer needed for doubly linked list,

net/core/neighbour.c static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev, · line 372

Quoted 3 lines
net/core/neighbour.c +0 -0
 		while ((n = rcu_dereference_protected(*np,
 					lockdep_is_held(&tbl->lock))) != NULL) {

and this while can be converted to the for-loop macro.

Quoted 50 lines
net/core/neighbour.c +8 -10
 			if (dev && n->dev != dev) {
-				np = &n->next;
+				np = (struct neighbour __rcu **)&n->list.next;
 				continue;
 			}
 			if (skip_perm && n->nud_state & NUD_PERMANENT) {
-				np = &n->next;
+				np = (struct neighbour __rcu **)&n->list.next;
 				continue;
 			}
-			rcu_assign_pointer(*np,
-				   rcu_dereference_protected(n->next,
-						lockdep_is_held(&tbl->lock)));
+			hlist_del_rcu(&n->list);
 			write_lock(&n->lock);
 			neigh_del_timer(n);
 			neigh_mark_dead(n);
@@ -530,9 +507,9 @@ static void neigh_get_hash_rnd(u32 *x)
 
 static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
 {
-	size_t size = (1 << shift) * sizeof(struct neighbour *);
+	size_t size = (1 << shift) * sizeof(struct hlist_head);
 	struct neigh_hash_table *ret;
-	struct neighbour __rcu **buckets;
+	struct hlist_head *buckets;
 	int i;
 
 	ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
@@ -541,7 +518,7 @@ static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
 	if (size <= PAGE_SIZE) {
 		buckets = kzalloc(size, GFP_ATOMIC);
 	} else {
-		buckets = (struct neighbour __rcu **)
+		buckets = (struct hlist_head *)
 			  __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
 					   get_order(size));
 		kmemleak_alloc(buckets, size, 1, GFP_ATOMIC);
@@ -562,8 +539,8 @@ static void neigh_hash_free_rcu(struct rcu_head *head)
 	struct neigh_hash_table *nht = container_of(head,
 						    struct neigh_hash_table,
 						    rcu);
-	size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
-	struct neighbour __rcu **buckets = nht->hash_buckets;
+	size_t size = (1 << nht->hash_shift) * sizeof(struct hlist_head);
+	struct hlist_head *buckets = nht->hash_buckets;
 
 	if (size <= PAGE_SIZE) {
 		kfree(buckets);

net/core/neighbour.c static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl, · line 573

Quoted 8 lines
net/core/neighbour.c +3 -2
@@ -591,22 +568,18 @@ static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
 	for (i = 0; i < (1 << old_nht->hash_shift); i++) {
 		struct neighbour *n, *next;
 
-		for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
-						   lockdep_is_held(&tbl->lock));
+		for (n = (struct neighbour *)
+			rcu_dereference_protected(hlist_first_rcu(&old_nht->hash_buckets[i]),
+						  lockdep_is_held(&tbl->lock));

This can be macro.

Quoted 5 lines
net/core/neighbour.c +0 -0
 		     n != NULL;
 		     n = next) {
 			hash = tbl->hash(n->primary_key, n->dev,
 					 new_nht->hash_rnd);
 

net/core/neighbour.c static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl, · line 580

Quoted 10 lines
net/core/neighbour.c +1 -8
 			hash >>= (32 - new_nht->hash_shift);
-			next = rcu_dereference_protected(n->next,
-						lockdep_is_held(&tbl->lock));
-
-			rcu_assign_pointer(n->next,
-					   rcu_dereference_protected(
-						new_nht->hash_buckets[hash],
-						lockdep_is_held(&tbl->lock)));
-			rcu_assign_pointer(new_nht->hash_buckets[hash], n);
+			next = (struct neighbour *)rcu_dereference(hlist_next_rcu(&n->list));

This can be static inline function and reused for for-loop macro.

Quoted 8 lines
net/core/neighbour.c +2 -0
+			hlist_del_rcu(&n->list);
+			hlist_add_head_rcu(&n->list, &new_nht->hash_buckets[hash]);
 		}
 	}
 
@@ -693,11 +666,10 @@ ___neigh_create(struct neigh_table *tbl, const void *pkey,
 		goto out_tbl_unlock;
 	}

net/core/neighbour.c ___neigh_create(struct neigh_table *tbl, const void *pkey, · line 672

Quoted 10 lines
net/core/neighbour.c +4 -5
 
-	for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
-					    lockdep_is_held(&tbl->lock));
-	     n1 != NULL;
-	     n1 = rcu_dereference_protected(n1->next,
-			lockdep_is_held(&tbl->lock))) {
+	hlist_for_each_entry_rcu(n1,
+				 &nht->hash_buckets[hash_val],
+				 list,
+				 lockdep_is_held(&tbl->lock)) {

Let's define hlist_for_each_entry_rcu() as neigh-specific macro.

Quoted 16 lines
net/core/neighbour.c +1 -4
 		if (dev == n1->dev && !memcmp(n1->primary_key, n->primary_key, key_len)) {
 			if (want_ref)
 				neigh_hold(n1);
@@ -713,10 +685,7 @@ ___neigh_create(struct neigh_table *tbl, const void *pkey,
 		list_add_tail(&n->managed_list, &n->tbl->managed_list);
 	if (want_ref)
 		neigh_hold(n);
-	rcu_assign_pointer(n->next,
-			   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->list, &nht->hash_buckets[hash_val]);
 	write_unlock_bh(&tbl->lock);
 	neigh_dbg(2, "neigh %p is created\n", n);
 	rc = n;

net/core/neighbour.c static void neigh_periodic_work(struct work_struct *work) · line 948

Quoted 5 lines
net/core/neighbour.c +1 -1
@@ -976,7 +945,7 @@ static void neigh_periodic_work(struct work_struct *work)
 		goto out;
 
 	for (i = 0 ; i < (1 << nht->hash_shift); i++) {
-		np = &nht->hash_buckets[i];
+		np = (struct neighbour __rcu **)&nht->hash_buckets[i].first;

No np here too,

net/core/neighbour.c static void neigh_periodic_work(struct work_struct *work) · line 950

Quoted 3 lines
net/core/neighbour.c +0 -0
 		while ((n = rcu_dereference_protected(*np,
 				lockdep_is_held(&tbl->lock))) != NULL) {

and for-loop macro here.

Quoted 43 lines
net/core/neighbour.c +4 -10
@@ -999,9 +968,7 @@ static void neigh_periodic_work(struct work_struct *work)
 			    (state == NUD_FAILED ||
 			     !time_in_range_open(jiffies, n->used,
 						 n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
-				rcu_assign_pointer(*np,
-					rcu_dereference_protected(n->next,
-						lockdep_is_held(&tbl->lock)));
+				hlist_del_rcu(&n->list);
 				neigh_mark_dead(n);
 				write_unlock(&n->lock);
 				neigh_cleanup_and_release(n);
@@ -1010,7 +977,7 @@ static void neigh_periodic_work(struct work_struct *work)
 			write_unlock(&n->lock);
 
 next_elt:
-			np = &n->next;
+			np = (struct neighbour __rcu **)&n->list.next;
 		}
 		/*
 		 * It's fine to release lock here, even if hash table
@@ -2728,9 +2695,7 @@ static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
 	for (h = s_h; h < (1 << nht->hash_shift); h++) {
 		if (h > s_h)
 			s_idx = 0;
-		for (n = rcu_dereference(nht->hash_buckets[h]), idx = 0;
-		     n != NULL;
-		     n = rcu_dereference(n->next)) {
+		hlist_for_each_entry_rcu(n, &nht->hash_buckets[h], list) {
 			if (idx < s_idx || !net_eq(dev_net(n->dev), net))
 				goto next;
 			if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
@@ -3097,9 +3062,7 @@ void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void
 	for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
 		struct neighbour *n;
 
-		for (n = rcu_dereference(nht->hash_buckets[chain]);
-		     n != NULL;
-		     n = rcu_dereference(n->next))
+		hlist_for_each_entry_rcu(n, &nht->hash_buckets[chain], list)
 			cb(n, cookie);
 	}
 	read_unlock_bh(&tbl->lock);

net/core/neighbour.c void __neigh_for_each_release(struct neigh_table *tbl, · line 3088

Quoted 7 lines
net/core/neighbour.c +1 -1
@@ -3120,7 +3083,7 @@ void __neigh_for_each_release(struct neigh_table *tbl,
 		struct neighbour *n;
 		struct neighbour __rcu **np;
 
-		np = &nht->hash_buckets[chain];
+		np = (struct neighbour __rcu **)&nht->hash_buckets[chain].first;
 		while ((n = rcu_dereference_protected(*np,
 					lockdep_is_held(&tbl->lock))) != NULL) {

Same here.

Quoted 46 lines
net/core/neighbour.c +5 -11
 			int release;
@@ -3128,12 +3091,10 @@ void __neigh_for_each_release(struct neigh_table *tbl,
 			write_lock(&n->lock);
 			release = cb(n);
 			if (release) {
-				rcu_assign_pointer(*np,
-					rcu_dereference_protected(n->next,
-						lockdep_is_held(&tbl->lock)));
+				hlist_del_rcu(&n->list);
 				neigh_mark_dead(n);
 			} else
-				np = &n->next;
+				np = (struct neighbour __rcu **)&n->list.next;
 			write_unlock(&n->lock);
 			if (release)
 				neigh_cleanup_and_release(n);
@@ -3200,25 +3161,21 @@ static struct neighbour *neigh_get_first(struct seq_file *seq)
 
 	state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
 	for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
-		n = rcu_dereference(nht->hash_buckets[bucket]);
-
-		while (n) {
+		hlist_for_each_entry_rcu(n, &nht->hash_buckets[bucket], list) {
 			if (!net_eq(dev_net(n->dev), net))
-				goto next;
+				continue;
 			if (state->neigh_sub_iter) {
 				loff_t fakep = 0;
 				void *v;
 
 				v = state->neigh_sub_iter(state, n, &fakep);
 				if (!v)
-					goto next;
+					continue;
 			}
 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
 				break;
 			if (READ_ONCE(n->nud_state) & ~NUD_NOARP)
 				break;
-next:
-			n = rcu_dereference(n->next);
 		}
 
 		if (n)

net/core/neighbour.c static struct neighbour *neigh_get_next(struct seq_file *seq, · line 3203

Quoted 6 lines
net/core/neighbour.c +2 -1
@@ -3242,7 +3199,8 @@ static struct neighbour *neigh_get_next(struct seq_file *seq,
 		if (v)
 			return n;
 	}
-	n = rcu_dereference(n->next);
+
+	n = (struct neighbour *)rcu_dereference(hlist_next_rcu(&n->list));

inline helper should be used,

net/core/neighbour.c static struct neighbour *neigh_get_next(struct seq_file *seq, · line 3222

Quoted 10 lines
net/core/neighbour.c +2 -1
 	while (1) {
 		while (n) {
@@ -3260,7 +3218,8 @@ static struct neighbour *neigh_get_next(struct seq_file *seq,
 			if (READ_ONCE(n->nud_state) & ~NUD_NOARP)
 				break;
 next:
-			n = rcu_dereference(n->next);
+
+			n = (struct neighbour *)rcu_dereference(hlist_next_rcu(&n->list));

same here,

net/core/neighbour.c static struct neighbour *neigh_get_next(struct seq_file *seq, · line 3232

Quoted 10 lines
net/core/neighbour.c +2 -1
 		}
 
 		if (n)
@@ -3269,7 +3228,8 @@ static struct neighbour *neigh_get_next(struct seq_file *seq,
 		if (++state->bucket >= (1 << nht->hash_shift))
 			break;
 
-		n = rcu_dereference(nht->hash_buckets[state->bucket]);
+		n = (struct neighbour *)
+		    rcu_dereference(hlist_first_rcu(&nht->hash_buckets[state->bucket]));

and here.

Quoted 5 lines
net/core/neighbour.c +0 -0
 	}
 
 	if (n && pos)
Signature · 1 line
2.46.0
#3 lore raw

Re: [PATCH net-next v2 1/2] Convert neighbour-table to use hlist

Thank you for reviewing this

Quoted 35 lines
Quoted 35 lines

Use doubly-linked instead of singly-linked list when linking neighbours, so that it is possible to remove neighbours without traversing the entire table.

---
 include/net/neighbour.h |   8 +--
 net/core/neighbour.c    | 124 ++++++++++++++--------------------------
 2 files changed, 46 insertions(+), 86 deletions(-)
include/net/neighbour.h +2 -2
index a44f262a7384..5dde118323e3 100644
@@ -135,7 +135,7 @@ 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	list;
 	struct neigh_table	*tbl;
 	struct neigh_parms	*parms;
 	unsigned long		confirmed;
@@ -190,7 +190,7 @@ struct pneigh_entry {
 #define NEIGH_NUM_HASH_RND	4
 
 struct neigh_hash_table {
-	struct neighbour __rcu	**hash_buckets;
+	struct hlist_head	*hash_buckets;
 	unsigned int		hash_shift;
 	__u32			hash_rnd[NEIGH_NUM_HASH_RND];
 	struct rcu_head		rcu;
@@ -304,9 +304,9 @@ static inline struct neighbour *___neigh_lookup_noref(
 	u32 hash_val;
 
Quoted 6 lines

include/net/neighbour.h static inline struct neighbour *___neigh_lookup_noref( · line 307

Quoted 3 lines
include/net/neighbour.h +1 -1
 	hash_val = hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
-	for (n = rcu_dereference(nht->hash_buckets[hash_val]);
+	for (n = (struct neighbour *)rcu_dereference(hlist_first_rcu(&nht->hash_buckets[hash_val]));

This for loop and hlist_first_rcu(&nht->hash_buckets[hash_val]) can also be written with a macro and an inline function.

Good point, I'll convert all of these to use neigh_{first,next}_rcu{,protected}.

Quoted 77 lines

include/net/neighbour.h static inline struct neighbour *___neigh_lookup_noref( · line 309

Quoted 4 lines
include/net/neighbour.h +1 -1
 	     n != NULL;
-	     n = rcu_dereference(n->next)) {
+	     n = (struct neighbour *)rcu_dereference(hlist_next_rcu(&n->list))) {

This part is also reused multiple times so should be an inline function.

I have similar patches for struct in_ifaddr.ifa_next (not upstreamed yet), and this will be a good example for you.
https://github.com/q2ven/linux/commit/a51fdf7ccc14bf6edba58bacf7faaeebe811d41b

Quoted 65 lines
include/net/neighbour.h +0 -0
 		if (n->dev == dev && key_eq(n, pkey))
 			return n;
 	}
net/core/neighbour.c +4 -26
index 77b819cd995b..86b174baae27 100644
@@ -37,6 +37,7 @@
 #include <linux/string.h>
 #include <linux/log2.h>
 #include <linux/inetdevice.h>
+#include <linux/rculist.h>
 #include <net/addrconf.h>
 
 #include <trace/events/neigh.h>
@@ -205,18 +206,13 @@ static void neigh_update_flags(struct neighbour *neigh, u32 flags, int *notify,
 	}
 }
 
-static bool neigh_del(struct neighbour *n, struct neighbour __rcu **np,
-		      struct neigh_table *tbl)
+static bool neigh_del(struct neighbour *n, struct neigh_table *tbl)
 {
 	bool retval = false;
 
 	write_lock(&n->lock);
 	if (refcount_read(&n->refcnt) == 1) {
-		struct neighbour *neigh;
-
-		neigh = rcu_dereference_protected(n->next,
-						  lockdep_is_held(&tbl->lock));
-		rcu_assign_pointer(*np, neigh);
+		hlist_del_rcu(&n->list);
 		neigh_mark_dead(n);
 		retval = true;
 	}
@@ -228,25 +224,7 @@ static bool neigh_del(struct neighbour *n, struct neighbour __rcu **np,
 
 bool neigh_remove_one(struct neighbour *ndel, struct neigh_table *tbl)
 {
-	struct neigh_hash_table *nht;
-	void *pkey = ndel->primary_key;
-	u32 hash_val;
-	struct neighbour *n;
-	struct neighbour __rcu **np;
-
-	nht = rcu_dereference_protected(tbl->nht,
-					lockdep_is_held(&tbl->lock));
-	hash_val = tbl->hash(pkey, ndel->dev, nht->hash_rnd);
-	hash_val = hash_val >> (32 - nht->hash_shift);
-
-	np = &nht->hash_buckets[hash_val];
-	while ((n = rcu_dereference_protected(*np,
-					      lockdep_is_held(&tbl->lock)))) {
-		if (n == ndel)
-			return neigh_del(n, np, tbl);
-		np = &n->next;
-	}
-	return false;
+	return neigh_del(ndel, tbl);
 }
 
 static int neigh_forced_gc(struct neigh_table *tbl)
@@ -388,21 +366,20 @@ static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev,
 
Quoted 7 lines

net/core/neighbour.c static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev, · line 370

Quoted 5 lines
net/core/neighbour.c +2 -1
 	for (i = 0; i < (1 << nht->hash_shift); i++) {
 		struct neighbour *n;
-		struct neighbour __rcu **np = &nht->hash_buckets[i];
+		struct neighbour __rcu **np =
+			(struct neighbour __rcu **)&nht->hash_buckets[i].first;

This will be no longer needed for doubly linked list,

This is not as-necessary with a doubly-linked list, but unfortunately I cannot eliminate it completely, as the n might be released in the loop body.

I can convert this function to use a struct neighour *next instead, if it is more palatable.

Quoted 6 lines

net/core/neighbour.c static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev, · line 372

Quoted 4 lines
net/core/neighbour.c +0 -0
 		while ((n = rcu_dereference_protected(*np,
 					lockdep_is_held(&tbl->lock))) != NULL) {

and this while can be converted to the for-loop macro.

As far as I understand, this cannot be converted into the for-loop macro, as the cursor can be released during the loop-body, resulting in use-after-free when trying to increment it.

Quoted 10 lines
Quoted 10 lines
net/core/neighbour.c +2 -2
 			if (dev && n->dev != dev) {
-				np = &n->next;
+				np = (struct neighbour __rcu **)&n->list.next;
 				continue;
 			}
 			if (skip_perm && n->nud_state & NUD_PERMANENT) {
-				np = &n->next;
+				np = (struct neighbour __rcu **)&n->list.next;
 				continue;
Quoted 8 lines

net/core/neighbour.c static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev, · line 385

Quoted 8 lines
net/core/neighbour.c +1 -3
 			}
-			rcu_assign_pointer(*np,
-				   rcu_dereference_protected(n->next,
-						lockdep_is_held(&tbl->lock)));
+			hlist_del_rcu(&n->list);
 			write_lock(&n->lock);
 			neigh_del_timer(n);
 			neigh_mark_dead(n);

== SNIP ==

Quoted 10 lines
Quoted 10 lines
net/core/neighbour.c +2 -0
+			hlist_del_rcu(&n->list);
+			hlist_add_head_rcu(&n->list, &new_nht->hash_buckets[hash]);
 		}
 	}
@@ -693,11 +666,10 @@ ___neigh_create(struct neigh_table *tbl, const void *pkey,
 		goto out_tbl_unlock;
 	}
 
Quoted 11 lines

net/core/neighbour.c ___neigh_create(struct neigh_table *tbl, const void *pkey, · line 672

Quoted 9 lines
net/core/neighbour.c +4 -5
-	for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
-					    lockdep_is_held(&tbl->lock));
-	     n1 != NULL;
-	     n1 = rcu_dereference_protected(n1->next,
-			lockdep_is_held(&tbl->lock))) {
+	hlist_for_each_entry_rcu(n1,
+				 &nht->hash_buckets[hash_val],
+				 list,
+				 lockdep_is_held(&tbl->lock)) {

Let's define hlist_for_each_entry_rcu() as neigh-specific macro.

Can you elaborate on this?
Do you want the list parameter to be eliminated?

Quoted 19 lines
Quoted 19 lines
net/core/neighbour.c +1 -4
 		if (dev == n1->dev && !memcmp(n1->primary_key, n->primary_key, key_len)) {
 			if (want_ref)
 				neigh_hold(n1);
@@ -713,10 +685,7 @@ ___neigh_create(struct neigh_table *tbl, const void *pkey,
 		list_add_tail(&n->managed_list, &n->tbl->managed_list);
 	if (want_ref)
 		neigh_hold(n);
-	rcu_assign_pointer(n->next,
-			   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->list, &nht->hash_buckets[hash_val]);
 	write_unlock_bh(&tbl->lock);
 	neigh_dbg(2, "neigh %p is created\n", n);
 	rc = n;
@@ -976,7 +945,7 @@ static void neigh_periodic_work(struct work_struct *work)
 		goto out;
 
Quoted 5 lines

net/core/neighbour.c static void neigh_periodic_work(struct work_struct *work) · line 948

Quoted 3 lines
net/core/neighbour.c +1 -1
 	for (i = 0 ; i < (1 << nht->hash_shift); i++) {
-		np = &nht->hash_buckets[i];
+		np = (struct neighbour __rcu **)&nht->hash_buckets[i].first;

No np here too,

Same as the other loop in neigh_flush_dev, we must keep np in order to avoid UAF, because n might be freed in the loop body.

#4 lore raw

Re: [PATCH net-next v2 1/2] Convert neighbour-table to use hlist

From: Gilad Naaman <[email protected]>
Date: Tue, 8 Oct 2024 07:38:55 +0000

Quoted 10 lines
Quoted 9 lines

net/core/neighbour.c static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev, · line 370

Quoted 7 lines
net/core/neighbour.c +2 -1
@@ -388,21 +366,20 @@ static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev,
 
 	for (i = 0; i < (1 << nht->hash_shift); i++) {
 		struct neighbour *n;
-		struct neighbour __rcu **np = &nht->hash_buckets[i];
+		struct neighbour __rcu **np =
+			(struct neighbour __rcu **)&nht->hash_buckets[i].first;

This will be no longer needed for doubly linked list,

Quoted 6 lines

This is not as-necessary with a doubly-linked list, but unfortunately I cannot eliminate it completely, as the n might be released in the loop body.

I can convert this function to use a struct neighour *next instead, if it is more palatable.

Yes, using hlist_for_each_entry_safe() is more preferable.

Mixing for() and while() is harder to read.

[...]

Quoted 16 lines
Quoted 4 lines
Quoted 4 lines
net/core/neighbour.c +0 -0
@@ -693,11 +666,10 @@ ___neigh_create(struct neigh_table *tbl, const void *pkey,
 		goto out_tbl_unlock;
 	}
 
Quoted 11 lines

net/core/neighbour.c ___neigh_create(struct neigh_table *tbl, const void *pkey, · line 672

Quoted 9 lines
net/core/neighbour.c +4 -5
-	for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
-					    lockdep_is_held(&tbl->lock));
-	     n1 != NULL;
-	     n1 = rcu_dereference_protected(n1->next,
-			lockdep_is_held(&tbl->lock))) {
+	hlist_for_each_entry_rcu(n1,
+				 &nht->hash_buckets[hash_val],
+				 list,
+				 lockdep_is_held(&tbl->lock)) {

Let's define hlist_for_each_entry_rcu() as neigh-specific macro.

Quoted 2 lines

Can you elaborate on this?
Do you want the list parameter to be eliminated?

I mean like

#define neigh_for_each(...)		\
	hlist_for_each_entry(...)
#define neigh_for_each_rcu(...)		\
	hlist_for_each_entry_rcu(...)

are better if there's repeated arguments.

#5 lore raw

Re: [PATCH net-next v2 1/2] Convert neighbour-table to use hlist

In reply to Gilad Naaman

From: Gilad Naaman <[email protected]>
Date: Sun, 6 Oct 2024 06:47:42 +0000

net/core/neighbour.c static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, · line 2698

Quoted 8 lines
net/core/neighbour.c +1 -3
@@ -2728,9 +2695,7 @@ static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
 	for (h = s_h; h < (1 << nht->hash_shift); h++) {
 		if (h > s_h)
 			s_idx = 0;
-		for (n = rcu_dereference(nht->hash_buckets[h]), idx = 0;
-		     n != NULL;
-		     n = rcu_dereference(n->next)) {
+		hlist_for_each_entry_rcu(n, &nht->hash_buckets[h], list) {

idx = 0 was accidentally removed.

Quoted 3 lines
net/core/neighbour.c +0 -0
 			if (idx < s_idx || !net_eq(dev_net(n->dev), net))
 				goto next;
 			if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
#6 lore raw

Re: [PATCH net-next v2 1/2] Convert neighbour-table to use hlist

In reply to Gilad Naaman
kernel test robot noticed "BUG:unable_to_handle_page_fault_for_address" on:

Hello,

kernel test robot noticed "BUG:unable_to_handle_page_fault_for_address" on:

commit: febd2a80c498e3be8afdfe826ca49966bd9aca9a ("[PATCH net-next v2 1/2] Convert neighbour-table to use hlist")
url: https://github.com/intel-lab-lkp/linux/commits/Gilad-Naaman/Convert-neighbour-table-to-use-hlist/20241006-145017
base: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git 8f602276d3902642fdc3429b548d73c745446601 patch link: https://lore.kernel.org/all/[email protected]/
patch subject: [PATCH net-next v2 1/2] Convert neighbour-table to use hlist

in testcase: trinity
version: trinity-i386-abe9de86-1_20230429
with following parameters:

	runtime: 300s
	group: group-02
	nr_groups: 5

compiler: gcc-12
test machine: qemu-system-x86_64 -enable-kvm -cpu SandyBridge -smp 2 -m 16G

(please refer to attached dmesg/kmsg for entire log/backtrace)

+--------------------------------------------------------------+------------+------------+
|                                                              | 8f602276d3 | febd2a80c4 |
+--------------------------------------------------------------+------------+------------+
| BUG:unable_to_handle_page_fault_for_address                  | 0          | 100        |
| Oops                                                         | 0          | 100        |
| RIP:___neigh_lookup_noref                                    | 0          | 100        |
+--------------------------------------------------------------+------------+------------+

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-lkp/[email protected]

[   42.953184][   T23] BUG: unable to handle page fault for address: ffffffffdeaf2215
[   42.954660][   T23] #PF: supervisor read access in kernel mode
[   42.955397][   T23] #PF: error_code(0x0000) - not-present page
[   42.956085][   T23] PGD 3441067 P4D 3441067 PUD 3443067 PMD 0
[   42.956877][   T23] Oops: Oops: 0000 [#1] PREEMPT SMP
[   42.957461][   T23] CPU: 1 UID: 0 PID: 23 Comm: kworker/1:0 Tainted: G                T  6.12.0-rc1-00350-gfebd2a80c498 #1 b68071eb3c38aea083990c8ab76066a73681badf
[   42.959169][   T23] Tainted: [T]=RANDSTRUCT
[   42.959657][   T23] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
[   42.960816][   T23] Workqueue: mld mld_ifc_work
[ 42.968090][ T23] RIP: 0010:___neigh_lookup_noref (include/net/neighbour.h:310) 
[ 42.969009][ T23] Code: 00 00 48 c7 c2 de e1 0a 83 48 c7 c7 fc 39 1f 83 e8 b0 ea 08 ff 48 85 db 0f 84 85 00 00 00 48 8d bb 28 03 00 00 e8 31 18 1a ff <4c> 3b ab 28 03 00 00 75 0f 4c 89 e6 48 89 df e8 59 f8 ff ff 84 c0
All code
========
   0:	00 00                	add    %al,(%rax)
   2:	48 c7 c2 de e1 0a 83 	mov    $0xffffffff830ae1de,%rdx
   9:	48 c7 c7 fc 39 1f 83 	mov    $0xffffffff831f39fc,%rdi
  10:	e8 b0 ea 08 ff       	call   0xffffffffff08eac5
  15:	48 85 db             	test   %rbx,%rbx
  18:	0f 84 85 00 00 00    	je     0xa3
  1e:	48 8d bb 28 03 00 00 	lea    0x328(%rbx),%rdi
  25:	e8 31 18 1a ff       	call   0xffffffffff1a185b
  2a:*	4c 3b ab 28 03 00 00 	cmp    0x328(%rbx),%r13		<-- trapping instruction
  31:	75 0f                	jne    0x42
  33:	4c 89 e6             	mov    %r12,%rsi
  36:	48 89 df             	mov    %rbx,%rdi
  39:	e8 59 f8 ff ff       	call   0xfffffffffffff897
  3e:	84 c0                	test   %al,%al

Code starting with the faulting instruction
===========================================

   0:	4c 3b ab 28 03 00 00 	cmp    0x328(%rbx),%r13
   7:	75 0f                	jne    0x18
   9:	4c 89 e6             	mov    %r12,%rsi
   c:	48 89 df             	mov    %rbx,%rdi
   f:	e8 59 f8 ff ff       	call   0xfffffffffffff86d
  14:	84 c0                	test   %al,%al
[   42.971354][   T23] RSP: 0018:ffffc900000cbb88 EFLAGS: 00210246
[   42.972125][   T23] RAX: 0000000000000000 RBX: ffffffffdeaf1eed RCX: 0000000000000000
[   42.973071][   T23] RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000000
[   42.974124][   T23] RBP: ffff88811af34b40 R08: 0000000000000000 R09: 0000000000000000
[   42.975040][   T23] R10: 0000000000000000 R11: 0000000000000000 R12: ffff88811c6fa028
[   42.975974][   T23] R13: ffff88811bc7a000 R14: 000000000000001d R15: 0000607bd0004020
[   42.976897][   T23] FS:  0000000000000000(0000) GS:ffff88842fc00000(0000) knlGS:0000000000000000
[   42.977854][   T23] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   42.978590][   T23] CR2: ffffffffdeaf2215 CR3: 000000000343f000 CR4: 00000000000406b0
[   42.979442][   T23] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[   42.980369][   T23] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[   42.981266][   T23] Call Trace:
[   42.981636][   T23]  <TASK>
[ 42.981988][ T23] ? __die_body (arch/x86/kernel/dumpstack.c:421) 
[ 42.982475][ T23] ? page_fault_oops (arch/x86/mm/fault.c:715) 
[ 42.983091][ T23] ? exc_page_fault (arch/x86/mm/fault.c:1479 arch/x86/mm/fault.c:1539) 
[ 42.983677][ T23] ? asm_exc_page_fault (arch/x86/include/asm/idtentry.h:623) 
[ 42.984326][ T23] ? ___neigh_lookup_noref (include/net/neighbour.h:310) 
[ 42.985102][ T23] ? ___neigh_lookup_noref (include/net/neighbour.h:310) 
[ 42.985789][ T23] ip6_finish_output2 (include/net/ndisc.h:368 net/ipv6/ip6_output.c:128) 
[ 42.986377][ T23] ip6_output (net/ipv6/ip6_output.c:251) 
[ 42.986913][ T23] dst_output+0x59/0x64 
[ 42.987506][ T23] mld_sendpack (net/ipv6/mcast.c:1823) 
[ 42.988064][ T23] mld_send_cr (net/ipv6/mcast.c:2121) 
[ 42.988539][ T23] mld_ifc_work (net/ipv6/mcast.c:2653) 
[ 42.989034][ T23] ? process_one_work (kernel/workqueue.c:3205) 
[ 42.989680][ T23] process_one_work (kernel/workqueue.c:3234) 
[ 42.990204][ T23] ? process_one_work (kernel/workqueue.c:3205) 
[ 42.990790][ T23] process_scheduled_works (kernel/workqueue.c:3310) 
[ 42.991361][ T23] worker_thread (include/linux/list.h:373 kernel/workqueue.c:946 kernel/workqueue.c:3392) 
[ 42.991863][ T23] ? drain_dead_softirq_workfn (kernel/workqueue.c:3337) 
[ 42.992581][ T23] kthread (kernel/kthread.c:391) 
[ 42.993075][ T23] ? kthread (kernel/kthread.c:374) 
[ 42.993578][ T23] ? list_del_init (include/linux/lockdep.h:249) 
[ 42.994107][ T23] ret_from_fork (arch/x86/kernel/process.c:153) 
[ 42.994606][ T23] ? list_del_init (include/linux/lockdep.h:249) 
[ 42.995189][ T23] ret_from_fork_asm (arch/x86/entry/entry_64.S:254) 
[   42.995794][   T23]  </TASK>
[   42.996192][   T23] Modules linked in:
[   42.996717][   T23] CR2: ffffffffdeaf2215
[   42.997192][   T23] ---[ end trace 0000000000000000 ]---
[ 42.997790][ T23] RIP: 0010:___neigh_lookup_noref (include/net/neighbour.h:310) 
[ 42.998628][ T23] Code: 00 00 48 c7 c2 de e1 0a 83 48 c7 c7 fc 39 1f 83 e8 b0 ea 08 ff 48 85 db 0f 84 85 00 00 00 48 8d bb 28 03 00 00 e8 31 18 1a ff <4c> 3b ab 28 03 00 00 75 0f 4c 89 e6 48 89 df e8 59 f8 ff ff 84 c0
All code
========
   0:	00 00                	add    %al,(%rax)
   2:	48 c7 c2 de e1 0a 83 	mov    $0xffffffff830ae1de,%rdx
   9:	48 c7 c7 fc 39 1f 83 	mov    $0xffffffff831f39fc,%rdi
  10:	e8 b0 ea 08 ff       	call   0xffffffffff08eac5
  15:	48 85 db             	test   %rbx,%rbx
  18:	0f 84 85 00 00 00    	je     0xa3
  1e:	48 8d bb 28 03 00 00 	lea    0x328(%rbx),%rdi
  25:	e8 31 18 1a ff       	call   0xffffffffff1a185b
  2a:*	4c 3b ab 28 03 00 00 	cmp    0x328(%rbx),%r13		<-- trapping instruction
  31:	75 0f                	jne    0x42
  33:	4c 89 e6             	mov    %r12,%rsi
  36:	48 89 df             	mov    %rbx,%rdi
  39:	e8 59 f8 ff ff       	call   0xfffffffffffff897
  3e:	84 c0                	test   %al,%al

Code starting with the faulting instruction
===========================================

   0:	4c 3b ab 28 03 00 00 	cmp    0x328(%rbx),%r13
   7:	75 0f                	jne    0x18
   9:	4c 89 e6             	mov    %r12,%rsi
   c:	48 89 df             	mov    %rbx,%rdi
   f:	e8 59 f8 ff ff       	call   0xfffffffffffff86d
  14:	84 c0                	test   %al,%al

The kernel config and materials to reproduce are available at:
https://download.01.org/0day-ci/archive/20241009/[email protected]

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw

PATCH v2 2/2 Create netdev->neighbour association

In reply to Gilad Naaman

Create a mapping between a netdev and its neighoburs, allowing for much cheaper flushes.

---
 .../networking/net_cachelines/net_device.rst  |   1 +
 include/linux/netdevice.h                     |   6 ++
 include/net/neighbour.h                       |  10 +-
 include/net/neighbour_tables.h                |  13 +++
 net/core/neighbour.c                          | 102 +++++++++++++-----
 5 files changed, 99 insertions(+), 33 deletions(-)
 create mode 100644 include/net/neighbour_tables.h
Documentation/networking/net_cachelines/net_device.rst +1 -0
index 22b07c814f4a..510c407d7268 100644
@@ -183,3 +183,4 @@ struct_devlink_port*                devlink_port
 struct_dpll_pin*                    dpll_pin                                                        
 struct hlist_head                   page_pools
 struct dim_irq_moder*               irq_moder
+struct hlist_head                   neighbours[3]
include/linux/netdevice.h +6 -0
index e87b5e488325..071b89c56e07 100644
@@ -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;
@@ -2009,6 +2010,9 @@ enum netdev_reg_state {
  *	@dpll_pin: Pointer to the SyncE source pin of a DPLL subsystem,
  *		   where the clock is recovered.
  *
+ *	 @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.
  */
@@ -2399,6 +2403,8 @@ struct net_device {
 	/** @irq_moder: dim parameters used if IS_ENABLED(CONFIG_DIMLIB). */
 	struct dim_irq_moder	*irq_moder;
 
+	struct hlist_head neighbours[NEIGH_NR_TABLES];
+
 	u8			priv[] ____cacheline_aligned
 				       __counted_by(priv_len);
 } ____cacheline_aligned;
include/net/neighbour.h +2 -8
index 5dde118323e3..dee5c6337fb5 100644
@@ -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	list;
+	struct hlist_node	dev_list;
 	struct neigh_table	*tbl;
 	struct neigh_parms	*parms;
 	unsigned long		confirmed;
@@ -236,14 +238,6 @@ struct neigh_table {
 	struct pneigh_entry	**phash_buckets;
 };
 
-enum {
-	NEIGH_ARP_TABLE = 0,
-	NEIGH_ND_TABLE = 1,
-	NEIGH_DN_TABLE = 2,
-	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 +13 -0
new file mode 100644
index 000000000000..ad98b49d58db
@@ -0,0 +1,13 @@
+/* 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_DN_TABLE = 2,
+	NEIGH_NR_TABLES,
+	NEIGH_LINK_TABLE = NEIGH_NR_TABLES /* Pseudo table for neigh_xmit */
+};
+
+#endif
net/core/neighbour.c +77 -25

150 lines not on this page · raw message

Signature · 1 line
2.46.0
#8 lore raw

Re: [PATCH net-next v2 2/2] Create netdev->neighbour association

kernel test robot noticed "WARNING:suspicious_RCU_usage" on:

Hello,

kernel test robot noticed "WARNING:suspicious_RCU_usage" on:

commit: a64ba5de5ee501e1027b1b3bc0cf3a80047cfa05 ("[PATCH net-next v2 2/2] Create netdev->neighbour association")
url: https://github.com/intel-lab-lkp/linux/commits/Gilad-Naaman/Convert-neighbour-table-to-use-hlist/20241006-145017
base: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git 8f602276d3902642fdc3429b548d73c745446601 patch link: https://lore.kernel.org/all/[email protected]/
patch subject: [PATCH net-next v2 2/2] Create netdev->neighbour association

in testcase: rcutorture
version:
with following parameters:

	runtime: 300s
	test: default
	torture_type: trivial

compiler: gcc-12
test machine: qemu-system-x86_64 -enable-kvm -cpu SandyBridge -smp 2 -m 16G

(please refer to attached dmesg/kmsg for entire log/backtrace)

+---------------------------------------------------------------+------------+------------+
|                                                               | febd2a80c4 | a64ba5de5e |
+---------------------------------------------------------------+------------+------------+
| WARNING:suspicious_RCU_usage                                  | 0          | 6          |
| net/core/neighbour.c:#suspicious_rcu_dereference_check()usage | 0          | 6          |
+---------------------------------------------------------------+------------+------------+

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-lkp/[email protected]

[    7.752029][    T1] WARNING: suspicious RCU usage
[    7.752608][    T1] 6.12.0-rc1-00351-ga64ba5de5ee5 #1 Not tainted
[    7.753375][    T1] -----------------------------
[    7.753944][    T1] net/core/neighbour.c:427 suspicious rcu_dereference_check() usage!
[    7.754874][    T1]
[    7.754874][    T1] other info that might help us debug this:
[    7.754874][    T1]
[    7.756040][    T1]
[    7.756040][    T1] rcu_scheduler_active = 2, debug_locks = 1
[    7.756973][    T1] 3 locks held by swapper/1:
[ 7.757526][ T1] #0: 4271c954 (rtnl_mutex){+.+.}-{3:3}, at: rtnl_lock (net/core/rtnetlink.c:80) 
[ 7.758448][ T1] #1: 4272abf8 ((inetaddr_chain).rwsem){++++}-{3:3}, at: blocking_notifier_call_chain (kernel/notifier.c:388 kernel/notifier.c:376) 
[ 7.759702][ T1] #2: 4272a180 (&tbl->lock){+...}-{2:2}, at: __neigh_ifdown+0x1d/0x108 
[    7.760778][    T1]
[    7.760778][    T1] stack backtrace:
[    7.761508][    T1] CPU: 0 UID: 0 PID: 1 Comm: swapper Not tainted 6.12.0-rc1-00351-ga64ba5de5ee5 #1
[    7.762586][    T1] Call Trace:
[ 7.762968][ T1] ? dump_stack_lvl (lib/dump_stack.c:123 (discriminator 1)) 
[ 7.763512][ T1] ? dump_stack (lib/dump_stack.c:130) 
[ 7.764002][ T1] ? lockdep_rcu_suspicious (kernel/locking/lockdep.c:6822) 
[ 7.764645][ T1] ? neigh_flush_dev (net/core/neighbour.c:427 (discriminator 9)) 
[ 7.765201][ T1] ? _raw_write_lock_bh (kernel/locking/spinlock.c:335) 
[ 7.765587][ T1] ? __neigh_ifdown+0x29/0x108 
[ 7.765993][ T1] ? neigh_ifdown (net/core/neighbour.c:488) 
[ 7.766325][ T1] ? arp_ifdown (net/ipv4/arp.c:1363) 
[ 7.766647][ T1] ? fib_inetaddr_event (net/ipv4/fib_frontend.c:1430 net/ipv4/fib_frontend.c:1454) 
[ 7.767026][ T1] ? notifier_call_chain (kernel/notifier.c:95) 
[ 7.767414][ T1] ? blocking_notifier_call_chain (kernel/notifier.c:388 kernel/notifier.c:376) 
[ 7.767858][ T1] ? __inet_del_ifa (net/ipv4/devinet.c:447) 
[ 7.768226][ T1] ? devinet_ioctl (net/ipv4/devinet.c:1253) 
[ 7.768589][ T1] ? ic_setup_if (net/ipv4/ipconfig.c:382) 
[ 7.768934][ T1] ? ip_auto_config (net/ipv4/ipconfig.c:1652) 
[ 7.769343][ T1] ? root_nfs_parse_addr (net/ipv4/ipconfig.c:1477) 
[ 7.769729][ T1] ? do_one_initcall (init/main.c:1269) 
[ 7.770099][ T1] ? do_initcalls (init/main.c:1330 init/main.c:1347) 
[ 7.770446][ T1] ? rest_init (init/main.c:1461) 
[ 7.770779][ T1] ? kernel_init_freeable (init/main.c:1584) 
[ 7.771175][ T1] ? kernel_init (init/main.c:1471) 
[ 7.771514][ T1] ? ret_from_fork (arch/x86/kernel/process.c:153) 
[ 7.771860][ T1] ? rest_init (init/main.c:1461) 
[ 7.772192][ T1] ? ret_from_fork_asm (arch/x86/entry/entry_32.S:737) 
[ 7.772563][ T1] ? entry_INT80_32 (arch/x86/entry/entry_32.S:941) 

The kernel config and materials to reproduce are available at:
https://download.01.org/0day-ci/archive/20241009/[email protected]

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki