Skip to contentSkip to conversation

firmware: arm_scmi: Roll back partial protocol table registration

PATCH Applied Sudeep Holla 1 participant 3 messages Jul 22 – Jul 24 System control & power/management interface (SCPI/SCMI) Message Protocol drivers

lore
Share & tools
lore

Sudeep Holla

author maintainer
#0 lore raw

PATCH 1/2 firmware: arm_scmi: Roll back partial protocol table registration

scmi_protocol_table_register() can leave earlier requests registered when a later entry in the same ID table fails. Each request retains a pointer to the driver's ID table, so a failed module load can leave a dangling pointer after the module storage is released.

Unrequest only the successfully registered prefix, in reverse order, before returning the failure. Leave the failed entry and the remaining entries untouched because matching requests can be owned by another driver.

Fixes 2858f6e5f064 ("firmware: arm_scmi: Add multiple protocols registration support")
---
 drivers/firmware/arm_scmi/bus.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

Hi,

Couple of more issues[2] reported as part of review of [1]. These are addressing those issues.

Regards,
Sudeep

[1] https://patch.msgid.link/[email protected]
[2] https://sashiko.dev/#/patchset/[email protected]

drivers/firmware/arm_scmi/bus.c +20 -11
index df396ab638dd..af3d28717cd2 100644
@@ -135,17 +135,6 @@ static int scmi_protocol_device_request(const struct scmi_device_id *id_table)
 	return ret;
 }
 
-static int scmi_protocol_table_register(const struct scmi_device_id *id_table)
-{
-	int ret = 0;
-	const struct scmi_device_id *entry;
-
-	for (entry = id_table; entry->name && ret == 0; entry++)
-		ret = scmi_protocol_device_request(entry);
-
-	return ret;
-}
-
 /**
  * scmi_protocol_device_unrequest  - Helper to unrequest a device
  *
@@ -191,6 +180,26 @@ static void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table
 	}
 }
 
+static int scmi_protocol_table_register(const struct scmi_device_id *id_table)
+{
+	const struct scmi_device_id *entry;
+	int ret;
+
+	for (entry = id_table; entry->name; entry++) {
+		ret = scmi_protocol_device_request(entry);
+		if (ret)
+			goto err_unrequest;
+	}
+
+	return 0;
+
+err_unrequest:
+	while (entry != id_table)
+		scmi_protocol_device_unrequest(--entry);
+
+	return ret;
+}
+
 static void
 scmi_protocol_table_unregister(const struct scmi_device_id *id_table)
 {
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 2/2 firmware: arm_scmi: Unrequest devices if driver registration fails

scmi_driver_register() requests protocol devices before registering the driver. If driver_register() fails, those requests remain in the global IDR and retain pointers to the module's ID table. Once the failed module load releases that storage, later request matching or SCMI device creation can dereference the stale pointers.

Unrequest the complete protocol table before returning the registration failure. At this point table registration succeeded, so every entry is owned by the current registration attempt.

Fixes d3cd7c525fd2 ("firmware: arm_scmi: Refactor protocol device creation")
---
 drivers/firmware/arm_scmi/bus.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/firmware/arm_scmi/bus.c +7 -3
index af3d28717cd2..a86fa18fc5d2 100644
@@ -395,10 +395,14 @@ int scmi_driver_register(struct scmi_driver *driver, struct module *owner,
 	driver->driver.mod_name = mod_name;
 
 	retval = driver_register(&driver->driver);
-	if (!retval)
-		pr_debug("Registered new scmi driver %s\n", driver->name);
+	if (retval) {
+		scmi_protocol_table_unregister(driver->id_table);
+		return retval;
+	}
 
-	return retval;
+	pr_debug("Registered new scmi driver %s\n", driver->name);
+
+	return 0;
 }
 EXPORT_SYMBOL_GPL(scmi_driver_register);
 
Signature · 1 line
2.43.0
author maintainer Applied
#2 lore raw
In reply to Sudeep Holla
Quoted 11 lines · Sudeep Holla

scmi_protocol_table_register() can leave earlier requests registered when a later entry in the same ID table fails. Each request retains a pointer to the driver's ID table, so a failed module load can leave a dangling pointer after the module storage is released.

Unrequest only the successfully registered prefix, in reverse order, before returning the failure. Leave the failed entry and the remaining entries untouched because matching requests can be owned by another driver.

[...]

Applied to sudeep.holla/linux (for-next/scmi/updates), thanks!

[1/2] firmware: arm_scmi: Roll back partial protocol table registration

      https://git.kernel.org/sudeep.holla/c/2224b622260b
[2/2] firmware: arm_scmi: Unrequest devices if driver registration fails
      https://git.kernel.org/sudeep.holla/c/9f7cd6a62aa7
Signature · 2 lines
Regards,
Sudeep