Re: [PATCH net-next v3] netns: optimize netns cleaning by batching unhash_nsid calls

From: Qiliang Yuan

Date: Thu Jan 29 2026 - 21:44:30 EST


Hi Kuniyuki,

Thank you for your valuable feedback!

On Wed, 28 Jan 2026 09:13:59 -0800 Kuniyuki Iwashima <kuniyu@xxxxxxxxxx> wrote:
> idr_for_each() sounds better to me.

I have integrated this in v4. It indeed makes the IDR traversal much more
idiomatic and efficient.

> If we replace list_del_rcu(&net->list); with
> list_del_init_rcu(&net->list);, we can check net->list.pprev
> instead of adding dying_net, which is a bit racy since
> idr_for_each() could return a net which would have been
> processed in the next cleanup_net() invocation.

To resolve this race, I've moved the setting of 'is_dying = true' inside
cleanup_net() while still holding the net_rwsem write lock. This ensures
all namespaces in the current kill_list are marked before we release the
lock and perform the batch unhashing.

Regarding list_del_init_rcu(), as it is not a standard API, I evaluated
using list_del_rcu() followed by INIT_LIST_HEAD(). However, resetting
list pointers is generally unsafe for RCU readers (e.g., in for_each_net_rcu),
as it could cause them to enter an infinite loop. Using the 'is_dying'
boolean under the existing lock seems to be the safest and simplest approach.

I've also cleaned up the redundant nsid_lock and the duplicate Signed-off-by
tags as you suggested.

The complexity is now O(N_system * N_ids), effectively eliminating the
O(M_batch) multiplier.

I've just sent out the v4 patch. Looking forward to your thoughts.

Thanks,
Qiliang