i915_active.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. /*
  2. * SPDX-License-Identifier: MIT
  3. *
  4. * Copyright © 2019 Intel Corporation
  5. */
  6. #include <linux/debugobjects.h>
  7. #include "gt/intel_context.h"
  8. #include "gt/intel_engine_heartbeat.h"
  9. #include "gt/intel_engine_pm.h"
  10. #include "gt/intel_ring.h"
  11. #include "i915_drv.h"
  12. #include "i915_active.h"
  13. /*
  14. * Active refs memory management
  15. *
  16. * To be more economical with memory, we reap all the i915_active trees as
  17. * they idle (when we know the active requests are inactive) and allocate the
  18. * nodes from a local slab cache to hopefully reduce the fragmentation.
  19. */
  20. static struct kmem_cache *slab_cache;
  21. struct active_node {
  22. struct rb_node node;
  23. struct i915_active_fence base;
  24. struct i915_active *ref;
  25. u64 timeline;
  26. };
  27. #define fetch_node(x) rb_entry(READ_ONCE(x), typeof(struct active_node), node)
  28. static inline struct active_node *
  29. node_from_active(struct i915_active_fence *active)
  30. {
  31. return container_of(active, struct active_node, base);
  32. }
  33. #define take_preallocated_barriers(x) llist_del_all(&(x)->preallocated_barriers)
  34. static inline bool is_barrier(const struct i915_active_fence *active)
  35. {
  36. return IS_ERR(rcu_access_pointer(active->fence));
  37. }
  38. static inline struct llist_node *barrier_to_ll(struct active_node *node)
  39. {
  40. GEM_BUG_ON(!is_barrier(&node->base));
  41. return (struct llist_node *)&node->base.cb.node;
  42. }
  43. static inline struct intel_engine_cs *
  44. __barrier_to_engine(struct active_node *node)
  45. {
  46. return (struct intel_engine_cs *)READ_ONCE(node->base.cb.node.prev);
  47. }
  48. static inline struct intel_engine_cs *
  49. barrier_to_engine(struct active_node *node)
  50. {
  51. GEM_BUG_ON(!is_barrier(&node->base));
  52. return __barrier_to_engine(node);
  53. }
  54. static inline struct active_node *barrier_from_ll(struct llist_node *x)
  55. {
  56. return container_of((struct list_head *)x,
  57. struct active_node, base.cb.node);
  58. }
  59. #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM) && IS_ENABLED(CONFIG_DEBUG_OBJECTS)
  60. static void *active_debug_hint(void *addr)
  61. {
  62. struct i915_active *ref = addr;
  63. return (void *)ref->active ?: (void *)ref->retire ?: (void *)ref;
  64. }
  65. static const struct debug_obj_descr active_debug_desc = {
  66. .name = "i915_active",
  67. .debug_hint = active_debug_hint,
  68. };
  69. static void debug_active_init(struct i915_active *ref)
  70. {
  71. debug_object_init(ref, &active_debug_desc);
  72. }
  73. static void debug_active_activate(struct i915_active *ref)
  74. {
  75. lockdep_assert_held(&ref->tree_lock);
  76. debug_object_activate(ref, &active_debug_desc);
  77. }
  78. static void debug_active_deactivate(struct i915_active *ref)
  79. {
  80. lockdep_assert_held(&ref->tree_lock);
  81. if (!atomic_read(&ref->count)) /* after the last dec */
  82. debug_object_deactivate(ref, &active_debug_desc);
  83. }
  84. static void debug_active_fini(struct i915_active *ref)
  85. {
  86. debug_object_free(ref, &active_debug_desc);
  87. }
  88. static void debug_active_assert(struct i915_active *ref)
  89. {
  90. debug_object_assert_init(ref, &active_debug_desc);
  91. }
  92. #else
  93. static inline void debug_active_init(struct i915_active *ref) { }
  94. static inline void debug_active_activate(struct i915_active *ref) { }
  95. static inline void debug_active_deactivate(struct i915_active *ref) { }
  96. static inline void debug_active_fini(struct i915_active *ref) { }
  97. static inline void debug_active_assert(struct i915_active *ref) { }
  98. #endif
  99. static void
  100. __active_retire(struct i915_active *ref)
  101. {
  102. struct rb_root root = RB_ROOT;
  103. struct active_node *it, *n;
  104. unsigned long flags;
  105. GEM_BUG_ON(i915_active_is_idle(ref));
  106. /* return the unused nodes to our slabcache -- flushing the allocator */
  107. if (!atomic_dec_and_lock_irqsave(&ref->count, &ref->tree_lock, flags))
  108. return;
  109. GEM_BUG_ON(rcu_access_pointer(ref->excl.fence));
  110. debug_active_deactivate(ref);
  111. /* Even if we have not used the cache, we may still have a barrier */
  112. if (!ref->cache)
  113. ref->cache = fetch_node(ref->tree.rb_node);
  114. /* Keep the MRU cached node for reuse */
  115. if (ref->cache) {
  116. /* Discard all other nodes in the tree */
  117. rb_erase(&ref->cache->node, &ref->tree);
  118. root = ref->tree;
  119. /* Rebuild the tree with only the cached node */
  120. rb_link_node(&ref->cache->node, NULL, &ref->tree.rb_node);
  121. rb_insert_color(&ref->cache->node, &ref->tree);
  122. GEM_BUG_ON(ref->tree.rb_node != &ref->cache->node);
  123. /* Make the cached node available for reuse with any timeline */
  124. ref->cache->timeline = 0; /* needs cmpxchg(u64) */
  125. }
  126. spin_unlock_irqrestore(&ref->tree_lock, flags);
  127. /* After the final retire, the entire struct may be freed */
  128. if (ref->retire)
  129. ref->retire(ref);
  130. /* ... except if you wait on it, you must manage your own references! */
  131. wake_up_var(ref);
  132. /* Finally free the discarded timeline tree */
  133. rbtree_postorder_for_each_entry_safe(it, n, &root, node) {
  134. GEM_BUG_ON(i915_active_fence_isset(&it->base));
  135. kmem_cache_free(slab_cache, it);
  136. }
  137. }
  138. static void
  139. active_work(struct work_struct *wrk)
  140. {
  141. struct i915_active *ref = container_of(wrk, typeof(*ref), work);
  142. GEM_BUG_ON(!atomic_read(&ref->count));
  143. if (atomic_add_unless(&ref->count, -1, 1))
  144. return;
  145. __active_retire(ref);
  146. }
  147. static void
  148. active_retire(struct i915_active *ref)
  149. {
  150. GEM_BUG_ON(!atomic_read(&ref->count));
  151. if (atomic_add_unless(&ref->count, -1, 1))
  152. return;
  153. if (ref->flags & I915_ACTIVE_RETIRE_SLEEPS) {
  154. queue_work(system_unbound_wq, &ref->work);
  155. return;
  156. }
  157. __active_retire(ref);
  158. }
  159. static inline struct dma_fence **
  160. __active_fence_slot(struct i915_active_fence *active)
  161. {
  162. return (struct dma_fence ** __force)&active->fence;
  163. }
  164. static inline bool
  165. active_fence_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
  166. {
  167. struct i915_active_fence *active =
  168. container_of(cb, typeof(*active), cb);
  169. return cmpxchg(__active_fence_slot(active), fence, NULL) == fence;
  170. }
  171. static void
  172. node_retire(struct dma_fence *fence, struct dma_fence_cb *cb)
  173. {
  174. if (active_fence_cb(fence, cb))
  175. active_retire(container_of(cb, struct active_node, base.cb)->ref);
  176. }
  177. static void
  178. excl_retire(struct dma_fence *fence, struct dma_fence_cb *cb)
  179. {
  180. if (active_fence_cb(fence, cb))
  181. active_retire(container_of(cb, struct i915_active, excl.cb));
  182. }
  183. static struct active_node *__active_lookup(struct i915_active *ref, u64 idx)
  184. {
  185. struct active_node *it;
  186. GEM_BUG_ON(idx == 0); /* 0 is the unordered timeline, rsvd for cache */
  187. /*
  188. * We track the most recently used timeline to skip a rbtree search
  189. * for the common case, under typical loads we never need the rbtree
  190. * at all. We can reuse the last slot if it is empty, that is
  191. * after the previous activity has been retired, or if it matches the
  192. * current timeline.
  193. */
  194. it = READ_ONCE(ref->cache);
  195. if (it) {
  196. u64 cached = READ_ONCE(it->timeline);
  197. /* Once claimed, this slot will only belong to this idx */
  198. if (cached == idx)
  199. return it;
  200. /*
  201. * An unclaimed cache [.timeline=0] can only be claimed once.
  202. *
  203. * If the value is already non-zero, some other thread has
  204. * claimed the cache and we know that is does not match our
  205. * idx. If, and only if, the timeline is currently zero is it
  206. * worth competing to claim it atomically for ourselves (for
  207. * only the winner of that race will cmpxchg return the old
  208. * value of 0).
  209. */
  210. if (!cached && !cmpxchg64(&it->timeline, 0, idx))
  211. return it;
  212. }
  213. BUILD_BUG_ON(offsetof(typeof(*it), node));
  214. /* While active, the tree can only be built; not destroyed */
  215. GEM_BUG_ON(i915_active_is_idle(ref));
  216. it = fetch_node(ref->tree.rb_node);
  217. while (it) {
  218. if (it->timeline < idx) {
  219. it = fetch_node(it->node.rb_right);
  220. } else if (it->timeline > idx) {
  221. it = fetch_node(it->node.rb_left);
  222. } else {
  223. WRITE_ONCE(ref->cache, it);
  224. break;
  225. }
  226. }
  227. /* NB: If the tree rotated beneath us, we may miss our target. */
  228. return it;
  229. }
  230. static struct i915_active_fence *
  231. active_instance(struct i915_active *ref, u64 idx)
  232. {
  233. struct active_node *node;
  234. struct rb_node **p, *parent;
  235. node = __active_lookup(ref, idx);
  236. if (likely(node))
  237. return &node->base;
  238. spin_lock_irq(&ref->tree_lock);
  239. GEM_BUG_ON(i915_active_is_idle(ref));
  240. parent = NULL;
  241. p = &ref->tree.rb_node;
  242. while (*p) {
  243. parent = *p;
  244. node = rb_entry(parent, struct active_node, node);
  245. if (node->timeline == idx)
  246. goto out;
  247. if (node->timeline < idx)
  248. p = &parent->rb_right;
  249. else
  250. p = &parent->rb_left;
  251. }
  252. /*
  253. * XXX: We should preallocate this before i915_active_ref() is ever
  254. * called, but we cannot call into fs_reclaim() anyway, so use GFP_ATOMIC.
  255. */
  256. node = kmem_cache_alloc(slab_cache, GFP_ATOMIC);
  257. if (!node)
  258. goto out;
  259. __i915_active_fence_init(&node->base, NULL, node_retire);
  260. node->ref = ref;
  261. node->timeline = idx;
  262. rb_link_node(&node->node, parent, p);
  263. rb_insert_color(&node->node, &ref->tree);
  264. out:
  265. WRITE_ONCE(ref->cache, node);
  266. spin_unlock_irq(&ref->tree_lock);
  267. return &node->base;
  268. }
  269. void __i915_active_init(struct i915_active *ref,
  270. int (*active)(struct i915_active *ref),
  271. void (*retire)(struct i915_active *ref),
  272. unsigned long flags,
  273. struct lock_class_key *mkey,
  274. struct lock_class_key *wkey)
  275. {
  276. debug_active_init(ref);
  277. ref->flags = flags;
  278. ref->active = active;
  279. ref->retire = retire;
  280. spin_lock_init(&ref->tree_lock);
  281. ref->tree = RB_ROOT;
  282. ref->cache = NULL;
  283. init_llist_head(&ref->preallocated_barriers);
  284. atomic_set(&ref->count, 0);
  285. __mutex_init(&ref->mutex, "i915_active", mkey);
  286. __i915_active_fence_init(&ref->excl, NULL, excl_retire);
  287. INIT_WORK(&ref->work, active_work);
  288. #if IS_ENABLED(CONFIG_LOCKDEP)
  289. lockdep_init_map(&ref->work.lockdep_map, "i915_active.work", wkey, 0);
  290. #endif
  291. }
  292. static bool ____active_del_barrier(struct i915_active *ref,
  293. struct active_node *node,
  294. struct intel_engine_cs *engine)
  295. {
  296. struct llist_node *head = NULL, *tail = NULL;
  297. struct llist_node *pos, *next;
  298. GEM_BUG_ON(node->timeline != engine->kernel_context->timeline->fence_context);
  299. /*
  300. * Rebuild the llist excluding our node. We may perform this
  301. * outside of the kernel_context timeline mutex and so someone
  302. * else may be manipulating the engine->barrier_tasks, in
  303. * which case either we or they will be upset :)
  304. *
  305. * A second __active_del_barrier() will report failure to claim
  306. * the active_node and the caller will just shrug and know not to
  307. * claim ownership of its node.
  308. *
  309. * A concurrent i915_request_add_active_barriers() will miss adding
  310. * any of the tasks, but we will try again on the next -- and since
  311. * we are actively using the barrier, we know that there will be
  312. * at least another opportunity when we idle.
  313. */
  314. llist_for_each_safe(pos, next, llist_del_all(&engine->barrier_tasks)) {
  315. if (node == barrier_from_ll(pos)) {
  316. node = NULL;
  317. continue;
  318. }
  319. pos->next = head;
  320. head = pos;
  321. if (!tail)
  322. tail = pos;
  323. }
  324. if (head)
  325. llist_add_batch(head, tail, &engine->barrier_tasks);
  326. return !node;
  327. }
  328. static bool
  329. __active_del_barrier(struct i915_active *ref, struct active_node *node)
  330. {
  331. return ____active_del_barrier(ref, node, barrier_to_engine(node));
  332. }
  333. static bool
  334. replace_barrier(struct i915_active *ref, struct i915_active_fence *active)
  335. {
  336. if (!is_barrier(active)) /* proto-node used by our idle barrier? */
  337. return false;
  338. /*
  339. * This request is on the kernel_context timeline, and so
  340. * we can use it to substitute for the pending idle-barrer
  341. * request that we want to emit on the kernel_context.
  342. */
  343. return __active_del_barrier(ref, node_from_active(active));
  344. }
  345. int i915_active_add_request(struct i915_active *ref, struct i915_request *rq)
  346. {
  347. u64 idx = i915_request_timeline(rq)->fence_context;
  348. struct dma_fence *fence = &rq->fence;
  349. struct i915_active_fence *active;
  350. int err;
  351. /* Prevent reaping in case we malloc/wait while building the tree */
  352. err = i915_active_acquire(ref);
  353. if (err)
  354. return err;
  355. do {
  356. active = active_instance(ref, idx);
  357. if (!active) {
  358. err = -ENOMEM;
  359. goto out;
  360. }
  361. if (replace_barrier(ref, active)) {
  362. RCU_INIT_POINTER(active->fence, NULL);
  363. atomic_dec(&ref->count);
  364. }
  365. } while (unlikely(is_barrier(active)));
  366. fence = __i915_active_fence_set(active, fence);
  367. if (!fence)
  368. __i915_active_acquire(ref);
  369. else
  370. dma_fence_put(fence);
  371. out:
  372. i915_active_release(ref);
  373. return err;
  374. }
  375. static struct dma_fence *
  376. __i915_active_set_fence(struct i915_active *ref,
  377. struct i915_active_fence *active,
  378. struct dma_fence *fence)
  379. {
  380. struct dma_fence *prev;
  381. if (replace_barrier(ref, active)) {
  382. RCU_INIT_POINTER(active->fence, fence);
  383. return NULL;
  384. }
  385. prev = __i915_active_fence_set(active, fence);
  386. if (!prev)
  387. __i915_active_acquire(ref);
  388. return prev;
  389. }
  390. struct dma_fence *
  391. i915_active_set_exclusive(struct i915_active *ref, struct dma_fence *f)
  392. {
  393. /* We expect the caller to manage the exclusive timeline ordering */
  394. return __i915_active_set_fence(ref, &ref->excl, f);
  395. }
  396. bool i915_active_acquire_if_busy(struct i915_active *ref)
  397. {
  398. debug_active_assert(ref);
  399. return atomic_add_unless(&ref->count, 1, 0);
  400. }
  401. static void __i915_active_activate(struct i915_active *ref)
  402. {
  403. spin_lock_irq(&ref->tree_lock); /* __active_retire() */
  404. if (!atomic_fetch_inc(&ref->count))
  405. debug_active_activate(ref);
  406. spin_unlock_irq(&ref->tree_lock);
  407. }
  408. int i915_active_acquire(struct i915_active *ref)
  409. {
  410. int err;
  411. if (i915_active_acquire_if_busy(ref))
  412. return 0;
  413. if (!ref->active) {
  414. __i915_active_activate(ref);
  415. return 0;
  416. }
  417. err = mutex_lock_interruptible(&ref->mutex);
  418. if (err)
  419. return err;
  420. if (likely(!i915_active_acquire_if_busy(ref))) {
  421. err = ref->active(ref);
  422. if (!err)
  423. __i915_active_activate(ref);
  424. }
  425. mutex_unlock(&ref->mutex);
  426. return err;
  427. }
  428. int i915_active_acquire_for_context(struct i915_active *ref, u64 idx)
  429. {
  430. struct i915_active_fence *active;
  431. int err;
  432. err = i915_active_acquire(ref);
  433. if (err)
  434. return err;
  435. active = active_instance(ref, idx);
  436. if (!active) {
  437. i915_active_release(ref);
  438. return -ENOMEM;
  439. }
  440. return 0; /* return with active ref */
  441. }
  442. void i915_active_release(struct i915_active *ref)
  443. {
  444. debug_active_assert(ref);
  445. active_retire(ref);
  446. }
  447. static void enable_signaling(struct i915_active_fence *active)
  448. {
  449. struct dma_fence *fence;
  450. if (unlikely(is_barrier(active)))
  451. return;
  452. fence = i915_active_fence_get(active);
  453. if (!fence)
  454. return;
  455. dma_fence_enable_sw_signaling(fence);
  456. dma_fence_put(fence);
  457. }
  458. static int flush_barrier(struct active_node *it)
  459. {
  460. struct intel_engine_cs *engine;
  461. if (likely(!is_barrier(&it->base)))
  462. return 0;
  463. engine = __barrier_to_engine(it);
  464. smp_rmb(); /* serialise with add_active_barriers */
  465. if (!is_barrier(&it->base))
  466. return 0;
  467. return intel_engine_flush_barriers(engine);
  468. }
  469. static int flush_lazy_signals(struct i915_active *ref)
  470. {
  471. struct active_node *it, *n;
  472. int err = 0;
  473. enable_signaling(&ref->excl);
  474. rbtree_postorder_for_each_entry_safe(it, n, &ref->tree, node) {
  475. err = flush_barrier(it); /* unconnected idle barrier? */
  476. if (err)
  477. break;
  478. enable_signaling(&it->base);
  479. }
  480. return err;
  481. }
  482. int __i915_active_wait(struct i915_active *ref, int state)
  483. {
  484. might_sleep();
  485. /* Any fence added after the wait begins will not be auto-signaled */
  486. if (i915_active_acquire_if_busy(ref)) {
  487. int err;
  488. err = flush_lazy_signals(ref);
  489. i915_active_release(ref);
  490. if (err)
  491. return err;
  492. if (___wait_var_event(ref, i915_active_is_idle(ref),
  493. state, 0, 0, schedule()))
  494. return -EINTR;
  495. }
  496. /*
  497. * After the wait is complete, the caller may free the active.
  498. * We have to flush any concurrent retirement before returning.
  499. */
  500. flush_work(&ref->work);
  501. return 0;
  502. }
  503. static int __await_active(struct i915_active_fence *active,
  504. int (*fn)(void *arg, struct dma_fence *fence),
  505. void *arg)
  506. {
  507. struct dma_fence *fence;
  508. if (is_barrier(active)) /* XXX flush the barrier? */
  509. return 0;
  510. fence = i915_active_fence_get(active);
  511. if (fence) {
  512. int err;
  513. err = fn(arg, fence);
  514. dma_fence_put(fence);
  515. if (err < 0)
  516. return err;
  517. }
  518. return 0;
  519. }
  520. struct wait_barrier {
  521. struct wait_queue_entry base;
  522. struct i915_active *ref;
  523. };
  524. static int
  525. barrier_wake(wait_queue_entry_t *wq, unsigned int mode, int flags, void *key)
  526. {
  527. struct wait_barrier *wb = container_of(wq, typeof(*wb), base);
  528. if (i915_active_is_idle(wb->ref)) {
  529. list_del(&wq->entry);
  530. i915_sw_fence_complete(wq->private);
  531. kfree(wq);
  532. }
  533. return 0;
  534. }
  535. static int __await_barrier(struct i915_active *ref, struct i915_sw_fence *fence)
  536. {
  537. struct wait_barrier *wb;
  538. wb = kmalloc(sizeof(*wb), GFP_KERNEL);
  539. if (unlikely(!wb))
  540. return -ENOMEM;
  541. GEM_BUG_ON(i915_active_is_idle(ref));
  542. if (!i915_sw_fence_await(fence)) {
  543. kfree(wb);
  544. return -EINVAL;
  545. }
  546. wb->base.flags = 0;
  547. wb->base.func = barrier_wake;
  548. wb->base.private = fence;
  549. wb->ref = ref;
  550. add_wait_queue(__var_waitqueue(ref), &wb->base);
  551. return 0;
  552. }
  553. static int await_active(struct i915_active *ref,
  554. unsigned int flags,
  555. int (*fn)(void *arg, struct dma_fence *fence),
  556. void *arg, struct i915_sw_fence *barrier)
  557. {
  558. int err = 0;
  559. if (!i915_active_acquire_if_busy(ref))
  560. return 0;
  561. if (flags & I915_ACTIVE_AWAIT_EXCL &&
  562. rcu_access_pointer(ref->excl.fence)) {
  563. err = __await_active(&ref->excl, fn, arg);
  564. if (err)
  565. goto out;
  566. }
  567. if (flags & I915_ACTIVE_AWAIT_ACTIVE) {
  568. struct active_node *it, *n;
  569. rbtree_postorder_for_each_entry_safe(it, n, &ref->tree, node) {
  570. err = __await_active(&it->base, fn, arg);
  571. if (err)
  572. goto out;
  573. }
  574. }
  575. if (flags & I915_ACTIVE_AWAIT_BARRIER) {
  576. err = flush_lazy_signals(ref);
  577. if (err)
  578. goto out;
  579. err = __await_barrier(ref, barrier);
  580. if (err)
  581. goto out;
  582. }
  583. out:
  584. i915_active_release(ref);
  585. return err;
  586. }
  587. static int rq_await_fence(void *arg, struct dma_fence *fence)
  588. {
  589. return i915_request_await_dma_fence(arg, fence);
  590. }
  591. int i915_request_await_active(struct i915_request *rq,
  592. struct i915_active *ref,
  593. unsigned int flags)
  594. {
  595. return await_active(ref, flags, rq_await_fence, rq, &rq->submit);
  596. }
  597. static int sw_await_fence(void *arg, struct dma_fence *fence)
  598. {
  599. return i915_sw_fence_await_dma_fence(arg, fence, 0,
  600. GFP_NOWAIT | __GFP_NOWARN);
  601. }
  602. int i915_sw_fence_await_active(struct i915_sw_fence *fence,
  603. struct i915_active *ref,
  604. unsigned int flags)
  605. {
  606. return await_active(ref, flags, sw_await_fence, fence, fence);
  607. }
  608. void i915_active_fini(struct i915_active *ref)
  609. {
  610. debug_active_fini(ref);
  611. GEM_BUG_ON(atomic_read(&ref->count));
  612. GEM_BUG_ON(work_pending(&ref->work));
  613. mutex_destroy(&ref->mutex);
  614. if (ref->cache)
  615. kmem_cache_free(slab_cache, ref->cache);
  616. }
  617. static inline bool is_idle_barrier(struct active_node *node, u64 idx)
  618. {
  619. return node->timeline == idx && !i915_active_fence_isset(&node->base);
  620. }
  621. static struct active_node *reuse_idle_barrier(struct i915_active *ref, u64 idx)
  622. {
  623. struct rb_node *prev, *p;
  624. if (RB_EMPTY_ROOT(&ref->tree))
  625. return NULL;
  626. GEM_BUG_ON(i915_active_is_idle(ref));
  627. /*
  628. * Try to reuse any existing barrier nodes already allocated for this
  629. * i915_active, due to overlapping active phases there is likely a
  630. * node kept alive (as we reuse before parking). We prefer to reuse
  631. * completely idle barriers (less hassle in manipulating the llists),
  632. * but otherwise any will do.
  633. */
  634. if (ref->cache && is_idle_barrier(ref->cache, idx)) {
  635. p = &ref->cache->node;
  636. goto match;
  637. }
  638. prev = NULL;
  639. p = ref->tree.rb_node;
  640. while (p) {
  641. struct active_node *node =
  642. rb_entry(p, struct active_node, node);
  643. if (is_idle_barrier(node, idx))
  644. goto match;
  645. prev = p;
  646. if (node->timeline < idx)
  647. p = READ_ONCE(p->rb_right);
  648. else
  649. p = READ_ONCE(p->rb_left);
  650. }
  651. /*
  652. * No quick match, but we did find the leftmost rb_node for the
  653. * kernel_context. Walk the rb_tree in-order to see if there were
  654. * any idle-barriers on this timeline that we missed, or just use
  655. * the first pending barrier.
  656. */
  657. for (p = prev; p; p = rb_next(p)) {
  658. struct active_node *node =
  659. rb_entry(p, struct active_node, node);
  660. struct intel_engine_cs *engine;
  661. if (node->timeline > idx)
  662. break;
  663. if (node->timeline < idx)
  664. continue;
  665. if (is_idle_barrier(node, idx))
  666. goto match;
  667. /*
  668. * The list of pending barriers is protected by the
  669. * kernel_context timeline, which notably we do not hold
  670. * here. i915_request_add_active_barriers() may consume
  671. * the barrier before we claim it, so we have to check
  672. * for success.
  673. */
  674. engine = __barrier_to_engine(node);
  675. smp_rmb(); /* serialise with add_active_barriers */
  676. if (is_barrier(&node->base) &&
  677. ____active_del_barrier(ref, node, engine))
  678. goto match;
  679. }
  680. return NULL;
  681. match:
  682. spin_lock_irq(&ref->tree_lock);
  683. rb_erase(p, &ref->tree); /* Hide from waits and sibling allocations */
  684. if (p == &ref->cache->node)
  685. WRITE_ONCE(ref->cache, NULL);
  686. spin_unlock_irq(&ref->tree_lock);
  687. return rb_entry(p, struct active_node, node);
  688. }
  689. int i915_active_acquire_preallocate_barrier(struct i915_active *ref,
  690. struct intel_engine_cs *engine)
  691. {
  692. intel_engine_mask_t tmp, mask = engine->mask;
  693. struct llist_node *first = NULL, *last = NULL;
  694. struct intel_gt *gt = engine->gt;
  695. GEM_BUG_ON(i915_active_is_idle(ref));
  696. /* Wait until the previous preallocation is completed */
  697. while (!llist_empty(&ref->preallocated_barriers))
  698. cond_resched();
  699. /*
  700. * Preallocate a node for each physical engine supporting the target
  701. * engine (remember virtual engines have more than one sibling).
  702. * We can then use the preallocated nodes in
  703. * i915_active_acquire_barrier()
  704. */
  705. GEM_BUG_ON(!mask);
  706. for_each_engine_masked(engine, gt, mask, tmp) {
  707. u64 idx = engine->kernel_context->timeline->fence_context;
  708. struct llist_node *prev = first;
  709. struct active_node *node;
  710. rcu_read_lock();
  711. node = reuse_idle_barrier(ref, idx);
  712. rcu_read_unlock();
  713. if (!node) {
  714. node = kmem_cache_alloc(slab_cache, GFP_KERNEL);
  715. if (!node)
  716. goto unwind;
  717. RCU_INIT_POINTER(node->base.fence, NULL);
  718. node->base.cb.func = node_retire;
  719. node->timeline = idx;
  720. node->ref = ref;
  721. }
  722. if (!i915_active_fence_isset(&node->base)) {
  723. /*
  724. * Mark this as being *our* unconnected proto-node.
  725. *
  726. * Since this node is not in any list, and we have
  727. * decoupled it from the rbtree, we can reuse the
  728. * request to indicate this is an idle-barrier node
  729. * and then we can use the rb_node and list pointers
  730. * for our tracking of the pending barrier.
  731. */
  732. RCU_INIT_POINTER(node->base.fence, ERR_PTR(-EAGAIN));
  733. node->base.cb.node.prev = (void *)engine;
  734. __i915_active_acquire(ref);
  735. }
  736. GEM_BUG_ON(rcu_access_pointer(node->base.fence) != ERR_PTR(-EAGAIN));
  737. GEM_BUG_ON(barrier_to_engine(node) != engine);
  738. first = barrier_to_ll(node);
  739. first->next = prev;
  740. if (!last)
  741. last = first;
  742. intel_engine_pm_get(engine);
  743. }
  744. GEM_BUG_ON(!llist_empty(&ref->preallocated_barriers));
  745. llist_add_batch(first, last, &ref->preallocated_barriers);
  746. return 0;
  747. unwind:
  748. while (first) {
  749. struct active_node *node = barrier_from_ll(first);
  750. first = first->next;
  751. atomic_dec(&ref->count);
  752. intel_engine_pm_put(barrier_to_engine(node));
  753. kmem_cache_free(slab_cache, node);
  754. }
  755. return -ENOMEM;
  756. }
  757. void i915_active_acquire_barrier(struct i915_active *ref)
  758. {
  759. struct llist_node *pos, *next;
  760. unsigned long flags;
  761. GEM_BUG_ON(i915_active_is_idle(ref));
  762. /*
  763. * Transfer the list of preallocated barriers into the
  764. * i915_active rbtree, but only as proto-nodes. They will be
  765. * populated by i915_request_add_active_barriers() to point to the
  766. * request that will eventually release them.
  767. */
  768. llist_for_each_safe(pos, next, take_preallocated_barriers(ref)) {
  769. struct active_node *node = barrier_from_ll(pos);
  770. struct intel_engine_cs *engine = barrier_to_engine(node);
  771. struct rb_node **p, *parent;
  772. spin_lock_irqsave_nested(&ref->tree_lock, flags,
  773. SINGLE_DEPTH_NESTING);
  774. parent = NULL;
  775. p = &ref->tree.rb_node;
  776. while (*p) {
  777. struct active_node *it;
  778. parent = *p;
  779. it = rb_entry(parent, struct active_node, node);
  780. if (it->timeline < node->timeline)
  781. p = &parent->rb_right;
  782. else
  783. p = &parent->rb_left;
  784. }
  785. rb_link_node(&node->node, parent, p);
  786. rb_insert_color(&node->node, &ref->tree);
  787. spin_unlock_irqrestore(&ref->tree_lock, flags);
  788. GEM_BUG_ON(!intel_engine_pm_is_awake(engine));
  789. llist_add(barrier_to_ll(node), &engine->barrier_tasks);
  790. intel_engine_pm_put_delay(engine, 2);
  791. }
  792. }
  793. static struct dma_fence **ll_to_fence_slot(struct llist_node *node)
  794. {
  795. return __active_fence_slot(&barrier_from_ll(node)->base);
  796. }
  797. void i915_request_add_active_barriers(struct i915_request *rq)
  798. {
  799. struct intel_engine_cs *engine = rq->engine;
  800. struct llist_node *node, *next;
  801. unsigned long flags;
  802. GEM_BUG_ON(!intel_context_is_barrier(rq->context));
  803. GEM_BUG_ON(intel_engine_is_virtual(engine));
  804. GEM_BUG_ON(i915_request_timeline(rq) != engine->kernel_context->timeline);
  805. node = llist_del_all(&engine->barrier_tasks);
  806. if (!node)
  807. return;
  808. /*
  809. * Attach the list of proto-fences to the in-flight request such
  810. * that the parent i915_active will be released when this request
  811. * is retired.
  812. */
  813. spin_lock_irqsave(&rq->lock, flags);
  814. llist_for_each_safe(node, next, node) {
  815. /* serialise with reuse_idle_barrier */
  816. smp_store_mb(*ll_to_fence_slot(node), &rq->fence);
  817. list_add_tail((struct list_head *)node, &rq->fence.cb_list);
  818. }
  819. spin_unlock_irqrestore(&rq->lock, flags);
  820. }
  821. /*
  822. * __i915_active_fence_set: Update the last active fence along its timeline
  823. * @active: the active tracker
  824. * @fence: the new fence (under construction)
  825. *
  826. * Records the new @fence as the last active fence along its timeline in
  827. * this active tracker, moving the tracking callbacks from the previous
  828. * fence onto this one. Gets and returns a reference to the previous fence
  829. * (if not already completed), which the caller must put after making sure
  830. * that it is executed before the new fence. To ensure that the order of
  831. * fences within the timeline of the i915_active_fence is understood, it
  832. * should be locked by the caller.
  833. */
  834. struct dma_fence *
  835. __i915_active_fence_set(struct i915_active_fence *active,
  836. struct dma_fence *fence)
  837. {
  838. struct dma_fence *prev;
  839. unsigned long flags;
  840. /*
  841. * In case of fences embedded in i915_requests, their memory is
  842. * SLAB_FAILSAFE_BY_RCU, then it can be reused right after release
  843. * by new requests. Then, there is a risk of passing back a pointer
  844. * to a new, completely unrelated fence that reuses the same memory
  845. * while tracked under a different active tracker. Combined with i915
  846. * perf open/close operations that build await dependencies between
  847. * engine kernel context requests and user requests from different
  848. * timelines, this can lead to dependency loops and infinite waits.
  849. *
  850. * As a countermeasure, we try to get a reference to the active->fence
  851. * first, so if we succeed and pass it back to our user then it is not
  852. * released and potentially reused by an unrelated request before the
  853. * user has a chance to set up an await dependency on it.
  854. */
  855. prev = i915_active_fence_get(active);
  856. if (fence == prev)
  857. return fence;
  858. GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags));
  859. /*
  860. * Consider that we have two threads arriving (A and B), with
  861. * C already resident as the active->fence.
  862. *
  863. * Both A and B have got a reference to C or NULL, depending on the
  864. * timing of the interrupt handler. Let's assume that if A has got C
  865. * then it has locked C first (before B).
  866. *
  867. * Note the strong ordering of the timeline also provides consistent
  868. * nesting rules for the fence->lock; the inner lock is always the
  869. * older lock.
  870. */
  871. spin_lock_irqsave(fence->lock, flags);
  872. if (prev)
  873. spin_lock_nested(prev->lock, SINGLE_DEPTH_NESTING);
  874. /*
  875. * A does the cmpxchg first, and so it sees C or NULL, as before, or
  876. * something else, depending on the timing of other threads and/or
  877. * interrupt handler. If not the same as before then A unlocks C if
  878. * applicable and retries, starting from an attempt to get a new
  879. * active->fence. Meanwhile, B follows the same path as A.
  880. * Once A succeeds with cmpxch, B fails again, retires, gets A from
  881. * active->fence, locks it as soon as A completes, and possibly
  882. * succeeds with cmpxchg.
  883. */
  884. while (cmpxchg(__active_fence_slot(active), prev, fence) != prev) {
  885. if (prev) {
  886. spin_unlock(prev->lock);
  887. dma_fence_put(prev);
  888. }
  889. spin_unlock_irqrestore(fence->lock, flags);
  890. prev = i915_active_fence_get(active);
  891. GEM_BUG_ON(prev == fence);
  892. spin_lock_irqsave(fence->lock, flags);
  893. if (prev)
  894. spin_lock_nested(prev->lock, SINGLE_DEPTH_NESTING);
  895. }
  896. /*
  897. * If prev is NULL then the previous fence must have been signaled
  898. * and we know that we are first on the timeline. If it is still
  899. * present then, having the lock on that fence already acquired, we
  900. * serialise with the interrupt handler, in the process of removing it
  901. * from any future interrupt callback. A will then wait on C before
  902. * executing (if present).
  903. *
  904. * As B is second, it sees A as the previous fence and so waits for
  905. * it to complete its transition and takes over the occupancy for
  906. * itself -- remembering that it needs to wait on A before executing.
  907. */
  908. if (prev) {
  909. __list_del_entry(&active->cb.node);
  910. spin_unlock(prev->lock); /* serialise with prev->cb_list */
  911. }
  912. list_add_tail(&active->cb.node, &fence->cb_list);
  913. spin_unlock_irqrestore(fence->lock, flags);
  914. return prev;
  915. }
  916. int i915_active_fence_set(struct i915_active_fence *active,
  917. struct i915_request *rq)
  918. {
  919. struct dma_fence *fence;
  920. int err = 0;
  921. /* Must maintain timeline ordering wrt previous active requests */
  922. fence = __i915_active_fence_set(active, &rq->fence);
  923. if (fence) {
  924. err = i915_request_await_dma_fence(rq, fence);
  925. dma_fence_put(fence);
  926. }
  927. return err;
  928. }
  929. void i915_active_noop(struct dma_fence *fence, struct dma_fence_cb *cb)
  930. {
  931. active_fence_cb(fence, cb);
  932. }
  933. struct auto_active {
  934. struct i915_active base;
  935. struct kref ref;
  936. };
  937. struct i915_active *i915_active_get(struct i915_active *ref)
  938. {
  939. struct auto_active *aa = container_of(ref, typeof(*aa), base);
  940. kref_get(&aa->ref);
  941. return &aa->base;
  942. }
  943. static void auto_release(struct kref *ref)
  944. {
  945. struct auto_active *aa = container_of(ref, typeof(*aa), ref);
  946. i915_active_fini(&aa->base);
  947. kfree(aa);
  948. }
  949. void i915_active_put(struct i915_active *ref)
  950. {
  951. struct auto_active *aa = container_of(ref, typeof(*aa), base);
  952. kref_put(&aa->ref, auto_release);
  953. }
  954. static int auto_active(struct i915_active *ref)
  955. {
  956. i915_active_get(ref);
  957. return 0;
  958. }
  959. static void auto_retire(struct i915_active *ref)
  960. {
  961. i915_active_put(ref);
  962. }
  963. struct i915_active *i915_active_create(void)
  964. {
  965. struct auto_active *aa;
  966. aa = kmalloc(sizeof(*aa), GFP_KERNEL);
  967. if (!aa)
  968. return NULL;
  969. kref_init(&aa->ref);
  970. i915_active_init(&aa->base, auto_active, auto_retire, 0);
  971. return &aa->base;
  972. }
  973. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  974. #include "selftests/i915_active.c"
  975. #endif
  976. void i915_active_module_exit(void)
  977. {
  978. kmem_cache_destroy(slab_cache);
  979. }
  980. int __init i915_active_module_init(void)
  981. {
  982. slab_cache = KMEM_CACHE(active_node, SLAB_HWCACHE_ALIGN);
  983. if (!slab_cache)
  984. return -ENOMEM;
  985. return 0;
  986. }