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 8/8] dma-buf: dma-fence: Remove TIMESTAMP bit
Date: Fri, 27 Nov 2026 10:36:29 +0000	[thread overview]
Message-ID: <sashiko-outbox-237144@kernel.org> (raw)
In-Reply-To: <20261127101812.2706432-10-phasta@kernel.org>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] dma-buf: sync_debug: recursive spinlock deadlock in
  sync_print_fence()
- [Medium] dma-buf: dma-fence: race in dma_fence_timestamp() with i915
  fence signaling
- [Medium] drm/sched: unlocked timestamp write in
  drm_sched_overwrite_scheduled_ts()
--

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

dma-buf: dma-fence: Remove TIMESTAMP bit

This commit removes DMA_FENCE_FLAG_TIMESTAMP_BIT from enum
dma_fence_flag_bits and stops setting it in
dma_fence_signal_timestamp_locked().

> diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
> index 0776ca7c795e8..64d3ac4f0b8fa 100644
> --- a/drivers/dma-buf/dma-fence.c
> +++ b/drivers/dma-buf/dma-fence.c
> @@ -376,9 +376,7 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>  
>  	/* Stash the cb_list before replacing it with the timestamp */
>  	list_replace(&fence->cb_list, &cb_list);
> -
>  	fence->timestamp = timestamp;
> -	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
>  
>  	list_for_each_entry_safe(cur, tmp, &cb_list, node) {
>  		INIT_LIST_HEAD(&cur->node);

[Severity: Medium]
Does removing DMA_FENCE_FLAG_TIMESTAMP_BIT allow dma_fence_timestamp() to
read rq->fence.cb_list.next as the timestamp on i915 request fences?

In struct dma_fence, cb_list and timestamp share a union.  i915 sets
DMA_FENCE_FLAG_SIGNALED_BIT via __dma_fence_signal() before acquiring
rq->lock to replace cb_list and write rq->fence.timestamp:

CPU0                              CPU1
irq_signal_request()
  __dma_fence_signal(&rq->fence)
    test_and_set_bit(SIGNALED_BIT,
                     &fence->flags)
                                  sync_file_ioctl_fence_info()
                                    sync_fill_fence_info()
                                      dma_fence_timestamp(&rq->fence, &ts)
                                        dma_fence_lock_irqsave(...)
                                        if (dma_fence_test_signaled_flag())
                                          *ts = fence->timestamp;
                                          <-- reads fence->cb_list.next
                                        dma_fence_unlock_irqrestore(...)
  irq_work_queue(&b->irq_work)
...
signal_irq_work()
  spin_lock(&rq->lock)
  list_replace(&rq->fence.cb_list, ...)
  __dma_fence_signal__timestamp(...)
  spin_unlock(&rq->lock)

Because dma_fence_timestamp() now only checks DMA_FENCE_FLAG_SIGNALED_BIT
under the fence lock, a concurrent call during this window reads the raw
kernel pointer in cb_list.next as the timestamp and copies it to userspace
in struct sync_fence_info.timestamp_ns via SYNC_IOC_FILE_INFO.

> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> index 0a7ffda6a4cfe..7fd9393e60035 100644
> --- a/include/linux/dma-fence.h
> +++ b/include/linux/dma-fence.h
[ ... ]
> @@ -114,7 +113,6 @@ enum dma_fence_flag_bits {
>  	DMA_FENCE_FLAG_INLINE_LOCK_BIT,
>  	DMA_FENCE_FLAG_SEQNO64_BIT,
>  	DMA_FENCE_FLAG_SIGNALED_BIT,
> -	DMA_FENCE_FLAG_TIMESTAMP_BIT,
>  	DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
>  
>  	DMA_FENCE_FLAG_USER_BITS, /* must always be last member */

[Severity: High]
Does replacing the DMA_FENCE_FLAG_TIMESTAMP_BIT check with spinlock
serialization in dma_fence_timestamp() cause a recursive spinlock deadlock
in sync_print_obj() -> sync_print_fence()?

In drivers/dma-buf/sw_sync.c, sync_pt_create() initializes each pt->base
with &obj->lock as its fence spinlock.  When /sys/kernel/debug/sync/info is
read, sync_print_obj() already holds &obj->lock when it calls
sync_print_fence():

sync_info_debugfs_show()
  sync_print_obj()
    spin_lock(&obj->lock)
    sync_print_fence(&pt->base)
      dma_fence_timestamp(&pt->base, &ts)
        dma_fence_lock_irqsave(fence, flags)  <-- deadlocks on &obj->lock

[Severity: Medium]
With fence->timestamp now synchronized by the fence spinlock instead of
DMA_FENCE_FLAG_TIMESTAMP_BIT, is the unlocked write in
drm_sched_overwrite_scheduled_ts() a data race?

drivers/gpu/drm/scheduler/sched_main.c:drm_sched_overwrite_scheduled_ts() {
    ...
	if (dma_fence_timestamp(current_fence, &current_finished) == 0)
		next_fence->timestamp = current_finished;
}

drm_sched_overwrite_scheduled_ts() only holds sched->job_list_lock when
overwriting next_fence->timestamp, racing with concurrent readers such as
amdgpu_ctx_fence_time() calling dma_fence_timestamp(&s_fence->scheduled,
&scheduled_ts) under the fence spinlock and risking torn 64-bit ktime_t
reads on 32-bit architectures.

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

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

Thread overview: 12+ 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:18 ` [RFC PATCH 4/8] dma-buf: dma-fence: Improve dma_fence_timestamp() Philipp Stanner
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 [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-237144@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