Merge tag '9p-for-5.3' of git://github.com/martinetd/linux
[linux-2.6-microblaze.git] / drivers / gpu / drm / i915 / i915_globals.c
1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2019 Intel Corporation
5  */
6
7 #include <linux/slab.h>
8 #include <linux/workqueue.h>
9
10 #include "i915_active.h"
11 #include "i915_gem_context.h"
12 #include "i915_gem_object.h"
13 #include "i915_globals.h"
14 #include "i915_request.h"
15 #include "i915_scheduler.h"
16 #include "i915_vma.h"
17
18 static LIST_HEAD(globals);
19
20 static atomic_t active;
21 static atomic_t epoch;
22 static struct park_work {
23         struct rcu_work work;
24         int epoch;
25 } park;
26
27 static void i915_globals_shrink(void)
28 {
29         struct i915_global *global;
30
31         /*
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.
35          */
36         list_for_each_entry(global, &globals, link)
37                 global->shrink();
38 }
39
40 static void __i915_globals_park(struct work_struct *work)
41 {
42         /* Confirm nothing woke up in the last grace period */
43         if (park.epoch == atomic_read(&epoch))
44                 i915_globals_shrink();
45 }
46
47 void __init i915_global_register(struct i915_global *global)
48 {
49         GEM_BUG_ON(!global->shrink);
50         GEM_BUG_ON(!global->exit);
51
52         list_add_tail(&global->link, &globals);
53 }
54
55 static void __i915_globals_cleanup(void)
56 {
57         struct i915_global *global, *next;
58
59         list_for_each_entry_safe_reverse(global, next, &globals, link)
60                 global->exit();
61 }
62
63 static __initconst int (* const initfn[])(void) = {
64         i915_global_active_init,
65         i915_global_context_init,
66         i915_global_gem_context_init,
67         i915_global_objects_init,
68         i915_global_request_init,
69         i915_global_scheduler_init,
70         i915_global_vma_init,
71 };
72
73 int __init i915_globals_init(void)
74 {
75         int i;
76
77         for (i = 0; i < ARRAY_SIZE(initfn); i++) {
78                 int err;
79
80                 err = initfn[i]();
81                 if (err) {
82                         __i915_globals_cleanup();
83                         return err;
84                 }
85         }
86
87         INIT_RCU_WORK(&park.work, __i915_globals_park);
88         return 0;
89 }
90
91 void i915_globals_park(void)
92 {
93         /*
94          * Defer shrinking the global slab caches (and other work) until
95          * after a RCU grace period has completed with no activity. This
96          * is to try and reduce the latency impact on the consumers caused
97          * by us shrinking the caches the same time as they are trying to
98          * allocate, with the assumption being that if we idle long enough
99          * for an RCU grace period to elapse since the last use, it is likely
100          * to be longer until we need the caches again.
101          */
102         if (!atomic_dec_and_test(&active))
103                 return;
104
105         park.epoch = atomic_inc_return(&epoch);
106         queue_rcu_work(system_wq, &park.work);
107 }
108
109 void i915_globals_unpark(void)
110 {
111         atomic_inc(&epoch);
112         atomic_inc(&active);
113 }
114
115 void __exit i915_globals_exit(void)
116 {
117         /* Flush any residual park_work */
118         atomic_inc(&epoch);
119         flush_rcu_work(&park.work);
120
121         __i915_globals_cleanup();
122
123         /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
124         rcu_barrier();
125 }