public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg KH <greg@kroah.com>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Kernel development list <linux-kernel@vger.kernel.org>,
	Kay Sievers <kay.sievers@vrfy.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Randy Dunlap <randy.dunlap@oracle.com>,
	Cornelia Huck <cornelia.huck@de.ibm.com>
Subject: Re: [RFC] New kobject/kset/ktype documentation and example code
Date: Wed, 28 Nov 2007 22:18:14 -0800	[thread overview]
Message-ID: <20071129061814.GJ26327@kroah.com> (raw)
In-Reply-To: <Pine.LNX.4.44L0.0711281359440.5566-100000@iolanthe.rowland.org>

On Wed, Nov 28, 2007 at 02:03:28PM -0500, Alan Stern wrote:
> On Tue, 27 Nov 2007, Greg KH wrote:
> 
> > Part of the difficulty in understanding the driver model - and the kobject
> > abstraction upon which it is built - is that there is no obvious starting
> > place. Dealing with kobjects requires understanding a few different types,
> > all of which make reference to each other. In an attempt to make things
> > easier, we'll take a multi-pass approach, starting with vague terms and
> > adding detail as we go. To that end, here are some quick definitions of
> > some terms we will be working with.
> > 
> >  - A kobject is an object of type struct kobject.  Kobjects have a name
> >    and a reference count.  A kobject also has a parent pointer (allowing
> >    objects to be arranged into hierarchies), a specific type, and,
> >    usually, a representation in the sysfs virtual filesystem.
> 
> As Cornelia said, it would be worthwhile mentioning krefs in this
> document as well.  They are simple enough to explain, after all.

Now added, thanks.

> > Initialization of kobjects
> > 
> > Code which creates a kobject must, of course, initialize that object. Some
> > of the internal fields are setup with a (mandatory) call to kobject_init():
> 
> kobject_init() isn't mandatory if you use kobject_register().  But then 
> Kay wants to do away with kobject_register()...
> 
> > The other kobject fields which should be set, directly or indirectly, by
> > the creator are its ktype, kset, and parent. We will get to those shortly,
> > however please note that the ktype and kset must be set before the
> > kobject_init() function is called.
> 
> In fact kset, ktype, and parent are optional, right?  You might mention
> at this point that not all those fields are needed, and explain later
> which combinations are legal.

They are optional, but if you want to do anything, you need to set them :)

> > When a reference is released, the call to kobject_put() will decrement the
> > reference count and, possibly, free the object. Note that kobject_init()
> > sets the reference count to one, so the code which sets up the kobject will
> > need to do a kobject_put() eventually to release that reference.
> 
> It's worth mentioning here (and perhaps elsewhere too) that all of the
> function calls described here can sleep and hence must be made in
> process context, with the exception of the *_get() routines.  It's
> possible to call *_put() in atomic context; the SCSI core does this
> (with device_put, not kobject_put) and has to jump through hoops to run
> the corresponding release routine in a waitqueue task.  In general,
> though, it isn't safe.

Is this really needed?  If anyone calls them from non-process context,
they will get a nasty run-time warning, right?

> > Because kobjects are dynamic, they must not be declared statically or on
> > the stack, but instead, always from the heap.  Future versions of the
> > kernel will contain a run-time check for kobjects that are created
> > statically and will warn the developer of this improper usage.
> 
> Why not?  What's wrong with static kobjects?  I've never understood this.

They are reference counted.  Other portions of the kernel can grab them
and think they are safe to use.  If you do this with a static object,
what happens when the code goes away?

Most of the nasty race conditions that require this are now cleaned up
with Tejun's great sysfs work, so you will probably not see problems if
you do this, but in general, it's not a good thing to do.

> > ktypes and release methods
> > 
> > One important thing still missing from the discussion is what happens to a
> > kobject when its reference count reaches zero. The code which created the
> > kobject generally does not know when that will happen; if it did, there
> > would be little point in using a kobject in the first place. Even
> > predicatable object lifecycles become more complicated when sysfs is
> 
> predictable

thanks.

> > 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. If these constraints are not met, the code is
> > flawed.  Note that the kernel will warn you if you forget to provide a
> > release() method.  Do not try to get rid of this warning by providing an
> > "empty" release function, you will be mocked merciously by the kobject
> > maintainer if you attempt this.
> 
> Not to mention that doing this will leak memory.  Unless the kobject
> is static...

heh.

I think your other questions are already answered, right?

thanks,

greg k-h

  parent reply	other threads:[~2007-11-29  6:16 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-27 23:02 [RFC] New kobject/kset/ktype documentation and example code Greg KH
2007-11-27 23:03 ` [RFC] sample kobject implementation Greg KH
2007-11-27 23:04 ` [RFC] Sample kset/ktype/kobject implementation Greg KH
2007-11-28 16:35   ` Cornelia Huck
2007-11-29  6:11     ` Greg KH
2007-11-29  9:39       ` Cornelia Huck
2007-11-29 20:39         ` Greg KH
2007-11-29 22:11           ` Alan Stern
2007-11-30  5:07             ` Dave Young
2007-11-30  5:57               ` Dave Young
2007-11-30 14:51               ` Alan Stern
2007-11-30  6:41             ` Greg KH
2007-11-27 23:10 ` [RFC] New kobject/kset/ktype documentation and example code Kyle McMartin
2007-11-27 23:29   ` Greg KH
2007-11-27 23:21 ` Frans Pop
2007-11-28  3:50 ` Jonathan Corbet
2007-11-29  5:46   ` Greg KH
2007-11-28  9:01 ` Cornelia Huck
2007-11-28 12:35   ` Kay Sievers
2007-11-28 15:52     ` Cornelia Huck
2007-11-28 16:03       ` Kay Sievers
2007-11-28 16:09         ` Cornelia Huck
2007-11-28 17:06           ` Greg KH
2007-11-28 19:18   ` Alan Stern
2007-11-29 10:12     ` Cornelia Huck
2007-11-29 15:47       ` Alan Stern
2007-11-29 16:28         ` Cornelia Huck
2007-11-29 16:55           ` Alan Stern
2007-11-29 17:52             ` Cornelia Huck
2007-11-29  5:59   ` Greg KH
2007-11-28 11:45 ` Cornelia Huck
2007-11-28 12:23   ` Kay Sievers
2007-11-28 15:48     ` Cornelia Huck
2007-11-28 15:57       ` Kay Sievers
2007-11-28 16:12         ` Cornelia Huck
2007-11-28 16:36           ` Kay Sievers
2007-11-28 16:51             ` Cornelia Huck
2007-11-28 17:00               ` Kay Sievers
2007-11-29  6:08                 ` Greg KH
2007-11-29  7:50                   ` Kay Sievers
2007-11-29  9:35                     ` Cornelia Huck
2007-11-29 10:53                       ` Kay Sievers
2007-11-29  6:02     ` Greg KH
2007-11-29  6:04   ` Greg KH
2007-11-29  9:41     ` Cornelia Huck
2007-11-28 19:03 ` Alan Stern
2007-11-28 19:28   ` Kay Sievers
2007-11-28 19:36     ` Alan Stern
2007-11-28 19:46       ` Kay Sievers
2007-11-28 20:42         ` [PATCH] kobject: make sure kobj->ktype is set before kobject_init Alan Stern
2007-11-28 20:52           ` Kay Sievers
2007-11-28 21:45           ` Greg KH
2007-11-28 22:00             ` Alan Stern
2007-11-28 22:38               ` Greg KH
2007-11-29 10:05               ` Cornelia Huck
2007-11-29 10:59                 ` Kay Sievers
2007-11-29 11:48                   ` Cornelia Huck
2007-11-29 15:54                   ` Alan Stern
2007-11-29 16:04                     ` Kay Sievers
2007-11-29 16:21                       ` Cornelia Huck
2007-11-29 21:53                         ` kobject_init rewrite Greg KH
2007-11-29 21:54                           ` Greg KH
2007-11-30  9:31                             ` Cornelia Huck
2007-11-29 22:16                           ` Alan Stern
2007-11-29 22:24                             ` Greg KH
2007-11-29 17:06                       ` [PATCH] kobject: make sure kobj->ktype is set before kobject_init Alan Stern
2007-11-29 17:17                         ` Kay Sievers
2007-11-29 18:04                           ` Alan Stern
2007-11-29 18:33                             ` Kay Sievers
2007-11-29 19:05                               ` Alan Stern
2007-11-29 19:51                                 ` Kay Sievers
2007-11-29 20:09                                   ` Alan Stern
2007-11-29 20:19                                     ` Kay Sievers
2007-11-29 20:26                                       ` Kay Sievers
2007-11-30  9:30                                         ` Cornelia Huck
2007-11-29  6:18   ` Greg KH [this message]
2007-11-29 15:42     ` [RFC] New kobject/kset/ktype documentation and example code Alan Stern

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=20071129061814.GJ26327@kroah.com \
    --to=greg@kroah.com \
    --cc=corbet@lwn.net \
    --cc=cornelia.huck@de.ibm.com \
    --cc=kay.sievers@vrfy.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=randy.dunlap@oracle.com \
    --cc=stern@rowland.harvard.edu \
    /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