drm/bridge: protect encoder bridge chain with a mutex
Subsystems (3)
Luca Ceresoli
reviewerv2 cover drm/bridge: protect encoder bridge chain with a mutex
This series ensures that the bridge chain of the encoder will not be modified while some other concurrent code flows are iterating over it.
This is part of the work towards removal of bridges from a still existing DRM pipeline without use-after-free. The grand plan was discussed in [1]. Here's the work breakdown (➜ marks the current series):
1. ➜ add refcounting to DRM bridges (struct drm_bridge)
(based on devm_drm_bridge_alloc() [0])
A. ✔ add new alloc API and refcounting (v6.16)
B. ✔ convert all bridge drivers to new API (v6.17)
C. ✔ kunit tests (v6.17)
D. ✔ add get/put to drm_bridge_add/remove() + attach/detach()
and warn on old allocation pattern (v6.17)
E. ➜ add get/put on drm_bridge accessors
1. ✔ drm_bridge_chain_get_first_bridge() + add a cleanup action (drm-misc-next)
2. ✔ drm_bridge_get_prev_bridge() (drm-misc-next)
3. ✔ drm_bridge_get_next_bridge() (drm-misc-next)
4. ✔ drm_for_each_bridge_in_chain() (drm-misc-next)
5. ✔ drm_bridge_connector_init (drm-misc-next)
6. ➜ protect encoder bridge chain with a mutex
7. of_drm_find_bridge
8. drm_of_find_panel_or_bridge, *_of_get_bridge
9. … enforce drm_bridge_add before drm_bridge_attach
F. ✔ debugfs improvements
1. ✔ add top-level 'bridges' file (v6.16)
2. ✔ show refcount and list removed bridges (drm-misc-next)
2. … handle gracefully atomic updates during bridge removal
3. … DSI host-device driver interaction
4. ✔ removing the need for the "always-disconnected" connector
5. finish the hotplug bridge work, moving code to the core and potentially removing the hotplug-bridge itself (this needs to be clarified as points 1-3 are developed)
The per-encoder bridge chain is currently assumed to be static once it is fully initialized. Work is in progress to add hot-pluggable bridges, breaking that assumption.
With hotplug and especially hot-unplug, bridges will be added and removed without notice, and thus be added/removed to/from the encoder chain in drm_bridge_attach/detach(), concurrently to the code iterating on the chain. This can result in disruption of the code iterating over the chain. The rationale is explained by a detailed example in patch 2.
Avoid bugs by introducing a mutex to make list insertion, removal and iterations mutually exclusive.
[1] https://lore.kernel.org/lkml/[email protected]/#t
---
Changes in v2:
- Improve commit messages and add documentation as per v1 review
- Patch 4: fixed infinite loop when encoder->bridge_chain is empty
- Link to v1: https://lore.kernel.org/r/20250926-drm-bridge-alloc-encoder-chain-mutex-v1-0-23b62c47356a@bootlin.com
---
Luca Ceresoli (7):
drm/encoder: add mutex to protect the bridge chain
drm/encoder: drm_encoder_cleanup: take chain mutex while tearing down drm/bridge: lock the encoder bridge chain mutex during insertion
drm/bridge: lock the encoder chain in scoped for_each loops
drm/bridge: prevent encoder chain changes while iterating with list_for_each_entry_from() drm/bridge: prevent encoder chain changes while iterating with list_for_each_entry_reverse() drm/bridge: prevent encoder chain changes in pre_enable/post_disable
drivers/gpu/drm/drm_bridge.c | 51 ++++++++++++++++++----------------- drivers/gpu/drm/drm_encoder.c | 4 +++ include/drm/drm_bridge.h | 62 +++++++++++++++++++++++-------------------- include/drm/drm_encoder.h | 39 +++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 53 deletions(-)
---
base-commit: 0f2efbe6d8305b91c9b2c92ebaf8c24a614bc305
change-id: 20250925-drm-bridge-alloc-encoder-chain-mutex-b78d62085ee5
Best regards,
Signature · 1 line
Luca Ceresoli <[email protected]>
Luca Ceresoli
reviewerPATCH v2 1/7 drm/encoder: add mutex to protect the bridge chain
The per-encoder bridge chain is currently assumed to be static once it is fully initialized. Work is in progress to add hot-pluggable bridges, breaking that assumption.
With bridge removal, the encoder chain can change without notice, removing tail bridges. This can be problematic while iterating over the chain.
Add a mutex to be taken whenever looping or changing the encoder chain.
Also add two APIs to lock/unlock the mutex without the need to manipulate internal struct drm_encoder fields.
---
Changes in v2:
- Added documentation to new APIs
--- drivers/gpu/drm/drm_encoder.c | 2 ++ include/drm/drm_encoder.h | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+)
drivers/gpu/drm/drm_encoder.c +2 -0
@@ -129,6 +129,7 @@ static int __drm_encoder_init(struct drm_device *dev, }INIT_LIST_HEAD (&encoder->bridge_chain); +mutex_init (&encoder->bridge_chain_mutex);list_add_tail (&encoder->head, &dev->mode_config.encoder_list); encoder->index = dev->mode_config.num_encoder++;
@@ -202,6 +203,7 @@ void drm_encoder_cleanup(struct drm_encoder *encoder)kfree (encoder->name);list_del (&encoder->head); dev->mode_config.num_encoder--; +mutex_destroy (&encoder->bridge_chain_mutex);memset (encoder,0 ,sizeof (*encoder)); }
include/drm/drm_encoder.h +39 -0
@@ -25,6 +25,7 @@#include <linux/list.h> #include <linux/ctype.h> +#include <linux/mutex.h> #include <drm/drm_crtc.h> #include <drm/drm_mode.h> #include <drm/drm_mode_object.h>
@@ -189,6 +190,9 @@ struct drm_encoder {*/ struct list_head bridge_chain; +/** @bridge_chain_mutex: protect bridge_chain from changes while iterating */ +struct mutex bridge_chain_mutex; +const struct drm_encoder_funcs *funcs;const struct drm_encoder_helper_funcs *helper_private;
@@ -319,6 +323,41 @@ static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev,return mo ?obj_to_encoder (mo) :NULL ; } +/** +* drm_encoder_chain_lock - lock the encoder bridge chain +* @encoder: encoder whose bridge chain must be locked +* +* Locks the mutex protecting the bridge chain from concurrent access. +* To be called by code modifying ot iterating over the bridge chain to +* prevent the list from changing while iterating over it. +* Call drm_encoder_chain_unlock() when done to unlock the mutex. +* +* Returns: +* Pointer to @encoder. Useful to lock the chain and then operate on the +* in the same statement, e.g.: +* list_first_entry_or_null(&drm_encoder_chain_lock(encoder)->bridge_chain) +*/ +static inline struct drm_encoder *drm_encoder_chain_lock (struct drm_encoder *encoder) +{ +if (!WARN_ON_ONCE (!encoder)) +mutex_lock (&encoder->bridge_chain_mutex); + +return encoder; +} + +/** +* drm_encoder_chain_unlock - unlock the encoder bridge chain +* @encoder: encoder whose bridge chain must be unlocked +* +* Unlocks the mutex protecting the bridge chain from concurrent access, +* matching drm_encoder_chain_lock(). +*/ +static inline void drm_encoder_chain_unlock (struct drm_encoder *encoder) +{ +if (!WARN_ON_ONCE (!encoder)) +mutex_unlock (&encoder->bridge_chain_mutex); +} +void drm_encoder_cleanup (struct drm_encoder *encoder);/**
Signature · 1 line
2.51.0
Re: [PATCH v2 1/7] drm/encoder: add mutex to protect the bridge chain
Quoted 74 lines · Luca Ceresoli
The per-encoder bridge chain is currently assumed to be static once it is fully initialized. Work is in progress to add hot-pluggable bridges, breaking that assumption.
With bridge removal, the encoder chain can change without notice, removing tail bridges. This can be problematic while iterating over the chain.
Add a mutex to be taken whenever looping or changing the encoder chain.
Also add two APIs to lock/unlock the mutex without the need to manipulate internal struct
drm_encoderfields.Signed-off-by Luca Ceresoli <[email protected]>---
Changes in v2:
- Added documentation to new APIs--- drivers/gpu/drm/drm_encoder.c | 2 ++ include/drm/drm_encoder.h | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+)drivers/gpu/drm/drm_encoder.c +2
-0@@ -129,6 +129,7 @@ static int __drm_encoder_init(struct drm_device *dev, }INIT_LIST_HEAD (&encoder->bridge_chain); +mutex_init (&encoder->bridge_chain_mutex);list_add_tail (&encoder->head, &dev->mode_config.encoder_list); encoder->index = dev->mode_config.num_encoder++;@@ -202,6 +203,7 @@ void drm_encoder_cleanup(struct drm_encoder *encoder)kfree (encoder->name);list_del (&encoder->head); dev->mode_config.num_encoder--; +mutex_destroy (&encoder->bridge_chain_mutex);memset (encoder,0 ,sizeof (*encoder)); }include/drm/drm_encoder.h +9
-0@@ -25,6 +25,7 @@#include <linux/list.h> #include <linux/ctype.h> +#include <linux/mutex.h> #include <drm/drm_crtc.h> #include <drm/drm_mode.h> #include <drm/drm_mode_object.h> @@ -189,6 +190,9 @@ struct drm_encoder {*/ struct list_head bridge_chain; +/** @bridge_chain_mutex: protect bridge_chain from changes while iterating */ +struct mutex bridge_chain_mutex; +const struct drm_encoder_funcs *funcs;const struct drm_encoder_helper_funcs *helper_private;@@ -319,6 +323,41 @@ static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev,return mo ?obj_to_encoder (mo) :NULL ; } +/** +* drm_encoder_chain_lock - lock the encoder bridge chain +* @encoder: encoder whose bridge chain must be locked +* +* Locks the mutex protecting the bridge chain from concurrent access.
include/drm/drm_encoder.h static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev, · line 340
Quoted 10 lines
include/drm/drm_encoder.h +10
-0+* To be called by code modifying ot iterating over the bridge chain to +* prevent the list from changing while iterating over it. +* Call drm_encoder_chain_unlock() when done to unlock the mutex. +* +* Returns: +* Pointer to @encoder. Useful to lock the chain and then operate on the +* in the same statement, e.g.: +* list_first_entry_or_null(&drm_encoder_chain_lock(encoder)->bridge_chain) +*/ +static inline struct drm_encoder *drm_encoder_chain_lock (struct drm_encoder *encoder)
What is the use case for these wrappers? I'm asking especially since you almost never use the return value of the _lock() one. I think with scoped_guard you can get the same kind of code without needing extra API or extra wrappers.
Quoted 27 lines
include/drm/drm_encoder.h +20
-0+{ +if (!WARN_ON_ONCE (!encoder)) +mutex_lock (&encoder->bridge_chain_mutex); + +return encoder; +} + +/** +* drm_encoder_chain_unlock - unlock the encoder bridge chain +* @encoder: encoder whose bridge chain must be unlocked +* +* Unlocks the mutex protecting the bridge chain from concurrent access, +* matching drm_encoder_chain_lock(). +*/ +static inline void drm_encoder_chain_unlock (struct drm_encoder *encoder) +{ +if (!WARN_ON_ONCE (!encoder)) +mutex_unlock (&encoder->bridge_chain_mutex); +} +void drm_encoder_cleanup (struct drm_encoder *encoder);/** Signature · 2 lines
-- 2.51.0
Signature · 2 lines
With best wishes Dmitry
Luca Ceresoli
reviewerRe: [PATCH v2 1/7] drm/encoder: add mutex to protect the bridge chain
Hi Dmitry,
Quoted 16 lines · Dmitry Baryshkov
Quoted 16 lines
include/drm/drm_encoder.h +12
-0@@ -319,6 +323,41 @@ static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev,return mo ?obj_to_encoder (mo) :NULL ; } +/** +* drm_encoder_chain_lock - lock the encoder bridge chain +* @encoder: encoder whose bridge chain must be locked +* +* Locks the mutex protecting the bridge chain from concurrent access. +* To be called by code modifying ot iterating over the bridge chain to +* prevent the list from changing while iterating over it. +* Call drm_encoder_chain_unlock() when done to unlock the mutex. +* +* Returns: +* Pointer to @encoder. Useful to lock the chain and then operate on the +* in the same statement, e.g.:
Quoted 8 lines
include/drm/drm_encoder.h static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev, · line 340
Quoted 3 lines
include/drm/drm_encoder.h +3
-0+* list_first_entry_or_null(&drm_encoder_chain_lock(encoder)->bridge_chain) +*/ +static inline struct drm_encoder *drm_encoder_chain_lock (struct drm_encoder *encoder)What is the use case for these wrappers? I'm asking especially since you almost never use the return value of the
_lock()one. I think withscoped_guardyou can get the same kind of code without needing extra API or extra wrappers.
For two reasons.
One is to avoid drm_encoder users to need to access internal fields (encapsulation, in object-oriented jargon). But if I read correctly between the lines of your question, it is not worth because drm_bridge and drm_encoder are already interdependent?
The second is that the C language spec sets tight constraints to the drm_for_each_bridge_in_chain_scoped(). The macro must look like:
#define drm_for_each_bridge_in_chain_scoped(encoder, bridge) \
for (struct drm_bridge *bridge = <FOO>; clause-2; clause-3)
'----------- clause-1 ----------'
clause-1 must:
* declare a 'struct drm_bridge *' variable (the loop cursor)
* initialize it via <FOO> which thus must be a rvalue of type 'struct drm_bridge *' (<FOO> must be a function or a macro, as a variable with the correct value is not available)
* use the struct drm_encoder * as its sole input
* lock the encoder chain mutex
* get a reference to the bridge (as Maxime requested)
* ensure the bridge reference is put and the mutex is released on break and return (clause-3 can't do that)
Given the above, we still need a function that locks the encoder chain mutex and returns the encoder (bullets 3 and 4), like drm_encoder_chain_lock(). I'm OK with removing drm_encoder_chain_lock() and replace it with an internal macro or function in drm_bridge.h though.
However I'm not sure how to use scoped_guard here because it doesn't return a pointer that can then be passed further. Basically we are constrained to have a chain of function or macro calls, each eating the result of the inner one, with the outer one returning a bridge pointer for the loop cursor variable. There might be some macro magic I'm missing, in such case don't hesitate to mention that.
Best regards,
Luca
Signature · 3 lines
Luca Ceresoli, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Luca Ceresoli
reviewerRe: [PATCH v2 1/7] drm/encoder: add mutex to protect the bridge chain
Hi Dmitry,
Quoted 60 lines · Luca Ceresoli
Hi Dmitry,
Quoted 16 lines · Dmitry Baryshkov
Quoted 16 lines
include/drm/drm_encoder.h +12
-0@@ -319,6 +323,41 @@ static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev,return mo ?obj_to_encoder (mo) :NULL ; } +/** +* drm_encoder_chain_lock - lock the encoder bridge chain +* @encoder: encoder whose bridge chain must be locked +* +* Locks the mutex protecting the bridge chain from concurrent access. +* To be called by code modifying ot iterating over the bridge chain to +* prevent the list from changing while iterating over it. +* Call drm_encoder_chain_unlock() when done to unlock the mutex. +* +* Returns: +* Pointer to @encoder. Useful to lock the chain and then operate on the +* in the same statement, e.g.: Quoted 8 lines
include/drm/drm_encoder.h static inline struct drm_encoder *drm_encoder_find(struct drm_device *dev, · line 340
Quoted 3 lines
include/drm/drm_encoder.h +3
-0+* list_first_entry_or_null(&drm_encoder_chain_lock(encoder)->bridge_chain) +*/ +static inline struct drm_encoder *drm_encoder_chain_lock (struct drm_encoder *encoder)What is the use case for these wrappers? I'm asking especially since you almost never use the return value of the
_lock()one. I think withscoped_guardyou can get the same kind of code without needing extra API or extra wrappers.For two reasons.
One is to avoid
drm_encoderusers to need to access internal fields (encapsulation, in object-oriented jargon). But if I read correctly between the lines of your question, it is not worth becausedrm_bridgeanddrm_encoderare already interdependent?The second is that the C language spec sets tight constraints to the
drm_for_each_bridge_in_chain_scoped(). The macro must look like:#define
drm_for_each_bridge_in_chain_scoped(encoder, bridge)\
for (structdrm_bridge*bridge = <FOO>; clause-2; clause-3)
'----------- clause-1 ----------'clause-1 must:
* declare a 'struct
drm_bridge*' variable (the loop cursor)
* initialize it via <FOO> which thus must be a rvalue of type 'structdrm_bridge*' (<FOO> must be a function or a macro, as a variable with the correct value is not available)
* use the structdrm_encoder* as its sole input
* lock the encoder chain mutex
* get a reference to the bridge (as Maxime requested)
* ensure the bridge reference is put and the mutex is released on break and return (clause-3 can't do that)Given the above, we still need a function that locks the encoder chain mutex and returns the encoder (bullets 3 and 4), like
drm_encoder_chain_lock(). I'm OK with removingdrm_encoder_chain_lock()and replace it with an internal macro or function indrm_bridge.hthough.
Quoted 6 lines
However I'm not sure how to use
scoped_guardhere because it doesn't return a pointer that can then be passed further. Basically we are constrained to have a chain of function or macro calls, each eating the result of the inner one, with the outer one returning a bridge pointer for the loop cursor variable. There might be some macro magic I'm missing, in such case don't hesitate to mention that.
I realized I was not fully clear, sorry about that. The inability to use scoped_guard refers to the drm_for_each_bridge_in_chain_scoped() implementation, being a macro defining a for loop. However scoped_guard can be used in normal locking code such as the changes in patches 3, 6 and 7.
Luca
Signature · 3 lines
Luca Ceresoli, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Luca Ceresoli
reviewerPATCH v2 2/7 drm/encoder: drm_encoder_cleanup: take chain mutex while tearing down
In reply to Luca Ceresolidrm_encoder_cleanup() modifies the encoder chain by removing bridges via drm_bridge_detach(). Protect this whole operation by taking the mutex, so that:
* any users iterating over the chain will not access it during the change
* any other code willing to modify the list (e.g. drm_bridge_attach()) will wait until drm_encoder_cleanup() is done
Note that the _safe macro in use here is providing a different kind of protection than the mutex:
1. list_for_each_entry_safe() allows removing the current entry from the list it is iterating on, synchronously; the non-safe version would be unable to find the next entry when the current entry has been removed
2. the mutex being added allows to ensure that the list is not used concurrently by other code while it is being modified; this prevents such other concurrent code to derail because it is iterating over an element while it is removed
The _safe macro, which works by taking the "next" pointer in addition to the "current" one, is insufficient to provide the protection at item
2. This is visible e.g. when the "next" element is removed by other concurrent code. This is what would happen without the added mutex:
1. start loop: list_for_each_entry_safe(pos, n, ...) sets;
pos = list_first_entry() = (bridge 1)
n = list_next_entry(pos) = (bridge 2)
2. enter the loop 1st time, do something with *pos (bridge 1)
3. in the meanwhile bridge 2 is hot-unplugged
-> another thread removes bridge 2
-> drm_bridge_detach()
-> list_del() sets (bridge 2)->next = LIST_POISON1
4. loop iteration 1 finishes, list_for_each_entry_safe() sets:
pos = n (previously set to bridge 2)
n = (bridge 2)->next = LIST_POISON1
5. enter the loop 2nd time, do something with *pos (bridge 2)
6. loop iteration 2 finishes, list_for_each_entry_safe() sets:
pos = n = LIST_POISON1 ==> bug!---
Changes in v2:
- Expanded commit messge with rationale, as discussed
--- drivers/gpu/drm/drm_encoder.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/gpu/drm/drm_encoder.c +2 -0
@@ -195,9 +195,11 @@ void drm_encoder_cleanup(struct drm_encoder *encoder)* the indices on the drm_encoder after us in the encoder_list. */ +mutex_lock (&encoder->bridge_chain_mutex);list_for_each_entry_safe (bridge, next, &encoder->bridge_chain, chain_node)drm_bridge_detach (bridge); +mutex_unlock (&encoder->bridge_chain_mutex);drm_mode_object_unregister (dev, &encoder->base);kfree (encoder->name);
Signature · 1 line
2.51.0
Luca Ceresoli
reviewerPATCH v2 3/7 drm/bridge: lock the encoder bridge chain mutex during insertion
In reply to Luca Ceresolidrm_bridge_attach() modifies the encoder bridge chain, so take a mutex around such operations to allow users of the chain to protect themselves from chain modifications while iterating.
---
Changes in v2:
- Removed comment before on drm_bridge_detach()
--- drivers/gpu/drm/drm_bridge.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/gpu/drm/drm_bridge.c +4 -0
@@ -453,10 +453,12 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge, bridge->dev = encoder->dev; bridge->encoder = encoder; +drm_encoder_chain_lock (encoder);if (previous)list_add (&bridge->chain_node, &previous->chain_node);else list_add (&bridge->chain_node, &encoder->bridge_chain); +drm_encoder_chain_unlock (encoder);if (bridge->funcs->attach) { ret = bridge->funcs->attach (bridge, encoder, flags);
@@ -487,7 +489,9 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge, err_reset_bridge: bridge->dev =NULL ; bridge->encoder =NULL ; +drm_encoder_chain_lock (encoder);list_del (&bridge->chain_node); +drm_encoder_chain_unlock (encoder);if (ret != -EPROBE_DEFER ) DRM_ERROR("failed to attach bridge %pOF to encoder %s: %d\n",
Signature · 1 line
2.51.0
Luca Ceresoli
reviewerPATCH v2 4/7 drm/bridge: lock the encoder chain in scoped for_each loops
In reply to Luca Ceresolidrm_for_each_bridge_in_chain_scoped() and drm_for_each_bridge_in_chain_from() currently get/put the bridge at each iteration. But they don't protect the encoder chain, so it could change (bridges added/removed) while some code is iterating over the list itself. To make iterations safe, change the logic of these for_each macros to lock the encoder chain mutex at the beginning and unlock it at the end of the loop (be it at the end of the list, or earlier due to a 'break' or 'return' statement).
Also remove the get/put on the current bridge because it is not needed anymore. In fact all bridges in the encoder chain are refcounted already thanks to the drm_bridge_get() in drm_bridge_attach() and the drm_bridge_put() in drm_bridge_detach(). So while iterating with the mutex held the list cannot change _and_ the refcount of all bridges in the list cannot drop to zero.
---
Changed in v2:
- Fixed infinite loop in drm_for_each_bridge_in_chain_scoped() when encoder->bridge_chain is empty, reported here:
https://lore.kernel.org/lkml/[email protected]/
- Slightly improved commit message
--- include/drm/drm_bridge.h | 62 ++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 29 deletions(-)
include/drm/drm_bridge.h +33 -29
@@ -1440,26 +1440,29 @@ drm_bridge_chain_get_last_bridge(struct drm_encoder *encoder) struct drm_bridge, chain_node)); } -/** -* drm_bridge_get_next_bridge_and_put - Get the next bridge in the chain -* and put the previous -* @bridge: bridge object -* -* Same as drm_bridge_get_next_bridge() but additionally puts the @bridge. -* -* RETURNS: -* the next bridge in the chain after @bridge, or NULL if @bridge is the last. -*/ -static inline struct drm_bridge * -drm_bridge_get_next_bridge_and_put (struct drm_bridge *bridge) +static inline struct drm_bridge *drm_bridge_encoder_chain_lock (struct drm_bridge *bridge) { -struct drm_bridge *next =drm_bridge_get_next_bridge (bridge); +drm_encoder_chain_lock (bridge->encoder); + +return bridge; +} -drm_bridge_put (bridge); +/* Internal to drm_for_each_bridge_in_chain*() */ +static inline struct drm_bridge *__drm_encoder_bridge_chain_next (struct drm_bridge *bridge) +{ +if (list_is_last (&bridge->chain_node, &bridge->encoder->bridge_chain)) { +drm_encoder_chain_unlock (bridge->encoder); -return next; +return NULL ; + } + +return list_next_entry (bridge, chain_node); } +/* Internal to drm_for_each_bridge_in_chain*() */ +DEFINE_FREE(drm_bridge_encoder_chain_unlock, struct drm_bridge *, + if (_T) drm_encoder_chain_unlock(_T->encoder);) +/** * drm_for_each_bridge_in_chain_scoped - iterate over all bridges attached * to an encoder
@@ -1469,14 +1472,15 @@ drm_bridge_get_next_bridge_and_put(struct drm_bridge *bridge)* * Iterate over all bridges present in the bridge chain attached to @encoder. * -* Automatically gets/puts the bridge reference while iterating, and puts -* the reference even if returning or breaking in the middle of the loop. +* Automatically locks the encoder chain mutex to prevent chain +* modifications while iterating. */ -#define drm_for_each_bridge_in_chain_scoped (encoder, bridge) \ - for (struct drm_bridge *bridge __free(drm_bridge_put) = \ - drm_bridge_chain_get_first_bridge(encoder); \ - bridge; \ - bridge = drm_bridge_get_next_bridge_and_put(bridge)) +#define drm_for_each_bridge_in_chain_scoped (encoder, bridge) \ + for (struct drm_bridge *bridge __free(drm_bridge_encoder_chain_unlock) = \ + list_first_entry_or_null(&drm_encoder_chain_lock(encoder)->bridge_chain, \ + struct drm_bridge, chain_node); \ + bridge; \ + bridge = __drm_encoder_bridge_chain_next(bridge)) \/** * drm_for_each_bridge_in_chain_from - iterate over all bridges starting
@@ -1488,14 +1492,14 @@ drm_bridge_get_next_bridge_and_put(struct drm_bridge *bridge)* Iterate over all bridges in the encoder chain starting from * @first_bridge, included. * -* Automatically gets/puts the bridge reference while iterating, and puts -* the reference even if returning or breaking in the middle of the loop. +* Automatically locks the encoder chain mutex to prevent chain +* modifications while iterating. */ -#define drm_for_each_bridge_in_chain_from (first_bridge, bridge) \ - for (struct drm_bridge *bridge __free(drm_bridge_put) = \ - drm_bridge_get(first_bridge); \ - bridge; \ - bridge = drm_bridge_get_next_bridge_and_put(bridge)) +#define drm_for_each_bridge_in_chain_from (first_bridge, bridge) \ + for (struct drm_bridge *bridge __free(drm_bridge_encoder_chain_unlock) = \ + drm_bridge_encoder_chain_lock(first_bridge); \ + bridge; \ + bridge = __drm_encoder_bridge_chain_next(bridge)) \ enum drm_mode_status drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
Signature · 1 line
2.51.0
Maxime Ripard
maintainerRe: [PATCH v2 4/7] drm/bridge: lock the encoder chain in scoped for_each loops
Quoted 9 lines · Luca Ceresoli
drm_for_each_bridge_in_chain_scoped()anddrm_for_each_bridge_in_chain_from()currently get/put the bridge at each iteration. But they don't protect the encoder chain, so it could change (bridges added/removed) while some code is iterating over the list itself. To make iterations safe, change the logic of thesefor_eachmacros to lock the encoder chain mutex at the beginning and unlock it at the end of the loop (be it at the end of the list, or earlier due to a 'break' or 'return' statement).
Quoted 6 lines
Also remove the get/put on the current bridge because it is not needed anymore. In fact all bridges in the encoder chain are refcounted already thanks to the
drm_bridge_get()indrm_bridge_attach()and thedrm_bridge_put()indrm_bridge_detach(). So while iterating with the mutex held the list cannot change _and_ the refcount of all bridges in the list cannot drop to zero.
This second paragraph *really* needs to be its own patch. And I'm not really sure playing games when it comes to refcounting is a good idea.
A strict, simple, rule is way easier to follow than trying to figure out two years from now why this loop skips the refcounting.
Unless you have a performance issue that is, in which case you should add a comment (and we will need a meaningful benchmark to back that claim).
Quoted 30 lines
Signed-off-by Luca Ceresoli <[email protected]>---
Changed in v2:
- Fixed infinite loop indrm_for_each_bridge_in_chain_scoped()whenencoder->bridge_chainis empty, reported here:
https://lore.kernel.org/lkml/[email protected]/
- Slightly improved commit message--- include/drm/drm_bridge.h | 62 ++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 29 deletions(-)include/drm/drm_bridge.h +0
-9@@ -1440,26 +1440,29 @@ drm_bridge_chain_get_last_bridge(struct drm_encoder *encoder) struct drm_bridge, chain_node)); } -/** -* drm_bridge_get_next_bridge_and_put - Get the next bridge in the chain -* and put the previous -* @bridge: bridge object -* -* Same as drm_bridge_get_next_bridge() but additionally puts the @bridge. -* -* RETURNS: -* the next bridge in the chain after @bridge, or NULL if @bridge is the last.
include/drm/drm_bridge.h drm_bridge_chain_get_last_bridge(struct drm_encoder *encoder) · line 1448
Quoted 10 lines
include/drm/drm_bridge.h +5
-4-*/ -static inline struct drm_bridge * -drm_bridge_get_next_bridge_and_put (struct drm_bridge *bridge) +static inline struct drm_bridge *drm_bridge_encoder_chain_lock (struct drm_bridge *bridge) { -struct drm_bridge *next =drm_bridge_get_next_bridge (bridge); +drm_encoder_chain_lock (bridge->encoder); + +return bridge; +}
You create a public function, this needs to be documented.
Maxime
Luca Ceresoli
reviewerRe: [PATCH v2 4/7] drm/bridge: lock the encoder chain in scoped for_each loops
Hello Maxime,
Quoted 20 lines · On Fri, 3 Oct 2025 16:04:50 +0200 Maxime Ripard <[email protected]> wrote:
Quoted 9 lines · Luca Ceresoli
drm_for_each_bridge_in_chain_scoped()anddrm_for_each_bridge_in_chain_from()currently get/put the bridge at each iteration. But they don't protect the encoder chain, so it could change (bridges added/removed) while some code is iterating over the list itself. To make iterations safe, change the logic of thesefor_eachmacros to lock the encoder chain mutex at the beginning and unlock it at the end of the loop (be it at the end of the list, or earlier due to a 'break' or 'return' statement).Quoted 6 lines
Also remove the get/put on the current bridge because it is not needed anymore. In fact all bridges in the encoder chain are refcounted already thanks to the
drm_bridge_get()indrm_bridge_attach()and thedrm_bridge_put()indrm_bridge_detach(). So while iterating with the mutex held the list cannot change _and_ the refcount of all bridges in the list cannot drop to zero.This second paragraph *really* needs to be its own patch. And I'm not really sure playing games when it comes to refcounting is a good idea.
Quoted 6 lines
A strict, simple, rule is way easier to follow than trying to figure out two years from now why this loop skips the refcounting.
Unless you have a performance issue that is, in which case you should add a comment (and we will need a meaningful benchmark to back that claim).
Just to give some background, I have realized we need to lock the encoder chain after drm_for_each_bridge_in_chain_scoped() was added. Should I had realized it before, I would have sent it in the form you can see in this patch, without the get/put because it is not necessary. Not sure whether that would have changed the reception.
But I'm not aware of any performance issue, and the impact of refcounting should not be small, soI'll try re-adding an explicit get/put on top of the current version. It will likely make the macro more complicated but should be reasonably doable. So, expect a v3 with that change to we can all see how it looks.
Best regards,
Luca
Signature · 3 lines
Luca Ceresoli, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Luca Ceresoli
reviewerPATCH v2 5/7 drm/bridge: prevent encoder chain changes while iterating with list_for_each_entry_from()
In reply to Luca CeresoliThese loops in drm_bridge.c iterate over the encoder chain using list_for_each_entry_from(), which does not prevent changes to the bridge chain while iterating over it.
Convert most of those loops to instead use drm_for_each_bridge_in_chain_from(), which locks the chain.
This also simplifies code.
All the "simple" loops are converted here. The only ones not touched are those in drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_post_disable(), because they have nested loops which are not well handled by drm_for_each_bridge_in_chain_from(). These two functions are handled by a separate commit.
--- drivers/gpu/drm/drm_bridge.c | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-)
drivers/gpu/drm/drm_bridge.c +12 -23
@@ -618,7 +618,7 @@ void drm_bridge_detach(struct drm_bridge *bridge)/** * drm_bridge_chain_mode_valid - validate the mode against all bridges in the * encoder chain. -* @bridge: bridge control structure +* @first_bridge: bridge control structure * @info: display info against which the mode shall be validated * @mode: desired mode to be validated *
@@ -632,17 +632,14 @@ void drm_bridge_detach(struct drm_bridge *bridge)* MODE_OK on success, drm_mode_status Enum error code on failure */ enum drm_mode_status -drm_bridge_chain_mode_valid (struct drm_bridge *bridge, +drm_bridge_chain_mode_valid (struct drm_bridge *first_bridge,const struct drm_display_info *info,const struct drm_display_mode *mode) { -struct drm_encoder *encoder; - -if (!bridge) +if (!first_bridge)return MODE_OK ; - encoder = bridge->encoder; -list_for_each_entry_from (bridge, &encoder->bridge_chain, chain_node) { +drm_for_each_bridge_in_chain_from (first_bridge, bridge) {enum drm_mode_status ret;if (!bridge->funcs->mode_valid)
@@ -660,7 +657,7 @@ EXPORT_SYMBOL(drm_bridge_chain_mode_valid);/** * drm_bridge_chain_mode_set - set proposed mode for all bridges in the * encoder chain -* @bridge: bridge control structure +* @first_bridge: bridge control structure * @mode: desired mode to be set for the encoder chain * @adjusted_mode: updated mode that works for this encoder chain *
@@ -669,20 +666,16 @@ EXPORT_SYMBOL(drm_bridge_chain_mode_valid);* * Note: the bridge passed should be the one closest to the encoder */ -void drm_bridge_chain_mode_set (struct drm_bridge *bridge, +void drm_bridge_chain_mode_set (struct drm_bridge *first_bridge,const struct drm_display_mode *mode,const struct drm_display_mode *adjusted_mode) { -struct drm_encoder *encoder; - -if (!bridge) +if (!first_bridge)return ; - encoder = bridge->encoder; -list_for_each_entry_from (bridge, &encoder->bridge_chain, chain_node) { +drm_for_each_bridge_in_chain_from (first_bridge, bridge)if (bridge->funcs->mode_set) bridge->funcs->mode_set (bridge, mode, adjusted_mode); - } }EXPORT_SYMBOL (drm_bridge_chain_mode_set);
@@ -906,7 +899,7 @@ EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);/** * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain -* @bridge: bridge control structure +* @first_bridge: bridge control structure * @state: atomic state being committed * * Calls &drm_bridge_funcs.atomic_enable (falls back on
@@ -916,22 +909,18 @@ EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);* * Note: the bridge passed should be the one closest to the encoder */ -void drm_atomic_bridge_chain_enable (struct drm_bridge *bridge, +void drm_atomic_bridge_chain_enable (struct drm_bridge *first_bridge,struct drm_atomic_state *state) { -struct drm_encoder *encoder; - -if (!bridge) +if (!first_bridge)return ; - encoder = bridge->encoder; -list_for_each_entry_from (bridge, &encoder->bridge_chain, chain_node) { +drm_for_each_bridge_in_chain_from (first_bridge, bridge)if (bridge->funcs->atomic_enable) { bridge->funcs->atomic_enable (bridge, state); }else if (bridge->funcs->enable) { bridge->funcs->enable (bridge); } - } }EXPORT_SYMBOL (drm_atomic_bridge_chain_enable);
Signature · 1 line
2.51.0
Luca Ceresoli
reviewerPATCH v2 6/7 drm/bridge: prevent encoder chain changes while iterating with list_for_each_entry_reverse()
In reply to Luca CeresoliThese loops in drm_bridge.c iterate over the encoder chain using list_for_each_entry_reverse(), which does not prevent changes to the bridge chain while iterating over it.
Take the encoder chain mutex while iterating to avoid chain changes while iterating.
All the "simple" loops are converted. drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_post_disable() are handled by a separate commit.
--- drivers/gpu/drm/drm_bridge.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_bridge.c +7 -1
@@ -701,6 +701,7 @@ void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,return ; encoder = bridge->encoder; +drm_encoder_chain_lock (encoder);list_for_each_entry_reverse (iter, &encoder->bridge_chain, chain_node) {if (iter->funcs->atomic_disable) { iter->funcs->atomic_disable (iter, state);
@@ -711,6 +712,7 @@ void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,if (iter == bridge)break ; } +drm_encoder_chain_unlock (encoder); }EXPORT_SYMBOL (drm_atomic_bridge_chain_disable);
@@ -1215,6 +1217,7 @@ int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,return ret; encoder = bridge->encoder; +drm_encoder_chain_lock (encoder);list_for_each_entry_reverse (iter, &encoder->bridge_chain, chain_node) {int ret;
@@ -1229,12 +1232,15 @@ int drm_atomic_bridge_chain_check(struct drm_bridge *bridge, crtc_state->state); ret =drm_atomic_bridge_check (iter, crtc_state, conn_state); -if (ret) +if (ret) { +drm_encoder_chain_unlock (encoder);return ret; + }if (iter == bridge)break ; } +drm_encoder_chain_unlock (encoder);return 0 ; }
Signature · 1 line
2.51.0
Luca Ceresoli
reviewerPATCH v2 7/7 drm/bridge: prevent encoder chain changes in pre_enable/post_disable
In reply to Luca CeresoliTake the encoder chain mutex while iterating over the encoder chain in drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_post_disable() to ensure the lists won't change while being inspected.
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 in its current implementation. 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.
---
Changes in v2:
- Improved commit message
--- drivers/gpu/drm/drm_bridge.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/gpu/drm/drm_bridge.c +4 -0
@@ -760,6 +760,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 ;
@@ -808,6 +809,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);
@@ -854,6 +856,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;
@@ -896,6 +899,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);
Signature · 1 line
2.51.0