Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1079992pxb; Tue, 17 Aug 2021 03:23:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyeU26kKpCbnAF/YuxKsaauJRGMwHXbPBrph93NyN8BvenlBIlB6LMXULGcDcBumZ1Tjn32 X-Received: by 2002:a17:906:2642:: with SMTP id i2mr3253866ejc.323.1629195787236; Tue, 17 Aug 2021 03:23:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629195787; cv=none; d=google.com; s=arc-20160816; b=wBY+jCItVnbSCKktXuQx0Zy313Iqdd96OBWCnalQAg4TUbrzypWzwHkZBX4/pUtCX6 /6SqFDKE7Nle57PS1lA90DgvTcnaF6OQ3l5r9q7PqTcYwFWaUF7Q8tq+2WnxoB3JY0ac B63tD8RhdoYemwK9D8XuAWB7oA8Gyv6Crl6/g5Ttp/MLb+nUTpuQ5Eph3uvHkvS6ZXpe 4uIGErgoAHZY8ue/RDdmEQg/1TVkjhQVaJgtCxlEXhQnA1YITk3hBE128725l0hu8Ncy DeyndVr7TCu44W2DIamEI6Eyb8LnVG/si9s/Pcqg3rCMCGaQ+/WmSF7aCl4r6+V+O/si 4pqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=oRyPuKKOBakVGi7BzUz0aCZ6qjxSBDTY/WItFLOtqAU=; b=XDp/41IFWNG9AdCTbyFeohAxxpYfFJfq78iu7ca2WY1CjHGbSJqkcexhYEFonxnZZP KzLx9oOiE9NDOpL3Xv5wYNRxuiVPpwb4qKXUo2FCM25kafa9GKObTdjNujl6uufnNHtn 9nH3zLnl3i2rkYc9Sxba6fnoPGXyDErXoB1+CdWkHzw8KV18y0pq1I8zqynpkQmYmTCm RQqjdP9w8INWMzQ1blyVnwq6v/r/kX8ODTToWZkDs8988upTDt/W5U8TbgjEOpfCO4k1 dC1AJBHPME8017Gik3VnU1YY11vduvhYJS869mdBW2LYOH5FPbpFk/vcrZ75pk/0w1r2 OzCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=LkfBcz0s; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b18si1803013ejh.638.2021.08.17.03.22.43; Tue, 17 Aug 2021 03:23:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=LkfBcz0s; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236240AbhHQKVX (ORCPT + 99 others); Tue, 17 Aug 2021 06:21:23 -0400 Received: from out2.migadu.com ([188.165.223.204]:13895 "EHLO out2.migadu.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235651AbhHQKVK (ORCPT ); Tue, 17 Aug 2021 06:21:10 -0400 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1629195634; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=oRyPuKKOBakVGi7BzUz0aCZ6qjxSBDTY/WItFLOtqAU=; b=LkfBcz0snirdno7/M/htlIRw+ZadPgi00aa+0mB7opgr2/3lmOqog4nT8ph7zHD2+5rUtN nNWp+bJHrf1JjTlay7rq9gsJ2VAyXpl4O8o/dDMPPrv4e/YY/qAuOOe7vZ9+hb8OM5cRng aFC3yAdprLK8ipIIbcWgKA/bCnphtAI= From: Yajun Deng To: davem@davemloft.net, kuba@kernel.org Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Yajun Deng Subject: [PATCH net-next] net: net_namespace: Optimize the code Date: Tue, 17 Aug 2021 18:20:01 +0800 Message-Id: <20210817102001.1125-1-yajun.deng@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: yajun.deng@linux.dev Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Inline ops_free(), becase there is only one caller. Separate net_drop_ns() and net_free(), so the net_free() can be called directly. Add free_exit_list() helper function for free net_exit_list. Signed-off-by: Yajun Deng --- net/core/net_namespace.c | 49 ++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c index 9b5a767eddd5..27acbf2df78f 100644 --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c @@ -148,13 +148,6 @@ static int ops_init(const struct pernet_operations *ops, struct net *net) return err; } -static void ops_free(const struct pernet_operations *ops, struct net *net) -{ - if (ops->id && ops->size) { - kfree(net_generic(net, *ops->id)); - } -} - static void ops_pre_exit_list(const struct pernet_operations *ops, struct list_head *net_exit_list) { @@ -184,7 +177,7 @@ static void ops_free_list(const struct pernet_operations *ops, struct net *net; if (ops->size && ops->id) { list_for_each_entry(net, net_exit_list, exit_list) - ops_free(ops, net); + kfree(net_generic(net, *ops->id)); } } @@ -433,15 +426,17 @@ static struct net *net_alloc(void) static void net_free(struct net *net) { - kfree(rcu_access_pointer(net->gen)); - kmem_cache_free(net_cachep, net); + if (refcount_dec_and_test(&net->passive)) { + kfree(rcu_access_pointer(net->gen)); + kmem_cache_free(net_cachep, net); + } } void net_drop_ns(void *p) { - struct net *ns = p; - if (ns && refcount_dec_and_test(&ns->passive)) - net_free(ns); + struct net *net = (struct net *)p; + if (net) + net_free(net); } struct net *copy_net_ns(unsigned long flags, @@ -479,7 +474,7 @@ struct net *copy_net_ns(unsigned long flags, put_userns: key_remove_domain(net->key_domain); put_user_ns(user_ns); - net_drop_ns(net); + net_free(net); dec_ucounts: dec_net_namespaces(ucounts); return ERR_PTR(rv); @@ -611,7 +606,7 @@ static void cleanup_net(struct work_struct *work) dec_net_namespaces(net->ucounts); key_remove_domain(net->key_domain); put_user_ns(net->user_ns); - net_drop_ns(net); + net_free(net); } } @@ -1120,6 +1115,14 @@ static int __init net_ns_init(void) pure_initcall(net_ns_init); +static void free_exit_list(struct pernet_operations *ops, struct list_head *net_exit_list) +{ + ops_pre_exit_list(ops, net_exit_list); + synchronize_rcu(); + ops_exit_list(ops, net_exit_list); + ops_free_list(ops, net_exit_list); +} + #ifdef CONFIG_NET_NS static int __register_pernet_operations(struct list_head *list, struct pernet_operations *ops) @@ -1145,10 +1148,7 @@ static int __register_pernet_operations(struct list_head *list, out_undo: /* If I have an error cleanup all namespaces I initialized */ list_del(&ops->list); - ops_pre_exit_list(ops, &net_exit_list); - synchronize_rcu(); - ops_exit_list(ops, &net_exit_list); - ops_free_list(ops, &net_exit_list); + free_exit_list(ops, &net_exit_list); return error; } @@ -1161,10 +1161,8 @@ static void __unregister_pernet_operations(struct pernet_operations *ops) /* See comment in __register_pernet_operations() */ for_each_net(net) list_add_tail(&net->exit_list, &net_exit_list); - ops_pre_exit_list(ops, &net_exit_list); - synchronize_rcu(); - ops_exit_list(ops, &net_exit_list); - ops_free_list(ops, &net_exit_list); + + free_exit_list(ops, &net_exit_list); } #else @@ -1187,10 +1185,7 @@ static void __unregister_pernet_operations(struct pernet_operations *ops) } else { LIST_HEAD(net_exit_list); list_add(&init_net.exit_list, &net_exit_list); - ops_pre_exit_list(ops, &net_exit_list); - synchronize_rcu(); - ops_exit_list(ops, &net_exit_list); - ops_free_list(ops, &net_exit_list); + free_exit_list(ops, &net_exit_list); } } -- 2.32.0