aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTejun Heo <tj@kernel.org>2014-01-10 08:57:23 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-01-10 16:48:08 -0500
commitf601f9a2bf7dc1f7ee18feece4c4e2fc6845d6c4 (patch)
treef8213b3d7542c85c42cb1a20668098609d65762c
parent45a140e587f3d32d8d424ed940dffb61e1739047 (diff)
kernfs: invoke kernfs_unmap_bin_file() directly from __kernfs_remove()
kernfs_unmap_bin_file() is supposed to unmap all memory mappings of the target file before kernfs_remove() finishes; however, it currently is being called from kernfs_addrm_finish() and has the same race problem as the original implementation of deactivation when there are multiple removers - only the remover which snatches the node to its addrm_cxt->removed list is guaranteed to wait for its completion before returning. It can be fixed by moving kernfs_unmap_bin_file() invocation from kernfs_addrm_finish() to __kernfs_remove(). The function may be called multiple times but that shouldn't do any harm. We end up dropping kernfs_mutex in the removal loop and the node may be removed inbetween by someone else. kernfs_unlink_sibling() is updated to test whether the node has already been removed and return accordingly. __kernfs_remove() in turn performs post-unlinking cleanup only if it actually unlinked the node. KERNFS_HAS_MMAP test is moved out of the unmap function into __kernfs_remove() so that we don't unlock kernfs_mutex unnecessarily. While at it, drop the now meaningless "bin" qualifier from the function name. v2: Rewritten to fit the v2 restructuring of removal path. HAS_MMAP test relocated. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--fs/kernfs/dir.c42
-rw-r--r--fs/kernfs/file.c5
-rw-r--r--fs/kernfs/kernfs-internal.h2
3 files changed, 35 insertions, 14 deletions
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index e565ec096ae9..f878e4f2efe7 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -121,13 +121,17 @@ static int kernfs_link_sibling(struct kernfs_node *kn)
121 * Locking: 121 * Locking:
122 * mutex_lock(kernfs_mutex) 122 * mutex_lock(kernfs_mutex)
123 */ 123 */
124static void kernfs_unlink_sibling(struct kernfs_node *kn) 124static bool kernfs_unlink_sibling(struct kernfs_node *kn)
125{ 125{
126 if (RB_EMPTY_NODE(&kn->rb))
127 return false;
128
126 if (kernfs_type(kn) == KERNFS_DIR) 129 if (kernfs_type(kn) == KERNFS_DIR)
127 kn->parent->dir.subdirs--; 130 kn->parent->dir.subdirs--;
128 131
129 rb_erase(&kn->rb, &kn->parent->dir.children); 132 rb_erase(&kn->rb, &kn->parent->dir.children);
130 RB_CLEAR_NODE(&kn->rb); 133 RB_CLEAR_NODE(&kn->rb);
134 return true;
131} 135}
132 136
133/** 137/**
@@ -496,7 +500,6 @@ void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
496 500
497 acxt->removed = kn->u.removed_list; 501 acxt->removed = kn->u.removed_list;
498 502
499 kernfs_unmap_bin_file(kn);
500 kernfs_put(kn); 503 kernfs_put(kn);
501 } 504 }
502} 505}
@@ -854,23 +857,44 @@ static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
854 857
855 /* unlink the subtree node-by-node */ 858 /* unlink the subtree node-by-node */
856 do { 859 do {
857 struct kernfs_iattrs *ps_iattr;
858
859 pos = kernfs_leftmost_descendant(kn); 860 pos = kernfs_leftmost_descendant(kn);
860 861
861 if (pos->parent) { 862 /*
862 kernfs_unlink_sibling(pos); 863 * We're gonna release kernfs_mutex to unmap bin files,
864 * Make sure @pos doesn't go away inbetween.
865 */
866 kernfs_get(pos);
867
868 /*
869 * This must be come before unlinking; otherwise, when
870 * there are multiple removers, some may finish before
871 * unmapping is complete.
872 */
873 if (pos->flags & KERNFS_HAS_MMAP) {
874 mutex_unlock(&kernfs_mutex);
875 kernfs_unmap_file(pos);
876 mutex_lock(&kernfs_mutex);
877 }
878
879 /*
880 * kernfs_unlink_sibling() succeeds once per node. Use it
881 * to decide who's responsible for cleanups.
882 */
883 if (!pos->parent || kernfs_unlink_sibling(pos)) {
884 struct kernfs_iattrs *ps_iattr =
885 pos->parent ? pos->parent->iattr : NULL;
863 886
864 /* update timestamps on the parent */ 887 /* update timestamps on the parent */
865 ps_iattr = pos->parent->iattr;
866 if (ps_iattr) { 888 if (ps_iattr) {
867 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME; 889 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
868 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME; 890 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
869 } 891 }
892
893 pos->u.removed_list = acxt->removed;
894 acxt->removed = pos;
870 } 895 }
871 896
872 pos->u.removed_list = acxt->removed; 897 kernfs_put(pos);
873 acxt->removed = pos;
874 } while (pos != kn); 898 } while (pos != kn);
875} 899}
876 900
diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
index 231a171f48b6..404ffd2f27bc 100644
--- a/fs/kernfs/file.c
+++ b/fs/kernfs/file.c
@@ -700,14 +700,11 @@ static int kernfs_fop_release(struct inode *inode, struct file *filp)
700 return 0; 700 return 0;
701} 701}
702 702
703void kernfs_unmap_bin_file(struct kernfs_node *kn) 703void kernfs_unmap_file(struct kernfs_node *kn)
704{ 704{
705 struct kernfs_open_node *on; 705 struct kernfs_open_node *on;
706 struct kernfs_open_file *of; 706 struct kernfs_open_file *of;
707 707
708 if (!(kn->flags & KERNFS_HAS_MMAP))
709 return;
710
711 spin_lock_irq(&kernfs_open_node_lock); 708 spin_lock_irq(&kernfs_open_node_lock);
712 on = kn->attr.open; 709 on = kn->attr.open;
713 if (on) 710 if (on)
diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h
index 57a93f4d645c..e9ec38c86074 100644
--- a/fs/kernfs/kernfs-internal.h
+++ b/fs/kernfs/kernfs-internal.h
@@ -113,7 +113,7 @@ struct kernfs_node *kernfs_new_node(struct kernfs_root *root, const char *name,
113 */ 113 */
114extern const struct file_operations kernfs_file_fops; 114extern const struct file_operations kernfs_file_fops;
115 115
116void kernfs_unmap_bin_file(struct kernfs_node *kn); 116void kernfs_unmap_file(struct kernfs_node *kn);
117 117
118/* 118/*
119 * symlink.c 119 * symlink.c