Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit abd54f02 authored by Tejun Heo's avatar Tejun Heo Committed by Greg Kroah-Hartman
Browse files

kernfs: replace kernfs_node->u.completion with kernfs_root->deactivate_waitq



kernfs_node->u.completion is used to notify deactivation completion
from kernfs_put_active() to kernfs_deactivate().  We now allow
multiple racing removals of the same node and the current removal
scheme is no longer correct - kernfs_remove() invocation may return
before the node is properly deactivated if it races against another
removal.  The removal path will be restructured to address the issue.

To help such restructure which requires supporting multiple waiters,
this patch replaces kernfs_node->u.completion with
kernfs_root->deactivate_waitq.  This makes deactivation event
notifications share a per-root waitqueue_head; however, the wait path
is quite cold and this will also allow shaving one pointer off
kernfs_node.

v2: Refreshed on top of ("kernfs: make kernfs_deactivate() honor
    KERNFS_LOCKDEP flag").

Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent a6607930
Loading
Loading
Loading
Loading
+13 −18
Original line number Original line Diff line number Diff line
@@ -8,6 +8,7 @@
 * This file is released under the GPLv2.
 * This file is released under the GPLv2.
 */
 */


#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/fs.h>
#include <linux/namei.h>
#include <linux/namei.h>
#include <linux/idr.h>
#include <linux/idr.h>
@@ -151,6 +152,7 @@ struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
 */
 */
void kernfs_put_active(struct kernfs_node *kn)
void kernfs_put_active(struct kernfs_node *kn)
{
{
	struct kernfs_root *root = kernfs_root(kn);
	int v;
	int v;


	if (unlikely(!kn))
	if (unlikely(!kn))
@@ -162,11 +164,7 @@ void kernfs_put_active(struct kernfs_node *kn)
	if (likely(v != KN_DEACTIVATED_BIAS))
	if (likely(v != KN_DEACTIVATED_BIAS))
		return;
		return;


	/*
	wake_up_all(&root->deactivate_waitq);
	 * atomic_dec_return() is a mb(), we'll always see the updated
	 * kn->u.completion.
	 */
	complete(kn->u.completion);
}
}


/**
/**
@@ -177,28 +175,24 @@ void kernfs_put_active(struct kernfs_node *kn)
 */
 */
static void kernfs_deactivate(struct kernfs_node *kn)
static void kernfs_deactivate(struct kernfs_node *kn)
{
{
	DECLARE_COMPLETION_ONSTACK(wait);
	struct kernfs_root *root = kernfs_root(kn);
	int v;


	BUG_ON(!(kn->flags & KERNFS_REMOVED));
	BUG_ON(!(kn->flags & KERNFS_REMOVED));


	if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF))
	if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF))
		return;
		return;


	kn->u.completion = (void *)&wait;

	if (kn->flags & KERNFS_LOCKDEP)
	if (kn->flags & KERNFS_LOCKDEP)
		rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
		rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
	/* atomic_add_return() is a mb(), put_active() will always see
	 * the updated kn->u.completion.
	 */
	v = atomic_add_return(KN_DEACTIVATED_BIAS, &kn->active);


	if (v != KN_DEACTIVATED_BIAS) {
	atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
		if (kn->flags & KERNFS_LOCKDEP)

	if ((kn->flags & KERNFS_LOCKDEP) &&
	    atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
		lock_contended(&kn->dep_map, _RET_IP_);
		lock_contended(&kn->dep_map, _RET_IP_);
		wait_for_completion(&wait);

	}
	wait_event(root->deactivate_waitq,
		   atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);


	if (kn->flags & KERNFS_LOCKDEP) {
	if (kn->flags & KERNFS_LOCKDEP) {
		lock_acquired(&kn->dep_map, _RET_IP_);
		lock_acquired(&kn->dep_map, _RET_IP_);
@@ -630,6 +624,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)


	root->dir_ops = kdops;
	root->dir_ops = kdops;
	root->kn = kn;
	root->kn = kn;
	init_waitqueue_head(&root->deactivate_waitq);


	return root;
	return root;
}
}
+2 −2
Original line number Original line Diff line number Diff line
@@ -15,7 +15,7 @@
#include <linux/lockdep.h>
#include <linux/lockdep.h>
#include <linux/rbtree.h>
#include <linux/rbtree.h>
#include <linux/atomic.h>
#include <linux/atomic.h>
#include <linux/completion.h>
#include <linux/wait.h>


struct file;
struct file;
struct dentry;
struct dentry;
@@ -92,7 +92,6 @@ struct kernfs_node {
	struct rb_node		rb;
	struct rb_node		rb;


	union {
	union {
		struct completion	*completion;
		struct kernfs_node	*removed_list;
		struct kernfs_node	*removed_list;
	} u;
	} u;


@@ -133,6 +132,7 @@ struct kernfs_root {
	/* private fields, do not use outside kernfs proper */
	/* private fields, do not use outside kernfs proper */
	struct ida		ino_ida;
	struct ida		ino_ida;
	struct kernfs_dir_ops	*dir_ops;
	struct kernfs_dir_ops	*dir_ops;
	wait_queue_head_t	deactivate_waitq;
};
};


struct kernfs_open_file {
struct kernfs_open_file {