Skip to content

Commit 4edbe13

Browse files
author
Al Viro
committed
make struct mountpoint bear the dentry reference to mountpoint, not struct mount
Using dput_to_list() to shift the contributing reference from ->mnt_mountpoint to ->mnt_mp->m_dentry. Dentries are dropped (with dput_to_list()) as soon as struct mountpoint is destroyed; in cases where we are under namespace_sem we use the global list, shrinking it in namespace_unlock(). In case of detaching stuck MNT_LOCKed children at final mntput_no_expire() we use a local list and shrink it ourselves. ->mnt_ex_mountpoint crap is gone. Signed-off-by: Al Viro <[email protected]>
1 parent 9bdebc2 commit 4edbe13

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

fs/mount.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ struct mount {
6969
int mnt_expiry_mark; /* true if marked for expiry */
7070
struct hlist_head mnt_pins;
7171
struct fs_pin mnt_umount;
72-
struct dentry *mnt_ex_mountpoint;
7372
} __randomize_layout;
7473

7574
#define MNT_NS_INTERNAL ERR_PTR(-EINVAL) /* distinct from any mnt_namespace */

fs/namespace.c

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ static struct hlist_head *mount_hashtable __read_mostly;
6969
static struct hlist_head *mountpoint_hashtable __read_mostly;
7070
static struct kmem_cache *mnt_cache __read_mostly;
7171
static DECLARE_RWSEM(namespace_sem);
72+
static HLIST_HEAD(unmounted); /* protected by namespace_sem */
73+
static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
7274

7375
/* /sys/fs */
7476
struct kobject *fs_kobj;
@@ -172,7 +174,6 @@ unsigned int mnt_get_count(struct mount *mnt)
172174
static void drop_mountpoint(struct fs_pin *p)
173175
{
174176
struct mount *m = container_of(p, struct mount, mnt_umount);
175-
dput(m->mnt_ex_mountpoint);
176177
pin_remove(p);
177178
mntput(&m->mnt);
178179
}
@@ -739,7 +740,7 @@ static struct mountpoint *get_mountpoint(struct dentry *dentry)
739740

740741
/* Add the new mountpoint to the hash table */
741742
read_seqlock_excl(&mount_lock);
742-
new->m_dentry = dentry;
743+
new->m_dentry = dget(dentry);
743744
new->m_count = 1;
744745
hlist_add_head(&new->m_hash, mp_hash(dentry));
745746
INIT_HLIST_HEAD(&new->m_list);
@@ -752,19 +753,30 @@ static struct mountpoint *get_mountpoint(struct dentry *dentry)
752753
return mp;
753754
}
754755

755-
static void put_mountpoint(struct mountpoint *mp)
756+
/*
757+
* vfsmount lock must be held. Additionally, the caller is responsible
758+
* for serializing calls for given disposal list.
759+
*/
760+
static void __put_mountpoint(struct mountpoint *mp, struct list_head *list)
756761
{
757762
if (!--mp->m_count) {
758763
struct dentry *dentry = mp->m_dentry;
759764
BUG_ON(!hlist_empty(&mp->m_list));
760765
spin_lock(&dentry->d_lock);
761766
dentry->d_flags &= ~DCACHE_MOUNTED;
762767
spin_unlock(&dentry->d_lock);
768+
dput_to_list(dentry, list);
763769
hlist_del(&mp->m_hash);
764770
kfree(mp);
765771
}
766772
}
767773

774+
/* called with namespace_lock and vfsmount lock */
775+
static void put_mountpoint(struct mountpoint *mp)
776+
{
777+
__put_mountpoint(mp, &ex_mountpoints);
778+
}
779+
768780
static inline int check_mnt(struct mount *mnt)
769781
{
770782
return mnt->mnt_ns == current->nsproxy->mnt_ns;
@@ -813,7 +825,7 @@ static struct mountpoint *unhash_mnt(struct mount *mnt)
813825
*/
814826
static void detach_mnt(struct mount *mnt, struct path *old_path)
815827
{
816-
old_path->dentry = mnt->mnt_mountpoint;
828+
old_path->dentry = dget(mnt->mnt_mountpoint);
817829
old_path->mnt = &mnt->mnt_parent->mnt;
818830
put_mountpoint(unhash_mnt(mnt));
819831
}
@@ -823,8 +835,6 @@ static void detach_mnt(struct mount *mnt, struct path *old_path)
823835
*/
824836
static void umount_mnt(struct mount *mnt)
825837
{
826-
/* old mountpoint will be dropped when we can do that */
827-
mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
828838
put_mountpoint(unhash_mnt(mnt));
829839
}
830840

@@ -837,7 +847,7 @@ void mnt_set_mountpoint(struct mount *mnt,
837847
{
838848
mp->m_count++;
839849
mnt_add_count(mnt, 1); /* essentially, that's mntget */
840-
child_mnt->mnt_mountpoint = dget(mp->m_dentry);
850+
child_mnt->mnt_mountpoint = mp->m_dentry;
841851
child_mnt->mnt_parent = mnt;
842852
child_mnt->mnt_mp = mp;
843853
hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
@@ -864,7 +874,6 @@ static void attach_mnt(struct mount *mnt,
864874
void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
865875
{
866876
struct mountpoint *old_mp = mnt->mnt_mp;
867-
struct dentry *old_mountpoint = mnt->mnt_mountpoint;
868877
struct mount *old_parent = mnt->mnt_parent;
869878

870879
list_del_init(&mnt->mnt_child);
@@ -874,22 +883,6 @@ void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct m
874883
attach_mnt(mnt, parent, mp);
875884

876885
put_mountpoint(old_mp);
877-
878-
/*
879-
* Safely avoid even the suggestion this code might sleep or
880-
* lock the mount hash by taking advantage of the knowledge that
881-
* mnt_change_mountpoint will not release the final reference
882-
* to a mountpoint.
883-
*
884-
* During mounting, the mount passed in as the parent mount will
885-
* continue to use the old mountpoint and during unmounting, the
886-
* old mountpoint will continue to exist until namespace_unlock,
887-
* which happens well after mnt_change_mountpoint.
888-
*/
889-
spin_lock(&old_mountpoint->d_lock);
890-
old_mountpoint->d_lockref.count--;
891-
spin_unlock(&old_mountpoint->d_lock);
892-
893886
mnt_add_count(old_parent, -1);
894887
}
895888

@@ -1142,6 +1135,8 @@ static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
11421135

11431136
static void mntput_no_expire(struct mount *mnt)
11441137
{
1138+
LIST_HEAD(list);
1139+
11451140
rcu_read_lock();
11461141
if (likely(READ_ONCE(mnt->mnt_ns))) {
11471142
/*
@@ -1182,10 +1177,11 @@ static void mntput_no_expire(struct mount *mnt)
11821177
if (unlikely(!list_empty(&mnt->mnt_mounts))) {
11831178
struct mount *p, *tmp;
11841179
list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
1185-
umount_mnt(p);
1180+
__put_mountpoint(unhash_mnt(p), &list);
11861181
}
11871182
}
11881183
unlock_mount_hash();
1184+
shrink_dentry_list(&list);
11891185

11901186
if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
11911187
struct task_struct *task = current;
@@ -1371,16 +1367,18 @@ int may_umount(struct vfsmount *mnt)
13711367

13721368
EXPORT_SYMBOL(may_umount);
13731369

1374-
static HLIST_HEAD(unmounted); /* protected by namespace_sem */
1375-
13761370
static void namespace_unlock(void)
13771371
{
13781372
struct hlist_head head;
1373+
LIST_HEAD(list);
13791374

13801375
hlist_move_list(&unmounted, &head);
1376+
list_splice_init(&ex_mountpoints, &list);
13811377

13821378
up_write(&namespace_sem);
13831379

1380+
shrink_dentry_list(&list);
1381+
13841382
if (likely(hlist_empty(&head)))
13851383
return;
13861384

0 commit comments

Comments
 (0)