Re: [PATCH v2 3/9] drm/bridge: add drm_for_each_bridge_in_chain_scoped()

From: Luca Ceresoli
Date: Tue Aug 19 2025 - 12:03:37 EST


Hi Maxime,

On Tue, 19 Aug 2025 15:47:06 +0200
Maxime Ripard <mripard@xxxxxxxxxx> wrote:

> > +/**
> > + * drm_for_each_bridge_in_chain_scoped - iterate over all bridges attached
> > + * to an encoder
> > + * @encoder: the encoder to iterate bridges on
> > + * @bridge: a bridge pointer updated to point to the current bridge at each
> > + * iteration
> > + *
> > + * 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.
> > + */
> > +#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); \
>
> So my understanding is that the initial value of bridge would be cleaned
> up with drm_bridge_put...
>
> > + bridge; \
> > + bridge = drm_bridge_get_next_bridge_and_put(bridge))
>
> ... but also when iterating?
>
> So if we have more than 0 values, we put two references?

No, this is not the case. The __free action is executed only when
exiting the entire for loop, not a single iteration.

This is consistent with the fact that the loop variable is persistent
across iterations.

I tested this macro in both cases:

* looping over the entire chain the final value of @bridge will be
NULL and the cleanup action won't call drm_bridge_put()
* breaking before the last element, @bridge is non-NULL and the
cleanup action does call drm_bridge_put()

See examples such as for_each_child_of_node_scoped() and other OF
iterators which work in the same way (which is no coincidence, I used
them as starting point for writing this patch).

Best regards,
Luca

--
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com