Skip to contentSkip to conversation

drm/panel: Refcounted panel allocation

RFC PATCH In review Anusha Srivatsa 4 participants 12 messages Mar 13, 2025 – Mar 17, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover drm/panel: Refcounted panel allocation

Primary motivation is to avoid havig a dangling pointer with panels. In a scenario where panel device no longer exists, the DRM driver has no idea of it. It can continue to try and access a panel leading to use-after-free situations.
This is the initial draft more for feedback only. Once the path forward is clear, further drivers will be converted to using the API.

---
Anusha Srivatsa (2):
drm/panel: Add new helpers for refcounted panel allocatons drm/panel/panel-simple: Use the new allocation in place of devm_kzalloc()

 drivers/gpu/drm/drm_panel.c          | 50 +++++++++++++++++++++++++++++++
 drivers/gpu/drm/panel/panel-simple.c |  4 +--
 include/drm/drm_panel.h              | 58 ++++++++++++++++++++++++++++++++++++
 3 files changed, 109 insertions(+), 3 deletions(-)

---
base-commit: 3282422bf251db541fe07c548ca304130d37d754
change-id: 20250311-drm-panel-c327992e932e

Best regards,

Signature · 1 line
Anusha Srivatsa <[email protected]>
#1 lore raw

RFC PATCH 1/2 drm/panel: Add new helpers for refcounted panel allocatons

Introduce reference counted allocations for panels to avoid use-after-free. The patch adds the macro devm_drm_bridge_alloc() to allocate a new refcounted panel. Followed the documentation for drmm_encoder_alloc() and devm_drm_dev_alloc and other similar implementations for this purpose.

Also adding drm_panel_get() and drm_panel_put() to suitably increment and decrement the refcount

---
 drivers/gpu/drm/drm_panel.c | 50 ++++++++++++++++++++++++++++++++++++++
 include/drm/drm_panel.h     | 58 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+)
drivers/gpu/drm/drm_panel.c +50 -0
index c627e42a7ce70459f50eb5095fffc806ca45dabf..b55e380e4a2f7ffd940c207e841c197d85113907 100644
@@ -79,6 +79,7 @@ EXPORT_SYMBOL(drm_panel_init);
  */
 void drm_panel_add(struct drm_panel *panel)
 {
+	drm_panel_get(panel);
 	mutex_lock(&panel_lock);
 	list_add_tail(&panel->list, &panel_list);
 	mutex_unlock(&panel_lock);
@@ -96,6 +97,7 @@ void drm_panel_remove(struct drm_panel *panel)
 	mutex_lock(&panel_lock);
 	list_del_init(&panel->list);
 	mutex_unlock(&panel_lock);
+	drm_panel_put(panel);
 }
 EXPORT_SYMBOL(drm_panel_remove);
 
@@ -355,6 +357,54 @@ struct drm_panel *of_drm_find_panel(const struct device_node *np)
 }
 EXPORT_SYMBOL(of_drm_find_panel);
 
+/* Internal function (for refcounted panels) */
+void __drm_panel_free(struct kref *kref)
+{
+	struct drm_panel *panel = container_of(kref, struct drm_panel, refcount);
+	void *container = ((void *)panel) - panel->container_offset;
+
+	kfree(container);
+}
+EXPORT_SYMBOL(__drm_panel_free);
+
+static void drm_panel_put_void(void *data)
+{
+	struct drm_panel *panel = (struct drm_panel *)data;
+
+	drm_panel_put(panel);
+}
+
+void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t offset,
+			     const struct drm_panel_funcs *funcs)
+{
+	void *container;
+	struct drm_panel *panel;
+	int err;
+
+	if (!funcs) {
+		dev_warn(dev, "Missing funcs pointer\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	container = kzalloc(size, GFP_KERNEL);
+	if (!container)
+		return ERR_PTR(-ENOMEM);
+
+	panel = container + offset;
+	panel->container_offset = offset;
+	panel->funcs = funcs;
+	kref_init(&panel->refcount);
+
+	err = devm_add_action_or_reset(dev, drm_panel_put_void, panel);
+	if (err)
+		return ERR_PTR(err);
+
+	drm_panel_init(panel, dev, funcs, panel->connector_type);
+
+	return container;
+}
+EXPORT_SYMBOL(__devm_drm_panel_alloc);
+
 /**
  * of_drm_get_panel_orientation - look up the orientation of the panel through
  * the "rotation" binding from a device tree node
include/drm/drm_panel.h +58 -0

72 lines not on this page · raw message

Signature · 1 line
2.48.1
#9 lore raw

RFC PATCH 2/2 drm/panel/panel-simple: Use the new allocation in place of devm_kzalloc()

In reply to Anusha Srivatsa

Start using the new helper that does the refcounted allocations.

---
 drivers/gpu/drm/panel/panel-simple.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/gpu/drm/panel/panel-simple.c +1 -3
index 232b03c1a259eb15e423b9d452d28e2ff95c70f8..d7530c3533af34f83ce8c6d6067e7f293f2d4bf1 100644
@@ -579,7 +579,7 @@ static int panel_simple_probe(struct device *dev, const struct panel_desc *desc)
 	u32 bus_flags;
 	int err;
 
-	panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
+	panel = devm_drm_panel_alloc(dev, struct panel_simple, base, &panel_simple_funcs);
 	if (!panel)
 		return -ENOMEM;
 
@@ -694,8 +694,6 @@ static int panel_simple_probe(struct device *dev, const struct panel_desc *desc)
 	pm_runtime_set_autosuspend_delay(dev, 1000);
 	pm_runtime_use_autosuspend(dev);
 
-	drm_panel_init(&panel->base, dev, &panel_simple_funcs, connector_type);
-
 	err = drm_panel_of_backlight(&panel->base);
 	if (err) {
 		dev_err_probe(dev, err, "Could not find backlight\n");
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH RFC 1/2] drm/panel: Add new helpers for refcounted panel allocatons

In reply to Anusha Srivatsa

Hello Anusha,

Quoted 10 lines · On Wed, 12 Mar 2025 20:54:42 -0400 Anusha Srivatsa <[email protected]> wrote:

Introduce reference counted allocations for panels to avoid use-after-free. The patch adds the macro devm_drm_bridge_alloc() to allocate a new refcounted panel. Followed the documentation for drmm_encoder_alloc() and devm_drm_dev_alloc and other similar implementations for this purpose.

Also adding drm_panel_get() and drm_panel_put() to suitably increment and decrement the refcount

I'm very happy to see the very first step of the panel rework mentioned by Maxime see the light! :-)

This patch looks mostly good to me, and the similarity with my bridge refcounting work is by itself reassuring.

I have a few notes, one is relevant and the others are minor details, see below.

In the Subject line: s/allocatons/allocations/

[...]

Quoted 16 lines
drivers/gpu/drm/drm_panel.c +16 -0
+void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t offset,
+			     const struct drm_panel_funcs *funcs)
+{
+	void *container;
+	struct drm_panel *panel;
+	int err;
+
+	if (!funcs) {
+		dev_warn(dev, "Missing funcs pointer\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	container = kzalloc(size, GFP_KERNEL);
+	if (!container)
+		return ERR_PTR(-ENOMEM);
+

drivers/gpu/drm/drm_panel.c struct drm_panel *of_drm_find_panel(const struct device_node *np) · line 402

Quoted 10 lines
drivers/gpu/drm/drm_panel.c +10 -0
+	panel = container + offset;
+	panel->container_offset = offset;
+	panel->funcs = funcs;
+	kref_init(&panel->refcount);
+
+	err = devm_add_action_or_reset(dev, drm_panel_put_void, panel);
+	if (err)
+		return ERR_PTR(err);
+
+	drm_panel_init(panel, dev, funcs, panel->connector_type);

panel->connector_type here is uninitialized. You are passing panel->connector_type to drm_panel_init(), which will then copy it into panel->connector_type itself.

include/drm/drm_panel.h struct drm_panel { · line 276

Quoted 7 lines
include/drm/drm_panel.h +7 -0
+
+	/**
+	 * @container_offset: Offset of this struct within the container
+	 * struct embedding it. Used for refcounted panels to free the
+	 * embeddeing struct when the refcount drops to zero.
+	 */
+	size_t container_offset;

While storing the offset obviously works, and that's what I had implemented in my latest bridge refcounting series, after some discussion with Maxime we agreed storing a container pointer instead of the offset is cleaner. I think it would be good here as well.

See: https://lore.kernel.org/lkml/20250227-macho-convivial-tody-cea7dc@houat/

include/drm/drm_panel.h struct drm_panel { · line 294

Quoted 10 lines
include/drm/drm_panel.h +10 -0
+/**
+ * drm_panel_get - Acquire a panel reference
+ * @panel: DRM panel
+ *
+ * This function increments the panel's refcount.
+ *
+ */
+static inline void drm_panel_get(struct drm_panel *panel)
+{
+

Remove empty line.

include/drm/drm_panel.h struct drm_panel { · line 314

Quoted 5 lines
include/drm/drm_panel.h +5 -0
+void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t offset,
+			     const struct drm_panel_funcs *funcs);
+
+/**
+ * devm_drm_panel_alloc - Allocate and initialize an refcounted panel

s/an/a/ -- same typo as in my bridge series so I'm fixing it in my series as well :)

Quoted 6 lines

+ * @dev: struct device of the panel device
+ * @type: the type of the struct which contains struct &drm_panel
+ * @member: the name of the &drm_panel within @type
+ * @funcs: callbacks for this panel
+ *
+ * The returned refcount is initialised to 1

In my opinion it is important to clarify that the caller does not have to explicitly call drm_panel_put() on the returned pointer, because devm will do it. Without clarifying, a user might think they need to, and that would result in an extra put, which would be a bug.

Adapting from [0], that would be:

* The returned refcount is initialized to 1. This reference will be
* automatically dropped via devm (by calling drm_panel_put()) when @dev
* is removed.

[0] https://lore.kernel.org/lkml/[email protected]/

Luca

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
maintainer
#3 lore raw

Re: [PATCH RFC 1/2] drm/panel: Add new helpers for refcounted panel allocatons

Quoted 51 lines · Luca Ceresoli

Hello Anusha,

Quoted 10 lines · On Wed, 12 Mar 2025 20:54:42 -0400 Anusha Srivatsa <[email protected]> wrote:

Introduce reference counted allocations for panels to avoid use-after-free. The patch adds the macro devm_drm_bridge_alloc() to allocate a new refcounted panel. Followed the documentation for drmm_encoder_alloc() and devm_drm_dev_alloc and other similar implementations for this purpose.

Also adding drm_panel_get() and drm_panel_put() to suitably increment and decrement the refcount

I'm very happy to see the very first step of the panel rework mentioned by Maxime see the light! :-)

This patch looks mostly good to me, and the similarity with my bridge refcounting work is by itself reassuring.

I have a few notes, one is relevant and the others are minor details, see below.

In the Subject line: s/allocatons/allocations/

[...]

Quoted 22 lines
drivers/gpu/drm/drm_panel.c +22 -0
+void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t offset,
+			     const struct drm_panel_funcs *funcs)
+{
+	void *container;
+	struct drm_panel *panel;
+	int err;
+
+	if (!funcs) {
+		dev_warn(dev, "Missing funcs pointer\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	container = kzalloc(size, GFP_KERNEL);
+	if (!container)
+		return ERR_PTR(-ENOMEM);
+
+	panel = container + offset;
+	panel->container_offset = offset;
+	panel->funcs = funcs;
+	kref_init(&panel->refcount);
+
+	err = devm_add_action_or_reset(dev, drm_panel_put_void, panel);
Quoted 8 lines

drivers/gpu/drm/drm_panel.c struct drm_panel *of_drm_find_panel(const struct device_node *np) · line 402

Quoted 4 lines
drivers/gpu/drm/drm_panel.c +4 -0
+	if (err)
+		return ERR_PTR(err);
+
+	drm_panel_init(panel, dev, funcs, panel->connector_type);

panel->connector_type here is uninitialized. You are passing panel->connector_type to drm_panel_init(), which will then copy it into panel->connector_type itself.

Yeah, we need to have the connector type as a parameter.

Maxime

maintainer
#7 lore raw

Re: [PATCH RFC 1/2] drm/panel: Add new helpers for refcounted panel allocatons

In reply to Anusha Srivatsa

Hi Anusha,

In addition to the feedback Luca already provided, I have a few comments

Quoted 28 lines · Anusha Srivatsa

Introduce reference counted allocations for panels to avoid use-after-free. The patch adds the macro devm_drm_bridge_alloc() to allocate a new refcounted panel. Followed the documentation for drmm_encoder_alloc() and devm_drm_dev_alloc and other similar implementations for this purpose.

Also adding drm_panel_get() and drm_panel_put() to suitably increment and decrement the refcount

---
 drivers/gpu/drm/drm_panel.c | 50 ++++++++++++++++++++++++++++++++++++++
 include/drm/drm_panel.h     | 58 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+)
drivers/gpu/drm/drm_panel.c +1 -0
index c627e42a7ce70459f50eb5095fffc806ca45dabf..b55e380e4a2f7ffd940c207e841c197d85113907 100644
@@ -79,6 +79,7 @@ EXPORT_SYMBOL(drm_panel_init);
  */
 void drm_panel_add(struct drm_panel *panel)
 {
+	drm_panel_get(panel);
 	mutex_lock(&panel_lock);
 	list_add_tail(&panel->list, &panel_list);
 	mutex_unlock(&panel_lock);

drivers/gpu/drm/drm_panel.c void drm_panel_remove(struct drm_panel *panel) · line 102

Quoted 6 lines
drivers/gpu/drm/drm_panel.c +1 -0
@@ -96,6 +97,7 @@ void drm_panel_remove(struct drm_panel *panel)
 	mutex_lock(&panel_lock);
 	list_del_init(&panel->list);
 	mutex_unlock(&panel_lock);
+	drm_panel_put(panel);
 }
 EXPORT_SYMBOL(drm_panel_remove);

I think these two should be added as a separate patch, with some additional comment on why it's needed (because we store a pointer in the panel list).

Quoted 42 lines
drivers/gpu/drm/drm_panel.c +37 -0
@@ -355,6 +357,54 @@ struct drm_panel *of_drm_find_panel(const struct device_node *np)
 }
 EXPORT_SYMBOL(of_drm_find_panel);
 
+/* Internal function (for refcounted panels) */
+void __drm_panel_free(struct kref *kref)
+{
+	struct drm_panel *panel = container_of(kref, struct drm_panel, refcount);
+	void *container = ((void *)panel) - panel->container_offset;
+
+	kfree(container);
+}
+EXPORT_SYMBOL(__drm_panel_free);
+
+static void drm_panel_put_void(void *data)
+{
+	struct drm_panel *panel = (struct drm_panel *)data;
+
+	drm_panel_put(panel);
+}
+
+void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t offset,
+			     const struct drm_panel_funcs *funcs)
+{
+	void *container;
+	struct drm_panel *panel;
+	int err;
+
+	if (!funcs) {
+		dev_warn(dev, "Missing funcs pointer\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	container = kzalloc(size, GFP_KERNEL);
+	if (!container)
+		return ERR_PTR(-ENOMEM);
+
+	panel = container + offset;
+	panel->container_offset = offset;
+	panel->funcs = funcs;
+	kref_init(&panel->refcount);

drivers/gpu/drm/drm_panel.c struct drm_panel *of_drm_find_panel(const struct device_node *np) · line 406

Quoted 10 lines
drivers/gpu/drm/drm_panel.c +10 -0
+
+	err = devm_add_action_or_reset(dev, drm_panel_put_void, panel);
+	if (err)
+		return ERR_PTR(err);
+
+	drm_panel_init(panel, dev, funcs, panel->connector_type);
+
+	return container;
+}
+EXPORT_SYMBOL(__devm_drm_panel_alloc);

Similarly, here, I think we'd need to split that some more. Ideally, we should have a series of patches doing

1: Adding that allocation function you have right now, but using
devm_kzalloc

2: Adding the reference counting to drm_panel, with drm_panel_get /
drm_panel_put and the devm_action to put the reference in __devm_drm_panel_alloc()

3: Adding X patches to add calls to drm_bridge_get/drm_bridge_put
everywhere it's needed, starting indeed by drm_panel_add/drm_panel_put. We don't have to do all of them in that series though. of_drm_find_panel though will probably merit a series of its own, given we'd have to fix all its callers too.

4: Convert some panels to the new allocation function. You already did
that with panel_simple so there's nothing to change yet, but once we agree on the API we should mass convert all the panels.

Maxime

#4 lore raw

Re: [PATCH RFC 1/2] drm/panel: Add new helpers for refcounted panel allocatons

In reply to Luca Ceresoli
Quoted 19 lines · Luca Ceresoli

Hello Anusha,

Quoted 10 lines · On Wed, 12 Mar 2025 20:54:42 -0400 Anusha Srivatsa <[email protected]> wrote:

Introduce reference counted allocations for panels to avoid use-after-free. The patch adds the macro devm_drm_bridge_alloc() to allocate a new refcounted panel. Followed the documentation for drmm_encoder_alloc() and devm_drm_dev_alloc and other similar implementations for this purpose.

Also adding drm_panel_get() and drm_panel_put() to suitably increment and decrement the refcount

I'm very happy to see the very first step of the panel rework mentioned by Maxime see the light! :-)

Quoted 8 lines

This patch looks mostly good to me, and the similarity with my bridge refcounting work is by itself reassuring.

I have a few notes, one is relevant and the others are minor details, see below.

In the Subject line: s/allocatons/allocations/

good catch.

Quoted 35 lines

[...]

Quoted 1 line

+void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t

offset,

Quoted 15 lines
drivers/gpu/drm/drm_panel.c +15 -0
+                          const struct drm_panel_funcs *funcs)
+{
+     void *container;
+     struct drm_panel *panel;
+     int err;
+
+     if (!funcs) {
+             dev_warn(dev, "Missing funcs pointer\n");
+             return ERR_PTR(-EINVAL);
+     }
+
+     container = kzalloc(size, GFP_KERNEL);
+     if (!container)
+             return ERR_PTR(-ENOMEM);
+

drivers/gpu/drm/drm_panel.c struct drm_panel *of_drm_find_panel(const struct device_node *np) · line 402

Quoted 10 lines
drivers/gpu/drm/drm_panel.c +10 -0
+     panel = container + offset;
+     panel->container_offset = offset;
+     panel->funcs = funcs;
+     kref_init(&panel->refcount);
+
+     err = devm_add_action_or_reset(dev, drm_panel_put_void, panel);
+     if (err)
+             return ERR_PTR(err);
+
+     drm_panel_init(panel, dev, funcs, panel->connector_type);

panel->connector_type here is uninitialized. You are passing panel->connector_type to drm_panel_init(), which will then copy it into panel->connector_type itself.

Quoted 1 line

So you mean I pass connector_type from the driver calling the helper, so

there is access to the connector type here?

Quoted 8 lines

include/drm/drm_panel.h struct drm_panel { · line 276

Quoted 7 lines
include/drm/drm_panel.h +7 -0
+
+     /**
+      * @container_offset: Offset of this struct within the container
+      * struct embedding it. Used for refcounted panels to free the
+      * embeddeing struct when the refcount drops to zero.
+      */
+     size_t container_offset;
Quoted 8 lines

While storing the offset obviously works, and that's what I had implemented in my latest bridge refcounting series, after some discussion with Maxime we agreed storing a container pointer instead of the offset is cleaner. I think it would be good here as well.

See:
https://lore.kernel.org/lkml/20250227-macho-convivial-tody-cea7dc@houat/

so just void *container instead of size_t container_offset.

Quoted 4 lines
Quoted 4 lines

+/**
+ * drm_panel_get - Acquire a panel reference
+ * @panel: DRM panel
+ *

Quoted 9 lines

include/drm/drm_panel.h struct drm_panel { · line 294

Quoted 6 lines

+ * This function increments the panel's refcount.
+ *
+ */

include/drm/drm_panel.h +3 -0
+static inline void drm_panel_get(struct drm_panel *panel)
+{
+

Remove empty line.

will do.

Quoted 23 lines
Quoted 2 lines

+void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t

offset,

include/drm/drm_panel.h struct drm_panel { · line 314

Quoted 4 lines
include/drm/drm_panel.h +4 -0
+                          const struct drm_panel_funcs *funcs);
+
+/**
+ * devm_drm_panel_alloc - Allocate and initialize an refcounted panel

s/an/a/ -- same typo as in my bridge series so I'm fixing it in my series as well :)

Quoted 6 lines

+ * @dev: struct device of the panel device
+ * @type: the type of the struct which contains struct &drm_panel
+ * @member: the name of the &drm_panel within @type
+ * @funcs: callbacks for this panel
+ *
+ * The returned refcount is initialised to 1

In my opinion it is important to clarify that the caller does not have to explicitly call drm_panel_put() on the returned pointer, because devm will do it. Without clarifying, a user might think they need to, and that would result in an extra put, which would be a bug.

Quoted 9 lines

Adapting from [0], that would be:

* The returned refcount is initialized to 1. This reference will be
* automatically dropped via devm (by calling drm_panel_put()) when @dev
* is removed.

[0]
https://lore.kernel.org/lkml/[email protected]/

WIll make this change.

Thanks for the feedback!

Anusha

Quoted 8 lines

Luca

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

#8 lore raw

Re: [PATCH RFC 1/2] drm/panel: Add new helpers for refcounted panel allocatons

In reply to Maxime Ripard
Quoted 38 lines · Maxime Ripard

Hi Anusha,

In addition to the feedback Luca already provided, I have a few comments

Quoted 6 lines · Anusha Srivatsa

Introduce reference counted allocations for panels to avoid use-after-free. The patch adds the macro devm_drm_bridge_alloc() to allocate a new refcounted panel. Followed the documentation for drmm_encoder_alloc() and devm_drm_dev_alloc and other similar implementations for this purpose.

Quoted 7 lines

Also adding drm_panel_get() and drm_panel_put() to suitably increment and decrement the refcount

---
 drivers/gpu/drm/drm_panel.c | 50 ++++++++++++++++++++++++++++++++++++++
 include/drm/drm_panel.h     | 58

+++++++++++++++++++++++++++++++++++++++++++++

Quoted 4 lines
 2 files changed, 108 insertions(+)
drivers/gpu/drm/drm_panel.c +0 -0

index

c627e42a7ce70459f50eb5095fffc806ca45dabf..b55e380e4a2f7ffd940c207e841c197d85113907 100644

Quoted 13 lines
drivers/gpu/drm/drm_panel.c +1 -0
@@ -79,6 +79,7 @@ EXPORT_SYMBOL(drm_panel_init);
  */
 void drm_panel_add(struct drm_panel *panel)
 {
+     drm_panel_get(panel);
      mutex_lock(&panel_lock);
      list_add_tail(&panel->list, &panel_list);
      mutex_unlock(&panel_lock);
@@ -96,6 +97,7 @@ void drm_panel_remove(struct drm_panel *panel)
      mutex_lock(&panel_lock);
      list_del_init(&panel->list);
Quoted 9 lines

drivers/gpu/drm/drm_panel.c void drm_panel_remove(struct drm_panel *panel) · line 102

Quoted 4 lines
drivers/gpu/drm/drm_panel.c +1 -0
      mutex_unlock(&panel_lock);
+     drm_panel_put(panel);
 }
 EXPORT_SYMBOL(drm_panel_remove);

I think these two should be added as a separate patch, with some additional comment on why it's needed (because we store a pointer in the panel list).

Sounds good.

Quoted 72 lines

drivers/gpu/drm/drm_panel.c struct drm_panel *of_drm_find_panel(const struct

Quoted 2 lines
drivers/gpu/drm/drm_panel.c +0 -0
@@ -355,6 +357,54 @@ struct drm_panel *of_drm_find_panel(const struct

device_node *np)

drivers/gpu/drm/drm_panel.c struct drm_panel *of_drm_find_panel(const struct device_node *np)

Quoted 7 lines
drivers/gpu/drm/drm_panel.c +4 -0
 }
 EXPORT_SYMBOL(of_drm_find_panel);
 
+/* Internal function (for refcounted panels) */
+void __drm_panel_free(struct kref *kref)
+{
+     struct drm_panel *panel = container_of(kref, struct drm_panel,

refcount);

Quoted 4 lines
drivers/gpu/drm/drm_panel.c +4 -0
+     void *container = ((void *)panel) - panel->container_offset;
+
+     kfree(container);
+}

drivers/gpu/drm/drm_panel.c struct drm_panel *of_drm_find_panel(const struct device_node *np)

Quoted 10 lines
drivers/gpu/drm/drm_panel.c +10 -0
+EXPORT_SYMBOL(__drm_panel_free);
+
+static void drm_panel_put_void(void *data)
+{
+     struct drm_panel *panel = (struct drm_panel *)data;
+
+     drm_panel_put(panel);
+}
+
+void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t

offset,

Quoted 19 lines
drivers/gpu/drm/drm_panel.c +19 -0
+                          const struct drm_panel_funcs *funcs)
+{
+     void *container;
+     struct drm_panel *panel;
+     int err;
+
+     if (!funcs) {
+             dev_warn(dev, "Missing funcs pointer\n");
+             return ERR_PTR(-EINVAL);
+     }
+
+     container = kzalloc(size, GFP_KERNEL);
+     if (!container)
+             return ERR_PTR(-ENOMEM);
+
+     panel = container + offset;
+     panel->container_offset = offset;
+     panel->funcs = funcs;
+     kref_init(&panel->refcount);

drivers/gpu/drm/drm_panel.c struct drm_panel *of_drm_find_panel(const struct device_node *np) · line 406

Quoted 10 lines
drivers/gpu/drm/drm_panel.c +10 -0
+
+     err = devm_add_action_or_reset(dev, drm_panel_put_void, panel);
+     if (err)
+             return ERR_PTR(err);
+
+     drm_panel_init(panel, dev, funcs, panel->connector_type);
+
+     return container;
+}
+EXPORT_SYMBOL(__devm_drm_panel_alloc);

Similarly, here, I think we'd need to split that some more. Ideally, we should have a series of patches doing

1: Adding that allocation function you have right now, but using
devm_kzalloc

2: Adding the reference counting to drm_panel, with drm_panel_get /
drm_panel_put and the devm_action to put the reference in __devm_drm_panel_alloc()

3: Adding X patches to add calls to drm_bridge_get/drm_bridge_put
everywhere it's needed, starting indeed by drm_panel_add/drm_panel_put. We don't have to do all of them in that series though. of_drm_find_panel though will probably merit a series of its own, given we'd have to fix all its callers too.

Quoted 5 lines

4: Convert some panels to the new allocation function. You already did
that with panel_simple so there's nothing to change yet, but once we agree on the API we should mass convert all the panels.

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.

Thanks!
Anusha

Quoted 2 lines

Maxime

#5 lore raw

Re: [PATCH RFC 1/2] drm/panel: Add new helpers for refcounted panel allocatons

In reply to Anusha Srivatsa

Hello Anusha,

Quoted 32 lines · On Thu, 13 Mar 2025 16:34:45 -0400 Anusha Srivatsa <[email protected]> wrote:
Quoted 32 lines
Quoted 1 line

+void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t

offset,

Quoted 15 lines
drivers/gpu/drm/drm_panel.c +15 -0
+                          const struct drm_panel_funcs *funcs)
+{
+     void *container;
+     struct drm_panel *panel;
+     int err;
+
+     if (!funcs) {
+             dev_warn(dev, "Missing funcs pointer\n");
+             return ERR_PTR(-EINVAL);
+     }
+
+     container = kzalloc(size, GFP_KERNEL);
+     if (!container)
+             return ERR_PTR(-ENOMEM);
+

drivers/gpu/drm/drm_panel.c struct drm_panel *of_drm_find_panel(const struct device_node *np) · line 402

Quoted 10 lines
drivers/gpu/drm/drm_panel.c +10 -0
+     panel = container + offset;
+     panel->container_offset = offset;
+     panel->funcs = funcs;
+     kref_init(&panel->refcount);
+
+     err = devm_add_action_or_reset(dev, drm_panel_put_void, panel);
+     if (err)
+             return ERR_PTR(err);
+
+     drm_panel_init(panel, dev, funcs, panel->connector_type);  

panel->connector_type here is uninitialized. You are passing panel->connector_type to drm_panel_init(), which will then copy it into panel->connector_type itself.

Quoted 2 lines
Quoted 1 line

So you mean I pass connector_type from the driver calling the helper, so

there is access to the connector type here?

I'm not a panel expert, but I think it makes sense that to create the panel you need to know the connection type, and that is what Maxime suggested.

Quoted 12 lines
Quoted 12 lines

include/drm/drm_panel.h struct drm_panel { · line 276

Quoted 6 lines
include/drm/drm_panel.h +6 -0
+     /**
+      * @container_offset: Offset of this struct within the container
+      * struct embedding it. Used for refcounted panels to free the
+      * embeddeing struct when the refcount drops to zero.
+      */
+     size_t container_offset;  

While storing the offset obviously works, and that's what I had implemented in my latest bridge refcounting series, after some discussion with Maxime we agreed storing a container pointer instead of the offset is cleaner. I think it would be good here as well.

Quoted 5 lines
Quoted 3 lines

See:
https://lore.kernel.org/lkml/20250227-macho-convivial-tody-cea7dc@houat/

so just void *container instead of size_t container_offset.

Exactly. You can have a look at the patch I sent earlier today:
https://lore.kernel.org/lkml/[email protected]/

Luca

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#6 lore raw

Re: [PATCH RFC 1/2] drm/panel: Add new helpers for refcounted panel allocatons

Quoted 61 lines · Luca Ceresoli

Hello Anusha,

Quoted 1 line · On Thu, 13 Mar 2025 16:34:45 -0400 Anusha Srivatsa <[email protected]> wrote:
Quoted 1 line
Quoted 1 line

+void *__devm_drm_panel_alloc(struct device *dev, size_t size,

size_t

Quoted 31 lines
Quoted 31 lines

offset,

Quoted 15 lines
drivers/gpu/drm/drm_panel.c +15 -0
+                          const struct drm_panel_funcs *funcs)
+{
+     void *container;
+     struct drm_panel *panel;
+     int err;
+
+     if (!funcs) {
+             dev_warn(dev, "Missing funcs pointer\n");
+             return ERR_PTR(-EINVAL);
+     }
+
+     container = kzalloc(size, GFP_KERNEL);
+     if (!container)
+             return ERR_PTR(-ENOMEM);
+

drivers/gpu/drm/drm_panel.c struct drm_panel *of_drm_find_panel(const struct device_node *np) · line 402

Quoted 10 lines
drivers/gpu/drm/drm_panel.c +10 -0
+     panel = container + offset;
+     panel->container_offset = offset;
+     panel->funcs = funcs;
+     kref_init(&panel->refcount);
+
+     err = devm_add_action_or_reset(dev, drm_panel_put_void, panel);
+     if (err)
+             return ERR_PTR(err);
+
+     drm_panel_init(panel, dev, funcs, panel->connector_type);

panel->connector_type here is uninitialized. You are passing panel->connector_type to drm_panel_init(), which will then copy it into panel->connector_type itself.

Quoted 1 line
Quoted 1 line

So you mean I pass connector_type from the driver calling the helper,

so

Quoted 1 line

there is access to the connector type here?

I'm not a panel expert, but I think it makes sense that to create the panel you need to know the connection type, and that is what Maxime suggested.

Quoted 2 lines
Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+     /**
+      * @container_offset: Offset of this struct within the

container

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
include/drm/drm_panel.h +4 -0
+      * struct embedding it. Used for refcounted panels to free the
+      * embeddeing struct when the refcount drops to zero.
+      */
+     size_t container_offset;
Quoted 7 lines
Quoted 7 lines

While storing the offset obviously works, and that's what I had implemented in my latest bridge refcounting series, after some discussion with Maxime we agreed storing a container pointer instead of the offset is cleaner. I think it would be good here as well.

See:

Quoted 10 lines

https://lore.kernel.org/lkml/20250227-macho-convivial-tody-cea7dc@houat/

Quoted 3 lines
Quoted 1 line

so just void *container instead of size_t container_offset.

Exactly. You can have a look at the patch I sent earlier today:

https://lore.kernel.org/lkml/[email protected]/

This helps. Thanks

Anusha

Quoted 8 lines

Luca

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

maintainer
#10 lore raw

Re: [PATCH RFC 2/2] drm/panel/panel-simple: Use the new allocation in place of devm_kzalloc()

In reply to Anusha Srivatsa
Quoted 22 lines · Anusha Srivatsa

Start using the new helper that does the refcounted allocations.

---
  drivers/gpu/drm/panel/panel-simple.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
drivers/gpu/drm/panel/panel-simple.c +1 -1
index 232b03c1a259eb15e423b9d452d28e2ff95c70f8..d7530c3533af34f83ce8c6d6067e7f293f2d4bf1 100644
@@ -579,7 +579,7 @@ static int panel_simple_probe(struct device *dev, const struct panel_desc *desc)
  	u32 bus_flags;
  	int err;
 
-	panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
+	panel = devm_drm_panel_alloc(dev, struct panel_simple, base, &panel_simple_funcs);
  	if (!panel)
  		return -ENOMEM;
 

drivers/gpu/drm/panel/panel-simple.c static int panel_simple_probe(struct device *dev, const struct panel_desc *desc) · line 697

Quoted 4 lines
drivers/gpu/drm/panel/panel-simple.c +0 -1
@@ -694,8 +694,6 @@ static int panel_simple_probe(struct device *dev, const struct panel_desc *desc)
  	pm_runtime_set_autosuspend_delay(dev, 1000);
  	pm_runtime_use_autosuspend(dev);
 
-	drm_panel_init(&panel->base, dev, &panel_simple_funcs, connector_type);

As pointed by the helpers review, you're loosing the connector_type info here.

drivers/gpu/drm/panel/panel-simple.c static int panel_simple_probe(struct device *dev, const struct panel_desc *desc) · line 699

Quoted 5 lines
drivers/gpu/drm/panel/panel-simple.c +0 -1
-
  	err = drm_panel_of_backlight(&panel->base);
  	if (err) {
  		dev_err_probe(dev, err, "Could not find backlight\n");

Thanks !
Neil

#11 lore raw

Re: [PATCH RFC 2/2] drm/panel/panel-simple: Use the new allocation in place of devm_kzalloc()

Quoted 32 lines · Neil Armstrong

Review

Quoted 9 lines · Anusha Srivatsa

Start using the new helper that does the refcounted allocations.

---
  drivers/gpu/drm/panel/panel-simple.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/panel/panel-simple.c

Quoted 1 line

index

232b03c1a259eb15e423b9d452d28e2ff95c70f8..d7530c3533af34f83ce8c6d6067e7f293f2d4bf1 100644

drivers/gpu/drm/panel/panel-simple.c static int panel_simple_probe(struct device *dev,

Quoted 3 lines
drivers/gpu/drm/panel/panel-simple.c +0 -0
@@ -579,7 +579,7 @@ static int panel_simple_probe(struct device *dev,

const struct panel_desc *desc)

drivers/gpu/drm/panel/panel-simple.c static int panel_simple_probe(struct device *dev, const struct panel_desc *desc)

Quoted 5 lines
drivers/gpu/drm/panel/panel-simple.c +1 -1
      u32 bus_flags;
      int err;
 
-     panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
+     panel = devm_drm_panel_alloc(dev, struct panel_simple, base,

&panel_simple_funcs);

drivers/gpu/drm/panel/panel-simple.c static int panel_simple_probe(struct device *dev,

Quoted 4 lines
drivers/gpu/drm/panel/panel-simple.c +0 -0
      if (!panel)
              return -ENOMEM;
 
@@ -694,8 +694,6 @@ static int panel_simple_probe(struct device *dev,

const struct panel_desc *desc)

Quoted 3 lines
drivers/gpu/drm/panel/panel-simple.c +0 -0
      pm_runtime_set_autosuspend_delay(dev, 1000);
      pm_runtime_use_autosuspend(dev);
 
Quoted 7 lines

drivers/gpu/drm/panel/panel-simple.c static int panel_simple_probe(struct device *dev, · line 697

Quoted 1 line
drivers/gpu/drm/panel/panel-simple.c +0 -1
-     drm_panel_init(&panel->base, dev, &panel_simple_funcs,

connector_type);

As pointed by the helpers review, you're loosing the connector_type info here.

Yes Niels, Looking into this. The conector type has to be passed from here

to the API...

Anusha

Quoted 10 lines

drivers/gpu/drm/panel/panel-simple.c static int panel_simple_probe(struct device *dev, const struct panel_desc *desc) · line 699

Quoted 5 lines
drivers/gpu/drm/panel/panel-simple.c +0 -1
-
      err = drm_panel_of_backlight(&panel->base);
      if (err) {
              dev_err_probe(dev, err, "Could not find backlight\n");

Thanks !
Neil