If dev->devt is not set, cdev_add() will not be called, so if device_add() fails, cdev_del() is not needed. Fix this by checking dev->devt in error case. Fixes: 233ed09d7fda ("chardev: add helper function to register char devs with a struct device") Reported-by: Hulk Robot <hulkci@xxxxxxxxxx> Signed-off-by: Yang Yingliang <yangyingliang@xxxxxxxxxx> --- fs/char_dev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/char_dev.c b/fs/char_dev.c index ba0ded7842a7..3f667292608c 100644 --- a/fs/char_dev.c +++ b/fs/char_dev.c @@ -547,7 +547,7 @@ int cdev_device_add(struct cdev *cdev, struct device *dev) } rc = device_add(dev); - if (rc) + if (rc && dev->devt) cdev_del(cdev); return rc; -- 2.25.1