A GEM reference keeps the buffer alive, but it does not keep its
surfaces alive. vmw_bo_surface() walks the buffer's resource tree
without holding the buffer reservation. A concurrent surface release
can remove and free a surface while the lookup is still using its
tree node. The caller enters its RCU read-side critical section only
after the walk.

Reserve the buffer before calling vmw_bo_surface() and keep it
reserved until the lookup has tried to take a surface reference.
The reservation prevents the surface from being detached during
the lookup, and kref_get_unless_zero() rejects a surface whose
reference count has already reached zero. Assert that the caller
holds the reservation in vmw_bo_surface().

Fixes: d6667f0ddf46 ("drm/vmwgfx: Fix handling of dumb buffers")
Signed-off-by: Changyul Lee <[email protected]>
---
 drivers/gpu/drm/vmwgfx/vmwgfx_bo.c      | 2 ++
 drivers/gpu/drm/vmwgfx/vmwgfx_surface.c | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c 
b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
index a6d7156e4281..625c4b33daa2 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
@@ -864,6 +864,8 @@ struct vmw_surface *vmw_bo_surface(struct vmw_bo *vbo)
        struct vmw_surface *surf = NULL;
        struct rb_node *rb_itr = vbo->res_tree.rb_node;
 
+       dma_resv_assert_held(vbo->tbo.base.resv);
+
        if (vbo->is_dumb && vbo->dumb_surface) {
                res = &vbo->dumb_surface->res;
                goto out;
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c 
b/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c
index 3064a2a960e1..13548fdfa84c 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c
@@ -877,6 +877,7 @@ vmw_lookup_user_surface_for_buffer(struct vmw_private *vmw, 
struct vmw_bo *bo,
        struct vmw_surface *surf;
        struct ttm_base_object *base;
 
+       ttm_bo_reserve(&bo->tbo, false, false, NULL);
        surf = vmw_bo_surface(bo);
        if (surf) {
                rcu_read_lock();
@@ -891,6 +892,7 @@ vmw_lookup_user_surface_for_buffer(struct vmw_private *vmw, 
struct vmw_bo *bo,
                }
                rcu_read_unlock();
        }
+       ttm_bo_unreserve(&bo->tbo);
 
        return user_srf;
 }

base-commit: af1cf92e8617a7f7aa24b415596cbf64b76a12e7
-- 
2.43.0

Reply via email to