reservation.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 fence *fence)
  95. {
  96. u32 i;
  97. fence_get(fence);
  98. preempt_disable();
  99. write_seqcount_begin(&obj->seq);
  100. for (i = 0; i < fobj->shared_count; ++i) {
  101. struct 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. 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 fence *fence)
  127. {
  128. unsigned i;
  129. struct fence *old_fence = NULL;
  130. 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 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. 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 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 fence *fence)
  202. {
  203. struct 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. 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. fence_put(rcu_dereference_protected(old->shared[i],
  222. reservation_object_held(obj)));
  223. if (old_fence)
  224. 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 fence **pfence_excl,
  241. unsigned *pshared_count,
  242. struct fence ***pshared)
  243. {
  244. unsigned shared_count = 0;
  245. unsigned retry = 1;
  246. struct fence **shared = NULL, *fence_excl = NULL;
  247. int ret = 0;
  248. while (retry) {
  249. struct reservation_object_list *fobj;
  250. unsigned seq;
  251. seq = read_seqcount_begin(&obj->seq);
  252. rcu_read_lock();
  253. fobj = rcu_dereference(obj->fence);
  254. if (fobj) {
  255. struct fence **nshared;
  256. size_t sz = sizeof(*shared) * fobj->shared_max;
  257. nshared = krealloc(shared, sz,
  258. GFP_NOWAIT | __GFP_NOWARN);
  259. if (!nshared) {
  260. rcu_read_unlock();
  261. nshared = krealloc(shared, sz, GFP_KERNEL);
  262. if (nshared) {
  263. shared = nshared;
  264. continue;
  265. }
  266. ret = -ENOMEM;
  267. shared_count = 0;
  268. break;
  269. }
  270. shared = nshared;
  271. memcpy(shared, fobj->shared, sz);
  272. shared_count = fobj->shared_count;
  273. } else
  274. shared_count = 0;
  275. fence_excl = rcu_dereference(obj->fence_excl);
  276. retry = read_seqcount_retry(&obj->seq, seq);
  277. if (retry)
  278. goto unlock;
  279. if (!fence_excl || fence_get_rcu(fence_excl)) {
  280. unsigned i;
  281. for (i = 0; i < shared_count; ++i) {
  282. if (fence_get_rcu(shared[i]))
  283. continue;
  284. /* uh oh, refcount failed, abort and retry */
  285. while (i--)
  286. fence_put(shared[i]);
  287. if (fence_excl) {
  288. fence_put(fence_excl);
  289. fence_excl = NULL;
  290. }
  291. retry = 1;
  292. break;
  293. }
  294. } else
  295. retry = 1;
  296. unlock:
  297. rcu_read_unlock();
  298. }
  299. *pshared_count = shared_count;
  300. if (shared_count)
  301. *pshared = shared;
  302. else {
  303. *pshared = NULL;
  304. kfree(shared);
  305. }
  306. *pfence_excl = fence_excl;
  307. return ret;
  308. }
  309. EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
  310. /**
  311. * reservation_object_wait_timeout_rcu - Wait on reservation's objects
  312. * shared and/or exclusive fences.
  313. * @obj: the reservation object
  314. * @wait_all: if true, wait on all fences, else wait on just exclusive fence
  315. * @intr: if true, do interruptible wait
  316. * @timeout: timeout value in jiffies or zero to return immediately
  317. *
  318. * RETURNS
  319. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
  320. * greater than zer on success.
  321. */
  322. long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
  323. bool wait_all, bool intr,
  324. unsigned long timeout)
  325. {
  326. struct fence *fence;
  327. unsigned seq, shared_count, i = 0;
  328. long ret = timeout;
  329. if (!timeout)
  330. return reservation_object_test_signaled_rcu(obj, wait_all);
  331. retry:
  332. fence = NULL;
  333. shared_count = 0;
  334. seq = read_seqcount_begin(&obj->seq);
  335. rcu_read_lock();
  336. if (wait_all) {
  337. struct reservation_object_list *fobj =
  338. rcu_dereference(obj->fence);
  339. if (fobj)
  340. shared_count = fobj->shared_count;
  341. if (read_seqcount_retry(&obj->seq, seq))
  342. goto unlock_retry;
  343. for (i = 0; i < shared_count; ++i) {
  344. struct fence *lfence = rcu_dereference(fobj->shared[i]);
  345. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &lfence->flags))
  346. continue;
  347. if (!fence_get_rcu(lfence))
  348. goto unlock_retry;
  349. if (fence_is_signaled(lfence)) {
  350. fence_put(lfence);
  351. continue;
  352. }
  353. fence = lfence;
  354. break;
  355. }
  356. }
  357. if (!shared_count) {
  358. struct fence *fence_excl = rcu_dereference(obj->fence_excl);
  359. if (read_seqcount_retry(&obj->seq, seq))
  360. goto unlock_retry;
  361. if (fence_excl &&
  362. !test_bit(FENCE_FLAG_SIGNALED_BIT, &fence_excl->flags)) {
  363. if (!fence_get_rcu(fence_excl))
  364. goto unlock_retry;
  365. if (fence_is_signaled(fence_excl))
  366. fence_put(fence_excl);
  367. else
  368. fence = fence_excl;
  369. }
  370. }
  371. rcu_read_unlock();
  372. if (fence) {
  373. ret = fence_wait_timeout(fence, intr, ret);
  374. fence_put(fence);
  375. if (ret > 0 && wait_all && (i + 1 < shared_count))
  376. goto retry;
  377. }
  378. return ret;
  379. unlock_retry:
  380. rcu_read_unlock();
  381. goto retry;
  382. }
  383. EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
  384. static inline int
  385. reservation_object_test_signaled_single(struct fence *passed_fence)
  386. {
  387. struct fence *fence, *lfence = passed_fence;
  388. int ret = 1;
  389. if (!test_bit(FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
  390. fence = fence_get_rcu(lfence);
  391. if (!fence)
  392. return -1;
  393. ret = !!fence_is_signaled(fence);
  394. fence_put(fence);
  395. }
  396. return ret;
  397. }
  398. /**
  399. * reservation_object_test_signaled_rcu - Test if a reservation object's
  400. * fences have been signaled.
  401. * @obj: the reservation object
  402. * @test_all: if true, test all fences, otherwise only test the exclusive
  403. * fence
  404. *
  405. * RETURNS
  406. * true if all fences signaled, else false
  407. */
  408. bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
  409. bool test_all)
  410. {
  411. unsigned seq, shared_count;
  412. int ret = true;
  413. retry:
  414. shared_count = 0;
  415. seq = read_seqcount_begin(&obj->seq);
  416. rcu_read_lock();
  417. if (test_all) {
  418. unsigned i;
  419. struct reservation_object_list *fobj =
  420. rcu_dereference(obj->fence);
  421. if (fobj)
  422. shared_count = fobj->shared_count;
  423. if (read_seqcount_retry(&obj->seq, seq))
  424. goto unlock_retry;
  425. for (i = 0; i < shared_count; ++i) {
  426. struct fence *fence = rcu_dereference(fobj->shared[i]);
  427. ret = reservation_object_test_signaled_single(fence);
  428. if (ret < 0)
  429. goto unlock_retry;
  430. else if (!ret)
  431. break;
  432. }
  433. /*
  434. * There could be a read_seqcount_retry here, but nothing cares
  435. * about whether it's the old or newer fence pointers that are
  436. * signaled. That race could still have happened after checking
  437. * read_seqcount_retry. If you care, use ww_mutex_lock.
  438. */
  439. }
  440. if (!shared_count) {
  441. struct fence *fence_excl = rcu_dereference(obj->fence_excl);
  442. if (read_seqcount_retry(&obj->seq, seq))
  443. goto unlock_retry;
  444. if (fence_excl) {
  445. ret = reservation_object_test_signaled_single(
  446. fence_excl);
  447. if (ret < 0)
  448. goto unlock_retry;
  449. }
  450. }
  451. rcu_read_unlock();
  452. return ret;
  453. unlock_retry:
  454. rcu_read_unlock();
  455. goto retry;
  456. }
  457. EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);