2019-02-28 18:20:33 +08:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
* Copyright © 2019 Intel Corporation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/workqueue.h>
|
|
|
|
|
|
|
|
#include "i915_active.h"
|
2019-05-28 17:29:49 +08:00
|
|
|
#include "gem/i915_gem_context.h"
|
2019-05-28 17:29:44 +08:00
|
|
|
#include "gem/i915_gem_object.h"
|
2019-02-28 18:20:33 +08:00
|
|
|
#include "i915_globals.h"
|
|
|
|
#include "i915_request.h"
|
|
|
|
#include "i915_scheduler.h"
|
2019-02-28 18:20:34 +08:00
|
|
|
#include "i915_vma.h"
|
2019-02-28 18:20:33 +08:00
|
|
|
|
2019-03-06 05:38:30 +08:00
|
|
|
static LIST_HEAD(globals);
|
|
|
|
|
2019-04-08 17:17:02 +08:00
|
|
|
static atomic_t active;
|
|
|
|
static atomic_t epoch;
|
|
|
|
static struct park_work {
|
2019-12-18 17:40:57 +08:00
|
|
|
struct delayed_work work;
|
|
|
|
struct rcu_head rcu;
|
|
|
|
unsigned long flags;
|
|
|
|
#define PENDING 0
|
2019-04-08 17:17:02 +08:00
|
|
|
int epoch;
|
|
|
|
} park;
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
list_for_each_entry(global, &globals, link)
|
|
|
|
global->shrink();
|
|
|
|
}
|
|
|
|
|
2019-12-18 17:40:57 +08:00
|
|
|
static void __i915_globals_grace(struct rcu_head *rcu)
|
|
|
|
{
|
|
|
|
/* Ratelimit parking as shrinking is quite slow */
|
|
|
|
schedule_delayed_work(&park.work, round_jiffies_up_relative(2 * HZ));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __i915_globals_queue_rcu(void)
|
|
|
|
{
|
|
|
|
park.epoch = atomic_inc_return(&epoch);
|
|
|
|
if (!atomic_read(&active)) {
|
|
|
|
init_rcu_head(&park.rcu);
|
|
|
|
call_rcu(&park.rcu, __i915_globals_grace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:17:02 +08:00
|
|
|
static void __i915_globals_park(struct work_struct *work)
|
|
|
|
{
|
2019-12-18 17:40:57 +08:00
|
|
|
destroy_rcu_head(&park.rcu);
|
|
|
|
|
2019-04-08 17:17:02 +08:00
|
|
|
/* Confirm nothing woke up in the last grace period */
|
2019-12-18 17:40:57 +08:00
|
|
|
if (park.epoch != atomic_read(&epoch)) {
|
|
|
|
__i915_globals_queue_rcu();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clear_bit(PENDING, &park.flags);
|
|
|
|
i915_globals_shrink();
|
2019-04-08 17:17:02 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 05:38:30 +08:00
|
|
|
void __init i915_global_register(struct i915_global *global)
|
2019-02-28 18:20:33 +08:00
|
|
|
{
|
2019-03-06 05:38:30 +08:00
|
|
|
GEM_BUG_ON(!global->shrink);
|
|
|
|
GEM_BUG_ON(!global->exit);
|
2019-02-28 18:20:33 +08:00
|
|
|
|
2019-03-06 05:38:30 +08:00
|
|
|
list_add_tail(&global->link, &globals);
|
|
|
|
}
|
2019-02-28 18:20:33 +08:00
|
|
|
|
2019-03-06 05:38:30 +08:00
|
|
|
static void __i915_globals_cleanup(void)
|
|
|
|
{
|
|
|
|
struct i915_global *global, *next;
|
2019-02-28 18:20:33 +08:00
|
|
|
|
2019-03-06 05:38:30 +08:00
|
|
|
list_for_each_entry_safe_reverse(global, next, &globals, link)
|
|
|
|
global->exit();
|
|
|
|
}
|
2019-02-28 18:20:34 +08:00
|
|
|
|
2019-03-06 05:38:30 +08:00
|
|
|
static __initconst int (* const initfn[])(void) = {
|
|
|
|
i915_global_active_init,
|
|
|
|
i915_global_context_init,
|
2019-03-08 21:25:19 +08:00
|
|
|
i915_global_gem_context_init,
|
2019-03-06 05:38:30 +08:00
|
|
|
i915_global_objects_init,
|
|
|
|
i915_global_request_init,
|
|
|
|
i915_global_scheduler_init,
|
|
|
|
i915_global_vma_init,
|
|
|
|
};
|
2019-02-28 18:20:34 +08:00
|
|
|
|
2019-03-06 05:38:30 +08:00
|
|
|
int __init i915_globals_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
2019-02-28 18:20:33 +08:00
|
|
|
|
2019-03-06 05:38:30 +08:00
|
|
|
for (i = 0; i < ARRAY_SIZE(initfn); i++) {
|
|
|
|
int err;
|
2019-02-28 18:20:34 +08:00
|
|
|
|
2019-03-06 05:38:30 +08:00
|
|
|
err = initfn[i]();
|
|
|
|
if (err) {
|
|
|
|
__i915_globals_cleanup();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2019-02-28 18:20:33 +08:00
|
|
|
|
2019-12-18 17:40:57 +08:00
|
|
|
INIT_DELAYED_WORK(&park.work, __i915_globals_park);
|
2019-03-06 05:38:30 +08:00
|
|
|
return 0;
|
2019-02-28 18:20:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void i915_globals_park(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Defer shrinking the global slab caches (and other work) until
|
|
|
|
* after a RCU grace period has completed with no activity. This
|
|
|
|
* is to try and reduce the latency impact on the consumers caused
|
|
|
|
* by us shrinking the caches the same time as they are trying to
|
|
|
|
* allocate, with the assumption being that if we idle long enough
|
|
|
|
* for an RCU grace period to elapse since the last use, it is likely
|
|
|
|
* to be longer until we need the caches again.
|
|
|
|
*/
|
|
|
|
if (!atomic_dec_and_test(&active))
|
|
|
|
return;
|
|
|
|
|
2019-12-18 17:40:57 +08:00
|
|
|
/* Queue cleanup after the next RCU grace period has freed slabs */
|
|
|
|
if (!test_and_set_bit(PENDING, &park.flags))
|
|
|
|
__i915_globals_queue_rcu();
|
2019-02-28 18:20:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void i915_globals_unpark(void)
|
|
|
|
{
|
|
|
|
atomic_inc(&epoch);
|
|
|
|
atomic_inc(&active);
|
|
|
|
}
|
|
|
|
|
2019-12-18 17:40:57 +08:00
|
|
|
static void __exit __i915_globals_flush(void)
|
|
|
|
{
|
|
|
|
atomic_inc(&active); /* skip shrinking */
|
|
|
|
|
|
|
|
rcu_barrier(); /* wait for the work to be queued */
|
|
|
|
flush_delayed_work(&park.work);
|
|
|
|
|
|
|
|
atomic_dec(&active);
|
|
|
|
}
|
|
|
|
|
2019-02-28 18:20:33 +08:00
|
|
|
void __exit i915_globals_exit(void)
|
|
|
|
{
|
2019-12-18 17:40:57 +08:00
|
|
|
GEM_BUG_ON(atomic_read(&active));
|
2019-02-28 18:20:33 +08:00
|
|
|
|
2019-12-18 17:40:57 +08:00
|
|
|
__i915_globals_flush();
|
2019-03-06 05:38:30 +08:00
|
|
|
__i915_globals_cleanup();
|
2019-02-28 18:20:33 +08:00
|
|
|
|
|
|
|
/* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
|
|
|
|
rcu_barrier();
|
|
|
|
}
|