From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=213.97.179.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796122649; b=fQwpmAnhH7fIfyqGE8RfBudf8K7t6xyxnrBG90+mbBAqWym6P4lOAwhyhoO3VlmvJpFM otpmG8cJZx2OzaZ9ju3E5gzW5J3e+izHyZliJuJnKUM051rMEisp0HMsJrGIFkzQVCAWN Y45yQqi7gYiQuF/xZCBJxu/Pc54lhGVrrq/e9/Ka0FnR0mjHgEeDDAQAuatJdpGmOcZK2 HHzNQgUIpAQsDYCNiigHj4IIxiZtpfpYvVQX/frBAiiN9aD9aqY8n5D24gxstTbRyUReO YCp22/Bx53RAtHAhkssxAcizxbDFFzYtGIkqbTGX6mi7aldH65DTa8MkqzpYrA0ar4A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796122649; h=DMARC-Filter:DKIM-Signature:Received:Message-ID:Date:MIME-Version: User-Agent:Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=mkvWh19CVFdTOrC4Z0qNqPw1uRirP0iV9ZNaoeqiTeM=; b=WLSnQ7mEbJ9/8BsYBjTbKk0yopK/nkNM9Q9Rqj2/8esiPkVVqrbptpEO0TEs+TDDYqiz GKn02E4rNtYyt3qLXGuzLRcCSIlZzncXxy2EarsPmDIXvcBA4qphHBfX2CEvaGByajTzr UB73vvScua1wjwuOCAw5/Fm+S1l/bEQWlDcarq3TDEtiFPydwlPy/p9DF7DApcJe4LxNN gQiafrFFRrvlCpaAhbZV6rKg2Sgaq8e4mSd8/6czMzBzwleAWngOl7jXng8GDoWAjoPAT AG6ljacjEqC2DTFe2RZiAjNRO47aLWICXdKPoCs+juwMH5Pa+Ozd34h/omGOGj1YcaA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=ejUkxk+u; arc=none smtp.remote-ip=213.97.179.56 Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EAE28606A31A; Tue, 1 Dec 2026 10:57:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EAE28606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=ejUkxk+u DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:From:Cc:To:Subject: MIME-Version:Date:Message-ID:From:Reply-To; bh=mkvWh19CVFdTOrC4Z0qNqPw1uRirP0iV9ZNaoeqiTeM=; t=1796122648; x=2111482648; b=ejUkxk+un0KlwPA6Zd+08hJWeseiXb1fUerj7tVR+OmMyBLIrwuK7zYlBuT9EdYI1Qbt8+By171 AgOwd+fGnar6JxXjyut+XuddeK5Z6rvJQ2kuq9huVkBJKwrUHMlWB1PvxDGnH2BXgJBXd67KX7xSU Ag/O4ks7M5RO9jA7b6s5Vnm9+muYxqZoFibXnK/q7hWsTRQgMkgjvehZg02FOXAHr/24Go3ujKZS6 6PWD8h3JhzBECcO8DZhjRM6pVfvLoIBQPt3vKjdPvQ2hb3khhjoMnEGsbADH8vJxlLDibxgan62OR kafMESfmdLcS+zi1YZt00q5ZM481bQfHkR6A==; Received: from [81.79.79.1] (helo=[192.168.0.116]) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_128_GCM:128) (Exim) id 1xYLXu-00000000bjQ-3LWg; Tue, 01 Dec 2026 11:57:14 +0100 Message-ID: Date: Tue, 1 Dec 2026 10:57:13 +0000 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 1/3] dma-buf: dma-fence: Fix timestamp misordering To: Philipp Stanner , Sumit Semwal , =?UTF-8?Q?Christian_K=C3=B6nig?= , Alex Deucher , David Airlie , Simona Vetter , Matthew Brost , Danilo Krummrich , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , 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 References: <20261201091712.716354-2-phasta@kernel.org> <20261201091712.716354-3-phasta@kernel.org> Content-Language: en-GB From: Tvrtko Ursulin In-Reply-To: <20261201091712.716354-3-phasta@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 01/12/2026 09:17, Philipp Stanner wrote: > 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; LGTM. Reviewed-by: Tvrtko Ursulin Regards, Tvrtko