neighbour: Namespacify arp_tbl and nd_tbl.
Subsystems (16)
v6 cover neighbour: Namespacify arp_tbl and nd_tbl.
The neighbour subsystem is almost ready to drop RTNL.
However, the control paths are serialised by the global per-table lock.
This series converts arp_tbl and nd_tbl to per-netns table.
With the series, /proc/sys/net/ipv{4,6}/neigh/default/ can be configured per netns, which was only configurable in init_net.
To avoid potential regression, all the settings are inherited from init_net by default, and this behaviour is controlled by a new sysctl knob, net.core.neigh_inherit_init_net:
# sysctl net.core.neigh_inherit_init_net net.core.neigh_inherit_init_net = 1 # sysctl net.ipv4.neigh.default.gc_thresh1 net.ipv4.neigh.default.gc_thresh1 = 128 # sysctl net.ipv4.neigh.default.gc_thresh1=129 net.ipv4.neigh.default.gc_thresh1 = 129 # unshare -n sysctl net.ipv4.neigh.default.gc_thresh1 net.ipv4.neigh.default.gc_thresh1 = 129
If it is turned off, all settings are reset in the new netns:
# sysctl net.core.neigh_inherit_init_net=0 net.core.neigh_inherit_init_net = 0 # unshare -n sysctl net.ipv4.neigh.default.gc_thresh1 net.ipv4.neigh.default.gc_thresh1 = 128
Series overview:
Patch 1 deflakes test_neigh.sh.
Patch 2 ~ 3 are misc cleanup.
Patch 4 ~ 7 store arp_tbl/nd_tbl to net->neigh_tables[] and remove the global neigh_tables[].
Patch 8 ~ 9 replace the direct access to arp_tbl/nd_tbl to net->neigh_tables[] using new helpers.
Patch 10 ~ 12 finally replace the global table with per-netns table.
Patch 13 ~ 14 clean up unnecessary net_eq().
Patch 15 updates test_neigh.sh.
Note that some buggy drivers access nd_tbl without checking disable_ipv6_mod, so nd_tbl's extern definition is still left.
After this series, I will follow up on two suggestions by Ido:
https://lore.kernel.org/netdev/20260902143023.GA3966681@shredder/
Changes:
v6:
* Patch 12
* Define sysctl_neigh_inherit_init_net in neighbour.c for CONFIG_SYSCTL=n
v5: https://lore.kernel.org/netdev/[email protected]/
* Rebased on the latest net-next
v4: https://lore.kernel.org/netdev/[email protected]/
* Patch 12
* Add sysctl knob, net.core.neigh_inherit_init_net
* Inherit all neigh parms by default
v3: https://lore.kernel.org/netdev/[email protected]/
* Add Patch 1 & 11
* Patch 12
* Remove timer_shutdown_sync() in neigh_flush_one() and
rely on tbl->entries (Patch 11) to free it in neigh_table_free().
* Patch 15
* Remove stale comments
v2: https://lore.kernel.org/netdev/[email protected]/
* Add Patch 11 & 13
* Patch 7
* Add note about mlx5e_tc_update_neigh_used_value()
* Patch 9
* Add __maybe_unused to net in neigh_table_clear()
* Patch 10
* panic() when register_pernet_subsys(&arp_net_ops) fails
* Add timer_shutdown_sync() in neigh_flush_one()
* Patch 11
* Split from the next patch
* Remove net comparison in pneigh_dump_table()
* Remove net arg of pneigh_create(), pneigh_delete(), and pneigh_lookup()
v1: https://lore.kernel.org/netdev/[email protected]/
Kuniyuki Iwashima (15):
selftest: net: Deflake Periodic GC test in test_neigh.sh.
neighbour: Remove __neigh_for_each_release().
neighbour: Remove lock dance for neigh_update_{gc,managed}_list().
neighbour: Remove unnecessary EXPORT_SYMBOL().
neighbour: Remove __rcu from neigh_tables[].
neighbour: Store arp_tbl and nd_tbl in net->neigh_tables[].
neighbour: Remove neigh_tables[].
ipv4: Replace &arp_tbl with arp_table(net).
ipv6: Replace &nd_tbl with nd_table(net).
neighbour: Clean up neigh_table_init() and neigh_table_clear().
neighbour: Convert neigh_table.entries to refcount_t.
neighbour: Namespacify neigh_tables.
neighbour: Don't store net in struct pneigh_entry.
neighbour: Remove unnecessary net_eq().
selftest: net: Specify netns for ip ntable in test_neigh.sh.
Documentation/admin-guide/sysctl/net.rst | 14 + drivers/infiniband/ulp/ipoib/ipoib_main.c | 27 +- .../marvell/prestera/prestera_router.c | 10 +- .../mellanox/mlx5/core/en/rep/neigh.c | 29 +- .../mellanox/mlx5/core/en/tc_tun_encap.c | 23 +- .../mellanox/mlx5/core/en_accel/ipsec.c | 6 +- .../ethernet/mellanox/mlxsw/spectrum_router.c | 31 +- .../ethernet/mellanox/mlxsw/spectrum_span.c | 10 +- .../netronome/nfp/flower/tunnel_conf.c | 14 +- drivers/net/ethernet/rocker/rocker_main.c | 2 +- drivers/net/ethernet/rocker/rocker_ofdpa.c | 2 +- drivers/net/ethernet/sfc/tc_counters.c | 8 +- drivers/net/ethernet/sfc/tc_encap_actions.c | 4 +- drivers/net/vrf.c | 2 +- drivers/net/vxlan/vxlan_core.c | 16 +- include/net/arp.h | 10 +- include/net/ndisc.h | 20 +- include/net/neighbour.h | 22 +- include/net/net_namespace.h | 4 + include/net/route.h | 7 +- net/bridge/br_arp_nd_proxy.c | 4 +- net/core/neighbour.c | 402 +++++++++--------- net/core/sysctl_net_core.c | 10 + net/ieee802154/6lowpan/tx.c | 3 +- net/ipv4/arp.c | 131 ++++-- net/ipv4/devinet.c | 18 +- net/ipv4/fib_semantics.c | 7 +- net/ipv4/route.c | 2 +- net/ipv6/addrconf.c | 17 +- net/ipv6/ip6_output.c | 4 +- net/ipv6/ndisc.c | 146 ++++--- net/ipv6/route.c | 18 +- tools/testing/selftests/net/test_neigh.sh | 66 +-- 33 files changed, 620 insertions(+), 469 deletions(-)
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
PATCH v6 1/15 selftest: net: Deflake Periodic GC test in test_neigh.sh.
test_neigh.sh has a test case to check if GC is running periodically.
It applies a new value to BASE_REACHABLE_TIME, waits for half of the old value (+2s) to ensure the config is reflected, and waits for BASE_REACHABLE_TIME to see if the next GC is triggered.
orig_base_reachable=$(ip -j ntable show name "$tbl_name" | jq '.[] | \
select(has("thresh1")) | .["base_reachable"]')
run_cmd "ip ntable change name $tbl_name thresh1 10 base_reachable 10000"
run_cmd "sleep $(((orig_base_reachable / 1000) / 2 + 2))"This is because neigh_periodic_work() schedules the next GC with (BASE_REACHABLE_TIME / 2) seconds:
queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);However, the very first run is scheduled with a longer period in neigh_table_init(), which will be up to 45s:
neigh_set_reach_time(&tbl->parms);
queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
tbl->parms.reachable_time);The initial value is randomly chosen, so if it is larger than 27s (larger than two "sleep"s), the test fails.
The test fails more frequently when neigh_table is namespacified by the later patch.
In addition, the long "sleep" makes the test more flaky on kernel with CONFIG_NO_HZ_IDLE=y, which is enabled by defconfig.
The tbl->gc_work is initialised with INIT_DEFERRABLE_WORK() and queued to system_power_efficient_wq.
Even when the next GC is supposed to be triggered 5s later, "sleep" makes the CPU idle, delaying GC execution until often 20s later, which is too late.
Let's take the initial tbl->parms.reachable_time into account and poll periodic_gc_runs every 0.1s up to the proper timeout, not to make CPU idle.
---
It seems NIPA has CONFIG_HZ_PERIODIC=y and has no problem. We may want to add it in net/config.
https://netdev-ctrl.bots.linux.dev/logs/vmksft/net-dbg/results/769301/config
--- tools/testing/selftests/net/test_neigh.sh | 27 ++++++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-)
tools/testing/selftests/net/test_neigh.sh +21 -6
@@ -63,6 +63,15 @@ exit_cleanup_all() exit "${EXIT_STATUS}" } +get_periodic_gc_runs() +{ + local ns=$1 + local tbl_name=$2 + + ip -n "$ns" -j -s ntable show name "$tbl_name" | \ + jq '.[] | select(has("periodic_gc_runs")) | .["periodic_gc_runs"]' +} + ################################################################################ # Tests
@@ -282,24 +291,30 @@ extern_valid_common() local periodic_gc_runs_t0 local periodic_gc_runs_t1 local orig_base_reachable + local base_reachable=10000 local orig_gc_stale + local timeout run_cmd "ip -n $ns1 neigh flush dev veth0" orig_thresh1=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh1")) | .["thresh1"]') orig_base_reachable=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh1")) | .["base_reachable"]') - run_cmd "ip ntable change name $tbl_name thresh1 10 base_reachable 10000" + run_cmd "ip ntable change name $tbl_name thresh1 10 base_reachable $base_reachable" orig_gc_stale=$(ip -n "$ns1" -j ntable show name "$tbl_name" dev veth0 | jq '.[]["gc_stale"]') run_cmd "ip -n $ns1 ntable change name $tbl_name dev veth0 gc_stale 1000" run_cmd "ip -n $ns1 neigh add $ip_addr lladdr $mac nud stale dev veth0 extern_valid" run_cmd "ip -n $ns1 neigh add ${subnet}3 lladdr $mac nud stale dev veth0" - # Wait orig_base_reachable/2 for the new interval to take effect. - run_cmd "sleep $(((orig_base_reachable / 1000) / 2 + 2))" + # Wait up to neigh_set_reach_time(BASE_REACHABLE_TIME) (~45s) + 5s + # for the new interval to take effect, see neigh_table_init(). + timeout=$(((orig_base_reachable / 1000) * 3 / 2 + 5)) + slowwait_for_counter "$timeout" 1 get_periodic_gc_runs "$ns1" "$tbl_name" >/dev/null for i in {1..20}; do run_cmd "ip -n $ns1 neigh add ${subnet}$((i + 4)) nud none dev veth0" done - periodic_gc_runs_t0=$(ip -j -s ntable show name "$tbl_name" | jq '.[] | select(has("periodic_gc_runs")) | .["periodic_gc_runs"]') - run_cmd "sleep 10" - periodic_gc_runs_t1=$(ip -j -s ntable show name "$tbl_name" | jq '.[] | select(has("periodic_gc_runs")) | .["periodic_gc_runs"]') + periodic_gc_runs_t0=$(get_periodic_gc_runs "$ns1" "$tbl_name") + # Wait up to BASE_REACHABLE_TIME / 2 + 5s, see neigh_periodic_work(). + timeout=$(((base_reachable / 1000) / 2 + 5)) + slowwait_for_counter "$timeout" 1 get_periodic_gc_runs "$ns1" "$tbl_name" >/dev/null + periodic_gc_runs_t1=$(get_periodic_gc_runs "$ns1" "$tbl_name") [[ $periodic_gc_runs_t1 -ne $periodic_gc_runs_t0 ]] check_err $? "Periodic garbage collection did not run" run_cmd "ip -n $ns1 neigh get $ip_addr dev veth0 | grep \"extern_valid\""
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
[email protected]
AI reviewRe: [PATCH v6 net-next 01/15] selftest: net: Deflake Periodic GC test in test_neigh.sh.
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Critical: 0 · High: 0 · Medium: 1 · Low: 4
- [Medium] The new completion barrier is the wrong signal:
neigh_periodic_work() bumps NEIGH_CACHE_STAT_INC(tbl,…
- [Low] Undisclosed netns switch of the periodic_gc_runs read: the new get_periodic_gc_runs() helper queries the counter inside $ns1 (`ip -n…
- [Low] The patch fixes a currently reproducible selftest flake (extern_valid_common()'s periodic-GC wait in…
- [Low] Comment/code drift: the section header comment in extern_valid_common() still describes the removed sleep-based…
- [Low] The first slowwait_for_counter call (line 309) is the setup barrier that guarantees neigh_periodic_work() completed once and re-…
--
selftest: net: Deflake Periodic GC test in test_neigh.sh.
This replaces the two fixed sleeps in the periodic garbage collection test case of test_neigh.sh with slowwait_for_counter() polling of the periodic_gc_runs table statistic. A new helper, get_periodic_gc_runs(), reads the counter, and the timeouts now account for the randomized initial tbl->parms.reachable_time set up by neigh_table_init().
No Link: tags are present in the commit header.
[Severity: Low]
Would a Fixes: tag be appropriate here? The commit message describes a failure that is reproducible on current kernels ("The initial value is randomly chosen, so if it is larger than 27s (larger than two "sleep"s), the test fails"), and git blame points at the two commits that added the sleeps being removed:
171f2ee31a42f ("selftests: net: add test_neigh.sh") added the "sleep 10" wait
f8fded7536a93 added the "sleep $(((orig_base_reachable / 1000) / 2 + 2))" wait
Without a tag the deflake will not be routed to trees that still run the flaky version of the test.
Quoted 7 lines
tools/testing/selftests/net/test_neigh.sh +0
-0@@ -63,6 +63,15 @@ exit_cleanup_all() exit "${EXIT_STATUS}" }
tools/testing/selftests/net/test_neigh.sh exit_cleanup_all() · line 74
Quoted 10 lines
tools/testing/selftests/net/test_neigh.sh +9
-0+get_periodic_gc_runs() +{ + local ns=$1 + local tbl_name=$2 + + ip -n "$ns" -j -s ntable show name "$tbl_name" | \ + jq '.[] | select(has("periodic_gc_runs")) | .["periodic_gc_runs"]' +} +
[Severity: Low]
The old code read periodic_gc_runs in the initial namespace:
periodic_gc_runs_t0=$(ip -j -s ntable show name "$tbl_name" | jq ...)
while get_periodic_gc_runs() is called with "$ns1", so the counter is now read inside ns1. Was the namespace change intentional? It is not mentioned in the commit message.
At this commit it makes no difference, since neightbl_dump_info() in net/core/neighbour.c emits neightbl_fill_info() (including NDTA_STATS) for every registered table regardless of the requesting netns, and only the per-device parms rows are filtered by net_eq().
Inside this series it does matter for a while, though. After "neighbour:
Namespacify neigh_tables." the counter read in ns1 belongs to ns1's table (default base_reachable 30000, so a 15s GC interval), while base_reachable=10000 is still written to init_net's table below, so the second wait's 10s budget is shorter than the interval ns1 actually uses. The mismatch only disappears with the later "selftest: net: Specify netns for ip ntable in test_neigh.sh." patch, which moves the ntable reads and writes into ns1. Would it be simpler to switch the ntable read/write and the counter read in the same patch?
Quoted 16 lines
tools/testing/selftests/net/test_neigh.sh +3
-1################################################################################ # Tests@@ -282,24 +291,30 @@ extern_valid_common() local periodic_gc_runs_t0 local periodic_gc_runs_t1 local orig_base_reachable + local base_reachable=10000 local orig_gc_stale + local timeout run_cmd "ip -n $ns1 neigh flush dev veth0" orig_thresh1=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh1")) | .["thresh1"]') orig_base_reachable=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh1")) | .["base_reachable"]') - run_cmd "ip ntable change name $tbl_name thresh1 10 base_reachable 10000" + run_cmd "ip ntable change name $tbl_name thresh1 10 base_reachable $base_reachable"
tools/testing/selftests/net/test_neigh.sh extern_valid_common() · line 309
Quoted 10 lines
tools/testing/selftests/net/test_neigh.sh +4
-2orig_gc_stale=$(ip -n "$ns1" -j ntable show name "$tbl_name" dev veth0 | jq '.[]["gc_stale"]') run_cmd "ip -n $ns1 ntable change name $tbl_name dev veth0 gc_stale 1000" run_cmd "ip -n $ns1 neigh add $ip_addr lladdr $mac nud stale dev veth0 extern_valid" run_cmd "ip -n $ns1 neigh add ${subnet}3 lladdr $mac nud stale dev veth0" - # Wait orig_base_reachable/2 for the new interval to take effect. - run_cmd "sleep $(((orig_base_reachable / 1000) / 2 + 2))" + # Wait up to neigh_set_reach_time(BASE_REACHABLE_TIME) (~45s) + 5s + # for the new interval to take effect, see neigh_table_init(). + timeout=$(((orig_base_reachable / 1000) * 3 / 2 + 5)) + slowwait_for_counter "$timeout" 1 get_periodic_gc_runs "$ns1" "$tbl_name" >/dev/null
[Severity: Low]
Should the exit status of this first wait be checked? Only stdout is redirected, the status is never fed to check_err, and the test body runs with set +e (set -e is enabled and cleared inside setup()).
slowwait_for_counter() in tools/testing/selftests/net/lib.sh does return non-zero on timeout:
slowwait_for_counter()
{
local timeout=$1; shift
local delta=$1; shift
local base=$("$@")
slowwait "$timeout" until_counter_is ">= $((base + delta))" "$@"
}If this wait times out (up to ~50s with the default base_reachable=30000), the script still adds the 20 entries, samples t0 and then waits only base_reachable/1000/2 + 5 = 10s, a bound that assumes the new 5s interval is already armed by the requeue at the end of neigh_periodic_work():
queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);The resulting failure is then reported as "Periodic garbage collection did not run" with no hint that the setup wait was the thing that timed out.
There is also the inverse case: this wait can be satisfied by the initial run armed in neigh_table_init() with the randomized reachable_time, so the "new interval took effect" precondition is not really established.
tools/testing/selftests/net/test_neigh.sh extern_valid_common() · line 317
Quoted 11 lines
tools/testing/selftests/net/test_neigh.sh +5
-3for i in {1..20}; do run_cmd "ip -n $ns1 neigh add ${subnet}$((i + 4)) nud none dev veth0" done - periodic_gc_runs_t0=$(ip -j -s ntable show name "$tbl_name" | jq '.[] | select(has("periodic_gc_runs")) | .["periodic_gc_runs"]') - run_cmd "sleep 10" - periodic_gc_runs_t1=$(ip -j -s ntable show name "$tbl_name" | jq '.[] | select(has("periodic_gc_runs")) | .["periodic_gc_runs"]') + periodic_gc_runs_t0=$(get_periodic_gc_runs "$ns1" "$tbl_name") + # Wait up to BASE_REACHABLE_TIME / 2 + 5s, see neigh_periodic_work(). + timeout=$(((base_reachable / 1000) / 2 + 5)) + slowwait_for_counter "$timeout" 1 get_periodic_gc_runs "$ns1" "$tbl_name" >/dev/null + periodic_gc_runs_t1=$(get_periodic_gc_runs "$ns1" "$tbl_name")
[Severity: Medium]
Is the periodic_gc_runs increment a strong enough barrier for the checks that follow? neigh_periodic_work() bumps the counter as its first statement, before the table lock is taken and before anything is reaped:
net/core/neighbour.c:neigh_periodic_work() {
NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
spin_lock_bh(&tbl->lock);
...
}and the bucket walk drops the lock and reschedules after every bucket:
spin_unlock_bh(&tbl->lock); cond_resched(); spin_lock_bh(&tbl->lock);
slowwait_for_counter() returns as soon as the counter reaches base + 1, so the pass may still be in flight when the script proceeds to:
run_cmd "ip -n $ns1 neigh get ${subnet}3 dev veth0"
check_fail $? "Entry without \"extern_valid\" flag survived periodic garbage collection"
Can this fail spuriously if the worker is preempted mid-scan, for instance on a single-vCPU or debug kernel, given that only a couple of ip/jq invocations separate the wait from the check? The removed "sleep 10" implicitly covered the whole pass.
The positive check just above it looks weakened in the same way: the extern_valid entry can be reported as surviving simply because GC has not walked its bucket yet.
Would polling the observable effect (the ${subnet}3 entry being gone), or waiting for the counter to advance by 2, be more robust?
Related to the second timeout: base_reachable/1000/2 + 5 is measured from the moment the increment is observed, but the next run is only armed at the end of the pass, which eats into the 5s of slack.
[Severity: Low]
Does the block comment above this test case still describe what the code does? It reads:
# Check that an "extern_valid" entry survives a periodic garbage
# collection. Add an "extern_valid" entry, add more than "thresh1" # regular entries, wait "base_reachable" (longer than "gc_stale") # seconds and check that the "extern_valid" entry was not deleted.
The code no longer waits base_reachable seconds; it polls periodic_gc_runs and returns on the first additional GC run, bounded by base_reachable/1000/2 + 5. The comment also survives unchanged to the end of the series. Could it be updated along with the wait, including the "longer than gc_stale" rationale?
Quoted 3 lines
tools/testing/selftests/net/test_neigh.sh +0
-0[[ $periodic_gc_runs_t1 -ne $periodic_gc_runs_t0 ]] check_err $? "Periodic garbage collection did not run" run_cmd "ip -n $ns1 neigh get $ip_addr dev veth0 | grep \"extern_valid\""
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
PATCH v6 2/15 neighbour: Remove __neigh_for_each_release().
In reply to Kuniyuki IwashimaSince commit 6deb53595092 ("net: remove unused ATM protocols and legacy ATM device drivers"), there is no in-kernel user of __neigh_for_each_release().
Let's remove it.
--- include/net/neighbour.h | 2 -- net/core/neighbour.c | 31 ------------------------------- 2 files changed, 33 deletions(-)
include/net/neighbour.h +0 -2
@@ -405,8 +405,6 @@ static inline struct net *pneigh_net(const struct pneigh_entry *pneigh)void neigh_app_ns (struct neighbour *n);void neigh_for_each (struct neigh_table *tbl,void (*cb)(struct neighbour *,void *),void *cookie); -void __neigh_for_each_release (struct neigh_table *tbl, -int (*cb)(struct neighbour *));int neigh_xmit (int fam,struct net_device *,const void *,struct sk_buff *);struct neigh_seq_state {
net/core/neighbour.c +0 -31
@@ -3161,37 +3161,6 @@ void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void }EXPORT_SYMBOL (neigh_for_each); -/* The tbl->lock must be held as a writer and BH disabled. */ -void __neigh_for_each_release (struct neigh_table *tbl, -int (*cb)(struct neighbour *)) -{ -struct neigh_hash_table *nht; -int chain; - - nht =rcu_dereference_protected (tbl->nht, -lockdep_is_held (&tbl->lock)); -for (chain =0 ; chain < (1 << nht->hash_shift); chain++) { -struct hlist_node *tmp; -struct neighbour *n; - -neigh_for_each_in_bucket_safe (n, tmp, &nht->hash_heads[chain]) { -int release; - -write_lock (&n->lock); - release =cb (n); -if (release) { -hlist_del_rcu (&n->hash); -hlist_del_rcu (&n->dev_list); -neigh_mark_dead (n); - } -write_unlock (&n->lock); -if (release) -neigh_cleanup_and_release (n); - } - } -} -EXPORT_SYMBOL (__neigh_for_each_release); -int neigh_xmit (int index,struct net_device *dev,const void *addr,struct sk_buff *skb) {
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
PATCH v6 3/15 neighbour: Remove lock dance for neigh_update_{gc,managed}_list().
In reply to Kuniyuki Iwashima__neigh_update() could call neigh_update_gc_list() and neigh_update_managed_list().
Both of them acquire neigh->tbl->lock and neigh->lock, check neigh->dead, perform link operations, and release the locks.
Let's remove the lock dance.
Note that neigh->dead is always marked under neigh->tbl->lock.
--- net/core/neighbour.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-)
net/core/neighbour.c +19 -20
@@ -135,6 +135,8 @@ EXPORT_SYMBOL(neigh_rand_reach_time);static void neigh_mark_dead (struct neighbour *n) { +lockdep_assert_held (&n->tbl->lock); + n->dead =1 ;if (!list_empty (&n->gc_list)) {list_del_init (&n->gc_list);
@@ -148,11 +150,6 @@ static void neigh_update_gc_list(struct neighbour *n) {bool on_gc_list, exempt_from_gc; -spin_lock_bh (&n->tbl->lock); -write_lock (&n->lock); -if (n->dead) - goto out; -/* remove from the gc list if new state is permanent or if neighbor is * externally learned / validated; otherwise entry should be on the gc * list
@@ -169,20 +166,12 @@ static void neigh_update_gc_list(struct neighbour *n)list_add_tail (&n->gc_list, &n->tbl->gc_list);atomic_inc (&n->tbl->gc_entries); } -out: -write_unlock (&n->lock); -spin_unlock_bh (&n->tbl->lock); }static void neigh_update_managed_list (struct neighbour *n) {bool on_managed_list, add_to_managed; -spin_lock_bh (&n->tbl->lock); -write_lock (&n->lock); -if (n->dead) - goto out; - add_to_managed = n->flags &NTF_MANAGED ; on_managed_list = !list_empty (&n->managed_list);
@@ -190,9 +179,6 @@ static void neigh_update_managed_list(struct neighbour *n)list_del_init (&n->managed_list); else if (add_to_managed && !on_managed_list) list_add_tail(&n->managed_list, &n->tbl->managed_list); -out: -write_unlock (&n->lock); -spin_unlock_bh (&n->tbl->lock); } static void neigh_update_flags(struct neighbour *neigh, u32 flags, int *notify,
@@ -1523,10 +1509,23 @@ static int __neigh_update(struct neighbour *neigh, const u8 *lladdr,write_unlock_bh (&neigh->lock); -if (((new ^ old) &NUD_PERMANENT ) || gc_update) -neigh_update_gc_list (neigh); -if (managed_update) -neigh_update_managed_list (neigh); + gc_update |= !!((new ^ old) &NUD_PERMANENT ); +if (gc_update || managed_update) { +spin_lock_bh (&neigh->tbl->lock); + +if (!neigh->dead) { +write_lock (&neigh->lock); + +if (gc_update) +neigh_update_gc_list (neigh); +if (managed_update) +neigh_update_managed_list (neigh); + +write_unlock (&neigh->lock); + } + +spin_unlock_bh (&neigh->tbl->lock); + }if (notify)call_netevent_notifiers (NETEVENT_NEIGH_UPDATE , neigh);
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
PATCH v6 4/15 neighbour: Remove unnecessary EXPORT_SYMBOL().
In reply to Kuniyuki IwashimaCommit 1bf20cc62a54 ("neighbour: remove obsolete EXPORT_SYMBOL()") unexported some functions in neighbour.c because IPv6 is no longer built as module.
Since it unexported neigh_table_init(), no module can register neigh_table dynamically.
Let's unexport a few more functions.
--- net/core/neighbour.c | 17 ----------------- 1 file changed, 17 deletions(-)
net/core/neighbour.c +0 -17
@@ -131,7 +131,6 @@ unsigned long neigh_rand_reach_time(unsigned long base) {return base ?get_random_u32_below (base) + (base >>1 ) :0 ; } -EXPORT_SYMBOL (neigh_rand_reach_time);static void neigh_mark_dead (struct neighbour *n) {
@@ -1539,7 +1538,6 @@ int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, {return __neigh_update (neigh, lladdr, new, flags, nlmsg_pid,NULL ); } -EXPORT_SYMBOL (neigh_update);/* Update the neigh to listen temporarily for probe responses, even if it is * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
@@ -1557,7 +1555,6 @@ void __neigh_set_probe_once(struct neighbour *neigh) jiffies +max (NEIGH_VAR (neigh->parms,RETRANS_TIME ), HZ/100)); } -EXPORT_SYMBOL (__neigh_set_probe_once); struct neighbour *neigh_event_ns(struct neigh_table *tbl, u8 *lladdr, void *saddr,
@@ -1570,7 +1567,6 @@ struct neighbour *neigh_event_ns(struct neigh_table *tbl, NEIGH_UPDATE_F_OVERRIDE, 0);return neigh; } -EXPORT_SYMBOL (neigh_event_ns);/* called with read_lock_bh(&n->lock); */ static void neigh_hh_init (struct neighbour *n)
@@ -1623,7 +1619,6 @@ int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)kfree_skb_reason (skb,SKB_DROP_REASON_NEIGH_HH_FILLFAIL ); goto out; } -EXPORT_SYMBOL (neigh_resolve_output);/* As fast as possible without hh cache */
@@ -1740,7 +1735,6 @@ void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,mod_timer (&tbl->proxy_timer, sched_next);spin_unlock (&tbl->proxy_queue.lock); } -EXPORT_SYMBOL (pneigh_enqueue);static inline struct neigh_parms *lookup_neigh_parms (struct neigh_table *tbl,struct net *net,int ifindex)
@@ -1788,7 +1782,6 @@ struct neigh_parms *neigh_parms_alloc(struct net_device *dev, }return p; } -EXPORT_SYMBOL (neigh_parms_alloc);static void neigh_rcu_free_parms (struct rcu_head *head) {
@@ -1811,7 +1804,6 @@ void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)netdev_put (parms->dev, &parms->dev_tracker);call_rcu (&parms->rcu_head, neigh_rcu_free_parms); } -EXPORT_SYMBOL (neigh_parms_release);static struct lock_class_key neigh_table_proxy_queue_class;
@@ -3398,7 +3390,6 @@ void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tblreturn *pos ?neigh_get_idx_any (seq, pos) :SEQ_START_TOKEN ; } -EXPORT_SYMBOL (neigh_seq_start);void *neigh_seq_next (struct seq_file *seq,void *v,loff_t *pos) {
@@ -3425,7 +3416,6 @@ void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos) ++(*pos);return rc; } -EXPORT_SYMBOL (neigh_seq_next); void neigh_seq_stop(struct seq_file *seq, void *v) __releases(tbl->lock)
@@ -3437,7 +3427,6 @@ void neigh_seq_stop(struct seq_file *seq, void *v)spin_unlock_bh (&tbl->lock);rcu_read_unlock (); } -EXPORT_SYMBOL (neigh_seq_stop);/* statistics via seq_file */
@@ -3561,7 +3550,6 @@ void neigh_app_ns(struct neighbour *n) {neigh_notify (n,RTM_GETNEIGH ,NLM_F_REQUEST ,0 ); } -EXPORT_SYMBOL (neigh_app_ns);#ifdef CONFIG_SYSCTL static int unres_qlen_max =INT_MAX /SKB_TRUESIZE (ETH_FRAME_LEN );
@@ -3657,7 +3645,6 @@ int neigh_proc_dointvec(const struct ctl_table *ctl, int write, void *buffer,neigh_proc_update (ctl, write);return ret; } -EXPORT_SYMBOL (neigh_proc_dointvec);int neigh_proc_dointvec_jiffies (const struct ctl_table *ctl,int write,void *buffer,size_t *lenp,loff_t *ppos)
@@ -3667,7 +3654,6 @@ int neigh_proc_dointvec_jiffies(const struct ctl_table *ctl, int write, void *buneigh_proc_update (ctl, write);return ret; } -EXPORT_SYMBOL (neigh_proc_dointvec_jiffies); static int neigh_proc_dointvec_userhz_jiffies(const struct ctl_table *ctl, int write, void *buffer, size_t *lenp,
@@ -3687,7 +3673,6 @@ int neigh_proc_dointvec_ms_jiffies(const struct ctl_table *ctl, int write,neigh_proc_update (ctl, write);return ret; } -EXPORT_SYMBOL (neigh_proc_dointvec_ms_jiffies); static int neigh_proc_dointvec_unres_qlen(const struct ctl_table *ctl, int write, void *buffer, size_t *lenp,
@@ -3894,7 +3879,6 @@ int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p, err:return -ENOBUFS ; } -EXPORT_SYMBOL (neigh_sysctl_register);void neigh_sysctl_unregister (struct neigh_parms *p) {
@@ -3905,7 +3889,6 @@ void neigh_sysctl_unregister(struct neigh_parms *p) kfree(t); } } -EXPORT_SYMBOL (neigh_sysctl_unregister);#endif /* CONFIG_SYSCTL */
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
PATCH v6 5/15 neighbour: Remove __rcu from neigh_tables[].
In reply to Kuniyuki IwashimaSince commit 1bf20cc62a54 ("neighbour: remove obsolete EXPORT_SYMBOL()") unexported neigh_table_init(), neigh_table cannot be registered dynamically.
Let's remove RCU protection for neigh_tables[].
Note that neightbl_set() no longer needs RCU because it holds spin_lock_bh(&tbl->lock).
--- net/core/neighbour.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-)
net/core/neighbour.c +9 -14
@@ -1807,7 +1807,7 @@ void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)static struct lock_class_key neigh_table_proxy_queue_class; -static struct neigh_table __rcu *neigh_tables[NEIGH_NR_TABLES ]__read_mostly ; +static struct neigh_table *neigh_tables[NEIGH_NR_TABLES ]__read_mostly ;void neigh_table_init (int index,struct neigh_table *tbl) {
@@ -1864,7 +1864,7 @@ void neigh_table_init(int index, struct neigh_table *tbl) tbl->last_flush = now; tbl->last_rand = now + tbl->parms.reachable_time *20 ; -rcu_assign_pointer (neigh_tables[index], tbl); + neigh_tables[index] = tbl; }/*
@@ -1873,8 +1873,7 @@ void neigh_table_init(int index, struct neigh_table *tbl)*/ int neigh_table_clear (int index,struct neigh_table *tbl) { -RCU_INIT_POINTER (neigh_tables[index],NULL ); -synchronize_rcu (); + neigh_tables[index] =NULL ;/* It is not clean... Fix it to unload IPv6 module safely */ cancel_delayed_work_sync (&tbl->managed_work);
@@ -1906,10 +1905,10 @@ static struct neigh_table *neigh_find_table(int family)switch (family) {case AF_INET : - tbl =rcu_dereference_rtnl (neigh_tables[NEIGH_ARP_TABLE ]); + tbl = neigh_tables[NEIGH_ARP_TABLE ];break ;case AF_INET6 : - tbl =rcu_dereference_rtnl (neigh_tables[NEIGH_ND_TABLE ]); + tbl = neigh_tables[NEIGH_ND_TABLE ];break ; }
@@ -2391,10 +2390,8 @@ static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, ndtmsg =nlmsg_data (nlh); -rcu_read_lock (); -for (tidx =0 ; tidx <NEIGH_NR_TABLES ; tidx++) { - tbl =rcu_dereference (neigh_tables[tidx]); + tbl = neigh_tables[tidx];if (!tbl)continue ;
@@ -2408,7 +2405,6 @@ static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, }if (!found) { -rcu_read_unlock (); err = -ENOENT ; goto errout; }
@@ -2537,7 +2533,6 @@ static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, errout_tbl_lock:spin_unlock_bh (&tbl->lock); -rcu_read_unlock (); errout:return err; }
@@ -2589,7 +2584,7 @@ static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)for (tidx =0 ; tidx <NEIGH_NR_TABLES ; tidx++) {struct neigh_parms *p; - tbl =rcu_dereference (neigh_tables[tidx]); + tbl = neigh_tables[tidx];if (!tbl)continue ;
@@ -2949,7 +2944,7 @@ static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)rcu_read_lock ();for (t =0 ; t <NEIGH_NR_TABLES ; t++) { - tbl =rcu_dereference (neigh_tables[t]); + tbl = neigh_tables[t];if (!tbl)continue ;
@@ -3162,7 +3157,7 @@ int neigh_xmit(int index, struct net_device *dev,struct neighbour *neigh;rcu_read_lock (); - tbl =rcu_dereference (neigh_tables[index]); + tbl = neigh_tables[index];if (!tbl) {rcu_read_unlock (); goto out_kfree_skb;
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
PATCH v6 6/15 neighbour: Store arp_tbl and nd_tbl in net->neigh_tables[].
In reply to Kuniyuki IwashimaWe will remove RTNL for neigh_add() and neigh_delete(), but they are still serialised by per-protocol neigh_table.lock.
We can avoid contention by converting neigh_tables[] to per-netns, but arp_tbl and nd_tbl are directly used in many places.
As a prep, let's store &arp_tbl and &nd_tbl in net->neigh_tables[].
We will replace such users with arp_table(net) and nd_table(net) and then allocate per-netns neigh_table.
Note that nd_table() still returns &nd_tbl in case disable_ipv6_mod is 1 because some buggy drivers use nd_tbl without checking it.
proc_create_net() is guarded with CONFIG_PROC_FS because it returns NULL when =n and setup_net() fails and panic()s. Also a later patch moves neigh_sysctl_register() under the guard.
---
v4: Add note about CONFIG_PROC_FS
--- include/net/arp.h | 6 +++++- include/net/ndisc.h | 9 +++++++++ include/net/neighbour.h | 2 ++ include/net/net_namespace.h | 4 ++++ net/core/neighbour.c | 12 ++++++++++++ net/ipv4/arp.c | 23 +++++++++++++++++++++-- net/ipv6/ndisc.c | 12 +++++++++++- 7 files changed, 64 insertions(+), 4 deletions(-)
include/net/arp.h +5 -1
@@ -7,9 +7,13 @@#include <linux/hash.h> #include <net/neighbour.h> -extern struct neigh_table arp_tbl; +static inline struct neigh_table *arp_table (struct net *net) +{ +return net->neigh_tables[NEIGH_ARP_TABLE ]; +} +static inline u32 arp_hashfn (const void *pkey,const struct net_device *dev,u32 *hash_rnd) {u32 key = *(const u32 *)pkey;
include/net/ndisc.h +9 -0
@@ -67,6 +67,15 @@ struct prefix_info;extern struct neigh_table nd_tbl; +static inline struct neigh_table *nd_table (struct net *net) +{ +#if IS_ENABLED (CONFIG_IPV6 ) +if (disable_ipv6_mod) +return &nd_tbl; +#endif +return net->neigh_tables[NEIGH_ND_TABLE ]; +} +struct nd_msg {struct icmp6hdr icmph;struct in6_addr target;
include/net/neighbour.h +2 -0
@@ -339,6 +339,8 @@ static inline void neigh_confirm(struct neighbour *n) } } +int neigh_table_register (struct net *net,struct neigh_table *tbl,int index); +void neigh_table_unregister (struct net *net,int index);void neigh_table_init (int index,struct neigh_table *tbl);int neigh_table_clear (int index,struct neigh_table *tbl); struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
include/net/net_namespace.h +4 -0
@@ -39,6 +39,7 @@#include <net/netns/mctp.h> #include <net/netns/vsock.h> #include <net/net_trackers.h> +#include <net/neighbour_tables.h> #include <linux/ns_common.h> #include <linux/idr.h> #include <linux/skbuff.h>
@@ -47,6 +48,7 @@struct user_namespace ;struct proc_dir_entry ; +struct neigh_table ;struct net_device ;struct sock ;struct ctl_table_header ;
@@ -103,6 +105,8 @@ struct net {struct proc_dir_entry *proc_net;struct proc_dir_entry *proc_net_stat; +struct neigh_table *neigh_tables[NEIGH_NR_TABLES ]; +#ifdef CONFIG_SYSCTL struct ctl_table_set sysctls;#endif
net/core/neighbour.c +12 -0
@@ -1899,6 +1899,18 @@ int neigh_table_clear(int index, struct neigh_table *tbl)return 0 ; } +int neigh_table_register (struct net *net,struct neigh_table *tbl,int index) +{ + net->neigh_tables[index] = tbl; + +return 0 ; +} + +void neigh_table_unregister (struct net *net,int index) +{ + net->neigh_tables[index] =NULL ; +} +static struct neigh_table *neigh_find_table (int family) {struct neigh_table *tbl =NULL ;
net/ipv4/arp.c +21 -2
@@ -1495,15 +1495,34 @@ static const struct seq_operations arp_seq_ops = {static int __net_init arp_net_init (struct net *net) { +int err; + + err =neigh_table_register (net, &arp_tbl,NEIGH_ARP_TABLE ); +if (err) + goto err; + +#ifdef CONFIG_PROC_FS if (!proc_create_net ("arp" ,0444 , net->proc_net, &arp_seq_ops, -sizeof (struct neigh_seq_state ))) -return -ENOMEM ; +sizeof (struct neigh_seq_state ))) { + err = -ENOMEM ; + goto err_proc_create; + } +#endif +return 0 ; + +#ifdef CONFIG_PROC_FS +err_proc_create: +neigh_table_unregister (net,NEIGH_ARP_TABLE ); +#endif +err: +return err; } static void __net_exit arp_net_exit(struct net *net) {remove_proc_entry ("arp" , net->proc_net); +neigh_table_unregister (net,NEIGH_ARP_TABLE ); }static struct pernet_operations arp_net_ops = {
net/ipv6/ndisc.c +11 -1
@@ -1974,12 +1974,16 @@ static int __net_init ndisc_net_init(struct net *net)struct sock *sk;int err; + err =neigh_table_register (net, &nd_tbl,NEIGH_ND_TABLE ); +if (err) + goto err; + err =inet_ctl_sock_create (&sk,PF_INET6 ,SOCK_RAW ,IPPROTO_ICMPV6 , net);if (err <0 ) {net_err_ratelimited ("NDISC: Failed to initialize the control socket (err %d)\n" , err); -return err; + goto err_sock_create; } net->ipv6.ndisc_sk = sk;
@@ -1990,11 +1994,17 @@ static int __net_init ndisc_net_init(struct net *net)inet6_clear_bit (MC6_LOOP , sk);return 0 ; + +err_sock_create: +neigh_table_unregister (net,NEIGH_ND_TABLE ); +err: +return err; } static void __net_exit ndisc_net_exit(struct net *net) {inet_ctl_sock_destroy (net->ipv6.ndisc_sk); +neigh_table_unregister (net,NEIGH_ND_TABLE ); }static struct pernet_operations ndisc_net_ops = {
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
[email protected]
AI reviewRe: [PATCH v6 net-next 06/15] neighbour: Store arp_tbl and nd_tbl in net->neigh_tables[].
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Critical: 0 · High: 0 (1) · Medium: 0 · Low: 1
- [Low] Commit message rationale for the new #ifdef CONFIG_PROC_FS guard in arp_net_init() is factually wrong.
Pre-existing issues:
- [High] Concern (verified RESOLVED later in this series): at this commit, ndisc_init() (net/ipv6/ndisc.c:2015-2041) calls…
--
neighbour: Store arp_tbl and nd_tbl in net->neigh_tables[].
This adds a per-netns neigh_tables[] array to struct net along with neigh_table_register()/neigh_table_unregister() helpers, and stores &arp_tbl and &nd_tbl there from arp_net_init() and ndisc_net_init() as preparation for making neigh_table per-netns. New arp_table(net) and nd_table(net) accessors are added.
The commit message says:
proc_create_net() is guarded with CONFIG_PROC_FS because it returns NULL when =n and setup_net() fails and panic()s.
[Severity: Low]
Is the panic() part of this description accurate?
arp_net_ops is registered from arp_init():
net/ipv4/arp.c:arp_init() {
neigh_table_init(NEIGH_ARP_TABLE, &arp_tbl);
dev_add_pack(&arp_packet_type);
register_pernet_subsys(&arp_net_ops);
...
}arp_init() runs from inet_init() (fs_initcall), while the only setup_net() call that panics is setup_net(&init_net) in net_ns_init(), which is a pure_initcall and has already completed by then. For an ops registered later, a failing ->init is handled in __register_pernet_operations():
error = ops_init(ops, net); /* on failure the ops are undone and the error returned */
and arp_init() is void, so that -ENOMEM is silently discarded.
Wouldn't the actual consequence on CONFIG_PROC_FS=n be that arp_net_ops never gets registered, so net->neigh_tables[NEIGH_ARP_TABLE] stays NULL in every netns, which would then break arp_table(net) once the follow-up patches convert the ARP users over? Could the changelog describe that instead?
Quoted 56 lines
net/core/neighbour.c +12
-0@@ -1899,6 +1899,18 @@ int neigh_table_clear(int index, struct neigh_table *tbl)return 0 ; } +int neigh_table_register (struct net *net,struct neigh_table *tbl,int index) +{ + net->neigh_tables[index] = tbl; + +return 0 ; +} + +void neigh_table_unregister (struct net *net,int index) +{ + net->neigh_tables[index] =NULL ; +} +static struct neigh_table *neigh_find_table (int family) {struct neigh_table *tbl =NULL ;net/ipv4/arp.c +20
-2@@ -1495,15 +1495,34 @@ static const struct seq_operations arp_seq_ops = {static int __net_init arp_net_init (struct net *net) { +int err; + + err =neigh_table_register (net, &arp_tbl,NEIGH_ARP_TABLE ); +if (err) + goto err; + +#ifdef CONFIG_PROC_FS if (!proc_create_net ("arp" ,0444 , net->proc_net, &arp_seq_ops, -sizeof (struct neigh_seq_state ))) -return -ENOMEM ; +sizeof (struct neigh_seq_state ))) { + err = -ENOMEM ; + goto err_proc_create; + } +#endif +return 0 ; + +#ifdef CONFIG_PROC_FS +err_proc_create: +neigh_table_unregister (net,NEIGH_ARP_TABLE ); +#endif +err: +return err; }
[ ... ]
Quoted 10 lines
net/ipv6/ndisc.c +2
-0@@ -1974,12 +1974,16 @@ static int __net_init ndisc_net_init(struct net *net)struct sock *sk;int err; + err =neigh_table_register (net, &nd_tbl,NEIGH_ND_TABLE ); +if (err)
net/ipv6/ndisc.c static int __net_init ndisc_net_init(struct net *net) · line 1987
Quoted 10 lines
net/ipv6/ndisc.c +3
-1+ goto err; + err =inet_ctl_sock_create (&sk,PF_INET6 ,SOCK_RAW ,IPPROTO_ICMPV6 , net);if (err <0 ) {net_err_ratelimited ("NDISC: Failed to initialize the control socket (err %d)\n" , err); -return err; + goto err_sock_create; }
[Severity: High]
This isn't a bug introduced by this patch, but while unwinding is being added here, does the neighbour table in ndisc_init() still leak on the sysctl failure path?
net/ipv6/ndisc.c:ndisc_init() {
neigh_table_init(NEIGH_ND_TABLE, &nd_tbl);#ifdef CONFIG_SYSCTL err = neigh_sysctl_register(NULL, &nd_tbl.parms, ndisc_ifinfo_sysctl_change); if (err) goto out_unregister_pernet; out: #endif return err;
#ifdef CONFIG_SYSCTLout_unregister_pernet:
unregister_pernet_subsys(&ndisc_net_ops); goto out;
#endif
}
If neigh_sysctl_register() returns -ENOBUFS (kmemdup or register_net_sysctl_sz() failure), only the pernet ops are unregistered and neigh_table_clear(NEIGH_ND_TABLE, &nd_tbl) is never called, so nd_tbl->stats, nd_tbl->nht, nd_tbl->phash_buckets and the /proc/net/stat/ndisc_cache entry stay around.
neigh_table_init() also leaves work queued and the proxy timer armed on the abandoned table:
net/core/neighbour.c:neigh_table_init() {
INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
tbl->parms.reachable_time);
INIT_DEFERRABLE_WORK(&tbl->managed_work, neigh_managed_work);
queue_delayed_work(system_power_efficient_wq, &tbl->managed_work, 0);
...
}inet6_init() jumps to ndisc_fail on ndisc_init() failure and never calls ndisc_cleanup(), so nothing balances the initialised table.
This does get resolved later in the same series: after "neighbour:
Namespacify neigh_tables." ndisc_init() becomes just register_pernet_subsys(&ndisc_net_ops), and both neigh_table_register() and neigh_sysctl_register() move into ndisc_net_init() with full unwinding through neigh_sysctl_unregister() and neigh_table_unregister(). Noting it here only for completeness.
Quoted 11 lines
net/ipv6/ndisc.c +5
-0net->ipv6.ndisc_sk = sk;@@ -1990,11 +1994,17 @@ static int __net_init ndisc_net_init(struct net *net)inet6_clear_bit (MC6_LOOP , sk);return 0 ; + +err_sock_create: +neigh_table_unregister (net,NEIGH_ND_TABLE ); +err: +return err; }
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
PATCH v6 7/15 neighbour: Remove neigh_tables[].
In reply to Kuniyuki IwashimaNow, &arp_tbl and &nd_tbl are stored in net->neigh_tables[].
Let's use net->neigh_tables[] in neighbour.c and remove the global neigh_tables[].
--- include/net/neighbour.h | 4 ++-- net/core/neighbour.c | 34 +++++++++++++++------------------- net/ipv4/arp.c | 2 +- net/ipv6/ndisc.c | 4 ++-- 4 files changed, 20 insertions(+), 24 deletions(-)
include/net/neighbour.h +2 -2
@@ -341,8 +341,8 @@ static inline void neigh_confirm(struct neighbour *n)int neigh_table_register (struct net *net,struct neigh_table *tbl,int index);void neigh_table_unregister (struct net *net,int index); -void neigh_table_init (int index,struct neigh_table *tbl); -int neigh_table_clear (int index,struct neigh_table *tbl); +void neigh_table_init (struct neigh_table *tbl); +int neigh_table_clear (struct neigh_table *tbl);struct neighbour *neigh_lookup (struct neigh_table *tbl,const void *pkey,struct net_device *dev); struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
net/core/neighbour.c +15 -19
@@ -1807,9 +1807,7 @@ void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)static struct lock_class_key neigh_table_proxy_queue_class; -static struct neigh_table *neigh_tables[NEIGH_NR_TABLES ]__read_mostly ; - -void neigh_table_init (int index,struct neigh_table *tbl) +void neigh_table_init (struct neigh_table *tbl) {unsigned long now = jiffies;unsigned long phsize;
@@ -1863,18 +1861,14 @@ void neigh_table_init(int index, struct neigh_table *tbl) tbl->last_flush = now; tbl->last_rand = now + tbl->parms.reachable_time *20 ; - - neigh_tables[index] = tbl; }/* * Only called from ndisc_cleanup(), which means this is dead code * because we no longer can unload IPv6 module. */ -int neigh_table_clear (int index,struct neigh_table *tbl) +int neigh_table_clear (struct neigh_table *tbl) { - neigh_tables[index] =NULL ; -/* It is not clean... Fix it to unload IPv6 module safely */ cancel_delayed_work_sync (&tbl->managed_work);cancel_delayed_work_sync (&tbl->gc_work);
@@ -1911,16 +1905,16 @@ void neigh_table_unregister(struct net *net, int index) net->neigh_tables[index] =NULL ; } -static struct neigh_table *neigh_find_table (int family) +static struct neigh_table *neigh_find_table (struct net *net,int family) {struct neigh_table *tbl =NULL ;switch (family) {case AF_INET : - tbl = neigh_tables[NEIGH_ARP_TABLE ]; + tbl = net->neigh_tables[NEIGH_ARP_TABLE ];break ;case AF_INET6 : - tbl = neigh_tables[NEIGH_ND_TABLE ]; + tbl = net->neigh_tables[NEIGH_ND_TABLE ];break ; }
@@ -1974,7 +1968,7 @@ static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, } } - tbl =neigh_find_table (ndm->ndm_family); + tbl =neigh_find_table (net, ndm->ndm_family);if (tbl ==NULL )return -EAFNOSUPPORT ;
@@ -2060,7 +2054,7 @@ static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, } } - tbl =neigh_find_table (ndm->ndm_family); + tbl =neigh_find_table (net, ndm->ndm_family);if (tbl ==NULL )return -EAFNOSUPPORT ;
@@ -2403,7 +2397,7 @@ static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, ndtmsg =nlmsg_data (nlh);for (tidx =0 ; tidx <NEIGH_NR_TABLES ; tidx++) { - tbl = neigh_tables[tidx]; + tbl = net->neigh_tables[tidx];if (!tbl)continue ;
@@ -2596,7 +2590,7 @@ static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)for (tidx =0 ; tidx <NEIGH_NR_TABLES ; tidx++) {struct neigh_parms *p; - tbl = neigh_tables[tidx]; + tbl = net->neigh_tables[tidx];if (!tbl)continue ;
@@ -2933,6 +2927,7 @@ static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb) {const struct nlmsghdr *nlh = cb->nlh;struct neigh_dump_filter filter = {}; +struct net *net =sock_net (skb->sk);struct neigh_table *tbl;int t, family, s_t;int proxy =0 ;
@@ -2956,8 +2951,7 @@ static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)rcu_read_lock ();for (t =0 ; t <NEIGH_NR_TABLES ; t++) { - tbl = neigh_tables[t]; - + tbl = net->neigh_tables[t];if (!tbl)continue ;if (t < s_t || (family && tbl->family != family))
@@ -3079,7 +3073,7 @@ static int neigh_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,rcu_read_lock (); - tbl =neigh_find_table (ndm->ndm_family); + tbl =neigh_find_table (net, ndm->ndm_family);if (!tbl) {NL_SET_ERR_MSG (extack,"Unsupported family in header for neighbor get request" ); err = -EAFNOSUPPORT ;
@@ -3167,9 +3161,11 @@ int neigh_xmit(int index, struct net_device *dev,if (likely (index <NEIGH_NR_TABLES )) {struct neigh_table *tbl;struct neighbour *neigh; +struct net *net;rcu_read_lock (); - tbl = neigh_tables[index]; + net =dev_net_rcu (dev); + tbl = net->neigh_tables[index];if (!tbl) {rcu_read_unlock (); goto out_kfree_skb;
net/ipv4/arp.c +1 -1
@@ -1532,7 +1532,7 @@ static struct pernet_operations arp_net_ops = {void __init arp_init (void ) { -neigh_table_init (NEIGH_ARP_TABLE , &arp_tbl); +neigh_table_init (&arp_tbl);dev_add_pack (&arp_packet_type);register_pernet_subsys (&arp_net_ops);
net/ipv6/ndisc.c +2 -2
@@ -2022,7 +2022,7 @@ int __init ndisc_init(void)/* * Initialize the neighbour table */ -neigh_table_init (NEIGH_ND_TABLE , &nd_tbl); +neigh_table_init (&nd_tbl);#ifdef CONFIG_SYSCTL err = neigh_sysctl_register(NULL, &nd_tbl.parms,
@@ -2055,6 +2055,6 @@ void ndisc_cleanup(void)#ifdef CONFIG_SYSCTL neigh_sysctl_unregister (&nd_tbl.parms);#endif -neigh_table_clear (NEIGH_ND_TABLE , &nd_tbl); +neigh_table_clear (&nd_tbl);unregister_pernet_subsys (&ndisc_net_ops); }
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
PATCH v6 8/15 ipv4: Replace &arp_tbl with arp_table(net).
In reply to Kuniyuki IwashimaWe will allocate per-netns neigh_table in net->neigh_tables[].
Let's replace &arp_tbl with arp_table(net) and remove extern definition for arp_tbl.
Three notes:
1. mlx5e_rep_netevent_event() and nfp_tun_neigh_event_handler() have code assuming neigh_table other than &arp_tbl and &nb_tbl, and the part is removed as it will be false once per-netns table is allocated.
2. prestera and rocker uses init_net because they set
dev->netns_immutable to true.
3. mlx5e_tc_update_neigh_used_value() dereferences nhe->neigh_dev in trace_mlx5e_tc_update_neigh_used_value().
---
v2: Add note about mlx5e_tc_update_neigh_used_value()
--- drivers/infiniband/ulp/ipoib/ipoib_main.c | 27 ++++-- .../marvell/prestera/prestera_router.c | 10 ++- .../mellanox/mlx5/core/en/rep/neigh.c | 22 ++--- .../mellanox/mlx5/core/en/tc_tun_encap.c | 23 ++--- .../mellanox/mlx5/core/en_accel/ipsec.c | 6 +- .../ethernet/mellanox/mlxsw/spectrum_router.c | 18 ++-- .../ethernet/mellanox/mlxsw/spectrum_span.c | 5 +- .../netronome/nfp/flower/tunnel_conf.c | 10 +-- drivers/net/ethernet/rocker/rocker_main.c | 2 +- drivers/net/ethernet/rocker/rocker_ofdpa.c | 2 +- drivers/net/ethernet/sfc/tc_counters.c | 6 +- drivers/net/ethernet/sfc/tc_encap_actions.c | 2 +- drivers/net/vxlan/vxlan_core.c | 10 ++- include/net/arp.h | 6 +- include/net/route.h | 7 +- net/bridge/br_arp_nd_proxy.c | 2 +- net/ipv4/arp.c | 88 +++++++++++-------- net/ipv4/devinet.c | 18 ++-- net/ipv4/fib_semantics.c | 5 +- net/ipv4/route.c | 2 +- 20 files changed, 159 insertions(+), 112 deletions(-)
drivers/infiniband/ulp/ipoib/ipoib_main.c +19 -8
@@ -1378,7 +1378,8 @@ struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)return neigh; } -static void __ipoib_reap_neigh (struct ipoib_dev_priv *priv) +static void __ipoib_reap_neigh (struct ipoib_dev_priv *priv, +int gc_interval) {struct ipoib_neigh_table *ntbl = &priv->ntbl;struct ipoib_neigh_hash *htbl;
@@ -1397,7 +1398,7 @@ static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv) goto out_unlock;/* neigh is obsolete if it was idle for two GC periods */ - dt =2 * arp_tbl.gc_interval; + dt =2 * gc_interval; neigh_obsolete = jiffies - dt;for (i =0 ; i < htbl->size; i++) {
@@ -1433,11 +1434,16 @@ static void ipoib_reap_neigh(struct work_struct *work) {struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv, neigh_reap_task.work); +struct net_device *dev = priv->dev; +struct net *net =dev_net (dev); +struct neigh_table *tbl; +int gc_interval; -__ipoib_reap_neigh (priv); + tbl =arp_table (net); + gc_interval = tbl->gc_interval; +__ipoib_reap_neigh (priv, gc_interval); -queue_delayed_work (priv->wq, &priv->neigh_reap_task, - arp_tbl.gc_interval); +queue_delayed_work (priv->wq, &priv->neigh_reap_task, gc_interval); }
@@ -1590,8 +1596,11 @@ void ipoib_neigh_free(struct ipoib_neigh *neigh)static int ipoib_neigh_hash_init (struct ipoib_dev_priv *priv) {struct ipoib_neigh_table *ntbl = &priv->ntbl; -struct ipoib_neigh_hash *htbl;struct ipoib_neigh __rcu **buckets; +struct net_device *dev = priv->dev; +struct net *net =dev_net (dev); +struct ipoib_neigh_hash *htbl; +struct neigh_table *tbl;u32 size;clear_bit (IPOIB_NEIGH_TBL_FLUSH , &priv->flags);
@@ -1599,7 +1608,9 @@ static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv) htbl =kzalloc_obj (*htbl);if (!htbl)return -ENOMEM ; - size =roundup_pow_of_two (arp_tbl.gc_thresh3); + + tbl =arp_table (net); + size =roundup_pow_of_two (tbl->gc_thresh3); buckets =kvzalloc_objs (*buckets, size);if (!buckets) {kfree (htbl);
@@ -1614,7 +1625,7 @@ static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)/* start garbage collection */ queue_delayed_work (priv->wq, &priv->neigh_reap_task, - arp_tbl.gc_interval); + tbl->gc_interval);return 0 ; }
drivers/net/ethernet/marvell/prestera/prestera_router.c +6 -4
@@ -683,7 +683,7 @@ __prestera_k_arb_n_offload_set(struct prestera_switch *sw, {struct neighbour *n; - n =neigh_lookup (&arp_tbl, &nc->key.addr.u.ipv4, + n =neigh_lookup (arp_table (&init_net), &nc->key.addr.u.ipv4, nc->key.dev);if (!n)return ;
@@ -790,7 +790,7 @@ __prestera_k_arb_nc_kern_n_fetch(struct prestera_switch *sw,int err;memset (&nc->nh_neigh_info,0 ,sizeof (nc->nh_neigh_info)); - n =neigh_lookup (&arp_tbl, &nc->key.addr.u.ipv4, nc->key.dev); + n =neigh_lookup (arp_table (&init_net), &nc->key.addr.u.ipv4, nc->key.dev);if (!n) goto out;
@@ -1052,10 +1052,12 @@ static void __prestera_k_arb_hw_state_upd(struct prestera_switch *sw,#endif /* PRESTERA_IMPLICITY_RESOLVE_DEAD_NEIGH */ if (nc->key.addr.v ==PRESTERA_IPV4 ) { - n =neigh_lookup (&arp_tbl, &nc->key.addr.u.ipv4, +struct neigh_table *tbl =arp_table (&init_net); + + n =neigh_lookup (tbl, &nc->key.addr.u.ipv4, nc->key.dev);if (!n) - n =neigh_create (&arp_tbl, &nc->key.addr.u.ipv4, + n =neigh_create (tbl, &nc->key.addr.u.ipv4, nc->key.dev); }else { n =NULL ;
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c +9 -13
@@ -27,10 +27,16 @@ static unsigned long mlx5e_rep_ipv6_interval(void)static void mlx5e_rep_neigh_update_init_interval (struct mlx5e_rep_priv *rpriv) { -unsigned long ipv4_interval =NEIGH_VAR (&arp_tbl.parms,DELAY_PROBE_TIME );unsigned long ipv6_interval =mlx5e_rep_ipv6_interval ();struct net_device *netdev = rpriv->netdev; -struct mlx5e_priv *priv =netdev_priv (netdev); +struct net *net =dev_net (netdev); +unsigned long ipv4_interval; +struct neigh_table *tbl; +struct mlx5e_priv *priv; + + priv =netdev_priv (netdev); + tbl =arp_table (net); + ipv4_interval =NEIGH_VAR (&tbl->parms,DELAY_PROBE_TIME ); rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);mlx5_fc_update_sampling_interval (priv->mdev, rpriv->neigh_update.min_interval);
@@ -217,12 +223,6 @@ static int mlx5e_rep_netevent_event(struct notifier_block *nb,switch (event) {case NETEVENT_NEIGH_UPDATE : n = ptr; -#if IS_ENABLED (CONFIG_IPV6 ) -if (n->tbl != &nd_tbl && n->tbl != &arp_tbl) -#else - if (n->tbl != &arp_tbl) -#endif -return NOTIFY_DONE ; update_work =mlx5e_alloc_neigh_update_work (priv, n);if (!update_work)
@@ -238,11 +238,7 @@ static int mlx5e_rep_netevent_event(struct notifier_block *nb,* changes in the default table, we only care about changes * done per device delay prob time parameter. */ -#if IS_ENABLED (CONFIG_IPV6 ) -if (!p->dev || (p->tbl != &nd_tbl && p->tbl != &arp_tbl)) -#else - if (!p->dev || p->tbl != &arp_tbl) -#endif +if (!p->dev)return NOTIFY_DONE ;rcu_read_lock ();
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +12 -11
@@ -393,20 +393,10 @@ void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)struct mlx5e_encap_entry *e =NULL ;struct mlx5e_tc_flow *flow;struct mlx5_fc *counter; -struct neigh_table *tbl;bool neigh_used = false;struct neighbour *n;u64 lastuse; -if (m_neigh->family ==AF_INET ) - tbl = &arp_tbl; -#if IS_ENABLED (CONFIG_IPV6 ) - else if (m_neigh->family == AF_INET6) - tbl = &nd_tbl; -#endif - else - return; -/* mlx5e_get_next_valid_encap() releases previous encap before returning * next one. */
@@ -447,12 +437,23 @@ void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)trace_mlx5e_tc_update_neigh_used_value (nhe, neigh_used);if (neigh_used) { +struct net_device *dev =READ_ONCE (nhe->neigh_dev); +struct net *net =dev_net (dev); +struct neigh_table *tbl; + nhe->reported_lastuse = jiffies; +#if IS_ENABLED (CONFIG_IPV6 ) +if (m_neigh->family !=AF_INET ) + tbl = &nd_tbl; + else +#endif + tbl =arp_table (net); +/* find the relevant neigh according to the cached device and * dst ip pair */ - n =neigh_lookup (tbl, &m_neigh->dst_ip,READ_ONCE (nhe->neigh_dev)); + n =neigh_lookup (tbl, &m_neigh->dst_ip, dev);if (!n)return ;
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c +4 -2
@@ -262,6 +262,7 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry,struct net_device *netdev = sa_entry->dev;struct xfrm_state *x = sa_entry->x;struct dst_entry *rt_dst_entry; +struct neigh_table *tbl;struct flowi4 fl4 = {};struct flowi6 fl6 = {};struct neighbour *n;
@@ -364,9 +365,10 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry,return ; neigh: - n =neigh_lookup (&arp_tbl, pkey, netdev); + tbl =arp_table (dev_net (netdev)); + n =neigh_lookup (tbl, pkey, netdev);if (!n) { - n =neigh_create (&arp_tbl, pkey, netdev); + n =neigh_create (tbl, pkey, netdev);if (IS_ERR (n))return ;neigh_event_send (n,NULL );
drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c +12 -6
@@ -2397,14 +2397,15 @@ mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)static void mlxsw_sp_router_neighs_update_interval_init (struct mlxsw_sp *mlxsw_sp) { +struct net *net =mlxsw_sp_net (mlxsw_sp);unsigned long interval;#if IS_ENABLED (CONFIG_IPV6 ) interval = min_t(unsigned long, -NEIGH_VAR (&arp_tbl.parms,DELAY_PROBE_TIME ), +NEIGH_VAR (&arp_table (net)->parms,DELAY_PROBE_TIME ),NEIGH_VAR (&nd_tbl.parms,DELAY_PROBE_TIME ));#else - interval =NEIGH_VAR (&arp_tbl.parms,DELAY_PROBE_TIME ); + interval =NEIGH_VAR (&arp_table (net)->parms,DELAY_PROBE_TIME );#endif mlxsw_sp->router->neighs_update.interval =jiffies_to_msecs (interval); }
@@ -2414,6 +2415,8 @@ static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp, int ent_index) {u64 max_rifs =MLXSW_CORE_RES_GET (mlxsw_sp->core,MAX_RIFS ); +struct net *net =mlxsw_sp_net (mlxsw_sp); +struct neigh_table *tbl;struct net_device *dev;struct neighbour *n;__be32 dipn;
@@ -2429,9 +2432,10 @@ static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,return ; } + tbl =arp_table (net); dipn =htonl (dip); dev =mlxsw_sp_rif_dev (mlxsw_sp->router->rifs[rif]); - n =neigh_lookup (&arp_tbl, &dipn, dev); + n =neigh_lookup (tbl, &dipn, dev);if (!n)return ;
@@ -3014,11 +3018,12 @@ static int mlxsw_sp_neigh_rif_made_sync(struct mlxsw_sp *mlxsw_sp, .mlxsw_sp = mlxsw_sp, .rif = rif, }; +struct net *net =mlxsw_sp_net (mlxsw_sp);if (!mlxsw_sp_dev_lower_is_port (mlxsw_sp_rif_dev (rif)))return 0 ; -neigh_for_each (&arp_tbl, mlxsw_sp_neigh_rif_made_sync_each, &rms); +neigh_for_each (arp_table (net), mlxsw_sp_neigh_rif_made_sync_each, &rms);if (rms.err) goto err_arp;
@@ -4622,7 +4627,7 @@ static int mlxsw_sp_nexthop4_init(struct mlxsw_sp *mlxsw_sp, nh->nh_weight =1 ;#endif memcpy (&nh->gw_addr, &fib_nh->fib_nh_gw4,sizeof (fib_nh->fib_nh_gw4)); - nh->neigh_tbl = &arp_tbl; + nh->neigh_tbl =arp_table (mlxsw_sp_net (mlxsw_sp)); err =mlxsw_sp_nexthop_insert (mlxsw_sp, nh);if (err)return err;
@@ -5112,6 +5117,7 @@ mlxsw_sp_nexthop_obj_init(struct mlxsw_sp *mlxsw_sp, struct nh_notifier_single_info *nh_obj, int weight) {struct net_device *dev = nh_obj->dev; +struct net *net =dev_net (dev);int err; nh->nhgi = nh_grp->nhgi;
@@ -5120,7 +5126,7 @@ mlxsw_sp_nexthop_obj_init(struct mlxsw_sp *mlxsw_sp,switch (nh_obj->gw_family) {case AF_INET :memcpy (&nh->gw_addr, &nh_obj->ipv4,sizeof (nh_obj->ipv4)); - nh->neigh_tbl = &arp_tbl; + nh->neigh_tbl =arp_table (net);break ;case AF_INET6 :memcpy (&nh->gw_addr, &nh_obj->ipv6,sizeof (nh_obj->ipv6));
drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c +4 -1
@@ -456,6 +456,7 @@ mlxsw_sp_span_entry_gretap4_parms(struct mlxsw_sp *mlxsw_sp,bool inherit_tos = tparm.iph.tos &0x1 ;bool inherit_ttl = !tparm.iph.ttl;union mlxsw_sp_l3addr gw = daddr; +struct neigh_table *tbl =NULL ;struct net_device *l3edev; if (!(to_dev->flags & IFF_UP) ||
@@ -469,9 +470,11 @@ mlxsw_sp_span_entry_gretap4_parms(struct mlxsw_sp *mlxsw_sp,return mlxsw_sp_span_entry_unoffloadable (sparmsp); l3edev =mlxsw_sp_span_gretap4_route (to_dev, &saddr.addr4, &gw.addr4); +if (l3edev) + tbl =arp_table (dev_net (l3edev));return mlxsw_sp_span_entry_tunnel_parms_common (l3edev, saddr, daddr, gw, tparm.iph.ttl, - &arp_tbl, sparmsp); + tbl, sparmsp); } static int
drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c +3 -7
@@ -209,6 +209,7 @@ void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb) {struct nfp_tun_active_tuns *payload;struct net_device *netdev; +struct neigh_table *tbl;int count, i, pay_len;struct neighbour *n;__be32 ipv4_addr;
@@ -235,7 +236,8 @@ void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb)if (!netdev)continue ; - n =neigh_lookup (&arp_tbl, &ipv4_addr, netdev); + tbl =arp_table (dev_net (netdev)); + n =neigh_lookup (tbl, &ipv4_addr, netdev);if (!n)continue ;
@@ -729,12 +731,6 @@ nfp_tun_neigh_event_handler(struct notifier_block *nb, unsigned long event,default :return NOTIFY_DONE ; } -#if IS_ENABLED (CONFIG_IPV6 ) -if (n->tbl != &nd_tbl && n->tbl != &arp_tbl) -#else -if (n->tbl != &arp_tbl) -#endif -return NOTIFY_DONE ; app_priv = container_of(nb, struct nfp_flower_priv, tun.neigh_nb); app = app_priv->app;
drivers/net/ethernet/rocker/rocker_main.c +1 -1
@@ -3134,7 +3134,7 @@ static int rocker_netevent_event(struct notifier_block *unused,switch (event) {case NETEVENT_NEIGH_UPDATE : -if (n->tbl != &arp_tbl) +if (n->tbl !=arp_table (&init_net))return NOTIFY_DONE ; dev = n->dev;if (!rocker_port_dev_check (dev))
drivers/net/ethernet/rocker/rocker_ofdpa.c +1 -1
@@ -1336,7 +1336,7 @@ static int ofdpa_port_ipv4_resolve(struct ofdpa_port *ofdpa_port,int err =0 ;if (!n) { - n =neigh_create (&arp_tbl, &ip_addr, dev); + n =neigh_create (arp_table (&init_net), &ip_addr, dev);if (IS_ERR (n))return PTR_ERR (n); }
drivers/net/ethernet/sfc/tc_counters.c +5 -1
@@ -91,6 +91,7 @@ static void efx_tc_counter_work(struct work_struct *work)struct efx_tc_action_set *act;unsigned long touched;struct neighbour *n; +struct net *net;spin_lock_bh (&cnt->lock); touched =READ_ONCE (cnt->touched);
@@ -103,12 +104,15 @@ static void efx_tc_counter_work(struct work_struct *work)continue ;if (time_after_eq (encap->neigh->used, touched))continue ; + encap->neigh->used = touched; + net = encap->neigh->net; +/* We have passed traffic using this ARP entry, so * indicate to the ARP cache that it's still active */ if (encap->neigh->dst_ip) - n =neigh_lookup (&arp_tbl, &encap->neigh->dst_ip, + n =neigh_lookup (arp_table (net), &encap->neigh->dst_ip, encap->neigh->egdev);else #if IS_ENABLED(CONFIG_IPV6)
drivers/net/ethernet/sfc/tc_encap_actions.c +1 -1
@@ -527,7 +527,7 @@ static int efx_neigh_event(struct efx_nic *efx, struct neighbour *n)if (WARN_ON (!efx->tc))return NOTIFY_DONE ; -if (n->tbl == &arp_tbl) { +if (n->tbl->family ==AF_INET ) { keysize =sizeof (keys.dst_ip);#if IS_ENABLED (CONFIG_IPV6 ) } else if (n->tbl == &nd_tbl) {
drivers/net/vxlan/vxlan_core.c +7 -3
@@ -1841,11 +1841,12 @@ static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)static int arp_reduce (struct net_device *dev,struct sk_buff *skb,__be32 vni) { +struct neigh_table *tbl =arp_table (dev_net (dev));struct vxlan_dev *vxlan =netdev_priv (dev); +struct neighbour *n;struct arphdr *parp;u8 *arpptr, *sha;__be32 sip, tip; -struct neighbour *n;if (dev->flags &IFF_NOARP ) goto out;
@@ -1877,7 +1878,7 @@ static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni) ipv4_is_multicast(tip)) goto out; - n =neigh_lookup (&arp_tbl, &tip, dev); + n =neigh_lookup (tbl, &tip, dev);if (n) {struct vxlan_rdst *rdst =NULL ;
@@ -2106,6 +2107,7 @@ static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)static bool route_shortcircuit (struct net_device *dev,struct sk_buff *skb) {struct vxlan_dev *vxlan =netdev_priv (dev); +struct neigh_table *tbl;struct neighbour *n;if (is_multicast_ether_addr (eth_hdr (skb)->h_dest))
@@ -2119,8 +2121,10 @@ static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)if (!pskb_network_may_pull (skb,sizeof (struct iphdr )))return false; + + tbl =arp_table (dev_net (dev)); pip =ip_hdr (skb); - n =neigh_lookup (&arp_tbl, &pip->daddr, dev); + n =neigh_lookup (tbl, &pip->daddr, dev);if (!n && (vxlan->cfg.flags &VXLAN_F_L3MISS )) {union vxlan_addr ipa = { .sin.sin_addr.s_addr = pip->daddr,
include/net/arp.h +3 -3
@@ -7,8 +7,6 @@#include <linux/hash.h> #include <net/neighbour.h> -extern struct neigh_table arp_tbl; -static inline struct neigh_table *arp_table (struct net *net) {return net->neigh_tables[NEIGH_ARP_TABLE ];
@@ -25,10 +23,12 @@ static inline u32 arp_hashfn(const void *pkey, const struct net_device *dev, u32#ifdef CONFIG_INET static inline struct neighbour *__ipv4_neigh_lookup_noref (struct net_device *dev,u32 key) { +struct neigh_table *tbl =arp_table (dev_net (dev)); +if (dev->flags & (IFF_LOOPBACK |IFF_POINTOPOINT )) key =INADDR_ANY ; -return ___neigh_lookup_noref (&arp_tbl, neigh_key_eq32, arp_hashfn, &key, dev); +return ___neigh_lookup_noref (tbl, neigh_key_eq32, arp_hashfn, &key, dev); }#else static inline
include/net/route.h +5 -2
@@ -405,8 +405,11 @@ static inline struct neighbour *ip_neigh_gw4(struct net_device *dev,struct neighbour *neigh; neigh =__ipv4_neigh_lookup_noref (dev, (__force u32 )daddr); -if (unlikely (!neigh)) - neigh =__neigh_create (&arp_tbl, &daddr, dev, false); +if (unlikely (!neigh)) { +struct neigh_table *tbl =arp_table (dev_net (dev)); + + neigh =__neigh_create (tbl, &daddr, dev, false); + }return neigh; }
net/bridge/br_arp_nd_proxy.c +1 -1
@@ -193,7 +193,7 @@ void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,return ; } - n =neigh_lookup (&arp_tbl, &tip, vlandev); + n =neigh_lookup (arp_table (dev_net (vlandev)), &tip, vlandev);if (n) {struct net_bridge_fdb_entry *f;u8 ha[ETH_ALEN ]__aligned(2) ;
net/ipv4/arp.c +52 -36
255 lines not on this page · raw message
net/ipv4/devinet.c +10 -8
@@ -265,7 +265,9 @@ EXPORT_SYMBOL(in_dev_finish_destroy);static struct in_device *inetdev_init (struct net_device *dev) { +struct net *net =dev_net (dev);struct in_device *in_dev; +struct neigh_table *tbl;int err = -ENOMEM ;ASSERT_RTNL ();
@@ -273,11 +275,12 @@ static struct in_device *inetdev_init(struct net_device *dev) in_dev =kzalloc_obj (*in_dev);if (!in_dev) goto out; -memcpy (&in_dev->cnf,dev_net (dev)->ipv4.devconf_dflt, -sizeof (in_dev->cnf)); + + tbl =arp_table (net); +memcpy (&in_dev->cnf, net->ipv4.devconf_dflt,sizeof (in_dev->cnf)); in_dev->cnf.sysctl =NULL ; in_dev->dev = dev; - in_dev->arp_parms =neigh_parms_alloc (dev, &arp_tbl); + in_dev->arp_parms =neigh_parms_alloc (dev, tbl);if (!in_dev->arp_parms) goto out_kfree;if (IPV4_DEVCONF (in_dev->cnf,FORWARDING ))
@@ -291,7 +294,7 @@ static struct in_device *inetdev_init(struct net_device *dev) err =devinet_sysctl_register (in_dev);if (err) { in_dev->dead =1 ; -neigh_parms_release (&arp_tbl, in_dev->arp_parms); +neigh_parms_release (tbl, in_dev->arp_parms);in_dev_put (in_dev); in_dev =NULL ; goto out;
@@ -313,13 +316,12 @@ static struct in_device *inetdev_init(struct net_device *dev)static void inetdev_destroy (struct in_device *in_dev) { -struct net_device *dev; +struct net_device *dev = in_dev->dev; +struct net *net =dev_net (dev);struct in_ifaddr *ifa;ASSERT_RTNL (); - dev = in_dev->dev; - in_dev->dead =1 ;RCU_INIT_POINTER (dev->ip_ptr,NULL );
@@ -332,7 +334,7 @@ static void inetdev_destroy(struct in_device *in_dev) }devinet_sysctl_unregister (in_dev); -neigh_parms_release (&arp_tbl, in_dev->arp_parms); +neigh_parms_release (arp_table (net), in_dev->arp_parms);arp_ifdown (dev);in_dev_put (in_dev);
net/ipv4/fib_semantics.c +3 -2
@@ -610,11 +610,12 @@ static int fib_detect_death(struct fib_info *fi, int order, int dflt) {const struct fib_nh_common *nhc =fib_info_nhc (fi,0 ); -struct neighbour *n; +struct net *net = fi->fib_net;int state =NUD_NONE ; +struct neighbour *n;if (likely (nhc->nhc_gw_family ==AF_INET )) - n =neigh_lookup (&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev); + n =neigh_lookup (arp_table (net), &nhc->nhc_gw.ipv4, nhc->nhc_dev);else if (IS_ENABLED (CONFIG_IPV6 ) && nhc->nhc_gw_family ==AF_INET6 ) n =neigh_lookup (&nd_tbl, &nhc->nhc_gw.ipv6, nhc->nhc_dev);else
net/ipv4/route.c +1 -1
@@ -817,7 +817,7 @@ static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flow n =__ipv4_neigh_lookup (rt->dst.dev, (__force u32 )new_gw);if (!n) - n =neigh_create (&arp_tbl, &new_gw, rt->dst.dev); + n =neigh_create (arp_table (net), &new_gw, rt->dst.dev);if (!IS_ERR (n)) {if (!(READ_ONCE (n->nud_state) &NUD_VALID )) {neigh_event_send (n,NULL );
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
[email protected]
AI reviewRe: [PATCH v6 net-next 08/15] ipv4: Replace &arp_tbl with arp_table(net).
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Critical: 0 · High: 1 · Medium: 0 (1) · Low: 2
- [High] mlx5e_tc_update_neigh_used_value() (drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c) now…
- [Low] Documentation/networking/switchdev.rst still instructs switchdev driver authors to resolve nexthops against, and monitor updates to,…
- [Low] The patch introduces new reads of the RCU-protected netns pointer dev->nd_net via the non-RCU accessor dev_net() in paths that run only…
Pre-existing issues:
- [Medium] The neighbour fallback path in mlx5e_ipsec_init_macs() (drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c, label…
--
ipv4: Replace &arp_tbl with arp_table(net).
In preparation for per-netns neighbour tables, this converts all users of the global arp_tbl to the arp_table(net) accessor, makes arp_tbl static in net/ipv4/arp.c and drops its EXPORT_SYMBOL. It also removes some driver checks that compared n->tbl against &arp_tbl and &nd_tbl.
A few questions below.
Quoted 26 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +0
-10@@ -393,20 +393,10 @@ void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)struct mlx5e_encap_entry *e =NULL ;struct mlx5e_tc_flow *flow;struct mlx5_fc *counter; -struct neigh_table *tbl;bool neigh_used = false;struct neighbour *n;u64 lastuse; -if (m_neigh->family ==AF_INET ) - tbl = &arp_tbl; -#if IS_ENABLED (CONFIG_IPV6 ) - else if (m_neigh->family == AF_INET6) - tbl = &nd_tbl; -#endif - else - return; -/* mlx5e_get_next_valid_encap() releases previous encap before returning * next one. */
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe) · line 442
Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +3
-0@@ -447,12 +437,23 @@ void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)trace_mlx5e_tc_update_neigh_used_value (nhe, neigh_used);if (neigh_used) { +struct net_device *dev =READ_ONCE (nhe->neigh_dev); +struct net *net =dev_net (dev); +struct neigh_table *tbl;
[Severity: High]
Is nhe->neigh_dev safe to dereference here? As far as I can tell no reference is taken on it. It is only stored:
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c:mlx5e_rep_neigh_entry_create() {
...
WRITE_ONCE((*nhe)->neigh_dev, neigh_dev);
...
}and there is no netdev_hold()/dev_hold() for it anywhere, and no NETDEV_UNREGISTER handling in the mlx5 rep neigh code that would clear it.
The rest of the driver only ever compares the field, never dereferences it:
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c:mlx5e_rep_neigh_update() {
...
same_dev = READ_ONCE(nhe->neigh_dev) == n->dev;
...
}Before this patch the pointer was only handed to neigh_lookup(), which never loads from the netdev (arp_hashfn() uses hash32_ptr(dev) and the lookup only compares n->dev == dev), so a stale pointer was harmless. Now dev_net(dev) reads dev->nd_net and the result is dereferenced again by arp_table(net) as net->neigh_tables[NEIGH_ARP_TABLE].
If the cached route egress device (for example a vlan or bond on top of the uplink) was unregistered and freed while the nhe is still alive, does this read freed memory and then index a garbage struct net? The reachable path is the periodic work:
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c:mlx5e_rep_neigh_stats_work() {
...
while ((nhe = mlx5e_get_next_nhe(rpriv, nhe)) != NULL)
mlx5e_tc_update_neigh_used_value(nhe);
...
}and rtnl_lock() held there does not keep an already-unregistered netdev alive, since free_netdev() runs from netdev_run_todo() after rtnl_unlock().
Also, note 3 in the changelog says:
"3. mlx5e_tc_update_neigh_used_value() dereferences nhe->neigh_dev
in trace_mlx5e_tc_update_neigh_used_value()."
Does that justification hold? The tracepoint dereference lives in TP_fast_assign():
drivers/net/ethernet/mellanox/mlx5/core/diag/en_tc_tracepoint.h:
TP_STRUCT__entry(__string(devname, nhe->neigh_dev->name)
which only runs when the tracepoint is enabled, so it does not show the pointer is safe to dereference unconditionally.
Would deriving the netns from a refcounted device such as e->out_dev or priv->netdev work instead?
Quoted 24 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +8
-1nhe->reported_lastuse = jiffies; +#if IS_ENABLED (CONFIG_IPV6 ) +if (m_neigh->family !=AF_INET ) + tbl = &nd_tbl; + else +#endif + tbl =arp_table (net); +/* find the relevant neigh according to the cached device and * dst ip pair */ - n =neigh_lookup (tbl, &m_neigh->dst_ip,READ_ONCE (nhe->neigh_dev)); + n =neigh_lookup (tbl, &m_neigh->dst_ip, dev);if (!n)return ;drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c +0
-0@@ -364,9 +365,10 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry,return ;
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry, · line 374
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c +3
-2neigh: - n =neigh_lookup (&arp_tbl, pkey, netdev); + tbl =arp_table (dev_net (netdev)); + n =neigh_lookup (tbl, pkey, netdev);if (!n) { - n =neigh_create (&arp_tbl, pkey, netdev); + n =neigh_create (tbl, pkey, netdev);if (IS_ERR (n))return ;neigh_event_send (n,NULL );
[Severity: Medium]
This isn't a bug introduced by this patch, since the pre-patch code already used &arp_tbl unconditionally here, but should this fallback pick the table based on addrs->family?
The AF_INET6 branch above can reach this label with a 16-byte key:
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c:mlx5e_ipsec_init_macs() {
...
pkey = &addrs->saddr.a6;
...
case AF_INET6:
if (!IS_ENABLED(CONFIG_IPV6) ||
ip6_dst_lookup(dev_net(netdev), NULL, &rt_dst_entry, &fl6))
goto neigh;
...
}and the ARP table has .family = AF_INET and .key_len = 4, so the lookup hashes and compares only the first 4 bytes of the IPv6 address. Can neigh_create() then install a bogus ARP entry and send an ARP request for an address made from those 4 bytes, leaving the IPv6 packet-offload SA with attrs->drop set or an unrelated MAC? A separate fix using the netns ND table for AF_INET6 seems needed.
Quoted 14 lines
drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c +2
-1@@ -235,7 +236,8 @@ void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb)if (!netdev)continue ; - n =neigh_lookup (&arp_tbl, &ipv4_addr, netdev); + tbl =arp_table (dev_net (netdev)); + n =neigh_lookup (tbl, &ipv4_addr, netdev);if (!n)continue ;
[ ... ]
Quoted 15 lines
drivers/net/vxlan/vxlan_core.c +2
-1@@ -1841,11 +1841,12 @@ static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)static int arp_reduce (struct net_device *dev,struct sk_buff *skb,__be32 vni) { +struct neigh_table *tbl =arp_table (dev_net (dev));struct vxlan_dev *vxlan =netdev_priv (dev); +struct neighbour *n;struct arphdr *parp;u8 *arpptr, *sha;__be32 sip, tip; -struct neighbour *n;
[ ... ]
Quoted 15 lines
include/net/arp.h +0
-2@@ -7,8 +7,6 @@#include <linux/hash.h> #include <net/neighbour.h> -extern struct neigh_table arp_tbl; -static inline struct neigh_table *arp_table (struct net *net) {return net->neigh_tables[NEIGH_ARP_TABLE ];@@ -25,10 +23,12 @@ static inline u32 arp_hashfn(const void *pkey, const struct net_device *dev, u32#ifdef CONFIG_INET
include/net/arp.h static inline u32 arp_hashfn(const void *pkey, const struct net_device *dev, u32 · line 32
Quoted 10 lines
include/net/arp.h +3
-1static inline struct neighbour *__ipv4_neigh_lookup_noref (struct net_device *dev,u32 key) { +struct neigh_table *tbl =arp_table (dev_net (dev)); +if (dev->flags & (IFF_LOOPBACK |IFF_POINTOPOINT )) key =INADDR_ANY ; -return ___neigh_lookup_noref (&arp_tbl, neigh_key_eq32, arp_hashfn, &key, dev); +return ___neigh_lookup_noref (tbl, neigh_key_eq32, arp_hashfn, &key, dev); }
[Severity: Low]
Should these new netns reads use dev_net_rcu() instead of dev_net()?
possible_net_t holds a struct net __rcu *net published with rcu_assign_pointer() via write_pnet(), and the two accessors differ:
include/net/net_namespace.h:
static inline struct net *read_pnet(const possible_net_t *pnet)
{
return rcu_dereference_protected(pnet->net, true);
}
...
static inline struct net *read_pnet_rcu(const possible_net_t *pnet)
{
return rcu_dereference(pnet->net);
}dev_net() goes through read_pnet(), which asserts unconditionally that the caller holds RTNL or a device reference, so it silently turns off lockdep/sparse RCU checking on these new dereferences.
The sites added by this patch that run under rcu_read_lock() only are __ipv4_neigh_lookup_noref() and ip_neigh_gw4() (reached from ip_finish_output2()), br_do_proxy_suppress_arp(), the vxlan arp_reduce()/route_shortcircuit() xmit paths, and nfp_tunnel_keep_alive(), which is called inside its own rcu_read_lock(). None of them held RTNL or a netns reference before, and none of them read dev->nd_net at all before this patch.
The dev_net() uses in inetdev_init(), inetdev_destroy() and arp_ifdown() look fine since RTNL is held there.
Quoted 45 lines
include/net/route.h +5
-2@@ -405,8 +405,11 @@ static inline struct neighbour *ip_neigh_gw4(struct net_device *dev,struct neighbour *neigh; neigh =__ipv4_neigh_lookup_noref (dev, (__force u32 )daddr); -if (unlikely (!neigh)) - neigh =__neigh_create (&arp_tbl, &daddr, dev, false); +if (unlikely (!neigh)) { +struct neigh_table *tbl =arp_table (dev_net (dev)); + + neigh =__neigh_create (tbl, &daddr, dev, false); + }return neigh; }net/bridge/br_arp_nd_proxy.c +1
-1@@ -193,7 +193,7 @@ void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,return ; } - n =neigh_lookup (&arp_tbl, &tip, vlandev); + n =neigh_lookup (arp_table (dev_net (vlandev)), &tip, vlandev);if (n) {struct net_bridge_fdb_entry *f;u8 ha[ETH_ALEN ]__aligned(2) ;net/ipv4/arp.c +1
-1@@ -149,7 +149,7 @@ static const struct neigh_ops arp_direct_ops = { .connected_output = neigh_direct_output, }; -struct neigh_table arp_tbl = { +static struct neigh_table arp_tbl = { .family =AF_INET , .key_len =4 , .protocol =cpu_to_be16 (ETH_P_IP ),
net/ipv4/arp.c struct neigh_table arp_tbl = { · line 185
Quoted 4 lines
net/ipv4/arp.c +0
-1@@ -182,7 +182,6 @@ struct neigh_table arp_tbl = { .gc_thresh2 = 512, .gc_thresh3 = 1024, }; -EXPORT_SYMBOL (arp_tbl);
[Severity: Low]
Should Documentation/networking/switchdev.rst be updated in this patch too?
Its Nexthop Resolution section still tells switchdev driver authors to use the symbol that becomes file-local here, in four places:
"address discovery comes via the ARP (or ND) process and is available via
the arp_tbl neighbor table."
"The driver can monitor for updates to arp_tbl using the netevent notifier
NETEVENT_NEIGH_UPDATE. ... The driver implements ndo_neigh_destroy to know when arp_tbl neighbor entries are purged from the port."
At this commit, git grep arp_tbl matches only net/ipv4/arp.c and Documentation/networking/switchdev.rst, and the doc points at rocker as the reference implementation while rocker itself is converted to arp_table(&init_net) here. The doc also never mentions arp_table(net) or the new requirement to pick the table belonging to the device's netns (or init_net for netns-immutable devices). The text is still unchanged at the end of the series (d4be111038a59be6217f9b018ec033041ab7bf2a).
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
Re: [PATCH v6 net-next 08/15] ipv4: Replace &arp_tbl with arp_table(net).
Quoted 8 lines · On Thu, Sep 3, 2026 at 8:10 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Critical: 0 · High: 1 · Medium: 0 (1) · Low: 2
- [High]
mlx5e_tc_update_neigh_used_value()(drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c) now…
This is a pre-existing issue as mentioned in the commit message.
3. mlx5e_tc_update_neigh_used_value() dereferences nhe->neigh_dev in trace_mlx5e_tc_update_neigh_used_value().
Quoted 4 lines
- [Low] estill instructs switchdev
driver authors to resolve nexthops against, and monitor updates to,…
- [Low] The patch introduces new reads of the RCU-protected netns pointerdev->nd_netvia the non-RCU accessordev_net()in paths that run only…
I'll follow up on these if there are no other critical issues.
Quoted 336 lines
Pre-existing issues:
- [Medium] The neighbour fallback path inmlx5e_ipsec_init_macs()(drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c, label…--
ipv4: Replace
&arp_tblwitharp_table(net).In preparation for per-netns neighbour tables, this converts all users of the global
arp_tblto thearp_table(net)accessor, makesarp_tblstatic innet/ipv4/arp.cand drops itsEXPORT_SYMBOL. It also removes some driver checks that comparedn->tblagainst&arp_tbland&nd_tbl.A few questions below.
Quoted 26 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +0
-10@@ -393,20 +393,10 @@ void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)struct mlx5e_encap_entry *e =NULL ;struct mlx5e_tc_flow *flow;struct mlx5_fc *counter; -struct neigh_table *tbl;bool neigh_used = false;struct neighbour *n;u64 lastuse; -if (m_neigh->family ==AF_INET ) - tbl = &arp_tbl; -#if IS_ENABLED (CONFIG_IPV6 ) - else if (m_neigh->family == AF_INET6) - tbl = &nd_tbl; -#endif - else - return; -/* mlx5e_get_next_valid_encap() releases previous encap before returning * next one. */ drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe) · line 442
Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +3
-0@@ -447,12 +437,23 @@ void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)trace_mlx5e_tc_update_neigh_used_value (nhe, neigh_used);if (neigh_used) { +struct net_device *dev =READ_ONCE (nhe->neigh_dev); +struct net *net =dev_net (dev); +struct neigh_table *tbl;[Severity: High]
Isnhe->neigh_devsafe to dereference here? As far as I can tell no reference is taken on it. It is only stored:drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c:mlx5e_rep_neigh_entry_create() { ... WRITE_ONCE((*nhe)->neigh_dev, neigh_dev); ... }and there is no netdev_hold()/dev_hold() for it anywhere, and no
NETDEV_UNREGISTERhandling in the mlx5 rep neigh code that would clear it.The rest of the driver only ever compares the field, never dereferences it:
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c:mlx5e_rep_neigh_update() { ... same_dev = READ_ONCE(nhe->neigh_dev) == n->dev; ... }Before this patch the pointer was only handed to
neigh_lookup(), which never loads from the netdev (arp_hashfn()useshash32_ptr(dev)and the lookup only comparesn->dev== dev), so a stale pointer was harmless. Nowdev_net(dev)readsdev->nd_netand the result is dereferenced again byarp_table(net)as net->neigh_tables[NEIGH_ARP_TABLE].If the cached route egress device (for example a vlan or bond on top of the uplink) was unregistered and freed while the nhe is still alive, does this read freed memory and then index a garbage struct net? The reachable path is the periodic work:
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c:mlx5e_rep_neigh_stats_work() { ... while ((nhe = mlx5e_get_next_nhe(rpriv, nhe)) != NULL) mlx5e_tc_update_neigh_used_value(nhe); ... }and
rtnl_lock()held there does not keep an already-unregistered netdev alive, sincefree_netdev()runs fromnetdev_run_todo()afterrtnl_unlock().Also, note 3 in the changelog says:
"3.
mlx5e_tc_update_neigh_used_value()dereferencesnhe->neigh_dev
intrace_mlx5e_tc_update_neigh_used_value()."Does that justification hold? The tracepoint dereference lives in
TP_fast_assign():
drivers/net/ethernet/mellanox/mlx5/core/diag/en_tc_tracepoint.h:TP_STRUCT__entry(__string(devname, nhe->neigh_dev->name)which only runs when the tracepoint is enabled, so it does not show the pointer is safe to dereference unconditionally.
Would deriving the netns from a refcounted device such as
e->out_devorpriv->netdevwork instead?Quoted 24 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +8
-1nhe->reported_lastuse = jiffies; +#if IS_ENABLED (CONFIG_IPV6 ) +if (m_neigh->family !=AF_INET ) + tbl = &nd_tbl; + else +#endif + tbl =arp_table (net); +/* find the relevant neigh according to the cached device and * dst ip pair */ - n =neigh_lookup (tbl, &m_neigh->dst_ip,READ_ONCE (nhe->neigh_dev)); + n =neigh_lookup (tbl, &m_neigh->dst_ip, dev);if (!n)return ;drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c +0
-0@@ -364,9 +365,10 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry,return ;drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry, · line 374
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c +3
-2neigh: - n =neigh_lookup (&arp_tbl, pkey, netdev); + tbl =arp_table (dev_net (netdev)); + n =neigh_lookup (tbl, pkey, netdev);if (!n) { - n =neigh_create (&arp_tbl, pkey, netdev); + n =neigh_create (tbl, pkey, netdev);if (IS_ERR (n))return ;neigh_event_send (n,NULL );[Severity: Medium]
This isn't a bug introduced by this patch, since the pre-patch code already used&arp_tblunconditionally here, but should this fallback pick the table based onaddrs->family?The
AF_INET6branch above can reach this label with a 16-byte key:drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c:mlx5e_ipsec_init_macs() { ... pkey = &addrs->saddr.a6; ... case AF_INET6: if (!IS_ENABLED(CONFIG_IPV6) || ip6_dst_lookup(dev_net(netdev), NULL, &rt_dst_entry, &fl6)) goto neigh; ... }and the ARP table has .family =
AF_INETand .key_len = 4, so the lookup hashes and compares only the first 4 bytes of the IPv6 address. Canneigh_create()then install a bogus ARP entry and send an ARP request for an address made from those 4 bytes, leaving the IPv6 packet-offload SA withattrs->dropset or an unrelated MAC? A separate fix using the netns ND table forAF_INET6seems needed.Quoted 14 lines
drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c +2
-1@@ -235,7 +236,8 @@ void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb)if (!netdev)continue ; - n =neigh_lookup (&arp_tbl, &ipv4_addr, netdev); + tbl =arp_table (dev_net (netdev)); + n =neigh_lookup (tbl, &ipv4_addr, netdev);if (!n)continue ;[ ... ]
Quoted 15 lines
drivers/net/vxlan/vxlan_core.c +2
-1@@ -1841,11 +1841,12 @@ static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)static int arp_reduce (struct net_device *dev,struct sk_buff *skb,__be32 vni) { +struct neigh_table *tbl =arp_table (dev_net (dev));struct vxlan_dev *vxlan =netdev_priv (dev); +struct neighbour *n;struct arphdr *parp;u8 *arpptr, *sha;__be32 sip, tip; -struct neighbour *n;[ ... ]
Quoted 15 lines
include/net/arp.h +0
-2@@ -7,8 +7,6 @@#include <linux/hash.h> #include <net/neighbour.h> -extern struct neigh_table arp_tbl; -static inline struct neigh_table *arp_table (struct net *net) {return net->neigh_tables[NEIGH_ARP_TABLE ];@@ -25,10 +23,12 @@ static inline u32 arp_hashfn(const void *pkey, const struct net_device *dev, u32#ifdef CONFIG_INET include/net/arp.h static inline u32 arp_hashfn(const void *pkey, const struct net_device *dev, u32 · line 32
Quoted 10 lines
include/net/arp.h +3
-1static inline struct neighbour *__ipv4_neigh_lookup_noref (struct net_device *dev,u32 key) { +struct neigh_table *tbl =arp_table (dev_net (dev)); +if (dev->flags & (IFF_LOOPBACK |IFF_POINTOPOINT )) key =INADDR_ANY ; -return ___neigh_lookup_noref (&arp_tbl, neigh_key_eq32, arp_hashfn, &key, dev); +return ___neigh_lookup_noref (tbl, neigh_key_eq32, arp_hashfn, &key, dev); }[Severity: Low]
Should these new netns reads usedev_net_rcu()instead ofdev_net()?
possible_net_tholds a struct net__rcu*net published withrcu_assign_pointer()viawrite_pnet(), and the two accessors differ:
include/net/net_namespace.h:static inline struct net *read_pnet(const possible_net_t *pnet) { return rcu_dereference_protected(pnet->net, true); } ... static inline struct net *read_pnet_rcu(const possible_net_t *pnet) { return rcu_dereference(pnet->net); }
dev_net()goes throughread_pnet(), which asserts unconditionally that the caller holds RTNL or a device reference, so it silently turns off lockdep/sparse RCU checking on these new dereferences.The sites added by this patch that run under
rcu_read_lock()only are__ipv4_neigh_lookup_noref()andip_neigh_gw4()(reached fromip_finish_output2()),br_do_proxy_suppress_arp(), the vxlan arp_reduce()/route_shortcircuit() xmit paths, andnfp_tunnel_keep_alive(), which is called inside its ownrcu_read_lock(). None of them held RTNL or a netns reference before, and none of them readdev->nd_netat all before this patch.The
dev_net()uses ininetdev_init(),inetdev_destroy()andarp_ifdown()look fine since RTNL is held there.Quoted 45 lines
include/net/route.h +5
-2@@ -405,8 +405,11 @@ static inline struct neighbour *ip_neigh_gw4(struct net_device *dev,struct neighbour *neigh; neigh =__ipv4_neigh_lookup_noref (dev, (__force u32 )daddr); -if (unlikely (!neigh)) - neigh =__neigh_create (&arp_tbl, &daddr, dev, false); +if (unlikely (!neigh)) { +struct neigh_table *tbl =arp_table (dev_net (dev)); + + neigh =__neigh_create (tbl, &daddr, dev, false); + }return neigh; }net/bridge/br_arp_nd_proxy.c +1
-1@@ -193,7 +193,7 @@ void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,return ; } - n =neigh_lookup (&arp_tbl, &tip, vlandev); + n =neigh_lookup (arp_table (dev_net (vlandev)), &tip, vlandev);if (n) {struct net_bridge_fdb_entry *f;u8 ha[ETH_ALEN ]__aligned(2) ;net/ipv4/arp.c +1
-1@@ -149,7 +149,7 @@ static const struct neigh_ops arp_direct_ops = { .connected_output = neigh_direct_output, }; -struct neigh_table arp_tbl = { +static struct neigh_table arp_tbl = { .family =AF_INET , .key_len =4 , .protocol =cpu_to_be16 (ETH_P_IP ),net/ipv4/arp.c struct neigh_table arp_tbl = { · line 185
Quoted 4 lines
net/ipv4/arp.c +0
-1@@ -182,7 +182,6 @@ struct neigh_table arp_tbl = { .gc_thresh2 = 512, .gc_thresh3 = 1024, }; -EXPORT_SYMBOL (arp_tbl);[Severity: Low]
ShouldDocumentation/networking/switchdev.rstbe updated in this patch too?Its Nexthop Resolution section still tells switchdev driver authors to use the symbol that becomes file-local here, in four places:
"address discovery comes via the ARP (or ND) process and is available via
thearp_tblneighbor table.""The driver can monitor for updates to
arp_tblusing the netevent notifier
NETEVENT_NEIGH_UPDATE. ... The driver implementsndo_neigh_destroyto know whenarp_tblneighbor entries are purged from the port."At this commit, git grep
arp_tblmatches onlynet/ipv4/arp.candDocumentation/networking/switchdev.rst, and the doc points at rocker as the reference implementation while rocker itself is converted toarp_table(&init_net)here. The doc also never mentionsarp_table(net)or the new requirement to pick the table belonging to the device's netns (orinit_netfor netns-immutable devices). The text is still unchanged at the end of the series (d4be111038a59be6217f9b018ec033041ab7bf2a).[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
PATCH v6 9/15 ipv6: Replace &nd_tbl with nd_table(net).
In reply to Kuniyuki IwashimaWe will allocate per-netns neigh_table in net->neigh_tables[].
Let's replace &nd_tbl with nd_table(net).
---
Cc: Saeed Mahameed <[email protected]>
Cc: Leon Romanovsky <[email protected]>
Cc: Tariq Toukan <[email protected]>
Cc: Mark Bloch <[email protected]>
Cc: Petr Machata <[email protected]>
Cc: Edward Cree <[email protected]>
Cc: Alexander Aring <[email protected]>
Cc: Stefan Schmidt <[email protected]>
Cc: Miquel Raynal <[email protected]>
--- .../mellanox/mlx5/core/en/rep/neigh.c | 7 +- .../mellanox/mlx5/core/en/tc_tun_encap.c | 2 +- .../ethernet/mellanox/mlxsw/spectrum_router.c | 13 ++- .../ethernet/mellanox/mlxsw/spectrum_span.c | 5 +- .../netronome/nfp/flower/tunnel_conf.c | 4 +- drivers/net/ethernet/sfc/tc_counters.c | 2 +- drivers/net/ethernet/sfc/tc_encap_actions.c | 2 +- drivers/net/vrf.c | 2 +- drivers/net/vxlan/vxlan_core.c | 6 +- include/net/ndisc.h | 11 ++- net/bridge/br_arp_nd_proxy.c | 2 +- net/ieee802154/6lowpan/tx.c | 3 +- net/ipv4/fib_semantics.c | 2 +- net/ipv6/addrconf.c | 17 ++-- net/ipv6/ip6_output.c | 4 +- net/ipv6/ndisc.c | 91 ++++++++++--------- net/ipv6/route.c | 18 ++-- 17 files changed, 112 insertions(+), 79 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c +4 -3
@@ -17,26 +17,27 @@#include "fs_core.h" #include "diag/en_rep_tracepoint.h" -static unsigned long mlx5e_rep_ipv6_interval (void ) +static unsigned long mlx5e_rep_ipv6_interval (struct net *net) {if (IS_ENABLED (CONFIG_IPV6 ) &&ipv6_mod_enabled ()) -return NEIGH_VAR (&nd_tbl.parms,DELAY_PROBE_TIME ); +return NEIGH_VAR (&nd_table (net)->parms,DELAY_PROBE_TIME );return ~0UL ; }static void mlx5e_rep_neigh_update_init_interval (struct mlx5e_rep_priv *rpriv) { -unsigned long ipv6_interval =mlx5e_rep_ipv6_interval ();struct net_device *netdev = rpriv->netdev;struct net *net =dev_net (netdev);unsigned long ipv4_interval; +unsigned long ipv6_interval;struct neigh_table *tbl;struct mlx5e_priv *priv; priv =netdev_priv (netdev); tbl =arp_table (net); ipv4_interval =NEIGH_VAR (&tbl->parms,DELAY_PROBE_TIME ); + ipv6_interval =mlx5e_rep_ipv6_interval (net); rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);mlx5_fc_update_sampling_interval (priv->mdev, rpriv->neigh_update.min_interval);
drivers/net/ethernet/mellanox/mlx5/core/en/tc_tun_encap.c +1 -1
@@ -445,7 +445,7 @@ void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)#if IS_ENABLED (CONFIG_IPV6 )if (m_neigh->family !=AF_INET ) - tbl = &nd_tbl; + tbl =nd_table (net); else#endif tbl =arp_table (net);
drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c +8 -5
@@ -2403,7 +2403,7 @@ mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)#if IS_ENABLED (CONFIG_IPV6 ) interval = min_t(unsigned long,NEIGH_VAR (&arp_table (net)->parms,DELAY_PROBE_TIME ), -NEIGH_VAR (&nd_tbl.parms,DELAY_PROBE_TIME )); +NEIGH_VAR (&nd_table (net)->parms,DELAY_PROBE_TIME ));#else interval =NEIGH_VAR (&arp_table (net)->parms,DELAY_PROBE_TIME );#endif
@@ -2449,6 +2449,8 @@ static void mlxsw_sp_router_neigh_ent_ipv6_process(struct mlxsw_sp *mlxsw_sp, char *rauhtd_pl, int rec_index) { +struct net *net =mlxsw_sp_net (mlxsw_sp); +struct neigh_table *tbl;struct net_device *dev;struct neighbour *n;struct in6_addr dip;
@@ -2462,8 +2464,9 @@ static void mlxsw_sp_router_neigh_ent_ipv6_process(struct mlxsw_sp *mlxsw_sp,return ; } + tbl =nd_table (net); dev =mlxsw_sp_rif_dev (mlxsw_sp->router->rifs[rif]); - n =neigh_lookup (&nd_tbl, &dip, dev); + n =neigh_lookup (tbl, &dip, dev);if (!n)return ;
@@ -3028,7 +3031,7 @@ static int mlxsw_sp_neigh_rif_made_sync(struct mlxsw_sp *mlxsw_sp, goto err_arp;#if IS_ENABLED (CONFIG_IPV6 ) -neigh_for_each (&nd_tbl, mlxsw_sp_neigh_rif_made_sync_each, &rms); +neigh_for_each (nd_table (net), mlxsw_sp_neigh_rif_made_sync_each, &rms);#endif if (rms.err) goto err_nd;
@@ -5131,7 +5134,7 @@ mlxsw_sp_nexthop_obj_init(struct mlxsw_sp *mlxsw_sp,case AF_INET6 :memcpy (&nh->gw_addr, &nh_obj->ipv6,sizeof (nh_obj->ipv6));#if IS_ENABLED (CONFIG_IPV6 ) - nh->neigh_tbl = &nd_tbl; + nh->neigh_tbl =nd_table (net);#endif break ; }
@@ -6987,7 +6990,7 @@ static int mlxsw_sp_nexthop6_init(struct mlxsw_sp *mlxsw_sp, nh->nh_weight = rt->fib6_nh->fib_nh_weight;memcpy (&nh->gw_addr, &rt->fib6_nh->fib_nh_gw6,sizeof (nh->gw_addr));#if IS_ENABLED (CONFIG_IPV6 ) - nh->neigh_tbl = &nd_tbl; + nh->neigh_tbl =nd_table (mlxsw_sp_net (mlxsw_sp));#endif err =mlxsw_sp_nexthop_counter_enable (mlxsw_sp, nh);
drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c +4 -1
@@ -564,6 +564,7 @@ mlxsw_sp_span_entry_gretap6_parms(struct mlxsw_sp *mlxsw_sp,union mlxsw_sp_l3addr daddr = { .addr6 = tparm.raddr };bool inherit_ttl = !tparm.hop_limit;union mlxsw_sp_l3addr gw = daddr; +struct neigh_table *tbl =NULL ;struct net_device *l3edev; if (!(to_dev->flags & IFF_UP) ||
@@ -577,9 +578,11 @@ mlxsw_sp_span_entry_gretap6_parms(struct mlxsw_sp *mlxsw_sp,return mlxsw_sp_span_entry_unoffloadable (sparmsp); l3edev =mlxsw_sp_span_gretap6_route (to_dev, &saddr.addr6, &gw.addr6); +if (l3edev) + tbl =nd_table (dev_net (l3edev));return mlxsw_sp_span_entry_tunnel_parms_common (l3edev, saddr, daddr, gw, tparm.hop_limit, - &nd_tbl, sparmsp); + tbl, sparmsp); } static int
drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c +3 -1
@@ -253,6 +253,7 @@ void nfp_tunnel_keep_alive_v6(struct nfp_app *app, struct sk_buff *skb)#if IS_ENABLED (CONFIG_IPV6 )struct nfp_tun_active_tuns_v6 *payload;struct net_device *netdev; +struct neigh_table *tbl;int count, i, pay_len;struct neighbour *n;void *ipv6_add;
@@ -279,7 +280,8 @@ void nfp_tunnel_keep_alive_v6(struct nfp_app *app, struct sk_buff *skb)if (!netdev)continue ; - n =neigh_lookup (&nd_tbl, ipv6_add, netdev); + tbl =nd_table (dev_net (netdev)); + n =neigh_lookup (tbl, ipv6_add, netdev);if (!n)continue ;
drivers/net/ethernet/sfc/tc_counters.c +1 -1
@@ -116,7 +116,7 @@ static void efx_tc_counter_work(struct work_struct *work) encap->neigh->egdev); else #if IS_ENABLED(CONFIG_IPV6) - n =neigh_lookup (&nd_tbl, + n =neigh_lookup (nd_table (net), &encap->neigh->dst_ip6, encap->neigh->egdev);#else
drivers/net/ethernet/sfc/tc_encap_actions.c +1 -1
@@ -530,7 +530,7 @@ static int efx_neigh_event(struct efx_nic *efx, struct neighbour *n)if (n->tbl->family ==AF_INET ) { keysize =sizeof (keys.dst_ip);#if IS_ENABLED (CONFIG_IPV6 ) - } else if (n->tbl == &nd_tbl) { + } else if (n->tbl->family == AF_INET6) { ipv6 = true; keysize =sizeof (keys.dst_ip6);#endif
drivers/net/vrf.c +1 -1
@@ -616,7 +616,7 @@ static int vrf_finish_output6(struct net *net, struct sock *sk, nexthop =rt6_nexthop (dst_rt6_info (dst), &ipv6_hdr (skb)->daddr); neigh =__ipv6_neigh_lookup_noref (dst->dev, nexthop);if (unlikely (!neigh)) - neigh =__neigh_create (&nd_tbl, nexthop, dst->dev, false); + neigh =__neigh_create (nd_table (net), nexthop, dst->dev, false);if (!IS_ERR (neigh)) {sock_confirm_neigh (skb, neigh); ret =neigh_output (neigh, skb, false);
drivers/net/vxlan/vxlan_core.c +4 -2
@@ -2052,7 +2052,7 @@ static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni) ipv6_addr_is_multicast(&msg->target)) goto out; - n =neigh_lookup (&nd_tbl, &msg->target, dev); + n =neigh_lookup (nd_table (dev_net (dev)), &msg->target, dev);if (n) {struct vxlan_rdst *rdst =NULL ;
@@ -2149,8 +2149,10 @@ static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)return false;if (!pskb_network_may_pull (skb,sizeof (struct ipv6hdr )))return false; + + tbl =nd_table (dev_net (dev)); pip6 =ipv6_hdr (skb); - n =neigh_lookup (&nd_tbl, &pip6->daddr, dev); + n =neigh_lookup (tbl, &pip6->daddr, dev);if (!n && (vxlan->cfg.flags &VXLAN_F_L3MISS )) {union vxlan_addr ipa = { .sin6.sin6_addr = pip6->daddr,
include/net/ndisc.h +8 -3
@@ -363,7 +363,9 @@ static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, _static inline struct neighbour *__ipv6_neigh_lookup_noref (struct net_device *dev,const void *pkey) { -return ___neigh_lookup_noref (&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev); +struct neigh_table *tbl =nd_table (dev_net (dev)); + +return ___neigh_lookup_noref (tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev); }static inline struct neighbour *__ipv6_neigh_lookup (struct net_device *dev,const void *pkey)
@@ -397,8 +399,11 @@ static inline struct neighbour *ip_neigh_gw6(struct net_device *dev,struct neighbour *neigh; neigh =__ipv6_neigh_lookup_noref (dev, addr); -if (unlikely (!neigh)) - neigh =__neigh_create (&nd_tbl, addr, dev, false); +if (unlikely (!neigh)) { +struct neigh_table *tbl =nd_table (dev_net (dev)); + + neigh =__neigh_create (tbl, addr, dev, false); + }return neigh;#else
net/bridge/br_arp_nd_proxy.c +1 -1
@@ -470,7 +470,7 @@ void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,return ; } - n =neigh_lookup (&nd_tbl, &msg->target, vlandev); + n =neigh_lookup (nd_table (dev_net (vlandev)), &msg->target, vlandev);if (n) {struct net_bridge_fdb_entry *f;u8 ha[ETH_ALEN ]__aligned(2) ;
net/ieee802154/6lowpan/tx.c +2 -1
@@ -58,8 +58,9 @@ int lowpan_header_create(struct sk_buff *skb, struct net_device *ldev, info->daddr.mode =IEEE802154_ADDR_SHORT ; }else {__le16 short_addr =cpu_to_le16 (IEEE802154_ADDR_SHORT_UNSPEC ); +struct neigh_table *tbl =nd_table (dev_net (ldev)); - n =neigh_lookup (&nd_tbl, &hdr->daddr, ldev); + n =neigh_lookup (tbl, &hdr->daddr, ldev);if (n) { llneigh =lowpan_802154_neigh (neighbour_priv (n));read_lock_bh (&n->lock);
net/ipv4/fib_semantics.c +1 -1
@@ -617,7 +617,7 @@ static int fib_detect_death(struct fib_info *fi, int order,if (likely (nhc->nhc_gw_family ==AF_INET )) n =neigh_lookup (arp_table (net), &nhc->nhc_gw.ipv4, nhc->nhc_dev);else if (IS_ENABLED (CONFIG_IPV6 ) && nhc->nhc_gw_family ==AF_INET6 ) - n =neigh_lookup (&nd_tbl, &nhc->nhc_gw.ipv6, nhc->nhc_dev); + n =neigh_lookup (nd_table (net), &nhc->nhc_gw.ipv6, nhc->nhc_dev);else n =NULL ;
net/ipv6/addrconf.c +11 -6
@@ -376,6 +376,8 @@ static int snmp6_alloc_dev(struct inet6_dev *idev)static struct inet6_dev *ipv6_add_dev (struct net_device *dev) { +struct net *net =dev_net (dev); +struct neigh_table *tbl;struct inet6_dev *ndev;int err = -ENOMEM ;
@@ -393,14 +395,15 @@ static struct inet6_dev *ipv6_add_dev(struct net_device *dev) ndev->dev = dev;INIT_LIST_HEAD (&ndev->addr_list);timer_setup (&ndev->rs_timer, addrconf_rs_timer,0 ); -memcpy (&ndev->cnf,dev_net (dev)->ipv6.devconf_dflt,sizeof (ndev->cnf)); +memcpy (&ndev->cnf, net->ipv6.devconf_dflt,sizeof (ndev->cnf));if (ndev->cnf.stable_secret.initialized) ndev->cnf.addr_gen_mode =IN6_ADDR_GEN_MODE_STABLE_PRIVACY ; + tbl =nd_table (net); ndev->cnf.mtu6 = dev->mtu; ndev->ra_mtu =0 ; - ndev->nd_parms =neigh_parms_alloc (dev, &nd_tbl); + ndev->nd_parms =neigh_parms_alloc (dev, tbl);if (!ndev->nd_parms) {kfree (ndev);return ERR_PTR (err);
@@ -413,7 +416,7 @@ static struct inet6_dev *ipv6_add_dev(struct net_device *dev)if (snmp6_alloc_dev (ndev) <0 ) {netdev_dbg (dev,"%s: cannot allocate memory for statistics\n" , __func__); -neigh_parms_release (&nd_tbl, ndev->nd_parms); +neigh_parms_release (tbl, ndev->nd_parms);netdev_put (dev, &ndev->dev_tracker);kfree (ndev);return ERR_PTR (err);
@@ -481,7 +484,7 @@ static struct inet6_dev *ipv6_add_dev(struct net_device *dev)return ndev; err_release: -neigh_parms_release (&nd_tbl, ndev->nd_parms); +neigh_parms_release (tbl, ndev->nd_parms); ndev->dead =1 ;in6_dev_finish_destroy (ndev);return ERR_PTR (err);
@@ -4039,9 +4042,11 @@ static int addrconf_ifdown(struct net_device *dev, bool unregister)/* Last: Shot the device (if unregistered) */ if (unregister) { +struct neigh_table *tbl =nd_table (net); +addrconf_sysctl_unregister (idev); -neigh_parms_release (&nd_tbl, idev->nd_parms); -neigh_ifdown (&nd_tbl, dev); +neigh_parms_release (tbl, idev->nd_parms); +neigh_ifdown (tbl, dev);in6_dev_put (idev); }return 0 ;
net/ipv6/ip6_output.c +2 -2
@@ -127,7 +127,7 @@ static int ip6_finish_output2(struct net *net, struct sock *sk, struct sk_buff *if (IS_ERR_OR_NULL (neigh)) {if (unlikely (!neigh)) - neigh =__neigh_create (&nd_tbl, nexthop, dev, false); + neigh =__neigh_create (nd_table (net), nexthop, dev, false);if (IS_ERR (neigh)) {IP6_INC_STATS (net, idev,IPSTATS_MIB_OUTNOROUTES );kfree_skb_reason (skb,SKB_DROP_REASON_NEIGH_CREATEFAIL );
@@ -583,7 +583,7 @@ int ip6_forward(struct sk_buff *skb)/* XXX: idev->cnf.proxy_ndp? */ if (READ_ONCE (net->ipv6.devconf_all->proxy_ndp) && -pneigh_lookup (&nd_tbl, net, &hdr->daddr, skb->dev)) { +pneigh_lookup (nd_table (net), net, &hdr->daddr, skb->dev)) {int proxied =ip6_forward_proxy_check (skb); hdr =ipv6_hdr (skb);
net/ipv6/ndisc.c +50 -41
264 lines not on this page · raw message
net/ipv6/route.c +10 -8
@@ -217,7 +217,7 @@ struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,if (n)return n; - n =neigh_create (&nd_tbl, daddr, dev); + n =neigh_create (nd_table (dev_net (dev)), daddr, dev);return IS_ERR (n) ?NULL : n; }
@@ -4241,6 +4241,7 @@ static int ip6_route_del(struct fib6_config *cfg,static void rt6_do_redirect (struct dst_entry *dst,struct sock *sk,struct sk_buff *skb) {struct netevent_redirect netevent; +struct net_device *dev = skb->dev;struct rt6_info *rt, *nrt =NULL ;struct fib6_result res = {};struct ndisc_options ndopts;
@@ -4274,7 +4275,7 @@ static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_bureturn ; } - in6_dev =__in6_dev_get (skb->dev); + in6_dev =__in6_dev_get (dev);if (!in6_dev)return ; if (READ_ONCE(in6_dev->cnf.forwarding) ||
@@ -4286,15 +4287,14 @@ static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_bu* first-hop router for the specified ICMP Destination Address. */ -if (!ndisc_parse_options (skb->dev, msg->opt, optlen, &ndopts)) { +if (!ndisc_parse_options (dev, msg->opt, optlen, &ndopts)) {net_dbg_ratelimited ("rt6_redirect: invalid ND options\n" );return ; } lladdr =NULL ;if (ndopts.nd_opts_tgt_lladdr) { - lladdr =ndisc_opt_addr_data (ndopts.nd_opts_tgt_lladdr, - skb->dev); + lladdr =ndisc_opt_addr_data (ndopts.nd_opts_tgt_lladdr, dev);if (!lladdr) {net_dbg_ratelimited ("rt6_redirect: invalid link-layer address length\n" );return ;
@@ -4313,7 +4313,7 @@ static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_bu*/ dst_confirm_neigh (&rt->dst, &ipv6_hdr (skb)->saddr); - neigh =__neigh_lookup (&nd_tbl, &msg->target, skb->dev,1 ); + neigh =__neigh_lookup (nd_table (dev_net (dev)), &msg->target, dev,1 );if (!neigh)return ;
@@ -4321,7 +4321,7 @@ static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_bu* We have finally decided to accept it. */ - ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, + ndisc_update(dev, neigh, lladdr, NUD_STALE, NEIGH_UPDATE_F_WEAK_OVERRIDE| NEIGH_UPDATE_F_OVERRIDE| (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
@@ -5042,9 +5042,11 @@ void rt6_sync_down_dev(struct net_device *dev, unsigned long event)void rt6_disable_ip (struct net_device *dev,unsigned long event) { +struct net *net =dev_net (dev); +rt6_sync_down_dev (dev, event);rt6_uncached_list_flush_dev (dev); -neigh_ifdown (&nd_tbl, dev); +neigh_ifdown (nd_table (net), dev); }struct rt6_mtu_change_arg {
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
[email protected]
AI reviewRe: [PATCH v6 net-next 09/15] ipv6: Replace &nd_tbl with nd_table(net).
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Critical: 1 · High: 0 · Medium: 0 · Low: 0
- [Critical] Calling nd_table(net) before inet6_init() executes results in a NULL pointer dereference during driver probe.
--
Quoted 24 lines
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c +3
-3@@ -17,26 +17,27 @@#include "fs_core.h" #include "diag/en_rep_tracepoint.h" -static unsigned long mlx5e_rep_ipv6_interval (void ) +static unsigned long mlx5e_rep_ipv6_interval (struct net *net) {if (IS_ENABLED (CONFIG_IPV6 ) &&ipv6_mod_enabled ()) -return NEIGH_VAR (&nd_tbl.parms,DELAY_PROBE_TIME ); +return NEIGH_VAR (&nd_table (net)->parms,DELAY_PROBE_TIME );return ~0UL ; }static void mlx5e_rep_neigh_update_init_interval (struct mlx5e_rep_priv *rpriv) { -unsigned long ipv6_interval =mlx5e_rep_ipv6_interval ();struct net_device *netdev = rpriv->netdev;struct net *net =dev_net (netdev);unsigned long ipv4_interval; +unsigned long ipv6_interval;
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c line 43
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c +1
-0struct neigh_table *tbl;struct mlx5e_priv *priv; priv =netdev_priv (netdev); tbl =arp_table (net); ipv4_interval =NEIGH_VAR (&tbl->parms,DELAY_PROBE_TIME ); + ipv6_interval =mlx5e_rep_ipv6_interval (net); rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);mlx5_fc_update_sampling_interval (priv->mdev, rpriv->neigh_update.min_interval);
[Severity: Critical]
Unlike &nd_tbl, which is a statically allocated object that is valid from the very first instruction of the kernel, nd_table(net) reads net->neigh_tables[NEIGH_ND_TABLE], which is only populated once ndisc_init()/inet6_init() has run.
Both of the new dereferences here are on driver probe paths that can run before that point: mlx5e_rep_ipv6_interval() (via mlx5e_rep_neigh_update_init_interval()) and mlxsw_sp_router_neighs_update_interval_init() take &nd_table(net)->parms and feed it to NEIGH_VAR(), so a NULL table turns into a dereference of offsetof(struct neigh_table, parms) rather than a benign zero. For built-in drivers, drivers/ is linked before net/, and both the driver module_init() and inet6_init() are device_initcall(), so the driver can win that race and oops during boot. Note that ipv6_mod_enabled() does not help here: it only reflects the ipv6.disable boot parameter and returns true well before the nd table exists.
The same concern applies to any other conversion in this series that can be reached from driver probe or from a netdev notifier that runs before inet6_init().
How do you want to handle this? Either nd_table(net) needs to be safe to call before ndisc_init() (e.g. by allocating the per-netns slots earlier, in a pernet subsys registered ahead of the drivers), or these callers need an explicit NULL check on the returned table before touching ->parms. Please spell out the ordering guarantee you are relying on in the changelog, since it is no longer obvious once &nd_tbl becomes an indirection through struct net.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
Re: [PATCH v6 net-next 09/15] ipv6: Replace &nd_tbl with nd_table(net).
Quoted 52 lines · On Thu, Sep 3, 2026 at 8:10 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Critical: 1 · High: 0 · Medium: 0 · Low: 0
- [Critical] Calling
nd_table(net)beforeinet6_init()executes results in a NULL pointer dereference during driver probe.--
Quoted 24 lines
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c +3
-3@@ -17,26 +17,27 @@#include "fs_core.h" #include "diag/en_rep_tracepoint.h" -static unsigned long mlx5e_rep_ipv6_interval (void ) +static unsigned long mlx5e_rep_ipv6_interval (struct net *net) {if (IS_ENABLED (CONFIG_IPV6 ) &&ipv6_mod_enabled ()) -return NEIGH_VAR (&nd_tbl.parms,DELAY_PROBE_TIME ); +return NEIGH_VAR (&nd_table (net)->parms,DELAY_PROBE_TIME );return ~0UL ; }static void mlx5e_rep_neigh_update_init_interval (struct mlx5e_rep_priv *rpriv) { -unsigned long ipv6_interval =mlx5e_rep_ipv6_interval ();struct net_device *netdev = rpriv->netdev;struct net *net =dev_net (netdev);unsigned long ipv4_interval; +unsigned long ipv6_interval;drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c line 43
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/en/rep/neigh.c +1
-0struct neigh_table *tbl;struct mlx5e_priv *priv; priv =netdev_priv (netdev); tbl =arp_table (net); ipv4_interval =NEIGH_VAR (&tbl->parms,DELAY_PROBE_TIME ); + ipv6_interval =mlx5e_rep_ipv6_interval (net); rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);mlx5_fc_update_sampling_interval (priv->mdev, rpriv->neigh_update.min_interval);[Severity: Critical]
Unlike&nd_tbl, which is a statically allocated object that is valid from the very first instruction of the kernel,nd_table(net)reads net->neigh_tables[NEIGH_ND_TABLE], which is only populated once ndisc_init()/inet6_init() has run.
Quoted 10 lines
Both of the new dereferences here are on driver probe paths that can run before that point:
mlx5e_rep_ipv6_interval()(viamlx5e_rep_neigh_update_init_interval()) andmlxsw_sp_router_neighs_update_interval_init()take &nd_table(net)->parms and feed it toNEIGH_VAR(), so a NULL table turns into a dereference ofoffsetof(struct neigh_table, parms)rather than a benign zero. For built-in drivers,drivers/is linked beforenet/, and both the drivermodule_init()andinet6_init()aredevice_initcall(), so the driver can win that race and oops during boot.
I'm a bit surprised that IPv6 uses device_initcall() instead of fs_, but looking at 309b905deee5, this is for IPv4 dependency.
If such a race occurs, there should be more issues, and the cleaner way would be to add a new init level between fs and device for IPv6.
Quoted 18 lines
Note that
ipv6_mod_enabled()does not help here: it only reflects the ipv6.disable boot parameter and returns true well before the nd table exists.The same concern applies to any other conversion in this series that can be reached from driver probe or from a netdev notifier that runs before
inet6_init().How do you want to handle this? Either
nd_table(net)needs to be safe to call beforendisc_init()(e.g. by allocating the per-netns slots earlier, in a pernet subsys registered ahead of the drivers), or these callers need an explicit NULL check on the returned table before touching ->parms. Please spell out the ordering guarantee you are relying on in the changelog, since it is no longer obvious once&nd_tblbecomes an indirection through struct net.--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
PATCH v6 10/15 neighbour: Clean up neigh_table_init() and neigh_table_clear().
In reply to Kuniyuki IwashimaCurrently, neigh_table_init() and neigh_table_clear() are called once at the boot time, but we will call them for each netns.
Let's clean up them.
For neigh_table_init(),
* Allocate all memory first and unwind on failure
* Remove !tbl->entry_size check since it is always true for arp_tbl and nd_tbl
* Initialise everything before starting delayed works
For neigh_table_clear(),
* Reorder function to match neigh_table_init()
* Use timer_shutdown_sync() instead of timer_delete_sync()
* Remove pneigh_queue_purge() since it is called via neigh_ifdown()
* Replace pr_crit() with DEBUG_NET_WARN_ON_ONCE()
* Call neigh_hash_free_rcu() directly without call_rcu()
In a later patch, neigh_table_clear() will be split so that tbl->{stats,phash_buckets,nht} will be freed only when all neighbour entries are freed.
---
v2: Add __maybe_unused to net in neigh_table_clear() to
avoid transient W=1 warning for CONFIG_PROC_FS=n (next patch removes it anyway)
--- net/core/neighbour.c | 98 +++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 43 deletions(-)
net/core/neighbour.c +55 -43
@@ -1810,57 +1810,68 @@ static struct lock_class_key neigh_table_proxy_queue_class;void neigh_table_init (struct neigh_table *tbl) {unsigned long now = jiffies; +struct net *net = &init_net;unsigned long phsize; -INIT_LIST_HEAD (&tbl->parms_list); -INIT_LIST_HEAD (&tbl->gc_list); -INIT_LIST_HEAD (&tbl->managed_list); +RCU_INIT_POINTER (tbl->nht,neigh_hash_alloc (3 )); +if (!tbl->nht) + goto err_hash; -list_add (&tbl->parms.list, &tbl->parms_list); -write_pnet (&tbl->parms.net, &init_net); -refcount_set (&tbl->parms.refcnt,1 ); -neigh_set_reach_time (&tbl->parms); - tbl->parms.qlen =0 ; + phsize = (PNEIGH_HASHMASK +1 ) *sizeof (struct pneigh_entry *); + tbl->phash_buckets =kzalloc (phsize,GFP_KERNEL ); +if (!tbl->phash_buckets) + goto err_phash; + + tbl->entry_size =ALIGN (offsetof(struct neighbour , primary_key) + + tbl->key_len,NEIGH_PRIV_ALIGN ); tbl->stats = alloc_percpu(struct neigh_statistics);if (!tbl->stats) -panic ("cannot create neighbour cache statistics" ); + goto err_stats;#ifdef CONFIG_PROC_FS -if (!proc_create_seq_data (tbl->id,0 , init_net.proc_net_stat, - &neigh_stat_seq_ops, tbl)) -panic ("cannot create neighbour proc dir entry" ); +if (!proc_create_seq_data (tbl->id,0 , net->proc_net_stat, + &neigh_stat_seq_ops, tbl)) + goto err_proc;#endif -RCU_INIT_POINTER (tbl->nht,neigh_hash_alloc (3 )); - - phsize = (PNEIGH_HASHMASK +1 ) *sizeof (struct pneigh_entry *); - tbl->phash_buckets =kzalloc (phsize,GFP_KERNEL ); - -if (!tbl->nht || !tbl->phash_buckets) -panic ("cannot allocate neighbour cache hashes" ); - -if (!tbl->entry_size) - tbl->entry_size =ALIGN (offsetof(struct neighbour , primary_key) + - tbl->key_len,NEIGH_PRIV_ALIGN ); -else -WARN_ON (tbl->entry_size %NEIGH_PRIV_ALIGN ); + tbl->parms.tbl = tbl; + tbl->parms.qlen =0 ; +INIT_LIST_HEAD (&tbl->parms_list); +list_add (&tbl->parms.list, &tbl->parms_list); +write_pnet (&tbl->parms.net, net); +refcount_set (&tbl->parms.refcnt,1 ); +neigh_set_reach_time (&tbl->parms); + tbl->last_flush = now; + tbl->last_rand = now + tbl->parms.reachable_time *20 ;spin_lock_init (&tbl->lock);mutex_init (&tbl->phash_lock); +skb_queue_head_init_class (&tbl->proxy_queue, + &neigh_table_proxy_queue_class); +timer_setup (&tbl->proxy_timer, neigh_proxy_process,0 ); +INIT_LIST_HEAD (&tbl->gc_list);INIT_DEFERRABLE_WORK (&tbl->gc_work, neigh_periodic_work);queue_delayed_work (system_power_efficient_wq, &tbl->gc_work, - tbl->parms.reachable_time); + tbl->parms.reachable_time); + +INIT_LIST_HEAD (&tbl->managed_list);INIT_DEFERRABLE_WORK (&tbl->managed_work, neigh_managed_work);queue_delayed_work (system_power_efficient_wq, &tbl->managed_work,0 ); -timer_setup (&tbl->proxy_timer, neigh_proxy_process,0 ); -skb_queue_head_init_class (&tbl->proxy_queue, - &neigh_table_proxy_queue_class); +return ; - tbl->last_flush = now; - tbl->last_rand = now + tbl->parms.reachable_time *20 ; +#ifdef CONFIG_PROC_FS +err_proc: +free_percpu (tbl->stats); +#endif +err_stats: +kfree (tbl->phash_buckets); +err_phash: +neigh_hash_free_rcu (&rcu_dereference_protected (tbl->nht,1 )->rcu); +err_hash: +panic ("cannot allocate memory" ); }/*
@@ -1869,26 +1880,27 @@ void neigh_table_init(struct neigh_table *tbl)*/ int neigh_table_clear (struct neigh_table *tbl) { -/* It is not clean... Fix it to unload IPv6 module safely */ +struct net *net__maybe_unused = &init_net; +struct neigh_hash_table *nht; +cancel_delayed_work_sync (&tbl->managed_work);cancel_delayed_work_sync (&tbl->gc_work); -timer_delete_sync (&tbl->proxy_timer); -pneigh_queue_purge (&tbl->proxy_queue,NULL , tbl->family); +timer_shutdown_sync (&tbl->proxy_timer); +neigh_ifdown (tbl,NULL ); -if (atomic_read (&tbl->entries)) -pr_crit ("neighbour leakage\n" ); +DEBUG_NET_WARN_ON_ONCE (atomic_read (&tbl->entries)); -call_rcu (&rcu_dereference_protected (tbl->nht,1 )->rcu, - neigh_hash_free_rcu); - tbl->nht =NULL ; +remove_proc_entry (tbl->id, net->proc_net_stat); + +free_percpu (tbl->stats); + tbl->stats =NULL ;kfree (tbl->phash_buckets); tbl->phash_buckets =NULL ; -remove_proc_entry (tbl->id, init_net.proc_net_stat); - -free_percpu (tbl->stats); - tbl->stats =NULL ; + nht =rcu_dereference_protected (tbl->nht,1 ); + tbl->nht =NULL ; +neigh_hash_free_rcu (&nht->rcu);return 0 ; }
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
[email protected]
AI reviewRe: [PATCH v6 net-next 10/15] neighbour: Clean up neigh_table_init() and neigh_table_clear().
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Critical: 0 · High: 1 · Medium: 0 · Low: 0 (1)
- [High] net/core/neighbour.c:neigh_table_clear() now reclaims the RCU- published neighbour hash table synchronously: `nht =…
Pre-existing issues:
- [Low] neigh_table_clear() (net/core/neighbour.c:1881-1906) frees table- wide resources (free_percpu(tbl->stats) at 1895,…
--
neighbour: Clean up neigh_table_init() and neigh_table_clear().
This reworks neigh_table_init() to allocate everything up front and unwind on failure, and reorders neigh_table_clear() to mirror it. The teardown side also switches to timer_shutdown_sync(), drops the explicit pneigh_queue_purge(), replaces pr_crit() with DEBUG_NET_WARN_ON_ONCE(), and calls neigh_hash_free_rcu() directly instead of via call_rcu().
net/core/neighbour.c
Quoted 4 lines
net/core/neighbour.c +0
-0
[ ... neigh_table_init() hunk snipped ... ]
Quoted 8 lines
net/core/neighbour.c +3
-1@@ -1869,26 +1880,27 @@ void neigh_table_init(struct neigh_table *tbl)*/ int neigh_table_clear (struct neigh_table *tbl) { -/* It is not clean... Fix it to unload IPv6 module safely */ +struct net *net__maybe_unused = &init_net; +struct neigh_hash_table *nht; +
net/core/neighbour.c void neigh_table_init(struct neigh_table *tbl) · line 1891
Quoted 10 lines
net/core/neighbour.c +3
-4cancel_delayed_work_sync (&tbl->managed_work);cancel_delayed_work_sync (&tbl->gc_work); -timer_delete_sync (&tbl->proxy_timer); -pneigh_queue_purge (&tbl->proxy_queue,NULL , tbl->family); +timer_shutdown_sync (&tbl->proxy_timer); +neigh_ifdown (tbl,NULL ); -if (atomic_read (&tbl->entries)) -pr_crit ("neighbour leakage\n" ); +DEBUG_NET_WARN_ON_ONCE (atomic_read (&tbl->entries));
[Severity: Low]
This isn't a bug introduced by this patch, but the diagnostic swap makes a pre-existing lifecycle problem invisible on production kernels: with CONFIG_DEBUG_NET=n, DEBUG_NET_WARN_ON_ONCE() compiles to nothing, so the "still have entries" condition that previously always printed is no longer reported.
The table-owned resources are still freed unconditionally just below, and a neighbour destroyed afterwards touches them:
net/core/neighbour.c:neigh_destroy() {
NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
...
atomic_dec(&neigh->tbl->entries);
}The unconditional freeing itself is unchanged from the baseline, and the next patch in the series, "neighbour: Convert neigh_table.entries to refcount_t", defers freeing of tbl->stats, tbl->phash_buckets and tbl->nht into neigh_table_free() once the last reference is dropped.
Is dropping the production-visible message intentional for this intermediate commit?
Quoted 9 lines
net/core/neighbour.c +4
-3-call_rcu (&rcu_dereference_protected (tbl->nht,1 )->rcu, - neigh_hash_free_rcu); - tbl->nht =NULL ; +remove_proc_entry (tbl->id, net->proc_net_stat); + +free_percpu (tbl->stats); + tbl->stats =NULL ;
net/core/neighbour.c void neigh_table_init(struct neigh_table *tbl) · line 1903
Quoted 10 lines
net/core/neighbour.c +3
-4kfree (tbl->phash_buckets); tbl->phash_buckets =NULL ; -remove_proc_entry (tbl->id, init_net.proc_net_stat); - -free_percpu (tbl->stats); - tbl->stats =NULL ; + nht =rcu_dereference_protected (tbl->nht,1 ); + tbl->nht =NULL ; +neigh_hash_free_rcu (&nht->rcu);
[Severity: High]
Can this free the neighbour hash table while lockless readers still hold a pointer to it?
neigh_hash_free_rcu() is an RCU callback body, so calling it directly releases both the bucket array and the table object immediately:
net/core/neighbour.c:neigh_hash_free_rcu() {
...
kfree(nht->hash_heads);
kfree(nht);
}Nothing in the new teardown sequence waits for a grace period:cancel_delayed_work_sync() twice, timer_shutdown_sync(), neigh_ifdown() and remove_proc_entry(). neigh_ifdown() only takes tbl->lock, which the readers never take.
The readers dereference tbl->nht under RCU with no NULL check:
include/net/neighbour.h:___neigh_lookup_noref() {
struct neigh_hash_table *nht = rcu_dereference(tbl->nht);
...
hash_val = hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
neigh_for_each_in_bucket_rcu(n, &nht->hash_heads[hash_val])
}so a reader that already loaded nht reads hash_shift, hash_rnd and hash_heads out of freed slab memory and then follows whatever hlist contents are left there. neigh_lookup(), neigh_dump_table(), neigh_seq_start() and neightbl_fill_info() have the same shape.
Is the table still published while this runs? ndisc_cleanup() calls neigh_table_clear(&nd_tbl) before unregister_pernet_subsys(&ndisc_net_ops), and the pernet exit path is what clears net->neigh_tables[NEIGH_ND_TABLE], so nd_tbl remains installed in every netns during the free. An unprivileged RTM_GETNEIGH dump reaches it holding only rcu_read_lock():
net/core/neighbour.c:neigh_dump_info() {
rcu_read_lock();
for (t = 0; t < NEIGH_NR_TABLES; t++) {
tbl = net->neigh_tables[t];and ndisc_cleanup() is live today from the module-init error unwind:
net/ipv6/af_inet6.c:inet6_init() {igmp_fail:
ndisc_cleanup();
The peer site retiring the same object type still uses a grace period:
net/core/neighbour.c:neigh_hash_grow() {
rcu_assign_pointer(tbl->nht, new_nht);
call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
}Would keeping call_rcu()/kfree_rcu(), or inserting a synchronize_net() before the free, be preferable here? The plain NULL store into the __rcu pointer tbl->nht predates this patch; what is new is the access to freed memory. This also does not appear to be addressed later in the series: at the end of the range the same unsynchronised free lives in neigh_table_free(), now additionally doing kfree(tbl), while neigh_table_unregister() clears net->neigh_tables[index] and calls neigh_table_clear()/neigh_table_put() with no grace period in between.
Quoted 3 lines
net/core/neighbour.c +0
-0return 0 ; }
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
Re: [PATCH v6 net-next 10/15] neighbour: Clean up neigh_table_init() and neigh_table_clear().
Quoted 8 lines · On Thu, Sep 3, 2026 at 8:10 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Critical: 0 · High: 1 · Medium: 0 · Low: 0 (1)
- [High] net/core/neighbour.c:neigh_table_clear() now reclaims the RCU- published neighbour hash table synchronously: `nht =…
This is false-positive.
IPv6 is unloadable and neigh_table_clear() is not called except for nd_init() failure, which we don't care.
Quoted 158 lines
Pre-existing issues:
- [Low]neigh_table_clear()(net/core/neighbour.c:1881-1906) frees table- wide resources (free_percpu(tbl->stats)at 1895,…--
neighbour: Clean up
neigh_table_init()andneigh_table_clear().This reworks
neigh_table_init()to allocate everything up front and unwind on failure, and reordersneigh_table_clear()to mirror it. The teardown side also switches totimer_shutdown_sync(), drops the explicitpneigh_queue_purge(), replacespr_crit()withDEBUG_NET_WARN_ON_ONCE(), and callsneigh_hash_free_rcu()directly instead of viacall_rcu().net/core/neighbour.c
Quoted 4 lines
net/core/neighbour.c +0
-0[ ...
neigh_table_init()hunk snipped ... ]Quoted 8 lines
net/core/neighbour.c +3
-1@@ -1869,26 +1880,27 @@ void neigh_table_init(struct neigh_table *tbl)*/ int neigh_table_clear (struct neigh_table *tbl) { -/* It is not clean... Fix it to unload IPv6 module safely */ +struct net *net__maybe_unused = &init_net; +struct neigh_hash_table *nht; +net/core/neighbour.c void neigh_table_init(struct neigh_table *tbl) · line 1891
Quoted 10 lines
net/core/neighbour.c +3
-4cancel_delayed_work_sync (&tbl->managed_work);cancel_delayed_work_sync (&tbl->gc_work); -timer_delete_sync (&tbl->proxy_timer); -pneigh_queue_purge (&tbl->proxy_queue,NULL , tbl->family); +timer_shutdown_sync (&tbl->proxy_timer); +neigh_ifdown (tbl,NULL ); -if (atomic_read (&tbl->entries)) -pr_crit ("neighbour leakage\n" ); +DEBUG_NET_WARN_ON_ONCE (atomic_read (&tbl->entries));[Severity: Low]
This isn't a bug introduced by this patch, but the diagnostic swap makes a pre-existing lifecycle problem invisible on production kernels: with
CONFIG_DEBUG_NET=n,DEBUG_NET_WARN_ON_ONCE()compiles to nothing, so the "still have entries" condition that previously always printed is no longer reported.The table-owned resources are still freed unconditionally just below, and a neighbour destroyed afterwards touches them:
net/core/neighbour.c:neigh_destroy() { NEIGH_CACHE_STAT_INC(neigh->tbl, destroys); ... atomic_dec(&neigh->tbl->entries); }The unconditional freeing itself is unchanged from the baseline, and the next patch in the series, "neighbour: Convert neigh_table.entries to
refcount_t", defers freeing oftbl->stats,tbl->phash_bucketsandtbl->nhtintoneigh_table_free()once the last reference is dropped.Is dropping the production-visible message intentional for this intermediate commit?
Quoted 9 lines
net/core/neighbour.c +4
-3-call_rcu (&rcu_dereference_protected (tbl->nht,1 )->rcu, - neigh_hash_free_rcu); - tbl->nht =NULL ; +remove_proc_entry (tbl->id, net->proc_net_stat); + +free_percpu (tbl->stats); + tbl->stats =NULL ;net/core/neighbour.c void neigh_table_init(struct neigh_table *tbl) · line 1903
Quoted 10 lines
net/core/neighbour.c +3
-4kfree (tbl->phash_buckets); tbl->phash_buckets =NULL ; -remove_proc_entry (tbl->id, init_net.proc_net_stat); - -free_percpu (tbl->stats); - tbl->stats =NULL ; + nht =rcu_dereference_protected (tbl->nht,1 ); + tbl->nht =NULL ; +neigh_hash_free_rcu (&nht->rcu);[Severity: High]
Can this free the neighbour hash table while lockless readers still hold a pointer to it?
neigh_hash_free_rcu()is an RCU callback body, so calling it directly releases both the bucket array and the table object immediately:net/core/neighbour.c:neigh_hash_free_rcu() { ... kfree(nht->hash_heads); kfree(nht); }Nothing in the new teardown sequence waits for a grace period:
cancel_delayed_work_sync()twice,timer_shutdown_sync(),neigh_ifdown()andremove_proc_entry().neigh_ifdown()only takestbl->lock, which the readers never take.The readers dereference
tbl->nhtunder RCU with no NULL check:include/net/neighbour.h:___neigh_lookup_noref() { struct neigh_hash_table *nht = rcu_dereference(tbl->nht); ... hash_val = hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); neigh_for_each_in_bucket_rcu(n, &nht->hash_heads[hash_val]) }so a reader that already loaded nht reads
hash_shift,hash_rndandhash_headsout of freed slab memory and then follows whatever hlist contents are left there.neigh_lookup(),neigh_dump_table(),neigh_seq_start()andneightbl_fill_info()have the same shape.Is the table still published while this runs?
ndisc_cleanup()callsneigh_table_clear(&nd_tbl)beforeunregister_pernet_subsys(&ndisc_net_ops), and the pernet exit path is what clears net->neigh_tables[NEIGH_ND_TABLE], sond_tblremains installed in every netns during the free. An unprivilegedRTM_GETNEIGHdump reaches it holding onlyrcu_read_lock():net/core/neighbour.c:neigh_dump_info() { rcu_read_lock(); for (t = 0; t < NEIGH_NR_TABLES; t++) { tbl = net->neigh_tables[t];and
ndisc_cleanup()is live today from the module-init error unwind:net/ipv6/af_inet6.c:inet6_init() {
igmp_fail:ndisc_cleanup();The peer site retiring the same object type still uses a grace period:
net/core/neighbour.c:neigh_hash_grow() { rcu_assign_pointer(tbl->nht, new_nht); call_rcu(&old_nht->rcu, neigh_hash_free_rcu); }Would keeping call_rcu()/kfree_rcu(), or inserting a
synchronize_net()before the free, be preferable here? The plain NULL store into the__rcupointertbl->nhtpredates this patch; what is new is the access to freed memory. This also does not appear to be addressed later in the series: at the end of the range the same unsynchronised free lives inneigh_table_free(), now additionally doing kfree(tbl), whileneigh_table_unregister()clears net->neigh_tables[index] and calls neigh_table_clear()/neigh_table_put() with no grace period in between.net/core/neighbour.c void neigh_table_init(struct neigh_table *tbl) · line 1906
Quoted 3 lines
net/core/neighbour.c +0
-0return 0 ; }--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
PATCH v6 11/15 neighbour: Convert neigh_table.entries to refcount_t.
In reply to Kuniyuki IwashimaWe will allocate neigh_table for each netns and free it when netns is destroyed.
neigh_ifdown() cleans up all neighbour entries during netns dismantle, but there is no synchronisation between timers because neigh_del_timer() uses timer_delete() to stop a timer.
If neigh_table were freed while timer were running, neigh_destroy() would touch the freed table.
If we called timer_delete_sync() in neigh_flush_one() under tbl->lock, lockdep would complain although it is false-positive.
Let's convert neigh_table.entries to refcount_t and destruct neigh_table only when the count reaches 0.
--- include/net/neighbour.h | 2 +- net/core/neighbour.c | 58 ++++++++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 19 deletions(-)
include/net/neighbour.h +1 -1
@@ -234,7 +234,7 @@ struct neigh_table {struct delayed_work managed_work;struct timer_list proxy_timer;struct sk_buff_head proxy_queue; -atomic_t entries; +refcount_t entries;atomic_t gc_entries;struct list_head gc_list;struct list_head managed_list;
net/core/neighbour.c +40 -18
126 lines not on this page · raw message
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
[email protected]
AI reviewRe: [PATCH v6 net-next 11/15] neighbour: Convert neigh_table.entries to refcount_t.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Critical: 0 · High: 0 · Medium: 1 · Low: 1
- [Medium] neigh_table_free() frees RCU-published, table-owned resources with no grace period, while the objects that point at the table are…
- [Low] Teardown ordering concern: neigh_table_clear() ends with neigh_table_put(), dropping the base reference installed by…
--
neighbour: Convert neigh_table.entries to refcount_t.
This converts neigh_table.entries from atomic_t to refcount_t and adds neigh_table_get()/neigh_table_put() helpers plus a new neigh_table_free() so that the table resources are released only when the last neighbour is gone. neigh_table_clear() now just drops the base reference taken in neigh_table_init().
A couple of questions about the new teardown path below.
Quoted 28 lines
net/core/neighbour.c +17
-0@@ -55,6 +55,23 @@ static void neigh_notify(struct neighbour *n, int type, int flags, u32 pid);static void __neigh_notify (struct neighbour *n,int type,int flags,u32 pid);static void pneigh_ifdown (struct neigh_table *tbl,struct net_device *dev,bool skip_perm); +static void neigh_table_free (struct neigh_table *tbl); + +static void neigh_table_get (struct neigh_table *tbl) +{ +refcount_inc (&tbl->entries); +} + +static void neigh_table_put (struct neigh_table *tbl) +{ +if (refcount_dec_and_test (&tbl->entries)) +neigh_table_free (tbl); +} + +static int neigh_table_entries (struct neigh_table *tbl) +{ +return refcount_read (&tbl->entries) -1 ; +}#ifdef CONFIG_PROC_FS static const struct seq_operations neigh_stat_seq_ops;
[ ... ]
Quoted 8 lines
net/core/neighbour.c +1
-0@@ -1845,6 +1862,7 @@ void neigh_table_init(struct neigh_table *tbl) tbl->last_flush = now; tbl->last_rand = now + tbl->parms.reachable_time *20 ; +refcount_set (&tbl->entries,1 );spin_lock_init (&tbl->lock);mutex_init (&tbl->phash_lock); skb_queue_head_init_class(&tbl->proxy_queue,
[ ... ]
Quoted 9 lines
net/core/neighbour.c +1
-1@@ -924,7 +941,7 @@ void neigh_destroy(struct neighbour *neigh)neigh_dbg (2 ,"neigh %p is destroyed\n" , neigh); -atomic_dec (&neigh->tbl->entries); +neigh_table_put (neigh->tbl);kfree_rcu (neigh, rcu); }EXPORT_SYMBOL (neigh_destroy);
[ ... ]
Quoted 8 lines
net/core/neighbour.c +4
-0@@ -1874,6 +1892,21 @@ void neigh_table_init(struct neigh_table *tbl)panic ("cannot allocate memory" ); } +static void neigh_table_free (struct neigh_table *tbl) +{ +struct neigh_hash_table *nht; +
net/core/neighbour.c void neigh_table_init(struct neigh_table *tbl) · line 1908
Quoted 10 lines
net/core/neighbour.c +10
-0+free_percpu (tbl->stats); + tbl->stats =NULL ; + +kfree (tbl->phash_buckets); + tbl->phash_buckets =NULL ; + + nht =rcu_dereference_protected (tbl->nht,1 ); + tbl->nht =NULL ; +neigh_hash_free_rcu (&nht->rcu); +}
[Severity: Medium]
Is it safe to free these RCU-published members synchronously here? neigh_table_free() calls free_percpu(tbl->stats), kfree(tbl->phash_buckets) and invokes the RCU callback neigh_hash_free_rcu() directly, with no grace period in between.
At the same time neigh_destroy() drops the table reference before the neighbour body is retired:
neigh_table_put(neigh->tbl); kfree_rcu(neigh, rcu);
If that put is the last one, the table (the pointee) is reclaimed immediately while the neighbour holding neigh->tbl survives a full grace period.
Lockless readers hold no-ref neighbour pointers under rcu_read_lock() and dereference the table through them, for example in __neigh_event_send():
NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
which is reached from the transmit path ip_finish_output2()/ip6_finish_output2() -> neigh_output() -> neigh_event_send(). tbl->nht is read via rcu_dereference() in ___neigh_lookup_noref(), and tbl->phash_buckets is declared __rcu and walked by pneigh_lookup().
The same file retires nht only after a grace period:
net/core/neighbour.c:neigh_hash_grow() {
...
rcu_assign_pointer(tbl->nht, new_nht);
call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
...
}Note also that neigh_table_put() can now reach this free from softirq context, since neigh_timer_handler() -> neigh_release() -> neigh_destroy() runs long after neigh_table_clear() returned, and neigh_destroy() releases the netdev reference before the table reference, so netdev_wait_allrefs() does not order it either.
Most reader windows do look drained in practice (cleanup_net() runs synchronize_rcu_expedited() before the pernet exit ops, and unregister_netdevice_many_notify() runs synchronize_net() before the neighbour flush), so I could not show a reader that is still in flight at that instant. Still, would it be better to defer this free with call_rcu()/kfree_rcu(), or to drop the table reference from the neighbour's own RCU callback, especially since the follow-up patch adds kfree(tbl) to this function?
Quoted 21 lines
net/core/neighbour.c +0
-6/* * Only called from ndisc_cleanup(), which means this is dead code * because we no longer can unload IPv6 module. @@ -1881,26 +1914,15 @@ void neigh_table_init(struct neigh_table *tbl)int neigh_table_clear (struct neigh_table *tbl) {struct net *net__maybe_unused = &init_net; -struct neigh_hash_table *nht;cancel_delayed_work_sync (&tbl->managed_work);cancel_delayed_work_sync (&tbl->gc_work);timer_shutdown_sync (&tbl->proxy_timer);neigh_ifdown (tbl,NULL ); -DEBUG_NET_WARN_ON_ONCE (atomic_read (&tbl->entries)); -remove_proc_entry (tbl->id, net->proc_net_stat); -free_percpu (tbl->stats); - tbl->stats =NULL ; -
net/core/neighbour.c void neigh_table_init(struct neigh_table *tbl) · line 1928
Quoted 10 lines
net/core/neighbour.c +1
-6-kfree (tbl->phash_buckets); - tbl->phash_buckets =NULL ; - - nht =rcu_dereference_protected (tbl->nht,1 ); - tbl->nht =NULL ; -neigh_hash_free_rcu (&nht->rcu); +neigh_table_put (tbl);return 0 ; }
[Severity: Low]
This is more of an ordering question. neigh_table_clear() drops the base reference installed by refcount_set(&tbl->entries, 1) in neigh_table_init(), but at this commit the table is still published: in ndisc_cleanup() the call order is neigh_table_clear(&nd_tbl) followed by unregister_pernet_subsys(&ndisc_net_ops).
Once the base reference is gone, neigh_table_entries() returns refcount_read() - 1, which is entries - 1 in general and -1 when the last neighbour is gone. That value feeds the __u32 uAPI field:
.ndtc_entries = neigh_table_entries(tbl),
and the %08x in neigh_stat_seq_show(), where it would print as ffffffff.
Checking the rest of the series, this looks unreachable in the final
state: neigh_table_unregister() sets net->neigh_tables[index] = NULL before calling neigh_table_clear(), the proc entry is removed and drained before the put, and ndisc_cleanup() no longer calls neigh_table_clear() at all. At this commit the only caller is the inet6_init() error path that the comment above already describes as dead code.
Would it still be worth dropping the base reference only after the table is unpublished, so the biased accessor is never read without it?
Quoted 18 lines
net/core/neighbour.c +2
-2@@ -2275,7 +2297,7 @@ static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,struct ndt_config ndc = { .ndtc_key_len = tbl->key_len, .ndtc_entry_size = tbl->entry_size, - .ndtc_entries =atomic_read (&tbl->entries), + .ndtc_entries =neigh_table_entries (tbl), .ndtc_last_flush =jiffies_to_msecs (flush_delta), .ndtc_last_rand =jiffies_to_msecs (rand_delta), .ndtc_proxy_qlen =READ_ONCE (tbl->proxy_queue.qlen),@@ -3495,7 +3517,7 @@ static int neigh_stat_seq_show(struct seq_file *seq, void *v) seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx " "%08lx %08lx %08lx " "%08lx %08lx %08lx\n", - atomic_read(&tbl->entries), + neigh_table_entries(tbl), st->allocs, st->destroys,
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
Re: [PATCH v6 net-next 11/15] neighbour: Convert neigh_table.entries to refcount_t.
Quoted 6 lines · On Thu, Sep 3, 2026 at 8:10 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Critical: 0 · High: 0 · Medium: 1 · Low: 1
Both are false-positive.
Quoted 2 lines
- [Medium]
neigh_table_free()frees RCU-published, table-owned resources with no grace period, while the objects that point at the table are…
When arp/ND's ->exit() is called, there should be no inflight RCU readers and we can simply rely on neigh entries' refcnt.
Quoted 2 lines
- [Low] Teardown ordering concern:
neigh_table_clear()ends withneigh_table_put(), dropping the base reference installed by…
If a user is reading proc entries, netns cannot be dismantled.
Quoted 215 lines
--
neighbour: Convert neigh_table.entries to
refcount_t.This converts neigh_table.entries from
atomic_ttorefcount_tand adds neigh_table_get()/neigh_table_put() helpers plus a newneigh_table_free()so that the table resources are released only when the last neighbour is gone.neigh_table_clear()now just drops the base reference taken inneigh_table_init().A couple of questions about the new teardown path below.
Quoted 28 lines
net/core/neighbour.c +17
-0@@ -55,6 +55,23 @@ static void neigh_notify(struct neighbour *n, int type, int flags, u32 pid);static void __neigh_notify (struct neighbour *n,int type,int flags,u32 pid);static void pneigh_ifdown (struct neigh_table *tbl,struct net_device *dev,bool skip_perm); +static void neigh_table_free (struct neigh_table *tbl); + +static void neigh_table_get (struct neigh_table *tbl) +{ +refcount_inc (&tbl->entries); +} + +static void neigh_table_put (struct neigh_table *tbl) +{ +if (refcount_dec_and_test (&tbl->entries)) +neigh_table_free (tbl); +} + +static int neigh_table_entries (struct neigh_table *tbl) +{ +return refcount_read (&tbl->entries) -1 ; +}#ifdef CONFIG_PROC_FS static const struct seq_operations neigh_stat_seq_ops;[ ... ]
Quoted 8 lines
net/core/neighbour.c +1
-0@@ -1845,6 +1862,7 @@ void neigh_table_init(struct neigh_table *tbl) tbl->last_flush = now; tbl->last_rand = now + tbl->parms.reachable_time *20 ; +refcount_set (&tbl->entries,1 );spin_lock_init (&tbl->lock);mutex_init (&tbl->phash_lock); skb_queue_head_init_class(&tbl->proxy_queue,[ ... ]
Quoted 9 lines
net/core/neighbour.c +1
-1@@ -924,7 +941,7 @@ void neigh_destroy(struct neighbour *neigh)neigh_dbg (2 ,"neigh %p is destroyed\n" , neigh); -atomic_dec (&neigh->tbl->entries); +neigh_table_put (neigh->tbl);kfree_rcu (neigh, rcu); }EXPORT_SYMBOL (neigh_destroy);[ ... ]
Quoted 8 lines
net/core/neighbour.c +4
-0@@ -1874,6 +1892,21 @@ void neigh_table_init(struct neigh_table *tbl)panic ("cannot allocate memory" ); } +static void neigh_table_free (struct neigh_table *tbl) +{ +struct neigh_hash_table *nht; +net/core/neighbour.c void neigh_table_init(struct neigh_table *tbl) · line 1908
Quoted 10 lines
net/core/neighbour.c +10
-0+free_percpu (tbl->stats); + tbl->stats =NULL ; + +kfree (tbl->phash_buckets); + tbl->phash_buckets =NULL ; + + nht =rcu_dereference_protected (tbl->nht,1 ); + tbl->nht =NULL ; +neigh_hash_free_rcu (&nht->rcu); +}[Severity: Medium]
Is it safe to free these RCU-published members synchronously here?neigh_table_free()callsfree_percpu(tbl->stats),kfree(tbl->phash_buckets)and invokes the RCU callbackneigh_hash_free_rcu()directly, with no grace period in between.At the same time
neigh_destroy()drops the table reference before the neighbour body is retired:neigh_table_put(neigh->tbl); kfree_rcu(neigh, rcu);If that put is the last one, the table (the pointee) is reclaimed immediately while the neighbour holding
neigh->tblsurvives a full grace period.Lockless readers hold no-ref neighbour pointers under
rcu_read_lock()and dereference the table through them, for example in__neigh_event_send():NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);which is reached from the transmit path ip_finish_output2()/ip6_finish_output2() ->
neigh_output()->neigh_event_send().tbl->nhtis read viarcu_dereference()in___neigh_lookup_noref(), andtbl->phash_bucketsis declared__rcuand walked bypneigh_lookup().The same file retires nht only after a grace period:
net/core/neighbour.c:neigh_hash_grow() { ... rcu_assign_pointer(tbl->nht, new_nht); call_rcu(&old_nht->rcu, neigh_hash_free_rcu); ... }Note also that
neigh_table_put()can now reach this free from softirq context, sinceneigh_timer_handler()->neigh_release()->neigh_destroy()runs long afterneigh_table_clear()returned, andneigh_destroy()releases the netdev reference before the table reference, sonetdev_wait_allrefs()does not order it either.Most reader windows do look drained in practice (
cleanup_net()runssynchronize_rcu_expedited()before the pernet exit ops, andunregister_netdevice_many_notify()runssynchronize_net()before the neighbour flush), so I could not show a reader that is still in flight at that instant. Still, would it be better to defer this free with call_rcu()/kfree_rcu(), or to drop the table reference from the neighbour's own RCU callback, especially since the follow-up patch adds kfree(tbl) to this function?Quoted 21 lines
net/core/neighbour.c +0
-6/* * Only called from ndisc_cleanup(), which means this is dead code * because we no longer can unload IPv6 module. @@ -1881,26 +1914,15 @@ void neigh_table_init(struct neigh_table *tbl)int neigh_table_clear (struct neigh_table *tbl) {struct net *net__maybe_unused = &init_net; -struct neigh_hash_table *nht;cancel_delayed_work_sync (&tbl->managed_work);cancel_delayed_work_sync (&tbl->gc_work);timer_shutdown_sync (&tbl->proxy_timer);neigh_ifdown (tbl,NULL ); -DEBUG_NET_WARN_ON_ONCE (atomic_read (&tbl->entries)); -remove_proc_entry (tbl->id, net->proc_net_stat); -free_percpu (tbl->stats); - tbl->stats =NULL ; -net/core/neighbour.c void neigh_table_init(struct neigh_table *tbl) · line 1928
Quoted 10 lines
net/core/neighbour.c +1
-6-kfree (tbl->phash_buckets); - tbl->phash_buckets =NULL ; - - nht =rcu_dereference_protected (tbl->nht,1 ); - tbl->nht =NULL ; -neigh_hash_free_rcu (&nht->rcu); +neigh_table_put (tbl);return 0 ; }[Severity: Low]
This is more of an ordering question.neigh_table_clear()drops the base reference installed byrefcount_set(&tbl->entries, 1)inneigh_table_init(), but at this commit the table is still published: inndisc_cleanup()the call order isneigh_table_clear(&nd_tbl)followed byunregister_pernet_subsys(&ndisc_net_ops).Once the base reference is gone,
neigh_table_entries()returnsrefcount_read()- 1, which is entries - 1 in general and -1 when the last neighbour is gone. That value feeds the__u32uAPI field:.ndtc_entries =
neigh_table_entries(tbl),and the %08x in
neigh_stat_seq_show(), where it would print as ffffffff.Checking the rest of the series, this looks unreachable in the final
state:neigh_table_unregister()sets net->neigh_tables[index] = NULL before callingneigh_table_clear(), the proc entry is removed and drained before the put, andndisc_cleanup()no longer callsneigh_table_clear()at all. At this commit the only caller is theinet6_init()error path that the comment above already describes as dead code.Would it still be worth dropping the base reference only after the table is unpublished, so the biased accessor is never read without it?
Quoted 10 lines
net/core/neighbour.c +1
-1@@ -2275,7 +2297,7 @@ static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,struct ndt_config ndc = { .ndtc_key_len = tbl->key_len, .ndtc_entry_size = tbl->entry_size, - .ndtc_entries =atomic_read (&tbl->entries), + .ndtc_entries =neigh_table_entries (tbl), .ndtc_last_flush =jiffies_to_msecs (flush_delta), .ndtc_last_rand =jiffies_to_msecs (rand_delta), .ndtc_proxy_qlen =READ_ONCE (tbl->proxy_queue.qlen),net/core/neighbour.c static int neigh_stat_seq_show(struct seq_file *seq, void *v) · line 3523
Quoted 8 lines
net/core/neighbour.c +1
-1@@ -3495,7 +3517,7 @@ static int neigh_stat_seq_show(struct seq_file *seq, void *v) seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx " "%08lx %08lx %08lx " "%08lx %08lx %08lx\n", - atomic_read(&tbl->entries), + neigh_table_entries(tbl), st->allocs, st->destroys,--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
PATCH v6 12/15 neighbour: Namespacify neigh_tables.
In reply to Kuniyuki IwashimaNow, neigh_table is ready to be namespacified.
Let's allocate per-netns neigh_table in neigh_table_register() and call neigh_table_init() and neigh_sysctl_register() for it.
proc_create_seq_data() is changed to proc_create_net_data().
Since each netns has its own sysctl knobs, RTM_SETNEIGHTBL can configure the default table param (ifindex==0) and GC parameters per netns.
To avoid potential regression, all the default parameters under /proc/sys/net/ipv{4,6}/neigh/default/, are inherited from init_net by default.
This behaviour is controlled by a new sysctl knob, net.core.neigh_inherit_init_net:
# sysctl net.core.neigh_inherit_init_net net.core.neigh_inherit_init_net = 1 # sysctl net.ipv4.neigh.default.gc_thresh1 net.ipv4.neigh.default.gc_thresh1 = 128 # sysctl net.ipv4.neigh.default.gc_thresh1=129 net.ipv4.neigh.default.gc_thresh1 = 129 # unshare -n sysctl net.ipv4.neigh.default.gc_thresh1 net.ipv4.neigh.default.gc_thresh1 = 129
If it is turned off, all settings are reset in the new netns:
# sysctl net.core.neigh_inherit_init_net=0 net.core.neigh_inherit_init_net = 0 # unshare -n sysctl net.ipv4.neigh.default.gc_thresh1 net.ipv4.neigh.default.gc_thresh1 = 128
The next patch will remove other unnecessary net_eq().
Note that CONFIG_SYSCTL cannot be enabled without CONFIG_PROC_FS.
---
v6:
* Define sysctl_neigh_inherit_init_net in neighbour.c for CONFIG_SYSCTL=n
v4:
* Add sysctl knob, net.core.neigh_inherit_init_net
* Inherit all neigh parms by default
* Move RTM_SETNEIGHTBL changes from patch 14
v3:
* Remove timer_shutdown_sync() in neigh_flush_one() and
rely on tbl->entries to free it in neigh_table_free().
v2:
* panic() when register_pernet_subsys(&arp_net_ops) fails
* Add timer_shutdown_sync() in neigh_flush_one()
--- Documentation/admin-guide/sysctl/net.rst | 14 ++++ include/net/neighbour.h | 4 +- net/core/neighbour.c | 81 ++++++++++++++++-------- net/core/sysctl_net_core.c | 10 +++ net/ipv4/arp.c | 20 ++++-- net/ipv6/ndisc.c | 43 +++++-------- 6 files changed, 112 insertions(+), 60 deletions(-)
Documentation/admin-guide/sysctl/net.rst +14 -0
@@ -413,6 +413,20 @@ new netns has been created. Default : 0 (for compatibility reasons) +neigh_inherit_init_net +---------------------- + +Controls if a new network namespace should inherit all current +settings under /proc/sys/net/ipv{4,6}/neigh/default/. + +By default, the value is set to 1, and all settings are inherited +from init_net. + +If set to 0, both IPv4 and IPv6 settings are reset to their default +values. + +Default : 1 (for compatibility reasons) + txrehash --------
include/net/neighbour.h +2 -2
@@ -31,6 +31,8 @@#include <net/rtnetlink.h> #include <net/neighbour_tables.h> +extern int sysctl_neigh_inherit_init_net; +/* * NUD stands for "neighbor unreachability detection" */
@@ -341,8 +343,6 @@ static inline void neigh_confirm(struct neighbour *n)int neigh_table_register (struct net *net,struct neigh_table *tbl,int index);void neigh_table_unregister (struct net *net,int index); -void neigh_table_init (struct neigh_table *tbl); -int neigh_table_clear (struct neigh_table *tbl);struct neighbour *neigh_lookup (struct neigh_table *tbl,const void *pkey,struct net_device *dev); struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
net/core/neighbour.c +56 -25
157 lines not on this page · raw message
net/core/sysctl_net_core.c +10 -0
@@ -21,6 +21,7 @@#include <net/ip.h> #include <net/sock.h> +#include <net/neighbour.h> #include <net/net_ratelimit.h> #include <net/busy_poll.h> #include <net/pkt_sched.h>
@@ -676,6 +677,15 @@ static struct ctl_table net_core_table[] = { .proc_handler = proc_do_skb_defer_max, .extra1 = SYSCTL_ZERO, }, + { + .procname = "neigh_inherit_init_net", + .data = &sysctl_neigh_inherit_init_net, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, + }, };static const struct ctl_table netns_core_table[] = {
net/ipv4/arp.c +15 -5
@@ -1518,6 +1518,12 @@ static int __net_init arp_net_init(struct net *net) goto err;#ifdef CONFIG_PROC_FS +#ifdef CONFIG_SYSCTL + err =neigh_sysctl_register (NULL , &arp_table (net)->parms,NULL ); +if (err) + goto err_sysctl; +#endif +if (!proc_create_net ("arp" ,0444 , net->proc_net, &arp_seq_ops,sizeof (struct neigh_seq_state ))) { err = -ENOMEM ;
@@ -1529,6 +1535,10 @@ static int __net_init arp_net_init(struct net *net)#ifdef CONFIG_PROC_FS err_proc_create: +#ifdef CONFIG_SYSCTL +neigh_sysctl_unregister (&arp_table (net)->parms); +err_sysctl: +#endif neigh_table_unregister (net,NEIGH_ARP_TABLE );#endif err:
@@ -1538,6 +1548,9 @@ static int __net_init arp_net_init(struct net *net) static void __net_exit arp_net_exit(struct net *net) {remove_proc_entry ("arp" , net->proc_net); +#ifdef CONFIG_SYSCTL +neigh_sysctl_unregister (&arp_table (net)->parms); +#endif neigh_table_unregister (net,NEIGH_ARP_TABLE ); }
@@ -1548,12 +1561,9 @@ static struct pernet_operations arp_net_ops = {void __init arp_init (void ) { -neigh_table_init (&arp_tbl); +if (register_pernet_subsys (&arp_net_ops)) +panic ("Cannot allocate arp table\n" );dev_add_pack (&arp_packet_type); -register_pernet_subsys (&arp_net_ops); -#ifdef CONFIG_SYSCTL -neigh_sysctl_register (NULL , &arp_tbl.parms,NULL ); -#endif register_netdevice_notifier (&arp_netdev_notifier); }
net/ipv6/ndisc.c +15 -28
72 lines not on this page · raw message
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
[email protected]
AI reviewRe: [PATCH v6 net-next 12/15] neighbour: Namespacify neigh_tables.
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Critical: 0 · High: 0 · Medium: 1 · Low: 3
- [Medium] neigh_table_unregister() (net/core/neighbour.c:1963-1969) retracts the table with a plain store `net->neigh_tables[index] =…
- [Low] tools/testing/selftests/net/test_neigh.sh (extern_valid_common) still drives "ip ntable change ... thresh1/2/3, base_reachable" in the…
- [Low] The new per-netns neigh_table allocations are not memcg-
accounted: neigh_table_register() uses kmemdup(tbl, sizeof(*tbl),…
- [Low] neigh_table_inherit() copies init_net's gc_interval, gc_thresh1/2/3 and the whole parms.data[] array with bare READ_ONCE()…
--
neighbour: Namespacify neigh_tables.
This makes struct neigh_table a per-netns object: neigh_table_register() now kmemdup()s the template table, optionally inherits init_net's GC and default parms values, and calls neigh_table_init() plus neigh_sysctl_register() for each namespace. A new knob, net.core.neigh_inherit_init_net, controls the inheritance.
A few questions on the new allocation and teardown paths below.
Quoted 4 lines
net/core/neighbour.c +0
-0
[ ... ]
Quoted 36 lines
net/core/neighbour.c +13
-8@@ -1905,16 +1905,12 @@ static void neigh_table_free(struct neigh_table *tbl) nht =rcu_dereference_protected (tbl->nht,1 ); tbl->nht =NULL ;neigh_hash_free_rcu (&nht->rcu); + +kfree (tbl); } -/* -* Only called from ndisc_cleanup(), which means this is dead code -* because we no longer can unload IPv6 module. -*/ -int neigh_table_clear (struct neigh_table *tbl) +static void neigh_table_clear (struct net *net,struct neigh_table *tbl) { -struct net *net__maybe_unused = &init_net; -cancel_delayed_work_sync (&tbl->managed_work);cancel_delayed_work_sync (&tbl->gc_work);timer_shutdown_sync (&tbl->proxy_timer);@@ -1923,20 +1919,61 @@ int neigh_table_clear(struct neigh_table *tbl)remove_proc_entry (tbl->id, net->proc_net_stat);neigh_table_put (tbl); +} -return 0 ; +int sysctl_neigh_inherit_init_net__read_mostly =1 ; + +static void neigh_table_inherit (struct net *net,struct neigh_table *tbl, +int index) +{ +const struct neigh_table *init_tbl = init_net.neigh_tables[index]; +bool inherit =READ_ONCE (sysctl_neigh_inherit_init_net); +int i; +
net/core/neighbour.c int neigh_table_clear(struct neigh_table *tbl) · line 1942
Quoted 10 lines
net/core/neighbour.c +10
-0+if (net_eq (net, &init_net) || !inherit) +return ; + + tbl->gc_interval =READ_ONCE (init_tbl->gc_interval); + tbl->gc_thresh1 =READ_ONCE (init_tbl->gc_thresh1); + tbl->gc_thresh2 =READ_ONCE (init_tbl->gc_thresh2); + tbl->gc_thresh3 =READ_ONCE (init_tbl->gc_thresh3); + +for (i =0 ; i <NEIGH_VAR_DATA_MAX ; i++) + tbl->parms.data[i] =READ_ONCE (init_tbl->parms.data[i]);
[Severity: Low]
Should this snapshot of init_net's table be taken under init_tbl->lock?
neightbl_set() updates exactly these fields under spin_lock_bh(&tbl->lock), with the comment:
/* * We acquire tbl->lock to be nice to the periodic timers and * make sure they always see a consistent set of values. */ spin_lock_bh(&tbl->lock); ... WRITE_ONCE(tbl->gc_thresh1, nla_get_u32(tb[NDTA_THRESH1]));
An "unshare -n" concurrent with RTM_SETNEIGHTBL on init_net can therefore copy a mix of old and new threshold values into the new namespace.
For what it's worth, the same fields are already written without tbl->lock via the sysctl path that this patch registers per netns (neigh_sysctl_register() points NEIGH_VAR_GC_THRESH1..3 and NEIGH_VAR_GC_INTERVAL .data at &tbl->gc_thresh1..3 and &tbl->gc_interval with plain proc_dointvec* handlers), and nothing enforces an ordering relation among the thresholds, so the resulting combination is one an administrator could configure directly.
net/core/neighbour.c int neigh_table_clear(struct neigh_table *tbl) · line 1953
Quoted 11 lines
net/core/neighbour.c +7
-0}int neigh_table_register (struct net *net,struct neigh_table *tbl,int index) { +int err; + + tbl =kmemdup (tbl,sizeof (*tbl),GFP_KERNEL ); +if (!tbl) { + err = -ENOMEM ; + goto err; + }
[Severity: Low]
Should these per-netns allocations be memcg accounted?
Two tables (ARP and ND) are now created for every network namespace, and namespace creation is reachable by unprivileged users through user namespaces. Besides this kmemdup(), neigh_table_init() allocates neigh_hash_alloc(3), kzalloc(phsize, GFP_KERNEL) for phash_buckets and alloc_percpu(struct neigh_statistics), the last of which scales with nr_cpu_ids for each of the two tables per namespace.
neigh_sysctl_register() in the same file already uses the accounted flag for its per-netns allocation:
t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL_ACCOUNT);
Would GFP_KERNEL_ACCOUNT here and alloc_percpu_gfp(..., GFP_KERNEL_ACCOUNT) in neigh_table_init() be more consistent with that?
[Severity: Low]
Does this break the forced-GC and periodic-GC subtests in tools/testing/selftests/net/test_neigh.sh?
extern_valid_common() still runs the thresholds and counter readback in the initial netns while the entries are created inside $ns1:
run_cmd "ip ntable change name $tbl_name thresh3 10 thresh2 9 thresh1 8"
With per-netns tables, neigh_table_inherit() only snapshots values at registration time, and setup() creates ns1/ns2 before this command runs, so $ns1's thresholds are unaffected. The same applies to the
"ip -j -s ntable show" readback of forced_gc_runs/periodic_gc_runs, which now reports init_net's per-netns tbl->stats rather than $ns1's.
The last patch of this series, "selftest: net: Specify netns for ip ntable in test_neigh.sh.", converts these commands to "ip -n $ns1 ntable ...", so the only residual effect is a two-commit window where the selftest can fail.
Quoted 14 lines
net/core/neighbour.c +11
-0+ +neigh_table_inherit (net, tbl, index); + + err =neigh_table_init (net, tbl); +if (err) + goto free_table; + net->neigh_tables[index] = tbl;return 0 ; + +free_table: +kfree (tbl); +err:
net/core/neighbour.c int neigh_table_clear(struct neigh_table *tbl) · line 1977
Quoted 10 lines
net/core/neighbour.c +4
-0+return err; }void neigh_table_unregister (struct net *net,int index) { +struct neigh_table *tbl = net->neigh_tables[index]; + net->neigh_tables[index] =NULL ; +neigh_table_clear (net, tbl); }
[Severity: Medium]
Is a grace period needed between retracting the slot and freeing the table?
The store is a plain one, and neigh_table_clear() -> neigh_table_put() -> neigh_table_free() now ends in kfree(tbl), so the table can be freed immediately after the slot is cleared.
Readers load the same slot with a plain load under rcu_read_lock() and then dereference it, for example neigh_xmit():
rcu_read_lock();
net = dev_net_rcu(dev);
tbl = net->neigh_tables[index];
if (!tbl) { ... }
...
neigh = __neigh_create(tbl, addr, dev, false);which goes on to take spin_lock_bh(&tbl->lock), rcu_dereference(tbl->nht) and touch the per-cpu tbl->stats. Before this patch the pointer referred to the static arp_tbl/nd_tbl, so a stale read was harmless; now it is freed memory.
Some drivers also cache the pointer in their own state across work items, for instance mlxsw_sp_nexthop4_init() in drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c:
nh->neigh_tbl = arp_table(net);
I was not able to construct a concrete racing reader: cleanup_net() runs synchronize_rcu_expedited() and unregisters all netdevs before the subsys exit ops, and the netlink and /proc readers hold a netns reference. But the preceding patch, "neighbour: Remove __rcu from neigh_tables[]", justified dropping the RCU annotation with "neigh_table cannot be registered dynamically", which no longer holds once the slots are published and freed at runtime.
Would an RCU-annotated store plus kfree_rcu() of the table remove the dependence on that ordering?
Quoted 2 lines
net/core/neighbour.c +0
-0static struct neigh_table *neigh_find_table (struct net *net,int family)
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
Re: [PATCH v6 net-next 12/15] neighbour: Namespacify neigh_tables.
Quoted 8 lines · On Thu, Sep 3, 2026 at 8:10 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Critical: 0 · High: 0 · Medium: 1 · Low: 3
- [Medium]
neigh_table_unregister()(net/core/neighbour.c:1963-1969) retracts the table with a plain store `net->neigh_tables[index] =…
This is false positive because at this moment there should be no reader of net->neigh_tables[].
Quoted 2 lines
- [Low]
tools/testing/selftests/net/test_neigh.sh(extern_valid_common) still drives "ip ntable change ... thresh1/2/3,base_reachable" in the…
This is updated in the selftest patch.
Quoted 2 lines
- [Low] The new per-netns
neigh_tableallocations are not memcg-
accounted:neigh_table_register()uses kmemdup(tbl, sizeof(*tbl),…
This could be follow-up if we care.
Also, AI missed neigh_hash_grow().
Quoted 2 lines
- [Low]
neigh_table_inherit()copies init_net'sgc_interval, gc_thresh1/2/3 and the whole parms.data[] array with bare READ_ONCE()…
No need to lock init_net's neigh_table lock, READ_ONCE() is sufficient.
Quoted 210 lines
--
neighbour: Namespacify
neigh_tables.This makes struct
neigh_tablea per-netns object:neigh_table_register()now kmemdup()s the template table, optionally inherits init_net's GC and default parms values, and callsneigh_table_init()plusneigh_sysctl_register()for each namespace. A new knob, net.core.neigh_inherit_init_net, controls the inheritance.A few questions on the new allocation and teardown paths below.
Quoted 4 lines
net/core/neighbour.c +0
-0[ ... ]
Quoted 36 lines
net/core/neighbour.c +13
-8@@ -1905,16 +1905,12 @@ static void neigh_table_free(struct neigh_table *tbl) nht =rcu_dereference_protected (tbl->nht,1 ); tbl->nht =NULL ;neigh_hash_free_rcu (&nht->rcu); + +kfree (tbl); } -/* -* Only called from ndisc_cleanup(), which means this is dead code -* because we no longer can unload IPv6 module. -*/ -int neigh_table_clear (struct neigh_table *tbl) +static void neigh_table_clear (struct net *net,struct neigh_table *tbl) { -struct net *net__maybe_unused = &init_net; -cancel_delayed_work_sync (&tbl->managed_work);cancel_delayed_work_sync (&tbl->gc_work);timer_shutdown_sync (&tbl->proxy_timer);@@ -1923,20 +1919,61 @@ int neigh_table_clear(struct neigh_table *tbl)remove_proc_entry (tbl->id, net->proc_net_stat);neigh_table_put (tbl); +} -return 0 ; +int sysctl_neigh_inherit_init_net__read_mostly =1 ; + +static void neigh_table_inherit (struct net *net,struct neigh_table *tbl, +int index) +{ +const struct neigh_table *init_tbl = init_net.neigh_tables[index]; +bool inherit =READ_ONCE (sysctl_neigh_inherit_init_net); +int i; +net/core/neighbour.c int neigh_table_clear(struct neigh_table *tbl) · line 1942
Quoted 10 lines
net/core/neighbour.c +10
-0+if (net_eq (net, &init_net) || !inherit) +return ; + + tbl->gc_interval =READ_ONCE (init_tbl->gc_interval); + tbl->gc_thresh1 =READ_ONCE (init_tbl->gc_thresh1); + tbl->gc_thresh2 =READ_ONCE (init_tbl->gc_thresh2); + tbl->gc_thresh3 =READ_ONCE (init_tbl->gc_thresh3); + +for (i =0 ; i <NEIGH_VAR_DATA_MAX ; i++) + tbl->parms.data[i] =READ_ONCE (init_tbl->parms.data[i]);[Severity: Low]
Should this snapshot of init_net's table be taken underinit_tbl->lock?
neightbl_set()updates exactly these fields underspin_lock_bh(&tbl->lock), with the comment:/* * We acquire tbl->lock to be nice to the periodic timers and * make sure they always see a consistent set of values. */ spin_lock_bh(&tbl->lock); ... WRITE_ONCE(tbl->gc_thresh1, nla_get_u32(tb[NDTA_THRESH1]));An "unshare -n" concurrent with
RTM_SETNEIGHTBLoninit_netcan therefore copy a mix of old and new threshold values into the new namespace.For what it's worth, the same fields are already written without
tbl->lockvia the sysctl path that this patch registers per netns (neigh_sysctl_register()points NEIGH_VAR_GC_THRESH1..3 andNEIGH_VAR_GC_INTERVAL.data at &tbl->gc_thresh1..3 and&tbl->gc_intervalwith plain proc_dointvec* handlers), and nothing enforces an ordering relation among the thresholds, so the resulting combination is one an administrator could configure directly.net/core/neighbour.c int neigh_table_clear(struct neigh_table *tbl) · line 1953
Quoted 11 lines
net/core/neighbour.c +7
-0}int neigh_table_register (struct net *net,struct neigh_table *tbl,int index) { +int err; + + tbl =kmemdup (tbl,sizeof (*tbl),GFP_KERNEL ); +if (!tbl) { + err = -ENOMEM ; + goto err; + }[Severity: Low]
Should these per-netns allocations be memcg accounted?Two tables (ARP and ND) are now created for every network namespace, and namespace creation is reachable by unprivileged users through user namespaces. Besides this
kmemdup(),neigh_table_init()allocatesneigh_hash_alloc(3),kzalloc(phsize, GFP_KERNEL)forphash_bucketsandalloc_percpu(struct neigh_statistics), the last of which scales withnr_cpu_idsfor each of the two tables per namespace.
neigh_sysctl_register()in the same file already uses the accounted flag for its per-netns allocation:t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL_ACCOUNT);Would
GFP_KERNEL_ACCOUNThere andalloc_percpu_gfp(..., GFP_KERNEL_ACCOUNT)inneigh_table_init()be more consistent with that?[Severity: Low]
Does this break the forced-GC and periodic-GC subtests intools/testing/selftests/net/test_neigh.sh?
extern_valid_common()still runs the thresholds and counter readback in the initial netns while the entries are created inside $ns1:
run_cmd"ip ntable change name $tbl_name thresh3 10 thresh2 9 thresh1 8"With per-netns tables,
neigh_table_inherit()only snapshots values at registration time, andsetup()creates ns1/ns2 before this command runs, so $ns1's thresholds are unaffected. The same applies to the
"ip -j -s ntable show" readback of forced_gc_runs/periodic_gc_runs, which now reports init_net's per-netnstbl->statsrather than $ns1's.The last patch of this series, "selftest: net: Specify netns for ip ntable in
test_neigh.sh.", converts these commands to "ip -n $ns1 ntable ...", so the only residual effect is a two-commit window where the selftest can fail.Quoted 14 lines
net/core/neighbour.c +11
-0+ +neigh_table_inherit (net, tbl, index); + + err =neigh_table_init (net, tbl); +if (err) + goto free_table; + net->neigh_tables[index] = tbl;return 0 ; + +free_table: +kfree (tbl); +err:net/core/neighbour.c int neigh_table_clear(struct neigh_table *tbl) · line 1977
Quoted 10 lines
net/core/neighbour.c +4
-0+return err; }void neigh_table_unregister (struct net *net,int index) { +struct neigh_table *tbl = net->neigh_tables[index]; + net->neigh_tables[index] =NULL ; +neigh_table_clear (net, tbl); }[Severity: Medium]
Is a grace period needed between retracting the slot and freeing the table?The store is a plain one, and
neigh_table_clear()->neigh_table_put()->neigh_table_free()now ends in kfree(tbl), so the table can be freed immediately after the slot is cleared.Readers load the same slot with a plain load under
rcu_read_lock()and then dereference it, for exampleneigh_xmit():rcu_read_lock(); net = dev_net_rcu(dev); tbl = net->neigh_tables[index]; if (!tbl) { ... } ... neigh = __neigh_create(tbl, addr, dev, false);which goes on to take
spin_lock_bh(&tbl->lock),rcu_dereference(tbl->nht)and touch the per-cputbl->stats. Before this patch the pointer referred to the static arp_tbl/nd_tbl, so a stale read was harmless; now it is freed memory.Some drivers also cache the pointer in their own state across work items, for instance
mlxsw_sp_nexthop4_init()indrivers/net/ethernet/mellanox/mlxsw/spectrum_router.c:nh->neigh_tbl = arp_table(net);I was not able to construct a concrete racing reader:
cleanup_net()runssynchronize_rcu_expedited()and unregisters all netdevs before the subsys exit ops, and the netlink and /proc readers hold a netns reference. But the preceding patch, "neighbour: Remove__rcufrom neigh_tables[]", justified dropping the RCU annotation with "neigh_tablecannot be registered dynamically", which no longer holds once the slots are published and freed at runtime.Would an RCU-annotated store plus
kfree_rcu()of the table remove the dependence on that ordering?net/core/neighbour.c int neigh_table_clear(struct neigh_table *tbl) · line 1979
Quoted 2 lines
net/core/neighbour.c +0
-0static struct neigh_table *neigh_find_table (struct net *net,int family)[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
PATCH v6 13/15 neighbour: Don't store net in struct pneigh_entry.
In reply to Kuniyuki Iwashimaneigh_table is now per-netns, so struct pneigh_entry does not need to store a net pointer.
Let's remove it and net comparison for pneigh_entry.
We no longer need to pass net to pneigh_create(), pneigh_delete(), and pneigh_lookup().
---
v2:
* Split from the next patch
* Remove net comparison in pneigh_dump_table()
* Remove net arg of pneigh_create(), pneigh_delete(), and pneigh_lookup()
--- include/net/neighbour.h | 12 +++------ net/core/neighbour.c | 55 ++++++++++++++--------------------------- net/ipv4/arp.c | 6 ++--- net/ipv6/ip6_output.c | 2 +- net/ipv6/ndisc.c | 4 +-- 5 files changed, 27 insertions(+), 52 deletions(-)
include/net/neighbour.h +3 -9
@@ -181,7 +181,6 @@ struct neigh_ops {struct pneigh_entry {struct pneigh_entry __rcu *next; -possible_net_t net;struct net_device *dev;netdevice_tracker dev_tracker;union {
@@ -391,19 +390,14 @@ static inline void neigh_set_reach_time(struct neigh_parms *p)void pneigh_enqueue (struct neigh_table *tbl,struct neigh_parms *p,struct sk_buff *skb); -struct pneigh_entry *pneigh_lookup (struct neigh_table *tbl,struct net *net, +struct pneigh_entry *pneigh_lookup (struct neigh_table *tbl,const void *key,struct net_device *dev); -int pneigh_create (struct neigh_table *tbl,struct net *net,const void *key, +int pneigh_create (struct neigh_table *tbl,const void *key,struct net_device *dev,u32 flags,u8 protocol,bool permanent); -int pneigh_delete (struct neigh_table *tbl,struct net *net,const void *key, +int pneigh_delete (struct neigh_table *tbl,const void *key,struct net_device *dev); -static inline struct net *pneigh_net (const struct pneigh_entry *pneigh) -{ -return read_pnet (&pneigh->net); -} -void neigh_app_ns (struct neighbour *n);void neigh_for_each (struct neigh_table *tbl,void (*cb)(struct neighbour *,void *),void *cookie);
net/core/neighbour.c +18 -37
174 lines not on this page · raw message
net/ipv4/arp.c +3 -3
@@ -868,7 +868,7 @@ static int arp_process(struct net *net, struct sock *sk, struct sk_buff *skb) (arp_fwd_proxy (in_dev, dev, rt) ||arp_fwd_pvlan (in_dev, dev, rt, sip, tip) || (rt->dst.dev != dev && - pneigh_lookup(tbl, net, &tip, dev)))) { + pneigh_lookup(tbl, &tip, dev)))) { n = neigh_event_ns(tbl, sha, &sip, dev);if (n)neigh_release (n);
@@ -1094,7 +1094,7 @@ static int arp_req_set_public(struct net *net, struct arpreq *r,if (mask) {__be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr; -return pneigh_create (tbl, net, &ip, dev,0 ,0 , false); +return pneigh_create (tbl, &ip, dev,0 ,0 , false); }return arp_req_set_proxy (net, dev,1 );
@@ -1243,7 +1243,7 @@ static int arp_req_delete_public(struct net *net, struct arpreq *r,if (mask) {__be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr; -return pneigh_delete (tbl, net, &ip, dev); +return pneigh_delete (tbl, &ip, dev); }return arp_req_set_proxy (net, dev,0 );
net/ipv6/ip6_output.c +1 -1
@@ -583,7 +583,7 @@ int ip6_forward(struct sk_buff *skb)/* XXX: idev->cnf.proxy_ndp? */ if (READ_ONCE (net->ipv6.devconf_all->proxy_ndp) && -pneigh_lookup (nd_table (net), net, &hdr->daddr, skb->dev)) { +pneigh_lookup (nd_table (net), &hdr->daddr, skb->dev)) {int proxied =ip6_forward_proxy_check (skb); hdr =ipv6_hdr (skb);
net/ipv6/ndisc.c +2 -2
@@ -771,7 +771,7 @@ static int pndisc_is_router(const void *pkey,struct pneigh_entry *n;int ret =-1 ; - n =pneigh_lookup (nd_table (net), net, pkey, dev); + n =pneigh_lookup (nd_table (net), pkey, dev);if (n) ret = !!(READ_ONCE (n->flags) &NTF_ROUTER );
@@ -1103,7 +1103,7 @@ static enum skb_drop_reason ndisc_recv_na(struct sk_buff *skb)if (lladdr && !memcmp (lladdr, dev->dev_addr, dev->addr_len) &&READ_ONCE (net->ipv6.devconf_all->forwarding) &&READ_ONCE (net->ipv6.devconf_all->proxy_ndp) && -pneigh_lookup (tbl, net, &msg->target, dev)) { +pneigh_lookup (tbl, &msg->target, dev)) {/* XXX: idev->cnf.proxy_ndp */ goto out; }
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
PATCH v6 14/15 neighbour: Remove unnecessary net_eq().
In reply to Kuniyuki Iwashimaneigh_table is now per-netns, and net_eq() is no longer needed.
Let's remove them.
---
v2: Split pneigh part into the prev patch
--- net/core/neighbour.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)
net/core/neighbour.c +4 -12
@@ -2662,9 +2662,6 @@ static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb) nidx =0 ; p =list_next_entry (&tbl->parms, list);list_for_each_entry_from_rcu (p, &tbl->parms_list, list) { -if (!net_eq (neigh_parms_net (p), net)) -continue ; -if (nidx < neigh_skip) goto next;
@@ -2842,12 +2839,11 @@ static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,struct netlink_callback *cb, struct neigh_dump_filter *filter) { - struct net *net = sock_net(skb->sk); -struct neighbour *n; -int err =0 , h, s_h = cb->args[1 ]; int idx, s_idx = idx = cb->args[2]; -struct neigh_hash_table *nht; +int err =0 , h, s_h = cb->args[1 ];unsigned int flags =NLM_F_MULTI ; +struct neigh_hash_table *nht; +struct neighbour *n;if (filter->dev_idx || filter->master_idx) flags |=NLM_F_DUMP_FILTERED ;
@@ -2859,7 +2855,7 @@ static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, s_idx =0 ; idx =0 ;neigh_for_each_in_bucket_rcu (n, &nht->hash_heads[h]) { -if (idx < s_idx || !net_eq (dev_net (n->dev), net)) +if (idx < s_idx) goto next;if (neigh_ifindex_filtered (n->dev, filter->dev_idx) ||neigh_master_filtered (n->dev, filter->master_idx))
@@ -3264,10 +3260,6 @@ static struct neighbour *neigh_get_valid(struct seq_file *seq, 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 ;
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
PATCH v6 15/15 selftest: net: Specify netns for ip ntable in test_neigh.sh.
In reply to Kuniyuki Iwashimatest_neigh.sh configures the following attributes in the root netns, but now these can be set per netns.
* NDTA_THRESH1 / NDTA_THRESH2 / NDTA_THRESH3
* NDTPA_BASE_REACHABLE_TIME
Let's specify netns for "ip ntable".
---
v3: Remove stale comments
--- tools/testing/selftests/net/test_neigh.sh | 41 ++++++++++++----------- 1 file changed, 22 insertions(+), 19 deletions(-)
tools/testing/selftests/net/test_neigh.sh +22 -19
@@ -240,9 +240,6 @@ extern_valid_common() # Check that an "extern_valid" entry survives a forced garbage # collection. Add an entry, wait 5 seconds and add more entries than # "thresh3" so that forced garbage collection will run. - # - # Note that the garbage collection thresholds are global resources and - # that changes in the initial namespace affect all the namespaces. local forced_gc_runs_t0 local forced_gc_runs_t1 local orig_thresh1
@@ -250,18 +247,23 @@ extern_valid_common() local orig_thresh3 run_cmd "ip -n $ns1 neigh flush dev veth0" - orig_thresh1=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh1")) | .["thresh1"]') - orig_thresh2=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh2")) | .["thresh2"]') - orig_thresh3=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh3")) | .["thresh3"]') - run_cmd "ip ntable change name $tbl_name thresh3 10 thresh2 9 thresh1 8" + orig_thresh1=$(ip -n "$ns1" -j ntable show name "$tbl_name" | \ + jq '.[] | select(has("thresh1")) | .["thresh1"]') + orig_thresh2=$(ip -n "$ns1" -j ntable show name "$tbl_name" | \ + jq '.[] | select(has("thresh2")) | .["thresh2"]') + orig_thresh3=$(ip -n "$ns1" -j ntable show name "$tbl_name" | \ + jq '.[] | select(has("thresh3")) | .["thresh3"]') + run_cmd "ip -n $ns1 ntable change name $tbl_name thresh3 10 thresh2 9 thresh1 8" run_cmd "ip -n $ns1 neigh add $ip_addr lladdr $mac nud stale dev veth0 extern_valid" run_cmd "ip -n $ns1 neigh add ${subnet}3 lladdr $mac nud stale dev veth0" run_cmd "sleep 5" - forced_gc_runs_t0=$(ip -j -s ntable show name "$tbl_name" | jq '.[] | select(has("forced_gc_runs")) | .["forced_gc_runs"]') + forced_gc_runs_t0=$(ip -n "$ns1" -j -s ntable show name "$tbl_name" | \ + jq '.[] | select(has("forced_gc_runs")) | .["forced_gc_runs"]') for i in {1..20}; do run_cmd "ip -n $ns1 neigh add ${subnet}$((i + 4)) nud none dev veth0" done - forced_gc_runs_t1=$(ip -j -s ntable show name "$tbl_name" | jq '.[] | select(has("forced_gc_runs")) | .["forced_gc_runs"]') + forced_gc_runs_t1=$(ip -n "$ns1" -j -s ntable show name "$tbl_name" | \ + jq '.[] | select(has("forced_gc_runs")) | .["forced_gc_runs"]') if [[ $forced_gc_runs_t1 -eq $forced_gc_runs_t0 ]]; then check_err 1 "Forced garbage collection did not run" fi
@@ -272,7 +274,8 @@ extern_valid_common() log_test "$af_str \"extern_valid\" flag: Forced garbage collection" - run_cmd "ip ntable change name $tbl_name thresh3 $orig_thresh3 thresh2 $orig_thresh2 thresh1 $orig_thresh1" + run_cmd "ip -n $ns1 ntable change name $tbl_name \ + thresh3 $orig_thresh3 thresh2 $orig_thresh2 thresh1 $orig_thresh1" RET=0
@@ -284,10 +287,6 @@ extern_valid_common() # collection. Add an "extern_valid" entry, add more than "thresh1" # regular entries, wait "base_reachable" (longer than "gc_stale") # seconds and check that the "extern_valid" entry was not deleted. - # - # Note that the garbage collection thresholds and "base_reachable" are - # global resources and that changes in the initial namespace affect all - # the namespaces. local periodic_gc_runs_t0 local periodic_gc_runs_t1 local orig_base_reachable
@@ -296,10 +295,13 @@ extern_valid_common() local timeout run_cmd "ip -n $ns1 neigh flush dev veth0" - orig_thresh1=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh1")) | .["thresh1"]') - orig_base_reachable=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh1")) | .["base_reachable"]') - run_cmd "ip ntable change name $tbl_name thresh1 10 base_reachable $base_reachable" - orig_gc_stale=$(ip -n "$ns1" -j ntable show name "$tbl_name" dev veth0 | jq '.[]["gc_stale"]') + orig_thresh1=$(ip -n "$ns1" -j ntable show name "$tbl_name" | \ + jq '.[] | select(has("thresh1")) | .["thresh1"]') + orig_base_reachable=$(ip -n "$ns1" -j ntable show name "$tbl_name" | \ + jq '.[] | select(has("thresh1")) | .["base_reachable"]') + run_cmd "ip -n $ns1 ntable change name $tbl_name thresh1 10 base_reachable $base_reachable" + orig_gc_stale=$(ip -n "$ns1" -j ntable show name "$tbl_name" dev veth0 | \ + jq '.[]["gc_stale"]') run_cmd "ip -n $ns1 ntable change name $tbl_name dev veth0 gc_stale 1000" run_cmd "ip -n $ns1 neigh add $ip_addr lladdr $mac nud stale dev veth0 extern_valid" run_cmd "ip -n $ns1 neigh add ${subnet}3 lladdr $mac nud stale dev veth0"
@@ -325,7 +327,8 @@ extern_valid_common() log_test "$af_str \"extern_valid\" flag: Periodic garbage collection" run_cmd "ip -n $ns1 ntable change name $tbl_name dev veth0 gc_stale $orig_gc_stale" - run_cmd "ip ntable change name $tbl_name thresh1 $orig_thresh1 base_reachable $orig_base_reachable" + run_cmd "ip -n $ns1 ntable change name $tbl_name \ + thresh1 $orig_thresh1 base_reachable $orig_base_reachable" } extern_valid_ipv4()
Signature · 1 line
2.55.0.970.g62bdec98f9-goog
[email protected]
AI reviewRe: [PATCH v6 net-next 15/15] selftest: net: Specify netns for ip ntable in test_neigh.sh.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Critical: 0 · High: 0 · Medium: 1 · Low: 0
- [Medium] The series namespacifies the neighbour tables (commit "neighbour: Namespacify neigh_tables."), and this patch is the…
--
selftest: net: Specify netns for ip ntable in test_neigh.sh.
Now that the neighbour tables are per netns, this patch converts the "ip ntable" invocations in test_neigh.sh to "ip -n $ns1 ntable" so the thresholds and base_reachable_time are set in the namespace the test actually uses, and drops the comments that described those knobs as global resources.
Quoted 19 lines
tools/testing/selftests/net/test_neigh.sh +0
-4@@ -240,9 +240,6 @@ extern_valid_common() # Check that an "extern_valid" entry survives a forced garbage # collection. Add an entry, wait 5 seconds and add more entries than # "thresh3" so that forced garbage collection will run. - # - # Note that the garbage collection thresholds are global resources and - # that changes in the initial namespace affect all the namespaces. local forced_gc_runs_t0 local forced_gc_runs_t1 local orig_thresh1@@ -250,18 +247,23 @@ extern_valid_common() local orig_thresh3 run_cmd "ip -n $ns1 neigh flush dev veth0" - orig_thresh1=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh1")) | .["thresh1"]')
tools/testing/selftests/net/test_neigh.sh extern_valid_common() · line 256
Quoted 10 lines
tools/testing/selftests/net/test_neigh.sh +7
-3- orig_thresh2=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh2")) | .["thresh2"]') - orig_thresh3=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh3")) | .["thresh3"]') - run_cmd "ip ntable change name $tbl_name thresh3 10 thresh2 9 thresh1 8" + orig_thresh1=$(ip -n "$ns1" -j ntable show name "$tbl_name" | \ + jq '.[] | select(has("thresh1")) | .["thresh1"]') + orig_thresh2=$(ip -n "$ns1" -j ntable show name "$tbl_name" | \ + jq '.[] | select(has("thresh2")) | .["thresh2"]') + orig_thresh3=$(ip -n "$ns1" -j ntable show name "$tbl_name" | \ + jq '.[] | select(has("thresh3")) | .["thresh3"]') + run_cmd "ip -n $ns1 ntable change name $tbl_name thresh3 10 thresh2 9 thresh1 8"
[Severity: Medium]
This hunk moves test_neigh.sh over to the per-netns form, but should tools/testing/selftests/net/netfilter/nft_nat_zones.sh get the same treatment in this series? That script tunes the same knobs from the root netns:
tools/testing/selftests/net/netfilter/nft_nat_zones.sh {
...
setup_ns gw srv
...
sysctl -q net.ipv6.neigh.default.gc_thresh1=512 2>/dev/null
sysctl -q net.ipv6.neigh.default.gc_thresh2=1024 2>/dev/null
sysctl -q net.ipv6.neigh.default.gc_thresh3=4096 2>/dev/null
sysctl -q net.ipv4.neigh.default.gc_thresh1=512 2>/dev/null
sysctl -q net.ipv4.neigh.default.gc_thresh2=1024 2>/dev/null
sysctl -q net.ipv4.neigh.default.gc_thresh3=4096 2>/dev/null
...
}With the namespacified tables, neigh_sysctl_register() points those ctl entries at the per-netns table and registers them in that netns:
net/core/neighbour.c:neigh_sysctl_register() {
...
t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
...
}and the only inheritance from init_net is a one-time snapshot taken when the namespace's table is registered:
net/core/neighbour.c:neigh_table_inherit() {
...
tbl->gc_thresh1 = READ_ONCE(init_tbl->gc_thresh1);
tbl->gc_thresh2 = READ_ONCE(init_tbl->gc_thresh2);
tbl->gc_thresh3 = READ_ONCE(init_tbl->gc_thresh3);
...
}Since nft_nat_zones.sh runs "setup_ns gw srv" before writing the raised values, do the gw and srv namespaces still end up with the compiled-in defaults instead of 512/1024/4096? If so, the raised thresholds are a no-op for the gw namespace that actually holds the per-veth neighbour entries, and the gate in neigh_periodic_work():
net/core/neighbour.c:neigh_periodic_work() {
...
if (neigh_table_entries(tbl) < READ_ONCE(tbl->gc_thresh1))
goto out;
...
}applies with the unraised limit, which can make the nft counter checks come up short once the entry count grows (the script's own comment suggests raising maxclients to 2000).
The cleanup() function in the same script restores only the root-netns values, which would likewise no longer affect the namespaces that ran the test.
Would moving the sysctl writes above "setup_ns gw srv", or issuing them with "ip netns exec \"$gw\" sysctl ...", be the intended conversion here?
[ ... ]
Quoted 11 lines
tools/testing/selftests/net/test_neigh.sh +0
-4@@ -284,10 +287,6 @@ extern_valid_common() # collection. Add an "extern_valid" entry, add more than "thresh1" # regular entries, wait "base_reachable" (longer than "gc_stale") # seconds and check that the "extern_valid" entry was not deleted. - # - # Note that the garbage collection thresholds and "base_reachable" are - # global resources and that changes in the initial namespace affect all - # the namespaces. local periodic_gc_runs_t0 local periodic_gc_runs_t1 local orig_base_reachable
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
Re: [PATCH v6 net-next 15/15] selftest: net: Specify netns for ip ntable in test_neigh.sh.
Quoted 8 lines · On Thu, Sep 3, 2026 at 8:10 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Critical: 0 · High: 0 · Medium: 1 · Low: 0
- [Medium] The series namespacifies the neighbour tables (commit "neighbour: Namespacify
neigh_tables."), and this patch is the…
I'll follow up on nft_nat_zones.sh, the series already has 15 patches.
Quoted 124 lines
--
selftest: net: Specify netns for ip ntable in
test_neigh.sh.Now that the neighbour tables are per netns, this patch converts the "ip ntable" invocations in
test_neigh.shto "ip -n $ns1 ntable" so the thresholds andbase_reachable_timeare set in the namespace the test actually uses, and drops the comments that described those knobs as global resources.Quoted 19 lines
tools/testing/selftests/net/test_neigh.sh +0
-4@@ -240,9 +240,6 @@ extern_valid_common() # Check that an "extern_valid" entry survives a forced garbage # collection. Add an entry, wait 5 seconds and add more entries than # "thresh3" so that forced garbage collection will run. - # - # Note that the garbage collection thresholds are global resources and - # that changes in the initial namespace affect all the namespaces. local forced_gc_runs_t0 local forced_gc_runs_t1 local orig_thresh1@@ -250,18 +247,23 @@ extern_valid_common() local orig_thresh3 run_cmd "ip -n $ns1 neigh flush dev veth0" - orig_thresh1=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh1")) | .["thresh1"]')tools/testing/selftests/net/test_neigh.sh extern_valid_common() · line 256
Quoted 10 lines
tools/testing/selftests/net/test_neigh.sh +7
-3- orig_thresh2=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh2")) | .["thresh2"]') - orig_thresh3=$(ip -j ntable show name "$tbl_name" | jq '.[] | select(has("thresh3")) | .["thresh3"]') - run_cmd "ip ntable change name $tbl_name thresh3 10 thresh2 9 thresh1 8" + orig_thresh1=$(ip -n "$ns1" -j ntable show name "$tbl_name" | \ + jq '.[] | select(has("thresh1")) | .["thresh1"]') + orig_thresh2=$(ip -n "$ns1" -j ntable show name "$tbl_name" | \ + jq '.[] | select(has("thresh2")) | .["thresh2"]') + orig_thresh3=$(ip -n "$ns1" -j ntable show name "$tbl_name" | \ + jq '.[] | select(has("thresh3")) | .["thresh3"]') + run_cmd "ip -n $ns1 ntable change name $tbl_name thresh3 10 thresh2 9 thresh1 8"[Severity: Medium]
This hunk movestest_neigh.shover to the per-netns form, but shouldtools/testing/selftests/net/netfilter/nft_nat_zones.shget the same treatment in this series? That script tunes the same knobs from the root netns:tools/testing/selftests/net/netfilter/nft_nat_zones.sh { ... setup_ns gw srv ... sysctl -q net.ipv6.neigh.default.gc_thresh1=512 2>/dev/null sysctl -q net.ipv6.neigh.default.gc_thresh2=1024 2>/dev/null sysctl -q net.ipv6.neigh.default.gc_thresh3=4096 2>/dev/null sysctl -q net.ipv4.neigh.default.gc_thresh1=512 2>/dev/null sysctl -q net.ipv4.neigh.default.gc_thresh2=1024 2>/dev/null sysctl -q net.ipv4.neigh.default.gc_thresh3=4096 2>/dev/null ... }With the namespacified tables,
neigh_sysctl_register()points those ctl entries at the per-netns table and registers them in that netns:net/core/neighbour.c:neigh_sysctl_register() { ... t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1; t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2; t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3; ... }and the only inheritance from
init_netis a one-time snapshot taken when the namespace's table is registered:net/core/neighbour.c:neigh_table_inherit() { ... tbl->gc_thresh1 = READ_ONCE(init_tbl->gc_thresh1); tbl->gc_thresh2 = READ_ONCE(init_tbl->gc_thresh2); tbl->gc_thresh3 = READ_ONCE(init_tbl->gc_thresh3); ... }Since
nft_nat_zones.shruns "setup_nsgw srv" before writing the raised values, do the gw and srv namespaces still end up with the compiled-in defaults instead of 512/1024/4096? If so, the raised thresholds are a no-op for the gw namespace that actually holds the per-veth neighbour entries, and the gate inneigh_periodic_work():net/core/neighbour.c:neigh_periodic_work() { ... if (neigh_table_entries(tbl) < READ_ONCE(tbl->gc_thresh1)) goto out; ... }applies with the unraised limit, which can make the nft counter checks come up short once the entry count grows (the script's own comment suggests raising maxclients to 2000).
The
cleanup()function in the same script restores only the root-netns values, which would likewise no longer affect the namespaces that ran the test.Would moving the sysctl writes above "
setup_nsgw srv", or issuing them with "ip netns exec \"$gw\" sysctl ...", be the intended conversion here?[ ... ]
tools/testing/selftests/net/test_neigh.sh extern_valid_common() · line 292
Quoted 11 lines
tools/testing/selftests/net/test_neigh.sh +0
-4@@ -284,10 +287,6 @@ extern_valid_common() # collection. Add an "extern_valid" entry, add more than "thresh1" # regular entries, wait "base_reachable" (longer than "gc_stale") # seconds and check that the "extern_valid" entry was not deleted. - # - # Note that the garbage collection thresholds and "base_reachable" are - # global resources and that changes in the initial namespace affect all - # the namespaces. local periodic_gc_runs_t0 local periodic_gc_runs_t1 local orig_base_reachable[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260902203722.926528-1-kuniyu%40google.com
Quoted 48 lines · Kuniyuki Iwashima
The neighbour subsystem is almost ready to drop RTNL.
However, the control paths are serialised by the global per-table lock.
This series converts
arp_tblandnd_tblto per-netns table.With the series,
/proc/sys/net/ipv{4,6}/neigh/default/can be configured per netns, which was only configurable ininit_net.To avoid potential regression, all the settings are inherited from
init_netby default, and this behaviour is controlled by a new sysctl knob, net.core.neigh_inherit_init_net:# sysctl net.core.neigh_inherit_init_net net.core.neigh_inherit_init_net = 1 # sysctl net.ipv4.neigh.default.gc_thresh1 net.ipv4.neigh.default.gc_thresh1 = 128 # sysctl net.ipv4.neigh.default.gc_thresh1=129 net.ipv4.neigh.default.gc_thresh1 = 129 # unshare -n sysctl net.ipv4.neigh.default.gc_thresh1 net.ipv4.neigh.default.gc_thresh1 = 129If it is turned off, all settings are reset in the new netns:
# sysctl net.core.neigh_inherit_init_net=0 net.core.neigh_inherit_init_net = 0 # unshare -n sysctl net.ipv4.neigh.default.gc_thresh1 net.ipv4.neigh.default.gc_thresh1 = 128Series overview:
Patch 1 deflakes
test_neigh.sh.Patch 2 ~ 3 are misc cleanup.
Patch 4 ~ 7 store arp_tbl/nd_tbl to net->neigh_tables[] and remove the global neigh_tables[].
Patch 8 ~ 9 replace the direct access to arp_tbl/nd_tbl to net->neigh_tables[] using new helpers.
Patch 10 ~ 12 finally replace the global table with per-netns table.
Patch 13 ~ 14 clean up unnecessary
net_eq().
Quoted 7 lines
Patch 15 updates
test_neigh.sh.Note that some buggy drivers access
nd_tblwithout checkingdisable_ipv6_mod, so nd_tbl's extern definition is still left.After this series, I will follow up on two suggestions by Ido:
https://lore.kernel.org/netdev/20260902143023.GA3966681@shredder/
Thanks!
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Wed, 2 Sep 2026 20:36:41 +0000 you wrote:
The neighbour subsystem is almost ready to drop RTNL.
However, the control paths are serialised by the global per-table lock.
This series converts
arp_tblandnd_tblto per-netns table.[...]
Here is the summary with links:
- [v6,net-next,01/15] selftest: net: Deflake Periodic GC test in test_neigh.sh.
https://git.kernel.org/netdev/net-next/c/cc9bccbad049 - [v6,net-next,02/15] neighbour: Remove __neigh_for_each_release(). https://git.kernel.org/netdev/net-next/c/985f6522d856 - [v6,net-next,03/15] neighbour: Remove lock dance for neigh_update_{gc,managed}_list(). https://git.kernel.org/netdev/net-next/c/2f9ab22897bf - [v6,net-next,04/15] neighbour: Remove unnecessary EXPORT_SYMBOL(). https://git.kernel.org/netdev/net-next/c/ffc5821e8ab8 - [v6,net-next,05/15] neighbour: Remove __rcu from neigh_tables[]. https://git.kernel.org/netdev/net-next/c/cbc2fbb685f5 - [v6,net-next,06/15] neighbour: Store arp_tbl and nd_tbl in net->neigh_tables[]. https://git.kernel.org/netdev/net-next/c/b4f0115efd7f - [v6,net-next,07/15] neighbour: Remove neigh_tables[]. https://git.kernel.org/netdev/net-next/c/7bb5d7675cde - [v6,net-next,08/15] ipv4: Replace &arp_tbl with arp_table(net). https://git.kernel.org/netdev/net-next/c/2430df635a59 - [v6,net-next,09/15] ipv6: Replace &nd_tbl with nd_table(net). https://git.kernel.org/netdev/net-next/c/df5f4f372de5 - [v6,net-next,10/15] neighbour: Clean up neigh_table_init() and neigh_table_clear(). https://git.kernel.org/netdev/net-next/c/e265c2c432d9 - [v6,net-next,11/15] neighbour: Convert neigh_table.entries to refcount_t. https://git.kernel.org/netdev/net-next/c/1c2b20509463 - [v6,net-next,12/15] neighbour: Namespacify neigh_tables. https://git.kernel.org/netdev/net-next/c/cda2962b6e2b - [v6,net-next,13/15] neighbour: Don't store net in struct pneigh_entry. https://git.kernel.org/netdev/net-next/c/da5a7e7a90e6 - [v6,net-next,14/15] neighbour: Remove unnecessary net_eq(). https://git.kernel.org/netdev/net-next/c/fae1c59810b8 - [v6,net-next,15/15] selftest: net: Specify netns for ip ntable in test_neigh.sh. https://git.kernel.org/netdev/net-next/c/10aa85762861
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html