Skip to content

Commit 309a548

Browse files
gfxstrandairlied
authored andcommitted
drm/syncobj: Add a race-free drm_syncobj_fence_get helper (v2)
The atomic exchange operation in drm_syncobj_replace_fence is sufficient for the case where it races with itself. However, if you have a race between a replace_fence and dma_fence_get(syncobj->fence), you may end up with the entire replace_fence happening between the point in time where the one thread gets the syncobj->fence pointer and when it calls dma_fence_get() on it. If this happens, then the reference may be dropped before we get a chance to get a new one. The new helper uses dma_fence_get_rcu_safe to get rid of the race. This is also needed because it allows us to do a bit more than just get a reference in drm_syncobj_fence_get should we wish to do so. v2: - RCU isn't that scary - Call rcu_read_lock/unlock - Don't rename fence to _fence - Make the helper static inline Signed-off-by: Jason Ekstrand <jason@jlekstrand.net> Acked-by: Christian König <christian.koenig@amd.com> (v1) Signed-off-by: Dave Airlie <airlied@redhat.com>
1 parent afaf592 commit 309a548

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎drivers/gpu/drm/drm_syncobj.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ int drm_syncobj_find_fence(struct drm_file *file_private,
105105
if (!syncobj)
106106
return -ENOENT;
107107

108-
*fence = dma_fence_get(syncobj->fence);
108+
*fence = drm_syncobj_fence_get(syncobj);
109109
if (!*fence) {
110110
ret = -EINVAL;
111111
}

‎include/drm/drm_syncobj.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ drm_syncobj_put(struct drm_syncobj *obj)
7777
kref_put(&obj->refcount, drm_syncobj_free);
7878
}
7979

80+
static inline struct dma_fence *
81+
drm_syncobj_fence_get(struct drm_syncobj *syncobj)
82+
{
83+
struct dma_fence *fence;
84+
85+
rcu_read_lock();
86+
fence = dma_fence_get_rcu_safe(&syncobj->fence);
87+
rcu_read_unlock();
88+
89+
return fence;
90+
}
91+
8092
struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
8193
u32 handle);
8294
void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,

0 commit comments

Comments
 (0)