From nobody Sat Feb 7 12:19:29 2026 Received: from mail-dy1-f175.google.com (mail-dy1-f175.google.com [74.125.82.175]) (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 658B5332EAA for ; Mon, 26 Jan 2026 11:25:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769426704; cv=none; b=E8aiynh+bBfGtampGHycg2tWThbbq1MHutPFVrVc/iW2NH0td59DpHJ/id6IYyvzxJfYo+bcO3bBmxq4RthbrtlsEw4QsIvJ13BistY09WXiUYlVyroFK5k87PgLrLW7ylskbiqs2AjJHgc+A0EL6LbQJ5T6b7dS7jTx1fGT4Is= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769426704; c=relaxed/simple; bh=Mtm2H1Y8oOuQVd+PRp+dFj93+nf4N8QNg9rTYqrqjaE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VQvcpe/bfvidRmzNxKYBBKCdkxKusKNcFTqWqYNXGTZKtymDumtMv0l8NRs2NGqVTymogeIgmn7W5B4HK7r5vV+qG+ftnROjah27UBmTVfU4gi2UuSS2afnOa/n7ccZrFxFsoMx7u0nCkIsDgsQAv4fPA6Y77sS0cIsmVi3n0wQ= 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=EInhiTEo; arc=none smtp.client-ip=74.125.82.175 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="EInhiTEo" Received: by mail-dy1-f175.google.com with SMTP id 5a478bee46e88-2b72e49776eso7261865eec.1 for ; Mon, 26 Jan 2026 03:25:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769426702; x=1770031502; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dZKhkSOogmCeEXH98s2F1bz0X39ue/E5DNO3lCuMsCk=; b=EInhiTEojInl1fuBKk+iUEbsZ/YLZkD4SkSO6McakzE6K+YGxv6fmSiZmuSKK8xutq trWs0DJZo5Rofy9WjsOA/hV0uRIrvAAz8mRVdB8PIhKmcJW9OSKxwXNjBCujb+s4A5wx Mjhw9pB0K2jLtBEdiPXl8vWtpWhd10rEx769+Ur3+BDSSV3n0s58imxmmyNKpyZqxKjT UBLXmDesnt+xllVsyHeXFdl5QzJqeEDE4pXlWgrKV7jAqtzSmls9xWDJxpkK40N7S8rj Fr5Ei0fQQlJCAWgx8Gy1mqTp3kp6uA1LJAKOLec4KX+6zGcWfv3Znwb7OBxqzWUIH1lh cVoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769426702; x=1770031502; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=dZKhkSOogmCeEXH98s2F1bz0X39ue/E5DNO3lCuMsCk=; b=wk/3Zd7nHcWXH90lOnoqHKyoLUGrgMD3uQvToUxY6zZvhsyOK5Xp0pSQkUYFiviSQ5 3cVjnSBTZj+eMC1nrGVvCTFK8Hw9dQplrkgzrNwiqc/8sWjzU8HHTykg4uIX77NqVsH0 L8A7BhtGl/U+Z4m9TAhynzBbuJ1+NA65MuHCef1TvIVObtlQmpiKA8xh/74d7uEFvZ44 DbXB8V5Hlah94/U5LfSLIQggqyGEwSKR3ptZgA0/m1P2pEGddKA0rqbo76AM3p7QzRAf JASugNpHKCAbrMrlOM5+ljojVAe7e8CcMvBIxC8WjQAD1iXoBjudSy1JEIC+/3lsokYF Lwfw== X-Forwarded-Encrypted: i=1; AJvYcCV6LAgicbHOAMkxRbBVqvLqEmx/pt0DwlXq6ozny1ClFLg+v1QL3L2In0ivn1OzMKHGDk9EULKid2XrfTg=@vger.kernel.org X-Gm-Message-State: AOJu0YxSPl5nYWrYE8DHiMmfgzrdZR0psS7mGm6VURq8i0C0gRcUBlkR vZ4grZtsrljV/Qia9Uf5Fice++Dk4lWg7N+4vhYFxzgNtXCT/eANoN8OfHvOKA== X-Gm-Gg: AZuq6aIqsRJ98aHYB79dG5DFfXLo++Pvr/sK/cGy1tBcnPtlQdyaiadaRFHSt/fA8Tm jFPhJ+whDWXosSzF9yZH9niB1hOxOUZWQ6JlwBzFjAmhpWTV/40knGZJy2CSmClUBfiUIKJl4xD 7VutOaOdfODqLdYgMvHomHsDg92Do5Oy4YOTVOeDOq8P0DYCI4+lITxnLqCmB28qy1NVkjXjz6K 0Rfemtd9bVUtwQfGWKTsbuTzY/n6BZhztPF0+vW1KBOnEechkkvQALWTmsYl3p1mSVpk+RO67M5 oC1eV1DAaRAoXqyeuhoTcJirTFO2SPGqeQAW3blwH1tbHvrYssnBfqahMj54Xh+w2s4Xkl/RenE fgb5fGHIZ3esV9k711yOnWs53kIZOJk3bWtzYBRdu/Y42JT8yXK+4Me1TxUB9ekG2R4Vv1wX0+O y87AI= X-Received: by 2002:a05:7301:5784:b0:2af:7f2:50cc with SMTP id 5a478bee46e88-2b764839530mr2249978eec.38.1769426702372; Mon, 26 Jan 2026 03:25:02 -0800 (PST) Received: from debian ([74.48.213.230]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2b73aa08964sm12804457eec.28.2026.01.26.03.24.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jan 2026 03:25:01 -0800 (PST) From: Qiliang Yuan To: edumazet@google.com Cc: brauner@kernel.org, davem@davemloft.net, kuba@kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, pabeni@redhat.com, realwujing@gmail.com, yuanql9@chinatelecom.cn Subject: [PATCH v2] netns: optimize netns cleaning by batching unhash_nsid calls Date: Mon, 26 Jan 2026 06:24:51 -0500 Message-ID: <20260126112451.1071143-1-realwujing@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: References: 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 --- v2: - Remove unrelated ifindex and is_dying initialization in preinit_net. - Move is_dying =3D true to __put_net() to avoid an extra loop in cleanup_= net. v1: - Initial proposal using 'is_dying' flag to batch unhash_nsid calls. 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