dma-fence.c 18.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Fence mechanism for dma-buf and to allow for asynchronous dma access
 *
 * Copyright (C) 2012 Canonical Ltd
 * Copyright (C) 2012 Texas Instruments
 *
 * Authors:
 * Rob Clark <robdclark@gmail.com>
 * Maarten Lankhorst <maarten.lankhorst@canonical.com>
 */

#include <linux/slab.h>
#include <linux/export.h>
#include <linux/atomic.h>
16
#include <linux/dma-fence.h>
17
#include <linux/sched/signal.h>
18 19

#define CREATE_TRACE_POINTS
20
#include <trace/events/dma_fence.h>
21

22
EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit);
23
EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal);
24
EXPORT_TRACEPOINT_SYMBOL(dma_fence_signaled);
25

26 27 28
static DEFINE_SPINLOCK(dma_fence_stub_lock);
static struct dma_fence dma_fence_stub;

29
/*
30 31 32 33 34
 * fence context counter: each execution context should have its own
 * fence context, this allows checking if fences belong to the same
 * context or not. One device can have multiple separate contexts,
 * and they're used if some engine can run independently of another.
 */
35
static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(1);
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/**
 * DOC: DMA fences overview
 *
 * DMA fences, represented by &struct dma_fence, are the kernel internal
 * synchronization primitive for DMA operations like GPU rendering, video
 * encoding/decoding, or displaying buffers on a screen.
 *
 * A fence is initialized using dma_fence_init() and completed using
 * dma_fence_signal(). Fences are associated with a context, allocated through
 * dma_fence_context_alloc(), and all fences on the same context are
 * fully ordered.
 *
 * Since the purposes of fences is to facilitate cross-device and
 * cross-application synchronization, there's multiple ways to use one:
 *
 * - Individual fences can be exposed as a &sync_file, accessed as a file
 *   descriptor from userspace, created by calling sync_file_create(). This is
 *   called explicit fencing, since userspace passes around explicit
 *   synchronization points.
 *
 * - Some subsystems also have their own explicit fencing primitives, like
 *   &drm_syncobj. Compared to &sync_file, a &drm_syncobj allows the underlying
 *   fence to be updated.
 *
 * - Then there's also implicit fencing, where the synchronization points are
 *   implicitly passed around as part of shared &dma_buf instances. Such
 *   implicit fences are stored in &struct reservation_object through the
 *   &dma_buf.resv pointer.
 */

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
static const char *dma_fence_stub_get_name(struct dma_fence *fence)
{
        return "stub";
}

static const struct dma_fence_ops dma_fence_stub_ops = {
	.get_driver_name = dma_fence_stub_get_name,
	.get_timeline_name = dma_fence_stub_get_name,
};

/**
 * dma_fence_get_stub - return a signaled fence
 *
 * Return a stub fence which is already signaled.
 */
struct dma_fence *dma_fence_get_stub(void)
{
	spin_lock(&dma_fence_stub_lock);
	if (!dma_fence_stub.ops) {
		dma_fence_init(&dma_fence_stub,
			       &dma_fence_stub_ops,
			       &dma_fence_stub_lock,
			       0, 0);
		dma_fence_signal_locked(&dma_fence_stub);
	}
	spin_unlock(&dma_fence_stub_lock);

	return dma_fence_get(&dma_fence_stub);
}
EXPORT_SYMBOL(dma_fence_get_stub);

98
/**
99
 * dma_fence_context_alloc - allocate an array of fence contexts
100
 * @num: amount of contexts to allocate
101
 *
102 103 104
 * This function will return the first index of the number of fence contexts
 * allocated.  The fence context is used for setting &dma_fence.context to a
 * unique number by passing the context to dma_fence_init().
105
 */
106
u64 dma_fence_context_alloc(unsigned num)
107
{
108
	WARN_ON(!num);
109
	return atomic64_add_return(num, &dma_fence_context_counter) - num;
110
}
111
EXPORT_SYMBOL(dma_fence_context_alloc);
112 113

/**
114
 * dma_fence_signal_locked - signal completion of a fence
115 116 117
 * @fence: the fence to signal
 *
 * Signal completion for software callbacks on a fence, this will unblock
118 119
 * dma_fence_wait() calls and run all the callbacks added with
 * dma_fence_add_callback(). Can be called multiple times, but since a fence
120 121 122 123 124
 * can only go from the unsignaled to the signaled state and not back, it will
 * only be effective the first time.
 *
 * Unlike dma_fence_signal(), this function must be called with &dma_fence.lock
 * held.
125
 *
126 127
 * Returns 0 on success and a negative error value when @fence has been
 * signalled already.
128
 */
129
int dma_fence_signal_locked(struct dma_fence *fence)
130
{
131
	struct dma_fence_cb *cur, *tmp;
132 133
	int ret = 0;

134 135
	lockdep_assert_held(fence->lock);

136 137 138
	if (WARN_ON(!fence))
		return -EINVAL;

139
	if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
140 141 142
		ret = -EINVAL;

		/*
143
		 * we might have raced with the unlocked dma_fence_signal,
144 145
		 * still run through all callbacks
		 */
146 147 148
	} else {
		fence->timestamp = ktime_get();
		set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
149
		trace_dma_fence_signaled(fence);
150
	}
151 152 153 154 155 156 157

	list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
		list_del_init(&cur->node);
		cur->func(fence, cur);
	}
	return ret;
}
158
EXPORT_SYMBOL(dma_fence_signal_locked);
159 160

/**
161
 * dma_fence_signal - signal completion of a fence
162 163 164
 * @fence: the fence to signal
 *
 * Signal completion for software callbacks on a fence, this will unblock
165 166
 * dma_fence_wait() calls and run all the callbacks added with
 * dma_fence_add_callback(). Can be called multiple times, but since a fence
167 168 169 170 171
 * can only go from the unsignaled to the signaled state and not back, it will
 * only be effective the first time.
 *
 * Returns 0 on success and a negative error value when @fence has been
 * signalled already.
172
 */
173
int dma_fence_signal(struct dma_fence *fence)
174 175 176 177 178 179
{
	unsigned long flags;

	if (!fence)
		return -EINVAL;

180
	if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
181 182
		return -EINVAL;

183 184
	fence->timestamp = ktime_get();
	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
185
	trace_dma_fence_signaled(fence);
186

187 188
	if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
		struct dma_fence_cb *cur, *tmp;
189 190 191 192 193 194 195 196 197 198

		spin_lock_irqsave(fence->lock, flags);
		list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
			list_del_init(&cur->node);
			cur->func(fence, cur);
		}
		spin_unlock_irqrestore(fence->lock, flags);
	}
	return 0;
}
199
EXPORT_SYMBOL(dma_fence_signal);
200 201

/**
202
 * dma_fence_wait_timeout - sleep until the fence gets signaled
203
 * or until timeout elapses
204 205 206
 * @fence: the fence to wait on
 * @intr: if true, do an interruptible wait
 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
207 208 209 210 211 212 213 214 215
 *
 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
 * remaining timeout in jiffies on success. Other error values may be
 * returned on custom implementations.
 *
 * Performs a synchronous wait on this fence. It is assumed the caller
 * directly or indirectly (buf-mgr between reservation and committing)
 * holds a reference to the fence, otherwise the fence might be
 * freed before return, resulting in undefined behavior.
216 217
 *
 * See also dma_fence_wait() and dma_fence_wait_any_timeout().
218 219
 */
signed long
220
dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
221 222 223 224 225 226
{
	signed long ret;

	if (WARN_ON(timeout < 0))
		return -EINVAL;

227
	trace_dma_fence_wait_start(fence);
228 229 230 231
	if (fence->ops->wait)
		ret = fence->ops->wait(fence, intr, timeout);
	else
		ret = dma_fence_default_wait(fence, intr, timeout);
232
	trace_dma_fence_wait_end(fence);
233 234
	return ret;
}
235
EXPORT_SYMBOL(dma_fence_wait_timeout);
236

237 238 239 240 241 242 243
/**
 * dma_fence_release - default relese function for fences
 * @kref: &dma_fence.recfount
 *
 * This is the default release functions for &dma_fence. Drivers shouldn't call
 * this directly, but instead call dma_fence_put().
 */
244
void dma_fence_release(struct kref *kref)
245
{
246 247
	struct dma_fence *fence =
		container_of(kref, struct dma_fence, refcount);
248

249
	trace_dma_fence_destroy(fence);
250

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	if (WARN(!list_empty(&fence->cb_list),
		 "Fence %s:%s:%llx:%llx released with pending signals!\n",
		 fence->ops->get_driver_name(fence),
		 fence->ops->get_timeline_name(fence),
		 fence->context, fence->seqno)) {
		unsigned long flags;

		/*
		 * Failed to signal before release, likely a refcounting issue.
		 *
		 * This should never happen, but if it does make sure that we
		 * don't leave chains dangling. We set the error flag first
		 * so that the callbacks know this signal is due to an error.
		 */
		spin_lock_irqsave(fence->lock, flags);
		fence->error = -EDEADLK;
		dma_fence_signal_locked(fence);
		spin_unlock_irqrestore(fence->lock, flags);
	}
270 271 272 273

	if (fence->ops->release)
		fence->ops->release(fence);
	else
274
		dma_fence_free(fence);
275
}
276
EXPORT_SYMBOL(dma_fence_release);
277

278 279 280 281 282 283 284
/**
 * dma_fence_free - default release function for &dma_fence.
 * @fence: fence to release
 *
 * This is the default implementation for &dma_fence_ops.release. It calls
 * kfree_rcu() on @fence.
 */
285
void dma_fence_free(struct dma_fence *fence)
286
{
287
	kfree_rcu(fence, rcu);
288
}
289
EXPORT_SYMBOL(dma_fence_free);
290 291

/**
292
 * dma_fence_enable_sw_signaling - enable signaling on fence
293
 * @fence: the fence to enable
294
 *
295 296 297
 * This will request for sw signaling to be enabled, to make the fence
 * complete as soon as possible. This calls &dma_fence_ops.enable_signaling
 * internally.
298
 */
299
void dma_fence_enable_sw_signaling(struct dma_fence *fence)
300 301 302
{
	unsigned long flags;

303 304
	if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
			      &fence->flags) &&
305 306
	    !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) &&
	    fence->ops->enable_signaling) {
307
		trace_dma_fence_enable_signal(fence);
308 309 310 311

		spin_lock_irqsave(fence->lock, flags);

		if (!fence->ops->enable_signaling(fence))
312
			dma_fence_signal_locked(fence);
313 314 315 316

		spin_unlock_irqrestore(fence->lock, flags);
	}
}
317
EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
318 319

/**
320
 * dma_fence_add_callback - add a callback to be called when the fence
321
 * is signaled
322 323 324
 * @fence: the fence to wait on
 * @cb: the callback to register
 * @func: the function to call
325
 *
326
 * @cb will be initialized by dma_fence_add_callback(), no initialization
327 328 329 330 331
 * by the caller is required. Any number of callbacks can be registered
 * to a fence, but a callback can only be registered to one fence at a time.
 *
 * Note that the callback can be called from an atomic context.  If
 * fence is already signaled, this function will return -ENOENT (and
332
 * *not* call the callback).
333 334
 *
 * Add a software callback to the fence. Same restrictions apply to
335 336 337 338 339
 * refcount as it does to dma_fence_wait(), however the caller doesn't need to
 * keep a refcount to fence afterward dma_fence_add_callback() has returned:
 * when software access is enabled, the creator of the fence is required to keep
 * the fence alive until after it signals with dma_fence_signal(). The callback
 * itself can be called from irq context.
340
 *
341 342
 * Returns 0 in case of success, -ENOENT if the fence is already signaled
 * and -EINVAL in case of error.
343
 */
344 345
int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
			   dma_fence_func_t func)
346 347 348 349 350 351 352 353
{
	unsigned long flags;
	int ret = 0;
	bool was_set;

	if (WARN_ON(!fence || !func))
		return -EINVAL;

354
	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
355 356 357 358 359 360
		INIT_LIST_HEAD(&cb->node);
		return -ENOENT;
	}

	spin_lock_irqsave(fence->lock, flags);

361 362
	was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
				   &fence->flags);
363

364
	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
365
		ret = -ENOENT;
366
	else if (!was_set && fence->ops->enable_signaling) {
367
		trace_dma_fence_enable_signal(fence);
368 369

		if (!fence->ops->enable_signaling(fence)) {
370
			dma_fence_signal_locked(fence);
371 372 373 374 375 376 377 378 379 380 381 382 383
			ret = -ENOENT;
		}
	}

	if (!ret) {
		cb->func = func;
		list_add_tail(&cb->node, &fence->cb_list);
	} else
		INIT_LIST_HEAD(&cb->node);
	spin_unlock_irqrestore(fence->lock, flags);

	return ret;
}
384
EXPORT_SYMBOL(dma_fence_add_callback);
385

386 387
/**
 * dma_fence_get_status - returns the status upon completion
388
 * @fence: the dma_fence to query
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
 *
 * This wraps dma_fence_get_status_locked() to return the error status
 * condition on a signaled fence. See dma_fence_get_status_locked() for more
 * details.
 *
 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
 * been signaled without an error condition, or a negative error code
 * if the fence has been completed in err.
 */
int dma_fence_get_status(struct dma_fence *fence)
{
	unsigned long flags;
	int status;

	spin_lock_irqsave(fence->lock, flags);
	status = dma_fence_get_status_locked(fence);
	spin_unlock_irqrestore(fence->lock, flags);

	return status;
}
EXPORT_SYMBOL(dma_fence_get_status);

411
/**
412
 * dma_fence_remove_callback - remove a callback from the signaling list
413 414
 * @fence: the fence to wait on
 * @cb: the callback to remove
415 416
 *
 * Remove a previously queued callback from the fence. This function returns
417
 * true if the callback is successfully removed, or false if the fence has
418 419 420 421 422 423 424
 * already been signaled.
 *
 * *WARNING*:
 * Cancelling a callback should only be done if you really know what you're
 * doing, since deadlocks and race conditions could occur all too easily. For
 * this reason, it should only ever be done on hardware lockup recovery,
 * with a reference held to the fence.
425 426 427
 *
 * Behaviour is undefined if @cb has not been added to @fence using
 * dma_fence_add_callback() beforehand.
428 429
 */
bool
430
dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
431 432 433 434 435 436 437 438 439 440 441 442 443 444
{
	unsigned long flags;
	bool ret;

	spin_lock_irqsave(fence->lock, flags);

	ret = !list_empty(&cb->node);
	if (ret)
		list_del_init(&cb->node);

	spin_unlock_irqrestore(fence->lock, flags);

	return ret;
}
445
EXPORT_SYMBOL(dma_fence_remove_callback);
446 447

struct default_wait_cb {
448
	struct dma_fence_cb base;
449 450 451 452
	struct task_struct *task;
};

static void
453
dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
454 455 456 457 458 459 460 461
{
	struct default_wait_cb *wait =
		container_of(cb, struct default_wait_cb, base);

	wake_up_state(wait->task, TASK_NORMAL);
}

/**
462
 * dma_fence_default_wait - default sleep until the fence gets signaled
463
 * or until timeout elapses
464 465 466
 * @fence: the fence to wait on
 * @intr: if true, do an interruptible wait
 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
467 468
 *
 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
469 470 471
 * remaining timeout in jiffies on success. If timeout is zero the value one is
 * returned if the fence is already signaled for consistency with other
 * functions taking a jiffies timeout.
472 473
 */
signed long
474
dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
475 476 477
{
	struct default_wait_cb cb;
	unsigned long flags;
478
	signed long ret = timeout ? timeout : 1;
479 480
	bool was_set;

481
	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
482
		return ret;
483 484 485 486 487 488 489 490

	spin_lock_irqsave(fence->lock, flags);

	if (intr && signal_pending(current)) {
		ret = -ERESTARTSYS;
		goto out;
	}

491 492
	was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
				   &fence->flags);
493

494
	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
495 496
		goto out;

497
	if (!was_set && fence->ops->enable_signaling) {
498
		trace_dma_fence_enable_signal(fence);
499 500

		if (!fence->ops->enable_signaling(fence)) {
501
			dma_fence_signal_locked(fence);
502 503 504 505
			goto out;
		}
	}

506 507 508 509 510
	if (!timeout) {
		ret = 0;
		goto out;
	}

511
	cb.base.func = dma_fence_default_wait_cb;
512 513 514
	cb.task = current;
	list_add(&cb.base.node, &fence->cb_list);

515
	while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
		if (intr)
			__set_current_state(TASK_INTERRUPTIBLE);
		else
			__set_current_state(TASK_UNINTERRUPTIBLE);
		spin_unlock_irqrestore(fence->lock, flags);

		ret = schedule_timeout(ret);

		spin_lock_irqsave(fence->lock, flags);
		if (ret > 0 && intr && signal_pending(current))
			ret = -ERESTARTSYS;
	}

	if (!list_empty(&cb.base.node))
		list_del(&cb.base.node);
	__set_current_state(TASK_RUNNING);

out:
	spin_unlock_irqrestore(fence->lock, flags);
	return ret;
}
537
EXPORT_SYMBOL(dma_fence_default_wait);
538

539
static bool
540 541
dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
			    uint32_t *idx)
542 543 544 545
{
	int i;

	for (i = 0; i < count; ++i) {
546
		struct dma_fence *fence = fences[i];
547 548 549
		if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
			if (idx)
				*idx = i;
550
			return true;
551
		}
552 553 554 555 556
	}
	return false;
}

/**
557
 * dma_fence_wait_any_timeout - sleep until any fence gets signaled
558
 * or until timeout elapses
559 560 561 562 563 564
 * @fences: array of fences to wait on
 * @count: number of fences to wait on
 * @intr: if true, do an interruptible wait
 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
 * @idx: used to store the first signaled fence index, meaningful only on
 *	positive return
565 566 567 568 569 570 571 572
 *
 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
 * on success.
 *
 * Synchronous waits for the first fence in the array to be signaled. The
 * caller needs to hold a reference to all fences in the array, otherwise a
 * fence might be freed before return, resulting in undefined behavior.
573 574
 *
 * See also dma_fence_wait() and dma_fence_wait_timeout().
575 576
 */
signed long
577
dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
578
			   bool intr, signed long timeout, uint32_t *idx)
579 580 581 582 583 584 585 586 587 588
{
	struct default_wait_cb *cb;
	signed long ret = timeout;
	unsigned i;

	if (WARN_ON(!fences || !count || timeout < 0))
		return -EINVAL;

	if (timeout == 0) {
		for (i = 0; i < count; ++i)
589 590 591
			if (dma_fence_is_signaled(fences[i])) {
				if (idx)
					*idx = i;
592
				return 1;
593
			}
594 595 596 597 598 599 600 601 602 603 604

		return 0;
	}

	cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
	if (cb == NULL) {
		ret = -ENOMEM;
		goto err_free_cb;
	}

	for (i = 0; i < count; ++i) {
605
		struct dma_fence *fence = fences[i];
606 607

		cb[i].task = current;
608 609
		if (dma_fence_add_callback(fence, &cb[i].base,
					   dma_fence_default_wait_cb)) {
610
			/* This fence is already signaled */
611 612
			if (idx)
				*idx = i;
613 614 615 616 617 618 619 620 621 622
			goto fence_rm_cb;
		}
	}

	while (ret > 0) {
		if (intr)
			set_current_state(TASK_INTERRUPTIBLE);
		else
			set_current_state(TASK_UNINTERRUPTIBLE);

623
		if (dma_fence_test_signaled_any(fences, count, idx))
624 625 626 627 628 629 630 631 632 633 634 635
			break;

		ret = schedule_timeout(ret);

		if (ret > 0 && intr && signal_pending(current))
			ret = -ERESTARTSYS;
	}

	__set_current_state(TASK_RUNNING);

fence_rm_cb:
	while (i-- > 0)
636
		dma_fence_remove_callback(fences[i], &cb[i].base);
637 638 639 640 641 642

err_free_cb:
	kfree(cb);

	return ret;
}
643
EXPORT_SYMBOL(dma_fence_wait_any_timeout);
644

645
/**
646
 * dma_fence_init - Initialize a custom fence.
647 648 649 650 651
 * @fence: the fence to initialize
 * @ops: the dma_fence_ops for operations on this fence
 * @lock: the irqsafe spinlock to use for locking this fence
 * @context: the execution context this fence is run on
 * @seqno: a linear increasing sequence number for this context
652 653 654
 *
 * Initializes an allocated fence, the caller doesn't have to keep its
 * refcount after committing with this fence, but it will need to hold a
655
 * refcount again if &dma_fence_ops.enable_signaling gets called.
656 657
 *
 * context and seqno are used for easy comparison between fences, allowing
658
 * to check which fence is later by simply using dma_fence_later().
659 660
 */
void
661
dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
662
	       spinlock_t *lock, u64 context, u64 seqno)
663 664
{
	BUG_ON(!lock);
665
	BUG_ON(!ops || !ops->get_driver_name || !ops->get_timeline_name);
666 667 668 669 670 671 672 673

	kref_init(&fence->refcount);
	fence->ops = ops;
	INIT_LIST_HEAD(&fence->cb_list);
	fence->lock = lock;
	fence->context = context;
	fence->seqno = seqno;
	fence->flags = 0UL;
674
	fence->error = 0;
675

676
	trace_dma_fence_init(fence);
677
}
678
EXPORT_SYMBOL(dma_fence_init);