2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
7 #include <linux/debugobjects.h>
9 #include "gt/intel_engine_pm.h"
10 #include "gt/intel_ring.h"
13 #include "i915_active.h"
14 #include "i915_globals.h"
17 * Active refs memory management
19 * To be more economical with memory, we reap all the i915_active trees as
20 * they idle (when we know the active requests are inactive) and allocate the
21 * nodes from a local slab cache to hopefully reduce the fragmentation.
23 static struct i915_global_active {
24 struct i915_global base;
25 struct kmem_cache *slab_cache;
29 struct i915_active_fence base;
30 struct i915_active *ref;
35 static inline struct active_node *
36 node_from_active(struct i915_active_fence *active)
38 return container_of(active, struct active_node, base);
41 #define take_preallocated_barriers(x) llist_del_all(&(x)->preallocated_barriers)
43 static inline bool is_barrier(const struct i915_active_fence *active)
45 return IS_ERR(rcu_access_pointer(active->fence));
48 static inline struct llist_node *barrier_to_ll(struct active_node *node)
50 GEM_BUG_ON(!is_barrier(&node->base));
51 return (struct llist_node *)&node->base.cb.node;
54 static inline struct intel_engine_cs *
55 __barrier_to_engine(struct active_node *node)
57 return (struct intel_engine_cs *)READ_ONCE(node->base.cb.node.prev);
60 static inline struct intel_engine_cs *
61 barrier_to_engine(struct active_node *node)
63 GEM_BUG_ON(!is_barrier(&node->base));
64 return __barrier_to_engine(node);
67 static inline struct active_node *barrier_from_ll(struct llist_node *x)
69 return container_of((struct list_head *)x,
70 struct active_node, base.cb.node);
73 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM) && IS_ENABLED(CONFIG_DEBUG_OBJECTS)
75 static void *active_debug_hint(void *addr)
77 struct i915_active *ref = addr;
79 return (void *)ref->active ?: (void *)ref->retire ?: (void *)ref;
82 static struct debug_obj_descr active_debug_desc = {
83 .name = "i915_active",
84 .debug_hint = active_debug_hint,
87 static void debug_active_init(struct i915_active *ref)
89 debug_object_init(ref, &active_debug_desc);
92 static void debug_active_activate(struct i915_active *ref)
94 spin_lock_irq(&ref->tree_lock);
95 if (!atomic_read(&ref->count)) /* before the first inc */
96 debug_object_activate(ref, &active_debug_desc);
97 spin_unlock_irq(&ref->tree_lock);
100 static void debug_active_deactivate(struct i915_active *ref)
102 lockdep_assert_held(&ref->tree_lock);
103 if (!atomic_read(&ref->count)) /* after the last dec */
104 debug_object_deactivate(ref, &active_debug_desc);
107 static void debug_active_fini(struct i915_active *ref)
109 debug_object_free(ref, &active_debug_desc);
112 static void debug_active_assert(struct i915_active *ref)
114 debug_object_assert_init(ref, &active_debug_desc);
119 static inline void debug_active_init(struct i915_active *ref) { }
120 static inline void debug_active_activate(struct i915_active *ref) { }
121 static inline void debug_active_deactivate(struct i915_active *ref) { }
122 static inline void debug_active_fini(struct i915_active *ref) { }
123 static inline void debug_active_assert(struct i915_active *ref) { }
128 __active_retire(struct i915_active *ref)
130 struct active_node *it, *n;
134 GEM_BUG_ON(i915_active_is_idle(ref));
136 /* return the unused nodes to our slabcache -- flushing the allocator */
137 if (!atomic_dec_and_lock_irqsave(&ref->count, &ref->tree_lock, flags))
140 GEM_BUG_ON(rcu_access_pointer(ref->excl.fence));
141 debug_active_deactivate(ref);
147 spin_unlock_irqrestore(&ref->tree_lock, flags);
149 /* After the final retire, the entire struct may be freed */
153 /* ... except if you wait on it, you must manage your own references! */
156 rbtree_postorder_for_each_entry_safe(it, n, &root, node) {
157 GEM_BUG_ON(i915_active_fence_isset(&it->base));
158 kmem_cache_free(global.slab_cache, it);
163 active_work(struct work_struct *wrk)
165 struct i915_active *ref = container_of(wrk, typeof(*ref), work);
167 GEM_BUG_ON(!atomic_read(&ref->count));
168 if (atomic_add_unless(&ref->count, -1, 1))
171 __active_retire(ref);
175 active_retire(struct i915_active *ref)
177 GEM_BUG_ON(!atomic_read(&ref->count));
178 if (atomic_add_unless(&ref->count, -1, 1))
181 if (ref->flags & I915_ACTIVE_RETIRE_SLEEPS) {
182 queue_work(system_unbound_wq, &ref->work);
186 __active_retire(ref);
190 node_retire(struct dma_fence *fence, struct dma_fence_cb *cb)
192 i915_active_fence_cb(fence, cb);
193 active_retire(container_of(cb, struct active_node, base.cb)->ref);
197 excl_retire(struct dma_fence *fence, struct dma_fence_cb *cb)
199 i915_active_fence_cb(fence, cb);
200 active_retire(container_of(cb, struct i915_active, excl.cb));
203 static struct i915_active_fence *
204 active_instance(struct i915_active *ref, struct intel_timeline *tl)
206 struct active_node *node, *prealloc;
207 struct rb_node **p, *parent;
208 u64 idx = tl->fence_context;
211 * We track the most recently used timeline to skip a rbtree search
212 * for the common case, under typical loads we never need the rbtree
213 * at all. We can reuse the last slot if it is empty, that is
214 * after the previous activity has been retired, or if it matches the
217 node = READ_ONCE(ref->cache);
218 if (node && node->timeline == idx)
221 /* Preallocate a replacement, just in case */
222 prealloc = kmem_cache_alloc(global.slab_cache, GFP_KERNEL);
226 spin_lock_irq(&ref->tree_lock);
227 GEM_BUG_ON(i915_active_is_idle(ref));
230 p = &ref->tree.rb_node;
234 node = rb_entry(parent, struct active_node, node);
235 if (node->timeline == idx) {
236 kmem_cache_free(global.slab_cache, prealloc);
240 if (node->timeline < idx)
241 p = &parent->rb_right;
243 p = &parent->rb_left;
247 __i915_active_fence_init(&node->base, &tl->mutex, NULL, node_retire);
249 node->timeline = idx;
251 rb_link_node(&node->node, parent, p);
252 rb_insert_color(&node->node, &ref->tree);
256 spin_unlock_irq(&ref->tree_lock);
258 BUILD_BUG_ON(offsetof(typeof(*node), base));
262 void __i915_active_init(struct i915_active *ref,
263 int (*active)(struct i915_active *ref),
264 void (*retire)(struct i915_active *ref),
265 struct lock_class_key *key)
269 debug_active_init(ref);
272 ref->active = active;
273 ref->retire = ptr_unpack_bits(retire, &bits, 2);
274 if (bits & I915_ACTIVE_MAY_SLEEP)
275 ref->flags |= I915_ACTIVE_RETIRE_SLEEPS;
277 spin_lock_init(&ref->tree_lock);
281 init_llist_head(&ref->preallocated_barriers);
282 atomic_set(&ref->count, 0);
283 __mutex_init(&ref->mutex, "i915_active", key);
284 __i915_active_fence_init(&ref->excl, &ref->mutex, NULL, excl_retire);
285 INIT_WORK(&ref->work, active_work);
288 static bool ____active_del_barrier(struct i915_active *ref,
289 struct active_node *node,
290 struct intel_engine_cs *engine)
293 struct llist_node *head = NULL, *tail = NULL;
294 struct llist_node *pos, *next;
296 GEM_BUG_ON(node->timeline != engine->kernel_context->timeline->fence_context);
299 * Rebuild the llist excluding our node. We may perform this
300 * outside of the kernel_context timeline mutex and so someone
301 * else may be manipulating the engine->barrier_tasks, in
302 * which case either we or they will be upset :)
304 * A second __active_del_barrier() will report failure to claim
305 * the active_node and the caller will just shrug and know not to
306 * claim ownership of its node.
308 * A concurrent i915_request_add_active_barriers() will miss adding
309 * any of the tasks, but we will try again on the next -- and since
310 * we are actively using the barrier, we know that there will be
311 * at least another opportunity when we idle.
313 llist_for_each_safe(pos, next, llist_del_all(&engine->barrier_tasks)) {
314 if (node == barrier_from_ll(pos)) {
325 llist_add_batch(head, tail, &engine->barrier_tasks);
331 __active_del_barrier(struct i915_active *ref, struct active_node *node)
333 return ____active_del_barrier(ref, node, barrier_to_engine(node));
336 int i915_active_ref(struct i915_active *ref,
337 struct intel_timeline *tl,
338 struct dma_fence *fence)
340 struct i915_active_fence *active;
343 lockdep_assert_held(&tl->mutex);
345 /* Prevent reaping in case we malloc/wait while building the tree */
346 err = i915_active_acquire(ref);
350 active = active_instance(ref, tl);
356 if (is_barrier(active)) { /* proto-node used by our idle barrier */
358 * This request is on the kernel_context timeline, and so
359 * we can use it to substitute for the pending idle-barrer
360 * request that we want to emit on the kernel_context.
362 __active_del_barrier(ref, node_from_active(active));
363 RCU_INIT_POINTER(active->fence, NULL);
364 atomic_dec(&ref->count);
366 if (!__i915_active_fence_set(active, fence))
367 atomic_inc(&ref->count);
370 i915_active_release(ref);
374 void i915_active_set_exclusive(struct i915_active *ref, struct dma_fence *f)
376 /* We expect the caller to manage the exclusive timeline ordering */
377 GEM_BUG_ON(i915_active_is_idle(ref));
380 * As we don't know which mutex the caller is using, we told a small
381 * lie to the debug code that it is using the i915_active.mutex;
382 * and now we must stick to that lie.
384 mutex_acquire(&ref->mutex.dep_map, 0, 0, _THIS_IP_);
385 if (!__i915_active_fence_set(&ref->excl, f))
386 atomic_inc(&ref->count);
387 mutex_release(&ref->mutex.dep_map, 0, _THIS_IP_);
390 bool i915_active_acquire_if_busy(struct i915_active *ref)
392 debug_active_assert(ref);
393 return atomic_add_unless(&ref->count, 1, 0);
396 int i915_active_acquire(struct i915_active *ref)
400 if (i915_active_acquire_if_busy(ref))
403 err = mutex_lock_interruptible(&ref->mutex);
407 if (!atomic_read(&ref->count) && ref->active)
408 err = ref->active(ref);
410 debug_active_activate(ref);
411 atomic_inc(&ref->count);
414 mutex_unlock(&ref->mutex);
419 void i915_active_release(struct i915_active *ref)
421 debug_active_assert(ref);
425 static void enable_signaling(struct i915_active_fence *active)
427 struct dma_fence *fence;
429 fence = i915_active_fence_get(active);
433 dma_fence_enable_sw_signaling(fence);
434 dma_fence_put(fence);
437 int i915_active_wait(struct i915_active *ref)
439 struct active_node *it, *n;
444 if (!i915_active_acquire_if_busy(ref))
447 /* Flush lazy signals */
448 enable_signaling(&ref->excl);
449 rbtree_postorder_for_each_entry_safe(it, n, &ref->tree, node) {
450 if (is_barrier(&it->base)) /* unconnected idle barrier */
453 enable_signaling(&it->base);
455 /* Any fence added after the wait begins will not be auto-signaled */
457 i915_active_release(ref);
461 if (wait_var_event_interruptible(ref, i915_active_is_idle(ref)))
467 int i915_request_await_active(struct i915_request *rq, struct i915_active *ref)
471 if (rcu_access_pointer(ref->excl.fence)) {
472 struct dma_fence *fence;
475 fence = dma_fence_get_rcu_safe(&ref->excl.fence);
478 err = i915_request_await_dma_fence(rq, fence);
479 dma_fence_put(fence);
483 /* In the future we may choose to await on all fences */
488 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
489 void i915_active_fini(struct i915_active *ref)
491 debug_active_fini(ref);
492 GEM_BUG_ON(atomic_read(&ref->count));
493 GEM_BUG_ON(work_pending(&ref->work));
494 GEM_BUG_ON(!RB_EMPTY_ROOT(&ref->tree));
495 mutex_destroy(&ref->mutex);
499 static inline bool is_idle_barrier(struct active_node *node, u64 idx)
501 return node->timeline == idx && !i915_active_fence_isset(&node->base);
504 static struct active_node *reuse_idle_barrier(struct i915_active *ref, u64 idx)
506 struct rb_node *prev, *p;
508 if (RB_EMPTY_ROOT(&ref->tree))
511 spin_lock_irq(&ref->tree_lock);
512 GEM_BUG_ON(i915_active_is_idle(ref));
515 * Try to reuse any existing barrier nodes already allocated for this
516 * i915_active, due to overlapping active phases there is likely a
517 * node kept alive (as we reuse before parking). We prefer to reuse
518 * completely idle barriers (less hassle in manipulating the llists),
519 * but otherwise any will do.
521 if (ref->cache && is_idle_barrier(ref->cache, idx)) {
522 p = &ref->cache->node;
527 p = ref->tree.rb_node;
529 struct active_node *node =
530 rb_entry(p, struct active_node, node);
532 if (is_idle_barrier(node, idx))
536 if (node->timeline < idx)
543 * No quick match, but we did find the leftmost rb_node for the
544 * kernel_context. Walk the rb_tree in-order to see if there were
545 * any idle-barriers on this timeline that we missed, or just use
546 * the first pending barrier.
548 for (p = prev; p; p = rb_next(p)) {
549 struct active_node *node =
550 rb_entry(p, struct active_node, node);
551 struct intel_engine_cs *engine;
553 if (node->timeline > idx)
556 if (node->timeline < idx)
559 if (is_idle_barrier(node, idx))
563 * The list of pending barriers is protected by the
564 * kernel_context timeline, which notably we do not hold
565 * here. i915_request_add_active_barriers() may consume
566 * the barrier before we claim it, so we have to check
569 engine = __barrier_to_engine(node);
570 smp_rmb(); /* serialise with add_active_barriers */
571 if (is_barrier(&node->base) &&
572 ____active_del_barrier(ref, node, engine))
576 spin_unlock_irq(&ref->tree_lock);
581 rb_erase(p, &ref->tree); /* Hide from waits and sibling allocations */
582 if (p == &ref->cache->node)
584 spin_unlock_irq(&ref->tree_lock);
586 return rb_entry(p, struct active_node, node);
589 int i915_active_acquire_preallocate_barrier(struct i915_active *ref,
590 struct intel_engine_cs *engine)
592 intel_engine_mask_t tmp, mask = engine->mask;
593 struct intel_gt *gt = engine->gt;
594 struct llist_node *pos, *next;
597 GEM_BUG_ON(i915_active_is_idle(ref));
598 GEM_BUG_ON(!llist_empty(&ref->preallocated_barriers));
601 * Preallocate a node for each physical engine supporting the target
602 * engine (remember virtual engines have more than one sibling).
603 * We can then use the preallocated nodes in
604 * i915_active_acquire_barrier()
606 for_each_engine_masked(engine, gt, mask, tmp) {
607 u64 idx = engine->kernel_context->timeline->fence_context;
608 struct active_node *node;
610 node = reuse_idle_barrier(ref, idx);
612 node = kmem_cache_alloc(global.slab_cache, GFP_KERNEL);
618 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
620 &engine->kernel_context->timeline->mutex;
622 RCU_INIT_POINTER(node->base.fence, NULL);
623 node->base.cb.func = node_retire;
624 node->timeline = idx;
628 if (!i915_active_fence_isset(&node->base)) {
630 * Mark this as being *our* unconnected proto-node.
632 * Since this node is not in any list, and we have
633 * decoupled it from the rbtree, we can reuse the
634 * request to indicate this is an idle-barrier node
635 * and then we can use the rb_node and list pointers
636 * for our tracking of the pending barrier.
638 RCU_INIT_POINTER(node->base.fence, ERR_PTR(-EAGAIN));
639 node->base.cb.node.prev = (void *)engine;
640 atomic_inc(&ref->count);
643 GEM_BUG_ON(barrier_to_engine(node) != engine);
644 llist_add(barrier_to_ll(node), &ref->preallocated_barriers);
645 intel_engine_pm_get(engine);
651 llist_for_each_safe(pos, next, take_preallocated_barriers(ref)) {
652 struct active_node *node = barrier_from_ll(pos);
654 atomic_dec(&ref->count);
655 intel_engine_pm_put(barrier_to_engine(node));
657 kmem_cache_free(global.slab_cache, node);
662 void i915_active_acquire_barrier(struct i915_active *ref)
664 struct llist_node *pos, *next;
667 GEM_BUG_ON(i915_active_is_idle(ref));
670 * Transfer the list of preallocated barriers into the
671 * i915_active rbtree, but only as proto-nodes. They will be
672 * populated by i915_request_add_active_barriers() to point to the
673 * request that will eventually release them.
675 spin_lock_irqsave_nested(&ref->tree_lock, flags, SINGLE_DEPTH_NESTING);
676 llist_for_each_safe(pos, next, take_preallocated_barriers(ref)) {
677 struct active_node *node = barrier_from_ll(pos);
678 struct intel_engine_cs *engine = barrier_to_engine(node);
679 struct rb_node **p, *parent;
682 p = &ref->tree.rb_node;
684 struct active_node *it;
688 it = rb_entry(parent, struct active_node, node);
689 if (it->timeline < node->timeline)
690 p = &parent->rb_right;
692 p = &parent->rb_left;
694 rb_link_node(&node->node, parent, p);
695 rb_insert_color(&node->node, &ref->tree);
697 GEM_BUG_ON(!intel_engine_pm_is_awake(engine));
698 llist_add(barrier_to_ll(node), &engine->barrier_tasks);
699 intel_engine_pm_put(engine);
701 spin_unlock_irqrestore(&ref->tree_lock, flags);
704 void i915_request_add_active_barriers(struct i915_request *rq)
706 struct intel_engine_cs *engine = rq->engine;
707 struct llist_node *node, *next;
710 GEM_BUG_ON(intel_engine_is_virtual(engine));
711 GEM_BUG_ON(i915_request_timeline(rq) != engine->kernel_context->timeline);
713 node = llist_del_all(&engine->barrier_tasks);
717 * Attach the list of proto-fences to the in-flight request such
718 * that the parent i915_active will be released when this request
721 spin_lock_irqsave(&rq->lock, flags);
722 llist_for_each_safe(node, next, node) {
723 RCU_INIT_POINTER(barrier_from_ll(node)->base.fence, &rq->fence);
724 smp_wmb(); /* serialise with reuse_idle_barrier */
725 list_add_tail((struct list_head *)node, &rq->fence.cb_list);
727 spin_unlock_irqrestore(&rq->lock, flags);
730 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
731 #define active_is_held(active) lockdep_is_held((active)->lock)
733 #define active_is_held(active) true
737 * __i915_active_fence_set: Update the last active fence along its timeline
738 * @active: the active tracker
739 * @fence: the new fence (under construction)
741 * Records the new @fence as the last active fence along its timeline in
742 * this active tracker, moving the tracking callbacks from the previous
743 * fence onto this one. Returns the previous fence (if not already completed),
744 * which the caller must ensure is executed before the new fence. To ensure
745 * that the order of fences within the timeline of the i915_active_fence is
746 * maintained, it must be locked by the caller.
749 __i915_active_fence_set(struct i915_active_fence *active,
750 struct dma_fence *fence)
752 struct dma_fence *prev;
755 /* NB: must be serialised by an outer timeline mutex (active->lock) */
756 spin_lock_irqsave(fence->lock, flags);
757 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags));
759 prev = rcu_dereference_protected(active->fence, active_is_held(active));
761 GEM_BUG_ON(prev == fence);
762 spin_lock_nested(prev->lock, SINGLE_DEPTH_NESTING);
763 __list_del_entry(&active->cb.node);
764 spin_unlock(prev->lock); /* serialise with prev->cb_list */
767 * active->fence is reset by the callback from inside
768 * interrupt context. We need to serialise our list
769 * manipulation with the fence->lock to prevent the prev
770 * being lost inside an interrupt (it can't be replaced as
771 * no other caller is allowed to enter __i915_active_fence_set
772 * as we hold the timeline lock). After serialising with
773 * the callback, we need to double check which ran first,
774 * our list_del() [decoupling prev from the callback] or
777 prev = rcu_access_pointer(active->fence);
780 rcu_assign_pointer(active->fence, fence);
781 list_add_tail(&active->cb.node, &fence->cb_list);
783 spin_unlock_irqrestore(fence->lock, flags);
788 int i915_active_fence_set(struct i915_active_fence *active,
789 struct i915_request *rq)
791 struct dma_fence *fence;
794 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
795 lockdep_assert_held(active->lock);
798 /* Must maintain timeline ordering wrt previous active requests */
800 fence = __i915_active_fence_set(active, &rq->fence);
801 if (fence) /* but the previous fence may not belong to that timeline! */
802 fence = dma_fence_get_rcu(fence);
805 err = i915_request_await_dma_fence(rq, fence);
806 dma_fence_put(fence);
812 void i915_active_noop(struct dma_fence *fence, struct dma_fence_cb *cb)
814 i915_active_fence_cb(fence, cb);
817 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
818 #include "selftests/i915_active.c"
821 static void i915_global_active_shrink(void)
823 kmem_cache_shrink(global.slab_cache);
826 static void i915_global_active_exit(void)
828 kmem_cache_destroy(global.slab_cache);
831 static struct i915_global_active global = { {
832 .shrink = i915_global_active_shrink,
833 .exit = i915_global_active_exit,
836 int __init i915_global_active_init(void)
838 global.slab_cache = KMEM_CACHE(active_node, SLAB_HWCACHE_ALIGN);
839 if (!global.slab_cache)
842 i915_global_register(&global.base);