]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/dma-buf/reservation.c
dma-buf: nuke reservation_object seq number
[linux.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_WD_CLASS(reservation_ww_class);
50 EXPORT_SYMBOL(reservation_ww_class);
51
52 /**
53  * reservation_object_list_alloc - allocate fence list
54  * @shared_max: number of fences we need space for
55  *
56  * Allocate a new reservation_object_list and make sure to correctly initialize
57  * shared_max.
58  */
59 static struct reservation_object_list *
60 reservation_object_list_alloc(unsigned int shared_max)
61 {
62         struct reservation_object_list *list;
63
64         list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL);
65         if (!list)
66                 return NULL;
67
68         list->shared_max = (ksize(list) - offsetof(typeof(*list), shared)) /
69                 sizeof(*list->shared);
70
71         return list;
72 }
73
74 /**
75  * reservation_object_list_free - free fence list
76  * @list: list to free
77  *
78  * Free a reservation_object_list and make sure to drop all references.
79  */
80 static void reservation_object_list_free(struct reservation_object_list *list)
81 {
82         unsigned int i;
83
84         if (!list)
85                 return;
86
87         for (i = 0; i < list->shared_count; ++i)
88                 dma_fence_put(rcu_dereference_protected(list->shared[i], true));
89
90         kfree_rcu(list, rcu);
91 }
92
93 /**
94  * reservation_object_init - initialize a reservation object
95  * @obj: the reservation object
96  */
97 void reservation_object_init(struct reservation_object *obj)
98 {
99         ww_mutex_init(&obj->lock, &reservation_ww_class);
100         RCU_INIT_POINTER(obj->fence, NULL);
101         RCU_INIT_POINTER(obj->fence_excl, NULL);
102 }
103 EXPORT_SYMBOL(reservation_object_init);
104
105 /**
106  * reservation_object_fini - destroys a reservation object
107  * @obj: the reservation object
108  */
109 void reservation_object_fini(struct reservation_object *obj)
110 {
111         struct reservation_object_list *fobj;
112         struct dma_fence *excl;
113
114         /*
115          * This object should be dead and all references must have
116          * been released to it, so no need to be protected with rcu.
117          */
118         excl = rcu_dereference_protected(obj->fence_excl, 1);
119         if (excl)
120                 dma_fence_put(excl);
121
122         fobj = rcu_dereference_protected(obj->fence, 1);
123         reservation_object_list_free(fobj);
124         ww_mutex_destroy(&obj->lock);
125 }
126 EXPORT_SYMBOL(reservation_object_fini);
127
128 /**
129  * reservation_object_reserve_shared - Reserve space to add shared fences to
130  * a reservation_object.
131  * @obj: reservation object
132  * @num_fences: number of fences we want to add
133  *
134  * Should be called before reservation_object_add_shared_fence().  Must
135  * be called with obj->lock held.
136  *
137  * RETURNS
138  * Zero for success, or -errno
139  */
140 int reservation_object_reserve_shared(struct reservation_object *obj,
141                                       unsigned int num_fences)
142 {
143         struct reservation_object_list *old, *new;
144         unsigned int i, j, k, max;
145
146         reservation_object_assert_held(obj);
147
148         old = reservation_object_get_list(obj);
149
150         if (old && old->shared_max) {
151                 if ((old->shared_count + num_fences) <= old->shared_max)
152                         return 0;
153                 else
154                         max = max(old->shared_count + num_fences,
155                                   old->shared_max * 2);
156         } else {
157                 max = 4;
158         }
159
160         new = reservation_object_list_alloc(max);
161         if (!new)
162                 return -ENOMEM;
163
164         /*
165          * no need to bump fence refcounts, rcu_read access
166          * requires the use of kref_get_unless_zero, and the
167          * references from the old struct are carried over to
168          * the new.
169          */
170         for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
171                 struct dma_fence *fence;
172
173                 fence = rcu_dereference_protected(old->shared[i],
174                                                   reservation_object_held(obj));
175                 if (dma_fence_is_signaled(fence))
176                         RCU_INIT_POINTER(new->shared[--k], fence);
177                 else
178                         RCU_INIT_POINTER(new->shared[j++], fence);
179         }
180         new->shared_count = j;
181
182         /*
183          * We are not changing the effective set of fences here so can
184          * merely update the pointer to the new array; both existing
185          * readers and new readers will see exactly the same set of
186          * active (unsignaled) shared fences. Individual fences and the
187          * old array are protected by RCU and so will not vanish under
188          * the gaze of the rcu_read_lock() readers.
189          */
190         rcu_assign_pointer(obj->fence, new);
191
192         if (!old)
193                 return 0;
194
195         /* Drop the references to the signaled fences */
196         for (i = k; i < max; ++i) {
197                 struct dma_fence *fence;
198
199                 fence = rcu_dereference_protected(new->shared[i],
200                                                   reservation_object_held(obj));
201                 dma_fence_put(fence);
202         }
203         kfree_rcu(old, rcu);
204
205         return 0;
206 }
207 EXPORT_SYMBOL(reservation_object_reserve_shared);
208
209 /**
210  * reservation_object_add_shared_fence - Add a fence to a shared slot
211  * @obj: the reservation object
212  * @fence: the shared fence to add
213  *
214  * Add a fence to a shared slot, obj->lock must be held, and
215  * reservation_object_reserve_shared() has been called.
216  */
217 void reservation_object_add_shared_fence(struct reservation_object *obj,
218                                          struct dma_fence *fence)
219 {
220         struct reservation_object_list *fobj;
221         struct dma_fence *old;
222         unsigned int i, count;
223
224         dma_fence_get(fence);
225
226         reservation_object_assert_held(obj);
227
228         fobj = reservation_object_get_list(obj);
229         count = fobj->shared_count;
230
231         for (i = 0; i < count; ++i) {
232
233                 old = rcu_dereference_protected(fobj->shared[i],
234                                                 reservation_object_held(obj));
235                 if (old->context == fence->context ||
236                     dma_fence_is_signaled(old))
237                         goto replace;
238         }
239
240         BUG_ON(fobj->shared_count >= fobj->shared_max);
241         old = NULL;
242         count++;
243
244 replace:
245         RCU_INIT_POINTER(fobj->shared[i], fence);
246         /* pointer update must be visible before we extend the shared_count */
247         smp_store_mb(fobj->shared_count, count);
248         dma_fence_put(old);
249 }
250 EXPORT_SYMBOL(reservation_object_add_shared_fence);
251
252 /**
253  * reservation_object_add_excl_fence - Add an exclusive fence.
254  * @obj: the reservation object
255  * @fence: the shared fence to add
256  *
257  * Add a fence to the exclusive slot.  The obj->lock must be held.
258  */
259 void reservation_object_add_excl_fence(struct reservation_object *obj,
260                                        struct dma_fence *fence)
261 {
262         struct dma_fence *old_fence = reservation_object_get_excl(obj);
263         struct reservation_object_list *old;
264         u32 i = 0;
265
266         reservation_object_assert_held(obj);
267
268         old = reservation_object_get_list(obj);
269         if (old)
270                 i = old->shared_count;
271
272         if (fence)
273                 dma_fence_get(fence);
274
275         preempt_disable();
276         rcu_assign_pointer(obj->fence_excl, fence);
277         /* pointer update must be visible before we modify the shared_count */
278         if (old)
279                 smp_store_mb(old->shared_count, 0);
280         preempt_enable();
281
282         /* inplace update, no shared fences */
283         while (i--)
284                 dma_fence_put(rcu_dereference_protected(old->shared[i],
285                                                 reservation_object_held(obj)));
286
287         dma_fence_put(old_fence);
288 }
289 EXPORT_SYMBOL(reservation_object_add_excl_fence);
290
291 /**
292 * reservation_object_copy_fences - Copy all fences from src to dst.
293 * @dst: the destination reservation object
294 * @src: the source reservation object
295 *
296 * Copy all fences from src to dst. dst-lock must be held.
297 */
298 int reservation_object_copy_fences(struct reservation_object *dst,
299                                    struct reservation_object *src)
300 {
301         struct reservation_object_list *src_list, *dst_list;
302         struct dma_fence *old, *new;
303         unsigned int i, shared_count;
304
305         reservation_object_assert_held(dst);
306
307         rcu_read_lock();
308
309 retry:
310         reservation_object_fences(src, &new, &src_list, &shared_count);
311         if (shared_count) {
312                 rcu_read_unlock();
313
314                 dst_list = reservation_object_list_alloc(shared_count);
315                 if (!dst_list)
316                         return -ENOMEM;
317
318                 rcu_read_lock();
319                 reservation_object_fences(src, &new, &src_list, &shared_count);
320                 if (!src_list || shared_count > dst_list->shared_max) {
321                         kfree(dst_list);
322                         goto retry;
323                 }
324
325                 dst_list->shared_count = 0;
326                 for (i = 0; i < shared_count; ++i) {
327                         struct dma_fence *fence;
328
329                         fence = rcu_dereference(src_list->shared[i]);
330                         if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
331                                      &fence->flags))
332                                 continue;
333
334                         if (!dma_fence_get_rcu(fence)) {
335                                 reservation_object_list_free(dst_list);
336                                 goto retry;
337                         }
338
339                         if (dma_fence_is_signaled(fence)) {
340                                 dma_fence_put(fence);
341                                 continue;
342                         }
343
344                         rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
345                 }
346         } else {
347                 dst_list = NULL;
348         }
349
350         if (new && !dma_fence_get_rcu(new)) {
351                 reservation_object_list_free(dst_list);
352                 goto retry;
353         }
354         rcu_read_unlock();
355
356         src_list = reservation_object_get_list(dst);
357         old = reservation_object_get_excl(dst);
358
359         preempt_disable();
360         rcu_assign_pointer(dst->fence_excl, new);
361         rcu_assign_pointer(dst->fence, dst_list);
362         preempt_enable();
363
364         reservation_object_list_free(src_list);
365         dma_fence_put(old);
366
367         return 0;
368 }
369 EXPORT_SYMBOL(reservation_object_copy_fences);
370
371 /**
372  * reservation_object_get_fences_rcu - Get an object's shared and exclusive
373  * fences without update side lock held
374  * @obj: the reservation object
375  * @pfence_excl: the returned exclusive fence (or NULL)
376  * @pshared_count: the number of shared fences returned
377  * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
378  * the required size, and must be freed by caller)
379  *
380  * Retrieve all fences from the reservation object. If the pointer for the
381  * exclusive fence is not specified the fence is put into the array of the
382  * shared fences as well. Returns either zero or -ENOMEM.
383  */
384 int reservation_object_get_fences_rcu(struct reservation_object *obj,
385                                       struct dma_fence **pfence_excl,
386                                       unsigned *pshared_count,
387                                       struct dma_fence ***pshared)
388 {
389         struct dma_fence **shared = NULL;
390         struct dma_fence *fence_excl;
391         unsigned int shared_count;
392         int ret = 1;
393
394         do {
395                 struct reservation_object_list *fobj;
396                 unsigned int i;
397                 size_t sz = 0;
398
399                 i = 0;
400
401                 rcu_read_lock();
402                 reservation_object_fences(obj, &fence_excl, &fobj,
403                                           &shared_count);
404
405                 if (fence_excl && !dma_fence_get_rcu(fence_excl))
406                         goto unlock;
407
408                 if (fobj)
409                         sz += sizeof(*shared) * fobj->shared_max;
410
411                 if (!pfence_excl && fence_excl)
412                         sz += sizeof(*shared);
413
414                 if (sz) {
415                         struct dma_fence **nshared;
416
417                         nshared = krealloc(shared, sz,
418                                            GFP_NOWAIT | __GFP_NOWARN);
419                         if (!nshared) {
420                                 rcu_read_unlock();
421
422                                 dma_fence_put(fence_excl);
423                                 fence_excl = NULL;
424
425                                 nshared = krealloc(shared, sz, GFP_KERNEL);
426                                 if (nshared) {
427                                         shared = nshared;
428                                         continue;
429                                 }
430
431                                 ret = -ENOMEM;
432                                 break;
433                         }
434                         shared = nshared;
435                         for (i = 0; i < shared_count; ++i) {
436                                 shared[i] = rcu_dereference(fobj->shared[i]);
437                                 if (!dma_fence_get_rcu(shared[i]))
438                                         break;
439                         }
440                 }
441
442                 if (i != shared_count) {
443                         while (i--)
444                                 dma_fence_put(shared[i]);
445                         dma_fence_put(fence_excl);
446                         goto unlock;
447                 }
448
449                 ret = 0;
450 unlock:
451                 rcu_read_unlock();
452         } while (ret);
453
454         if (pfence_excl)
455                 *pfence_excl = fence_excl;
456         else if (fence_excl)
457                 shared[++shared_count] = fence_excl;
458
459         if (!shared_count) {
460                 kfree(shared);
461                 shared = NULL;
462         }
463
464         *pshared_count = shared_count;
465         *pshared = shared;
466         return ret;
467 }
468 EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
469
470 /**
471  * reservation_object_wait_timeout_rcu - Wait on reservation's objects
472  * shared and/or exclusive fences.
473  * @obj: the reservation object
474  * @wait_all: if true, wait on all fences, else wait on just exclusive fence
475  * @intr: if true, do interruptible wait
476  * @timeout: timeout value in jiffies or zero to return immediately
477  *
478  * RETURNS
479  * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
480  * greater than zer on success.
481  */
482 long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
483                                          bool wait_all, bool intr,
484                                          unsigned long timeout)
485 {
486         struct reservation_object_list *fobj;
487         struct dma_fence *fence;
488         unsigned shared_count;
489         long ret = timeout ? timeout : 1;
490         int i;
491
492 retry:
493         rcu_read_lock();
494         i = -1;
495
496         reservation_object_fences(obj, &fence, &fobj, &shared_count);
497         if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
498                 if (!dma_fence_get_rcu(fence))
499                         goto unlock_retry;
500
501                 if (dma_fence_is_signaled(fence)) {
502                         dma_fence_put(fence);
503                         fence = NULL;
504                 }
505
506         } else {
507                 fence = NULL;
508         }
509
510         if (wait_all) {
511                 for (i = 0; !fence && i < shared_count; ++i) {
512                         struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
513
514                         if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
515                                      &lfence->flags))
516                                 continue;
517
518                         if (!dma_fence_get_rcu(lfence))
519                                 goto unlock_retry;
520
521                         if (dma_fence_is_signaled(lfence)) {
522                                 dma_fence_put(lfence);
523                                 continue;
524                         }
525
526                         fence = lfence;
527                         break;
528                 }
529         }
530
531         rcu_read_unlock();
532         if (fence) {
533                 ret = dma_fence_wait_timeout(fence, intr, ret);
534                 dma_fence_put(fence);
535                 if (ret > 0 && wait_all && (i + 1 < shared_count))
536                         goto retry;
537         }
538         return ret;
539
540 unlock_retry:
541         rcu_read_unlock();
542         goto retry;
543 }
544 EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
545
546
547 static inline int
548 reservation_object_test_signaled_single(struct dma_fence *passed_fence)
549 {
550         struct dma_fence *fence, *lfence = passed_fence;
551         int ret = 1;
552
553         if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
554                 fence = dma_fence_get_rcu(lfence);
555                 if (!fence)
556                         return -1;
557
558                 ret = !!dma_fence_is_signaled(fence);
559                 dma_fence_put(fence);
560         }
561         return ret;
562 }
563
564 /**
565  * reservation_object_test_signaled_rcu - Test if a reservation object's
566  * fences have been signaled.
567  * @obj: the reservation object
568  * @test_all: if true, test all fences, otherwise only test the exclusive
569  * fence
570  *
571  * RETURNS
572  * true if all fences signaled, else false
573  */
574 bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
575                                           bool test_all)
576 {
577         struct reservation_object_list *fobj;
578         struct dma_fence *fence_excl;
579         unsigned shared_count;
580         int ret;
581
582         rcu_read_lock();
583 retry:
584         ret = true;
585
586         reservation_object_fences(obj, &fence_excl, &fobj, &shared_count);
587         if (test_all) {
588                 unsigned i;
589
590                 for (i = 0; i < shared_count; ++i) {
591                         struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
592
593                         ret = reservation_object_test_signaled_single(fence);
594                         if (ret < 0)
595                                 goto retry;
596                         else if (!ret)
597                                 break;
598                 }
599         }
600
601         if (!shared_count && fence_excl) {
602                 ret = reservation_object_test_signaled_single(fence_excl);
603                 if (ret < 0)
604                         goto retry;
605         }
606
607         rcu_read_unlock();
608         return ret;
609 }
610 EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);