Skip to contentSkip to conversation

SCMI Power Control fixes

PATCH New Cristian Marussi 1 participant 4 messages Nov 30 System control & power/management interface (SCPI/SCMI) Message Protocol drivers

lore
Share & tools
lore

Cristian Marussi

author reviewer
#0 lore raw

v3 cover SCMI Power Control fixes

Hi,

A small series with a few fixes for Sashiko-reports.

Patch 1/2 is a fix to a possible deadlock already posted on the list.

Patch 2/2 is a rework aiming at fixing some unbind-time issues flagged by Sashiko around ABBA deadlocks and missing unregistration of the reboot notifiers.

Patch 3/3 is really a fix for an old bug that has always been there and still not reported: no need to register a reboot notifier if we are not going into shutdown/reboot.

Based on sudeep/for-next/arm_fw/updates that sports the new directory layout drivers/firmware/arm/scmi/

Thanks,
Cristian

Cristian Marussi (3):
firmware: arm_scmi: Avoid deadlocks between reboot notifier and
forceful work
firmware: arm_scmi: Rework SCMI Power Control driver removal
firmware: arm_scmi: Ensure no reboot notifier is registered on suspend

 .../firmware/arm/scmi/scmi_power_control.c    | 108 +++++++++++++++---
 1 file changed, 94 insertions(+), 14 deletions(-)
Signature · 1 line
2.54.0
author reviewer
#1 lore raw

PATCH v3 1/3 firmware: arm_scmi: Avoid deadlocks between reboot notifier and forceful work

When an ongoing reboot is detected scmi_reboot_notifier() cancels the scheduled pending delayed work by calling cancel_delayed_work_sync():
this is done while holding sc->state_mtx which is also taken by the worker is trying to synchronously cancel.

Such locking, though, guarantees mutual exclusion between the conflicting activities carried on by the two threads, but it is in turn leading to an ABBA deadlock with the internal reboot notifier locking.

Stop synchronously waiting for the cancellation so as to avoid deadlock and temporarily drop state_mtx while unregistering the reboot notfier.

Fixes 2c4b97fee94a ("firmware: arm_scmi: Add SCMI System Power Control driver")

---
v2 --> v3
- drop mutex before unregistering reboot notifier
- use dev_warn() to log forceful transititon

---
 .../firmware/arm/scmi/scmi_power_control.c    | 22 ++++++++++++++-----
 1 file changed, 16 insertions(+), 6 deletions(-)
drivers/firmware/arm/scmi/scmi_power_control.c +16 -6
index e791a206768c..0be9ca01e3fe 100644
@@ -140,7 +140,7 @@ static int scmi_reboot_notifier(struct notifier_block *nb,
 
 	if (sc->state == SCMI_SYSPOWER_REBOOTING) {
 		dev_dbg(sc->dev, "Reboot in progress...cancel delayed work.\n");
-		cancel_delayed_work_sync(&sc->forceful_work);
+		cancel_delayed_work(&sc->forceful_work);
 	}
 	mutex_unlock(&sc->state_mtx);
 
@@ -188,12 +188,22 @@ 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);
+	if (sc->state == SCMI_SYSPOWER_IN_PROGRESS) {
+		mutex_unlock(&sc->state_mtx);
+		/*
+		 * Avoid deadlock by unregistering reboot notifier first
+		 * while NOT holding state_mtx.
+		 */
+		unregister_reboot_notifier(&sc->reboot_nb);
+		mutex_lock(&sc->state_mtx);
+		/* Re-evaluate current state after mutex re-acquired */
+		if (sc->state == SCMI_SYSPOWER_IN_PROGRESS) {
+			dev_warn(sc->dev,
+				 "Graceful request timed out...forcing !\n");
+			scmi_request_forceful_transition(sc);
+		}
+	}
 	mutex_unlock(&sc->state_mtx);
 }
 
Signature · 1 line
2.54.0
author reviewer
#2 lore raw

PATCH v3 2/3 firmware: arm_scmi: Rework SCMI Power Control driver removal

In reply to Cristian Marussi

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")

---
v2 --> v3
- remove counter-productive srcu_dead checks in workers
- unregister SCMI notifier right after the srcu_dead draining
- let worker be drained by cancel_work

---
 .../firmware/arm/scmi/scmi_power_control.c    | 76 +++++++++++++++++--
 1 file changed, 70 insertions(+), 6 deletions(-)
drivers/firmware/arm/scmi/scmi_power_control.c +70 -6
index 0be9ca01e3fe..c4db5ed6c53f 100644
@@ -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;
 
@@ -105,6 +113,9 @@ struct scmi_syspower_conf {
 #define dwork_to_sconf(x)		\
 	container_of(x, struct scmi_syspower_conf, forceful_work)
 
+#define work_to_sconf(x)		\
+	container_of(x, struct scmi_syspower_conf, suspend_work)
+
 /**
  * scmi_reboot_notifier  - A reboot notifier to catch an ongoing successful
  * system transition
@@ -122,6 +133,10 @@ static int scmi_reboot_notifier(struct notifier_block *nb,
 {
 	struct scmi_syspower_conf *sc = reboot_nb_to_sconf(nb);
 
+	guard(srcu)(&sc->dead_srcu);
+	if (READ_ONCE(sc->dead))
+		return NOTIFY_DONE;
+
 	mutex_lock(&sc->state_mtx);
 	switch (reason) {
 	case SYS_HALT:
@@ -234,8 +249,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 {
@@ -294,6 +307,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",
@@ -355,6 +372,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;
@@ -362,12 +383,54 @@ 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);
+
+	/* 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");
+
+	/*
+	 * Once here any possible in-flight 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);
+	cancel_work_sync(&sc->suspend_work);
+
+	/* Unregister reboot notifier...if any */
+	unregister_reboot_notifier(&sc->reboot_nb);
+
+	cleanup_srcu_struct(&sc->dead_srcu);
 }
 
 static int scmi_system_power_resume(struct device *dev)
@@ -394,6 +457,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);
Signature · 1 line
2.54.0
author reviewer
#3 lore raw

PATCH v3 3/3 firmware: arm_scmi: Ensure no reboot notifier is registered on suspend

In reply to Cristian Marussi

Register a reboot notifier only when the platform originated notification payload embeds a non-zero timeout field and the requested transition is a shutdown or a reboot/reset since it does not make any sense to register a reboot notifier when a suspend transition is requested: it would never be called and it could intefere badly with the forceful workers.

Fixes 2c4b97fee94a ("firmware: arm_scmi: Add SCMI System Power Control driver")
---
 drivers/firmware/arm/scmi/scmi_power_control.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/firmware/arm/scmi/scmi_power_control.c +8 -2
index c4db5ed6c53f..72560f158a5f 100644
@@ -241,7 +241,13 @@ static void scmi_request_graceful_transition(struct scmi_syspower_conf *sc,
 {
 	unsigned int adj_timeout_ms = 0;
 
-	if (timeout_ms) {
+	/*
+	 * Note that by the SCMI specification this field should be considered
+	 * valid ONLY for graceful shutdown requests.
+	 *
+	 * Make sure to ignore any buggy firmware requests.
+	 */
+	if (sc->required_transition < SCMI_SYSTEM_POWERUP && timeout_ms) {
 		int ret;
 
 		sc->reboot_nb.notifier_call = &scmi_reboot_notifier;
@@ -342,7 +348,7 @@ static int scmi_userspace_notifier(struct notifier_block *nb,
 	sc->required_transition = er->system_state;
 
 	/* Leaving a trace in logs of who triggered the shutdown/reboot. */
-	dev_info(sc->dev, "Serving shutdown/reboot request: %d\n",
+	dev_info(sc->dev, "Serving shutdown/reboot/suspend request: %d\n",
 		 sc->required_transition);
 
 	scmi_request_graceful_transition(sc, er->timeout);
Signature · 1 line
2.54.0