Skip to contentSkip to conversation

remoteproc: sysfs/debugfs: fix race while updating recovery flag

PATCH In review Satya Durga Srinivasu Prabhala 3 participants 4 messages Feb 1, 2023 – Feb 14, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 remoteproc: sysfs/debugfs: fix race while updating recovery flag

When multiple clients try to update the recovery flag, it is possible that, race condition would lead to undesired results as updates to recovery flag isn't protected by any mechanism today. To avoid such issues, take remoteproc mutex lock before updating recovery flag and release the lock once done.

---
v1 -> v2:
- addressed comments from Mukesh Ojha
1. take & release lock only while updating recovery flag
2. update debugfs

 drivers/remoteproc/remoteproc_debugfs.c | 4 ++++
 drivers/remoteproc/remoteproc_sysfs.c   | 4 ++++
 2 files changed, 8 insertions(+)
drivers/remoteproc/remoteproc_debugfs.c +4 -0
index b86c1d09c70c..2c44d375024e 100644
@@ -226,10 +226,14 @@ rproc_recovery_write(struct file *filp, const char __user *user_buf,
 
 	if (!strncmp(buf, "enabled", count)) {
 		/* change the flag and begin the recovery process if needed */
+		mutex_lock(&rproc->lock);
 		rproc->recovery_disabled = false;
+		mutex_unlock(&rproc->lock);
 		rproc_trigger_recovery(rproc);
 	} else if (!strncmp(buf, "disabled", count)) {
+		mutex_lock(&rproc->lock);
 		rproc->recovery_disabled = true;
+		mutex_unlock(&rproc->lock);
 	} else if (!strncmp(buf, "recover", count)) {
 		/* begin the recovery process without changing the flag */
 		rproc_trigger_recovery(rproc);
drivers/remoteproc/remoteproc_sysfs.c +4 -0
index 8c7ea8922638..628e0de9a132 100644
@@ -50,10 +50,14 @@ static ssize_t recovery_store(struct device *dev,
 
 	if (sysfs_streq(buf, "enabled")) {
 		/* change the flag and begin the recovery process if needed */
+		mutex_lock(&rproc->lock);
 		rproc->recovery_disabled = false;
+		mutex_unlock(&rproc->lock);
 		rproc_trigger_recovery(rproc);
 	} else if (sysfs_streq(buf, "disabled")) {
+		mutex_lock(&rproc->lock);
 		rproc->recovery_disabled = true;
+		mutex_unlock(&rproc->lock);
 	} else if (sysfs_streq(buf, "recover")) {
 		/* begin the recovery process without changing the flag */
 		rproc_trigger_recovery(rproc);
Signature · 1 line
2.38.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Satya Durga Srinivasu Prabhala

When multiple clients try to update the recovery flag, it is possible that, race condition would lead to undesired results as updates to recovery flag isn't protected by any mechanism today. To avoid such issues, take remoteproc mutex lock before updating recovery flag and release the lock once done.

LGTM.

-Mukesh

Quoted 48 lines

---
v1 -> v2:
- addressed comments from Mukesh Ojha
1. take & release lock only while updating recovery flag
2. update debugfs

  drivers/remoteproc/remoteproc_debugfs.c | 4 ++++
  drivers/remoteproc/remoteproc_sysfs.c   | 4 ++++
  2 files changed, 8 insertions(+)
drivers/remoteproc/remoteproc_debugfs.c +4 -0
index b86c1d09c70c..2c44d375024e 100644
@@ -226,10 +226,14 @@ rproc_recovery_write(struct file *filp, const char __user *user_buf,
 
  	if (!strncmp(buf, "enabled", count)) {
  		/* change the flag and begin the recovery process if needed */
+		mutex_lock(&rproc->lock);
  		rproc->recovery_disabled = false;
+		mutex_unlock(&rproc->lock);
  		rproc_trigger_recovery(rproc);
  	} else if (!strncmp(buf, "disabled", count)) {
+		mutex_lock(&rproc->lock);
  		rproc->recovery_disabled = true;
+		mutex_unlock(&rproc->lock);
  	} else if (!strncmp(buf, "recover", count)) {
  		/* begin the recovery process without changing the flag */
  		rproc_trigger_recovery(rproc);
drivers/remoteproc/remoteproc_sysfs.c +4 -0
index 8c7ea8922638..628e0de9a132 100644
@@ -50,10 +50,14 @@ static ssize_t recovery_store(struct device *dev,
 
  	if (sysfs_streq(buf, "enabled")) {
  		/* change the flag and begin the recovery process if needed */
+		mutex_lock(&rproc->lock);
  		rproc->recovery_disabled = false;
+		mutex_unlock(&rproc->lock);
  		rproc_trigger_recovery(rproc);
  	} else if (sysfs_streq(buf, "disabled")) {
+		mutex_lock(&rproc->lock);
  		rproc->recovery_disabled = true;
+		mutex_unlock(&rproc->lock);
  	} else if (sysfs_streq(buf, "recover")) {
  		/* begin the recovery process without changing the flag */
  		rproc_trigger_recovery(rproc);
#2 lore raw
Quoted 9 lines · Mukesh Ojha
Quoted 6 lines · Satya Durga Srinivasu Prabhala

When multiple clients try to update the recovery flag, it is possible that, race condition would lead to undesired results as updates to recovery flag isn't protected by any mechanism today. To avoid such issues, take remoteproc mutex lock before updating recovery flag and release the lock once done.

Quoted 5 lines
Quoted 1 line

LGTM.

Thanks Mukesh.

Hi Bjorn,

Can you help cross check?

Best,
Satya

Quoted 51 lines

-Mukesh

Quoted 49 lines

---
v1 -> v2:
- addressed comments from Mukesh Ojha
   1. take & release lock only while updating recovery flag
   2. update debugfs

  drivers/remoteproc/remoteproc_debugfs.c | 4 ++++ drivers/remoteproc/remoteproc_sysfs.c   | 4 ++++

  2 files changed, 8 insertions(+)
drivers/remoteproc/remoteproc_debugfs.c +4 -0
index b86c1d09c70c..2c44d375024e 100644
@@ -226,10 +226,14 @@ rproc_recovery_write(struct file *filp, const 
 char __user *user_buf,
 
       if (!strncmp(buf, "enabled", count)) {
           /* change the flag and begin the recovery process if needed */
+        mutex_lock(&rproc->lock);
           rproc->recovery_disabled = false;
+        mutex_unlock(&rproc->lock);
           rproc_trigger_recovery(rproc);
       } else if (!strncmp(buf, "disabled", count)) {
+        mutex_lock(&rproc->lock);
           rproc->recovery_disabled = true;
+        mutex_unlock(&rproc->lock);
       } else if (!strncmp(buf, "recover", count)) {
           /* begin the recovery process without changing the flag */

          rproc_trigger_recovery(rproc);

drivers/remoteproc/remoteproc_sysfs.c +4 -0
index 8c7ea8922638..628e0de9a132 100644
@@ -50,10 +50,14 @@ static ssize_t recovery_store(struct device *dev,
 
       if (sysfs_streq(buf, "enabled")) {
           /* change the flag and begin the recovery process if needed */
+        mutex_lock(&rproc->lock);
           rproc->recovery_disabled = false;
+        mutex_unlock(&rproc->lock);
           rproc_trigger_recovery(rproc);
       } else if (sysfs_streq(buf, "disabled")) {
+        mutex_lock(&rproc->lock);
           rproc->recovery_disabled = true;
+        mutex_unlock(&rproc->lock);
       } else if (sysfs_streq(buf, "recover")) {
           /* begin the recovery process without changing the flag */
           rproc_trigger_recovery(rproc);
maintainer
#3 lore raw
In reply to Satya Durga Srinivasu Prabhala
Quoted 6 lines · Satya Durga Srinivasu Prabhala

When multiple clients try to update the recovery flag, it is possible that, race condition would lead to undesired results as updates to recovery flag isn't protected by any mechanism today. To avoid such issues, take remoteproc mutex lock before updating recovery flag and release the lock once done.

The only query of recovery_disabled that I can see is in rproc_crash_handler_work(), outside of any lock. So I'm not able to see the issue you're referring to.

Can you please help me understand better?

Thanks,
Bjorn

Quoted 52 lines

---
v1 -> v2:
- addressed comments from Mukesh Ojha
1. take & release lock only while updating recovery flag
2. update debugfs

 drivers/remoteproc/remoteproc_debugfs.c | 4 ++++
 drivers/remoteproc/remoteproc_sysfs.c   | 4 ++++
 2 files changed, 8 insertions(+)
drivers/remoteproc/remoteproc_debugfs.c +4 -0
index b86c1d09c70c..2c44d375024e 100644
@@ -226,10 +226,14 @@ rproc_recovery_write(struct file *filp, const char __user *user_buf,
 
 	if (!strncmp(buf, "enabled", count)) {
 		/* change the flag and begin the recovery process if needed */
+		mutex_lock(&rproc->lock);
 		rproc->recovery_disabled = false;
+		mutex_unlock(&rproc->lock);
 		rproc_trigger_recovery(rproc);
 	} else if (!strncmp(buf, "disabled", count)) {
+		mutex_lock(&rproc->lock);
 		rproc->recovery_disabled = true;
+		mutex_unlock(&rproc->lock);
 	} else if (!strncmp(buf, "recover", count)) {
 		/* begin the recovery process without changing the flag */
 		rproc_trigger_recovery(rproc);
drivers/remoteproc/remoteproc_sysfs.c +4 -0
index 8c7ea8922638..628e0de9a132 100644
@@ -50,10 +50,14 @@ static ssize_t recovery_store(struct device *dev,
 
 	if (sysfs_streq(buf, "enabled")) {
 		/* change the flag and begin the recovery process if needed */
+		mutex_lock(&rproc->lock);
 		rproc->recovery_disabled = false;
+		mutex_unlock(&rproc->lock);
 		rproc_trigger_recovery(rproc);
 	} else if (sysfs_streq(buf, "disabled")) {
+		mutex_lock(&rproc->lock);
 		rproc->recovery_disabled = true;
+		mutex_unlock(&rproc->lock);
 	} else if (sysfs_streq(buf, "recover")) {
 		/* begin the recovery process without changing the flag */
 		rproc_trigger_recovery(rproc);
Signature · 1 line
2.38.1