2019-05-27 14:55:21 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2016-06-01 21:10:03 +08:00
|
|
|
/*
|
2016-10-25 20:00:45 +08:00
|
|
|
* dma-fence-array: aggregate fences to be waited together
|
2016-06-01 21:10:03 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Collabora Ltd
|
|
|
|
* Copyright (C) 2016 Advanced Micro Devices, Inc.
|
|
|
|
* Authors:
|
|
|
|
* Gustavo Padovan <gustavo@padovan.org>
|
|
|
|
* Christian König <christian.koenig@amd.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/export.h>
|
|
|
|
#include <linux/slab.h>
|
2016-10-25 20:00:45 +08:00
|
|
|
#include <linux/dma-fence-array.h>
|
2016-06-01 21:10:03 +08:00
|
|
|
|
2019-08-12 05:09:02 +08:00
|
|
|
#define PENDING_ERROR 1
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static const char *dma_fence_array_get_driver_name(struct dma_fence *fence)
|
2016-06-01 21:10:03 +08:00
|
|
|
{
|
2016-10-25 20:00:45 +08:00
|
|
|
return "dma_fence_array";
|
2016-06-01 21:10:03 +08:00
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static const char *dma_fence_array_get_timeline_name(struct dma_fence *fence)
|
2016-06-01 21:10:03 +08:00
|
|
|
{
|
|
|
|
return "unbound";
|
|
|
|
}
|
|
|
|
|
2019-08-12 05:09:02 +08:00
|
|
|
static void dma_fence_array_set_pending_error(struct dma_fence_array *array,
|
|
|
|
int error)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Propagate the first error reported by any of our fences, but only
|
|
|
|
* before we ourselves are signaled.
|
|
|
|
*/
|
|
|
|
if (error)
|
|
|
|
cmpxchg(&array->base.error, PENDING_ERROR, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dma_fence_array_clear_pending_error(struct dma_fence_array *array)
|
|
|
|
{
|
|
|
|
/* Clear the error flag if not actually set. */
|
|
|
|
cmpxchg(&array->base.error, PENDING_ERROR, 0);
|
|
|
|
}
|
|
|
|
|
dma-buf/fence: Fix lock inversion within dma-fence-array
Ages ago Rob Clark noted,
"Currently with fence-array, we have a potential deadlock situation. If
we fence_add_callback() on an array-fence, the array-fence's lock is
acquired first, and in it's ->enable_signaling() callback, it will install
cbs on it's array-member fences, so the array-member's lock is acquired
second.
But in the signal path, the array-member's lock is acquired first, and
the array-fence's lock acquired second."
Rob proposed either extensive changes to dma-fence to unnest the
fence-array signaling, or to defer the signaling onto a workqueue. This
is a more refined version of the later, that should keep the latency
of the fence signaling to a minimum by using an irq-work, which is
executed asap.
Reported-by: Rob Clark <robdclark@gmail.com>
Suggested-by: Rob Clark <robdclark@gmail.com>
References: 1476635975-21981-1-git-send-email-robdclark@gmail.com
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Rob Clark <robdclark@gmail.com>
Cc: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Christian König <christian.koenig@amd.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20171114162719.30958-1-chris@chris-wilson.co.uk
2017-11-15 00:27:19 +08:00
|
|
|
static void irq_dma_fence_array_work(struct irq_work *wrk)
|
|
|
|
{
|
|
|
|
struct dma_fence_array *array = container_of(wrk, typeof(*array), work);
|
|
|
|
|
2019-08-12 05:09:02 +08:00
|
|
|
dma_fence_array_clear_pending_error(array);
|
|
|
|
|
dma-buf/fence: Fix lock inversion within dma-fence-array
Ages ago Rob Clark noted,
"Currently with fence-array, we have a potential deadlock situation. If
we fence_add_callback() on an array-fence, the array-fence's lock is
acquired first, and in it's ->enable_signaling() callback, it will install
cbs on it's array-member fences, so the array-member's lock is acquired
second.
But in the signal path, the array-member's lock is acquired first, and
the array-fence's lock acquired second."
Rob proposed either extensive changes to dma-fence to unnest the
fence-array signaling, or to defer the signaling onto a workqueue. This
is a more refined version of the later, that should keep the latency
of the fence signaling to a minimum by using an irq-work, which is
executed asap.
Reported-by: Rob Clark <robdclark@gmail.com>
Suggested-by: Rob Clark <robdclark@gmail.com>
References: 1476635975-21981-1-git-send-email-robdclark@gmail.com
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Rob Clark <robdclark@gmail.com>
Cc: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Christian König <christian.koenig@amd.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20171114162719.30958-1-chris@chris-wilson.co.uk
2017-11-15 00:27:19 +08:00
|
|
|
dma_fence_signal(&array->base);
|
|
|
|
dma_fence_put(&array->base);
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static void dma_fence_array_cb_func(struct dma_fence *f,
|
|
|
|
struct dma_fence_cb *cb)
|
2016-06-01 21:10:03 +08:00
|
|
|
{
|
2016-10-25 20:00:45 +08:00
|
|
|
struct dma_fence_array_cb *array_cb =
|
|
|
|
container_of(cb, struct dma_fence_array_cb, cb);
|
|
|
|
struct dma_fence_array *array = array_cb->array;
|
2016-06-01 21:10:03 +08:00
|
|
|
|
2019-08-12 05:09:02 +08:00
|
|
|
dma_fence_array_set_pending_error(array, f->error);
|
|
|
|
|
2016-06-01 21:10:03 +08:00
|
|
|
if (atomic_dec_and_test(&array->num_pending))
|
dma-buf/fence: Fix lock inversion within dma-fence-array
Ages ago Rob Clark noted,
"Currently with fence-array, we have a potential deadlock situation. If
we fence_add_callback() on an array-fence, the array-fence's lock is
acquired first, and in it's ->enable_signaling() callback, it will install
cbs on it's array-member fences, so the array-member's lock is acquired
second.
But in the signal path, the array-member's lock is acquired first, and
the array-fence's lock acquired second."
Rob proposed either extensive changes to dma-fence to unnest the
fence-array signaling, or to defer the signaling onto a workqueue. This
is a more refined version of the later, that should keep the latency
of the fence signaling to a minimum by using an irq-work, which is
executed asap.
Reported-by: Rob Clark <robdclark@gmail.com>
Suggested-by: Rob Clark <robdclark@gmail.com>
References: 1476635975-21981-1-git-send-email-robdclark@gmail.com
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Rob Clark <robdclark@gmail.com>
Cc: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Christian König <christian.koenig@amd.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20171114162719.30958-1-chris@chris-wilson.co.uk
2017-11-15 00:27:19 +08:00
|
|
|
irq_work_queue(&array->work);
|
|
|
|
else
|
|
|
|
dma_fence_put(&array->base);
|
2016-06-01 21:10:03 +08:00
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
|
2016-06-01 21:10:03 +08:00
|
|
|
{
|
2016-10-25 20:00:45 +08:00
|
|
|
struct dma_fence_array *array = to_dma_fence_array(fence);
|
|
|
|
struct dma_fence_array_cb *cb = (void *)(&array[1]);
|
2016-06-01 21:10:03 +08:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < array->num_fences; ++i) {
|
|
|
|
cb[i].array = array;
|
2016-06-01 21:10:04 +08:00
|
|
|
/*
|
|
|
|
* As we may report that the fence is signaled before all
|
|
|
|
* callbacks are complete, we need to take an additional
|
|
|
|
* reference count on the array so that we do not free it too
|
|
|
|
* early. The core fence handling will only hold the reference
|
|
|
|
* until we signal the array as complete (but that is now
|
|
|
|
* insufficient).
|
|
|
|
*/
|
2016-10-25 20:00:45 +08:00
|
|
|
dma_fence_get(&array->base);
|
|
|
|
if (dma_fence_add_callback(array->fences[i], &cb[i].cb,
|
|
|
|
dma_fence_array_cb_func)) {
|
2019-08-12 05:09:02 +08:00
|
|
|
int error = array->fences[i]->error;
|
|
|
|
|
|
|
|
dma_fence_array_set_pending_error(array, error);
|
2016-10-25 20:00:45 +08:00
|
|
|
dma_fence_put(&array->base);
|
2019-08-12 05:09:02 +08:00
|
|
|
if (atomic_dec_and_test(&array->num_pending)) {
|
|
|
|
dma_fence_array_clear_pending_error(array);
|
2016-06-01 21:10:03 +08:00
|
|
|
return false;
|
2019-08-12 05:09:02 +08:00
|
|
|
}
|
2016-06-01 21:10:04 +08:00
|
|
|
}
|
2016-06-01 21:10:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static bool dma_fence_array_signaled(struct dma_fence *fence)
|
2016-06-01 21:10:03 +08:00
|
|
|
{
|
2016-10-25 20:00:45 +08:00
|
|
|
struct dma_fence_array *array = to_dma_fence_array(fence);
|
2016-06-01 21:10:03 +08:00
|
|
|
|
2024-06-12 13:13:20 +08:00
|
|
|
if (atomic_read(&array->num_pending) > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
dma_fence_array_clear_pending_error(array);
|
|
|
|
return true;
|
2016-06-01 21:10:03 +08:00
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static void dma_fence_array_release(struct dma_fence *fence)
|
2016-06-01 21:10:03 +08:00
|
|
|
{
|
2016-10-25 20:00:45 +08:00
|
|
|
struct dma_fence_array *array = to_dma_fence_array(fence);
|
2016-06-01 21:10:03 +08:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < array->num_fences; ++i)
|
2016-10-25 20:00:45 +08:00
|
|
|
dma_fence_put(array->fences[i]);
|
2016-06-01 21:10:03 +08:00
|
|
|
|
|
|
|
kfree(array->fences);
|
2016-10-25 20:00:45 +08:00
|
|
|
dma_fence_free(fence);
|
2016-06-01 21:10:03 +08:00
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
const struct dma_fence_ops dma_fence_array_ops = {
|
|
|
|
.get_driver_name = dma_fence_array_get_driver_name,
|
|
|
|
.get_timeline_name = dma_fence_array_get_timeline_name,
|
|
|
|
.enable_signaling = dma_fence_array_enable_signaling,
|
|
|
|
.signaled = dma_fence_array_signaled,
|
|
|
|
.release = dma_fence_array_release,
|
2016-06-01 21:10:03 +08:00
|
|
|
};
|
2016-10-25 20:00:45 +08:00
|
|
|
EXPORT_SYMBOL(dma_fence_array_ops);
|
2016-06-01 21:10:03 +08:00
|
|
|
|
|
|
|
/**
|
2016-10-25 20:00:45 +08:00
|
|
|
* dma_fence_array_create - Create a custom fence array
|
2016-06-01 21:10:04 +08:00
|
|
|
* @num_fences: [in] number of fences to add in the array
|
|
|
|
* @fences: [in] array containing the fences
|
|
|
|
* @context: [in] fence context to use
|
|
|
|
* @seqno: [in] sequence number to use
|
2016-08-17 07:31:00 +08:00
|
|
|
* @signal_on_any: [in] signal on any fence in the array
|
2016-06-01 21:10:03 +08:00
|
|
|
*
|
2016-10-25 20:00:45 +08:00
|
|
|
* Allocate a dma_fence_array object and initialize the base fence with
|
|
|
|
* dma_fence_init().
|
2016-06-01 21:10:03 +08:00
|
|
|
* In case of error it returns NULL.
|
|
|
|
*
|
2016-08-17 07:31:00 +08:00
|
|
|
* The caller should allocate the fences array with num_fences size
|
2016-06-01 21:10:03 +08:00
|
|
|
* and fill it with the fences it wants to add to the object. Ownership of this
|
2016-10-25 20:00:45 +08:00
|
|
|
* array is taken and dma_fence_put() is used on each fence on release.
|
2016-06-01 21:10:04 +08:00
|
|
|
*
|
|
|
|
* If @signal_on_any is true the fence array signals if any fence in the array
|
|
|
|
* signals, otherwise it signals when all fences in the array signal.
|
2016-06-01 21:10:03 +08:00
|
|
|
*/
|
2016-10-25 20:00:45 +08:00
|
|
|
struct dma_fence_array *dma_fence_array_create(int num_fences,
|
|
|
|
struct dma_fence **fences,
|
|
|
|
u64 context, unsigned seqno,
|
|
|
|
bool signal_on_any)
|
2016-06-01 21:10:03 +08:00
|
|
|
{
|
2016-10-25 20:00:45 +08:00
|
|
|
struct dma_fence_array *array;
|
2016-06-01 21:10:03 +08:00
|
|
|
size_t size = sizeof(*array);
|
|
|
|
|
|
|
|
/* Allocate the callback structures behind the array. */
|
2016-10-25 20:00:45 +08:00
|
|
|
size += num_fences * sizeof(struct dma_fence_array_cb);
|
2016-06-01 21:10:03 +08:00
|
|
|
array = kzalloc(size, GFP_KERNEL);
|
|
|
|
if (!array)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
spin_lock_init(&array->lock);
|
2016-10-25 20:00:45 +08:00
|
|
|
dma_fence_init(&array->base, &dma_fence_array_ops, &array->lock,
|
|
|
|
context, seqno);
|
dma-buf/fence: Fix lock inversion within dma-fence-array
Ages ago Rob Clark noted,
"Currently with fence-array, we have a potential deadlock situation. If
we fence_add_callback() on an array-fence, the array-fence's lock is
acquired first, and in it's ->enable_signaling() callback, it will install
cbs on it's array-member fences, so the array-member's lock is acquired
second.
But in the signal path, the array-member's lock is acquired first, and
the array-fence's lock acquired second."
Rob proposed either extensive changes to dma-fence to unnest the
fence-array signaling, or to defer the signaling onto a workqueue. This
is a more refined version of the later, that should keep the latency
of the fence signaling to a minimum by using an irq-work, which is
executed asap.
Reported-by: Rob Clark <robdclark@gmail.com>
Suggested-by: Rob Clark <robdclark@gmail.com>
References: 1476635975-21981-1-git-send-email-robdclark@gmail.com
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Rob Clark <robdclark@gmail.com>
Cc: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Christian König <christian.koenig@amd.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20171114162719.30958-1-chris@chris-wilson.co.uk
2017-11-15 00:27:19 +08:00
|
|
|
init_irq_work(&array->work, irq_dma_fence_array_work);
|
2016-06-01 21:10:03 +08:00
|
|
|
|
|
|
|
array->num_fences = num_fences;
|
2016-06-01 21:10:04 +08:00
|
|
|
atomic_set(&array->num_pending, signal_on_any ? 1 : num_fences);
|
2016-06-01 21:10:03 +08:00
|
|
|
array->fences = fences;
|
|
|
|
|
2019-08-12 05:09:02 +08:00
|
|
|
array->base.error = PENDING_ERROR;
|
|
|
|
|
2016-06-01 21:10:03 +08:00
|
|
|
return array;
|
|
|
|
}
|
2016-10-25 20:00:45 +08:00
|
|
|
EXPORT_SYMBOL(dma_fence_array_create);
|
2017-03-18 00:34:49 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* dma_fence_match_context - Check if all fences are from the given context
|
|
|
|
* @fence: [in] fence or fence array
|
|
|
|
* @context: [in] fence context to check all fences against
|
|
|
|
*
|
|
|
|
* Checks the provided fence or, for a fence array, all fences in the array
|
|
|
|
* against the given context. Returns false if any fence is from a different
|
|
|
|
* context.
|
|
|
|
*/
|
|
|
|
bool dma_fence_match_context(struct dma_fence *fence, u64 context)
|
|
|
|
{
|
|
|
|
struct dma_fence_array *array = to_dma_fence_array(fence);
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if (!dma_fence_is_array(fence))
|
|
|
|
return fence->context == context;
|
|
|
|
|
|
|
|
for (i = 0; i < array->num_fences; i++) {
|
|
|
|
if (array->fences[i]->context != context)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_fence_match_context);
|