]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
net: use ns_common_init()
authorChristian Brauner <brauner@kernel.org>
Fri, 12 Sep 2025 11:52:34 +0000 (13:52 +0200)
committerChristian Brauner <brauner@kernel.org>
Fri, 19 Sep 2025 12:26:13 +0000 (14:26 +0200)
Don't cargo-cult the same thing over and over.

Signed-off-by: Christian Brauner <brauner@kernel.org>
net/core/net_namespace.c

index 1b6f3826dd0e1f5d5333e4dccd122afe08759e2c..5fb7bd8ac45afc26661135605a1c70333d5df3fc 100644 (file)
@@ -397,10 +397,22 @@ static __net_init void preinit_net_sysctl(struct net *net)
 }
 
 /* init code that must occur even if setup_net() is not called. */
-static __net_init void preinit_net(struct net *net, struct user_namespace *user_ns)
+static __net_init int preinit_net(struct net *net, struct user_namespace *user_ns)
 {
+       const struct proc_ns_operations *ns_ops;
+       int ret;
+
+#ifdef CONFIG_NET_NS
+       ns_ops = &netns_operations;
+#else
+       ns_ops = NULL;
+#endif
+
+       ret = ns_common_init(&net->ns, ns_ops, false);
+       if (ret)
+               return ret;
+
        refcount_set(&net->passive, 1);
-       refcount_set(&net->ns.count, 1);
        ref_tracker_dir_init(&net->refcnt_tracker, 128, "net_refcnt");
        ref_tracker_dir_init(&net->notrefcnt_tracker, 128, "net_notrefcnt");
 
@@ -420,6 +432,7 @@ static __net_init void preinit_net(struct net *net, struct user_namespace *user_
        INIT_LIST_HEAD(&net->ptype_all);
        INIT_LIST_HEAD(&net->ptype_specific);
        preinit_net_sysctl(net);
+       return 0;
 }
 
 /*
@@ -559,7 +572,9 @@ struct net *copy_net_ns(unsigned long flags,
                goto dec_ucounts;
        }
 
-       preinit_net(net, user_ns);
+       rv = preinit_net(net, user_ns);
+       if (rv < 0)
+               goto dec_ucounts;
        net->ucounts = ucounts;
        get_user_ns(user_ns);
 
@@ -812,15 +827,15 @@ static void net_ns_net_debugfs(struct net *net)
 
 static __net_init int net_ns_net_init(struct net *net)
 {
-#ifdef CONFIG_NET_NS
-       net->ns.ops = &netns_operations;
-#endif
-       net->ns.inum = PROC_NET_INIT_INO;
-       if (net != &init_net) {
-               int ret = ns_alloc_inum(&net->ns);
-               if (ret)
-                       return ret;
-       }
+       int ret = 0;
+
+       if (net == &init_net)
+               net->ns.inum = PROC_NET_INIT_INO;
+       else
+               ret = proc_alloc_inum(&to_ns_common(net)->inum);
+       if (ret)
+               return ret;
+
        net_ns_net_debugfs(net);
        return 0;
 }
@@ -1282,7 +1297,12 @@ void __init net_ns_init(void)
 #ifdef CONFIG_KEYS
        init_net.key_domain = &init_net_key_domain;
 #endif
-       preinit_net(&init_net, &init_user_ns);
+       /*
+        * This currently cannot fail as the initial network namespace
+        * has a static inode number.
+        */
+       if (preinit_net(&init_net, &init_user_ns))
+               panic("Could not preinitialize the initial network namespace");
 
        down_write(&pernet_ops_rwsem);
        if (setup_net(&init_net))