<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Fri, May 9, 2025 at 8:45 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 Fri, 09 May 2025, Maxime Ripard <<a href="mailto:mripard@kernel.org" target="_blank">mripard@kernel.org</a>> wrote:<br>
> On Thu, May 08, 2025 at 05:27:21PM +0300, Jani Nikula wrote:<br>
>> 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>
> That answers why you need a drm_panel pointer, but not really why the<br>
> i915 needs to allocate it itself. The whole point of panel drivers it to<br>
> decouple panel drivers from the connector driver (and everything<br>
> upstream).<br>
><br>
> drm_panel is always allocated by the panel driver itself. I don't really<br>
> see a good reason to change that.<br>
><br>
> If you don't have a panel descriptor in the ACPI tables, then you can<br>
> always allocate a panel-edp driver or whatever from i915 and getting its<br>
> drm_panel?<br>
<br>
The thing is, absolutely none of our hardware/firmware/software stack<br>
was designed in a way that would fit the drm_panel model. (Or, arguably,<br>
drm_panel wasn't designed in a way that would fit our stack, in the<br>
chronology of things.)<br>
<br>
It's all one PCI device. All in the same MMIO space. The VBT (Video BIOS<br>
Tables) contain all the information for the panels, as well as for<br>
absolutely everything else about our display hardware. It's not separate<br>
in any meaningful way.<br>
<br>
Having a separate panel driver would get in the way. Having panel-edp<br>
would get in the way. That's why there isn't a single x86 user for<br>
drm_panel, AFAICT.<br>
<br>
Yes, we only really need the drm_panel handle, to play ball with the<br>
things that were built around it, specifically drm_panel_follower.<br>
<br>
And we do need to allocate drm_panel ourselves. We're both the host and<br>
the panel driver at the same time, and there's no benefit in trying to<br>
articially change that. DRM infrastructure should provide frameworks<br>
that are usable for everyone, instead of trying to force everyone into<br>
the same model, whether it makes sense or not.<br>
<br></blockquote><div>The point was to actually make it easier and less prone to bugs and not the other way around.</div><div>Trying to understand the changes needed to make this work for i915 usecase.</div><div><br></div><div>I get that embedding drm_panel within intel_panel which is embedded</div><div>within intel_connector, which also embeds drm_connector and so on can get confusing to use. But Jani,</div><div>about drm_panel_follower , which is the panel that is following the intel_panel? Is it the associated</div><div>touchscreen if any or some other device?</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">
<br>
BR,<br>
Jani.<br>
<br>
-- <br>
Jani Nikula, Intel<br>
<br>
</blockquote></div></div>