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

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

Reply via email to