Skip to contentSkip to conversation

drm/virtio: set seqno for dma-fence

PATCH In review Chia-I Wu 3 participants 6 messages Apr 29, 2019 – May 6, 2019

Subsystems (3)
lore
Share & tools
lore

Chia-I Wu

author reviewer
#0 lore raw

PATCH 1/3 drm/virtio: set seqno for dma-fence

This is motivated by having meaningful ftrace events, but it also fixes use cases where dma_fence_is_later is called, such as in sync_file_merge.

In other drivers, fence creation and cmdbuf submission normally happen atomically,

  mutex_lock();
  fence = dma_fence_create(..., ++timeline->seqno);
  submit_cmdbuf();
  mutex_unlock();

and have no such issue. But in our driver, because most ioctls queue commands into ctrlq, we do not want to grab a lock. Instead, we set seqno to 0 when a fence is created, and update it when the command is finally queued and the seqno is known.

---
 drivers/gpu/drm/virtio/virtgpu_drv.h   |  1 -
 drivers/gpu/drm/virtio/virtgpu_fence.c | 17 ++++++++++-------
 2 files changed, 10 insertions(+), 8 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +0 -1
index 491dec0712b3..90461feeafdb 100644
@@ -102,7 +102,6 @@ struct virtio_gpu_fence {
 	struct dma_fence f;
 	struct virtio_gpu_fence_driver *drv;
 	struct list_head node;
-	uint64_t seq;
 };
 #define to_virtio_fence(x) \
 	container_of(x, struct virtio_gpu_fence, f)
drivers/gpu/drm/virtio/virtgpu_fence.c +10 -7
index 87d1966192f4..72b4f7561432 100644
@@ -40,16 +40,14 @@ bool virtio_fence_signaled(struct dma_fence *f)
 {
 	struct virtio_gpu_fence *fence = to_virtio_fence(f);
 
-	if (atomic64_read(&fence->drv->last_seq) >= fence->seq)
+	if (atomic64_read(&fence->drv->last_seq) >= fence->f.seqno)
 		return true;
 	return false;
 }
 
 static void virtio_fence_value_str(struct dma_fence *f, char *str, int size)
 {
-	struct virtio_gpu_fence *fence = to_virtio_fence(f);
-
-	snprintf(str, size, "%llu", fence->seq);
+	snprintf(str, size, "%llu", f->seqno);
 }
 
 static void virtio_timeline_value_str(struct dma_fence *f, char *str, int size)
@@ -76,6 +74,11 @@ struct virtio_gpu_fence *virtio_gpu_fence_alloc(struct virtio_gpu_device *vgdev)
 		return fence;
 
 	fence->drv = drv;
+
+	/* This only partially initializes the fence because the seqno is
+	 * unknown yet.  The fence must not be used outside of the driver
+	 * until virtio_gpu_fence_emit is called.
+	 */
 	dma_fence_init(&fence->f, &virtio_fence_ops, &drv->lock, drv->context, 0);
 
 	return fence;
@@ -89,13 +92,13 @@ int virtio_gpu_fence_emit(struct virtio_gpu_device *vgdev,
 	unsigned long irq_flags;
 
 	spin_lock_irqsave(&drv->lock, irq_flags);
-	fence->seq = ++drv->sync_seq;
+	fence->f.seqno = ++drv->sync_seq;
 	dma_fence_get(&fence->f);
 	list_add_tail(&fence->node, &drv->fences);
 	spin_unlock_irqrestore(&drv->lock, irq_flags);
 
 	cmd_hdr->flags |= cpu_to_le32(VIRTIO_GPU_FLAG_FENCE);
-	cmd_hdr->fence_id = cpu_to_le64(fence->seq);
+	cmd_hdr->fence_id = cpu_to_le64(fence->f.seqno);
 	return 0;
 }
 
@@ -109,7 +112,7 @@ void virtio_gpu_fence_event_process(struct virtio_gpu_device *vgdev,
 	spin_lock_irqsave(&drv->lock, irq_flags);
 	atomic64_set(&vgdev->fence_drv.last_seq, last_seq);
 	list_for_each_entry_safe(fence, tmp, &drv->fences, node) {
-		if (last_seq < fence->seq)
+		if (last_seq < fence->f.seqno)
 			continue;
 		dma_fence_signal_locked(&fence->f);
 		list_del(&fence->node);
Signature · 6 lines
2.21.0.593.g511ec345e18-goog

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

PATCH 2/3 drm/virtio: trace drm_fence_emit

For most drivers, drm_fence_init is followed by drm_fence_emit immediately. But for our driver, they are done separately. We also don't know the fence seqno until drm_fence_emit.

---
 drivers/gpu/drm/virtio/virtgpu_fence.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/virtio/virtgpu_fence.c +3 -0
index 72b4f7561432..e22918ca621b 100644
@@ -24,6 +24,7 @@
  */
 
 #include <drm/drmP.h>
+#include <trace/events/dma_fence.h>
 #include "virtgpu_drv.h"
 
 static const char *virtio_get_driver_name(struct dma_fence *f)
@@ -97,6 +98,8 @@ int virtio_gpu_fence_emit(struct virtio_gpu_device *vgdev,
 	list_add_tail(&fence->node, &drv->fences);
 	spin_unlock_irqrestore(&drv->lock, irq_flags);
 
+	trace_dma_fence_emit(&fence->f);
+
 	cmd_hdr->flags |= cpu_to_le32(VIRTIO_GPU_FLAG_FENCE);
 	cmd_hdr->fence_id = cpu_to_le64(fence->f.seqno);
 	return 0;
Signature · 6 lines
2.21.0.593.g511ec345e18-goog

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

PATCH 3/3 drm/virtio: add trace events for commands

In reply to Chia-I Wu

Trace when commands are queued for both ctrlq and cursorq. Trace when responses are received for ctrlq.

---
 drivers/gpu/drm/virtio/Makefile               |  2 +-
 drivers/gpu/drm/virtio/virtgpu_trace.h        | 52 +++++++++++++++++++
 drivers/gpu/drm/virtio/virtgpu_trace_points.c |  5 ++
 drivers/gpu/drm/virtio/virtgpu_vq.c           | 10 ++++
 4 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/virtio/virtgpu_trace.h
 create mode 100644 drivers/gpu/drm/virtio/virtgpu_trace_points.c
drivers/gpu/drm/virtio/Makefile +1 -1
index 4e90cc8fa651..42949a17ff70 100644
@@ -6,6 +6,6 @@
 virtio-gpu-y := virtgpu_drv.o virtgpu_kms.o virtgpu_gem.o \
 	virtgpu_fb.o virtgpu_display.o virtgpu_vq.o virtgpu_ttm.o \
 	virtgpu_fence.o virtgpu_object.o virtgpu_debugfs.o virtgpu_plane.o \
-	virtgpu_ioctl.o virtgpu_prime.o
+	virtgpu_ioctl.o virtgpu_prime.o virtgpu_trace_points.o
 
 obj-$(CONFIG_DRM_VIRTIO_GPU) += virtio-gpu.o
drivers/gpu/drm/virtio/virtgpu_trace.h new file +52 -0

52 lines not on this page · raw message

drivers/gpu/drm/virtio/virtgpu_trace_points.c new file +5 -0
new file mode 100644
index 000000000000..1970cb6f24ef
@@ -0,0 +1,5 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "virtgpu_drv.h"
+
+#define CREATE_TRACE_POINTS
+#include "virtgpu_trace.h"
drivers/gpu/drm/virtio/virtgpu_vq.c +10 -0
index e62fe24b1a2e..2c5eeccb88c0 100644
@@ -28,6 +28,7 @@
 
 #include <drm/drmP.h>
 #include "virtgpu_drv.h"
+#include "virtgpu_trace.h"
 #include <linux/virtio.h>
 #include <linux/virtio_config.h>
 #include <linux/virtio_ring.h>
@@ -192,6 +193,9 @@ void virtio_gpu_dequeue_ctrl_func(struct work_struct *work)
 
 	list_for_each_entry_safe(entry, tmp, &reclaim_list, list) {
 		resp = (struct virtio_gpu_ctrl_hdr *)entry->resp_buf;
+
+		trace_virtio_gpu_cmd_response(vgdev->ctrlq.vq, resp);
+
 		if (resp->type != cpu_to_le32(VIRTIO_GPU_RESP_OK_NODATA)) {
 			if (resp->type >= cpu_to_le32(VIRTIO_GPU_RESP_ERR_UNSPEC)) {
 				struct virtio_gpu_ctrl_hdr *cmd;
@@ -284,6 +288,9 @@ static int virtio_gpu_queue_ctrl_buffer_locked(struct virtio_gpu_device *vgdev,
 		spin_lock(&vgdev->ctrlq.qlock);
 		goto retry;
 	} else {
+		trace_virtio_gpu_cmd_queue(vq,
+			(struct virtio_gpu_ctrl_hdr *)vbuf->buf);
+
 		virtqueue_kick(vq);
 	}
 
@@ -359,6 +366,9 @@ static int virtio_gpu_queue_cursor(struct virtio_gpu_device *vgdev,
 		spin_lock(&vgdev->cursorq.qlock);
 		goto retry;
 	} else {
+		trace_virtio_gpu_cmd_queue(vq,
+			(struct virtio_gpu_ctrl_hdr *)vbuf->buf);
+
 		virtqueue_kick(vq);
 	}
 
Signature · 6 lines
2.21.0.593.g511ec345e18-goog

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author reviewer
#3 lore raw
In reply to Chia-I Wu

(Add missing CCs)

Quoted 97 lines · Chia-I Wu

This is motivated by having meaningful ftrace events, but it also fixes use cases where dma_fence_is_later is called, such as in sync_file_merge.

In other drivers, fence creation and cmdbuf submission normally happen atomically,

  mutex_lock();
  fence = dma_fence_create(..., ++timeline->seqno);
  submit_cmdbuf();
  mutex_unlock();

and have no such issue. But in our driver, because most ioctls queue commands into ctrlq, we do not want to grab a lock. Instead, we set seqno to 0 when a fence is created, and update it when the command is finally queued and the seqno is known.

---
 drivers/gpu/drm/virtio/virtgpu_drv.h   |  1 -
 drivers/gpu/drm/virtio/virtgpu_fence.c | 17 ++++++++++-------
 2 files changed, 10 insertions(+), 8 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +0 -1
index 491dec0712b3..90461feeafdb 100644
@@ -102,7 +102,6 @@ struct virtio_gpu_fence {
        struct dma_fence f;
        struct virtio_gpu_fence_driver *drv;
        struct list_head node;
-       uint64_t seq;
 };
 #define to_virtio_fence(x) \
        container_of(x, struct virtio_gpu_fence, f)
drivers/gpu/drm/virtio/virtgpu_fence.c +10 -8
index 87d1966192f4..72b4f7561432 100644
@@ -40,16 +40,14 @@ bool virtio_fence_signaled(struct dma_fence *f)
 {
        struct virtio_gpu_fence *fence = to_virtio_fence(f);
 
-       if (atomic64_read(&fence->drv->last_seq) >= fence->seq)
+       if (atomic64_read(&fence->drv->last_seq) >= fence->f.seqno)
                return true;
        return false;
 }
 
 static void virtio_fence_value_str(struct dma_fence *f, char *str, int size)
 {
-       struct virtio_gpu_fence *fence = to_virtio_fence(f);
-
-       snprintf(str, size, "%llu", fence->seq);
+       snprintf(str, size, "%llu", f->seqno);
 }
 
 static void virtio_timeline_value_str(struct dma_fence *f, char *str, int size)
@@ -76,6 +74,11 @@ struct virtio_gpu_fence *virtio_gpu_fence_alloc(struct virtio_gpu_device *vgdev)
                return fence;
 
        fence->drv = drv;
+
+       /* This only partially initializes the fence because the seqno is
+        * unknown yet.  The fence must not be used outside of the driver
+        * until virtio_gpu_fence_emit is called.
+        */
        dma_fence_init(&fence->f, &virtio_fence_ops, &drv->lock, drv->context, 0);
 
        return fence;
@@ -89,13 +92,13 @@ int virtio_gpu_fence_emit(struct virtio_gpu_device *vgdev,
        unsigned long irq_flags;
 
        spin_lock_irqsave(&drv->lock, irq_flags);
-       fence->seq = ++drv->sync_seq;
+       fence->f.seqno = ++drv->sync_seq;
        dma_fence_get(&fence->f);
        list_add_tail(&fence->node, &drv->fences);
        spin_unlock_irqrestore(&drv->lock, irq_flags);
 
        cmd_hdr->flags |= cpu_to_le32(VIRTIO_GPU_FLAG_FENCE);
-       cmd_hdr->fence_id = cpu_to_le64(fence->seq);
+       cmd_hdr->fence_id = cpu_to_le64(fence->f.seqno);
        return 0;
 }
 
@@ -109,7 +112,7 @@ void virtio_gpu_fence_event_process(struct virtio_gpu_device *vgdev,
        spin_lock_irqsave(&drv->lock, irq_flags);
        atomic64_set(&vgdev->fence_drv.last_seq, last_seq);
        list_for_each_entry_safe(fence, tmp, &drv->fences, node) {
-               if (last_seq < fence->seq)
+               if (last_seq < fence->f.seqno)
                        continue;
                dma_fence_signal_locked(&fence->f);
                list_del(&fence->node);
--
Quoted 2 lines

2.21.0.593.g511ec345e18-goog

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Reviewed-by on v1 1/3
#4 lore raw
In reply to Chia-I Wu

Hi Chia-I,

Quoted 13 lines · Chia-I Wu

This is motivated by having meaningful ftrace events, but it also fixes use cases where dma_fence_is_later is called, such as in sync_file_merge.

In other drivers, fence creation and cmdbuf submission normally happen atomically,

  mutex_lock();
  fence = dma_fence_create(..., ++timeline->seqno);
  submit_cmdbuf();
  mutex_unlock();
Quoted 6 lines

and have no such issue. But in our driver, because most ioctls queue commands into ctrlq, we do not want to grab a lock. Instead, we set seqno to 0 when a fence is created, and update it when the command is finally queued and the seqno is known.

The series looks great. For the lot:

-Emil
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

maintainer
#5 lore raw
In reply to Chia-I Wu
Quoted 12 lines · Chia-I Wu

This is motivated by having meaningful ftrace events, but it also fixes use cases where dma_fence_is_later is called, such as in sync_file_merge.

In other drivers, fence creation and cmdbuf submission normally happen atomically,

  mutex_lock();
  fence = dma_fence_create(..., ++timeline->seqno);
  submit_cmdbuf();
  mutex_unlock();
Quoted 4 lines

and have no such issue. But in our driver, because most ioctls queue commands into ctrlq, we do not want to grab a lock. Instead, we set seqno to 0 when a fence is created, and update it when the command is finally queued and the seqno is known.

Series pushed to drm-misc-next.

thanks,
Gerd

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel