dma-resv.c 16.4 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/mm.h>
38
#include <linux/sched/mm.h>
39
#include <linux/mmu_notifier.h>
40

R
Rob Clark 已提交
41 42 43 44 45 46 47 48 49 50 51
/**
 * 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.
 */

52
DEFINE_WD_CLASS(reservation_ww_class);
53
EXPORT_SYMBOL(reservation_ww_class);
54

55
/**
56
 * dma_resv_list_alloc - allocate fence list
57 58
 * @shared_max: number of fences we need space for
 *
59
 * Allocate a new dma_resv_list and make sure to correctly initialize
60 61
 * shared_max.
 */
62
static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max)
63
{
64
	struct dma_resv_list *list;
65

C
Christian König 已提交
66
	list = kmalloc(struct_size(list, shared, shared_max), GFP_KERNEL);
67 68 69 70 71 72 73 74 75 76
	if (!list)
		return NULL;

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

	return list;
}

/**
77
 * dma_resv_list_free - free fence list
78 79
 * @list: list to free
 *
80
 * Free a dma_resv_list and make sure to drop all references.
81
 */
82
static void dma_resv_list_free(struct dma_resv_list *list)
83 84 85 86 87 88 89 90 91 92 93 94
{
	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);
}

95
#if IS_ENABLED(CONFIG_LOCKDEP)
S
Steven Price 已提交
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)
S
Steven Price 已提交
105
		return -ENOMEM;
106 107

	dma_resv_init(&obj);
108
	address_space_init_once(&mapping);
109

110
	mmap_read_lock(mm);
111 112 113 114
	ww_acquire_init(&ctx, &reservation_ww_class);
	ret = dma_resv_lock(&obj, &ctx);
	if (ret == -EDEADLK)
		dma_resv_lock_slow(&obj, &ctx);
115
	fs_reclaim_acquire(GFP_KERNEL);
116 117 118
	/* for unmap_mapping_range on trylocked buffer objects in shrinkers */
	i_mmap_lock_write(&mapping);
	i_mmap_unlock_write(&mapping);
119 120 121 122 123 124 125
#ifdef CONFIG_MMU_NOTIFIER
	lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
	__dma_fence_might_wait();
	lock_map_release(&__mmu_notifier_invalidate_range_start_map);
#else
	__dma_fence_might_wait();
#endif
126 127
	fs_reclaim_release(GFP_KERNEL);
	ww_mutex_unlock(&obj.lock);
128
	ww_acquire_fini(&ctx);
129
	mmap_read_unlock(mm);
130 131
	
	mmput(mm);
S
Steven Price 已提交
132 133

	return 0;
134 135 136 137
}
subsys_initcall(dma_resv_lockdep);
#endif

138
/**
139
 * dma_resv_init - initialize a reservation object
140 141
 * @obj: the reservation object
 */
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 148 149
	RCU_INIT_POINTER(obj->fence, NULL);
	RCU_INIT_POINTER(obj->fence_excl, NULL);
}
150
EXPORT_SYMBOL(dma_resv_init);
151 152

/**
153
 * dma_resv_fini - destroys a reservation object
154 155
 * @obj: the reservation object
 */
156
void dma_resv_fini(struct dma_resv *obj)
157
{
158
	struct dma_resv_list *fobj;
159 160 161 162 163 164 165 166 167 168 169
	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);
170
	dma_resv_list_free(fobj);
171 172
	ww_mutex_destroy(&obj->lock);
}
173
EXPORT_SYMBOL(dma_resv_fini);
174

R
Rob Clark 已提交
175
/**
176 177
 * dma_resv_reserve_shared - Reserve space to add shared fences to
 * a dma_resv.
R
Rob Clark 已提交
178
 * @obj: reservation object
179
 * @num_fences: number of fences we want to add
R
Rob Clark 已提交
180
 *
181
 * Should be called before dma_resv_add_shared_fence().  Must
R
Rob Clark 已提交
182 183 184 185
 * be called with obj->lock held.
 *
 * RETURNS
 * Zero for success, or -errno
186
 */
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 201
			max = max(old->shared_count + num_fences,
				  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 208
	if (!new)
		return -ENOMEM;
209 210 211 212 213 214 215

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

219
		fence = rcu_dereference_protected(old->shared[i],
220
						  dma_resv_held(obj));
221 222
		if (dma_fence_is_signaled(fence))
			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 230 231 232 233 234
	 * 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.
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

R
Rob Clark 已提交
255
/**
256
 * dma_resv_add_shared_fence - Add a fence to a shared slot
R
Rob Clark 已提交
257 258 259
 * @obj: the reservation object
 * @fence: the shared fence to add
 *
260
 * Add a fence to a shared slot, obj->lock must be held, and
261
 * dma_resv_reserve_shared() has been called.
262
 */
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 270
	dma_fence_get(fence);

271
	dma_resv_assert_held(obj);
272

273
	fobj = dma_resv_get_list(obj);
274
	count = fobj->shared_count;
275

276 277
	write_seqcount_begin(&obj->seq);

278
	for (i = 0; i < count; ++i) {
279

280
		old = rcu_dereference_protected(fobj->shared[i],
281
						dma_resv_held(obj));
282 283
		if (old->context == fence->context ||
		    dma_fence_is_signaled(old))
284 285 286 287
			goto replace;
	}

	BUG_ON(fobj->shared_count >= fobj->shared_max);
288
	old = NULL;
289
	count++;
290 291 292

replace:
	RCU_INIT_POINTER(fobj->shared[i], fence);
293 294
	/* pointer update must be visible before we extend the shared_count */
	smp_store_mb(fobj->shared_count, count);
295 296

	write_seqcount_end(&obj->seq);
297
	dma_fence_put(old);
298
}
299
EXPORT_SYMBOL(dma_resv_add_shared_fence);
300

R
Rob Clark 已提交
301
/**
302
 * dma_resv_add_excl_fence - Add an exclusive fence.
R
Rob Clark 已提交
303 304 305 306 307
 * @obj: the reservation object
 * @fence: the shared fence to add
 *
 * Add a fence to the exclusive slot.  The obj->lock must be held.
 */
308
void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence)
309
{
310 311
	struct dma_fence *old_fence = dma_resv_get_excl(obj);
	struct dma_resv_list *old;
312 313
	u32 i = 0;

314
	dma_resv_assert_held(obj);
315

316
	old = dma_resv_get_list(obj);
317
	if (old)
318 319 320
		i = old->shared_count;

	if (fence)
321
		dma_fence_get(fence);
322

323 324 325
	write_seqcount_begin(&obj->seq);
	/* write_seqcount_begin provides the necessary memory barrier */
	RCU_INIT_POINTER(obj->fence_excl, fence);
326
	if (old)
327 328
		old->shared_count = 0;
	write_seqcount_end(&obj->seq);
329 330 331

	/* inplace update, no shared fences */
	while (i--)
332
		dma_fence_put(rcu_dereference_protected(old->shared[i],
333
						dma_resv_held(obj)));
334

335
	dma_fence_put(old_fence);
336
}
337
EXPORT_SYMBOL(dma_resv_add_excl_fence);
338

339
/**
340
* dma_resv_copy_fences - Copy all fences from src to dst.
341 342 343
* @dst: the destination reservation object
* @src: the source reservation object
*
344
* Copy all fences from src to dst. dst-lock must be held.
345
*/
346
int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
347
{
348
	struct dma_resv_list *src_list, *dst_list;
349
	struct dma_fence *old, *new;
350
	unsigned i;
351

352
	dma_resv_assert_held(dst);
353

354
	rcu_read_lock();
355
	src_list = rcu_dereference(src->fence);
356

357
retry:
358 359 360
	if (src_list) {
		unsigned shared_count = src_list->shared_count;

361 362
		rcu_read_unlock();

363
		dst_list = dma_resv_list_alloc(shared_count);
364 365 366
		if (!dst_list)
			return -ENOMEM;

367
		rcu_read_lock();
368 369
		src_list = rcu_dereference(src->fence);
		if (!src_list || src_list->shared_count > shared_count) {
370 371 372 373 374
			kfree(dst_list);
			goto retry;
		}

		dst_list->shared_count = 0;
375
		for (i = 0; i < src_list->shared_count; ++i) {
376 377 378 379 380 381 382 383
			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)) {
384
				dma_resv_list_free(dst_list);
385
				src_list = rcu_dereference(src->fence);
386 387 388 389 390 391 392 393
				goto retry;
			}

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

394
			rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
395
		}
396 397 398 399
	} else {
		dst_list = NULL;
	}

400
	new = dma_fence_get_rcu_safe(&src->fence_excl);
401 402
	rcu_read_unlock();

403 404
	src_list = dma_resv_get_list(dst);
	old = dma_resv_get_excl(dst);
405

406 407 408 409 410
	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);
411

412
	dma_resv_list_free(src_list);
413 414 415 416
	dma_fence_put(old);

	return 0;
}
417
EXPORT_SYMBOL(dma_resv_copy_fences);
418

R
Rob Clark 已提交
419
/**
420
 * dma_resv_get_fences_rcu - Get an object's shared and exclusive
R
Rob Clark 已提交
421 422 423 424 425 426 427
 * 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)
 *
428 429 430
 * 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 已提交
431
 */
432 433 434 435
int dma_resv_get_fences_rcu(struct dma_resv *obj,
			    struct dma_fence **pfence_excl,
			    unsigned *pshared_count,
			    struct dma_fence ***pshared)
436
{
437 438
	struct dma_fence **shared = NULL;
	struct dma_fence *fence_excl;
439 440
	unsigned int shared_count;
	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 458 459 460 461 462 463
		if (fobj)
			sz += sizeof(*shared) * fobj->shared_max;

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

		if (sz) {
464
			struct dma_fence **nshared;
465 466 467 468 469

			nshared = krealloc(shared, sz,
					   GFP_NOWAIT | __GFP_NOWARN);
			if (!nshared) {
				rcu_read_unlock();
470 471 472 473

				dma_fence_put(fence_excl);
				fence_excl = NULL;

474 475 476 477 478 479 480 481 482 483
				nshared = krealloc(shared, sz, GFP_KERNEL);
				if (nshared) {
					shared = nshared;
					continue;
				}

				ret = -ENOMEM;
				break;
			}
			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 495
				dma_fence_put(shared[i]);
			dma_fence_put(fence_excl);
496 497 498 499
			goto unlock;
		}

		ret = 0;
500 501
unlock:
		rcu_read_unlock();
502 503
	} while (ret);

504 505 506
	if (pfence_excl)
		*pfence_excl = fence_excl;
	else if (fence_excl)
Q
Qiang Yu 已提交
507
		shared[shared_count++] = fence_excl;
508

509
	if (!shared_count) {
510
		kfree(shared);
511
		shared = NULL;
512
	}
513 514 515

	*pshared_count = shared_count;
	*pshared = shared;
516 517
	return ret;
}
518
EXPORT_SYMBOL_GPL(dma_resv_get_fences_rcu);
519

R
Rob Clark 已提交
520
/**
521
 * dma_resv_wait_timeout_rcu - Wait on reservation's objects
R
Rob Clark 已提交
522 523 524 525 526 527 528 529 530 531
 * 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.
 */
532 533 534
long dma_resv_wait_timeout_rcu(struct dma_resv *obj,
			       bool wait_all, bool intr,
			       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 543
	shared_count = 0;
	seq = read_seqcount_begin(&obj->seq);
544
	rcu_read_lock();
545
	i = -1;
546

547
	fence = rcu_dereference(obj->fence_excl);
548 549 550 551 552 553 554 555 556 557 558 559 560
	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;
	}

561
	if (wait_all) {
562 563 564 565 566
		struct dma_resv_list *fobj = rcu_dereference(obj->fence);

		if (fobj)
			shared_count = fobj->shared_count;

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

570 571
			if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
				     &lfence->flags))
572 573
				continue;

574
			if (!dma_fence_get_rcu(lfence))
575 576
				goto unlock_retry;

577 578
			if (dma_fence_is_signaled(lfence)) {
				dma_fence_put(lfence);
579 580 581 582 583 584 585 586 587 588
				continue;
			}

			fence = lfence;
			break;
		}
	}

	rcu_read_unlock();
	if (fence) {
589 590 591 592 593
		if (read_seqcount_retry(&obj->seq, seq)) {
			dma_fence_put(fence);
			goto retry;
		}

594 595
		ret = dma_fence_wait_timeout(fence, intr, ret);
		dma_fence_put(fence);
596 597 598 599 600 601 602 603 604
		if (ret > 0 && wait_all && (i + 1 < shared_count))
			goto retry;
	}
	return ret;

unlock_retry:
	rcu_read_unlock();
	goto retry;
}
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 612
	int ret = 1;

613 614
	if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
		fence = dma_fence_get_rcu(lfence);
615 616 617
		if (!fence)
			return -1;

618 619
		ret = !!dma_fence_is_signaled(fence);
		dma_fence_put(fence);
620 621 622 623
	}
	return ret;
}

R
Rob Clark 已提交
624
/**
625
 * dma_resv_test_signaled_rcu - Test if a reservation object's
R
Rob Clark 已提交
626 627 628 629 630 631 632 633
 * 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
 */
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 643
	shared_count = 0;
	seq = read_seqcount_begin(&obj->seq);
644 645 646 647

	if (test_all) {
		unsigned i;

648 649 650 651 652
		struct dma_resv_list *fobj = rcu_dereference(obj->fence);

		if (fobj)
			shared_count = fobj->shared_count;

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 660 661 662
			else if (!ret)
				break;
		}

663
		if (read_seqcount_retry(&obj->seq, seq))
664
			goto retry;
665 666
	}

667 668 669 670 671 672 673 674 675 676 677 678 679
	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;
		}
	}

680 681 682
	rcu_read_unlock();
	return ret;
}
683
EXPORT_SYMBOL_GPL(dma_resv_test_signaled_rcu);