Re: [PATCH] kobject: make sure kobj->ktype is set before kobject_init

Alan Stern
2007-11-29T17:07:01Z

On Thu, 29 Nov 2007, Kay Sievers wrote:

> > In fact, if we were designing the kobject API from scratch, I'd suggest
> > making the ktype value an argument to kobject_init() so that it
> > _couldn't_ be omitted.
>
> Sounds fine, maybe we should also pass the name along, so it will be
> obvious what happens here:
> int kobject_init(struct kobject *kobj, struct kobj_type *type, const char *fmt, ...)

I don't know... Normally *_init() routines can't fail, but this could.
Then things like device_register() would run into trouble: The caller
wouldn't know whether a failure occurred before or after the
kobject_init() call, so it wouldn't know what sort of cleanup action
was needed: kfree() or device_put().

> Oh, if you want to rewind on error and have an initialized but still
> unregistered kobject, and just want to free the allocated name by
> calling kobject_cleanup() or kobject_put() you might not expect, that
> your whole object that embeds the kobject will be gone. Just something
> we need to document ...

When that sort of thing happens, the unwinding should be done by the
code responsible for whole object. For example, if device_add() fails
then the caller should go on to call device_put() rather than
kfree(dev).

That's how you would expect things to work in most cases. There aren't
many bare kobjects in the kernel.

I agree that documenting this behavior would be good.

Alan Stern