Skip to content

Commit f601f9a

Browse files
htejungregkh
authored andcommitted
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>
1 parent 45a140e commit f601f9a

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

fs/kernfs/dir.c

+33-9
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,17 @@ static int kernfs_link_sibling(struct kernfs_node *kn)
121121
* Locking:
122122
* mutex_lock(kernfs_mutex)
123123
*/
124-
static void kernfs_unlink_sibling(struct kernfs_node *kn)
124+
static bool kernfs_unlink_sibling(struct kernfs_node *kn)
125125
{
126+
if (RB_EMPTY_NODE(&kn->rb))
127+
return false;
128+
126129
if (kernfs_type(kn) == KERNFS_DIR)
127130
kn->parent->dir.subdirs--;
128131

129132
rb_erase(&kn->rb, &kn->parent->dir.children);
130133
RB_CLEAR_NODE(&kn->rb);
134+
return true;
131135
}
132136

133137
/**
@@ -496,7 +500,6 @@ void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
496500

497501
acxt->removed = kn->u.removed_list;
498502

499-
kernfs_unmap_bin_file(kn);
500503
kernfs_put(kn);
501504
}
502505
}
@@ -854,23 +857,44 @@ static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
854857

855858
/* unlink the subtree node-by-node */
856859
do {
857-
struct kernfs_iattrs *ps_iattr;
858-
859860
pos = kernfs_leftmost_descendant(kn);
860861

861-
if (pos->parent) {
862-
kernfs_unlink_sibling(pos);
862+
/*
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;
863886

864887
/* update timestamps on the parent */
865-
ps_iattr = pos->parent->iattr;
866888
if (ps_iattr) {
867889
ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
868890
ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
869891
}
892+
893+
pos->u.removed_list = acxt->removed;
894+
acxt->removed = pos;
870895
}
871896

872-
pos->u.removed_list = acxt->removed;
873-
acxt->removed = pos;
897+
kernfs_put(pos);
874898
} while (pos != kn);
875899
}
876900

fs/kernfs/file.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -700,14 +700,11 @@ static int kernfs_fop_release(struct inode *inode, struct file *filp)
700700
return 0;
701701
}
702702

703-
void kernfs_unmap_bin_file(struct kernfs_node *kn)
703+
void kernfs_unmap_file(struct kernfs_node *kn)
704704
{
705705
struct kernfs_open_node *on;
706706
struct kernfs_open_file *of;
707707

708-
if (!(kn->flags & KERNFS_HAS_MMAP))
709-
return;
710-
711708
spin_lock_irq(&kernfs_open_node_lock);
712709
on = kn->attr.open;
713710
if (on)

fs/kernfs/kernfs-internal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ struct kernfs_node *kernfs_new_node(struct kernfs_root *root, const char *name,
113113
*/
114114
extern const struct file_operations kernfs_file_fops;
115115

116-
void kernfs_unmap_bin_file(struct kernfs_node *kn);
116+
void kernfs_unmap_file(struct kernfs_node *kn);
117117

118118
/*
119119
* symlink.c

0 commit comments

Comments
 (0)