mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Jan Kara <jack@suse.cz>
Cc: Philipp Matthias Hahn <pmhahn@pmhahn.de>,
	 Alexander Viro <viro@zeniv.linux.org.uk>,
	linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org
Subject: Re: Proper error-handling of cdev_add() after cdev_alloc() ?
Date: Tue, 2 Jun 2026 15:21:30 +0200	[thread overview]
Message-ID: <20260602-gleiten-tontechnik-kalender-d506d306d6ea@brauner> (raw)
In-Reply-To: <f64qxrwhhfrxxcp4z7kaeudoz5x7irzu5tjhcu2qarzdhtf6e3@g4g3ytghcyoe>

On Sat, May 30, 2026 at 01:15:37PM +0200, Jan Kara wrote:
> Hello!
> 
> On Wed 27-05-26 16:01:31, Philipp Matthias Hahn wrote:
> > > How should errors of `cdev_add()` after `cdev_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:
> 
> > Am Tue, May 12, 2026 at 10:20:13AM +0200 schrieb Philipp Matthias Hahn:
> > > 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;  // success
> > > 
> > > I 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 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.
> 
> This indeed looks like proper handling of the failure.

Yes, sorry, I forgot to reply to this mail... Thanks, Jan!

      reply	other threads:[~2026-06-02 13:21 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-12  8:20 Philipp Matthias Hahn
2026-05-27 14:01 ` Philipp Matthias Hahn
2026-05-30 11:15   ` Jan Kara
2026-06-02 13:21     ` Christian Brauner [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260602-gleiten-tontechnik-kalender-d506d306d6ea@brauner \
    --to=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pmhahn@pmhahn.de \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®