reservation.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. nshared = krealloc(shared, sz, GFP_KERNEL);
  354. if (nshared) {
  355. shared = nshared;
  356. continue;
  357. }
  358. ret = -ENOMEM;
  359. break;
  360. }
  361. shared = nshared;
  362. shared_count = fobj ? fobj->shared_count : 0;
  363. for (i = 0; i < shared_count; ++i) {
  364. shared[i] = rcu_dereference(fobj->shared[i]);
  365. if (!dma_fence_get_rcu(shared[i]))
  366. break;
  367. }
  368. if (!pfence_excl && fence_excl) {
  369. shared[i] = fence_excl;
  370. fence_excl = NULL;
  371. ++i;
  372. ++shared_count;
  373. }
  374. }
  375. if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
  376. while (i--)
  377. dma_fence_put(shared[i]);
  378. dma_fence_put(fence_excl);
  379. goto unlock;
  380. }
  381. ret = 0;
  382. unlock:
  383. rcu_read_unlock();
  384. } while (ret);
  385. if (!shared_count) {
  386. kfree(shared);
  387. shared = NULL;
  388. }
  389. *pshared_count = shared_count;
  390. *pshared = shared;
  391. if (pfence_excl)
  392. *pfence_excl = fence_excl;
  393. return ret;
  394. }
  395. EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
  396. /**
  397. * reservation_object_wait_timeout_rcu - Wait on reservation's objects
  398. * shared and/or exclusive fences.
  399. * @obj: the reservation object
  400. * @wait_all: if true, wait on all fences, else wait on just exclusive fence
  401. * @intr: if true, do interruptible wait
  402. * @timeout: timeout value in jiffies or zero to return immediately
  403. *
  404. * RETURNS
  405. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
  406. * greater than zer on success.
  407. */
  408. long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
  409. bool wait_all, bool intr,
  410. unsigned long timeout)
  411. {
  412. struct dma_fence *fence;
  413. unsigned seq, shared_count;
  414. long ret = timeout ? timeout : 1;
  415. int i;
  416. retry:
  417. shared_count = 0;
  418. seq = read_seqcount_begin(&obj->seq);
  419. rcu_read_lock();
  420. i = -1;
  421. fence = rcu_dereference(obj->fence_excl);
  422. if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  423. if (!dma_fence_get_rcu(fence))
  424. goto unlock_retry;
  425. if (dma_fence_is_signaled(fence)) {
  426. dma_fence_put(fence);
  427. fence = NULL;
  428. }
  429. } else {
  430. fence = NULL;
  431. }
  432. if (wait_all) {
  433. struct reservation_object_list *fobj =
  434. rcu_dereference(obj->fence);
  435. if (fobj)
  436. shared_count = fobj->shared_count;
  437. for (i = 0; !fence && i < shared_count; ++i) {
  438. struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
  439. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  440. &lfence->flags))
  441. continue;
  442. if (!dma_fence_get_rcu(lfence))
  443. goto unlock_retry;
  444. if (dma_fence_is_signaled(lfence)) {
  445. dma_fence_put(lfence);
  446. continue;
  447. }
  448. fence = lfence;
  449. break;
  450. }
  451. }
  452. rcu_read_unlock();
  453. if (fence) {
  454. if (read_seqcount_retry(&obj->seq, seq)) {
  455. dma_fence_put(fence);
  456. goto retry;
  457. }
  458. ret = dma_fence_wait_timeout(fence, intr, ret);
  459. dma_fence_put(fence);
  460. if (ret > 0 && wait_all && (i + 1 < shared_count))
  461. goto retry;
  462. }
  463. return ret;
  464. unlock_retry:
  465. rcu_read_unlock();
  466. goto retry;
  467. }
  468. EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
  469. static inline int
  470. reservation_object_test_signaled_single(struct dma_fence *passed_fence)
  471. {
  472. struct dma_fence *fence, *lfence = passed_fence;
  473. int ret = 1;
  474. if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
  475. fence = dma_fence_get_rcu(lfence);
  476. if (!fence)
  477. return -1;
  478. ret = !!dma_fence_is_signaled(fence);
  479. dma_fence_put(fence);
  480. }
  481. return ret;
  482. }
  483. /**
  484. * reservation_object_test_signaled_rcu - Test if a reservation object's
  485. * fences have been signaled.
  486. * @obj: the reservation object
  487. * @test_all: if true, test all fences, otherwise only test the exclusive
  488. * fence
  489. *
  490. * RETURNS
  491. * true if all fences signaled, else false
  492. */
  493. bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
  494. bool test_all)
  495. {
  496. unsigned seq, shared_count;
  497. int ret;
  498. rcu_read_lock();
  499. retry:
  500. ret = true;
  501. shared_count = 0;
  502. seq = read_seqcount_begin(&obj->seq);
  503. if (test_all) {
  504. unsigned i;
  505. struct reservation_object_list *fobj =
  506. rcu_dereference(obj->fence);
  507. if (fobj)
  508. shared_count = fobj->shared_count;
  509. for (i = 0; i < shared_count; ++i) {
  510. struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
  511. ret = reservation_object_test_signaled_single(fence);
  512. if (ret < 0)
  513. goto retry;
  514. else if (!ret)
  515. break;
  516. }
  517. if (read_seqcount_retry(&obj->seq, seq))
  518. goto retry;
  519. }
  520. if (!shared_count) {
  521. struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
  522. if (fence_excl) {
  523. ret = reservation_object_test_signaled_single(
  524. fence_excl);
  525. if (ret < 0)
  526. goto retry;
  527. if (read_seqcount_retry(&obj->seq, seq))
  528. goto retry;
  529. }
  530. }
  531. rcu_read_unlock();
  532. return ret;
  533. }
  534. EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);