Message ID | 20220110091228.v5.2.Idde68b05b88d4a2e6e54766c653f3a6d9e419ce6@changeid (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | [v5,1/2] rpmsg: char: Fix race between the release of rpmsg_ctrldev and cdev | expand |
On Mon, Jan 10, 2022 at 09:12:32AM -0800, Matthias Kaehlcke wrote: > struct rpmsg_eptdev contains a struct cdev. The current code frees > the rpmsg_eptdev struct in rpmsg_eptdev_destroy(), but the cdev is > a managed object, therefore its release is not predictable and the > rpmsg_eptdev could be freed before the cdev is entirely released. > > The cdev_device_add/del() API was created to address this issue > (see commit 233ed09d7fda), use it instead of cdev add/del(). > > Fixes: c0cdc19f84a4 ("rpmsg: Driver for user space endpoint interface") > Signed-off-by: Matthias Kaehlcke <mka@chromium.org> > --- > > Changes in v5: > - patch added to the series > > drivers/rpmsg/rpmsg_char.c | 10 ++-------- > 1 file changed, 2 insertions(+), 8 deletions(-) > > diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c > index ba85f5d11960..5bc1e6017587 100644 > --- a/drivers/rpmsg/rpmsg_char.c > +++ b/drivers/rpmsg/rpmsg_char.c > @@ -92,7 +92,7 @@ static int rpmsg_eptdev_destroy(struct device *dev, void *data) > /* wake up any blocked readers */ > wake_up_interruptible(&eptdev->readq); > > - device_del(&eptdev->dev); > + cdev_device_del(&eptdev->cdev, &eptdev->dev); > put_device(&eptdev->dev); > > return 0; > @@ -380,19 +380,13 @@ static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, > dev->id = ret; > dev_set_name(dev, "rpmsg%d", ret); > > - ret = cdev_add(&eptdev->cdev, dev->devt, 1); > + ret = cdev_device_add(&eptdev->cdev, &eptdev->dev); > if (ret) > goto free_ept_ida; > > /* We can now rely on the release function for cleanup */ > dev->release = rpmsg_eptdev_release_device; > > - ret = device_add(dev); > - if (ret) { > - dev_err(dev, "device_add failed: %d\n", ret); > - put_device(dev); > - } > - > return ret; > Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org> > free_ept_ida: > -- > 2.34.1.575.g55b058a8bb-goog >
On Mon 10 Jan 09:12 PST 2022, Matthias Kaehlcke wrote: > struct rpmsg_eptdev contains a struct cdev. The current code frees > the rpmsg_eptdev struct in rpmsg_eptdev_destroy(), but the cdev is > a managed object, therefore its release is not predictable and the > rpmsg_eptdev could be freed before the cdev is entirely released. > > The cdev_device_add/del() API was created to address this issue > (see commit 233ed09d7fda), use it instead of cdev add/del(). > > Fixes: c0cdc19f84a4 ("rpmsg: Driver for user space endpoint interface") > Signed-off-by: Matthias Kaehlcke <mka@chromium.org> > --- > > Changes in v5: > - patch added to the series > > drivers/rpmsg/rpmsg_char.c | 10 ++-------- > 1 file changed, 2 insertions(+), 8 deletions(-) > > diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c > index ba85f5d11960..5bc1e6017587 100644 > --- a/drivers/rpmsg/rpmsg_char.c > +++ b/drivers/rpmsg/rpmsg_char.c There's a cdev_del() in rpmsg_eptdev_release_device() that I think needs to go as well, in line with patch 1. Regards, Bjorn > @@ -92,7 +92,7 @@ static int rpmsg_eptdev_destroy(struct device *dev, void *data) > /* wake up any blocked readers */ > wake_up_interruptible(&eptdev->readq); > > - device_del(&eptdev->dev); > + cdev_device_del(&eptdev->cdev, &eptdev->dev); > put_device(&eptdev->dev); > > return 0; > @@ -380,19 +380,13 @@ static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, > dev->id = ret; > dev_set_name(dev, "rpmsg%d", ret); > > - ret = cdev_add(&eptdev->cdev, dev->devt, 1); > + ret = cdev_device_add(&eptdev->cdev, &eptdev->dev); > if (ret) > goto free_ept_ida; > > /* We can now rely on the release function for cleanup */ > dev->release = rpmsg_eptdev_release_device; > > - ret = device_add(dev); > - if (ret) { > - dev_err(dev, "device_add failed: %d\n", ret); > - put_device(dev); > - } > - > return ret; > > free_ept_ida: > -- > 2.34.1.575.g55b058a8bb-goog >
On Mon, Jan 10, 2022 at 10:19:32AM -0800, Bjorn Andersson wrote: > On Mon 10 Jan 09:12 PST 2022, Matthias Kaehlcke wrote: > > > struct rpmsg_eptdev contains a struct cdev. The current code frees > > the rpmsg_eptdev struct in rpmsg_eptdev_destroy(), but the cdev is > > a managed object, therefore its release is not predictable and the > > rpmsg_eptdev could be freed before the cdev is entirely released. > > > > The cdev_device_add/del() API was created to address this issue > > (see commit 233ed09d7fda), use it instead of cdev add/del(). > > > > Fixes: c0cdc19f84a4 ("rpmsg: Driver for user space endpoint interface") > > Signed-off-by: Matthias Kaehlcke <mka@chromium.org> > > --- > > > > Changes in v5: > > - patch added to the series > > > > drivers/rpmsg/rpmsg_char.c | 10 ++-------- > > 1 file changed, 2 insertions(+), 8 deletions(-) > > > > diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c > > index ba85f5d11960..5bc1e6017587 100644 > > --- a/drivers/rpmsg/rpmsg_char.c > > +++ b/drivers/rpmsg/rpmsg_char.c > > There's a cdev_del() in rpmsg_eptdev_release_device() that I think needs > to go as well, in line with patch 1. Good catch, thanks, will fix.
diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c index ba85f5d11960..5bc1e6017587 100644 --- a/drivers/rpmsg/rpmsg_char.c +++ b/drivers/rpmsg/rpmsg_char.c @@ -92,7 +92,7 @@ static int rpmsg_eptdev_destroy(struct device *dev, void *data) /* wake up any blocked readers */ wake_up_interruptible(&eptdev->readq); - device_del(&eptdev->dev); + cdev_device_del(&eptdev->cdev, &eptdev->dev); put_device(&eptdev->dev); return 0; @@ -380,19 +380,13 @@ static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev, dev->id = ret; dev_set_name(dev, "rpmsg%d", ret); - ret = cdev_add(&eptdev->cdev, dev->devt, 1); + ret = cdev_device_add(&eptdev->cdev, &eptdev->dev); if (ret) goto free_ept_ida; /* We can now rely on the release function for cleanup */ dev->release = rpmsg_eptdev_release_device; - ret = device_add(dev); - if (ret) { - dev_err(dev, "device_add failed: %d\n", ret); - put_device(dev); - } - return ret; free_ept_ida:
struct rpmsg_eptdev contains a struct cdev. The current code frees the rpmsg_eptdev struct in rpmsg_eptdev_destroy(), but the cdev is a managed object, therefore its release is not predictable and the rpmsg_eptdev could be freed before the cdev is entirely released. The cdev_device_add/del() API was created to address this issue (see commit 233ed09d7fda), use it instead of cdev add/del(). Fixes: c0cdc19f84a4 ("rpmsg: Driver for user space endpoint interface") Signed-off-by: Matthias Kaehlcke <mka@chromium.org> --- Changes in v5: - patch added to the series drivers/rpmsg/rpmsg_char.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)