* [PATCH] char: scx200_gpio: check cdev_add() return value
@ 2026-07-30 9:53 Linkai Gong
2026-07-31 12:22 ` Greg Kroah-Hartman
0 siblings, 1 reply; 2+ messages in thread
From: Linkai Gong @ 2026-07-30 9:53 UTC (permalink / raw)
To: Jim Cromie, Arnd Bergmann, Greg Kroah-Hartman
Cc: Andrew Morton, linux-kernel, Linkai Gong
cdev_add() can fail. The driver currently ignores the return value and
reports probe success even when the cdev was not registered, leaving a
chrdev region allocated with no working file operations.
Check the return value, emit an error, and unwind the chrdev region and
platform device on failure.
Fixes: 635adb6cd25c ("[PATCH] scx200_gpio: use 1 cdev for N minors, not N for N")
Signed-off-by: Linkai Gong <gonglinkai@kylinos.cn>
---
drivers/char/scx200_gpio.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/char/scx200_gpio.c b/drivers/char/scx200_gpio.c
index 700e6affea6f..d9d66d4c2312 100644
--- a/drivers/char/scx200_gpio.c
+++ b/drivers/char/scx200_gpio.c
@@ -107,10 +107,16 @@ static int __init scx200_gpio_init(void)
}
cdev_init(&scx200_gpio_cdev, &scx200_gpio_fileops);
- cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
+ rc = cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
+ if (rc < 0) {
+ dev_err(&pdev->dev, "cdev_add err: %d\n", rc);
+ goto undo_chrdev_region;
+ }
return 0; /* succeed */
+undo_chrdev_region:
+ unregister_chrdev_region(devid, MAX_PINS);
undo_platform_device_add:
platform_device_del(pdev);
undo_malloc:
--
2.25.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] char: scx200_gpio: check cdev_add() return value
2026-07-30 9:53 [PATCH] char: scx200_gpio: check cdev_add() return value Linkai Gong
@ 2026-07-31 12:22 ` Greg Kroah-Hartman
0 siblings, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2026-07-31 12:22 UTC (permalink / raw)
To: Linkai Gong; +Cc: Jim Cromie, Arnd Bergmann, Andrew Morton, linux-kernel
On Thu, Jul 30, 2026 at 05:53:21PM +0800, Linkai Gong wrote:
> cdev_add() can fail. The driver currently ignores the return value and
> reports probe success even when the cdev was not registered, leaving a
> chrdev region allocated with no working file operations.
>
> Check the return value, emit an error, and unwind the chrdev region and
> platform device on failure.
>
> Fixes: 635adb6cd25c ("[PATCH] scx200_gpio: use 1 cdev for N minors, not N for N")
> Signed-off-by: Linkai Gong <gonglinkai@kylinos.cn>
> ---
> drivers/char/scx200_gpio.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/char/scx200_gpio.c b/drivers/char/scx200_gpio.c
> index 700e6affea6f..d9d66d4c2312 100644
> --- a/drivers/char/scx200_gpio.c
> +++ b/drivers/char/scx200_gpio.c
> @@ -107,10 +107,16 @@ static int __init scx200_gpio_init(void)
> }
>
> cdev_init(&scx200_gpio_cdev, &scx200_gpio_fileops);
> - cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
> + rc = cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
> + if (rc < 0) {
Should just be:
if (rc) {
right?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-31 12:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-30 9:53 [PATCH] char: scx200_gpio: check cdev_add() return value Linkai Gong
2026-07-31 12:22 ` Greg Kroah-Hartman
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox