All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tejun Heo <tj@kernel.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	Chengming Zhou <zhouchengming@bytedance.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Imran Khan <imran.f.khan@oracle.com>,
	kernel-team@fb.com, Tejun Heo <tj@kernel.org>
Subject: [PATCH 6/9] kernfs: Add KERNFS_REMOVING flags
Date: Sat, 27 Aug 2022 19:04:37 -1000	[thread overview]
Message-ID: <20220828050440.734579-7-tj@kernel.org> (raw)
In-Reply-To: <20220828050440.734579-1-tj@kernel.org>

KERNFS_ACTIVATED tracks whether a given node has ever been activated. As a
node was only deactivated on removal, this was used for

 1. Drain optimization (removed by the previous patch).
 2. To hide !activated nodes
 3. To avoid double activations
 4. Reject adding children to a node being removed
 5. Skip activaing a node which is being removed.

We want to decouple deactivation from removal so that nodes can be
deactivated and hidden dynamically, which makes KERNFS_ACTIVATED useless for
all of the above purposes.

#1 is already gone. #2 and #3 can instead test whether the node is currently
active. A new flag KERNFS_REMOVING is added to explicitly mark nodes which
are being removed for #4 and #5.

While this leaves KERNFS_ACTIVATED with no users, leave it be as it will be
used in a following patch.

Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Chengming Zhou <zhouchengming@bytedance.com>
---
 fs/kernfs/dir.c        | 21 +++++++--------------
 include/linux/kernfs.h |  1 +
 2 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index b3d2018a334d..f8cbd05e9b68 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -705,13 +705,7 @@ struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
 			goto err_unlock;
 	}
 
-	/*
-	 * ACTIVATED is protected with kernfs_mutex but it was clear when
-	 * @kn was added to idr and we just wanna see it set.  No need to
-	 * grab kernfs_mutex.
-	 */
-	if (unlikely(!(kn->flags & KERNFS_ACTIVATED) ||
-		     !atomic_inc_not_zero(&kn->count)))
+	if (unlikely(!kernfs_active(kn) || !atomic_inc_not_zero(&kn->count)))
 		goto err_unlock;
 
 	spin_unlock(&kernfs_idr_lock);
@@ -753,10 +747,7 @@ int kernfs_add_one(struct kernfs_node *kn)
 		goto out_unlock;
 
 	ret = -ENOENT;
-	if (parent->flags & KERNFS_EMPTY_DIR)
-		goto out_unlock;
-
-	if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent))
+	if (parent->flags & (KERNFS_REMOVING | KERNFS_EMPTY_DIR))
 		goto out_unlock;
 
 	kn->hash = kernfs_name_hash(kn->name, kn->ns);
@@ -1336,7 +1327,7 @@ void kernfs_activate(struct kernfs_node *kn)
 
 	pos = NULL;
 	while ((pos = kernfs_next_descendant_post(pos, kn))) {
-		if (pos->flags & KERNFS_ACTIVATED)
+		if (kernfs_active(pos) || (pos->flags & KERNFS_REMOVING))
 			continue;
 
 		WARN_ON_ONCE(pos->parent && RB_EMPTY_NODE(&pos->rb));
@@ -1368,11 +1359,13 @@ static void __kernfs_remove(struct kernfs_node *kn)
 
 	pr_debug("kernfs %s: removing\n", kn->name);
 
-	/* prevent any new usage under @kn by deactivating all nodes */
+	/* prevent new usage by marking all nodes removing and deactivating */
 	pos = NULL;
-	while ((pos = kernfs_next_descendant_post(pos, kn)))
+	while ((pos = kernfs_next_descendant_post(pos, kn))) {
+		pos->flags |= KERNFS_REMOVING;
 		if (kernfs_active(pos))
 			atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
+	}
 
 	/* deactivate and unlink the subtree node-by-node */
 	do {
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index 367044d7708c..b77d257c1f7e 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -112,6 +112,7 @@ enum kernfs_node_flag {
 	KERNFS_SUICIDED		= 0x0800,
 	KERNFS_EMPTY_DIR	= 0x1000,
 	KERNFS_HAS_RELEASE	= 0x2000,
+	KERNFS_REMOVING		= 0x4000,
 };
 
 /* @flags for kernfs_create_root() */
-- 
2.37.2


  parent reply	other threads:[~2022-08-28  5:05 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-28  5:04 [PATCHSET v2 for-6.1] kernfs, cgroup: implement kernfs_show() and cgroup_file_show() Tejun Heo
2022-08-28  5:04 ` [PATCH 1/9] kernfs: Simply by replacing kernfs_deref_open_node() with of_on() Tejun Heo
2022-09-09 14:00   ` [tip: sched/psi] " tip-bot2 for Tejun Heo
2022-08-28  5:04 ` [PATCH 2/9] kernfs: Drop unnecessary "mutex" local variable initialization Tejun Heo
2022-09-09 14:00   ` [tip: sched/psi] " tip-bot2 for Tejun Heo
2022-08-28  5:04 ` [PATCH 3/9] kernfs: Refactor kernfs_get_open_node() Tejun Heo
2022-09-09 14:00   ` [tip: sched/psi] " tip-bot2 for Tejun Heo
2022-08-28  5:04 ` [PATCH 4/9] kernfs: Skip kernfs_drain_open_files() more aggressively Tejun Heo
2022-09-09 14:00   ` [tip: sched/psi] " tip-bot2 for Tejun Heo
2022-08-28  5:04 ` [PATCH 5/9] kernfs: Improve kernfs_drain() and always call on removal Tejun Heo
2022-09-09 14:00   ` [tip: sched/psi] " tip-bot2 for Tejun Heo
2022-08-28  5:04 ` Tejun Heo [this message]
2022-09-09 14:00   ` [tip: sched/psi] kernfs: Add KERNFS_REMOVING flags tip-bot2 for Tejun Heo
2022-08-28  5:04 ` [PATCH 7/9] kernfs: Factor out kernfs_activate_one() Tejun Heo
2022-09-09 14:00   ` [tip: sched/psi] " tip-bot2 for Tejun Heo
2022-08-28  5:04 ` [PATCH 8/9] kernfs: Implement kernfs_show() Tejun Heo
2022-09-09 14:00   ` [tip: sched/psi] " tip-bot2 for Tejun Heo
2022-08-28  5:04 ` [PATCH 9/9] cgroup: Implement cgroup_file_show() Tejun Heo
2022-09-09 14:00   ` [tip: sched/psi] " tip-bot2 for Tejun Heo
2022-08-28  8:33 ` [PATCHSET v2 for-6.1] kernfs, cgroup: implement kernfs_show() and cgroup_file_show() Chengming Zhou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220828050440.734579-7-tj@kernel.org \
    --to=tj@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=hannes@cmpxchg.org \
    --cc=imran.f.khan@oracle.com \
    --cc=kernel-team@fb.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=zhouchengming@bytedance.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.