[PATCH 6/9] mnt: simplify ns_common_init() handling

Christian Brauner posted 9 patches 2 weeks, 1 day ago
[PATCH 6/9] mnt: simplify ns_common_init() handling
Posted by Christian Brauner 2 weeks, 1 day ago
Assign the reserved MNT_NS_ANON_INO sentinel to anonymous mount
namespaces and cleanup the initial mount ns allocation. This is just a
preparatory patch and the ns->inum check in ns_common_init() will be
dropped in the next patch.

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 fs/namespace.c    | 7 ++++---
 kernel/nscommon.c | 2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/fs/namespace.c b/fs/namespace.c
index c8251545d57e..09e4ecd44972 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -4104,6 +4104,8 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
 		return ERR_PTR(-ENOMEM);
 	}
 
+	if (anon)
+		new_ns->ns.inum = MNT_NS_ANON_INO;
 	ret = ns_common_init(&new_ns->ns, &mntns_operations, !anon);
 	if (ret) {
 		kfree(new_ns);
@@ -6020,10 +6022,9 @@ static void __init init_mount_tree(void)
 	if (IS_ERR(mnt))
 		panic("Can't create rootfs");
 
-	ns = alloc_mnt_ns(&init_user_ns, true);
+	ns = alloc_mnt_ns(&init_user_ns, false);
 	if (IS_ERR(ns))
 		panic("Can't allocate initial namespace");
-	ns->ns.inum = PROC_MNT_INIT_INO;
 	m = real_mount(mnt);
 	ns->root = m;
 	ns->nr_mounts = 1;
@@ -6037,7 +6038,7 @@ static void __init init_mount_tree(void)
 	set_fs_pwd(current->fs, &root);
 	set_fs_root(current->fs, &root);
 
-	ns_tree_add(ns);
+	ns_tree_add_raw(ns);
 	init_mnt_ns = ns;
 }
 
diff --git a/kernel/nscommon.c b/kernel/nscommon.c
index ebf4783d0505..e10fad8afe61 100644
--- a/kernel/nscommon.c
+++ b/kernel/nscommon.c
@@ -5,7 +5,7 @@
 int ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
 		   bool alloc_inum)
 {
-	if (alloc_inum) {
+	if (alloc_inum && !ns->inum) {
 		int ret;
 		ret = proc_alloc_inum(&ns->inum);
 		if (ret)

-- 
2.47.3
Re: [PATCH 6/9] mnt: simplify ns_common_init() handling
Posted by Jan Kara 2 weeks ago
On Wed 17-09-25 12:28:05, Christian Brauner wrote:
> Assign the reserved MNT_NS_ANON_INO sentinel to anonymous mount
> namespaces and cleanup the initial mount ns allocation. This is just a
> preparatory patch and the ns->inum check in ns_common_init() will be
> dropped in the next patch.
> 
> Signed-off-by: Christian Brauner <brauner@kernel.org>

...
> ---
>  fs/namespace.c    | 7 ++++---
>  kernel/nscommon.c | 2 +-
>  2 files changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/namespace.c b/fs/namespace.c
> index c8251545d57e..09e4ecd44972 100644
> --- a/fs/namespace.c
> +++ b/fs/namespace.c
> @@ -4104,6 +4104,8 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
>  		return ERR_PTR(-ENOMEM);
>  	}
>  
> +	if (anon)
> +		new_ns->ns.inum = MNT_NS_ANON_INO;
>  	ret = ns_common_init(&new_ns->ns, &mntns_operations, !anon);
>  	if (ret) {
>  		kfree(new_ns);
> @@ -6020,10 +6022,9 @@ static void __init init_mount_tree(void)
>  	if (IS_ERR(mnt))
>  		panic("Can't create rootfs");
>  
> -	ns = alloc_mnt_ns(&init_user_ns, true);
> +	ns = alloc_mnt_ns(&init_user_ns, false);
>  	if (IS_ERR(ns))
>  		panic("Can't allocate initial namespace");
> -	ns->ns.inum = PROC_MNT_INIT_INO;
>  	m = real_mount(mnt);
>  	ns->root = m;
>  	ns->nr_mounts = 1;
> @@ -6037,7 +6038,7 @@ static void __init init_mount_tree(void)
>  	set_fs_pwd(current->fs, &root);
>  	set_fs_root(current->fs, &root);
>  
> -	ns_tree_add(ns);
> +	ns_tree_add_raw(ns);

But we don't have ns->ns_id set by anything now? Or am I missing something?

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR
Re: [PATCH 6/9] mnt: simplify ns_common_init() handling
Posted by Christian Brauner 2 weeks ago
On Wed, Sep 17, 2025 at 06:45:11PM +0200, Jan Kara wrote:
> On Wed 17-09-25 12:28:05, Christian Brauner wrote:
> > Assign the reserved MNT_NS_ANON_INO sentinel to anonymous mount
> > namespaces and cleanup the initial mount ns allocation. This is just a
> > preparatory patch and the ns->inum check in ns_common_init() will be
> > dropped in the next patch.
> > 
> > Signed-off-by: Christian Brauner <brauner@kernel.org>
> 
> ...
> > ---
> >  fs/namespace.c    | 7 ++++---
> >  kernel/nscommon.c | 2 +-
> >  2 files changed, 5 insertions(+), 4 deletions(-)
> > 
> > diff --git a/fs/namespace.c b/fs/namespace.c
> > index c8251545d57e..09e4ecd44972 100644
> > --- a/fs/namespace.c
> > +++ b/fs/namespace.c
> > @@ -4104,6 +4104,8 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
> >  		return ERR_PTR(-ENOMEM);
> >  	}
> >  
> > +	if (anon)
> > +		new_ns->ns.inum = MNT_NS_ANON_INO;
> >  	ret = ns_common_init(&new_ns->ns, &mntns_operations, !anon);
> >  	if (ret) {
> >  		kfree(new_ns);
> > @@ -6020,10 +6022,9 @@ static void __init init_mount_tree(void)
> >  	if (IS_ERR(mnt))
> >  		panic("Can't create rootfs");
> >  
> > -	ns = alloc_mnt_ns(&init_user_ns, true);
> > +	ns = alloc_mnt_ns(&init_user_ns, false);
> >  	if (IS_ERR(ns))
> >  		panic("Can't allocate initial namespace");
> > -	ns->ns.inum = PROC_MNT_INIT_INO;
> >  	m = real_mount(mnt);
> >  	ns->root = m;
> >  	ns->nr_mounts = 1;
> > @@ -6037,7 +6038,7 @@ static void __init init_mount_tree(void)
> >  	set_fs_pwd(current->fs, &root);
> >  	set_fs_root(current->fs, &root);
> >  
> > -	ns_tree_add(ns);
> > +	ns_tree_add_raw(ns);
> 
> But we don't have ns->ns_id set by anything now? Or am I missing something?

It is set in alloc_mnt_ns() via ns_tree_gen_id(). :)
Unless I'm missing something.
But we still need to add PROC_MNT_INIT_INTO until the final conversion.
I'm fixing that in-tree.
Re: [PATCH 6/9] mnt: simplify ns_common_init() handling
Posted by Jan Kara 2 weeks ago
On Thu 18-09-25 10:15:01, Christian Brauner wrote:
> On Wed, Sep 17, 2025 at 06:45:11PM +0200, Jan Kara wrote:
> > On Wed 17-09-25 12:28:05, Christian Brauner wrote:
> > > Assign the reserved MNT_NS_ANON_INO sentinel to anonymous mount
> > > namespaces and cleanup the initial mount ns allocation. This is just a
> > > preparatory patch and the ns->inum check in ns_common_init() will be
> > > dropped in the next patch.
> > > 
> > > Signed-off-by: Christian Brauner <brauner@kernel.org>
> > 
> > ...
> > > ---
> > >  fs/namespace.c    | 7 ++++---
> > >  kernel/nscommon.c | 2 +-
> > >  2 files changed, 5 insertions(+), 4 deletions(-)
> > > 
> > > diff --git a/fs/namespace.c b/fs/namespace.c
> > > index c8251545d57e..09e4ecd44972 100644
> > > --- a/fs/namespace.c
> > > +++ b/fs/namespace.c
> > > @@ -4104,6 +4104,8 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
> > >  		return ERR_PTR(-ENOMEM);
> > >  	}
> > >  
> > > +	if (anon)
> > > +		new_ns->ns.inum = MNT_NS_ANON_INO;
> > >  	ret = ns_common_init(&new_ns->ns, &mntns_operations, !anon);
> > >  	if (ret) {
> > >  		kfree(new_ns);
> > > @@ -6020,10 +6022,9 @@ static void __init init_mount_tree(void)
> > >  	if (IS_ERR(mnt))
> > >  		panic("Can't create rootfs");
> > >  
> > > -	ns = alloc_mnt_ns(&init_user_ns, true);
> > > +	ns = alloc_mnt_ns(&init_user_ns, false);
> > >  	if (IS_ERR(ns))
> > >  		panic("Can't allocate initial namespace");
> > > -	ns->ns.inum = PROC_MNT_INIT_INO;
> > >  	m = real_mount(mnt);
> > >  	ns->root = m;
> > >  	ns->nr_mounts = 1;
> > > @@ -6037,7 +6038,7 @@ static void __init init_mount_tree(void)
> > >  	set_fs_pwd(current->fs, &root);
> > >  	set_fs_root(current->fs, &root);
> > >  
> > > -	ns_tree_add(ns);
> > > +	ns_tree_add_raw(ns);
> > 
> > But we don't have ns->ns_id set by anything now? Or am I missing something?
> 
> It is set in alloc_mnt_ns() via ns_tree_gen_id(). :)
> Unless I'm missing something.

Ah, right. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR