amdgpu_userq_get_doorbell_index() pins any BO passed as doorbell_handle
into the doorbell domain. For a regular BO, TTM moves it with
ttm_bo_move_null() and its contents are lost. Since an importer on the
same device gets the exporter's GEM object, a client can do this to a
buffer another client shared with it.

Reject BOs that were not created in the doorbell domain.

v2: drop the error message, use a local abo variable (Christian)

Fixes: f09c1e6077ab ("drm/amdgpu: generate doorbell index for userqueue")
Signed-off-by: Jesse Zhang <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
index 89e03d3a930c..5a922f283743 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
@@ -469,6 +469,7 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr 
*uq_mgr,
        u64 doorbell_index;
        struct drm_gem_object *gobj;
        struct amdgpu_userq_obj *db_obj = db_info->db_obj;
+       struct amdgpu_bo *abo;
        int r, db_size;
 
        gobj = drm_gem_object_lookup(filp, db_info->doorbell_handle);
@@ -477,7 +478,17 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr 
*uq_mgr,
                return -EINVAL;
        }
 
-       db_obj->obj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
+       /*
+        * Pinning a regular BO into the doorbell domain would discard its
+        * contents, possibly those of a buffer shared by another client.
+        */
+       abo = gem_to_amdgpu_bo(gobj);
+       if (!(abo->preferred_domains & AMDGPU_GEM_DOMAIN_DOORBELL)) {
+               drm_gem_object_put(gobj);
+               return -EINVAL;
+       }
+
+       db_obj->obj = amdgpu_bo_ref(abo);
        drm_gem_object_put(gobj);
 
        r = amdgpu_bo_reserve(db_obj->obj, true);
-- 
2.50.1

Reply via email to