drm/i915: Use i915_global_register()
authorChris Wilson <chris@chris-wilson.co.uk>
Tue, 5 Mar 2019 21:38:30 +0000 (21:38 +0000)
committerChris Wilson <chris@chris-wilson.co.uk>
Wed, 6 Mar 2019 10:00:50 +0000 (10:00 +0000)
Rather than manually add every new global into each hook, use
i915_global_register() function and keep a list of registered globals to
invoke instead.

However, I haven't found a way for random drivers to add an .init table
to avoid having to manually add ourselves to i915_globals_init() each
time.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Link: https://patchwork.freedesktop.org/patch/msgid/20190305213830.18094-1-chris@chris-wilson.co.uk
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
14 files changed:
drivers/gpu/drm/i915/i915_active.c
drivers/gpu/drm/i915/i915_active.h
drivers/gpu/drm/i915/i915_gem_context.c
drivers/gpu/drm/i915/i915_gem_context.h
drivers/gpu/drm/i915/i915_gem_object.c
drivers/gpu/drm/i915/i915_gem_object.h
drivers/gpu/drm/i915/i915_globals.c
drivers/gpu/drm/i915/i915_globals.h
drivers/gpu/drm/i915/i915_request.c
drivers/gpu/drm/i915/i915_request.h
drivers/gpu/drm/i915/i915_scheduler.c
drivers/gpu/drm/i915/i915_scheduler.h
drivers/gpu/drm/i915/i915_vma.c
drivers/gpu/drm/i915/i915_vma.h

index d9f6471ac16cf32acef29b8f8149a674b4ca2eaa..863ae12707bac4b853655ef912da8eb2df920012 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "i915_drv.h"
 #include "i915_active.h"
+#include "i915_globals.h"
 
 #define BKL(ref) (&(ref)->i915->drm.struct_mutex)
 
@@ -17,6 +18,7 @@
  * nodes from a local slab cache to hopefully reduce the fragmentation.
  */
 static struct i915_global_active {
+       struct i915_global base;
        struct kmem_cache *slab_cache;
 } global;
 
@@ -285,21 +287,27 @@ void i915_active_retire_noop(struct i915_active_request *active,
 #include "selftests/i915_active.c"
 #endif
 
-int __init i915_global_active_init(void)
+static void i915_global_active_shrink(void)
 {
-       global.slab_cache = KMEM_CACHE(active_node, SLAB_HWCACHE_ALIGN);
-       if (!global.slab_cache)
-               return -ENOMEM;
-
-       return 0;
+       kmem_cache_shrink(global.slab_cache);
 }
 
-void i915_global_active_shrink(void)
+static void i915_global_active_exit(void)
 {
-       kmem_cache_shrink(global.slab_cache);
+       kmem_cache_destroy(global.slab_cache);
 }
 
-void i915_global_active_exit(void)
+static struct i915_global_active global = { {
+       .shrink = i915_global_active_shrink,
+       .exit = i915_global_active_exit,
+} };
+
+int __init i915_global_active_init(void)
 {
-       kmem_cache_destroy(global.slab_cache);
+       global.slab_cache = KMEM_CACHE(active_node, SLAB_HWCACHE_ALIGN);
+       if (!global.slab_cache)
+               return -ENOMEM;
+
+       i915_global_register(&global.base);
+       return 0;
 }
index 5fbd9102384beae4c4d7e5113da7906aaae4c6f3..8142a334b37b0ea001fea83833d8e41ace4a7634 100644 (file)
@@ -419,8 +419,4 @@ void i915_active_fini(struct i915_active *ref);
 static inline void i915_active_fini(struct i915_active *ref) { }
 #endif
 
-int i915_global_active_init(void);
-void i915_global_active_shrink(void);
-void i915_global_active_exit(void);
-
 #endif /* _I915_ACTIVE_H_ */
index 1e3211e909f1967cdc27936fb30cdc13fe698f55..b9f32194798208459cb787772f0052ec408e996d 100644 (file)
@@ -88,6 +88,7 @@
 #include <linux/log2.h>
 #include <drm/i915_drm.h>
 #include "i915_drv.h"
+#include "i915_globals.h"
 #include "i915_trace.h"
 #include "intel_lrc_reg.h"
 #include "intel_workarounds.h"
@@ -95,6 +96,7 @@
 #define ALL_L3_SLICES(dev) (1 << NUM_L3_SLICES(dev)) - 1
 
 static struct i915_global_context {
+       struct i915_global base;
        struct kmem_cache *slab_luts;
 } global;
 
@@ -1423,21 +1425,27 @@ out_unlock:
 #include "selftests/i915_gem_context.c"
 #endif
 
-int __init i915_global_context_init(void)
+static void i915_global_context_shrink(void)
 {
-       global.slab_luts = KMEM_CACHE(i915_lut_handle, 0);
-       if (!global.slab_luts)
-               return -ENOMEM;
-
-       return 0;
+       kmem_cache_shrink(global.slab_luts);
 }
 
-void i915_global_context_shrink(void)
+static void i915_global_context_exit(void)
 {
-       kmem_cache_shrink(global.slab_luts);
+       kmem_cache_destroy(global.slab_luts);
 }
 
-void i915_global_context_exit(void)
+static struct i915_global_context global = { {
+       .shrink = i915_global_context_shrink,
+       .exit = i915_global_context_exit,
+} };
+
+int __init i915_global_context_init(void)
 {
-       kmem_cache_destroy(global.slab_luts);
+       global.slab_luts = KMEM_CACHE(i915_lut_handle, 0);
+       if (!global.slab_luts)
+               return -ENOMEM;
+
+       i915_global_register(&global.base);
+       return 0;
 }
index be63666ffaac84e3a1acc9b9822b401c9fb678c8..2f9ef333acaa7e4162bb2bf0eea196e4deba526f 100644 (file)
@@ -411,8 +411,4 @@ void intel_context_init(struct intel_context *ce,
 struct i915_lut_handle *i915_lut_handle_alloc(void);
 void i915_lut_handle_free(struct i915_lut_handle *lut);
 
-int i915_global_context_init(void);
-void i915_global_context_shrink(void);
-void i915_global_context_exit(void);
-
 #endif /* !__I915_GEM_CONTEXT_H__ */
index 4aeb8c3b87e478fcac0c4a637bd9663d58f46e9a..ac6a5ab8458665ae24c5645188a90be293612516 100644 (file)
 
 #include "i915_drv.h"
 #include "i915_gem_object.h"
+#include "i915_globals.h"
 
 static struct i915_global_object {
+       struct i915_global base;
        struct kmem_cache *slab_objects;
 } global;
 
@@ -61,6 +63,21 @@ void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
                !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE);
 }
 
+static void i915_global_objects_shrink(void)
+{
+       kmem_cache_shrink(global.slab_objects);
+}
+
+static void i915_global_objects_exit(void)
+{
+       kmem_cache_destroy(global.slab_objects);
+}
+
+static struct i915_global_object global = { {
+       .shrink = i915_global_objects_shrink,
+       .exit = i915_global_objects_exit,
+} };
+
 int __init i915_global_objects_init(void)
 {
        global.slab_objects =
@@ -68,15 +85,6 @@ int __init i915_global_objects_init(void)
        if (!global.slab_objects)
                return -ENOMEM;
 
+       i915_global_register(&global.base);
        return 0;
 }
-
-void i915_global_objects_shrink(void)
-{
-       kmem_cache_shrink(global.slab_objects);
-}
-
-void i915_global_objects_exit(void)
-{
-       kmem_cache_destroy(global.slab_objects);
-}
index 0eaa2b3aeb62edce3e47f9543c1e652fbe40aecd..1a24dc97e4fdb0abe820e5fd3ffcb399bfc1ac2d 100644 (file)
@@ -502,8 +502,4 @@ void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
                                         unsigned int cache_level);
 void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj);
 
-int i915_global_objects_init(void);
-void i915_global_objects_shrink(void);
-void i915_global_objects_exit(void);
-
 #endif
index cfd0bc462f58403dd82f6c470511c959b1222443..1cf4e8bc8ec6fd7f7f56964a8d2a73129f47079b 100644 (file)
 #include "i915_scheduler.h"
 #include "i915_vma.h"
 
-int __init i915_globals_init(void)
+static LIST_HEAD(globals);
+
+void __init i915_global_register(struct i915_global *global)
 {
-       int err;
+       GEM_BUG_ON(!global->shrink);
+       GEM_BUG_ON(!global->exit);
 
-       err = i915_global_active_init();
-       if (err)
-               return err;
+       list_add_tail(&global->link, &globals);
+}
 
-       err = i915_global_context_init();
-       if (err)
-               goto err_active;
+static void __i915_globals_cleanup(void)
+{
+       struct i915_global *global, *next;
 
-       err = i915_global_objects_init();
-       if (err)
-               goto err_context;
+       list_for_each_entry_safe_reverse(global, next, &globals, link)
+               global->exit();
+}
 
-       err = i915_global_request_init();
-       if (err)
-               goto err_objects;
+static __initconst int (* const initfn[])(void) = {
+       i915_global_active_init,
+       i915_global_context_init,
+       i915_global_objects_init,
+       i915_global_request_init,
+       i915_global_scheduler_init,
+       i915_global_vma_init,
+};
 
-       err = i915_global_scheduler_init();
-       if (err)
-               goto err_request;
+int __init i915_globals_init(void)
+{
+       int i;
 
-       err = i915_global_vma_init();
-       if (err)
-               goto err_scheduler;
+       for (i = 0; i < ARRAY_SIZE(initfn); i++) {
+               int err;
 
-       return 0;
+               err = initfn[i]();
+               if (err) {
+                       __i915_globals_cleanup();
+                       return err;
+               }
+       }
 
-err_scheduler:
-       i915_global_scheduler_exit();
-err_request:
-       i915_global_request_exit();
-err_objects:
-       i915_global_objects_exit();
-err_context:
-       i915_global_context_exit();
-err_active:
-       i915_global_active_exit();
-       return err;
+       return 0;
 }
 
 static void i915_globals_shrink(void)
 {
+       struct i915_global *global;
+
        /*
         * kmem_cache_shrink() discards empty slabs and reorders partially
         * filled slabs to prioritise allocating from the mostly full slabs,
         * with the aim of reducing fragmentation.
         */
-       i915_global_active_shrink();
-       i915_global_context_shrink();
-       i915_global_objects_shrink();
-       i915_global_request_shrink();
-       i915_global_scheduler_shrink();
-       i915_global_vma_shrink();
+       list_for_each_entry(global, &globals, link)
+               global->shrink();
 }
 
 static atomic_t active;
@@ -128,12 +127,7 @@ void __exit i915_globals_exit(void)
        rcu_barrier();
        flush_scheduled_work();
 
-       i915_global_vma_exit();
-       i915_global_scheduler_exit();
-       i915_global_request_exit();
-       i915_global_objects_exit();
-       i915_global_context_exit();
-       i915_global_active_exit();
+       __i915_globals_cleanup();
 
        /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
        rcu_barrier();
index e468f0413a73bcde8ebc5ff901af123fd18a0a82..a45529022a42b4c43f525565075f51375600dc4d 100644 (file)
@@ -7,9 +7,28 @@
 #ifndef _I915_GLOBALS_H_
 #define _I915_GLOBALS_H_
 
+typedef void (*i915_global_func_t)(void);
+
+struct i915_global {
+       struct list_head link;
+
+       i915_global_func_t shrink;
+       i915_global_func_t exit;
+};
+
+void i915_global_register(struct i915_global *global);
+
 int i915_globals_init(void);
 void i915_globals_park(void);
 void i915_globals_unpark(void);
 void i915_globals_exit(void);
 
+/* constructors */
+int i915_global_active_init(void);
+int i915_global_context_init(void);
+int i915_global_objects_init(void);
+int i915_global_request_init(void);
+int i915_global_scheduler_init(void);
+int i915_global_vma_init(void);
+
 #endif /* _I915_GLOBALS_H_ */
index bcf3c1a155e2a5f9bead03b9a312ffb5c4ab01fe..f8a63495114c42c5d2597cb0cd17cded2511be38 100644 (file)
@@ -31,6 +31,7 @@
 
 #include "i915_drv.h"
 #include "i915_active.h"
+#include "i915_globals.h"
 #include "i915_reset.h"
 
 struct execute_cb {
@@ -40,6 +41,7 @@ struct execute_cb {
 };
 
 static struct i915_global_request {
+       struct i915_global base;
        struct kmem_cache *slab_requests;
        struct kmem_cache *slab_dependencies;
        struct kmem_cache *slab_execute_cbs;
@@ -1338,6 +1340,25 @@ void i915_retire_requests(struct drm_i915_private *i915)
 #include "selftests/i915_request.c"
 #endif
 
+static void i915_global_request_shrink(void)
+{
+       kmem_cache_shrink(global.slab_dependencies);
+       kmem_cache_shrink(global.slab_execute_cbs);
+       kmem_cache_shrink(global.slab_requests);
+}
+
+static void i915_global_request_exit(void)
+{
+       kmem_cache_destroy(global.slab_dependencies);
+       kmem_cache_destroy(global.slab_execute_cbs);
+       kmem_cache_destroy(global.slab_requests);
+}
+
+static struct i915_global_request global = { {
+       .shrink = i915_global_request_shrink,
+       .exit = i915_global_request_exit,
+} };
+
 int __init i915_global_request_init(void)
 {
        global.slab_requests = KMEM_CACHE(i915_request,
@@ -1360,6 +1381,7 @@ int __init i915_global_request_init(void)
        if (!global.slab_dependencies)
                goto err_execute_cbs;
 
+       i915_global_register(&global.base);
        return 0;
 
 err_execute_cbs:
@@ -1368,17 +1390,3 @@ err_requests:
        kmem_cache_destroy(global.slab_requests);
        return -ENOMEM;
 }
-
-void i915_global_request_shrink(void)
-{
-       kmem_cache_shrink(global.slab_dependencies);
-       kmem_cache_shrink(global.slab_execute_cbs);
-       kmem_cache_shrink(global.slab_requests);
-}
-
-void i915_global_request_exit(void)
-{
-       kmem_cache_destroy(global.slab_dependencies);
-       kmem_cache_destroy(global.slab_execute_cbs);
-       kmem_cache_destroy(global.slab_requests);
-}
index 4dd1dea1d1afe2d2e5c6bd8ae176a4ade03bb27a..8c8fa50106440922a701936e7b6797fe2f3c59df 100644 (file)
@@ -406,8 +406,4 @@ static inline void i915_request_mark_complete(struct i915_request *rq)
 
 void i915_retire_requests(struct drm_i915_private *i915);
 
-int i915_global_request_init(void);
-void i915_global_request_shrink(void);
-void i915_global_request_exit(void);
-
 #endif /* I915_REQUEST_H */
index 3f0a4d56bd37f5c0b56cc5a45140662da6bb76cd..e0f609d015647a4ee923f8597ec66c409f484c19 100644 (file)
@@ -7,10 +7,12 @@
 #include <linux/mutex.h>
 
 #include "i915_drv.h"
+#include "i915_globals.h"
 #include "i915_request.h"
 #include "i915_scheduler.h"
 
 static struct i915_global_scheduler {
+       struct i915_global base;
        struct kmem_cache *slab_dependencies;
        struct kmem_cache *slab_priorities;
 } global;
@@ -437,6 +439,23 @@ void __i915_priolist_free(struct i915_priolist *p)
        kmem_cache_free(global.slab_priorities, p);
 }
 
+static void i915_global_scheduler_shrink(void)
+{
+       kmem_cache_shrink(global.slab_dependencies);
+       kmem_cache_shrink(global.slab_priorities);
+}
+
+static void i915_global_scheduler_exit(void)
+{
+       kmem_cache_destroy(global.slab_dependencies);
+       kmem_cache_destroy(global.slab_priorities);
+}
+
+static struct i915_global_scheduler global = { {
+       .shrink = i915_global_scheduler_shrink,
+       .exit = i915_global_scheduler_exit,
+} };
+
 int __init i915_global_scheduler_init(void)
 {
        global.slab_dependencies = KMEM_CACHE(i915_dependency,
@@ -449,21 +468,10 @@ int __init i915_global_scheduler_init(void)
        if (!global.slab_priorities)
                goto err_priorities;
 
+       i915_global_register(&global.base);
        return 0;
 
 err_priorities:
        kmem_cache_destroy(global.slab_priorities);
        return -ENOMEM;
 }
-
-void i915_global_scheduler_shrink(void)
-{
-       kmem_cache_shrink(global.slab_dependencies);
-       kmem_cache_shrink(global.slab_priorities);
-}
-
-void i915_global_scheduler_exit(void)
-{
-       kmem_cache_destroy(global.slab_dependencies);
-       kmem_cache_destroy(global.slab_priorities);
-}
index 6ce450cf63fa89fefebdfecc8c549f1144d2331c..9a1d257f3d6e59e2743036678ae29ab00f16a385 100644 (file)
@@ -134,8 +134,4 @@ static inline void i915_priolist_free(struct i915_priolist *p)
                __i915_priolist_free(p);
 }
 
-int i915_global_scheduler_init(void);
-void i915_global_scheduler_shrink(void);
-void i915_global_scheduler_exit(void);
-
 #endif /* _I915_SCHEDULER_H_ */
index 757a33998bbf042000d78038013b29defb3afa14..36726392e737759c4ffc52840de6e745f2454082 100644 (file)
 #include "i915_vma.h"
 
 #include "i915_drv.h"
+#include "i915_globals.h"
 #include "intel_ringbuffer.h"
 #include "intel_frontbuffer.h"
 
 #include <drm/drm_gem.h>
 
 static struct i915_global_vma {
+       struct i915_global base;
        struct kmem_cache *slab_vmas;
 } global;
 
@@ -1054,21 +1056,27 @@ unpin:
 #include "selftests/i915_vma.c"
 #endif
 
-int __init i915_global_vma_init(void)
+static void i915_global_vma_shrink(void)
 {
-       global.slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
-       if (!global.slab_vmas)
-               return -ENOMEM;
-
-       return 0;
+       kmem_cache_shrink(global.slab_vmas);
 }
 
-void i915_global_vma_shrink(void)
+static void i915_global_vma_exit(void)
 {
-       kmem_cache_shrink(global.slab_vmas);
+       kmem_cache_destroy(global.slab_vmas);
 }
 
-void i915_global_vma_exit(void)
+static struct i915_global_vma global = { {
+       .shrink = i915_global_vma_shrink,
+       .exit = i915_global_vma_exit,
+} };
+
+int __init i915_global_vma_init(void)
 {
-       kmem_cache_destroy(global.slab_vmas);
+       global.slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
+       if (!global.slab_vmas)
+               return -ENOMEM;
+
+       i915_global_register(&global.base);
+       return 0;
 }
index 37f93358aa3c490594d4bdb4dc44af09786baeba..6eab70953a57f7c254da4f386da1a15de055a81f 100644 (file)
@@ -443,8 +443,4 @@ void i915_vma_parked(struct drm_i915_private *i915);
 struct i915_vma *i915_vma_alloc(void);
 void i915_vma_free(struct i915_vma *vma);
 
-int i915_global_vma_init(void);
-void i915_global_vma_shrink(void);
-void i915_global_vma_exit(void);
-
 #endif