2009-04-10 02:35:51

by Alexey Dobriyan

[permalink] [raw]
Subject: [PATCH 07/30] netns: extract net_create()

net_create() will be used by C/R code to create fresh netns on restart.

Signed-off-by: Alexey Dobriyan <[email protected]>
---

include/net/net_namespace.h | 1 +
net/core/net_namespace.c | 44 ++++++++++++++++++++------------------------
2 files changed, 21 insertions(+), 24 deletions(-)

--- a/include/net/net_namespace.h
+++ b/include/net/net_namespace.h
@@ -107,6 +107,7 @@ static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
extern struct list_head net_namespace_list;

#ifdef CONFIG_NET_NS
+struct net *net_create(void);
extern void __put_net(struct net *net);

static inline struct net *get_net(struct net *net)
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -115,38 +115,34 @@ static void net_free(struct net *net)
kmem_cache_free(net_cachep, net);
}

-struct net *copy_net_ns(unsigned long flags, struct net *old_net)
+struct net *net_create(void)
{
- struct net *new_net = NULL;
- int err;
-
- if (!(flags & CLONE_NEWNET))
- return get_net(old_net);
-
- err = -ENOMEM;
- new_net = net_alloc();
- if (!new_net)
- goto out_err;
+ struct net *net;
+ int rv;

+ net = net_alloc();
+ if (!net)
+ return ERR_PTR(-ENOMEM);
mutex_lock(&net_mutex);
- err = setup_net(new_net);
- if (!err) {
+ rv = setup_net(net);
+ if (rv == 0) {
rtnl_lock();
- list_add_tail(&new_net->list, &net_namespace_list);
+ list_add_tail(&net->list, &net_namespace_list);
rtnl_unlock();
}
mutex_unlock(&net_mutex);
+ if (rv < 0) {
+ net_free(net);
+ return ERR_PTR(rv);
+ }
+ return net;
+}

- if (err)
- goto out_free;
-out:
- return new_net;
-
-out_free:
- net_free(new_net);
-out_err:
- new_net = ERR_PTR(err);
- goto out;
+struct net *copy_net_ns(unsigned long flags, struct net *old_net)
+{
+ if (!(flags & CLONE_NEWNET))
+ return get_net(old_net);
+ return net_create();
}

static void cleanup_net(struct work_struct *work)


2009-04-10 09:05:28

by Ingo Molnar

[permalink] [raw]
Subject: Re: [PATCH 07/30] netns: extract net_create()


* Alexey Dobriyan <[email protected]> wrote:

> --- a/include/net/net_namespace.h
> +++ b/include/net/net_namespace.h
> @@ -107,6 +107,7 @@ static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
> extern struct list_head net_namespace_list;
>
> #ifdef CONFIG_NET_NS
> +struct net *net_create(void);
> extern void __put_net(struct net *net);

Ditto.

Ingo

2009-04-10 22:56:49

by Serge E. Hallyn

[permalink] [raw]
Subject: Re: [PATCH 07/30] netns: extract net_create()

Quoting Alexey Dobriyan ([email protected]):
> net_create() will be used by C/R code to create fresh netns on restart.
>
> Signed-off-by: Alexey Dobriyan <[email protected]>

Acked-by: Serge Hallyn <[email protected]>

Although this ends up making a new assumption that setup_net(), and
therefore the pernet_ops->init() functions, do not return error > 0.
There's nothing actually stopping that (outside of code review, but
there appears to be no comment anywhere saying that's uncooth).

If it returns > 0, then we return the ns as though it were valid, but
don't add it to the list of namespaces... Do we care?

-serge

> ---
>
> include/net/net_namespace.h | 1 +
> net/core/net_namespace.c | 44 ++++++++++++++++++++------------------------
> 2 files changed, 21 insertions(+), 24 deletions(-)
>
> --- a/include/net/net_namespace.h
> +++ b/include/net/net_namespace.h
> @@ -107,6 +107,7 @@ static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
> extern struct list_head net_namespace_list;
>
> #ifdef CONFIG_NET_NS
> +struct net *net_create(void);
> extern void __put_net(struct net *net);
>
> static inline struct net *get_net(struct net *net)
> --- a/net/core/net_namespace.c
> +++ b/net/core/net_namespace.c
> @@ -115,38 +115,34 @@ static void net_free(struct net *net)
> kmem_cache_free(net_cachep, net);
> }
>
> -struct net *copy_net_ns(unsigned long flags, struct net *old_net)
> +struct net *net_create(void)
> {
> - struct net *new_net = NULL;
> - int err;
> -
> - if (!(flags & CLONE_NEWNET))
> - return get_net(old_net);
> -
> - err = -ENOMEM;
> - new_net = net_alloc();
> - if (!new_net)
> - goto out_err;
> + struct net *net;
> + int rv;
>
> + net = net_alloc();
> + if (!net)
> + return ERR_PTR(-ENOMEM);
> mutex_lock(&net_mutex);
> - err = setup_net(new_net);
> - if (!err) {
> + rv = setup_net(net);
> + if (rv == 0) {
> rtnl_lock();
> - list_add_tail(&new_net->list, &net_namespace_list);
> + list_add_tail(&net->list, &net_namespace_list);
> rtnl_unlock();
> }
> mutex_unlock(&net_mutex);
> + if (rv < 0) {
> + net_free(net);
> + return ERR_PTR(rv);
> + }
> + return net;
> +}
>
> - if (err)
> - goto out_free;
> -out:
> - return new_net;
> -
> -out_free:
> - net_free(new_net);
> -out_err:
> - new_net = ERR_PTR(err);
> - goto out;
> +struct net *copy_net_ns(unsigned long flags, struct net *old_net)
> +{
> + if (!(flags & CLONE_NEWNET))
> + return get_net(old_net);
> + return net_create();
> }
>
> static void cleanup_net(struct work_struct *work)

2009-04-13 07:11:19

by Alexey Dobriyan

[permalink] [raw]
Subject: Re: [PATCH 07/30] netns: extract net_create()

On Fri, Apr 10, 2009 at 05:56:36PM -0500, Serge E. Hallyn wrote:
> Quoting Alexey Dobriyan ([email protected]):
> > net_create() will be used by C/R code to create fresh netns on restart.
> >
> > Signed-off-by: Alexey Dobriyan <[email protected]>
>
> Acked-by: Serge Hallyn <[email protected]>
>
> Although this ends up making a new assumption that setup_net(), and
> therefore the pernet_ops->init() functions, do not return error > 0.
> There's nothing actually stopping that (outside of code review, but
> there appears to be no comment anywhere saying that's uncooth).
>
> If it returns > 0, then we return the ns as though it were valid, but
> don't add it to the list of namespaces... Do we care?

I think, no.

Recalling all the code put under netns changes, ->init hooks were put
under 0/-E convention. For sure, my changes in netns xtables,
conntracking and xfrm were definitely under it.

> > -struct net *copy_net_ns(unsigned long flags, struct net *old_net)
> > +struct net *net_create(void)