dri-devel 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, media-ci@linuxtv.org
Subject: Re: [PATCH 1/3] dma-buf/dma-fence: Ensure correct ordering in dma_fence_timestamp()
Date: Thu, 19 Nov 2026 13:42:14 +0000	[thread overview]
Message-ID: <sashiko-outbox-223192@kernel.org> (raw)
In-Reply-To: <20261119131830.2535266-3-phasta@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dma-buf/dma-fence: dma_fence_timestamp() reads cb_list.next
  on i915 fences
--

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

dma-buf/dma-fence: Ensure correct ordering in dma_fence_timestamp()

This commit replaces the lockless test_bit() and cpu_relax() loop in
dma_fence_timestamp() with fence spinlock acquisition to ensure memory
ordering when reading fence->timestamp.

> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> index dd07d128adc3b..34aefb63a7fe8 100644
> --- a/include/linux/dma-fence.h
> +++ b/include/linux/dma-fence.h
> @@ -708,13 +708,17 @@ static inline void dma_fence_set_error(struct dma_fence *fence,
>   */
>  static inline ktime_t dma_fence_timestamp(struct dma_fence *fence)
>  {
> -	if (WARN_ON(!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
> -		return ktime_get();
> +	unsigned long flags;
> +	ktime_t ret;
>  
> -	while (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags))
> -		cpu_relax();
> +	dma_fence_lock_irqsave(fence, flags);
> +	if (WARN_ON(!dma_fence_test_signaled_flag(fence)))
> +		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: Medium]
Can dma_fence_timestamp() read fence->cb_list.next instead of a valid
timestamp when a fence signals in two phases?

In struct dma_fence, cb_list and timestamp share an anonymous union.
i915 signals request fences in two phases in
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c: __dma_fence_signal() first
sets DMA_FENCE_FLAG_SIGNALED_BIT without holding rq->lock, and
signal_irq_work() only later acquires rq->lock, replaces cb_list, and
calls __dma_fence_signal__timestamp() to write fence->timestamp and set
DMA_FENCE_FLAG_TIMESTAMP_BIT:

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

Because dma_fence_timestamp() no longer waits for
DMA_FENCE_FLAG_TIMESTAMP_BIT, callers such as sync_fill_fence_info() can
read the raw kernel pointer in fence->cb_list.next as the completion
timestamp and copy it to userspace in info->timestamp_ns.

Also, the kernel-doc comment above dma_fence_timestamp() still states
that this helper busy-waits for the timestamp to appear.

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

  reply	other threads:[~2026-11-19 13:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 13:18 [PATCH 0/3] dma-fence: Fix timestamp memory ordering Philipp Stanner
2026-11-19 13:18 ` [PATCH 1/3] dma-buf/dma-fence: Ensure correct ordering in dma_fence_timestamp() Philipp Stanner
2026-11-19 13:42   ` sashiko-bot [this message]
2026-11-19 13:18 ` [PATCH 2/3] dma-buf: dma-fence: Add dma_fence_overwrite_timestamp() Philipp Stanner
2026-11-19 13:18 ` [PATCH 3/3] drm/sched: Fix memory ordering with dma_fence_overwrite_timestamp() Philipp Stanner
2026-11-19 13: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-223192@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@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