netns: optimize netns cleaning by batching unhash_nsid calls
Subsystems (2)
PATCH v5 1/1 netns: optimize netns cleaning by batching unhash_nsid calls
Currently, unhash_nsid() scans the entire system for each netns being killed, leading to O(M_batch * N_system * log(N_ids)) complexity.
Optimize this to O(N_system * N_ids) by batching unhash operations. Move unhash_nsid() out of the per-netns loop in cleanup_net() to perform a single-pass traversal over survivor namespaces.
Identify dying peers by an 'is_dying' flag, which is set under net_rwsem write lock after the netns is removed from the global list. This batches the unhashing work and eliminates the O(M_batch) multiplier.
Use a restartable idr_get_next() loop for iteration. This avoids the unsafe modification issue inherent to idr_for_each() callbacks and allows dropping the nsid_lock to safely call sleepy rtnl_net_notifyid().
Clean up redundant nsid_lock and simplify the destruction loop now that unhashing is centralized.
---
v5:
- Use idr_get_next() for restartable iteration safely handling removals.
- Drop unhash_nsid_callback() to avoid context safety issues.
v4:
- Move unhash_nsid() out of the batch loop to reduce complexity from O(M*N) to O(N).
- Use idr_for_each() for efficient, single-pass IDR traversal.
- Mark 'is_dying' under net_rwsem to safely identify and batch unhashing.
- Simplify destruction loop by removing redundant locking and per-netns unhash logic.
v3:
- Update target tree to net-next.
- Post as a new thread instead of a reply.
v2:
- Move 'is_dying' setting to __put_net() to eliminate the O(M_batch) loop.
- Remove redundant initializations in preinit_net().
v1:
- Initial implementation of batch unhash_nsid().
include/net/net_namespace.h | 1 + net/core/net_namespace.c | 34 ++++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-)
include/net/net_namespace.h +1 -0
@@ -69,6 +69,7 @@ struct net {unsigned int dev_base_seq;/* protected by rtnl_mutex */ u32 ifindex; +bool is_dying;spinlock_t nsid_lock;atomic_t fnhe_genid;
net/core/net_namespace.c +20 -14
@@ -624,9 +624,11 @@ void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid) }EXPORT_SYMBOL_GPL (net_ns_get_ownership); -static void unhash_nsid (struct net *net,struct net *last) +static void unhash_nsid (struct net *last) { -struct net *tmp; +struct net *tmp, *peer; +int id; +/* This function is only called from cleanup_net() work, * and this work is the only process, that may delete * a net from net_namespace_list. So, when the below
@@ -634,22 +636,23 @@ static void unhash_nsid(struct net *net, struct net *last)* use for_each_net_rcu() or net_rwsem. */ for_each_net (tmp) { -int id; - + id =0 ;spin_lock (&tmp->nsid_lock); - id =__peernet2id (tmp, net); -if (id >=0 ) -idr_remove (&tmp->netns_ids, id); +while ((peer =idr_get_next (&tmp->netns_ids, &id))) { +if (peer->is_dying) { +idr_remove (&tmp->netns_ids, id); +spin_unlock (&tmp->nsid_lock); +rtnl_net_notifyid (tmp,RTM_DELNSID , id,0 ,NULL , +GFP_KERNEL ); +spin_lock (&tmp->nsid_lock); + }else { + id++; + } + }spin_unlock (&tmp->nsid_lock); -if (id >=0 ) -rtnl_net_notifyid (tmp,RTM_DELNSID , id,0 ,NULL , -GFP_KERNEL );if (tmp == last)break ; } -spin_lock (&net->nsid_lock); -idr_destroy (&net->netns_ids); -spin_unlock (&net->nsid_lock); }static LLIST_HEAD (cleanup_list);
@@ -674,6 +677,7 @@ static void cleanup_net(struct work_struct *work)llist_for_each_entry (net, net_kill_list, cleanup_list) {ns_tree_remove (net);list_del_rcu (&net->list); + net->is_dying = true; }/* Cache last net. After we unlock rtnl, no one new net * added to net_namespace_list can assign nsid pointer
@@ -688,8 +692,10 @@ static void cleanup_net(struct work_struct *work) last = list_last_entry(&net_namespace_list, struct net, list);up_write (&net_rwsem); +unhash_nsid (last); +llist_for_each_entry (net, net_kill_list, cleanup_list) { -unhash_nsid (net, last); +idr_destroy (&net->netns_ids);list_add_tail (&net->exit_list, &net_exit_list); }
Signature · 1 line
2.51.0
Quoted 3 lines · Qiliang Yuan
Currently,
unhash_nsid()scans the entire system for each netns being killed, leading toO(M_batch * N_system * log(N_ids))complexity.
batch and system is a bit unclear, maybe M_dying_net, N_alive_net or something ?
Also, isn't __peernet2id() also O(N_ids) ?
Quoted 44 lines
Optimize this to
O(N_system * N_ids)by batching unhash operations. Moveunhash_nsid()out of the per-netns loop incleanup_net()to perform a single-pass traversal over survivor namespaces.Identify dying peers by an '
is_dying' flag, which is set undernet_rwsemwrite lock after the netns is removed from the global list. This batches the unhashing work and eliminates theO(M_batch)multiplier.Use a restartable
idr_get_next()loop for iteration. This avoids the unsafe modification issue inherent toidr_for_each()callbacks and allows dropping thensid_lockto safely call sleepyrtnl_net_notifyid().Clean up redundant
nsid_lockand simplify the destruction loop now that unhashing is centralized.Signed-off-by Qiliang Yuan <[email protected]>---
v5:
- Useidr_get_next()for restartable iteration safely handling removals.
- Dropunhash_nsid_callback()to avoid context safety issues.
v4:
- Moveunhash_nsid()out of the batch loop to reduce complexity fromO(M*N)to O(N).
- Useidr_for_each()for efficient, single-pass IDR traversal.
- Mark 'is_dying' undernet_rwsemto safely identify and batch unhashing.
- Simplify destruction loop by removing redundant locking and per-netns unhash logic.
v3:
- Update target tree to net-next.
- Post as a new thread instead of a reply.
v2:
- Move 'is_dying' setting to__put_net()to eliminate theO(M_batch)loop.
- Remove redundant initializations inpreinit_net().
v1:
- Initial implementation of batchunhash_nsid().include/net/net_namespace.h | 1 + net/core/net_namespace.c | 34 ++++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-)include/net/net_namespace.h +0
-0
include/net/net_namespace.h struct net { · line 72
Quoted 4 lines
include/net/net_namespace.h +1
-0@@ -69,6 +69,7 @@ struct net {unsigned int dev_base_seq;/* protected by rtnl_mutex */ u32 ifindex; +bool is_dying;
Please put this in a hole, maybe after after hash_mix :
$ pahole -C net vmlinux
Quoted 8 lines
include/net/net_namespace.h +0
-0spinlock_t nsid_lock;atomic_t fnhe_genid;net/core/net_namespace.c +0
-0
net/core/net_namespace.c void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid) · line 630
Quoted 9 lines
net/core/net_namespace.c +3
-2@@ -624,9 +624,11 @@ void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid) }EXPORT_SYMBOL_GPL (net_ns_get_ownership); -static void unhash_nsid (struct net *net,struct net *last) +static void unhash_nsid (struct net *last) { -struct net *tmp; +struct net *tmp, *peer; +int id;
No need to move id up.
Quoted 14 lines
net/core/net_namespace.c +2
-4+/* This function is only called from cleanup_net() work, * and this work is the only process, that may delete * a net from net_namespace_list. So, when the below @@ -634,22 +636,23 @@ static void unhash_nsid(struct net *net, struct net *last)* use for_each_net_rcu() or net_rwsem. */ for_each_net (tmp) { -int id; - + id =0 ;spin_lock (&tmp->nsid_lock); - id =__peernet2id (tmp, net); -if (id >=0 )
net/core/net_namespace.c static void unhash_nsid(struct net *net, struct net *last) · line 649
Quoted 10 lines
net/core/net_namespace.c +9
-1-idr_remove (&tmp->netns_ids, id); +while ((peer =idr_get_next (&tmp->netns_ids, &id))) { +if (peer->is_dying) { +idr_remove (&tmp->netns_ids, id); +spin_unlock (&tmp->nsid_lock); +rtnl_net_notifyid (tmp,RTM_DELNSID , id,0 ,NULL , +GFP_KERNEL ); +spin_lock (&tmp->nsid_lock); + }else { + id++;
This can be done unconditionally as the id will never be assigned to dying nets.
while (...) {
id++;
if (!net->dying)
continue;
/* do cleanup */
}Quoted 38 lines
net/core/net_namespace.c +6
-8+ } + }spin_unlock (&tmp->nsid_lock); -if (id >=0 ) -rtnl_net_notifyid (tmp,RTM_DELNSID , id,0 ,NULL , -GFP_KERNEL );if (tmp == last)break ; } -spin_lock (&net->nsid_lock); -idr_destroy (&net->netns_ids); -spin_unlock (&net->nsid_lock); }static LLIST_HEAD (cleanup_list);@@ -674,6 +677,7 @@ static void cleanup_net(struct work_struct *work)llist_for_each_entry (net, net_kill_list, cleanup_list) {ns_tree_remove (net);list_del_rcu (&net->list); + net->is_dying = true; }/* Cache last net. After we unlock rtnl, no one new net * added to net_namespace_list can assign nsid pointer @@ -688,8 +692,10 @@ static void cleanup_net(struct work_struct *work) last = list_last_entry(&net_namespace_list, struct net, list);up_write (&net_rwsem); +unhash_nsid (last); +llist_for_each_entry (net, net_kill_list, cleanup_list) { -unhash_nsid (net, last); +idr_destroy (&net->netns_ids);list_add_tail (&net->exit_list, &net_exit_list); } --2.51.0
Hi Kuniyuki,
Thanks for your review and suggestions!
Quoted 2 lines · On Tue, 3 Feb 2026 16:20:18 -0800 Kuniyuki Iwashima <[email protected]> wrote:
batch and system is a bit unclear, maybe
M_dying_net,N_alive_netor something ?
Good point. I've updated the terminology in the commit message to M_dying_net and N_alive_net for better clarity.
Quoted 1 line
Also, isn't
__peernet2id()alsoO(N_ids)?
Yes, you are right. __peernet2id() performs a linear search in the IDR, so it is indeed O(N_id). I've corrected the complexity analysis in the commit log. This actually makes the batching optimization even more valuable.
Quoted 3 lines
Please put this in a hole, maybe after after
hash_mix:$ pahole -C net vmlinux
Updated. I've moved the 'is_dying' flag to the hole after 'hash_mix' to avoid increasing the size of struct net.
Quoted 1 line
No need to move id up.
Fixed. I've scoped the 'id' variable locally within the traversal loop to keep it cleaner.
Quoted 11 lines
This can be done unconditionally as the id will never be assigned to dying nets.
while (...) { id++; if (!net->dying) continue; /* do cleanup */ }
Simplified as suggested. The loop logic is much cleaner now with the unconditional increment.
I've sent out v6 with these changes.
v6 link: https://lore.kernel.org/all/[email protected]/
Best regards,
Qiliang