Skip to contentSkip to conversation

drm/amdgpu: Reject UALink handle ioctl on devices without UALink

PATCH Superseded Mukul Joshi 3 participants 6 messages Sep 22 – Sep 23

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 drm/amdgpu: Reject UALink handle ioctl on devices without UALink

amdgpu_gem_ualink_handle_ioctl() dereferences adev->ualink.info before validating it, but that structure is only allocated when the UALink IP block is present, which is currently added only for GFX 12.1 only. The ioctl is registered for every amdgpu device with DRM_RENDER_ALLOW, so on any other ASIC a render node user can oops the kernel with a single call.

Return -EOPNOTSUPP when the UALink software state is absent.

Fixes 8a8bb5cb0410 ("drm/amdgpu: Add ioctl infra for exporting/importing UALink handles")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +7 -0
index 8411ea17172f..3d84f5a7a34d 100644
@@ -4072,6 +4072,13 @@ int amdgpu_gem_ualink_handle_ioctl(struct drm_device *dev, void *data,
 	u32 gem_handle;
 	int r, fd = -1;
 
+	/* The ioctl is registered for every amdgpu device, but the UALink
+	 * software state only exists on ASICs that instantiate the UALink IP
+	 * block.
+	 */
+	if (!adev->ualink.info)
+		return -EOPNOTSUPP;
+
 	if (adev->ualink.info->accel_state !=
 	    AMDGPU_UALINK_ACCEL_STATE_ACTIVE) {
 		dev_err(adev->dev,
Signature · 1 line
2.55.0
#1 lore raw

PATCH 2/3 drm/amdgpu: Fix BO reference leak in UALink export error path

amdgpu_ualink_export_handle() takes an explicit amdgpu_bo_ref() before building the exporter node, and the out: label only drops the reference from drm_gem_object_lookup(). The xarray insert failure path unrefs the BO, but the drm_gem_prime_handle_to_dmabuf() failure path does not.

Userspace can hit that path at will, for example by exhausting its file descriptor table so the dmabuf export returns -EMFILE, leaking a BO reference and its pinned VRAM on every attempt.

Fixes 6b1a5ad90778 ("drm/amdgpu: Implement UALink handle export")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +1 -0
index 3d84f5a7a34d..a7ae9ffde1b5 100644
@@ -4023,6 +4023,7 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,
 		if (IS_ERR(exp_xa_node->dmabuf)) {
 			r = PTR_ERR(exp_xa_node->dmabuf);
 			dev_err(adev->dev, "Failed to generate DMABuf for the BO\n");
+			amdgpu_bo_unref(&robj);
 			kfree(exp_xa_node);
 			goto out;
 		}
Signature · 1 line
2.55.0
Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] drm/amdgpu: Fix BO reference leak in UALink export error path

Quoted 21 lines · Mukul Joshi

amdgpu_ualink_export_handle() takes an explicit amdgpu_bo_ref() before building the exporter node, and the out: label only drops the reference from drm_gem_object_lookup(). The xarray insert failure path unrefs the BO, but the drm_gem_prime_handle_to_dmabuf() failure path does not.

Userspace can hit that path at will, for example by exhausting its file descriptor table so the dmabuf export returns -EMFILE, leaking a BO reference and its pinned VRAM on every attempt.

Fixes 6b1a5ad90778 ("drm/amdgpu: Implement UALink handle export")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 1 +
  1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -0
index 3d84f5a7a34d..a7ae9ffde1b5 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp, · line 4026

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +1 -0
@@ -4023,6 +4023,7 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,
  		if (IS_ERR(exp_xa_node->dmabuf)) {
  			r = PTR_ERR(exp_xa_node->dmabuf);
  			dev_err(adev->dev, "Failed to generate DMABuf for the BO\n");
+			amdgpu_bo_unref(&robj);

This would be clearer if you wrote it as

	amdgpu_bo_unref(&exp_xa_node->bo);

Because that's the field that the reference got assigned to and where it would get leaked from when you kfree(exp_xa_node). With that fixed, the patch is

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -0
  			kfree(exp_xa_node);
  			goto out;
  		}
#3 lore raw

PATCH 3/3 drm/amdgpu: Fix race on ualink_handle_lo in UALink handle export

In reply to Mukul Joshi

amdgpu_ualink_export_handle() tests robj->ualink_handle_lo and stores the generated handle into it without any lock; xa_lock only covers handle generation and the xarray insert.

Two threads can therefore both observe a zero handle for the same BO and both allocate an exporter node, take an amdgpu_bo_ref() and insert into exp_xa, with the last store to ualink_handle_lo winning. Teardown only erases the handle recorded in the BO, so the losing node keeps its BO reference and pinned VRAM until device teardown.

Serialize the check-generate-store sequence on a new per-device mutex. The BO reservation lock cannot be used, since drm_gem_prime_handle_to_dmabuf() reserves the BO itself via ttm_bo_setup_export().

Fixes 6b1a5ad90778 ("drm/amdgpu: Implement UALink handle export")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 12 +++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h |  5 +++++
 2 files changed, 14 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +9 -3
index a7ae9ffde1b5..32443bdfc594 100644
@@ -3991,6 +3991,8 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,
 		goto out;
 	}
 
+	mutex_lock(&adev->ualink.exp_lock);
+
 	if (!robj->ualink_handle_lo) {
 		/* If no ualink handle generated for BO, then generate one and
 		 * add it to the exporter Xarray.
@@ -3999,7 +4001,7 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,
 		if (!exp_xa_node) {
 			dev_err(adev->dev, "Failed to allocate exp_xa_node\n");
 			r = -ENOMEM;
-			goto out;
+			goto unlock;
 		}
 
 		amdgpu_bo_ref(robj);
@@ -4025,7 +4027,7 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,
 			dev_err(adev->dev, "Failed to generate DMABuf for the BO\n");
 			amdgpu_bo_unref(&robj);
 			kfree(exp_xa_node);
-			goto out;
+			goto unlock;
 		}
 
 		xa_lock(&adev->ualink.exp_xa);
@@ -4039,7 +4041,7 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,
 			dma_buf_put(exp_xa_node->dmabuf);
 			amdgpu_bo_unref(&robj);
 			kfree(exp_xa_node);
-			goto out;
+			goto unlock;
 		}
 
 		robj->ualink_handle_lo = handle.handle_lo;
@@ -4059,6 +4061,8 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,
 			handle_out->handle_hi = exp_xa_node->handle.handle_hi;
 	}
 
+unlock:
+	mutex_unlock(&adev->ualink.exp_lock);
 out:
 	drm_gem_object_put(gobj);
 	return r;
@@ -4139,6 +4143,7 @@ int amdgpu_ualink_manager_start(struct amdgpu_device *adev)
 	adev->mmhub.funcs->setup_vm_pt_regs(adev, adev->vm_manager.npa_vmid,
 			amdgpu_gmc_pd_addr(adev->ualink.npa_vm.root.bo));
 
+	mutex_init(&adev->ualink.exp_lock);
 	xa_init(&adev->ualink.exp_xa);
 	xa_init(&adev->ualink.imp_xa);
 	xa_init(&adev->ualink.handle_invalid_xa);
@@ -4272,6 +4277,7 @@ void amdgpu_ualink_manager_stop(struct amdgpu_device *adev)
 	xa_destroy(&adev->ualink.exp_xa);
 	xa_destroy(&adev->ualink.imp_xa);
 	xa_destroy(&adev->ualink.handle_invalid_xa);
+	mutex_destroy(&adev->ualink.exp_lock);
 
 	for (i = 0; i < AMDGPU_UALINK_ACCEL_MAX; i++)
 		mutex_destroy(&adev->ualink.conn_state[i].lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h +5 -0
index 85c9a9634a4b..2a6be8b9d5b2 100644
@@ -336,6 +336,11 @@ struct amdgpu_ualink_mgr {
 	/* handle irq from ualink client of remote GPUs */
 	struct amdgpu_irq_src irq;
 
+	/* Serializes handle allocation in amdgpu_ualink_export_handle() so
+	 * that a BO gets at most one exporter xarray node.
+	 */
+	struct mutex	exp_lock;
+
 	/* Xarray to store info about exported BOs */
 	struct xarray	exp_xa;
 
Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH 3/3] drm/amdgpu: Fix race on ualink_handle_lo in UALink handle export

Quoted 28 lines · Mukul Joshi

amdgpu_ualink_export_handle() tests robj->ualink_handle_lo and stores the generated handle into it without any lock; xa_lock only covers handle generation and the xarray insert.

Two threads can therefore both observe a zero handle for the same BO and both allocate an exporter node, take an amdgpu_bo_ref() and insert into exp_xa, with the last store to ualink_handle_lo winning. Teardown only erases the handle recorded in the BO, so the losing node keeps its BO reference and pinned VRAM until device teardown.

Serialize the check-generate-store sequence on a new per-device mutex. The BO reservation lock cannot be used, since drm_gem_prime_handle_to_dmabuf() reserves the BO itself via ttm_bo_setup_export().

Fixes 6b1a5ad90778 ("drm/amdgpu: Implement UALink handle export")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 12 +++++++++---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h |  5 +++++
  2 files changed, 14 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -0
index a7ae9ffde1b5..32443bdfc594 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp, · line 3995

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +2 -0
@@ -3991,6 +3991,8 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,
  		goto out;
  	}
 
+	mutex_lock(&adev->ualink.exp_lock);
+

The import path solves the same multi-thread race using xa_lock plus node_state. Export could follow the same approach by adding a node_state to the exp_xa node, but that requires refactoring amdgpu_ualink_export_handle(). Adding a per-adev mutex, adev->ualink.exp_lock, is simpler, at the cost of serializing all export ioctls.

Regards,
Philip

Quoted 71 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +7 -3
  	if (!robj->ualink_handle_lo) {
  		/* If no ualink handle generated for BO, then generate one and
  		 * add it to the exporter Xarray.
@@ -3999,7 +4001,7 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,
  		if (!exp_xa_node) {
  			dev_err(adev->dev, "Failed to allocate exp_xa_node\n");
  			r = -ENOMEM;
-			goto out;
+			goto unlock;
  		}
 
  		amdgpu_bo_ref(robj);
@@ -4025,7 +4027,7 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,
  			dev_err(adev->dev, "Failed to generate DMABuf for the BO\n");
  			amdgpu_bo_unref(&robj);
  			kfree(exp_xa_node);
-			goto out;
+			goto unlock;
  		}
 
  		xa_lock(&adev->ualink.exp_xa);
@@ -4039,7 +4041,7 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,
  			dma_buf_put(exp_xa_node->dmabuf);
  			amdgpu_bo_unref(&robj);
  			kfree(exp_xa_node);
-			goto out;
+			goto unlock;
  		}
 
  		robj->ualink_handle_lo = handle.handle_lo;
@@ -4059,6 +4061,8 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,
  			handle_out->handle_hi = exp_xa_node->handle.handle_hi;
  	}
 
+unlock:
+	mutex_unlock(&adev->ualink.exp_lock);
  out:
  	drm_gem_object_put(gobj);
  	return r;
@@ -4139,6 +4143,7 @@ int amdgpu_ualink_manager_start(struct amdgpu_device *adev)
  	adev->mmhub.funcs->setup_vm_pt_regs(adev, adev->vm_manager.npa_vmid,
  			amdgpu_gmc_pd_addr(adev->ualink.npa_vm.root.bo));
 
+	mutex_init(&adev->ualink.exp_lock);
  	xa_init(&adev->ualink.exp_xa);
  	xa_init(&adev->ualink.imp_xa);
  	xa_init(&adev->ualink.handle_invalid_xa);
@@ -4272,6 +4277,7 @@ void amdgpu_ualink_manager_stop(struct amdgpu_device *adev)
  	xa_destroy(&adev->ualink.exp_xa);
  	xa_destroy(&adev->ualink.imp_xa);
  	xa_destroy(&adev->ualink.handle_invalid_xa);
+	mutex_destroy(&adev->ualink.exp_lock);
 
  	for (i = 0; i < AMDGPU_UALINK_ACCEL_MAX; i++)
  		mutex_destroy(&adev->ualink.conn_state[i].lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h +5 -0
index 85c9a9634a4b..2a6be8b9d5b2 100644
@@ -336,6 +336,11 @@ struct amdgpu_ualink_mgr {
  	/* handle irq from ualink client of remote GPUs */
  	struct amdgpu_irq_src irq;
 
+	/* Serializes handle allocation in amdgpu_ualink_export_handle() so
+	 * that a BO gets at most one exporter xarray node.
+	 */
+	struct mutex	exp_lock;
+
  	/* Xarray to store info about exported BOs */
  	struct xarray	exp_xa;
 
Reviewed-by on v1 1/3
#5 lore raw
In reply to Mukul Joshi
Quoted 21 lines · Mukul Joshi

amdgpu_gem_ualink_handle_ioctl() dereferences adev->ualink.info before validating it, but that structure is only allocated when the UALink IP block is present, which is currently added only for GFX 12.1 only. The ioctl is registered for every amdgpu device with DRM_RENDER_ALLOW, so on any other ASIC a render node user can oops the kernel with a single call.

Return -EOPNOTSUPP when the UALink software state is absent.

Fixes 8a8bb5cb0410 ("drm/amdgpu: Add ioctl infra for exporting/importing UALink handles")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 7 +++++++
  1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -0
index 8411ea17172f..3d84f5a7a34d 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c int amdgpu_gem_ualink_handle_ioctl(struct drm_device *dev, void *data, · line 4080

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +6 -0
@@ -4072,6 +4072,13 @@ int amdgpu_gem_ualink_handle_ioctl(struct drm_device *dev, void *data,
  	u32 gem_handle;
  	int r, fd = -1;
 
+	/* The ioctl is registered for every amdgpu device, but the UALink
+	 * software state only exists on ASICs that instantiate the UALink IP
+	 * block.
+	 */
+	if (!adev->ualink.info)
+		return -EOPNOTSUPP;

I would fold that into the next if-condition, which also prints a helpful error message.

	if (!adev->ualink.info ||
	    adev->ualink.info->accel_state != AMDGPU_UALINK_ACCEL_STATE_ACTIVE) {
		...
	}

With that fixed, the patch is

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +1 -0
+
  	if (adev->ualink.info->accel_state !=
  	    AMDGPU_UALINK_ACCEL_STATE_ACTIVE) {
  		dev_err(adev->dev,