Skip to contentSkip to conversation

drm/amdgpu: Fix UserQ fence driver reference race

PATCH In review Erhan Bilgili 2 participants 2 messages Nov 13

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix UserQ fence driver reference race

USERQ_WAIT adds the driver to fence_drv_xa before taking its reference. Once the lock is dropped, USERQ_SIGNAL can remove the entry and put the driver before WAIT gets its reference. This can free the driver while WAIT is still using it.

Take the reference before dropping fence_drv_lock.

Fixes a292fdecd728 ("drm/amdgpu: Implement userqueue signal/wait IOCTL")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -2
index dd976a50e053..13a339918ee3 100644
@@ -912,12 +912,12 @@ amdgpu_userq_wait_return_fence_info(struct drm_device *dev, struct drm_file *fil
 		mutex_lock(&waitq->fence_drv_lock);
 		r = xa_alloc(&waitq->fence_drv_xa, &index, fence_drv,
 			     xa_limit_32b, GFP_KERNEL);
+		if (!r)
+			amdgpu_userq_fence_driver_get(fence_drv);
 		mutex_unlock(&waitq->fence_drv_lock);
 		if (r)
 			goto put_waitq;
 
-		amdgpu_userq_fence_driver_get(fence_drv);
-
 		trace_amdgpu_userq_wait_deps(dev->dev, waitq, userq_fence);
 
 		/* Store drm syncobj's gpu va address and value */
Signature · 1 line
2.56.1
maintainer
#1 lore raw
Quoted 4 lines · Erhan Bilgili

USERQ_WAIT adds the driver to fence_drv_xa before taking its reference. Once the lock is dropped, USERQ_SIGNAL can remove the entry and put the driver before WAIT gets its reference. This can free the driver while WAIT is still using it.

The fence is holding the reference to the fence driver so that is completely unnecessary.

Quoted 3 lines

Take the reference before dropping fence_drv_lock.

That just unnecessarily complicates the code.

It could be that we change the userq fence so that it doesn't hold the fence driver reference in the future, but that would require a completely different approach here.

Regards,
Christian.

Quoted 25 lines
Fixes a292fdecd728 ("drm/amdgpu: Implement userqueue signal/wait IOCTL")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -2
index dd976a50e053..13a339918ee3 100644
@@ -912,12 +912,12 @@ amdgpu_userq_wait_return_fence_info(struct drm_device *dev, struct drm_file *fil
 		mutex_lock(&waitq->fence_drv_lock);
 		r = xa_alloc(&waitq->fence_drv_xa, &index, fence_drv,
 			     xa_limit_32b, GFP_KERNEL);
+		if (!r)
+			amdgpu_userq_fence_driver_get(fence_drv);
 		mutex_unlock(&waitq->fence_drv_lock);
 		if (r)
 			goto put_waitq;
 
-		amdgpu_userq_fence_driver_get(fence_drv);
-
 		trace_amdgpu_userq_wait_deps(dev->dev, waitq, userq_fence);
 
 		/* Store drm syncobj's gpu va address and value */