mount fixes
Al Viro
maintainerRFC PATCH 1/1 mount fixes
Several regression fixes of varying severity; live ingit://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs.git, individual patches in followups. Help with testing and reviewing would be very welcome.
1) attach_recursive_mnt() slowdown - severe in some pathological cases. This cycle regression, fortunately trivial to fix. Underlying mechanism of slowdown (bypassed by that fix) is due to bad semantics of skip_mnt_tree(), but that's a separate story.
The case with severe slowdown is mounting on top of a long chain of overmounts, all peers with each other. Ends up being time-quardatic by the depth of chain, easily triggers softlockup warnings. Since the slow part is under mount_lock, all pathname resolution pretty much stops for the duration. Kudos for catching that to "Lai, Yi" <[email protected]>...
2) reparenting in propagate_umount() should *NOT* be done to mounts that are themselves going to be taken out. I'd even put "<argument> is not to going away, and it overmounts the top of a stack of mounts that are going away" right on top of reparent()... and lost the logics in its caller that used to check that at some point during reshuffling the queue ;-/ This cycle's regression and that one is not just a slowdown - in some cases it might end up as data corruptor. Trivial to fix...
3) permission checks for --make-{shared,slave,private,unlinkable} (i.e. MS_{SHARED,...} in mount(2) flags) ended up being too restrictive. The checks used to be too weak until the last cycle. Then they were made stricter - basically to "mount is a part of our namespace and we have admin permissions there". Turns out that CRIU userland depended upon the weak^Wabsent checks we used to have there before. Sane replacement that would suffice for them is "mount is a part of *SOME* namespace that we have admin permissions for". Last cycle userland regression...
4) change_mnt_propagation() slowdown in some cases. On umount we want all victims out of propagation graph and propagation between the surviving mounts to be unchanged. So if victim used to have slaves, they need to be transfered to its peer (if any) or master. In case when victim had many peers, all taken out by that umount(), that ended up with all its slaves being gradually transferred between all peers until we finally ran out of those. It can easily lead to quadratic time. The patch in -rc1 switched that to "just find where they'll end up upfront, and move them once", which eliminated that... except that I hadn't noticed that on massage of change_mnt_propagation() we ended up calculating the place where they'd be transferred in cases when there had been nothing to transfer. With obvious effects when there had been a large peer group entirely taken out, with not a single slave between them. The minimal fix ("call propagation_source() only if we are going to use its return value") is enough to recover in all cases.
Longer term we should kick all victims out of propagation graph at once and I have that plotted out, but that's for the next merge window; for now the minimal obvious fix is good enough.
Al Viro
maintainerPATCH 1/4 fix the softlockups in attach_recursive_mnt()
In case when we mounting something on top of a large stack of overmounts, all of them being peers of each other, we get quadratic time by the depth of overmount stack. Easily fixed by doing commit_tree() before reparenting the overmount; simplifies commit_tree() as well - it doesn't need to skip the already mounted stuff that had been reparented on top of the new mounts.
Since we are holding mount_lock through both reparenting and call of commit_tree(), the order does not matter from the mount hash point of view.
--- fs/namespace.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
fs/namespace.c +2 -5
@@ -1197,10 +1197,7 @@ static void commit_tree(struct mount *mnt)if (!mnt_ns_attached (mnt)) {for (struct mount *m = mnt; m; m =next_mnt (m, mnt)) -if (unlikely (mnt_ns_attached (m))) - m =skip_mnt_tree (m); -else -mnt_add_to_ns (n, m); +mnt_add_to_ns (n, m); n->nr_mounts += n->pending_mounts; n->pending_mounts =0 ; }
@@ -2704,6 +2701,7 @@ static int attach_recursive_mnt(struct mount *source_mnt,lock_mnt_tree (child); q =__lookup_mnt (&child->mnt_parent->mnt, child->mnt_mountpoint); +commit_tree (child);if (q) {struct mountpoint *mp = root.mp;struct mount *r = child;
@@ -2713,7 +2711,6 @@ static int attach_recursive_mnt(struct mount *source_mnt, mp = shorter;mnt_change_mountpoint (r, mp, q); } -commit_tree (child); }unpin_mountpoint (&root);unlock_mount_hash ();
Signature · 1 line
2.47.2
Al Viro
maintainerPATCH 2/4 propagate_umount(): only surviving overmounts should be remounted
In reply to Al Viro... as the comments in reparent() clearly say. As it is, we reparent *all* overmounts of the mounts being taken out, including those that are taken out themselves. It's not only a potentially massive slowdown (on a pathological setup we might end up with O(N^2) time for N mounts being kicked out), it can end up with incorrect ->overmount in the surviving mounts.
--- fs/pnode.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
fs/pnode.c +4 -3
@@ -637,10 +637,11 @@ void propagate_umount(struct list_head *set) }// now to_umount consists of all acceptable candidates -// deal with reparenting of remaining overmounts on those +// deal with reparenting of surviving overmounts on those list_for_each_entry (m, &to_umount, mnt_list) { -if (m->overmount) -reparent (m->overmount); +struct mount *over = m->overmount; +if (over && !will_be_unmounted (over)) +reparent (over); }// and fold them into the set
Signature · 1 line
2.47.2
Al Viro
maintainerPATCH 3/4 use uniform permission checks for all mount propagation changes
In reply to Al Virodo_change_type() and do_set_group() are operating on different aspects of the same thing - propagation graph. The latter asks for mounts involved to be mounted in namespace(s) the caller has CAP_SYS_ADMIN for. The former is a mess - originally it didn't even check that mount *is* mounted. That got fixed, but the resulting check turns out to be too strict for userland - in effect, we check that mount is in our namespace, having already checked that we have CAP_SYS_ADMIN there.
What we really need (in both cases) is
* we only touch mounts that are mounted. Hard requirement,
data corruption if that's get violated.
* we don't allow to mess with a namespace unless you already
have enough permissions to do so (i.e. CAP_SYS_ADMIN in its userns).
That's an equivalent of what do_set_group() does; let's extract that into a helper (may_change_propagation()) and use it in both do_set_group() and do_change_type().
--- fs/namespace.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-)
fs/namespace.c +20 -14
@@ -2859,6 +2859,19 @@ static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)return attach_recursive_mnt (mnt, p, mp); } +static int may_change_propagation (const struct mount *m) +{ +struct mnt_namespace *ns = m->mnt_ns; + +// it must be mounted in some namespace +if (IS_ERR_OR_NULL (ns))// is_mounted() +return -EINVAL ; +// and the caller must be admin in userns of that namespace +if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN )) +return -EPERM ; +return 0 ; +} +/* * Sanity check the flags to change_mnt_propagation. */
@@ -2895,10 +2908,10 @@ static int do_change_type(struct path *path, int ms_flags)return -EINVAL ;namespace_lock (); -if (!check_mnt (mnt)) { - err = -EINVAL ; + err =may_change_propagation (mnt); +if (err) goto out_unlock; - } +if (type ==MS_SHARED ) { err =invent_group_ids (mnt, recurse);if (err)
@@ -3344,18 +3357,11 @@ static int do_set_group(struct path *from_path, struct path *to_path)namespace_lock (); - err = -EINVAL ; -/* To and From must be mounted */ -if (!is_mounted (&from->mnt)) - goto out; -if (!is_mounted (&to->mnt)) - goto out; - - err = -EPERM ; -/* We should be allowed to modify mount namespaces of both mounts */ -if (!ns_capable (from->mnt_ns->user_ns,CAP_SYS_ADMIN )) + err =may_change_propagation (from); +if (err) goto out; -if (!ns_capable (to->mnt_ns->user_ns,CAP_SYS_ADMIN )) + err =may_change_propagation (to); +if (err) goto out; err = -EINVAL ;
Signature · 1 line
2.47.2
Al Viro
maintainerPATCH 4/4 change_mnt_propagation(): calculate propagation source only if we'll need it
In reply to Al ViroWe only need it when mount in question was sending events downstream (then recepients need to switch to new master) or the mount is being turned into slave (then we need a new master for it).
That wouldn't be a big deal, except that it causes quite a bit of work when umount_tree() is taking a large peer group out. Adding a trivial "don't bother calling propagation_source() unless we are going to use its results" logics improves the things quite a bit.
We are still doing unnecessary work on bulk removals from propagation graph, but the full solution for that will have to wait for the next merge window.
--- fs/pnode.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/pnode.c +2 -1
@@ -111,7 +111,8 @@ void change_mnt_propagation(struct mount *mnt, int type)return ; }if (IS_MNT_SHARED (mnt)) { - m =propagation_source (mnt); +if (type ==MS_SLAVE || !hlist_empty (&mnt->mnt_slave_list)) + m =propagation_source (mnt);if (list_empty (&mnt->mnt_share)) {mnt_release_group_id (mnt); }else {
Signature · 1 line
2.47.2
Al Viro
maintainerQuoted 9 lines · Al Viro
4)
change_mnt_propagation()slowdown in some cases. On umount we want all victims out of propagation graph and propagation between the surviving mounts to be unchanged. So if victim used to have slaves, they need to be transfered to its peer (if any) or master. In case when victim had many peers, all taken out by thatumount(), that ended up with all its slaves being gradually transferred between all peers until we finally ran out of those. It can easily lead to quadratic time. The patch in -rc1 switched that to "just find where they'll end up upfront, and move them once", which eliminated that... except that I hadn't noticed that on massage ofchange_mnt_propagation()
Quoted 8 lines
we ended up calculating the place where they'd be transferred in cases when there had been nothing to transfer. With obvious effects when there had been a large peer group entirely taken out, with not a single slave between them. The minimal fix ("call
propagation_source()only if we are going to use its return value") is enough to recover in all cases.
Longer term we should kick all victims out of propagation graph at once and I have that plotted out, but that's for the next merge window; for now the minimal obvious fix is good enough.
FWIW, proposed longer term fix (on top of this series, completely untested) would be the patch below. Basically, calculate where the slaves end up for all mounts to be removed, taking the mounts themselves out of propagation graph, then do all transfers; duplicate work on finding destinations is avoided that way, since if we run into a mount that already had destination found, we don't need to trace the rest of the way. That's guaranteed O(removed mounts) for finding destinations and removing from propagation graph and O(surviving mounts that have master removed) for transfers.
fs/namespace.c +2 -1
@@ -1842,6 +1842,8 @@ static void umount_tree(struct mount *mnt, enum umount_tree_flags how)if (how &UMOUNT_PROPAGATE )propagate_umount (&tmp_list); +bulk_make_private (&tmp_list); +while (!list_empty (&tmp_list)) {struct mnt_namespace *ns;bool disconnect;
@@ -1866,7 +1868,6 @@ static void umount_tree(struct mount *mnt, enum umount_tree_flags how)umount_mnt (p); } } -change_mnt_propagation (p,MS_PRIVATE );if (disconnect)hlist_add_head (&p->mnt_umount, &unmounted);
fs/pnode.c +52 -15
88 lines not on this page · raw message
fs/pnode.h +1 -0
@@ -42,6 +42,7 @@ static inline bool peers(const struct mount *m1, const struct mount *m2) }void change_mnt_propagation (struct mount *,int ); +void bulk_make_private (struct list_head *);int propagate_mnt (struct mount *,struct mountpoint *,struct mount *,struct hlist_head *);void propagate_umount (struct list_head *);
Re: [PATCH 3/4] use uniform permission checks for all mount propagation changes
In reply to Al ViroQuoted 16 lines · Al Viro
do_change_type()anddo_set_group()are operating on different aspects of the same thing - propagation graph. The latter asks for mounts involved to be mounted in namespace(s) the caller hasCAP_SYS_ADMINfor. The former is a mess - originally it didn't even check that mount *is* mounted. That got fixed, but the resulting check turns out to be too strict for userland - in effect, we check that mount is in our namespace, having already checked that we haveCAP_SYS_ADMINthere.What we really need (in both cases) is
* we only touch mounts that are mounted. Hard requirement,
data corruption if that's get violated.
* we don't allow to mess with a namespace unless you already
have enough permissions to do so (i.e.CAP_SYS_ADMINin its userns).
Quoted 4 lines
That's an equivalent of what
do_set_group()does; let's extract that into a helper (may_change_propagation()) and use it in bothdo_set_group()anddo_change_type().
Al, thank you for the fix.
Quoted 70 lines
Fixes 12f147ddd6de "do_change_type(): refuse to operate on unmounted/not ours mounts"Signed-off-by Al Viro <[email protected]>--- fs/namespace.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-)fs/namespace.c +20
-15@@ -2859,6 +2859,19 @@ static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)return attach_recursive_mnt (mnt, p, mp); } +static int may_change_propagation (const struct mount *m) +{ +struct mnt_namespace *ns = m->mnt_ns; + +// it must be mounted in some namespace +if (IS_ERR_OR_NULL (ns))// is_mounted() +return -EINVAL ; +// and the caller must be admin in userns of that namespace +if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN )) +return -EPERM ; +return 0 ; +} +/* * Sanity check the flags to change_mnt_propagation. */ @@ -2895,10 +2908,10 @@ static int do_change_type(struct path *path, int ms_flags)return -EINVAL ;namespace_lock (); -if (!check_mnt (mnt)) { - err = -EINVAL ; + err =may_change_propagation (mnt); +if (err) goto out_unlock; - } +if (type ==MS_SHARED ) { err =invent_group_ids (mnt, recurse);if (err)@@ -3344,18 +3357,11 @@ static int do_set_group(struct path *from_path, struct path *to_path)namespace_lock (); - err = -EINVAL ; -/* To and From must be mounted */ -if (!is_mounted (&from->mnt)) - goto out; -if (!is_mounted (&to->mnt)) - goto out; - - err = -EPERM ; -/* We should be allowed to modify mount namespaces of both mounts */ -if (!ns_capable (from->mnt_ns->user_ns,CAP_SYS_ADMIN )) + err =may_change_propagation (from); +if (err) goto out; -if (!ns_capable (to->mnt_ns->user_ns,CAP_SYS_ADMIN )) + err =may_change_propagation (to); +if (err) goto out; err = -EINVAL ; --2.47.2
Re: [PATCH 3/4] use uniform permission checks for all mount propagation changes
In reply to Al ViroLooks good! Thanks!
(CRIU tests PASS for the whole patchset)
Quoted 90 lines · Al Viro
do_change_type()anddo_set_group()are operating on different aspects of the same thing - propagation graph. The latter asks for mounts involved to be mounted in namespace(s) the caller hasCAP_SYS_ADMINfor. The former is a mess - originally it didn't even check that mount *is* mounted. That got fixed, but the resulting check turns out to be too strict for userland - in effect, we check that mount is in our namespace, having already checked that we haveCAP_SYS_ADMINthere.What we really need (in both cases) is
* we only touch mounts that are mounted. Hard requirement,
data corruption if that's get violated.
* we don't allow to mess with a namespace unless you already
have enough permissions to do so (i.e.CAP_SYS_ADMINin its userns).That's an equivalent of what
do_set_group()does; let's extract that into a helper (may_change_propagation()) and use it in bothdo_set_group()anddo_change_type().Fixes 12f147ddd6de "do_change_type(): refuse to operate on unmounted/not ours mounts"Signed-off-by Al Viro <[email protected]>--- fs/namespace.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-)fs/namespace.c +20
-15@@ -2859,6 +2859,19 @@ static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)return attach_recursive_mnt (mnt, p, mp); } +static int may_change_propagation (const struct mount *m) +{ +struct mnt_namespace *ns = m->mnt_ns; + +// it must be mounted in some namespace +if (IS_ERR_OR_NULL (ns))// is_mounted() +return -EINVAL ; +// and the caller must be admin in userns of that namespace +if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN )) +return -EPERM ; +return 0 ; +} +/* * Sanity check the flags to change_mnt_propagation. */ @@ -2895,10 +2908,10 @@ static int do_change_type(struct path *path, int ms_flags)return -EINVAL ;namespace_lock (); -if (!check_mnt (mnt)) { - err = -EINVAL ; + err =may_change_propagation (mnt); +if (err) goto out_unlock; - } +if (type ==MS_SHARED ) { err =invent_group_ids (mnt, recurse);if (err)@@ -3344,18 +3357,11 @@ static int do_set_group(struct path *from_path, struct path *to_path)namespace_lock (); - err = -EINVAL ; -/* To and From must be mounted */ -if (!is_mounted (&from->mnt)) - goto out; -if (!is_mounted (&to->mnt)) - goto out; - - err = -EPERM ; -/* We should be allowed to modify mount namespaces of both mounts */ -if (!ns_capable (from->mnt_ns->user_ns,CAP_SYS_ADMIN )) + err =may_change_propagation (from); +if (err) goto out; -if (!ns_capable (to->mnt_ns->user_ns,CAP_SYS_ADMIN )) + err =may_change_propagation (to); +if (err) goto out; err = -EINVAL ; --2.47.2
Re: [PATCH 1/4] fix the softlockups in attach_recursive_mnt()
In reply to Al ViroQuoted 11 lines · Al Viro
In case when we mounting something on top of a large stack of overmounts, all of them being peers of each other, we get quadratic time by the depth of overmount stack. Easily fixed by doing
commit_tree()before reparenting the overmount; simplifiescommit_tree()as well - it doesn't need to skip the already mounted stuff that had been reparented on top of the new mounts.Since we are holding
mount_lockthrough both reparenting and call ofcommit_tree(), the order does not matter from the mount hash point of view.
Quoted 5 lines
Reported-by "Lai, Yi" <[email protected]>Tested-by "Lai, Yi" <[email protected]>Fixes 663206854f02 "copy_tree(): don't link the mounts via mnt_list"Signed-off-by Al Viro <[email protected]>---
Re: [PATCH 2/4] propagate_umount(): only surviving overmounts should be remounted
In reply to Al ViroQuoted 10 lines · Al Viro
... as the comments in
reparent()clearly say. As it is, we reparent *all* overmounts of the mounts being taken out, including those that are taken out themselves. It's not only a potentially massive slowdown (on a pathological setup we might end up with O(N^2) time for N mounts being kicked out), it can end up with incorrect ->overmount in the surviving mounts.Fixes f0d0ba19985d "Rewrite of propagate_umount()"Signed-off-by Al Viro <[email protected]>---
Re: [PATCH 3/4] use uniform permission checks for all mount propagation changes
In reply to Al ViroQuoted 15 lines · Al Viro
do_change_type()anddo_set_group()are operating on different aspects of the same thing - propagation graph. The latter asks for mounts involved to be mounted in namespace(s) the caller hasCAP_SYS_ADMINfor. The former is a mess - originally it didn't even check that mount *is* mounted. That got fixed, but the resulting check turns out to be too strict for userland - in effect, we check that mount is in our namespace, having already checked that we haveCAP_SYS_ADMINthere.What we really need (in both cases) is
* we only touch mounts that are mounted. Hard requirement,
data corruption if that's get violated.
* we don't allow to mess with a namespace unless you already
have enough permissions to do so (i.e.CAP_SYS_ADMINin its userns).
Quoted 7 lines
That's an equivalent of what
do_set_group()does; let's extract that into a helper (may_change_propagation()) and use it in bothdo_set_group()anddo_change_type().Fixes 12f147ddd6de "do_change_type(): refuse to operate on unmounted/not ours mounts"Signed-off-by Al Viro <[email protected]>---
Re: [PATCH 4/4] change_mnt_propagation(): calculate propagation source only if we'll need it
In reply to Al ViroQuoted 9 lines · Al Viro
We only need it when mount in question was sending events downstream (then recepients need to switch to new master) or the mount is being turned into slave (then we need a new master for it).
That wouldn't be a big deal, except that it causes quite a bit of work when
umount_tree()is taking a large peer group out. Adding a trivial "don't bother callingpropagation_source()unless we are going to use its results" logics improves the things quite a bit.
Quoted 6 lines
We are still doing unnecessary work on bulk removals from propagation graph, but the full solution for that will have to wait for the next merge window.
Fixes 955336e204ab "do_make_slave(): choose new master sanely"Signed-off-by Al Viro <[email protected]>---
Al Viro
maintainer(collected *-by and slightly cleaned the text in commit message of [3/4]; otherwise identical to what had been posted and sat in #fixes)
The following changes since commit 8742b2d8935f476449ef37e263bc4da3295c7b58:
Merge tag 'pull-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs (2025-08-12 12:10:33 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs.git tags/pull-fixes
for you to fetch changes up to fb924b7b8669503582e003dd7b7340ee49029801:
change_mnt_propagation(): calculate propagation source only if we'll need it (2025-08-19 12:05:59 -0400)
----------------------------------------------------------------
fixes for several recent mount-related regressions
----------------------------------------------------------------
Al Viro (4):
fix the softlockups in attach_recursive_mnt() propagate_umount(): only surviving overmounts should be reparented use uniform permission checks for all mount propagation changes change_mnt_propagation(): calculate propagation source only if we'll need it
fs/namespace.c | 41 ++++++++++++++++++++++------------------- fs/pnode.c | 10 ++++++---- 2 files changed, 28 insertions(+), 23 deletions(-)
Quoted 3 lines · Al Viro
(collected *-by and slightly cleaned the text in commit message of [3/4]; otherwise identical to what had been posted and sat in #fixes)
Minor note relating to my workflow: I almost overlooked this pull request, because it was a continuation of a thread with the subject "[PATCHES][RFC][CFT]", and then shows up in my mailbox as part of that thread.
So even though you had removed those RFC/CFT notes from the subject line, that ended up being not entirely obvious in my MUA, and I was about to archive the email before I started looking closer.
Now, this time I obviously caught it, but in general it might be safer to make it more obvious that it has gone from a RFC to a "this is a real pull". Not being threaded is obviously one way, or just a bigger note to make it more obvious.
And hey, this is not generally a huge problem. I could miss emails for other random reasons like flaky spam detectors or just plain (hopefully rare) incompetence on my part.
So obviously the general fix to "why didn't Linus pull" is to send me a follow-up email reminding me about missed pull requests, but I thought I'd mention this as a "avoid potential delays and confusion" thing.
Linus
The pull request you sent on Tue, 19 Aug 2025 17:12:28 +0100:
The pull request you sent on Tue, 19 Aug 2025 17:12:28 +0100:
Quoted 1 line
git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs.gittags/pull-fixes
has been merged into torvalds/linux.git:
https://git.kernel.org/torvalds/c/b19a97d57c15643494ac8bfaaa35e3ee472d41da
Thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/prtracker.html