Skip to contentSkip to conversation

drm/virtio: Add drm_panic support

PATCH Superseded Ryosuke Yasuoka 3 participants 6 messages Nov 29, 2024 – Feb 3, 2025

Subsystems (4)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 drm/virtio: Add drm_panic support

From: Jocelyn Falempe <[email protected]>

Virtio gpu supports the drm_panic module, which displays a message to the screen when a kernel panic occurs.

---
v5:
Based on Dmitry's comment, fix the followings
- Rename virtio_panic_buffer to panic_vbuf
- Remove some unnecessary dummy ret and return directly.
- Reject if the bo is VRAM BO
- Remove virtio_gpu_panic_put_vbuf() before notify
- Add description for panic buffer allocation
- Remove virtio_gpu_panic_object_array and use virtio_gpu_panic_array_alloc() to allocate objs instead of static allocation in stack.

v4:
https://lkml.org/lkml/2024/11/13/346

- As per Dmitry's comment, make virtio_panic_buffer private to virtio_gpu_device.

v3:
https://lkml.org/lkml/2024/11/8/155

- As per Jocelyn's comment, add a finite timeout 500usec in virtio_gpu_panic_put_vbuf() to avoid infinite loop

v2:
https://lkml.org/lkml/2024/11/6/668

- Remove unnecessary virtio_gpu_vbuffer_inline
- Remove reclaim_list and just call drm_gem_object_put() if there is an obj
- Don't wait for an event in virtio_gpu_panic_queue_ctrl_sgs and just return -ENOMEM. Also add error handlers for this error.
- Use virtio_gpu_panic_queue_fenced_ctrl_buffer() in virtio_gpu_panic_cmd_resource_flush
- Remove fence and objs arguments because these are always NULL in panic handler.
- Rename virtio_gpu_panic_queue_fenced_ctrl_buffer to ..._queue_ctrl_buffer
- Rename virtio_gpu_panic_alloc_cmd to ..._panic_init_cmd

v1:
https://lkml.org/lkml/2024/10/31/154

 drivers/gpu/drm/virtio/virtgpu_drv.h   |  20 ++++
 drivers/gpu/drm/virtio/virtgpu_gem.c   |  14 +++
 drivers/gpu/drm/virtio/virtgpu_plane.c | 139 ++++++++++++++++++++++++
 drivers/gpu/drm/virtio/virtgpu_vq.c    | 143 +++++++++++++++++++++++--
 4 files changed, 310 insertions(+), 6 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +20 -0
index 64c236169db8..b82950ce75ba 100644
@@ -125,6 +125,12 @@ struct virtio_gpu_object_array {
 	struct drm_gem_object *objs[] __counted_by(total);
 };
 
+#define MAX_INLINE_CMD_SIZE   96
+#define MAX_INLINE_RESP_SIZE  24
+#define VBUFFER_SIZE	      (sizeof(struct virtio_gpu_vbuffer) \
+			      + MAX_INLINE_CMD_SIZE		 \
+			      + MAX_INLINE_RESP_SIZE)
+
 struct virtio_gpu_vbuffer;
 struct virtio_gpu_device;
 
@@ -267,6 +273,7 @@ struct virtio_gpu_device {
 	spinlock_t resource_export_lock;
 	/* protects map state and host_visible_mm */
 	spinlock_t host_visible_lock;
+	void *panic_vbuf;
 };
 
 struct virtio_gpu_fpriv {
@@ -305,6 +312,7 @@ int virtio_gpu_mode_dumb_mmap(struct drm_file *file_priv,
 			      struct drm_device *dev,
 			      uint32_t handle, uint64_t *offset_p);
 
+struct virtio_gpu_object_array *virtio_gpu_panic_array_alloc(void);
 struct virtio_gpu_object_array *virtio_gpu_array_alloc(u32 nents);
 struct virtio_gpu_object_array*
 virtio_gpu_array_from_handles(struct drm_file *drm_file, u32 *handles, u32 nents);
@@ -329,12 +337,23 @@ void virtio_gpu_cmd_create_resource(struct virtio_gpu_device *vgdev,
 				    struct virtio_gpu_fence *fence);
 void virtio_gpu_cmd_unref_resource(struct virtio_gpu_device *vgdev,
 				   struct virtio_gpu_object *bo);
+int virtio_gpu_panic_cmd_transfer_to_host_2d(struct virtio_gpu_device *vgdev,
+					     uint64_t offset,
+					     uint32_t width, uint32_t height,
+					     uint32_t x, uint32_t y,
+					     struct virtio_gpu_object_array *objs,
+					     struct virtio_gpu_vbuffer *vbuf);
 void virtio_gpu_cmd_transfer_to_host_2d(struct virtio_gpu_device *vgdev,
 					uint64_t offset,
 					uint32_t width, uint32_t height,
 					uint32_t x, uint32_t y,
 					struct virtio_gpu_object_array *objs,
 					struct virtio_gpu_fence *fence);
+int virtio_gpu_panic_cmd_resource_flush(struct virtio_gpu_device *vgdev,
+					struct virtio_gpu_vbuffer *vbuf,
+					uint32_t resource_id,
+					uint32_t x, uint32_t y,
+					uint32_t width, uint32_t height);
 void virtio_gpu_cmd_resource_flush(struct virtio_gpu_device *vgdev,
 				   uint32_t resource_id,
 				   uint32_t x, uint32_t y,
@@ -399,6 +418,7 @@ void virtio_gpu_ctrl_ack(struct virtqueue *vq);
 void virtio_gpu_cursor_ack(struct virtqueue *vq);
 void virtio_gpu_dequeue_ctrl_func(struct work_struct *work);
 void virtio_gpu_dequeue_cursor_func(struct work_struct *work);
+void virtio_gpu_panic_notify(struct virtio_gpu_device *vgdev);
 void virtio_gpu_notify(struct virtio_gpu_device *vgdev);
 
 int
drivers/gpu/drm/virtio/virtgpu_gem.c +14 -0
index 7db48d17ee3a..9f2b47222e0f 100644
@@ -161,6 +161,20 @@ void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
 	virtio_gpu_notify(vgdev);
 }
 
+/* For drm panic */
+struct virtio_gpu_object_array *virtio_gpu_panic_array_alloc(void)
+{
+	struct virtio_gpu_object_array *objs;
+
+	objs = kmalloc(sizeof(struct virtio_gpu_object_array), GFP_ATOMIC);
+	if (!objs)
+		return NULL;
+
+	objs->nents = 0;
+	objs->total = 1;
+	return objs;
+}
+
 struct virtio_gpu_object_array *virtio_gpu_array_alloc(u32 nents)
 {
 	struct virtio_gpu_object_array *objs;
drivers/gpu/drm/virtio/virtgpu_plane.c +139 -0

174 lines not on this page · raw message

drivers/gpu/drm/virtio/virtgpu_vq.c +137 -6

179 lines not on this page · raw message

Signature · 1 line
2.47.0
maintainer
#1 lore raw

On 11/29/24 15:24, Ryosuke Yasuoka wrote:
....

drivers/gpu/drm/virtio/virtgpu_plane.c static void virtio_gpu_cursor_plane_update(struct drm_plane *plane, · line 418

Quoted 11 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +11 -0
+static int virtio_drm_get_scanout_buffer(struct drm_plane *plane,
+					 struct drm_scanout_buffer *sb)
+{
+	struct virtio_gpu_object *bo;
+
+	if (!plane->state || !plane->state->fb || !plane->state->visible)
+		return -ENODEV;
+
+	bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]);
+	if (virtio_gpu_is_vram(bo))
+		return -ENODEV;

VirtIO-GPU now supports importing external dmabufs, we should reject bo->base.base.import_attach here now too.

drivers/gpu/drm/virtio/virtgpu_plane.c static void virtio_gpu_cursor_plane_update(struct drm_plane *plane, · line 426

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +8 -0
+
+	/* try to vmap it if possible */
+	if (!bo->base.vaddr) {
+		int ret;
+
+		ret = drm_gem_shmem_vmap(&bo->base, &sb->map[0]);
+		if (ret)
+			return ret;

I've now noticed that drm_gem_shmem_vmap() expects BO reservation lock to be held and we can't take lock it at a panic time.

https://elixir.bootlin.com/linux/v6.12.1/source/drivers/gpu/drm/drm_gem_shmem_helper.c#L330

This resv warning isn't triggered because bo->base.vaddr is set for VT framebufffer BO when panic happens.

We actually should reject all BOs that don't have bo->base.vaddr set at the panic time. Please correct it in v6 and rebase on top of a recent drm-next tree.

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +10 -0
+	} else {
+		iosys_map_set_vaddr(&sb->map[0], bo->base.vaddr);
+	}
+
+	sb->format = plane->state->fb->format;
+	sb->height = plane->state->fb->height;
+	sb->width = plane->state->fb->width;
+	sb->pitch[0] = plane->state->fb->pitches[0];
+	return 0;
+}

...

drivers/gpu/drm/virtio/virtgpu_plane.c static void virtio_gpu_cursor_plane_update(struct drm_plane *plane, · line 460

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +9 -0
+static void virtio_panic_flush(struct drm_plane *plane)
+{
+	struct virtio_gpu_object *bo;
+	struct drm_device *dev = plane->dev;
+	struct virtio_gpu_device *vgdev = dev->dev_private;
+	struct drm_rect rect;
+	void *p_vbuf = vgdev->panic_vbuf;
+	struct virtio_gpu_vbuffer *vbuf_dumb_bo = p_vbuf;
+	struct virtio_gpu_vbuffer *vbuf_resource_flush = p_vbuf + VBUFFER_SIZE;

This p_vbuf + VBUFFER_SIZE looks suspicious. The VBUFFER_SIZE macro isn't guarded with parentheses (), hence this vbuf_resource_flush is pointing at the CMD part of the vbuf_dumb_bo?

Won't be using kmem_cache_zalloc(vgdev->vbufs, GFP_ATOMIC) to dynamically allocate both buffers make everything cleaner?

...

Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_vq.c +0 -5
-#define MAX_INLINE_CMD_SIZE   96
-#define MAX_INLINE_RESP_SIZE  24
-#define VBUFFER_SIZE          (sizeof(struct virtio_gpu_vbuffer) \
-			       + MAX_INLINE_CMD_SIZE		 \
-			       + MAX_INLINE_RESP_SIZE)...
Signature · 2 lines
Best regards,
Dmitry
maintainer
#2 lore raw
Quoted 29 lines · Dmitry Osipenko

On 11/29/24 15:24, Ryosuke Yasuoka wrote:
....

drivers/gpu/drm/virtio/virtgpu_plane.c static void virtio_gpu_cursor_plane_update(struct drm_plane *plane, · line 418

Quoted 11 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +11 -0
+static int virtio_drm_get_scanout_buffer(struct drm_plane *plane,
+					 struct drm_scanout_buffer *sb)
+{
+	struct virtio_gpu_object *bo;
+
+	if (!plane->state || !plane->state->fb || !plane->state->visible)
+		return -ENODEV;
+
+	bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]);
+	if (virtio_gpu_is_vram(bo))
+		return -ENODEV;

VirtIO-GPU now supports importing external dmabufs, we should reject bo->base.base.import_attach here now too.

drivers/gpu/drm/virtio/virtgpu_plane.c static void virtio_gpu_cursor_plane_update(struct drm_plane *plane, · line 426

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +8 -0
+
+	/* try to vmap it if possible */
+	if (!bo->base.vaddr) {
+		int ret;
+
+		ret = drm_gem_shmem_vmap(&bo->base, &sb->map[0]);
+		if (ret)
+			return ret;

I've now noticed that drm_gem_shmem_vmap() expects BO reservation lock to be held and we can't take lock it at a panic time.

Quoted 8 lines

https://elixir.bootlin.com/linux/v6.12.1/source/drivers/gpu/drm/drm_gem_shmem_helper.c#L330

This resv warning isn't triggered because bo->base.vaddr is set for VT framebufffer BO when panic happens.

We actually should reject all BOs that don't have bo->base.vaddr set at the panic time. Please correct it in v6 and rebase on top of a recent drm-next tree.

Think ideally we need to have a pre-allocated and pre-mapped BO. Then when panic happens, use that BO and tell host to display it, i.e. not to reuse currently displayed BO. This will make panic display work in all conditions. WDYT?

Signature · 2 lines
Best regards,
Dmitry
#3 lore raw
Quoted 30 lines · Dmitry Osipenko
Quoted 29 lines · Dmitry Osipenko

On 11/29/24 15:24, Ryosuke Yasuoka wrote:
....

drivers/gpu/drm/virtio/virtgpu_plane.c static void virtio_gpu_cursor_plane_update(struct drm_plane *plane, · line 418

Quoted 11 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +11 -0
+static int virtio_drm_get_scanout_buffer(struct drm_plane *plane,
+					 struct drm_scanout_buffer *sb)
+{
+	struct virtio_gpu_object *bo;
+
+	if (!plane->state || !plane->state->fb || !plane->state->visible)
+		return -ENODEV;
+
+	bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]);
+	if (virtio_gpu_is_vram(bo))
+		return -ENODEV;

VirtIO-GPU now supports importing external dmabufs, we should reject bo->base.base.import_attach here now too.

drivers/gpu/drm/virtio/virtgpu_plane.c static void virtio_gpu_cursor_plane_update(struct drm_plane *plane, · line 426

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +8 -0
+
+	/* try to vmap it if possible */
+	if (!bo->base.vaddr) {
+		int ret;
+
+		ret = drm_gem_shmem_vmap(&bo->base, &sb->map[0]);
+		if (ret)
+			return ret;

I've now noticed that drm_gem_shmem_vmap() expects BO reservation lock to be held and we can't take lock it at a panic time.

Quoted 4 lines
Quoted 4 lines

https://elixir.bootlin.com/linux/v6.12.1/source/drivers/gpu/drm/drm_gem_shmem_helper.c#L330

This resv warning isn't triggered because bo->base.vaddr is set for VT framebufffer BO when panic happens.

I usually test using gnome desktop in the VM, and in this case vaddr is not set, and calling vmap() is needed. (and I don't get the warning, I will check why).

Quoted 4 lines
Quoted 4 lines

We actually should reject all BOs that don't have bo->base.vaddr set at the panic time. Please correct it in v6 and rebase on top of a recent drm-next tree.

The lock is there to prevent race conditions between concurrent tasks. In the panic handler, no other tasks can run in parallel. Also the buffer object is the one currently displayed, so it can't be in the middle of a free/resize or move operation. So I think it's safe to call vmap() from the panic handler.

Quoted 6 lines

Think ideally we need to have a pre-allocated and pre-mapped BO. Then when panic happens, use that BO and tell host to display it, i.e. not to reuse currently displayed BO. This will make panic display work in all conditions. WDYT?

This means a full framebuffer will be allocated only for the panic use case. For GPU with a small amount of VRAM, it's not possible. But virtio-gpu is a bit special as it uses system RAM, so it might be less problematic to do that.

An alternate solution would be to make sure the framebuffer is vmapped (at least if it's shmem) so the panic handler won't need to call vmap().

Best regards,

Signature · 7 lines
Jocelyn



-- 

Jocelyn
maintainer
#4 lore raw
Quoted 7 lines · Jocelyn Falempe
Quoted 7 lines
Quoted 7 lines

I've now noticed that drm_gem_shmem_vmap() expects BO reservation lock to be held and we can't take lock it at a panic time.

https://elixir.bootlin.com/linux/v6.12.1/source/drivers/gpu/drm/
drm_gem_shmem_helper.c#L330

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

This resv warning isn't triggered because bo->base.vaddr is set for VT framebufffer BO when panic happens.

I usually test using gnome desktop in the VM, and in this case vaddr is not set, and calling vmap() is needed. (and I don't get the warning, I will check why).

All those get_pages() and vmap() that drm_gem_shmem_vmap() uses may sleep. You'll need to add atomic variants for the vmap funcs if it's really needed.

Like I said in the comment to v6, let's start with rejecting BOs that aren't vmapped. If atomic vmap will become needed, it will be a separate problem to solve.

Signature · 2 lines
Best regards,
Dmitry
#5 lore raw
In reply to Dmitry Osipenko
Quoted 14 lines · Dmitry Osipenko

On 11/29/24 15:24, Ryosuke Yasuoka wrote:
....

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +10 -0
+     } else {
+             iosys_map_set_vaddr(&sb->map[0], bo->base.vaddr);
+     }
+
+     sb->format = plane->state->fb->format;
+     sb->height = plane->state->fb->height;
+     sb->width = plane->state->fb->width;
+     sb->pitch[0] = plane->state->fb->pitches[0];
+     return 0;
+}

...

Quoted 13 lines

drivers/gpu/drm/virtio/virtgpu_plane.c static void virtio_gpu_cursor_plane_update(struct drm_plane *plane, · line 460

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +9 -0
+static void virtio_panic_flush(struct drm_plane *plane)
+{
+     struct virtio_gpu_object *bo;
+     struct drm_device *dev = plane->dev;
+     struct virtio_gpu_device *vgdev = dev->dev_private;
+     struct drm_rect rect;
+     void *p_vbuf = vgdev->panic_vbuf;
+     struct virtio_gpu_vbuffer *vbuf_dumb_bo = p_vbuf;
+     struct virtio_gpu_vbuffer *vbuf_resource_flush = p_vbuf + VBUFFER_SIZE;

This p_vbuf + VBUFFER_SIZE looks suspicious. The VBUFFER_SIZE macro isn't guarded with parentheses (), hence this vbuf_resource_flush is pointing at the CMD part of the vbuf_dumb_bo?

No. I intend vbuf_resource_flush to point to a different vbuf than vbuf_dumb_bo. I allocate 2 vbufs in virtio_gpu_plane_init() so that vbuf_dumb_bo points to the first vbuf and vbuf_resource_flush points to the second one.

Quoted 10 lines

Won't be using kmem_cache_zalloc(vgdev->vbufs, GFP_ATOMIC) to dynamically allocate both buffers make everything cleaner?

...

drivers/gpu/drm/virtio/virtgpu_vq.c

Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_vq.c +0 -5
-#define MAX_INLINE_CMD_SIZE   96
-#define MAX_INLINE_RESP_SIZE  24
-#define VBUFFER_SIZE          (sizeof(struct virtio_gpu_vbuffer) \
-                            + MAX_INLINE_CMD_SIZE             \
-                            + MAX_INLINE_RESP_SIZE)...

Yes, it is feasible.

Thank you for your review.
Ryosuke

Quoted 3 lines

Best regards,
Dmitry