[PATCH v6 21/26] drm/bridge: add list of removed refcounted bridges

Luca Ceresoli
2025-02-06T18:15:35Z

Between drm_bridge_add() and drm_bridge_remove() bridges "published" to the
DRM core via the global bridge_list and visible in
/sys/kernel/debug/dri/bridges. However between drm_bridge_remove() and the
last drm_bridge_put(), a refcounted bridge memory is still allocated even
though the bridge is not "published", i.e. not in bridges_list, so it is
invisible in debugfs. This prevents debugging refcounted bridges lifetime,
especially leaks doe to any missing drm_bridge_put().

In order to allow debugfs to also show the removed refcounted bridges, move
such bridges into a new ad-hoc list until they are freed.

Note this requires adding INIT_LIST_HEAD(&bridge->list) in the bridge
initialization code: the lack of such init was not exposing any bug so far,
but it would with the new code.

Document the new behaviour of drm_bridge_remove() and update the
drm_bridge_add() documentation to stay consistent.

Signed-off-by: Luca Ceresoli <luca.ceresoli@bootlin.com>

---

This patch was added in v6.
---
drivers/gpu/drm/drm_bridge.c | 23 ++++++++++++++++++++---
drivers/gpu/drm/drm_internal.h | 1 +
2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c
index fc44a5d227a89a12b5c3299a29776cfddb36ce27..b315c7c4e32cc27723c69c795efe4f3313134204 100644
--- a/drivers/gpu/drm/drm_bridge.c
+++ b/drivers/gpu/drm/drm_bridge.c
@@ -315,9 +315,10 @@
* driver.
*/

-/* Protect bridge_list */
+/* Protect bridge_list and bridge_removed_list */
DEFINE_MUTEX(bridge_lock);
LIST_HEAD(bridge_list);
+LIST_HEAD(bridge_removed_list);

/* Internal function (for refcounted bridges) */
void __drm_bridge_free(struct kref *kref)
@@ -327,6 +328,10 @@ void __drm_bridge_free(struct kref *kref)

DRM_DEBUG("bridge=%p, container=%p FREE\n", bridge, container);

+ mutex_lock(&bridge_lock);
+ list_del(&bridge->list);
+ mutex_unlock(&bridge_lock);
+
kfree(container);
}
EXPORT_SYMBOL(__drm_bridge_free);
@@ -355,6 +360,8 @@ void *__devm_drm_bridge_alloc(struct device *dev, size_t size, size_t offset,
return ERR_PTR(-ENOMEM);

bridge = container + offset;
+
+ INIT_LIST_HEAD(&bridge->list);
bridge->container_offset = offset;
bridge->funcs = funcs;
kref_init(&bridge->refcount);
@@ -371,7 +378,10 @@ void *__devm_drm_bridge_alloc(struct device *dev, size_t size, size_t offset,
EXPORT_SYMBOL(__devm_drm_bridge_alloc);

/**
- * drm_bridge_add - add the given bridge to the global bridge list
+ * drm_bridge_add - add to published bridges
+ *
+ * Adds the given bridge to the global bridge list, so it can be found by
+ * of_drm_find_bridge().
*
* @bridge: bridge control structure
*/
@@ -423,7 +433,12 @@ int devm_drm_bridge_add(struct device *dev, struct drm_bridge *bridge)
EXPORT_SYMBOL(devm_drm_bridge_add);

/**
- * drm_bridge_remove - remove the given bridge from the global bridge list
+ * drm_bridge_remove - remove from published bridges
+ *
+ * Remove the given bridge from the global bridge list, so it won't be
+ * found by of_drm_find_bridge(). If the bridge is refcounted it also adds
+ * it to the removed bridge list, to keep track of removed bridges until
+ * their allocated memory is finally freed.
*
* @bridge: bridge control structure
*/
@@ -435,6 +450,8 @@ void drm_bridge_remove(struct drm_bridge *bridge)

mutex_lock(&bridge_lock);
list_del_init(&bridge->list);
+ if (drm_bridge_is_refcounted(bridge))
+ list_add_tail(&bridge->list, &bridge_removed_list);
mutex_unlock(&bridge_lock);

list_for_each_entry_safe(br, tmp, &bridge_list, list)
diff --git a/drivers/gpu/drm/drm_internal.h b/drivers/gpu/drm/drm_internal.h
index b6e875d4b25faae6bb0bb952c3c12bd4819698ec..bbfc938b21c13bc69c36d3833f6cb6d5d22d1c54 100644
--- a/drivers/gpu/drm/drm_internal.h
+++ b/drivers/gpu/drm/drm_internal.h
@@ -51,6 +51,7 @@ struct drm_vblank_crtc;
// for drm_debugfs.c
extern struct mutex bridge_lock;
extern struct list_head bridge_list;
+extern struct list_head bridge_removed_list;

/* drm_client_event.c */
#if defined(CONFIG_DRM_CLIENT)

--
2.34.1