Skip to contentSkip to conversation

rpmsg: char: Add mutex protection for rpmsg_eptdev_open()

PATCH RESEND In review Shengjiu Wang 2 participants 3 messages May 21, 2022 – Jun 14, 2022 Remote processor messaging (rpmsg)

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 rpmsg: char: Add mutex protection for rpmsg_eptdev_open()

There is no mutex protection for rpmsg_eptdev_open(), especially for eptdev->ept read and write operation. It may cause issues when multiple instances call rpmsg_eptdev_open() in parallel,the return state may be success or EBUGY.

Fixes 964e8bedd5a1 ("rpmsg: char: Return an error if device already open")

---
changes in resend:
- add fixes tag

 drivers/rpmsg/rpmsg_char.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/rpmsg/rpmsg_char.c +6 -1
index b6183d4f62a2..4f2189111494 100644
@@ -120,8 +120,11 @@ static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
 	struct rpmsg_device *rpdev = eptdev->rpdev;
 	struct device *dev = &eptdev->dev;
 
-	if (eptdev->ept)
+	mutex_lock(&eptdev->ept_lock);
+	if (eptdev->ept) {
+		mutex_unlock(&eptdev->ept_lock);
 		return -EBUSY;
+	}
 
 	get_device(dev);
 
@@ -137,11 +140,13 @@ static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
 	if (!ept) {
 		dev_err(dev, "failed to open %s\n", eptdev->chinfo.name);
 		put_device(dev);
+		mutex_unlock(&eptdev->ept_lock);
 		return -EINVAL;
 	}
 
 	eptdev->ept = ept;
 	filp->private_data = eptdev;
+	mutex_unlock(&eptdev->ept_lock);
 
 	return 0;
 }
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 8 lines · Shengjiu Wang

There is no mutex protection for rpmsg_eptdev_open(), especially for eptdev->ept read and write operation. It may cause issues when multiple instances call rpmsg_eptdev_open() in parallel,the return state may be success or EBUGY.

Fixes 964e8bedd5a1 ("rpmsg: char: Return an error if device already open")

This looks good. I will fix the above typo and apply the patch when the 5.19 cycle starts.

Thanks,
Mathieu

Quoted 41 lines

---
changes in resend:
- add fixes tag

 drivers/rpmsg/rpmsg_char.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/rpmsg/rpmsg_char.c +6 -1
index b6183d4f62a2..4f2189111494 100644
@@ -120,8 +120,11 @@ static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
 	struct rpmsg_device *rpdev = eptdev->rpdev;
 	struct device *dev = &eptdev->dev;
 
-	if (eptdev->ept)
+	mutex_lock(&eptdev->ept_lock);
+	if (eptdev->ept) {
+		mutex_unlock(&eptdev->ept_lock);
 		return -EBUSY;
+	}
 
 	get_device(dev);
 
@@ -137,11 +140,13 @@ static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
 	if (!ept) {
 		dev_err(dev, "failed to open %s\n", eptdev->chinfo.name);
 		put_device(dev);
+		mutex_unlock(&eptdev->ept_lock);
 		return -EINVAL;
 	}
 
 	eptdev->ept = ept;
 	filp->private_data = eptdev;
+	mutex_unlock(&eptdev->ept_lock);
 
 	return 0;
 }
Signature · 1 line
2.17.1
maintainer
#2 lore raw
In reply to Shengjiu Wang
Quoted 6 lines · Shengjiu Wang

There is no mutex protection for rpmsg_eptdev_open(), especially for eptdev->ept read and write operation. It may cause issues when multiple instances call rpmsg_eptdev_open() in parallel,the return state may be success or EBUGY.

Quoted 8 lines
Fixes 964e8bedd5a1 ("rpmsg: char: Return an error if device already open")

---
changes in resend:
- add fixes tag

 drivers/rpmsg/rpmsg_char.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

I have applied your patch.

Thanks,
Mathieu

Quoted 35 lines
drivers/rpmsg/rpmsg_char.c +6 -1
index b6183d4f62a2..4f2189111494 100644
@@ -120,8 +120,11 @@ static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
 	struct rpmsg_device *rpdev = eptdev->rpdev;
 	struct device *dev = &eptdev->dev;
 
-	if (eptdev->ept)
+	mutex_lock(&eptdev->ept_lock);
+	if (eptdev->ept) {
+		mutex_unlock(&eptdev->ept_lock);
 		return -EBUSY;
+	}
 
 	get_device(dev);
 
@@ -137,11 +140,13 @@ static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
 	if (!ept) {
 		dev_err(dev, "failed to open %s\n", eptdev->chinfo.name);
 		put_device(dev);
+		mutex_unlock(&eptdev->ept_lock);
 		return -EINVAL;
 	}
 
 	eptdev->ept = ept;
 	filp->private_data = eptdev;
+	mutex_unlock(&eptdev->ept_lock);
 
 	return 0;
 }
Signature · 1 line
2.17.1