Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Srinivasan Shanmugam" <srinivasan.shanmugam@amd.com>
Cc: dri-devel@lists.freedesktop.org, intel-xe@lists.freedesktop.org
Subject: Re: [PATCH v9 1/3] drm: Add drm_work_fence helper
Date: Wed, 09 Sep 2026 04:56:14 +0000	[thread overview]
Message-ID: <20260909045614.AFE911F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <20260909044454.399340-2-srinivasan.shanmugam@amd.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] drm_work_fence_cancel_sync() fails to synchronize with the dma-fence callback, allowing queue_work() to be called concurrently with driver teardown.
- [High] The driver's ops->destroy() callback can unexpectedly be executed in hard IRQ context during the cancellation race.
--

commit 781c5ea671786cbee47b9bed0159aa892cc5e2b7
Author: Srinivasan Shanmugam <srinivasan.shanmugam@amd.com>

drm: Add drm_work_fence helper

This commit introduces drm_work_fence, a helper that queues a work item
when a dma-fence signals. It manages dma-fence callbacks, kref lifetime,
and workqueue dispatch.

> diff --git a/drivers/gpu/drm/drm_work_fence.c b/drivers/gpu/drm/drm_work_fence.c
> new file mode 100644
> index 0000000000000..5e0cbc53edb80
> --- /dev/null
> +++ b/drivers/gpu/drm/drm_work_fence.c
[ ... ]
> +static void drm_work_fence_destroy(struct kref *kref)
> +{
> +	struct drm_work_fence *wfence =
> +		container_of(kref, struct drm_work_fence, refcount);
> +	struct dma_fence *fence = wfence->fence;
> +
> +	wfence->ops->destroy(wfence);

[Severity: High]
Can this ops->destroy() callback be executed in hard IRQ context?

If a dma-fence callback races with driver teardown and queue_work() fails,
the final reference might be dropped directly from the IRQ callback:

drm_work_fence_cb() -> drm_work_fence_queue()

If queue_work() returns false, drm_work_fence_put() drops the last reference
here, triggering this destroy callback under the dma-fence spinlock with
IRQs disabled.

> +	dma_fence_put(fence);	/* NULL-safe */
> +}
[ ... ]
> +static void drm_work_fence_queue(struct drm_work_fence *wfence)
> +{
> +	if (!queue_work(wfence->wq, &wfence->work))
> +		drm_work_fence_put(wfence);
> +}

[Severity: High]
Is there a use-after-free risk if wfence->wq is accessed here during a
concurrent driver teardown?

This relies on drm_work_fence_cancel_sync() successfully synchronizing
against the callback. However, if the synchronization fails, wfence->wq
could be accessed after the workqueue has been destroyed by the caller.

[ ... ]
> +void drm_work_fence_cancel_sync(struct drm_work_fence *wfence)
> +{
> +	if (drm_work_fence_cancel(wfence))
> +		return;
> +	if (disable_work_sync(&wfence->work))
> +		drm_work_fence_put(wfence);
> +}

[Severity: High]
Does this provide the expected synchronous cancellation guarantee, or does
it leave a window for concurrent execution during teardown?

If the dma-fence signals, the callback is dequeued but hasn't called
queue_work() yet. If drm_work_fence_cancel_sync() is called concurrently:

1. drm_work_fence_cancel() calls dma_fence_remove_callback(), which
   returns false because the callback is already dequeued.
2. disable_work_sync() returns false immediately because the work is
   not yet queued.
3. The function returns, falsely indicating the work is fully canceled.

This allows the driver teardown to proceed while the in-flight callback
might then call queue_work() on a potentially destroyed workqueue.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260909044454.399340-1-srinivasan.shanmugam@amd.com?part=1

  reply	other threads:[~2026-09-09  4:56 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-09  4:44 [PATCH v9 0/3] drm: Extract dma-fence-to-workqueue pattern into common helpers Srinivasan Shanmugam
2026-09-09  4:44 ` [PATCH v9 1/3] drm: Add drm_work_fence helper Srinivasan Shanmugam
2026-09-09  4:56   ` sashiko-bot [this message]
2026-09-09  4:44 ` [PATCH v9 2/3] drm: Add drm_user_fence helper Srinivasan Shanmugam
2026-09-09  4:54   ` sashiko-bot
2026-09-09  4:44 ` [PATCH v9 3/3] drm/xe: Convert xe_user_fence to drm_user_fence Srinivasan Shanmugam
2026-09-09  4:57   ` sashiko-bot
2026-09-09  4:51 ` ✗ CI.checkpatch: warning for drm: Extract dma-fence-to-workqueue pattern into common helpers (rev2) Patchwork
2026-09-09  4:53 ` ✓ CI.KUnit: success " Patchwork
2026-09-09  5:45 ` ✓ Xe.CI.BAT: " Patchwork
2026-09-09 10:49 ` ✓ Xe.CI.FULL: " Patchwork

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=20260909045614.AFE911F00A3D@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=srinivasan.shanmugam@amd.com \
    /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