]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
netns: optimize netns cleaning by batching unhash_nsid calls
authorQiliang Yuan <realwujing@gmail.com>
Wed, 4 Feb 2026 07:48:42 +0000 (02:48 -0500)
committerJakub Kicinski <kuba@kernel.org>
Sat, 7 Feb 2026 04:01:31 +0000 (20:01 -0800)
commit7acee67a6bce02e0af8a4bf7b412e2164d5a48e9
treec80805b1aa16de4d99edb4d3011bfdf1f981184c
parent24cf78c738318f3d2b961a1ab4b3faf1eca860d7
netns: optimize netns cleaning by batching unhash_nsid calls

Currently, unhash_nsid() scans the entire system for each netns being
killed, leading to O(L_dying_net * M_alive_net * N_id) complexity, as
__peernet2id() also performs a linear search in the IDR.

Optimize this to O(M_alive_net * N_id) 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(L_dying_net) multiplier.

To minimize the impact on struct net size, 'is_dying' is placed in an
existing hole after 'hash_mix' in struct net.

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.

Signed-off-by: Qiliang Yuan <yuanql9@chinatelecom.cn>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
Link: https://patch.msgid.link/20260204074854.3506916-1-realwujing@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/net_namespace.h
net/core/net_namespace.c