From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57D1F3AB28C for ; Fri, 12 Jun 2026 10:43:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781261005; cv=none; b=qOwi59GgdHefGsdiUwn96T3zRPF/mqxKuXP1JWQ0znFAGouI0DKtqRUpacMy8xq49bMqKWonRdMlJaKGJWaxUffUkC95NBMQNoZMiFtK9zosAVAS8/OtfxsCBi6UiH2tteU70BmM4QsoeW1czz1tpJTlu4oyX8B+Gw/ZFIw4FRQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781261005; c=relaxed/simple; bh=YzhpR/AKhwB+FM6wIEyzp81z9V1G6UG9oGTNQKLg1dk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=BEuw1H/6JgJHrPN0NkCbBsHOooJwj7sDdoEDU1tOxpKgM8OLRoTWaVXhhFN6mN2sisJCzdsoGdkqL2bza3zdnnA3ud8ElZIowJN/dDMos+Y2AwWmkcyuJaOYqZEdjk79TUj4iZYWNsQYywpWQLOxMTZjC+TLVyNaVtrnZMnZ/Rs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LRnJAoBx; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LRnJAoBx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E53321F000E9; Fri, 12 Jun 2026 10:43:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1781261001; bh=H4drlEs7mwLSm0ngtWZFdgIn1T1tuqSEWbBzTCWzvu8=; h=From:To:Cc:Subject:Date; b=LRnJAoBxc0NYKhdlNg8pyExFMQ1VQexXaygX4uk1JV5sKTwa6P5ibKMk029LcNl9b WqeTFrfEpy5wO9n5wOcoHNjwOb8Op86VVObGV3nYyevL8BEO2Qvh9lmdmgFn1747sH ecIkcLsADIOu3vRweEFE0px9gOhaZMk1ALbTXHGmjFj+IwB9RzooDaGIe2QsknlR70 Z2Q7wUIkEr2toknvgvadlIm8x3kPkZ/N8DPwL6gxn2YXjyrafGWEugC+guYNOsCNb6 wsOrDTTtgVYyaQoX/TIVRx2E+esX2p0FAhispV+6PrMfvII8MCYEKEPoyiJWnoV6rX 4xsupVYSFOiRQ== From: Philipp Stanner To: Danilo Krummrich , Maarten Lankhorst , David Airlie , Simona Vetter , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Tvrtko Ursulin , Boris Brezillon , "Paul E . McKenney" Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Philipp Stanner Subject: [RFC PATCH] dma-buf/dma_fence: Make races for dma_fence_is_signaled() less likely Date: Fri, 12 Jun 2026 12:42:52 +0200 Message-ID: <20260612104251.2264707-2-phasta@kernel.org> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit dma_fence_is_signaled() returns whether a fence has been signaled already. That function contains a fast path opportunistic check which is not guarded by the lock and, according to Christian, cannot be guarded by the lock without causing a massive performance regression. This now means that dma_fence_is_signaled() can return true WHILE the fence callbacks are still being executed. This is razy and has lead to at least one bug solved in: commit c8a5d5ea3ba6 ("nouveau: fix client work fence deletion race") Make this race impossible, by simply setting the bit only once the callbacks are actually completed. Signed-off-by: Philipp Stanner --- drivers/dma-buf/dma-fence.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c index c7ea1e75d38a..2416cc86ce93 100644 --- a/drivers/dma-buf/dma-fence.c +++ b/drivers/dma-buf/dma-fence.c @@ -359,8 +359,19 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence, dma_fence_assert_held(fence); - if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, - &fence->flags))) + /* + * First test the bit, so we don't signal an already signaled fence again. + * The lock protects against multiple parties setting the bit. The bit + * is then set at the end of the function. + * + * The background is that there is a fast path check in + * dma_fence_is_signaled() which does not use lock protection and can + * return true *while* the fence callbacks are still executing. + * + * This fast path check supposedly cannot be guarded by the lock because + * of significant performance regressions. + */ + if (unlikely(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))) return; trace_dma_fence_signaled(fence); @@ -384,6 +395,9 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence, INIT_LIST_HEAD(&cur->node); cur->func(fence, cur); } + + // TODO: we need some barrier here, don't we? + set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags); } EXPORT_SYMBOL(dma_fence_signal_timestamp_locked); -- 2.54.0