Skip to content

Commit 9c19fb1

Browse files
gfxstrandairlied
authored andcommitted
drm/syncobj: Add a callback mechanism for replace_fence (v3)
It is useful in certain circumstances to know when the fence is replaced in a syncobj. Specifically, it may be useful to know when the fence goes from NULL to something valid. This does make syncobj_replace_fence a little more expensive because it has to take a lock but, in the common case where there is no callback list, it spends a very short amount of time inside the lock. v2: - Don't lock in drm_syncobj_fence_get. We only really need to lock around fence_replace to make the callback work. v3: - Fix the cb_list comment to make kbuild happy Signed-off-by: Jason Ekstrand <jason@jlekstrand.net> Signed-off-by: Dave Airlie <airlied@redhat.com>
1 parent 5e60a10 commit 9c19fb1

2 files changed

Lines changed: 97 additions & 2 deletions

File tree

‎drivers/gpu/drm/drm_syncobj.c‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,46 @@ struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
8080
}
8181
EXPORT_SYMBOL(drm_syncobj_find);
8282

83+
static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj,
84+
struct drm_syncobj_cb *cb,
85+
drm_syncobj_func_t func)
86+
{
87+
cb->func = func;
88+
list_add_tail(&cb->node, &syncobj->cb_list);
89+
}
90+
91+
/**
92+
* drm_syncobj_add_callback - adds a callback to syncobj::cb_list
93+
* @syncobj: Sync object to which to add the callback
94+
* @cb: Callback to add
95+
* @func: Func to use when initializing the drm_syncobj_cb struct
96+
*
97+
* This adds a callback to be called next time the fence is replaced
98+
*/
99+
void drm_syncobj_add_callback(struct drm_syncobj *syncobj,
100+
struct drm_syncobj_cb *cb,
101+
drm_syncobj_func_t func)
102+
{
103+
spin_lock(&syncobj->lock);
104+
drm_syncobj_add_callback_locked(syncobj, cb, func);
105+
spin_unlock(&syncobj->lock);
106+
}
107+
EXPORT_SYMBOL(drm_syncobj_add_callback);
108+
109+
/**
110+
* drm_syncobj_add_callback - removes a callback to syncobj::cb_list
111+
* @syncobj: Sync object from which to remove the callback
112+
* @cb: Callback to remove
113+
*/
114+
void drm_syncobj_remove_callback(struct drm_syncobj *syncobj,
115+
struct drm_syncobj_cb *cb)
116+
{
117+
spin_lock(&syncobj->lock);
118+
list_del_init(&cb->node);
119+
spin_unlock(&syncobj->lock);
120+
}
121+
EXPORT_SYMBOL(drm_syncobj_remove_callback);
122+
83123
/**
84124
* drm_syncobj_replace_fence - replace fence in a sync object.
85125
* @syncobj: Sync object to replace fence in
@@ -91,10 +131,24 @@ void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
91131
struct dma_fence *fence)
92132
{
93133
struct dma_fence *old_fence;
134+
struct drm_syncobj_cb *cur, *tmp;
94135

95136
if (fence)
96137
dma_fence_get(fence);
97-
old_fence = xchg(&syncobj->fence, fence);
138+
139+
spin_lock(&syncobj->lock);
140+
141+
old_fence = syncobj->fence;
142+
syncobj->fence = fence;
143+
144+
if (fence != old_fence) {
145+
list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {
146+
list_del_init(&cur->node);
147+
cur->func(syncobj, cur);
148+
}
149+
}
150+
151+
spin_unlock(&syncobj->lock);
98152

99153
dma_fence_put(old_fence);
100154
}
@@ -130,7 +184,7 @@ void drm_syncobj_free(struct kref *kref)
130184
struct drm_syncobj *syncobj = container_of(kref,
131185
struct drm_syncobj,
132186
refcount);
133-
dma_fence_put(syncobj->fence);
187+
drm_syncobj_replace_fence(syncobj, NULL);
134188
kfree(syncobj);
135189
}
136190
EXPORT_SYMBOL(drm_syncobj_free);
@@ -146,6 +200,8 @@ static int drm_syncobj_create(struct drm_file *file_private,
146200
return -ENOMEM;
147201

148202
kref_init(&syncobj->refcount);
203+
INIT_LIST_HEAD(&syncobj->cb_list);
204+
spin_lock_init(&syncobj->lock);
149205

150206
idr_preload(GFP_KERNEL);
151207
spin_lock(&file_private->syncobj_table_lock);

‎include/drm/drm_syncobj.h‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include "linux/dma-fence.h"
3030

31+
struct drm_syncobj_cb;
32+
3133
/**
3234
* struct drm_syncobj - sync object.
3335
*
@@ -43,15 +45,47 @@ struct drm_syncobj {
4345
/**
4446
* @fence:
4547
* NULL or a pointer to the fence bound to this object.
48+
*
49+
* This field should not be used directly. Use drm_syncobj_fence_get
50+
* and drm_syncobj_replace_fence instead.
4651
*/
4752
struct dma_fence *fence;
53+
/**
54+
* @cb_list:
55+
* List of callbacks to call when the fence gets replaced
56+
*/
57+
struct list_head cb_list;
58+
/**
59+
* @lock:
60+
* locks cb_list and write-locks fence.
61+
*/
62+
spinlock_t lock;
4863
/**
4964
* @file:
5065
* a file backing for this syncobj.
5166
*/
5267
struct file *file;
5368
};
5469

70+
typedef void (*drm_syncobj_func_t)(struct drm_syncobj *syncobj,
71+
struct drm_syncobj_cb *cb);
72+
73+
/**
74+
* struct drm_syncobj_cb - callback for drm_syncobj_add_callback
75+
* @node: used by drm_syncob_add_callback to append this struct to
76+
* syncobj::cb_list
77+
* @func: drm_syncobj_func_t to call
78+
*
79+
* This struct will be initialized by drm_syncobj_add_callback, additional
80+
* data can be passed along by embedding drm_syncobj_cb in another struct.
81+
* The callback will get called the next time drm_syncobj_replace_fence is
82+
* called.
83+
*/
84+
struct drm_syncobj_cb {
85+
struct list_head node;
86+
drm_syncobj_func_t func;
87+
};
88+
5589
void drm_syncobj_free(struct kref *kref);
5690

5791
/**
@@ -91,6 +125,11 @@ drm_syncobj_fence_get(struct drm_syncobj *syncobj)
91125

92126
struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
93127
u32 handle);
128+
void drm_syncobj_add_callback(struct drm_syncobj *syncobj,
129+
struct drm_syncobj_cb *cb,
130+
drm_syncobj_func_t func);
131+
void drm_syncobj_remove_callback(struct drm_syncobj *syncobj,
132+
struct drm_syncobj_cb *cb);
94133
void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
95134
struct dma_fence *fence);
96135
int drm_syncobj_find_fence(struct drm_file *file_private,

0 commit comments

Comments
 (0)