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 1BCEE606A32A for ; Fri, 20 Nov 2026 11:16:48 +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=luUsY8wS Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 514FB10E6E0; Fri, 20 Nov 2026 11:16:41 +0000 (UTC) Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id 54F5410E6E0 for ; Fri, 20 Nov 2026 11:16:35 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 9EFB8462CD; Fri, 20 Nov 2026 11:16:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0E411F000CA; Fri, 20 Nov 2026 11:16:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795173393; bh=1rA97F5/npt/N9vCtRHW4c9+9dN2t8X2ZribjEzABLY=; h=From:To:Cc:Subject:Date; b=luUsY8wSZU63nKy52IFDDfqhhiq+t+o+hbbc/+Gd5ufIEz12jspoREg0hXHaDM0g2 4vywmRiBQTJA9vtfNKZdpz/vBuQnEOMbD6wj4/QdQZAXihIoqmYtXdFWdonlCJ1TD1 WWZJcI4PnLlKrTiUOX+vl+u2U7jCCeMDWw5F2AsDs5RwFz15dHivHC2RoB75rJBw9N gdOqhTarpq+gXjliwH93PoH5hpi3br5bYXk4lgcuOgxcptm8Twc+rtDvmJ1vN6pU3m 3TwCyECKZPZxWoO6scqt8iVrSMKu0aFfNJyF3SEsoD2FHnZSY83xIaM9CV1iMXhbWq AjvfBKriIPurA== From: Philipp Stanner To: Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Philipp Stanner , Simona Vetter , Chris Wilson , Danilo Krummrich , Gary Guo , Alice Ryhl , Daniel Almeida , Tvrtko Ursulin , "Paul E . McKenney" Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v4] dma-buf/dma-fence: Prevent misordering for error checks Date: Fri, 20 Nov 2026 12:16:16 +0100 Message-ID: <20261120111615.2940647-2-phasta@kernel.org> X-Mailer: git-send-email 2.56.0 X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: , Sender: "dri-devel" 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=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 Many users of dma_fence do things like: // Thread A dma_fence_set_error(f, -EINVAL); dma_fence_signal(f); // <- test_and_set_bit() orders against the above. // Thread B if (dma_fence_is_signaled(f)) // <- Does test_bit(), no ordering. pr_info("err: %i\n", f->error); // <- Can be loaded first. There is no memory ordering between reading of the signaled-bit and reading of the error code. This can result in outdated, wrong error codes being evaluated by the user. That's a significant bug when we consider that fences' purpose is to propagate the state of jobs running on hardware. dma_fence_get_status() does take the fence lock, presumably to ensure ordering, but it is broken, too, because the read misordering can simply happen within the critical section. It's not entirely clear since when this bug exists, but at least since dma_fence_get_status() was added, which is why its birth commit is marked as the Fixes commit here. Ensure ordering for everyone evaluating the error code centrally by using test_bit_acquire() in dma_fence_test_signaled_flag(). Cc: stable@vger.kernel.org Fixes: d6c99f4bf093 ("dma-fence: Wrap querying the fence->status") Signed-off-by: Philipp Stanner --- Changes in v4: - Keep the hint at the function being opportunistic. (Christian) Changes in v3: - Be more precise in code comments and commit message about how the ordering works. (Gary) - Update the function's documentation to inform about ordering semantics. --- include/linux/dma-fence.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h index dd07d128adc3..64330bf52561 100644 --- a/include/linux/dma-fence.h +++ b/include/linux/dma-fence.h @@ -487,16 +487,23 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence); * with the fence in any way. The user must, therefore, ensure through other * means that fences get signaled eventually. * - * This function uses test_bit(), which is thread-safe. Naturally, this function - * should be used opportunistically; a fence could get signaled at any moment - * after the check is done. + * Naturally, this function should be used opportunistically; a fence could get + * signaled at any moment after the check is done. + * + * This function provides acquire ordering. This means that all stores + * preceding the signaling of a fence are visible in correct order to users of + * this function. * * Return: true if signaled, false otherwise. */ static inline bool dma_fence_test_signaled_flag(struct dma_fence *fence) { - return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags); + /* + * The release counterpart is the test_and_set_bit() in + * dma_fence_signal_timestamp_locked(). + */ + return test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags); } /** -- 2.56.0