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 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  */
reservation_object_reserve_shared(struct reservation_object * obj)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
reservation_object_add_shared_inplace(struct reservation_object * obj,struct reservation_object_list * fobj,struct dma_fence * fence)103 reservation_object_add_shared_inplace(struct reservation_object *obj,
104 				      struct reservation_object_list *fobj,
105 				      struct dma_fence *fence)
106 {
107 	struct dma_fence *signaled = NULL;
108 	u32 i, signaled_idx;
109 
110 	dma_fence_get(fence);
111 
112 	preempt_disable();
113 	write_seqcount_begin(&obj->seq);
114 
115 	for (i = 0; i < fobj->shared_count; ++i) {
116 		struct dma_fence *old_fence;
117 
118 		old_fence = rcu_dereference_protected(fobj->shared[i],
119 						reservation_object_held(obj));
120 
121 		if (old_fence->context == fence->context) {
122 			/* memory barrier is added by write_seqcount_begin */
123 			RCU_INIT_POINTER(fobj->shared[i], fence);
124 			write_seqcount_end(&obj->seq);
125 			preempt_enable();
126 
127 			dma_fence_put(old_fence);
128 			return;
129 		}
130 
131 		if (!signaled && dma_fence_is_signaled(old_fence)) {
132 			signaled = old_fence;
133 			signaled_idx = i;
134 		}
135 	}
136 
137 	/*
138 	 * memory barrier is added by write_seqcount_begin,
139 	 * fobj->shared_count is protected by this lock too
140 	 */
141 	if (signaled) {
142 		RCU_INIT_POINTER(fobj->shared[signaled_idx], fence);
143 	} else {
144 		BUG_ON(fobj->shared_count >= fobj->shared_max);
145 		RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
146 		fobj->shared_count++;
147 	}
148 
149 	write_seqcount_end(&obj->seq);
150 	preempt_enable();
151 
152 	dma_fence_put(signaled);
153 }
154 
155 static void
reservation_object_add_shared_replace(struct reservation_object * obj,struct reservation_object_list * old,struct reservation_object_list * fobj,struct dma_fence * fence)156 reservation_object_add_shared_replace(struct reservation_object *obj,
157 				      struct reservation_object_list *old,
158 				      struct reservation_object_list *fobj,
159 				      struct dma_fence *fence)
160 {
161 	unsigned i, j, k;
162 
163 	dma_fence_get(fence);
164 
165 	if (!old) {
166 		RCU_INIT_POINTER(fobj->shared[0], fence);
167 		fobj->shared_count = 1;
168 		goto done;
169 	}
170 
171 	/*
172 	 * no need to bump fence refcounts, rcu_read access
173 	 * requires the use of kref_get_unless_zero, and the
174 	 * references from the old struct are carried over to
175 	 * the new.
176 	 */
177 	for (i = 0, j = 0, k = fobj->shared_max; i < old->shared_count; ++i) {
178 		struct dma_fence *check;
179 
180 		check = rcu_dereference_protected(old->shared[i],
181 						reservation_object_held(obj));
182 
183 		if (check->context == fence->context ||
184 		    dma_fence_is_signaled(check))
185 			RCU_INIT_POINTER(fobj->shared[--k], check);
186 		else
187 			RCU_INIT_POINTER(fobj->shared[j++], check);
188 	}
189 	fobj->shared_count = j;
190 	RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
191 	fobj->shared_count++;
192 
193 done:
194 	preempt_disable();
195 	write_seqcount_begin(&obj->seq);
196 	/*
197 	 * RCU_INIT_POINTER can be used here,
198 	 * seqcount provides the necessary barriers
199 	 */
200 	RCU_INIT_POINTER(obj->fence, fobj);
201 	write_seqcount_end(&obj->seq);
202 	preempt_enable();
203 
204 	if (!old)
205 		return;
206 
207 	/* Drop the references to the signaled fences */
208 	for (i = k; i < fobj->shared_max; ++i) {
209 		struct dma_fence *f;
210 
211 		f = rcu_dereference_protected(fobj->shared[i],
212 					      reservation_object_held(obj));
213 		dma_fence_put(f);
214 	}
215 	kfree_rcu(old, rcu);
216 }
217 
218 /**
219  * reservation_object_add_shared_fence - Add a fence to a shared slot
220  * @obj: the reservation object
221  * @fence: the shared fence to add
222  *
223  * Add a fence to a shared slot, obj->lock must be held, and
224  * reservation_object_reserve_shared() has been called.
225  */
reservation_object_add_shared_fence(struct reservation_object * obj,struct dma_fence * fence)226 void reservation_object_add_shared_fence(struct reservation_object *obj,
227 					 struct dma_fence *fence)
228 {
229 	struct reservation_object_list *old, *fobj = obj->staged;
230 
231 	old = reservation_object_get_list(obj);
232 	obj->staged = NULL;
233 
234 	if (!fobj)
235 		reservation_object_add_shared_inplace(obj, old, fence);
236 	else
237 		reservation_object_add_shared_replace(obj, old, fobj, fence);
238 }
239 EXPORT_SYMBOL(reservation_object_add_shared_fence);
240 
241 /**
242  * reservation_object_add_excl_fence - Add an exclusive fence.
243  * @obj: the reservation object
244  * @fence: the shared fence to add
245  *
246  * Add a fence to the exclusive slot.  The obj->lock must be held.
247  */
reservation_object_add_excl_fence(struct reservation_object * obj,struct dma_fence * fence)248 void reservation_object_add_excl_fence(struct reservation_object *obj,
249 				       struct dma_fence *fence)
250 {
251 	struct dma_fence *old_fence = reservation_object_get_excl(obj);
252 	struct reservation_object_list *old;
253 	u32 i = 0;
254 
255 	old = reservation_object_get_list(obj);
256 	if (old)
257 		i = old->shared_count;
258 
259 	if (fence)
260 		dma_fence_get(fence);
261 
262 	preempt_disable();
263 	write_seqcount_begin(&obj->seq);
264 	/* write_seqcount_begin provides the necessary memory barrier */
265 	RCU_INIT_POINTER(obj->fence_excl, fence);
266 	if (old)
267 		old->shared_count = 0;
268 	write_seqcount_end(&obj->seq);
269 	preempt_enable();
270 
271 	/* inplace update, no shared fences */
272 	while (i--)
273 		dma_fence_put(rcu_dereference_protected(old->shared[i],
274 						reservation_object_held(obj)));
275 
276 	dma_fence_put(old_fence);
277 }
278 EXPORT_SYMBOL(reservation_object_add_excl_fence);
279 
280 /**
281 * reservation_object_copy_fences - Copy all fences from src to dst.
282 * @dst: the destination reservation object
283 * @src: the source reservation object
284 *
285 * Copy all fences from src to dst. dst-lock must be held.
286 */
reservation_object_copy_fences(struct reservation_object * dst,struct reservation_object * src)287 int reservation_object_copy_fences(struct reservation_object *dst,
288 				   struct reservation_object *src)
289 {
290 	struct reservation_object_list *src_list, *dst_list;
291 	struct dma_fence *old, *new;
292 	size_t size;
293 	unsigned i;
294 
295 	rcu_read_lock();
296 	src_list = rcu_dereference(src->fence);
297 
298 retry:
299 	if (src_list) {
300 		unsigned shared_count = src_list->shared_count;
301 
302 		size = offsetof(typeof(*src_list), shared[shared_count]);
303 		rcu_read_unlock();
304 
305 		dst_list = kmalloc(size, GFP_KERNEL);
306 		if (!dst_list)
307 			return -ENOMEM;
308 
309 		rcu_read_lock();
310 		src_list = rcu_dereference(src->fence);
311 		if (!src_list || src_list->shared_count > shared_count) {
312 			kfree(dst_list);
313 			goto retry;
314 		}
315 
316 		dst_list->shared_count = 0;
317 		dst_list->shared_max = shared_count;
318 		for (i = 0; i < src_list->shared_count; ++i) {
319 			struct dma_fence *fence;
320 
321 			fence = rcu_dereference(src_list->shared[i]);
322 			if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
323 				     &fence->flags))
324 				continue;
325 
326 			if (!dma_fence_get_rcu(fence)) {
327 				kfree(dst_list);
328 				src_list = rcu_dereference(src->fence);
329 				goto retry;
330 			}
331 
332 			if (dma_fence_is_signaled(fence)) {
333 				dma_fence_put(fence);
334 				continue;
335 			}
336 
337 			rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
338 		}
339 	} else {
340 		dst_list = NULL;
341 	}
342 
343 	new = dma_fence_get_rcu_safe(&src->fence_excl);
344 	rcu_read_unlock();
345 
346 	kfree(dst->staged);
347 	dst->staged = NULL;
348 
349 	src_list = reservation_object_get_list(dst);
350 	old = reservation_object_get_excl(dst);
351 
352 	preempt_disable();
353 	write_seqcount_begin(&dst->seq);
354 	/* write_seqcount_begin provides the necessary memory barrier */
355 	RCU_INIT_POINTER(dst->fence_excl, new);
356 	RCU_INIT_POINTER(dst->fence, dst_list);
357 	write_seqcount_end(&dst->seq);
358 	preempt_enable();
359 
360 	if (src_list)
361 		kfree_rcu(src_list, rcu);
362 	dma_fence_put(old);
363 
364 	return 0;
365 }
366 EXPORT_SYMBOL(reservation_object_copy_fences);
367 
368 /**
369  * reservation_object_get_fences_rcu - Get an object's shared and exclusive
370  * fences without update side lock held
371  * @obj: the reservation object
372  * @pfence_excl: the returned exclusive fence (or NULL)
373  * @pshared_count: the number of shared fences returned
374  * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
375  * the required size, and must be freed by caller)
376  *
377  * Retrieve all fences from the reservation object. If the pointer for the
378  * exclusive fence is not specified the fence is put into the array of the
379  * shared fences as well. Returns either zero or -ENOMEM.
380  */
reservation_object_get_fences_rcu(struct reservation_object * obj,struct dma_fence ** pfence_excl,unsigned * pshared_count,struct dma_fence *** pshared)381 int reservation_object_get_fences_rcu(struct reservation_object *obj,
382 				      struct dma_fence **pfence_excl,
383 				      unsigned *pshared_count,
384 				      struct dma_fence ***pshared)
385 {
386 	struct dma_fence **shared = NULL;
387 	struct dma_fence *fence_excl;
388 	unsigned int shared_count;
389 	int ret = 1;
390 
391 	do {
392 		struct reservation_object_list *fobj;
393 		unsigned int i, seq;
394 		size_t sz = 0;
395 
396 		shared_count = i = 0;
397 
398 		rcu_read_lock();
399 		seq = read_seqcount_begin(&obj->seq);
400 
401 		fence_excl = rcu_dereference(obj->fence_excl);
402 		if (fence_excl && !dma_fence_get_rcu(fence_excl))
403 			goto unlock;
404 
405 		fobj = rcu_dereference(obj->fence);
406 		if (fobj)
407 			sz += sizeof(*shared) * fobj->shared_max;
408 
409 		if (!pfence_excl && fence_excl)
410 			sz += sizeof(*shared);
411 
412 		if (sz) {
413 			struct dma_fence **nshared;
414 
415 			nshared = krealloc(shared, sz,
416 					   GFP_NOWAIT | __GFP_NOWARN);
417 			if (!nshared) {
418 				rcu_read_unlock();
419 
420 				dma_fence_put(fence_excl);
421 				fence_excl = NULL;
422 
423 				nshared = krealloc(shared, sz, GFP_KERNEL);
424 				if (nshared) {
425 					shared = nshared;
426 					continue;
427 				}
428 
429 				ret = -ENOMEM;
430 				break;
431 			}
432 			shared = nshared;
433 			shared_count = fobj ? fobj->shared_count : 0;
434 			for (i = 0; i < shared_count; ++i) {
435 				shared[i] = rcu_dereference(fobj->shared[i]);
436 				if (!dma_fence_get_rcu(shared[i]))
437 					break;
438 			}
439 
440 			if (!pfence_excl && fence_excl) {
441 				shared[i] = fence_excl;
442 				fence_excl = NULL;
443 				++i;
444 				++shared_count;
445 			}
446 		}
447 
448 		if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
449 			while (i--)
450 				dma_fence_put(shared[i]);
451 			dma_fence_put(fence_excl);
452 			goto unlock;
453 		}
454 
455 		ret = 0;
456 unlock:
457 		rcu_read_unlock();
458 	} while (ret);
459 
460 	if (!shared_count) {
461 		kfree(shared);
462 		shared = NULL;
463 	}
464 
465 	*pshared_count = shared_count;
466 	*pshared = shared;
467 	if (pfence_excl)
468 		*pfence_excl = fence_excl;
469 
470 	return ret;
471 }
472 EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
473 
474 /**
475  * reservation_object_wait_timeout_rcu - Wait on reservation's objects
476  * shared and/or exclusive fences.
477  * @obj: the reservation object
478  * @wait_all: if true, wait on all fences, else wait on just exclusive fence
479  * @intr: if true, do interruptible wait
480  * @timeout: timeout value in jiffies or zero to return immediately
481  *
482  * RETURNS
483  * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
484  * greater than zer on success.
485  */
reservation_object_wait_timeout_rcu(struct reservation_object * obj,bool wait_all,bool intr,unsigned long timeout)486 long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
487 					 bool wait_all, bool intr,
488 					 unsigned long timeout)
489 {
490 	struct dma_fence *fence;
491 	unsigned seq, shared_count;
492 	long ret = timeout ? timeout : 1;
493 	int i;
494 
495 retry:
496 	shared_count = 0;
497 	seq = read_seqcount_begin(&obj->seq);
498 	rcu_read_lock();
499 	i = -1;
500 
501 	fence = rcu_dereference(obj->fence_excl);
502 	if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
503 		if (!dma_fence_get_rcu(fence))
504 			goto unlock_retry;
505 
506 		if (dma_fence_is_signaled(fence)) {
507 			dma_fence_put(fence);
508 			fence = NULL;
509 		}
510 
511 	} else {
512 		fence = NULL;
513 	}
514 
515 	if (wait_all) {
516 		struct reservation_object_list *fobj =
517 						rcu_dereference(obj->fence);
518 
519 		if (fobj)
520 			shared_count = fobj->shared_count;
521 
522 		for (i = 0; !fence && i < shared_count; ++i) {
523 			struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
524 
525 			if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
526 				     &lfence->flags))
527 				continue;
528 
529 			if (!dma_fence_get_rcu(lfence))
530 				goto unlock_retry;
531 
532 			if (dma_fence_is_signaled(lfence)) {
533 				dma_fence_put(lfence);
534 				continue;
535 			}
536 
537 			fence = lfence;
538 			break;
539 		}
540 	}
541 
542 	rcu_read_unlock();
543 	if (fence) {
544 		if (read_seqcount_retry(&obj->seq, seq)) {
545 			dma_fence_put(fence);
546 			goto retry;
547 		}
548 
549 		ret = dma_fence_wait_timeout(fence, intr, ret);
550 		dma_fence_put(fence);
551 		if (ret > 0 && wait_all && (i + 1 < shared_count))
552 			goto retry;
553 	}
554 	return ret;
555 
556 unlock_retry:
557 	rcu_read_unlock();
558 	goto retry;
559 }
560 EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
561 
562 
563 static inline int
reservation_object_test_signaled_single(struct dma_fence * passed_fence)564 reservation_object_test_signaled_single(struct dma_fence *passed_fence)
565 {
566 	struct dma_fence *fence, *lfence = passed_fence;
567 	int ret = 1;
568 
569 	if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
570 		fence = dma_fence_get_rcu(lfence);
571 		if (!fence)
572 			return -1;
573 
574 		ret = !!dma_fence_is_signaled(fence);
575 		dma_fence_put(fence);
576 	}
577 	return ret;
578 }
579 
580 /**
581  * reservation_object_test_signaled_rcu - Test if a reservation object's
582  * fences have been signaled.
583  * @obj: the reservation object
584  * @test_all: if true, test all fences, otherwise only test the exclusive
585  * fence
586  *
587  * RETURNS
588  * true if all fences signaled, else false
589  */
reservation_object_test_signaled_rcu(struct reservation_object * obj,bool test_all)590 bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
591 					  bool test_all)
592 {
593 	unsigned seq, shared_count;
594 	int ret;
595 
596 	rcu_read_lock();
597 retry:
598 	ret = true;
599 	shared_count = 0;
600 	seq = read_seqcount_begin(&obj->seq);
601 
602 	if (test_all) {
603 		unsigned i;
604 
605 		struct reservation_object_list *fobj =
606 						rcu_dereference(obj->fence);
607 
608 		if (fobj)
609 			shared_count = fobj->shared_count;
610 
611 		for (i = 0; i < shared_count; ++i) {
612 			struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
613 
614 			ret = reservation_object_test_signaled_single(fence);
615 			if (ret < 0)
616 				goto retry;
617 			else if (!ret)
618 				break;
619 		}
620 
621 		if (read_seqcount_retry(&obj->seq, seq))
622 			goto retry;
623 	}
624 
625 	if (!shared_count) {
626 		struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
627 
628 		if (fence_excl) {
629 			ret = reservation_object_test_signaled_single(
630 								fence_excl);
631 			if (ret < 0)
632 				goto retry;
633 
634 			if (read_seqcount_retry(&obj->seq, seq))
635 				goto retry;
636 		}
637 	}
638 
639 	rcu_read_unlock();
640 	return ret;
641 }
642 EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);
643