From nobody Wed Oct 1 22:33:19 2025 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 15E8523506F for ; Fri, 26 Sep 2025 16:35:08 +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=1758904511; cv=none; b=A37gICfKwubiqqUU12zrk4Zdx279GDk5pQbzPY8L9MMUi20pz/eI6s3bUx27g7ApQgV3nJdVr1vKvQQ+H5PiMnNNteM1i8Zky16gKg62FpGE+TD3jpsYWybPXQPurX0ZezJ4VXFrqc6HewxXXkp1bMxZ07EqyEbT85K+iPOMRW4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758904511; c=relaxed/simple; bh=Tjtf+qkmGfwEJxwpsS39gFlyPr9z1XY1oZupy2jZouE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=g6DXrOdKBH+iRqxPNT5vyupT6S8arJGqCl2osTbL5gf7btOQrBmPZquNXUcfwVwMFEAiXD6+eL89HNx5Ow6JQGNzggMp0feRv1UtnOWMWZJ46n7++5OaYjWaAYDSZqtgTctjLDb9Fl9KPFYfZBzoDLY7aAL3dhqVtWwDM7T0pYU= 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=UQc9EmEw; 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="UQc9EmEw" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id 686704E40E11; Fri, 26 Sep 2025 16:35:07 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 3EBD9606B5; Fri, 26 Sep 2025 16:35:07 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 40313102F18CE; Fri, 26 Sep 2025 18:35:04 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1758904506; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=6xVw/khtwiX9YEqRoJlZJqW1RWke6AJC8b2cUTe6EF4=; b=UQc9EmEwq8CeuvRpDVvNT+LRASjb0Nqr+P7f9zDqVMuUWvFi4hWxc1WDDzwGItwtZWJNdt TpFuTAAIbE+A5eO4BXnILDMzL5YC2H1dJUM6BEVk39vQ+gegdqm5HWRmCkr1o5X8J7q5+f kdq5fdMjbc/mQzZxLASB8sPjdXb9OhhSAsZ1+xOu54z3urts6qVMnfznsTEko9x9isPI8q 6TKsKDQLmdm77Tzdq4S41ryzJ4KCXKn2SPmR88UXMGCntvX7kateFedm4jNV6ihl1JLh+P tZB2VUFFqKMv2F5VqNWygXcCtwP/ReY+ZqmE37GBiXbmVvaheCAa3ncm+mR1Yg== From: Luca Ceresoli Date: Fri, 26 Sep 2025 18:33:27 +0200 Subject: [PATCH v2 1/2] drm/bridge: add drm_bridge_unplug() and drm_bridge_enter/exit() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250926-drm-bridge-atomic-vs-remove-v2-1-69f7d5ca1a92@bootlin.com> References: <20250926-drm-bridge-atomic-vs-remove-v2-0-69f7d5ca1a92@bootlin.com> In-Reply-To: <20250926-drm-bridge-atomic-vs-remove-v2-0-69f7d5ca1a92@bootlin.com> To: Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter Cc: Hui Pu , Thomas Petazzoni , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Luca Ceresoli , Dmitry Baryshkov X-Mailer: b4 0.14.2 X-Last-TLS-Session-Version: TLSv1.3 To allow DRM bridges to be removable, add synchronization functions allowing to tell when a bridge hardware has been physically unplugged and to mark a critical section that should not be entered after that. This is inspired by the drm_dev_unplugged/enter/exit() functions for struct drm_device. Suggested-by: Maxime Ripard Link: https://lore.kernel.org/all/20250106-vigorous-talented-viper-fa49d9@h= ouat/ Signed-off-by: Luca Ceresoli --- drivers/gpu/drm/drm_bridge.c | 58 ++++++++++++++++++++++++++++++++++++++++= ++++ include/drm/drm_bridge.h | 12 +++++++++ 2 files changed, 70 insertions(+) diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c index d031447eebc955efcf1e018d39c015b62b969eae..3ebf6cc820e058a67f712763c34= 1a75c671c82d1 100644 --- a/drivers/gpu/drm/drm_bridge.c +++ b/drivers/gpu/drm/drm_bridge.c @@ -27,6 +27,7 @@ #include #include #include +#include =20 #include #include @@ -200,6 +201,63 @@ static DEFINE_MUTEX(bridge_lock); static LIST_HEAD(bridge_list); =20 +DEFINE_STATIC_SRCU(drm_bridge_unplug_srcu); + +/** + * drm_bridge_enter - Enter DRM bridge critical section + * @dev: DRM bridge + * @idx: Pointer to index that will be passed to the matching drm_bridge_e= xit() + * + * This function marks and protects the beginning of a section that should= not + * be entered after the bridge has been unplugged. The section end is mark= ed + * with drm_bridge_exit(). Calls to this function can be nested. + * + * Returns: + * True if it is OK to enter the section, false otherwise. + */ +bool drm_bridge_enter(struct drm_bridge *bridge, int *idx) +{ + *idx =3D srcu_read_lock(&drm_bridge_unplug_srcu); + + if (bridge->unplugged) { + srcu_read_unlock(&drm_bridge_unplug_srcu, *idx); + return false; + } + + return true; +} +EXPORT_SYMBOL(drm_bridge_enter); + +/** + * drm_bridge_exit - Exit DRM bridge critical section + * @idx: index returned by drm_bridge_enter() + * + * This function marks the end of a section that should not be entered aft= er + * the bridge has been unplugged. + */ +void drm_bridge_exit(int idx) +{ + srcu_read_unlock(&drm_bridge_unplug_srcu, idx); +} +EXPORT_SYMBOL(drm_bridge_exit); + +/** + * drm_bridge_unplug - unplug a DRM bridge + * @dev: DRM bridge + * + * This tells the bridge has been physically unplugged and no operations on + * device resources must be done anymore. Entry-points can use + * drm_bridge_enter() and drm_bridge_exit() to protect device resources in + * a race free manner. + */ +void drm_bridge_unplug(struct drm_bridge *bridge) +{ + bridge->unplugged =3D true; + + synchronize_srcu(&drm_bridge_unplug_srcu); +} +EXPORT_SYMBOL(drm_bridge_unplug); + static void __drm_bridge_free(struct kref *kref) { struct drm_bridge *bridge =3D container_of(kref, struct drm_bridge, refco= unt); diff --git a/include/drm/drm_bridge.h b/include/drm/drm_bridge.h index 76e05930f50e00f6ef5999b3f5a476215951028d..6b325de9e41ba7ee3649eaa60df= e105d6155f824 100644 --- a/include/drm/drm_bridge.h +++ b/include/drm/drm_bridge.h @@ -1143,6 +1143,14 @@ struct drm_bridge { */ struct kref refcount; =20 + /** + * @unplugged: + * + * Flag to tell if the bridge has been unplugged. + * See drm_bridge_enter() and drm_bridge_unplug(). + */ + bool unplugged; + /** @driver_private: pointer to the bridge driver's internal context */ void *driver_private; /** @ops: bitmask of operations supported by the bridge */ @@ -1278,6 +1286,10 @@ drm_priv_to_bridge(struct drm_private_obj *priv) return container_of(priv, struct drm_bridge, base); } =20 +bool drm_bridge_enter(struct drm_bridge *bridge, int *idx); +void drm_bridge_exit(int idx); +void drm_bridge_unplug(struct drm_bridge *bridge); + struct drm_bridge *drm_bridge_get(struct drm_bridge *bridge); void drm_bridge_put(struct drm_bridge *bridge); =20 --=20 2.51.0 From nobody Wed Oct 1 22:33:19 2025 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (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 5A7FF2D542F for ; Fri, 26 Sep 2025 16:35:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.171.202.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758904513; cv=none; b=TAAvLayb+4L77jDC0f+lXPxop/ZODP1FCtudp0JGtCWgjJIMjoV47HgqBhS2dgXN1uP5It7Su07TlUb48II/mZD+zQXKt9SRBLn9p1EWZa40RoQYUqFN77bkJmoTJvVeL4zFoYF0er3YAqyBM07c4fnhZboDKUf+xIktW4lUs48= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758904513; c=relaxed/simple; bh=NQkO9sERJ5k+kSkZyB8VfhDtTdRQ4XwjrisUwBVDo+U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Tl/K8WV3J1ew0KG5G9coTA0Jk9rkXYEuMXwtE0bEJQ9DRKrlW7HvEfAnGIdxzofoCmSxN5XEvk0fBlUGbLElTL7OAbB0Zq9Uzq6tnExCo6gxL5T4gOudje1r6eQBopeo7dT8ExCci+AhVTYm/rd8aQDMbWAnWohHhJOmhztpjz0= 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=L1atmfeB; arc=none smtp.client-ip=185.171.202.116 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="L1atmfeB" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id 27D28C00D98; Fri, 26 Sep 2025 16:34:52 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 85F1D606B5; Fri, 26 Sep 2025 16:35:09 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id B0B56102F1829; Fri, 26 Sep 2025 18:35:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1758904508; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=ICzSvLvJb7esyCKXOeOL5sJVQ8upayrrtyMxyExJw7M=; b=L1atmfeBiDc6ktznLNxQbLAPCdAs2qwzkmXmYL+F1kKNRUOxRYYki4LKTc32XRi+Y2/zMn eAljRYkPA9LVeWDK3HG7JvDAaLy5jsZ1xnWwRChVy3RRUq88NpFKhysLHXkKMjXtv2yY1h WoPjpNrnuM7Izs7DDepPGLm6Hpcn4T2MsClHITKghBWIRmMMToqCV82k8MFTM8/q+m3Sms 0EmEBpeA+DRggKp/c2WD43BNwigbcXlA0CkYHpjUlh77U2zq2YMo+tHzenq2fXUt/AsqRZ rSpgEkJN9zE4tgtijaUN2WrvbH6gpIyFNPwIl09uF0S+QGEbB/e1qv4hFYYFXA== From: Luca Ceresoli Date: Fri, 26 Sep 2025 18:33:28 +0200 Subject: [PATCH v2 2/2] drm/bridge: ti-sn65dsi83: protect device resources on unplug Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250926-drm-bridge-atomic-vs-remove-v2-2-69f7d5ca1a92@bootlin.com> References: <20250926-drm-bridge-atomic-vs-remove-v2-0-69f7d5ca1a92@bootlin.com> In-Reply-To: <20250926-drm-bridge-atomic-vs-remove-v2-0-69f7d5ca1a92@bootlin.com> To: Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter Cc: Hui Pu , Thomas Petazzoni , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Luca Ceresoli , Dmitry Baryshkov X-Mailer: b4 0.14.2 X-Last-TLS-Session-Version: TLSv1.3 To support hot-unplug of this bridge we need to protect access to device resources in case sn65dsi83_remove() happens concurrently to other code. Some care is needed for the case when the unplug happens before sn65dsi83_atomic_disable() has a chance to enter the critical section (i.e. a successful drm_bridge_enter() call), which occurs whenever the hardware is removed while the display is active. When that happens, sn65dsi83_atomic_disable() in unable to release the resources taken by sn65dsi83_atomic_pre_enable(). To ensure those resources are released exactly once on device removal: * move the code to release them to a dedicated function * register that function when the resources are taken in sn65dsi83_atomic_pre_enable() * if sn65dsi83_atomic_disable() happens before sn65dsi83_remove() (typical non-hot-unplug case): * sn65dsi83_atomic_disable() can enter the critical section (drm_bridge_enter() returns 0) -> it releases and executes the devres action * if sn65dsi83_atomic_disable() happens after sn65dsi83_remove() (typical hot-unplug case): * sn65dsi83_remove() -> drm_bridge_unplug() prevents sn65dsi83_atomic_disable() from entering the critical section (drm_bridge_enter() returns nonzero), so sn65dsi83_atomic_disable() cannot release and execute the devres action * the devres action is executed at the end of sn65dsi83_remove() Signed-off-by: Luca Ceresoli --- Changed in v2: - Use a devres action instead of a flag --- drivers/gpu/drm/bridge/ti-sn65dsi83.c | 85 ++++++++++++++++++++++++++++---= ---- 1 file changed, 68 insertions(+), 17 deletions(-) diff --git a/drivers/gpu/drm/bridge/ti-sn65dsi83.c b/drivers/gpu/drm/bridge= /ti-sn65dsi83.c index 033c44326552ab167d4e8d9b74957c585e4c6fb7..92014366449237668b50cc884a6= 7f52fcd5777c3 100644 --- a/drivers/gpu/drm/bridge/ti-sn65dsi83.c +++ b/drivers/gpu/drm/bridge/ti-sn65dsi83.c @@ -406,6 +406,10 @@ static void sn65dsi83_reset_work(struct work_struct *w= s) { struct sn65dsi83 *ctx =3D container_of(ws, struct sn65dsi83, reset_work); int ret; + int idx; + + if (!drm_bridge_enter(&ctx->bridge, &idx)) + return; =20 /* Reset the pipe */ ret =3D sn65dsi83_reset_pipe(ctx); @@ -415,12 +419,18 @@ static void sn65dsi83_reset_work(struct work_struct *= ws) } if (ctx->irq) enable_irq(ctx->irq); + + drm_bridge_exit(idx); } =20 static void sn65dsi83_handle_errors(struct sn65dsi83 *ctx) { unsigned int irq_stat; int ret; + int idx; + + if (!drm_bridge_enter(&ctx->bridge, &idx)) + return; =20 /* * Schedule a reset in case of: @@ -441,6 +451,8 @@ static void sn65dsi83_handle_errors(struct sn65dsi83 *c= tx) =20 schedule_work(&ctx->reset_work); } + + drm_bridge_exit(idx); } =20 static void sn65dsi83_monitor_work(struct work_struct *work) @@ -463,6 +475,40 @@ static void sn65dsi83_monitor_stop(struct sn65dsi83 *c= tx) cancel_delayed_work_sync(&ctx->monitor_work); } =20 +/* + * Release resources taken by sn65dsi83_atomic_pre_enable(). + * + * Normally invoked by sn65dsi83_atomic_disable(). But if + * sn65dsi83_remove() is invoked while enabled, then drm_bridge_unplug() + * would defuse sn65dsi83_atomic_disable(). Thus a devres action is used to + * ensure exactly one code path (removal or atomic disable) invokes this + * code. + */ +static void sn65dsi83_release_resources(void *data) +{ + struct sn65dsi83 *ctx =3D (struct sn65dsi83 *)data; + int ret; + + if (ctx->irq) { + /* Disable irq */ + regmap_write(ctx->regmap, REG_IRQ_EN, 0x0); + regmap_write(ctx->regmap, REG_IRQ_GLOBAL, 0x0); + } else { + /* Stop the polling task */ + sn65dsi83_monitor_stop(ctx); + } + + /* Put the chip in reset, pull EN line low, and assure 10ms reset low tim= ing. */ + gpiod_set_value_cansleep(ctx->enable_gpio, 0); + usleep_range(10000, 11000); + + ret =3D regulator_disable(ctx->vcc); + if (ret) + dev_err(ctx->dev, "Failed to disable vcc: %d\n", ret); + + regcache_mark_dirty(ctx->regmap); +} + static void sn65dsi83_atomic_pre_enable(struct drm_bridge *bridge, struct drm_atomic_state *state) { @@ -478,10 +524,15 @@ static void sn65dsi83_atomic_pre_enable(struct drm_br= idge *bridge, __le16 le16val; u16 val; int ret; + int idx; + + if (!drm_bridge_enter(bridge, &idx)) + return; =20 ret =3D regulator_enable(ctx->vcc); if (ret) { dev_err(ctx->dev, "Failed to enable vcc: %d\n", ret); + drm_bridge_exit(idx); return; } =20 @@ -625,6 +676,8 @@ static void sn65dsi83_atomic_pre_enable(struct drm_brid= ge *bridge, dev_err(ctx->dev, "failed to lock PLL, ret=3D%i\n", ret); /* On failure, disable PLL again and exit. */ regmap_write(ctx->regmap, REG_RC_PLL_EN, 0x00); + devm_add_action(ctx->dev, sn65dsi83_release_resources, ctx); + drm_bridge_exit(idx); return; } =20 @@ -633,6 +686,9 @@ static void sn65dsi83_atomic_pre_enable(struct drm_brid= ge *bridge, =20 /* Wait for 10ms after soft reset as specified in datasheet */ usleep_range(10000, 12000); + + devm_add_action(ctx->dev, sn65dsi83_release_resources, ctx); + drm_bridge_exit(idx); } =20 static void sn65dsi83_atomic_enable(struct drm_bridge *bridge, @@ -640,6 +696,10 @@ static void sn65dsi83_atomic_enable(struct drm_bridge = *bridge, { struct sn65dsi83 *ctx =3D bridge_to_sn65dsi83(bridge); unsigned int pval; + int idx; + + if (!drm_bridge_enter(bridge, &idx)) + return; =20 /* Clear all errors that got asserted during initialization. */ regmap_read(ctx->regmap, REG_IRQ_STAT, &pval); @@ -659,32 +719,22 @@ static void sn65dsi83_atomic_enable(struct drm_bridge= *bridge, /* Use the polling task */ sn65dsi83_monitor_start(ctx); } + + drm_bridge_exit(idx); } =20 static void sn65dsi83_atomic_disable(struct drm_bridge *bridge, struct drm_atomic_state *state) { struct sn65dsi83 *ctx =3D bridge_to_sn65dsi83(bridge); - int ret; - - if (ctx->irq) { - /* Disable irq */ - regmap_write(ctx->regmap, REG_IRQ_EN, 0x0); - regmap_write(ctx->regmap, REG_IRQ_GLOBAL, 0x0); - } else { - /* Stop the polling task */ - sn65dsi83_monitor_stop(ctx); - } + int idx; =20 - /* Put the chip in reset, pull EN line low, and assure 10ms reset low tim= ing. */ - gpiod_set_value_cansleep(ctx->enable_gpio, 0); - usleep_range(10000, 11000); + if (!drm_bridge_enter(bridge, &idx)) + return; =20 - ret =3D regulator_disable(ctx->vcc); - if (ret) - dev_err(ctx->dev, "Failed to disable vcc: %d\n", ret); + devm_release_action(ctx->dev, sn65dsi83_release_resources, ctx); =20 - regcache_mark_dirty(ctx->regmap); + drm_bridge_exit(idx); } =20 static enum drm_mode_status @@ -1005,6 +1055,7 @@ static void sn65dsi83_remove(struct i2c_client *clien= t) { struct sn65dsi83 *ctx =3D i2c_get_clientdata(client); =20 + drm_bridge_unplug(&ctx->bridge); drm_bridge_remove(&ctx->bridge); } =20 --=20 2.51.0