Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Maxime Ripard <mripard@kernel.org>
To: Luca Ceresoli <luca.ceresoli@bootlin.com>
Cc: "Alexandre Belloni" <alexandre.belloni@bootlin.com>,
	"Hervé Codina" <herve.codina@bootlin.com>,
	"Thomas Petazzoni" <thomas.petazzoni@bootlin.com>,
	linux-doc@vger.kernel.org,
	"Catalin Marinas" <catalin.marinas@arm.com>,
	"Paul Kocialkowski" <contact@paulk.fr>,
	dri-devel@lists.freedesktop.org,
	"Claudiu Beznea" <claudiu.beznea@tuxon.dev>,
	"Laurent Pinchart" <Laurent.pinchart@ideasonboard.com>,
	"Andrzej Hajda" <andrzej.hajda@intel.com>,
	"David Airlie" <airlied@gmail.com>,
	"Fabio Estevam" <festevam@gmail.com>,
	"Marek Szyprowski" <m.szyprowski@samsung.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Robert Foss" <rfoss@kernel.org>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Sam Ravnborg" <sam@ravnborg.org>,
	"Jernej Skrabec" <jernej.skrabec@gmail.com>,
	"Daniel Thompson" <danielt@kernel.org>,
	"Jagan Teki" <jagan@amarulasolutions.com>,
	"Jessica Zhang" <quic_jesszhan@quicinc.com>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"Will Deacon" <will@kernel.org>,
	"Jonas Karlman" <jonas@kwiboo.se>,
	"Sascha Hauer" <s.hauer@pengutronix.de>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Inki Dae" <inki.dae@samsung.com>,
	linux-arm-kernel@lists.infradead.org,
	"Neil Armstrong" <neil.armstrong@linaro.org>,
	"Boris Brezillon" <bbrezillon@kernel.org>,
	linux-kernel@vger.kernel.org,
	"Paul Kocialkowski" <paul.kocialkowski@bootlin.com>,
	"Pengutronix Kernel Team" <kernel@pengutronix.de>,
	"Dmitry Baryshkov" <dmitry.baryshkov@linaro.org>,
	"Shawn Guo" <shawnguo@kernel.org>
Subject: Re: [PATCH v6 14/26] drm/bridge: add support for refcounted DRM bridges
Date: Fri, 7 Feb 2025 12:47:51 +0100	[thread overview]
Message-ID: <20250207-ingenious-daffodil-dugong-51be57@houat> (raw)
In-Reply-To: <20250206-hotplug-drm-bridge-v6-14-9d6f2c9c3058@bootlin.com>

[-- Attachment #1: Type: text/plain, Size: 8746 bytes --]

Hi,

On Thu, Feb 06, 2025 at 07:14:29PM +0100, Luca Ceresoli wrote:
> DRM bridges are currently considered as a fixed element of a DRM card, and
> thus their lifetime is assumed to extend for as long as the card
> exists. New use cases, such as hot-pluggable hardware with video bridges,
> require DRM bridges to be added and removed to a DRM card without tearing
> the card down. This is possible for connectors already (used by DP MST), so
> add this possibility to DRM bridges as well.
> 
> Implementation is based on drm_connector_init() as far as it makes sense,
> and differs when it doesn't. A difference is that bridges are not exposed
> to userspace, hence struct drm_bridge does not embed a struct
> drm_mode_object which would provide the refcount. Instead we add to struct
> drm_bridge a refcount field (we don't need other struct drm_mode_object
> fields here) and instead of using the drm_mode_object_*() functions we
> reimplement from those functions the few lines that drm_bridge needs for
> refcounting.
> 
> Also add a new devm_drm_bridge_alloc() macro to allocate a new refcounted
> bridge.
> 
> Signed-off-by: Luca Ceresoli <luca.ceresoli@bootlin.com>

So, a couple of general comments:

- I've said it a couple of times already, but I really think you're
  making it harder than necessary for you here. This (and only this!)
  should be the very first series you should be pushing. The rest can
  only ever work if that work goes through, and it's already hard enough
  as it is. So, split that patch into a series of its own, get that
  merged, and then we will be able to deal with panels conversion and
  whatever. That's even more true with panels since there's ongoing work
  that will make it easier for you too. So the best thing here is
  probably to wait.

- This patch really needs to be split into several patches, something
  along the lines of:

  + Creating devm_drm_bridge_alloc()
  + Adding refcounting
  + Taking the references in all the needed places
  + Converting a bunch of drivers

> Changes in v6:
>  - use drm_warn, not WARN_ON (Jani Nikula)
>  - Add devm_drm_bridge_alloc() to replace drm_bridge_init() (similar to
>    drmm_encoder_alloc)
>  - Remove .destroy func: deallocation is done via the struct offset
>    computed by the devm_drm_bridge_alloc() macro
>  - use fixed free callback, as the same callback is used in all cases
>    anyway (remove free_cb, add bool is_refcounted)
>  - add drm_bridge_get/put() to drm_bridge_attach/detach() (add the bridge
>    to a list)
>  - make some DRM_DEBUG() strings more informative
> 
> This patch was added in v5.
> ---
>  drivers/gpu/drm/drm_bridge.c |  76 ++++++++++++++++++++++++++--
>  include/drm/drm_bridge.h     | 117 +++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 189 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c
> index 1955a231378050abf1071d74a145831b425c47c5..f694b32ca59cb91c32846bc00b43360df41cc1ad 100644
> --- a/drivers/gpu/drm/drm_bridge.c
> +++ b/drivers/gpu/drm/drm_bridge.c
> @@ -200,6 +200,57 @@
>  DEFINE_MUTEX(bridge_lock);
>  LIST_HEAD(bridge_list);
>  
> +/* Internal function (for refcounted bridges) */
> +void __drm_bridge_free(struct kref *kref)
> +{
> +	struct drm_bridge *bridge = container_of(kref, struct drm_bridge, refcount);
> +	void *container = ((void *)bridge) - bridge->container_offset;
> +
> +	DRM_DEBUG("bridge=%p, container=%p FREE\n", bridge, container);

Pointers are not really useful to track here, since they are obfuscated
most of the time. Using the bridge device name would probably be better
(or removing the SHOUTING DEBUG entirely :))

> +	kfree(container);
> +}
> +EXPORT_SYMBOL(__drm_bridge_free);
> +
> +static void drm_bridge_put_void(void *data)
> +{
> +	struct drm_bridge *bridge = (struct drm_bridge *)data;
> +
> +	drm_bridge_put(bridge);
> +}
> +
> +void *__devm_drm_bridge_alloc(struct device *dev, size_t size, size_t offset,
> +			      const struct drm_bridge_funcs *funcs)
> +{
> +	void *container;
> +	struct drm_bridge *bridge;
> +	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);
> +
> +	bridge = container + offset;
> +	bridge->container_offset = offset;
> +	bridge->funcs = funcs;
> +	kref_init(&bridge->refcount);
> +	bridge->is_refcounted = 1;
> +
> +	err = devm_add_action_or_reset(dev, drm_bridge_put_void, bridge);
> +	if (err)
> +		return ERR_PTR(err);
> +
> +	DRM_DEBUG("bridge=%p, container=%p, funcs=%ps ALLOC\n", bridge, container, funcs);
> +
> +	return container;
> +}
> +EXPORT_SYMBOL(__devm_drm_bridge_alloc);
> +
>  /**
>   * drm_bridge_add - add the given bridge to the global bridge list
>   *
> @@ -209,6 +260,10 @@ void drm_bridge_add(struct drm_bridge *bridge)
>  {
>  	struct drm_bridge *br, *tmp;
>  
> +	DRM_DEBUG("bridge=%p ADD\n", bridge);
> +
> +	drm_bridge_get(bridge);
> +
>  	mutex_init(&bridge->hpd_mutex);
>  
>  	if (bridge->ops & DRM_BRIDGE_OP_HDMI)
> @@ -257,6 +312,8 @@ void drm_bridge_remove(struct drm_bridge *bridge)
>  {
>  	struct drm_bridge *br, *tmp;
>  
> +	DRM_DEBUG("bridge=%p REMOVE\n", bridge);
> +
>  	mutex_lock(&bridge_lock);
>  	list_del_init(&bridge->list);
>  	mutex_unlock(&bridge_lock);
> @@ -266,6 +323,8 @@ void drm_bridge_remove(struct drm_bridge *bridge)
>  			br->funcs->bridge_event_notify(br, DRM_EVENT_BRIDGE_REMOVE, bridge);
>  
>  	mutex_destroy(&bridge->hpd_mutex);
> +
> +	drm_bridge_put(bridge);
>  }
>  EXPORT_SYMBOL(drm_bridge_remove);
>  
> @@ -326,11 +385,17 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
>  	if (!encoder || !bridge)
>  		return -EINVAL;
>  
> -	if (previous && (!previous->dev || previous->encoder != encoder))
> -		return -EINVAL;
> +	drm_bridge_get(bridge);
>  
> -	if (bridge->dev)
> -		return -EBUSY;
> +	if (previous && (!previous->dev || previous->encoder != encoder)) {
> +		ret = -EINVAL;
> +		goto err_put_bridge;
> +	}
> +
> +	if (bridge->dev) {
> +		ret = -EBUSY;
> +		goto err_put_bridge;
> +	}
>  
>  	bridge->dev = encoder->dev;
>  	bridge->encoder = encoder;
> @@ -379,6 +444,8 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
>  			      "failed to attach bridge %pOF to encoder %s\n",
>  			      bridge->of_node, encoder->name);
>  
> +err_put_bridge:
> +	drm_bridge_put(bridge);
>  	return ret;
>  }
>  EXPORT_SYMBOL(drm_bridge_attach);
> @@ -399,6 +466,7 @@ void drm_bridge_detach(struct drm_bridge *bridge)
>  
>  	list_del(&bridge->chain_node);
>  	bridge->dev = NULL;
> +	drm_bridge_put(bridge);
>  }
>  
>  /**
> diff --git a/include/drm/drm_bridge.h b/include/drm/drm_bridge.h
> index ad7ba444a13e5ecf16f996de3742e4ac67dc21f1..43cef0f6ccd36034f64ad2babfebea62db1d9e43 100644
> --- a/include/drm/drm_bridge.h
> +++ b/include/drm/drm_bridge.h
> @@ -31,6 +31,7 @@
>  #include <drm/drm_encoder.h>
>  #include <drm/drm_mode_object.h>
>  #include <drm/drm_modes.h>
> +#include <drm/drm_print.h>
>  
>  struct device_node;
>  
> @@ -863,6 +864,22 @@ struct drm_bridge {
>  	const struct drm_bridge_timings *timings;
>  	/** @funcs: control functions */
>  	const struct drm_bridge_funcs *funcs;
> +
> +	/**
> +	 * @container_offset: Offset of this struct within the container
> +	 * struct embedding it. Used for refcounted bridges to free the
> +	 * embeddeing struct when the refcount drops to zero. Unused on
> +	 * legacy bridges.
> +	 */
> +	size_t container_offset;

This shouldn't be in there. You can create an intermediate structure and
store both pointers for the action to consume.

> +	/**
> +	 * @refcount: reference count for bridges with dynamic lifetime
> +	 * (see drm_bridge_init)
> +	 */
> +	struct kref refcount;
> +	/** @is_refcounted: this bridge has dynamic lifetime management */
> +	bool is_refcounted;
> +

I'm not sure we want to treat both paths separately too. It'll require
to update most of/all the drivers, but it's not too hard with
coccinelle:

virtual patch

@@
identifier f;
identifier b, c, d;
expression bf;
type T;
@@

 f(...)
 {
	...
-	T *c;
+	T *c;
	...
-	c = devm_kzalloc(d, ...);
+	c = devm_drm_bridge_alloc(d, T, b, bf);
	...
-	c->b.funcs = bf;
	...
	drm_bridge_add(&c->b);
	...
 }

We need to add a bit more variations (like kzalloc vs devm_kzalloc,
drm_bridge_add vs devm_drm_bridge_add, etc.), but it should be a good
first approximation

Maxime

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 273 bytes --]

  reply	other threads:[~2025-02-07 11:49 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-06 18:14 [PATCH v6 00/26] Add support for hot-pluggable DRM bridges Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 01/26] drm/debugfs: fix printk format for bridge index Luca Ceresoli
2025-02-07  2:06   ` Dmitry Baryshkov
2025-02-06 18:14 ` [PATCH v6 02/26] drm: of: drm_of_find_panel_or_bridge: move misplaced comment Luca Ceresoli
2025-02-07  2:20   ` Dmitry Baryshkov
2025-02-06 18:14 ` [PATCH v6 03/26] drm/bridge: panel: use drm_bridge_is_panel() instead of open code Luca Ceresoli
2025-02-07  2:21   ` Dmitry Baryshkov
2025-02-06 18:14 ` [PATCH v6 04/26] drm/bridge: panel: drm_panel_bridge_remove: warn when called on non-panel bridge Luca Ceresoli
2025-02-07  2:22   ` Dmitry Baryshkov
2025-02-07  8:59     ` Luca Ceresoli
2025-02-07  7:25   ` Maxime Ripard
2025-02-06 18:14 ` [PATCH v6 05/26] drm/debugfs: add top-level 'bridges' file showing all added bridges Luca Ceresoli
2025-02-07  2:41   ` Dmitry Baryshkov
2025-02-07  8:54     ` Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 06/26] drm/panel: move all code into bridge/panel.c Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 07/26] drm/bridge: panel: forbid initializing a panel with unknown connector type Luca Ceresoli
2025-02-07  2:44   ` Dmitry Baryshkov
2025-02-06 18:14 ` [PATCH v6 08/26] drm/bridge: panel: add a panel_bridge to every panel Luca Ceresoli
2025-02-07  2:49   ` Dmitry Baryshkov
2025-02-07  8:54     ` Luca Ceresoli
2025-02-07 19:43       ` Dmitry Baryshkov
2025-02-10 17:11         ` Luca Ceresoli
2025-02-10 18:34   ` Maxime Ripard
2025-02-18  9:43     ` Chen-Yu Tsai
2025-02-18 10:17       ` Maxime Ripard
2025-02-06 18:14 ` [PATCH v6 09/26] drm/bridge: move devm_drm_of_get_bridge and drmm_of_get_bridge to drm_bridge.c Luca Ceresoli
2025-02-07  2:52   ` Dmitry Baryshkov
2025-02-07  8:54     ` Luca Ceresoli
2025-02-07 19:47       ` Dmitry Baryshkov
2025-02-06 18:14 ` [PATCH v6 10/26] drm/bridge: add devm_drm_of_get_bridge_by_node() Luca Ceresoli
2025-02-07  2:53   ` Dmitry Baryshkov
2025-02-07  8:54     ` Luca Ceresoli
2025-02-10 18:22   ` Maxime Ripard
2025-02-06 18:14 ` [PATCH v6 11/26] drm/bridge: samsung-dsim: use devm_drm_of_get_bridge[_by_node] to find the out_bridge Luca Ceresoli
2025-02-07  2:55   ` Dmitry Baryshkov
2025-02-07  8:54     ` Luca Ceresoli
2025-02-10 18:23   ` Maxime Ripard
2025-02-06 18:14 ` [PATCH v6 12/26] drm/bridge: allow bridges to be informed about added and removed bridges Luca Ceresoli
2025-02-07  3:01   ` Dmitry Baryshkov
2025-02-06 18:14 ` [PATCH v6 13/26] drm/encoder: add drm_encoder_cleanup_from() Luca Ceresoli
2025-02-07  3:03   ` Dmitry Baryshkov
2025-02-07  8:53     ` Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 14/26] drm/bridge: add support for refcounted DRM bridges Luca Ceresoli
2025-02-07 11:47   ` Maxime Ripard [this message]
2025-02-07 19:54     ` Dmitry Baryshkov
2025-02-10 12:31       ` Maxime Ripard
2025-02-10 14:23         ` Dmitry Baryshkov
2025-02-10 17:12           ` Luca Ceresoli
2025-02-10 18:16             ` Maxime Ripard
2025-02-10 18:17           ` Maxime Ripard
2025-02-10 17:12       ` Luca Ceresoli
2025-02-10 23:14         ` Dmitry Baryshkov
2025-02-11  8:48           ` Maxime Ripard
2025-02-12  0:55             ` Dmitry Baryshkov
2025-02-12 10:08               ` Maxime Ripard
2025-02-10 17:12     ` Luca Ceresoli
2025-02-11 13:10       ` Maxime Ripard
2025-02-26 14:28         ` Luca Ceresoli
2025-02-27  9:32           ` Maxime Ripard
2025-02-27 11:31             ` Luca Ceresoli
2025-02-27 14:39               ` Maxime Ripard
2025-03-13 11:56     ` Luca Ceresoli
2025-03-13 18:07       ` Maxime Ripard
2025-03-14  8:11         ` Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 15/26] drm/bridge: devm_drm_of_get_bridge and drmm_of_get_bridge: automatically put the bridge Luca Ceresoli
2025-02-07  3:17   ` Dmitry Baryshkov
2025-02-07 10:44     ` Luca Ceresoli
2025-02-07 19:57       ` Dmitry Baryshkov
2025-02-10 18:00         ` Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 16/26] drm/bridge: increment refcount in of_drm_find_bridge() Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 17/26] drm/bridge: add devm_drm_put_bridge() and devm_drm_put_and_clear_bridge() Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 18/26] drm/bridge: add documentation of refcounted bridges Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 19/26] drm/tests: bridge: add KUnit tests for DRM bridges (init and destroy) Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 20/26] drm/debugfs: bridges_show: show refcount Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 21/26] drm/bridge: add list of removed refcounted bridges Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 22/26] drm/debugfs: show removed bridges Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 23/26] drm/bridge: samsung-dsim: use refcounting for the out_bridge Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 24/26] drm/bridge: panel: use dynamic lifetime management Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 25/26] drm/bridge: ti-sn65dsi83: " Luca Ceresoli
2025-02-06 18:14 ` [PATCH v6 26/26] drm/bridge: hotplug-bridge: add driver to support hot-pluggable DSI bridges Luca Ceresoli
2025-02-07  9:01 ` [PATCH v6 00/26] Add support for hot-pluggable DRM bridges Luca Ceresoli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250207-ingenious-daffodil-dugong-51be57@houat \
    --to=mripard@kernel.org \
    --cc=Laurent.pinchart@ideasonboard.com \
    --cc=airlied@gmail.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=andrzej.hajda@intel.com \
    --cc=bbrezillon@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=claudiu.beznea@tuxon.dev \
    --cc=contact@paulk.fr \
    --cc=corbet@lwn.net \
    --cc=danielt@kernel.org \
    --cc=dmitry.baryshkov@linaro.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=festevam@gmail.com \
    --cc=herve.codina@bootlin.com \
    --cc=inki.dae@samsung.com \
    --cc=jagan@amarulasolutions.com \
    --cc=jernej.skrabec@gmail.com \
    --cc=jonas@kwiboo.se \
    --cc=kernel@pengutronix.de \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luca.ceresoli@bootlin.com \
    --cc=m.szyprowski@samsung.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=neil.armstrong@linaro.org \
    --cc=paul.kocialkowski@bootlin.com \
    --cc=quic_jesszhan@quicinc.com \
    --cc=rfoss@kernel.org \
    --cc=s.hauer@pengutronix.de \
    --cc=sam@ravnborg.org \
    --cc=shawnguo@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=tzimmermann@suse.de \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox