<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Mon, May 5, 2025 at 2:54 AM Maxime Ripard <<a href="mailto:mripard@kernel.org">mripard@kernel.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Jani,<br>
<br>
On Tue, Apr 29, 2025 at 12:22:00PM +0300, Jani Nikula wrote:<br>
> On Tue, 29 Apr 2025, Maxime Ripard <<a href="mailto:mripard@kernel.org" target="_blank">mripard@kernel.org</a>> wrote:<br>
> > Hi Jani,<br>
> ><br>
> > On Mon, Apr 28, 2025 at 07:31:50PM +0300, Jani Nikula wrote:<br>
> >> On Mon, 31 Mar 2025, Anusha Srivatsa <<a href="mailto:asrivats@redhat.com" target="_blank">asrivats@redhat.com</a>> wrote:<br>
> >> > Allocate panel via reference counting. Add _get() and _put() helper<br>
> >> > functions to ensure panel allocations are refcounted. Avoid use after<br>
> >> > free by ensuring panel pointer is valid and can be usable till the last<br>
> >> > reference is put.<br>
> >> ><br>
> >> > Reviewed-by: Luca Ceresoli <<a href="mailto:luca.ceresoli@bootlin.com" target="_blank">luca.ceresoli@bootlin.com</a>><br>
> >> > Reviewed-by: Maxime Ripard <<a href="mailto:mripard@kernel.org" target="_blank">mripard@kernel.org</a>><br>
> >> > Signed-off-by: Anusha Srivatsa <<a href="mailto:asrivats@redhat.com" target="_blank">asrivats@redhat.com</a>><br>
> >> ><br>
> >> > ---<br>
> >> > v4: Add refcounting documentation in this patch (Maxime)<br>
> >> ><br>
> >> > v3: Add include in this patch (Luca)<br>
> >> ><br>
> >> > v2: Export drm_panel_put/get() (Maxime)<br>
> >> > - Change commit log with better workding (Luca, Maxime)<br>
> >> > - Change drm_panel_put() to return void (Luca)<br>
> >> > - Code Cleanups - add return in documentation, replace bridge to<br>
> >> > panel (Luca)<br>
> >> > ---<br>
> >> > drivers/gpu/drm/drm_panel.c | 64 ++++++++++++++++++++++++++++++++++++++++++++-<br>
> >> > include/drm/drm_panel.h | 19 ++++++++++++++<br>
> >> > 2 files changed, 82 insertions(+), 1 deletion(-)<br>
> >> ><br>
> >> > diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c<br>
> >> > index bdeab5710ee324dc1742fbc77582250960556308..7b17531d85a4dc3031709919564d2e4d8332f748 100644<br>
> >> > --- a/drivers/gpu/drm/drm_panel.c<br>
> >> > +++ b/drivers/gpu/drm/drm_panel.c<br>
> >> > @@ -355,24 +355,86 @@ struct drm_panel *of_drm_find_panel(const struct device_node *np)<br>
> >> > }<br>
> >> > EXPORT_SYMBOL(of_drm_find_panel);<br>
> >> > <br>
> >> > +static void __drm_panel_free(struct kref *kref)<br>
> >> > +{<br>
> >> > + struct drm_panel *panel = container_of(kref, struct drm_panel, refcount);<br>
> >> > +<br>
> >> > + kfree(panel->container);<br>
> >> > +}<br>
> >> > +<br>
> >> > +/**<br>
> >> > + * drm_panel_get - Acquire a panel reference<br>
> >> > + * @panel: DRM panel<br>
> >> > + *<br>
> >> > + * This function increments the panel's refcount.<br>
> >> > + * Returns:<br>
> >> > + * Pointer to @panel<br>
> >> > + */<br>
> >> > +struct drm_panel *drm_panel_get(struct drm_panel *panel)<br>
> >> > +{<br>
> >> > + if (!panel)<br>
> >> > + return panel;<br>
> >> > +<br>
> >> > + kref_get(&panel->refcount);<br>
> >> > +<br>
> >> > + return panel;<br>
> >> > +}<br>
> >> > +EXPORT_SYMBOL(drm_panel_get);<br>
> >> > +<br>
> >> > +/**<br>
> >> > + * drm_panel_put - Release a panel reference<br>
> >> > + * @panel: DRM panel<br>
> >> > + *<br>
> >> > + * This function decrements the panel's reference count and frees the<br>
> >> > + * object if the reference count drops to zero.<br>
> >> > + */<br>
> >> > +void drm_panel_put(struct drm_panel *panel)<br>
> >> > +{<br>
> >> > + if (panel)<br>
> >> > + kref_put(&panel->refcount, __drm_panel_free);<br>
> >> > +}<br>
> >> > +EXPORT_SYMBOL(drm_panel_put);<br>
> >> > +<br>
> >> > +/**<br>
> >> > + * drm_panel_put_void - wrapper to drm_panel_put() taking a void pointer<br>
> >> > + *<br>
> >> > + * @data: pointer to @struct drm_panel, cast to a void pointer<br>
> >> > + *<br>
> >> > + * Wrapper of drm_panel_put() to be used when a function taking a void<br>
> >> > + * pointer is needed, for example as a devm action.<br>
> >> > + */<br>
> >> > +static void drm_panel_put_void(void *data)<br>
> >> > +{<br>
> >> > + struct drm_panel *panel = (struct drm_panel *)data;<br>
> >> > +<br>
> >> > + drm_panel_put(panel);<br>
> >> > +}<br>
> >> > +<br>
> >> > void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t offset,<br>
> >> > const struct drm_panel_funcs *funcs,<br>
> >> > int connector_type)<br>
> >> > {<br>
> >> > void *container;<br>
> >> > struct drm_panel *panel;<br>
> >> > + int err;<br>
> >> > <br>
> >> > if (!funcs) {<br>
> >> > dev_warn(dev, "Missing funcs pointer\n");<br>
> >> > return ERR_PTR(-EINVAL);<br>
> >> > }<br>
> >> > <br>
> >> > - container = devm_kzalloc(dev, size, GFP_KERNEL);<br>
> >> > + container = kzalloc(size, GFP_KERNEL);<br>
> >> > if (!container)<br>
> >> > return ERR_PTR(-ENOMEM);<br>
> >> > <br>
> >> > panel = container + offset;<br>
> >> > + panel->container = container;<br>
> >> > panel->funcs = funcs;<br>
> >> > + kref_init(&panel->refcount);<br>
> >> <br>
> >> Hi Anusha, this should be done in drm_panel_init() instead.<br>
> >><br>
> >> There are many users of drm_panel that don't use devm_drm_panel_alloc()<br>
> >> but allocate separately, and call drm_panel_init() only.<br>
> ><br>
> > That wouldn't really work, because then drivers would have allocated the<br>
> > panel with devm_kzalloc and thus the structure would be freed when the<br>
> > device is removed, no matter the reference counting state.<br>
> ><br>
> >> They'll all have refcount set to 0 instead of 1 like kref_init() does.<br>
> >> <br>
> >> This means all subsequent get/put pairs on such panels will lead to<br>
> >> __drm_panel_free() being called! But through a lucky coincidence, that<br>
> >> will be a nop because panel->container is also not initialized...<br>
> >> <br>
> >> I'm sorry to say, the drm refcounting interface is quite broken for such<br>
> >> use cases.<br>
> ><br>
> > The plan is to convert all panel drivers to that function, and Anusha<br>
> > already sent series to do. It still needs a bit of work, but it should<br>
> > land soon-ish.<br>
> ><br>
> > For the transitional period though, it's not clear to me what you think<br>
> > is broken at the moment, and / or what should be fixed.<br>
> ><br>
> > Would you prefer an explicit check on container not being 0, with a<br>
> > comment?<br>
> <br>
> I'm looking at what it would take to add drm_panel support to i915 so<br>
> that you could have drm_panel_followers on it. There are gaps of course,<br>
> but initially it would mean allocating and freeing drm_panel ourselves,<br>
> not via devm_drm_panel_alloc() nor devm_kzalloc(), because none of the<br>
> other stuff is allocated that way. drm_panel would just sit as a<br>
> sub-struct inside struct intel_panel, which is a sub-struct of struct<br>
> intel_connector, which has its own allocation...<br>
<br>
I'm not entirely sure why you would need to allocate it from i915? The<br>
drm_panel structure is only meant to be allocated by panel drivers, and<br>
afaik no panel interface controller is allocating it.<br>
<br>
> But basically in its current state, the refcounting would not be<br>
> reliable for that use case. I guess with panel->container being NULL<br>
> nothing happens, but the idea that the refcount drops back to 0 after a<br>
> get/put is a bit scary.<br>
> <br>
> Anyway, I think there should be no harm in moving the kref init to<br>
> drm_panel_init(), right?<br>
<br>
I mean, there is because the plan so far was to remove drm_panel_init() :)<br>
<br></blockquote><div><br></div><div>Jani,</div><div>the series that converts all drivers to use the new API:</div><div><a href="https://patchwork.freedesktop.org/series/147082/">https://patchwork.freedesktop.org/series/147082/</a></div><div><a href="https://patchwork.freedesktop.org/series/147157/">https://patchwork.freedesktop.org/series/147157/</a></div><div><a href="https://patchwork.freedesktop.org/series/147246/">https://patchwork.freedesktop.org/series/147246/</a></div><div><br></div><div>not landed yet but these are WIP. Still trying to understand your point though... not sure what is broken.</div><div><br></div><div><br></div><div>Anusha</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Maxime<br>
</blockquote></div></div>