OpenVZ Forum


Home » Mailing lists » Devel » [PATCH] Merge sys_clone() and sys_unshare() nsproxy handling
Re: [PATCH] Merge sys_clone() and sys_unshare() nsproxy handling [message #17529 is a reply to message #17528] Tue, 27 February 2007 05:05 Go to previous messageGo to previous message
serue is currently offline  serue
Messages: 750
Registered: February 2006
Senior Member
Quoting Badari Pulavarty (pbadari@us.ibm.com):
> Hi,
> 
> sys_clone() and sys_unshare() handles copy/clone of nsproxy
> and its associated namespaces differently.
> 
> This patch merges all the handling into common functions
> (as much as possible). 
> 
> Please review. If no objections, I would like to submit for 
> inclusion into -mm.

Thanks, Badari, this looks great.  Barring failures in the set of
tests I just fired off, I'd love to see this merged.

thanks,
-serge

> Ran standard LTP tests, unshare uts, ipc tests fine.
> 
> Thanks,
> Badari
> 
> Merge sys_clone/sys_unshare nsproxy and namespace copy handling.
> 
> - Create a new nsproxy and its associated namespaces and pass it
> back to caller to attach it to right process.
> 
> - Changed all copy_*_ns() routines to attach the namespace to
> passed in new nsproxy instead of task->nsproxy.
> 
> - Get rid of all individual unshare_*_ns() routines and make
> use of copy_*_ns() instead.
> 
> 
> Signed-off-by: Badari Pulavarty <pbadari@us.ibm.com>
> ---
>  fs/namespace.c                |   11 +--
>  include/linux/ipc.h           |    8 +-
>  include/linux/mnt_namespace.h |    5 -
>  include/linux/nsproxy.h       |    2 
>  include/linux/pid_namespace.h |    3 
>  include/linux/utsname.h       |   16 -----
>  ipc/util.c                    |   24 -------
>  kernel/fork.c                 |   85 +-------------------------
>  kernel/nsproxy.c              |  133 +++++++++++++++++++++++++-----------------
>  kernel/pid.c                  |    4 -
>  kernel/utsname.c              |   24 -------
>  11 files changed, 113 insertions(+), 202 deletions(-)
> 
> Index: linux-2.6.21-rc1/fs/namespace.c
> ===================================================================
> --- linux-2.6.21-rc1.orig/fs/namespace.c	2007-02-20 20:32:30.000000000 -0800
> +++ linux-2.6.21-rc1/fs/namespace.c	2007-02-26 10:27:42.000000000 -0800
> @@ -1441,10 +1441,9 @@ dput_out:
>   * Allocate a new namespace structure and populate it with contents
>   * copied from the namespace of the passed in task structure.
>   */
> -struct mnt_namespace *dup_mnt_ns(struct task_struct *tsk,
> +static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
>  		struct fs_struct *fs)
>  {
> -	struct mnt_namespace *mnt_ns = tsk->nsproxy->mnt_ns;
>  	struct mnt_namespace *new_ns;
>  	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
>  	struct vfsmount *p, *q;
> @@ -1509,9 +1508,9 @@ struct mnt_namespace *dup_mnt_ns(struct 
>  	return new_ns;
>  }
>  
> -int copy_mnt_ns(int flags, struct task_struct *tsk)
> +int copy_mnt_ns(int flags, struct mnt_namespace *ns, struct nsproxy *new_nsp,
> +		struct fs_struct *new_fs)
>  {
> -	struct mnt_namespace *ns = tsk->nsproxy->mnt_ns;
>  	struct mnt_namespace *new_ns;
>  	int err = 0;
>  
> @@ -1528,13 +1527,13 @@ int copy_mnt_ns(int flags, struct task_s
>  		goto out;
>  	}
>  
> -	new_ns = dup_mnt_ns(tsk, tsk->fs);
> +	new_ns = dup_mnt_ns(ns, new_fs);
>  	if (!new_ns) {
>  		err = -ENOMEM;
>  		goto out;
>  	}
>  
> -	tsk->nsproxy->mnt_ns = new_ns;
> +	new_nsp->mnt_ns = new_ns;
>  
>  out:
>  	put_mnt_ns(ns);
> Index: linux-2.6.21-rc1/include/linux/mnt_namespace.h
> ===================================================================
> --- linux-2.6.21-rc1.orig/include/linux/mnt_namespace.h	2007-02-20 20:32:30.000000000 -0800
> +++ linux-2.6.21-rc1/include/linux/mnt_namespace.h	2007-02-26 10:27:42.000000000 -0800
> @@ -14,10 +14,9 @@ struct mnt_namespace {
>  	int event;
>  };
>  
> -extern int copy_mnt_ns(int, struct task_struct *);
> -extern void __put_mnt_ns(struct mnt_namespace *ns);
> -extern struct mnt_namespace *dup_mnt_ns(struct task_struct *,
> +extern int copy_mnt_ns(int, struct mnt_namespace *, struct nsproxy *,
>  		struct fs_struct *);
> +extern void __put_mnt_ns(struct mnt_namespace *ns);
>  
>  static inline void put_mnt_ns(struct mnt_namespace *ns)
>  {
> Index: linux-2.6.21-rc1/kernel/nsproxy.c
> ===================================================================
> --- linux-2.6.21-rc1.orig/kernel/nsproxy.c	2007-02-20 20:32:30.000000000 -0800
> +++ linux-2.6.21-rc1/kernel/nsproxy.c	2007-02-26 10:27:42.000000000 -0800
> @@ -38,8 +38,6 @@ void get_task_namespaces(struct task_str
>  
>  /*
>   * creates a copy of "orig" with refcount 1.
> - * This does not grab references to the contained namespaces,
> - * so that needs to be done by dup_namespaces.
>   */
>  static inline struct nsproxy *clone_namespaces(struct nsproxy *orig)
>  {
> @@ -52,26 +50,50 @@ static inline struct nsproxy *clone_name
>  }
>  
>  /*
> - * copies the nsproxy, setting refcount to 1, and grabbing a
> - * reference to all contained namespaces.  Called from
> - * sys_unshare()
> + * Create new nsproxy and all of its the associated namespaces.
> + * Return the newly created nsproxy.  Do not attach this to the task,
> + * leave it to the caller to do proper locking and attach it to task.
>   */
> -struct nsproxy *dup_namespaces(struct nsproxy *orig)
> +static struct nsproxy *create_new_namespaces(int flags, struct task_struct *tsk,
> +			struct fs_struct *new_fs)
>  {
> -	struct nsproxy *ns = clone_namespaces(orig);
> +	struct nsproxy *new_nsp;
> +	int err;
>  
> -	if (ns) {
> -		if (ns->mnt_ns)
> -			get_mnt_ns(ns->mnt_ns);
> -		if (ns->uts_ns)
> -			get_uts_ns(ns->uts_ns);
> -		if (ns->ipc_ns)
> -			get_ipc_ns(ns->ipc_ns);
> -		if (ns->pid_ns)
> -			get_pid_ns(ns->pid_ns);
> -	}
> +	new_nsp = clone_namespaces(tsk->nsproxy);
> +	if (!new_nsp)
> +		return ERR_PTR(-ENOMEM);
>  
> -	return ns;
> +	err = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, new_nsp, new_fs);
> +	if (err)
> +		goto out_ns;
> +
> +	err = copy_utsname(flags, tsk->nsproxy->uts_ns, new_nsp);
> +	if (err)
> +		goto out_uts;
> +
> +	err = copy_ipcs(flags, tsk->nsproxy->ipc_ns, new_nsp);
> +	if (err)
> +		goto out_ipc;
> +
> +	err = copy_pid_ns(flags, tsk->nsproxy->pid_ns, new_nsp);
> +	if (err)
> +		goto out_pid;
> +
> +	return new_nsp;
> +
> +out_pid:
> +	if (new_nsp->ipc_ns)
> +		put_ipc_ns(new_nsp->ipc_ns);
> +out_ipc:
> +	if (new_nsp->uts_ns)
> +		put_uts_ns(new_nsp->uts_ns);
> +out_uts:
> +	if (new_nsp->mnt_ns)
> +		put_mnt_ns(new_nsp->mnt_ns);
> +out_ns:
> +	kfree(new_nsp);
> +	return ERR_PTR(err);
>  }
>  
>  /*
> @@ -92,47 +114,16 @@ int copy_namespaces(int flags, struct ta
>  	if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
>  		return 0;
>  
> -	new_ns = clone_namespaces(old_ns);
> -	if (!new_ns) {
> -		err = -ENOMEM;
> +	new_ns = create_new_namespaces(flags, tsk, tsk->fs);
> +	if (IS_ERR(new_ns)) {
> +		err = PTR_ERR(new_ns);
>  		goto out;
>  	}
>  
>  	tsk->nsproxy = new_ns;
> -
> -	err = copy_mnt_ns(flags, tsk);
> -	if (err)
> -		goto out_ns;
> -
> -	err = copy_utsname(flags, tsk);
> -	if (err)
> -		goto out_uts;
> -
> -	err = copy_ipcs(flags, tsk);
> -	if (err)
> -		goto out_ipc;
> -
> -	err = copy_pid_ns(flags, tsk);
> -	if (err)
> -		goto out_pid;
> -
>  out:
>  	put_nsproxy(old_ns);
>  	return err;
> -
> -out_pid:
> -	if (new_ns->ipc_ns)
> -		put_ipc_ns(new_ns->ipc_ns);
> -out_ipc:
> -	if (new_ns->uts_ns)
> -		put_uts_ns(new_ns->uts_ns);
> -out_uts:
> -	if (new_ns->mnt_ns)
> -		put_mnt_ns(new_ns->mnt_ns);
> -out_ns:
> -	tsk->nsproxy = old_ns;
> -	kfree(new_ns);
> -	goto out;
>  }
>  
>  void free_nsproxy(struct nsproxy *ns)
> @@ -147,3 +138,41 @@ void free_nsproxy(struct nsproxy *ns)
>  		put_pid_ns(ns->pid_ns);
>  	kfree(ns);
>  }
> +
> +/*
> + * Called from unshare. Unshare all the namespaces part of nsproxy.
> + * On sucess, returns the new nsproxy and a reference to old nsproxy
> + * to make sure it stays around.
> + */
> +int unshare_nsproxy_namespaces(unsigned long unshare_flags,
> +		struct nsproxy **new_nsp, struct fs_struct *new_fs)
> +{
> +	struct nsproxy *old_ns = current->nsproxy;
> +	int err = 0;
> +
> +	if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
> +		return 0;
> +
> +#ifndef CONFIG_IPC_NS
> +	if (unshare_flags & CLONE_NEWIPC)
> +		return -EINVAL;
> +#endif
> +
> +#ifndef CONFIG_UTS_NS
> +	if (unshare_flags & CLONE_NEWUTS)
> +		return -EINVAL;
> +#endif
> +
> +	if (!capable(CAP_SYS_ADMIN))
> +		return -EPERM;
> +
> +	get_nsproxy(old_ns);
> +
> +	*new_nsp = create_new_namespaces(unshare_flags, current,
> +				new_fs ? new_fs : current->fs);
> +	if (IS_ERR(*new_nsp)) {
> +		err = PTR_ERR(*new_nsp);
> +		put_nsproxy(old_ns);
> +	}
> +	return err;
> +}
> Index: linux-2.6.21-rc1/include/linux/utsname.h
> ===================================================================
> --- linux-2.6.21-rc1.orig/include/linux/utsname.h	2007-02-20 20:32:30.000000000 -0800
> +++ linux-2.6.21-rc1/include/linux/utsname.h	2007-02-26 10:27:42.000000000 -0800
> @@ -49,9 +49,7 @@ static inline void get_uts_ns(struct uts
>  }
>  
>  #ifdef CONFIG_UTS_NS
> -extern int unshare_utsname(unsigned long unshare_flags,
> -				struct uts_namespace **new_uts);
> -extern int copy_utsname(int flags, struct task_struct *tsk);
> +extern int copy_utsname(int flags, struct 
...

 
Read Message
Read Message
Read Message
Previous Topic: [PATCH 2/2] src/lib/config.c: Fix "cast discards qualifiers from pointer target type" warn
Next Topic: [PATCH] attach_pid() with struct pid parameter
Goto Forum:
  


Current Time: Sat Sep 20 15:52:12 GMT 2025

Total time taken to generate the page: 0.05415 seconds