Re: [PATCH 2/3] dma-buf: dma-fence: Add dma_fence_overwrite_timestamp()

From: Christian König

Date: Fri Nov 20 2026 - 04:39:08 EST


On 11/19/26 14:18, Philipp Stanner wrote:
> A fence's timestamp is just an opportunistic data point for debugging
> information etc. drm_sched, for example, uses the timestamps for its
> scheduling algorithms.
>
> In rare situations it can make sense to allow users to overwrite a
> timestamp with a more accurate value. This is relatively harmless since
> the timestamp has no meaning for the implementation itself.
>
> Add dma_fence_overwrite_timestamp().

I think that this is a very bad idea since it gives the impression that overwriting the timestamp on a fence is ok.

That was only done on the scheduled dma-fence because nobody cares about that time in the uAPI. For all other cases that can blow up.

I would rather come up with a clean approach for the scheduler to track the spend time per entity which would make that hack completely superfluous.

IIRC Tvrtko already created patches for that, I'm just not sure if they were ever committed.

Regards,
Christian.

>
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Philipp Stanner <phasta@xxxxxxxxxx>
> ---
> drivers/dma-buf/dma-fence.c | 28 ++++++++++++++++++++++++++++
> include/linux/dma-fence.h | 1 +
> 2 files changed, 29 insertions(+)
>
> diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
> index bd58688b81a7..f1b7e90864bf 100644
> --- a/drivers/dma-buf/dma-fence.c
> +++ b/drivers/dma-buf/dma-fence.c
> @@ -412,6 +412,34 @@ void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
> }
> EXPORT_SYMBOL(dma_fence_signal_timestamp);
>
> +/**
> + * dma_fence_overwrite_timestamp - overwrite a fence's timestamp
> + * @fence: the fence whose timestamp shall be overwritten
> + * @timestamp: the timestamp
> + *
> + * A dma_fence's timestamp is merely an opportunistically evaluated value to
> + * obtain information such as scheduling granularity. In some situations it can
> + * make sense to overwrite with a custom value. This function provides said
> + * functionality with the correct memory ordering.
> + *
> + * Return: 0 on success, -EAGAIN if the fence was not yet signaled.
> + */
> +int dma_fence_overwrite_timestamp(struct dma_fence *fence, ktime_t timestamp)
> +{
> + unsigned long flags;
> + int ret = 0;
> +
> + dma_fence_lock_irqsave(fence, flags);
> + if (dma_fence_test_signaled_flag(fence))
> + fence->timestamp = timestamp;
> + else
> + ret = -EAGAIN;
> + dma_fence_unlock_irqrestore(fence, flags);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL(dma_fence_overwrite_timestamp);
> +
> /**
> * dma_fence_signal_locked - signal completion of a fence
> * @fence: the fence to signal
> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> index 34aefb63a7fe..815471b844f0 100644
> --- a/include/linux/dma-fence.h
> +++ b/include/linux/dma-fence.h
> @@ -447,6 +447,7 @@ bool dma_fence_check_and_signal_locked(struct dma_fence *fence);
> void dma_fence_signal_locked(struct dma_fence *fence);
> void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
> void dma_fence_signal_timestamp_locked(struct dma_fence *fence, ktime_t timestamp);
> +int dma_fence_overwrite_timestamp(struct dma_fence *fence, ktime_t timestamp);
> signed long dma_fence_default_wait(struct dma_fence *fence,
> bool intr, signed long timeout);
> int dma_fence_add_callback(struct dma_fence *fence,