<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Thu, May 8, 2025 at 10:27 AM Jani Nikula <<a href="mailto:jani.nikula@linux.intel.com">jani.nikula@linux.intel.com</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">On Mon, 05 May 2025, Anusha Srivatsa <<a href="mailto:asrivats@redhat.com" target="_blank">asrivats@redhat.com</a>> wrote:<br>
> On Mon, May 5, 2025 at 2:54 AM Maxime Ripard <<a href="mailto:mripard@kernel.org" target="_blank">mripard@kernel.org</a>> wrote:<br>
><br>
>> 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<br>
>> after<br>
>> > >> > free by ensuring panel pointer is valid and can be usable till the<br>
>> 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>
>> ++++++++++++++++++++++++++++++++++++++++++++-<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<br>
>> b/drivers/gpu/drm/drm_panel.c<br>
>> > >> > index<br>
>> bdeab5710ee324dc1742fbc77582250960556308..7b17531d85a4dc3031709919564d2e4d8332f748<br>
>> 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<br>
>> 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<br>
>> 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<br>
>> 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<br>
>> 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<br>
>> 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,<br>
>> 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<br>
>> 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<br>
>> 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<br>
>> 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>
I'm looking into a use case involving drm_panel_follower, which requires<br>
a drm_panel. I don't really need any of the other stuff in drm_panel.<br>
<br>
And basically you'd have one drm_panel per connector that is connected<br>
to a panel, within the same driver.<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>
The problem with that is that it forces a certain type of allocation of<br>
drm_panel. devm_drm_panel_alloc() allows embedding drm_panel inside<br>
another struct, but that's inflexible for our use case, where we'd<br>
probably like to embed it inside something that's already allocated as<br>
part of something else.<br>
<br></blockquote><div>Jani, </div><div>will intel_connector_init() be one of the code points where this will be inflexible?</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">
I mean devm_drm_panel_alloc() is great, but please don't make its use<br>
mandatory!<br>
<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">> Jani,<br>
> the series that converts all drivers to use the new API:<br>
> <a href="https://patchwork.freedesktop.org/series/147082/" rel="noreferrer" target="_blank">https://patchwork.freedesktop.org/series/147082/</a><br>
> <a href="https://patchwork.freedesktop.org/series/147157/" rel="noreferrer" target="_blank">https://patchwork.freedesktop.org/series/147157/</a><br>
> <a href="https://patchwork.freedesktop.org/series/147246/" rel="noreferrer" target="_blank">https://patchwork.freedesktop.org/series/147246/</a><br>
><br>
> not landed yet but these are WIP. Still trying to understand your point<br>
> though... not sure what is broken.<br>
<br>
Nothing upstream is broken per se, but if you allocated drm_panel<br>
directly yourself, initialized it with drm_panel_init(), its refcount<br>
would initially be 0, not 1, and each subsequent get/put on it would<br>
call __drm_panel_free().<br>
<br>
Even that doesn't break stuff, because by luck panel->container is also<br>
NULL in this case.<br>
<br>
<br>
BR,<br>
Jani.<br>
<br>
-- <br>
Jani Nikula, Intel<br>
<br>
</blockquote></div></div>