Skip to contentSkip to conversation

ns: support file handles

PATCH In review Christian Brauner 7 participants 71 messages Sep 12, 2025 – Sep 19, 2025

Subsystems (12)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

v2 cover ns: support file handles

For a while now we have supported file handles for pidfds. This has proven to be very useful.

Extend the concept to cover namespaces as well. After this patchset it is possible to encode and decode namespace file handles using the commong name_to_handle_at() and open_by_handle_at() apis.

Namespaces file descriptors can already be derived from pidfds which means they aren't subject to overmount protection bugs. IOW, it's irrelevant if the caller would not have access to an appropriate /proc/<pid>/ns/ directory as they could always just derive the namespace based on a pidfd already.

It has the same advantage as pidfds. It's possible to reliably and for the lifetime of the system refer to a namespace without pinning any resources and to compare them.

Permission checking is kept simple. If the caller is located in the namespace the file handle refers to they are able to open it otherwise they must hold privilege over the owning namespace of the relevant namespace.

Both the network namespace and the mount namespace already have an associated cookie that isn't recycled and is fully exposed to userspace. Move this into ns_common and use the same id space for all namespaces so they can trivially and reliably be compared.

There's more coming based on the iterator infrastructure but the series is large enough and focuses on file handles.

Extensive selftests included.

---
Changes in v2:
- Address various review comments.
- Use a common NS_GET_ID ioctl() instead of individual ioctls.
- Link to v1: https://lore.kernel.org/[email protected]

---
Christian Brauner (33):
pidfs: validate extensible ioctls
nsfs: drop tautological ioctl() check
nsfs: validate extensible ioctls
block: use extensible_ioctl_valid()
ns: move to_ns_common() to ns_common.h
nsfs: add nsfs.h header
ns: uniformly initialize ns_common
cgroup: use ns_common_init()
ipc: use ns_common_init()
mnt: use ns_common_init()
net: use ns_common_init()
pid: use ns_common_init()
time: use ns_common_init()
user: use ns_common_init()
uts: use ns_common_init()
ns: remove ns_alloc_inum()
nstree: make iterator generic
mnt: support ns lookup
cgroup: support ns lookup
ipc: support ns lookup
net: support ns lookup
pid: support ns lookup
time: support ns lookup
user: support ns lookup
uts: support ns lookup
ns: add to_<type>_ns() to respective headers
nsfs: add current_in_namespace()
nsfs: support file handles
nsfs: support exhaustive file handles
nsfs: add missing id retrieval support
tools: update nsfs.h uapi header selftests/namespaces: add identifier selftests selftests/namespaces: add file handle selftests

 block/blk-integrity.c                              |    8 +-
 fs/fhandle.c                                       |    6 +
 fs/internal.h                                      |    1 +
 fs/mount.h                                         |   10 +-
 fs/namespace.c                                     |  156 +--
 fs/nsfs.c                                          |  201 ++-
 fs/pidfs.c                                         |    2 +-
 include/linux/cgroup.h                             |    5 +
 include/linux/exportfs.h                           |    6 +
 include/linux/fs.h                                 |   14 +
 include/linux/ipc_namespace.h                      |    5 +
 include/linux/ns_common.h                          |   29 +
 include/linux/nsfs.h                               |   40 +
 include/linux/nsproxy.h                            |   11 -
 include/linux/nstree.h                             |   89 ++
 include/linux/pid_namespace.h                      |    5 +
 include/linux/proc_ns.h                            |   32 +-
 include/linux/time_namespace.h                     |    9 +
 include/linux/user_namespace.h                     |    5 +
 include/linux/utsname.h                            |    5 +
 include/net/net_namespace.h                        |    6 +
 include/uapi/linux/fcntl.h                         |    1 +
 include/uapi/linux/nsfs.h                          |   15 +-
 init/main.c                                        |    2 +
 ipc/msgutil.c                                      |    1 +
 ipc/namespace.c                                    |   12 +-
 ipc/shm.c                                          |    2 +
 kernel/Makefile                                    |    2 +-
 kernel/cgroup/cgroup.c                             |    2 +
 kernel/cgroup/namespace.c                          |   24 +-
 kernel/nstree.c                                    |  233 ++++
 kernel/pid_namespace.c                             |   13 +-
 kernel/time/namespace.c                            |   23 +-
 kernel/user_namespace.c                            |   17 +-
 kernel/utsname.c                                   |   28 +-
 net/core/net_namespace.c                           |   59 +-
 tools/include/uapi/linux/nsfs.h                    |   17 +-
 tools/testing/selftests/namespaces/.gitignore      |    2 +
 tools/testing/selftests/namespaces/Makefile        |    7 +
 tools/testing/selftests/namespaces/config          |    7 +
 .../selftests/namespaces/file_handle_test.c        | 1429 ++++++++++++++++++++
 tools/testing/selftests/namespaces/nsid_test.c     |  986 ++++++++++++++
 42 files changed, 3257 insertions(+), 270 deletions(-)

---
base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
change-id: 20250905-work-namespace-c68826dda0d4

author maintainer
#1 lore raw

PATCH v2 1/33 pidfs: validate extensible ioctls

Validate extensible ioctls stricter than we do now.

---
 fs/pidfs.c         |  2 +-
 include/linux/fs.h | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
fs/pidfs.c +1 -1
index edc35522d75c..0a5083b9cce5 100644
@@ -440,7 +440,7 @@ static bool pidfs_ioctl_valid(unsigned int cmd)
 		 * erronously mistook the file descriptor for a pidfd.
 		 * This is not perfect but will catch most cases.
 		 */
-		return (_IOC_TYPE(cmd) == _IOC_TYPE(PIDFD_GET_INFO));
+		return extensible_ioctl_valid(cmd, PIDFD_GET_INFO, PIDFD_INFO_SIZE_VER0);
 	}
 
 	return false;
include/linux/fs.h +14 -0
index d7ab4f96d705..2f2edc53bf3c 100644
@@ -4023,4 +4023,18 @@ static inline bool vfs_empty_path(int dfd, const char __user *path)
 
 int generic_atomic_write_valid(struct kiocb *iocb, struct iov_iter *iter);
 
+static inline bool extensible_ioctl_valid(unsigned int cmd_a,
+					  unsigned int cmd_b, size_t min_size)
+{
+	if (_IOC_DIR(cmd_a) != _IOC_DIR(cmd_b))
+		return false;
+	if (_IOC_TYPE(cmd_a) != _IOC_TYPE(cmd_b))
+		return false;
+	if (_IOC_NR(cmd_a) != _IOC_NR(cmd_b))
+		return false;
+	if (_IOC_SIZE(cmd_a) < min_size)
+		return false;
+	return true;
+}
+
 #endif /* _LINUX_FS_H */
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH v2 2/33 nsfs: drop tautological ioctl() check

In reply to Christian Brauner

This check does absolutely nothing. So drop it.

---
 fs/nsfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nsfs.c +1 -1
index 59aa801347a7..ce51041bb624 100644
@@ -163,7 +163,7 @@ static bool nsfs_ioctl_valid(unsigned int cmd)
 	case NS_GET_TGID_FROM_PIDNS:
 	case NS_GET_PID_IN_PIDNS:
 	case NS_GET_TGID_IN_PIDNS:
-		return (_IOC_TYPE(cmd) == _IOC_TYPE(cmd));
+		return true;
 	}
 
 	/* Extensible ioctls require some extra handling. */
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH v2 3/33 nsfs: validate extensible ioctls

In reply to Christian Brauner

Validate extensible ioctls stricter than we do now.

---
 fs/nsfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/nsfs.c +3 -1
index ce51041bb624..d016d36272d4 100644
@@ -169,9 +169,11 @@ static bool nsfs_ioctl_valid(unsigned int cmd)
 	/* Extensible ioctls require some extra handling. */
 	switch (_IOC_NR(cmd)) {
 	case _IOC_NR(NS_MNT_GET_INFO):
+		return extensible_ioctl_valid(cmd, NS_MNT_GET_INFO, MNT_NS_INFO_SIZE_VER0);
 	case _IOC_NR(NS_MNT_GET_NEXT):
+		return extensible_ioctl_valid(cmd, NS_MNT_GET_NEXT, MNT_NS_INFO_SIZE_VER0);
 	case _IOC_NR(NS_MNT_GET_PREV):
-		return (_IOC_TYPE(cmd) == _IOC_TYPE(cmd));
+		return extensible_ioctl_valid(cmd, NS_MNT_GET_PREV, MNT_NS_INFO_SIZE_VER0);
 	}
 
 	return false;
Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH v2 4/33 block: use extensible_ioctl_valid()

In reply to Christian Brauner

Use the new extensible_ioctl_valid() helper which is equivalent to what is done here.

---
 block/blk-integrity.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
block/blk-integrity.c +3 -5
index 056b8948369d..609d75d6a39b 100644
@@ -58,16 +58,14 @@ int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
 int blk_get_meta_cap(struct block_device *bdev, unsigned int cmd,
 		     struct logical_block_metadata_cap __user *argp)
 {
-	struct blk_integrity *bi = blk_get_integrity(bdev->bd_disk);
+	struct blk_integrity *bi;
 	struct logical_block_metadata_cap meta_cap = {};
 	size_t usize = _IOC_SIZE(cmd);
 
-	if (_IOC_DIR(cmd)  != _IOC_DIR(FS_IOC_GETLBMD_CAP) ||
-	    _IOC_TYPE(cmd) != _IOC_TYPE(FS_IOC_GETLBMD_CAP) ||
-	    _IOC_NR(cmd)   != _IOC_NR(FS_IOC_GETLBMD_CAP) ||
-	    _IOC_SIZE(cmd) < LBMD_SIZE_VER0)
+	if (extensible_ioctl_valid(cmd, FS_IOC_GETLBMD_CAP, LBMD_SIZE_VER0))
 		return -ENOIOCTLCMD;
 
+	bi = blk_get_integrity(bdev->bd_disk);
 	if (!bi)
 		goto out;
 
Signature · 1 line
2.47.3
#5 lore raw

Re: [PATCH v2 04/33] block: use extensible_ioctl_valid()

Quoted 2 lines · Christian Brauner

Use the new extensible_ioctl_valid() helper which is equivalent to what is done here.

I'm seeing several LTP tests (at least getdents02, msync04, renameat01 and statx12) failing in yesterday's -next pending-fixes on Raspberry Pi 4 with bisections pointing to this commit. renameat01 fails with:

  renameat01    0  TINFO  :  Using /tmp/LTP_renEtNZrS as tmpdir (nfs filesystem)
  renameat01    1  TBROK  :  tst_device.c:97: Could not stat loop device 0
  renameat01    2  TBROK  :  tst_device.c:97: Remaining cases broken

Full log for that run:

https://lava.sirena.org.uk/scheduler/job/1830765#L6680

bisect log for renameat (all the bisects cover the same builds, though I split the jobs up so it's different test jobs for some of the tests):

# bad: [179688318d56cee63802eb49e3503d799c43db6c] Merge branch 'for-linux-next-fixes' of https://gitlab.freedesktop.org/drm/misc/kernel.git
# good: [f83ec76bf285bea5727f478a68b894f5543ca76e] Linux 6.17-rc6
# good: [690aa09b1845c0d5c3c29dabd50a9d0488c97c48] ASoC: Intel: catpt: Expose correct bit depth to userspace
# good: [3254959b4dd065eae396cf78ccc1361460b2f53e] ASoC: amd: amd_sdw: Add quirks for some new Dell laptops
# good: [9004a450fccbeb40a71cc173747da37a459fd4dc] ASoC: codecs: lpass-wsa-macro: Fix speaker quality distortion
# good: [ec630c2c8ce215dd365b8c3644f004f645714a0f] ASoC: SDCA: Reorder members of hide struct to remove holes
# good: [68f27f7c7708183e7873c585ded2f1b057ac5b97] ASoC: qcom: q6apm-lpass-dais: Fix NULL pointer dereference if source graph failed
# good: [0c28431f6fe13f3a3be0978f79c1a7ae8a93d028] ASoC: SOF: imx: Fix devm_ioremap_resource check
# good: [28edfaa10ca1b370b1a27fde632000d35c43402c] ASoC: SDCA: Add quirk for incorrect function types for 3 systems
# good: [35fc531a59694f24a2456569cf7d1a9c6436841c] ASoC: SOF: Intel: hda-stream: Fix incorrect variable used in error message
# good: [9b17d3724df55ecc2bc67978822585f2b023be48] ASoC: wm8974: Correct PLL rate rounding
# good: [f54d87dad7619c8026e95b848d6ef677b9f2b55f] ASoC: rt712: avoid skipping the blind write
git bisect start '179688318d56cee63802eb49e3503d799c43db6c' 'f83ec76bf285bea5727f478a68b894f5543ca76e' '690aa09b1845c0d5c3c29dabd50a9d0488c97c48' '3254959b4dd065eae396cf78ccc1361460b2f53e' '9004a450fccbeb40a71cc173747da37a459fd4dc' 'ec630c2c8ce215dd365b8c3644f004f645714a0f' '68f27f7c7708183e7873c585ded2f1b057ac5b97' '0c28431f6fe13f3a3be0978f79c1a7ae8a93d028' '28edfaa10ca1b370b1a27fde632000d35c43402c' '35fc531a59694f24a2456569cf7d1a9c6436841c' '9b17d3724df55ecc2bc67978822585f2b023be48' 'f54d87dad7619c8026e95b848d6ef677b9f2b55f'

# test job: [690aa09b1845c0d5c3c29dabd50a9d0488c97c48] https://lava.sirena.org.uk/scheduler/job/1805508
# test job: [3254959b4dd065eae396cf78ccc1361460b2f53e] https://lava.sirena.org.uk/scheduler/job/1769788
# test job: [9004a450fccbeb40a71cc173747da37a459fd4dc] https://lava.sirena.org.uk/scheduler/job/1774162
# test job: [ec630c2c8ce215dd365b8c3644f004f645714a0f] https://lava.sirena.org.uk/scheduler/job/1772919
# test job: [68f27f7c7708183e7873c585ded2f1b057ac5b97] https://lava.sirena.org.uk/scheduler/job/1772831
# test job: [0c28431f6fe13f3a3be0978f79c1a7ae8a93d028] https://lava.sirena.org.uk/scheduler/job/1762707
# test job: [28edfaa10ca1b370b1a27fde632000d35c43402c] https://lava.sirena.org.uk/scheduler/job/1762245
# test job: [35fc531a59694f24a2456569cf7d1a9c6436841c] https://lava.sirena.org.uk/scheduler/job/1762959
# test job: [9b17d3724df55ecc2bc67978822585f2b023be48] https://lava.sirena.org.uk/scheduler/job/1758830
# test job: [f54d87dad7619c8026e95b848d6ef677b9f2b55f] https://lava.sirena.org.uk/scheduler/job/1758090
# test job: [179688318d56cee63802eb49e3503d799c43db6c] https://lava.sirena.org.uk/scheduler/job/1830765
# bad: [179688318d56cee63802eb49e3503d799c43db6c] Merge branch 'for-linux-next-fixes' of https://gitlab.freedesktop.org/drm/misc/kernel.git
git bisect bad 179688318d56cee63802eb49e3503d799c43db6c
# test job: [4c3c40178b0e1578a3898092cc33ffb2618edc51] https://lava.sirena.org.uk/scheduler/job/1830986
# good: [4c3c40178b0e1578a3898092cc33ffb2618edc51] Merge branch 'for-next' of https://git.kernel.org/pub/scm/linux/kernel/git/dlemoal/zonefs.git
git bisect good 4c3c40178b0e1578a3898092cc33ffb2618edc51
# test job: [78ca913586336540ec12f262c5bdf16f8925de82] https://lava.sirena.org.uk/scheduler/job/1831233
# bad: [78ca913586336540ec12f262c5bdf16f8925de82] Merge branch 'vfs.all' of https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
git bisect bad 78ca913586336540ec12f262c5bdf16f8925de82
# test job: [06dd3eda0e958cdae48ca755eb5047484f678d78] https://lava.sirena.org.uk/scheduler/job/1831745
# good: [06dd3eda0e958cdae48ca755eb5047484f678d78] Merge branch 'vfs-6.18.rust' into vfs.all
git bisect good 06dd3eda0e958cdae48ca755eb5047484f678d78
# test job: [a7ec4da2c05c5f8be52c2ac884d5672d0a805ee0] https://lava.sirena.org.uk/scheduler/job/1832263
# bad: [a7ec4da2c05c5f8be52c2ac884d5672d0a805ee0] Merge patch series "ns: support file handles"
git bisect bad a7ec4da2c05c5f8be52c2ac884d5672d0a805ee0
# test job: [670f2f915084d1c53f14d59946011b7645601813] https://lava.sirena.org.uk/scheduler/job/1832304
# bad: [670f2f915084d1c53f14d59946011b7645601813] nstree: make iterator generic
git bisect bad 670f2f915084d1c53f14d59946011b7645601813
# test job: [011090b6c0a97a3aa1f659d670d85bbf0eddbe06] https://lava.sirena.org.uk/scheduler/job/1832355
# bad: [011090b6c0a97a3aa1f659d670d85bbf0eddbe06] cgroup: use ns_common_init()
git bisect bad 011090b6c0a97a3aa1f659d670d85bbf0eddbe06
# test job: [60949057a2e71c9244e82608adf269e62e6ac443] https://lava.sirena.org.uk/scheduler/job/1832402
# bad: [60949057a2e71c9244e82608adf269e62e6ac443] block: use extensible_ioctl_valid()
git bisect bad 60949057a2e71c9244e82608adf269e62e6ac443
# test job: [4d906371d1f9fc9ce47b2c8f37444680246557bc] https://lava.sirena.org.uk/scheduler/job/1832439
# good: [4d906371d1f9fc9ce47b2c8f37444680246557bc] nsfs: drop tautological ioctl() check
git bisect good 4d906371d1f9fc9ce47b2c8f37444680246557bc
# test job: [f8527a29f4619f74bc30a9845ea87abb9a6faa1e] https://lava.sirena.org.uk/scheduler/job/1832550
# good: [f8527a29f4619f74bc30a9845ea87abb9a6faa1e] nsfs: validate extensible ioctls
git bisect good f8527a29f4619f74bc30a9845ea87abb9a6faa1e
# first bad commit: [60949057a2e71c9244e82608adf269e62e6ac443] block: use extensible_ioctl_valid()
#6 lore raw

Re: [PATCH v2 04/33] block: use extensible_ioctl_valid()

Yeah, the:

	if (extensible_ioctl_valid(cmd, FS_IOC_GETLBMD_CAP, LBMD_SIZE_VER0))
		return -ENOIOCTLCMD;

test is inverted... It should be if (!valid) return instead of if (valid) return;

regards,
dan carpenter

reviewer
#7 lore raw

Re: [PATCH v2 04/33] block: use extensible_ioctl_valid()

Christian, this bug still seems to be present in your vfs.all branch. Can you please fix it up? Thanks!

Honza

Quoted 11 lines · Dan Carpenter

Yeah, the:

	if (extensible_ioctl_valid(cmd, FS_IOC_GETLBMD_CAP, LBMD_SIZE_VER0))
		return -ENOIOCTLCMD;

test is inverted... It should be if (!valid) return instead of if (valid) return;

regards,
dan carpenter

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#8 lore raw

Re: [PATCH v2 04/33] block: use extensible_ioctl_valid()

Quoted 2 lines · Jan Kara

Christian, this bug still seems to be present in your vfs.all branch. Can you please fix it up? Thanks!

Sorry, I fixed this now!

author maintainer
#9 lore raw

PATCH v2 5/33 ns: move to_ns_common() to ns_common.h

In reply to Christian Brauner

Move the helper to ns_common.h where it belongs.

---
 include/linux/ns_common.h | 20 ++++++++++++++++++++
 include/linux/nsproxy.h   | 11 -----------
 2 files changed, 20 insertions(+), 11 deletions(-)
include/linux/ns_common.h +20 -0
index 7d22ea50b098..bc2e0758e1c9 100644
@@ -6,6 +6,15 @@
 
 struct proc_ns_operations;
 
+struct cgroup_namespace;
+struct ipc_namespace;
+struct mnt_namespace;
+struct net;
+struct pid_namespace;
+struct time_namespace;
+struct user_namespace;
+struct uts_namespace;
+
 struct ns_common {
 	struct dentry *stashed;
 	const struct proc_ns_operations *ops;
@@ -13,4 +22,15 @@ struct ns_common {
 	refcount_t count;
 };
 
+#define to_ns_common(__ns)                              \
+	_Generic((__ns),                                \
+		struct cgroup_namespace *: &(__ns)->ns, \
+		struct ipc_namespace *:    &(__ns)->ns, \
+		struct mnt_namespace *:    &(__ns)->ns, \
+		struct net *:              &(__ns)->ns, \
+		struct pid_namespace *:    &(__ns)->ns, \
+		struct time_namespace *:   &(__ns)->ns, \
+		struct user_namespace *:   &(__ns)->ns, \
+		struct uts_namespace *:    &(__ns)->ns)
+
 #endif
include/linux/nsproxy.h +0 -11
index dab6a1734a22..e6bec522b139 100644
@@ -42,17 +42,6 @@ struct nsproxy {
 };
 extern struct nsproxy init_nsproxy;
 
-#define to_ns_common(__ns)                              \
-	_Generic((__ns),                                \
-		struct cgroup_namespace *: &(__ns->ns), \
-		struct ipc_namespace *:    &(__ns->ns), \
-		struct net *:              &(__ns->ns), \
-		struct pid_namespace *:    &(__ns->ns), \
-		struct mnt_namespace *:    &(__ns->ns), \
-		struct time_namespace *:   &(__ns->ns), \
-		struct user_namespace *:   &(__ns->ns), \
-		struct uts_namespace *:    &(__ns->ns))
-
 /*
  * A structure to encompass all bits needed to install
  * a partial or complete new set of namespaces.
Signature · 1 line
2.47.3
author maintainer
#10 lore raw

PATCH v2 6/33 nsfs: add nsfs.h header

In reply to Christian Brauner

And move the stuff out from proc_ns.h where it really doesn't belong.

---
 include/linux/nsfs.h    | 26 ++++++++++++++++++++++++++
 include/linux/proc_ns.h | 13 +------------
 2 files changed, 27 insertions(+), 12 deletions(-)
include/linux/nsfs.h new file +26 -0
new file mode 100644
index 000000000000..fb84aa538091
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2025 Christian Brauner <[email protected]> */
+
+#ifndef _LINUX_NSFS_H
+#define _LINUX_NSFS_H
+
+#include <linux/ns_common.h>
+
+struct path;
+struct task_struct;
+struct proc_ns_operations;
+
+int ns_get_path(struct path *path, struct task_struct *task,
+		const struct proc_ns_operations *ns_ops);
+typedef struct ns_common *ns_get_path_helper_t(void *);
+int ns_get_path_cb(struct path *path, ns_get_path_helper_t ns_get_cb,
+		   void *private_data);
+
+bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino);
+
+int ns_get_name(char *buf, size_t size, struct task_struct *task,
+			const struct proc_ns_operations *ns_ops);
+void nsfs_init(void);
+
+#endif /* _LINUX_NSFS_H */
+
include/linux/proc_ns.h +1 -12
index 4b20375f3783..5e1a4b378b79 100644
@@ -5,7 +5,7 @@
 #ifndef _LINUX_PROC_NS_H
 #define _LINUX_PROC_NS_H
 
-#include <linux/ns_common.h>
+#include <linux/nsfs.h>
 #include <uapi/linux/nsfs.h>
 
 struct pid_namespace;
@@ -75,16 +75,5 @@ static inline int ns_alloc_inum(struct ns_common *ns)
 #define ns_free_inum(ns) proc_free_inum((ns)->inum)
 
 #define get_proc_ns(inode) ((struct ns_common *)(inode)->i_private)
-extern int ns_get_path(struct path *path, struct task_struct *task,
-			const struct proc_ns_operations *ns_ops);
-typedef struct ns_common *ns_get_path_helper_t(void *);
-extern int ns_get_path_cb(struct path *path, ns_get_path_helper_t ns_get_cb,
-			    void *private_data);
-
-extern bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino);
-
-extern int ns_get_name(char *buf, size_t size, struct task_struct *task,
-			const struct proc_ns_operations *ns_ops);
-extern void nsfs_init(void);
 
 #endif /* _LINUX_PROC_NS_H */
Signature · 1 line
2.47.3
author maintainer
#11 lore raw

PATCH v2 7/33 ns: uniformly initialize ns_common

In reply to Christian Brauner

No point in cargo-culting the same code across all the different types. Use one common initializer.

---
 include/linux/proc_ns.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/linux/proc_ns.h +16 -0
index 5e1a4b378b79..dbb119bda097 100644
@@ -72,6 +72,22 @@ static inline int ns_alloc_inum(struct ns_common *ns)
 	return proc_alloc_inum(&ns->inum);
 }
 
+static inline int ns_common_init(struct ns_common *ns,
+				 const struct proc_ns_operations *ops,
+				 bool alloc_inum)
+{
+	if (alloc_inum) {
+		int ret;
+		ret = proc_alloc_inum(&ns->inum);
+		if (ret)
+			return ret;
+	}
+	refcount_set(&ns->count, 1);
+	ns->stashed = NULL;
+	ns->ops = ops;
+	return 0;
+}
+
 #define ns_free_inum(ns) proc_free_inum((ns)->inum)
 
 #define get_proc_ns(inode) ((struct ns_common *)(inode)->i_private)
Signature · 1 line
2.47.3
author maintainer
#12 lore raw

PATCH v2 8/33 cgroup: use ns_common_init()

In reply to Christian Brauner

Don't cargo-cult the same thing over and over.

---
 kernel/cgroup/namespace.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
kernel/cgroup/namespace.c +4 -8
index 144a464e45c6..0391b6ab0bf1 100644
@@ -21,20 +21,16 @@ static void dec_cgroup_namespaces(struct ucounts *ucounts)
 
 static struct cgroup_namespace *alloc_cgroup_ns(void)
 {
-	struct cgroup_namespace *new_ns;
+	struct cgroup_namespace *new_ns __free(kfree) = NULL;
 	int ret;
 
 	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns)
 		return ERR_PTR(-ENOMEM);
-	ret = ns_alloc_inum(&new_ns->ns);
-	if (ret) {
-		kfree(new_ns);
+	ret = ns_common_init(&new_ns->ns, &cgroupns_operations, true);
+	if (ret)
 		return ERR_PTR(ret);
-	}
-	refcount_set(&new_ns->ns.count, 1);
-	new_ns->ns.ops = &cgroupns_operations;
-	return new_ns;
+	return no_free_ptr(new_ns);
 }
 
 void free_cgroup_ns(struct cgroup_namespace *ns)
Signature · 1 line
2.47.3
author maintainer
#14 lore raw

PATCH v2 9/33 ipc: use ns_common_init()

In reply to Christian Brauner

Don't cargo-cult the same thing over and over.

---
 ipc/namespace.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
ipc/namespace.c +1 -3
index 4df91ceeeafe..d4188a88ee57 100644
@@ -61,12 +61,10 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
 	if (ns == NULL)
 		goto fail_dec;
 
-	err = ns_alloc_inum(&ns->ns);
+	err = ns_common_init(&ns->ns, &ipcns_operations, true);
 	if (err)
 		goto fail_free;
-	ns->ns.ops = &ipcns_operations;
 
-	refcount_set(&ns->ns.count, 1);
 	ns->user_ns = get_user_ns(user_ns);
 	ns->ucounts = ucounts;
 
Signature · 1 line
2.47.3
author maintainer
#15 lore raw

PATCH v2 10/33 mnt: use ns_common_init()

In reply to Christian Brauner

Don't cargo-cult the same thing over and over.

---
 fs/namespace.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
fs/namespace.c +6 -9
index ddfd4457d338..14c5cdbdd6e1 100644
@@ -4177,18 +4177,15 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
 		dec_mnt_namespaces(ucounts);
 		return ERR_PTR(-ENOMEM);
 	}
-	if (!anon) {
-		ret = ns_alloc_inum(&new_ns->ns);
-		if (ret) {
-			kfree(new_ns);
-			dec_mnt_namespaces(ucounts);
-			return ERR_PTR(ret);
-		}
+
+	ret = ns_common_init(&new_ns->ns, &mntns_operations, !anon);
+	if (ret) {
+		kfree(new_ns);
+		dec_mnt_namespaces(ucounts);
+		return ERR_PTR(ret);
 	}
-	new_ns->ns.ops = &mntns_operations;
 	if (!anon)
 		new_ns->seq = atomic64_inc_return(&mnt_ns_seq);
-	refcount_set(&new_ns->ns.count, 1);
 	refcount_set(&new_ns->passive, 1);
 	new_ns->mounts = RB_ROOT;
 	INIT_LIST_HEAD(&new_ns->mnt_ns_list);
Signature · 1 line
2.47.3
author maintainer
#16 lore raw

PATCH v2 11/33 net: use ns_common_init()

In reply to Christian Brauner

Don't cargo-cult the same thing over and over.

---
 net/core/net_namespace.c | 46 +++++++++++++++++++++++++++++++++-------------
 1 file changed, 33 insertions(+), 13 deletions(-)
net/core/net_namespace.c +33 -13
index 1b6f3826dd0e..5fb7bd8ac45a 100644
@@ -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))
Signature · 1 line
2.47.3
reviewer
#17 lore raw

Re: [PATCH v2 11/33] net: use ns_common_init()

Quoted 3 lines · Christian Brauner

Don't cargo-cult the same thing over and over.

...

net/core/net_namespace.c struct net *copy_net_ns(unsigned long flags, · line 577

Quoted 8 lines
net/core/net_namespace.c +3 -1
@@ -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;

Umm, this seems to be leaking 'net' on error exit.

net/core/net_namespace.c struct net *copy_net_ns(unsigned long flags, · line 580

Quoted 3 lines
net/core/net_namespace.c +0 -0
 	net->ucounts = ucounts;
 	get_user_ns(user_ns);
 

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#18 lore raw

Re: [PATCH v2 11/33] net: use ns_common_init()

Quoted 11 lines · Jan Kara
Quoted 3 lines · Christian Brauner

Don't cargo-cult the same thing over and over.

...

Quoted 4 lines
(quoted diff) +0 -0
@@ -559,7 +572,9 @@ struct net *copy_net_ns(unsigned long flags,
 		goto dec_ucounts;
 	}
 
Quoted 6 lines

Review struct net *copy_net_ns(unsigned long flags, · line 577

Quoted 4 lines
(quoted diff) +3 -1
-	preinit_net(net, user_ns);
+	rv = preinit_net(net, user_ns);
+	if (rv < 0)
+		goto dec_ucounts;

Umm, this seems to be leaking 'net' on error exit.

Sorry about this:

net/core/net_namespace.c +4 -3
index 5fb7bd8ac45a..466de530c495 100644
@@ -572,12 +572,13 @@ struct net *copy_net_ns(unsigned long flags,
                goto dec_ucounts;
        }
 
-       rv = preinit_net(net, user_ns);
-       if (rv < 0)
-               goto dec_ucounts;
        net->ucounts = ucounts;
        get_user_ns(user_ns);
 
+       rv = preinit_net(net, user_ns);
+       if (rv < 0)
+               goto put_userns;
+
        rv = down_read_killable(&pernet_ops_rwsem);
        if (rv < 0)
                goto put_userns;

Thanks for noticing! Fixed in-tree.

reviewer Reviewed-by
#19 lore raw

Re: [PATCH v2 11/33] net: use ns_common_init()

Quoted 12 lines · Christian Brauner
Quoted 11 lines · Jan Kara
Quoted 3 lines · Christian Brauner

Don't cargo-cult the same thing over and over.

...

Quoted 4 lines
net/core/net_namespace.c +0 -0
@@ -559,7 +572,9 @@ struct net *copy_net_ns(unsigned long flags,
 		goto dec_ucounts;
 	}
 
Quoted 8 lines
Quoted 6 lines

net/core/net_namespace.c struct net *copy_net_ns(unsigned long flags,

Quoted 4 lines
net/core/net_namespace.c +3 -1
-	preinit_net(net, user_ns);
+	rv = preinit_net(net, user_ns);
+	if (rv < 0)
+		goto dec_ucounts;

Umm, this seems to be leaking 'net' on error exit.

Sorry about this:

Looks good now. Feel free to add:

Honza

Quoted 24 lines
net/core/net_namespace.c +4 -3
index 5fb7bd8ac45a..466de530c495 100644
@@ -572,12 +572,13 @@ struct net *copy_net_ns(unsigned long flags,
                goto dec_ucounts;
        }
 
-       rv = preinit_net(net, user_ns);
-       if (rv < 0)
-               goto dec_ucounts;
        net->ucounts = ucounts;
        get_user_ns(user_ns);
 
+       rv = preinit_net(net, user_ns);
+       if (rv < 0)
+               goto put_userns;
+
        rv = down_read_killable(&pernet_ops_rwsem);
        if (rv < 0)
                goto put_userns;

Thanks for noticing! Fixed in-tree.

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#20 lore raw

Re: [PATCH v2 11/33] net: use ns_common_init()

In reply to Christian Brauner

net/core/net_namespace.c static __net_init void preinit_net_sysctl(struct net *net) · line 411

Quoted 1 line · Christian Brauner
net/core/net_namespace.c +1 -0
+	ret = ns_common_init(&net->ns, ns_ops, false);
					       ^^^^^

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (ret)
+		return ret;

How would that possibly fail? You are not trying to grab inum here, what's there to fail?

net/core/net_namespace.c struct net *copy_net_ns(unsigned long flags, · line 577

Quoted 8 lines
net/core/net_namespace.c +3 -1
@@ -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;

Ditto.

author maintainer
#21 lore raw

PATCH v2 12/33 pid: use ns_common_init()

In reply to Christian Brauner

Don't cargo-cult the same thing over and over.

---
 kernel/pid_namespace.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/pid_namespace.c +1 -3
index 7098ed44e717..20ce4052d1c5 100644
@@ -102,17 +102,15 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
 	if (ns->pid_cachep == NULL)
 		goto out_free_idr;
 
-	err = ns_alloc_inum(&ns->ns);
+	err = ns_common_init(&ns->ns, &pidns_operations, true);
 	if (err)
 		goto out_free_idr;
-	ns->ns.ops = &pidns_operations;
 
 	ns->pid_max = PID_MAX_LIMIT;
 	err = register_pidns_sysctls(ns);
 	if (err)
 		goto out_free_inum;
 
-	refcount_set(&ns->ns.count, 1);
 	ns->level = level;
 	ns->parent = get_pid_ns(parent_pid_ns);
 	ns->user_ns = get_user_ns(user_ns);
Signature · 1 line
2.47.3
author maintainer
#22 lore raw

PATCH v2 13/33 time: use ns_common_init()

In reply to Christian Brauner

Don't cargo-cult the same thing over and over.

---
 kernel/time/namespace.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/time/namespace.c +2 -5
index 667452768ed3..0be93d8f2896 100644
@@ -88,22 +88,19 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kmalloc(sizeof(*ns), GFP_KERNEL_ACCOUNT);
+	ns = kzalloc(sizeof(*ns), GFP_KERNEL_ACCOUNT);
 	if (!ns)
 		goto fail_dec;
 
-	refcount_set(&ns->ns.count, 1);
-
 	ns->vvar_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	if (!ns->vvar_page)
 		goto fail_free;
 
-	err = ns_alloc_inum(&ns->ns);
+	err = ns_common_init(&ns->ns, &timens_operations, true);
 	if (err)
 		goto fail_free_page;
 
 	ns->ucounts = ucounts;
-	ns->ns.ops = &timens_operations;
 	ns->user_ns = get_user_ns(user_ns);
 	ns->offsets = old_ns->offsets;
 	ns->frozen_offsets = false;
Signature · 1 line
2.47.3
author maintainer
#23 lore raw

PATCH v2 14/33 user: use ns_common_init()

In reply to Christian Brauner

Don't cargo-cult the same thing over and over.

---
 kernel/user_namespace.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
kernel/user_namespace.c +2 -3
index 682f40d5632d..98f4fe84d039 100644
@@ -124,12 +124,11 @@ int create_user_ns(struct cred *new)
 		goto fail_dec;
 
 	ns->parent_could_setfcap = cap_raised(new->cap_effective, CAP_SETFCAP);
-	ret = ns_alloc_inum(&ns->ns);
+
+	ret = ns_common_init(&ns->ns, &userns_operations, true);
 	if (ret)
 		goto fail_free;
-	ns->ns.ops = &userns_operations;
 
-	refcount_set(&ns->ns.count, 1);
 	/* Leave the new->user_ns reference with the new user namespace. */
 	ns->parent = parent_ns;
 	ns->level = parent_ns->level + 1;
Signature · 1 line
2.47.3
author maintainer
#24 lore raw

PATCH v2 15/33 uts: use ns_common_init()

In reply to Christian Brauner

Don't cargo-cult the same thing over and over.

---
 kernel/utsname.c | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)
kernel/utsname.c +2 -14
index b1ac3ca870f2..02037010b378 100644
@@ -27,16 +27,6 @@ static void dec_uts_namespaces(struct ucounts *ucounts)
 	dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
 }
 
-static struct uts_namespace *create_uts_ns(void)
-{
-	struct uts_namespace *uts_ns;
-
-	uts_ns = kmem_cache_alloc(uts_ns_cache, GFP_KERNEL);
-	if (uts_ns)
-		refcount_set(&uts_ns->ns.count, 1);
-	return uts_ns;
-}
-
 /*
  * Clone a new ns copying an original utsname, setting refcount to 1
  * @old_ns: namespace to clone
@@ -55,17 +45,15 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = create_uts_ns();
+	ns = kmem_cache_zalloc(uts_ns_cache, GFP_KERNEL);
 	if (!ns)
 		goto fail_dec;
 
-	err = ns_alloc_inum(&ns->ns);
+	err = ns_common_init(&ns->ns, &utsns_operations, true);
 	if (err)
 		goto fail_free;
 
 	ns->ucounts = ucounts;
-	ns->ns.ops = &utsns_operations;
-
 	down_read(&uts_sem);
 	memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
 	ns->user_ns = get_user_ns(user_ns);
Signature · 1 line
2.47.3
author maintainer
#25 lore raw

PATCH v2 16/33 ns: remove ns_alloc_inum()

In reply to Christian Brauner

It's now unused.

---
 include/linux/proc_ns.h | 6 ------
 1 file changed, 6 deletions(-)
include/linux/proc_ns.h +0 -6
index dbb119bda097..e50d312f9fee 100644
@@ -66,12 +66,6 @@ static inline void proc_free_inum(unsigned int inum) {}
 
 #endif /* CONFIG_PROC_FS */
 
-static inline int ns_alloc_inum(struct ns_common *ns)
-{
-	WRITE_ONCE(ns->stashed, NULL);
-	return proc_alloc_inum(&ns->inum);
-}
-
 static inline int ns_common_init(struct ns_common *ns,
 				 const struct proc_ns_operations *ops,
 				 bool alloc_inum)
Signature · 1 line
2.47.3
author maintainer
#26 lore raw

PATCH v2 17/33 nstree: make iterator generic

In reply to Christian Brauner

Move the namespace iteration infrastructure originally introduced for mount namespaces into a generic library usable by all namespace types.

---
 include/linux/ns_common.h |   9 ++
 include/linux/nstree.h    |  89 ++++++++++++++++++
 include/linux/proc_ns.h   |   3 +
 kernel/Makefile           |   2 +-
 kernel/nstree.c           | 233 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 335 insertions(+), 1 deletion(-)
include/linux/ns_common.h +9 -0
index bc2e0758e1c9..7224072cccc5 100644
@@ -3,6 +3,7 @@
 #define _LINUX_NS_COMMON_H
 
 #include <linux/refcount.h>
+#include <linux/rbtree.h>
 
 struct proc_ns_operations;
 
@@ -20,6 +21,14 @@ struct ns_common {
 	const struct proc_ns_operations *ops;
 	unsigned int inum;
 	refcount_t count;
+	union {
+		struct {
+			u64 ns_id;
+			struct rb_node ns_tree_node;
+			struct list_head ns_list_node;
+		};
+		struct rcu_head ns_rcu;
+	};
 };
 
 #define to_ns_common(__ns)                              \
include/linux/nstree.h new file +89 -0

89 lines not on this page · raw message

include/linux/proc_ns.h +3 -0
index e50d312f9fee..7f89f0829e60 100644
@@ -79,6 +79,9 @@ static inline int ns_common_init(struct ns_common *ns,
 	refcount_set(&ns->count, 1);
 	ns->stashed = NULL;
 	ns->ops = ops;
+	ns->ns_id = 0;
+	RB_CLEAR_NODE(&ns->ns_tree_node);
+	INIT_LIST_HEAD(&ns->ns_list_node);
 	return 0;
 }
 
kernel/Makefile +1 -1
index c60623448235..b807516a1b43 100644
@@ -8,7 +8,7 @@ obj-y     = fork.o exec_domain.o panic.o \
 	    sysctl.o capability.o ptrace.o user.o \
 	    signal.o sys.o umh.o workqueue.o pid.o task_work.o \
 	    extable.o params.o \
-	    kthread.o sys_ni.o nsproxy.o \
+	    kthread.o sys_ni.o nsproxy.o nstree.o \
 	    notifier.o ksysfs.o cred.o reboot.o \
 	    async.o range.o smpboot.o ucount.o regset.o ksyms_common.o
 
kernel/nstree.c new file +233 -0

233 lines not on this page · raw message

Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 17/33
#27 lore raw

Re: [PATCH v2 17/33] nstree: make iterator generic

Quoted 4 lines · Christian Brauner

Move the namespace iteration infrastructure originally introduced for mount namespaces into a generic library usable by all namespace types.

Looks good. Feel free to add:

Honza

Quoted 400 lines
---
 include/linux/ns_common.h |   9 ++
 include/linux/nstree.h    |  89 ++++++++++++++++++
 include/linux/proc_ns.h   |   3 +
 kernel/Makefile           |   2 +-
 kernel/nstree.c           | 233 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 335 insertions(+), 1 deletion(-)
include/linux/ns_common.h +9 -0
index bc2e0758e1c9..7224072cccc5 100644
@@ -3,6 +3,7 @@
 #define _LINUX_NS_COMMON_H
 
 #include <linux/refcount.h>
+#include <linux/rbtree.h>
 
 struct proc_ns_operations;
 
@@ -20,6 +21,14 @@ struct ns_common {
 	const struct proc_ns_operations *ops;
 	unsigned int inum;
 	refcount_t count;
+	union {
+		struct {
+			u64 ns_id;
+			struct rb_node ns_tree_node;
+			struct list_head ns_list_node;
+		};
+		struct rcu_head ns_rcu;
+	};
 };
 
 #define to_ns_common(__ns)                              \
include/linux/nstree.h new file +89 -0
new file mode 100644
index 000000000000..e26951a83924
@@ -0,0 +1,89 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_NSTREE_H
+#define _LINUX_NSTREE_H
+
+#include <linux/ns_common.h>
+#include <linux/nsproxy.h>
+#include <linux/rbtree.h>
+#include <linux/seqlock.h>
+#include <linux/rculist.h>
+#include <linux/cookie.h>
+
+/**
+ * struct ns_tree - Namespace tree
+ * @ns_tree: Rbtree of namespaces of a particular type
+ * @ns_list: Sequentially walkable list of all namespaces of this type
+ * @ns_tree_lock: Seqlock to protect the tree and list
+ */
+struct ns_tree {
+	struct rb_root ns_tree;
+	struct list_head ns_list;
+	seqlock_t ns_tree_lock;
+	int type;
+};
+
+extern struct ns_tree cgroup_ns_tree;
+extern struct ns_tree ipc_ns_tree;
+extern struct ns_tree mnt_ns_tree;
+extern struct ns_tree net_ns_tree;
+extern struct ns_tree pid_ns_tree;
+extern struct ns_tree time_ns_tree;
+extern struct ns_tree user_ns_tree;
+extern struct ns_tree uts_ns_tree;
+
+#define to_ns_tree(__ns)					\
+	_Generic((__ns),					\
+		struct cgroup_namespace *: &(cgroup_ns_tree),	\
+		struct ipc_namespace *:    &(ipc_ns_tree),	\
+		struct net *:              &(net_ns_tree),	\
+		struct pid_namespace *:    &(pid_ns_tree),	\
+		struct mnt_namespace *:    &(mnt_ns_tree),	\
+		struct time_namespace *:   &(time_ns_tree),	\
+		struct user_namespace *:   &(user_ns_tree),	\
+		struct uts_namespace *:    &(uts_ns_tree))
+
+u64 ns_tree_gen_id(struct ns_common *ns);
+void __ns_tree_add_raw(struct ns_common *ns, struct ns_tree *ns_tree);
+void __ns_tree_remove(struct ns_common *ns, struct ns_tree *ns_tree);
+struct ns_common *ns_tree_lookup_rcu(u64 ns_id, int ns_type);
+struct ns_common *__ns_tree_adjoined_rcu(struct ns_common *ns,
+					 struct ns_tree *ns_tree,
+					 bool previous);
+
+static inline void __ns_tree_add(struct ns_common *ns, struct ns_tree *ns_tree)
+{
+	ns_tree_gen_id(ns);
+	__ns_tree_add_raw(ns, ns_tree);
+}
+
+/**
+ * ns_tree_add_raw - Add a namespace to a namespace
+ * @ns: Namespace to add
+ *
+ * This function adds a namespace to the appropriate namespace tree
+ * without assigning a id.
+ */
+#define ns_tree_add_raw(__ns) __ns_tree_add_raw(to_ns_common(__ns), to_ns_tree(__ns))
+
+/**
+ * ns_tree_add - Add a namespace to a namespace tree
+ * @ns: Namespace to add
+ *
+ * This function assigns a new id to the namespace and adds it to the
+ * appropriate namespace tree and list.
+ */
+#define ns_tree_add(__ns) __ns_tree_add(to_ns_common(__ns), to_ns_tree(__ns))
+
+/**
+ * ns_tree_remove - Remove a namespace from a namespace tree
+ * @ns: Namespace to remove
+ *
+ * This function removes a namespace from the appropriate namespace
+ * tree and list.
+ */
+#define ns_tree_remove(__ns)  __ns_tree_remove(to_ns_common(__ns), to_ns_tree(__ns))
+
+#define ns_tree_adjoined_rcu(__ns, __previous) \
+	__ns_tree_adjoined_rcu(to_ns_common(__ns), to_ns_tree(__ns), __previous)
+
+#endif /* _LINUX_NSTREE_H */
include/linux/proc_ns.h +3 -0
index e50d312f9fee..7f89f0829e60 100644
@@ -79,6 +79,9 @@ static inline int ns_common_init(struct ns_common *ns,
 	refcount_set(&ns->count, 1);
 	ns->stashed = NULL;
 	ns->ops = ops;
+	ns->ns_id = 0;
+	RB_CLEAR_NODE(&ns->ns_tree_node);
+	INIT_LIST_HEAD(&ns->ns_list_node);
 	return 0;
 }
 
kernel/Makefile +1 -1
index c60623448235..b807516a1b43 100644
@@ -8,7 +8,7 @@ obj-y     = fork.o exec_domain.o panic.o \
 	    sysctl.o capability.o ptrace.o user.o \
 	    signal.o sys.o umh.o workqueue.o pid.o task_work.o \
 	    extable.o params.o \
-	    kthread.o sys_ni.o nsproxy.o \
+	    kthread.o sys_ni.o nsproxy.o nstree.o \
 	    notifier.o ksysfs.o cred.o reboot.o \
 	    async.o range.o smpboot.o ucount.o regset.o ksyms_common.o
 
kernel/nstree.c new file +233 -0
new file mode 100644
index 000000000000..bbe8bedc924c
@@ -0,0 +1,233 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/nstree.h>
+#include <linux/proc_ns.h>
+#include <linux/vfsdebug.h>
+
+struct ns_tree mnt_ns_tree = {
+	.ns_tree = RB_ROOT,
+	.ns_list = LIST_HEAD_INIT(mnt_ns_tree.ns_list),
+	.ns_tree_lock = __SEQLOCK_UNLOCKED(mnt_ns_tree.ns_tree_lock),
+	.type = CLONE_NEWNS,
+};
+
+struct ns_tree net_ns_tree = {
+	.ns_tree = RB_ROOT,
+	.ns_list = LIST_HEAD_INIT(net_ns_tree.ns_list),
+	.ns_tree_lock = __SEQLOCK_UNLOCKED(net_ns_tree.ns_tree_lock),
+	.type = CLONE_NEWNET,
+};
+EXPORT_SYMBOL_GPL(net_ns_tree);
+
+struct ns_tree uts_ns_tree = {
+	.ns_tree = RB_ROOT,
+	.ns_list = LIST_HEAD_INIT(uts_ns_tree.ns_list),
+	.ns_tree_lock = __SEQLOCK_UNLOCKED(uts_ns_tree.ns_tree_lock),
+	.type = CLONE_NEWUTS,
+};
+
+struct ns_tree user_ns_tree = {
+	.ns_tree = RB_ROOT,
+	.ns_list = LIST_HEAD_INIT(user_ns_tree.ns_list),
+	.ns_tree_lock = __SEQLOCK_UNLOCKED(user_ns_tree.ns_tree_lock),
+	.type = CLONE_NEWUSER,
+};
+
+struct ns_tree ipc_ns_tree = {
+	.ns_tree = RB_ROOT,
+	.ns_list = LIST_HEAD_INIT(ipc_ns_tree.ns_list),
+	.ns_tree_lock = __SEQLOCK_UNLOCKED(ipc_ns_tree.ns_tree_lock),
+	.type = CLONE_NEWIPC,
+};
+
+struct ns_tree pid_ns_tree = {
+	.ns_tree = RB_ROOT,
+	.ns_list = LIST_HEAD_INIT(pid_ns_tree.ns_list),
+	.ns_tree_lock = __SEQLOCK_UNLOCKED(pid_ns_tree.ns_tree_lock),
+	.type = CLONE_NEWPID,
+};
+
+struct ns_tree cgroup_ns_tree = {
+	.ns_tree = RB_ROOT,
+	.ns_list = LIST_HEAD_INIT(cgroup_ns_tree.ns_list),
+	.ns_tree_lock = __SEQLOCK_UNLOCKED(cgroup_ns_tree.ns_tree_lock),
+	.type = CLONE_NEWCGROUP,
+};
+
+struct ns_tree time_ns_tree = {
+	.ns_tree = RB_ROOT,
+	.ns_list = LIST_HEAD_INIT(time_ns_tree.ns_list),
+	.ns_tree_lock = __SEQLOCK_UNLOCKED(time_ns_tree.ns_tree_lock),
+	.type = CLONE_NEWTIME,
+};
+
+DEFINE_COOKIE(namespace_cookie);
+
+static inline struct ns_common *node_to_ns(const struct rb_node *node)
+{
+	if (!node)
+		return NULL;
+	return rb_entry(node, struct ns_common, ns_tree_node);
+}
+
+static inline int ns_cmp(struct rb_node *a, const struct rb_node *b)
+{
+	struct ns_common *ns_a = node_to_ns(a);
+	struct ns_common *ns_b = node_to_ns(b);
+	u64 ns_id_a = ns_a->ns_id;
+	u64 ns_id_b = ns_b->ns_id;
+
+	if (ns_id_a < ns_id_b)
+		return -1;
+	if (ns_id_a > ns_id_b)
+		return 1;
+	return 0;
+}
+
+void __ns_tree_add_raw(struct ns_common *ns, struct ns_tree *ns_tree)
+{
+	struct rb_node *node, *prev;
+
+	VFS_WARN_ON_ONCE(!ns->ns_id);
+
+	write_seqlock(&ns_tree->ns_tree_lock);
+
+	VFS_WARN_ON_ONCE(ns->ops->type != ns_tree->type);
+
+	node = rb_find_add_rcu(&ns->ns_tree_node, &ns_tree->ns_tree, ns_cmp);
+	/*
+	 * If there's no previous entry simply add it after the
+	 * head and if there is add it after the previous entry.
+	 */
+	prev = rb_prev(&ns->ns_tree_node);
+	if (!prev)
+		list_add_rcu(&ns->ns_list_node, &ns_tree->ns_list);
+	else
+		list_add_rcu(&ns->ns_list_node, &node_to_ns(prev)->ns_list_node);
+
+	write_sequnlock(&ns_tree->ns_tree_lock);
+
+	VFS_WARN_ON_ONCE(node);
+}
+
+void __ns_tree_remove(struct ns_common *ns, struct ns_tree *ns_tree)
+{
+	VFS_WARN_ON_ONCE(RB_EMPTY_NODE(&ns->ns_tree_node));
+	VFS_WARN_ON_ONCE(list_empty(&ns->ns_list_node));
+	VFS_WARN_ON_ONCE(ns->ops->type != ns_tree->type);
+
+	write_seqlock(&ns_tree->ns_tree_lock);
+	rb_erase(&ns->ns_tree_node, &ns_tree->ns_tree);
+	list_bidir_del_rcu(&ns->ns_list_node);
+	RB_CLEAR_NODE(&ns->ns_tree_node);
+	write_sequnlock(&ns_tree->ns_tree_lock);
+}
+EXPORT_SYMBOL_GPL(__ns_tree_remove);
+
+static int ns_find(const void *key, const struct rb_node *node)
+{
+	const u64 ns_id = *(u64 *)key;
+	const struct ns_common *ns = node_to_ns(node);
+
+	if (ns_id < ns->ns_id)
+		return -1;
+	if (ns_id > ns->ns_id)
+		return 1;
+	return 0;
+}
+
+
+static struct ns_tree *ns_tree_from_type(int ns_type)
+{
+	switch (ns_type) {
+	case CLONE_NEWCGROUP:
+		return &cgroup_ns_tree;
+	case CLONE_NEWIPC:
+		return &ipc_ns_tree;
+	case CLONE_NEWNS:
+		return &mnt_ns_tree;
+	case CLONE_NEWNET:
+		return &net_ns_tree;
+	case CLONE_NEWPID:
+		return &pid_ns_tree;
+	case CLONE_NEWUSER:
+		return &user_ns_tree;
+	case CLONE_NEWUTS:
+		return &uts_ns_tree;
+	case CLONE_NEWTIME:
+		return &time_ns_tree;
+	}
+
+	return NULL;
+}
+
+struct ns_common *ns_tree_lookup_rcu(u64 ns_id, int ns_type)
+{
+	struct ns_tree *ns_tree;
+	struct rb_node *node;
+	unsigned int seq;
+
+	RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "suspicious ns_tree_lookup_rcu() usage");
+
+	ns_tree = ns_tree_from_type(ns_type);
+	if (!ns_tree)
+		return NULL;
+
+	do {
+		seq = read_seqbegin(&ns_tree->ns_tree_lock);
+		node = rb_find_rcu(&ns_id, &ns_tree->ns_tree, ns_find);
+		if (node)
+			break;
+	} while (read_seqretry(&ns_tree->ns_tree_lock, seq));
+
+	if (!node)
+		return NULL;
+
+	VFS_WARN_ON_ONCE(node_to_ns(node)->ops->type != ns_type);
+
+	return node_to_ns(node);
+}
+
+/**
+ * ns_tree_adjoined_rcu - find the next/previous namespace in the same
+ * tree
+ * @ns: namespace to start from
+ * @previous: if true find the previous namespace, otherwise the next
+ *
+ * Find the next or previous namespace in the same tree as @ns. If
+ * there is no next/previous namespace, -ENOENT is returned.
+ */
+struct ns_common *__ns_tree_adjoined_rcu(struct ns_common *ns,
+					 struct ns_tree *ns_tree, bool previous)
+{
+	struct list_head *list;
+
+	RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "suspicious ns_tree_adjoined_rcu() usage");
+
+	if (previous)
+		list = rcu_dereference(list_bidir_prev_rcu(&ns->ns_list_node));
+	else
+		list = rcu_dereference(list_next_rcu(&ns->ns_list_node));
+	if (list_is_head(list, &ns_tree->ns_list))
+		return ERR_PTR(-ENOENT);
+
+	VFS_WARN_ON_ONCE(list_entry_rcu(list, struct ns_common, ns_list_node)->ops->type != ns_tree->type);
+
+	return list_entry_rcu(list, struct ns_common, ns_list_node);
+}
+
+/**
+ * ns_tree_gen_id - generate a new namespace id
+ * @ns: namespace to generate id for
+ *
+ * Generates a new namespace id and assigns it to the namespace. All
+ * namespaces types share the same id space and thus can be compared
+ * directly. IOW, when two ids of two namespace are equal, they are
+ * identical.
+ */
+u64 ns_tree_gen_id(struct ns_common *ns)
+{
+	guard(preempt)();
+	ns->ns_id = gen_cookie_next(&namespace_cookie);
+	return ns->ns_id;
+}
Signature · 2 lines
-- 
2.47.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#28 lore raw

PATCH v2 18/33 mnt: support ns lookup

In reply to Christian Brauner

Move the mount namespace to the generic ns lookup infrastructure. This allows us to drop a bunch of members from struct mnt_namespace.
t

---
 fs/mount.h     |  10 +---
 fs/namespace.c | 141 +++++++++++++--------------------------------------------
 fs/nsfs.c      |   4 +-
 3 files changed, 35 insertions(+), 120 deletions(-)
fs/mount.h +2 -8
index 97737051a8b9..76bf863c9ae2 100644
@@ -17,11 +17,7 @@ struct mnt_namespace {
 	};
 	struct user_namespace	*user_ns;
 	struct ucounts		*ucounts;
-	u64			seq;	/* Sequence number to prevent loops */
-	union {
-		wait_queue_head_t	poll;
-		struct rcu_head		mnt_ns_rcu;
-	};
+	wait_queue_head_t	poll;
 	u64			seq_origin; /* Sequence number of origin mount namespace */
 	u64 event;
 #ifdef CONFIG_FSNOTIFY
@@ -30,8 +26,6 @@ struct mnt_namespace {
 #endif
 	unsigned int		nr_mounts; /* # of mounts in the namespace */
 	unsigned int		pending_mounts;
-	struct rb_node		mnt_ns_tree_node; /* node in the mnt_ns_tree */
-	struct list_head	mnt_ns_list; /* entry in the sequential list of mounts namespace */
 	refcount_t		passive; /* number references not pinning @mounts */
 } __randomize_layout;
 
@@ -173,7 +167,7 @@ static inline bool is_local_mountpoint(const struct dentry *dentry)
 
 static inline bool is_anon_ns(struct mnt_namespace *ns)
 {
-	return ns->seq == 0;
+	return ns->ns.ns_id == 0;
 }
 
 static inline bool anon_ns_root(const struct mount *m)
fs/namespace.c +31 -110

257 lines not on this page · raw message

fs/nsfs.c +2 -2
index d016d36272d4..80e631aeb3ce 100644
@@ -139,7 +139,7 @@ static int copy_ns_info_to_user(const struct mnt_namespace *mnt_ns,
 	 * the size value will be set to the size the kernel knows about.
 	 */
 	kinfo->size		= min(usize, sizeof(*kinfo));
-	kinfo->mnt_ns_id	= mnt_ns->seq;
+	kinfo->mnt_ns_id	= mnt_ns->ns.ns_id;
 	kinfo->nr_mounts	= READ_ONCE(mnt_ns->nr_mounts);
 	/* Subtract the root mount of the mount namespace. */
 	if (kinfo->nr_mounts)
@@ -221,7 +221,7 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 
 		mnt_ns = container_of(ns, struct mnt_namespace, ns);
 		idp = (__u64 __user *)arg;
-		id = mnt_ns->seq;
+		id = mnt_ns->ns.ns_id;
 		return put_user(id, idp);
 	}
 	case NS_GET_PID_FROM_PIDNS:
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 18/33
#29 lore raw

Re: [PATCH v2 18/33] mnt: support ns lookup

Quoted 4 lines · Christian Brauner

Move the mount namespace to the generic ns lookup infrastructure. This allows us to drop a bunch of members from struct mnt_namespace.
t

Just one nit below. Feel free to add:

fs/namespace.c static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a · line 4112

Quoted 8 lines
fs/namespace.c +3 -1
@@ -4185,11 +4107,11 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
 		return ERR_PTR(ret);
 	}
 	if (!anon)
-		new_ns->seq = atomic64_inc_return(&mnt_ns_seq);
+		ns_tree_gen_id(&new_ns->ns);
+	RB_CLEAR_NODE(&new_ns->ns.ns_tree_node);
+	INIT_LIST_HEAD(&new_ns->ns.ns_list_node);

Why do you initialize ns_tree_node and ns_list_node here when ns_init_common() already initialized them?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#30 lore raw

Re: [PATCH v2 18/33] mnt: support ns lookup

Quoted 10 lines · Jan Kara
Quoted 4 lines · Christian Brauner

Move the mount namespace to the generic ns lookup infrastructure. This allows us to drop a bunch of members from struct mnt_namespace.
t

Just one nit below. Feel free to add:

Quoted 11 lines

fs/namespace.c static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a · line 4112

Quoted 8 lines
fs/namespace.c +3 -1
@@ -4185,11 +4107,11 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
 		return ERR_PTR(ret);
 	}
 	if (!anon)
-		new_ns->seq = atomic64_inc_return(&mnt_ns_seq);
+		ns_tree_gen_id(&new_ns->ns);
+	RB_CLEAR_NODE(&new_ns->ns.ns_tree_node);
+	INIT_LIST_HEAD(&new_ns->ns.ns_list_node);

Why do you initialize ns_tree_node and ns_list_node here when ns_init_common() already initialized them?

I forgot to remove that. I should note I did all this while severly sick. :D

#31 lore raw

Re: [PATCH v2 18/33] mnt: support ns lookup

In reply to Christian Brauner

Hi,

From: Christian Brauner <[email protected]>
Date: Fri, 12 Sep 2025 13:52:41 +0200
[...]

fs/namespace.c struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, · line 4200

Quoted 6 lines
fs/namespace.c +1 -1
@@ -4275,7 +4197,7 @@ struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
 	if (pwdmnt)
 		mntput(pwdmnt);
 
-	mnt_ns_tree_add(new_ns);
+	ns_tree_add_raw(new_ns);

When copy_tree() fails, new_ns's rb tree could be empty,

	guard(namespace_excl)();
...
	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
	if (IS_ERR(new)) {
		emptied_ns = new_ns;
		return ERR_CAST(new);
	}

which seems to trigger this warning in __ns_tree_remove().

	VFS_WARN_ON_ONCE(RB_EMPTY_NODE(&ns->ns_tree_node));

stack trace captured by syzbot:

WARNING: kernel/nstree.c:115 at __ns_tree_remove+0x28d/0x330 kernel/nstree.c:115, CPU#0: syz.5.2042/14092
Modules linked in:
CPU: 0 UID: 0 PID: 14092 Comm: syz.5.2042 Not tainted syzkaller #0 PREEMPT(full)
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 08/18/2025
RIP: 0010:__ns_tree_remove+0x28d/0x330 kernel/nstree.c:115
Code: 0f 85 a4 00 00 00 48 8b 04 24 ff 00 48 8b 7c 24 18 48 83 c4 30 5b 41 5c 41 5d 41 5e 41 5f 5d e9 e9 b4 f8 09 e8 94 f1 31 00 90 <0f> 0b 90 e9 cc fd ff ff e8 86 f1 31 00 90 0f 0b 90 e9 ee fd ff ff
RSP: 0018:ffffc9001040faa8 EFLAGS: 00010287
RAX: ffffffff818de18c RBX: ffff8880565d4620 RCX: 0000000000080000
RDX: ffffc9001ca51000 RSI: 00000000000003f8 RDI: 00000000000003f9
RBP: dffffc0000000000 R08: ffffffff8e1eff53 R09: 1ffffffff1c3dfea
R10: dffffc0000000000 R11: fffffbfff1c3dfeb R12: ffff8880565d4620
R13: 1ffff1100acba8c3 R14: ffff8880565d4600 R15: ffffffff8e1ea7c0
FS:  00007fba829d56c0(0000) GS:ffff8881259e0000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 000000110c3b8de5 CR3: 000000004bb54000 CR4: 00000000003526f0
Call Trace:
 <TASK>
 mnt_ns_tree_remove fs/namespace.c:165 [inline]
 free_mnt_ns+0xd1/0x110 fs/namespace.c:4074
 namespace_unlock+0x529/0x760 fs/namespace.c:1710
 class_namespace_excl_destructor fs/namespace.c:96 [inline]
 copy_mnt_ns+0x6e0/0x870 fs/namespace.c:4180
 create_new_namespaces+0xd1/0x720 kernel/nsproxy.c:78
 unshare_nsproxy_namespaces+0x11c/0x170 kernel/nsproxy.c:218
 ksys_unshare+0x4c8/0x8c0 kernel/fork.c:3144
 __do_sys_unshare kernel/fork.c:3215 [inline]
 __se_sys_unshare kernel/fork.c:3213 [inline]
 __x64_sys_unshare+0x38/0x50 kernel/fork.c:3213
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xfa/0xfa0 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fba8478eba9
Code: ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 a8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007fba829d5038 EFLAGS: 00000246 ORIG_RAX: 0000000000000110
RAX: ffffffffffffffda RBX: 00007fba849d6180 RCX: 00007fba8478eba9
RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000028060400
RBP: 00007fba84811e19 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007fba849d6218 R14: 00007fba849d6180 R15: 00007ffe311606c8
 </TASK>
maintainer
#32 lore raw

Re: [PATCH v2 18/33] mnt: support ns lookup

On Tue, Sep 16, 2025 at 03:24:07AM +0000, Kuniyuki Iwashima wrote:

See upthread; basically, that

	if (!is_anon_ns(ns))
		ns_tree_remove(ns);

should've been

        if (!RB_EMPTY_NODE(&to_ns_common(ns)->ns_tree_node))
		ns_tree_remove(ns);
maintainer
#33 lore raw

Re: [PATCH v2 18/33] mnt: support ns lookup

In reply to Christian Brauner
Quoted 12 lines · Christian Brauner

Move the mount namespace to the generic ns lookup infrastructure. This allows us to drop a bunch of members from struct mnt_namespace.

static void mnt_ns_tree_remove(struct mnt_namespace *ns) {

fs/namespace.c +0 -6
 	/* remove from global mount namespace list */
-	if (!is_anon_ns(ns)) {
-		mnt_ns_tree_write_lock();
-		rb_erase(&ns->mnt_ns_tree_node, &mnt_ns_tree);
-		list_bidir_del_rcu(&ns->mnt_ns_list);
-		mnt_ns_tree_write_unlock();
-	}

fs/namespace.c DEFINE_FREE(mnt_ns_release, struct mnt_namespace *, if (_T) mnt_ns_release(_T)) · line 148

Quoted 10 lines
fs/namespace.c +2 -8
-
-	call_rcu(&ns->mnt_ns_rcu, mnt_ns_release_rcu);
-}
-
-static int mnt_ns_find(const void *key, const struct rb_node *node)
-{
-	const u64 mnt_ns_id = *(u64 *)key;
-	const struct mnt_namespace *ns = node_to_mnt_ns(node);
+	if (!is_anon_ns(ns))
+		ns_tree_remove(ns);

Conflicts with "mnt_ns_tree_remove(): DTRT if mnt_ns had never been added to mnt_ns_list".

FWIW, the right thing to do here is

	if (!RB_EMPTY_NODE(to_ns_common(ns)->ns_tree_node))
		ns_tree_remove(ns);
maintainer
#34 lore raw

Re: [PATCH v2 18/33] mnt: support ns lookup

Quoted 1 line · Al Viro
	if (!RB_EMPTY_NODE(to_ns_common(ns)->ns_tree_node))

if (!RB_EMPTY_NODE(&to_ns_common(ns)->ns_tree_node))

obviously...

maintainer
#35 lore raw

Re: [PATCH v2 18/33] mnt: support ns lookup

Quoted 6 lines · Al Viro
Quoted 1 line · Al Viro
	if (!RB_EMPTY_NODE(to_ns_common(ns)->ns_tree_node))

if (!RB_EMPTY_NODE(&to_ns_common(ns)->ns_tree_node))

obviously...

FWIW, how about the following - I put the commit below into never-rebased branch, pull it into #work.mount and you do the same to your branch just prior to 18/33? The difference from one in #work.mount is that this variant checks RB_EMPTY_NODE(&ns->mnt_ns_tree_node) instead of list_empty(&ns->mnt_ns_list). The reasons why it's safe lockless are pretty much the same...

Objections? Does vfs/vfs.git #no-rebases-mnt_ns_tree_remove look sane for you?

mnt_ns_tree_remove(): DTRT if mnt_ns had never been added to mnt_ns_list

Actual removal is done under the lock, but for checking if need to bother the lockless RB_EMPTY_NODE() is safe - either that namespace had never been added to mnt_ns_tree, in which case the the node will stay empty, or whoever had allocated it has called mnt_ns_tree_add() and it has already run to completion. After that point RB_EMPTY_NODE() will become false and will remain false, no matter what we do with other nodes in the tree.

---

fs/namespace.c +1 -1
index ae6d1312b184..39afeb521a80 100644
@@ -187,7 +187,7 @@ static void mnt_ns_release_rcu(struct rcu_head *rcu)
 static void mnt_ns_tree_remove(struct mnt_namespace *ns)
 {
 	/* remove from global mount namespace list */
-	if (!is_anon_ns(ns)) {
+	if (!RB_EMPTY_NODE(&ns->mnt_ns_tree_node)) {
 		mnt_ns_tree_write_lock();
 		rb_erase(&ns->mnt_ns_tree_node, &mnt_ns_tree);
 		list_bidir_del_rcu(&ns->mnt_ns_list);
author maintainer
#36 lore raw

Re: [PATCH v2 18/33] mnt: support ns lookup

Quoted 15 lines · Al Viro
Quoted 6 lines · Al Viro
Quoted 1 line · Al Viro
	if (!RB_EMPTY_NODE(to_ns_common(ns)->ns_tree_node))

if (!RB_EMPTY_NODE(&to_ns_common(ns)->ns_tree_node))

obviously...

FWIW, how about the following - I put the commit below into never-rebased branch, pull it into #work.mount and you do the same to your branch just prior to 18/33? The difference from one in #work.mount is that this variant checks RB_EMPTY_NODE(&ns->mnt_ns_tree_node) instead of list_empty(&ns->mnt_ns_list). The reasons why it's safe lockless are pretty much the same...

Quoted 2 lines

Objections? Does vfs/vfs.git #no-rebases-mnt_ns_tree_remove look sane for you?

Perfect, thank you!

Quoted 25 lines

mnt_ns_tree_remove(): DTRT if mnt_ns had never been added to mnt_ns_list

Actual removal is done under the lock, but for checking if need to bother the lockless RB_EMPTY_NODE() is safe - either that namespace had never been added to mnt_ns_tree, in which case the the node will stay empty, or whoever had allocated it has called mnt_ns_tree_add() and it has already run to completion. After that point RB_EMPTY_NODE() will become false and will remain false, no matter what we do with other nodes in the tree.

---

fs/namespace.c +1 -1
index ae6d1312b184..39afeb521a80 100644
@@ -187,7 +187,7 @@ static void mnt_ns_release_rcu(struct rcu_head *rcu)
 static void mnt_ns_tree_remove(struct mnt_namespace *ns)
 {
 	/* remove from global mount namespace list */
-	if (!is_anon_ns(ns)) {
+	if (!RB_EMPTY_NODE(&ns->mnt_ns_tree_node)) {
 		mnt_ns_tree_write_lock();
 		rb_erase(&ns->mnt_ns_tree_node, &mnt_ns_tree);
 		list_bidir_del_rcu(&ns->mnt_ns_list);
maintainer
#37 lore raw

Re: [PATCH v2 18/33] mnt: support ns lookup

Quoted 1 line · Christian Brauner

Perfect, thank you!

All right, #work.mount contains this now:

commit 1b966c4471e6c3862a14f80aeb316ef636d40f84
Merge: 57a7b5b0b6d9 38f4885088fc
Author: Al Viro <[email protected]>
Date: Wed Sep 17 15:58:06 2025 -0400

Merge branch 'no-rebase-mnt_ns_tree_remove' into work.mount

and vfs/vfs.git #no-rebase-mnt_ns_tree_remove is 38f4885088fc

IOW, merge it into your branch and do your regular changes on top of that - should be no conflicts in mnt_ns_tree_remove() after that (diff between no-rebase-mnt_ns_tree_remove and work.mount has nothing in that function)

author maintainer
#38 lore raw

PATCH v2 19/33 cgroup: support ns lookup

In reply to Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

---
 kernel/cgroup/cgroup.c    | 2 ++
 kernel/cgroup/namespace.c | 7 +++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
kernel/cgroup/cgroup.c +2 -0
index 312c6a8b55bb..092e6bf081ed 100644
@@ -59,6 +59,7 @@
 #include <linux/sched/cputime.h>
 #include <linux/sched/deadline.h>
 #include <linux/psi.h>
+#include <linux/nstree.h>
 #include <net/sock.h>
 
 #define CREATE_TRACE_POINTS
@@ -6312,6 +6313,7 @@ int __init cgroup_init(void)
 	WARN_ON(register_filesystem(&cpuset_fs_type));
 #endif
 
+	ns_tree_add(&init_cgroup_ns);
 	return 0;
 }
 
kernel/cgroup/namespace.c +5 -2
index 0391b6ab0bf1..fc12c416dfeb 100644
@@ -5,7 +5,7 @@
 #include <linux/slab.h>
 #include <linux/nsproxy.h>
 #include <linux/proc_ns.h>
-
+#include <linux/nstree.h>
 
 /* cgroup namespaces */
 
@@ -30,16 +30,19 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
 	ret = ns_common_init(&new_ns->ns, &cgroupns_operations, true);
 	if (ret)
 		return ERR_PTR(ret);
+	ns_tree_add(new_ns);
 	return no_free_ptr(new_ns);
 }
 
 void free_cgroup_ns(struct cgroup_namespace *ns)
 {
+	ns_tree_remove(ns);
 	put_css_set(ns->root_cset);
 	dec_cgroup_namespaces(ns->ucounts);
 	put_user_ns(ns->user_ns);
 	ns_free_inum(&ns->ns);
-	kfree(ns);
+	/* Concurrent nstree traversal depends on a grace period. */
+	kfree_rcu(ns, ns.ns_rcu);
 }
 EXPORT_SYMBOL(free_cgroup_ns);
 
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 19/33
#39 lore raw

Re: [PATCH v2 19/33] cgroup: support ns lookup

Quoted 5 lines · Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

Looks good. Feel free to add:

Honza

Quoted 63 lines
---
 kernel/cgroup/cgroup.c    | 2 ++
 kernel/cgroup/namespace.c | 7 +++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
kernel/cgroup/cgroup.c +2 -0
index 312c6a8b55bb..092e6bf081ed 100644
@@ -59,6 +59,7 @@
 #include <linux/sched/cputime.h>
 #include <linux/sched/deadline.h>
 #include <linux/psi.h>
+#include <linux/nstree.h>
 #include <net/sock.h>
 
 #define CREATE_TRACE_POINTS
@@ -6312,6 +6313,7 @@ int __init cgroup_init(void)
 	WARN_ON(register_filesystem(&cpuset_fs_type));
 #endif
 
+	ns_tree_add(&init_cgroup_ns);
 	return 0;
 }
 
kernel/cgroup/namespace.c +5 -2
index 0391b6ab0bf1..fc12c416dfeb 100644
@@ -5,7 +5,7 @@
 #include <linux/slab.h>
 #include <linux/nsproxy.h>
 #include <linux/proc_ns.h>
-
+#include <linux/nstree.h>
 
 /* cgroup namespaces */
 
@@ -30,16 +30,19 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
 	ret = ns_common_init(&new_ns->ns, &cgroupns_operations, true);
 	if (ret)
 		return ERR_PTR(ret);
+	ns_tree_add(new_ns);
 	return no_free_ptr(new_ns);
 }
 
 void free_cgroup_ns(struct cgroup_namespace *ns)
 {
+	ns_tree_remove(ns);
 	put_css_set(ns->root_cset);
 	dec_cgroup_namespaces(ns->ucounts);
 	put_user_ns(ns->user_ns);
 	ns_free_inum(&ns->ns);
-	kfree(ns);
+	/* Concurrent nstree traversal depends on a grace period. */
+	kfree_rcu(ns, ns.ns_rcu);
 }
 EXPORT_SYMBOL(free_cgroup_ns);
 
Signature · 2 lines
-- 
2.47.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#40 lore raw

PATCH v2 20/33 ipc: support ns lookup

In reply to Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

---
 ipc/msgutil.c   | 1 +
 ipc/namespace.c | 3 +++
 ipc/shm.c       | 2 ++
 3 files changed, 6 insertions(+)
ipc/msgutil.c +1 -0
index c7be0c792647..bbf61275df41 100644
@@ -15,6 +15,7 @@
 #include <linux/proc_ns.h>
 #include <linux/uaccess.h>
 #include <linux/sched.h>
+#include <linux/nstree.h>
 
 #include "util.h"
 
ipc/namespace.c +3 -0
index d4188a88ee57..9f923c1a1eb3 100644
@@ -15,6 +15,7 @@
 #include <linux/mount.h>
 #include <linux/user_namespace.h>
 #include <linux/proc_ns.h>
+#include <linux/nstree.h>
 #include <linux/sched/task.h>
 
 #include "util.h"
@@ -85,6 +86,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
 
 	sem_init_ns(ns);
 	shm_init_ns(ns);
+	ns_tree_add(ns);
 
 	return ns;
 
@@ -201,6 +203,7 @@ void put_ipc_ns(struct ipc_namespace *ns)
 		mq_clear_sbinfo(ns);
 		spin_unlock(&mq_lock);
 
+		ns_tree_remove(ns);
 		if (llist_add(&ns->mnt_llist, &free_ipc_list))
 			schedule_work(&free_ipc_work);
 	}
ipc/shm.c +2 -0
index a9310b6dbbc3..3db36773dd10 100644
@@ -45,6 +45,7 @@
 #include <linux/mount.h>
 #include <linux/ipc_namespace.h>
 #include <linux/rhashtable.h>
+#include <linux/nstree.h>
 
 #include <linux/uaccess.h>
 
@@ -148,6 +149,7 @@ void shm_exit_ns(struct ipc_namespace *ns)
 static int __init ipc_ns_init(void)
 {
 	shm_init_ns(&init_ipc_ns);
+	ns_tree_add(&init_ipc_ns);
 	return 0;
 }
 
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 20/33
#41 lore raw

Re: [PATCH v2 20/33] ipc: support ns lookup

Quoted 4 lines · Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

Looks good. Feel free to add:

Honza

Quoted 70 lines
---
 ipc/msgutil.c   | 1 +
 ipc/namespace.c | 3 +++
 ipc/shm.c       | 2 ++
 3 files changed, 6 insertions(+)
ipc/msgutil.c +1 -0
index c7be0c792647..bbf61275df41 100644
@@ -15,6 +15,7 @@
 #include <linux/proc_ns.h>
 #include <linux/uaccess.h>
 #include <linux/sched.h>
+#include <linux/nstree.h>
 
 #include "util.h"
 
ipc/namespace.c +3 -0
index d4188a88ee57..9f923c1a1eb3 100644
@@ -15,6 +15,7 @@
 #include <linux/mount.h>
 #include <linux/user_namespace.h>
 #include <linux/proc_ns.h>
+#include <linux/nstree.h>
 #include <linux/sched/task.h>
 
 #include "util.h"
@@ -85,6 +86,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
 
 	sem_init_ns(ns);
 	shm_init_ns(ns);
+	ns_tree_add(ns);
 
 	return ns;
 
@@ -201,6 +203,7 @@ void put_ipc_ns(struct ipc_namespace *ns)
 		mq_clear_sbinfo(ns);
 		spin_unlock(&mq_lock);
 
+		ns_tree_remove(ns);
 		if (llist_add(&ns->mnt_llist, &free_ipc_list))
 			schedule_work(&free_ipc_work);
 	}
ipc/shm.c +2 -0
index a9310b6dbbc3..3db36773dd10 100644
@@ -45,6 +45,7 @@
 #include <linux/mount.h>
 #include <linux/ipc_namespace.h>
 #include <linux/rhashtable.h>
+#include <linux/nstree.h>
 
 #include <linux/uaccess.h>
 
@@ -148,6 +149,7 @@ void shm_exit_ns(struct ipc_namespace *ns)
 static int __init ipc_ns_init(void)
 {
 	shm_init_ns(&init_ipc_ns);
+	ns_tree_add(&init_ipc_ns);
 	return 0;
 }
 
Signature · 2 lines
-- 
2.47.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#42 lore raw

PATCH v2 21/33 net: support ns lookup

In reply to Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

The network namespace has a separate list with different lifetime rules which we can just leave in tact. We have a similar concept for mount namespaces as well where it is on two differenet lists for different purposes.

---
 include/net/net_namespace.h | 1 +
 net/core/net_namespace.c    | 8 ++++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
include/net/net_namespace.h +1 -0
index 025a7574b275..42075748dff1 100644
@@ -11,6 +11,7 @@
 #include <linux/list.h>
 #include <linux/sysctl.h>
 #include <linux/uidgid.h>
+#include <linux/nstree.h>
 
 #include <net/flow.h>
 #include <net/netns/core.h>
net/core/net_namespace.c +6 -2
index 5fb7bd8ac45a..169ec22c4758 100644
@@ -20,6 +20,7 @@
 #include <linux/sched/task.h>
 #include <linux/uidgid.h>
 #include <linux/proc_fs.h>
+#include <linux/nstree.h>
 
 #include <net/aligned_data.h>
 #include <net/sock.h>
@@ -445,7 +446,7 @@ static __net_init int setup_net(struct net *net)
 	LIST_HEAD(net_exit_list);
 	int error = 0;
 
-	net->net_cookie = atomic64_inc_return(&net_aligned_data.net_cookie);
+	net->net_cookie = ns_tree_gen_id(&net->ns);
 
 	list_for_each_entry(ops, &pernet_list, list) {
 		error = ops_init(ops, net);
@@ -455,6 +456,7 @@ static __net_init int setup_net(struct net *net)
 	down_write(&net_rwsem);
 	list_add_tail_rcu(&net->list, &net_namespace_list);
 	up_write(&net_rwsem);
+	ns_tree_add_raw(net);
 out:
 	return error;
 
@@ -674,8 +676,10 @@ static void cleanup_net(struct work_struct *work)
 
 	/* Don't let anyone else find us. */
 	down_write(&net_rwsem);
-	llist_for_each_entry(net, net_kill_list, cleanup_list)
+	llist_for_each_entry(net, net_kill_list, cleanup_list) {
+		ns_tree_remove(net);
 		list_del_rcu(&net->list);
+	}
 	/* Cache last net. After we unlock rtnl, no one new net
 	 * added to net_namespace_list can assign nsid pointer
 	 * to a net from net_kill_list (see peernet2id_alloc()).
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 21/33
#43 lore raw

Re: [PATCH v2 21/33] net: support ns lookup

Quoted 9 lines · Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

The network namespace has a separate list with different lifetime rules which we can just leave in tact. We have a similar concept for mount namespaces as well where it is on two differenet lists for different purposes.

Just some nits below. Feel free to add:

include/net/net_namespace.h line 17

Quoted 12 lines
include/net/net_namespace.h +1 -0
index 025a7574b275..42075748dff1 100644
@@ -11,6 +11,7 @@
 #include <linux/list.h>
 #include <linux/sysctl.h>
 #include <linux/uidgid.h>
+#include <linux/nstree.h>
 
 #include <net/flow.h>
 #include <net/netns/core.h>

Why this include?

Quoted 13 lines
net/core/net_namespace.c +1 -0
index 5fb7bd8ac45a..169ec22c4758 100644
@@ -20,6 +20,7 @@
 #include <linux/sched/task.h>
 #include <linux/uidgid.h>
 #include <linux/proc_fs.h>
+#include <linux/nstree.h>
 
 #include <net/aligned_data.h>
 #include <net/sock.h>

net/core/net_namespace.c static __net_init int setup_net(struct net *net) · line 449

Quoted 5 lines
net/core/net_namespace.c +1 -1
@@ -445,7 +446,7 @@ static __net_init int setup_net(struct net *net)
 	LIST_HEAD(net_exit_list);
 	int error = 0;
 
-	net->net_cookie = atomic64_inc_return(&net_aligned_data.net_cookie);
+	net->net_cookie = ns_tree_gen_id(&net->ns);

net_cookie seems to be unused now and can be deleted?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#44 lore raw

Re: [PATCH v2 21/33] net: support ns lookup

Quoted 25 lines · Jan Kara
Quoted 9 lines · Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

The network namespace has a separate list with different lifetime rules which we can just leave in tact. We have a similar concept for mount namespaces as well where it is on two differenet lists for different purposes.

Just some nits below. Feel free to add:

Quoted 10 lines
include/net/net_namespace.h +1 -0
index 025a7574b275..42075748dff1 100644
@@ -11,6 +11,7 @@
 #include <linux/list.h>
 #include <linux/sysctl.h>
 #include <linux/uidgid.h>
+#include <linux/nstree.h>
 
Quoted 4 lines

include/net/net_namespace.h line 17

Quoted 2 lines
include/net/net_namespace.h +0 -0
 #include <net/flow.h>
 #include <net/netns/core.h>

Why this include?

Dropped.

Quoted 17 lines
Quoted 17 lines
net/core/net_namespace.c +1 -0
index 5fb7bd8ac45a..169ec22c4758 100644
@@ -20,6 +20,7 @@
 #include <linux/sched/task.h>
 #include <linux/uidgid.h>
 #include <linux/proc_fs.h>
+#include <linux/nstree.h>
 
 #include <net/aligned_data.h>
 #include <net/sock.h>
@@ -445,7 +446,7 @@ static __net_init int setup_net(struct net *net)
 	LIST_HEAD(net_exit_list);
 	int error = 0;
 
Quoted 4 lines

net/core/net_namespace.c static __net_init int setup_net(struct net *net) · line 449

Quoted 2 lines
net/core/net_namespace.c +1 -1
-	net->net_cookie = atomic64_inc_return(&net_aligned_data.net_cookie);
+	net->net_cookie = ns_tree_gen_id(&net->ns);

net_cookie seems to be unused now and can be deleted?

Yes. I planned to do this in a follow-up patch because it'll create some churn in net/. I'd rather get that in early after the next mw.

#45 lore raw

Re: [PATCH v2 21/33] net: support ns lookup

In reply to Christian Brauner

From: Christian Brauner <[email protected]>
Date: Fri, 12 Sep 2025 13:52:44 +0200

Quoted 31 lines

Support the generic ns lookup infrastructure to support file handles for namespaces.

The network namespace has a separate list with different lifetime rules which we can just leave in tact. We have a similar concept for mount namespaces as well where it is on two differenet lists for different purposes.

---
 include/net/net_namespace.h | 1 +
 net/core/net_namespace.c    | 8 ++++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
include/net/net_namespace.h +1 -0
index 025a7574b275..42075748dff1 100644
@@ -11,6 +11,7 @@
 #include <linux/list.h>
 #include <linux/sysctl.h>
 #include <linux/uidgid.h>
+#include <linux/nstree.h>
 
 #include <net/flow.h>
 #include <net/netns/core.h>
net/core/net_namespace.c +0 -0
index 5fb7bd8ac45a..169ec22c4758 100644

net/core/net_namespace.c line 25

Quoted 6 lines
net/core/net_namespace.c +1 -0
@@ -20,6 +20,7 @@
 #include <linux/sched/task.h>
 #include <linux/uidgid.h>
 #include <linux/proc_fs.h>
+#include <linux/nstree.h>
 
 #include <net/aligned_data.h>

nit: This is no longer necessary.

Quoted 19 lines
net/core/net_namespace.c +2 -1
 #include <net/sock.h>
@@ -445,7 +446,7 @@ static __net_init int setup_net(struct net *net)
 	LIST_HEAD(net_exit_list);
 	int error = 0;
 
-	net->net_cookie = atomic64_inc_return(&net_aligned_data.net_cookie);
+	net->net_cookie = ns_tree_gen_id(&net->ns);
 
 	list_for_each_entry(ops, &pernet_list, list) {
 		error = ops_init(ops, net);
@@ -455,6 +456,7 @@ static __net_init int setup_net(struct net *net)
 	down_write(&net_rwsem);
 	list_add_tail_rcu(&net->list, &net_namespace_list);
 	up_write(&net_rwsem);
+	ns_tree_add_raw(net);
 out:
 	return error;
 

net/core/net_namespace.c static void cleanup_net(struct work_struct *work) · line 680

Quoted 6 lines
net/core/net_namespace.c +2 -1
@@ -674,8 +676,10 @@ static void cleanup_net(struct work_struct *work)
 
 	/* Don't let anyone else find us. */
 	down_write(&net_rwsem);
-	llist_for_each_entry(net, net_kill_list, cleanup_list)
+	llist_for_each_entry(net, net_kill_list, cleanup_list) {
+		ns_tree_remove(net);

Looking at refcount_inc_not_zero() in nsfs_fh_to_dentry(), I think ns_tree_remove() fits better in __put_net() which is called just after refcount_dec_and_test(&net->ns.count).

cleanup_net() is single threaded work and somtimes takes long with too many netns and the netns may stay longer unnecessarily until next cleanup_net().

Quoted 8 lines
net/core/net_namespace.c +1 -0
 		list_del_rcu(&net->list);
+	}
 	/* Cache last net. After we unlock rtnl, no one new net
 	 * added to net_namespace_list can assign nsid pointer
 	 * to a net from net_kill_list (see peernet2id_alloc()).
Signature · 2 lines
-- 
2.47.3
author maintainer
#46 lore raw

PATCH v2 22/33 pid: support ns lookup

In reply to Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

---
 kernel/pid_namespace.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/pid_namespace.c +4 -0
index 20ce4052d1c5..228ae20299f9 100644
@@ -23,6 +23,7 @@
 #include <linux/sched/task.h>
 #include <linux/sched/signal.h>
 #include <linux/idr.h>
+#include <linux/nstree.h>
 #include <uapi/linux/wait.h>
 #include "pid_sysctl.h"
 
@@ -122,6 +123,7 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
 	ns->memfd_noexec_scope = pidns_memfd_noexec_scope(parent_pid_ns);
 #endif
 
+	ns_tree_add(ns);
 	return ns;
 
 out_free_inum:
@@ -147,6 +149,7 @@ static void delayed_free_pidns(struct rcu_head *p)
 
 static void destroy_pid_namespace(struct pid_namespace *ns)
 {
+	ns_tree_remove(ns);
 	unregister_pidns_sysctls(ns);
 
 	ns_free_inum(&ns->ns);
@@ -473,6 +476,7 @@ static __init int pid_namespaces_init(void)
 #endif
 
 	register_pid_ns_sysctl_table_vm();
+	ns_tree_add(&init_pid_ns);
 	return 0;
 }
 
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 22/33
#47 lore raw

Re: [PATCH v2 22/33] pid: support ns lookup

Quoted 4 lines · Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

Looks good. Feel free to add:

Honza

Quoted 44 lines
---
 kernel/pid_namespace.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/pid_namespace.c +4 -0
index 20ce4052d1c5..228ae20299f9 100644
@@ -23,6 +23,7 @@
 #include <linux/sched/task.h>
 #include <linux/sched/signal.h>
 #include <linux/idr.h>
+#include <linux/nstree.h>
 #include <uapi/linux/wait.h>
 #include "pid_sysctl.h"
 
@@ -122,6 +123,7 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
 	ns->memfd_noexec_scope = pidns_memfd_noexec_scope(parent_pid_ns);
 #endif
 
+	ns_tree_add(ns);
 	return ns;
 
 out_free_inum:
@@ -147,6 +149,7 @@ static void delayed_free_pidns(struct rcu_head *p)
 
 static void destroy_pid_namespace(struct pid_namespace *ns)
 {
+	ns_tree_remove(ns);
 	unregister_pidns_sysctls(ns);
 
 	ns_free_inum(&ns->ns);
@@ -473,6 +476,7 @@ static __init int pid_namespaces_init(void)
 #endif
 
 	register_pid_ns_sysctl_table_vm();
+	ns_tree_add(&init_pid_ns);
 	return 0;
 }
 
Signature · 2 lines
-- 
2.47.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#48 lore raw

PATCH v2 23/33 time: support ns lookup

In reply to Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

---
 include/linux/time_namespace.h |  5 +++++
 init/main.c                    |  2 ++
 kernel/time/namespace.c        | 11 ++++++++++-
 3 files changed, 17 insertions(+), 1 deletion(-)
include/linux/time_namespace.h +5 -0
index bb2c52f4fc94..7f6af7a9771e 100644
@@ -33,6 +33,7 @@ struct time_namespace {
 extern struct time_namespace init_time_ns;
 
 #ifdef CONFIG_TIME_NS
+void __init time_ns_init(void);
 extern int vdso_join_timens(struct task_struct *task,
 			    struct time_namespace *ns);
 extern void timens_commit(struct task_struct *tsk, struct time_namespace *ns);
@@ -108,6 +109,10 @@ static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
 }
 
 #else
+static inline void __init time_ns_init(void)
+{
+}
+
 static inline int vdso_join_timens(struct task_struct *task,
 				   struct time_namespace *ns)
 {
init/main.c +2 -0
index 0ee0ee7b7c2c..e7d2c57c65a7 100644
@@ -103,6 +103,7 @@
 #include <linux/randomize_kstack.h>
 #include <linux/pidfs.h>
 #include <linux/ptdump.h>
+#include <linux/time_namespace.h>
 #include <net/net_namespace.h>
 
 #include <asm/io.h>
@@ -1072,6 +1073,7 @@ void start_kernel(void)
 	fork_init();
 	proc_caches_init();
 	uts_ns_init();
+	time_ns_init();
 	key_init();
 	security_init();
 	dbg_late_init();
kernel/time/namespace.c +10 -1
index 0be93d8f2896..408f60d0a3b6 100644
@@ -12,6 +12,7 @@
 #include <linux/seq_file.h>
 #include <linux/proc_ns.h>
 #include <linux/export.h>
+#include <linux/nstree.h>
 #include <linux/time.h>
 #include <linux/slab.h>
 #include <linux/cred.h>
@@ -104,6 +105,7 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
 	ns->user_ns = get_user_ns(user_ns);
 	ns->offsets = old_ns->offsets;
 	ns->frozen_offsets = false;
+	ns_tree_add(ns);
 	return ns;
 
 fail_free_page:
@@ -250,11 +252,13 @@ static void timens_set_vvar_page(struct task_struct *task,
 
 void free_time_ns(struct time_namespace *ns)
 {
+	ns_tree_remove(ns);
 	dec_time_namespaces(ns->ucounts);
 	put_user_ns(ns->user_ns);
 	ns_free_inum(&ns->ns);
 	__free_page(ns->vvar_page);
-	kfree(ns);
+	/* Concurrent nstree traversal depends on a grace period. */
+	kfree_rcu(ns, ns.ns_rcu);
 }
 
 static struct time_namespace *to_time_ns(struct ns_common *ns)
@@ -487,3 +491,8 @@ struct time_namespace init_time_ns = {
 	.ns.ops		= &timens_operations,
 	.frozen_offsets	= true,
 };
+
+void __init time_ns_init(void)
+{
+	ns_tree_add(&init_time_ns);
+}
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 23/33
#49 lore raw

Re: [PATCH v2 23/33] time: support ns lookup

Quoted 5 lines · Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

Looks good. Feel free to add:

Honza

Quoted 97 lines
---
 include/linux/time_namespace.h |  5 +++++
 init/main.c                    |  2 ++
 kernel/time/namespace.c        | 11 ++++++++++-
 3 files changed, 17 insertions(+), 1 deletion(-)
include/linux/time_namespace.h +5 -0
index bb2c52f4fc94..7f6af7a9771e 100644
@@ -33,6 +33,7 @@ struct time_namespace {
 extern struct time_namespace init_time_ns;
 
 #ifdef CONFIG_TIME_NS
+void __init time_ns_init(void);
 extern int vdso_join_timens(struct task_struct *task,
 			    struct time_namespace *ns);
 extern void timens_commit(struct task_struct *tsk, struct time_namespace *ns);
@@ -108,6 +109,10 @@ static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
 }
 
 #else
+static inline void __init time_ns_init(void)
+{
+}
+
 static inline int vdso_join_timens(struct task_struct *task,
 				   struct time_namespace *ns)
 {
init/main.c +2 -0
index 0ee0ee7b7c2c..e7d2c57c65a7 100644
@@ -103,6 +103,7 @@
 #include <linux/randomize_kstack.h>
 #include <linux/pidfs.h>
 #include <linux/ptdump.h>
+#include <linux/time_namespace.h>
 #include <net/net_namespace.h>
 
 #include <asm/io.h>
@@ -1072,6 +1073,7 @@ void start_kernel(void)
 	fork_init();
 	proc_caches_init();
 	uts_ns_init();
+	time_ns_init();
 	key_init();
 	security_init();
 	dbg_late_init();
kernel/time/namespace.c +10 -1
index 0be93d8f2896..408f60d0a3b6 100644
@@ -12,6 +12,7 @@
 #include <linux/seq_file.h>
 #include <linux/proc_ns.h>
 #include <linux/export.h>
+#include <linux/nstree.h>
 #include <linux/time.h>
 #include <linux/slab.h>
 #include <linux/cred.h>
@@ -104,6 +105,7 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
 	ns->user_ns = get_user_ns(user_ns);
 	ns->offsets = old_ns->offsets;
 	ns->frozen_offsets = false;
+	ns_tree_add(ns);
 	return ns;
 
 fail_free_page:
@@ -250,11 +252,13 @@ static void timens_set_vvar_page(struct task_struct *task,
 
 void free_time_ns(struct time_namespace *ns)
 {
+	ns_tree_remove(ns);
 	dec_time_namespaces(ns->ucounts);
 	put_user_ns(ns->user_ns);
 	ns_free_inum(&ns->ns);
 	__free_page(ns->vvar_page);
-	kfree(ns);
+	/* Concurrent nstree traversal depends on a grace period. */
+	kfree_rcu(ns, ns.ns_rcu);
 }
 
 static struct time_namespace *to_time_ns(struct ns_common *ns)
@@ -487,3 +491,8 @@ struct time_namespace init_time_ns = {
 	.ns.ops		= &timens_operations,
 	.frozen_offsets	= true,
 };
+
+void __init time_ns_init(void)
+{
+	ns_tree_add(&init_time_ns);
+}
Signature · 2 lines
-- 
2.47.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#50 lore raw

PATCH v2 24/33 user: support ns lookup

In reply to Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

---
 kernel/user_namespace.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/user_namespace.c +6 -1
index 98f4fe84d039..ade5b6806c5c 100644
@@ -21,6 +21,7 @@
 #include <linux/fs_struct.h>
 #include <linux/bsearch.h>
 #include <linux/sort.h>
+#include <linux/nstree.h>
 
 static struct kmem_cache *user_ns_cachep __ro_after_init;
 static DEFINE_MUTEX(userns_state_mutex);
@@ -158,6 +159,7 @@ int create_user_ns(struct cred *new)
 		goto fail_keyring;
 
 	set_cred_user_ns(new, ns);
+	ns_tree_add(ns);
 	return 0;
 fail_keyring:
 #ifdef CONFIG_PERSISTENT_KEYRINGS
@@ -200,6 +202,7 @@ static void free_user_ns(struct work_struct *work)
 	do {
 		struct ucounts *ucounts = ns->ucounts;
 		parent = ns->parent;
+		ns_tree_remove(ns);
 		if (ns->gid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
 			kfree(ns->gid_map.forward);
 			kfree(ns->gid_map.reverse);
@@ -218,7 +221,8 @@ static void free_user_ns(struct work_struct *work)
 		retire_userns_sysctls(ns);
 		key_free_user_ns(ns);
 		ns_free_inum(&ns->ns);
-		kmem_cache_free(user_ns_cachep, ns);
+		/* Concurrent nstree traversal depends on a grace period. */
+		kfree_rcu(ns, ns.ns_rcu);
 		dec_user_namespaces(ucounts);
 		ns = parent;
 	} while (refcount_dec_and_test(&parent->ns.count));
@@ -1412,6 +1416,7 @@ const struct proc_ns_operations userns_operations = {
 static __init int user_namespaces_init(void)
 {
 	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC | SLAB_ACCOUNT);
+	ns_tree_add(&init_user_ns);
 	return 0;
 }
 subsys_initcall(user_namespaces_init);
Signature · 1 line
2.47.3
reviewer
#51 lore raw

Re: [PATCH v2 24/33] user: support ns lookup

Quoted 4 lines · Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

...

Quoted 9 lines
kernel/user_namespace.c +1 -0
@@ -200,6 +202,7 @@ static void free_user_ns(struct work_struct *work)
 	do {
 		struct ucounts *ucounts = ns->ucounts;
 		parent = ns->parent;
+		ns_tree_remove(ns);
 		if (ns->gid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
 			kfree(ns->gid_map.forward);
 			kfree(ns->gid_map.reverse);

kernel/user_namespace.c static void free_user_ns(struct work_struct *work) · line 225

Quoted 6 lines
kernel/user_namespace.c +2 -1
@@ -218,7 +221,8 @@ static void free_user_ns(struct work_struct *work)
 		retire_userns_sysctls(ns);
 		key_free_user_ns(ns);
 		ns_free_inum(&ns->ns);
-		kmem_cache_free(user_ns_cachep, ns);
+		/* Concurrent nstree traversal depends on a grace period. */
+		kfree_rcu(ns, ns.ns_rcu);

So this is correct for now but it's a bit of a landmine. A lot of stuff that ns references is kfreed before the RCU expires. Thus if you lookup ns using id, then even if you're under RCU protection you have to be very careful about what you can and cannot dereference. IMHO this deserves a careful documentation at least or, preferably, split free_user_ns() into pre and post-RCU period parts...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#52 lore raw

Re: [PATCH v2 24/33] user: support ns lookup

Quoted 20 lines · Jan Kara
Quoted 4 lines · Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

...

Quoted 14 lines
kernel/user_namespace.c +2 -1
@@ -200,6 +202,7 @@ static void free_user_ns(struct work_struct *work)
 	do {
 		struct ucounts *ucounts = ns->ucounts;
 		parent = ns->parent;
+		ns_tree_remove(ns);
 		if (ns->gid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
 			kfree(ns->gid_map.forward);
 			kfree(ns->gid_map.reverse);
@@ -218,7 +221,8 @@ static void free_user_ns(struct work_struct *work)
 		retire_userns_sysctls(ns);
 		key_free_user_ns(ns);
 		ns_free_inum(&ns->ns);
-		kmem_cache_free(user_ns_cachep, ns);
+		/* Concurrent nstree traversal depends on a grace period. */
Quoted 8 lines

kernel/user_namespace.c static void free_user_ns(struct work_struct *work) · line 225

Quoted 1 line
kernel/user_namespace.c +1 -0
+		kfree_rcu(ns, ns.ns_rcu);

So this is correct for now but it's a bit of a landmine. A lot of stuff that ns references is kfreed before the RCU expires. Thus if you lookup ns using id, then even if you're under RCU protection you have to be very careful about what you can and cannot dereference. IMHO this deserves a careful documentation at least or, preferably, split free_user_ns() into pre and post-RCU period parts...

Right, the thing is that you cannot touch anything in any namespace structure without having an actual reference to it. IOW, the only thing that's valid under rcu is to access the reference count. That's the only guarantee that the _generic_ infrastructure gives _and_ expects. IOW, if one can get a live reference (inc_not_zero) that thing better be valid.

Individual namespace implementers may ofc provide additional guarantees but they are not transparent to the generic infrastructure.

Otherwise I fully agree.

reviewer Reviewed-by
#53 lore raw

Re: [PATCH v2 24/33] user: support ns lookup

Quoted 36 lines · Christian Brauner
Quoted 20 lines · Jan Kara
Quoted 4 lines · Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

...

Quoted 14 lines
kernel/user_namespace.c +2 -1
@@ -200,6 +202,7 @@ static void free_user_ns(struct work_struct *work)
 	do {
 		struct ucounts *ucounts = ns->ucounts;
 		parent = ns->parent;
+		ns_tree_remove(ns);
 		if (ns->gid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
 			kfree(ns->gid_map.forward);
 			kfree(ns->gid_map.reverse);
@@ -218,7 +221,8 @@ static void free_user_ns(struct work_struct *work)
 		retire_userns_sysctls(ns);
 		key_free_user_ns(ns);
 		ns_free_inum(&ns->ns);
-		kmem_cache_free(user_ns_cachep, ns);
+		/* Concurrent nstree traversal depends on a grace period. */
Quoted 8 lines

kernel/user_namespace.c static void free_user_ns(struct work_struct *work) · line 225

Quoted 1 line
kernel/user_namespace.c +1 -0
+		kfree_rcu(ns, ns.ns_rcu);

So this is correct for now but it's a bit of a landmine. A lot of stuff that ns references is kfreed before the RCU expires. Thus if you lookup ns using id, then even if you're under RCU protection you have to be very careful about what you can and cannot dereference. IMHO this deserves a careful documentation at least or, preferably, split free_user_ns() into pre and post-RCU period parts...

Right, the thing is that you cannot touch anything in any namespace structure without having an actual reference to it. IOW, the only thing that's valid under rcu is to access the reference count. That's the only guarantee that the _generic_ infrastructure gives _and_ expects. IOW, if one can get a live reference (inc_not_zero) that thing better be valid.

Quoted 4 lines

Individual namespace implementers may ofc provide additional guarantees but they are not transparent to the generic infrastructure.

Otherwise I fully agree.

I guess fair enough for this patch set so feel free to add:

but longer term we might need to revisit this.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#54 lore raw

PATCH v2 25/33 uts: support ns lookup

In reply to Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

---
 kernel/utsname.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/utsname.c +6 -1
index 02037010b378..64155417ae0c 100644
@@ -13,6 +13,7 @@
 #include <linux/cred.h>
 #include <linux/user_namespace.h>
 #include <linux/proc_ns.h>
+#include <linux/nstree.h>
 #include <linux/sched/task.h>
 
 static struct kmem_cache *uts_ns_cache __ro_after_init;
@@ -58,6 +59,7 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
 	memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
 	ns->user_ns = get_user_ns(user_ns);
 	up_read(&uts_sem);
+	ns_tree_add(ns);
 	return ns;
 
 fail_free:
@@ -93,10 +95,12 @@ struct uts_namespace *copy_utsname(unsigned long flags,
 
 void free_uts_ns(struct uts_namespace *ns)
 {
+	ns_tree_remove(ns);
 	dec_uts_namespaces(ns->ucounts);
 	put_user_ns(ns->user_ns);
 	ns_free_inum(&ns->ns);
-	kmem_cache_free(uts_ns_cache, ns);
+	/* Concurrent nstree traversal depends on a grace period. */
+	kfree_rcu(ns, ns.ns_rcu);
 }
 
 static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
@@ -162,4 +166,5 @@ void __init uts_ns_init(void)
 			offsetof(struct uts_namespace, name),
 			sizeof_field(struct uts_namespace, name),
 			NULL);
+	ns_tree_add(&init_uts_ns);
 }
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 25/33
#55 lore raw

Re: [PATCH v2 25/33] uts: support ns lookup

Quoted 4 lines · Christian Brauner

Support the generic ns lookup infrastructure to support file handles for namespaces.

Looks good. Feel free to add:

Honza

Quoted 48 lines
---
 kernel/utsname.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/utsname.c +6 -1
index 02037010b378..64155417ae0c 100644
@@ -13,6 +13,7 @@
 #include <linux/cred.h>
 #include <linux/user_namespace.h>
 #include <linux/proc_ns.h>
+#include <linux/nstree.h>
 #include <linux/sched/task.h>
 
 static struct kmem_cache *uts_ns_cache __ro_after_init;
@@ -58,6 +59,7 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
 	memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
 	ns->user_ns = get_user_ns(user_ns);
 	up_read(&uts_sem);
+	ns_tree_add(ns);
 	return ns;
 
 fail_free:
@@ -93,10 +95,12 @@ struct uts_namespace *copy_utsname(unsigned long flags,
 
 void free_uts_ns(struct uts_namespace *ns)
 {
+	ns_tree_remove(ns);
 	dec_uts_namespaces(ns->ucounts);
 	put_user_ns(ns->user_ns);
 	ns_free_inum(&ns->ns);
-	kmem_cache_free(uts_ns_cache, ns);
+	/* Concurrent nstree traversal depends on a grace period. */
+	kfree_rcu(ns, ns.ns_rcu);
 }
 
 static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
@@ -162,4 +166,5 @@ void __init uts_ns_init(void)
 			offsetof(struct uts_namespace, name),
 			sizeof_field(struct uts_namespace, name),
 			NULL);
+	ns_tree_add(&init_uts_ns);
 }
Signature · 2 lines
-- 
2.47.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#56 lore raw

PATCH v2 26/33 ns: add to_<type>_ns() to respective headers

In reply to Christian Brauner

Every namespace type has a container_of(ns, <ns_type>, ns) static inline function that is currently not exposed in the header. So we have a bunch of places that open-code it via container_of(). Move it to the headers so we can use it directly.

---
 include/linux/cgroup.h         | 5 +++++
 include/linux/ipc_namespace.h  | 5 +++++
 include/linux/pid_namespace.h  | 5 +++++
 include/linux/time_namespace.h | 4 ++++
 include/linux/user_namespace.h | 5 +++++
 include/linux/utsname.h        | 5 +++++
 include/net/net_namespace.h    | 5 +++++
 ipc/namespace.c                | 5 -----
 kernel/cgroup/namespace.c      | 5 -----
 kernel/pid_namespace.c         | 5 -----
 kernel/time/namespace.c        | 5 -----
 kernel/user_namespace.c        | 5 -----
 kernel/utsname.c               | 5 -----
 net/core/net_namespace.c       | 5 -----
 14 files changed, 34 insertions(+), 35 deletions(-)
include/linux/cgroup.h +5 -0
index b18fb5fcb38e..9ca25346f7cb 100644
@@ -794,6 +794,11 @@ extern struct cgroup_namespace init_cgroup_ns;
 
 #ifdef CONFIG_CGROUPS
 
+static inline struct cgroup_namespace *to_cg_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct cgroup_namespace, ns);
+}
+
 void free_cgroup_ns(struct cgroup_namespace *ns);
 
 struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
include/linux/ipc_namespace.h +5 -0
index e8240cf2611a..924e4754374f 100644
@@ -129,6 +129,11 @@ static inline int mq_init_ns(struct ipc_namespace *ns) { return 0; }
 #endif
 
 #if defined(CONFIG_IPC_NS)
+static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct ipc_namespace, ns);
+}
+
 extern struct ipc_namespace *copy_ipcs(unsigned long flags,
 	struct user_namespace *user_ns, struct ipc_namespace *ns);
 
include/linux/pid_namespace.h +5 -0
index 7c67a5811199..ba0efc8c8596 100644
@@ -54,6 +54,11 @@ extern struct pid_namespace init_pid_ns;
 #define PIDNS_ADDING (1U << 31)
 
 #ifdef CONFIG_PID_NS
+static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct pid_namespace, ns);
+}
+
 static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)
 {
 	if (ns != &init_pid_ns)
include/linux/time_namespace.h +4 -0
index 7f6af7a9771e..a47a4ce4183e 100644
@@ -33,6 +33,10 @@ struct time_namespace {
 extern struct time_namespace init_time_ns;
 
 #ifdef CONFIG_TIME_NS
+static inline struct time_namespace *to_time_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct time_namespace, ns);
+}
 void __init time_ns_init(void);
 extern int vdso_join_timens(struct task_struct *task,
 			    struct time_namespace *ns);
include/linux/user_namespace.h +5 -0
index a0bb6d012137..a09056ad090e 100644
@@ -168,6 +168,11 @@ static inline void set_userns_rlimit_max(struct user_namespace *ns,
 
 #ifdef CONFIG_USER_NS
 
+static inline struct user_namespace *to_user_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct user_namespace, ns);
+}
+
 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
 {
 	if (ns)
include/linux/utsname.h +5 -0
index bf7613ba412b..5d34c4f0f945 100644
@@ -30,6 +30,11 @@ struct uts_namespace {
 extern struct uts_namespace init_uts_ns;
 
 #ifdef CONFIG_UTS_NS
+static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct uts_namespace, ns);
+}
+
 static inline void get_uts_ns(struct uts_namespace *ns)
 {
 	refcount_inc(&ns->ns.count);
include/net/net_namespace.h +5 -0
index 42075748dff1..b9c5f6c7ee1e 100644
@@ -263,6 +263,11 @@ void ipx_unregister_sysctl(void);
 #ifdef CONFIG_NET_NS
 void __put_net(struct net *net);
 
+static inline struct net *to_net_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct net, ns);
+}
+
 /* Try using get_net_track() instead */
 static inline struct net *get_net(struct net *net)
 {
ipc/namespace.c +0 -5
index 9f923c1a1eb3..89588819956b 100644
@@ -209,11 +209,6 @@ void put_ipc_ns(struct ipc_namespace *ns)
 	}
 }
 
-static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct ipc_namespace, ns);
-}
-
 static struct ns_common *ipcns_get(struct task_struct *task)
 {
 	struct ipc_namespace *ns = NULL;
kernel/cgroup/namespace.c +0 -5
index fc12c416dfeb..5a327914b565 100644
@@ -89,11 +89,6 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
 	return new_ns;
 }
 
-static inline struct cgroup_namespace *to_cg_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct cgroup_namespace, ns);
-}
-
 static int cgroupns_install(struct nsset *nsset, struct ns_common *ns)
 {
 	struct nsproxy *nsproxy = nsset->nsproxy;
kernel/pid_namespace.c +0 -5
index 228ae20299f9..9b327420309e 100644
@@ -345,11 +345,6 @@ int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
 	return 0;
 }
 
-static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct pid_namespace, ns);
-}
-
 static struct ns_common *pidns_get(struct task_struct *task)
 {
 	struct pid_namespace *ns;
kernel/time/namespace.c +0 -5
index 408f60d0a3b6..20b65f90549e 100644
@@ -261,11 +261,6 @@ void free_time_ns(struct time_namespace *ns)
 	kfree_rcu(ns, ns.ns_rcu);
 }
 
-static struct time_namespace *to_time_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct time_namespace, ns);
-}
-
 static struct ns_common *timens_get(struct task_struct *task)
 {
 	struct time_namespace *ns = NULL;
kernel/user_namespace.c +0 -5
index ade5b6806c5c..cfb0e28f2779 100644
@@ -1325,11 +1325,6 @@ bool current_in_userns(const struct user_namespace *target_ns)
 }
 EXPORT_SYMBOL(current_in_userns);
 
-static inline struct user_namespace *to_user_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct user_namespace, ns);
-}
-
 static struct ns_common *userns_get(struct task_struct *task)
 {
 	struct user_namespace *user_ns;
kernel/utsname.c +0 -5
index 64155417ae0c..a682830742d3 100644
@@ -103,11 +103,6 @@ void free_uts_ns(struct uts_namespace *ns)
 	kfree_rcu(ns, ns.ns_rcu);
 }
 
-static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct uts_namespace, ns);
-}
-
 static struct ns_common *utsns_get(struct task_struct *task)
 {
 	struct uts_namespace *ns = NULL;
net/core/net_namespace.c +0 -5
index 169ec22c4758..a57b3cda8dbc 100644
@@ -1541,11 +1541,6 @@ static struct ns_common *netns_get(struct task_struct *task)
 	return net ? &net->ns : NULL;
 }
 
-static inline struct net *to_net_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct net, ns);
-}
-
 static void netns_put(struct ns_common *ns)
 {
 	put_net(to_net_ns(ns));
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 26/33
#57 lore raw

Re: [PATCH v2 26/33] ns: add to_<type>_ns() to respective headers

Quoted 7 lines · Christian Brauner

Every namespace type has a container_of(ns, <ns_type>, ns) static inline function that is currently not exposed in the header. So we have a bunch of places that open-code it via container_of(). Move it to the headers so we can use it directly.

Looks good. Feel free to add:

Honza

Quoted 244 lines
---
 include/linux/cgroup.h         | 5 +++++
 include/linux/ipc_namespace.h  | 5 +++++
 include/linux/pid_namespace.h  | 5 +++++
 include/linux/time_namespace.h | 4 ++++
 include/linux/user_namespace.h | 5 +++++
 include/linux/utsname.h        | 5 +++++
 include/net/net_namespace.h    | 5 +++++
 ipc/namespace.c                | 5 -----
 kernel/cgroup/namespace.c      | 5 -----
 kernel/pid_namespace.c         | 5 -----
 kernel/time/namespace.c        | 5 -----
 kernel/user_namespace.c        | 5 -----
 kernel/utsname.c               | 5 -----
 net/core/net_namespace.c       | 5 -----
 14 files changed, 34 insertions(+), 35 deletions(-)
include/linux/cgroup.h +5 -0
index b18fb5fcb38e..9ca25346f7cb 100644
@@ -794,6 +794,11 @@ extern struct cgroup_namespace init_cgroup_ns;
 
 #ifdef CONFIG_CGROUPS
 
+static inline struct cgroup_namespace *to_cg_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct cgroup_namespace, ns);
+}
+
 void free_cgroup_ns(struct cgroup_namespace *ns);
 
 struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
include/linux/ipc_namespace.h +5 -0
index e8240cf2611a..924e4754374f 100644
@@ -129,6 +129,11 @@ static inline int mq_init_ns(struct ipc_namespace *ns) { return 0; }
 #endif
 
 #if defined(CONFIG_IPC_NS)
+static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct ipc_namespace, ns);
+}
+
 extern struct ipc_namespace *copy_ipcs(unsigned long flags,
 	struct user_namespace *user_ns, struct ipc_namespace *ns);
 
include/linux/pid_namespace.h +5 -0
index 7c67a5811199..ba0efc8c8596 100644
@@ -54,6 +54,11 @@ extern struct pid_namespace init_pid_ns;
 #define PIDNS_ADDING (1U << 31)
 
 #ifdef CONFIG_PID_NS
+static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct pid_namespace, ns);
+}
+
 static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)
 {
 	if (ns != &init_pid_ns)
include/linux/time_namespace.h +4 -0
index 7f6af7a9771e..a47a4ce4183e 100644
@@ -33,6 +33,10 @@ struct time_namespace {
 extern struct time_namespace init_time_ns;
 
 #ifdef CONFIG_TIME_NS
+static inline struct time_namespace *to_time_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct time_namespace, ns);
+}
 void __init time_ns_init(void);
 extern int vdso_join_timens(struct task_struct *task,
 			    struct time_namespace *ns);
include/linux/user_namespace.h +5 -0
index a0bb6d012137..a09056ad090e 100644
@@ -168,6 +168,11 @@ static inline void set_userns_rlimit_max(struct user_namespace *ns,
 
 #ifdef CONFIG_USER_NS
 
+static inline struct user_namespace *to_user_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct user_namespace, ns);
+}
+
 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
 {
 	if (ns)
include/linux/utsname.h +5 -0
index bf7613ba412b..5d34c4f0f945 100644
@@ -30,6 +30,11 @@ struct uts_namespace {
 extern struct uts_namespace init_uts_ns;
 
 #ifdef CONFIG_UTS_NS
+static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct uts_namespace, ns);
+}
+
 static inline void get_uts_ns(struct uts_namespace *ns)
 {
 	refcount_inc(&ns->ns.count);
include/net/net_namespace.h +5 -0
index 42075748dff1..b9c5f6c7ee1e 100644
@@ -263,6 +263,11 @@ void ipx_unregister_sysctl(void);
 #ifdef CONFIG_NET_NS
 void __put_net(struct net *net);
 
+static inline struct net *to_net_ns(struct ns_common *ns)
+{
+	return container_of(ns, struct net, ns);
+}
+
 /* Try using get_net_track() instead */
 static inline struct net *get_net(struct net *net)
 {
ipc/namespace.c +0 -5
index 9f923c1a1eb3..89588819956b 100644
@@ -209,11 +209,6 @@ void put_ipc_ns(struct ipc_namespace *ns)
 	}
 }
 
-static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct ipc_namespace, ns);
-}
-
 static struct ns_common *ipcns_get(struct task_struct *task)
 {
 	struct ipc_namespace *ns = NULL;
kernel/cgroup/namespace.c +0 -5
index fc12c416dfeb..5a327914b565 100644
@@ -89,11 +89,6 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
 	return new_ns;
 }
 
-static inline struct cgroup_namespace *to_cg_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct cgroup_namespace, ns);
-}
-
 static int cgroupns_install(struct nsset *nsset, struct ns_common *ns)
 {
 	struct nsproxy *nsproxy = nsset->nsproxy;
kernel/pid_namespace.c +0 -5
index 228ae20299f9..9b327420309e 100644
@@ -345,11 +345,6 @@ int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
 	return 0;
 }
 
-static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct pid_namespace, ns);
-}
-
 static struct ns_common *pidns_get(struct task_struct *task)
 {
 	struct pid_namespace *ns;
kernel/time/namespace.c +0 -5
index 408f60d0a3b6..20b65f90549e 100644
@@ -261,11 +261,6 @@ void free_time_ns(struct time_namespace *ns)
 	kfree_rcu(ns, ns.ns_rcu);
 }
 
-static struct time_namespace *to_time_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct time_namespace, ns);
-}
-
 static struct ns_common *timens_get(struct task_struct *task)
 {
 	struct time_namespace *ns = NULL;
kernel/user_namespace.c +0 -5
index ade5b6806c5c..cfb0e28f2779 100644
@@ -1325,11 +1325,6 @@ bool current_in_userns(const struct user_namespace *target_ns)
 }
 EXPORT_SYMBOL(current_in_userns);
 
-static inline struct user_namespace *to_user_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct user_namespace, ns);
-}
-
 static struct ns_common *userns_get(struct task_struct *task)
 {
 	struct user_namespace *user_ns;
kernel/utsname.c +0 -5
index 64155417ae0c..a682830742d3 100644
@@ -103,11 +103,6 @@ void free_uts_ns(struct uts_namespace *ns)
 	kfree_rcu(ns, ns.ns_rcu);
 }
 
-static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct uts_namespace, ns);
-}
-
 static struct ns_common *utsns_get(struct task_struct *task)
 {
 	struct uts_namespace *ns = NULL;
net/core/net_namespace.c +0 -5
index 169ec22c4758..a57b3cda8dbc 100644
@@ -1541,11 +1541,6 @@ static struct ns_common *netns_get(struct task_struct *task)
 	return net ? &net->ns : NULL;
 }
 
-static inline struct net *to_net_ns(struct ns_common *ns)
-{
-	return container_of(ns, struct net, ns);
-}
-
 static void netns_put(struct ns_common *ns)
 {
 	put_net(to_net_ns(ns));
Signature · 2 lines
-- 
2.47.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#58 lore raw

PATCH v2 27/33 nsfs: add current_in_namespace()

In reply to Christian Brauner

Add a helper to easily check whether a given namespace is the caller's current namespace. This is currently open-coded in a lot of places. Simply switch on the type and compare the results.

---
 include/linux/nsfs.h | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
include/linux/nsfs.h +15 -1
index fb84aa538091..e5a5fa83d36b 100644
@@ -5,6 +5,8 @@
 #define _LINUX_NSFS_H
 
 #include <linux/ns_common.h>
+#include <linux/cred.h>
+#include <linux/pid_namespace.h>
 
 struct path;
 struct task_struct;
@@ -22,5 +24,17 @@ int ns_get_name(char *buf, size_t size, struct task_struct *task,
 			const struct proc_ns_operations *ns_ops);
 void nsfs_init(void);
 
-#endif /* _LINUX_NSFS_H */
+#define __current_namespace_from_type(__ns)				\
+	_Generic((__ns),						\
+		struct cgroup_namespace *: current->nsproxy->cgroup_ns,	\
+		struct ipc_namespace *:    current->nsproxy->ipc_ns,	\
+		struct net *:              current->nsproxy->net_ns,	\
+		struct pid_namespace *:    task_active_pid_ns(current),	\
+		struct mnt_namespace *:    current->nsproxy->mnt_ns,	\
+		struct time_namespace *:   current->nsproxy->time_ns,	\
+		struct user_namespace *:   current_user_ns(),		\
+		struct uts_namespace *:    current->nsproxy->uts_ns)
+
+#define current_in_namespace(__ns) (__current_namespace_from_type(__ns) == __ns)
 
+#endif /* _LINUX_NSFS_H */
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 27/33
#59 lore raw

Re: [PATCH v2 27/33] nsfs: add current_in_namespace()

Quoted 6 lines · Christian Brauner

Add a helper to easily check whether a given namespace is the caller's current namespace. This is currently open-coded in a lot of places. Simply switch on the type and compare the results.

Looks good. Feel free to add:

Honza

Quoted 40 lines
---
 include/linux/nsfs.h | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
include/linux/nsfs.h +15 -1
index fb84aa538091..e5a5fa83d36b 100644
@@ -5,6 +5,8 @@
 #define _LINUX_NSFS_H
 
 #include <linux/ns_common.h>
+#include <linux/cred.h>
+#include <linux/pid_namespace.h>
 
 struct path;
 struct task_struct;
@@ -22,5 +24,17 @@ int ns_get_name(char *buf, size_t size, struct task_struct *task,
 			const struct proc_ns_operations *ns_ops);
 void nsfs_init(void);
 
-#endif /* _LINUX_NSFS_H */
+#define __current_namespace_from_type(__ns)				\
+	_Generic((__ns),						\
+		struct cgroup_namespace *: current->nsproxy->cgroup_ns,	\
+		struct ipc_namespace *:    current->nsproxy->ipc_ns,	\
+		struct net *:              current->nsproxy->net_ns,	\
+		struct pid_namespace *:    task_active_pid_ns(current),	\
+		struct mnt_namespace *:    current->nsproxy->mnt_ns,	\
+		struct time_namespace *:   current->nsproxy->time_ns,	\
+		struct user_namespace *:   current_user_ns(),		\
+		struct uts_namespace *:    current->nsproxy->uts_ns)
+
+#define current_in_namespace(__ns) (__current_namespace_from_type(__ns) == __ns)
 
+#endif /* _LINUX_NSFS_H */
Signature · 2 lines
-- 
2.47.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#60 lore raw

PATCH v2 28/33 nsfs: support file handles

In reply to Christian Brauner

A while ago we added support for file handles to pidfs so pidfds can be encoded and decoded as file handles. Userspace has adopted this quickly and it's proven very useful. Implement file handles for namespaces as well.

A process is not always able to open /proc/self/ns/. That requires procfs to be mounted and for /proc/self/ or /proc/self/ns/ to not be overmounted. However, userspace can always derive a namespace fd from a pidfd. And that always works for a task's own namespace.

There's no need to introduce unnecessary behavioral differences between /proc/self/ns/ fds, pidfd-derived namespace fds, and file-handle-derived namespace fds. So namespace file handles are always decodable if the caller is located in the namespace the file handle refers to.

This also allows a task to e.g., store a set of file handles to its namespaces in a file on-disk so it can verify when it gets rexeced that they're still valid and so on. This is akin to the pidfd use-case.

Or just plainly for namespace comparison reasons where a file handle to the task's own namespace can be easily compared against others.

---
 fs/nsfs.c                 | 158 ++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/exportfs.h  |   6 ++
 include/uapi/linux/nsfs.h |   9 +++
 3 files changed, 173 insertions(+)
fs/nsfs.c +158 -0
index 80e631aeb3ce..926e2680414e 100644
@@ -13,6 +13,12 @@
 #include <linux/nsfs.h>
 #include <linux/uaccess.h>
 #include <linux/mnt_namespace.h>
+#include <linux/ipc_namespace.h>
+#include <linux/time_namespace.h>
+#include <linux/utsname.h>
+#include <linux/exportfs.h>
+#include <linux/nstree.h>
+#include <net/net_namespace.h>
 
 #include "mount.h"
 #include "internal.h"
@@ -417,12 +423,164 @@ static const struct stashed_operations nsfs_stashed_ops = {
 	.put_data = nsfs_put_data,
 };
 
+#define NSFS_FID_SIZE_U32_VER0 (NSFS_FILE_HANDLE_SIZE_VER0 / sizeof(u32))
+#define NSFS_FID_SIZE_U32_LATEST (NSFS_FILE_HANDLE_SIZE_LATEST / sizeof(u32))
+
+static int nsfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
+			  struct inode *parent)
+{
+	struct nsfs_file_handle *fid = (struct nsfs_file_handle *)fh;
+	struct ns_common *ns = inode->i_private;
+	int len = *max_len;
+
+	if (parent)
+		return FILEID_INVALID;
+
+	if (len < NSFS_FID_SIZE_U32_VER0) {
+		*max_len = NSFS_FID_SIZE_U32_LATEST;
+		return FILEID_INVALID;
+	} else if (len > NSFS_FID_SIZE_U32_LATEST) {
+		*max_len = NSFS_FID_SIZE_U32_LATEST;
+	}
+
+	fid->ns_id	= ns->ns_id;
+	fid->ns_type	= ns->ops->type;
+	fid->ns_inum	= inode->i_ino;
+	return FILEID_NSFS;
+}
+
+static struct dentry *nsfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
+					int fh_len, int fh_type)
+{
+	struct path path __free(path_put) = {};
+	struct nsfs_file_handle *fid = (struct nsfs_file_handle *)fh;
+	struct user_namespace *owning_ns = NULL;
+	struct ns_common *ns;
+	int ret;
+
+	if (fh_len < NSFS_FID_SIZE_U32_VER0)
+		return NULL;
+
+	/* Check that any trailing bytes are zero. */
+	if ((fh_len > NSFS_FID_SIZE_U32_LATEST) &&
+	    memchr_inv((void *)fid + NSFS_FID_SIZE_U32_LATEST, 0,
+		       fh_len - NSFS_FID_SIZE_U32_LATEST))
+		return NULL;
+
+	switch (fh_type) {
+	case FILEID_NSFS:
+		break;
+	default:
+		return NULL;
+	}
+
+	scoped_guard(rcu) {
+		ns = ns_tree_lookup_rcu(fid->ns_id, fid->ns_type);
+		if (!ns)
+			return NULL;
+
+		VFS_WARN_ON_ONCE(ns->ns_id != fid->ns_id);
+		VFS_WARN_ON_ONCE(ns->ops->type != fid->ns_type);
+		VFS_WARN_ON_ONCE(ns->inum != fid->ns_inum);
+
+		if (!refcount_inc_not_zero(&ns->count))
+			return NULL;
+	}
+
+	switch (ns->ops->type) {
+#ifdef CONFIG_CGROUPS
+	case CLONE_NEWCGROUP:
+		if (!current_in_namespace(to_cg_ns(ns)))
+			owning_ns = to_cg_ns(ns)->user_ns;
+		break;
+#endif
+#ifdef CONFIG_IPC_NS
+	case CLONE_NEWIPC:
+		if (!current_in_namespace(to_ipc_ns(ns)))
+			owning_ns = to_ipc_ns(ns)->user_ns;
+		break;
+#endif
+	case CLONE_NEWNS:
+		if (!current_in_namespace(to_mnt_ns(ns)))
+			owning_ns = to_mnt_ns(ns)->user_ns;
+		break;
+#ifdef CONFIG_NET_NS
+	case CLONE_NEWNET:
+		if (!current_in_namespace(to_net_ns(ns)))
+			owning_ns = to_net_ns(ns)->user_ns;
+		break;
+#endif
+#ifdef CONFIG_PID_NS
+	case CLONE_NEWPID:
+		if (!current_in_namespace(to_pid_ns(ns))) {
+			owning_ns = to_pid_ns(ns)->user_ns;
+		} else if (!READ_ONCE(to_pid_ns(ns)->child_reaper)) {
+			ns->ops->put(ns);
+			return ERR_PTR(-EPERM);
+		}
+		break;
+#endif
+#ifdef CONFIG_TIME_NS
+	case CLONE_NEWTIME:
+		if (!current_in_namespace(to_time_ns(ns)))
+			owning_ns = to_time_ns(ns)->user_ns;
+		break;
+#endif
+#ifdef CONFIG_USER_NS
+	case CLONE_NEWUSER:
+		if (!current_in_namespace(to_user_ns(ns)))
+			owning_ns = to_user_ns(ns);
+		break;
+#endif
+#ifdef CONFIG_UTS_NS
+	case CLONE_NEWUTS:
+		if (!current_in_namespace(to_uts_ns(ns)))
+			owning_ns = to_uts_ns(ns)->user_ns;
+		break;
+#endif
+	default:
+		return ERR_PTR(-EOPNOTSUPP);
+	}
+
+	if (owning_ns && !ns_capable(owning_ns, CAP_SYS_ADMIN)) {
+		ns->ops->put(ns);
+		return ERR_PTR(-EPERM);
+	}
+
+	/* path_from_stashed() unconditionally consumes the reference. */
+	ret = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return no_free_ptr(path.dentry);
+}
+
+static int nsfs_export_permission(struct handle_to_path_ctx *ctx,
+				   unsigned int oflags)
+{
+	/* nsfs_fh_to_dentry() performs all permission checks. */
+	return 0;
+}
+
+static struct file *nsfs_export_open(struct path *path, unsigned int oflags)
+{
+	return file_open_root(path, "", oflags, 0);
+}
+
+static const struct export_operations nsfs_export_operations = {
+	.encode_fh	= nsfs_encode_fh,
+	.fh_to_dentry	= nsfs_fh_to_dentry,
+	.open		= nsfs_export_open,
+	.permission	= nsfs_export_permission,
+};
+
 static int nsfs_init_fs_context(struct fs_context *fc)
 {
 	struct pseudo_fs_context *ctx = init_pseudo(fc, NSFS_MAGIC);
 	if (!ctx)
 		return -ENOMEM;
 	ctx->ops = &nsfs_ops;
+	ctx->eops = &nsfs_export_operations;
 	ctx->dops = &ns_dentry_operations;
 	fc->s_fs_info = (void *)&nsfs_stashed_ops;
 	return 0;
include/linux/exportfs.h +6 -0
index cfb0dd1ea49c..3aac58a520c7 100644
@@ -122,6 +122,12 @@ enum fid_type {
 	FILEID_BCACHEFS_WITHOUT_PARENT = 0xb1,
 	FILEID_BCACHEFS_WITH_PARENT = 0xb2,
 
+	/*
+	 *
+	 * 64 bit namespace identifier, 32 bit namespace type, 32 bit inode number.
+	 */
+	FILEID_NSFS = 0xf1,
+
 	/*
 	 * 64 bit unique kernfs id
 	 */
include/uapi/linux/nsfs.h +9 -0
index 97d8d80d139f..fa86fe3c8bd3 100644
@@ -53,4 +53,13 @@ enum init_ns_ino {
 	MNT_NS_INIT_INO		= 0xEFFFFFF8U,
 };
 
+struct nsfs_file_handle {
+	__u64 ns_id;
+	__u32 ns_type;
+	__u32 ns_inum;
+};
+
+#define NSFS_FILE_HANDLE_SIZE_VER0 16 /* sizeof first published struct */
+#define NSFS_FILE_HANDLE_SIZE_LATEST sizeof(struct nsfs_file_handle) /* sizeof latest published struct */
+
 #endif /* __LINUX_NSFS_H */
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 28/33
#61 lore raw

Re: [PATCH v2 28/33] nsfs: support file handles

Quoted 24 lines · Christian Brauner

A while ago we added support for file handles to pidfs so pidfds can be encoded and decoded as file handles. Userspace has adopted this quickly and it's proven very useful. Implement file handles for namespaces as well.

A process is not always able to open /proc/self/ns/. That requires procfs to be mounted and for /proc/self/ or /proc/self/ns/ to not be overmounted. However, userspace can always derive a namespace fd from a pidfd. And that always works for a task's own namespace.

There's no need to introduce unnecessary behavioral differences between /proc/self/ns/ fds, pidfd-derived namespace fds, and file-handle-derived namespace fds. So namespace file handles are always decodable if the caller is located in the namespace the file handle refers to.

This also allows a task to e.g., store a set of file handles to its namespaces in a file on-disk so it can verify when it gets rexeced that they're still valid and so on. This is akin to the pidfd use-case.

Or just plainly for namespace comparison reasons where a file handle to the task's own namespace can be easily compared against others.

...

Quoted 41 lines
fs/nsfs.c +41 -0
+	switch (ns->ops->type) {
+#ifdef CONFIG_CGROUPS
+	case CLONE_NEWCGROUP:
+		if (!current_in_namespace(to_cg_ns(ns)))
+			owning_ns = to_cg_ns(ns)->user_ns;
+		break;
+#endif
+#ifdef CONFIG_IPC_NS
+	case CLONE_NEWIPC:
+		if (!current_in_namespace(to_ipc_ns(ns)))
+			owning_ns = to_ipc_ns(ns)->user_ns;
+		break;
+#endif
+	case CLONE_NEWNS:
+		if (!current_in_namespace(to_mnt_ns(ns)))
+			owning_ns = to_mnt_ns(ns)->user_ns;
+		break;
+#ifdef CONFIG_NET_NS
+	case CLONE_NEWNET:
+		if (!current_in_namespace(to_net_ns(ns)))
+			owning_ns = to_net_ns(ns)->user_ns;
+		break;
+#endif
+#ifdef CONFIG_PID_NS
+	case CLONE_NEWPID:
+		if (!current_in_namespace(to_pid_ns(ns))) {
+			owning_ns = to_pid_ns(ns)->user_ns;
+		} else if (!READ_ONCE(to_pid_ns(ns)->child_reaper)) {
+			ns->ops->put(ns);
+			return ERR_PTR(-EPERM);
+		}
+		break;
+#endif
+#ifdef CONFIG_TIME_NS
+	case CLONE_NEWTIME:
+		if (!current_in_namespace(to_time_ns(ns)))
+			owning_ns = to_time_ns(ns)->user_ns;
+		break;
+#endif
+#ifdef CONFIG_USER_NS
+	case CLONE_NEWUSER:

fs/nsfs.c static const struct stashed_operations nsfs_stashed_ops = { · line 540

Quoted 10 lines
fs/nsfs.c +10 -0
+		if (!current_in_namespace(to_user_ns(ns)))
+			owning_ns = to_user_ns(ns);
+		break;
+#endif
+#ifdef CONFIG_UTS_NS
+	case CLONE_NEWUTS:
+		if (!current_in_namespace(to_uts_ns(ns)))
+			owning_ns = to_uts_ns(ns)->user_ns;
+		break;
+#endif

Frankly, switches like these are asking for more Generic usage ;) But ok for now.

Quoted 5 lines
fs/nsfs.c +5 -0
+	default:
+		return ERR_PTR(-EOPNOTSUPP);
+	}
+
+	if (owning_ns && !ns_capable(owning_ns, CAP_SYS_ADMIN)) {

fs/nsfs.c static const struct stashed_operations nsfs_stashed_ops = { · line 555

Quoted 10 lines
fs/nsfs.c +10 -0
+		ns->ops->put(ns);
+		return ERR_PTR(-EPERM);
+	}
+
+	/* path_from_stashed() unconditionally consumes the reference. */
+	ret = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return no_free_ptr(path.dentry);

Ugh, so IMO this is very subtle because we declare

struct path path __free(path_put)

but then do no_free_ptr(path.dentry). I really had to lookup implementation of no_free_ptr() to check whether we are leaking mnt reference here or not (we are not). But that seems as an implementation detail we shouldn't better rely on? Wouldn't be:

	return dget(path.dentry);

much clearer (and sligthly less efficient, I know, but who cares)?

Otherwise looks good to me so feel free to add:

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#62 lore raw

Re: [PATCH v2 28/33] nsfs: support file handles

Quoted 103 lines · Jan Kara
Quoted 24 lines · Christian Brauner

A while ago we added support for file handles to pidfs so pidfds can be encoded and decoded as file handles. Userspace has adopted this quickly and it's proven very useful. Implement file handles for namespaces as well.

A process is not always able to open /proc/self/ns/. That requires procfs to be mounted and for /proc/self/ or /proc/self/ns/ to not be overmounted. However, userspace can always derive a namespace fd from a pidfd. And that always works for a task's own namespace.

There's no need to introduce unnecessary behavioral differences between /proc/self/ns/ fds, pidfd-derived namespace fds, and file-handle-derived namespace fds. So namespace file handles are always decodable if the caller is located in the namespace the file handle refers to.

This also allows a task to e.g., store a set of file handles to its namespaces in a file on-disk so it can verify when it gets rexeced that they're still valid and so on. This is akin to the pidfd use-case.

Or just plainly for namespace comparison reasons where a file handle to the task's own namespace can be easily compared against others.

...

Quoted 41 lines
fs/nsfs.c +41 -0
+	switch (ns->ops->type) {
+#ifdef CONFIG_CGROUPS
+	case CLONE_NEWCGROUP:
+		if (!current_in_namespace(to_cg_ns(ns)))
+			owning_ns = to_cg_ns(ns)->user_ns;
+		break;
+#endif
+#ifdef CONFIG_IPC_NS
+	case CLONE_NEWIPC:
+		if (!current_in_namespace(to_ipc_ns(ns)))
+			owning_ns = to_ipc_ns(ns)->user_ns;
+		break;
+#endif
+	case CLONE_NEWNS:
+		if (!current_in_namespace(to_mnt_ns(ns)))
+			owning_ns = to_mnt_ns(ns)->user_ns;
+		break;
+#ifdef CONFIG_NET_NS
+	case CLONE_NEWNET:
+		if (!current_in_namespace(to_net_ns(ns)))
+			owning_ns = to_net_ns(ns)->user_ns;
+		break;
+#endif
+#ifdef CONFIG_PID_NS
+	case CLONE_NEWPID:
+		if (!current_in_namespace(to_pid_ns(ns))) {
+			owning_ns = to_pid_ns(ns)->user_ns;
+		} else if (!READ_ONCE(to_pid_ns(ns)->child_reaper)) {
+			ns->ops->put(ns);
+			return ERR_PTR(-EPERM);
+		}
+		break;
+#endif
+#ifdef CONFIG_TIME_NS
+	case CLONE_NEWTIME:
+		if (!current_in_namespace(to_time_ns(ns)))
+			owning_ns = to_time_ns(ns)->user_ns;
+		break;
+#endif
+#ifdef CONFIG_USER_NS
+	case CLONE_NEWUSER:

fs/nsfs.c static const struct stashed_operations nsfs_stashed_ops = { · line 540

Quoted 10 lines
fs/nsfs.c +10 -0
+		if (!current_in_namespace(to_user_ns(ns)))
+			owning_ns = to_user_ns(ns);
+		break;
+#endif
+#ifdef CONFIG_UTS_NS
+	case CLONE_NEWUTS:
+		if (!current_in_namespace(to_uts_ns(ns)))
+			owning_ns = to_uts_ns(ns)->user_ns;
+		break;
+#endif

Frankly, switches like these are asking for more Generic usage ;) But ok for now.

Quoted 5 lines
fs/nsfs.c +5 -0
+	default:
+		return ERR_PTR(-EOPNOTSUPP);
+	}
+
+	if (owning_ns && !ns_capable(owning_ns, CAP_SYS_ADMIN)) {

fs/nsfs.c static const struct stashed_operations nsfs_stashed_ops = { · line 555

Quoted 10 lines
fs/nsfs.c +10 -0
+		ns->ops->put(ns);
+		return ERR_PTR(-EPERM);
+	}
+
+	/* path_from_stashed() unconditionally consumes the reference. */
+	ret = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return no_free_ptr(path.dentry);

Ugh, so IMO this is very subtle because we declare

struct path path __free(path_put)

Quoted 8 lines

but then do no_free_ptr(path.dentry). I really had to lookup implementation of no_free_ptr() to check whether we are leaking mnt reference here or not (we are not). But that seems as an implementation detail we shouldn't better rely on? Wouldn't be:

	return dget(path.dentry);

much clearer (and sligthly less efficient, I know, but who cares)?

Fine by me as well!

author maintainer
#63 lore raw

PATCH v2 29/33 nsfs: support exhaustive file handles

In reply to Christian Brauner

Pidfd file handles are exhaustive meaning they don't require a handle on another pidfd to pass to open_by_handle_at() so it can derive the filesystem to decode in. Instead it can be derived from the file handle itself. The same is possible for namespace file handles.

---
 fs/fhandle.c               |  6 ++++++
 fs/internal.h              |  1 +
 fs/nsfs.c                  | 10 ++++++++++
 include/uapi/linux/fcntl.h |  1 +
 4 files changed, 18 insertions(+)
fs/fhandle.c +6 -0
index 7c236f64cdea..f18c855bb0c2 100644
@@ -11,6 +11,7 @@
 #include <linux/personality.h>
 #include <linux/uaccess.h>
 #include <linux/compat.h>
+#include <linux/nsfs.h>
 #include "internal.h"
 #include "mount.h"
 
@@ -189,6 +190,11 @@ static int get_path_anchor(int fd, struct path *root)
 		return 0;
 	}
 
+	if (fd == FD_NSFS_ROOT) {
+		nsfs_get_root(root);
+		return 0;
+	}
+
 	return -EBADF;
 }
 
fs/internal.h +1 -0
index 38e8aab27bbd..a33d18ee5b74 100644
@@ -355,3 +355,4 @@ int anon_inode_getattr(struct mnt_idmap *idmap, const struct path *path,
 int anon_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 		       struct iattr *attr);
 void pidfs_get_root(struct path *path);
+void nsfs_get_root(struct path *path);
fs/nsfs.c +10 -0
index 926e2680414e..22765fcab18e 100644
@@ -25,6 +25,14 @@
 
 static struct vfsmount *nsfs_mnt;
 
+static struct path nsfs_root_path = {};
+
+void nsfs_get_root(struct path *path)
+{
+	*path = nsfs_root_path;
+	path_get(path);
+}
+
 static long ns_ioctl(struct file *filp, unsigned int ioctl,
 			unsigned long arg);
 static const struct file_operations ns_file_operations = {
@@ -598,4 +606,6 @@ void __init nsfs_init(void)
 	if (IS_ERR(nsfs_mnt))
 		panic("can't set nsfs up\n");
 	nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
+	nsfs_root_path.mnt = nsfs_mnt;
+	nsfs_root_path.dentry = nsfs_mnt->mnt_root;
 }
include/uapi/linux/fcntl.h +1 -0
index f291ab4f94eb..3741ea1b73d8 100644
@@ -111,6 +111,7 @@
 #define PIDFD_SELF_THREAD_GROUP		-10001 /* Current thread group leader. */
 
 #define FD_PIDFS_ROOT			-10002 /* Root of the pidfs filesystem */
+#define FD_NSFS_ROOT			-10003 /* Root of the nsfs filesystem */
 #define FD_INVALID			-10009 /* Invalid file descriptor: -10000 - EBADF = -10009 */
 
 /* Generic flags for the *at(2) family of syscalls. */
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 29/33
#64 lore raw

Re: [PATCH v2 29/33] nsfs: support exhaustive file handles

Quoted 7 lines · Christian Brauner

Pidfd file handles are exhaustive meaning they don't require a handle on another pidfd to pass to open_by_handle_at() so it can derive the filesystem to decode in. Instead it can be derived from the file handle itself. The same is possible for namespace file handles.

Looks good. Feel free to add:

Honza

Quoted 82 lines
---
 fs/fhandle.c               |  6 ++++++
 fs/internal.h              |  1 +
 fs/nsfs.c                  | 10 ++++++++++
 include/uapi/linux/fcntl.h |  1 +
 4 files changed, 18 insertions(+)
fs/fhandle.c +6 -0
index 7c236f64cdea..f18c855bb0c2 100644
@@ -11,6 +11,7 @@
 #include <linux/personality.h>
 #include <linux/uaccess.h>
 #include <linux/compat.h>
+#include <linux/nsfs.h>
 #include "internal.h"
 #include "mount.h"
 
@@ -189,6 +190,11 @@ static int get_path_anchor(int fd, struct path *root)
 		return 0;
 	}
 
+	if (fd == FD_NSFS_ROOT) {
+		nsfs_get_root(root);
+		return 0;
+	}
+
 	return -EBADF;
 }
 
fs/internal.h +1 -0
index 38e8aab27bbd..a33d18ee5b74 100644
@@ -355,3 +355,4 @@ int anon_inode_getattr(struct mnt_idmap *idmap, const struct path *path,
 int anon_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 		       struct iattr *attr);
 void pidfs_get_root(struct path *path);
+void nsfs_get_root(struct path *path);
fs/nsfs.c +10 -0
index 926e2680414e..22765fcab18e 100644
@@ -25,6 +25,14 @@
 
 static struct vfsmount *nsfs_mnt;
 
+static struct path nsfs_root_path = {};
+
+void nsfs_get_root(struct path *path)
+{
+	*path = nsfs_root_path;
+	path_get(path);
+}
+
 static long ns_ioctl(struct file *filp, unsigned int ioctl,
 			unsigned long arg);
 static const struct file_operations ns_file_operations = {
@@ -598,4 +606,6 @@ void __init nsfs_init(void)
 	if (IS_ERR(nsfs_mnt))
 		panic("can't set nsfs up\n");
 	nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
+	nsfs_root_path.mnt = nsfs_mnt;
+	nsfs_root_path.dentry = nsfs_mnt->mnt_root;
 }
include/uapi/linux/fcntl.h +1 -0
index f291ab4f94eb..3741ea1b73d8 100644
@@ -111,6 +111,7 @@
 #define PIDFD_SELF_THREAD_GROUP		-10001 /* Current thread group leader. */
 
 #define FD_PIDFS_ROOT			-10002 /* Root of the pidfs filesystem */
+#define FD_NSFS_ROOT			-10003 /* Root of the nsfs filesystem */
 #define FD_INVALID			-10009 /* Invalid file descriptor: -10000 - EBADF = -10009 */
 
 /* Generic flags for the *at(2) family of syscalls. */
Signature · 2 lines
-- 
2.47.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#65 lore raw

PATCH v2 30/33 nsfs: add missing id retrieval support

In reply to Christian Brauner

The mount namespace has supported id retrieval for a while already. Add support for the other types as well.

---
 fs/nsfs.c                 | 25 +++++++++++++------------
 include/uapi/linux/nsfs.h |  6 ++++--
 2 files changed, 17 insertions(+), 14 deletions(-)
fs/nsfs.c +13 -12
index 22765fcab18e..8484bc4dd3de 100644
@@ -177,6 +177,7 @@ static bool nsfs_ioctl_valid(unsigned int cmd)
 	case NS_GET_TGID_FROM_PIDNS:
 	case NS_GET_PID_IN_PIDNS:
 	case NS_GET_TGID_IN_PIDNS:
+	case NS_GET_ID:
 		return true;
 	}
 
@@ -226,18 +227,6 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 		argp = (uid_t __user *) arg;
 		uid = from_kuid_munged(current_user_ns(), user_ns->owner);
 		return put_user(uid, argp);
-	case NS_GET_MNTNS_ID: {
-		__u64 __user *idp;
-		__u64 id;
-
-		if (ns->ops->type != CLONE_NEWNS)
-			return -EINVAL;
-
-		mnt_ns = container_of(ns, struct mnt_namespace, ns);
-		idp = (__u64 __user *)arg;
-		id = mnt_ns->ns.ns_id;
-		return put_user(id, idp);
-	}
 	case NS_GET_PID_FROM_PIDNS:
 		fallthrough;
 	case NS_GET_TGID_FROM_PIDNS:
@@ -283,6 +272,18 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 			ret = -ESRCH;
 		return ret;
 	}
+	case NS_GET_MNTNS_ID:
+		if (ns->ops->type != CLONE_NEWNS)
+			return -EINVAL;
+		fallthrough;
+	case NS_GET_ID: {
+		__u64 __user *idp;
+		__u64 id;
+
+		idp = (__u64 __user *)arg;
+		id = ns->ns_id;
+		return put_user(id, idp);
+	}
 	}
 
 	/* extensible ioctls */
include/uapi/linux/nsfs.h +4 -2
index fa86fe3c8bd3..5d5bf22464c9 100644
@@ -16,8 +16,6 @@
 #define NS_GET_NSTYPE		_IO(NSIO, 0x3)
 /* Get owner UID (in the caller's user namespace) for a user namespace */
 #define NS_GET_OWNER_UID	_IO(NSIO, 0x4)
-/* Get the id for a mount namespace */
-#define NS_GET_MNTNS_ID		_IOR(NSIO, 0x5, __u64)
 /* Translate pid from target pid namespace into the caller's pid namespace. */
 #define NS_GET_PID_FROM_PIDNS	_IOR(NSIO, 0x6, int)
 /* Return thread-group leader id of pid in the callers pid namespace. */
@@ -42,6 +40,10 @@ struct mnt_ns_info {
 /* Get previous namespace. */
 #define NS_MNT_GET_PREV		_IOR(NSIO, 12, struct mnt_ns_info)
 
+/* Retrieve namespace identifiers. */
+#define NS_GET_MNTNS_ID		_IOR(NSIO, 5,  __u64)
+#define NS_GET_ID		_IOR(NSIO, 13, __u64)
+
 enum init_ns_ino {
 	IPC_NS_INIT_INO		= 0xEFFFFFFFU,
 	UTS_NS_INIT_INO		= 0xEFFFFFFEU,
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 30/33
#66 lore raw

Re: [PATCH v2 30/33] nsfs: add missing id retrieval support

Quoted 4 lines · Christian Brauner

The mount namespace has supported id retrieval for a while already. Add support for the other types as well.

Looks good. Feel free to add:

Honza

Quoted 83 lines
---
 fs/nsfs.c                 | 25 +++++++++++++------------
 include/uapi/linux/nsfs.h |  6 ++++--
 2 files changed, 17 insertions(+), 14 deletions(-)
fs/nsfs.c +13 -12
index 22765fcab18e..8484bc4dd3de 100644
@@ -177,6 +177,7 @@ static bool nsfs_ioctl_valid(unsigned int cmd)
 	case NS_GET_TGID_FROM_PIDNS:
 	case NS_GET_PID_IN_PIDNS:
 	case NS_GET_TGID_IN_PIDNS:
+	case NS_GET_ID:
 		return true;
 	}
 
@@ -226,18 +227,6 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 		argp = (uid_t __user *) arg;
 		uid = from_kuid_munged(current_user_ns(), user_ns->owner);
 		return put_user(uid, argp);
-	case NS_GET_MNTNS_ID: {
-		__u64 __user *idp;
-		__u64 id;
-
-		if (ns->ops->type != CLONE_NEWNS)
-			return -EINVAL;
-
-		mnt_ns = container_of(ns, struct mnt_namespace, ns);
-		idp = (__u64 __user *)arg;
-		id = mnt_ns->ns.ns_id;
-		return put_user(id, idp);
-	}
 	case NS_GET_PID_FROM_PIDNS:
 		fallthrough;
 	case NS_GET_TGID_FROM_PIDNS:
@@ -283,6 +272,18 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 			ret = -ESRCH;
 		return ret;
 	}
+	case NS_GET_MNTNS_ID:
+		if (ns->ops->type != CLONE_NEWNS)
+			return -EINVAL;
+		fallthrough;
+	case NS_GET_ID: {
+		__u64 __user *idp;
+		__u64 id;
+
+		idp = (__u64 __user *)arg;
+		id = ns->ns_id;
+		return put_user(id, idp);
+	}
 	}
 
 	/* extensible ioctls */
include/uapi/linux/nsfs.h +4 -2
index fa86fe3c8bd3..5d5bf22464c9 100644
@@ -16,8 +16,6 @@
 #define NS_GET_NSTYPE		_IO(NSIO, 0x3)
 /* Get owner UID (in the caller's user namespace) for a user namespace */
 #define NS_GET_OWNER_UID	_IO(NSIO, 0x4)
-/* Get the id for a mount namespace */
-#define NS_GET_MNTNS_ID		_IOR(NSIO, 0x5, __u64)
 /* Translate pid from target pid namespace into the caller's pid namespace. */
 #define NS_GET_PID_FROM_PIDNS	_IOR(NSIO, 0x6, int)
 /* Return thread-group leader id of pid in the callers pid namespace. */
@@ -42,6 +40,10 @@ struct mnt_ns_info {
 /* Get previous namespace. */
 #define NS_MNT_GET_PREV		_IOR(NSIO, 12, struct mnt_ns_info)
 
+/* Retrieve namespace identifiers. */
+#define NS_GET_MNTNS_ID		_IOR(NSIO, 5,  __u64)
+#define NS_GET_ID		_IOR(NSIO, 13, __u64)
+
 enum init_ns_ino {
 	IPC_NS_INIT_INO		= 0xEFFFFFFFU,
 	UTS_NS_INIT_INO		= 0xEFFFFFFEU,
Signature · 2 lines
-- 
2.47.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#67 lore raw

PATCH v2 31/33 tools: update nsfs.h uapi header

In reply to Christian Brauner

Update the nsfs.h tools header to the uapi/nsfs.h header so we can rely on it in the selftests.

---
 tools/include/uapi/linux/nsfs.h | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
tools/include/uapi/linux/nsfs.h +15 -2
index 34127653fd00..33c9b578b3b2 100644
@@ -16,8 +16,6 @@
 #define NS_GET_NSTYPE		_IO(NSIO, 0x3)
 /* Get owner UID (in the caller's user namespace) for a user namespace */
 #define NS_GET_OWNER_UID	_IO(NSIO, 0x4)
-/* Get the id for a mount namespace */
-#define NS_GET_MNTNS_ID		_IOR(NSIO, 0x5, __u64)
 /* Translate pid from target pid namespace into the caller's pid namespace. */
 #define NS_GET_PID_FROM_PIDNS	_IOR(NSIO, 0x6, int)
 /* Return thread-group leader id of pid in the callers pid namespace. */
@@ -42,4 +40,19 @@ struct mnt_ns_info {
 /* Get previous namespace. */
 #define NS_MNT_GET_PREV		_IOR(NSIO, 12, struct mnt_ns_info)
 
+/* Retrieve namespace identifiers. */
+#define NS_GET_MNTNS_ID		_IOR(NSIO, 5,  __u64)
+#define NS_GET_ID		_IOR(NSIO, 13, __u64)
+
+enum init_ns_ino {
+	IPC_NS_INIT_INO		= 0xEFFFFFFFU,
+	UTS_NS_INIT_INO		= 0xEFFFFFFEU,
+	USER_NS_INIT_INO	= 0xEFFFFFFDU,
+	PID_NS_INIT_INO		= 0xEFFFFFFCU,
+	CGROUP_NS_INIT_INO	= 0xEFFFFFFBU,
+	TIME_NS_INIT_INO	= 0xEFFFFFFAU,
+	NET_NS_INIT_INO		= 0xEFFFFFF9U,
+	MNT_NS_INIT_INO		= 0xEFFFFFF8U,
+};
+
 #endif /* __LINUX_NSFS_H */
Signature · 1 line
2.47.3
author maintainer
#68 lore raw

PATCH v2 32/33 selftests/namespaces: add identifier selftests

In reply to Christian Brauner

Add a bunch of selftests for the identifier retrieval ioctls.

---
 tools/testing/selftests/namespaces/.gitignore  |   1 +
 tools/testing/selftests/namespaces/Makefile    |   7 +
 tools/testing/selftests/namespaces/config      |   7 +
 tools/testing/selftests/namespaces/nsid_test.c | 986 +++++++++++++++++++++++++
 4 files changed, 1001 insertions(+)
tools/testing/selftests/namespaces/.gitignore new file +1 -0
new file mode 100644
index 000000000000..c1e8d634dd21
@@ -0,0 +1 @@
+nsid_test
tools/testing/selftests/namespaces/Makefile new file +7 -0
new file mode 100644
index 000000000000..9280c703533e
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-only
+CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
+
+TEST_GEN_PROGS := nsid_test
+
+include ../lib.mk
+
tools/testing/selftests/namespaces/config new file +7 -0
new file mode 100644
index 000000000000..d09836260262
@@ -0,0 +1,7 @@
+CONFIG_UTS_NS=y
+CONFIG_TIME_NS=y
+CONFIG_IPC_NS=y
+CONFIG_USER_NS=y
+CONFIG_PID_NS=y
+CONFIG_NET_NS=y
+CONFIG_CGROUPS=y
tools/testing/selftests/namespaces/nsid_test.c new file +986 -0

986 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#69 lore raw

PATCH v2 33/33 selftests/namespaces: add file handle selftests

In reply to Christian Brauner

Add a bunch of selftests for namespace file handles.

---
 tools/testing/selftests/namespaces/.gitignore      |    1 +
 tools/testing/selftests/namespaces/Makefile        |    2 +-
 .../selftests/namespaces/file_handle_test.c        | 1429 ++++++++++++++++++++
 3 files changed, 1431 insertions(+), 1 deletion(-)
tools/testing/selftests/namespaces/.gitignore +1 -0
index c1e8d634dd21..7639dbf58bbf 100644
@@ -1 +1,2 @@
 nsid_test
+file_handle_test
tools/testing/selftests/namespaces/Makefile +1 -1
index 9280c703533e..f6c117ce2c2b 100644
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
 
-TEST_GEN_PROGS := nsid_test
+TEST_GEN_PROGS := nsid_test file_handle_test
 
 include ../lib.mk
 
tools/testing/selftests/namespaces/file_handle_test.c new file +1429 -0

1,429 lines not on this page · raw message

Signature · 1 line
2.47.3
maintainer
#70 lore raw
In reply to Christian Brauner

On Fri, Sep 12, 2025 at 01:52:23PM +0200, Christian Brauner wrote:

A nit on whatever script you are using:

base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585

is less convenient than

base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585 (v6.17-rc1)