Skip to content

Commit 45a140e

Browse files
htejungregkh
authored andcommitted
kernfs: restructure removal path to fix possible premature return
The recursive nature of kernfs_remove() means that, even if kernfs_remove() is not allowed to be called multiple times on the same node, there may be race conditions between removal of parent and its descendants. While we can claim that kernfs_remove() shouldn't be called on one of the descendants while the removal of an ancestor is in progress, such rule is unnecessarily restrictive and very difficult to enforce. It's better to simply allow invoking kernfs_remove() as the caller sees fit as long as the caller ensures that the node is accessible. The current behavior in such situations is broken. Whoever enters removal path first takes the node off the hierarchy and then deactivates. Following removers either return as soon as it notices that it's not the first one or can't even find the target node as it has already been removed from the hierarchy. In both cases, the following removers may finish prematurely while the nodes which should be removed and drained are still being processed by the first one. This patch restructures so that multiple removers, whether through recursion or direction invocation, always follow the following rules. * When there are multiple concurrent removers, only one puts the base ref. * Regardless of which one puts the base ref, all removers are blocked until the target node is fully deactivated and removed. To achieve the above, removal path now first deactivates the subtree, drains it and then unlinks one-by-one. __kernfs_deactivate() is called directly from __kernfs_removal() and drops and regrabs kernfs_mutex for each descendant to drain active refs. As this means that multiple removers can enter __kernfs_deactivate() for the same node, the function is updated so that it can handle multiple deactivators of the same node - only one actually deactivates but all wait till drain completion. The restructured removal path guarantees that a removed node gets unlinked only after the node is deactivated and drained. Combined with proper multiple deactivator handling, this guarantees that any invocation of kernfs_remove() returns only after the node itself and all its descendants are deactivated, drained and removed. v2: Draining separated into a separate loop (used to be in the same loop as unlink) and done from __kernfs_deactivate(). This is to allow exposing deactivation as a separate interface later. Root node removal was broken in v1 patch. Fixed. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ae34372 commit 45a140e

2 files changed

Lines changed: 87 additions & 53 deletions

File tree

‎fs/kernfs/dir.c‎

Lines changed: 86 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,38 @@ void kernfs_put_active(struct kernfs_node *kn)
181181
* kernfs_drain - drain kernfs_node
182182
* @kn: kernfs_node to drain
183183
*
184-
* Drain existing usages.
184+
* Drain existing usages of @kn. Mutiple removers may invoke this function
185+
* concurrently on @kn and all will return after draining is complete.
186+
* Returns %true if drain is performed and kernfs_mutex was temporarily
187+
* released. %false if @kn was already drained and no operation was
188+
* necessary.
189+
*
190+
* The caller is responsible for ensuring @kn stays pinned while this
191+
* function is in progress even if it gets removed by someone else.
185192
*/
186-
static void kernfs_drain(struct kernfs_node *kn)
193+
static bool kernfs_drain(struct kernfs_node *kn)
194+
__releases(&kernfs_mutex) __acquires(&kernfs_mutex)
187195
{
188196
struct kernfs_root *root = kernfs_root(kn);
189197

198+
lockdep_assert_held(&kernfs_mutex);
190199
WARN_ON_ONCE(atomic_read(&kn->active) >= 0);
191200

201+
/*
202+
* We want to go through the active ref lockdep annotation at least
203+
* once for all node removals, but the lockdep annotation can't be
204+
* nested inside kernfs_mutex and deactivation can't make forward
205+
* progress if we keep dropping the mutex. Use JUST_ACTIVATED to
206+
* force the slow path once for each deactivation if lockdep is
207+
* enabled.
208+
*/
209+
if ((!kernfs_lockdep(kn) || !(kn->flags & KERNFS_JUST_DEACTIVATED)) &&
210+
atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
211+
return false;
212+
213+
kn->flags &= ~KERNFS_JUST_DEACTIVATED;
214+
mutex_unlock(&kernfs_mutex);
215+
192216
if (kernfs_lockdep(kn)) {
193217
rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
194218
if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
@@ -202,6 +226,9 @@ static void kernfs_drain(struct kernfs_node *kn)
202226
lock_acquired(&kn->dep_map, _RET_IP_);
203227
rwsem_release(&kn->dep_map, 1, _RET_IP_);
204228
}
229+
230+
mutex_lock(&kernfs_mutex);
231+
return true;
205232
}
206233

207234
/**
@@ -446,49 +473,6 @@ int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn,
446473
return 0;
447474
}
448475

449-
/**
450-
* kernfs_remove_one - remove kernfs_node from parent
451-
* @acxt: addrm context to use
452-
* @kn: kernfs_node to be removed
453-
*
454-
* Mark @kn removed and drop nlink of parent inode if @kn is a
455-
* directory. @kn is unlinked from the children list.
456-
*
457-
* This function should be called between calls to
458-
* kernfs_addrm_start() and kernfs_addrm_finish() and should be
459-
* passed the same @acxt as passed to kernfs_addrm_start().
460-
*
461-
* LOCKING:
462-
* Determined by kernfs_addrm_start().
463-
*/
464-
static void kernfs_remove_one(struct kernfs_addrm_cxt *acxt,
465-
struct kernfs_node *kn)
466-
{
467-
struct kernfs_iattrs *ps_iattr;
468-
469-
/*
470-
* Removal can be called multiple times on the same node. Only the
471-
* first invocation is effective and puts the base ref.
472-
*/
473-
if (atomic_read(&kn->active) < 0)
474-
return;
475-
476-
if (kn->parent) {
477-
kernfs_unlink_sibling(kn);
478-
479-
/* Update timestamps on the parent */
480-
ps_iattr = kn->parent->iattr;
481-
if (ps_iattr) {
482-
ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
483-
ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
484-
}
485-
}
486-
487-
atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
488-
kn->u.removed_list = acxt->removed;
489-
acxt->removed = kn;
490-
}
491-
492476
/**
493477
* kernfs_addrm_finish - finish up kernfs_node add/remove
494478
* @acxt: addrm context to finish up
@@ -512,7 +496,6 @@ void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
512496

513497
acxt->removed = kn->u.removed_list;
514498

515-
kernfs_drain(kn);
516499
kernfs_unmap_bin_file(kn);
517500
kernfs_put(kn);
518501
}
@@ -822,23 +805,73 @@ static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
822805
return pos->parent;
823806
}
824807

808+
static void __kernfs_deactivate(struct kernfs_node *kn)
809+
{
810+
struct kernfs_node *pos;
811+
812+
lockdep_assert_held(&kernfs_mutex);
813+
814+
/* prevent any new usage under @kn by deactivating all nodes */
815+
pos = NULL;
816+
while ((pos = kernfs_next_descendant_post(pos, kn))) {
817+
if (atomic_read(&pos->active) >= 0) {
818+
atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
819+
pos->flags |= KERNFS_JUST_DEACTIVATED;
820+
}
821+
}
822+
823+
/*
824+
* Drain the subtree. If kernfs_drain() blocked to drain, which is
825+
* indicated by %true return, it temporarily released kernfs_mutex
826+
* and the rbtree might have been modified inbetween breaking our
827+
* future walk. Restart the walk after each %true return.
828+
*/
829+
pos = NULL;
830+
while ((pos = kernfs_next_descendant_post(pos, kn))) {
831+
bool drained;
832+
833+
kernfs_get(pos);
834+
drained = kernfs_drain(pos);
835+
kernfs_put(pos);
836+
if (drained)
837+
pos = NULL;
838+
}
839+
}
840+
825841
static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
826842
struct kernfs_node *kn)
827843
{
828-
struct kernfs_node *pos, *next;
844+
struct kernfs_node *pos;
845+
846+
lockdep_assert_held(&kernfs_mutex);
829847

830848
if (!kn)
831849
return;
832850

833851
pr_debug("kernfs %s: removing\n", kn->name);
834852

835-
next = NULL;
853+
__kernfs_deactivate(kn);
854+
855+
/* unlink the subtree node-by-node */
836856
do {
837-
pos = next;
838-
next = kernfs_next_descendant_post(pos, kn);
839-
if (pos)
840-
kernfs_remove_one(acxt, pos);
841-
} while (next);
857+
struct kernfs_iattrs *ps_iattr;
858+
859+
pos = kernfs_leftmost_descendant(kn);
860+
861+
if (pos->parent) {
862+
kernfs_unlink_sibling(pos);
863+
864+
/* update timestamps on the parent */
865+
ps_iattr = pos->parent->iattr;
866+
if (ps_iattr) {
867+
ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
868+
ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
869+
}
870+
}
871+
872+
pos->u.removed_list = acxt->removed;
873+
acxt->removed = pos;
874+
} while (pos != kn);
842875
}
843876

844877
/**

‎include/linux/kernfs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enum kernfs_node_type {
3737
#define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
3838

3939
enum kernfs_node_flag {
40+
KERNFS_JUST_DEACTIVATED = 0x0010, /* used to aid lockdep annotation */
4041
KERNFS_NS = 0x0020,
4142
KERNFS_HAS_SEQ_SHOW = 0x0040,
4243
KERNFS_HAS_MMAP = 0x0080,

0 commit comments

Comments
 (0)