dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Maaz Mombasawala" <maaz.mombasawala@broadcom.com>,
	dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v8] drm/vmwgfx: Reserve ttm object before resv usage
Date: Fri, 20 Nov 2026 21:41:30 +0000	[thread overview]
Message-ID: <sashiko-outbox-226206@kernel.org> (raw)
In-Reply-To: <20261120212332.2025335-1-maaz.mombasawala@broadcom.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] drm/vmwgfx: race and NULL deref on guest_memory_bo in
  crc_generate_worker()
- [Medium] drm/vmwgfx: crc_generate_worker() clears res_dirty in
  vmw_resource_unreserve()
--

commit 0a12649f28f55fc0f97ac247d686a2212c714dd0
Author: Maaz Mombasawala <maaz.mombasawala@broadcom.com>

drm/vmwgfx: Reserve ttm object before resv usage

This commit reserves the surface resource under cmdbuf_mutex and reserves
its backing guest_memory_bo before computing the CRC in
crc_generate_worker().

> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_vkms.c b/drivers/gpu/drm/vmwgfx/vmwgfx_vkms.c
> index 3d0d5dfa869f9..8ca8632b97eae 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_vkms.c
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_vkms.c
> @@ -114,6 +114,7 @@ crc_generate_worker(struct work_struct *work)
>  	u64 frame_start, frame_end;
>  	u32 crc32 = 0;
>  	struct vmw_surface *surf = 0;
> +	bool valid_crc = false;
>  
>  	spin_lock_irq(&du->vkms.crc_state_lock);
>  	crc_pending = du->vkms.crc_pending;
> @@ -131,6 +132,7 @@ crc_generate_worker(struct work_struct *work)
>  	spin_unlock_irq(&du->vkms.crc_state_lock);
>  
>  	if (surf) {
> +		int ret;
>  		if (vmw_surface_sync(vmw, surf)) {
>  			drm_warn(
>  				crtc->dev,
> @@ -138,10 +140,35 @@ crc_generate_worker(struct work_struct *work)
>  			return;
>  		}
>  
> -		compute_crc(crtc, surf, &crc32);
> +		if (surf->res.guest_memory_bo) {
> +			struct vmw_resource *res = &surf->res;
> +
> +			mutex_lock(&res->dev_priv->cmdbuf_mutex);
> +			ret = vmw_resource_reserve(res, false, true);
> +			if (ret != 0)
> +				goto no_res_reserve;
> +			ret = ttm_bo_reserve(&res->guest_memory_bo->tbo, false,
> +					     false, NULL);

[Severity: High]
Can surf->res.guest_memory_bo still become NULL or be mid-teardown when
crc_generate_worker() reserves and maps it?

For dumb buffers, vmw_dumb_create() drops the surface's GEM reference on
vbo, leaving surf->res.guest_memory_bo as an unreferenced pointer that
vmw_bo_free() clears under cmdbuf_mutex when the last GEM reference is
dropped.  Checking surf->res.guest_memory_bo before acquiring
cmdbuf_mutex without re-checking it after locking leaves a race with
vmw_bo_free():

CPU0                                  CPU1 (kworker)
ttm_bo_release()
  ...
  vmw_bo_free()
                                      crc_generate_worker()
                                        if (surf->res.guest_memory_bo)
    mutex_lock(&cmdbuf_mutex)
    res->guest_memory_bo = NULL
    mutex_unlock(&cmdbuf_mutex)
    kfree(vbo)
                                        mutex_lock(&cmdbuf_mutex)
                                        vmw_resource_reserve(res, false, true)
                                        ttm_bo_reserve(&res->guest_memory_bo->tbo, ...)
                                          <-- NULL pointer dereference

Even if surf->res.guest_memory_bo is checked under cmdbuf_mutex,
ttm_bo_release() calls ttm_bo_cleanup_memtype_use() (which frees
bo->resource and sets it to NULL) and unlocks bo->base.resv before
calling bo->destroy (vmw_bo_free()).  If crc_generate_worker() takes
cmdbuf_mutex after ttm_bo_cleanup_memtype_use() unlocks bo->base.resv
but before vmw_bo_free() takes cmdbuf_mutex, ttm_bo_reserve() succeeds on
the dying BO and compute_crc() -> vmw_bo_map_and_cache() -> ttm_bo_kmap()
dereferences a NULL bo->resource.

In addition, vmw_surface_sync() is called just above without holding
cmdbuf_mutex or reserving surf->res, and it unconditionally dereferences
surf->res.guest_memory_bo in ttm_bo_reserve(&bo->tbo, ...).

> +			if (ret != 0) {
> +				drm_warn(&vmw->drm, "%s: failed reserve\n",
> +					 __func__);
> +				goto no_tbo_reserve;
> +			}
> +			compute_crc(crtc, surf, &crc32);
> +			valid_crc = true;
> +			ttm_bo_unreserve(&res->guest_memory_bo->tbo);
> +no_tbo_reserve:
> +			vmw_resource_unreserve(res, true, false, false, NULL,
> +					       0);

[Severity: Medium]
Should dirty_set be false in vmw_resource_unreserve() here?

Passing dirty_set = true and dirty = false unconditionally clears
res->res_dirty, including on the no_tbo_reserve error path or if the
resource was dirtied after vmw_surface_sync() ran outside cmdbuf_mutex,
which can cause later clean or eviction paths to skip reading back dirty
GPU surface data to the guest memory buffer.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120212332.2025335-1-maaz.mombasawala@broadcom.com?part=1

      reply	other threads:[~2026-11-20 21:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 21:23 [PATCH v8] drm/vmwgfx: Reserve ttm object before resv usage Maaz Mombasawala
2026-11-20 21:41 ` sashiko-bot [this message]

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-226206@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=maaz.mombasawala@broadcom.com \
    --cc=sashiko-reviews@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox