kernfs: Implement kernfs_show()
authorTejun Heo <tj@kernel.org>
Sun, 28 Aug 2022 05:04:39 +0000 (19:04 -1000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 1 Sep 2022 16:08:44 +0000 (18:08 +0200)
Currently, kernfs nodes can be created hidden and activated later by calling
kernfs_activate() to allow creation of multiple nodes to succeed or fail as
a unit. This is an one-way one-time-only transition. This patch introduces
kernfs_show() which can toggle visibility dynamically.

As the currently proposed use - toggling the cgroup pressure files - only
requires operating on leaf nodes, for the sake of simplicity, restrict it as
such for now.

Hiding uses the same mechanism as deactivation and likewise guarantees that
there are no in-flight operations on completion. KERNFS_ACTIVATED and
KERNFS_HIDDEN are used to manage the interactions between activations and
show/hide operations. A node is visible iff both activated & !hidden.

Cc: Chengming Zhou <zhouchengming@bytedance.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Tested-by: Chengming Zhou <zhouchengming@bytedance.com>
Reviewed-by: Chengming Zhou <zhouchengming@bytedance.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
Link: https://lore.kernel.org/r/20220828050440.734579-9-tj@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/kernfs/dir.c
include/linux/kernfs.h

index c9323956c63c84aba84c1de03f99b6b3589e9016..7fb5a72cc96d637d0c8b722e6b89bdccb8b8b169 100644 (file)
@@ -1311,7 +1311,7 @@ static void kernfs_activate_one(struct kernfs_node *kn)
 
        kn->flags |= KERNFS_ACTIVATED;
 
-       if (kernfs_active(kn) || (kn->flags & KERNFS_REMOVING))
+       if (kernfs_active(kn) || (kn->flags & (KERNFS_HIDDEN | KERNFS_REMOVING)))
                return;
 
        WARN_ON_ONCE(kn->parent && RB_EMPTY_NODE(&kn->rb));
@@ -1347,6 +1347,41 @@ void kernfs_activate(struct kernfs_node *kn)
        up_write(&root->kernfs_rwsem);
 }
 
+/**
+ * kernfs_show - show or hide a node
+ * @kn: kernfs_node to show or hide
+ * @show: whether to show or hide
+ *
+ * If @show is %false, @kn is marked hidden and deactivated. A hidden node is
+ * ignored in future activaitons. If %true, the mark is removed and activation
+ * state is restored. This function won't implicitly activate a new node in a
+ * %KERNFS_ROOT_CREATE_DEACTIVATED root which hasn't been activated yet.
+ *
+ * To avoid recursion complexities, directories aren't supported for now.
+ */
+void kernfs_show(struct kernfs_node *kn, bool show)
+{
+       struct kernfs_root *root = kernfs_root(kn);
+
+       if (WARN_ON_ONCE(kernfs_type(kn) == KERNFS_DIR))
+               return;
+
+       down_write(&root->kernfs_rwsem);
+
+       if (show) {
+               kn->flags &= ~KERNFS_HIDDEN;
+               if (kn->flags & KERNFS_ACTIVATED)
+                       kernfs_activate_one(kn);
+       } else {
+               kn->flags |= KERNFS_HIDDEN;
+               if (kernfs_active(kn))
+                       atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
+               kernfs_drain(kn);
+       }
+
+       up_write(&root->kernfs_rwsem);
+}
+
 static void __kernfs_remove(struct kernfs_node *kn)
 {
        struct kernfs_node *pos;
index b77d257c1f7e5d2239f51bd00b57e99a6a14f142..73f5c120def88bd8d2136c64a2fbbe4d0494b54a 100644 (file)
@@ -108,6 +108,7 @@ enum kernfs_node_flag {
        KERNFS_HAS_SEQ_SHOW     = 0x0040,
        KERNFS_HAS_MMAP         = 0x0080,
        KERNFS_LOCKDEP          = 0x0100,
+       KERNFS_HIDDEN           = 0x0200,
        KERNFS_SUICIDAL         = 0x0400,
        KERNFS_SUICIDED         = 0x0800,
        KERNFS_EMPTY_DIR        = 0x1000,
@@ -430,6 +431,7 @@ struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
                                       const char *name,
                                       struct kernfs_node *target);
 void kernfs_activate(struct kernfs_node *kn);
+void kernfs_show(struct kernfs_node *kn, bool show);
 void kernfs_remove(struct kernfs_node *kn);
 void kernfs_break_active_protection(struct kernfs_node *kn);
 void kernfs_unbreak_active_protection(struct kernfs_node *kn);