reservation.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. DEFINE_WW_CLASS(reservation_ww_class);
  37. EXPORT_SYMBOL(reservation_ww_class);
  38. struct lock_class_key reservation_seqcount_class;
  39. EXPORT_SYMBOL(reservation_seqcount_class);
  40. const char reservation_seqcount_string[] = "reservation_seqcount";
  41. EXPORT_SYMBOL(reservation_seqcount_string);
  42. /*
  43. * Reserve space to add a shared fence to a reservation_object,
  44. * must be called with obj->lock held.
  45. */
  46. int reservation_object_reserve_shared(struct reservation_object *obj)
  47. {
  48. struct reservation_object_list *fobj, *old;
  49. u32 max;
  50. old = reservation_object_get_list(obj);
  51. if (old && old->shared_max) {
  52. if (old->shared_count < old->shared_max) {
  53. /* perform an in-place update */
  54. kfree(obj->staged);
  55. obj->staged = NULL;
  56. return 0;
  57. } else
  58. max = old->shared_max * 2;
  59. } else
  60. max = 4;
  61. /*
  62. * resize obj->staged or allocate if it doesn't exist,
  63. * noop if already correct size
  64. */
  65. fobj = krealloc(obj->staged, offsetof(typeof(*fobj), shared[max]),
  66. GFP_KERNEL);
  67. if (!fobj)
  68. return -ENOMEM;
  69. obj->staged = fobj;
  70. fobj->shared_max = max;
  71. return 0;
  72. }
  73. EXPORT_SYMBOL(reservation_object_reserve_shared);
  74. static void
  75. reservation_object_add_shared_inplace(struct reservation_object *obj,
  76. struct reservation_object_list *fobj,
  77. struct fence *fence)
  78. {
  79. u32 i;
  80. fence_get(fence);
  81. preempt_disable();
  82. write_seqcount_begin(&obj->seq);
  83. for (i = 0; i < fobj->shared_count; ++i) {
  84. struct fence *old_fence;
  85. old_fence = rcu_dereference_protected(fobj->shared[i],
  86. reservation_object_held(obj));
  87. if (old_fence->context == fence->context) {
  88. /* memory barrier is added by write_seqcount_begin */
  89. RCU_INIT_POINTER(fobj->shared[i], fence);
  90. write_seqcount_end(&obj->seq);
  91. preempt_enable();
  92. fence_put(old_fence);
  93. return;
  94. }
  95. }
  96. /*
  97. * memory barrier is added by write_seqcount_begin,
  98. * fobj->shared_count is protected by this lock too
  99. */
  100. RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
  101. fobj->shared_count++;
  102. write_seqcount_end(&obj->seq);
  103. preempt_enable();
  104. }
  105. static void
  106. reservation_object_add_shared_replace(struct reservation_object *obj,
  107. struct reservation_object_list *old,
  108. struct reservation_object_list *fobj,
  109. struct fence *fence)
  110. {
  111. unsigned i;
  112. struct fence *old_fence = NULL;
  113. fence_get(fence);
  114. if (!old) {
  115. RCU_INIT_POINTER(fobj->shared[0], fence);
  116. fobj->shared_count = 1;
  117. goto done;
  118. }
  119. /*
  120. * no need to bump fence refcounts, rcu_read access
  121. * requires the use of kref_get_unless_zero, and the
  122. * references from the old struct are carried over to
  123. * the new.
  124. */
  125. fobj->shared_count = old->shared_count;
  126. for (i = 0; i < old->shared_count; ++i) {
  127. struct fence *check;
  128. check = rcu_dereference_protected(old->shared[i],
  129. reservation_object_held(obj));
  130. if (!old_fence && check->context == fence->context) {
  131. old_fence = check;
  132. RCU_INIT_POINTER(fobj->shared[i], fence);
  133. } else
  134. RCU_INIT_POINTER(fobj->shared[i], check);
  135. }
  136. if (!old_fence) {
  137. RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
  138. fobj->shared_count++;
  139. }
  140. done:
  141. preempt_disable();
  142. write_seqcount_begin(&obj->seq);
  143. /*
  144. * RCU_INIT_POINTER can be used here,
  145. * seqcount provides the necessary barriers
  146. */
  147. RCU_INIT_POINTER(obj->fence, fobj);
  148. write_seqcount_end(&obj->seq);
  149. preempt_enable();
  150. if (old)
  151. kfree_rcu(old, rcu);
  152. if (old_fence)
  153. fence_put(old_fence);
  154. }
  155. /*
  156. * Add a fence to a shared slot, obj->lock must be held, and
  157. * reservation_object_reserve_shared_fence has been called.
  158. */
  159. void reservation_object_add_shared_fence(struct reservation_object *obj,
  160. struct fence *fence)
  161. {
  162. struct reservation_object_list *old, *fobj = obj->staged;
  163. old = reservation_object_get_list(obj);
  164. obj->staged = NULL;
  165. if (!fobj) {
  166. BUG_ON(old->shared_count >= old->shared_max);
  167. reservation_object_add_shared_inplace(obj, old, fence);
  168. } else
  169. reservation_object_add_shared_replace(obj, old, fobj, fence);
  170. }
  171. EXPORT_SYMBOL(reservation_object_add_shared_fence);
  172. void reservation_object_add_excl_fence(struct reservation_object *obj,
  173. struct fence *fence)
  174. {
  175. struct fence *old_fence = reservation_object_get_excl(obj);
  176. struct reservation_object_list *old;
  177. u32 i = 0;
  178. old = reservation_object_get_list(obj);
  179. if (old)
  180. i = old->shared_count;
  181. if (fence)
  182. fence_get(fence);
  183. preempt_disable();
  184. write_seqcount_begin(&obj->seq);
  185. /* write_seqcount_begin provides the necessary memory barrier */
  186. RCU_INIT_POINTER(obj->fence_excl, fence);
  187. if (old)
  188. old->shared_count = 0;
  189. write_seqcount_end(&obj->seq);
  190. preempt_enable();
  191. /* inplace update, no shared fences */
  192. while (i--)
  193. fence_put(rcu_dereference_protected(old->shared[i],
  194. reservation_object_held(obj)));
  195. if (old_fence)
  196. fence_put(old_fence);
  197. }
  198. EXPORT_SYMBOL(reservation_object_add_excl_fence);
  199. int reservation_object_get_fences_rcu(struct reservation_object *obj,
  200. struct fence **pfence_excl,
  201. unsigned *pshared_count,
  202. struct fence ***pshared)
  203. {
  204. unsigned shared_count = 0;
  205. unsigned retry = 1;
  206. struct fence **shared = NULL, *fence_excl = NULL;
  207. int ret = 0;
  208. while (retry) {
  209. struct reservation_object_list *fobj;
  210. unsigned seq;
  211. seq = read_seqcount_begin(&obj->seq);
  212. rcu_read_lock();
  213. fobj = rcu_dereference(obj->fence);
  214. if (fobj) {
  215. struct fence **nshared;
  216. size_t sz = sizeof(*shared) * fobj->shared_max;
  217. nshared = krealloc(shared, sz,
  218. GFP_NOWAIT | __GFP_NOWARN);
  219. if (!nshared) {
  220. rcu_read_unlock();
  221. nshared = krealloc(shared, sz, GFP_KERNEL);
  222. if (nshared) {
  223. shared = nshared;
  224. continue;
  225. }
  226. ret = -ENOMEM;
  227. shared_count = 0;
  228. break;
  229. }
  230. shared = nshared;
  231. memcpy(shared, fobj->shared, sz);
  232. shared_count = fobj->shared_count;
  233. } else
  234. shared_count = 0;
  235. fence_excl = rcu_dereference(obj->fence_excl);
  236. retry = read_seqcount_retry(&obj->seq, seq);
  237. if (retry)
  238. goto unlock;
  239. if (!fence_excl || fence_get_rcu(fence_excl)) {
  240. unsigned i;
  241. for (i = 0; i < shared_count; ++i) {
  242. if (fence_get_rcu(shared[i]))
  243. continue;
  244. /* uh oh, refcount failed, abort and retry */
  245. while (i--)
  246. fence_put(shared[i]);
  247. if (fence_excl) {
  248. fence_put(fence_excl);
  249. fence_excl = NULL;
  250. }
  251. retry = 1;
  252. break;
  253. }
  254. } else
  255. retry = 1;
  256. unlock:
  257. rcu_read_unlock();
  258. }
  259. *pshared_count = shared_count;
  260. if (shared_count)
  261. *pshared = shared;
  262. else {
  263. *pshared = NULL;
  264. kfree(shared);
  265. }
  266. *pfence_excl = fence_excl;
  267. return ret;
  268. }
  269. EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
  270. long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
  271. bool wait_all, bool intr,
  272. unsigned long timeout)
  273. {
  274. struct fence *fence;
  275. unsigned seq, shared_count, i = 0;
  276. long ret = timeout;
  277. if (!timeout)
  278. return reservation_object_test_signaled_rcu(obj, wait_all);
  279. retry:
  280. fence = NULL;
  281. shared_count = 0;
  282. seq = read_seqcount_begin(&obj->seq);
  283. rcu_read_lock();
  284. if (wait_all) {
  285. struct reservation_object_list *fobj =
  286. rcu_dereference(obj->fence);
  287. if (fobj)
  288. shared_count = fobj->shared_count;
  289. if (read_seqcount_retry(&obj->seq, seq))
  290. goto unlock_retry;
  291. for (i = 0; i < shared_count; ++i) {
  292. struct fence *lfence = rcu_dereference(fobj->shared[i]);
  293. if (test_bit(FENCE_FLAG_SIGNALED_BIT, &lfence->flags))
  294. continue;
  295. if (!fence_get_rcu(lfence))
  296. goto unlock_retry;
  297. if (fence_is_signaled(lfence)) {
  298. fence_put(lfence);
  299. continue;
  300. }
  301. fence = lfence;
  302. break;
  303. }
  304. }
  305. if (!shared_count) {
  306. struct fence *fence_excl = rcu_dereference(obj->fence_excl);
  307. if (read_seqcount_retry(&obj->seq, seq))
  308. goto unlock_retry;
  309. if (fence_excl &&
  310. !test_bit(FENCE_FLAG_SIGNALED_BIT, &fence_excl->flags)) {
  311. if (!fence_get_rcu(fence_excl))
  312. goto unlock_retry;
  313. if (fence_is_signaled(fence_excl))
  314. fence_put(fence_excl);
  315. else
  316. fence = fence_excl;
  317. }
  318. }
  319. rcu_read_unlock();
  320. if (fence) {
  321. ret = fence_wait_timeout(fence, intr, ret);
  322. fence_put(fence);
  323. if (ret > 0 && wait_all && (i + 1 < shared_count))
  324. goto retry;
  325. }
  326. return ret;
  327. unlock_retry:
  328. rcu_read_unlock();
  329. goto retry;
  330. }
  331. EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
  332. static inline int
  333. reservation_object_test_signaled_single(struct fence *passed_fence)
  334. {
  335. struct fence *fence, *lfence = passed_fence;
  336. int ret = 1;
  337. if (!test_bit(FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
  338. fence = fence_get_rcu(lfence);
  339. if (!fence)
  340. return -1;
  341. ret = !!fence_is_signaled(fence);
  342. fence_put(fence);
  343. }
  344. return ret;
  345. }
  346. bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
  347. bool test_all)
  348. {
  349. unsigned seq, shared_count;
  350. int ret = true;
  351. retry:
  352. shared_count = 0;
  353. seq = read_seqcount_begin(&obj->seq);
  354. rcu_read_lock();
  355. if (test_all) {
  356. unsigned i;
  357. struct reservation_object_list *fobj =
  358. rcu_dereference(obj->fence);
  359. if (fobj)
  360. shared_count = fobj->shared_count;
  361. if (read_seqcount_retry(&obj->seq, seq))
  362. goto unlock_retry;
  363. for (i = 0; i < shared_count; ++i) {
  364. struct fence *fence = rcu_dereference(fobj->shared[i]);
  365. ret = reservation_object_test_signaled_single(fence);
  366. if (ret < 0)
  367. goto unlock_retry;
  368. else if (!ret)
  369. break;
  370. }
  371. /*
  372. * There could be a read_seqcount_retry here, but nothing cares
  373. * about whether it's the old or newer fence pointers that are
  374. * signaled. That race could still have happened after checking
  375. * read_seqcount_retry. If you care, use ww_mutex_lock.
  376. */
  377. }
  378. if (!shared_count) {
  379. struct fence *fence_excl = rcu_dereference(obj->fence_excl);
  380. if (read_seqcount_retry(&obj->seq, seq))
  381. goto unlock_retry;
  382. if (fence_excl) {
  383. ret = reservation_object_test_signaled_single(
  384. fence_excl);
  385. if (ret < 0)
  386. goto unlock_retry;
  387. }
  388. }
  389. rcu_read_unlock();
  390. return ret;
  391. unlock_retry:
  392. rcu_read_unlock();
  393. goto retry;
  394. }
  395. EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);