2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
7 #include <linux/slab.h>
8 #include <linux/workqueue.h>
10 #include "i915_active.h"
11 #include "gem/i915_gem_context.h"
12 #include "gem/i915_gem_object.h"
13 #include "i915_globals.h"
14 #include "i915_request.h"
15 #include "i915_scheduler.h"
18 static LIST_HEAD(globals);
20 static atomic_t active;
21 static atomic_t epoch;
22 static struct park_work {
27 static void i915_globals_shrink(void)
29 struct i915_global *global;
32 * kmem_cache_shrink() discards empty slabs and reorders partially
33 * filled slabs to prioritise allocating from the mostly full slabs,
34 * with the aim of reducing fragmentation.
36 list_for_each_entry(global, &globals, link)
40 static void __i915_globals_park(struct work_struct *work)
42 /* Confirm nothing woke up in the last grace period */
43 if (park.epoch == atomic_read(&epoch))
44 i915_globals_shrink();
47 void __init i915_global_register(struct i915_global *global)
49 GEM_BUG_ON(!global->shrink);
50 GEM_BUG_ON(!global->exit);
52 list_add_tail(&global->link, &globals);
55 static void __i915_globals_cleanup(void)
57 struct i915_global *global, *next;
59 list_for_each_entry_safe_reverse(global, next, &globals, link)
63 static __initconst int (* const initfn[])(void) = {
64 i915_global_active_init,
65 i915_global_buddy_init,
66 i915_global_context_init,
67 i915_global_gem_context_init,
68 i915_global_objects_init,
69 i915_global_request_init,
70 i915_global_scheduler_init,
74 int __init i915_globals_init(void)
78 for (i = 0; i < ARRAY_SIZE(initfn); i++) {
83 __i915_globals_cleanup();
88 INIT_RCU_WORK(&park.work, __i915_globals_park);
92 void i915_globals_park(void)
95 * Defer shrinking the global slab caches (and other work) until
96 * after a RCU grace period has completed with no activity. This
97 * is to try and reduce the latency impact on the consumers caused
98 * by us shrinking the caches the same time as they are trying to
99 * allocate, with the assumption being that if we idle long enough
100 * for an RCU grace period to elapse since the last use, it is likely
101 * to be longer until we need the caches again.
103 if (!atomic_dec_and_test(&active))
106 park.epoch = atomic_inc_return(&epoch);
107 queue_rcu_work(system_wq, &park.work);
110 void i915_globals_unpark(void)
116 void __exit i915_globals_exit(void)
118 /* Flush any residual park_work */
120 flush_rcu_work(&park.work);
122 __i915_globals_cleanup();
124 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */