[PATCH] char: pc8736x_gpio: check cdev_add() return value

Linkai Gong posted 1 patch 1 month, 2 weeks ago
drivers/char/pc8736x_gpio.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
[PATCH] char: pc8736x_gpio: check cdev_add() return value
Posted by Linkai Gong 1 month, 2 weeks ago
cdev_add() can fail, but the driver ignored the return value and still
reported success. That leaves a registered chrdev region without usable
file operations.

Check the return value, report the error, and unwind the chrdev region
and I/O region on failure.

Fixes: babcfade4737 ("[PATCH] pc8736x_gpio: fix re-modprobe errors: fix/finish cdev-init")
Signed-off-by: Linkai Gong <gonglinkai@kylinos.cn>
---
 drivers/char/pc8736x_gpio.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/char/pc8736x_gpio.c b/drivers/char/pc8736x_gpio.c
index 5f4696813cea..1a8adf1e8fa2 100644
--- a/drivers/char/pc8736x_gpio.c
+++ b/drivers/char/pc8736x_gpio.c
@@ -321,12 +321,17 @@ static int __init pc8736x_gpio_init(void)
 
 	pc8736x_init_shadow();
 
-	/* ignore minor errs, and succeed */
 	cdev_init(&pc8736x_gpio_cdev, &pc8736x_gpio_fileops);
-	cdev_add(&pc8736x_gpio_cdev, devid, PC8736X_GPIO_CT);
+	rc = cdev_add(&pc8736x_gpio_cdev, devid, PC8736X_GPIO_CT);
+	if (rc) {
+		dev_err(&pdev->dev, "cdev_add failed: %d\n", rc);
+		goto undo_chrdev_region;
+	}
 
 	return 0;
 
+undo_chrdev_region:
+	unregister_chrdev_region(devid, PC8736X_GPIO_CT);
 undo_request_region:
 	release_region(pc8736x_gpio_base, PC8736X_GPIO_RANGE);
 undo_platform_dev_add:
-- 
2.25.1