7fd1b3945a0493149671f9027247a9943a941f1a
[linux-2.6-block.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_globals.h"
12 #include "i915_request.h"
13 #include "i915_scheduler.h"
14
15 int __init i915_globals_init(void)
16 {
17         int err;
18
19         err = i915_global_active_init();
20         if (err)
21                 return err;
22
23         err = i915_global_request_init();
24         if (err)
25                 goto err_active;
26
27         err = i915_global_scheduler_init();
28         if (err)
29                 goto err_request;
30
31         return 0;
32
33 err_request:
34         i915_global_request_exit();
35 err_active:
36         i915_global_active_exit();
37         return err;
38 }
39
40 static void i915_globals_shrink(void)
41 {
42         /*
43          * kmem_cache_shrink() discards empty slabs and reorders partially
44          * filled slabs to prioritise allocating from the mostly full slabs,
45          * with the aim of reducing fragmentation.
46          */
47         i915_global_active_shrink();
48         i915_global_request_shrink();
49         i915_global_scheduler_shrink();
50 }
51
52 static atomic_t active;
53 static atomic_t epoch;
54 struct park_work {
55         struct rcu_work work;
56         int epoch;
57 };
58
59 static void __i915_globals_park(struct work_struct *work)
60 {
61         struct park_work *wrk = container_of(work, typeof(*wrk), work.work);
62
63         /* Confirm nothing woke up in the last grace period */
64         if (wrk->epoch == atomic_read(&epoch))
65                 i915_globals_shrink();
66
67         kfree(wrk);
68 }
69
70 void i915_globals_park(void)
71 {
72         struct park_work *wrk;
73
74         /*
75          * Defer shrinking the global slab caches (and other work) until
76          * after a RCU grace period has completed with no activity. This
77          * is to try and reduce the latency impact on the consumers caused
78          * by us shrinking the caches the same time as they are trying to
79          * allocate, with the assumption being that if we idle long enough
80          * for an RCU grace period to elapse since the last use, it is likely
81          * to be longer until we need the caches again.
82          */
83         if (!atomic_dec_and_test(&active))
84                 return;
85
86         wrk = kmalloc(sizeof(*wrk), GFP_KERNEL);
87         if (!wrk)
88                 return;
89
90         wrk->epoch = atomic_inc_return(&epoch);
91         INIT_RCU_WORK(&wrk->work, __i915_globals_park);
92         queue_rcu_work(system_wq, &wrk->work);
93 }
94
95 void i915_globals_unpark(void)
96 {
97         atomic_inc(&epoch);
98         atomic_inc(&active);
99 }
100
101 void __exit i915_globals_exit(void)
102 {
103         /* Flush any residual park_work */
104         rcu_barrier();
105         flush_scheduled_work();
106
107         i915_global_scheduler_exit();
108         i915_global_request_exit();
109         i915_global_active_exit();
110
111         /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
112         rcu_barrier();
113 }