aboutsummaryrefslogtreecommitdiffstats
path: root/fs/namespace.c
diff options
context:
space:
mode:
authorAl Viro <viro@zeniv.linux.org.uk>2014-03-20 21:10:51 -0400
committerAl Viro <viro@zeniv.linux.org.uk>2014-03-30 19:18:51 -0400
commit38129a13e6e71f666e0468e99fdd932a687b4d7e (patch)
tree438e817fdf7d224f9fda1186eb24b1bbc37a4b5c /fs/namespace.c
parent0b1b901b5a98bb36943d10820efc796f7cd45ff3 (diff)
switch mnt_hash to hlist
fixes RCU bug - walking through hlist is safe in face of element moves, since it's self-terminating. Cyclic lists are not - if we end up jumping to another hash chain, we'll loop infinitely without ever hitting the original list head. [fix for dumb braino folded] Spotted by: Max Kellermann <mk@cm4all.com> Cc: stable@vger.kernel.org Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Diffstat (limited to 'fs/namespace.c')
-rw-r--r--fs/namespace.c79
1 files changed, 43 insertions, 36 deletions
diff --git a/fs/namespace.c b/fs/namespace.c
index d3fb9f00576e..2ffc5a2905d4 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -59,7 +59,7 @@ static DEFINE_SPINLOCK(mnt_id_lock);
59static int mnt_id_start = 0; 59static int mnt_id_start = 0;
60static int mnt_group_start = 1; 60static int mnt_group_start = 1;
61 61
62static struct list_head *mount_hashtable __read_mostly; 62static struct hlist_head *mount_hashtable __read_mostly;
63static struct hlist_head *mountpoint_hashtable __read_mostly; 63static struct hlist_head *mountpoint_hashtable __read_mostly;
64static struct kmem_cache *mnt_cache __read_mostly; 64static struct kmem_cache *mnt_cache __read_mostly;
65static DECLARE_RWSEM(namespace_sem); 65static DECLARE_RWSEM(namespace_sem);
@@ -78,7 +78,7 @@ EXPORT_SYMBOL_GPL(fs_kobj);
78 */ 78 */
79__cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock); 79__cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
80 80
81static inline struct list_head *m_hash(struct vfsmount *mnt, struct dentry *dentry) 81static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
82{ 82{
83 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 83 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
84 tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 84 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
@@ -217,7 +217,7 @@ static struct mount *alloc_vfsmnt(const char *name)
217 mnt->mnt_writers = 0; 217 mnt->mnt_writers = 0;
218#endif 218#endif
219 219
220 INIT_LIST_HEAD(&mnt->mnt_hash); 220 INIT_HLIST_NODE(&mnt->mnt_hash);
221 INIT_LIST_HEAD(&mnt->mnt_child); 221 INIT_LIST_HEAD(&mnt->mnt_child);
222 INIT_LIST_HEAD(&mnt->mnt_mounts); 222 INIT_LIST_HEAD(&mnt->mnt_mounts);
223 INIT_LIST_HEAD(&mnt->mnt_list); 223 INIT_LIST_HEAD(&mnt->mnt_list);
@@ -605,10 +605,10 @@ bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
605 */ 605 */
606struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 606struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
607{ 607{
608 struct list_head *head = m_hash(mnt, dentry); 608 struct hlist_head *head = m_hash(mnt, dentry);
609 struct mount *p; 609 struct mount *p;
610 610
611 list_for_each_entry_rcu(p, head, mnt_hash) 611 hlist_for_each_entry_rcu(p, head, mnt_hash)
612 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 612 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
613 return p; 613 return p;
614 return NULL; 614 return NULL;
@@ -620,20 +620,16 @@ struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
620 */ 620 */
621struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry) 621struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry)
622{ 622{
623 struct list_head *head = m_hash(mnt, dentry); 623 struct mount *p, *res;
624 struct mount *p, *res = NULL; 624 res = p = __lookup_mnt(mnt, dentry);
625 625 if (!p)
626 list_for_each_entry(p, head, mnt_hash) 626 goto out;
627 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 627 hlist_for_each_entry_continue(p, mnt_hash) {
628 goto found;
629 return res;
630found:
631 res = p;
632 list_for_each_entry_continue(p, head, mnt_hash) {
633 if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry) 628 if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry)
634 break; 629 break;
635 res = p; 630 res = p;
636 } 631 }
632out:
637 return res; 633 return res;
638} 634}
639 635
@@ -750,7 +746,7 @@ static void detach_mnt(struct mount *mnt, struct path *old_path)
750 mnt->mnt_parent = mnt; 746 mnt->mnt_parent = mnt;
751 mnt->mnt_mountpoint = mnt->mnt.mnt_root; 747 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
752 list_del_init(&mnt->mnt_child); 748 list_del_init(&mnt->mnt_child);
753 list_del_init(&mnt->mnt_hash); 749 hlist_del_init_rcu(&mnt->mnt_hash);
754 put_mountpoint(mnt->mnt_mp); 750 put_mountpoint(mnt->mnt_mp);
755 mnt->mnt_mp = NULL; 751 mnt->mnt_mp = NULL;
756} 752}
@@ -777,7 +773,7 @@ static void attach_mnt(struct mount *mnt,
777 struct mountpoint *mp) 773 struct mountpoint *mp)
778{ 774{
779 mnt_set_mountpoint(parent, mp, mnt); 775 mnt_set_mountpoint(parent, mp, mnt);
780 list_add(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry)); 776 hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry));
781 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 777 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
782} 778}
783 779
@@ -800,9 +796,9 @@ static void commit_tree(struct mount *mnt, struct mount *shadows)
800 list_splice(&head, n->list.prev); 796 list_splice(&head, n->list.prev);
801 797
802 if (shadows) 798 if (shadows)
803 list_add(&mnt->mnt_hash, &shadows->mnt_hash); 799 hlist_add_after_rcu(&shadows->mnt_hash, &mnt->mnt_hash);
804 else 800 else
805 list_add(&mnt->mnt_hash, 801 hlist_add_head_rcu(&mnt->mnt_hash,
806 m_hash(&parent->mnt, mnt->mnt_mountpoint)); 802 m_hash(&parent->mnt, mnt->mnt_mountpoint));
807 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 803 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
808 touch_mnt_namespace(n); 804 touch_mnt_namespace(n);
@@ -1193,26 +1189,28 @@ int may_umount(struct vfsmount *mnt)
1193 1189
1194EXPORT_SYMBOL(may_umount); 1190EXPORT_SYMBOL(may_umount);
1195 1191
1196static LIST_HEAD(unmounted); /* protected by namespace_sem */ 1192static HLIST_HEAD(unmounted); /* protected by namespace_sem */
1197 1193
1198static void namespace_unlock(void) 1194static void namespace_unlock(void)
1199{ 1195{
1200 struct mount *mnt; 1196 struct mount *mnt;
1201 LIST_HEAD(head); 1197 struct hlist_head head = unmounted;
1202 1198
1203 if (likely(list_empty(&unmounted))) { 1199 if (likely(hlist_empty(&head))) {
1204 up_write(&namespace_sem); 1200 up_write(&namespace_sem);
1205 return; 1201 return;
1206 } 1202 }
1207 1203
1208 list_splice_init(&unmounted, &head); 1204 head.first->pprev = &head.first;
1205 INIT_HLIST_HEAD(&unmounted);
1206
1209 up_write(&namespace_sem); 1207 up_write(&namespace_sem);
1210 1208
1211 synchronize_rcu(); 1209 synchronize_rcu();
1212 1210
1213 while (!list_empty(&head)) { 1211 while (!hlist_empty(&head)) {
1214 mnt = list_first_entry(&head, struct mount, mnt_hash); 1212 mnt = hlist_entry(head.first, struct mount, mnt_hash);
1215 list_del_init(&mnt->mnt_hash); 1213 hlist_del_init(&mnt->mnt_hash);
1216 if (mnt->mnt_ex_mountpoint.mnt) 1214 if (mnt->mnt_ex_mountpoint.mnt)
1217 path_put(&mnt->mnt_ex_mountpoint); 1215 path_put(&mnt->mnt_ex_mountpoint);
1218 mntput(&mnt->mnt); 1216 mntput(&mnt->mnt);
@@ -1233,16 +1231,19 @@ static inline void namespace_lock(void)
1233 */ 1231 */
1234void umount_tree(struct mount *mnt, int how) 1232void umount_tree(struct mount *mnt, int how)
1235{ 1233{
1236 LIST_HEAD(tmp_list); 1234 HLIST_HEAD(tmp_list);
1237 struct mount *p; 1235 struct mount *p;
1236 struct mount *last = NULL;
1238 1237
1239 for (p = mnt; p; p = next_mnt(p, mnt)) 1238 for (p = mnt; p; p = next_mnt(p, mnt)) {
1240 list_move(&p->mnt_hash, &tmp_list); 1239 hlist_del_init_rcu(&p->mnt_hash);
1240 hlist_add_head(&p->mnt_hash, &tmp_list);
1241 }
1241 1242
1242 if (how) 1243 if (how)
1243 propagate_umount(&tmp_list); 1244 propagate_umount(&tmp_list);
1244 1245
1245 list_for_each_entry(p, &tmp_list, mnt_hash) { 1246 hlist_for_each_entry(p, &tmp_list, mnt_hash) {
1246 list_del_init(&p->mnt_expire); 1247 list_del_init(&p->mnt_expire);
1247 list_del_init(&p->mnt_list); 1248 list_del_init(&p->mnt_list);
1248 __touch_mnt_namespace(p->mnt_ns); 1249 __touch_mnt_namespace(p->mnt_ns);
@@ -1260,8 +1261,13 @@ void umount_tree(struct mount *mnt, int how)
1260 p->mnt_mp = NULL; 1261 p->mnt_mp = NULL;
1261 } 1262 }
1262 change_mnt_propagation(p, MS_PRIVATE); 1263 change_mnt_propagation(p, MS_PRIVATE);
1264 last = p;
1265 }
1266 if (last) {
1267 last->mnt_hash.next = unmounted.first;
1268 unmounted.first = tmp_list.first;
1269 unmounted.first->pprev = &unmounted.first;
1263 } 1270 }
1264 list_splice(&tmp_list, &unmounted);
1265} 1271}
1266 1272
1267static void shrink_submounts(struct mount *mnt); 1273static void shrink_submounts(struct mount *mnt);
@@ -1645,8 +1651,9 @@ static int attach_recursive_mnt(struct mount *source_mnt,
1645 struct mountpoint *dest_mp, 1651 struct mountpoint *dest_mp,
1646 struct path *parent_path) 1652 struct path *parent_path)
1647{ 1653{
1648 LIST_HEAD(tree_list); 1654 HLIST_HEAD(tree_list);
1649 struct mount *child, *p; 1655 struct mount *child, *p;
1656 struct hlist_node *n;
1650 int err; 1657 int err;
1651 1658
1652 if (IS_MNT_SHARED(dest_mnt)) { 1659 if (IS_MNT_SHARED(dest_mnt)) {
@@ -1671,9 +1678,9 @@ static int attach_recursive_mnt(struct mount *source_mnt,
1671 commit_tree(source_mnt, NULL); 1678 commit_tree(source_mnt, NULL);
1672 } 1679 }
1673 1680
1674 list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 1681 hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
1675 struct mount *q; 1682 struct mount *q;
1676 list_del_init(&child->mnt_hash); 1683 hlist_del_init(&child->mnt_hash);
1677 q = __lookup_mnt_last(&child->mnt_parent->mnt, 1684 q = __lookup_mnt_last(&child->mnt_parent->mnt,
1678 child->mnt_mountpoint); 1685 child->mnt_mountpoint);
1679 commit_tree(child, q); 1686 commit_tree(child, q);
@@ -2818,7 +2825,7 @@ void __init mnt_init(void)
2818 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 2825 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
2819 2826
2820 mount_hashtable = alloc_large_system_hash("Mount-cache", 2827 mount_hashtable = alloc_large_system_hash("Mount-cache",
2821 sizeof(struct list_head), 2828 sizeof(struct hlist_head),
2822 mhash_entries, 19, 2829 mhash_entries, 19,
2823 0, 2830 0,
2824 &m_hash_shift, &m_hash_mask, 0, 0); 2831 &m_hash_shift, &m_hash_mask, 0, 0);
@@ -2832,7 +2839,7 @@ void __init mnt_init(void)
2832 panic("Failed to allocate mount hash table\n"); 2839 panic("Failed to allocate mount hash table\n");
2833 2840
2834 for (u = 0; u <= m_hash_mask; u++) 2841 for (u = 0; u <= m_hash_mask; u++)
2835 INIT_LIST_HEAD(&mount_hashtable[u]); 2842 INIT_HLIST_HEAD(&mount_hashtable[u]);
2836 for (u = 0; u <= mp_hash_mask; u++) 2843 for (u = 0; u <= mp_hash_mask; u++)
2837 INIT_HLIST_HEAD(&mountpoint_hashtable[u]); 2844 INIT_HLIST_HEAD(&mountpoint_hashtable[u]);
2838 2845