GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / dma-buf / reservation.c
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
35 #include <linux/reservation.h>
36 #include <linux/export.h>
37
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
49 DEFINE_WW_CLASS(reservation_ww_class);
50 EXPORT_SYMBOL(reservation_ww_class);
51
52 struct lock_class_key reservation_seqcount_class;
53 EXPORT_SYMBOL(reservation_seqcount_class);
54
55 const char reservation_seqcount_string[] = "reservation_seqcount";
56 EXPORT_SYMBOL(reservation_seqcount_string);
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
68  */
69 int 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 }
100 EXPORT_SYMBOL(reservation_object_reserve_shared);
101
102 static void
103 reservation_object_add_shared_inplace(struct reservation_object *obj,
104                                       struct reservation_object_list *fobj,
105                                       struct dma_fence *fence)
106 {
107         u32 i;
108
109         dma_fence_get(fence);
110
111         preempt_disable();
112         write_seqcount_begin(&obj->seq);
113
114         for (i = 0; i < fobj->shared_count; ++i) {
115                 struct dma_fence *old_fence;
116
117                 old_fence = rcu_dereference_protected(fobj->shared[i],
118                                                 reservation_object_held(obj));
119
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();
125
126                         dma_fence_put(old_fence);
127                         return;
128                 }
129         }
130
131         /*
132          * memory barrier is added by write_seqcount_begin,
133          * fobj->shared_count is protected by this lock too
134          */
135         RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
136         fobj->shared_count++;
137
138         write_seqcount_end(&obj->seq);
139         preempt_enable();
140 }
141
142 static void
143 reservation_object_add_shared_replace(struct reservation_object *obj,
144                                       struct reservation_object_list *old,
145                                       struct reservation_object_list *fobj,
146                                       struct dma_fence *fence)
147 {
148         unsigned i;
149         struct dma_fence *old_fence = NULL;
150
151         dma_fence_get(fence);
152
153         if (!old) {
154                 RCU_INIT_POINTER(fobj->shared[0], fence);
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) {
168                 struct dma_fence *check;
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);
176                 } else
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++;
182         }
183
184 done:
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
198         dma_fence_put(old_fence);
199 }
200
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  *
206  * Add a fence to a shared slot, obj->lock must be held, and
207  * reservation_object_reserve_shared() has been called.
208  */
209 void reservation_object_add_shared_fence(struct reservation_object *obj,
210                                          struct dma_fence *fence)
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) {
218                 BUG_ON(old->shared_count >= old->shared_max);
219                 reservation_object_add_shared_inplace(obj, old, fence);
220         } else
221                 reservation_object_add_shared_replace(obj, old, fobj, fence);
222 }
223 EXPORT_SYMBOL(reservation_object_add_shared_fence);
224
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  */
232 void reservation_object_add_excl_fence(struct reservation_object *obj,
233                                        struct dma_fence *fence)
234 {
235         struct dma_fence *old_fence = reservation_object_get_excl(obj);
236         struct reservation_object_list *old;
237         u32 i = 0;
238
239         old = reservation_object_get_list(obj);
240         if (old)
241                 i = old->shared_count;
242
243         if (fence)
244                 dma_fence_get(fence);
245
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();
254
255         /* inplace update, no shared fences */
256         while (i--)
257                 dma_fence_put(rcu_dereference_protected(old->shared[i],
258                                                 reservation_object_held(obj)));
259
260         dma_fence_put(old_fence);
261 }
262 EXPORT_SYMBOL(reservation_object_add_excl_fence);
263
264 /**
265 * reservation_object_copy_fences - Copy all fences from src to dst.
266 * @dst: the destination reservation object
267 * @src: the source reservation object
268 *
269 * Copy all fences from src to dst. dst-lock must be held.
270 */
271 int reservation_object_copy_fences(struct reservation_object *dst,
272                                    struct reservation_object *src)
273 {
274         struct reservation_object_list *src_list, *dst_list;
275         struct dma_fence *old, *new;
276         size_t size;
277         unsigned i;
278
279         rcu_read_lock();
280         src_list = rcu_dereference(src->fence);
281
282 retry:
283         if (src_list) {
284                 unsigned shared_count = src_list->shared_count;
285
286                 size = offsetof(typeof(*src_list), shared[shared_count]);
287                 rcu_read_unlock();
288
289                 dst_list = kmalloc(size, GFP_KERNEL);
290                 if (!dst_list)
291                         return -ENOMEM;
292
293                 rcu_read_lock();
294                 src_list = rcu_dereference(src->fence);
295                 if (!src_list || src_list->shared_count > shared_count) {
296                         kfree(dst_list);
297                         goto retry;
298                 }
299
300                 dst_list->shared_count = 0;
301                 dst_list->shared_max = shared_count;
302                 for (i = 0; i < src_list->shared_count; ++i) {
303                         struct dma_fence *fence;
304
305                         fence = rcu_dereference(src_list->shared[i]);
306                         if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
307                                      &fence->flags))
308                                 continue;
309
310                         if (!dma_fence_get_rcu(fence)) {
311                                 kfree(dst_list);
312                                 src_list = rcu_dereference(src->fence);
313                                 goto retry;
314                         }
315
316                         if (dma_fence_is_signaled(fence)) {
317                                 dma_fence_put(fence);
318                                 continue;
319                         }
320
321                         dst_list->shared[dst_list->shared_count++] = fence;
322                 }
323         } else {
324                 dst_list = NULL;
325         }
326
327         new = dma_fence_get_rcu_safe(&src->fence_excl);
328         rcu_read_unlock();
329
330         kfree(dst->staged);
331         dst->staged = NULL;
332
333         src_list = reservation_object_get_list(dst);
334         old = reservation_object_get_excl(dst);
335
336         preempt_disable();
337         write_seqcount_begin(&dst->seq);
338         /* write_seqcount_begin provides the necessary memory barrier */
339         RCU_INIT_POINTER(dst->fence_excl, new);
340         RCU_INIT_POINTER(dst->fence, dst_list);
341         write_seqcount_end(&dst->seq);
342         preempt_enable();
343
344         if (src_list)
345                 kfree_rcu(src_list, rcu);
346         dma_fence_put(old);
347
348         return 0;
349 }
350 EXPORT_SYMBOL(reservation_object_copy_fences);
351
352 /**
353  * reservation_object_get_fences_rcu - Get an object's shared and exclusive
354  * fences without update side lock held
355  * @obj: the reservation object
356  * @pfence_excl: the returned exclusive fence (or NULL)
357  * @pshared_count: the number of shared fences returned
358  * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
359  * the required size, and must be freed by caller)
360  *
361  * RETURNS
362  * Zero or -errno
363  */
364 int reservation_object_get_fences_rcu(struct reservation_object *obj,
365                                       struct dma_fence **pfence_excl,
366                                       unsigned *pshared_count,
367                                       struct dma_fence ***pshared)
368 {
369         struct dma_fence **shared = NULL;
370         struct dma_fence *fence_excl;
371         unsigned int shared_count;
372         int ret = 1;
373
374         do {
375                 struct reservation_object_list *fobj;
376                 unsigned seq;
377                 unsigned int i;
378
379                 shared_count = i = 0;
380
381                 rcu_read_lock();
382                 seq = read_seqcount_begin(&obj->seq);
383
384                 fence_excl = rcu_dereference(obj->fence_excl);
385                 if (fence_excl && !dma_fence_get_rcu(fence_excl))
386                         goto unlock;
387
388                 fobj = rcu_dereference(obj->fence);
389                 if (fobj) {
390                         struct dma_fence **nshared;
391                         size_t sz = sizeof(*shared) * fobj->shared_max;
392
393                         nshared = krealloc(shared, sz,
394                                            GFP_NOWAIT | __GFP_NOWARN);
395                         if (!nshared) {
396                                 rcu_read_unlock();
397
398                                 dma_fence_put(fence_excl);
399                                 fence_excl = NULL;
400
401                                 nshared = krealloc(shared, sz, GFP_KERNEL);
402                                 if (nshared) {
403                                         shared = nshared;
404                                         continue;
405                                 }
406
407                                 ret = -ENOMEM;
408                                 break;
409                         }
410                         shared = nshared;
411                         shared_count = fobj->shared_count;
412
413                         for (i = 0; i < shared_count; ++i) {
414                                 shared[i] = rcu_dereference(fobj->shared[i]);
415                                 if (!dma_fence_get_rcu(shared[i]))
416                                         break;
417                         }
418                 }
419
420                 if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
421                         while (i--)
422                                 dma_fence_put(shared[i]);
423                         dma_fence_put(fence_excl);
424                         goto unlock;
425                 }
426
427                 ret = 0;
428 unlock:
429                 rcu_read_unlock();
430         } while (ret);
431
432         if (!shared_count) {
433                 kfree(shared);
434                 shared = NULL;
435         }
436
437         *pshared_count = shared_count;
438         *pshared = shared;
439         *pfence_excl = fence_excl;
440
441         return ret;
442 }
443 EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
444
445 /**
446  * reservation_object_wait_timeout_rcu - Wait on reservation's objects
447  * shared and/or exclusive fences.
448  * @obj: the reservation object
449  * @wait_all: if true, wait on all fences, else wait on just exclusive fence
450  * @intr: if true, do interruptible wait
451  * @timeout: timeout value in jiffies or zero to return immediately
452  *
453  * RETURNS
454  * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
455  * greater than zer on success.
456  */
457 long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
458                                          bool wait_all, bool intr,
459                                          unsigned long timeout)
460 {
461         struct dma_fence *fence;
462         unsigned seq, shared_count;
463         long ret = timeout ? timeout : 1;
464         int i;
465
466 retry:
467         shared_count = 0;
468         seq = read_seqcount_begin(&obj->seq);
469         rcu_read_lock();
470         i = -1;
471
472         fence = rcu_dereference(obj->fence_excl);
473         if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
474                 if (!dma_fence_get_rcu(fence))
475                         goto unlock_retry;
476
477                 if (dma_fence_is_signaled(fence)) {
478                         dma_fence_put(fence);
479                         fence = NULL;
480                 }
481
482         } else {
483                 fence = NULL;
484         }
485
486         if (wait_all) {
487                 struct reservation_object_list *fobj =
488                                                 rcu_dereference(obj->fence);
489
490                 if (fobj)
491                         shared_count = fobj->shared_count;
492
493                 for (i = 0; !fence && i < shared_count; ++i) {
494                         struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
495
496                         if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
497                                      &lfence->flags))
498                                 continue;
499
500                         if (!dma_fence_get_rcu(lfence))
501                                 goto unlock_retry;
502
503                         if (dma_fence_is_signaled(lfence)) {
504                                 dma_fence_put(lfence);
505                                 continue;
506                         }
507
508                         fence = lfence;
509                         break;
510                 }
511         }
512
513         rcu_read_unlock();
514         if (fence) {
515                 if (read_seqcount_retry(&obj->seq, seq)) {
516                         dma_fence_put(fence);
517                         goto retry;
518                 }
519
520                 ret = dma_fence_wait_timeout(fence, intr, ret);
521                 dma_fence_put(fence);
522                 if (ret > 0 && wait_all && (i + 1 < shared_count))
523                         goto retry;
524         }
525         return ret;
526
527 unlock_retry:
528         rcu_read_unlock();
529         goto retry;
530 }
531 EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
532
533
534 static inline int
535 reservation_object_test_signaled_single(struct dma_fence *passed_fence)
536 {
537         struct dma_fence *fence, *lfence = passed_fence;
538         int ret = 1;
539
540         if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
541                 fence = dma_fence_get_rcu(lfence);
542                 if (!fence)
543                         return -1;
544
545                 ret = !!dma_fence_is_signaled(fence);
546                 dma_fence_put(fence);
547         }
548         return ret;
549 }
550
551 /**
552  * reservation_object_test_signaled_rcu - Test if a reservation object's
553  * fences have been signaled.
554  * @obj: the reservation object
555  * @test_all: if true, test all fences, otherwise only test the exclusive
556  * fence
557  *
558  * RETURNS
559  * true if all fences signaled, else false
560  */
561 bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
562                                           bool test_all)
563 {
564         unsigned seq, shared_count;
565         int ret;
566
567         rcu_read_lock();
568 retry:
569         ret = true;
570         shared_count = 0;
571         seq = read_seqcount_begin(&obj->seq);
572
573         if (test_all) {
574                 unsigned i;
575
576                 struct reservation_object_list *fobj =
577                                                 rcu_dereference(obj->fence);
578
579                 if (fobj)
580                         shared_count = fobj->shared_count;
581
582                 for (i = 0; i < shared_count; ++i) {
583                         struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
584
585                         ret = reservation_object_test_signaled_single(fence);
586                         if (ret < 0)
587                                 goto retry;
588                         else if (!ret)
589                                 break;
590                 }
591
592                 if (read_seqcount_retry(&obj->seq, seq))
593                         goto retry;
594         }
595
596         if (!shared_count) {
597                 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
598
599                 if (fence_excl) {
600                         ret = reservation_object_test_signaled_single(
601                                                                 fence_excl);
602                         if (ret < 0)
603                                 goto retry;
604
605                         if (read_seqcount_retry(&obj->seq, seq))
606                                 goto retry;
607                 }
608         }
609
610         rcu_read_unlock();
611         return ret;
612 }
613 EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);