dma-resv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
  4. *
  5. * Based on bo.c which bears the following copyright notice,
  6. * but is dual licensed:
  7. *
  8. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  9. * All Rights Reserved.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the
  13. * "Software"), to deal in the Software without restriction, including
  14. * without limitation the rights to use, copy, modify, merge, publish,
  15. * distribute, sub license, and/or sell copies of the Software, and to
  16. * permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice (including the
  20. * next paragraph) shall be included in all copies or substantial portions
  21. * of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  26. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  27. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  28. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  29. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. *
  31. **************************************************************************/
  32. /*
  33. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  34. */
  35. #include <linux/dma-resv.h>
  36. #include <linux/dma-fence-array.h>
  37. #include <linux/export.h>
  38. #include <linux/mm.h>
  39. #include <linux/sched/mm.h>
  40. #include <linux/mmu_notifier.h>
  41. #include <linux/seq_file.h>
  42. /**
  43. * DOC: Reservation Object Overview
  44. *
  45. * The reservation object provides a mechanism to manage a container of
  46. * dma_fence object associated with a resource. A reservation object
  47. * can have any number of fences attaches to it. Each fence carries an usage
  48. * parameter determining how the operation represented by the fence is using the
  49. * resource. The RCU mechanism is used to protect read access to fences from
  50. * locked write-side updates.
  51. *
  52. * See struct dma_resv for more details.
  53. */
  54. DEFINE_WD_CLASS(reservation_ww_class);
  55. EXPORT_SYMBOL(reservation_ww_class);
  56. /* Mask for the lower fence pointer bits */
  57. #define DMA_RESV_LIST_MASK 0x3
  58. struct dma_resv_list {
  59. struct rcu_head rcu;
  60. u32 num_fences, max_fences;
  61. struct dma_fence __rcu *table[];
  62. };
  63. /* Extract the fence and usage flags from an RCU protected entry in the list. */
  64. static void dma_resv_list_entry(struct dma_resv_list *list, unsigned int index,
  65. struct dma_resv *resv, struct dma_fence **fence,
  66. enum dma_resv_usage *usage)
  67. {
  68. long tmp;
  69. tmp = (long)rcu_dereference_check(list->table[index],
  70. resv ? dma_resv_held(resv) : true);
  71. *fence = (struct dma_fence *)(tmp & ~DMA_RESV_LIST_MASK);
  72. if (usage)
  73. *usage = tmp & DMA_RESV_LIST_MASK;
  74. }
  75. /* Set the fence and usage flags at the specific index in the list. */
  76. static void dma_resv_list_set(struct dma_resv_list *list,
  77. unsigned int index,
  78. struct dma_fence *fence,
  79. enum dma_resv_usage usage)
  80. {
  81. long tmp = ((long)fence) | usage;
  82. RCU_INIT_POINTER(list->table[index], (struct dma_fence *)tmp);
  83. }
  84. /*
  85. * Allocate a new dma_resv_list and make sure to correctly initialize
  86. * max_fences.
  87. */
  88. static struct dma_resv_list *dma_resv_list_alloc(unsigned int max_fences)
  89. {
  90. struct dma_resv_list *list;
  91. size_t size;
  92. /* Round up to the next kmalloc bucket size. */
  93. size = kmalloc_size_roundup(struct_size(list, table, max_fences));
  94. list = kmalloc(size, GFP_KERNEL);
  95. if (!list)
  96. return NULL;
  97. /* Given the resulting bucket size, recalculated max_fences. */
  98. list->max_fences = (size - offsetof(typeof(*list), table)) /
  99. sizeof(*list->table);
  100. return list;
  101. }
  102. /* Free a dma_resv_list and make sure to drop all references. */
  103. static void dma_resv_list_free(struct dma_resv_list *list)
  104. {
  105. unsigned int i;
  106. if (!list)
  107. return;
  108. for (i = 0; i < list->num_fences; ++i) {
  109. struct dma_fence *fence;
  110. dma_resv_list_entry(list, i, NULL, &fence, NULL);
  111. dma_fence_put(fence);
  112. }
  113. kfree_rcu(list, rcu);
  114. }
  115. /**
  116. * dma_resv_init - initialize a reservation object
  117. * @obj: the reservation object
  118. */
  119. void dma_resv_init(struct dma_resv *obj)
  120. {
  121. ww_mutex_init(&obj->lock, &reservation_ww_class);
  122. RCU_INIT_POINTER(obj->fences, NULL);
  123. }
  124. EXPORT_SYMBOL(dma_resv_init);
  125. /**
  126. * dma_resv_fini - destroys a reservation object
  127. * @obj: the reservation object
  128. */
  129. void dma_resv_fini(struct dma_resv *obj)
  130. {
  131. /*
  132. * This object should be dead and all references must have
  133. * been released to it, so no need to be protected with rcu.
  134. */
  135. dma_resv_list_free(rcu_dereference_protected(obj->fences, true));
  136. ww_mutex_destroy(&obj->lock);
  137. }
  138. EXPORT_SYMBOL(dma_resv_fini);
  139. /* Dereference the fences while ensuring RCU rules */
  140. static inline struct dma_resv_list *dma_resv_fences_list(struct dma_resv *obj)
  141. {
  142. return rcu_dereference_check(obj->fences, dma_resv_held(obj));
  143. }
  144. /**
  145. * dma_resv_reserve_fences - Reserve space to add fences to a dma_resv object.
  146. * @obj: reservation object
  147. * @num_fences: number of fences we want to add
  148. *
  149. * Should be called before dma_resv_add_fence(). Must be called with @obj
  150. * locked through dma_resv_lock().
  151. *
  152. * Note that the preallocated slots need to be re-reserved if @obj is unlocked
  153. * at any time before calling dma_resv_add_fence(). This is validated when
  154. * CONFIG_DEBUG_MUTEXES is enabled.
  155. *
  156. * RETURNS
  157. * Zero for success, or -errno
  158. */
  159. int dma_resv_reserve_fences(struct dma_resv *obj, unsigned int num_fences)
  160. {
  161. struct dma_resv_list *old, *new;
  162. unsigned int i, j, k, max;
  163. dma_resv_assert_held(obj);
  164. /* Driver and component code should never call this function with
  165. * num_fences=0. If they do it usually points to bugs when calculating
  166. * the number of needed fences dynamically.
  167. */
  168. if (WARN_ON(!num_fences))
  169. return -EINVAL;
  170. old = dma_resv_fences_list(obj);
  171. if (old && old->max_fences) {
  172. if ((old->num_fences + num_fences) <= old->max_fences)
  173. return 0;
  174. max = max(old->num_fences + num_fences, old->max_fences * 2);
  175. } else {
  176. max = max(4ul, roundup_pow_of_two(num_fences));
  177. }
  178. new = dma_resv_list_alloc(max);
  179. if (!new)
  180. return -ENOMEM;
  181. /*
  182. * no need to bump fence refcounts, rcu_read access
  183. * requires the use of kref_get_unless_zero, and the
  184. * references from the old struct are carried over to
  185. * the new.
  186. */
  187. for (i = 0, j = 0, k = max; i < (old ? old->num_fences : 0); ++i) {
  188. enum dma_resv_usage usage;
  189. struct dma_fence *fence;
  190. dma_resv_list_entry(old, i, obj, &fence, &usage);
  191. if (dma_fence_is_signaled(fence))
  192. RCU_INIT_POINTER(new->table[--k], fence);
  193. else
  194. dma_resv_list_set(new, j++, fence, usage);
  195. }
  196. new->num_fences = j;
  197. /*
  198. * We are not changing the effective set of fences here so can
  199. * merely update the pointer to the new array; both existing
  200. * readers and new readers will see exactly the same set of
  201. * active (unsignaled) fences. Individual fences and the
  202. * old array are protected by RCU and so will not vanish under
  203. * the gaze of the rcu_read_lock() readers.
  204. */
  205. rcu_assign_pointer(obj->fences, new);
  206. if (!old)
  207. return 0;
  208. /* Drop the references to the signaled fences */
  209. for (i = k; i < max; ++i) {
  210. struct dma_fence *fence;
  211. fence = rcu_dereference_protected(new->table[i],
  212. dma_resv_held(obj));
  213. dma_fence_put(fence);
  214. }
  215. kfree_rcu(old, rcu);
  216. return 0;
  217. }
  218. EXPORT_SYMBOL(dma_resv_reserve_fences);
  219. #ifdef CONFIG_DEBUG_MUTEXES
  220. /**
  221. * dma_resv_reset_max_fences - reset fences for debugging
  222. * @obj: the dma_resv object to reset
  223. *
  224. * Reset the number of pre-reserved fence slots to test that drivers do
  225. * correct slot allocation using dma_resv_reserve_fences(). See also
  226. * &dma_resv_list.max_fences.
  227. */
  228. void dma_resv_reset_max_fences(struct dma_resv *obj)
  229. {
  230. struct dma_resv_list *fences = dma_resv_fences_list(obj);
  231. dma_resv_assert_held(obj);
  232. /* Test fence slot reservation */
  233. if (fences)
  234. fences->max_fences = fences->num_fences;
  235. }
  236. EXPORT_SYMBOL(dma_resv_reset_max_fences);
  237. #endif
  238. /**
  239. * dma_resv_add_fence - Add a fence to the dma_resv obj
  240. * @obj: the reservation object
  241. * @fence: the fence to add
  242. * @usage: how the fence is used, see enum dma_resv_usage
  243. *
  244. * Add a fence to a slot, @obj must be locked with dma_resv_lock(), and
  245. * dma_resv_reserve_fences() has been called.
  246. *
  247. * See also &dma_resv.fence for a discussion of the semantics.
  248. */
  249. void dma_resv_add_fence(struct dma_resv *obj, struct dma_fence *fence,
  250. enum dma_resv_usage usage)
  251. {
  252. struct dma_resv_list *fobj;
  253. struct dma_fence *old;
  254. unsigned int i, count;
  255. dma_fence_get(fence);
  256. dma_resv_assert_held(obj);
  257. /* Drivers should not add containers here, instead add each fence
  258. * individually.
  259. */
  260. WARN_ON(dma_fence_is_container(fence));
  261. fobj = dma_resv_fences_list(obj);
  262. count = fobj->num_fences;
  263. for (i = 0; i < count; ++i) {
  264. enum dma_resv_usage old_usage;
  265. dma_resv_list_entry(fobj, i, obj, &old, &old_usage);
  266. if ((old->context == fence->context && old_usage >= usage &&
  267. dma_fence_is_later_or_same(fence, old)) ||
  268. dma_fence_is_signaled(old)) {
  269. dma_resv_list_set(fobj, i, fence, usage);
  270. dma_fence_put(old);
  271. return;
  272. }
  273. }
  274. BUG_ON(fobj->num_fences >= fobj->max_fences);
  275. count++;
  276. dma_resv_list_set(fobj, i, fence, usage);
  277. /* pointer update must be visible before we extend the num_fences */
  278. smp_store_mb(fobj->num_fences, count);
  279. }
  280. EXPORT_SYMBOL(dma_resv_add_fence);
  281. /**
  282. * dma_resv_replace_fences - replace fences in the dma_resv obj
  283. * @obj: the reservation object
  284. * @context: the context of the fences to replace
  285. * @replacement: the new fence to use instead
  286. * @usage: how the new fence is used, see enum dma_resv_usage
  287. *
  288. * Replace fences with a specified context with a new fence. Only valid if the
  289. * operation represented by the original fence has no longer access to the
  290. * resources represented by the dma_resv object when the new fence completes.
  291. *
  292. * And example for using this is replacing a preemption fence with a page table
  293. * update fence which makes the resource inaccessible.
  294. */
  295. void dma_resv_replace_fences(struct dma_resv *obj, uint64_t context,
  296. struct dma_fence *replacement,
  297. enum dma_resv_usage usage)
  298. {
  299. struct dma_resv_list *list;
  300. unsigned int i;
  301. dma_resv_assert_held(obj);
  302. list = dma_resv_fences_list(obj);
  303. for (i = 0; list && i < list->num_fences; ++i) {
  304. struct dma_fence *old;
  305. dma_resv_list_entry(list, i, obj, &old, NULL);
  306. if (old->context != context)
  307. continue;
  308. dma_resv_list_set(list, i, dma_fence_get(replacement), usage);
  309. dma_fence_put(old);
  310. }
  311. }
  312. EXPORT_SYMBOL(dma_resv_replace_fences);
  313. /* Restart the unlocked iteration by initializing the cursor object. */
  314. static void dma_resv_iter_restart_unlocked(struct dma_resv_iter *cursor)
  315. {
  316. cursor->index = 0;
  317. cursor->num_fences = 0;
  318. cursor->fences = dma_resv_fences_list(cursor->obj);
  319. if (cursor->fences)
  320. cursor->num_fences = cursor->fences->num_fences;
  321. cursor->is_restarted = true;
  322. }
  323. /* Walk to the next not signaled fence and grab a reference to it */
  324. static void dma_resv_iter_walk_unlocked(struct dma_resv_iter *cursor)
  325. {
  326. if (!cursor->fences)
  327. return;
  328. do {
  329. /* Drop the reference from the previous round */
  330. dma_fence_put(cursor->fence);
  331. if (cursor->index >= cursor->num_fences) {
  332. cursor->fence = NULL;
  333. break;
  334. }
  335. dma_resv_list_entry(cursor->fences, cursor->index++,
  336. cursor->obj, &cursor->fence,
  337. &cursor->fence_usage);
  338. cursor->fence = dma_fence_get_rcu(cursor->fence);
  339. if (!cursor->fence) {
  340. dma_resv_iter_restart_unlocked(cursor);
  341. continue;
  342. }
  343. if (!dma_fence_is_signaled(cursor->fence) &&
  344. cursor->usage >= cursor->fence_usage)
  345. break;
  346. } while (true);
  347. }
  348. /**
  349. * dma_resv_iter_first_unlocked - first fence in an unlocked dma_resv obj.
  350. * @cursor: the cursor with the current position
  351. *
  352. * Subsequent fences are iterated with dma_resv_iter_next_unlocked().
  353. *
  354. * Beware that the iterator can be restarted. Code which accumulates statistics
  355. * or similar needs to check for this with dma_resv_iter_is_restarted(). For
  356. * this reason prefer the locked dma_resv_iter_first() whenever possible.
  357. *
  358. * Returns the first fence from an unlocked dma_resv obj.
  359. */
  360. struct dma_fence *dma_resv_iter_first_unlocked(struct dma_resv_iter *cursor)
  361. {
  362. rcu_read_lock();
  363. do {
  364. dma_resv_iter_restart_unlocked(cursor);
  365. dma_resv_iter_walk_unlocked(cursor);
  366. } while (dma_resv_fences_list(cursor->obj) != cursor->fences);
  367. rcu_read_unlock();
  368. return cursor->fence;
  369. }
  370. EXPORT_SYMBOL(dma_resv_iter_first_unlocked);
  371. /**
  372. * dma_resv_iter_next_unlocked - next fence in an unlocked dma_resv obj.
  373. * @cursor: the cursor with the current position
  374. *
  375. * Beware that the iterator can be restarted. Code which accumulates statistics
  376. * or similar needs to check for this with dma_resv_iter_is_restarted(). For
  377. * this reason prefer the locked dma_resv_iter_next() whenever possible.
  378. *
  379. * Returns the next fence from an unlocked dma_resv obj.
  380. */
  381. struct dma_fence *dma_resv_iter_next_unlocked(struct dma_resv_iter *cursor)
  382. {
  383. bool restart;
  384. rcu_read_lock();
  385. cursor->is_restarted = false;
  386. restart = dma_resv_fences_list(cursor->obj) != cursor->fences;
  387. do {
  388. if (restart)
  389. dma_resv_iter_restart_unlocked(cursor);
  390. dma_resv_iter_walk_unlocked(cursor);
  391. restart = true;
  392. } while (dma_resv_fences_list(cursor->obj) != cursor->fences);
  393. rcu_read_unlock();
  394. return cursor->fence;
  395. }
  396. EXPORT_SYMBOL(dma_resv_iter_next_unlocked);
  397. /**
  398. * dma_resv_iter_first - first fence from a locked dma_resv object
  399. * @cursor: cursor to record the current position
  400. *
  401. * Subsequent fences are iterated with dma_resv_iter_next_unlocked().
  402. *
  403. * Return the first fence in the dma_resv object while holding the
  404. * &dma_resv.lock.
  405. */
  406. struct dma_fence *dma_resv_iter_first(struct dma_resv_iter *cursor)
  407. {
  408. struct dma_fence *fence;
  409. dma_resv_assert_held(cursor->obj);
  410. cursor->index = 0;
  411. cursor->fences = dma_resv_fences_list(cursor->obj);
  412. fence = dma_resv_iter_next(cursor);
  413. cursor->is_restarted = true;
  414. return fence;
  415. }
  416. EXPORT_SYMBOL_GPL(dma_resv_iter_first);
  417. /**
  418. * dma_resv_iter_next - next fence from a locked dma_resv object
  419. * @cursor: cursor to record the current position
  420. *
  421. * Return the next fences from the dma_resv object while holding the
  422. * &dma_resv.lock.
  423. */
  424. struct dma_fence *dma_resv_iter_next(struct dma_resv_iter *cursor)
  425. {
  426. struct dma_fence *fence;
  427. dma_resv_assert_held(cursor->obj);
  428. cursor->is_restarted = false;
  429. do {
  430. if (!cursor->fences ||
  431. cursor->index >= cursor->fences->num_fences)
  432. return NULL;
  433. dma_resv_list_entry(cursor->fences, cursor->index++,
  434. cursor->obj, &fence, &cursor->fence_usage);
  435. } while (cursor->fence_usage > cursor->usage);
  436. return fence;
  437. }
  438. EXPORT_SYMBOL_GPL(dma_resv_iter_next);
  439. /**
  440. * dma_resv_copy_fences - Copy all fences from src to dst.
  441. * @dst: the destination reservation object
  442. * @src: the source reservation object
  443. *
  444. * Copy all fences from src to dst. dst-lock must be held.
  445. */
  446. int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
  447. {
  448. struct dma_resv_iter cursor;
  449. struct dma_resv_list *list;
  450. struct dma_fence *f;
  451. dma_resv_assert_held(dst);
  452. list = NULL;
  453. dma_resv_iter_begin(&cursor, src, DMA_RESV_USAGE_BOOKKEEP);
  454. dma_resv_for_each_fence_unlocked(&cursor, f) {
  455. if (dma_resv_iter_is_restarted(&cursor)) {
  456. dma_resv_list_free(list);
  457. list = dma_resv_list_alloc(cursor.num_fences);
  458. if (!list) {
  459. dma_resv_iter_end(&cursor);
  460. return -ENOMEM;
  461. }
  462. list->num_fences = 0;
  463. }
  464. dma_fence_get(f);
  465. dma_resv_list_set(list, list->num_fences++, f,
  466. dma_resv_iter_usage(&cursor));
  467. }
  468. dma_resv_iter_end(&cursor);
  469. list = rcu_replace_pointer(dst->fences, list, dma_resv_held(dst));
  470. dma_resv_list_free(list);
  471. return 0;
  472. }
  473. EXPORT_SYMBOL(dma_resv_copy_fences);
  474. /**
  475. * dma_resv_get_fences - Get an object's fences
  476. * fences without update side lock held
  477. * @obj: the reservation object
  478. * @usage: controls which fences to include, see enum dma_resv_usage.
  479. * @num_fences: the number of fences returned
  480. * @fences: the array of fence ptrs returned (array is krealloc'd to the
  481. * required size, and must be freed by caller)
  482. *
  483. * Retrieve all fences from the reservation object.
  484. * Returns either zero or -ENOMEM.
  485. */
  486. int dma_resv_get_fences(struct dma_resv *obj, enum dma_resv_usage usage,
  487. unsigned int *num_fences, struct dma_fence ***fences)
  488. {
  489. struct dma_resv_iter cursor;
  490. struct dma_fence *fence;
  491. *num_fences = 0;
  492. *fences = NULL;
  493. dma_resv_iter_begin(&cursor, obj, usage);
  494. dma_resv_for_each_fence_unlocked(&cursor, fence) {
  495. if (dma_resv_iter_is_restarted(&cursor)) {
  496. struct dma_fence **new_fences;
  497. unsigned int count;
  498. while (*num_fences)
  499. dma_fence_put((*fences)[--(*num_fences)]);
  500. count = cursor.num_fences + 1;
  501. /* Eventually re-allocate the array */
  502. new_fences = krealloc_array(*fences, count,
  503. sizeof(void *),
  504. GFP_KERNEL);
  505. if (count && !new_fences) {
  506. kfree(*fences);
  507. *fences = NULL;
  508. *num_fences = 0;
  509. dma_resv_iter_end(&cursor);
  510. return -ENOMEM;
  511. }
  512. *fences = new_fences;
  513. }
  514. (*fences)[(*num_fences)++] = dma_fence_get(fence);
  515. }
  516. dma_resv_iter_end(&cursor);
  517. return 0;
  518. }
  519. EXPORT_SYMBOL_GPL(dma_resv_get_fences);
  520. /**
  521. * dma_resv_get_singleton - Get a single fence for all the fences
  522. * @obj: the reservation object
  523. * @usage: controls which fences to include, see enum dma_resv_usage.
  524. * @fence: the resulting fence
  525. *
  526. * Get a single fence representing all the fences inside the resv object.
  527. * Returns either 0 for success or -ENOMEM.
  528. *
  529. * Warning: This can't be used like this when adding the fence back to the resv
  530. * object since that can lead to stack corruption when finalizing the
  531. * dma_fence_array.
  532. *
  533. * Returns 0 on success and negative error values on failure.
  534. */
  535. int dma_resv_get_singleton(struct dma_resv *obj, enum dma_resv_usage usage,
  536. struct dma_fence **fence)
  537. {
  538. struct dma_fence_array *array;
  539. struct dma_fence **fences;
  540. unsigned count;
  541. int r;
  542. r = dma_resv_get_fences(obj, usage, &count, &fences);
  543. if (r)
  544. return r;
  545. if (count == 0) {
  546. *fence = NULL;
  547. return 0;
  548. }
  549. if (count == 1) {
  550. *fence = fences[0];
  551. kfree(fences);
  552. return 0;
  553. }
  554. array = dma_fence_array_create(count, fences,
  555. dma_fence_context_alloc(1),
  556. 1, false);
  557. if (!array) {
  558. while (count--)
  559. dma_fence_put(fences[count]);
  560. kfree(fences);
  561. return -ENOMEM;
  562. }
  563. *fence = &array->base;
  564. return 0;
  565. }
  566. EXPORT_SYMBOL_GPL(dma_resv_get_singleton);
  567. /**
  568. * dma_resv_wait_timeout - Wait on reservation's objects fences
  569. * @obj: the reservation object
  570. * @usage: controls which fences to include, see enum dma_resv_usage.
  571. * @intr: if true, do interruptible wait
  572. * @timeout: timeout value in jiffies or zero to return immediately
  573. *
  574. * Callers are not required to hold specific locks, but maybe hold
  575. * dma_resv_lock() already
  576. * RETURNS
  577. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
  578. * greater than zero on success.
  579. */
  580. long dma_resv_wait_timeout(struct dma_resv *obj, enum dma_resv_usage usage,
  581. bool intr, unsigned long timeout)
  582. {
  583. long ret = timeout ? timeout : 1;
  584. struct dma_resv_iter cursor;
  585. struct dma_fence *fence;
  586. dma_resv_iter_begin(&cursor, obj, usage);
  587. dma_resv_for_each_fence_unlocked(&cursor, fence) {
  588. ret = dma_fence_wait_timeout(fence, intr, ret);
  589. if (ret <= 0) {
  590. dma_resv_iter_end(&cursor);
  591. return ret;
  592. }
  593. }
  594. dma_resv_iter_end(&cursor);
  595. return ret;
  596. }
  597. EXPORT_SYMBOL_GPL(dma_resv_wait_timeout);
  598. /**
  599. * dma_resv_set_deadline - Set a deadline on reservation's objects fences
  600. * @obj: the reservation object
  601. * @usage: controls which fences to include, see enum dma_resv_usage.
  602. * @deadline: the requested deadline (MONOTONIC)
  603. *
  604. * May be called without holding the dma_resv lock. Sets @deadline on
  605. * all fences filtered by @usage.
  606. */
  607. void dma_resv_set_deadline(struct dma_resv *obj, enum dma_resv_usage usage,
  608. ktime_t deadline)
  609. {
  610. struct dma_resv_iter cursor;
  611. struct dma_fence *fence;
  612. dma_resv_iter_begin(&cursor, obj, usage);
  613. dma_resv_for_each_fence_unlocked(&cursor, fence) {
  614. dma_fence_set_deadline(fence, deadline);
  615. }
  616. dma_resv_iter_end(&cursor);
  617. }
  618. EXPORT_SYMBOL_GPL(dma_resv_set_deadline);
  619. /**
  620. * dma_resv_test_signaled - Test if a reservation object's fences have been
  621. * signaled.
  622. * @obj: the reservation object
  623. * @usage: controls which fences to include, see enum dma_resv_usage.
  624. *
  625. * Callers are not required to hold specific locks, but maybe hold
  626. * dma_resv_lock() already.
  627. *
  628. * RETURNS
  629. *
  630. * True if all fences signaled, else false.
  631. */
  632. bool dma_resv_test_signaled(struct dma_resv *obj, enum dma_resv_usage usage)
  633. {
  634. struct dma_resv_iter cursor;
  635. struct dma_fence *fence;
  636. dma_resv_iter_begin(&cursor, obj, usage);
  637. dma_resv_for_each_fence_unlocked(&cursor, fence) {
  638. dma_resv_iter_end(&cursor);
  639. return false;
  640. }
  641. dma_resv_iter_end(&cursor);
  642. return true;
  643. }
  644. EXPORT_SYMBOL_GPL(dma_resv_test_signaled);
  645. /**
  646. * dma_resv_describe - Dump description of the resv object into seq_file
  647. * @obj: the reservation object
  648. * @seq: the seq_file to dump the description into
  649. *
  650. * Dump a textual description of the fences inside an dma_resv object into the
  651. * seq_file.
  652. */
  653. void dma_resv_describe(struct dma_resv *obj, struct seq_file *seq)
  654. {
  655. static const char *usage[] = { "kernel", "write", "read", "bookkeep" };
  656. struct dma_resv_iter cursor;
  657. struct dma_fence *fence;
  658. dma_resv_for_each_fence(&cursor, obj, DMA_RESV_USAGE_READ, fence) {
  659. seq_printf(seq, "\t%s fence:",
  660. usage[dma_resv_iter_usage(&cursor)]);
  661. dma_fence_describe(fence, seq);
  662. }
  663. }
  664. EXPORT_SYMBOL_GPL(dma_resv_describe);
  665. #if IS_ENABLED(CONFIG_LOCKDEP)
  666. static int __init dma_resv_lockdep(void)
  667. {
  668. struct mm_struct *mm = mm_alloc();
  669. struct ww_acquire_ctx ctx;
  670. struct dma_resv obj;
  671. struct address_space mapping;
  672. int ret;
  673. if (!mm)
  674. return -ENOMEM;
  675. dma_resv_init(&obj);
  676. address_space_init_once(&mapping);
  677. mmap_read_lock(mm);
  678. ww_acquire_init(&ctx, &reservation_ww_class);
  679. ret = dma_resv_lock(&obj, &ctx);
  680. if (ret == -EDEADLK)
  681. dma_resv_lock_slow(&obj, &ctx);
  682. fs_reclaim_acquire(GFP_KERNEL);
  683. /* for unmap_mapping_range on trylocked buffer objects in shrinkers */
  684. i_mmap_lock_write(&mapping);
  685. i_mmap_unlock_write(&mapping);
  686. #ifdef CONFIG_MMU_NOTIFIER
  687. lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
  688. __dma_fence_might_wait();
  689. lock_map_release(&__mmu_notifier_invalidate_range_start_map);
  690. #else
  691. __dma_fence_might_wait();
  692. #endif
  693. fs_reclaim_release(GFP_KERNEL);
  694. ww_mutex_unlock(&obj.lock);
  695. ww_acquire_fini(&ctx);
  696. mmap_read_unlock(mm);
  697. mmput(mm);
  698. return 0;
  699. }
  700. subsys_initcall(dma_resv_lockdep);
  701. #endif