public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg KH <greg@kroah.com>
To: "Artem B. Bityutskiy" <dedekind@infradead.org>
Cc: linux-kernel <linux-kernel@vger.kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>
Subject: Re: [Bug? Report] kref problem
Date: Thu, 16 Mar 2006 08:53:23 -0800	[thread overview]
Message-ID: <20060316165323.GA10197@kroah.com> (raw)
In-Reply-To: <1142509279.3920.31.camel@sauron.oktetlabs.ru>

On Thu, Mar 16, 2006 at 02:41:19PM +0300, Artem B. Bityutskiy wrote:
> struct my_obj_a
> {
> 	struct kobject kobj;
> } a;
> 
> struct my_obj_b
> {
> 	struct kobject kobj;
> } b;

Don't statically create kobjects, it's not nice.  But the real problem
is below...

> static __init int test_init(void)
> {
> 	int err;
> 
> 	kobject_init(&a.kobj);
> 	a.kobj.ktype = &a_ktype;
> 	err = kobject_set_name(&a.kobj, "A");
> 	if (err)
> 		return err;
> 	printk("a inited, kref %d\n", atomic_read(&a.kobj.kref.refcount));
> 	
> 	kobject_init(&b.kobj);
> 	b.kobj.ktype = &b_ktype;
> 	b.kobj.parent = &a.kobj;
> 	err = kobject_set_name(&b.kobj, "B");
> 	if (err)
> 		goto out_a;
> 	printk("b inited, kref %d\n", atomic_read(&b.kobj.kref.refcount));
> 
> 	err = sysfs_create_dir(&a.kobj);
> 	if (err)
> 		goto out_b;
> 	printk("dir A created, A kref %d, B kref %d\n",
> 	       atomic_read(&a.kobj.kref.refcount), atomic_read(&b.kobj.kref.refcount));
> 	
> 	err = sysfs_create_dir(&b.kobj);
> 	if (err)
> 		goto out_a_dir;
> 	printk("dir B created, A kref %d, B kref %d\n",
> 	       atomic_read(&a.kobj.kref.refcount), atomic_read(&b.kobj.kref.refcount));
> 
> 	kobject_put(&b.kobj);
> 	printk("kobj B put, A kref %d, B kref %d\n",
> 	       atomic_read(&a.kobj.kref.refcount), atomic_read(&b.kobj.kref.refcount));
> 
> 	kobject_put(&a.kobj);
> 	printk("kobj A put, A kref %d, B kref %d\n",
> 	       atomic_read(&a.kobj.kref.refcount), atomic_read(&b.kobj.kref.refcount));
> 
> 	return 0;
> 
> out_a_dir:
> 	sysfs_remove_dir(&a.kobj);
> out_b:
> 	kobject_put(&b.kobj);
> out_a:
> 	kobject_put(&a.kobj);
> 	return err;
> }
> module_init(test_init);
> 
> static void a_release(struct kobject *kobj)
> {
> 	struct my_obj_a *a;
> 	
> 	printk("%s\n", __FUNCTION__);
> 	a = container_of(kobj, struct my_obj_a, kobj);
> 	sysfs_remove_dir(&a->kobj);

Woah, don't do that here, the kobject core already does this.  A release
function is for you to release the memory you have created with this
kobject, not to mess with sysfs.

So, don't do this and everything should work just fine.

thanks,

greg k-h

  parent reply	other threads:[~2006-03-16 16:53 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-16 11:41 [Bug? Report] kref problem Artem B. Bityutskiy
2006-03-16 16:47 ` Greg KH
2006-03-16 16:50   ` Greg KH
2006-03-16 16:50   ` Artem B. Bityutskiy
2006-03-16 16:53 ` Greg KH [this message]
2006-03-16 17:07   ` Artem B. Bityutskiy
2006-03-16 17:10     ` Artem B. Bityutskiy
2006-03-16 17:20       ` Greg KH
2006-03-16 18:00         ` Artem B. Bityutskiy
2006-03-16 18:10           ` Greg KH
2006-03-16 17:18     ` Greg KH
2006-03-16 17:31   ` Dave Hansen
2006-03-16 17:42     ` Greg KH
2006-03-16 17:45   ` Artem B. Bityutskiy
2006-03-16 17:58     ` Greg KH
2006-03-16 18:08       ` Artem B. Bityutskiy
2006-03-16 18:20         ` Greg KH
2006-03-17  9:30           ` Artem B. Bityutskiy
2006-03-17 15:18             ` Greg KH
2006-03-17 16:34               ` Artem B. Bityutskiy

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=20060316165323.GA10197@kroah.com \
    --to=greg@kroah.com \
    --cc=dedekind@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.de \
    /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