Skip to contentSkip to conversation

remoteproc: improve robustness for rproc_attach fail cases

PATCH Applied Jingyi Wang 2 participants 5 messages Jun 23 – Jul 10 Remote processor (remoteproc)

lore
Share & tools
lore
#0 lore raw

v3 cover remoteproc: improve robustness for rproc_attach fail cases

Failure in rproc_attach() path can cause issues like NULL pointer dereference when doing recovery and rproc_add() fail, improve the robustness for rproc_attach fail cases.

---
Changes in v3:
- update and check subdevs_started flag in rproc_start_subdevices()/rproc_stop_subdevices()
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- commit msg update
- Introduce subdevs_started flag instead of check nullptr
- Add change to cancel crash_handler work
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Jingyi Wang (3):
remoteproc: core: Attach rproc asynchronously in rproc_add() path via schedule_work()
remoteproc: qcom: Check subdev start status in rproc_stop()
remoteproc: core: cancel crash_handler work in rproc_add() error path

 drivers/remoteproc/remoteproc_core.c | 28 ++++++++++++++++++++--------
 include/linux/remoteproc.h           |  4 ++++
 2 files changed, 24 insertions(+), 8 deletions(-)

---
base-commit: 4fa3f5fabb30bf00d7475d5a33459ea83d639bf9
change-id: 20260623-rproc-attach-issue-a0adafa203b8

Best regards,

Signature · 1 line
Jingyi Wang <[email protected]>
#1 lore raw

PATCH v3 1/3 remoteproc: core: Attach rproc asynchronously in rproc_add() path via schedule_work()

Unlike the remoteproc firmware load path where rproc_add() call rproc_auto_boot_callback() asynchronously and ignores the return value of rproc_boot(), the attach path calls rproc_boot() synchronously and propagates its return value back to rproc_add(). This means a failure during rproc_attach() causes rproc_add() to fail and triggers resource release, removing the remoteproc from sysfs and making it unavailable for recovery or further boot attempts.

Align the remoteproc attach path with the firmware load path by introducing attach_work and scheduling rproc_boot() asynchronously via schedule_work(). This keeps the remoteproc registered and available in sysfs even if the initial attach attempt fails, and avoids blocking rproc_add() on the attach result.

---
 drivers/remoteproc/remoteproc_core.c | 20 ++++++++++++--------
 include/linux/remoteproc.h           |  2 ++
 2 files changed, 14 insertions(+), 8 deletions(-)
drivers/remoteproc/remoteproc_core.c +12 -8
index f003be006b1b..50f754e27d1a 100644
@@ -1668,18 +1668,21 @@ static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
 	release_firmware(fw);
 }
 
+static void rproc_attach_work(struct work_struct *work)
+{
+	struct rproc *rproc = container_of(work, struct rproc, attach_work);
+
+	rproc_boot(rproc);
+}
+
 static int rproc_trigger_auto_boot(struct rproc *rproc)
 {
 	int ret;
 
-	/*
-	 * Since the remote processor is in a detached state, it has already
-	 * been booted by another entity.  As such there is no point in waiting
-	 * for a firmware image to be loaded, we can simply initiate the process
-	 * of attaching to it immediately.
-	 */
-	if (rproc->state == RPROC_DETACHED)
-		return rproc_boot(rproc);
+	if (rproc->state == RPROC_DETACHED) {
+		schedule_work(&rproc->attach_work);
+		return 0;
+	}
 
 	/*
 	 * We're initiating an asynchronous firmware loading, so we can
@@ -2507,6 +2510,7 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,
 	INIT_LIST_HEAD(&rproc->dump_segments);
 
 	INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
+	INIT_WORK(&rproc->attach_work, rproc_attach_work);
 
 	rproc->state = RPROC_OFFLINE;
 
include/linux/remoteproc.h +2 -0
index 7c1546d48008..f1d14d075bf3 100644
@@ -259,6 +259,7 @@ enum rproc_features {
  * @subdevs: list of subdevices, to following the running state
  * @notifyids: idr for dynamically assigning rproc-wide unique notify ids
  * @index: index of this rproc device
+ * @attach_work: workqueue for attaching rproc
  * @crash_handler: workqueue for handling a crash
  * @crash_cnt: crash counter
  * @recovery_disabled: flag that state if recovery was disabled
@@ -301,6 +302,7 @@ struct rproc {
 	struct list_head subdevs;
 	struct idr notifyids;
 	int index;
+	struct work_struct attach_work;
 	struct work_struct crash_handler;
 	unsigned int crash_cnt;
 	bool recovery_disabled;
Signature · 1 line
2.34.1
#2 lore raw

PATCH v3 2/3 remoteproc: qcom: Check subdev start status in rproc_stop()

In reply to Jingyi Wang

For rproc that doing attach, rproc_start_subdevices() is called only when attach successfully. If rproc_report_crash() is called in the attach function, rproc_boot_recovery()->rproc_stop()->rproc_stop_subdevices()-> glink_subdev_stop() could be called and cause NULL pointer dereference:

 Unable to handle kernel NULL pointer dereference at virtual address 0000000000000300
 Mem abort info:
 ...
 pc : qcom_glink_smem_unregister+0x14/0x48 [qcom_glink_smem]
 lr : glink_subdev_stop+0x1c/0x30 [qcom_common]
 ...
 Call trace:
  qcom_glink_smem_unregister+0x14/0x48 [qcom_glink_smem] (P)
  glink_subdev_stop+0x1c/0x30 [qcom_common]
  rproc_stop+0x58/0x17c
  rproc_trigger_recovery+0xb0/0x150
  rproc_crash_handler_work+0xa4/0xc4
  process_scheduled_works+0x18c/0x2d8
  worker_thread+0x144/0x280
  kthread+0x124/0x138
  ret_from_fork+0x10/0x20
 Code: a9be7bfd 910003fd a90153f3 aa0003f3 (b9430000)
 ---[ end trace 0000000000000000 ]---

Introduce "subdevs_started" flag to indicate rproc_start_subdevices() has been called successfully. Ensure subdevices are only stopped if they have been started.

---
 drivers/remoteproc/remoteproc_core.c | 7 +++++++
 include/linux/remoteproc.h           | 2 ++
 2 files changed, 9 insertions(+)
drivers/remoteproc/remoteproc_core.c +7 -0
index 50f754e27d1a..71a7be7c1e9f 100644
@@ -1099,6 +1099,8 @@ static int rproc_start_subdevices(struct rproc *rproc)
 		}
 	}
 
+	rproc->subdevs_started = true;
+
 	return 0;
 
 unroll_registration:
@@ -1114,10 +1116,15 @@ static void rproc_stop_subdevices(struct rproc *rproc, bool crashed)
 {
 	struct rproc_subdev *subdev;
 
+	if (!rproc->subdevs_started)
+		return;
+
 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
 		if (subdev->stop)
 			subdev->stop(subdev, crashed);
 	}
+
+	rproc->subdevs_started = false;
 }
 
 static void rproc_unprepare_subdevices(struct rproc *rproc)
include/linux/remoteproc.h +2 -0
index f1d14d075bf3..17ed75a11e15 100644
@@ -272,6 +272,7 @@ enum rproc_features {
  * @has_iommu: flag to indicate if remote processor is behind an MMU
  * @auto_boot: flag to indicate if remote processor should be auto-started
  * @sysfs_read_only: flag to make remoteproc sysfs files read only
+ * @subdevs_started: flag to indicate if subdevs have started
  * @dump_segments: list of segments in the firmware
  * @nb_vdev: number of vdev currently handled by rproc
  * @elf_class: firmware ELF class
@@ -314,6 +315,7 @@ struct rproc {
 	bool has_iommu;
 	bool auto_boot;
 	bool sysfs_read_only;
+	bool subdevs_started;
 	struct list_head dump_segments;
 	int nb_vdev;
 	u8 elf_class;
Signature · 1 line
2.34.1
#3 lore raw

PATCH v3 3/3 remoteproc: core: cancel crash_handler work in rproc_add() error path

In reply to Jingyi Wang

Ensure crash_handler work is cancelled before tearing down rproc resources to avoid accessing freed memory.

---
 drivers/remoteproc/remoteproc_core.c | 1 +
 1 file changed, 1 insertion(+)
drivers/remoteproc/remoteproc_core.c +1 -0
index 71a7be7c1e9f..ae885afc618d 100644
@@ -2327,6 +2327,7 @@ int rproc_add(struct rproc *rproc)
 	return 0;
 
 rproc_remove_dev:
+	cancel_work_sync(&rproc->crash_handler);
 	rproc_delete_debug_dir(rproc);
 	device_del(dev);
 rproc_remove_cdev:
Signature · 1 line
2.34.1
maintainer Applied
#4 lore raw
In reply to Jingyi Wang
Quoted 5 lines · Jingyi Wang

Failure in rproc_attach() path can cause issues like NULL pointer dereference when doing recovery and rproc_add() fail, improve the robustness for rproc_attach fail cases.

Applied, thanks!

[1/3] remoteproc: core: Attach rproc asynchronously in rproc_add() path via schedule_work()

      commit: 4aaf30e2b0319ba6b147191f6d2bd856c16a51ba
[2/3] remoteproc: qcom: Check subdev start status in rproc_stop()
      commit: a1aac0756a91193dfc50baf7b52bb4ee0ba91759
[3/3] remoteproc: core: cancel crash_handler work in rproc_add() error path
      commit: 573e7e135ea28716008083c311db7994b4d72191

Best regards,

Signature · 1 line
Bjorn Andersson <[email protected]>