dma-resv.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. list = kmalloc(struct_size(list, table, max_fences), GFP_KERNEL);
  92. if (!list)
  93. return NULL;
  94. list->max_fences = (ksize(list) - offsetof(typeof(*list), table)) /
  95. sizeof(*list->table);
  96. return list;
  97. }
  98. /* Free a dma_resv_list and make sure to drop all references. */
  99. static void dma_resv_list_free(struct dma_resv_list *list)
  100. {
  101. unsigned int i;
  102. if (!list)
  103. return;
  104. for (i = 0; i < list->num_fences; ++i) {
  105. struct dma_fence *fence;
  106. dma_resv_list_entry(list, i, NULL, &fence, NULL);
  107. dma_fence_put(fence);
  108. }
  109. kfree_rcu(list, rcu);
  110. }
  111. /**
  112. * dma_resv_init - initialize a reservation object
  113. * @obj: the reservation object
  114. */
  115. void dma_resv_init(struct dma_resv *obj)
  116. {
  117. ww_mutex_init(&obj->lock, &reservation_ww_class);
  118. RCU_INIT_POINTER(obj->fences, NULL);
  119. }
  120. EXPORT_SYMBOL(dma_resv_init);
  121. /**
  122. * dma_resv_fini - destroys a reservation object
  123. * @obj: the reservation object
  124. */
  125. void dma_resv_fini(struct dma_resv *obj)
  126. {
  127. /*
  128. * This object should be dead and all references must have
  129. * been released to it, so no need to be protected with rcu.
  130. */
  131. dma_resv_list_free(rcu_dereference_protected(obj->fences, true));
  132. ww_mutex_destroy(&obj->lock);
  133. }
  134. EXPORT_SYMBOL(dma_resv_fini);
  135. /* Dereference the fences while ensuring RCU rules */
  136. static inline struct dma_resv_list *dma_resv_fences_list(struct dma_resv *obj)
  137. {
  138. return rcu_dereference_check(obj->fences, dma_resv_held(obj));
  139. }
  140. /**
  141. * dma_resv_reserve_fences - Reserve space to add fences to a dma_resv object.
  142. * @obj: reservation object
  143. * @num_fences: number of fences we want to add
  144. *
  145. * Should be called before dma_resv_add_fence(). Must be called with @obj
  146. * locked through dma_resv_lock().
  147. *
  148. * Note that the preallocated slots need to be re-reserved if @obj is unlocked
  149. * at any time before calling dma_resv_add_fence(). This is validated when
  150. * CONFIG_DEBUG_MUTEXES is enabled.
  151. *
  152. * RETURNS
  153. * Zero for success, or -errno
  154. */
  155. int dma_resv_reserve_fences(struct dma_resv *obj, unsigned int num_fences)
  156. {
  157. struct dma_resv_list *old, *new;
  158. unsigned int i, j, k, max;
  159. dma_resv_assert_held(obj);
  160. old = dma_resv_fences_list(obj);
  161. if (old && old->max_fences) {
  162. if ((old->num_fences + num_fences) <= old->max_fences)
  163. return 0;
  164. max = max(old->num_fences + num_fences, old->max_fences * 2);
  165. } else {
  166. max = max(4ul, roundup_pow_of_two(num_fences));
  167. }
  168. new = dma_resv_list_alloc(max);
  169. if (!new)
  170. return -ENOMEM;
  171. /*
  172. * no need to bump fence refcounts, rcu_read access
  173. * requires the use of kref_get_unless_zero, and the
  174. * references from the old struct are carried over to
  175. * the new.
  176. */
  177. for (i = 0, j = 0, k = max; i < (old ? old->num_fences : 0); ++i) {
  178. enum dma_resv_usage usage;
  179. struct dma_fence *fence;
  180. dma_resv_list_entry(old, i, obj, &fence, &usage);
  181. if (dma_fence_is_signaled(fence))
  182. RCU_INIT_POINTER(new->table[--k], fence);
  183. else
  184. dma_resv_list_set(new, j++, fence, usage);
  185. }
  186. new->num_fences = j;
  187. /*
  188. * We are not changing the effective set of fences here so can
  189. * merely update the pointer to the new array; both existing
  190. * readers and new readers will see exactly the same set of
  191. * active (unsignaled) fences. Individual fences and the
  192. * old array are protected by RCU and so will not vanish under
  193. * the gaze of the rcu_read_lock() readers.
  194. */
  195. rcu_assign_pointer(obj->fences, new);
  196. if (!old)
  197. return 0;
  198. /* Drop the references to the signaled fences */
  199. for (i = k; i < max; ++i) {
  200. struct dma_fence *fence;
  201. fence = rcu_dereference_protected(new->table[i],
  202. dma_resv_held(obj));
  203. dma_fence_put(fence);
  204. }
  205. kfree_rcu(old, rcu);
  206. return 0;
  207. }
  208. EXPORT_SYMBOL(dma_resv_reserve_fences);
  209. #ifdef CONFIG_DEBUG_MUTEXES
  210. /**
  211. * dma_resv_reset_max_fences - reset fences for debugging
  212. * @obj: the dma_resv object to reset
  213. *
  214. * Reset the number of pre-reserved fence slots to test that drivers do
  215. * correct slot allocation using dma_resv_reserve_fences(). See also
  216. * &dma_resv_list.max_fences.
  217. */
  218. void dma_resv_reset_max_fences(struct dma_resv *obj)
  219. {
  220. struct dma_resv_list *fences = dma_resv_fences_list(obj);
  221. dma_resv_assert_held(obj);
  222. /* Test fence slot reservation */
  223. if (fences)
  224. fences->max_fences = fences->num_fences;
  225. }
  226. EXPORT_SYMBOL(dma_resv_reset_max_fences);
  227. #endif
  228. /**
  229. * dma_resv_add_fence - Add a fence to the dma_resv obj
  230. * @obj: the reservation object
  231. * @fence: the fence to add
  232. * @usage: how the fence is used, see enum dma_resv_usage
  233. *
  234. * Add a fence to a slot, @obj must be locked with dma_resv_lock(), and
  235. * dma_resv_reserve_fences() has been called.
  236. *
  237. * See also &dma_resv.fence for a discussion of the semantics.
  238. */
  239. void dma_resv_add_fence(struct dma_resv *obj, struct dma_fence *fence,
  240. enum dma_resv_usage usage)
  241. {
  242. struct dma_resv_list *fobj;
  243. struct dma_fence *old;
  244. unsigned int i, count;
  245. dma_fence_get(fence);
  246. dma_resv_assert_held(obj);
  247. /* Drivers should not add containers here, instead add each fence
  248. * individually.
  249. */
  250. WARN_ON(dma_fence_is_container(fence));
  251. fobj = dma_resv_fences_list(obj);
  252. count = fobj->num_fences;
  253. for (i = 0; i < count; ++i) {
  254. enum dma_resv_usage old_usage;
  255. dma_resv_list_entry(fobj, i, obj, &old, &old_usage);
  256. if ((old->context == fence->context && old_usage >= usage &&
  257. dma_fence_is_later_or_same(fence, old)) ||
  258. dma_fence_is_signaled(old)) {
  259. dma_resv_list_set(fobj, i, fence, usage);
  260. dma_fence_put(old);
  261. return;
  262. }
  263. }
  264. BUG_ON(fobj->num_fences >= fobj->max_fences);
  265. count++;
  266. dma_resv_list_set(fobj, i, fence, usage);
  267. /* pointer update must be visible before we extend the num_fences */
  268. smp_store_mb(fobj->num_fences, count);
  269. }
  270. EXPORT_SYMBOL(dma_resv_add_fence);
  271. /**
  272. * dma_resv_replace_fences - replace fences in the dma_resv obj
  273. * @obj: the reservation object
  274. * @context: the context of the fences to replace
  275. * @replacement: the new fence to use instead
  276. * @usage: how the new fence is used, see enum dma_resv_usage
  277. *
  278. * Replace fences with a specified context with a new fence. Only valid if the
  279. * operation represented by the original fence has no longer access to the
  280. * resources represented by the dma_resv object when the new fence completes.
  281. *
  282. * And example for using this is replacing a preemption fence with a page table
  283. * update fence which makes the resource inaccessible.
  284. */
  285. void dma_resv_replace_fences(struct dma_resv *obj, uint64_t context,
  286. struct dma_fence *replacement,
  287. enum dma_resv_usage usage)
  288. {
  289. struct dma_resv_list *list;
  290. unsigned int i;
  291. dma_resv_assert_held(obj);
  292. list = dma_resv_fences_list(obj);
  293. for (i = 0; list && i < list->num_fences; ++i) {
  294. struct dma_fence *old;
  295. dma_resv_list_entry(list, i, obj, &old, NULL);
  296. if (old->context != context)
  297. continue;
  298. dma_resv_list_set(list, i, dma_fence_get(replacement), usage);
  299. dma_fence_put(old);
  300. }
  301. }
  302. EXPORT_SYMBOL(dma_resv_replace_fences);
  303. /* Restart the unlocked iteration by initializing the cursor object. */
  304. static void dma_resv_iter_restart_unlocked(struct dma_resv_iter *cursor)
  305. {
  306. cursor->index = 0;
  307. cursor->num_fences = 0;
  308. cursor->fences = dma_resv_fences_list(cursor->obj);
  309. if (cursor->fences)
  310. cursor->num_fences = cursor->fences->num_fences;
  311. cursor->is_restarted = true;
  312. }
  313. /* Walk to the next not signaled fence and grab a reference to it */
  314. static void dma_resv_iter_walk_unlocked(struct dma_resv_iter *cursor)
  315. {
  316. if (!cursor->fences)
  317. return;
  318. do {
  319. /* Drop the reference from the previous round */
  320. dma_fence_put(cursor->fence);
  321. if (cursor->index >= cursor->num_fences) {
  322. cursor->fence = NULL;
  323. break;
  324. }
  325. dma_resv_list_entry(cursor->fences, cursor->index++,
  326. cursor->obj, &cursor->fence,
  327. &cursor->fence_usage);
  328. cursor->fence = dma_fence_get_rcu(cursor->fence);
  329. if (!cursor->fence) {
  330. dma_resv_iter_restart_unlocked(cursor);
  331. continue;
  332. }
  333. if (!dma_fence_is_signaled(cursor->fence) &&
  334. cursor->usage >= cursor->fence_usage)
  335. break;
  336. } while (true);
  337. }
  338. /**
  339. * dma_resv_iter_first_unlocked - first fence in an unlocked dma_resv obj.
  340. * @cursor: the cursor with the current position
  341. *
  342. * Subsequent fences are iterated with dma_resv_iter_next_unlocked().
  343. *
  344. * Beware that the iterator can be restarted. Code which accumulates statistics
  345. * or similar needs to check for this with dma_resv_iter_is_restarted(). For
  346. * this reason prefer the locked dma_resv_iter_first() whenver possible.
  347. *
  348. * Returns the first fence from an unlocked dma_resv obj.
  349. */
  350. struct dma_fence *dma_resv_iter_first_unlocked(struct dma_resv_iter *cursor)
  351. {
  352. rcu_read_lock();
  353. do {
  354. dma_resv_iter_restart_unlocked(cursor);
  355. dma_resv_iter_walk_unlocked(cursor);
  356. } while (dma_resv_fences_list(cursor->obj) != cursor->fences);
  357. rcu_read_unlock();
  358. return cursor->fence;
  359. }
  360. EXPORT_SYMBOL(dma_resv_iter_first_unlocked);
  361. /**
  362. * dma_resv_iter_next_unlocked - next fence in an unlocked dma_resv obj.
  363. * @cursor: the cursor with the current position
  364. *
  365. * Beware that the iterator can be restarted. Code which accumulates statistics
  366. * or similar needs to check for this with dma_resv_iter_is_restarted(). For
  367. * this reason prefer the locked dma_resv_iter_next() whenver possible.
  368. *
  369. * Returns the next fence from an unlocked dma_resv obj.
  370. */
  371. struct dma_fence *dma_resv_iter_next_unlocked(struct dma_resv_iter *cursor)
  372. {
  373. bool restart;
  374. rcu_read_lock();
  375. cursor->is_restarted = false;
  376. restart = dma_resv_fences_list(cursor->obj) != cursor->fences;
  377. do {
  378. if (restart)
  379. dma_resv_iter_restart_unlocked(cursor);
  380. dma_resv_iter_walk_unlocked(cursor);
  381. restart = true;
  382. } while (dma_resv_fences_list(cursor->obj) != cursor->fences);
  383. rcu_read_unlock();
  384. return cursor->fence;
  385. }
  386. EXPORT_SYMBOL(dma_resv_iter_next_unlocked);
  387. /**
  388. * dma_resv_iter_first - first fence from a locked dma_resv object
  389. * @cursor: cursor to record the current position
  390. *
  391. * Subsequent fences are iterated with dma_resv_iter_next_unlocked().
  392. *
  393. * Return the first fence in the dma_resv object while holding the
  394. * &dma_resv.lock.
  395. */
  396. struct dma_fence *dma_resv_iter_first(struct dma_resv_iter *cursor)
  397. {
  398. struct dma_fence *fence;
  399. dma_resv_assert_held(cursor->obj);
  400. cursor->index = 0;
  401. cursor->fences = dma_resv_fences_list(cursor->obj);
  402. fence = dma_resv_iter_next(cursor);
  403. cursor->is_restarted = true;
  404. return fence;
  405. }
  406. EXPORT_SYMBOL_GPL(dma_resv_iter_first);
  407. /**
  408. * dma_resv_iter_next - next fence from a locked dma_resv object
  409. * @cursor: cursor to record the current position
  410. *
  411. * Return the next fences from the dma_resv object while holding the
  412. * &dma_resv.lock.
  413. */
  414. struct dma_fence *dma_resv_iter_next(struct dma_resv_iter *cursor)
  415. {
  416. struct dma_fence *fence;
  417. dma_resv_assert_held(cursor->obj);
  418. cursor->is_restarted = false;
  419. do {
  420. if (!cursor->fences ||
  421. cursor->index >= cursor->fences->num_fences)
  422. return NULL;
  423. dma_resv_list_entry(cursor->fences, cursor->index++,
  424. cursor->obj, &fence, &cursor->fence_usage);
  425. } while (cursor->fence_usage > cursor->usage);
  426. return fence;
  427. }
  428. EXPORT_SYMBOL_GPL(dma_resv_iter_next);
  429. /**
  430. * dma_resv_copy_fences - Copy all fences from src to dst.
  431. * @dst: the destination reservation object
  432. * @src: the source reservation object
  433. *
  434. * Copy all fences from src to dst. dst-lock must be held.
  435. */
  436. int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
  437. {
  438. struct dma_resv_iter cursor;
  439. struct dma_resv_list *list;
  440. struct dma_fence *f;
  441. dma_resv_assert_held(dst);
  442. list = NULL;
  443. dma_resv_iter_begin(&cursor, src, DMA_RESV_USAGE_BOOKKEEP);
  444. dma_resv_for_each_fence_unlocked(&cursor, f) {
  445. if (dma_resv_iter_is_restarted(&cursor)) {
  446. dma_resv_list_free(list);
  447. list = dma_resv_list_alloc(cursor.num_fences);
  448. if (!list) {
  449. dma_resv_iter_end(&cursor);
  450. return -ENOMEM;
  451. }
  452. list->num_fences = 0;
  453. }
  454. dma_fence_get(f);
  455. dma_resv_list_set(list, list->num_fences++, f,
  456. dma_resv_iter_usage(&cursor));
  457. }
  458. dma_resv_iter_end(&cursor);
  459. list = rcu_replace_pointer(dst->fences, list, dma_resv_held(dst));
  460. dma_resv_list_free(list);
  461. return 0;
  462. }
  463. EXPORT_SYMBOL(dma_resv_copy_fences);
  464. /**
  465. * dma_resv_get_fences - Get an object's fences
  466. * fences without update side lock held
  467. * @obj: the reservation object
  468. * @usage: controls which fences to include, see enum dma_resv_usage.
  469. * @num_fences: the number of fences returned
  470. * @fences: the array of fence ptrs returned (array is krealloc'd to the
  471. * required size, and must be freed by caller)
  472. *
  473. * Retrieve all fences from the reservation object.
  474. * Returns either zero or -ENOMEM.
  475. */
  476. int dma_resv_get_fences(struct dma_resv *obj, enum dma_resv_usage usage,
  477. unsigned int *num_fences, struct dma_fence ***fences)
  478. {
  479. struct dma_resv_iter cursor;
  480. struct dma_fence *fence;
  481. *num_fences = 0;
  482. *fences = NULL;
  483. dma_resv_iter_begin(&cursor, obj, usage);
  484. dma_resv_for_each_fence_unlocked(&cursor, fence) {
  485. if (dma_resv_iter_is_restarted(&cursor)) {
  486. struct dma_fence **new_fences;
  487. unsigned int count;
  488. while (*num_fences)
  489. dma_fence_put((*fences)[--(*num_fences)]);
  490. count = cursor.num_fences + 1;
  491. /* Eventually re-allocate the array */
  492. new_fences = krealloc_array(*fences, count,
  493. sizeof(void *),
  494. GFP_KERNEL);
  495. if (count && !new_fences) {
  496. kfree(*fences);
  497. *fences = NULL;
  498. *num_fences = 0;
  499. dma_resv_iter_end(&cursor);
  500. return -ENOMEM;
  501. }
  502. *fences = new_fences;
  503. }
  504. (*fences)[(*num_fences)++] = dma_fence_get(fence);
  505. }
  506. dma_resv_iter_end(&cursor);
  507. return 0;
  508. }
  509. EXPORT_SYMBOL_GPL(dma_resv_get_fences);
  510. /**
  511. * dma_resv_get_singleton - Get a single fence for all the fences
  512. * @obj: the reservation object
  513. * @usage: controls which fences to include, see enum dma_resv_usage.
  514. * @fence: the resulting fence
  515. *
  516. * Get a single fence representing all the fences inside the resv object.
  517. * Returns either 0 for success or -ENOMEM.
  518. *
  519. * Warning: This can't be used like this when adding the fence back to the resv
  520. * object since that can lead to stack corruption when finalizing the
  521. * dma_fence_array.
  522. *
  523. * Returns 0 on success and negative error values on failure.
  524. */
  525. int dma_resv_get_singleton(struct dma_resv *obj, enum dma_resv_usage usage,
  526. struct dma_fence **fence)
  527. {
  528. struct dma_fence_array *array;
  529. struct dma_fence **fences;
  530. unsigned count;
  531. int r;
  532. r = dma_resv_get_fences(obj, usage, &count, &fences);
  533. if (r)
  534. return r;
  535. if (count == 0) {
  536. *fence = NULL;
  537. return 0;
  538. }
  539. if (count == 1) {
  540. *fence = fences[0];
  541. kfree(fences);
  542. return 0;
  543. }
  544. array = dma_fence_array_create(count, fences,
  545. dma_fence_context_alloc(1),
  546. 1, false);
  547. if (!array) {
  548. while (count--)
  549. dma_fence_put(fences[count]);
  550. kfree(fences);
  551. return -ENOMEM;
  552. }
  553. *fence = &array->base;
  554. return 0;
  555. }
  556. EXPORT_SYMBOL_GPL(dma_resv_get_singleton);
  557. /**
  558. * dma_resv_wait_timeout - Wait on reservation's objects fences
  559. * @obj: the reservation object
  560. * @usage: controls which fences to include, see enum dma_resv_usage.
  561. * @intr: if true, do interruptible wait
  562. * @timeout: timeout value in jiffies or zero to return immediately
  563. *
  564. * Callers are not required to hold specific locks, but maybe hold
  565. * dma_resv_lock() already
  566. * RETURNS
  567. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
  568. * greater than zer on success.
  569. */
  570. long dma_resv_wait_timeout(struct dma_resv *obj, enum dma_resv_usage usage,
  571. bool intr, unsigned long timeout)
  572. {
  573. long ret = timeout ? timeout : 1;
  574. struct dma_resv_iter cursor;
  575. struct dma_fence *fence;
  576. dma_resv_iter_begin(&cursor, obj, usage);
  577. dma_resv_for_each_fence_unlocked(&cursor, fence) {
  578. ret = dma_fence_wait_timeout(fence, intr, ret);
  579. if (ret <= 0) {
  580. dma_resv_iter_end(&cursor);
  581. return ret;
  582. }
  583. }
  584. dma_resv_iter_end(&cursor);
  585. return ret;
  586. }
  587. EXPORT_SYMBOL_GPL(dma_resv_wait_timeout);
  588. /**
  589. * dma_resv_test_signaled - Test if a reservation object's fences have been
  590. * signaled.
  591. * @obj: the reservation object
  592. * @usage: controls which fences to include, see enum dma_resv_usage.
  593. *
  594. * Callers are not required to hold specific locks, but maybe hold
  595. * dma_resv_lock() already.
  596. *
  597. * RETURNS
  598. *
  599. * True if all fences signaled, else false.
  600. */
  601. bool dma_resv_test_signaled(struct dma_resv *obj, enum dma_resv_usage usage)
  602. {
  603. struct dma_resv_iter cursor;
  604. struct dma_fence *fence;
  605. dma_resv_iter_begin(&cursor, obj, usage);
  606. dma_resv_for_each_fence_unlocked(&cursor, fence) {
  607. dma_resv_iter_end(&cursor);
  608. return false;
  609. }
  610. dma_resv_iter_end(&cursor);
  611. return true;
  612. }
  613. EXPORT_SYMBOL_GPL(dma_resv_test_signaled);
  614. /**
  615. * dma_resv_describe - Dump description of the resv object into seq_file
  616. * @obj: the reservation object
  617. * @seq: the seq_file to dump the description into
  618. *
  619. * Dump a textual description of the fences inside an dma_resv object into the
  620. * seq_file.
  621. */
  622. void dma_resv_describe(struct dma_resv *obj, struct seq_file *seq)
  623. {
  624. static const char *usage[] = { "kernel", "write", "read", "bookkeep" };
  625. struct dma_resv_iter cursor;
  626. struct dma_fence *fence;
  627. dma_resv_for_each_fence(&cursor, obj, DMA_RESV_USAGE_READ, fence) {
  628. seq_printf(seq, "\t%s fence:",
  629. usage[dma_resv_iter_usage(&cursor)]);
  630. dma_fence_describe(fence, seq);
  631. }
  632. }
  633. EXPORT_SYMBOL_GPL(dma_resv_describe);
  634. #if IS_ENABLED(CONFIG_LOCKDEP)
  635. static int __init dma_resv_lockdep(void)
  636. {
  637. struct mm_struct *mm = mm_alloc();
  638. struct ww_acquire_ctx ctx;
  639. struct dma_resv obj;
  640. struct address_space mapping;
  641. int ret;
  642. if (!mm)
  643. return -ENOMEM;
  644. dma_resv_init(&obj);
  645. address_space_init_once(&mapping);
  646. mmap_read_lock(mm);
  647. ww_acquire_init(&ctx, &reservation_ww_class);
  648. ret = dma_resv_lock(&obj, &ctx);
  649. if (ret == -EDEADLK)
  650. dma_resv_lock_slow(&obj, &ctx);
  651. fs_reclaim_acquire(GFP_KERNEL);
  652. /* for unmap_mapping_range on trylocked buffer objects in shrinkers */
  653. i_mmap_lock_write(&mapping);
  654. i_mmap_unlock_write(&mapping);
  655. #ifdef CONFIG_MMU_NOTIFIER
  656. lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
  657. __dma_fence_might_wait();
  658. lock_map_release(&__mmu_notifier_invalidate_range_start_map);
  659. #else
  660. __dma_fence_might_wait();
  661. #endif
  662. fs_reclaim_release(GFP_KERNEL);
  663. ww_mutex_unlock(&obj.lock);
  664. ww_acquire_fini(&ctx);
  665. mmap_read_unlock(mm);
  666. mmput(mm);
  667. return 0;
  668. }
  669. subsys_initcall(dma_resv_lockdep);
  670. #endif