Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62A91606A322 for ; Fri, 4 Dec 2026 09:37:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=mailbox.org header.i=@mailbox.org header.a=rsa-sha256 header.s=mail20150812 header.b=NWX9tucT Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C832210E5AF; Fri, 4 Dec 2026 09:36:58 +0000 (UTC) Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) by gabe.freedesktop.org (Postfix) with ESMTPS id A91B3112E50; Fri, 4 Dec 2026 09:36:52 +0000 (UTC) Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4jgmbj048czKqPy; Fri, 04 Dec 2026 10:36:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1796377009; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=W4O8X76ANWNdls9c4DCayrfT2zY4SRkp1sdw/88L368=; b=NWX9tucT17DlMMVaUCKPNzC5n4373bcHyOAjebCUH/uf8IhAKGBxQWk0cQe7Q3nY6bkwYq dg3SWFJD5URlFc7ua7UjbPQ40GukJvJhj+mC2UoAJ53gyNg5UdS6nWtC83BLmpcmmj6Ify Fl586AxkX4b2yn3pnFSynVZuR8S3Yiu3BYjRD+Ggy/r2FIU6D8GlWFQKrrnF2eVY0iL3gb Q0XPDllgCeqgWo3PETWTaBHbup95u8nhoOOQrjLb+L23xiH+zW1JG3eFWi6vXxey8sKayw Ma1XhYptM4QiR0DjGILijD5N3s/wvTkcV0zp54uowhq9QEtc2/L97vLCxDSWbQ== Message-ID: Subject: Re: [PATCH v4] drm/sched: Track sched fence scheduled and finished time directly From: Philipp Stanner To: Tvrtko Ursulin , dri-devel@lists.freedesktop.org Cc: kernel-dev@igalia.com, Christian =?ISO-8859-1?Q?K=F6nig?= , Danilo Krummrich , Matthew Brost , Philipp Stanner , amd-gfx@lists.freedesktop.org Date: Fri, 04 Dec 2026 10:36:47 +0100 In-Reply-To: <20261203174025.10557-1-tvrtko.ursulin@igalia.com> References: <20261203174025.10557-1-tvrtko.ursulin@igalia.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.60.2 (3.60.2-3.fc44) X-Archived-By: MIME-Version: 1.0 X-MBO-RS-ID: 442416d67f4c0acf046 X-MBO-RS-META: bboan4oz377j7xxfkr3m7po5g4yzz6n5 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: phasta@kernel.org Sender: "dri-devel" X-Spam-Status: No, score=-16.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Thu, 2026-12-03 at 17:40 +0000, Tvrtko Ursulin wrote: > Philipp reports the current usage of overwriting the dma-fence timestamp > from the free_job worker is racy and unsafe due missing memory ordering > and especially with drivers which use own unordered submit workqueue for > the scheduler. Consider also that the timestamp field is the union in the > dma-fence object. I *think* this should de jure be fixed by my series being in misc- fixes: https://lore.kernel.org/dri-devel/944048822422e000f5af5abbb04797dd3159e763.= camel@mailbox.org/ Nevertheless, your patch here is desirable. Do we agree that it should end up in misc-next as a general improvement? >=20 > On top of these serious issues, there is also a smaller problem where the > said race can cause GPU time overaccounting. Which race, you mean the unordered worker? > Lets try and solve all issues in one swoop by stopping messing with > dma-fence internals and just track what we want in our own derived fence > object. We'd have to agree on what purpose this patch should have. Either we backmerge the ordering fixes into misc-next, then this patch here would just be an improvement for better timestamp accuracy, but not a fix of UB. Or we don't backmerge, this patch is rebased onto misc-next and also fixes the undefined behavior, but independently from misc-fixes. I think the former is better? Opinions? > We care about fence submit and finish times and by adding a dedicated > lock to manage them we can safely access both and adjust the former > without any races, and without needing to overwrite the internal dma-fenc= e > timestamp field. How's it determinend whether a timestamp is not yet set to begin with? IOW, what kind of accesses does the lock synchronize? >=20 > Signed-off-by: Tvrtko Ursulin > References: https://lore.kernel.org/dri-devel/20261119131830.2535266-2-ph= asta@kernel.org/ > References: https://lore.kernel.org/dri-devel/20261117094324.8256-1-tvrtk= o.ursulin@igalia.com/ > Cc: Christian K=C3=B6nig > Cc: Danilo Krummrich > Cc: Matthew Brost > Cc: Philipp Stanner > Cc: amd-gfx@lists.freedesktop.org > --- > v2: > =C2=A0* Fix kerneldoc copy and paste. > =C2=A0* Grab timestamps after obtaing locks. > =C2=A0* Prevent negative job durations on canceled/timedout jobs. >=20 > v3: > =C2=A0* Do not miss timestamp adjustment when multiple jobs complete. >=20 > v4: > =C2=A0* Rebase for upstream changes. > =C2=A0* More consolidate advance timestamp helper with a better kerneldoc= . > --- > =C2=A0drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c=C2=A0=C2=A0=C2=A0 | 15 ++++= ---- > =C2=A0drivers/gpu/drm/scheduler/sched_entity.c=C2=A0=C2=A0 |=C2=A0 6 ++-- > =C2=A0drivers/gpu/drm/scheduler/sched_fence.c=C2=A0=C2=A0=C2=A0 | 21 ++++= +++++-- > =C2=A0drivers/gpu/drm/scheduler/sched_internal.h | 41 +++++++++++++++++++= +++ > =C2=A0drivers/gpu/drm/scheduler/sched_main.c=C2=A0=C2=A0=C2=A0=C2=A0 | 19= ++-------- > =C2=A0include/drm/gpu_scheduler.h=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 39 ++++++++++++++++++= ++ > =C2=A06 files changed, 113 insertions(+), 28 deletions(-) >=20 > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c b/drivers/gpu/drm/am= d/amdgpu/amdgpu_ctx.c > index 415f23aa08a0..1a6985283acc 100644 > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c Could or should that be a separate patch? What is the change to amdgpu achieving, exactly? > @@ -171,21 +171,20 @@ static unsigned int amdgpu_ctx_get_hw_prio(struct a= mdgpu_ctx *ctx, u32 hw_ip) > =C2=A0static ktime_t amdgpu_ctx_fence_time(struct dma_fence *fence) > =C2=A0{ > =C2=A0 struct drm_sched_fence *s_fence; > + ktime_t start, end; > =C2=A0 > =C2=A0 if (!fence) > =C2=A0 return ns_to_ktime(0); > =C2=A0 > - /* When the fence is not even scheduled it can't have spend time */ > =C2=A0 s_fence =3D to_drm_sched_fence(fence); > - if (!test_bit_acquire(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->scheduled= .flags)) > - return ns_to_ktime(0); > + drm_sched_fence_get_times(s_fence, &start, &end); > + > + /* When the fence is not even scheduled it can't have spend time */ > + if (!start) Is it common to evaluate a ktime_t like that? The readability IMO decreases because before you at least saw "timestamp is now set, you can read it", whereas now you check whether a time is !=3D 0. I currently have another patch that modifies dma_fence_timestamp() like so: /** * bla bla * * Return: -EAGAIN if there's no timestamp yet. */ int dma_fence_timestamp(struct dma_fence *fence, ktime_t *ts); An API like that feels far more robust and intuitive to me. Can it make sense to split your accessor function into two, get_start_timestamp() and get_end_timestamp()? > + return start; > =C2=A0 > =C2=A0 /* When it is still running account how much already spend */ > - if (!test_bit_acquire(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->finished.= flags)) > - return ktime_sub(ktime_get(), s_fence->scheduled.timestamp); > - > - return ktime_sub(s_fence->finished.timestamp, > - s_fence->scheduled.timestamp); > + return ktime_sub(end ?: ktime_get(), start); > =C2=A0} > =C2=A0 > =C2=A0static ktime_t amdgpu_ctx_entity_time(struct amdgpu_ctx *ctx, > diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/s= cheduler/sched_entity.c > index bc5141c85fbc..fd59844cb989 100644 > --- a/drivers/gpu/drm/scheduler/sched_entity.c > +++ b/drivers/gpu/drm/scheduler/sched_entity.c > @@ -78,8 +78,10 @@ ktime_t drm_sched_entity_stats_job_add_gpu_time(struct= drm_sched_job *job) > =C2=A0 struct drm_sched_fence *s_fence =3D job->s_fence; > =C2=A0 ktime_t start, end, duration; > =C2=A0 > - start =3D dma_fence_timestamp(&s_fence->scheduled); > - end =3D dma_fence_timestamp(&s_fence->finished); > + drm_sched_fence_get_times(s_fence, &start, &end); > + if (WARN_ON_ONCE(!start || !end || ktime_after(start, end))) The WARN is new. Can this now suddenly occur? >=20 > =C2=A0 > =C2=A0void drm_sched_fence_finished(struct drm_sched_fence *fence, int re= sult) > =C2=A0{ > + unsigned long flags; > + ktime_t now; > + > =C2=A0 if (result) > =C2=A0 dma_fence_set_error(&fence->finished, result); > - dma_fence_signal(&fence->finished); > + > + spin_lock_irqsave(&fence->ts_lock, flags); > + now =3D ktime_get(); > + fence->finished_ts =3D now; > + spin_unlock_irqrestore(&fence->ts_lock, flags); [=E2=80=A6] > +static inline void > +drm_sched_fence_advance_start_time(struct drm_sched_fence *next, > + =C2=A0=C2=A0 struct drm_sched_fence *prev) > +{ > + spin_lock_irq(&prev->ts_lock); > + spin_lock_nested(&next->ts_lock, SINGLE_DEPTH_NESTING); /* Timeline ord= er */ What's the deal with the locking functions here? Why do you lock with irqsave above, and here with _irq? What's _nested supposed to achieve? > + if (next->start_ts && > + =C2=A0=C2=A0=C2=A0 ktime_after(prev->finished_ts, next->start_ts) && > + =C2=A0=C2=A0=C2=A0 (!next->finished_ts || > + =C2=A0=C2=A0=C2=A0=C2=A0 !ktime_after(prev->finished_ts, next->finished= _ts))) > + next->start_ts =3D prev->finished_ts; Hard to read logic. Can that be made more readable with `goto unlock`? > + spin_unlock(&next->ts_lock); > + spin_unlock_irq(&prev->ts_lock); > +} > + [=E2=80=A6] > + /** > + * @ts_lock: Lock used for @start_ts and @finished_ts. nit s/used for/protecting > + */ > + spinlock_t ts_lock; > + > + /** > + * @start_ts: ktime of fence GPU execution start or 0 is not started. > + * > + * Must be accessed via helpers. > + */ > + ktime_t start_ts; > + > + /** > + * @finished_ts: ktime of fence GPU execution end or 0 if not finished. GPU execution of whom? ;) This gets me thinking. So it's the GPU execution time of a =E2=80=A6 well, = job. Not the fences, but they are supposed to track the execution status of a job. Now with sched_fence, we have this weird situation that it's an API object, mostly entrenched into amdgpu to do container_of on just one fence and then obtain things like the scheduled status. Still, struct drm_sched_job annoyingly also contains the job->submit_ts So I am afraid that our API is about to drift further apart regarding consistency, because the job tracks when it was submitted, but the sched_fence when it was scheduled and when it was finished. I'm not sure how to set this straight, but if we have so many related timestamps it sounds as if there should be a distinct job_times object for that. But who would access that when and why? That question mostly boils down to amdgpu's desire to obtain these timestamps (why does it even need that, btw? Is that related to your scheduling granularity? drm_sched does the scheduling, so why does amdgpu need *exact* timestamps other than the fence timestamps?) We also now have the scheduling stats time object, but I guess that's fine since it's distinct and just accumulates job-times. But hence my question, if the scheduler does the scheduling, then why does an external party need the super-duper-exact timestamps? > + * > + * Must be accessed via helpers. > + */ > + ktime_t finished_ts; > + > =C2=A0 /** > =C2=A0 * @deadline: deadline set on &drm_sched_fence.finished which > =C2=A0 * potentially needs to be propagated to &drm_sched_fence.parent > @@ -344,6 +363,26 @@ struct drm_sched_fence { > =C2=A0 > =C2=A0struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f); > =C2=A0 > +/** > + * drm_sched_fence_get_times() - Get scheduler fence start and finished = times > + * @fence: Scheduler fence > + * @scheduled: Pointer to scheduled time storage > + * @finished: Pointer to finished time storage > + * > + * Obtains scheduler fence scheduled and finished times. > + */ > +static inline void > +drm_sched_fence_get_times(struct drm_sched_fence *fence, > + =C2=A0 ktime_t *scheduled, ktime_t *finished) Yeah, I think two functions are the right thing really, because then you can return "not yet available". *if* anyone really needs only the scheduled without the finished time. See my question above. P.