Commit 5e6a0e05 authored by Shengjiu Wang's avatar Shengjiu Wang Committed by Mathieu Poirier

remoteproc: core: Move state checking to remoteproc_core

There is no mutex protection of these state checking for 'stop'
and 'detach' which can't guarantee there is no another instance
is trying to do same operation.

Consider two instances case:
Instance1: echo stop > /sys/class/remoteproc/remoteproc0/state
Instance2: echo stop > /sys/class/remoteproc/remoteproc0/state

The issue is that the instance2 case may success, Or it
may fail with -EINVAL, which is uncertain.

So move this state checking in rproc_cdev_write() and
state_store() for 'stop', 'detach' operation to
'rproc_shutdown' , 'rproc_detach' function under the mutex
protection.
Signed-off-by: default avatarShengjiu Wang <shengjiu.wang@nxp.com>
Link: https://lore.kernel.org/r/1648434012-16655-3-git-send-email-shengjiu.wang@nxp.comSigned-off-by: default avatarMathieu Poirier <mathieu.poirier@linaro.org>
parent 8f454f95
...@@ -34,15 +34,8 @@ static ssize_t rproc_cdev_write(struct file *filp, const char __user *buf, size_ ...@@ -34,15 +34,8 @@ static ssize_t rproc_cdev_write(struct file *filp, const char __user *buf, size_
if (!strncmp(cmd, "start", len)) { if (!strncmp(cmd, "start", len)) {
ret = rproc_boot(rproc); ret = rproc_boot(rproc);
} else if (!strncmp(cmd, "stop", len)) { } else if (!strncmp(cmd, "stop", len)) {
if (rproc->state != RPROC_RUNNING &&
rproc->state != RPROC_ATTACHED)
return -EINVAL;
ret = rproc_shutdown(rproc); ret = rproc_shutdown(rproc);
} else if (!strncmp(cmd, "detach", len)) { } else if (!strncmp(cmd, "detach", len)) {
if (rproc->state != RPROC_ATTACHED)
return -EINVAL;
ret = rproc_detach(rproc); ret = rproc_detach(rproc);
} else { } else {
dev_err(&rproc->dev, "Unrecognized option\n"); dev_err(&rproc->dev, "Unrecognized option\n");
......
...@@ -2071,6 +2071,12 @@ int rproc_shutdown(struct rproc *rproc) ...@@ -2071,6 +2071,12 @@ int rproc_shutdown(struct rproc *rproc)
return ret; return ret;
} }
if (rproc->state != RPROC_RUNNING &&
rproc->state != RPROC_ATTACHED) {
ret = -EINVAL;
goto out;
}
/* if the remote proc is still needed, bail out */ /* if the remote proc is still needed, bail out */
if (!atomic_dec_and_test(&rproc->power)) if (!atomic_dec_and_test(&rproc->power))
goto out; goto out;
...@@ -2130,6 +2136,11 @@ int rproc_detach(struct rproc *rproc) ...@@ -2130,6 +2136,11 @@ int rproc_detach(struct rproc *rproc)
return ret; return ret;
} }
if (rproc->state != RPROC_ATTACHED) {
ret = -EINVAL;
goto out;
}
/* if the remote proc is still needed, bail out */ /* if the remote proc is still needed, bail out */
if (!atomic_dec_and_test(&rproc->power)) { if (!atomic_dec_and_test(&rproc->power)) {
ret = 0; ret = 0;
......
...@@ -198,15 +198,8 @@ static ssize_t state_store(struct device *dev, ...@@ -198,15 +198,8 @@ static ssize_t state_store(struct device *dev,
if (ret) if (ret)
dev_err(&rproc->dev, "Boot failed: %d\n", ret); dev_err(&rproc->dev, "Boot failed: %d\n", ret);
} else if (sysfs_streq(buf, "stop")) { } else if (sysfs_streq(buf, "stop")) {
if (rproc->state != RPROC_RUNNING &&
rproc->state != RPROC_ATTACHED)
return -EINVAL;
ret = rproc_shutdown(rproc); ret = rproc_shutdown(rproc);
} else if (sysfs_streq(buf, "detach")) { } else if (sysfs_streq(buf, "detach")) {
if (rproc->state != RPROC_ATTACHED)
return -EINVAL;
ret = rproc_detach(rproc); ret = rproc_detach(rproc);
} else { } else {
dev_err(&rproc->dev, "Unrecognised option: %s\n", buf); dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment