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