drm/bridge: handle gracefully atomic updates during bridge removal
Subsystems (3)
Luca Ceresoli
reviewerv2 cover drm/bridge: handle gracefully atomic updates during bridge removal
This is a first attempt at gracefully handling the case of atomic updates happening concurrently to physical removal of DRM bridges.
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 [0]. 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
6. … protect encoder bridge chain with a mutex
7. of_drm_find_bridge
8. drm_of_find_panel_or_bridge, *_of_get_bridge
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 idea was proposed by Maxime [1] and is based on the existing drm_dev_enter/exit() already existing for the DRM device.
This small series implements the core mechanism in drm_bridge.c and uses it in the ti-sn65dsi83 driver. This prevents usage of device resources by various code paths that can happen concurrently to unplug of the SN65DSI8x bridge.
[0] https://lore.kernel.org/lkml/[email protected]/#t
[1] https://lore.kernel.org/all/20250106-vigorous-talented-viper-fa49d9@houat/
---
Changes in v2:
- No changes to patch 1, discussion pending
- Use devres instead of a flag in patch 2
- Link to v1: https://lore.kernel.org/r/20250808-drm-bridge-atomic-vs-remove-v1-0-a52e933b08a8@bootlin.com
---
Luca Ceresoli (2):
drm/bridge: add drm_bridge_unplug() and drm_bridge_enter/exit() drm/bridge: ti-sn65dsi83: protect device resources on unplug
drivers/gpu/drm/bridge/ti-sn65dsi83.c | 85 ++++++++++++++++++++++++++++------- drivers/gpu/drm/drm_bridge.c | 58 ++++++++++++++++++++++++ include/drm/drm_bridge.h | 12 +++++ 3 files changed, 138 insertions(+), 17 deletions(-)
---
base-commit: 7acbe30813f04cccf7b2e8b571eb7936cfec0a87
change-id: 20250808-drm-bridge-atomic-vs-remove-1d7bb202c8ef
Best regards,
Signature · 1 line
Luca Ceresoli <[email protected]>
Luca Ceresoli
reviewerPATCH v2 1/2 drm/bridge: add drm_bridge_unplug() and drm_bridge_enter/exit()
To allow DRM bridges to be removable, add synchronization functions allowing to tell when a bridge hardware has been physically unplugged and to mark a critical section that should not be entered after that.
This is inspired by the drm_dev_unplugged/enter/exit() functions for struct drm_device.
--- drivers/gpu/drm/drm_bridge.c | 58 ++++++++++++++++++++++++++++++++++++++++++++ include/drm/drm_bridge.h | 12 +++++++++ 2 files changed, 70 insertions(+)
drivers/gpu/drm/drm_bridge.c +58 -0
@@ -27,6 +27,7 @@#include <linux/media-bus-format.h> #include <linux/module.h> #include <linux/mutex.h> +#include <linux/srcu.h> #include <drm/drm_atomic_state_helper.h> #include <drm/drm_bridge.h>
@@ -200,6 +201,63 @@static DEFINE_MUTEX (bridge_lock);static LIST_HEAD (bridge_list); +DEFINE_STATIC_SRCU (drm_bridge_unplug_srcu); + +/** +* drm_bridge_enter - Enter DRM bridge critical section +* @dev: DRM bridge +* @idx: Pointer to index that will be passed to the matching drm_bridge_exit() +* +* This function marks and protects the beginning of a section that should not +* be entered after the bridge has been unplugged. The section end is marked +* with drm_bridge_exit(). Calls to this function can be nested. +* +* Returns: +* True if it is OK to enter the section, false otherwise. +*/ +bool drm_bridge_enter (struct drm_bridge *bridge,int *idx) +{ + *idx =srcu_read_lock (&drm_bridge_unplug_srcu); + +if (bridge->unplugged) { +srcu_read_unlock (&drm_bridge_unplug_srcu, *idx); +return false; + } + +return true; +} +EXPORT_SYMBOL (drm_bridge_enter); + +/** +* drm_bridge_exit - Exit DRM bridge critical section +* @idx: index returned by drm_bridge_enter() +* +* This function marks the end of a section that should not be entered after +* the bridge has been unplugged. +*/ +void drm_bridge_exit (int idx) +{ +srcu_read_unlock (&drm_bridge_unplug_srcu, idx); +} +EXPORT_SYMBOL (drm_bridge_exit); + +/** +* drm_bridge_unplug - unplug a DRM bridge +* @dev: DRM bridge +* +* This tells the bridge has been physically unplugged and no operations on +* device resources must be done anymore. Entry-points can use +* drm_bridge_enter() and drm_bridge_exit() to protect device resources in +* a race free manner. +*/ +void drm_bridge_unplug (struct drm_bridge *bridge) +{ + bridge->unplugged = true; + +synchronize_srcu (&drm_bridge_unplug_srcu); +} +EXPORT_SYMBOL (drm_bridge_unplug); +static void __drm_bridge_free (struct kref *kref) { struct drm_bridge *bridge = container_of(kref, struct drm_bridge, refcount);
include/drm/drm_bridge.h +12 -0
@@ -1143,6 +1143,14 @@ struct drm_bridge {*/ struct kref refcount; +/** +* @unplugged: +* +* Flag to tell if the bridge has been unplugged. +* See drm_bridge_enter() and drm_bridge_unplug(). +*/ +bool unplugged; +/** @driver_private: pointer to the bridge driver's internal context */ void *driver_private;/** @ops: bitmask of operations supported by the bridge */
@@ -1278,6 +1286,10 @@ drm_priv_to_bridge(struct drm_private_obj *priv) return container_of(priv, struct drm_bridge, base); } +bool drm_bridge_enter (struct drm_bridge *bridge,int *idx); +void drm_bridge_exit (int idx); +void drm_bridge_unplug (struct drm_bridge *bridge); +struct drm_bridge *drm_bridge_get (struct drm_bridge *bridge);void drm_bridge_put (struct drm_bridge *bridge);
Signature · 1 line
2.51.0
kernel test robot
build botRe: [PATCH v2 1/2] drm/bridge: add drm_bridge_unplug() and drm_bridge_enter/exit()
kernel test robot noticed the following build warnings:
Hi Luca,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 7acbe30813f04cccf7b2e8b571eb7936cfec0a87]
url: https://github.com/intel-lab-lkp/linux/commits/Luca-Ceresoli/drm-bridge-add-drm_bridge_unplug-and-drm_bridge_enter-exit/20250927-003721 base: 7acbe30813f04cccf7b2e8b571eb7936cfec0a87 patch link: https://lore.kernel.org/r/20250926-drm-bridge-atomic-vs-remove-v2-1-69f7d5ca1a92%40bootlin.com patch subject: [PATCH v2 1/2] drm/bridge: add drm_bridge_unplug() and drm_bridge_enter/exit() config: x86_64-buildonly-randconfig-002-20250927 (https://download.01.org/0day-ci/archive/20250927/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250927/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: drivers/gpu/drm/drm_bridge.c:218 function parameter 'bridge' not described in 'drm_bridge_enter' >> Warning: drivers/gpu/drm/drm_bridge.c:253 function parameter 'bridge' not described in 'drm_bridge_unplug'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Luca Ceresoli
reviewerPATCH v2 2/2 drm/bridge: ti-sn65dsi83: protect device resources on unplug
In reply to Luca CeresoliTo support hot-unplug of this bridge we need to protect access to device resources in case sn65dsi83_remove() happens concurrently to other code.
Some care is needed for the case when the unplug happens before sn65dsi83_atomic_disable() has a chance to enter the critical section (i.e. a successful drm_bridge_enter() call), which occurs whenever the hardware is removed while the display is active. When that happens, sn65dsi83_atomic_disable() in unable to release the resources taken by sn65dsi83_atomic_pre_enable().
To ensure those resources are released exactly once on device removal:
* move the code to release them to a dedicated function
* register that function when the resources are taken in sn65dsi83_atomic_pre_enable()
* if sn65dsi83_atomic_disable() happens before sn65dsi83_remove() (typical non-hot-unplug case):
* sn65dsi83_atomic_disable() can enter the critical section
(drm_bridge_enter() returns 0) -> it releases and executes the
devres action * if sn65dsi83_atomic_disable() happens after sn65dsi83_remove() (typical hot-unplug case):
* sn65dsi83_remove() -> drm_bridge_unplug() prevents sn65dsi83_atomic_disable() from entering the critical section (drm_bridge_enter() returns nonzero), so sn65dsi83_atomic_disable() cannot release and execute the devres action
* the devres action is executed at the end of sn65dsi83_remove()
---
Changed in v2:
- Use a devres action instead of a flag
--- drivers/gpu/drm/bridge/ti-sn65dsi83.c | 85 ++++++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 17 deletions(-)
drivers/gpu/drm/bridge/ti-sn65dsi83.c +68 -17
@@ -406,6 +406,10 @@ static void sn65dsi83_reset_work(struct work_struct *ws) { struct sn65dsi83 *ctx = container_of(ws, struct sn65dsi83, reset_work);int ret; +int idx; + +if (!drm_bridge_enter (&ctx->bridge, &idx)) +return ;/* Reset the pipe */ ret =sn65dsi83_reset_pipe (ctx);
@@ -415,12 +419,18 @@ static void sn65dsi83_reset_work(struct work_struct *ws) }if (ctx->irq)enable_irq (ctx->irq); + +drm_bridge_exit (idx); }static void sn65dsi83_handle_errors (struct sn65dsi83 *ctx) {unsigned int irq_stat;int ret; +int idx; + +if (!drm_bridge_enter (&ctx->bridge, &idx)) +return ;/* * Schedule a reset in case of:
@@ -441,6 +451,8 @@ static void sn65dsi83_handle_errors(struct sn65dsi83 *ctx)schedule_work (&ctx->reset_work); } + +drm_bridge_exit (idx); }static void sn65dsi83_monitor_work (struct work_struct *work)
@@ -463,6 +475,40 @@ static void sn65dsi83_monitor_stop(struct sn65dsi83 *ctx)cancel_delayed_work_sync (&ctx->monitor_work); } +/* +* Release resources taken by sn65dsi83_atomic_pre_enable(). +* +* Normally invoked by sn65dsi83_atomic_disable(). But if +* sn65dsi83_remove() is invoked while enabled, then drm_bridge_unplug() +* would defuse sn65dsi83_atomic_disable(). Thus a devres action is used to +* ensure exactly one code path (removal or atomic disable) invokes this +* code. +*/ +static void sn65dsi83_release_resources (void *data) +{ +struct sn65dsi83 *ctx = (struct sn65dsi83 *)data; +int ret; + +if (ctx->irq) { +/* Disable irq */ +regmap_write (ctx->regmap,REG_IRQ_EN ,0x0 ); +regmap_write (ctx->regmap,REG_IRQ_GLOBAL ,0x0 ); + }else { +/* Stop the polling task */ +sn65dsi83_monitor_stop (ctx); + } + +/* Put the chip in reset, pull EN line low, and assure 10ms reset low timing. */ +gpiod_set_value_cansleep (ctx->enable_gpio,0 ); +usleep_range (10000 ,11000 ); + + ret =regulator_disable (ctx->vcc); +if (ret) +dev_err (ctx->dev,"Failed to disable vcc: %d\n" , ret); + +regcache_mark_dirty (ctx->regmap); +} +static void sn65dsi83_atomic_pre_enable (struct drm_bridge *bridge,struct drm_atomic_state *state) {
@@ -478,10 +524,15 @@ static void sn65dsi83_atomic_pre_enable(struct drm_bridge *bridge,__le16 le16val;u16 val;int ret; +int idx; + +if (!drm_bridge_enter (bridge, &idx)) +return ; ret =regulator_enable (ctx->vcc);if (ret) {dev_err (ctx->dev,"Failed to enable vcc: %d\n" , ret); +drm_bridge_exit (idx);return ; }
@@ -625,6 +676,8 @@ static void sn65dsi83_atomic_pre_enable(struct drm_bridge *bridge,dev_err (ctx->dev,"failed to lock PLL, ret=%i\n" , ret);/* On failure, disable PLL again and exit. */ regmap_write (ctx->regmap,REG_RC_PLL_EN ,0x00 ); +devm_add_action (ctx->dev, sn65dsi83_release_resources, ctx); +drm_bridge_exit (idx);return ; }
@@ -633,6 +686,9 @@ static void sn65dsi83_atomic_pre_enable(struct drm_bridge *bridge,/* Wait for 10ms after soft reset as specified in datasheet */ usleep_range (10000 ,12000 ); + +devm_add_action (ctx->dev, sn65dsi83_release_resources, ctx); +drm_bridge_exit (idx); } static void sn65dsi83_atomic_enable(struct drm_bridge *bridge,
@@ -640,6 +696,10 @@ static void sn65dsi83_atomic_enable(struct drm_bridge *bridge, {struct sn65dsi83 *ctx =bridge_to_sn65dsi83 (bridge);unsigned int pval; +int idx; + +if (!drm_bridge_enter (bridge, &idx)) +return ;/* Clear all errors that got asserted during initialization. */ regmap_read (ctx->regmap,REG_IRQ_STAT , &pval);
@@ -659,32 +719,22 @@ static void sn65dsi83_atomic_enable(struct drm_bridge *bridge,/* Use the polling task */ sn65dsi83_monitor_start (ctx); } + +drm_bridge_exit (idx); }static void sn65dsi83_atomic_disable (struct drm_bridge *bridge,struct drm_atomic_state *state) {struct sn65dsi83 *ctx =bridge_to_sn65dsi83 (bridge); -int ret; - -if (ctx->irq) { -/* Disable irq */ -regmap_write (ctx->regmap,REG_IRQ_EN ,0x0 ); -regmap_write (ctx->regmap,REG_IRQ_GLOBAL ,0x0 ); - }else { -/* Stop the polling task */ -sn65dsi83_monitor_stop (ctx); - } +int idx; -/* Put the chip in reset, pull EN line low, and assure 10ms reset low timing. */ -gpiod_set_value_cansleep (ctx->enable_gpio,0 ); -usleep_range (10000 ,11000 ); +if (!drm_bridge_enter (bridge, &idx)) +return ; - ret =regulator_disable (ctx->vcc); -if (ret) -dev_err (ctx->dev,"Failed to disable vcc: %d\n" , ret); +devm_release_action (ctx->dev, sn65dsi83_release_resources, ctx); -regcache_mark_dirty (ctx->regmap); +drm_bridge_exit (idx); } static enum drm_mode_status
@@ -1005,6 +1055,7 @@ static void sn65dsi83_remove(struct i2c_client *client) {struct sn65dsi83 *ctx =i2c_get_clientdata (client); +drm_bridge_unplug (&ctx->bridge);drm_bridge_remove (&ctx->bridge); }
Signature · 1 line
2.51.0