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>,
	"Simona Vetter" <simona.vetter@ffwll.ch>,
	"Chris Wilson" <chris@chris-wilson.co.uk>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Gary Guo" <gary@garyguo.net>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tvrtko Ursulin" <tvrtko.ursulin@igalia.com>,
	"Paul E . McKenney" <paulmck@kernel.org>,
	"Yunxiang Li" <Yunxiang.Li@amd.com>,
	"Alex Deucher" <alexander.deucher@amd.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>
Cc: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH 3/3] drm/sched: Fix memory ordering with dma_fence_overwrite_timestamp()
Date: Thu, 19 Nov 2026 14:18:31 +0100	[thread overview]
Message-ID: <20261119131830.2535266-5-phasta@kernel.org> (raw)
In-Reply-To: <20261119131830.2535266-2-phasta@kernel.org>

There is a memory ordering bug in drm_sched_get_finished_job(). The
manual timestamp flag check with test_bit() is not ordered.

The dma_fence framework now provides dma_fence_overwrite_timestamp(), a
function to overwrite a timestamp in a safe manner with correct memory
ordering.

Solve the bug with dma_fence_overwrite_timestamp().

Cc: stable@vger.kernel.org
Fixes: f7fe64ad0f22 ("drm/sched: Split free_job into own work item")
Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 drivers/gpu/drm/scheduler/sched_main.c | 28 +++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c
index 99252674e266..a065a6d2eb1c 100644
--- a/drivers/gpu/drm/scheduler/sched_main.c
+++ b/drivers/gpu/drm/scheduler/sched_main.c
@@ -66,6 +66,7 @@
  * This implies waiting for previously executed jobs.
  */
 
+#include <linux/dma-fence.h>
 #include <linux/export.h>
 #include <linux/wait.h>
 #include <linux/sched.h>
@@ -898,6 +899,22 @@ drm_sched_select_entity(struct drm_gpu_scheduler *sched)
 	return IS_ERR(entity) ? NULL : entity;
 }
 
+/**
+ * drm_sched_overwrite_scheduled_timestamp - make timestamp more accurate
+ *
+ * Overwrites the timestamp of the provided scheduled fence with the one from
+ * the finished fence, if the scheduled fence is already signaled. Does nothing
+ * otherwise.
+ */
+static void
+drm_sched_overwrite_scheduled_timestamp(struct dma_fence *finished,
+					struct dma_fence *scheduled)
+{
+	ktime_t timestamp = dma_fence_timestamp(finished);
+
+	dma_fence_overwrite_timestamp(scheduled, timestamp);
+}
+
 /**
  * drm_sched_get_finished_job - fetch the next finished job to be destroyed
  *
@@ -928,11 +945,12 @@ 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 */
-			if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT,
-				     &next->s_fence->scheduled.flags))
-				next->s_fence->scheduled.timestamp =
-					dma_fence_timestamp(&job->s_fence->finished);
+			/* Try to make the scheduled timestamp more accurate. */
+			drm_sched_overwrite_scheduled_timestamp(
+					&job->s_fence->finished,
+					&next->s_fence->scheduled
+					);
+
 
 			/* start TO timer for next job */
 			drm_sched_start_timeout(sched);
-- 
2.56.0


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

Thread overview: 10+ 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
2026-11-20  9:33   ` Christian König
2026-11-20 13:23     ` Philipp Stanner
2026-11-19 13:18 ` [PATCH 2/3] dma-buf: dma-fence: Add dma_fence_overwrite_timestamp() Philipp Stanner
2026-11-20  9:38   ` Christian König
2026-11-20 10:01     ` Philipp Stanner
2026-11-19 13:18 ` Philipp Stanner [this message]
2026-11-19 13:36   ` [PATCH 3/3] drm/sched: Fix memory ordering with dma_fence_overwrite_timestamp() 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=20261119131830.2535266-5-phasta@kernel.org \
    --to=phasta@kernel.org \
    --cc=Yunxiang.Li@amd.com \
    --cc=airlied@gmail.com \
    --cc=alexander.deucher@amd.com \
    --cc=aliceryhl@google.com \
    --cc=chris@chris-wilson.co.uk \
    --cc=christian.koenig@amd.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=paulmck@kernel.org \
    --cc=simona.vetter@ffwll.ch \
    --cc=simona@ffwll.ch \
    --cc=stable@vger.kernel.org \
    --cc=sumit.semwal@linaro.org \
    --cc=tvrtko.ursulin@igalia.com \
    --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.