reservation.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. if (old_fence)
  170. dma_fence_put(old_fence);
  171. }
  172. /**
  173. * reservation_object_add_shared_fence - Add a fence to a shared slot
  174. * @obj: the reservation object
  175. * @fence: the shared fence to add
  176. *
  177. * Add a fence to a shared slot, obj->lock must be held, and
  178. * reservation_object_reserve_shared() has been called.
  179. */
  180. void reservation_object_add_shared_fence(struct reservation_object *obj,
  181. struct dma_fence *fence)
  182. {
  183. struct reservation_object_list *old, *fobj = obj->staged;
  184. old = reservation_object_get_list(obj);
  185. obj->staged = NULL;
  186. if (!fobj) {
  187. BUG_ON(old->shared_count >= old->shared_max);
  188. reservation_object_add_shared_inplace(obj, old, fence);
  189. } else
  190. reservation_object_add_shared_replace(obj, old, fobj, fence);
  191. }
  192. EXPORT_SYMBOL(reservation_object_add_shared_fence);
  193. /**
  194. * reservation_object_add_excl_fence - Add an exclusive fence.
  195. * @obj: the reservation object
  196. * @fence: the shared fence to add
  197. *
  198. * Add a fence to the exclusive slot. The obj->lock must be held.
  199. */
  200. void reservation_object_add_excl_fence(struct reservation_object *obj,
  201. struct dma_fence *fence)
  202. {
  203. struct dma_fence *old_fence = reservation_object_get_excl(obj);
  204. struct reservation_object_list *old;
  205. u32 i = 0;
  206. old = reservation_object_get_list(obj);
  207. if (old)
  208. i = old->shared_count;
  209. if (fence)
  210. dma_fence_get(fence);
  211. preempt_disable();
  212. write_seqcount_begin(&obj->seq);
  213. /* write_seqcount_begin provides the necessary memory barrier */
  214. RCU_INIT_POINTER(obj->fence_excl, fence);
  215. if (old)
  216. old->shared_count = 0;
  217. write_seqcount_end(&obj->seq);
  218. preempt_enable();
  219. /* inplace update, no shared fences */
  220. while (i--)
  221. dma_fence_put(rcu_dereference_protected(old->shared[i],
  222. reservation_object_held(obj)));
  223. if (old_fence)
  224. dma_fence_put(old_fence);
  225. }
  226. EXPORT_SYMBOL(reservation_object_add_excl_fence);
  227. /**
  228. * reservation_object_get_fences_rcu - Get an object's shared and exclusive
  229. * fences without update side lock held
  230. * @obj: the reservation object
  231. * @pfence_excl: the returned exclusive fence (or NULL)
  232. * @pshared_count: the number of shared fences returned
  233. * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
  234. * the required size, and must be freed by caller)
  235. *
  236. * RETURNS
  237. * Zero or -errno
  238. */
  239. int reservation_object_get_fences_rcu(struct reservation_object *obj,
  240. struct dma_fence **pfence_excl,
  241. unsigned *pshared_count,
  242. struct dma_fence ***pshared)
  243. {
  244. struct dma_fence **shared = NULL;
  245. struct dma_fence *fence_excl;
  246. unsigned int shared_count;
  247. int ret = 1;
  248. do {
  249. struct reservation_object_list *fobj;
  250. unsigned seq;
  251. unsigned int i;
  252. shared_count = i = 0;
  253. rcu_read_lock();
  254. seq = read_seqcount_begin(&obj->seq);
  255. fence_excl = rcu_dereference(obj->fence_excl);
  256. if (fence_excl && !dma_fence_get_rcu(fence_excl))
  257. goto unlock;
  258. fobj = rcu_dereference(obj->fence);
  259. if (fobj) {
  260. struct dma_fence **nshared;
  261. size_t sz = sizeof(*shared) * fobj->shared_max;
  262. nshared = krealloc(shared, sz,
  263. GFP_NOWAIT | __GFP_NOWARN);
  264. if (!nshared) {
  265. rcu_read_unlock();
  266. nshared = krealloc(shared, sz, GFP_KERNEL);
  267. if (nshared) {
  268. shared = nshared;
  269. continue;
  270. }
  271. ret = -ENOMEM;
  272. break;
  273. }
  274. shared = nshared;
  275. shared_count = fobj->shared_count;
  276. for (i = 0; i < shared_count; ++i) {
  277. shared[i] = rcu_dereference(fobj->shared[i]);
  278. if (!dma_fence_get_rcu(shared[i]))
  279. break;
  280. }
  281. }
  282. if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
  283. while (i--)
  284. dma_fence_put(shared[i]);
  285. dma_fence_put(fence_excl);
  286. goto unlock;
  287. }
  288. ret = 0;
  289. unlock:
  290. rcu_read_unlock();
  291. } while (ret);
  292. if (!shared_count) {
  293. kfree(shared);
  294. shared = NULL;
  295. }
  296. *pshared_count = shared_count;
  297. *pshared = shared;
  298. *pfence_excl = fence_excl;
  299. return ret;
  300. }
  301. EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
  302. /**
  303. * reservation_object_wait_timeout_rcu - Wait on reservation's objects
  304. * shared and/or exclusive fences.
  305. * @obj: the reservation object
  306. * @wait_all: if true, wait on all fences, else wait on just exclusive fence
  307. * @intr: if true, do interruptible wait
  308. * @timeout: timeout value in jiffies or zero to return immediately
  309. *
  310. * RETURNS
  311. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
  312. * greater than zer on success.
  313. */
  314. long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
  315. bool wait_all, bool intr,
  316. unsigned long timeout)
  317. {
  318. struct dma_fence *fence;
  319. unsigned seq, shared_count, i = 0;
  320. long ret = timeout ? timeout : 1;
  321. retry:
  322. fence = NULL;
  323. shared_count = 0;
  324. seq = read_seqcount_begin(&obj->seq);
  325. rcu_read_lock();
  326. if (wait_all) {
  327. struct reservation_object_list *fobj =
  328. rcu_dereference(obj->fence);
  329. if (fobj)
  330. shared_count = fobj->shared_count;
  331. for (i = 0; i < shared_count; ++i) {
  332. struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
  333. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  334. &lfence->flags))
  335. continue;
  336. if (!dma_fence_get_rcu(lfence))
  337. goto unlock_retry;
  338. if (dma_fence_is_signaled(lfence)) {
  339. dma_fence_put(lfence);
  340. continue;
  341. }
  342. fence = lfence;
  343. break;
  344. }
  345. }
  346. if (!shared_count) {
  347. struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
  348. if (fence_excl &&
  349. !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  350. &fence_excl->flags)) {
  351. if (!dma_fence_get_rcu(fence_excl))
  352. goto unlock_retry;
  353. if (dma_fence_is_signaled(fence_excl))
  354. dma_fence_put(fence_excl);
  355. else
  356. fence = fence_excl;
  357. }
  358. }
  359. rcu_read_unlock();
  360. if (fence) {
  361. if (read_seqcount_retry(&obj->seq, seq)) {
  362. dma_fence_put(fence);
  363. goto retry;
  364. }
  365. ret = dma_fence_wait_timeout(fence, intr, ret);
  366. dma_fence_put(fence);
  367. if (ret > 0 && wait_all && (i + 1 < shared_count))
  368. goto retry;
  369. }
  370. return ret;
  371. unlock_retry:
  372. rcu_read_unlock();
  373. goto retry;
  374. }
  375. EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
  376. static inline int
  377. reservation_object_test_signaled_single(struct dma_fence *passed_fence)
  378. {
  379. struct dma_fence *fence, *lfence = passed_fence;
  380. int ret = 1;
  381. if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
  382. fence = dma_fence_get_rcu(lfence);
  383. if (!fence)
  384. return -1;
  385. ret = !!dma_fence_is_signaled(fence);
  386. dma_fence_put(fence);
  387. }
  388. return ret;
  389. }
  390. /**
  391. * reservation_object_test_signaled_rcu - Test if a reservation object's
  392. * fences have been signaled.
  393. * @obj: the reservation object
  394. * @test_all: if true, test all fences, otherwise only test the exclusive
  395. * fence
  396. *
  397. * RETURNS
  398. * true if all fences signaled, else false
  399. */
  400. bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
  401. bool test_all)
  402. {
  403. unsigned seq, shared_count;
  404. int ret;
  405. rcu_read_lock();
  406. retry:
  407. ret = true;
  408. shared_count = 0;
  409. seq = read_seqcount_begin(&obj->seq);
  410. if (test_all) {
  411. unsigned i;
  412. struct reservation_object_list *fobj =
  413. rcu_dereference(obj->fence);
  414. if (fobj)
  415. shared_count = fobj->shared_count;
  416. for (i = 0; i < shared_count; ++i) {
  417. struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
  418. ret = reservation_object_test_signaled_single(fence);
  419. if (ret < 0)
  420. goto retry;
  421. else if (!ret)
  422. break;
  423. }
  424. if (read_seqcount_retry(&obj->seq, seq))
  425. goto retry;
  426. }
  427. if (!shared_count) {
  428. struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
  429. if (fence_excl) {
  430. ret = reservation_object_test_signaled_single(
  431. fence_excl);
  432. if (ret < 0)
  433. goto retry;
  434. if (read_seqcount_retry(&obj->seq, seq))
  435. goto retry;
  436. }
  437. }
  438. rcu_read_unlock();
  439. return ret;
  440. }
  441. EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);