Skip to contentSkip to conversation

remoteproc: core: check rproc->power value before decreasing it

PATCH In review Shengjiu Wang 2 participants 2 messages Mar 18, 2022 – Mar 24, 2022 Remote processor (remoteproc)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 remoteproc: core: check rproc->power value before decreasing it

There are possibility that two 'stop' operation happen in parallel, then the rproc->power may be decreased to -1, that this reference count will be in wrong state. So check rproc->power to make sure it is larger than zero before decreasing it.

---
 drivers/remoteproc/remoteproc_core.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/remoteproc/remoteproc_core.c +3 -0
index c510125769b9..84e065ad8743 100644
@@ -2075,6 +2075,9 @@ int rproc_shutdown(struct rproc *rproc)
 		return ret;
 	}
 
+	if (atomic_read(&rproc->power) <= 0)
+		goto out;
+
 	/* if the remote proc is still needed, bail out */
 	if (!atomic_dec_and_test(&rproc->power))
 		goto out;
Signature · 1 line
2.17.1
maintainer
#1 lore raw

Hi Shengjiu,

Quoted 16 lines · Shengjiu Wang

There are possibility that two 'stop' operation happen in parallel, then the rproc->power may be decreased to -1, that this reference count will be in wrong state. So check rproc->power to make sure it is larger than zero before decreasing it.

---
 drivers/remoteproc/remoteproc_core.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/remoteproc/remoteproc_core.c +0 -0
index c510125769b9..84e065ad8743 100644

drivers/remoteproc/remoteproc_core.c int rproc_shutdown(struct rproc *rproc) · line 2080

Quoted 6 lines
drivers/remoteproc/remoteproc_core.c +3 -0
@@ -2075,6 +2075,9 @@ int rproc_shutdown(struct rproc *rproc)
 		return ret;
 	}
 
+	if (atomic_read(&rproc->power) <= 0)
+		goto out;
+

Although the proposed solution will likely work, I think it is best to use rproc->state for this. Simply do the same test as in state_store()[1] and exit if the conditions are not satisfied. Please do the same thing for rproc_detach().

Thanks,
Mathieu

[1]. https://elixir.bootlin.com/linux/v5.17/source/drivers/remoteproc/remoteproc_sysfs.c#L205

Quoted 6 lines
drivers/remoteproc/remoteproc_core.c +0 -0
 	/* if the remote proc is still needed, bail out */
 	if (!atomic_dec_and_test(&rproc->power))
 		goto out;
Signature · 1 line
2.17.1