On 11/19/25 13:05, Luca Ceresoli wrote:
of_drm_find_bridge() does not increment the refcount for the returned
bridge, but that is required now. However converting it and all its users
is not realistically doable at once given the large amount of (direct and
indirect) callers and the complexity of some. Also, "of_drm_find_bridge is
oddly named according to our convention and it would make more sense to be
called drm_of_find_bridge()" (quoted from Link: below).

Solve both issues by creating a new drm_of_find_bridge() that is identical
to of_drm_find_bridge() except it takes a reference. Then
of_drm_find_bridge() will be deprecated to be eventually removed.

Suggested-by: Maxime Ripard <[email protected]>
Link: 
https://lore.kernel.org/dri-devel/20250319-stylish-lime-mongoose-0a18ad@houat/
Signed-off-by: Luca Ceresoli <[email protected]>

---

Note: a simple implementation would just be
   { return drm_bridge_get(of_drm_find_bridge(np)); }
but it would release the mutex before getting the reference, so it is
not safe. Make things simple by duplicating the code. A later patch will
make instead the (to be deprecated) of_drm_find_bridge() become a wrapper
of the new drm_of_find_bridge()
---
  drivers/gpu/drm/drm_bridge.c | 29 +++++++++++++++++++++++++++++
  include/drm/drm_bridge.h     |  5 +++++
  2 files changed, 34 insertions(+)

diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c
index 8f355df883d8..d98a7b4a83c0 100644
--- a/drivers/gpu/drm/drm_bridge.c
+++ b/drivers/gpu/drm/drm_bridge.c
@@ -1417,6 +1417,35 @@ void drm_bridge_hpd_notify(struct drm_bridge *bridge,
  EXPORT_SYMBOL_GPL(drm_bridge_hpd_notify);
#ifdef CONFIG_OF
+/**
+ * drm_of_find_bridge - find the bridge corresponding to the device node in
+ *                     the global bridge list
+ * @np: device node
+ *
+ * The refcount of the returned bridge is incremented. Use drm_bridge_put()
+ * when done with it.
+ *
+ * RETURNS:
+ * drm_bridge control struct on success, NULL on failure
+ */
+struct drm_bridge *drm_of_find_bridge(struct device_node *np)
+{
+       struct drm_bridge *bridge;
+
+       mutex_lock(&bridge_lock);
+
+       list_for_each_entry(bridge, &bridge_list, list) {
+               if (bridge->of_node == np) {
+                       mutex_unlock(&bridge_lock);

It seems a bit strange to unlock the mutex just before the drm_bridge_get, is it expected?

If no, I think you can use scoped_guard(mutex, &bridge_lock) to avoid messing with mutex_unlock, IIRC, scoped_guard will unlock the mutex just after the return, so in your case, just after the drm_bridge_get.

+                       return drm_bridge_get(bridge);
+               }
+       }
+
+       mutex_unlock(&bridge_lock);
+       return NULL;
+}
+EXPORT_SYMBOL(drm_of_find_bridge);
+
  /**
   * of_drm_find_bridge - find the bridge corresponding to the device node in
   *                    the global bridge list
diff --git a/include/drm/drm_bridge.h b/include/drm/drm_bridge.h
index 0ff7ab4aa868..e74e91004c48 100644
--- a/include/drm/drm_bridge.h
+++ b/include/drm/drm_bridge.h
@@ -1313,8 +1313,13 @@ int drm_bridge_attach(struct drm_encoder *encoder, 
struct drm_bridge *bridge,
                      enum drm_bridge_attach_flags flags);
#ifdef CONFIG_OF
+struct drm_bridge *drm_of_find_bridge(struct device_node *np);
  struct drm_bridge *of_drm_find_bridge(struct device_node *np);
  #else
+static inline struct drm_bridge *drm_of_find_bridge(struct device_node *np)
+{
+       return NULL;
+}
  static inline struct drm_bridge *of_drm_find_bridge(struct device_node *np)
  {
        return NULL;


Reply via email to