mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] SCMI Power Control fixes
@ 2026-11-16  8:42 Cristian Marussi
  2026-11-16  8:42 ` [PATCH 1/2] firmware: arm_scmi: Fix deadlock between reboot notifier and forceful work Cristian Marussi
  2026-11-16  8:42 ` [PATCH 2/2] firmware: arm_scmi: Fix SCMI Power Control driver removal Cristian Marussi
  0 siblings, 2 replies; 7+ messages in thread
From: Cristian Marussi @ 2026-11-16  8:42 UTC (permalink / raw)
  To: linux-kernel, linux-arm-kernel, arm-scmi
  Cc: sudeep.holla, philip.radford, Cristian Marussi

Hi,

Patch 1/2 is a fix to a possible deadlock already posted on the list which
had still some residual issues as flagged by Sashiko: I addressed such
issues (hopefully) maintaining the original authorship and reposted within
this series to minimize conflicts.

Patch 2/2 is a rework aiming at fixing some unbind-time issues flagged by
Sashiko.

Based on v7.4-rc2.

Thanks,
Cristian

Chengyu Liu (1):
  firmware: arm_scmi: Fix deadlock between reboot notifier and forceful
    work

Cristian Marussi (1):
  firmware: arm_scmi: Fix SCMI Power Control driver removal

 .../firmware/arm_scmi/scmi_power_control.c    | 102 +++++++++++++++---
 1 file changed, 89 insertions(+), 13 deletions(-)

-- 
2.54.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] firmware: arm_scmi: Fix deadlock between reboot notifier and forceful work
  2026-11-16  8:42 [PATCH 0/2] SCMI Power Control fixes Cristian Marussi
@ 2026-11-16  8:42 ` Cristian Marussi
  2026-11-16 18:25   ` Sudeep Holla
  2026-11-16  8:42 ` [PATCH 2/2] firmware: arm_scmi: Fix SCMI Power Control driver removal Cristian Marussi
  1 sibling, 1 reply; 7+ messages in thread
From: Cristian Marussi @ 2026-11-16  8:42 UTC (permalink / raw)
  To: linux-kernel, linux-arm-kernel, arm-scmi
  Cc: sudeep.holla, philip.radford, Chengyu Liu, stable, Cristian Marussi

From: Chengyu Liu <lui2012qwq@gmail.com>

scmi_reboot_notifier() calls cancel_delayed_work_sync() while holding
sc->state_mtx, but the work it waits on - scmi_forceful_work_func() -
takes the same mutex.  If a forceful transition work is in flight when
the reboot notifier fires, the notifier holds the lock and waits for
the callback while the callback waits for the lock: deadlock, and the
reboot wedges.

Moving the cancel outside the lock is not sufficient on its own, as the
callback also calls unregister_reboot_notifier() unconditionally, which
blocks on the reboot chain rwsem (down_write vs. the in-progress
chain's down_read) while the notifier waits for the callback - the
deadlock would just move to a different lock.  Fix both sides:

- Notifier: compute the decision under state_mtx, drop the lock, then
  cancel.  A callback that slips in between sees state ==
  SCMI_SYSPOWER_REBOOTING (not IN_PROGRESS), skips the forceful
  request and finishes, so the cancel cannot miss it.  (The callback's
  own system_state early-return does not cover a work item that
  started before the reboot began: kernel_restart_prepare() runs the
  notifier chain before setting system_state.)

- Callback: only unregister the reboot notifier when actually forcing
  the transition - that is the only case that needs it, because the
  forceful kernel_restart()/kernel_power_off() would otherwise
  re-enter scmi_reboot_notifier and self-deadlock on state_mtx.  When
  the state is not IN_PROGRESS there is nothing to force, and skipping
  the unregister keeps the callback from blocking on the reboot chain
  rwsem while the notifier waits for it.

Found by static analysis.

Cc: stable@vger.kernel.org
Fixes: 2c4b97fee94a ("firmware: arm_scmi: Add SCMI System Power Control driver")
Signed-off-by: Chengyu Liu <lui2012qwq@gmail.com>
[Cristian: drop lock before triggering forceful reboot (needs_forceful)]
Tested-by: Cristian Marussi <cristian.marussi@arm.com>
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
 .../firmware/arm_scmi/scmi_power_control.c    | 26 ++++++++++++++-----
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/drivers/firmware/arm_scmi/scmi_power_control.c b/drivers/firmware/arm_scmi/scmi_power_control.c
index e791a206768c..e0680e33180e 100644
--- a/drivers/firmware/arm_scmi/scmi_power_control.c
+++ b/drivers/firmware/arm_scmi/scmi_power_control.c
@@ -121,6 +121,7 @@ static int scmi_reboot_notifier(struct notifier_block *nb,
 				unsigned long reason, void *__unused)
 {
 	struct scmi_syspower_conf *sc = reboot_nb_to_sconf(nb);
+	bool cancel_work;
 
 	mutex_lock(&sc->state_mtx);
 	switch (reason) {
@@ -138,11 +139,18 @@ static int scmi_reboot_notifier(struct notifier_block *nb,
 		break;
 	}
 
-	if (sc->state == SCMI_SYSPOWER_REBOOTING) {
+	cancel_work = sc->state == SCMI_SYSPOWER_REBOOTING;
+	mutex_unlock(&sc->state_mtx);
+
+	if (cancel_work) {
 		dev_dbg(sc->dev, "Reboot in progress...cancel delayed work.\n");
+		/*
+		 * cancel_delayed_work_sync() waits for a running
+		 * scmi_forceful_work_func(), which takes state_mtx - it must
+		 * not be called with the lock held.
+		 */
 		cancel_delayed_work_sync(&sc->forceful_work);
 	}
-	mutex_unlock(&sc->state_mtx);
 
 	return NOTIFY_OK;
 }
@@ -181,6 +189,7 @@ static void scmi_forceful_work_func(struct work_struct *work)
 {
 	struct scmi_syspower_conf *sc;
 	struct delayed_work *dwork;
+	bool needs_forceful;
 
 	if (system_state > SYSTEM_RUNNING)
 		return;
@@ -188,13 +197,16 @@ static void scmi_forceful_work_func(struct work_struct *work)
 	dwork = to_delayed_work(work);
 	sc = dwork_to_sconf(dwork);
 
-	dev_dbg(sc->dev, "Graceful request timed out...forcing !\n");
 	mutex_lock(&sc->state_mtx);
-	/* avoid deadlock by unregistering reboot notifier first */
-	unregister_reboot_notifier(&sc->reboot_nb);
-	if (sc->state == SCMI_SYSPOWER_IN_PROGRESS)
-		scmi_request_forceful_transition(sc);
+	needs_forceful = sc->state == SCMI_SYSPOWER_IN_PROGRESS;
 	mutex_unlock(&sc->state_mtx);
+
+	if (needs_forceful) {
+		dev_dbg(sc->dev, "Graceful request timed out...forcing !\n");
+		/* avoid deadlock by unregistering reboot notifier first */
+		unregister_reboot_notifier(&sc->reboot_nb);
+		scmi_request_forceful_transition(sc);
+	}
 }
 
 /**
-- 
2.54.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/2] firmware: arm_scmi: Fix SCMI Power Control driver removal
  2026-11-16  8:42 [PATCH 0/2] SCMI Power Control fixes Cristian Marussi
  2026-11-16  8:42 ` [PATCH 1/2] firmware: arm_scmi: Fix deadlock between reboot notifier and forceful work Cristian Marussi
@ 2026-11-16  8:42 ` Cristian Marussi
  2026-11-16 18:14   ` Sudeep Holla
  1 sibling, 1 reply; 7+ messages in thread
From: Cristian Marussi @ 2026-11-16  8:42 UTC (permalink / raw)
  To: linux-kernel, linux-arm-kernel, arm-scmi
  Cc: sudeep.holla, philip.radford, Cristian Marussi, Sashiko

Once a reboot is initiated following a graceful shutdown request received
from the SCMI platform server there are a couple of registered notifiers
and possibly a scheduled delayed work.

Take care to properly stop, unregister and cleanup all such resources
when the driver is unbound.

Fixes: 2c4b97fee94a ("firmware: arm_scmi: Add SCMI System Power Control driver")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-dbe4eba7-1520-40a1-92e8-8892b6e47c4b
Tested-by: Cristian Marussi <cristian.marussi@arm.com>
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
 .../firmware/arm_scmi/scmi_power_control.c    | 76 +++++++++++++++++--
 1 file changed, 70 insertions(+), 6 deletions(-)

diff --git a/drivers/firmware/arm_scmi/scmi_power_control.c b/drivers/firmware/arm_scmi/scmi_power_control.c
index e0680e33180e..b0a10fd92fa6 100644
--- a/drivers/firmware/arm_scmi/scmi_power_control.c
+++ b/drivers/firmware/arm_scmi/scmi_power_control.c
@@ -43,6 +43,7 @@
  * possibly emitted by the platform will be ignored.
  */
 
+#include <linux/cleanup.h>
 #include <linux/math.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -51,6 +52,7 @@
 #include <linux/reboot.h>
 #include <linux/scmi_protocol.h>
 #include <linux/slab.h>
+#include <linux/srcu.h>
 #include <linux/suspend.h>
 #include <linux/time64.h>
 #include <linux/timer.h>
@@ -75,6 +77,8 @@ enum scmi_syspower_state {
  * @state_mtx: @state related mutex
  * @required_transition: The requested transition as decribed in the received
  *			 SCMI SystemPower notification
+ * @dead_srcu: Am SRCU to protect dead flag
+ * @dead: A boolean flag to be set during driver removal
  * @userspace_nb: The notifier_block registered against the SCMI SystemPower
  *		  notification to start the needed userspace interactions.
  * @reboot_nb: A notifier_block optionally used to track reboot progress
@@ -89,6 +93,10 @@ struct scmi_syspower_conf {
 	struct mutex state_mtx;
 	enum scmi_system_events required_transition;
 
+	/* Protect dead */
+	struct srcu_struct dead_srcu;
+	bool dead;
+
 	struct notifier_block userspace_nb;
 	struct notifier_block reboot_nb;
 
@@ -123,6 +131,10 @@ static int scmi_reboot_notifier(struct notifier_block *nb,
 	struct scmi_syspower_conf *sc = reboot_nb_to_sconf(nb);
 	bool cancel_work;
 
+	guard(srcu)(&sc->dead_srcu);
+	if (READ_ONCE(sc->dead))
+		return NOTIFY_DONE;
+
 	mutex_lock(&sc->state_mtx);
 	switch (reason) {
 	case SYS_HALT:
@@ -197,6 +209,10 @@ static void scmi_forceful_work_func(struct work_struct *work)
 	dwork = to_delayed_work(work);
 	sc = dwork_to_sconf(dwork);
 
+	guard(srcu)(&sc->dead_srcu);
+	if (READ_ONCE(sc->dead))
+		return;
+
 	mutex_lock(&sc->state_mtx);
 	needs_forceful = sc->state == SCMI_SYSPOWER_IN_PROGRESS;
 	mutex_unlock(&sc->state_mtx);
@@ -236,8 +252,6 @@ static void scmi_request_graceful_transition(struct scmi_syspower_conf *sc,
 		if (!ret) {
 			/* Wait only up to 75% of the advertised timeout */
 			adj_timeout_ms = mult_frac(timeout_ms, 3, 4);
-			INIT_DELAYED_WORK(&sc->forceful_work,
-					  scmi_forceful_work_func);
 			schedule_delayed_work(&sc->forceful_work,
 					      msecs_to_jiffies(adj_timeout_ms));
 		} else {
@@ -296,6 +310,10 @@ static int scmi_userspace_notifier(struct notifier_block *nb,
 	struct scmi_system_power_state_notifier_report *er = data;
 	struct scmi_syspower_conf *sc = userspace_nb_to_sconf(nb);
 
+	guard(srcu)(&sc->dead_srcu);
+	if (READ_ONCE(sc->dead))
+		return NOTIFY_DONE;
+
 	if (er->system_state >= SCMI_SYSTEM_MAX ||
 	    er->system_state == SCMI_SYSTEM_POWERUP) {
 		dev_err(sc->dev, "Ignoring unsupported system_state: 0x%X\n",
@@ -357,6 +375,10 @@ static int scmi_syspower_probe(struct scmi_device *sdev)
 	if (!sc)
 		return -ENOMEM;
 
+	ret = init_srcu_struct(&sc->dead_srcu);
+	if (ret)
+		return ret;
+
 	sc->state = SCMI_SYSPOWER_IDLE;
 	mutex_init(&sc->state_mtx);
 	sc->required_transition = SCMI_SYSTEM_MAX;
@@ -364,12 +386,53 @@ static int scmi_syspower_probe(struct scmi_device *sdev)
 	sc->dev = &sdev->dev;
 	dev_set_drvdata(&sdev->dev, sc);
 
+	INIT_DELAYED_WORK(&sc->forceful_work, scmi_forceful_work_func);
 	INIT_WORK(&sc->suspend_work, scmi_suspend_work_func);
 
-	return handle->notify_ops->devm_event_notifier_register(sdev,
-							   SCMI_PROTOCOL_SYSTEM,
-					 SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
-						       NULL, &sc->userspace_nb);
+	ret = handle->notify_ops->event_notifier_register(handle, SCMI_PROTOCOL_SYSTEM,
+							  SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
+							  NULL, &sc->userspace_nb);
+	if (ret) {
+		cleanup_srcu_struct(&sc->dead_srcu);
+		return ret;
+	}
+
+	return 0;
+}
+
+static void scmi_syspower_remove(struct scmi_device *sdev)
+{
+	struct scmi_handle *handle = sdev->handle;
+	struct scmi_syspower_conf *sc;
+	int ret;
+
+	sc = dev_get_drvdata(&sdev->dev);
+	if (!sc)
+		return;
+
+	/* Wait for any notifier to complete and inhibit further processing */
+	WRITE_ONCE(sc->dead, true);
+	synchronize_srcu(&sc->dead_srcu);
+
+	/*
+	 * Once here any possible in-flight userspace or reboot notifier
+	 * will have completed and further notifiers will bailout immediately.
+	 * Cancel any work which was possibly already scheduled.
+	 */
+	cancel_delayed_work_sync(&sc->forceful_work);
+
+	/* Unregister reboot notifier...if any */
+	unregister_reboot_notifier(&sc->reboot_nb);
+
+	/* Unregister userspace notifier */
+	ret = handle->notify_ops->event_notifier_unregister(handle,
+							    SCMI_PROTOCOL_SYSTEM,
+							    SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
+							    NULL, &sc->userspace_nb);
+	if (ret)
+		dev_warn(sc->dev, "Failed to unregister notifier\n");
+
+	cleanup_srcu_struct(&sc->dead_srcu);
 }
 
 static int scmi_system_power_resume(struct device *dev)
@@ -396,6 +459,7 @@ static struct scmi_driver scmi_system_power_driver = {
 	},
 	.name = "scmi-system-power",
 	.probe = scmi_syspower_probe,
+	.remove = scmi_syspower_remove,
 	.id_table = scmi_id_table,
 };
 module_scmi_driver(scmi_system_power_driver);
-- 
2.54.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] firmware: arm_scmi: Fix SCMI Power Control driver removal
  2026-11-16  8:42 ` [PATCH 2/2] firmware: arm_scmi: Fix SCMI Power Control driver removal Cristian Marussi
@ 2026-11-16 18:14   ` Sudeep Holla
  2026-11-17 11:35     ` Cristian Marussi
  0 siblings, 1 reply; 7+ messages in thread
From: Sudeep Holla @ 2026-11-16 18:14 UTC (permalink / raw)
  To: Cristian Marussi
  Cc: linux-kernel, linux-arm-kernel, arm-scmi, philip.radford,
	Sudeep Holla, Sashiko

On Mon, Nov 16, 2026 at 08:42:11AM +0000, Cristian Marussi wrote:
> Once a reboot is initiated following a graceful shutdown request received
> from the SCMI platform server there are a couple of registered notifiers
> and possibly a scheduled delayed work.
> 
> Take care to properly stop, unregister and cleanup all such resources
> when the driver is unbound.
> 
> Fixes: 2c4b97fee94a ("firmware: arm_scmi: Add SCMI System Power Control driver")
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/bug/linux-dbe4eba7-1520-40a1-92e8-8892b6e47c4b
> Tested-by: Cristian Marussi <cristian.marussi@arm.com>
> Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
> ---
>  .../firmware/arm_scmi/scmi_power_control.c    | 76 +++++++++++++++++--
>  1 file changed, 70 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/firmware/arm_scmi/scmi_power_control.c b/drivers/firmware/arm_scmi/scmi_power_control.c
> index e0680e33180e..b0a10fd92fa6 100644
> --- a/drivers/firmware/arm_scmi/scmi_power_control.c
> +++ b/drivers/firmware/arm_scmi/scmi_power_control.c

[...]

> @@ -296,6 +310,10 @@ static int scmi_userspace_notifier(struct notifier_block *nb,
>  	struct scmi_system_power_state_notifier_report *er = data;
>  	struct scmi_syspower_conf *sc = userspace_nb_to_sconf(nb);
>  
> +	guard(srcu)(&sc->dead_srcu);
> +	if (READ_ONCE(sc->dead))
> +		return NOTIFY_DONE;
> +
>  	if (er->system_state >= SCMI_SYSTEM_MAX ||
>  	    er->system_state == SCMI_SYSTEM_POWERUP) {
>  		dev_err(sc->dev, "Ignoring unsupported system_state: 0x%X\n",
> @@ -357,6 +375,10 @@ static int scmi_syspower_probe(struct scmi_device *sdev)
>  	if (!sc)
>  		return -ENOMEM;
>  
> +	ret = init_srcu_struct(&sc->dead_srcu);
> +	if (ret)
> +		return ret;
> +
>  	sc->state = SCMI_SYSPOWER_IDLE;
>  	mutex_init(&sc->state_mtx);
>  	sc->required_transition = SCMI_SYSTEM_MAX;
> @@ -364,12 +386,53 @@ static int scmi_syspower_probe(struct scmi_device *sdev)
>  	sc->dev = &sdev->dev;
>  	dev_set_drvdata(&sdev->dev, sc);
>  
> +	INIT_DELAYED_WORK(&sc->forceful_work, scmi_forceful_work_func);
>  	INIT_WORK(&sc->suspend_work, scmi_suspend_work_func);
>  
> -	return handle->notify_ops->devm_event_notifier_register(sdev,
> -							   SCMI_PROTOCOL_SYSTEM,
> -					 SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
> -						       NULL, &sc->userspace_nb);
> +	ret = handle->notify_ops->event_notifier_register(handle, SCMI_PROTOCOL_SYSTEM,
> +							  SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
> +							  NULL, &sc->userspace_nb);
> +	if (ret) {
> +		cleanup_srcu_struct(&sc->dead_srcu);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static void scmi_syspower_remove(struct scmi_device *sdev)
> +{
> +	struct scmi_handle *handle = sdev->handle;
> +	struct scmi_syspower_conf *sc;
> +	int ret;
> +
> +	sc = dev_get_drvdata(&sdev->dev);
> +	if (!sc)
> +		return;
> +
> +	/* Wait for any notifier to complete and inhibit further processing */
> +	WRITE_ONCE(sc->dead, true);
> +	synchronize_srcu(&sc->dead_srcu);
> +
> +	/*
> +	 * Once here any possible in-flight userspace or reboot notifier
> +	 * will have completed and further notifiers will bailout immediately.
> +	 * Cancel any work which was possibly already scheduled.
> +	 */
> +	cancel_delayed_work_sync(&sc->forceful_work);
> +

To keep it symmetric with probe, do we need cancelling of suspend_work as
well here ?

> +
> +	/* Unregister reboot notifier...if any */
> +	unregister_reboot_notifier(&sc->reboot_nb);
> +
> +	/* Unregister userspace notifier */
> +	ret = handle->notify_ops->event_notifier_unregister(handle,
> +							    SCMI_PROTOCOL_SYSTEM,
> +							    SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
> +							    NULL, &sc->userspace_nb);

Just wondering if it is better to stop receiving notifications from firmware
first and then cancel any pending work ? Basically reverse order of probe
for remove.

-- 
Regards,
Sudeep

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] firmware: arm_scmi: Fix deadlock between reboot notifier and forceful work
  2026-11-16  8:42 ` [PATCH 1/2] firmware: arm_scmi: Fix deadlock between reboot notifier and forceful work Cristian Marussi
@ 2026-11-16 18:25   ` Sudeep Holla
  2026-11-17 11:25     ` Cristian Marussi
  0 siblings, 1 reply; 7+ messages in thread
From: Sudeep Holla @ 2026-11-16 18:25 UTC (permalink / raw)
  To: Cristian Marussi
  Cc: linux-kernel, linux-arm-kernel, arm-scmi, philip.radford,
	Sudeep Holla, Chengyu Liu, stable

On Mon, Nov 16, 2026 at 08:42:10AM +0000, Cristian Marussi wrote:
> From: Chengyu Liu <lui2012qwq@gmail.com>
> 
> scmi_reboot_notifier() calls cancel_delayed_work_sync() while holding
> sc->state_mtx, but the work it waits on - scmi_forceful_work_func() -
> takes the same mutex.  If a forceful transition work is in flight when
> the reboot notifier fires, the notifier holds the lock and waits for
> the callback while the callback waits for the lock: deadlock, and the
> reboot wedges.
> 
> Moving the cancel outside the lock is not sufficient on its own, as the
> callback also calls unregister_reboot_notifier() unconditionally, which
> blocks on the reboot chain rwsem (down_write vs. the in-progress
> chain's down_read) while the notifier waits for the callback - the
> deadlock would just move to a different lock.  Fix both sides:
> 
> - Notifier: compute the decision under state_mtx, drop the lock, then
>   cancel.  A callback that slips in between sees state ==
>   SCMI_SYSPOWER_REBOOTING (not IN_PROGRESS), skips the forceful
>   request and finishes, so the cancel cannot miss it.  (The callback's
>   own system_state early-return does not cover a work item that
>   started before the reboot began: kernel_restart_prepare() runs the
>   notifier chain before setting system_state.)
> 
> - Callback: only unregister the reboot notifier when actually forcing
>   the transition - that is the only case that needs it, because the
>   forceful kernel_restart()/kernel_power_off() would otherwise
>   re-enter scmi_reboot_notifier and self-deadlock on state_mtx.  When
>   the state is not IN_PROGRESS there is nothing to force, and skipping
>   the unregister keeps the callback from blocking on the reboot chain
>   rwsem while the notifier waits for it.
> 
> Found by static analysis.
> 
> Cc: stable@vger.kernel.org
> Fixes: 2c4b97fee94a ("firmware: arm_scmi: Add SCMI System Power Control driver")
> Signed-off-by: Chengyu Liu <lui2012qwq@gmail.com>
> [Cristian: drop lock before triggering forceful reboot (needs_forceful)]
> Tested-by: Cristian Marussi <cristian.marussi@arm.com>
> Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
> ---
>  .../firmware/arm_scmi/scmi_power_control.c    | 26 ++++++++++++++-----
>  1 file changed, 19 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/firmware/arm_scmi/scmi_power_control.c b/drivers/firmware/arm_scmi/scmi_power_control.c
> index e791a206768c..e0680e33180e 100644
> --- a/drivers/firmware/arm_scmi/scmi_power_control.c
> +++ b/drivers/firmware/arm_scmi/scmi_power_control.c
> @@ -121,6 +121,7 @@ static int scmi_reboot_notifier(struct notifier_block *nb,
>  				unsigned long reason, void *__unused)
>  {
>  	struct scmi_syspower_conf *sc = reboot_nb_to_sconf(nb);
> +	bool cancel_work;
>  
>  	mutex_lock(&sc->state_mtx);
>  	switch (reason) {
> @@ -138,11 +139,18 @@ static int scmi_reboot_notifier(struct notifier_block *nb,
>  		break;
>  	}
>  
> -	if (sc->state == SCMI_SYSPOWER_REBOOTING) {
> +	cancel_work = sc->state == SCMI_SYSPOWER_REBOOTING;
> +	mutex_unlock(&sc->state_mtx);
> +
> +	if (cancel_work) {
>  		dev_dbg(sc->dev, "Reboot in progress...cancel delayed work.\n");
> +		/*
> +		 * cancel_delayed_work_sync() waits for a running
> +		 * scmi_forceful_work_func(), which takes state_mtx - it must
> +		 * not be called with the lock held.
> +		 */
>  		cancel_delayed_work_sync(&sc->forceful_work);

Sashiko still complains about the deadlock.

How about changing this to non-sync cancel_delayed_work() so that it doesn't
wait and deadlock like it complains ?

>  	}
> -	mutex_unlock(&sc->state_mtx);
>  
>  	return NOTIFY_OK;
>  }
> @@ -181,6 +189,7 @@ static void scmi_forceful_work_func(struct work_struct *work)
>  {
>  	struct scmi_syspower_conf *sc;
>  	struct delayed_work *dwork;
> +	bool needs_forceful;
>  
>  	if (system_state > SYSTEM_RUNNING)
>  		return;
> @@ -188,13 +197,16 @@ static void scmi_forceful_work_func(struct work_struct *work)
>  	dwork = to_delayed_work(work);
>  	sc = dwork_to_sconf(dwork);
>  
> -	dev_dbg(sc->dev, "Graceful request timed out...forcing !\n");
>  	mutex_lock(&sc->state_mtx);
> -	/* avoid deadlock by unregistering reboot notifier first */
> -	unregister_reboot_notifier(&sc->reboot_nb);
> -	if (sc->state == SCMI_SYSPOWER_IN_PROGRESS)
> -		scmi_request_forceful_transition(sc);
> +	needs_forceful = sc->state == SCMI_SYSPOWER_IN_PROGRESS;
>  	mutex_unlock(&sc->state_mtx);
> +
> +	if (needs_forceful) {
> +		dev_dbg(sc->dev, "Graceful request timed out...forcing !\n");
> +		/* avoid deadlock by unregistering reboot notifier first */
> +		unregister_reboot_notifier(&sc->reboot_nb);

And re-evaluate needs_forceful again here after un-registration before
calling below forced transition ?

-- 
Regards,
Sudeep

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] firmware: arm_scmi: Fix deadlock between reboot notifier and forceful work
  2026-11-16 18:25   ` Sudeep Holla
@ 2026-11-17 11:25     ` Cristian Marussi
  0 siblings, 0 replies; 7+ messages in thread
From: Cristian Marussi @ 2026-11-17 11:25 UTC (permalink / raw)
  To: Sudeep Holla
  Cc: Cristian Marussi, linux-kernel, linux-arm-kernel, arm-scmi,
	philip.radford, Chengyu Liu, stable

On Mon, Nov 16, 2026 at 06:25:52PM +0000, Sudeep Holla wrote:
> On Mon, Nov 16, 2026 at 08:42:10AM +0000, Cristian Marussi wrote:
> > From: Chengyu Liu <lui2012qwq@gmail.com>

Hi Sudeep,

> > 
> > scmi_reboot_notifier() calls cancel_delayed_work_sync() while holding
> > sc->state_mtx, but the work it waits on - scmi_forceful_work_func() -
> > takes the same mutex.  If a forceful transition work is in flight when
> > the reboot notifier fires, the notifier holds the lock and waits for
> > the callback while the callback waits for the lock: deadlock, and the
> > reboot wedges.
> > 
> > Moving the cancel outside the lock is not sufficient on its own, as the
> > callback also calls unregister_reboot_notifier() unconditionally, which
> > blocks on the reboot chain rwsem (down_write vs. the in-progress
> > chain's down_read) while the notifier waits for the callback - the
> > deadlock would just move to a different lock.  Fix both sides:
> > 
> > - Notifier: compute the decision under state_mtx, drop the lock, then
> >   cancel.  A callback that slips in between sees state ==
> >   SCMI_SYSPOWER_REBOOTING (not IN_PROGRESS), skips the forceful
> >   request and finishes, so the cancel cannot miss it.  (The callback's
> >   own system_state early-return does not cover a work item that
> >   started before the reboot began: kernel_restart_prepare() runs the
> >   notifier chain before setting system_state.)
> > 
> > - Callback: only unregister the reboot notifier when actually forcing
> >   the transition - that is the only case that needs it, because the
> >   forceful kernel_restart()/kernel_power_off() would otherwise
> >   re-enter scmi_reboot_notifier and self-deadlock on state_mtx.  When
> >   the state is not IN_PROGRESS there is nothing to force, and skipping
> >   the unregister keeps the callback from blocking on the reboot chain
> >   rwsem while the notifier waits for it.
> > 
> > Found by static analysis.
> > 
> > Cc: stable@vger.kernel.org
> > Fixes: 2c4b97fee94a ("firmware: arm_scmi: Add SCMI System Power Control driver")
> > Signed-off-by: Chengyu Liu <lui2012qwq@gmail.com>
> > [Cristian: drop lock before triggering forceful reboot (needs_forceful)]
> > Tested-by: Cristian Marussi <cristian.marussi@arm.com>
> > Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
> > ---
> >  .../firmware/arm_scmi/scmi_power_control.c    | 26 ++++++++++++++-----
> >  1 file changed, 19 insertions(+), 7 deletions(-)
> > 
> > diff --git a/drivers/firmware/arm_scmi/scmi_power_control.c b/drivers/firmware/arm_scmi/scmi_power_control.c
> > index e791a206768c..e0680e33180e 100644
> > --- a/drivers/firmware/arm_scmi/scmi_power_control.c
> > +++ b/drivers/firmware/arm_scmi/scmi_power_control.c
> > @@ -121,6 +121,7 @@ static int scmi_reboot_notifier(struct notifier_block *nb,
> >  				unsigned long reason, void *__unused)
> >  {
> >  	struct scmi_syspower_conf *sc = reboot_nb_to_sconf(nb);
> > +	bool cancel_work;
> >  
> >  	mutex_lock(&sc->state_mtx);
> >  	switch (reason) {
> > @@ -138,11 +139,18 @@ static int scmi_reboot_notifier(struct notifier_block *nb,
> >  		break;
> >  	}
> >  
> > -	if (sc->state == SCMI_SYSPOWER_REBOOTING) {
> > +	cancel_work = sc->state == SCMI_SYSPOWER_REBOOTING;
> > +	mutex_unlock(&sc->state_mtx);
> > +
> > +	if (cancel_work) {
> >  		dev_dbg(sc->dev, "Reboot in progress...cancel delayed work.\n");
> > +		/*
> > +		 * cancel_delayed_work_sync() waits for a running
> > +		 * scmi_forceful_work_func(), which takes state_mtx - it must
> > +		 * not be called with the lock held.
> > +		 */
> >  		cancel_delayed_work_sync(&sc->forceful_work);
> 
> Sashiko still complains about the deadlock.
> 

Yes I know.

> How about changing this to non-sync cancel_delayed_work() so that it doesn't
> wait and deadlock like it complains ?
> 

Indeed I think this is just the solution...I mean dropping this patch as
a whole and keeping the original wider locking section BUT do not cancel 
synchronously, but simply with cancel_delayed_work(&sc->forceful_work);

> >  	}
> > -	mutex_unlock(&sc->state_mtx);
> >  
> >  	return NOTIFY_OK;
> >  }
> > @@ -181,6 +189,7 @@ static void scmi_forceful_work_func(struct work_struct *work)
> >  {
> >  	struct scmi_syspower_conf *sc;
> >  	struct delayed_work *dwork;
> > +	bool needs_forceful;
> >  
> >  	if (system_state > SYSTEM_RUNNING)
> >  		return;
> > @@ -188,13 +197,16 @@ static void scmi_forceful_work_func(struct work_struct *work)
> >  	dwork = to_delayed_work(work);
> >  	sc = dwork_to_sconf(dwork);
> >  
> > -	dev_dbg(sc->dev, "Graceful request timed out...forcing !\n");
> >  	mutex_lock(&sc->state_mtx);
> > -	/* avoid deadlock by unregistering reboot notifier first */
> > -	unregister_reboot_notifier(&sc->reboot_nb);
> > -	if (sc->state == SCMI_SYSPOWER_IN_PROGRESS)
> > -		scmi_request_forceful_transition(sc);
> > +	needs_forceful = sc->state == SCMI_SYSPOWER_IN_PROGRESS;
> >  	mutex_unlock(&sc->state_mtx);
> > +
> > +	if (needs_forceful) {
> > +		dev_dbg(sc->dev, "Graceful request timed out...forcing !\n");
> > +		/* avoid deadlock by unregistering reboot notifier first */
> > +		unregister_reboot_notifier(&sc->reboot_nb);
> 
> And re-evaluate needs_forceful again here after un-registration before
> calling below forced transition ?
>

If all of this is locked as it was originally there is no need, the two
threads should just sync properly...

Let's see what Sashiko thinks..

Thanks,
Cristian

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] firmware: arm_scmi: Fix SCMI Power Control driver removal
  2026-11-16 18:14   ` Sudeep Holla
@ 2026-11-17 11:35     ` Cristian Marussi
  0 siblings, 0 replies; 7+ messages in thread
From: Cristian Marussi @ 2026-11-17 11:35 UTC (permalink / raw)
  To: Sudeep Holla
  Cc: Cristian Marussi, linux-kernel, linux-arm-kernel, arm-scmi,
	philip.radford, Sashiko

On Mon, Nov 16, 2026 at 06:14:50PM +0000, Sudeep Holla wrote:
> On Mon, Nov 16, 2026 at 08:42:11AM +0000, Cristian Marussi wrote:
> > Once a reboot is initiated following a graceful shutdown request received
> > from the SCMI platform server there are a couple of registered notifiers
> > and possibly a scheduled delayed work.
> > 

Hi,

> > Take care to properly stop, unregister and cleanup all such resources
> > when the driver is unbound.
> > 
> > Fixes: 2c4b97fee94a ("firmware: arm_scmi: Add SCMI System Power Control driver")
> > Reported-by: Sashiko <sashiko-bot@kernel.org>
> > Closes: https://sashiko.dev/#/bug/linux-dbe4eba7-1520-40a1-92e8-8892b6e47c4b
> > Tested-by: Cristian Marussi <cristian.marussi@arm.com>
> > Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
> > ---
> >  .../firmware/arm_scmi/scmi_power_control.c    | 76 +++++++++++++++++--
> >  1 file changed, 70 insertions(+), 6 deletions(-)
> > 
> > diff --git a/drivers/firmware/arm_scmi/scmi_power_control.c b/drivers/firmware/arm_scmi/scmi_power_control.c
> > index e0680e33180e..b0a10fd92fa6 100644
> > --- a/drivers/firmware/arm_scmi/scmi_power_control.c
> > +++ b/drivers/firmware/arm_scmi/scmi_power_control.c
> 
> [...]
> 
> > @@ -296,6 +310,10 @@ static int scmi_userspace_notifier(struct notifier_block *nb,
> >  	struct scmi_system_power_state_notifier_report *er = data;
> >  	struct scmi_syspower_conf *sc = userspace_nb_to_sconf(nb);
> >  
> > +	guard(srcu)(&sc->dead_srcu);
> > +	if (READ_ONCE(sc->dead))
> > +		return NOTIFY_DONE;
> > +
> >  	if (er->system_state >= SCMI_SYSTEM_MAX ||
> >  	    er->system_state == SCMI_SYSTEM_POWERUP) {
> >  		dev_err(sc->dev, "Ignoring unsupported system_state: 0x%X\n",
> > @@ -357,6 +375,10 @@ static int scmi_syspower_probe(struct scmi_device *sdev)
> >  	if (!sc)
> >  		return -ENOMEM;
> >  
> > +	ret = init_srcu_struct(&sc->dead_srcu);
> > +	if (ret)
> > +		return ret;
> > +
> >  	sc->state = SCMI_SYSPOWER_IDLE;
> >  	mutex_init(&sc->state_mtx);
> >  	sc->required_transition = SCMI_SYSTEM_MAX;
> > @@ -364,12 +386,53 @@ static int scmi_syspower_probe(struct scmi_device *sdev)
> >  	sc->dev = &sdev->dev;
> >  	dev_set_drvdata(&sdev->dev, sc);
> >  
> > +	INIT_DELAYED_WORK(&sc->forceful_work, scmi_forceful_work_func);
> >  	INIT_WORK(&sc->suspend_work, scmi_suspend_work_func);
> >  
> > -	return handle->notify_ops->devm_event_notifier_register(sdev,
> > -							   SCMI_PROTOCOL_SYSTEM,
> > -					 SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
> > -						       NULL, &sc->userspace_nb);
> > +	ret = handle->notify_ops->event_notifier_register(handle, SCMI_PROTOCOL_SYSTEM,
> > +							  SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
> > +							  NULL, &sc->userspace_nb);
> > +	if (ret) {
> > +		cleanup_srcu_struct(&sc->dead_srcu);
> > +		return ret;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static void scmi_syspower_remove(struct scmi_device *sdev)
> > +{
> > +	struct scmi_handle *handle = sdev->handle;
> > +	struct scmi_syspower_conf *sc;
> > +	int ret;
> > +
> > +	sc = dev_get_drvdata(&sdev->dev);
> > +	if (!sc)
> > +		return;
> > +
> > +	/* Wait for any notifier to complete and inhibit further processing */
> > +	WRITE_ONCE(sc->dead, true);
> > +	synchronize_srcu(&sc->dead_srcu);
> > +
> > +	/*
> > +	 * Once here any possible in-flight userspace or reboot notifier
> > +	 * will have completed and further notifiers will bailout immediately.
> > +	 * Cancel any work which was possibly already scheduled.
> > +	 */
> > +	cancel_delayed_work_sync(&sc->forceful_work);
> > +
> 
> To keep it symmetric with probe, do we need cancelling of suspend_work as
> well here ?
> 

Yes, I was going to do that in V1, then I decided it was not
needed...without any good reason really O_o...
Sashiko pointed out the same...I'm adding in V2.

> > +
> > +	/* Unregister reboot notifier...if any */
> > +	unregister_reboot_notifier(&sc->reboot_nb);
> > +
> > +	/* Unregister userspace notifier */
> > +	ret = handle->notify_ops->event_notifier_unregister(handle,
> > +							    SCMI_PROTOCOL_SYSTEM,
> > +							    SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
> > +							    NULL, &sc->userspace_nb);
> 
> Just wondering if it is better to stop receiving notifications from firmware
> first and then cancel any pending work ? Basically reverse order of probe
> for remove.
> 

I think the order is irrelevant given the dead_srcu gating, BUT indeed I
kept this order so as to keep the undo in reverse order: in fact the
probe at first issues some INIT_WORK, but the real schedule_work happens
only later after a notif is received, so the order is:

 - init_srcu
 - static WORK init
 - register SCMI notif callback
 - RX notify msg
 - register reboot notifier
 - schedule delayed work

Also, even if the notifiers are inhibited by dead_srcu, I felt better to
cancel that work as soon as possible.

Thoughts ?

Thanks,
Cristian

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-17 11:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16  8:42 [PATCH 0/2] SCMI Power Control fixes Cristian Marussi
2026-11-16  8:42 ` [PATCH 1/2] firmware: arm_scmi: Fix deadlock between reboot notifier and forceful work Cristian Marussi
2026-11-16 18:25   ` Sudeep Holla
2026-11-17 11:25     ` Cristian Marussi
2026-11-16  8:42 ` [PATCH 2/2] firmware: arm_scmi: Fix SCMI Power Control driver removal Cristian Marussi
2026-11-16 18:14   ` Sudeep Holla
2026-11-17 11:35     ` Cristian Marussi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®