dma-resv.c 16.1 KB
Newer Older
1
/*
2
 * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * Based on bo.c which bears the following copyright notice,
 * but is dual licensed:
 *
 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/
/*
 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
 */

35
#include <linux/dma-resv.h>
36
#include <linux/export.h>
37
#include <linux/sched/mm.h>
38

R
Rob Clark 已提交
39 40 41 42 43 44 45 46 47 48 49
/**
 * DOC: Reservation Object Overview
 *
 * The reservation object provides a mechanism to manage shared and
 * exclusive fences associated with a buffer.  A reservation object
 * can have attached one exclusive fence (normally associated with
 * write operations) or N shared fences (read operations).  The RCU
 * mechanism is used to protect read access to fences from locked
 * write-side updates.
 */

50
DEFINE_WD_CLASS(reservation_ww_class);
51
EXPORT_SYMBOL(reservation_ww_class);
52

53 54 55 56 57 58
struct lock_class_key reservation_seqcount_class;
EXPORT_SYMBOL(reservation_seqcount_class);

const char reservation_seqcount_string[] = "reservation_seqcount";
EXPORT_SYMBOL(reservation_seqcount_string);

59
/**
60
 * dma_resv_list_alloc - allocate fence list
61 62
 * @shared_max: number of fences we need space for
 *
63
 * Allocate a new dma_resv_list and make sure to correctly initialize
64 65
 * shared_max.
 */
66
static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max)
67
{
68
	struct dma_resv_list *list;
69 70 71 72 73 74 75 76 77 78 79 80

	list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL);
	if (!list)
		return NULL;

	list->shared_max = (ksize(list) - offsetof(typeof(*list), shared)) /
		sizeof(*list->shared);

	return list;
}

/**
81
 * dma_resv_list_free - free fence list
82 83
 * @list: list to free
 *
84
 * Free a dma_resv_list and make sure to drop all references.
85
 */
86
static void dma_resv_list_free(struct dma_resv_list *list)
87 88 89 90 91 92 93 94 95 96 97 98
{
	unsigned int i;

	if (!list)
		return;

	for (i = 0; i < list->shared_count; ++i)
		dma_fence_put(rcu_dereference_protected(list->shared[i], true));

	kfree_rcu(list, rcu);
}

99
#if IS_ENABLED(CONFIG_LOCKDEP)
S
Steven Price 已提交
100
static int __init dma_resv_lockdep(void)
101 102 103 104 105
{
	struct mm_struct *mm = mm_alloc();
	struct dma_resv obj;

	if (!mm)
S
Steven Price 已提交
106
		return -ENOMEM;
107 108 109 110 111 112 113 114 115 116 117

	dma_resv_init(&obj);

	down_read(&mm->mmap_sem);
	ww_mutex_lock(&obj.lock, NULL);
	fs_reclaim_acquire(GFP_KERNEL);
	fs_reclaim_release(GFP_KERNEL);
	ww_mutex_unlock(&obj.lock);
	up_read(&mm->mmap_sem);
	
	mmput(mm);
S
Steven Price 已提交
118 119

	return 0;
120 121 122 123
}
subsys_initcall(dma_resv_lockdep);
#endif

124
/**
125
 * dma_resv_init - initialize a reservation object
126 127
 * @obj: the reservation object
 */
128
void dma_resv_init(struct dma_resv *obj)
129 130
{
	ww_mutex_init(&obj->lock, &reservation_ww_class);
131 132 133

	__seqcount_init(&obj->seq, reservation_seqcount_string,
			&reservation_seqcount_class);
134 135 136
	RCU_INIT_POINTER(obj->fence, NULL);
	RCU_INIT_POINTER(obj->fence_excl, NULL);
}
137
EXPORT_SYMBOL(dma_resv_init);
138 139

/**
140
 * dma_resv_fini - destroys a reservation object
141 142
 * @obj: the reservation object
 */
143
void dma_resv_fini(struct dma_resv *obj)
144
{
145
	struct dma_resv_list *fobj;
146 147 148 149 150 151 152 153 154 155 156
	struct dma_fence *excl;

	/*
	 * This object should be dead and all references must have
	 * been released to it, so no need to be protected with rcu.
	 */
	excl = rcu_dereference_protected(obj->fence_excl, 1);
	if (excl)
		dma_fence_put(excl);

	fobj = rcu_dereference_protected(obj->fence, 1);
157
	dma_resv_list_free(fobj);
158 159
	ww_mutex_destroy(&obj->lock);
}
160
EXPORT_SYMBOL(dma_resv_fini);
161

R
Rob Clark 已提交
162
/**
163 164
 * dma_resv_reserve_shared - Reserve space to add shared fences to
 * a dma_resv.
R
Rob Clark 已提交
165
 * @obj: reservation object
166
 * @num_fences: number of fences we want to add
R
Rob Clark 已提交
167
 *
168
 * Should be called before dma_resv_add_shared_fence().  Must
R
Rob Clark 已提交
169 170 171 172
 * be called with obj->lock held.
 *
 * RETURNS
 * Zero for success, or -errno
173
 */
174
int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences)
175
{
176
	struct dma_resv_list *old, *new;
177
	unsigned int i, j, k, max;
178

179
	dma_resv_assert_held(obj);
180

181
	old = dma_resv_get_list(obj);
182 183

	if (old && old->shared_max) {
184
		if ((old->shared_count + num_fences) <= old->shared_max)
185
			return 0;
186
		else
187 188
			max = max(old->shared_count + num_fences,
				  old->shared_max * 2);
189
	} else {
190
		max = 4;
191
	}
192

193
	new = dma_resv_list_alloc(max);
194 195
	if (!new)
		return -ENOMEM;
196 197 198 199 200 201 202

	/*
	 * no need to bump fence refcounts, rcu_read access
	 * requires the use of kref_get_unless_zero, and the
	 * references from the old struct are carried over to
	 * the new.
	 */
203 204
	for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
		struct dma_fence *fence;
205

206
		fence = rcu_dereference_protected(old->shared[i],
207
						  dma_resv_held(obj));
208 209
		if (dma_fence_is_signaled(fence))
			RCU_INIT_POINTER(new->shared[--k], fence);
210
		else
211
			RCU_INIT_POINTER(new->shared[j++], fence);
212
	}
213
	new->shared_count = j;
214

215
	/*
216 217 218 219 220 221
	 * We are not changing the effective set of fences here so can
	 * merely update the pointer to the new array; both existing
	 * readers and new readers will see exactly the same set of
	 * active (unsignaled) shared fences. Individual fences and the
	 * old array are protected by RCU and so will not vanish under
	 * the gaze of the rcu_read_lock() readers.
222
	 */
223
	rcu_assign_pointer(obj->fence, new);
224

225
	if (!old)
226
		return 0;
227

228
	/* Drop the references to the signaled fences */
229
	for (i = k; i < max; ++i) {
230
		struct dma_fence *fence;
231

232
		fence = rcu_dereference_protected(new->shared[i],
233
						  dma_resv_held(obj));
234
		dma_fence_put(fence);
235 236
	}
	kfree_rcu(old, rcu);
237 238

	return 0;
239
}
240
EXPORT_SYMBOL(dma_resv_reserve_shared);
241

R
Rob Clark 已提交
242
/**
243
 * dma_resv_add_shared_fence - Add a fence to a shared slot
R
Rob Clark 已提交
244 245 246
 * @obj: the reservation object
 * @fence: the shared fence to add
 *
247
 * Add a fence to a shared slot, obj->lock must be held, and
248
 * dma_resv_reserve_shared() has been called.
249
 */
250
void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence)
251
{
252
	struct dma_resv_list *fobj;
253
	struct dma_fence *old;
254
	unsigned int i, count;
255

256 257
	dma_fence_get(fence);

258
	dma_resv_assert_held(obj);
259

260
	fobj = dma_resv_get_list(obj);
261
	count = fobj->shared_count;
262

263 264 265
	preempt_disable();
	write_seqcount_begin(&obj->seq);

266
	for (i = 0; i < count; ++i) {
267

268
		old = rcu_dereference_protected(fobj->shared[i],
269
						dma_resv_held(obj));
270 271
		if (old->context == fence->context ||
		    dma_fence_is_signaled(old))
272 273 274 275
			goto replace;
	}

	BUG_ON(fobj->shared_count >= fobj->shared_max);
276
	old = NULL;
277
	count++;
278 279 280

replace:
	RCU_INIT_POINTER(fobj->shared[i], fence);
281 282
	/* pointer update must be visible before we extend the shared_count */
	smp_store_mb(fobj->shared_count, count);
283 284 285

	write_seqcount_end(&obj->seq);
	preempt_enable();
286
	dma_fence_put(old);
287
}
288
EXPORT_SYMBOL(dma_resv_add_shared_fence);
289

R
Rob Clark 已提交
290
/**
291
 * dma_resv_add_excl_fence - Add an exclusive fence.
R
Rob Clark 已提交
292 293 294 295 296
 * @obj: the reservation object
 * @fence: the shared fence to add
 *
 * Add a fence to the exclusive slot.  The obj->lock must be held.
 */
297
void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence)
298
{
299 300
	struct dma_fence *old_fence = dma_resv_get_excl(obj);
	struct dma_resv_list *old;
301 302
	u32 i = 0;

303
	dma_resv_assert_held(obj);
304

305
	old = dma_resv_get_list(obj);
306
	if (old)
307 308 309
		i = old->shared_count;

	if (fence)
310
		dma_fence_get(fence);
311

312
	preempt_disable();
313 314 315
	write_seqcount_begin(&obj->seq);
	/* write_seqcount_begin provides the necessary memory barrier */
	RCU_INIT_POINTER(obj->fence_excl, fence);
316
	if (old)
317 318
		old->shared_count = 0;
	write_seqcount_end(&obj->seq);
319
	preempt_enable();
320 321 322

	/* inplace update, no shared fences */
	while (i--)
323
		dma_fence_put(rcu_dereference_protected(old->shared[i],
324
						dma_resv_held(obj)));
325

326
	dma_fence_put(old_fence);
327
}
328
EXPORT_SYMBOL(dma_resv_add_excl_fence);
329

330
/**
331
* dma_resv_copy_fences - Copy all fences from src to dst.
332 333 334
* @dst: the destination reservation object
* @src: the source reservation object
*
335
* Copy all fences from src to dst. dst-lock must be held.
336
*/
337
int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
338
{
339
	struct dma_resv_list *src_list, *dst_list;
340
	struct dma_fence *old, *new;
341
	unsigned i;
342

343
	dma_resv_assert_held(dst);
344

345
	rcu_read_lock();
346
	src_list = rcu_dereference(src->fence);
347

348
retry:
349 350 351
	if (src_list) {
		unsigned shared_count = src_list->shared_count;

352 353
		rcu_read_unlock();

354
		dst_list = dma_resv_list_alloc(shared_count);
355 356 357
		if (!dst_list)
			return -ENOMEM;

358
		rcu_read_lock();
359 360
		src_list = rcu_dereference(src->fence);
		if (!src_list || src_list->shared_count > shared_count) {
361 362 363 364 365
			kfree(dst_list);
			goto retry;
		}

		dst_list->shared_count = 0;
366
		for (i = 0; i < src_list->shared_count; ++i) {
367 368 369 370 371 372 373 374
			struct dma_fence *fence;

			fence = rcu_dereference(src_list->shared[i]);
			if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
				     &fence->flags))
				continue;

			if (!dma_fence_get_rcu(fence)) {
375
				dma_resv_list_free(dst_list);
376
				src_list = rcu_dereference(src->fence);
377 378 379 380 381 382 383 384
				goto retry;
			}

			if (dma_fence_is_signaled(fence)) {
				dma_fence_put(fence);
				continue;
			}

385
			rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
386
		}
387 388 389 390
	} else {
		dst_list = NULL;
	}

391
	new = dma_fence_get_rcu_safe(&src->fence_excl);
392 393
	rcu_read_unlock();

394 395
	src_list = dma_resv_get_list(dst);
	old = dma_resv_get_excl(dst);
396 397

	preempt_disable();
398 399 400 401 402
	write_seqcount_begin(&dst->seq);
	/* write_seqcount_begin provides the necessary memory barrier */
	RCU_INIT_POINTER(dst->fence_excl, new);
	RCU_INIT_POINTER(dst->fence, dst_list);
	write_seqcount_end(&dst->seq);
403 404
	preempt_enable();

405
	dma_resv_list_free(src_list);
406 407 408 409
	dma_fence_put(old);

	return 0;
}
410
EXPORT_SYMBOL(dma_resv_copy_fences);
411

R
Rob Clark 已提交
412
/**
413
 * dma_resv_get_fences_rcu - Get an object's shared and exclusive
R
Rob Clark 已提交
414 415 416 417 418 419 420
 * fences without update side lock held
 * @obj: the reservation object
 * @pfence_excl: the returned exclusive fence (or NULL)
 * @pshared_count: the number of shared fences returned
 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
 * the required size, and must be freed by caller)
 *
421 422 423
 * Retrieve all fences from the reservation object. If the pointer for the
 * exclusive fence is not specified the fence is put into the array of the
 * shared fences as well. Returns either zero or -ENOMEM.
R
Rob Clark 已提交
424
 */
425 426 427 428
int dma_resv_get_fences_rcu(struct dma_resv *obj,
			    struct dma_fence **pfence_excl,
			    unsigned *pshared_count,
			    struct dma_fence ***pshared)
429
{
430 431
	struct dma_fence **shared = NULL;
	struct dma_fence *fence_excl;
432 433
	unsigned int shared_count;
	int ret = 1;
434

435
	do {
436
		struct dma_resv_list *fobj;
437
		unsigned int i, seq;
438
		size_t sz = 0;
439

440
		shared_count = i = 0;
441 442

		rcu_read_lock();
443
		seq = read_seqcount_begin(&obj->seq);
444

445
		fence_excl = rcu_dereference(obj->fence_excl);
446
		if (fence_excl && !dma_fence_get_rcu(fence_excl))
447
			goto unlock;
448

449
		fobj = rcu_dereference(obj->fence);
450 451 452 453 454 455 456
		if (fobj)
			sz += sizeof(*shared) * fobj->shared_max;

		if (!pfence_excl && fence_excl)
			sz += sizeof(*shared);

		if (sz) {
457
			struct dma_fence **nshared;
458 459 460 461 462

			nshared = krealloc(shared, sz,
					   GFP_NOWAIT | __GFP_NOWARN);
			if (!nshared) {
				rcu_read_unlock();
463 464 465 466

				dma_fence_put(fence_excl);
				fence_excl = NULL;

467 468 469 470 471 472 473 474 475 476
				nshared = krealloc(shared, sz, GFP_KERNEL);
				if (nshared) {
					shared = nshared;
					continue;
				}

				ret = -ENOMEM;
				break;
			}
			shared = nshared;
477
			shared_count = fobj ? fobj->shared_count : 0;
478
			for (i = 0; i < shared_count; ++i) {
479
				shared[i] = rcu_dereference(fobj->shared[i]);
480
				if (!dma_fence_get_rcu(shared[i]))
481
					break;
482
			}
483
		}
484

485
		if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
486
			while (i--)
487 488
				dma_fence_put(shared[i]);
			dma_fence_put(fence_excl);
489 490 491 492
			goto unlock;
		}

		ret = 0;
493 494
unlock:
		rcu_read_unlock();
495 496
	} while (ret);

497 498 499
	if (pfence_excl)
		*pfence_excl = fence_excl;
	else if (fence_excl)
Q
Qiang Yu 已提交
500
		shared[shared_count++] = fence_excl;
501

502
	if (!shared_count) {
503
		kfree(shared);
504
		shared = NULL;
505
	}
506 507 508

	*pshared_count = shared_count;
	*pshared = shared;
509 510
	return ret;
}
511
EXPORT_SYMBOL_GPL(dma_resv_get_fences_rcu);
512

R
Rob Clark 已提交
513
/**
514
 * dma_resv_wait_timeout_rcu - Wait on reservation's objects
R
Rob Clark 已提交
515 516 517 518 519 520 521 522 523 524
 * shared and/or exclusive fences.
 * @obj: the reservation object
 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
 * @intr: if true, do interruptible wait
 * @timeout: timeout value in jiffies or zero to return immediately
 *
 * RETURNS
 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
 * greater than zer on success.
 */
525 526 527
long dma_resv_wait_timeout_rcu(struct dma_resv *obj,
			       bool wait_all, bool intr,
			       unsigned long timeout)
528
{
529
	struct dma_fence *fence;
530
	unsigned seq, shared_count;
531
	long ret = timeout ? timeout : 1;
532
	int i;
533

534
retry:
535 536
	shared_count = 0;
	seq = read_seqcount_begin(&obj->seq);
537
	rcu_read_lock();
538
	i = -1;
539

540
	fence = rcu_dereference(obj->fence_excl);
541 542 543 544 545 546 547 548 549 550 551 552 553
	if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
		if (!dma_fence_get_rcu(fence))
			goto unlock_retry;

		if (dma_fence_is_signaled(fence)) {
			dma_fence_put(fence);
			fence = NULL;
		}

	} else {
		fence = NULL;
	}

554
	if (wait_all) {
555 556 557 558 559
		struct dma_resv_list *fobj = rcu_dereference(obj->fence);

		if (fobj)
			shared_count = fobj->shared_count;

560
		for (i = 0; !fence && i < shared_count; ++i) {
561
			struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
562

563 564
			if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
				     &lfence->flags))
565 566
				continue;

567
			if (!dma_fence_get_rcu(lfence))
568 569
				goto unlock_retry;

570 571
			if (dma_fence_is_signaled(lfence)) {
				dma_fence_put(lfence);
572 573 574 575 576 577 578 579 580 581
				continue;
			}

			fence = lfence;
			break;
		}
	}

	rcu_read_unlock();
	if (fence) {
582 583 584 585 586
		if (read_seqcount_retry(&obj->seq, seq)) {
			dma_fence_put(fence);
			goto retry;
		}

587 588
		ret = dma_fence_wait_timeout(fence, intr, ret);
		dma_fence_put(fence);
589 590 591 592 593 594 595 596 597
		if (ret > 0 && wait_all && (i + 1 < shared_count))
			goto retry;
	}
	return ret;

unlock_retry:
	rcu_read_unlock();
	goto retry;
}
598
EXPORT_SYMBOL_GPL(dma_resv_wait_timeout_rcu);
599 600


601
static inline int dma_resv_test_signaled_single(struct dma_fence *passed_fence)
602
{
603
	struct dma_fence *fence, *lfence = passed_fence;
604 605
	int ret = 1;

606 607
	if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
		fence = dma_fence_get_rcu(lfence);
608 609 610
		if (!fence)
			return -1;

611 612
		ret = !!dma_fence_is_signaled(fence);
		dma_fence_put(fence);
613 614 615 616
	}
	return ret;
}

R
Rob Clark 已提交
617
/**
618
 * dma_resv_test_signaled_rcu - Test if a reservation object's
R
Rob Clark 已提交
619 620 621 622 623 624 625 626
 * fences have been signaled.
 * @obj: the reservation object
 * @test_all: if true, test all fences, otherwise only test the exclusive
 * fence
 *
 * RETURNS
 * true if all fences signaled, else false
 */
627
bool dma_resv_test_signaled_rcu(struct dma_resv *obj, bool test_all)
628
{
629
	unsigned seq, shared_count;
630
	int ret;
631

632
	rcu_read_lock();
633
retry:
634
	ret = true;
635 636
	shared_count = 0;
	seq = read_seqcount_begin(&obj->seq);
637 638 639 640

	if (test_all) {
		unsigned i;

641 642 643 644 645
		struct dma_resv_list *fobj = rcu_dereference(obj->fence);

		if (fobj)
			shared_count = fobj->shared_count;

646
		for (i = 0; i < shared_count; ++i) {
647
			struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
648

649
			ret = dma_resv_test_signaled_single(fence);
650
			if (ret < 0)
651
				goto retry;
652 653 654 655
			else if (!ret)
				break;
		}

656
		if (read_seqcount_retry(&obj->seq, seq))
657
			goto retry;
658 659
	}

660 661 662 663 664 665 666 667 668 669 670 671 672
	if (!shared_count) {
		struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);

		if (fence_excl) {
			ret = dma_resv_test_signaled_single(fence_excl);
			if (ret < 0)
				goto retry;

			if (read_seqcount_retry(&obj->seq, seq))
				goto retry;
		}
	}

673 674 675
	rcu_read_unlock();
	return ret;
}
676
EXPORT_SYMBOL_GPL(dma_resv_test_signaled_rcu);