]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/dma-buf/reservation.c
dma-buf: dma_fence_put is NULL safe
[mirror_ubuntu-bionic-kernel.git] / drivers / dma-buf / reservation.c
CommitLineData
786d7257 1/*
04a5faa8 2 * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
786d7257
ML
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
35#include <linux/reservation.h>
36#include <linux/export.h>
37
dad6c394
RC
38/**
39 * DOC: Reservation Object Overview
40 *
41 * The reservation object provides a mechanism to manage shared and
42 * exclusive fences associated with a buffer. A reservation object
43 * can have attached one exclusive fence (normally associated with
44 * write operations) or N shared fences (read operations). The RCU
45 * mechanism is used to protect read access to fences from locked
46 * write-side updates.
47 */
48
786d7257
ML
49DEFINE_WW_CLASS(reservation_ww_class);
50EXPORT_SYMBOL(reservation_ww_class);
04a5faa8 51
3c3b177a
ML
52struct lock_class_key reservation_seqcount_class;
53EXPORT_SYMBOL(reservation_seqcount_class);
54
55const char reservation_seqcount_string[] = "reservation_seqcount";
56EXPORT_SYMBOL(reservation_seqcount_string);
dad6c394
RC
57
58/**
59 * reservation_object_reserve_shared - Reserve space to add a shared
60 * fence to a reservation_object.
61 * @obj: reservation object
62 *
63 * Should be called before reservation_object_add_shared_fence(). Must
64 * be called with obj->lock held.
65 *
66 * RETURNS
67 * Zero for success, or -errno
04a5faa8
ML
68 */
69int reservation_object_reserve_shared(struct reservation_object *obj)
70{
71 struct reservation_object_list *fobj, *old;
72 u32 max;
73
74 old = reservation_object_get_list(obj);
75
76 if (old && old->shared_max) {
77 if (old->shared_count < old->shared_max) {
78 /* perform an in-place update */
79 kfree(obj->staged);
80 obj->staged = NULL;
81 return 0;
82 } else
83 max = old->shared_max * 2;
84 } else
85 max = 4;
86
87 /*
88 * resize obj->staged or allocate if it doesn't exist,
89 * noop if already correct size
90 */
91 fobj = krealloc(obj->staged, offsetof(typeof(*fobj), shared[max]),
92 GFP_KERNEL);
93 if (!fobj)
94 return -ENOMEM;
95
96 obj->staged = fobj;
97 fobj->shared_max = max;
98 return 0;
99}
100EXPORT_SYMBOL(reservation_object_reserve_shared);
101
102static void
103reservation_object_add_shared_inplace(struct reservation_object *obj,
104 struct reservation_object_list *fobj,
f54d1867 105 struct dma_fence *fence)
04a5faa8
ML
106{
107 u32 i;
108
f54d1867 109 dma_fence_get(fence);
3c3b177a
ML
110
111 preempt_disable();
112 write_seqcount_begin(&obj->seq);
113
04a5faa8 114 for (i = 0; i < fobj->shared_count; ++i) {
f54d1867 115 struct dma_fence *old_fence;
04a5faa8 116
3c3b177a
ML
117 old_fence = rcu_dereference_protected(fobj->shared[i],
118 reservation_object_held(obj));
04a5faa8 119
3c3b177a
ML
120 if (old_fence->context == fence->context) {
121 /* memory barrier is added by write_seqcount_begin */
122 RCU_INIT_POINTER(fobj->shared[i], fence);
123 write_seqcount_end(&obj->seq);
124 preempt_enable();
04a5faa8 125
f54d1867 126 dma_fence_put(old_fence);
04a5faa8
ML
127 return;
128 }
129 }
130
04a5faa8 131 /*
3c3b177a
ML
132 * memory barrier is added by write_seqcount_begin,
133 * fobj->shared_count is protected by this lock too
04a5faa8 134 */
3c3b177a 135 RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
04a5faa8 136 fobj->shared_count++;
3c3b177a
ML
137
138 write_seqcount_end(&obj->seq);
139 preempt_enable();
04a5faa8
ML
140}
141
142static void
143reservation_object_add_shared_replace(struct reservation_object *obj,
144 struct reservation_object_list *old,
145 struct reservation_object_list *fobj,
f54d1867 146 struct dma_fence *fence)
04a5faa8
ML
147{
148 unsigned i;
f54d1867 149 struct dma_fence *old_fence = NULL;
04a5faa8 150
f54d1867 151 dma_fence_get(fence);
04a5faa8
ML
152
153 if (!old) {
3c3b177a 154 RCU_INIT_POINTER(fobj->shared[0], fence);
04a5faa8
ML
155 fobj->shared_count = 1;
156 goto done;
157 }
158
159 /*
160 * no need to bump fence refcounts, rcu_read access
161 * requires the use of kref_get_unless_zero, and the
162 * references from the old struct are carried over to
163 * the new.
164 */
165 fobj->shared_count = old->shared_count;
166
167 for (i = 0; i < old->shared_count; ++i) {
f54d1867 168 struct dma_fence *check;
3c3b177a
ML
169
170 check = rcu_dereference_protected(old->shared[i],
171 reservation_object_held(obj));
172
173 if (!old_fence && check->context == fence->context) {
174 old_fence = check;
175 RCU_INIT_POINTER(fobj->shared[i], fence);
04a5faa8 176 } else
3c3b177a
ML
177 RCU_INIT_POINTER(fobj->shared[i], check);
178 }
179 if (!old_fence) {
180 RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
181 fobj->shared_count++;
04a5faa8 182 }
04a5faa8
ML
183
184done:
3c3b177a
ML
185 preempt_disable();
186 write_seqcount_begin(&obj->seq);
187 /*
188 * RCU_INIT_POINTER can be used here,
189 * seqcount provides the necessary barriers
190 */
191 RCU_INIT_POINTER(obj->fence, fobj);
192 write_seqcount_end(&obj->seq);
193 preempt_enable();
194
195 if (old)
196 kfree_rcu(old, rcu);
197
f3e31b73 198 dma_fence_put(old_fence);
04a5faa8
ML
199}
200
dad6c394
RC
201/**
202 * reservation_object_add_shared_fence - Add a fence to a shared slot
203 * @obj: the reservation object
204 * @fence: the shared fence to add
205 *
04a5faa8 206 * Add a fence to a shared slot, obj->lock must be held, and
f5bef0b8 207 * reservation_object_reserve_shared() has been called.
04a5faa8
ML
208 */
209void reservation_object_add_shared_fence(struct reservation_object *obj,
f54d1867 210 struct dma_fence *fence)
04a5faa8
ML
211{
212 struct reservation_object_list *old, *fobj = obj->staged;
213
214 old = reservation_object_get_list(obj);
215 obj->staged = NULL;
216
217 if (!fobj) {
3c3b177a 218 BUG_ON(old->shared_count >= old->shared_max);
04a5faa8
ML
219 reservation_object_add_shared_inplace(obj, old, fence);
220 } else
221 reservation_object_add_shared_replace(obj, old, fobj, fence);
222}
223EXPORT_SYMBOL(reservation_object_add_shared_fence);
224
dad6c394
RC
225/**
226 * reservation_object_add_excl_fence - Add an exclusive fence.
227 * @obj: the reservation object
228 * @fence: the shared fence to add
229 *
230 * Add a fence to the exclusive slot. The obj->lock must be held.
231 */
04a5faa8 232void reservation_object_add_excl_fence(struct reservation_object *obj,
f54d1867 233 struct dma_fence *fence)
04a5faa8 234{
f54d1867 235 struct dma_fence *old_fence = reservation_object_get_excl(obj);
04a5faa8
ML
236 struct reservation_object_list *old;
237 u32 i = 0;
238
239 old = reservation_object_get_list(obj);
3c3b177a 240 if (old)
04a5faa8 241 i = old->shared_count;
04a5faa8
ML
242
243 if (fence)
f54d1867 244 dma_fence_get(fence);
04a5faa8 245
3c3b177a
ML
246 preempt_disable();
247 write_seqcount_begin(&obj->seq);
248 /* write_seqcount_begin provides the necessary memory barrier */
249 RCU_INIT_POINTER(obj->fence_excl, fence);
250 if (old)
251 old->shared_count = 0;
252 write_seqcount_end(&obj->seq);
253 preempt_enable();
04a5faa8
ML
254
255 /* inplace update, no shared fences */
256 while (i--)
f54d1867 257 dma_fence_put(rcu_dereference_protected(old->shared[i],
3c3b177a 258 reservation_object_held(obj)));
04a5faa8 259
f3e31b73 260 dma_fence_put(old_fence);
04a5faa8
ML
261}
262EXPORT_SYMBOL(reservation_object_add_excl_fence);
3c3b177a 263
dad6c394
RC
264/**
265 * reservation_object_get_fences_rcu - Get an object's shared and exclusive
266 * fences without update side lock held
267 * @obj: the reservation object
268 * @pfence_excl: the returned exclusive fence (or NULL)
269 * @pshared_count: the number of shared fences returned
270 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
271 * the required size, and must be freed by caller)
272 *
273 * RETURNS
274 * Zero or -errno
275 */
3c3b177a 276int reservation_object_get_fences_rcu(struct reservation_object *obj,
f54d1867 277 struct dma_fence **pfence_excl,
3c3b177a 278 unsigned *pshared_count,
f54d1867 279 struct dma_fence ***pshared)
3c3b177a 280{
f54d1867
CW
281 struct dma_fence **shared = NULL;
282 struct dma_fence *fence_excl;
fedf5413
CW
283 unsigned int shared_count;
284 int ret = 1;
3c3b177a 285
fedf5413 286 do {
3c3b177a
ML
287 struct reservation_object_list *fobj;
288 unsigned seq;
fedf5413 289 unsigned int i;
3c3b177a 290
fedf5413 291 shared_count = i = 0;
3c3b177a
ML
292
293 rcu_read_lock();
fedf5413
CW
294 seq = read_seqcount_begin(&obj->seq);
295
296 fence_excl = rcu_dereference(obj->fence_excl);
f54d1867 297 if (fence_excl && !dma_fence_get_rcu(fence_excl))
fedf5413 298 goto unlock;
3c3b177a
ML
299
300 fobj = rcu_dereference(obj->fence);
301 if (fobj) {
f54d1867 302 struct dma_fence **nshared;
3c3b177a
ML
303 size_t sz = sizeof(*shared) * fobj->shared_max;
304
305 nshared = krealloc(shared, sz,
306 GFP_NOWAIT | __GFP_NOWARN);
307 if (!nshared) {
308 rcu_read_unlock();
309 nshared = krealloc(shared, sz, GFP_KERNEL);
310 if (nshared) {
311 shared = nshared;
312 continue;
313 }
314
315 ret = -ENOMEM;
3c3b177a
ML
316 break;
317 }
318 shared = nshared;
3c3b177a 319 shared_count = fobj->shared_count;
3c3b177a
ML
320
321 for (i = 0; i < shared_count; ++i) {
fedf5413 322 shared[i] = rcu_dereference(fobj->shared[i]);
f54d1867 323 if (!dma_fence_get_rcu(shared[i]))
fedf5413 324 break;
3c3b177a 325 }
fedf5413 326 }
3c3b177a 327
fedf5413
CW
328 if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
329 while (i--)
f54d1867
CW
330 dma_fence_put(shared[i]);
331 dma_fence_put(fence_excl);
fedf5413
CW
332 goto unlock;
333 }
334
335 ret = 0;
3c3b177a
ML
336unlock:
337 rcu_read_unlock();
fedf5413
CW
338 } while (ret);
339
340 if (!shared_count) {
3c3b177a 341 kfree(shared);
fedf5413 342 shared = NULL;
3c3b177a 343 }
fedf5413
CW
344
345 *pshared_count = shared_count;
346 *pshared = shared;
3c3b177a
ML
347 *pfence_excl = fence_excl;
348
349 return ret;
350}
351EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
352
dad6c394
RC
353/**
354 * reservation_object_wait_timeout_rcu - Wait on reservation's objects
355 * shared and/or exclusive fences.
356 * @obj: the reservation object
357 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
358 * @intr: if true, do interruptible wait
359 * @timeout: timeout value in jiffies or zero to return immediately
360 *
361 * RETURNS
362 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
363 * greater than zer on success.
364 */
3c3b177a
ML
365long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
366 bool wait_all, bool intr,
367 unsigned long timeout)
368{
f54d1867 369 struct dma_fence *fence;
3c3b177a 370 unsigned seq, shared_count, i = 0;
06a66b5c 371 long ret = timeout ? timeout : 1;
fb8b7d2b 372
3c3b177a
ML
373retry:
374 fence = NULL;
375 shared_count = 0;
376 seq = read_seqcount_begin(&obj->seq);
377 rcu_read_lock();
378
379 if (wait_all) {
5136629d
JT
380 struct reservation_object_list *fobj =
381 rcu_dereference(obj->fence);
3c3b177a
ML
382
383 if (fobj)
384 shared_count = fobj->shared_count;
385
3c3b177a 386 for (i = 0; i < shared_count; ++i) {
f54d1867 387 struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
3c3b177a 388
f54d1867
CW
389 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
390 &lfence->flags))
3c3b177a
ML
391 continue;
392
f54d1867 393 if (!dma_fence_get_rcu(lfence))
3c3b177a
ML
394 goto unlock_retry;
395
f54d1867
CW
396 if (dma_fence_is_signaled(lfence)) {
397 dma_fence_put(lfence);
3c3b177a
ML
398 continue;
399 }
400
401 fence = lfence;
402 break;
403 }
404 }
405
406 if (!shared_count) {
f54d1867 407 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
3c3b177a 408
3c3b177a 409 if (fence_excl &&
f54d1867
CW
410 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
411 &fence_excl->flags)) {
412 if (!dma_fence_get_rcu(fence_excl))
3c3b177a
ML
413 goto unlock_retry;
414
f54d1867
CW
415 if (dma_fence_is_signaled(fence_excl))
416 dma_fence_put(fence_excl);
3c3b177a
ML
417 else
418 fence = fence_excl;
419 }
420 }
421
422 rcu_read_unlock();
423 if (fence) {
1cec20f0 424 if (read_seqcount_retry(&obj->seq, seq)) {
f54d1867 425 dma_fence_put(fence);
1cec20f0
CW
426 goto retry;
427 }
428
f54d1867
CW
429 ret = dma_fence_wait_timeout(fence, intr, ret);
430 dma_fence_put(fence);
3c3b177a
ML
431 if (ret > 0 && wait_all && (i + 1 < shared_count))
432 goto retry;
433 }
434 return ret;
435
436unlock_retry:
437 rcu_read_unlock();
438 goto retry;
439}
440EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
441
442
443static inline int
f54d1867 444reservation_object_test_signaled_single(struct dma_fence *passed_fence)
3c3b177a 445{
f54d1867 446 struct dma_fence *fence, *lfence = passed_fence;
3c3b177a
ML
447 int ret = 1;
448
f54d1867
CW
449 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
450 fence = dma_fence_get_rcu(lfence);
3c3b177a
ML
451 if (!fence)
452 return -1;
453
f54d1867
CW
454 ret = !!dma_fence_is_signaled(fence);
455 dma_fence_put(fence);
3c3b177a
ML
456 }
457 return ret;
458}
459
dad6c394
RC
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 */
3c3b177a
ML
470bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
471 bool test_all)
472{
473 unsigned seq, shared_count;
b68d8379 474 int ret;
3c3b177a 475
b68d8379 476 rcu_read_lock();
3c3b177a 477retry:
b68d8379 478 ret = true;
3c3b177a
ML
479 shared_count = 0;
480 seq = read_seqcount_begin(&obj->seq);
3c3b177a
ML
481
482 if (test_all) {
483 unsigned i;
484
5136629d
JT
485 struct reservation_object_list *fobj =
486 rcu_dereference(obj->fence);
3c3b177a
ML
487
488 if (fobj)
489 shared_count = fobj->shared_count;
490
3c3b177a 491 for (i = 0; i < shared_count; ++i) {
f54d1867 492 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
3c3b177a
ML
493
494 ret = reservation_object_test_signaled_single(fence);
495 if (ret < 0)
b68d8379 496 goto retry;
3c3b177a
ML
497 else if (!ret)
498 break;
499 }
500
b68d8379
CW
501 if (read_seqcount_retry(&obj->seq, seq))
502 goto retry;
3c3b177a
ML
503 }
504
505 if (!shared_count) {
f54d1867 506 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
3c3b177a 507
3c3b177a 508 if (fence_excl) {
5136629d
JT
509 ret = reservation_object_test_signaled_single(
510 fence_excl);
3c3b177a 511 if (ret < 0)
b68d8379
CW
512 goto retry;
513
514 if (read_seqcount_retry(&obj->seq, seq))
515 goto retry;
3c3b177a
ML
516 }
517 }
518
519 rcu_read_unlock();
520 return ret;
3c3b177a
ML
521}
522EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);