virgl: virgl_transfer should own its virgl_resource
authorChia-I Wu <olvaffe@gmail.com>
Wed, 15 May 2019 22:38:49 +0000 (15:38 -0700)
committerChia-I Wu <olvaffe@gmail.com>
Thu, 13 Jun 2019 01:20:30 +0000 (18:20 -0700)
We should avoid having potentially dangling pointers to
pipe_resources in general.

Signed-off-by: Chia-I Wu <olvaffe@gmail.com>
Reviewed-by: Alexandros Frantzis <alexandros.frantzis@collabora.com>
src/gallium/drivers/virgl/virgl_resource.c
src/gallium/drivers/virgl/virgl_transfer_queue.c

index e840caa52a940b55521be0e654afbc8042d13903..4d427894dacf64a85b0bdf974f3fbc17612ca3e4 100644 (file)
@@ -415,7 +415,9 @@ virgl_resource_create_transfer(struct virgl_context *vctx,
    if (!trans)
       return NULL;
 
-   trans->base.resource = pres;
+   /* note that trans is not zero-initialized */
+   trans->base.resource = NULL;
+   pipe_resource_reference(&trans->base.resource, pres);
    trans->base.level = level;
    trans->base.usage = usage;
    trans->base.box = *box;
@@ -442,7 +444,9 @@ void virgl_resource_destroy_transfer(struct virgl_context *vctx,
                                      struct virgl_transfer *trans)
 {
    pipe_resource_reference(&trans->copy_src_res, NULL);
+
    util_range_destroy(&trans->range);
+   pipe_resource_reference(&trans->base.resource, NULL);
    slab_free(&vctx->transfer_pool, trans);
 }
 
index 81edfe51ffb2f49f5121e14ffd70c454816f37e6..f41c0defbbe3ec98b9e376d4f46c8448a8094c49 100644 (file)
@@ -120,9 +120,7 @@ static void remove_transfer(struct virgl_transfer_queue *queue,
                             struct list_action_args *args)
 {
    struct virgl_transfer *queued = args->queued;
-   struct pipe_resource *pres = queued->base.resource;
    list_del(&queued->queue_link);
-   pipe_resource_reference(&pres, NULL);
    virgl_resource_destroy_transfer(queue->vctx, queued);
 }
 
@@ -292,13 +290,9 @@ void virgl_transfer_queue_fini(struct virgl_transfer_queue *queue)
 int virgl_transfer_queue_unmap(struct virgl_transfer_queue *queue,
                                struct virgl_transfer *transfer)
 {
-   struct pipe_resource *res, *pres;
+   struct pipe_resource *res = transfer->base.resource;
    struct list_iteration_args iter;
 
-   pres = NULL;
-   res = transfer->base.resource;
-   pipe_resource_reference(&pres, res);
-
    /* We don't support copy transfers in the transfer queue. */
    assert(!transfer->copy_src_res);