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