mnt: In propgate_umount handle visiting mounts in any order

While investigating some poor umount performance I realized that in
the case of overlapping mount trees where some of the mounts are locked
the code has been failing to unmount all of the mounts it should
have been unmounting.

This failure to unmount all of the necessary
mounts can be reproduced with:

$ cat locked_mounts_test.sh

mount -t tmpfs test-base /mnt
mount --make-shared /mnt
mkdir -p /mnt/b

mount -t tmpfs test1 /mnt/b
mount --make-shared /mnt/b
mkdir -p /mnt/b/10

mount -t tmpfs test2 /mnt/b/10
mount --make-shared /mnt/b/10
mkdir -p /mnt/b/10/20

mount --rbind /mnt/b /mnt/b/10/20

unshare -Urm --propagation unchaged /bin/sh -c 'sleep 5; if [ $(grep test /proc/self/mountinfo | wc -l) -eq 1 ] ; then echo SUCCESS ; else echo FAILURE ; fi'
sleep 1
umount -l /mnt/b
wait %%

$ unshare -Urm ./locked_mounts_test.sh

This failure is corrected by removing the prepass that marks mounts
that may be umounted.

A first pass is added that umounts mounts if possible and if not sets
mount mark if they could be unmounted if they weren't locked and adds
them to a list to umount possibilities.  This first pass reconsiders
the mounts parent if it is on the list of umount possibilities, ensuring
that information of umoutability will pass from child to mount parent.

A second pass then walks through all mounts that are umounted and processes
their children unmounting them or marking them for reparenting.

A last pass cleans up the state on the mounts that could not be umounted
and if applicable reparents them to their first parent that remained
mounted.

While a bit longer than the old code this code is much more robust
as it allows information to flow up from the leaves and down
from the trunk making the order in which mounts are encountered
in the umount propgation tree irrelevant.

Cc: stable@vger.kernel.org
Fixes: 0c56fe3142 ("mnt: Don't propagate unmounts to locked mounts")
Reviewed-by: Andrei Vagin <avagin@virtuozzo.com>
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
This commit is contained in:
Eric W. Biederman 2016-10-24 16:16:13 -05:00
parent 570487d3fa
commit 99b19d1647
3 changed files with 88 additions and 60 deletions

View File

@ -58,7 +58,7 @@ struct mount {
struct mnt_namespace *mnt_ns; /* containing namespace */ struct mnt_namespace *mnt_ns; /* containing namespace */
struct mountpoint *mnt_mp; /* where is it mounted */ struct mountpoint *mnt_mp; /* where is it mounted */
struct hlist_node mnt_mp_list; /* list mounts with the same mountpoint */ struct hlist_node mnt_mp_list; /* list mounts with the same mountpoint */
struct list_head mnt_reparent; /* reparent list entry */ struct list_head mnt_umounting; /* list entry for umount propagation */
#ifdef CONFIG_FSNOTIFY #ifdef CONFIG_FSNOTIFY
struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks; struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks;
__u32 mnt_fsnotify_mask; __u32 mnt_fsnotify_mask;

View File

@ -236,7 +236,7 @@ static struct mount *alloc_vfsmnt(const char *name)
INIT_LIST_HEAD(&mnt->mnt_slave_list); INIT_LIST_HEAD(&mnt->mnt_slave_list);
INIT_LIST_HEAD(&mnt->mnt_slave); INIT_LIST_HEAD(&mnt->mnt_slave);
INIT_HLIST_NODE(&mnt->mnt_mp_list); INIT_HLIST_NODE(&mnt->mnt_mp_list);
INIT_LIST_HEAD(&mnt->mnt_reparent); INIT_LIST_HEAD(&mnt->mnt_umounting);
init_fs_pin(&mnt->mnt_umount, drop_mountpoint); init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
} }
return mnt; return mnt;

View File

@ -413,85 +413,94 @@ void propagate_mount_unlock(struct mount *mnt)
} }
} }
/* static void umount_one(struct mount *mnt, struct list_head *to_umount)
* Mark all mounts that the MNT_LOCKED logic will allow to be unmounted.
*/
static void mark_umount_candidates(struct mount *mnt)
{ {
struct mount *parent = mnt->mnt_parent; CLEAR_MNT_MARK(mnt);
struct mount *m; mnt->mnt.mnt_flags |= MNT_UMOUNT;
list_del_init(&mnt->mnt_child);
BUG_ON(parent == mnt); list_del_init(&mnt->mnt_umounting);
list_move_tail(&mnt->mnt_list, to_umount);
for (m = propagation_next(parent, parent); m;
m = propagation_next(m, parent)) {
struct mount *child = __lookup_mnt(&m->mnt,
mnt->mnt_mountpoint);
if (!child || (child->mnt.mnt_flags & MNT_UMOUNT))
continue;
if (!IS_MNT_LOCKED(child) || IS_MNT_MARKED(m)) {
SET_MNT_MARK(child);
}
}
} }
/* /*
* NOTE: unmounting 'mnt' naturally propagates to all other mounts its * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
* parent propagates to. * parent propagates to.
*/ */
static void __propagate_umount(struct mount *mnt, struct list_head *to_reparent) static bool __propagate_umount(struct mount *mnt,
struct list_head *to_umount,
struct list_head *to_restore)
{ {
struct mount *parent = mnt->mnt_parent; bool progress = false;
struct mount *m; struct mount *child;
BUG_ON(parent == mnt);
for (m = propagation_next(parent, parent); m;
m = propagation_next(m, parent)) {
struct mount *topper;
struct mount *child = __lookup_mnt(&m->mnt,
mnt->mnt_mountpoint);
/* /*
* umount the child only if the child has no children * The state of the parent won't change if this mount is
* and the child is marked safe to unmount. * already unmounted or marked as without children.
*/ */
if (!child || !IS_MNT_MARKED(child)) if (mnt->mnt.mnt_flags & (MNT_UMOUNT | MNT_MARKED))
goto out;
/* Verify topper is the only grandchild that has not been
* speculatively unmounted.
*/
list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
if (child->mnt_mountpoint == mnt->mnt.mnt_root)
continue; continue;
CLEAR_MNT_MARK(child); if (!list_empty(&child->mnt_umounting) && IS_MNT_MARKED(child))
continue;
/* Found a mounted child */
goto children;
}
/* If there is exactly one mount covering all of child /* Mark mounts that can be unmounted if not locked */
* replace child with that mount. SET_MNT_MARK(mnt);
*/ progress = true;
topper = find_topper(child);
if (topper)
list_add_tail(&topper->mnt_reparent, to_reparent);
if (topper || list_empty(&child->mnt_mounts)) { /* If a mount is without children and not locked umount it. */
list_del_init(&child->mnt_child); if (!IS_MNT_LOCKED(mnt)) {
list_del_init(&child->mnt_reparent); umount_one(mnt, to_umount);
child->mnt.mnt_flags |= MNT_UMOUNT; } else {
list_move_tail(&child->mnt_list, &mnt->mnt_list); children:
list_move_tail(&mnt->mnt_umounting, to_restore);
}
out:
return progress;
}
static void umount_list(struct list_head *to_umount,
struct list_head *to_restore)
{
struct mount *mnt, *child, *tmp;
list_for_each_entry(mnt, to_umount, mnt_list) {
list_for_each_entry_safe(child, tmp, &mnt->mnt_mounts, mnt_child) {
/* topper? */
if (child->mnt_mountpoint == mnt->mnt.mnt_root)
list_move_tail(&child->mnt_umounting, to_restore);
else
umount_one(child, to_umount);
} }
} }
} }
static void reparent_mounts(struct list_head *to_reparent) static void restore_mounts(struct list_head *to_restore)
{ {
while (!list_empty(to_reparent)) { /* Restore mounts to a clean working state */
while (!list_empty(to_restore)) {
struct mount *mnt, *parent; struct mount *mnt, *parent;
struct mountpoint *mp; struct mountpoint *mp;
mnt = list_first_entry(to_reparent, struct mount, mnt_reparent); mnt = list_first_entry(to_restore, struct mount, mnt_umounting);
list_del_init(&mnt->mnt_reparent); CLEAR_MNT_MARK(mnt);
list_del_init(&mnt->mnt_umounting);
/* Where should this mount be reparented to? */ /* Should this mount be reparented? */
mp = mnt->mnt_mp; mp = mnt->mnt_mp;
parent = mnt->mnt_parent; parent = mnt->mnt_parent;
while (parent->mnt.mnt_flags & MNT_UMOUNT) { while (parent->mnt.mnt_flags & MNT_UMOUNT) {
mp = parent->mnt_mp; mp = parent->mnt_mp;
parent = parent->mnt_parent; parent = parent->mnt_parent;
} }
if (parent != mnt->mnt_parent)
mnt_change_mountpoint(parent, mp, mnt); mnt_change_mountpoint(parent, mp, mnt);
} }
} }
@ -506,15 +515,34 @@ static void reparent_mounts(struct list_head *to_reparent)
int propagate_umount(struct list_head *list) int propagate_umount(struct list_head *list)
{ {
struct mount *mnt; struct mount *mnt;
LIST_HEAD(to_reparent); LIST_HEAD(to_restore);
LIST_HEAD(to_umount);
list_for_each_entry_reverse(mnt, list, mnt_list) list_for_each_entry(mnt, list, mnt_list) {
mark_umount_candidates(mnt); struct mount *parent = mnt->mnt_parent;
struct mount *m;
list_for_each_entry(mnt, list, mnt_list) for (m = propagation_next(parent, parent); m;
__propagate_umount(mnt, &to_reparent); m = propagation_next(m, parent)) {
struct mount *child = __lookup_mnt(&m->mnt,
mnt->mnt_mountpoint);
if (!child)
continue;
reparent_mounts(&to_reparent); /* Check the child and parents while progress is made */
while (__propagate_umount(child,
&to_umount, &to_restore)) {
/* Is the parent a umount candidate? */
child = child->mnt_parent;
if (list_empty(&child->mnt_umounting))
break;
}
}
}
umount_list(&to_umount, &to_restore);
restore_mounts(&to_restore);
list_splice_tail(&to_umount, list);
return 0; return 0;
} }