AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Philipp Stanner" <phasta@kernel.org>
Cc: dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org,
	intel-gfx@lists.freedesktop.org, media-ci@linuxtv.org
Subject: Re: [RFC PATCH 3/8] dma-buf: dma-fence: Ensure ordering with locks for timestamps
Date: Fri, 27 Nov 2026 10:48:36 +0000	[thread overview]
Message-ID: <sashiko-outbox-237167@kernel.org> (raw)
In-Reply-To: <20261127101812.2706432-5-phasta@kernel.org>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] dma-buf: dma-fence: union cb_list pointer read in
  dma_fence_timestamp()
- [High] dma-buf: dma-fence: dma_fence_timestamp() locks fence after
  signaling
- [Medium] dma-buf: dma-fence: unconditional spin_lock_irqsave in
  dma_fence_timestamp()
- [Medium] dma-buf: dma-fence: undocumented lock and no locked
  dma_fence_timestamp()
--

commit 2dafcb9d7a9ed94c64a7c7994912a38c5f4e4ac5
Author: Philipp Stanner <phasta@kernel.org>

dma-buf: dma-fence: Ensure ordering with locks for timestamps

This commit replaces the spin-wait on DMA_FENCE_FLAG_TIMESTAMP_BIT in
dma_fence_timestamp() with acquiring the fence's spinlock and drops the
corresponding smp_mb__before_atomic() barrier from
dma_fence_signal_timestamp_locked().

> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> index 9743d217264de..3a05fe9f64e8f 100644
> --- a/include/linux/dma-fence.h
> +++ b/include/linux/dma-fence.h
> @@ -706,23 +706,23 @@ static inline void dma_fence_set_error(struct dma_fence *fence,
>   * dma_fence_timestamp - helper to get the completion timestamp of a fence
>   * @fence: fence to get the timestamp from.
>   *
> - * After a fence is signaled the timestamp is updated with the signaling time,
> - * but setting the timestamp can race with tasks waiting for the signaling. This
> - * helper busy waits for the correct timestamp to appear.
> + * Return:
> + * The fence's timestamp if the fence is already signaled, the current time
> + * otherwise.
>   */
>  static inline ktime_t dma_fence_timestamp(struct dma_fence *fence)
>  {
> -	/*
> -	 * The test_bit_acquire() calls order against the
> -	 * smp_mb__before_atomic() in dma_fence_signal_timestamp_locked().
> -	 */
> -	if (WARN_ON(!test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
> -		return ktime_get();
> +	unsigned long flags;
> +	ktime_t ret;
>  
> -	while (!test_bit_acquire(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags))
> -		cpu_relax();
> +	dma_fence_lock_irqsave(fence, flags);
> +	if (WARN_ON(!test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
> +		ret = ktime_get();
> +	else
> +		ret = fence->timestamp;
> +	dma_fence_unlock_irqrestore(fence, flags);
>  
> -	return fence->timestamp;
> +	return ret;
>  }
>  
>  signed long dma_fence_wait_timeout(struct dma_fence *,

[Severity: High]
Can dma_fence_timestamp() read fence->cb_list.next instead of a valid
timestamp on i915 request fences?

In struct dma_fence, cb_list and timestamp share an anonymous union.  In
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c, irq_signal_request() and the
first loop of signal_irq_work() call __dma_fence_signal(&rq->fence), which
sets DMA_FENCE_FLAG_SIGNALED_BIT without holding rq->lock (the fence's
extern_lock) and before replacing rq->fence.cb_list or writing
rq->fence.timestamp:

CPU0                              CPU1
irq_signal_request()
  __dma_fence_signal(&rq->fence)
    test_and_set_bit(SIGNALED_BIT)
  irq_work_queue(&b->irq_work)
                                  sync_fill_fence_info()
                                    dma_fence_is_signaled() -> true
                                    dma_fence_timestamp(&rq->fence)
                                      dma_fence_lock_irqsave()
                                      ret = fence->timestamp
                                      /* reads fence->cb_list.next */
                                      dma_fence_unlock_irqrestore()
signal_irq_work()
  spin_lock(&rq->lock)
  list_replace(&rq->fence.cb_list, ...)
  __dma_fence_signal__timestamp()
  spin_unlock(&rq->lock)

Without the wait on DMA_FENCE_FLAG_TIMESTAMP_BIT, a caller running between
__dma_fence_signal() and the locked section in signal_irq_work() sees
DMA_FENCE_FLAG_SIGNALED_BIT set under rq->lock and reads
rq->fence.cb_list.next as the timestamp, corrupting merged fence timestamp
comparisons in __dma_fence_unwrap_merge() and copying a raw kernel heap
pointer to userspace via info->timestamp_ns in SYNC_IOC_FILE_INFO.

[Severity: High]
Does locking dma_fence_spinlock(fence) in dma_fence_timestamp() cause a
use-after-free on fence->extern_lock when the lock's owner frees it after
signaling?

The dma-fence lifetime rules in include/linux/dma-fence.h ("DOC: Safe
external access to driver provided object members") state that
fence->extern_lock must not be accessed after the fence has been signaled
because drivers may free the lock after an RCU grace period.  Unlike
helpers that check dma_fence_test_signaled_flag() before touching
fence->extern_lock, dma_fence_timestamp() is called on already-signaled
fences, such as in __dma_fence_unwrap_merge() during SYNC_IOC_MERGE.

For exporters where the structure holding extern_lock does not outlive
every reference to the signaled fence, such as ffs_dmabuf_release() freeing
priv (which holds the priv->lock passed to dma_fence_init() in
ffs_dmabuf_transfer()) or xe_hw_fence_irq_finish() freeing irq->lock after
synchronize_rcu(), dma_fence_lock_irqsave() in dma_fence_timestamp() writes
to freed memory.

[Severity: Medium]
Could taking dma_fence_lock_irqsave() on every dma_fence_timestamp() call
add unnecessary lock contention and irqsave overhead on hot paths?

Previously, dma_fence_timestamp() only performed read-only bit tests and a
timestamp load, keeping signaled fences in a shared cacheline state.
Acquiring and releasing the spinlock with local interrupts disabled dirties
the lock cacheline on every query.  In drm_sched_free_job_work(),
dma_fence_timestamp() is called multiple times per completed job (including
twice back-to-back on s_fence->scheduled and s_fence->finished in
drm_sched_entity_stats_job_add_gpu_time(), which share s_fence->lock).  In
__dma_fence_unwrap_merge(), every signaled fence in an unwrapped fence
array or chain now takes its spinlock in both passes, contending on the
global dma_fence_stub.inline_lock or on shared driver ring and IRQ locks.

[Severity: Medium]
Will dma_fence_timestamp() deadlock if called while the fence's spinlock is
already held?

dma_fence_timestamp() now acquires dma_fence_spinlock(fence) internally,
without documenting that callers must not hold the fence lock or providing
a dma_fence_timestamp_locked() helper.  Callers that already hold the fence
lock, such as fence completion callbacks invoked from
dma_fence_signal_timestamp_locked() or sync_print_fence() in
drivers/dma-buf/sync_debug.c (which runs under spin_lock(&obj->lock), the
extern_lock passed to dma_fence_init() in sync_pt_create(), and is
converted to call dma_fence_timestamp() in commit "dma-buf: sync_debug:
don't use TIMESTAMP bit" later in this series), will recursively deadlock
on the same spinlock:

sync_print_obj()
  spin_lock(&obj->lock)
  sync_print_fence(s, &pt->base, false)
    dma_fence_timestamp(&pt->base)
      dma_fence_lock_irqsave(&pt->base, flags)  <-- spins forever

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127101812.2706432-2-phasta@kernel.org?part=3

  reply	other threads:[~2026-11-27 10:48 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 10:18 [RFC PATCH 0/8] dma-fence: Remove TIMESTAMP bit Philipp Stanner
2026-11-27 10:18 ` [RFC PATCH 1/8] dma-buf: dma-fence: Hot-fix timestamp misordering Philipp Stanner
2026-11-27 10:18 ` [RFC PATCH 2/8] drm/sched: " Philipp Stanner
2026-11-27 10:18 ` [RFC PATCH 3/8] dma-buf: dma-fence: Ensure ordering with locks for timestamps Philipp Stanner
2026-11-27 10:48   ` sashiko-bot [this message]
2026-11-27 10:18 ` [RFC PATCH 4/8] dma-buf: dma-fence: Improve dma_fence_timestamp() Philipp Stanner
2026-11-27 10:42   ` sashiko-bot
2026-11-27 10:18 ` [RFC PATCH 5/8] drm/amdgpu: Stop using dma_fence internals Philipp Stanner
2026-11-27 10:18 ` [RFC PATCH 6/8] dma-buf: sync_debug: don't use TIMESTAMP bit Philipp Stanner
2026-11-27 10:26   ` sashiko-bot
2026-11-27 10:18 ` [RFC PATCH 7/8] drm/i915: Remove " Philipp Stanner
2026-11-27 10:35   ` sashiko-bot
2026-11-27 10:18 ` [RFC PATCH 8/8] dma-buf: dma-fence: " Philipp Stanner
2026-11-27 10:36   ` 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-237167@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=media-ci@linuxtv.org \
    --cc=phasta@kernel.org \
    --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