Re: [RFC] New kobject/kset/ktype documentation and example code

Cornelia Huck
2007-11-29T10:13:06Z

On Wed, 28 Nov 2007 14:18:18 -0500 (EST),
Alan Stern <stern@rowland.harvard.edu> wrote:

> On Wed, 28 Nov 2007, Cornelia Huck wrote:
>
> > We should perhaps add a bit fat warning here:
> >
> > Note that once you registered your kobject via kobject_add(), you must
> > never use kfree() to free it directly. The only safe way is to use
>
> Slightly ambiguous. Instead just say:
>
> If you have initialized your kobject via kobject_init() or
> kobject_register(), you must not deallocate the kobject anywhere other
> than its release() method (which is invoked during the final
> kobject_put() call). Otherwise the kernel will leak memory.

And references to the kobject's kset...

>
> > > One important point cannot be overstated: every kobject must have a
> > > release() method, and the kobject must persist (in a consistent state)
> > > until that method is called.
> >
> > Which is especially hurting if you use kobjects in modules. (Which
> > reminds me: Must dig up the patchset that fixes the module unload vs.
> > release problem.)
>
> In theory modules shouldn't present a problem -- especially if Greg
> merges the "Kobjects: drop child->parent ref at unregistration" patch.
>
> When a module is unloaded, it has to unregister all its kobjects, which
> should force all their children to be unregistered too. At that time
> the children's drivers should drop all their references to the parent
> kobject, leaving only references held by the module being unloaded.
> Presumably it can arrange to drop its own references before its exit()
> routine returns.
>
> The only problem arises when a child's driver retains a reference to
> the parent kobject. If things are done properly, this reference should
> involve incrementing the module count -- which would prevent the module
> from being unloaded in the first place.

This still leaves the possibility that random code may grab a reference
once the kobject is present in the tree and lookupable. You gave up the
control of the number of references to your object once you made it
public.