Proper error-handling of cdev_add() after cdev_alloc() ?
Hello,
How should errors of cdev_add() after cdev_alloc() be handled?
The basic schema seems to be like this:
cdev = cdev_alloc();
if (!cdev)
return -ENOMEM;
...
ret = cdev_add(cdev, ...);
if (ret < 0) {
/* WHAT HERE ??? */
return ret;
}
...
return 0; // successI found 5 case of how errors are handled:
1. kfree(cdev); drivers/media/v4l2-core/v4l2-dev.c:1060
2. kobject_put(&cdev->kobj); drivers/char/xillybus/xillybus_class.c:107 fs/char_dev.c:293 drivers/s390/char/vmlogrdr.c:780 drivers/tty/tty_io.c:3171 drivers/uio/uio.c:925
3. cdev_del(cdev); drivers/char/virtio_console.c:1435 fs/fuse/cuse.c:419 drivers/misc/mei/main.c:1318 drivers/s390/char/tape_class.c:92 drivers/s390/char/vmur.c:928 drivers/scsi/sg.c:1560 drivers/scsi/st.c:4307 drivers/staging/vme_user/vme_user.c:694
4. No error handling at all.
arch/sh/boards/mach-landisk/gio.c:145
5. panic()
Currently I'm unsure of what's the correct way.
My current understanding is, that cdev_del() should only be called if cdev_add() succeeded, so those 8 findings above should be fixed. But to what?
Thank you in advance
Philipp
Hello,
Quoted 1 line
How should errors of
cdev_add()aftercdev_alloc()be handled?
sorry for ping-ing you directly, but so far got no answer and I would like to get a definitive answer on this before I spend more time on this to create and send patches. Thank you.
Am Tue, May 12, 2026 at 10:20:13AM +0200 schrieb Philipp Matthias Hahn:
Quoted 37 lines
The basic schema seems to be like this:
cdev = cdev_alloc(); if (!cdev) return -ENOMEM; ... ret = cdev_add(cdev, ...); if (ret < 0) { /* WHAT HERE ??? */ return ret; } ... return 0; // successI found 5 case of how errors are handled:
1. kfree(cdev); drivers/media/v4l2-core/v4l2-dev.c:10602. kobject_put(&cdev->kobj); drivers/char/xillybus/xillybus_class.c:107 fs/char_dev.c:293 drivers/s390/char/vmlogrdr.c:780 drivers/tty/tty_io.c:3171 drivers/uio/uio.c:9253. cdev_del(cdev); drivers/char/virtio_console.c:1435 fs/fuse/cuse.c:419 drivers/misc/mei/main.c:1318 drivers/s390/char/tape_class.c:92 drivers/s390/char/vmur.c:928 drivers/scsi/sg.c:1560 drivers/scsi/st.c:4307 drivers/staging/vme_user/vme_user.c:6944. No error handling at all.
Quoted 8 lines
arch/sh/boards/mach-landisk/gio.c:1455.
panic()Currently I'm unsure of what's the correct way.
My current understanding is, thatcdev_del()should only be called ifcdev_add()succeeded, so those findings above should be fixed. But to what?
I guess __register_chrdev() from fs/char_dev.c:293 is correct and
kobject_put(&cdev->kobj);
should be used.
Thank you in advance
Philipp
Jan Kara
reviewerHello!
Quoted 5 lines · Philipp Matthias Hahn
Quoted 1 line
How should errors of
cdev_add()aftercdev_alloc()be handled?sorry for ping-ing you directly, but so far got no answer and I would like to get a definitive answer on this before I spend more time on this to create and send patches. Thank you.
So I don't really feel like chrdev specialist but based on reading the code:
Quoted 42 lines
Am Tue, May 12, 2026 at 10:20:13AM +0200 schrieb Philipp Matthias Hahn:
Quoted 41 lines
The basic schema seems to be like this:
cdev = cdev_alloc(); if (!cdev) return -ENOMEM; ... ret = cdev_add(cdev, ...); if (ret < 0) { /* WHAT HERE ??? */ return ret; } ... return 0; // successI found 5 case of how errors are handled:
1. kfree(cdev); drivers/media/v4l2-core/v4l2-dev.c:10602. kobject_put(&cdev->kobj); drivers/char/xillybus/xillybus_class.c:107 fs/char_dev.c:293 drivers/s390/char/vmlogrdr.c:780 drivers/tty/tty_io.c:3171 drivers/uio/uio.c:9253. cdev_del(cdev); drivers/char/virtio_console.c:1435 fs/fuse/cuse.c:419 drivers/misc/mei/main.c:1318 drivers/s390/char/tape_class.c:92 drivers/s390/char/vmur.c:928 drivers/scsi/sg.c:1560 drivers/scsi/st.c:4307 drivers/staging/vme_user/vme_user.c:6944. No error handling at all.
arch/sh/boards/mach-landisk/gio.c:1455.
panic()
Quoted 8 lines
Quoted 4 lines
Currently I'm unsure of what's the correct way.
My current understanding is, thatcdev_del()should only be called ifcdev_add()succeeded, so those findings above should be fixed. But to what?I guess
__register_chrdev()fromfs/char_dev.c:293is correct andkobject_put(&cdev->kobj);should be used.
This indeed looks like proper handling of the failure.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerQuoted 59 lines · Jan Kara
Hello!
Quoted 5 lines · Philipp Matthias Hahn
Quoted 1 line
How should errors of
cdev_add()aftercdev_alloc()be handled?sorry for ping-ing you directly, but so far got no answer and I would like to get a definitive answer on this before I spend more time on this to create and send patches. Thank you.
So I don't really feel like chrdev specialist but based on reading the code:
Quoted 47 lines
Am Tue, May 12, 2026 at 10:20:13AM +0200 schrieb Philipp Matthias Hahn:
Quoted 45 lines
The basic schema seems to be like this:
cdev = cdev_alloc(); if (!cdev) return -ENOMEM; ... ret = cdev_add(cdev, ...); if (ret < 0) { /* WHAT HERE ??? */ return ret; } ... return 0; // successI found 5 case of how errors are handled:
1. kfree(cdev); drivers/media/v4l2-core/v4l2-dev.c:10602. kobject_put(&cdev->kobj); drivers/char/xillybus/xillybus_class.c:107 fs/char_dev.c:293 drivers/s390/char/vmlogrdr.c:780 drivers/tty/tty_io.c:3171 drivers/uio/uio.c:9253. cdev_del(cdev); drivers/char/virtio_console.c:1435 fs/fuse/cuse.c:419 drivers/misc/mei/main.c:1318 drivers/s390/char/tape_class.c:92 drivers/s390/char/vmur.c:928 drivers/scsi/sg.c:1560 drivers/scsi/st.c:4307 drivers/staging/vme_user/vme_user.c:6944. No error handling at all.
arch/sh/boards/mach-landisk/gio.c:1455.
panic()Currently I'm unsure of what's the correct way.
My current understanding is, thatcdev_del()should only be called ifcdev_add()succeeded, so those findings above should be fixed. But to what?
Quoted 5 lines
Quoted 3 lines
I guess
__register_chrdev()fromfs/char_dev.c:293is correct andkobject_put(&cdev->kobj);should be used.
This indeed looks like proper handling of the failure.
Yes, sorry, I forgot to reply to this mail... Thanks, Jan!