<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Thu, Mar 13, 2025 at 10:42 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 Anusha,<br>
<br>
In addition to the feedback Luca already provided, I have a few comments<br>
<br>
On Wed, Mar 12, 2025 at 08:54:42PM -0400, Anusha Srivatsa wrote:<br>
> Introduce reference counted allocations for panels to avoid<br>
> use-after-free. The patch adds the macro devm_drm_bridge_alloc()<br>
> to allocate a new refcounted panel. Followed the documentation for<br>
> drmm_encoder_alloc() and devm_drm_dev_alloc and other similar<br>
> implementations for this purpose.<br>
> <br>
> Also adding drm_panel_get() and drm_panel_put() to suitably<br>
> increment and decrement the refcount<br>
> <br>
> Signed-off-by: Anusha Srivatsa <<a href="mailto:asrivats@redhat.com" target="_blank">asrivats@redhat.com</a>><br>
> ---<br>
>  drivers/gpu/drm/drm_panel.c | 50 ++++++++++++++++++++++++++++++++++++++<br>
>  include/drm/drm_panel.h     | 58 +++++++++++++++++++++++++++++++++++++++++++++<br>
>  2 files changed, 108 insertions(+)<br>
> <br>
> diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c<br>
> index c627e42a7ce70459f50eb5095fffc806ca45dabf..b55e380e4a2f7ffd940c207e841c197d85113907 100644<br>
> --- a/drivers/gpu/drm/drm_panel.c<br>
> +++ b/drivers/gpu/drm/drm_panel.c<br>
> @@ -79,6 +79,7 @@ EXPORT_SYMBOL(drm_panel_init);<br>
>   */<br>
>  void drm_panel_add(struct drm_panel *panel)<br>
>  {<br>
> +     drm_panel_get(panel);<br>
>       mutex_lock(&panel_lock);<br>
>       list_add_tail(&panel->list, &panel_list);<br>
>       mutex_unlock(&panel_lock);<br>
> @@ -96,6 +97,7 @@ void drm_panel_remove(struct drm_panel *panel)<br>
>       mutex_lock(&panel_lock);<br>
>       list_del_init(&panel->list);<br>
>       mutex_unlock(&panel_lock);<br>
> +     drm_panel_put(panel);<br>
>  }<br>
>  EXPORT_SYMBOL(drm_panel_remove);<br>
<br>
I think these two should be added as a separate patch, with some<br>
additional comment on why it's needed (because we store a pointer in the<br>
panel list).<br></blockquote><div>Sounds good. <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
>  <br>
> @@ -355,6 +357,54 @@ struct drm_panel *of_drm_find_panel(const struct device_node *np)<br>
>  }<br>
>  EXPORT_SYMBOL(of_drm_find_panel);<br>
>  <br>
> +/* Internal function (for refcounted panels) */<br>
> +void __drm_panel_free(struct kref *kref)<br>
> +{<br>
> +     struct drm_panel *panel = container_of(kref, struct drm_panel, refcount);<br>
> +     void *container = ((void *)panel) - panel->container_offset;<br>
> +<br>
> +     kfree(container);<br>
> +}<br>
> +EXPORT_SYMBOL(__drm_panel_free);<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>
> +{<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 = kzalloc(size, GFP_KERNEL);<br>
> +     if (!container)<br>
> +             return ERR_PTR(-ENOMEM);<br>
> +<br>
> +     panel = container + offset;<br>
> +     panel->container_offset = offset;<br>
> +     panel->funcs = funcs;<br>
> +     kref_init(&panel->refcount);<br>
> +<br>
> +     err = devm_add_action_or_reset(dev, drm_panel_put_void, panel);<br>
> +     if (err)<br>
> +             return ERR_PTR(err);<br>
> +<br>
> +     drm_panel_init(panel, dev, funcs, panel->connector_type);<br>
> +<br>
> +     return container;<br>
> +}<br>
> +EXPORT_SYMBOL(__devm_drm_panel_alloc);<br>
<br>
Similarly, here, I think we'd need to split that some more. Ideally, we<br>
should have a series of patches doing<br>
<br>
1: Adding that allocation function you have right now, but using<br>
   devm_kzalloc<br>
<br>
2: Adding the reference counting to drm_panel, with drm_panel_get /<br>
   drm_panel_put and the devm_action to put the reference in<br>
   __devm_drm_panel_alloc()<br>
<br>
3: Adding X patches to add calls to drm_bridge_get/drm_bridge_put<br>
   everywhere it's needed, starting indeed by<br>
   drm_panel_add/drm_panel_put. We don't have to do all of them in that<br>
   series though. of_drm_find_panel though will probably merit a series<br>
   of its own, given we'd have to fix all its callers too.<br>
<br>
4: Convert some panels to the new allocation function. You already did<br>
   that with panel_simple so there's nothing to change yet, but once we<br>
   agree on the API we should mass convert all the panels.<br>
<br></blockquote><div> </div><div>I want to get the API right before making mass conversion of drivers. Will split this patch as you have suggested above. Will leave out fixing of of_drm_find_panel() callers to a separate series as well.</div><div><br></div><div><br></div><div>Thanks!</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>