From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtpout-03.galae.net (smtpout-03.galae.net [185.246.85.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D60E28A3EF; Wed, 19 Nov 2025 15:05:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.85.4 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763564726; cv=none; b=op8vEKSbHoOGLU3UDYkM7+tWsYM4gmdCaCaEohTC/YAzafprarSykI0Pj4QRhF7tsJkm/dZsNQXttrIAuMq/ZpAIdC527iBKsy1FXKwM/7LEzOOBhYvPPb/Jbqzrxh3ET4WG7/sJ/+2R5my7IAgCbvFq5kiW1uziMQV6U795eTw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763564726; c=relaxed/simple; bh=Qqus3GcZo9UeS54mCFvjyr0P2gu9agHXL/0LSp5j8e8=; h=Mime-Version:Content-Type:Date:Message-Id:Subject:Cc:To:From: References:In-Reply-To; b=nEajF2iCgEfUe99RpLe82LA5xeZAIVnVRmrsSlEpZXKnWslkIYayxj8bSI7E1xMrTj5BXbIjWV7WEBVbKU52LtaIFVTMMwzgwfvxfKjfKf1o+Clu2L/ciOEZiJw0chRLyrB5Q433BJDXkq3n1gt7rLDo+zyz4wEQexutHjKFG9w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=sOEuzAxE; arc=none smtp.client-ip=185.246.85.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="sOEuzAxE" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id 915DE4E417A5; Wed, 19 Nov 2025 15:05:22 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 5ACA160699; Wed, 19 Nov 2025 15:05:22 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id A4C1410371A6A; Wed, 19 Nov 2025 16:05:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1763564719; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=F7QYsJvko3OwXBuoQqrMWaiZs2Tw0yizTy6p7UD4rac=; b=sOEuzAxEZWuQ3AUbFtEgUlf2cgtfZWFBSyylVKX8jZsilf0pjAXIS/1dUfJVSv11XNTJuI C0XUe2nv5ky9Mn/3MfiFXv1n/0JLHDWaG0itUKmX2QaboBI96GBbNFKrjGYu+8SCdV0DBT LKR1vwDMaGbq7Lkp1S3fONcv4bUe4hEcHgUnX1la5CRLY0y1raK/rkHodxNqKDleqAdoSi l3OyZqJGWvRsUtgJI97b41UaOy9g8IytQezx8tiRjlP6MmIfs6wVpTYa4Exv8EJrrMgsKh R21w3WrZaNqF/CfQUtZMsjnN4+WrwU+pd84dsLKf7gEcDQXt1HEx0nqD76YBcA== Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Wed, 19 Nov 2025 16:05:05 +0100 Message-Id: Subject: Re: [PATCH 06/26] drm/bridge: add devm_drm_of_find_bridge Cc: "Hui Pu" , "Thomas Petazzoni" , , , , , , , , , To: "Louis Chauvet" , "Andrzej Hajda" , "Neil Armstrong" , "Robert Foss" , "Laurent Pinchart" , "Jonas Karlman" , "Jernej Skrabec" , "Maarten Lankhorst" , "Maxime Ripard" , "Thomas Zimmermann" , "David Airlie" , "Simona Vetter" , "Jonathan Corbet" , "Alexey Brodkin" , "Phong LE" , "Liu Ying" , "Shawn Guo" , "Sascha Hauer" , "Pengutronix Kernel Team" , "Fabio Estevam" , "Adrien Grassein" , "Laurent Pinchart" , "Tomi Valkeinen" , "Kieran Bingham" , "Geert Uytterhoeven" , "Magnus Damm" , "Kevin Hilman" , "Jerome Brunet" , "Martin Blumenstingl" , "Chun-Kuang Hu" , "Philipp Zabel" , "Matthias Brugger" , "AngeloGioacchino Del Regno" , "Anitha Chrisanthus" , "Edmund Dea" , "Inki Dae" , "Seung-Woo Kim" , "Kyungmin Park" , "Krzysztof Kozlowski" , "Alim Akhtar" From: "Luca Ceresoli" X-Mailer: aerc 0.20.1 References: <20251119-drm-bridge-alloc-getput-drm_of_find_bridge-v1-0-0db98a7fe474@bootlin.com> <20251119-drm-bridge-alloc-getput-drm_of_find_bridge-v1-6-0db98a7fe474@bootlin.com> <0858117f-9397-4045-9b7d-490ad24926cb@bootlin.com> In-Reply-To: <0858117f-9397-4045-9b7d-490ad24926cb@bootlin.com> X-Last-TLS-Session-Version: TLSv1.3 Hi Louis, On Wed Nov 19, 2025 at 3:33 PM CET, Louis Chauvet wrote: > > > On 11/19/25 13:05, Luca Ceresoli wrote: >> Several drivers (about 20) follow the same pattern: >> >> 1. get a pointer to a bridge (typically the next bridge in the chain) = by >> calling of_drm_find_bridge() >> 2. store the returned pointer in the private driver data, keep it unti= l >> driver .remove >> 3. dereference the pointer at attach time and possibly at other times >> >> of_drm_find_bridge() is now deprecated because it does not increment the >> refcount and should be replaced with drm_of_find_bridge() + >> drm_bridge_put(). >> >> However some of those drivers have a complex code flow and adding a >> drm_bridge_put() call in all the appropriate locations is error-prone, >> leads to ugly and more complex code, and can lead to errors over time wi= th >> code flow changes. >> >> To handle all those drivers in a straightforward way, add a devm variant= of >> drm_of_find_bridge() that adds a devm action to invoke drm_bridge_put() >> when the said driver is removed. This allows all those drivers to put th= e >> reference automatically and safely with a one line change: >> >> - priv->next_bridge =3D of_drm_find_bridge(remote_np); >> + priv->next_bridge =3D devm_drm_of_find_bridge(dev, remote_np); >> >> Signed-off-by: Luca Ceresoli >> --- >> drivers/gpu/drm/drm_bridge.c | 30 ++++++++++++++++++++++++++++++ >> include/drm/drm_bridge.h | 5 +++++ >> 2 files changed, 35 insertions(+) >> >> diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c >> index 09ad825f9cb8..c7baafbe5695 100644 >> --- a/drivers/gpu/drm/drm_bridge.c >> +++ b/drivers/gpu/drm/drm_bridge.c >> @@ -1446,6 +1446,36 @@ struct drm_bridge *drm_of_find_bridge(struct devi= ce_node *np) >> } >> EXPORT_SYMBOL(drm_of_find_bridge); >> >> +/** >> + * devm_drm_of_find_bridge - find the bridge corresponding to the devic= e >> + * node in the global bridge list and add a devm >> + * action to put it >> + * >> + * @dev: device requesting the bridge >> + * @np: device node >> + * >> + * On success the returned bridge refcount is incremented, and a devm >> + * action is added to call drm_bridge_put() when @dev is removed. So th= e >> + * caller does not have to put the returned bridge explicitly. >> + * >> + * RETURNS: >> + * drm_bridge control struct on success, NULL on failure > > I am not sure for the "NULL on failure", you return ERR_PTR(err), which > is probably not NULL but an error code. Indeed. Apologies for the mess in this series: it was adapted from an old one using a different approach, so I had to adapt lots of details, and missed a few along the way. :( About the value to return, maybe it's better to use the same semantics as drm_of_find_bridge(), i.e. NULL on error. I don't think a caller would have anything clever to do with an error return value other tan bailing out. And the only error path for devm_add_action_or_reset() is on a small allocation, so it basically cannot happen. >> +struct drm_bridge *devm_drm_of_find_bridge(struct device *dev, struct d= evice_node *np) >> +{ >> + struct drm_bridge *bridge =3D drm_of_find_bridge(np); >> + >> + if (bridge) { >> + int err =3D devm_add_action_or_reset(dev, drm_bridge_put_void, bridge= ); >> + >> + if (err) >> + return ERR_PTR(err); >> + } So this would become: if (bridge) { if (devm_add_action_or_reset(dev, drm_bridge_put_void, bridge)) return NULL; } >> + >> + return bridge; >> +} Luca -- Luca Ceresoli, Bootlin Embedded Linux and Kernel engineering https://bootlin.com