public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Qiliang Yuan <realwujing@gmail.com>
To: kuniyu@google.com
Cc: brauner@kernel.org, davem@davemloft.net, edumazet@google.com,
	horms@kernel.org, jlayton@kernel.org, kuba@kernel.org,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	pabeni@redhat.com, realwujing@gmail.com, sd@queasysnail.net,
	yuanql9@chinatelecom.cn
Subject: Re: [PATCH net-next v3] netns: optimize netns cleaning by batching unhash_nsid calls
Date: Wed, 28 Jan 2026 07:19:16 -0500	[thread overview]
Message-ID: <20260128121921.1761236-1-realwujing@gmail.com> (raw)
In-Reply-To: <CAAVpQUBbdsNPkvL7JHrgqen7DM70S2VYTtgHx-kjWq3ZqB6uqA@mail.gmail.com>

Hi Kuniyuki,

On Tue, Jan 27, 2026 at 6:05 PM Kuniyuki Iwashima <kuniyu@google.com> wrote:
>
> On Tue, Jan 27, 2026 at 5:22 PM Qiliang Yuan <realwujing@gmail.com> wrote:
> >
> > Currently, unhash_nsid() scans the entire net_namespace_list for each
> > netns in a destruction batch during cleanup_net(). This leads to
> > O(M_batch * N_system * M_nsids) complexity, where M_batch is the
> > destruction batch size, N_system is the total number of namespaces,
> > and M_nsids is the number of IDs in each IDR.
> >
> > Reduce the complexity to O(N_system * M_nsids) by introducing an
> > 'is_dying' flag to mark namespaces being destroyed. This allows
> > unhash_nsid() to perform a single-pass traversal over the system's
> > namespaces. In this pass, for each survivor namespace, iterate
> > through its netns_ids and remove any mappings that point to a marked
> > namespace, effectively eliminating the M_batch multiplier.
> >
> > Signed-off-by: Qiliang Yuan <realwujing@gmail.com>
> > Signed-off-by: Qiliang Yuan <yuanql9@chinatelecom.cn>
>
> Why two SOBs with the same person ?

- Signed-off-by: Qiliang Yuan <realwujing@gmail.com> (Personal email)
- Signed-off-by: Qiliang Yuan <yuanql9@chinatelecom.cn> (Work email)

My work email often has trouble receiving external mailing list replies, 
so I've included both to ensure I don't miss any feedback and to 
properly attribute the work. The v8 version should have everything 
matching correctly now.

> > diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
> > index a6e6a964a287..50fdd4f9bb3b 100644
> > --- a/net/core/net_namespace.c
> > +++ b/net/core/net_namespace.c
> > @@ -624,9 +624,10 @@ 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;
> > +
> >         /* 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
> > @@ -636,20 +637,34 @@ static void unhash_nsid(struct net *net, struct net *last)
> >         for_each_net(tmp) {
> >                 int id;
> >
> > -               spin_lock(&tmp->nsid_lock);
> > -               id = __peernet2id(tmp, net);
> > -               if (id >= 0)
> > -                       idr_remove(&tmp->netns_ids, id);
> > -               spin_unlock(&tmp->nsid_lock);
> > -               if (id >= 0)
> > -                       rtnl_net_notifyid(tmp, RTM_DELNSID, id, 0, NULL,
> > -                                         GFP_KERNEL);
> > +               for (id = 0; ; id++) {
>
> Doesn't this rather slow down in a common case where
> init_net has ids for other netns since it is never dismantled ?

Yes, you're right. In the original code, we only scanned 'tmp' for specific 'net' 
which was being killed. Now we are scanning all IDs in 'tmp' to find any dying 
peers. 

If 'tmp' (like init_net) has many long-lived netns IDs, we end up iterating through 
them even if none of them are dying.

To address this and avoid the overhead, I can use idr_for_each() with a callback 
to find and collect dying IDs, or keep the O(M_batch) outer loop but optimize the 
inner part if it's truly problematic. 

However, given that this is the cleanup path, I thought the batching benefit 
(N_system vs M_batch * N_system) would outweigh the per-netns IDR scan. 

I'll revert to a more efficient iteration or use idr_for_each() to handle this 
gracefully in v4.

Thanks,
Qiliang

  reply	other threads:[~2026-01-28 12:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-28  1:21 [PATCH net-next v3] netns: optimize netns cleaning by batching unhash_nsid calls Qiliang Yuan
2026-01-28  2:05 ` Kuniyuki Iwashima
2026-01-28 12:19   ` Qiliang Yuan [this message]
2026-01-28 17:13     ` Kuniyuki Iwashima
2026-01-30  2:44       ` Qiliang Yuan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260128121921.1761236-1-realwujing@gmail.com \
    --to=realwujing@gmail.com \
    --cc=brauner@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=jlayton@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sd@queasysnail.net \
    --cc=yuanql9@chinatelecom.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox