From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796116662; b=nxjjjc/J8CneUarjqANeSOh7NZuHR1e4HIbftAIEYL4IAr8lzF5pTY8n/ultgtyW/C4S sll4s5SGnvy6ZZgnINbwF9bgyTV+cEbNhE8BbJpBD7wQl4Z/LgIxV0xPJD67fbgi6qelQ E2aMbxDTSHsOg0SEGwS6/objWrCL7El14dZbL5MbMyfkiuEZuNhsn0+m1J1PHYf8esh+2 fT/Q06xHd4PEMDhnvyC+HKjQRY4rbONRAVck+I9WvCYYTQek5YYQHwXYEaZLY60IjIb9f Wf78jBDomLC2x6ZPxFobr46E1MAcc8F3/DFW9D6+D03AqKQO6D+UtsOtz/zHYPK2V0w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796116662; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=DuN9DE1QyfD23zdWva7OHuejJdZZBJ/XZtzzgI1/WmQ=; b=0Zw6DN4ieAK6RVQzjVyc5EV6br1OHpklcRu+SKVMcxZmhkGv1T7kqSJraL6zSeL/J5NY zCZznJGyZanf+qsD7r0swOr3GvWI+E8IXJ6Reww88LJvFcLaOMX8Tg4Nym1e8dpgVXCto xp/nRB6PLNNQHA/+VhPaZBq4x+qibTxRXNquPxf1mEeJ2F/MKDVMbkftYohR5cVlpiCqX Ao7YopI68S0FVRVIVwcokJmfGFSOHpgZn/zD5HqenURR9aIjfl1c7oXbA56p0/7UOLICP TEtf2MQAJ1MX9lj9LN//vdepu6hf+mN7ALF+XhVA4s52orQLSBMrXrex94NShUlYyXQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=EVyvdxaS; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6B739606A31A; Tue, 1 Dec 2026 09:17:42 +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=EVyvdxaS Received: by smtp.kernel.org (Postfix) with ESMTPSA id ECD751F00924; Tue, 1 Dec 2026 09:17:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796116662; bh=DuN9DE1QyfD23zdWva7OHuejJdZZBJ/XZtzzgI1/WmQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=EVyvdxaSwCndKkYIvgGz9U8Dwxl41KYO7qqgIlCdWFVdTQ6/MqBTpZ82vavPuOuf+ KotlvB38NoCzAsEORLo+xqLefVbC2N96qKfUeKAMFVAGdrrHd+f+2Saxn5yP35zLbl dxzSzX8Sy0ecsfbDHJEB9oJKT8PM339BBGb0v8bfNpNPPdp8pH6rv4wTxCt2laFyZR 1dPyNvqbB16i2pivC/xvHf7WfhqfBKYuBUtH9tuZ61KSBcurFmV5Z6Ic7ukBTlokeE sHTwh6epm9EkuFYycU7tUHC99gryDaBw9n+UYzA2Q979lcdty3J5yKkdB1Ag/uvZ6s m6O4AJ9SjnNkw== 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 1/3] dma-buf: dma-fence: Fix timestamp misordering Date: Tue, 1 Dec 2026 10:17:11 +0100 Message-ID: <20261201091712.716354-3-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-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit fence timestamps can be written and read without any memory ordering since, presumably, forever. This can cause real bugs for parties whose algorithms depend on valid timestamp values. The current misordering allows for the bit DMA_FENCE_FLAG_TIMESTAMP_BIT to be read as true before the union containing the timestamp actually gets the timestamp set. Thus, a reader could interpret the list-head in the union as a timestamp value by accident. So currently, both direct users of dma_fence_timestamp() and parties that check the flag DMA_FENCE_FLAG_TIMESTAMP_BIT manually rely on UB, which is especially likely to produce bugs on weakly ordered architectures. Fix the problem for now by adding appropriate memory barriers to ensure ordering. Cc: stable@vger.kernel.org Fixes: 76250f2b743b ("dma-buf/fence: Avoid use of uninitialised timestamp") Signed-off-by: Philipp Stanner --- drivers/dma-buf/dma-fence.c | 9 +++++++++ include/linux/dma-fence.h | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c index bd58688b81a7..ccb8258690f7 100644 --- a/drivers/dma-buf/dma-fence.c +++ b/drivers/dma-buf/dma-fence.c @@ -378,6 +378,15 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence, list_replace(&fence->cb_list, &cb_list); fence->timestamp = timestamp; + /* + * TODO: + * This is a (temporary?) fix to prevent misordering until we can + * completely remove the TIMESTAMP bit. + * + * The counterpart of this barrier is the test_bit_acquire() in + * dma_fence_timestamp(). + */ + smp_mb__before_atomic(); set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags); list_for_each_entry_safe(cur, tmp, &cb_list, node) { diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h index 64330bf52561..fcfdf302f1d9 100644 --- a/include/linux/dma-fence.h +++ b/include/linux/dma-fence.h @@ -718,7 +718,11 @@ static inline ktime_t dma_fence_timestamp(struct dma_fence *fence) if (WARN_ON(!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))) return ktime_get(); - while (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) + /* + * The test_bit_acquire() calls order against the + * smp_mb__before_atomic() in dma_fence_signal_timestamp_locked(). + */ + while (!test_bit_acquire(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) cpu_relax(); return fence->timestamp; -- 2.56.0