reservation.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. Both src->lock as well as dst-lock must be
  231. * held.
  232. */
  233. int reservation_object_copy_fences(struct reservation_object *dst,
  234. struct reservation_object *src)
  235. {
  236. struct reservation_object_list *src_list, *dst_list;
  237. struct dma_fence *old, *new;
  238. size_t size;
  239. unsigned i;
  240. src_list = reservation_object_get_list(src);
  241. if (src_list) {
  242. size = offsetof(typeof(*src_list),
  243. shared[src_list->shared_count]);
  244. dst_list = kmalloc(size, GFP_KERNEL);
  245. if (!dst_list)
  246. return -ENOMEM;
  247. dst_list->shared_count = src_list->shared_count;
  248. dst_list->shared_max = src_list->shared_count;
  249. for (i = 0; i < src_list->shared_count; ++i)
  250. dst_list->shared[i] =
  251. dma_fence_get(src_list->shared[i]);
  252. } else {
  253. dst_list = NULL;
  254. }
  255. kfree(dst->staged);
  256. dst->staged = NULL;
  257. src_list = reservation_object_get_list(dst);
  258. old = reservation_object_get_excl(dst);
  259. new = reservation_object_get_excl(src);
  260. dma_fence_get(new);
  261. preempt_disable();
  262. write_seqcount_begin(&dst->seq);
  263. /* write_seqcount_begin provides the necessary memory barrier */
  264. RCU_INIT_POINTER(dst->fence_excl, new);
  265. RCU_INIT_POINTER(dst->fence, dst_list);
  266. write_seqcount_end(&dst->seq);
  267. preempt_enable();
  268. if (src_list)
  269. kfree_rcu(src_list, rcu);
  270. dma_fence_put(old);
  271. return 0;
  272. }
  273. EXPORT_SYMBOL(reservation_object_copy_fences);
  274. /**
  275. * reservation_object_get_fences_rcu - Get an object's shared and exclusive
  276. * fences without update side lock held
  277. * @obj: the reservation object
  278. * @pfence_excl: the returned exclusive fence (or NULL)
  279. * @pshared_count: the number of shared fences returned
  280. * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
  281. * the required size, and must be freed by caller)
  282. *
  283. * RETURNS
  284. * Zero or -errno
  285. */
  286. int reservation_object_get_fences_rcu(struct reservation_object *obj,
  287. struct dma_fence **pfence_excl,
  288. unsigned *pshared_count,
  289. struct dma_fence ***pshared)
  290. {
  291. struct dma_fence **shared = NULL;
  292. struct dma_fence *fence_excl;
  293. unsigned int shared_count;
  294. int ret = 1;
  295. do {
  296. struct reservation_object_list *fobj;
  297. unsigned seq;
  298. unsigned int i;
  299. shared_count = i = 0;
  300. rcu_read_lock();
  301. seq = read_seqcount_begin(&obj->seq);
  302. fence_excl = rcu_dereference(obj->fence_excl);
  303. if (fence_excl && !dma_fence_get_rcu(fence_excl))
  304. goto unlock;
  305. fobj = rcu_dereference(obj->fence);
  306. if (fobj) {
  307. struct dma_fence **nshared;
  308. size_t sz = sizeof(*shared) * fobj->shared_max;
  309. nshared = krealloc(shared, sz,
  310. GFP_NOWAIT | __GFP_NOWARN);
  311. if (!nshared) {
  312. rcu_read_unlock();
  313. nshared = krealloc(shared, sz, GFP_KERNEL);
  314. if (nshared) {
  315. shared = nshared;
  316. continue;
  317. }
  318. ret = -ENOMEM;
  319. break;
  320. }
  321. shared = nshared;
  322. shared_count = fobj->shared_count;
  323. for (i = 0; i < shared_count; ++i) {
  324. shared[i] = rcu_dereference(fobj->shared[i]);
  325. if (!dma_fence_get_rcu(shared[i]))
  326. break;
  327. }
  328. }
  329. if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
  330. while (i--)
  331. dma_fence_put(shared[i]);
  332. dma_fence_put(fence_excl);
  333. goto unlock;
  334. }
  335. ret = 0;
  336. unlock:
  337. rcu_read_unlock();
  338. } while (ret);
  339. if (!shared_count) {
  340. kfree(shared);
  341. shared = NULL;
  342. }
  343. *pshared_count = shared_count;
  344. *pshared = shared;
  345. *pfence_excl = fence_excl;
  346. return ret;
  347. }
  348. EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
  349. /**
  350. * reservation_object_wait_timeout_rcu - Wait on reservation's objects
  351. * shared and/or exclusive fences.
  352. * @obj: the reservation object
  353. * @wait_all: if true, wait on all fences, else wait on just exclusive fence
  354. * @intr: if true, do interruptible wait
  355. * @timeout: timeout value in jiffies or zero to return immediately
  356. *
  357. * RETURNS
  358. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
  359. * greater than zer on success.
  360. */
  361. long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
  362. bool wait_all, bool intr,
  363. unsigned long timeout)
  364. {
  365. struct dma_fence *fence;
  366. unsigned seq, shared_count, i = 0;
  367. long ret = timeout ? timeout : 1;
  368. retry:
  369. shared_count = 0;
  370. seq = read_seqcount_begin(&obj->seq);
  371. rcu_read_lock();
  372. fence = rcu_dereference(obj->fence_excl);
  373. if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
  374. if (!dma_fence_get_rcu(fence))
  375. goto unlock_retry;
  376. if (dma_fence_is_signaled(fence)) {
  377. dma_fence_put(fence);
  378. fence = NULL;
  379. }
  380. } else {
  381. fence = NULL;
  382. }
  383. if (!fence && wait_all) {
  384. struct reservation_object_list *fobj =
  385. rcu_dereference(obj->fence);
  386. if (fobj)
  387. shared_count = fobj->shared_count;
  388. for (i = 0; i < shared_count; ++i) {
  389. struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
  390. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  391. &lfence->flags))
  392. continue;
  393. if (!dma_fence_get_rcu(lfence))
  394. goto unlock_retry;
  395. if (dma_fence_is_signaled(lfence)) {
  396. dma_fence_put(lfence);
  397. continue;
  398. }
  399. fence = lfence;
  400. break;
  401. }
  402. }
  403. rcu_read_unlock();
  404. if (fence) {
  405. if (read_seqcount_retry(&obj->seq, seq)) {
  406. dma_fence_put(fence);
  407. goto retry;
  408. }
  409. ret = dma_fence_wait_timeout(fence, intr, ret);
  410. dma_fence_put(fence);
  411. if (ret > 0 && wait_all && (i + 1 < shared_count))
  412. goto retry;
  413. }
  414. return ret;
  415. unlock_retry:
  416. rcu_read_unlock();
  417. goto retry;
  418. }
  419. EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
  420. static inline int
  421. reservation_object_test_signaled_single(struct dma_fence *passed_fence)
  422. {
  423. struct dma_fence *fence, *lfence = passed_fence;
  424. int ret = 1;
  425. if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
  426. fence = dma_fence_get_rcu(lfence);
  427. if (!fence)
  428. return -1;
  429. ret = !!dma_fence_is_signaled(fence);
  430. dma_fence_put(fence);
  431. }
  432. return ret;
  433. }
  434. /**
  435. * reservation_object_test_signaled_rcu - Test if a reservation object's
  436. * fences have been signaled.
  437. * @obj: the reservation object
  438. * @test_all: if true, test all fences, otherwise only test the exclusive
  439. * fence
  440. *
  441. * RETURNS
  442. * true if all fences signaled, else false
  443. */
  444. bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
  445. bool test_all)
  446. {
  447. unsigned seq, shared_count;
  448. int ret;
  449. rcu_read_lock();
  450. retry:
  451. ret = true;
  452. shared_count = 0;
  453. seq = read_seqcount_begin(&obj->seq);
  454. if (test_all) {
  455. unsigned i;
  456. struct reservation_object_list *fobj =
  457. rcu_dereference(obj->fence);
  458. if (fobj)
  459. shared_count = fobj->shared_count;
  460. for (i = 0; i < shared_count; ++i) {
  461. struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
  462. ret = reservation_object_test_signaled_single(fence);
  463. if (ret < 0)
  464. goto retry;
  465. else if (!ret)
  466. break;
  467. }
  468. if (read_seqcount_retry(&obj->seq, seq))
  469. goto retry;
  470. }
  471. if (!shared_count) {
  472. struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
  473. if (fence_excl) {
  474. ret = reservation_object_test_signaled_single(
  475. fence_excl);
  476. if (ret < 0)
  477. goto retry;
  478. if (read_seqcount_retry(&obj->seq, seq))
  479. goto retry;
  480. }
  481. }
  482. rcu_read_unlock();
  483. return ret;
  484. }
  485. EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);