From nobody Sat Feb 7 21:11:55 2026 Received: from mail-dl1-f49.google.com (mail-dl1-f49.google.com [74.125.82.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 632441991CB for ; Wed, 28 Jan 2026 01:22:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769563364; cv=none; b=Is83biou/9Q6EQSQA10qt0cuYYYMbxkGm3v+zxf6tzD+UqojUd5egssFoSg4S4xP94J42RcrmjcxWq7PmKD8rhrYDSB8JRzBMIPemtM7gI0GKBwrIU0qz3NG9OB4KB3yCz8EBoTyj+F0LhdYYI8NaRzd7uGC4KOq20eOiJm2NDo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769563364; c=relaxed/simple; bh=wdSxOj+/8brg2xyTPyKuK4zewxw/8x/s52TKc+XkVw4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=U34HvUjcWbwMfVRaR8PHjdwGO9dyluflUu6iR8IoTdZ9r8mp6if6stHVNEWMVgPPCjZntDOf1Zj1yKgy8vZnO/B/DzYz5f7WaXR8VydyUfmc3lVexeIjHDr336NrkTRSILOmwztFWhkEqgghFbfMqx/NJMJ0DekhtixqkFdsMvQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=L96/DjME; arc=none smtp.client-ip=74.125.82.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="L96/DjME" Received: by mail-dl1-f49.google.com with SMTP id a92af1059eb24-12331482b8fso1293735c88.1 for ; Tue, 27 Jan 2026 17:22:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769563361; x=1770168161; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=WdlJkODskdsR/d8eXcoiwCooiZWIMrUfvjPFzHYdnGE=; b=L96/DjMEm9QbxBY1xN7aB76nfrUgJMOHXvH4xt5dZ3wnaLf44oWHPIAg7TxYpZD5b/ HUBQTainkroJ6I4sJEyvzsxvtvp+DKO7G+NPwnbsE4Jwcde4sD+DMuB8p+XdqAFfuZoP 3YyrGzjemrT+tzV9jFmHVCA9QPgyz2dwDuXozIs3NY/CuGeHXnODxlnSl8NVAWiWW/VQ tvZh+7NFUceNs6yQjN+fOdlcw3dQ01VWu1oz9gWfGp7mQZhnTXDHjOiSucsriR4ZF8fD 0xUlsNemLuirwpQ0PIwWnuIlO0XGORW7WyLB4V+xZotx0L12ef2IOiYxqoHufHOWUeL1 YsDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769563361; x=1770168161; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=WdlJkODskdsR/d8eXcoiwCooiZWIMrUfvjPFzHYdnGE=; b=UA6x43ujktEghDeUTzEnSli4/2a7Um/GcC+42k6CNjU7we74OCGR6ixfAq7e7s5uF/ OnVfmXPSeWHX3TYJtAe0QHEWfjkY6hOcTjXnKY8sobqcCq50SWXylI5H5146TADoyMUU s6I7P0bM4wfEQFTuKTG0ZjVdNEFKGZAWuSfS1BqFkVHReZmEXBqxqgDSlTf3XQVznjvX Stxzyi2E3TAVl/XU2EdNIMcu5LzDGCg+FsGCQq+qnJHpZNm6u96EeO3W/hw8ISQtH3C3 K2T3s1bsTGWpOaTQ5ITWYnKajeQ/4OKZSMRAXGTT2JxpZ7LZRt2pClG8WkHHAuBomk4T MDwg== X-Forwarded-Encrypted: i=1; AJvYcCUJlpZS8pP/3MR1b8gD+Kts6MYGIpw2RWkiZ2yrv37xyGj5FGhjdP/UGUpDI8frZf1ndejoJcJWPiE89PU=@vger.kernel.org X-Gm-Message-State: AOJu0YwYJHCwtAFnQs8n+Tdyfp2Nj6OnsSeb7FQsdV+ZRG++a/VmV25i YLqhf3DIHO1EtCBXLNGMWmeT0PvM6u5iRAl5o5DtpxAb+Vnpi81yh3hK X-Gm-Gg: AZuq6aJsmdvXnHfUR1r6/jlgg1jF+MH4oEmSkp14ybc/MMnt6F1hTUvLBjSR3dORHox YaAEQcjZ61fZTu9aALxGR0EcmrlRmBRxeBPHBBIAhL2DNn9LyM4z/EaRBdYTR9iz+ukmRBIEBBT WXVopndl7a+EOuCQvufU3QivCpAUvFYKaMWvSRsf6V61ueUCb/gxuEULbuhk2MbmJNCwVFYUfs3 tpp3s8bR2xFG05CcVXZVCiaTz1QS1Yo8bHLIJWv1ZJzu7JqgT3dmxZkZqLb/TlHODbT6KfMSO6t 8w2ioU7vTgUcK4WAaoQXXSINSOVCYr4FMNu4R5ruUUhZ7cUSZrdUMpAt7L7Xpo4+k4BRuAqBXd3 XymesPLTxe+6T5QJ5Pf60l5v83QK5UtHZoHCwyoyCrxW2mtEo+yvXr6cjXRQ7xTBu4xFqa6qjF4 Xpf4ggTEg0zFVAoA== X-Received: by 2002:a05:7022:912:b0:11b:f056:a1b3 with SMTP id a92af1059eb24-124a00684dfmr2656657c88.11.1769563361149; Tue, 27 Jan 2026 17:22:41 -0800 (PST) Received: from debian ([74.48.213.230]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-124a9de948esm581244c88.9.2026.01.27.17.22.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jan 2026 17:22:40 -0800 (PST) From: Qiliang Yuan To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Christian Brauner , Kuniyuki Iwashima , Sabrina Dubroca , Jeff Layton , Qiliang Yuan Cc: Qiliang Yuan , Simon Horman , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v3] netns: optimize netns cleaning by batching unhash_nsid calls Date: Tue, 27 Jan 2026 20:21:31 -0500 Message-ID: <20260128012145.1682016-1-realwujing@gmail.com> X-Mailer: git-send-email 2.51.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Signed-off-by: Qiliang Yuan --- 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 | 46 ++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h index cb664f6e3558..bd1acc6056ac 100644 --- a/include/net/net_namespace.h +++ b/include/net/net_namespace.h @@ -69,6 +69,7 @@ struct net { =20 unsigned int dev_base_seq; /* protected by rtnl_mutex */ u32 ifindex; + bool is_dying; =20 spinlock_t nsid_lock; atomic_t fnhe_genid; 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); =20 -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; =20 - spin_lock(&tmp->nsid_lock); - id =3D __peernet2id(tmp, net); - if (id >=3D 0) - idr_remove(&tmp->netns_ids, id); - spin_unlock(&tmp->nsid_lock); - if (id >=3D 0) - rtnl_net_notifyid(tmp, RTM_DELNSID, id, 0, NULL, - GFP_KERNEL); + for (id =3D 0; ; id++) { + struct net *peer; + bool dying; + + rcu_read_lock(); + peer =3D idr_get_next(&tmp->netns_ids, &id); + dying =3D peer && peer->is_dying; + rcu_read_unlock(); + + if (!peer) + break; + if (!dying) + continue; + + spin_lock(&tmp->nsid_lock); + if (idr_find(&tmp->netns_ids, id) =3D=3D peer) + idr_remove(&tmp->netns_ids, id); + else + peer =3D NULL; + spin_unlock(&tmp->nsid_lock); + + if (peer) + rtnl_net_notifyid(tmp, RTM_DELNSID, id, 0, + NULL, GFP_KERNEL); + } if (tmp =3D=3D last) break; } - spin_lock(&net->nsid_lock); - idr_destroy(&net->netns_ids); - spin_unlock(&net->nsid_lock); } =20 static LLIST_HEAD(cleanup_list); @@ -688,8 +703,12 @@ static void cleanup_net(struct work_struct *work) last =3D list_last_entry(&net_namespace_list, struct net, list); up_write(&net_rwsem); =20 + unhash_nsid(last); + llist_for_each_entry(net, net_kill_list, cleanup_list) { - unhash_nsid(net, last); + spin_lock(&net->nsid_lock); + idr_destroy(&net->netns_ids); + spin_unlock(&net->nsid_lock); list_add_tail(&net->exit_list, &net_exit_list); } =20 @@ -739,6 +758,7 @@ static DECLARE_WORK(net_cleanup_work, cleanup_net); void __put_net(struct net *net) { ref_tracker_dir_exit(&net->refcnt_tracker); + net->is_dying =3D true; /* Cleanup the network namespace in process context */ if (llist_add(&net->cleanup_list, &cleanup_list)) queue_work(netns_wq, &net_cleanup_work); --=20 2.51.0