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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#if IS_ENABLED(CONFIG_LOCKDEP)
static void __init dma_resv_lockdep(void)
{
	struct mm_struct *mm = mm_alloc();
	struct dma_resv obj;

	if (!mm)
		return;

	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);
}
subsys_initcall(dma_resv_lockdep);
#endif

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

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

/**
138
 * dma_resv_fini - destroys a reservation object
139 140
 * @obj: the reservation object
 */
141
void dma_resv_fini(struct dma_resv *obj)
142
{
143
	struct dma_resv_list *fobj;
144 145 146 147 148 149 150 151 152 153 154
	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);
155
	dma_resv_list_free(fobj);
156 157
	ww_mutex_destroy(&obj->lock);
}
158
EXPORT_SYMBOL(dma_resv_fini);
159

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

177
	dma_resv_assert_held(obj);
178

179
	old = dma_resv_get_list(obj);
180 181

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

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

	/*
	 * 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.
	 */
201 202
	for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
		struct dma_fence *fence;
203

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

213
	/*
214 215 216 217 218 219
	 * 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.
220
	 */
221
	rcu_assign_pointer(obj->fence, new);
222

223
	if (!old)
224
		return 0;
225

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

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

	return 0;
237
}
238
EXPORT_SYMBOL(dma_resv_reserve_shared);
239

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

254 255
	dma_fence_get(fence);

256
	dma_resv_assert_held(obj);
257

258
	fobj = dma_resv_get_list(obj);
259
	count = fobj->shared_count;
260

261 262 263
	preempt_disable();
	write_seqcount_begin(&obj->seq);

264
	for (i = 0; i < count; ++i) {
265

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

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

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

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

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

301
	dma_resv_assert_held(obj);
302

303
	old = dma_resv_get_list(obj);
304
	if (old)
305 306 307
		i = old->shared_count;

	if (fence)
308
		dma_fence_get(fence);
309

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

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

324
	dma_fence_put(old_fence);
325
}
326
EXPORT_SYMBOL(dma_resv_add_excl_fence);
327

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

341
	dma_resv_assert_held(dst);
342

343
	rcu_read_lock();
344
	src_list = rcu_dereference(src->fence);
345

346
retry:
347 348 349
	if (src_list) {
		unsigned shared_count = src_list->shared_count;

350 351
		rcu_read_unlock();

352
		dst_list = dma_resv_list_alloc(shared_count);
353 354 355
		if (!dst_list)
			return -ENOMEM;

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

		dst_list->shared_count = 0;
364
		for (i = 0; i < src_list->shared_count; ++i) {
365 366 367 368 369 370 371 372
			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)) {
373
				dma_resv_list_free(dst_list);
374
				src_list = rcu_dereference(src->fence);
375 376 377 378 379 380 381 382
				goto retry;
			}

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

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

389
	new = dma_fence_get_rcu_safe(&src->fence_excl);
390 391
	rcu_read_unlock();

392 393
	src_list = dma_resv_get_list(dst);
	old = dma_resv_get_excl(dst);
394 395

	preempt_disable();
396 397 398 399 400
	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);
401 402
	preempt_enable();

403
	dma_resv_list_free(src_list);
404 405 406 407
	dma_fence_put(old);

	return 0;
}
408
EXPORT_SYMBOL(dma_resv_copy_fences);
409

R
Rob Clark 已提交
410
/**
411
 * dma_resv_get_fences_rcu - Get an object's shared and exclusive
R
Rob Clark 已提交
412 413 414 415 416 417 418
 * 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)
 *
419 420 421
 * 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 已提交
422
 */
423 424 425 426
int dma_resv_get_fences_rcu(struct dma_resv *obj,
			    struct dma_fence **pfence_excl,
			    unsigned *pshared_count,
			    struct dma_fence ***pshared)
427
{
428 429
	struct dma_fence **shared = NULL;
	struct dma_fence *fence_excl;
430 431
	unsigned int shared_count;
	int ret = 1;
432

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

438
		shared_count = i = 0;
439 440

		rcu_read_lock();
441
		seq = read_seqcount_begin(&obj->seq);
442

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

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

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

		if (sz) {
455
			struct dma_fence **nshared;
456 457 458 459 460

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

				dma_fence_put(fence_excl);
				fence_excl = NULL;

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

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

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

		ret = 0;
491 492
unlock:
		rcu_read_unlock();
493 494
	} while (ret);

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

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

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

R
Rob Clark 已提交
511
/**
512
 * dma_resv_wait_timeout_rcu - Wait on reservation's objects
R
Rob Clark 已提交
513 514 515 516 517 518 519 520 521 522
 * 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.
 */
523 524 525
long dma_resv_wait_timeout_rcu(struct dma_resv *obj,
			       bool wait_all, bool intr,
			       unsigned long timeout)
526
{
527
	struct dma_fence *fence;
528
	unsigned seq, shared_count;
529
	long ret = timeout ? timeout : 1;
530
	int i;
531

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

538
	fence = rcu_dereference(obj->fence_excl);
539 540 541 542 543 544 545 546 547 548 549 550 551
	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;
	}

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

		if (fobj)
			shared_count = fobj->shared_count;

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

561 562
			if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
				     &lfence->flags))
563 564
				continue;

565
			if (!dma_fence_get_rcu(lfence))
566 567
				goto unlock_retry;

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

			fence = lfence;
			break;
		}
	}

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

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

unlock_retry:
	rcu_read_unlock();
	goto retry;
}
596
EXPORT_SYMBOL_GPL(dma_resv_wait_timeout_rcu);
597 598


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

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

609 610
		ret = !!dma_fence_is_signaled(fence);
		dma_fence_put(fence);
611 612 613 614
	}
	return ret;
}

R
Rob Clark 已提交
615
/**
616
 * dma_resv_test_signaled_rcu - Test if a reservation object's
R
Rob Clark 已提交
617 618 619 620 621 622 623 624
 * 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
 */
625
bool dma_resv_test_signaled_rcu(struct dma_resv *obj, bool test_all)
626
{
627
	unsigned seq, shared_count;
628
	int ret;
629

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

	if (test_all) {
		unsigned i;

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

		if (fobj)
			shared_count = fobj->shared_count;

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

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

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

658 659 660 661 662 663 664 665 666 667 668 669 670
	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;
		}
	}

671 672 673
	rcu_read_unlock();
	return ret;
}
674
EXPORT_SYMBOL_GPL(dma_resv_test_signaled_rcu);