Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp36200pxb; Tue, 12 Apr 2022 16:05:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw42m/3GGY7pzvv1/2PmwfAxM+yAwjHj20bksVEy0nejhIMz2K6xKVsHHCcIPVtPmlacX2k X-Received: by 2002:a17:90b:350f:b0:1c7:7769:3cc6 with SMTP id ls15-20020a17090b350f00b001c777693cc6mr7451062pjb.120.1649804742657; Tue, 12 Apr 2022 16:05:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649804742; cv=none; d=google.com; s=arc-20160816; b=amLF5HRIvnixvcOEaRZoXTUNlvwQFgFix5ODeO7+Oz1jTubDOJ1f5pVlnPWULQXTTR 0oaiivmyLguGNcfs0xvrZKz4ihTGpUt6szgRGtcWOLfmGu6vnxXaFZ6p5Mh2CEO6Zkt5 ZTPffHJbPVceT47EspWLt/BGgkTOekWuThDPkmBy0Iv9oTKI/kgDTB9FrqHUOsGKBSBo yxs0DrNZIrogylBglINVcJc7+dVZTiCf9MVoRwnn3OwJ1tSVG+++MhM7zVw6QDXvVDdQ qmRKDID3pKaISHLtSKS173trPoWqZYOXjE+OuF6n2vy6KMKGhBMgscQjTWkmy1vmV+6R xtRw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=iGNxO9xvX7vJqVU/oczWuKCG/D6pHODBT/Ny81PuXCo=; b=ii5z/rDeijQIyXK7/ctpoI/AAo4fT14ZYAPW9ig3Qpu8eXWu1IlfgeWGqhcM0YL/V+ HeHd6s3uWDYTAlA1aBA7cM0HAl2TmxyovehljsWoFU2tAFRm5EiZrLvnubqsrWZiRG41 HB1Kylf6oOfzCW2Z6B2Pu0IMlJeml9vsOv5sKUj6UwRvvQI/rLklucMHYWCin/1Ddix2 9Vz173CEp73TCKJRgDhQyZCMrZoVfvNT6bssNhA5sqAAD9L8lcD3cKlrfYJnZMyTgwdo 0S1QLM1U+UQGvOCTUDrJbmawavs5wbdHIElZ0JxePtpWXvv35iNzygH9U831XqK+KfTN kdsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zssfWWqB; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id u5-20020a17090341c500b00158745f32c1si6295773ple.171.2022.04.12.16.05.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 16:05:42 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zssfWWqB; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F15BA1F0445; Tue, 12 Apr 2022 14:48:07 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383361AbiDLIgt (ORCPT + 99 others); Tue, 12 Apr 2022 04:36:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353143AbiDLHhY (ORCPT ); Tue, 12 Apr 2022 03:37:24 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB600515BD; Tue, 12 Apr 2022 00:09:35 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 56C3B6153F; Tue, 12 Apr 2022 07:09:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 610DBC385A1; Tue, 12 Apr 2022 07:09:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649747374; bh=T7TznzvdMqlaPzkJUDKOMgqBHtIEXvib62hGt+9AWGQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zssfWWqBEnE/+/z2gGPC1AupRIQv3zhHgggnR82Ez+3rvN8WE82rMnr6BYoTga8ah lBVmqGea0masUEa3LQl5btM+rQnrKRfj0iE/EfQHQjUUtEzWyy6ZJHeuhzjJygW7Ix B2bvloZOH1RYqHSLTxBJhuH1Oq/PeO43o8rG7MWQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , "David S. Miller" , Sasha Levin Subject: [PATCH 5.17 055/343] net: initialize init_net earlier Date: Tue, 12 Apr 2022 08:27:53 +0200 Message-Id: <20220412062952.693254073@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220412062951.095765152@linuxfoundation.org> References: <20220412062951.095765152@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Dumazet [ Upstream commit 9c1be1935fb68b2413796cdc03d019b8cf35ab51 ] While testing a patch that will follow later ("net: add netns refcount tracker to struct nsproxy") I found that devtmpfs_init() was called before init_net was initialized. This is a bug, because devtmpfs_setup() calls ksys_unshare(CLONE_NEWNS); This has the effect of increasing init_net refcount, which will be later overwritten to 1, as part of setup_net(&init_net) We had too many prior patches [1] trying to work around the root cause. Really, make sure init_net is in BSS section, and that net_ns_init() is called earlier at boot time. Note that another patch ("vfs: add netns refcount tracker to struct fs_context") also will need net_ns_init() being called before vfs_caches_init() As a bonus, this patch saves around 4KB in .data section. [1] f8c46cb39079 ("netns: do not call pernet ops for not yet set up init_net namespace") b5082df8019a ("net: Initialise init_net.count to 1") 734b65417b24 ("net: Statically initialize init_net.dev_base_head") v2: fixed a build error reported by kernel build bots (CONFIG_NET=n) Signed-off-by: Eric Dumazet Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- include/net/net_namespace.h | 6 ++++++ init/main.c | 2 ++ net/core/dev.c | 3 +-- net/core/net_namespace.c | 17 +++++------------ 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h index 5b61c462e534..374cc7b260fc 100644 --- a/include/net/net_namespace.h +++ b/include/net/net_namespace.h @@ -513,4 +513,10 @@ static inline void fnhe_genid_bump(struct net *net) atomic_inc(&net->fnhe_genid); } +#ifdef CONFIG_NET +void net_ns_init(void); +#else +static inline void net_ns_init(void) {} +#endif + #endif /* __NET_NET_NAMESPACE_H */ diff --git a/init/main.c b/init/main.c index 65fa2e41a9c0..ada50f5a15e4 100644 --- a/init/main.c +++ b/init/main.c @@ -99,6 +99,7 @@ #include #include #include +#include #include #include @@ -1116,6 +1117,7 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void) key_init(); security_init(); dbg_late_init(); + net_ns_init(); vfs_caches_init(); pagecache_init(); signals_init(); diff --git a/net/core/dev.c b/net/core/dev.c index 1baab07820f6..91cf709c98b3 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -10732,8 +10732,7 @@ static int __net_init netdev_init(struct net *net) BUILD_BUG_ON(GRO_HASH_BUCKETS > 8 * sizeof_field(struct napi_struct, gro_bitmask)); - if (net != &init_net) - INIT_LIST_HEAD(&net->dev_base_head); + INIT_LIST_HEAD(&net->dev_base_head); net->dev_name_head = netdev_create_hash(); if (net->dev_name_head == NULL) diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c index a5b5bb99c644..212e65add951 100644 --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c @@ -44,13 +44,7 @@ EXPORT_SYMBOL_GPL(net_rwsem); static struct key_tag init_net_key_domain = { .usage = REFCOUNT_INIT(1) }; #endif -struct net init_net = { - .ns.count = REFCOUNT_INIT(1), - .dev_base_head = LIST_HEAD_INIT(init_net.dev_base_head), -#ifdef CONFIG_KEYS - .key_domain = &init_net_key_domain, -#endif -}; +struct net init_net; EXPORT_SYMBOL(init_net); static bool init_net_initialized; @@ -1084,7 +1078,7 @@ static void rtnl_net_notifyid(struct net *net, int cmd, int id, u32 portid, rtnl_set_sk_err(net, RTNLGRP_NSID, err); } -static int __init net_ns_init(void) +void __init net_ns_init(void) { struct net_generic *ng; @@ -1105,6 +1099,9 @@ static int __init net_ns_init(void) rcu_assign_pointer(init_net.gen, ng); +#ifdef CONFIG_KEYS + init_net.key_domain = &init_net_key_domain; +#endif down_write(&pernet_ops_rwsem); if (setup_net(&init_net, &init_user_ns)) panic("Could not setup the initial network namespace"); @@ -1119,12 +1116,8 @@ static int __init net_ns_init(void) RTNL_FLAG_DOIT_UNLOCKED); rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid, RTNL_FLAG_DOIT_UNLOCKED); - - return 0; } -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); -- 2.35.1