From mboxrd@z Thu Jan 1 00:00:00 1970 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 17DDD606A325 for ; Tue, 1 Dec 2026 09:18:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=HXeGfmCj Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id A900D10F6B9; Tue, 1 Dec 2026 09:17:55 +0000 (UTC) Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3E60510F6A7; Tue, 1 Dec 2026 09:17:49 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 60A04623DA; Tue, 1 Dec 2026 09:17:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 92C0C1F00924; Tue, 1 Dec 2026 09:17:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796116668; bh=hx61BzasXnL/H+12UUyj83jFtJ0OcvJYIhpc+jg3DFk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=HXeGfmCj2Xlu+kMbS5gsguG6UuxvtLUu5v9qtwm2VAbJpULB8+BzU/NF/KUmqAN0f 2YxxW2sWUfnTcSmKQnd1n2tqtYWH+2wlsnblN7FIk5+VGowbnCTJs4rA/LRRuw1eKi qkSx1tZP6eK+5MXDSAImmA8OkhhYAmI7KdGTMDbeDFG/cjvJ9XL/SHhDO5LALoyrYm 1n9cmDE8W8IiPYZ0acDbcgH7ogpVAN5j28aOJK0eG4brQD1FZcJGAGFriAMAeuX2Ng Xex/5UcoZpKDYyiQYkAKodv8KFerjMpxGJX17WM+E0aYrBQAkI4mV/E/pj+QDjgjQI 4lTGcLlHsgtjw== From: Philipp Stanner To: Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Philipp Stanner , Alex Deucher , David Airlie , Simona Vetter , Matthew Brost , Danilo Krummrich , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Tvrtko Ursulin , Pierre-Eric Pelloux-Prayer , Shahyan Soltani , Ruslan Vagner , "Mario Limonciello (AMD)" , Gustavo Padovan , Chris Wilson Cc: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, amd-gfx@lists.freedesktop.org, stable@vger.kernel.org Subject: [PATCH v2 2/3] drm/sched: Prevent overwrite of non-existing timestamp Date: Tue, 1 Dec 2026 10:17:12 +0100 Message-ID: <20261201091712.716354-4-phasta@kernel.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261201091712.716354-2-phasta@kernel.org> References: <20261201091712.716354-2-phasta@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "amd-gfx" X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=unavailable 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 dma_fence timestamps could so far be written and read without any memory ordering, a bug that presumably got overlooked because x86 is strongly ordered. On more weakly ordered architectures, however, bugs could arise at any moment since this is UB. The current misordering allows for the bit DMA_FENCE_FLAG_TIMESTAMP_BIT to be read as true before the union containing the timestamp actually has the timestamp set. drm_sched currently (sort of illegally) overwrites a fence's timestamp. To do that somewhat safely, you have to wait for the timestamp to be present, otherwise you overwrite another member of the union whose memory the timestamp shares. Ensure correct ordering in the scheduler's timestamp evaluation through test_bit_acquire(). Cc: stable@vger.kernel.org Fixes: f7fe64ad0f22 ("drm/sched: Split free_job into own work item") Signed-off-by: Philipp Stanner Reviewed-by: Tvrtko Ursulin --- drivers/gpu/drm/scheduler/sched_main.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c index 99252674e266..1a40a3549d7a 100644 --- a/drivers/gpu/drm/scheduler/sched_main.c +++ b/drivers/gpu/drm/scheduler/sched_main.c @@ -928,8 +928,19 @@ 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, + /* Make the scheduled timestamp more accurate. + * + * TODO: + * Stop overwriting a fence's timestamp. + * + * test_bit_acquire() ensures ordering so that only + * timestamps that are actually present already get + * overwritten (timestamp is a union). + * + * 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); -- 2.56.0