Skip to contentSkip to conversation

drm/virtio: Add drm_panic support

PATCH Superseded Ryosuke Yasuoka 2 participants 4 messages Jan 30, 2025 – Feb 4, 2025

Subsystems (4)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

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

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

---
v6:
Based on Dmitry's comment, fix the followings
- Reject external dmabufs backing the GEM object
- Allocate vbuf with kmem_cache_zalloc(..., GFP_ATOMIC) instead of drmm_kzalloc().

v5:
https://lore.kernel.org/all/CAHpthZrZ6DjsCQ4baQ80b2vOTdkR=vHDx=10W7DTS4ohxb6=pg@mail.gmail.com/

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://lore.kernel.org/all/[email protected]/

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

v3:
https://lore.kernel.org/all/[email protected]/

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

v2:
https://lore.kernel.org/all/[email protected]/

- 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://lore.kernel.org/all/[email protected]/

 drivers/gpu/drm/virtio/virtgpu_drv.h   |  17 +++
 drivers/gpu/drm/virtio/virtgpu_gem.c   |  14 +++
 drivers/gpu/drm/virtio/virtgpu_plane.c | 103 ++++++++++++++++
 drivers/gpu/drm/virtio/virtgpu_vq.c    | 157 ++++++++++++++++++++++++-
 4 files changed, 285 insertions(+), 6 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +17 -0
index f42ca9d8ed10..44511f316851 100644
@@ -126,6 +126,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;
 
@@ -310,6 +316,7 @@ int virtio_gpu_mode_dumb_create(struct drm_file *file_priv,
 				struct drm_device *dev,
 				struct drm_mode_create_dumb *args);
 
+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);
@@ -334,12 +341,21 @@ 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);
 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);
+void virtio_gpu_panic_cmd_resource_flush(struct virtio_gpu_device *vgdev,
+					 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,
@@ -408,6 +424,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 5aab588fc400..dde8fc1a3689 100644
@@ -148,6 +148,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 +103 -0

132 lines not on this page · raw message

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

205 lines not on this page · raw message

base-commit: 64179a1416e1420a34226ab3beb5f84710953d16

Signature · 1 line
2.47.1
maintainer
#1 lore raw

Hi,

On 1/30/25 12:05, Ryosuke Yasuoka wrote:
...

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +7 -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;

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

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +10 -0
+
+	bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]);
+	if (virtio_gpu_is_vram(bo) || bo->base.base.import_attach)
+		return -ENODEV;
+
+	/* try to vmap it if possible */
+	if (!bo->base.vaddr) {
+		int ret;
+
+		ret = drm_gem_shmem_vmap(&bo->base, &sb->map[0]);

drm_gem_shmem_vmap() requires BO resv to be locked, it also may invoke drm_gem_shmem_get_pages() that sleeps. This function can't be used in atomic context.

For the starter, let's reject !bo->base.vaddr BOs. Normally, shmem FB BOs are always vmapped.

Signature · 2 lines
Best regards,
Dmitry
#2 lore raw

Hi,

Quoted 27 lines · Dmitry Osipenko

Hi,

On 1/30/25 12:05, Ryosuke Yasuoka wrote:
...

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +7 -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;

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

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_plane.c +10 -0
+
+     bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]);
+     if (virtio_gpu_is_vram(bo) || bo->base.base.import_attach)
+             return -ENODEV;
+
+     /* try to vmap it if possible */
+     if (!bo->base.vaddr) {
+             int ret;
+
+             ret = drm_gem_shmem_vmap(&bo->base, &sb->map[0]);

drm_gem_shmem_vmap() requires BO resv to be locked, it also may invoke drm_gem_shmem_get_pages() that sleeps. This function can't be used in atomic context.

Quoted 2 lines

For the starter, let's reject !bo->base.vaddr BOs. Normally, shmem FB BOs are always vmapped.

OK. I'll reject !bo->base.vaddr BO in the next patch. I think this panic feature won't work when I call panic in GUI environment (I test in Gnome) without vmap(). It is OK as the first version but I think I should mention it somewhere. Where should I mention, in a commit message or in code like below?

~~~
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]);

/* Only support mapped shmem bo */
if (virtio_gpu_is_vram(bo) || bo->base.base.import_attach || !bo->base.vaddr) return -ENODEV;
~~~

Quoted 4 lines

--
Best regards,
Dmitry

Thank you for your comment.
Ryosuke

maintainer
#3 lore raw
In reply to Ryosuke Yasuoka
Quoted 5 lines · Ryosuke Yasuoka
drivers/gpu/drm/virtio/virtgpu_drv.h +0 -0
index f42ca9d8ed10..44511f316851 100644

drivers/gpu/drm/virtio/virtgpu_drv.h struct virtio_gpu_object_array { · line 134

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_drv.h +6 -0
@@ -126,6 +126,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)
+

Let's keep these definitions in the old place if it's unnecessary to move them.

Signature · 2 lines
Best regards,
Dmitry