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 <[email protected]>

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/[email protected]?part=1

Reply via email to