Skip to contentSkip to conversation

namespace: rework connected mounts

PATCH In review Christian Brauner 3 participants 13 messages Oct 2 – Oct 21

Subsystems (3)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

cover namespace: rework connected mounts

This is a simplified version and doesn't require vacating existing mounts and doesn't require such heavy machinery.

UMOUNT_CONNECTED as implemented allows for the creation of reference count cycles. Here's a simple example

  mkdir /x; mkfifo /ready /go
  unshare -m sh -c 'mount -t tmpfs tmpfs /x
                    truncate -s 8M /x/img; mkfs.ext4 -q /x/img
                    dev=$(losetup -f --show /x/img)
                    mkdir /x/mp; mount $dev /x/mp
                    echo $dev > /ready; read r < /go' &
  read dev < /ready
  rmdir /x
  echo > /go
  wait
  losetup -d $dev
  losetup -a

Take a directory /x on the host, create a new mount namespace, mount a tmpfs on /x, use a file on that tmpfs as the backing file for a loop device, mount that loop device on that tmpfs. Now rmdir /x on the host. This will lazily unmount the mount on top of /x in the container with UMOUNT_CONNECTED. Once the namespace exits nothing references the mount anymore. Now the tmpfs is pinned by the backing file of the loop device and the loop mount is owned by the tmpfs superblock.

Fun fact, such cycles can be formed by at least the following subsystems and I have added reproducers for all of them:

(1) a loop mount P from an image on a tmpfs next to it, so that P's death shows as the loop device giving up its backing file

(2) autofs with a FIFO on P as its pipe, zram with a device node on P as its writeback device, both on a minix image since vfat has neither

(3) ecryptfs with its lower directory on P, under a passphrase token added to the session keyring

(4) binfmt_misc in a new user namespace with an 'F' interpreter on P

(5) a fuse server that answers FUSE_INIT with passthrough on and registers a file on P as a backing file

(6) zloop with its zone files in a directory on P

(7) a mass storage gadget on the dummy UDC with its LUN file on P, mounted from the SCSI disk the gadget shows up as

(8) md with a RAID1 of one loop device and its bitmap file on P, which skips while SET_BITMAP_FILE has no way to succeed

(9) rmdir of P's mountpoint from the parent, then the child exits, then the device must be free and LOOP_CLR_FD must release the file

The underlying mechanism is UMOUNT_CONNECTED (MNT_LOCKED falls into the same class). With UMOUNT_CONNECTED an unmounted mount stays attached to its parent. This is used to protect revealing the underlying mount and is a non-negotiable security mechanism. So now the parent owns that mount and is put on the parent's final mntput(). That moves it to mnt_stuck_children and ultimately it's cleaned up by cleanup_mnt().

The fact that ownership of the child mount gets transferred to the parent turns every reference from a child's superblock back to one of its ancestors into a cycle.

Don't keep the child attached at all. What the parent needs is that a lookup at the child's mountpoint keeps finding some mount, not the child itself and it's not a guarantee we have given really.

When an unmounted mount would have stayed attached to its unmounted parent disconnect it like every other unmounted mount and leave a marker behind.

A lookup on the parent that misses the mount hash and hits a marker finds knullfs. Either a file or a directory. Nothing leads from a marker to any other mount.

The marker is owned by the parent and dropped by the parent's final mntput() or by __detach_mounts() when the mountpoint is deleted from under it. It is allocated together with the mount.

With that every unmounted mount is a root and holds only its own reference which namespace_unlock() drops. No unmounted mount owns another one. A superblock that pins an ancestor can't form a cycle.

It has a visible change. Mounts left connected (rmdir etc.) used to stay traversable through the parent for as long as something held the parent. Now it is detached with the umount. It lives as long as something references it but it isn't reachable through the parent anymore and ".." inside it leads nowhere which is the same as for every other lazily unmounted mount.

---
Christian Brauner (3):
nullfs: add an empty immutable regular file
namespace: rework connected mounts
selftests/filesystems: test covered mounts

 Documentation/filesystems/propagate_umount.txt     |   12 +-
 fs/mount.h                                         |   11 +-
 fs/namespace.c                                     |  154 +-
 fs/nullfs.c                                        |   46 +
 fs/pnode.c                                         |    4 +-
 .../selftests/filesystems/mount_cycle/.gitignore   |    3 +
 .../selftests/filesystems/mount_cycle/Makefile     |    7 +-
 .../selftests/filesystems/mount_cycle/config       |   39 +
 .../filesystems/mount_cycle/locked_handle_test.c   |  377 +++++
 .../filesystems/mount_cycle/loop_cycle_test.c      | 1542 ++++++++++++++++++++
 .../filesystems/mount_cycle/mount_cover_test.c     |  565 +++++++
 .../selftests/filesystems/mount_cycle/settings     |    1 +
 12 files changed, 2709 insertions(+), 52 deletions(-)

---
base-commit: e7d906b21f721c8ed884cecabdb182626941beb8
change-id: 20261002-work-mount-cover-b102ce0597aa

author maintainer
#1 lore raw

PATCH 1/3 nullfs: add an empty immutable regular file

Add nullfs_new_file() to allocate an empty immutable regular file on a nullfs instance as a dentry of its own. It is never hashed under the root and so can't be found by lookup. Reads return nothing, changes are refused, file locks, leases and delegations are refused as.

---
 fs/mount.h     |  1 +
 fs/namespace.c | 25 +++++++++++++++++++++++++
 fs/nullfs.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+)
fs/mount.h +1 -0
index 4e68e5cbc254..2e29cdbaeb74 100644
@@ -6,6 +6,7 @@
 #include <linux/fs_pin.h>
 
 extern struct file_system_type nullfs_fs_type;
+extern struct dentry *nullfs_new_file(struct super_block *sb);
 extern struct vfsmount *knullfs;
 extern struct list_head notify_list;
 
fs/namespace.c +25 -0
index e1b0ade95b0d..ff21e0440fae 100644
@@ -81,6 +81,7 @@ static struct hlist_head *mount_hashtable __ro_after_init;
 static struct hlist_head *mountpoint_hashtable __ro_after_init;
 static struct kmem_cache *mnt_cache __ro_after_init;
 struct vfsmount *knullfs __ro_after_init;	/* private nullfs instance */
+static struct vfsmount *knullfs_file __ro_after_init;	/* its regular file */
 static DECLARE_RWSEM(namespace_sem);
 static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
 static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
@@ -6330,6 +6331,25 @@ static void __init mount_rootfs_on_nullfs(struct vfsmount *mnt,
 		attach_mnt(real_mount(mnt), mp.parent, mp.mp);
 }
 
+static struct vfsmount *__init knullfs_file_mount(void)
+{
+	struct dentry *file;
+	struct mount *mnt;
+
+	file = nullfs_new_file(knullfs->mnt_sb);
+	if (IS_ERR(file))
+		return ERR_CAST(file);
+	mnt = clone_mnt(real_mount(knullfs), file, CL_PRIVATE);
+	dput(file);
+	if (IS_ERR(mnt))
+		return ERR_CAST(mnt);
+	mnt->mnt_ns = MNT_NS_INTERNAL;
+	mnt->mnt.mnt_flags |= MNT_INTERNAL | MNT_READONLY;
+	/* nothing is ever mounted on it either */
+	dont_mount(mnt->mnt.mnt_root);
+	return &mnt->mnt;
+}
+
 static void __init init_mount_tree(void)
 {
 	struct vfsmount *mnt, *nullfs_mnt;
@@ -6342,6 +6362,8 @@ static void __init init_mount_tree(void)
 	 * (1) nullfs with mount id 1
 	 * (2) mutable rootfs with mount id 2
 	 * (3) private nullfs for kthreads (SB_KERNMOUNT), kept in knullfs
+	 * (4) a second mount of (3) rooted on a regular file, kept in
+	 *     knullfs_file
 	 *
 	 * with (2) mounted on top of (1). The init_task's root and pwd
 	 * are pointed at (3) so all kthreads start isolated in nullfs.
@@ -6383,6 +6405,9 @@ static void __init init_mount_tree(void)
 	dont_mount(knullfs->mnt_root);
 	/* and nothing is ever written through it */
 	knullfs->mnt_flags |= MNT_READONLY;
+	knullfs_file = knullfs_file_mount();
+	if (IS_ERR(knullfs_file))
+		panic("VFS: Failed to create the nullfs file stand-in");
 	root.mnt	= knullfs;
 	root.dentry	= knullfs->mnt_root;
 
fs/nullfs.c +46 -0

52 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/3] nullfs: add an empty immutable regular file

Quoted 4 lines · Christian Brauner

Add nullfs_new_file() to allocate an empty immutable regular file on a nullfs instance as a dentry of its own. It is never hashed under the root and so can't be found by lookup. Reads return nothing, changes are refused, file locks, leases and delegations are refused as.

[...]

Quoted 5 lines
fs/nullfs.c +5 -0
+/* a file of nullfs is permanently empty */
+static ssize_t nullfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
+{
+       return 0;
+}

fs/nullfs.c static const struct file_operations nullfs_dir_operations = { · line 64

Quoted 10 lines
fs/nullfs.c +10 -0
+
+/* an empty regular file, with the same refusals as the directory */
+static const struct file_operations nullfs_file_operations = {
+       .llseek         = generic_file_llseek,
+       .read_iter      = nullfs_file_read_iter,
+       .fsync          = noop_fsync,
+       .lock           = nullfs_nolock,
+       .flock          = nullfs_nolock,
+       .setlease       = nullfs_nolease,
+};

If this is supposed to function as a sort of error sentinel, you could also leave out the handlers for .llseek / .read_iter / .fsync. Maybe even not set ->f_op at all so that it defaults to no_open_fops and open() fails?

author maintainer
#3 lore raw

Re: [PATCH 1/3] nullfs: add an empty immutable regular file

Quoted 18 lines · Jann Horn
Quoted 4 lines · Christian Brauner

Add nullfs_new_file() to allocate an empty immutable regular file on a nullfs instance as a dentry of its own. It is never hashed under the root and so can't be found by lookup. Reads return nothing, changes are refused, file locks, leases and delegations are refused as.

[...]

Quoted 12 lines
fs/nullfs.c +12 -0
+/* a file of nullfs is permanently empty */
+static ssize_t nullfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
+{
+       return 0;
+}
+
+/* an empty regular file, with the same refusals as the directory */
+static const struct file_operations nullfs_file_operations = {
+       .llseek         = generic_file_llseek,
+       .read_iter      = nullfs_file_read_iter,
+       .fsync          = noop_fsync,
+       .lock           = nullfs_nolock,
Quoted 8 lines

fs/nullfs.c static const struct file_operations nullfs_dir_operations = { · line 64

Quoted 3 lines
fs/nullfs.c +3 -0
+       .flock          = nullfs_nolock,
+       .setlease       = nullfs_nolease,
+};

If this is supposed to function as a sort of error sentinel, you could also leave out the handlers for .llseek / .read_iter / .fsync. Maybe even not set ->f_op at all so that it defaults to no_open_fops and open() fails?

The least regression potential is if we model like a sink which is the goal here. But I did have your intuition initially.

author maintainer
#4 lore raw

PATCH 2/3 namespace: rework connected mounts

In reply to Christian Brauner

UMOUNT_CONNECTED as implemented allows for the creation of reference count cycles. Here's a simple example

  mkdir /x; mkfifo /ready /go
  unshare -m sh -c 'mount -t tmpfs tmpfs /x
                    truncate -s 8M /x/img; mkfs.ext4 -q /x/img
                    dev=$(losetup -f --show /x/img)
                    mkdir /x/mp; mount $dev /x/mp
                    echo $dev > /ready; read r < /go' &
  read dev < /ready
  rmdir /x
  echo > /go
  wait
  losetup -d $dev
  losetup -a

Take a directory /x on the host, create a new mount namespace, mount a tmpfs on /x, use a file on that tmpfs as the backing file for a loop device, mount that loop device on that tmpfs. Now rmdir /x on the host. This will lazily unmount the mount on top of /x in the container with UMOUNT_CONNECTED. Once the namespace exits nothing references the mount anymore. Now the tmpfs is pinned by the backing file of the loop device and the loop mount is owned by the tmpfs superblock.

Fun fact, such cycles can be formed by at least the following subsystems and I have added reproducers for all of them:

(1) a loop mount P from an image on a tmpfs next to it, so that P's death shows as the loop device giving up its backing file

(2) autofs with a FIFO on P as its pipe, zram with a device node on P as its writeback device, both on a minix image since vfat has neither

(3) ecryptfs with its lower directory on P, under a passphrase token added to the session keyring

(4) binfmt_misc in a new user namespace with an 'F' interpreter on P

(5) a fuse server that answers FUSE_INIT with passthrough on and registers a file on P as a backing file

(6) zloop with its zone files in a directory on P

(7) a mass storage gadget on the dummy UDC with its LUN file on P, mounted from the SCSI disk the gadget shows up as

(8) md with a RAID1 of one loop device and its bitmap file on P, which skips while SET_BITMAP_FILE has no way to succeed

(9) rmdir of P's mountpoint from the parent, then the child exits, then the device must be free and LOOP_CLR_FD must release the file

The underlying mechanism is UMOUNT_CONNECTED (MNT_LOCKED falls into the same class). With UMOUNT_CONNECTED an unmounted mount stays attached to its parent. This is used to protect revealing the underlying mount and is a non-negotiable security mechanism. So now the parent owns that mount and is put on the parent's final mntput(). That moves it to mnt_stuck_children and ultimately it's cleaned up by cleanup_mnt().

The fact that ownership of the child mount gets transferred to the parent turns every reference from a child's superblock back to one of its ancestors into a cycle.

Don't keep the child attached at all. What the parent needs is that a lookup at the child's mountpoint keeps finding some mount, not the child itself and it's not a guarantee we have given really.

When an unmounted mount would have stayed attached to its unmounted parent disconnect it like every other unmounted mount and leave a marker behind.

A lookup on the parent that misses the mount hash and hits a marker finds knullfs. Either a file or a directory. Nothing leads from a marker to any other mount.

The marker is owned by the parent and dropped by the parent's final mntput() or by __detach_mounts() when the mountpoint is deleted from under it. It is allocated together with the mount.

With that every unmounted mount is a root and holds only its own reference which namespace_unlock() drops. No unmounted mount owns another one. A superblock that pins an ancestor can't form a cycle.

It has a visible change. Mounts left connected (rmdir etc.) used to stay traversable through the parent for as long as something held the parent. Now it is detached with the umount. It lives as long as something references it but it isn't reachable through the parent anymore and ".." inside it leads nowhere which is the same as for every other lazily unmounted mount.

---
 Documentation/filesystems/propagate_umount.txt |  12 +--
 fs/mount.h                                     |  10 +-
 fs/namespace.c                                 | 129 +++++++++++++++++--------
 fs/pnode.c                                     |   4 +-
 4 files changed, 104 insertions(+), 51 deletions(-)
Documentation/filesystems/propagate_umount.txt +6 -6
index 9a7eb96df300..de58ce817582 100644
@@ -32,12 +32,12 @@ of set.  They can be reparented to the place where the bottom of
 stack is attached to a mount that will survive.  NOTE: doing that
 will violate a constraint on having no more than one mount with
 the same parent/mountpoint pair; however, the caller (umount_tree())
-will immediately remedy that - it may keep unmounted element attached
-to parent, but only if the parent itself is unmounted.  Since all
-conflicts created by reparenting have common parent *not* in the
-set and one side of the conflict (bottom of the stack of overmounts)
-is in the set, it will be resolved.  However, we rely upon umount_tree()
-doing that pretty much immediately after the call of propagate_umount().
+will immediately remedy that - it detaches every element of the set
+from its parent.  Since all conflicts created by reparenting have
+common parent *not* in the set and one side of the conflict (bottom
+of the stack of overmounts) is in the set, it will be resolved.
+However, we rely upon umount_tree() doing that pretty much immediately
+after the call of propagate_umount().
 
 Algorithm is based on two statements:
 	1) for any set S, there is a maximal non-shifting subset of S
fs/mount.h +7 -3
index 2e29cdbaeb74..4d23cade9f6c 100644
@@ -43,9 +43,12 @@ struct mnt_pcp {
 struct mountpoint {
 	struct hlist_node m_hash;
 	struct dentry *m_dentry;
-	struct hlist_head m_list;
+	struct hlist_head m_list;	/* mounts on it and pins */
+	struct hlist_head m_covers;	/* covers of unmounted parents */
 };
 
+struct mnt_cover;
+
 struct mount {
 	struct hlist_node mnt_hash;
 	struct mount *mnt_parent;
@@ -87,7 +90,7 @@ struct mount {
 	struct mountpoint *mnt_mp;	/* where is it mounted */
 	union {
 		struct hlist_node mnt_mp_list;	/* list mounts with the same mountpoint */
-		struct hlist_node mnt_umount;
+		struct hlist_node mnt_umount;	/* on the unmounted list */
 	};
 #ifdef CONFIG_FSNOTIFY
 	struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks;
@@ -101,7 +104,8 @@ struct mount {
 	int mnt_group_id;		/* peer group identifier */
 	int mnt_expiry_mark;		/* true if marked for expiry */
 	struct hlist_head mnt_pins;
-	struct hlist_head mnt_stuck_children;
+	struct mnt_cover *mnt_cover;	/* the one it may leave behind */
+	struct hlist_head mnt_covers;	/* left behind by its unmounted children */
 	struct hlist_node mnt_ns_visible; /* link in ns->mnt_visible_mounts */
 	struct mount *overmount;	/* mounted on ->mnt_root */
 } __randomize_layout;
fs/namespace.c +89 -40

276 lines not on this page · raw message

fs/pnode.c +2 -2
index 2cd667958efe..5f07fa14099b 100644
@@ -697,8 +697,8 @@ static void handle_locked(struct mount *m, struct list_head *to_umount)
  * the same parent and mountpoint; that will be remedied as soon as we
  * return from propagate_umount() - its caller (umount_tree()) will detach
  * the stack from the parent it (and now @m) is attached to.  umount_tree()
- * might choose to keep unmounted pieces stuck to each other, but it always
- * detaches them from the mounts that remain in the tree.
+ * detaches every unmounted mount from its parent, whether that parent
+ * remains in the tree or not.
  */
 static void reparent(struct mount *m)
 {
Signature · 1 line
2.53.0
maintainer
#5 lore raw

fs/namespace.c static inline struct hlist_head *mp_hash(struct dentry *dentry) · line 221

Quoted 8 lines · Christian Brauner
fs/namespace.c +8 -0
+/*
+ * What an unmounted mount leaves behind at its unmounted parent instead of
+ * staying attached to it. A lookup on the parent at the mountpoint finds
+ * a stand-in for as long as the cover is there. The parent frees it.
+ */
+struct mnt_cover {
+	struct hlist_node node;		/* parent->mnt_covers, RCU */
+	struct hlist_node pin;		/* mp->m_covers, keeps the mountpoint */

Is there any point for that to be an hlist? I mean, you only ever add to the head, walk it only forward and remove them one by one as you walk... What's wrong with plain struct mnt_cover *next; instead?

fs/namespace.c static inline struct hlist_head *mp_hash(struct dentry *dentry) · line 225

Quoted 4 lines
fs/namespace.c +4 -0
+	struct dentry *dentry;
+	struct mountpoint *mp;
+	struct rcu_head rcu;
+};

... and seeing that the only place where you have a lockless traversal of ->node is only looking at ->dentry, it might make sense to make that

	struct hlist_node node;		/* parent->mnt_covers, RCU */
	struct dentry *dentry;
	union {
		struct {	// never used by lockless readers
			struct mnt_cover *next;
			struct mountpoint *mp;
		}
		struct rcu_head rcu;
	}

perhaps?

author maintainer
#6 lore raw
Quoted 3 lines · Al Viro

Is there any point for that to be an hlist? I mean, you only ever add to the head, walk it only forward and remove them one by one as you walk... What's wrong with plain struct mnt_cover *next; instead?

Not quite. The parent's final mntput() drops its covers as well and that removes from the middle of mp->m_covers. A mountpoint can carry covers of more than one unmounted parent. So with a plain next pointer that becomes a walk over the mountpoint's covers.

Quoted 11 lines

... and seeing that the only place where you have a lockless traversal of ->node is only looking at ->dentry, it might make sense to make that

	struct hlist_node node;		/* parent->mnt_covers, RCU */
	struct dentry *dentry;
	union {
		struct {	// never used by lockless readers
			struct mnt_cover *next;
			struct mountpoint *mp;
		}
		struct rcu_head rcu;
	}
Quoted 1 line

perhaps?

Sure. Note that it's a separate allocation so 40 bytes land in kmalloc-64 just like 64 do. IOW, it's a cleanup, not a saving. I can do that for 7.4.

maintainer
#7 lore raw
Quoted 3 lines · Christian Brauner

Sure. Note that it's a separate allocation so 40 bytes land in kmalloc-64 just like 64 do. IOW, it's a cleanup, not a saving. I can do that for 7.4.

FWIW, I really don't like that separate allocation ;-/ For the majority of struct mount instances we never access that thing after it's been set up...

Sure, I see your point re separating the cleanup_mnt() guts and detaching from the parent, and yes, having two separate structures is a simple way to achieve that. However, your variant pays for that with overhead and I'm not convinced that it's actually simpler.

Consider the situation when mount reaches

	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);

in the end of cleanup_mnt(). Possible states:

1) never called leave_cover() at all.
2) called leave_cover(), matching drop_cover() has already happened.
3) called leave_cover(), matching drop_cover() is yet to come.

(1) is by far the most likely. Suppose we have leave_cover() set a new flag (MNT_COVER) and _not_ clear ->mnt_cover; that happens under mount_lock and always ordered before cleanup_mnt(). Then we can have cleanup_mnt() do the following:

	if no MNT_COVER		// case 1
		call_rcu(...)
	else
		read_seqlock_excl(&mount_lock);
		set MNT_MAY_FREE
		if cover is already off the lists	// case 2
			call_rcu(...)
		read_sequnlock_excl(&mount_lock);

and in drop_cover()

	remove from lists
	if MNT_MAY_FREE		// case 3
		call_rcu(...)

with delayed_free_vfsmnt() doing all freeing. But then we don't need the separate allocation at all. What's more, we can reuse ->mnt_mp for ->mnt_cover->mp and ->mnt_mp_list for ->mnt_cover->pin just by having __umount_mnt() make

	hlist_del_init(&mnt->mnt_mp_list);
	mp = mnt->mnt_mp;   
	mnt->mnt_mp = NULL;  
	maybe_free_mountpoint(mp, shrink_list);

conditional upon the lack of MNT_COVER. That kills your extra check in maybe_free_mountpoint(), with the loops in __detach_mounts() folded together (they'd walk the same list, using MNT_COVER to discriminate between umount_tree() and drop_cover()).

->cover->node moves to mount itself; it would be tempting to union it with mnt_hash (making removal from mnt_hash in __umount_mnt() also conditional upon !MNT_COVER). _Probably_ safe - callers of __lookup_mnt() either have mount_lock or check the seqcount side of things soon enough. Will need to check how that affects the proof of safety in your "skip synchronize_rcu if possible" series, though...

->cover->dentry... Fuck knows - might union with ->mnt_mountpoint (again, conditional upon MNT_COVER), might put it separately; ->mnt_cover goes away, so the size of struct mount won't grow. Bits in ->mnt_flags are not scarce these days - we can afford stealing two easily...

I'll try to cook something along those lines; looks at least plausible...

author maintainer
#8 lore raw
Quoted 62 lines · Al Viro
Quoted 3 lines · Christian Brauner

Sure. Note that it's a separate allocation so 40 bytes land in kmalloc-64 just like 64 do. IOW, it's a cleanup, not a saving. I can do that for 7.4.

FWIW, I really don't like that separate allocation ;-/ For the majority of struct mount instances we never access that thing after it's been set up...

Sure, I see your point re separating the cleanup_mnt() guts and detaching from the parent, and yes, having two separate structures is a simple way to achieve that. However, your variant pays for that with overhead and I'm not convinced that it's actually simpler.

Consider the situation when mount reaches

	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);

in the end of cleanup_mnt(). Possible states:

1) never called leave_cover() at all.
2) called leave_cover(), matching drop_cover() has already happened.
3) called leave_cover(), matching drop_cover() is yet to come.

(1) is by far the most likely. Suppose we have leave_cover() set a new flag (MNT_COVER) and _not_ clear ->mnt_cover; that happens under mount_lock and always ordered before cleanup_mnt(). Then we can have cleanup_mnt() do the following:

	if no MNT_COVER		// case 1
		call_rcu(...)
	else
		read_seqlock_excl(&mount_lock);
		set MNT_MAY_FREE
		if cover is already off the lists	// case 2
			call_rcu(...)
		read_sequnlock_excl(&mount_lock);

and in drop_cover()

	remove from lists
	if MNT_MAY_FREE		// case 3
		call_rcu(...)

with delayed_free_vfsmnt() doing all freeing. But then we don't need the separate allocation at all. What's more, we can reuse ->mnt_mp for ->mnt_cover->mp and ->mnt_mp_list for ->mnt_cover->pin just by having __umount_mnt() make

	hlist_del_init(&mnt->mnt_mp_list);
	mp = mnt->mnt_mp;   
	mnt->mnt_mp = NULL;  
	maybe_free_mountpoint(mp, shrink_list);

conditional upon the lack of MNT_COVER. That kills your extra check in maybe_free_mountpoint(), with the loops in __detach_mounts() folded together (they'd walk the same list, using MNT_COVER to discriminate between umount_tree() and drop_cover()).

->cover->node moves to mount itself; it would be tempting to union it with mnt_hash (making removal from mnt_hash in __umount_mnt() also conditional upon !MNT_COVER). _Probably_ safe - callers of __lookup_mnt() either have mount_lock or check the seqcount side of things soon enough. Will need to check how that affects the proof of safety in your "skip synchronize_rcu if possible" series, though...

Quoted 7 lines

->cover->dentry... Fuck knows - might union with ->mnt_mountpoint (again, conditional upon MNT_COVER), might put it separately; ->mnt_cover goes away, so the size of struct mount won't grow. Bits in ->mnt_flags are not scarce these days - we can afford stealing two easily...

I'll try to cook something along those lines; looks at least plausible...

My original proposal reused struct mount just like this. It did it differently in other aspects but the idea was the same.

And note that I had about 5 different solutions to this and every single one of them was performance tested and profiled and only this one didn't actually cause meaningful performance regressions.

Note that as written the pin cannot be mnt_mp_list. It's unioned with mnt_umount and umount_tree() puts every unmounted mount on the unmounted list through it right after umount_mnt(). A cover that stays on the mountpoint's list and goes on the unmounted list corrupts one of the two.

The hash union is safe afaict. We already move a hash node between buckes without a gp in mnt_change_mountpoint() under the seqlock. That's fine a read will retry. It doesn't matter for the gp series.

But it makes dead mounts reachable when walking buckets and that's exactly the "nobody can find it anymore" argument in my gp series. So just put the cover into mnt_child. Nothing walks the mnt_child under rcu and a reader can't end up in the covers list through a stale next pointer. Plus, the proof stays as it is.

But, my main objection is this really ugly zombie state this creates and that I hate with a passion and tried to get rid of by explicitly not reusing struct mount itself and using a separate allocation.

In this variant above a cover is a struct mount that has been through cleanup_mnt(). After that point its superblock has been deactivated and its root dentry put, so mnt_sb and mnt_root are unpinned and maybe freed. Its mount id has been freed and can be handed to the next mount. So the zombie carries a live mount's id and its counters are zero.

Yet, the mount stays linked. It sits on the parent's covers list, which the lookup walks under rcu. It sits on the mountpoint's m_list which __detach_mounts() walks and maybe_free_mountpoint() counts.

That struct mount-as-cover variant also keeps pinning completely arbitrary things like the devname, the idmap reference and the percpu counters and whatever else we might end up glueing to it.

This has a bunch of ugly side-effects:

* A parentless mount sits on its own root. Both vfs_create_mount() and __umount_mnt() make that a fact. Now, a cover in this idea has itself as parent and a mountpoint maybe on another superblock.

* Current readers are only fine because they test for a parent first, but the invariant itself is gone.

* Since covers aren't mounted anywhere every walker of the mountpiont list now needs to branch on yet another flag, forever.

* Doomed and unreferenced on mainline now means: unlinked and rcu freed. A cover here is doomed, unreferenced, yet on two lists, and freed by whichever of two parties does touch that thing last.

I dislike all of this fragility. I had to go through UMOUNT_CONNECTED and I kinda had enough of this.

In the current approach nothing in struct mnt_cover can ever dagnle. Which was the point.

With struct mount itself as a cover walking the covers, __detach_mounts() and any debug printk are one added dereference away from a UAF.

So I'm not really on board with reusing struct mount for this. I think it adds unnecessary sublety without actually buying us anything just to shave off that allocation. For all I care we can make it a kmem cache. But I did profile this heavily and the performance numbers didn't move because of it.

author maintainer
#9 lore raw
Quoted 138 lines · Christian Brauner
Quoted 62 lines · Al Viro
Quoted 3 lines · Christian Brauner

Sure. Note that it's a separate allocation so 40 bytes land in kmalloc-64 just like 64 do. IOW, it's a cleanup, not a saving. I can do that for 7.4.

FWIW, I really don't like that separate allocation ;-/ For the majority of struct mount instances we never access that thing after it's been set up...

Sure, I see your point re separating the cleanup_mnt() guts and detaching from the parent, and yes, having two separate structures is a simple way to achieve that. However, your variant pays for that with overhead and I'm not convinced that it's actually simpler.

Consider the situation when mount reaches

	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);

in the end of cleanup_mnt(). Possible states:

1) never called leave_cover() at all.
2) called leave_cover(), matching drop_cover() has already happened.
3) called leave_cover(), matching drop_cover() is yet to come.

(1) is by far the most likely. Suppose we have leave_cover() set a new flag (MNT_COVER) and _not_ clear ->mnt_cover; that happens under mount_lock and always ordered before cleanup_mnt(). Then we can have cleanup_mnt() do the following:

	if no MNT_COVER		// case 1
		call_rcu(...)
	else
		read_seqlock_excl(&mount_lock);
		set MNT_MAY_FREE
		if cover is already off the lists	// case 2
			call_rcu(...)
		read_sequnlock_excl(&mount_lock);

and in drop_cover()

	remove from lists
	if MNT_MAY_FREE		// case 3
		call_rcu(...)

with delayed_free_vfsmnt() doing all freeing. But then we don't need the separate allocation at all. What's more, we can reuse ->mnt_mp for ->mnt_cover->mp and ->mnt_mp_list for ->mnt_cover->pin just by having __umount_mnt() make

	hlist_del_init(&mnt->mnt_mp_list);
	mp = mnt->mnt_mp;   
	mnt->mnt_mp = NULL;  
	maybe_free_mountpoint(mp, shrink_list);

conditional upon the lack of MNT_COVER. That kills your extra check in maybe_free_mountpoint(), with the loops in __detach_mounts() folded together (they'd walk the same list, using MNT_COVER to discriminate between umount_tree() and drop_cover()).

->cover->node moves to mount itself; it would be tempting to union it with mnt_hash (making removal from mnt_hash in __umount_mnt() also conditional upon !MNT_COVER). _Probably_ safe - callers of __lookup_mnt() either have mount_lock or check the seqcount side of things soon enough. Will need to check how that affects the proof of safety in your "skip synchronize_rcu if possible" series, though...

Quoted 7 lines

->cover->dentry... Fuck knows - might union with ->mnt_mountpoint (again, conditional upon MNT_COVER), might put it separately; ->mnt_cover goes away, so the size of struct mount won't grow. Bits in ->mnt_flags are not scarce these days - we can afford stealing two easily...

I'll try to cook something along those lines; looks at least plausible...

My original proposal reused struct mount just like this. It did it differently in other aspects but the idea was the same.

And note that I had about 5 different solutions to this and every single one of them was performance tested and profiled and only this one didn't actually cause meaningful performance regressions.

Note that as written the pin cannot be mnt_mp_list. It's unioned with mnt_umount and umount_tree() puts every unmounted mount on the unmounted list through it right after umount_mnt(). A cover that stays on the mountpoint's list and goes on the unmounted list corrupts one of the two.

The hash union is safe afaict. We already move a hash node between buckes without a gp in mnt_change_mountpoint() under the seqlock. That's fine a read will retry. It doesn't matter for the gp series.

But it makes dead mounts reachable when walking buckets and that's exactly the "nobody can find it anymore" argument in my gp series. So just put the cover into mnt_child. Nothing walks the mnt_child under rcu and a reader can't end up in the covers list through a stale next pointer. Plus, the proof stays as it is.

But, my main objection is this really ugly zombie state this creates and that I hate with a passion and tried to get rid of by explicitly not reusing struct mount itself and using a separate allocation.

In this variant above a cover is a struct mount that has been through cleanup_mnt(). After that point its superblock has been deactivated and its root dentry put, so mnt_sb and mnt_root are unpinned and maybe freed. Its mount id has been freed and can be handed to the next mount. So the zombie carries a live mount's id and its counters are zero.

Yet, the mount stays linked. It sits on the parent's covers list, which the lookup walks under rcu. It sits on the mountpoint's m_list which __detach_mounts() walks and maybe_free_mountpoint() counts.

That struct mount-as-cover variant also keeps pinning completely arbitrary things like the devname, the idmap reference and the percpu counters and whatever else we might end up glueing to it.

This has a bunch of ugly side-effects:

* A parentless mount sits on its own root. Both vfs_create_mount() and __umount_mnt() make that a fact. Now, a cover in this idea has itself as parent and a mountpoint maybe on another superblock.

* Current readers are only fine because they test for a parent first, but the invariant itself is gone.

* Since covers aren't mounted anywhere every walker of the mountpiont list now needs to branch on yet another flag, forever.

* Doomed and unreferenced on mainline now means: unlinked and rcu freed. A cover here is doomed, unreferenced, yet on two lists, and freed by whichever of two parties does touch that thing last.

I dislike all of this fragility. I had to go through UMOUNT_CONNECTED and I kinda had enough of this.

In the current approach nothing in struct mnt_cover can ever dagnle. Which was the point.

With struct mount itself as a cover walking the covers, __detach_mounts() and any debug printk are one added dereference away from a UAF.

Quoted 5 lines

So I'm not really on board with reusing struct mount for this. I think it adds unnecessary sublety without actually buying us anything just to shave off that allocation. For all I care we can make it a kmem cache. But I did profile this heavily and the performance numbers didn't move because of it.

Oh, and I have more fixes coming on top of what I did in mainline because UMOUNT_CONNECTED is one of the most broken "security mechanisms" I have ever seen. I know this wasn't your doing ofc but this whole thing should not have been an acceptable solution. It is a giant can of worms and full of holes.

maintainer
#10 lore raw
In reply to Christian Brauner

fs/namespace.c static void noinline mntput_no_expire_slowpath(struct mount *mnt) · line 1392

Quoted 8 lines · Christian Brauner
fs/namespace.c +0 -4
@@ -1386,13 +1444,10 @@ static void noinline mntput_no_expire_slowpath(struct mount *mnt)
 	if (unlikely(!list_empty(&mnt->mnt_expire)))
 		list_del(&mnt->mnt_expire);
 
-	if (unlikely(!list_empty(&mnt->mnt_mounts))) {
-		struct mount *p, *tmp;
-		list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts,  mnt_child) {
-			__umount_mnt(p, &list);

BTW, at that point we are left with only one caller of __umount_mnt(), always with &ex_mountpoints as the second argument.

author maintainer
#11 lore raw
Quoted 2 lines · Al Viro

BTW, at that point we are left with only one caller of __umount_mnt(), always with &ex_mountpoints as the second argument.

I can fold that into umount_mnt() for 7.4.

author maintainer
#12 lore raw

PATCH 3/3 selftests/filesystems: test covered mounts

In reply to Christian Brauner

Test that mount cycles are resolved and test that mount covers behave as expected.

---
 .../selftests/filesystems/mount_cycle/.gitignore   |    3 +
 .../selftests/filesystems/mount_cycle/Makefile     |    7 +-
 .../selftests/filesystems/mount_cycle/config       |   39 +
 .../filesystems/mount_cycle/locked_handle_test.c   |  377 +++++
 .../filesystems/mount_cycle/loop_cycle_test.c      | 1542 ++++++++++++++++++++
 .../filesystems/mount_cycle/mount_cover_test.c     |  565 +++++++
 .../selftests/filesystems/mount_cycle/settings     |    1 +
 7 files changed, 2533 insertions(+), 1 deletion(-)
tools/testing/selftests/filesystems/mount_cycle/.gitignore +3 -0
index d11f5b720d5b..30f7dd071bcb 100644
@@ -1,5 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0-only
+loop_cycle_test
 unmounted_tree_test
 overmount_reparent_test
+mount_cover_test
+locked_handle_test
 nsfs_rbind_loop_test
 overmount_ns_file_test
tools/testing/selftests/filesystems/mount_cycle/Makefile +6 -1
index 49a8402ca858..fbe8c5e19d42 100644
@@ -1,7 +1,12 @@
 # SPDX-License-Identifier: GPL-2.0
-TEST_GEN_PROGS := unmounted_tree_test overmount_reparent_test
+TEST_GEN_PROGS := loop_cycle_test unmounted_tree_test overmount_reparent_test mount_cover_test locked_handle_test
 TEST_GEN_PROGS += nsfs_rbind_loop_test overmount_ns_file_test
 
 CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES)
 
+LOCAL_HDRS += ../readdir_hold.h
+
 include ../../lib.mk
+
+$(OUTPUT)/locked_handle_test: LDLIBS += -pthread
+$(OUTPUT)/mount_cover_test: LDLIBS += -pthread
tools/testing/selftests/filesystems/mount_cycle/config new file +39 -0
new file mode 100644
index 000000000000..3bd5ce46af74
@@ -0,0 +1,39 @@
+CONFIG_USER_NS=y
+CONFIG_TMPFS=y
+CONFIG_BLK_DEV_LOOP=y
+CONFIG_VFAT_FS=y
+CONFIG_MSDOS_FS=y
+CONFIG_NLS_CODEPAGE_437=y
+CONFIG_NLS_ISO8859_1=y
+CONFIG_MINIX_FS=y
+CONFIG_AUTOFS_FS=y
+CONFIG_ZRAM=y
+CONFIG_ZRAM_WRITEBACK=y
+CONFIG_KEYS=y
+CONFIG_ECRYPT_FS=y
+CONFIG_BINFMT_MISC=y
+CONFIG_FUSE_FS=y
+CONFIG_FUSE_PASSTHROUGH=y
+CONFIG_BLK_DEV_ZONED=y
+CONFIG_BLK_DEV_ZONED_LOOP=y
+CONFIG_CONFIGFS_FS=y
+CONFIG_USB_SUPPORT=y
+CONFIG_USB=y
+CONFIG_USB_STORAGE=y
+CONFIG_SCSI=y
+CONFIG_BLK_DEV_SD=y
+CONFIG_USB_GADGET=y
+CONFIG_USB_DUMMY_HCD=y
+CONFIG_USB_CONFIGFS=y
+CONFIG_USB_CONFIGFS_MASS_STORAGE=y
+CONFIG_MD=y
+CONFIG_BLK_DEV_MD=y
+CONFIG_MD_RAID1=y
+CONFIG_MD_BITMAP=y
+CONFIG_MD_BITMAP_FILE=y
+CONFIG_INOTIFY_USER=y
+CONFIG_DNOTIFY=y
+CONFIG_FANOTIFY=y
+CONFIG_FILE_LOCKING=y
+CONFIG_CRYPTO_AES=y
+CONFIG_USERFAULTFD=y
tools/testing/selftests/filesystems/mount_cycle/locked_handle_test.c new file +377 -0

377 lines not on this page · raw message

tools/testing/selftests/filesystems/mount_cycle/loop_cycle_test.c new file +1542 -0

1,542 lines not on this page · raw message

tools/testing/selftests/filesystems/mount_cycle/mount_cover_test.c new file +565 -0

565 lines not on this page · raw message

tools/testing/selftests/filesystems/mount_cycle/settings new file +1 -0
new file mode 100644
index 000000000000..694d70710ff0
@@ -0,0 +1 @@
+timeout=300
Signature · 1 line
2.53.0