Skip to content

Commit 7a534ae

Browse files
Matthias Kaehlckeandersson
authored andcommitted
rpmsg: char: Fix race between the release of rpmsg_eptdev and cdev
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 ("chardev: add helper function to register char devs with a struct device")'), use it instead of cdev add/del(). Fixes: c0cdc19 ("rpmsg: Driver for user space endpoint interface") Suggested-by: Bjorn Andersson <[email protected]> Signed-off-by: Matthias Kaehlcke <[email protected]> Reviewed-by: Mathieu Poirier <[email protected]> Reviewed-by: Stephen Boyd <[email protected]> Reviewed-by: Bjorn Andersson <[email protected]> Signed-off-by: Bjorn Andersson <[email protected]> Link: https://lore.kernel.org/r/20220110104706.v6.2.Idde68b05b88d4a2e6e54766c653f3a6d9e419ce6@changeid
1 parent b7fb2da commit 7a534ae

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

drivers/rpmsg/rpmsg_char.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static int rpmsg_eptdev_destroy(struct device *dev, void *data)
9393
/* wake up any blocked readers */
9494
wake_up_interruptible(&eptdev->readq);
9595

96-
device_del(&eptdev->dev);
96+
cdev_device_del(&eptdev->cdev, &eptdev->dev);
9797
put_device(&eptdev->dev);
9898

9999
return 0;
@@ -336,7 +336,6 @@ static void rpmsg_eptdev_release_device(struct device *dev)
336336

337337
ida_simple_remove(&rpmsg_ept_ida, dev->id);
338338
ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt));
339-
cdev_del(&eptdev->cdev);
340339
kfree(eptdev);
341340
}
342341

@@ -381,19 +380,13 @@ static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev,
381380
dev->id = ret;
382381
dev_set_name(dev, "rpmsg%d", ret);
383382

384-
ret = cdev_add(&eptdev->cdev, dev->devt, 1);
383+
ret = cdev_device_add(&eptdev->cdev, &eptdev->dev);
385384
if (ret)
386385
goto free_ept_ida;
387386

388387
/* We can now rely on the release function for cleanup */
389388
dev->release = rpmsg_eptdev_release_device;
390389

391-
ret = device_add(dev);
392-
if (ret) {
393-
dev_err(dev, "device_add failed: %d\n", ret);
394-
put_device(dev);
395-
}
396-
397390
return ret;
398391

399392
free_ept_ida:

0 commit comments

Comments
 (0)