Drivers or other kernel subsystems may allow subsystem-specific policy and
configuration to be applied to cgroups.  If these subsystems track private data
on a per-cgroup basis, they need a way to be notified about cgroup destruction
so that they can clean up their own internal data for that cgroup.  Let's add a
blocking_notifier that will be called whenever a cgroup in the v2 hierarchy is
destroyed to allow this cleanup.

I'm arbitrarily restricting this behavior to the default (cgroup-v2) hierarchy
for now since I don't anticipate it being terribly useful on the legacy
hierarchies.  We can certainly relax that restriction if someone comes up with
a use case that needs this on the v1 hierarchies.

Cc: Tejun Heo <t...@kernel.org>
Cc: cgro...@vger.kernel.org
Signed-off-by: Matt Roper <matthew.d.ro...@intel.com>
---
 include/linux/cgroup.h |  3 +++
 kernel/cgroup/cgroup.c | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 473e0c0abb86..c9896027ed99 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -43,6 +43,9 @@
 /* walk all threaded css_sets in the domain */
 #define CSS_TASK_ITER_THREADED         (1U << 1)
 
+/* Driver-registered callbacks for cgroup destruction */
+extern struct blocking_notifier_head cgroup_destroy_notifier_list;
+
 /* a css_task_iter should be treated as an opaque object */
 struct css_task_iter {
        struct cgroup_subsys            *ss;
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 2cf06c274e4c..f12c32c6ec7f 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -75,6 +75,15 @@
 DEFINE_MUTEX(cgroup_mutex);
 DEFINE_SPINLOCK(css_set_lock);
 
+/*
+ * Driver-registered callbacks for cgroup destruction.  Drivers may wish to
+ * track their own per-cgroup data.  Registering a callback on this list will
+ * allow them to detect cgroup destruction and perform any appropriate cleanup
+ * of that data when the cgroup is destroyed.
+ */
+BLOCKING_NOTIFIER_HEAD(cgroup_destroy_notifier_list);
+EXPORT_SYMBOL_GPL(cgroup_destroy_notifier_list);
+
 #ifdef CONFIG_PROVE_RCU
 EXPORT_SYMBOL_GPL(cgroup_mutex);
 EXPORT_SYMBOL_GPL(css_set_lock);
@@ -5086,6 +5095,15 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
        for_each_css(css, ssid, cgrp)
                kill_css(css);
 
+       /*
+        * Notify listeners of cgroup destruction on the default hierarchy.
+        * Drivers that store per-cgroup data may register for callback to know
+        * when it's safe to reap that data.
+        */
+       if (cgroup_on_dfl(cgrp))
+               blocking_notifier_call_chain(&cgroup_destroy_notifier_list,
+                                            0, cgrp);
+
        /*
         * Remove @cgrp directory along with the base files.  @cgrp has an
         * extra ref on its kn.
-- 
2.14.3

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

Reply via email to