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 "i915_globals.h"
12 #include "i915_request.h"
13 #include "i915_scheduler.h"
15 int __init i915_globals_init(void)
19 err = i915_global_active_init();
23 err = i915_global_request_init();
27 err = i915_global_scheduler_init();
34 i915_global_request_exit();
36 i915_global_active_exit();
40 static void i915_globals_shrink(void)
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.
47 i915_global_active_shrink();
48 i915_global_request_shrink();
49 i915_global_scheduler_shrink();
52 static atomic_t active;
53 static atomic_t epoch;
59 static void __i915_globals_park(struct work_struct *work)
61 struct park_work *wrk = container_of(work, typeof(*wrk), work.work);
63 /* Confirm nothing woke up in the last grace period */
64 if (wrk->epoch == atomic_read(&epoch))
65 i915_globals_shrink();
70 void i915_globals_park(void)
72 struct park_work *wrk;
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.
83 if (!atomic_dec_and_test(&active))
86 wrk = kmalloc(sizeof(*wrk), GFP_KERNEL);
90 wrk->epoch = atomic_inc_return(&epoch);
91 INIT_RCU_WORK(&wrk->work, __i915_globals_park);
92 queue_rcu_work(system_wq, &wrk->work);
95 void i915_globals_unpark(void)
101 void __exit i915_globals_exit(void)
103 /* Flush any residual park_work */
105 flush_scheduled_work();
107 i915_global_scheduler_exit();
108 i915_global_request_exit();
109 i915_global_active_exit();
111 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */