Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Philipp Stanner" <phasta@kernel.org>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Christian König" <christian.koenig@amd.com>,
	"Simona Vetter" <simona.vetter@ffwll.ch>,
	"Chris Wilson" <chris@chris-wilson.co.uk>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Gary Guo" <gary@garyguo.net>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tvrtko Ursulin" <tvrtko.ursulin@igalia.com>
Cc: <linux-media@vger.kernel.org>, <dri-devel@lists.freedesktop.org>,
	<linux-kernel@vger.kernel.org>, <stable@vger.kernel.org>
Subject: Re: [PATCH v2] dma-buf/dma-fence: Prevent misordering for error checks
Date: Wed, 18 Nov 2026 12:59:54 +0000	[thread overview]
Message-ID: <DMYCSXNA6VQ9.22FS1PMBBYWRO@garyguo.net> (raw)
In-Reply-To: <20261118110914.1471416-2-phasta@kernel.org>

On Wed Nov 18, 2026 at 11:09 AM GMT, Philipp Stanner wrote:
> Many users of dma_fence do things like:
>
> // Thread A
> dma_fence_set_error(f, -EINVAL);
> dma_fence_signal(f); // <- Takes lock, which 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.
>
> Though dma_fence_get_status() does take the fence lock, it is equally
> broken because the 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 <phasta@kernel.org>
> ---
> Changes in v2:
>   - Use classic C style comments. (Christian)
> ---
>  include/linux/dma-fence.h | 13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> index dd07d128adc3..a3fa1341765e 100644
> --- a/include/linux/dma-fence.h
> +++ b/include/linux/dma-fence.h
> @@ -496,7 +496,18 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence);
>  static inline bool
>  dma_fence_test_signaled_flag(struct dma_fence *fence)
>  {
> -	return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
> +	/*
> +	 * Best-effort ordering of the locklessly evaluated signaled-bit,
> +	 * especially so that reads of fence->error are not misordered.

I am not sure if this should focus on the error, or it should just provide a
general guarantee that anything happens before dma_fence_signal is visible when
signal test returns true. I would lean on the latter?

> +	 *
> +	 * The release counterpart is typically ensured like this:
> +	 *   a) Error codes must only be set before a fence signals and
> +	 *   b) The fence's spinlock's barriers effective in dma_fence_signal()
> +	 *      ensure ordering.

Spinlock has nothing to do with this. The only thing that guarantees the
ordering is test_and_set_bit in (c).

So probably easier just to say something like

    /* Pairs with test_and_set_bit() in dma_fence_signal_timestamp_locked */

Best,
Gary

> +	 *   c) For users of dma_fence_signal_locked(), the barrier implied in
> +	 *      test_and_set_bit() should do the trick.
> +	 */
> +	return test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
>  }
>  
>  /**



  reply	other threads:[~2026-11-18 12:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 11:09 [PATCH v2] dma-buf/dma-fence: Prevent misordering for error checks Philipp Stanner
2026-11-18 12:59 ` Gary Guo [this message]
2026-11-18 13:15   ` Philipp Stanner
2026-11-18 13:49     ` Gary Guo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DMYCSXNA6VQ9.22FS1PMBBYWRO@garyguo.net \
    --to=gary@garyguo.net \
    --cc=aliceryhl@google.com \
    --cc=chris@chris-wilson.co.uk \
    --cc=christian.koenig@amd.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=phasta@kernel.org \
    --cc=simona.vetter@ffwll.ch \
    --cc=stable@vger.kernel.org \
    --cc=sumit.semwal@linaro.org \
    --cc=tvrtko.ursulin@igalia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox