Commit 43cf1fc0 authored by Chunming Zhou's avatar Chunming Zhou Committed by Christian König

drm: fix deadlock of syncobj v6

v2:
add a mutex between sync_cb execution and free.
v3:
clearly separating the roles for pt_lock and cb_mutex (Chris)
v4:
the cb_mutex should be taken outside of the pt_lock around
this if() block. (Chris)
v5:
fix a corner case
v6:
tidy drm_syncobj_fence_get_or_add_callback up. (Chris)

Tested by syncobj_basic and syncobj_wait of igt.
Signed-off-by: default avatarChunming Zhou <david1.zhou@amd.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Christian König <christian.koenig@amd.com>
Cc: intel-gfx@lists.freedesktop.org
Reviewed-by: default avatarChris Wilson <chris@chris-wilson.co.uk>
Signed-off-by: default avatarChristian König <christian.koenig@amd.com>
Link: https://patchwork.kernel.org/patch/10652893/
parent 3d42f1dd
...@@ -106,6 +106,42 @@ struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private, ...@@ -106,6 +106,42 @@ struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
} }
EXPORT_SYMBOL(drm_syncobj_find); EXPORT_SYMBOL(drm_syncobj_find);
static struct dma_fence
*drm_syncobj_find_signal_pt_for_point(struct drm_syncobj *syncobj,
uint64_t point)
{
struct drm_syncobj_signal_pt *signal_pt;
if ((syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) &&
(point <= syncobj->timeline)) {
struct drm_syncobj_stub_fence *fence =
kzalloc(sizeof(struct drm_syncobj_stub_fence),
GFP_KERNEL);
if (!fence)
return NULL;
spin_lock_init(&fence->lock);
dma_fence_init(&fence->base,
&drm_syncobj_stub_fence_ops,
&fence->lock,
syncobj->timeline_context,
point);
dma_fence_signal(&fence->base);
return &fence->base;
}
list_for_each_entry(signal_pt, &syncobj->signal_pt_list, list) {
if (point > signal_pt->value)
continue;
if ((syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) &&
(point != signal_pt->value))
continue;
return dma_fence_get(&signal_pt->fence_array->base);
}
return NULL;
}
static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj, static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj,
struct drm_syncobj_cb *cb, struct drm_syncobj_cb *cb,
drm_syncobj_func_t func) drm_syncobj_func_t func)
...@@ -114,115 +150,71 @@ static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj, ...@@ -114,115 +150,71 @@ static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj,
list_add_tail(&cb->node, &syncobj->cb_list); list_add_tail(&cb->node, &syncobj->cb_list);
} }
static int drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj, static void drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj,
struct dma_fence **fence, struct dma_fence **fence,
struct drm_syncobj_cb *cb, struct drm_syncobj_cb *cb,
drm_syncobj_func_t func) drm_syncobj_func_t func)
{ {
int ret; u64 pt_value = 0;
ret = drm_syncobj_search_fence(syncobj, 0, 0, fence); if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) {
if (!ret) /*BINARY syncobj always wait on last pt */
return 1; pt_value = syncobj->signal_point;
spin_lock(&syncobj->lock); if (pt_value == 0)
/* We've already tried once to get a fence and failed. Now that we pt_value += DRM_SYNCOBJ_BINARY_POINT;
* have the lock, try one more time just to be sure we don't add a
* callback when a fence has already been set.
*/
if (!list_empty(&syncobj->signal_pt_list)) {
spin_unlock(&syncobj->lock);
drm_syncobj_search_fence(syncobj, 0, 0, fence);
if (*fence)
return 1;
spin_lock(&syncobj->lock);
} else {
*fence = NULL;
drm_syncobj_add_callback_locked(syncobj, cb, func);
ret = 0;
} }
spin_unlock(&syncobj->lock);
return ret; mutex_lock(&syncobj->cb_mutex);
spin_lock(&syncobj->pt_lock);
*fence = drm_syncobj_find_signal_pt_for_point(syncobj, pt_value);
spin_unlock(&syncobj->pt_lock);
if (!*fence)
drm_syncobj_add_callback_locked(syncobj, cb, func);
mutex_unlock(&syncobj->cb_mutex);
} }
void drm_syncobj_add_callback(struct drm_syncobj *syncobj, void drm_syncobj_add_callback(struct drm_syncobj *syncobj,
struct drm_syncobj_cb *cb, struct drm_syncobj_cb *cb,
drm_syncobj_func_t func) drm_syncobj_func_t func)
{ {
spin_lock(&syncobj->lock); mutex_lock(&syncobj->cb_mutex);
drm_syncobj_add_callback_locked(syncobj, cb, func); drm_syncobj_add_callback_locked(syncobj, cb, func);
spin_unlock(&syncobj->lock); mutex_unlock(&syncobj->cb_mutex);
} }
void drm_syncobj_remove_callback(struct drm_syncobj *syncobj, void drm_syncobj_remove_callback(struct drm_syncobj *syncobj,
struct drm_syncobj_cb *cb) struct drm_syncobj_cb *cb)
{ {
spin_lock(&syncobj->lock); mutex_lock(&syncobj->cb_mutex);
list_del_init(&cb->node); list_del_init(&cb->node);
spin_unlock(&syncobj->lock); mutex_unlock(&syncobj->cb_mutex);
} }
static void drm_syncobj_init(struct drm_syncobj *syncobj) static void drm_syncobj_init(struct drm_syncobj *syncobj)
{ {
spin_lock(&syncobj->lock); spin_lock(&syncobj->pt_lock);
syncobj->timeline_context = dma_fence_context_alloc(1); syncobj->timeline_context = dma_fence_context_alloc(1);
syncobj->timeline = 0; syncobj->timeline = 0;
syncobj->signal_point = 0; syncobj->signal_point = 0;
init_waitqueue_head(&syncobj->wq); init_waitqueue_head(&syncobj->wq);
INIT_LIST_HEAD(&syncobj->signal_pt_list); INIT_LIST_HEAD(&syncobj->signal_pt_list);
spin_unlock(&syncobj->lock); spin_unlock(&syncobj->pt_lock);
} }
static void drm_syncobj_fini(struct drm_syncobj *syncobj) static void drm_syncobj_fini(struct drm_syncobj *syncobj)
{ {
struct drm_syncobj_signal_pt *signal_pt = NULL, *tmp; struct drm_syncobj_signal_pt *signal_pt = NULL, *tmp;
spin_lock(&syncobj->lock); spin_lock(&syncobj->pt_lock);
list_for_each_entry_safe(signal_pt, tmp, list_for_each_entry_safe(signal_pt, tmp,
&syncobj->signal_pt_list, list) { &syncobj->signal_pt_list, list) {
list_del(&signal_pt->list); list_del(&signal_pt->list);
dma_fence_put(&signal_pt->fence_array->base); dma_fence_put(&signal_pt->fence_array->base);
kfree(signal_pt); kfree(signal_pt);
} }
spin_unlock(&syncobj->lock); spin_unlock(&syncobj->pt_lock);
}
static struct dma_fence
*drm_syncobj_find_signal_pt_for_point(struct drm_syncobj *syncobj,
uint64_t point)
{
struct drm_syncobj_signal_pt *signal_pt;
if ((syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) &&
(point <= syncobj->timeline)) {
struct drm_syncobj_stub_fence *fence =
kzalloc(sizeof(struct drm_syncobj_stub_fence),
GFP_KERNEL);
if (!fence)
return NULL;
spin_lock_init(&fence->lock);
dma_fence_init(&fence->base,
&drm_syncobj_stub_fence_ops,
&fence->lock,
syncobj->timeline_context,
point);
dma_fence_signal(&fence->base);
return &fence->base;
}
list_for_each_entry(signal_pt, &syncobj->signal_pt_list, list) {
if (point > signal_pt->value)
continue;
if ((syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) &&
(point != signal_pt->value))
continue;
return dma_fence_get(&signal_pt->fence_array->base);
}
return NULL;
} }
static int drm_syncobj_create_signal_pt(struct drm_syncobj *syncobj, static int drm_syncobj_create_signal_pt(struct drm_syncobj *syncobj,
...@@ -249,14 +241,14 @@ static int drm_syncobj_create_signal_pt(struct drm_syncobj *syncobj, ...@@ -249,14 +241,14 @@ static int drm_syncobj_create_signal_pt(struct drm_syncobj *syncobj,
fences[num_fences++] = dma_fence_get(fence); fences[num_fences++] = dma_fence_get(fence);
/* timeline syncobj must take this dependency */ /* timeline syncobj must take this dependency */
if (syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) { if (syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) {
spin_lock(&syncobj->lock); spin_lock(&syncobj->pt_lock);
if (!list_empty(&syncobj->signal_pt_list)) { if (!list_empty(&syncobj->signal_pt_list)) {
tail_pt = list_last_entry(&syncobj->signal_pt_list, tail_pt = list_last_entry(&syncobj->signal_pt_list,
struct drm_syncobj_signal_pt, list); struct drm_syncobj_signal_pt, list);
fences[num_fences++] = fences[num_fences++] =
dma_fence_get(&tail_pt->fence_array->base); dma_fence_get(&tail_pt->fence_array->base);
} }
spin_unlock(&syncobj->lock); spin_unlock(&syncobj->pt_lock);
} }
signal_pt->fence_array = dma_fence_array_create(num_fences, fences, signal_pt->fence_array = dma_fence_array_create(num_fences, fences,
syncobj->timeline_context, syncobj->timeline_context,
...@@ -266,16 +258,16 @@ static int drm_syncobj_create_signal_pt(struct drm_syncobj *syncobj, ...@@ -266,16 +258,16 @@ static int drm_syncobj_create_signal_pt(struct drm_syncobj *syncobj,
goto fail; goto fail;
} }
spin_lock(&syncobj->lock); spin_lock(&syncobj->pt_lock);
if (syncobj->signal_point >= point) { if (syncobj->signal_point >= point) {
DRM_WARN("A later signal is ready!"); DRM_WARN("A later signal is ready!");
spin_unlock(&syncobj->lock); spin_unlock(&syncobj->pt_lock);
goto exist; goto exist;
} }
signal_pt->value = point; signal_pt->value = point;
list_add_tail(&signal_pt->list, &syncobj->signal_pt_list); list_add_tail(&signal_pt->list, &syncobj->signal_pt_list);
syncobj->signal_point = point; syncobj->signal_point = point;
spin_unlock(&syncobj->lock); spin_unlock(&syncobj->pt_lock);
wake_up_all(&syncobj->wq); wake_up_all(&syncobj->wq);
return 0; return 0;
...@@ -294,7 +286,7 @@ static void drm_syncobj_garbage_collection(struct drm_syncobj *syncobj) ...@@ -294,7 +286,7 @@ static void drm_syncobj_garbage_collection(struct drm_syncobj *syncobj)
{ {
struct drm_syncobj_signal_pt *signal_pt, *tmp, *tail_pt; struct drm_syncobj_signal_pt *signal_pt, *tmp, *tail_pt;
spin_lock(&syncobj->lock); spin_lock(&syncobj->pt_lock);
tail_pt = list_last_entry(&syncobj->signal_pt_list, tail_pt = list_last_entry(&syncobj->signal_pt_list,
struct drm_syncobj_signal_pt, struct drm_syncobj_signal_pt,
list); list);
...@@ -315,7 +307,7 @@ static void drm_syncobj_garbage_collection(struct drm_syncobj *syncobj) ...@@ -315,7 +307,7 @@ static void drm_syncobj_garbage_collection(struct drm_syncobj *syncobj)
} }
} }
spin_unlock(&syncobj->lock); spin_unlock(&syncobj->pt_lock);
} }
/** /**
* drm_syncobj_replace_fence - replace fence in a sync object. * drm_syncobj_replace_fence - replace fence in a sync object.
...@@ -344,13 +336,14 @@ void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, ...@@ -344,13 +336,14 @@ void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
drm_syncobj_create_signal_pt(syncobj, fence, pt_value); drm_syncobj_create_signal_pt(syncobj, fence, pt_value);
if (fence) { if (fence) {
struct drm_syncobj_cb *cur, *tmp; struct drm_syncobj_cb *cur, *tmp;
LIST_HEAD(cb_list);
spin_lock(&syncobj->lock); mutex_lock(&syncobj->cb_mutex);
list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) { list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {
list_del_init(&cur->node); list_del_init(&cur->node);
cur->func(syncobj, cur); cur->func(syncobj, cur);
} }
spin_unlock(&syncobj->lock); mutex_unlock(&syncobj->cb_mutex);
} }
} }
EXPORT_SYMBOL(drm_syncobj_replace_fence); EXPORT_SYMBOL(drm_syncobj_replace_fence);
...@@ -386,11 +379,11 @@ drm_syncobj_point_get(struct drm_syncobj *syncobj, u64 point, u64 flags, ...@@ -386,11 +379,11 @@ drm_syncobj_point_get(struct drm_syncobj *syncobj, u64 point, u64 flags,
if (ret < 0) if (ret < 0)
return ret; return ret;
} }
spin_lock(&syncobj->lock); spin_lock(&syncobj->pt_lock);
*fence = drm_syncobj_find_signal_pt_for_point(syncobj, point); *fence = drm_syncobj_find_signal_pt_for_point(syncobj, point);
if (!*fence) if (!*fence)
ret = -EINVAL; ret = -EINVAL;
spin_unlock(&syncobj->lock); spin_unlock(&syncobj->pt_lock);
return ret; return ret;
} }
...@@ -500,7 +493,8 @@ int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags, ...@@ -500,7 +493,8 @@ int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
kref_init(&syncobj->refcount); kref_init(&syncobj->refcount);
INIT_LIST_HEAD(&syncobj->cb_list); INIT_LIST_HEAD(&syncobj->cb_list);
spin_lock_init(&syncobj->lock); spin_lock_init(&syncobj->pt_lock);
mutex_init(&syncobj->cb_mutex);
if (flags & DRM_SYNCOBJ_CREATE_TYPE_TIMELINE) if (flags & DRM_SYNCOBJ_CREATE_TYPE_TIMELINE)
syncobj->type = DRM_SYNCOBJ_TYPE_TIMELINE; syncobj->type = DRM_SYNCOBJ_TYPE_TIMELINE;
else else
......
...@@ -75,9 +75,13 @@ struct drm_syncobj { ...@@ -75,9 +75,13 @@ struct drm_syncobj {
*/ */
struct list_head cb_list; struct list_head cb_list;
/** /**
* @lock: Protects syncobj list and write-locks &fence. * @pt_lock: Protects pt list.
*/ */
spinlock_t lock; spinlock_t pt_lock;
/**
* @cb_mutex: Protects syncobj cb list.
*/
struct mutex cb_mutex;
/** /**
* @file: A file backing for this syncobj. * @file: A file backing for this syncobj.
*/ */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment