All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Aditya Tipre" <tipreaditya@gmail.com>, dri-devel@lists.freedesktop.org
Cc: virtualization@lists.linux.dev,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [RFC PATCH 1/2] drm/virtio: accumulate confirmed dumb-buffer damage
Date: Sat, 05 Dec 2026 06:59:23 +0000	[thread overview]
Message-ID: <sashiko-outbox-251175@kernel.org> (raw)
In-Reply-To: <20261205063321.56168-2-tipreaditya@gmail.com>

Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [High] drm/virtio: shared fence context breaks transfer fence ordering
  and isolation
- [High] drm/virtio: synchronous BO put in free_vbuf() deadlocks ctrlq
  dequeue_work
- [Medium] drm/virtio: same-FB partial damage regresses to full uploads
  when tainted
- [Medium] drm/virtio: virtio_gpu_transfer_reply() omits
  ERR_INVALID_PARAMETER
- [Medium] drm/virtio: cursor tainting resets global damage_epoch and
  defeats buffer_age
- [Medium] drm/virtio: empty-damage tracked commits trigger full flush
  and DMA sync
- [Medium] drm/virtio: skipping resp_cb on bad used_len leaks BOs and
  hangs waiters
- [Low] drm/virtio: virtgpu_restore() unrefs missing host resources
  after hibernation
- [Low] drm/virtio: virtio_gpu_plane_atomic_check() merges damage clips
  when unused
--

commit 492f9808c0eea48a3f2f0413632f1015bbd8c079
Author: Aditya Tipre <tipreaditya@gmail.com>

drm/virtio: accumulate confirmed dumb-buffer damage

This commit adds opt-in damage history tracking for single-scanout linear
XRGB8888 shmem dumb buffers when virgl is disabled, confirming uploads via
fenced transfer responses so rotating buffers can upload only accumulated
damage.

> diff --git a/drivers/gpu/drm/virtio/virtgpu_damage.c b/drivers/gpu/drm/virtio/virtgpu_damage.c
> new file mode 100644
> index 0000000000000..1bbc4bde1eda5
> --- /dev/null
> +++ b/drivers/gpu/drm/virtio/virtgpu_damage.c

[ ... ]

> +/* Final gate, after every queue-space wait and before descriptor insertion. */
> +bool virtio_gpu_damage_plan(struct virtio_gpu_device *vgdev,
> +			    struct virtio_gpu_transfer *transfer)
> +{
> +	struct virtio_gpu_object *bo = transfer->bo;
> +	struct virtio_gpu_damage_stamp *stamp = &bo->damage_stamp;
> +	struct virtio_gpu_damage_history *history = &vgdev->outputs[transfer->output].damage;
> +	u64 sequence, age;
> +	u32 i;
> +
> +	lockdep_assert_held(&vgdev->ctrlq.qlock);
> +	transfer->upload = transfer->full;
> +	transfer->stamp.valid = vgdev->damage_enabled && !vgdev->damage_broken &&
> +		!vgdev->transfers_stopping && !bo->damage_tainted && history->valid &&
> +		virtio_gpu_damage_cookie_matches(vgdev, history, &transfer->stamp.cookie);
> +	if (bo->damage_version == U64_MAX) {
> +		vgdev->damage_broken = true;
> +		transfer->stamp.valid = false;
> +	}
> +	/* Overlapping full writes cannot establish an execution order either. */
> +	if (bo->damage_pending)
> +		transfer->stamp.valid = false;
> +	if (!transfer->stamp.valid || vgdev->transfer_count[transfer->output] ||
> +	    bo->damage_pending || !stamp->valid || stamp->output != transfer->output ||
> +	    stamp->cookie.device_epoch != vgdev->damage_epoch ||
> +	    stamp->cookie.epoch != history->epoch || stamp->version != bo->damage_version ||
> +	    stamp->cookie.sequence > history->sequence)
> +		return true;

[Severity: Medium]
Does this regress same-framebuffer partial damage to full-framebuffer
uploads whenever a buffer object is tainted or lacks a confirmed stamp?

When a buffer object is tainted (for example by dma-buf export or a second
GEM_OPEN) or when a cursor update or overlapping transfer invalidates its
stamp, virtio_gpu_damage_eligible() still admits it to the tracked path.
virtio_gpu_damage_plan() then defaults transfer->upload to transfer->full
(and virtio_gpu_update_damage() does the same on fallback), forcing a
full-screen upload and flush even for same-framebuffer commits with small
FB_DAMAGE_CLIPS or DIRTYFB rectangles.  Excluding tainted objects from
virtio_gpu_damage_eligible() or preserving same-framebuffer partial damage
on fallback would avoid this regression.

[ ... ]

> diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.c b/drivers/gpu/drm/virtio/virtgpu_drv.c
> index 52c174f9bff7f..7b28853eb64b0 100644
> --- a/drivers/gpu/drm/virtio/virtgpu_drv.c
> +++ b/drivers/gpu/drm/virtio/virtgpu_drv.c

[ ... ]

> @@ -251,6 +256,9 @@ static int virtgpu_restore(struct virtio_device *vdev)
>  	}
>  
>  	virtio_device_ready(vdev);
> +	/* Quarantine refs may release a BO and queue its resource unref. */
> +	virtio_gpu_transfer_cancel_all(vgdev);
> +	virtio_gpu_transfer_start(vgdev);
>  
>  	virtgpu_hibernation_restore(vgdev);

[Severity: Low]
Can virtio_gpu_transfer_cancel_all() queue VIRTIO_GPU_CMD_RESOURCE_UNREF
for host resources that do not exist yet after hibernation?

If a quarantined transfer holds the last gem reference on a buffer object
when virtgpu_freeze() runs, virtio_gpu_object_unref_all() unrefs the host
resource before reset while leaving bo->created set.  Calling
virtio_gpu_transfer_cancel_all() before virtgpu_hibernation_restore() then
drops that final reference in virtio_gpu_free_object(), which removes bo
from obj_restore_list and sends VIRTIO_GPU_CMD_RESOURCE_UNREF to the
resumed host.  The host rejects it with
VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID, logging an error and resetting
damage tracking after virtio_gpu_transfer_start().

[ ... ]

> diff --git a/drivers/gpu/drm/virtio/virtgpu_plane.c b/drivers/gpu/drm/virtio/virtgpu_plane.c
> index 20aa3d108ecfa..ad77686758430 100644
> --- a/drivers/gpu/drm/virtio/virtgpu_plane.c
> +++ b/drivers/gpu/drm/virtio/virtgpu_plane.c

[ ... ]

> @@ -135,6 +157,21 @@ static int virtio_gpu_plane_atomic_check(struct drm_plane *plane,
>  						  DRM_PLANE_NO_SCALING,
>  						  DRM_PLANE_NO_SCALING,
>  						  is_cursor, true);
> +	if (!ret && !is_cursor) {
> +		struct virtio_gpu_plane_state *vgstate = to_virtio_gpu_plane_state(new_plane_state);
> +		struct virtio_gpu_device *vgdev = plane->dev->dev_private;
> +		bool ignore = new_plane_state->ignore_damage_clips;
> +
> +		vgstate->damage_eligible = virtio_gpu_damage_eligible(vgdev, new_plane_state) &&
> +			new_plane_state->crtc_w == crtc_state->mode.hdisplay &&
> +			new_plane_state->crtc_h == crtc_state->mode.vdisplay;
> +		/* Save frame damage before the per-resource FB-switch fallback. */
> +		new_plane_state->ignore_damage_clips = false;
> +		if (!drm_atomic_helper_damage_merged(old_plane_state, new_plane_state,
> +						    &vgstate->frame_damage))
> +			vgstate->frame_damage = (struct drm_rect) {};
> +		new_plane_state->ignore_damage_clips = ignore;
> +	}
>  	return ret;
>  }

[Severity: Low]
Why is drm_atomic_helper_damage_merged() called unconditionally here when
vgstate->damage_eligible is false?

On the default buffer_age=0 path and on ineligible planes, this walks
fb_damage_clips only for virtio_gpu_primary_plane_update() to discard
vgstate->frame_damage and run drm_atomic_helper_damage_merged() a second
time.  Gating the merge on vgstate->damage_eligible avoids the redundant
work on every primary-plane commit.

[ ... ]

> @@ -268,12 +340,25 @@ static void virtio_gpu_primary_plane_update(struct drm_plane *plane,
>  		return;
>  	}
>  
> -	if (!drm_atomic_helper_damage_merged(old_state, plane->state, &rect))
> -		return;
> -
>  	bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]);
> -	if (bo->dumb)
> -		virtio_gpu_update_dumb_bo(vgdev, plane->state, &rect);
> +	crtc_state = drm_atomic_get_new_crtc_state(state, plane->state->crtc);
> +	tracked = virtio_gpu_damage_begin(vgdev, output->index, vgstate,
> +		to_virtio_gpu_plane_state(old_state),
> +		crtc_state && drm_atomic_crtc_needs_modeset(crtc_state));
> +	if (tracked) {
> +		if (!virtio_gpu_update_damage(vgdev, vgstate, &rect))
> +			virtio_gpu_update_dumb_bo(vgdev, plane->state, &rect);
> +		/* A binding change still needs SET_SCANOUT and a valid flush. */
> +		if (!drm_rect_visible(&rect))
> +			rect = DRM_RECT_INIT(0, 0, plane->state->fb->width,
> +					     plane->state->fb->height);
> +	} else {
> +		virtio_gpu_damage_invalidate(vgdev, output->index);
> +		if (!drm_atomic_helper_damage_merged(old_state, plane->state, &rect))
> +			return;
> +		if (bo->dumb)
> +			virtio_gpu_update_dumb_bo(vgdev, plane->state, &rect);
> +	}

[Severity: Medium]
Should an empty rect on a same-framebuffer commit skip the flush instead
of expanding to the full framebuffer?

When virtio_gpu_damage_plan() returns false for an empty-damage
same-framebuffer commit, this unconditionally expands rect to full-screen
dimensions and falls through to virtio_gpu_resource_flush(), even when no
scanout binding change or modeset occurred.  At baseline and on the
untracked branch, empty-damage same-framebuffer commits return early
without a host flush.

In addition, virtio_gpu_transfer_to_host_2d() runs
dma_sync_sgtable_for_device() over the entire framebuffer before
virtio_gpu_queue_ctrl_sgs() calls virtio_gpu_damage_plan() and decides no
copy is needed.

[ ... ]

> @@ -457,6 +547,7 @@ static void virtio_gpu_cursor_plane_update(struct drm_plane *plane,
>  		vgfb = to_virtio_gpu_framebuffer(plane->state->fb);
>  		vgplane_st = to_virtio_gpu_plane_state(plane->state);
>  		bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
> +		virtio_gpu_damage_taint(vgdev, bo);
>  		handle = bo->hw_res_handle;
>  	} else {
>  		handle = 0;

[Severity: Medium]
Does tainting the cursor buffer object on every cursor update defeat
buffer_age on interactive desktops?

virtio_gpu_cursor_plane_update() calls virtio_gpu_damage_taint() on every
cursor commit, including pure cursor moves where the cursor framebuffer
has not changed.  Because virtio_gpu_damage_taint_locked() calls
virtio_gpu_damage_reset_locked(), every cursor move acquires ctrlq.qlock,
bumps vgdev->damage_epoch, and invalidates all scanout histories; in
combined primary and cursor commits the cursor update runs after the
primary update and immediately invalidates the primary transfer's stamp.

Similarly, a transient -EBUSY or allocation failure in
virtio_gpu_update_damage() calls virtio_gpu_damage_taint(), permanently
tainting the primary buffer object and resetting global damage history.

[ ... ]

> diff --git a/drivers/gpu/drm/virtio/virtgpu_transfer.c b/drivers/gpu/drm/virtio/virtgpu_transfer.c
> new file mode 100644
> index 0000000000000..1e71e3e480c94
> --- /dev/null
> +++ b/drivers/gpu/drm/virtio/virtgpu_transfer.c

[ ... ]

> +struct virtio_gpu_transfer *
> +virtio_gpu_transfer_alloc(struct virtio_gpu_device *vgdev,
> +			  struct virtio_gpu_object *bo, u32 output)
> +{
> +	struct virtio_gpu_transfer *transfer;
> +
> +	if (output >= VIRTIO_GPU_MAX_SCANOUTS)
> +		return NULL;
> +	transfer = kzalloc_obj(*transfer);
> +	if (!transfer)
> +		return NULL;
> +	transfer->fence = virtio_gpu_fence_alloc(vgdev, vgdev->fence_drv.context, 0);

[Severity: High]
Does placing transfer->fence on the shared vgdev->fence_drv.context break
dma_fence ordering and transfer response isolation?

Tracked transfers attach transfer->fence to bo->resv via
virtio_gpu_array_add_fence(), and dma_resv_add_fence() replaces any
earlier fence on the same context because dma_fence requires monotonic
signaling order within a context.  Two regressions follow:

First, if a tracked transfer F2 replaces an earlier in-flight command F1
in bo->resv and F2 is later cancelled individually by
virtio_gpu_fence_cancel(), bo->resv sees all fences as signaled while F1
is still executing on the host.

Second, if any later fenced ctrlq command on vgdev->fence_drv.context
completes before an earlier transfer T1 is dequeued,
virtio_gpu_fence_event_process() signals T1->fence with error 0 and
removes it from drv->fences.  Waiters on bo->resv proceed as if T1
succeeded, and a subsequent virtio_gpu_fence_cancel() on an error or
unmatched reply silently drops the -EIO or -ENODEV error.  Giving
independently cancellable transfers their own dma_fence context would
avoid both violations.

[ ... ]

> +	/* Never infer this command's completion from a later generic fence. */
> +	if (length == sizeof(*response) &&
> +	    (response->flags & cpu_to_le32(VIRTIO_GPU_FLAG_FENCE)) &&
> +	    le64_to_cpu(response->fence_id) == transfer->fence_id) {
> +		type = le32_to_cpu(response->type);
> +		matched = type == VIRTIO_GPU_RESP_OK_NODATA ||
> +			  (type >= VIRTIO_GPU_RESP_ERR_UNSPEC &&
> +			   type <= VIRTIO_GPU_RESP_ERR_INVALID_CONTEXT_ID);
> +	}

[Severity: Medium]
Should the upper error bound include VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER?

In include/uapi/linux/virtio_gpu.h, VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER
follows VIRTIO_GPU_RESP_ERR_INVALID_CONTEXT_ID.  Stopping at
VIRTIO_GPU_RESP_ERR_INVALID_CONTEXT_ID misclassifies a valid
ERR_INVALID_PARAMETER reply as unmatched, leaving the transfer in
VIRTIO_GPU_TRANSFER_UNCERTAIN with vgdev->transfer_count[output] and
bo->damage_pending leaked until device reset, which disables incremental
uploads on that output.

[ ... ]

> diff --git a/drivers/gpu/drm/virtio/virtgpu_vq.c b/drivers/gpu/drm/virtio/virtgpu_vq.c
> index 21271966b05cd..2f8d018554ca7 100644
> --- a/drivers/gpu/drm/virtio/virtgpu_vq.c
> +++ b/drivers/gpu/drm/virtio/virtgpu_vq.c

[ ... ]

> @@ -202,6 +203,7 @@ static void *virtio_gpu_alloc_cmd_cb(struct virtio_gpu_device *vgdev,
>  static void free_vbuf(struct virtio_gpu_device *vgdev,
>  		      struct virtio_gpu_vbuffer *vbuf)
>  {
> +	virtio_gpu_transfer_put(vbuf->transfer);
>  	if (vbuf->resp_size > MAX_INLINE_RESP_SIZE)
>  		kfree(vbuf->resp_buf);
>  	kvfree(vbuf->data_buf);

[Severity: High]
Can calling virtio_gpu_transfer_put() synchronously in free_vbuf()
deadlock ctrlq.dequeue_work?

virtio_gpu_transfer_release() drops transfer->bo synchronously via
drm_gem_object_put().  In virtio_gpu_dequeue_ctrl_func(), ctrlq.ack_queue
is woken and entry->objs is handed to obj_free_work before free_vbuf()
runs.  If userspace has already closed the buffer object and obj_free_work
drops the objs reference first, free_vbuf() drops the final gem reference
directly on ctrlq.dequeue_work:

ctrlq.dequeue_work                obj_free_work / submitter
virtio_gpu_dequeue_ctrl_func()
  wake_up(&ctrlq.ack_queue)
                                  (submitter refills ctrlq.vq)
  virtio_gpu_array_put_free_delayed()
                                  virtio_gpu_array_put_free_work()
                                    drm_gem_object_put(bo)
  free_vbuf()
    virtio_gpu_transfer_put()
      drm_gem_object_put(bo)
        virtio_gpu_free_object()
          virtio_gpu_cmd_unref_resource()
            wait_event(ctrlq.ack_queue, ...)  <-- deadlocks

Because ctrlq.dequeue_work is the only worker that reclaims ctrlq slots
and wakes ack_queue, ctrlq wedges permanently.  The same call path also
acquires vgdev->obj_restore_lock inside ctrlq.dequeue_work, creating an
ABBA deadlock with virtio_gpu_object_unref_all() and
virtio_gpu_object_restore_all(), which hold obj_restore_lock while queuing
ctrlq commands.  Deferring the transfer's gem release to obj_free_work,
like entry->objs, would prevent both deadlocks.

[ ... ]

> @@ -304,6 +307,25 @@ void virtio_gpu_dequeue_ctrl_func(struct work_struct *work)
>  
>  	list_for_each_entry(entry, &reclaim_list, list) {
>  		resp = (struct virtio_gpu_ctrl_hdr *)entry->resp_buf;
> +		if (entry->transfer) {
> +			if (!virtio_gpu_transfer_reply(vgdev, entry->transfer, resp,
> +						       entry->used_len)) {
> +				DRM_ERROR_RATELIMITED("invalid transfer response, resource %u\n",
> +						      entry->transfer->bo->hw_res_handle);
> +				continue;
> +			}
> +		}
> +		if (entry->used_len < sizeof(*resp) || entry->used_len > entry->resp_size) {
> +			spin_lock(&vgdev->ctrlq.qlock);
> +			virtio_gpu_damage_reset_locked(vgdev);
> +			spin_unlock(&vgdev->ctrlq.qlock);
> +			continue;
> +		}

[Severity: Medium]
Can continuing on an out-of-range entry->used_len leak buffer objects and
hang waiters permanently, even with buffer_age disabled?

This check runs unconditionally for all ctrlq commands and skips both
virtio_gpu_fence_event_process() and entry->resp_cb().  If a buggy or
non-conforming host returns a used length outside [sizeof(*resp),
entry->resp_size], skipping virtio_gpu_cmd_unref_cb() leaks the
virtio_gpu_object and its backing storage, and skipping
virtio_gpu_cmd_resource_uuid_cb() or virtio_gpu_cmd_resource_map_cb()
leaves virtgpu_virtio_get_uuid() or virtio_gpu_vram_mmap() stuck in an
uninterruptible wait_event(vgdev->resp_wq, ...) forever.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205063321.56168-1-tipreaditya@gmail.com?part=1

  reply	other threads:[~2026-12-05  6:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  6:33 [RFC PATCH 0/2] drm/virtio: avoid full uploads for rotating dumb buffers Aditya Tipre
2026-12-05  6:33 ` [RFC PATCH 1/2] drm/virtio: accumulate confirmed dumb-buffer damage Aditya Tipre
2026-12-05  6:59   ` sashiko-bot [this message]
2026-12-05  6:33 ` [RFC PATCH 2/2] drm/virtio: test transfer and damage history Aditya Tipre
2026-12-05  6:58   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251175@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=eperezma@redhat.com \
    --cc=mst@redhat.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tipreaditya@gmail.com \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.