Skip to contentSkip to conversation

drm/virtio: rework backing storage handling

PATCH In review Gerd Hoffmann 2 participants 6 messages Feb 7, 2020

Subsystems (5)
lore
Share & tools
lore

Gerd Hoffmann

author maintainer
#0 lore raw

v2 cover drm/virtio: rework backing storage handling

Gerd Hoffmann (4):
drm/virtio: simplify virtio_gpu_alloc_cmd
drm/virtio: resource teardown tweaks
drm/virtio: move mapping teardown to virtio_gpu_cleanup_object() drm/virtio: move virtio_gpu_mem_entry initialization to new function

 drivers/gpu/drm/virtio/virtgpu_drv.h    |  10 +-
 drivers/gpu/drm/virtio/virtgpu_object.c |  88 ++++++++++++--
 drivers/gpu/drm/virtio/virtgpu_vq.c     | 152 ++++++------------------
 3 files changed, 124 insertions(+), 126 deletions(-)
Signature · 6 lines
2.18.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#1 lore raw

PATCH v2 1/4 drm/virtio: simplify virtio_gpu_alloc_cmd

Just call virtio_gpu_alloc_cmd_resp with some fixed args instead of duplicating most of the function body.

---
 drivers/gpu/drm/virtio/virtgpu_vq.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/gpu/drm/virtio/virtgpu_vq.c +9 -17
index 41e475fbd67b..df499fb64ac7 100644
@@ -120,23 +120,6 @@ virtio_gpu_vbuf_ctrl_hdr(struct virtio_gpu_vbuffer *vbuf)
 	return (struct virtio_gpu_ctrl_hdr *)vbuf->buf;
 }
 
-static void *virtio_gpu_alloc_cmd(struct virtio_gpu_device *vgdev,
-				  struct virtio_gpu_vbuffer **vbuffer_p,
-				  int size)
-{
-	struct virtio_gpu_vbuffer *vbuf;
-
-	vbuf = virtio_gpu_get_vbuf(vgdev, size,
-				   sizeof(struct virtio_gpu_ctrl_hdr),
-				   NULL, NULL);
-	if (IS_ERR(vbuf)) {
-		*vbuffer_p = NULL;
-		return ERR_CAST(vbuf);
-	}
-	*vbuffer_p = vbuf;
-	return vbuf->buf;
-}
-
 static struct virtio_gpu_update_cursor*
 virtio_gpu_alloc_cursor(struct virtio_gpu_device *vgdev,
 			struct virtio_gpu_vbuffer **vbuffer_p)
@@ -172,6 +155,15 @@ static void *virtio_gpu_alloc_cmd_resp(struct virtio_gpu_device *vgdev,
 	return (struct virtio_gpu_command *)vbuf->buf;
 }
 
+static void *virtio_gpu_alloc_cmd(struct virtio_gpu_device *vgdev,
+				  struct virtio_gpu_vbuffer **vbuffer_p,
+				  int size)
+{
+	return virtio_gpu_alloc_cmd_resp(vgdev, NULL, vbuffer_p, size,
+					 sizeof(struct virtio_gpu_ctrl_hdr),
+					 NULL);
+}
+
 static void free_vbuf(struct virtio_gpu_device *vgdev,
 		      struct virtio_gpu_vbuffer *vbuf)
 {
Signature · 1 line
2.18.1
author maintainer
#2 lore raw

PATCH v2 2/4 drm/virtio: resource teardown tweaks

In reply to Gerd Hoffmann

Add new virtio_gpu_cleanup_object() helper function for object cleanup. Wire up callback function for resource unref, do cleanup from callback when we know the host stopped using the resource.

---
 drivers/gpu/drm/virtio/virtgpu_drv.h    |  4 +++-
 drivers/gpu/drm/virtio/virtgpu_object.c | 19 +++++++++++-----
 drivers/gpu/drm/virtio/virtgpu_vq.c     | 29 ++++++++++++++++++++++---
 3 files changed, 43 insertions(+), 9 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +3 -1
index 7e69c06e168e..1bc13f6b161b 100644
@@ -114,6 +114,7 @@ struct virtio_gpu_vbuffer {
 	char *resp_buf;
 	int resp_size;
 	virtio_gpu_resp_cb resp_cb;
+	void *resp_cb_data;
 
 	struct virtio_gpu_object_array *objs;
 	struct list_head list;
@@ -262,7 +263,7 @@ void virtio_gpu_cmd_create_resource(struct virtio_gpu_device *vgdev,
 				    struct virtio_gpu_object_array *objs,
 				    struct virtio_gpu_fence *fence);
 void virtio_gpu_cmd_unref_resource(struct virtio_gpu_device *vgdev,
-				   uint32_t resource_id);
+				   struct virtio_gpu_object *bo);
 void virtio_gpu_cmd_transfer_to_host_2d(struct virtio_gpu_device *vgdev,
 					uint64_t offset,
 					uint32_t width, uint32_t height,
@@ -355,6 +356,7 @@ void virtio_gpu_fence_event_process(struct virtio_gpu_device *vdev,
 				    u64 last_seq);
 
 /* virtio_gpu_object */
+void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo);
 struct drm_gem_object *virtio_gpu_create_object(struct drm_device *dev,
 						size_t size);
 int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
drivers/gpu/drm/virtio/virtgpu_object.c +14 -5
index 017a9e0fc3bb..28a161af7503 100644
@@ -61,6 +61,14 @@ static void virtio_gpu_resource_id_put(struct virtio_gpu_device *vgdev, uint32_t
 	}
 }
 
+void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)
+{
+	struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;
+
+	virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle);
+	drm_gem_shmem_free_object(&bo->base.base);
+}
+
 static void virtio_gpu_free_object(struct drm_gem_object *obj)
 {
 	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);
@@ -68,11 +76,12 @@ static void virtio_gpu_free_object(struct drm_gem_object *obj)
 
 	if (bo->pages)
 		virtio_gpu_object_detach(vgdev, bo);
-	if (bo->created)
-		virtio_gpu_cmd_unref_resource(vgdev, bo->hw_res_handle);
-	virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle);
-
-	drm_gem_shmem_free_object(obj);
+	if (bo->created) {
+		virtio_gpu_cmd_unref_resource(vgdev, bo);
+		/* completion handler calls virtio_gpu_cleanup_object() */
+		return;
+	}
+	virtio_gpu_cleanup_object(bo);
 }
 
 static const struct drm_gem_object_funcs virtio_gpu_gem_funcs = {
drivers/gpu/drm/virtio/virtgpu_vq.c +26 -3
index df499fb64ac7..4e22c3914f94 100644
@@ -164,6 +164,16 @@ static void *virtio_gpu_alloc_cmd(struct virtio_gpu_device *vgdev,
 					 NULL);
 }
 
+static void *virtio_gpu_alloc_cmd_cb(struct virtio_gpu_device *vgdev,
+				     struct virtio_gpu_vbuffer **vbuffer_p,
+				     int size,
+				     virtio_gpu_resp_cb cb)
+{
+	return virtio_gpu_alloc_cmd_resp(vgdev, cb, vbuffer_p, size,
+					 sizeof(struct virtio_gpu_ctrl_hdr),
+					 NULL);
+}
+
 static void free_vbuf(struct virtio_gpu_device *vgdev,
 		      struct virtio_gpu_vbuffer *vbuf)
 {
@@ -507,18 +517,31 @@ void virtio_gpu_cmd_create_resource(struct virtio_gpu_device *vgdev,
 	bo->created = true;
 }
 
+static void virtio_gpu_cmd_unref_cb(struct virtio_gpu_device *vgdev,
+				    struct virtio_gpu_vbuffer *vbuf)
+{
+	struct virtio_gpu_object *bo;
+
+	bo = vbuf->resp_cb_data;
+	vbuf->resp_cb_data = NULL;
+
+	virtio_gpu_cleanup_object(bo);
+}
+
 void virtio_gpu_cmd_unref_resource(struct virtio_gpu_device *vgdev,
-				   uint32_t resource_id)
+				   struct virtio_gpu_object *bo)
 {
 	struct virtio_gpu_resource_unref *cmd_p;
 	struct virtio_gpu_vbuffer *vbuf;
 
-	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
+	cmd_p = virtio_gpu_alloc_cmd_cb(vgdev, &vbuf, sizeof(*cmd_p),
+					virtio_gpu_cmd_unref_cb);
 	memset(cmd_p, 0, sizeof(*cmd_p));
 
 	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_UNREF);
-	cmd_p->resource_id = cpu_to_le32(resource_id);
+	cmd_p->resource_id = cpu_to_le32(bo->hw_res_handle);
 
+	vbuf->resp_cb_data = bo;
 	virtio_gpu_queue_ctrl_buffer(vgdev, vbuf);
 }
 
Signature · 1 line
2.18.1
author maintainer
#3 lore raw

PATCH v2 3/4 drm/virtio: move mapping teardown to virtio_gpu_cleanup_object()

In reply to Gerd Hoffmann

Stop sending DETACH_BACKING commands, that will happening anyway when releasing resources via UNREF. Handle guest-side cleanup in virtio_gpu_cleanup_object(), called when the host finished processing the UNREF command.

---
 drivers/gpu/drm/virtio/virtgpu_drv.h    |  2 --
 drivers/gpu/drm/virtio/virtgpu_object.c | 14 ++++++--
 drivers/gpu/drm/virtio/virtgpu_vq.c     | 46 -------------------------
 3 files changed, 12 insertions(+), 50 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +0 -2
index 1bc13f6b161b..d37ddd7644f6 100644
@@ -281,8 +281,6 @@ void virtio_gpu_cmd_set_scanout(struct virtio_gpu_device *vgdev,
 int virtio_gpu_object_attach(struct virtio_gpu_device *vgdev,
 			     struct virtio_gpu_object *obj,
 			     struct virtio_gpu_fence *fence);
-void virtio_gpu_object_detach(struct virtio_gpu_device *vgdev,
-			      struct virtio_gpu_object *obj);
 int virtio_gpu_attach_status_page(struct virtio_gpu_device *vgdev);
 int virtio_gpu_detach_status_page(struct virtio_gpu_device *vgdev);
 void virtio_gpu_cursor_ping(struct virtio_gpu_device *vgdev,
drivers/gpu/drm/virtio/virtgpu_object.c +12 -2
index 28a161af7503..bce2b3d843fe 100644
@@ -23,6 +23,7 @@
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#include <linux/dma-mapping.h>
 #include <linux/moduleparam.h>
 
 #include "virtgpu_drv.h"
@@ -65,6 +66,17 @@ void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)
 {
 	struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;
 
+	if (bo->pages) {
+		if (bo->mapped) {
+			dma_unmap_sg(vgdev->vdev->dev.parent,
+				     bo->pages->sgl, bo->mapped,
+				     DMA_TO_DEVICE);
+			bo->mapped = 0;
+		}
+		sg_free_table(bo->pages);
+		bo->pages = NULL;
+		drm_gem_shmem_unpin(&bo->base.base);
+	}
 	virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle);
 	drm_gem_shmem_free_object(&bo->base.base);
 }
@@ -74,8 +86,6 @@ static void virtio_gpu_free_object(struct drm_gem_object *obj)
 	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);
 	struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;
 
-	if (bo->pages)
-		virtio_gpu_object_detach(vgdev, bo);
 	if (bo->created) {
 		virtio_gpu_cmd_unref_resource(vgdev, bo);
 		/* completion handler calls virtio_gpu_cleanup_object() */
drivers/gpu/drm/virtio/virtgpu_vq.c +0 -46

58 lines not on this page · raw message

Signature · 1 line
2.18.1
author maintainer
#4 lore raw

PATCH v2 4/4 drm/virtio: move virtio_gpu_mem_entry initialization to new function

In reply to Gerd Hoffmann

Introduce new virtio_gpu_object_shmem_init() helper function which will create the virtio_gpu_mem_entry array, containing the backing storage information for the host. For the most path this just moves code from virtio_gpu_object_attach().

---
 drivers/gpu/drm/virtio/virtgpu_drv.h    |  4 +-
 drivers/gpu/drm/virtio/virtgpu_object.c | 55 ++++++++++++++++++++++++-
 drivers/gpu/drm/virtio/virtgpu_vq.c     | 51 ++---------------------
 3 files changed, 60 insertions(+), 50 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +3 -1
index d37ddd7644f6..6c78c77a2afc 100644
@@ -71,6 +71,7 @@ struct virtio_gpu_object {
 
 	struct sg_table *pages;
 	uint32_t mapped;
+
 	bool dumb;
 	bool created;
 };
@@ -280,7 +281,8 @@ void virtio_gpu_cmd_set_scanout(struct virtio_gpu_device *vgdev,
 				uint32_t x, uint32_t y);
 int virtio_gpu_object_attach(struct virtio_gpu_device *vgdev,
 			     struct virtio_gpu_object *obj,
-			     struct virtio_gpu_fence *fence);
+			     struct virtio_gpu_mem_entry *ents,
+			     unsigned int nents);
 int virtio_gpu_attach_status_page(struct virtio_gpu_device *vgdev);
 int virtio_gpu_detach_status_page(struct virtio_gpu_device *vgdev);
 void virtio_gpu_cursor_ping(struct virtio_gpu_device *vgdev,
drivers/gpu/drm/virtio/virtgpu_object.c +54 -1

73 lines not on this page · raw message

drivers/gpu/drm/virtio/virtgpu_vq.c +3 -48

59 lines not on this page · raw message

Signature · 1 line
2.18.1
reviewer Reviewed-by on v2 4/4
#5 lore raw

Re: [PATCH v2 4/4] drm/virtio: move virtio_gpu_mem_entry initialization to new function

Quoted 120 lines · Gerd Hoffmann

Introduce new virtio_gpu_object_shmem_init() helper function which will create the virtio_gpu_mem_entry array, containing the backing storage information for the host. For the most path this just moves code from virtio_gpu_object_attach().

---
 drivers/gpu/drm/virtio/virtgpu_drv.h    |  4 +-
 drivers/gpu/drm/virtio/virtgpu_object.c | 55 ++++++++++++++++++++++++-
 drivers/gpu/drm/virtio/virtgpu_vq.c     | 51 ++---------------------
 3 files changed, 60 insertions(+), 50 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +3 -1
index d37ddd7644f6..6c78c77a2afc 100644
@@ -71,6 +71,7 @@ struct virtio_gpu_object {
 
        struct sg_table *pages;
        uint32_t mapped;
+
        bool dumb;
        bool created;
 };
@@ -280,7 +281,8 @@ void virtio_gpu_cmd_set_scanout(struct virtio_gpu_device *vgdev,
                                uint32_t x, uint32_t y);
 int virtio_gpu_object_attach(struct virtio_gpu_device *vgdev,
                             struct virtio_gpu_object *obj,
-                            struct virtio_gpu_fence *fence);
+                            struct virtio_gpu_mem_entry *ents,
+                            unsigned int nents);
 int virtio_gpu_attach_status_page(struct virtio_gpu_device *vgdev);
 int virtio_gpu_detach_status_page(struct virtio_gpu_device *vgdev);
 void virtio_gpu_cursor_ping(struct virtio_gpu_device *vgdev,
drivers/gpu/drm/virtio/virtgpu_object.c +54 -1
index bce2b3d843fe..8870ee23ff2b 100644
@@ -121,6 +121,51 @@ struct drm_gem_object *virtio_gpu_create_object(struct drm_device *dev,
        return &bo->base.base;
 }
 
+static int virtio_gpu_object_shmem_init(struct virtio_gpu_device *vgdev,
+                                       struct virtio_gpu_object *bo,
+                                       struct virtio_gpu_mem_entry **ents,
+                                       unsigned int *nents)
+{
+       bool use_dma_api = !virtio_has_iommu_quirk(vgdev->vdev);
+       struct scatterlist *sg;
+       int si, ret;
+
+       ret = drm_gem_shmem_pin(&bo->base.base);
+       if (ret < 0)
+               return -EINVAL;
+
+       bo->pages = drm_gem_shmem_get_sg_table(&bo->base.base);
+       if (!bo->pages) {
+               drm_gem_shmem_unpin(&bo->base.base);
+               return -EINVAL;
+       }
+
+       if (use_dma_api) {
+               bo->mapped = dma_map_sg(vgdev->vdev->dev.parent,
+                                       bo->pages->sgl, bo->pages->nents,
+                                       DMA_TO_DEVICE);
+               *nents = bo->mapped;
+       } else {
+               *nents = bo->pages->nents;
+       }
+
+       *ents = kmalloc_array(*nents, sizeof(struct virtio_gpu_mem_entry),
+                             GFP_KERNEL);
+       if (!(*ents)) {
+               DRM_ERROR("failed to allocate ent list\n");
+               return -ENOMEM;
+       }
+
+       for_each_sg(bo->pages->sgl, sg, *nents, si) {
+               (*ents)[si].addr = cpu_to_le64(use_dma_api
+                                              ? sg_dma_address(sg)
+                                              : sg_phys(sg));
+               (*ents)[si].length = cpu_to_le32(sg->length);
+               (*ents)[si].padding = 0;
+       }
+       return 0;
+}
+
 int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
                             struct virtio_gpu_object_params *params,
                             struct virtio_gpu_object **bo_ptr,
@@ -129,6 +174,8 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
        struct virtio_gpu_object_array *objs = NULL;
        struct drm_gem_shmem_object *shmem_obj;
        struct virtio_gpu_object *bo;
+       struct virtio_gpu_mem_entry *ents;
+       unsigned int nents;
        int ret;
 
        *bo_ptr = NULL;
@@ -165,7 +212,13 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
                                               objs, fence);
        }
 
-       ret = virtio_gpu_object_attach(vgdev, bo, NULL);
+       ret = virtio_gpu_object_shmem_init(vgdev, bo, &ents, &nents);
+       if (ret != 0) {
+               virtio_gpu_free_object(&shmem_obj->base);
+               return ret;
+       }
+
+       ret = virtio_gpu_object_attach(vgdev, bo, ents, nents);
        if (ret != 0) {
                virtio_gpu_free_object(&shmem_obj->base);
                return ret;
drivers/gpu/drm/virtio/virtgpu_vq.c +0 -0
index 87c439156151..8360f7338209 100644

drivers/gpu/drm/virtio/virtgpu_vq.c void virtio_gpu_cmd_submit(struct virtio_gpu_device *vgdev, · line 1087

Quoted 2 lines
drivers/gpu/drm/virtio/virtgpu_vq.c +0 -0
@@ -1086,56 +1086,11 @@ void virtio_gpu_cmd_submit(struct virtio_gpu_device *vgdev,
 
 int virtio_gpu_object_attach(struct virtio_gpu_device *vgdev,

nit: why do we keep this wrapper function?

Series is

Quoted 60 lines
drivers/gpu/drm/virtio/virtgpu_vq.c +3 -49
                             struct virtio_gpu_object *obj,
-                            struct virtio_gpu_fence *fence)
+                            struct virtio_gpu_mem_entry *ents,
+                            unsigned int nents)
 {
-       bool use_dma_api = !virtio_has_iommu_quirk(vgdev->vdev);
-       struct virtio_gpu_mem_entry *ents;
-       struct scatterlist *sg;
-       int si, nents, ret;
-
-       if (WARN_ON_ONCE(!obj->created))
-               return -EINVAL;
-       if (WARN_ON_ONCE(obj->pages))
-               return -EINVAL;
-
-       ret = drm_gem_shmem_pin(&obj->base.base);
-       if (ret < 0)
-               return -EINVAL;
-
-       obj->pages = drm_gem_shmem_get_sg_table(&obj->base.base);
-       if (obj->pages == NULL) {
-               drm_gem_shmem_unpin(&obj->base.base);
-               return -EINVAL;
-       }
-
-       if (use_dma_api) {
-               obj->mapped = dma_map_sg(vgdev->vdev->dev.parent,
-                                        obj->pages->sgl, obj->pages->nents,
-                                        DMA_TO_DEVICE);
-               nents = obj->mapped;
-       } else {
-               nents = obj->pages->nents;
-       }
-
-       /* gets freed when the ring has consumed it */
-       ents = kmalloc_array(nents, sizeof(struct virtio_gpu_mem_entry),
-                            GFP_KERNEL);
-       if (!ents) {
-               DRM_ERROR("failed to allocate ent list\n");
-               return -ENOMEM;
-       }
-
-       for_each_sg(obj->pages->sgl, sg, nents, si) {
-               ents[si].addr = cpu_to_le64(use_dma_api
-                                           ? sg_dma_address(sg)
-                                           : sg_phys(sg));
-               ents[si].length = cpu_to_le32(sg->length);
-               ents[si].padding = 0;
-       }
-
        virtio_gpu_cmd_resource_attach_backing(vgdev, obj->hw_res_handle,
-                                              ents, nents,
-                                              fence);
+                                              ents, nents, NULL);
        return 0;
 }
 
--

2.18.1