[PATCH 7/7] drm/bridge: prevent encoder chain changes while iterating in drm_atomic_bridge_chain_post_disable/pre_enable()

Luca Ceresoli
2025-09-26T16:00:59Z

Take the encoder chain mutex while iterating over the encoder chain in
drm_atomic_bridge_chain_pre_enable() and
drm_atomic_bridge_chain_post-disable().

These functions have nested list_for_each_*() loops, which makes them
complicated. list_for_each_entry_from() loops could be replaced by
drm_for_each_bridge_in_chain_from(), but it would not work in a nested
way. Besides, there is no "_reverse" variant of
drm_for_each_bridge_in_chain_from().

Keep code simple and readable by explicitly locking around the outer
loop. Thankfully there are no return points inside the loops, so the change
is trivial and readable.

Signed-off-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
---
drivers/gpu/drm/drm_bridge.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c
index 66c0a80db8426ffb360248895cfe2a11d6007ed7..00e0a88128d0eeabf25911591ad8c482d6c1caf8 100644
--- a/drivers/gpu/drm/drm_bridge.c
+++ b/drivers/gpu/drm/drm_bridge.c
@@ -765,6 +765,7 @@ void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,

encoder = bridge->encoder;

+ drm_encoder_chain_lock(encoder);
list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
limit = NULL;

@@ -813,6 +814,7 @@ void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
/* Jump all bridges that we have already post_disabled */
bridge = limit;
}
+ drm_encoder_chain_unlock(encoder);
}
EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);

@@ -859,6 +861,7 @@ void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,

encoder = bridge->encoder;

+ drm_encoder_chain_lock(encoder);
list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
if (iter->pre_enable_prev_first) {
next = iter;
@@ -901,6 +904,7 @@ void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
if (iter == bridge)
break;
}
+ drm_encoder_chain_unlock(encoder);
}
EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);

--
2.51.0