All of lore.kernel.org
 help / color / mirror / Atom feed
From: Philipp Stanner <phasta@kernel.org>
To: "Sumit Semwal" <sumit.semwal@linaro.org>,
	"Christian König" <christian.koenig@amd.com>,
	"Philipp Stanner" <phasta@kernel.org>,
	"Alex Deucher" <alexander.deucher@amd.com>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Jani Nikula" <jani.nikula@linux.intel.com>,
	"Joonas Lahtinen" <joonas.lahtinen@linux.intel.com>,
	"Rodrigo Vivi" <rodrigo.vivi@intel.com>,
	"Tvrtko Ursulin" <tursulin@ursulin.net>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"Mario Limonciello (AMD)" <superm1@kernel.org>,
	"Ruslan Vagner" <rusya92266@gmail.com>,
	"Gustavo Padovan" <gustavo.padovan@collabora.com>,
	"Chris Wilson" <chris@chris-wilson.co.uk>
Cc: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org,
	linux-kernel@vger.kernel.org, amd-gfx@lists.freedesktop.org,
	intel-gfx@lists.freedesktop.org
Subject: [RFC PATCH 4/8] dma-buf: dma-fence: Improve dma_fence_timestamp()
Date: Fri, 27 Nov 2026 11:18:09 +0100	[thread overview]
Message-ID: <20261127101812.2706432-6-phasta@kernel.org> (raw)
In-Reply-To: <20261127101812.2706432-2-phasta@kernel.org>

dma_fence_timestamp() can be made a more robust and useful function by
having it return an error code that indicates whether the fence was
signaled or not. Only if it's signaled, returning a timestamp makes
sense, otherwise the user should choose what value to interpret as a
replacement.

Have dma_fence_timestamp() return an integer status code. Return the
timestamp via a pointer.

To be able to port drm_sched, move its hack for overwriting functions to
a temporary function for now.

Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 drivers/dma-buf/dma-fence-unwrap.c       | 20 +++++++++-----
 drivers/dma-buf/sync_file.c              |  8 +++---
 drivers/gpu/drm/scheduler/sched_entity.c |  4 +--
 drivers/gpu/drm/scheduler/sched_main.c   | 34 +++++++++++++++---------
 include/linux/dma-fence.h                | 20 ++++++++------
 5 files changed, 53 insertions(+), 33 deletions(-)

diff --git a/drivers/dma-buf/dma-fence-unwrap.c b/drivers/dma-buf/dma-fence-unwrap.c
index 120fe0c2a75a..a1e9b1ee2c32 100644
--- a/drivers/dma-buf/dma-fence-unwrap.c
+++ b/drivers/dma-buf/dma-fence-unwrap.c
@@ -136,10 +136,14 @@ struct dma_fence *__dma_fence_unwrap_merge(size_t num_fences,
 				unsignaled = dma_fence_get(tmp);
 				++count;
 			} else {
-				ktime_t t = dma_fence_timestamp(tmp);
+				ktime_t fence_ts = ktime_get();
+				/* Leaves fence_ts unchanged if fence is not yet
+				 * signaled.
+				 */
+				dma_fence_timestamp(tmp, &fence_ts);
 
-				if (ktime_after(t, timestamp))
-					timestamp = t;
+				if (ktime_after(fence_ts, timestamp))
+					timestamp = fence_ts;
 			}
 		}
 	}
@@ -168,10 +172,14 @@ struct dma_fence *__dma_fence_unwrap_merge(size_t num_fences,
 			if (!dma_fence_is_signaled(tmp)) {
 				array[count++] = dma_fence_get(tmp);
 			} else {
-				ktime_t t = dma_fence_timestamp(tmp);
+				ktime_t fence_ts = ktime_get();
+				/* Leaves fence_ts unchanged if fence is not yet
+				 * signaled.
+				 */
+				dma_fence_timestamp(tmp, &fence_ts);
 
-				if (ktime_after(t, timestamp))
-					timestamp = t;
+				if (ktime_after(fence_ts, timestamp))
+					timestamp = fence_ts;
 			}
 		}
 	}
diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c
index 2166bbdf7e4a..54ef230bcc4b 100644
--- a/drivers/dma-buf/sync_file.c
+++ b/drivers/dma-buf/sync_file.c
@@ -268,6 +268,7 @@ static long sync_file_ioctl_merge(struct sync_file *sync_file,
 static int sync_fill_fence_info(struct dma_fence *fence,
 				 struct sync_fence_info *info)
 {
+	ktime_t fence_ts = ktime_set(0, 0);
 	const char __rcu *timeline;
 	const char __rcu *driver;
 
@@ -282,10 +283,9 @@ static int sync_fill_fence_info(struct dma_fence *fence,
 		sizeof(info->driver_name));
 
 	info->status = dma_fence_get_status(fence);
-	info->timestamp_ns =
-		dma_fence_is_signaled(fence) ?
-			ktime_to_ns(dma_fence_timestamp(fence)) :
-			ktime_set(0, 0);
+
+	dma_fence_timestamp(fence, &fence_ts);
+	info->timestamp_ns = ktime_to_ns(fence_ts);
 
 	rcu_read_unlock();
 
diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c
index 03e68b4102d5..5ceffb4d3114 100644
--- a/drivers/gpu/drm/scheduler/sched_entity.c
+++ b/drivers/gpu/drm/scheduler/sched_entity.c
@@ -78,8 +78,8 @@ ktime_t drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job)
 	struct drm_sched_fence *s_fence = job->s_fence;
 	ktime_t start, end, duration;
 
-	start = dma_fence_timestamp(&s_fence->scheduled);
-	end = dma_fence_timestamp(&s_fence->finished);
+	dma_fence_timestamp(&s_fence->scheduled, &start);
+	dma_fence_timestamp(&s_fence->finished, &end);
 	duration = ktime_sub(end, start);
 
 	spin_lock(&stats->lock);
diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c
index 44553db9b04c..e515bfa949b4 100644
--- a/drivers/gpu/drm/scheduler/sched_main.c
+++ b/drivers/gpu/drm/scheduler/sched_main.c
@@ -898,6 +898,26 @@ drm_sched_select_entity(struct drm_gpu_scheduler *sched)
 	return IS_ERR(entity) ? NULL : entity;
 }
 
+/*
+ * This is a temporary, hacky function until we can find a canonical solution
+ * for not overwriting fence timestamps.
+ *
+ * FIXME: Remove this hack.
+ */
+static void drm_sched_overwrite_scheduled_ts(struct drm_sched_job *current_job,
+					     struct drm_sched_job *next_job)
+{
+	struct dma_fence *current_fence = &current_job->s_fence->finished;
+	struct dma_fence *next_fence = &next_job->s_fence->scheduled;
+	ktime_t current_finished, next_scheduled;
+
+	if (dma_fence_timestamp(next_fence, &next_scheduled) != 0)
+		return;
+
+	if (dma_fence_timestamp(current_fence, &current_finished) == 0)
+		next_fence->timestamp = current_finished;
+}
+
 /**
  * drm_sched_get_finished_job - fetch the next finished job to be destroyed
  *
@@ -928,19 +948,7 @@ drm_sched_get_finished_job(struct drm_gpu_scheduler *sched)
 		next = list_first_entry_or_null(&sched->pending_list,
 						typeof(*next), list);
 		if (next) {
-			/* Make the scheduled timestamp more accurate.
-			 *
-			 * TODO
-			 * The test_bit_acquire() is a hotfix against timestamp
-			 * misordering. Replace it with something better.
-			 *
-			 * The counter-part of the barrier is located in
-			 * dma_fence_signal_timestamp_locked().
-			 */
-			if (test_bit_acquire(DMA_FENCE_FLAG_TIMESTAMP_BIT,
-				     &next->s_fence->scheduled.flags))
-				next->s_fence->scheduled.timestamp =
-					dma_fence_timestamp(&job->s_fence->finished);
+			drm_sched_overwrite_scheduled_ts(job, next);
 
 			/* start TO timer for next job */
 			drm_sched_start_timeout(sched);
diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
index 1c1aa4c7635a..70bb8434ac1a 100644
--- a/include/linux/dma-fence.h
+++ b/include/linux/dma-fence.h
@@ -679,21 +679,25 @@ static inline void dma_fence_set_error(struct dma_fence *fence,
 /**
  * dma_fence_timestamp - helper to get the completion timestamp of a fence
  * @fence: fence to get the timestamp from.
+ * @ts: a place to store the timestamp to.
+ *
+ * Writes the fences timestamp into @ts if the fence is already signaled.
+ * Leaves @ts unchanged otherwise.
  *
  * Return:
- * The fence's timestamp if the fence is already signaled, the current time
- * otherwise.
+ * 0 if the timestamp was written to @ts, -ENOENT if the fence was not yet
+ * signaled.
  */
-static inline ktime_t dma_fence_timestamp(struct dma_fence *fence)
+static inline int dma_fence_timestamp(struct dma_fence *fence, ktime_t *ts)
 {
 	unsigned long flags;
-	ktime_t ret;
+	int ret = -ENOENT;
 
 	dma_fence_lock_irqsave(fence, flags);
-	if (WARN_ON(!test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
-		ret = ktime_get();
-	else
-		ret = fence->timestamp;
+	if (dma_fence_test_signaled_flag(fence)) {
+		*ts = fence->timestamp;
+		ret = 0;
+	}
 	dma_fence_unlock_irqrestore(fence, flags);
 
 	return ret;
-- 
2.56.0


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

Thread overview: 16+ 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:48   ` sashiko-bot
2026-11-27 10:18 ` Philipp Stanner [this message]
2026-11-27 10:42   ` [RFC PATCH 4/8] dma-buf: dma-fence: Improve dma_fence_timestamp() sashiko-bot
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
2026-11-27 12:27 ` ✓ i915.CI.BAT: success for " Patchwork
2026-11-28 17:57 ` ✓ i915.CI.Full: " Patchwork

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=20261127101812.2706432-6-phasta@kernel.org \
    --to=phasta@kernel.org \
    --cc=airlied@gmail.com \
    --cc=alexander.deucher@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=chris@chris-wilson.co.uk \
    --cc=christian.koenig@amd.com \
    --cc=dakr@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gustavo.padovan@collabora.com \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=jani.nikula@linux.intel.com \
    --cc=joonas.lahtinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=matthew.brost@intel.com \
    --cc=mripard@kernel.org \
    --cc=rodrigo.vivi@intel.com \
    --cc=rusya92266@gmail.com \
    --cc=simona@ffwll.ch \
    --cc=sumit.semwal@linaro.org \
    --cc=superm1@kernel.org \
    --cc=tursulin@ursulin.net \
    --cc=tzimmermann@suse.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.