reservation.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
  3. *
  4. * Based on bo.c which bears the following copyright notice,
  5. * but is dual licensed:
  6. *
  7. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  8. * All Rights Reserved.
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a
  11. * copy of this software and associated documentation files (the
  12. * "Software"), to deal in the Software without restriction, including
  13. * without limitation the rights to use, copy, modify, merge, publish,
  14. * distribute, sub license, and/or sell copies of the Software, and to
  15. * permit persons to whom the Software is furnished to do so, subject to
  16. * the following conditions:
  17. *
  18. * The above copyright notice and this permission notice (including the
  19. * next paragraph) shall be included in all copies or substantial portions
  20. * of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  25. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  26. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  27. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  28. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. *
  30. **************************************************************************/
  31. /*
  32. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  33. */
  34. #include <linux/reservation.h>
  35. #include <linux/export.h>
  36. /**
  37. * DOC: Reservation Object Overview
  38. *
  39. * The reservation object provides a mechanism to manage shared and
  40. * exclusive fences associated with a buffer. A reservation object
  41. * can have attached one exclusive fence (normally associated with
  42. * write operations) or N shared fences (read operations). The RCU
  43. * mechanism is used to protect read access to fences from locked
  44. * write-side updates.
  45. */
  46. DEFINE_WD_CLASS(reservation_ww_class);
  47. EXPORT_SYMBOL(reservation_ww_class);
  48. struct lock_class_key reservation_seqcount_class;
  49. EXPORT_SYMBOL(reservation_seqcount_class);
  50. const char reservation_seqcount_string[] = "reservation_seqcount";
  51. EXPORT_SYMBOL(reservation_seqcount_string);
  52. /**
  53. * reservation_object_reserve_shared - Reserve space to add a shared
  54. * fence to a reservation_object.
  55. * @obj: reservation object
  56. *
  57. * Should be called before reservation_object_add_shared_fence(). Must
  58. * be called with obj->lock held.
  59. *
  60. * RETURNS
  61. * Zero for success, or -errno
  62. */
  63. int reservation_object_reserve_shared(struct reservation_object *obj)
  64. {
  65. struct reservation_object_list *fobj, *old;
  66. u32 max;
  67. old = reservation_object_get_list(obj);
  68. if (old && old->shared_max) {
  69. if (old->shared_count < old->shared_max) {
  70. /* perform an in-place update */
  71. kfree(obj->staged);
  72. obj->staged = NULL;
  73. return 0;
  74. } else
  75. max = old->shared_max * 2;
  76. } else
  77. max = 4;
  78. /*
  79. * resize obj->staged or allocate if it doesn't exist,
  80. * noop if already correct size
  81. */
  82. fobj = krealloc(obj->staged, offsetof(typeof(*fobj), shared[max]),
  83. GFP_KERNEL);
  84. if (!fobj)
  85. return -ENOMEM;
  86. obj->staged = fobj;
  87. fobj->shared_max = max;
  88. return 0;
  89. }
  90. EXPORT_SYMBOL(reservation_object_reserve_shared);
  91. static void
  92. reservation_object_add_shared_inplace(struct reservation_object *obj,
  93. struct reservation_object_list *fobj,
  94. struct dma_fence *fence)
  95. {
  96. struct dma_fence *signaled = NULL;
  97. u32 i, signaled_idx;
  98. dma_fence_get(fence);
  99. preempt_disable();
  100. write_seqcount_begin(&obj->seq);
  101. for (i = 0; i < fobj->shared_count; ++i) {
  102. struct dma_fence *old_fence;
  103. old_fence = rcu_dereference_protected(fobj->shared[i],
  104. reservation_object_held(obj));
  105. if (old_fence->context == fence->context) {
  106. /* memory barrier is added by write_seqcount_begin */
  107. RCU_INIT_POINTER(fobj->shared[i], fence);
  108. write_seqcount_end(&obj->seq);
  109. preempt_enable();
  110. dma_fence_put(old_fence);
  111. return;
  112. }
  113. if (!signaled && dma_fence_is_signaled(old_fence)) {
  114. signaled = old_fence;
  115. signaled_idx = i;
  116. }
  117. }
  118. /*
  119. * memory barrier is added by write_seqcount_begin,
  120. * fobj->shared_count is protected by this lock too
  121. */
  122. if (signaled) {
  123. RCU_INIT_POINTER(fobj->shared[signaled_idx], fence);
  124. } else {
  125. BUG_ON(fobj->shared_count >= fobj->shared_max);
  126. RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
  127. fobj->shared_count++;
  128. }
  129. write_seqcount_end(&obj->seq);
  130. preempt_enable();
  131. dma_fence_put(signaled);
  132. }
  133. static void
  134. reservation_object_add_shared_replace(struct reservation_object *obj,
  135. struct reservation_object_list *old,
  136. struct reservation_object_list *fobj,
  137. struct dma_fence *fence)
  138. {
  139. unsigned i, j, k;
  140. dma_fence_get(fence);
  141. if (!old) {
  142. RCU_INIT_POINTER(fobj->shared[0], fence);
  143. fobj->shared_count = 1;
  144. goto done;
  145. }
  146. /*
  147. * no need to bump fence refcounts, rcu_read access
  148. * requires the use of kref_get_unless_zero, and the
  149. * references from the old struct are carried over to
  150. * the new.
  151. */
  152. for (i = 0, j = 0, k = fobj->shared_max; i < old->shared_count; ++i) {
  153. struct dma_fence *check;
  154. check = rcu_dereference_protected(old->shared[i],
  155. reservation_object_held(obj));
  156. if (check->context == fence->context ||
  157. dma_fence_is_signaled(check))
  158. RCU_INIT_POINTER(fobj->shared[--k], check);
  159. else
  160. RCU_INIT_POINTER(fobj->shared[j++], check);
  161. }
  162. fobj->shared_count = j;
  163. RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
  164. fobj->shared_count++;
  165. done:
  166. preempt_disable();
  167. write_seqcount_begin(&obj->seq);
  168. /*
  169. * RCU_INIT_POINTER can be used here,
  170. * seqcount provides the necessary barriers
  171. */
  172. RCU_INIT_POINTER(obj->fence, fobj);
  173. write_seqcount_end(&obj->seq);
  174. preempt_enable();
  175. if (!old)
  176. return;
  177. /* Drop the references to the signaled fences */
  178. for (i = k; i < fobj->shared_max; ++i) {
  179. struct dma_fence *f;
  180. f = rcu_dereference_protected(fobj->shared[i],
  181. reservation_object_held(obj));
  182. dma_fence_put(f);
  183. }
  184. kfree_rcu(old, rcu);
  185. }
  186. /**
  187. * reservation_object_add_shared_fence - Add a fence to a shared slot
  188. * @obj: the reservation object
  189. * @fence: the shared fence to add
  190. *
  191. * Add a fence to a shared slot, obj->lock must be held, and
  192. * reservation_object_reserve_shared() has been called.
  193. */
  194. void reservation_object_add_shared_fence(struct reservation_object *obj,
  195. struct dma_fence *fence)
  196. {
  197. struct reservation_object_list *old, *fobj = obj->staged;
  198. old = reservation_object_get_list(obj);
  199. obj->staged = NULL;
  200. if (!fobj)
  201. reservation_object_add_shared_inplace(obj, old, fence);
  202. else
  203. reservation_object_add_shared_replace(obj, old, fobj, fence);
  204. }
  205. EXPORT_SYMBOL(reservation_object_add_shared_fence);
  206. /**
  207. * reservation_object_add_excl_fence - Add an exclusive fence.
  208. * @obj: the reservation object
  209. * @fence: the shared fence to add
  210. *
  211. * Add a fence to the exclusive slot. The obj->lock must be held.
  212. */
  213. void reservation_object_add_excl_fence(struct reservation_object *obj,
  214. struct dma_fence *fence)
  215. {
  216. struct dma_fence *old_fence = reservation_object_get_excl(obj);
  217. struct reservation_object_list *old;
  218. u32 i = 0;
  219. old = reservation_object_get_list(obj);
  220. if (old)
  221. i = old->shared_count;
  222. if (fence)
  223. dma_fence_get(fence);
  224. preempt_disable();
  225. write_seqcount_begin(&obj->seq);
  226. /* write_seqcount_begin provides the necessary memory barrier */
  227. RCU_INIT_POINTER(obj->fence_excl, fence);
  228. if (old)
  229. old->shared_count = 0;
  230. write_seqcount_end(&obj->seq);
  231. preempt_enable();
  232. /* inplace update, no shared fences */
  233. while (i--)
  234. dma_fence_put(rcu_dereference_protected(old->shared[i],
  235. reservation_object_held(obj)));
  236. dma_fence_put(old_fence);
  237. }
  238. EXPORT_SYMBOL(reservation_object_add_excl_fence);
  239. /**
  240. * reservation_object_copy_fences - Copy all fences from src to dst.
  241. * @dst: the destination reservation object
  242. * @src: the source reservation object
  243. *
  244. * Copy all fences from src to dst. dst-lock must be held.
  245. */
  246. int reservation_object_copy_fences(struct reservation_object *dst,
  247. struct reservation_object *src)
  248. {
  249. struct reservation_object_list *src_list, *dst_list;
  250. struct dma_fence *old, *new;
  251. size_t size;
  252. unsigned i;
  253. rcu_read_lock();
  254. src_list = rcu_dereference(src->fence);
  255. retry:
  256. if (src_list) {
  257. unsigned shared_count = src_list->shared_count;
  258. size = offsetof(typeof(*src_list), shared[shared_count]);
  259. rcu_read_unlock();
  260. dst_list = kmalloc(size, GFP_KERNEL);
  261. if (!dst_list)
  262. return -ENOMEM;
  263. rcu_read_lock();
  264. src_list = rcu_dereference(src->fence);
  265. if (!src_list || src_list->shared_count > shared_count) {
  266. kfree(dst_list);
  267. goto retry;
  268. }
  269. dst_list->shared_count = 0;
  270. dst_list->shared_max = shared_count;
  271. for (i = 0; i < src_list->shared_count; ++i) {
  272. struct dma_fence *fence;
  273. fence = rcu_dereference(src_list->shared[i]);
  274. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  275. &fence->flags))
  276. continue;
  277. if (!dma_fence_get_rcu(fence)) {
  278. kfree(dst_list);
  279. src_list = rcu_dereference(src->fence);
  280. goto retry;
  281. }
  282. if (dma_fence_is_signaled(fence)) {
  283. dma_fence_put(fence);
  284. continue;
  285. }
  286. rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
  287. }
  288. } else {
  289. dst_list = NULL;
  290. }
  291. new = dma_fence_get_rcu_safe(&src->fence_excl);
  292. rcu_read_unlock();
  293. kfree(dst->staged);
  294. dst->staged = NULL;
  295. src_list = reservation_object_get_list(dst);
  296. old = reservation_object_get_excl(dst);
  297. preempt_disable();
  298. write_seqcount_begin(&dst->seq);
  299. /* write_seqcount_begin provides the necessary memory barrier */
  300. RCU_INIT_POINTER(dst->fence_excl, new);
  301. RCU_INIT_POINTER(dst->fence, dst_list);
  302. write_seqcount_end(&dst->seq);
  303. preempt_enable();
  304. if (src_list)
  305. kfree_rcu(src_list, rcu);
  306. dma_fence_put(old);
  307. return 0;
  308. }
  309. EXPORT_SYMBOL(reservation_object_copy_fences);
  310. /**
  311. * reservation_object_get_fences_rcu - Get an object's shared and exclusive
  312. * fences without update side lock held
  313. * @obj: the reservation object
  314. * @pfence_excl: the returned exclusive fence (or NULL)
  315. * @pshared_count: the number of shared fences returned
  316. * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
  317. * the required size, and must be freed by caller)
  318. *
  319. * Retrieve all fences from the reservation object. If the pointer for the
  320. * exclusive fence is not specified the fence is put into the array of the
  321. * shared fences as well. Returns either zero or -ENOMEM.
  322. */
  323. int reservation_object_get_fences_rcu(struct reservation_object *obj,
  324. struct dma_fence **pfence_excl,
  325. unsigned *pshared_count,
  326. struct dma_fence ***pshared)
  327. {
  328. struct dma_fence **shared = NULL;
  329. struct dma_fence *fence_excl;
  330. unsigned int shared_count;
  331. int ret = 1;
  332. do {
  333. struct reservation_object_list *fobj;
  334. unsigned int i, seq;
  335. size_t sz = 0;
  336. shared_count = i = 0;
  337. rcu_read_lock();
  338. seq = read_seqcount_begin(&obj->seq);
  339. fence_excl = rcu_dereference(obj->fence_excl);
  340. if (fence_excl && !dma_fence_get_rcu(fence_excl))
  341. goto unlock;
  342. fobj = rcu_dereference(obj->fence);
  343. if (fobj)
  344. sz += sizeof(*shared) * fobj->shared_max;
  345. if (!pfence_excl && fence_excl)
  346. sz += sizeof(*shared);
  347. if (sz) {
  348. struct dma_fence **nshared;
  349. nshared = krealloc(shared, sz,
  350. GFP_NOWAIT | __GFP_NOWARN);
  351. if (!nshared) {
  352. rcu_read_unlock();
  353. dma_fence_put(fence_excl);
  354. fence_excl = NULL;
  355. nshared = krealloc(shared, sz, GFP_KERNEL);
  356. if (nshared) {
  357. shared = nshared;
  358. continue;
  359. }
  360. ret = -ENOMEM;
  361. break;
  362. }
  363. shared = nshared;
  364. shared_count = fobj ? fobj->shared_count : 0;
  365. for (i = 0; i < shared_count; ++i) {
  366. shared[i] = rcu_dereference(fobj->shared[i]);
  367. if (!dma_fence_get_rcu(shared[i]))
  368. break;
  369. }
  370. if (!pfence_excl && fence_excl) {
  371. shared[i] = fence_excl;
  372. fence_excl = NULL;
  373. ++i;
  374. ++shared_count;
  375. }
  376. }
  377. if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
  378. while (i--)
  379. dma_fence_put(shared[i]);
  380. dma_fence_put(fence_excl);
  381. goto unlock;
  382. }
  383. ret = 0;
  384. unlock:
  385. rcu_read_unlock();
  386. } while (ret);
  387. if (!shared_count) {
  388. kfree(shared);
  389. shared = NULL;
  390. }
  391. *pshared_count = shared_count;
  392. *pshared = shared;
  393. if (pfence_excl)
  394. *pfence_excl = fence_excl;
  395. return ret;
  396. }
  397. EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
  398. /**
  399. * reservation_object_wait_timeout_rcu - Wait on reservation's objects
  400. * shared and/or exclusive fences.
  401. * @obj: the reservation object
  402. * @wait_all: if true, wait on all fences, else wait on just exclusive fence
  403. * @intr: if true, do interruptible wait
  404. * @timeout: timeout value in jiffies or zero to return immediately
  405. *
  406. * RETURNS
  407. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
  408. * greater than zer on success.
  409. */
  410. long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
  411. bool wait_all, bool intr,
  412. unsigned long timeout)
  413. {
  414. struct dma_fence *fence;
  415. unsigned seq, shared_count;
  416. long ret = timeout ? timeout : 1;
  417. int i;
  418. retry:
  419. shared_count = 0;
  420. seq = read_seqcount_begin(&obj->seq);
  421. rcu_read_lock();
  422. i = -1;
  423. fence = rcu_dereference(obj->fence_excl);
  424. if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  425. if (!dma_fence_get_rcu(fence))
  426. goto unlock_retry;
  427. if (dma_fence_is_signaled(fence)) {
  428. dma_fence_put(fence);
  429. fence = NULL;
  430. }
  431. } else {
  432. fence = NULL;
  433. }
  434. if (wait_all) {
  435. struct reservation_object_list *fobj =
  436. rcu_dereference(obj->fence);
  437. if (fobj)
  438. shared_count = fobj->shared_count;
  439. for (i = 0; !fence && i < shared_count; ++i) {
  440. struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
  441. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  442. &lfence->flags))
  443. continue;
  444. if (!dma_fence_get_rcu(lfence))
  445. goto unlock_retry;
  446. if (dma_fence_is_signaled(lfence)) {
  447. dma_fence_put(lfence);
  448. continue;
  449. }
  450. fence = lfence;
  451. break;
  452. }
  453. }
  454. rcu_read_unlock();
  455. if (fence) {
  456. if (read_seqcount_retry(&obj->seq, seq)) {
  457. dma_fence_put(fence);
  458. goto retry;
  459. }
  460. ret = dma_fence_wait_timeout(fence, intr, ret);
  461. dma_fence_put(fence);
  462. if (ret > 0 && wait_all && (i + 1 < shared_count))
  463. goto retry;
  464. }
  465. return ret;
  466. unlock_retry:
  467. rcu_read_unlock();
  468. goto retry;
  469. }
  470. EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
  471. static inline int
  472. reservation_object_test_signaled_single(struct dma_fence *passed_fence)
  473. {
  474. struct dma_fence *fence, *lfence = passed_fence;
  475. int ret = 1;
  476. if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
  477. fence = dma_fence_get_rcu(lfence);
  478. if (!fence)
  479. return -1;
  480. ret = !!dma_fence_is_signaled(fence);
  481. dma_fence_put(fence);
  482. }
  483. return ret;
  484. }
  485. /**
  486. * reservation_object_test_signaled_rcu - Test if a reservation object's
  487. * fences have been signaled.
  488. * @obj: the reservation object
  489. * @test_all: if true, test all fences, otherwise only test the exclusive
  490. * fence
  491. *
  492. * RETURNS
  493. * true if all fences signaled, else false
  494. */
  495. bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
  496. bool test_all)
  497. {
  498. unsigned seq, shared_count;
  499. int ret;
  500. rcu_read_lock();
  501. retry:
  502. ret = true;
  503. shared_count = 0;
  504. seq = read_seqcount_begin(&obj->seq);
  505. if (test_all) {
  506. unsigned i;
  507. struct reservation_object_list *fobj =
  508. rcu_dereference(obj->fence);
  509. if (fobj)
  510. shared_count = fobj->shared_count;
  511. for (i = 0; i < shared_count; ++i) {
  512. struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
  513. ret = reservation_object_test_signaled_single(fence);
  514. if (ret < 0)
  515. goto retry;
  516. else if (!ret)
  517. break;
  518. }
  519. if (read_seqcount_retry(&obj->seq, seq))
  520. goto retry;
  521. }
  522. if (!shared_count) {
  523. struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
  524. if (fence_excl) {
  525. ret = reservation_object_test_signaled_single(
  526. fence_excl);
  527. if (ret < 0)
  528. goto retry;
  529. if (read_seqcount_retry(&obj->seq, seq))
  530. goto retry;
  531. }
  532. }
  533. rcu_read_unlock();
  534. return ret;
  535. }
  536. EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);