reservation.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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_WW_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. u32 i;
  97. dma_fence_get(fence);
  98. preempt_disable();
  99. write_seqcount_begin(&obj->seq);
  100. for (i = 0; i < fobj->shared_count; ++i) {
  101. struct dma_fence *old_fence;
  102. old_fence = rcu_dereference_protected(fobj->shared[i],
  103. reservation_object_held(obj));
  104. if (old_fence->context == fence->context) {
  105. /* memory barrier is added by write_seqcount_begin */
  106. RCU_INIT_POINTER(fobj->shared[i], fence);
  107. write_seqcount_end(&obj->seq);
  108. preempt_enable();
  109. dma_fence_put(old_fence);
  110. return;
  111. }
  112. }
  113. /*
  114. * memory barrier is added by write_seqcount_begin,
  115. * fobj->shared_count is protected by this lock too
  116. */
  117. RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
  118. fobj->shared_count++;
  119. write_seqcount_end(&obj->seq);
  120. preempt_enable();
  121. }
  122. static void
  123. reservation_object_add_shared_replace(struct reservation_object *obj,
  124. struct reservation_object_list *old,
  125. struct reservation_object_list *fobj,
  126. struct dma_fence *fence)
  127. {
  128. unsigned i;
  129. struct dma_fence *old_fence = NULL;
  130. dma_fence_get(fence);
  131. if (!old) {
  132. RCU_INIT_POINTER(fobj->shared[0], fence);
  133. fobj->shared_count = 1;
  134. goto done;
  135. }
  136. /*
  137. * no need to bump fence refcounts, rcu_read access
  138. * requires the use of kref_get_unless_zero, and the
  139. * references from the old struct are carried over to
  140. * the new.
  141. */
  142. fobj->shared_count = old->shared_count;
  143. for (i = 0; i < old->shared_count; ++i) {
  144. struct dma_fence *check;
  145. check = rcu_dereference_protected(old->shared[i],
  146. reservation_object_held(obj));
  147. if (!old_fence && check->context == fence->context) {
  148. old_fence = check;
  149. RCU_INIT_POINTER(fobj->shared[i], fence);
  150. } else
  151. RCU_INIT_POINTER(fobj->shared[i], check);
  152. }
  153. if (!old_fence) {
  154. RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
  155. fobj->shared_count++;
  156. }
  157. done:
  158. preempt_disable();
  159. write_seqcount_begin(&obj->seq);
  160. /*
  161. * RCU_INIT_POINTER can be used here,
  162. * seqcount provides the necessary barriers
  163. */
  164. RCU_INIT_POINTER(obj->fence, fobj);
  165. write_seqcount_end(&obj->seq);
  166. preempt_enable();
  167. if (old)
  168. kfree_rcu(old, rcu);
  169. dma_fence_put(old_fence);
  170. }
  171. /**
  172. * reservation_object_add_shared_fence - Add a fence to a shared slot
  173. * @obj: the reservation object
  174. * @fence: the shared fence to add
  175. *
  176. * Add a fence to a shared slot, obj->lock must be held, and
  177. * reservation_object_reserve_shared() has been called.
  178. */
  179. void reservation_object_add_shared_fence(struct reservation_object *obj,
  180. struct dma_fence *fence)
  181. {
  182. struct reservation_object_list *old, *fobj = obj->staged;
  183. old = reservation_object_get_list(obj);
  184. obj->staged = NULL;
  185. if (!fobj) {
  186. BUG_ON(old->shared_count >= old->shared_max);
  187. reservation_object_add_shared_inplace(obj, old, fence);
  188. } else
  189. reservation_object_add_shared_replace(obj, old, fobj, fence);
  190. }
  191. EXPORT_SYMBOL(reservation_object_add_shared_fence);
  192. /**
  193. * reservation_object_add_excl_fence - Add an exclusive fence.
  194. * @obj: the reservation object
  195. * @fence: the shared fence to add
  196. *
  197. * Add a fence to the exclusive slot. The obj->lock must be held.
  198. */
  199. void reservation_object_add_excl_fence(struct reservation_object *obj,
  200. struct dma_fence *fence)
  201. {
  202. struct dma_fence *old_fence = reservation_object_get_excl(obj);
  203. struct reservation_object_list *old;
  204. u32 i = 0;
  205. old = reservation_object_get_list(obj);
  206. if (old)
  207. i = old->shared_count;
  208. if (fence)
  209. dma_fence_get(fence);
  210. preempt_disable();
  211. write_seqcount_begin(&obj->seq);
  212. /* write_seqcount_begin provides the necessary memory barrier */
  213. RCU_INIT_POINTER(obj->fence_excl, fence);
  214. if (old)
  215. old->shared_count = 0;
  216. write_seqcount_end(&obj->seq);
  217. preempt_enable();
  218. /* inplace update, no shared fences */
  219. while (i--)
  220. dma_fence_put(rcu_dereference_protected(old->shared[i],
  221. reservation_object_held(obj)));
  222. dma_fence_put(old_fence);
  223. }
  224. EXPORT_SYMBOL(reservation_object_add_excl_fence);
  225. /**
  226. * reservation_object_copy_fences - Copy all fences from src to dst.
  227. * @dst: the destination reservation object
  228. * @src: the source reservation object
  229. *
  230. * Copy all fences from src to dst. dst-lock must be held.
  231. */
  232. int reservation_object_copy_fences(struct reservation_object *dst,
  233. struct reservation_object *src)
  234. {
  235. struct reservation_object_list *src_list, *dst_list;
  236. struct dma_fence *old, *new;
  237. size_t size;
  238. unsigned i;
  239. rcu_read_lock();
  240. src_list = rcu_dereference(src->fence);
  241. retry:
  242. if (src_list) {
  243. unsigned shared_count = src_list->shared_count;
  244. size = offsetof(typeof(*src_list), shared[shared_count]);
  245. rcu_read_unlock();
  246. dst_list = kmalloc(size, GFP_KERNEL);
  247. if (!dst_list)
  248. return -ENOMEM;
  249. rcu_read_lock();
  250. src_list = rcu_dereference(src->fence);
  251. if (!src_list || src_list->shared_count > shared_count) {
  252. kfree(dst_list);
  253. goto retry;
  254. }
  255. dst_list->shared_count = 0;
  256. dst_list->shared_max = shared_count;
  257. for (i = 0; i < src_list->shared_count; ++i) {
  258. struct dma_fence *fence;
  259. fence = rcu_dereference(src_list->shared[i]);
  260. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  261. &fence->flags))
  262. continue;
  263. if (!dma_fence_get_rcu(fence)) {
  264. kfree(dst_list);
  265. src_list = rcu_dereference(src->fence);
  266. goto retry;
  267. }
  268. if (dma_fence_is_signaled(fence)) {
  269. dma_fence_put(fence);
  270. continue;
  271. }
  272. dst_list->shared[dst_list->shared_count++] = fence;
  273. }
  274. } else {
  275. dst_list = NULL;
  276. }
  277. new = dma_fence_get_rcu_safe(&src->fence_excl);
  278. rcu_read_unlock();
  279. kfree(dst->staged);
  280. dst->staged = NULL;
  281. src_list = reservation_object_get_list(dst);
  282. old = reservation_object_get_excl(dst);
  283. preempt_disable();
  284. write_seqcount_begin(&dst->seq);
  285. /* write_seqcount_begin provides the necessary memory barrier */
  286. RCU_INIT_POINTER(dst->fence_excl, new);
  287. RCU_INIT_POINTER(dst->fence, dst_list);
  288. write_seqcount_end(&dst->seq);
  289. preempt_enable();
  290. if (src_list)
  291. kfree_rcu(src_list, rcu);
  292. dma_fence_put(old);
  293. return 0;
  294. }
  295. EXPORT_SYMBOL(reservation_object_copy_fences);
  296. /**
  297. * reservation_object_get_fences_rcu - Get an object's shared and exclusive
  298. * fences without update side lock held
  299. * @obj: the reservation object
  300. * @pfence_excl: the returned exclusive fence (or NULL)
  301. * @pshared_count: the number of shared fences returned
  302. * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
  303. * the required size, and must be freed by caller)
  304. *
  305. * RETURNS
  306. * Zero or -errno
  307. */
  308. int reservation_object_get_fences_rcu(struct reservation_object *obj,
  309. struct dma_fence **pfence_excl,
  310. unsigned *pshared_count,
  311. struct dma_fence ***pshared)
  312. {
  313. struct dma_fence **shared = NULL;
  314. struct dma_fence *fence_excl;
  315. unsigned int shared_count;
  316. int ret = 1;
  317. do {
  318. struct reservation_object_list *fobj;
  319. unsigned seq;
  320. unsigned int i;
  321. shared_count = i = 0;
  322. rcu_read_lock();
  323. seq = read_seqcount_begin(&obj->seq);
  324. fence_excl = rcu_dereference(obj->fence_excl);
  325. if (fence_excl && !dma_fence_get_rcu(fence_excl))
  326. goto unlock;
  327. fobj = rcu_dereference(obj->fence);
  328. if (fobj) {
  329. struct dma_fence **nshared;
  330. size_t sz = sizeof(*shared) * fobj->shared_max;
  331. nshared = krealloc(shared, sz,
  332. GFP_NOWAIT | __GFP_NOWARN);
  333. if (!nshared) {
  334. rcu_read_unlock();
  335. dma_fence_put(fence_excl);
  336. fence_excl = NULL;
  337. nshared = krealloc(shared, sz, GFP_KERNEL);
  338. if (nshared) {
  339. shared = nshared;
  340. continue;
  341. }
  342. ret = -ENOMEM;
  343. break;
  344. }
  345. shared = nshared;
  346. shared_count = fobj->shared_count;
  347. for (i = 0; i < shared_count; ++i) {
  348. shared[i] = rcu_dereference(fobj->shared[i]);
  349. if (!dma_fence_get_rcu(shared[i]))
  350. break;
  351. }
  352. }
  353. if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
  354. while (i--)
  355. dma_fence_put(shared[i]);
  356. dma_fence_put(fence_excl);
  357. goto unlock;
  358. }
  359. ret = 0;
  360. unlock:
  361. rcu_read_unlock();
  362. } while (ret);
  363. if (!shared_count) {
  364. kfree(shared);
  365. shared = NULL;
  366. }
  367. *pshared_count = shared_count;
  368. *pshared = shared;
  369. *pfence_excl = fence_excl;
  370. return ret;
  371. }
  372. EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
  373. /**
  374. * reservation_object_wait_timeout_rcu - Wait on reservation's objects
  375. * shared and/or exclusive fences.
  376. * @obj: the reservation object
  377. * @wait_all: if true, wait on all fences, else wait on just exclusive fence
  378. * @intr: if true, do interruptible wait
  379. * @timeout: timeout value in jiffies or zero to return immediately
  380. *
  381. * RETURNS
  382. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
  383. * greater than zer on success.
  384. */
  385. long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
  386. bool wait_all, bool intr,
  387. unsigned long timeout)
  388. {
  389. struct dma_fence *fence;
  390. unsigned seq, shared_count;
  391. long ret = timeout ? timeout : 1;
  392. int i;
  393. retry:
  394. shared_count = 0;
  395. seq = read_seqcount_begin(&obj->seq);
  396. rcu_read_lock();
  397. i = -1;
  398. fence = rcu_dereference(obj->fence_excl);
  399. if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  400. if (!dma_fence_get_rcu(fence))
  401. goto unlock_retry;
  402. if (dma_fence_is_signaled(fence)) {
  403. dma_fence_put(fence);
  404. fence = NULL;
  405. }
  406. } else {
  407. fence = NULL;
  408. }
  409. if (wait_all) {
  410. struct reservation_object_list *fobj =
  411. rcu_dereference(obj->fence);
  412. if (fobj)
  413. shared_count = fobj->shared_count;
  414. for (i = 0; !fence && i < shared_count; ++i) {
  415. struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
  416. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  417. &lfence->flags))
  418. continue;
  419. if (!dma_fence_get_rcu(lfence))
  420. goto unlock_retry;
  421. if (dma_fence_is_signaled(lfence)) {
  422. dma_fence_put(lfence);
  423. continue;
  424. }
  425. fence = lfence;
  426. break;
  427. }
  428. }
  429. rcu_read_unlock();
  430. if (fence) {
  431. if (read_seqcount_retry(&obj->seq, seq)) {
  432. dma_fence_put(fence);
  433. goto retry;
  434. }
  435. ret = dma_fence_wait_timeout(fence, intr, ret);
  436. dma_fence_put(fence);
  437. if (ret > 0 && wait_all && (i + 1 < shared_count))
  438. goto retry;
  439. }
  440. return ret;
  441. unlock_retry:
  442. rcu_read_unlock();
  443. goto retry;
  444. }
  445. EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
  446. static inline int
  447. reservation_object_test_signaled_single(struct dma_fence *passed_fence)
  448. {
  449. struct dma_fence *fence, *lfence = passed_fence;
  450. int ret = 1;
  451. if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
  452. fence = dma_fence_get_rcu(lfence);
  453. if (!fence)
  454. return -1;
  455. ret = !!dma_fence_is_signaled(fence);
  456. dma_fence_put(fence);
  457. }
  458. return ret;
  459. }
  460. /**
  461. * reservation_object_test_signaled_rcu - Test if a reservation object's
  462. * fences have been signaled.
  463. * @obj: the reservation object
  464. * @test_all: if true, test all fences, otherwise only test the exclusive
  465. * fence
  466. *
  467. * RETURNS
  468. * true if all fences signaled, else false
  469. */
  470. bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
  471. bool test_all)
  472. {
  473. unsigned seq, shared_count;
  474. int ret;
  475. rcu_read_lock();
  476. retry:
  477. ret = true;
  478. shared_count = 0;
  479. seq = read_seqcount_begin(&obj->seq);
  480. if (test_all) {
  481. unsigned i;
  482. struct reservation_object_list *fobj =
  483. rcu_dereference(obj->fence);
  484. if (fobj)
  485. shared_count = fobj->shared_count;
  486. for (i = 0; i < shared_count; ++i) {
  487. struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
  488. ret = reservation_object_test_signaled_single(fence);
  489. if (ret < 0)
  490. goto retry;
  491. else if (!ret)
  492. break;
  493. }
  494. if (read_seqcount_retry(&obj->seq, seq))
  495. goto retry;
  496. }
  497. if (!shared_count) {
  498. struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
  499. if (fence_excl) {
  500. ret = reservation_object_test_signaled_single(
  501. fence_excl);
  502. if (ret < 0)
  503. goto retry;
  504. if (read_seqcount_retry(&obj->seq, seq))
  505. goto retry;
  506. }
  507. }
  508. rcu_read_unlock();
  509. return ret;
  510. }
  511. EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);