Skip to contentSkip to conversation

remoteproc: core: Fix the error unwind paths

PATCH New Yonghao Zhang 1 participant 5 messages Nov 24 Remote processor (remoteproc)

lore
Share & tools
lore
#0 lore raw

v2 cover remoteproc: core: Fix the error unwind paths

This series fixes four problems in the error unwind paths of the remoteproc core.

The most visible change in v2 is 3/4. It no longer reworks the attach unwind to prefer detach() over stop(): without the STM32 review that behaviour change required, it could not be justified. The patch now only guards the unconditional ops->stop() call against an implementation that does not provide it, exactly as 2/4 does in rproc_start(). Implementations that provide stop() keep the existing unwind, so the STM32 detach() path is not affected. The detach()-first semantics can be revisited separately with the platform maintainers.

The patches:

- 1/4 resets a freed vring entry to FW_RSC_ADDR_ANY instead of 0. 0 is a valid device address, so a remote processor that reads the entry while it is still running could mistake it for a real vring. The sentence in the comment above the reset that is no longer true is dropped at the same time.

- 2/4 skips the stop() rollback in rproc_start() when the implementation does not provide it, instead of dereferencing NULL.

- 3/4 adds the same guard to the attach unwind in __rproc_attach().

- 4/4 releases the resources of a boot that a failed recovery was trying to restore, following the cleanup sequence of rproc_shutdown(), and voids the power references when the failure leaves the processor offline or detached.

Testing: on an Allwinner T153 (sun8iw22) with an E907 remote core, driven by an out-of-tree platform driver. The vring reset of 1/4 and the recovery failure of 4/4 were exercised on this hardware. The guards in 2/4 and 3/4 cover configurations that the in-tree drivers do not exercise today.

---
Changes in v2:
- 1/4: Keep the existing comment and drop only the sentence that became obsolete; trim the changelog (Mathieu).
- 2/4: Select between stop_rproc and unprepare_subdevices at the rproc_start_subdevices() error path instead of branching in the shared unwind path (Mathieu).
- 3/4: Replace the detach()-first rework with a guard against a missing stop(), so no existing driver changes behaviour. The STM32 detach() path is not affected anymore.
- 4/4: Describe the code as applied, follow the __rproc_shutdown() cleanup sequence, and check for RPROC_OFFLINE/RPROC_DETACHED explicitly; trim the changelog (Mathieu).

v1: https://lore.kernel.org/all/[email protected]

Yonghao Zhang (4):
remoteproc: core: Reset freed vring entries to FW_RSC_ADDR_ANY
remoteproc: core: Guard against a missing stop() in rproc_start()
remoteproc: core: Guard against a missing stop() in the attach unwind
remoteproc: core: Clean up after a failed recovery

 drivers/remoteproc/remoteproc_core.c | 47 +++++++++++++++++++++++-----
 1 file changed, 40 insertions(+), 7 deletions(-)

base-commit: f39ba58941efa63e6c7d10d53d183d6077524bdb

Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/4 remoteproc: core: Reset freed vring entries to FW_RSC_ADDR_ANY

rproc_free_vring() resets a vring entry in the resource table as it releases it. Resetting da to 0 leaves the entry looking like a vring at device address 0; write FW_RSC_ADDR_ANY instead, the marker for an unallocated address, which is what the entry means from then on.

The last sentence of the comment above the reset claims that the cached table is only available for a processor booted by the core. rproc_reset_rsc_table_on_{stop,detach}() now install a copy of the table for processors started by another entity, so drop the sentence.

Fixes c0d631570ad5 ("remoteproc: set vring addresses in resource table")
Fixes 4d3ebb3b9990 ("remoteproc: Refactor function rproc_free_vring()")
---
 drivers/remoteproc/remoteproc_core.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/remoteproc/remoteproc_core.c +2 -4
index d5fa9d23bd5c..b6fe74d34e5c 100644
@@ -417,16 +417,14 @@ void rproc_free_vring(struct rproc_vring *rvring)
 	 * At this point rproc_stop() has been called and the installed resource
 	 * table in the remote processor memory may no longer be accessible. As
 	 * such and as per rproc_stop(), rproc->table_ptr points to the cached
-	 * resource table (rproc->cached_table).  The cached resource table is
-	 * only available when a remote processor has been booted by the
-	 * remoteproc core, otherwise it is NULL.
+	 * resource table (rproc->cached_table).
 	 *
 	 * Based on the above, reset the virtio device section in the cached
 	 * resource table only if there is one to work with.
 	 */
 	if (rproc->table_ptr) {
 		rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
-		rsc->vring[idx].da = 0;
+		rsc->vring[idx].da = FW_RSC_ADDR_ANY;
 		rsc->vring[idx].notifyid = -1;
 	}
 }
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/4 remoteproc: core: Guard against a missing stop() in rproc_start()

In reply to Yonghao Zhang

When the subdevice registration fails after a successful ops->start(), rproc_start() unrolls with an unconditional ops->stop() call: an implementation without stop() turns that error path into a NULL dereference, and there is no other way to undo a start once the processor is up.

Nothing rules that implementation out. rproc_validate() checks the callbacks against the state a processor registers in: start() for an offline one, attach() for a detached one, which never look at stop(); the only written rule, Documentation/staging/remoteproc.rst ("Every remoteproc implementation should at least provide the ->start and ->stop handlers"), is a should the core does not enforce. The in-tree implementations all provide both, or neither when they only attach (commit 1168af40b1ad ("remoteproc: k3-r5: Add support for IPC-only mode for all R5Fs")), so none of them can reach the call today.

Skip the rollback call when there is no stop(): with nothing to roll the start back with, the processor stays running and the failure is reported by the boot attempt itself.

Fixes 7bdc9650f036 ("remoteproc: Introduce subdevices")
---
 drivers/remoteproc/remoteproc_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/remoteproc/remoteproc_core.c +4 -1
index b6fe74d34e5c..e44b45510b4e 100644
@@ -1310,7 +1310,10 @@ static int rproc_start(struct rproc *rproc, const struct firmware *fw)
 	if (ret) {
 		dev_err(dev, "failed to probe subdevices for %s: %d\n",
 			rproc->name, ret);
-		goto stop_rproc;
+		if (rproc->ops->stop)
+			goto stop_rproc;
+		dev_err(dev, "can't roll %s back: no stop()\n", rproc->name);
+		goto unprepare_subdevices;
 	}
 
 	rproc->state = RPROC_RUNNING;
Signature · 1 line
2.34.1
#3 lore raw

PATCH v2 3/4 remoteproc: core: Guard against a missing stop() in the attach unwind

In reply to Yonghao Zhang

When the subdevice registration fails after a successful ops->attach(), __rproc_attach() unrolls with an unconditional ops->stop() call: an implementation without stop() turns that error path into a NULL dereference.

An attach-only implementation, such as commit 1168af40b1ad ("remoteproc:
k3-r5: Add support for IPC-only mode for all R5Fs"), has no stop() and can reach that path. Skip the rollback call when there is no stop():
with nothing to roll the attach back with, the processor stays running and the failure is reported by the attach attempt itself.

Fixes d848a4819d85 ("remoteproc: Introducing function rproc_attach()")
---
 drivers/remoteproc/remoteproc_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/remoteproc/remoteproc_core.c +4 -1
index e44b45510b4e..8c3fc85b8f04 100644
@@ -1357,7 +1357,10 @@ static int __rproc_attach(struct rproc *rproc)
 	if (ret) {
 		dev_err(dev, "failed to probe subdevices for %s: %d\n",
 			rproc->name, ret);
-		goto stop_rproc;
+		if (rproc->ops->stop)
+			goto stop_rproc;
+		dev_err(dev, "can't roll %s back: no stop()\n", rproc->name);
+		goto unprepare_subdevices;
 	}
 
 	rproc->state = RPROC_ATTACHED;
Signature · 1 line
2.34.1
#4 lore raw

PATCH v2 4/4 remoteproc: core: Clean up after a failed recovery

In reply to Yonghao Zhang

Release the resources on the failure paths of rproc_boot_recovery(), the same way rproc_shutdown() does, and void the power count in rproc_trigger_recovery() when a failed recovery leaves the processor offline or detached.

Fixes ba194232edc0 ("remoteproc: Support attach recovery after rproc crash")
---
 drivers/remoteproc/remoteproc_core.c | 31 +++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
drivers/remoteproc/remoteproc_core.c +30 -1
index 8c3fc85b8f04..3e700c94110d 100644
@@ -1800,7 +1800,7 @@ static int rproc_boot_recovery(struct rproc *rproc)
 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
 	if (ret < 0) {
 		dev_err(dev, "request_firmware failed: %d\n", ret);
-		return ret;
+		goto clean_up_resources;
 	}
 
 	/* boot the remote processor up again */
@@ -1808,6 +1808,24 @@ static int rproc_boot_recovery(struct rproc *rproc)
 
 	release_firmware(firmware_p);
 
+	if (ret < 0)
+		goto clean_up_resources;
+
+	return 0;
+
+clean_up_resources:
+	/*
+	 * rproc_stop() has already switched the remote processor off;
+	 * release the resources of the boot this recovery was trying to
+	 * restore.
+	 */
+	rproc_resource_cleanup(rproc);
+	/* release HW resources if needed */
+	rproc_unprepare_device(rproc);
+	rproc_disable_iommu(rproc);
+	kfree(rproc->cached_table);
+	rproc->cached_table = NULL;
+	rproc->table_ptr = NULL;
 	return ret;
 }
 
@@ -1848,6 +1866,17 @@ int rproc_trigger_recovery(struct rproc *rproc)
 	else
 		ret = rproc_boot_recovery(rproc);
 
+	/*
+	 * A failed recovery leaves the remote processor offline or
+	 * detached, where rproc_shutdown() cannot release the power
+	 * references: void them.
+	 */
+	if (ret && (rproc->state == RPROC_OFFLINE ||
+		    rproc->state == RPROC_DETACHED)) {
+		dev_err(dev, "failed to recover %s: %d\n", rproc->name, ret);
+		atomic_set(&rproc->power, 0);
+	}
+
 unlock_mutex:
 	mutex_unlock(&rproc->lock);
 	return ret;
Signature · 1 line
2.34.1