rpmsg: char: Add mutex protection for rpmsg_eptdev_open()
authorShengjiu Wang <shengjiu.wang@nxp.com>
Sat, 21 May 2022 03:35:05 +0000 (11:35 +0800)
committerMathieu Poirier <mathieu.poirier@linaro.org>
Tue, 14 Jun 2022 20:58:10 +0000 (14:58 -0600)
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 EBUSY.

Fixes: 964e8bedd5a1 ("rpmsg: char: Return an error if device already open")
Signed-off-by: Shengjiu Wang <shengjiu.wang@nxp.com>
Link: https://lore.kernel.org/r/1653104105-16779-1-git-send-email-shengjiu.wang@nxp.com
Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>
drivers/rpmsg/rpmsg_char.c

index b6183d4f62a22e61102834cda88253b25723b267..4f2189111494a4f8f8913a83db2a1a1cd3f1de80 100644 (file)
@@ -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;
 }