From nobody Mon Dec 1 23:05:38 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 C06D826D4F9; Wed, 26 Nov 2025 13:20:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764163209; cv=none; b=Y3F+NTfK6qDAOcnqAmYz1OXKgKXh3avqgwZXym4zOYWlaxPap+ZPxFOUYLz08FG8EiieQZpfb8CERFlJxyrGONVJK2XkWjP7QbJvZQWgT2GMoJzCoJg+trdW/Kpi0rmuA03GJfXGg5lj2yBuOhhFBJZUjZ42A/M/+S80I1SJCWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764163209; c=relaxed/simple; bh=KbS42U1qFlYFL5sleuJnRbKXaGff7v361nDoJWGgUJc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bm3OG3grp93BoIs+KVbquDnXjgFGUjLIPJmNQCbIOOcXGYK4LGv7Ya1IY0LB8JxhXTsY4Outo5zp3vld7qJ8Yi3XPrI7eDjNj/J82Q0sT1S7BrrheOGET31K5+KnHACT8WsVfkp2uB+Vc5UPOB/6JzjufTAUGx99ZEIitaxeG1M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bg/rra/0; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bg/rra/0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1BD03C116B1; Wed, 26 Nov 2025 13:20:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1764163208; bh=KbS42U1qFlYFL5sleuJnRbKXaGff7v361nDoJWGgUJc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bg/rra/0CVDd38GC+JM3UPYJMGAtWN6a2xKH5FHkwEKvc4UK8v9gdVQz4nEMq4VU1 retBOj2Xy0M0UTUvbhGqfazajUowYQ6uVv/WHzEQg2uoFc+Z8GUbglEIowKIBDKM+H yUI8DvjgoRw2Dwz2WgAyoDQDCjziDkXyG+ekwhtPLtuYYGjcGmQIXx0fiSMk/mfXpI MGUn9Bo9HTGWjLl9GCh1pDocnWuQ5lU13TgZ5mQPGzzUh2pUzBVuk/RoAACdZdiun3 OqkujnIQo7bixifkEvnxzq7a2mlIC26vbwJNL8YXyP7oiOkovBBYX5BxIEnEeeFRKo k7t2S97/02ArA== From: Philipp Stanner To: Sumit Semwal , Gustavo Padovan , =?UTF-8?q?Christian=20K=C3=B6nig?= , Felix Kuehling , Alex Deucher , David Airlie , Simona Vetter , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , Huang Rui , Matthew Auld , Matthew Brost , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Lucas De Marchi , =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= Cc: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, amd-gfx@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, intel-xe@lists.freedesktop.org, rust-for-linux@vger.kernel.org, Philipp Stanner , Tvrtko Ursulin Subject: [PATCH 1/6] dma-buf/dma-fence: Add dma_fence_test_signaled_flag() Date: Wed, 26 Nov 2025 14:19:10 +0100 Message-ID: <20251126131914.149445-3-phasta@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20251126131914.149445-2-phasta@kernel.org> References: <20251126131914.149445-2-phasta@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The dma_fence framework checks at many places whether the signaled flag of a fence is already set. The code can be simplified and made more readable by providing a helper function for that. Add dma_fence_test_signaled_flag(), which only checks whether a fence is signaled. Use it internally. Suggested-by: Tvrtko Ursulin Signed-off-by: Philipp Stanner Reviewed-by: Matthew Brost --- drivers/dma-buf/dma-fence.c | 19 +++++++++---------- include/linux/dma-fence.h | 24 ++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c index 39e6f93dc310..25117a906846 100644 --- a/drivers/dma-buf/dma-fence.c +++ b/drivers/dma-buf/dma-fence.c @@ -372,8 +372,7 @@ int dma_fence_signal_timestamp_locked(struct dma_fence = *fence, =20 lockdep_assert_held(fence->lock); =20 - if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, - &fence->flags))) + if (unlikely(dma_fence_test_signaled_flag(fence))) return -EINVAL; =20 /* Stash the cb_list before replacing it with the timestamp */ @@ -545,7 +544,7 @@ void dma_fence_release(struct kref *kref) trace_dma_fence_destroy(fence); =20 if (!list_empty(&fence->cb_list) && - !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { + !dma_fence_test_signaled_flag(fence)) { const char __rcu *timeline; const char __rcu *driver; unsigned long flags; @@ -602,7 +601,7 @@ static bool __dma_fence_enable_signaling(struct dma_fen= ce *fence) was_set =3D test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags); =20 - if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) + if (dma_fence_test_signaled_flag(fence)) return false; =20 if (!was_set && fence->ops->enable_signaling) { @@ -666,7 +665,7 @@ int dma_fence_add_callback(struct dma_fence *fence, str= uct dma_fence_cb *cb, if (WARN_ON(!fence || !func)) return -EINVAL; =20 - if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { + if (dma_fence_test_signaled_flag(fence)) { INIT_LIST_HEAD(&cb->node); return -ENOENT; } @@ -783,7 +782,7 @@ dma_fence_default_wait(struct dma_fence *fence, bool in= tr, signed long timeout) =20 spin_lock_irqsave(fence->lock, flags); =20 - if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) + if (dma_fence_test_signaled_flag(fence)) goto out; =20 if (intr && signal_pending(current)) { @@ -800,7 +799,7 @@ dma_fence_default_wait(struct dma_fence *fence, bool in= tr, signed long timeout) cb.task =3D current; list_add(&cb.base.node, &fence->cb_list); =20 - while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) { + while (!dma_fence_test_signaled_flag(fence) && ret > 0) { if (intr) __set_current_state(TASK_INTERRUPTIBLE); else @@ -832,7 +831,7 @@ dma_fence_test_signaled_any(struct dma_fence **fences, = uint32_t count, =20 for (i =3D 0; i < count; ++i) { struct dma_fence *fence =3D fences[i]; - if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { + if (dma_fence_test_signaled_flag(fence)) { if (idx) *idx =3D i; return true; @@ -1108,7 +1107,7 @@ const char __rcu *dma_fence_driver_name(struct dma_fe= nce *fence) RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "RCU protection is required for safe access to returned string"); =20 - if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) + if (!dma_fence_test_signaled_flag(fence)) return fence->ops->get_driver_name(fence); else return "detached-driver"; @@ -1140,7 +1139,7 @@ const char __rcu *dma_fence_timeline_name(struct dma_= fence *fence) RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "RCU protection is required for safe access to returned string"); =20 - if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) + if (!dma_fence_test_signaled_flag(fence)) return fence->ops->get_timeline_name(fence); else return "signaled-timeline"; diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h index 64639e104110..19972f5d176f 100644 --- a/include/linux/dma-fence.h +++ b/include/linux/dma-fence.h @@ -401,6 +401,26 @@ void dma_fence_enable_sw_signaling(struct dma_fence *f= ence); const char __rcu *dma_fence_driver_name(struct dma_fence *fence); const char __rcu *dma_fence_timeline_name(struct dma_fence *fence); =20 +/* + * dma_fence_test_signaled_flag - Only check whether a fence is signaled y= et. + * @fence: the fence to check + * + * This function just checks whether @fence is signaled, without interacti= ng + * with the fence in any way. The user must, therefore, ensure through oth= er + * means that fences get signaled eventually. + * + * This function uses test_bit(), which is thread-safe. Naturally, this fu= nction + * should be used opportunistically; a fence could get signaled at any mom= ent + * after the check is done. + * + * 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); +} + /** * dma_fence_is_signaled_locked - Return an indication if the fence * is signaled yet. @@ -418,7 +438,7 @@ const char __rcu *dma_fence_timeline_name(struct dma_fe= nce *fence); static inline bool dma_fence_is_signaled_locked(struct dma_fence *fence) { - if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) + if (dma_fence_test_signaled_flag(fence)) return true; =20 if (fence->ops->signaled && fence->ops->signaled(fence)) { @@ -448,7 +468,7 @@ dma_fence_is_signaled_locked(struct dma_fence *fence) static inline bool dma_fence_is_signaled(struct dma_fence *fence) { - if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) + if (dma_fence_test_signaled_flag(fence)) return true; =20 if (fence->ops->signaled && fence->ops->signaled(fence)) { --=20 2.49.0