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 5/9] kernfs: Improve kernfs_drain() and always call on removal
Date: Sat, 27 Aug 2022 19:04:36 -1000	[thread overview]
Message-ID: <20220828050440.734579-6-tj@kernel.org> (raw)
In-Reply-To: <20220828050440.734579-1-tj@kernel.org>

__kernfs_remove() was skipping draining based on KERNFS_ACTIVATED - whether
the node has ever been activated since creation. Instead, update it to
always call kernfs_drain() which now drains or skips based on the precise
drain conditions. This ensures that the nodes will be deactivated and
drained regardless of their states.

This doesn't make meaningful difference now but will enable deactivating and
draining nodes dynamically by making removals safe when racing those
operations.

While at it, drop / update comments.

v2: Fix the inverted test on kernfs_should_drain_open_files() noted by
    Chengming. This was fixed by the next unrelated patch in the previous
    posting.

Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Chengming Zhou <zhouchengming@bytedance.com>
---
 fs/kernfs/dir.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 8ae44db920d4..b3d2018a334d 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -472,6 +472,16 @@ static void kernfs_drain(struct kernfs_node *kn)
 	lockdep_assert_held_write(&root->kernfs_rwsem);
 	WARN_ON_ONCE(kernfs_active(kn));
 
+	/*
+	 * Skip draining if already fully drained. This avoids draining and its
+	 * lockdep annotations for nodes which have never been activated
+	 * allowing embedding kernfs_remove() in create error paths without
+	 * worrying about draining.
+	 */
+	if (atomic_read(&kn->active) == KN_DEACTIVATED_BIAS &&
+	    !kernfs_should_drain_open_files(kn))
+		return;
+
 	up_write(&root->kernfs_rwsem);
 
 	if (kernfs_lockdep(kn)) {
@@ -480,7 +490,6 @@ static void kernfs_drain(struct kernfs_node *kn)
 			lock_contended(&kn->dep_map, _RET_IP_);
 	}
 
-	/* but everyone should wait for draining */
 	wait_event(root->deactivate_waitq,
 		   atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
 
@@ -1370,23 +1379,14 @@ static void __kernfs_remove(struct kernfs_node *kn)
 		pos = kernfs_leftmost_descendant(kn);
 
 		/*
-		 * kernfs_drain() drops kernfs_rwsem temporarily and @pos's
+		 * kernfs_drain() may drop kernfs_rwsem temporarily and @pos's
 		 * base ref could have been put by someone else by the time
 		 * the function returns.  Make sure it doesn't go away
 		 * underneath us.
 		 */
 		kernfs_get(pos);
 
-		/*
-		 * Drain iff @kn was activated.  This avoids draining and
-		 * its lockdep annotations for nodes which have never been
-		 * activated and allows embedding kernfs_remove() in create
-		 * error paths without worrying about draining.
-		 */
-		if (kn->flags & KERNFS_ACTIVATED)
-			kernfs_drain(pos);
-		else
-			WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
+		kernfs_drain(pos);
 
 		/*
 		 * kernfs_unlink_sibling() succeeds once per node.  Use it
-- 
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 ` Tejun Heo [this message]
2022-09-09 14:00   ` [tip: sched/psi] kernfs: Improve kernfs_drain() and always call on removal tip-bot2 for Tejun Heo
2022-08-28  5:04 ` [PATCH 6/9] kernfs: Add KERNFS_REMOVING flags Tejun Heo
2022-09-09 14:00   ` [tip: sched/psi] " 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-6-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.