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